src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
author huffman
Mon Apr 26 19:55:50 2010 -0700 (2010-04-26)
changeset 36437 e76cb1d4663c
parent 36431 340755027840
child 36438 d8b26fac82f5
permissions -rw-r--r--
reorganize subsection headings
hoelzl@33714
     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
huffman@36336
     9
imports SEQ Euclidean_Space Product_Vector Glbs
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
huffman@36362
    51
  unfolding subset_eq Ball_def mem_def by auto
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)"
huffman@36362
    58
  using openin_clauses by simp
huffman@36362
    59
huffman@36362
    60
lemma openin_Union[intro]: "(\<forall>S \<in>K. openin U S) \<Longrightarrow> openin U (\<Union> K)"
huffman@36362
    61
  using openin_clauses by simp
himmelma@33175
    62
himmelma@33175
    63
lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"
himmelma@33175
    64
  using openin_Union[of "{S,T}" U] by auto
himmelma@33175
    65
himmelma@33175
    66
lemma openin_topspace[intro, simp]: "openin U (topspace U)" by (simp add: openin_Union topspace_def)
himmelma@33175
    67
himmelma@33175
    68
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
    69
proof-
himmelma@33175
    70
  {assume ?lhs then have ?rhs by auto }
himmelma@33175
    71
  moreover
himmelma@33175
    72
  {assume H: ?rhs
himmelma@33175
    73
    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
    74
      unfolding Ball_def ex_simps(6)[symmetric] choice_iff by blast
himmelma@33175
    75
    from t have th0: "\<forall>x\<in> t`S. openin U x" by auto
himmelma@33175
    76
    have "\<Union> t`S = S" using t by auto
himmelma@33175
    77
    with openin_Union[OF th0] have "openin U S" by simp }
himmelma@33175
    78
  ultimately show ?thesis by blast
himmelma@33175
    79
qed
himmelma@33175
    80
himmelma@33175
    81
subsection{* Closed sets *}
himmelma@33175
    82
himmelma@33175
    83
definition "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"
himmelma@33175
    84
himmelma@33175
    85
lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U" by (metis closedin_def)
himmelma@33175
    86
lemma closedin_empty[simp]: "closedin U {}" by (simp add: closedin_def)
himmelma@33175
    87
lemma closedin_topspace[intro,simp]:
himmelma@33175
    88
  "closedin U (topspace U)" by (simp add: closedin_def)
himmelma@33175
    89
lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"
himmelma@33175
    90
  by (auto simp add: Diff_Un closedin_def)
himmelma@33175
    91
himmelma@33175
    92
lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union> {A - s|s. s\<in>S}" by auto
himmelma@33175
    93
lemma closedin_Inter[intro]: assumes Ke: "K \<noteq> {}" and Kc: "\<forall>S \<in>K. closedin U S"
himmelma@33175
    94
  shows "closedin U (\<Inter> K)"  using Ke Kc unfolding closedin_def Diff_Inter by auto
himmelma@33175
    95
himmelma@33175
    96
lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"
himmelma@33175
    97
  using closedin_Inter[of "{S,T}" U] by auto
himmelma@33175
    98
himmelma@33175
    99
lemma Diff_Diff_Int: "A - (A - B) = A \<inter> B" by blast
himmelma@33175
   100
lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"
himmelma@33175
   101
  apply (auto simp add: closedin_def Diff_Diff_Int inf_absorb2)
himmelma@33175
   102
  apply (metis openin_subset subset_eq)
himmelma@33175
   103
  done
himmelma@33175
   104
himmelma@33175
   105
lemma openin_closedin:  "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"
himmelma@33175
   106
  by (simp add: openin_closedin_eq)
himmelma@33175
   107
himmelma@33175
   108
lemma openin_diff[intro]: assumes oS: "openin U S" and cT: "closedin U T" shows "openin U (S - T)"
himmelma@33175
   109
proof-
himmelma@33175
   110
  have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT
himmelma@33175
   111
    by (auto simp add: topspace_def openin_subset)
himmelma@33175
   112
  then show ?thesis using oS cT by (auto simp add: closedin_def)
himmelma@33175
   113
qed
himmelma@33175
   114
himmelma@33175
   115
lemma closedin_diff[intro]: assumes oS: "closedin U S" and cT: "openin U T" shows "closedin U (S - T)"
himmelma@33175
   116
proof-
himmelma@33175
   117
  have "S - T = S \<inter> (topspace U - T)" using closedin_subset[of U S]  oS cT
himmelma@33175
   118
    by (auto simp add: topspace_def )
himmelma@33175
   119
  then show ?thesis using oS cT by (auto simp add: openin_closedin_eq)
himmelma@33175
   120
qed
himmelma@33175
   121
himmelma@33175
   122
subsection{* Subspace topology. *}
himmelma@33175
   123
himmelma@33175
   124
definition "subtopology U V = topology {S \<inter> V |S. openin U S}"
himmelma@33175
   125
himmelma@33175
   126
lemma istopology_subtopology: "istopology {S \<inter> V |S. openin U S}" (is "istopology ?L")
himmelma@33175
   127
proof-
himmelma@33175
   128
  have "{} \<in> ?L" by blast
himmelma@33175
   129
  {fix A B assume A: "A \<in> ?L" and B: "B \<in> ?L"
himmelma@33175
   130
    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
   131
    have "A\<inter>B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"  using Sa Sb by blast+
himmelma@33175
   132
    then have "A \<inter> B \<in> ?L" by blast}
himmelma@33175
   133
  moreover
himmelma@33175
   134
  {fix K assume K: "K \<subseteq> ?L"
himmelma@33175
   135
    have th0: "?L = (\<lambda>S. S \<inter> V) ` openin U "
himmelma@33175
   136
      apply (rule set_ext)
himmelma@33175
   137
      apply (simp add: Ball_def image_iff)
himmelma@33175
   138
      by (metis mem_def)
himmelma@33175
   139
    from K[unfolded th0 subset_image_iff]
himmelma@33175
   140
    obtain Sk where Sk: "Sk \<subseteq> openin U" "K = (\<lambda>S. S \<inter> V) ` Sk" by blast
himmelma@33175
   141
    have "\<Union>K = (\<Union>Sk) \<inter> V" using Sk by auto
himmelma@33175
   142
    moreover have "openin U (\<Union> Sk)" using Sk by (auto simp add: subset_eq mem_def)
himmelma@33175
   143
    ultimately have "\<Union>K \<in> ?L" by blast}
himmelma@33175
   144
  ultimately show ?thesis unfolding istopology_def by blast
himmelma@33175
   145
qed
himmelma@33175
   146
himmelma@33175
   147
lemma openin_subtopology:
himmelma@33175
   148
  "openin (subtopology U V) S \<longleftrightarrow> (\<exists> T. (openin U T) \<and> (S = T \<inter> V))"
himmelma@33175
   149
  unfolding subtopology_def topology_inverse'[OF istopology_subtopology]
himmelma@33175
   150
  by (auto simp add: Collect_def)
himmelma@33175
   151
himmelma@33175
   152
lemma topspace_subtopology: "topspace(subtopology U V) = topspace U \<inter> V"
himmelma@33175
   153
  by (auto simp add: topspace_def openin_subtopology)
himmelma@33175
   154
himmelma@33175
   155
lemma closedin_subtopology:
himmelma@33175
   156
  "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"
himmelma@33175
   157
  unfolding closedin_def topspace_subtopology
himmelma@33175
   158
  apply (simp add: openin_subtopology)
himmelma@33175
   159
  apply (rule iffI)
himmelma@33175
   160
  apply clarify
himmelma@33175
   161
  apply (rule_tac x="topspace U - T" in exI)
himmelma@33175
   162
  by auto
himmelma@33175
   163
himmelma@33175
   164
lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"
himmelma@33175
   165
  unfolding openin_subtopology
himmelma@33175
   166
  apply (rule iffI, clarify)
himmelma@33175
   167
  apply (frule openin_subset[of U])  apply blast
himmelma@33175
   168
  apply (rule exI[where x="topspace U"])
himmelma@33175
   169
  by auto
himmelma@33175
   170
himmelma@33175
   171
lemma subtopology_superset: assumes UV: "topspace U \<subseteq> V"
himmelma@33175
   172
  shows "subtopology U V = U"
himmelma@33175
   173
proof-
himmelma@33175
   174
  {fix S
himmelma@33175
   175
    {fix T assume T: "openin U T" "S = T \<inter> V"
himmelma@33175
   176
      from T openin_subset[OF T(1)] UV have eq: "S = T" by blast
himmelma@33175
   177
      have "openin U S" unfolding eq using T by blast}
himmelma@33175
   178
    moreover
himmelma@33175
   179
    {assume S: "openin U S"
himmelma@33175
   180
      hence "\<exists>T. openin U T \<and> S = T \<inter> V"
himmelma@33175
   181
        using openin_subset[OF S] UV by auto}
himmelma@33175
   182
    ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S" by blast}
himmelma@33175
   183
  then show ?thesis unfolding topology_eq openin_subtopology by blast
himmelma@33175
   184
qed
himmelma@33175
   185
himmelma@33175
   186
himmelma@33175
   187
lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"
himmelma@33175
   188
  by (simp add: subtopology_superset)
himmelma@33175
   189
himmelma@33175
   190
lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"
himmelma@33175
   191
  by (simp add: subtopology_superset)
himmelma@33175
   192
himmelma@33175
   193
subsection{* The universal Euclidean versions are what we use most of the time *}
himmelma@33175
   194
himmelma@33175
   195
definition
himmelma@33175
   196
  euclidean :: "'a::topological_space topology" where
himmelma@33175
   197
  "euclidean = topology open"
himmelma@33175
   198
himmelma@33175
   199
lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"
himmelma@33175
   200
  unfolding euclidean_def
himmelma@33175
   201
  apply (rule cong[where x=S and y=S])
himmelma@33175
   202
  apply (rule topology_inverse[symmetric])
himmelma@33175
   203
  apply (auto simp add: istopology_def)
himmelma@33175
   204
  by (auto simp add: mem_def subset_eq)
himmelma@33175
   205
himmelma@33175
   206
lemma topspace_euclidean: "topspace euclidean = UNIV"
himmelma@33175
   207
  apply (simp add: topspace_def)
himmelma@33175
   208
  apply (rule set_ext)
himmelma@33175
   209
  by (auto simp add: open_openin[symmetric])
himmelma@33175
   210
himmelma@33175
   211
lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"
himmelma@33175
   212
  by (simp add: topspace_euclidean topspace_subtopology)
himmelma@33175
   213
himmelma@33175
   214
lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"
himmelma@33175
   215
  by (simp add: closed_def closedin_def topspace_euclidean open_openin Compl_eq_Diff_UNIV)
himmelma@33175
   216
himmelma@33175
   217
lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"
himmelma@33175
   218
  by (simp add: open_openin openin_subopen[symmetric])
himmelma@33175
   219
himmelma@33175
   220
subsection{* Open and closed balls. *}
himmelma@33175
   221
himmelma@33175
   222
definition
himmelma@33175
   223
  ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where
himmelma@33175
   224
  "ball x e = {y. dist x y < e}"
himmelma@33175
   225
himmelma@33175
   226
definition
himmelma@33175
   227
  cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where
himmelma@33175
   228
  "cball x e = {y. dist x y \<le> e}"
himmelma@33175
   229
himmelma@33175
   230
lemma mem_ball[simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e" by (simp add: ball_def)
himmelma@33175
   231
lemma mem_cball[simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e" by (simp add: cball_def)
himmelma@33175
   232
himmelma@33175
   233
lemma mem_ball_0 [simp]:
himmelma@33175
   234
  fixes x :: "'a::real_normed_vector"
himmelma@33175
   235
  shows "x \<in> ball 0 e \<longleftrightarrow> norm x < e"
himmelma@33175
   236
  by (simp add: dist_norm)
himmelma@33175
   237
himmelma@33175
   238
lemma mem_cball_0 [simp]:
himmelma@33175
   239
  fixes x :: "'a::real_normed_vector"
himmelma@33175
   240
  shows "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"
himmelma@33175
   241
  by (simp add: dist_norm)
himmelma@33175
   242
himmelma@33175
   243
lemma centre_in_cball[simp]: "x \<in> cball x e \<longleftrightarrow> 0\<le> e"  by simp
himmelma@33175
   244
lemma ball_subset_cball[simp,intro]: "ball x e \<subseteq> cball x e" by (simp add: subset_eq)
himmelma@33175
   245
lemma subset_ball[intro]: "d <= e ==> ball x d \<subseteq> ball x e" by (simp add: subset_eq)
himmelma@33175
   246
lemma subset_cball[intro]: "d <= e ==> cball x d \<subseteq> cball x e" by (simp add: subset_eq)
himmelma@33175
   247
lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"
himmelma@33175
   248
  by (simp add: expand_set_eq) arith
himmelma@33175
   249
himmelma@33175
   250
lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"
himmelma@33175
   251
  by (simp add: expand_set_eq)
himmelma@33175
   252
himmelma@33175
   253
lemma diff_less_iff: "(a::real) - b > 0 \<longleftrightarrow> a > b"
himmelma@33175
   254
  "(a::real) - b < 0 \<longleftrightarrow> a < b"
himmelma@33175
   255
  "a - b < c \<longleftrightarrow> a < c +b" "a - b > c \<longleftrightarrow> a > c +b" by arith+
himmelma@33175
   256
lemma diff_le_iff: "(a::real) - b \<ge> 0 \<longleftrightarrow> a \<ge> b" "(a::real) - b \<le> 0 \<longleftrightarrow> a \<le> b"
himmelma@33175
   257
  "a - b \<le> c \<longleftrightarrow> a \<le> c +b" "a - b \<ge> c \<longleftrightarrow> a \<ge> c +b"  by arith+
himmelma@33175
   258
himmelma@33175
   259
lemma open_ball[intro, simp]: "open (ball x e)"
himmelma@33175
   260
  unfolding open_dist ball_def Collect_def Ball_def mem_def
himmelma@33175
   261
  unfolding dist_commute
himmelma@33175
   262
  apply clarify
himmelma@33175
   263
  apply (rule_tac x="e - dist xa x" in exI)
himmelma@33175
   264
  using dist_triangle_alt[where z=x]
himmelma@33175
   265
  apply (clarsimp simp add: diff_less_iff)
himmelma@33175
   266
  apply atomize
himmelma@33175
   267
  apply (erule_tac x="y" in allE)
himmelma@33175
   268
  apply (erule_tac x="xa" in allE)
himmelma@33175
   269
  by arith
himmelma@33175
   270
himmelma@33175
   271
lemma centre_in_ball[simp]: "x \<in> ball x e \<longleftrightarrow> e > 0" by (metis mem_ball dist_self)
himmelma@33175
   272
lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"
himmelma@33175
   273
  unfolding open_dist subset_eq mem_ball Ball_def dist_commute ..
himmelma@33175
   274
hoelzl@33714
   275
lemma openE[elim?]:
hoelzl@33714
   276
  assumes "open S" "x\<in>S" 
hoelzl@33714
   277
  obtains e where "e>0" "ball x e \<subseteq> S"
hoelzl@33714
   278
  using assms unfolding open_contains_ball by auto
hoelzl@33714
   279
himmelma@33175
   280
lemma open_contains_ball_eq: "open S \<Longrightarrow> \<forall>x. x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
himmelma@33175
   281
  by (metis open_contains_ball subset_eq centre_in_ball)
himmelma@33175
   282
himmelma@33175
   283
lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"
himmelma@33175
   284
  unfolding mem_ball expand_set_eq
himmelma@33175
   285
  apply (simp add: not_less)
himmelma@33175
   286
  by (metis zero_le_dist order_trans dist_self)
himmelma@33175
   287
himmelma@33175
   288
lemma ball_empty[intro]: "e \<le> 0 ==> ball x e = {}" by simp
himmelma@33175
   289
himmelma@33175
   290
subsection{* Basic "localization" results are handy for connectedness. *}
himmelma@33175
   291
himmelma@33175
   292
lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"
himmelma@33175
   293
  by (auto simp add: openin_subtopology open_openin[symmetric])
himmelma@33175
   294
himmelma@33175
   295
lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"
himmelma@33175
   296
  by (auto simp add: openin_open)
himmelma@33175
   297
himmelma@33175
   298
lemma open_openin_trans[trans]:
himmelma@33175
   299
 "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"
himmelma@33175
   300
  by (metis Int_absorb1  openin_open_Int)
himmelma@33175
   301
himmelma@33175
   302
lemma open_subset:  "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"
himmelma@33175
   303
  by (auto simp add: openin_open)
himmelma@33175
   304
himmelma@33175
   305
lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"
himmelma@33175
   306
  by (simp add: closedin_subtopology closed_closedin Int_ac)
himmelma@33175
   307
himmelma@33175
   308
lemma closedin_closed_Int: "closed S ==> closedin (subtopology euclidean U) (U \<inter> S)"
himmelma@33175
   309
  by (metis closedin_closed)
himmelma@33175
   310
himmelma@33175
   311
lemma closed_closedin_trans: "closed S \<Longrightarrow> closed T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> closedin (subtopology euclidean S) T"
himmelma@33175
   312
  apply (subgoal_tac "S \<inter> T = T" )
himmelma@33175
   313
  apply auto
himmelma@33175
   314
  apply (frule closedin_closed_Int[of T S])
himmelma@33175
   315
  by simp
himmelma@33175
   316
himmelma@33175
   317
lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"
himmelma@33175
   318
  by (auto simp add: closedin_closed)
himmelma@33175
   319
himmelma@33175
   320
lemma openin_euclidean_subtopology_iff:
himmelma@33175
   321
  fixes S U :: "'a::metric_space set"
himmelma@33175
   322
  shows "openin (subtopology euclidean U) S
himmelma@33175
   323
  \<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
   324
proof-
himmelma@33175
   325
  {assume ?lhs hence ?rhs unfolding openin_subtopology open_openin[symmetric]
himmelma@33175
   326
      by (simp add: open_dist) blast}
himmelma@33175
   327
  moreover
himmelma@33175
   328
  {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
   329
    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
   330
      by metis
himmelma@33175
   331
    let ?T = "\<Union>{B. \<exists>x\<in>S. B = ball x (d x)}"
himmelma@33175
   332
    have oT: "open ?T" by auto
himmelma@33175
   333
    { fix x assume "x\<in>S"
himmelma@33175
   334
      hence "x \<in> \<Union>{B. \<exists>x\<in>S. B = ball x (d x)}"
himmelma@33175
   335
        apply simp apply(rule_tac x="ball x(d x)" in exI) apply auto
himmelma@33175
   336
        by (rule d [THEN conjunct1])
himmelma@33175
   337
      hence "x\<in> ?T \<inter> U" using SU and `x\<in>S` by auto  }
himmelma@33175
   338
    moreover
himmelma@33175
   339
    { fix y assume "y\<in>?T"
himmelma@33175
   340
      then obtain B where "y\<in>B" "B\<in>{B. \<exists>x\<in>S. B = ball x (d x)}" by auto
himmelma@33175
   341
      then obtain x where "x\<in>S" and x:"y \<in> ball x (d x)" by auto
himmelma@33175
   342
      assume "y\<in>U"
himmelma@33175
   343
      hence "y\<in>S" using d[OF `x\<in>S`] and x by(auto simp add: dist_commute) }
himmelma@33175
   344
    ultimately have "S = ?T \<inter> U" by blast
himmelma@33175
   345
    with oT have ?lhs unfolding openin_subtopology open_openin[symmetric] by blast}
himmelma@33175
   346
  ultimately show ?thesis by blast
himmelma@33175
   347
qed
himmelma@33175
   348
himmelma@33175
   349
text{* These "transitivity" results are handy too. *}
himmelma@33175
   350
himmelma@33175
   351
lemma openin_trans[trans]: "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T
himmelma@33175
   352
  \<Longrightarrow> openin (subtopology euclidean U) S"
himmelma@33175
   353
  unfolding open_openin openin_open by blast
himmelma@33175
   354
himmelma@33175
   355
lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"
himmelma@33175
   356
  by (auto simp add: openin_open intro: openin_trans)
himmelma@33175
   357
himmelma@33175
   358
lemma closedin_trans[trans]:
himmelma@33175
   359
 "closedin (subtopology euclidean T) S \<Longrightarrow>
himmelma@33175
   360
           closedin (subtopology euclidean U) T
himmelma@33175
   361
           ==> closedin (subtopology euclidean U) S"
himmelma@33175
   362
  by (auto simp add: closedin_closed closed_closedin closed_Inter Int_assoc)
himmelma@33175
   363
himmelma@33175
   364
lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"
himmelma@33175
   365
  by (auto simp add: closedin_closed intro: closedin_trans)
himmelma@33175
   366
himmelma@33175
   367
subsection{* Connectedness *}
himmelma@33175
   368
himmelma@33175
   369
definition "connected S \<longleftrightarrow>
himmelma@33175
   370
  ~(\<exists>e1 e2. open e1 \<and> open e2 \<and> S \<subseteq> (e1 \<union> e2) \<and> (e1 \<inter> e2 \<inter> S = {})
himmelma@33175
   371
  \<and> ~(e1 \<inter> S = {}) \<and> ~(e2 \<inter> S = {}))"
himmelma@33175
   372
himmelma@33175
   373
lemma connected_local:
himmelma@33175
   374
 "connected S \<longleftrightarrow> ~(\<exists>e1 e2.
himmelma@33175
   375
                 openin (subtopology euclidean S) e1 \<and>
himmelma@33175
   376
                 openin (subtopology euclidean S) e2 \<and>
himmelma@33175
   377
                 S \<subseteq> e1 \<union> e2 \<and>
himmelma@33175
   378
                 e1 \<inter> e2 = {} \<and>
himmelma@33175
   379
                 ~(e1 = {}) \<and>
himmelma@33175
   380
                 ~(e2 = {}))"
himmelma@33175
   381
unfolding connected_def openin_open by (safe, blast+)
himmelma@33175
   382
huffman@34105
   383
lemma exists_diff:
huffman@34105
   384
  fixes P :: "'a set \<Rightarrow> bool"
huffman@34105
   385
  shows "(\<exists>S. P(- S)) \<longleftrightarrow> (\<exists>S. P S)" (is "?lhs \<longleftrightarrow> ?rhs")
himmelma@33175
   386
proof-
himmelma@33175
   387
  {assume "?lhs" hence ?rhs by blast }
himmelma@33175
   388
  moreover
himmelma@33175
   389
  {fix S assume H: "P S"
huffman@34105
   390
    have "S = - (- S)" by auto
huffman@34105
   391
    with H have "P (- (- S))" by metis }
himmelma@33175
   392
  ultimately show ?thesis by metis
himmelma@33175
   393
qed
himmelma@33175
   394
himmelma@33175
   395
lemma connected_clopen: "connected S \<longleftrightarrow>
himmelma@33175
   396
        (\<forall>T. openin (subtopology euclidean S) T \<and>
himmelma@33175
   397
            closedin (subtopology euclidean S) T \<longrightarrow> T = {} \<or> T = S)" (is "?lhs \<longleftrightarrow> ?rhs")
himmelma@33175
   398
proof-
huffman@34105
   399
  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
   400
    unfolding connected_def openin_open closedin_closed
himmelma@33175
   401
    apply (subst exists_diff) by blast
huffman@34105
   402
  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@34105
   403
    (is " _ \<longleftrightarrow> \<not> (\<exists>e2 e1. ?P e2 e1)") apply (simp add: closed_def) by metis
himmelma@33175
   404
himmelma@33175
   405
  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
   406
    (is "_ \<longleftrightarrow> \<not> (\<exists>t' t. ?Q t' t)")
himmelma@33175
   407
    unfolding connected_def openin_open closedin_closed by auto
himmelma@33175
   408
  {fix e2
himmelma@33175
   409
    {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
   410
        by auto}
himmelma@33175
   411
    then have "(\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by metis}
himmelma@33175
   412
  then have "\<forall>e2. (\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by blast
himmelma@33175
   413
  then show ?thesis unfolding th0 th1 by simp
himmelma@33175
   414
qed
himmelma@33175
   415
himmelma@33175
   416
lemma connected_empty[simp, intro]: "connected {}"
himmelma@33175
   417
  by (simp add: connected_def)
himmelma@33175
   418
himmelma@33175
   419
subsection{* Hausdorff and other separation properties *}
himmelma@33175
   420
himmelma@33175
   421
class t0_space =
himmelma@33175
   422
  assumes t0_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U)"
himmelma@33175
   423
himmelma@33175
   424
class t1_space =
himmelma@33175
   425
  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
   426
begin
himmelma@33175
   427
himmelma@33175
   428
subclass t0_space
himmelma@33175
   429
proof
himmelma@33175
   430
qed (fast dest: t1_space)
himmelma@33175
   431
himmelma@33175
   432
end
himmelma@33175
   433
himmelma@33175
   434
text {* T2 spaces are also known as Hausdorff spaces. *}
himmelma@33175
   435
himmelma@33175
   436
class t2_space =
himmelma@33175
   437
  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
   438
begin
himmelma@33175
   439
himmelma@33175
   440
subclass t1_space
himmelma@33175
   441
proof
himmelma@33175
   442
qed (fast dest: hausdorff)
himmelma@33175
   443
himmelma@33175
   444
end
himmelma@33175
   445
himmelma@33175
   446
instance metric_space \<subseteq> t2_space
himmelma@33175
   447
proof
himmelma@33175
   448
  fix x y :: "'a::metric_space"
himmelma@33175
   449
  assume xy: "x \<noteq> y"
himmelma@33175
   450
  let ?U = "ball x (dist x y / 2)"
himmelma@33175
   451
  let ?V = "ball y (dist x y / 2)"
himmelma@33175
   452
  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
   453
               ==> ~(d x y * 2 < d x z \<and> d z y * 2 < d x z)" by arith
himmelma@33175
   454
  have "open ?U \<and> open ?V \<and> x \<in> ?U \<and> y \<in> ?V \<and> ?U \<inter> ?V = {}"
himmelma@33175
   455
    using dist_pos_lt[OF xy] th0[of dist,OF dist_triangle dist_commute]
himmelma@33175
   456
    by (auto simp add: expand_set_eq)
himmelma@33175
   457
  then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
himmelma@33175
   458
    by blast
himmelma@33175
   459
qed
himmelma@33175
   460
himmelma@33175
   461
lemma separation_t2:
himmelma@33175
   462
  fixes x y :: "'a::t2_space"
himmelma@33175
   463
  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
   464
  using hausdorff[of x y] by blast
himmelma@33175
   465
himmelma@33175
   466
lemma separation_t1:
himmelma@33175
   467
  fixes x y :: "'a::t1_space"
himmelma@33175
   468
  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
   469
  using t1_space[of x y] by blast
himmelma@33175
   470
himmelma@33175
   471
lemma separation_t0:
himmelma@33175
   472
  fixes x y :: "'a::t0_space"
himmelma@33175
   473
  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> ~(x\<in>U \<longleftrightarrow> y\<in>U))"
himmelma@33175
   474
  using t0_space[of x y] by blast
himmelma@33175
   475
himmelma@33175
   476
subsection{* Limit points *}
himmelma@33175
   477
himmelma@33175
   478
definition
himmelma@33175
   479
  islimpt:: "'a::topological_space \<Rightarrow> 'a set \<Rightarrow> bool"
himmelma@33175
   480
    (infixr "islimpt" 60) where
himmelma@33175
   481
  "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
   482
himmelma@33175
   483
lemma islimptI:
himmelma@33175
   484
  assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
himmelma@33175
   485
  shows "x islimpt S"
himmelma@33175
   486
  using assms unfolding islimpt_def by auto
himmelma@33175
   487
himmelma@33175
   488
lemma islimptE:
himmelma@33175
   489
  assumes "x islimpt S" and "x \<in> T" and "open T"
himmelma@33175
   490
  obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"
himmelma@33175
   491
  using assms unfolding islimpt_def by auto
himmelma@33175
   492
himmelma@33175
   493
lemma islimpt_subset: "x islimpt S \<Longrightarrow> S \<subseteq> T ==> x islimpt T" by (auto simp add: islimpt_def)
himmelma@33175
   494
himmelma@33175
   495
lemma islimpt_approachable:
himmelma@33175
   496
  fixes x :: "'a::metric_space"
himmelma@33175
   497
  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"
himmelma@33175
   498
  unfolding islimpt_def
himmelma@33175
   499
  apply auto
himmelma@33175
   500
  apply(erule_tac x="ball x e" in allE)
himmelma@33175
   501
  apply auto
himmelma@33175
   502
  apply(rule_tac x=y in bexI)
himmelma@33175
   503
  apply (auto simp add: dist_commute)
himmelma@33175
   504
  apply (simp add: open_dist, drule (1) bspec)
himmelma@33175
   505
  apply (clarify, drule spec, drule (1) mp, auto)
himmelma@33175
   506
  done
himmelma@33175
   507
himmelma@33175
   508
lemma islimpt_approachable_le:
himmelma@33175
   509
  fixes x :: "'a::metric_space"
himmelma@33175
   510
  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> S. x' \<noteq> x \<and> dist x' x <= e)"
