src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
author huffman
Wed, 24 Aug 2011 16:08:21 -0700
changeset 45373 219a6fe4cfae
parent 45372 68e8eb0ce8aa
child 45374 ea85d78a994e
permissions -rw-r--r--
add lemma closure_union;
simplify some proofs;
hoelzl@33711
     1
(*  title:      HOL/Library/Topology_Euclidian_Space.thy
himmelma@33175
     2
    Author:     Amine Chaieb, University of Cambridge
himmelma@33175
     3
    Author:     Robert Himmelmann, TU Muenchen
huffman@44946
     4
    Author:     Brian Huffman, Portland State University
himmelma@33175
     5
*)
himmelma@33175
     6
himmelma@33175
     7
header {* Elementary topology in Euclidean space. *}
himmelma@33175
     8
himmelma@33175
     9
theory Topology_Euclidean_Space
huffman@45372
    10
imports SEQ Linear_Algebra "~~/src/HOL/Library/Glbs" Norm_Arith L2_Norm
himmelma@33175
    11
begin
himmelma@33175
    12
hoelzl@37489
    13
(* to be moved elsewhere *)
hoelzl@37489
    14
hoelzl@37489
    15
lemma euclidean_dist_l2:"dist x (y::'a::euclidean_space) = setL2 (\<lambda>i. dist(x$$i) (y$$i)) {..<DIM('a)}"
hoelzl@37489
    16
  unfolding dist_norm norm_eq_sqrt_inner setL2_def apply(subst euclidean_inner)
huffman@45314
    17
  by(auto simp add:power2_eq_square)
hoelzl@37489
    18
hoelzl@37489
    19
lemma dist_nth_le: "dist (x $$ i) (y $$ i) \<le> dist x (y::'a::euclidean_space)"
hoelzl@37489
    20
  apply(subst(2) euclidean_dist_l2) apply(cases "i<DIM('a)")
hoelzl@37489
    21
  apply(rule member_le_setL2) by auto
hoelzl@37489
    22
huffman@45372
    23
subsection {* General notion of a topology as a value *}
himmelma@33175
    24
huffman@45035
    25
definition "istopology L \<longleftrightarrow> L {} \<and> (\<forall>S T. L S \<longrightarrow> L T \<longrightarrow> L (S \<inter> T)) \<and> (\<forall>K. Ball K L \<longrightarrow> L (\<Union> K))"
huffman@45035
    26
typedef (open) 'a topology = "{L::('a set) \<Rightarrow> bool. istopology L}"
himmelma@33175
    27
  morphisms "openin" "topology"
himmelma@33175
    28
  unfolding istopology_def by blast
himmelma@33175
    29
himmelma@33175
    30
lemma istopology_open_in[intro]: "istopology(openin U)"
himmelma@33175
    31
  using openin[of U] by blast
himmelma@33175
    32
himmelma@33175
    33
lemma topology_inverse': "istopology U \<Longrightarrow> openin (topology U) = U"
huffman@45035
    34
  using topology_inverse[unfolded mem_Collect_eq] .
himmelma@33175
    35
himmelma@33175
    36
lemma topology_inverse_iff: "istopology U \<longleftrightarrow> openin (topology U) = U"
himmelma@33175
    37
  using topology_inverse[of U] istopology_open_in[of "topology U"] by auto
himmelma@33175
    38
himmelma@33175
    39
lemma topology_eq: "T1 = T2 \<longleftrightarrow> (\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S)"
himmelma@33175
    40
proof-
himmelma@33175
    41
  {assume "T1=T2" hence "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S" by simp}
himmelma@33175
    42
  moreover
himmelma@33175
    43
  {assume H: "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S"
huffman@45035
    44
    hence "openin T1 = openin T2" by (simp add: fun_eq_iff)
himmelma@33175
    45
    hence "topology (openin T1) = topology (openin T2)" by simp
himmelma@33175
    46
    hence "T1 = T2" unfolding openin_inverse .}
himmelma@33175
    47
  ultimately show ?thesis by blast
himmelma@33175
    48
qed
himmelma@33175
    49
himmelma@33175
    50
text{* Infer the "universe" from union of all sets in the topology. *}
himmelma@33175
    51
himmelma@33175
    52
definition "topspace T =  \<Union>{S. openin T S}"
himmelma@33175
    53
huffman@45081
    54
subsubsection {* Main properties of open sets *}
himmelma@33175
    55
himmelma@33175
    56
lemma openin_clauses:
himmelma@33175
    57
  fixes U :: "'a topology"
himmelma@33175
    58
  shows "openin U {}"
himmelma@33175
    59
  "\<And>S T. openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S\<inter>T)"
himmelma@33175
    60
  "\<And>K. (\<forall>S \<in> K. openin U S) \<Longrightarrow> openin U (\<Union>K)"
huffman@45035
    61
  using openin[of U] unfolding istopology_def mem_Collect_eq
huffman@45035
    62
  by fast+
himmelma@33175
    63
himmelma@33175
    64
lemma openin_subset[intro]: "openin U S \<Longrightarrow> S \<subseteq> topspace U"
himmelma@33175
    65
  unfolding topspace_def by blast
himmelma@33175
    66
lemma openin_empty[simp]: "openin U {}" by (simp add: openin_clauses)
himmelma@33175
    67
himmelma@33175
    68
lemma openin_Int[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<inter> T)"
huffman@36358
    69
  using openin_clauses by simp
huffman@36358
    70
huffman@36358
    71
lemma openin_Union[intro]: "(\<forall>S \<in>K. openin U S) \<Longrightarrow> openin U (\<Union> K)"
huffman@36358
    72
  using openin_clauses by simp
himmelma@33175
    73
himmelma@33175
    74
lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"
himmelma@33175
    75
  using openin_Union[of "{S,T}" U] by auto
himmelma@33175
    76
himmelma@33175
    77
lemma openin_topspace[intro, simp]: "openin U (topspace U)" by (simp add: openin_Union topspace_def)
himmelma@33175
    78
himmelma@33175
    79
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")
huffman@36584
    80
proof
huffman@36584
    81
  assume ?lhs then show ?rhs by auto
huffman@36584
    82
next
huffman@36584
    83
  assume H: ?rhs
huffman@36584
    84
  let ?t = "\<Union>{T. openin U T \<and> T \<subseteq> S}"
huffman@36584
    85
  have "openin U ?t" by (simp add: openin_Union)
huffman@36584
    86
  also have "?t = S" using H by auto
huffman@36584
    87
  finally show "openin U S" .
himmelma@33175
    88
qed
himmelma@33175
    89
huffman@45081
    90
subsubsection {* Closed sets *}
himmelma@33175
    91
himmelma@33175
    92
definition "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"
himmelma@33175
    93
himmelma@33175
    94
lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U" by (metis closedin_def)
himmelma@33175
    95
lemma closedin_empty[simp]: "closedin U {}" by (simp add: closedin_def)
himmelma@33175
    96
lemma closedin_topspace[intro,simp]:
himmelma@33175
    97
  "closedin U (topspace U)" by (simp add: closedin_def)
himmelma@33175
    98
lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"
himmelma@33175
    99
  by (auto simp add: Diff_Un closedin_def)
himmelma@33175
   100
himmelma@33175
   101
lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union> {A - s|s. s\<in>S}" by auto
himmelma@33175
   102
lemma closedin_Inter[intro]: assumes Ke: "K \<noteq> {}" and Kc: "\<forall>S \<in>K. closedin U S"
himmelma@33175
   103
  shows "closedin U (\<Inter> K)"  using Ke Kc unfolding closedin_def Diff_Inter by auto
himmelma@33175
   104
himmelma@33175
   105
lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"
himmelma@33175
   106
  using closedin_Inter[of "{S,T}" U] by auto
himmelma@33175
   107
himmelma@33175
   108
lemma Diff_Diff_Int: "A - (A - B) = A \<inter> B" by blast
himmelma@33175
   109
lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"
himmelma@33175
   110
  apply (auto simp add: closedin_def Diff_Diff_Int inf_absorb2)
himmelma@33175
   111
  apply (metis openin_subset subset_eq)
himmelma@33175
   112
  done
himmelma@33175
   113
himmelma@33175
   114
lemma openin_closedin:  "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"
himmelma@33175
   115
  by (simp add: openin_closedin_eq)
himmelma@33175
   116
himmelma@33175
   117
lemma openin_diff[intro]: assumes oS: "openin U S" and cT: "closedin U T" shows "openin U (S - T)"
himmelma@33175
   118
proof-
himmelma@33175
   119
  have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT
himmelma@33175
   120
    by (auto simp add: topspace_def openin_subset)
himmelma@33175
   121
  then show ?thesis using oS cT by (auto simp add: closedin_def)
himmelma@33175
   122
qed
himmelma@33175
   123
himmelma@33175
   124
lemma closedin_diff[intro]: assumes oS: "closedin U S" and cT: "openin U T" shows "closedin U (S - T)"
himmelma@33175
   125
proof-
himmelma@33175
   126
  have "S - T = S \<inter> (topspace U - T)" using closedin_subset[of U S]  oS cT
himmelma@33175
   127
    by (auto simp add: topspace_def )
himmelma@33175
   128
  then show ?thesis using oS cT by (auto simp add: openin_closedin_eq)
himmelma@33175
   129
qed
himmelma@33175
   130
huffman@45081
   131
subsubsection {* Subspace topology *}
huffman@45035
   132
huffman@45035
   133
definition "subtopology U V = topology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
huffman@45035
   134
huffman@45035
   135
lemma istopology_subtopology: "istopology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
huffman@45035
   136
  (is "istopology ?L")
himmelma@33175
   137
proof-
huffman@45035
   138
  have "?L {}" by blast
huffman@45035
   139
  {fix A B assume A: "?L A" and B: "?L B"
himmelma@33175
   140
    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
   141
    have "A\<inter>B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"  using Sa Sb by blast+
huffman@45035
   142
    then have "?L (A \<inter> B)" by blast}
himmelma@33175
   143
  moreover
huffman@45035
   144
  {fix K assume K: "K \<subseteq> Collect ?L"
huffman@45035
   145
    have th0: "Collect ?L = (\<lambda>S. S \<inter> V) ` Collect (openin U)"
nipkow@39535
   146
      apply (rule set_eqI)
himmelma@33175
   147
      apply (simp add: Ball_def image_iff)
huffman@45035
   148
      by metis
himmelma@33175
   149
    from K[unfolded th0 subset_image_iff]
huffman@45035
   150
    obtain Sk where Sk: "Sk \<subseteq> Collect (openin U)" "K = (\<lambda>S. S \<inter> V) ` Sk" by blast
himmelma@33175
   151
    have "\<Union>K = (\<Union>Sk) \<inter> V" using Sk by auto
huffman@45035
   152
    moreover have "openin U (\<Union> Sk)" using Sk by (auto simp add: subset_eq)
huffman@45035
   153
    ultimately have "?L (\<Union>K)" by blast}
huffman@45035
   154
  ultimately show ?thesis
huffman@45035
   155
    unfolding subset_eq mem_Collect_eq istopology_def by blast
himmelma@33175
   156
qed
himmelma@33175
   157
himmelma@33175
   158
lemma openin_subtopology:
himmelma@33175
   159
  "openin (subtopology U V) S \<longleftrightarrow> (\<exists> T. (openin U T) \<and> (S = T \<inter> V))"
himmelma@33175
   160
  unfolding subtopology_def topology_inverse'[OF istopology_subtopology]
huffman@45035
   161
  by auto
himmelma@33175
   162
himmelma@33175
   163
lemma topspace_subtopology: "topspace(subtopology U V) = topspace U \<inter> V"
himmelma@33175
   164
  by (auto simp add: topspace_def openin_subtopology)
himmelma@33175
   165
himmelma@33175
   166
lemma closedin_subtopology:
himmelma@33175
   167
  "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"
himmelma@33175
   168
  unfolding closedin_def topspace_subtopology
himmelma@33175
   169
  apply (simp add: openin_subtopology)
himmelma@33175
   170
  apply (rule iffI)
himmelma@33175
   171
  apply clarify
himmelma@33175
   172
  apply (rule_tac x="topspace U - T" in exI)
himmelma@33175
   173
  by auto
himmelma@33175
   174
himmelma@33175
   175
lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"
himmelma@33175
   176
  unfolding openin_subtopology
himmelma@33175
   177
  apply (rule iffI, clarify)
himmelma@33175
   178
  apply (frule openin_subset[of U])  apply blast
himmelma@33175
   179
  apply (rule exI[where x="topspace U"])
himmelma@33175
   180
  by auto
himmelma@33175
   181
himmelma@33175
   182
lemma subtopology_superset: assumes UV: "topspace U \<subseteq> V"
himmelma@33175
   183
  shows "subtopology U V = U"
himmelma@33175
   184
proof-
himmelma@33175
   185
  {fix S
himmelma@33175
   186
    {fix T assume T: "openin U T" "S = T \<inter> V"
himmelma@33175
   187
      from T openin_subset[OF T(1)] UV have eq: "S = T" by blast
himmelma@33175
   188
      have "openin U S" unfolding eq using T by blast}
himmelma@33175
   189
    moreover
himmelma@33175
   190
    {assume S: "openin U S"
himmelma@33175
   191
      hence "\<exists>T. openin U T \<and> S = T \<inter> V"
himmelma@33175
   192
        using openin_subset[OF S] UV by auto}
himmelma@33175
   193
    ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S" by blast}
himmelma@33175
   194
  then show ?thesis unfolding topology_eq openin_subtopology by blast
himmelma@33175
   195
qed
himmelma@33175
   196
himmelma@33175
   197
lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"
himmelma@33175
   198
  by (simp add: subtopology_superset)
himmelma@33175
   199
himmelma@33175
   200
lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"
himmelma@33175
   201
  by (simp add: subtopology_superset)
himmelma@33175
   202
huffman@45081
   203
subsubsection {* The standard Euclidean topology *}
himmelma@33175
   204
himmelma@33175
   205
definition
himmelma@33175
   206
  euclidean :: "'a::topological_space topology" where
himmelma@33175
   207
  "euclidean = topology open"
himmelma@33175
   208
himmelma@33175
   209
lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"
himmelma@33175
   210
  unfolding euclidean_def
himmelma@33175
   211
  apply (rule cong[where x=S and y=S])
himmelma@33175
   212
  apply (rule topology_inverse[symmetric])
himmelma@33175
   213
  apply (auto simp add: istopology_def)
huffman@45035
   214
  done
himmelma@33175
   215
himmelma@33175
   216
lemma topspace_euclidean: "topspace euclidean = UNIV"
himmelma@33175
   217
  apply (simp add: topspace_def)
nipkow@39535
   218
  apply (rule set_eqI)
himmelma@33175
   219
  by (auto simp add: open_openin[symmetric])
himmelma@33175
   220
himmelma@33175
   221
lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"
himmelma@33175
   222
  by (simp add: topspace_euclidean topspace_subtopology)
himmelma@33175
   223
himmelma@33175
   224
lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"
himmelma@33175
   225
  by (simp add: closed_def closedin_def topspace_euclidean open_openin Compl_eq_Diff_UNIV)
himmelma@33175
   226
himmelma@33175
   227
lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"
himmelma@33175
   228
  by (simp add: open_openin openin_subopen[symmetric])
himmelma@33175
   229
huffman@45081
   230
text {* Basic "localization" results are handy for connectedness. *}
huffman@45081
   231
huffman@45081
   232
lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"
huffman@45081
   233
  by (auto simp add: openin_subtopology open_openin[symmetric])
huffman@45081
   234
huffman@45081
   235
lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"
huffman@45081
   236
  by (auto simp add: openin_open)
huffman@45081
   237
huffman@45081
   238
lemma open_openin_trans[trans]:
huffman@45081
   239
 "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"
huffman@45081
   240
  by (metis Int_absorb1  openin_open_Int)
huffman@45081
   241
huffman@45081
   242
lemma open_subset:  "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"
huffman@45081
   243
  by (auto simp add: openin_open)
huffman@45081
   244
huffman@45081
   245
lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"
huffman@45081
   246
  by (simp add: closedin_subtopology closed_closedin Int_ac)
huffman@45081
   247
huffman@45081
   248
lemma closedin_closed_Int: "closed S ==> closedin (subtopology euclidean U) (U \<inter> S)"
huffman@45081
   249
  by (metis closedin_closed)
huffman@45081
   250
huffman@45081
   251
lemma closed_closedin_trans: "closed S \<Longrightarrow> closed T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> closedin (subtopology euclidean S) T"
huffman@45081
   252
  apply (subgoal_tac "S \<inter> T = T" )
huffman@45081
   253
  apply auto
huffman@45081
   254
  apply (frule closedin_closed_Int[of T S])
huffman@45081
   255
  by simp
huffman@45081
   256
huffman@45081
   257
lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"
huffman@45081
   258
  by (auto simp add: closedin_closed)
huffman@45081
   259
huffman@45081
   260
lemma openin_euclidean_subtopology_iff:
huffman@45081
   261
  fixes S U :: "'a::metric_space set"
huffman@45081
   262
  shows "openin (subtopology euclidean U) S
huffman@45081
   263
  \<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")
huffman@45081
   264
proof
huffman@45081
   265
  assume ?lhs thus ?rhs unfolding openin_open open_dist by blast
huffman@45081
   266
next
huffman@45081
   267
  def T \<equiv> "{x. \<exists>a\<in>S. \<exists>d>0. (\<forall>y\<in>U. dist y a < d \<longrightarrow> y \<in> S) \<and> dist x a < d}"
huffman@45081
   268
  have 1: "\<forall>x\<in>T. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> T"
huffman@45081
   269
    unfolding T_def
huffman@45081
   270
    apply clarsimp
huffman@45081
   271
    apply (rule_tac x="d - dist x a" in exI)
huffman@45081
   272
    apply (clarsimp simp add: less_diff_eq)
huffman@45081
   273
    apply (erule rev_bexI)
huffman@45081
   274
    apply (rule_tac x=d in exI, clarify)
huffman@45081
   275
    apply (erule le_less_trans [OF dist_triangle])
huffman@45081
   276
    done
huffman@45081
   277
  assume ?rhs hence 2: "S = U \<inter> T"
huffman@45081
   278
    unfolding T_def
huffman@45081
   279
    apply auto
huffman@45081
   280
    apply (drule (1) bspec, erule rev_bexI)
huffman@45081
   281
    apply auto
huffman@45081
   282
    done
huffman@45081
   283
  from 1 2 show ?lhs
huffman@45081
   284
    unfolding openin_open open_dist by fast
huffman@45081
   285
qed
huffman@45081
   286
huffman@45081
   287
text {* These "transitivity" results are handy too *}
huffman@45081
   288
huffman@45081
   289
lemma openin_trans[trans]: "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T
huffman@45081
   290
  \<Longrightarrow> openin (subtopology euclidean U) S"
huffman@45081
   291
  unfolding open_openin openin_open by blast
huffman@45081
   292
huffman@45081
   293
lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"
huffman@45081
   294
  by (auto simp add: openin_open intro: openin_trans)
huffman@45081
   295
huffman@45081
   296
lemma closedin_trans[trans]:
huffman@45081
   297
 "closedin (subtopology euclidean T) S \<Longrightarrow>
huffman@45081
   298
           closedin (subtopology euclidean U) T
huffman@45081
   299
           ==> closedin (subtopology euclidean U) S"
huffman@45081
   300
  by (auto simp add: closedin_closed closed_closedin closed_Inter Int_assoc)
huffman@45081
   301
huffman@45081
   302
lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"
huffman@45081
   303
  by (auto simp add: closedin_closed intro: closedin_trans)
huffman@45081
   304
huffman@45081
   305
huffman@45081
   306
subsection {* Open and closed balls *}
himmelma@33175
   307
himmelma@33175
   308
definition
himmelma@33175
   309
  ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where
himmelma@33175
   310
  "ball x e = {y. dist x y < e}"
himmelma@33175
   311
himmelma@33175
   312
definition
himmelma@33175
   313
  cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where
himmelma@33175
   314
  "cball x e = {y. dist x y \<le> e}"
himmelma@33175
   315
himmelma@33175
   316
lemma mem_ball[simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e" by (simp add: ball_def)
himmelma@33175
   317
lemma mem_cball[simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e" by (simp add: cball_def)
himmelma@33175
   318
himmelma@33175
   319
lemma mem_ball_0 [simp]:
himmelma@33175
   320
  fixes x :: "'a::real_normed_vector"
himmelma@33175
   321
  shows "x \<in> ball 0 e \<longleftrightarrow> norm x < e"
himmelma@33175
   322
  by (simp add: dist_norm)
himmelma@33175
   323
himmelma@33175
   324
lemma mem_cball_0 [simp]:
himmelma@33175
   325
  fixes x :: "'a::real_normed_vector"
himmelma@33175
   326
  shows "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"
himmelma@33175
   327
  by (simp add: dist_norm)
himmelma@33175
   328
himmelma@33175
   329
lemma centre_in_cball[simp]: "x \<in> cball x e \<longleftrightarrow> 0\<le> e"  by simp
himmelma@33175
   330
lemma ball_subset_cball[simp,intro]: "ball x e \<subseteq> cball x e" by (simp add: subset_eq)
himmelma@33175
   331
lemma subset_ball[intro]: "d <= e ==> ball x d \<subseteq> ball x e" by (simp add: subset_eq)
himmelma@33175
   332
lemma subset_cball[intro]: "d <= e ==> cball x d \<subseteq> cball x e" by (simp add: subset_eq)
himmelma@33175
   333
lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"
nipkow@39535
   334
  by (simp add: set_eq_iff) arith
himmelma@33175
   335
himmelma@33175
   336
lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"
nipkow@39535
   337
  by (simp add: set_eq_iff)
himmelma@33175
   338
himmelma@33175
   339
lemma diff_less_iff: "(a::real) - b > 0 \<longleftrightarrow> a > b"
himmelma@33175
   340
  "(a::real) - b < 0 \<longleftrightarrow> a < b"
himmelma@33175
   341
  "a - b < c \<longleftrightarrow> a < c +b" "a - b > c \<longleftrightarrow> a > c +b" by arith+
himmelma@33175
   342
lemma diff_le_iff: "(a::real) - b \<ge> 0 \<longleftrightarrow> a \<ge> b" "(a::real) - b \<le> 0 \<longleftrightarrow> a \<le> b"
himmelma@33175
   343
  "a - b \<le> c \<longleftrightarrow> a \<le> c +b" "a - b \<ge> c \<longleftrightarrow> a \<ge> c +b"  by arith+
himmelma@33175
   344
himmelma@33175
   345
lemma open_ball[intro, simp]: "open (ball x e)"
huffman@45035
   346
  unfolding open_dist ball_def mem_Collect_eq Ball_def
himmelma@33175
   347
  unfolding dist_commute
himmelma@33175
   348
  apply clarify
himmelma@33175
   349
  apply (rule_tac x="e - dist xa x" in exI)
himmelma@33175
   350
  using dist_triangle_alt[where z=x]
himmelma@33175
   351
  apply (clarsimp simp add: diff_less_iff)
himmelma@33175
   352
  apply atomize
himmelma@33175
   353
  apply (erule_tac x="y" in allE)
himmelma@33175
   354
  apply (erule_tac x="xa" in allE)
himmelma@33175
   355
  by arith
himmelma@33175
   356
himmelma@33175
   357
lemma centre_in_ball[simp]: "x \<in> ball x e \<longleftrightarrow> e > 0" by (metis mem_ball dist_self)
himmelma@33175
   358
lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"
himmelma@33175
   359
  unfolding open_dist subset_eq mem_ball Ball_def dist_commute ..
himmelma@33175
   360
hoelzl@33711
   361
lemma openE[elim?]:
hoelzl@33711
   362
  assumes "open S" "x\<in>S" 
hoelzl@33711
   363
  obtains e where "e>0" "ball x e \<subseteq> S"
hoelzl@33711
   364
  using assms unfolding open_contains_ball by auto
hoelzl@33711
   365
himmelma@33175
   366
lemma open_contains_ball_eq: "open S \<Longrightarrow> \<forall>x. x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
himmelma@33175
   367
  by (metis open_contains_ball subset_eq centre_in_ball)
himmelma@33175
   368
himmelma@33175
   369
lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"
nipkow@39535
   370
  unfolding mem_ball set_eq_iff
himmelma@33175
   371
  apply (simp add: not_less)
himmelma@33175
   372
  by (metis zero_le_dist order_trans dist_self)
himmelma@33175
   373
himmelma@33175
   374
lemma ball_empty[intro]: "e \<le> 0 ==> ball x e = {}" by simp
himmelma@33175
   375
himmelma@33175
   376
himmelma@33175
   377
subsection{* Connectedness *}
himmelma@33175
   378
himmelma@33175
   379
definition "connected S \<longleftrightarrow>
himmelma@33175
   380
  ~(\<exists>e1 e2. open e1 \<and> open e2 \<and> S \<subseteq> (e1 \<union> e2) \<and> (e1 \<inter> e2 \<inter> S = {})
himmelma@33175
   381
  \<and> ~(e1 \<inter> S = {}) \<and> ~(e2 \<inter> S = {}))"
himmelma@33175
   382
himmelma@33175
   383
lemma connected_local:
himmelma@33175
   384
 "connected S \<longleftrightarrow> ~(\<exists>e1 e2.
himmelma@33175
   385
                 openin (subtopology euclidean S) e1 \<and>
himmelma@33175
   386
                 openin (subtopology euclidean S) e2 \<and>
himmelma@33175
   387
                 S \<subseteq> e1 \<union> e2 \<and>
himmelma@33175
   388
                 e1 \<inter> e2 = {} \<and>
himmelma@33175
   389
                 ~(e1 = {}) \<and>
himmelma@33175
   390
                 ~(e2 = {}))"
himmelma@33175
   391
unfolding connected_def openin_open by (safe, blast+)
himmelma@33175
   392
huffman@34099
   393
lemma exists_diff:
huffman@34099
   394
  fixes P :: "'a set \<Rightarrow> bool"
huffman@34099
   395
  shows "(\<exists>S. P(- S)) \<longleftrightarrow> (\<exists>S. P S)" (is "?lhs \<longleftrightarrow> ?rhs")
himmelma@33175
   396
proof-
himmelma@33175
   397
  {assume "?lhs" hence ?rhs by blast }
himmelma@33175
   398
  moreover
himmelma@33175
   399
  {fix S assume H: "P S"
huffman@34099
   400
    have "S = - (- S)" by auto
huffman@34099
   401
    with H have "P (- (- S))" by metis }
himmelma@33175
   402
  ultimately show ?thesis by metis
himmelma@33175
   403
qed
himmelma@33175
   404
himmelma@33175
   405
lemma connected_clopen: "connected S \<longleftrightarrow>
himmelma@33175
   406
        (\<forall>T. openin (subtopology euclidean S) T \<and>
himmelma@33175
   407
            closedin (subtopology euclidean S) T \<longrightarrow> T = {} \<or> T = S)" (is "?lhs \<longleftrightarrow> ?rhs")
himmelma@33175
   408
proof-
huffman@34099
   409
  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
   410
    unfolding connected_def openin_open closedin_closed
himmelma@33175
   411
    apply (subst exists_diff) by blast
huffman@34099
   412
  hence th0: "connected S \<longleftrightarrow> \<not> (\<exists>e2 e1. closed e2 \<and> open e1 \<and> S \<subseteq> e1 \<union> (- e2) \<and> e1 \<inter> (- e2) \<inter> S = {} \<and> e1 \<inter> S \<noteq> {} \<and> (- e2) \<inter> S \<noteq> {})"
huffman@34099
   413
    (is " _ \<longleftrightarrow> \<not> (\<exists>e2 e1. ?P e2 e1)") apply (simp add: closed_def) by metis
himmelma@33175
   414
himmelma@33175
   415
  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
   416
    (is "_ \<longleftrightarrow> \<not> (\<exists>t' t. ?Q t' t)")
himmelma@33175
   417
    unfolding connected_def openin_open closedin_closed by auto
himmelma@33175
   418
  {fix e2
himmelma@33175
   419
    {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
   420
        by auto}
himmelma@33175
   421
    then have "(\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by metis}
himmelma@33175
   422
  then have "\<forall>e2. (\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by blast
himmelma@33175
   423
  then show ?thesis unfolding th0 th1 by simp
himmelma@33175
   424
qed
himmelma@33175
   425
himmelma@33175
   426
lemma connected_empty[simp, intro]: "connected {}"
himmelma@33175
   427
  by (simp add: connected_def)
himmelma@33175
   428
huffman@45081
   429
himmelma@33175
   430
subsection{* Limit points *}
himmelma@33175
   431
huffman@45078
   432
definition (in topological_space)
huffman@45078
   433
  islimpt:: "'a \<Rightarrow> 'a set \<Rightarrow> bool" (infixr "islimpt" 60) where
himmelma@33175
   434
  "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
   435
himmelma@33175
   436
lemma islimptI:
himmelma@33175
   437
  assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
himmelma@33175
   438
  shows "x islimpt S"
himmelma@33175
   439
  using assms unfolding islimpt_def by auto
himmelma@33175
   440
himmelma@33175
   441
lemma islimptE:
himmelma@33175
   442
  assumes "x islimpt S" and "x \<in> T" and "open T"
himmelma@33175
   443
  obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"
himmelma@33175
   444
  using assms unfolding islimpt_def by auto
himmelma@33175
   445
himmelma@33175
   446
lemma islimpt_subset: "x islimpt S \<Longrightarrow> S \<subseteq> T ==> x islimpt T" by (auto simp add: islimpt_def)
himmelma@33175
   447
himmelma@33175
   448
lemma islimpt_approachable:
himmelma@33175
   449
  fixes x :: "'a::metric_space"
himmelma@33175
   450
  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"
himmelma@33175
   451
  unfolding islimpt_def
himmelma@33175
   452
  apply auto
himmelma@33175
   453
  apply(erule_tac x="ball x e" in allE)
himmelma@33175
   454
  apply auto
himmelma@33175
   455
  apply(rule_tac x=y in bexI)
himmelma@33175
   456
  apply (auto simp add: dist_commute)
himmelma@33175
   457
  apply (simp add: open_dist, drule (1) bspec)
himmelma@33175
   458
  apply (clarify, drule spec, drule (1) mp, auto)
himmelma@33175
   459
  done
himmelma@33175
   460
himmelma@33175
   461
lemma islimpt_approachable_le:
himmelma@33175
   462
  fixes x :: "'a::metric_space"
himmelma@33175
   463
  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> S. x' \<noteq> x \<and> dist x' x <= e)"
himmelma@33175
   464
  unfolding islimpt_approachable
himmelma@33175
   465
  using approachable_lt_le[where f="\<lambda>x'. dist x' x" and P="\<lambda>x'. \<not> (x'\<in>S \<and> x'\<noteq>x)"]
paulson@33307
   466
  by metis 
himmelma@33175
   467
huffman@45081
   468
text {* A perfect space has no isolated points. *}
huffman@45081
   469
huffman@45078
   470
class perfect_space = topological_space +
huffman@45078
   471
  assumes islimpt_UNIV [simp, intro]: "x islimpt UNIV"
himmelma@33175
   472
himmelma@33175
   473
lemma perfect_choose_dist:
huffman@44943
   474
  fixes x :: "'a::{perfect_space, metric_space}"
himmelma@33175
   475
  shows "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"
himmelma@33175
   476
using islimpt_UNIV [of x]
himmelma@33175
   477
by (simp add: islimpt_approachable)
himmelma@33175
   478
huffman@44987
   479
instance euclidean_space \<subseteq> perfect_space
huffman@44980
   480
proof
huffman@44980
   481
  fix x :: 'a
hoelzl@37489
   482
  { fix e :: real assume "0 < e"
huffman@44980
   483
    def y \<equiv> "x + scaleR (e/2) (sgn (basis 0))"
huffman@44980
   484
    from `0 < e` have "y \<noteq> x"
huffman@45032
   485
      unfolding y_def by (simp add: sgn_zero_iff DIM_positive)
huffman@44980
   486
    from `0 < e` have "dist y x < e"
huffman@44980
   487
      unfolding y_def by (simp add: dist_norm norm_sgn)
himmelma@33175
   488
    from `y \<noteq> x` and `dist y x < e`
himmelma@33175
   489
    have "\<exists>y\<in>UNIV. y \<noteq> x \<and> dist y x < e" by auto
himmelma@33175
   490
  }
himmelma@33175
   491
  then show "x islimpt UNIV" unfolding islimpt_approachable by blast
himmelma@33175
   492
qed
himmelma@33175
   493
himmelma@33175
   494
lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"
himmelma@33175
   495
  unfolding closed_def
himmelma@33175
   496
  apply (subst open_subopen)
huffman@34099
   497
  apply (simp add: islimpt_def subset_eq)
huffman@45035
   498
  by (metis ComplE ComplI)
himmelma@33175
   499
himmelma@33175
   500
lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"
himmelma@33175
   501
  unfolding islimpt_def by auto
himmelma@33175
   502
himmelma@33175
   503
lemma finite_set_avoid:
himmelma@33175
   504
  fixes a :: "'a::metric_space"
himmelma@33175
   505
  assumes fS: "finite S" shows  "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d <= dist a x"
himmelma@33175
   506
proof(induct rule: finite_induct[OF fS])
boehmes@42734
   507
  case 1 thus ?case by (auto intro: zero_less_one)
himmelma@33175
   508
next
himmelma@33175
   509
  case (2 x F)
himmelma@33175
   510
  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
   511
  {assume "x = a" hence ?case using d by auto  }
himmelma@33175
   512
  moreover
himmelma@33175
   513
  {assume xa: "x\<noteq>a"
himmelma@33175
   514
    let ?d = "min d (dist a x)"
himmelma@33175
   515
    have dp: "?d > 0" using xa d(1) using dist_nz by auto
himmelma@33175
   516
    from d have d': "\<forall>x\<in>F. x\<noteq>a \<longrightarrow> ?d \<le> dist a x" by auto
himmelma@33175
   517
    with dp xa have ?case by(auto intro!: exI[where x="?d"]) }
himmelma@33175
   518
  ultimately show ?case by blast
himmelma@33175
   519
qed
himmelma@33175
   520
himmelma@33175
   521
lemma islimpt_finite:
himmelma@33175
   522
  fixes S :: "'a::metric_space set"
himmelma@33175
   523
  assumes fS: "finite S" shows "\<not> a islimpt S"
himmelma@33175
   524
  unfolding islimpt_approachable
himmelma@33175
   525
  using finite_set_avoid[OF fS, of a] by (metis dist_commute  not_le)
himmelma@33175
   526
himmelma@33175
   527
lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"
himmelma@33175
   528
  apply (rule iffI)
himmelma@33175
   529
  defer
himmelma@33175
   530
  apply (metis Un_upper1 Un_upper2 islimpt_subset)
himmelma@33175
   531
  unfolding islimpt_def
himmelma@33175
   532
  apply (rule ccontr, clarsimp, rename_tac A B)
himmelma@33175
   533
  apply (drule_tac x="A \<inter> B" in spec)
himmelma@33175
   534
  apply (auto simp add: open_Int)
himmelma@33175
   535
  done
himmelma@33175
   536
himmelma@33175
   537
lemma discrete_imp_closed:
himmelma@33175
   538
  fixes S :: "'a::metric_space set"
himmelma@33175
   539
  assumes e: "0 < e" and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"
himmelma@33175
   540
  shows "closed S"
himmelma@33175
   541
proof-
himmelma@33175
   542
  {fix x assume C: "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e"
himmelma@33175
   543
    from e have e2: "e/2 > 0" by arith
himmelma@33175
   544
    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
   545
    let ?m = "min (e/2) (dist x y) "
himmelma@33175
   546
    from e2 y(2) have mp: "?m > 0" by (simp add: dist_nz[THEN sym])
himmelma@33175
   547
    from C[rule_format, OF mp] obtain z where z: "z \<in> S" "z\<noteq>x" "dist z x < ?m" by blast
himmelma@33175
   548
    have th: "dist z y < e" using z y
himmelma@33175
   549
      by (intro dist_triangle_lt [where z=x], simp)
himmelma@33175
   550
    from d[rule_format, OF y(1) z(1) th] y z
himmelma@33175
   551
    have False by (auto simp add: dist_commute)}
himmelma@33175
   552
  then show ?thesis by (metis islimpt_approachable closed_limpt [where 'a='a])
himmelma@33175
   553
qed
himmelma@33175
   554
huffman@45081
   555
huffman@45081
   556
subsection {* Interior of a Set *}
huffman@45081
   557
himmelma@33175
   558
definition "interior S = {x. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S}"
himmelma@33175
   559
himmelma@33175
   560
lemma interior_eq: "interior S = S \<longleftrightarrow> open S"
nipkow@39535
   561
  apply (simp add: set_eq_iff interior_def)
himmelma@33175
   562
  apply (subst (2) open_subopen) by (safe, blast+)
himmelma@33175
   563
himmelma@33175
   564
lemma interior_open: "open S ==> (interior S = S)" by (metis interior_eq)
himmelma@33175
   565
himmelma@33175
   566
lemma interior_empty[simp]: "interior {} = {}" by (simp add: interior_def)
himmelma@33175
   567
himmelma@33175
   568
lemma open_interior[simp, intro]: "open(interior S)"
himmelma@33175
   569
  apply (simp add: interior_def)
himmelma@33175
   570
  apply (subst open_subopen) by blast
himmelma@33175
   571
himmelma@33175
   572
lemma interior_interior[simp]: "interior(interior S) = interior S" by (metis interior_eq open_interior)
himmelma@33175
   573
lemma interior_subset: "interior S \<subseteq> S" by (auto simp add: interior_def)
himmelma@33175
   574
lemma subset_interior: "S \<subseteq> T ==> (interior S) \<subseteq> (interior T)" by (auto simp add: interior_def)
himmelma@33175
   575
lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T ==> T \<subseteq> (interior S)" by (auto simp add: interior_def)
himmelma@33175
   576
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
   577
  by (metis equalityI interior_maximal interior_subset open_interior)
himmelma@33175
   578
lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e. 0 < e \<and> ball x e \<subseteq> S)"
himmelma@33175
   579
  apply (simp add: interior_def)
himmelma@33175
   580
  by (metis open_contains_ball centre_in_ball open_ball subset_trans)
himmelma@33175
   581
himmelma@33175
   582
lemma open_subset_interior: "open S ==> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"
himmelma@33175
   583
  by (metis interior_maximal interior_subset subset_trans)
himmelma@33175
   584
himmelma@33175
   585
lemma interior_inter[simp]: "interior(S \<inter> T) = interior S \<inter> interior T"
himmelma@33175
   586
  apply (rule equalityI, simp)
himmelma@33175
   587
  apply (metis Int_lower1 Int_lower2 subset_interior)
himmelma@33175
   588
  by (metis Int_mono interior_subset open_Int open_interior open_subset_interior)
himmelma@33175
   589
himmelma@33175
   590
lemma interior_limit_point [intro]:
himmelma@33175
   591
  fixes x :: "'a::perfect_space"
himmelma@33175
   592
  assumes x: "x \<in> interior S" shows "x islimpt S"
huffman@44943
   593
  using x islimpt_UNIV [of x]
huffman@44943
   594
  unfolding interior_def islimpt_def
huffman@44943
   595
  apply (clarsimp, rename_tac T T')
huffman@44943
   596
  apply (drule_tac x="T \<inter> T'" in spec)
huffman@44943
   597
  apply (auto simp add: open_Int)
huffman@44943
   598
  done
himmelma@33175
   599
himmelma@33175
   600
lemma interior_closed_Un_empty_interior:
himmelma@33175
   601
  assumes cS: "closed S" and iT: "interior T = {}"
himmelma@33175
   602
  shows "interior(S \<union> T) = interior S"
himmelma@33175
   603
proof
himmelma@33175
   604
  show "interior S \<subseteq> interior (S\<union>T)"
himmelma@33175
   605
    by (rule subset_interior, blast)
himmelma@33175
   606
next
himmelma@33175
   607
  show "interior (S \<union> T) \<subseteq> interior S"
himmelma@33175
   608
  proof
himmelma@33175
   609
    fix x assume "x \<in> interior (S \<union> T)"
himmelma@33175
   610
    then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T"
himmelma@33175
   611
      unfolding interior_def by fast
himmelma@33175
   612
    show "x \<in> interior S"
himmelma@33175
   613
    proof (rule ccontr)
himmelma@33175
   614
      assume "x \<notin> interior S"
himmelma@33175
   615
      with `x \<in> R` `open R` obtain y where "y \<in> R - S"
nipkow@39535
   616
        unfolding interior_def set_eq_iff by fast
himmelma@33175
   617
      from `open R` `closed S` have "open (R - S)" by (rule open_Diff)
himmelma@33175
   618
      from `R \<subseteq> S \<union> T` have "R - S \<subseteq> T" by fast
himmelma@33175
   619
      from `y \<in> R - S` `open (R - S)` `R - S \<subseteq> T` `interior T = {}`
himmelma@33175
   620
      show "False" unfolding interior_def by fast
himmelma@33175
   621
    qed
himmelma@33175
   622
  qed
himmelma@33175
   623
qed
himmelma@33175
   624
huffman@45236
   625
lemma interior_Times: "interior (A \<times> B) = interior A \<times> interior B"
huffman@45236
   626
proof (rule interior_unique)
huffman@45236
   627
  show "interior A \<times> interior B \<subseteq> A \<times> B"
huffman@45236
   628
    by (intro Sigma_mono interior_subset)
huffman@45236
   629
  show "open (interior A \<times> interior B)"
huffman@45236
   630
    by (intro open_Times open_interior)
huffman@45236
   631
  show "\<forall>T. T \<subseteq> A \<times> B \<and> open T \<longrightarrow> T \<subseteq> interior A \<times> interior B"
huffman@45236
   632
    apply (simp add: open_prod_def, clarify)
huffman@45236
   633
    apply (drule (1) bspec, clarify, rename_tac C D)
huffman@45236
   634
    apply (simp add: interior_def, rule conjI)
huffman@45236
   635
    apply (rule_tac x=C in exI, clarsimp)
huffman@45236
   636
    apply (rule SigmaD1, erule subsetD, erule subsetD, erule (1) SigmaI)
huffman@45236
   637
    apply (rule_tac x=D in exI, clarsimp)
huffman@45236
   638
    apply (rule SigmaD2, erule subsetD, erule subsetD, erule (1) SigmaI)
huffman@45236
   639
    done
huffman@45236
   640
qed
huffman@45236
   641
himmelma@33175
   642
huffman@45081
   643
subsection {* Closure of a Set *}
himmelma@33175
   644
himmelma@33175
   645
definition "closure S = S \<union> {x | x. x islimpt S}"
himmelma@33175
   646
huffman@45373
   647
lemma interior_closure: "interior S = - (closure (- S))"
huffman@45373
   648
  unfolding interior_def closure_def islimpt_def by auto
huffman@45373
   649
huffman@34099
   650
lemma closure_interior: "closure S = - interior (- S)"
huffman@45373
   651
  unfolding interior_closure by simp
himmelma@33175
   652
himmelma@33175
   653
lemma closed_closure[simp, intro]: "closed (closure S)"
huffman@45373
   654
  unfolding closure_interior by (simp add: closed_Compl)
huffman@45373
   655
huffman@45373
   656
lemma closure_subset: "S \<subseteq> closure S"
huffman@45373
   657
  unfolding closure_def by simp
himmelma@33175
   658
himmelma@33175
   659
lemma closure_hull: "closure S = closed hull S"
himmelma@33175
   660
proof-
himmelma@33175
   661
  have "S \<subseteq> closure S"
huffman@45373
   662
    by (rule closure_subset)
himmelma@33175
   663
  moreover
himmelma@33175
   664
  have "closed (closure S)"
huffman@45373
   665
    by (rule closed_closure)
himmelma@33175
   666
  moreover
himmelma@33175
   667
  { fix t
himmelma@33175
   668
    assume *:"S \<subseteq> t" "closed t"
himmelma@33175
   669
    { fix x
himmelma@33175
   670
      assume "x islimpt S"
himmelma@33175
   671
      hence "x islimpt t" using *(1)
himmelma@33175
   672
        using islimpt_subset[of x, of S, of t]
himmelma@33175
   673
        by blast
himmelma@33175
   674
    }
himmelma@33175
   675
    with * have "closure S \<subseteq> t"
himmelma@33175
   676
      unfolding closure_def
himmelma@33175
   677
      using closed_limpt[of t]
himmelma@33175
   678
      by auto
himmelma@33175
   679
  }
himmelma@33175
   680
  ultimately show ?thesis
huffman@45373
   681
    by (rule hull_unique [symmetric])
himmelma@33175
   682
qed
himmelma@33175
   683
himmelma@33175
   684
lemma closure_eq: "closure S = S \<longleftrightarrow> closed S"
himmelma@33175
   685
  unfolding closure_hull
huffman@45035
   686
  using hull_eq[of closed, OF  closed_Inter, of S]
huffman@45035
   687
  by metis
himmelma@33175
   688
himmelma@33175
   689
lemma closure_closed[simp]: "closed S \<Longrightarrow> closure S = S"
himmelma@33175
   690
  using closure_eq[of S]
himmelma@33175
   691
  by simp
himmelma@33175
   692
himmelma@33175
   693
lemma closure_closure[simp]: "closure (closure S) = closure S"
huffman@45373
   694
  unfolding closure_hull by (rule hull_hull)
himmelma@33175
   695
himmelma@33175
   696
lemma subset_closure: "S \<subseteq> T \<Longrightarrow> closure S \<subseteq> closure T"
huffman@45373
   697
  unfolding closure_hull by (rule hull_mono)
himmelma@33175
   698
himmelma@33175
   699
lemma closure_minimal: "S \<subseteq> T \<Longrightarrow>  closed T \<Longrightarrow> closure S \<subseteq> T"
huffman@45373
   700
  unfolding closure_hull by (rule hull_minimal)
himmelma@33175
   701
himmelma@33175
   702
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
   703
  using hull_unique[of S T closed]
huffman@45035
   704
  unfolding closure_hull
himmelma@33175
   705
  by simp
himmelma@33175
   706
himmelma@33175
   707
lemma closure_empty[simp]: "closure {} = {}"
huffman@45373
   708
  using closed_empty by (rule closure_closed)
himmelma@33175
   709
himmelma@33175
   710
lemma closure_univ[simp]: "closure UNIV = UNIV"
huffman@45373
   711
  using closed_UNIV by (rule closure_closed)
huffman@45373
   712
huffman@45373
   713
lemma closure_union [simp]: "closure (S \<union> T) = closure S \<union> closure T"
huffman@45373
   714
  unfolding closure_interior by simp
himmelma@33175
   715
himmelma@33175
   716
lemma closure_eq_empty: "closure S = {} \<longleftrightarrow> S = {}"
himmelma@33175
   717
  using closure_empty closure_subset[of S]
himmelma@33175
   718
  by blast
himmelma@33175
   719
himmelma@33175
   720
lemma closure_subset_eq: "closure S \<subseteq> S \<longleftrightarrow> closed S"
himmelma@33175
   721
  using closure_eq[of S] closure_subset[of S]
himmelma@33175
   722
  by simp
himmelma@33175
   723
himmelma@33175
   724
lemma open_inter_closure_eq_empty:
himmelma@33175
   725
  "open S \<Longrightarrow> (S \<inter> closure T) = {} \<longleftrightarrow> S \<inter> T = {}"
huffman@34099
   726
  using open_subset_interior[of S "- T"]
huffman@34099
   727
  using interior_subset[of "- T"]
himmelma@33175
   728
  unfolding closure_interior
himmelma@33175
   729
  by auto
himmelma@33175
   730
himmelma@33175
   731
lemma open_inter_closure_subset:
himmelma@33175
   732
  "open S \<Longrightarrow> (S \<inter> (closure T)) \<subseteq> closure(S \<inter> T)"
himmelma@33175
   733
proof
himmelma@33175
   734
  fix x
himmelma@33175
   735
  assume as: "open S" "x \<in> S \<inter> closure T"
himmelma@33175
   736
  { assume *:"x islimpt T"
himmelma@33175
   737
    have "x islimpt (S \<inter> T)"
himmelma@33175
   738
    proof (rule islimptI)
himmelma@33175
   739
      fix A
himmelma@33175
   740
      assume "x \<in> A" "open A"
himmelma@33175
   741
      with as have "x \<in> A \<inter> S" "open (A \<inter> S)"
himmelma@33175
   742
        by (simp_all add: open_Int)
himmelma@33175
   743
      with * obtain y where "y \<in> T" "y \<in> A \<inter> S" "y \<noteq> x"
himmelma@33175
   744
        by (rule islimptE)
himmelma@33175
   745
      hence "y \<in> S \<inter> T" "y \<in> A \<and> y \<noteq> x"
himmelma@33175
   746
        by simp_all
himmelma@33175
   747
      thus "\<exists>y\<in>(S \<inter> T). y \<in> A \<and> y \<noteq> x" ..
himmelma@33175
   748
    qed
himmelma@33175
   749
  }
himmelma@33175
   750
  then show "x \<in> closure (S \<inter> T)" using as
himmelma@33175
   751
    unfolding closure_def
himmelma@33175
   752
    by blast
himmelma@33175
   753
qed
himmelma@33175
   754
huffman@34099
   755
lemma closure_complement: "closure(- S) = - interior(S)"
huffman@45373
   756
  unfolding closure_interior by simp
himmelma@33175
   757
huffman@34099
   758
lemma interior_complement: "interior(- S) = - closure(S)"
huffman@45373
   759
  unfolding closure_interior by simp
himmelma@33175
   760
huffman@45236
   761
lemma closure_Times: "closure (A \<times> B) = closure A \<times> closure B"
huffman@45236
   762
proof (intro closure_unique conjI)
huffman@45236
   763
  show "A \<times> B \<subseteq> closure A \<times> closure B"
huffman@45236
   764
    by (intro Sigma_mono closure_subset)
huffman@45236
   765
  show "closed (closure A \<times> closure B)"
huffman@45236
   766
    by (intro closed_Times closed_closure)
huffman@45236
   767
  show "\<forall>T. A \<times> B \<subseteq> T \<and> closed T \<longrightarrow> closure A \<times> closure B \<subseteq> T"
huffman@45236
   768
    apply (simp add: closed_def open_prod_def, clarify)
huffman@45236
   769
    apply (rule ccontr)
huffman@45236
   770
    apply (drule_tac x="(a, b)" in bspec, simp, clarify, rename_tac C D)
huffman@45236
   771
    apply (simp add: closure_interior interior_def)
huffman@45236
   772
    apply (drule_tac x=C in spec)
huffman@45236
   773
    apply (drule_tac x=D in spec)
huffman@45236
   774
    apply auto
huffman@45236
   775
    done
huffman@45236
   776
qed
huffman@45236
   777
huffman@45081
   778
huffman@45081
   779
subsection {* Frontier (aka boundary) *}
himmelma@33175
   780
himmelma@33175
   781
definition "frontier S = closure S - interior S"
himmelma@33175
   782
himmelma@33175
   783
lemma frontier_closed: "closed(frontier S)"
himmelma@33175
   784
  by (simp add: frontier_def closed_Diff)
himmelma@33175
   785
huffman@34099
   786
lemma frontier_closures: "frontier S = (closure S) \<inter> (closure(- S))"
himmelma@33175
   787
  by (auto simp add: frontier_def interior_closure)
himmelma@33175
   788
himmelma@33175
   789
lemma frontier_straddle:
himmelma@33175
   790
  fixes a :: "'a::metric_space"
himmelma@33175
   791
  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
   792
proof
himmelma@33175
   793
  assume "?lhs"
himmelma@33175
   794
  { fix e::real
himmelma@33175
   795
    assume "e > 0"
himmelma@33175
   796
    let ?rhse = "(\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e)"
himmelma@33175
   797
    { assume "a\<in>S"
himmelma@33175
   798
      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
   799
      moreover have "\<exists>x. x \<notin> S \<and> dist a x < e" using `?lhs` `a\<in>S`
himmelma@33175
   800
        unfolding frontier_closures closure_def islimpt_def using `e>0`
himmelma@33175
   801
        by (auto, erule_tac x="ball a e" in allE, auto)
himmelma@33175
   802
      ultimately have ?rhse by auto
himmelma@33175
   803
    }
himmelma@33175
   804
    moreover
himmelma@33175
   805
    { assume "a\<notin>S"
himmelma@33175
   806
      hence ?rhse using `?lhs`
himmelma@33175
   807
        unfolding frontier_closures closure_def islimpt_def
himmelma@33175
   808
        using open_ball[of a e] `e > 0`
paulson@33307
   809
          by simp (metis centre_in_ball mem_ball open_ball) 
himmelma@33175
   810
    }
himmelma@33175
   811
    ultimately have ?rhse by auto
himmelma@33175
   812
  }
himmelma@33175
   813
  thus ?rhs by auto
himmelma@33175
   814
next
himmelma@33175
   815
  assume ?rhs
himmelma@33175
   816
  moreover
himmelma@33175
   817
  { fix T assume "a\<notin>S" and
himmelma@33175
   818
    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
   819
    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
   820
    then obtain e where "e>0" "ball a e \<subseteq> T" by auto
himmelma@33175
   821
    then obtain y where y:"y\<in>S" "dist a y < e"  using as(1) by auto
himmelma@33175
   822
    have "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> a"
himmelma@33175
   823
      using `dist a y < e` `ball a e \<subseteq> T` unfolding ball_def using `y\<in>S` `a\<notin>S` by auto
himmelma@33175
   824
  }
himmelma@33175
   825
  hence "a \<in> closure S" unfolding closure_def islimpt_def using `?rhs` by auto
himmelma@33175
   826
  moreover
himmelma@33175
   827
  { fix T assume "a \<in> T"  "open T" "a\<in>S"
himmelma@33175
   828
    then obtain e where "e>0" and balle: "ball a e \<subseteq> T" unfolding open_contains_ball using `?rhs` by auto
himmelma@33175
   829
    obtain x where "x \<notin> S" "dist a x < e" using `?rhs` using `e>0` by auto
huffman@34099
   830
    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
   831
  }
huffman@34099
   832
  hence "a islimpt (- S) \<or> a\<notin>S" unfolding islimpt_def by auto
huffman@34099
   833
  ultimately show ?lhs unfolding frontier_closures using closure_def[of "- S"] by auto
himmelma@33175
   834
qed
himmelma@33175
   835
himmelma@33175
   836
lemma frontier_subset_closed: "closed S \<Longrightarrow> frontier S \<subseteq> S"
himmelma@33175
   837
  by (metis frontier_def closure_closed Diff_subset)
himmelma@33175
   838
hoelzl@34951
   839
lemma frontier_empty[simp]: "frontier {} = {}"
huffman@36358
   840
  by (simp add: frontier_def)
himmelma@33175
   841
himmelma@33175
   842
lemma frontier_subset_eq: "frontier S \<subseteq> S \<longleftrightarrow> closed S"
himmelma@33175
   843
proof-
himmelma@33175
   844
  { assume "frontier S \<subseteq> S"
himmelma@33175
   845
    hence "closure S \<subseteq> S" using interior_subset unfolding frontier_def by auto
himmelma@33175
   846
    hence "closed S" using closure_subset_eq by auto
himmelma@33175
   847
  }
huffman@36358
   848
  thus ?thesis using frontier_subset_closed[of S] ..
himmelma@33175
   849
qed
himmelma@33175
   850
huffman@34099
   851
lemma frontier_complement: "frontier(- S) = frontier S"
himmelma@33175
   852
  by (auto simp add: frontier_def closure_complement interior_complement)
himmelma@33175
   853
himmelma@33175
   854
lemma frontier_disjoint_eq: "frontier S \<inter> S = {} \<longleftrightarrow> open S"
huffman@34099
   855
  using frontier_complement frontier_subset_eq[of "- S"]
huffman@34099
   856
  unfolding open_closed by auto
himmelma@33175
   857
huffman@45081
   858
huffman@44952
   859
subsection {* Filters and the ``eventually true'' quantifier *}
huffman@44952
   860
himmelma@33175
   861
definition
huffman@44952
   862
  at_infinity :: "'a::real_normed_vector filter" where
huffman@44952
   863
  "at_infinity = Abs_filter (\<lambda>P. \<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x)"
himmelma@33175
   864
himmelma@33175
   865
definition
huffman@44952
   866
  indirection :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> 'a filter"
huffman@44952
   867
    (infixr "indirection" 70) where
himmelma@33175
   868
  "a indirection v = (at a) within {b. \<exists>c\<ge>0. b - a = scaleR c v}"
himmelma@33175
   869
huffman@44952
   870
text{* Prove That They are all filters. *}
himmelma@33175
   871
huffman@36354
   872
lemma eventually_at_infinity:
huffman@36354
   873
  "eventually P at_infinity \<longleftrightarrow> (\<exists>b. \<forall>x. norm x >= b \<longrightarrow> P x)"
himmelma@33175
   874
unfolding at_infinity_def
huffman@44952
   875
proof (rule eventually_Abs_filter, rule is_filter.intro)
huffman@36354
   876
  fix P Q :: "'a \<Rightarrow> bool"
huffman@36354
   877
  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@36354
   878
  then obtain r s where
huffman@36354
   879
    "\<forall>x. r \<le> norm x \<longrightarrow> P x" and "\<forall>x. s \<le> norm x \<longrightarrow> Q x" by auto
huffman@36354
   880
  then have "\<forall>x. max r s \<le> norm x \<longrightarrow> P x \<and> Q x" by simp
huffman@36354
   881
  then show "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x \<and> Q x" ..
huffman@36354
   882
qed auto
himmelma@33175
   883
huffman@36433
   884
text {* Identify Trivial limits, where we can't approach arbitrarily closely. *}
himmelma@33175
   885
himmelma@33175
   886
lemma trivial_limit_within:
himmelma@33175
   887
  shows "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"
himmelma@33175
   888
proof
himmelma@33175
   889
  assume "trivial_limit (at a within S)"
himmelma@33175
   890
  thus "\<not> a islimpt S"
himmelma@33175
   891
    unfolding trivial_limit_def
huffman@36354
   892
    unfolding eventually_within eventually_at_topological
himmelma@33175
   893
    unfolding islimpt_def
nipkow@39535
   894
    apply (clarsimp simp add: set_eq_iff)
himmelma@33175
   895
    apply (rename_tac T, rule_tac x=T in exI)
huffman@36354
   896
    apply (clarsimp, drule_tac x=y in bspec, simp_all)
himmelma@33175
   897
    done
himmelma@33175
   898
next
himmelma@33175
   899
  assume "\<not> a islimpt S"
himmelma@33175
   900
  thus "trivial_limit (at a within S)"
himmelma@33175
   901
    unfolding trivial_limit_def
huffman@36354
   902
    unfolding eventually_within eventually_at_topological
himmelma@33175
   903
    unfolding islimpt_def
huffman@36354
   904
    apply clarsimp
huffman@36354
   905
    apply (rule_tac x=T in exI)
huffman@36354
   906
    apply auto
himmelma@33175
   907
    done
himmelma@33175
   908
qed
himmelma@33175
   909
himmelma@33175
   910
lemma trivial_limit_at_iff: "trivial_limit (at a) \<longleftrightarrow> \<not> a islimpt UNIV"
himmelma@33175
   911
  using trivial_limit_within [of a UNIV]
himmelma@33175
   912
  by (simp add: within_UNIV)
himmelma@33175
   913
himmelma@33175
   914
lemma trivial_limit_at:
himmelma@33175
   915
  fixes a :: "'a::perfect_space"
himmelma@33175
   916
  shows "\<not> trivial_limit (at a)"
himmelma@33175
   917
  by (simp add: trivial_limit_at_iff)
himmelma@33175
   918
himmelma@33175
   919
lemma trivial_limit_at_infinity:
huffman@44952
   920
  "\<not> trivial_limit (at_infinity :: ('a::{real_normed_vector,perfect_space}) filter)"
huffman@36354
   921
  unfolding trivial_limit_def eventually_at_infinity
huffman@36354
   922
  apply clarsimp
huffman@44943
   923
  apply (subgoal_tac "\<exists>x::'a. x \<noteq> 0", clarify)
huffman@44943
   924
   apply (rule_tac x="scaleR (b / norm x) x" in exI, simp)
huffman@44943
   925
  apply (cut_tac islimpt_UNIV [of "0::'a", unfolded islimpt_def])
huffman@44943
   926
  apply (drule_tac x=UNIV in spec, simp)
himmelma@33175
   927
  done
himmelma@33175
   928
huffman@36433
   929
text {* Some property holds "sufficiently close" to the limit point. *}
himmelma@33175
   930
himmelma@33175
   931
lemma eventually_at: (* FIXME: this replaces Limits.eventually_at *)
himmelma@33175
   932
  "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"
himmelma@33175
   933
unfolding eventually_at dist_nz by auto
himmelma@33175
   934
himmelma@33175
   935
lemma eventually_within: "eventually P (at a within S) \<longleftrightarrow>
himmelma@33175
   936
        (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"
himmelma@33175
   937
unfolding eventually_within eventually_at dist_nz by auto
himmelma@33175
   938
himmelma@33175
   939
lemma eventually_within_le: "eventually P (at a within S) \<longleftrightarrow>
himmelma@33175
   940
        (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a <= d \<longrightarrow> P x)" (is "?lhs = ?rhs")
himmelma@33175
   941
unfolding eventually_within
paulson@33307
   942
by auto (metis Rats_dense_in_nn_real order_le_less_trans order_refl) 
himmelma@33175
   943
himmelma@33175
   944
lemma eventually_happens: "eventually P net ==> trivial_limit net \<or> (\<exists>x. P x)"
huffman@36354
   945
  unfolding trivial_limit_def
huffman@36354
   946
  by (auto elim: eventually_rev_mp)
himmelma@33175
   947
himmelma@33175
   948
lemma trivial_limit_eventually: "trivial_limit net \<Longrightarrow> eventually P net"
huffman@36354
   949
  unfolding trivial_limit_def by (auto elim: eventually_rev_mp)
himmelma@33175
   950
himmelma@33175
   951
lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"
huffman@45201
   952
  by (simp add: filter_eq_iff)
himmelma@33175
   953
himmelma@33175
   954
text{* Combining theorems for "eventually" *}
himmelma@33175
   955
himmelma@33175
   956
lemma eventually_rev_mono:
himmelma@33175
   957
  "eventually P net \<Longrightarrow> (\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually Q net"
himmelma@33175
   958
using eventually_mono [of P Q] by fast
himmelma@33175
   959
himmelma@33175
   960
lemma not_eventually: "(\<forall>x. \<not> P x ) \<Longrightarrow> ~(trivial_limit net) ==> ~(eventually (\<lambda>x. P x) net)"
himmelma@33175
   961
  by (simp add: eventually_False)
himmelma@33175
   962
huffman@45081
   963
huffman@36433
   964
subsection {* Limits *}
himmelma@33175
   965
huffman@44952
   966
text{* Notation Lim to avoid collition with lim defined in analysis *}
huffman@44952
   967
huffman@44952
   968
definition Lim :: "'a filter \<Rightarrow> ('a \<Rightarrow> 'b::t2_space) \<Rightarrow> 'b"
huffman@44952
   969
  where "Lim A f = (THE l. (f ---> l) A)"
himmelma@33175
   970
himmelma@33175
   971
lemma Lim:
himmelma@33175
   972
 "(f ---> l) net \<longleftrightarrow>
himmelma@33175
   973
        trivial_limit net \<or>
himmelma@33175
   974
        (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"
himmelma@33175
   975
  unfolding tendsto_iff trivial_limit_eq by auto
himmelma@33175
   976
himmelma@33175
   977
text{* Show that they yield usual definitions in the various cases. *}
himmelma@33175
   978
himmelma@33175
   979
lemma Lim_within_le: "(f ---> l)(at a within S) \<longleftrightarrow>
himmelma@33175
   980
           (\<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
   981
  by (auto simp add: tendsto_iff eventually_within_le)
himmelma@33175
   982
himmelma@33175
   983
lemma Lim_within: "(f ---> l) (at a within S) \<longleftrightarrow>
himmelma@33175
   984
        (\<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
   985
  by (auto simp add: tendsto_iff eventually_within)
himmelma@33175
   986
himmelma@33175
   987
lemma Lim_at: "(f ---> l) (at a) \<longleftrightarrow>
himmelma@33175
   988
        (\<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
   989
  by (auto simp add: tendsto_iff eventually_at)
himmelma@33175
   990
himmelma@33175
   991
lemma Lim_at_iff_LIM: "(f ---> l) (at a) \<longleftrightarrow> f -- a --> l"
himmelma@33175
   992
  unfolding Lim_at LIM_def by (simp only: zero_less_dist_iff)
himmelma@33175
   993
himmelma@33175
   994
lemma Lim_at_infinity:
himmelma@33175
   995
  "(f ---> l) at_infinity \<longleftrightarrow> (\<forall>e>0. \<exists>b. \<forall>x. norm x >= b \<longrightarrow> dist (f x) l < e)"
himmelma@33175
   996
  by (auto simp add: tendsto_iff eventually_at_infinity)
himmelma@33175
   997
himmelma@33175
   998
lemma Lim_sequentially:
himmelma@33175
   999
 "(S ---> l) sequentially \<longleftrightarrow>
himmelma@33175
  1000
          (\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (S n) l < e)"
huffman@45081
  1001
  by (rule LIMSEQ_def) (* FIXME: redundant *)
himmelma@33175
  1002
himmelma@33175
  1003
lemma Lim_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> (f ---> l) net"
himmelma@33175
  1004
  by (rule topological_tendstoI, auto elim: eventually_rev_mono)
himmelma@33175
  1005
himmelma@33175
  1006
text{* The expected monotonicity property. *}
himmelma@33175
  1007
himmelma@33175
  1008
lemma Lim_within_empty: "(f ---> l) (net within {})"
himmelma@33175
  1009
  unfolding tendsto_def Limits.eventually_within by simp
himmelma@33175
  1010
himmelma@33175
  1011
lemma Lim_within_subset: "(f ---> l) (net within S) \<Longrightarrow> T \<subseteq> S \<Longrightarrow> (f ---> l) (net within T)"
himmelma@33175
  1012
  unfolding tendsto_def Limits.eventually_within
himmelma@33175
  1013
  by (auto elim!: eventually_elim1)
himmelma@33175
  1014
himmelma@33175
  1015
lemma Lim_Un: assumes "(f ---> l) (net within S)" "(f ---> l) (net within T)"
himmelma@33175
  1016
  shows "(f ---> l) (net within (S \<union> T))"
himmelma@33175
  1017
  using assms unfolding tendsto_def Limits.eventually_within
himmelma@33175
  1018
  apply clarify
himmelma@33175
  1019
  apply (drule spec, drule (1) mp, drule (1) mp)
himmelma@33175
  1020
  apply (drule spec, drule (1) mp, drule (1) mp)
himmelma@33175
  1021
  apply (auto elim: eventually_elim2)
himmelma@33175
  1022
  done
himmelma@33175
  1023
himmelma@33175
  1024
lemma Lim_Un_univ:
himmelma@33175
  1025
 "(f ---> l) (net within S) \<Longrightarrow> (f ---> l) (net within T) \<Longrightarrow>  S \<union> T = UNIV
himmelma@33175
  1026
        ==> (f ---> l) net"
himmelma@33175
  1027
  by (metis Lim_Un within_UNIV)
himmelma@33175
  1028
himmelma@33175
  1029
text{* Interrelations between restricted and unrestricted limits. *}
himmelma@33175
  1030
himmelma@33175
  1031
lemma Lim_at_within: "(f ---> l) net ==> (f ---> l)(net within S)"
himmelma@33175
  1032
  (* FIXME: rename *)
himmelma@33175
  1033
  unfolding tendsto_def Limits.eventually_within
himmelma@33175
  1034
  apply (clarify, drule spec, drule (1) mp, drule (1) mp)
himmelma@33175
  1035
  by (auto elim!: eventually_elim1)
himmelma@33175
  1036
huffman@45081
  1037
lemma eventually_within_interior:
huffman@45081
  1038
  assumes "x \<in> interior S"
huffman@45081
  1039
  shows "eventually P (at x within S) \<longleftrightarrow> eventually P (at x)" (is "?lhs = ?rhs")
huffman@45081
  1040
proof-
huffman@45081
  1041
  from assms obtain T where T: "open T" "x \<in> T" "T \<subseteq> S"
huffman@45081
  1042
    unfolding interior_def by fast
huffman@45081
  1043
  { assume "?lhs"
huffman@45081
  1044
    then obtain A where "open A" "x \<in> A" "\<forall>y\<in>A. y \<noteq> x \<longrightarrow> y \<in> S \<longrightarrow> P y"
huffman@45081
  1045
      unfolding Limits.eventually_within Limits.eventually_at_topological
huffman@45081
  1046
      by auto
huffman@45081
  1047
    with T have "open (A \<inter> T)" "x \<in> A \<inter> T" "\<forall>y\<in>(A \<inter> T). y \<noteq> x \<longrightarrow> P y"
huffman@45081
  1048
      by auto
huffman@45081
  1049
    then have "?rhs"
huffman@45081
  1050
      unfolding Limits.eventually_at_topological by auto
huffman@45081
  1051
  } moreover
huffman@45081
  1052
  { assume "?rhs" hence "?lhs"
huffman@45081
  1053
      unfolding Limits.eventually_within
huffman@45081
  1054
      by (auto elim: eventually_elim1)
huffman@45081
  1055
  } ultimately
huffman@45081
  1056
  show "?thesis" ..
huffman@45081
  1057
qed
huffman@45081
  1058
huffman@45081
  1059
lemma at_within_interior:
huffman@45081
  1060
  "x \<in> interior S \<Longrightarrow> at x within S = at x"
huffman@45081
  1061
  by (simp add: filter_eq_iff eventually_within_interior)
huffman@45081
  1062
huffman@45081
  1063
lemma at_within_open:
huffman@45081
  1064
  "\<lbrakk>x \<in> S; open S\<rbrakk> \<Longrightarrow> at x within S = at x"
huffman@45081
  1065
  by (simp only: at_within_interior interior_open)
huffman@45081
  1066
himmelma@33175
  1067
lemma Lim_within_open:
himmelma@33175
  1068
  fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
himmelma@33175
  1069
  assumes"a \<in> S" "open S"
huffman@45081
  1070
  shows "(f ---> l)(at a within S) \<longleftrightarrow> (f ---> l)(at a)"
huffman@45081
  1071
  using assms by (simp only: at_within_open)
himmelma@33175
  1072
hoelzl@44195
  1073
lemma Lim_within_LIMSEQ:
hoelzl@44195
  1074
  fixes a :: real and L :: "'a::metric_space"
hoelzl@44195
  1075
  assumes "\<forall>S. (\<forall>n. S n \<noteq> a \<and> S n \<in> T) \<and> S ----> a \<longrightarrow> (\<lambda>n. X (S n)) ----> L"
hoelzl@44195
  1076
  shows "(X ---> L) (at a within T)"
hoelzl@44195
  1077
proof (rule ccontr)
hoelzl@44195
  1078
  assume "\<not> (X ---> L) (at a within T)"
hoelzl@44195
  1079
  hence "\<exists>r>0. \<forall>s>0. \<exists>x\<in>T. x \<noteq> a \<and> \<bar>x - a\<bar> < s \<and> r \<le> dist (X x) L"
hoelzl@44195
  1080
    unfolding tendsto_iff eventually_within dist_norm by (simp add: not_less[symmetric])
hoelzl@44195
  1081
  then obtain r where r: "r > 0" "\<And>s. s > 0 \<Longrightarrow> \<exists>x\<in>T-{a}. \<bar>x - a\<bar> < s \<and> dist (X x) L \<ge> r" by blast
hoelzl@44195
  1082
hoelzl@44195
  1083
  let ?F = "\<lambda>n::nat. SOME x. x \<in> T \<and> x \<noteq> a \<and> \<bar>x - a\<bar> < inverse (real (Suc n)) \<and> dist (X x) L \<ge> r"
hoelzl@44195
  1084
  have "\<And>n. \<exists>x. x \<in> T \<and> x \<noteq> a \<and> \<bar>x - a\<bar> < inverse (real (Suc n)) \<and> dist (X x) L \<ge> r"
hoelzl@44195
  1085
    using r by (simp add: Bex_def)
hoelzl@44195
  1086
  hence F: "\<And>n. ?F n \<in> T \<and> ?F n \<noteq> a \<and> \<bar>?F n - a\<bar> < inverse (real (Suc n)) \<and> dist (X (?F n)) L \<ge> r"
hoelzl@44195
  1087
    by (rule someI_ex)
hoelzl@44195
  1088
  hence F1: "\<And>n. ?F n \<in> T \<and> ?F n \<noteq> a"
hoelzl@44195
  1089
    and F2: "\<And>n. \<bar>?F n - a\<bar> < inverse (real (Suc n))"
hoelzl@44195
  1090
    and F3: "\<And>n. dist (X (?F n)) L \<ge> r"
hoelzl@44195
  1091
    by fast+
hoelzl@44195
  1092
hoelzl@44195
  1093
  have "?F ----> a"
hoelzl@44195
  1094
  proof (rule LIMSEQ_I, unfold real_norm_def)
hoelzl@44195
  1095
      fix e::real
hoelzl@44195
  1096
      assume "0 < e"
hoelzl@44195
  1097
        (* choose no such that inverse (real (Suc n)) < e *)
hoelzl@44195
  1098
      then have "\<exists>no. inverse (real (Suc no)) < e" by (rule reals_Archimedean)
hoelzl@44195
  1099
      then obtain m where nodef: "inverse (real (Suc m)) < e" by auto
hoelzl@44195
  1100
      show "\<exists>no. \<forall>n. no \<le> n --> \<bar>?F n - a\<bar> < e"
hoelzl@44195
  1101
      proof (intro exI allI impI)
hoelzl@44195
  1102
        fix n
hoelzl@44195
  1103
        assume mlen: "m \<le> n"
hoelzl@44195
  1104
        have "\<bar>?F n - a\<bar> < inverse (real (Suc n))"
hoelzl@44195
  1105
          by (rule F2)
hoelzl@44195
  1106
        also have "inverse (real (Suc n)) \<le> inverse (real (Suc m))"
hoelzl@44195
  1107
          using mlen by auto
hoelzl@44195
  1108
        also from nodef have
hoelzl@44195
  1109
          "inverse (real (Suc m)) < e" .
hoelzl@44195
  1110
        finally show "\<bar>?F n - a\<bar> < e" .
hoelzl@44195
  1111
      qed
hoelzl@44195
  1112
  qed
hoelzl@44195
  1113
  moreover note `\<forall>S. (\<forall>n. S n \<noteq> a \<and> S n \<in> T) \<and> S ----> a \<longrightarrow> (\<lambda>n. X (S n)) ----> L`
hoelzl@44195
  1114
  ultimately have "(\<lambda>n. X (?F n)) ----> L" using F1 by simp
hoelzl@44195
  1115
  
hoelzl@44195
  1116
  moreover have "\<not> ((\<lambda>n. X (?F n)) ----> L)"
hoelzl@44195
  1117
  proof -
hoelzl@44195
  1118
    {
hoelzl@44195
  1119
      fix no::nat
hoelzl@44195
  1120
      obtain n where "n = no + 1" by simp
hoelzl@44195
  1121
      then have nolen: "no \<le> n" by simp
hoelzl@44195
  1122
        (* We prove this by showing that for any m there is an n\<ge>m such that |X (?F n) - L| \<ge> r *)
hoelzl@44195
  1123
      have "dist (X (?F n)) L \<ge> r"
hoelzl@44195
  1124
        by (rule F3)
hoelzl@44195
  1125
      with nolen have "\<exists>n. no \<le> n \<and> dist (X (?F n)) L \<ge> r" by fast
hoelzl@44195
  1126
    }
hoelzl@44195
  1127
    then have "(\<forall>no. \<exists>n. no \<le> n \<and> dist (X (?F n)) L \<ge> r)" by simp
hoelzl@44195
  1128
    with r have "\<exists>e>0. (\<forall>no. \<exists>n. no \<le> n \<and> dist (X (?F n)) L \<ge> e)" by auto
hoelzl@44195
  1129
    thus ?thesis by (unfold LIMSEQ_def, auto simp add: not_less)
hoelzl@44195
  1130
  qed
hoelzl@44195
  1131
  ultimately show False by simp
hoelzl@44195
  1132
qed
hoelzl@44195
  1133
hoelzl@44195
  1134
lemma Lim_right_bound:
hoelzl@44195
  1135
  fixes f :: "real \<Rightarrow> real"
hoelzl@44195
  1136
  assumes mono: "\<And>a b. a \<in> I \<Longrightarrow> b \<in> I \<Longrightarrow> x < a \<Longrightarrow> a \<le> b \<Longrightarrow> f a \<le> f b"
hoelzl@44195
  1137
  assumes bnd: "\<And>a. a \<in> I \<Longrightarrow> x < a \<Longrightarrow> K \<le> f a"
hoelzl@44195
  1138
  shows "(f ---> Inf (f ` ({x<..} \<inter> I))) (at x within ({x<..} \<inter> I))"
hoelzl@44195
  1139
proof cases
hoelzl@44195
  1140
  assume "{x<..} \<inter> I = {}" then show ?thesis by (simp add: Lim_within_empty)
hoelzl@44195
  1141
next
hoelzl@44195
  1142
  assume [simp]: "{x<..} \<inter> I \<noteq> {}"
hoelzl@44195
  1143
  show ?thesis
hoelzl@44195
  1144
  proof (rule Lim_within_LIMSEQ, safe)
hoelzl@44195
  1145
    fix S assume S: "\<forall>n. S n \<noteq> x \<and> S n \<in> {x <..} \<inter> I" "S ----> x"
hoelzl@44195
  1146
    
hoelzl@44195
  1147
    show "(\<lambda>n. f (S n)) ----> Inf (f ` ({x<..} \<inter> I))"
hoelzl@44195
  1148
    proof (rule LIMSEQ_I, rule ccontr)
hoelzl@44195
  1149
      fix r :: real assume "0 < r"
hoelzl@44195
  1150
      with Inf_close[of "f ` ({x<..} \<inter> I)" r]
hoelzl@44195
  1151
      obtain y where y: "x < y" "y \<in> I" "f y < Inf (f ` ({x <..} \<inter> I)) + r" by auto
hoelzl@44195
  1152
      from `x < y` have "0 < y - x" by auto
hoelzl@44195
  1153
      from S(2)[THEN LIMSEQ_D, OF this]
hoelzl@44195
  1154
      obtain N where N: "\<And>n. N \<le> n \<Longrightarrow> \<bar>S n - x\<bar> < y - x" by auto
hoelzl@44195
  1155
      
hoelzl@44195
  1156
      assume "\<not> (\<exists>N. \<forall>n\<ge>N. norm (f (S n) - Inf (f ` ({x<..} \<inter> I))) < r)"
hoelzl@44195
  1157
      moreover have "\<And>n. Inf (f ` ({x<..} \<inter> I)) \<le> f (S n)"
hoelzl@44195
  1158
        using S bnd by (intro Inf_lower[where z=K]) auto
hoelzl@44195
  1159
      ultimately obtain n where n: "N \<le> n" "r + Inf (f ` ({x<..} \<inter> I)) \<le> f (S n)"
hoelzl@44195
  1160
        by (auto simp: not_less field_simps)
hoelzl@44195
  1161
      with N[OF n(1)] mono[OF _ `y \<in> I`, of "S n"] S(1)[THEN spec, of n] y
hoelzl@44195
  1162
      show False by auto
hoelzl@44195
  1163
    qed
hoelzl@44195
  1164
  qed
hoelzl@44195
  1165
qed
hoelzl@44195
  1166
himmelma@33175
  1167
text{* Another limit point characterization. *}
himmelma@33175
  1168
himmelma@33175
  1169
lemma islimpt_sequential:
huffman@36667
  1170
  fixes x :: "'a::metric_space"
himmelma@33175
  1171
  shows "x islimpt S \<longleftrightarrow> (\<exists>f. (\<forall>n::nat. f n \<in> S -{x}) \<and> (f ---> x) sequentially)"
himmelma@33175
  1172
    (is "?lhs = ?rhs")
himmelma@33175
  1173
proof
himmelma@33175
  1174
  assume ?lhs
himmelma@33175
  1175
  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
  1176
    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
  1177
  { fix n::nat
himmelma@33175
  1178
    have "f (inverse (real n + 1)) \<in> S - {x}" using f by auto
himmelma@33175
  1179
  }
himmelma@33175
  1180
  moreover
himmelma@33175
  1181
  { fix e::real assume "e>0"
himmelma@33175
  1182
    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
  1183
    then obtain N::nat where "inverse (real (N + 1)) < e" by auto
himmelma@33175
  1184
    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
  1185
    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
  1186
    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
  1187
  }
himmelma@33175
  1188
  hence " ((\<lambda>n. f (inverse (real n + 1))) ---> x) sequentially"
himmelma@33175
  1189
    unfolding Lim_sequentially using f by auto
himmelma@33175
  1190
  ultimately show ?rhs apply (rule_tac x="(\<lambda>n::nat. f (inverse (real n + 1)))" in exI) by auto
himmelma@33175
  1191
next
himmelma@33175
  1192
  assume ?rhs
himmelma@33175
  1193
  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
  1194
  { fix e::real assume "e>0"
himmelma@33175
  1195
    then obtain N where "dist (f N) x < e" using f(2) by auto
himmelma@33175
  1196
    moreover have "f N\<in>S" "f N \<noteq> x" using f(1) by auto
himmelma@33175
  1197
    ultimately have "\<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e" by auto
himmelma@33175
  1198
  }
himmelma@33175
  1199
  thus ?lhs unfolding islimpt_approachable by auto
himmelma@33175
  1200
qed
himmelma@33175
  1201
huffman@44983
  1202
lemma Lim_inv: (* TODO: delete *)
huffman@44952
  1203
  fixes f :: "'a \<Rightarrow> real" and A :: "'a filter"
huffman@44952
  1204
  assumes "(f ---> l) A" and "l \<noteq> 0"
huffman@44952
  1205
  shows "((inverse o f) ---> inverse l) A"
huffman@36433
  1206
  unfolding o_def using assms by (rule tendsto_inverse)
huffman@36433
  1207
himmelma@33175
  1208
lemma Lim_null:
himmelma@33175
  1209
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
huffman@44983
  1210
  shows "(f ---> l) net \<longleftrightarrow> ((\<lambda>x. f(x) - l) ---> 0) net"
himmelma@33175
  1211
  by (simp add: Lim dist_norm)
himmelma@33175
  1212
himmelma@33175
  1213
lemma Lim_null_comparison:
himmelma@33175
  1214
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1215
  assumes "eventually (\<lambda>x. norm (f x) \<le> g x) net" "(g ---> 0) net"
himmelma@33175
  1216
  shows "(f ---> 0) net"
huffman@45119
  1217
proof (rule metric_tendsto_imp_tendsto)
huffman@45119
  1218
  show "(g ---> 0) net" by fact
huffman@45119
  1219
  show "eventually (\<lambda>x. dist (f x) 0 \<le> dist (g x) 0) net"
huffman@45119
  1220
    using assms(1) by (rule eventually_elim1, simp add: dist_norm)
himmelma@33175
  1221
qed
himmelma@33175
  1222
himmelma@33175
  1223
lemma Lim_transform_bound:
himmelma@33175
  1224
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1225
  fixes g :: "'a \<Rightarrow> 'c::real_normed_vector"
himmelma@33175
  1226
  assumes "eventually (\<lambda>n. norm(f n) <= norm(g n)) net"  "(g ---> 0) net"
himmelma@33175
  1227
  shows "(f ---> 0) net"
huffman@45119
  1228
  using assms(1) tendsto_norm_zero [OF assms(2)]
huffman@45119
  1229
  by (rule Lim_null_comparison)
himmelma@33175
  1230
himmelma@33175
  1231
text{* Deducing things about the limit from the elements. *}
himmelma@33175
  1232
himmelma@33175
  1233
lemma Lim_in_closed_set:
himmelma@33175
  1234
  assumes "closed S" "eventually (\<lambda>x. f(x) \<in> S) net" "\<not>(trivial_limit net)" "(f ---> l) net"
himmelma@33175
  1235
  shows "l \<in> S"
himmelma@33175
  1236
proof (rule ccontr)
himmelma@33175
  1237
  assume "l \<notin> S"
himmelma@33175
  1238
  with `closed S` have "open (- S)" "l \<in> - S"
himmelma@33175
  1239
    by (simp_all add: open_Compl)
himmelma@33175
  1240
  with assms(4) have "eventually (\<lambda>x. f x \<in> - S) net"
himmelma@33175
  1241
    by (rule topological_tendstoD)
himmelma@33175
  1242
  with assms(2) have "eventually (\<lambda>x. False) net"
himmelma@33175
  1243
    by (rule eventually_elim2) simp
himmelma@33175
  1244
  with assms(3) show "False"
himmelma@33175
  1245
    by (simp add: eventually_False)
himmelma@33175
  1246
qed
himmelma@33175
  1247
himmelma@33175
  1248
text{* Need to prove closed(cball(x,e)) before deducing this as a corollary. *}
himmelma@33175
  1249
himmelma@33175
  1250
lemma Lim_dist_ubound:
himmelma@33175
  1251
  assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. dist a (f x) <= e) net"
himmelma@33175
  1252
  shows "dist a l <= e"
huffman@45119
  1253
proof-
huffman@45119
  1254
  have "dist a l \<in> {..e}"
huffman@45119
  1255
  proof (rule Lim_in_closed_set)
huffman@45119
  1256
    show "closed {..e}" by simp
huffman@45119
  1257
    show "eventually (\<lambda>x. dist a (f x) \<in> {..e}) net" by (simp add: assms)
huffman@45119
  1258
    show "\<not> trivial_limit net" by fact
huffman@45119
  1259
    show "((\<lambda>x. dist a (f x)) ---> dist a l) net" by (intro tendsto_intros assms)
huffman@45119
  1260
  qed
huffman@45119
  1261
  thus ?thesis by simp
himmelma@33175
  1262
qed
himmelma@33175
  1263
himmelma@33175
  1264
lemma Lim_norm_ubound:
himmelma@33175
  1265
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1266
  assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. norm(f x) <= e) net"
himmelma@33175
  1267
  shows "norm(l) <= e"
huffman@45119
  1268
proof-
huffman@45119
  1269
  have "norm l \<in> {..e}"
huffman@45119
  1270
  proof (rule Lim_in_closed_set)
huffman@45119
  1271
    show "closed {..e}" by simp
huffman@45119
  1272
    show "eventually (\<lambda>x. norm (f x) \<in> {..e}) net" by (simp add: assms)
huffman@45119
  1273
    show "\<not> trivial_limit net" by fact
huffman@45119
  1274
    show "((\<lambda>x. norm (f x)) ---> norm l) net" by (intro tendsto_intros assms)
huffman@45119
  1275
  qed
huffman@45119
  1276
  thus ?thesis by simp
himmelma@33175
  1277
qed
himmelma@33175
  1278
himmelma@33175
  1279
lemma Lim_norm_lbound:
himmelma@33175
  1280
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1281
  assumes "\<not> (trivial_limit net)"  "(f ---> l) net"  "eventually (\<lambda>x. e <= norm(f x)) net"
himmelma@33175
  1282
  shows "e \<le> norm l"
huffman@45119
  1283
proof-
huffman@45119
  1284
  have "norm l \<in> {e..}"
huffman@45119
  1285
  proof (rule Lim_in_closed_set)
huffman@45119
  1286
    show "closed {e..}" by simp
huffman@45119
  1287
    show "eventually (\<lambda>x. norm (f x) \<in> {e..}) net" by (simp add: assms)
huffman@45119
  1288
    show "\<not> trivial_limit net" by fact
huffman@45119
  1289
    show "((\<lambda>x. norm (f x)) ---> norm l) net" by (intro tendsto_intros assms)
huffman@45119
  1290
  qed
huffman@45119
  1291
  thus ?thesis by simp
himmelma@33175
  1292
qed
himmelma@33175
  1293
himmelma@33175
  1294
text{* Uniqueness of the limit, when nontrivial. *}
himmelma@33175
  1295
himmelma@33175
  1296
lemma tendsto_Lim:
himmelma@33175
  1297
  fixes f :: "'a \<Rightarrow> 'b::t2_space"
himmelma@33175
  1298
  shows "~(trivial_limit net) \<Longrightarrow> (f ---> l) net ==> Lim net f = l"
hoelzl@42841
  1299
  unfolding Lim_def using tendsto_unique[of net f] by auto
himmelma@33175
  1300
himmelma@33175
  1301
text{* Limit under bilinear function *}
himmelma@33175
  1302
himmelma@33175
  1303
lemma Lim_bilinear:
himmelma@33175
  1304
  assumes "(f ---> l) net" and "(g ---> m) net" and "bounded_bilinear h"
himmelma@33175
  1305
  shows "((\<lambda>x. h (f x) (g x)) ---> (h l m)) net"
himmelma@33175
  1306
using `bounded_bilinear h` `(f ---> l) net` `(g ---> m) net`
himmelma@33175
  1307
by (rule bounded_bilinear.tendsto)
himmelma@33175
  1308
himmelma@33175
  1309
text{* These are special for limits out of the same vector space. *}
himmelma@33175
  1310
himmelma@33175
  1311
lemma Lim_within_id: "(id ---> a) (at a within s)"
himmelma@33175
  1312
  unfolding tendsto_def Limits.eventually_within eventually_at_topological
himmelma@33175
  1313
  by auto
himmelma@33175
  1314
himmelma@33175
  1315
lemma Lim_at_id: "(id ---> a) (at a)"
himmelma@33175
  1316
apply (subst within_UNIV[symmetric]) by (simp add: Lim_within_id)
himmelma@33175
  1317
himmelma@33175
  1318
lemma Lim_at_zero:
himmelma@33175
  1319
  fixes a :: "'a::real_normed_vector"
himmelma@33175
  1320
  fixes l :: "'b::topological_space"
himmelma@33175
  1321
  shows "(f ---> l) (at a) \<longleftrightarrow> ((\<lambda>x. f(a + x)) ---> l) (at 0)" (is "?lhs = ?rhs")
huffman@45119
  1322
  using LIM_offset_zero LIM_offset_zero_cancel ..
himmelma@33175
  1323
huffman@44952
  1324
text{* It's also sometimes useful to extract the limit point from the filter. *}
himmelma@33175
  1325
himmelma@33175
  1326
definition
huffman@44952
  1327
  netlimit :: "'a::t2_space filter \<Rightarrow> 'a" where
himmelma@33175
  1328
  "netlimit net = (SOME a. ((\<lambda>x. x) ---> a) net)"
himmelma@33175
  1329
himmelma@33175
  1330
lemma netlimit_within:
himmelma@33175
  1331
  assumes "\<not> trivial_limit (at a within S)"
himmelma@33175
  1332
  shows "netlimit (at a within S) = a"
himmelma@33175
  1333
unfolding netlimit_def
himmelma@33175
  1334
apply (rule some_equality)
himmelma@33175
  1335
apply (rule Lim_at_within)
huffman@44983
  1336
apply (rule LIM_ident)
hoelzl@42841
  1337
apply (erule tendsto_unique [OF assms])
himmelma@33175
  1338
apply (rule Lim_at_within)
huffman@44983
  1339
apply (rule LIM_ident)
himmelma@33175
  1340
done
himmelma@33175
  1341
himmelma@33175
  1342
lemma netlimit_at:
huffman@44943
  1343
  fixes a :: "'a::{perfect_space,t2_space}"
himmelma@33175
  1344
  shows "netlimit (at a) = a"
himmelma@33175
  1345
  apply (subst within_UNIV[symmetric])
himmelma@33175
  1346
  using netlimit_within[of a UNIV]
himmelma@33175
  1347
  by (simp add: trivial_limit_at within_UNIV)
himmelma@33175
  1348
huffman@45081
  1349
lemma lim_within_interior:
huffman@45081
  1350
  "x \<in> interior S \<Longrightarrow> (f ---> l) (at x within S) \<longleftrightarrow> (f ---> l) (at x)"
huffman@45081
  1351
  by (simp add: at_within_interior)
huffman@45081
  1352
huffman@45081
  1353
lemma netlimit_within_interior:
huffman@45081
  1354
  fixes x :: "'a::{t2_space,perfect_space}"
huffman@45081
  1355
  assumes "x \<in> interior S"
huffman@45081
  1356
  shows "netlimit (at x within S) = x"
huffman@45081
  1357
using assms by (simp add: at_within_interior netlimit_at)
huffman@45081
  1358
himmelma@33175
  1359
text{* Transformation of limit. *}
himmelma@33175
  1360
himmelma@33175
  1361
lemma Lim_transform:
himmelma@33175
  1362
  fixes f g :: "'a::type \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  1363
  assumes "((\<lambda>x. f x - g x) ---> 0) net" "(f ---> l) net"
himmelma@33175
  1364
  shows "(g ---> l) net"
huffman@45119
  1365
  using tendsto_diff [OF assms(2) assms(1)] by simp
himmelma@33175
  1366
himmelma@33175
  1367
lemma Lim_transform_eventually:
huffman@36667
  1368
  "eventually (\<lambda>x. f x = g x) net \<Longrightarrow> (f ---> l) net \<Longrightarrow> (g ---> l) net"
himmelma@33175
  1369
  apply (rule topological_tendstoI)
himmelma@33175
  1370
  apply (drule (2) topological_tendstoD)
himmelma@33175
  1371
  apply (erule (1) eventually_elim2, simp)
himmelma@33175
  1372
  done
himmelma@33175
  1373
himmelma@33175
  1374
lemma Lim_transform_within:
huffman@36667
  1375
  assumes "0 < d" and "\<forall>x'\<in>S. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"
huffman@36667
  1376
  and "(f ---> l) (at x within S)"
huffman@36667
  1377
  shows "(g ---> l) (at x within S)"
huffman@36667
  1378
proof (rule Lim_transform_eventually)
huffman@36667
  1379
  show "eventually (\<lambda>x. f x = g x) (at x within S)"
huffman@36667
  1380
    unfolding eventually_within
huffman@36667
  1381
    using assms(1,2) by auto
huffman@36667
  1382
  show "(f ---> l) (at x within S)" by fact
huffman@36667
  1383
qed
himmelma@33175
  1384
himmelma@33175
  1385
lemma Lim_transform_at:
huffman@36667
  1386
  assumes "0 < d" and "\<forall>x'. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"
huffman@36667
  1387
  and "(f ---> l) (at x)"
huffman@36667
  1388
  shows "(g ---> l) (at x)"
huffman@36667
  1389
proof (rule Lim_transform_eventually)
huffman@36667
  1390
  show "eventually (\<lambda>x. f x = g x) (at x)"
huffman@36667
  1391
    unfolding eventually_at
huffman@36667
  1392
    using assms(1,2) by auto
huffman@36667
  1393
  show "(f ---> l) (at x)" by fact
huffman@36667
  1394
qed
himmelma@33175
  1395
himmelma@33175
  1396
text{* Common case assuming being away from some crucial point like 0. *}
himmelma@33175
  1397
himmelma@33175
  1398
lemma Lim_transform_away_within:
huffman@36669
  1399
  fixes a b :: "'a::t1_space"
huffman@36667
  1400
  assumes "a \<noteq> b" and "\<forall>x\<in>S. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"
himmelma@33175
  1401
  and "(f ---> l) (at a within S)"
himmelma@33175
  1402
  shows "(g ---> l) (at a within S)"
huffman@36669
  1403
proof (rule Lim_transform_eventually)
huffman@36669
  1404
  show "(f ---> l) (at a within S)" by fact
huffman@36669
  1405
  show "eventually (\<lambda>x. f x = g x) (at a within S)"
huffman@36669
  1406
    unfolding Limits.eventually_within eventually_at_topological
huffman@36669
  1407
    by (rule exI [where x="- {b}"], simp add: open_Compl assms)
himmelma@33175
  1408
qed
himmelma@33175
  1409
himmelma@33175
  1410
lemma Lim_transform_away_at:
huffman@36669
  1411
  fixes a b :: "'a::t1_space"
himmelma@33175
  1412
  assumes ab: "a\<noteq>b" and fg: "\<forall>x. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"
himmelma@33175
  1413
  and fl: "(f ---> l) (at a)"
himmelma@33175
  1414
  shows "(g ---> l) (at a)"
himmelma@33175
  1415
  using Lim_transform_away_within[OF ab, of UNIV f g l] fg fl
himmelma@33175
  1416
  by (auto simp add: within_UNIV)
himmelma@33175
  1417
himmelma@33175
  1418
text{* Alternatively, within an open set. *}
himmelma@33175
  1419
himmelma@33175
  1420
lemma Lim_transform_within_open:
huffman@36667
  1421
  assumes "open S" and "a \<in> S" and "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> f x = g x"
huffman@36667
  1422
  and "(f ---> l) (at a)"
himmelma@33175
  1423
  shows "(g ---> l) (at a)"
huffman@36667
  1424
proof (rule Lim_transform_eventually)
huffman@36667
  1425
  show "eventually (\<lambda>x. f x = g x) (at a)"
huffman@36667
  1426
    unfolding eventually_at_topological
huffman@36667
  1427
    using assms(1,2,3) by auto
huffman@36667
  1428
  show "(f ---> l) (at a)" by fact
himmelma@33175
  1429
qed
himmelma@33175
  1430
himmelma@33175
  1431
text{* A congruence rule allowing us to transform limits assuming not at point. *}
himmelma@33175
  1432
himmelma@33175
  1433
(* FIXME: Only one congruence rule for tendsto can be used at a time! *)
himmelma@33175
  1434
huffman@36358
  1435
lemma Lim_cong_within(*[cong add]*):
hoelzl@44195
  1436
  assumes "a = b" "x = y" "S = T"
hoelzl@44195
  1437
  assumes "\<And>x. x \<noteq> b \<Longrightarrow> x \<in> T \<Longrightarrow> f x = g x"
hoelzl@44195
  1438
  shows "(f ---> x) (at a within S) \<longleftrightarrow> (g ---> y) (at b within T)"
huffman@36667
  1439
  unfolding tendsto_def Limits.eventually_within eventually_at_topological
huffman@36667
  1440
  using assms by simp
huffman@36667
  1441
huffman@36667
  1442
lemma Lim_cong_at(*[cong add]*):
hoelzl@44195
  1443
  assumes "a = b" "x = y"
huffman@36667
  1444
  assumes "\<And>x. x \<noteq> a \<Longrightarrow> f x = g x"
hoelzl@44195
  1445
  shows "((\<lambda>x. f x) ---> x) (at a) \<longleftrightarrow> ((g ---> y) (at a))"
huffman@36667
  1446
  unfolding tendsto_def eventually_at_topological
huffman@36667
  1447
  using assms by simp
himmelma@33175
  1448
himmelma@33175
  1449
text{* Useful lemmas on closure and set of possible sequential limits.*}
himmelma@33175
  1450
himmelma@33175
  1451
lemma closure_sequential:
huffman@36667
  1452
  fixes l :: "'a::metric_space"
himmelma@33175
  1453
  shows "l \<in> closure S \<longleftrightarrow> (\<exists>x. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially)" (is "?lhs = ?rhs")
himmelma@33175
  1454
proof
himmelma@33175
  1455
  assume "?lhs" moreover
himmelma@33175
  1456
  { assume "l \<in> S"
huffman@44983
  1457
    hence "?rhs" using tendsto_const[of l sequentially] by auto
himmelma@33175
  1458
  } moreover
himmelma@33175
  1459
  { assume "l islimpt S"
himmelma@33175
  1460
    hence "?rhs" unfolding islimpt_sequential by auto
himmelma@33175
  1461
  } ultimately
himmelma@33175
  1462
  show "?rhs" unfolding closure_def by auto
himmelma@33175
  1463
next
himmelma@33175
  1464
  assume "?rhs"
himmelma@33175
  1465
  thus "?lhs" unfolding closure_def unfolding islimpt_sequential by auto
himmelma@33175
  1466
qed
himmelma@33175
  1467
himmelma@33175
  1468
lemma closed_sequential_limits:
himmelma@33175
  1469
  fixes S :: "'a::metric_space set"
himmelma@33175
  1470
  shows "closed S \<longleftrightarrow> (\<forall>x l. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially \<longrightarrow> l \<in> S)"
himmelma@33175
  1471
  unfolding closed_limpt
himmelma@33175
  1472
  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
  1473
  by metis
himmelma@33175
  1474
himmelma@33175
  1475
lemma closure_approachable:
himmelma@33175
  1476
  fixes S :: "'a::metric_space set"
himmelma@33175
  1477
  shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e)"
himmelma@33175
  1478
  apply (auto simp add: closure_def islimpt_approachable)
himmelma@33175
  1479
  by (metis dist_self)
himmelma@33175
  1480
himmelma@33175
  1481
lemma closed_approachable:
himmelma@33175
  1482
  fixes S :: "'a::metric_space set"
himmelma@33175
  1483
  shows "closed S ==> (\<forall>e>0. \<exists>y\<in>S. dist y x < e) \<longleftrightarrow> x \<in> S"
himmelma@33175
  1484
  by (metis closure_closed closure_approachable)
himmelma@33175
  1485
himmelma@33175
  1486
text{* Some other lemmas about sequences. *}
himmelma@33175
  1487
huffman@36437
  1488
lemma sequentially_offset:
huffman@36437
  1489
  assumes "eventually (\<lambda>i. P i) sequentially"
huffman@36437
  1490
  shows "eventually (\<lambda>i. P (i + k)) sequentially"
huffman@36437
  1491
  using assms unfolding eventually_sequentially by (metis trans_le_add1)
huffman@36437
  1492
himmelma@33175
  1493
lemma seq_offset:
huffman@36437
  1494
  assumes "(f ---> l) sequentially"
huffman@36437
  1495
  shows "((\<lambda>i. f (i + k)) ---> l) sequentially"
huffman@36437
  1496
  using assms unfolding tendsto_def
huffman@36437
  1497
  by clarify (rule sequentially_offset, simp)
himmelma@33175
  1498
himmelma@33175
  1499
lemma seq_offset_neg:
himmelma@33175
  1500
  "(f ---> l) sequentially ==> ((\<lambda>i. f(i - k)) ---> l) sequentially"
himmelma@33175
  1501
  apply (rule topological_tendstoI)
himmelma@33175
  1502
  apply (drule (2) topological_tendstoD)
himmelma@33175
  1503
  apply (simp only: eventually_sequentially)
himmelma@33175
  1504
  apply (subgoal_tac "\<And>N k (n::nat). N + k <= n ==> N <= n - k")
himmelma@33175
  1505
  apply metis
himmelma@33175
  1506
  by arith
himmelma@33175
  1507
himmelma@33175
  1508
lemma seq_offset_rev:
himmelma@33175
  1509
  "((\<lambda>i. f(i + k)) ---> l) sequentially ==> (f ---> l) sequentially"
himmelma@33175
  1510
  apply (rule topological_tendstoI)
himmelma@33175
  1511
  apply (drule (2) topological_tendstoD)
himmelma@33175
  1512
  apply (simp only: eventually_sequentially)
himmelma@33175
  1513
  apply (subgoal_tac "\<And>N k (n::nat). N + k <= n ==> N <= n - k \<and> (n - k) + k = n")
himmelma@33175
  1514
  by metis arith
himmelma@33175
  1515
himmelma@33175
  1516
lemma seq_harmonic: "((\<lambda>n. inverse (real n)) ---> 0) sequentially"
himmelma@33175
  1517
proof-
himmelma@33175
  1518
  { fix e::real assume "e>0"
himmelma@33175
  1519
    hence "\<exists>N::nat. \<forall>n::nat\<ge>N. inverse (real n) < e"
himmelma@33175
  1520
      using real_arch_inv[of e] apply auto apply(rule_tac x=n in exI)
huffman@36358
  1521
      by (metis le_imp_inverse_le not_less real_of_nat_gt_zero_cancel_iff real_of_nat_less_iff xt1(7))
himmelma@33175
  1522
  }
himmelma@33175
  1523
  thus ?thesis unfolding Lim_sequentially dist_norm by simp
himmelma@33175
  1524
qed
himmelma@33175
  1525
huffman@45081
  1526
subsection {* More properties of closed balls *}
himmelma@33175
  1527
himmelma@33175
  1528
lemma closed_cball: "closed (cball x e)"
himmelma@33175
  1529
unfolding cball_def closed_def
himmelma@33175
  1530
unfolding Collect_neg_eq [symmetric] not_le
himmelma@33175
  1531
apply (clarsimp simp add: open_dist, rename_tac y)
himmelma@33175
  1532
apply (rule_tac x="dist x y - e" in exI, clarsimp)
himmelma@33175
  1533
apply (rename_tac x')
himmelma@33175
  1534
apply (cut_tac x=x and y=x' and z=y in dist_triangle)
himmelma@33175
  1535
apply simp
himmelma@33175
  1536
done
himmelma@33175
  1537
himmelma@33175
  1538
lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"
himmelma@33175
  1539
proof-
himmelma@33175
  1540
  { fix x and e::real assume "x\<in>S" "e>0" "ball x e \<subseteq> S"
himmelma@33175
  1541
    hence "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)
himmelma@33175
  1542
  } moreover
himmelma@33175
  1543
  { fix x and e::real assume "x\<in>S" "e>0" "cball x e \<subseteq> S"
himmelma@33175
  1544
    hence "\<exists>d>0. ball x d \<subseteq> S" unfolding subset_eq apply(rule_tac x="e/2" in exI) by auto
himmelma@33175
  1545
  } ultimately
himmelma@33175
  1546
  show ?thesis unfolding open_contains_ball by auto
himmelma@33175
  1547
qed
himmelma@33175
  1548
himmelma@33175
  1549
lemma open_contains_cball_eq: "open S ==> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"
huffman@45035
  1550
  by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball)
himmelma@33175
  1551
himmelma@33175
  1552
lemma mem_interior_cball: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S)"
himmelma@33175
  1553
  apply (simp add: interior_def, safe)
himmelma@33175
  1554
  apply (force simp add: open_contains_cball)
himmelma@33175
  1555
  apply (rule_tac x="ball x e" in exI)
huffman@36358
  1556
  apply (simp add: subset_trans [OF ball_subset_cball])
himmelma@33175
  1557
  done
himmelma@33175
  1558
himmelma@33175
  1559
lemma islimpt_ball:
himmelma@33175
  1560
  fixes x y :: "'a::{real_normed_vector,perfect_space}"
himmelma@33175
  1561
  shows "y islimpt ball x e \<longleftrightarrow> 0 < e \<and> y \<in> cball x e" (is "?lhs = ?rhs")
himmelma@33175
  1562
proof
himmelma@33175
  1563
  assume "?lhs"
himmelma@33175
  1564
  { assume "e \<le> 0"
himmelma@33175
  1565
    hence *:"ball x e = {}" using ball_eq_empty[of x e] by auto
himmelma@33175
  1566
    have False using `?lhs` unfolding * using islimpt_EMPTY[of y] by auto
himmelma@33175
  1567
  }
himmelma@33175
  1568
  hence "e > 0" by (metis not_less)
himmelma@33175
  1569
  moreover
himmelma@33175
  1570
  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
  1571
  ultimately show "?rhs" by auto
himmelma@33175
  1572
next
himmelma@33175
  1573
  assume "?rhs" hence "e>0"  by auto
himmelma@33175
  1574
  { fix d::real assume "d>0"
himmelma@33175
  1575
    have "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1576
    proof(cases "d \<le> dist x y")
himmelma@33175
  1577
      case True thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1578
      proof(cases "x=y")
himmelma@33175
  1579
        case True hence False using `d \<le> dist x y` `d>0` by auto
himmelma@33175
  1580
        thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d" by auto
himmelma@33175
  1581
      next
himmelma@33175
  1582
        case False
himmelma@33175
  1583
himmelma@33175
  1584
        have "dist x (y - (d / (2 * dist y x)) *\<^sub>R (y - x))
himmelma@33175
  1585
              = norm (x - y + (d / (2 * norm (y - x))) *\<^sub>R (y - x))"
himmelma@33175
  1586
          unfolding mem_cball mem_ball dist_norm diff_diff_eq2 diff_add_eq[THEN sym] by auto
himmelma@33175
  1587
        also have "\<dots> = \<bar>- 1 + d / (2 * norm (x - y))\<bar> * norm (x - y)"
himmelma@33175
  1588
          using scaleR_left_distrib[of "- 1" "d / (2 * norm (y - x))", THEN sym, of "y - x"]
himmelma@33175
  1589
          unfolding scaleR_minus_left scaleR_one
himmelma@33175
  1590
          by (auto simp add: norm_minus_commute)
himmelma@33175
  1591
        also have "\<dots> = \<bar>- norm (x - y) + d / 2\<bar>"
himmelma@33175
  1592
          unfolding abs_mult_pos[of "norm (x - y)", OF norm_ge_zero[of "x - y"]]
huffman@36770
  1593
          unfolding left_distrib using `x\<noteq>y`[unfolded dist_nz, unfolded dist_norm] by auto
himmelma@33175
  1594
        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
  1595
        finally have "y - (d / (2 * dist y x)) *\<^sub>R (y - x) \<in> ball x e" using `d>0` by auto
himmelma@33175
  1596
himmelma@33175
  1597
        moreover
himmelma@33175
  1598
himmelma@33175
  1599
        have "(d / (2*dist y x)) *\<^sub>R (y - x) \<noteq> 0"
himmelma@33175
  1600
          using `x\<noteq>y`[unfolded dist_nz] `d>0` unfolding scaleR_eq_0_iff by (auto simp add: dist_commute)
himmelma@33175
  1601
        moreover
himmelma@33175
  1602
        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
  1603
          using `d>0` `x\<noteq>y`[unfolded dist_nz] dist_commute[of x y]
himmelma@33175
  1604
          unfolding dist_norm by auto
himmelma@33175
  1605
        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
  1606
      qed
himmelma@33175
  1607
    next
himmelma@33175
  1608
      case False hence "d > dist x y" by auto
himmelma@33175
  1609
      show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1610
      proof(cases "x=y")
himmelma@33175
  1611
        case True
himmelma@33175
  1612
        obtain z where **: "z \<noteq> y" "dist z y < min e d"
himmelma@33175
  1613
          using perfect_choose_dist[of "min e d" y]
himmelma@33175
  1614
          using `d > 0` `e>0` by auto
himmelma@33175
  1615
        show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1616
          unfolding `x = y`
himmelma@33175
  1617
          using `z \<noteq> y` **
himmelma@33175
  1618
          by (rule_tac x=z in bexI, auto simp add: dist_commute)
himmelma@33175
  1619
      next
himmelma@33175
  1620
        case False thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
himmelma@33175
  1621
          using `d>0` `d > dist x y` `?rhs` by(rule_tac x=x in bexI, auto)
himmelma@33175
  1622
      qed
himmelma@33175
  1623
    qed  }
himmelma@33175
  1624
  thus "?lhs" unfolding mem_cball islimpt_approachable mem_ball by auto
himmelma@33175
  1625
qed
himmelma@33175
  1626
himmelma@33175
  1627
lemma closure_ball_lemma:
himmelma@33175
  1628
  fixes x y :: "'a::real_normed_vector"
himmelma@33175
  1629
  assumes "x \<noteq> y" shows "y islimpt ball x (dist x y)"
himmelma@33175
  1630
proof (rule islimptI)
himmelma@33175
  1631
  fix T assume "y \<in> T" "open T"
himmelma@33175
  1632
  then obtain r where "0 < r" "\<forall>z. dist z y < r \<longrightarrow> z \<in> T"
himmelma@33175
  1633
    unfolding open_dist by fast
himmelma@33175
  1634
  (* choose point between x and y, within distance r of y. *)
himmelma@33175
  1635
  def k \<equiv> "min 1 (r / (2 * dist x y))"
himmelma@33175
  1636
  def z \<equiv> "y + scaleR k (x - y)"
himmelma@33175
  1637
  have z_def2: "z = x + scaleR (1 - k) (y - x)"
himmelma@33175
  1638
    unfolding z_def by (simp add: algebra_simps)
himmelma@33175
  1639
  have "dist z y < r"
himmelma@33175
  1640
    unfolding z_def k_def using `0 < r`
himmelma@33175
  1641
    by (simp add: dist_norm min_def)
himmelma@33175
  1642
  hence "z \<in> T" using `\<forall>z. dist z y < r \<longrightarrow> z \<in> T` by simp
himmelma@33175
  1643
  have "dist x z < dist x y"
himmelma@33175
  1644
    unfolding z_def2 dist_norm
himmelma@33175
  1645
    apply (simp add: norm_minus_commute)
himmelma@33175
  1646
    apply (simp only: dist_norm [symmetric])
himmelma@33175
  1647
    apply (subgoal_tac "\<bar>1 - k\<bar> * dist x y < 1 * dist x y", simp)
himmelma@33175
  1648
    apply (rule mult_strict_right_mono)
himmelma@33175
  1649
    apply (simp add: k_def divide_pos_pos zero_less_dist_iff `0 < r` `x \<noteq> y`)
himmelma@33175
  1650
    apply (simp add: zero_less_dist_iff `x \<noteq> y`)
himmelma@33175
  1651
    done
himmelma@33175
  1652
  hence "z \<in> ball x (dist x y)" by simp
himmelma@33175
  1653
  have "z \<noteq> y"
himmelma@33175
  1654
    unfolding z_def k_def using `x \<noteq> y` `0 < r`
himmelma@33175
  1655
    by (simp add: min_def)
himmelma@33175
  1656
  show "\<exists>z\<in>ball x (dist x y). z \<in> T \<and> z \<noteq> y"
himmelma@33175
  1657
    using `z \<in> ball x (dist x y)` `z \<in> T` `z \<noteq> y`
himmelma@33175
  1658
    by fast
himmelma@33175
  1659
qed
himmelma@33175
  1660
himmelma@33175
  1661
lemma closure_ball:
himmelma@33175
  1662
  fixes x :: "'a::real_normed_vector"
himmelma@33175
  1663
  shows "0 < e \<Longrightarrow> closure (ball x e) = cball x e"
himmelma@33175
  1664
apply (rule equalityI)
himmelma@33175
  1665
apply (rule closure_minimal)
himmelma@33175
  1666
apply (rule ball_subset_cball)
himmelma@33175
  1667
apply (rule closed_cball)
himmelma@33175
  1668
apply (rule subsetI, rename_tac y)
himmelma@33175
  1669
apply (simp add: le_less [where 'a=real])
himmelma@33175
  1670
apply (erule disjE)
himmelma@33175
  1671
apply (rule subsetD [OF closure_subset], simp)
himmelma@33175
  1672
apply (simp add: closure_def)
himmelma@33175
  1673
apply clarify
himmelma@33175
  1674
apply (rule closure_ball_lemma)
himmelma@33175
  1675
apply (simp add: zero_less_dist_iff)
himmelma@33175
  1676
done
himmelma@33175
  1677
himmelma@33175
  1678
(* In a trivial vector space, this fails for e = 0. *)
himmelma@33175
  1679
lemma interior_cball:
himmelma@33175
  1680
  fixes x :: "'a::{real_normed_vector, perfect_space}"
himmelma@33175
  1681
  shows "interior (cball x e) = ball x e"
himmelma@33175
  1682
proof(cases "e\<ge>0")
himmelma@33175
  1683
  case False note cs = this
himmelma@33175
  1684
  from cs have "ball x e = {}" using ball_empty[of e x] by auto moreover
himmelma@33175
  1685
  { fix y assume "y \<in> cball x e"
himmelma@33175
  1686
    hence False unfolding mem_cball using dist_nz[of x y] cs by auto  }
himmelma@33175
  1687
  hence "cball x e = {}" by auto
himmelma@33175
  1688
  hence "interior (cball x e) = {}" using interior_empty by auto
himmelma@33175
  1689
  ultimately show ?thesis by blast
himmelma@33175
  1690
next
himmelma@33175
  1691
  case True note cs = this
himmelma@33175
  1692
  have "ball x e \<subseteq> cball x e" using ball_subset_cball by auto moreover
himmelma@33175
  1693
  { fix S y assume as: "S \<subseteq> cball x e" "open S" "y\<in>S"
himmelma@33175
  1694
    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
  1695
himmelma@33175
  1696
    then obtain xa where xa_y: "xa \<noteq> y" and xa: "dist xa y < d"
himmelma@33175
  1697
      using perfect_choose_dist [of d] by auto
himmelma@33175
  1698
    have "xa\<in>S" using d[THEN spec[where x=xa]] using xa by(auto simp add: dist_commute)
himmelma@33175
  1699
    hence xa_cball:"xa \<in> cball x e" using as(1) by auto
himmelma@33175
  1700
himmelma@33175
  1701
    hence "y \<in> ball x e" proof(cases "x = y")
himmelma@33175
  1702
      case True
himmelma@33175
  1703
      hence "e>0" using xa_y[unfolded dist_nz] xa_cball[unfolded mem_cball] by (auto simp add: dist_commute)
himmelma@33175
  1704
      thus "y \<in> ball x e" using `x = y ` by simp
himmelma@33175
  1705
    next
himmelma@33175
  1706
      case False
himmelma@33175
  1707
      have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) y < d" unfolding dist_norm
himmelma@33175
  1708
        using `d>0` norm_ge_zero[of "y - x"] `x \<noteq> y` by auto
himmelma@33175
  1709
      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
  1710
      have "y - x \<noteq> 0" using `x \<noteq> y` by auto
himmelma@33175
  1711
      hence **:"d / (2 * norm (y - x)) > 0" unfolding zero_less_norm_iff[THEN sym]
himmelma@33175
  1712
        using `d>0` divide_pos_pos[of d "2*norm (y - x)"] by auto
himmelma@33175
  1713
himmelma@33175
  1714
      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
  1715
        by (auto simp add: dist_norm algebra_simps)
himmelma@33175
  1716
      also have "\<dots> = norm ((1 + d / (2 * norm (y - x))) *\<^sub>R (y - x))"
himmelma@33175
  1717
        by (auto simp add: algebra_simps)
himmelma@33175
  1718
      also have "\<dots> = \<bar>1 + d / (2 * norm (y - x))\<bar> * norm (y - x)"
himmelma@33175
  1719
        using ** by auto
himmelma@33175
  1720
      also have "\<dots> = (dist y x) + d/2"using ** by (auto simp add: left_distrib dist_norm)
himmelma@33175
  1721
      finally have "e \<ge> dist x y +d/2" using *[unfolded mem_cball] by (auto simp add: dist_commute)
himmelma@33175
  1722
      thus "y \<in> ball x e" unfolding mem_ball using `d>0` by auto
himmelma@33175
  1723
    qed  }
himmelma@33175
  1724
  hence "\<forall>S \<subseteq> cball x e. open S \<longrightarrow> S \<subseteq> ball x e" by auto
himmelma@33175
  1725
  ultimately show ?thesis using interior_unique[of "ball x e" "cball x e"] using open_ball[of x e] by auto
himmelma@33175
  1726
qed
himmelma@33175
  1727
himmelma@33175
  1728
lemma frontier_ball:
himmelma@33175
  1729
  fixes a :: "'a::real_normed_vector"
himmelma@33175
  1730
  shows "0 < e ==> frontier(ball a e) = {x. dist a x = e}"
huffman@36358
  1731
  apply (simp add: frontier_def closure_ball interior_open order_less_imp_le)
nipkow@39535
  1732
  apply (simp add: set_eq_iff)
himmelma@33175
  1733
  by arith
himmelma@33175
  1734
himmelma@33175
  1735
lemma frontier_cball:
himmelma@33175
  1736
  fixes a :: "'a::{real_normed_vector, perfect_space}"
himmelma@33175
  1737
  shows "frontier(cball a e) = {x. dist a x = e}"
huffman@36358
  1738
  apply (simp add: frontier_def interior_cball closed_cball order_less_imp_le)
nipkow@39535
  1739
  apply (simp add: set_eq_iff)
himmelma@33175
  1740
  by arith
himmelma@33175
  1741
himmelma@33175
  1742
lemma cball_eq_empty: "(cball x e = {}) \<longleftrightarrow> e < 0"
nipkow@39535
  1743
  apply (simp add: set_eq_iff not_le)
himmelma@33175
  1744
  by (metis zero_le_dist dist_self order_less_le_trans)
himmelma@33175
  1745
lemma cball_empty: "e < 0 ==> cball x e = {}" by (simp add: cball_eq_empty)
himmelma@33175
  1746
himmelma@33175
  1747
lemma cball_eq_sing:
huffman@44943
  1748
  fixes x :: "'a::{metric_space,perfect_space}"
himmelma@33175
  1749
  shows "(cball x e = {x}) \<longleftrightarrow> e = 0"
himmelma@33175
  1750
proof (rule linorder_cases)
himmelma@33175
  1751
  assume e: "0 < e"
himmelma@33175
  1752
  obtain a where "a \<noteq> x" "dist a x < e"
himmelma@33175
  1753
    using perfect_choose_dist [OF e] by auto
himmelma@33175
  1754
  hence "a \<noteq> x" "dist x a \<le> e" by (auto simp add: dist_commute)
nipkow@39535
  1755
  with e show ?thesis by (auto simp add: set_eq_iff)
himmelma@33175
  1756
qed auto
himmelma@33175
  1757
himmelma@33175
  1758
lemma cball_sing:
himmelma@33175
  1759
  fixes x :: "'a::metric_space"
himmelma@33175
  1760
  shows "e = 0 ==> cball x e = {x}"
nipkow@39535
  1761
  by (auto simp add: set_eq_iff)
himmelma@33175
  1762
huffman@45081
  1763
huffman@45081
  1764
subsection {* Boundedness *}
himmelma@33175
  1765
himmelma@33175
  1766
  (* FIXME: This has to be unified with BSEQ!! *)
huffman@45078
  1767
definition (in metric_space)
huffman@45078
  1768
  bounded :: "'a set \<Rightarrow> bool" where
himmelma@33175
  1769
  "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"
himmelma@33175
  1770
himmelma@33175
  1771
lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"
himmelma@33175
  1772
unfolding bounded_def
himmelma@33175
  1773
apply safe
himmelma@33175
  1774
apply (rule_tac x="dist a x + e" in exI, clarify)
himmelma@33175
  1775
apply (drule (1) bspec)
himmelma@33175
  1776
apply (erule order_trans [OF dist_triangle add_left_mono])
himmelma@33175
  1777
apply auto
himmelma@33175
  1778
done
himmelma@33175
  1779
himmelma@33175
  1780
lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"
himmelma@33175
  1781
unfolding bounded_any_center [where a=0]
himmelma@33175
  1782
by (simp add: dist_norm)
himmelma@33175
  1783
himmelma@33175
  1784
lemma bounded_empty[simp]: "bounded {}" by (simp add: bounded_def)
himmelma@33175
  1785
lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T ==> bounded S"
himmelma@33175
  1786
  by (metis bounded_def subset_eq)
himmelma@33175
  1787
himmelma@33175
  1788
lemma bounded_interior[intro]: "bounded S ==> bounded(interior S)"
himmelma@33175
  1789
  by (metis bounded_subset interior_subset)
himmelma@33175
  1790
himmelma@33175
  1791
lemma bounded_closure[intro]: assumes "bounded S" shows "bounded(closure S)"
himmelma@33175
  1792
proof-
himmelma@33175
  1793
  from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a" unfolding bounded_def by auto
himmelma@33175
  1794
  { fix y assume "y \<in> closure S"
himmelma@33175
  1795
    then obtain f where f: "\<forall>n. f n \<in> S"  "(f ---> y) sequentially"
himmelma@33175
  1796
      unfolding closure_sequential by auto
himmelma@33175
  1797
    have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp
himmelma@33175
  1798
    hence "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"
himmelma@33175
  1799
      by (rule eventually_mono, simp add: f(1))
himmelma@33175
  1800
    have "dist x y \<le> a"
himmelma@33175
  1801
      apply (rule Lim_dist_ubound [of sequentially f])
himmelma@33175
  1802
      apply (rule trivial_limit_sequentially)
himmelma@33175
  1803
      apply (rule f(2))
himmelma@33175
  1804
      apply fact
himmelma@33175
  1805
      done
himmelma@33175
  1806
  }
himmelma@33175
  1807
  thus ?thesis unfolding bounded_def by auto
himmelma@33175
  1808
qed
himmelma@33175
  1809
himmelma@33175
  1810
lemma bounded_cball[simp,intro]: "bounded (cball x e)"
himmelma@33175
  1811
  apply (simp add: bounded_def)
himmelma@33175
  1812
  apply (rule_tac x=x in exI)
himmelma@33175
  1813
  apply (rule_tac x=e in exI)
himmelma@33175
  1814
  apply auto
himmelma@33175
  1815
  done
himmelma@33175
  1816
himmelma@33175
  1817
lemma bounded_ball[simp,intro]: "bounded(ball x e)"
himmelma@33175
  1818
  by (metis ball_subset_cball bounded_cball bounded_subset)
himmelma@33175
  1819
huffman@36358
  1820
lemma finite_imp_bounded[intro]:
huffman@36358
  1821
  fixes S :: "'a::metric_space set" assumes "finite S" shows "bounded S"
himmelma@33175
  1822
proof-
huffman@36358
  1823
  { fix a and F :: "'a set" assume as:"bounded F"
himmelma@33175
  1824
    then obtain x e where "\<forall>y\<in>F. dist x y \<le> e" unfolding bounded_def by auto
himmelma@33175
  1825
    hence "\<forall>y\<in>(insert a F). dist x y \<le> max e (dist x a)" by auto
himmelma@33175
  1826
    hence "bounded (insert a F)" unfolding bounded_def by (intro exI)
himmelma@33175
  1827
  }
himmelma@33175
  1828
  thus ?thesis using finite_induct[of S bounded]  using bounded_empty assms by auto
himmelma@33175
  1829
qed
himmelma@33175
  1830
himmelma@33175
  1831
lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"
himmelma@33175
  1832
  apply (auto simp add: bounded_def)
himmelma@33175
  1833
  apply (rename_tac x y r s)
himmelma@33175
  1834
  apply (rule_tac x=x in exI)
himmelma@33175
  1835
  apply (rule_tac x="max r (dist x y + s)" in exI)
himmelma@33175
  1836
  apply (rule ballI, rename_tac z, safe)
himmelma@33175
  1837
  apply (drule (1) bspec, simp)
himmelma@33175
  1838
  apply (drule (1) bspec)
himmelma@33175
  1839
  apply (rule min_max.le_supI2)
himmelma@33175
  1840
  apply (erule order_trans [OF dist_triangle add_left_mono])
himmelma@33175
  1841
  done
himmelma@33175
  1842
himmelma@33175
  1843
lemma bounded_Union[intro]: "finite F \<Longrightarrow> (\<forall>S\<in>F. bounded S) \<Longrightarrow> bounded(\<Union>F)"
himmelma@33175
  1844
  by (induct rule: finite_induct[of F], auto)
himmelma@33175
  1845
himmelma@33175
  1846
lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x <= b)"
himmelma@33175
  1847
  apply (simp add: bounded_iff)
himmelma@33175
  1848
  apply (subgoal_tac "\<And>x (y::real). 0 < 1 + abs y \<and> (x <= y \<longrightarrow> x <= 1 + abs y)")
himmelma@33175
  1849
  by metis arith
himmelma@33175
  1850
himmelma@33175
  1851
lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"
himmelma@33175
  1852
  by (metis Int_lower1 Int_lower2 bounded_subset)
himmelma@33175
  1853
himmelma@33175
  1854
lemma bounded_diff[intro]: "bounded S ==> bounded (S - T)"
himmelma@33175
  1855
apply (metis Diff_subset bounded_subset)
himmelma@33175
  1856
done
himmelma@33175
  1857
himmelma@33175
  1858
lemma bounded_insert[intro]:"bounded(insert x S) \<longleftrightarrow> bounded S"
himmelma@33175
  1859
  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
  1860
himmelma@33175
  1861
lemma not_bounded_UNIV[simp, intro]:
himmelma@33175
  1862
  "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"
himmelma@33175
  1863
proof(auto simp add: bounded_pos not_le)
himmelma@33175
  1864
  obtain x :: 'a where "x \<noteq> 0"
himmelma@33175
  1865
    using perfect_choose_dist [OF zero_less_one] by fast
himmelma@33175
  1866
  fix b::real  assume b: "b >0"
himmelma@33175
  1867
  have b1: "b +1 \<ge> 0" using b by simp
himmelma@33175
  1868
  with `x \<noteq> 0` have "b < norm (scaleR (b + 1) (sgn x))"
himmelma@33175
  1869
    by (simp add: norm_sgn)
himmelma@33175
  1870
  then show "\<exists>x::'a. b < norm x" ..
himmelma@33175
  1871
qed
himmelma@33175
  1872
himmelma@33175
  1873
lemma bounded_linear_image:
himmelma@33175
  1874
  assumes "bounded S" "bounded_linear f"
himmelma@33175
  1875
  shows "bounded(f ` S)"
himmelma@33175
  1876
proof-
himmelma@33175
  1877
  from assms(1) obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto
himmelma@33175
  1878
  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
  1879
  { fix x assume "x\<in>S"
himmelma@33175
  1880
    hence "norm x \<le> b" using b by auto
himmelma@33175
  1881
    hence "norm (f x) \<le> B * b" using B(2) apply(erule_tac x=x in allE)
huffman@36770
  1882
      by (metis B(1) B(2) order_trans mult_le_cancel_left_pos)
himmelma@33175
  1883
  }
himmelma@33175
  1884
  thus ?thesis unfolding bounded_pos apply(rule_tac x="b*B" in exI)
huffman@36770
  1885
    using b B mult_pos_pos [of b B] by (auto simp add: mult_commute)
himmelma@33175
  1886
qed
himmelma@33175
  1887
himmelma@33175
  1888
lemma bounded_scaling:
himmelma@33175
  1889
  fixes S :: "'a::real_normed_vector set"
himmelma@33175
  1890
  shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x) ` S)"
himmelma@33175
  1891
  apply (rule bounded_linear_image, assumption)
huffman@45145
  1892
  apply (rule bounded_linear_scaleR_right)
himmelma@33175
  1893
  done
himmelma@33175
  1894
himmelma@33175
  1895
lemma bounded_translation:
himmelma@33175
  1896
  fixes S :: "'a::real_normed_vector set"
himmelma@33175
  1897
  assumes "bounded S" shows "bounded ((\<lambda>x. a + x) ` S)"
himmelma@33175
  1898
proof-
himmelma@33175
  1899
  from assms obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto
himmelma@33175
  1900
  { fix x assume "x\<in>S"
himmelma@33175
  1901
    hence "norm (a + x) \<le> b + norm a" using norm_triangle_ineq[of a x] b by auto
himmelma@33175
  1902
  }
himmelma@33175
  1903
  thus ?thesis unfolding bounded_pos using norm_ge_zero[of a] b(1) using add_strict_increasing[of b 0 "norm a"]
himmelma@33175
  1904
    by (auto intro!: add exI[of _ "b + norm a"])
himmelma@33175
  1905
qed
himmelma@33175
  1906
himmelma@33175
  1907
himmelma@33175
  1908
text{* Some theorems on sups and infs using the notion "bounded". *}
himmelma@33175
  1909
himmelma@33175
  1910
lemma bounded_real:
himmelma@33175
  1911
  fixes S :: "real set"
himmelma@33175
  1912
  shows "bounded S \<longleftrightarrow>  (\<exists>a. \<forall>x\<in>S. abs x <= a)"
himmelma@33175
  1913
  by (simp add: bounded_iff)
himmelma@33175
  1914
paulson@33270
  1915
lemma bounded_has_Sup:
paulson@33270
  1916
  fixes S :: "real set"
paulson@33270
  1917
  assumes "bounded S" "S \<noteq> {}"
paulson@33270
  1918
  shows "\<forall>x\<in>S. x <= Sup S" and "\<forall>b. (\<forall>x\<in>S. x <= b) \<longrightarrow> Sup S <= b"
paulson@33270
  1919
proof
paulson@33270
  1920
  fix x assume "x\<in>S"
paulson@33270
  1921
  thus "x \<le> Sup S"
paulson@33270
  1922
    by (metis SupInf.Sup_upper abs_le_D1 assms(1) bounded_real)
paulson@33270
  1923
next
paulson@33270
  1924
  show "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b" using assms
paulson@33270
  1925
    by (metis SupInf.Sup_least)
paulson@33270
  1926
qed
paulson@33270
  1927
paulson@33270
  1928
lemma Sup_insert:
paulson@33270
  1929
  fixes S :: "real set"
paulson@33270
  1930
  shows "bounded S ==> Sup(insert x S) = (if S = {} then x else max x (Sup S))" 
paulson@33270
  1931
by auto (metis Int_absorb Sup_insert_nonempty assms bounded_has_Sup(1) disjoint_iff_not_equal) 
paulson@33270
  1932
paulson@33270
  1933
lemma Sup_insert_finite:
paulson@33270
  1934
  fixes S :: "real set"
paulson@33270
  1935
  shows "finite S \<Longrightarrow> Sup(insert x S) = (if S = {} then x else max x (Sup S))"
paulson@33270
  1936
  apply (rule Sup_insert)
paulson@33270
  1937
  apply (rule finite_imp_bounded)
paulson@33270
  1938
  by simp
paulson@33270
  1939
paulson@33270
  1940
lemma bounded_has_Inf:
paulson@33270
  1941
  fixes S :: "real set"
paulson@33270
  1942
  assumes "bounded S"  "S \<noteq> {}"
paulson@33270
  1943
  shows "\<forall>x\<in>S. x >= Inf S" and "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S >= b"
himmelma@33175
  1944
proof
himmelma@33175
  1945
  fix x assume "x\<in>S"
himmelma@33175
  1946
  from assms(1) obtain a where a:"\<forall>x\<in>S. \<bar>x\<bar> \<le> a" unfolding bounded_real by auto
paulson@33270
  1947
  thus "x \<ge> Inf S" using `x\<in>S`
paulson@33270
  1948
    by (metis Inf_lower_EX abs_le_D2 minus_le_iff)
himmelma@33175
  1949
next
paulson@33270
  1950
  show "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S \<ge> b" using assms
paulson@33270
  1951
    by (metis SupInf.Inf_greatest)
paulson@33270
  1952
qed
paulson@33270
  1953
paulson@33270
  1954
lemma Inf_insert:
paulson@33270
  1955
  fixes S :: "real set"
paulson@33270
  1956
  shows "bounded S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))" 
paulson@33270
  1957
by auto (metis Int_absorb Inf_insert_nonempty bounded_has_Inf(1) disjoint_iff_not_equal) 
paulson@33270
  1958
lemma Inf_insert_finite:
paulson@33270
  1959
  fixes S :: "real set"
paulson@33270
  1960
  shows "finite S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))"
paulson@33270
  1961
  by (rule Inf_insert, rule finite_imp_bounded, simp)
paulson@33270
  1962
himmelma@33175
  1963
(* TODO: Move this to RComplete.thy -- would need to include Glb into RComplete *)
himmelma@33175
  1964
lemma real_isGlb_unique: "[| isGlb R S x; isGlb R S y |] ==> x = (y::real)"
himmelma@33175
  1965
  apply (frule isGlb_isLb)
himmelma@33175
  1966
  apply (frule_tac x = y in isGlb_isLb)
himmelma@33175
  1967
  apply (blast intro!: order_antisym dest!: isGlb_le_isLb)
himmelma@33175
  1968
  done
himmelma@33175
  1969
huffman@45081
  1970
huffman@36433
  1971
subsection {* Equivalent versions of compactness *}
huffman@36433
  1972
huffman@36433
  1973
subsubsection{* Sequential compactness *}
himmelma@33175
  1974
himmelma@33175
  1975
definition
himmelma@33175
  1976
  compact :: "'a::metric_space set \<Rightarrow> bool" where (* TODO: generalize *)
himmelma@33175
  1977
  "compact S \<longleftrightarrow>
himmelma@33175
  1978
   (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>
himmelma@33175
  1979
       (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially))"
himmelma@33175
  1980
huffman@44946
  1981
lemma compactI:
huffman@44946
  1982
  assumes "\<And>f. \<forall>n. f n \<in> S \<Longrightarrow> \<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially"
huffman@44946
  1983
  shows "compact S"
huffman@44946
  1984
  unfolding compact_def using assms by fast
huffman@44946
  1985
huffman@44946
  1986
lemma compactE:
huffman@44946
  1987
  assumes "compact S" "\<forall>n. f n \<in> S"
huffman@44946
  1988
  obtains l r where "l \<in> S" "subseq r" "((f \<circ> r) ---> l) sequentially"
huffman@44946
  1989
  using assms unfolding compact_def by fast
huffman@44946
  1990
himmelma@33175
  1991
text {*
himmelma@33175
  1992
  A metric space (or topological vector space) is said to have the
himmelma@33175
  1993
  Heine-Borel property if every closed and bounded subset is compact.
himmelma@33175
  1994
*}
himmelma@33175
  1995
huffman@45078
  1996
class heine_borel = metric_space +
himmelma@33175
  1997
  assumes bounded_imp_convergent_subsequence:
himmelma@33175
  1998
    "bounded s \<Longrightarrow> \<forall>n. f n \<in> s
himmelma@33175
  1999
      \<Longrightarrow> \<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2000
himmelma@33175
  2001
lemma bounded_closed_imp_compact:
himmelma@33175
  2002
  fixes s::"'a::heine_borel set"
himmelma@33175
  2003
  assumes "bounded s" and "closed s" shows "compact s"
himmelma@33175
  2004
proof (unfold compact_def, clarify)
himmelma@33175
  2005
  fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
himmelma@33175
  2006
  obtain l r where r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2007
    using bounded_imp_convergent_subsequence [OF `bounded s` `\<forall>n. f n \<in> s`] by auto
himmelma@33175
  2008
  from f have fr: "\<forall>n. (f \<circ> r) n \<in> s" by simp
himmelma@33175
  2009
  have "l \<in> s" using `closed s` fr l
himmelma@33175
  2010
    unfolding closed_sequential_limits by blast
himmelma@33175
  2011
  show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2012
    using `l \<in> s` r l by blast
himmelma@33175
  2013
qed
himmelma@33175
  2014
himmelma@33175
  2015
lemma subseq_bigger: assumes "subseq r" shows "n \<le> r n"
himmelma@33175
  2016
proof(induct n)
himmelma@33175
  2017
  show "0 \<le> r 0" by auto
himmelma@33175
  2018
next
himmelma@33175
  2019
  fix n assume "n \<le> r n"
himmelma@33175
  2020
  moreover have "r n < r (Suc n)"
himmelma@33175
  2021
    using assms [unfolded subseq_def] by auto
himmelma@33175
  2022
  ultimately show "Suc n \<le> r (Suc n)" by auto
himmelma@33175
  2023
qed
himmelma@33175
  2024
himmelma@33175
  2025
lemma eventually_subseq:
himmelma@33175
  2026
  assumes r: "subseq r"
himmelma@33175
  2027
  shows "eventually P sequentially \<Longrightarrow> eventually (\<lambda>n. P (r n)) sequentially"
himmelma@33175
  2028
unfolding eventually_sequentially
himmelma@33175
  2029
by (metis subseq_bigger [OF r] le_trans)
himmelma@33175
  2030
himmelma@33175
  2031
lemma lim_subseq:
himmelma@33175
  2032
  "subseq r \<Longrightarrow> (s ---> l) sequentially \<Longrightarrow> ((s o r) ---> l) sequentially"
himmelma@33175
  2033
unfolding tendsto_def eventually_sequentially o_def
himmelma@33175
  2034
by (metis subseq_bigger le_trans)
himmelma@33175
  2035
himmelma@33175
  2036
lemma num_Axiom: "EX! g. g 0 = e \<and> (\<forall>n. g (Suc n) = f n (g n))"
himmelma@33175
  2037
  unfolding Ex1_def
himmelma@33175
  2038
  apply (rule_tac x="nat_rec e f" in exI)
himmelma@33175
  2039
  apply (rule conjI)+
himmelma@33175
  2040
apply (rule def_nat_rec_0, simp)
himmelma@33175
  2041
apply (rule allI, rule def_nat_rec_Suc, simp)
himmelma@33175
  2042
apply (rule allI, rule impI, rule ext)
himmelma@33175
  2043
apply (erule conjE)
himmelma@33175
  2044
apply (induct_tac x)
huffman@36358
  2045
apply simp
himmelma@33175
  2046
apply (erule_tac x="n" in allE)
himmelma@33175
  2047
apply (simp)
himmelma@33175
  2048
done
himmelma@33175
  2049
himmelma@33175
  2050
lemma convergent_bounded_increasing: fixes s ::"nat\<Rightarrow>real"
himmelma@33175
  2051
  assumes "incseq s" and "\<forall>n. abs(s n) \<le> b"
himmelma@33175
  2052
  shows "\<exists> l. \<forall>e::real>0. \<exists> N. \<forall>n \<ge> N.  abs(s n - l) < e"
himmelma@33175
  2053
proof-
himmelma@33175
  2054
  have "isUb UNIV (range s) b" using assms(2) and abs_le_D1 unfolding isUb_def and setle_def by auto
himmelma@33175
  2055
  then obtain t where t:"isLub UNIV (range s) t" using reals_complete[of "range s" ] by auto
himmelma@33175
  2056
  { fix e::real assume "e>0" and as:"\<forall>N. \<exists>n\<ge>N. \<not> \<bar>s n - t\<bar> < e"
himmelma@33175
  2057
    { fix n::nat
himmelma@33175
  2058
      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
  2059
      have "t \<ge> s N" using isLub_isUb[OF t, unfolded isUb_def setle_def] by auto
himmelma@33175
  2060
      with n have "s N \<le> t - e" using `e>0` by auto
himmelma@33175
  2061
      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
  2062
    hence "isUb UNIV (range s) (t - e)" unfolding isUb_def and setle_def by auto
himmelma@33175
  2063
    hence False using isLub_le_isUb[OF t, of "t - e"] and `e>0` by auto  }
himmelma@33175
  2064
  thus ?thesis by blast
himmelma@33175
  2065
qed
himmelma@33175
  2066
himmelma@33175
  2067
lemma convergent_bounded_monotone: fixes s::"nat \<Rightarrow> real"
himmelma@33175
  2068
  assumes "\<forall>n. abs(s n) \<le> b" and "monoseq s"
himmelma@33175
  2069
  shows "\<exists>l. \<forall>e::real>0. \<exists>N. \<forall>n\<ge>N. abs(s n - l) < e"
himmelma@33175
  2070
  using convergent_bounded_increasing[of s b] assms using convergent_bounded_increasing[of "\<lambda>n. - s n" b]
himmelma@33175
  2071
  unfolding monoseq_def incseq_def
himmelma@33175
  2072
  apply auto unfolding minus_add_distrib[THEN sym, unfolded diff_minus[THEN sym]]
himmelma@33175
  2073
  unfolding abs_minus_cancel by(rule_tac x="-l" in exI)auto
himmelma@33175
  2074
hoelzl@37489
  2075
(* TODO: merge this lemma with the ones above *)
hoelzl@37489
  2076
lemma bounded_increasing_convergent: fixes s::"nat \<Rightarrow> real"
hoelzl@37489
  2077
  assumes "bounded {s n| n::nat. True}"  "\<forall>n. (s n) \<le>(s(Suc n))"
hoelzl@37489
  2078
  shows "\<exists>l. (s ---> l) sequentially"
hoelzl@37489
  2079
proof-
hoelzl@37489
  2080
  obtain a where a:"\<forall>n. \<bar> (s n)\<bar> \<le>  a" using assms(1)[unfolded bounded_iff] by auto
hoelzl@37489
  2081
  { fix m::nat
hoelzl@37489
  2082
    have "\<And> n. n\<ge>m \<longrightarrow>  (s m) \<le> (s n)"
hoelzl@37489
  2083
      apply(induct_tac n) apply simp using assms(2) apply(erule_tac x="na" in allE)
hoelzl@37489
  2084
      apply(case_tac "m \<le> na") unfolding not_less_eq_eq by(auto simp add: not_less_eq_eq)  }
hoelzl@37489
  2085
  hence "\<forall>m n. m \<le> n \<longrightarrow> (s m) \<le> (s n)" by auto
hoelzl@37489
  2086
  then obtain l where "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<bar> (s n) - l\<bar> < e" using convergent_bounded_monotone[OF a]
hoelzl@37489
  2087
    unfolding monoseq_def by auto
hoelzl@37489
  2088
  thus ?thesis unfolding Lim_sequentially apply(rule_tac x="l" in exI)
hoelzl@37489
  2089
    unfolding dist_norm  by auto
hoelzl@37489
  2090
qed
hoelzl@37489
  2091
himmelma@33175
  2092
lemma compact_real_lemma:
himmelma@33175
  2093
  assumes "\<forall>n::nat. abs(s n) \<le> b"
himmelma@33175
  2094
  shows "\<exists>(l::real) r. subseq r \<and> ((s \<circ> r) ---> l) sequentially"
himmelma@33175
  2095
proof-
himmelma@33175
  2096
  obtain r where r:"subseq r" "monoseq (\<lambda>n. s (r n))"
himmelma@33175
  2097
    using seq_monosub[of s] by auto
himmelma@33175
  2098
  thus ?thesis using convergent_bounded_monotone[of "\<lambda>n. s (r n)" b] and assms
himmelma@33175
  2099
    unfolding tendsto_iff dist_norm eventually_sequentially by auto
himmelma@33175
  2100
qed
himmelma@33175
  2101
himmelma@33175
  2102
instance real :: heine_borel
himmelma@33175
  2103
proof
himmelma@33175
  2104
  fix s :: "real set" and f :: "nat \<Rightarrow> real"
himmelma@33175
  2105
  assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
himmelma@33175
  2106
  then obtain b where b: "\<forall>n. abs (f n) \<le> b"
himmelma@33175
  2107
    unfolding bounded_iff by auto
himmelma@33175
  2108
  obtain l :: real and r :: "nat \<Rightarrow> nat" where
himmelma@33175
  2109
    r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2110
    using compact_real_lemma [OF b] by auto
himmelma@33175
  2111
  thus "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2112
    by auto
himmelma@33175
  2113
qed
himmelma@33175
  2114
huffman@45009
  2115
lemma bounded_component: "bounded s \<Longrightarrow> bounded ((\<lambda>x. x $$ i) ` s)"
huffman@45009
  2116
  apply (erule bounded_linear_image)
huffman@45009
  2117
  apply (rule bounded_linear_euclidean_component)
huffman@45009
  2118
  done
himmelma@33175
  2119
himmelma@33175
  2120
lemma compact_lemma:
hoelzl@37489
  2121
  fixes f :: "nat \<Rightarrow> 'a::euclidean_space"
himmelma@33175
  2122
  assumes "bounded s" and "\<forall>n. f n \<in> s"
hoelzl@37489
  2123
  shows "\<forall>d. \<exists>l::'a. \<exists> r. subseq r \<and>
hoelzl@37489
  2124
        (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) $$ i) (l $$ i) < e) sequentially)"
himmelma@33175
  2125
proof
hoelzl@37489
  2126
  fix d'::"nat set" def d \<equiv> "d' \<inter> {..<DIM('a)}"
hoelzl@37489
  2127
  have "finite d" "d\<subseteq>{..<DIM('a)}" unfolding d_def by auto
hoelzl@37489
  2128
  hence "\<exists>l::'a. \<exists>r. subseq r \<and>
hoelzl@37489
  2129
      (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) $$ i) (l $$ i) < e) sequentially)"
himmelma@33175
  2130
  proof(induct d) case empty thus ?case unfolding subseq_def by auto
hoelzl@37489
  2131
  next case (insert k d) have k[intro]:"k<DIM('a)" using insert by auto
hoelzl@37489
  2132
    have s': "bounded ((\<lambda>x. x $$ k) ` s)" using `bounded s` by (rule bounded_component)
hoelzl@37489
  2133
    obtain l1::"'a" and r1 where r1:"subseq r1" and
hoelzl@37489
  2134
      lr1:"\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) $$ i) (l1 $$ i) < e) sequentially"
hoelzl@37489
  2135
      using insert(3) using insert(4) by auto
hoelzl@37489
  2136
    have f': "\<forall>n. f (r1 n) $$ k \<in> (\<lambda>x. x $$ k) ` s" using `\<forall>n. f n \<in> s` by simp
hoelzl@37489
  2137
    obtain l2 r2 where r2:"subseq r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) $$ k) ---> l2) sequentially"
himmelma@33175
  2138
      using bounded_imp_convergent_subsequence[OF s' f'] unfolding o_def by auto
himmelma@33175
  2139
    def r \<equiv> "r1 \<circ> r2" have r:"subseq r"
himmelma@33175
  2140
      using r1 and r2 unfolding r_def o_def subseq_def by auto
himmelma@33175
  2141
    moreover
hoelzl@37489
  2142
    def l \<equiv> "(\<chi>\<chi> i. if i = k then l2 else l1$$i)::'a"
himmelma@33175
  2143
    { fix e::real assume "e>0"
hoelzl@37489
  2144
      from lr1 `e>0` have N1:"eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) $$ i) (l1 $$ i) < e) sequentially" by blast
hoelzl@37489
  2145
      from lr2 `e>0` have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) $$ k) l2 < e) sequentially" by (rule tendstoD)
hoelzl@37489
  2146
      from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) $$ i) (l1 $$ i) < e) sequentially"
himmelma@33175
  2147
        by (rule eventually_subseq)
hoelzl@37489
  2148
      have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) $$ i) (l $$ i) < e) sequentially"
hoelzl@37489
  2149
        using N1' N2 apply(rule eventually_elim2) unfolding l_def r_def o_def
hoelzl@37489
  2150
        using insert.prems by auto
himmelma@33175
  2151
    }
himmelma@33175
  2152
    ultimately show ?case by auto
himmelma@33175
  2153
  qed
hoelzl@37489
  2154
  thus "\<exists>l::'a. \<exists>r. subseq r \<and>
hoelzl@37489
  2155
      (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d'. dist (f (r n) $$ i) (l $$ i) < e) sequentially)"
hoelzl@37489
  2156
    apply safe apply(rule_tac x=l in exI,rule_tac x=r in exI) apply safe
hoelzl@37489
  2157
    apply(erule_tac x=e in allE) unfolding d_def eventually_sequentially apply safe 
hoelzl@37489
  2158
    apply(rule_tac x=N in exI) apply safe apply(erule_tac x=n in allE,safe)
hoelzl@37489
  2159
    apply(erule_tac x=i in ballE) 
hoelzl@37489
  2160
  proof- fix i and r::"nat=>nat" and n::nat and e::real and l::'a
hoelzl@37489
  2161
    assume "i\<in>d'" "i \<notin> d' \<inter> {..<DIM('a)}" and e:"e>0"
hoelzl@37489
  2162
    hence *:"i\<ge>DIM('a)" by auto
hoelzl@37489
  2163
    thus "dist (f (r n) $$ i) (l $$ i) < e" using e by auto
hoelzl@37489
  2164
  qed
hoelzl@37489
  2165
qed
hoelzl@37489
  2166
hoelzl@37489
  2167
instance euclidean_space \<subseteq> heine_borel
himmelma@33175
  2168
proof
hoelzl@37489
  2169
  fix s :: "'a set" and f :: "nat \<Rightarrow> 'a"
himmelma@33175
  2170
  assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
hoelzl@37489
  2171
  then obtain l::'a and r where r: "subseq r"
hoelzl@37489
  2172
    and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>UNIV. dist (f (r n) $$ i) (l $$ i) < e) sequentially"
himmelma@33175
  2173
    using compact_lemma [OF s f] by blast
hoelzl@37489
  2174
  let ?d = "{..<DIM('a)}"
himmelma@33175
  2175
  { fix e::real assume "e>0"
himmelma@33175
  2176
    hence "0 < e / (real_of_nat (card ?d))"
hoelzl@37489
  2177
      using DIM_positive using divide_pos_pos[of e, of "real_of_nat (card ?d)"] by auto
hoelzl@37489
  2178
    with l have "eventually (\<lambda>n. \<forall>i. dist (f (r n) $$ i) (l $$ i) < e / (real_of_nat (card ?d))) sequentially"
himmelma@33175
  2179
      by simp
himmelma@33175
  2180
    moreover
hoelzl@37489
  2181
    { fix n assume n: "\<forall>i. dist (f (r n) $$ i) (l $$ i) < e / (real_of_nat (card ?d))"
hoelzl@37489
  2182
      have "dist (f (r n)) l \<le> (\<Sum>i\<in>?d. dist (f (r n) $$ i) (l $$ i))"
hoelzl@37489
  2183
        apply(subst euclidean_dist_l2) using zero_le_dist by (rule setL2_le_setsum)
himmelma@33175
  2184
      also have "\<dots> < (\<Sum>i\<in>?d. e / (real_of_nat (card ?d)))"
hoelzl@37489
  2185
        apply(rule setsum_strict_mono) using n by auto
hoelzl@37489
  2186
      finally have "dist (f (r n)) l < e" unfolding setsum_constant
hoelzl@37489
  2187
        using DIM_positive[where 'a='a] by auto
himmelma@33175
  2188
    }
himmelma@33175
  2189
    ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"
himmelma@33175
  2190
      by (rule eventually_elim1)
himmelma@33175
  2191
  }
himmelma@33175
  2192
  hence *:"((f \<circ> r) ---> l) sequentially" unfolding o_def tendsto_iff by simp
himmelma@33175
  2193
  with r show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" by auto
himmelma@33175
  2194
qed
himmelma@33175
  2195
himmelma@33175
  2196
lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst ` s)"
himmelma@33175
  2197
unfolding bounded_def
himmelma@33175
  2198
apply clarify
himmelma@33175
  2199
apply (rule_tac x="a" in exI)
himmelma@33175
  2200
apply (rule_tac x="e" in exI)
himmelma@33175
  2201
apply clarsimp
himmelma@33175
  2202
apply (drule (1) bspec)
himmelma@33175
  2203
apply (simp add: dist_Pair_Pair)
himmelma@33175
  2204
apply (erule order_trans [OF real_sqrt_sum_squares_ge1])
himmelma@33175
  2205
done
himmelma@33175
  2206
himmelma@33175
  2207
lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd ` s)"
himmelma@33175
  2208
unfolding bounded_def
himmelma@33175
  2209
apply clarify
himmelma@33175
  2210
apply (rule_tac x="b" in exI)
himmelma@33175
  2211
apply (rule_tac x="e" in exI)
himmelma@33175
  2212
apply clarsimp
himmelma@33175
  2213
apply (drule (1) bspec)
himmelma@33175
  2214
apply (simp add: dist_Pair_Pair)
himmelma@33175
  2215
apply (erule order_trans [OF real_sqrt_sum_squares_ge2])
himmelma@33175
  2216
done
himmelma@33175
  2217
haftmann@37678
  2218
instance prod :: (heine_borel, heine_borel) heine_borel
himmelma@33175
  2219
proof
himmelma@33175
  2220
  fix s :: "('a * 'b) set" and f :: "nat \<Rightarrow> 'a * 'b"
himmelma@33175
  2221
  assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
himmelma@33175
  2222
  from s have s1: "bounded (fst ` s)" by (rule bounded_fst)
himmelma@33175
  2223
  from f have f1: "\<forall>n. fst (f n) \<in> fst ` s" by simp
himmelma@33175
  2224
  obtain l1 r1 where r1: "subseq r1"
himmelma@33175
  2225
    and l1: "((\<lambda>n. fst (f (r1 n))) ---> l1) sequentially"
himmelma@33175
  2226
    using bounded_imp_convergent_subsequence [OF s1 f1]
himmelma@33175
  2227
    unfolding o_def by fast
himmelma@33175
  2228
  from s have s2: "bounded (snd ` s)" by (rule bounded_snd)
himmelma@33175
  2229
  from f have f2: "\<forall>n. snd (f (r1 n)) \<in> snd ` s" by simp
himmelma@33175
  2230
  obtain l2 r2 where r2: "subseq r2"
himmelma@33175
  2231
    and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) ---> l2) sequentially"
himmelma@33175
  2232
    using bounded_imp_convergent_subsequence [OF s2 f2]
himmelma@33175
  2233
    unfolding o_def by fast
himmelma@33175
  2234
  have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) ---> l1) sequentially"
himmelma@33175
  2235
    using lim_subseq [OF r2 l1] unfolding o_def .
himmelma@33175
  2236
  have l: "((f \<circ> (r1 \<circ> r2)) ---> (l1, l2)) sequentially"
himmelma@33175
  2237
    using tendsto_Pair [OF l1' l2] unfolding o_def by simp
himmelma@33175
  2238
  have r: "subseq (r1 \<circ> r2)"
himmelma@33175
  2239
    using r1 r2 unfolding subseq_def by simp
himmelma@33175
  2240
  show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2241
    using l r by fast
himmelma@33175
  2242
qed
himmelma@33175
  2243
huffman@36433
  2244
subsubsection{* Completeness *}
himmelma@33175
  2245
himmelma@33175
  2246
lemma cauchy_def:
himmelma@33175
  2247
  "Cauchy s \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m n. m \<ge> N \<and> n \<ge> N --> dist(s m)(s n) < e)"
himmelma@33175
  2248
unfolding Cauchy_def by blast
himmelma@33175
  2249
himmelma@33175
  2250
definition
himmelma@33175
  2251
  complete :: "'a::metric_space set \<Rightarrow> bool" where
himmelma@33175
  2252
  "complete s \<longleftrightarrow> (\<forall>f. (\<forall>n. f n \<in> s) \<and> Cauchy f
himmelma@33175
  2253
                      --> (\<exists>l \<in> s. (f ---> l) sequentially))"
himmelma@33175
  2254
himmelma@33175
  2255
lemma cauchy: "Cauchy s \<longleftrightarrow> (\<forall>e>0.\<exists> N::nat. \<forall>n\<ge>N. dist(s n)(s N) < e)" (is "?lhs = ?rhs")
himmelma@33175
  2256
proof-
himmelma@33175
  2257
  { assume ?rhs
himmelma@33175
  2258
    { fix e::real
himmelma@33175
  2259
      assume "e>0"
himmelma@33175
  2260
      with `?rhs` obtain N where N:"\<forall>n\<ge>N. dist (s n) (s N) < e/2"
himmelma@33175
  2261
        by (erule_tac x="e/2" in allE) auto
himmelma@33175
  2262
      { fix n m
himmelma@33175
  2263
        assume nm:"N \<le> m \<and> N \<le> n"
himmelma@33175
  2264
        hence "dist (s m) (s n) < e" using N
himmelma@33175
  2265
          using dist_triangle_half_l[of "s m" "s N" "e" "s n"]
himmelma@33175
  2266
          by blast
himmelma@33175
  2267
      }
himmelma@33175
  2268
      hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < e"
himmelma@33175
  2269
        by blast
himmelma@33175
  2270
    }
himmelma@33175
  2271
    hence ?lhs
himmelma@33175
  2272
      unfolding cauchy_def
himmelma@33175
  2273
      by blast
himmelma@33175
  2274
  }
himmelma@33175
  2275
  thus ?thesis
himmelma@33175
  2276
    unfolding cauchy_def
himmelma@33175
  2277
    using dist_triangle_half_l
himmelma@33175
  2278
    by blast
himmelma@33175
  2279
qed
himmelma@33175
  2280
himmelma@33175
  2281
lemma convergent_imp_cauchy:
himmelma@33175
  2282
 "(s ---> l) sequentially ==> Cauchy s"
himmelma@33175
  2283
proof(simp only: cauchy_def, rule, rule)
himmelma@33175
  2284
  fix e::real assume "e>0" "(s ---> l) sequentially"
himmelma@33175
  2285
  then obtain N::nat where N:"\<forall>n\<ge>N. dist (s n) l < e/2" unfolding Lim_sequentially by(erule_tac x="e/2" in allE) auto
himmelma@33175
  2286
  thus "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < e"  using dist_triangle_half_l[of _ l e _] by (rule_tac x=N in exI) auto
himmelma@33175
  2287
qed
himmelma@33175
  2288
huffman@34098
  2289
lemma cauchy_imp_bounded: assumes "Cauchy s" shows "bounded (range s)"
himmelma@33175
  2290
proof-
himmelma@33175
  2291
  from assms obtain N::nat where "\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < 1" unfolding cauchy_def apply(erule_tac x= 1 in allE) by auto
himmelma@33175
  2292
  hence N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto
himmelma@33175
  2293
  moreover
himmelma@33175
  2294
  have "bounded (s ` {0..N})" using finite_imp_bounded[of "s ` {1..N}"] by auto
himmelma@33175
  2295
  then obtain a where a:"\<forall>x\<in>s ` {0..N}. dist (s N) x \<le> a"
himmelma@33175
  2296
    unfolding bounded_any_center [where a="s N"] by auto
himmelma@33175
  2297
  ultimately show "?thesis"
himmelma@33175
  2298
    unfolding bounded_any_center [where a="s N"]
himmelma@33175
  2299
    apply(rule_tac x="max a 1" in exI) apply auto
huffman@34098
  2300
    apply(erule_tac x=y in allE) apply(erule_tac x=y in ballE) by auto
himmelma@33175
  2301
qed
himmelma@33175
  2302
himmelma@33175
  2303
lemma compact_imp_complete: assumes "compact s" shows "complete s"
himmelma@33175
  2304
proof-
himmelma@33175
  2305
  { fix f assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"
himmelma@33175
  2306
    from as(1) obtain l r where lr: "l\<in>s" "subseq r" "((f \<circ> r) ---> l) sequentially" using assms unfolding compact_def by blast
himmelma@33175
  2307
himmelma@33175
  2308
    note lr' = subseq_bigger [OF lr(2)]
himmelma@33175
  2309
himmelma@33175
  2310
    { fix e::real assume "e>0"
himmelma@33175
  2311
      from as(2) obtain N where N:"\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (f m) (f n) < e/2" unfolding cauchy_def using `e>0` apply (erule_tac x="e/2" in allE) by auto
himmelma@33175
  2312
      from lr(3)[unfolded Lim_sequentially, THEN spec[where x="e/2"]] obtain M where M:"\<forall>n\<ge>M. dist ((f \<circ> r) n) l < e/2" using `e>0` by auto
himmelma@33175
  2313
      { fix n::nat assume n:"n \<ge> max N M"
himmelma@33175
  2314
        have "dist ((f \<circ> r) n) l < e/2" using n M by auto
himmelma@33175
  2315
        moreover have "r n \<ge> N" using lr'[of n] n by auto
himmelma@33175
  2316
        hence "dist (f n) ((f \<circ> r) n) < e / 2" using N using n by auto
himmelma@33175
  2317
        ultimately have "dist (f n) l < e" using dist_triangle_half_r[of "f (r n)" "f n" e l] by (auto simp add: dist_commute)  }
himmelma@33175
  2318
      hence "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast  }
himmelma@33175
  2319
    hence "\<exists>l\<in>s. (f ---> l) sequentially" using `l\<in>s` unfolding Lim_sequentially by auto  }
himmelma@33175
  2320
  thus ?thesis unfolding complete_def by auto
himmelma@33175
  2321
qed
himmelma@33175
  2322
himmelma@33175
  2323
instance heine_borel < complete_space
himmelma@33175
  2324
proof
himmelma@33175
  2325
  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
huffman@34098
  2326
  hence "bounded (range f)"
huffman@34098
  2327
    by (rule cauchy_imp_bounded)
himmelma@33175
  2328
  hence "compact (closure (range f))"
himmelma@33175
  2329
    using bounded_closed_imp_compact [of "closure (range f)"] by auto
himmelma@33175
  2330
  hence "complete (closure (range f))"
huffman@34098
  2331
    by (rule compact_imp_complete)
himmelma@33175
  2332
  moreover have "\<forall>n. f n \<in> closure (range f)"
himmelma@33175
  2333
    using closure_subset [of "range f"] by auto
himmelma@33175
  2334
  ultimately have "\<exists>l\<in>closure (range f). (f ---> l) sequentially"
himmelma@33175
  2335
    using `Cauchy f` unfolding complete_def by auto
himmelma@33175
  2336
  then show "convergent f"
huffman@36660
  2337
    unfolding convergent_def by auto
himmelma@33175
  2338
qed
himmelma@33175
  2339
himmelma@33175
  2340
lemma complete_univ: "complete (UNIV :: 'a::complete_space set)"
himmelma@33175
  2341
proof(simp add: complete_def, rule, rule)
himmelma@33175
  2342
  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
himmelma@33175
  2343
  hence "convergent f" by (rule Cauchy_convergent)
huffman@36660
  2344
  thus "\<exists>l. f ----> l" unfolding convergent_def .  
himmelma@33175
  2345
qed
himmelma@33175
  2346
himmelma@33175
  2347
lemma complete_imp_closed: assumes "complete s" shows "closed s"
himmelma@33175
  2348
proof -
himmelma@33175
  2349
  { fix x assume "x islimpt s"
himmelma@33175
  2350
    then obtain f where f: "\<forall>n. f n \<in> s - {x}" "(f ---> x) sequentially"
himmelma@33175
  2351
      unfolding islimpt_sequential by auto
himmelma@33175
  2352
    then obtain l where l: "l\<in>s" "(f ---> l) sequentially"
himmelma@33175
  2353
      using `complete s`[unfolded complete_def] using convergent_imp_cauchy[of f x] by auto
hoelzl@42841
  2354
    hence "x \<in> s"  using tendsto_unique[of sequentially f l x] trivial_limit_sequentially f(2) by auto
himmelma@33175
  2355
  }
himmelma@33175
  2356
  thus "closed s" unfolding closed_limpt by auto
himmelma@33175
  2357
qed
himmelma@33175
  2358
himmelma@33175
  2359
lemma complete_eq_closed:
himmelma@33175
  2360
  fixes s :: "'a::complete_space set"
himmelma@33175
  2361
  shows "complete s \<longleftrightarrow> closed s" (is "?lhs = ?rhs")
himmelma@33175
  2362
proof
himmelma@33175
  2363
  assume ?lhs thus ?rhs by (rule complete_imp_closed)
himmelma@33175
  2364
next
himmelma@33175
  2365
  assume ?rhs
himmelma@33175
  2366
  { fix f assume as:"\<forall>n::nat. f n \<in> s" "Cauchy f"
himmelma@33175
  2367
    then obtain l where "(f ---> l) sequentially" using complete_univ[unfolded complete_def, THEN spec[where x=f]] by auto
himmelma@33175
  2368
    hence "\<exists>l\<in>s. (f ---> l) sequentially" using `?rhs`[unfolded closed_sequential_limits, THEN spec[where x=f], THEN spec[where x=l]] using as(1) by auto  }
himmelma@33175
  2369
  thus ?lhs unfolding complete_def by auto
himmelma@33175
  2370
qed
himmelma@33175
  2371
himmelma@33175
  2372
lemma convergent_eq_cauchy:
himmelma@33175
  2373
  fixes s :: "nat \<Rightarrow> 'a::complete_space"
himmelma@33175
  2374
  shows "(\<exists>l. (s ---> l) sequentially) \<longleftrightarrow> Cauchy s" (is "?lhs = ?rhs")
himmelma@33175
  2375
proof
himmelma@33175
  2376
  assume ?lhs then obtain l where "(s ---> l) sequentially" by auto
himmelma@33175
  2377
  thus ?rhs using convergent_imp_cauchy by auto
himmelma@33175
  2378
next
himmelma@33175
  2379
  assume ?rhs thus ?lhs using complete_univ[unfolded complete_def, THEN spec[where x=s]] by auto
himmelma@33175
  2380
qed
himmelma@33175
  2381
himmelma@33175
  2382
lemma convergent_imp_bounded:
himmelma@33175
  2383
  fixes s :: "nat \<Rightarrow> 'a::metric_space"
himmelma@33175
  2384
  shows "(s ---> l) sequentially ==> bounded (s ` (UNIV::(nat set)))"
himmelma@33175
  2385
  using convergent_imp_cauchy[of s]
himmelma@33175
  2386
  using cauchy_imp_bounded[of s]
himmelma@33175
  2387
  unfolding image_def
himmelma@33175
  2388
  by auto
himmelma@33175
  2389
huffman@36433
  2390
subsubsection{* Total boundedness *}
himmelma@33175
  2391
himmelma@33175
  2392
fun helper_1::"('a::metric_space set) \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> 'a" where
himmelma@33175
  2393
  "helper_1 s e n = (SOME y::'a. y \<in> s \<and> (\<forall>m<n. \<not> (dist (helper_1 s e m) y < e)))"
himmelma@33175
  2394
declare helper_1.simps[simp del]
himmelma@33175
  2395
himmelma@33175
  2396
lemma compact_imp_totally_bounded:
himmelma@33175
  2397
  assumes "compact s"
himmelma@33175
  2398
  shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` k))"
himmelma@33175
  2399
proof(rule, rule, rule ccontr)
himmelma@33175
  2400
  fix e::real assume "e>0" and assm:"\<not> (\<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k)"
himmelma@33175
  2401
  def x \<equiv> "helper_1 s e"
himmelma@33175
  2402
  { fix n
himmelma@33175
  2403
    have "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)"
himmelma@33175
  2404
    proof(induct_tac rule:nat_less_induct)
himmelma@33175
  2405
      fix n  def Q \<equiv> "(\<lambda>y. y \<in> s \<and> (\<forall>m<n. \<not> dist (x m) y < e))"
himmelma@33175
  2406
      assume as:"\<forall>m<n. x m \<in> s \<and> (\<forall>ma<m. \<not> dist (x ma) (x m) < e)"
himmelma@33175
  2407
      have "\<not> s \<subseteq> (\<Union>x\<in>x ` {0..<n}. ball x e)" using assm apply simp apply(erule_tac x="x ` {0 ..< n}" in allE) using as by auto
himmelma@33175
  2408
      then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x ` {0..<n}. ball x e)" unfolding subset_eq by auto
himmelma@33175
  2409
      have "Q (x n)" unfolding x_def and helper_1.simps[of s e n]
himmelma@33175
  2410
        apply(rule someI2[where a=z]) unfolding x_def[symmetric] and Q_def using z by auto
himmelma@33175
  2411
      thus "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)" unfolding Q_def by auto
himmelma@33175
  2412
    qed }
himmelma@33175
  2413
  hence "\<forall>n::nat. x n \<in> s" and x:"\<forall>n. \<forall>m < n. \<not> (dist (x m) (x n) < e)" by blast+
himmelma@33175
  2414
  then obtain l r where "l\<in>s" and r:"subseq r" and "((x \<circ> r) ---> l) sequentially" using assms(1)[unfolded compact_def, THEN spec[where x=x]] by auto
himmelma@33175
  2415
  from this(3) have "Cauchy (x \<circ> r)" using convergent_imp_cauchy by auto
himmelma@33175
  2416
  then obtain N::nat where N:"\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist ((x \<circ> r) m) ((x \<circ> r) n) < e" unfolding cauchy_def using `e>0` by auto
himmelma@33175
  2417
  show False
himmelma@33175
  2418
    using N[THEN spec[where x=N], THEN spec[where x="N+1"]]
himmelma@33175
  2419
    using r[unfolded subseq_def, THEN spec[where x=N], THEN spec[where x="N+1"]]
himmelma@33175
  2420
    using x[THEN spec[where x="r (N+1)"], THEN spec[where x="r (N)"]] by auto
himmelma@33175
  2421
qed
himmelma@33175
  2422
huffman@36433
  2423
subsubsection{* Heine-Borel theorem *}
huffman@36433
  2424
huffman@36433
  2425
text {* Following Burkill \& Burkill vol. 2. *}
himmelma@33175
  2426
himmelma@33175
  2427
lemma heine_borel_lemma: fixes s::"'a::metric_space set"
himmelma@33175
  2428
  assumes "compact s"  "s \<subseteq> (\<Union> t)"  "\<forall>b \<in> t. open b"
himmelma@33175
  2429
  shows "\<exists>e>0. \<forall>x \<in> s. \<exists>b \<in> t. ball x e \<subseteq> b"
himmelma@33175
  2430
proof(rule ccontr)
himmelma@33175
  2431
  assume "\<not> (\<exists>e>0. \<forall>x\<in>s. \<exists>b\<in>t. ball x e \<subseteq> b)"
himmelma@33175
  2432
  hence cont:"\<forall>e>0. \<exists>x\<in>s. \<forall>xa\<in>t. \<not> (ball x e \<subseteq> xa)" by auto
himmelma@33175
  2433
  { fix n::nat
himmelma@33175
  2434
    have "1 / real (n + 1) > 0" by auto
himmelma@33175
  2435
    hence "\<exists>x. x\<in>s \<and> (\<forall>xa\<in>t. \<not> (ball x (inverse (real (n+1))) \<subseteq> xa))" using cont unfolding Bex_def by auto }
himmelma@33175
  2436
  hence "\<forall>n::nat. \<exists>x. x \<in> s \<and> (\<forall>xa\<in>t. \<not> ball x (inverse (real (n + 1))) \<subseteq> xa)" by auto
himmelma@33175
  2437
  then obtain f where f:"\<forall>n::nat. f n \<in> s \<and> (\<forall>xa\<in>t. \<not> ball (f n) (inverse (real (n + 1))) \<subseteq> xa)"
himmelma@33175
  2438
    using choice[of "\<lambda>n::nat. \<lambda>x. x\<in>s \<and> (\<forall>xa\<in>t. \<not> ball x (inverse (real (n + 1))) \<subseteq> xa)"] by auto
himmelma@33175
  2439
himmelma@33175
  2440
  then obtain l r where l:"l\<in>s" and r:"subseq r" and lr:"((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2441
    using assms(1)[unfolded compact_def, THEN spec[where x=f]] by auto
himmelma@33175
  2442
himmelma@33175
  2443
  obtain b where "l\<in>b" "b\<in>t" using assms(2) and l by auto
himmelma@33175
  2444
  then obtain e where "e>0" and e:"\<forall>z. dist z l < e \<longrightarrow> z\<in>b"
himmelma@33175
  2445
    using assms(3)[THEN bspec[where x=b]] unfolding open_dist by auto
himmelma@33175
  2446
himmelma@33175
  2447
  then obtain N1 where N1:"\<forall>n\<ge>N1. dist ((f \<circ> r) n) l < e / 2"
himmelma@33175
  2448
    using lr[unfolded Lim_sequentially, THEN spec[where x="e/2"]] by auto
himmelma@33175
  2449
himmelma@33175
  2450
  obtain N2::nat where N2:"N2>0" "inverse (real N2) < e /2" using real_arch_inv[of "e/2"] and `e>0` by auto
himmelma@33175
  2451
  have N2':"inverse (real (r (N1 + N2) +1 )) < e/2"
himmelma@33175
  2452
    apply(rule order_less_trans) apply(rule less_imp_inverse_less) using N2
himmelma@33175
  2453
    using subseq_bigger[OF r, of "N1 + N2"] by auto
himmelma@33175
  2454
himmelma@33175
  2455
  def x \<equiv> "(f (r (N1 + N2)))"
himmelma@33175
  2456
  have x:"\<not> ball x (inverse (real (r (N1 + N2) + 1))) \<subseteq> b" unfolding x_def
himmelma@33175
  2457
    using f[THEN spec[where x="r (N1 + N2)"]] using `b\<in>t` by auto
himmelma@33175
  2458
  have "\<exists>y\<in>ball x (inverse (real (r (N1 + N2) + 1))). y\<notin>b" apply(rule ccontr) using x by auto
himmelma@33175
  2459
  then obtain y where y:"y \<in> ball x (inverse (real (r (N1 + N2) + 1)))" "y \<notin> b" by auto
himmelma@33175
  2460
himmelma@33175
  2461
  have "dist x l < e/2" using N1 unfolding x_def o_def by auto
himmelma@33175
  2462
  hence "dist y l < e" using y N2' using dist_triangle[of y l x]by (auto simp add:dist_commute)
himmelma@33175
  2463
himmelma@33175
  2464
  thus False using e and `y\<notin>b` by auto
himmelma@33175
  2465
qed
himmelma@33175
  2466
himmelma@33175
  2467
lemma compact_imp_heine_borel: "compact s ==> (\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f)
himmelma@33175
  2468
               \<longrightarrow> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f')))"
himmelma@33175
  2469
proof clarify
himmelma@33175
  2470
  fix f assume "compact s" " \<forall>t\<in>f. open t" "s \<subseteq> \<Union>f"
himmelma@33175
  2471
  then obtain e::real where "e>0" and "\<forall>x\<in>s. \<exists>b\<in>f. ball x e \<subseteq> b" using heine_borel_lemma[of s f] by auto
himmelma@33175
  2472
  hence "\<forall>x\<in>s. \<exists>b. b\<in>f \<and> ball x e \<subseteq> b" by auto
himmelma@33175
  2473
  hence "\<exists>bb. \<forall>x\<in>s. bb x \<in>f \<and> ball x e \<subseteq> bb x" using bchoice[of s "\<lambda>x b. b\<in>f \<and> ball x e \<subseteq> b"] by auto
himmelma@33175
  2474
  then obtain  bb where bb:"\<forall>x\<in>s. (bb x) \<in> f \<and> ball x e \<subseteq> (bb x)" by blast
himmelma@33175
  2475
himmelma@33175
  2476
  from `compact s` have  "\<exists> k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k" using compact_imp_totally_bounded[of s] `e>0` by auto
himmelma@33175
  2477
  then obtain k where k:"finite k" "k \<subseteq> s" "s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k" by auto
himmelma@33175
  2478
himmelma@33175
  2479
  have "finite (bb ` k)" using k(1) by auto
himmelma@33175
  2480
  moreover
himmelma@33175
  2481
  { fix x assume "x\<in>s"
himmelma@33175
  2482
    hence "x\<in>\<Union>(\<lambda>x. ball x e) ` k" using k(3)  unfolding subset_eq by auto
himmelma@33175
  2483
    hence "\<exists>X\<in>bb ` k. x \<in> X" using bb k(2) by blast
himmelma@33175
  2484
    hence "x \<in> \<Union>(bb ` k)" using  Union_iff[of x "bb ` k"] by auto
himmelma@33175
  2485
  }
himmelma@33175
  2486
  ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f'" using bb k(2) by (rule_tac x="bb ` k" in exI) auto
himmelma@33175
  2487
qed
himmelma@33175
  2488
huffman@36433
  2489
subsubsection {* Bolzano-Weierstrass property *}
himmelma@33175
  2490
himmelma@33175
  2491
lemma heine_borel_imp_bolzano_weierstrass:
himmelma@33175
  2492
  assumes "\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f) --> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f'))"
himmelma@33175
  2493
          "infinite t"  "t \<subseteq> s"
himmelma@33175
  2494
  shows "\<exists>x \<in> s. x islimpt t"
himmelma@33175
  2495
proof(rule ccontr)
himmelma@33175
  2496
  assume "\<not> (\<exists>x \<in> s. x islimpt t)"
himmelma@33175
  2497
  then obtain f where f:"\<forall>x\<in>s. x \<in> f x \<and> open (f x) \<and> (\<forall>y\<in>t. y \<in> f x \<longrightarrow> y = x)" unfolding islimpt_def
himmelma@33175
  2498
    using bchoice[of s "\<lambda> x T. x \<in> T \<and> open T \<and> (\<forall>y\<in>t. y \<in> T \<longrightarrow> y = x)"] by auto
himmelma@33175
  2499
  obtain g where g:"g\<subseteq>{t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"
himmelma@33175
  2500
    using assms(1)[THEN spec[where x="{t. \<exists>x. x\<in>s \<and> t = f x}"]] using f by auto
himmelma@33175
  2501
  from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa" by auto
himmelma@33175
  2502
  { fix x y assume "x\<in>t" "y\<in>t" "f x = f y"
himmelma@33175
  2503
    hence "x \<in> f x"  "y \<in> f x \<longrightarrow> y = x" using f[THEN bspec[where x=x]] and `t\<subseteq>s` by auto
himmelma@33175
  2504
    hence "x = y" using `f x = f y` and f[THEN bspec[where x=y]] and `y\<in>t` and `t\<subseteq>s` by auto  }
huffman@36358
  2505
  hence "inj_on f t" unfolding inj_on_def by simp
huffman@36358
  2506
  hence "infinite (f ` t)" using assms(2) using finite_imageD by auto
himmelma@33175
  2507
  moreover
himmelma@33175
  2508
  { fix x assume "x\<in>t" "f x \<notin> g"
himmelma@33175
  2509
    from g(3) assms(3) `x\<in>t` obtain h where "h\<in>g" and "x\<in>h" by auto
himmelma@33175
  2510
    then obtain y where "y\<in>s" "h = f y" using g'[THEN bspec[where x=h]] by auto
himmelma@33175
  2511
    hence "y = x" using f[THEN bspec[where x=y]] and `x\<in>t` and `x\<in>h`[unfolded `h = f y`] by auto
himmelma@33175
  2512
    hence False using `f x \<notin> g` `h\<in>g` unfolding `h = f y` by auto  }
himmelma@33175
  2513
  hence "f ` t \<subseteq> g" by auto
himmelma@33175
  2514
  ultimately show False using g(2) using finite_subset by auto
himmelma@33175
  2515
qed
himmelma@33175
  2516
huffman@36433
  2517
subsubsection {* Complete the chain of compactness variants *}
himmelma@33175
  2518
huffman@44944
  2519
lemma islimpt_range_imp_convergent_subsequence:
huffman@44944
  2520
  fixes f :: "nat \<Rightarrow> 'a::metric_space"
huffman@44944
  2521
  assumes "l islimpt (range f)"
huffman@44944
  2522
  shows "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
huffman@44944
  2523
proof (intro exI conjI)
huffman@44944
  2524
  have *: "\<And>e. 0 < e \<Longrightarrow> \<exists>n. 0 < dist (f n) l \<and> dist (f n) l < e"
huffman@44944
  2525
    using assms unfolding islimpt_def
huffman@44944
  2526
    by (drule_tac x="ball l e" in spec)
huffman@44944
  2527
       (auto simp add: zero_less_dist_iff dist_commute)
huffman@44944
  2528
huffman@44944
  2529
  def t \<equiv> "\<lambda>e. LEAST n. 0 < dist (f n) l \<and> dist (f n) l < e"
huffman@44944
  2530
  have f_t_neq: "\<And>e. 0 < e \<Longrightarrow> 0 < dist (f (t e)) l"
huffman@44944
  2531
    unfolding t_def by (rule LeastI2_ex [OF * conjunct1])
huffman@44944
  2532
  have f_t_closer: "\<And>e. 0 < e \<Longrightarrow> dist (f (t e)) l < e"
huffman@44944
  2533
    unfolding t_def by (rule LeastI2_ex [OF * conjunct2])
huffman@44944
  2534
  have t_le: "\<And>n e. 0 < dist (f n) l \<Longrightarrow> dist (f n) l < e \<Longrightarrow> t e \<le> n"
huffman@44944
  2535
    unfolding t_def by (simp add: Least_le)
huffman@44944
  2536
  have less_tD: "\<And>n e. n < t e \<Longrightarrow> 0 < dist (f n) l \<Longrightarrow> e \<le> dist (f n) l"
huffman@44944
  2537
    unfolding t_def by (drule not_less_Least) simp
huffman@44944
  2538
  have t_antimono: "\<And>e e'. 0 < e \<Longrightarrow> e \<le> e' \<Longrightarrow> t e' \<le> t e"
huffman@44944
  2539
    apply (rule t_le)
huffman@44944
  2540
    apply (erule f_t_neq)
huffman@44944
  2541
    apply (erule (1) less_le_trans [OF f_t_closer])
huffman@44944
  2542
    done
huffman@44944
  2543
  have t_dist_f_neq: "\<And>n. 0 < dist (f n) l \<Longrightarrow> t (dist (f n) l) \<noteq> n"
huffman@44944
  2544
    by (drule f_t_closer) auto
huffman@44944
  2545
  have t_less: "\<And>e. 0 < e \<Longrightarrow> t e < t (dist (f (t e)) l)"
huffman@44944
  2546
    apply (subst less_le)
huffman@44944
  2547
    apply (rule conjI)
huffman@44944
  2548
    apply (rule t_antimono)
huffman@44944
  2549
    apply (erule f_t_neq)
huffman@44944
  2550
    apply (erule f_t_closer [THEN less_imp_le])
huffman@44944
  2551
    apply (rule t_dist_f_neq [symmetric])
huffman@44944
  2552
    apply (erule f_t_neq)
huffman@44944
  2553
    done
huffman@44944
  2554
  have dist_f_t_less':
huffman@44944
  2555
    "\<And>e e'. 0 < e \<Longrightarrow> 0 < e' \<Longrightarrow> t e \<le> t e' \<Longrightarrow> dist (f (t e')) l < e"
huffman@44944
  2556
    apply (simp add: le_less)
huffman@44944
  2557
    apply (erule disjE)
huffman@44944
  2558
    apply (rule less_trans)
huffman@44944
  2559
    apply (erule f_t_closer)
huffman@44944
  2560
    apply (rule le_less_trans)
huffman@44944
  2561
    apply (erule less_tD)
huffman@44944
  2562
    apply (erule f_t_neq)
huffman@44944
  2563
    apply (erule f_t_closer)
huffman@44944
  2564
    apply (erule subst)
huffman@44944
  2565
    apply (erule f_t_closer)
huffman@44944
  2566
    done
huffman@44944
  2567
huffman@44944
  2568
  def r \<equiv> "nat_rec (t 1) (\<lambda>_ x. t (dist (f x) l))"
huffman@44944
  2569
  have r_simps: "r 0 = t 1" "\<And>n. r (Suc n) = t (dist (f (r n)) l)"
huffman@44944
  2570
    unfolding r_def by simp_all
huffman@44944
  2571
  have f_r_neq: "\<And>n. 0 < dist (f (r n)) l"
huffman@44944
  2572
    by (induct_tac n) (simp_all add: r_simps f_t_neq)
huffman@44944
  2573
huffman@44944
  2574
  show "subseq r"
huffman@44944
  2575
    unfolding subseq_Suc_iff
huffman@44944
  2576
    apply (rule allI)
huffman@44944
  2577
    apply (case_tac n)
huffman@44944
  2578
    apply (simp_all add: r_simps)
huffman@44944
  2579
    apply (rule t_less, rule zero_less_one)
huffman@44944
  2580
    apply (rule t_less, rule f_r_neq)
huffman@44944
  2581
    done
huffman@44944
  2582
  show "((f \<circ> r) ---> l) sequentially"
huffman@44944
  2583
    unfolding Lim_sequentially o_def
huffman@44944
  2584
    apply (clarify, rule_tac x="t e" in exI, clarify)
huffman@44944
  2585
    apply (drule le_trans, rule seq_suble [OF `subseq r`])
huffman@44944
  2586
    apply (case_tac n, simp_all add: r_simps dist_f_t_less' f_r_neq)
huffman@44944
  2587
    done
huffman@44944
  2588
qed
huffman@44944
  2589
huffman@44944
  2590
lemma finite_range_imp_infinite_repeats:
huffman@44944
  2591
  fixes f :: "nat \<Rightarrow> 'a"
huffman@44944
  2592
  assumes "finite (range f)"
huffman@44944
  2593
  shows "\<exists>k. infinite {n. f n = k}"
huffman@44944
  2594
proof -
huffman@44944
  2595
  { fix A :: "'a set" assume "finite A"
huffman@44944
  2596
    hence "\<And>f. infinite {n. f n \<in> A} \<Longrightarrow> \<exists>k. infinite {n. f n = k}"
huffman@44944
  2597
    proof (induct)
huffman@44944
  2598
      case empty thus ?case by simp
huffman@44944
  2599
    next
huffman@44944
  2600
      case (insert x A)
huffman@44944
  2601
     show ?case
huffman@44944
  2602
      proof (cases "finite {n. f n = x}")
huffman@44944
  2603
        case True
huffman@44944
  2604
        with `infinite {n. f n \<in> insert x A}`
huffman@44944
  2605
        have "infinite {n. f n \<in> A}" by simp
huffman@44944
  2606
        thus "\<exists>k. infinite {n. f n = k}" by (rule insert)
huffman@44944
  2607
      next
huffman@44944
  2608
        case False thus "\<exists>k. infinite {n. f n = k}" ..
huffman@44944
  2609
      qed
huffman@44944
  2610
    qed
huffman@44944
  2611
  } note H = this
huffman@44944
  2612
  from assms show "\<exists>k. infinite {n. f n = k}"
huffman@44944
  2613
    by (rule H) simp
huffman@44944
  2614
qed
huffman@44944
  2615
huffman@44944
  2616
lemma bolzano_weierstrass_imp_compact:
huffman@44944
  2617
  fixes s :: "'a::metric_space set"
huffman@44944
  2618
  assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
huffman@44944
  2619
  shows "compact s"
huffman@44944
  2620
proof -
huffman@44944
  2621
  { fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
huffman@44944
  2622
    have "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
huffman@44944
  2623
    proof (cases "finite (range f)")
huffman@44944
  2624
      case True
huffman@44944
  2625
      hence "\<exists>l. infinite {n. f n = l}"
huffman@44944
  2626
        by (rule finite_range_imp_infinite_repeats)
huffman@44944
  2627
      then obtain l where "infinite {n. f n = l}" ..
huffman@44944
  2628
      hence "\<exists>r. subseq r \<and> (\<forall>n. r n \<in> {n. f n = l})"
huffman@44944
  2629
        by (rule infinite_enumerate)
huffman@44944
  2630
      then obtain r where "subseq r" and fr: "\<forall>n. f (r n) = l" by auto
huffman@44944
  2631
      hence "subseq r \<and> ((f \<circ> r) ---> l) sequentially"
huffman@44983
  2632
        unfolding o_def by (simp add: fr tendsto_const)
huffman@44944
  2633
      hence "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
huffman@44944
  2634
        by - (rule exI)
huffman@44944
  2635
      from f have "\<forall>n. f (r n) \<in> s" by simp
huffman@44944
  2636
      hence "l \<in> s" by (simp add: fr)
huffman@44944
  2637
      thus "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
huffman@44944
  2638
        by (rule rev_bexI) fact
huffman@44944
  2639
    next
huffman@44944
  2640
      case False
huffman@44944
  2641
      with f assms have "\<exists>x\<in>s. x islimpt (range f)" by auto
huffman@44944
  2642
      then obtain l where "l \<in> s" "l islimpt (range f)" ..
huffman@44944
  2643
      have "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
huffman@44944
  2644
        using `l islimpt (range f)`
huffman@44944
  2645
        by (rule islimpt_range_imp_convergent_subsequence)
huffman@44944
  2646
      with `l \<in> s` show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" ..
huffman@44944
  2647
    qed
huffman@44944
  2648
  }
huffman@44944
  2649
  thus ?thesis unfolding compact_def by auto
huffman@44944
  2650
qed
huffman@44944
  2651
himmelma@33175
  2652
primrec helper_2::"(real \<Rightarrow> 'a::metric_space) \<Rightarrow> nat \<Rightarrow> 'a" where
himmelma@33175
  2653
  "helper_2 beyond 0 = beyond 0" |
himmelma@33175
  2654
  "helper_2 beyond (Suc n) = beyond (dist undefined (helper_2 beyond n) + 1 )"
himmelma@33175
  2655
himmelma@33175
  2656
lemma bolzano_weierstrass_imp_bounded: fixes s::"'a::metric_space set"
himmelma@33175
  2657
  assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
himmelma@33175
  2658
  shows "bounded s"
himmelma@33175
  2659
proof(rule ccontr)
himmelma@33175
  2660
  assume "\<not> bounded s"
himmelma@33175
  2661
  then obtain beyond where "\<forall>a. beyond a \<in>s \<and> \<not> dist undefined (beyond a) \<le> a"
himmelma@33175
  2662
    unfolding bounded_any_center [where a=undefined]
himmelma@33175
  2663
    apply simp using choice[of "\<lambda>a x. x\<in>s \<and> \<not> dist undefined x \<le> a"] by auto
himmelma@33175
  2664
  hence beyond:"\<And>a. beyond a \<in>s" "\<And>a. dist undefined (beyond a) > a"
himmelma@33175
  2665
    unfolding linorder_not_le by auto
himmelma@33175
  2666
  def x \<equiv> "helper_2 beyond"
himmelma@33175
  2667
himmelma@33175
  2668
  { fix m n ::nat assume "m<n"
himmelma@33175
  2669
    hence "dist undefined (x m) + 1 < dist undefined (x n)"
himmelma@33175
  2670
    proof(induct n)
himmelma@33175
  2671
      case 0 thus ?case by auto
himmelma@33175
  2672
    next
himmelma@33175
  2673
      case (Suc n)
himmelma@33175
  2674
      have *:"dist undefined (x n) + 1 < dist undefined (x (Suc n))"
himmelma@33175
  2675
        unfolding x_def and helper_2.simps
himmelma@33175
  2676
        using beyond(2)[of "dist undefined (helper_2 beyond n) + 1"] by auto
himmelma@33175
  2677
      thus ?case proof(cases "m < n")
himmelma@33175
  2678
        case True thus ?thesis using Suc and * by auto
himmelma@33175
  2679
      next
himmelma@33175
  2680
        case False hence "m = n" using Suc(2) by auto
himmelma@33175
  2681
        thus ?thesis using * by auto
himmelma@33175
  2682
      qed
himmelma@33175
  2683
    qed  } note * = this
himmelma@33175
  2684
  { fix m n ::nat assume "m\<noteq>n"
himmelma@33175
  2685
    have "1 < dist (x m) (x n)"
himmelma@33175
  2686
    proof(cases "m<n")
himmelma@33175
  2687
      case True
himmelma@33175
  2688
      hence "1 < dist undefined (x n) - dist undefined (x m)" using *[of m n] by auto
himmelma@33175
  2689
      thus ?thesis using dist_triangle [of undefined "x n" "x m"] by arith
himmelma@33175
  2690
    next
himmelma@33175
  2691
      case False hence "n<m" using `m\<noteq>n` by auto
himmelma@33175
  2692
      hence "1 < dist undefined (x m) - dist undefined (x n)" using *[of n m] by auto
himmelma@33175
  2693
      thus ?thesis using dist_triangle2 [of undefined "x m" "x n"] by arith
himmelma@33175
  2694
    qed  } note ** = this
himmelma@33175
  2695
  { fix a b assume "x a = x b" "a \<noteq> b"
himmelma@33175
  2696
    hence False using **[of a b] by auto  }
himmelma@33175
  2697
  hence "inj x" unfolding inj_on_def by auto
himmelma@33175
  2698
  moreover
himmelma@33175
  2699
  { fix n::nat
himmelma@33175
  2700
    have "x n \<in> s"
himmelma@33175
  2701
    proof(cases "n = 0")
himmelma@33175
  2702
      case True thus ?thesis unfolding x_def using beyond by auto
himmelma@33175
  2703
    next
himmelma@33175
  2704
      case False then obtain z where "n = Suc z" using not0_implies_Suc by auto
himmelma@33175
  2705
      thus ?thesis unfolding x_def using beyond by auto
himmelma@33175
  2706
    qed  }
himmelma@33175
  2707
  ultimately have "infinite (range x) \<and> range x \<subseteq> s" unfolding x_def using range_inj_infinite[of "helper_2 beyond"] using beyond(1) by auto
himmelma@33175
  2708
himmelma@33175
  2709
  then obtain l where "l\<in>s" and l:"l islimpt range x" using assms[THEN spec[where x="range x"]] by auto
himmelma@33175
  2710
  then obtain y where "x y \<noteq> l" and y:"dist (x y) l < 1/2" unfolding islimpt_approachable apply(erule_tac x="1/2" in allE) by auto
himmelma@33175
  2711
  then obtain z where "x z \<noteq> l" and z:"dist (x z) l < dist (x y) l" using l[unfolded islimpt_approachable, THEN spec[where x="dist (x y) l"]]
himmelma@33175
  2712
    unfolding dist_nz by auto
himmelma@33175
  2713
  show False using y and z and dist_triangle_half_l[of "x y" l 1 "x z"] and **[of y z] by auto
himmelma@33175
  2714
qed
himmelma@33175
  2715
himmelma@33175
  2716
lemma sequence_infinite_lemma:
huffman@44947
  2717
  fixes f :: "nat \<Rightarrow> 'a::t1_space"
huffman@44947
  2718
  assumes "\<forall>n. f n \<noteq> l" and "(f ---> l) sequentially"
huffman@34098
  2719
  shows "infinite (range f)"
huffman@34098
  2720
proof
huffman@34098
  2721
  assume "finite (range f)"
huffman@44947
  2722
  hence "closed (range f)" by (rule finite_imp_closed)
huffman@44947
  2723
  hence "open (- range f)" by (rule open_Compl)
huffman@44947
  2724
  from assms(1) have "l \<in> - range f" by auto
huffman@44947
  2725
  from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"
huffman@44947
  2726
    using `open (- range f)` `l \<in> - range f` by (rule topological_tendstoD)
huffman@44947
  2727
  thus False unfolding eventually_sequentially by auto
huffman@44947
  2728
qed
huffman@44947
  2729
huffman@44947
  2730
lemma closure_insert:
huffman@44947
  2731
  fixes x :: "'a::t1_space"
huffman@44947
  2732
  shows "closure (insert x s) = insert x (closure s)"
huffman@44947
  2733
apply (rule closure_unique)
huffman@44947
  2734
apply (rule conjI [OF insert_mono [OF closure_subset]])
huffman@44947
  2735
apply (rule conjI [OF closed_insert [OF closed_closure]])
huffman@44947
  2736
apply (simp add: closure_minimal)
huffman@44947
  2737
done
huffman@44947
  2738
huffman@44947
  2739
lemma islimpt_insert:
huffman@44947
  2740
  fixes x :: "'a::t1_space"
huffman@44947
  2741
  shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"
huffman@44947
  2742
proof
huffman@44947
  2743
  assume *: "x islimpt (insert a s)"
huffman@44947
  2744
  show "x islimpt s"
huffman@44947
  2745
  proof (rule islimptI)
huffman@44947
  2746
    fix t assume t: "x \<in> t" "open t"
huffman@44947
  2747
    show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"
huffman@44947
  2748
    proof (cases "x = a")
huffman@44947
  2749
      case True
huffman@44947
  2750
      obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"
huffman@44947
  2751
        using * t by (rule islimptE)
huffman@44947
  2752
      with `x = a` show ?thesis by auto
huffman@44947
  2753
    next
huffman@44947
  2754
      case False
huffman@44947
  2755
      with t have t': "x \<in> t - {a}" "open (t - {a})"
huffman@44947
  2756
        by (simp_all add: open_Diff)
huffman@44947
  2757
      obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"
huffman@44947
  2758
        using * t' by (rule islimptE)
huffman@44947
  2759
      thus ?thesis by auto
huffman@44947
  2760
    qed
huffman@44947
  2761
  qed
huffman@44947
  2762
next
huffman@44947
  2763
  assume "x islimpt s" thus "x islimpt (insert a s)"
huffman@44947
  2764
    by (rule islimpt_subset) auto
huffman@44947
  2765
qed
huffman@44947
  2766
huffman@44947
  2767
lemma islimpt_union_finite:
huffman@44947
  2768
  fixes x :: "'a::t1_space"
huffman@44947
  2769
  shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"
huffman@44947
  2770
by (induct set: finite, simp_all add: islimpt_insert)
huffman@44947
  2771
 
himmelma@33175
  2772
lemma sequence_unique_limpt:
huffman@44947
  2773
  fixes f :: "nat \<Rightarrow> 'a::t2_space"
huffman@44947
  2774
  assumes "(f ---> l) sequentially" and "l' islimpt (range f)"
himmelma@33175
  2775
  shows "l' = l"
huffman@44947
  2776
proof (rule ccontr)
huffman@44947
  2777
  assume "l' \<noteq> l"
huffman@44947
  2778
  obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"
huffman@44947
  2779
    using hausdorff [OF `l' \<noteq> l`] by auto
huffman@44947
  2780
  have "eventually (\<lambda>n. f n \<in> t) sequentially"
huffman@44947
  2781
    using assms(1) `open t` `l \<in> t` by (rule topological_tendstoD)
huffman@44947
  2782
  then obtain N where "\<forall>n\<ge>N. f n \<in> t"
huffman@44947
  2783
    unfolding eventually_sequentially by auto
huffman@44947
  2784
huffman@44947
  2785
  have "UNIV = {..<N} \<union> {N..}" by auto
huffman@44947
  2786
  hence "l' islimpt (f ` ({..<N} \<union> {N..}))" using assms(2) by simp
huffman@44947
  2787
  hence "l' islimpt (f ` {..<N} \<union> f ` {N..})" by (simp add: image_Un)
huffman@44947
  2788
  hence "l' islimpt (f ` {N..})" by (simp add: islimpt_union_finite)
huffman@44947
  2789
  then obtain y where "y \<in> f ` {N..}" "y \<in> s" "y \<noteq> l'"
huffman@44947
  2790
    using `l' \<in> s` `open s` by (rule islimptE)
huffman@44947
  2791
  then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'" by auto
huffman@44947
  2792
  with `\<forall>n\<ge>N. f n \<in> t` have "f n \<in> s \<inter> t" by simp
huffman@44947
  2793
  with `s \<inter> t = {}` show False by simp
himmelma@33175
  2794
qed
himmelma@33175
  2795
himmelma@33175
  2796
lemma bolzano_weierstrass_imp_closed:
himmelma@33175
  2797
  fixes s :: "'a::metric_space set" (* TODO: can this be generalized? *)
himmelma@33175
  2798
  assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
himmelma@33175
  2799
  shows "closed s"
himmelma@33175
  2800
proof-
himmelma@33175
  2801
  { fix x l assume as: "\<forall>n::nat. x n \<in> s" "(x ---> l) sequentially"
himmelma@33175
  2802
    hence "l \<in> s"
himmelma@33175
  2803
    proof(cases "\<forall>n. x n \<noteq> l")
himmelma@33175
  2804
      case False thus "l\<in>s" using as(1) by auto
himmelma@33175
  2805
    next
himmelma@33175
  2806
      case True note cas = this
huffman@34098
  2807
      with as(2) have "infinite (range x)" using sequence_infinite_lemma[of x l] by auto
huffman@34098
  2808
      then obtain l' where "l'\<in>s" "l' islimpt (range x)" using assms[THEN spec[where x="range x"]] as(1) by auto
himmelma@33175
  2809
      thus "l\<in>s" using sequence_unique_limpt[of x l l'] using as cas by auto
himmelma@33175
  2810
    qed  }
himmelma@33175
  2811
  thus ?thesis unfolding closed_sequential_limits by fast
himmelma@33175
  2812
qed
himmelma@33175
  2813
huffman@45081
  2814
text {* Hence express everything as an equivalence. *}
himmelma@33175
  2815
himmelma@33175
  2816
lemma compact_eq_heine_borel:
huffman@44945
  2817
  fixes s :: "'a::metric_space set"
himmelma@33175
  2818
  shows "compact s \<longleftrightarrow>
himmelma@33175
  2819
           (\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f)
himmelma@33175
  2820
               --> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f')))" (is "?lhs = ?rhs")
himmelma@33175
  2821
proof
huffman@44945
  2822
  assume ?lhs thus ?rhs by (rule compact_imp_heine_borel)
himmelma@33175
  2823
next
himmelma@33175
  2824
  assume ?rhs
himmelma@33175
  2825
  hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x\<in>s. x islimpt t)"
himmelma@33175
  2826
    by (blast intro: heine_borel_imp_bolzano_weierstrass[of s])
huffman@44945
  2827
  thus ?lhs by (rule bolzano_weierstrass_imp_compact)
himmelma@33175
  2828
qed
himmelma@33175
  2829
himmelma@33175
  2830
lemma compact_eq_bolzano_weierstrass:
huffman@44945
  2831
  fixes s :: "'a::metric_space set"
himmelma@33175
  2832
  shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))" (is "?lhs = ?rhs")
himmelma@33175
  2833
proof
himmelma@33175
  2834
  assume ?lhs thus ?rhs unfolding compact_eq_heine_borel using heine_borel_imp_bolzano_weierstrass[of s] by auto
himmelma@33175
  2835
next
huffman@44945
  2836
  assume ?rhs thus ?lhs by (rule bolzano_weierstrass_imp_compact)
himmelma@33175
  2837
qed
himmelma@33175
  2838
himmelma@33175
  2839
lemma compact_eq_bounded_closed:
himmelma@33175
  2840
  fixes s :: "'a::heine_borel set"
himmelma@33175
  2841
  shows "compact s \<longleftrightarrow> bounded s \<and> closed s"  (is "?lhs = ?rhs")
himmelma@33175
  2842
proof
himmelma@33175
  2843
  assume ?lhs thus ?rhs unfolding compact_eq_bolzano_weierstrass using bolzano_weierstrass_imp_bounded bolzano_weierstrass_imp_closed by auto
himmelma@33175
  2844
next
himmelma@33175
  2845
  assume ?rhs thus ?lhs using bounded_closed_imp_compact by auto
himmelma@33175
  2846
qed
himmelma@33175
  2847
himmelma@33175
  2848
lemma compact_imp_bounded:
himmelma@33175
  2849
  fixes s :: "'a::metric_space set"
himmelma@33175
  2850
  shows "compact s ==> bounded s"
himmelma@33175
  2851
proof -
himmelma@33175
  2852
  assume "compact s"
himmelma@33175
  2853
  hence "\<forall>f. (\<forall>t\<in>f. open t) \<and> s \<subseteq> \<Union>f \<longrightarrow> (\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f')"
himmelma@33175
  2854
    by (rule compact_imp_heine_borel)
himmelma@33175
  2855
  hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"
himmelma@33175
  2856
    using heine_borel_imp_bolzano_weierstrass[of s] by auto
himmelma@33175
  2857
  thus "bounded s"
himmelma@33175
  2858
    by (rule bolzano_weierstrass_imp_bounded)
himmelma@33175
  2859
qed
himmelma@33175
  2860
himmelma@33175
  2861
lemma compact_imp_closed:
himmelma@33175
  2862
  fixes s :: "'a::metric_space set"
himmelma@33175
  2863
  shows "compact s ==> closed s"
himmelma@33175
  2864
proof -
himmelma@33175
  2865
  assume "compact s"
himmelma@33175
  2866
  hence "\<forall>f. (\<forall>t\<in>f. open t) \<and> s \<subseteq> \<Union>f \<longrightarrow> (\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f')"
himmelma@33175
  2867
    by (rule compact_imp_heine_borel)
himmelma@33175
  2868
  hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"
himmelma@33175
  2869
    using heine_borel_imp_bolzano_weierstrass[of s] by auto
himmelma@33175
  2870
  thus "closed s"
himmelma@33175
  2871
    by (rule bolzano_weierstrass_imp_closed)
himmelma@33175
  2872
qed
himmelma@33175
  2873
himmelma@33175
  2874
text{* In particular, some common special cases. *}
himmelma@33175
  2875
himmelma@33175
  2876
lemma compact_empty[simp]:
himmelma@33175
  2877
 "compact {}"
himmelma@33175
  2878
  unfolding compact_def
himmelma@33175
  2879
  by simp
himmelma@33175
  2880
huffman@44946
  2881
lemma subseq_o: "subseq r \<Longrightarrow> subseq s \<Longrightarrow> subseq (r \<circ> s)"
huffman@44946
  2882
  unfolding subseq_def by simp (* TODO: move somewhere else *)
huffman@44946
  2883
huffman@44946
  2884
lemma compact_union [intro]:
huffman@44946
  2885
  assumes "compact s" and "compact t"
huffman@44946
  2886
  shows "compact (s \<union> t)"
huffman@44946
  2887
proof (rule compactI)
huffman@44946
  2888
  fix f :: "nat \<Rightarrow> 'a"
huffman@44946
  2889
  assume "\<forall>n. f n \<in> s \<union> t"
huffman@44946
  2890
  hence "infinite {n. f n \<in> s \<union> t}" by simp
huffman@44946
  2891
  hence "infinite {n. f n \<in> s} \<or> infinite {n. f n \<in> t}" by simp
huffman@44946
  2892
  thus "\<exists>l\<in>s \<union> t. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
huffman@44946
  2893
  proof
huffman@44946
  2894
    assume "infinite {n. f n \<in> s}"
huffman@44946
  2895
    from infinite_enumerate [OF this]
huffman@44946
  2896
    obtain q where "subseq q" "\<forall>n. (f \<circ> q) n \<in> s" by auto
huffman@44946
  2897
    obtain r l where "l \<in> s" "subseq r" "((f \<circ> q \<circ> r) ---> l) sequentially"
huffman@44946
  2898
      using `compact s` `\<forall>n. (f \<circ> q) n \<in> s` by (rule compactE)
huffman@44946
  2899
    hence "l \<in> s \<union> t" "subseq (q \<circ> r)" "((f \<circ> (q \<circ> r)) ---> l) sequentially"
huffman@44946
  2900
      using `subseq q` by (simp_all add: subseq_o o_assoc)
huffman@44946
  2901
    thus ?thesis by auto
huffman@44946
  2902
  next
huffman@44946
  2903
    assume "infinite {n. f n \<in> t}"
huffman@44946
  2904
    from infinite_enumerate [OF this]
huffman@44946
  2905
    obtain q where "subseq q" "\<forall>n. (f \<circ> q) n \<in> t" by auto
huffman@44946
  2906
    obtain r l where "l \<in> t" "subseq r" "((f \<circ> q \<circ> r) ---> l) sequentially"
huffman@44946
  2907
      using `compact t` `\<forall>n. (f \<circ> q) n \<in> t` by (rule compactE)
huffman@44946
  2908
    hence "l \<in> s \<union> t" "subseq (q \<circ> r)" "((f \<circ> (q \<circ> r)) ---> l) sequentially"
huffman@44946
  2909
      using `subseq q` by (simp_all add: subseq_o o_assoc)
huffman@44946
  2910
    thus ?thesis by auto
huffman@44946
  2911
  qed
huffman@44946
  2912
qed
huffman@44946
  2913
huffman@44946
  2914
lemma compact_inter_closed [intro]:
huffman@44946
  2915
  assumes "compact s" and "closed t"
huffman@44946
  2916
  shows "compact (s \<inter> t)"
huffman@44946
  2917
proof (rule compactI)
huffman@44946
  2918
  fix f :: "nat \<Rightarrow> 'a"
huffman@44946
  2919
  assume "\<forall>n. f n \<in> s \<inter> t"
huffman@44946
  2920
  hence "\<forall>n. f n \<in> s" and "\<forall>n. f n \<in> t" by simp_all
huffman@44946
  2921
  obtain l r where "l \<in> s" "subseq r" "((f \<circ> r) ---> l) sequentially"
huffman@44946
  2922
    using `compact s` `\<forall>n. f n \<in> s` by (rule compactE)
himmelma@33175
  2923
  moreover
huffman@44946
  2924
  from `closed t` `\<forall>n. f n \<in> t` `((f \<circ> r) ---> l) sequentially` have "l \<in> t"
huffman@44946
  2925
    unfolding closed_sequential_limits o_def by fast
huffman@44946
  2926
  ultimately show "\<exists>l\<in>s \<inter> t. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
himmelma@33175
  2927
    by auto
himmelma@33175
  2928
qed
himmelma@33175
  2929
huffman@44946
  2930
lemma closed_inter_compact [intro]:
huffman@44946
  2931
  assumes "closed s" and "compact t"
huffman@44946
  2932
  shows "compact (s \<inter> t)"
huffman@44946
  2933
  using compact_inter_closed [of t s] assms
huffman@44946
  2934
  by (simp add: Int_commute)
huffman@44946
  2935
huffman@44946
  2936
lemma compact_inter [intro]:
huffman@44946
  2937
  assumes "compact s" and "compact t"
huffman@44946
  2938
  shows "compact (s \<inter> t)"
huffman@44946
  2939
  using assms by (intro compact_inter_closed compact_imp_closed)
himmelma@33175
  2940
himmelma@33175
  2941
lemma compact_sing [simp]: "compact {a}"
himmelma@33175
  2942
  unfolding compact_def o_def subseq_def
himmelma@33175
  2943
  by (auto simp add: tendsto_const)
himmelma@33175
  2944
huffman@44946
  2945
lemma compact_insert [simp]:
huffman@44946
  2946
  assumes "compact s" shows "compact (insert x s)"
huffman@44946
  2947
proof -
huffman@44946
  2948
  have "compact ({x} \<union> s)"
huffman@44946
  2949
    using compact_sing assms by (rule compact_union)
huffman@44946
  2950
  thus ?thesis by simp
huffman@44946
  2951
qed
huffman@44946
  2952
huffman@44946
  2953
lemma finite_imp_compact:
huffman@44946
  2954
  shows "finite s \<Longrightarrow> compact s"
huffman@44946
  2955
  by (induct set: finite) simp_all
huffman@44946
  2956
himmelma@33175
  2957
lemma compact_cball[simp]:
himmelma@33175
  2958
  fixes x :: "'a::heine_borel"
himmelma@33175
  2959
  shows "compact(cball x e)"
himmelma@33175
  2960
  using compact_eq_bounded_closed bounded_cball closed_cball
himmelma@33175
  2961
  by blast
himmelma@33175
  2962
himmelma@33175
  2963
lemma compact_frontier_bounded[intro]:
himmelma@33175
  2964
  fixes s :: "'a::heine_borel set"
himmelma@33175
  2965
  shows "bounded s ==> compact(frontier s)"
himmelma@33175
  2966
  unfolding frontier_def
himmelma@33175
  2967
  using compact_eq_bounded_closed
himmelma@33175
  2968
  by blast
himmelma@33175
  2969
himmelma@33175
  2970
lemma compact_frontier[intro]:
himmelma@33175
  2971
  fixes s :: "'a::heine_borel set"
himmelma@33175
  2972
  shows "compact s ==> compact (frontier s)"
himmelma@33175
  2973
  using compact_eq_bounded_closed compact_frontier_bounded
himmelma@33175
  2974
  by blast
himmelma@33175
  2975
himmelma@33175
  2976
lemma frontier_subset_compact:
himmelma@33175
  2977
  fixes s :: "'a::heine_borel set"
himmelma@33175
  2978
  shows "compact s ==> frontier s \<subseteq> s"
himmelma@33175
  2979
  using frontier_subset_closed compact_eq_bounded_closed
himmelma@33175
  2980
  by blast
himmelma@33175
  2981
himmelma@33175
  2982
lemma open_delete:
huffman@36668
  2983
  fixes s :: "'a::t1_space set"
huffman@36668
  2984
  shows "open s \<Longrightarrow> open (s - {x})"
huffman@36668
  2985
  by (simp add: open_Diff)
himmelma@33175
  2986
himmelma@33175
  2987
text{* Finite intersection property. I could make it an equivalence in fact. *}
himmelma@33175
  2988
himmelma@33175
  2989
lemma compact_imp_fip:
himmelma@33175
  2990
  assumes "compact s"  "\<forall>t \<in> f. closed t"
himmelma@33175
  2991
        "\<forall>f'. finite f' \<and> f' \<subseteq> f --> (s \<inter> (\<Inter> f') \<noteq> {})"
himmelma@33175
  2992
  shows "s \<inter> (\<Inter> f) \<noteq> {}"
himmelma@33175
  2993
proof
himmelma@33175
  2994
  assume as:"s \<inter> (\<Inter> f) = {}"
huffman@34099
  2995
  hence "s \<subseteq> \<Union> uminus ` f" by auto
huffman@34099
  2996
  moreover have "Ball (uminus ` f) open" using open_Diff closed_Diff using assms(2) by auto
huffman@34099
  2997
  ultimately obtain f' where f':"f' \<subseteq> uminus ` f"  "finite f'"  "s \<subseteq> \<Union>f'" using assms(1)[unfolded compact_eq_heine_borel, THEN spec[where x="(\<lambda>t. - t) ` f"]] by auto
huffman@34099
  2998
  hence "finite (uminus ` f') \<and> uminus ` f' \<subseteq> f" by(auto simp add: Diff_Diff_Int)
huffman@34099
  2999
  hence "s \<inter> \<Inter>uminus ` f' \<noteq> {}" using assms(3)[THEN spec[where x="uminus ` f'"]] by auto
himmelma@33175
  3000
  thus False using f'(3) unfolding subset_eq and Union_iff by blast
himmelma@33175
  3001
qed
himmelma@33175
  3002
huffman@45081
  3003
huffman@45081
  3004
subsection {* Bounded closed nest property (proof does not use Heine-Borel) *}
himmelma@33175
  3005
himmelma@33175
  3006
lemma bounded_closed_nest:
himmelma@33175
  3007
  assumes "\<forall>n. closed(s n)" "\<forall>n. (s n \<noteq> {})"
himmelma@33175
  3008
  "(\<forall>m n. m \<le> n --> s n \<subseteq> s m)"  "bounded(s 0)"
himmelma@33175
  3009
  shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s(n)"
himmelma@33175
  3010
proof-
himmelma@33175
  3011
  from assms(2) obtain x where x:"\<forall>n::nat. x n \<in> s n" using choice[of "\<lambda>n x. x\<in> s n"] by auto
himmelma@33175
  3012
  from assms(4,1) have *:"compact (s 0)" using bounded_closed_imp_compact[of "s 0"] by auto
himmelma@33175
  3013
himmelma@33175
  3014
  then obtain l r where lr:"l\<in>s 0" "subseq r" "((x \<circ> r) ---> l) sequentially"
himmelma@33175
  3015
    unfolding compact_def apply(erule_tac x=x in allE)  using x using assms(3) by blast
himmelma@33175
  3016
himmelma@33175
  3017
  { fix n::nat
himmelma@33175
  3018
    { fix e::real assume "e>0"
himmelma@33175
  3019
      with lr(3) obtain N where N:"\<forall>m\<ge>N. dist ((x \<circ> r) m) l < e" unfolding Lim_sequentially by auto
himmelma@33175
  3020
      hence "dist ((x \<circ> r) (max N n)) l < e" by auto
himmelma@33175
  3021
      moreover
himmelma@33175
  3022
      have "r (max N n) \<ge> n" using lr(2) using subseq_bigger[of r "max N n"] by auto
himmelma@33175
  3023
      hence "(x \<circ> r) (max N n) \<in> s n"
himmelma@33175
  3024
        using x apply(erule_tac x=n in allE)
himmelma@33175
  3025
        using x apply(erule_tac x="r (max N n)" in allE)
himmelma@33175
  3026
        using assms(3) apply(erule_tac x=n in allE)apply( erule_tac x="r (max N n)" in allE) by auto
himmelma@33175
  3027
      ultimately have "\<exists>y\<in>s n. dist y l < e" by auto
himmelma@33175
  3028
    }
himmelma@33175
  3029
    hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by blast
himmelma@33175
  3030
  }
himmelma@33175
  3031
  thus ?thesis by auto
himmelma@33175
  3032
qed
himmelma@33175
  3033
huffman@45081
  3034
text {* Decreasing case does not even need compactness, just completeness. *}
himmelma@33175
  3035
himmelma@33175
  3036
lemma decreasing_closed_nest:
himmelma@33175
  3037
  assumes "\<forall>n. closed(s n)"
himmelma@33175
  3038
          "\<forall>n. (s n \<noteq> {})"
himmelma@33175
  3039
          "\<forall>m n. m \<le> n --> s n \<subseteq> s m"
himmelma@33175
  3040
          "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y \<in> (s n). dist x y < e"
himmelma@33175
  3041
  shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s n"
himmelma@33175
  3042
proof-
himmelma@33175
  3043
  have "\<forall>n. \<exists> x. x\<in>s n" using assms(2) by auto
himmelma@33175
  3044
  hence "\<exists>t. \<forall>n. t n \<in> s n" using choice[of "\<lambda> n x. x \<in> s n"] by auto
himmelma@33175
  3045
  then obtain t where t: "\<forall>n. t n \<in> s n" by auto
himmelma@33175
  3046
  { fix e::real assume "e>0"
himmelma@33175
  3047
    then obtain N where N:"\<forall>x\<in>s N. \<forall>y\<in>s N. dist x y < e" using assms(4) by auto
himmelma@33175
  3048
    { fix m n ::nat assume "N \<le> m \<and> N \<le> n"
himmelma@33175
  3049
      hence "t m \<in> s N" "t n \<in> s N" using assms(3) t unfolding  subset_eq t by blast+
himmelma@33175
  3050
      hence "dist (t m) (t n) < e" using N by auto
himmelma@33175
  3051
    }
himmelma@33175
  3052
    hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (t m) (t n) < e" by auto
himmelma@33175
  3053
  }
himmelma@33175
  3054
  hence  "Cauchy t" unfolding cauchy_def by auto
himmelma@33175
  3055
  then obtain l where l:"(t ---> l) sequentially" using complete_univ unfolding complete_def by auto
himmelma@33175
  3056
  { fix n::nat
himmelma@33175
  3057
    { fix e::real assume "e>0"
himmelma@33175
  3058
      then obtain N::nat where N:"\<forall>n\<ge>N. dist (t n) l < e" using l[unfolded Lim_sequentially] by auto
himmelma@33175
  3059
      have "t (max n N) \<in> s n" using assms(3) unfolding subset_eq apply(erule_tac x=n in allE) apply (erule_tac x="max n N" in allE) using t by auto
himmelma@33175
  3060
      hence "\<exists>y\<in>s n. dist y l < e" apply(rule_tac x="t (max n N)" in bexI) using N by auto
himmelma@33175
  3061
    }
himmelma@33175
  3062
    hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by auto
himmelma@33175
  3063
  }
himmelma@33175
  3064
  then show ?thesis by auto
himmelma@33175
  3065
qed
himmelma@33175
  3066
huffman@45081
  3067
text {* Strengthen it to the intersection actually being a singleton. *}
himmelma@33175
  3068
himmelma@33175
  3069
lemma decreasing_closed_nest_sing:
huffman@34098
  3070
  fixes s :: "nat \<Rightarrow> 'a::heine_borel set"
himmelma@33175
  3071
  assumes "\<forall>n. closed(s n)"
himmelma@33175
  3072
          "\<forall>n. s n \<noteq> {}"
himmelma@33175
  3073
          "\<forall>m n. m \<le> n --> s n \<subseteq> s m"
himmelma@33175
  3074
          "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y\<in>(s n). dist x y < e"
huffman@34098
  3075
  shows "\<exists>a. \<Inter>(range s) = {a}"
himmelma@33175
  3076
proof-
himmelma@33175
  3077
  obtain a where a:"\<forall>n. a \<in> s n" using decreasing_closed_nest[of s] using assms by auto
huffman@34098
  3078
  { fix b assume b:"b \<in> \<Inter>(range s)"
himmelma@33175
  3079
    { fix e::real assume "e>0"
himmelma@33175
  3080
      hence "dist a b < e" using assms(4 )using b using a by blast
himmelma@33175
  3081
    }
huffman@36770
  3082
    hence "dist a b = 0" by (metis dist_eq_0_iff dist_nz less_le)
himmelma@33175
  3083
  }
huffman@34098
  3084
  with a have "\<Inter>(range s) = {a}" unfolding image_def by auto
huffman@34098
  3085
  thus ?thesis ..
himmelma@33175
  3086
qed
himmelma@33175
  3087
himmelma@33175
  3088
text{* Cauchy-type criteria for uniform convergence. *}
himmelma@33175
  3089
himmelma@33175
  3090
lemma uniformly_convergent_eq_cauchy: fixes s::"nat \<Rightarrow> 'b \<Rightarrow> 'a::heine_borel" shows
himmelma@33175
  3091
 "(\<exists>l. \<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e) \<longleftrightarrow>
himmelma@33175
  3092
  (\<forall>e>0. \<exists>N. \<forall>m n x. N \<le> m \<and> N \<le> n \<and> P x  --> dist (s m x) (s n x) < e)" (is "?lhs = ?rhs")
himmelma@33175
  3093
proof(rule)
himmelma@33175
  3094
  assume ?lhs
himmelma@33175
  3095
  then obtain l where l:"\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist (s n x) (l x) < e" by auto
himmelma@33175
  3096
  { fix e::real assume "e>0"
himmelma@33175
  3097
    then obtain N::nat where N:"\<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist (s n x) (l x) < e / 2" using l[THEN spec[where x="e/2"]] by auto
himmelma@33175
  3098
    { fix n m::nat and x::"'b" assume "N \<le> m \<and> N \<le> n \<and> P x"
himmelma@33175
  3099
      hence "dist (s m x) (s n x) < e"
himmelma@33175
  3100
        using N[THEN spec[where x=m], THEN spec[where x=x]]
himmelma@33175
  3101
        using N[THEN spec[where x=n], THEN spec[where x=x]]
himmelma@33175
  3102
        using dist_triangle_half_l[of "s m x" "l x" e "s n x"] by auto  }
himmelma@33175
  3103
    hence "\<exists>N. \<forall>m n x. N \<le> m \<and> N \<le> n \<and> P x  --> dist (s m x) (s n x) < e"  by auto  }
himmelma@33175
  3104
  thus ?rhs by auto
himmelma@33175
  3105
next
himmelma@33175
  3106
  assume ?rhs
himmelma@33175
  3107
  hence "\<forall>x. P x \<longrightarrow> Cauchy (\<lambda>n. s n x)" unfolding cauchy_def apply auto by (erule_tac x=e in allE)auto
himmelma@33175
  3108
  then obtain l where l:"\<forall>x. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l x) sequentially" unfolding convergent_eq_cauchy[THEN sym]
himmelma@33175
  3109
    using choice[of "\<lambda>x l. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l) sequentially"] by auto
himmelma@33175
  3110
  { fix e::real assume "e>0"
himmelma@33175
  3111
    then obtain N where N:"\<forall>m n x. N \<le> m \<and> N \<le> n \<and> P x \<longrightarrow> dist (s m x) (s n x) < e/2"
himmelma@33175
  3112
      using `?rhs`[THEN spec[where x="e/2"]] by auto
himmelma@33175
  3113
    { fix x assume "P x"
himmelma@33175
  3114
      then obtain M where M:"\<forall>n\<ge>M. dist (s n x) (l x) < e/2"
himmelma@33175
  3115
        using l[THEN spec[where x=x], unfolded Lim_sequentially] using `e>0` by(auto elim!: allE[where x="e/2"])
himmelma@33175
  3116
      fix n::nat assume "n\<ge>N"
himmelma@33175
  3117
      hence "dist(s n x)(l x) < e"  using `P x`and N[THEN spec[where x=n], THEN spec[where x="N+M"], THEN spec[where x=x]]
himmelma@33175
  3118
        using M[THEN spec[where x="N+M"]] and dist_triangle_half_l[of "s n x" "s (N+M) x" e "l x"] by (auto simp add: dist_commute)  }
himmelma@33175
  3119
    hence "\<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist(s n x)(l x) < e" by auto }
himmelma@33175
  3120
  thus ?lhs by auto
himmelma@33175
  3121
qed
himmelma@33175
  3122
himmelma@33175
  3123
lemma uniformly_cauchy_imp_uniformly_convergent:
himmelma@33175
  3124
  fixes s :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::heine_borel"
himmelma@33175
  3125
  assumes "\<forall>e>0.\<exists>N. \<forall>m (n::nat) x. N \<le> m \<and> N \<le> n \<and> P x --> dist(s m x)(s n x) < e"
himmelma@33175
  3126
          "\<forall>x. P x --> (\<forall>e>0. \<exists>N. \<forall>n. N \<le> n --> dist(s n x)(l x) < e)"
himmelma@33175
  3127
  shows "\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e"
himmelma@33175
  3128
proof-
himmelma@33175
  3129
  obtain l' where l:"\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist (s n x) (l' x) < e"
himmelma@33175
  3130
    using assms(1) unfolding uniformly_convergent_eq_cauchy[THEN sym] by auto
himmelma@33175
  3131
  moreover
himmelma@33175
  3132
  { fix x assume "P x"
hoelzl@42841
  3133
    hence "l x = l' x" using tendsto_unique[OF trivial_limit_sequentially, of "\<lambda>n. s n x" "l x" "l' x"]
himmelma@33175
  3134
      using l and assms(2) unfolding Lim_sequentially by blast  }
himmelma@33175
  3135
  ultimately show ?thesis by auto
himmelma@33175
  3136
qed
himmelma@33175
  3137
huffman@45081
  3138
huffman@36433
  3139
subsection {* Continuity *}
huffman@36433
  3140
huffman@36433
  3141
text {* Define continuity over a net to take in restrictions of the set. *}
himmelma@33175
  3142
himmelma@33175
  3143
definition
huffman@44952
  3144
  continuous :: "'a::t2_space filter \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"
huffman@44952
  3145
  where "continuous net f \<longleftrightarrow> (f ---> f(netlimit net)) net"
himmelma@33175
  3146
himmelma@33175
  3147
lemma continuous_trivial_limit:
himmelma@33175
  3148
 "trivial_limit net ==> continuous net f"
himmelma@33175
  3149
  unfolding continuous_def tendsto_def trivial_limit_eq by auto
himmelma@33175
  3150
himmelma@33175
  3151
lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f ---> f(x)) (at x within s)"
himmelma@33175
  3152
  unfolding continuous_def
himmelma@33175
  3153
  unfolding tendsto_def
himmelma@33175
  3154
  using netlimit_within[of x s]
himmelma@33175
  3155
  by (cases "trivial_limit (at x within s)") (auto simp add: trivial_limit_eventually)
himmelma@33175
  3156
himmelma@33175
  3157
lemma continuous_at: "continuous (at x) f \<longleftrightarrow> (f ---> f(x)) (at x)"
himmelma@33175
  3158
  using continuous_within [of x UNIV f] by (simp add: within_UNIV)
himmelma@33175
  3159
himmelma@33175
  3160
lemma continuous_at_within:
himmelma@33175
  3161
  assumes "continuous (at x) f"  shows "continuous (at x within s) f"
himmelma@33175
  3162
  using assms unfolding continuous_at continuous_within
himmelma@33175
  3163
  by (rule Lim_at_within)
himmelma@33175
  3164
himmelma@33175
  3165
text{* Derive the epsilon-delta forms, which we often use as "definitions" *}
himmelma@33175
  3166
himmelma@33175
  3167
lemma continuous_within_eps_delta:
himmelma@33175
  3168
  "continuous (at x within s) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. \<forall>x'\<in> s.  dist x' x < d --> dist (f x') (f x) < e)"
himmelma@33175
  3169
  unfolding continuous_within and Lim_within
himmelma@33175
  3170
  apply auto unfolding dist_nz[THEN sym] apply(auto elim!:allE) apply(rule_tac x=d in exI) by auto
himmelma@33175
  3171
himmelma@33175
  3172
lemma continuous_at_eps_delta: "continuous (at x) f \<longleftrightarrow>  (\<forall>e>0. \<exists>d>0.
himmelma@33175
  3173
                           \<forall>x'. dist x' x < d --> dist(f x')(f x) < e)"
himmelma@33175
  3174
  using continuous_within_eps_delta[of x UNIV f]
himmelma@33175
  3175
  unfolding within_UNIV by blast
himmelma@33175
  3176
himmelma@33175
  3177
text{* Versions in terms of open balls. *}
himmelma@33175
  3178
himmelma@33175
  3179
lemma continuous_within_ball:
himmelma@33175
  3180
 "continuous (at x within s) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.
himmelma@33175
  3181
                            f ` (ball x d \<inter> s) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
himmelma@33175
  3182
proof
himmelma@33175
  3183
  assume ?lhs
himmelma@33175
  3184
  { fix e::real assume "e>0"
himmelma@33175
  3185
    then obtain d where d: "d>0" "\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e"
himmelma@33175
  3186
      using `?lhs`[unfolded continuous_within Lim_within] by auto
himmelma@33175
  3187
    { fix y assume "y\<in>f ` (ball x d \<inter> s)"
himmelma@33175
  3188
      hence "y \<in> ball (f x) e" using d(2) unfolding dist_nz[THEN sym]
huffman@36358
  3189
        apply (auto simp add: dist_commute) apply(erule_tac x=xa in ballE) apply auto using `e>0` by auto
himmelma@33175
  3190
    }
himmelma@33175
  3191
    hence "\<exists>d>0. f ` (ball x d \<inter> s) \<subseteq> ball (f x) e" using `d>0` unfolding subset_eq ball_def by (auto simp add: dist_commute)  }
himmelma@33175
  3192
  thus ?rhs by auto
himmelma@33175
  3193
next
himmelma@33175
  3194
  assume ?rhs thus ?lhs unfolding continuous_within Lim_within ball_def subset_eq
himmelma@33175
  3195
    apply (auto simp add: dist_commute) apply(erule_tac x=e in allE) by auto
himmelma@33175
  3196
qed
himmelma@33175
  3197
himmelma@33175
  3198
lemma continuous_at_ball:
himmelma@33175
  3199
  "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f ` (ball x d) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
himmelma@33175
  3200
proof
himmelma@33175
  3201
  assume ?lhs thus ?rhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
himmelma@33175
  3202
    apply auto apply(erule_tac x=e in allE) apply auto apply(rule_tac x=d in exI) apply auto apply(erule_tac x=xa in allE) apply (auto simp add: dist_commute dist_nz)
himmelma@33175
  3203
    unfolding dist_nz[THEN sym] by auto
himmelma@33175
  3204
next
himmelma@33175
  3205
  assume ?rhs thus ?lhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
himmelma@33175
  3206
    apply auto apply(erule_tac x=e in allE) apply auto apply(rule_tac x=d in exI) apply auto apply(erule_tac x="f xa" in allE) by (auto simp add: dist_commute dist_nz)
himmelma@33175
  3207
qed
himmelma@33175
  3208
huffman@36436
  3209
text{* Define setwise continuity in terms of limits within the set. *}
himmelma@33175
  3210
himmelma@33175
  3211
definition
huffman@36355
  3212
  continuous_on ::
huffman@36355
  3213
    "'a set \<Rightarrow> ('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"
huffman@36355
  3214
where
huffman@36436
  3215
  "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. (f ---> f x) (at x within s))"
huffman@36436
  3216
huffman@36436
  3217
lemma continuous_on_topological:
huffman@36355
  3218
  "continuous_on s f \<longleftrightarrow>
huffman@36355
  3219
    (\<forall>x\<in>s. \<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow>
huffman@36436
  3220
      (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"
huffman@36436
  3221
unfolding continuous_on_def tendsto_def
huffman@36436
  3222
unfolding Limits.eventually_within eventually_at_topological
huffman@36436
  3223
by (intro ball_cong [OF refl] all_cong imp_cong ex_cong conj_cong refl) auto
huffman@36355
  3224
huffman@36355
  3225
lemma continuous_on_iff:
huffman@36355
  3226
  "continuous_on s f \<longleftrightarrow>
huffman@36436
  3227
    (\<forall>x\<in>s. \<forall>e>0. \<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e)"
huffman@36436
  3228
unfolding continuous_on_def Lim_within
huffman@36436
  3229
apply (intro ball_cong [OF refl] all_cong ex_cong)
huffman@36436
  3230
apply (rename_tac y, case_tac "y = x", simp)
huffman@36436
  3231
apply (simp add: dist_nz)
huffman@36355
  3232
done
himmelma@33175
  3233
himmelma@33175
  3234
definition
himmelma@33175
  3235
  uniformly_continuous_on ::
huffman@36436
  3236
    "'a set \<Rightarrow> ('a::metric_space \<Rightarrow> 'b::metric_space) \<Rightarrow> bool"
huffman@36436
  3237
where
himmelma@33175
  3238
  "uniformly_continuous_on s f \<longleftrightarrow>
huffman@36436
  3239
    (\<forall>e>0. \<exists>d>0. \<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e)"
himmelma@35172
  3240
himmelma@33175
  3241
text{* Some simple consequential lemmas. *}
himmelma@33175
  3242
himmelma@33175
  3243
lemma uniformly_continuous_imp_continuous:
himmelma@33175
  3244
 " uniformly_continuous_on s f ==> continuous_on s f"
huffman@36355
  3245
  unfolding uniformly_continuous_on_def continuous_on_iff by blast
himmelma@33175
  3246
himmelma@33175
  3247
lemma continuous_at_imp_continuous_within:
himmelma@33175
  3248
 "continuous (at x) f ==> continuous (at x within s) f"
himmelma@33175
  3249
  unfolding continuous_within continuous_at using Lim_at_within by auto
himmelma@33175
  3250
huffman@36436
  3251
lemma Lim_trivial_limit: "trivial_limit net \<Longrightarrow> (f ---> l) net"
huffman@36436
  3252
unfolding tendsto_def by (simp add: trivial_limit_eq)
huffman@36436
  3253
huffman@36355
  3254
lemma continuous_at_imp_continuous_on:
huffman@36436
  3255
  assumes "\<forall>x\<in>s. continuous (at x) f"
himmelma@33175
  3256
  shows "continuous_on s f"
huffman@36436
  3257
unfolding continuous_on_def
huffman@36436
  3258
proof
huffman@36436
  3259
  fix x assume "x \<in> s"
huffman@36436
  3260
  with assms have *: "(f ---> f (netlimit (at x))) (at x)"
huffman@36436
  3261
    unfolding continuous_def by simp
huffman@36436
  3262
  have "(f ---> f x) (at x)"
huffman@36436
  3263
  proof (cases "trivial_limit (at x)")
huffman@36436
  3264
    case True thus ?thesis
huffman@36436
  3265
      by (rule Lim_trivial_limit)
huffman@36436
  3266
  next
huffman@36436
  3267
    case False
huffman@36667
  3268
    hence 1: "netlimit (at x) = x"
huffman@36436
  3269
      using netlimit_within [of x UNIV]
huffman@36436
  3270
      by (simp add: within_UNIV)
huffman@36436
  3271
    with * show ?thesis by simp
huffman@36436
  3272
  qed
huffman@36436
  3273
  thus "(f ---> f x) (at x within s)"
huffman@36436
  3274
    by (rule Lim_at_within)
himmelma@33175
  3275
qed
himmelma@33175
  3276
himmelma@33175
  3277
lemma continuous_on_eq_continuous_within:
huffman@36436
  3278
  "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x within s) f)"
huffman@36436
  3279
unfolding continuous_on_def continuous_def
huffman@36436
  3280
apply (rule ball_cong [OF refl])
huffman@36436
  3281
apply (case_tac "trivial_limit (at x within s)")
huffman@36436
  3282
apply (simp add: Lim_trivial_limit)
huffman@36436
  3283
apply (simp add: netlimit_within)
huffman@36436
  3284
done
huffman@36436
  3285
huffman@36436
  3286
lemmas continuous_on = continuous_on_def -- "legacy theorem name"
himmelma@33175
  3287
himmelma@33175
  3288
lemma continuous_on_eq_continuous_at:
huffman@36355
  3289
  shows "open s ==> (continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x) f))"
himmelma@33175
  3290
  by (auto simp add: continuous_on continuous_at Lim_within_open)
himmelma@33175
  3291
himmelma@33175
  3292
lemma continuous_within_subset:
himmelma@33175
  3293
 "continuous (at x within s) f \<Longrightarrow> t \<subseteq> s
himmelma@33175
  3294
             ==> continuous (at x within t) f"
himmelma@33175
  3295
  unfolding continuous_within by(metis Lim_within_subset)
himmelma@33175
  3296
himmelma@33175
  3297
lemma continuous_on_subset:
huffman@36355
  3298
  shows "continuous_on s f \<Longrightarrow> t \<subseteq> s ==> continuous_on t f"
himmelma@33175
  3299
  unfolding continuous_on by (metis subset_eq Lim_within_subset)
himmelma@33175
  3300
himmelma@33175
  3301
lemma continuous_on_interior:
huffman@36355
  3302
  shows "continuous_on s f \<Longrightarrow> x \<in> interior s ==> continuous (at x) f"
himmelma@33175
  3303
unfolding interior_def
himmelma@33175
  3304
apply simp
himmelma@33175
  3305
by (meson continuous_on_eq_continuous_at continuous_on_subset)
himmelma@33175
  3306
himmelma@33175
  3307
lemma continuous_on_eq:
huffman@36436
  3308
  "(\<forall>x \<in> s. f x = g x) \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on s g"
huffman@36436
  3309
  unfolding continuous_on_def tendsto_def Limits.eventually_within
huffman@36436
  3310
  by simp
himmelma@33175
  3311
huffman@45081
  3312
text {* Characterization of various kinds of continuity in terms of sequences. *}
himmelma@33175
  3313
himmelma@33175
  3314
(* \<longrightarrow> could be generalized, but \<longleftarrow> requires metric space *)
himmelma@33175
  3315
lemma continuous_within_sequentially:
himmelma@33175
  3316
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
himmelma@33175
  3317
  shows "continuous (at a within s) f \<longleftrightarrow>
himmelma@33175
  3318
                (\<forall>x. (\<forall>n::nat. x n \<in> s) \<and> (x ---> a) sequentially
himmelma@33175
  3319
                     --> ((f o x) ---> f a) sequentially)" (is "?lhs = ?rhs")
himmelma@33175
  3320
proof
himmelma@33175
  3321
  assume ?lhs
himmelma@33175
  3322
  { fix x::"nat \<Rightarrow> 'a" assume x:"\<forall>n. x n \<in> s" "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (x n) a < e"
himmelma@33175
  3323
    fix e::real assume "e>0"
himmelma@33175
  3324
    from `?lhs` obtain d where "d>0" and d:"\<forall>x\<in>s. 0 < dist x a \<and> dist x a < d \<longrightarrow> dist (f x) (f a) < e" unfolding continuous_within Lim_within using `e>0` by auto
himmelma@33175
  3325
    from x(2) `d>0` obtain N where N:"\<forall>n\<ge>N. dist (x n) a < d" by auto
himmelma@33175
  3326
    hence "\<exists>N. \<forall>n\<ge>N. dist ((f \<circ> x) n) (f a) < e"
himmelma@33175
  3327
      apply(rule_tac  x=N in exI) using N d  apply auto using x(1)
himmelma@33175
  3328
      apply(erule_tac x=n in allE) apply(erule_tac x=n in allE)
himmelma@33175
  3329
      apply(erule_tac x="x n" in ballE)  apply auto unfolding dist_nz[THEN sym] apply auto using `e>0` by auto
himmelma@33175
  3330
  }
himmelma@33175
  3331
  thus ?rhs unfolding continuous_within unfolding Lim_sequentially by simp
himmelma@33175
  3332
next
himmelma@33175
  3333
  assume ?rhs
himmelma@33175
  3334
  { fix e::real assume "e>0"
himmelma@33175
  3335
    assume "\<not> (\<exists>d>0. \<forall>x\<in>s. 0 < dist x a \<and> dist x a < d \<longrightarrow> dist (f x) (f a) < e)"
himmelma@33175
  3336
    hence "\<forall>d. \<exists>x. d>0 \<longrightarrow> x\<in>s \<and> (0 < dist x a \<and> dist x a < d \<and> \<not> dist (f x) (f a) < e)" by blast
himmelma@33175
  3337
    then obtain x where x:"\<forall>d>0. x d \<in> s \<and> (0 < dist (x d) a \<and> dist (x d) a < d \<and> \<not> dist (f (x d)) (f a) < e)"
himmelma@33175
  3338
      using choice[of "\<lambda>d x.0<d \<longrightarrow> x\<in>s \<and> (0 < dist x a \<and> dist x a < d \<and> \<not> dist (f x) (f a) < e)"] by auto
himmelma@33175
  3339
    { fix d::real assume "d>0"
himmelma@33175
  3340
      hence "\<exists>N::nat. inverse (real (N + 1)) < d" using real_arch_inv[of d] by (auto, rule_tac x="n - 1" in exI)auto
himmelma@33175
  3341
      then obtain N::nat where N:"inverse (real (N + 1)) < d" by auto
himmelma@33175
  3342
      { fix n::nat assume n:"n\<ge>N"
himmelma@33175
  3343
        hence "dist (x (inverse (real (n + 1)))) a < inverse (real (n + 1))" using x[THEN spec[where x="inverse (real (n + 1))"]] by auto
himmelma@33175
  3344
        moreover have "inverse (real (n + 1)) < d" using N n by (auto, metis Suc_le_mono le_SucE less_imp_inverse_less nat_le_real_less order_less_trans real_of_nat_Suc real_of_nat_Suc_gt_zero)
himmelma@33175
  3345
        ultimately have "dist (x (inverse (real (n + 1)))) a < d" by auto
himmelma@33175
  3346
      }
himmelma@33175
  3347
      hence "\<exists>N::nat. \<forall>n\<ge>N. dist (x (inverse (real (n + 1)))) a < d" by auto
himmelma@33175
  3348
    }
himmelma@33175
  3349
    hence "(\<forall>n::nat. x (inverse (real (n + 1))) \<in> s) \<and> (\<forall>e>0. \<exists>N::nat. \<forall>n\<ge>N. dist (x (inverse (real (n + 1)))) a < e)" using x by auto
himmelma@33175
  3350
    hence "\<forall>e>0. \<exists>N::nat. \<forall>n\<ge>N. dist (f (x (inverse (real (n + 1))))) (f a) < e"  using `?rhs`[THEN spec[where x="\<lambda>n::nat. x (inverse (real (n+1)))"], unfolded Lim_sequentially] by auto
himmelma@33175
  3351
    hence "False" apply(erule_tac x=e in allE) using `e>0` using x by auto
himmelma@33175
  3352
  }
himmelma@33175
  3353
  thus ?lhs  unfolding continuous_within unfolding Lim_within unfolding Lim_sequentially by blast
himmelma@33175
  3354
qed
himmelma@33175
  3355
himmelma@33175
  3356
lemma continuous_at_sequentially:
himmelma@33175
  3357
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
himmelma@33175
  3358
  shows "continuous (at a) f \<longleftrightarrow> (\<forall>x. (x ---> a) sequentially
himmelma@33175
  3359
                  --> ((f o x) ---> f a) sequentially)"
himmelma@33175
  3360
  using continuous_within_sequentially[of a UNIV f] unfolding within_UNIV by auto
himmelma@33175
  3361
himmelma@33175
  3362
lemma continuous_on_sequentially:
huffman@36436
  3363
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
huffman@36355
  3364
  shows "continuous_on s f \<longleftrightarrow>
huffman@36355
  3365
    (\<forall>x. \<forall>a \<in> s. (\<forall>n. x(n) \<in> s) \<and> (x ---> a) sequentially
himmelma@33175
  3366
                    --> ((f o x) ---> f(a)) sequentially)" (is "?lhs = ?rhs")
himmelma@33175
  3367
proof
himmelma@33175
  3368
  assume ?rhs thus ?lhs using continuous_within_sequentially[of _ s f] unfolding continuous_on_eq_continuous_within by auto
himmelma@33175
  3369
next
himmelma@33175
  3370
  assume ?lhs thus ?rhs unfolding continuous_on_eq_continuous_within using continuous_within_sequentially[of _ s f] by auto
himmelma@33175
  3371
qed
himmelma@33175
  3372
huffman@36437
  3373
lemma uniformly_continuous_on_sequentially':
huffman@36437
  3374
  "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>
huffman@36437
  3375
                    ((\<lambda>n. dist (x n) (y n)) ---> 0) sequentially
huffman@36437
  3376
                    \<longrightarrow> ((\<lambda>n. dist (f(x n)) (f(y n))) ---> 0) sequentially)" (is "?lhs = ?rhs")
himmelma@33175
  3377
proof
himmelma@33175
  3378
  assume ?lhs
huffman@36437
  3379
  { fix x y assume x:"\<forall>n. x n \<in> s" and y:"\<forall>n. y n \<in> s" and xy:"((\<lambda>n. dist (x n) (y n)) ---> 0) sequentially"
himmelma@33175
  3380
    { fix e::real assume "e>0"
himmelma@33175
  3381
      then obtain d where "d>0" and d:"\<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e"
himmelma@33175
  3382
        using `?lhs`[unfolded uniformly_continuous_on_def, THEN spec[where x=e]] by auto
huffman@36437
  3383
      obtain N where N:"\<forall>n\<ge>N. dist (x n) (y n) < d" using xy[unfolded Lim_sequentially dist_norm] and `d>0` by auto
himmelma@33175
  3384
      { fix n assume "n\<ge>N"
huffman@36437
  3385
        hence "dist (f (x n)) (f (y n)) < e"
himmelma@33175
  3386
          using N[THEN spec[where x=n]] using d[THEN bspec[where x="x n"], THEN bspec[where x="y n"]] using x and y
huffman@36437
  3387
          unfolding dist_commute by simp  }
huffman@36437
  3388
      hence "\<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"  by auto  }
huffman@36437
  3389
    hence "((\<lambda>n. dist (f(x n)) (f(y n))) ---> 0) sequentially" unfolding Lim_sequentially and dist_real_def by auto  }
himmelma@33175
  3390
  thus ?rhs by auto
himmelma@33175
  3391
next
himmelma@33175
  3392
  assume ?rhs
himmelma@33175
  3393
  { assume "\<not> ?lhs"
himmelma@33175
  3394
    then obtain e where "e>0" "\<forall>d>0. \<exists>x\<in>s. \<exists>x'\<in>s. dist x' x < d \<and> \<not> dist (f x') (f x) < e" unfolding uniformly_continuous_on_def by auto
himmelma@33175
  3395
    then obtain fa where fa:"\<forall>x.  0 < x \<longrightarrow> fst (fa x) \<in> s \<and> snd (fa x) \<in> s \<and> dist (fst (fa x)) (snd (fa x)) < x \<and> \<not> dist (f (fst (fa x))) (f (snd (fa x))) < e"
himmelma@33175
  3396
      using choice[of "\<lambda>d x. d>0 \<longrightarrow> fst x \<in> s \<and> snd x \<in> s \<and> dist (snd x) (fst x) < d \<and> \<not> dist (f (snd x)) (f (fst x)) < e"] unfolding Bex_def
himmelma@33175
  3397
      by (auto simp add: dist_commute)
himmelma@33175
  3398
    def x \<equiv> "\<lambda>n::nat. fst (fa (inverse (real n + 1)))"
himmelma@33175
  3399
    def y \<equiv> "\<lambda>n::nat. snd (fa (inverse (real n + 1)))"
himmelma@33175
  3400
    have xyn:"\<forall>n. x n \<in> s \<and> y n \<in> s" and xy0:"\<forall>n. dist (x n) (y n) < inverse (real n + 1)" and fxy:"\<forall>n. \<not> dist (f (x n)) (f (y n)) < e"
himmelma@33175
  3401
      unfolding x_def and y_def using fa by auto
himmelma@33175
  3402
    { fix e::real assume "e>0"
himmelma@33175
  3403
      then obtain N::nat where "N \<noteq> 0" and N:"0 < inverse (real N) \<and> inverse (real N) < e" unfolding real_arch_inv[of e]   by auto
himmelma@33175
  3404
      { fix n::nat assume "n\<ge>N"
himmelma@33175
  3405
        hence "inverse (real n + 1) < inverse (real N)" using real_of_nat_ge_zero and `N\<noteq>0` by auto
himmelma@33175
  3406
        also have "\<dots> < e" using N by auto
himmelma@33175
  3407
        finally have "inverse (real n + 1) < e" by auto
huffman@36437
  3408
        hence "dist (x n) (y n) < e" using xy0[THEN spec[where x=n]] by auto  }
huffman@36437
  3409
      hence "\<exists>N. \<forall>n\<ge>N. dist (x n) (y n) < e" by auto  }
huffman@36437
  3410
    hence "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e" using `?rhs`[THEN spec[where x=x], THEN spec[where x=y]] and xyn unfolding Lim_sequentially dist_real_def by auto
huffman@36437
  3411
    hence False using fxy and `e>0` by auto  }
himmelma@33175
  3412
  thus ?lhs unfolding uniformly_continuous_on_def by blast
himmelma@33175
  3413
qed
himmelma@33175
  3414
huffman@36437
  3415
lemma uniformly_continuous_on_sequentially:
huffman@36437
  3416
  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
huffman@36437
  3417
  shows "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>
huffman@36437
  3418
                    ((\<lambda>n. x n - y n) ---> 0) sequentially
huffman@36437
  3419
                    \<longrightarrow> ((\<lambda>n. f(x n) - f(y n)) ---> 0) sequentially)" (is "?lhs = ?rhs")
huffman@36437
  3420
(* BH: maybe the previous lemma should replace this one? *)
huffman@36437
  3421
unfolding uniformly_continuous_on_sequentially'
huffman@44983
  3422
unfolding dist_norm tendsto_norm_zero_iff ..
huffman@36437
  3423
himmelma@33175
  3424
text{* The usual transformation theorems. *}
himmelma@33175
  3425
himmelma@33175
  3426
lemma continuous_transform_within:
huffman@36667
  3427
  fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"
himmelma@33175
  3428
  assumes "0 < d" "x \<in> s" "\<forall>x' \<in> s. dist x' x < d --> f x' = g x'"
himmelma@33175
  3429
          "continuous (at x within s) f"
himmelma@33175
  3430
  shows "continuous (at x within s) g"
huffman@36667
  3431
unfolding continuous_within
huffman@36667
  3432
proof (rule Lim_transform_within)
huffman@36667
  3433
  show "0 < d" by fact
huffman@36667
  3434
  show "\<forall>x'\<in>s. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"
huffman@36667
  3435
    using assms(3) by auto
huffman@36667
  3436
  have "f x = g x"
huffman@36667
  3437
    using assms(1,2,3) by auto
huffman@36667
  3438
  thus "(f ---> g x) (at x within s)"
huffman@36667
  3439
    using assms(4) unfolding continuous_within by simp
himmelma@33175
  3440
qed
himmelma@33175
  3441
himmelma@33175
  3442
lemma continuous_transform_at:
huffman@36667
  3443
  fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"
himmelma@33175
  3444
  assumes "0 < d" "\<forall>x'. dist x' x < d --> f x' = g x'"
himmelma@33175
  3445
          "continuous (at x) f"
himmelma@33175
  3446
  shows "continuous (at x) g"
huffman@36667
  3447
  using continuous_transform_within [of d x UNIV f g] assms
huffman@36667
  3448
  by (simp add: within_UNIV)
himmelma@33175
  3449
himmelma@33175
  3450
text{* Combination results for pointwise continuity. *}
himmelma@33175
  3451
himmelma@33175
  3452
lemma continuous_const: "continuous net (\<lambda>x. c)"
huffman@44983
  3453
  by (auto simp add: continuous_def tendsto_const)
himmelma@33175
  3454
himmelma@33175
  3455
lemma continuous_cmul:
himmelma@33175
  3456
  fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3457
  shows "continuous net f ==> continuous net (\<lambda>x. c *\<^sub>R f x)"
huffman@44983
  3458
  by (auto simp add: continuous_def intro: tendsto_intros)
himmelma@33175
  3459
himmelma@33175
  3460
lemma continuous_neg:
himmelma@33175
  3461
  fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3462
  shows "continuous net f ==> continuous net (\<lambda>x. -(f x))"
huffman@44983
  3463
  by (auto simp add: continuous_def tendsto_minus)
himmelma@33175
  3464
himmelma@33175
  3465
lemma continuous_add:
himmelma@33175
  3466
  fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3467
  shows "continuous net f \<Longrightarrow> continuous net g \<Longrightarrow> continuous net (\<lambda>x. f x + g x)"
huffman@44983
  3468
  by (auto simp add: continuous_def tendsto_add)
himmelma@33175
  3469
himmelma@33175
  3470
lemma continuous_sub:
himmelma@33175
  3471
  fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3472
  shows "continuous net f \<Longrightarrow> continuous net g \<Longrightarrow> continuous net (\<lambda>x. f x - g x)"
huffman@44983
  3473
  by (auto simp add: continuous_def tendsto_diff)
himmelma@33175
  3474
hoelzl@34951
  3475
himmelma@33175
  3476
text{* Same thing for setwise continuity. *}
himmelma@33175
  3477
himmelma@33175
  3478
lemma continuous_on_const:
himmelma@33175
  3479
 "continuous_on s (\<lambda>x. c)"
huffman@44983
  3480
  unfolding continuous_on_def by (auto intro: tendsto_intros)
himmelma@33175
  3481
himmelma@33175
  3482
lemma continuous_on_cmul:
huffman@36436
  3483
  fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
huffman@36436
  3484
  shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. c *\<^sub>R (f x))"
huffman@36436
  3485
  unfolding continuous_on_def by (auto intro: tendsto_intros)
himmelma@33175
  3486
himmelma@33175
  3487
lemma continuous_on_neg:
huffman@36436
  3488
  fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3489
  shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. - f x)"
huffman@36436
  3490
  unfolding continuous_on_def by (auto intro: tendsto_intros)
himmelma@33175
  3491
himmelma@33175
  3492
lemma continuous_on_add:
huffman@36436
  3493
  fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3494
  shows "continuous_on s f \<Longrightarrow> continuous_on s g
himmelma@33175
  3495
           \<Longrightarrow> continuous_on s (\<lambda>x. f x + g x)"
huffman@36436
  3496
  unfolding continuous_on_def by (auto intro: tendsto_intros)
himmelma@33175
  3497
himmelma@33175
  3498
lemma continuous_on_sub:
huffman@36436
  3499
  fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3500
  shows "continuous_on s f \<Longrightarrow> continuous_on s g
himmelma@33175
  3501
           \<Longrightarrow> continuous_on s (\<lambda>x. f x - g x)"
huffman@36436
  3502
  unfolding continuous_on_def by (auto intro: tendsto_intros)
himmelma@33175
  3503
himmelma@33175
  3504
text{* Same thing for uniform continuity, using sequential formulations. *}
himmelma@33175
  3505
himmelma@33175
  3506
lemma uniformly_continuous_on_const:
himmelma@33175
  3507
 "uniformly_continuous_on s (\<lambda>x. c)"
himmelma@33175
  3508
  unfolding uniformly_continuous_on_def by simp
himmelma@33175
  3509
himmelma@33175
  3510
lemma uniformly_continuous_on_cmul:
huffman@36437
  3511
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3512
  assumes "uniformly_continuous_on s f"
himmelma@33175
  3513
  shows "uniformly_continuous_on s (\<lambda>x. c *\<^sub>R f(x))"
himmelma@33175
  3514
proof-
himmelma@33175
  3515
  { fix x y assume "((\<lambda>n. f (x n) - f (y n)) ---> 0) sequentially"
himmelma@33175
  3516
    hence "((\<lambda>n. c *\<^sub>R f (x n) - c *\<^sub>R f (y n)) ---> 0) sequentially"
huffman@45145
  3517
      using tendsto_scaleR [OF tendsto_const, of "(\<lambda>n. f (x n) - f (y n))" 0 sequentially c]
himmelma@33175
  3518
      unfolding scaleR_zero_right scaleR_right_diff_distrib by auto
himmelma@33175
  3519
  }
huffman@36437
  3520
  thus ?thesis using assms unfolding uniformly_continuous_on_sequentially'
huffman@44983
  3521
    unfolding dist_norm tendsto_norm_zero_iff by auto
himmelma@33175
  3522
qed
himmelma@33175
  3523
himmelma@33175
  3524
lemma dist_minus:
himmelma@33175
  3525
  fixes x y :: "'a::real_normed_vector"
himmelma@33175
  3526
  shows "dist (- x) (- y) = dist x y"
himmelma@33175
  3527
  unfolding dist_norm minus_diff_minus norm_minus_cancel ..
himmelma@33175
  3528
himmelma@33175
  3529
lemma uniformly_continuous_on_neg:
himmelma@33175
  3530
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3531
  shows "uniformly_continuous_on s f
himmelma@33175
  3532
         ==> uniformly_continuous_on s (\<lambda>x. -(f x))"
himmelma@33175
  3533
  unfolding uniformly_continuous_on_def dist_minus .
himmelma@33175
  3534
himmelma@33175
  3535
lemma uniformly_continuous_on_add:
huffman@36437
  3536
  fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3537
  assumes "uniformly_continuous_on s f" "uniformly_continuous_on s g"
himmelma@33175
  3538
  shows "uniformly_continuous_on s (\<lambda>x. f x + g x)"
himmelma@33175
  3539
proof-
himmelma@33175
  3540
  {  fix x y assume "((\<lambda>n. f (x n) - f (y n)) ---> 0) sequentially"
himmelma@33175
  3541
                    "((\<lambda>n. g (x n) - g (y n)) ---> 0) sequentially"
himmelma@33175
  3542
    hence "((\<lambda>xa. f (x xa) - f (y xa) + (g (x xa) - g (y xa))) ---> 0 + 0) sequentially"
huffman@44983
  3543
      using tendsto_add[of "\<lambda> n. f (x n) - f (y n)" 0  sequentially "\<lambda> n. g (x n) - g (y n)" 0] by auto
himmelma@33175
  3544
    hence "((\<lambda>n. f (x n) + g (x n) - (f (y n) + g (y n))) ---> 0) sequentially" unfolding Lim_sequentially and add_diff_add [symmetric] by auto  }
huffman@36437
  3545
  thus ?thesis using assms unfolding uniformly_continuous_on_sequentially'
huffman@44983
  3546
    unfolding dist_norm tendsto_norm_zero_iff by auto
himmelma@33175
  3547
qed
himmelma@33175
  3548
himmelma@33175
  3549
lemma uniformly_continuous_on_sub:
huffman@36437
  3550
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3551
  shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s g
himmelma@33175
  3552
           ==> uniformly_continuous_on s  (\<lambda>x. f x - g x)"
himmelma@33175
  3553
  unfolding ab_diff_minus
himmelma@33175
  3554
  using uniformly_continuous_on_add[of s f "\<lambda>x. - g x"]
himmelma@33175
  3555
  using uniformly_continuous_on_neg[of s g] by auto
himmelma@33175
  3556
himmelma@33175
  3557
text{* Identity function is continuous in every sense. *}
himmelma@33175
  3558
himmelma@33175
  3559
lemma continuous_within_id:
himmelma@33175
  3560
 "continuous (at a within s) (\<lambda>x. x)"
huffman@44983
  3561
  unfolding continuous_within by (rule Lim_at_within [OF LIM_ident])
himmelma@33175
  3562
himmelma@33175
  3563
lemma continuous_at_id:
himmelma@33175
  3564
 "continuous (at a) (\<lambda>x. x)"
huffman@44983
  3565
  unfolding continuous_at by (rule LIM_ident)
himmelma@33175
  3566
himmelma@33175
  3567
lemma continuous_on_id:
himmelma@33175
  3568
 "continuous_on s (\<lambda>x. x)"
huffman@36436
  3569
  unfolding continuous_on_def by (auto intro: tendsto_ident_at_within)
himmelma@33175
  3570
himmelma@33175
  3571
lemma uniformly_continuous_on_id:
himmelma@33175
  3572
 "uniformly_continuous_on s (\<lambda>x. x)"
himmelma@33175
  3573
  unfolding uniformly_continuous_on_def by auto
himmelma@33175
  3574
himmelma@33175
  3575
text{* Continuity of all kinds is preserved under composition. *}
himmelma@33175
  3576
huffman@36437
  3577
lemma continuous_within_topological:
huffman@36437
  3578
  "continuous (at x within s) f \<longleftrightarrow>
huffman@36437
  3579
    (\<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow>
huffman@36437
  3580
      (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"
huffman@36437
  3581
unfolding continuous_within
huffman@36437
  3582
unfolding tendsto_def Limits.eventually_within eventually_at_topological
huffman@36437
  3583
by (intro ball_cong [OF refl] all_cong imp_cong ex_cong conj_cong refl) auto
huffman@36437
  3584
himmelma@33175
  3585
lemma continuous_within_compose:
huffman@36437
  3586
  assumes "continuous (at x within s) f"
huffman@36437
  3587
  assumes "continuous (at (f x) within f ` s) g"
himmelma@33175
  3588
  shows "continuous (at x within s) (g o f)"
huffman@36437
  3589
using assms unfolding continuous_within_topological by simp metis
himmelma@33175
  3590
himmelma@33175
  3591
lemma continuous_at_compose:
himmelma@33175
  3592
  assumes "continuous (at x) f"  "continuous (at (f x)) g"
himmelma@33175
  3593
  shows "continuous (at x) (g o f)"
himmelma@33175
  3594
proof-
himmelma@33175
  3595
  have " continuous (at (f x) within range f) g" using assms(2) using continuous_within_subset[of "f x" UNIV g "range f", unfolded within_UNIV] by auto
himmelma@33175
  3596
  thus ?thesis using assms(1) using continuous_within_compose[of x UNIV f g, unfolded within_UNIV] by auto
himmelma@33175
  3597
qed
himmelma@33175
  3598
himmelma@33175
  3599
lemma continuous_on_compose:
huffman@36436
  3600
  "continuous_on s f \<Longrightarrow> continuous_on (f ` s) g \<Longrightarrow> continuous_on s (g o f)"
huffman@36436
  3601
  unfolding continuous_on_topological by simp metis
himmelma@33175
  3602
himmelma@33175
  3603
lemma uniformly_continuous_on_compose:
himmelma@33175
  3604
  assumes "uniformly_continuous_on s f"  "uniformly_continuous_on (f ` s) g"
himmelma@33175
  3605
  shows "uniformly_continuous_on s (g o f)"
himmelma@33175
  3606
proof-
himmelma@33175
  3607
  { fix e::real assume "e>0"
himmelma@33175
  3608
    then obtain d where "d>0" and d:"\<forall>x\<in>f ` s. \<forall>x'\<in>f ` s. dist x' x < d \<longrightarrow> dist (g x') (g x) < e" using assms(2) unfolding uniformly_continuous_on_def by auto
himmelma@33175
  3609
    obtain d' where "d'>0" "\<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d' \<longrightarrow> dist (f x') (f x) < d" using `d>0` using assms(1) unfolding uniformly_continuous_on_def by auto
himmelma@33175
  3610
    hence "\<exists>d>0. \<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist ((g \<circ> f) x') ((g \<circ> f) x) < e" using `d>0` using d by auto  }
himmelma@33175
  3611
  thus ?thesis using assms unfolding uniformly_continuous_on_def by auto
himmelma@33175
  3612
qed
himmelma@33175
  3613
himmelma@33175
  3614
text{* Continuity in terms of open preimages. *}
himmelma@33175
  3615
himmelma@33175
  3616
lemma continuous_at_open:
huffman@36437
  3617
  shows "continuous (at x) f \<longleftrightarrow> (\<forall>t. open t \<and> f x \<in> t --> (\<exists>s. open s \<and> x \<in> s \<and> (\<forall>x' \<in> s. (f x') \<in> t)))"
huffman@36437
  3618
unfolding continuous_within_topological [of x UNIV f, unfolded within_UNIV]
huffman@36437
  3619
unfolding imp_conjL by (intro all_cong imp_cong ex_cong conj_cong refl) auto
himmelma@33175
  3620
himmelma@33175
  3621
lemma continuous_on_open:
huffman@36437
  3622
  shows "continuous_on s f \<longleftrightarrow>
himmelma@33175
  3623
        (\<forall>t. openin (subtopology euclidean (f ` s)) t
himmelma@33175
  3624
            --> openin (subtopology euclidean s) {x \<in> s. f x \<in> t})" (is "?lhs = ?rhs")
huffman@36437
  3625
proof (safe)
huffman@36437
  3626
  fix t :: "'b set"
huffman@36437
  3627
  assume 1: "continuous_on s f"
huffman@36437
  3628
  assume 2: "openin (subtopology euclidean (f ` s)) t"
huffman@36437
  3629
  from 2 obtain B where B: "open B" and t: "t = f ` s \<inter> B"
huffman@36437
  3630
    unfolding openin_open by auto
huffman@36437
  3631
  def U == "\<Union>{A. open A \<and> (\<forall>x\<in>s. x \<in> A \<longrightarrow> f x \<in> B)}"
huffman@36437
  3632
  have "open U" unfolding U_def by (simp add: open_Union)
huffman@36437
  3633
  moreover have "\<forall>x\<in>s. x \<in> U \<longleftrightarrow> f x \<in> t"
huffman@36437
  3634
  proof (intro ballI iffI)
huffman@36437
  3635
    fix x assume "x \<in> s" and "x \<in> U" thus "f x \<in> t"
huffman@36437
  3636
      unfolding U_def t by auto
huffman@36437
  3637
  next
huffman@36437
  3638
    fix x assume "x \<in> s" and "f x \<in> t"
huffman@36437
  3639
    hence "x \<in> s" and "f x \<in> B"
huffman@36437
  3640
      unfolding t by auto
huffman@36437
  3641
    with 1 B obtain A where "open A" "x \<in> A" "\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B"
huffman@36437
  3642
      unfolding t continuous_on_topological by metis
huffman@36437
  3643
    then show "x \<in> U"
huffman@36437
  3644
      unfolding U_def by auto
huffman@36437
  3645
  qed
huffman@36437
  3646
  ultimately have "open U \<and> {x \<in> s. f x \<in> t} = s \<inter> U" by auto
huffman@36437
  3647
  then show "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
huffman@36437
  3648
    unfolding openin_open by fast
himmelma@33175
  3649
next
huffman@36437
  3650
  assume "?rhs" show "continuous_on s f"
huffman@36437
  3651
  unfolding continuous_on_topological
huffman@36437
  3652
  proof (clarify)
huffman@36437
  3653
    fix x and B assume "x \<in> s" and "open B" and "f x \<in> B"
huffman@36437
  3654
    have "openin (subtopology euclidean (f ` s)) (f ` s \<inter> B)"
huffman@36437
  3655
      unfolding openin_open using `open B` by auto
huffman@36437
  3656
    then have "openin (subtopology euclidean s) {x \<in> s. f x \<in> f ` s \<inter> B}"
huffman@36437
  3657
      using `?rhs` by fast
huffman@36437
  3658
    then show "\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)"
huffman@36437
  3659
      unfolding openin_open using `x \<in> s` and `f x \<in> B` by auto
huffman@36437
  3660
  qed
huffman@36437
  3661
qed
huffman@36437
  3662
huffman@36437
  3663
text {* Similarly in terms of closed sets. *}
himmelma@33175
  3664
himmelma@33175
  3665
lemma continuous_on_closed:
huffman@36355
  3666
  shows "continuous_on s f \<longleftrightarrow>  (\<forall>t. closedin (subtopology euclidean (f ` s)) t  --> closedin (subtopology euclidean s) {x \<in> s. f x \<in> t})" (is "?lhs = ?rhs")
himmelma@33175
  3667
proof
himmelma@33175
  3668
  assume ?lhs
himmelma@33175
  3669
  { fix t
himmelma@33175
  3670
    have *:"s - {x \<in> s. f x \<in> f ` s - t} = {x \<in> s. f x \<in> t}" by auto
himmelma@33175
  3671
    have **:"f ` s - (f ` s - (f ` s - t)) = f ` s - t" by auto
himmelma@33175
  3672
    assume as:"closedin (subtopology euclidean (f ` s)) t"
himmelma@33175
  3673
    hence "closedin (subtopology euclidean (f ` s)) (f ` s - (f ` s - t))" unfolding closedin_def topspace_euclidean_subtopology unfolding ** by auto
himmelma@33175
  3674
    hence "closedin (subtopology euclidean s) {x \<in> s. f x \<in> t}" using `?lhs`[unfolded continuous_on_open, THEN spec[where x="(f ` s) - t"]]
himmelma@33175
  3675
      unfolding openin_closedin_eq topspace_euclidean_subtopology unfolding * by auto  }
himmelma@33175
  3676
  thus ?rhs by auto
himmelma@33175
  3677
next
himmelma@33175
  3678
  assume ?rhs
himmelma@33175
  3679
  { fix t
himmelma@33175
  3680
    have *:"s - {x \<in> s. f x \<in> f ` s - t} = {x \<in> s. f x \<in> t}" by auto
himmelma@33175
  3681
    assume as:"openin (subtopology euclidean (f ` s)) t"
himmelma@33175
  3682
    hence "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}" using `?rhs`[THEN spec[where x="(f ` s) - t"]]
himmelma@33175
  3683
      unfolding openin_closedin_eq topspace_euclidean_subtopology *[THEN sym] closedin_subtopology by auto }
himmelma@33175
  3684
  thus ?lhs unfolding continuous_on_open by auto
himmelma@33175
  3685
qed
himmelma@33175
  3686
huffman@45081
  3687
text {* Half-global and completely global cases. *}
himmelma@33175
  3688
himmelma@33175
  3689
lemma continuous_open_in_preimage:
himmelma@33175
  3690
  assumes "continuous_on s f"  "open t"
himmelma@33175
  3691
  shows "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
himmelma@33175
  3692
proof-
himmelma@33175
  3693
  have *:"\<forall>x. x \<in> s \<and> f x \<in> t \<longleftrightarrow> x \<in> s \<and> f x \<in> (t \<inter> f ` s)" by auto
himmelma@33175
  3694
  have "openin (subtopology euclidean (f ` s)) (t \<inter> f ` s)"
himmelma@33175
  3695
    using openin_open_Int[of t "f ` s", OF assms(2)] unfolding openin_open by auto
himmelma@33175
  3696
  thus ?thesis using assms(1)[unfolded continuous_on_open, THEN spec[where x="t \<inter> f ` s"]] using * by auto
himmelma@33175
  3697
qed
himmelma@33175
  3698
himmelma@33175
  3699
lemma continuous_closed_in_preimage:
himmelma@33175
  3700
  assumes "continuous_on s f"  "closed t"
himmelma@33175
  3701
  shows "closedin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
himmelma@33175
  3702
proof-
himmelma@33175
  3703
  have *:"\<forall>x. x \<in> s \<and> f x \<in> t \<longleftrightarrow> x \<in> s \<and> f x \<in> (t \<inter> f ` s)" by auto
himmelma@33175
  3704
  have "closedin (subtopology euclidean (f ` s)) (t \<inter> f ` s)"
himmelma@33175
  3705
    using closedin_closed_Int[of t "f ` s", OF assms(2)] unfolding Int_commute by auto
himmelma@33175
  3706
  thus ?thesis
himmelma@33175
  3707
    using assms(1)[unfolded continuous_on_closed, THEN spec[where x="t \<inter> f ` s"]] using * by auto
himmelma@33175
  3708
qed
himmelma@33175
  3709
himmelma@33175
  3710
lemma continuous_open_preimage:
himmelma@33175
  3711
  assumes "continuous_on s f" "open s" "open t"
himmelma@33175
  3712
  shows "open {x \<in> s. f x \<in> t}"
himmelma@33175
  3713
proof-
himmelma@33175
  3714
  obtain T where T: "open T" "{x \<in> s. f x \<in> t} = s \<inter> T"
himmelma@33175
  3715
    using continuous_open_in_preimage[OF assms(1,3)] unfolding openin_open by auto
himmelma@33175
  3716
  thus ?thesis using open_Int[of s T, OF assms(2)] by auto
himmelma@33175
  3717
qed
himmelma@33175
  3718
himmelma@33175
  3719
lemma continuous_closed_preimage:
himmelma@33175
  3720
  assumes "continuous_on s f" "closed s" "closed t"
himmelma@33175
  3721
  shows "closed {x \<in> s. f x \<in> t}"
himmelma@33175
  3722
proof-
himmelma@33175
  3723
  obtain T where T: "closed T" "{x \<in> s. f x \<in> t} = s \<inter> T"
himmelma@33175
  3724
    using continuous_closed_in_preimage[OF assms(1,3)] unfolding closedin_closed by auto
himmelma@33175
  3725
  thus ?thesis using closed_Int[of s T, OF assms(2)] by auto
himmelma@33175
  3726
qed
himmelma@33175
  3727
himmelma@33175
  3728
lemma continuous_open_preimage_univ:
himmelma@33175
  3729
  shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open {x. f x \<in> s}"
himmelma@33175
  3730
  using continuous_open_preimage[of UNIV f s] open_UNIV continuous_at_imp_continuous_on by auto
himmelma@33175
  3731
himmelma@33175
  3732
lemma continuous_closed_preimage_univ:
himmelma@33175
  3733
  shows "(\<forall>x. continuous (at x) f) \<Longrightarrow> closed s ==> closed {x. f x \<in> s}"
himmelma@33175
  3734
  using continuous_closed_preimage[of UNIV f s] closed_UNIV continuous_at_imp_continuous_on by auto
himmelma@33175
  3735
himmelma@33175
  3736
lemma continuous_open_vimage:
himmelma@33175
  3737
  shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open (f -` s)"
himmelma@33175
  3738
  unfolding vimage_def by (rule continuous_open_preimage_univ)
himmelma@33175
  3739
himmelma@33175
  3740
lemma continuous_closed_vimage:
himmelma@33175
  3741
  shows "\<forall>x. continuous (at x) f \<Longrightarrow> closed s \<Longrightarrow> closed (f -` s)"
himmelma@33175
  3742
  unfolding vimage_def by (rule continuous_closed_preimage_univ)
himmelma@33175
  3743
huffman@36437
  3744
lemma interior_image_subset:
himmelma@35172
  3745
  assumes "\<forall>x. continuous (at x) f" "inj f"
himmelma@35172
  3746
  shows "interior (f ` s) \<subseteq> f ` (interior s)"
himmelma@35172
  3747
  apply rule unfolding interior_def mem_Collect_eq image_iff apply safe
himmelma@35172
  3748
proof- fix x T assume as:"open T" "x \<in> T" "T \<subseteq> f ` s" 
himmelma@35172
  3749
  hence "x \<in> f ` s" by auto then guess y unfolding image_iff .. note y=this
himmelma@35172
  3750
  thus "\<exists>xa\<in>{x. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> s}. x = f xa" apply(rule_tac x=y in bexI) using assms as
himmelma@35172
  3751
    apply safe apply(rule_tac x="{x. f x \<in> T}" in exI) apply(safe,rule continuous_open_preimage_univ)
himmelma@35172
  3752
  proof- fix x assume "f x \<in> T" hence "f x \<in> f ` s" using as by auto
himmelma@35172
  3753
    thus "x \<in> s" unfolding inj_image_mem_iff[OF assms(2)] . qed auto qed
himmelma@35172
  3754
huffman@45081
  3755
text {* Equality of continuous functions on closure and related results. *}
himmelma@33175
  3756
himmelma@33175
  3757
lemma continuous_closed_in_preimage_constant:
huffman@36668
  3758
  fixes f :: "_ \<Rightarrow> 'b::t1_space"
huffman@36355
  3759
  shows "continuous_on s f ==> closedin (subtopology euclidean s) {x \<in> s. f x = a}"
huffman@36668
  3760
  using continuous_closed_in_preimage[of s f "{a}"] by auto
himmelma@33175
  3761
himmelma@33175
  3762
lemma continuous_closed_preimage_constant:
huffman@36668
  3763
  fixes f :: "_ \<Rightarrow> 'b::t1_space"
huffman@36355
  3764
  shows "continuous_on s f \<Longrightarrow> closed s ==> closed {x \<in> s. f x = a}"
huffman@36668
  3765
  using continuous_closed_preimage[of s f "{a}"] by auto
himmelma@33175
  3766
himmelma@33175
  3767
lemma continuous_constant_on_closure:
huffman@36668
  3768
  fixes f :: "_ \<Rightarrow> 'b::t1_space"
himmelma@33175
  3769
  assumes "continuous_on (closure s) f"
himmelma@33175
  3770
          "\<forall>x \<in> s. f x = a"
himmelma@33175
  3771
  shows "\<forall>x \<in> (closure s). f x = a"
himmelma@33175
  3772
    using continuous_closed_preimage_constant[of "closure s" f a]
himmelma@33175
  3773
    assms closure_minimal[of s "{x \<in> closure s. f x = a}"] closure_subset unfolding subset_eq by auto
himmelma@33175
  3774
himmelma@33175
  3775
lemma image_closure_subset:
himmelma@33175
  3776
  assumes "continuous_on (closure s) f"  "closed t"  "(f ` s) \<subseteq> t"
himmelma@33175
  3777
  shows "f ` (closure s) \<subseteq> t"
himmelma@33175
  3778
proof-
himmelma@33175
  3779
  have "s \<subseteq> {x \<in> closure s. f x \<in> t}" using assms(3) closure_subset by auto
himmelma@33175
  3780
  moreover have "closed {x \<in> closure s. f x \<in> t}"
himmelma@33175
  3781
    using continuous_closed_preimage[OF assms(1)] and assms(2) by auto
himmelma@33175
  3782
  ultimately have "closure s = {x \<in> closure s . f x \<in> t}"
himmelma@33175
  3783
    using closure_minimal[of s "{x \<in> closure s. f x \<in> t}"] by auto
himmelma@33175
  3784
  thus ?thesis by auto
himmelma@33175
  3785
qed
himmelma@33175
  3786
himmelma@33175
  3787
lemma continuous_on_closure_norm_le:
himmelma@33175
  3788
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
himmelma@33175
  3789
  assumes "continuous_on (closure s) f"  "\<forall>y \<in> s. norm(f y) \<le> b"  "x \<in> (closure s)"
himmelma@33175
  3790
  shows "norm(f x) \<le> b"
himmelma@33175
  3791
proof-
himmelma@33175
  3792
  have *:"f ` s \<subseteq> cball 0 b" using assms(2)[unfolded mem_cball_0[THEN sym]] by auto
himmelma@33175
  3793
  show ?thesis
himmelma@33175
  3794
    using image_closure_subset[OF assms(1) closed_cball[of 0 b] *] assms(3)
himmelma@33175
  3795
    unfolding subset_eq apply(erule_tac x="f x" in ballE) by (auto simp add: dist_norm)
himmelma@33175
  3796
qed
himmelma@33175
  3797
huffman@45081
  3798
text {* Making a continuous function avoid some value in a neighbourhood. *}
himmelma@33175
  3799
himmelma@33175
  3800
lemma continuous_within_avoid:
himmelma@33175
  3801
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
himmelma@33175
  3802
  assumes "continuous (at x within s) f"  "x \<in> s"  "f x \<noteq> a"
himmelma@33175
  3803
  shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e --> f y \<noteq> a"
himmelma@33175
  3804
proof-
himmelma@33175
  3805
  obtain d where "d>0" and d:"\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < dist (f x) a"
himmelma@33175
  3806
    using assms(1)[unfolded continuous_within Lim_within, THEN spec[where x="dist (f x) a"]] assms(3)[unfolded dist_nz] by auto
himmelma@33175
  3807
  { fix y assume " y\<in>s"  "dist x y < d"
himmelma@33175
  3808
    hence "f y \<noteq> a" using d[THEN bspec[where x=y]] assms(3)[unfolded dist_nz]
himmelma@33175
  3809
      apply auto unfolding dist_nz[THEN sym] by (auto simp add: dist_commute) }
himmelma@33175
  3810
  thus ?thesis using `d>0` by auto
himmelma@33175
  3811
qed
himmelma@33175
  3812
himmelma@33175
  3813
lemma continuous_at_avoid:
himmelma@33175
  3814
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
himmelma@33175
  3815
  assumes "continuous (at x) f"  "f x \<noteq> a"
himmelma@33175
  3816
  shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"
himmelma@33175
  3817
using assms using continuous_within_avoid[of x UNIV f a, unfolded within_UNIV] by auto
himmelma@33175
  3818
himmelma@33175
  3819
lemma continuous_on_avoid:
huffman@36355
  3820
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* TODO: generalize *)
himmelma@33175
  3821
  assumes "continuous_on s f"  "x \<in> s"  "f x \<noteq> a"
himmelma@33175
  3822
  shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e \<longrightarrow> f y \<noteq> a"
himmelma@33175
  3823
using assms(1)[unfolded continuous_on_eq_continuous_within, THEN bspec[where x=x], OF assms(2)]  continuous_within_avoid[of x s f a]  assms(2,3) by auto
himmelma@33175
  3824
himmelma@33175
  3825
lemma continuous_on_open_avoid:
huffman@36355
  3826
  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* TODO: generalize *)
himmelma@33175
  3827
  assumes "continuous_on s f"  "open s"  "x \<in> s"  "f x \<noteq> a"
himmelma@33175
  3828
  shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"
himmelma@33175
  3829
using assms(1)[unfolded continuous_on_eq_continuous_at[OF assms(2)], THEN bspec[where x=x], OF assms(3)]  continuous_at_avoid[of x f a]  assms(3,4) by auto
himmelma@33175
  3830
huffman@45081
  3831
text {* Proving a function is constant by proving open-ness of level set. *}
himmelma@33175
  3832
himmelma@33175
  3833
lemma continuous_levelset_open_in_cases:
huffman@36668
  3834
  fixes f :: "_ \<Rightarrow> 'b::t1_space"
huffman@36355
  3835
  shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>
himmelma@33175
  3836
        openin (subtopology euclidean s) {x \<in> s. f x = a}
himmelma@33175
  3837
        ==> (\<forall>x \<in> s. f x \<noteq> a) \<or> (\<forall>x \<in> s. f x = a)"
himmelma@33175
  3838
unfolding connected_clopen using continuous_closed_in_preimage_constant by auto
himmelma@33175
  3839
himmelma@33175
  3840
lemma continuous_levelset_open_in:
huffman@36668
  3841
  fixes f :: "_ \<Rightarrow> 'b::t1_space"
huffman@36355
  3842
  shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>
himmelma@33175
  3843
        openin (subtopology euclidean s) {x \<in> s. f x = a} \<Longrightarrow>
himmelma@33175
  3844
        (\<exists>x \<in> s. f x = a)  ==> (\<forall>x \<in> s. f x = a)"
himmelma@33175
  3845
using continuous_levelset_open_in_cases[of s f ]
himmelma@33175
  3846
by meson
himmelma@33175
  3847
himmelma@33175
  3848
lemma continuous_levelset_open:
huffman@36668
  3849
  fixes f :: "_ \<Rightarrow> 'b::t1_space"
himmelma@33175
  3850
  assumes "connected s"  "continuous_on s f"  "open {x \<in> s. f x = a}"  "\<exists>x \<in> s.  f x = a"
himmelma@33175
  3851
  shows "\<forall>x \<in> s. f x = a"
huffman@36358
  3852
using continuous_levelset_open_in[OF assms(1,2), of a, unfolded openin_open] using assms (3,4) by fast
himmelma@33175
  3853
huffman@45081
  3854
text {* Some arithmetical combinations (more to prove). *}
himmelma@33175
  3855
himmelma@33175
  3856
lemma open_scaling[intro]:
himmelma@33175
  3857
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  3858
  assumes "c \<noteq> 0"  "open s"
himmelma@33175
  3859
  shows "open((\<lambda>x. c *\<^sub>R x) ` s)"
himmelma@33175
  3860
proof-
himmelma@33175
  3861
  { fix x assume "x \<in> s"
himmelma@33175
  3862
    then obtain e where "e>0" and e:"\<forall>x'. dist x' x < e \<longrightarrow> x' \<in> s" using assms(2)[unfolded open_dist, THEN bspec[where x=x]] by auto
huffman@36770
  3863
    have "e * abs c > 0" using assms(1)[unfolded zero_less_abs_iff[THEN sym]] using mult_pos_pos[OF `e>0`] by auto
himmelma@33175
  3864
    moreover
himmelma@33175
  3865
    { fix y assume "dist y (c *\<^sub>R x) < e * \<bar>c\<bar>"
himmelma@33175
  3866
      hence "norm ((1 / c) *\<^sub>R y - x) < e" unfolding dist_norm
himmelma@33175
  3867
        using norm_scaleR[of c "(1 / c) *\<^sub>R y - x", unfolded scaleR_right_diff_distrib, unfolded scaleR_scaleR] assms(1)
himmelma@33175
  3868
          assms(1)[unfolded zero_less_abs_iff[THEN sym]] by (simp del:zero_less_abs_iff)
himmelma@33175
  3869
      hence "y \<in> op *\<^sub>R c ` s" using rev_image_eqI[of "(1 / c) *\<^sub>R y" s y "op *\<^sub>R c"]  e[THEN spec[where x="(1 / c) *\<^sub>R y"]]  assms(1) unfolding dist_norm scaleR_scaleR by auto  }
himmelma@33175
  3870
    ultimately have "\<exists>e>0. \<forall>x'. dist x' (c *\<^sub>R x) < e \<longrightarrow> x' \<in> op *\<^sub>R c ` s" apply(rule_tac x="e * abs c" in exI) by auto  }
himmelma@33175
  3871
  thus ?thesis unfolding open_dist by auto
himmelma@33175
  3872
qed
himmelma@33175
  3873
himmelma@33175
  3874
lemma minus_image_eq_vimage:
himmelma@33175
  3875
  fixes A :: "'a::ab_group_add set"
himmelma@33175
  3876
  shows "(\<lambda>x. - x) ` A = (\<lambda>x. - x) -` A"
himmelma@33175
  3877
  by (auto intro!: image_eqI [where f="\<lambda>x. - x"])
himmelma@33175
  3878
himmelma@33175
  3879
lemma open_negations:
himmelma@33175
  3880
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  3881
  shows "open s ==> open ((\<lambda> x. -x) ` s)"
himmelma@33175
  3882
  unfolding scaleR_minus1_left [symmetric]
himmelma@33175
  3883
  by (rule open_scaling, auto)
himmelma@33175
  3884
himmelma@33175
  3885
lemma open_translation:
himmelma@33175
  3886
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  3887
  assumes "open s"  shows "open((\<lambda>x. a + x) ` s)"
himmelma@33175
  3888
proof-
himmelma@33175
  3889
  { fix x have "continuous (at x) (\<lambda>x. x - a)" using continuous_sub[of "at x" "\<lambda>x. x" "\<lambda>x. a"] continuous_at_id[of x] continuous_const[of "at x" a] by auto  }
himmelma@33175
  3890
  moreover have "{x. x - a \<in> s}  = op + a ` s" apply auto unfolding image_iff apply(rule_tac x="x - a" in bexI) by auto
himmelma@33175
  3891
  ultimately show ?thesis using continuous_open_preimage_univ[of "\<lambda>x. x - a" s] using assms by auto
himmelma@33175
  3892
qed
himmelma@33175
  3893
himmelma@33175
  3894
lemma open_affinity:
himmelma@33175
  3895
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  3896
  assumes "open s"  "c \<noteq> 0"
himmelma@33175
  3897
  shows "open ((\<lambda>x. a + c *\<^sub>R x) ` s)"
himmelma@33175
  3898
proof-
himmelma@33175
  3899
  have *:"(\<lambda>x. a + c *\<^sub>R x) = (\<lambda>x. a + x) \<circ> (\<lambda>x. c *\<^sub>R x)" unfolding o_def ..
himmelma@33175
  3900
  have "op + a ` op *\<^sub>R c ` s = (op + a \<circ> op *\<^sub>R c) ` s" by auto
himmelma@33175
  3901
  thus ?thesis using assms open_translation[of "op *\<^sub>R c ` s" a] unfolding * by auto
himmelma@33175
  3902
qed
himmelma@33175
  3903
himmelma@33175
  3904
lemma interior_translation:
himmelma@33175
  3905
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  3906
  shows "interior ((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (interior s)"
nipkow@39535
  3907
proof (rule set_eqI, rule)
himmelma@33175
  3908
  fix x assume "x \<in> interior (op + a ` s)"
himmelma@33175
  3909
  then obtain e where "e>0" and e:"ball x e \<subseteq> op + a ` s" unfolding mem_interior by auto
himmelma@33175
  3910
  hence "ball (x - a) e \<subseteq> s" unfolding subset_eq Ball_def mem_ball dist_norm apply auto apply(erule_tac x="a + xa" in allE) unfolding ab_group_add_class.diff_diff_eq[THEN sym] by auto
himmelma@33175
  3911
  thus "x \<in> op + a ` interior s" unfolding image_iff apply(rule_tac x="x - a" in bexI) unfolding mem_interior using `e > 0` by auto
himmelma@33175
  3912
next
himmelma@33175
  3913
  fix x assume "x \<in> op + a ` interior s"
himmelma@33175
  3914
  then obtain y e where "e>0" and e:"ball y e \<subseteq> s" and y:"x = a + y" unfolding image_iff Bex_def mem_interior by auto
himmelma@33175
  3915
  { fix z have *:"a + y - z = y + a - z" by auto
himmelma@33175
  3916
    assume "z\<in>ball x e"
himmelma@33175
  3917
    hence "z - a \<in> s" using e[unfolded subset_eq, THEN bspec[where x="z - a"]] unfolding mem_ball dist_norm y ab_group_add_class.diff_diff_eq2 * by auto
himmelma@33175
  3918
    hence "z \<in> op + a ` s" unfolding image_iff by(auto intro!: bexI[where x="z - a"])  }
himmelma@33175
  3919
  hence "ball x e \<subseteq> op + a ` s" unfolding subset_eq by auto
himmelma@33175
  3920
  thus "x \<in> interior (op + a ` s)" unfolding mem_interior using `e>0` by auto
himmelma@33175
  3921
qed
himmelma@33175
  3922
huffman@45081
  3923
text {* We can now extend limit compositions to consider the scalar multiplier. *}
huffman@36433
  3924
huffman@36433
  3925
lemma continuous_vmul:
huffman@36433
  3926
  fixes c :: "'a::metric_space \<Rightarrow> real" and v :: "'b::real_normed_vector"
huffman@36433
  3927
  shows "continuous net c ==> continuous net (\<lambda>x. c(x) *\<^sub>R v)"
huffman@44983
  3928
  unfolding continuous_def by (intro tendsto_intros)
huffman@36433
  3929
huffman@36433
  3930
lemma continuous_mul:
huffman@36433
  3931
  fixes c :: "'a::metric_space \<Rightarrow> real"
huffman@36433
  3932
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
huffman@36433
  3933
  shows "continuous net c \<Longrightarrow> continuous net f
huffman@36433
  3934
             ==> continuous net (\<lambda>x. c(x) *\<^sub>R f x) "
huffman@36433
  3935
  unfolding continuous_def by (intro tendsto_intros)
huffman@36433
  3936
hoelzl@43036
  3937
lemmas continuous_intros = continuous_add continuous_vmul continuous_cmul
hoelzl@43036
  3938
  continuous_const continuous_sub continuous_at_id continuous_within_id continuous_mul
huffman@36433
  3939
huffman@36433
  3940
lemma continuous_on_vmul:
huffman@36433
  3941
  fixes c :: "'a::metric_space \<Rightarrow> real" and v :: "'b::real_normed_vector"
huffman@36433
  3942
  shows "continuous_on s c ==> continuous_on s (\<lambda>x. c(x) *\<^sub>R v)"
huffman@36433
  3943
  unfolding continuous_on_eq_continuous_within using continuous_vmul[of _ c] by auto
huffman@36433
  3944
huffman@36433
  3945
lemma continuous_on_mul:
huffman@36433
  3946
  fixes c :: "'a::metric_space \<Rightarrow> real"
huffman@36433
  3947
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
huffman@36433
  3948
  shows "continuous_on s c \<Longrightarrow> continuous_on s f
huffman@36433
  3949
             ==> continuous_on s (\<lambda>x. c(x) *\<^sub>R f x)"
huffman@36433
  3950
  unfolding continuous_on_eq_continuous_within using continuous_mul[of _ c] by auto
huffman@36433
  3951
hoelzl@43036
  3952
lemma continuous_on_mul_real:
hoelzl@43036
  3953
  fixes f :: "'a::metric_space \<Rightarrow> real"
hoelzl@43036
  3954
  fixes g :: "'a::metric_space \<Rightarrow> real"
hoelzl@43036
  3955
  shows "continuous_on s f \<Longrightarrow> continuous_on s g
hoelzl@43036
  3956
             ==> continuous_on s (\<lambda>x. f x * g x)"
hoelzl@43036
  3957
  using continuous_on_mul[of s f g] unfolding real_scaleR_def .
hoelzl@43036
  3958
hoelzl@43036
  3959
lemmas continuous_on_intros = continuous_on_add continuous_on_const
hoelzl@43036
  3960
  continuous_on_id continuous_on_compose continuous_on_cmul continuous_on_neg
hoelzl@43036
  3961
  continuous_on_sub continuous_on_mul continuous_on_vmul continuous_on_mul_real
hoelzl@43036
  3962
  uniformly_continuous_on_add uniformly_continuous_on_const
hoelzl@43036
  3963
  uniformly_continuous_on_id uniformly_continuous_on_compose
hoelzl@43036
  3964
  uniformly_continuous_on_cmul uniformly_continuous_on_neg
hoelzl@43036
  3965
  uniformly_continuous_on_sub
huffman@36433
  3966
huffman@45081
  3967
text {* And so we have continuity of inverse. *}
huffman@36433
  3968
huffman@36433
  3969
lemma continuous_inv:
huffman@36433
  3970
  fixes f :: "'a::metric_space \<Rightarrow> real"
huffman@36433
  3971
  shows "continuous net f \<Longrightarrow> f(netlimit net) \<noteq> 0
huffman@36433
  3972
           ==> continuous net (inverse o f)"
huffman@36433
  3973
  unfolding continuous_def using Lim_inv by auto
huffman@36433
  3974
huffman@36433
  3975
lemma continuous_at_within_inv:
huffman@36433
  3976
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_field"
huffman@36433
  3977
  assumes "continuous (at a within s) f" "f a \<noteq> 0"
huffman@36433
  3978
  shows "continuous (at a within s) (inverse o f)"
huffman@36433
  3979
  using assms unfolding continuous_within o_def
huffman@36433
  3980
  by (intro tendsto_intros)
huffman@36433
  3981
huffman@36433
  3982
lemma continuous_at_inv:
huffman@36433
  3983
  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_field"
huffman@36433
  3984
  shows "continuous (at a) f \<Longrightarrow> f a \<noteq> 0
huffman@36433
  3985
         ==> continuous (at a) (inverse o f) "
huffman@36433
  3986
  using within_UNIV[THEN sym, of "at a"] using continuous_at_within_inv[of a UNIV] by auto
huffman@36433
  3987
huffman@36433
  3988
text {* Topological properties of linear functions. *}
huffman@36433
  3989
huffman@36433
  3990
lemma linear_lim_0:
huffman@36433
  3991
  assumes "bounded_linear f" shows "(f ---> 0) (at (0))"
huffman@36433
  3992
proof-
huffman@36433
  3993
  interpret f: bounded_linear f by fact
huffman@36433
  3994
  have "(f ---> f 0) (at 0)"
huffman@36433
  3995
    using tendsto_ident_at by (rule f.tendsto)
huffman@36433
  3996
  thus ?thesis unfolding f.zero .
huffman@36433
  3997
qed
huffman@36433
  3998
huffman@36433
  3999
lemma linear_continuous_at:
huffman@36433
  4000
  assumes "bounded_linear f"  shows "continuous (at a) f"
huffman@36433
  4001
  unfolding continuous_at using assms
huffman@36433
  4002
  apply (rule bounded_linear.tendsto)
huffman@36433
  4003
  apply (rule tendsto_ident_at)
huffman@36433
  4004
  done
huffman@36433
  4005
huffman@36433
  4006
lemma linear_continuous_within:
huffman@36433
  4007
  shows "bounded_linear f ==> continuous (at x within s) f"
huffman@36433
  4008
  using continuous_at_imp_continuous_within[of x f s] using linear_continuous_at[of f] by auto
huffman@36433
  4009
huffman@36433
  4010
lemma linear_continuous_on:
huffman@36433
  4011
  shows "bounded_linear f ==> continuous_on s f"
huffman@36433
  4012
  using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto
huffman@36433
  4013
huffman@45081
  4014
text {* Also bilinear functions, in composition form. *}
huffman@36433
  4015
huffman@36433
  4016
lemma bilinear_continuous_at_compose:
huffman@36433
  4017
  shows "continuous (at x) f \<Longrightarrow> continuous (at x) g \<Longrightarrow> bounded_bilinear h
huffman@36433
  4018
        ==> continuous (at x) (\<lambda>x. h (f x) (g x))"
huffman@36433
  4019
  unfolding continuous_at using Lim_bilinear[of f "f x" "(at x)" g "g x" h] by auto
huffman@36433
  4020
huffman@36433
  4021
lemma bilinear_continuous_within_compose:
huffman@36433
  4022
  shows "continuous (at x within s) f \<Longrightarrow> continuous (at x within s) g \<Longrightarrow> bounded_bilinear h
huffman@36433
  4023
        ==> continuous (at x within s) (\<lambda>x. h (f x) (g x))"
huffman@36433
  4024
  unfolding continuous_within using Lim_bilinear[of f "f x"] by auto
huffman@36433
  4025
huffman@36433
  4026
lemma bilinear_continuous_on_compose:
huffman@36433
  4027
  shows "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> bounded_bilinear h
huffman@36433
  4028
             ==> continuous_on s (\<lambda>x. h (f x) (g x))"
huffman@36437
  4029
  unfolding continuous_on_def
huffman@36437
  4030
  by (fast elim: bounded_bilinear.tendsto)
huffman@36433
  4031
huffman@45081
  4032
text {* Preservation of compactness and connectedness under continuous function. *}
himmelma@33175
  4033
himmelma@33175
  4034
lemma compact_continuous_image:
himmelma@33175
  4035
  assumes "continuous_on s f"  "compact s"
himmelma@33175
  4036
  shows "compact(f ` s)"
himmelma@33175
  4037
proof-
himmelma@33175
  4038
  { fix x assume x:"\<forall>n::nat. x n \<in> f ` s"
himmelma@33175
  4039
    then obtain y where y:"\<forall>n. y n \<in> s \<and> x n = f (y n)" unfolding image_iff Bex_def using choice[of "\<lambda>n xa. xa \<in> s \<and> x n = f xa"] by auto
himmelma@33175
  4040
    then obtain l r where "l\<in>s" and r:"subseq r" and lr:"((y \<circ> r) ---> l) sequentially" using assms(2)[unfolded compact_def, THEN spec[where x=y]] by auto
himmelma@33175
  4041
    { fix e::real assume "e>0"
huffman@36355
  4042
      then obtain d where "d>0" and d:"\<forall>x'\<in>s. dist x' l < d \<longrightarrow> dist (f x') (f l) < e" using assms(1)[unfolded continuous_on_iff, THEN bspec[where x=l], OF `l\<in>s`] by auto
himmelma@33175
  4043
      then obtain N::nat where N:"\<forall>n\<ge>N. dist ((y \<circ> r) n) l < d" using lr[unfolded Lim_sequentially, THEN spec[where x=d]] by auto
himmelma@33175
  4044
      { fix n::nat assume "n\<ge>N" hence "dist ((x \<circ> r) n) (f l) < e" using N[THEN spec[where x=n]] d[THEN bspec[where x="y (r n)"]] y[THEN spec[where x="r n"]] by auto  }
himmelma@33175
  4045
      hence "\<exists>N. \<forall>n\<ge>N. dist ((x \<circ> r) n) (f l) < e" by auto  }
himmelma@33175
  4046
    hence "\<exists>l\<in>f ` s. \<exists>r. subseq r \<and> ((x \<circ> r) ---> l) sequentially" unfolding Lim_sequentially using r lr `l\<in>s` by auto  }
himmelma@33175
  4047
  thus ?thesis unfolding compact_def by auto
himmelma@33175
  4048
qed
himmelma@33175
  4049
himmelma@33175
  4050
lemma connected_continuous_image:
himmelma@33175
  4051
  assumes "continuous_on s f"  "connected s"
himmelma@33175
  4052
  shows "connected(f ` s)"
himmelma@33175
  4053
proof-
himmelma@33175
  4054
  { fix T assume as: "T \<noteq> {}"  "T \<noteq> f ` s"  "openin (subtopology euclidean (f ` s)) T"  "closedin (subtopology euclidean (f ` s)) T"
himmelma@33175
  4055
    have "{x \<in> s. f x \<in> T} = {} \<or> {x \<in> s. f x \<in> T} = s"
himmelma@33175
  4056
      using assms(1)[unfolded continuous_on_open, THEN spec[where x=T]]
himmelma@33175
  4057
      using assms(1)[unfolded continuous_on_closed, THEN spec[where x=T]]
himmelma@33175
  4058
      using assms(2)[unfolded connected_clopen, THEN spec[where x="{x \<in> s. f x \<in> T}"]] as(3,4) by auto
himmelma@33175
  4059
    hence False using as(1,2)
himmelma@33175
  4060
      using as(4)[unfolded closedin_def topspace_euclidean_subtopology] by auto }
himmelma@33175
  4061
  thus ?thesis unfolding connected_clopen by auto
himmelma@33175
  4062
qed
himmelma@33175
  4063
huffman@45081
  4064
text {* Continuity implies uniform continuity on a compact domain. *}
himmelma@33175
  4065
himmelma@33175
  4066
lemma compact_uniformly_continuous:
himmelma@33175
  4067
  assumes "continuous_on s f"  "compact s"
himmelma@33175
  4068
  shows "uniformly_continuous_on s f"
himmelma@33175
  4069
proof-
himmelma@33175
  4070
    { fix x assume x:"x\<in>s"
huffman@36355
  4071
      hence "\<forall>xa. \<exists>y. 0 < xa \<longrightarrow> (y > 0 \<and> (\<forall>x'\<in>s. dist x' x < y \<longrightarrow> dist (f x') (f x) < xa))" using assms(1)[unfolded continuous_on_iff, THEN bspec[where x=x]] by auto
himmelma@33175
  4072
      hence "\<exists>fa. \<forall>xa>0. \<forall>x'\<in>s. fa xa > 0 \<and> (dist x' x < fa xa \<longrightarrow> dist (f x') (f x) < xa)" using choice[of "\<lambda>e d. e>0 \<longrightarrow> d>0 \<and>(\<forall>x'\<in>s. (dist x' x < d \<longrightarrow> dist (f x') (f x) < e))"] by auto  }
himmelma@33175
  4073
    then have "\<forall>x\<in>s. \<exists>y. \<forall>xa. 0 < xa \<longrightarrow> (\<forall>x'\<in>s. y xa > 0 \<and> (dist x' x < y xa \<longrightarrow> dist (f x') (f x) < xa))" by auto
himmelma@33175
  4074
    then obtain d where d:"\<forall>e>0. \<forall>x\<in>s. \<forall>x'\<in>s. d x e > 0 \<and> (dist x' x < d x e \<longrightarrow> dist (f x') (f x) < e)"
himmelma@33175
  4075
      using bchoice[of s "\<lambda>x fa. \<forall>xa>0. \<forall>x'\<in>s. fa xa > 0 \<and> (dist x' x < fa xa \<longrightarrow> dist (f x') (f x) < xa)"] by blast
himmelma@33175
  4076
himmelma@33175
  4077
  { fix e::real assume "e>0"
himmelma@33175
  4078
himmelma@33175
  4079
    { fix x assume "x\<in>s" hence "x \<in> ball x (d x (e / 2))" unfolding centre_in_ball using d[THEN spec[where x="e/2"]] using `e>0` by auto  }
himmelma@33175
  4080
    hence "s \<subseteq> \<Union>{ball x (d x (e / 2)) |x. x \<in> s}" unfolding subset_eq by auto
himmelma@33175
  4081
    moreover
himmelma@33175
  4082
    { fix b assume "b\<in>{ball x (d x (e / 2)) |x. x \<in> s}" hence "open b" by auto  }
himmelma@33175
  4083
    ultimately obtain ea where "ea>0" and ea:"\<forall>x\<in>s. \<exists>b\<in>{ball x (d x (e / 2)) |x. x \<in> s}. ball x ea \<subseteq> b" using heine_borel_lemma[OF assms(2), of "{ball x (d x (e / 2)) | x. x\<in>s }"] by auto
himmelma@33175
  4084
himmelma@33175
  4085
    { fix x y assume "x\<in>s" "y\<in>s" and as:"dist y x < ea"
himmelma@33175
  4086
      obtain z where "z\<in>s" and z:"ball x ea \<subseteq> ball z (d z (e / 2))" using ea[THEN bspec[where x=x]] and `x\<in>s` by auto
himmelma@33175
  4087
      hence "x\<in>ball z (d z (e / 2))" using `ea>0` unfolding subset_eq by auto
himmelma@33175
  4088
      hence "dist (f z) (f x) < e / 2" using d[THEN spec[where x="e/2"]] and `e>0` and `x\<in>s` and `z\<in>s`
himmelma@33175
  4089
        by (auto  simp add: dist_commute)
himmelma@33175
  4090
      moreover have "y\<in>ball z (d z (e / 2))" using as and `ea>0` and z[unfolded subset_eq]
himmelma@33175
  4091
        by (auto simp add: dist_commute)
himmelma@33175
  4092
      hence "dist (f z) (f y) < e / 2" using d[THEN spec[where x="e/2"]] and `e>0` and `y\<in>s` and `z\<in>s`
himmelma@33175
  4093
        by (auto  simp add: dist_commute)
himmelma@33175
  4094
      ultimately have "dist (f y) (f x) < e" using dist_triangle_half_r[of "f z" "f x" e "f y"]
himmelma@33175
  4095
        by (auto simp add: dist_commute)  }
himmelma@33175
  4096
    then have "\<exists>d>0. \<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e" using `ea>0` by auto  }
himmelma@33175
  4097
  thus ?thesis unfolding uniformly_continuous_on_def by auto
himmelma@33175
  4098
qed
himmelma@33175
  4099
himmelma@33175
  4100
text{* Continuity of inverse function on compact domain. *}
himmelma@33175
  4101
himmelma@33175
  4102
lemma continuous_on_inverse:
himmelma@33175
  4103
  fixes f :: "'a::heine_borel \<Rightarrow> 'b::heine_borel"
himmelma@33175
  4104
    (* TODO: can this be generalized more? *)
himmelma@33175
  4105
  assumes "continuous_on s f"  "compact s"  "\<forall>x \<in> s. g (f x) = x"
himmelma@33175
  4106
  shows "continuous_on (f ` s) g"
himmelma@33175
  4107
proof-
himmelma@33175
  4108
  have *:"g ` f ` s = s" using assms(3) by (auto simp add: image_iff)
himmelma@33175
  4109
  { fix t assume t:"closedin (subtopology euclidean (g ` f ` s)) t"
himmelma@33175
  4110
    then obtain T where T: "closed T" "t = s \<inter> T" unfolding closedin_closed unfolding * by auto
himmelma@33175
  4111
    have "continuous_on (s \<inter> T) f" using continuous_on_subset[OF assms(1), of "s \<inter> t"]
himmelma@33175
  4112
      unfolding T(2) and Int_left_absorb by auto
himmelma@33175
  4113
    moreover have "compact (s \<inter> T)"
himmelma@33175
  4114
      using assms(2) unfolding compact_eq_bounded_closed
himmelma@33175
  4115
      using bounded_subset[of s "s \<inter> T"] and T(1) by auto
himmelma@33175
  4116
    ultimately have "closed (f ` t)" using T(1) unfolding T(2)
himmelma@33175
  4117
      using compact_continuous_image [of "s \<inter> T" f] unfolding compact_eq_bounded_closed by auto
himmelma@33175
  4118
    moreover have "{x \<in> f ` s. g x \<in> t} = f ` s \<inter> f ` t" using assms(3) unfolding T(2) by auto
himmelma@33175
  4119
    ultimately have "closedin (subtopology euclidean (f ` s)) {x \<in> f ` s. g x \<in> t}"
himmelma@33175
  4120
      unfolding closedin_closed by auto  }
himmelma@33175
  4121
  thus ?thesis unfolding continuous_on_closed by auto
himmelma@33175
  4122
qed
himmelma@33175
  4123
huffman@36433
  4124
text {* A uniformly convergent limit of continuous functions is continuous. *}
himmelma@33175
  4125
himmelma@33175
  4126
lemma continuous_uniform_limit:
huffman@45083
  4127
  fixes f :: "'a \<Rightarrow> 'b::metric_space \<Rightarrow> 'c::metric_space"
huffman@45083
  4128
  assumes "\<not> trivial_limit F"
huffman@45083
  4129
  assumes "eventually (\<lambda>n. continuous_on s (f n)) F"
huffman@45083
  4130
  assumes "\<forall>e>0. eventually (\<lambda>n. \<forall>x\<in>s. dist (f n x) (g x) < e) F"
himmelma@33175
  4131
  shows "continuous_on s g"
himmelma@33175
  4132
proof-
himmelma@33175
  4133
  { fix x and e::real assume "x\<in>s" "e>0"
huffman@45083
  4134
    have "eventually (\<lambda>n. \<forall>x\<in>s. dist (f n x) (g x) < e / 3) F"
huffman@45083
  4135
      using `e>0` assms(3)[THEN spec[where x="e/3"]] by auto
huffman@45083
  4136
    from eventually_happens [OF eventually_conj [OF this assms(2)]]
huffman@45083
  4137
    obtain n where n:"\<forall>x\<in>s. dist (f n x) (g x) < e / 3"  "continuous_on s (f n)"
huffman@45083
  4138
      using assms(1) by blast
himmelma@33175
  4139
    have "e / 3 > 0" using `e>0` by auto
himmelma@33175
  4140
    then obtain d where "d>0" and d:"\<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f n x') (f n x) < e / 3"
huffman@36355
  4141
      using n(2)[unfolded continuous_on_iff, THEN bspec[where x=x], OF `x\<in>s`, THEN spec[where x="e/3"]] by blast
huffman@45083
  4142
    { fix y assume "y \<in> s" and "dist y x < d"
huffman@45083
  4143
      hence "dist (f n y) (f n x) < e / 3"
huffman@45083
  4144
        by (rule d [rule_format])
huffman@45083
  4145
      hence "dist (f n y) (g x) < 2 * e / 3"
huffman@45083
  4146
        using dist_triangle [of "f n y" "g x" "f n x"]
huffman@45083
  4147
        using n(1)[THEN bspec[where x=x], OF `x\<in>s`]
huffman@45083
  4148
        by auto
huffman@45083
  4149
      hence "dist (g y) (g x) < e"
huffman@45083
  4150
        using n(1)[THEN bspec[where x=y], OF `y\<in>s`]
huffman@45083
  4151
        using dist_triangle3 [of "g y" "g x" "f n y"]
huffman@45083
  4152
        by auto }
huffman@45083
  4153
    hence "\<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (g x') (g x) < e"
huffman@45083
  4154
      using `d>0` by auto }
huffman@36355
  4155
  thus ?thesis unfolding continuous_on_iff by auto
himmelma@33175
  4156
qed
himmelma@33175
  4157
huffman@45081
  4158
huffman@45081
  4159
subsection {* Topological stuff lifted from and dropped to R *}
himmelma@33175
  4160
himmelma@33175
  4161
lemma open_real:
himmelma@33175
  4162
  fixes s :: "real set" shows
himmelma@33175
  4163
 "open s \<longleftrightarrow>
himmelma@33175
  4164
        (\<forall>x \<in> s. \<exists>e>0. \<forall>x'. abs(x' - x) < e --> x' \<in> s)" (is "?lhs = ?rhs")
himmelma@33175
  4165
  unfolding open_dist dist_norm by simp
himmelma@33175
  4166
himmelma@33175
  4167
lemma islimpt_approachable_real:
himmelma@33175
  4168
  fixes s :: "real set"
himmelma@33175
  4169
  shows "x islimpt s \<longleftrightarrow> (\<forall>e>0.  \<exists>x'\<in> s. x' \<noteq> x \<and> abs(x' - x) < e)"
himmelma@33175
  4170
  unfolding islimpt_approachable dist_norm by simp
himmelma@33175
  4171
himmelma@33175
  4172
lemma closed_real:
himmelma@33175
  4173
  fixes s :: "real set"
himmelma@33175
  4174
  shows "closed s \<longleftrightarrow>
himmelma@33175
  4175
        (\<forall>x. (\<forall>e>0.  \<exists>x' \<in> s. x' \<noteq> x \<and> abs(x' - x) < e)
himmelma@33175
  4176
            --> x \<in> s)"
himmelma@33175
  4177
  unfolding closed_limpt islimpt_approachable dist_norm by simp
himmelma@33175
  4178
himmelma@33175
  4179
lemma continuous_at_real_range:
himmelma@33175
  4180
  fixes f :: "'a::real_normed_vector \<Rightarrow> real"
himmelma@33175
  4181
  shows "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.
himmelma@33175
  4182
        \<forall>x'. norm(x' - x) < d --> abs(f x' - f x) < e)"
himmelma@33175
  4183
  unfolding continuous_at unfolding Lim_at
himmelma@33175
  4184
  unfolding dist_nz[THEN sym] unfolding dist_norm apply auto
himmelma@33175
  4185
  apply(erule_tac x=e in allE) apply auto apply (rule_tac x=d in exI) apply auto apply (erule_tac x=x' in allE) apply auto
himmelma@33175
  4186
  apply(erule_tac x=e in allE) by auto
himmelma@33175
  4187
himmelma@33175
  4188
lemma continuous_on_real_range:
himmelma@33175
  4189
  fixes f :: "'a::real_normed_vector \<Rightarrow> real"
himmelma@33175
  4190
  shows "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. \<forall>e>0. \<exists>d>0. (\<forall>x' \<in> s. norm(x' - x) < d --> abs(f x' - f x) < e))"
huffman@36355
  4191
  unfolding continuous_on_iff dist_norm by simp
himmelma@33175
  4192
himmelma@33175
  4193
lemma continuous_at_norm: "continuous (at x) norm"
himmelma@33175
  4194
  unfolding continuous_at by (intro tendsto_intros)
himmelma@33175
  4195
himmelma@33175
  4196
lemma continuous_on_norm: "continuous_on s norm"
himmelma@33175
  4197
unfolding continuous_on by (intro ballI tendsto_intros)
himmelma@33175
  4198
himmelma@33175
  4199
lemma continuous_at_infnorm: "continuous (at x) infnorm"
himmelma@33175
  4200
  unfolding continuous_at Lim_at o_def unfolding dist_norm
himmelma@33175
  4201
  apply auto apply (rule_tac x=e in exI) apply auto
himmelma@33175
  4202
  using order_trans[OF real_abs_sub_infnorm infnorm_le_norm, of _ x] by (metis xt1(7))
himmelma@33175
  4203
huffman@45081
  4204
text {* Hence some handy theorems on distance, diameter etc. of/from a set. *}
himmelma@33175
  4205
himmelma@33175
  4206
lemma compact_attains_sup:
himmelma@33175
  4207
  fixes s :: "real set"
himmelma@33175
  4208
  assumes "compact s"  "s \<noteq> {}"
himmelma@33175
  4209
  shows "\<exists>x \<in> s. \<forall>y \<in> s. y \<le> x"
himmelma@33175
  4210
proof-
himmelma@33175
  4211
  from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto
paulson@33270
  4212
  { fix e::real assume as: "\<forall>x\<in>s. x \<le> Sup s" "Sup s \<notin> s"  "0 < e" "\<forall>x'\<in>s. x' = Sup s \<or> \<not> Sup s - x' < e"
paulson@33270
  4213
    have "isLub UNIV s (Sup s)" using Sup[OF assms(2)] unfolding setle_def using as(1) by auto
paulson@33270
  4214
    moreover have "isUb UNIV s (Sup s - e)" unfolding isUb_def unfolding setle_def using as(4,2) by auto
paulson@33270
  4215
    ultimately have False using isLub_le_isUb[of UNIV s "Sup s" "Sup s - e"] using `e>0` by auto  }
paulson@33270
  4216
  thus ?thesis using bounded_has_Sup(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="Sup s"]]
paulson@33270
  4217
    apply(rule_tac x="Sup s" in bexI) by auto
paulson@33270
  4218
qed
paulson@33270
  4219
paulson@33270
  4220
lemma Inf:
paulson@33270
  4221
  fixes S :: "real set"
paulson@33270
  4222
  shows "S \<noteq> {} ==> (\<exists>b. b <=* S) ==> isGlb UNIV S (Inf S)"
paulson@33270
  4223
by (auto simp add: isLb_def setle_def setge_def isGlb_def greatestP_def) 
himmelma@33175
  4224
himmelma@33175
  4225
lemma compact_attains_inf:
himmelma@33175
  4226
  fixes s :: "real set"
himmelma@33175
  4227
  assumes "compact s" "s \<noteq> {}"  shows "\<exists>x \<in> s. \<forall>y \<in> s. x \<le> y"
himmelma@33175
  4228
proof-
himmelma@33175
  4229
  from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto
paulson@33270
  4230
  { fix e::real assume as: "\<forall>x\<in>s. x \<ge> Inf s"  "Inf s \<notin> s"  "0 < e"
paulson@33270
  4231
      "\<forall>x'\<in>s. x' = Inf s \<or> \<not> abs (x' - Inf s) < e"
paulson@33270
  4232
    have "isGlb UNIV s (Inf s)" using Inf[OF assms(2)] unfolding setge_def using as(1) by auto
himmelma@33175
  4233
    moreover
himmelma@33175
  4234
    { fix x assume "x \<in> s"
paulson@33270
  4235
      hence *:"abs (x - Inf s) = x - Inf s" using as(1)[THEN bspec[where x=x]] by auto
paulson@33270
  4236
      have "Inf s + e \<le> x" using as(4)[THEN bspec[where x=x]] using as(2) `x\<in>s` unfolding * by auto }
paulson@33270
  4237
    hence "isLb UNIV s (Inf s + e)" unfolding isLb_def and setge_def by auto
paulson@33270
  4238
    ultimately have False using isGlb_le_isLb[of UNIV s "Inf s" "Inf s + e"] using `e>0` by auto  }
paulson@33270
  4239
  thus ?thesis using bounded_has_Inf(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="Inf s"]]
paulson@33270
  4240
    apply(rule_tac x="Inf s" in bexI) by auto
himmelma@33175
  4241
qed
himmelma@33175
  4242
himmelma@33175
  4243
lemma continuous_attains_sup:
himmelma@33175
  4244
  fixes f :: "'a::metric_space \<Rightarrow> real"
himmelma@33175
  4245
  shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f
himmelma@33175
  4246
        ==> (\<exists>x \<in> s. \<forall>y \<in> s.  f y \<le> f x)"
himmelma@33175
  4247
  using compact_attains_sup[of "f ` s"]
himmelma@33175
  4248
  using compact_continuous_image[of s f] by auto
himmelma@33175
  4249
himmelma@33175
  4250
lemma continuous_attains_inf:
himmelma@33175
  4251
  fixes f :: "'a::metric_space \<Rightarrow> real"
himmelma@33175
  4252
  shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f
himmelma@33175
  4253
        \<Longrightarrow> (\<exists>x \<in> s. \<forall>y \<in> s. f x \<le> f y)"
himmelma@33175
  4254
  using compact_attains_inf[of "f ` s"]
himmelma@33175
  4255
  using compact_continuous_image[of s f] by auto
himmelma@33175
  4256
himmelma@33175
  4257
lemma distance_attains_sup:
himmelma@33175
  4258
  assumes "compact s" "s \<noteq> {}"
himmelma@33175
  4259
  shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a y \<le> dist a x"
himmelma@33175
  4260
proof (rule continuous_attains_sup [OF assms])
himmelma@33175
  4261
  { fix x assume "x\<in>s"
himmelma@33175
  4262
    have "(dist a ---> dist a x) (at x within s)"
huffman@44983
  4263
      by (intro tendsto_dist tendsto_const Lim_at_within LIM_ident)
himmelma@33175
  4264
  }
himmelma@33175
  4265
  thus "continuous_on s (dist a)"
himmelma@33175
  4266
    unfolding continuous_on ..
himmelma@33175
  4267
qed
himmelma@33175
  4268
huffman@45081
  4269
text {* For \emph{minimal} distance, we only need closure, not compactness. *}
himmelma@33175
  4270
himmelma@33175
  4271
lemma distance_attains_inf:
himmelma@33175
  4272
  fixes a :: "'a::heine_borel"
himmelma@33175
  4273
  assumes "closed s"  "s \<noteq> {}"
himmelma@33175
  4274
  shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a x \<le> dist a y"
himmelma@33175
  4275
proof-
himmelma@33175
  4276
  from assms(2) obtain b where "b\<in>s" by auto
himmelma@33175
  4277
  let ?B = "cball a (dist b a) \<inter> s"
himmelma@33175
  4278
  have "b \<in> ?B" using `b\<in>s` by (simp add: dist_commute)
himmelma@33175
  4279
  hence "?B \<noteq> {}" by auto
himmelma@33175
  4280
  moreover
himmelma@33175
  4281
  { fix x assume "x\<in>?B"
himmelma@33175
  4282
    fix e::real assume "e>0"
himmelma@33175
  4283
    { fix x' assume "x'\<in>?B" and as:"dist x' x < e"
himmelma@33175
  4284
      from as have "\<bar>dist a x' - dist a x\<bar> < e"
himmelma@33175
  4285
        unfolding abs_less_iff minus_diff_eq
himmelma@33175
  4286
        using dist_triangle2 [of a x' x]
himmelma@33175
  4287
        using dist_triangle [of a x x']
himmelma@33175
  4288
        by arith
himmelma@33175
  4289
    }
himmelma@33175
  4290
    hence "\<exists>d>0. \<forall>x'\<in>?B. dist x' x < d \<longrightarrow> \<bar>dist a x' - dist a x\<bar> < e"
himmelma@33175
  4291
      using `e>0` by auto
himmelma@33175
  4292
  }
himmelma@33175
  4293
  hence "continuous_on (cball a (dist b a) \<inter> s) (dist a)"
himmelma@33175
  4294
    unfolding continuous_on Lim_within dist_norm real_norm_def
himmelma@33175
  4295
    by fast
himmelma@33175
  4296
  moreover have "compact ?B"
himmelma@33175
  4297
    using compact_cball[of a "dist b a"]
himmelma@33175
  4298
    unfolding compact_eq_bounded_closed
himmelma@33175
  4299
    using bounded_Int and closed_Int and assms(1) by auto
himmelma@33175
  4300
  ultimately obtain x where "x\<in>cball a (dist b a) \<inter> s" "\<forall>y\<in>cball a (dist b a) \<inter> s. dist a x \<le> dist a y"
himmelma@33175
  4301
    using continuous_attains_inf[of ?B "dist a"] by fastsimp
himmelma@33175
  4302
  thus ?thesis by fastsimp
himmelma@33175
  4303
qed
himmelma@33175
  4304
huffman@45081
  4305
huffman@36433
  4306
subsection {* Pasted sets *}
himmelma@33175
  4307
himmelma@33175
  4308
lemma bounded_Times:
himmelma@33175
  4309
  assumes "bounded s" "bounded t" shows "bounded (s \<times> t)"
himmelma@33175
  4310
proof-
himmelma@33175
  4311
  obtain x y a b where "\<forall>z\<in>s. dist x z \<le> a" "\<forall>z\<in>t. dist y z \<le> b"
himmelma@33175
  4312
    using assms [unfolded bounded_def] by auto
himmelma@33175
  4313
  then have "\<forall>z\<in>s \<times> t. dist (x, y) z \<le> sqrt (a\<twosuperior> + b\<twosuperior>)"
himmelma@33175
  4314
    by (auto simp add: dist_Pair_Pair real_sqrt_le_mono add_mono power_mono)
himmelma@33175
  4315
  thus ?thesis unfolding bounded_any_center [where a="(x, y)"] by auto
himmelma@33175
  4316
qed
himmelma@33175
  4317
himmelma@33175
  4318
lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"
himmelma@33175
  4319
by (induct x) simp
himmelma@33175
  4320
himmelma@33175
  4321
lemma compact_Times: "compact s \<Longrightarrow> compact t \<Longrightarrow> compact (s \<times> t)"
himmelma@33175
  4322
unfolding compact_def
himmelma@33175
  4323
apply clarify
himmelma@33175
  4324
apply (drule_tac x="fst \<circ> f" in spec)
himmelma@33175
  4325
apply (drule mp, simp add: mem_Times_iff)
himmelma@33175
  4326
apply (clarify, rename_tac l1 r1)
himmelma@33175
  4327
apply (drule_tac x="snd \<circ> f \<circ> r1" in spec)
himmelma@33175
  4328
apply (drule mp, simp add: mem_Times_iff)
himmelma@33175
  4329
apply (clarify, rename_tac l2 r2)
himmelma@33175
  4330
apply (rule_tac x="(l1, l2)" in rev_bexI, simp)
himmelma@33175
  4331
apply (rule_tac x="r1 \<circ> r2" in exI)
himmelma@33175
  4332
apply (rule conjI, simp add: subseq_def)
himmelma@33175
  4333
apply (drule_tac r=r2 in lim_subseq [COMP swap_prems_rl], assumption)
himmelma@33175
  4334
apply (drule (1) tendsto_Pair) back
himmelma@33175
  4335
apply (simp add: o_def)
himmelma@33175
  4336
done
himmelma@33175
  4337
huffman@45081
  4338
text{* Hence some useful properties follow quite easily. *}
himmelma@33175
  4339
himmelma@33175
  4340
lemma compact_scaling:
himmelma@33175
  4341
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4342
  assumes "compact s"  shows "compact ((\<lambda>x. c *\<^sub>R x) ` s)"
himmelma@33175
  4343
proof-
himmelma@33175
  4344
  let ?f = "\<lambda>x. scaleR c x"
huffman@45145
  4345
  have *:"bounded_linear ?f" by (rule bounded_linear_scaleR_right)
himmelma@33175
  4346
  show ?thesis using compact_continuous_image[of s ?f] continuous_at_imp_continuous_on[of s ?f]
himmelma@33175
  4347
    using linear_continuous_at[OF *] assms by auto
himmelma@33175
  4348
qed
himmelma@33175
  4349
himmelma@33175
  4350
lemma compact_negations:
himmelma@33175
  4351
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4352
  assumes "compact s"  shows "compact ((\<lambda>x. -x) ` s)"
himmelma@33175
  4353
  using compact_scaling [OF assms, of "- 1"] by auto
himmelma@33175
  4354
himmelma@33175
  4355
lemma compact_sums:
himmelma@33175
  4356
  fixes s t :: "'a::real_normed_vector set"
himmelma@33175
  4357
  assumes "compact s"  "compact t"  shows "compact {x + y | x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4358
proof-
himmelma@33175
  4359
  have *:"{x + y | x y. x \<in> s \<and> y \<in> t} = (\<lambda>z. fst z + snd z) ` (s \<times> t)"
himmelma@33175
  4360
    apply auto unfolding image_iff apply(rule_tac x="(xa, y)" in bexI) by auto
himmelma@33175
  4361
  have "continuous_on (s \<times> t) (\<lambda>z. fst z + snd z)"
himmelma@33175
  4362
    unfolding continuous_on by (rule ballI) (intro tendsto_intros)
himmelma@33175
  4363
  thus ?thesis unfolding * using compact_continuous_image compact_Times [OF assms] by auto
himmelma@33175
  4364
qed
himmelma@33175
  4365
himmelma@33175
  4366
lemma compact_differences:
himmelma@33175
  4367
  fixes s t :: "'a::real_normed_vector set"
himmelma@33175
  4368
  assumes "compact s" "compact t"  shows "compact {x - y | x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4369
proof-
himmelma@33175
  4370
  have "{x - y | x y. x\<in>s \<and> y \<in> t} =  {x + y | x y. x \<in> s \<and> y \<in> (uminus ` t)}"
himmelma@33175
  4371
    apply auto apply(rule_tac x= xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
himmelma@33175
  4372
  thus ?thesis using compact_sums[OF assms(1) compact_negations[OF assms(2)]] by auto
himmelma@33175
  4373
qed
himmelma@33175
  4374
himmelma@33175
  4375
lemma compact_translation:
himmelma@33175
  4376
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4377
  assumes "compact s"  shows "compact ((\<lambda>x. a + x) ` s)"
himmelma@33175
  4378
proof-
himmelma@33175
  4379
  have "{x + y |x y. x \<in> s \<and> y \<in> {a}} = (\<lambda>x. a + x) ` s" by auto
himmelma@33175
  4380
  thus ?thesis using compact_sums[OF assms compact_sing[of a]] by auto
himmelma@33175
  4381
qed
himmelma@33175
  4382
himmelma@33175
  4383
lemma compact_affinity:
himmelma@33175
  4384
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4385
  assumes "compact s"  shows "compact ((\<lambda>x. a + c *\<^sub>R x) ` s)"
himmelma@33175
  4386
proof-
himmelma@33175
  4387
  have "op + a ` op *\<^sub>R c ` s = (\<lambda>x. a + c *\<^sub>R x) ` s" by auto
himmelma@33175
  4388
  thus ?thesis using compact_translation[OF compact_scaling[OF assms], of a c] by auto
himmelma@33175
  4389
qed
himmelma@33175
  4390
huffman@45081
  4391
text {* Hence we get the following. *}
himmelma@33175
  4392
himmelma@33175
  4393
lemma compact_sup_maxdistance:
himmelma@33175
  4394
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4395
  assumes "compact s"  "s \<noteq> {}"
himmelma@33175
  4396
  shows "\<exists>x\<in>s. \<exists>y\<in>s. \<forall>u\<in>s. \<forall>v\<in>s. norm(u - v) \<le> norm(x - y)"
himmelma@33175
  4397
proof-
himmelma@33175
  4398
  have "{x - y | x y . x\<in>s \<and> y\<in>s} \<noteq> {}" using `s \<noteq> {}` by auto
himmelma@33175
  4399
  then obtain x where x:"x\<in>{x - y |x y. x \<in> s \<and> y \<in> s}"  "\<forall>y\<in>{x - y |x y. x \<in> s \<and> y \<in> s}. norm y \<le> norm x"
himmelma@33175
  4400
    using compact_differences[OF assms(1) assms(1)]
huffman@36358
  4401
    using distance_attains_sup[where 'a="'a", unfolded dist_norm, of "{x - y | x y . x\<in>s \<and> y\<in>s}" 0] by auto
himmelma@33175
  4402
  from x(1) obtain a b where "a\<in>s" "b\<in>s" "x = a - b" by auto
himmelma@33175
  4403
  thus ?thesis using x(2)[unfolded `x = a - b`] by blast
himmelma@33175
  4404
qed
himmelma@33175
  4405
huffman@45081
  4406
text {* We can state this in terms of diameter of a set. *}
himmelma@33175
  4407
paulson@33270
  4408
definition "diameter s = (if s = {} then 0::real else Sup {norm(x - y) | x y. x \<in> s \<and> y \<in> s})"
himmelma@33175
  4409
  (* TODO: generalize to class metric_space *)
himmelma@33175
  4410
himmelma@33175
  4411
lemma diameter_bounded:
himmelma@33175
  4412
  assumes "bounded s"
himmelma@33175
  4413
  shows "\<forall>x\<in>s. \<forall>y\<in>s. norm(x - y) \<le> diameter s"
himmelma@33175
  4414
        "\<forall>d>0. d < diameter s --> (\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d)"
himmelma@33175
  4415
proof-
himmelma@33175
  4416
  let ?D = "{norm (x - y) |x y. x \<in> s \<and> y \<in> s}"
himmelma@33175
  4417
  obtain a where a:"\<forall>x\<in>s. norm x \<le> a" using assms[unfolded bounded_iff] by auto
himmelma@33175
  4418
  { fix x y assume "x \<in> s" "y \<in> s"
haftmann@36349
  4419
    hence "norm (x - y) \<le> 2 * a" using norm_triangle_ineq[of x "-y", unfolded norm_minus_cancel] a[THEN bspec[where x=x]] a[THEN bspec[where x=y]] by (auto simp add: field_simps)  }
himmelma@33175
  4420
  note * = this
himmelma@33175
  4421
  { fix x y assume "x\<in>s" "y\<in>s"  hence "s \<noteq> {}" by auto
huffman@36358
  4422
    have "norm(x - y) \<le> diameter s" unfolding diameter_def using `s\<noteq>{}` *[OF `x\<in>s` `y\<in>s`] `x\<in>s` `y\<in>s`
paulson@33307
  4423
      by simp (blast intro!: Sup_upper *) }
himmelma@33175
  4424
  moreover
himmelma@33175
  4425
  { fix d::real assume "d>0" "d < diameter s"
himmelma@33175
  4426
    hence "s\<noteq>{}" unfolding diameter_def by auto
himmelma@33175
  4427
    have "\<exists>d' \<in> ?D. d' > d"
himmelma@33175
  4428
    proof(rule ccontr)
himmelma@33175
  4429
      assume "\<not> (\<exists>d'\<in>{norm (x - y) |x y. x \<in> s \<and> y \<in> s}. d < d')"
paulson@33307
  4430
      hence "\<forall>d'\<in>?D. d' \<le> d" by auto (metis not_leE) 
paulson@33307
  4431
      thus False using `d < diameter s` `s\<noteq>{}` 
paulson@33307
  4432
        apply (auto simp add: diameter_def) 
paulson@33307
  4433
        apply (drule Sup_real_iff [THEN [2] rev_iffD2])
paulson@33307
  4434
        apply (auto, force) 
paulson@33307
  4435
        done
himmelma@33175
  4436
    qed
himmelma@33175
  4437
    hence "\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d" by auto  }
himmelma@33175
  4438
  ultimately show "\<forall>x\<in>s. \<forall>y\<in>s. norm(x - y) \<le> diameter s"
himmelma@33175
  4439
        "\<forall>d>0. d < diameter s --> (\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d)" by auto
himmelma@33175
  4440
qed
himmelma@33175
  4441
himmelma@33175
  4442
lemma diameter_bounded_bound:
himmelma@33175
  4443
 "bounded s \<Longrightarrow> x \<in> s \<Longrightarrow> y \<in> s ==> norm(x - y) \<le> diameter s"
himmelma@33175
  4444
  using diameter_bounded by blast
himmelma@33175
  4445
himmelma@33175
  4446
lemma diameter_compact_attained:
himmelma@33175
  4447
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4448
  assumes "compact s"  "s \<noteq> {}"
himmelma@33175
  4449
  shows "\<exists>x\<in>s. \<exists>y\<in>s. (norm(x - y) = diameter s)"
himmelma@33175
  4450
proof-
himmelma@33175
  4451
  have b:"bounded s" using assms(1) by (rule compact_imp_bounded)
himmelma@33175
  4452
  then obtain x y where xys:"x\<in>s" "y\<in>s" and xy:"\<forall>u\<in>s. \<forall>v\<in>s. norm (u - v) \<le> norm (x - y)" using compact_sup_maxdistance[OF assms] by auto
huffman@36358
  4453
  hence "diameter s \<le> norm (x - y)"
huffman@36358
  4454
    unfolding diameter_def by clarsimp (rule Sup_least, fast+)
paulson@33307
  4455
  thus ?thesis
huffman@36358
  4456
    by (metis b diameter_bounded_bound order_antisym xys)
himmelma@33175
  4457
qed
himmelma@33175
  4458
huffman@45081
  4459
text {* Related results with closure as the conclusion. *}
himmelma@33175
  4460
himmelma@33175
  4461
lemma closed_scaling:
himmelma@33175
  4462
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4463
  assumes "closed s" shows "closed ((\<lambda>x. c *\<^sub>R x) ` s)"
himmelma@33175
  4464
proof(cases "s={}")
himmelma@33175
  4465
  case True thus ?thesis by auto
himmelma@33175
  4466
next
himmelma@33175
  4467
  case False
himmelma@33175
  4468
  show ?thesis
himmelma@33175
  4469
  proof(cases "c=0")
himmelma@33175
  4470
    have *:"(\<lambda>x. 0) ` s = {0}" using `s\<noteq>{}` by auto
huffman@36668
  4471
    case True thus ?thesis apply auto unfolding * by auto
himmelma@33175
  4472
  next
himmelma@33175
  4473
    case False
himmelma@33175
  4474
    { fix x l assume as:"\<forall>n::nat. x n \<in> scaleR c ` s"  "(x ---> l) sequentially"
himmelma@33175
  4475
      { fix n::nat have "scaleR (1 / c) (x n) \<in> s"
himmelma@33175
  4476
          using as(1)[THEN spec[where x=n]]
hoelzl@37489
  4477
          using `c\<noteq>0` by auto
himmelma@33175
  4478
      }
himmelma@33175
  4479
      moreover
himmelma@33175
  4480
      { fix e::real assume "e>0"
himmelma@33175
  4481
        hence "0 < e *\<bar>c\<bar>"  using `c\<noteq>0` mult_pos_pos[of e "abs c"] by auto
himmelma@33175
  4482
        then obtain N where "\<forall>n\<ge>N. dist (x n) l < e * \<bar>c\<bar>"
himmelma@33175
  4483
          using as(2)[unfolded Lim_sequentially, THEN spec[where x="e * abs c"]] by auto
himmelma@33175
  4484
        hence "\<exists>N. \<forall>n\<ge>N. dist (scaleR (1 / c) (x n)) (scaleR (1 / c) l) < e"
himmelma@33175
  4485
          unfolding dist_norm unfolding scaleR_right_diff_distrib[THEN sym]
himmelma@33175
  4486
          using mult_imp_div_pos_less[of "abs c" _ e] `c\<noteq>0` by auto  }
himmelma@33175
  4487
      hence "((\<lambda>n. scaleR (1 / c) (x n)) ---> scaleR (1 / c) l) sequentially" unfolding Lim_sequentially by auto
himmelma@33175
  4488
      ultimately have "l \<in> scaleR c ` s"
himmelma@33175
  4489
        using assms[unfolded closed_sequential_limits, THEN spec[where x="\<lambda>n. scaleR (1/c) (x n)"], THEN spec[where x="scaleR (1/c) l"]]
himmelma@33175
  4490
        unfolding image_iff using `c\<noteq>0` apply(rule_tac x="scaleR (1 / c) l" in bexI) by auto  }
himmelma@33175
  4491
    thus ?thesis unfolding closed_sequential_limits by fast
himmelma@33175
  4492
  qed
himmelma@33175
  4493
qed
himmelma@33175
  4494
himmelma@33175
  4495
lemma closed_negations:
himmelma@33175
  4496
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4497
  assumes "closed s"  shows "closed ((\<lambda>x. -x) ` s)"
himmelma@33175
  4498
  using closed_scaling[OF assms, of "- 1"] by simp
himmelma@33175
  4499
himmelma@33175
  4500
lemma compact_closed_sums:
himmelma@33175
  4501
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  4502
  assumes "compact s"  "closed t"  shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4503
proof-
himmelma@33175
  4504
  let ?S = "{x + y |x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4505
  { fix x l assume as:"\<forall>n. x n \<in> ?S"  "(x ---> l) sequentially"
himmelma@33175
  4506
    from as(1) obtain f where f:"\<forall>n. x n = fst (f n) + snd (f n)"  "\<forall>n. fst (f n) \<in> s"  "\<forall>n. snd (f n) \<in> t"
himmelma@33175
  4507
      using choice[of "\<lambda>n y. x n = (fst y) + (snd y) \<and> fst y \<in> s \<and> snd y \<in> t"] by auto
himmelma@33175
  4508
    obtain l' r where "l'\<in>s" and r:"subseq r" and lr:"(((\<lambda>n. fst (f n)) \<circ> r) ---> l') sequentially"
himmelma@33175
  4509
      using assms(1)[unfolded compact_def, THEN spec[where x="\<lambda> n. fst (f n)"]] using f(2) by auto
himmelma@33175
  4510
    have "((\<lambda>n. snd (f (r n))) ---> l - l') sequentially"
huffman@44983
  4511
      using tendsto_diff[OF lim_subseq[OF r as(2)] lr] and f(1) unfolding o_def by auto
himmelma@33175
  4512
    hence "l - l' \<in> t"
himmelma@33175
  4513
      using assms(2)[unfolded closed_sequential_limits, THEN spec[where x="\<lambda> n. snd (f (r n))"], THEN spec[where x="l - l'"]]
himmelma@33175
  4514
      using f(3) by auto
himmelma@33175
  4515
    hence "l \<in> ?S" using `l' \<in> s` apply auto apply(rule_tac x=l' in exI) apply(rule_tac x="l - l'" in exI) by auto
himmelma@33175
  4516
  }
himmelma@33175
  4517
  thus ?thesis unfolding closed_sequential_limits by fast
himmelma@33175
  4518
qed
himmelma@33175
  4519
himmelma@33175
  4520
lemma closed_compact_sums:
himmelma@33175
  4521
  fixes s t :: "'a::real_normed_vector set"
himmelma@33175
  4522
  assumes "closed s"  "compact t"
himmelma@33175
  4523
  shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4524
proof-
himmelma@33175
  4525
  have "{x + y |x y. x \<in> t \<and> y \<in> s} = {x + y |x y. x \<in> s \<and> y \<in> t}" apply auto
himmelma@33175
  4526
    apply(rule_tac x=y in exI) apply auto apply(rule_tac x=y in exI) by auto
himmelma@33175
  4527
  thus ?thesis using compact_closed_sums[OF assms(2,1)] by simp
himmelma@33175
  4528
qed
himmelma@33175
  4529
himmelma@33175
  4530
lemma compact_closed_differences:
himmelma@33175
  4531
  fixes s t :: "'a::real_normed_vector set"
himmelma@33175
  4532
  assumes "compact s"  "closed t"
himmelma@33175
  4533
  shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4534
proof-
himmelma@33175
  4535
  have "{x + y |x y. x \<in> s \<and> y \<in> uminus ` t} =  {x - y |x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4536
    apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
himmelma@33175
  4537
  thus ?thesis using compact_closed_sums[OF assms(1) closed_negations[OF assms(2)]] by auto
himmelma@33175
  4538
qed
himmelma@33175
  4539
himmelma@33175
  4540
lemma closed_compact_differences:
himmelma@33175
  4541
  fixes s t :: "'a::real_normed_vector set"
himmelma@33175
  4542
  assumes "closed s" "compact t"
himmelma@33175
  4543
  shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4544
proof-
himmelma@33175
  4545
  have "{x + y |x y. x \<in> s \<and> y \<in> uminus ` t} = {x - y |x y. x \<in> s \<and> y \<in> t}"
himmelma@33175
  4546
    apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
himmelma@33175
  4547
 thus ?thesis using closed_compact_sums[OF assms(1) compact_negations[OF assms(2)]] by simp
himmelma@33175
  4548
qed
himmelma@33175
  4549
himmelma@33175
  4550
lemma closed_translation:
himmelma@33175
  4551
  fixes a :: "'a::real_normed_vector"
himmelma@33175
  4552
  assumes "closed s"  shows "closed ((\<lambda>x. a + x) ` s)"
himmelma@33175
  4553
proof-
himmelma@33175
  4554
  have "{a + y |y. y \<in> s} = (op + a ` s)" by auto
himmelma@33175
  4555
  thus ?thesis using compact_closed_sums[OF compact_sing[of a] assms] by auto
himmelma@33175
  4556
qed
himmelma@33175
  4557
huffman@34099
  4558
lemma translation_Compl:
huffman@34099
  4559
  fixes a :: "'a::ab_group_add"
huffman@34099
  4560
  shows "(\<lambda>x. a + x) ` (- t) = - ((\<lambda>x. a + x) ` t)"
huffman@34099
  4561
  apply (auto simp add: image_iff) apply(rule_tac x="x - a" in bexI) by auto
huffman@34099
  4562
himmelma@33175
  4563
lemma translation_UNIV:
himmelma@33175
  4564
  fixes a :: "'a::ab_group_add" shows "range (\<lambda>x. a + x) = UNIV"
himmelma@33175
  4565
  apply (auto simp add: image_iff) apply(rule_tac x="x - a" in exI) by auto
himmelma@33175
  4566
himmelma@33175
  4567
lemma translation_diff:
himmelma@33175
  4568
  fixes a :: "'a::ab_group_add"
himmelma@33175
  4569
  shows "(\<lambda>x. a + x) ` (s - t) = ((\<lambda>x. a + x) ` s) - ((\<lambda>x. a + x) ` t)"
himmelma@33175
  4570
  by auto
himmelma@33175
  4571
himmelma@33175
  4572
lemma closure_translation:
himmelma@33175
  4573
  fixes a :: "'a::real_normed_vector"
himmelma@33175
  4574
  shows "closure ((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (closure s)"
himmelma@33175
  4575
proof-
huffman@34099
  4576
  have *:"op + a ` (- s) = - op + a ` s"
himmelma@33175
  4577
    apply auto unfolding image_iff apply(rule_tac x="x - a" in bexI) by auto
huffman@34099
  4578
  show ?thesis unfolding closure_interior translation_Compl
huffman@34099
  4579
    using interior_translation[of a "- s"] unfolding * by auto
himmelma@33175
  4580
qed
himmelma@33175
  4581
himmelma@33175
  4582
lemma frontier_translation:
himmelma@33175
  4583
  fixes a :: "'a::real_normed_vector"
himmelma@33175
  4584
  shows "frontier((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (frontier s)"
himmelma@33175
  4585
  unfolding frontier_def translation_diff interior_translation closure_translation by auto
himmelma@33175
  4586
huffman@45081
  4587
huffman@45081
  4588
subsection {* Separation between points and sets *}
himmelma@33175
  4589
himmelma@33175
  4590
lemma separate_point_closed:
himmelma@33175
  4591
  fixes s :: "'a::heine_borel set"
himmelma@33175
  4592
  shows "closed s \<Longrightarrow> a \<notin> s  ==> (\<exists>d>0. \<forall>x\<in>s. d \<le> dist a x)"
himmelma@33175
  4593
proof(cases "s = {}")
himmelma@33175
  4594
  case True
himmelma@33175
  4595
  thus ?thesis by(auto intro!: exI[where x=1])
himmelma@33175
  4596
next
himmelma@33175
  4597
  case False
himmelma@33175
  4598
  assume "closed s" "a \<notin> s"
himmelma@33175
  4599
  then obtain x where "x\<in>s" "\<forall>y\<in>s. dist a x \<le> dist a y" using `s \<noteq> {}` distance_attains_inf [of s a] by blast
himmelma@33175
  4600
  with `x\<in>s` show ?thesis using dist_pos_lt[of a x] and`a \<notin> s` by blast
himmelma@33175
  4601
qed
himmelma@33175
  4602
himmelma@33175
  4603
lemma separate_compact_closed:
himmelma@33175
  4604
  fixes s t :: "'a::{heine_borel, real_normed_vector} set"
himmelma@33175
  4605
    (* TODO: does this generalize to heine_borel? *)
himmelma@33175
  4606
  assumes "compact s" and "closed t" and "s \<inter> t = {}"
himmelma@33175
  4607
  shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"
himmelma@33175
  4608
proof-
himmelma@33175
  4609
  have "0 \<notin> {x - y |x y. x \<in> s \<and> y \<in> t}" using assms(3) by auto
himmelma@33175
  4610
  then obtain d where "d>0" and d:"\<forall>x\<in>{x - y |x y. x \<in> s \<and> y \<in> t}. d \<le> dist 0 x"
himmelma@33175
  4611
    using separate_point_closed[OF compact_closed_differences[OF assms(1,2)], of 0] by auto
himmelma@33175
  4612
  { fix x y assume "x\<in>s" "y\<in>t"
himmelma@33175
  4613
    hence "x - y \<in> {x - y |x y. x \<in> s \<and> y \<in> t}" by auto
himmelma@33175
  4614
    hence "d \<le> dist (x - y) 0" using d[THEN bspec[where x="x - y"]] using dist_commute
himmelma@33175
  4615
      by (auto  simp add: dist_commute)
himmelma@33175
  4616
    hence "d \<le> dist x y" unfolding dist_norm by auto  }
himmelma@33175
  4617
  thus ?thesis using `d>0` by auto
himmelma@33175
  4618
qed
himmelma@33175
  4619
himmelma@33175
  4620
lemma separate_closed_compact:
himmelma@33175
  4621
  fixes s t :: "'a::{heine_borel, real_normed_vector} set"
himmelma@33175
  4622
  assumes "closed s" and "compact t" and "s \<inter> t = {}"
himmelma@33175
  4623
  shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"
himmelma@33175
  4624
proof-
himmelma@33175
  4625
  have *:"t \<inter> s = {}" using assms(3) by auto
himmelma@33175
  4626
  show ?thesis using separate_compact_closed[OF assms(2,1) *]
himmelma@33175
  4627
    apply auto apply(rule_tac x=d in exI) apply auto apply (erule_tac x=y in ballE)
himmelma@33175
  4628
    by (auto simp add: dist_commute)
himmelma@33175
  4629
qed
himmelma@33175
  4630
huffman@45081
  4631
huffman@36435
  4632
subsection {* Intervals *}
hoelzl@37489
  4633
  
hoelzl@37489
  4634
lemma interval: fixes a :: "'a::ordered_euclidean_space" shows
hoelzl@37489
  4635
  "{a <..< b} = {x::'a. \<forall>i<DIM('a). a$$i < x$$i \<and> x$$i < b$$i}" and
hoelzl@37489
  4636
  "{a .. b} = {x::'a. \<forall>i<DIM('a). a$$i \<le> x$$i \<and> x$$i \<le> b$$i}"
nipkow@39535
  4637
  by(auto simp add:set_eq_iff eucl_le[where 'a='a] eucl_less[where 'a='a])
hoelzl@37489
  4638
hoelzl@37489
  4639
lemma mem_interval: fixes a :: "'a::ordered_euclidean_space" shows
hoelzl@37489
  4640
  "x \<in> {a<..<b} \<longleftrightarrow> (\<forall>i<DIM('a). a$$i < x$$i \<and> x$$i < b$$i)"
hoelzl@37489
  4641
  "x \<in> {a .. b} \<longleftrightarrow> (\<forall>i<DIM('a). a$$i \<le> x$$i \<and> x$$i \<le> b$$i)"
nipkow@39535
  4642
  using interval[of a b] by(auto simp add: set_eq_iff eucl_le[where 'a='a] eucl_less[where 'a='a])
hoelzl@37489
  4643
hoelzl@37489
  4644
lemma interval_eq_empty: fixes a :: "'a::ordered_euclidean_space" shows
hoelzl@37489
  4645
 "({a <..< b} = {} \<longleftrightarrow> (\<exists>i<DIM('a). b$$i \<le> a$$i))" (is ?th1) and
hoelzl@37489
  4646
 "({a  ..  b} = {} \<longleftrightarrow> (\<exists>i<DIM('a). b$$i < a$$i))" (is ?th2)
himmelma@33175
  4647
proof-
hoelzl@37489
  4648
  { fix i x assume i:"i<DIM('a)" and as:"b$$i \<le> a$$i" and x:"x\<in>{a <..< b}"
hoelzl@37489
  4649
    hence "a $$ i < x $$ i \<and> x $$ i < b $$ i" unfolding mem_interval by auto
hoelzl@37489
  4650
    hence "a$$i < b$$i" by auto
himmelma@33175
  4651
    hence False using as by auto  }
himmelma@33175
  4652
  moreover
hoelzl@37489
  4653
  { assume as:"\<forall>i<DIM('a). \<not> (b$$i \<le> a$$i)"
himmelma@33175
  4654
    let ?x = "(1/2) *\<^sub>R (a + b)"
hoelzl@37489
  4655
    { fix i assume i:"i<DIM('a)" 
hoelzl@37489
  4656
      have "a$$i < b$$i" using as[THEN spec[where x=i]] using i by auto
hoelzl@37489
  4657
      hence "a$$i < ((1/2) *\<^sub>R (a+b)) $$ i" "((1/2) *\<^sub>R (a+b)) $$ i < b$$i"
hoelzl@37489
  4658
        unfolding euclidean_simps by auto }
himmelma@33175
  4659
    hence "{a <..< b} \<noteq> {}" using mem_interval(1)[of "?x" a b] by auto  }
himmelma@33175
  4660
  ultimately show ?th1 by blast
himmelma@33175
  4661
hoelzl@37489
  4662
  { fix i x assume i:"i<DIM('a)" and as:"b$$i < a$$i" and x:"x\<in>{a .. b}"
hoelzl@37489
  4663
    hence "a $$ i \<le> x $$ i \<and> x $$ i \<le> b $$ i" unfolding mem_interval by auto
hoelzl@37489
  4664
    hence "a$$i \<le> b$$i" by auto
himmelma@33175
  4665
    hence False using as by auto  }
himmelma@33175
  4666
  moreover
hoelzl@37489
  4667
  { assume as:"\<forall>i<DIM('a). \<not> (b$$i < a$$i)"
himmelma@33175
  4668
    let ?x = "(1/2) *\<^sub>R (a + b)"
hoelzl@37489
  4669
    { fix i assume i:"i<DIM('a)"
hoelzl@37489
  4670
      have "a$$i \<le> b$$i" using as[THEN spec[where x=i]] by auto
hoelzl@37489
  4671
      hence "a$$i \<le> ((1/2) *\<^sub>R (a+b)) $$ i" "((1/2) *\<^sub>R (a+b)) $$ i \<le> b$$i"
hoelzl@37489
  4672
        unfolding euclidean_simps by auto }
himmelma@33175
  4673
    hence "{a .. b} \<noteq> {}" using mem_interval(2)[of "?x" a b] by auto  }
himmelma@33175
  4674
  ultimately show ?th2 by blast
himmelma@33175
  4675
qed
himmelma@33175
  4676
hoelzl@37489
  4677
lemma interval_ne_empty: fixes a :: "'a::ordered_euclidean_space" shows
hoelzl@37489
  4678
  "{a  ..  b} \<noteq> {} \<longleftrightarrow> (\<forall>i<DIM('a). a$$i \<le> b$$i)" and
hoelzl@37489
  4679
  "{a <..< b} \<noteq> {} \<longleftrightarrow> (\<forall>i<DIM('a). a$$i < b$$i)"
hoelzl@37489
  4680
  unfolding interval_eq_empty[of a b] by fastsimp+
hoelzl@37489
  4681
hoelzl@37489
  4682
lemma interval_sing: fixes a :: "'a::ordered_euclidean_space" shows
hoelzl@37489
  4683
 "{a .. a} = {a}" "{a<..<a} = {}"
nipkow@39535
  4684
  apply(auto simp add: set_eq_iff euclidean_eq[where 'a='a] eucl_less[where 'a='a] eucl_le[where 'a='a])
hoelzl@37489
  4685
  apply (simp add: order_eq_iff) apply(rule_tac x=0 in exI) by (auto simp add: not_less less_imp_le)
hoelzl@37489
  4686
hoelzl@37489
  4687
lemma subset_interval_imp: fixes a :: "'a::ordered_euclidean_space" shows
hoelzl@37489
  4688
 "(\<forall>i<DIM('a). a$$i \<le> c$$i \<and> d$$i \<le> b$$i) \<Longrightarrow> {c .. d} \<subseteq> {a .. b}" and
hoelzl@37489
  4689
 "(\<forall>i<DIM('a). a$$i < c$$i \<and> d$$i < b$$i) \<Longrightarrow> {c .. d} \<subseteq> {a<..<b}" and
hoelzl@37489
  4690
 "(\<forall>i<DIM('a). a$$i \<le> c$$i \<and> d$$i \<le> b$$i) \<Longrightarrow> {c<..<d} \<subseteq> {a .. b}" and
hoelzl@37489
  4691
 "(\<forall>i<DIM('a). a$$i \<le> c$$i \<and> d$$i \<le> b$$i) \<Longrightarrow> {c<..<d} \<subseteq> {a<..<b}"
hoelzl@37489
  4692
  unfolding subset_eq[unfolded Ball_def] unfolding mem_interval 
himmelma@33175
  4693
  by (auto intro: order_trans less_le_trans le_less_trans less_imp_le) (* BH: Why doesn't just "auto" work here? *)
himmelma@33175
  4694
hoelzl@37489
  4695
lemma interval_open_subset_closed:  fixes a :: "'a::ordered_euclidean_space" shows
himmelma@33175
  4696
 "{a<..<b} \<subseteq> {a .. b}"
himmelma@33175
  4697
proof(simp add: subset_eq, rule)
himmelma@33175
  4698
  fix x
himmelma@33175
  4699
  assume x:"x \<in>{a<..<b}"
hoelzl@37489
  4700
  { fix i assume "i<DIM('a)"
hoelzl@37489
  4701
    hence "a $$ i \<le> x $$ i"
hoelzl@37489
  4702
      using x order_less_imp_le[of "a$$i" "x$$i"] 
nipkow@39535
  4703
      by(simp add: set_eq_iff eucl_less[where 'a='a] eucl_le[where 'a='a] euclidean_eq)
himmelma@33175
  4704
  }
himmelma@33175
  4705
  moreover
hoelzl@37489
  4706
  { fix i assume "i<DIM('a)"
hoelzl@37489
  4707
    hence "x $$ i \<le> b $$ i"
hoelzl@37489
  4708
      using x order_less_imp_le[of "x$$i" "b$$i"]
nipkow@39535
  4709
      by(simp add: set_eq_iff eucl_less[where 'a='a] eucl_le[where 'a='a] euclidean_eq)
himmelma@33175
  4710
  }
himmelma@33175
  4711
  ultimately
himmelma@33175
  4712
  show "a \<le> x \<and> x \<le> b"
nipkow@39535
  4713
    by(simp add: set_eq_iff eucl_less[where 'a='a] eucl_le[where 'a='a] euclidean_eq)
hoelzl@37489
  4714
qed
hoelzl@37489
  4715
hoelzl@37489
  4716
lemma subset_interval: fixes a :: "'a::ordered_euclidean_space" shows
hoelzl@37489
  4717
 "{c .. d} \<subseteq> {a .. b} \<longleftrightarrow> (\<forall>i<DIM('a). c$$i \<le> d$$i) --> (\<forall>i<DIM('a). a$$i \<le> c$$i \<and> d$$i \<le> b$$i)" (is ?th1) and
hoelzl@37489
  4718
 "{c .. d} \<subseteq> {a<..<b} \<longleftrightarrow> (\<forall>i<DIM('a). c$$i \<le> d$$i) --> (\<forall>i<DIM('a). a$$i < c$$i \<and> d$$i < b$$i)" (is ?th2) and
hoelzl@37489
  4719
 "{c<..<d} \<subseteq> {a .. b} \<longleftrightarrow> (\<forall>i<DIM('a). c$$i < d$$i) --> (\<forall>i<DIM('a). a$$i \<le> c$$i \<and> d$$i \<le> b$$i)" (is ?th3) and
hoelzl@37489
  4720
 "{c<..<d} \<subseteq> {a<..<b} \<longleftrightarrow> (\<forall>i<DIM('a). c$$i < d$$i) --> (\<forall>i<DIM('a). a$$i \<le> c$$i \<and> d$$i \<le> b$$i)" (is ?th4)
himmelma@33175
  4721
proof-
himmelma@33175
  4722
  show ?th1 unfolding subset_eq and Ball_def and mem_interval by (auto intro: order_trans)
himmelma@33175
  4723
  show ?th2 unfolding subset_eq and Ball_def and mem_interval by (auto intro: le_less_trans less_le_trans order_trans less_imp_le)
hoelzl@37489
  4724
  { assume as: "{c<..<d} \<subseteq> {a .. b}" "\<forall>i<DIM('a). c$$i < d$$i"
hoelzl@37489
  4725
    hence "{c<..<d} \<noteq> {}" unfolding interval_eq_empty by auto
hoelzl@37489
  4726
    fix i assume i:"i<DIM('a)"
himmelma@33175
  4727
    (** TODO combine the following two parts as done in the HOL_light version. **)
hoelzl@37489
  4728
    { let ?x = "(\<chi>\<chi> j. (if j=i then ((min (a$$j) (d$$j))+c$$j)/2 else (c$$j+d$$j)/2))::'a"
hoelzl@37489
  4729
      assume as2: "a$$i > c$$i"
hoelzl@37489
  4730
      { fix j assume j:"j<DIM('a)"
hoelzl@37489
  4731
        hence "c $$ j < ?x $$ j \<and> ?x $$ j < d $$ j"
hoelzl@37489
  4732
          apply(cases "j=i") using as(2)[THEN spec[where x=j]] i
hoelzl@37489
  4733
          by (auto simp add: as2)  }
hoelzl@37489
  4734
      hence "?x\<in>{c<..<d}" using i unfolding mem_interval by auto
hoelzl@37489
  4735
      moreover
hoelzl@37489
  4736
      have "?x\<notin>{a .. b}"
hoelzl@37489
  4737
        unfolding mem_interval apply auto apply(rule_tac x=i in exI)
hoelzl@37489
  4738
        using as(2)[THEN spec[where x=i]] and as2 i
hoelzl@37489
  4739
        by auto
hoelzl@37489
  4740
      ultimately have False using as by auto  }
hoelzl@37489
  4741
    hence "a$$i \<le> c$$i" by(rule ccontr)auto
hoelzl@37489
  4742
    moreover
hoelzl@37489
  4743
    { let ?x = "(\<chi>\<chi> j. (if j=i then ((max (b$$j) (c$$j))+d$$j)/2 else (c$$j+d$$j)/2))::'a"
hoelzl@37489
  4744
      assume as2: "b$$i < d$$i"
hoelzl@37489
  4745
      { fix j assume "j<DIM('a)"
hoelzl@37489
  4746
        hence "d $$ j > ?x $$ j \<and> ?x $$ j > c $$ j" 
himmelma@33175
  4747
          apply(cases "j=i") using as(2)[THEN spec[where x=j]]
huffman@36358
  4748
          by (auto simp add: as2)  }
himmelma@33175
  4749
      hence "?x\<in>{c<..<d}" unfolding mem_interval by auto
himmelma@33175
  4750
      moreover
himmelma@33175
  4751
      have "?x\<notin>{a .. b}"
himmelma@33175
  4752
        unfolding mem_interval apply auto apply(rule_tac x=i in exI)
hoelzl@37489
  4753
        using as(2)[THEN spec[where x=i]] and as2 using i
huffman@36358
  4754
        by auto
himmelma@33175
  4755
      ultimately have False using as by auto  }
hoelzl@37489
  4756
    hence "b$$i \<ge> d$$i" by(rule ccontr)auto
himmelma@33175
  4757
    ultimately
hoelzl@37489
  4758
    have "a$$i \<le> c$$i \<and> d$$i \<le> b$$i" by auto
himmelma@33175
  4759
  } note part1 = this
hoelzl@37489
  4760
  show ?th3 unfolding subset_eq and Ball_def and mem_interval 
hoelzl@37489
  4761
    apply(rule,rule,rule,rule) apply(rule part1) unfolding subset_eq and Ball_def and mem_interval
hoelzl@37489
  4762
    prefer 4 apply auto by(erule_tac x=i in allE,erule_tac x=i in allE,fastsimp)+ 
hoelzl@37489
  4763
  { assume as:"{c<..<d} \<subseteq> {a<..<b}" "\<forall>i<DIM('a). c$$i < d$$i"
hoelzl@37489
  4764
    fix i assume i:"i<DIM('a)"
himmelma@33175
  4765
    from as(1) have "{c<..<d} \<subseteq> {a..b}" using interval_open_subset_closed[of a b] by auto
hoelzl@37489
  4766
    hence "a$$i \<le> c$$i \<and> d$$i \<le> b$$i" using part1 and as(2) using i by auto  } note * = this
hoelzl@37489
  4767
  show ?th4 unfolding subset_eq and Ball_def and mem_interval 
hoelzl@37489
  4768
    apply(rule,rule,rule,rule) apply(rule *) unfolding subset_eq and Ball_def and mem_interval prefer 4
hoelzl@37489
  4769
    apply auto by(erule_tac x=i in allE, simp)+ 
hoelzl@37489
  4770
qed
hoelzl@37489
  4771
hoelzl@37489
  4772
lemma disjoint_interval: fixes a::"'a::ordered_euclidean_space" shows
hoelzl@37489
  4773
  "{a .. b} \<inter> {c .. d} = {} \<longleftrightarrow> (\<exists>i<DIM('a). (b$$i < a$$i \<or> d$$i < c$$i \<or> b$$i < c$$i \<or> d$$i < a$$i))" (is ?th1) and
hoelzl@37489
  4774
  "{a .. b} \<inter> {c<..<d} = {} \<longleftrightarrow> (\<exists>i<DIM('a). (b$$i < a$$i \<or> d$$i \<le> c$$i \<or> b$$i \<le> c$$i \<or> d$$i \<le> a$$i))" (is ?th2) and
hoelzl@37489
  4775
  "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> (\<exists>i<DIM('a). (b$$i \<le> a$$i \<or> d$$i < c$$i \<or> b$$i \<le> c$$i \<or> d$$i \<le> a$$i))" (is ?th3) and
hoelzl@37489
  4776
  "{a<..<b} \<inter> {c<..<d} = {} \<longleftrightarrow> (\<exists>i<DIM('a). (b$$i \<le> a$$i \<or> d$$i \<le> c$$i \<or> b$$i \<le> c$$i \<or> d$$i \<le> a$$i))" (is ?th4)
himmelma@33175
  4777
proof-
hoelzl@37489
  4778
  let ?z = "(\<chi>\<chi> i. ((max (a$$i) (c$$i)) + (min (b$$i) (d$$i))) / 2)::'a"
nipkow@39535
  4779
  note * = set_eq_iff Int_iff empty_iff mem_interval all_conj_distrib[THEN sym] eq_False
hoelzl@37489
  4780
  show ?th1 unfolding * apply safe apply(erule_tac x="?z" in allE)
hoelzl@37489
  4781
    unfolding not_all apply(erule exE,rule_tac x=x in exI) apply(erule_tac[2-] x=i in allE) by auto
hoelzl@37489
  4782
  show ?th2 unfolding * apply safe apply(erule_tac x="?z" in allE)
hoelzl@37489
  4783
    unfolding not_all apply(erule exE,rule_tac x=x in exI) apply(erule_tac[2-] x=i in allE) by auto
hoelzl@37489
  4784
  show ?th3 unfolding * apply safe apply(erule_tac x="?z" in allE)
hoelzl@37489
  4785
    unfolding not_all apply(erule exE,rule_tac x=x in exI) apply(erule_tac[2-] x=i in allE) by auto
hoelzl@37489
  4786
  show ?th4 unfolding * apply safe apply(erule_tac x="?z" in allE)
hoelzl@37489
  4787
    unfolding not_all apply(erule exE,rule_tac x=x in exI) apply(erule_tac[2-] x=i in allE) by auto
hoelzl@37489
  4788
qed
hoelzl@37489
  4789
hoelzl@37489
  4790
lemma inter_interval: fixes a :: "'a::ordered_euclidean_space" shows
hoelzl@37489
  4791
 "{a .. b} \<inter> {c .. d} =  {(\<chi>\<chi> i. max (a$$i) (c$$i)) .. (\<chi>\<chi> i. min (b$$i) (d$$i))}"
nipkow@39535
  4792
  unfolding set_eq_iff and Int_iff and mem_interval
huffman@36358
  4793
  by auto
himmelma@33175
  4794
himmelma@33175
  4795
(* Moved interval_open_subset_closed a bit upwards *)
himmelma@33175
  4796
huffman@45117
  4797
lemma open_interval[intro]:
huffman@45117
  4798
  fixes a b :: "'a::ordered_euclidean_space" shows "open {a<..<b}"
himmelma@33175
  4799
proof-
huffman@45117
  4800
  have "open (\<Inter>i<DIM('a). (\<lambda>x. x$$i) -` {a$$i<..<b$$i})"
huffman@45117
  4801
    by (intro open_INT finite_lessThan ballI continuous_open_vimage allI
huffman@45117
  4802
      linear_continuous_at bounded_linear_euclidean_component
huffman@45117
  4803
      open_real_greaterThanLessThan)
huffman@45117
  4804
  also have "(\<Inter>i<DIM('a). (\<lambda>x. x$$i) -` {a$$i<..<b$$i}) = {a<..<b}"
huffman@45117
  4805
    by (auto simp add: eucl_less [where 'a='a])
huffman@45117
  4806
  finally show "open {a<..<b}" .
huffman@45117
  4807
qed
huffman@45117
  4808
huffman@45117
  4809
lemma closed_interval[intro]:
huffman@45117
  4810
  fixes a b :: "'a::ordered_euclidean_space" shows "closed {a .. b}"
himmelma@33175
  4811
proof-
huffman@45117
  4812
  have "closed (\<Inter>i<DIM('a). (\<lambda>x. x$$i) -` {a$$i .. b$$i})"
huffman@45117
  4813
    by (intro closed_INT ballI continuous_closed_vimage allI
huffman@45117
  4814
      linear_continuous_at bounded_linear_euclidean_component
huffman@45117
  4815
      closed_real_atLeastAtMost)
huffman@45117
  4816
  also have "(\<Inter>i<DIM('a). (\<lambda>x. x$$i) -` {a$$i .. b$$i}) = {a .. b}"
huffman@45117
  4817
    by (auto simp add: eucl_le [where 'a='a])
huffman@45117
  4818
  finally show "closed {a .. b}" .
himmelma@33175
  4819
qed
himmelma@33175
  4820
hoelzl@37489
  4821
lemma interior_closed_interval[intro]: fixes a :: "'a::ordered_euclidean_space" shows
himmelma@33175
  4822
 "interior {a .. b} = {a<..<b}" (is "?L = ?R")
himmelma@33175
  4823
proof(rule subset_antisym)
himmelma@33175
  4824
  show "?R \<subseteq> ?L" using interior_maximal[OF interval_open_subset_closed open_interval] by auto
himmelma@33175
  4825
next
himmelma@33175
  4826
  { fix x assume "\<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> {a..b}"
himmelma@33175
  4827
    then obtain s where s:"open s" "x \<in> s" "s \<subseteq> {a..b}" by auto
himmelma@33175
  4828
    then obtain e where "e>0" and e:"\<forall>x'. dist x' x < e \<longrightarrow> x' \<in> {a..b}" unfolding open_dist and subset_eq by auto
hoelzl@37489
  4829
    { fix i assume i:"i<DIM('a)"
himmelma@33175
  4830
      have "dist (x - (e / 2) *\<^sub>R basis i) x < e"
himmelma@33175
  4831
           "dist (x + (e / 2) *\<^sub>R basis i) x < e"
himmelma@33175
  4832
        unfolding dist_norm apply auto
hoelzl@37489
  4833
        unfolding norm_minus_cancel using norm_basis and `e>0` by auto
hoelzl@37489
  4834
      hence "a $$ i \<le> (x - (e / 2) *\<^sub>R basis i) $$ i"
hoelzl@37489
  4835
                     "(x + (e / 2) *\<^sub>R basis i) $$ i \<le> b $$ i"
himmelma@33175
  4836
        using e[THEN spec[where x="x - (e/2) *\<^sub>R basis i"]]
himmelma@33175
  4837
        and   e[THEN spec[where x="x + (e/2) *\<^sub>R basis i"]]
himmelma@33175
  4838
        unfolding mem_interval by (auto elim!: allE[where x=i])
hoelzl@37489
  4839
      hence "a $$ i < x $$ i" and "x $$ i < b $$ i" unfolding euclidean_simps
hoelzl@37489
  4840
        unfolding basis_component using `e>0` i by auto  }
himmelma@33175
  4841
    hence "x \<in> {a<..<b}" unfolding mem_interval by auto  }
himmelma@33175
  4842
  thus "?L \<subseteq> ?R" unfolding interior_def and subset_eq by auto
himmelma@33175
  4843
qed
himmelma@33175
  4844
hoelzl@37489
  4845
lemma bounded_closed_interval: fixes a :: "'a::ordered_euclidean_space" shows "bounded {a .. b}"
himmelma@33175
  4846
proof-
hoelzl@37489
  4847
  let ?b = "\<Sum>i<DIM('a). \<bar>a$$i\<bar> + \<bar>b$$i\<bar>"
hoelzl@37489
  4848
  { fix x::"'a" assume x:"\<forall>i<DIM('a). a $$ i \<le> x $$ i \<and> x $$ i \<le> b $$ i"
hoelzl@37489
  4849
    { fix i assume "i<DIM('a)"
hoelzl@37489
  4850
      hence "\<bar>x$$i\<bar> \<le> \<bar>a$$i\<bar> + \<bar>b$$i\<bar>" using x[THEN spec[where x=i]] by auto  }
hoelzl@37489
  4851
    hence "(\<Sum>i<DIM('a). \<bar>x $$ i\<bar>) \<le> ?b" apply-apply(rule setsum_mono) by auto
himmelma@33175
  4852
    hence "norm x \<le> ?b" using norm_le_l1[of x] by auto  }
himmelma@33175
  4853
  thus ?thesis unfolding interval and bounded_iff by auto
himmelma@33175
  4854
qed
himmelma@33175
  4855
hoelzl@37489
  4856
lemma bounded_interval: fixes a :: "'a::ordered_euclidean_space" shows
himmelma@33175
  4857
 "bounded {a .. b} \<and> bounded {a<..<b}"
himmelma@33175
  4858
  using bounded_closed_interval[of a b]
himmelma@33175
  4859
  using interval_open_subset_closed[of a b]
himmelma@33175
  4860
  using bounded_subset[of "{a..b}" "{a<..<b}"]
himmelma@33175
  4861
  by simp
himmelma@33175
  4862
hoelzl@37489
  4863
lemma not_interval_univ: fixes a :: "'a::ordered_euclidean_space" shows
himmelma@33175
  4864
 "({a .. b} \<noteq> UNIV) \<and> ({a<..<b} \<noteq> UNIV)"
hoelzl@37489
  4865
  using bounded_interval[of a b] by auto
hoelzl@37489
  4866
hoelzl@37489
  4867
lemma compact_interval: fixes a :: "'a::ordered_euclidean_space" shows "compact {a .. b}"
hoelzl@37489
  4868
  using bounded_closed_imp_compact[of "{a..b}"] using bounded_interval[of a b]
himmelma@33175
  4869
  by auto
himmelma@33175
  4870
hoelzl@37489
  4871
lemma open_interval_midpoint: fixes a :: "'a::ordered_euclidean_space"
himmelma@33175
  4872
  assumes "{a<..<b} \<noteq> {}" shows "((1/2) *\<^sub>R (a + b)) \<in> {a<..<b}"
himmelma@33175
  4873
proof-
hoelzl@37489
  4874
  { fix i assume "i<DIM('a)"
hoelzl@37489
  4875
    hence "a $$ i < ((1 / 2) *\<^sub>R (a + b)) $$ i \<and> ((1 / 2) *\<^sub>R (a + b)) $$ i < b $$ i"
himmelma@33175
  4876
      using assms[unfolded interval_ne_empty, THEN spec[where x=i]]
hoelzl@37489
  4877
      unfolding euclidean_simps by auto  }
himmelma@33175
  4878
  thus ?thesis unfolding mem_interval by auto
himmelma@33175
  4879
qed
himmelma@33175
  4880
hoelzl@37489
  4881
lemma open_closed_interval_convex: fixes x :: "'a::ordered_euclidean_space"
himmelma@33175
  4882
  assumes x:"x \<in> {a<..<b}" and y:"y \<in> {a .. b}" and e:"0 < e" "e \<le> 1"
himmelma@33175
  4883
  shows "(e *\<^sub>R x + (1 - e) *\<^sub>R y) \<in> {a<..<b}"
himmelma@33175
  4884
proof-
hoelzl@37489
  4885
  { fix i assume i:"i<DIM('a)"
hoelzl@37489
  4886
    have "a $$ i = e * a$$i + (1 - e) * a$$i" unfolding left_diff_distrib by simp
hoelzl@37489
  4887
    also have "\<dots> < e * x $$ i + (1 - e) * y $$ i" apply(rule add_less_le_mono)
himmelma@33175
  4888
      using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all
hoelzl@37489
  4889
      using x unfolding mem_interval using i apply simp
hoelzl@37489
  4890
      using y unfolding mem_interval using i apply simp
himmelma@33175
  4891
      done
hoelzl@37489
  4892
    finally have "a $$ i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) $$ i" unfolding euclidean_simps by auto
himmelma@33175
  4893
    moreover {
hoelzl@37489
  4894
    have "b $$ i = e * b$$i + (1 - e) * b$$i" unfolding left_diff_distrib by simp
hoelzl@37489
  4895
    also have "\<dots> > e * x $$ i + (1 - e) * y $$ i" apply(rule add_less_le_mono)
himmelma@33175
  4896
      using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all
hoelzl@37489
  4897
      using x unfolding mem_interval using i apply simp
hoelzl@37489
  4898
      using y unfolding mem_interval using i apply simp
himmelma@33175
  4899
      done
hoelzl@37489
  4900
    finally have "(e *\<^sub>R x + (1 - e) *\<^sub>R y) $$ i < b $$ i" unfolding euclidean_simps by auto
hoelzl@37489
  4901
    } ultimately have "a $$ i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) $$ i \<and> (e *\<^sub>R x + (1 - e) *\<^sub>R y) $$ i < b $$ i" by auto }
himmelma@33175
  4902
  thus ?thesis unfolding mem_interval by auto
himmelma@33175
  4903
qed
himmelma@33175
  4904
hoelzl@37489
  4905
lemma closure_open_interval: fixes a :: "'a::ordered_euclidean_space"
himmelma@33175
  4906
  assumes "{a<..<b} \<noteq> {}"
himmelma@33175
  4907
  shows "closure {a<..<b} = {a .. b}"
himmelma@33175
  4908
proof-
hoelzl@37489
  4909
  have ab:"a < b" using assms[unfolded interval_ne_empty] apply(subst eucl_less) by auto
himmelma@33175
  4910
  let ?c = "(1 / 2) *\<^sub>R (a + b)"
himmelma@33175
  4911
  { fix x assume as:"x \<in> {a .. b}"
himmelma@33175
  4912
    def f == "\<lambda>n::nat. x + (inverse (real n + 1)) *\<^sub>R (?c - x)"
himmelma@33175
  4913
    { fix n assume fn:"f n < b \<longrightarrow> a < f n \<longrightarrow> f n = x" and xc:"x \<noteq> ?c"
himmelma@33175
  4914
      have *:"0 < inverse (real n + 1)" "inverse (real n + 1) \<le> 1" unfolding inverse_le_1_iff by auto
himmelma@33175
  4915
      have "(inverse (real n + 1)) *\<^sub>R ((1 / 2) *\<^sub>R (a + b)) + (1 - inverse (real n + 1)) *\<^sub>R x =
himmelma@33175
  4916
        x + (inverse (real n + 1)) *\<^sub>R (((1 / 2) *\<^sub>R (a + b)) - x)"
himmelma@33175
  4917
        by (auto simp add: algebra_simps)
himmelma@33175
  4918
      hence "f n < b" and "a < f n" using open_closed_interval_convex[OF open_interval_midpoint[OF assms] as *] unfolding f_def by auto
hoelzl@37489
  4919
      hence False using fn unfolding f_def using xc by auto  }
himmelma@33175
  4920
    moreover
himmelma@33175
  4921
    { assume "\<not> (f ---> x) sequentially"
himmelma@33175
  4922
      { fix e::real assume "e>0"
himmelma@33175
  4923
        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
  4924
        then obtain N::nat where "inverse (real (N + 1)) < e" by auto
himmelma@33175
  4925
        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
  4926
        hence "\<exists>N::nat. \<forall>n\<ge>N. inverse (real n + 1) < e" by auto  }
himmelma@33175
  4927
      hence "((\<lambda>n. inverse (real n + 1)) ---> 0) sequentially"
himmelma@33175
  4928
        unfolding Lim_sequentially by(auto simp add: dist_norm)
himmelma@33175
  4929
      hence "(f ---> x) sequentially" unfolding f_def
huffman@44983
  4930
        using tendsto_add[OF tendsto_const, of "\<lambda>n::nat. (inverse (real n + 1)) *\<^sub>R ((1 / 2) *\<^sub>R (a + b) - x)" 0 sequentially x]
huffman@45145
  4931
        using tendsto_scaleR [OF _ tendsto_const, of "\<lambda>n::nat. inverse (real n + 1)" 0 sequentially "((1 / 2) *\<^sub>R (a + b) - x)"] by auto  }
himmelma@33175
  4932
    ultimately have "x \<in> closure {a<..<b}"
himmelma@33175
  4933
      using as and open_interval_midpoint[OF assms] unfolding closure_def unfolding islimpt_sequential by(cases "x=?c")auto  }
himmelma@33175
  4934
  thus ?thesis using closure_minimal[OF interval_open_subset_closed closed_interval, of a b] by blast
himmelma@33175
  4935
qed
himmelma@33175
  4936
hoelzl@37489
  4937
lemma bounded_subset_open_interval_symmetric: fixes s::"('a::ordered_euclidean_space) set"
himmelma@33175
  4938
  assumes "bounded s"  shows "\<exists>a. s \<subseteq> {-a<..<a}"
himmelma@33175
  4939
proof-
himmelma@33175
  4940
  obtain b where "b>0" and b:"\<forall>x\<in>s. norm x \<le> b" using assms[unfolded bounded_pos] by auto
hoelzl@37489
  4941
  def a \<equiv> "(\<chi>\<chi> i. b+1)::'a"
himmelma@33175
  4942
  { fix x assume "x\<in>s"
hoelzl@37489
  4943
    fix i assume i:"i<DIM('a)"
hoelzl@37489
  4944
    hence "(-a)$$i < x$$i" and "x$$i < a$$i" using b[THEN bspec[where x=x], OF `x\<in>s`]
hoelzl@37489
  4945
      and component_le_norm[of x i] unfolding euclidean_simps and a_def by auto  }
hoelzl@37489
  4946
  thus ?thesis by(auto intro: exI[where x=a] simp add: eucl_less[where 'a='a])
himmelma@33175
  4947
qed
himmelma@33175
  4948
himmelma@33175
  4949
lemma bounded_subset_open_interval:
hoelzl@37489
  4950
  fixes s :: "('a::ordered_euclidean_space) set"
himmelma@33175
  4951
  shows "bounded s ==> (\<exists>a b. s \<subseteq> {a<..<b})"
himmelma@33175
  4952
  by (auto dest!: bounded_subset_open_interval_symmetric)
himmelma@33175
  4953
himmelma@33175
  4954
lemma bounded_subset_closed_interval_symmetric:
hoelzl@37489
  4955
  fixes s :: "('a::ordered_euclidean_space) set"
himmelma@33175
  4956
  assumes "bounded s" shows "\<exists>a. s \<subseteq> {-a .. a}"
himmelma@33175
  4957
proof-
himmelma@33175
  4958
  obtain a where "s \<subseteq> {- a<..<a}" using bounded_subset_open_interval_symmetric[OF assms] by auto
himmelma@33175
  4959
  thus ?thesis using interval_open_subset_closed[of "-a" a] by auto
himmelma@33175
  4960
qed
himmelma@33175
  4961
himmelma@33175
  4962
lemma bounded_subset_closed_interval:
hoelzl@37489
  4963
  fixes s :: "('a::ordered_euclidean_space) set"
himmelma@33175
  4964
  shows "bounded s ==> (\<exists>a b. s \<subseteq> {a .. b})"
himmelma@33175
  4965
  using bounded_subset_closed_interval_symmetric[of s] by auto
himmelma@33175
  4966
himmelma@33175
  4967
lemma frontier_closed_interval:
hoelzl@37489
  4968
  fixes a b :: "'a::ordered_euclidean_space"
himmelma@33175
  4969
  shows "frontier {a .. b} = {a .. b} - {a<..<b}"
himmelma@33175
  4970
  unfolding frontier_def unfolding interior_closed_interval and closure_closed[OF closed_interval] ..
himmelma@33175
  4971
himmelma@33175
  4972
lemma frontier_open_interval:
hoelzl@37489
  4973
  fixes a b :: "'a::ordered_euclidean_space"
himmelma@33175
  4974
  shows "frontier {a<..<b} = (if {a<..<b} = {} then {} else {a .. b} - {a<..<b})"
himmelma@33175
  4975
proof(cases "{a<..<b} = {}")
himmelma@33175
  4976
  case True thus ?thesis using frontier_empty by auto
himmelma@33175
  4977
next
himmelma@33175
  4978
  case False thus ?thesis unfolding frontier_def and closure_open_interval[OF False] and interior_open[OF open_interval] by auto
himmelma@33175
  4979
qed
himmelma@33175
  4980
hoelzl@37489
  4981
lemma inter_interval_mixed_eq_empty: fixes a :: "'a::ordered_euclidean_space"
himmelma@33175
  4982
  assumes "{c<..<d} \<noteq> {}"  shows "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> {a<..<b} \<inter> {c<..<d} = {}"
himmelma@33175
  4983
  unfolding closure_open_interval[OF assms, THEN sym] unfolding open_inter_closure_eq_empty[OF open_interval] ..
himmelma@33175
  4984
himmelma@33175
  4985
himmelma@33175
  4986
(* Some stuff for half-infinite intervals too; FIXME: notation?  *)
himmelma@33175
  4987
huffman@37673
  4988
lemma closed_interval_left: fixes b::"'a::euclidean_space"
hoelzl@37489
  4989
  shows "closed {x::'a. \<forall>i<DIM('a). x$$i \<le> b$$i}"
himmelma@33175
  4990
proof-
hoelzl@37489
  4991
  { fix i assume i:"i<DIM('a)"
hoelzl@37489
  4992
    fix x::"'a" assume x:"\<forall>e>0. \<exists>x'\<in>{x. \<forall>i<DIM('a). x $$ i \<le> b $$ i}. x' \<noteq> x \<and> dist x' x < e"
hoelzl@37489
  4993
    { assume "x$$i > b$$i"
hoelzl@37489
  4994
      then obtain y where "y $$ i \<le> b $$ i"  "y \<noteq> x"  "dist y x < x$$i - b$$i"
hoelzl@37489
  4995
        using x[THEN spec[where x="x$$i - b$$i"]] using i by auto
hoelzl@37489
  4996
      hence False using component_le_norm[of "y - x" i] unfolding dist_norm euclidean_simps using i 
hoelzl@37489
  4997
        by auto   }
hoelzl@37489
  4998
    hence "x$$i \<le> b$$i" by(rule ccontr)auto  }
himmelma@33175
  4999
  thus ?thesis unfolding closed_limpt unfolding islimpt_approachable by blast
himmelma@33175
  5000
qed
himmelma@33175
  5001
huffman@37673
  5002
lemma closed_interval_right: fixes a::"'a::euclidean_space"
hoelzl@37489
  5003
  shows "closed {x::'a. \<forall>i<DIM('a). a$$i \<le> x$$i}"
himmelma@33175
  5004
proof-
hoelzl@37489
  5005
  { fix i assume i:"i<DIM('a)"
hoelzl@37489
  5006
    fix x::"'a" assume x:"\<forall>e>0. \<exists>x'\<in>{x. \<forall>i<DIM('a). a $$ i \<le> x $$ i}. x' \<noteq> x \<and> dist x' x < e"
hoelzl@37489
  5007
    { assume "a$$i > x$$i"
hoelzl@37489
  5008
      then obtain y where "a $$ i \<le> y $$ i"  "y \<noteq> x"  "dist y x < a$$i - x$$i"
hoelzl@37489
  5009
        using x[THEN spec[where x="a$$i - x$$i"]] i by auto
hoelzl@37489
  5010
      hence False using component_le_norm[of "y - x" i] unfolding dist_norm and euclidean_simps by auto   }
hoelzl@37489
  5011
    hence "a$$i \<le> x$$i" by(rule ccontr)auto  }
himmelma@33175
  5012
  thus ?thesis unfolding closed_limpt unfolding islimpt_approachable by blast
himmelma@33175
  5013
qed
himmelma@33175
  5014
huffman@36435
  5015
text {* Intervals in general, including infinite and mixtures of open and closed. *}
himmelma@33175
  5016
huffman@37731
  5017
definition "is_interval (s::('a::euclidean_space) set) \<longleftrightarrow>
hoelzl@37489
  5018
  (\<forall>a\<in>s. \<forall>b\<in>s. \<forall>x. (\<forall>i<DIM('a). ((a$$i \<le> x$$i \<and> x$$i \<le> b$$i) \<or> (b$$i \<le> x$$i \<and> x$$i \<le> a$$i))) \<longrightarrow> x \<in> s)"
hoelzl@37489
  5019
hoelzl@37489
  5020
lemma is_interval_interval: "is_interval {a .. b::'a::ordered_euclidean_space}" (is ?th1)
hoelzl@39320
  5021
  "is_interval {a<..<b}" (is ?th2) proof -
himmelma@33175
  5022
  have *:"\<And>x y z::real. x < y \<Longrightarrow> y < z \<Longrightarrow> x < z" by auto
himmelma@33175
  5023
  show ?th1 ?th2  unfolding is_interval_def mem_interval Ball_def atLeastAtMost_iff
huffman@36770
  5024
    by(meson order_trans le_less_trans less_le_trans *)+ qed
himmelma@33175
  5025
himmelma@33175
  5026
lemma is_interval_empty:
himmelma@33175
  5027
 "is_interval {}"
himmelma@33175
  5028
  unfolding is_interval_def
himmelma@33175
  5029
  by simp
himmelma@33175
  5030
himmelma@33175
  5031
lemma is_interval_univ:
himmelma@33175
  5032
 "is_interval UNIV"
himmelma@33175
  5033
  unfolding is_interval_def
himmelma@33175
  5034
  by simp
himmelma@33175
  5035
huffman@45081
  5036
huffman@45081
  5037
subsection {* Closure of halfspaces and hyperplanes *}
himmelma@33175
  5038
huffman@45090
  5039
lemma isCont_open_vimage:
huffman@45090
  5040
  assumes "\<And>x. isCont f x" and "open s" shows "open (f -` s)"
huffman@45090
  5041
proof -
huffman@45090
  5042
  from assms(1) have "continuous_on UNIV f"
huffman@45090
  5043
    unfolding isCont_def continuous_on_def within_UNIV by simp
huffman@45090
  5044
  hence "open {x \<in> UNIV. f x \<in> s}"
huffman@45090
  5045
    using open_UNIV `open s` by (rule continuous_open_preimage)
huffman@45090
  5046
  thus "open (f -` s)"
huffman@45090
  5047
    by (simp add: vimage_def)
huffman@45090
  5048
qed
huffman@45090
  5049
huffman@45090
  5050
lemma isCont_closed_vimage:
huffman@45090
  5051
  assumes "\<And>x. isCont f x" and "closed s" shows "closed (f -` s)"
huffman@45090
  5052
  using assms unfolding closed_def vimage_Compl [symmetric]
huffman@45090
  5053
  by (rule isCont_open_vimage)
huffman@45090
  5054
huffman@45084
  5055
lemma open_Collect_less:
huffman@45090
  5056
  fixes f g :: "'a::topological_space \<Rightarrow> real"
huffman@45090
  5057
  assumes f: "\<And>x. isCont f x"
huffman@45090
  5058
  assumes g: "\<And>x. isCont g x"
huffman@45084
  5059
  shows "open {x. f x < g x}"
huffman@45084
  5060
proof -
huffman@45084
  5061
  have "open ((\<lambda>x. g x - f x) -` {0<..})"
huffman@45090
  5062
    using isCont_diff [OF g f] open_real_greaterThan
huffman@45090
  5063
    by (rule isCont_open_vimage)
huffman@45084
  5064
  also have "((\<lambda>x. g x - f x) -` {0<..}) = {x. f x < g x}"
huffman@45084
  5065
    by auto
huffman@45084
  5066
  finally show ?thesis .
huffman@45084
  5067
qed
huffman@45084
  5068
huffman@45084
  5069
lemma closed_Collect_le:
huffman@45090
  5070
  fixes f g :: "'a::topological_space \<Rightarrow> real"
huffman@45090
  5071
  assumes f: "\<And>x. isCont f x"
huffman@45090
  5072
  assumes g: "\<And>x. isCont g x"
huffman@45084
  5073
  shows "closed {x. f x \<le> g x}"
huffman@45084
  5074
proof -
huffman@45084
  5075
  have "closed ((\<lambda>x. g x - f x) -` {0..})"
huffman@45090
  5076
    using isCont_diff [OF g f] closed_real_atLeast
huffman@45090
  5077
    by (rule isCont_closed_vimage)
huffman@45084
  5078
  also have "((\<lambda>x. g x - f x) -` {0..}) = {x. f x \<le> g x}"
huffman@45084
  5079
    by auto
huffman@45084
  5080
  finally show ?thesis .
huffman@45084
  5081
qed
huffman@45084
  5082
huffman@45084
  5083
lemma closed_Collect_eq:
huffman@45090
  5084
  fixes f g :: "'a::topological_space \<Rightarrow> 'b::t2_space"
huffman@45090
  5085
  assumes f: "\<And>x. isCont f x"
huffman@45090
  5086
  assumes g: "\<And>x. isCont g x"
huffman@45084
  5087
  shows "closed {x. f x = g x}"
huffman@45084
  5088
proof -
huffman@45087
  5089
  have "open {(x::'b, y::'b). x \<noteq> y}"
huffman@45087
  5090
    unfolding open_prod_def by (auto dest!: hausdorff)
huffman@45087
  5091
  hence "closed {(x::'b, y::'b). x = y}"
huffman@45087
  5092
    unfolding closed_def split_def Collect_neg_eq .
huffman@45090
  5093
  with isCont_Pair [OF f g]
huffman@45087
  5094
  have "closed ((\<lambda>x. (f x, g x)) -` {(x, y). x = y})"
huffman@45090
  5095
    by (rule isCont_closed_vimage)
huffman@45087
  5096
  also have "\<dots> = {x. f x = g x}" by auto
huffman@45084
  5097
  finally show ?thesis .
huffman@45084
  5098
qed
huffman@45084
  5099
himmelma@33175
  5100
lemma Lim_inner:
himmelma@33175
  5101
  assumes "(f ---> l) net"  shows "((\<lambda>y. inner a (f y)) ---> inner a l) net"
himmelma@33175
  5102
  by (intro tendsto_intros assms)
himmelma@33175
  5103
himmelma@33175
  5104
lemma continuous_at_inner: "continuous (at x) (inner a)"
himmelma@33175
  5105
  unfolding continuous_at by (intro tendsto_intros)
himmelma@33175
  5106
hoelzl@39320
  5107
lemma continuous_at_euclidean_component[intro!, simp]: "continuous (at x) (\<lambda>x. x $$ i)"
hoelzl@39320
  5108
  unfolding euclidean_component_def by (rule continuous_at_inner)
hoelzl@39320
  5109
himmelma@33175
  5110
lemma continuous_on_inner:
himmelma@33175
  5111
  fixes s :: "'a::real_inner set"
himmelma@33175
  5112
  shows "continuous_on s (inner a)"
himmelma@33175
  5113
  unfolding continuous_on by (rule ballI) (intro tendsto_intros)
himmelma@33175
  5114
himmelma@33175
  5115
lemma closed_halfspace_le: "closed {x. inner a x \<le> b}"
huffman@45104
  5116
  by (simp add: closed_Collect_le)
himmelma@33175
  5117
himmelma@33175
  5118
lemma closed_halfspace_ge: "closed {x. inner a x \<ge> b}"
huffman@45104
  5119
  by (simp add: closed_Collect_le)
himmelma@33175
  5120
himmelma@33175
  5121
lemma closed_hyperplane: "closed {x. inner a x = b}"
huffman@45104
  5122
  by (simp add: closed_Collect_eq)
himmelma@33175
  5123
himmelma@33175
  5124
lemma closed_halfspace_component_le:
huffman@37673
  5125
  shows "closed {x::'a::euclidean_space. x$$i \<le> a}"
huffman@45104
  5126
  by (simp add: closed_Collect_le)
himmelma@33175
  5127
himmelma@33175
  5128
lemma closed_halfspace_component_ge:
huffman@37673
  5129
  shows "closed {x::'a::euclidean_space. x$$i \<ge> a}"
huffman@45104
  5130
  by (simp add: closed_Collect_le)
himmelma@33175
  5131
huffman@45081
  5132
text {* Openness of halfspaces. *}
himmelma@33175
  5133
himmelma@33175
  5134
lemma open_halfspace_lt: "open {x. inner a x < b}"
huffman@45104
  5135
  by (simp add: open_Collect_less)
himmelma@33175
  5136
himmelma@33175
  5137
lemma open_halfspace_gt: "open {x. inner a x > b}"
huffman@45104
  5138
  by (simp add: open_Collect_less)
himmelma@33175
  5139
himmelma@33175
  5140
lemma open_halfspace_component_lt:
huffman@37673
  5141
  shows "open {x::'a::euclidean_space. x$$i < a}"
huffman@45104
  5142
  by (simp add: open_Collect_less)
himmelma@33175
  5143
himmelma@33175
  5144
lemma open_halfspace_component_gt:
huffman@45090
  5145
  shows "open {x::'a::euclidean_space. x$$i > a}"
huffman@45104
  5146
  by (simp add: open_Collect_less)
himmelma@33175
  5147
hoelzl@38902
  5148
text{* Instantiation for intervals on @{text ordered_euclidean_space} *}
hoelzl@38902
  5149
hoelzl@38902
  5150
lemma eucl_lessThan_eq_halfspaces:
hoelzl@38902
  5151
  fixes a :: "'a\<Colon>ordered_euclidean_space"
hoelzl@38902
  5152
  shows "{..<a} = (\<Inter>i<DIM('a). {x. x $$ i < a $$ i})"
hoelzl@38902
  5153
 by (auto simp: eucl_less[where 'a='a])
hoelzl@38902
  5154
hoelzl@38902
  5155
lemma eucl_greaterThan_eq_halfspaces:
hoelzl@38902
  5156
  fixes a :: "'a\<Colon>ordered_euclidean_space"
hoelzl@38902
  5157
  shows "{a<..} = (\<Inter>i<DIM('a). {x. a $$ i < x $$ i})"
hoelzl@38902
  5158
 by (auto simp: eucl_less[where 'a='a])
hoelzl@38902
  5159
hoelzl@38902
  5160
lemma eucl_atMost_eq_halfspaces:
hoelzl@38902
  5161
  fixes a :: "'a\<Colon>ordered_euclidean_space"
hoelzl@38902
  5162
  shows "{.. a} = (\<Inter>i<DIM('a). {x. x $$ i \<le> a $$ i})"
hoelzl@38902
  5163
 by (auto simp: eucl_le[where 'a='a])
hoelzl@38902
  5164
hoelzl@38902
  5165
lemma eucl_atLeast_eq_halfspaces:
hoelzl@38902
  5166
  fixes a :: "'a\<Colon>ordered_euclidean_space"
hoelzl@38902
  5167
  shows "{a ..} = (\<Inter>i<DIM('a). {x. a $$ i \<le> x $$ i})"
hoelzl@38902
  5168
 by (auto simp: eucl_le[where 'a='a])
hoelzl@38902
  5169
hoelzl@38902
  5170
lemma open_eucl_lessThan[simp, intro]:
hoelzl@38902
  5171
  fixes a :: "'a\<Colon>ordered_euclidean_space"
hoelzl@38902
  5172
  shows "open {..< a}"
hoelzl@38902
  5173
  by (auto simp: eucl_lessThan_eq_halfspaces open_halfspace_component_lt)
hoelzl@38902
  5174
hoelzl@38902
  5175
lemma open_eucl_greaterThan[simp, intro]:
hoelzl@38902
  5176
  fixes a :: "'a\<Colon>ordered_euclidean_space"
hoelzl@38902
  5177
  shows "open {a <..}"
hoelzl@38902
  5178
  by (auto simp: eucl_greaterThan_eq_halfspaces open_halfspace_component_gt)
hoelzl@38902
  5179
hoelzl@38902
  5180
lemma closed_eucl_atMost[simp, intro]:
hoelzl@38902
  5181
  fixes a :: "'a\<Colon>ordered_euclidean_space"
hoelzl@38902
  5182
  shows "closed {.. a}"
hoelzl@38902
  5183
  unfolding eucl_atMost_eq_halfspaces
huffman@45104
  5184
  by (simp add: closed_INT closed_Collect_le)
hoelzl@38902
  5185
hoelzl@38902
  5186
lemma closed_eucl_atLeast[simp, intro]:
hoelzl@38902
  5187
  fixes a :: "'a\<Colon>ordered_euclidean_space"
hoelzl@38902
  5188
  shows "closed {a ..}"
hoelzl@38902
  5189
  unfolding eucl_atLeast_eq_halfspaces
huffman@45104
  5190
  by (simp add: closed_INT closed_Collect_le)
hoelzl@38902
  5191
hoelzl@39320
  5192
lemma open_vimage_euclidean_component: "open S \<Longrightarrow> open ((\<lambda>x. x $$ i) -` S)"
hoelzl@39320
  5193
  by (auto intro!: continuous_open_vimage)
hoelzl@39320
  5194
huffman@45081
  5195
text {* This gives a simple derivation of limit component bounds. *}
himmelma@33175
  5196
huffman@37673
  5197
lemma Lim_component_le: fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
hoelzl@37489
  5198
  assumes "(f ---> l) net" "\<not> (trivial_limit net)"  "eventually (\<lambda>x. f(x)$$i \<le> b) net"
hoelzl@37489
  5199
  shows "l$$i \<le> b"
himmelma@33175
  5200
proof-
hoelzl@37489
  5201
  { fix x have "x \<in> {x::'b. inner (basis i) x \<le> b} \<longleftrightarrow> x$$i \<le> b"
hoelzl@37489
  5202
      unfolding euclidean_component_def by auto  } note * = this
himmelma@33175
  5203
  show ?thesis using Lim_in_closed_set[of "{x. inner (basis i) x \<le> b}" f net l] unfolding *
hoelzl@37489
  5204
    using closed_halfspace_le[of "(basis i)::'b" b] and assms(1,2,3) by auto
hoelzl@37489
  5205
qed
hoelzl@37489
  5206
huffman@37673
  5207
lemma Lim_component_ge: fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
hoelzl@37489
  5208
  assumes "(f ---> l) net"  "\<not> (trivial_limit net)"  "eventually (\<lambda>x. b \<le> (f x)$$i) net"
hoelzl@37489
  5209
  shows "b \<le> l$$i"
himmelma@33175
  5210
proof-
hoelzl@37489
  5211
  { fix x have "x \<in> {x::'b. inner (basis i) x \<ge> b} \<longleftrightarrow> x$$i \<ge> b"
hoelzl@37489
  5212
      unfolding euclidean_component_def by auto  } note * = this
himmelma@33175
  5213
  show ?thesis using Lim_in_closed_set[of "{x. inner (basis i) x \<ge> b}" f net l] unfolding *
hoelzl@37489
  5214
    using closed_halfspace_ge[of b "(basis i)"] and assms(1,2,3) by auto
hoelzl@37489
  5215
qed
hoelzl@37489
  5216
huffman@37673
  5217
lemma Lim_component_eq: fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
hoelzl@37489
  5218
  assumes net:"(f ---> l) net" "~(trivial_limit net)" and ev:"eventually (\<lambda>x. f(x)$$i = b) net"
hoelzl@37489
  5219
  shows "l$$i = b"
huffman@45082
  5220
  using ev[unfolded order_eq_iff eventually_conj_iff] using Lim_component_ge[OF net, of b i] and Lim_component_le[OF net, of i b] by auto
himmelma@33175
  5221
text{* Limits relative to a union.                                               *}
himmelma@33175
  5222
himmelma@33175
  5223
lemma eventually_within_Un:
himmelma@33175
  5224
  "eventually P (net within (s \<union> t)) \<longleftrightarrow>
himmelma@33175
  5225
    eventually P (net within s) \<and> eventually P (net within t)"
himmelma@33175
  5226
  unfolding Limits.eventually_within
himmelma@33175
  5227
  by (auto elim!: eventually_rev_mp)
himmelma@33175
  5228
himmelma@33175
  5229
lemma Lim_within_union:
himmelma@33175
  5230
 "(f ---> l) (net within (s \<union> t)) \<longleftrightarrow>
himmelma@33175
  5231
  (f ---> l) (net within s) \<and> (f ---> l) (net within t)"
himmelma@33175
  5232
  unfolding tendsto_def
himmelma@33175
  5233
  by (auto simp add: eventually_within_Un)
himmelma@33175
  5234
huffman@36438
  5235
lemma Lim_topological:
huffman@36438
  5236
 "(f ---> l) net \<longleftrightarrow>
huffman@36438
  5237
        trivial_limit net \<or>
huffman@36438
  5238
        (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) net)"
huffman@36438
  5239
  unfolding tendsto_def trivial_limit_eq by auto
huffman@36438
  5240
himmelma@33175
  5241
lemma continuous_on_union:
himmelma@33175
  5242
  assumes "closed s" "closed t" "continuous_on s f" "continuous_on t f"
himmelma@33175
  5243
  shows "continuous_on (s \<union> t) f"
huffman@36438
  5244
  using assms unfolding continuous_on Lim_within_union
huffman@36438
  5245
  unfolding Lim_topological trivial_limit_within closed_limpt by auto
himmelma@33175
  5246
himmelma@33175
  5247
lemma continuous_on_cases:
himmelma@33175
  5248
  assumes "closed s" "closed t" "continuous_on s f" "continuous_on t g"
himmelma@33175
  5249
          "\<forall>x. (x\<in>s \<and> \<not> P x) \<or> (x \<in> t \<and> P x) \<longrightarrow> f x = g x"
himmelma@33175
  5250
  shows "continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)"
himmelma@33175
  5251
proof-
himmelma@33175
  5252
  let ?h = "(\<lambda>x. if P x then f x else g x)"
himmelma@33175
  5253
  have "\<forall>x\<in>s. f x = (if P x then f x else g x)" using assms(5) by auto
himmelma@33175
  5254
  hence "continuous_on s ?h" using continuous_on_eq[of s f ?h] using assms(3) by auto
himmelma@33175
  5255
  moreover
himmelma@33175
  5256
  have "\<forall>x\<in>t. g x = (if P x then f x else g x)" using assms(5) by auto
himmelma@33175
  5257
  hence "continuous_on t ?h" using continuous_on_eq[of t g ?h] using assms(4) by auto
himmelma@33175
  5258
  ultimately show ?thesis using continuous_on_union[OF assms(1,2), of ?h] by auto
himmelma@33175
  5259
qed
himmelma@33175
  5260
himmelma@33175
  5261
himmelma@33175
  5262
text{* Some more convenient intermediate-value theorem formulations.             *}
himmelma@33175
  5263
himmelma@33175
  5264
lemma connected_ivt_hyperplane:
himmelma@33175
  5265
  assumes "connected s" "x \<in> s" "y \<in> s" "inner a x \<le> b" "b \<le> inner a y"
himmelma@33175
  5266
  shows "\<exists>z \<in> s. inner a z = b"
himmelma@33175
  5267
proof(rule ccontr)
himmelma@33175
  5268
  assume as:"\<not> (\<exists>z\<in>s. inner a z = b)"
himmelma@33175
  5269
  let ?A = "{x. inner a x < b}"
himmelma@33175
  5270
  let ?B = "{x. inner a x > b}"
himmelma@33175
  5271
  have "open ?A" "open ?B" using open_halfspace_lt and open_halfspace_gt by auto
himmelma@33175
  5272
  moreover have "?A \<inter> ?B = {}" by auto
himmelma@33175
  5273
  moreover have "s \<subseteq> ?A \<union> ?B" using as by auto
himmelma@33175
  5274
  ultimately show False using assms(1)[unfolded connected_def not_ex, THEN spec[where x="?A"], THEN spec[where x="?B"]] and assms(2-5) by auto
himmelma@33175
  5275
qed
himmelma@33175
  5276
huffman@37673
  5277
lemma connected_ivt_component: fixes x::"'a::euclidean_space" shows
hoelzl@37489
  5278
 "connected s \<Longrightarrow> x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> x$$k \<le> a \<Longrightarrow> a \<le> y$$k \<Longrightarrow> (\<exists>z\<in>s.  z$$k = a)"
hoelzl@37489
  5279
  using connected_ivt_hyperplane[of s x y "(basis k)::'a" a]
hoelzl@37489
  5280
  unfolding euclidean_component_def by auto
himmelma@33175
  5281
huffman@45081
  5282
huffman@36433
  5283
subsection {* Homeomorphisms *}
himmelma@33175
  5284
himmelma@33175
  5285
definition "homeomorphism s t f g \<equiv>
himmelma@33175
  5286
     (\<forall>x\<in>s. (g(f x) = x)) \<and> (f ` s = t) \<and> continuous_on s f \<and>
himmelma@33175
  5287
     (\<forall>y\<in>t. (f(g y) = y)) \<and> (g ` t = s) \<and> continuous_on t g"
himmelma@33175
  5288
himmelma@33175
  5289
definition
himmelma@33175
  5290
  homeomorphic :: "'a::metric_space set \<Rightarrow> 'b::metric_space set \<Rightarrow> bool"
himmelma@33175
  5291
    (infixr "homeomorphic" 60) where
himmelma@33175
  5292
  homeomorphic_def: "s homeomorphic t \<equiv> (\<exists>f g. homeomorphism s t f g)"
himmelma@33175
  5293
himmelma@33175
  5294
lemma homeomorphic_refl: "s homeomorphic s"
himmelma@33175
  5295
  unfolding homeomorphic_def
himmelma@33175
  5296
  unfolding homeomorphism_def
himmelma@33175
  5297
  using continuous_on_id
himmelma@33175
  5298
  apply(rule_tac x = "(\<lambda>x. x)" in exI)
himmelma@33175
  5299
  apply(rule_tac x = "(\<lambda>x. x)" in exI)
himmelma@33175
  5300
  by blast
himmelma@33175
  5301
himmelma@33175
  5302
lemma homeomorphic_sym:
himmelma@33175
  5303
 "s homeomorphic t \<longleftrightarrow> t homeomorphic s"
himmelma@33175
  5304
unfolding homeomorphic_def
himmelma@33175
  5305
unfolding homeomorphism_def
paulson@33307
  5306
by blast 
himmelma@33175
  5307
himmelma@33175
  5308
lemma homeomorphic_trans:
himmelma@33175
  5309
  assumes "s homeomorphic t" "t homeomorphic u" shows "s homeomorphic u"
himmelma@33175
  5310
proof-
himmelma@33175
  5311
  obtain f1 g1 where fg1:"\<forall>x\<in>s. g1 (f1 x) = x"  "f1 ` s = t" "continuous_on s f1" "\<forall>y\<in>t. f1 (g1 y) = y" "g1 ` t = s" "continuous_on t g1"
himmelma@33175
  5312
    using assms(1) unfolding homeomorphic_def homeomorphism_def by auto
himmelma@33175
  5313
  obtain f2 g2 where fg2:"\<forall>x\<in>t. g2 (f2 x) = x"  "f2 ` t = u" "continuous_on t f2" "\<forall>y\<in>u. f2 (g2 y) = y" "g2 ` u = t" "continuous_on u g2"
himmelma@33175
  5314
    using assms(2) unfolding homeomorphic_def homeomorphism_def by auto
himmelma@33175
  5315
himmelma@33175
  5316
  { fix x assume "x\<in>s" hence "(g1 \<circ> g2) ((f2 \<circ> f1) x) = x" using fg1(1)[THEN bspec[where x=x]] and fg2(1)[THEN bspec[where x="f1 x"]] and fg1(2) by auto }
himmelma@33175
  5317
  moreover have "(f2 \<circ> f1) ` s = u" using fg1(2) fg2(2) by auto
himmelma@33175
  5318
  moreover have "continuous_on s (f2 \<circ> f1)" using continuous_on_compose[OF fg1(3)] and fg2(3) unfolding fg1(2) by auto
himmelma@33175
  5319
  moreover { fix y assume "y\<in>u" hence "(f2 \<circ> f1) ((g1 \<circ> g2) y) = y" using fg2(4)[THEN bspec[where x=y]] and fg1(4)[THEN bspec[where x="g2 y"]] and fg2(5) by auto }
himmelma@33175
  5320
  moreover have "(g1 \<circ> g2) ` u = s" using fg1(5) fg2(5) by auto
himmelma@33175
  5321
  moreover have "continuous_on u (g1 \<circ> g2)" using continuous_on_compose[OF fg2(6)] and fg1(6)  unfolding fg2(5) by auto
himmelma@33175
  5322
  ultimately show ?thesis unfolding homeomorphic_def homeomorphism_def apply(rule_tac x="f2 \<circ> f1" in exI) apply(rule_tac x="g1 \<circ> g2" in exI) by auto
himmelma@33175
  5323
qed
himmelma@33175
  5324
himmelma@33175
  5325
lemma homeomorphic_minimal:
himmelma@33175
  5326
 "s homeomorphic t \<longleftrightarrow>
himmelma@33175
  5327
    (\<exists>f g. (\<forall>x\<in>s. f(x) \<in> t \<and> (g(f(x)) = x)) \<and>
himmelma@33175
  5328
           (\<forall>y\<in>t. g(y) \<in> s \<and> (f(g(y)) = y)) \<and>
himmelma@33175
  5329
           continuous_on s f \<and> continuous_on t g)"
himmelma@33175
  5330
unfolding homeomorphic_def homeomorphism_def
himmelma@33175
  5331
apply auto apply (rule_tac x=f in exI) apply (rule_tac x=g in exI)
himmelma@33175
  5332
apply auto apply (rule_tac x=f in exI) apply (rule_tac x=g in exI) apply auto
himmelma@33175
  5333
unfolding image_iff
himmelma@33175
  5334
apply(erule_tac x="g x" in ballE) apply(erule_tac x="x" in ballE)
himmelma@33175
  5335
apply auto apply(rule_tac x="g x" in bexI) apply auto
himmelma@33175
  5336
apply(erule_tac x="f x" in ballE) apply(erule_tac x="x" in ballE)
himmelma@33175
  5337
apply auto apply(rule_tac x="f x" in bexI) by auto
himmelma@33175
  5338
huffman@36433
  5339
text {* Relatively weak hypotheses if a set is compact. *}
himmelma@33175
  5340
himmelma@33175
  5341
lemma homeomorphism_compact:
himmelma@33175
  5342
  fixes f :: "'a::heine_borel \<Rightarrow> 'b::heine_borel"
himmelma@33175
  5343
    (* class constraint due to continuous_on_inverse *)
himmelma@33175
  5344
  assumes "compact s" "continuous_on s f"  "f ` s = t"  "inj_on f s"
himmelma@33175
  5345
  shows "\<exists>g. homeomorphism s t f g"
himmelma@33175
  5346
proof-
himmelma@33175
  5347
  def g \<equiv> "\<lambda>x. SOME y. y\<in>s \<and> f y = x"
himmelma@33175
  5348
  have g:"\<forall>x\<in>s. g (f x) = x" using assms(3) assms(4)[unfolded inj_on_def] unfolding g_def by auto
himmelma@33175
  5349
  { fix y assume "y\<in>t"
himmelma@33175
  5350
    then obtain x where x:"f x = y" "x\<in>s" using assms(3) by auto
himmelma@33175
  5351
    hence "g (f x) = x" using g by auto
himmelma@33175
  5352
    hence "f (g y) = y" unfolding x(1)[THEN sym] by auto  }
himmelma@33175
  5353
  hence g':"\<forall>x\<in>t. f (g x) = x" by auto
himmelma@33175
  5354
  moreover
himmelma@33175
  5355
  { fix x
himmelma@33175
  5356
    have "x\<in>s \<Longrightarrow> x \<in> g ` t" using g[THEN bspec[where x=x]] unfolding image_iff using assms(3) by(auto intro!: bexI[where x="f x"])
himmelma@33175
  5357
    moreover
himmelma@33175
  5358
    { assume "x\<in>g ` t"
himmelma@33175
  5359
      then obtain y where y:"y\<in>t" "g y = x" by auto
himmelma@33175
  5360
      then obtain x' where x':"x'\<in>s" "f x' = y" using assms(3) by auto
himmelma@33175
  5361
      hence "x \<in> s" unfolding g_def using someI2[of "\<lambda>b. b\<in>s \<and> f b = y" x' "\<lambda>x. x\<in>s"] unfolding y(2)[THEN sym] and g_def by auto }
huffman@36358
  5362
    ultimately have "x\<in>s \<longleftrightarrow> x \<in> g ` t" ..  }
himmelma@33175
  5363
  hence "g ` t = s" by auto
himmelma@33175
  5364
  ultimately
himmelma@33175
  5365
  show ?thesis unfolding homeomorphism_def homeomorphic_def
himmelma@33175
  5366
    apply(rule_tac x=g in exI) using g and assms(3) and continuous_on_inverse[OF assms(2,1), of g, unfolded assms(3)] and assms(2) by auto
himmelma@33175
  5367
qed
himmelma@33175
  5368
himmelma@33175
  5369
lemma homeomorphic_compact:
himmelma@33175
  5370
  fixes f :: "'a::heine_borel \<Rightarrow> 'b::heine_borel"
himmelma@33175
  5371
    (* class constraint due to continuous_on_inverse *)
himmelma@33175
  5372
  shows "compact s \<Longrightarrow> continuous_on s f \<Longrightarrow> (f ` s = t) \<Longrightarrow> inj_on f s
himmelma@33175
  5373
          \<Longrightarrow> s homeomorphic t"
blanchet@37486
  5374
  unfolding homeomorphic_def by (metis homeomorphism_compact)
himmelma@33175
  5375
himmelma@33175
  5376
text{* Preservation of topological properties.                                   *}
himmelma@33175
  5377
himmelma@33175
  5378
lemma homeomorphic_compactness:
himmelma@33175
  5379
 "s homeomorphic t ==> (compact s \<longleftrightarrow> compact t)"
himmelma@33175
  5380
unfolding homeomorphic_def homeomorphism_def
himmelma@33175
  5381
by (metis compact_continuous_image)
himmelma@33175
  5382
himmelma@33175
  5383
text{* Results on translation, scaling etc.                                      *}
himmelma@33175
  5384
himmelma@33175
  5385
lemma homeomorphic_scaling:
himmelma@33175
  5386
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  5387
  assumes "c \<noteq> 0"  shows "s homeomorphic ((\<lambda>x. c *\<^sub>R x) ` s)"
himmelma@33175
  5388
  unfolding homeomorphic_minimal
himmelma@33175
  5389
  apply(rule_tac x="\<lambda>x. c *\<^sub>R x" in exI)
himmelma@33175
  5390
  apply(rule_tac x="\<lambda>x. (1 / c) *\<^sub>R x" in exI)
himmelma@33175
  5391
  using assms apply auto
himmelma@33175
  5392
  using continuous_on_cmul[OF continuous_on_id] by auto
himmelma@33175
  5393
himmelma@33175
  5394
lemma homeomorphic_translation:
himmelma@33175
  5395
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  5396
  shows "s homeomorphic ((\<lambda>x. a + x) ` s)"
himmelma@33175
  5397
  unfolding homeomorphic_minimal
himmelma@33175
  5398
  apply(rule_tac x="\<lambda>x. a + x" in exI)
himmelma@33175
  5399
  apply(rule_tac x="\<lambda>x. -a + x" in exI)
himmelma@33175
  5400
  using continuous_on_add[OF continuous_on_const continuous_on_id] by auto
himmelma@33175
  5401
himmelma@33175
  5402
lemma homeomorphic_affinity:
himmelma@33175
  5403
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  5404
  assumes "c \<noteq> 0"  shows "s homeomorphic ((\<lambda>x. a + c *\<^sub>R x) ` s)"
himmelma@33175
  5405
proof-
himmelma@33175
  5406
  have *:"op + a ` op *\<^sub>R c ` s = (\<lambda>x. a + c *\<^sub>R x) ` s" by auto
himmelma@33175
  5407
  show ?thesis
himmelma@33175
  5408
    using homeomorphic_trans
himmelma@33175
  5409
    using homeomorphic_scaling[OF assms, of s]
himmelma@33175
  5410
    using homeomorphic_translation[of "(\<lambda>x. c *\<^sub>R x) ` s" a] unfolding * by auto
himmelma@33175
  5411
qed
himmelma@33175
  5412
himmelma@33175
  5413
lemma homeomorphic_balls:
himmelma@33175
  5414
  fixes a b ::"'a::real_normed_vector" (* FIXME: generalize to metric_space *)
himmelma@33175
  5415
  assumes "0 < d"  "0 < e"
himmelma@33175
  5416
  shows "(ball a d) homeomorphic  (ball b e)" (is ?th)
himmelma@33175
  5417
        "(cball a d) homeomorphic (cball b e)" (is ?cth)
himmelma@33175
  5418
proof-
himmelma@33175
  5419
  have *:"\<bar>e / d\<bar> > 0" "\<bar>d / e\<bar> >0" using assms using divide_pos_pos by auto
himmelma@33175
  5420
  show ?th unfolding homeomorphic_minimal
himmelma@33175
  5421
    apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
himmelma@33175
  5422
    apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
himmelma@33175
  5423
    using assms apply (auto simp add: dist_commute)
himmelma@33175
  5424
    unfolding dist_norm
himmelma@33175
  5425
    apply (auto simp add: pos_divide_less_eq mult_strict_left_mono)
himmelma@33175
  5426
    unfolding continuous_on
huffman@36659
  5427
    by (intro ballI tendsto_intros, simp)+
himmelma@33175
  5428
next
himmelma@33175
  5429
  have *:"\<bar>e / d\<bar> > 0" "\<bar>d / e\<bar> >0" using assms using divide_pos_pos by auto
himmelma@33175
  5430
  show ?cth unfolding homeomorphic_minimal
himmelma@33175
  5431
    apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
himmelma@33175
  5432
    apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
himmelma@33175
  5433
    using assms apply (auto simp add: dist_commute)
himmelma@33175
  5434
    unfolding dist_norm
himmelma@33175
  5435
    apply (auto simp add: pos_divide_le_eq)
himmelma@33175
  5436
    unfolding continuous_on
huffman@36659
  5437
    by (intro ballI tendsto_intros, simp)+
himmelma@33175
  5438
qed
himmelma@33175
  5439
himmelma@33175
  5440
text{* "Isometry" (up to constant bounds) of injective linear map etc.           *}
himmelma@33175
  5441
himmelma@33175
  5442
lemma cauchy_isometric:
hoelzl@37489
  5443
  fixes x :: "nat \<Rightarrow> 'a::euclidean_space"
himmelma@33175
  5444
  assumes e:"0 < e" and s:"subspace s" and f:"bounded_linear f" and normf:"\<forall>x\<in>s. norm(f x) \<ge> e * norm(x)" and xs:"\<forall>n::nat. x n \<in> s" and cf:"Cauchy(f o x)"
himmelma@33175
  5445
  shows "Cauchy x"
himmelma@33175
  5446
proof-
himmelma@33175
  5447
  interpret f: bounded_linear f by fact
himmelma@33175
  5448
  { fix d::real assume "d>0"
himmelma@33175
  5449
    then obtain N where N:"\<forall>n\<ge>N. norm (f (x n) - f (x N)) < e * d"
himmelma@33175
  5450
      using cf[unfolded cauchy o_def dist_norm, THEN spec[where x="e*d"]] and e and mult_pos_pos[of e d] by auto
himmelma@33175
  5451
    { fix n assume "n\<ge>N"
himmelma@33175
  5452
      hence "norm (f (x n - x N)) < e * d" using N[THEN spec[where x=n]] unfolding f.diff[THEN sym] by auto
himmelma@33175
  5453
      moreover have "e * norm (x n - x N) \<le> norm (f (x n - x N))"
himmelma@33175
  5454
        using subspace_sub[OF s, of "x n" "x N"] using xs[THEN spec[where x=N]] and xs[THEN spec[where x=n]]
himmelma@33175
  5455
        using normf[THEN bspec[where x="x n - x N"]] by auto
himmelma@33175
  5456
      ultimately have "norm (x n - x N) < d" using `e>0`
himmelma@33175
  5457
        using mult_left_less_imp_less[of e "norm (x n - x N)" d] by auto   }
himmelma@33175
  5458
    hence "\<exists>N. \<forall>n\<ge>N. norm (x n - x N) < d" by auto }
himmelma@33175
  5459
  thus ?thesis unfolding cauchy and dist_norm by auto
himmelma@33175
  5460
qed
himmelma@33175
  5461
himmelma@33175
  5462
lemma complete_isometric_image:
hoelzl@37489
  5463
  fixes f :: "'a::euclidean_space => 'b::euclidean_space"
himmelma@33175
  5464
  assumes "0 < e" and s:"subspace s" and f:"bounded_linear f" and normf:"\<forall>x\<in>s. norm(f x) \<ge> e * norm(x)" and cs:"complete s"
himmelma@33175
  5465
  shows "complete(f ` s)"
himmelma@33175
  5466
proof-
himmelma@33175
  5467
  { fix g assume as:"\<forall>n::nat. g n \<in> f ` s" and cfg:"Cauchy g"
paulson@33307
  5468
    then obtain x where "\<forall>n. x n \<in> s \<and> g n = f (x n)" 
himmelma@33175
  5469
      using choice[of "\<lambda> n xa. xa \<in> s \<and> g n = f xa"] by auto
himmelma@33175
  5470
    hence x:"\<forall>n. x n \<in> s"  "\<forall>n. g n = f (x n)" by auto
nipkow@39535
  5471
    hence "f \<circ> x = g" unfolding fun_eq_iff by auto
himmelma@33175
  5472
    then obtain l where "l\<in>s" and l:"(x ---> l) sequentially"
himmelma@33175
  5473
      using cs[unfolded complete_def, THEN spec[where x="x"]]
himmelma@33175
  5474
      using cauchy_isometric[OF `0<e` s f normf] and cfg and x(1) by auto
himmelma@33175
  5475
    hence "\<exists>l\<in>f ` s. (g ---> l) sequentially"
himmelma@33175
  5476
      using linear_continuous_at[OF f, unfolded continuous_at_sequentially, THEN spec[where x=x], of l]
himmelma@33175
  5477
      unfolding `f \<circ> x = g` by auto  }
himmelma@33175
  5478
  thus ?thesis unfolding complete_def by auto
himmelma@33175
  5479
qed
himmelma@33175
  5480
himmelma@33175
  5481
lemma dist_0_norm:
himmelma@33175
  5482
  fixes x :: "'a::real_normed_vector"
himmelma@33175
  5483
  shows "dist 0 x = norm x"
himmelma@33175
  5484
unfolding dist_norm by simp
himmelma@33175
  5485
hoelzl@37489
  5486
lemma injective_imp_isometric: fixes f::"'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
himmelma@33175
  5487
  assumes s:"closed s"  "subspace s"  and f:"bounded_linear f" "\<forall>x\<in>s. (f x = 0) \<longrightarrow> (x = 0)"
himmelma@33175
  5488
  shows "\<exists>e>0. \<forall>x\<in>s. norm (f x) \<ge> e * norm(x)"
hoelzl@37489
  5489
proof(cases "s \<subseteq> {0::'a}")
himmelma@33175
  5490
  case True
himmelma@33175
  5491
  { fix x assume "x \<in> s"
himmelma@33175
  5492
    hence "x = 0" using True by auto
himmelma@33175
  5493
    hence "norm x \<le> norm (f x)" by auto  }
himmelma@33175
  5494
  thus ?thesis by(auto intro!: exI[where x=1])
himmelma@33175
  5495
next
himmelma@33175
  5496
  interpret f: bounded_linear f by fact
himmelma@33175
  5497
  case False
himmelma@33175
  5498
  then obtain a where a:"a\<noteq>0" "a\<in>s" by auto
himmelma@33175
  5499
  from False have "s \<noteq> {}" by auto
himmelma@33175
  5500
  let ?S = "{f x| x. (x \<in> s \<and> norm x = norm a)}"
hoelzl@37489
  5501
  let ?S' = "{x::'a. x\<in>s \<and> norm x = norm a}"
hoelzl@37489
  5502
  let ?S'' = "{x::'a. norm x = norm a}"
himmelma@33175
  5503
huffman@36358
  5504
  have "?S'' = frontier(cball 0 (norm a))" unfolding frontier_cball and dist_norm by auto
himmelma@33175
  5505
  hence "compact ?S''" using compact_frontier[OF compact_cball, of 0 "norm a"] by auto
himmelma@33175
  5506
  moreover have "?S' = s \<inter> ?S''" by auto
himmelma@33175
  5507
  ultimately have "compact ?S'" using closed_inter_compact[of s ?S''] using s(1) by auto
himmelma@33175
  5508
  moreover have *:"f ` ?S' = ?S" by auto
himmelma@33175
  5509
  ultimately have "compact ?S" using compact_continuous_image[OF linear_continuous_on[OF f(1)], of ?S'] by auto
himmelma@33175
  5510
  hence "closed ?S" using compact_imp_closed by auto
himmelma@33175
  5511
  moreover have "?S \<noteq> {}" using a by auto
himmelma@33175
  5512
  ultimately obtain b' where "b'\<in>?S" "\<forall>y\<in>?S. norm b' \<le> norm y" using distance_attains_inf[of ?S 0] unfolding dist_0_norm by auto
himmelma@33175
  5513
  then obtain b where "b\<in>s" and ba:"norm b = norm a" and b:"\<forall>x\<in>{x \<in> s. norm x = norm a}. norm (f b) \<le> norm (f x)" unfolding *[THEN sym] unfolding image_iff by auto
himmelma@33175
  5514
himmelma@33175
  5515
  let ?e = "norm (f b) / norm b"
himmelma@33175
  5516
  have "norm b > 0" using ba and a and norm_ge_zero by auto
himmelma@33175
  5517
  moreover have "norm (f b) > 0" using f(2)[THEN bspec[where x=b], OF `b\<in>s`] using `norm b >0` unfolding zero_less_norm_iff by auto
himmelma@33175
  5518
  ultimately have "0 < norm (f b) / norm b" by(simp only: divide_pos_pos)
himmelma@33175
  5519
  moreover
himmelma@33175
  5520
  { fix x assume "x\<in>s"
himmelma@33175
  5521
    hence "norm (f b) / norm b * norm x \<le> norm (f x)"
himmelma@33175
  5522
    proof(cases "x=0")
himmelma@33175
  5523
      case True thus "norm (f b) / norm b * norm x \<le> norm (f x)" by auto
himmelma@33175
  5524
    next
himmelma@33175
  5525
      case False
himmelma@33175
  5526
      hence *:"0 < norm a / norm x" using `a\<noteq>0` unfolding zero_less_norm_iff[THEN sym] by(simp only: divide_pos_pos)
hoelzl@37489
  5527
      have "\<forall>c. \<forall>x\<in>s. c *\<^sub>R x \<in> s" using s[unfolded subspace_def] by auto
himmelma@33175
  5528
      hence "(norm a / norm x) *\<^sub>R x \<in> {x \<in> s. norm x = norm a}" using `x\<in>s` and `x\<noteq>0` by auto
himmelma@33175
  5529
      thus "norm (f b) / norm b * norm x \<le> norm (f x)" using b[THEN bspec[where x="(norm a / norm x) *\<^sub>R x"]]
himmelma@33175
  5530
        unfolding f.scaleR and ba using `x\<noteq>0` `a\<noteq>0`
huffman@36770
  5531
        by (auto simp add: mult_commute pos_le_divide_eq pos_divide_le_eq)
himmelma@33175
  5532
    qed }
himmelma@33175
  5533
  ultimately
himmelma@33175
  5534
  show ?thesis by auto
himmelma@33175
  5535
qed
himmelma@33175
  5536
himmelma@33175
  5537
lemma closed_injective_image_subspace:
hoelzl@37489
  5538
  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
himmelma@33175
  5539
  assumes "subspace s" "bounded_linear f" "\<forall>x\<in>s. f x = 0 --> x = 0" "closed s"
himmelma@33175
  5540
  shows "closed(f ` s)"
himmelma@33175
  5541
proof-
himmelma@33175
  5542
  obtain e where "e>0" and e:"\<forall>x\<in>s. e * norm x \<le> norm (f x)" using injective_imp_isometric[OF assms(4,1,2,3)] by auto
himmelma@33175
  5543
  show ?thesis using complete_isometric_image[OF `e>0` assms(1,2) e] and assms(4)
himmelma@33175
  5544
    unfolding complete_eq_closed[THEN sym] by auto
himmelma@33175
  5545
qed
himmelma@33175
  5546
huffman@45081
  5547
huffman@45081
  5548
subsection {* Some properties of a canonical subspace *}
himmelma@33175
  5549
himmelma@33175
  5550
lemma subspace_substandard:
hoelzl@37489
  5551
  "subspace {x::'a::euclidean_space. (\<forall>i<DIM('a). P i \<longrightarrow> x$$i = 0)}"
huffman@45314
  5552
  unfolding subspace_def by auto
himmelma@33175
  5553
himmelma@33175
  5554
lemma closed_substandard:
hoelzl@37489
  5555
 "closed {x::'a::euclidean_space. \<forall>i<DIM('a). P i --> x$$i = 0}" (is "closed ?A")
himmelma@33175
  5556
proof-
hoelzl@37489
  5557
  let ?D = "{i. P i} \<inter> {..<DIM('a)}"
huffman@45314
  5558
  have "closed (\<Inter>i\<in>?D. {x::'a. x$$i = 0})"
huffman@45314
  5559
    by (simp add: closed_INT closed_Collect_eq)
huffman@45314
  5560
  also have "(\<Inter>i\<in>?D. {x::'a. x$$i = 0}) = ?A"
huffman@45314
  5561
    by auto
huffman@45314
  5562
  finally show "closed ?A" .
himmelma@33175
  5563
qed
himmelma@33175
  5564
hoelzl@37489
  5565
lemma dim_substandard: assumes "d\<subseteq>{..<DIM('a::euclidean_space)}"
hoelzl@37489
  5566
  shows "dim {x::'a::euclidean_space. \<forall>i<DIM('a). i \<notin> d \<longrightarrow> x$$i = 0} = card d" (is "dim ?A = _")
himmelma@33175
  5567
proof-
hoelzl@37489
  5568
  let ?D = "{..<DIM('a)}"
hoelzl@37489
  5569
  let ?B = "(basis::nat => 'a) ` d"
hoelzl@37489
  5570
  let ?bas = "basis::nat \<Rightarrow> 'a"
huffman@45032
  5571
  have "?B \<subseteq> ?A" by auto
himmelma@33175
  5572
  moreover
hoelzl@37489
  5573
  { fix x::"'a" assume "x\<in>?A"
hoelzl@37489
  5574
    hence "finite d" "x\<in>?A" using assms by(auto intro:finite_subset)
hoelzl@37489
  5575
    hence "x\<in> span ?B"
himmelma@33175
  5576
    proof(induct d arbitrary: x)
hoelzl@37489
  5577
      case empty hence "x=0" apply(subst euclidean_eq) by auto
himmelma@33175
  5578
      thus ?case using subspace_0[OF subspace_span[of "{}"]] by auto
himmelma@33175
  5579
    next
himmelma@33175
  5580
      case (insert k F)
hoelzl@37489
  5581
      hence *:"\<forall>i<DIM('a). i \<notin> insert k F \<longrightarrow> x $$ i = 0" by auto
himmelma@33175
  5582
      have **:"F \<subseteq> insert k F" by auto
hoelzl@37489
  5583
      def y \<equiv> "x - x$$k *\<^sub>R basis k"
hoelzl@37489
  5584
      have y:"x = y + (x$$k) *\<^sub>R basis k" unfolding y_def by auto
himmelma@33175
  5585
      { fix i assume i':"i \<notin> F"
hoelzl@37489
  5586
        hence "y $$ i = 0" unfolding y_def 
huffman@45314
  5587
          using *[THEN spec[where x=i]] by auto }
hoelzl@37489
  5588
      hence "y \<in> span (basis ` F)" using insert(3) by auto
hoelzl@37489
  5589
      hence "y \<in> span (basis ` (insert k F))"
himmelma@33175
  5590
        using span_mono[of "?bas ` F" "?bas ` (insert k F)"]
hoelzl@37489
  5591
        using image_mono[OF **, of basis] using assms by auto
himmelma@33175
  5592
      moreover
himmelma@33175
  5593
      have "basis k \<in> span (?bas ` (insert k F))" by(rule span_superset, auto)
hoelzl@37489
  5594
      hence "x$$k *\<^sub>R basis k \<in> span (?bas ` (insert k F))"
huffman@36593
  5595
        using span_mul by auto
himmelma@33175
  5596
      ultimately
hoelzl@37489
  5597
      have "y + x$$k *\<^sub>R basis k \<in> span (?bas ` (insert k F))"
himmelma@33175
  5598
        using span_add by auto
himmelma@33175
  5599
      thus ?case using y by auto
himmelma@33175
  5600
    qed
himmelma@33175
  5601
  }
himmelma@33175
  5602
  hence "?A \<subseteq> span ?B" by auto
himmelma@33175
  5603
  moreover
himmelma@33175
  5604
  { fix x assume "x \<in> ?B"
hoelzl@37489
  5605
    hence "x\<in>{(basis i)::'a |i. i \<in> ?D}" using assms by auto  }
hoelzl@37489
  5606
  hence "independent ?B" using independent_mono[OF independent_basis, of ?B] and assms by auto
himmelma@33175
  5607
  moreover
himmelma@33175
  5608
  have "d \<subseteq> ?D" unfolding subset_eq using assms by auto
hoelzl@37489
  5609
  hence *:"inj_on (basis::nat\<Rightarrow>'a) d" using subset_inj_on[OF basis_inj, of "d"] by auto
hoelzl@33712
  5610
  have "card ?B = card d" unfolding card_image[OF *] by auto
himmelma@33175
  5611
  ultimately show ?thesis using dim_unique[of "basis ` d" ?A] by auto
himmelma@33175
  5612
qed
himmelma@33175
  5613
himmelma@33175
  5614
text{* Hence closure and completeness of all subspaces.                          *}
himmelma@33175
  5615
himmelma@33175
  5616
lemma closed_subspace_lemma: "n \<le> card (UNIV::'n::finite set) \<Longrightarrow> \<exists>A::'n set. card A = n"
himmelma@33175
  5617
apply (induct n)
himmelma@33175
  5618
apply (rule_tac x="{}" in exI, simp)
himmelma@33175
  5619
apply clarsimp
himmelma@33175
  5620
apply (subgoal_tac "\<exists>x. x \<notin> A")
himmelma@33175
  5621
apply (erule exE)
himmelma@33175
  5622
apply (rule_tac x="insert x A" in exI, simp)
himmelma@33175
  5623
apply (subgoal_tac "A \<noteq> UNIV", auto)
himmelma@33175
  5624
done
himmelma@33175
  5625
hoelzl@37489
  5626
lemma closed_subspace: fixes s::"('a::euclidean_space) set"
himmelma@33175
  5627
  assumes "subspace s" shows "closed s"
himmelma@33175
  5628
proof-
hoelzl@37489
  5629
  have *:"dim s \<le> DIM('a)" using dim_subset_UNIV by auto
hoelzl@37489
  5630
  def d \<equiv> "{..<dim s}" have t:"card d = dim s" unfolding d_def by auto
hoelzl@37489
  5631
  let ?t = "{x::'a. \<forall>i<DIM('a). i \<notin> d \<longrightarrow> x$$i = 0}"
hoelzl@37489
  5632
  have "\<exists>f. linear f \<and> f ` {x::'a. \<forall>i<DIM('a). i \<notin> d \<longrightarrow> x $$ i = 0} = s \<and>
hoelzl@37489
  5633
      inj_on f {x::'a. \<forall>i<DIM('a). i \<notin> d \<longrightarrow> x $$ i = 0}"
hoelzl@37489
  5634
    apply(rule subspace_isomorphism[OF subspace_substandard[of "\<lambda>i. i \<notin> d"]])
hoelzl@37489
  5635
    using dim_substandard[of d,where 'a='a] and t unfolding d_def using * assms by auto
hoelzl@37489
  5636
  then guess f apply-by(erule exE conjE)+ note f = this
hoelzl@37489
  5637
  interpret f: bounded_linear f using f unfolding linear_conv_bounded_linear by auto
himmelma@33175
  5638
  have "\<forall>x\<in>?t. f x = 0 \<longrightarrow> x = 0" using f.zero using f(3)[unfolded inj_on_def]
himmelma@33175
  5639
    by(erule_tac x=0 in ballE) auto
himmelma@33175
  5640
  moreover have "closed ?t" using closed_substandard .
himmelma@33175
  5641
  moreover have "subspace ?t" using subspace_substandard .
himmelma@33175
  5642
  ultimately show ?thesis using closed_injective_image_subspace[of ?t f]
hoelzl@37489
  5643
    unfolding f(2) using f(1) unfolding linear_conv_bounded_linear by auto
himmelma@33175
  5644
qed
himmelma@33175
  5645
himmelma@33175
  5646
lemma complete_subspace:
hoelzl@37489
  5647
  fixes s :: "('a::euclidean_space) set" shows "subspace s ==> complete s"
himmelma@33175
  5648
  using complete_eq_closed closed_subspace
himmelma@33175
  5649
  by auto
himmelma@33175
  5650
himmelma@33175
  5651
lemma dim_closure:
hoelzl@37489
  5652
  fixes s :: "('a::euclidean_space) set"
himmelma@33175
  5653
  shows "dim(closure s) = dim s" (is "?dc = ?d")
himmelma@33175
  5654
proof-
himmelma@33175
  5655
  have "?dc \<le> ?d" using closure_minimal[OF span_inc, of s]
himmelma@33175
  5656
    using closed_subspace[OF subspace_span, of s]
himmelma@33175
  5657
    using dim_subset[of "closure s" "span s"] unfolding dim_span by auto
himmelma@33175
  5658
  thus ?thesis using dim_subset[OF closure_subset, of s] by auto
himmelma@33175
  5659
qed
himmelma@33175
  5660
huffman@45081
  5661
huffman@36433
  5662
subsection {* Affine transformations of intervals *}
himmelma@33175
  5663
himmelma@33175
  5664
lemma real_affinity_le:
haftmann@35028
  5665
 "0 < (m::'a::linordered_field) ==> (m * x + c \<le> y \<longleftrightarrow> x \<le> inverse(m) * y + -(c / m))"
himmelma@33175
  5666
  by (simp add: field_simps inverse_eq_divide)
himmelma@33175
  5667
himmelma@33175
  5668
lemma real_le_affinity:
haftmann@35028
  5669
 "0 < (m::'a::linordered_field) ==> (y \<le> m * x + c \<longleftrightarrow> inverse(m) * y + -(c / m) \<le> x)"
himmelma@33175
  5670
  by (simp add: field_simps inverse_eq_divide)
himmelma@33175
  5671
himmelma@33175
  5672
lemma real_affinity_lt:
haftmann@35028
  5673
 "0 < (m::'a::linordered_field) ==> (m * x + c < y \<longleftrightarrow> x < inverse(m) * y + -(c / m))"
himmelma@33175
  5674
  by (simp add: field_simps inverse_eq_divide)
himmelma@33175
  5675
himmelma@33175
  5676
lemma real_lt_affinity:
haftmann@35028
  5677
 "0 < (m::'a::linordered_field) ==> (y < m * x + c \<longleftrightarrow> inverse(m) * y + -(c / m) < x)"
himmelma@33175
  5678
  by (simp add: field_simps inverse_eq_divide)
himmelma@33175
  5679
himmelma@33175
  5680
lemma real_affinity_eq:
haftmann@35028
  5681
 "(m::'a::linordered_field) \<noteq> 0 ==> (m * x + c = y \<longleftrightarrow> x = inverse(m) * y + -(c / m))"
himmelma@33175
  5682
  by (simp add: field_simps inverse_eq_divide)
himmelma@33175
  5683
himmelma@33175
  5684
lemma real_eq_affinity:
haftmann@35028
  5685
 "(m::'a::linordered_field) \<noteq> 0 ==> (y = m * x + c  \<longleftrightarrow> inverse(m) * y + -(c / m) = x)"
himmelma@33175
  5686
  by (simp add: field_simps inverse_eq_divide)
himmelma@33175
  5687
himmelma@33175
  5688
lemma image_affinity_interval: fixes m::real
hoelzl@37489
  5689
  fixes a b c :: "'a::ordered_euclidean_space"
himmelma@33175
  5690
  shows "(\<lambda>x. m *\<^sub>R x + c) ` {a .. b} =
himmelma@33175
  5691
            (if {a .. b} = {} then {}
himmelma@33175
  5692
            else (if 0 \<le> m then {m *\<^sub>R a + c .. m *\<^sub>R b + c}
himmelma@33175
  5693
            else {m *\<^sub>R b + c .. m *\<^sub>R a + c}))"
hoelzl@37489
  5694
proof(cases "m=0")  
himmelma@33175
  5695
  { fix x assume "x \<le> c" "c \<le> x"
hoelzl@37489
  5696
    hence "x=c" unfolding eucl_le[where 'a='a] apply-
hoelzl@37489
  5697
      apply(subst euclidean_eq) by (auto intro: order_antisym) }
himmelma@33175
  5698
  moreover case True
hoelzl@37489
  5699
  moreover have "c \<in> {m *\<^sub>R a + c..m *\<^sub>R b + c}" unfolding True by(auto simp add: eucl_le[where 'a='a])
himmelma@33175
  5700
  ultimately show ?thesis by auto
himmelma@33175
  5701
next
himmelma@33175
  5702
  case False
himmelma@33175
  5703
  { fix y assume "a \<le> y" "y \<le> b" "m > 0"
himmelma@33175
  5704
    hence "m *\<^sub>R a + c \<le> m *\<^sub>R y + c"  "m *\<^sub>R y + c \<le> m *\<^sub>R b + c"
huffman@45314
  5705
      unfolding eucl_le[where 'a='a] by auto
himmelma@33175
  5706
  } moreover
himmelma@33175
  5707
  { fix y assume "a \<le> y" "y \<le> b" "m < 0"
himmelma@33175
  5708
    hence "m *\<^sub>R b + c \<le> m *\<^sub>R y + c"  "m *\<^sub>R y + c \<le> m *\<^sub>R a + c"
huffman@45314
  5709
      unfolding eucl_le[where 'a='a] by(auto simp add: mult_left_mono_neg)
himmelma@33175
  5710
  } moreover
himmelma@33175
  5711
  { fix y assume "m > 0"  "m *\<^sub>R a + c \<le> y"  "y \<le> m *\<^sub>R b + c"
himmelma@33175
  5712
    hence "y \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}"
hoelzl@37489
  5713
      unfolding image_iff Bex_def mem_interval eucl_le[where 'a='a]
huffman@45371
  5714
      apply (intro exI[where x="(1 / m) *\<^sub>R (y - c)"])
huffman@45314
  5715
      by(auto simp add: pos_le_divide_eq pos_divide_le_eq mult_commute diff_le_iff)
himmelma@33175
  5716
  } moreover
himmelma@33175
  5717
  { fix y assume "m *\<^sub>R b + c \<le> y" "y \<le> m *\<^sub>R a + c" "m < 0"
himmelma@33175
  5718
    hence "y \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}"
hoelzl@37489
  5719
      unfolding image_iff Bex_def mem_interval eucl_le[where 'a='a]
huffman@45371
  5720
      apply (intro exI[where x="(1 / m) *\<^sub>R (y - c)"])
huffman@45314
  5721
      by(auto simp add: neg_le_divide_eq neg_divide_le_eq mult_commute diff_le_iff)
himmelma@33175
  5722
  }
himmelma@33175
  5723
  ultimately show ?thesis using False by auto
himmelma@33175
  5724
qed
himmelma@33175
  5725
hoelzl@37489
  5726
lemma image_smult_interval:"(\<lambda>x. m *\<^sub>R (x::_::ordered_euclidean_space)) ` {a..b} =
himmelma@33175
  5727
  (if {a..b} = {} then {} else if 0 \<le> m then {m *\<^sub>R a..m *\<^sub>R b} else {m *\<^sub>R b..m *\<^sub>R a})"
himmelma@33175
  5728
  using image_affinity_interval[of m 0 a b] by auto
himmelma@33175
  5729
huffman@45081
  5730
huffman@45081
  5731
subsection {* Banach fixed point theorem (not really topological...) *}
himmelma@33175
  5732
himmelma@33175
  5733
lemma banach_fix:
himmelma@33175
  5734
  assumes s:"complete s" "s \<noteq> {}" and c:"0 \<le> c" "c < 1" and f:"(f ` s) \<subseteq> s" and
himmelma@33175
  5735
          lipschitz:"\<forall>x\<in>s. \<forall>y\<in>s. dist (f x) (f y) \<le> c * dist x y"
himmelma@33175
  5736
  shows "\<exists>! x\<in>s. (f x = x)"
himmelma@33175
  5737
proof-
himmelma@33175
  5738
  have "1 - c > 0" using c by auto
himmelma@33175
  5739
himmelma@33175
  5740
  from s(2) obtain z0 where "z0 \<in> s" by auto
himmelma@33175
  5741
  def z \<equiv> "\<lambda>n. (f ^^ n) z0"
himmelma@33175
  5742
  { fix n::nat
himmelma@33175
  5743
    have "z n \<in> s" unfolding z_def
himmelma@33175
  5744
    proof(induct n) case 0 thus ?case using `z0 \<in>s` by auto
himmelma@33175
  5745
    next case Suc thus ?case using f by auto qed }
himmelma@33175
  5746
  note z_in_s = this
himmelma@33175
  5747
himmelma@33175
  5748
  def d \<equiv> "dist (z 0) (z 1)"
himmelma@33175
  5749
himmelma@33175
  5750
  have fzn:"\<And>n. f (z n) = z (Suc n)" unfolding z_def by auto
himmelma@33175
  5751
  { fix n::nat
himmelma@33175
  5752
    have "dist (z n) (z (Suc n)) \<le> (c ^ n) * d"
himmelma@33175
  5753
    proof(induct n)
himmelma@33175
  5754
      case 0 thus ?case unfolding d_def by auto
himmelma@33175
  5755
    next
himmelma@33175
  5756
      case (Suc m)
himmelma@33175
  5757
      hence "c * dist (z m) (z (Suc m)) \<le> c ^ Suc m * d"
haftmann@38880
  5758
        using `0 \<le> c` using mult_left_mono[of "dist (z m) (z (Suc m))" "c ^ m * d" c] by auto
himmelma@33175
  5759
      thus ?case using lipschitz[THEN bspec[where x="z m"], OF z_in_s, THEN bspec[where x="z (Suc m)"], OF z_in_s]
himmelma@33175
  5760
        unfolding fzn and mult_le_cancel_left by auto
himmelma@33175
  5761
    qed
himmelma@33175
  5762
  } note cf_z = this
himmelma@33175
  5763
himmelma@33175
  5764
  { fix n m::nat
himmelma@33175
  5765
    have "(1 - c) * dist (z m) (z (m+n)) \<le> (c ^ m) * d * (1 - c ^ n)"
himmelma@33175
  5766
    proof(induct n)
himmelma@33175
  5767
      case 0 show ?case by auto
himmelma@33175
  5768
    next
himmelma@33175
  5769
      case (Suc k)
himmelma@33175
  5770
      have "(1 - c) * dist (z m) (z (m + Suc k)) \<le> (1 - c) * (dist (z m) (z (m + k)) + dist (z (m + k)) (z (Suc (m + k))))"
himmelma@33175
  5771
        using dist_triangle and c by(auto simp add: dist_triangle)
himmelma@33175
  5772
      also have "\<dots> \<le> (1 - c) * (dist (z m) (z (m + k)) + c ^ (m + k) * d)"
himmelma@33175
  5773
        using cf_z[of "m + k"] and c by auto
himmelma@33175
  5774
      also have "\<dots> \<le> c ^ m * d * (1 - c ^ k) + (1 - c) * c ^ (m + k) * d"
haftmann@36349
  5775
        using Suc by (auto simp add: field_simps)
himmelma@33175
  5776
      also have "\<dots> = (c ^ m) * (d * (1 - c ^ k) + (1 - c) * c ^ k * d)"
haftmann@36349
  5777
        unfolding power_add by (auto simp add: field_simps)
himmelma@33175
  5778
      also have "\<dots> \<le> (c ^ m) * d * (1 - c ^ Suc k)"
haftmann@36349
  5779
        using c by (auto simp add: field_simps)
himmelma@33175
  5780
      finally show ?case by auto
himmelma@33175
  5781
    qed
himmelma@33175
  5782
  } note cf_z2 = this
himmelma@33175
  5783
  { fix e::real assume "e>0"
himmelma@33175
  5784
    hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (z m) (z n) < e"
himmelma@33175
  5785
    proof(cases "d = 0")
himmelma@33175
  5786
      case True
boehmes@42734
  5787
      have *: "\<And>x. ((1 - c) * x \<le> 0) = (x \<le> 0)" using `1 - c > 0`
boehmes@42734
  5788
        by (metis mult_zero_left real_mult_commute real_mult_le_cancel_iff1)
boehmes@42734
  5789
      from True have "\<And>n. z n = z0" using cf_z2[of 0] and c unfolding z_def
boehmes@42734
  5790
        by (simp add: *)
himmelma@33175
  5791
      thus ?thesis using `e>0` by auto
himmelma@33175
  5792
    next
himmelma@33175
  5793
      case False hence "d>0" unfolding d_def using zero_le_dist[of "z 0" "z 1"]
huffman@36770
  5794
        by (metis False d_def less_le)
himmelma@33175
  5795
      hence "0 < e * (1 - c) / d" using `e>0` and `1-c>0`
himmelma@33175
  5796
        using divide_pos_pos[of "e * (1 - c)" d] and mult_pos_pos[of e "1 - c"] by auto
himmelma@33175
  5797
      then obtain N where N:"c ^ N < e * (1 - c) / d" using real_arch_pow_inv[of "e * (1 - c) / d" c] and c by auto
himmelma@33175
  5798
      { fix m n::nat assume "m>n" and as:"m\<ge>N" "n\<ge>N"
himmelma@33175
  5799
        have *:"c ^ n \<le> c ^ N" using `n\<ge>N` and c using power_decreasing[OF `n\<ge>N`, of c] by auto
himmelma@33175
  5800
        have "1 - c ^ (m - n) > 0" using c and power_strict_mono[of c 1 "m - n"] using `m>n` by auto
himmelma@33175
  5801
        hence **:"d * (1 - c ^ (m - n)) / (1 - c) > 0"
huffman@36770
  5802
          using mult_pos_pos[OF `d>0`, of "1 - c ^ (m - n)"]
himmelma@33175
  5803
          using divide_pos_pos[of "d * (1 - c ^ (m - n))" "1 - c"]
himmelma@33175
  5804
          using `0 < 1 - c` by auto
himmelma@33175
  5805
himmelma@33175
  5806
        have "dist (z m) (z n) \<le> c ^ n * d * (1 - c ^ (m - n)) / (1 - c)"
himmelma@33175
  5807
          using cf_z2[of n "m - n"] and `m>n` unfolding pos_le_divide_eq[OF `1-c>0`]
huffman@36770
  5808
          by (auto simp add: mult_commute dist_commute)
himmelma@33175
  5809
        also have "\<dots> \<le> c ^ N * d * (1 - c ^ (m - n)) / (1 - c)"
himmelma@33175
  5810
          using mult_right_mono[OF * order_less_imp_le[OF **]]
huffman@36770
  5811
          unfolding mult_assoc by auto
himmelma@33175
  5812
        also have "\<dots> < (e * (1 - c) / d) * d * (1 - c ^ (m - n)) / (1 - c)"
huffman@36770
  5813
          using mult_strict_right_mono[OF N **] unfolding mult_assoc by auto
himmelma@33175
  5814
        also have "\<dots> = e * (1 - c ^ (m - n))" using c and `d>0` and `1 - c > 0` by auto
himmelma@33175
  5815
        also have "\<dots> \<le> e" using c and `1 - c ^ (m - n) > 0` and `e>0` using mult_right_le_one_le[of e "1 - c ^ (m - n)"] by auto
himmelma@33175
  5816
        finally have  "dist (z m) (z n) < e" by auto
himmelma@33175
  5817
      } note * = this
himmelma@33175
  5818
      { fix m n::nat assume as:"N\<le>m" "N\<le>n"
himmelma@33175
  5819
        hence "dist (z n) (z m) < e"
himmelma@33175
  5820
        proof(cases "n = m")
himmelma@33175
  5821
          case True thus ?thesis using `e>0` by auto
himmelma@33175
  5822
        next
himmelma@33175
  5823
          case False thus ?thesis using as and *[of n m] *[of m n] unfolding nat_neq_iff by (auto simp add: dist_commute)
himmelma@33175
  5824
        qed }
himmelma@33175
  5825
      thus ?thesis by auto
himmelma@33175
  5826
    qed
himmelma@33175
  5827
  }
himmelma@33175
  5828
  hence "Cauchy z" unfolding cauchy_def by auto
himmelma@33175
  5829
  then obtain x where "x\<in>s" and x:"(z ---> x) sequentially" using s(1)[unfolded compact_def complete_def, THEN spec[where x=z]] and z_in_s by auto
himmelma@33175
  5830
himmelma@33175
  5831
  def e \<equiv> "dist (f x) x"
himmelma@33175
  5832
  have "e = 0" proof(rule ccontr)
himmelma@33175
  5833
    assume "e \<noteq> 0" hence "e>0" unfolding e_def using zero_le_dist[of "f x" x]
himmelma@33175
  5834
      by (metis dist_eq_0_iff dist_nz e_def)
himmelma@33175
  5835
    then obtain N where N:"\<forall>n\<ge>N. dist (z n) x < e / 2"
himmelma@33175
  5836
      using x[unfolded Lim_sequentially, THEN spec[where x="e/2"]] by auto
himmelma@33175
  5837
    hence N':"dist (z N) x < e / 2" by auto
himmelma@33175
  5838
himmelma@33175
  5839
    have *:"c * dist (z N) x \<le> dist (z N) x" unfolding mult_le_cancel_right2
himmelma@33175
  5840
      using zero_le_dist[of "z N" x] and c
huffman@36770
  5841
      by (metis dist_eq_0_iff dist_nz order_less_asym less_le)
himmelma@33175
  5842
    have "dist (f (z N)) (f x) \<le> c * dist (z N) x" using lipschitz[THEN bspec[where x="z N"], THEN bspec[where x=x]]
himmelma@33175
  5843
      using z_in_s[of N] `x\<in>s` using c by auto
himmelma@33175
  5844
    also have "\<dots> < e / 2" using N' and c using * by auto
himmelma@33175
  5845
    finally show False unfolding fzn
himmelma@33175
  5846
      using N[THEN spec[where x="Suc N"]] and dist_triangle_half_r[of "z (Suc N)" "f x" e x]
himmelma@33175
  5847
      unfolding e_def by auto
himmelma@33175
  5848
  qed
himmelma@33175
  5849
  hence "f x = x" unfolding e_def by auto
himmelma@33175
  5850
  moreover
himmelma@33175
  5851
  { fix y assume "f y = y" "y\<in>s"
himmelma@33175
  5852
    hence "dist x y \<le> c * dist x y" using lipschitz[THEN bspec[where x=x], THEN bspec[where x=y]]
himmelma@33175
  5853
      using `x\<in>s` and `f x = x` by auto
himmelma@33175
  5854
    hence "dist x y = 0" unfolding mult_le_cancel_right1
himmelma@33175
  5855
      using c and zero_le_dist[of x y] by auto
himmelma@33175
  5856
    hence "y = x" by auto
himmelma@33175
  5857
  }
hoelzl@34986
  5858
  ultimately show ?thesis using `x\<in>s` by blast+
himmelma@33175
  5859
qed
himmelma@33175
  5860
huffman@45081
  5861
subsection {* Edelstein fixed point theorem *}
himmelma@33175
  5862
himmelma@33175
  5863
lemma edelstein_fix:
himmelma@33175
  5864
  fixes s :: "'a::real_normed_vector set"
himmelma@33175
  5865
  assumes s:"compact s" "s \<noteq> {}" and gs:"(g ` s) \<subseteq> s"
himmelma@33175
  5866
      and dist:"\<forall>x\<in>s. \<forall>y\<in>s. x \<noteq> y \<longrightarrow> dist (g x) (g y) < dist x y"
himmelma@33175
  5867
  shows "\<exists>! x\<in>s. g x = x"
himmelma@33175
  5868
proof(cases "\<exists>x\<in>s. g x \<noteq> x")
himmelma@33175
  5869
  obtain x where "x\<in>s" using s(2) by auto
himmelma@33175
  5870
  case False hence g:"\<forall>x\<in>s. g x = x" by auto
himmelma@33175
  5871
  { fix y assume "y\<in>s"
himmelma@33175
  5872
    hence "x = y" using `x\<in>s` and dist[THEN bspec[where x=x], THEN bspec[where x=y]]
himmelma@33175
  5873
      unfolding g[THEN bspec[where x=x], OF `x\<in>s`]
himmelma@33175
  5874
      unfolding g[THEN bspec[where x=y], OF `y\<in>s`] by auto  }
hoelzl@34986
  5875
  thus ?thesis using `x\<in>s` and g by blast+
himmelma@33175
  5876
next
himmelma@33175
  5877
  case True
himmelma@33175
  5878
  then obtain x where [simp]:"x\<in>s" and "g x \<noteq> x" by auto
himmelma@33175
  5879
  { fix x y assume "x \<in> s" "y \<in> s"
himmelma@33175
  5880
    hence "dist (g x) (g y) \<le> dist x y"
himmelma@33175
  5881
      using dist[THEN bspec[where x=x], THEN bspec[where x=y]] by auto } note dist' = this
himmelma@33175
  5882
  def y \<equiv> "g x"
himmelma@33175
  5883
  have [simp]:"y\<in>s" unfolding y_def using gs[unfolded image_subset_iff] and `x\<in>s` by blast
himmelma@33175
  5884
  def f \<equiv> "\<lambda>n. g ^^ n"
himmelma@33175
  5885
  have [simp]:"\<And>n z. g (f n z) = f (Suc n) z" unfolding f_def by auto
himmelma@33175
  5886
  have [simp]:"\<And>z. f 0 z = z" unfolding f_def by auto
himmelma@33175
  5887
  { fix n::nat and z assume "z\<in>s"
himmelma@33175
  5888
    have "f n z \<in> s" unfolding f_def
himmelma@33175
  5889
    proof(induct n)
himmelma@33175
  5890
      case 0 thus ?case using `z\<in>s` by simp
himmelma@33175
  5891
    next
himmelma@33175
  5892
      case (Suc n) thus ?case using gs[unfolded image_subset_iff] by auto
himmelma@33175
  5893
    qed } note fs = this
himmelma@33175
  5894
  { fix m n ::nat assume "m\<le>n"
himmelma@33175
  5895
    fix w z assume "w\<in>s" "z\<in>s"
himmelma@33175
  5896
    have "dist (f n w) (f n z) \<le> dist (f m w) (f m z)" using `m\<le>n`
himmelma@33175
  5897
    proof(induct n)
himmelma@33175
  5898
      case 0 thus ?case by auto
himmelma@33175
  5899
    next
himmelma@33175
  5900
      case (Suc n)
himmelma@33175
  5901
      thus ?case proof(cases "m\<le>n")
himmelma@33175
  5902
        case True thus ?thesis using Suc(1)
himmelma@33175
  5903
          using dist'[OF fs fs, OF `w\<in>s` `z\<in>s`, of n n] by auto
himmelma@33175
  5904
      next
himmelma@33175
  5905
        case False hence mn:"m = Suc n" using Suc(2) by simp
himmelma@33175
  5906
        show ?thesis unfolding mn  by auto
himmelma@33175
  5907
      qed
himmelma@33175
  5908
    qed } note distf = this
himmelma@33175
  5909
himmelma@33175
  5910
  def h \<equiv> "\<lambda>n. (f n x, f n y)"
himmelma@33175
  5911
  let ?s2 = "s \<times> s"
himmelma@33175
  5912
  obtain l r where "l\<in>?s2" and r:"subseq r" and lr:"((h \<circ> r) ---> l) sequentially"
himmelma@33175
  5913
    using compact_Times [OF s(1) s(1), unfolded compact_def, THEN spec[where x=h]] unfolding  h_def
himmelma@33175
  5914
    using fs[OF `x\<in>s`] and fs[OF `y\<in>s`] by blast
himmelma@33175
  5915
  def a \<equiv> "fst l" def b \<equiv> "snd l"
himmelma@33175
  5916
  have lab:"l = (a, b)" unfolding a_def b_def by simp
himmelma@33175
  5917
  have [simp]:"a\<in>s" "b\<in>s" unfolding a_def b_def using `l\<in>?s2` by auto
himmelma@33175
  5918
himmelma@33175
  5919
  have lima:"((fst \<circ> (h \<circ> r)) ---> a) sequentially"
himmelma@33175
  5920
   and limb:"((snd \<circ> (h \<circ> r)) ---> b) sequentially"
himmelma@33175
  5921
    using lr
huffman@45032
  5922
    unfolding o_def a_def b_def by (rule tendsto_intros)+
himmelma@33175
  5923
himmelma@33175
  5924
  { fix n::nat
himmelma@33175
  5925
    have *:"\<And>fx fy (x::'a) y. dist fx fy \<le> dist x y \<Longrightarrow> \<not> (dist (fx - fy) (a - b) < dist a b - dist x y)" unfolding dist_norm by norm
himmelma@33175
  5926
    { fix x y :: 'a
himmelma@33175
  5927
      have "dist (-x) (-y) = dist x y" unfolding dist_norm
himmelma@33175
  5928
        using norm_minus_cancel[of "x - y"] by (auto simp add: uminus_add_conv_diff) } note ** = this
himmelma@33175
  5929
himmelma@33175
  5930
    { assume as:"dist a b > dist (f n x) (f n y)"
himmelma@33175
  5931
      then obtain Na Nb where "\<forall>m\<ge>Na. dist (f (r m) x) a < (dist a b - dist (f n x) (f n y)) / 2"
himmelma@33175
  5932
        and "\<forall>m\<ge>Nb. dist (f (r m) y) b < (dist a b - dist (f n x) (f n y)) / 2"
himmelma@33175
  5933
        using lima limb unfolding h_def Lim_sequentially by (fastsimp simp del: less_divide_eq_number_of1)
himmelma@33175
  5934
      hence "dist (f (r (Na + Nb + n)) x - f (r (Na + Nb + n)) y) (a - b) < dist a b - dist (f n x) (f n y)"
himmelma@33175
  5935
        apply(erule_tac x="Na+Nb+n" in allE)
himmelma@33175
  5936
        apply(erule_tac x="Na+Nb+n" in allE) apply simp
himmelma@33175
  5937
        using dist_triangle_add_half[of a "f (r (Na + Nb + n)) x" "dist a b - dist (f n x) (f n y)"
himmelma@33175
  5938
          "-b"  "- f (r (Na + Nb + n)) y"]
haftmann@36349
  5939
        unfolding ** by (auto simp add: algebra_simps dist_commute)
himmelma@33175
  5940
      moreover
himmelma@33175
  5941
      have "dist (f (r (Na + Nb + n)) x - f (r (Na + Nb + n)) y) (a - b) \<ge> dist a b - dist (f n x) (f n y)"
himmelma@33175
  5942
        using distf[of n "r (Na+Nb+n)", OF _ `x\<in>s` `y\<in>s`]
himmelma@33175
  5943
        using subseq_bigger[OF r, of "Na+Nb+n"]
himmelma@33175
  5944
        using *[of "f (r (Na + Nb + n)) x" "f (r (Na + Nb + n)) y" "f n x" "f n y"] by auto
himmelma@33175
  5945
      ultimately have False by simp
himmelma@33175
  5946
    }
himmelma@33175
  5947
    hence "dist a b \<le> dist (f n x) (f n y)" by(rule ccontr)auto }
himmelma@33175
  5948
  note ab_fn = this
himmelma@33175
  5949
himmelma@33175
  5950
  have [simp]:"a = b" proof(rule ccontr)
himmelma@33175
  5951
    def e \<equiv> "dist a b - dist (g a) (g b)"
himmelma@33175
  5952
    assume "a\<noteq>b" hence "e > 0" unfolding e_def using dist by fastsimp
himmelma@33175
  5953
    hence "\<exists>n. dist (f n x) a < e/2 \<and> dist (f n y) b < e/2"
himmelma@33175
  5954
      using lima limb unfolding Lim_sequentially
himmelma@33175
  5955
      apply (auto elim!: allE[where x="e/2"]) apply(rule_tac x="r (max N Na)" in exI) unfolding h_def by fastsimp
himmelma@33175
  5956
    then obtain n where n:"dist (f n x) a < e/2 \<and> dist (f n y) b < e/2" by auto
himmelma@33175
  5957
    have "dist (f (Suc n) x) (g a) \<le> dist (f n x) a"
himmelma@33175
  5958
      using dist[THEN bspec[where x="f n x"], THEN bspec[where x="a"]] and fs by auto
himmelma@33175
  5959
    moreover have "dist (f (Suc n) y) (g b) \<le> dist (f n y) b"
himmelma@33175
  5960
      using dist[THEN bspec[where x="f n y"], THEN bspec[where x="b"]] and fs by auto
himmelma@33175
  5961
    ultimately have "dist (f (Suc n) x) (g a) + dist (f (Suc n) y) (g b) < e" using n by auto
himmelma@33175
  5962
    thus False unfolding e_def using ab_fn[of "Suc n"] by norm
himmelma@33175
  5963
  qed
himmelma@33175
  5964
himmelma@33175
  5965
  have [simp]:"\<And>n. f (Suc n) x = f n y" unfolding f_def y_def by(induct_tac n)auto
himmelma@33175
  5966
  { fix x y assume "x\<in>s" "y\<in>s" moreover
himmelma@33175
  5967
    fix e::real assume "e>0" ultimately
himmelma@33175
  5968
    have "dist y x < e \<longrightarrow> dist (g y) (g x) < e" using dist by fastsimp }
huffman@36355
  5969
  hence "continuous_on s g" unfolding continuous_on_iff by auto
himmelma@33175
  5970
himmelma@33175
  5971
  hence "((snd \<circ> h \<circ> r) ---> g a) sequentially" unfolding continuous_on_sequentially
himmelma@33175
  5972
    apply (rule allE[where x="\<lambda>n. (fst \<circ> h \<circ> r) n"]) apply (erule ballE[where x=a])
himmelma@33175
  5973
    using lima unfolding h_def o_def using fs[OF `x\<in>s`] by (auto simp add: y_def)
hoelzl@42841
  5974
  hence "g a = a" using tendsto_unique[OF trivial_limit_sequentially limb, of "g a"]
himmelma@33175
  5975
    unfolding `a=b` and o_assoc by auto
himmelma@33175
  5976
  moreover
himmelma@33175
  5977
  { fix x assume "x\<in>s" "g x = x" "x\<noteq>a"
himmelma@33175
  5978
    hence "False" using dist[THEN bspec[where x=a], THEN bspec[where x=x]]
himmelma@33175
  5979
      using `g a = a` and `a\<in>s` by auto  }
hoelzl@34986
  5980
  ultimately show "\<exists>!x\<in>s. g x = x" using `a\<in>s` by blast
himmelma@33175
  5981
qed
himmelma@33175
  5982
hoelzl@37489
  5983
hoelzl@37489
  5984
(** TODO move this someplace else within this theory **)
hoelzl@37489
  5985
instance euclidean_space \<subseteq> banach ..
hoelzl@37489
  5986
huffman@44989
  5987
declare tendsto_const [intro] (* FIXME: move *)
huffman@44989
  5988
huffman@44983
  5989
text {* Legacy theorem names *}
huffman@44983
  5990
huffman@44983
  5991
lemmas Lim_ident_at = LIM_ident
huffman@44983
  5992
lemmas Lim_const = tendsto_const
huffman@45145
  5993
lemmas Lim_cmul = tendsto_scaleR [OF tendsto_const]
huffman@44983
  5994
lemmas Lim_neg = tendsto_minus
huffman@44983
  5995
lemmas Lim_add = tendsto_add
huffman@44983
  5996
lemmas Lim_sub = tendsto_diff
huffman@45145
  5997
lemmas Lim_mul = tendsto_scaleR
huffman@45145
  5998
lemmas Lim_vmul = tendsto_scaleR [OF _ tendsto_const]
huffman@44983
  5999
lemmas Lim_null_norm = tendsto_norm_zero_iff [symmetric]
huffman@44983
  6000
lemmas Lim_linear = bounded_linear.tendsto [COMP swap_prems_rl]
huffman@45145
  6001
lemmas Lim_component = tendsto_euclidean_component
huffman@44983
  6002
lemmas Lim_intros = Lim_add Lim_const Lim_sub Lim_cmul Lim_vmul Lim_within_id
huffman@44983
  6003
himmelma@33175
  6004
end