src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
author himmelma
Wed, 17 Feb 2010 18:33:45 +0100
changeset 35172 579dd5570f96
parent 35028 108662d50512
child 35820 b57c3afd1484
permissions -rw-r--r--
Added integration to Multivariate-Analysis (upto FTC)
hoelzl@33711
     1
(*  title:      HOL/Library/Topology_Euclidian_Space.thy
himmelma@33175
     2
    Author:     Amine Chaieb, University of Cambridge
himmelma@33175
     3
    Author:     Robert Himmelmann, TU Muenchen
himmelma@33175
     4
*)
himmelma@33175
     5
himmelma@33175
     6
header {* Elementary topology in Euclidean space. *}
himmelma@33175
     7
himmelma@33175
     8
theory Topology_Euclidean_Space
himmelma@33175
     9
imports SEQ Euclidean_Space Product_Vector
himmelma@33175
    10
begin
himmelma@33175
    11
himmelma@33175
    12
subsection{* General notion of a topology *}
himmelma@33175
    13
himmelma@33175
    14
definition "istopology L \<longleftrightarrow> {} \<in> L \<and> (\<forall>S \<in>L. \<forall>T \<in>L. S \<inter> T \<in> L) \<and> (\<forall>K. K \<subseteq>L \<longrightarrow> \<Union> K \<in> L)"
himmelma@33175
    15
typedef (open) 'a topology = "{L::('a set) set. istopology L}"
himmelma@33175
    16
  morphisms "openin" "topology"
himmelma@33175
    17
  unfolding istopology_def by blast
himmelma@33175
    18
himmelma@33175
    19
lemma istopology_open_in[intro]: "istopology(openin U)"
himmelma@33175
    20
  using openin[of U] by blast
himmelma@33175
    21
himmelma@33175
    22
lemma topology_inverse': "istopology U \<Longrightarrow> openin (topology U) = U"
himmelma@33175
    23
  using topology_inverse[unfolded mem_def Collect_def] .
himmelma@33175
    24
himmelma@33175
    25
lemma topology_inverse_iff: "istopology U \<longleftrightarrow> openin (topology U) = U"
himmelma@33175
    26
  using topology_inverse[of U] istopology_open_in[of "topology U"] by auto
himmelma@33175
    27
himmelma@33175
    28
lemma topology_eq: "T1 = T2 \<longleftrightarrow> (\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S)"
himmelma@33175
    29
proof-
himmelma@33175
    30
  {assume "T1=T2" hence "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S" by simp}
himmelma@33175
    31
  moreover
himmelma@33175
    32
  {assume H: "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S"
himmelma@33175
    33
    hence "openin T1 = openin T2" by (metis mem_def set_ext)
himmelma@33175
    34
    hence "topology (openin T1) = topology (openin T2)" by simp
himmelma@33175
    35
    hence "T1 = T2" unfolding openin_inverse .}
himmelma@33175
    36
  ultimately show ?thesis by blast
himmelma@33175
    37
qed
himmelma@33175
    38
himmelma@33175
    39
text{* Infer the "universe" from union of all sets in the topology. *}
himmelma@33175
    40
himmelma@33175
    41
definition "topspace T =  \<Union>{S. openin T S}"
himmelma@33175
    42
himmelma@33175
    43
subsection{* Main properties of open sets *}
himmelma@33175
    44
himmelma@33175
    45
lemma openin_clauses:
himmelma@33175
    46
  fixes U :: "'a topology"
himmelma@33175
    47
  shows "openin U {}"
himmelma@33175
    48
  "\<And>S T. openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S\<inter>T)"
himmelma@33175
    49
  "\<And>K. (\<forall>S \<in> K. openin U S) \<Longrightarrow> openin U (\<Union>K)"
himmelma@33175
    50
  using openin[of U] unfolding istopology_def Collect_def mem_def
himmelma@33175
    51
  by (metis mem_def subset_eq)+
himmelma@33175
    52
himmelma@33175
    53
lemma openin_subset[intro]: "openin U S \<Longrightarrow> S \<subseteq> topspace U"
himmelma@33175
    54
  unfolding topspace_def by blast
himmelma@33175
    55
lemma openin_empty[simp]: "openin U {}" by (simp add: openin_clauses)
himmelma@33175
    56
himmelma@33175
    57
lemma openin_Int[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<inter> T)"
himmelma@33175
    58
  by (simp add: openin_clauses)
himmelma@33175
    59
himmelma@33175
    60
lemma openin_Union[intro]: "(\<forall>S \<in>K. openin U S) \<Longrightarrow> openin U (\<Union> K)" by (simp add: openin_clauses)
himmelma@33175
    61
himmelma@33175
    62
lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"
himmelma@33175
    63
  using openin_Union[of "{S,T}" U] by auto
himmelma@33175
    64
himmelma@33175
    65
lemma openin_topspace[intro, simp]: "openin U (topspace U)" by (simp add: openin_Union topspace_def)
himmelma@33175
    66
himmelma@33175
    67
lemma openin_subopen: "openin U S \<longleftrightarrow> (\<forall>x \<in> S. \<exists>T. openin U T \<and> x \<in> T \<and> T \<subseteq> S)" (is "?lhs \<longleftrightarrow> ?rhs")
himmelma@33175
    68
proof-
himmelma@33175
    69
  {assume ?lhs then have ?rhs by auto }
himmelma@33175
    70
  moreover
himmelma@33175
    71
  {assume H: ?rhs
himmelma@33175
    72
    then obtain t where t: "\<forall>x\<in>S. openin U (t x) \<and> x \<in> t x \<and> t x \<subseteq> S"
himmelma@33175
    73
      unfolding Ball_def ex_simps(6)[symmetric] choice_iff by blast
himmelma@33175
    74
    from t have th0: "\<forall>x\<in> t`S. openin U x" by auto
himmelma@33175
    75
    have "\<Union> t`S = S" using t by auto
himmelma@33175
    76
    with openin_Union[OF th0] have "openin U S" by simp }
himmelma@33175
    77
  ultimately show ?thesis by blast
himmelma@33175
    78
qed
himmelma@33175
    79
himmelma@33175
    80
subsection{* Closed sets *}
himmelma@33175
    81
himmelma@33175
    82
definition "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"
himmelma@33175
    83
himmelma@33175
    84
lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U" by (metis closedin_def)
himmelma@33175
    85
lemma closedin_empty[simp]: "closedin U {}" by (simp add: closedin_def)
himmelma@33175
    86
lemma closedin_topspace[intro,simp]:
himmelma@33175
    87
  "closedin U (topspace U)" by (simp add: closedin_def)
himmelma@33175
    88
lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"
himmelma@33175
    89
  by (auto simp add: Diff_Un closedin_def)
himmelma@33175
    90
himmelma@33175
    91
lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union> {A - s|s. s\<in>S}" by auto
himmelma@33175
    92
lemma closedin_Inter[intro]: assumes Ke: "K \<noteq> {}" and Kc: "\<forall>S \<in>K. closedin U S"
himmelma@33175
    93
  shows "closedin U (\<Inter> K)"  using Ke Kc unfolding closedin_def Diff_Inter by auto
himmelma@33175
    94
himmelma@33175
    95
lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"
himmelma@33175
    96
  using closedin_Inter[of "{S,T}" U] by auto
himmelma@33175
    97
himmelma@33175
    98
lemma Diff_Diff_Int: "A - (A - B) = A \<inter> B" by blast
himmelma@33175
    99
lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"
himmelma@33175
   100
  apply (auto simp add: closedin_def Diff_Diff_Int inf_absorb2)
himmelma@33175
   101
  apply (metis openin_subset subset_eq)
himmelma@33175
   102
  done
himmelma@33175
   103
himmelma@33175
   104
lemma openin_closedin:  "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"
himmelma@33175
   105
  by (simp add: openin_closedin_eq)
himmelma@33175
   106
himmelma@33175
   107
lemma openin_diff[intro]: assumes oS: "openin U S" and cT: "closedin U T" shows "openin U (S - T)"
himmelma@33175
   108
proof-
himmelma@33175
   109
  have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT
himmelma@33175
   110
    by (auto simp add: topspace_def openin_subset)
himmelma@33175
   111
  then show ?thesis using oS cT by (auto simp add: closedin_def)
himmelma@33175
   112
qed
himmelma@33175
   113
himmelma@33175
   114
lemma closedin_diff[intro]: assumes oS: "closedin U S" and cT: "openin U T" shows "closedin U (S - T)"
himmelma@33175
   115
proof-
himmelma@33175
   116
  have "S - T = S \<inter> (topspace U - T)" using closedin_subset[of U S]  oS cT
himmelma@33175
   117
    by (auto simp add: topspace_def )
himmelma@33175
   118
  then show ?thesis using oS cT by (auto simp add: openin_closedin_eq)
himmelma@33175
   119
qed
himmelma@33175
   120
himmelma@33175
   121
subsection{* Subspace topology. *}
himmelma@33175
   122
himmelma@33175
   123
definition "subtopology U V = topology {S \<inter> V |S. openin U S}"
himmelma@33175
   124
himmelma@33175
   125
lemma istopology_subtopology: "istopology {S \<inter> V |S. openin U S}" (is "istopology ?L")
himmelma@33175
   126
proof-
himmelma@33175
   127
  have "{} \<in> ?L" by blast
himmelma@33175
   128
  {fix A B assume A: "A \<in> ?L" and B: "B \<in> ?L"
himmelma@33175
   129
    from A B obtain Sa and Sb where Sa: "openin U Sa" "A = Sa \<inter> V" and Sb: "openin U Sb" "B = Sb \<inter> V" by blast
himmelma@33175
   130
    have "A\<inter>B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"  using Sa Sb by blast+
himmelma@33175
   131
    then have "A \<inter> B \<in> ?L" by blast}
himmelma@33175
   132
  moreover
himmelma@33175
   133
  {fix K assume K: "K \<subseteq> ?L"
himmelma@33175
   134
    have th0: "?L = (\<lambda>S. S \<inter> V) ` openin U "
himmelma@33175
   135
      apply (rule set_ext)
himmelma@33175
   136
      apply (simp add: Ball_def image_iff)
himmelma@33175
   137
      by (metis mem_def)
himmelma@33175
   138
    from K[unfolded th0 subset_image_iff]
himmelma@33175
   139
    obtain Sk where Sk: "Sk \<subseteq> openin U" "K = (\<lambda>S. S \<inter> V) ` Sk" by blast
himmelma@33175
   140
    have "\<Union>K = (\<Union>Sk) \<inter> V" using Sk by auto
himmelma@33175
   141
    moreover have "openin U (\<Union> Sk)" using Sk by (auto simp add: subset_eq mem_def)
himmelma@33175
   142
    ultimately have "\<Union>K \<in> ?L" by blast}
himmelma@33175
   143
  ultimately show ?thesis unfolding istopology_def by blast
himmelma@33175
   144
qed
himmelma@33175
   145
himmelma@33175
   146
lemma openin_subtopology:
himmelma@33175
   147
  "openin (subtopology U V) S \<longleftrightarrow> (\<exists> T. (openin U T) \<and> (S = T \<inter> V))"
himmelma@33175
   148
  unfolding subtopology_def topology_inverse'[OF istopology_subtopology]
himmelma@33175
   149
  by (auto simp add: Collect_def)
himmelma@33175
   150
himmelma@33175
   151
lemma topspace_subtopology: "topspace(subtopology U V) = topspace U \<inter> V"
himmelma@33175
   152
  by (auto simp add: topspace_def openin_subtopology)
himmelma@33175
   153
himmelma@33175
   154
lemma closedin_subtopology:
himmelma@33175
   155
  "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"
himmelma@33175
   156
  unfolding closedin_def topspace_subtopology
himmelma@33175
   157
  apply (simp add: openin_subtopology)
himmelma@33175
   158
  apply (rule iffI)
himmelma@33175
   159
  apply clarify
himmelma@33175
   160
  apply (rule_tac x="topspace U - T" in exI)
himmelma@33175
   161
  by auto
himmelma@33175
   162
himmelma@33175
   163
lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"
himmelma@33175
   164
  unfolding openin_subtopology
himmelma@33175
   165
  apply (rule iffI, clarify)
himmelma@33175
   166
  apply (frule openin_subset[of U])  apply blast
himmelma@33175
   167
  apply (rule exI[where x="topspace U"])
himmelma@33175
   168
  by auto
himmelma@33175
   169
himmelma@33175
   170
lemma subtopology_superset: assumes UV: "topspace U \<subseteq> V"
himmelma@33175
   171
  shows "subtopology U V = U"
himmelma@33175
   172
proof-
himmelma@33175
   173
  {fix S
himmelma@33175
   174
    {fix T assume T: "openin U T" "S = T \<inter> V"
himmelma@33175
   175
      from T openin_subset[OF T(1)] UV have eq: "S = T" by blast
himmelma@33175
   176
      have "openin U S" unfolding eq using T by blast}
himmelma@33175
   177
    moreover
himmelma@33175
   178
    {assume S: "openin U S"
himmelma@33175
   179
      hence "\<exists>T. openin U T \<and> S = T \<inter> V"
himmelma@33175
   180
        using openin_subset[OF S] UV by auto}
himmelma@33175
   181
    ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S" by blast}
himmelma@33175
   182
  then show ?thesis unfolding topology_eq openin_subtopology by blast
himmelma@33175
   183
qed
himmelma@33175
   184
himmelma@33175
   185
himmelma@33175
   186
lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"
himmelma@33175
   187
  by (simp add: subtopology_superset)
himmelma@33175
   188
himmelma@33175
   189
lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"
himmelma@33175
   190
  by (simp add: subtopology_superset)
himmelma@33175
   191
himmelma@33175
   192
subsection{* The universal Euclidean versions are what we use most of the time *}
himmelma@33175
   193
himmelma@33175
   194
definition
himmelma@33175
   195
  euclidean :: "'a::topological_space topology" where
himmelma@33175
   196
  "euclidean = topology open"
himmelma@33175
   197
himmelma@33175
   198
lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"
himmelma@33175
   199
  unfolding euclidean_def
himmelma@33175
   200
  apply (rule cong[where x=S and y=S])
himmelma@33175
   201
  apply (rule topology_inverse[symmetric])
himmelma@33175
   202
  apply (auto simp add: istopology_def)
himmelma@33175
   203
  by (auto simp add: mem_def subset_eq)
himmelma@33175
   204
himmelma@33175
   205
lemma topspace_euclidean: "topspace euclidean = UNIV"
himmelma@33175
   206
  apply (simp add: topspace_def)
himmelma@33175
   207
  apply (rule set_ext)
himmelma@33175
   208
  by (auto simp add: open_openin[symmetric])
himmelma@33175
   209
himmelma@33175
   210
lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"
himmelma@33175
   211
  by (simp add: topspace_euclidean topspace_subtopology)
himmelma@33175
   212
himmelma@33175
   213
lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"
himmelma@33175
   214
  by (simp add: closed_def closedin_def topspace_euclidean open_openin Compl_eq_Diff_UNIV)
himmelma@33175
   215
himmelma@33175
   216
lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"
himmelma@33175
   217
  by (simp add: open_openin openin_subopen[symmetric])
himmelma@33175
   218
himmelma@33175
   219
subsection{* Open and closed balls. *}
himmelma@33175
   220
himmelma@33175
   221
definition
himmelma@33175
   222
  ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where
himmelma@33175
   223
  "ball x e = {y. dist x y < e}"
himmelma@33175
   224
himmelma@33175
   225
definition
himmelma@33175
   226
  cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where
himmelma@33175
   227
  "cball x e = {y. dist x y \<le> e}"
himmelma@33175
   228
himmelma@33175
   229
lemma mem_ball[simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e" by (simp add: ball_def)
himmelma@33175
   230
lemma mem_cball[simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e" by (simp add: cball_def)
himmelma@33175
   231
himmelma@33175
   232
lemma mem_ball_0 [simp]:
himmelma@33175
   233
  fixes x :: "'a::real_normed_vector"
himmelma@33175
   234
  shows "x \<in> ball 0 e \<longleftrightarrow> norm x < e"
himmelma@33175
   235
  by (simp add: dist_norm)
himmelma@33175
   236
himmelma@33175
   237
lemma mem_cball_0 [simp]:
himmelma@33175
   238
  fixes x :: "'a::real_normed_vector"
himmelma@33175
   239
  shows "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"
himmelma@33175
   240
  by (simp add: dist_norm)
himmelma@33175
   241
himmelma@33175
   242
lemma centre_in_cball[simp]: "x \<in> cball x e \<longleftrightarrow> 0\<le> e"  by simp
himmelma@33175
   243
lemma ball_subset_cball[simp,intro]: "ball x e \<subseteq> cball x e" by (simp add: subset_eq)
himmelma@33175
   244
lemma subset_ball[intro]: "d <= e ==> ball x d \<subseteq> ball x e" by (simp add: subset_eq)
himmelma@33175
   245
lemma subset_cball[intro]: "d <= e ==> cball x d \<subseteq> cball x e" by (simp add: subset_eq)
himmelma@33175
   246
lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"
himmelma@33175
   247
  by (simp add: expand_set_eq) arith
himmelma@33175
   248
himmelma@33175
   249
lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"
himmelma@33175
   250
  by (simp add: expand_set_eq)
himmelma@33175
   251
himmelma@33175
   252
subsection{* Topological properties of open balls *}
himmelma@33175
   253
himmelma@33175
   254
lemma diff_less_iff: "(a::real) - b > 0 \<longleftrightarrow> a > b"
himmelma@33175
   255
  "(a::real) - b < 0 \<longleftrightarrow> a < b"
himmelma@33175
   256
  "a - b < c \<longleftrightarrow> a < c +b" "a - b > c \<longleftrightarrow> a > c +b" by arith+
himmelma@33175
   257
lemma diff_le_iff: "(a::real) - b \<ge> 0 \<longleftrightarrow> a \<ge> b" "(a::real) - b \<le> 0 \<longleftrightarrow> a \<le> b"
himmelma@33175
   258
  "a - b \<le> c \<longleftrightarrow> a \<le> c +b" "a - b \<ge> c \<longleftrightarrow> a \<ge> c +b"  by arith+
himmelma@33175
   259
himmelma@33175
   260
lemma open_ball[intro, simp]: "open (ball x e)"
himmelma@33175
   261
  unfolding open_dist ball_def Collect_def Ball_def mem_def
himmelma@33175
   262
  unfolding dist_commute
himmelma@33175
   263
  apply clarify
himmelma@33175
   264
  apply (rule_tac x="e - dist xa x" in exI)
himmelma@33175
   265
  using dist_triangle_alt[where z=x]
himmelma@33175
   266
  apply (clarsimp simp add: diff_less_iff)
himmelma@33175
   267
  apply atomize
himmelma@33175
   268
  apply (erule_tac x="y" in allE)
himmelma@33175
   269
  apply (erule_tac x="xa" in allE)
himmelma@33175
   270
  by arith
himmelma@33175
   271
himmelma@33175
   272
lemma centre_in_ball[simp]: "x \<in> ball x e \<longleftrightarrow> e > 0" by (metis mem_ball dist_self)
himmelma@33175
   273
lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"
himmelma@33175
   274
  unfolding open_dist subset_eq mem_ball Ball_def dist_commute ..
himmelma@33175
   275
hoelzl@33711
   276
lemma openE[elim?]:
hoelzl@33711
   277
  assumes "open S" "x\<in>S" 
hoelzl@33711
   278
  obtains e where "e>0" "ball x e \<subseteq> S"
hoelzl@33711
   279
  using assms unfolding open_contains_ball by auto
hoelzl@33711
   280
himmelma@33175
   281
lemma open_contains_ball_eq: "open S \<Longrightarrow> \<forall>x. x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
himmelma@33175
   282
  by (metis open_contains_ball subset_eq centre_in_ball)
himmelma@33175
   283
himmelma@33175
   284
lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"
himmelma@33175
   285
  unfolding mem_ball expand_set_eq
himmelma@33175
   286
  apply (simp add: not_less)
himmelma@33175
   287
  by (metis zero_le_dist order_trans dist_self)
himmelma@33175
   288
himmelma@33175
   289
lemma ball_empty[intro]: "e \<le> 0 ==> ball x e = {}" by simp
himmelma@33175
   290
himmelma@33175
   291
subsection{* Basic "localization" results are handy for connectedness. *}
himmelma@33175
   292
himmelma@33175
   293
lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"
himmelma@33175
   294
  by (auto simp add: openin_subtopology open_openin[symmetric])
himmelma@33175
   295
himmelma@33175
   296
lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"
himmelma@33175
   297
  by (auto simp add: openin_open)
himmelma@33175
   298
himmelma@33175
   299
lemma open_openin_trans[trans]:
himmelma@33175
   300
 "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"
himmelma@33175
   301
  by (metis Int_absorb1  openin_open_Int)
himmelma@33175
   302
himmelma@33175
   303
lemma open_subset:  "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"
himmelma@33175
   304
  by (auto simp add: openin_open)
himmelma@33175
   305
himmelma@33175
   306
lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"
himmelma@33175
   307
  by (simp add: closedin_subtopology closed_closedin Int_ac)
himmelma@33175
   308
himmelma@33175
   309
lemma closedin_closed_Int: "closed S ==> closedin (subtopology euclidean U) (U \<inter> S)"
himmelma@33175
   310
  by (metis closedin_closed)
himmelma@33175
   311
himmelma@33175
   312
lemma closed_closedin_trans: "closed S \<Longrightarrow> closed T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> closedin (subtopology euclidean S) T"
himmelma@33175
   313
  apply (subgoal_tac "S \<inter> T = T" )
himmelma@33175
   314
  apply auto
himmelma@33175
   315
  apply (frule closedin_closed_Int[of T S])
himmelma@33175
   316
  by simp
himmelma@33175
   317
himmelma@33175
   318
lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"
himmelma@33175
   319
  by (auto simp add: closedin_closed)
himmelma@33175
   320
himmelma@33175
   321
lemma openin_euclidean_subtopology_iff:
himmelma@33175
   322
  fixes S U :: "'a::metric_space set"
himmelma@33175
   323
  shows "openin (subtopology euclidean U) S
himmelma@33175
   324
  \<longleftrightarrow> S \<subseteq> U \<and> (\<forall>x\<in>S. \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x'\<in> S)" (is "?lhs \<longleftrightarrow> ?rhs")
himmelma@33175
   325
proof-
himmelma@33175
   326
  {assume ?lhs hence ?rhs unfolding openin_subtopology open_openin[symmetric]
himmelma@33175
   327
      by (simp add: open_dist) blast}
himmelma@33175
   328
  moreover
himmelma@33175
   329
  {assume SU: "S \<subseteq> U" and H: "\<And>x. x \<in> S \<Longrightarrow> \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x' \<in> S"
himmelma@33175
   330
    from H obtain d where d: "\<And>x . x\<in> S \<Longrightarrow> d x > 0 \<and> (\<forall>x' \<in> U. dist x' x < d x \<longrightarrow> x' \<in> S)"
himmelma@33175
   331
      by metis
himmelma@33175
   332
    let ?T = "\<Union>{B. \<exists>x\<in>S. B = ball x (d x)}"
himmelma@33175
   333
    have oT: "open ?T" by auto
himmelma@33175
   334
    { fix x assume "x\<in>S"
himmelma@33175
   335
      hence "x \<in> \<Union>{B. \<exists>x\<in>S. B = ball x (d x)}"
himmelma@33175
   336
        apply simp apply(rule_tac x="ball x(d x)" in exI) apply auto
himmelma@33175
   337
        by (rule d [THEN conjunct1])
himmelma@33175
   338
      hence "x\<in> ?T \<inter> U" using SU and `x\<in>S` by auto  }
himmelma@33175
   339
    moreover
himmelma@33175
   340
    { fix y assume "y\<in>?T"
himmelma@33175
   341
      then obtain B where "y\<in>B" "B\<in>{B. \<exists>x\<in>S. B = ball x (d x)}" by auto
himmelma@33175
   342
      then obtain x where "x\<in>S" and x:"y \<in> ball x (d x)" by auto
himmelma@33175
   343
      assume "y\<in>U"
himmelma@33175
   344
      hence "y\<in>S" using d[OF `x\<in>S`] and x by(auto simp add: dist_commute) }
himmelma@33175
   345
    ultimately have "S = ?T \<inter> U" by blast
himmelma@33175
   346
    with oT have ?lhs unfolding openin_subtopology open_openin[symmetric] by blast}
himmelma@33175
   347
  ultimately show ?thesis by blast
himmelma@33175
   348
qed
himmelma@33175
   349
himmelma@33175
   350
text{* These "transitivity" results are handy too. *}
himmelma@33175
   351
himmelma@33175
   352
lemma openin_trans[trans]: "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T
himmelma@33175
   353
  \<Longrightarrow> openin (subtopology euclidean U) S"
himmelma@33175
   354
  unfolding open_openin openin_open by blast
himmelma@33175
   355
himmelma@33175
   356
lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"
himmelma@33175
   357
  by (auto simp add: openin_open intro: openin_trans)
himmelma@33175
   358
himmelma@33175
   359
lemma closedin_trans[trans]:
himmelma@33175
   360
 "closedin (subtopology euclidean T) S \<Longrightarrow>
himmelma@33175
   361
           closedin (subtopology euclidean U) T
himmelma@33175
   362
           ==> closedin (subtopology euclidean U) S"
himmelma@33175
   363
  by (auto simp add: closedin_closed closed_closedin closed_Inter Int_assoc)
himmelma@33175
   364
himmelma@33175
   365
lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"
himmelma@33175
   366
  by (auto simp add: closedin_closed intro: closedin_trans)
himmelma@33175
   367
himmelma@33175
   368
subsection{* Connectedness *}
himmelma@33175
   369
himmelma@33175
   370
definition "connected S \<longleftrightarrow>
himmelma@33175
   371
  ~(\<exists>e1 e2. open e1 \<and> open e2 \<and> S \<subseteq> (e1 \<union> e2) \<and> (e1 \<inter> e2 \<inter> S = {})
himmelma@33175
   372
  \<and> ~(e1 \<inter> S = {}) \<and> ~(e2 \<inter> S = {}))"
himmelma@33175
   373
himmelma@33175
   374
lemma connected_local:
himmelma@33175
   375
 "connected S \<longleftrightarrow> ~(\<exists>e1 e2.
himmelma@33175
   376
                 openin (subtopology euclidean S) e1 \<and>
himmelma@33175
   377
                 openin (subtopology euclidean S) e2 \<and>
himmelma@33175
   378
                 S \<subseteq> e1 \<union> e2 \<and>
himmelma@33175
   379
                 e1 \<inter> e2 = {} \<and>
himmelma@33175
   380
                 ~(e1 = {}) \<and>
himmelma@33175
   381
                 ~(e2 = {}))"
himmelma@33175
   382
unfolding connected_def openin_open by (safe, blast+)
himmelma@33175
   383
huffman@34099
   384
lemma exists_diff:
huffman@34099
   385
  fixes P :: "'a set \<Rightarrow> bool"
huffman@34099
   386
  shows "(\<exists>S. P(- S)) \<longleftrightarrow> (\<exists>S. P S)" (is "?lhs \<longleftrightarrow> ?rhs")
himmelma@33175
   387
proof-
himmelma@33175
   388
  {assume "?lhs" hence ?rhs by blast }
himmelma@33175
   389
  moreover
himmelma@33175
   390
  {fix S assume H: "P S"
huffman@34099
   391
    have "S = - (- S)" by auto
huffman@34099
   392
    with H have "P (- (- S))" by metis }
himmelma@33175
   393
  ultimately show ?thesis by metis
himmelma@33175
   394
qed
himmelma@33175
   395
himmelma@33175
   396
lemma connected_clopen: "connected S \<longleftrightarrow>
himmelma@33175
   397
        (\<forall>T. openin (subtopology euclidean S) T \<and>
himmelma@33175
   398
            closedin (subtopology euclidean S) T \<longrightarrow> T = {} \<or> T = S)" (is "?lhs \<longleftrightarrow> ?rhs")
himmelma@33175
   399
proof-
huffman@34099
   400
  have " \<not> connected S \<longleftrightarrow> (\<exists>e1 e2. open e1 \<and> open (- e2) \<and> S \<subseteq> e1 \<union> (- e2) \<and> e1 \<inter> (- e2) \<inter> S = {} \<and> e1 \<inter> S \<noteq> {} \<and> (- e2) \<inter> S \<noteq> {})"
himmelma@33175
   401
    unfolding connected_def openin_open closedin_closed
himmelma@33175
   402
    apply (subst exists_diff) by blast
huffman@34099
   403
  hence th0: "connected S \<longleftrightarrow> \<not> (\<exists>e2 e1. closed e2 \<and> open e1 \<and> S \<subseteq> e1 \<union> (- e2) \<and> e1 \<inter> (- e2) \<inter> S = {} \<and> e1 \<inter> S \<noteq> {} \<and> (- e2) \<inter> S \<noteq> {})"
huffman@34099
   404
    (is " _ \<longleftrightarrow> \<not> (\<exists>e2 e1. ?P e2 e1)") apply (simp add: closed_def) by metis
himmelma@33175
   405
himmelma@33175
   406
  have th1: "?rhs \<longleftrightarrow> \<not> (\<exists>t' t. closed t'\<and>t = S\<inter>t' \<and> t\<noteq>{} \<and> t\<noteq>S \<and> (\<exists>t'. open t' \<and> t = S \<inter> t'))"
himmelma@33175
   407
    (is "_ \<longleftrightarrow> \<not> (\<exists>t' t. ?Q t' t)")
himmelma@33175
   408
    unfolding connected_def openin_open closedin_closed by auto
himmelma@33175
   409
  {fix e2
himmelma@33175
   410
    {fix e1 have "?P e2 e1 \<longleftrightarrow> (\<exists>t.  closed e2 \<and> t = S\<inter>e2 \<and> open e1 \<and> t = S\<inter>e1 \<and> t\<noteq>{} \<and> t\<noteq>S)"
himmelma@33175
   411
        by auto}
himmelma@33175
   412
    then have "(\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by metis}
himmelma@33175
   413
  then have "\<forall>e2. (\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by blast
himmelma@33175
   414
  then show ?thesis unfolding th0 th1 by simp
himmelma@33175
   415
qed
himmelma@33175
   416
himmelma@33175
   417
lemma connected_empty[simp, intro]: "connected {}"
himmelma@33175
   418
  by (simp add: connected_def)
himmelma@33175
   419
himmelma@33175
   420
subsection{* Hausdorff and other separation properties *}
himmelma@33175
   421
himmelma@33175
   422
class t0_space =
himmelma@33175
   423
  assumes t0_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U)"
himmelma@33175
   424
himmelma@33175
   425
class t1_space =
himmelma@33175
   426
  assumes t1_space: "x \<noteq> y \<Longrightarrow> \<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<notin> U \<and> x \<notin> V \<and> y \<in> V"
himmelma@33175
   427
begin
himmelma@33175
   428
himmelma@33175
   429
subclass t0_space
himmelma@33175
   430
proof
himmelma@33175
   431
qed (fast dest: t1_space)
himmelma@33175
   432
himmelma@33175
   433
end
himmelma@33175
   434
himmelma@33175
   435
text {* T2 spaces are also known as Hausdorff spaces. *}
himmelma@33175
   436
himmelma@33175
   437
class t2_space =
himmelma@33175
   438
  assumes hausdorff: "x \<noteq> y \<Longrightarrow> \<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
himmelma@33175
   439
begin
himmelma@33175
   440
himmelma@33175
   441
subclass t1_space
himmelma@33175
   442
proof
himmelma@33175
   443
qed (fast dest: hausdorff)
himmelma@33175
   444
himmelma@33175
   445
end
himmelma@33175
   446
himmelma@33175
   447
instance metric_space \<subseteq> t2_space
himmelma@33175
   448
proof
himmelma@33175
   449
  fix x y :: "'a::metric_space"
himmelma@33175
   450
  assume xy: "x \<noteq> y"
himmelma@33175
   451
  let ?U = "ball x (dist x y / 2)"
himmelma@33175
   452
  let ?V = "ball y (dist x y / 2)"
himmelma@33175
   453
  have th0: "\<And>d x y z. (d x z :: real) <= d x y + d y z \<Longrightarrow> d y z = d z y
himmelma@33175
   454
               ==> ~(d x y * 2 < d x z \<and> d z y * 2 < d x z)" by arith
himmelma@33175
   455
  have "open ?U \<and> open ?V \<and> x \<in> ?U \<and> y \<in> ?V \<and> ?U \<inter> ?V = {}"
himmelma@33175
   456
    using dist_pos_lt[OF xy] th0[of dist,OF dist_triangle dist_commute]
himmelma@33175
   457
    by (auto simp add: expand_set_eq)
himmelma@33175
   458
  then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
himmelma@33175
   459
    by blast
himmelma@33175
   460
qed
himmelma@33175
   461
himmelma@33175
   462
lemma separation_t2:
himmelma@33175
   463
  fixes x y :: "'a::t2_space"
himmelma@33175
   464
  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {})"
himmelma@33175
   465
  using hausdorff[of x y] by blast
himmelma@33175
   466
himmelma@33175
   467
lemma separation_t1:
himmelma@33175
   468
  fixes x y :: "'a::t1_space"
himmelma@33175
   469
  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U V. open U \<and> open V \<and> x \<in>U \<and> y\<notin> U \<and> x\<notin>V \<and> y\<in>V)"
himmelma@33175
   470
  using t1_space[of x y] by blast
himmelma@33175
   471
himmelma@33175
   472
lemma separation_t0:
himmelma@33175
   473
  fixes x y :: "'a::t0_space"
himmelma@33175
   474
  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> ~(x\<in>U \<longleftrightarrow> y\<in>U))"
himmelma@33175
   475
  using t0_space[of x y] by blast
himmelma@33175
   476
himmelma@33175
   477
subsection{* Limit points *}
himmelma@33175
   478
himmelma@33175
   479
definition
himmelma@33175
   480
  islimpt:: "'a::topological_space \<Rightarrow> 'a set \<Rightarrow> bool"
himmelma@33175
   481
    (infixr "islimpt" 60) where
himmelma@33175
   482
  "x islimpt S \<longleftrightarrow> (\<forall>T. x\<in>T \<longrightarrow> open T \<longrightarrow> (\<exists>y\<in>S. y\<in>T \<and> y\<noteq>x))"
himmelma@33175
   483
himmelma@33175
   484
lemma islimptI:
himmelma@33175
   485
  assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
himmelma@33175
   486
  shows "x islimpt S"
himmelma@33175
   487
  using assms unfolding islimpt_def by auto
himmelma@33175
   488
himmelma@33175
   489
lemma islimptE:
himmelma@33175
   490
  assumes "x islimpt S" and "x \<in> T" and "open T"
himmelma@33175
   491
  obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"
himmelma@33175
   492
  using assms unfolding islimpt_def by auto
himmelma@33175
   493
himmelma@33175
   494
lemma islimpt_subset: "x islimpt S \<Longrightarrow> S \<subseteq> T ==> x islimpt T" by (auto simp add: islimpt_def)
himmelma@33175
   495
himmelma@33175
   496
lemma islimpt_approachable:
himmelma@33175
   497
  fixes x :: "'a::metric_space"
himmelma@33175
   498
  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"
himmelma@33175
   499
  unfolding islimpt_def
himmelma@33175
   500
  apply auto
himmelma@33175
   501
  apply(erule_tac x="ball x e" in allE)
himmelma@33175
   502
  apply auto
himmelma@33175
   503
  apply(rule_tac x=y in bexI)
himmelma@33175
   504
  apply (auto simp add: dist_commute)
himmelma@33175
   505
  apply (simp add: open_dist, drule (1) bspec)
himmelma@33175
   506
  apply (clarify, drule spec, drule (1) mp, auto)
himmelma@33175
   507
  done
himmelma@33175
   508
himmelma@33175
   509
lemma islimpt_approachable_le:
himmelma@33175
   510
  fixes x :: "'a::metric_space"
himmelma@33175
   511
  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> S. x' \<noteq> x \<and> dist x' x <= e)"
himmelma@33175
   512
  unfolding islimpt_approachable
himmelma@33175
   513
  using approachable_lt_le[where f="\<lambda>x'. dist x' x" and P="\<lambda>x'. \<not> (x'\<in>S \<and> x'\<noteq>x)"]
paulson@33307
   514
  by metis 
himmelma@33175
   515
himmelma@33175
   516
class perfect_space =
himmelma@33175
   517
  (* FIXME: perfect_space should inherit from topological_space *)
himmelma@33175
   518
  assumes islimpt_UNIV [simp, intro]: "(x::'a::metric_space) islimpt UNIV"
himmelma@33175
   519
himmelma@33175
   520
lemma perfect_choose_dist:
himmelma@33175
   521
  fixes x :: "'a::perfect_space"
himmelma@33175
   522
  shows "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"
himmelma@33175
   523
using islimpt_UNIV [of x]
himmelma@33175
   524
by (simp add: islimpt_approachable)
himmelma@33175
   525
himmelma@33175
   526
instance real :: perfect_space
himmelma@33175
   527
apply default
himmelma@33175
   528
apply (rule islimpt_approachable [THEN iffD2])
himmelma@33175
   529
apply (clarify, rule_tac x="x + e/2" in bexI)
himmelma@33175
   530
apply (auto simp add: dist_norm)
himmelma@33175
   531
done
himmelma@33175
   532
hoelzl@34291
   533
instance cart :: (perfect_space, finite) perfect_space
himmelma@33175
   534
proof
himmelma@33175
   535
  fix x :: "'a ^ 'b"
himmelma@33175
   536
  {
himmelma@33175
   537
    fix e :: real assume "0 < e"
himmelma@33175
   538
    def a \<equiv> "x $ undefined"
himmelma@33175
   539
    have "a islimpt UNIV" by (rule islimpt_UNIV)
himmelma@33175
   540
    with `0 < e` obtain b where "b \<noteq> a" and "dist b a < e"
himmelma@33175
   541
      unfolding islimpt_approachable by auto
himmelma@33175
   542
    def y \<equiv> "Cart_lambda ((Cart_nth x)(undefined := b))"
himmelma@33175
   543
    from `b \<noteq> a` have "y \<noteq> x"
himmelma@33175
   544
      unfolding a_def y_def by (simp add: Cart_eq)
himmelma@33175
   545
    from `dist b a < e` have "dist y x < e"
himmelma@33175
   546
      unfolding dist_vector_def a_def y_def
himmelma@33175
   547
      apply simp
himmelma@33175
   548
      apply (rule le_less_trans [OF setL2_le_setsum [OF zero_le_dist]])
himmelma@33175
   549
      apply (subst setsum_diff1' [where a=undefined], simp, simp, simp)
himmelma@33175
   550
      done
himmelma@33175
   551
    from `y \<noteq> x` and `dist y x < e`
himmelma@33175
   552
    have "\<exists>y\<in>UNIV. y \<noteq> x \<and> dist y x < e" by auto
himmelma@33175
   553
  }
himmelma@33175
   554
  then show "x islimpt UNIV" unfolding islimpt_approachable by blast
himmelma@33175
   555
qed
himmelma@33175
   556
himmelma@33175
   557
lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"
himmelma@33175
   558
  unfolding closed_def
himmelma@33175
   559
  apply (subst open_subopen)
huffman@34099
   560
  apply (simp add: islimpt_def subset_eq)
huffman@34099
   561
  by (metis ComplE ComplI insertCI insert_absorb mem_def)
himmelma@33175
   562
himmelma@33175
   563
lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"
himmelma@33175
   564
  unfolding islimpt_def by auto
himmelma@33175
   565
hoelzl@34291
   566
lemma closed_positive_orthant: "closed {x::real^'n. \<forall>i. 0 \<le>x$i}"
himmelma@33175
   567
proof-
himmelma@33175
   568
  let ?U = "UNIV :: 'n set"
himmelma@33175
   569
  let ?O = "{x::real^'n. \<forall>i. x$i\<ge>0}"
himmelma@33175
   570
  {fix x:: "real^'n" and i::'n assume H: "\<forall>e>0. \<exists>x'\<in>?O. x' \<noteq> x \<and> dist x' x < e"
himmelma@33175
   571
    and xi: "x$i < 0"
himmelma@33175
   572
    from xi have th0: "-x$i > 0" by arith
himmelma@33175
   573
    from H[rule_format, OF th0] obtain x' where x': "x' \<in>?O" "x' \<noteq> x" "dist x' x < -x $ i" by blast
himmelma@33175
   574
      have th:" \<And>b a (x::real). abs x <= b \<Longrightarrow> b <= a ==> ~(a + x < 0)" by arith
himmelma@33175
   575
      have th': "\<And>x (y::real). x < 0 \<Longrightarrow> 0 <= y ==> abs x <= abs (y - x)" by arith
himmelma@33175
   576
      have th1: "\<bar>x$i\<bar> \<le> \<bar>(x' - x)$i\<bar>" using x'(1) xi
himmelma@33175
   577
        apply (simp only: vector_component)
himmelma@33175
   578
        by (rule th') auto
himmelma@33175
   579
      have th2: "\<bar>dist x x'\<bar> \<ge> \<bar>(x' - x)$i\<bar>" using  component_le_norm[of "x'-x" i]
himmelma@33175
   580
        apply (simp add: dist_norm) by norm
himmelma@33175
   581
      from th[OF th1 th2] x'(3) have False by (simp add: dist_commute) }
himmelma@33175
   582
  then show ?thesis unfolding closed_limpt islimpt_approachable
himmelma@33175
   583
    unfolding not_le[symmetric] by blast
himmelma@33175
   584
qed
himmelma@33175
   585
himmelma@33175
   586
lemma finite_set_avoid:
himmelma@33175
   587
  fixes a :: "'a::metric_space"
himmelma@33175
   588
  assumes fS: "finite S" shows  "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d <= dist a x"
himmelma@33175
   589
proof(induct rule: finite_induct[OF fS])
himmelma@33175
   590
  case 1 thus ?case apply auto by ferrack
himmelma@33175
   591
next
himmelma@33175
   592
  case (2 x F)
himmelma@33175
   593
  from 2 obtain d where d: "d >0" "\<forall>x\<in>F. x\<noteq>a \<longrightarrow> d \<le> dist a x" by blast
himmelma@33175
   594
  {assume "x = a" hence ?case using d by auto  }
himmelma@33175
   595
  moreover
himmelma@33175
   596
  {assume xa: "x\<noteq>a"
himmelma@33175
   597
    let ?d = "min d (dist a x)"
himmelma@33175
   598
    have dp: "?d > 0" using xa d(1) using dist_nz by auto
himmelma@33175
   599
    from d have d': "\<forall>x\<in>F. x\<noteq>a \<longrightarrow> ?d \<le> dist a x" by auto
himmelma@33175
   600
    with dp xa have ?case by(auto intro!: exI[where x="?d"]) }
himmelma@33175
   601
  ultimately show ?case by blast
himmelma@33175
   602
qed
himmelma@33175
   603
himmelma@33175
   604
lemma islimpt_finite:
himmelma@33175
   605
  fixes S :: "'a::metric_space set"
himmelma@33175
   606
  assumes fS: "finite S" shows "\<not> a islimpt S"
himmelma@33175
   607
  unfolding islimpt_approachable
himmelma@33175
   608
  using finite_set_avoid[OF fS, of a] by (metis dist_commute  not_le)
himmelma@33175
   609
himmelma@33175
   610
lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"
himmelma@33175
   611
  apply (rule iffI)
himmelma@33175
   612
  defer
himmelma@33175
   613
  apply (metis Un_upper1 Un_upper2 islimpt_subset)
himmelma@33175
   614
  unfolding islimpt_def
himmelma@33175
   615
  apply (rule ccontr, clarsimp, rename_tac A B)
himmelma@33175
   616
  apply (drule_tac x="A \<inter> B" in spec)
himmelma@33175
   617
  apply (auto simp add: open_Int)
himmelma@33175
   618
  done
himmelma@33175
   619
himmelma@33175
   620
lemma discrete_imp_closed:
himmelma@33175
   621
  fixes S :: "'a::metric_space set"
himmelma@33175
   622
  assumes e: "0 < e" and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"
himmelma@33175
   623
  shows "closed S"
himmelma@33175
   624
proof-
himmelma@33175
   625
  {fix x assume C: "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e"
himmelma@33175
   626
    from e have e2: "e/2 > 0" by arith
himmelma@33175
   627
    from C[rule_format, OF e2] obtain y where y: "y \<in> S" "y\<noteq>x" "dist y x < e/2" by blast
himmelma@33175
   628
    let ?m = "min (e/2) (dist x y) "
himmelma@33175
   629
    from e2 y(2) have mp: "?m > 0" by (simp add: dist_nz[THEN sym])
himmelma@33175
   630
    from C[rule_format, OF mp] obtain z where z: "z \<in> S" "z\<noteq>x" "dist z x < ?m" by blast
himmelma@33175
   631
    have th: "dist z y < e" using z y
himmelma@33175
   632
      by (intro dist_triangle_lt [where z=x], simp)
himmelma@33175
   633
    from d[rule_format, OF y(1) z(1) th] y z
himmelma@33175
   634
    have False by (auto simp add: dist_commute)}
himmelma@33175
   635
  then show ?thesis by (metis islimpt_approachable closed_limpt [where 'a='a])
himmelma@33175
   636
qed
himmelma@33175
   637
himmelma@33175
   638
subsection{* Interior of a Set *}
himmelma@33175
   639
definition "interior S = {x. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S}"
himmelma@33175
   640
himmelma@33175
   641
lemma interior_eq: "interior S = S \<longleftrightarrow> open S"
himmelma@33175
   642
  apply (simp add: expand_set_eq interior_def)
himmelma@33175
   643
  apply (subst (2) open_subopen) by (safe, blast+)
himmelma@33175
   644
himmelma@33175
   645
lemma interior_open: "open S ==> (interior S = S)" by (metis interior_eq)
himmelma@33175
   646
himmelma@33175
   647
lemma interior_empty[simp]: "interior {} = {}" by (simp add: interior_def)
himmelma@33175
   648
himmelma@33175
   649
lemma open_interior[simp, intro]: "open(interior S)"
himmelma@33175
   650
  apply (simp add: interior_def)
himmelma@33175
   651
  apply (subst open_subopen) by blast
himmelma@33175
   652
himmelma@33175
   653
lemma interior_interior[simp]: "interior(interior S) = interior S" by (metis interior_eq open_interior)
himmelma@33175
   654
lemma interior_subset: "interior S \<subseteq> S" by (auto simp add: interior_def)
himmelma@33175
   655
lemma subset_interior: "S \<subseteq> T ==> (interior S) \<subseteq> (interior T)" by (auto simp add: interior_def)
himmelma@33175
   656
lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T ==> T \<subseteq> (interior S)" by (auto simp add: interior_def)
himmelma@33175
   657
lemma interior_unique: "T \<subseteq> S \<Longrightarrow> open T  \<Longrightarrow> (\<forall>T'. T' \<subseteq> S \<and> open T' \<longrightarrow> T' \<subseteq> T) \<Longrightarrow> interior S = T"
himmelma@33175
   658
  by (metis equalityI interior_maximal interior_subset open_interior)
himmelma@33175
   659
lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e. 0 < e \<and> ball x e \<subseteq> S)"
himmelma@33175
   660
  apply (simp add: interior_def)
himmelma@33175
   661
  by (metis open_contains_ball centre_in_ball open_ball subset_trans)
himmelma@33175
   662
himmelma@33175
   663
lemma open_subset_interior: "open S ==> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"
himmelma@33175
   664
  by (metis interior_maximal interior_subset subset_trans)
himmelma@33175
   665
himmelma@33175
   666
lemma interior_inter[simp]: "interior(S \<inter> T) = interior S \<inter> interior T"
himmelma@33175
   667
  apply (rule equalityI, simp)
himmelma@33175
   668
  apply (metis Int_lower1 Int_lower2 subset_interior)
himmelma@33175
   669
  by (metis Int_mono interior_subset open_Int open_interior open_subset_interior)
himmelma@33175
   670
himmelma@33175
   671
lemma interior_limit_point [intro]:
himmelma@33175
   672
  fixes x :: "'a::perfect_space"
himmelma@33175
   673
  assumes x: "x \<in> interior S" shows "x islimpt S"
himmelma@33175
   674
proof-
himmelma@33175
   675
  from x obtain e where e: "e>0" "\<forall>x'. dist x x' < e \<longrightarrow> x' \<in> S"
himmelma@33175
   676
    unfolding mem_interior subset_eq Ball_def mem_ball by blast
himmelma@33175
   677
  {
himmelma@33175
   678
    fix d::real assume d: "d>0"
himmelma@33175
   679
    let ?m = "min d e"
himmelma@33175
   680
    have mde2: "0 < ?m" using e(1) d(1) by simp
himmelma@33175
   681
    from perfect_choose_dist [OF mde2, of x]
himmelma@33175
   682
    obtain y where "y \<noteq> x" and "dist y x < ?m" by blast
himmelma@33175
   683
    then have "dist y x < e" "dist y x < d" by simp_all
himmelma@33175
   684
    from `dist y x < e` e(2) have "y \<in> S" by (simp add: dist_commute)
himmelma@33175
   685
    have "\<exists>x'\<in>S. x'\<noteq> x \<and> dist x' x < d"
himmelma@33175
   686
      using `y \<in> S` `y \<noteq> x` `dist y x < d` by fast
himmelma@33175
   687
  }
himmelma@33175
   688
  then show ?thesis unfolding islimpt_approachable by blast
himmelma@33175
   689
qed
himmelma@33175
   690
himmelma@33175
   691
lemma interior_closed_Un_empty_interior:
himmelma@33175
   692
  assumes cS: "closed S" and iT: "interior T = {}"
himmelma@33175
   693
  shows "interior(S \<union> T) = interior S"
himmelma@33175
   694
proof
himmelma@33175
   695
  show "interior S \<subseteq> interior (S\<union>T)"
himmelma@33175
   696
    by (rule subset_interior, blast)
himmelma@33175
   697
next
himmelma@33175
   698
  show "interior (S \<union> T) \<subseteq> interior S"
himmelma@33175
   699
  proof
himmelma@33175
   700
    fix x assume "x \<in> interior (S \<union> T)"
himmelma@33175
   701
    then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T"
himmelma@33175
   702
      unfolding interior_def by fast
himmelma@33175
   703
    show "x \<in> interior S"
himmelma@33175
   704
    proof (rule ccontr)
himmelma@33175
   705
      assume "x \<notin> interior S"
himmelma@33175
   706
      with `x \<in> R` `open R` obtain y where "y \<in> R - S"
himmelma@33175
   707
        unfolding interior_def expand_set_eq by fast
himmelma@33175
   708
      from `open R` `closed S` have "open (R - S)" by (rule open_Diff)
himmelma@33175
   709
      from `R \<subseteq> S \<union> T` have "R - S \<subseteq> T" by fast
himmelma@33175
   710
      from `y \<in> R - S` `open (R - S)` `R - S \<subseteq> T` `interior T = {}`
himmelma@33175
   711
      show "False" unfolding interior_def by fast
himmelma@33175
   712
    qed
himmelma@33175
   713
  qed
himmelma@33175
   714
qed
himmelma@33175
   715
himmelma@33175
   716
himmelma@33175
   717
subsection{* Closure of a Set *}
himmelma@33175
   718
himmelma@33175
   719
definition "closure S = S \<union> {x | x. x islimpt S}"
himmelma@33175
   720
huffman@34099
   721
lemma closure_interior: "closure S = - interior (- S)"
himmelma@33175
   722
proof-
himmelma@33175
   723
  { fix x
huffman@34099
   724
    have "x\<in>- interior (- S) \<longleftrightarrow> x \<in> closure S"  (is "?lhs = ?rhs")
himmelma@33175
   725
    proof
huffman@34099
   726
      let ?exT = "\<lambda> y. (\<exists>T. open T \<and> y \<in> T \<and> T \<subseteq> - S)"
himmelma@33175
   727
      assume "?lhs"
himmelma@33175
   728
      hence *:"\<not> ?exT x"
himmelma@33175
   729
        unfolding interior_def
himmelma@33175
   730
        by simp
himmelma@33175
   731
      { assume "\<not> ?rhs"
himmelma@33175
   732
        hence False using *
himmelma@33175
   733
          unfolding closure_def islimpt_def
himmelma@33175
   734
          by blast
himmelma@33175
   735
      }
himmelma@33175
   736
      thus "?rhs"
himmelma@33175
   737
        by blast
himmelma@33175
   738
    next
himmelma@33175
   739
      assume "?rhs" thus "?lhs"
himmelma@33175
   740
        unfolding closure_def interior_def islimpt_def
himmelma@33175
   741
        by blast
himmelma@33175
   742
    qed
himmelma@33175
   743
  }
himmelma@33175
   744
  thus ?thesis
himmelma@33175
   745
    by blast
himmelma@33175
   746
qed
himmelma@33175
   747
huffman@34099
   748
lemma interior_closure: "interior S = - (closure (- S))"
himmelma@33175
   749
proof-
himmelma@33175
   750
  { fix x
huffman@34099
   751
    have "x \<in> interior S \<longleftrightarrow> x \<in> - (closure (- S))"
himmelma@33175
   752
      unfolding interior_def closure_def islimpt_def
paulson@33307
   753
      by auto
himmelma@33175
   754
  }
himmelma@33175
   755
  thus ?thesis
himmelma@33175
   756
    by blast
himmelma@33175
   757
qed
himmelma@33175
   758
himmelma@33175
   759
lemma closed_closure[simp, intro]: "closed (closure S)"
himmelma@33175
   760
proof-
huffman@34099
   761
  have "closed (- interior (-S))" by blast
himmelma@33175
   762
  thus ?thesis using closure_interior[of S] by simp
himmelma@33175
   763
qed
himmelma@33175
   764
himmelma@33175
   765
lemma closure_hull: "closure S = closed hull S"
himmelma@33175
   766
proof-
himmelma@33175
   767
  have "S \<subseteq> closure S"
himmelma@33175
   768
    unfolding closure_def
himmelma@33175
   769
    by blast
himmelma@33175
   770
  moreover
himmelma@33175
   771
  have "closed (closure S)"
himmelma@33175
   772
    using closed_closure[of S]
himmelma@33175
   773
    by assumption
himmelma@33175
   774
  moreover
himmelma@33175
   775
  { fix t
himmelma@33175
   776
    assume *:"S \<subseteq> t" "closed t"
himmelma@33175
   777
    { fix x
himmelma@33175
   778
      assume "x islimpt S"
himmelma@33175
   779
      hence "x islimpt t" using *(1)
himmelma@33175
   780
        using islimpt_subset[of x, of S, of t]
himmelma@33175
   781
        by blast
himmelma@33175
   782
    }
himmelma@33175
   783
    with * have "closure S \<subseteq> t"
himmelma@33175
   784
      unfolding closure_def
himmelma@33175
   785
      using closed_limpt[of t]
himmelma@33175
   786
      by auto
himmelma@33175
   787
  }
himmelma@33175
   788
  ultimately show ?thesis
himmelma@33175
   789
    using hull_unique[of S, of "closure S", of closed]
himmelma@33175
   790
    unfolding mem_def
himmelma@33175
   791
    by simp
himmelma@33175
   792
qed
himmelma@33175
   793
himmelma@33175
   794
lemma closure_eq: "closure S = S \<longleftrightarrow> closed S"
himmelma@33175
   795
  unfolding closure_hull
himmelma@33175
   796
  using hull_eq[of closed, unfolded mem_def, OF  closed_Inter, of S]
himmelma@33175
   797
  by (metis mem_def subset_eq)
himmelma@33175
   798
himmelma@33175
   799
lemma closure_closed[simp]: "closed S \<Longrightarrow> closure S = S"
himmelma@33175
   800
  using closure_eq[of S]
himmelma@33175
   801
  by simp
himmelma@33175
   802
himmelma@33175
   803
lemma closure_closure[simp]: "closure (closure S) = closure S"
himmelma@33175
   804
  unfolding closure_hull
himmelma@33175
   805
  using hull_hull[of closed S]
himmelma@33175
   806
  by assumption
himmelma@33175
   807
himmelma@33175
   808
lemma closure_subset: "S \<subseteq> closure S"
himmelma@33175
   809
  unfolding closure_hull
himmelma@33175
   810
  using hull_subset[of S closed]
himmelma@33175
   811
  by assumption
himmelma@33175
   812
himmelma@33175
   813
lemma subset_closure: "S \<subseteq> T \<Longrightarrow> closure S \<subseteq> closure T"
himmelma@33175
   814
  unfolding closure_hull
himmelma@33175
   815
  using hull_mono[of S T closed]
himmelma@33175
   816
  by assumption
himmelma@33175
   817
himmelma@33175
   818
lemma closure_minimal: "S \<subseteq> T \<Longrightarrow>  closed T \<Longrightarrow> closure S \<subseteq> T"
himmelma@33175
   819
  using hull_minimal[of S T closed]
himmelma@33175
   820
  unfolding closure_hull mem_def
himmelma@33175
   821
  by simp
himmelma@33175
   822
himmelma@33175
   823
lemma closure_unique: "S \<subseteq> T \<and> closed T \<and> (\<forall> T'. S \<subseteq> T' \<and> closed T' \<longrightarrow> T \<subseteq> T') \<Longrightarrow> closure S = T"
himmelma@33175
   824
  using hull_unique[of S T closed]
himmelma@33175
   825
  unfolding closure_hull mem_def
himmelma@33175
   826
  by simp
himmelma@33175
   827
himmelma@33175
   828
lemma closure_empty[simp]: "closure {} = {}"
himmelma@33175
   829
  using closed_empty closure_closed[of "{}"]
himmelma@33175
   830
  by simp
himmelma@33175
   831
himmelma@33175
   832
lemma closure_univ[simp]: "closure UNIV = UNIV"
himmelma@33175
   833
  using closure_closed[of UNIV]
himmelma@33175
   834
  by simp
himmelma@33175
   835
himmelma@33175
   836
lemma closure_eq_empty: "closure S = {} \<longleftrightarrow> S = {}"
himmelma@33175
   837
  using closure_empty closure_subset[of S]
himmelma@33175
   838
  by blast
himmelma@33175
   839
himmelma@33175
   840
lemma closure_subset_eq: "closure S \<subseteq> S \<longleftrightarrow> closed S"
himmelma@33175
   841
  using closure_eq[of S] closure_subset[of S]
himmelma@33175
   842
  by simp
himmelma@33175
   843
himmelma@33175
   844
lemma open_inter_closure_eq_empty:
himmelma@33175
   845
  "open S \<Longrightarrow> (S \<inter> closure T) = {} \<longleftrightarrow> S \<inter> T = {}"
huffman@34099
   846
  using open_subset_interior[of S "- T"]
huffman@34099
   847
  using interior_subset[of "- T"]
himmelma@33175
   848
  unfolding closure_interior
himmelma@33175
   849
  by auto
himmelma@33175
   850
himmelma@33175
   851
lemma open_inter_closure_subset:
himmelma@33175
   852
  "open S \<Longrightarrow> (S \<inter> (closure T)) \<subseteq> closure(S \<inter> T)"
himmelma@33175
   853
proof
himmelma@33175
   854
  fix x
himmelma@33175
   855
  assume as: "open S" "x \<in> S \<inter> closure T"
himmelma@33175
   856
  { assume *:"x islimpt T"
himmelma@33175
   857
    have "x islimpt (S \<inter> T)"
himmelma@33175
   858
    proof (rule islimptI)
himmelma@33175
   859
      fix A
himmelma@33175
   860
      assume "x \<in> A" "open A"
himmelma@33175
   861
      with as have "x \<in> A \<inter> S" "open (A \<inter> S)"
himmelma@33175
   862
        by (simp_all add: open_Int)
himmelma@33175
   863
      with * obtain y where "y \<in> T" "y \<in> A \<inter> S" "y \<noteq> x"
himmelma@33175
   864
        by (rule islimptE)
himmelma@33175
   865
      hence "y \<in> S \<inter> T" "y \<in> A \<and> y \<noteq> x"
himmelma@33175
   866
        by simp_all
himmelma@33175
   867
      thus "\<exists>y\<in>(S \<inter> T). y \<in> A \<and> y \<noteq> x" ..
himmelma@33175
   868
    qed
himmelma@33175
   869
  }
himmelma@33175
   870
  then show "x \<in> closure (S \<inter> T)" using as
himmelma@33175
   871
    unfolding closure_def
himmelma@33175
   872
    by blast
himmelma@33175
   873
qed
himmelma@33175
   874
huffman@34099
   875
lemma closure_complement: "closure(- S) = - interior(S)"
himmelma@33175
   876
proof-
huffman@34099
   877
  have "S = - (- S)"
himmelma@33175
   878
    by auto
himmelma@33175
   879
  thus ?thesis
himmelma@33175
   880
    unfolding closure_interior
himmelma@33175
   881
    by auto
himmelma@33175
   882
qed
himmelma@33175
   883
huffman@34099
   884
lemma interior_complement: "interior(- S) = - closure(S)"
himmelma@33175
   885
  unfolding closure_interior
himmelma@33175
   886
  by blast
himmelma@33175
   887
himmelma@33175
   888
subsection{* Frontier (aka boundary) *}
himmelma@33175
   889
himmelma@33175
   890
definition "frontier S = closure S - interior S"
himmelma@33175
   891
himmelma@33175
   892
lemma frontier_closed: "closed(frontier S)"
himmelma@33175
   893
  by (simp add: frontier_def closed_Diff)
himmelma@33175
   894
huffman@34099
   895
lemma frontier_closures: "frontier S = (closure S) \<inter> (closure(- S))"
himmelma@33175
   896
  by (auto simp add: frontier_def interior_closure)
himmelma@33175
   897
himmelma@33175
   898
lemma frontier_straddle:
himmelma@33175
   899
  fixes a :: "'a::metric_space"
himmelma@33175
   900
  shows "a \<in> frontier S \<longleftrightarrow> (\<forall>e>0. (\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e))" (is "?lhs \<longleftrightarrow> ?rhs")
himmelma@33175
   901
proof
himmelma@33175
   902
  assume "?lhs"
himmelma@33175
   903
  { fix e::real
himmelma@33175
   904
    assume "e > 0"
himmelma@33175
   905
    let ?rhse = "(\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e)"
himmelma@33175
   906
    { assume "a\<in>S"
himmelma@33175
   907
      have "\<exists>x\<in>S. dist a x < e" using `e>0` `a\<in>S` by(rule_tac x=a in bexI) auto
himmelma@33175
   908
      moreover have "\<exists>x. x \<notin> S \<and> dist a x < e" using `?lhs` `a\<in>S`
himmelma@33175
   909
        unfolding frontier_closures closure_def islimpt_def using `e>0`
himmelma@33175
   910
        by (auto, erule_tac x="ball a e" in allE, auto)
himmelma@33175
   911
      ultimately have ?rhse by auto
himmelma@33175
   912
    }
himmelma@33175
   913
    moreover
himmelma@33175
   914
    { assume "a\<notin>S"
himmelma@33175
   915
      hence ?rhse using `?lhs`
himmelma@33175
   916
        unfolding frontier_closures closure_def islimpt_def
himmelma@33175
   917
        using open_ball[of a e] `e > 0`
paulson@33307
   918
          by simp (metis centre_in_ball mem_ball open_ball) 
himmelma@33175
   919
    }
himmelma@33175
   920
    ultimately have ?rhse by auto
himmelma@33175
   921
  }
himmelma@33175
   922
  thus ?rhs by auto
himmelma@33175
   923
next
himmelma@33175
   924
  assume ?rhs
himmelma@33175
   925
  moreover
himmelma@33175
   926
  { fix T assume "a\<notin>S" and
himmelma@33175
   927
    as:"\<forall>e>0. (\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e)" "a \<notin> S" "a \<in> T" "open T"
himmelma@33175
   928
    from `open T` `a \<in> T` have "\<exists>e>0. ball a e \<subseteq> T" unfolding open_contains_ball[of T] by auto
himmelma@33175
   929
    then obtain e where "e>0" "ball a e \<subseteq> T" by auto
himmelma@33175
   930
    then obtain y where y:"y\<in>S" "dist a y < e"  using as(1) by auto
himmelma@33175
   931
    have "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> a"
himmelma@33175
   932
      using `dist a y < e` `ball a e \<subseteq> T` unfolding ball_def using `y\<in>S` `a\<notin>S` by auto
himmelma@33175
   933
  }
himmelma@33175
   934
  hence "a \<in> closure S" unfolding closure_def islimpt_def using `?rhs` by auto
himmelma@33175
   935
  moreover
himmelma@33175
   936
  { fix T assume "a \<in> T"  "open T" "a\<in>S"
himmelma@33175
   937
    then obtain e where "e>0" and balle: "ball a e \<subseteq> T" unfolding open_contains_ball using `?rhs` by auto
himmelma@33175
   938
    obtain x where "x \<notin> S" "dist a x < e" using `?rhs` using `e>0` by auto
huffman@34099
   939
    hence "\<exists>y\<in>- S. y \<in> T \<and> y \<noteq> a" using balle `a\<in>S` unfolding ball_def by (rule_tac x=x in bexI)auto
himmelma@33175
   940
  }
huffman@34099
   941
  hence "a islimpt (- S) \<or> a\<notin>S" unfolding islimpt_def by auto
huffman@34099
   942
  ultimately show ?lhs unfolding frontier_closures using closure_def[of "- S"] by auto
himmelma@33175
   943
qed
himmelma@33175
   944
himmelma@33175
   945
lemma frontier_subset_closed: "closed S \<Longrightarrow> frontier S \<subseteq> S"
himmelma@33175
   946
  by (metis frontier_def closure_closed Diff_subset)
himmelma@33175
   947
hoelzl@34951
   948
lemma frontier_empty[simp]: "frontier {} = {}"
himmelma@33175
   949
  by (simp add: frontier_def closure_empty)
himmelma@33175
   950
himmelma@33175
   951
lemma frontier_subset_eq: "frontier S \<subseteq> S \<longleftrightarrow> closed S"
himmelma@33175
   952
proof-
himmelma@33175
   953
  { assume "frontier S \<subseteq> S"
himmelma@33175
   954
    hence "closure S \<subseteq> S" using interior_subset unfolding frontier_def by auto
himmelma@33175
   955
    hence "closed S" using closure_subset_eq by auto
himmelma@33175
   956
  }
himmelma@33175
   957
  thus ?thesis using frontier_subset_closed[of S] by auto
himmelma@33175
   958
qed
himmelma@33175
   959
huffman@34099
   960
lemma frontier_complement: "frontier(- S) = frontier S"
himmelma@33175
   961
  by (auto simp add: frontier_def closure_complement interior_complement)
himmelma@33175
   962
himmelma@33175
   963
lemma frontier_disjoint_eq: "frontier S \<inter> S = {} \<longleftrightarrow> open S"
huffman@34099
   964
  using frontier_complement frontier_subset_eq[of "- S"]
huffman@34099
   965
  unfolding open_closed by auto
himmelma@33175
   966
himmelma@33175
   967
subsection{* Common nets and The "within" modifier for nets. *}
himmelma@33175
   968
himmelma@33175
   969
definition
himmelma@33175
   970
  at_infinity :: "'a::real_normed_vector net" where
himmelma@33175
   971
  "at_infinity = Abs_net (range (\<lambda>r. {x. r \<le> norm x}))"
himmelma@33175
   972
himmelma@33175
   973
definition
himmelma@33175
   974
  indirection :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> 'a net" (infixr "indirection" 70) where
himmelma@33175
   975
  "a indirection v = (at a) within {b. \<exists>c\<ge>0. b - a = scaleR c v}"
himmelma@33175
   976
himmelma@33175
   977
text{* Prove That They are all nets. *}
himmelma@33175
   978
himmelma@33175
   979
lemma Rep_net_at_infinity:
himmelma@33175
   980
  "Rep_net at_infinity = range (\<lambda>r. {x. r \<le> norm x})"
himmelma@33175
   981
unfolding at_infinity_def
himmelma@33175
   982
apply (rule Abs_net_inverse')
himmelma@33175
   983
apply (rule image_nonempty, simp)
himmelma@33175
   984
apply (clarsimp, rename_tac r s)
himmelma@33175
   985
apply (rule_tac x="max r s" in exI, auto)
himmelma@33175
   986
done
himmelma@33175
   987
himmelma@33175
   988
lemma within_UNIV: "net within UNIV = net"
himmelma@33175
   989
  by (simp add: Rep_net_inject [symmetric] Rep_net_within)
himmelma@33175
   990
himmelma@33175
   991
subsection{* Identify Trivial limits, where we can't approach arbitrarily closely. *}
himmelma@33175
   992
himmelma@33175
   993
definition
himmelma@33175
   994
  trivial_limit :: "'a net \<Rightarrow> bool" where
himmelma@33175
   995
  "trivial_limit net \<longleftrightarrow> {} \<in> Rep_net net"
himmelma@33175
   996
himmelma@33175
   997
lemma trivial_limit_within:
himmelma@33175
   998
  shows "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"
himmelma@33175
   999
proof
himmelma@33175
  1000
  assume "trivial_limit (at a within S)"
himmelma@33175
  1001
  thus "\<not> a islimpt S"
himmelma@33175
  1002
    unfolding trivial_limit_def
himmelma@33175
  1003
    unfolding Rep_net_within Rep_net_at
himmelma@33175
  1004
    unfolding islimpt_def
himmelma@33175
  1005
    apply (clarsimp simp add: expand_set_eq)
himmelma@33175
  1006
    apply (rename_tac T, rule_tac x=T in exI)
himmelma@33175
  1007
    apply (clarsimp, drule_tac x=y in spec, simp)
himmelma@33175
  1008
    done
himmelma@33175
  1009
next
himmelma@33175
  1010
  assume "\<not> a islimpt S"
himmelma@33175
  1011
  thus "trivial_limit (at a within S)"
himmelma@33175
  1012
    unfolding trivial_limit_def
himmelma@33175
  1013
    unfolding Rep_net_within Rep_net_at
himmelma@33175
  1014
    unfolding islimpt_def
himmelma@33175
  1015
    apply (clarsimp simp add: image_image)
himmelma@33175
  1016
    apply (rule_tac x=T in image_eqI)
himmelma@33175
  1017
    apply (auto simp add: expand_set_eq)
himmelma@33175
  1018
    done
himmelma@33175
  1019
qed
himmelma@33175
  1020
himmelma@33175
  1021
lemma trivial_limit_at_iff: "trivial_limit (at a) \<longleftrightarrow> \<not> a islimpt UNIV"
himmelma@33175
  1022
  using trivial_limit_within [of a UNIV]
himmelma@33175
  1023
  by (simp add: within_UNIV)
himmelma@33175
  1024
himmelma@33175
  1025
lemma trivial_limit_at:
himmelma@33175
  1026
  fixes a :: "'a::perfect_space"
himmelma@33175
  1027
  shows "\<not> trivial_limit (at a)"
himmelma@33175
  1028
  by (simp add: trivial_limit_at_iff)
himmelma@33175
  1029
himmelma@33175
  1030
lemma trivial_limit_at_infinity:
himmelma@33175
  1031
  "\<not> trivial_limit (at_infinity :: ('a::{real_normed_vector,zero_neq_one}) net)"
himmelma@33175
  1032
  (* FIXME: find a more appropriate type class *)
himmelma@33175
  1033
  unfolding trivial_limit_def Rep_net_at_infinity
himmelma@33175
  1034
  apply (clarsimp simp add: expand_set_eq)
himmelma@33175
  1035
  apply (drule_tac x="scaleR r (sgn 1)" in spec)
himmelma@33175
  1036
  apply (simp add: norm_sgn)
himmelma@33175
  1037
  done
himmelma@33175
  1038
hoelzl@34951
  1039
lemma trivial_limit_sequentially[intro]: "\<not> trivial_limit sequentially"
himmelma@33175
  1040
  by (auto simp add: trivial_limit_def Rep_net_sequentially)
himmelma@33175
  1041
himmelma@33175
  1042
subsection{* Some property holds "sufficiently close" to the limit point. *}
himmelma@33175
  1043
himmelma@33175
  1044
lemma eventually_at: (* FIXME: this replaces Limits.eventually_at *)
himmelma@33175
  1045
  "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"
himmelma@33175
  1046
unfolding eventually_at dist_nz by auto
himmelma@33175
  1047
himmelma@33175
  1048
lemma eventually_at_infinity:
himmelma@33175
  1049
  "eventually P at_infinity \<longleftrightarrow> (\<exists>b. \<forall>x. norm x >= b \<longrightarrow> P x)"
himmelma@33175
  1050
unfolding eventually_def Rep_net_at_infinity by auto
himmelma@33175
  1051
himmelma@33175
  1052
lemma eventually_within: "eventually P (at a within S) \<longleftrightarrow>
himmelma@33175
  1053
        (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"
himmelma@33175
  1054
unfolding eventually_within eventually_at dist_nz by auto
himmelma@33175
  1055
himmelma@33175
  1056
lemma eventually_within_le: "eventually P (at a within S) \<longleftrightarrow>
himmelma@33175
  1057
        (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a <= d \<longrightarrow> P x)" (is "?lhs = ?rhs")
himmelma@33175
  1058
unfolding eventually_within
paulson@33307
  1059
by auto (metis Rats_dense_in_nn_real order_le_less_trans order_refl) 
himmelma@33175
  1060
himmelma@33175
  1061
lemma eventually_happens: "eventually P net ==> trivial_limit net \<or> (\<exists>x. P x)"
himmelma@33175
  1062
  unfolding eventually_def trivial_limit_def
himmelma@33175
  1063
  using Rep_net_nonempty [of net] by auto
himmelma@33175
  1064
himmelma@33175
  1065
lemma always_eventually: "(\<forall>x. P x) ==> eventually P net"
himmelma@33175
  1066
  unfolding eventually_def trivial_limit_def
himmelma@33175
  1067
  using Rep_net_nonempty [of net] by auto
himmelma@33175
  1068
himmelma@33175
  1069
lemma trivial_limit_eventually: "trivial_limit net \<Longrightarrow> eventually P net"
himmelma@33175
  1070
  unfolding trivial_limit_def eventually_def by auto
himmelma@33175
  1071
himmelma@33175
  1072
lemma eventually_False: "eventually (\<lambda>x. False) net \<longleftrightarrow> trivial_limit net"
himmelma@33175
  1073
  unfolding trivial_limit_def eventually_def by auto
himmelma@33175
  1074
himmelma@33175
  1075
lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"
himmelma@33175
  1076
  apply (safe elim!: trivial_limit_eventually)
himmelma@33175
  1077
  apply (simp add: eventually_False [symmetric])
himmelma@33175
  1078
  done
himmelma@33175
  1079
himmelma@33175
  1080
text{* Combining theorems for "eventually" *}
himmelma@33175
  1081
himmelma@33175
  1082
lemma eventually_conjI:
himmelma@33175
  1083
  "\<lbrakk>eventually (\<lambda>x. P x) net; eventually (\<lambda>x. Q x) net\<rbrakk>
himmelma@33175
  1084
    \<Longrightarrow> eventually (\<lambda>x. P x \<and> Q x) net"
himmelma@33175
  1085
by (rule eventually_conj)
himmelma@33175
  1086
himmelma@33175
  1087
lemma eventually_rev_mono:
himmelma@33175
  1088
  "eventually P net \<Longrightarrow> (\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually Q net"
himmelma@33175
  1089
using eventually_mono [of P Q] by fast
himmelma@33175
  1090
himmelma@33175
  1091
lemma eventually_and: " eventually (\<lambda>x. P x \<and> Q x) net \<longleftrightarrow> eventually P net \<and> eventually Q net"
himmelma@33175
  1092
  by (auto intro!: eventually_conjI elim: eventually_rev_mono)
himmelma@33175
  1093
himmelma@33175
  1094
lemma eventually_false: "eventually (\<lambda>x. False) net \<longleftrightarrow> trivial_limit net"
himmelma@33175
  1095
  by (auto simp add: eventually_False)
himmelma@33175
  1096
himmelma@33175
  1097
lemma not_eventually: "(\<forall>x. \<not> P x ) \<Longrightarrow> ~(trivial_limit net) ==> ~(eventually (\<lambda>x. P x) net)"
himmelma@33175
  1098
  by (simp add: eventually_False)
himmelma@33175
  1099
himmelma@33175
  1100
subsection{* Limits, defined as vacuously true when the limit is trivial. *}
himmelma@33175
  1101
himmelma@33175
  1102
  text{* Notation Lim to avoid collition with lim defined in analysis *}
himmelma@33175
  1103
definition
himmelma@33175
  1104
  Lim :: "'a net \<Rightarrow> ('a \<Rightarrow> 'b::t2_space) \<Rightarrow> 'b" where
himmelma@33175
  1105
  "Lim net f = (THE l. (f ---> l) net)"
himmelma@33175
  1106
himmelma@33175
  1107
lemma Lim:
himmelma@33175
  1108
 "(f ---> l) net \<longleftrightarrow>
himmelma@33175
  1109
        trivial_limit net \<or>
himmelma@33175
  1110
        (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"
himmelma@33175
  1111
  unfolding tendsto_iff trivial_limit_eq by auto
himmelma@33175
  1112
himmelma@33175
  1113
himmelma@33175
  1114
text{* Show that they yield usual definitions in the various cases. *}
himmelma@33175
  1115
himmelma@33175
  1116
lemma Lim_within_le: "(f ---> l)(at a within S) \<longleftrightarrow>
himmelma@33175
  1117
           (\<forall>e>0. \<exists>d>0. \<forall>x\<in>S. 0 < dist x a  \<and> dist x a  <= d \<longrightarrow> dist (f x) l < e)"
himmelma@33175
  1118
  by (auto simp add: tendsto_iff eventually_within_le)
himmelma@33175
  1119
himmelma@33175
  1120
lemma Lim_within: "(f ---> l) (at a within S) \<longleftrightarrow>
himmelma@33175
  1121
        (\<forall>e >0. \<exists>d>0. \<forall>x \<in> S. 0 < dist x a  \<and> dist x a  < d  \<longrightarrow> dist (f x) l < e)"
himmelma@33175
  1122
  by (auto simp add: tendsto_iff eventually_within)
himmelma@33175
  1123
himmelma@33175
  1124
lemma Lim_at: "(f ---> l) (at a) \<longleftrightarrow>
himmelma@33175
  1125
        (\<forall>e >0. \<exists>d>0. \<forall>x. 0 < dist x a  \<and> dist x a  < d  \<longrightarrow> dist (f x) l < e)"
himmelma@33175
  1126
  by (auto simp add: tendsto_iff eventually_at)
himmelma@33175
  1127
himmelma@33175
  1128
lemma Lim_at_iff_LIM: "(f ---> l) (at a) \<longleftrightarrow> f -- a --> l"
himmelma@33175
  1129
  unfolding Lim_at LIM_def by (simp only: zero_less_dist_iff)
himmelma@33175
  1130
himmelma@33175
  1131
lemma Lim_at_infinity:
himmelma@33175
  1132
  "(f ---> l) at_infinity \<longleftrightarrow> (\<forall>e>0. \<exists>b. \<forall>x. norm x >= b \<longrightarrow> dist (f x) l < e)"
himmelma@33175
  1133
  by (auto simp add: tendsto_iff eventually_at_infinity)
himmelma@33175
  1134
himmelma@33175
  1135
lemma Lim_sequentially:
himmelma@33175
  1136
 "(S ---> l) sequentially \<longleftrightarrow>
himmelma@33175
  1137
          (\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (S n) l < e)"
himmelma@33175
  1138
  by (auto simp add: tendsto_iff eventually_sequentially)
himmelma@33175
  1139
himmelma@33175
  1140
lemma Lim_sequentially_iff_LIMSEQ: "(S ---> l) sequentially \<longleftrightarrow> S ----> l"
himmelma@33175
  1141
  unfolding Lim_sequentially LIMSEQ_def ..
himmelma@33175
  1142
himmelma@33175
  1143
lemma Lim_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> (f ---> l) net"
himmelma@33175
  1144
  by (rule topological_tendstoI, auto elim: eventually_rev_mono)
himmelma@33175
  1145
himmelma@33175
  1146
text{* The expected monotonicity property. *}
himmelma@33175
  1147
himmelma@33175
  1148
lemma Lim_within_empty: "(f ---> l) (net within {})"
himmelma@33175
  1149
  unfolding tendsto_def Limits.eventually_within by simp
himmelma@33175
  1150
himmelma@33175
  1151
lemma Lim_within_subset: "(f ---> l) (net within S) \<Longrightarrow> T \<subseteq> S \<Longrightarrow> (f ---> l) (net within T)"
himmelma@33175
  1152
  unfolding tendsto_def Limits.eventually_within
himmelma@33175
  1153
  by (auto elim!: eventually_elim1)
himmelma@33175
  1154
himmelma@33175
  1155
lemma Lim_Un: assumes "(f ---> l) (net within S)" "(f ---> l) (net within T)"
himmelma@33175
  1156
  shows "(f ---> l) (net within (S \<union> T))"
himmelma@33175
  1157
  using assms unfolding tendsto_def Limits.eventually_within
himmelma@33175
  1158
  apply clarify
himmelma@33175
  1159
  apply (drule spec, drule (1) mp, drule (1) mp)
himmelma@33175
  1160
  apply (drule spec, drule (1) mp, drule (1) mp)
himmelma@33175
  1161
  apply (auto elim: eventually_elim2)
himmelma@33175
  1162
  done
himmelma@33175
  1163
himmelma@33175
  1164
lemma Lim_Un_univ:
himmelma@33175
  1165
 "(f ---> l) (net within S) \<Longrightarrow> (f ---> l) (net within T) \<Longrightarrow>  S \<union> T = UNIV
himmelma@33175
  1166
        ==> (f ---> l) net"
himmelma@33175
  1167
  by (metis Lim_Un within_UNIV)
himmelma@33175
  1168
himmelma@33175
  1169
text{* Interrelations between restricted and unrestricted limits. *}
himmelma@33175
  1170
himmelma@33175
  1171
lemma Lim_at_within: "(f ---> l) net ==> (f ---> l)(net within S)"
himmelma@33175
  1172
  (* FIXME: rename *)
himmelma@33175
  1173
  unfolding tendsto_def Limits.eventually_within
himmelma@33175
  1174
  apply (clarify, drule spec, drule (1) mp, drule (1) mp)
himmelma@33175
  1175
  by (auto elim!: eventually_elim1)
himmelma@33175
  1176
himmelma@33175
  1177
lemma Lim_within_open:
himmelma@33175
  1178
  fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
himmelma@33175
  1179
  assumes"a \<in> S" "open S"
himmelma@33175
  1180
  shows "(f ---> l)(at a within S) \<longleftrightarrow> (f ---> l)(at a)" (is "?lhs \<longleftrightarrow> ?rhs")
himmelma@33175
  1181
proof
himmelma@33175
  1182
  assume ?lhs
himmelma@33175
  1183
  { fix A assume "open A" "l \<in> A"
himmelma@33175
  1184
    with `?lhs` have "eventually (\<lambda>x. f x \<in> A) (at a within S)"
himmelma@33175
  1185
      by (rule topological_tendstoD)
himmelma@33175
  1186
    hence "eventually (\<lambda>x. x \<in> S \<longrightarrow> f x \<in> A) (at a)"
himmelma@33175
  1187
      unfolding Limits.eventually_within .
himmelma@33175
  1188
    then obtain T where "open T" "a \<in> T" "\<forall>x\<in>T. x \<noteq> a \<longrightarrow> x \<in> S \<longrightarrow> f x \<in> A"
himmelma@33175
  1189
      unfolding eventually_at_topological by fast
himmelma@33175
  1190
    hence "open (T \<inter> S)" "a \<in> T \<inter> S" "\<forall>x\<in>(T \<inter> S). x \<noteq> a \<longrightarrow> f x \<in> A"
himmelma@33175
  1191
      using assms by auto
himmelma@33175
  1192
    hence "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. x \<noteq> a \<longrightarrow> f x \<in> A)"
himmelma@33175
  1193
      by fast
himmelma@33175
  1194
    hence "eventually (\<lambda>x. f x \<in> A) (at a)"
himmelma@33175
  1195
      unfolding eventually_at_topological .
himmelma@33175
  1196
  }
himmelma@33175
  1197
  thus ?rhs by (rule topological_tendstoI)
himmelma@33175
  1198
next
himmelma@33175
  1199
  assume ?rhs
himmelma@33175
  1200
  thus ?lhs by (rule Lim_at_within)
himmelma@33175
  1201
qed
himmelma@33175
  1202
himmelma@33175
  1203
text{* Another limit point characterization. *}
himmelma@33175
  1204
himmelma@33175
  1205
lemma islimpt_sequential:
himmelma@33175
  1206
  fixes x :: "'a::metric_space" (* FIXME: generalize to topological_space *)
himmelma@33175
  1207
  shows "x islimpt S \<longleftrightarrow> (\<exists>f. (\<forall>n::nat. f n \<in> S -{x}) \<and> (f ---> x) sequentially)"
himmelma@33175
  1208
    (is "?lhs = ?rhs")
himmelma@33175
  1209
proof
himmelma@33175
  1210
  assume ?lhs
himmelma@33175
  1211
  then obtain f where f:"\<forall>y. y>0 \<longrightarrow> f y \<in> S \<and> f y \<noteq> x \<and> dist (f y) x < y"
himmelma@33175
  1212
    unfolding islimpt_approachable using choice[of "\<lambda>e y. e>0 \<longrightarrow> y\<in>S \<and> y\<noteq>x \<and> dist y x < e"] by auto
himmelma@33175
  1213
  { fix n::nat
himmelma@33175
  1214
    have "f (inverse (real n + 1)) \<in> S - {x}" using f by auto
himmelma@33175
  1215
  }
himmelma@33175
  1216
  moreover
himmelma@33175
  1217
  { fix e::real assume "e>0"
himmelma@33175
  1218
    hence "\<exists>N::nat. inverse (real (N + 1)) < e" using real_arch_inv[of e] apply (auto simp add: Suc_pred') apply(rule_tac x="n - 1" in exI) by auto
himmelma@33175
  1219
    then obtain N::nat where "inverse (real (N + 1)) < e" by auto
himmelma@33175
  1220
    hence "\<forall>n\<ge>N. inverse (real n + 1) < e" by (auto, metis Suc_le_mono le_SucE less_imp_inverse_less nat_le_real_less order_less_trans real_of_nat_Suc real_of_nat_Suc_gt_zero)
himmelma@33175
  1221
    moreover have "\<forall>n\<ge>N. dist (f (inverse (real n + 1))) x < (inverse (real n + 1))" using f `e>0` by auto
himmelma@33175
  1222
    ultimately have "\<exists>N::nat. \<forall>n\<ge>N. dist (f (inverse (real n + 1))) x < e" apply(rule_tac x=N in exI) apply auto apply(erule_tac x=n in allE)+ by auto
himmelma@33175
  1223
  }
himmelma@33175
  1224
  hence " ((\<lambda>n. f (inverse (real n + 1))) ---> x) sequentially"
himmelma@33175
  1225
    unfolding Lim_sequentially using f by auto
himmelma@33175
  1226
  ultimately show ?rhs apply (rule_tac x="(\<lambda>n::nat. f (inverse (real n + 1)))" in exI) by auto
himmelma@33175
  1227
next
himmelma@33175
  1228
  assume ?rhs
himmelma@33175
  1229
  then obtain f::"nat\<Rightarrow>'a"  where f:"(\<forall>n. f n \<in> S - {x})" "(\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (f n) x < e)" unfolding Lim_sequentially by auto
himmelma@33175
  1230
  { fix e::real assume "e>0"
himmelma@33175
  1231
    then obtain N where "dist (f N) x < e" using f(2) by auto
himmelma@33175
  1232
    moreover have "f N\<in>S" "f N \<noteq> x" using f(1) by auto
himmelma@33175
  1233
    ultimately have "\<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e" by auto
himmelma@33175
  1234
  }
himmelma@33175
  1235
  thus ?lhs unfolding islimpt_approachable by auto
himmelma@33175
  1236
qed
himmelma@33175
  1237
himmelma@33175
  1238
text{* Basic arithmetical combining theorems for limits. *}
himmelma@33175
  1239
himmelma@33175
  1240
lemma Lim_linear:
himmelma@33175
  1241
  assumes "(f ---> l) net" "bounded_linear h"
himmelma@33175
  1242
  shows "((\<lambda>x. h (f x)) ---> h l) net"
himmelma@33175
  1243
using `bounded_linear h` `(f ---> l) net`
himmelma@33175
  1244
by (rule bounded_linear.tendsto)
himmelma@33175
  1245
himmelma@33175
  1246
lemma Lim_ident_at: "((\<lambda>x. x) ---> a) (at a)"
himmelma@33175
  1247
  unfolding tendsto_def Limits.eventually_at_topological by fast
himmelma@33175
  1248
hoelzl@34951
  1249
lemma Lim_const[intro]: "((\<lambda>x. a) ---> a) net" by (rule tendsto_const)
hoelzl@34951
  1250
hoelzl@34951
  1251
lemma Lim_cmul[intro]:
himmelma@33175
  1252
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1253
  shows "(f ---> l) net ==> ((\<lambda>x. c *\<^sub>R f x) ---> c *\<^sub>R l) net"
himmelma@33175
  1254
  by (intro tendsto_intros)
himmelma@33175
  1255
himmelma@33175
  1256
lemma Lim_neg:
himmelma@33175
  1257
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1258
  shows "(f ---> l) net ==> ((\<lambda>x. -(f x)) ---> -l) net"
himmelma@33175
  1259
  by (rule tendsto_minus)
himmelma@33175
  1260
himmelma@33175
  1261
lemma Lim_add: fixes f :: "'a \<Rightarrow> 'b::real_normed_vector" shows
himmelma@33175
  1262
 "(f ---> l) net \<Longrightarrow> (g ---> m) net \<Longrightarrow> ((\<lambda>x. f(x) + g(x)) ---> l + m) net"
himmelma@33175
  1263
  by (rule tendsto_add)
himmelma@33175
  1264
himmelma@33175
  1265
lemma Lim_sub:
himmelma@33175
  1266
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1267
  shows "(f ---> l) net \<Longrightarrow> (g ---> m) net \<Longrightarrow> ((\<lambda>x. f(x) - g(x)) ---> l - m) net"
himmelma@33175
  1268
  by (rule tendsto_diff)
himmelma@33175
  1269
himmelma@33175
  1270
lemma Lim_null:
himmelma@33175
  1271
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1272
  shows "(f ---> l) net \<longleftrightarrow> ((\<lambda>x. f(x) - l) ---> 0) net" by (simp add: Lim dist_norm)
himmelma@33175
  1273
himmelma@33175
  1274
lemma Lim_null_norm:
himmelma@33175
  1275
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1276
  shows "(f ---> 0) net \<longleftrightarrow> ((\<lambda>x. norm(f x)) ---> 0) net"
himmelma@33175
  1277
  by (simp add: Lim dist_norm)
himmelma@33175
  1278
himmelma@33175
  1279
lemma Lim_null_comparison:
himmelma@33175
  1280
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1281
  assumes "eventually (\<lambda>x. norm (f x) \<le> g x) net" "(g ---> 0) net"
himmelma@33175
  1282
  shows "(f ---> 0) net"
himmelma@33175
  1283
proof(simp add: tendsto_iff, rule+)
himmelma@33175
  1284
  fix e::real assume "0<e"
himmelma@33175
  1285
  { fix x
himmelma@33175
  1286
    assume "norm (f x) \<le> g x" "dist (g x) 0 < e"
himmelma@33175
  1287
    hence "dist (f x) 0 < e" by (simp add: dist_norm)
himmelma@33175
  1288
  }
himmelma@33175
  1289
  thus "eventually (\<lambda>x. dist (f x) 0 < e) net"
himmelma@33175
  1290
    using eventually_and[of "\<lambda>x. norm(f x) <= g x" "\<lambda>x. dist (g x) 0 < e" net]
himmelma@33175
  1291
    using eventually_mono[of "(\<lambda>x. norm (f x) \<le> g x \<and> dist (g x) 0 < e)" "(\<lambda>x. dist (f x) 0 < e)" net]
himmelma@33175
  1292
    using assms `e>0` unfolding tendsto_iff by auto
himmelma@33175
  1293
qed
himmelma@33175
  1294
himmelma@33175
  1295
lemma Lim_component:
hoelzl@34291
  1296
  fixes f :: "'a \<Rightarrow> 'b::metric_space ^ 'n"
himmelma@33175
  1297
  shows "(f ---> l) net \<Longrightarrow> ((\<lambda>a. f a $i) ---> l$i) net"
himmelma@33175
  1298
  unfolding tendsto_iff
himmelma@33175
  1299
  apply (clarify)
himmelma@33175
  1300
  apply (drule spec, drule (1) mp)
himmelma@33175
  1301
  apply (erule eventually_elim1)
himmelma@33175
  1302
  apply (erule le_less_trans [OF dist_nth_le])
himmelma@33175
  1303
  done
himmelma@33175
  1304
himmelma@33175
  1305
lemma Lim_transform_bound:
himmelma@33175
  1306
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1307
  fixes g :: "'a \<Rightarrow> 'c::real_normed_vector"
himmelma@33175
  1308
  assumes "eventually (\<lambda>n. norm(f n) <= norm(g n)) net"  "(g ---> 0) net"
himmelma@33175
  1309
  shows "(f ---> 0) net"
himmelma@33175
  1310
proof (rule tendstoI)
himmelma@33175
  1311
  fix e::real assume "e>0"
himmelma@33175
  1312
  { fix x
himmelma@33175
  1313
    assume "norm (f x) \<le> norm (g x)" "dist (g x) 0 < e"
himmelma@33175
  1314
    hence "dist (f x) 0 < e" by (simp add: dist_norm)}
himmelma@33175
  1315
  thus "eventually (\<lambda>x. dist (f x) 0 < e) net"
himmelma@33175
  1316
    using eventually_and[of "\<lambda>x. norm (f x) \<le> norm (g x)" "\<lambda>x. dist (g x) 0 < e" net]
himmelma@33175
  1317
    using eventually_mono[of "\<lambda>x. norm (f x) \<le> norm (g x) \<and> dist (g x) 0 < e" "\<lambda>x. dist (f x) 0 < e" net]
himmelma@33175
  1318
    using assms `e>0` unfolding tendsto_iff by blast
himmelma@33175
  1319
qed
himmelma@33175
  1320
himmelma@33175
  1321
text{* Deducing things about the limit from the elements. *}
himmelma@33175
  1322
himmelma@33175
  1323
lemma Lim_in_closed_set:
himmelma@33175
  1324
  assumes "closed S" "eventually (\<lambda>x. f(x) \<in> S) net" "\<not>(trivial_limit net)" "(f ---> l) net"
himmelma@33175
  1325
  shows "l \<in> S"
himmelma@33175
  1326
proof (rule ccontr)
himmelma@33175
  1327
  assume "l \<notin> S"
himmelma@33175
  1328
  with `closed S` have "open (- S)" "l \<in> - S"
himmelma@33175
  1329
    by (simp_all add: open_Compl)
himmelma@33175
  1330
  with assms(4) have "eventually (\<lambda>x. f x \<in> - S) net"
himmelma@33175
  1331
    by (rule topological_tendstoD)
himmelma@33175
  1332
  with assms(2) have "eventually (\<lambda>x. False) net"
himmelma@33175
  1333
    by (rule eventually_elim2) simp
himmelma@33175
  1334
  with assms(3) show "False"
himmelma@33175
  1335
    by (simp add: eventually_False)
himmelma@33175
  1336
qed
himmelma@33175
  1337
himmelma@33175
  1338
text{* Need to prove closed(cball(x,e)) before deducing this as a corollary. *}
himmelma@33175
  1339
himmelma@33175
  1340
lemma Lim_dist_ubound:
himmelma@33175
  1341
  assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. dist a (f x) <= e) net"
himmelma@33175
  1342
  shows "dist a l <= e"
himmelma@33175
  1343
proof (rule ccontr)
himmelma@33175
  1344
  assume "\<not> dist a l \<le> e"
himmelma@33175
  1345
  then have "0 < dist a l - e" by simp
himmelma@33175
  1346
  with assms(2) have "eventually (\<lambda>x. dist (f x) l < dist a l - e) net"
himmelma@33175
  1347
    by (rule tendstoD)
himmelma@33175
  1348
  with assms(3) have "eventually (\<lambda>x. dist a (f x) \<le> e \<and> dist (f x) l < dist a l - e) net"
himmelma@33175
  1349
    by (rule eventually_conjI)
himmelma@33175
  1350
  then obtain w where "dist a (f w) \<le> e" "dist (f w) l < dist a l - e"
himmelma@33175
  1351
    using assms(1) eventually_happens by auto
himmelma@33175
  1352
  hence "dist a (f w) + dist (f w) l < e + (dist a l - e)"
himmelma@33175
  1353
    by (rule add_le_less_mono)
himmelma@33175
  1354
  hence "dist a (f w) + dist (f w) l < dist a l"
himmelma@33175
  1355
    by simp
himmelma@33175
  1356
  also have "\<dots> \<le> dist a (f w) + dist (f w) l"
himmelma@33175
  1357
    by (rule dist_triangle)
himmelma@33175
  1358
  finally show False by simp
himmelma@33175
  1359
qed
himmelma@33175
  1360
himmelma@33175
  1361
lemma Lim_norm_ubound:
himmelma@33175
  1362
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1363
  assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. norm(f x) <= e) net"
himmelma@33175
  1364
  shows "norm(l) <= e"
himmelma@33175
  1365
proof (rule ccontr)
himmelma@33175
  1366
  assume "\<not> norm l \<le> e"
himmelma@33175
  1367
  then have "0 < norm l - e" by simp
himmelma@33175
  1368
  with assms(2) have "eventually (\<lambda>x. dist (f x) l < norm l - e) net"
himmelma@33175
  1369
    by (rule tendstoD)
himmelma@33175
  1370
  with assms(3) have "eventually (\<lambda>x. norm (f x) \<le> e \<and> dist (f x) l < norm l - e) net"
himmelma@33175
  1371
    by (rule eventually_conjI)
himmelma@33175
  1372
  then obtain w where "norm (f w) \<le> e" "dist (f w) l < norm l - e"
himmelma@33175
  1373
    using assms(1) eventually_happens by auto
himmelma@33175
  1374
  hence "norm (f w - l) < norm l - e" "norm (f w) \<le> e" by (simp_all add: dist_norm)
himmelma@33175
  1375
  hence "norm (f w - l) + norm (f w) < norm l" by simp
himmelma@33175
  1376
  hence "norm (f w - l - f w) < norm l" by (rule le_less_trans [OF norm_triangle_ineq4])
himmelma@33175
  1377
  thus False using `\<not> norm l \<le> e` by simp
himmelma@33175
  1378
qed
himmelma@33175
  1379
himmelma@33175
  1380
lemma Lim_norm_lbound:
himmelma@33175
  1381
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1382
  assumes "\<not> (trivial_limit net)"  "(f ---> l) net"  "eventually (\<lambda>x. e <= norm(f x)) net"
himmelma@33175
  1383
  shows "e \<le> norm l"
himmelma@33175
  1384
proof (rule ccontr)
himmelma@33175
  1385
  assume "\<not> e \<le> norm l"
himmelma@33175
  1386
  then have "0 < e - norm l" by simp
himmelma@33175
  1387
  with assms(2) have "eventually (\<lambda>x. dist (f x) l < e - norm l) net"
himmelma@33175
  1388
    by (rule tendstoD)
himmelma@33175
  1389
  with assms(3) have "eventually (\<lambda>x. e \<le> norm (f x) \<and> dist (f x) l < e - norm l) net"
himmelma@33175
  1390
    by (rule eventually_conjI)
himmelma@33175
  1391
  then obtain w where "e \<le> norm (f w)" "dist (f w) l < e - norm l"
himmelma@33175
  1392
    using assms(1) eventually_happens by auto
himmelma@33175
  1393
  hence "norm (f w - l) + norm l < e" "e \<le> norm (f w)" by (simp_all add: dist_norm)
himmelma@33175
  1394
  hence "norm (f w - l) + norm l < norm (f w)" by (rule less_le_trans)
himmelma@33175
  1395
  hence "norm (f w - l + l) < norm (f w)" by (rule le_less_trans [OF norm_triangle_ineq])
himmelma@33175
  1396
  thus False by simp
himmelma@33175
  1397
qed
himmelma@33175
  1398
himmelma@33175
  1399
text{* Uniqueness of the limit, when nontrivial. *}
himmelma@33175
  1400
himmelma@33175
  1401
lemma Lim_unique:
himmelma@33175
  1402
  fixes f :: "'a \<Rightarrow> 'b::t2_space"
himmelma@33175
  1403
  assumes "\<not> trivial_limit net"  "(f ---> l) net"  "(f ---> l') net"
himmelma@33175
  1404
  shows "l = l'"
himmelma@33175
  1405
proof (rule ccontr)
himmelma@33175
  1406
  assume "l \<noteq> l'"
himmelma@33175
  1407
  obtain U V where "open U" "open V" "l \<in> U" "l' \<in> V" "U \<inter> V = {}"
himmelma@33175
  1408
    using hausdorff [OF `l \<noteq> l'`] by fast
himmelma@33175
  1409
  have "eventually (\<lambda>x. f x \<in> U) net"
himmelma@33175
  1410
    using `(f ---> l) net` `open U` `l \<in> U` by (rule topological_tendstoD)
himmelma@33175
  1411
  moreover
himmelma@33175
  1412
  have "eventually (\<lambda>x. f x \<in> V) net"
himmelma@33175
  1413
    using `(f ---> l') net` `open V` `l' \<in> V` by (rule topological_tendstoD)
himmelma@33175
  1414
  ultimately
himmelma@33175
  1415
  have "eventually (\<lambda>x. False) net"
himmelma@33175
  1416
  proof (rule eventually_elim2)
himmelma@33175
  1417
    fix x
himmelma@33175
  1418
    assume "f x \<in> U" "f x \<in> V"
himmelma@33175
  1419
    hence "f x \<in> U \<inter> V" by simp
himmelma@33175
  1420
    with `U \<inter> V = {}` show "False" by simp
himmelma@33175
  1421
  qed
himmelma@33175
  1422
  with `\<not> trivial_limit net` show "False"
himmelma@33175
  1423
    by (simp add: eventually_False)
himmelma@33175
  1424
qed
himmelma@33175
  1425
himmelma@33175
  1426
lemma tendsto_Lim:
himmelma@33175
  1427
  fixes f :: "'a \<Rightarrow> 'b::t2_space"
himmelma@33175
  1428
  shows "~(trivial_limit net) \<Longrightarrow> (f ---> l) net ==> Lim net f = l"
himmelma@33175
  1429
  unfolding Lim_def using Lim_unique[of net f] by auto
himmelma@33175
  1430
himmelma@33175
  1431
text{* Limit under bilinear function *}
himmelma@33175
  1432
himmelma@33175
  1433
lemma Lim_bilinear:
himmelma@33175
  1434
  assumes "(f ---> l) net" and "(g ---> m) net" and "bounded_bilinear h"
himmelma@33175
  1435
  shows "((\<lambda>x. h (f x) (g x)) ---> (h l m)) net"
himmelma@33175
  1436
using `bounded_bilinear h` `(f ---> l) net` `(g ---> m) net`
himmelma@33175
  1437
by (rule bounded_bilinear.tendsto)
himmelma@33175
  1438
himmelma@33175
  1439
text{* These are special for limits out of the same vector space. *}
himmelma@33175
  1440
himmelma@33175
  1441
lemma Lim_within_id: "(id ---> a) (at a within s)"
himmelma@33175
  1442
  unfolding tendsto_def Limits.eventually_within eventually_at_topological
himmelma@33175
  1443
  by auto
himmelma@33175
  1444
himmelma@33175
  1445
lemma Lim_at_id: "(id ---> a) (at a)"
himmelma@33175
  1446
apply (subst within_UNIV[symmetric]) by (simp add: Lim_within_id)
himmelma@33175
  1447
himmelma@33175
  1448
lemma Lim_at_zero:
himmelma@33175
  1449
  fixes a :: "'a::real_normed_vector"
himmelma@33175
  1450
  fixes l :: "'b::topological_space"
himmelma@33175
  1451
  shows "(f ---> l) (at a) \<longleftrightarrow> ((\<lambda>x. f(a + x)) ---> l) (at 0)" (is "?lhs = ?rhs")
himmelma@33175
  1452
proof
himmelma@33175
  1453
  assume "?lhs"
himmelma@33175
  1454
  { fix S assume "open S" "l \<in> S"
himmelma@33175
  1455
    with `?lhs` have "eventually (\<lambda>x. f x \<in> S) (at a)"
himmelma@33175
  1456
      by (rule topological_tendstoD)
himmelma@33175
  1457
    then obtain d where d: "d>0" "\<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<in> S"
himmelma@33175
  1458
      unfolding Limits.eventually_at by fast
himmelma@33175
  1459
    { fix x::"'a" assume "x \<noteq> 0 \<and> dist x 0 < d"
himmelma@33175
  1460
      hence "f (a + x) \<in> S" using d
himmelma@33175
  1461
      apply(erule_tac x="x+a" in allE)
himmelma@33175
  1462
      by(auto simp add: comm_monoid_add.mult_commute dist_norm dist_commute)
himmelma@33175
  1463
    }
himmelma@33175
  1464
    hence "\<exists>d>0. \<forall>x. x \<noteq> 0 \<and> dist x 0 < d \<longrightarrow> f (a + x) \<in> S"
himmelma@33175
  1465
      using d(1) by auto
himmelma@33175
  1466
    hence "eventually (\<lambda>x. f (a + x) \<in> S) (at 0)"
himmelma@33175
  1467
      unfolding Limits.eventually_at .
himmelma@33175
  1468
  }
himmelma@33175
  1469
  thus "?rhs" by (rule topological_tendstoI)
himmelma@33175
  1470
next
himmelma@33175
  1471
  assume "?rhs"
himmelma@33175
  1472
  { fix S assume "open S" "l \<in> S"
himmelma@33175
  1473
    with `?rhs` have "eventually (\<lambda>x. f (a + x) \<in> S) (at 0)"
himmelma@33175
  1474
      by (rule topological_tendstoD)
himmelma@33175
  1475
    then obtain d where d: "d>0" "\<forall>x. x \<noteq> 0 \<and> dist x 0 < d \<longrightarrow> f (a + x) \<in> S"
himmelma@33175
  1476
      unfolding Limits.eventually_at by fast
himmelma@33175
  1477
    { fix x::"'a" assume "x \<noteq> a \<and> dist x a < d"
himmelma@33175
  1478
      hence "f x \<in> S" using d apply(erule_tac x="x-a" in allE)
himmelma@33175
  1479
        by(auto simp add: comm_monoid_add.mult_commute dist_norm dist_commute)
himmelma@33175
  1480
    }
himmelma@33175
  1481
    hence "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<in> S" using d(1) by auto
himmelma@33175
  1482
    hence "eventually (\<lambda>x. f x \<in> S) (at a)" unfolding Limits.eventually_at .
himmelma@33175
  1483
  }
himmelma@33175
  1484
  thus "?lhs" by (rule topological_tendstoI)
himmelma@33175
  1485
qed
himmelma@33175
  1486
himmelma@33175
  1487
text{* It's also sometimes useful to extract the limit point from the net.  *}
himmelma@33175
  1488
himmelma@33175
  1489
definition
himmelma@33175
  1490
  netlimit :: "'a::t2_space net \<Rightarrow> 'a" where
himmelma@33175
  1491
  "netlimit net = (SOME a. ((\<lambda>x. x) ---> a) net)"
himmelma@33175
  1492
himmelma@33175
  1493
lemma netlimit_within:
himmelma@33175
  1494
  assumes "\<not> trivial_limit (at a within S)"
himmelma@33175
  1495
  shows "netlimit (at a within S) = a"
himmelma@33175
  1496
unfolding netlimit_def
himmelma@33175
  1497
apply (rule some_equality)
himmelma@33175
  1498
apply (rule Lim_at_within)
himmelma@33175
  1499
apply (rule Lim_ident_at)
himmelma@33175
  1500
apply (erule Lim_unique [OF assms])
himmelma@33175
  1501
apply (rule Lim_at_within)
himmelma@33175
  1502
apply (rule Lim_ident_at)
himmelma@33175
  1503
done
himmelma@33175
  1504
himmelma@33175
  1505
lemma netlimit_at:
himmelma@33175
  1506
  fixes a :: "'a::perfect_space"
himmelma@33175
  1507
  shows "netlimit (at a) = a"
himmelma@33175
  1508
  apply (subst within_UNIV[symmetric])
himmelma@33175
  1509
  using netlimit_within[of a UNIV]
himmelma@33175
  1510
  by (simp add: trivial_limit_at within_UNIV)
himmelma@33175
  1511
himmelma@33175
  1512
text{* Transformation of limit. *}
himmelma@33175
  1513
himmelma@33175
  1514
lemma Lim_transform:
himmelma@33175
  1515
  fixes f g :: "'a::type \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1516
  assumes "((\<lambda>x. f x - g x) ---> 0) net" "(f ---> l) net"
himmelma@33175
  1517
  shows "(g ---> l) net"
himmelma@33175
  1518
proof-
himmelma@33175
  1519
  from assms have "((\<lambda>x. f x - g x - f x) ---> 0 - l) net" using Lim_sub[of "\<lambda>x. f x - g x" 0 net f l] by auto
himmelma@33175
  1520
  thus "?thesis" using Lim_neg [of "\<lambda> x. - g x" "-l" net] by auto
himmelma@33175
  1521
qed
himmelma@33175
  1522
himmelma@33175
  1523
lemma Lim_transform_eventually:
himmelma@33175
  1524
  "eventually (\<lambda>x. f x = g x) net \<Longrightarrow> (f ---> l) net ==> (g ---> l) net"
himmelma@33175
  1525
  apply (rule topological_tendstoI)
himmelma@33175
  1526
  apply (drule (2) topological_tendstoD)
himmelma@33175
  1527
  apply (erule (1) eventually_elim2, simp)
himmelma@33175
  1528
  done
himmelma@33175
  1529
himmelma@33175
  1530
lemma Lim_transform_within:
himmelma@33175
  1531
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1532
  assumes "0 < d" "(\<forall>x'\<in>S. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x')"
himmelma@33175
  1533
          "(f ---> l) (at x within S)"
himmelma@33175
  1534
  shows   "(g ---> l) (at x within S)"
himmelma@33175
  1535
  using assms(1,3) unfolding Lim_within
himmelma@33175
  1536
  apply -
himmelma@33175
  1537
  apply (clarify, rename_tac e)
himmelma@33175
  1538
  apply (drule_tac x=e in spec, clarsimp, rename_tac r)
himmelma@33175
  1539
  apply (rule_tac x="min d r" in exI, clarsimp, rename_tac y)
himmelma@33175
  1540
  apply (drule_tac x=y in bspec, assumption, clarsimp)
himmelma@33175
  1541
  apply (simp add: assms(2))
himmelma@33175
  1542
  done
himmelma@33175
  1543
himmelma@33175
  1544
lemma Lim_transform_at:
himmelma@33175
  1545
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1546
  shows "0 < d \<Longrightarrow> (\<forall>x'. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x') \<Longrightarrow>
himmelma@33175
  1547
  (f ---> l) (at x) ==> (g ---> l) (at x)"
himmelma@33175
  1548
  apply (subst within_UNIV[symmetric])
himmelma@33175
  1549
  using Lim_transform_within[of d UNIV x f g l]
himmelma@33175
  1550
  by (auto simp add: within_UNIV)
himmelma@33175
  1551
himmelma@33175
  1552
text{* Common case assuming being away from some crucial point like 0. *}
himmelma@33175
  1553
himmelma@33175
  1554
lemma Lim_transform_away_within:
himmelma@33175
  1555
  fixes a b :: "'a::metric_space"
himmelma@33175
  1556
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1557
  assumes "a\<noteq>b" "\<forall>x\<in> S. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"
himmelma@33175
  1558
  and "(f ---> l) (at a within S)"
himmelma@33175
  1559
  shows "(g ---> l) (at a within S)"
himmelma@33175
  1560
proof-
himmelma@33175
  1561
  have "\<forall>x'\<in>S. 0 < dist x' a \<and> dist x' a < dist a b \<longrightarrow> f x' = g x'" using assms(2)
himmelma@33175
  1562
    apply auto apply(erule_tac x=x' in ballE) by (auto simp add: dist_commute)
himmelma@33175
  1563
  thus ?thesis using Lim_transform_within[of "dist a b" S a f g l] using assms(1,3) unfolding dist_nz by auto
himmelma@33175
  1564
qed
himmelma@33175
  1565
himmelma@33175
  1566
lemma Lim_transform_away_at:
himmelma@33175
  1567
  fixes a b :: "'a::metric_space"
himmelma@33175
  1568
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1569
  assumes ab: "a\<noteq>b" and fg: "\<forall>x. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"
himmelma@33175
  1570
  and fl: "(f ---> l) (at a)"
himmelma@33175
  1571
  shows "(g ---> l) (at a)"
himmelma@33175
  1572
  using Lim_transform_away_within[OF ab, of UNIV f g l] fg fl
himmelma@33175
  1573
  by (auto simp add: within_UNIV)
himmelma@33175
  1574
himmelma@33175
  1575
text{* Alternatively, within an open set. *}
himmelma@33175
  1576
himmelma@33175
  1577
lemma Lim_transform_within_open:
himmelma@33175
  1578
  fixes a :: "'a::metric_space"
himmelma@33175
  1579
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1580
  assumes "open S"  "a \<in> S"  "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> f x = g x"  "(f ---> l) (at a)"
himmelma@33175
  1581
  shows "(g ---> l) (at a)"
himmelma@33175
  1582
proof-
himmelma@33175
  1583
  from assms(1,2) obtain e::real where "e>0" and e:"ball a e \<subseteq> S" unfolding open_contains_ball by auto
himmelma@33175
  1584
  hence "\<forall>x'. 0 < dist x' a \<and> dist x' a < e \<longrightarrow> f x' = g x'" using assms(3)
himmelma@33175
  1585
    unfolding ball_def subset_eq apply auto apply(erule_tac x=x' in allE) apply(erule_tac x=x' in ballE) by(auto simp add: dist_commute)
himmelma@33175
  1586
  thus ?thesis using Lim_transform_at[of e a f g l] `e>0` assms(4) by auto
himmelma@33175
  1587
qed
himmelma@33175
  1588
himmelma@33175
  1589
text{* A congruence rule allowing us to transform limits assuming not at point. *}
himmelma@33175
  1590
himmelma@33175
  1591
(* FIXME: Only one congruence rule for tendsto can be used at a time! *)
himmelma@33175
  1592
himmelma@33175
  1593
lemma Lim_cong_within[cong add]:
himmelma@33175
  1594
  fixes a :: "'a::metric_space"
himmelma@33175
  1595
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1596
  shows "(\<And>x. x \<noteq> a \<Longrightarrow> f x = g x) ==> ((\<lambda>x. f x) ---> l) (at a within S) \<longleftrightarrow> ((g ---> l) (at a within S))"
himmelma@33175
  1597
  by (simp add: Lim_within dist_nz[symmetric])
himmelma@33175
  1598
himmelma@33175
  1599
lemma Lim_cong_at[cong add]:
himmelma@33175
  1600
  fixes a :: "'a::metric_space"
himmelma@33175
  1601
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1602
  shows "(\<And>x. x \<noteq> a ==> f x = g x) ==> (((\<lambda>x. f x) ---> l) (at a) \<longleftrightarrow> ((g ---> l) (at a)))"
himmelma@33175
  1603
  by (simp add: Lim_at dist_nz[symmetric])
himmelma@33175
  1604
himmelma@33175
  1605
text{* Useful lemmas on closure and set of possible sequential limits.*}
himmelma@33175
  1606
himmelma@33175
  1607
lemma closure_sequential:
himmelma@33175
  1608
  fixes l :: "'a::metric_space" (* TODO: generalize *)
himmelma@33175
  1609
  shows "l \<in> closure S \<longleftrightarrow> (\<exists>x. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially)" (is "?lhs = ?rhs")
himmelma@33175
  1610
proof
himmelma@33175
  1611
  assume "?lhs" moreover
himmelma@33175
  1612
  { assume "l \<in> S"
himmelma@33175
  1613
    hence "?rhs" using Lim_const[of l sequentially] by auto
himmelma@33175
  1614
  } moreover
himmelma@33175
  1615
  { assume "l islimpt S"
himmelma@33175
  1616
    hence "?rhs" unfolding islimpt_sequential by auto
himmelma@33175
  1617
  } ultimately
himmelma@33175
  1618
  show "?rhs" unfolding closure_def by auto
himmelma@33175
  1619
next
himmelma@33175
  1620
  assume "?rhs"
himmelma@33175
  1621
  thus "?lhs" unfolding closure_def unfolding islimpt_sequential by auto
himmelma@33175
  1622
qed
himmelma@33175
  1623
himmelma@33175
  1624
lemma closed_sequential_limits:
himmelma@33175
  1625
  fixes S :: "'a::metric_space set"
himmelma@33175
  1626
  shows "closed S \<longleftrightarrow> (\<forall>x l. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially \<longrightarrow> l \<in> S)"
himmelma@33175
  1627
  unfolding closed_limpt
himmelma@33175
  1628
  using closure_sequential [where 'a='a] closure_closed [where 'a='a] closed_limpt [where 'a='a] islimpt_sequential [where 'a='a] mem_delete [where 'a='a]
himmelma@33175
  1629
  by metis
himmelma@33175
  1630
himmelma@33175
  1631
lemma closure_approachable:
himmelma@33175
  1632
  fixes S :: "'a::metric_space set"
himmelma@33175
  1633
  shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e)"
himmelma@33175
  1634
  apply (auto simp add: closure_def islimpt_approachable)
himmelma@33175
  1635
  by (metis dist_self)
himmelma@33175
  1636
himmelma@33175
  1637
lemma closed_approachable:
himmelma@33175
  1638
  fixes S :: "'a::metric_space set"
himmelma@33175
  1639
  shows "closed S ==> (\<forall>e>0. \<exists>y\<in>S. dist y x < e) \<longleftrightarrow> x \<in> S"
himmelma@33175
  1640
  by (metis closure_closed closure_approachable)
himmelma@33175
  1641
himmelma@33175
  1642
text{* Some other lemmas about sequences. *}
himmelma@33175
  1643
himmelma@33175
  1644
lemma seq_offset:
himmelma@33175
  1645
  fixes l :: "'a::metric_space" (* TODO: generalize *)
himmelma@33175
  1646
  shows "(f ---> l) sequentially ==> ((\<lambda>i. f( i + k)) ---> l) sequentially"
himmelma@33175
  1647
  apply (auto simp add: Lim_sequentially)
himmelma@33175
  1648
  by (metis trans_le_add1 )
himmelma@33175
  1649
himmelma@33175
  1650
lemma seq_offset_neg:
himmelma@33175
  1651
  "(f ---> l) sequentially ==> ((\<lambda>i. f(i - k)) ---> l) sequentially"
himmelma@33175
  1652
  apply (rule topological_tendstoI)
himmelma@33175
  1653
  apply (drule (2) topological_tendstoD)
himmelma@33175
  1654
  apply (simp only: eventually_sequentially)
himmelma@33175
  1655
  apply (subgoal_tac "\<And>N k (n::nat). N + k <= n ==> N <= n - k")
himmelma@33175
  1656
  apply metis
himmelma@33175
  1657
  by arith
himmelma@33175
  1658
himmelma@33175
  1659
lemma seq_offset_rev:
himmelma@33175
  1660
  "((\<lambda>i. f(i + k)) ---> l) sequentially ==> (f ---> l) sequentially"
himmelma@33175
  1661
  apply (rule topological_tendstoI)
himmelma@33175
  1662
  apply (drule (2) topological_tendstoD)
himmelma@33175
  1663
  apply (simp only: eventually_sequentially)
himmelma@33175
  1664
  apply (subgoal_tac "\<And>N k (n::nat). N + k <= n ==> N <= n - k \<and> (n - k) + k = n")
himmelma@33175
  1665
  by metis arith
himmelma@33175
  1666
himmelma@33175
  1667
lemma seq_harmonic: "((\<lambda>n. inverse (real n)) ---> 0) sequentially"
himmelma@33175
  1668
proof-
himmelma@33175
  1669
  { fix e::real assume "e>0"
himmelma@33175
  1670
    hence "\<exists>N::nat. \<forall>n::nat\<ge>N. inverse (real n) < e"
himmelma@33175
  1671
      using real_arch_inv[of e] apply auto apply(rule_tac x=n in exI)
himmelma@33175
  1672
      by (metis not_le le_imp_inverse_le not_less real_of_nat_gt_zero_cancel_iff real_of_nat_less_iff xt1(7))
himmelma@33175
  1673
  }
himmelma@33175
  1674
  thus ?thesis unfolding Lim_sequentially dist_norm by simp
himmelma@33175
  1675
qed
himmelma@33175
  1676
himmelma@33175
  1677
text{* More properties of closed balls. *}
himmelma@33175
  1678
himmelma@33175
  1679
lemma closed_cball: "closed (cball x e)"
himmelma@33175
  1680
unfolding cball_def closed_def
himmelma@33175
  1681
unfolding Collect_neg_eq [symmetric] not_le
himmelma@33175
  1682
apply (clarsimp simp add: open_dist, rename_tac y)
himmelma@33175
  1683
apply (rule_tac x="dist x y - e" in exI, clarsimp)
himmelma@33175
  1684
apply (rename_tac x')
himmelma@33175
  1685
apply (cut_tac x=x and y=x' and z=y in dist_triangle)
himmelma@33175
  1686
apply simp
himmelma@33175
  1687
done
himmelma@33175
  1688
himmelma@33175
  1689
lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"
himmelma@33175
  1690
proof-
himmelma@33175
  1691
  { fix x and e::real assume "x\<in>S" "e>0" "ball x e \<subseteq> S"
himmelma@33175
  1692
    hence "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)
himmelma@33175
  1693
  } moreover
himmelma@33175
  1694
  { fix x and e::real assume "x\<in>S" "e>0" "cball x e \<subseteq> S"
himmelma@33175
  1695
    hence "\<exists>d>0. ball x d \<subseteq> S" unfolding subset_eq apply(rule_tac x="e/2" in exI) by auto
himmelma@33175
  1696
  } ultimately
himmelma@33175
  1697
  show ?thesis unfolding open_contains_ball by auto
himmelma@33175
  1698
qed
himmelma@33175
  1699
himmelma@33175
  1700
lemma open_contains_cball_eq: "open S ==> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"
himmelma@33175
  1701
  by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball mem_def)
himmelma@33175
  1702
himmelma@33175
  1703
lemma mem_interior_cball: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S)"
himmelma@33175
  1704
  apply (simp add: interior_def, safe)
himmelma@33175
  1705
  apply (force simp add: open_contains_cball)
himmelma@33175
  1706
  apply (rule_tac x="ball x e" in exI)
himmelma@33175
  1707
  apply (simp add: open_ball centre_in_ball subset_trans [OF ball_subset_cball])
himmelma@33175
  1708
  done
himmelma@33175
  1709
himmelma@33175
  1710
lemma islimpt_ball:
himmelma@33175
  1711
  fixes x y :: "'a::{real_normed_vector,perfect_space}"
himmelma@33175
  1712
  shows "y islimpt ball x e \<longleftrightarrow> 0 < e \<and> y \<in> cball x e" (is "?lhs = ?rhs")
himmelma@33175
  1713
proof
himmelma@33175
  1714
  assume "?lhs"
himmelma@33175
  1715
  { assume "e \<le> 0"
himmelma@33175
  1716
    hence *:"ball x e = {}" using ball_eq_empty[of x e] by auto
himmelma@33175
  1717
    have False using `?lhs` unfolding * using islimpt_EMPTY[of y] by auto
himmelma@33175
  1718
  }
himmelma@33175
  1719
  hence "e > 0" by (metis not_less)
himmelma@33175
  1720
  moreover
himmelma@33175
  1721
  have "y \<in> cball x e" using closed_cball[of x e] islimpt_subset[of y "ball x e" "cball x e"] ball_subset_cball[of x e] `?lhs` unfolding closed_limpt by auto
himmelma@33175
  1722
  ultimately show "?rhs" by auto
himmelma@33175
  1723
next
himmelma@33175
  1724
  assume "?rhs" hence "e>0"  by auto
himmelma@33175
  1725
  { fix d::real assume "d>0"
himmelma@33175
  1726
    have "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1727
    proof(cases "d \<le> dist x y")
himmelma@33175
  1728
      case True thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1729
      proof(cases "x=y")
himmelma@33175
  1730
        case True hence False using `d \<le> dist x y` `d>0` by auto
himmelma@33175
  1731
        thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d" by auto
himmelma@33175
  1732
      next
himmelma@33175
  1733
        case False
himmelma@33175
  1734
himmelma@33175
  1735
        have "dist x (y - (d / (2 * dist y x)) *\<^sub>R (y - x))
himmelma@33175
  1736
              = norm (x - y + (d / (2 * norm (y - x))) *\<^sub>R (y - x))"
himmelma@33175
  1737
          unfolding mem_cball mem_ball dist_norm diff_diff_eq2 diff_add_eq[THEN sym] by auto
himmelma@33175
  1738
        also have "\<dots> = \<bar>- 1 + d / (2 * norm (x - y))\<bar> * norm (x - y)"
himmelma@33175
  1739
          using scaleR_left_distrib[of "- 1" "d / (2 * norm (y - x))", THEN sym, of "y - x"]
himmelma@33175
  1740
          unfolding scaleR_minus_left scaleR_one
himmelma@33175
  1741
          by (auto simp add: norm_minus_commute)
himmelma@33175
  1742
        also have "\<dots> = \<bar>- norm (x - y) + d / 2\<bar>"
himmelma@33175
  1743
          unfolding abs_mult_pos[of "norm (x - y)", OF norm_ge_zero[of "x - y"]]
himmelma@33175
  1744
          unfolding real_add_mult_distrib using `x\<noteq>y`[unfolded dist_nz, unfolded dist_norm] by auto
himmelma@33175
  1745
        also have "\<dots> \<le> e - d/2" using `d \<le> dist x y` and `d>0` and `?rhs` by(auto simp add: dist_norm)
himmelma@33175
  1746
        finally have "y - (d / (2 * dist y x)) *\<^sub>R (y - x) \<in> ball x e" using `d>0` by auto
himmelma@33175
  1747
himmelma@33175
  1748
        moreover
himmelma@33175
  1749
himmelma@33175
  1750
        have "(d / (2*dist y x)) *\<^sub>R (y - x) \<noteq> 0"
himmelma@33175
  1751
          using `x\<noteq>y`[unfolded dist_nz] `d>0` unfolding scaleR_eq_0_iff by (auto simp add: dist_commute)
himmelma@33175
  1752
        moreover
himmelma@33175
  1753
        have "dist (y - (d / (2 * dist y x)) *\<^sub>R (y - x)) y < d" unfolding dist_norm apply simp unfolding norm_minus_cancel
himmelma@33175
  1754
          using `d>0` `x\<noteq>y`[unfolded dist_nz] dist_commute[of x y]
himmelma@33175
  1755
          unfolding dist_norm by auto
himmelma@33175
  1756
        ultimately show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d" by (rule_tac  x="y - (d / (2*dist y x)) *\<^sub>R (y - x)" in bexI) auto
himmelma@33175
  1757
      qed
himmelma@33175
  1758
    next
himmelma@33175
  1759
      case False hence "d > dist x y" by auto
himmelma@33175
  1760
      show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1761
      proof(cases "x=y")
himmelma@33175
  1762
        case True
himmelma@33175
  1763
        obtain z where **: "z \<noteq> y" "dist z y < min e d"
himmelma@33175
  1764
          using perfect_choose_dist[of "min e d" y]
himmelma@33175
  1765
          using `d > 0` `e>0` by auto
himmelma@33175
  1766
        show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1767
          unfolding `x = y`
himmelma@33175
  1768
          using `z \<noteq> y` **
himmelma@33175
  1769
          by (rule_tac x=z in bexI, auto simp add: dist_commute)
himmelma@33175
  1770
      next
himmelma@33175
  1771
        case False thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1772
          using `d>0` `d > dist x y` `?rhs` by(rule_tac x=x in bexI, auto)
himmelma@33175
  1773
      qed
himmelma@33175
  1774
    qed  }
himmelma@33175
  1775
  thus "?lhs" unfolding mem_cball islimpt_approachable mem_ball by auto
himmelma@33175
  1776
qed
himmelma@33175
  1777
himmelma@33175
  1778
lemma closure_ball_lemma:
himmelma@33175
  1779
  fixes x y :: "'a::real_normed_vector"
himmelma@33175
  1780
  assumes "x \<noteq> y" shows "y islimpt ball x (dist x y)"
himmelma@33175
  1781
proof (rule islimptI)
himmelma@33175
  1782
  fix T assume "y \<in> T" "open T"
himmelma@33175
  1783
  then obtain r where "0 < r" "\<forall>z. dist z y < r \<longrightarrow> z \<in> T"
himmelma@33175
  1784
    unfolding open_dist by fast
himmelma@33175
  1785
  (* choose point between x and y, within distance r of y. *)
himmelma@33175
  1786
  def k \<equiv> "min 1 (r / (2 * dist x y))"
himmelma@33175
  1787
  def z \<equiv> "y + scaleR k (x - y)"
himmelma@33175
  1788
  have z_def2: "z = x + scaleR (1 - k) (y - x)"
himmelma@33175
  1789
    unfolding z_def by (simp add: algebra_simps)
himmelma@33175
  1790
  have "dist z y < r"
himmelma@33175
  1791
    unfolding z_def k_def using `0 < r`
himmelma@33175
  1792
    by (simp add: dist_norm min_def)
himmelma@33175
  1793
  hence "z \<in> T" using `\<forall>z. dist z y < r \<longrightarrow> z \<in> T` by simp
himmelma@33175
  1794
  have "dist x z < dist x y"
himmelma@33175
  1795
    unfolding z_def2 dist_norm
himmelma@33175
  1796
    apply (simp add: norm_minus_commute)
himmelma@33175
  1797
    apply (simp only: dist_norm [symmetric])
himmelma@33175
  1798
    apply (subgoal_tac "\<bar>1 - k\<bar> * dist x y < 1 * dist x y", simp)
himmelma@33175
  1799
    apply (rule mult_strict_right_mono)
himmelma@33175
  1800
    apply (simp add: k_def divide_pos_pos zero_less_dist_iff `0 < r` `x \<noteq> y`)
himmelma@33175
  1801
    apply (simp add: zero_less_dist_iff `x \<noteq> y`)
himmelma@33175
  1802
    done
himmelma@33175
  1803
  hence "z \<in> ball x (dist x y)" by simp
himmelma@33175
  1804
  have "z \<noteq> y"
himmelma@33175
  1805
    unfolding z_def k_def using `x \<noteq> y` `0 < r`
himmelma@33175
  1806
    by (simp add: min_def)
himmelma@33175
  1807
  show "\<exists>z\<in>ball x (dist x y). z \<in> T \<and> z \<noteq> y"
himmelma@33175
  1808
    using `z \<in> ball x (dist x y)` `z \<in> T` `z \<noteq> y`
himmelma@33175
  1809
    by fast
himmelma@33175
  1810
qed
himmelma@33175
  1811
himmelma@33175
  1812
lemma closure_ball:
himmelma@33175
  1813
  fixes x :: "'a::real_normed_vector"
himmelma@33175
  1814
  shows "0 < e \<Longrightarrow> closure (ball x e) = cball x e"
himmelma@33175
  1815
apply (rule equalityI)
himmelma@33175
  1816
apply (rule closure_minimal)
himmelma@33175
  1817
apply (rule ball_subset_cball)
himmelma@33175
  1818
apply (rule closed_cball)
himmelma@33175
  1819
apply (rule subsetI, rename_tac y)
himmelma@33175
  1820
apply (simp add: le_less [where 'a=real])
himmelma@33175
  1821
apply (erule disjE)
himmelma@33175
  1822
apply (rule subsetD [OF closure_subset], simp)
himmelma@33175
  1823
apply (simp add: closure_def)
himmelma@33175
  1824
apply clarify
himmelma@33175
  1825
apply (rule closure_ball_lemma)
himmelma@33175
  1826
apply (simp add: zero_less_dist_iff)
himmelma@33175
  1827
done
himmelma@33175
  1828
himmelma@33175
  1829
(* In a trivial vector space, this fails for e = 0. *)
himmelma@33175
  1830
lemma interior_cball:
himmelma@33175
  1831
  fixes x :: "'a::{real_normed_vector, perfect_space}"
himmelma@33175
  1832
  shows "interior (cball x e) = ball x e"
himmelma@33175
  1833
proof(cases "e\<ge>0")
himmelma@33175
  1834
  case False note cs = this
himmelma@33175
  1835
  from cs have "ball x e = {}" using ball_empty[of e x] by auto moreover
himmelma@33175
  1836
  { fix y assume "y \<in> cball x e"
himmelma@33175
  1837
    hence False unfolding mem_cball using dist_nz[of x y] cs by auto  }
himmelma@33175
  1838
  hence "cball x e = {}" by auto
himmelma@33175
  1839
  hence "interior (cball x e) = {}" using interior_empty by auto
himmelma@33175
  1840
  ultimately show ?thesis by blast
himmelma@33175
  1841
next
himmelma@33175
  1842
  case True note cs = this
himmelma@33175
  1843
  have "ball x e \<subseteq> cball x e" using ball_subset_cball by auto moreover
himmelma@33175
  1844
  { fix S y assume as: "S \<subseteq> cball x e" "open S" "y\<in>S"
himmelma@33175
  1845
    then obtain d where "d>0" and d:"\<forall>x'. dist x' y < d \<longrightarrow> x' \<in> S" unfolding open_dist by blast
himmelma@33175
  1846
himmelma@33175
  1847
    then obtain xa where xa_y: "xa \<noteq> y" and xa: "dist xa y < d"
himmelma@33175
  1848
      using perfect_choose_dist [of d] by auto
himmelma@33175
  1849
    have "xa\<in>S" using d[THEN spec[where x=xa]] using xa by(auto simp add: dist_commute)
himmelma@33175
  1850
    hence xa_cball:"xa \<in> cball x e" using as(1) by auto
himmelma@33175
  1851
himmelma@33175
  1852
    hence "y \<in> ball x e" proof(cases "x = y")
himmelma@33175
  1853
      case True
himmelma@33175
  1854
      hence "e>0" using xa_y[unfolded dist_nz] xa_cball[unfolded mem_cball] by (auto simp add: dist_commute)
himmelma@33175
  1855
      thus "y \<in> ball x e" using `x = y ` by simp
himmelma@33175
  1856
    next
himmelma@33175
  1857
      case False
himmelma@33175
  1858
      have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) y < d" unfolding dist_norm
himmelma@33175
  1859
        using `d>0` norm_ge_zero[of "y - x"] `x \<noteq> y` by auto
himmelma@33175
  1860
      hence *:"y + (d / 2 / dist y x) *\<^sub>R (y - x) \<in> cball x e" using d as(1)[unfolded subset_eq] by blast
himmelma@33175
  1861
      have "y - x \<noteq> 0" using `x \<noteq> y` by auto
himmelma@33175
  1862
      hence **:"d / (2 * norm (y - x)) > 0" unfolding zero_less_norm_iff[THEN sym]
himmelma@33175
  1863
        using `d>0` divide_pos_pos[of d "2*norm (y - x)"] by auto
himmelma@33175
  1864
himmelma@33175
  1865
      have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) x = norm (y + (d / (2 * norm (y - x))) *\<^sub>R y - (d / (2 * norm (y - x))) *\<^sub>R x - x)"
himmelma@33175
  1866
        by (auto simp add: dist_norm algebra_simps)
himmelma@33175
  1867
      also have "\<dots> = norm ((1 + d / (2 * norm (y - x))) *\<^sub>R (y - x))"
himmelma@33175
  1868
        by (auto simp add: algebra_simps)
himmelma@33175
  1869
      also have "\<dots> = \<bar>1 + d / (2 * norm (y - x))\<bar> * norm (y - x)"
himmelma@33175
  1870
        using ** by auto
himmelma@33175
  1871
      also have "\<dots> = (dist y x) + d/2"using ** by (auto simp add: left_distrib dist_norm)
himmelma@33175
  1872
      finally have "e \<ge> dist x y +d/2" using *[unfolded mem_cball] by (auto simp add: dist_commute)
himmelma@33175
  1873
      thus "y \<in> ball x e" unfolding mem_ball using `d>0` by auto
himmelma@33175
  1874
    qed  }
himmelma@33175
  1875
  hence "\<forall>S \<subseteq> cball x e. open S \<longrightarrow> S \<subseteq> ball x e" by auto
himmelma@33175
  1876
  ultimately show ?thesis using interior_unique[of "ball x e" "cball x e"] using open_ball[of x e] by auto
himmelma@33175
  1877
qed
himmelma@33175
  1878
himmelma@33175
  1879
lemma frontier_ball:
himmelma@33175
  1880
  fixes a :: "'a::real_normed_vector"
himmelma@33175
  1881
  shows "0 < e ==> frontier(ball a e) = {x. dist a x = e}"
himmelma@33175
  1882
  apply (simp add: frontier_def closure_ball interior_open open_ball order_less_imp_le)
himmelma@33175
  1883
  apply (simp add: expand_set_eq)
himmelma@33175
  1884
  by arith
himmelma@33175
  1885
himmelma@33175
  1886
lemma frontier_cball:
himmelma@33175
  1887
  fixes a :: "'a::{real_normed_vector, perfect_space}"
himmelma@33175
  1888
  shows "frontier(cball a e) = {x. dist a x = e}"
himmelma@33175
  1889
  apply (simp add: frontier_def interior_cball closed_cball closure_closed order_less_imp_le)
himmelma@33175
  1890
  apply (simp add: expand_set_eq)
himmelma@33175
  1891
  by arith
himmelma@33175
  1892
himmelma@33175
  1893
lemma cball_eq_empty: "(cball x e = {}) \<longleftrightarrow> e < 0"
himmelma@33175
  1894
  apply (simp add: expand_set_eq not_le)
himmelma@33175
  1895
  by (metis zero_le_dist dist_self order_less_le_trans)
himmelma@33175
  1896
lemma cball_empty: "e < 0 ==> cball x e = {}" by (simp add: cball_eq_empty)
himmelma@33175
  1897
himmelma@33175
  1898
lemma cball_eq_sing:
himmelma@33175
  1899
  fixes x :: "'a::perfect_space"
himmelma@33175
  1900
  shows "(cball x e = {x}) \<longleftrightarrow> e = 0"
himmelma@33175
  1901
proof (rule linorder_cases)
himmelma@33175
  1902
  assume e: "0 < e"
himmelma@33175
  1903
  obtain a where "a \<noteq> x" "dist a x < e"
himmelma@33175
  1904
    using perfect_choose_dist [OF e] by auto
himmelma@33175
  1905
  hence "a \<noteq> x" "dist x a \<le> e" by (auto simp add: dist_commute)
himmelma@33175
  1906
  with e show ?thesis by (auto simp add: expand_set_eq)
himmelma@33175
  1907
qed auto
himmelma@33175
  1908
himmelma@33175
  1909
lemma cball_sing:
himmelma@33175
  1910
  fixes x :: "'a::metric_space"
himmelma@33175
  1911
  shows "e = 0 ==> cball x e = {x}"
himmelma@33175
  1912
  by (auto simp add: expand_set_eq)
himmelma@33175
  1913
himmelma@33175
  1914
text{* For points in the interior, localization of limits makes no difference.   *}
himmelma@33175
  1915
himmelma@33175
  1916
lemma eventually_within_interior:
himmelma@33175
  1917
  assumes "x \<in> interior S"
himmelma@33175
  1918
  shows "eventually P (at x within S) \<longleftrightarrow> eventually P (at x)" (is "?lhs = ?rhs")
himmelma@33175
  1919
proof-
himmelma@33175
  1920
  from assms obtain T where T: "open T" "x \<in> T" "T \<subseteq> S"
himmelma@33175
  1921
    unfolding interior_def by fast
himmelma@33175
  1922
  { assume "?lhs"
himmelma@33175
  1923
    then obtain A where "open A" "x \<in> A" "\<forall>y\<in>A. y \<noteq> x \<longrightarrow> y \<in> S \<longrightarrow> P y"
himmelma@33175
  1924
      unfolding Limits.eventually_within Limits.eventually_at_topological
himmelma@33175
  1925
      by auto
himmelma@33175
  1926
    with T have "open (A \<inter> T)" "x \<in> A \<inter> T" "\<forall>y\<in>(A \<inter> T). y \<noteq> x \<longrightarrow> P y"
himmelma@33175
  1927
      by auto
himmelma@33175
  1928
    then have "?rhs"
himmelma@33175
  1929
      unfolding Limits.eventually_at_topological by auto
himmelma@33175
  1930
  } moreover
himmelma@33175
  1931
  { assume "?rhs" hence "?lhs"
himmelma@33175
  1932
      unfolding Limits.eventually_within
himmelma@33175
  1933
      by (auto elim: eventually_elim1)
himmelma@33175
  1934
  } ultimately
himmelma@33175
  1935
  show "?thesis" ..
himmelma@33175
  1936
qed
himmelma@33175
  1937
himmelma@33175
  1938
lemma lim_within_interior:
himmelma@33175
  1939
  "x \<in> interior S \<Longrightarrow> (f ---> l) (at x within S) \<longleftrightarrow> (f ---> l) (at x)"
himmelma@33175
  1940
  unfolding tendsto_def by (simp add: eventually_within_interior)
himmelma@33175
  1941
himmelma@33175
  1942
lemma netlimit_within_interior:
himmelma@33175
  1943
  fixes x :: "'a::{perfect_space, real_normed_vector}"
himmelma@33175
  1944
    (* FIXME: generalize to perfect_space *)
himmelma@33175
  1945
  assumes "x \<in> interior S"
himmelma@33175
  1946
  shows "netlimit(at x within S) = x" (is "?lhs = ?rhs")
himmelma@33175
  1947
proof-
himmelma@33175
  1948
  from assms obtain e::real where e:"e>0" "ball x e \<subseteq> S" using open_interior[of S] unfolding open_contains_ball using interior_subset[of S] by auto
himmelma@33175
  1949
  hence "\<not> trivial_limit (at x within S)" using islimpt_subset[of x "ball x e" S] unfolding trivial_limit_within islimpt_ball centre_in_cball by auto
himmelma@33175
  1950
  thus ?thesis using netlimit_within by auto
himmelma@33175
  1951
qed
himmelma@33175
  1952
himmelma@33175
  1953
subsection{* Boundedness. *}
himmelma@33175
  1954
himmelma@33175
  1955
  (* FIXME: This has to be unified with BSEQ!! *)
himmelma@33175
  1956
definition
himmelma@33175
  1957
  bounded :: "'a::metric_space set \<Rightarrow> bool" where
himmelma@33175
  1958
  "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"
himmelma@33175
  1959
himmelma@33175
  1960
lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"
himmelma@33175
  1961
unfolding bounded_def
himmelma@33175
  1962
apply safe
himmelma@33175
  1963
apply (rule_tac x="dist a x + e" in exI, clarify)
himmelma@33175
  1964
apply (drule (1) bspec)
himmelma@33175
  1965
apply (erule order_trans [OF dist_triangle add_left_mono])
himmelma@33175
  1966
apply auto
himmelma@33175
  1967
done
himmelma@33175
  1968
himmelma@33175
  1969
lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"
himmelma@33175
  1970
unfolding bounded_any_center [where a=0]
himmelma@33175
  1971
by (simp add: dist_norm)
himmelma@33175
  1972
himmelma@33175
  1973
lemma bounded_empty[simp]: "bounded {}" by (simp add: bounded_def)
himmelma@33175
  1974
lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T ==> bounded S"
himmelma@33175
  1975
  by (metis bounded_def subset_eq)
himmelma@33175
  1976
himmelma@33175
  1977
lemma bounded_interior[intro]: "bounded S ==> bounded(interior S)"
himmelma@33175
  1978
  by (metis bounded_subset interior_subset)
himmelma@33175
  1979
himmelma@33175
  1980
lemma bounded_closure[intro]: assumes "bounded S" shows "bounded(closure S)"
himmelma@33175
  1981
proof-
himmelma@33175
  1982
  from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a" unfolding bounded_def by auto
himmelma@33175
  1983
  { fix y assume "y \<in> closure S"
himmelma@33175
  1984
    then obtain f where f: "\<forall>n. f n \<in> S"  "(f ---> y) sequentially"
himmelma@33175
  1985
      unfolding closure_sequential by auto
himmelma@33175
  1986
    have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp
himmelma@33175
  1987
    hence "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"
himmelma@33175
  1988
      by (rule eventually_mono, simp add: f(1))
himmelma@33175
  1989
    have "dist x y \<le> a"
himmelma@33175
  1990
      apply (rule Lim_dist_ubound [of sequentially f])
himmelma@33175
  1991
      apply (rule trivial_limit_sequentially)
himmelma@33175
  1992
      apply (rule f(2))
himmelma@33175
  1993
      apply fact
himmelma@33175
  1994
      done
himmelma@33175
  1995
  }
himmelma@33175
  1996
  thus ?thesis unfolding bounded_def by auto
himmelma@33175
  1997
qed
himmelma@33175
  1998
himmelma@33175
  1999
lemma bounded_cball[simp,intro]: "bounded (cball x e)"
himmelma@33175
  2000
  apply (simp add: bounded_def)
himmelma@33175
  2001
  apply (rule_tac x=x in exI)
himmelma@33175
  2002
  apply (rule_tac x=e in exI)
himmelma@33175
  2003
  apply auto
himmelma@33175
  2004
  done
himmelma@33175
  2005
himmelma@33175
  2006
lemma bounded_ball[simp,intro]: "bounded(ball x e)"
himmelma@33175
  2007
  by (metis ball_subset_cball bounded_cball bounded_subset)
himmelma@33175
  2008
himmelma@33175
  2009
lemma finite_imp_bounded[intro]: assumes "finite S" shows "bounded S"
himmelma@33175
  2010
proof-
himmelma@33175
  2011
  { fix a F assume as:"bounded F"
himmelma@33175
  2012
    then obtain x e where "\<forall>y\<in>F. dist x y \<le> e" unfolding bounded_def by auto
himmelma@33175
  2013
    hence "\<forall>y\<in>(insert a F). dist x y \<le> max e (dist x a)" by auto
himmelma@33175
  2014
    hence "bounded (insert a F)" unfolding bounded_def by (intro exI)
himmelma@33175
  2015
  }
himmelma@33175
  2016
  thus ?thesis using finite_induct[of S bounded]  using bounded_empty assms by auto
himmelma@33175
  2017
qed
himmelma@33175
  2018
himmelma@33175
  2019
lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"
himmelma@33175
  2020
  apply (auto simp add: bounded_def)
himmelma@33175
  2021
  apply (rename_tac x y r s)
himmelma@33175
  2022
  apply (rule_tac x=x in exI)
himmelma@33175
  2023
  apply (rule_tac x="max r (dist x y + s)" in exI)
himmelma@33175
  2024
  apply (rule ballI, rename_tac z, safe)
himmelma@33175
  2025
  apply (drule (1) bspec, simp)
himmelma@33175
  2026
  apply (drule (1) bspec)
himmelma@33175
  2027
  apply (rule min_max.le_supI2)
himmelma@33175
  2028
  apply (erule order_trans [OF dist_triangle add_left_mono])
himmelma@33175
  2029
  done
himmelma@33175
  2030
himmelma@33175
  2031
lemma bounded_Union[intro]: "finite F \<Longrightarrow> (\<forall>S\<in>F. bounded S) \<Longrightarrow> bounded(\<Union>F)"
himmelma@33175
  2032
  by (induct rule: finite_induct[of F], auto)
himmelma@33175
  2033
himmelma@33175
  2034
lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x <= b)"
himmelma@33175
  2035
  apply (simp add: bounded_iff)
himmelma@33175
  2036
  apply (subgoal_tac "\<And>x (y::real). 0 < 1 + abs y \<and> (x <= y \<longrightarrow> x <= 1 + abs y)")
himmelma@33175
  2037
  by metis arith
himmelma@33175
  2038
himmelma@33175
  2039
lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"
himmelma@33175
  2040
  by (metis Int_lower1 Int_lower2 bounded_subset)
himmelma@33175
  2041
himmelma@33175
  2042
lemma bounded_diff[intro]: "bounded S ==> bounded (S - T)"
himmelma@33175
  2043
apply (metis Diff_subset bounded_subset)
himmelma@33175
  2044
done
himmelma@33175
  2045
himmelma@33175
  2046
lemma bounded_insert[intro]:"bounded(insert x S) \<longleftrightarrow> bounded S"
himmelma@33175
  2047
  by (metis Diff_cancel Un_empty_right Un_insert_right bounded_Un bounded_subset finite.emptyI finite_imp_bounded infinite_remove subset_insertI)
himmelma@33175
  2048
himmelma@33175
  2049
lemma not_bounded_UNIV[simp, intro]:
himmelma@33175
  2050
  "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"
himmelma@33175
  2051
proof(auto simp add: bounded_pos not_le)
himmelma@33175
  2052
  obtain x :: 'a where "x \<noteq> 0"
himmelma@33175
  2053
    using perfect_choose_dist [OF zero_less_one] by fast
himmelma@33175
  2054
  fix b::real  assume b: "b >0"
himmelma@33175
  2055
  have b1: "b +1 \<ge> 0" using b by simp
himmelma@33175
  2056
  with `x \<noteq> 0` have "b < norm (scaleR (b + 1) (sgn x))"
himmelma@33175
  2057
    by (simp add: norm_sgn)
himmelma@33175
  2058
  then show "\<exists>x::'a. b < norm x" ..
himmelma@33175
  2059
qed
himmelma@33175
  2060
himmelma@33175
  2061
lemma bounded_linear_image:
himmelma@33175
  2062
  assumes "bounded S" "bounded_linear f"
himmelma@33175
  2063
  shows "bounded(f ` S)"
himmelma@33175
  2064
proof-
himmelma@33175
  2065
  from assms(1) obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto
himmelma@33175
  2066
  from assms(2) obtain B where B:"B>0" "\<forall>x. norm (f x) \<le> B * norm x" using bounded_linear.pos_bounded by (auto simp add: mult_ac)
himmelma@33175
  2067
  { fix x assume "x\<in>S"
himmelma@33175
  2068
    hence "norm x \<le> b" using b by auto
himmelma@33175
  2069
    hence "norm (f x) \<le> B * b" using B(2) apply(erule_tac x=x in allE)
himmelma@33175
  2070
      by (metis B(1) B(2) real_le_trans real_mult_le_cancel_iff2)
himmelma@33175
  2071
  }
himmelma@33175
  2072
  thus ?thesis unfolding bounded_pos apply(rule_tac x="b*B" in exI)
himmelma@33175
  2073
    using b B real_mult_order[of b B] by (auto simp add: real_mult_commute)
himmelma@33175
  2074
qed
himmelma@33175
  2075
himmelma@33175
  2076
lemma bounded_scaling:
himmelma@33175
  2077
  fixes S :: "'a::real_normed_vector set"
himmelma@33175
  2078
  shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x) ` S)"
himmelma@33175
  2079
  apply (rule bounded_linear_image, assumption)
himmelma@33175
  2080
  apply (rule scaleR.bounded_linear_right)
himmelma@33175
  2081
  done
himmelma@33175
  2082
himmelma@33175
  2083
lemma bounded_translation:
himmelma@33175
  2084
  fixes S :: "'a::real_normed_vector set"
himmelma@33175
  2085
  assumes "bounded S" shows "bounded ((\<lambda>x. a + x) ` S)"
himmelma@33175
  2086
proof-
himmelma@33175
  2087
  from assms obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto
himmelma@33175
  2088
  { fix x assume "x\<in>S"
himmelma@33175
  2089
    hence "norm (a + x) \<le> b + norm a" using norm_triangle_ineq[of a x] b by auto
himmelma@33175
  2090
  }
himmelma@33175
  2091
  thus ?thesis unfolding bounded_pos using norm_ge_zero[of a] b(1) using add_strict_increasing[of b 0 "norm a"]
himmelma@33175
  2092
    by (auto intro!: add exI[of _ "b + norm a"])
himmelma@33175
  2093
qed
himmelma@33175
  2094
himmelma@33175
  2095
himmelma@33175
  2096
text{* Some theorems on sups and infs using the notion "bounded". *}
himmelma@33175
  2097
himmelma@33175
  2098
lemma bounded_real:
himmelma@33175
  2099
  fixes S :: "real set"
himmelma@33175
  2100
  shows "bounded S \<longleftrightarrow>  (\<exists>a. \<forall>x\<in>S. abs x <= a)"
himmelma@33175
  2101
  by (simp add: bounded_iff)
himmelma@33175
  2102
paulson@33270
  2103
lemma bounded_has_Sup:
paulson@33270
  2104
  fixes S :: "real set"
paulson@33270
  2105
  assumes "bounded S" "S \<noteq> {}"
paulson@33270
  2106
  shows "\<forall>x\<in>S. x <= Sup S" and "\<forall>b. (\<forall>x\<in>S. x <= b) \<longrightarrow> Sup S <= b"
paulson@33270
  2107
proof
paulson@33270
  2108
  fix x assume "x\<in>S"
paulson@33270
  2109
  thus "x \<le> Sup S"
paulson@33270
  2110
    by (metis SupInf.Sup_upper abs_le_D1 assms(1) bounded_real)
paulson@33270
  2111
next
paulson@33270
  2112
  show "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b" using assms
paulson@33270
  2113
    by (metis SupInf.Sup_least)
paulson@33270
  2114
qed
paulson@33270
  2115
paulson@33270
  2116
lemma Sup_insert:
paulson@33270
  2117
  fixes S :: "real set"
paulson@33270
  2118
  shows "bounded S ==> Sup(insert x S) = (if S = {} then x else max x (Sup S))" 
paulson@33270
  2119
by auto (metis Int_absorb Sup_insert_nonempty assms bounded_has_Sup(1) disjoint_iff_not_equal) 
paulson@33270
  2120
paulson@33270
  2121
lemma Sup_insert_finite:
paulson@33270
  2122
  fixes S :: "real set"
paulson@33270
  2123
  shows "finite S \<Longrightarrow> Sup(insert x S) = (if S = {} then x else max x (Sup S))"
paulson@33270
  2124
  apply (rule Sup_insert)
paulson@33270
  2125
  apply (rule finite_imp_bounded)
paulson@33270
  2126
  by simp
paulson@33270
  2127
paulson@33270
  2128
lemma bounded_has_Inf:
paulson@33270
  2129
  fixes S :: "real set"
paulson@33270
  2130
  assumes "bounded S"  "S \<noteq> {}"
paulson@33270
  2131
  shows "\<forall>x\<in>S. x >= Inf S" and "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S >= b"
himmelma@33175
  2132
proof
himmelma@33175
  2133
  fix x assume "x\<in>S"
himmelma@33175
  2134
  from assms(1) obtain a where a:"\<forall>x\<in>S. \<bar>x\<bar> \<le> a" unfolding bounded_real by auto
paulson@33270
  2135
  thus "x \<ge> Inf S" using `x\<in>S`
paulson@33270
  2136
    by (metis Inf_lower_EX abs_le_D2 minus_le_iff)
himmelma@33175
  2137
next
paulson@33270
  2138
  show "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S \<ge> b" using assms
paulson@33270
  2139
    by (metis SupInf.Inf_greatest)
paulson@33270
  2140
qed
paulson@33270
  2141
paulson@33270
  2142
lemma Inf_insert:
paulson@33270
  2143
  fixes S :: "real set"
paulson@33270
  2144
  shows "bounded S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))" 
paulson@33270
  2145
by auto (metis Int_absorb Inf_insert_nonempty bounded_has_Inf(1) disjoint_iff_not_equal) 
paulson@33270
  2146
lemma Inf_insert_finite:
paulson@33270
  2147
  fixes S :: "real set"
paulson@33270
  2148
  shows "finite S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))"
paulson@33270
  2149
  by (rule Inf_insert, rule finite_imp_bounded, simp)
paulson@33270
  2150
himmelma@33175
  2151
himmelma@33175
  2152
(* TODO: Move this to RComplete.thy -- would need to include Glb into RComplete *)
himmelma@33175
  2153
lemma real_isGlb_unique: "[| isGlb R S x; isGlb R S y |] ==> x = (y::real)"
himmelma@33175
  2154
  apply (frule isGlb_isLb)
himmelma@33175
  2155
  apply (frule_tac x = y in isGlb_isLb)
himmelma@33175
  2156
  apply (blast intro!: order_antisym dest!: isGlb_le_isLb)
himmelma@33175
  2157
  done
himmelma@33175
  2158
himmelma@33175
  2159
subsection{* Compactness (the definition is the one based on convegent subsequences). *}
himmelma@33175
  2160
himmelma@33175
  2161
definition
himmelma@33175
  2162
  compact :: "'a::metric_space set \<Rightarrow> bool" where (* TODO: generalize *)
himmelma@33175
  2163
  "compact S \<longleftrightarrow>
himmelma@33175
  2164
   (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>
himmelma@33175
  2165
       (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially))"
himmelma@33175
  2166
himmelma@33175
  2167
text {*
himmelma@33175
  2168
  A metric space (or topological vector space) is said to have the
himmelma@33175
  2169
  Heine-Borel property if every closed and bounded subset is compact.
himmelma@33175
  2170
*}
himmelma@33175
  2171
himmelma@33175
  2172
class heine_borel =
himmelma@33175
  2173
  assumes bounded_imp_convergent_subsequence:
himmelma@33175
  2174
    "bounded s \<Longrightarrow> \<forall>n. f n \<in> s
himmelma@33175
  2175
      \<Longrightarrow> \<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2176
himmelma@33175
  2177
lemma bounded_closed_imp_compact:
himmelma@33175
  2178
  fixes s::"'a::heine_borel set"
himmelma@33175
  2179
  assumes "bounded s" and "closed s" shows "compact s"
himmelma@33175
  2180
proof (unfold compact_def, clarify)
himmelma@33175
  2181
  fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
himmelma@33175
  2182
  obtain l r where r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2183
    using bounded_imp_convergent_subsequence [OF `bounded s` `\<forall>n. f n \<in> s`] by auto
himmelma@33175
  2184
  from f have fr: "\<forall>n. (f \<circ> r) n \<in> s" by simp
himmelma@33175
  2185
  have "l \<in> s" using `closed s` fr l
himmelma@33175
  2186
    unfolding closed_sequential_limits by blast
himmelma@33175
  2187
  show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2188
    using `l \<in> s` r l by blast
himmelma@33175
  2189
qed
himmelma@33175
  2190
himmelma@33175
  2191
lemma subseq_bigger: assumes "subseq r" shows "n \<le> r n"
himmelma@33175
  2192
proof(induct n)
himmelma@33175
  2193
  show "0 \<le> r 0" by auto
himmelma@33175
  2194
next
himmelma@33175
  2195
  fix n assume "n \<le> r n"
himmelma@33175
  2196
  moreover have "r n < r (Suc n)"
himmelma@33175
  2197
    using assms [unfolded subseq_def] by auto
himmelma@33175
  2198
  ultimately show "Suc n \<le> r (Suc n)" by auto
himmelma@33175
  2199
qed
himmelma@33175
  2200
himmelma@33175
  2201
lemma eventually_subseq:
himmelma@33175
  2202
  assumes r: "subseq r"
himmelma@33175
  2203
  shows "eventually P sequentially \<Longrightarrow> eventually (\<lambda>n. P (r n)) sequentially"
himmelma@33175
  2204
unfolding eventually_sequentially
himmelma@33175
  2205
by (metis subseq_bigger [OF r] le_trans)
himmelma@33175
  2206
himmelma@33175
  2207
lemma lim_subseq:
himmelma@33175
  2208
  "subseq r \<Longrightarrow> (s ---> l) sequentially \<Longrightarrow> ((s o r) ---> l) sequentially"
himmelma@33175
  2209
unfolding tendsto_def eventually_sequentially o_def
himmelma@33175
  2210
by (metis subseq_bigger le_trans)
himmelma@33175
  2211
himmelma@33175
  2212
lemma num_Axiom: "EX! g. g 0 = e \<and> (\<forall>n. g (Suc n) = f n (g n))"
himmelma@33175
  2213
  unfolding Ex1_def
himmelma@33175
  2214
  apply (rule_tac x="nat_rec e f" in exI)
himmelma@33175
  2215
  apply (rule conjI)+
himmelma@33175
  2216
apply (rule def_nat_rec_0, simp)
himmelma@33175
  2217
apply (rule allI, rule def_nat_rec_Suc, simp)
himmelma@33175
  2218
apply (rule allI, rule impI, rule ext)
himmelma@33175
  2219
apply (erule conjE)
himmelma@33175
  2220
apply (induct_tac x)
himmelma@33175
  2221
apply (simp add: nat_rec_0)
himmelma@33175
  2222
apply (erule_tac x="n" in allE)
himmelma@33175
  2223
apply (simp)
himmelma@33175
  2224
done
himmelma@33175
  2225
himmelma@33175
  2226
lemma convergent_bounded_increasing: fixes s ::"nat\<Rightarrow>real"
himmelma@33175
  2227
  assumes "incseq s" and "\<forall>n. abs(s n) \<le> b"
himmelma@33175
  2228
  shows "\<exists> l. \<forall>e::real>0. \<exists> N. \<forall>n \<ge> N.  abs(s n - l) < e"
himmelma@33175
  2229
proof-
himmelma@33175
  2230
  have "isUb UNIV (range s) b" using assms(2) and abs_le_D1 unfolding isUb_def and setle_def by auto
himmelma@33175
  2231
  then obtain t where t:"isLub UNIV (range s) t" using reals_complete[of "range s" ] by auto
himmelma@33175
  2232
  { fix e::real assume "e>0" and as:"\<forall>N. \<exists>n\<ge>N. \<not> \<bar>s n - t\<bar> < e"
himmelma@33175
  2233
    { fix n::nat
himmelma@33175
  2234
      obtain N where "N\<ge>n" and n:"\<bar>s N - t\<bar> \<ge> e" using as[THEN spec[where x=n]] by auto
himmelma@33175
  2235
      have "t \<ge> s N" using isLub_isUb[OF t, unfolded isUb_def setle_def] by auto
himmelma@33175
  2236
      with n have "s N \<le> t - e" using `e>0` by auto
himmelma@33175
  2237
      hence "s n \<le> t - e" using assms(1)[unfolded incseq_def, THEN spec[where x=n], THEN spec[where x=N]] using `n\<le>N` by auto  }
himmelma@33175
  2238
    hence "isUb UNIV (range s) (t - e)" unfolding isUb_def and setle_def by auto
himmelma@33175
  2239
    hence False using isLub_le_isUb[OF t, of "t - e"] and `e>0` by auto  }
himmelma@33175
  2240
  thus ?thesis by blast
himmelma@33175
  2241
qed
himmelma@33175
  2242
himmelma@33175
  2243
lemma convergent_bounded_monotone: fixes s::"nat \<Rightarrow> real"
himmelma@33175
  2244
  assumes "\<forall>n. abs(s n) \<le> b" and "monoseq s"
himmelma@33175
  2245
  shows "\<exists>l. \<forall>e::real>0. \<exists>N. \<forall>n\<ge>N. abs(s n - l) < e"
himmelma@33175
  2246
  using convergent_bounded_increasing[of s b] assms using convergent_bounded_increasing[of "\<lambda>n. - s n" b]
himmelma@33175
  2247
  unfolding monoseq_def incseq_def
himmelma@33175
  2248
  apply auto unfolding minus_add_distrib[THEN sym, unfolded diff_minus[THEN sym]]
himmelma@33175
  2249
  unfolding abs_minus_cancel by(rule_tac x="-l" in exI)auto
himmelma@33175
  2250
himmelma@33175
  2251
lemma compact_real_lemma:
himmelma@33175
  2252
  assumes "\<forall>n::nat. abs(s n) \<le> b"
himmelma@33175
  2253
  shows "\<exists>(l::real) r. subseq r \<and> ((s \<circ> r) ---> l) sequentially"
himmelma@33175
  2254
proof-
himmelma@33175
  2255
  obtain r where r:"subseq r" "monoseq (\<lambda>n. s (r n))"
himmelma@33175
  2256
    using seq_monosub[of s] by auto
himmelma@33175
  2257
  thus ?thesis using convergent_bounded_monotone[of "\<lambda>n. s (r n)" b] and assms
himmelma@33175
  2258
    unfolding tendsto_iff dist_norm eventually_sequentially by auto
himmelma@33175
  2259
qed
himmelma@33175
  2260
himmelma@33175
  2261
instance real :: heine_borel
himmelma@33175
  2262
proof
himmelma@33175
  2263
  fix s :: "real set" and f :: "nat \<Rightarrow> real"
himmelma@33175
  2264
  assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
himmelma@33175
  2265
  then obtain b where b: "\<forall>n. abs (f n) \<le> b"
himmelma@33175
  2266
    unfolding bounded_iff by auto
himmelma@33175
  2267
  obtain l :: real and r :: "nat \<Rightarrow> nat" where
himmelma@33175
  2268
    r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2269
    using compact_real_lemma [OF b] by auto
himmelma@33175
  2270
  thus "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2271
    by auto
himmelma@33175
  2272
qed
himmelma@33175
  2273
himmelma@33175
  2274
lemma bounded_component: "bounded s \<Longrightarrow> bounded ((\<lambda>x. x $ i) ` s)"
himmelma@33175
  2275
unfolding bounded_def
himmelma@33175
  2276
apply clarify
himmelma@33175
  2277
apply (rule_tac x="x $ i" in exI)
himmelma@33175
  2278
apply (rule_tac x="e" in exI)
himmelma@33175
  2279
apply clarify
himmelma@33175
  2280
apply (rule order_trans [OF dist_nth_le], simp)
himmelma@33175
  2281
done
himmelma@33175
  2282
himmelma@33175
  2283
lemma compact_lemma:
hoelzl@34291
  2284
  fixes f :: "nat \<Rightarrow> 'a::heine_borel ^ 'n"
himmelma@33175
  2285
  assumes "bounded s" and "\<forall>n. f n \<in> s"
himmelma@33175
  2286
  shows "\<forall>d.
himmelma@33175
  2287
        \<exists>l r. subseq r \<and>
himmelma@33175
  2288
        (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) $ i) (l $ i) < e) sequentially)"
himmelma@33175
  2289
proof
himmelma@33175
  2290
  fix d::"'n set" have "finite d" by simp
himmelma@33175
  2291
  thus "\<exists>l::'a ^ 'n. \<exists>r. subseq r \<and>
himmelma@33175
  2292
      (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) $ i) (l $ i) < e) sequentially)"
himmelma@33175
  2293
  proof(induct d) case empty thus ?case unfolding subseq_def by auto
himmelma@33175
  2294
  next case (insert k d)
himmelma@33175
  2295
    have s': "bounded ((\<lambda>x. x $ k) ` s)" using `bounded s` by (rule bounded_component)
himmelma@33175
  2296
    obtain l1::"'a^'n" and r1 where r1:"subseq r1" and lr1:"\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) $ i) (l1 $ i) < e) sequentially"
himmelma@33175
  2297
      using insert(3) by auto
himmelma@33175
  2298
    have f': "\<forall>n. f (r1 n) $ k \<in> (\<lambda>x. x $ k) ` s" using `\<forall>n. f n \<in> s` by simp
himmelma@33175
  2299
    obtain l2 r2 where r2:"subseq r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) $ k) ---> l2) sequentially"
himmelma@33175
  2300
      using bounded_imp_convergent_subsequence[OF s' f'] unfolding o_def by auto
himmelma@33175
  2301
    def r \<equiv> "r1 \<circ> r2" have r:"subseq r"
himmelma@33175
  2302
      using r1 and r2 unfolding r_def o_def subseq_def by auto
himmelma@33175
  2303
    moreover
himmelma@33175
  2304
    def l \<equiv> "(\<chi> i. if i = k then l2 else l1$i)::'a^'n"
himmelma@33175
  2305
    { fix e::real assume "e>0"
himmelma@33175
  2306
      from lr1 `e>0` have N1:"eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) $ i) (l1 $ i) < e) sequentially" by blast
himmelma@33175
  2307
      from lr2 `e>0` have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) $ k) l2 < e) sequentially" by (rule tendstoD)
himmelma@33175
  2308
      from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) $ i) (l1 $ i) < e) sequentially"
himmelma@33175
  2309
        by (rule eventually_subseq)
himmelma@33175
  2310
      have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) $ i) (l $ i) < e) sequentially"
himmelma@33175
  2311
        using N1' N2 by (rule eventually_elim2, simp add: l_def r_def)
himmelma@33175
  2312
    }
himmelma@33175
  2313
    ultimately show ?case by auto
himmelma@33175
  2314
  qed
himmelma@33175
  2315
qed
himmelma@33175
  2316
hoelzl@34291
  2317
instance cart :: (heine_borel, finite) heine_borel
himmelma@33175
  2318
proof
himmelma@33175
  2319
  fix s :: "('a ^ 'b) set" and f :: "nat \<Rightarrow> 'a ^ 'b"
himmelma@33175
  2320
  assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
himmelma@33175
  2321
  then obtain l r where r: "subseq r"
himmelma@33175
  2322
    and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>UNIV. dist (f (r n) $ i) (l $ i) < e) sequentially"
himmelma@33175
  2323
    using compact_lemma [OF s f] by blast
himmelma@33175
  2324
  let ?d = "UNIV::'b set"
himmelma@33175
  2325
  { fix e::real assume "e>0"
himmelma@33175
  2326
    hence "0 < e / (real_of_nat (card ?d))"
himmelma@33175
  2327
      using zero_less_card_finite using divide_pos_pos[of e, of "real_of_nat (card ?d)"] by auto
himmelma@33175
  2328
    with l have "eventually (\<lambda>n. \<forall>i. dist (f (r n) $ i) (l $ i) < e / (real_of_nat (card ?d))) sequentially"
himmelma@33175
  2329
      by simp
himmelma@33175
  2330
    moreover
himmelma@33175
  2331
    { fix n assume n: "\<forall>i. dist (f (r n) $ i) (l $ i) < e / (real_of_nat (card ?d))"
himmelma@33175
  2332
      have "dist (f (r n)) l \<le> (\<Sum>i\<in>?d. dist (f (r n) $ i) (l $ i))"
himmelma@33175
  2333
        unfolding dist_vector_def using zero_le_dist by (rule setL2_le_setsum)
himmelma@33175
  2334
      also have "\<dots> < (\<Sum>i\<in>?d. e / (real_of_nat (card ?d)))"
himmelma@33175
  2335
        by (rule setsum_strict_mono) (simp_all add: n)
himmelma@33175
  2336
      finally have "dist (f (r n)) l < e" by simp
himmelma@33175
  2337
    }
himmelma@33175
  2338
    ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"
himmelma@33175
  2339
      by (rule eventually_elim1)
himmelma@33175
  2340
  }
himmelma@33175
  2341
  hence *:"((f \<circ> r) ---> l) sequentially" unfolding o_def tendsto_iff by simp
himmelma@33175
  2342
  with r show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" by auto
himmelma@33175
  2343
qed
himmelma@33175
  2344
himmelma@33175
  2345
lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst ` s)"
himmelma@33175
  2346
unfolding bounded_def
himmelma@33175
  2347
apply clarify
himmelma@33175
  2348
apply (rule_tac x="a" in exI)
himmelma@33175
  2349
apply (rule_tac x="e" in exI)
himmelma@33175
  2350
apply clarsimp
himmelma@33175
  2351
apply (drule (1) bspec)
himmelma@33175
  2352
apply (simp add: dist_Pair_Pair)
himmelma@33175
  2353
apply (erule order_trans [OF real_sqrt_sum_squares_ge1])
himmelma@33175
  2354
done
himmelma@33175
  2355
himmelma@33175
  2356
lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd ` s)"
himmelma@33175
  2357
unfolding bounded_def
himmelma@33175
  2358
apply clarify
himmelma@33175
  2359
apply (rule_tac x="b" in exI)
himmelma@33175
  2360
apply (rule_tac x="e" in exI)
himmelma@33175
  2361
apply clarsimp
himmelma@33175
  2362
apply (drule (1) bspec)
himmelma@33175
  2363
apply (simp add: dist_Pair_Pair)
himmelma@33175
  2364
apply (erule order_trans [OF real_sqrt_sum_squares_ge2])
himmelma@33175
  2365
done
himmelma@33175
  2366
himmelma@33175
  2367
instance "*" :: (heine_borel, heine_borel) heine_borel
himmelma@33175
  2368
proof
himmelma@33175
  2369
  fix s :: "('a * 'b) set" and f :: "nat \<Rightarrow> 'a * 'b"
himmelma@33175
  2370
  assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
himmelma@33175
  2371
  from s have s1: "bounded (fst ` s)" by (rule bounded_fst)
himmelma@33175
  2372
  from f have f1: "\<forall>n. fst (f n) \<in> fst ` s" by simp
himmelma@33175
  2373
  obtain l1 r1 where r1: "subseq r1"
himmelma@33175
  2374
    and l1: "((\<lambda>n. fst (f (r1 n))) ---> l1) sequentially"
himmelma@33175
  2375
    using bounded_imp_convergent_subsequence [OF s1 f1]
himmelma@33175
  2376
    unfolding o_def by fast
himmelma@33175
  2377
  from s have s2: "bounded (snd ` s)" by (rule bounded_snd)
himmelma@33175
  2378
  from f have f2: "\<forall>n. snd (f (r1 n)) \<in> snd ` s" by simp
himmelma@33175
  2379
  obtain l2 r2 where r2: "subseq r2"
himmelma@33175
  2380
    and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) ---> l2) sequentially"
himmelma@33175
  2381
    using bounded_imp_convergent_subsequence [OF s2 f2]
himmelma@33175
  2382
    unfolding o_def by fast
himmelma@33175
  2383
  have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) ---> l1) sequentially"
himmelma@33175
  2384
    using lim_subseq [OF r2 l1] unfolding o_def .
himmelma@33175
  2385
  have l: "((f \<circ> (r1 \<circ> r2)) ---> (l1, l2)) sequentially"
himmelma@33175
  2386
    using tendsto_Pair [OF l1' l2] unfolding o_def by simp
himmelma@33175
  2387
  have r: "subseq (r1 \<circ> r2)"
himmelma@33175
  2388
    using r1 r2 unfolding subseq_def by simp
himmelma@33175
  2389
  show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2390
    using l r by fast
himmelma@33175
  2391
qed
himmelma@33175
  2392
himmelma@33175
  2393
subsection{* Completeness. *}
himmelma@33175
  2394
himmelma@33175
  2395
lemma cauchy_def:
himmelma@33175
  2396
  "Cauchy s \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m n. m \<ge> N \<and> n \<ge> N --> dist(s m)(s n) < e)"
himmelma@33175
  2397
unfolding Cauchy_def by blast
himmelma@33175
  2398
himmelma@33175
  2399
definition
himmelma@33175
  2400
  complete :: "'a::metric_space set \<Rightarrow> bool" where
himmelma@33175
  2401
  "complete s \<longleftrightarrow> (\<forall>f. (\<forall>n. f n \<in> s) \<and> Cauchy f
himmelma@33175
  2402
                      --> (\<exists>l \<in> s. (f ---> l) sequentially))"
himmelma@33175
  2403
himmelma@33175
  2404
lemma cauchy: "Cauchy s \<longleftrightarrow> (\<forall>e>0.\<exists> N::nat. \<forall>n\<ge>N. dist(s n)(s N) < e)" (is "?lhs = ?rhs")
himmelma@33175
  2405
proof-
himmelma@33175
  2406
  { assume ?rhs
himmelma@33175
  2407
    { fix e::real
himmelma@33175
  2408
      assume "e>0"
himmelma@33175
  2409
      with `?rhs` obtain N where N:"\<forall>n\<ge>N. dist (s n) (s N) < e/2"
himmelma@33175
  2410
        by (erule_tac x="e/2" in allE) auto
himmelma@33175
  2411
      { fix n m
himmelma@33175
  2412
        assume nm:"N \<le> m \<and> N \<le> n"
himmelma@33175
  2413
        hence "dist (s m) (s n) < e" using N
himmelma@33175
  2414
          using dist_triangle_half_l[of "s m" "s N" "e" "s n"]
himmelma@33175
  2415
          by blast
himmelma@33175
  2416
      }
himmelma@33175
  2417
      hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < e"
himmelma@33175
  2418
        by blast
himmelma@33175
  2419
    }
himmelma@33175
  2420
    hence ?lhs
himmelma@33175
  2421
      unfolding cauchy_def
himmelma@33175
  2422
      by blast
himmelma@33175
  2423
  }
himmelma@33175
  2424
  thus ?thesis
himmelma@33175
  2425
    unfolding cauchy_def
himmelma@33175
  2426
    using dist_triangle_half_l
himmelma@33175
  2427
    by blast
himmelma@33175
  2428
qed
himmelma@33175
  2429
himmelma@33175
  2430
lemma convergent_imp_cauchy:
himmelma@33175
  2431
 "(s ---> l) sequentially ==> Cauchy s"
himmelma@33175
  2432
proof(simp only: cauchy_def, rule, rule)
himmelma@33175
  2433
  fix e::real assume "e>0" "(s ---> l) sequentially"
himmelma@33175
  2434
  then obtain N::nat where N:"\<forall>n\<ge>N. dist (s n) l < e/2" unfolding Lim_sequentially by(erule_tac x="e/2" in allE) auto
himmelma@33175
  2435
  thus "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < e"  using dist_triangle_half_l[of _ l e _] by (rule_tac x=N in exI) auto
himmelma@33175
  2436
qed
himmelma@33175
  2437
huffman@34098
  2438
lemma cauchy_imp_bounded: assumes "Cauchy s" shows "bounded (range s)"
himmelma@33175
  2439
proof-
himmelma@33175
  2440
  from assms obtain N::nat where "\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < 1" unfolding cauchy_def apply(erule_tac x= 1 in allE) by auto
himmelma@33175
  2441
  hence N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto
himmelma@33175
  2442
  moreover
himmelma@33175
  2443
  have "bounded (s ` {0..N})" using finite_imp_bounded[of "s ` {1..N}"] by auto
himmelma@33175
  2444
  then obtain a where a:"\<forall>x\<in>s ` {0..N}. dist (s N) x \<le> a"
himmelma@33175
  2445
    unfolding bounded_any_center [where a="s N"] by auto
himmelma@33175
  2446
  ultimately show "?thesis"
himmelma@33175
  2447
    unfolding bounded_any_center [where a="s N"]
himmelma@33175
  2448
    apply(rule_tac x="max a 1" in exI) apply auto
huffman@34098
  2449
    apply(erule_tac x=y in allE) apply(erule_tac x=y in ballE) by auto
himmelma@33175
  2450
qed
himmelma@33175
  2451
himmelma@33175
  2452
lemma compact_imp_complete: assumes "compact s" shows "complete s"
himmelma@33175
  2453
proof-
himmelma@33175
  2454
  { fix f assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"
himmelma@33175
  2455
    from as(1) obtain l r where lr: "l\<in>s" "subseq r" "((f \<circ> r) ---> l) sequentially" using assms unfolding compact_def by blast
himmelma@33175
  2456
himmelma@33175
  2457
    note lr' = subseq_bigger [OF lr(2)]
himmelma@33175
  2458
himmelma@33175
  2459
    { fix e::real assume "e>0"
himmelma@33175
  2460
      from as(2) obtain N where N:"\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (f m) (f n) < e/2" unfolding cauchy_def using `e>0` apply (erule_tac x="e/2" in allE) by auto
himmelma@33175
  2461
      from lr(3)[unfolded Lim_sequentially, THEN spec[where x="e/2"]] obtain M where M:"\<forall>n\<ge>M. dist ((f \<circ> r) n) l < e/2" using `e>0` by auto
himmelma@33175
  2462
      { fix n::nat assume n:"n \<ge> max N M"
himmelma@33175
  2463
        have "dist ((f \<circ> r) n) l < e/2" using n M by auto
himmelma@33175
  2464
        moreover have "r n \<ge> N" using lr'[of n] n by auto
himmelma@33175
  2465
        hence "dist (f n) ((f \<circ> r) n) < e / 2" using N using n by auto
himmelma@33175
  2466
        ultimately have "dist (f n) l < e" using dist_triangle_half_r[of "f (r n)" "f n" e l] by (auto simp add: dist_commute)  }
himmelma@33175
  2467
      hence "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast  }
himmelma@33175
  2468
    hence "\<exists>l\<in>s. (f ---> l) sequentially" using `l\<in>s` unfolding Lim_sequentially by auto  }
himmelma@33175
  2469
  thus ?thesis unfolding complete_def by auto
himmelma@33175
  2470
qed
himmelma@33175
  2471
himmelma@33175
  2472
instance heine_borel < complete_space
himmelma@33175
  2473
proof
himmelma@33175
  2474
  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
huffman@34098
  2475
  hence "bounded (range f)"
huffman@34098
  2476
    by (rule cauchy_imp_bounded)
himmelma@33175
  2477
  hence "compact (closure (range f))"
himmelma@33175
  2478
    using bounded_closed_imp_compact [of "closure (range f)"] by auto
himmelma@33175
  2479
  hence "complete (closure (range f))"
huffman@34098
  2480
    by (rule compact_imp_complete)
himmelma@33175
  2481
  moreover have "\<forall>n. f n \<in> closure (range f)"
himmelma@33175
  2482
    using closure_subset [of "range f"] by auto
himmelma@33175
  2483
  ultimately have "\<exists>l\<in>closure (range f). (f ---> l) sequentially"
himmelma@33175
  2484
    using `Cauchy f` unfolding complete_def by auto
himmelma@33175
  2485
  then show "convergent f"
himmelma@33175
  2486
    unfolding convergent_def LIMSEQ_conv_tendsto [symmetric] by auto
himmelma@33175
  2487
qed
himmelma@33175
  2488
himmelma@33175
  2489
lemma complete_univ: "complete (UNIV :: 'a::complete_space set)"
himmelma@33175
  2490
proof(simp add: complete_def, rule, rule)
himmelma@33175
  2491
  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
himmelma@33175
  2492
  hence "convergent f" by (rule Cauchy_convergent)
himmelma@33175
  2493
  hence "\<exists>l. f ----> l" unfolding convergent_def .  
himmelma@33175
  2494
  thus "\<exists>l. (f ---> l) sequentially" unfolding LIMSEQ_conv_tendsto .
himmelma@33175
  2495
qed
himmelma@33175
  2496
himmelma@33175
  2497
lemma complete_imp_closed: assumes "complete s" shows "closed s"
himmelma@33175
  2498
proof -
himmelma@33175
  2499
  { fix x assume "x islimpt s"
himmelma@33175
  2500
    then obtain f where f: "\<forall>n. f n \<in> s - {x}" "(f ---> x) sequentially"
himmelma@33175
  2501
      unfolding islimpt_sequential by auto
himmelma@33175
  2502
    then obtain l where l: "l\<in>s" "(f ---> l) sequentially"
himmelma@33175
  2503
      using `complete s`[unfolded complete_def] using convergent_imp_cauchy[of f x] by auto
himmelma@33175
  2504
    hence "x \<in> s"  using Lim_unique[of sequentially f l x] trivial_limit_sequentially f(2) by auto
himmelma@33175
  2505
  }
himmelma@33175
  2506
  thus "closed s" unfolding closed_limpt by auto
himmelma@33175
  2507
qed
himmelma@33175
  2508
himmelma@33175
  2509
lemma complete_eq_closed:
himmelma@33175
  2510
  fixes s :: "'a::complete_space set"
himmelma@33175
  2511
  shows "complete s \<longleftrightarrow> closed s" (is "?lhs = ?rhs")
himmelma@33175
  2512
proof
himmelma@33175
  2513
  assume ?lhs thus ?rhs by (rule complete_imp_closed)
himmelma@33175
  2514
next
himmelma@33175
  2515
  assume ?rhs
himmelma@33175
  2516
  { fix f assume as:"\<forall>n::nat. f n \<in> s" "Cauchy f"
himmelma@33175
  2517
    then obtain l where "(f ---> l) sequentially" using complete_univ[unfolded complete_def, THEN spec[where x=f]] by auto
himmelma@33175
  2518
    hence "\<exists>l\<in>s. (f ---> l) sequentially" using `?rhs`[unfolded closed_sequential_limits, THEN spec[where x=f], THEN spec[where x=l]] using as(1) by auto  }
himmelma@33175
  2519
  thus ?lhs unfolding complete_def by auto
himmelma@33175
  2520
qed
himmelma@33175
  2521
himmelma@33175
  2522
lemma convergent_eq_cauchy:
himmelma@33175
  2523
  fixes s :: "nat \<Rightarrow> 'a::complete_space"
himmelma@33175
  2524
  shows "(\<exists>l. (s ---> l) sequentially) \<longleftrightarrow> Cauchy s" (is "?lhs = ?rhs")
himmelma@33175
  2525
proof
himmelma@33175
  2526
  assume ?lhs then obtain l where "(s ---> l) sequentially" by auto
himmelma@33175
  2527
  thus ?rhs using convergent_imp_cauchy by auto
himmelma@33175
  2528
next
himmelma@33175
  2529
  assume ?rhs thus ?lhs using complete_univ[unfolded complete_def, THEN spec[where x=s]] by auto
himmelma@33175
  2530
qed
himmelma@33175
  2531
himmelma@33175
  2532
lemma convergent_imp_bounded:
himmelma@33175
  2533
  fixes s :: "nat \<Rightarrow> 'a::metric_space"
himmelma@33175
  2534
  shows "(s ---> l) sequentially ==> bounded (s ` (UNIV::(nat set)))"
himmelma@33175
  2535
  using convergent_imp_cauchy[of s]
himmelma@33175
  2536
  using cauchy_imp_bounded[of s]
himmelma@33175
  2537
  unfolding image_def
himmelma@33175
  2538
  by auto
himmelma@33175
  2539
himmelma@33175
  2540
subsection{* Total boundedness. *}
himmelma@33175
  2541
himmelma@33175
  2542
fun helper_1::"('a::metric_space set) \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> 'a" where
himmelma@33175
  2543
  "helper_1 s e n = (SOME y::'a. y \<in> s \<and> (\<forall>m<n. \<not> (dist (helper_1 s e m) y < e)))"
himmelma@33175
  2544
declare helper_1.simps[simp del]
himmelma@33175
  2545
himmelma@33175
  2546
lemma compact_imp_totally_bounded:
himmelma@33175
  2547
  assumes "compact s"
himmelma@33175
  2548
  shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` k))"
himmelma@33175
  2549
proof(rule, rule, rule ccontr)
himmelma@33175
  2550
  fix e::real assume "e>0" and assm:"\<not> (\<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k)"
himmelma@33175
  2551
  def x \<equiv> "helper_1 s e"
himmelma@33175
  2552
  { fix n
himmelma@33175
  2553
    have "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)"
himmelma@33175
  2554
    proof(induct_tac rule:nat_less_induct)
himmelma@33175
  2555
      fix n  def Q \<equiv> "(\<lambda>y. y \<in> s \<and> (\<forall>m<n. \<not> dist (x m) y < e))"
himmelma@33175
  2556
      assume as:"\<forall>m<n. x m \<in> s \<and> (\<forall>ma<m. \<not> dist (x ma) (x m) < e)"
himmelma@33175
  2557
      have "\<not> s \<subseteq> (\<Union>x\<in>x ` {0..<n}. ball x e)" using assm apply simp apply(erule_tac x="x ` {0 ..< n}" in allE) using as by auto
himmelma@33175
  2558
      then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x ` {0..<n}. ball x e)" unfolding subset_eq by auto
himmelma@33175
  2559
      have "Q (x n)" unfolding x_def and helper_1.simps[of s e n]
himmelma@33175
  2560
        apply(rule someI2[where a=z]) unfolding x_def[symmetric] and Q_def using z by auto
himmelma@33175
  2561
      thus "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)" unfolding Q_def by auto
himmelma@33175
  2562
    qed }
himmelma@33175
  2563
  hence "\<forall>n::nat. x n \<in> s" and x:"\<forall>n. \<forall>m < n. \<not> (dist (x m) (x n) < e)" by blast+
himmelma@33175
  2564
  then obtain l r where "l\<in>s" and r:"subseq r" and "((x \<circ> r) ---> l) sequentially" using assms(1)[unfolded compact_def, THEN spec[where x=x]] by auto
himmelma@33175
  2565
  from this(3) have "Cauchy (x \<circ> r)" using convergent_imp_cauchy by auto
himmelma@33175
  2566
  then obtain N::nat where N:"\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist ((x \<circ> r) m) ((x \<circ> r) n) < e" unfolding cauchy_def using `e>0` by auto
himmelma@33175
  2567
  show False
himmelma@33175
  2568
    using N[THEN spec[where x=N], THEN spec[where x="N+1"]]
himmelma@33175
  2569
    using r[unfolded subseq_def, THEN spec[where x=N], THEN spec[where x="N+1"]]
himmelma@33175
  2570
    using x[THEN spec[where x="r (N+1)"], THEN spec[where x="r (N)"]] by auto
himmelma@33175
  2571
qed
himmelma@33175
  2572
himmelma@33175
  2573
subsection{* Heine-Borel theorem (following Burkill \& Burkill vol. 2) *}
himmelma@33175
  2574
himmelma@33175
  2575
lemma heine_borel_lemma: fixes s::"'a::metric_space set"
himmelma@33175
  2576
  assumes "compact s"  "s \<subseteq> (\<Union> t)"  "\<forall>b \<in> t. open b"
himmelma@33175
  2577
  shows "\<exists>e>0. \<forall>x \<in> s. \<exists>b \<in> t. ball x e \<subseteq> b"
himmelma@33175
  2578
proof(rule ccontr)
himmelma@33175
  2579
  assume "\<not> (\<exists>e>0. \<forall>x\<in>s. \<exists>b\<in>t. ball x e \<subseteq> b)"
himmelma@33175
  2580
  hence cont:"\<forall>e>0. \<exists>x\<in>s. \<forall>xa\<in>t. \<not> (ball x e \<subseteq> xa)" by auto
himmelma@33175
  2581
  { fix n::nat
himmelma@33175
  2582
    have "1 / real (n + 1) > 0" by auto
himmelma@33175
  2583
    hence "\<exists>x. x\<in>s \<and> (\<forall>xa\<in>t. \<not> (ball x (inverse (real (n+1))) \<subseteq> xa))" using cont unfolding Bex_def by auto }
himmelma@33175
  2584
  hence "\<forall>n::nat. \<exists>x. x \<in> s \<and> (\<forall>xa\<in>t. \<not> ball x (inverse (real (n + 1))) \<subseteq> xa)" by auto
himmelma@33175
  2585
  then obtain f where f:"\<forall>n::nat. f n \<in> s \<and> (\<forall>xa\<in>t. \<not> ball (f n) (inverse (real (n + 1))) \<subseteq> xa)"
himmelma@33175
  2586
    using choice[of "\<lambda>n::nat. \<lambda>x. x\<in>s \<and> (\<forall>xa\<in>t. \<not> ball x (inverse (real (n + 1))) \<subseteq> xa)"] by auto
himmelma@33175
  2587
himmelma@33175
  2588
  then obtain l r where l:"l\<in>s" and r:"subseq r" and lr:"((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2589
    using assms(1)[unfolded compact_def, THEN spec[where x=f]] by auto
himmelma@33175
  2590
himmelma@33175
  2591
  obtain b where "l\<in>b" "b\<in>t" using assms(2) and l by auto
himmelma@33175
  2592
  then obtain e where "e>0" and e:"\<forall>z. dist z l < e \<longrightarrow> z\<in>b"
himmelma@33175
  2593
    using assms(3)[THEN bspec[where x=b]] unfolding open_dist by auto
himmelma@33175
  2594
himmelma@33175
  2595
  then obtain N1 where N1:"\<forall>n\<ge>N1. dist ((f \<circ> r) n) l < e / 2"
himmelma@33175
  2596
    using lr[unfolded Lim_sequentially, THEN spec[where x="e/2"]] by auto
himmelma@33175
  2597
himmelma@33175
  2598
  obtain N2::nat where N2:"N2>0" "inverse (real N2) < e /2" using real_arch_inv[of "e/2"] and `e>0` by auto
himmelma@33175
  2599
  have N2':"inverse (real (r (N1 + N2) +1 )) < e/2"
himmelma@33175
  2600
    apply(rule order_less_trans) apply(rule less_imp_inverse_less) using N2
himmelma@33175
  2601
    using subseq_bigger[OF r, of "N1 + N2"] by auto
himmelma@33175
  2602
himmelma@33175
  2603
  def x \<equiv> "(f (r (N1 + N2)))"
himmelma@33175
  2604
  have x:"\<not> ball x (inverse (real (r (N1 + N2) + 1))) \<subseteq> b" unfolding x_def
himmelma@33175
  2605
    using f[THEN spec[where x="r (N1 + N2)"]] using `b\<in>t` by auto
himmelma@33175
  2606
  have "\<exists>y\<in>ball x (inverse (real (r (N1 + N2) + 1))). y\<notin>b" apply(rule ccontr) using x by auto
himmelma@33175
  2607
  then obtain y where y:"y \<in> ball x (inverse (real (r (N1 + N2) + 1)))" "y \<notin> b" by auto
himmelma@33175
  2608
himmelma@33175
  2609
  have "dist x l < e/2" using N1 unfolding x_def o_def by auto
himmelma@33175
  2610
  hence "dist y l < e" using y N2' using dist_triangle[of y l x]by (auto simp add:dist_commute)
himmelma@33175
  2611
himmelma@33175
  2612
  thus False using e and `y\<notin>b` by auto
himmelma@33175
  2613
qed
himmelma@33175
  2614
himmelma@33175
  2615
lemma compact_imp_heine_borel: "compact s ==> (\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f)
himmelma@33175
  2616
               \<longrightarrow> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f')))"
himmelma@33175
  2617
proof clarify
himmelma@33175
  2618
  fix f assume "compact s" " \<forall>t\<in>f. open t" "s \<subseteq> \<Union>f"
himmelma@33175
  2619
  then obtain e::real where "e>0" and "\<forall>x\<in>s. \<exists>b\<in>f. ball x e \<subseteq> b" using heine_borel_lemma[of s f] by auto
himmelma@33175
  2620
  hence "\<forall>x\<in>s. \<exists>b. b\<in>f \<and> ball x e \<subseteq> b" by auto
himmelma@33175
  2621
  hence "\<exists>bb. \<forall>x\<in>s. bb x \<in>f \<and> ball x e \<subseteq> bb x" using bchoice[of s "\<lambda>x b. b\<in>f \<and> ball x e \<subseteq> b"] by auto
himmelma@33175
  2622
  then obtain  bb where bb:"\<forall>x\<in>s. (bb x) \<in> f \<and> ball x e \<subseteq> (bb x)" by blast
himmelma@33175
  2623
himmelma@33175
  2624
  from `compact s` have  "\<exists> k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k" using compact_imp_totally_bounded[of s] `e>0` by auto
himmelma@33175
  2625
  then obtain k where k:"finite k" "k \<subseteq> s" "s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k" by auto
himmelma@33175
  2626
himmelma@33175
  2627
  have "finite (bb ` k)" using k(1) by auto
himmelma@33175
  2628
  moreover
himmelma@33175
  2629
  { fix x assume "x\<in>s"
himmelma@33175
  2630
    hence "x\<in>\<Union>(\<lambda>x. ball x e) ` k" using k(3)  unfolding subset_eq by auto
himmelma@33175
  2631
    hence "\<exists>X\<in>bb ` k. x \<in> X" using bb k(2) by blast
himmelma@33175
  2632
    hence "x \<in> \<Union>(bb ` k)" using  Union_iff[of x "bb ` k"] by auto
himmelma@33175
  2633
  }
himmelma@33175
  2634
  ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f'" using bb k(2) by (rule_tac x="bb ` k" in exI) auto
himmelma@33175
  2635
qed
himmelma@33175
  2636
himmelma@33175
  2637
subsection{* Bolzano-Weierstrass property. *}
himmelma@33175
  2638
himmelma@33175
  2639
lemma heine_borel_imp_bolzano_weierstrass:
himmelma@33175
  2640
  assumes "\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f) --> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f'))"
himmelma@33175
  2641
          "infinite t"  "t \<subseteq> s"
himmelma@33175
  2642
  shows "\<exists>x \<in> s. x islimpt t"
himmelma@33175
  2643
proof(rule ccontr)
himmelma@33175
  2644
  assume "\<not> (\<exists>x \<in> s. x islimpt t)"
himmelma@33175
  2645
  then obtain f where f:"\<forall>x\<in>s. x \<in> f x \<and> open (f x) \<and> (\<forall>y\<in>t. y \<in> f x \<longrightarrow> y = x)" unfolding islimpt_def
himmelma@33175
  2646
    using bchoice[of s "\<lambda> x T. x \<in> T \<and> open T \<and> (\<forall>y\<in>t. y \<in> T \<longrightarrow> y = x)"] by auto
himmelma@33175
  2647
  obtain g where g:"g\<subseteq>{t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"
himmelma@33175
  2648
    using assms(1)[THEN spec[where x="{t. \<exists>x. x\<in>s \<and> t = f x}"]] using f by auto
himmelma@33175
  2649
  from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa" by auto
himmelma@33175
  2650
  { fix x y assume "x\<in>t" "y\<in>t" "f x = f y"
himmelma@33175
  2651
    hence "x \<in> f x"  "y \<in> f x \<longrightarrow> y = x" using f[THEN bspec[where x=x]] and `t\<subseteq>s` by auto
himmelma@33175
  2652
    hence "x = y" using `f x = f y` and f[THEN bspec[where x=y]] and `y\<in>t` and `t\<subseteq>s` by auto  }
himmelma@33175
  2653
  hence "infinite (f ` t)" using assms(2) using finite_imageD[unfolded inj_on_def, of f t] by auto
himmelma@33175
  2654
  moreover
himmelma@33175
  2655
  { fix x assume "x\<in>t" "f x \<notin> g"
himmelma@33175
  2656
    from g(3) assms(3) `x\<in>t` obtain h where "h\<in>g" and "x\<in>h" by auto
himmelma@33175
  2657
    then obtain y where "y\<in>s" "h = f y" using g'[THEN bspec[where x=h]] by auto
himmelma@33175
  2658
    hence "y = x" using f[THEN bspec[where x=y]] and `x\<in>t` and `x\<in>h`[unfolded `h = f y`] by auto
himmelma@33175
  2659
    hence False using `f x \<notin> g` `h\<in>g` unfolding `h = f y` by auto  }
himmelma@33175
  2660
  hence "f ` t \<subseteq> g" by auto
himmelma@33175
  2661
  ultimately show False using g(2) using finite_subset by auto
himmelma@33175
  2662
qed
himmelma@33175
  2663
himmelma@33175
  2664
subsection{* Complete the chain of compactness variants. *}
himmelma@33175
  2665
himmelma@33175
  2666
primrec helper_2::"(real \<Rightarrow> 'a::metric_space) \<Rightarrow> nat \<Rightarrow> 'a" where
himmelma@33175
  2667
  "helper_2 beyond 0 = beyond 0" |
himmelma@33175
  2668
  "helper_2 beyond (Suc n) = beyond (dist undefined (helper_2 beyond n) + 1 )"
himmelma@33175
  2669
himmelma@33175
  2670
lemma bolzano_weierstrass_imp_bounded: fixes s::"'a::metric_space set"
himmelma@33175
  2671
  assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
himmelma@33175
  2672
  shows "bounded s"
himmelma@33175
  2673
proof(rule ccontr)
himmelma@33175
  2674
  assume "\<not> bounded s"
himmelma@33175
  2675
  then obtain beyond where "\<forall>a. beyond a \<in>s \<and> \<not> dist undefined (beyond a) \<le> a"
himmelma@33175
  2676
    unfolding bounded_any_center [where a=undefined]
himmelma@33175
  2677
    apply simp using choice[of "\<lambda>a x. x\<in>s \<and> \<not> dist undefined x \<le> a"] by auto
himmelma@33175
  2678
  hence beyond:"\<And>a. beyond a \<in>s" "\<And>a. dist undefined (beyond a) > a"
himmelma@33175
  2679
    unfolding linorder_not_le by auto
himmelma@33175
  2680
  def x \<equiv> "helper_2 beyond"
himmelma@33175
  2681
himmelma@33175
  2682
  { fix m n ::nat assume "m<n"
himmelma@33175
  2683
    hence "dist undefined (x m) + 1 < dist undefined (x n)"
himmelma@33175
  2684
    proof(induct n)
himmelma@33175
  2685
      case 0 thus ?case by auto
himmelma@33175
  2686
    next
himmelma@33175
  2687
      case (Suc n)
himmelma@33175
  2688
      have *:"dist undefined (x n) + 1 < dist undefined (x (Suc n))"
himmelma@33175
  2689
        unfolding x_def and helper_2.simps
himmelma@33175
  2690
        using beyond(2)[of "dist undefined (helper_2 beyond n) + 1"] by auto
himmelma@33175
  2691
      thus ?case proof(cases "m < n")
himmelma@33175
  2692
        case True thus ?thesis using Suc and * by auto
himmelma@33175
  2693
      next
himmelma@33175
  2694
        case False hence "m = n" using Suc(2) by auto
himmelma@33175
  2695
        thus ?thesis using * by auto
himmelma@33175
  2696
      qed
himmelma@33175
  2697
    qed  } note * = this
himmelma@33175
  2698
  { fix m n ::nat assume "m\<noteq>n"
himmelma@33175
  2699
    have "1 < dist (x m) (x n)"
himmelma@33175
  2700
    proof(cases "m<n")
himmelma@33175
  2701
      case True
himmelma@33175
  2702
      hence "1 < dist undefined (x n) - dist undefined (x m)" using *[of m n] by auto
himmelma@33175
  2703
      thus ?thesis using dist_triangle [of undefined "x n" "x m"] by arith
himmelma@33175
  2704
    next
himmelma@33175
  2705
      case False hence "n<m" using `m\<noteq>n` by auto
himmelma@33175
  2706
      hence "1 < dist undefined (x m) - dist undefined (x n)" using *[of n m] by auto
himmelma@33175
  2707
      thus ?thesis using dist_triangle2 [of undefined "x m" "x n"] by arith
himmelma@33175
  2708
    qed  } note ** = this
himmelma@33175
  2709
  { fix a b assume "x a = x b" "a \<noteq> b"
himmelma@33175
  2710
    hence False using **[of a b] by auto  }
himmelma@33175
  2711
  hence "inj x" unfolding inj_on_def by auto
himmelma@33175
  2712
  moreover
himmelma@33175
  2713
  { fix n::nat
himmelma@33175
  2714
    have "x n \<in> s"
himmelma@33175
  2715
    proof(cases "n = 0")
himmelma@33175
  2716
      case True thus ?thesis unfolding x_def using beyond by auto
himmelma@33175
  2717
    next
himmelma@33175
  2718
      case False then obtain z where "n = Suc z" using not0_implies_Suc by auto
himmelma@33175
  2719
      thus ?thesis unfolding x_def using beyond by auto
himmelma@33175
  2720
    qed  }
himmelma@33175
  2721
  ultimately have "infinite (range x) \<and> range x \<subseteq> s" unfolding x_def using range_inj_infinite[of "helper_2 beyond"] using beyond(1) by auto
himmelma@33175
  2722
himmelma@33175
  2723
  then obtain l where "l\<in>s" and l:"l islimpt range x" using assms[THEN spec[where x="range x"]] by auto
himmelma@33175
  2724
  then obtain y where "x y \<noteq> l" and y:"dist (x y) l < 1/2" unfolding islimpt_approachable apply(erule_tac x="1/2" in allE) by auto
himmelma@33175
  2725
  then obtain z where "x z \<noteq> l" and z:"dist (x z) l < dist (x y) l" using l[unfolded islimpt_approachable, THEN spec[where x="dist (x y) l"]]
himmelma@33175
  2726
    unfolding dist_nz by auto
himmelma@33175
  2727
  show False using y and z and dist_triangle_half_l[of "x y" l 1 "x z"] and **[of y z] by auto
himmelma@33175
  2728
qed
himmelma@33175
  2729
himmelma@33175
  2730
lemma sequence_infinite_lemma:
himmelma@33175
  2731
  fixes l :: "'a::metric_space" (* TODO: generalize *)
himmelma@33175
  2732
  assumes "\<forall>n::nat. (f n  \<noteq> l)"  "(f ---> l) sequentially"
huffman@34098
  2733
  shows "infinite (range f)"
huffman@34098
  2734
proof
huffman@34098
  2735
  let ?A = "(\<lambda>x. dist x l) ` range f"
huffman@34098
  2736
  assume "finite (range f)"
himmelma@33175
  2737
  hence **:"finite ?A" "?A \<noteq> {}" by auto
himmelma@33175
  2738
  obtain k where k:"dist (f k) l = Min ?A" using Min_in[OF **] by auto
himmelma@33175
  2739
  have "0 < Min ?A" using assms(1) unfolding dist_nz unfolding Min_gr_iff[OF **] by auto
himmelma@33175
  2740
  then obtain N where "dist (f N) l < Min ?A" using assms(2)[unfolded Lim_sequentially, THEN spec[where x="Min ?A"]] by auto
himmelma@33175
  2741
  moreover have "dist (f N) l \<in> ?A" by auto
himmelma@33175
  2742
  ultimately show False using Min_le[OF **(1), of "dist (f N) l"] by auto
himmelma@33175
  2743
qed
himmelma@33175
  2744
himmelma@33175
  2745
lemma sequence_unique_limpt:
himmelma@33175
  2746
  fixes l :: "'a::metric_space" (* TODO: generalize *)
huffman@34098
  2747
  assumes "\<forall>n::nat. (f n \<noteq> l)"  "(f ---> l) sequentially"  "l' islimpt (range f)"
himmelma@33175
  2748
  shows "l' = l"
himmelma@33175
  2749
proof(rule ccontr)
himmelma@33175
  2750
  def e \<equiv> "dist l' l"
himmelma@33175
  2751
  assume "l' \<noteq> l" hence "e>0" unfolding dist_nz e_def by auto
himmelma@33175
  2752
  then obtain N::nat where N:"\<forall>n\<ge>N. dist (f n) l < e / 2"
himmelma@33175
  2753
    using assms(2)[unfolded Lim_sequentially, THEN spec[where x="e/2"]] by auto
himmelma@33175
  2754
  def d \<equiv> "Min (insert (e/2) ((\<lambda>n. if dist (f n) l' = 0 then e/2 else dist (f n) l') ` {0 .. N}))"
himmelma@33175
  2755
  have "d>0" using `e>0` unfolding d_def e_def using zero_le_dist[of _ l', unfolded order_le_less] by auto
himmelma@33175
  2756
  obtain k where k:"f k \<noteq> l'"  "dist (f k) l' < d" using `d>0` and assms(3)[unfolded islimpt_approachable, THEN spec[where x="d"]] by auto
himmelma@33175
  2757
  have "k\<ge>N" using k(1)[unfolded dist_nz] using k(2)[unfolded d_def]
himmelma@33175
  2758
    by force
himmelma@33175
  2759
  hence "dist l' l < e" using N[THEN spec[where x=k]] using k(2)[unfolded d_def] and dist_triangle_half_r[of "f k" l' e l] by auto
himmelma@33175
  2760
  thus False unfolding e_def by auto
himmelma@33175
  2761
qed
himmelma@33175
  2762
himmelma@33175
  2763
lemma bolzano_weierstrass_imp_closed:
himmelma@33175
  2764
  fixes s :: "'a::metric_space set" (* TODO: can this be generalized? *)
himmelma@33175
  2765
  assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
himmelma@33175
  2766
  shows "closed s"
himmelma@33175
  2767
proof-
himmelma@33175
  2768
  { fix x l assume as: "\<forall>n::nat. x n \<in> s" "(x ---> l) sequentially"
himmelma@33175
  2769
    hence "l \<in> s"
himmelma@33175
  2770
    proof(cases "\<forall>n. x n \<noteq> l")
himmelma@33175
  2771
      case False thus "l\<in>s" using as(1) by auto
himmelma@33175
  2772
    next
himmelma@33175
  2773
      case True note cas = this
huffman@34098
  2774
      with as(2) have "infinite (range x)" using sequence_infinite_lemma[of x l] by auto
huffman@34098
  2775
      then obtain l' where "l'\<in>s" "l' islimpt (range x)" using assms[THEN spec[where x="range x"]] as(1) by auto
himmelma@33175
  2776
      thus "l\<in>s" using sequence_unique_limpt[of x l l'] using as cas by auto
himmelma@33175
  2777
    qed  }
himmelma@33175
  2778
  thus ?thesis unfolding closed_sequential_limits by fast
himmelma@33175
  2779
qed
himmelma@33175
  2780
himmelma@33175
  2781
text{* Hence express everything as an equivalence.   *}
himmelma@33175
  2782
himmelma@33175
  2783
lemma compact_eq_heine_borel:
himmelma@33175
  2784
  fixes s :: "'a::heine_borel set"
himmelma@33175
  2785
  shows "compact s \<longleftrightarrow>
himmelma@33175
  2786
           (\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f)
himmelma@33175
  2787
               --> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f')))" (is "?lhs = ?rhs")
himmelma@33175
  2788
proof
himmelma@33175
  2789
  assume ?lhs thus ?rhs using compact_imp_heine_borel[of s] by blast
himmelma@33175
  2790
next
himmelma@33175
  2791
  assume ?rhs
himmelma@33175
  2792
  hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x\<in>s. x islimpt t)"
himmelma@33175
  2793
    by (blast intro: heine_borel_imp_bolzano_weierstrass[of s])
himmelma@33175
  2794
  thus ?lhs using bolzano_weierstrass_imp_bounded[of s] bolzano_weierstrass_imp_closed[of s] bounded_closed_imp_compact[of s] by blast
himmelma@33175
  2795
qed
himmelma@33175
  2796
himmelma@33175
  2797
lemma compact_eq_bolzano_weierstrass:
himmelma@33175
  2798
  fixes s :: "'a::heine_borel set"
himmelma@33175
  2799
  shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))" (is "?lhs = ?rhs")
himmelma@33175
  2800
proof
himmelma@33175
  2801
  assume ?lhs thus ?rhs unfolding compact_eq_heine_borel using heine_borel_imp_bolzano_weierstrass[of s] by auto
himmelma@33175
  2802
next
himmelma@33175
  2803
  assume ?rhs thus ?lhs using bolzano_weierstrass_imp_bounded bolzano_weierstrass_imp_closed bounded_closed_imp_compact by auto
himmelma@33175
  2804
qed
himmelma@33175
  2805
himmelma@33175
  2806
lemma compact_eq_bounded_closed:
himmelma@33175
  2807
  fixes s :: "'a::heine_borel set"
himmelma@33175
  2808
  shows "compact s \<longleftrightarrow> bounded s \<and> closed s"  (is "?lhs = ?rhs")
himmelma@33175
  2809
proof
himmelma@33175
  2810
  assume ?lhs thus ?rhs unfolding compact_eq_bolzano_weierstrass using bolzano_weierstrass_imp_bounded bolzano_weierstrass_imp_closed by auto
himmelma@33175
  2811
next
himmelma@33175
  2812
  assume ?rhs thus ?lhs using bounded_closed_imp_compact by auto
himmelma@33175
  2813
qed
himmelma@33175
  2814
himmelma@33175
  2815
lemma compact_imp_bounded:
himmelma@33175
  2816
  fixes s :: "'a::metric_space set"
himmelma@33175
  2817
  shows "compact s ==> bounded s"
himmelma@33175
  2818
proof -
himmelma@33175
  2819
  assume "compact s"
himmelma@33175
  2820
  hence "\<forall>f. (\<forall>t\<in>f. open t) \<and> s \<subseteq> \<Union>f \<longrightarrow> (\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f')"
himmelma@33175
  2821
    by (rule compact_imp_heine_borel)
himmelma@33175
  2822
  hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"
himmelma@33175
  2823
    using heine_borel_imp_bolzano_weierstrass[of s] by auto
himmelma@33175
  2824
  thus "bounded s"
himmelma@33175
  2825
    by (rule bolzano_weierstrass_imp_bounded)
himmelma@33175
  2826
qed
himmelma@33175
  2827
himmelma@33175
  2828
lemma compact_imp_closed:
himmelma@33175
  2829
  fixes s :: "'a::metric_space set"
himmelma@33175
  2830
  shows "compact s ==> closed s"
himmelma@33175
  2831
proof -
himmelma@33175
  2832
  assume "compact s"
himmelma@33175
  2833
  hence "\<forall>f. (\<forall>t\<in>f. open t) \<and> s \<subseteq> \<Union>f \<longrightarrow> (\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f')"
himmelma@33175
  2834
    by (rule compact_imp_heine_borel)
himmelma@33175
  2835
  hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"
himmelma@33175
  2836
    using heine_borel_imp_bolzano_weierstrass[of s] by auto
himmelma@33175
  2837
  thus "closed s"
himmelma@33175
  2838
    by (rule bolzano_weierstrass_imp_closed)
himmelma@33175
  2839
qed
himmelma@33175
  2840
himmelma@33175
  2841
text{* In particular, some common special cases. *}
himmelma@33175
  2842
himmelma@33175
  2843
lemma compact_empty[simp]:
himmelma@33175
  2844
 "compact {}"
himmelma@33175
  2845
  unfolding compact_def
himmelma@33175
  2846
  by simp
himmelma@33175
  2847
himmelma@33175
  2848
(* TODO: can any of the next 3 lemmas be generalized to metric spaces? *)
himmelma@33175
  2849
himmelma@33175
  2850
  (* FIXME : Rename *)
himmelma@33175
  2851
lemma compact_union[intro]:
himmelma@33175
  2852
  fixes s t :: "'a::heine_borel set"
himmelma@33175
  2853
  shows "compact s \<Longrightarrow> compact t ==> compact (s \<union> t)"
himmelma@33175
  2854
  unfolding compact_eq_bounded_closed
himmelma@33175
  2855
  using bounded_Un[of s t]
himmelma@33175
  2856
  using closed_Un[of s t]
himmelma@33175
  2857
  by simp
himmelma@33175
  2858
himmelma@33175
  2859
lemma compact_inter[intro]:
himmelma@33175
  2860
  fixes s t :: "'a::heine_borel set"
himmelma@33175
  2861
  shows "compact s \<Longrightarrow> compact t ==> compact (s \<inter> t)"
himmelma@33175
  2862
  unfolding compact_eq_bounded_closed
himmelma@33175
  2863
  using bounded_Int[of s t]
himmelma@33175
  2864
  using closed_Int[of s t]
himmelma@33175
  2865
  by simp
himmelma@33175
  2866
himmelma@33175
  2867
lemma compact_inter_closed[intro]:
himmelma@33175
  2868
  fixes s t :: "'a::heine_borel set"
himmelma@33175
  2869
  shows "compact s \<Longrightarrow> closed t ==> compact (s \<inter> t)"
himmelma@33175
  2870
  unfolding compact_eq_bounded_closed
himmelma@33175
  2871
  using closed_Int[of s t]
himmelma@33175
  2872
  using bounded_subset[of "s \<inter> t" s]
himmelma@33175
  2873
  by blast
himmelma@33175
  2874
himmelma@33175
  2875
lemma closed_inter_compact[intro]:
himmelma@33175
  2876
  fixes s t :: "'a::heine_borel set"
himmelma@33175
  2877
  shows "closed s \<Longrightarrow> compact t ==> compact (s \<inter> t)"
himmelma@33175
  2878
proof-
himmelma@33175
  2879
  assume "closed s" "compact t"
himmelma@33175
  2880
  moreover
himmelma@33175
  2881
  have "s \<inter> t = t \<inter> s" by auto ultimately
himmelma@33175
  2882
  show ?thesis
himmelma@33175
  2883
    using compact_inter_closed[of t s]
himmelma@33175
  2884
    by auto
himmelma@33175
  2885
qed
himmelma@33175
  2886
himmelma@33175
  2887
lemma closed_sing [simp]:
himmelma@33175
  2888
  fixes a :: "'a::metric_space"
himmelma@33175
  2889
  shows "closed {a}"
himmelma@33175
  2890
  apply (clarsimp simp add: closed_def open_dist)
himmelma@33175
  2891
  apply (rule ccontr)
himmelma@33175
  2892
  apply (drule_tac x="dist x a" in spec)
himmelma@33175
  2893
  apply (simp add: dist_nz dist_commute)
himmelma@33175
  2894
  done
himmelma@33175
  2895
himmelma@33175
  2896
lemma finite_imp_closed:
himmelma@33175
  2897
  fixes s :: "'a::metric_space set"
himmelma@33175
  2898
  shows "finite s ==> closed s"
himmelma@33175
  2899
proof (induct set: finite)
himmelma@33175
  2900
  case empty show "closed {}" by simp
himmelma@33175
  2901
next
himmelma@33175
  2902
  case (insert x F)
himmelma@33175
  2903
  hence "closed ({x} \<union> F)" by (simp only: closed_Un closed_sing)
himmelma@33175
  2904
  thus "closed (insert x F)" by simp
himmelma@33175
  2905
qed
himmelma@33175
  2906
himmelma@33175
  2907
lemma finite_imp_compact:
himmelma@33175
  2908
  fixes s :: "'a::heine_borel set"
himmelma@33175
  2909
  shows "finite s ==> compact s"
himmelma@33175
  2910
  unfolding compact_eq_bounded_closed
himmelma@33175
  2911
  using finite_imp_closed finite_imp_bounded
himmelma@33175
  2912
  by blast
himmelma@33175
  2913
himmelma@33175
  2914
lemma compact_sing [simp]: "compact {a}"
himmelma@33175
  2915
  unfolding compact_def o_def subseq_def
himmelma@33175
  2916
  by (auto simp add: tendsto_const)
himmelma@33175
  2917
himmelma@33175
  2918
lemma compact_cball[simp]:
himmelma@33175
  2919
  fixes x :: "'a::heine_borel"
himmelma@33175
  2920
  shows "compact(cball x e)"
himmelma@33175
  2921
  using compact_eq_bounded_closed bounded_cball closed_cball
himmelma@33175
  2922
  by blast
himmelma@33175
  2923
himmelma@33175
  2924
lemma compact_frontier_bounded[intro]:
himmelma@33175
  2925
  fixes s :: "'a::heine_borel set"
himmelma@33175
  2926
  shows "bounded s ==> compact(frontier s)"
himmelma@33175
  2927
  unfolding frontier_def
himmelma@33175
  2928
  using compact_eq_bounded_closed
himmelma@33175
  2929
  by blast
himmelma@33175
  2930
himmelma@33175
  2931
lemma compact_frontier[intro]:
himmelma@33175
  2932
  fixes s :: "'a::heine_borel set"
himmelma@33175
  2933
  shows "compact s ==> compact (frontier s)"
himmelma@33175
  2934
  using compact_eq_bounded_closed compact_frontier_bounded
himmelma@33175
  2935
  by blast
himmelma@33175
  2936
himmelma@33175
  2937
lemma frontier_subset_compact:
himmelma@33175
  2938
  fixes s :: "'a::heine_borel set"
himmelma@33175
  2939
  shows "compact s ==> frontier s \<subseteq> s"
himmelma@33175
  2940
  using frontier_subset_closed compact_eq_bounded_closed
himmelma@33175
  2941
  by blast
himmelma@33175
  2942
himmelma@33175
  2943
lemma open_delete:
himmelma@33175
  2944
  fixes s :: "'a::metric_space set"
himmelma@33175
  2945
  shows "open s ==> open(s - {x})"
himmelma@33175
  2946
  using open_Diff[of s "{x}"] closed_sing
himmelma@33175
  2947
  by blast
himmelma@33175
  2948
himmelma@33175
  2949
text{* Finite intersection property. I could make it an equivalence in fact. *}
himmelma@33175
  2950
himmelma@33175
  2951
lemma compact_imp_fip:
himmelma@33175
  2952
  fixes s :: "'a::heine_borel set"
himmelma@33175
  2953
  assumes "compact s"  "\<forall>t \<in> f. closed t"
himmelma@33175
  2954
        "\<forall>f'. finite f' \<and> f' \<subseteq> f --> (s \<inter> (\<Inter> f') \<noteq> {})"
himmelma@33175
  2955
  shows "s \<inter> (\<Inter> f) \<noteq> {}"
himmelma@33175
  2956
proof
himmelma@33175
  2957
  assume as:"s \<inter> (\<Inter> f) = {}"
huffman@34099
  2958
  hence "s \<subseteq> \<Union> uminus ` f" by auto
huffman@34099
  2959
  moreover have "Ball (uminus ` f) open" using open_Diff closed_Diff using assms(2) by auto
huffman@34099
  2960
  ultimately obtain f' where f':"f' \<subseteq> uminus ` f"  "finite f'"  "s \<subseteq> \<Union>f'" using assms(1)[unfolded compact_eq_heine_borel, THEN spec[where x="(\<lambda>t. - t) ` f"]] by auto
huffman@34099
  2961
  hence "finite (uminus ` f') \<and> uminus ` f' \<subseteq> f" by(auto simp add: Diff_Diff_Int)
huffman@34099
  2962
  hence "s \<inter> \<Inter>uminus ` f' \<noteq> {}" using assms(3)[THEN spec[where x="uminus ` f'"]] by auto
himmelma@33175
  2963
  thus False using f'(3) unfolding subset_eq and Union_iff by blast
himmelma@33175
  2964
qed
himmelma@33175
  2965
himmelma@33175
  2966
subsection{* Bounded closed nest property (proof does not use Heine-Borel).            *}
himmelma@33175
  2967
himmelma@33175
  2968
lemma bounded_closed_nest:
himmelma@33175
  2969
  assumes "\<forall>n. closed(s n)" "\<forall>n. (s n \<noteq> {})"
himmelma@33175
  2970
  "(\<forall>m n. m \<le> n --> s n \<subseteq> s m)"  "bounded(s 0)"
himmelma@33175
  2971
  shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s(n)"
himmelma@33175
  2972
proof-
himmelma@33175
  2973
  from assms(2) obtain x where x:"\<forall>n::nat. x n \<in> s n" using choice[of "\<lambda>n x. x\<in> s n"] by auto
himmelma@33175
  2974
  from assms(4,1) have *:"compact (s 0)" using bounded_closed_imp_compact[of "s 0"] by auto
himmelma@33175
  2975
himmelma@33175
  2976
  then obtain l r where lr:"l\<in>s 0" "subseq r" "((x \<circ> r) ---> l) sequentially"
himmelma@33175
  2977
    unfolding compact_def apply(erule_tac x=x in allE)  using x using assms(3) by blast
himmelma@33175
  2978
himmelma@33175
  2979
  { fix n::nat
himmelma@33175
  2980
    { fix e::real assume "e>0"
himmelma@33175
  2981
      with lr(3) obtain N where N:"\<forall>m\<ge>N. dist ((x \<circ> r) m) l < e" unfolding Lim_sequentially by auto
himmelma@33175
  2982
      hence "dist ((x \<circ> r) (max N n)) l < e" by auto
himmelma@33175
  2983
      moreover
himmelma@33175
  2984
      have "r (max N n) \<ge> n" using lr(2) using subseq_bigger[of r "max N n"] by auto
himmelma@33175
  2985
      hence "(x \<circ> r) (max N n) \<in> s n"
himmelma@33175
  2986
        using x apply(erule_tac x=n in allE)
himmelma@33175
  2987
        using x apply(erule_tac x="r (max N n)" in allE)
himmelma@33175
  2988
        using assms(3) apply(erule_tac x=n in allE)apply( erule_tac x="r (max N n)" in allE) by auto
himmelma@33175
  2989
      ultimately have "\<exists>y\<in>s n. dist y l < e" by auto
himmelma@33175
  2990
    }
himmelma@33175
  2991
    hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by blast
himmelma@33175
  2992
  }
himmelma@33175
  2993
  thus ?thesis by auto
himmelma@33175
  2994
qed
himmelma@33175
  2995
himmelma@33175
  2996
text{* Decreasing case does not even need compactness, just completeness.        *}
himmelma@33175
  2997
himmelma@33175
  2998
lemma decreasing_closed_nest:
himmelma@33175
  2999
  assumes "\<forall>n. closed(s n)"
himmelma@33175
  3000
          "\<forall>n. (s n \<noteq> {})"
himmelma@33175
  3001
          "\<forall>m n. m \<le> n --> s n \<subseteq> s m"
himmelma@33175
  3002
          "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y \<in> (s n). dist x y < e"
himmelma@33175
  3003
  shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s n"
himmelma@33175
  3004
proof-
himmelma@33175
  3005
  have "\<forall>n. \<exists> x. x\<in>s n" using assms(2) by auto
himmelma@33175
  3006
  hence "\<exists>t. \<forall>n. t n \<in> s n" using choice[of "\<lambda> n x. x \<in> s n"] by auto
himmelma@33175
  3007
  then obtain t where t: "\<forall>n. t n \<in> s n" by auto
himmelma@33175
  3008
  { fix e::real assume "e>0"
himmelma@33175
  3009
    then obtain N where N:"\<forall>x\<in>s N. \<forall>y\<in>s N. dist x y < e" using assms(4) by auto
himmelma@33175
  3010
    { fix m n ::nat assume "N \<le> m \<and> N \<le> n"
himmelma@33175
  3011
      hence "t m \<in> s N" "t n \<in> s N" using assms(3) t unfolding  subset_eq t by blast+
himmelma@33175
  3012
      hence "dist (t m) (t n) < e" using N by auto
himmelma@33175
  3013
    }
himmelma@33175
  3014
    hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (t m) (t n) < e" by auto
himmelma@33175
  3015
  }
himmelma@33175
  3016
  hence  "Cauchy t" unfolding cauchy_def by auto
himmelma@33175
  3017
  then obtain l where l:"(t ---> l) sequentially" using complete_univ unfolding complete_def by auto
himmelma@33175
  3018
  { fix n::nat
himmelma@33175
  3019
    { fix e::real assume "e>0"
himmelma@33175
  3020
      then obtain N::nat where N:"\<forall>n\<ge>N. dist (t n) l < e" using l[unfolded Lim_sequentially] by auto
himmelma@33175
  3021
      have "t (max n N) \<in> s n" using assms(3) unfolding subset_eq apply(erule_tac x=n in allE) apply (erule_tac x="max n N" in allE) using t by auto
himmelma@33175
  3022
      hence "\<exists>y\<in>s n. dist y l < e" apply(rule_tac x="t (max n N)" in bexI) using N by auto
himmelma@33175
  3023
    }
himmelma@33175
  3024
    hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by auto
himmelma@33175
  3025
  }
himmelma@33175
  3026
  then show ?thesis by auto
himmelma@33175
  3027
qed
himmelma@33175
  3028
himmelma@33175
  3029
text{* Strengthen it to the intersection actually being a singleton.             *}
himmelma@33175
  3030
himmelma@33175
  3031
lemma decreasing_closed_nest_sing:
huffman@34098
  3032
  fixes s :: "nat \<Rightarrow> 'a::heine_borel set"
himmelma@33175
  3033
  assumes "\<forall>n. closed(s n)"
himmelma@33175
  3034
          "\<forall>n. s n \<noteq> {}"
himmelma@33175
  3035
          "\<forall>m n. m \<le> n --> s n \<subseteq> s m"
himmelma@33175
  3036
          "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y\<in>(s n). dist x y < e"
huffman@34098
  3037
  shows "\<exists>a. \<Inter>(range s) = {a}"
himmelma@33175
  3038
proof-
himmelma@33175
  3039
  obtain a where a:"\<forall>n. a \<in> s n" using decreasing_closed_nest[of s] using assms by auto
huffman@34098
  3040
  { fix b assume b:"b \<in> \<Inter>(range s)"
himmelma@33175
  3041
    { fix e::real assume "e>0"
himmelma@33175
  3042
      hence "dist a b < e" using assms(4 )using b using a by blast
himmelma@33175
  3043
    }
himmelma@33175
  3044
    hence "dist a b = 0" by (metis dist_eq_0_iff dist_nz real_less_def)
himmelma@33175
  3045
  }
huffman@34098
  3046
  with a have "\<Inter>(range s) = {a}" unfolding image_def by auto
huffman@34098
  3047
  thus ?thesis ..
himmelma@33175
  3048
qed
himmelma@33175
  3049
himmelma@33175
  3050
text{* Cauchy-type criteria for uniform convergence. *}
himmelma@33175
  3051
himmelma@33175
  3052
lemma uniformly_convergent_eq_cauchy: fixes s::"nat \<Rightarrow> 'b \<Rightarrow> 'a::heine_borel" shows
himmelma@33175
  3053
 "(\<exists>l. \<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e) \<longleftrightarrow>
himmelma@33175
  3054
  (\<forall>e>0. \<exists>N. \<forall>m n x. N \<le> m \<and> N \<le> n \<and> P x  --> dist (s m x) (s n x) < e)" (is "?lhs = ?rhs")
himmelma@33175
  3055
proof(rule)
himmelma@33175
  3056
  assume ?lhs
himmelma@33175
  3057
  then obtain l where l:"\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist (s n x) (l x) < e" by auto
himmelma@33175
  3058
  { fix e::real assume "e>0"
himmelma@33175
  3059
    then obtain N::nat where N:"\<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist (s n x) (l x) < e / 2" using l[THEN spec[where x="e/2"]] by auto
himmelma@33175
  3060
    { fix n m::nat and x::"'b" assume "N \<le> m \<and> N \<le> n \<and> P x"
himmelma@33175
  3061
      hence "dist (s m x) (s n x) < e"
himmelma@33175
  3062
        using N[THEN spec[where x=m], THEN spec[where x=x]]
himmelma@33175
  3063
        using N[THEN spec[where x=n], THEN spec[where x=x]]
himmelma@33175
  3064
        using dist_triangle_half_l[of "s m x" "l x" e "s n x"] by auto  }
himmelma@33175
  3065
    hence "\<exists>N. \<forall>m n x. N \<le> m \<and> N \<le> n \<and> P x  --> dist (s m x) (s n x) < e"  by auto  }
himmelma@33175
  3066
  thus ?rhs by auto
himmelma@33175
  3067
next
himmelma@33175
  3068
  assume ?rhs
himmelma@33175
  3069
  hence "\<forall>x. P x \<longrightarrow> Cauchy (\<lambda>n. s n x)" unfolding cauchy_def apply auto by (erule_tac x=e in allE)auto
himmelma@33175
  3070
  then obtain l where l:"\<forall>x. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l x) sequentially" unfolding convergent_eq_cauchy[THEN sym]
himmelma@33175
  3071
    using choice[of "\<lambda>x l. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l) sequentially"] by auto
himmelma@33175
  3072
  { fix e::real assume "e>0"
himmelma@33175
  3073
    then obtain N where N:"\<forall>m n x. N \<le> m \<and> N \<le> n \<and> P x \<longrightarrow> dist (s m x) (s n x) < e/2"
himmelma@33175
  3074
      using `?rhs`[THEN spec[where x="e/2"]] by auto
himmelma@33175
  3075
    { fix x assume "P x"
himmelma@33175
  3076
      then obtain M where M:"\<forall>n\<ge>M. dist (s n x) (l x) < e/2"
himmelma@33175
  3077
        using l[THEN spec[where x=x], unfolded Lim_sequentially] using `e>0` by(auto elim!: allE[where x="e/2"])
himmelma@33175
  3078
      fix n::nat assume "n\<ge>N"
himmelma@33175
  3079
      hence "dist(s n x)(l x) < e"  using `P x`and N[THEN spec[where x=n], THEN spec[where x="N+M"], THEN spec[where x=x]]
himmelma@33175
  3080
        using M[THEN spec[where x="N+M"]] and dist_triangle_half_l[of "s n x" "s (N+M) x" e "l x"] by (auto simp add: dist_commute)  }
himmelma@33175
  3081
    hence "\<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist(s n x)(l x) < e" by auto }
himmelma@33175
  3082
  thus ?lhs by auto
himmelma@33175
  3083
qed
himmelma@33175
  3084
himmelma@33175
  3085
lemma uniformly_cauchy_imp_uniformly_convergent:
himmelma@33175
  3086
  fixes s :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::heine_borel"
himmelma@33175
  3087
  assumes "\<forall>e>0.\<exists>N. \<forall>m (n::nat) x. N \<le> m \<and> N \<le> n \<and> P x --> dist(s m x)(s n x) < e"
himmelma@33175
  3088
          "\<forall>x. P x --> (\<forall>e>0. \<exists>N. \<forall>n. N \<le> n --> dist(s n x)(l x) < e)"
himmelma@33175
  3089
  shows "\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e"
himmelma@33175
  3090
proof-
himmelma@33175
  3091
  obtain l' where l:"\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist (s n x) (l' x) < e"
himmelma@33175
  3092
    using assms(1) unfolding uniformly_convergent_eq_cauchy[THEN sym] by auto
himmelma@33175
  3093
  moreover
himmelma@33175
  3094
  { fix x assume "P x"
himmelma@33175
  3095
    hence "l x = l' x" using Lim_unique[OF trivial_limit_sequentially, of "\<lambda>n. s n x" "l x" "l' x"]
himmelma@33175
  3096
      using l and assms(2) unfolding Lim_sequentially by blast  }
himmelma@33175
  3097
  ultimately show ?thesis by auto
himmelma@33175
  3098
qed
himmelma@33175
  3099
himmelma@33175
  3100
subsection{* Define continuity over a net to take in restrictions of the set. *}
himmelma@33175
  3101
himmelma@33175
  3102
definition
himmelma@33175
  3103
  continuous :: "'a::t2_space net \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool" where
himmelma@33175
  3104
  "continuous net f \<longleftrightarrow> (f ---> f(netlimit net)) net"
himmelma@33175
  3105
himmelma@33175
  3106
lemma continuous_trivial_limit:
himmelma@33175
  3107
 "trivial_limit net ==> continuous net f"
himmelma@33175
  3108
  unfolding continuous_def tendsto_def trivial_limit_eq by auto
himmelma@33175
  3109
himmelma@33175
  3110
lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f ---> f(x)) (at x within s)"
himmelma@33175
  3111
  unfolding continuous_def
himmelma@33175
  3112
  unfolding tendsto_def
himmelma@33175
  3113
  using netlimit_within[of x s]
himmelma@33175
  3114
  by (cases "trivial_limit (at x within s)") (auto simp add: trivial_limit_eventually)
himmelma@33175
  3115
himmelma@33175
  3116
lemma continuous_at: "continuous (at x) f \<longleftrightarrow> (f ---> f(x)) (at x)"
himmelma@33175
  3117
  using continuous_within [of x UNIV f] by (simp add: within_UNIV)
himmelma@33175
  3118
himmelma@33175
  3119
lemma continuous_at_within:
himmelma@33175
  3120
  assumes "continuous (at x) f"  shows "continuous (at x within s) f"
himmelma@33175
  3121
  using assms unfolding continuous_at continuous_within
himmelma@33175
  3122
  by (rule Lim_at_within)
himmelma@33175
  3123
himmelma@33175
  3124
text{* Derive the epsilon-delta forms, which we often use as "definitions" *}
himmelma@33175
  3125
himmelma@33175
  3126
lemma continuous_within_eps_delta:
himmelma@33175
  3127
  "continuous (at x within s) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. \<forall>x'\<in> s.  dist x' x < d --> dist (f x') (f x) < e)"
himmelma@33175
  3128
  unfolding continuous_within and Lim_within
himmelma@33175
  3129
  apply auto unfolding dist_nz[THEN sym] apply(auto elim!:allE) apply(rule_tac x=d in exI) by auto
himmelma@33175
  3130
himmelma@33175
  3131
lemma continuous_at_eps_delta: "continuous (at x) f \<longleftrightarrow>  (\<forall>e>0. \<exists>d>0.
himmelma@33175
  3132
                           \<forall>x'. dist x' x < d --> dist(f x')(f x) < e)"
himmelma@33175
  3133
  using continuous_within_eps_delta[of x UNIV f]
himmelma@33175
  3134
  unfolding within_UNIV by blast
himmelma@33175
  3135
himmelma@33175
  3136
text{* Versions in terms of open balls. *}
himmelma@33175
  3137
himmelma@33175
  3138
lemma continuous_within_ball:
himmelma@33175
  3139
 "continuous (at x within s) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.
himmelma@33175
  3140
                            f ` (ball x d \<inter> s) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
himmelma@33175
  3141
proof
himmelma@33175
  3142
  assume ?lhs
himmelma@33175
  3143
  { fix e::real assume "e>0"
himmelma@33175
  3144
    then obtain d where d: "d>0" "\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e"
himmelma@33175
  3145
      using `?lhs`[unfolded continuous_within Lim_within] by auto
himmelma@33175
  3146
    { fix y assume "y\<in>f ` (ball x d \<inter> s)"
himmelma@33175
  3147
      hence "y \<in> ball (f x) e" using d(2) unfolding dist_nz[THEN sym]
himmelma@33175
  3148
        apply (auto simp add: dist_commute mem_ball) apply(erule_tac x=xa in ballE) apply auto using `e>0` by auto
himmelma@33175
  3149
    }
himmelma@33175
  3150
    hence "\<exists>d>0. f ` (ball x d \<inter> s) \<subseteq> ball (f x) e" using `d>0` unfolding subset_eq ball_def by (auto simp add: dist_commute)  }
himmelma@33175
  3151
  thus ?rhs by auto
himmelma@33175
  3152
next
himmelma@33175
  3153
  assume ?rhs thus ?lhs unfolding continuous_within Lim_within ball_def subset_eq
himmelma@33175
  3154
    apply (auto simp add: dist_commute) apply(erule_tac x=e in allE) by auto
himmelma@33175
  3155
qed
himmelma@33175
  3156
himmelma@33175
  3157
lemma continuous_at_ball:
himmelma@33175
  3158
  "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f ` (ball x d) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
himmelma@33175
  3159
proof
himmelma@33175
  3160
  assume ?lhs thus ?rhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
himmelma@33175
  3161
    apply auto apply(erule_tac x=e in allE) apply auto apply(rule_tac x=d in exI) apply auto apply(erule_tac x=xa in allE) apply (auto simp add: dist_commute dist_nz)
himmelma@33175
  3162
    unfolding dist_nz[THEN sym] by auto
himmelma@33175
  3163
next
himmelma@33175
  3164
  assume ?rhs thus ?lhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
himmelma@33175
  3165
    apply auto apply(erule_tac x=e in allE) apply auto apply(rule_tac x=d in exI) apply auto apply(erule_tac x="f xa" in allE) by (auto simp add: dist_commute dist_nz)
himmelma@33175
  3166
qed
himmelma@33175
  3167
himmelma@33175
  3168
text{* For setwise continuity, just start from the epsilon-delta definitions. *}
himmelma@33175
  3169
himmelma@33175
  3170
definition
himmelma@33175
  3171
  continuous_on :: "'a::metric_space set \<Rightarrow> ('a \<Rightarrow> 'b::metric_space) \<Rightarrow> bool" where
himmelma@33175
  3172
  "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. \<forall>e>0. \<exists>d::real>0. \<forall>x' \<in> s. dist x' x < d --> dist (f x') (f x) < e)"
himmelma@33175
  3173
himmelma@33175
  3174
himmelma@33175
  3175
definition
himmelma@33175
  3176
  uniformly_continuous_on ::
himmelma@33175
  3177
    "'a::metric_space set \<Rightarrow> ('a \<Rightarrow> 'b::metric_space) \<Rightarrow> bool" where
himmelma@33175
  3178
  "uniformly_continuous_on s f \<longleftrightarrow>
himmelma@33175
  3179
        (\<forall>e>0. \<exists>d>0. \<forall>x\<in>s. \<forall> x'\<in>s. dist x' x < d
himmelma@33175
  3180
                           --> dist (f x') (f x) < e)"
himmelma@33175
  3181
himmelma@35172
  3182
himmelma@35172
  3183
text{* Lifting and dropping *}
himmelma@35172
  3184
himmelma@35172
  3185
lemma continuous_on_o_dest_vec1: fixes f::"real \<Rightarrow> 'a::real_normed_vector"
himmelma@35172
  3186
  assumes "continuous_on {a..b::real} f" shows "continuous_on {vec1 a..vec1 b} (f o dest_vec1)"
himmelma@35172
  3187
  using assms unfolding continuous_on_def apply safe
himmelma@35172
  3188
  apply(erule_tac x="x$1" in ballE,erule_tac x=e in allE) apply safe
himmelma@35172
  3189
  apply(rule_tac x=d in exI) apply safe unfolding o_def dist_real_def dist_real 
himmelma@35172
  3190
  apply(erule_tac x="dest_vec1 x'" in ballE) by(auto simp add:vector_le_def)
himmelma@35172
  3191
himmelma@35172
  3192
lemma continuous_on_o_vec1: fixes f::"real^1 \<Rightarrow> 'a::real_normed_vector"
himmelma@35172
  3193
  assumes "continuous_on {a..b} f" shows "continuous_on {dest_vec1 a..dest_vec1 b} (f o vec1)"
himmelma@35172
  3194
  using assms unfolding continuous_on_def apply safe
himmelma@35172
  3195
  apply(erule_tac x="vec x" in ballE,erule_tac x=e in allE) apply safe
himmelma@35172
  3196
  apply(rule_tac x=d in exI) apply safe unfolding o_def dist_real_def dist_real 
himmelma@35172
  3197
  apply(erule_tac x="vec1 x'" in ballE) by(auto simp add:vector_le_def)
himmelma@35172
  3198
himmelma@33175
  3199
text{* Some simple consequential lemmas. *}
himmelma@33175
  3200
himmelma@33175
  3201
lemma uniformly_continuous_imp_continuous:
himmelma@33175
  3202
 " uniformly_continuous_on s f ==> continuous_on s f"
himmelma@33175
  3203
  unfolding uniformly_continuous_on_def continuous_on_def by blast
himmelma@33175
  3204
himmelma@33175
  3205
lemma continuous_at_imp_continuous_within:
himmelma@33175
  3206
 "continuous (at x) f ==> continuous (at x within s) f"
himmelma@33175
  3207
  unfolding continuous_within continuous_at using Lim_at_within by auto
himmelma@33175
  3208
himmelma@33175
  3209
lemma continuous_at_imp_continuous_on: assumes "(\<forall>x \<in> s. continuous (at x) f)"
himmelma@33175
  3210
  shows "continuous_on s f"
himmelma@33175
  3211
proof(simp add: continuous_at continuous_on_def, rule, rule, rule)
himmelma@33175
  3212
  fix x and e::real assume "x\<in>s" "e>0"
himmelma@33175
  3213
  hence "eventually (\<lambda>xa. dist (f xa) (f x) < e) (at x)" using assms unfolding continuous_at tendsto_iff by auto
himmelma@33175
  3214
  then obtain d where d:"d>0" "\<forall>xa. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e" unfolding eventually_at by auto
himmelma@33175
  3215
  { fix x' assume "\<not> 0 < dist x' x"
himmelma@33175
  3216
    hence "x=x'"
himmelma@33175
  3217
      using dist_nz[of x' x] by auto
himmelma@33175
  3218
    hence "dist (f x') (f x) < e" using `e>0` by auto
himmelma@33175
  3219
  }
himmelma@33175
  3220
  thus "\<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e" using d by auto
himmelma@33175
  3221
qed
himmelma@33175
  3222
himmelma@33175
  3223
lemma continuous_on_eq_continuous_within:
himmelma@33175
  3224
 "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x within s) f)" (is "?lhs = ?rhs")
himmelma@33175
  3225
proof
himmelma@33175
  3226
  assume ?rhs
himmelma@33175
  3227
  { fix x assume "x\<in>s"
himmelma@33175
  3228
    fix e::real assume "e>0"
himmelma@33175
  3229
    assume "\<exists>d>0. \<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e"
himmelma@33175
  3230
    then obtain d where "d>0" and d:"\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e" by auto
himmelma@33175
  3231
    { fix x' assume as:"x'\<in>s" "dist x' x < d"
himmelma@33175
  3232
      hence "dist (f x') (f x) < e" using `e>0` d `x'\<in>s` dist_eq_0_iff[of x' x] zero_le_dist[of x' x] as(2) by (metis dist_eq_0_iff dist_nz) }
himmelma@33175
  3233
    hence "\<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e" using `d>0` by auto
himmelma@33175
  3234
  }
himmelma@33175
  3235
  thus ?lhs using `?rhs` unfolding continuous_on_def continuous_within Lim_within by auto
himmelma@33175
  3236
next
himmelma@33175
  3237
  assume ?lhs
himmelma@33175
  3238
  thus ?rhs unfolding continuous_on_def continuous_within Lim_within by blast
himmelma@33175
  3239
qed
himmelma@33175
  3240
himmelma@33175
  3241
lemma continuous_on:
himmelma@33175
  3242
 "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. (f ---> f(x)) (at x within s))"
himmelma@33175
  3243
  by (auto simp add: continuous_on_eq_continuous_within continuous_within)
himmelma@33175
  3244
himmelma@33175
  3245
lemma continuous_on_eq_continuous_at:
himmelma@33175
  3246
 "open s ==> (continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x) f))"
himmelma@33175
  3247
  by (auto simp add: continuous_on continuous_at Lim_within_open)
himmelma@33175
  3248
himmelma@33175
  3249
lemma continuous_within_subset:
himmelma@33175
  3250
 "continuous (at x within s) f \<Longrightarrow> t \<subseteq> s
himmelma@33175
  3251
             ==> continuous (at x within t) f"
himmelma@33175
  3252
  unfolding continuous_within by(metis Lim_within_subset)
himmelma@33175
  3253
himmelma@33175
  3254
lemma continuous_on_subset:
himmelma@33175
  3255
 "continuous_on s f \<Longrightarrow> t \<subseteq> s ==> continuous_on t f"
himmelma@33175
  3256
  unfolding continuous_on by (metis subset_eq Lim_within_subset)
himmelma@33175
  3257
himmelma@33175
  3258
lemma continuous_on_interior:
himmelma@33175
  3259
 "continuous_on s f \<Longrightarrow> x \<in> interior s ==> continuous (at x) f"
himmelma@33175
  3260
unfolding interior_def
himmelma@33175
  3261
apply simp
himmelma@33175
  3262
by (meson continuous_on_eq_continuous_at continuous_on_subset)
himmelma@33175
  3263
himmelma@33175
  3264
lemma continuous_on_eq:
himmelma@33175
  3265
 "(\<forall>x \<in> s. f x = g x) \<Longrightarrow> continuous_on s f
himmelma@33175
  3266
           ==> continuous_on s g"
himmelma@33175
  3267
  by (simp add: continuous_on_def)
himmelma@33175
  3268
himmelma@33175
  3269
text{* Characterization of various kinds of continuity in terms of sequences.  *}
himmelma@33175
  3270
himmelma@33175
  3271
(* \<longrightarrow> could be generalized, but \<longleftarrow> requires metric space *)
himmelma@33175
  3272
lemma continuous_within_sequentially:
himmelma@33175
  3273
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
himmelma@33175
  3274
  shows "continuous (at a within s) f \<longleftrightarrow>
himmelma@33175
  3275
                (\<forall>x. (\<forall>n::nat. x n \<in> s) \<and> (x ---> a) sequentially
himmelma@33175
  3276
                     --> ((f o x) ---> f a) sequentially)" (is "?lhs = ?rhs")
himmelma@33175
  3277
proof
himmelma@33175
  3278
  assume ?lhs
himmelma@33175
  3279
  { fix x::"nat \<Rightarrow> 'a" assume x:"\<forall>n. x n \<in> s" "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (x n) a < e"
himmelma@33175
  3280
    fix e::real assume "e>0"
himmelma@33175
  3281
    from `?lhs` obtain d where "d>0" and d:"\<forall>x\<in>s. 0 < dist x a \<and> dist x a < d \<longrightarrow> dist (f x) (f a) < e" unfolding continuous_within Lim_within using `e>0` by auto
himmelma@33175
  3282
    from x(2) `d>0` obtain N where N:"\<forall>n\<ge>N. dist (x n) a < d" by auto
himmelma@33175
  3283
    hence "\<exists>N. \<forall>n\<ge>N. dist ((f \<circ> x) n) (f a) < e"
himmelma@33175
  3284
      apply(rule_tac  x=N in exI) using N d  apply auto using x(1)
himmelma@33175
  3285
      apply(erule_tac x=n in allE) apply(erule_tac x=n in allE)
himmelma@33175
  3286
      apply(erule_tac x="x n" in ballE)  apply auto unfolding dist_nz[THEN sym] apply auto using `e>0` by auto
himmelma@33175
  3287
  }
himmelma@33175
  3288
  thus ?rhs unfolding continuous_within unfolding Lim_sequentially by simp
himmelma@33175
  3289
next
himmelma@33175
  3290
  assume ?rhs
himmelma@33175
  3291
  { fix e::real assume "e>0"
himmelma@33175
  3292
    assume "\<not> (\<exists>d>0. \<forall>x\<in>s. 0 < dist x a \<and> dist x a < d \<longrightarrow> dist (f x) (f a) < e)"
himmelma@33175
  3293
    hence "\<forall>d. \<exists>x. d>0 \<longrightarrow> x\<in>s \<and> (0 < dist x a \<and> dist x a < d \<and> \<not> dist (f x) (f a) < e)" by blast
himmelma@33175
  3294
    then obtain x where x:"\<forall>d>0. x d \<in> s \<and> (0 < dist (x d) a \<and> dist (x d) a < d \<and> \<not> dist (f (x d)) (f a) < e)"
himmelma@33175
  3295
      using choice[of "\<lambda>d x.0<d \<longrightarrow> x\<in>s \<and> (0 < dist x a \<and> dist x a < d \<and> \<not> dist (f x) (f a) < e)"] by auto
himmelma@33175
  3296
    { fix d::real assume "d>0"
himmelma@33175
  3297
      hence "\<exists>N::nat. inverse (real (N + 1)) < d" using real_arch_inv[of d] by (auto, rule_tac x="n - 1" in exI)auto
himmelma@33175
  3298
      then obtain N::nat where N:"inverse (real (N + 1)) < d" by auto
himmelma@33175
  3299
      { fix n::nat assume n:"n\<ge>N"
himmelma@33175
  3300
        hence "dist (x (inverse (real (n + 1)))) a < inverse (real (n + 1))" using x[THEN spec[where x="inverse (real (n + 1))"]] by auto
himmelma@33175
  3301
        moreover have "inverse (real (n + 1)) < d" using N n by (auto, metis Suc_le_mono le_SucE less_imp_inverse_less nat_le_real_less order_less_trans real_of_nat_Suc real_of_nat_Suc_gt_zero)
himmelma@33175
  3302
        ultimately have "dist (x (inverse (real (n + 1)))) a < d" by auto
himmelma@33175
  3303
      }
himmelma@33175
  3304
      hence "\<exists>N::nat. \<forall>n\<ge>N. dist (x (inverse (real (n + 1)))) a < d" by auto
himmelma@33175
  3305
    }
himmelma@33175
  3306
    hence "(\<forall>n::nat. x (inverse (real (n + 1))) \<in> s) \<and> (\<forall>e>0. \<exists>N::nat. \<forall>n\<ge>N. dist (x (inverse (real (n + 1)))) a < e)" using x by auto
himmelma@33175
  3307
    hence "\<forall>e>0. \<exists>N::nat. \<forall>n\<ge>N. dist (f (x (inverse (real (n + 1))))) (f a) < e"  using `?rhs`[THEN spec[where x="\<lambda>n::nat. x (inverse (real (n+1)))"], unfolded Lim_sequentially] by auto
himmelma@33175
  3308
    hence "False" apply(erule_tac x=e in allE) using `e>0` using x by auto
himmelma@33175
  3309
  }
himmelma@33175
  3310
  thus ?lhs  unfolding continuous_within unfolding Lim_within unfolding Lim_sequentially by blast
himmelma@33175
  3311
qed
himmelma@33175
  3312
himmelma@33175
  3313
lemma continuous_at_sequentially:
himmelma@33175
  3314
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
himmelma@33175
  3315
  shows "continuous (at a) f \<longleftrightarrow> (\<forall>x. (x ---> a) sequentially
himmelma@33175
  3316
                  --> ((f o x) ---> f a) sequentially)"
himmelma@33175
  3317
  using continuous_within_sequentially[of a UNIV f] unfolding within_UNIV by auto
himmelma@33175
  3318
himmelma@33175
  3319
lemma continuous_on_sequentially:
himmelma@33175
  3320
 "continuous_on s f \<longleftrightarrow>  (\<forall>x. \<forall>a \<in> s. (\<forall>n. x(n) \<in> s) \<and> (x ---> a) sequentially
himmelma@33175
  3321
                    --> ((f o x) ---> f(a)) sequentially)" (is "?lhs = ?rhs")
himmelma@33175
  3322
proof
himmelma@33175
  3323
  assume ?rhs thus ?lhs using continuous_within_sequentially[of _ s f] unfolding continuous_on_eq_continuous_within by auto
himmelma@33175
  3324
next
himmelma@33175
  3325
  assume ?lhs thus ?rhs unfolding continuous_on_eq_continuous_within using continuous_within_sequentially[of _ s f] by auto
himmelma@33175
  3326
qed
himmelma@33175
  3327
himmelma@33175
  3328
lemma uniformly_continuous_on_sequentially:
himmelma@33175
  3329
  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3330
  shows "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>
himmelma@33175
  3331
                    ((\<lambda>n. x n - y n) ---> 0) sequentially
himmelma@33175
  3332
                    \<longrightarrow> ((\<lambda>n. f(x n) - f(y n)) ---> 0) sequentially)" (is "?lhs = ?rhs")
himmelma@33175
  3333
proof
himmelma@33175
  3334
  assume ?lhs
himmelma@33175
  3335
  { fix x y assume x:"\<forall>n. x n \<in> s" and y:"\<forall>n. y n \<in> s" and xy:"((\<lambda>n. x n - y n) ---> 0) sequentially"
himmelma@33175
  3336
    { fix e::real assume "e>0"
himmelma@33175
  3337
      then obtain d where "d>0" and d:"\<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e"
himmelma@33175
  3338
        using `?lhs`[unfolded uniformly_continuous_on_def, THEN spec[where x=e]] by auto
himmelma@33175
  3339
      obtain N where N:"\<forall>n\<ge>N. norm (x n - y n - 0) < d" using xy[unfolded Lim_sequentially dist_norm] and `d>0` by auto
himmelma@33175
  3340
      { fix n assume "n\<ge>N"
himmelma@33175
  3341
        hence "norm (f (x n) - f (y n) - 0) < e"
himmelma@33175
  3342
          using N[THEN spec[where x=n]] using d[THEN bspec[where x="x n"], THEN bspec[where x="y n"]] using x and y
himmelma@33175
  3343
          unfolding dist_commute and dist_norm by simp  }
himmelma@33175
  3344
      hence "\<exists>N. \<forall>n\<ge>N. norm (f (x n) - f (y n) - 0) < e"  by auto  }
himmelma@33175
  3345
    hence "((\<lambda>n. f(x n) - f(y n)) ---> 0) sequentially" unfolding Lim_sequentially and dist_norm by auto  }
himmelma@33175
  3346
  thus ?rhs by auto
himmelma@33175
  3347
next
himmelma@33175
  3348
  assume ?rhs
himmelma@33175
  3349
  { assume "\<not> ?lhs"
himmelma@33175
  3350
    then obtain e where "e>0" "\<forall>d>0. \<exists>x\<in>s. \<exists>x'\<in>s. dist x' x < d \<and> \<not> dist (f x') (f x) < e" unfolding uniformly_continuous_on_def by auto
himmelma@33175
  3351
    then obtain fa where fa:"\<forall>x.  0 < x \<longrightarrow> fst (fa x) \<in> s \<and> snd (fa x) \<in> s \<and> dist (fst (fa x)) (snd (fa x)) < x \<and> \<not> dist (f (fst (fa x))) (f (snd (fa x))) < e"
himmelma@33175
  3352
      using choice[of "\<lambda>d x. d>0 \<longrightarrow> fst x \<in> s \<and> snd x \<in> s \<and> dist (snd x) (fst x) < d \<and> \<not> dist (f (snd x)) (f (fst x)) < e"] unfolding Bex_def
himmelma@33175
  3353
      by (auto simp add: dist_commute)
himmelma@33175
  3354
    def x \<equiv> "\<lambda>n::nat. fst (fa (inverse (real n + 1)))"
himmelma@33175
  3355
    def y \<equiv> "\<lambda>n::nat. snd (fa (inverse (real n + 1)))"
himmelma@33175
  3356
    have xyn:"\<forall>n. x n \<in> s \<and> y n \<in> s" and xy0:"\<forall>n. dist (x n) (y n) < inverse (real n + 1)" and fxy:"\<forall>n. \<not> dist (f (x n)) (f (y n)) < e"
himmelma@33175
  3357
      unfolding x_def and y_def using fa by auto
himmelma@33175
  3358
    have 1:"\<And>(x::'a) y. dist (x - y) 0 = dist x y" unfolding dist_norm by auto
himmelma@33175
  3359
    have 2:"\<And>(x::'b) y. dist (x - y) 0 = dist x y" unfolding dist_norm by auto
himmelma@33175
  3360
    { fix e::real assume "e>0"
himmelma@33175
  3361
      then obtain N::nat where "N \<noteq> 0" and N:"0 < inverse (real N) \<and> inverse (real N) < e" unfolding real_arch_inv[of e]   by auto
himmelma@33175
  3362
      { fix n::nat assume "n\<ge>N"
himmelma@33175
  3363
        hence "inverse (real n + 1) < inverse (real N)" using real_of_nat_ge_zero and `N\<noteq>0` by auto
himmelma@33175
  3364
        also have "\<dots> < e" using N by auto
himmelma@33175
  3365
        finally have "inverse (real n + 1) < e" by auto
himmelma@33175
  3366
        hence "dist (x n - y n) 0 < e" unfolding 1 using xy0[THEN spec[where x=n]] by auto  }
himmelma@33175
  3367
      hence "\<exists>N. \<forall>n\<ge>N. dist (x n - y n) 0 < e" by auto  }
himmelma@33175
  3368
    hence "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (f (x n) - f (y n)) 0 < e" using `?rhs`[THEN spec[where x=x], THEN spec[where x=y]] and xyn unfolding Lim_sequentially by auto
himmelma@33175
  3369
    hence False unfolding 2 using fxy and `e>0` by auto  }
himmelma@33175
  3370
  thus ?lhs unfolding uniformly_continuous_on_def by blast
himmelma@33175
  3371
qed
himmelma@33175
  3372
himmelma@33175
  3373
text{* The usual transformation theorems. *}
himmelma@33175
  3374
himmelma@33175
  3375
lemma continuous_transform_within:
himmelma@33175
  3376
  fixes f g :: "'a::metric_space \<Rightarrow> 'b::metric_space"
himmelma@33175
  3377
  assumes "0 < d" "x \<in> s" "\<forall>x' \<in> s. dist x' x < d --> f x' = g x'"
himmelma@33175
  3378
          "continuous (at x within s) f"
himmelma@33175
  3379
  shows "continuous (at x within s) g"
himmelma@33175
  3380
proof-
himmelma@33175
  3381
  { fix e::real assume "e>0"
himmelma@33175
  3382
    then obtain d' where d':"d'>0" "\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d' \<longrightarrow> dist (f xa) (f x) < e" using assms(4) unfolding continuous_within Lim_within by auto
himmelma@33175
  3383
    { fix x' assume "x'\<in>s" "0 < dist x' x" "dist x' x < (min d d')"
himmelma@33175
  3384
      hence "dist (f x') (g x) < e" using assms(2,3) apply(erule_tac x=x in ballE) using d' by auto  }
himmelma@33175
  3385
    hence "\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < (min d d') \<longrightarrow> dist (f xa) (g x) < e" by blast
himmelma@33175
  3386
    hence "\<exists>d>0. \<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (g x) < e" using `d>0` `d'>0` by(rule_tac x="min d d'" in exI)auto  }
himmelma@33175
  3387
  hence "(f ---> g x) (at x within s)" unfolding Lim_within using assms(1) by auto
himmelma@33175
  3388
  thus ?thesis unfolding continuous_within using Lim_transform_within[of d s x f g "g x"] using assms by blast
himmelma@33175
  3389
qed
himmelma@33175
  3390
himmelma@33175
  3391
lemma continuous_transform_at:
himmelma@33175
  3392
  fixes f g :: "'a::metric_space \<Rightarrow> 'b::metric_space"
himmelma@33175
  3393
  assumes "0 < d" "\<forall>x'. dist x' x < d --> f x' = g x'"
himmelma@33175
  3394
          "continuous (at x) f"
himmelma@33175
  3395
  shows "continuous (at x) g"
himmelma@33175
  3396
proof-
himmelma@33175
  3397
  { fix e::real assume "e>0"
himmelma@33175
  3398
    then obtain d' where d':"d'>0" "\<forall>xa. 0 < dist xa x \<and> dist xa x < d' \<longrightarrow> dist (f xa) (f x) < e" using assms(3) unfolding continuous_at Lim_at by auto
himmelma@33175
  3399
    { fix x' assume "0 < dist x' x" "dist x' x < (min d d')"
himmelma@33175
  3400
      hence "dist (f x') (g x) < e" using assms(2) apply(erule_tac x=x in allE) using d' by auto
himmelma@33175
  3401
    }
himmelma@33175
  3402
    hence "\<forall>xa. 0 < dist xa x \<and> dist xa x < (min d d') \<longrightarrow> dist (f xa) (g x) < e" by blast
himmelma@33175
  3403
    hence "\<exists>d>0. \<forall>xa. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (g x) < e" using `d>0` `d'>0` by(rule_tac x="min d d'" in exI)auto
himmelma@33175
  3404
  }
himmelma@33175
  3405
  hence "(f ---> g x) (at x)" unfolding Lim_at using assms(1) by auto
himmelma@33175
  3406
  thus ?thesis unfolding continuous_at using Lim_transform_at[of d x f g "g x"] using assms by blast
himmelma@33175
  3407
qed
himmelma@33175
  3408
himmelma@33175
  3409
text{* Combination results for pointwise continuity. *}
himmelma@33175
  3410
himmelma@33175
  3411
lemma continuous_const: "continuous net (\<lambda>x. c)"
himmelma@33175
  3412
  by (auto simp add: continuous_def Lim_const)
himmelma@33175
  3413
himmelma@33175
  3414
lemma continuous_cmul:
himmelma@33175
  3415
  fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3416
  shows "continuous net f ==> continuous net (\<lambda>x. c *\<^sub>R f x)"
himmelma@33175
  3417
  by (auto simp add: continuous_def Lim_cmul)
himmelma@33175
  3418
himmelma@33175
  3419
lemma continuous_neg:
himmelma@33175
  3420
  fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3421
  shows "continuous net f ==> continuous net (\<lambda>x. -(f x))"
himmelma@33175
  3422
  by (auto simp add: continuous_def Lim_neg)
himmelma@33175
  3423
himmelma@33175
  3424
lemma continuous_add:
himmelma@33175
  3425
  fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3426
  shows "continuous net f \<Longrightarrow> continuous net g \<Longrightarrow> continuous net (\<lambda>x. f x + g x)"
himmelma@33175
  3427
  by (auto simp add: continuous_def Lim_add)
himmelma@33175
  3428
himmelma@33175
  3429
lemma continuous_sub:
himmelma@33175
  3430
  fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3431
  shows "continuous net f \<Longrightarrow> continuous net g \<Longrightarrow> continuous net (\<lambda>x. f x - g x)"
himmelma@33175
  3432
  by (auto simp add: continuous_def Lim_sub)
himmelma@33175
  3433
hoelzl@34951
  3434
himmelma@33175
  3435
text{* Same thing for setwise continuity. *}
himmelma@33175
  3436
himmelma@33175
  3437
lemma continuous_on_const:
himmelma@33175
  3438
 "continuous_on s (\<lambda>x. c)"
himmelma@33175
  3439
  unfolding continuous_on_eq_continuous_within using continuous_const by blast
himmelma@33175
  3440
himmelma@33175
  3441
lemma continuous_on_cmul:
himmelma@33175
  3442
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3443
  shows "continuous_on s f ==>  continuous_on s (\<lambda>x. c *\<^sub>R (f x))"
himmelma@33175
  3444
  unfolding continuous_on_eq_continuous_within using continuous_cmul by blast
himmelma@33175
  3445
himmelma@33175
  3446
lemma continuous_on_neg:
himmelma@33175
  3447
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3448
  shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. - f x)"
himmelma@33175
  3449
  unfolding continuous_on_eq_continuous_within using continuous_neg by blast
himmelma@33175
  3450
himmelma@33175
  3451
lemma continuous_on_add:
himmelma@33175
  3452
  fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3453
  shows "continuous_on s f \<Longrightarrow> continuous_on s g
himmelma@33175
  3454
           \<Longrightarrow> continuous_on s (\<lambda>x. f x + g x)"
himmelma@33175
  3455
  unfolding continuous_on_eq_continuous_within using continuous_add by blast
himmelma@33175
  3456
himmelma@33175
  3457
lemma continuous_on_sub:
himmelma@33175
  3458
  fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3459
  shows "continuous_on s f \<Longrightarrow> continuous_on s g
himmelma@33175
  3460
           \<Longrightarrow> continuous_on s (\<lambda>x. f x - g x)"
himmelma@33175
  3461
  unfolding continuous_on_eq_continuous_within using continuous_sub by blast
himmelma@33175
  3462
himmelma@33175
  3463
text{* Same thing for uniform continuity, using sequential formulations. *}
himmelma@33175
  3464
himmelma@33175
  3465
lemma uniformly_continuous_on_const:
himmelma@33175
  3466
 "uniformly_continuous_on s (\<lambda>x. c)"
himmelma@33175
  3467
  unfolding uniformly_continuous_on_def by simp
himmelma@33175
  3468
himmelma@33175
  3469
lemma uniformly_continuous_on_cmul:
himmelma@33175
  3470
  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3471
    (* FIXME: generalize 'a to metric_space *)
himmelma@33175
  3472
  assumes "uniformly_continuous_on s f"
himmelma@33175
  3473
  shows "uniformly_continuous_on s (\<lambda>x. c *\<^sub>R f(x))"
himmelma@33175
  3474
proof-
himmelma@33175
  3475
  { fix x y assume "((\<lambda>n. f (x n) - f (y n)) ---> 0) sequentially"
himmelma@33175
  3476
    hence "((\<lambda>n. c *\<^sub>R f (x n) - c *\<^sub>R f (y n)) ---> 0) sequentially"
himmelma@33175
  3477
      using Lim_cmul[of "(\<lambda>n. f (x n) - f (y n))" 0 sequentially c]
himmelma@33175
  3478
      unfolding scaleR_zero_right scaleR_right_diff_distrib by auto
himmelma@33175
  3479
  }
himmelma@33175
  3480
  thus ?thesis using assms unfolding uniformly_continuous_on_sequentially by auto
himmelma@33175
  3481
qed
himmelma@33175
  3482
himmelma@33175
  3483
lemma dist_minus:
himmelma@33175
  3484
  fixes x y :: "'a::real_normed_vector"
himmelma@33175
  3485
  shows "dist (- x) (- y) = dist x y"
himmelma@33175
  3486
  unfolding dist_norm minus_diff_minus norm_minus_cancel ..
himmelma@33175
  3487
himmelma@33175
  3488
lemma uniformly_continuous_on_neg:
himmelma@33175
  3489
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3490
  shows "uniformly_continuous_on s f
himmelma@33175
  3491
         ==> uniformly_continuous_on s (\<lambda>x. -(f x))"
himmelma@33175
  3492
  unfolding uniformly_continuous_on_def dist_minus .
himmelma@33175
  3493
himmelma@33175
  3494
lemma uniformly_continuous_on_add:
himmelma@33175
  3495
  fixes f g :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector" (* FIXME: generalize 'a *)
himmelma@33175
  3496
  assumes "uniformly_continuous_on s f" "uniformly_continuous_on s g"
himmelma@33175
  3497
  shows "uniformly_continuous_on s (\<lambda>x. f x + g x)"
himmelma@33175
  3498
proof-
himmelma@33175
  3499
  {  fix x y assume "((\<lambda>n. f (x n) - f (y n)) ---> 0) sequentially"
himmelma@33175
  3500
                    "((\<lambda>n. g (x n) - g (y n)) ---> 0) sequentially"
himmelma@33175
  3501
    hence "((\<lambda>xa. f (x xa) - f (y xa) + (g (x xa) - g (y xa))) ---> 0 + 0) sequentially"
himmelma@33175
  3502
      using Lim_add[of "\<lambda> n. f (x n) - f (y n)" 0  sequentially "\<lambda> n. g (x n) - g (y n)" 0] by auto
himmelma@33175
  3503
    hence "((\<lambda>n. f (x n) + g (x n) - (f (y n) + g (y n))) ---> 0) sequentially" unfolding Lim_sequentially and add_diff_add [symmetric] by auto  }
himmelma@33175
  3504
  thus ?thesis using assms unfolding uniformly_continuous_on_sequentially by auto
himmelma@33175
  3505
qed
himmelma@33175
  3506
himmelma@33175
  3507
lemma uniformly_continuous_on_sub:
himmelma@33175
  3508
  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector" (* FIXME: generalize 'a *)
himmelma@33175
  3509
  shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s g
himmelma@33175
  3510
           ==> uniformly_continuous_on s  (\<lambda>x. f x - g x)"
himmelma@33175
  3511
  unfolding ab_diff_minus
himmelma@33175
  3512
  using uniformly_continuous_on_add[of s f "\<lambda>x. - g x"]
himmelma@33175
  3513
  using uniformly_continuous_on_neg[of s g] by auto
himmelma@33175
  3514
himmelma@33175
  3515
text{* Identity function is continuous in every sense. *}
himmelma@33175
  3516
himmelma@33175
  3517
lemma continuous_within_id:
himmelma@33175
  3518
 "continuous (at a within s) (\<lambda>x. x)"
himmelma@33175
  3519
  unfolding continuous_within by (rule Lim_at_within [OF Lim_ident_at])
himmelma@33175
  3520
himmelma@33175
  3521
lemma continuous_at_id:
himmelma@33175
  3522
 "continuous (at a) (\<lambda>x. x)"
himmelma@33175
  3523
  unfolding continuous_at by (rule Lim_ident_at)
himmelma@33175
  3524
himmelma@33175
  3525
lemma continuous_on_id:
himmelma@33175
  3526
 "continuous_on s (\<lambda>x. x)"
himmelma@33175
  3527
  unfolding continuous_on Lim_within by auto
himmelma@33175
  3528
himmelma@33175
  3529
lemma uniformly_continuous_on_id:
himmelma@33175
  3530
 "uniformly_continuous_on s (\<lambda>x. x)"
himmelma@33175
  3531
  unfolding uniformly_continuous_on_def by auto
himmelma@33175
  3532
himmelma@33175
  3533
text{* Continuity of all kinds is preserved under composition. *}
himmelma@33175
  3534
himmelma@33175
  3535
lemma continuous_within_compose:
himmelma@33175
  3536
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
himmelma@33175
  3537
  fixes g :: "'b::metric_space \<Rightarrow> 'c::metric_space"
himmelma@33175
  3538
  assumes "continuous (at x within s) f"   "continuous (at (f x) within f ` s) g"
himmelma@33175
  3539
  shows "continuous (at x within s) (g o f)"
himmelma@33175
  3540
proof-
himmelma@33175
  3541
  { fix e::real assume "e>0"
himmelma@33175
  3542
    with assms(2)[unfolded continuous_within Lim_within] obtain d  where "d>0" and d:"\<forall>xa\<in>f ` s. 0 < dist xa (f x) \<and> dist xa (f x) < d \<longrightarrow> dist (g xa) (g (f x)) < e" by auto
himmelma@33175
  3543
    from assms(1)[unfolded continuous_within Lim_within] obtain d' where "d'>0" and d':"\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d' \<longrightarrow> dist (f xa) (f x) < d" using `d>0` by auto
himmelma@33175
  3544
    { fix y assume as:"y\<in>s"  "0 < dist y x"  "dist y x < d'"
himmelma@33175
  3545
      hence "dist (f y) (f x) < d" using d'[THEN bspec[where x=y]] by (auto simp add:dist_commute)
himmelma@33175
  3546
      hence "dist (g (f y)) (g (f x)) < e" using as(1) d[THEN bspec[where x="f y"]] unfolding dist_nz[THEN sym] using `e>0` by auto   }
himmelma@33175
  3547
    hence "\<exists>d>0. \<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (g (f xa)) (g (f x)) < e" using `d'>0` by auto  }
himmelma@33175
  3548
  thus ?thesis unfolding continuous_within Lim_within by auto
himmelma@33175
  3549
qed
himmelma@33175
  3550
himmelma@33175
  3551
lemma continuous_at_compose:
himmelma@33175
  3552
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
himmelma@33175
  3553
  fixes g :: "'b::metric_space \<Rightarrow> 'c::metric_space"
himmelma@33175
  3554
  assumes "continuous (at x) f"  "continuous (at (f x)) g"
himmelma@33175
  3555
  shows "continuous (at x) (g o f)"
himmelma@33175
  3556
proof-
himmelma@33175
  3557
  have " continuous (at (f x) within range f) g" using assms(2) using continuous_within_subset[of "f x" UNIV g "range f", unfolded within_UNIV] by auto
himmelma@33175
  3558
  thus ?thesis using assms(1) using continuous_within_compose[of x UNIV f g, unfolded within_UNIV] by auto
himmelma@33175
  3559
qed
himmelma@33175
  3560
himmelma@33175
  3561
lemma continuous_on_compose:
himmelma@33175
  3562
 "continuous_on s f \<Longrightarrow> continuous_on (f ` s) g \<Longrightarrow> continuous_on s (g o f)"
himmelma@33175
  3563
  unfolding continuous_on_eq_continuous_within using continuous_within_compose[of _ s f g] by auto
himmelma@33175
  3564
himmelma@33175
  3565
lemma uniformly_continuous_on_compose:
himmelma@33175
  3566
  assumes "uniformly_continuous_on s f"  "uniformly_continuous_on (f ` s) g"
himmelma@33175
  3567
  shows "uniformly_continuous_on s (g o f)"
himmelma@33175
  3568
proof-
himmelma@33175
  3569
  { fix e::real assume "e>0"
himmelma@33175
  3570
    then obtain d where "d>0" and d:"\<forall>x\<in>f ` s. \<forall>x'\<in>f ` s. dist x' x < d \<longrightarrow> dist (g x') (g x) < e" using assms(2) unfolding uniformly_continuous_on_def by auto
himmelma@33175
  3571
    obtain d' where "d'>0" "\<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d' \<longrightarrow> dist (f x') (f x) < d" using `d>0` using assms(1) unfolding uniformly_continuous_on_def by auto
himmelma@33175
  3572
    hence "\<exists>d>0. \<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist ((g \<circ> f) x') ((g \<circ> f) x) < e" using `d>0` using d by auto  }
himmelma@33175
  3573
  thus ?thesis using assms unfolding uniformly_continuous_on_def by auto
himmelma@33175
  3574
qed
himmelma@33175
  3575
himmelma@33175
  3576
text{* Continuity in terms of open preimages. *}
himmelma@33175
  3577
himmelma@33175
  3578
lemma continuous_at_open:
himmelma@33175
  3579
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
himmelma@33175
  3580
  shows "continuous (at x) f \<longleftrightarrow> (\<forall>t. open t \<and> f x \<in> t --> (\<exists>s. open s \<and> x \<in> s \<and> (\<forall>x' \<in> s. (f x') \<in> t)))" (is "?lhs = ?rhs")
himmelma@33175
  3581
proof
himmelma@33175
  3582
  assume ?lhs
himmelma@33175
  3583
  { fix t assume as: "open t" "f x \<in> t"
himmelma@33175
  3584
    then obtain e where "e>0" and e:"ball (f x) e \<subseteq> t" unfolding open_contains_ball by auto
himmelma@33175
  3585
himmelma@33175
  3586
    obtain d where "d>0" and d:"\<forall>y. 0 < dist y x \<and> dist y x < d \<longrightarrow> dist (f y) (f x) < e" using `e>0` using `?lhs`[unfolded continuous_at Lim_at open_dist] by auto
himmelma@33175
  3587
himmelma@33175
  3588
    have "open (ball x d)" using open_ball by auto
himmelma@33175
  3589
    moreover have "x \<in> ball x d" unfolding centre_in_ball using `d>0` by simp
himmelma@33175
  3590
    moreover
himmelma@33175
  3591
    { fix x' assume "x'\<in>ball x d" hence "f x' \<in> t"
himmelma@33175
  3592
        using e[unfolded subset_eq Ball_def mem_ball, THEN spec[where x="f x'"]]    d[THEN spec[where x=x']]
himmelma@33175
  3593
        unfolding mem_ball apply (auto simp add: dist_commute)
himmelma@33175
  3594
        unfolding dist_nz[THEN sym] using as(2) by auto  }
himmelma@33175
  3595
    hence "\<forall>x'\<in>ball x d. f x' \<in> t" by auto
himmelma@33175
  3596
    ultimately have "\<exists>s. open s \<and> x \<in> s \<and> (\<forall>x'\<in>s. f x' \<in> t)"
himmelma@33175
  3597
      apply(rule_tac x="ball x d" in exI) by simp  }
himmelma@33175
  3598
  thus ?rhs by auto
himmelma@33175
  3599
next
himmelma@33175
  3600
  assume ?rhs
himmelma@33175
  3601
  { fix e::real assume "e>0"
himmelma@33175
  3602
    then obtain s where s: "open s"  "x \<in> s"  "\<forall>x'\<in>s. f x' \<in> ball (f x) e" using `?rhs`[unfolded continuous_at Lim_at, THEN spec[where x="ball (f x) e"]]
himmelma@33175
  3603
      unfolding centre_in_ball[of "f x" e, THEN sym] by auto
himmelma@33175
  3604
    then obtain d where "d>0" and d:"ball x d \<subseteq> s" unfolding open_contains_ball by auto
himmelma@33175
  3605
    { fix y assume "0 < dist y x \<and> dist y x < d"
himmelma@33175
  3606
      hence "dist (f y) (f x) < e" using d[unfolded subset_eq Ball_def mem_ball, THEN spec[where x=y]]
himmelma@33175
  3607
        using s(3)[THEN bspec[where x=y], unfolded mem_ball] by (auto simp add: dist_commute)  }
himmelma@33175
  3608
    hence "\<exists>d>0. \<forall>xa. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e" using `d>0` by auto  }
himmelma@33175
  3609
  thus ?lhs unfolding continuous_at Lim_at by auto
himmelma@33175
  3610
qed
himmelma@33175
  3611
himmelma@33175
  3612
lemma continuous_on_open:
himmelma@33175
  3613
 "continuous_on s f \<longleftrightarrow>
himmelma@33175
  3614
        (\<forall>t. openin (subtopology euclidean (f ` s)) t
himmelma@33175
  3615
            --> openin (subtopology euclidean s) {x \<in> s. f x \<in> t})" (is "?lhs = ?rhs")
himmelma@33175
  3616
proof
himmelma@33175
  3617
  assume ?lhs
himmelma@33175
  3618
  { fix t assume as:"openin (subtopology euclidean (f ` s)) t"
himmelma@33175
  3619
    have "{x \<in> s. f x \<in> t} \<subseteq> s" using as[unfolded openin_euclidean_subtopology_iff] by auto
himmelma@33175
  3620
    moreover
himmelma@33175
  3621
    { fix x assume as':"x\<in>{x \<in> s. f x \<in> t}"
himmelma@33175
  3622
      then obtain e where e: "e>0" "\<forall>x'\<in>f ` s. dist x' (f x) < e \<longrightarrow> x' \<in> t" using as[unfolded openin_euclidean_subtopology_iff, THEN conjunct2, THEN bspec[where x="f x"]] by auto
himmelma@33175
  3623
      from this(1) obtain d where d: "d>0" "\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e" using `?lhs`[unfolded continuous_on Lim_within, THEN bspec[where x=x]] using as' by auto
himmelma@33175
  3624
      have "\<exists>e>0. \<forall>x'\<in>s. dist x' x < e \<longrightarrow> x' \<in> {x \<in> s. f x \<in> t}" using d e unfolding dist_nz[THEN sym] by (rule_tac x=d in exI, auto)  }
himmelma@33175
  3625
    ultimately have "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}" unfolding openin_euclidean_subtopology_iff by auto  }
himmelma@33175
  3626
  thus ?rhs unfolding continuous_on Lim_within using openin by auto
himmelma@33175
  3627
next
himmelma@33175
  3628
  assume ?rhs
himmelma@33175
  3629
  { fix e::real and x assume "x\<in>s" "e>0"
himmelma@33175
  3630
    { fix xa x' assume "dist (f xa) (f x) < e" "xa \<in> s" "x' \<in> s" "dist (f xa) (f x') < e - dist (f xa) (f x)"
himmelma@33175
  3631
      hence "dist (f x') (f x) < e" using dist_triangle[of "f x'" "f x" "f xa"]
himmelma@33175
  3632
        by (auto simp add: dist_commute)  }
himmelma@33175
  3633
    hence "ball (f x) e \<inter> f ` s \<subseteq> f ` s \<and> (\<forall>xa\<in>ball (f x) e \<inter> f ` s. \<exists>ea>0. \<forall>x'\<in>f ` s. dist x' xa < ea \<longrightarrow> x' \<in> ball (f x) e \<inter> f ` s)" apply auto
himmelma@33175
  3634
      apply(rule_tac x="e - dist (f xa) (f x)" in exI) using `e>0` by (auto simp add: dist_commute)
himmelma@33175
  3635
    hence "\<forall>xa\<in>{xa \<in> s. f xa \<in> ball (f x) e \<inter> f ` s}. \<exists>ea>0. \<forall>x'\<in>s. dist x' xa < ea \<longrightarrow> x' \<in> {xa \<in> s. f xa \<in> ball (f x) e \<inter> f ` s}"
himmelma@33175
  3636
      using `?rhs`[unfolded openin_euclidean_subtopology_iff, THEN spec[where x="ball (f x) e \<inter> f ` s"]] by auto
himmelma@33175
  3637
    hence "\<exists>d>0. \<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e" apply(erule_tac x=x in ballE) apply auto using `e>0` `x\<in>s` by (auto simp add: dist_commute)  }
himmelma@33175
  3638
  thus ?lhs unfolding continuous_on Lim_within by auto
himmelma@33175
  3639
qed
himmelma@33175
  3640
himmelma@33175
  3641
(* ------------------------------------------------------------------------- *)
himmelma@33175
  3642
(* Similarly in terms of closed sets.                                        *)
himmelma@33175
  3643
(* ------------------------------------------------------------------------- *)
himmelma@33175
  3644
himmelma@33175
  3645
lemma continuous_on_closed:
himmelma@33175
  3646
 "continuous_on s f \<longleftrightarrow>  (\<forall>t. closedin (subtopology euclidean (f ` s)) t  --> closedin (subtopology euclidean s) {x \<in> s. f x \<in> t})" (is "?lhs = ?rhs")
himmelma@33175
  3647
proof
himmelma@33175
  3648
  assume ?lhs
himmelma@33175
  3649
  { fix t
himmelma@33175
  3650
    have *:"s - {x \<in> s. f x \<in> f ` s - t} = {x \<in> s. f x \<in> t}" by auto
himmelma@33175
  3651
    have **:"f ` s - (f ` s - (f ` s - t)) = f ` s - t" by auto
himmelma@33175
  3652
    assume as:"closedin (subtopology euclidean (f ` s)) t"
himmelma@33175
  3653
    hence "closedin (subtopology euclidean (f ` s)) (f ` s - (f ` s - t))" unfolding closedin_def topspace_euclidean_subtopology unfolding ** by auto
himmelma@33175
  3654
    hence "closedin (subtopology euclidean s) {x \<in> s. f x \<in> t}" using `?lhs`[unfolded continuous_on_open, THEN spec[where x="(f ` s) - t"]]
himmelma@33175
  3655
      unfolding openin_closedin_eq topspace_euclidean_subtopology unfolding * by auto  }
himmelma@33175
  3656
  thus ?rhs by auto
himmelma@33175
  3657
next
himmelma@33175
  3658
  assume ?rhs
himmelma@33175
  3659
  { fix t
himmelma@33175
  3660
    have *:"s - {x \<in> s. f x \<in> f ` s - t} = {x \<in> s. f x \<in> t}" by auto
himmelma@33175
  3661
    assume as:"openin (subtopology euclidean (f ` s)) t"
himmelma@33175
  3662
    hence "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}" using `?rhs`[THEN spec[where x="(f ` s) - t"]]
himmelma@33175
  3663
      unfolding openin_closedin_eq topspace_euclidean_subtopology *[THEN sym] closedin_subtopology by auto }
himmelma@33175
  3664
  thus ?lhs unfolding continuous_on_open by auto
himmelma@33175
  3665
qed
himmelma@33175
  3666
himmelma@33175
  3667
text{* Half-global and completely global cases.                                  *}
himmelma@33175
  3668
himmelma@33175
  3669
lemma continuous_open_in_preimage:
himmelma@33175
  3670
  assumes "continuous_on s f"  "open t"
himmelma@33175
  3671
  shows "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
himmelma@33175
  3672
proof-
himmelma@33175
  3673
  have *:"\<forall>x. x \<in> s \<and> f x \<in> t \<longleftrightarrow> x \<in> s \<and> f x \<in> (t \<inter> f ` s)" by auto
himmelma@33175
  3674
  have "openin (subtopology euclidean (f ` s)) (t \<inter> f ` s)"
himmelma@33175
  3675
    using openin_open_Int[of t "f ` s", OF assms(2)] unfolding openin_open by auto
himmelma@33175
  3676
  thus ?thesis using assms(1)[unfolded continuous_on_open, THEN spec[where x="t \<inter> f ` s"]] using * by auto
himmelma@33175
  3677
qed
himmelma@33175
  3678
himmelma@33175
  3679
lemma continuous_closed_in_preimage:
himmelma@33175
  3680
  assumes "continuous_on s f"  "closed t"
himmelma@33175
  3681
  shows "closedin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
himmelma@33175
  3682
proof-
himmelma@33175
  3683
  have *:"\<forall>x. x \<in> s \<and> f x \<in> t \<longleftrightarrow> x \<in> s \<and> f x \<in> (t \<inter> f ` s)" by auto
himmelma@33175
  3684
  have "closedin (subtopology euclidean (f ` s)) (t \<inter> f ` s)"
himmelma@33175
  3685
    using closedin_closed_Int[of t "f ` s", OF assms(2)] unfolding Int_commute by auto
himmelma@33175
  3686
  thus ?thesis
himmelma@33175
  3687
    using assms(1)[unfolded continuous_on_closed, THEN spec[where x="t \<inter> f ` s"]] using * by auto
himmelma@33175
  3688
qed
himmelma@33175
  3689
himmelma@33175
  3690
lemma continuous_open_preimage:
himmelma@33175
  3691
  assumes "continuous_on s f" "open s" "open t"
himmelma@33175
  3692
  shows "open {x \<in> s. f x \<in> t}"
himmelma@33175
  3693
proof-
himmelma@33175
  3694
  obtain T where T: "open T" "{x \<in> s. f x \<in> t} = s \<inter> T"
himmelma@33175
  3695
    using continuous_open_in_preimage[OF assms(1,3)] unfolding openin_open by auto
himmelma@33175
  3696
  thus ?thesis using open_Int[of s T, OF assms(2)] by auto
himmelma@33175
  3697
qed
himmelma@33175
  3698
himmelma@33175
  3699
lemma continuous_closed_preimage:
himmelma@33175
  3700
  assumes "continuous_on s f" "closed s" "closed t"
himmelma@33175
  3701
  shows "closed {x \<in> s. f x \<in> t}"
himmelma@33175
  3702
proof-
himmelma@33175
  3703
  obtain T where T: "closed T" "{x \<in> s. f x \<in> t} = s \<inter> T"
himmelma@33175
  3704
    using continuous_closed_in_preimage[OF assms(1,3)] unfolding closedin_closed by auto
himmelma@33175
  3705
  thus ?thesis using closed_Int[of s T, OF assms(2)] by auto
himmelma@33175
  3706
qed
himmelma@33175
  3707
himmelma@33175
  3708
lemma continuous_open_preimage_univ:
himmelma@33175
  3709
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
himmelma@33175
  3710
  shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open {x. f x \<in> s}"
himmelma@33175
  3711
  using continuous_open_preimage[of UNIV f s] open_UNIV continuous_at_imp_continuous_on by auto
himmelma@33175
  3712
himmelma@33175
  3713
lemma continuous_closed_preimage_univ:
himmelma@33175
  3714
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
himmelma@33175
  3715
  shows "(\<forall>x. continuous (at x) f) \<Longrightarrow> closed s ==> closed {x. f x \<in> s}"
himmelma@33175
  3716
  using continuous_closed_preimage[of UNIV f s] closed_UNIV continuous_at_imp_continuous_on by auto
himmelma@33175
  3717
himmelma@33175
  3718
lemma continuous_open_vimage:
himmelma@33175
  3719
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
himmelma@33175
  3720
  shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open (f -` s)"
himmelma@33175
  3721
  unfolding vimage_def by (rule continuous_open_preimage_univ)
himmelma@33175
  3722
himmelma@33175
  3723
lemma continuous_closed_vimage:
himmelma@33175
  3724
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
himmelma@33175
  3725
  shows "\<forall>x. continuous (at x) f \<Longrightarrow> closed s \<Longrightarrow> closed (f -` s)"
himmelma@33175
  3726
  unfolding vimage_def by (rule continuous_closed_preimage_univ)
himmelma@33175
  3727
himmelma@35172
  3728
lemma interior_image_subset: fixes f::"_::metric_space \<Rightarrow> _::metric_space"
himmelma@35172
  3729
  assumes "\<forall>x. continuous (at x) f" "inj f"
himmelma@35172
  3730
  shows "interior (f ` s) \<subseteq> f ` (interior s)"
himmelma@35172
  3731
  apply rule unfolding interior_def mem_Collect_eq image_iff apply safe
himmelma@35172
  3732
proof- fix x T assume as:"open T" "x \<in> T" "T \<subseteq> f ` s" 
himmelma@35172
  3733
  hence "x \<in> f ` s" by auto then guess y unfolding image_iff .. note y=this
himmelma@35172
  3734
  thus "\<exists>xa\<in>{x. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> s}. x = f xa" apply(rule_tac x=y in bexI) using assms as
himmelma@35172
  3735
    apply safe apply(rule_tac x="{x. f x \<in> T}" in exI) apply(safe,rule continuous_open_preimage_univ)
himmelma@35172
  3736
  proof- fix x assume "f x \<in> T" hence "f x \<in> f ` s" using as by auto
himmelma@35172
  3737
    thus "x \<in> s" unfolding inj_image_mem_iff[OF assms(2)] . qed auto qed
himmelma@35172
  3738
himmelma@33175
  3739
text{* Equality of continuous functions on closure and related results.          *}
himmelma@33175
  3740
himmelma@33175
  3741
lemma continuous_closed_in_preimage_constant:
himmelma@33175
  3742
 "continuous_on s f ==> closedin (subtopology euclidean s) {x \<in> s. f x = a}"
himmelma@33175
  3743
  using continuous_closed_in_preimage[of s f "{a}"] closed_sing by auto
himmelma@33175
  3744
himmelma@33175
  3745
lemma continuous_closed_preimage_constant:
himmelma@33175
  3746
 "continuous_on s f \<Longrightarrow> closed s ==> closed {x \<in> s. f x = a}"
himmelma@33175
  3747
  using continuous_closed_preimage[of s f "{a}"] closed_sing by auto
himmelma@33175
  3748
himmelma@33175
  3749
lemma continuous_constant_on_closure:
himmelma@33175
  3750
  assumes "continuous_on (closure s) f"
himmelma@33175
  3751
          "\<forall>x \<in> s. f x = a"
himmelma@33175
  3752
  shows "\<forall>x \<in> (closure s). f x = a"
himmelma@33175
  3753
    using continuous_closed_preimage_constant[of "closure s" f a]
himmelma@33175
  3754
    assms closure_minimal[of s "{x \<in> closure s. f x = a}"] closure_subset unfolding subset_eq by auto
himmelma@33175
  3755
himmelma@33175
  3756
lemma image_closure_subset:
himmelma@33175
  3757
  assumes "continuous_on (closure s) f"  "closed t"  "(f ` s) \<subseteq> t"
himmelma@33175
  3758
  shows "f ` (closure s) \<subseteq> t"
himmelma@33175
  3759
proof-
himmelma@33175
  3760
  have "s \<subseteq> {x \<in> closure s. f x \<in> t}" using assms(3) closure_subset by auto
himmelma@33175
  3761
  moreover have "closed {x \<in> closure s. f x \<in> t}"
himmelma@33175
  3762
    using continuous_closed_preimage[OF assms(1)] and assms(2) by auto
himmelma@33175
  3763
  ultimately have "closure s = {x \<in> closure s . f x \<in> t}"
himmelma@33175
  3764
    using closure_minimal[of s "{x \<in> closure s. f x \<in> t}"] by auto
himmelma@33175
  3765
  thus ?thesis by auto
himmelma@33175
  3766
qed
himmelma@33175
  3767
himmelma@33175
  3768
lemma continuous_on_closure_norm_le:
himmelma@33175
  3769
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3770
  assumes "continuous_on (closure s) f"  "\<forall>y \<in> s. norm(f y) \<le> b"  "x \<in> (closure s)"
himmelma@33175
  3771
  shows "norm(f x) \<le> b"
himmelma@33175
  3772
proof-
himmelma@33175
  3773
  have *:"f ` s \<subseteq> cball 0 b" using assms(2)[unfolded mem_cball_0[THEN sym]] by auto
himmelma@33175
  3774
  show ?thesis
himmelma@33175
  3775
    using image_closure_subset[OF assms(1) closed_cball[of 0 b] *] assms(3)
himmelma@33175
  3776
    unfolding subset_eq apply(erule_tac x="f x" in ballE) by (auto simp add: dist_norm)
himmelma@33175
  3777
qed
himmelma@33175
  3778
himmelma@33175
  3779
text{* Making a continuous function avoid some value in a neighbourhood.         *}
himmelma@33175
  3780
himmelma@33175
  3781
lemma continuous_within_avoid:
himmelma@33175
  3782
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
himmelma@33175
  3783
  assumes "continuous (at x within s) f"  "x \<in> s"  "f x \<noteq> a"
himmelma@33175
  3784
  shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e --> f y \<noteq> a"
himmelma@33175
  3785
proof-
himmelma@33175
  3786
  obtain d where "d>0" and d:"\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < dist (f x) a"
himmelma@33175
  3787
    using assms(1)[unfolded continuous_within Lim_within, THEN spec[where x="dist (f x) a"]] assms(3)[unfolded dist_nz] by auto
himmelma@33175
  3788
  { fix y assume " y\<in>s"  "dist x y < d"
himmelma@33175
  3789
    hence "f y \<noteq> a" using d[THEN bspec[where x=y]] assms(3)[unfolded dist_nz]
himmelma@33175
  3790
      apply auto unfolding dist_nz[THEN sym] by (auto simp add: dist_commute) }
himmelma@33175
  3791
  thus ?thesis using `d>0` by auto
himmelma@33175
  3792
qed
himmelma@33175
  3793
himmelma@33175
  3794
lemma continuous_at_avoid:
himmelma@33175
  3795
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
himmelma@33175
  3796
  assumes "continuous (at x) f"  "f x \<noteq> a"
himmelma@33175
  3797
  shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"
himmelma@33175
  3798
using assms using continuous_within_avoid[of x UNIV f a, unfolded within_UNIV] by auto
himmelma@33175
  3799
himmelma@33175
  3800
lemma continuous_on_avoid:
himmelma@33175
  3801
  assumes "continuous_on s f"  "x \<in> s"  "f x \<noteq> a"
himmelma@33175
  3802
  shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e \<longrightarrow> f y \<noteq> a"
himmelma@33175
  3803
using assms(1)[unfolded continuous_on_eq_continuous_within, THEN bspec[where x=x], OF assms(2)]  continuous_within_avoid[of x s f a]  assms(2,3) by auto
himmelma@33175
  3804
himmelma@33175
  3805
lemma continuous_on_open_avoid:
himmelma@33175
  3806
  assumes "continuous_on s f"  "open s"  "x \<in> s"  "f x \<noteq> a"
himmelma@33175
  3807
  shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"
himmelma@33175
  3808
using assms(1)[unfolded continuous_on_eq_continuous_at[OF assms(2)], THEN bspec[where x=x], OF assms(3)]  continuous_at_avoid[of x f a]  assms(3,4) by auto
himmelma@33175
  3809
himmelma@33175
  3810
text{* Proving a function is constant by proving open-ness of level set.         *}
himmelma@33175
  3811
himmelma@33175
  3812
lemma continuous_levelset_open_in_cases:
himmelma@33175
  3813
 "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>
himmelma@33175
  3814
        openin (subtopology euclidean s) {x \<in> s. f x = a}
himmelma@33175
  3815
        ==> (\<forall>x \<in> s. f x \<noteq> a) \<or> (\<forall>x \<in> s. f x = a)"
himmelma@33175
  3816
unfolding connected_clopen using continuous_closed_in_preimage_constant by auto
himmelma@33175
  3817
himmelma@33175
  3818
lemma continuous_levelset_open_in:
himmelma@33175
  3819
 "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>
himmelma@33175
  3820
        openin (subtopology euclidean s) {x \<in> s. f x = a} \<Longrightarrow>
himmelma@33175
  3821
        (\<exists>x \<in> s. f x = a)  ==> (\<forall>x \<in> s. f x = a)"
himmelma@33175
  3822
using continuous_levelset_open_in_cases[of s f ]
himmelma@33175
  3823
by meson
himmelma@33175
  3824
himmelma@33175
  3825
lemma continuous_levelset_open:
himmelma@33175
  3826
  assumes "connected s"  "continuous_on s f"  "open {x \<in> s. f x = a}"  "\<exists>x \<in> s.  f x = a"
himmelma@33175
  3827
  shows "\<forall>x \<in> s. f x = a"
himmelma@33175
  3828
using continuous_levelset_open_in[OF assms(1,2), of a, unfolded openin_open] using assms (3,4) by auto
himmelma@33175
  3829
himmelma@33175
  3830
text{* Some arithmetical combinations (more to prove).                           *}
himmelma@33175
  3831
himmelma@33175
  3832
lemma open_scaling[intro]:
himmelma@33175
  3833
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  3834
  assumes "c \<noteq> 0"  "open s"
himmelma@33175
  3835
  shows "open((\<lambda>x. c *\<^sub>R x) ` s)"
himmelma@33175
  3836
proof-
himmelma@33175
  3837
  { fix x assume "x \<in> s"
himmelma@33175
  3838
    then obtain e where "e>0" and e:"\<forall>x'. dist x' x < e \<longrightarrow> x' \<in> s" using assms(2)[unfolded open_dist, THEN bspec[where x=x]] by auto
himmelma@33175
  3839
    have "e * abs c > 0" using assms(1)[unfolded zero_less_abs_iff[THEN sym]] using real_mult_order[OF `e>0`] by auto
himmelma@33175
  3840
    moreover
himmelma@33175
  3841
    { fix y assume "dist y (c *\<^sub>R x) < e * \<bar>c\<bar>"
himmelma@33175
  3842
      hence "norm ((1 / c) *\<^sub>R y - x) < e" unfolding dist_norm
himmelma@33175
  3843
        using norm_scaleR[of c "(1 / c) *\<^sub>R y - x", unfolded scaleR_right_diff_distrib, unfolded scaleR_scaleR] assms(1)
himmelma@33175
  3844
          assms(1)[unfolded zero_less_abs_iff[THEN sym]] by (simp del:zero_less_abs_iff)
himmelma@33175
  3845
      hence "y \<in> op *\<^sub>R c ` s" using rev_image_eqI[of "(1 / c) *\<^sub>R y" s y "op *\<^sub>R c"]  e[THEN spec[where x="(1 / c) *\<^sub>R y"]]  assms(1) unfolding dist_norm scaleR_scaleR by auto  }
himmelma@33175
  3846
    ultimately have "\<exists>e>0. \<forall>x'. dist x' (c *\<^sub>R x) < e \<longrightarrow> x' \<in> op *\<^sub>R c ` s" apply(rule_tac x="e * abs c" in exI) by auto  }
himmelma@33175
  3847
  thus ?thesis unfolding open_dist by auto
himmelma@33175
  3848
qed
himmelma@33175
  3849
himmelma@33175
  3850
lemma minus_image_eq_vimage:
himmelma@33175
  3851
  fixes A :: "'a::ab_group_add set"
himmelma@33175
  3852
  shows "(\<lambda>x. - x) ` A = (\<lambda>x. - x) -` A"
himmelma@33175
  3853
  by (auto intro!: image_eqI [where f="\<lambda>x. - x"])
himmelma@33175
  3854
himmelma@33175
  3855
lemma open_negations:
himmelma@33175
  3856
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  3857
  shows "open s ==> open ((\<lambda> x. -x) ` s)"
himmelma@33175
  3858
  unfolding scaleR_minus1_left [symmetric]
himmelma@33175
  3859
  by (rule open_scaling, auto)
himmelma@33175
  3860
himmelma@33175
  3861
lemma open_translation:
himmelma@33175
  3862
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  3863
  assumes "open s"  shows "open((\<lambda>x. a + x) ` s)"
himmelma@33175
  3864
proof-
himmelma@33175
  3865
  { fix x have "continuous (at x) (\<lambda>x. x - a)" using continuous_sub[of "at x" "\<lambda>x. x" "\<lambda>x. a"] continuous_at_id[of x] continuous_const[of "at x" a] by auto  }
himmelma@33175
  3866
  moreover have "{x. x - a \<in> s}  = op + a ` s" apply auto unfolding image_iff apply(rule_tac x="x - a" in bexI) by auto
himmelma@33175
  3867
  ultimately show ?thesis using continuous_open_preimage_univ[of "\<lambda>x. x - a" s] using assms by auto
himmelma@33175
  3868
qed
himmelma@33175
  3869
himmelma@33175
  3870
lemma open_affinity:
himmelma@33175
  3871
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  3872
  assumes "open s"  "c \<noteq> 0"
himmelma@33175
  3873
  shows "open ((\<lambda>x. a + c *\<^sub>R x) ` s)"
himmelma@33175
  3874
proof-
himmelma@33175
  3875
  have *:"(\<lambda>x. a + c *\<^sub>R x) = (\<lambda>x. a + x) \<circ> (\<lambda>x. c *\<^sub>R x)" unfolding o_def ..
himmelma@33175
  3876
  have "op + a ` op *\<^sub>R c ` s = (op + a \<circ> op *\<^sub>R c) ` s" by auto
himmelma@33175
  3877
  thus ?thesis using assms open_translation[of "op *\<^sub>R c ` s" a] unfolding * by auto
himmelma@33175
  3878
qed
himmelma@33175
  3879
himmelma@33175
  3880
lemma interior_translation:
himmelma@33175
  3881
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  3882
  shows "interior ((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (interior s)"
himmelma@33175
  3883
proof (rule set_ext, rule)
himmelma@33175
  3884
  fix x assume "x \<in> interior (op + a ` s)"
himmelma@33175
  3885
  then obtain e where "e>0" and e:"ball x e \<subseteq> op + a ` s" unfolding mem_interior by auto
himmelma@33175
  3886
  hence "ball (x - a) e \<subseteq> s" unfolding subset_eq Ball_def mem_ball dist_norm apply auto apply(erule_tac x="a + xa" in allE) unfolding ab_group_add_class.diff_diff_eq[THEN sym] by auto
himmelma@33175
  3887
  thus "x \<in> op + a ` interior s" unfolding image_iff apply(rule_tac x="x - a" in bexI) unfolding mem_interior using `e > 0` by auto
himmelma@33175
  3888
next
himmelma@33175
  3889
  fix x assume "x \<in> op + a ` interior s"
himmelma@33175
  3890
  then obtain y e where "e>0" and e:"ball y e \<subseteq> s" and y:"x = a + y" unfolding image_iff Bex_def mem_interior by auto
himmelma@33175
  3891
  { fix z have *:"a + y - z = y + a - z" by auto
himmelma@33175
  3892
    assume "z\<in>ball x e"
himmelma@33175
  3893
    hence "z - a \<in> s" using e[unfolded subset_eq, THEN bspec[where x="z - a"]] unfolding mem_ball dist_norm y ab_group_add_class.diff_diff_eq2 * by auto
himmelma@33175
  3894
    hence "z \<in> op + a ` s" unfolding image_iff by(auto intro!: bexI[where x="z - a"])  }
himmelma@33175
  3895
  hence "ball x e \<subseteq> op + a ` s" unfolding subset_eq by auto
himmelma@33175
  3896
  thus "x \<in> interior (op + a ` s)" unfolding mem_interior using `e>0` by auto
himmelma@33175
  3897
qed
himmelma@33175
  3898
himmelma@33175
  3899
subsection {* Preservation of compactness and connectedness under continuous function.  *}
himmelma@33175
  3900
himmelma@33175
  3901
lemma compact_continuous_image:
himmelma@33175
  3902
  assumes "continuous_on s f"  "compact s"
himmelma@33175
  3903
  shows "compact(f ` s)"
himmelma@33175
  3904
proof-
himmelma@33175
  3905
  { fix x assume x:"\<forall>n::nat. x n \<in> f ` s"
himmelma@33175
  3906
    then obtain y where y:"\<forall>n. y n \<in> s \<and> x n = f (y n)" unfolding image_iff Bex_def using choice[of "\<lambda>n xa. xa \<in> s \<and> x n = f xa"] by auto
himmelma@33175
  3907
    then obtain l r where "l\<in>s" and r:"subseq r" and lr:"((y \<circ> r) ---> l) sequentially" using assms(2)[unfolded compact_def, THEN spec[where x=y]] by auto
himmelma@33175
  3908
    { fix e::real assume "e>0"
himmelma@33175
  3909
      then obtain d where "d>0" and d:"\<forall>x'\<in>s. dist x' l < d \<longrightarrow> dist (f x') (f l) < e" using assms(1)[unfolded continuous_on_def, THEN bspec[where x=l], OF `l\<in>s`] by auto
himmelma@33175
  3910
      then obtain N::nat where N:"\<forall>n\<ge>N. dist ((y \<circ> r) n) l < d" using lr[unfolded Lim_sequentially, THEN spec[where x=d]] by auto
himmelma@33175
  3911
      { fix n::nat assume "n\<ge>N" hence "dist ((x \<circ> r) n) (f l) < e" using N[THEN spec[where x=n]] d[THEN bspec[where x="y (r n)"]] y[THEN spec[where x="r n"]] by auto  }
himmelma@33175
  3912
      hence "\<exists>N. \<forall>n\<ge>N. dist ((x \<circ> r) n) (f l) < e" by auto  }
himmelma@33175
  3913
    hence "\<exists>l\<in>f ` s. \<exists>r. subseq r \<and> ((x \<circ> r) ---> l) sequentially" unfolding Lim_sequentially using r lr `l\<in>s` by auto  }
himmelma@33175
  3914
  thus ?thesis unfolding compact_def by auto
himmelma@33175
  3915
qed
himmelma@33175
  3916
himmelma@33175
  3917
lemma connected_continuous_image:
himmelma@33175
  3918
  assumes "continuous_on s f"  "connected s"
himmelma@33175
  3919
  shows "connected(f ` s)"
himmelma@33175
  3920
proof-
himmelma@33175
  3921
  { fix T assume as: "T \<noteq> {}"  "T \<noteq> f ` s"  "openin (subtopology euclidean (f ` s)) T"  "closedin (subtopology euclidean (f ` s)) T"
himmelma@33175
  3922
    have "{x \<in> s. f x \<in> T} = {} \<or> {x \<in> s. f x \<in> T} = s"
himmelma@33175
  3923
      using assms(1)[unfolded continuous_on_open, THEN spec[where x=T]]
himmelma@33175
  3924
      using assms(1)[unfolded continuous_on_closed, THEN spec[where x=T]]
himmelma@33175
  3925
      using assms(2)[unfolded connected_clopen, THEN spec[where x="{x \<in> s. f x \<in> T}"]] as(3,4) by auto
himmelma@33175
  3926
    hence False using as(1,2)
himmelma@33175
  3927
      using as(4)[unfolded closedin_def topspace_euclidean_subtopology] by auto }
himmelma@33175
  3928
  thus ?thesis unfolding connected_clopen by auto
himmelma@33175
  3929
qed
himmelma@33175
  3930
himmelma@33175
  3931
text{* Continuity implies uniform continuity on a compact domain.                *}
himmelma@33175
  3932
himmelma@33175
  3933
lemma compact_uniformly_continuous:
himmelma@33175
  3934
  assumes "continuous_on s f"  "compact s"
himmelma@33175
  3935
  shows "uniformly_continuous_on s f"
himmelma@33175
  3936
proof-
himmelma@33175
  3937
    { fix x assume x:"x\<in>s"
himmelma@33175
  3938
      hence "\<forall>xa. \<exists>y. 0 < xa \<longrightarrow> (y > 0 \<and> (\<forall>x'\<in>s. dist x' x < y \<longrightarrow> dist (f x') (f x) < xa))" using assms(1)[unfolded continuous_on_def, THEN bspec[where x=x]] by auto
himmelma@33175
  3939
      hence "\<exists>fa. \<forall>xa>0. \<forall>x'\<in>s. fa xa > 0 \<and> (dist x' x < fa xa \<longrightarrow> dist (f x') (f x) < xa)" using choice[of "\<lambda>e d. e>0 \<longrightarrow> d>0 \<and>(\<forall>x'\<in>s. (dist x' x < d \<longrightarrow> dist (f x') (f x) < e))"] by auto  }
himmelma@33175
  3940
    then have "\<forall>x\<in>s. \<exists>y. \<forall>xa. 0 < xa \<longrightarrow> (\<forall>x'\<in>s. y xa > 0 \<and> (dist x' x < y xa \<longrightarrow> dist (f x') (f x) < xa))" by auto
himmelma@33175
  3941
    then obtain d where d:"\<forall>e>0. \<forall>x\<in>s. \<forall>x'\<in>s. d x e > 0 \<and> (dist x' x < d x e \<longrightarrow> dist (f x') (f x) < e)"
himmelma@33175
  3942
      using bchoice[of s "\<lambda>x fa. \<forall>xa>0. \<forall>x'\<in>s. fa xa > 0 \<and> (dist x' x < fa xa \<longrightarrow> dist (f x') (f x) < xa)"] by blast
himmelma@33175
  3943
himmelma@33175
  3944
  { fix e::real assume "e>0"
himmelma@33175
  3945
himmelma@33175
  3946
    { fix x assume "x\<in>s" hence "x \<in> ball x (d x (e / 2))" unfolding centre_in_ball using d[THEN spec[where x="e/2"]] using `e>0` by auto  }
himmelma@33175
  3947
    hence "s \<subseteq> \<Union>{ball x (d x (e / 2)) |x. x \<in> s}" unfolding subset_eq by auto
himmelma@33175
  3948
    moreover
himmelma@33175
  3949
    { fix b assume "b\<in>{ball x (d x (e / 2)) |x. x \<in> s}" hence "open b" by auto  }
himmelma@33175
  3950
    ultimately obtain ea where "ea>0" and ea:"\<forall>x\<in>s. \<exists>b\<in>{ball x (d x (e / 2)) |x. x \<in> s}. ball x ea \<subseteq> b" using heine_borel_lemma[OF assms(2), of "{ball x (d x (e / 2)) | x. x\<in>s }"] by auto
himmelma@33175
  3951
himmelma@33175
  3952
    { fix x y assume "x\<in>s" "y\<in>s" and as:"dist y x < ea"
himmelma@33175
  3953
      obtain z where "z\<in>s" and z:"ball x ea \<subseteq> ball z (d z (e / 2))" using ea[THEN bspec[where x=x]] and `x\<in>s` by auto
himmelma@33175
  3954
      hence "x\<in>ball z (d z (e / 2))" using `ea>0` unfolding subset_eq by auto
himmelma@33175
  3955
      hence "dist (f z) (f x) < e / 2" using d[THEN spec[where x="e/2"]] and `e>0` and `x\<in>s` and `z\<in>s`
himmelma@33175
  3956
        by (auto  simp add: dist_commute)
himmelma@33175
  3957
      moreover have "y\<in>ball z (d z (e / 2))" using as and `ea>0` and z[unfolded subset_eq]
himmelma@33175
  3958
        by (auto simp add: dist_commute)
himmelma@33175
  3959
      hence "dist (f z) (f y) < e / 2" using d[THEN spec[where x="e/2"]] and `e>0` and `y\<in>s` and `z\<in>s`
himmelma@33175
  3960
        by (auto  simp add: dist_commute)
himmelma@33175
  3961
      ultimately have "dist (f y) (f x) < e" using dist_triangle_half_r[of "f z" "f x" e "f y"]
himmelma@33175
  3962
        by (auto simp add: dist_commute)  }
himmelma@33175
  3963
    then have "\<exists>d>0. \<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e" using `ea>0` by auto  }
himmelma@33175
  3964
  thus ?thesis unfolding uniformly_continuous_on_def by auto
himmelma@33175
  3965
qed
himmelma@33175
  3966
himmelma@33175
  3967
text{* Continuity of inverse function on compact domain. *}
himmelma@33175
  3968
himmelma@33175
  3969
lemma continuous_on_inverse:
himmelma@33175
  3970
  fixes f :: "'a::heine_borel \<Rightarrow> 'b::heine_borel"
himmelma@33175
  3971
    (* TODO: can this be generalized more? *)
himmelma@33175
  3972
  assumes "continuous_on s f"  "compact s"  "\<forall>x \<in> s. g (f x) = x"
himmelma@33175
  3973
  shows "continuous_on (f ` s) g"
himmelma@33175
  3974
proof-
himmelma@33175
  3975
  have *:"g ` f ` s = s" using assms(3) by (auto simp add: image_iff)
himmelma@33175
  3976
  { fix t assume t:"closedin (subtopology euclidean (g ` f ` s)) t"
himmelma@33175
  3977
    then obtain T where T: "closed T" "t = s \<inter> T" unfolding closedin_closed unfolding * by auto
himmelma@33175
  3978
    have "continuous_on (s \<inter> T) f" using continuous_on_subset[OF assms(1), of "s \<inter> t"]
himmelma@33175
  3979
      unfolding T(2) and Int_left_absorb by auto
himmelma@33175
  3980
    moreover have "compact (s \<inter> T)"
himmelma@33175
  3981
      using assms(2) unfolding compact_eq_bounded_closed
himmelma@33175
  3982
      using bounded_subset[of s "s \<inter> T"] and T(1) by auto
himmelma@33175
  3983
    ultimately have "closed (f ` t)" using T(1) unfolding T(2)
himmelma@33175
  3984
      using compact_continuous_image [of "s \<inter> T" f] unfolding compact_eq_bounded_closed by auto
himmelma@33175
  3985
    moreover have "{x \<in> f ` s. g x \<in> t} = f ` s \<inter> f ` t" using assms(3) unfolding T(2) by auto
himmelma@33175
  3986
    ultimately have "closedin (subtopology euclidean (f ` s)) {x \<in> f ` s. g x \<in> t}"
himmelma@33175
  3987
      unfolding closedin_closed by auto  }
himmelma@33175
  3988
  thus ?thesis unfolding continuous_on_closed by auto
himmelma@33175
  3989
qed
himmelma@33175
  3990
himmelma@33175
  3991
subsection{* A uniformly convergent limit of continuous functions is continuous.       *}
himmelma@33175
  3992
himmelma@33175
  3993
lemma norm_triangle_lt:
himmelma@33175
  3994
  fixes x y :: "'a::real_normed_vector"
himmelma@33175
  3995
  shows "norm x + norm y < e \<Longrightarrow> norm (x + y) < e"
himmelma@33175
  3996
by (rule le_less_trans [OF norm_triangle_ineq])
himmelma@33175
  3997
himmelma@33175
  3998
lemma continuous_uniform_limit:
himmelma@33175
  3999
  fixes f :: "'a \<Rightarrow> 'b::metric_space \<Rightarrow> 'c::real_normed_vector"
himmelma@33175
  4000
  assumes "\<not> (trivial_limit net)"  "eventually (\<lambda>n. continuous_on s (f n)) net"
himmelma@33175
  4001
  "\<forall>e>0. eventually (\<lambda>n. \<forall>x \<in> s. norm(f n x - g x) < e) net"
himmelma@33175
  4002
  shows "continuous_on s g"
himmelma@33175
  4003
proof-
himmelma@33175
  4004
  { fix x and e::real assume "x\<in>s" "e>0"
himmelma@33175
  4005
    have "eventually (\<lambda>n. \<forall>x\<in>s. norm (f n x - g x) < e / 3) net" using `e>0` assms(3)[THEN spec[where x="e/3"]] by auto
himmelma@33175
  4006
    then obtain n where n:"\<forall>xa\<in>s. norm (f n xa - g xa) < e / 3"  "continuous_on s (f n)"
himmelma@33175
  4007
      using eventually_and[of "(\<lambda>n. \<forall>x\<in>s. norm (f n x - g x) < e / 3)" "(\<lambda>n. continuous_on s (f n))" net] assms(1,2) eventually_happens by blast
himmelma@33175
  4008
    have "e / 3 > 0" using `e>0` by auto
himmelma@33175
  4009
    then obtain d where "d>0" and d:"\<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f n x') (f n x) < e / 3"
himmelma@33175
  4010
      using n(2)[unfolded continuous_on_def, THEN bspec[where x=x], OF `x\<in>s`, THEN spec[where x="e/3"]] by blast
himmelma@33175
  4011
    { fix y assume "y\<in>s" "dist y x < d"
himmelma@33175
  4012
      hence "dist (f n y) (f n x) < e / 3" using d[THEN bspec[where x=y]] by auto
himmelma@33175
  4013
      hence "norm (f n y - g x) < 2 * e / 3" using norm_triangle_lt[of "f n y - f n x" "f n x - g x" "2*e/3"]
himmelma@33175
  4014
        using n(1)[THEN bspec[where x=x], OF `x\<in>s`] unfolding dist_norm unfolding ab_group_add_class.ab_diff_minus by auto
himmelma@33175
  4015
      hence "dist (g y) (g x) < e" unfolding dist_norm using n(1)[THEN bspec[where x=y], OF `y\<in>s`]
himmelma@33175
  4016
        unfolding norm_minus_cancel[of "f n y - g y", THEN sym] using norm_triangle_lt[of "f n y - g x" "g y - f n y" e] by (auto simp add: uminus_add_conv_diff)  }
himmelma@33175
  4017
    hence "\<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (g x') (g x) < e" using `d>0` by auto  }
himmelma@33175
  4018
  thus ?thesis unfolding continuous_on_def by auto
himmelma@33175
  4019
qed
himmelma@33175
  4020
himmelma@33175
  4021
subsection{* Topological properties of linear functions.                               *}
himmelma@33175
  4022
himmelma@33175
  4023
lemma linear_lim_0:
himmelma@33175
  4024
  assumes "bounded_linear f" shows "(f ---> 0) (at (0))"
himmelma@33175
  4025
proof-
himmelma@33175
  4026
  interpret f: bounded_linear f by fact
himmelma@33175
  4027
  have "(f ---> f 0) (at 0)"
himmelma@33175
  4028
    using tendsto_ident_at by (rule f.tendsto)
himmelma@33175
  4029
  thus ?thesis unfolding f.zero .
himmelma@33175
  4030
qed
himmelma@33175
  4031
himmelma@33175
  4032
lemma linear_continuous_at:
himmelma@33175
  4033
  assumes "bounded_linear f"  shows "continuous (at a) f"
himmelma@33175
  4034
  unfolding continuous_at using assms
himmelma@33175
  4035
  apply (rule bounded_linear.tendsto)
himmelma@33175
  4036
  apply (rule tendsto_ident_at)
himmelma@33175
  4037
  done
himmelma@33175
  4038
himmelma@33175
  4039
lemma linear_continuous_within:
himmelma@33175
  4040
  shows "bounded_linear f ==> continuous (at x within s) f"
himmelma@33175
  4041
  using continuous_at_imp_continuous_within[of x f s] using linear_continuous_at[of f] by auto
himmelma@33175
  4042
himmelma@33175
  4043
lemma linear_continuous_on:
himmelma@33175
  4044
  shows "bounded_linear f ==> continuous_on s f"
himmelma@33175
  4045
  using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto
himmelma@33175
  4046
hoelzl@33711
  4047
lemma continuous_on_vec1:"continuous_on A (vec1::real\<Rightarrow>real^1)"
hoelzl@33711
  4048
  by(rule linear_continuous_on[OF bounded_linear_vec1])
hoelzl@33711
  4049
himmelma@33175
  4050
text{* Also bilinear functions, in composition form.                             *}
himmelma@33175
  4051
himmelma@33175
  4052
lemma bilinear_continuous_at_compose:
himmelma@33175
  4053
  shows "continuous (at x) f \<Longrightarrow> continuous (at x) g \<Longrightarrow> bounded_bilinear h
himmelma@33175
  4054
        ==> continuous (at x) (\<lambda>x. h (f x) (g x))"
himmelma@33175
  4055
  unfolding continuous_at using Lim_bilinear[of f "f x" "(at x)" g "g x" h] by auto
himmelma@33175
  4056
himmelma@33175
  4057
lemma bilinear_continuous_within_compose:
himmelma@33175
  4058
  shows "continuous (at x within s) f \<Longrightarrow> continuous (at x within s) g \<Longrightarrow> bounded_bilinear h
himmelma@33175
  4059
        ==> continuous (at x within s) (\<lambda>x. h (f x) (g x))"
himmelma@33175
  4060
  unfolding continuous_within using Lim_bilinear[of f "f x"] by auto
himmelma@33175
  4061
himmelma@33175
  4062
lemma bilinear_continuous_on_compose:
himmelma@33175
  4063
  shows "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> bounded_bilinear h
himmelma@33175
  4064
             ==> continuous_on s (\<lambda>x. h (f x) (g x))"
himmelma@33175
  4065
  unfolding continuous_on_eq_continuous_within apply auto apply(erule_tac x=x in ballE) apply auto apply(erule_tac x=x in ballE) apply auto
himmelma@33175
  4066
  using bilinear_continuous_within_compose[of _ s f g h] by auto
himmelma@33175
  4067
himmelma@33175
  4068
subsection{* Topological stuff lifted from and dropped to R                            *}
himmelma@33175
  4069
himmelma@33175
  4070
himmelma@33175
  4071
lemma open_real:
himmelma@33175
  4072
  fixes s :: "real set" shows
himmelma@33175
  4073
 "open s \<longleftrightarrow>
himmelma@33175
  4074
        (\<forall>x \<in> s. \<exists>e>0. \<forall>x'. abs(x' - x) < e --> x' \<in> s)" (is "?lhs = ?rhs")
himmelma@33175
  4075
  unfolding open_dist dist_norm by simp
himmelma@33175
  4076
himmelma@33175
  4077
lemma islimpt_approachable_real:
himmelma@33175
  4078
  fixes s :: "real set"
himmelma@33175
  4079
  shows "x islimpt s \<longleftrightarrow> (\<forall>e>0.  \<exists>x'\<in> s. x' \<noteq> x \<and> abs(x' - x) < e)"
himmelma@33175
  4080
  unfolding islimpt_approachable dist_norm by simp
himmelma@33175
  4081
himmelma@33175
  4082
lemma closed_real:
himmelma@33175
  4083
  fixes s :: "real set"
himmelma@33175
  4084
  shows "closed s \<longleftrightarrow>
himmelma@33175
  4085
        (\<forall>x. (\<forall>e>0.  \<exists>x' \<in> s. x' \<noteq> x \<and> abs(x' - x) < e)
himmelma@33175
  4086
            --> x \<in> s)"
himmelma@33175
  4087
  unfolding closed_limpt islimpt_approachable dist_norm by simp
himmelma@33175
  4088
himmelma@33175
  4089
lemma continuous_at_real_range:
himmelma@33175
  4090
  fixes f :: "'a::real_normed_vector \<Rightarrow> real"
himmelma@33175
  4091
  shows "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.
himmelma@33175
  4092
        \<forall>x'. norm(x' - x) < d --> abs(f x' - f x) < e)"
himmelma@33175
  4093
  unfolding continuous_at unfolding Lim_at
himmelma@33175
  4094
  unfolding dist_nz[THEN sym] unfolding dist_norm apply auto
himmelma@33175
  4095
  apply(erule_tac x=e in allE) apply auto apply (rule_tac x=d in exI) apply auto apply (erule_tac x=x' in allE) apply auto
himmelma@33175
  4096
  apply(erule_tac x=e in allE) by auto
himmelma@33175
  4097
himmelma@33175
  4098
lemma continuous_on_real_range:
himmelma@33175
  4099
  fixes f :: "'a::real_normed_vector \<Rightarrow> real"
himmelma@33175
  4100
  shows "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. \<forall>e>0. \<exists>d>0. (\<forall>x' \<in> s. norm(x' - x) < d --> abs(f x' - f x) < e))"
himmelma@33175
  4101
  unfolding continuous_on_def dist_norm by simp
himmelma@33175
  4102
himmelma@33175
  4103
lemma continuous_at_norm: "continuous (at x) norm"
himmelma@33175
  4104
  unfolding continuous_at by (intro tendsto_intros)
himmelma@33175
  4105
himmelma@33175
  4106
lemma continuous_on_norm: "continuous_on s norm"
himmelma@33175
  4107
unfolding continuous_on by (intro ballI tendsto_intros)
himmelma@33175
  4108
himmelma@33175
  4109
lemma continuous_at_component: "continuous (at a) (\<lambda>x. x $ i)"
himmelma@33175
  4110
unfolding continuous_at by (intro tendsto_intros)
himmelma@33175
  4111
himmelma@33175
  4112
lemma continuous_on_component: "continuous_on s (\<lambda>x. x $ i)"
himmelma@33175
  4113
unfolding continuous_on by (intro ballI tendsto_intros)
himmelma@33175
  4114
himmelma@33175
  4115
lemma continuous_at_infnorm: "continuous (at x) infnorm"
himmelma@33175
  4116
  unfolding continuous_at Lim_at o_def unfolding dist_norm
himmelma@33175
  4117
  apply auto apply (rule_tac x=e in exI) apply auto
himmelma@33175
  4118
  using order_trans[OF real_abs_sub_infnorm infnorm_le_norm, of _ x] by (metis xt1(7))
himmelma@33175
  4119
himmelma@33175
  4120
text{* Hence some handy theorems on distance, diameter etc. of/from a set.       *}
himmelma@33175
  4121
himmelma@33175
  4122
lemma compact_attains_sup:
himmelma@33175
  4123
  fixes s :: "real set"
himmelma@33175
  4124
  assumes "compact s"  "s \<noteq> {}"
himmelma@33175
  4125
  shows "\<exists>x \<in> s. \<forall>y \<in> s. y \<le> x"
himmelma@33175
  4126
proof-
himmelma@33175
  4127
  from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto
paulson@33270
  4128
  { fix e::real assume as: "\<forall>x\<in>s. x \<le> Sup s" "Sup s \<notin> s"  "0 < e" "\<forall>x'\<in>s. x' = Sup s \<or> \<not> Sup s - x' < e"
paulson@33270
  4129
    have "isLub UNIV s (Sup s)" using Sup[OF assms(2)] unfolding setle_def using as(1) by auto
paulson@33270
  4130
    moreover have "isUb UNIV s (Sup s - e)" unfolding isUb_def unfolding setle_def using as(4,2) by auto
paulson@33270
  4131
    ultimately have False using isLub_le_isUb[of UNIV s "Sup s" "Sup s - e"] using `e>0` by auto  }
paulson@33270
  4132
  thus ?thesis using bounded_has_Sup(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="Sup s"]]
paulson@33270
  4133
    apply(rule_tac x="Sup s" in bexI) by auto
paulson@33270
  4134
qed
paulson@33270
  4135
paulson@33270
  4136
lemma Inf:
paulson@33270
  4137
  fixes S :: "real set"
paulson@33270
  4138
  shows "S \<noteq> {} ==> (\<exists>b. b <=* S) ==> isGlb UNIV S (Inf S)"
paulson@33270
  4139
by (auto simp add: isLb_def setle_def setge_def isGlb_def greatestP_def) 
himmelma@33175
  4140
himmelma@33175
  4141
lemma compact_attains_inf:
himmelma@33175
  4142
  fixes s :: "real set"
himmelma@33175
  4143
  assumes "compact s" "s \<noteq> {}"  shows "\<exists>x \<in> s. \<forall>y \<in> s. x \<le> y"
himmelma@33175
  4144
proof-
himmelma@33175
  4145
  from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto
paulson@33270
  4146
  { fix e::real assume as: "\<forall>x\<in>s. x \<ge> Inf s"  "Inf s \<notin> s"  "0 < e"
paulson@33270
  4147
      "\<forall>x'\<in>s. x' = Inf s \<or> \<not> abs (x' - Inf s) < e"
paulson@33270
  4148
    have "isGlb UNIV s (Inf s)" using Inf[OF assms(2)] unfolding setge_def using as(1) by auto
himmelma@33175
  4149
    moreover
himmelma@33175
  4150
    { fix x assume "x \<in> s"
paulson@33270
  4151
      hence *:"abs (x - Inf s) = x - Inf s" using as(1)[THEN bspec[where x=x]] by auto
paulson@33270
  4152
      have "Inf s + e \<le> x" using as(4)[THEN bspec[where x=x]] using as(2) `x\<in>s` unfolding * by auto }
paulson@33270
  4153
    hence "isLb UNIV s (Inf s + e)" unfolding isLb_def and setge_def by auto
paulson@33270
  4154
    ultimately have False using isGlb_le_isLb[of UNIV s "Inf s" "Inf s + e"] using `e>0` by auto  }
paulson@33270
  4155
  thus ?thesis using bounded_has_Inf(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="Inf s"]]
paulson@33270
  4156
    apply(rule_tac x="Inf s" in bexI) by auto
himmelma@33175
  4157
qed
himmelma@33175
  4158
himmelma@33175
  4159
lemma continuous_attains_sup:
himmelma@33175
  4160
  fixes f :: "'a::metric_space \<Rightarrow> real"
himmelma@33175
  4161
  shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f
himmelma@33175
  4162
        ==> (\<exists>x \<in> s. \<forall>y \<in> s.  f y \<le> f x)"
himmelma@33175
  4163
  using compact_attains_sup[of "f ` s"]
himmelma@33175
  4164
  using compact_continuous_image[of s f] by auto
himmelma@33175
  4165
himmelma@33175
  4166
lemma continuous_attains_inf:
himmelma@33175
  4167
  fixes f :: "'a::metric_space \<Rightarrow> real"
himmelma@33175
  4168
  shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f
himmelma@33175
  4169
        \<Longrightarrow> (\<exists>x \<in> s. \<forall>y \<in> s. f x \<le> f y)"
himmelma@33175
  4170
  using compact_attains_inf[of "f ` s"]
himmelma@33175
  4171
  using compact_continuous_image[of s f] by auto
himmelma@33175
  4172
himmelma@33175
  4173
lemma distance_attains_sup:
himmelma@33175
  4174
  assumes "compact s" "s \<noteq> {}"
himmelma@33175
  4175
  shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a y \<le> dist a x"
himmelma@33175
  4176
proof (rule continuous_attains_sup [OF assms])
himmelma@33175
  4177
  { fix x assume "x\<in>s"
himmelma@33175
  4178
    have "(dist a ---> dist a x) (at x within s)"
himmelma@33175
  4179
      by (intro tendsto_dist tendsto_const Lim_at_within Lim_ident_at)
himmelma@33175
  4180
  }
himmelma@33175
  4181
  thus "continuous_on s (dist a)"
himmelma@33175
  4182
    unfolding continuous_on ..
himmelma@33175
  4183
qed
himmelma@33175
  4184
himmelma@33175
  4185
text{* For *minimal* distance, we only need closure, not compactness.            *}
himmelma@33175
  4186
himmelma@33175
  4187
lemma distance_attains_inf:
himmelma@33175
  4188
  fixes a :: "'a::heine_borel"
himmelma@33175
  4189
  assumes "closed s"  "s \<noteq> {}"
himmelma@33175
  4190
  shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a x \<le> dist a y"
himmelma@33175
  4191
proof-
himmelma@33175
  4192
  from assms(2) obtain b where "b\<in>s" by auto
himmelma@33175
  4193
  let ?B = "cball a (dist b a) \<inter> s"
himmelma@33175
  4194
  have "b \<in> ?B" using `b\<in>s` by (simp add: dist_commute)
himmelma@33175
  4195
  hence "?B \<noteq> {}" by auto
himmelma@33175
  4196
  moreover
himmelma@33175
  4197
  { fix x assume "x\<in>?B"
himmelma@33175
  4198
    fix e::real assume "e>0"
himmelma@33175
  4199
    { fix x' assume "x'\<in>?B" and as:"dist x' x < e"
himmelma@33175
  4200
      from as have "\<bar>dist a x' - dist a x\<bar> < e"
himmelma@33175
  4201
        unfolding abs_less_iff minus_diff_eq
himmelma@33175
  4202
        using dist_triangle2 [of a x' x]
himmelma@33175
  4203
        using dist_triangle [of a x x']
himmelma@33175
  4204
        by arith
himmelma@33175
  4205
    }
himmelma@33175
  4206
    hence "\<exists>d>0. \<forall>x'\<in>?B. dist x' x < d \<longrightarrow> \<bar>dist a x' - dist a x\<bar> < e"
himmelma@33175
  4207
      using `e>0` by auto
himmelma@33175
  4208
  }
himmelma@33175
  4209
  hence "continuous_on (cball a (dist b a) \<inter> s) (dist a)"
himmelma@33175
  4210
    unfolding continuous_on Lim_within dist_norm real_norm_def
himmelma@33175
  4211
    by fast
himmelma@33175
  4212
  moreover have "compact ?B"
himmelma@33175
  4213
    using compact_cball[of a "dist b a"]
himmelma@33175
  4214
    unfolding compact_eq_bounded_closed
himmelma@33175
  4215
    using bounded_Int and closed_Int and assms(1) by auto
himmelma@33175
  4216
  ultimately obtain x where "x\<in>cball a (dist b a) \<inter> s" "\<forall>y\<in>cball a (dist b a) \<inter> s. dist a x \<le> dist a y"
himmelma@33175
  4217
    using continuous_attains_inf[of ?B "dist a"] by fastsimp
himmelma@33175
  4218
  thus ?thesis by fastsimp
himmelma@33175
  4219
qed
himmelma@33175
  4220
himmelma@33175
  4221
subsection{* We can now extend limit compositions to consider the scalar multiplier.   *}
himmelma@33175
  4222
himmelma@33175
  4223
lemma Lim_mul:
himmelma@33175
  4224
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  4225
  assumes "(c ---> d) net"  "(f ---> l) net"
himmelma@33175
  4226
  shows "((\<lambda>x. c(x) *\<^sub>R f x) ---> (d *\<^sub>R l)) net"
himmelma@33175
  4227
  using assms by (rule scaleR.tendsto)
himmelma@33175
  4228
himmelma@33175
  4229
lemma Lim_vmul:
himmelma@33175
  4230
  fixes c :: "'a \<Rightarrow> real" and v :: "'b::real_normed_vector"
himmelma@33175
  4231
  shows "(c ---> d) net ==> ((\<lambda>x. c(x) *\<^sub>R v) ---> d *\<^sub>R v) net"
himmelma@33175
  4232
  by (intro tendsto_intros)
himmelma@33175
  4233
hoelzl@33711
  4234
lemmas Lim_intros = Lim_add Lim_const Lim_sub Lim_cmul Lim_vmul Lim_within_id
hoelzl@33711
  4235
himmelma@33175
  4236
lemma continuous_vmul:
himmelma@33175
  4237
  fixes c :: "'a::metric_space \<Rightarrow> real" and v :: "'b::real_normed_vector"
himmelma@33175
  4238
  shows "continuous net c ==> continuous net (\<lambda>x. c(x) *\<^sub>R v)"
himmelma@33175
  4239
  unfolding continuous_def using Lim_vmul[of c] by auto
himmelma@33175
  4240
himmelma@33175
  4241
lemma continuous_mul:
himmelma@33175
  4242
  fixes c :: "'a::metric_space \<Rightarrow> real"
himmelma@33175
  4243
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  4244
  shows "continuous net c \<Longrightarrow> continuous net f
himmelma@33175
  4245
             ==> continuous net (\<lambda>x. c(x) *\<^sub>R f x) "
himmelma@33175
  4246
  unfolding continuous_def by (intro tendsto_intros)
himmelma@33175
  4247
hoelzl@34951
  4248
lemmas continuous_intros = continuous_add continuous_vmul continuous_cmul continuous_const continuous_sub continuous_at_id continuous_within_id continuous_mul
hoelzl@34951
  4249
himmelma@33175
  4250
lemma continuous_on_vmul:
himmelma@33175
  4251
  fixes c :: "'a::metric_space \<Rightarrow> real" and v :: "'b::real_normed_vector"
himmelma@33175
  4252
  shows "continuous_on s c ==> continuous_on s (\<lambda>x. c(x) *\<^sub>R v)"
himmelma@33175
  4253
  unfolding continuous_on_eq_continuous_within using continuous_vmul[of _ c] by auto
himmelma@33175
  4254
himmelma@33175
  4255
lemma continuous_on_mul:
himmelma@33175
  4256
  fixes c :: "'a::metric_space \<Rightarrow> real"
himmelma@33175
  4257
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  4258
  shows "continuous_on s c \<Longrightarrow> continuous_on s f
himmelma@33175
  4259
             ==> continuous_on s (\<lambda>x. c(x) *\<^sub>R f x)"
himmelma@33175
  4260
  unfolding continuous_on_eq_continuous_within using continuous_mul[of _ c] by auto
himmelma@33175
  4261
hoelzl@34951
  4262
lemmas continuous_on_intros = continuous_on_add continuous_on_const continuous_on_id continuous_on_compose continuous_on_cmul continuous_on_neg continuous_on_sub
hoelzl@34951
  4263
  uniformly_continuous_on_add uniformly_continuous_on_const uniformly_continuous_on_id uniformly_continuous_on_compose uniformly_continuous_on_cmul uniformly_continuous_on_neg uniformly_continuous_on_sub
hoelzl@34951
  4264
  continuous_on_mul continuous_on_vmul
hoelzl@34951
  4265
himmelma@33175
  4266
text{* And so we have continuity of inverse.                                     *}
himmelma@33175
  4267
himmelma@33175
  4268
lemma Lim_inv:
himmelma@33175
  4269
  fixes f :: "'a \<Rightarrow> real"
himmelma@33175
  4270
  assumes "(f ---> l) (net::'a net)"  "l \<noteq> 0"
himmelma@33175
  4271
  shows "((inverse o f) ---> inverse l) net"
himmelma@33175
  4272
  unfolding o_def using assms by (rule tendsto_inverse)
himmelma@33175
  4273
himmelma@33175
  4274
lemma continuous_inv:
himmelma@33175
  4275
  fixes f :: "'a::metric_space \<Rightarrow> real"
himmelma@33175
  4276
  shows "continuous net f \<Longrightarrow> f(netlimit net) \<noteq> 0
himmelma@33175
  4277
           ==> continuous net (inverse o f)"
himmelma@33175
  4278
  unfolding continuous_def using Lim_inv by auto
himmelma@33175
  4279
himmelma@33175
  4280
lemma continuous_at_within_inv:
himmelma@33175
  4281
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_field"
himmelma@33175
  4282
  assumes "continuous (at a within s) f" "f a \<noteq> 0"
himmelma@33175
  4283
  shows "continuous (at a within s) (inverse o f)"
himmelma@33175
  4284
  using assms unfolding continuous_within o_def
himmelma@33175
  4285
  by (intro tendsto_intros)
himmelma@33175
  4286
himmelma@33175
  4287
lemma continuous_at_inv:
himmelma@33175
  4288
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_field"
himmelma@33175
  4289
  shows "continuous (at a) f \<Longrightarrow> f a \<noteq> 0
himmelma@33175
  4290
         ==> continuous (at a) (inverse o f) "
himmelma@33175
  4291
  using within_UNIV[THEN sym, of "at a"] using continuous_at_within_inv[of a UNIV] by auto
himmelma@33175
  4292
himmelma@33175
  4293
subsection{* Preservation properties for pasted sets.                                  *}
himmelma@33175
  4294
himmelma@33175
  4295
lemma bounded_pastecart:
himmelma@33175
  4296
  fixes s :: "('a::real_normed_vector ^ _) set" (* FIXME: generalize to metric_space *)
himmelma@33175
  4297
  assumes "bounded s" "bounded t"
himmelma@33175
  4298
  shows "bounded { pastecart x y | x y . (x \<in> s \<and> y \<in> t)}"
himmelma@33175
  4299
proof-
himmelma@33175
  4300
  obtain a b where ab:"\<forall>x\<in>s. norm x \<le> a" "\<forall>x\<in>t. norm x \<le> b" using assms[unfolded bounded_iff] by auto
himmelma@33175
  4301
  { fix x y assume "x\<in>s" "y\<in>t"
himmelma@33175
  4302
    hence "norm x \<le> a" "norm y \<le> b" using ab by auto
himmelma@33175
  4303
    hence "norm (pastecart x y) \<le> a + b" using norm_pastecart[of x y] by auto }
himmelma@33175
  4304
  thus ?thesis unfolding bounded_iff by auto
himmelma@33175
  4305
qed
himmelma@33175
  4306
himmelma@33175
  4307
lemma bounded_Times:
himmelma@33175
  4308
  assumes "bounded s" "bounded t" shows "bounded (s \<times> t)"
himmelma@33175
  4309
proof-
himmelma@33175
  4310
  obtain x y a b where "\<forall>z\<in>s. dist x z \<le> a" "\<forall>z\<in>t. dist y z \<le> b"
himmelma@33175
  4311
    using assms [unfolded bounded_def] by auto
himmelma@33175
  4312
  then have "\<forall>z\<in>s \<times> t. dist (x, y) z \<le> sqrt (a\<twosuperior> + b\<twosuperior>)"
himmelma@33175
  4313
    by (auto simp add: dist_Pair_Pair real_sqrt_le_mono add_mono power_mono)
himmelma@33175
  4314
  thus ?thesis unfolding bounded_any_center [where a="(x, y)"] by auto
himmelma@33175
  4315
qed
himmelma@33175
  4316
himmelma@33175
  4317
lemma closed_pastecart:
hoelzl@34291
  4318
  fixes s :: "(real ^ 'a) set" (* FIXME: generalize *)
himmelma@33175
  4319
  assumes "closed s"  "closed t"
himmelma@33175
  4320
  shows "closed {pastecart x y | x y . x \<in> s \<and> y \<in> t}"
himmelma@33175
  4321
proof-
himmelma@33175
  4322
  { fix x l assume as:"\<forall>n::nat. x n \<in> {pastecart x y |x y. x \<in> s \<and> y \<in> t}"  "(x ---> l) sequentially"
himmelma@33175
  4323
    { fix n::nat have "fstcart (x n) \<in> s" "sndcart (x n) \<in> t" using as(1)[THEN spec[where x=n]] by auto } note * = this
himmelma@33175
  4324
    moreover
himmelma@33175
  4325
    { fix e::real assume "e>0"
himmelma@33175
  4326
      then obtain N::nat where N:"\<forall>n\<ge>N. dist (x n) l < e" using as(2)[unfolded Lim_sequentially, THEN spec[where x=e]] by auto
himmelma@33175
  4327
      { fix n::nat assume "n\<ge>N"
himmelma@33175
  4328
        hence "dist (fstcart (x n)) (fstcart l) < e" "dist (sndcart (x n)) (sndcart l) < e"
himmelma@33175
  4329
          using N[THEN spec[where x=n]] dist_fstcart[of "x n" l] dist_sndcart[of "x n" l] by auto   }
himmelma@33175
  4330
      hence "\<exists>N. \<forall>n\<ge>N. dist (fstcart (x n)) (fstcart l) < e" "\<exists>N. \<forall>n\<ge>N. dist (sndcart (x n)) (sndcart l) < e" by auto  }
himmelma@33175
  4331
    ultimately have "fstcart l \<in> s" "sndcart l \<in> t"
himmelma@33175
  4332
      using assms(1)[unfolded closed_sequential_limits, THEN spec[where x="\<lambda>n. fstcart (x n)"], THEN spec[where x="fstcart l"]]
himmelma@33175
  4333
      using assms(2)[unfolded closed_sequential_limits, THEN spec[where x="\<lambda>n. sndcart (x n)"], THEN spec[where x="sndcart l"]]
himmelma@33175
  4334
      unfolding Lim_sequentially by auto
hoelzl@34951
  4335
    hence "l \<in> {pastecart x y |x y. x \<in> s \<and> y \<in> t}" apply- unfolding mem_Collect_eq apply(rule_tac x="fstcart l" in exI,rule_tac x="sndcart l" in exI) by auto }
himmelma@33175
  4336
  thus ?thesis unfolding closed_sequential_limits by auto
himmelma@33175
  4337
qed
himmelma@33175
  4338
himmelma@33175
  4339
lemma compact_pastecart:
himmelma@33175
  4340
  fixes s t :: "(real ^ _) set"
himmelma@33175
  4341
  shows "compact s \<Longrightarrow> compact t ==> compact {pastecart x y | x y . x \<in> s \<and> y \<in> t}"
himmelma@33175
  4342
  unfolding compact_eq_bounded_closed using bounded_pastecart[of s t] closed_pastecart[of s t] by auto
himmelma@33175
  4343
himmelma@33175
  4344
lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"
himmelma@33175
  4345
by (induct x) simp
himmelma@33175
  4346
himmelma@33175
  4347
lemma compact_Times: "compact s \<Longrightarrow> compact t \<Longrightarrow> compact (s \<times> t)"
himmelma@33175
  4348
unfolding compact_def
himmelma@33175
  4349
apply clarify
himmelma@33175
  4350
apply (drule_tac x="fst \<circ> f" in spec)
himmelma@33175
  4351
apply (drule mp, simp add: mem_Times_iff)
himmelma@33175
  4352
apply (clarify, rename_tac l1 r1)
himmelma@33175
  4353
apply (drule_tac x="snd \<circ> f \<circ> r1" in spec)
himmelma@33175
  4354
apply (drule mp, simp add: mem_Times_iff)
himmelma@33175
  4355
apply (clarify, rename_tac l2 r2)
himmelma@33175
  4356
apply (rule_tac x="(l1, l2)" in rev_bexI, simp)
himmelma@33175
  4357
apply (rule_tac x="r1 \<circ> r2" in exI)
himmelma@33175
  4358
apply (rule conjI, simp add: subseq_def)
himmelma@33175
  4359
apply (drule_tac r=r2 in lim_subseq [COMP swap_prems_rl], assumption)
himmelma@33175
  4360
apply (drule (1) tendsto_Pair) back
himmelma@33175
  4361
apply (simp add: o_def)
himmelma@33175
  4362
done
himmelma@33175
  4363
himmelma@33175
  4364
text{* Hence some useful properties follow quite easily.                         *}
himmelma@33175
  4365
himmelma@33175
  4366
lemma compact_scaling:
himmelma@33175
  4367
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4368
  assumes "compact s"  shows "compact ((\<lambda>x. c *\<^sub>R x) ` s)"
himmelma@33175
  4369
proof-
himmelma@33175
  4370
  let ?f = "\<lambda>x. scaleR c x"
himmelma@33175
  4371
  have *:"bounded_linear ?f" by (rule scaleR.bounded_linear_right)
himmelma@33175
  4372
  show ?thesis using compact_continuous_image[of s ?f] continuous_at_imp_continuous_on[of s ?f]
himmelma@33175
  4373
    using linear_continuous_at[OF *] assms by auto
himmelma@33175
  4374
qed
himmelma@33175
  4375
himmelma@33175
  4376
lemma compact_negations:
himmelma@33175
  4377
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4378
  assumes "compact s"  shows "compact ((\<lambda>x. -x) ` s)"
himmelma@33175
  4379
  using compact_scaling [OF assms, of "- 1"] by auto
himmelma@33175
  4380
himmelma@33175
  4381
lemma compact_sums:
himmelma@33175
  4382
  fixes s t :: "'a::real_normed_vector set"
himmelma@33175
  4383
  assumes "compact s"  "compact t"  shows "compact {x + y | x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4384
proof-
himmelma@33175
  4385
  have *:"{x + y | x y. x \<in> s \<and> y \<in> t} = (\<lambda>z. fst z + snd z) ` (s \<times> t)"
himmelma@33175
  4386
    apply auto unfolding image_iff apply(rule_tac x="(xa, y)" in bexI) by auto
himmelma@33175
  4387
  have "continuous_on (s \<times> t) (\<lambda>z. fst z + snd z)"
himmelma@33175
  4388
    unfolding continuous_on by (rule ballI) (intro tendsto_intros)
himmelma@33175
  4389
  thus ?thesis unfolding * using compact_continuous_image compact_Times [OF assms] by auto
himmelma@33175
  4390
qed
himmelma@33175
  4391
himmelma@33175
  4392
lemma compact_differences:
himmelma@33175
  4393
  fixes s t :: "'a::real_normed_vector set"
himmelma@33175
  4394
  assumes "compact s" "compact t"  shows "compact {x - y | x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4395
proof-
himmelma@33175
  4396
  have "{x - y | x y. x\<in>s \<and> y \<in> t} =  {x + y | x y. x \<in> s \<and> y \<in> (uminus ` t)}"
himmelma@33175
  4397
    apply auto apply(rule_tac x= xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
himmelma@33175
  4398
  thus ?thesis using compact_sums[OF assms(1) compact_negations[OF assms(2)]] by auto
himmelma@33175
  4399
qed
himmelma@33175
  4400
himmelma@33175
  4401
lemma compact_translation:
himmelma@33175
  4402
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4403
  assumes "compact s"  shows "compact ((\<lambda>x. a + x) ` s)"
himmelma@33175
  4404
proof-
himmelma@33175
  4405
  have "{x + y |x y. x \<in> s \<and> y \<in> {a}} = (\<lambda>x. a + x) ` s" by auto
himmelma@33175
  4406
  thus ?thesis using compact_sums[OF assms compact_sing[of a]] by auto
himmelma@33175
  4407
qed
himmelma@33175
  4408
himmelma@33175
  4409
lemma compact_affinity:
himmelma@33175
  4410
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4411
  assumes "compact s"  shows "compact ((\<lambda>x. a + c *\<^sub>R x) ` s)"
himmelma@33175
  4412
proof-
himmelma@33175
  4413
  have "op + a ` op *\<^sub>R c ` s = (\<lambda>x. a + c *\<^sub>R x) ` s" by auto
himmelma@33175
  4414
  thus ?thesis using compact_translation[OF compact_scaling[OF assms], of a c] by auto
himmelma@33175
  4415
qed
himmelma@33175
  4416
himmelma@33175
  4417
text{* Hence we get the following.                                               *}
himmelma@33175
  4418
himmelma@33175
  4419
lemma compact_sup_maxdistance:
himmelma@33175
  4420
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4421
  assumes "compact s"  "s \<noteq> {}"
himmelma@33175
  4422
  shows "\<exists>x\<in>s. \<exists>y\<in>s. \<forall>u\<in>s. \<forall>v\<in>s. norm(u - v) \<le> norm(x - y)"
himmelma@33175
  4423
proof-
himmelma@33175
  4424
  have "{x - y | x y . x\<in>s \<and> y\<in>s} \<noteq> {}" using `s \<noteq> {}` by auto
himmelma@33175
  4425
  then obtain x where x:"x\<in>{x - y |x y. x \<in> s \<and> y \<in> s}"  "\<forall>y\<in>{x - y |x y. x \<in> s \<and> y \<in> s}. norm y \<le> norm x"
himmelma@33175
  4426
    using compact_differences[OF assms(1) assms(1)]
himmelma@33175
  4427
    using distance_attains_sup[where 'a="'a", unfolded dist_norm, of "{x - y | x y . x\<in>s \<and> y\<in>s}" 0] by(auto simp add: norm_minus_cancel)
himmelma@33175
  4428
  from x(1) obtain a b where "a\<in>s" "b\<in>s" "x = a - b" by auto
himmelma@33175
  4429
  thus ?thesis using x(2)[unfolded `x = a - b`] by blast
himmelma@33175
  4430
qed
himmelma@33175
  4431
himmelma@33175
  4432
text{* We can state this in terms of diameter of a set.                          *}
himmelma@33175
  4433
paulson@33270
  4434
definition "diameter s = (if s = {} then 0::real else Sup {norm(x - y) | x y. x \<in> s \<and> y \<in> s})"
himmelma@33175
  4435
  (* TODO: generalize to class metric_space *)
himmelma@33175
  4436
himmelma@33175
  4437
lemma diameter_bounded:
himmelma@33175
  4438
  assumes "bounded s"
himmelma@33175
  4439
  shows "\<forall>x\<in>s. \<forall>y\<in>s. norm(x - y) \<le> diameter s"
himmelma@33175
  4440
        "\<forall>d>0. d < diameter s --> (\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d)"
himmelma@33175
  4441
proof-
himmelma@33175
  4442
  let ?D = "{norm (x - y) |x y. x \<in> s \<and> y \<in> s}"
himmelma@33175
  4443
  obtain a where a:"\<forall>x\<in>s. norm x \<le> a" using assms[unfolded bounded_iff] by auto
himmelma@33175
  4444
  { fix x y assume "x \<in> s" "y \<in> s"
himmelma@33175
  4445
    hence "norm (x - y) \<le> 2 * a" using norm_triangle_ineq[of x "-y", unfolded norm_minus_cancel] a[THEN bspec[where x=x]] a[THEN bspec[where x=y]] by (auto simp add: ring_simps)  }
himmelma@33175
  4446
  note * = this
himmelma@33175
  4447
  { fix x y assume "x\<in>s" "y\<in>s"  hence "s \<noteq> {}" by auto
paulson@33307
  4448
    have "norm(x - y) \<le> diameter s" unfolding diameter_def using `s\<noteq>{}` *[OF `x\<in>s` `y\<in>s`] `x\<in>s` `y\<in>s`  
paulson@33307
  4449
      by simp (blast intro!: Sup_upper *) }
himmelma@33175
  4450
  moreover
himmelma@33175
  4451
  { fix d::real assume "d>0" "d < diameter s"
himmelma@33175
  4452
    hence "s\<noteq>{}" unfolding diameter_def by auto
himmelma@33175
  4453
    have "\<exists>d' \<in> ?D. d' > d"
himmelma@33175
  4454
    proof(rule ccontr)
himmelma@33175
  4455
      assume "\<not> (\<exists>d'\<in>{norm (x - y) |x y. x \<in> s \<and> y \<in> s}. d < d')"
paulson@33307
  4456
      hence "\<forall>d'\<in>?D. d' \<le> d" by auto (metis not_leE) 
paulson@33307
  4457
      thus False using `d < diameter s` `s\<noteq>{}` 
paulson@33307
  4458
        apply (auto simp add: diameter_def) 
paulson@33307
  4459
        apply (drule Sup_real_iff [THEN [2] rev_iffD2])
paulson@33307
  4460
        apply (auto, force) 
paulson@33307
  4461
        done
himmelma@33175
  4462
    qed
himmelma@33175
  4463
    hence "\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d" by auto  }
himmelma@33175
  4464
  ultimately show "\<forall>x\<in>s. \<forall>y\<in>s. norm(x - y) \<le> diameter s"
himmelma@33175
  4465
        "\<forall>d>0. d < diameter s --> (\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d)" by auto
himmelma@33175
  4466
qed
himmelma@33175
  4467
himmelma@33175
  4468
lemma diameter_bounded_bound:
himmelma@33175
  4469
 "bounded s \<Longrightarrow> x \<in> s \<Longrightarrow> y \<in> s ==> norm(x - y) \<le> diameter s"
himmelma@33175
  4470
  using diameter_bounded by blast
himmelma@33175
  4471
himmelma@33175
  4472
lemma diameter_compact_attained:
himmelma@33175
  4473
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4474
  assumes "compact s"  "s \<noteq> {}"
himmelma@33175
  4475
  shows "\<exists>x\<in>s. \<exists>y\<in>s. (norm(x - y) = diameter s)"
himmelma@33175
  4476
proof-
himmelma@33175
  4477
  have b:"bounded s" using assms(1) by (rule compact_imp_bounded)
himmelma@33175
  4478
  then obtain x y where xys:"x\<in>s" "y\<in>s" and xy:"\<forall>u\<in>s. \<forall>v\<in>s. norm (u - v) \<le> norm (x - y)" using compact_sup_maxdistance[OF assms] by auto
paulson@33270
  4479
  hence "diameter s \<le> norm (x - y)" 
paulson@33270
  4480
    by (force simp add: diameter_def intro!: Sup_least) 
paulson@33307
  4481
  thus ?thesis
paulson@33307
  4482
    by (metis b diameter_bounded_bound order_antisym xys) 
himmelma@33175
  4483
qed
himmelma@33175
  4484
himmelma@33175
  4485
text{* Related results with closure as the conclusion.                           *}
himmelma@33175
  4486
himmelma@33175
  4487
lemma closed_scaling:
himmelma@33175
  4488
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4489
  assumes "closed s" shows "closed ((\<lambda>x. c *\<^sub>R x) ` s)"
himmelma@33175
  4490
proof(cases "s={}")
himmelma@33175
  4491
  case True thus ?thesis by auto
himmelma@33175
  4492
next
himmelma@33175
  4493
  case False
himmelma@33175
  4494
  show ?thesis
himmelma@33175
  4495
  proof(cases "c=0")
himmelma@33175
  4496
    have *:"(\<lambda>x. 0) ` s = {0}" using `s\<noteq>{}` by auto
himmelma@33175
  4497
    case True thus ?thesis apply auto unfolding * using closed_sing by auto
himmelma@33175
  4498
  next
himmelma@33175
  4499
    case False
himmelma@33175
  4500
    { fix x l assume as:"\<forall>n::nat. x n \<in> scaleR c ` s"  "(x ---> l) sequentially"
himmelma@33175
  4501
      { fix n::nat have "scaleR (1 / c) (x n) \<in> s"
himmelma@33175
  4502
          using as(1)[THEN spec[where x=n]]
himmelma@33175
  4503
          using `c\<noteq>0` by (auto simp add: vector_smult_assoc)
himmelma@33175
  4504
      }
himmelma@33175
  4505
      moreover
himmelma@33175
  4506
      { fix e::real assume "e>0"
himmelma@33175
  4507
        hence "0 < e *\<bar>c\<bar>"  using `c\<noteq>0` mult_pos_pos[of e "abs c"] by auto
himmelma@33175
  4508
        then obtain N where "\<forall>n\<ge>N. dist (x n) l < e * \<bar>c\<bar>"
himmelma@33175
  4509
          using as(2)[unfolded Lim_sequentially, THEN spec[where x="e * abs c"]] by auto
himmelma@33175
  4510
        hence "\<exists>N. \<forall>n\<ge>N. dist (scaleR (1 / c) (x n)) (scaleR (1 / c) l) < e"
himmelma@33175
  4511
          unfolding dist_norm unfolding scaleR_right_diff_distrib[THEN sym]
himmelma@33175
  4512
          using mult_imp_div_pos_less[of "abs c" _ e] `c\<noteq>0` by auto  }
himmelma@33175
  4513
      hence "((\<lambda>n. scaleR (1 / c) (x n)) ---> scaleR (1 / c) l) sequentially" unfolding Lim_sequentially by auto
himmelma@33175
  4514
      ultimately have "l \<in> scaleR c ` s"
himmelma@33175
  4515
        using assms[unfolded closed_sequential_limits, THEN spec[where x="\<lambda>n. scaleR (1/c) (x n)"], THEN spec[where x="scaleR (1/c) l"]]
himmelma@33175
  4516
        unfolding image_iff using `c\<noteq>0` apply(rule_tac x="scaleR (1 / c) l" in bexI) by auto  }
himmelma@33175
  4517
    thus ?thesis unfolding closed_sequential_limits by fast
himmelma@33175
  4518
  qed
himmelma@33175
  4519
qed
himmelma@33175
  4520
himmelma@33175
  4521
lemma closed_negations:
himmelma@33175
  4522
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4523
  assumes "closed s"  shows "closed ((\<lambda>x. -x) ` s)"
himmelma@33175
  4524
  using closed_scaling[OF assms, of "- 1"] by simp
himmelma@33175
  4525
himmelma@33175
  4526
lemma compact_closed_sums:
himmelma@33175
  4527
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4528
  assumes "compact s"  "closed t"  shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4529
proof-
himmelma@33175
  4530
  let ?S = "{x + y |x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4531
  { fix x l assume as:"\<forall>n. x n \<in> ?S"  "(x ---> l) sequentially"
himmelma@33175
  4532
    from as(1) obtain f where f:"\<forall>n. x n = fst (f n) + snd (f n)"  "\<forall>n. fst (f n) \<in> s"  "\<forall>n. snd (f n) \<in> t"
himmelma@33175
  4533
      using choice[of "\<lambda>n y. x n = (fst y) + (snd y) \<and> fst y \<in> s \<and> snd y \<in> t"] by auto
himmelma@33175
  4534
    obtain l' r where "l'\<in>s" and r:"subseq r" and lr:"(((\<lambda>n. fst (f n)) \<circ> r) ---> l') sequentially"
himmelma@33175
  4535
      using assms(1)[unfolded compact_def, THEN spec[where x="\<lambda> n. fst (f n)"]] using f(2) by auto
himmelma@33175
  4536
    have "((\<lambda>n. snd (f (r n))) ---> l - l') sequentially"
himmelma@33175
  4537
      using Lim_sub[OF lim_subseq[OF r as(2)] lr] and f(1) unfolding o_def by auto
himmelma@33175
  4538
    hence "l - l' \<in> t"
himmelma@33175
  4539
      using assms(2)[unfolded closed_sequential_limits, THEN spec[where x="\<lambda> n. snd (f (r n))"], THEN spec[where x="l - l'"]]
himmelma@33175
  4540
      using f(3) by auto
himmelma@33175
  4541
    hence "l \<in> ?S" using `l' \<in> s` apply auto apply(rule_tac x=l' in exI) apply(rule_tac x="l - l'" in exI) by auto
himmelma@33175
  4542
  }
himmelma@33175
  4543
  thus ?thesis unfolding closed_sequential_limits by fast
himmelma@33175
  4544
qed
himmelma@33175
  4545
himmelma@33175
  4546
lemma closed_compact_sums:
himmelma@33175
  4547
  fixes s t :: "'a::real_normed_vector set"
himmelma@33175
  4548
  assumes "closed s"  "compact t"
himmelma@33175
  4549
  shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4550
proof-
himmelma@33175
  4551
  have "{x + y |x y. x \<in> t \<and> y \<in> s} = {x + y |x y. x \<in> s \<and> y \<in> t}" apply auto
himmelma@33175
  4552
    apply(rule_tac x=y in exI) apply auto apply(rule_tac x=y in exI) by auto
himmelma@33175
  4553
  thus ?thesis using compact_closed_sums[OF assms(2,1)] by simp
himmelma@33175
  4554
qed
himmelma@33175
  4555
himmelma@33175
  4556
lemma compact_closed_differences:
himmelma@33175
  4557
  fixes s t :: "'a::real_normed_vector set"
himmelma@33175
  4558
  assumes "compact s"  "closed t"
himmelma@33175
  4559
  shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4560
proof-
himmelma@33175
  4561
  have "{x + y |x y. x \<in> s \<and> y \<in> uminus ` t} =  {x - y |x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4562
    apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
himmelma@33175
  4563
  thus ?thesis using compact_closed_sums[OF assms(1) closed_negations[OF assms(2)]] by auto
himmelma@33175
  4564
qed
himmelma@33175
  4565
himmelma@33175
  4566
lemma closed_compact_differences:
himmelma@33175
  4567
  fixes s t :: "'a::real_normed_vector set"
himmelma@33175
  4568
  assumes "closed s" "compact t"
himmelma@33175
  4569
  shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4570
proof-
himmelma@33175
  4571
  have "{x + y |x y. x \<in> s \<and> y \<in> uminus ` t} = {x - y |x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4572
    apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
himmelma@33175
  4573
 thus ?thesis using closed_compact_sums[OF assms(1) compact_negations[OF assms(2)]] by simp
himmelma@33175
  4574
qed
himmelma@33175
  4575
himmelma@33175
  4576
lemma closed_translation:
himmelma@33175
  4577
  fixes a :: "'a::real_normed_vector"
himmelma@33175
  4578
  assumes "closed s"  shows "closed ((\<lambda>x. a + x) ` s)"
himmelma@33175
  4579
proof-
himmelma@33175
  4580
  have "{a + y |y. y \<in> s} = (op + a ` s)" by auto
himmelma@33175
  4581
  thus ?thesis using compact_closed_sums[OF compact_sing[of a] assms] by auto
himmelma@33175
  4582
qed
himmelma@33175
  4583
huffman@34099
  4584
lemma translation_Compl:
huffman@34099
  4585
  fixes a :: "'a::ab_group_add"
huffman@34099
  4586
  shows "(\<lambda>x. a + x) ` (- t) = - ((\<lambda>x. a + x) ` t)"
huffman@34099
  4587
  apply (auto simp add: image_iff) apply(rule_tac x="x - a" in bexI) by auto
huffman@34099
  4588
himmelma@33175
  4589
lemma translation_UNIV:
himmelma@33175
  4590
  fixes a :: "'a::ab_group_add" shows "range (\<lambda>x. a + x) = UNIV"
himmelma@33175
  4591
  apply (auto simp add: image_iff) apply(rule_tac x="x - a" in exI) by auto
himmelma@33175
  4592
himmelma@33175
  4593
lemma translation_diff:
himmelma@33175
  4594
  fixes a :: "'a::ab_group_add"
himmelma@33175
  4595
  shows "(\<lambda>x. a + x) ` (s - t) = ((\<lambda>x. a + x) ` s) - ((\<lambda>x. a + x) ` t)"
himmelma@33175
  4596
  by auto
himmelma@33175
  4597
himmelma@33175
  4598
lemma closure_translation:
himmelma@33175
  4599
  fixes a :: "'a::real_normed_vector"
himmelma@33175
  4600
  shows "closure ((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (closure s)"
himmelma@33175
  4601
proof-
huffman@34099
  4602
  have *:"op + a ` (- s) = - op + a ` s"
himmelma@33175
  4603
    apply auto unfolding image_iff apply(rule_tac x="x - a" in bexI) by auto
huffman@34099
  4604
  show ?thesis unfolding closure_interior translation_Compl
huffman@34099
  4605
    using interior_translation[of a "- s"] unfolding * by auto
himmelma@33175
  4606
qed
himmelma@33175
  4607
himmelma@33175
  4608
lemma frontier_translation:
himmelma@33175
  4609
  fixes a :: "'a::real_normed_vector"
himmelma@33175
  4610
  shows "frontier((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (frontier s)"
himmelma@33175
  4611
  unfolding frontier_def translation_diff interior_translation closure_translation by auto
himmelma@33175
  4612
himmelma@33175
  4613
subsection{* Separation between points and sets.                                       *}
himmelma@33175
  4614
himmelma@33175
  4615
lemma separate_point_closed:
himmelma@33175
  4616
  fixes s :: "'a::heine_borel set"
himmelma@33175
  4617
  shows "closed s \<Longrightarrow> a \<notin> s  ==> (\<exists>d>0. \<forall>x\<in>s. d \<le> dist a x)"
himmelma@33175
  4618
proof(cases "s = {}")
himmelma@33175
  4619
  case True
himmelma@33175
  4620
  thus ?thesis by(auto intro!: exI[where x=1])
himmelma@33175
  4621
next
himmelma@33175
  4622
  case False
himmelma@33175
  4623
  assume "closed s" "a \<notin> s"
himmelma@33175
  4624
  then obtain x where "x\<in>s" "\<forall>y\<in>s. dist a x \<le> dist a y" using `s \<noteq> {}` distance_attains_inf [of s a] by blast
himmelma@33175
  4625
  with `x\<in>s` show ?thesis using dist_pos_lt[of a x] and`a \<notin> s` by blast
himmelma@33175
  4626
qed
himmelma@33175
  4627
himmelma@33175
  4628
lemma separate_compact_closed:
himmelma@33175
  4629
  fixes s t :: "'a::{heine_borel, real_normed_vector} set"
himmelma@33175
  4630
    (* TODO: does this generalize to heine_borel? *)
himmelma@33175
  4631
  assumes "compact s" and "closed t" and "s \<inter> t = {}"
himmelma@33175
  4632
  shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"
himmelma@33175
  4633
proof-
himmelma@33175
  4634
  have "0 \<notin> {x - y |x y. x \<in> s \<and> y \<in> t}" using assms(3) by auto
himmelma@33175
  4635
  then obtain d where "d>0" and d:"\<forall>x\<in>{x - y |x y. x \<in> s \<and> y \<in> t}. d \<le> dist 0 x"
himmelma@33175
  4636
    using separate_point_closed[OF compact_closed_differences[OF assms(1,2)], of 0] by auto
himmelma@33175
  4637
  { fix x y assume "x\<in>s" "y\<in>t"
himmelma@33175
  4638
    hence "x - y \<in> {x - y |x y. x \<in> s \<and> y \<in> t}" by auto
himmelma@33175
  4639
    hence "d \<le> dist (x - y) 0" using d[THEN bspec[where x="x - y"]] using dist_commute
himmelma@33175
  4640
      by (auto  simp add: dist_commute)
himmelma@33175
  4641
    hence "d \<le> dist x y" unfolding dist_norm by auto  }
himmelma@33175
  4642
  thus ?thesis using `d>0` by auto
himmelma@33175
  4643
qed
himmelma@33175
  4644
himmelma@33175
  4645
lemma separate_closed_compact:
himmelma@33175
  4646
  fixes s t :: "'a::{heine_borel, real_normed_vector} set"
himmelma@33175
  4647
  assumes "closed s" and "compact t" and "s \<inter> t = {}"
himmelma@33175
  4648
  shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"
himmelma@33175
  4649
proof-
himmelma@33175
  4650
  have *:"t \<inter> s = {}" using assms(3) by auto
himmelma@33175
  4651
  show ?thesis using separate_compact_closed[OF assms(2,1) *]
himmelma@33175
  4652
    apply auto apply(rule_tac x=d in exI) apply auto apply (erule_tac x=y in ballE)
himmelma@33175
  4653
    by (auto simp add: dist_commute)
himmelma@33175
  4654
qed
himmelma@33175
  4655
himmelma@33175
  4656
(* A cute way of denoting open and closed intervals using overloading.       *)
himmelma@33175
  4657
hoelzl@34291
  4658
lemma interval: fixes a :: "'a::ord^'n" shows
himmelma@33175
  4659
  "{a <..< b} = {x::'a^'n. \<forall>i. a$i < x$i \<and> x$i < b$i}" and
himmelma@33175
  4660
  "{a .. b} = {x::'a^'n. \<forall>i. a$i \<le> x$i \<and> x$i \<le> b$i}"
hoelzl@33758
  4661
  by (auto simp add: expand_set_eq vector_less_def vector_le_def)
himmelma@33175
  4662
hoelzl@34291
  4663
lemma mem_interval: fixes a :: "'a::ord^'n" shows
himmelma@33175
  4664
  "x \<in> {a<..<b} \<longleftrightarrow> (\<forall>i. a$i < x$i \<and> x$i < b$i)"
himmelma@33175
  4665
  "x \<in> {a .. b} \<longleftrightarrow> (\<forall>i. a$i \<le> x$i \<and> x$i \<le> b$i)"
hoelzl@33758
  4666
  using interval[of a b] by(auto simp add: expand_set_eq vector_less_def vector_le_def)
himmelma@33175
  4667
himmelma@33175
  4668
lemma mem_interval_1: fixes x :: "real^1" shows
himmelma@33175
  4669
 "(x \<in> {a .. b} \<longleftrightarrow> dest_vec1 a \<le> dest_vec1 x \<and> dest_vec1 x \<le> dest_vec1 b)"
himmelma@33175
  4670
 "(x \<in> {a<..<b} \<longleftrightarrow> dest_vec1 a < dest_vec1 x \<and> dest_vec1 x < dest_vec1 b)"
hoelzl@34951
  4671
by(simp_all add: Cart_eq vector_less_def vector_le_def forall_1)
himmelma@33175
  4672
hoelzl@33711
  4673
lemma vec1_interval:fixes a::"real" shows
hoelzl@33711
  4674
  "vec1 ` {a .. b} = {vec1 a .. vec1 b}"
hoelzl@33711
  4675
  "vec1 ` {a<..<b} = {vec1 a<..<vec1 b}"
hoelzl@33711
  4676
  apply(rule_tac[!] set_ext) unfolding image_iff vector_less_def unfolding mem_interval
hoelzl@34951
  4677
  unfolding forall_1 unfolding vec1_dest_vec1_simps
hoelzl@33711
  4678
  apply rule defer apply(rule_tac x="dest_vec1 x" in bexI) prefer 3 apply rule defer
hoelzl@33711
  4679
  apply(rule_tac x="dest_vec1 x" in bexI) by auto
hoelzl@33711
  4680
hoelzl@33711
  4681
hoelzl@34291
  4682
lemma interval_eq_empty: fixes a :: "real^'n" shows
himmelma@33175
  4683
 "({a <..< b} = {} \<longleftrightarrow> (\<exists>i. b$i \<le> a$i))" (is ?th1) and
himmelma@33175
  4684
 "({a  ..  b} = {} \<longleftrightarrow> (\<exists>i. b$i < a$i))" (is ?th2)
himmelma@33175
  4685
proof-
himmelma@33175
  4686
  { fix i x assume as:"b$i \<le> a$i" and x:"x\<in>{a <..< b}"
himmelma@33175
  4687
    hence "a $ i < x $ i \<and> x $ i < b $ i" unfolding mem_interval by auto
himmelma@33175
  4688
    hence "a$i < b$i" by auto
himmelma@33175
  4689
    hence False using as by auto  }
himmelma@33175
  4690
  moreover
himmelma@33175
  4691
  { assume as:"\<forall>i. \<not> (b$i \<le> a$i)"
himmelma@33175
  4692
    let ?x = "(1/2) *\<^sub>R (a + b)"
himmelma@33175
  4693
    { fix i
himmelma@33175
  4694
      have "a$i < b$i" using as[THEN spec[where x=i]] by auto
himmelma@33175
  4695
      hence "a$i < ((1/2) *\<^sub>R (a+b)) $ i" "((1/2) *\<^sub>R (a+b)) $ i < b$i"
himmelma@33175
  4696
        unfolding vector_smult_component and vector_add_component
himmelma@33175
  4697
        by (auto simp add: less_divide_eq_number_of1)  }
himmelma@33175
  4698
    hence "{a <..< b} \<noteq> {}" using mem_interval(1)[of "?x" a b] by auto  }
himmelma@33175
  4699
  ultimately show ?th1 by blast
himmelma@33175
  4700
himmelma@33175
  4701
  { fix i x assume as:"b$i < a$i" and x:"x\<in>{a .. b}"
himmelma@33175
  4702
    hence "a $ i \<le> x $ i \<and> x $ i \<le> b $ i" unfolding mem_interval by auto
himmelma@33175
  4703
    hence "a$i \<le> b$i" by auto
himmelma@33175
  4704
    hence False using as by auto  }
himmelma@33175
  4705
  moreover
himmelma@33175
  4706
  { assume as:"\<forall>i. \<not> (b$i < a$i)"
himmelma@33175
  4707
    let ?x = "(1/2) *\<^sub>R (a + b)"
himmelma@33175
  4708
    { fix i
himmelma@33175
  4709
      have "a$i \<le> b$i" using as[THEN spec[where x=i]] by auto
himmelma@33175
  4710
      hence "a$i \<le> ((1/2) *\<^sub>R (a+b)) $ i" "((1/2) *\<^sub>R (a+b)) $ i \<le> b$i"
himmelma@33175
  4711
        unfolding vector_smult_component and vector_add_component
himmelma@33175
  4712
        by (auto simp add: less_divide_eq_number_of1)  }
himmelma@33175
  4713
    hence "{a .. b} \<noteq> {}" using mem_interval(2)[of "?x" a b] by auto  }
himmelma@33175
  4714
  ultimately show ?th2 by blast
himmelma@33175
  4715
qed
himmelma@33175
  4716
hoelzl@34291
  4717
lemma interval_ne_empty: fixes a :: "real^'n" shows
himmelma@33175
  4718
  "{a  ..  b} \<noteq> {} \<longleftrightarrow> (\<forall>i. a$i \<le> b$i)" and
himmelma@33175
  4719
  "{a <..< b} \<noteq> {} \<longleftrightarrow> (\<forall>i. a$i < b$i)"
himmelma@33175
  4720
  unfolding interval_eq_empty[of a b] by (auto simp add: not_less not_le) (* BH: Why doesn't just "auto" work here? *)
himmelma@33175
  4721
hoelzl@34291
  4722
lemma subset_interval_imp: fixes a :: "real^'n" shows
himmelma@33175
  4723
 "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> {c .. d} \<subseteq> {a .. b}" and
himmelma@33175
  4724
 "(\<forall>i. a$i < c$i \<and> d$i < b$i) \<Longrightarrow> {c .. d} \<subseteq> {a<..<b}" and
himmelma@33175
  4725
 "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> {c<..<d} \<subseteq> {a .. b}" and
himmelma@33175
  4726
 "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> {c<..<d} \<subseteq> {a<..<b}"
himmelma@33175
  4727
  unfolding subset_eq[unfolded Ball_def] unfolding mem_interval
himmelma@33175
  4728
  by (auto intro: order_trans less_le_trans le_less_trans less_imp_le) (* BH: Why doesn't just "auto" work here? *)
himmelma@33175
  4729
hoelzl@34291
  4730
lemma interval_sing: fixes a :: "'a::linorder^'n" shows
himmelma@33175
  4731
 "{a .. a} = {a} \<and> {a<..<a} = {}"
hoelzl@33758
  4732
apply(auto simp add: expand_set_eq vector_less_def vector_le_def Cart_eq)
himmelma@33175
  4733
apply (simp add: order_eq_iff)
himmelma@33175
  4734
apply (auto simp add: not_less less_imp_le)
himmelma@33175
  4735
done
himmelma@33175
  4736
hoelzl@34291
  4737
lemma interval_open_subset_closed:  fixes a :: "'a::preorder^'n" shows
himmelma@33175
  4738
 "{a<..<b} \<subseteq> {a .. b}"
himmelma@33175
  4739
proof(simp add: subset_eq, rule)
himmelma@33175
  4740
  fix x
himmelma@33175
  4741
  assume x:"x \<in>{a<..<b}"
himmelma@33175
  4742
  { fix i
himmelma@33175
  4743
    have "a $ i \<le> x $ i"
himmelma@33175
  4744
      using x order_less_imp_le[of "a$i" "x$i"]
hoelzl@33758
  4745
      by(simp add: expand_set_eq vector_less_def vector_le_def Cart_eq)
himmelma@33175
  4746
  }
himmelma@33175
  4747
  moreover
himmelma@33175
  4748
  { fix i
himmelma@33175
  4749
    have "x $ i \<le> b $ i"
himmelma@33175
  4750
      using x order_less_imp_le[of "x$i" "b$i"]
hoelzl@33758
  4751
      by(simp add: expand_set_eq vector_less_def vector_le_def Cart_eq)
himmelma@33175
  4752
  }
himmelma@33175
  4753
  ultimately
himmelma@33175
  4754
  show "a \<le> x \<and> x \<le> b"
hoelzl@33758
  4755
    by(simp add: expand_set_eq vector_less_def vector_le_def Cart_eq)
himmelma@33175
  4756
qed
himmelma@33175
  4757
hoelzl@34291
  4758
lemma subset_interval: fixes a :: "real^'n" shows
himmelma@33175
  4759
 "{c .. d} \<subseteq> {a .. b} \<longleftrightarrow> (\<forall>i. c$i \<le> d$i) --> (\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i)" (is ?th1) and
himmelma@33175
  4760
 "{c .. d} \<subseteq> {a<..<b} \<longleftrightarrow> (\<forall>i. c$i \<le> d$i) --> (\<forall>i. a$i < c$i \<and> d$i < b$i)" (is ?th2) and
himmelma@33175
  4761
 "{c<..<d} \<subseteq> {a .. b} \<longleftrightarrow> (\<forall>i. c$i < d$i) --> (\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i)" (is ?th3) and
himmelma@33175
  4762
 "{c<..<d} \<subseteq> {a<..<b} \<longleftrightarrow> (\<forall>i. c$i < d$i) --> (\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i)" (is ?th4)
himmelma@33175
  4763
proof-
himmelma@33175
  4764
  show ?th1 unfolding subset_eq and Ball_def and mem_interval by (auto intro: order_trans)
himmelma@33175
  4765
  show ?th2 unfolding subset_eq and Ball_def and mem_interval by (auto intro: le_less_trans less_le_trans order_trans less_imp_le)
himmelma@33175
  4766
  { assume as: "{c<..<d} \<subseteq> {a .. b}" "\<forall>i. c$i < d$i"
himmelma@33175
  4767
    hence "{c<..<d} \<noteq> {}" unfolding interval_eq_empty by (auto, drule_tac x=i in spec, simp) (* BH: Why doesn't just "auto" work? *)
himmelma@33175
  4768
    fix i
himmelma@33175
  4769
    (** TODO combine the following two parts as done in the HOL_light version. **)
himmelma@33175
  4770
    { let ?x = "(\<chi> j. (if j=i then ((min (a$j) (d$j))+c$j)/2 else (c$j+d$j)/2))::real^'n"
himmelma@33175
  4771
      assume as2: "a$i > c$i"
himmelma@33175
  4772
      { fix j
himmelma@33175
  4773
        have "c $ j < ?x $ j \<and> ?x $ j < d $ j" unfolding Cart_lambda_beta
himmelma@33175
  4774
          apply(cases "j=i") using as(2)[THEN spec[where x=j]]
himmelma@33175
  4775
          by (auto simp add: less_divide_eq_number_of1 as2)  }
himmelma@33175
  4776
      hence "?x\<in>{c<..<d}" unfolding mem_interval by auto
himmelma@33175
  4777
      moreover
himmelma@33175
  4778
      have "?x\<notin>{a .. b}"
himmelma@33175
  4779
        unfolding mem_interval apply auto apply(rule_tac x=i in exI)
himmelma@33175
  4780
        using as(2)[THEN spec[where x=i]] and as2
himmelma@33175
  4781
        by (auto simp add: less_divide_eq_number_of1)
himmelma@33175
  4782
      ultimately have False using as by auto  }
himmelma@33175
  4783
    hence "a$i \<le> c$i" by(rule ccontr)auto
himmelma@33175
  4784
    moreover
himmelma@33175
  4785
    { let ?x = "(\<chi> j. (if j=i then ((max (b$j) (c$j))+d$j)/2 else (c$j+d$j)/2))::real^'n"
himmelma@33175
  4786
      assume as2: "b$i < d$i"
himmelma@33175
  4787
      { fix j
himmelma@33175
  4788
        have "d $ j > ?x $ j \<and> ?x $ j > c $ j" unfolding Cart_lambda_beta
himmelma@33175
  4789
          apply(cases "j=i") using as(2)[THEN spec[where x=j]]
himmelma@33175
  4790
          by (auto simp add: less_divide_eq_number_of1 as2)  }
himmelma@33175
  4791
      hence "?x\<in>{c<..<d}" unfolding mem_interval by auto
himmelma@33175
  4792
      moreover
himmelma@33175
  4793
      have "?x\<notin>{a .. b}"
himmelma@33175
  4794
        unfolding mem_interval apply auto apply(rule_tac x=i in exI)
himmelma@33175
  4795
        using as(2)[THEN spec[where x=i]] and as2
himmelma@33175
  4796
        by (auto simp add: less_divide_eq_number_of1)
himmelma@33175
  4797
      ultimately have False using as by auto  }
himmelma@33175
  4798
    hence "b$i \<ge> d$i" by(rule ccontr)auto
himmelma@33175
  4799
    ultimately
himmelma@33175
  4800
    have "a$i \<le> c$i \<and> d$i \<le> b$i" by auto
himmelma@33175
  4801
  } note part1 = this
himmelma@33175
  4802
  thus ?th3 unfolding subset_eq and Ball_def and mem_interval apply auto apply (erule_tac x=ia in allE, simp)+ by (erule_tac x=i in allE, erule_tac x=i in allE, simp)+
himmelma@33175
  4803
  { assume as:"{c<..<d} \<subseteq> {a<..<b}" "\<forall>i. c$i < d$i"
himmelma@33175
  4804
    fix i
himmelma@33175
  4805
    from as(1) have "{c<..<d} \<subseteq> {a..b}" using interval_open_subset_closed[of a b] by auto
himmelma@33175
  4806
    hence "a$i \<le> c$i \<and> d$i \<le> b$i" using part1 and as(2) by auto  } note * = this
himmelma@33175
  4807
  thus ?th4 unfolding subset_eq and Ball_def and mem_interval apply auto apply (erule_tac x=ia in allE, simp)+ by (erule_tac x=i in allE, erule_tac x=i in allE, simp)+
himmelma@33175
  4808
qed
himmelma@33175
  4809
hoelzl@34291
  4810
lemma disjoint_interval: fixes a::"real^'n" shows
himmelma@33175
  4811
  "{a .. b} \<inter> {c .. d} = {} \<longleftrightarrow> (\<exists>i. (b$i < a$i \<or> d$i < c$i \<or> b$i < c$i \<or> d$i < a$i))" (is ?th1) and
himmelma@33175
  4812
  "{a .. b} \<inter> {c<..<d} = {} \<longleftrightarrow> (\<exists>i. (b$i < a$i \<or> d$i \<le> c$i \<or> b$i \<le> c$i \<or> d$i \<le> a$i))" (is ?th2) and
himmelma@33175
  4813
  "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> (\<exists>i. (b$i \<le> a$i \<or> d$i < c$i \<or> b$i \<le> c$i \<or> d$i \<le> a$i))" (is ?th3) and
himmelma@33175
  4814
  "{a<..<b} \<inter> {c<..<d} = {} \<longleftrightarrow> (\<exists>i. (b$i \<le> a$i \<or> d$i \<le> c$i \<or> b$i \<le> c$i \<or> d$i \<le> a$i))" (is ?th4)
himmelma@33175
  4815
proof-
himmelma@33175
  4816
  let ?z = "(\<chi> i. ((max (a$i) (c$i)) + (min (b$i) (d$i))) / 2)::real^'n"
himmelma@33175
  4817
  show ?th1 ?th2 ?th3 ?th4
himmelma@33175
  4818
  unfolding expand_set_eq and Int_iff and empty_iff and mem_interval and all_conj_distrib[THEN sym] and eq_False
himmelma@33175
  4819
  apply (auto elim!: allE[where x="?z"])
himmelma@33175
  4820
  apply ((rule_tac x=x in exI, force) | (rule_tac x=i in exI, force))+
himmelma@33175
  4821
  done
himmelma@33175
  4822
qed
himmelma@33175
  4823
hoelzl@34291
  4824
lemma inter_interval: fixes a :: "'a::linorder^'n" shows
himmelma@33175
  4825
 "{a .. b} \<inter> {c .. d} =  {(\<chi> i. max (a$i) (c$i)) .. (\<chi> i. min (b$i) (d$i))}"
himmelma@33175
  4826
  unfolding expand_set_eq and Int_iff and mem_interval
himmelma@33175
  4827
  by (auto simp add: less_divide_eq_number_of1 intro!: bexI)
himmelma@33175
  4828
himmelma@33175
  4829
(* Moved interval_open_subset_closed a bit upwards *)
himmelma@33175
  4830
himmelma@33175
  4831
lemma open_interval_lemma: fixes x :: "real" shows
himmelma@33175
  4832
 "a < x \<Longrightarrow> x < b ==> (\<exists>d>0. \<forall>x'. abs(x' - x) < d --> a < x' \<and> x' < b)"
himmelma@33175
  4833
  by(rule_tac x="min (x - a) (b - x)" in exI, auto)
himmelma@33175
  4834
hoelzl@34951
  4835
lemma open_interval[intro]: fixes a :: "real^'n" shows "open {a<..<b}"
himmelma@33175
  4836
proof-
himmelma@33175
  4837
  { fix x assume x:"x\<in>{a<..<b}"
himmelma@33175
  4838
    { fix i
himmelma@33175
  4839
      have "\<exists>d>0. \<forall>x'. abs (x' - (x$i)) < d \<longrightarrow> a$i < x' \<and> x' < b$i"
himmelma@33175
  4840
        using x[unfolded mem_interval, THEN spec[where x=i]]
himmelma@33175
  4841
        using open_interval_lemma[of "a$i" "x$i" "b$i"] by auto  }
himmelma@33175
  4842
himmelma@33175
  4843
    hence "\<forall>i. \<exists>d>0. \<forall>x'. abs (x' - (x$i)) < d \<longrightarrow> a$i < x' \<and> x' < b$i" by auto
himmelma@33175
  4844
    then obtain d where d:"\<forall>i. 0 < d i \<and> (\<forall>x'. \<bar>x' - x $ i\<bar> < d i \<longrightarrow> a $ i < x' \<and> x' < b $ i)"
himmelma@33175
  4845
      using bchoice[of "UNIV" "\<lambda>i d. d>0 \<and> (\<forall>x'. \<bar>x' - x $ i\<bar> < d \<longrightarrow> a $ i < x' \<and> x' < b $ i)"] by auto
himmelma@33175
  4846
himmelma@33175
  4847
    let ?d = "Min (range d)"
himmelma@33175
  4848
    have **:"finite (range d)" "range d \<noteq> {}" by auto
himmelma@33175
  4849
    have "?d>0" unfolding Min_gr_iff[OF **] using d by auto
himmelma@33175
  4850
    moreover
himmelma@33175
  4851
    { fix x' assume as:"dist x' x < ?d"
himmelma@33175
  4852
      { fix i
himmelma@33175
  4853
        have "\<bar>x'$i - x $ i\<bar> < d i"
himmelma@33175
  4854
          using norm_bound_component_lt[OF as[unfolded dist_norm], of i]
himmelma@33175
  4855
          unfolding vector_minus_component and Min_gr_iff[OF **] by auto
himmelma@33175
  4856
        hence "a $ i < x' $ i" "x' $ i < b $ i" using d[THEN spec[where x=i]] by auto  }
himmelma@33175
  4857
      hence "a < x' \<and> x' < b" unfolding vector_less_def by auto  }
himmelma@33175
  4858
    ultimately have "\<exists>e>0. \<forall>x'. dist x' x < e \<longrightarrow> x' \<in> {a<..<b}" by (auto, rule_tac x="?d" in exI, simp)
himmelma@33175
  4859
  }
himmelma@33175
  4860
  thus ?thesis unfolding open_dist using open_interval_lemma by auto
himmelma@33175
  4861
qed
himmelma@33175
  4862
hoelzl@34951
  4863
lemma open_interval_real[intro]: fixes a :: "real" shows "open {a<..<b}"
hoelzl@33711
  4864
  using open_interval[of "vec1 a" "vec1 b"] unfolding open_contains_ball
hoelzl@33711
  4865
  apply-apply(rule,erule_tac x="vec1 x" in ballE) apply(erule exE,rule_tac x=e in exI)
hoelzl@33711
  4866
  unfolding subset_eq mem_ball apply(rule) defer apply(rule,erule conjE,erule_tac x="vec1 xa" in ballE)
hoelzl@33711
  4867
  by(auto simp add: vec1_dest_vec1_simps vector_less_def forall_1) 
hoelzl@33711
  4868
hoelzl@34951
  4869
lemma closed_interval[intro]: fixes a :: "real^'n" shows "closed {a .. b}"
himmelma@33175
  4870
proof-
himmelma@33175
  4871
  { fix x i assume as:"\<forall>e>0. \<exists>x'\<in>{a..b}. x' \<noteq> x \<and> dist x' x < e"(* and xab:"a$i > x$i \<or> b$i < x$i"*)
himmelma@33175
  4872
    { assume xa:"a$i > x$i"
himmelma@33175
  4873
      with as obtain y where y:"y\<in>{a..b}" "y \<noteq> x" "dist y x < a$i - x$i" by(erule_tac x="a$i - x$i" in allE)auto
himmelma@33175
  4874
      hence False unfolding mem_interval and dist_norm
himmelma@33175
  4875
        using component_le_norm[of "y-x" i, unfolded vector_minus_component] and xa by(auto elim!: allE[where x=i])
himmelma@33175
  4876
    } hence "a$i \<le> x$i" by(rule ccontr)auto
himmelma@33175
  4877
    moreover
himmelma@33175
  4878
    { assume xb:"b$i < x$i"
himmelma@33175
  4879
      with as obtain y where y:"y\<in>{a..b}" "y \<noteq> x" "dist y x < x$i - b$i" by(erule_tac x="x$i - b$i" in allE)auto
himmelma@33175
  4880
      hence False unfolding mem_interval and dist_norm
himmelma@33175
  4881
        using component_le_norm[of "y-x" i, unfolded vector_minus_component] and xb by(auto elim!: allE[where x=i])
himmelma@33175
  4882
    } hence "x$i \<le> b$i" by(rule ccontr)auto
himmelma@33175
  4883
    ultimately
himmelma@33175
  4884
    have "a $ i \<le> x $ i \<and> x $ i \<le> b $ i" by auto }
himmelma@33175
  4885
  thus ?thesis unfolding closed_limpt islimpt_approachable mem_interval by auto
himmelma@33175
  4886
qed
himmelma@33175
  4887
hoelzl@34951
  4888
lemma interior_closed_interval[intro]: fixes a :: "real^'n" shows
himmelma@33175
  4889
 "interior {a .. b} = {a<..<b}" (is "?L = ?R")
himmelma@33175
  4890
proof(rule subset_antisym)
himmelma@33175
  4891
  show "?R \<subseteq> ?L" using interior_maximal[OF interval_open_subset_closed open_interval] by auto
himmelma@33175
  4892
next
himmelma@33175
  4893
  { fix x assume "\<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> {a..b}"
himmelma@33175
  4894
    then obtain s where s:"open s" "x \<in> s" "s \<subseteq> {a..b}" by auto
himmelma@33175
  4895
    then obtain e where "e>0" and e:"\<forall>x'. dist x' x < e \<longrightarrow> x' \<in> {a..b}" unfolding open_dist and subset_eq by auto
himmelma@33175
  4896
    { fix i
himmelma@33175
  4897
      have "dist (x - (e / 2) *\<^sub>R basis i) x < e"
himmelma@33175
  4898
           "dist (x + (e / 2) *\<^sub>R basis i) x < e"
himmelma@33175
  4899
        unfolding dist_norm apply auto
himmelma@33175
  4900
        unfolding norm_minus_cancel using norm_basis[of i] and `e>0` by auto
himmelma@33175
  4901
      hence "a $ i \<le> (x - (e / 2) *\<^sub>R basis i) $ i"
himmelma@33175
  4902
                    "(x + (e / 2) *\<^sub>R basis i) $ i \<le> b $ i"
himmelma@33175
  4903
        using e[THEN spec[where x="x - (e/2) *\<^sub>R basis i"]]
himmelma@33175
  4904
        and   e[THEN spec[where x="x + (e/2) *\<^sub>R basis i"]]
himmelma@33175
  4905
        unfolding mem_interval by (auto elim!: allE[where x=i])
himmelma@33175
  4906
      hence "a $ i < x $ i" and "x $ i < b $ i"
himmelma@33175
  4907
        unfolding vector_minus_component and vector_add_component
himmelma@33175
  4908
        unfolding vector_smult_component and basis_component using `e>0` by auto   }
himmelma@33175
  4909
    hence "x \<in> {a<..<b}" unfolding mem_interval by auto  }
himmelma@33175
  4910
  thus "?L \<subseteq> ?R" unfolding interior_def and subset_eq by auto
himmelma@33175
  4911
qed
himmelma@33175
  4912
hoelzl@34291
  4913
lemma bounded_closed_interval: fixes a :: "real^'n" shows
himmelma@33175
  4914
 "bounded {a .. b}"
himmelma@33175
  4915
proof-
himmelma@33175
  4916
  let ?b = "\<Sum>i\<in>UNIV. \<bar>a$i\<bar> + \<bar>b$i\<bar>"
himmelma@33175
  4917
  { fix x::"real^'n" assume x:"\<forall>i. a $ i \<le> x $ i \<and> x $ i \<le> b $ i"
himmelma@33175
  4918
    { fix i
himmelma@33175
  4919
      have "\<bar>x$i\<bar> \<le> \<bar>a$i\<bar> + \<bar>b$i\<bar>" using x[THEN spec[where x=i]] by auto  }
himmelma@33175
  4920
    hence "(\<Sum>i\<in>UNIV. \<bar>x $ i\<bar>) \<le> ?b" by(rule setsum_mono)
himmelma@33175
  4921
    hence "norm x \<le> ?b" using norm_le_l1[of x] by auto  }
himmelma@33175
  4922
  thus ?thesis unfolding interval and bounded_iff by auto
himmelma@33175
  4923
qed
himmelma@33175
  4924
hoelzl@34291
  4925
lemma bounded_interval: fixes a :: "real^'n" shows
himmelma@33175
  4926
 "bounded {a .. b} \<and> bounded {a<..<b}"
himmelma@33175
  4927
  using bounded_closed_interval[of a b]
himmelma@33175
  4928
  using interval_open_subset_closed[of a b]
himmelma@33175
  4929
  using bounded_subset[of "{a..b}" "{a<..<b}"]
himmelma@33175
  4930
  by simp
himmelma@33175
  4931
hoelzl@34291
  4932
lemma not_interval_univ: fixes a :: "real^'n" shows
himmelma@33175
  4933
 "({a .. b} \<noteq> UNIV) \<and> ({a<..<b} \<noteq> UNIV)"
himmelma@33175
  4934
  using bounded_interval[of a b]
himmelma@33175
  4935
  by auto
himmelma@33175
  4936
hoelzl@34291
  4937
lemma compact_interval: fixes a :: "real^'n" shows
himmelma@33175
  4938
 "compact {a .. b}"
himmelma@33175
  4939
  using bounded_closed_imp_compact using bounded_interval[of a b] using closed_interval[of a b] by auto
himmelma@33175
  4940
hoelzl@34291
  4941
lemma open_interval_midpoint: fixes a :: "real^'n"
himmelma@33175
  4942
  assumes "{a<..<b} \<noteq> {}" shows "((1/2) *\<^sub>R (a + b)) \<in> {a<..<b}"
himmelma@33175
  4943
proof-
himmelma@33175
  4944
  { fix i
himmelma@33175
  4945
    have "a $ i < ((1 / 2) *\<^sub>R (a + b)) $ i \<and> ((1 / 2) *\<^sub>R (a + b)) $ i < b $ i"
himmelma@33175
  4946
      using assms[unfolded interval_ne_empty, THEN spec[where x=i]]
himmelma@33175
  4947
      unfolding vector_smult_component and vector_add_component
himmelma@33175
  4948
      by(auto simp add: less_divide_eq_number_of1)  }
himmelma@33175
  4949
  thus ?thesis unfolding mem_interval by auto
himmelma@33175
  4950
qed
himmelma@33175
  4951
hoelzl@34291
  4952
lemma open_closed_interval_convex: fixes x :: "real^'n"
himmelma@33175
  4953
  assumes x:"x \<in> {a<..<b}" and y:"y \<in> {a .. b}" and e:"0 < e" "e \<le> 1"
himmelma@33175
  4954
  shows "(e *\<^sub>R x + (1 - e) *\<^sub>R y) \<in> {a<..<b}"
himmelma@33175
  4955
proof-
himmelma@33175
  4956
  { fix i
himmelma@33175
  4957
    have "a $ i = e * a$i + (1 - e) * a$i" unfolding left_diff_distrib by simp
himmelma@33175
  4958
    also have "\<dots> < e * x $ i + (1 - e) * y $ i" apply(rule add_less_le_mono)
himmelma@33175
  4959
      using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all
himmelma@33175
  4960
      using x unfolding mem_interval  apply simp
himmelma@33175
  4961
      using y unfolding mem_interval  apply simp
himmelma@33175
  4962
      done
himmelma@33175
  4963
    finally have "a $ i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) $ i" by auto
himmelma@33175
  4964
    moreover {
himmelma@33175
  4965
    have "b $ i = e * b$i + (1 - e) * b$i" unfolding left_diff_distrib by simp
himmelma@33175
  4966
    also have "\<dots> > e * x $ i + (1 - e) * y $ i" apply(rule add_less_le_mono)
himmelma@33175
  4967
      using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all
himmelma@33175
  4968
      using x unfolding mem_interval  apply simp
himmelma@33175
  4969
      using y unfolding mem_interval  apply simp
himmelma@33175
  4970
      done
himmelma@33175
  4971
    finally have "(e *\<^sub>R x + (1 - e) *\<^sub>R y) $ i < b $ i" by auto
himmelma@33175
  4972
    } ultimately have "a $ i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) $ i \<and> (e *\<^sub>R x + (1 - e) *\<^sub>R y) $ i < b $ i" by auto }
himmelma@33175
  4973
  thus ?thesis unfolding mem_interval by auto
himmelma@33175
  4974
qed
himmelma@33175
  4975
hoelzl@34291
  4976
lemma closure_open_interval: fixes a :: "real^'n"
himmelma@33175
  4977
  assumes "{a<..<b} \<noteq> {}"
himmelma@33175
  4978
  shows "closure {a<..<b} = {a .. b}"
himmelma@33175
  4979
proof-
himmelma@33175
  4980
  have ab:"a < b" using assms[unfolded interval_ne_empty] unfolding vector_less_def by auto
himmelma@33175
  4981
  let ?c = "(1 / 2) *\<^sub>R (a + b)"
himmelma@33175
  4982
  { fix x assume as:"x \<in> {a .. b}"
himmelma@33175
  4983
    def f == "\<lambda>n::nat. x + (inverse (real n + 1)) *\<^sub>R (?c - x)"
himmelma@33175
  4984
    { fix n assume fn:"f n < b \<longrightarrow> a < f n \<longrightarrow> f n = x" and xc:"x \<noteq> ?c"
himmelma@33175
  4985
      have *:"0 < inverse (real n + 1)" "inverse (real n + 1) \<le> 1" unfolding inverse_le_1_iff by auto
himmelma@33175
  4986
      have "(inverse (real n + 1)) *\<^sub>R ((1 / 2) *\<^sub>R (a + b)) + (1 - inverse (real n + 1)) *\<^sub>R x =
himmelma@33175
  4987
        x + (inverse (real n + 1)) *\<^sub>R (((1 / 2) *\<^sub>R (a + b)) - x)"
himmelma@33175
  4988
        by (auto simp add: algebra_simps)
himmelma@33175
  4989
      hence "f n < b" and "a < f n" using open_closed_interval_convex[OF open_interval_midpoint[OF assms] as *] unfolding f_def by auto
himmelma@33175
  4990
      hence False using fn unfolding f_def using xc by(auto simp add: vector_mul_lcancel vector_ssub_ldistrib)  }
himmelma@33175
  4991
    moreover
himmelma@33175
  4992
    { assume "\<not> (f ---> x) sequentially"
himmelma@33175
  4993
      { fix e::real assume "e>0"
himmelma@33175
  4994
        hence "\<exists>N::nat. inverse (real (N + 1)) < e" using real_arch_inv[of e] apply (auto simp add: Suc_pred') apply(rule_tac x="n - 1" in exI) by auto
himmelma@33175
  4995
        then obtain N::nat where "inverse (real (N + 1)) < e" by auto
himmelma@33175
  4996
        hence "\<forall>n\<ge>N. inverse (real n + 1) < e" by (auto, metis Suc_le_mono le_SucE less_imp_inverse_less nat_le_real_less order_less_trans real_of_nat_Suc real_of_nat_Suc_gt_zero)
himmelma@33175
  4997
        hence "\<exists>N::nat. \<forall>n\<ge>N. inverse (real n + 1) < e" by auto  }
himmelma@33175
  4998
      hence "((\<lambda>n. inverse (real n + 1)) ---> 0) sequentially"
himmelma@33175
  4999
        unfolding Lim_sequentially by(auto simp add: dist_norm)
himmelma@33175
  5000
      hence "(f ---> x) sequentially" unfolding f_def
himmelma@33175
  5001
        using Lim_add[OF Lim_const, of "\<lambda>n::nat. (inverse (real n + 1)) *\<^sub>R ((1 / 2) *\<^sub>R (a + b) - x)" 0 sequentially x]
himmelma@33175
  5002
        using Lim_vmul[of "\<lambda>n::nat. inverse (real n + 1)" 0 sequentially "((1 / 2) *\<^sub>R (a + b) - x)"] by auto  }
himmelma@33175
  5003
    ultimately have "x \<in> closure {a<..<b}"
himmelma@33175
  5004
      using as and open_interval_midpoint[OF assms] unfolding closure_def unfolding islimpt_sequential by(cases "x=?c")auto  }
himmelma@33175
  5005
  thus ?thesis using closure_minimal[OF interval_open_subset_closed closed_interval, of a b] by blast
himmelma@33175
  5006
qed
himmelma@33175
  5007
hoelzl@34291
  5008
lemma bounded_subset_open_interval_symmetric: fixes s::"(real^'n) set"
himmelma@33175
  5009
  assumes "bounded s"  shows "\<exists>a. s \<subseteq> {-a<..<a}"
himmelma@33175
  5010
proof-
himmelma@33175
  5011
  obtain b where "b>0" and b:"\<forall>x\<in>s. norm x \<le> b" using assms[unfolded bounded_pos] by auto
himmelma@33175
  5012
  def a \<equiv> "(\<chi> i. b+1)::real^'n"
himmelma@33175
  5013
  { fix x assume "x\<in>s"
himmelma@33175
  5014
    fix i
himmelma@33175
  5015
    have "(-a)$i < x$i" and "x$i < a$i" using b[THEN bspec[where x=x], OF `x\<in>s`] and component_le_norm[of x i]
himmelma@33175
  5016
      unfolding vector_uminus_component and a_def and Cart_lambda_beta by auto
himmelma@33175
  5017
  }
himmelma@33175
  5018
  thus ?thesis by(auto intro: exI[where x=a] simp add: vector_less_def)
himmelma@33175
  5019
qed
himmelma@33175
  5020
himmelma@33175
  5021
lemma bounded_subset_open_interval:
hoelzl@34291
  5022
  fixes s :: "(real ^ 'n) set"
himmelma@33175
  5023
  shows "bounded s ==> (\<exists>a b. s \<subseteq> {a<..<b})"
himmelma@33175
  5024
  by (auto dest!: bounded_subset_open_interval_symmetric)
himmelma@33175
  5025
himmelma@33175
  5026
lemma bounded_subset_closed_interval_symmetric:
hoelzl@34291
  5027
  fixes s :: "(real ^ 'n) set"
himmelma@33175
  5028
  assumes "bounded s" shows "\<exists>a. s \<subseteq> {-a .. a}"
himmelma@33175
  5029
proof-
himmelma@33175
  5030
  obtain a where "s \<subseteq> {- a<..<a}" using bounded_subset_open_interval_symmetric[OF assms] by auto
himmelma@33175
  5031
  thus ?thesis using interval_open_subset_closed[of "-a" a] by auto
himmelma@33175
  5032
qed
himmelma@33175
  5033
himmelma@33175
  5034
lemma bounded_subset_closed_interval:
hoelzl@34291
  5035
  fixes s :: "(real ^ 'n) set"
himmelma@33175
  5036
  shows "bounded s ==> (\<exists>a b. s \<subseteq> {a .. b})"
himmelma@33175
  5037
  using bounded_subset_closed_interval_symmetric[of s] by auto
himmelma@33175
  5038
himmelma@33175
  5039
lemma frontier_closed_interval:
himmelma@33175
  5040
  fixes a b :: "real ^ _"
himmelma@33175
  5041
  shows "frontier {a .. b} = {a .. b} - {a<..<b}"
himmelma@33175
  5042
  unfolding frontier_def unfolding interior_closed_interval and closure_closed[OF closed_interval] ..
himmelma@33175
  5043
himmelma@33175
  5044
lemma frontier_open_interval:
himmelma@33175
  5045
  fixes a b :: "real ^ _"
himmelma@33175
  5046
  shows "frontier {a<..<b} = (if {a<..<b} = {} then {} else {a .. b} - {a<..<b})"
himmelma@33175
  5047
proof(cases "{a<..<b} = {}")
himmelma@33175
  5048
  case True thus ?thesis using frontier_empty by auto
himmelma@33175
  5049
next
himmelma@33175
  5050
  case False thus ?thesis unfolding frontier_def and closure_open_interval[OF False] and interior_open[OF open_interval] by auto
himmelma@33175
  5051
qed
himmelma@33175
  5052
hoelzl@34291
  5053
lemma inter_interval_mixed_eq_empty: fixes a :: "real^'n"
himmelma@33175
  5054
  assumes "{c<..<d} \<noteq> {}"  shows "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> {a<..<b} \<inter> {c<..<d} = {}"
himmelma@33175
  5055
  unfolding closure_open_interval[OF assms, THEN sym] unfolding open_inter_closure_eq_empty[OF open_interval] ..
himmelma@33175
  5056
himmelma@33175
  5057
himmelma@33175
  5058
(* Some special cases for intervals in R^1.                                  *)
himmelma@33175
  5059
himmelma@33175
  5060
lemma interval_cases_1: fixes x :: "real^1" shows
himmelma@33175
  5061
 "x \<in> {a .. b} ==> x \<in> {a<..<b} \<or> (x = a) \<or> (x = b)"
hoelzl@34951
  5062
  unfolding Cart_eq vector_less_def vector_le_def mem_interval by(auto simp del:dest_vec1_eq)
himmelma@33175
  5063
himmelma@33175
  5064
lemma in_interval_1: fixes x :: "real^1" shows
himmelma@33175
  5065
 "(x \<in> {a .. b} \<longleftrightarrow> dest_vec1 a \<le> dest_vec1 x \<and> dest_vec1 x \<le> dest_vec1 b) \<and>
himmelma@33175
  5066
  (x \<in> {a<..<b} \<longleftrightarrow> dest_vec1 a < dest_vec1 x \<and> dest_vec1 x < dest_vec1 b)"
hoelzl@34951
  5067
  unfolding Cart_eq vector_less_def vector_le_def mem_interval by(auto simp del:dest_vec1_eq)
himmelma@33175
  5068
himmelma@33175
  5069
lemma interval_eq_empty_1: fixes a :: "real^1" shows
himmelma@33175
  5070
  "{a .. b} = {} \<longleftrightarrow> dest_vec1 b < dest_vec1 a"
himmelma@33175
  5071
  "{a<..<b} = {} \<longleftrightarrow> dest_vec1 b \<le> dest_vec1 a"
hoelzl@34951
  5072
  unfolding interval_eq_empty and ex_1 by auto
himmelma@33175
  5073
himmelma@33175
  5074
lemma subset_interval_1: fixes a :: "real^1" shows
himmelma@33175
  5075
 "({a .. b} \<subseteq> {c .. d} \<longleftrightarrow>  dest_vec1 b < dest_vec1 a \<or>
himmelma@33175
  5076
                dest_vec1 c \<le> dest_vec1 a \<and> dest_vec1 a \<le> dest_vec1 b \<and> dest_vec1 b \<le> dest_vec1 d)"
himmelma@33175
  5077
 "({a .. b} \<subseteq> {c<..<d} \<longleftrightarrow>  dest_vec1 b < dest_vec1 a \<or>
himmelma@33175
  5078
                dest_vec1 c < dest_vec1 a \<and> dest_vec1 a \<le> dest_vec1 b \<and> dest_vec1 b < dest_vec1 d)"
himmelma@33175
  5079
 "({a<..<b} \<subseteq> {c .. d} \<longleftrightarrow>  dest_vec1 b \<le> dest_vec1 a \<or>
himmelma@33175
  5080
                dest_vec1 c \<le> dest_vec1 a \<and> dest_vec1 a < dest_vec1 b \<and> dest_vec1 b \<le> dest_vec1 d)"
himmelma@33175
  5081
 "({a<..<b} \<subseteq> {c<..<d} \<longleftrightarrow> dest_vec1 b \<le> dest_vec1 a \<or>
himmelma@33175
  5082
                dest_vec1 c \<le> dest_vec1 a \<and> dest_vec1 a < dest_vec1 b \<and> dest_vec1 b \<le> dest_vec1 d)"
hoelzl@34951
  5083
  unfolding subset_interval[of a b c d] unfolding forall_1 by auto
himmelma@33175
  5084
himmelma@33175
  5085
lemma eq_interval_1: fixes a :: "real^1" shows
himmelma@33175
  5086
 "{a .. b} = {c .. d} \<longleftrightarrow>
himmelma@33175
  5087
          dest_vec1 b < dest_vec1 a \<and> dest_vec1 d < dest_vec1 c \<or>
himmelma@33175
  5088
          dest_vec1 a = dest_vec1 c \<and> dest_vec1 b = dest_vec1 d"
hoelzl@34951
  5089
unfolding set_eq_subset[of "{a .. b}" "{c .. d}"]
hoelzl@34951
  5090
unfolding subset_interval_1(1)[of a b c d]
hoelzl@34951
  5091
unfolding subset_interval_1(1)[of c d a b]
hoelzl@34951
  5092
by auto
himmelma@33175
  5093
himmelma@33175
  5094
lemma disjoint_interval_1: fixes a :: "real^1" shows
himmelma@33175
  5095
  "{a .. b} \<inter> {c .. d} = {} \<longleftrightarrow> dest_vec1 b < dest_vec1 a \<or> dest_vec1 d < dest_vec1 c  \<or>  dest_vec1 b < dest_vec1 c \<or> dest_vec1 d < dest_vec1 a"
himmelma@33175
  5096
  "{a .. b} \<inter> {c<..<d} = {} \<longleftrightarrow> dest_vec1 b < dest_vec1 a \<or> dest_vec1 d \<le> dest_vec1 c  \<or>  dest_vec1 b \<le> dest_vec1 c \<or> dest_vec1 d \<le> dest_vec1 a"
himmelma@33175
  5097
  "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> dest_vec1 b \<le> dest_vec1 a \<or> dest_vec1 d < dest_vec1 c  \<or>  dest_vec1 b \<le> dest_vec1 c \<or> dest_vec1 d \<le> dest_vec1 a"
himmelma@33175
  5098
  "{a<..<b} \<inter> {c<..<d} = {} \<longleftrightarrow> dest_vec1 b \<le> dest_vec1 a \<or> dest_vec1 d \<le> dest_vec1 c  \<or>  dest_vec1 b \<le> dest_vec1 c \<or> dest_vec1 d \<le> dest_vec1 a"
hoelzl@34951
  5099
  unfolding disjoint_interval and ex_1 by auto
himmelma@33175
  5100
himmelma@33175
  5101
lemma open_closed_interval_1: fixes a :: "real^1" shows
himmelma@33175
  5102
 "{a<..<b} = {a .. b} - {a, b}"
hoelzl@34951
  5103
  unfolding expand_set_eq apply simp unfolding vector_less_def and vector_le_def and forall_1 and dest_vec1_eq[THEN sym] by(auto simp del:dest_vec1_eq)
himmelma@33175
  5104
himmelma@33175
  5105
lemma closed_open_interval_1: "dest_vec1 (a::real^1) \<le> dest_vec1 b ==> {a .. b} = {a<..<b} \<union> {a,b}"
hoelzl@34951
  5106
  unfolding expand_set_eq apply simp unfolding vector_less_def and vector_le_def and forall_1 and dest_vec1_eq[THEN sym] by(auto simp del:dest_vec1_eq)
himmelma@33175
  5107
himmelma@33175
  5108
(* Some stuff for half-infinite intervals too; FIXME: notation?  *)
himmelma@33175
  5109
hoelzl@34291
  5110
lemma closed_interval_left: fixes b::"real^'n"
himmelma@33175
  5111
  shows "closed {x::real^'n. \<forall>i. x$i \<le> b$i}"
himmelma@33175
  5112
proof-
himmelma@33175
  5113
  { fix i
himmelma@33175
  5114
    fix x::"real^'n" assume x:"\<forall>e>0. \<exists>x'\<in>{x. \<forall>i. x $ i \<le> b $ i}. x' \<noteq> x \<and> dist x' x < e"
himmelma@33175
  5115
    { assume "x$i > b$i"
himmelma@33175
  5116
      then obtain y where "y $ i \<le> b $ i"  "y \<noteq> x"  "dist y x < x$i - b$i" using x[THEN spec[where x="x$i - b$i"]] by auto
himmelma@33175
  5117
      hence False using component_le_norm[of "y - x" i] unfolding dist_norm and vector_minus_component by auto   }
himmelma@33175
  5118
    hence "x$i \<le> b$i" by(rule ccontr)auto  }
himmelma@33175
  5119
  thus ?thesis unfolding closed_limpt unfolding islimpt_approachable by blast
himmelma@33175
  5120
qed
himmelma@33175
  5121
hoelzl@34291
  5122
lemma closed_interval_right: fixes a::"real^'n"
himmelma@33175
  5123
  shows "closed {x::real^'n. \<forall>i. a$i \<le> x$i}"
himmelma@33175
  5124
proof-
himmelma@33175
  5125
  { fix i
himmelma@33175
  5126
    fix x::"real^'n" assume x:"\<forall>e>0. \<exists>x'\<in>{x. \<forall>i. a $ i \<le> x $ i}. x' \<noteq> x \<and> dist x' x < e"
himmelma@33175
  5127
    { assume "a$i > x$i"
himmelma@33175
  5128
      then obtain y where "a $ i \<le> y $ i"  "y \<noteq> x"  "dist y x < a$i - x$i" using x[THEN spec[where x="a$i - x$i"]] by auto
himmelma@33175
  5129
      hence False using component_le_norm[of "y - x" i] unfolding dist_norm and vector_minus_component by auto   }
himmelma@33175
  5130
    hence "a$i \<le> x$i" by(rule ccontr)auto  }
himmelma@33175
  5131
  thus ?thesis unfolding closed_limpt unfolding islimpt_approachable by blast
himmelma@33175
  5132
qed
himmelma@33175
  5133
himmelma@33175
  5134
subsection{* Intervals in general, including infinite and mixtures of open and closed. *}
himmelma@33175
  5135
himmelma@33175
  5136
definition "is_interval s \<longleftrightarrow> (\<forall>a\<in>s. \<forall>b\<in>s. \<forall>x. (\<forall>i. ((a$i \<le> x$i \<and> x$i \<le> b$i) \<or> (b$i \<le> x$i \<and> x$i \<le> a$i)))  \<longrightarrow> x \<in> s)"
himmelma@33175
  5137
hoelzl@34291
  5138
lemma is_interval_interval: "is_interval {a .. b::real^'n}" (is ?th1) "is_interval {a<..<b}" (is ?th2) proof - 
himmelma@33175
  5139
  have *:"\<And>x y z::real. x < y \<Longrightarrow> y < z \<Longrightarrow> x < z" by auto
himmelma@33175
  5140
  show ?th1 ?th2  unfolding is_interval_def mem_interval Ball_def atLeastAtMost_iff
himmelma@33175
  5141
    by(meson real_le_trans le_less_trans less_le_trans *)+ qed
himmelma@33175
  5142
himmelma@33175
  5143
lemma is_interval_empty:
himmelma@33175
  5144
 "is_interval {}"
himmelma@33175
  5145
  unfolding is_interval_def
himmelma@33175
  5146
  by simp
himmelma@33175
  5147
himmelma@33175
  5148
lemma is_interval_univ:
himmelma@33175
  5149
 "is_interval UNIV"
himmelma@33175
  5150
  unfolding is_interval_def
himmelma@33175
  5151
  by simp
himmelma@33175
  5152
himmelma@33175
  5153
subsection{* Closure of halfspaces and hyperplanes.                                    *}
himmelma@33175
  5154
himmelma@33175
  5155
lemma Lim_inner:
himmelma@33175
  5156
  assumes "(f ---> l) net"  shows "((\<lambda>y. inner a (f y)) ---> inner a l) net"
himmelma@33175
  5157
  by (intro tendsto_intros assms)
himmelma@33175
  5158
himmelma@33175
  5159
lemma continuous_at_inner: "continuous (at x) (inner a)"
himmelma@33175
  5160
  unfolding continuous_at by (intro tendsto_intros)
himmelma@33175
  5161
himmelma@33175
  5162
lemma continuous_on_inner:
himmelma@33175
  5163
  fixes s :: "'a::real_inner set"
himmelma@33175
  5164
  shows "continuous_on s (inner a)"
himmelma@33175
  5165
  unfolding continuous_on by (rule ballI) (intro tendsto_intros)
himmelma@33175
  5166
himmelma@33175
  5167
lemma closed_halfspace_le: "closed {x. inner a x \<le> b}"
himmelma@33175
  5168
proof-
himmelma@33175
  5169
  have "\<forall>x. continuous (at x) (inner a)"
himmelma@33175
  5170
    unfolding continuous_at by (rule allI) (intro tendsto_intros)
himmelma@33175
  5171
  hence "closed (inner a -` {..b})"
himmelma@33175
  5172
    using closed_real_atMost by (rule continuous_closed_vimage)
himmelma@33175
  5173
  moreover have "{x. inner a x \<le> b} = inner a -` {..b}" by auto
himmelma@33175
  5174
  ultimately show ?thesis by simp
himmelma@33175
  5175
qed
himmelma@33175
  5176
himmelma@33175
  5177
lemma closed_halfspace_ge: "closed {x. inner a x \<ge> b}"
himmelma@33175
  5178
  using closed_halfspace_le[of "-a" "-b"] unfolding inner_minus_left by auto
himmelma@33175
  5179
himmelma@33175
  5180
lemma closed_hyperplane: "closed {x. inner a x = b}"
himmelma@33175
  5181
proof-
himmelma@33175
  5182
  have "{x. inner a x = b} = {x. inner a x \<ge> b} \<inter> {x. inner a x \<le> b}" by auto
himmelma@33175
  5183
  thus ?thesis using closed_halfspace_le[of a b] and closed_halfspace_ge[of b a] using closed_Int by auto
himmelma@33175
  5184
qed
himmelma@33175
  5185
himmelma@33175
  5186
lemma closed_halfspace_component_le:
hoelzl@34291
  5187
  shows "closed {x::real^'n. x$i \<le> a}"
himmelma@33175
  5188
  using closed_halfspace_le[of "(basis i)::real^'n" a] unfolding inner_basis[OF assms] by auto
himmelma@33175
  5189
himmelma@33175
  5190
lemma closed_halfspace_component_ge:
hoelzl@34291
  5191
  shows "closed {x::real^'n. x$i \<ge> a}"
himmelma@33175
  5192
  using closed_halfspace_ge[of a "(basis i)::real^'n"] unfolding inner_basis[OF assms] by auto
himmelma@33175
  5193
himmelma@33175
  5194
text{* Openness of halfspaces.                                                   *}
himmelma@33175
  5195
himmelma@33175
  5196
lemma open_halfspace_lt: "open {x. inner a x < b}"
himmelma@33175
  5197
proof-
huffman@34099
  5198
  have "- {x. b \<le> inner a x} = {x. inner a x < b}" by auto
huffman@34099
  5199
  thus ?thesis using closed_halfspace_ge[unfolded closed_def, of b a] by auto
himmelma@33175
  5200
qed
himmelma@33175
  5201
himmelma@33175
  5202
lemma open_halfspace_gt: "open {x. inner a x > b}"
himmelma@33175
  5203
proof-
huffman@34099
  5204
  have "- {x. b \<ge> inner a x} = {x. inner a x > b}" by auto
huffman@34099
  5205
  thus ?thesis using closed_halfspace_le[unfolded closed_def, of a b] by auto
himmelma@33175
  5206
qed
himmelma@33175
  5207
himmelma@33175
  5208
lemma open_halfspace_component_lt:
hoelzl@34291
  5209
  shows "open {x::real^'n. x$i < a}"
himmelma@33175
  5210
  using open_halfspace_lt[of "(basis i)::real^'n" a] unfolding inner_basis[OF assms] by auto
himmelma@33175
  5211
himmelma@33175
  5212
lemma open_halfspace_component_gt:
hoelzl@34291
  5213
  shows "open {x::real^'n. x$i  > a}"
himmelma@33175
  5214
  using open_halfspace_gt[of a "(basis i)::real^'n"] unfolding inner_basis[OF assms] by auto
himmelma@33175
  5215
himmelma@33175
  5216
text{* This gives a simple derivation of limit component bounds.                 *}
himmelma@33175
  5217
hoelzl@34291
  5218
lemma Lim_component_le: fixes f :: "'a \<Rightarrow> real^'n"
himmelma@33175
  5219
  assumes "(f ---> l) net" "\<not> (trivial_limit net)"  "eventually (\<lambda>x. f(x)$i \<le> b) net"
himmelma@33175
  5220
  shows "l$i \<le> b"
himmelma@33175
  5221
proof-
himmelma@33175
  5222
  { fix x have "x \<in> {x::real^'n. inner (basis i) x \<le> b} \<longleftrightarrow> x$i \<le> b" unfolding inner_basis by auto } note * = this
himmelma@33175
  5223
  show ?thesis using Lim_in_closed_set[of "{x. inner (basis i) x \<le> b}" f net l] unfolding *
himmelma@33175
  5224
    using closed_halfspace_le[of "(basis i)::real^'n" b] and assms(1,2,3) by auto
himmelma@33175
  5225
qed
himmelma@33175
  5226
hoelzl@34291
  5227
lemma Lim_component_ge: fixes f :: "'a \<Rightarrow> real^'n"
himmelma@33175
  5228
  assumes "(f ---> l) net"  "\<not> (trivial_limit net)"  "eventually (\<lambda>x. b \<le> (f x)$i) net"
himmelma@33175
  5229
  shows "b \<le> l$i"
himmelma@33175
  5230
proof-
himmelma@33175
  5231
  { fix x have "x \<in> {x::real^'n. inner (basis i) x \<ge> b} \<longleftrightarrow> x$i \<ge> b" unfolding inner_basis by auto } note * = this
himmelma@33175
  5232
  show ?thesis using Lim_in_closed_set[of "{x. inner (basis i) x \<ge> b}" f net l] unfolding *
himmelma@33175
  5233
    using closed_halfspace_ge[of b "(basis i)::real^'n"] and assms(1,2,3) by auto
himmelma@33175
  5234
qed
himmelma@33175
  5235
hoelzl@34291
  5236
lemma Lim_component_eq: fixes f :: "'a \<Rightarrow> real^'n"
himmelma@33175
  5237
  assumes net:"(f ---> l) net" "~(trivial_limit net)" and ev:"eventually (\<lambda>x. f(x)$i = b) net"
himmelma@33175
  5238
  shows "l$i = b"
himmelma@33175
  5239
  using ev[unfolded order_eq_iff eventually_and] using Lim_component_ge[OF net, of b i] and Lim_component_le[OF net, of i b] by auto
himmelma@33175
  5240
himmelma@33175
  5241
lemma Lim_drop_le: fixes f :: "'a \<Rightarrow> real^1" shows
himmelma@33175
  5242
  "(f ---> l) net \<Longrightarrow> ~(trivial_limit net) \<Longrightarrow> eventually (\<lambda>x. dest_vec1 (f x) \<le> b) net ==> dest_vec1 l \<le> b"
hoelzl@34951
  5243
  using Lim_component_le[of f l net 1 b] by auto
himmelma@33175
  5244
himmelma@33175
  5245
lemma Lim_drop_ge: fixes f :: "'a \<Rightarrow> real^1" shows
himmelma@33175
  5246
 "(f ---> l) net \<Longrightarrow> ~(trivial_limit net) \<Longrightarrow> eventually (\<lambda>x. b \<le> dest_vec1 (f x)) net ==> b \<le> dest_vec1 l"
hoelzl@34951
  5247
  using Lim_component_ge[of f l net b 1] by auto
himmelma@33175
  5248
himmelma@33175
  5249
text{* Limits relative to a union.                                               *}
himmelma@33175
  5250
himmelma@33175
  5251
lemma eventually_within_Un:
himmelma@33175
  5252
  "eventually P (net within (s \<union> t)) \<longleftrightarrow>
himmelma@33175
  5253
    eventually P (net within s) \<and> eventually P (net within t)"
himmelma@33175
  5254
  unfolding Limits.eventually_within
himmelma@33175
  5255
  by (auto elim!: eventually_rev_mp)
himmelma@33175
  5256
himmelma@33175
  5257
lemma Lim_within_union:
himmelma@33175
  5258
 "(f ---> l) (net within (s \<union> t)) \<longleftrightarrow>
himmelma@33175
  5259
  (f ---> l) (net within s) \<and> (f ---> l) (net within t)"
himmelma@33175
  5260
  unfolding tendsto_def
himmelma@33175
  5261
  by (auto simp add: eventually_within_Un)
himmelma@33175
  5262
himmelma@33175
  5263
lemma continuous_on_union:
himmelma@33175
  5264
  assumes "closed s" "closed t" "continuous_on s f" "continuous_on t f"
himmelma@33175
  5265
  shows "continuous_on (s \<union> t) f"
himmelma@33175
  5266
  using assms unfolding continuous_on unfolding Lim_within_union
himmelma@33175
  5267
  unfolding Lim unfolding trivial_limit_within unfolding closed_limpt by auto
himmelma@33175
  5268
himmelma@33175
  5269
lemma continuous_on_cases:
himmelma@33175
  5270
  assumes "closed s" "closed t" "continuous_on s f" "continuous_on t g"
himmelma@33175
  5271
          "\<forall>x. (x\<in>s \<and> \<not> P x) \<or> (x \<in> t \<and> P x) \<longrightarrow> f x = g x"
himmelma@33175
  5272
  shows "continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)"
himmelma@33175
  5273
proof-
himmelma@33175
  5274
  let ?h = "(\<lambda>x. if P x then f x else g x)"
himmelma@33175
  5275
  have "\<forall>x\<in>s. f x = (if P x then f x else g x)" using assms(5) by auto
himmelma@33175
  5276
  hence "continuous_on s ?h" using continuous_on_eq[of s f ?h] using assms(3) by auto
himmelma@33175
  5277
  moreover
himmelma@33175
  5278
  have "\<forall>x\<in>t. g x = (if P x then f x else g x)" using assms(5) by auto
himmelma@33175
  5279
  hence "continuous_on t ?h" using continuous_on_eq[of t g ?h] using assms(4) by auto
himmelma@33175
  5280
  ultimately show ?thesis using continuous_on_union[OF assms(1,2), of ?h] by auto
himmelma@33175
  5281
qed
himmelma@33175
  5282
himmelma@33175
  5283
himmelma@33175
  5284
text{* Some more convenient intermediate-value theorem formulations.             *}
himmelma@33175
  5285
himmelma@33175
  5286
lemma connected_ivt_hyperplane:
himmelma@33175
  5287
  assumes "connected s" "x \<in> s" "y \<in> s" "inner a x \<le> b" "b \<le> inner a y"
himmelma@33175
  5288
  shows "\<exists>z \<in> s. inner a z = b"
himmelma@33175
  5289
proof(rule ccontr)
himmelma@33175
  5290
  assume as:"\<not> (\<exists>z\<in>s. inner a z = b)"
himmelma@33175
  5291
  let ?A = "{x. inner a x < b}"
himmelma@33175
  5292
  let ?B = "{x. inner a x > b}"
himmelma@33175
  5293
  have "open ?A" "open ?B" using open_halfspace_lt and open_halfspace_gt by auto
himmelma@33175
  5294
  moreover have "?A \<inter> ?B = {}" by auto
himmelma@33175
  5295
  moreover have "s \<subseteq> ?A \<union> ?B" using as by auto
himmelma@33175
  5296
  ultimately show False using assms(1)[unfolded connected_def not_ex, THEN spec[where x="?A"], THEN spec[where x="?B"]] and assms(2-5) by auto
himmelma@33175
  5297
qed
himmelma@33175
  5298
hoelzl@34291
  5299
lemma connected_ivt_component: fixes x::"real^'n" shows
himmelma@33175
  5300
 "connected s \<Longrightarrow> x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> x$k \<le> a \<Longrightarrow> a \<le> y$k \<Longrightarrow> (\<exists>z\<in>s.  z$k = a)"
himmelma@33175
  5301
  using connected_ivt_hyperplane[of s x y "(basis k)::real^'n" a] by (auto simp add: inner_basis)
himmelma@33175
  5302
himmelma@33175
  5303
text{* Also more convenient formulations of monotone convergence.                *}
himmelma@33175
  5304
himmelma@33175
  5305
lemma bounded_increasing_convergent: fixes s::"nat \<Rightarrow> real^1"
himmelma@33175
  5306
  assumes "bounded {s n| n::nat. True}"  "\<forall>n. dest_vec1(s n) \<le> dest_vec1(s(Suc n))"
himmelma@33175
  5307
  shows "\<exists>l. (s ---> l) sequentially"
himmelma@33175
  5308
proof-
himmelma@33175
  5309
  obtain a where a:"\<forall>n. \<bar>dest_vec1 (s n)\<bar> \<le>  a" using assms(1)[unfolded bounded_iff abs_dest_vec1] by auto
himmelma@33175
  5310
  { fix m::nat
himmelma@33175
  5311
    have "\<And> n. n\<ge>m \<longrightarrow> dest_vec1 (s m) \<le> dest_vec1 (s n)"
himmelma@33175
  5312
      apply(induct_tac n) apply simp using assms(2) apply(erule_tac x="na" in allE) by(auto simp add: not_less_eq_eq)  }
himmelma@33175
  5313
  hence "\<forall>m n. m \<le> n \<longrightarrow> dest_vec1 (s m) \<le> dest_vec1 (s n)" by auto
himmelma@33175
  5314
  then obtain l where "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<bar>dest_vec1 (s n) - l\<bar> < e" using convergent_bounded_monotone[OF a] unfolding monoseq_def by auto
himmelma@33175
  5315
  thus ?thesis unfolding Lim_sequentially apply(rule_tac x="vec1 l" in exI)
hoelzl@34951
  5316
    unfolding dist_norm unfolding abs_dest_vec1  by auto
himmelma@33175
  5317
qed
himmelma@33175
  5318
himmelma@33175
  5319
subsection{* Basic homeomorphism definitions.                                          *}
himmelma@33175
  5320
himmelma@33175
  5321
definition "homeomorphism s t f g \<equiv>
himmelma@33175
  5322
     (\<forall>x\<in>s. (g(f x) = x)) \<and> (f ` s = t) \<and> continuous_on s f \<and>
himmelma@33175
  5323
     (\<forall>y\<in>t. (f(g y) = y)) \<and> (g ` t = s) \<and> continuous_on t g"
himmelma@33175
  5324
himmelma@33175
  5325
definition
himmelma@33175
  5326
  homeomorphic :: "'a::metric_space set \<Rightarrow> 'b::metric_space set \<Rightarrow> bool"
himmelma@33175
  5327
    (infixr "homeomorphic" 60) where
himmelma@33175
  5328
  homeomorphic_def: "s homeomorphic t \<equiv> (\<exists>f g. homeomorphism s t f g)"
himmelma@33175
  5329
himmelma@33175
  5330
lemma homeomorphic_refl: "s homeomorphic s"
himmelma@33175
  5331
  unfolding homeomorphic_def
himmelma@33175
  5332
  unfolding homeomorphism_def
himmelma@33175
  5333
  using continuous_on_id
himmelma@33175
  5334
  apply(rule_tac x = "(\<lambda>x. x)" in exI)
himmelma@33175
  5335
  apply(rule_tac x = "(\<lambda>x. x)" in exI)
himmelma@33175
  5336
  by blast
himmelma@33175
  5337
himmelma@33175
  5338
lemma homeomorphic_sym:
himmelma@33175
  5339
 "s homeomorphic t \<longleftrightarrow> t homeomorphic s"
himmelma@33175
  5340
unfolding homeomorphic_def
himmelma@33175
  5341
unfolding homeomorphism_def
paulson@33307
  5342
by blast 
himmelma@33175
  5343
himmelma@33175
  5344
lemma homeomorphic_trans:
himmelma@33175
  5345
  assumes "s homeomorphic t" "t homeomorphic u" shows "s homeomorphic u"
himmelma@33175
  5346
proof-
himmelma@33175
  5347
  obtain f1 g1 where fg1:"\<forall>x\<in>s. g1 (f1 x) = x"  "f1 ` s = t" "continuous_on s f1" "\<forall>y\<in>t. f1 (g1 y) = y" "g1 ` t = s" "continuous_on t g1"
himmelma@33175
  5348
    using assms(1) unfolding homeomorphic_def homeomorphism_def by auto
himmelma@33175
  5349
  obtain f2 g2 where fg2:"\<forall>x\<in>t. g2 (f2 x) = x"  "f2 ` t = u" "continuous_on t f2" "\<forall>y\<in>u. f2 (g2 y) = y" "g2 ` u = t" "continuous_on u g2"
himmelma@33175
  5350
    using assms(2) unfolding homeomorphic_def homeomorphism_def by auto
himmelma@33175
  5351
himmelma@33175
  5352
  { fix x assume "x\<in>s" hence "(g1 \<circ> g2) ((f2 \<circ> f1) x) = x" using fg1(1)[THEN bspec[where x=x]] and fg2(1)[THEN bspec[where x="f1 x"]] and fg1(2) by auto }
himmelma@33175
  5353
  moreover have "(f2 \<circ> f1) ` s = u" using fg1(2) fg2(2) by auto
himmelma@33175
  5354
  moreover have "continuous_on s (f2 \<circ> f1)" using continuous_on_compose[OF fg1(3)] and fg2(3) unfolding fg1(2) by auto
himmelma@33175
  5355
  moreover { fix y assume "y\<in>u" hence "(f2 \<circ> f1) ((g1 \<circ> g2) y) = y" using fg2(4)[THEN bspec[where x=y]] and fg1(4)[THEN bspec[where x="g2 y"]] and fg2(5) by auto }
himmelma@33175
  5356
  moreover have "(g1 \<circ> g2) ` u = s" using fg1(5) fg2(5) by auto
himmelma@33175
  5357
  moreover have "continuous_on u (g1 \<circ> g2)" using continuous_on_compose[OF fg2(6)] and fg1(6)  unfolding fg2(5) by auto
himmelma@33175
  5358
  ultimately show ?thesis unfolding homeomorphic_def homeomorphism_def apply(rule_tac x="f2 \<circ> f1" in exI) apply(rule_tac x="g1 \<circ> g2" in exI) by auto
himmelma@33175
  5359
qed
himmelma@33175
  5360
himmelma@33175
  5361
lemma homeomorphic_minimal:
himmelma@33175
  5362
 "s homeomorphic t \<longleftrightarrow>
himmelma@33175
  5363
    (\<exists>f g. (\<forall>x\<in>s. f(x) \<in> t \<and> (g(f(x)) = x)) \<and>
himmelma@33175
  5364
           (\<forall>y\<in>t. g(y) \<in> s \<and> (f(g(y)) = y)) \<and>
himmelma@33175
  5365
           continuous_on s f \<and> continuous_on t g)"
himmelma@33175
  5366
unfolding homeomorphic_def homeomorphism_def
himmelma@33175
  5367
apply auto apply (rule_tac x=f in exI) apply (rule_tac x=g in exI)
himmelma@33175
  5368
apply auto apply (rule_tac x=f in exI) apply (rule_tac x=g in exI) apply auto
himmelma@33175
  5369
unfolding image_iff
himmelma@33175
  5370
apply(erule_tac x="g x" in ballE) apply(erule_tac x="x" in ballE)
himmelma@33175
  5371
apply auto apply(rule_tac x="g x" in bexI) apply auto
himmelma@33175
  5372
apply(erule_tac x="f x" in ballE) apply(erule_tac x="x" in ballE)
himmelma@33175
  5373
apply auto apply(rule_tac x="f x" in bexI) by auto
himmelma@33175
  5374
himmelma@33175
  5375
subsection{* Relatively weak hypotheses if a set is compact.                           *}
himmelma@33175
  5376
himmelma@33175
  5377
definition "inv_on f s = (\<lambda>x. SOME y. y\<in>s \<and> f y = x)"
himmelma@33175
  5378
himmelma@33175
  5379
lemma assumes "inj_on f s" "x\<in>s"
himmelma@33175
  5380
  shows "inv_on f s (f x) = x"
himmelma@33175
  5381
 using assms unfolding inj_on_def inv_on_def by auto
himmelma@33175
  5382
himmelma@33175
  5383
lemma homeomorphism_compact:
himmelma@33175
  5384
  fixes f :: "'a::heine_borel \<Rightarrow> 'b::heine_borel"
himmelma@33175
  5385
    (* class constraint due to continuous_on_inverse *)
himmelma@33175
  5386
  assumes "compact s" "continuous_on s f"  "f ` s = t"  "inj_on f s"
himmelma@33175
  5387
  shows "\<exists>g. homeomorphism s t f g"
himmelma@33175
  5388
proof-
himmelma@33175
  5389
  def g \<equiv> "\<lambda>x. SOME y. y\<in>s \<and> f y = x"
himmelma@33175
  5390
  have g:"\<forall>x\<in>s. g (f x) = x" using assms(3) assms(4)[unfolded inj_on_def] unfolding g_def by auto
himmelma@33175
  5391
  { fix y assume "y\<in>t"
himmelma@33175
  5392
    then obtain x where x:"f x = y" "x\<in>s" using assms(3) by auto
himmelma@33175
  5393
    hence "g (f x) = x" using g by auto
himmelma@33175
  5394
    hence "f (g y) = y" unfolding x(1)[THEN sym] by auto  }
himmelma@33175
  5395
  hence g':"\<forall>x\<in>t. f (g x) = x" by auto
himmelma@33175
  5396
  moreover
himmelma@33175
  5397
  { fix x
himmelma@33175
  5398
    have "x\<in>s \<Longrightarrow> x \<in> g ` t" using g[THEN bspec[where x=x]] unfolding image_iff using assms(3) by(auto intro!: bexI[where x="f x"])
himmelma@33175
  5399
    moreover
himmelma@33175
  5400
    { assume "x\<in>g ` t"
himmelma@33175
  5401
      then obtain y where y:"y\<in>t" "g y = x" by auto
himmelma@33175
  5402
      then obtain x' where x':"x'\<in>s" "f x' = y" using assms(3) by auto
himmelma@33175
  5403
      hence "x \<in> s" unfolding g_def using someI2[of "\<lambda>b. b\<in>s \<and> f b = y" x' "\<lambda>x. x\<in>s"] unfolding y(2)[THEN sym] and g_def by auto }
himmelma@33175
  5404
    ultimately have "x\<in>s \<longleftrightarrow> x \<in> g ` t" by auto  }
himmelma@33175
  5405
  hence "g ` t = s" by auto
himmelma@33175
  5406
  ultimately
himmelma@33175
  5407
  show ?thesis unfolding homeomorphism_def homeomorphic_def
himmelma@33175
  5408
    apply(rule_tac x=g in exI) using g and assms(3) and continuous_on_inverse[OF assms(2,1), of g, unfolded assms(3)] and assms(2) by auto
himmelma@33175
  5409
qed
himmelma@33175
  5410
himmelma@33175
  5411
lemma homeomorphic_compact:
himmelma@33175
  5412
  fixes f :: "'a::heine_borel \<Rightarrow> 'b::heine_borel"
himmelma@33175
  5413
    (* class constraint due to continuous_on_inverse *)
himmelma@33175
  5414
  shows "compact s \<Longrightarrow> continuous_on s f \<Longrightarrow> (f ` s = t) \<Longrightarrow> inj_on f s
himmelma@33175
  5415
          \<Longrightarrow> s homeomorphic t"
himmelma@33175
  5416
  unfolding homeomorphic_def by(metis homeomorphism_compact)
himmelma@33175
  5417
himmelma@33175
  5418
text{* Preservation of topological properties.                                   *}
himmelma@33175
  5419
himmelma@33175
  5420
lemma homeomorphic_compactness:
himmelma@33175
  5421
 "s homeomorphic t ==> (compact s \<longleftrightarrow> compact t)"
himmelma@33175
  5422
unfolding homeomorphic_def homeomorphism_def
himmelma@33175
  5423
by (metis compact_continuous_image)
himmelma@33175
  5424
himmelma@33175
  5425
text{* Results on translation, scaling etc.                                      *}
himmelma@33175
  5426
himmelma@33175
  5427
lemma homeomorphic_scaling:
himmelma@33175
  5428
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  5429
  assumes "c \<noteq> 0"  shows "s homeomorphic ((\<lambda>x. c *\<^sub>R x) ` s)"
himmelma@33175
  5430
  unfolding homeomorphic_minimal
himmelma@33175
  5431
  apply(rule_tac x="\<lambda>x. c *\<^sub>R x" in exI)
himmelma@33175
  5432
  apply(rule_tac x="\<lambda>x. (1 / c) *\<^sub>R x" in exI)
himmelma@33175
  5433
  using assms apply auto
himmelma@33175
  5434
  using continuous_on_cmul[OF continuous_on_id] by auto
himmelma@33175
  5435
himmelma@33175
  5436
lemma homeomorphic_translation:
himmelma@33175
  5437
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  5438
  shows "s homeomorphic ((\<lambda>x. a + x) ` s)"
himmelma@33175
  5439
  unfolding homeomorphic_minimal
himmelma@33175
  5440
  apply(rule_tac x="\<lambda>x. a + x" in exI)
himmelma@33175
  5441
  apply(rule_tac x="\<lambda>x. -a + x" in exI)
himmelma@33175
  5442
  using continuous_on_add[OF continuous_on_const continuous_on_id] by auto
himmelma@33175
  5443
himmelma@33175
  5444
lemma homeomorphic_affinity:
himmelma@33175
  5445
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  5446
  assumes "c \<noteq> 0"  shows "s homeomorphic ((\<lambda>x. a + c *\<^sub>R x) ` s)"
himmelma@33175
  5447
proof-
himmelma@33175
  5448
  have *:"op + a ` op *\<^sub>R c ` s = (\<lambda>x. a + c *\<^sub>R x) ` s" by auto
himmelma@33175
  5449
  show ?thesis
himmelma@33175
  5450
    using homeomorphic_trans
himmelma@33175
  5451
    using homeomorphic_scaling[OF assms, of s]
himmelma@33175
  5452
    using homeomorphic_translation[of "(\<lambda>x. c *\<^sub>R x) ` s" a] unfolding * by auto
himmelma@33175
  5453
qed
himmelma@33175
  5454
himmelma@33175
  5455
lemma homeomorphic_balls:
himmelma@33175
  5456
  fixes a b ::"'a::real_normed_vector" (* FIXME: generalize to metric_space *)
himmelma@33175
  5457
  assumes "0 < d"  "0 < e"
himmelma@33175
  5458
  shows "(ball a d) homeomorphic  (ball b e)" (is ?th)
himmelma@33175
  5459
        "(cball a d) homeomorphic (cball b e)" (is ?cth)
himmelma@33175
  5460
proof-
himmelma@33175
  5461
  have *:"\<bar>e / d\<bar> > 0" "\<bar>d / e\<bar> >0" using assms using divide_pos_pos by auto
himmelma@33175
  5462
  show ?th unfolding homeomorphic_minimal
himmelma@33175
  5463
    apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
himmelma@33175
  5464
    apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
himmelma@33175
  5465
    using assms apply (auto simp add: dist_commute)
himmelma@33175
  5466
    unfolding dist_norm
himmelma@33175
  5467
    apply (auto simp add: pos_divide_less_eq mult_strict_left_mono)
himmelma@33175
  5468
    unfolding continuous_on
himmelma@33175
  5469
    by (intro ballI tendsto_intros, simp, assumption)+
himmelma@33175
  5470
next
himmelma@33175
  5471
  have *:"\<bar>e / d\<bar> > 0" "\<bar>d / e\<bar> >0" using assms using divide_pos_pos by auto
himmelma@33175
  5472
  show ?cth unfolding homeomorphic_minimal
himmelma@33175
  5473
    apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
himmelma@33175
  5474
    apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
himmelma@33175
  5475
    using assms apply (auto simp add: dist_commute)
himmelma@33175
  5476
    unfolding dist_norm
himmelma@33175
  5477
    apply (auto simp add: pos_divide_le_eq)
himmelma@33175
  5478
    unfolding continuous_on
himmelma@33175
  5479
    by (intro ballI tendsto_intros, simp, assumption)+
himmelma@33175
  5480
qed
himmelma@33175
  5481
himmelma@33175
  5482
text{* "Isometry" (up to constant bounds) of injective linear map etc.           *}
himmelma@33175
  5483
himmelma@33175
  5484
lemma cauchy_isometric:
hoelzl@34291
  5485
  fixes x :: "nat \<Rightarrow> real ^ 'n"
himmelma@33175
  5486
  assumes e:"0 < e" and s:"subspace s" and f:"bounded_linear f" and normf:"\<forall>x\<in>s. norm(f x) \<ge> e * norm(x)" and xs:"\<forall>n::nat. x n \<in> s" and cf:"Cauchy(f o x)"
himmelma@33175
  5487
  shows "Cauchy x"
himmelma@33175
  5488
proof-
himmelma@33175
  5489
  interpret f: bounded_linear f by fact
himmelma@33175
  5490
  { fix d::real assume "d>0"
himmelma@33175
  5491
    then obtain N where N:"\<forall>n\<ge>N. norm (f (x n) - f (x N)) < e * d"
himmelma@33175
  5492
      using cf[unfolded cauchy o_def dist_norm, THEN spec[where x="e*d"]] and e and mult_pos_pos[of e d] by auto
himmelma@33175
  5493
    { fix n assume "n\<ge>N"
himmelma@33175
  5494
      hence "norm (f (x n - x N)) < e * d" using N[THEN spec[where x=n]] unfolding f.diff[THEN sym] by auto
himmelma@33175
  5495
      moreover have "e * norm (x n - x N) \<le> norm (f (x n - x N))"
himmelma@33175
  5496
        using subspace_sub[OF s, of "x n" "x N"] using xs[THEN spec[where x=N]] and xs[THEN spec[where x=n]]
himmelma@33175
  5497
        using normf[THEN bspec[where x="x n - x N"]] by auto
himmelma@33175
  5498
      ultimately have "norm (x n - x N) < d" using `e>0`
himmelma@33175
  5499
        using mult_left_less_imp_less[of e "norm (x n - x N)" d] by auto   }
himmelma@33175
  5500
    hence "\<exists>N. \<forall>n\<ge>N. norm (x n - x N) < d" by auto }
himmelma@33175
  5501
  thus ?thesis unfolding cauchy and dist_norm by auto
himmelma@33175
  5502
qed
himmelma@33175
  5503
himmelma@33175
  5504
lemma complete_isometric_image:
himmelma@33175
  5505
  fixes f :: "real ^ _ \<Rightarrow> real ^ _"
himmelma@33175
  5506
  assumes "0 < e" and s:"subspace s" and f:"bounded_linear f" and normf:"\<forall>x\<in>s. norm(f x) \<ge> e * norm(x)" and cs:"complete s"
himmelma@33175
  5507
  shows "complete(f ` s)"
himmelma@33175
  5508
proof-
himmelma@33175
  5509
  { fix g assume as:"\<forall>n::nat. g n \<in> f ` s" and cfg:"Cauchy g"
paulson@33307
  5510
    then obtain x where "\<forall>n. x n \<in> s \<and> g n = f (x n)" 
himmelma@33175
  5511
      using choice[of "\<lambda> n xa. xa \<in> s \<and> g n = f xa"] by auto
himmelma@33175
  5512
    hence x:"\<forall>n. x n \<in> s"  "\<forall>n. g n = f (x n)" by auto
himmelma@33175
  5513
    hence "f \<circ> x = g" unfolding expand_fun_eq by auto
himmelma@33175
  5514
    then obtain l where "l\<in>s" and l:"(x ---> l) sequentially"
himmelma@33175
  5515
      using cs[unfolded complete_def, THEN spec[where x="x"]]
himmelma@33175
  5516
      using cauchy_isometric[OF `0<e` s f normf] and cfg and x(1) by auto
himmelma@33175
  5517
    hence "\<exists>l\<in>f ` s. (g ---> l) sequentially"
himmelma@33175
  5518
      using linear_continuous_at[OF f, unfolded continuous_at_sequentially, THEN spec[where x=x], of l]
himmelma@33175
  5519
      unfolding `f \<circ> x = g` by auto  }
himmelma@33175
  5520
  thus ?thesis unfolding complete_def by auto
himmelma@33175
  5521
qed
himmelma@33175
  5522
himmelma@33175
  5523
lemma dist_0_norm:
himmelma@33175
  5524
  fixes x :: "'a::real_normed_vector"
himmelma@33175
  5525
  shows "dist 0 x = norm x"
himmelma@33175
  5526
unfolding dist_norm by simp
himmelma@33175
  5527
hoelzl@34291
  5528
lemma injective_imp_isometric: fixes f::"real^'m \<Rightarrow> real^'n"
himmelma@33175
  5529
  assumes s:"closed s"  "subspace s"  and f:"bounded_linear f" "\<forall>x\<in>s. (f x = 0) \<longrightarrow> (x = 0)"
himmelma@33175
  5530
  shows "\<exists>e>0. \<forall>x\<in>s. norm (f x) \<ge> e * norm(x)"
himmelma@33175
  5531
proof(cases "s \<subseteq> {0::real^'m}")
himmelma@33175
  5532
  case True
himmelma@33175
  5533
  { fix x assume "x \<in> s"
himmelma@33175
  5534
    hence "x = 0" using True by auto
himmelma@33175
  5535
    hence "norm x \<le> norm (f x)" by auto  }
himmelma@33175
  5536
  thus ?thesis by(auto intro!: exI[where x=1])
himmelma@33175
  5537
next
himmelma@33175
  5538
  interpret f: bounded_linear f by fact
himmelma@33175
  5539
  case False
himmelma@33175
  5540
  then obtain a where a:"a\<noteq>0" "a\<in>s" by auto
himmelma@33175
  5541
  from False have "s \<noteq> {}" by auto
himmelma@33175
  5542
  let ?S = "{f x| x. (x \<in> s \<and> norm x = norm a)}"
himmelma@33175
  5543
  let ?S' = "{x::real^'m. x\<in>s \<and> norm x = norm a}"
himmelma@33175
  5544
  let ?S'' = "{x::real^'m. norm x = norm a}"
himmelma@33175
  5545
himmelma@33175
  5546
  have "?S'' = frontier(cball 0 (norm a))" unfolding frontier_cball and dist_norm by (auto simp add: norm_minus_cancel)
himmelma@33175
  5547
  hence "compact ?S''" using compact_frontier[OF compact_cball, of 0 "norm a"] by auto
himmelma@33175
  5548
  moreover have "?S' = s \<inter> ?S''" by auto
himmelma@33175
  5549
  ultimately have "compact ?S'" using closed_inter_compact[of s ?S''] using s(1) by auto
himmelma@33175
  5550
  moreover have *:"f ` ?S' = ?S" by auto
himmelma@33175
  5551
  ultimately have "compact ?S" using compact_continuous_image[OF linear_continuous_on[OF f(1)], of ?S'] by auto
himmelma@33175
  5552
  hence "closed ?S" using compact_imp_closed by auto
himmelma@33175
  5553
  moreover have "?S \<noteq> {}" using a by auto
himmelma@33175
  5554
  ultimately obtain b' where "b'\<in>?S" "\<forall>y\<in>?S. norm b' \<le> norm y" using distance_attains_inf[of ?S 0] unfolding dist_0_norm by auto
himmelma@33175
  5555
  then obtain b where "b\<in>s" and ba:"norm b = norm a" and b:"\<forall>x\<in>{x \<in> s. norm x = norm a}. norm (f b) \<le> norm (f x)" unfolding *[THEN sym] unfolding image_iff by auto
himmelma@33175
  5556
himmelma@33175
  5557
  let ?e = "norm (f b) / norm b"
himmelma@33175
  5558
  have "norm b > 0" using ba and a and norm_ge_zero by auto
himmelma@33175
  5559
  moreover have "norm (f b) > 0" using f(2)[THEN bspec[where x=b], OF `b\<in>s`] using `norm b >0` unfolding zero_less_norm_iff by auto
himmelma@33175
  5560
  ultimately have "0 < norm (f b) / norm b" by(simp only: divide_pos_pos)
himmelma@33175
  5561
  moreover
himmelma@33175
  5562
  { fix x assume "x\<in>s"
himmelma@33175
  5563
    hence "norm (f b) / norm b * norm x \<le> norm (f x)"
himmelma@33175
  5564
    proof(cases "x=0")
himmelma@33175
  5565
      case True thus "norm (f b) / norm b * norm x \<le> norm (f x)" by auto
himmelma@33175
  5566
    next
himmelma@33175
  5567
      case False
himmelma@33175
  5568
      hence *:"0 < norm a / norm x" using `a\<noteq>0` unfolding zero_less_norm_iff[THEN sym] by(simp only: divide_pos_pos)
himmelma@33175
  5569
      have "\<forall>c. \<forall>x\<in>s. c *\<^sub>R x \<in> s" using s[unfolded subspace_def smult_conv_scaleR] by auto
himmelma@33175
  5570
      hence "(norm a / norm x) *\<^sub>R x \<in> {x \<in> s. norm x = norm a}" using `x\<in>s` and `x\<noteq>0` by auto
himmelma@33175
  5571
      thus "norm (f b) / norm b * norm x \<le> norm (f x)" using b[THEN bspec[where x="(norm a / norm x) *\<^sub>R x"]]
himmelma@33175
  5572
        unfolding f.scaleR and ba using `x\<noteq>0` `a\<noteq>0`
himmelma@33175
  5573
        by (auto simp add: real_mult_commute pos_le_divide_eq pos_divide_le_eq)
himmelma@33175
  5574
    qed }
himmelma@33175
  5575
  ultimately
himmelma@33175
  5576
  show ?thesis by auto
himmelma@33175
  5577
qed
himmelma@33175
  5578
himmelma@33175
  5579
lemma closed_injective_image_subspace:
himmelma@33175
  5580
  fixes f :: "real ^ _ \<Rightarrow> real ^ _"
himmelma@33175
  5581
  assumes "subspace s" "bounded_linear f" "\<forall>x\<in>s. f x = 0 --> x = 0" "closed s"
himmelma@33175
  5582
  shows "closed(f ` s)"
himmelma@33175
  5583
proof-
himmelma@33175
  5584
  obtain e where "e>0" and e:"\<forall>x\<in>s. e * norm x \<le> norm (f x)" using injective_imp_isometric[OF assms(4,1,2,3)] by auto
himmelma@33175
  5585
  show ?thesis using complete_isometric_image[OF `e>0` assms(1,2) e] and assms(4)
himmelma@33175
  5586
    unfolding complete_eq_closed[THEN sym] by auto
himmelma@33175
  5587
qed
himmelma@33175
  5588
himmelma@33175
  5589
subsection{* Some properties of a canonical subspace.                                  *}
himmelma@33175
  5590
himmelma@33175
  5591
lemma subspace_substandard:
himmelma@34289
  5592
 "subspace {x::real^_. (\<forall>i. P i \<longrightarrow> x$i = 0)}"
himmelma@33175
  5593
  unfolding subspace_def by(auto simp add: vector_add_component vector_smult_component elim!: ballE)
himmelma@33175
  5594
himmelma@33175
  5595
lemma closed_substandard:
hoelzl@34291
  5596
 "closed {x::real^'n. \<forall>i. P i --> x$i = 0}" (is "closed ?A")
himmelma@33175
  5597
proof-
himmelma@33175
  5598
  let ?D = "{i. P i}"
himmelma@33175
  5599
  let ?Bs = "{{x::real^'n. inner (basis i) x = 0}| i. i \<in> ?D}"
himmelma@33175
  5600
  { fix x
himmelma@33175
  5601
    { assume "x\<in>?A"
himmelma@33175
  5602
      hence x:"\<forall>i\<in>?D. x $ i = 0" by auto
himmelma@33175
  5603
      hence "x\<in> \<Inter> ?Bs" by(auto simp add: inner_basis x) }
himmelma@33175
  5604
    moreover
himmelma@33175
  5605
    { assume x:"x\<in>\<Inter>?Bs"
himmelma@33175
  5606
      { fix i assume i:"i \<in> ?D"
himmelma@33175
  5607
        then obtain B where BB:"B \<in> ?Bs" and B:"B = {x::real^'n. inner (basis i) x = 0}" by auto
himmelma@33175
  5608
        hence "x $ i = 0" unfolding B using x unfolding inner_basis by auto  }
himmelma@33175
  5609
      hence "x\<in>?A" by auto }
himmelma@33175
  5610
    ultimately have "x\<in>?A \<longleftrightarrow> x\<in> \<Inter>?Bs" by auto }
himmelma@33175
  5611
  hence "?A = \<Inter> ?Bs" by auto
himmelma@33175
  5612
  thus ?thesis by(auto simp add: closed_Inter closed_hyperplane)
himmelma@33175
  5613
qed
himmelma@33175
  5614
himmelma@33175
  5615
lemma dim_substandard:
hoelzl@34291
  5616
  shows "dim {x::real^'n. \<forall>i. i \<notin> d \<longrightarrow> x$i = 0} = card d" (is "dim ?A = _")
himmelma@33175
  5617
proof-
himmelma@33175
  5618
  let ?D = "UNIV::'n set"
himmelma@33175
  5619
  let ?B = "(basis::'n\<Rightarrow>real^'n) ` d"
himmelma@33175
  5620
himmelma@33175
  5621
    let ?bas = "basis::'n \<Rightarrow> real^'n"
himmelma@33175
  5622
himmelma@33175
  5623
  have "?B \<subseteq> ?A" by auto
himmelma@33175
  5624
himmelma@33175
  5625
  moreover
himmelma@33175
  5626
  { fix x::"real^'n" assume "x\<in>?A"
himmelma@33175
  5627
    with finite[of d]
himmelma@33175
  5628
    have "x\<in> span ?B"
himmelma@33175
  5629
    proof(induct d arbitrary: x)
himmelma@33175
  5630
      case empty hence "x=0" unfolding Cart_eq by auto
himmelma@33175
  5631
      thus ?case using subspace_0[OF subspace_span[of "{}"]] by auto
himmelma@33175
  5632
    next
himmelma@33175
  5633
      case (insert k F)
himmelma@33175
  5634
      hence *:"\<forall>i. i \<notin> insert k F \<longrightarrow> x $ i = 0" by auto
himmelma@33175
  5635
      have **:"F \<subseteq> insert k F" by auto
himmelma@33175
  5636
      def y \<equiv> "x - x$k *\<^sub>R basis k"
himmelma@33175
  5637
      have y:"x = y + (x$k) *\<^sub>R basis k" unfolding y_def by auto
himmelma@33175
  5638
      { fix i assume i':"i \<notin> F"
himmelma@33175
  5639
        hence "y $ i = 0" unfolding y_def unfolding vector_minus_component
himmelma@33175
  5640
          and vector_smult_component and basis_component
himmelma@33175
  5641
          using *[THEN spec[where x=i]] by auto }
himmelma@33175
  5642
      hence "y \<in> span (basis ` (insert k F))" using insert(3)
himmelma@33175
  5643
        using span_mono[of "?bas ` F" "?bas ` (insert k F)"]
himmelma@33175
  5644
        using image_mono[OF **, of basis] by auto
himmelma@33175
  5645
      moreover
himmelma@33175
  5646
      have "basis k \<in> span (?bas ` (insert k F))" by(rule span_superset, auto)
himmelma@33175
  5647
      hence "x$k *\<^sub>R basis k \<in> span (?bas ` (insert k F))"
himmelma@33175
  5648
        using span_mul [where 'a=real, unfolded smult_conv_scaleR] by auto
himmelma@33175
  5649
      ultimately
himmelma@33175
  5650
      have "y + x$k *\<^sub>R basis k \<in> span (?bas ` (insert k F))"
himmelma@33175
  5651
        using span_add by auto
himmelma@33175
  5652
      thus ?case using y by auto
himmelma@33175
  5653
    qed
himmelma@33175
  5654
  }
himmelma@33175
  5655
  hence "?A \<subseteq> span ?B" by auto
himmelma@33175
  5656
himmelma@33175
  5657
  moreover
himmelma@33175
  5658
  { fix x assume "x \<in> ?B"
himmelma@33175
  5659
    hence "x\<in>{(basis i)::real^'n |i. i \<in> ?D}" using assms by auto  }
himmelma@33175
  5660
  hence "independent ?B" using independent_mono[OF independent_stdbasis, of ?B] and assms by auto
himmelma@33175
  5661
himmelma@33175
  5662
  moreover
himmelma@33175
  5663
  have "d \<subseteq> ?D" unfolding subset_eq using assms by auto
himmelma@33175
  5664
  hence *:"inj_on (basis::'n\<Rightarrow>real^'n) d" using subset_inj_on[OF basis_inj, of "d"] by auto
hoelzl@33712
  5665
  have "card ?B = card d" unfolding card_image[OF *] by auto
himmelma@33175
  5666
himmelma@33175
  5667
  ultimately show ?thesis using dim_unique[of "basis ` d" ?A] by auto
himmelma@33175
  5668
qed
himmelma@33175
  5669
himmelma@33175
  5670
text{* Hence closure and completeness of all subspaces.                          *}
himmelma@33175
  5671
himmelma@33175
  5672
lemma closed_subspace_lemma: "n \<le> card (UNIV::'n::finite set) \<Longrightarrow> \<exists>A::'n set. card A = n"
himmelma@33175
  5673
apply (induct n)
himmelma@33175
  5674
apply (rule_tac x="{}" in exI, simp)
himmelma@33175
  5675
apply clarsimp
himmelma@33175
  5676
apply (subgoal_tac "\<exists>x. x \<notin> A")
himmelma@33175
  5677
apply (erule exE)
himmelma@33175
  5678
apply (rule_tac x="insert x A" in exI, simp)
himmelma@33175
  5679
apply (subgoal_tac "A \<noteq> UNIV", auto)
himmelma@33175
  5680
done
himmelma@33175
  5681
hoelzl@34291
  5682
lemma closed_subspace: fixes s::"(real^'n) set"
himmelma@33175
  5683
  assumes "subspace s" shows "closed s"
himmelma@33175
  5684
proof-
himmelma@33175
  5685
  have "dim s \<le> card (UNIV :: 'n set)" using dim_subset_univ by auto
himmelma@33175
  5686
  then obtain d::"'n set" where t: "card d = dim s"
himmelma@33175
  5687
    using closed_subspace_lemma by auto
himmelma@33175
  5688
  let ?t = "{x::real^'n. \<forall>i. i \<notin> d \<longrightarrow> x$i = 0}"
himmelma@33175
  5689
  obtain f where f:"bounded_linear f"  "f ` ?t = s" "inj_on f ?t"
himmelma@33175
  5690
    using subspace_isomorphism[unfolded linear_conv_bounded_linear, OF subspace_substandard[of "\<lambda>i. i \<notin> d"] assms]
himmelma@33175
  5691
    using dim_substandard[of d] and t by auto
himmelma@33175
  5692
  interpret f: bounded_linear f by fact
himmelma@33175
  5693
  have "\<forall>x\<in>?t. f x = 0 \<longrightarrow> x = 0" using f.zero using f(3)[unfolded inj_on_def]
himmelma@33175
  5694
    by(erule_tac x=0 in ballE) auto
himmelma@33175
  5695
  moreover have "closed ?t" using closed_substandard .
himmelma@33175
  5696
  moreover have "subspace ?t" using subspace_substandard .
himmelma@33175
  5697
  ultimately show ?thesis using closed_injective_image_subspace[of ?t f]
himmelma@33175
  5698
    unfolding f(2) using f(1) by auto
himmelma@33175
  5699
qed
himmelma@33175
  5700
himmelma@33175
  5701
lemma complete_subspace:
himmelma@33175
  5702
  fixes s :: "(real ^ _) set" shows "subspace s ==> complete s"
himmelma@33175
  5703
  using complete_eq_closed closed_subspace
himmelma@33175
  5704
  by auto
himmelma@33175
  5705
himmelma@33175
  5706
lemma dim_closure:
himmelma@33175
  5707
  fixes s :: "(real ^ _) set"
himmelma@33175
  5708
  shows "dim(closure s) = dim s" (is "?dc = ?d")
himmelma@33175
  5709
proof-
himmelma@33175
  5710
  have "?dc \<le> ?d" using closure_minimal[OF span_inc, of s]
himmelma@33175
  5711
    using closed_subspace[OF subspace_span, of s]
himmelma@33175
  5712
    using dim_subset[of "closure s" "span s"] unfolding dim_span by auto
himmelma@33175
  5713
  thus ?thesis using dim_subset[OF closure_subset, of s] by auto
himmelma@33175
  5714
qed
himmelma@33175
  5715
himmelma@33175
  5716
text{* Affine transformations of intervals.                                      *}
himmelma@33175
  5717
himmelma@33175
  5718
lemma affinity_inverses:
himmelma@33175
  5719
  assumes m0: "m \<noteq> (0::'a::field)"
himmelma@33175
  5720
  shows "(\<lambda>x. m *s x + c) o (\<lambda>x. inverse(m) *s x + (-(inverse(m) *s c))) = id"
himmelma@33175
  5721
  "(\<lambda>x. inverse(m) *s x + (-(inverse(m) *s c))) o (\<lambda>x. m *s x + c) = id"
himmelma@33175
  5722
  using m0
himmelma@33175
  5723
apply (auto simp add: expand_fun_eq vector_add_ldistrib vector_smult_assoc)
himmelma@33175
  5724
by (simp add: vector_smult_lneg[symmetric] vector_smult_assoc vector_sneg_minus1[symmetric])
himmelma@33175
  5725
himmelma@33175
  5726
lemma real_affinity_le:
haftmann@35028
  5727
 "0 < (m::'a::linordered_field) ==> (m * x + c \<le> y \<longleftrightarrow> x \<le> inverse(m) * y + -(c / m))"
himmelma@33175
  5728
  by (simp add: field_simps inverse_eq_divide)
himmelma@33175
  5729
himmelma@33175
  5730
lemma real_le_affinity:
haftmann@35028
  5731
 "0 < (m::'a::linordered_field) ==> (y \<le> m * x + c \<longleftrightarrow> inverse(m) * y + -(c / m) \<le> x)"
himmelma@33175
  5732
  by (simp add: field_simps inverse_eq_divide)
himmelma@33175
  5733
himmelma@33175
  5734
lemma real_affinity_lt:
haftmann@35028
  5735
 "0 < (m::'a::linordered_field) ==> (m * x + c < y \<longleftrightarrow> x < inverse(m) * y + -(c / m))"
himmelma@33175
  5736
  by (simp add: field_simps inverse_eq_divide)
himmelma@33175
  5737
himmelma@33175
  5738
lemma real_lt_affinity:
haftmann@35028
  5739
 "0 < (m::'a::linordered_field) ==> (y < m * x + c \<longleftrightarrow> inverse(m) * y + -(c / m) < x)"
himmelma@33175
  5740
  by (simp add: field_simps inverse_eq_divide)
himmelma@33175
  5741
himmelma@33175
  5742
lemma real_affinity_eq:
haftmann@35028
  5743
 "(m::'a::linordered_field) \<noteq> 0 ==> (m * x + c = y \<longleftrightarrow> x = inverse(m) * y + -(c / m))"
himmelma@33175
  5744
  by (simp add: field_simps inverse_eq_divide)
himmelma@33175
  5745
himmelma@33175
  5746
lemma real_eq_affinity:
haftmann@35028
  5747
 "(m::'a::linordered_field) \<noteq> 0 ==> (y = m * x + c  \<longleftrightarrow> inverse(m) * y + -(c / m) = x)"
himmelma@33175
  5748
  by (simp add: field_simps inverse_eq_divide)
himmelma@33175
  5749
himmelma@33175
  5750
lemma vector_affinity_eq:
himmelma@33175
  5751
  assumes m0: "(m::'a::field) \<noteq> 0"
himmelma@33175
  5752
  shows "m *s x + c = y \<longleftrightarrow> x = inverse m *s y + -(inverse m *s c)"
himmelma@33175
  5753
proof
himmelma@33175
  5754
  assume h: "m *s x + c = y"
himmelma@33175
  5755
  hence "m *s x = y - c" by (simp add: ring_simps)
himmelma@33175
  5756
  hence "inverse m *s (m *s x) = inverse m *s (y - c)" by simp
himmelma@33175
  5757
  then show "x = inverse m *s y + - (inverse m *s c)"
himmelma@33175
  5758
    using m0 by (simp add: vector_smult_assoc vector_ssub_ldistrib)
himmelma@33175
  5759
next
himmelma@33175
  5760
  assume h: "x = inverse m *s y + - (inverse m *s c)"
himmelma@33175
  5761
  show "m *s x + c = y" unfolding h diff_minus[symmetric]
himmelma@33175
  5762
    using m0 by (simp add: vector_smult_assoc vector_ssub_ldistrib)
himmelma@33175
  5763
qed
himmelma@33175
  5764
himmelma@33175
  5765
lemma vector_eq_affinity:
himmelma@33175
  5766
 "(m::'a::field) \<noteq> 0 ==> (y = m *s x + c \<longleftrightarrow> inverse(m) *s y + -(inverse(m) *s c) = x)"
himmelma@33175
  5767
  using vector_affinity_eq[where m=m and x=x and y=y and c=c]
himmelma@33175
  5768
  by metis
himmelma@33175
  5769
himmelma@33175
  5770
lemma image_affinity_interval: fixes m::real
hoelzl@34291
  5771
  fixes a b c :: "real^'n"
himmelma@33175
  5772
  shows "(\<lambda>x. m *\<^sub>R x + c) ` {a .. b} =
himmelma@33175
  5773
            (if {a .. b} = {} then {}
himmelma@33175
  5774
            else (if 0 \<le> m then {m *\<^sub>R a + c .. m *\<^sub>R b + c}
himmelma@33175
  5775
            else {m *\<^sub>R b + c .. m *\<^sub>R a + c}))"
himmelma@33175
  5776
proof(cases "m=0")
himmelma@33175
  5777
  { fix x assume "x \<le> c" "c \<le> x"
hoelzl@33758
  5778
    hence "x=c" unfolding vector_le_def and Cart_eq by (auto intro: order_antisym) }
himmelma@33175
  5779
  moreover case True
hoelzl@33758
  5780
  moreover have "c \<in> {m *\<^sub>R a + c..m *\<^sub>R b + c}" unfolding True by(auto simp add: vector_le_def)
himmelma@33175
  5781
  ultimately show ?thesis by auto
himmelma@33175
  5782
next
himmelma@33175
  5783
  case False
himmelma@33175
  5784
  { fix y assume "a \<le> y" "y \<le> b" "m > 0"
himmelma@33175
  5785
    hence "m *\<^sub>R a + c \<le> m *\<^sub>R y + c"  "m *\<^sub>R y + c \<le> m *\<^sub>R b + c"
hoelzl@33758
  5786
      unfolding vector_le_def by(auto simp add: vector_smult_component vector_add_component)
himmelma@33175
  5787
  } moreover
himmelma@33175
  5788
  { fix y assume "a \<le> y" "y \<le> b" "m < 0"
himmelma@33175
  5789
    hence "m *\<^sub>R b + c \<le> m *\<^sub>R y + c"  "m *\<^sub>R y + c \<le> m *\<^sub>R a + c"
hoelzl@33758
  5790
      unfolding vector_le_def by(auto simp add: vector_smult_component vector_add_component mult_left_mono_neg elim!:ballE)
himmelma@33175
  5791
  } moreover
himmelma@33175
  5792
  { fix y assume "m > 0"  "m *\<^sub>R a + c \<le> y"  "y \<le> m *\<^sub>R b + c"
himmelma@33175
  5793
    hence "y \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}"
hoelzl@33758
  5794
      unfolding image_iff Bex_def mem_interval vector_le_def
himmelma@33175
  5795
      apply(auto simp add: vector_smult_component vector_add_component vector_minus_component vector_smult_assoc pth_3[symmetric]
himmelma@33175
  5796
        intro!: exI[where x="(1 / m) *\<^sub>R (y - c)"])
himmelma@33175
  5797
      by(auto simp add: pos_le_divide_eq pos_divide_le_eq real_mult_commute diff_le_iff)
himmelma@33175
  5798
  } moreover
himmelma@33175
  5799
  { fix y assume "m *\<^sub>R b + c \<le> y" "y \<le> m *\<^sub>R a + c" "m < 0"
himmelma@33175
  5800
    hence "y \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}"
hoelzl@33758
  5801
      unfolding image_iff Bex_def mem_interval vector_le_def
himmelma@33175
  5802
      apply(auto simp add: vector_smult_component vector_add_component vector_minus_component vector_smult_assoc pth_3[symmetric]
himmelma@33175
  5803
        intro!: exI[where x="(1 / m) *\<^sub>R (y - c)"])
himmelma@33175
  5804
      by(auto simp add: neg_le_divide_eq neg_divide_le_eq real_mult_commute diff_le_iff)
himmelma@33175
  5805
  }
himmelma@33175
  5806
  ultimately show ?thesis using False by auto
himmelma@33175
  5807
qed
himmelma@33175
  5808
hoelzl@34291
  5809
lemma image_smult_interval:"(\<lambda>x. m *\<^sub>R (x::real^'n)) ` {a..b} =
himmelma@33175
  5810
  (if {a..b} = {} then {} else if 0 \<le> m then {m *\<^sub>R a..m *\<^sub>R b} else {m *\<^sub>R b..m *\<^sub>R a})"
himmelma@33175
  5811
  using image_affinity_interval[of m 0 a b] by auto
himmelma@33175
  5812
himmelma@33175
  5813
subsection{* Banach fixed point theorem (not really topological...) *}
himmelma@33175
  5814
himmelma@33175
  5815
lemma banach_fix:
himmelma@33175
  5816
  assumes s:"complete s" "s \<noteq> {}" and c:"0 \<le> c" "c < 1" and f:"(f ` s) \<subseteq> s" and
himmelma@33175
  5817
          lipschitz:"\<forall>x\<in>s. \<forall>y\<in>s. dist (f x) (f y) \<le> c * dist x y"
himmelma@33175
  5818
  shows "\<exists>! x\<in>s. (f x = x)"
himmelma@33175
  5819
proof-
himmelma@33175
  5820
  have "1 - c > 0" using c by auto
himmelma@33175
  5821
himmelma@33175
  5822
  from s(2) obtain z0 where "z0 \<in> s" by auto
himmelma@33175
  5823
  def z \<equiv> "\<lambda>n. (f ^^ n) z0"
himmelma@33175
  5824
  { fix n::nat
himmelma@33175
  5825
    have "z n \<in> s" unfolding z_def
himmelma@33175
  5826
    proof(induct n) case 0 thus ?case using `z0 \<in>s` by auto
himmelma@33175
  5827
    next case Suc thus ?case using f by auto qed }
himmelma@33175
  5828
  note z_in_s = this
himmelma@33175
  5829
himmelma@33175
  5830
  def d \<equiv> "dist (z 0) (z 1)"
himmelma@33175
  5831
himmelma@33175
  5832
  have fzn:"\<And>n. f (z n) = z (Suc n)" unfolding z_def by auto
himmelma@33175
  5833
  { fix n::nat
himmelma@33175
  5834
    have "dist (z n) (z (Suc n)) \<le> (c ^ n) * d"
himmelma@33175
  5835
    proof(induct n)
himmelma@33175
  5836
      case 0 thus ?case unfolding d_def by auto
himmelma@33175
  5837
    next
himmelma@33175
  5838
      case (Suc m)
himmelma@33175
  5839
      hence "c * dist (z m) (z (Suc m)) \<le> c ^ Suc m * d"
himmelma@33175
  5840
        using `0 \<le> c` using mult_mono1_class.mult_mono1[of "dist (z m) (z (Suc m))" "c ^ m * d" c] by auto
himmelma@33175
  5841
      thus ?case using lipschitz[THEN bspec[where x="z m"], OF z_in_s, THEN bspec[where x="z (Suc m)"], OF z_in_s]
himmelma@33175
  5842
        unfolding fzn and mult_le_cancel_left by auto
himmelma@33175
  5843
    qed
himmelma@33175
  5844
  } note cf_z = this
himmelma@33175
  5845
himmelma@33175
  5846
  { fix n m::nat
himmelma@33175
  5847
    have "(1 - c) * dist (z m) (z (m+n)) \<le> (c ^ m) * d * (1 - c ^ n)"
himmelma@33175
  5848
    proof(induct n)
himmelma@33175
  5849
      case 0 show ?case by auto
himmelma@33175
  5850
    next
himmelma@33175
  5851
      case (Suc k)
himmelma@33175
  5852
      have "(1 - c) * dist (z m) (z (m + Suc k)) \<le> (1 - c) * (dist (z m) (z (m + k)) + dist (z (m + k)) (z (Suc (m + k))))"
himmelma@33175
  5853
        using dist_triangle and c by(auto simp add: dist_triangle)
himmelma@33175
  5854
      also have "\<dots> \<le> (1 - c) * (dist (z m) (z (m + k)) + c ^ (m + k) * d)"
himmelma@33175
  5855
        using cf_z[of "m + k"] and c by auto
himmelma@33175
  5856
      also have "\<dots> \<le> c ^ m * d * (1 - c ^ k) + (1 - c) * c ^ (m + k) * d"
himmelma@33175
  5857
        using Suc by (auto simp add: ring_simps)
himmelma@33175
  5858
      also have "\<dots> = (c ^ m) * (d * (1 - c ^ k) + (1 - c) * c ^ k * d)"
himmelma@33175
  5859
        unfolding power_add by (auto simp add: ring_simps)
himmelma@33175
  5860
      also have "\<dots> \<le> (c ^ m) * d * (1 - c ^ Suc k)"
himmelma@33175
  5861
        using c by (auto simp add: ring_simps)
himmelma@33175
  5862
      finally show ?case by auto
himmelma@33175
  5863
    qed
himmelma@33175
  5864
  } note cf_z2 = this
himmelma@33175
  5865
  { fix e::real assume "e>0"
himmelma@33175
  5866
    hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (z m) (z n) < e"
himmelma@33175
  5867
    proof(cases "d = 0")
himmelma@33175
  5868
      case True
himmelma@33175
  5869
      hence "\<And>n. z n = z0" using cf_z2[of 0] and c unfolding z_def by (auto simp add: pos_prod_le[OF `1 - c > 0`])
himmelma@33175
  5870
      thus ?thesis using `e>0` by auto
himmelma@33175
  5871
    next
himmelma@33175
  5872
      case False hence "d>0" unfolding d_def using zero_le_dist[of "z 0" "z 1"]
himmelma@33175
  5873
        by (metis False d_def real_less_def)
himmelma@33175
  5874
      hence "0 < e * (1 - c) / d" using `e>0` and `1-c>0`
himmelma@33175
  5875
        using divide_pos_pos[of "e * (1 - c)" d] and mult_pos_pos[of e "1 - c"] by auto
himmelma@33175
  5876
      then obtain N where N:"c ^ N < e * (1 - c) / d" using real_arch_pow_inv[of "e * (1 - c) / d" c] and c by auto
himmelma@33175
  5877
      { fix m n::nat assume "m>n" and as:"m\<ge>N" "n\<ge>N"
himmelma@33175
  5878
        have *:"c ^ n \<le> c ^ N" using `n\<ge>N` and c using power_decreasing[OF `n\<ge>N`, of c] by auto
himmelma@33175
  5879
        have "1 - c ^ (m - n) > 0" using c and power_strict_mono[of c 1 "m - n"] using `m>n` by auto
himmelma@33175
  5880
        hence **:"d * (1 - c ^ (m - n)) / (1 - c) > 0"
himmelma@33175
  5881
          using real_mult_order[OF `d>0`, of "1 - c ^ (m - n)"]
himmelma@33175
  5882
          using divide_pos_pos[of "d * (1 - c ^ (m - n))" "1 - c"]
himmelma@33175
  5883
          using `0 < 1 - c` by auto
himmelma@33175
  5884
himmelma@33175
  5885
        have "dist (z m) (z n) \<le> c ^ n * d * (1 - c ^ (m - n)) / (1 - c)"
himmelma@33175
  5886
          using cf_z2[of n "m - n"] and `m>n` unfolding pos_le_divide_eq[OF `1-c>0`]
himmelma@33175
  5887
          by (auto simp add: real_mult_commute dist_commute)
himmelma@33175
  5888
        also have "\<dots> \<le> c ^ N * d * (1 - c ^ (m - n)) / (1 - c)"
himmelma@33175
  5889
          using mult_right_mono[OF * order_less_imp_le[OF **]]
himmelma@33175
  5890
          unfolding real_mult_assoc by auto
himmelma@33175
  5891
        also have "\<dots> < (e * (1 - c) / d) * d * (1 - c ^ (m - n)) / (1 - c)"
himmelma@33175
  5892
          using mult_strict_right_mono[OF N **] unfolding real_mult_assoc by auto
himmelma@33175
  5893
        also have "\<dots> = e * (1 - c ^ (m - n))" using c and `d>0` and `1 - c > 0` by auto
himmelma@33175
  5894
        also have "\<dots> \<le> e" using c and `1 - c ^ (m - n) > 0` and `e>0` using mult_right_le_one_le[of e "1 - c ^ (m - n)"] by auto
himmelma@33175
  5895
        finally have  "dist (z m) (z n) < e" by auto
himmelma@33175
  5896
      } note * = this
himmelma@33175
  5897
      { fix m n::nat assume as:"N\<le>m" "N\<le>n"
himmelma@33175
  5898
        hence "dist (z n) (z m) < e"
himmelma@33175
  5899
        proof(cases "n = m")
himmelma@33175
  5900
          case True thus ?thesis using `e>0` by auto
himmelma@33175
  5901
        next
himmelma@33175
  5902
          case False thus ?thesis using as and *[of n m] *[of m n] unfolding nat_neq_iff by (auto simp add: dist_commute)
himmelma@33175
  5903
        qed }
himmelma@33175
  5904
      thus ?thesis by auto
himmelma@33175
  5905
    qed
himmelma@33175
  5906
  }
himmelma@33175
  5907
  hence "Cauchy z" unfolding cauchy_def by auto
himmelma@33175
  5908
  then obtain x where "x\<in>s" and x:"(z ---> x) sequentially" using s(1)[unfolded compact_def complete_def, THEN spec[where x=z]] and z_in_s by auto
himmelma@33175
  5909
himmelma@33175
  5910
  def e \<equiv> "dist (f x) x"
himmelma@33175
  5911
  have "e = 0" proof(rule ccontr)
himmelma@33175
  5912
    assume "e \<noteq> 0" hence "e>0" unfolding e_def using zero_le_dist[of "f x" x]
himmelma@33175
  5913
      by (metis dist_eq_0_iff dist_nz e_def)
himmelma@33175
  5914
    then obtain N where N:"\<forall>n\<ge>N. dist (z n) x < e / 2"
himmelma@33175
  5915
      using x[unfolded Lim_sequentially, THEN spec[where x="e/2"]] by auto
himmelma@33175
  5916
    hence N':"dist (z N) x < e / 2" by auto
himmelma@33175
  5917
himmelma@33175
  5918
    have *:"c * dist (z N) x \<le> dist (z N) x" unfolding mult_le_cancel_right2
himmelma@33175
  5919
      using zero_le_dist[of "z N" x] and c
himmelma@33175
  5920
      by (metis dist_eq_0_iff dist_nz order_less_asym real_less_def)
himmelma@33175
  5921
    have "dist (f (z N)) (f x) \<le> c * dist (z N) x" using lipschitz[THEN bspec[where x="z N"], THEN bspec[where x=x]]
himmelma@33175
  5922
      using z_in_s[of N] `x\<in>s` using c by auto
himmelma@33175
  5923
    also have "\<dots> < e / 2" using N' and c using * by auto
himmelma@33175
  5924
    finally show False unfolding fzn
himmelma@33175
  5925
      using N[THEN spec[where x="Suc N"]] and dist_triangle_half_r[of "z (Suc N)" "f x" e x]
himmelma@33175
  5926
      unfolding e_def by auto
himmelma@33175
  5927
  qed
himmelma@33175
  5928
  hence "f x = x" unfolding e_def by auto
himmelma@33175
  5929
  moreover
himmelma@33175
  5930
  { fix y assume "f y = y" "y\<in>s"
himmelma@33175
  5931
    hence "dist x y \<le> c * dist x y" using lipschitz[THEN bspec[where x=x], THEN bspec[where x=y]]
himmelma@33175
  5932
      using `x\<in>s` and `f x = x` by auto
himmelma@33175
  5933
    hence "dist x y = 0" unfolding mult_le_cancel_right1
himmelma@33175
  5934
      using c and zero_le_dist[of x y] by auto
himmelma@33175
  5935
    hence "y = x" by auto
himmelma@33175
  5936
  }
hoelzl@34986
  5937
  ultimately show ?thesis using `x\<in>s` by blast+
himmelma@33175
  5938
qed
himmelma@33175
  5939
himmelma@33175
  5940
subsection{* Edelstein fixed point theorem.                                            *}
himmelma@33175
  5941
himmelma@33175
  5942
lemma edelstein_fix:
himmelma@33175
  5943
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  5944
  assumes s:"compact s" "s \<noteq> {}" and gs:"(g ` s) \<subseteq> s"
himmelma@33175
  5945
      and dist:"\<forall>x\<in>s. \<forall>y\<in>s. x \<noteq> y \<longrightarrow> dist (g x) (g y) < dist x y"
himmelma@33175
  5946
  shows "\<exists>! x\<in>s. g x = x"
himmelma@33175
  5947
proof(cases "\<exists>x\<in>s. g x \<noteq> x")
himmelma@33175
  5948
  obtain x where "x\<in>s" using s(2) by auto
himmelma@33175
  5949
  case False hence g:"\<forall>x\<in>s. g x = x" by auto
himmelma@33175
  5950
  { fix y assume "y\<in>s"
himmelma@33175
  5951
    hence "x = y" using `x\<in>s` and dist[THEN bspec[where x=x], THEN bspec[where x=y]]
himmelma@33175
  5952
      unfolding g[THEN bspec[where x=x], OF `x\<in>s`]
himmelma@33175
  5953
      unfolding g[THEN bspec[where x=y], OF `y\<in>s`] by auto  }
hoelzl@34986
  5954
  thus ?thesis using `x\<in>s` and g by blast+
himmelma@33175
  5955
next
himmelma@33175
  5956
  case True
himmelma@33175
  5957
  then obtain x where [simp]:"x\<in>s" and "g x \<noteq> x" by auto
himmelma@33175
  5958
  { fix x y assume "x \<in> s" "y \<in> s"
himmelma@33175
  5959
    hence "dist (g x) (g y) \<le> dist x y"
himmelma@33175
  5960
      using dist[THEN bspec[where x=x], THEN bspec[where x=y]] by auto } note dist' = this
himmelma@33175
  5961
  def y \<equiv> "g x"
himmelma@33175
  5962
  have [simp]:"y\<in>s" unfolding y_def using gs[unfolded image_subset_iff] and `x\<in>s` by blast
himmelma@33175
  5963
  def f \<equiv> "\<lambda>n. g ^^ n"
himmelma@33175
  5964
  have [simp]:"\<And>n z. g (f n z) = f (Suc n) z" unfolding f_def by auto
himmelma@33175
  5965
  have [simp]:"\<And>z. f 0 z = z" unfolding f_def by auto
himmelma@33175
  5966
  { fix n::nat and z assume "z\<in>s"
himmelma@33175
  5967
    have "f n z \<in> s" unfolding f_def
himmelma@33175
  5968
    proof(induct n)
himmelma@33175
  5969
      case 0 thus ?case using `z\<in>s` by simp
himmelma@33175
  5970
    next
himmelma@33175
  5971
      case (Suc n) thus ?case using gs[unfolded image_subset_iff] by auto
himmelma@33175
  5972
    qed } note fs = this
himmelma@33175
  5973
  { fix m n ::nat assume "m\<le>n"
himmelma@33175
  5974
    fix w z assume "w\<in>s" "z\<in>s"
himmelma@33175
  5975
    have "dist (f n w) (f n z) \<le> dist (f m w) (f m z)" using `m\<le>n`
himmelma@33175
  5976
    proof(induct n)
himmelma@33175
  5977
      case 0 thus ?case by auto
himmelma@33175
  5978
    next
himmelma@33175
  5979
      case (Suc n)
himmelma@33175
  5980
      thus ?case proof(cases "m\<le>n")
himmelma@33175
  5981
        case True thus ?thesis using Suc(1)
himmelma@33175
  5982
          using dist'[OF fs fs, OF `w\<in>s` `z\<in>s`, of n n] by auto
himmelma@33175
  5983
      next
himmelma@33175
  5984
        case False hence mn:"m = Suc n" using Suc(2) by simp
himmelma@33175
  5985
        show ?thesis unfolding mn  by auto
himmelma@33175
  5986
      qed
himmelma@33175
  5987
    qed } note distf = this
himmelma@33175
  5988
himmelma@33175
  5989
  def h \<equiv> "\<lambda>n. (f n x, f n y)"
himmelma@33175
  5990
  let ?s2 = "s \<times> s"
himmelma@33175
  5991
  obtain l r where "l\<in>?s2" and r:"subseq r" and lr:"((h \<circ> r) ---> l) sequentially"
himmelma@33175
  5992
    using compact_Times [OF s(1) s(1), unfolded compact_def, THEN spec[where x=h]] unfolding  h_def
himmelma@33175
  5993
    using fs[OF `x\<in>s`] and fs[OF `y\<in>s`] by blast
himmelma@33175
  5994
  def a \<equiv> "fst l" def b \<equiv> "snd l"
himmelma@33175
  5995
  have lab:"l = (a, b)" unfolding a_def b_def by simp
himmelma@33175
  5996
  have [simp]:"a\<in>s" "b\<in>s" unfolding a_def b_def using `l\<in>?s2` by auto
himmelma@33175
  5997
himmelma@33175
  5998
  have lima:"((fst \<circ> (h \<circ> r)) ---> a) sequentially"
himmelma@33175
  5999
   and limb:"((snd \<circ> (h \<circ> r)) ---> b) sequentially"
himmelma@33175
  6000
    using lr
himmelma@33175
  6001
    unfolding o_def a_def b_def by (simp_all add: tendsto_intros)
himmelma@33175
  6002
himmelma@33175
  6003
  { fix n::nat
himmelma@33175
  6004
    have *:"\<And>fx fy (x::'a) y. dist fx fy \<le> dist x y \<Longrightarrow> \<not> (dist (fx - fy) (a - b) < dist a b - dist x y)" unfolding dist_norm by norm
himmelma@33175
  6005
    { fix x y :: 'a
himmelma@33175
  6006
      have "dist (-x) (-y) = dist x y" unfolding dist_norm
himmelma@33175
  6007
        using norm_minus_cancel[of "x - y"] by (auto simp add: uminus_add_conv_diff) } note ** = this
himmelma@33175
  6008
himmelma@33175
  6009
    { assume as:"dist a b > dist (f n x) (f n y)"
himmelma@33175
  6010
      then obtain Na Nb where "\<forall>m\<ge>Na. dist (f (r m) x) a < (dist a b - dist (f n x) (f n y)) / 2"
himmelma@33175
  6011
        and "\<forall>m\<ge>Nb. dist (f (r m) y) b < (dist a b - dist (f n x) (f n y)) / 2"
himmelma@33175
  6012
        using lima limb unfolding h_def Lim_sequentially by (fastsimp simp del: less_divide_eq_number_of1)
himmelma@33175
  6013
      hence "dist (f (r (Na + Nb + n)) x - f (r (Na + Nb + n)) y) (a - b) < dist a b - dist (f n x) (f n y)"
himmelma@33175
  6014
        apply(erule_tac x="Na+Nb+n" in allE)
himmelma@33175
  6015
        apply(erule_tac x="Na+Nb+n" in allE) apply simp
himmelma@33175
  6016
        using dist_triangle_add_half[of a "f (r (Na + Nb + n)) x" "dist a b - dist (f n x) (f n y)"
himmelma@33175
  6017
          "-b"  "- f (r (Na + Nb + n)) y"]
himmelma@33175
  6018
        unfolding ** unfolding group_simps(12) by (auto simp add: dist_commute)
himmelma@33175
  6019
      moreover
himmelma@33175
  6020
      have "dist (f (r (Na + Nb + n)) x - f (r (Na + Nb + n)) y) (a - b) \<ge> dist a b - dist (f n x) (f n y)"
himmelma@33175
  6021
        using distf[of n "r (Na+Nb+n)", OF _ `x\<in>s` `y\<in>s`]
himmelma@33175
  6022
        using subseq_bigger[OF r, of "Na+Nb+n"]
himmelma@33175
  6023
        using *[of "f (r (Na + Nb + n)) x" "f (r (Na + Nb + n)) y" "f n x" "f n y"] by auto
himmelma@33175
  6024
      ultimately have False by simp
himmelma@33175
  6025
    }
himmelma@33175
  6026
    hence "dist a b \<le> dist (f n x) (f n y)" by(rule ccontr)auto }
himmelma@33175
  6027
  note ab_fn = this
himmelma@33175
  6028
himmelma@33175
  6029
  have [simp]:"a = b" proof(rule ccontr)
himmelma@33175
  6030
    def e \<equiv> "dist a b - dist (g a) (g b)"
himmelma@33175
  6031
    assume "a\<noteq>b" hence "e > 0" unfolding e_def using dist by fastsimp
himmelma@33175
  6032
    hence "\<exists>n. dist (f n x) a < e/2 \<and> dist (f n y) b < e/2"
himmelma@33175
  6033
      using lima limb unfolding Lim_sequentially
himmelma@33175
  6034
      apply (auto elim!: allE[where x="e/2"]) apply(rule_tac x="r (max N Na)" in exI) unfolding h_def by fastsimp
himmelma@33175
  6035
    then obtain n where n:"dist (f n x) a < e/2 \<and> dist (f n y) b < e/2" by auto
himmelma@33175
  6036
    have "dist (f (Suc n) x) (g a) \<le> dist (f n x) a"
himmelma@33175
  6037
      using dist[THEN bspec[where x="f n x"], THEN bspec[where x="a"]] and fs by auto
himmelma@33175
  6038
    moreover have "dist (f (Suc n) y) (g b) \<le> dist (f n y) b"
himmelma@33175
  6039
      using dist[THEN bspec[where x="f n y"], THEN bspec[where x="b"]] and fs by auto
himmelma@33175
  6040
    ultimately have "dist (f (Suc n) x) (g a) + dist (f (Suc n) y) (g b) < e" using n by auto
himmelma@33175
  6041
    thus False unfolding e_def using ab_fn[of "Suc n"] by norm
himmelma@33175
  6042
  qed
himmelma@33175
  6043
himmelma@33175
  6044
  have [simp]:"\<And>n. f (Suc n) x = f n y" unfolding f_def y_def by(induct_tac n)auto
himmelma@33175
  6045
  { fix x y assume "x\<in>s" "y\<in>s" moreover
himmelma@33175
  6046
    fix e::real assume "e>0" ultimately
himmelma@33175
  6047
    have "dist y x < e \<longrightarrow> dist (g y) (g x) < e" using dist by fastsimp }
himmelma@33175
  6048
  hence "continuous_on s g" unfolding continuous_on_def by auto
himmelma@33175
  6049
himmelma@33175
  6050
  hence "((snd \<circ> h \<circ> r) ---> g a) sequentially" unfolding continuous_on_sequentially
himmelma@33175
  6051
    apply (rule allE[where x="\<lambda>n. (fst \<circ> h \<circ> r) n"]) apply (erule ballE[where x=a])
himmelma@33175
  6052
    using lima unfolding h_def o_def using fs[OF `x\<in>s`] by (auto simp add: y_def)
himmelma@33175
  6053
  hence "g a = a" using Lim_unique[OF trivial_limit_sequentially limb, of "g a"]
himmelma@33175
  6054
    unfolding `a=b` and o_assoc by auto
himmelma@33175
  6055
  moreover
himmelma@33175
  6056
  { fix x assume "x\<in>s" "g x = x" "x\<noteq>a"
himmelma@33175
  6057
    hence "False" using dist[THEN bspec[where x=a], THEN bspec[where x=x]]
himmelma@33175
  6058
      using `g a = a` and `a\<in>s` by auto  }
hoelzl@34986
  6059
  ultimately show "\<exists>!x\<in>s. g x = x" using `a\<in>s` by blast
himmelma@33175
  6060
qed
himmelma@33175
  6061
himmelma@33175
  6062
end