himmelma@33175
   511
  unfolding islimpt_approachable
himmelma@33175
   512
  using approachable_lt_le[where f="\<lambda>x'. dist x' x" and P="\<lambda>x'. \<not> (x'\<in>S \<and> x'\<noteq>x)"]
paulson@33324
   513
  by metis 
himmelma@33175
   514
himmelma@33175
   515
class perfect_space =
himmelma@33175
   516
  (* FIXME: perfect_space should inherit from topological_space *)
himmelma@33175
   517
  assumes islimpt_UNIV [simp, intro]: "(x::'a::metric_space) islimpt UNIV"
himmelma@33175
   518
himmelma@33175
   519
lemma perfect_choose_dist:
himmelma@33175
   520
  fixes x :: "'a::perfect_space"
himmelma@33175
   521
  shows "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"
himmelma@33175
   522
using islimpt_UNIV [of x]
himmelma@33175
   523
by (simp add: islimpt_approachable)
himmelma@33175
   524
himmelma@33175
   525
instance real :: perfect_space
himmelma@33175
   526
apply default
himmelma@33175
   527
apply (rule islimpt_approachable [THEN iffD2])
himmelma@33175
   528
apply (clarify, rule_tac x="x + e/2" in bexI)
himmelma@33175
   529
apply (auto simp add: dist_norm)
himmelma@33175
   530
done
himmelma@33175
   531
hoelzl@34291
   532
instance cart :: (perfect_space, finite) perfect_space
himmelma@33175
   533
proof
himmelma@33175
   534
  fix x :: "'a ^ 'b"
himmelma@33175
   535
  {
himmelma@33175
   536
    fix e :: real assume "0 < e"
himmelma@33175
   537
    def a \<equiv> "x $ undefined"
himmelma@33175
   538
    have "a islimpt UNIV" by (rule islimpt_UNIV)
himmelma@33175
   539
    with `0 < e` obtain b where "b \<noteq> a" and "dist b a < e"
himmelma@33175
   540
      unfolding islimpt_approachable by auto
himmelma@33175
   541
    def y \<equiv> "Cart_lambda ((Cart_nth x)(undefined := b))"
himmelma@33175
   542
    from `b \<noteq> a` have "y \<noteq> x"
himmelma@33175
   543
      unfolding a_def y_def by (simp add: Cart_eq)
himmelma@33175
   544
    from `dist b a < e` have "dist y x < e"
himmelma@33175
   545
      unfolding dist_vector_def a_def y_def
himmelma@33175
   546
      apply simp
himmelma@33175
   547
      apply (rule le_less_trans [OF setL2_le_setsum [OF zero_le_dist]])
himmelma@33175
   548
      apply (subst setsum_diff1' [where a=undefined], simp, simp, simp)
himmelma@33175
   549
      done
himmelma@33175
   550
    from `y \<noteq> x` and `dist y x < e`
himmelma@33175
   551
    have "\<exists>y\<in>UNIV. y \<noteq> x \<and> dist y x < e" by auto
himmelma@33175
   552
  }
himmelma@33175
   553
  then show "x islimpt UNIV" unfolding islimpt_approachable by blast
himmelma@33175
   554
qed
himmelma@33175
   555
himmelma@33175
   556
lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"
himmelma@33175
   557
  unfolding closed_def
himmelma@33175
   558
  apply (subst open_subopen)
huffman@34105
   559
  apply (simp add: islimpt_def subset_eq)
huffman@34105
   560
  by (metis ComplE ComplI insertCI insert_absorb mem_def)
himmelma@33175
   561
himmelma@33175
   562
lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"
himmelma@33175
   563
  unfolding islimpt_def by auto
himmelma@33175
   564
hoelzl@34291
   565
lemma closed_positive_orthant: "closed {x::real^'n. \<forall>i. 0 \<le>x$i}"
himmelma@33175
   566
proof-
himmelma@33175
   567
  let ?U = "UNIV :: 'n set"
himmelma@33175
   568
  let ?O = "{x::real^'n. \<forall>i. x$i\<ge>0}"
himmelma@33175
   569
  {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
   570
    and xi: "x$i < 0"
himmelma@33175
   571
    from xi have th0: "-x$i > 0" by arith
himmelma@33175
   572
    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
   573
      have th:" \<And>b a (x::real). abs x <= b \<Longrightarrow> b <= a ==> ~(a + x < 0)" by arith
himmelma@33175
   574
      have th': "\<And>x (y::real). x < 0 \<Longrightarrow> 0 <= y ==> abs x <= abs (y - x)" by arith
himmelma@33175
   575
      have th1: "\<bar>x$i\<bar> \<le> \<bar>(x' - x)$i\<bar>" using x'(1) xi
himmelma@33175
   576
        apply (simp only: vector_component)
himmelma@33175
   577
        by (rule th') auto
himmelma@33175
   578
      have th2: "\<bar>dist x x'\<bar> \<ge> \<bar>(x' - x)$i\<bar>" using  component_le_norm[of "x'-x" i]
himmelma@33175
   579
        apply (simp add: dist_norm) by norm
himmelma@33175
   580
      from th[OF th1 th2] x'(3) have False by (simp add: dist_commute) }
himmelma@33175
   581
  then show ?thesis unfolding closed_limpt islimpt_approachable
himmelma@33175
   582
    unfolding not_le[symmetric] by blast
himmelma@33175
   583
qed
himmelma@33175
   584
himmelma@33175
   585
lemma finite_set_avoid:
himmelma@33175
   586
  fixes a :: "'a::metric_space"
himmelma@33175
   587
  assumes fS: "finite S" shows  "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d <= dist a x"
himmelma@33175
   588
proof(induct rule: finite_induct[OF fS])
himmelma@33175
   589
  case 1 thus ?case apply auto by ferrack
himmelma@33175
   590
next
himmelma@33175
   591
  case (2 x F)
himmelma@33175
   592
  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
   593
  {assume "x = a" hence ?case using d by auto  }
himmelma@33175
   594
  moreover
himmelma@33175
   595
  {assume xa: "x\<noteq>a"
himmelma@33175
   596
    let ?d = "min d (dist a x)"
himmelma@33175
   597
    have dp: "?d > 0" using xa d(1) using dist_nz by auto
himmelma@33175
   598
    from d have d': "\<forall>x\<in>F. x\<noteq>a \<longrightarrow> ?d \<le> dist a x" by auto
himmelma@33175
   599
    with dp xa have ?case by(auto intro!: exI[where x="?d"]) }
himmelma@33175
   600
  ultimately show ?case by blast
himmelma@33175
   601
qed
himmelma@33175
   602
himmelma@33175
   603
lemma islimpt_finite:
himmelma@33175
   604
  fixes S :: "'a::metric_space set"
himmelma@33175
   605
  assumes fS: "finite S" shows "\<not> a islimpt S"
himmelma@33175
   606
  unfolding islimpt_approachable
himmelma@33175
   607
  using finite_set_avoid[OF fS, of a] by (metis dist_commute  not_le)
himmelma@33175
   608
himmelma@33175
   609
lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"
himmelma@33175
   610
  apply (rule iffI)
himmelma@33175
   611
  defer
himmelma@33175
   612
  apply (metis Un_upper1 Un_upper2 islimpt_subset)
himmelma@33175
   613
  unfolding islimpt_def
himmelma@33175
   614
  apply (rule ccontr, clarsimp, rename_tac A B)
himmelma@33175
   615
  apply (drule_tac x="A \<inter> B" in spec)
himmelma@33175
   616
  apply (auto simp add: open_Int)
himmelma@33175
   617
  done
himmelma@33175
   618
himmelma@33175
   619
lemma discrete_imp_closed:
himmelma@33175
   620
  fixes S :: "'a::metric_space set"
himmelma@33175
   621
  assumes e: "0 < e" and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"
himmelma@33175
   622
  shows "closed S"
himmelma@33175
   623
proof-
himmelma@33175
   624
  {fix x assume C: "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e"
himmelma@33175
   625
    from e have e2: "e/2 > 0" by arith
himmelma@33175
   626
    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
   627
    let ?m = "min (e/2) (dist x y) "
himmelma@33175
   628
    from e2 y(2) have mp: "?m > 0" by (simp add: dist_nz[THEN sym])
himmelma@33175
   629
    from C[rule_format, OF mp] obtain z where z: "z \<in> S" "z\<noteq>x" "dist z x < ?m" by blast
himmelma@33175
   630
    have th: "dist z y < e" using z y
himmelma@33175
   631
      by (intro dist_triangle_lt [where z=x], simp)
himmelma@33175
   632
    from d[rule_format, OF y(1) z(1) th] y z
himmelma@33175
   633
    have False by (auto simp add: dist_commute)}
himmelma@33175
   634
  then show ?thesis by (metis islimpt_approachable closed_limpt [where 'a='a])
himmelma@33175
   635
qed
himmelma@33175
   636
himmelma@33175
   637
subsection{* Interior of a Set *}
himmelma@33175
   638
definition "interior S = {x. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S}"
himmelma@33175
   639
himmelma@33175
   640
lemma interior_eq: "interior S = S \<longleftrightarrow> open S"
himmelma@33175
   641
  apply (simp add: expand_set_eq interior_def)
himmelma@33175
   642
  apply (subst (2) open_subopen) by (safe, blast+)
himmelma@33175
   643
himmelma@33175
   644
lemma interior_open: "open S ==> (interior S = S)" by (metis interior_eq)
himmelma@33175
   645
himmelma@33175
   646
lemma interior_empty[simp]: "interior {} = {}" by (simp add: interior_def)
himmelma@33175
   647
himmelma@33175
   648
lemma open_interior[simp, intro]: "open(interior S)"
himmelma@33175
   649
  apply (simp add: interior_def)
himmelma@33175
   650
  apply (subst open_subopen) by blast
himmelma@33175
   651
himmelma@33175
   652
lemma interior_interior[simp]: "interior(interior S) = interior S" by (metis interior_eq open_interior)
himmelma@33175
   653
lemma interior_subset: "interior S \<subseteq> S" by (auto simp add: interior_def)
himmelma@33175
   654
lemma subset_interior: "S \<subseteq> T ==> (interior S) \<subseteq> (interior T)" by (auto simp add: interior_def)
himmelma@33175
   655
lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T ==> T \<subseteq> (interior S)" by (auto simp add: interior_def)
himmelma@33175
   656
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
   657
  by (metis equalityI interior_maximal interior_subset open_interior)
himmelma@33175
   658
lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e. 0 < e \<and> ball x e \<subseteq> S)"
himmelma@33175
   659
  apply (simp add: interior_def)
himmelma@33175
   660
  by (metis open_contains_ball centre_in_ball open_ball subset_trans)
himmelma@33175
   661
himmelma@33175
   662
lemma open_subset_interior: "open S ==> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"
himmelma@33175
   663
  by (metis interior_maximal interior_subset subset_trans)
himmelma@33175
   664
himmelma@33175
   665
lemma interior_inter[simp]: "interior(S \<inter> T) = interior S \<inter> interior T"
himmelma@33175
   666
  apply (rule equalityI, simp)
himmelma@33175
   667
  apply (metis Int_lower1 Int_lower2 subset_interior)
himmelma@33175
   668
  by (metis Int_mono interior_subset open_Int open_interior open_subset_interior)
himmelma@33175
   669
himmelma@33175
   670
lemma interior_limit_point [intro]:
himmelma@33175
   671
  fixes x :: "'a::perfect_space"
himmelma@33175
   672
  assumes x: "x \<in> interior S" shows "x islimpt S"
himmelma@33175
   673
proof-
himmelma@33175
   674
  from x obtain e where e: "e>0" "\<forall>x'. dist x x' < e \<longrightarrow> x' \<in> S"
himmelma@33175
   675
    unfolding mem_interior subset_eq Ball_def mem_ball by blast
himmelma@33175
   676
  {
himmelma@33175
   677
    fix d::real assume d: "d>0"
himmelma@33175
   678
    let ?m = "min d e"
himmelma@33175
   679
    have mde2: "0 < ?m" using e(1) d(1) by simp
himmelma@33175
   680
    from perfect_choose_dist [OF mde2, of x]
himmelma@33175
   681
    obtain y where "y \<noteq> x" and "dist y x < ?m" by blast
himmelma@33175
   682
    then have "dist y x < e" "dist y x < d" by simp_all
himmelma@33175
   683
    from `dist y x < e` e(2) have "y \<in> S" by (simp add: dist_commute)
himmelma@33175
   684
    have "\<exists>x'\<in>S. x'\<noteq> x \<and> dist x' x < d"
himmelma@33175
   685
      using `y \<in> S` `y \<noteq> x` `dist y x < d` by fast
himmelma@33175
   686
  }
himmelma@33175
   687
  then show ?thesis unfolding islimpt_approachable by blast
himmelma@33175
   688
qed
himmelma@33175
   689
himmelma@33175
   690
lemma interior_closed_Un_empty_interior:
himmelma@33175
   691
  assumes cS: "closed S" and iT: "interior T = {}"
himmelma@33175
   692
  shows "interior(S \<union> T) = interior S"
himmelma@33175
   693
proof
himmelma@33175
   694
  show "interior S \<subseteq> interior (S\<union>T)"
himmelma@33175
   695
    by (rule subset_interior, blast)
himmelma@33175
   696
next
himmelma@33175
   697
  show "interior (S \<union> T) \<subseteq> interior S"
himmelma@33175
   698
  proof
himmelma@33175
   699
    fix x assume "x \<in> interior (S \<union> T)"
himmelma@33175
   700
    then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T"
himmelma@33175
   701
      unfolding interior_def by fast
himmelma@33175
   702
    show "x \<in> interior S"
himmelma@33175
   703
    proof (rule ccontr)
himmelma@33175
   704
      assume "x \<notin> interior S"
himmelma@33175
   705
      with `x \<in> R` `open R` obtain y where "y \<in> R - S"
himmelma@33175
   706
        unfolding interior_def expand_set_eq by fast
himmelma@33175
   707
      from `open R` `closed S` have "open (R - S)" by (rule open_Diff)
himmelma@33175
   708
      from `R \<subseteq> S \<union> T` have "R - S \<subseteq> T" by fast
himmelma@33175
   709
      from `y \<in> R - S` `open (R - S)` `R - S \<subseteq> T` `interior T = {}`
himmelma@33175
   710
      show "False" unfolding interior_def by fast
himmelma@33175
   711
    qed
himmelma@33175
   712
  qed
himmelma@33175
   713
qed
himmelma@33175
   714
himmelma@33175
   715
himmelma@33175
   716
subsection{* Closure of a Set *}
himmelma@33175
   717
himmelma@33175
   718
definition "closure S = S \<union> {x | x. x islimpt S}"
himmelma@33175
   719
huffman@34105
   720
lemma closure_interior: "closure S = - interior (- S)"
himmelma@33175
   721
proof-
himmelma@33175
   722
  { fix x
huffman@34105
   723
    have "x\<in>- interior (- S) \<longleftrightarrow> x \<in> closure S"  (is "?lhs = ?rhs")
himmelma@33175
   724
    proof
huffman@34105
   725
      let ?exT = "\<lambda> y. (\<exists>T. open T \<and> y \<in> T \<and> T \<subseteq> - S)"
himmelma@33175
   726
      assume "?lhs"
himmelma@33175
   727
      hence *:"\<not> ?exT x"
himmelma@33175
   728
        unfolding interior_def
himmelma@33175
   729
        by simp
himmelma@33175
   730
      { assume "\<not> ?rhs"
himmelma@33175
   731
        hence False using *
himmelma@33175
   732
          unfolding closure_def islimpt_def
himmelma@33175
   733
          by blast
himmelma@33175
   734
      }
himmelma@33175
   735
      thus "?rhs"
himmelma@33175
   736
        by blast
himmelma@33175
   737
    next
himmelma@33175
   738
      assume "?rhs" thus "?lhs"
himmelma@33175
   739
        unfolding closure_def interior_def islimpt_def
himmelma@33175
   740
        by blast
himmelma@33175
   741
    qed
himmelma@33175
   742
  }
himmelma@33175
   743
  thus ?thesis
himmelma@33175
   744
    by blast
himmelma@33175
   745
qed
himmelma@33175
   746
huffman@34105
   747
lemma interior_closure: "interior S = - (closure (- S))"
himmelma@33175
   748
proof-
himmelma@33175
   749
  { fix x
huffman@34105
   750
    have "x \<in> interior S \<longleftrightarrow> x \<in> - (closure (- S))"
himmelma@33175
   751
      unfolding interior_def closure_def islimpt_def
paulson@33324
   752
      by auto
himmelma@33175
   753
  }
himmelma@33175
   754
  thus ?thesis
himmelma@33175
   755
    by blast
himmelma@33175
   756
qed
himmelma@33175
   757
himmelma@33175
   758
lemma closed_closure[simp, intro]: "closed (closure S)"
himmelma@33175
   759
proof-
huffman@34105
   760
  have "closed (- interior (-S))" by blast
himmelma@33175
   761
  thus ?thesis using closure_interior[of S] by simp
himmelma@33175
   762
qed
himmelma@33175
   763
himmelma@33175
   764
lemma closure_hull: "closure S = closed hull S"
himmelma@33175
   765
proof-
himmelma@33175
   766
  have "S \<subseteq> closure S"
himmelma@33175
   767
    unfolding closure_def
himmelma@33175
   768
    by blast
himmelma@33175
   769
  moreover
himmelma@33175
   770
  have "closed (closure S)"
himmelma@33175
   771
    using closed_closure[of S]
himmelma@33175
   772
    by assumption
himmelma@33175
   773
  moreover
himmelma@33175
   774
  { fix t
himmelma@33175
   775
    assume *:"S \<subseteq> t" "closed t"
himmelma@33175
   776
    { fix x
himmelma@33175
   777
      assume "x islimpt S"
himmelma@33175
   778
      hence "x islimpt t" using *(1)
himmelma@33175
   779
        using islimpt_subset[of x, of S, of t]
himmelma@33175
   780
        by blast
himmelma@33175
   781
    }
himmelma@33175
   782
    with * have "closure S \<subseteq> t"
himmelma@33175
   783
      unfolding closure_def
himmelma@33175
   784
      using closed_limpt[of t]
himmelma@33175
   785
      by auto
himmelma@33175
   786
  }
himmelma@33175
   787
  ultimately show ?thesis
himmelma@33175
   788
    using hull_unique[of S, of "closure S", of closed]
himmelma@33175
   789
    unfolding mem_def
himmelma@33175
   790
    by simp
himmelma@33175
   791
qed
himmelma@33175
   792
himmelma@33175
   793
lemma closure_eq: "closure S = S \<longleftrightarrow> closed S"
himmelma@33175
   794
  unfolding closure_hull
himmelma@33175
   795
  using hull_eq[of closed, unfolded mem_def, OF  closed_Inter, of S]
himmelma@33175
   796
  by (metis mem_def subset_eq)
himmelma@33175
   797
himmelma@33175
   798
lemma closure_closed[simp]: "closed S \<Longrightarrow> closure S = S"
himmelma@33175
   799
  using closure_eq[of S]
himmelma@33175
   800
  by simp
himmelma@33175
   801
himmelma@33175
   802
lemma closure_closure[simp]: "closure (closure S) = closure S"
himmelma@33175
   803
  unfolding closure_hull
himmelma@33175
   804
  using hull_hull[of closed S]
himmelma@33175
   805
  by assumption
himmelma@33175
   806
himmelma@33175
   807
lemma closure_subset: "S \<subseteq> closure S"
himmelma@33175
   808
  unfolding closure_hull
himmelma@33175
   809
  using hull_subset[of S closed]
himmelma@33175
   810
  by assumption
himmelma@33175
   811
himmelma@33175
   812
lemma subset_closure: "S \<subseteq> T \<Longrightarrow> closure S \<subseteq> closure T"
himmelma@33175
   813
  unfolding closure_hull
himmelma@33175
   814
  using hull_mono[of S T closed]
himmelma@33175
   815
  by assumption
himmelma@33175
   816
himmelma@33175
   817
lemma closure_minimal: "S \<subseteq> T \<Longrightarrow>  closed T \<Longrightarrow> closure S \<subseteq> T"
himmelma@33175
   818
  using hull_minimal[of S T closed]
himmelma@33175
   819
  unfolding closure_hull mem_def
himmelma@33175
   820
  by simp
himmelma@33175
   821
himmelma@33175
   822
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
   823
  using hull_unique[of S T closed]
himmelma@33175
   824
  unfolding closure_hull mem_def
himmelma@33175
   825
  by simp
himmelma@33175
   826
himmelma@33175
   827
lemma closure_empty[simp]: "closure {} = {}"
himmelma@33175
   828
  using closed_empty closure_closed[of "{}"]
himmelma@33175
   829
  by simp
himmelma@33175
   830
himmelma@33175
   831
lemma closure_univ[simp]: "closure UNIV = UNIV"
himmelma@33175
   832
  using closure_closed[of UNIV]
himmelma@33175
   833
  by simp
himmelma@33175
   834
himmelma@33175
   835
lemma closure_eq_empty: "closure S = {} \<longleftrightarrow> S = {}"
himmelma@33175
   836
  using closure_empty closure_subset[of S]
himmelma@33175
   837
  by blast
himmelma@33175
   838
himmelma@33175
   839
lemma closure_subset_eq: "closure S \<subseteq> S \<longleftrightarrow> closed S"
himmelma@33175
   840
  using closure_eq[of S] closure_subset[of S]
himmelma@33175
   841
  by simp
himmelma@33175
   842
himmelma@33175
   843
lemma open_inter_closure_eq_empty:
himmelma@33175
   844
  "open S \<Longrightarrow> (S \<inter> closure T) = {} \<longleftrightarrow> S \<inter> T = {}"
huffman@34105
   845
  using open_subset_interior[of S "- T"]
huffman@34105
   846
  using interior_subset[of "- T"]
himmelma@33175
   847
  unfolding closure_interior
himmelma@33175
   848
  by auto
himmelma@33175
   849
himmelma@33175
   850
lemma open_inter_closure_subset:
himmelma@33175
   851
  "open S \<Longrightarrow> (S \<inter> (closure T)) \<subseteq> closure(S \<inter> T)"
himmelma@33175
   852
proof
himmelma@33175
   853
  fix x
himmelma@33175
   854
  assume as: "open S" "x \<in> S \<inter> closure T"
himmelma@33175
   855
  { assume *:"x islimpt T"
himmelma@33175
   856
    have "x islimpt (S \<inter> T)"
himmelma@33175
   857
    proof (rule islimptI)
himmelma@33175
   858
      fix A
himmelma@33175
   859
      assume "x \<in> A" "open A"
himmelma@33175
   860
      with as have "x \<in> A \<inter> S" "open (A \<inter> S)"
himmelma@33175
   861
        by (simp_all add: open_Int)
himmelma@33175
   862
      with * obtain y where "y \<in> T" "y \<in> A \<inter> S" "y \<noteq> x"
himmelma@33175
   863
        by (rule islimptE)
himmelma@33175
   864
      hence "y \<in> S \<inter> T" "y \<in> A \<and> y \<noteq> x"
himmelma@33175
   865
        by simp_all
himmelma@33175
   866
      thus "\<exists>y\<in>(S \<inter> T). y \<in> A \<and> y \<noteq> x" ..
himmelma@33175
   867
    qed
himmelma@33175
   868
  }
himmelma@33175
   869
  then show "x \<in> closure (S \<inter> T)" using as
himmelma@33175
   870
    unfolding closure_def
himmelma@33175
   871
    by blast
himmelma@33175
   872
qed
himmelma@33175
   873
huffman@34105
   874
lemma closure_complement: "closure(- S) = - interior(S)"
himmelma@33175
   875
proof-
huffman@34105
   876
  have "S = - (- S)"
himmelma@33175
   877
    by auto
himmelma@33175
   878
  thus ?thesis
himmelma@33175
   879
    unfolding closure_interior
himmelma@33175
   880
    by auto
himmelma@33175
   881
qed
himmelma@33175
   882
huffman@34105
   883
lemma interior_complement: "interior(- S) = - closure(S)"
himmelma@33175
   884
  unfolding closure_interior
himmelma@33175
   885
  by blast
himmelma@33175
   886
himmelma@33175
   887
subsection{* Frontier (aka boundary) *}
himmelma@33175
   888
himmelma@33175
   889
definition "frontier S = closure S - interior S"
himmelma@33175
   890
himmelma@33175
   891
lemma frontier_closed: "closed(frontier S)"
himmelma@33175
   892
  by (simp add: frontier_def closed_Diff)
himmelma@33175
   893
huffman@34105
   894
lemma frontier_closures: "frontier S = (closure S) \<inter> (closure(- S))"
himmelma@33175
   895
  by (auto simp add: frontier_def interior_closure)
himmelma@33175
   896
himmelma@33175
   897
lemma frontier_straddle:
himmelma@33175
   898
  fixes a :: "'a::metric_space"
himmelma@33175
   899
  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
   900
proof
himmelma@33175
   901
  assume "?lhs"
himmelma@33175
   902
  { fix e::real
himmelma@33175
   903
    assume "e > 0"
himmelma@33175
   904
    let ?rhse = "(\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e)"
himmelma@33175
   905
    { assume "a\<in>S"
himmelma@33175
   906
      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
   907
      moreover have "\<exists>x. x \<notin> S \<and> dist a x < e" using `?lhs` `a\<in>S`
himmelma@33175
   908
        unfolding frontier_closures closure_def islimpt_def using `e>0`
himmelma@33175
   909
        by (auto, erule_tac x="ball a e" in allE, auto)
himmelma@33175
   910
      ultimately have ?rhse by auto
himmelma@33175
   911
    }
himmelma@33175
   912
    moreover
himmelma@33175
   913
    { assume "a\<notin>S"
himmelma@33175
   914
      hence ?rhse using `?lhs`
himmelma@33175
   915
        unfolding frontier_closures closure_def islimpt_def
himmelma@33175
   916
        using open_ball[of a e] `e > 0`
paulson@33324
   917
          by simp (metis centre_in_ball mem_ball open_ball) 
himmelma@33175
   918
    }
himmelma@33175
   919
    ultimately have ?rhse by auto
himmelma@33175
   920
  }
himmelma@33175
   921
  thus ?rhs by auto
himmelma@33175
   922
next
himmelma@33175
   923
  assume ?rhs
himmelma@33175
   924
  moreover
himmelma@33175
   925
  { fix T assume "a\<notin>S" and
himmelma@33175
   926
    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
   927
    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
   928
    then obtain e where "e>0" "ball a e \<subseteq> T" by auto
himmelma@33175
   929
    then obtain y where y:"y\<in>S" "dist a y < e"  using as(1) by auto
himmelma@33175
   930
    have "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> a"
himmelma@33175
   931
      using `dist a y < e` `ball a e \<subseteq> T` unfolding ball_def using `y\<in>S` `a\<notin>S` by auto
himmelma@33175
   932
  }
himmelma@33175
   933
  hence "a \<in> closure S" unfolding closure_def islimpt_def using `?rhs` by auto
himmelma@33175
   934
  moreover
himmelma@33175
   935
  { fix T assume "a \<in> T"  "open T" "a\<in>S"
himmelma@33175
   936
    then obtain e where "e>0" and balle: "ball a e \<subseteq> T" unfolding open_contains_ball using `?rhs` by auto
himmelma@33175
   937
    obtain x where "x \<notin> S" "dist a x < e" using `?rhs` using `e>0` by auto
huffman@34105
   938
    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
   939
  }
huffman@34105
   940
  hence "a islimpt (- S) \<or> a\<notin>S" unfolding islimpt_def by auto
huffman@34105
   941
  ultimately show ?lhs unfolding frontier_closures using closure_def[of "- S"] by auto
himmelma@33175
   942
qed
himmelma@33175
   943
himmelma@33175
   944
lemma frontier_subset_closed: "closed S \<Longrightarrow> frontier S \<subseteq> S"
himmelma@33175
   945
  by (metis frontier_def closure_closed Diff_subset)
himmelma@33175
   946
hoelzl@34964
   947
lemma frontier_empty[simp]: "frontier {} = {}"
huffman@36362
   948
  by (simp add: frontier_def)
himmelma@33175
   949
himmelma@33175
   950
lemma frontier_subset_eq: "frontier S \<subseteq> S \<longleftrightarrow> closed S"
himmelma@33175
   951
proof-
himmelma@33175
   952
  { assume "frontier S \<subseteq> S"
himmelma@33175
   953
    hence "closure S \<subseteq> S" using interior_subset unfolding frontier_def by auto
himmelma@33175
   954
    hence "closed S" using closure_subset_eq by auto
himmelma@33175
   955
  }
huffman@36362
   956
  thus ?thesis using frontier_subset_closed[of S] ..
himmelma@33175
   957
qed
himmelma@33175
   958
huffman@34105
   959
lemma frontier_complement: "frontier(- S) = frontier S"
himmelma@33175
   960
  by (auto simp add: frontier_def closure_complement interior_complement)
himmelma@33175
   961
himmelma@33175
   962
lemma frontier_disjoint_eq: "frontier S \<inter> S = {} \<longleftrightarrow> open S"
huffman@34105
   963
  using frontier_complement frontier_subset_eq[of "- S"]
huffman@34105
   964
  unfolding open_closed by auto
himmelma@33175
   965
huffman@36437
   966
subsection {* Nets and the ``eventually true'' quantifier *}
huffman@36437
   967
huffman@36437
   968
text {* Common nets and The "within" modifier for nets. *}
himmelma@33175
   969
himmelma@33175
   970
definition
himmelma@33175
   971
  at_infinity :: "'a::real_normed_vector net" where
huffman@36358
   972
  "at_infinity = Abs_net (\<lambda>P. \<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x)"
himmelma@33175
   973
himmelma@33175
   974
definition
himmelma@33175
   975
  indirection :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> 'a net" (infixr "indirection" 70) where
himmelma@33175
   976
  "a indirection v = (at a) within {b. \<exists>c\<ge>0. b - a = scaleR c v}"
himmelma@33175
   977
himmelma@33175
   978
text{* Prove That They are all nets. *}
himmelma@33175
   979
huffman@36358
   980
lemma eventually_at_infinity:
huffman@36358
   981
  "eventually P at_infinity \<longleftrightarrow> (\<exists>b. \<forall>x. norm x >= b \<longrightarrow> P x)"
himmelma@33175
   982
unfolding at_infinity_def
huffman@36358
   983
proof (rule eventually_Abs_net, rule is_filter.intro)
huffman@36358
   984
  fix P Q :: "'a \<Rightarrow> bool"
huffman@36358
   985
  assume "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x" and "\<exists>s. \<forall>x. s \<le> norm x \<longrightarrow> Q x"
huffman@36358
   986
  then obtain r s where
huffman@36358
   987
    "\<forall>x. r \<le> norm x \<longrightarrow> P x" and "\<forall>x. s \<le> norm x \<longrightarrow> Q x" by auto
huffman@36358
   988
  then have "\<forall>x. max r s \<le> norm x \<longrightarrow> P x \<and> Q x" by simp
huffman@36358
   989
  then show "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x \<and> Q x" ..
huffman@36358
   990
qed auto
himmelma@33175
   991
huffman@36437
   992
text {* Identify Trivial limits, where we can't approach arbitrarily closely. *}
himmelma@33175
   993
himmelma@33175
   994
definition
himmelma@33175
   995
  trivial_limit :: "'a net \<Rightarrow> bool" where
huffman@36358
   996
  "trivial_limit net \<longleftrightarrow> eventually (\<lambda>x. False) net"
himmelma@33175
   997
himmelma@33175
   998
lemma trivial_limit_within:
himmelma@33175
   999
  shows "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"
himmelma@33175
  1000
proof
himmelma@33175
  1001
  assume "trivial_limit (at a within S)"
himmelma@33175
  1002
  thus "\<not> a islimpt S"
himmelma@33175
  1003
    unfolding trivial_limit_def
huffman@36358
  1004
    unfolding eventually_within eventually_at_topological
himmelma@33175
  1005
    unfolding islimpt_def
himmelma@33175
  1006
    apply (clarsimp simp add: expand_set_eq)
himmelma@33175
  1007
    apply (rename_tac T, rule_tac x=T in exI)
huffman@36358
  1008
    apply (clarsimp, drule_tac x=y in bspec, simp_all)
himmelma@33175
  1009
    done
himmelma@33175
  1010
next
himmelma@33175
  1011
  assume "\<not> a islimpt S"
himmelma@33175
  1012
  thus "trivial_limit (at a within S)"
himmelma@33175
  1013
    unfolding trivial_limit_def
huffman@36358
  1014
    unfolding eventually_within eventually_at_topological
himmelma@33175
  1015
    unfolding islimpt_def
huffman@36358
  1016
    apply clarsimp
huffman@36358
  1017
    apply (rule_tac x=T in exI)
huffman@36358
  1018
    apply auto
himmelma@33175
  1019
    done
himmelma@33175
  1020
qed
himmelma@33175
  1021
himmelma@33175
  1022
lemma trivial_limit_at_iff: "trivial_limit (at a) \<longleftrightarrow> \<not> a islimpt UNIV"
himmelma@33175
  1023
  using trivial_limit_within [of a UNIV]
himmelma@33175
  1024
  by (simp add: within_UNIV)
himmelma@33175
  1025
himmelma@33175
  1026
lemma trivial_limit_at:
himmelma@33175
  1027
  fixes a :: "'a::perfect_space"
himmelma@33175
  1028
  shows "\<not> trivial_limit (at a)"
himmelma@33175
  1029
  by (simp add: trivial_limit_at_iff)
himmelma@33175
  1030
himmelma@33175
  1031
lemma trivial_limit_at_infinity:
himmelma@33175
  1032
  "\<not> trivial_limit (at_infinity :: ('a::{real_normed_vector,zero_neq_one}) net)"
himmelma@33175
  1033
  (* FIXME: find a more appropriate type class *)
huffman@36358
  1034
  unfolding trivial_limit_def eventually_at_infinity
huffman@36358
  1035
  apply clarsimp
huffman@36358
  1036
  apply (rule_tac x="scaleR b (sgn 1)" in exI)
himmelma@33175
  1037
  apply (simp add: norm_sgn)
himmelma@33175
  1038
  done
himmelma@33175
  1039
hoelzl@34964
  1040
lemma trivial_limit_sequentially[intro]: "\<not> trivial_limit sequentially"
huffman@36358
  1041
  by (auto simp add: trivial_limit_def eventually_sequentially)
himmelma@33175
  1042
huffman@36437
  1043
text {* Some property holds "sufficiently close" to the limit point. *}
himmelma@33175
  1044
himmelma@33175
  1045
lemma eventually_at: (* FIXME: this replaces Limits.eventually_at *)
himmelma@33175
  1046
  "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"
himmelma@33175
  1047
unfolding eventually_at dist_nz by auto
himmelma@33175
  1048
himmelma@33175
  1049
lemma eventually_within: "eventually P (at a within S) \<longleftrightarrow>
himmelma@33175
  1050
        (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"
himmelma@33175
  1051
unfolding eventually_within eventually_at dist_nz by auto
himmelma@33175
  1052
himmelma@33175
  1053
lemma eventually_within_le: "eventually P (at a within S) \<longleftrightarrow>
himmelma@33175
  1054
        (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a <= d \<longrightarrow> P x)" (is "?lhs = ?rhs")
himmelma@33175
  1055
unfolding eventually_within
paulson@33324
  1056
by auto (metis Rats_dense_in_nn_real order_le_less_trans order_refl) 
himmelma@33175
  1057
himmelma@33175
  1058
lemma eventually_happens: "eventually P net ==> trivial_limit net \<or> (\<exists>x. P x)"
huffman@36358
  1059
  unfolding trivial_limit_def
huffman@36358
  1060
  by (auto elim: eventually_rev_mp)
himmelma@33175
  1061
himmelma@33175
  1062
lemma always_eventually: "(\<forall>x. P x) ==> eventually P net"
huffman@36358
  1063
proof -
huffman@36358
  1064
  assume "\<forall>x. P x" hence "P = (\<lambda>x. True)" by (simp add: ext)
huffman@36358
  1065
  thus "eventually P net" by simp
huffman@36358
  1066
qed
himmelma@33175
  1067
himmelma@33175
  1068
lemma trivial_limit_eventually: "trivial_limit net \<Longrightarrow> eventually P net"
huffman@36358
  1069
  unfolding trivial_limit_def by (auto elim: eventually_rev_mp)
himmelma@33175
  1070
himmelma@33175
  1071
lemma eventually_False: "eventually (\<lambda>x. False) net \<longleftrightarrow> trivial_limit net"
huffman@36358
  1072
  unfolding trivial_limit_def ..
himmelma@33175
  1073
himmelma@33175
  1074
lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"
himmelma@33175
  1075
  apply (safe elim!: trivial_limit_eventually)
himmelma@33175
  1076
  apply (simp add: eventually_False [symmetric])
himmelma@33175
  1077
  done
himmelma@33175
  1078
himmelma@33175
  1079
text{* Combining theorems for "eventually" *}
himmelma@33175
  1080
himmelma@33175
  1081
lemma eventually_conjI:
himmelma@33175
  1082
  "\<lbrakk>eventually (\<lambda>x. P x) net; eventually (\<lambda>x. Q x) net\<rbrakk>
himmelma@33175
  1083
    \<Longrightarrow> eventually (\<lambda>x. P x \<and> Q x) net"
himmelma@33175
  1084
by (rule eventually_conj)
himmelma@33175
  1085
himmelma@33175
  1086
lemma eventually_rev_mono:
himmelma@33175
  1087
  "eventually P net \<Longrightarrow> (\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually Q net"
himmelma@33175
  1088
using eventually_mono [of P Q] by fast
himmelma@33175
  1089
himmelma@33175
  1090
lemma eventually_and: " eventually (\<lambda>x. P x \<and> Q x) net \<longleftrightarrow> eventually P net \<and> eventually Q net"
himmelma@33175
  1091
  by (auto intro!: eventually_conjI elim: eventually_rev_mono)
himmelma@33175
  1092
himmelma@33175
  1093
lemma eventually_false: "eventually (\<lambda>x. False) net \<longleftrightarrow> trivial_limit net"
himmelma@33175
  1094
  by (auto simp add: eventually_False)
himmelma@33175
  1095
himmelma@33175
  1096
lemma not_eventually: "(\<forall>x. \<not> P x ) \<Longrightarrow> ~(trivial_limit net) ==> ~(eventually (\<lambda>x. P x) net)"
himmelma@33175
  1097
  by (simp add: eventually_False)
himmelma@33175
  1098
huffman@36437
  1099
subsection {* Limits *}
himmelma@33175
  1100
himmelma@33175
  1101
  text{* Notation Lim to avoid collition with lim defined in analysis *}
himmelma@33175
  1102
definition
himmelma@33175
  1103
  Lim :: "'a net \<Rightarrow> ('a \<Rightarrow> 'b::t2_space) \<Rightarrow> 'b" where
himmelma@33175
  1104
  "Lim net f = (THE l. (f ---> l) net)"
himmelma@33175
  1105
himmelma@33175
  1106
lemma Lim:
himmelma@33175
  1107
 "(f ---> l) net \<longleftrightarrow>
himmelma@33175
  1108
        trivial_limit net \<or>
himmelma@33175
  1109
        (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"
himmelma@33175
  1110
  unfolding tendsto_iff trivial_limit_eq by auto
himmelma@33175
  1111
himmelma@33175
  1112
himmelma@33175
  1113
text{* Show that they yield usual definitions in the various cases. *}
himmelma@33175
  1114
himmelma@33175
  1115
lemma Lim_within_le: "(f ---> l)(at a within S) \<longleftrightarrow>
himmelma@33175
  1116
           (\<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
  1117
  by (auto simp add: tendsto_iff eventually_within_le)
himmelma@33175
  1118
himmelma@33175
  1119
lemma Lim_within: "(f ---> l) (at a within S) \<longleftrightarrow>
himmelma@33175
  1120
        (\<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
  1121
  by (auto simp add: tendsto_iff eventually_within)
himmelma@33175
  1122
himmelma@33175
  1123
lemma Lim_at: "(f ---> l) (at a) \<longleftrightarrow>
himmelma@33175
  1124
        (\<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
  1125
  by (auto simp add: tendsto_iff eventually_at)
himmelma@33175
  1126
himmelma@33175
  1127
lemma Lim_at_iff_LIM: "(f ---> l) (at a) \<longleftrightarrow> f -- a --> l"
himmelma@33175
  1128
  unfolding Lim_at LIM_def by (simp only: zero_less_dist_iff)
himmelma@33175
  1129
himmelma@33175
  1130
lemma Lim_at_infinity:
himmelma@33175
  1131
  "(f ---> l) at_infinity \<longleftrightarrow> (\<forall>e>0. \<exists>b. \<forall>x. norm x >= b \<longrightarrow> dist (f x) l < e)"
himmelma@33175
  1132
  by (auto simp add: tendsto_iff eventually_at_infinity)
himmelma@33175
  1133
himmelma@33175
  1134
lemma Lim_sequentially:
himmelma@33175
  1135
 "(S ---> l) sequentially \<longleftrightarrow>
himmelma@33175
  1136
          (\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (S n) l < e)"
himmelma@33175
  1137
  by (auto simp add: tendsto_iff eventually_sequentially)
himmelma@33175
  1138
himmelma@33175
  1139
lemma Lim_sequentially_iff_LIMSEQ: "(S ---> l) sequentially \<longleftrightarrow> S ----> l"
himmelma@33175
  1140
  unfolding Lim_sequentially LIMSEQ_def ..
himmelma@33175
  1141
himmelma@33175
  1142
lemma Lim_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> (f ---> l) net"
himmelma@33175
  1143
  by (rule topological_tendstoI, auto elim: eventually_rev_mono)
himmelma@33175
  1144
himmelma@33175
  1145
text{* The expected monotonicity property. *}
himmelma@33175
  1146
himmelma@33175
  1147
lemma Lim_within_empty: "(f ---> l) (net within {})"
himmelma@33175
  1148
  unfolding tendsto_def Limits.eventually_within by simp
himmelma@33175
  1149
himmelma@33175
  1150
lemma Lim_within_subset: "(f ---> l) (net within S) \<Longrightarrow> T \<subseteq> S \<Longrightarrow> (f ---> l) (net within T)"
himmelma@33175
  1151
  unfolding tendsto_def Limits.eventually_within
himmelma@33175
  1152
  by (auto elim!: eventually_elim1)
himmelma@33175
  1153
himmelma@33175
  1154
lemma Lim_Un: assumes "(f ---> l) (net within S)" "(f ---> l) (net within T)"
himmelma@33175
  1155
  shows "(f ---> l) (net within (S \<union> T))"
himmelma@33175
  1156
  using assms unfolding tendsto_def Limits.eventually_within
himmelma@33175
  1157
  apply clarify
himmelma@33175
  1158
  apply (drule spec, drule (1) mp, drule (1) mp)
himmelma@33175
  1159
  apply (drule spec, drule (1) mp, drule (1) mp)
himmelma@33175
  1160
  apply (auto elim: eventually_elim2)
himmelma@33175
  1161
  done
himmelma@33175
  1162
himmelma@33175
  1163
lemma Lim_Un_univ:
himmelma@33175
  1164
 "(f ---> l) (net within S) \<Longrightarrow> (f ---> l) (net within T) \<Longrightarrow>  S \<union> T = UNIV
himmelma@33175
  1165
        ==> (f ---> l) net"
himmelma@33175
  1166
  by (metis Lim_Un within_UNIV)
himmelma@33175
  1167
himmelma@33175
  1168
text{* Interrelations between restricted and unrestricted limits. *}
himmelma@33175
  1169
himmelma@33175
  1170
lemma Lim_at_within: "(f ---> l) net ==> (f ---> l)(net within S)"
himmelma@33175
  1171
  (* FIXME: rename *)
himmelma@33175
  1172
  unfolding tendsto_def Limits.eventually_within
himmelma@33175
  1173
  apply (clarify, drule spec, drule (1) mp, drule (1) mp)
himmelma@33175
  1174
  by (auto elim!: eventually_elim1)
himmelma@33175
  1175
himmelma@33175
  1176
lemma Lim_within_open:
himmelma@33175
  1177
  fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
himmelma@33175
  1178
  assumes"a \<in> S" "open S"
himmelma@33175
  1179
  shows "(f ---> l)(at a within S) \<longleftrightarrow> (f ---> l)(at a)" (is "?lhs \<longleftrightarrow> ?rhs")
himmelma@33175
  1180
proof
himmelma@33175
  1181
  assume ?lhs
himmelma@33175
  1182
  { fix A assume "open A" "l \<in> A"
himmelma@33175
  1183
    with `?lhs` have "eventually (\<lambda>x. f x \<in> A) (at a within S)"
himmelma@33175
  1184
      by (rule topological_tendstoD)
himmelma@33175
  1185
    hence "eventually (\<lambda>x. x \<in> S \<longrightarrow> f x \<in> A) (at a)"
himmelma@33175
  1186
      unfolding Limits.eventually_within .
himmelma@33175
  1187
    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
  1188
      unfolding eventually_at_topological by fast
himmelma@33175
  1189
    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
  1190
      using assms by auto
himmelma@33175
  1191
    hence "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. x \<noteq> a \<longrightarrow> f x \<in> A)"
himmelma@33175
  1192
      by fast
himmelma@33175
  1193
    hence "eventually (\<lambda>x. f x \<in> A) (at a)"
himmelma@33175
  1194
      unfolding eventually_at_topological .
himmelma@33175
  1195
  }
himmelma@33175
  1196
  thus ?rhs by (rule topological_tendstoI)
himmelma@33175
  1197
next
himmelma@33175
  1198
  assume ?rhs
himmelma@33175
  1199
  thus ?lhs by (rule Lim_at_within)
himmelma@33175
  1200
qed
himmelma@33175
  1201
himmelma@33175
  1202
text{* Another limit point characterization. *}
himmelma@33175
  1203
himmelma@33175
  1204
lemma islimpt_sequential:
himmelma@33175
  1205
  fixes x :: "'a::metric_space" (* FIXME: generalize to topological_space *)
himmelma@33175
  1206
  shows "x islimpt S \<longleftrightarrow> (\<exists>f. (\<forall>n::nat. f n \<in> S -{x}) \<and> (f ---> x) sequentially)"
himmelma@33175
  1207
    (is "?lhs = ?rhs")
himmelma@33175
  1208
proof
himmelma@33175
  1209
  assume ?lhs
himmelma@33175
  1210
  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
  1211
    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
  1212
  { fix n::nat
himmelma@33175
  1213
    have "f (inverse (real n + 1)) \<in> S - {x}" using f by auto
himmelma@33175
  1214
  }
himmelma@33175
  1215
  moreover
himmelma@33175
  1216
  { fix e::real assume "e>0"
himmelma@33175
  1217
    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
  1218
    then obtain N::nat where "inverse (real (N + 1)) < e" by auto
himmelma@33175
  1219
    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
  1220
    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
  1221
    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
  1222
  }
himmelma@33175
  1223
  hence " ((\<lambda>n. f (inverse (real n + 1))) ---> x) sequentially"
himmelma@33175
  1224
    unfolding Lim_sequentially using f by auto
himmelma@33175
  1225
  ultimately show ?rhs apply (rule_tac x="(\<lambda>n::nat. f (inverse (real n + 1)))" in exI) by auto
himmelma@33175
  1226
next
himmelma@33175
  1227
  assume ?rhs
himmelma@33175
  1228
  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
  1229
  { fix e::real assume "e>0"
himmelma@33175
  1230
    then obtain N where "dist (f N) x < e" using f(2) by auto
himmelma@33175
  1231
    moreover have "f N\<in>S" "f N \<noteq> x" using f(1) by auto
himmelma@33175
  1232
    ultimately have "\<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e" by auto
himmelma@33175
  1233
  }
himmelma@33175
  1234
  thus ?lhs unfolding islimpt_approachable by auto
himmelma@33175
  1235
qed
himmelma@33175
  1236
himmelma@33175
  1237
text{* Basic arithmetical combining theorems for limits. *}
himmelma@33175
  1238
himmelma@33175
  1239
lemma Lim_linear:
himmelma@33175
  1240
  assumes "(f ---> l) net" "bounded_linear h"
himmelma@33175
  1241
  shows "((\<lambda>x. h (f x)) ---> h l) net"
himmelma@33175
  1242
using `bounded_linear h` `(f ---> l) net`
himmelma@33175
  1243
by (rule bounded_linear.tendsto)
himmelma@33175
  1244
himmelma@33175
  1245
lemma Lim_ident_at: "((\<lambda>x. x) ---> a) (at a)"
himmelma@33175
  1246
  unfolding tendsto_def Limits.eventually_at_topological by fast
himmelma@33175
  1247
hoelzl@34964
  1248
lemma Lim_const[intro]: "((\<lambda>x. a) ---> a) net" by (rule tendsto_const)
hoelzl@34964
  1249
hoelzl@34964
  1250
lemma Lim_cmul[intro]:
himmelma@33175
  1251
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1252
  shows "(f ---> l) net ==> ((\<lambda>x. c *\<^sub>R f x) ---> c *\<^sub>R l) net"
himmelma@33175
  1253
  by (intro tendsto_intros)
himmelma@33175
  1254
himmelma@33175
  1255
lemma Lim_neg:
himmelma@33175
  1256
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1257
  shows "(f ---> l) net ==> ((\<lambda>x. -(f x)) ---> -l) net"
himmelma@33175
  1258
  by (rule tendsto_minus)
himmelma@33175
  1259
himmelma@33175
  1260
lemma Lim_add: fixes f :: "'a \<Rightarrow> 'b::real_normed_vector" shows
himmelma@33175
  1261
 "(f ---> l) net \<Longrightarrow> (g ---> m) net \<Longrightarrow> ((\<lambda>x. f(x) + g(x)) ---> l + m) net"
himmelma@33175
  1262
  by (rule tendsto_add)
himmelma@33175
  1263
himmelma@33175
  1264
lemma Lim_sub:
himmelma@33175
  1265
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1266
  shows "(f ---> l) net \<Longrightarrow> (g ---> m) net \<Longrightarrow> ((\<lambda>x. f(x) - g(x)) ---> l - m) net"
himmelma@33175
  1267
  by (rule tendsto_diff)
himmelma@33175
  1268
huffman@36437
  1269
lemma Lim_mul:
huffman@36437
  1270
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
huffman@36437
  1271
  assumes "(c ---> d) net"  "(f ---> l) net"
huffman@36437
  1272
  shows "((\<lambda>x. c(x) *\<^sub>R f x) ---> (d *\<^sub>R l)) net"
huffman@36437
  1273
  using assms by (rule scaleR.tendsto)
huffman@36437
  1274
huffman@36437
  1275
lemma Lim_inv:
huffman@36437
  1276
  fixes f :: "'a \<Rightarrow> real"
huffman@36437
  1277
  assumes "(f ---> l) (net::'a net)"  "l \<noteq> 0"
huffman@36437
  1278
  shows "((inverse o f) ---> inverse l) net"
huffman@36437
  1279
  unfolding o_def using assms by (rule tendsto_inverse)
huffman@36437
  1280
huffman@36437
  1281
lemma Lim_vmul:
huffman@36437
  1282
  fixes c :: "'a \<Rightarrow> real" and v :: "'b::real_normed_vector"
huffman@36437
  1283
  shows "(c ---> d) net ==> ((\<lambda>x. c(x) *\<^sub>R v) ---> d *\<^sub>R v) net"
huffman@36437
  1284
  by (intro tendsto_intros)
huffman@36437
  1285
himmelma@33175
  1286
lemma Lim_null:
himmelma@33175
  1287
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1288
  shows "(f ---> l) net \<longleftrightarrow> ((\<lambda>x. f(x) - l) ---> 0) net" by (simp add: Lim dist_norm)
himmelma@33175
  1289
himmelma@33175
  1290
lemma Lim_null_norm:
himmelma@33175
  1291
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1292
  shows "(f ---> 0) net \<longleftrightarrow> ((\<lambda>x. norm(f x)) ---> 0) net"
himmelma@33175
  1293
  by (simp add: Lim dist_norm)
himmelma@33175
  1294
himmelma@33175
  1295
lemma Lim_null_comparison:
himmelma@33175
  1296
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1297
  assumes "eventually (\<lambda>x. norm (f x) \<le> g x) net" "(g ---> 0) net"
himmelma@33175
  1298
  shows "(f ---> 0) net"
himmelma@33175
  1299
proof(simp add: tendsto_iff, rule+)
himmelma@33175
  1300
  fix e::real assume "0<e"
himmelma@33175
  1301
  { fix x
himmelma@33175
  1302
    assume "norm (f x) \<le> g x" "dist (g x) 0 < e"
himmelma@33175
  1303
    hence "dist (f x) 0 < e" by (simp add: dist_norm)
himmelma@33175
  1304
  }
himmelma@33175
  1305
  thus "eventually (\<lambda>x. dist (f x) 0 < e) net"
himmelma@33175
  1306
    using eventually_and[of "\<lambda>x. norm(f x) <= g x" "\<lambda>x. dist (g x) 0 < e" net]
himmelma@33175
  1307
    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
  1308
    using assms `e>0` unfolding tendsto_iff by auto
himmelma@33175
  1309
qed
himmelma@33175
  1310
himmelma@33175
  1311
lemma Lim_component:
hoelzl@34291
  1312
  fixes f :: "'a \<Rightarrow> 'b::metric_space ^ 'n"
himmelma@33175
  1313
  shows "(f ---> l) net \<Longrightarrow> ((\<lambda>a. f a $i) ---> l$i) net"
himmelma@33175
  1314
  unfolding tendsto_iff
himmelma@33175
  1315
  apply (clarify)
himmelma@33175
  1316
  apply (drule spec, drule (1) mp)
himmelma@33175
  1317
  apply (erule eventually_elim1)
himmelma@33175
  1318
  apply (erule le_less_trans [OF dist_nth_le])
himmelma@33175
  1319
  done
himmelma@33175
  1320
himmelma@33175
  1321
lemma Lim_transform_bound:
himmelma@33175
  1322
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1323
  fixes g :: "'a \<Rightarrow> 'c::real_normed_vector"
himmelma@33175
  1324
  assumes "eventually (\<lambda>n. norm(f n) <= norm(g n)) net"  "(g ---> 0) net"
himmelma@33175
  1325
  shows "(f ---> 0) net"
himmelma@33175
  1326
proof (rule tendstoI)
himmelma@33175
  1327
  fix e::real assume "e>0"
himmelma@33175
  1328
  { fix x
himmelma@33175
  1329
    assume "norm (f x) \<le> norm (g x)" "dist (g x) 0 < e"
himmelma@33175
  1330
    hence "dist (f x) 0 < e" by (simp add: dist_norm)}
himmelma@33175
  1331
  thus "eventually (\<lambda>x. dist (f x) 0 < e) net"
himmelma@33175
  1332
    using eventually_and[of "\<lambda>x. norm (f x) \<le> norm (g x)" "\<lambda>x. dist (g x) 0 < e" net]
himmelma@33175
  1333
    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
  1334
    using assms `e>0` unfolding tendsto_iff by blast
himmelma@33175
  1335
qed
himmelma@33175
  1336
himmelma@33175
  1337
text{* Deducing things about the limit from the elements. *}
himmelma@33175
  1338
himmelma@33175
  1339
lemma Lim_in_closed_set:
himmelma@33175
  1340
  assumes "closed S" "eventually (\<lambda>x. f(x) \<in> S) net" "\<not>(trivial_limit net)" "(f ---> l) net"
himmelma@33175
  1341
  shows "l \<in> S"
himmelma@33175
  1342
proof (rule ccontr)
himmelma@33175
  1343
  assume "l \<notin> S"
himmelma@33175
  1344
  with `closed S` have "open (- S)" "l \<in> - S"
himmelma@33175
  1345
    by (simp_all add: open_Compl)
himmelma@33175
  1346
  with assms(4) have "eventually (\<lambda>x. f x \<in> - S) net"
himmelma@33175
  1347
    by (rule topological_tendstoD)
himmelma@33175
  1348
  with assms(2) have "eventually (\<lambda>x. False) net"
himmelma@33175
  1349
    by (rule eventually_elim2) simp
himmelma@33175
  1350
  with assms(3) show "False"
himmelma@33175
  1351
    by (simp add: eventually_False)
himmelma@33175
  1352
qed
himmelma@33175
  1353
himmelma@33175
  1354
text{* Need to prove closed(cball(x,e)) before deducing this as a corollary. *}
himmelma@33175
  1355
himmelma@33175
  1356
lemma Lim_dist_ubound:
himmelma@33175
  1357
  assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. dist a (f x) <= e) net"
himmelma@33175
  1358
  shows "dist a l <= e"
himmelma@33175
  1359
proof (rule ccontr)
himmelma@33175
  1360
  assume "\<not> dist a l \<le> e"
himmelma@33175
  1361
  then have "0 < dist a l - e" by simp
himmelma@33175
  1362
  with assms(2) have "eventually (\<lambda>x. dist (f x) l < dist a l - e) net"
himmelma@33175
  1363
    by (rule tendstoD)
himmelma@33175
  1364
  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
  1365
    by (rule eventually_conjI)
himmelma@33175
  1366
  then obtain w where "dist a (f w) \<le> e" "dist (f w) l < dist a l - e"
himmelma@33175
  1367
    using assms(1) eventually_happens by auto
himmelma@33175
  1368
  hence "dist a (f w) + dist (f w) l < e + (dist a l - e)"
himmelma@33175
  1369
    by (rule add_le_less_mono)
himmelma@33175
  1370
  hence "dist a (f w) + dist (f w) l < dist a l"
himmelma@33175
  1371
    by simp
himmelma@33175
  1372
  also have "\<dots> \<le> dist a (f w) + dist (f w) l"
himmelma@33175
  1373
    by (rule dist_triangle)
himmelma@33175
  1374
  finally show False by simp
himmelma@33175
  1375
qed
himmelma@33175
  1376
himmelma@33175
  1377
lemma Lim_norm_ubound:
himmelma@33175
  1378
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1379
  assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. norm(f x) <= e) net"
himmelma@33175
  1380
  shows "norm(l) <= e"
himmelma@33175
  1381
proof (rule ccontr)
himmelma@33175
  1382
  assume "\<not> norm l \<le> e"
himmelma@33175
  1383
  then have "0 < norm l - e" by simp
himmelma@33175
  1384
  with assms(2) have "eventually (\<lambda>x. dist (f x) l < norm l - e) net"
himmelma@33175
  1385
    by (rule tendstoD)
himmelma@33175
  1386
  with assms(3) have "eventually (\<lambda>x. norm (f x) \<le> e \<and> dist (f x) l < norm l - e) net"
himmelma@33175
  1387
    by (rule eventually_conjI)
himmelma@33175
  1388
  then obtain w where "norm (f w) \<le> e" "dist (f w) l < norm l - e"
himmelma@33175
  1389
    using assms(1) eventually_happens by auto
himmelma@33175
  1390
  hence "norm (f w - l) < norm l - e" "norm (f w) \<le> e" by (simp_all add: dist_norm)
himmelma@33175
  1391
  hence "norm (f w - l) + norm (f w) < norm l" by simp
himmelma@33175
  1392
  hence "norm (f w - l - f w) < norm l" by (rule le_less_trans [OF norm_triangle_ineq4])
himmelma@33175
  1393
  thus False using `\<not> norm l \<le> e` by simp
himmelma@33175
  1394
qed
himmelma@33175
  1395
himmelma@33175
  1396
lemma Lim_norm_lbound:
himmelma@33175
  1397
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1398
  assumes "\<not> (trivial_limit net)"  "(f ---> l) net"  "eventually (\<lambda>x. e <= norm(f x)) net"
himmelma@33175
  1399
  shows "e \<le> norm l"
himmelma@33175
  1400
proof (rule ccontr)
himmelma@33175
  1401
  assume "\<not> e \<le> norm l"
himmelma@33175
  1402
  then have "0 < e - norm l" by simp
himmelma@33175
  1403
  with assms(2) have "eventually (\<lambda>x. dist (f x) l < e - norm l) net"
himmelma@33175
  1404
    by (rule tendstoD)
himmelma@33175
  1405
  with assms(3) have "eventually (\<lambda>x. e \<le> norm (f x) \<and> dist (f x) l < e - norm l) net"
himmelma@33175
  1406
    by (rule eventually_conjI)
himmelma@33175
  1407
  then obtain w where "e \<le> norm (f w)" "dist (f w) l < e - norm l"
himmelma@33175
  1408
    using assms(1) eventually_happens by auto
himmelma@33175
  1409
  hence "norm (f w - l) + norm l < e" "e \<le> norm (f w)" by (simp_all add: dist_norm)
himmelma@33175
  1410
  hence "norm (f w - l) + norm l < norm (f w)" by (rule less_le_trans)
himmelma@33175
  1411
  hence "norm (f w - l + l) < norm (f w)" by (rule le_less_trans [OF norm_triangle_ineq])
himmelma@33175
  1412
  thus False by simp
himmelma@33175
  1413
qed
himmelma@33175
  1414
himmelma@33175
  1415
text{* Uniqueness of the limit, when nontrivial. *}
himmelma@33175
  1416
himmelma@33175
  1417
lemma Lim_unique:
himmelma@33175
  1418
  fixes f :: "'a \<Rightarrow> 'b::t2_space"
himmelma@33175
  1419
  assumes "\<not> trivial_limit net"  "(f ---> l) net"  "(f ---> l') net"
himmelma@33175
  1420
  shows "l = l'"
himmelma@33175
  1421
proof (rule ccontr)
himmelma@33175
  1422
  assume "l \<noteq> l'"
himmelma@33175
  1423
  obtain U V where "open U" "open V" "l \<in> U" "l' \<in> V" "U \<inter> V = {}"
himmelma@33175
  1424
    using hausdorff [OF `l \<noteq> l'`] by fast
himmelma@33175
  1425
  have "eventually (\<lambda>x. f x \<in> U) net"
himmelma@33175
  1426
    using `(f ---> l) net` `open U` `l \<in> U` by (rule topological_tendstoD)
himmelma@33175
  1427
  moreover
himmelma@33175
  1428
  have "eventually (\<lambda>x. f x \<in> V) net"
himmelma@33175
  1429
    using `(f ---> l') net` `open V` `l' \<in> V` by (rule topological_tendstoD)
himmelma@33175
  1430
  ultimately
himmelma@33175
  1431
  have "eventually (\<lambda>x. False) net"
himmelma@33175
  1432
  proof (rule eventually_elim2)
himmelma@33175
  1433
    fix x
himmelma@33175
  1434
    assume "f x \<in> U" "f x \<in> V"
himmelma@33175
  1435
    hence "f x \<in> U \<inter> V" by simp
himmelma@33175
  1436
    with `U \<inter> V = {}` show "False" by simp
himmelma@33175
  1437
  qed
himmelma@33175
  1438
  with `\<not> trivial_limit net` show "False"
himmelma@33175
  1439
    by (simp add: eventually_False)
himmelma@33175
  1440
qed
himmelma@33175
  1441
himmelma@33175
  1442
lemma tendsto_Lim:
himmelma@33175
  1443
  fixes f :: "'a \<Rightarrow> 'b::t2_space"
himmelma@33175
  1444
  shows "~(trivial_limit net) \<Longrightarrow> (f ---> l) net ==> Lim net f = l"
himmelma@33175
  1445
  unfolding Lim_def using Lim_unique[of net f] by auto
himmelma@33175
  1446
himmelma@33175
  1447
text{* Limit under bilinear function *}
himmelma@33175
  1448
himmelma@33175
  1449
lemma Lim_bilinear:
himmelma@33175
  1450
  assumes "(f ---> l) net" and "(g ---> m) net" and "bounded_bilinear h"
himmelma@33175
  1451
  shows "((\<lambda>x. h (f x) (g x)) ---> (h l m)) net"
himmelma@33175
  1452
using `bounded_bilinear h` `(f ---> l) net` `(g ---> m) net`
himmelma@33175
  1453
by (rule bounded_bilinear.tendsto)
himmelma@33175
  1454
himmelma@33175
  1455
text{* These are special for limits out of the same vector space. *}
himmelma@33175
  1456
himmelma@33175
  1457
lemma Lim_within_id: "(id ---> a) (at a within s)"
himmelma@33175
  1458
  unfolding tendsto_def Limits.eventually_within eventually_at_topological
himmelma@33175
  1459
  by auto
himmelma@33175
  1460
huffman@36437
  1461
lemmas Lim_intros = Lim_add Lim_const Lim_sub Lim_cmul Lim_vmul Lim_within_id
huffman@36437
  1462
himmelma@33175
  1463
lemma Lim_at_id: "(id ---> a) (at a)"
himmelma@33175
  1464
apply (subst within_UNIV[symmetric]) by (simp add: Lim_within_id)
himmelma@33175
  1465
himmelma@33175
  1466
lemma Lim_at_zero:
himmelma@33175
  1467
  fixes a :: "'a::real_normed_vector"
himmelma@33175
  1468
  fixes l :: "'b::topological_space"
himmelma@33175
  1469
  shows "(f ---> l) (at a) \<longleftrightarrow> ((\<lambda>x. f(a + x)) ---> l) (at 0)" (is "?lhs = ?rhs")
himmelma@33175
  1470
proof
himmelma@33175
  1471
  assume "?lhs"
himmelma@33175
  1472
  { fix S assume "open S" "l \<in> S"
himmelma@33175
  1473
    with `?lhs` have "eventually (\<lambda>x. f x \<in> S) (at a)"
himmelma@33175
  1474
      by (rule topological_tendstoD)
himmelma@33175
  1475
    then obtain d where d: "d>0" "\<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<in> S"
himmelma@33175
  1476
      unfolding Limits.eventually_at by fast
himmelma@33175
  1477
    { fix x::"'a" assume "x \<noteq> 0 \<and> dist x 0 < d"
himmelma@33175
  1478
      hence "f (a + x) \<in> S" using d
himmelma@33175
  1479
      apply(erule_tac x="x+a" in allE)
haftmann@35820
  1480
      by (auto simp add: add_commute dist_norm dist_commute)
himmelma@33175
  1481
    }
himmelma@33175
  1482
    hence "\<exists>d>0. \<forall>x. x \<noteq> 0 \<and> dist x 0 < d \<longrightarrow> f (a + x) \<in> S"
himmelma@33175
  1483
      using d(1) by auto
himmelma@33175
  1484
    hence "eventually (\<lambda>x. f (a + x) \<in> S) (at 0)"
himmelma@33175
  1485
      unfolding Limits.eventually_at .
himmelma@33175
  1486
  }
himmelma@33175
  1487
  thus "?rhs" by (rule topological_tendstoI)
himmelma@33175
  1488
next
himmelma@33175
  1489
  assume "?rhs"
himmelma@33175
  1490
  { fix S assume "open S" "l \<in> S"
himmelma@33175
  1491
    with `?rhs` have "eventually (\<lambda>x. f (a + x) \<in> S) (at 0)"
himmelma@33175
  1492
      by (rule topological_tendstoD)
himmelma@33175
  1493
    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
  1494
      unfolding Limits.eventually_at by fast
himmelma@33175
  1495
    { fix x::"'a" assume "x \<noteq> a \<and> dist x a < d"
himmelma@33175
  1496
      hence "f x \<in> S" using d apply(erule_tac x="x-a" in allE)
haftmann@35820
  1497
        by(auto simp add: add_commute dist_norm dist_commute)
himmelma@33175
  1498
    }
himmelma@33175
  1499
    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
  1500
    hence "eventually (\<lambda>x. f x \<in> S) (at a)" unfolding Limits.eventually_at .
himmelma@33175
  1501
  }
himmelma@33175
  1502
  thus "?lhs" by (rule topological_tendstoI)
himmelma@33175
  1503
qed
himmelma@33175
  1504
himmelma@33175
  1505
text{* It's also sometimes useful to extract the limit point from the net.  *}
himmelma@33175
  1506
himmelma@33175
  1507
definition
himmelma@33175
  1508
  netlimit :: "'a::t2_space net \<Rightarrow> 'a" where
himmelma@33175
  1509
  "netlimit net = (SOME a. ((\<lambda>x. x) ---> a) net)"
himmelma@33175
  1510
himmelma@33175
  1511
lemma netlimit_within:
himmelma@33175
  1512
  assumes "\<not> trivial_limit (at a within S)"
himmelma@33175
  1513
  shows "netlimit (at a within S) = a"
himmelma@33175
  1514
unfolding netlimit_def
himmelma@33175
  1515
apply (rule some_equality)
himmelma@33175
  1516
apply (rule Lim_at_within)
himmelma@33175
  1517
apply (rule Lim_ident_at)
himmelma@33175
  1518
apply (erule Lim_unique [OF assms])
himmelma@33175
  1519
apply (rule Lim_at_within)
himmelma@33175
  1520
apply (rule Lim_ident_at)
himmelma@33175
  1521
done
himmelma@33175
  1522
himmelma@33175
  1523
lemma netlimit_at:
himmelma@33175
  1524
  fixes a :: "'a::perfect_space"
himmelma@33175
  1525
  shows "netlimit (at a) = a"
himmelma@33175
  1526
  apply (subst within_UNIV[symmetric])
himmelma@33175
  1527
  using netlimit_within[of a UNIV]
himmelma@33175
  1528
  by (simp add: trivial_limit_at within_UNIV)
himmelma@33175
  1529
himmelma@33175
  1530
text{* Transformation of limit. *}
himmelma@33175
  1531
himmelma@33175
  1532
lemma Lim_transform:
himmelma@33175
  1533
  fixes f g :: "'a::type \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1534
  assumes "((\<lambda>x. f x - g x) ---> 0) net" "(f ---> l) net"
himmelma@33175
  1535
  shows "(g ---> l) net"
himmelma@33175
  1536
proof-
himmelma@33175
  1537
  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
  1538
  thus "?thesis" using Lim_neg [of "\<lambda> x. - g x" "-l" net] by auto
himmelma@33175
  1539
qed
himmelma@33175
  1540
himmelma@33175
  1541
lemma Lim_transform_eventually:
himmelma@33175
  1542
  "eventually (\<lambda>x. f x = g x) net \<Longrightarrow> (f ---> l) net ==> (g ---> l) net"
himmelma@33175
  1543
  apply (rule topological_tendstoI)
himmelma@33175
  1544
  apply (drule (2) topological_tendstoD)
himmelma@33175
  1545
  apply (erule (1) eventually_elim2, simp)
himmelma@33175
  1546
  done
himmelma@33175
  1547
himmelma@33175
  1548
lemma Lim_transform_within:
himmelma@33175
  1549
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1550
  assumes "0 < d" "(\<forall>x'\<in>S. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x')"
himmelma@33175
  1551
          "(f ---> l) (at x within S)"
himmelma@33175
  1552
  shows   "(g ---> l) (at x within S)"
himmelma@33175
  1553
  using assms(1,3) unfolding Lim_within
himmelma@33175
  1554
  apply -
himmelma@33175
  1555
  apply (clarify, rename_tac e)
himmelma@33175
  1556
  apply (drule_tac x=e in spec, clarsimp, rename_tac r)
himmelma@33175
  1557
  apply (rule_tac x="min d r" in exI, clarsimp, rename_tac y)
himmelma@33175
  1558
  apply (drule_tac x=y in bspec, assumption, clarsimp)
himmelma@33175
  1559
  apply (simp add: assms(2))
himmelma@33175
  1560
  done
himmelma@33175
  1561
himmelma@33175
  1562
lemma Lim_transform_at:
himmelma@33175
  1563
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1564
  shows "0 < d \<Longrightarrow> (\<forall>x'. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x') \<Longrightarrow>
himmelma@33175
  1565
  (f ---> l) (at x) ==> (g ---> l) (at x)"
himmelma@33175
  1566
  apply (subst within_UNIV[symmetric])
himmelma@33175
  1567
  using Lim_transform_within[of d UNIV x f g l]
himmelma@33175
  1568
  by (auto simp add: within_UNIV)
himmelma@33175
  1569
himmelma@33175
  1570
text{* Common case assuming being away from some crucial point like 0. *}
himmelma@33175
  1571
himmelma@33175
  1572
lemma Lim_transform_away_within:
himmelma@33175
  1573
  fixes a b :: "'a::metric_space"
himmelma@33175
  1574
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1575
  assumes "a\<noteq>b" "\<forall>x\<in> S. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"
himmelma@33175
  1576
  and "(f ---> l) (at a within S)"
himmelma@33175
  1577
  shows "(g ---> l) (at a within S)"
himmelma@33175
  1578
proof-
himmelma@33175
  1579
  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
  1580
    apply auto apply(erule_tac x=x' in ballE) by (auto simp add: dist_commute)
himmelma@33175
  1581
  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
  1582
qed
himmelma@33175
  1583
himmelma@33175
  1584
lemma Lim_transform_away_at:
himmelma@33175
  1585
  fixes a b :: "'a::metric_space"
himmelma@33175
  1586
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1587
  assumes ab: "a\<noteq>b" and fg: "\<forall>x. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"
himmelma@33175
  1588
  and fl: "(f ---> l) (at a)"
himmelma@33175
  1589
  shows "(g ---> l) (at a)"
himmelma@33175
  1590
  using Lim_transform_away_within[OF ab, of UNIV f g l] fg fl
himmelma@33175
  1591
  by (auto simp add: within_UNIV)
himmelma@33175
  1592
himmelma@33175
  1593
text{* Alternatively, within an open set. *}
himmelma@33175
  1594
himmelma@33175
  1595
lemma Lim_transform_within_open:
himmelma@33175
  1596
  fixes a :: "'a::metric_space"
himmelma@33175
  1597
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1598
  assumes "open S"  "a \<in> S"  "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> f x = g x"  "(f ---> l) (at a)"
himmelma@33175
  1599
  shows "(g ---> l) (at a)"
himmelma@33175
  1600
proof-
himmelma@33175
  1601
  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
  1602
  hence "\<forall>x'. 0 < dist x' a \<and> dist x' a < e \<longrightarrow> f x' = g x'" using assms(3)
himmelma@33175
  1603
    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
  1604
  thus ?thesis using Lim_transform_at[of e a f g l] `e>0` assms(4) by auto
himmelma@33175
  1605
qed
himmelma@33175
  1606
himmelma@33175
  1607
text{* A congruence rule allowing us to transform limits assuming not at point. *}
himmelma@33175
  1608
himmelma@33175
  1609
(* FIXME: Only one congruence rule for tendsto can be used at a time! *)
himmelma@33175
  1610
huffman@36362
  1611
lemma Lim_cong_within(*[cong add]*):
himmelma@33175
  1612
  fixes a :: "'a::metric_space"
himmelma@33175
  1613
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1614
  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
  1615
  by (simp add: Lim_within dist_nz[symmetric])
himmelma@33175
  1616
himmelma@33175
  1617
lemma Lim_cong_at[cong add]:
himmelma@33175
  1618
  fixes a :: "'a::metric_space"
himmelma@33175
  1619
  fixes l :: "'b::metric_space" (* TODO: generalize *)
himmelma@33175
  1620
  shows "(\<And>x. x \<noteq> a ==> f x = g x) ==> (((\<lambda>x. f x) ---> l) (at a) \<longleftrightarrow> ((g ---> l) (at a)))"
himmelma@33175
  1621
  by (simp add: Lim_at dist_nz[symmetric])
himmelma@33175
  1622
himmelma@33175
  1623
text{* Useful lemmas on closure and set of possible sequential limits.*}
himmelma@33175
  1624
himmelma@33175
  1625
lemma closure_sequential:
himmelma@33175
  1626
  fixes l :: "'a::metric_space" (* TODO: generalize *)
himmelma@33175
  1627
  shows "l \<in> closure S \<longleftrightarrow> (\<exists>x. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially)" (is "?lhs = ?rhs")
himmelma@33175
  1628
proof
himmelma@33175
  1629
  assume "?lhs" moreover
himmelma@33175
  1630
  { assume "l \<in> S"
himmelma@33175
  1631
    hence "?rhs" using Lim_const[of l sequentially] by auto
himmelma@33175
  1632
  } moreover
himmelma@33175
  1633
  { assume "l islimpt S"
himmelma@33175
  1634
    hence "?rhs" unfolding islimpt_sequential by auto
himmelma@33175
  1635
  } ultimately
himmelma@33175
  1636
  show "?rhs" unfolding closure_def by auto
himmelma@33175
  1637
next
himmelma@33175
  1638
  assume "?rhs"
himmelma@33175
  1639
  thus "?lhs" unfolding closure_def unfolding islimpt_sequential by auto
himmelma@33175
  1640
qed
himmelma@33175
  1641
himmelma@33175
  1642
lemma closed_sequential_limits:
himmelma@33175
  1643
  fixes S :: "'a::metric_space set"
himmelma@33175
  1644
  shows "closed S \<longleftrightarrow> (\<forall>x l. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially \<longrightarrow> l \<in> S)"
himmelma@33175
  1645
  unfolding closed_limpt
himmelma@33175
  1646
  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
  1647
  by metis
himmelma@33175
  1648
himmelma@33175
  1649
lemma closure_approachable:
himmelma@33175
  1650
  fixes S :: "'a::metric_space set"
himmelma@33175
  1651
  shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e)"
himmelma@33175
  1652
  apply (auto simp add: closure_def islimpt_approachable)
himmelma@33175
  1653
  by (metis dist_self)
himmelma@33175
  1654
himmelma@33175
  1655
lemma closed_approachable:
himmelma@33175
  1656
  fixes S :: "'a::metric_space set"
himmelma@33175
  1657
  shows "closed S ==> (\<forall>e>0. \<exists>y\<in>S. dist y x < e) \<longleftrightarrow> x \<in> S"
himmelma@33175
  1658
  by (metis closure_closed closure_approachable)
himmelma@33175
  1659
himmelma@33175
  1660
text{* Some other lemmas about sequences. *}
himmelma@33175
  1661
himmelma@33175
  1662
lemma seq_offset:
himmelma@33175
  1663
  fixes l :: "'a::metric_space" (* TODO: generalize *)
himmelma@33175
  1664
  shows "(f ---> l) sequentially ==> ((\<lambda>i. f( i + k)) ---> l) sequentially"
himmelma@33175
  1665
  apply (auto simp add: Lim_sequentially)
himmelma@33175
  1666
  by (metis trans_le_add1 )
himmelma@33175
  1667
himmelma@33175
  1668
lemma seq_offset_neg:
himmelma@33175
  1669
  "(f ---> l) sequentially ==> ((\<lambda>i. f(i - k)) ---> l) sequentially"
himmelma@33175
  1670
  apply (rule topological_tendstoI)
himmelma@33175
  1671
  apply (drule (2) topological_tendstoD)
himmelma@33175
  1672
  apply (simp only: eventually_sequentially)
himmelma@33175
  1673
  apply (subgoal_tac "\<And>N k (n::nat). N + k <= n ==> N <= n - k")
himmelma@33175
  1674
  apply metis
himmelma@33175
  1675
  by arith
himmelma@33175
  1676
himmelma@33175
  1677
lemma seq_offset_rev:
himmelma@33175
  1678
  "((\<lambda>i. f(i + k)) ---> l) sequentially ==> (f ---> l) sequentially"
himmelma@33175
  1679
  apply (rule topological_tendstoI)
himmelma@33175
  1680
  apply (drule (2) topological_tendstoD)
himmelma@33175
  1681
  apply (simp only: eventually_sequentially)
himmelma@33175
  1682
  apply (subgoal_tac "\<And>N k (n::nat). N + k <= n ==> N <= n - k \<and> (n - k) + k = n")
himmelma@33175
  1683
  by metis arith
himmelma@33175
  1684
himmelma@33175
  1685
lemma seq_harmonic: "((\<lambda>n. inverse (real n)) ---> 0) sequentially"
himmelma@33175
  1686
proof-
himmelma@33175
  1687
  { fix e::real assume "e>0"
himmelma@33175
  1688
    hence "\<exists>N::nat. \<forall>n::nat\<ge>N. inverse (real n) < e"
himmelma@33175
  1689
      using real_arch_inv[of e] apply auto apply(rule_tac x=n in exI)
huffman@36362
  1690
      by (metis le_imp_inverse_le not_less real_of_nat_gt_zero_cancel_iff real_of_nat_less_iff xt1(7))
himmelma@33175
  1691
  }
himmelma@33175
  1692
  thus ?thesis unfolding Lim_sequentially dist_norm by simp
himmelma@33175
  1693
qed
himmelma@33175
  1694
huffman@36437
  1695
subsection {* More properties of closed balls. *}
himmelma@33175
  1696
himmelma@33175
  1697
lemma closed_cball: "closed (cball x e)"
himmelma@33175
  1698
unfolding cball_def closed_def
himmelma@33175
  1699
unfolding Collect_neg_eq [symmetric] not_le
himmelma@33175
  1700
apply (clarsimp simp add: open_dist, rename_tac y)
himmelma@33175
  1701
apply (rule_tac x="dist x y - e" in exI, clarsimp)
himmelma@33175
  1702
apply (rename_tac x')
himmelma@33175
  1703
apply (cut_tac x=x and y=x' and z=y in dist_triangle)
himmelma@33175
  1704
apply simp
himmelma@33175
  1705
done
himmelma@33175
  1706
himmelma@33175
  1707
lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"
himmelma@33175
  1708
proof-
himmelma@33175
  1709
  { fix x and e::real assume "x\<in>S" "e>0" "ball x e \<subseteq> S"
himmelma@33175
  1710
    hence "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)
himmelma@33175
  1711
  } moreover
himmelma@33175
  1712
  { fix x and e::real assume "x\<in>S" "e>0" "cball x e \<subseteq> S"
himmelma@33175
  1713
    hence "\<exists>d>0. ball x d \<subseteq> S" unfolding subset_eq apply(rule_tac x="e/2" in exI) by auto
himmelma@33175
  1714
  } ultimately
himmelma@33175
  1715
  show ?thesis unfolding open_contains_ball by auto
himmelma@33175
  1716
qed
himmelma@33175
  1717
himmelma@33175
  1718
lemma open_contains_cball_eq: "open S ==> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"
himmelma@33175
  1719
  by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball mem_def)
himmelma@33175
  1720
himmelma@33175
  1721
lemma mem_interior_cball: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S)"
himmelma@33175
  1722
  apply (simp add: interior_def, safe)
himmelma@33175
  1723
  apply (force simp add: open_contains_cball)
himmelma@33175
  1724
  apply (rule_tac x="ball x e" in exI)
huffman@36362
  1725
  apply (simp add: subset_trans [OF ball_subset_cball])
himmelma@33175
  1726
  done
himmelma@33175
  1727
himmelma@33175
  1728
lemma islimpt_ball:
himmelma@33175
  1729
  fixes x y :: "'a::{real_normed_vector,perfect_space}"
himmelma@33175
  1730
  shows "y islimpt ball x e \<longleftrightarrow> 0 < e \<and> y \<in> cball x e" (is "?lhs = ?rhs")
himmelma@33175
  1731
proof
himmelma@33175
  1732
  assume "?lhs"
himmelma@33175
  1733
  { assume "e \<le> 0"
himmelma@33175
  1734
    hence *:"ball x e = {}" using ball_eq_empty[of x e] by auto
himmelma@33175
  1735
    have False using `?lhs` unfolding * using islimpt_EMPTY[of y] by auto
himmelma@33175
  1736
  }
himmelma@33175
  1737
  hence "e > 0" by (metis not_less)
himmelma@33175
  1738
  moreover
himmelma@33175
  1739
  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
  1740
  ultimately show "?rhs" by auto
himmelma@33175
  1741
next
himmelma@33175
  1742
  assume "?rhs" hence "e>0"  by auto
himmelma@33175
  1743
  { fix d::real assume "d>0"
himmelma@33175
  1744
    have "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1745
    proof(cases "d \<le> dist x y")
himmelma@33175
  1746
      case True thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1747
      proof(cases "x=y")
himmelma@33175
  1748
        case True hence False using `d \<le> dist x y` `d>0` by auto
himmelma@33175
  1749
        thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d" by auto
himmelma@33175
  1750
      next
himmelma@33175
  1751
        case False
himmelma@33175
  1752
himmelma@33175
  1753
        have "dist x (y - (d / (2 * dist y x)) *\<^sub>R (y - x))
himmelma@33175
  1754
              = norm (x - y + (d / (2 * norm (y - x))) *\<^sub>R (y - x))"
himmelma@33175
  1755
          unfolding mem_cball mem_ball dist_norm diff_diff_eq2 diff_add_eq[THEN sym] by auto
himmelma@33175
  1756
        also have "\<dots> = \<bar>- 1 + d / (2 * norm (x - y))\<bar> * norm (x - y)"
himmelma@33175
  1757
          using scaleR_left_distrib[of "- 1" "d / (2 * norm (y - x))", THEN sym, of "y - x"]
himmelma@33175
  1758
          unfolding scaleR_minus_left scaleR_one
himmelma@33175
  1759
          by (auto simp add: norm_minus_commute)
himmelma@33175
  1760
        also have "\<dots> = \<bar>- norm (x - y) + d / 2\<bar>"
himmelma@33175
  1761
          unfolding abs_mult_pos[of "norm (x - y)", OF norm_ge_zero[of "x - y"]]
himmelma@33175
  1762
          unfolding real_add_mult_distrib using `x\<noteq>y`[unfolded dist_nz, unfolded dist_norm] by auto
himmelma@33175
  1763
        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
  1764
        finally have "y - (d / (2 * dist y x)) *\<^sub>R (y - x) \<in> ball x e" using `d>0` by auto
himmelma@33175
  1765
himmelma@33175
  1766
        moreover
himmelma@33175
  1767
himmelma@33175
  1768
        have "(d / (2*dist y x)) *\<^sub>R (y - x) \<noteq> 0"
himmelma@33175
  1769
          using `x\<noteq>y`[unfolded dist_nz] `d>0` unfolding scaleR_eq_0_iff by (auto simp add: dist_commute)
himmelma@33175
  1770
        moreover
himmelma@33175
  1771
        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
  1772
          using `d>0` `x\<noteq>y`[unfolded dist_nz] dist_commute[of x y]
himmelma@33175
  1773
          unfolding dist_norm by auto
himmelma@33175
  1774
        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
  1775
      qed
himmelma@33175
  1776
    next
himmelma@33175
  1777
      case False hence "d > dist x y" by auto
himmelma@33175
  1778
      show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1779
      proof(cases "x=y")
himmelma@33175
  1780
        case True
himmelma@33175
  1781
        obtain z where **: "z \<noteq> y" "dist z y < min e d"
himmelma@33175
  1782
          using perfect_choose_dist[of "min e d" y]
himmelma@33175
  1783
          using `d > 0` `e>0` by auto
himmelma@33175
  1784
        show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1785
          unfolding `x = y`
himmelma@33175
  1786
          using `z \<noteq> y` **
himmelma@33175
  1787
          by (rule_tac x=z in bexI, auto simp add: dist_commute)
himmelma@33175
  1788
      next
himmelma@33175
  1789
        case False thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1790
          using `d>0` `d > dist x y` `?rhs` by(rule_tac x=x in bexI, auto)
himmelma@33175
  1791
      qed
himmelma@33175
  1792
    qed  }
himmelma@33175
  1793
  thus "?lhs" unfolding mem_cball islimpt_approachable mem_ball by auto
himmelma@33175
  1794
qed
himmelma@33175
  1795
himmelma@33175
  1796
lemma closure_ball_lemma:
himmelma@33175
  1797
  fixes x y :: "'a::real_normed_vector"
himmelma@33175
  1798
  assumes "x \<noteq> y" shows "y islimpt ball x (dist x y)"
himmelma@33175
  1799
proof (rule islimptI)
himmelma@33175
  1800
  fix T assume "y \<in> T" "open T"
himmelma@33175
  1801
  then obtain r where "0 < r" "\<forall>z. dist z y < r \<longrightarrow> z \<in> T"
himmelma@33175
  1802
    unfolding open_dist by fast
himmelma@33175
  1803
  (* choose point between x and y, within distance r of y. *)
himmelma@33175
  1804
  def k \<equiv> "min 1 (r / (2 * dist x y))"
himmelma@33175
  1805
  def z \<equiv> "y + scaleR k (x - y)"
himmelma@33175
  1806
  have z_def2: "z = x + scaleR (1 - k) (y - x)"
himmelma@33175
  1807
    unfolding z_def by (simp add: algebra_simps)
himmelma@33175
  1808
  have "dist z y < r"
himmelma@33175
  1809
    unfolding z_def k_def using `0 < r`
himmelma@33175
  1810
    by (simp add: dist_norm min_def)
himmelma@33175
  1811
  hence "z \<in> T" using `\<forall>z. dist z y < r \<longrightarrow> z \<in> T` by simp
himmelma@33175
  1812
  have "dist x z < dist x y"
himmelma@33175
  1813
    unfolding z_def2 dist_norm
himmelma@33175
  1814
    apply (simp add: norm_minus_commute)
himmelma@33175
  1815
    apply (simp only: dist_norm [symmetric])
himmelma@33175
  1816
    apply (subgoal_tac "\<bar>1 - k\<bar> * dist x y < 1 * dist x y", simp)
himmelma@33175
  1817
    apply (rule mult_strict_right_mono)
himmelma@33175
  1818
    apply (simp add: k_def divide_pos_pos zero_less_dist_iff `0 < r` `x \<noteq> y`)
himmelma@33175
  1819
    apply (simp add: zero_less_dist_iff `x \<noteq> y`)
himmelma@33175
  1820
    done
himmelma@33175
  1821
  hence "z \<in> ball x (dist x y)" by simp
himmelma@33175
  1822
  have "z \<noteq> y"
himmelma@33175
  1823
    unfolding z_def k_def using `x \<noteq> y` `0 < r`
himmelma@33175
  1824
    by (simp add: min_def)
himmelma@33175
  1825
  show "\<exists>z\<in>ball x (dist x y). z \<in> T \<and> z \<noteq> y"
himmelma@33175
  1826
    using `z \<in> ball x (dist x y)` `z \<in> T` `z \<noteq> y`
himmelma@33175
  1827
    by fast
himmelma@33175
  1828
qed
himmelma@33175
  1829
himmelma@33175
  1830
lemma closure_ball:
himmelma@33175
  1831
  fixes x :: "'a::real_normed_vector"
himmelma@33175
  1832
  shows "0 < e \<Longrightarrow> closure (ball x e) = cball x e"
himmelma@33175
  1833
apply (rule equalityI)
himmelma@33175
  1834
apply (rule closure_minimal)
himmelma@33175
  1835
apply (rule ball_subset_cball)
himmelma@33175
  1836
apply (rule closed_cball)
himmelma@33175
  1837
apply (rule subsetI, rename_tac y)
himmelma@33175
  1838
apply (simp add: le_less [where 'a=real])
himmelma@33175
  1839
apply (erule disjE)
himmelma@33175
  1840
apply (rule subsetD [OF closure_subset], simp)
himmelma@33175
  1841
apply (simp add: closure_def)
himmelma@33175
  1842
apply clarify
himmelma@33175
  1843
apply (rule closure_ball_lemma)
himmelma@33175
  1844
apply (simp add: zero_less_dist_iff)
himmelma@33175
  1845
done
himmelma@33175
  1846
himmelma@33175
  1847
(* In a trivial vector space, this fails for e = 0. *)
himmelma@33175
  1848
lemma interior_cball:
himmelma@33175
  1849
  fixes x :: "'a::{real_normed_vector, perfect_space}"
himmelma@33175
  1850
  shows "interior (cball x e) = ball x e"
himmelma@33175
  1851
proof(cases "e\<ge>0")
himmelma@33175
  1852
  case False note cs = this
himmelma@33175
  1853
  from cs have "ball x e = {}" using ball_empty[of e x] by auto moreover
himmelma@33175
  1854
  { fix y assume "y \<in> cball x e"
himmelma@33175
  1855
    hence False unfolding mem_cball using dist_nz[of x y] cs by auto  }
himmelma@33175
  1856
  hence "cball x e = {}" by auto
himmelma@33175
  1857
  hence "interior (cball x e) = {}" using interior_empty by auto
himmelma@33175
  1858
  ultimately show ?thesis by blast
himmelma@33175
  1859
next
himmelma@33175
  1860
  case True note cs = this
himmelma@33175
  1861
  have "ball x e \<subseteq> cball x e" using ball_subset_cball by auto moreover
himmelma@33175
  1862
  { fix S y assume as: "S \<subseteq> cball x e" "open S" "y\<in>S"
himmelma@33175
  1863
    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
  1864
himmelma@33175
  1865
    then obtain xa where xa_y: "xa \<noteq> y" and xa: "dist xa y < d"
himmelma@33175
  1866
      using perfect_choose_dist [of d] by auto
himmelma@33175
  1867
    have "xa\<in>S" using d[THEN spec[where x=xa]] using xa by(auto simp add: dist_commute)
himmelma@33175
  1868
    hence xa_cball:"xa \<in> cball x e" using as(1) by auto
himmelma@33175
  1869
himmelma@33175
  1870
    hence "y \<in> ball x e" proof(cases "x = y")
himmelma@33175
  1871
      case True
himmelma@33175
  1872
      hence "e>0" using xa_y[unfolded dist_nz] xa_cball[unfolded mem_cball] by (auto simp add: dist_commute)
himmelma@33175
  1873
      thus "y \<in> ball x e" using `x = y ` by simp
himmelma@33175
  1874
    next
himmelma@33175
  1875
      case False
himmelma@33175
  1876
      have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) y < d" unfolding dist_norm
himmelma@33175
  1877
        using `d>0` norm_ge_zero[of "y - x"] `x \<noteq> y` by auto
himmelma@33175
  1878
      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
  1879
      have "y - x \<noteq> 0" using `x \<noteq> y` by auto
himmelma@33175
  1880
      hence **:"d / (2 * norm (y - x)) > 0" unfolding zero_less_norm_iff[THEN sym]
himmelma@33175
  1881
        using `d>0` divide_pos_pos[of d "2*norm (y - x)"] by auto
himmelma@33175
  1882
himmelma@33175
  1883
      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
  1884
        by (auto simp add: dist_norm algebra_simps)
himmelma@33175
  1885
      also have "\<dots> = norm ((1 + d / (2 * norm (y - x))) *\<^sub>R (y - x))"
himmelma@33175
  1886
        by (auto simp add: algebra_simps)
himmelma@33175
  1887
      also have "\<dots> = \<bar>1 + d / (2 * norm (y - x))\<bar> * norm (y - x)"
himmelma@33175
  1888
        using ** by auto
himmelma@33175
  1889
      also have "\<dots> = (dist y x) + d/2"using ** by (auto simp add: left_distrib dist_norm)
himmelma@33175
  1890
      finally have "e \<ge> dist x y +d/2" using *[unfolded mem_cball] by (auto simp add: dist_commute)
himmelma@33175
  1891
      thus "y \<in> ball x e" unfolding mem_ball using `d>0` by auto
himmelma@33175
  1892
    qed  }
himmelma@33175
  1893
  hence "\<forall>S \<subseteq> cball x e. open S \<longrightarrow> S \<subseteq> ball x e" by auto
himmelma@33175
  1894
  ultimately show ?thesis using interior_unique[of "ball x e" "cball x e"] using open_ball[of x e] by auto
himmelma@33175
  1895
qed
himmelma@33175
  1896
himmelma@33175
  1897
lemma frontier_ball:
himmelma@33175
  1898
  fixes a :: "'a::real_normed_vector"
himmelma@33175
  1899
  shows "0 < e ==> frontier(ball a e) = {x. dist a x = e}"
huffman@36362
  1900
  apply (simp add: frontier_def closure_ball interior_open order_less_imp_le)
himmelma@33175
  1901
  apply (simp add: expand_set_eq)
himmelma@33175
  1902
  by arith
himmelma@33175
  1903
himmelma@33175
  1904
lemma frontier_cball:
himmelma@33175
  1905
  fixes a :: "'a::{real_normed_vector, perfect_space}"
himmelma@33175
  1906
  shows "frontier(cball a e) = {x. dist a x = e}"
huffman@36362
  1907
  apply (simp add: frontier_def interior_cball closed_cball order_less_imp_le)
himmelma@33175
  1908
  apply (simp add: expand_set_eq)
himmelma@33175
  1909
  by arith
himmelma@33175
  1910
himmelma@33175
  1911
lemma cball_eq_empty: "(cball x e = {}) \<longleftrightarrow> e < 0"
himmelma@33175
  1912
  apply (simp add: expand_set_eq not_le)
himmelma@33175
  1913
  by (metis zero_le_dist dist_self order_less_le_trans)
himmelma@33175
  1914
lemma cball_empty: "e < 0 ==> cball x e = {}" by (simp add: cball_eq_empty)
himmelma@33175
  1915
himmelma@33175
  1916
lemma cball_eq_sing:
himmelma@33175
  1917
  fixes x :: "'a::perfect_space"
himmelma@33175
  1918
  shows "(cball x e = {x}) \<longleftrightarrow> e = 0"
himmelma@33175
  1919
proof (rule linorder_cases)
himmelma@33175
  1920
  assume e: "0 < e"
himmelma@33175
  1921
  obtain a where "a \<noteq> x" "dist a x < e"
himmelma@33175
  1922
    using perfect_choose_dist [OF e] by auto
himmelma@33175
  1923
  hence "a \<noteq> x" "dist x a \<le> e" by (auto simp add: dist_commute)
himmelma@33175
  1924
  with e show ?thesis by (auto simp add: expand_set_eq)
himmelma@33175
  1925
qed auto
himmelma@33175
  1926
himmelma@33175
  1927
lemma cball_sing:
himmelma@33175
  1928
  fixes x :: "'a::metric_space"
himmelma@33175
  1929
  shows "e = 0 ==> cball x e = {x}"
himmelma@33175
  1930
  by (auto simp add: expand_set_eq)
himmelma@33175
  1931
himmelma@33175
  1932
text{* For points in the interior, localization of limits makes no difference.   *}
himmelma@33175
  1933
himmelma@33175
  1934
lemma eventually_within_interior:
himmelma@33175
  1935
  assumes "x \<in> interior S"
himmelma@33175
  1936
  shows "eventually P (at x within S) \<longleftrightarrow> eventually P (at x)" (is "?lhs = ?rhs")
himmelma@33175
  1937
proof-
himmelma@33175
  1938
  from assms obtain T where T: "open T" "x \<in> T" "T \<subseteq> S"
himmelma@33175
  1939
    unfolding interior_def by fast
himmelma@33175
  1940
  { assume "?lhs"
himmelma@33175
  1941
    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
  1942
      unfolding Limits.eventually_within Limits.eventually_at_topological
himmelma@33175
  1943
      by auto
himmelma@33175
  1944
    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
  1945
      by auto
himmelma@33175
  1946
    then have "?rhs"
himmelma@33175
  1947
      unfolding Limits.eventually_at_topological by auto
himmelma@33175
  1948
  } moreover
himmelma@33175
  1949
  { assume "?rhs" hence "?lhs"
himmelma@33175
  1950
      unfolding Limits.eventually_within
himmelma@33175
  1951
      by (auto elim: eventually_elim1)
himmelma@33175
  1952
  } ultimately
himmelma@33175
  1953
  show "?thesis" ..
himmelma@33175
  1954
qed
himmelma@33175
  1955
himmelma@33175
  1956
lemma lim_within_interior:
himmelma@33175
  1957
  "x \<in> interior S \<Longrightarrow> (f ---> l) (at x within S) \<longleftrightarrow> (f ---> l) (at x)"
himmelma@33175
  1958
  unfolding tendsto_def by (simp add: eventually_within_interior)
himmelma@33175
  1959
himmelma@33175
  1960
lemma netlimit_within_interior:
himmelma@33175
  1961
  fixes x :: "'a::{perfect_space, real_normed_vector}"
himmelma@33175
  1962
    (* FIXME: generalize to perfect_space *)
himmelma@33175
  1963
  assumes "x \<in> interior S"
himmelma@33175
  1964
  shows "netlimit(at x within S) = x" (is "?lhs = ?rhs")
himmelma@33175
  1965
proof-
himmelma@33175
  1966
  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
  1967
  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
  1968
  thus ?thesis using netlimit_within by auto
himmelma@33175
  1969
qed
himmelma@33175
  1970
himmelma@33175
  1971
subsection{* Boundedness. *}
himmelma@33175
  1972
himmelma@33175
  1973
  (* FIXME: This has to be unified with BSEQ!! *)
himmelma@33175
  1974
definition
himmelma@33175
  1975
  bounded :: "'a::metric_space set \<Rightarrow> bool" where
himmelma@33175
  1976
  "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"
himmelma@33175
  1977
himmelma@33175
  1978
lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"
himmelma@33175
  1979
unfolding bounded_def
himmelma@33175
  1980
apply safe
himmelma@33175
  1981
apply (rule_tac x="dist a x + e" in exI, clarify)
himmelma@33175
  1982
apply (drule (1) bspec)
himmelma@33175
  1983
apply (erule order_trans [OF dist_triangle add_left_mono])
himmelma@33175
  1984
apply auto
himmelma@33175
  1985
done
himmelma@33175
  1986
himmelma@33175
  1987
lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"
himmelma@33175
  1988
unfolding bounded_any_center [where a=0]
himmelma@33175
  1989
by (simp add: dist_norm)
himmelma@33175
  1990
himmelma@33175
  1991
lemma bounded_empty[simp]: "bounded {}" by (simp add: bounded_def)
himmelma@33175
  1992
lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T ==> bounded S"
himmelma@33175
  1993
  by (metis bounded_def subset_eq)
himmelma@33175
  1994
himmelma@33175
  1995
lemma bounded_interior[intro]: "bounded S ==> bounded(interior S)"
himmelma@33175
  1996
  by (metis bounded_subset interior_subset)
himmelma@33175
  1997
himmelma@33175
  1998
lemma bounded_closure[intro]: assumes "bounded S" shows "bounded(closure S)"
himmelma@33175
  1999
proof-
himmelma@33175
  2000
  from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a" unfolding bounded_def by auto
himmelma@33175
  2001
  { fix y assume "y \<in> closure S"
himmelma@33175
  2002
    then obtain f where f: "\<forall>n. f n \<in> S"  "(f ---> y) sequentially"
himmelma@33175
  2003
      unfolding closure_sequential by auto
himmelma@33175
  2004
    have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp
himmelma@33175
  2005
    hence "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"
himmelma@33175
  2006
      by (rule eventually_mono, simp add: f(1))
himmelma@33175
  2007
    have "dist x y \<le> a"
himmelma@33175
  2008
      apply (rule Lim_dist_ubound [of sequentially f])
himmelma@33175
  2009
      apply (rule trivial_limit_sequentially)
himmelma@33175
  2010
      apply (rule f(2))
himmelma@33175
  2011
      apply fact
himmelma@33175
  2012
      done
himmelma@33175
  2013
  }
himmelma@33175
  2014
  thus ?thesis unfolding bounded_def by auto
himmelma@33175
  2015
qed
himmelma@33175
  2016
himmelma@33175
  2017
lemma bounded_cball[simp,intro]: "bounded (cball x e)"
himmelma@33175
  2018
  apply (simp add: bounded_def)
himmelma@33175
  2019
  apply (rule_tac x=x in exI)
himmelma@33175
  2020
  apply (rule_tac x=e in exI)
himmelma@33175
  2021
  apply auto
himmelma@33175
  2022
  done
himmelma@33175
  2023
himmelma@33175
  2024
lemma bounded_ball[simp,intro]: "bounded(ball x e)"
himmelma@33175
  2025
  by (metis ball_subset_cball bounded_cball bounded_subset)
himmelma@33175
  2026
huffman@36362
  2027
lemma finite_imp_bounded[intro]:
huffman@36362
  2028
  fixes S :: "'a::metric_space set" assumes "finite S" shows "bounded S"
himmelma@33175
  2029
proof-
huffman@36362
  2030
  { fix a and F :: "'a set" assume as:"bounded F"
himmelma@33175
  2031
    then obtain x e where "\<forall>y\<in>F. dist x y \<le> e" unfolding bounded_def by auto
himmelma@33175
  2032
    hence "\<forall>y\<in>(insert a F). dist x y \<le> max e (dist x a)" by auto
himmelma@33175
  2033
    hence "bounded (insert a F)" unfolding bounded_def by (intro exI)
himmelma@33175
  2034
  }
himmelma@33175
  2035
  thus ?thesis using finite_induct[of S bounded]  using bounded_empty assms by auto
himmelma@33175
  2036
qed
himmelma@33175
  2037
himmelma@33175
  2038
lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"
himmelma@33175
  2039
  apply (auto simp add: bounded_def)
himmelma@33175
  2040
  apply (rename_tac x y r s)
himmelma@33175
  2041
  apply (rule_tac x=x in exI)
himmelma@33175
  2042
  apply (rule_tac x="max r (dist x y + s)" in exI)
himmelma@33175
  2043
  apply (rule ballI, rename_tac z, safe)
himmelma@33175
  2044
  apply (drule (1) bspec, simp)
himmelma@33175
  2045
  apply (drule (1) bspec)
himmelma@33175
  2046
  apply (rule min_max.le_supI2)
himmelma@33175
  2047
  apply (erule order_trans [OF dist_triangle add_left_mono])
himmelma@33175
  2048
  done
himmelma@33175
  2049
himmelma@33175
  2050
lemma bounded_Union[intro]: "finite F \<Longrightarrow> (\<forall>S\<in>F. bounded S) \<Longrightarrow> bounded(\<Union>F)"
himmelma@33175
  2051
  by (induct rule: finite_induct[of F], auto)
himmelma@33175
  2052
himmelma@33175
  2053
lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x <= b)"
himmelma@33175
  2054
  apply (simp add: bounded_iff)
himmelma@33175
  2055
  apply (subgoal_tac "\<And>x (y::real). 0 < 1 + abs y \<and> (x <= y \<longrightarrow> x <= 1 + abs y)")
himmelma@33175
  2056
  by metis arith
himmelma@33175
  2057
himmelma@33175
  2058
lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"
himmelma@33175
  2059
  by (metis Int_lower1 Int_lower2 bounded_subset)
himmelma@33175
  2060
himmelma@33175
  2061
lemma bounded_diff[intro]: "bounded S ==> bounded (S - T)"
himmelma@33175
  2062
apply (metis Diff_subset bounded_subset)
himmelma@33175
  2063
done
himmelma@33175
  2064
himmelma@33175
  2065
lemma bounded_insert[intro]:"bounded(insert x S) \<longleftrightarrow> bounded S"
himmelma@33175
  2066
  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
  2067
himmelma@33175
  2068
lemma not_bounded_UNIV[simp, intro]:
himmelma@33175
  2069
  "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"
himmelma@33175
  2070
proof(auto simp add: bounded_pos not_le)
himmelma@33175
  2071
  obtain x :: 'a where "x \<noteq> 0"
himmelma@33175
  2072
    using perfect_choose_dist [OF zero_less_one] by fast
himmelma@33175
  2073
  fix b::real  assume b: "b >0"
himmelma@33175
  2074
  have b1: "b +1 \<ge> 0" using b by simp
himmelma@33175
  2075
  with `x \<noteq> 0` have "b < norm (scaleR (b + 1) (sgn x))"
himmelma@33175
  2076
    by (simp add: norm_sgn)
himmelma@33175
  2077
  then show "\<exists>x::'a. b < norm x" ..
himmelma@33175
  2078
qed
himmelma@33175
  2079
himmelma@33175
  2080
lemma bounded_linear_image:
himmelma@33175
  2081
  assumes "bounded S" "bounded_linear f"
himmelma@33175
  2082
  shows "bounded(f ` S)"
himmelma@33175
  2083
proof-
himmelma@33175
  2084
  from assms(1) obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto
himmelma@33175
  2085
  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
  2086
  { fix x assume "x\<in>S"
himmelma@33175
  2087
    hence "norm x \<le> b" using b by auto
himmelma@33175
  2088
    hence "norm (f x) \<le> B * b" using B(2) apply(erule_tac x=x in allE)
himmelma@33175
  2089
      by (metis B(1) B(2) real_le_trans real_mult_le_cancel_iff2)
himmelma@33175
  2090
  }
himmelma@33175
  2091
  thus ?thesis unfolding bounded_pos apply(rule_tac x="b*B" in exI)
himmelma@33175
  2092
    using b B real_mult_order[of b B] by (auto simp add: real_mult_commute)
himmelma@33175
  2093
qed
himmelma@33175
  2094
himmelma@33175
  2095
lemma bounded_scaling:
himmelma@33175
  2096
  fixes S :: "'a::real_normed_vector set"
himmelma@33175
  2097
  shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x) ` S)"
himmelma@33175
  2098
  apply (rule bounded_linear_image, assumption)
himmelma@33175
  2099
  apply (rule scaleR.bounded_linear_right)
himmelma@33175
  2100
  done
himmelma@33175
  2101
himmelma@33175
  2102
lemma bounded_translation:
himmelma@33175
  2103
  fixes S :: "'a::real_normed_vector set"
himmelma@33175
  2104
  assumes "bounded S" shows "bounded ((\<lambda>x. a + x) ` S)"
himmelma@33175
  2105
proof-
himmelma@33175
  2106
  from assms obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto
himmelma@33175
  2107
  { fix x assume "x\<in>S"
himmelma@33175
  2108
    hence "norm (a + x) \<le> b + norm a" using norm_triangle_ineq[of a x] b by auto
himmelma@33175
  2109
  }
himmelma@33175
  2110
  thus ?thesis unfolding bounded_pos using norm_ge_zero[of a] b(1) using add_strict_increasing[of b 0 "norm a"]
himmelma@33175
  2111
    by (auto intro!: add exI[of _ "b + norm a"])
himmelma@33175
  2112
qed
himmelma@33175
  2113
himmelma@33175
  2114
himmelma@33175
  2115
text{* Some theorems on sups and infs using the notion "bounded". *}
himmelma@33175
  2116
himmelma@33175
  2117
lemma bounded_real:
himmelma@33175
  2118
  fixes S :: "real set"
himmelma@33175
  2119
  shows "bounded S \<longleftrightarrow>  (\<exists>a. \<forall>x\<in>S. abs x <= a)"
himmelma@33175
  2120
  by (simp add: bounded_iff)
himmelma@33175
  2121
paulson@33270
  2122
lemma bounded_has_Sup:
paulson@33270
  2123
  fixes S :: "real set"
paulson@33270
  2124
  assumes "bounded S" "S \<noteq> {}"
paulson@33270
  2125
  shows "\<forall>x\<in>S. x <= Sup S" and "\<forall>b. (\<forall>x\<in>S. x <= b) \<longrightarrow> Sup S <= b"
paulson@33270
  2126
proof
paulson@33270
  2127
  fix x assume "x\<in>S"
paulson@33270
  2128
  thus "x \<le> Sup S"
paulson@33270
  2129
    by (metis SupInf.Sup_upper abs_le_D1 assms(1) bounded_real)
paulson@33270
  2130
next
paulson@33270
  2131
  show "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b" using assms
paulson@33270
  2132
    by (metis SupInf.Sup_least)
paulson@33270
  2133
qed
paulson@33270
  2134
paulson@33270
  2135
lemma Sup_insert:
paulson@33270
  2136
  fixes S :: "real set"
paulson@33270
  2137
  shows "bounded S ==> Sup(insert x S) = (if S = {} then x else max x (Sup S))" 
paulson@33270
  2138
by auto (metis Int_absorb Sup_insert_nonempty assms bounded_has_Sup(1) disjoint_iff_not_equal) 
paulson@33270
  2139
paulson@33270
  2140
lemma Sup_insert_finite:
paulson@33270
  2141
  fixes S :: "real set"
paulson@33270
  2142
  shows "finite S \<Longrightarrow> Sup(insert x S) = (if S = {} then x else max x (Sup S))"
paulson@33270
  2143
  apply (rule Sup_insert)
paulson@33270
  2144
  apply (rule finite_imp_bounded)
paulson@33270
  2145
  by simp
paulson@33270
  2146
paulson@33270
  2147
lemma bounded_has_Inf:
paulson@33270
  2148
  fixes S :: "real set"
paulson@33270
  2149
  assumes "bounded S"  "S \<noteq> {}"
paulson@33270
  2150
  shows "\<forall>x\<in>S. x >= Inf S" and "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S >= b"
himmelma@33175
  2151
proof
himmelma@33175
  2152
  fix x assume "x\<in>S"
himmelma@33175
  2153
  from assms(1) obtain a where a:"\<forall>x\<in>S. \<bar>x\<bar> \<le> a" unfolding bounded_real by auto
paulson@33270
  2154
  thus "x \<ge> Inf S" using `x\<in>S`
paulson@33270
  2155
    by (metis Inf_lower_EX abs_le_D2 minus_le_iff)
himmelma@33175
  2156
next
paulson@33270
  2157
  show "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S \<ge> b" using assms
paulson@33270
  2158
    by (metis SupInf.Inf_greatest)
paulson@33270
  2159
qed
paulson@33270
  2160
paulson@33270
  2161
lemma Inf_insert:
paulson@33270
  2162
  fixes S :: "real set"
paulson@33270
  2163
  shows "bounded S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))" 
paulson@33270
  2164
by auto (metis Int_absorb Inf_insert_nonempty bounded_has_Inf(1) disjoint_iff_not_equal) 
paulson@33270
  2165
lemma Inf_insert_finite:
paulson@33270
  2166
  fixes S :: "real set"
paulson@33270
  2167
  shows "finite S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))"
paulson@33270
  2168
  by (rule Inf_insert, rule finite_imp_bounded, simp)
paulson@33270
  2169
himmelma@33175
  2170
himmelma@33175
  2171
(* TODO: Move this to RComplete.thy -- would need to include Glb into RComplete *)
himmelma@33175
  2172
lemma real_isGlb_unique: "[| isGlb R S x; isGlb R S y |] ==> x = (y::real)"
himmelma@33175
  2173
  apply (frule isGlb_isLb)
himmelma@33175
  2174
  apply (frule_tac x = y in isGlb_isLb)
himmelma@33175
  2175
  apply (blast intro!: order_antisym dest!: isGlb_le_isLb)
himmelma@33175
  2176
  done
himmelma@33175
  2177
huffman@36437
  2178
subsection {* Equivalent versions of compactness *}
huffman@36437
  2179
huffman@36437
  2180
subsubsection{* Sequential compactness *}
himmelma@33175
  2181
himmelma@33175
  2182
definition
himmelma@33175
  2183
  compact :: "'a::metric_space set \<Rightarrow> bool" where (* TODO: generalize *)
himmelma@33175
  2184
  "compact S \<longleftrightarrow>
himmelma@33175
  2185
   (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>
himmelma@33175
  2186
       (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially))"
himmelma@33175
  2187
himmelma@33175
  2188
text {*
himmelma@33175
  2189
  A metric space (or topological vector space) is said to have the
himmelma@33175
  2190
  Heine-Borel property if every closed and bounded subset is compact.
himmelma@33175
  2191
*}
himmelma@33175
  2192
himmelma@33175
  2193
class heine_borel =
himmelma@33175
  2194
  assumes bounded_imp_convergent_subsequence:
himmelma@33175
  2195
    "bounded s \<Longrightarrow> \<forall>n. f n \<in> s
himmelma@33175
  2196
      \<Longrightarrow> \<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2197
himmelma@33175
  2198
lemma bounded_closed_imp_compact:
himmelma@33175
  2199
  fixes s::"'a::heine_borel set"
himmelma@33175
  2200
  assumes "bounded s" and "closed s" shows "compact s"
himmelma@33175
  2201
proof (unfold compact_def, clarify)
himmelma@33175
  2202
  fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
himmelma@33175
  2203
  obtain l r where r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2204
    using bounded_imp_convergent_subsequence [OF `bounded s` `\<forall>n. f n \<in> s`] by auto
himmelma@33175
  2205
  from f have fr: "\<forall>n. (f \<circ> r) n \<in> s" by simp
himmelma@33175
  2206
  have "l \<in> s" using `closed s` fr l
himmelma@33175
  2207
    unfolding closed_sequential_limits by blast
himmelma@33175
  2208
  show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2209
    using `l \<in> s` r l by blast
himmelma@33175
  2210
qed
himmelma@33175
  2211
himmelma@33175
  2212
lemma subseq_bigger: assumes "subseq r" shows "n \<le> r n"
himmelma@33175
  2213
proof(induct n)
himmelma@33175
  2214
  show "0 \<le> r 0" by auto
himmelma@33175
  2215
next
himmelma@33175
  2216
  fix n assume "n \<le> r n"
himmelma@33175
  2217
  moreover have "r n < r (Suc n)"
himmelma@33175
  2218
    using assms [unfolded subseq_def] by auto
himmelma@33175
  2219
  ultimately show "Suc n \<le> r (Suc n)" by auto
himmelma@33175
  2220
qed
himmelma@33175
  2221
himmelma@33175
  2222
lemma eventually_subseq:
himmelma@33175
  2223
  assumes r: "subseq r"
himmelma@33175
  2224
  shows "eventually P sequentially \<Longrightarrow> eventually (\<lambda>n. P (r n)) sequentially"
himmelma@33175
  2225
unfolding eventually_sequentially
himmelma@33175
  2226
by (metis subseq_bigger [OF r] le_trans)
himmelma@33175
  2227
himmelma@33175
  2228
lemma lim_subseq:
himmelma@33175
  2229
  "subseq r \<Longrightarrow> (s ---> l) sequentially \<Longrightarrow> ((s o r) ---> l) sequentially"
himmelma@33175
  2230
unfolding tendsto_def eventually_sequentially o_def
himmelma@33175
  2231
by (metis subseq_bigger le_trans)
himmelma@33175
  2232
himmelma@33175
  2233
lemma num_Axiom: "EX! g. g 0 = e \<and> (\<forall>n. g (Suc n) = f n (g n))"
himmelma@33175
  2234
  unfolding Ex1_def
himmelma@33175
  2235
  apply (rule_tac x="nat_rec e f" in exI)
himmelma@33175
  2236
  apply (rule conjI)+
himmelma@33175
  2237
apply (rule def_nat_rec_0, simp)
himmelma@33175
  2238
apply (rule allI, rule def_nat_rec_Suc, simp)
himmelma@33175
  2239
apply (rule allI, rule impI, rule ext)
himmelma@33175
  2240
apply (erule conjE)
himmelma@33175
  2241
apply (induct_tac x)
huffman@36362
  2242
apply simp
himmelma@33175
  2243
apply (erule_tac x="n" in allE)
himmelma@33175
  2244
apply (simp)
himmelma@33175
  2245
done
himmelma@33175
  2246
himmelma@33175
  2247
lemma convergent_bounded_increasing: fixes s ::"nat\<Rightarrow>real"
himmelma@33175
  2248
  assumes "incseq s" and "\<forall>n. abs(s n) \<le> b"
himmelma@33175
  2249
  shows "\<exists> l. \<forall>e::real>0. \<exists> N. \<forall>n \<ge> N.  abs(s n - l) < e"
himmelma@33175
  2250
proof-
himmelma@33175
  2251
  have "isUb UNIV (range s) b" using assms(2) and abs_le_D1 unfolding isUb_def and setle_def by auto
himmelma@33175
  2252
  then obtain t where t:"isLub UNIV (range s) t" using reals_complete[of "range s" ] by auto
himmelma@33175
  2253
  { fix e::real assume "e>0" and as:"\<forall>N. \<exists>n\<ge>N. \<not> \<bar>s n - t\<bar> < e"
himmelma@33175
  2254
    { fix n::nat
himmelma@33175
  2255
      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
  2256
      have "t \<ge> s N" using isLub_isUb[OF t, unfolded isUb_def setle_def] by auto
himmelma@33175
  2257
      with n have "s N \<le> t - e" using `e>0` by auto
himmelma@33175
  2258
      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
  2259
    hence "isUb UNIV (range s) (t - e)" unfolding isUb_def and setle_def by auto
himmelma@33175
  2260
    hence False using isLub_le_isUb[OF t, of "t - e"] and `e>0` by auto  }
himmelma@33175
  2261
  thus ?thesis by blast
himmelma@33175
  2262
qed
himmelma@33175
  2263
himmelma@33175
  2264
lemma convergent_bounded_monotone: fixes s::"nat \<Rightarrow> real"
himmelma@33175
  2265
  assumes "\<forall>n. abs(s n) \<le> b" and "monoseq s"
himmelma@33175
  2266
  shows "\<exists>l. \<forall>e::real>0. \<exists>N. \<forall>n\<ge>N. abs(s n - l) < e"
himmelma@33175
  2267
  using convergent_bounded_increasing[of s b] assms using convergent_bounded_increasing[of "\<lambda>n. - s n" b]
himmelma@33175
  2268
  unfolding monoseq_def incseq_def
himmelma@33175
  2269
  apply auto unfolding minus_add_distrib[THEN sym, unfolded diff_minus[THEN sym]]
himmelma@33175
  2270
  unfolding abs_minus_cancel by(rule_tac x="-l" in exI)auto
himmelma@33175
  2271
himmelma@33175
  2272
lemma compact_real_lemma:
himmelma@33175
  2273
  assumes "\<forall>n::nat. abs(s n) \<le> b"
himmelma@33175
  2274
  shows "\<exists>(l::real) r. subseq r \<and> ((s \<circ> r) ---> l) sequentially"
himmelma@33175
  2275
proof-
himmelma@33175
  2276
  obtain r where r:"subseq r" "monoseq (\<lambda>n. s (r n))"
himmelma@33175
  2277
    using seq_monosub[of s] by auto
himmelma@33175
  2278
  thus ?thesis using convergent_bounded_monotone[of "\<lambda>n. s (r n)" b] and assms
himmelma@33175
  2279
    unfolding tendsto_iff dist_norm eventually_sequentially by auto
himmelma@33175
  2280
qed
himmelma@33175
  2281
himmelma@33175
  2282
instance real :: heine_borel
himmelma@33175
  2283
proof
himmelma@33175
  2284
  fix s :: "real set" and f :: "nat \<Rightarrow> real"
himmelma@33175
  2285
  assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
himmelma@33175
  2286
  then obtain b where b: "\<forall>n. abs (f n) \<le> b"
himmelma@33175
  2287
    unfolding bounded_iff by auto
himmelma@33175
  2288
  obtain l :: real and r :: "nat \<Rightarrow> nat" where
himmelma@33175
  2289
    r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"