src/HOL/Library/Topology_Euclidean_Space.thy
changeset 33175 2083bde13ce1
parent 33083 1fad3160d873
child 33176 d6936fd7cda8
     1.1 --- a/src/HOL/Library/Topology_Euclidean_Space.thy	Fri Oct 23 14:33:07 2009 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,6027 +0,0 @@
     1.4 -(*  Title:      HOL/Library/Topology_Euclidian_Space.thy
     1.5 -    Author:     Amine Chaieb, University of Cambridge
     1.6 -    Author:     Robert Himmelmann, TU Muenchen
     1.7 -*)
     1.8 -
     1.9 -header {* Elementary topology in Euclidean space. *}
    1.10 -
    1.11 -theory Topology_Euclidean_Space
    1.12 -imports SEQ Euclidean_Space Product_Vector
    1.13 -begin
    1.14 -
    1.15 -declare fstcart_pastecart[simp] sndcart_pastecart[simp]
    1.16 -
    1.17 -subsection{* General notion of a topology *}
    1.18 -
    1.19 -definition "istopology L \<longleftrightarrow> {} \<in> L \<and> (\<forall>S \<in>L. \<forall>T \<in>L. S \<inter> T \<in> L) \<and> (\<forall>K. K \<subseteq>L \<longrightarrow> \<Union> K \<in> L)"
    1.20 -typedef (open) 'a topology = "{L::('a set) set. istopology L}"
    1.21 -  morphisms "openin" "topology"
    1.22 -  unfolding istopology_def by blast
    1.23 -
    1.24 -lemma istopology_open_in[intro]: "istopology(openin U)"
    1.25 -  using openin[of U] by blast
    1.26 -
    1.27 -lemma topology_inverse': "istopology U \<Longrightarrow> openin (topology U) = U"
    1.28 -  using topology_inverse[unfolded mem_def Collect_def] .
    1.29 -
    1.30 -lemma topology_inverse_iff: "istopology U \<longleftrightarrow> openin (topology U) = U"
    1.31 -  using topology_inverse[of U] istopology_open_in[of "topology U"] by auto
    1.32 -
    1.33 -lemma topology_eq: "T1 = T2 \<longleftrightarrow> (\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S)"
    1.34 -proof-
    1.35 -  {assume "T1=T2" hence "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S" by simp}
    1.36 -  moreover
    1.37 -  {assume H: "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S"
    1.38 -    hence "openin T1 = openin T2" by (metis mem_def set_ext)
    1.39 -    hence "topology (openin T1) = topology (openin T2)" by simp
    1.40 -    hence "T1 = T2" unfolding openin_inverse .}
    1.41 -  ultimately show ?thesis by blast
    1.42 -qed
    1.43 -
    1.44 -text{* Infer the "universe" from union of all sets in the topology. *}
    1.45 -
    1.46 -definition "topspace T =  \<Union>{S. openin T S}"
    1.47 -
    1.48 -subsection{* Main properties of open sets *}
    1.49 -
    1.50 -lemma openin_clauses:
    1.51 -  fixes U :: "'a topology"
    1.52 -  shows "openin U {}"
    1.53 -  "\<And>S T. openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S\<inter>T)"
    1.54 -  "\<And>K. (\<forall>S \<in> K. openin U S) \<Longrightarrow> openin U (\<Union>K)"
    1.55 -  using openin[of U] unfolding istopology_def Collect_def mem_def
    1.56 -  by (metis mem_def subset_eq)+
    1.57 -
    1.58 -lemma openin_subset[intro]: "openin U S \<Longrightarrow> S \<subseteq> topspace U"
    1.59 -  unfolding topspace_def by blast
    1.60 -lemma openin_empty[simp]: "openin U {}" by (simp add: openin_clauses)
    1.61 -
    1.62 -lemma openin_Int[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<inter> T)"
    1.63 -  by (simp add: openin_clauses)
    1.64 -
    1.65 -lemma openin_Union[intro]: "(\<forall>S \<in>K. openin U S) \<Longrightarrow> openin U (\<Union> K)" by (simp add: openin_clauses)
    1.66 -
    1.67 -lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"
    1.68 -  using openin_Union[of "{S,T}" U] by auto
    1.69 -
    1.70 -lemma openin_topspace[intro, simp]: "openin U (topspace U)" by (simp add: openin_Union topspace_def)
    1.71 -
    1.72 -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")
    1.73 -proof-
    1.74 -  {assume ?lhs then have ?rhs by auto }
    1.75 -  moreover
    1.76 -  {assume H: ?rhs
    1.77 -    then obtain t where t: "\<forall>x\<in>S. openin U (t x) \<and> x \<in> t x \<and> t x \<subseteq> S"
    1.78 -      unfolding Ball_def ex_simps(6)[symmetric] choice_iff by blast
    1.79 -    from t have th0: "\<forall>x\<in> t`S. openin U x" by auto
    1.80 -    have "\<Union> t`S = S" using t by auto
    1.81 -    with openin_Union[OF th0] have "openin U S" by simp }
    1.82 -  ultimately show ?thesis by blast
    1.83 -qed
    1.84 -
    1.85 -subsection{* Closed sets *}
    1.86 -
    1.87 -definition "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"
    1.88 -
    1.89 -lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U" by (metis closedin_def)
    1.90 -lemma closedin_empty[simp]: "closedin U {}" by (simp add: closedin_def)
    1.91 -lemma closedin_topspace[intro,simp]:
    1.92 -  "closedin U (topspace U)" by (simp add: closedin_def)
    1.93 -lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"
    1.94 -  by (auto simp add: Diff_Un closedin_def)
    1.95 -
    1.96 -lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union> {A - s|s. s\<in>S}" by auto
    1.97 -lemma closedin_Inter[intro]: assumes Ke: "K \<noteq> {}" and Kc: "\<forall>S \<in>K. closedin U S"
    1.98 -  shows "closedin U (\<Inter> K)"  using Ke Kc unfolding closedin_def Diff_Inter by auto
    1.99 -
   1.100 -lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"
   1.101 -  using closedin_Inter[of "{S,T}" U] by auto
   1.102 -
   1.103 -lemma Diff_Diff_Int: "A - (A - B) = A \<inter> B" by blast
   1.104 -lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"
   1.105 -  apply (auto simp add: closedin_def Diff_Diff_Int inf_absorb2)
   1.106 -  apply (metis openin_subset subset_eq)
   1.107 -  done
   1.108 -
   1.109 -lemma openin_closedin:  "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"
   1.110 -  by (simp add: openin_closedin_eq)
   1.111 -
   1.112 -lemma openin_diff[intro]: assumes oS: "openin U S" and cT: "closedin U T" shows "openin U (S - T)"
   1.113 -proof-
   1.114 -  have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT
   1.115 -    by (auto simp add: topspace_def openin_subset)
   1.116 -  then show ?thesis using oS cT by (auto simp add: closedin_def)
   1.117 -qed
   1.118 -
   1.119 -lemma closedin_diff[intro]: assumes oS: "closedin U S" and cT: "openin U T" shows "closedin U (S - T)"
   1.120 -proof-
   1.121 -  have "S - T = S \<inter> (topspace U - T)" using closedin_subset[of U S]  oS cT
   1.122 -    by (auto simp add: topspace_def )
   1.123 -  then show ?thesis using oS cT by (auto simp add: openin_closedin_eq)
   1.124 -qed
   1.125 -
   1.126 -subsection{* Subspace topology. *}
   1.127 -
   1.128 -definition "subtopology U V = topology {S \<inter> V |S. openin U S}"
   1.129 -
   1.130 -lemma istopology_subtopology: "istopology {S \<inter> V |S. openin U S}" (is "istopology ?L")
   1.131 -proof-
   1.132 -  have "{} \<in> ?L" by blast
   1.133 -  {fix A B assume A: "A \<in> ?L" and B: "B \<in> ?L"
   1.134 -    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
   1.135 -    have "A\<inter>B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"  using Sa Sb by blast+
   1.136 -    then have "A \<inter> B \<in> ?L" by blast}
   1.137 -  moreover
   1.138 -  {fix K assume K: "K \<subseteq> ?L"
   1.139 -    have th0: "?L = (\<lambda>S. S \<inter> V) ` openin U "
   1.140 -      apply (rule set_ext)
   1.141 -      apply (simp add: Ball_def image_iff)
   1.142 -      by (metis mem_def)
   1.143 -    from K[unfolded th0 subset_image_iff]
   1.144 -    obtain Sk where Sk: "Sk \<subseteq> openin U" "K = (\<lambda>S. S \<inter> V) ` Sk" by blast
   1.145 -    have "\<Union>K = (\<Union>Sk) \<inter> V" using Sk by auto
   1.146 -    moreover have "openin U (\<Union> Sk)" using Sk by (auto simp add: subset_eq mem_def)
   1.147 -    ultimately have "\<Union>K \<in> ?L" by blast}
   1.148 -  ultimately show ?thesis unfolding istopology_def by blast
   1.149 -qed
   1.150 -
   1.151 -lemma openin_subtopology:
   1.152 -  "openin (subtopology U V) S \<longleftrightarrow> (\<exists> T. (openin U T) \<and> (S = T \<inter> V))"
   1.153 -  unfolding subtopology_def topology_inverse'[OF istopology_subtopology]
   1.154 -  by (auto simp add: Collect_def)
   1.155 -
   1.156 -lemma topspace_subtopology: "topspace(subtopology U V) = topspace U \<inter> V"
   1.157 -  by (auto simp add: topspace_def openin_subtopology)
   1.158 -
   1.159 -lemma closedin_subtopology:
   1.160 -  "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"
   1.161 -  unfolding closedin_def topspace_subtopology
   1.162 -  apply (simp add: openin_subtopology)
   1.163 -  apply (rule iffI)
   1.164 -  apply clarify
   1.165 -  apply (rule_tac x="topspace U - T" in exI)
   1.166 -  by auto
   1.167 -
   1.168 -lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"
   1.169 -  unfolding openin_subtopology
   1.170 -  apply (rule iffI, clarify)
   1.171 -  apply (frule openin_subset[of U])  apply blast
   1.172 -  apply (rule exI[where x="topspace U"])
   1.173 -  by auto
   1.174 -
   1.175 -lemma subtopology_superset: assumes UV: "topspace U \<subseteq> V"
   1.176 -  shows "subtopology U V = U"
   1.177 -proof-
   1.178 -  {fix S
   1.179 -    {fix T assume T: "openin U T" "S = T \<inter> V"
   1.180 -      from T openin_subset[OF T(1)] UV have eq: "S = T" by blast
   1.181 -      have "openin U S" unfolding eq using T by blast}
   1.182 -    moreover
   1.183 -    {assume S: "openin U S"
   1.184 -      hence "\<exists>T. openin U T \<and> S = T \<inter> V"
   1.185 -        using openin_subset[OF S] UV by auto}
   1.186 -    ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S" by blast}
   1.187 -  then show ?thesis unfolding topology_eq openin_subtopology by blast
   1.188 -qed
   1.189 -
   1.190 -
   1.191 -lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"
   1.192 -  by (simp add: subtopology_superset)
   1.193 -
   1.194 -lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"
   1.195 -  by (simp add: subtopology_superset)
   1.196 -
   1.197 -subsection{* The universal Euclidean versions are what we use most of the time *}
   1.198 -
   1.199 -definition
   1.200 -  euclidean :: "'a::topological_space topology" where
   1.201 -  "euclidean = topology open"
   1.202 -
   1.203 -lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"
   1.204 -  unfolding euclidean_def
   1.205 -  apply (rule cong[where x=S and y=S])
   1.206 -  apply (rule topology_inverse[symmetric])
   1.207 -  apply (auto simp add: istopology_def)
   1.208 -  by (auto simp add: mem_def subset_eq)
   1.209 -
   1.210 -lemma topspace_euclidean: "topspace euclidean = UNIV"
   1.211 -  apply (simp add: topspace_def)
   1.212 -  apply (rule set_ext)
   1.213 -  by (auto simp add: open_openin[symmetric])
   1.214 -
   1.215 -lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"
   1.216 -  by (simp add: topspace_euclidean topspace_subtopology)
   1.217 -
   1.218 -lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"
   1.219 -  by (simp add: closed_def closedin_def topspace_euclidean open_openin Compl_eq_Diff_UNIV)
   1.220 -
   1.221 -lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"
   1.222 -  by (simp add: open_openin openin_subopen[symmetric])
   1.223 -
   1.224 -subsection{* Open and closed balls. *}
   1.225 -
   1.226 -definition
   1.227 -  ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where
   1.228 -  "ball x e = {y. dist x y < e}"
   1.229 -
   1.230 -definition
   1.231 -  cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where
   1.232 -  "cball x e = {y. dist x y \<le> e}"
   1.233 -
   1.234 -lemma mem_ball[simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e" by (simp add: ball_def)
   1.235 -lemma mem_cball[simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e" by (simp add: cball_def)
   1.236 -
   1.237 -lemma mem_ball_0 [simp]:
   1.238 -  fixes x :: "'a::real_normed_vector"
   1.239 -  shows "x \<in> ball 0 e \<longleftrightarrow> norm x < e"
   1.240 -  by (simp add: dist_norm)
   1.241 -
   1.242 -lemma mem_cball_0 [simp]:
   1.243 -  fixes x :: "'a::real_normed_vector"
   1.244 -  shows "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"
   1.245 -  by (simp add: dist_norm)
   1.246 -
   1.247 -lemma centre_in_cball[simp]: "x \<in> cball x e \<longleftrightarrow> 0\<le> e"  by simp
   1.248 -lemma ball_subset_cball[simp,intro]: "ball x e \<subseteq> cball x e" by (simp add: subset_eq)
   1.249 -lemma subset_ball[intro]: "d <= e ==> ball x d \<subseteq> ball x e" by (simp add: subset_eq)
   1.250 -lemma subset_cball[intro]: "d <= e ==> cball x d \<subseteq> cball x e" by (simp add: subset_eq)
   1.251 -lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"
   1.252 -  by (simp add: expand_set_eq) arith
   1.253 -
   1.254 -lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"
   1.255 -  by (simp add: expand_set_eq)
   1.256 -
   1.257 -subsection{* Topological properties of open balls *}
   1.258 -
   1.259 -lemma diff_less_iff: "(a::real) - b > 0 \<longleftrightarrow> a > b"
   1.260 -  "(a::real) - b < 0 \<longleftrightarrow> a < b"
   1.261 -  "a - b < c \<longleftrightarrow> a < c +b" "a - b > c \<longleftrightarrow> a > c +b" by arith+
   1.262 -lemma diff_le_iff: "(a::real) - b \<ge> 0 \<longleftrightarrow> a \<ge> b" "(a::real) - b \<le> 0 \<longleftrightarrow> a \<le> b"
   1.263 -  "a - b \<le> c \<longleftrightarrow> a \<le> c +b" "a - b \<ge> c \<longleftrightarrow> a \<ge> c +b"  by arith+
   1.264 -
   1.265 -lemma open_ball[intro, simp]: "open (ball x e)"
   1.266 -  unfolding open_dist ball_def Collect_def Ball_def mem_def
   1.267 -  unfolding dist_commute
   1.268 -  apply clarify
   1.269 -  apply (rule_tac x="e - dist xa x" in exI)
   1.270 -  using dist_triangle_alt[where z=x]
   1.271 -  apply (clarsimp simp add: diff_less_iff)
   1.272 -  apply atomize
   1.273 -  apply (erule_tac x="y" in allE)
   1.274 -  apply (erule_tac x="xa" in allE)
   1.275 -  by arith
   1.276 -
   1.277 -lemma centre_in_ball[simp]: "x \<in> ball x e \<longleftrightarrow> e > 0" by (metis mem_ball dist_self)
   1.278 -lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"
   1.279 -  unfolding open_dist subset_eq mem_ball Ball_def dist_commute ..
   1.280 -
   1.281 -lemma open_contains_ball_eq: "open S \<Longrightarrow> \<forall>x. x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
   1.282 -  by (metis open_contains_ball subset_eq centre_in_ball)
   1.283 -
   1.284 -lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"
   1.285 -  unfolding mem_ball expand_set_eq
   1.286 -  apply (simp add: not_less)
   1.287 -  by (metis zero_le_dist order_trans dist_self)
   1.288 -
   1.289 -lemma ball_empty[intro]: "e \<le> 0 ==> ball x e = {}" by simp
   1.290 -
   1.291 -subsection{* Basic "localization" results are handy for connectedness. *}
   1.292 -
   1.293 -lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"
   1.294 -  by (auto simp add: openin_subtopology open_openin[symmetric])
   1.295 -
   1.296 -lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"
   1.297 -  by (auto simp add: openin_open)
   1.298 -
   1.299 -lemma open_openin_trans[trans]:
   1.300 - "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"
   1.301 -  by (metis Int_absorb1  openin_open_Int)
   1.302 -
   1.303 -lemma open_subset:  "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"
   1.304 -  by (auto simp add: openin_open)
   1.305 -
   1.306 -lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"
   1.307 -  by (simp add: closedin_subtopology closed_closedin Int_ac)
   1.308 -
   1.309 -lemma closedin_closed_Int: "closed S ==> closedin (subtopology euclidean U) (U \<inter> S)"
   1.310 -  by (metis closedin_closed)
   1.311 -
   1.312 -lemma closed_closedin_trans: "closed S \<Longrightarrow> closed T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> closedin (subtopology euclidean S) T"
   1.313 -  apply (subgoal_tac "S \<inter> T = T" )
   1.314 -  apply auto
   1.315 -  apply (frule closedin_closed_Int[of T S])
   1.316 -  by simp
   1.317 -
   1.318 -lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"
   1.319 -  by (auto simp add: closedin_closed)
   1.320 -
   1.321 -lemma openin_euclidean_subtopology_iff:
   1.322 -  fixes S U :: "'a::metric_space set"
   1.323 -  shows "openin (subtopology euclidean U) S
   1.324 -  \<longleftrightarrow> S \<subseteq> U \<and> (\<forall>x\<in>S. \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x'\<in> S)" (is "?lhs \<longleftrightarrow> ?rhs")
   1.325 -proof-
   1.326 -  {assume ?lhs hence ?rhs unfolding openin_subtopology open_openin[symmetric]
   1.327 -      by (simp add: open_dist) blast}
   1.328 -  moreover
   1.329 -  {assume SU: "S \<subseteq> U" and H: "\<And>x. x \<in> S \<Longrightarrow> \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x' \<in> S"
   1.330 -    from H obtain d where d: "\<And>x . x\<in> S \<Longrightarrow> d x > 0 \<and> (\<forall>x' \<in> U. dist x' x < d x \<longrightarrow> x' \<in> S)"
   1.331 -      by metis
   1.332 -    let ?T = "\<Union>{B. \<exists>x\<in>S. B = ball x (d x)}"
   1.333 -    have oT: "open ?T" by auto
   1.334 -    { fix x assume "x\<in>S"
   1.335 -      hence "x \<in> \<Union>{B. \<exists>x\<in>S. B = ball x (d x)}"
   1.336 -        apply simp apply(rule_tac x="ball x(d x)" in exI) apply auto
   1.337 -        by (rule d [THEN conjunct1])
   1.338 -      hence "x\<in> ?T \<inter> U" using SU and `x\<in>S` by auto  }
   1.339 -    moreover
   1.340 -    { fix y assume "y\<in>?T"
   1.341 -      then obtain B where "y\<in>B" "B\<in>{B. \<exists>x\<in>S. B = ball x (d x)}" by auto
   1.342 -      then obtain x where "x\<in>S" and x:"y \<in> ball x (d x)" by auto
   1.343 -      assume "y\<in>U"
   1.344 -      hence "y\<in>S" using d[OF `x\<in>S`] and x by(auto simp add: dist_commute) }
   1.345 -    ultimately have "S = ?T \<inter> U" by blast
   1.346 -    with oT have ?lhs unfolding openin_subtopology open_openin[symmetric] by blast}
   1.347 -  ultimately show ?thesis by blast
   1.348 -qed
   1.349 -
   1.350 -text{* These "transitivity" results are handy too. *}
   1.351 -
   1.352 -lemma openin_trans[trans]: "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T
   1.353 -  \<Longrightarrow> openin (subtopology euclidean U) S"
   1.354 -  unfolding open_openin openin_open by blast
   1.355 -
   1.356 -lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"
   1.357 -  by (auto simp add: openin_open intro: openin_trans)
   1.358 -
   1.359 -lemma closedin_trans[trans]:
   1.360 - "closedin (subtopology euclidean T) S \<Longrightarrow>
   1.361 -           closedin (subtopology euclidean U) T
   1.362 -           ==> closedin (subtopology euclidean U) S"
   1.363 -  by (auto simp add: closedin_closed closed_closedin closed_Inter Int_assoc)
   1.364 -
   1.365 -lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"
   1.366 -  by (auto simp add: closedin_closed intro: closedin_trans)
   1.367 -
   1.368 -subsection{* Connectedness *}
   1.369 -
   1.370 -definition "connected S \<longleftrightarrow>
   1.371 -  ~(\<exists>e1 e2. open e1 \<and> open e2 \<and> S \<subseteq> (e1 \<union> e2) \<and> (e1 \<inter> e2 \<inter> S = {})
   1.372 -  \<and> ~(e1 \<inter> S = {}) \<and> ~(e2 \<inter> S = {}))"
   1.373 -
   1.374 -lemma connected_local:
   1.375 - "connected S \<longleftrightarrow> ~(\<exists>e1 e2.
   1.376 -                 openin (subtopology euclidean S) e1 \<and>
   1.377 -                 openin (subtopology euclidean S) e2 \<and>
   1.378 -                 S \<subseteq> e1 \<union> e2 \<and>
   1.379 -                 e1 \<inter> e2 = {} \<and>
   1.380 -                 ~(e1 = {}) \<and>
   1.381 -                 ~(e2 = {}))"
   1.382 -unfolding connected_def openin_open by (safe, blast+)
   1.383 -
   1.384 -lemma exists_diff: "(\<exists>S. P(UNIV - S)) \<longleftrightarrow> (\<exists>S. P S)" (is "?lhs \<longleftrightarrow> ?rhs")
   1.385 -proof-
   1.386 -
   1.387 -  {assume "?lhs" hence ?rhs by blast }
   1.388 -  moreover
   1.389 -  {fix S assume H: "P S"
   1.390 -    have "S = UNIV - (UNIV - S)" by auto
   1.391 -    with H have "P (UNIV - (UNIV - S))" by metis }
   1.392 -  ultimately show ?thesis by metis
   1.393 -qed
   1.394 -
   1.395 -lemma connected_clopen: "connected S \<longleftrightarrow>
   1.396 -        (\<forall>T. openin (subtopology euclidean S) T \<and>
   1.397 -            closedin (subtopology euclidean S) T \<longrightarrow> T = {} \<or> T = S)" (is "?lhs \<longleftrightarrow> ?rhs")
   1.398 -proof-
   1.399 -  have " \<not> connected S \<longleftrightarrow> (\<exists>e1 e2. open e1 \<and> open (UNIV - e2) \<and> S \<subseteq> e1 \<union> (UNIV - e2) \<and> e1 \<inter> (UNIV - e2) \<inter> S = {} \<and> e1 \<inter> S \<noteq> {} \<and> (UNIV - e2) \<inter> S \<noteq> {})"
   1.400 -    unfolding connected_def openin_open closedin_closed
   1.401 -    apply (subst exists_diff) by blast
   1.402 -  hence th0: "connected S \<longleftrightarrow> \<not> (\<exists>e2 e1. closed e2 \<and> open e1 \<and> S \<subseteq> e1 \<union> (UNIV - e2) \<and> e1 \<inter> (UNIV - e2) \<inter> S = {} \<and> e1 \<inter> S \<noteq> {} \<and> (UNIV - e2) \<inter> S \<noteq> {})"
   1.403 -    (is " _ \<longleftrightarrow> \<not> (\<exists>e2 e1. ?P e2 e1)") apply (simp add: closed_def Compl_eq_Diff_UNIV) by metis
   1.404 -
   1.405 -  have th1: "?rhs \<longleftrightarrow> \<not> (\<exists>t' t. closed t'\<and>t = S\<inter>t' \<and> t\<noteq>{} \<and> t\<noteq>S \<and> (\<exists>t'. open t' \<and> t = S \<inter> t'))"
   1.406 -    (is "_ \<longleftrightarrow> \<not> (\<exists>t' t. ?Q t' t)")
   1.407 -    unfolding connected_def openin_open closedin_closed by auto
   1.408 -  {fix e2
   1.409 -    {fix e1 have "?P e2 e1 \<longleftrightarrow> (\<exists>t.  closed e2 \<and> t = S\<inter>e2 \<and> open e1 \<and> t = S\<inter>e1 \<and> t\<noteq>{} \<and> t\<noteq>S)"
   1.410 -        by auto}
   1.411 -    then have "(\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by metis}
   1.412 -  then have "\<forall>e2. (\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by blast
   1.413 -  then show ?thesis unfolding th0 th1 by simp
   1.414 -qed
   1.415 -
   1.416 -lemma connected_empty[simp, intro]: "connected {}"
   1.417 -  by (simp add: connected_def)
   1.418 -
   1.419 -subsection{* Hausdorff and other separation properties *}
   1.420 -
   1.421 -class t0_space =
   1.422 -  assumes t0_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U)"
   1.423 -
   1.424 -class t1_space =
   1.425 -  assumes t1_space: "x \<noteq> y \<Longrightarrow> \<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<notin> U \<and> x \<notin> V \<and> y \<in> V"
   1.426 -begin
   1.427 -
   1.428 -subclass t0_space
   1.429 -proof
   1.430 -qed (fast dest: t1_space)
   1.431 -
   1.432 -end
   1.433 -
   1.434 -text {* T2 spaces are also known as Hausdorff spaces. *}
   1.435 -
   1.436 -class t2_space =
   1.437 -  assumes hausdorff: "x \<noteq> y \<Longrightarrow> \<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
   1.438 -begin
   1.439 -
   1.440 -subclass t1_space
   1.441 -proof
   1.442 -qed (fast dest: hausdorff)
   1.443 -
   1.444 -end
   1.445 -
   1.446 -instance metric_space \<subseteq> t2_space
   1.447 -proof
   1.448 -  fix x y :: "'a::metric_space"
   1.449 -  assume xy: "x \<noteq> y"
   1.450 -  let ?U = "ball x (dist x y / 2)"
   1.451 -  let ?V = "ball y (dist x y / 2)"
   1.452 -  have th0: "\<And>d x y z. (d x z :: real) <= d x y + d y z \<Longrightarrow> d y z = d z y
   1.453 -               ==> ~(d x y * 2 < d x z \<and> d z y * 2 < d x z)" by arith
   1.454 -  have "open ?U \<and> open ?V \<and> x \<in> ?U \<and> y \<in> ?V \<and> ?U \<inter> ?V = {}"
   1.455 -    using dist_pos_lt[OF xy] th0[of dist,OF dist_triangle dist_commute]
   1.456 -    by (auto simp add: expand_set_eq)
   1.457 -  then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
   1.458 -    by blast
   1.459 -qed
   1.460 -
   1.461 -lemma separation_t2:
   1.462 -  fixes x y :: "'a::t2_space"
   1.463 -  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {})"
   1.464 -  using hausdorff[of x y] by blast
   1.465 -
   1.466 -lemma separation_t1:
   1.467 -  fixes x y :: "'a::t1_space"
   1.468 -  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U V. open U \<and> open V \<and> x \<in>U \<and> y\<notin> U \<and> x\<notin>V \<and> y\<in>V)"
   1.469 -  using t1_space[of x y] by blast
   1.470 -
   1.471 -lemma separation_t0:
   1.472 -  fixes x y :: "'a::t0_space"
   1.473 -  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> ~(x\<in>U \<longleftrightarrow> y\<in>U))"
   1.474 -  using t0_space[of x y] by blast
   1.475 -
   1.476 -subsection{* Limit points *}
   1.477 -
   1.478 -definition
   1.479 -  islimpt:: "'a::topological_space \<Rightarrow> 'a set \<Rightarrow> bool"
   1.480 -    (infixr "islimpt" 60) where
   1.481 -  "x islimpt S \<longleftrightarrow> (\<forall>T. x\<in>T \<longrightarrow> open T \<longrightarrow> (\<exists>y\<in>S. y\<in>T \<and> y\<noteq>x))"
   1.482 -
   1.483 -lemma islimptI:
   1.484 -  assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
   1.485 -  shows "x islimpt S"
   1.486 -  using assms unfolding islimpt_def by auto
   1.487 -
   1.488 -lemma islimptE:
   1.489 -  assumes "x islimpt S" and "x \<in> T" and "open T"
   1.490 -  obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"
   1.491 -  using assms unfolding islimpt_def by auto
   1.492 -
   1.493 -lemma islimpt_subset: "x islimpt S \<Longrightarrow> S \<subseteq> T ==> x islimpt T" by (auto simp add: islimpt_def)
   1.494 -
   1.495 -lemma islimpt_approachable:
   1.496 -  fixes x :: "'a::metric_space"
   1.497 -  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"
   1.498 -  unfolding islimpt_def
   1.499 -  apply auto
   1.500 -  apply(erule_tac x="ball x e" in allE)
   1.501 -  apply auto
   1.502 -  apply(rule_tac x=y in bexI)
   1.503 -  apply (auto simp add: dist_commute)
   1.504 -  apply (simp add: open_dist, drule (1) bspec)
   1.505 -  apply (clarify, drule spec, drule (1) mp, auto)
   1.506 -  done
   1.507 -
   1.508 -lemma islimpt_approachable_le:
   1.509 -  fixes x :: "'a::metric_space"
   1.510 -  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> S. x' \<noteq> x \<and> dist x' x <= e)"
   1.511 -  unfolding islimpt_approachable
   1.512 -  using approachable_lt_le[where f="\<lambda>x'. dist x' x" and P="\<lambda>x'. \<not> (x'\<in>S \<and> x'\<noteq>x)"]
   1.513 -  by metis (* FIXME: VERY slow! *)
   1.514 -
   1.515 -class perfect_space =
   1.516 -  (* FIXME: perfect_space should inherit from topological_space *)
   1.517 -  assumes islimpt_UNIV [simp, intro]: "(x::'a::metric_space) islimpt UNIV"
   1.518 -
   1.519 -lemma perfect_choose_dist:
   1.520 -  fixes x :: "'a::perfect_space"
   1.521 -  shows "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"
   1.522 -using islimpt_UNIV [of x]
   1.523 -by (simp add: islimpt_approachable)
   1.524 -
   1.525 -instance real :: perfect_space
   1.526 -apply default
   1.527 -apply (rule islimpt_approachable [THEN iffD2])
   1.528 -apply (clarify, rule_tac x="x + e/2" in bexI)
   1.529 -apply (auto simp add: dist_norm)
   1.530 -done
   1.531 -
   1.532 -instance "^" :: (perfect_space, finite) perfect_space
   1.533 -proof
   1.534 -  fix x :: "'a ^ 'b"
   1.535 -  {
   1.536 -    fix e :: real assume "0 < e"
   1.537 -    def a \<equiv> "x $ undefined"
   1.538 -    have "a islimpt UNIV" by (rule islimpt_UNIV)
   1.539 -    with `0 < e` obtain b where "b \<noteq> a" and "dist b a < e"
   1.540 -      unfolding islimpt_approachable by auto
   1.541 -    def y \<equiv> "Cart_lambda ((Cart_nth x)(undefined := b))"
   1.542 -    from `b \<noteq> a` have "y \<noteq> x"
   1.543 -      unfolding a_def y_def by (simp add: Cart_eq)
   1.544 -    from `dist b a < e` have "dist y x < e"
   1.545 -      unfolding dist_vector_def a_def y_def
   1.546 -      apply simp
   1.547 -      apply (rule le_less_trans [OF setL2_le_setsum [OF zero_le_dist]])
   1.548 -      apply (subst setsum_diff1' [where a=undefined], simp, simp, simp)
   1.549 -      done
   1.550 -    from `y \<noteq> x` and `dist y x < e`
   1.551 -    have "\<exists>y\<in>UNIV. y \<noteq> x \<and> dist y x < e" by auto
   1.552 -  }
   1.553 -  then show "x islimpt UNIV" unfolding islimpt_approachable by blast
   1.554 -qed
   1.555 -
   1.556 -lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"
   1.557 -  unfolding closed_def
   1.558 -  apply (subst open_subopen)
   1.559 -  apply (simp add: islimpt_def subset_eq Compl_eq_Diff_UNIV)
   1.560 -  by (metis DiffE DiffI UNIV_I insertCI insert_absorb mem_def)
   1.561 -
   1.562 -lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"
   1.563 -  unfolding islimpt_def by auto
   1.564 -
   1.565 -lemma closed_positive_orthant: "closed {x::real^'n::finite. \<forall>i. 0 \<le>x$i}"
   1.566 -proof-
   1.567 -  let ?U = "UNIV :: 'n set"
   1.568 -  let ?O = "{x::real^'n. \<forall>i. x$i\<ge>0}"
   1.569 -  {fix x:: "real^'n" and i::'n assume H: "\<forall>e>0. \<exists>x'\<in>?O. x' \<noteq> x \<and> dist x' x < e"
   1.570 -    and xi: "x$i < 0"
   1.571 -    from xi have th0: "-x$i > 0" by arith
   1.572 -    from H[rule_format, OF th0] obtain x' where x': "x' \<in>?O" "x' \<noteq> x" "dist x' x < -x $ i" by blast
   1.573 -      have th:" \<And>b a (x::real). abs x <= b \<Longrightarrow> b <= a ==> ~(a + x < 0)" by arith
   1.574 -      have th': "\<And>x (y::real). x < 0 \<Longrightarrow> 0 <= y ==> abs x <= abs (y - x)" by arith
   1.575 -      have th1: "\<bar>x$i\<bar> \<le> \<bar>(x' - x)$i\<bar>" using x'(1) xi
   1.576 -        apply (simp only: vector_component)
   1.577 -        by (rule th') auto
   1.578 -      have th2: "\<bar>dist x x'\<bar> \<ge> \<bar>(x' - x)$i\<bar>" using  component_le_norm[of "x'-x" i]
   1.579 -        apply (simp add: dist_norm) by norm
   1.580 -      from th[OF th1 th2] x'(3) have False by (simp add: dist_commute) }
   1.581 -  then show ?thesis unfolding closed_limpt islimpt_approachable
   1.582 -    unfolding not_le[symmetric] by blast
   1.583 -qed
   1.584 -
   1.585 -lemma finite_set_avoid:
   1.586 -  fixes a :: "'a::metric_space"
   1.587 -  assumes fS: "finite S" shows  "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d <= dist a x"
   1.588 -proof(induct rule: finite_induct[OF fS])
   1.589 -  case 1 thus ?case apply auto by ferrack
   1.590 -next
   1.591 -  case (2 x F)
   1.592 -  from 2 obtain d where d: "d >0" "\<forall>x\<in>F. x\<noteq>a \<longrightarrow> d \<le> dist a x" by blast
   1.593 -  {assume "x = a" hence ?case using d by auto  }
   1.594 -  moreover
   1.595 -  {assume xa: "x\<noteq>a"
   1.596 -    let ?d = "min d (dist a x)"
   1.597 -    have dp: "?d > 0" using xa d(1) using dist_nz by auto
   1.598 -    from d have d': "\<forall>x\<in>F. x\<noteq>a \<longrightarrow> ?d \<le> dist a x" by auto
   1.599 -    with dp xa have ?case by(auto intro!: exI[where x="?d"]) }
   1.600 -  ultimately show ?case by blast
   1.601 -qed
   1.602 -
   1.603 -lemma islimpt_finite:
   1.604 -  fixes S :: "'a::metric_space set"
   1.605 -  assumes fS: "finite S" shows "\<not> a islimpt S"
   1.606 -  unfolding islimpt_approachable
   1.607 -  using finite_set_avoid[OF fS, of a] by (metis dist_commute  not_le)
   1.608 -
   1.609 -lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"
   1.610 -  apply (rule iffI)
   1.611 -  defer
   1.612 -  apply (metis Un_upper1 Un_upper2 islimpt_subset)
   1.613 -  unfolding islimpt_def
   1.614 -  apply (rule ccontr, clarsimp, rename_tac A B)
   1.615 -  apply (drule_tac x="A \<inter> B" in spec)
   1.616 -  apply (auto simp add: open_Int)
   1.617 -  done
   1.618 -
   1.619 -lemma discrete_imp_closed:
   1.620 -  fixes S :: "'a::metric_space set"
   1.621 -  assumes e: "0 < e" and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"
   1.622 -  shows "closed S"
   1.623 -proof-
   1.624 -  {fix x assume C: "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e"
   1.625 -    from e have e2: "e/2 > 0" by arith
   1.626 -    from C[rule_format, OF e2] obtain y where y: "y \<in> S" "y\<noteq>x" "dist y x < e/2" by blast
   1.627 -    let ?m = "min (e/2) (dist x y) "
   1.628 -    from e2 y(2) have mp: "?m > 0" by (simp add: dist_nz[THEN sym])
   1.629 -    from C[rule_format, OF mp] obtain z where z: "z \<in> S" "z\<noteq>x" "dist z x < ?m" by blast
   1.630 -    have th: "dist z y < e" using z y
   1.631 -      by (intro dist_triangle_lt [where z=x], simp)
   1.632 -    from d[rule_format, OF y(1) z(1) th] y z
   1.633 -    have False by (auto simp add: dist_commute)}
   1.634 -  then show ?thesis by (metis islimpt_approachable closed_limpt [where 'a='a])
   1.635 -qed
   1.636 -
   1.637 -subsection{* Interior of a Set *}
   1.638 -definition "interior S = {x. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S}"
   1.639 -
   1.640 -lemma interior_eq: "interior S = S \<longleftrightarrow> open S"
   1.641 -  apply (simp add: expand_set_eq interior_def)
   1.642 -  apply (subst (2) open_subopen) by (safe, blast+)
   1.643 -
   1.644 -lemma interior_open: "open S ==> (interior S = S)" by (metis interior_eq)
   1.645 -
   1.646 -lemma interior_empty[simp]: "interior {} = {}" by (simp add: interior_def)
   1.647 -
   1.648 -lemma open_interior[simp, intro]: "open(interior S)"
   1.649 -  apply (simp add: interior_def)
   1.650 -  apply (subst open_subopen) by blast
   1.651 -
   1.652 -lemma interior_interior[simp]: "interior(interior S) = interior S" by (metis interior_eq open_interior)
   1.653 -lemma interior_subset: "interior S \<subseteq> S" by (auto simp add: interior_def)
   1.654 -lemma subset_interior: "S \<subseteq> T ==> (interior S) \<subseteq> (interior T)" by (auto simp add: interior_def)
   1.655 -lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T ==> T \<subseteq> (interior S)" by (auto simp add: interior_def)
   1.656 -lemma interior_unique: "T \<subseteq> S \<Longrightarrow> open T  \<Longrightarrow> (\<forall>T'. T' \<subseteq> S \<and> open T' \<longrightarrow> T' \<subseteq> T) \<Longrightarrow> interior S = T"
   1.657 -  by (metis equalityI interior_maximal interior_subset open_interior)
   1.658 -lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e. 0 < e \<and> ball x e \<subseteq> S)"
   1.659 -  apply (simp add: interior_def)
   1.660 -  by (metis open_contains_ball centre_in_ball open_ball subset_trans)
   1.661 -
   1.662 -lemma open_subset_interior: "open S ==> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"
   1.663 -  by (metis interior_maximal interior_subset subset_trans)
   1.664 -
   1.665 -lemma interior_inter[simp]: "interior(S \<inter> T) = interior S \<inter> interior T"
   1.666 -  apply (rule equalityI, simp)
   1.667 -  apply (metis Int_lower1 Int_lower2 subset_interior)
   1.668 -  by (metis Int_mono interior_subset open_Int open_interior open_subset_interior)
   1.669 -
   1.670 -lemma interior_limit_point [intro]:
   1.671 -  fixes x :: "'a::perfect_space"
   1.672 -  assumes x: "x \<in> interior S" shows "x islimpt S"
   1.673 -proof-
   1.674 -  from x obtain e where e: "e>0" "\<forall>x'. dist x x' < e \<longrightarrow> x' \<in> S"
   1.675 -    unfolding mem_interior subset_eq Ball_def mem_ball by blast
   1.676 -  {
   1.677 -    fix d::real assume d: "d>0"
   1.678 -    let ?m = "min d e"
   1.679 -    have mde2: "0 < ?m" using e(1) d(1) by simp
   1.680 -    from perfect_choose_dist [OF mde2, of x]
   1.681 -    obtain y where "y \<noteq> x" and "dist y x < ?m" by blast
   1.682 -    then have "dist y x < e" "dist y x < d" by simp_all
   1.683 -    from `dist y x < e` e(2) have "y \<in> S" by (simp add: dist_commute)
   1.684 -    have "\<exists>x'\<in>S. x'\<noteq> x \<and> dist x' x < d"
   1.685 -      using `y \<in> S` `y \<noteq> x` `dist y x < d` by fast
   1.686 -  }
   1.687 -  then show ?thesis unfolding islimpt_approachable by blast
   1.688 -qed
   1.689 -
   1.690 -lemma interior_closed_Un_empty_interior:
   1.691 -  assumes cS: "closed S" and iT: "interior T = {}"
   1.692 -  shows "interior(S \<union> T) = interior S"
   1.693 -proof
   1.694 -  show "interior S \<subseteq> interior (S\<union>T)"
   1.695 -    by (rule subset_interior, blast)
   1.696 -next
   1.697 -  show "interior (S \<union> T) \<subseteq> interior S"
   1.698 -  proof
   1.699 -    fix x assume "x \<in> interior (S \<union> T)"
   1.700 -    then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T"
   1.701 -      unfolding interior_def by fast
   1.702 -    show "x \<in> interior S"
   1.703 -    proof (rule ccontr)
   1.704 -      assume "x \<notin> interior S"
   1.705 -      with `x \<in> R` `open R` obtain y where "y \<in> R - S"
   1.706 -        unfolding interior_def expand_set_eq by fast
   1.707 -      from `open R` `closed S` have "open (R - S)" by (rule open_Diff)
   1.708 -      from `R \<subseteq> S \<union> T` have "R - S \<subseteq> T" by fast
   1.709 -      from `y \<in> R - S` `open (R - S)` `R - S \<subseteq> T` `interior T = {}`
   1.710 -      show "False" unfolding interior_def by fast
   1.711 -    qed
   1.712 -  qed
   1.713 -qed
   1.714 -
   1.715 -
   1.716 -subsection{* Closure of a Set *}
   1.717 -
   1.718 -definition "closure S = S \<union> {x | x. x islimpt S}"
   1.719 -
   1.720 -lemma closure_interior: "closure S = UNIV - interior (UNIV - S)"
   1.721 -proof-
   1.722 -  { fix x
   1.723 -    have "x\<in>UNIV - interior (UNIV - S) \<longleftrightarrow> x \<in> closure S"  (is "?lhs = ?rhs")
   1.724 -    proof
   1.725 -      let ?exT = "\<lambda> y. (\<exists>T. open T \<and> y \<in> T \<and> T \<subseteq> UNIV - S)"
   1.726 -      assume "?lhs"
   1.727 -      hence *:"\<not> ?exT x"
   1.728 -        unfolding interior_def
   1.729 -        by simp
   1.730 -      { assume "\<not> ?rhs"
   1.731 -        hence False using *
   1.732 -          unfolding closure_def islimpt_def
   1.733 -          by blast
   1.734 -      }
   1.735 -      thus "?rhs"
   1.736 -        by blast
   1.737 -    next
   1.738 -      assume "?rhs" thus "?lhs"
   1.739 -        unfolding closure_def interior_def islimpt_def
   1.740 -        by blast
   1.741 -    qed
   1.742 -  }
   1.743 -  thus ?thesis
   1.744 -    by blast
   1.745 -qed
   1.746 -
   1.747 -lemma interior_closure: "interior S = UNIV - (closure (UNIV - S))"
   1.748 -proof-
   1.749 -  { fix x
   1.750 -    have "x \<in> interior S \<longleftrightarrow> x \<in> UNIV - (closure (UNIV - S))"
   1.751 -      unfolding interior_def closure_def islimpt_def
   1.752 -      by blast (* FIXME: VERY slow! *)
   1.753 -  }
   1.754 -  thus ?thesis
   1.755 -    by blast
   1.756 -qed
   1.757 -
   1.758 -lemma closed_closure[simp, intro]: "closed (closure S)"
   1.759 -proof-
   1.760 -  have "closed (UNIV - interior (UNIV -S))" by blast
   1.761 -  thus ?thesis using closure_interior[of S] by simp
   1.762 -qed
   1.763 -
   1.764 -lemma closure_hull: "closure S = closed hull S"
   1.765 -proof-
   1.766 -  have "S \<subseteq> closure S"
   1.767 -    unfolding closure_def
   1.768 -    by blast
   1.769 -  moreover
   1.770 -  have "closed (closure S)"
   1.771 -    using closed_closure[of S]
   1.772 -    by assumption
   1.773 -  moreover
   1.774 -  { fix t
   1.775 -    assume *:"S \<subseteq> t" "closed t"
   1.776 -    { fix x
   1.777 -      assume "x islimpt S"
   1.778 -      hence "x islimpt t" using *(1)
   1.779 -        using islimpt_subset[of x, of S, of t]
   1.780 -        by blast
   1.781 -    }
   1.782 -    with * have "closure S \<subseteq> t"
   1.783 -      unfolding closure_def
   1.784 -      using closed_limpt[of t]
   1.785 -      by auto
   1.786 -  }
   1.787 -  ultimately show ?thesis
   1.788 -    using hull_unique[of S, of "closure S", of closed]
   1.789 -    unfolding mem_def
   1.790 -    by simp
   1.791 -qed
   1.792 -
   1.793 -lemma closure_eq: "closure S = S \<longleftrightarrow> closed S"
   1.794 -  unfolding closure_hull
   1.795 -  using hull_eq[of closed, unfolded mem_def, OF  closed_Inter, of S]
   1.796 -  by (metis mem_def subset_eq)
   1.797 -
   1.798 -lemma closure_closed[simp]: "closed S \<Longrightarrow> closure S = S"
   1.799 -  using closure_eq[of S]
   1.800 -  by simp
   1.801 -
   1.802 -lemma closure_closure[simp]: "closure (closure S) = closure S"
   1.803 -  unfolding closure_hull
   1.804 -  using hull_hull[of closed S]
   1.805 -  by assumption
   1.806 -
   1.807 -lemma closure_subset: "S \<subseteq> closure S"
   1.808 -  unfolding closure_hull
   1.809 -  using hull_subset[of S closed]
   1.810 -  by assumption
   1.811 -
   1.812 -lemma subset_closure: "S \<subseteq> T \<Longrightarrow> closure S \<subseteq> closure T"
   1.813 -  unfolding closure_hull
   1.814 -  using hull_mono[of S T closed]
   1.815 -  by assumption
   1.816 -
   1.817 -lemma closure_minimal: "S \<subseteq> T \<Longrightarrow>  closed T \<Longrightarrow> closure S \<subseteq> T"
   1.818 -  using hull_minimal[of S T closed]
   1.819 -  unfolding closure_hull mem_def
   1.820 -  by simp
   1.821 -
   1.822 -lemma closure_unique: "S \<subseteq> T \<and> closed T \<and> (\<forall> T'. S \<subseteq> T' \<and> closed T' \<longrightarrow> T \<subseteq> T') \<Longrightarrow> closure S = T"
   1.823 -  using hull_unique[of S T closed]
   1.824 -  unfolding closure_hull mem_def
   1.825 -  by simp
   1.826 -
   1.827 -lemma closure_empty[simp]: "closure {} = {}"
   1.828 -  using closed_empty closure_closed[of "{}"]
   1.829 -  by simp
   1.830 -
   1.831 -lemma closure_univ[simp]: "closure UNIV = UNIV"
   1.832 -  using closure_closed[of UNIV]
   1.833 -  by simp
   1.834 -
   1.835 -lemma closure_eq_empty: "closure S = {} \<longleftrightarrow> S = {}"
   1.836 -  using closure_empty closure_subset[of S]
   1.837 -  by blast
   1.838 -
   1.839 -lemma closure_subset_eq: "closure S \<subseteq> S \<longleftrightarrow> closed S"
   1.840 -  using closure_eq[of S] closure_subset[of S]
   1.841 -  by simp
   1.842 -
   1.843 -lemma open_inter_closure_eq_empty:
   1.844 -  "open S \<Longrightarrow> (S \<inter> closure T) = {} \<longleftrightarrow> S \<inter> T = {}"
   1.845 -  using open_subset_interior[of S "UNIV - T"]
   1.846 -  using interior_subset[of "UNIV - T"]
   1.847 -  unfolding closure_interior
   1.848 -  by auto
   1.849 -
   1.850 -lemma open_inter_closure_subset:
   1.851 -  "open S \<Longrightarrow> (S \<inter> (closure T)) \<subseteq> closure(S \<inter> T)"
   1.852 -proof
   1.853 -  fix x
   1.854 -  assume as: "open S" "x \<in> S \<inter> closure T"
   1.855 -  { assume *:"x islimpt T"
   1.856 -    have "x islimpt (S \<inter> T)"
   1.857 -    proof (rule islimptI)
   1.858 -      fix A
   1.859 -      assume "x \<in> A" "open A"
   1.860 -      with as have "x \<in> A \<inter> S" "open (A \<inter> S)"
   1.861 -        by (simp_all add: open_Int)
   1.862 -      with * obtain y where "y \<in> T" "y \<in> A \<inter> S" "y \<noteq> x"
   1.863 -        by (rule islimptE)
   1.864 -      hence "y \<in> S \<inter> T" "y \<in> A \<and> y \<noteq> x"
   1.865 -        by simp_all
   1.866 -      thus "\<exists>y\<in>(S \<inter> T). y \<in> A \<and> y \<noteq> x" ..
   1.867 -    qed
   1.868 -  }
   1.869 -  then show "x \<in> closure (S \<inter> T)" using as
   1.870 -    unfolding closure_def
   1.871 -    by blast
   1.872 -qed
   1.873 -
   1.874 -lemma closure_complement: "closure(UNIV - S) = UNIV - interior(S)"
   1.875 -proof-
   1.876 -  have "S = UNIV - (UNIV - S)"
   1.877 -    by auto
   1.878 -  thus ?thesis
   1.879 -    unfolding closure_interior
   1.880 -    by auto
   1.881 -qed
   1.882 -
   1.883 -lemma interior_complement: "interior(UNIV - S) = UNIV - closure(S)"
   1.884 -  unfolding closure_interior
   1.885 -  by blast
   1.886 -
   1.887 -subsection{* Frontier (aka boundary) *}
   1.888 -
   1.889 -definition "frontier S = closure S - interior S"
   1.890 -
   1.891 -lemma frontier_closed: "closed(frontier S)"
   1.892 -  by (simp add: frontier_def closed_Diff)
   1.893 -
   1.894 -lemma frontier_closures: "frontier S = (closure S) \<inter> (closure(UNIV - S))"
   1.895 -  by (auto simp add: frontier_def interior_closure)
   1.896 -
   1.897 -lemma frontier_straddle:
   1.898 -  fixes a :: "'a::metric_space"
   1.899 -  shows "a \<in> frontier S \<longleftrightarrow> (\<forall>e>0. (\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e))" (is "?lhs \<longleftrightarrow> ?rhs")
   1.900 -proof
   1.901 -  assume "?lhs"
   1.902 -  { fix e::real
   1.903 -    assume "e > 0"
   1.904 -    let ?rhse = "(\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e)"
   1.905 -    { assume "a\<in>S"
   1.906 -      have "\<exists>x\<in>S. dist a x < e" using `e>0` `a\<in>S` by(rule_tac x=a in bexI) auto
   1.907 -      moreover have "\<exists>x. x \<notin> S \<and> dist a x < e" using `?lhs` `a\<in>S`
   1.908 -        unfolding frontier_closures closure_def islimpt_def using `e>0`
   1.909 -        by (auto, erule_tac x="ball a e" in allE, auto)
   1.910 -      ultimately have ?rhse by auto
   1.911 -    }
   1.912 -    moreover
   1.913 -    { assume "a\<notin>S"
   1.914 -      hence ?rhse using `?lhs`
   1.915 -        unfolding frontier_closures closure_def islimpt_def
   1.916 -        using open_ball[of a e] `e > 0`
   1.917 -        by (auto, erule_tac x = "ball a e" in allE, auto) (* FIXME: VERY slow! *)
   1.918 -    }
   1.919 -    ultimately have ?rhse by auto
   1.920 -  }
   1.921 -  thus ?rhs by auto
   1.922 -next
   1.923 -  assume ?rhs
   1.924 -  moreover
   1.925 -  { fix T assume "a\<notin>S" and
   1.926 -    as:"\<forall>e>0. (\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e)" "a \<notin> S" "a \<in> T" "open T"
   1.927 -    from `open T` `a \<in> T` have "\<exists>e>0. ball a e \<subseteq> T" unfolding open_contains_ball[of T] by auto
   1.928 -    then obtain e where "e>0" "ball a e \<subseteq> T" by auto
   1.929 -    then obtain y where y:"y\<in>S" "dist a y < e"  using as(1) by auto
   1.930 -    have "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> a"
   1.931 -      using `dist a y < e` `ball a e \<subseteq> T` unfolding ball_def using `y\<in>S` `a\<notin>S` by auto
   1.932 -  }
   1.933 -  hence "a \<in> closure S" unfolding closure_def islimpt_def using `?rhs` by auto
   1.934 -  moreover
   1.935 -  { fix T assume "a \<in> T"  "open T" "a\<in>S"
   1.936 -    then obtain e where "e>0" and balle: "ball a e \<subseteq> T" unfolding open_contains_ball using `?rhs` by auto
   1.937 -    obtain x where "x \<notin> S" "dist a x < e" using `?rhs` using `e>0` by auto
   1.938 -    hence "\<exists>y\<in>UNIV - S. y \<in> T \<and> y \<noteq> a" using balle `a\<in>S` unfolding ball_def by (rule_tac x=x in bexI)auto
   1.939 -  }
   1.940 -  hence "a islimpt (UNIV - S) \<or> a\<notin>S" unfolding islimpt_def by auto
   1.941 -  ultimately show ?lhs unfolding frontier_closures using closure_def[of "UNIV - S"] by auto
   1.942 -qed
   1.943 -
   1.944 -lemma frontier_subset_closed: "closed S \<Longrightarrow> frontier S \<subseteq> S"
   1.945 -  by (metis frontier_def closure_closed Diff_subset)
   1.946 -
   1.947 -lemma frontier_empty: "frontier {} = {}"
   1.948 -  by (simp add: frontier_def closure_empty)
   1.949 -
   1.950 -lemma frontier_subset_eq: "frontier S \<subseteq> S \<longleftrightarrow> closed S"
   1.951 -proof-
   1.952 -  { assume "frontier S \<subseteq> S"
   1.953 -    hence "closure S \<subseteq> S" using interior_subset unfolding frontier_def by auto
   1.954 -    hence "closed S" using closure_subset_eq by auto
   1.955 -  }
   1.956 -  thus ?thesis using frontier_subset_closed[of S] by auto
   1.957 -qed
   1.958 -
   1.959 -lemma frontier_complement: "frontier(UNIV - S) = frontier S"
   1.960 -  by (auto simp add: frontier_def closure_complement interior_complement)
   1.961 -
   1.962 -lemma frontier_disjoint_eq: "frontier S \<inter> S = {} \<longleftrightarrow> open S"
   1.963 -  using frontier_complement frontier_subset_eq[of "UNIV - S"]
   1.964 -  unfolding open_closed Compl_eq_Diff_UNIV by auto
   1.965 -
   1.966 -subsection{* Common nets and The "within" modifier for nets. *}
   1.967 -
   1.968 -definition
   1.969 -  at_infinity :: "'a::real_normed_vector net" where
   1.970 -  "at_infinity = Abs_net (range (\<lambda>r. {x. r \<le> norm x}))"
   1.971 -
   1.972 -definition
   1.973 -  indirection :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> 'a net" (infixr "indirection" 70) where
   1.974 -  "a indirection v = (at a) within {b. \<exists>c\<ge>0. b - a = scaleR c v}"
   1.975 -
   1.976 -text{* Prove That They are all nets. *}
   1.977 -
   1.978 -lemma Rep_net_at_infinity:
   1.979 -  "Rep_net at_infinity = range (\<lambda>r. {x. r \<le> norm x})"
   1.980 -unfolding at_infinity_def
   1.981 -apply (rule Abs_net_inverse')
   1.982 -apply (rule image_nonempty, simp)
   1.983 -apply (clarsimp, rename_tac r s)
   1.984 -apply (rule_tac x="max r s" in exI, auto)
   1.985 -done
   1.986 -
   1.987 -lemma within_UNIV: "net within UNIV = net"
   1.988 -  by (simp add: Rep_net_inject [symmetric] Rep_net_within)
   1.989 -
   1.990 -subsection{* Identify Trivial limits, where we can't approach arbitrarily closely. *}
   1.991 -
   1.992 -definition
   1.993 -  trivial_limit :: "'a net \<Rightarrow> bool" where
   1.994 -  "trivial_limit net \<longleftrightarrow> {} \<in> Rep_net net"
   1.995 -
   1.996 -lemma trivial_limit_within:
   1.997 -  shows "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"
   1.998 -proof
   1.999 -  assume "trivial_limit (at a within S)"
  1.1000 -  thus "\<not> a islimpt S"
  1.1001 -    unfolding trivial_limit_def
  1.1002 -    unfolding Rep_net_within Rep_net_at
  1.1003 -    unfolding islimpt_def
  1.1004 -    apply (clarsimp simp add: expand_set_eq)
  1.1005 -    apply (rename_tac T, rule_tac x=T in exI)
  1.1006 -    apply (clarsimp, drule_tac x=y in spec, simp)
  1.1007 -    done
  1.1008 -next
  1.1009 -  assume "\<not> a islimpt S"
  1.1010 -  thus "trivial_limit (at a within S)"
  1.1011 -    unfolding trivial_limit_def
  1.1012 -    unfolding Rep_net_within Rep_net_at
  1.1013 -    unfolding islimpt_def
  1.1014 -    apply (clarsimp simp add: image_image)
  1.1015 -    apply (rule_tac x=T in image_eqI)
  1.1016 -    apply (auto simp add: expand_set_eq)
  1.1017 -    done
  1.1018 -qed
  1.1019 -
  1.1020 -lemma trivial_limit_at_iff: "trivial_limit (at a) \<longleftrightarrow> \<not> a islimpt UNIV"
  1.1021 -  using trivial_limit_within [of a UNIV]
  1.1022 -  by (simp add: within_UNIV)
  1.1023 -
  1.1024 -lemma trivial_limit_at:
  1.1025 -  fixes a :: "'a::perfect_space"
  1.1026 -  shows "\<not> trivial_limit (at a)"
  1.1027 -  by (simp add: trivial_limit_at_iff)
  1.1028 -
  1.1029 -lemma trivial_limit_at_infinity:
  1.1030 -  "\<not> trivial_limit (at_infinity :: ('a::{real_normed_vector,zero_neq_one}) net)"
  1.1031 -  (* FIXME: find a more appropriate type class *)
  1.1032 -  unfolding trivial_limit_def Rep_net_at_infinity
  1.1033 -  apply (clarsimp simp add: expand_set_eq)
  1.1034 -  apply (drule_tac x="scaleR r (sgn 1)" in spec)
  1.1035 -  apply (simp add: norm_sgn)
  1.1036 -  done
  1.1037 -
  1.1038 -lemma trivial_limit_sequentially: "\<not> trivial_limit sequentially"
  1.1039 -  by (auto simp add: trivial_limit_def Rep_net_sequentially)
  1.1040 -
  1.1041 -subsection{* Some property holds "sufficiently close" to the limit point. *}
  1.1042 -
  1.1043 -lemma eventually_at: (* FIXME: this replaces Limits.eventually_at *)
  1.1044 -  "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"
  1.1045 -unfolding eventually_at dist_nz by auto
  1.1046 -
  1.1047 -lemma eventually_at_infinity:
  1.1048 -  "eventually P at_infinity \<longleftrightarrow> (\<exists>b. \<forall>x. norm x >= b \<longrightarrow> P x)"
  1.1049 -unfolding eventually_def Rep_net_at_infinity by auto
  1.1050 -
  1.1051 -lemma eventually_within: "eventually P (at a within S) \<longleftrightarrow>
  1.1052 -        (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"
  1.1053 -unfolding eventually_within eventually_at dist_nz by auto
  1.1054 -
  1.1055 -lemma eventually_within_le: "eventually P (at a within S) \<longleftrightarrow>
  1.1056 -        (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a <= d \<longrightarrow> P x)" (is "?lhs = ?rhs")
  1.1057 -unfolding eventually_within
  1.1058 -apply safe
  1.1059 -apply (rule_tac x="d/2" in exI, simp)
  1.1060 -apply (rule_tac x="d" in exI, simp)
  1.1061 -done
  1.1062 -
  1.1063 -lemma eventually_happens: "eventually P net ==> trivial_limit net \<or> (\<exists>x. P x)"
  1.1064 -  unfolding eventually_def trivial_limit_def
  1.1065 -  using Rep_net_nonempty [of net] by auto
  1.1066 -
  1.1067 -lemma always_eventually: "(\<forall>x. P x) ==> eventually P net"
  1.1068 -  unfolding eventually_def trivial_limit_def
  1.1069 -  using Rep_net_nonempty [of net] by auto
  1.1070 -
  1.1071 -lemma trivial_limit_eventually: "trivial_limit net \<Longrightarrow> eventually P net"
  1.1072 -  unfolding trivial_limit_def eventually_def by auto
  1.1073 -
  1.1074 -lemma eventually_False: "eventually (\<lambda>x. False) net \<longleftrightarrow> trivial_limit net"
  1.1075 -  unfolding trivial_limit_def eventually_def by auto
  1.1076 -
  1.1077 -lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"
  1.1078 -  apply (safe elim!: trivial_limit_eventually)
  1.1079 -  apply (simp add: eventually_False [symmetric])
  1.1080 -  done
  1.1081 -
  1.1082 -text{* Combining theorems for "eventually" *}
  1.1083 -
  1.1084 -lemma eventually_conjI:
  1.1085 -  "\<lbrakk>eventually (\<lambda>x. P x) net; eventually (\<lambda>x. Q x) net\<rbrakk>
  1.1086 -    \<Longrightarrow> eventually (\<lambda>x. P x \<and> Q x) net"
  1.1087 -by (rule eventually_conj)
  1.1088 -
  1.1089 -lemma eventually_rev_mono:
  1.1090 -  "eventually P net \<Longrightarrow> (\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually Q net"
  1.1091 -using eventually_mono [of P Q] by fast
  1.1092 -
  1.1093 -lemma eventually_and: " eventually (\<lambda>x. P x \<and> Q x) net \<longleftrightarrow> eventually P net \<and> eventually Q net"
  1.1094 -  by (auto intro!: eventually_conjI elim: eventually_rev_mono)
  1.1095 -
  1.1096 -lemma eventually_false: "eventually (\<lambda>x. False) net \<longleftrightarrow> trivial_limit net"
  1.1097 -  by (auto simp add: eventually_False)
  1.1098 -
  1.1099 -lemma not_eventually: "(\<forall>x. \<not> P x ) \<Longrightarrow> ~(trivial_limit net) ==> ~(eventually (\<lambda>x. P x) net)"
  1.1100 -  by (simp add: eventually_False)
  1.1101 -
  1.1102 -subsection{* Limits, defined as vacuously true when the limit is trivial. *}
  1.1103 -
  1.1104 -  text{* Notation Lim to avoid collition with lim defined in analysis *}
  1.1105 -definition
  1.1106 -  Lim :: "'a net \<Rightarrow> ('a \<Rightarrow> 'b::t2_space) \<Rightarrow> 'b" where
  1.1107 -  "Lim net f = (THE l. (f ---> l) net)"
  1.1108 -
  1.1109 -lemma Lim:
  1.1110 - "(f ---> l) net \<longleftrightarrow>
  1.1111 -        trivial_limit net \<or>
  1.1112 -        (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"
  1.1113 -  unfolding tendsto_iff trivial_limit_eq by auto
  1.1114 -
  1.1115 -
  1.1116 -text{* Show that they yield usual definitions in the various cases. *}
  1.1117 -
  1.1118 -lemma Lim_within_le: "(f ---> l)(at a within S) \<longleftrightarrow>
  1.1119 -           (\<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)"
  1.1120 -  by (auto simp add: tendsto_iff eventually_within_le)
  1.1121 -
  1.1122 -lemma Lim_within: "(f ---> l) (at a within S) \<longleftrightarrow>
  1.1123 -        (\<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)"
  1.1124 -  by (auto simp add: tendsto_iff eventually_within)
  1.1125 -
  1.1126 -lemma Lim_at: "(f ---> l) (at a) \<longleftrightarrow>
  1.1127 -        (\<forall>e >0. \<exists>d>0. \<forall>x. 0 < dist x a  \<and> dist x a  < d  \<longrightarrow> dist (f x) l < e)"
  1.1128 -  by (auto simp add: tendsto_iff eventually_at)
  1.1129 -
  1.1130 -lemma Lim_at_iff_LIM: "(f ---> l) (at a) \<longleftrightarrow> f -- a --> l"
  1.1131 -  unfolding Lim_at LIM_def by (simp only: zero_less_dist_iff)
  1.1132 -
  1.1133 -lemma Lim_at_infinity:
  1.1134 -  "(f ---> l) at_infinity \<longleftrightarrow> (\<forall>e>0. \<exists>b. \<forall>x. norm x >= b \<longrightarrow> dist (f x) l < e)"
  1.1135 -  by (auto simp add: tendsto_iff eventually_at_infinity)
  1.1136 -
  1.1137 -lemma Lim_sequentially:
  1.1138 - "(S ---> l) sequentially \<longleftrightarrow>
  1.1139 -          (\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (S n) l < e)"
  1.1140 -  by (auto simp add: tendsto_iff eventually_sequentially)
  1.1141 -
  1.1142 -lemma Lim_sequentially_iff_LIMSEQ: "(S ---> l) sequentially \<longleftrightarrow> S ----> l"
  1.1143 -  unfolding Lim_sequentially LIMSEQ_def ..
  1.1144 -
  1.1145 -lemma Lim_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> (f ---> l) net"
  1.1146 -  by (rule topological_tendstoI, auto elim: eventually_rev_mono)
  1.1147 -
  1.1148 -text{* The expected monotonicity property. *}
  1.1149 -
  1.1150 -lemma Lim_within_empty: "(f ---> l) (net within {})"
  1.1151 -  unfolding tendsto_def Limits.eventually_within by simp
  1.1152 -
  1.1153 -lemma Lim_within_subset: "(f ---> l) (net within S) \<Longrightarrow> T \<subseteq> S \<Longrightarrow> (f ---> l) (net within T)"
  1.1154 -  unfolding tendsto_def Limits.eventually_within
  1.1155 -  by (auto elim!: eventually_elim1)
  1.1156 -
  1.1157 -lemma Lim_Un: assumes "(f ---> l) (net within S)" "(f ---> l) (net within T)"
  1.1158 -  shows "(f ---> l) (net within (S \<union> T))"
  1.1159 -  using assms unfolding tendsto_def Limits.eventually_within
  1.1160 -  apply clarify
  1.1161 -  apply (drule spec, drule (1) mp, drule (1) mp)
  1.1162 -  apply (drule spec, drule (1) mp, drule (1) mp)
  1.1163 -  apply (auto elim: eventually_elim2)
  1.1164 -  done
  1.1165 -
  1.1166 -lemma Lim_Un_univ:
  1.1167 - "(f ---> l) (net within S) \<Longrightarrow> (f ---> l) (net within T) \<Longrightarrow>  S \<union> T = UNIV
  1.1168 -        ==> (f ---> l) net"
  1.1169 -  by (metis Lim_Un within_UNIV)
  1.1170 -
  1.1171 -text{* Interrelations between restricted and unrestricted limits. *}
  1.1172 -
  1.1173 -lemma Lim_at_within: "(f ---> l) net ==> (f ---> l)(net within S)"
  1.1174 -  (* FIXME: rename *)
  1.1175 -  unfolding tendsto_def Limits.eventually_within
  1.1176 -  apply (clarify, drule spec, drule (1) mp, drule (1) mp)
  1.1177 -  by (auto elim!: eventually_elim1)
  1.1178 -
  1.1179 -lemma Lim_within_open:
  1.1180 -  fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
  1.1181 -  assumes"a \<in> S" "open S"
  1.1182 -  shows "(f ---> l)(at a within S) \<longleftrightarrow> (f ---> l)(at a)" (is "?lhs \<longleftrightarrow> ?rhs")
  1.1183 -proof
  1.1184 -  assume ?lhs
  1.1185 -  { fix A assume "open A" "l \<in> A"
  1.1186 -    with `?lhs` have "eventually (\<lambda>x. f x \<in> A) (at a within S)"
  1.1187 -      by (rule topological_tendstoD)
  1.1188 -    hence "eventually (\<lambda>x. x \<in> S \<longrightarrow> f x \<in> A) (at a)"
  1.1189 -      unfolding Limits.eventually_within .
  1.1190 -    then obtain T where "open T" "a \<in> T" "\<forall>x\<in>T. x \<noteq> a \<longrightarrow> x \<in> S \<longrightarrow> f x \<in> A"
  1.1191 -      unfolding eventually_at_topological by fast
  1.1192 -    hence "open (T \<inter> S)" "a \<in> T \<inter> S" "\<forall>x\<in>(T \<inter> S). x \<noteq> a \<longrightarrow> f x \<in> A"
  1.1193 -      using assms by auto
  1.1194 -    hence "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. x \<noteq> a \<longrightarrow> f x \<in> A)"
  1.1195 -      by fast
  1.1196 -    hence "eventually (\<lambda>x. f x \<in> A) (at a)"
  1.1197 -      unfolding eventually_at_topological .
  1.1198 -  }
  1.1199 -  thus ?rhs by (rule topological_tendstoI)
  1.1200 -next
  1.1201 -  assume ?rhs
  1.1202 -  thus ?lhs by (rule Lim_at_within)
  1.1203 -qed
  1.1204 -
  1.1205 -text{* Another limit point characterization. *}
  1.1206 -
  1.1207 -lemma islimpt_sequential:
  1.1208 -  fixes x :: "'a::metric_space" (* FIXME: generalize to topological_space *)
  1.1209 -  shows "x islimpt S \<longleftrightarrow> (\<exists>f. (\<forall>n::nat. f n \<in> S -{x}) \<and> (f ---> x) sequentially)"
  1.1210 -    (is "?lhs = ?rhs")
  1.1211 -proof
  1.1212 -  assume ?lhs
  1.1213 -  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"
  1.1214 -    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
  1.1215 -  { fix n::nat
  1.1216 -    have "f (inverse (real n + 1)) \<in> S - {x}" using f by auto
  1.1217 -  }
  1.1218 -  moreover
  1.1219 -  { fix e::real assume "e>0"
  1.1220 -    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
  1.1221 -    then obtain N::nat where "inverse (real (N + 1)) < e" by auto
  1.1222 -    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)
  1.1223 -    moreover have "\<forall>n\<ge>N. dist (f (inverse (real n + 1))) x < (inverse (real n + 1))" using f `e>0` by auto
  1.1224 -    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
  1.1225 -  }
  1.1226 -  hence " ((\<lambda>n. f (inverse (real n + 1))) ---> x) sequentially"
  1.1227 -    unfolding Lim_sequentially using f by auto
  1.1228 -  ultimately show ?rhs apply (rule_tac x="(\<lambda>n::nat. f (inverse (real n + 1)))" in exI) by auto
  1.1229 -next
  1.1230 -  assume ?rhs
  1.1231 -  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
  1.1232 -  { fix e::real assume "e>0"
  1.1233 -    then obtain N where "dist (f N) x < e" using f(2) by auto
  1.1234 -    moreover have "f N\<in>S" "f N \<noteq> x" using f(1) by auto
  1.1235 -    ultimately have "\<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e" by auto
  1.1236 -  }
  1.1237 -  thus ?lhs unfolding islimpt_approachable by auto
  1.1238 -qed
  1.1239 -
  1.1240 -text{* Basic arithmetical combining theorems for limits. *}
  1.1241 -
  1.1242 -lemma Lim_linear:
  1.1243 -  assumes "(f ---> l) net" "bounded_linear h"
  1.1244 -  shows "((\<lambda>x. h (f x)) ---> h l) net"
  1.1245 -using `bounded_linear h` `(f ---> l) net`
  1.1246 -by (rule bounded_linear.tendsto)
  1.1247 -
  1.1248 -lemma Lim_ident_at: "((\<lambda>x. x) ---> a) (at a)"
  1.1249 -  unfolding tendsto_def Limits.eventually_at_topological by fast
  1.1250 -
  1.1251 -lemma Lim_const: "((\<lambda>x. a) ---> a) net"
  1.1252 -  by (rule tendsto_const)
  1.1253 -
  1.1254 -lemma Lim_cmul:
  1.1255 -  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1.1256 -  shows "(f ---> l) net ==> ((\<lambda>x. c *\<^sub>R f x) ---> c *\<^sub>R l) net"
  1.1257 -  by (intro tendsto_intros)
  1.1258 -
  1.1259 -lemma Lim_neg:
  1.1260 -  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1.1261 -  shows "(f ---> l) net ==> ((\<lambda>x. -(f x)) ---> -l) net"
  1.1262 -  by (rule tendsto_minus)
  1.1263 -
  1.1264 -lemma Lim_add: fixes f :: "'a \<Rightarrow> 'b::real_normed_vector" shows
  1.1265 - "(f ---> l) net \<Longrightarrow> (g ---> m) net \<Longrightarrow> ((\<lambda>x. f(x) + g(x)) ---> l + m) net"
  1.1266 -  by (rule tendsto_add)
  1.1267 -
  1.1268 -lemma Lim_sub:
  1.1269 -  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1.1270 -  shows "(f ---> l) net \<Longrightarrow> (g ---> m) net \<Longrightarrow> ((\<lambda>x. f(x) - g(x)) ---> l - m) net"
  1.1271 -  by (rule tendsto_diff)
  1.1272 -
  1.1273 -lemma Lim_null:
  1.1274 -  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1.1275 -  shows "(f ---> l) net \<longleftrightarrow> ((\<lambda>x. f(x) - l) ---> 0) net" by (simp add: Lim dist_norm)
  1.1276 -
  1.1277 -lemma Lim_null_norm:
  1.1278 -  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1.1279 -  shows "(f ---> 0) net \<longleftrightarrow> ((\<lambda>x. norm(f x)) ---> 0) net"
  1.1280 -  by (simp add: Lim dist_norm)
  1.1281 -
  1.1282 -lemma Lim_null_comparison:
  1.1283 -  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1.1284 -  assumes "eventually (\<lambda>x. norm (f x) \<le> g x) net" "(g ---> 0) net"
  1.1285 -  shows "(f ---> 0) net"
  1.1286 -proof(simp add: tendsto_iff, rule+)
  1.1287 -  fix e::real assume "0<e"
  1.1288 -  { fix x
  1.1289 -    assume "norm (f x) \<le> g x" "dist (g x) 0 < e"
  1.1290 -    hence "dist (f x) 0 < e" by (simp add: dist_norm)
  1.1291 -  }
  1.1292 -  thus "eventually (\<lambda>x. dist (f x) 0 < e) net"
  1.1293 -    using eventually_and[of "\<lambda>x. norm(f x) <= g x" "\<lambda>x. dist (g x) 0 < e" net]
  1.1294 -    using eventually_mono[of "(\<lambda>x. norm (f x) \<le> g x \<and> dist (g x) 0 < e)" "(\<lambda>x. dist (f x) 0 < e)" net]
  1.1295 -    using assms `e>0` unfolding tendsto_iff by auto
  1.1296 -qed
  1.1297 -
  1.1298 -lemma Lim_component:
  1.1299 -  fixes f :: "'a \<Rightarrow> 'b::metric_space ^ 'n::finite"
  1.1300 -  shows "(f ---> l) net \<Longrightarrow> ((\<lambda>a. f a $i) ---> l$i) net"
  1.1301 -  unfolding tendsto_iff
  1.1302 -  apply (clarify)
  1.1303 -  apply (drule spec, drule (1) mp)
  1.1304 -  apply (erule eventually_elim1)
  1.1305 -  apply (erule le_less_trans [OF dist_nth_le])
  1.1306 -  done
  1.1307 -
  1.1308 -lemma Lim_transform_bound:
  1.1309 -  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1.1310 -  fixes g :: "'a \<Rightarrow> 'c::real_normed_vector"
  1.1311 -  assumes "eventually (\<lambda>n. norm(f n) <= norm(g n)) net"  "(g ---> 0) net"
  1.1312 -  shows "(f ---> 0) net"
  1.1313 -proof (rule tendstoI)
  1.1314 -  fix e::real assume "e>0"
  1.1315 -  { fix x
  1.1316 -    assume "norm (f x) \<le> norm (g x)" "dist (g x) 0 < e"
  1.1317 -    hence "dist (f x) 0 < e" by (simp add: dist_norm)}
  1.1318 -  thus "eventually (\<lambda>x. dist (f x) 0 < e) net"
  1.1319 -    using eventually_and[of "\<lambda>x. norm (f x) \<le> norm (g x)" "\<lambda>x. dist (g x) 0 < e" net]
  1.1320 -    using eventually_mono[of "\<lambda>x. norm (f x) \<le> norm (g x) \<and> dist (g x) 0 < e" "\<lambda>x. dist (f x) 0 < e" net]
  1.1321 -    using assms `e>0` unfolding tendsto_iff by blast
  1.1322 -qed
  1.1323 -
  1.1324 -text{* Deducing things about the limit from the elements. *}
  1.1325 -
  1.1326 -lemma Lim_in_closed_set:
  1.1327 -  assumes "closed S" "eventually (\<lambda>x. f(x) \<in> S) net" "\<not>(trivial_limit net)" "(f ---> l) net"
  1.1328 -  shows "l \<in> S"
  1.1329 -proof (rule ccontr)
  1.1330 -  assume "l \<notin> S"
  1.1331 -  with `closed S` have "open (- S)" "l \<in> - S"
  1.1332 -    by (simp_all add: open_Compl)
  1.1333 -  with assms(4) have "eventually (\<lambda>x. f x \<in> - S) net"
  1.1334 -    by (rule topological_tendstoD)
  1.1335 -  with assms(2) have "eventually (\<lambda>x. False) net"
  1.1336 -    by (rule eventually_elim2) simp
  1.1337 -  with assms(3) show "False"
  1.1338 -    by (simp add: eventually_False)
  1.1339 -qed
  1.1340 -
  1.1341 -text{* Need to prove closed(cball(x,e)) before deducing this as a corollary. *}
  1.1342 -
  1.1343 -lemma Lim_dist_ubound:
  1.1344 -  assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. dist a (f x) <= e) net"
  1.1345 -  shows "dist a l <= e"
  1.1346 -proof (rule ccontr)
  1.1347 -  assume "\<not> dist a l \<le> e"
  1.1348 -  then have "0 < dist a l - e" by simp
  1.1349 -  with assms(2) have "eventually (\<lambda>x. dist (f x) l < dist a l - e) net"
  1.1350 -    by (rule tendstoD)
  1.1351 -  with assms(3) have "eventually (\<lambda>x. dist a (f x) \<le> e \<and> dist (f x) l < dist a l - e) net"
  1.1352 -    by (rule eventually_conjI)
  1.1353 -  then obtain w where "dist a (f w) \<le> e" "dist (f w) l < dist a l - e"
  1.1354 -    using assms(1) eventually_happens by auto
  1.1355 -  hence "dist a (f w) + dist (f w) l < e + (dist a l - e)"
  1.1356 -    by (rule add_le_less_mono)
  1.1357 -  hence "dist a (f w) + dist (f w) l < dist a l"
  1.1358 -    by simp
  1.1359 -  also have "\<dots> \<le> dist a (f w) + dist (f w) l"
  1.1360 -    by (rule dist_triangle)
  1.1361 -  finally show False by simp
  1.1362 -qed
  1.1363 -
  1.1364 -lemma Lim_norm_ubound:
  1.1365 -  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1.1366 -  assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. norm(f x) <= e) net"
  1.1367 -  shows "norm(l) <= e"
  1.1368 -proof (rule ccontr)
  1.1369 -  assume "\<not> norm l \<le> e"
  1.1370 -  then have "0 < norm l - e" by simp
  1.1371 -  with assms(2) have "eventually (\<lambda>x. dist (f x) l < norm l - e) net"
  1.1372 -    by (rule tendstoD)
  1.1373 -  with assms(3) have "eventually (\<lambda>x. norm (f x) \<le> e \<and> dist (f x) l < norm l - e) net"
  1.1374 -    by (rule eventually_conjI)
  1.1375 -  then obtain w where "norm (f w) \<le> e" "dist (f w) l < norm l - e"
  1.1376 -    using assms(1) eventually_happens by auto
  1.1377 -  hence "norm (f w - l) < norm l - e" "norm (f w) \<le> e" by (simp_all add: dist_norm)
  1.1378 -  hence "norm (f w - l) + norm (f w) < norm l" by simp
  1.1379 -  hence "norm (f w - l - f w) < norm l" by (rule le_less_trans [OF norm_triangle_ineq4])
  1.1380 -  thus False using `\<not> norm l \<le> e` by simp
  1.1381 -qed
  1.1382 -
  1.1383 -lemma Lim_norm_lbound:
  1.1384 -  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1.1385 -  assumes "\<not> (trivial_limit net)"  "(f ---> l) net"  "eventually (\<lambda>x. e <= norm(f x)) net"
  1.1386 -  shows "e \<le> norm l"
  1.1387 -proof (rule ccontr)
  1.1388 -  assume "\<not> e \<le> norm l"
  1.1389 -  then have "0 < e - norm l" by simp
  1.1390 -  with assms(2) have "eventually (\<lambda>x. dist (f x) l < e - norm l) net"
  1.1391 -    by (rule tendstoD)
  1.1392 -  with assms(3) have "eventually (\<lambda>x. e \<le> norm (f x) \<and> dist (f x) l < e - norm l) net"
  1.1393 -    by (rule eventually_conjI)
  1.1394 -  then obtain w where "e \<le> norm (f w)" "dist (f w) l < e - norm l"
  1.1395 -    using assms(1) eventually_happens by auto
  1.1396 -  hence "norm (f w - l) + norm l < e" "e \<le> norm (f w)" by (simp_all add: dist_norm)
  1.1397 -  hence "norm (f w - l) + norm l < norm (f w)" by (rule less_le_trans)
  1.1398 -  hence "norm (f w - l + l) < norm (f w)" by (rule le_less_trans [OF norm_triangle_ineq])
  1.1399 -  thus False by simp
  1.1400 -qed
  1.1401 -
  1.1402 -text{* Uniqueness of the limit, when nontrivial. *}
  1.1403 -
  1.1404 -lemma Lim_unique:
  1.1405 -  fixes f :: "'a \<Rightarrow> 'b::t2_space"
  1.1406 -  assumes "\<not> trivial_limit net"  "(f ---> l) net"  "(f ---> l') net"
  1.1407 -  shows "l = l'"
  1.1408 -proof (rule ccontr)
  1.1409 -  assume "l \<noteq> l'"
  1.1410 -  obtain U V where "open U" "open V" "l \<in> U" "l' \<in> V" "U \<inter> V = {}"
  1.1411 -    using hausdorff [OF `l \<noteq> l'`] by fast
  1.1412 -  have "eventually (\<lambda>x. f x \<in> U) net"
  1.1413 -    using `(f ---> l) net` `open U` `l \<in> U` by (rule topological_tendstoD)
  1.1414 -  moreover
  1.1415 -  have "eventually (\<lambda>x. f x \<in> V) net"
  1.1416 -    using `(f ---> l') net` `open V` `l' \<in> V` by (rule topological_tendstoD)
  1.1417 -  ultimately
  1.1418 -  have "eventually (\<lambda>x. False) net"
  1.1419 -  proof (rule eventually_elim2)
  1.1420 -    fix x
  1.1421 -    assume "f x \<in> U" "f x \<in> V"
  1.1422 -    hence "f x \<in> U \<inter> V" by simp
  1.1423 -    with `U \<inter> V = {}` show "False" by simp
  1.1424 -  qed
  1.1425 -  with `\<not> trivial_limit net` show "False"
  1.1426 -    by (simp add: eventually_False)
  1.1427 -qed
  1.1428 -
  1.1429 -lemma tendsto_Lim:
  1.1430 -  fixes f :: "'a \<Rightarrow> 'b::t2_space"
  1.1431 -  shows "~(trivial_limit net) \<Longrightarrow> (f ---> l) net ==> Lim net f = l"
  1.1432 -  unfolding Lim_def using Lim_unique[of net f] by auto
  1.1433 -
  1.1434 -text{* Limit under bilinear function *}
  1.1435 -
  1.1436 -lemma Lim_bilinear:
  1.1437 -  assumes "(f ---> l) net" and "(g ---> m) net" and "bounded_bilinear h"
  1.1438 -  shows "((\<lambda>x. h (f x) (g x)) ---> (h l m)) net"
  1.1439 -using `bounded_bilinear h` `(f ---> l) net` `(g ---> m) net`
  1.1440 -by (rule bounded_bilinear.tendsto)
  1.1441 -
  1.1442 -text{* These are special for limits out of the same vector space. *}
  1.1443 -
  1.1444 -lemma Lim_within_id: "(id ---> a) (at a within s)"
  1.1445 -  unfolding tendsto_def Limits.eventually_within eventually_at_topological
  1.1446 -  by auto
  1.1447 -
  1.1448 -lemma Lim_at_id: "(id ---> a) (at a)"
  1.1449 -apply (subst within_UNIV[symmetric]) by (simp add: Lim_within_id)
  1.1450 -
  1.1451 -lemma Lim_at_zero:
  1.1452 -  fixes a :: "'a::real_normed_vector"
  1.1453 -  fixes l :: "'b::topological_space"
  1.1454 -  shows "(f ---> l) (at a) \<longleftrightarrow> ((\<lambda>x. f(a + x)) ---> l) (at 0)" (is "?lhs = ?rhs")
  1.1455 -proof
  1.1456 -  assume "?lhs"
  1.1457 -  { fix S assume "open S" "l \<in> S"
  1.1458 -    with `?lhs` have "eventually (\<lambda>x. f x \<in> S) (at a)"
  1.1459 -      by (rule topological_tendstoD)
  1.1460 -    then obtain d where d: "d>0" "\<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<in> S"
  1.1461 -      unfolding Limits.eventually_at by fast
  1.1462 -    { fix x::"'a" assume "x \<noteq> 0 \<and> dist x 0 < d"
  1.1463 -      hence "f (a + x) \<in> S" using d
  1.1464 -      apply(erule_tac x="x+a" in allE)
  1.1465 -      by(auto simp add: comm_monoid_add.mult_commute dist_norm dist_commute)
  1.1466 -    }
  1.1467 -    hence "\<exists>d>0. \<forall>x. x \<noteq> 0 \<and> dist x 0 < d \<longrightarrow> f (a + x) \<in> S"
  1.1468 -      using d(1) by auto
  1.1469 -    hence "eventually (\<lambda>x. f (a + x) \<in> S) (at 0)"
  1.1470 -      unfolding Limits.eventually_at .
  1.1471 -  }
  1.1472 -  thus "?rhs" by (rule topological_tendstoI)
  1.1473 -next
  1.1474 -  assume "?rhs"
  1.1475 -  { fix S assume "open S" "l \<in> S"
  1.1476 -    with `?rhs` have "eventually (\<lambda>x. f (a + x) \<in> S) (at 0)"
  1.1477 -      by (rule topological_tendstoD)
  1.1478 -    then obtain d where d: "d>0" "\<forall>x. x \<noteq> 0 \<and> dist x 0 < d \<longrightarrow> f (a + x) \<in> S"
  1.1479 -      unfolding Limits.eventually_at by fast
  1.1480 -    { fix x::"'a" assume "x \<noteq> a \<and> dist x a < d"
  1.1481 -      hence "f x \<in> S" using d apply(erule_tac x="x-a" in allE)
  1.1482 -        by(auto simp add: comm_monoid_add.mult_commute dist_norm dist_commute)
  1.1483 -    }
  1.1484 -    hence "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<in> S" using d(1) by auto
  1.1485 -    hence "eventually (\<lambda>x. f x \<in> S) (at a)" unfolding Limits.eventually_at .
  1.1486 -  }
  1.1487 -  thus "?lhs" by (rule topological_tendstoI)
  1.1488 -qed
  1.1489 -
  1.1490 -text{* It's also sometimes useful to extract the limit point from the net.  *}
  1.1491 -
  1.1492 -definition
  1.1493 -  netlimit :: "'a::t2_space net \<Rightarrow> 'a" where
  1.1494 -  "netlimit net = (SOME a. ((\<lambda>x. x) ---> a) net)"
  1.1495 -
  1.1496 -lemma netlimit_within:
  1.1497 -  assumes "\<not> trivial_limit (at a within S)"
  1.1498 -  shows "netlimit (at a within S) = a"
  1.1499 -unfolding netlimit_def
  1.1500 -apply (rule some_equality)
  1.1501 -apply (rule Lim_at_within)
  1.1502 -apply (rule Lim_ident_at)
  1.1503 -apply (erule Lim_unique [OF assms])
  1.1504 -apply (rule Lim_at_within)
  1.1505 -apply (rule Lim_ident_at)
  1.1506 -done
  1.1507 -
  1.1508 -lemma netlimit_at:
  1.1509 -  fixes a :: "'a::perfect_space"
  1.1510 -  shows "netlimit (at a) = a"
  1.1511 -  apply (subst within_UNIV[symmetric])
  1.1512 -  using netlimit_within[of a UNIV]
  1.1513 -  by (simp add: trivial_limit_at within_UNIV)
  1.1514 -
  1.1515 -text{* Transformation of limit. *}
  1.1516 -
  1.1517 -lemma Lim_transform:
  1.1518 -  fixes f g :: "'a::type \<Rightarrow> 'b::real_normed_vector"
  1.1519 -  assumes "((\<lambda>x. f x - g x) ---> 0) net" "(f ---> l) net"
  1.1520 -  shows "(g ---> l) net"
  1.1521 -proof-
  1.1522 -  from assms have "((\<lambda>x. f x - g x - f x) ---> 0 - l) net" using Lim_sub[of "\<lambda>x. f x - g x" 0 net f l] by auto
  1.1523 -  thus "?thesis" using Lim_neg [of "\<lambda> x. - g x" "-l" net] by auto
  1.1524 -qed
  1.1525 -
  1.1526 -lemma Lim_transform_eventually:
  1.1527 -  "eventually (\<lambda>x. f x = g x) net \<Longrightarrow> (f ---> l) net ==> (g ---> l) net"
  1.1528 -  apply (rule topological_tendstoI)
  1.1529 -  apply (drule (2) topological_tendstoD)
  1.1530 -  apply (erule (1) eventually_elim2, simp)
  1.1531 -  done
  1.1532 -
  1.1533 -lemma Lim_transform_within:
  1.1534 -  fixes l :: "'b::metric_space" (* TODO: generalize *)
  1.1535 -  assumes "0 < d" "(\<forall>x'\<in>S. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x')"
  1.1536 -          "(f ---> l) (at x within S)"
  1.1537 -  shows   "(g ---> l) (at x within S)"
  1.1538 -  using assms(1,3) unfolding Lim_within
  1.1539 -  apply -
  1.1540 -  apply (clarify, rename_tac e)
  1.1541 -  apply (drule_tac x=e in spec, clarsimp, rename_tac r)
  1.1542 -  apply (rule_tac x="min d r" in exI, clarsimp, rename_tac y)
  1.1543 -  apply (drule_tac x=y in bspec, assumption, clarsimp)
  1.1544 -  apply (simp add: assms(2))
  1.1545 -  done
  1.1546 -
  1.1547 -lemma Lim_transform_at:
  1.1548 -  fixes l :: "'b::metric_space" (* TODO: generalize *)
  1.1549 -  shows "0 < d \<Longrightarrow> (\<forall>x'. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x') \<Longrightarrow>
  1.1550 -  (f ---> l) (at x) ==> (g ---> l) (at x)"
  1.1551 -  apply (subst within_UNIV[symmetric])
  1.1552 -  using Lim_transform_within[of d UNIV x f g l]
  1.1553 -  by (auto simp add: within_UNIV)
  1.1554 -
  1.1555 -text{* Common case assuming being away from some crucial point like 0. *}
  1.1556 -
  1.1557 -lemma Lim_transform_away_within:
  1.1558 -  fixes a b :: "'a::metric_space"
  1.1559 -  fixes l :: "'b::metric_space" (* TODO: generalize *)
  1.1560 -  assumes "a\<noteq>b" "\<forall>x\<in> S. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"
  1.1561 -  and "(f ---> l) (at a within S)"
  1.1562 -  shows "(g ---> l) (at a within S)"
  1.1563 -proof-
  1.1564 -  have "\<forall>x'\<in>S. 0 < dist x' a \<and> dist x' a < dist a b \<longrightarrow> f x' = g x'" using assms(2)
  1.1565 -    apply auto apply(erule_tac x=x' in ballE) by (auto simp add: dist_commute)
  1.1566 -  thus ?thesis using Lim_transform_within[of "dist a b" S a f g l] using assms(1,3) unfolding dist_nz by auto
  1.1567 -qed
  1.1568 -
  1.1569 -lemma Lim_transform_away_at:
  1.1570 -  fixes a b :: "'a::metric_space"
  1.1571 -  fixes l :: "'b::metric_space" (* TODO: generalize *)
  1.1572 -  assumes ab: "a\<noteq>b" and fg: "\<forall>x. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"
  1.1573 -  and fl: "(f ---> l) (at a)"
  1.1574 -  shows "(g ---> l) (at a)"
  1.1575 -  using Lim_transform_away_within[OF ab, of UNIV f g l] fg fl
  1.1576 -  by (auto simp add: within_UNIV)
  1.1577 -
  1.1578 -text{* Alternatively, within an open set. *}
  1.1579 -
  1.1580 -lemma Lim_transform_within_open:
  1.1581 -  fixes a :: "'a::metric_space"
  1.1582 -  fixes l :: "'b::metric_space" (* TODO: generalize *)
  1.1583 -  assumes "open S"  "a \<in> S"  "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> f x = g x"  "(f ---> l) (at a)"
  1.1584 -  shows "(g ---> l) (at a)"
  1.1585 -proof-
  1.1586 -  from assms(1,2) obtain e::real where "e>0" and e:"ball a e \<subseteq> S" unfolding open_contains_ball by auto
  1.1587 -  hence "\<forall>x'. 0 < dist x' a \<and> dist x' a < e \<longrightarrow> f x' = g x'" using assms(3)
  1.1588 -    unfolding ball_def subset_eq apply auto apply(erule_tac x=x' in allE) apply(erule_tac x=x' in ballE) by(auto simp add: dist_commute)
  1.1589 -  thus ?thesis using Lim_transform_at[of e a f g l] `e>0` assms(4) by auto
  1.1590 -qed
  1.1591 -
  1.1592 -text{* A congruence rule allowing us to transform limits assuming not at point. *}
  1.1593 -
  1.1594 -(* FIXME: Only one congruence rule for tendsto can be used at a time! *)
  1.1595 -
  1.1596 -lemma Lim_cong_within[cong add]:
  1.1597 -  fixes a :: "'a::metric_space"
  1.1598 -  fixes l :: "'b::metric_space" (* TODO: generalize *)
  1.1599 -  shows "(\<And>x. x \<noteq> a \<Longrightarrow> f x = g x) ==> ((\<lambda>x. f x) ---> l) (at a within S) \<longleftrightarrow> ((g ---> l) (at a within S))"
  1.1600 -  by (simp add: Lim_within dist_nz[symmetric])
  1.1601 -
  1.1602 -lemma Lim_cong_at[cong add]:
  1.1603 -  fixes a :: "'a::metric_space"
  1.1604 -  fixes l :: "'b::metric_space" (* TODO: generalize *)
  1.1605 -  shows "(\<And>x. x \<noteq> a ==> f x = g x) ==> (((\<lambda>x. f x) ---> l) (at a) \<longleftrightarrow> ((g ---> l) (at a)))"
  1.1606 -  by (simp add: Lim_at dist_nz[symmetric])
  1.1607 -
  1.1608 -text{* Useful lemmas on closure and set of possible sequential limits.*}
  1.1609 -
  1.1610 -lemma closure_sequential:
  1.1611 -  fixes l :: "'a::metric_space" (* TODO: generalize *)
  1.1612 -  shows "l \<in> closure S \<longleftrightarrow> (\<exists>x. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially)" (is "?lhs = ?rhs")
  1.1613 -proof
  1.1614 -  assume "?lhs" moreover
  1.1615 -  { assume "l \<in> S"
  1.1616 -    hence "?rhs" using Lim_const[of l sequentially] by auto
  1.1617 -  } moreover
  1.1618 -  { assume "l islimpt S"
  1.1619 -    hence "?rhs" unfolding islimpt_sequential by auto
  1.1620 -  } ultimately
  1.1621 -  show "?rhs" unfolding closure_def by auto
  1.1622 -next
  1.1623 -  assume "?rhs"
  1.1624 -  thus "?lhs" unfolding closure_def unfolding islimpt_sequential by auto
  1.1625 -qed
  1.1626 -
  1.1627 -lemma closed_sequential_limits:
  1.1628 -  fixes S :: "'a::metric_space set"
  1.1629 -  shows "closed S \<longleftrightarrow> (\<forall>x l. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially \<longrightarrow> l \<in> S)"
  1.1630 -  unfolding closed_limpt
  1.1631 -  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]
  1.1632 -  by metis
  1.1633 -
  1.1634 -lemma closure_approachable:
  1.1635 -  fixes S :: "'a::metric_space set"
  1.1636 -  shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e)"
  1.1637 -  apply (auto simp add: closure_def islimpt_approachable)
  1.1638 -  by (metis dist_self)
  1.1639 -
  1.1640 -lemma closed_approachable:
  1.1641 -  fixes S :: "'a::metric_space set"
  1.1642 -  shows "closed S ==> (\<forall>e>0. \<exists>y\<in>S. dist y x < e) \<longleftrightarrow> x \<in> S"
  1.1643 -  by (metis closure_closed closure_approachable)
  1.1644 -
  1.1645 -text{* Some other lemmas about sequences. *}
  1.1646 -
  1.1647 -lemma seq_offset:
  1.1648 -  fixes l :: "'a::metric_space" (* TODO: generalize *)
  1.1649 -  shows "(f ---> l) sequentially ==> ((\<lambda>i. f( i + k)) ---> l) sequentially"
  1.1650 -  apply (auto simp add: Lim_sequentially)
  1.1651 -  by (metis trans_le_add1 )
  1.1652 -
  1.1653 -lemma seq_offset_neg:
  1.1654 -  "(f ---> l) sequentially ==> ((\<lambda>i. f(i - k)) ---> l) sequentially"
  1.1655 -  apply (rule topological_tendstoI)
  1.1656 -  apply (drule (2) topological_tendstoD)
  1.1657 -  apply (simp only: eventually_sequentially)
  1.1658 -  apply (subgoal_tac "\<And>N k (n::nat). N + k <= n ==> N <= n - k")
  1.1659 -  apply metis
  1.1660 -  by arith
  1.1661 -
  1.1662 -lemma seq_offset_rev:
  1.1663 -  "((\<lambda>i. f(i + k)) ---> l) sequentially ==> (f ---> l) sequentially"
  1.1664 -  apply (rule topological_tendstoI)
  1.1665 -  apply (drule (2) topological_tendstoD)
  1.1666 -  apply (simp only: eventually_sequentially)
  1.1667 -  apply (subgoal_tac "\<And>N k (n::nat). N + k <= n ==> N <= n - k \<and> (n - k) + k = n")
  1.1668 -  by metis arith
  1.1669 -
  1.1670 -lemma seq_harmonic: "((\<lambda>n. inverse (real n)) ---> 0) sequentially"
  1.1671 -proof-
  1.1672 -  { fix e::real assume "e>0"
  1.1673 -    hence "\<exists>N::nat. \<forall>n::nat\<ge>N. inverse (real n) < e"
  1.1674 -      using real_arch_inv[of e] apply auto apply(rule_tac x=n in exI)
  1.1675 -      by (metis not_le le_imp_inverse_le not_less real_of_nat_gt_zero_cancel_iff real_of_nat_less_iff xt1(7))
  1.1676 -  }
  1.1677 -  thus ?thesis unfolding Lim_sequentially dist_norm by simp
  1.1678 -qed
  1.1679 -
  1.1680 -text{* More properties of closed balls. *}
  1.1681 -
  1.1682 -lemma closed_cball: "closed (cball x e)"
  1.1683 -unfolding cball_def closed_def
  1.1684 -unfolding Collect_neg_eq [symmetric] not_le
  1.1685 -apply (clarsimp simp add: open_dist, rename_tac y)
  1.1686 -apply (rule_tac x="dist x y - e" in exI, clarsimp)
  1.1687 -apply (rename_tac x')
  1.1688 -apply (cut_tac x=x and y=x' and z=y in dist_triangle)
  1.1689 -apply simp
  1.1690 -done
  1.1691 -
  1.1692 -lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"
  1.1693 -proof-
  1.1694 -  { fix x and e::real assume "x\<in>S" "e>0" "ball x e \<subseteq> S"
  1.1695 -    hence "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)
  1.1696 -  } moreover
  1.1697 -  { fix x and e::real assume "x\<in>S" "e>0" "cball x e \<subseteq> S"
  1.1698 -    hence "\<exists>d>0. ball x d \<subseteq> S" unfolding subset_eq apply(rule_tac x="e/2" in exI) by auto
  1.1699 -  } ultimately
  1.1700 -  show ?thesis unfolding open_contains_ball by auto
  1.1701 -qed
  1.1702 -
  1.1703 -lemma open_contains_cball_eq: "open S ==> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"
  1.1704 -  by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball mem_def)
  1.1705 -
  1.1706 -lemma mem_interior_cball: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S)"
  1.1707 -  apply (simp add: interior_def, safe)
  1.1708 -  apply (force simp add: open_contains_cball)
  1.1709 -  apply (rule_tac x="ball x e" in exI)
  1.1710 -  apply (simp add: open_ball centre_in_ball subset_trans [OF ball_subset_cball])
  1.1711 -  done
  1.1712 -
  1.1713 -lemma islimpt_ball:
  1.1714 -  fixes x y :: "'a::{real_normed_vector,perfect_space}"
  1.1715 -  shows "y islimpt ball x e \<longleftrightarrow> 0 < e \<and> y \<in> cball x e" (is "?lhs = ?rhs")
  1.1716 -proof
  1.1717 -  assume "?lhs"
  1.1718 -  { assume "e \<le> 0"
  1.1719 -    hence *:"ball x e = {}" using ball_eq_empty[of x e] by auto
  1.1720 -    have False using `?lhs` unfolding * using islimpt_EMPTY[of y] by auto
  1.1721 -  }
  1.1722 -  hence "e > 0" by (metis not_less)
  1.1723 -  moreover
  1.1724 -  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
  1.1725 -  ultimately show "?rhs" by auto
  1.1726 -next
  1.1727 -  assume "?rhs" hence "e>0"  by auto
  1.1728 -  { fix d::real assume "d>0"
  1.1729 -    have "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  1.1730 -    proof(cases "d \<le> dist x y")
  1.1731 -      case True thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  1.1732 -      proof(cases "x=y")
  1.1733 -        case True hence False using `d \<le> dist x y` `d>0` by auto
  1.1734 -        thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d" by auto
  1.1735 -      next
  1.1736 -        case False
  1.1737 -
  1.1738 -        have "dist x (y - (d / (2 * dist y x)) *\<^sub>R (y - x))
  1.1739 -              = norm (x - y + (d / (2 * norm (y - x))) *\<^sub>R (y - x))"
  1.1740 -          unfolding mem_cball mem_ball dist_norm diff_diff_eq2 diff_add_eq[THEN sym] by auto
  1.1741 -        also have "\<dots> = \<bar>- 1 + d / (2 * norm (x - y))\<bar> * norm (x - y)"
  1.1742 -          using scaleR_left_distrib[of "- 1" "d / (2 * norm (y - x))", THEN sym, of "y - x"]
  1.1743 -          unfolding scaleR_minus_left scaleR_one
  1.1744 -          by (auto simp add: norm_minus_commute)
  1.1745 -        also have "\<dots> = \<bar>- norm (x - y) + d / 2\<bar>"
  1.1746 -          unfolding abs_mult_pos[of "norm (x - y)", OF norm_ge_zero[of "x - y"]]
  1.1747 -          unfolding real_add_mult_distrib using `x\<noteq>y`[unfolded dist_nz, unfolded dist_norm] by auto
  1.1748 -        also have "\<dots> \<le> e - d/2" using `d \<le> dist x y` and `d>0` and `?rhs` by(auto simp add: dist_norm)
  1.1749 -        finally have "y - (d / (2 * dist y x)) *\<^sub>R (y - x) \<in> ball x e" using `d>0` by auto
  1.1750 -
  1.1751 -        moreover
  1.1752 -
  1.1753 -        have "(d / (2*dist y x)) *\<^sub>R (y - x) \<noteq> 0"
  1.1754 -          using `x\<noteq>y`[unfolded dist_nz] `d>0` unfolding scaleR_eq_0_iff by (auto simp add: dist_commute)
  1.1755 -        moreover
  1.1756 -        have "dist (y - (d / (2 * dist y x)) *\<^sub>R (y - x)) y < d" unfolding dist_norm apply simp unfolding norm_minus_cancel
  1.1757 -          using `d>0` `x\<noteq>y`[unfolded dist_nz] dist_commute[of x y]
  1.1758 -          unfolding dist_norm by auto
  1.1759 -        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
  1.1760 -      qed
  1.1761 -    next
  1.1762 -      case False hence "d > dist x y" by auto
  1.1763 -      show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  1.1764 -      proof(cases "x=y")
  1.1765 -        case True
  1.1766 -        obtain z where **: "z \<noteq> y" "dist z y < min e d"
  1.1767 -          using perfect_choose_dist[of "min e d" y]
  1.1768 -          using `d > 0` `e>0` by auto
  1.1769 -        show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  1.1770 -          unfolding `x = y`
  1.1771 -          using `z \<noteq> y` **
  1.1772 -          by (rule_tac x=z in bexI, auto simp add: dist_commute)
  1.1773 -      next
  1.1774 -        case False thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  1.1775 -          using `d>0` `d > dist x y` `?rhs` by(rule_tac x=x in bexI, auto)
  1.1776 -      qed
  1.1777 -    qed  }
  1.1778 -  thus "?lhs" unfolding mem_cball islimpt_approachable mem_ball by auto
  1.1779 -qed
  1.1780 -
  1.1781 -lemma closure_ball_lemma:
  1.1782 -  fixes x y :: "'a::real_normed_vector"
  1.1783 -  assumes "x \<noteq> y" shows "y islimpt ball x (dist x y)"
  1.1784 -proof (rule islimptI)
  1.1785 -  fix T assume "y \<in> T" "open T"
  1.1786 -  then obtain r where "0 < r" "\<forall>z. dist z y < r \<longrightarrow> z \<in> T"
  1.1787 -    unfolding open_dist by fast
  1.1788 -  (* choose point between x and y, within distance r of y. *)
  1.1789 -  def k \<equiv> "min 1 (r / (2 * dist x y))"
  1.1790 -  def z \<equiv> "y + scaleR k (x - y)"
  1.1791 -  have z_def2: "z = x + scaleR (1 - k) (y - x)"
  1.1792 -    unfolding z_def by (simp add: algebra_simps)
  1.1793 -  have "dist z y < r"
  1.1794 -    unfolding z_def k_def using `0 < r`
  1.1795 -    by (simp add: dist_norm min_def)
  1.1796 -  hence "z \<in> T" using `\<forall>z. dist z y < r \<longrightarrow> z \<in> T` by simp
  1.1797 -  have "dist x z < dist x y"
  1.1798 -    unfolding z_def2 dist_norm
  1.1799 -    apply (simp add: norm_minus_commute)
  1.1800 -    apply (simp only: dist_norm [symmetric])
  1.1801 -    apply (subgoal_tac "\<bar>1 - k\<bar> * dist x y < 1 * dist x y", simp)
  1.1802 -    apply (rule mult_strict_right_mono)
  1.1803 -    apply (simp add: k_def divide_pos_pos zero_less_dist_iff `0 < r` `x \<noteq> y`)
  1.1804 -    apply (simp add: zero_less_dist_iff `x \<noteq> y`)
  1.1805 -    done
  1.1806 -  hence "z \<in> ball x (dist x y)" by simp
  1.1807 -  have "z \<noteq> y"
  1.1808 -    unfolding z_def k_def using `x \<noteq> y` `0 < r`
  1.1809 -    by (simp add: min_def)
  1.1810 -  show "\<exists>z\<in>ball x (dist x y). z \<in> T \<and> z \<noteq> y"
  1.1811 -    using `z \<in> ball x (dist x y)` `z \<in> T` `z \<noteq> y`
  1.1812 -    by fast
  1.1813 -qed
  1.1814 -
  1.1815 -lemma closure_ball:
  1.1816 -  fixes x :: "'a::real_normed_vector"
  1.1817 -  shows "0 < e \<Longrightarrow> closure (ball x e) = cball x e"
  1.1818 -apply (rule equalityI)
  1.1819 -apply (rule closure_minimal)
  1.1820 -apply (rule ball_subset_cball)
  1.1821 -apply (rule closed_cball)
  1.1822 -apply (rule subsetI, rename_tac y)
  1.1823 -apply (simp add: le_less [where 'a=real])
  1.1824 -apply (erule disjE)
  1.1825 -apply (rule subsetD [OF closure_subset], simp)
  1.1826 -apply (simp add: closure_def)
  1.1827 -apply clarify
  1.1828 -apply (rule closure_ball_lemma)
  1.1829 -apply (simp add: zero_less_dist_iff)
  1.1830 -done
  1.1831 -
  1.1832 -(* In a trivial vector space, this fails for e = 0. *)
  1.1833 -lemma interior_cball:
  1.1834 -  fixes x :: "'a::{real_normed_vector, perfect_space}"
  1.1835 -  shows "interior (cball x e) = ball x e"
  1.1836 -proof(cases "e\<ge>0")
  1.1837 -  case False note cs = this
  1.1838 -  from cs have "ball x e = {}" using ball_empty[of e x] by auto moreover
  1.1839 -  { fix y assume "y \<in> cball x e"
  1.1840 -    hence False unfolding mem_cball using dist_nz[of x y] cs by auto  }
  1.1841 -  hence "cball x e = {}" by auto
  1.1842 -  hence "interior (cball x e) = {}" using interior_empty by auto
  1.1843 -  ultimately show ?thesis by blast
  1.1844 -next
  1.1845 -  case True note cs = this
  1.1846 -  have "ball x e \<subseteq> cball x e" using ball_subset_cball by auto moreover
  1.1847 -  { fix S y assume as: "S \<subseteq> cball x e" "open S" "y\<in>S"
  1.1848 -    then obtain d where "d>0" and d:"\<forall>x'. dist x' y < d \<longrightarrow> x' \<in> S" unfolding open_dist by blast
  1.1849 -
  1.1850 -    then obtain xa where xa_y: "xa \<noteq> y" and xa: "dist xa y < d"
  1.1851 -      using perfect_choose_dist [of d] by auto
  1.1852 -    have "xa\<in>S" using d[THEN spec[where x=xa]] using xa by(auto simp add: dist_commute)
  1.1853 -    hence xa_cball:"xa \<in> cball x e" using as(1) by auto
  1.1854 -
  1.1855 -    hence "y \<in> ball x e" proof(cases "x = y")
  1.1856 -      case True
  1.1857 -      hence "e>0" using xa_y[unfolded dist_nz] xa_cball[unfolded mem_cball] by (auto simp add: dist_commute)
  1.1858 -      thus "y \<in> ball x e" using `x = y ` by simp
  1.1859 -    next
  1.1860 -      case False
  1.1861 -      have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) y < d" unfolding dist_norm
  1.1862 -        using `d>0` norm_ge_zero[of "y - x"] `x \<noteq> y` by auto
  1.1863 -      hence *:"y + (d / 2 / dist y x) *\<^sub>R (y - x) \<in> cball x e" using d as(1)[unfolded subset_eq] by blast
  1.1864 -      have "y - x \<noteq> 0" using `x \<noteq> y` by auto
  1.1865 -      hence **:"d / (2 * norm (y - x)) > 0" unfolding zero_less_norm_iff[THEN sym]
  1.1866 -        using `d>0` divide_pos_pos[of d "2*norm (y - x)"] by auto
  1.1867 -
  1.1868 -      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)"
  1.1869 -        by (auto simp add: dist_norm algebra_simps)
  1.1870 -      also have "\<dots> = norm ((1 + d / (2 * norm (y - x))) *\<^sub>R (y - x))"
  1.1871 -        by (auto simp add: algebra_simps)
  1.1872 -      also have "\<dots> = \<bar>1 + d / (2 * norm (y - x))\<bar> * norm (y - x)"
  1.1873 -        using ** by auto
  1.1874 -      also have "\<dots> = (dist y x) + d/2"using ** by (auto simp add: left_distrib dist_norm)
  1.1875 -      finally have "e \<ge> dist x y +d/2" using *[unfolded mem_cball] by (auto simp add: dist_commute)
  1.1876 -      thus "y \<in> ball x e" unfolding mem_ball using `d>0` by auto
  1.1877 -    qed  }
  1.1878 -  hence "\<forall>S \<subseteq> cball x e. open S \<longrightarrow> S \<subseteq> ball x e" by auto
  1.1879 -  ultimately show ?thesis using interior_unique[of "ball x e" "cball x e"] using open_ball[of x e] by auto
  1.1880 -qed
  1.1881 -
  1.1882 -lemma frontier_ball:
  1.1883 -  fixes a :: "'a::real_normed_vector"
  1.1884 -  shows "0 < e ==> frontier(ball a e) = {x. dist a x = e}"
  1.1885 -  apply (simp add: frontier_def closure_ball interior_open open_ball order_less_imp_le)
  1.1886 -  apply (simp add: expand_set_eq)
  1.1887 -  by arith
  1.1888 -
  1.1889 -lemma frontier_cball:
  1.1890 -  fixes a :: "'a::{real_normed_vector, perfect_space}"
  1.1891 -  shows "frontier(cball a e) = {x. dist a x = e}"
  1.1892 -  apply (simp add: frontier_def interior_cball closed_cball closure_closed order_less_imp_le)
  1.1893 -  apply (simp add: expand_set_eq)
  1.1894 -  by arith
  1.1895 -
  1.1896 -lemma cball_eq_empty: "(cball x e = {}) \<longleftrightarrow> e < 0"
  1.1897 -  apply (simp add: expand_set_eq not_le)
  1.1898 -  by (metis zero_le_dist dist_self order_less_le_trans)
  1.1899 -lemma cball_empty: "e < 0 ==> cball x e = {}" by (simp add: cball_eq_empty)
  1.1900 -
  1.1901 -lemma cball_eq_sing:
  1.1902 -  fixes x :: "'a::perfect_space"
  1.1903 -  shows "(cball x e = {x}) \<longleftrightarrow> e = 0"
  1.1904 -proof (rule linorder_cases)
  1.1905 -  assume e: "0 < e"
  1.1906 -  obtain a where "a \<noteq> x" "dist a x < e"
  1.1907 -    using perfect_choose_dist [OF e] by auto
  1.1908 -  hence "a \<noteq> x" "dist x a \<le> e" by (auto simp add: dist_commute)
  1.1909 -  with e show ?thesis by (auto simp add: expand_set_eq)
  1.1910 -qed auto
  1.1911 -
  1.1912 -lemma cball_sing:
  1.1913 -  fixes x :: "'a::metric_space"
  1.1914 -  shows "e = 0 ==> cball x e = {x}"
  1.1915 -  by (auto simp add: expand_set_eq)
  1.1916 -
  1.1917 -text{* For points in the interior, localization of limits makes no difference.   *}
  1.1918 -
  1.1919 -lemma eventually_within_interior:
  1.1920 -  assumes "x \<in> interior S"
  1.1921 -  shows "eventually P (at x within S) \<longleftrightarrow> eventually P (at x)" (is "?lhs = ?rhs")
  1.1922 -proof-
  1.1923 -  from assms obtain T where T: "open T" "x \<in> T" "T \<subseteq> S"
  1.1924 -    unfolding interior_def by fast
  1.1925 -  { assume "?lhs"
  1.1926 -    then obtain A where "open A" "x \<in> A" "\<forall>y\<in>A. y \<noteq> x \<longrightarrow> y \<in> S \<longrightarrow> P y"
  1.1927 -      unfolding Limits.eventually_within Limits.eventually_at_topological
  1.1928 -      by auto
  1.1929 -    with T have "open (A \<inter> T)" "x \<in> A \<inter> T" "\<forall>y\<in>(A \<inter> T). y \<noteq> x \<longrightarrow> P y"
  1.1930 -      by auto
  1.1931 -    then have "?rhs"
  1.1932 -      unfolding Limits.eventually_at_topological by auto
  1.1933 -  } moreover
  1.1934 -  { assume "?rhs" hence "?lhs"
  1.1935 -      unfolding Limits.eventually_within
  1.1936 -      by (auto elim: eventually_elim1)
  1.1937 -  } ultimately
  1.1938 -  show "?thesis" ..
  1.1939 -qed
  1.1940 -
  1.1941 -lemma lim_within_interior:
  1.1942 -  "x \<in> interior S \<Longrightarrow> (f ---> l) (at x within S) \<longleftrightarrow> (f ---> l) (at x)"
  1.1943 -  unfolding tendsto_def by (simp add: eventually_within_interior)
  1.1944 -
  1.1945 -lemma netlimit_within_interior:
  1.1946 -  fixes x :: "'a::{perfect_space, real_normed_vector}"
  1.1947 -    (* FIXME: generalize to perfect_space *)
  1.1948 -  assumes "x \<in> interior S"
  1.1949 -  shows "netlimit(at x within S) = x" (is "?lhs = ?rhs")
  1.1950 -proof-
  1.1951 -  from assms obtain e::real where e:"e>0" "ball x e \<subseteq> S" using open_interior[of S] unfolding open_contains_ball using interior_subset[of S] by auto
  1.1952 -  hence "\<not> trivial_limit (at x within S)" using islimpt_subset[of x "ball x e" S] unfolding trivial_limit_within islimpt_ball centre_in_cball by auto
  1.1953 -  thus ?thesis using netlimit_within by auto
  1.1954 -qed
  1.1955 -
  1.1956 -subsection{* Boundedness. *}
  1.1957 -
  1.1958 -  (* FIXME: This has to be unified with BSEQ!! *)
  1.1959 -definition
  1.1960 -  bounded :: "'a::metric_space set \<Rightarrow> bool" where
  1.1961 -  "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"
  1.1962 -
  1.1963 -lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"
  1.1964 -unfolding bounded_def
  1.1965 -apply safe
  1.1966 -apply (rule_tac x="dist a x + e" in exI, clarify)
  1.1967 -apply (drule (1) bspec)
  1.1968 -apply (erule order_trans [OF dist_triangle add_left_mono])
  1.1969 -apply auto
  1.1970 -done
  1.1971 -
  1.1972 -lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"
  1.1973 -unfolding bounded_any_center [where a=0]
  1.1974 -by (simp add: dist_norm)
  1.1975 -
  1.1976 -lemma bounded_empty[simp]: "bounded {}" by (simp add: bounded_def)
  1.1977 -lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T ==> bounded S"
  1.1978 -  by (metis bounded_def subset_eq)
  1.1979 -
  1.1980 -lemma bounded_interior[intro]: "bounded S ==> bounded(interior S)"
  1.1981 -  by (metis bounded_subset interior_subset)
  1.1982 -
  1.1983 -lemma bounded_closure[intro]: assumes "bounded S" shows "bounded(closure S)"
  1.1984 -proof-
  1.1985 -  from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a" unfolding bounded_def by auto
  1.1986 -  { fix y assume "y \<in> closure S"
  1.1987 -    then obtain f where f: "\<forall>n. f n \<in> S"  "(f ---> y) sequentially"
  1.1988 -      unfolding closure_sequential by auto
  1.1989 -    have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp
  1.1990 -    hence "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"
  1.1991 -      by (rule eventually_mono, simp add: f(1))
  1.1992 -    have "dist x y \<le> a"
  1.1993 -      apply (rule Lim_dist_ubound [of sequentially f])
  1.1994 -      apply (rule trivial_limit_sequentially)
  1.1995 -      apply (rule f(2))
  1.1996 -      apply fact
  1.1997 -      done
  1.1998 -  }
  1.1999 -  thus ?thesis unfolding bounded_def by auto
  1.2000 -qed
  1.2001 -
  1.2002 -lemma bounded_cball[simp,intro]: "bounded (cball x e)"
  1.2003 -  apply (simp add: bounded_def)
  1.2004 -  apply (rule_tac x=x in exI)
  1.2005 -  apply (rule_tac x=e in exI)
  1.2006 -  apply auto
  1.2007 -  done
  1.2008 -
  1.2009 -lemma bounded_ball[simp,intro]: "bounded(ball x e)"
  1.2010 -  by (metis ball_subset_cball bounded_cball bounded_subset)
  1.2011 -
  1.2012 -lemma finite_imp_bounded[intro]: assumes "finite S" shows "bounded S"
  1.2013 -proof-
  1.2014 -  { fix a F assume as:"bounded F"
  1.2015 -    then obtain x e where "\<forall>y\<in>F. dist x y \<le> e" unfolding bounded_def by auto
  1.2016 -    hence "\<forall>y\<in>(insert a F). dist x y \<le> max e (dist x a)" by auto
  1.2017 -    hence "bounded (insert a F)" unfolding bounded_def by (intro exI)
  1.2018 -  }
  1.2019 -  thus ?thesis using finite_induct[of S bounded]  using bounded_empty assms by auto
  1.2020 -qed
  1.2021 -
  1.2022 -lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"
  1.2023 -  apply (auto simp add: bounded_def)
  1.2024 -  apply (rename_tac x y r s)
  1.2025 -  apply (rule_tac x=x in exI)
  1.2026 -  apply (rule_tac x="max r (dist x y + s)" in exI)
  1.2027 -  apply (rule ballI, rename_tac z, safe)
  1.2028 -  apply (drule (1) bspec, simp)
  1.2029 -  apply (drule (1) bspec)
  1.2030 -  apply (rule min_max.le_supI2)
  1.2031 -  apply (erule order_trans [OF dist_triangle add_left_mono])
  1.2032 -  done
  1.2033 -
  1.2034 -lemma bounded_Union[intro]: "finite F \<Longrightarrow> (\<forall>S\<in>F. bounded S) \<Longrightarrow> bounded(\<Union>F)"
  1.2035 -  by (induct rule: finite_induct[of F], auto)
  1.2036 -
  1.2037 -lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x <= b)"
  1.2038 -  apply (simp add: bounded_iff)
  1.2039 -  apply (subgoal_tac "\<And>x (y::real). 0 < 1 + abs y \<and> (x <= y \<longrightarrow> x <= 1 + abs y)")
  1.2040 -  by metis arith
  1.2041 -
  1.2042 -lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"
  1.2043 -  by (metis Int_lower1 Int_lower2 bounded_subset)
  1.2044 -
  1.2045 -lemma bounded_diff[intro]: "bounded S ==> bounded (S - T)"
  1.2046 -apply (metis Diff_subset bounded_subset)
  1.2047 -done
  1.2048 -
  1.2049 -lemma bounded_insert[intro]:"bounded(insert x S) \<longleftrightarrow> bounded S"
  1.2050 -  by (metis Diff_cancel Un_empty_right Un_insert_right bounded_Un bounded_subset finite.emptyI finite_imp_bounded infinite_remove subset_insertI)
  1.2051 -
  1.2052 -lemma not_bounded_UNIV[simp, intro]:
  1.2053 -  "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"
  1.2054 -proof(auto simp add: bounded_pos not_le)
  1.2055 -  obtain x :: 'a where "x \<noteq> 0"
  1.2056 -    using perfect_choose_dist [OF zero_less_one] by fast
  1.2057 -  fix b::real  assume b: "b >0"
  1.2058 -  have b1: "b +1 \<ge> 0" using b by simp
  1.2059 -  with `x \<noteq> 0` have "b < norm (scaleR (b + 1) (sgn x))"
  1.2060 -    by (simp add: norm_sgn)
  1.2061 -  then show "\<exists>x::'a. b < norm x" ..
  1.2062 -qed
  1.2063 -
  1.2064 -lemma bounded_linear_image:
  1.2065 -  assumes "bounded S" "bounded_linear f"
  1.2066 -  shows "bounded(f ` S)"
  1.2067 -proof-
  1.2068 -  from assms(1) obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto
  1.2069 -  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)
  1.2070 -  { fix x assume "x\<in>S"
  1.2071 -    hence "norm x \<le> b" using b by auto
  1.2072 -    hence "norm (f x) \<le> B * b" using B(2) apply(erule_tac x=x in allE)
  1.2073 -      by (metis B(1) B(2) real_le_trans real_mult_le_cancel_iff2)
  1.2074 -  }
  1.2075 -  thus ?thesis unfolding bounded_pos apply(rule_tac x="b*B" in exI)
  1.2076 -    using b B real_mult_order[of b B] by (auto simp add: real_mult_commute)
  1.2077 -qed
  1.2078 -
  1.2079 -lemma bounded_scaling:
  1.2080 -  fixes S :: "'a::real_normed_vector set"
  1.2081 -  shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x) ` S)"
  1.2082 -  apply (rule bounded_linear_image, assumption)
  1.2083 -  apply (rule scaleR.bounded_linear_right)
  1.2084 -  done
  1.2085 -
  1.2086 -lemma bounded_translation:
  1.2087 -  fixes S :: "'a::real_normed_vector set"
  1.2088 -  assumes "bounded S" shows "bounded ((\<lambda>x. a + x) ` S)"
  1.2089 -proof-
  1.2090 -  from assms obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto
  1.2091 -  { fix x assume "x\<in>S"
  1.2092 -    hence "norm (a + x) \<le> b + norm a" using norm_triangle_ineq[of a x] b by auto
  1.2093 -  }
  1.2094 -  thus ?thesis unfolding bounded_pos using norm_ge_zero[of a] b(1) using add_strict_increasing[of b 0 "norm a"]
  1.2095 -    by (auto intro!: add exI[of _ "b + norm a"])
  1.2096 -qed
  1.2097 -
  1.2098 -
  1.2099 -text{* Some theorems on sups and infs using the notion "bounded". *}
  1.2100 -
  1.2101 -lemma bounded_real:
  1.2102 -  fixes S :: "real set"
  1.2103 -  shows "bounded S \<longleftrightarrow>  (\<exists>a. \<forall>x\<in>S. abs x <= a)"
  1.2104 -  by (simp add: bounded_iff)
  1.2105 -
  1.2106 -lemma bounded_has_rsup: assumes "bounded S" "S \<noteq> {}"
  1.2107 -  shows "\<forall>x\<in>S. x <= rsup S" and "\<forall>b. (\<forall>x\<in>S. x <= b) \<longrightarrow> rsup S <= b"
  1.2108 -proof
  1.2109 -  fix x assume "x\<in>S"
  1.2110 -  from assms(1) obtain a where a:"\<forall>x\<in>S. \<bar>x\<bar> \<le> a" unfolding bounded_real by auto
  1.2111 -  hence *:"S *<= a" using setleI[of S a] by (metis abs_le_interval_iff mem_def)
  1.2112 -  thus "x \<le> rsup S" using rsup[OF `S\<noteq>{}`] using assms(1)[unfolded bounded_real] using isLubD2[of UNIV S "rsup S" x] using `x\<in>S` by auto
  1.2113 -next
  1.2114 -  show "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> rsup S \<le> b" using assms
  1.2115 -  using rsup[of S, unfolded isLub_def isUb_def leastP_def setle_def setge_def]
  1.2116 -  apply (auto simp add: bounded_real)
  1.2117 -  by (auto simp add: isLub_def isUb_def leastP_def setle_def setge_def)
  1.2118 -qed
  1.2119 -
  1.2120 -lemma rsup_insert: assumes "bounded S"
  1.2121 -  shows "rsup(insert x S) = (if S = {} then x else max x (rsup S))"
  1.2122 -proof(cases "S={}")
  1.2123 -  case True thus ?thesis using rsup_finite_in[of "{x}"] by auto
  1.2124 -next
  1.2125 -  let ?S = "insert x S"
  1.2126 -  case False
  1.2127 -  hence *:"\<forall>x\<in>S. x \<le> rsup S" using bounded_has_rsup(1)[of S] using assms by auto
  1.2128 -  hence "insert x S *<= max x (rsup S)" unfolding setle_def by auto
  1.2129 -  hence "isLub UNIV ?S (rsup ?S)" using rsup[of ?S] by auto
  1.2130 -  moreover
  1.2131 -  have **:"isUb UNIV ?S (max x (rsup S))" unfolding isUb_def setle_def using * by auto
  1.2132 -  { fix y assume as:"isUb UNIV (insert x S) y"
  1.2133 -    hence "max x (rsup S) \<le> y" unfolding isUb_def using rsup_le[OF `S\<noteq>{}`]
  1.2134 -      unfolding setle_def by auto  }
  1.2135 -  hence "max x (rsup S) <=* isUb UNIV (insert x S)" unfolding setge_def Ball_def mem_def by auto
  1.2136 -  hence "isLub UNIV ?S (max x (rsup S))" using ** isLubI2[of UNIV ?S "max x (rsup S)"] unfolding Collect_def by auto
  1.2137 -  ultimately show ?thesis using real_isLub_unique[of UNIV ?S] using `S\<noteq>{}` by auto
  1.2138 -qed
  1.2139 -
  1.2140 -lemma sup_insert_finite: "finite S \<Longrightarrow> rsup(insert x S) = (if S = {} then x else max x (rsup S))"
  1.2141 -  apply (rule rsup_insert)
  1.2142 -  apply (rule finite_imp_bounded)
  1.2143 -  by simp
  1.2144 -
  1.2145 -lemma bounded_has_rinf:
  1.2146 -  assumes "bounded S"  "S \<noteq> {}"
  1.2147 -  shows "\<forall>x\<in>S. x >= rinf S" and "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> rinf S >= b"
  1.2148 -proof
  1.2149 -  fix x assume "x\<in>S"
  1.2150 -  from assms(1) obtain a where a:"\<forall>x\<in>S. \<bar>x\<bar> \<le> a" unfolding bounded_real by auto
  1.2151 -  hence *:"- a <=* S" using setgeI[of S "-a"] unfolding abs_le_interval_iff by auto
  1.2152 -  thus "x \<ge> rinf S" using rinf[OF `S\<noteq>{}`] using isGlbD2[of UNIV S "rinf S" x] using `x\<in>S` by auto
  1.2153 -next
  1.2154 -  show "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> rinf S \<ge> b" using assms
  1.2155 -  using rinf[of S, unfolded isGlb_def isLb_def greatestP_def setle_def setge_def]
  1.2156 -  apply (auto simp add: bounded_real)
  1.2157 -  by (auto simp add: isGlb_def isLb_def greatestP_def setle_def setge_def)
  1.2158 -qed
  1.2159 -
  1.2160 -(* TODO: Move this to RComplete.thy -- would need to include Glb into RComplete *)
  1.2161 -lemma real_isGlb_unique: "[| isGlb R S x; isGlb R S y |] ==> x = (y::real)"
  1.2162 -  apply (frule isGlb_isLb)
  1.2163 -  apply (frule_tac x = y in isGlb_isLb)
  1.2164 -  apply (blast intro!: order_antisym dest!: isGlb_le_isLb)
  1.2165 -  done
  1.2166 -
  1.2167 -lemma rinf_insert: assumes "bounded S"
  1.2168 -  shows "rinf(insert x S) = (if S = {} then x else min x (rinf S))" (is "?lhs = ?rhs")
  1.2169 -proof(cases "S={}")
  1.2170 -  case True thus ?thesis using rinf_finite_in[of "{x}"] by auto
  1.2171 -next
  1.2172 -  let ?S = "insert x S"
  1.2173 -  case False
  1.2174 -  hence *:"\<forall>x\<in>S. x \<ge> rinf S" using bounded_has_rinf(1)[of S] using assms by auto
  1.2175 -  hence "min x (rinf S) <=* insert x S" unfolding setge_def by auto
  1.2176 -  hence "isGlb UNIV ?S (rinf ?S)" using rinf[of ?S] by auto
  1.2177 -  moreover
  1.2178 -  have **:"isLb UNIV ?S (min x (rinf S))" unfolding isLb_def setge_def using * by auto
  1.2179 -  { fix y assume as:"isLb UNIV (insert x S) y"
  1.2180 -    hence "min x (rinf S) \<ge> y" unfolding isLb_def using rinf_ge[OF `S\<noteq>{}`]
  1.2181 -      unfolding setge_def by auto  }
  1.2182 -  hence "isLb UNIV (insert x S) *<= min x (rinf S)" unfolding setle_def Ball_def mem_def by auto
  1.2183 -  hence "isGlb UNIV ?S (min x (rinf S))" using ** isGlbI2[of UNIV ?S "min x (rinf S)"] unfolding Collect_def by auto
  1.2184 -  ultimately show ?thesis using real_isGlb_unique[of UNIV ?S] using `S\<noteq>{}` by auto
  1.2185 -qed
  1.2186 -
  1.2187 -lemma inf_insert_finite: "finite S ==> rinf(insert x S) = (if S = {} then x else min x (rinf S))"
  1.2188 -  by (rule rinf_insert, rule finite_imp_bounded, simp)
  1.2189 -
  1.2190 -subsection{* Compactness (the definition is the one based on convegent subsequences). *}
  1.2191 -
  1.2192 -definition
  1.2193 -  compact :: "'a::metric_space set \<Rightarrow> bool" where (* TODO: generalize *)
  1.2194 -  "compact S \<longleftrightarrow>
  1.2195 -   (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>
  1.2196 -       (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially))"
  1.2197 -
  1.2198 -text {*
  1.2199 -  A metric space (or topological vector space) is said to have the
  1.2200 -  Heine-Borel property if every closed and bounded subset is compact.
  1.2201 -*}
  1.2202 -
  1.2203 -class heine_borel =
  1.2204 -  assumes bounded_imp_convergent_subsequence:
  1.2205 -    "bounded s \<Longrightarrow> \<forall>n. f n \<in> s
  1.2206 -      \<Longrightarrow> \<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  1.2207 -
  1.2208 -lemma bounded_closed_imp_compact:
  1.2209 -  fixes s::"'a::heine_borel set"
  1.2210 -  assumes "bounded s" and "closed s" shows "compact s"
  1.2211 -proof (unfold compact_def, clarify)
  1.2212 -  fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
  1.2213 -  obtain l r where r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
  1.2214 -    using bounded_imp_convergent_subsequence [OF `bounded s` `\<forall>n. f n \<in> s`] by auto
  1.2215 -  from f have fr: "\<forall>n. (f \<circ> r) n \<in> s" by simp
  1.2216 -  have "l \<in> s" using `closed s` fr l
  1.2217 -    unfolding closed_sequential_limits by blast
  1.2218 -  show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  1.2219 -    using `l \<in> s` r l by blast
  1.2220 -qed
  1.2221 -
  1.2222 -lemma subseq_bigger: assumes "subseq r" shows "n \<le> r n"
  1.2223 -proof(induct n)
  1.2224 -  show "0 \<le> r 0" by auto
  1.2225 -next
  1.2226 -  fix n assume "n \<le> r n"
  1.2227 -  moreover have "r n < r (Suc n)"
  1.2228 -    using assms [unfolded subseq_def] by auto
  1.2229 -  ultimately show "Suc n \<le> r (Suc n)" by auto
  1.2230 -qed
  1.2231 -
  1.2232 -lemma eventually_subseq:
  1.2233 -  assumes r: "subseq r"
  1.2234 -  shows "eventually P sequentially \<Longrightarrow> eventually (\<lambda>n. P (r n)) sequentially"
  1.2235 -unfolding eventually_sequentially
  1.2236 -by (metis subseq_bigger [OF r] le_trans)
  1.2237 -
  1.2238 -lemma lim_subseq:
  1.2239 -  "subseq r \<Longrightarrow> (s ---> l) sequentially \<Longrightarrow> ((s o r) ---> l) sequentially"
  1.2240 -unfolding tendsto_def eventually_sequentially o_def
  1.2241 -by (metis subseq_bigger le_trans)
  1.2242 -
  1.2243 -lemma num_Axiom: "EX! g. g 0 = e \<and> (\<forall>n. g (Suc n) = f n (g n))"
  1.2244 -  unfolding Ex1_def
  1.2245 -  apply (rule_tac x="nat_rec e f" in exI)
  1.2246 -  apply (rule conjI)+
  1.2247 -apply (rule def_nat_rec_0, simp)
  1.2248 -apply (rule allI, rule def_nat_rec_Suc, simp)
  1.2249 -apply (rule allI, rule impI, rule ext)
  1.2250 -apply (erule conjE)
  1.2251 -apply (induct_tac x)
  1.2252 -apply (simp add: nat_rec_0)
  1.2253 -apply (erule_tac x="n" in allE)
  1.2254 -apply (simp)
  1.2255 -done
  1.2256 -
  1.2257 -lemma convergent_bounded_increasing: fixes s ::"nat\<Rightarrow>real"
  1.2258 -  assumes "incseq s" and "\<forall>n. abs(s n) \<le> b"
  1.2259 -  shows "\<exists> l. \<forall>e::real>0. \<exists> N. \<forall>n \<ge> N.  abs(s n - l) < e"
  1.2260 -proof-
  1.2261 -  have "isUb UNIV (range s) b" using assms(2) and abs_le_D1 unfolding isUb_def and setle_def by auto
  1.2262 -  then obtain t where t:"isLub UNIV (range s) t" using reals_complete[of "range s" ] by auto
  1.2263 -  { fix e::real assume "e>0" and as:"\<forall>N. \<exists>n\<ge>N. \<not> \<bar>s n - t\<bar> < e"
  1.2264 -    { fix n::nat
  1.2265 -      obtain N where "N\<ge>n" and n:"\<bar>s N - t\<bar> \<ge> e" using as[THEN spec[where x=n]] by auto
  1.2266 -      have "t \<ge> s N" using isLub_isUb[OF t, unfolded isUb_def setle_def] by auto
  1.2267 -      with n have "s N \<le> t - e" using `e>0` by auto
  1.2268 -      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  }
  1.2269 -    hence "isUb UNIV (range s) (t - e)" unfolding isUb_def and setle_def by auto
  1.2270 -    hence False using isLub_le_isUb[OF t, of "t - e"] and `e>0` by auto  }
  1.2271 -  thus ?thesis by blast
  1.2272 -qed
  1.2273 -
  1.2274 -lemma convergent_bounded_monotone: fixes s::"nat \<Rightarrow> real"
  1.2275 -  assumes "\<forall>n. abs(s n) \<le> b" and "monoseq s"
  1.2276 -  shows "\<exists>l. \<forall>e::real>0. \<exists>N. \<forall>n\<ge>N. abs(s n - l) < e"
  1.2277 -  using convergent_bounded_increasing[of s b] assms using convergent_bounded_increasing[of "\<lambda>n. - s n" b]
  1.2278 -  unfolding monoseq_def incseq_def
  1.2279 -  apply auto unfolding minus_add_distrib[THEN sym, unfolded diff_minus[THEN sym]]
  1.2280 -  unfolding abs_minus_cancel by(rule_tac x="-l" in exI)auto
  1.2281 -
  1.2282 -lemma compact_real_lemma:
  1.2283 -  assumes "\<forall>n::nat. abs(s n) \<le> b"
  1.2284 -  shows "\<exists>(l::real) r. subseq r \<and> ((s \<circ> r) ---> l) sequentially"
  1.2285 -proof-
  1.2286 -  obtain r where r:"subseq r" "monoseq (\<lambda>n. s (r n))"
  1.2287 -    using seq_monosub[of s] by auto
  1.2288 -  thus ?thesis using convergent_bounded_monotone[of "\<lambda>n. s (r n)" b] and assms
  1.2289 -    unfolding tendsto_iff dist_norm eventually_sequentially by auto
  1.2290 -qed
  1.2291 -
  1.2292 -instance real :: heine_borel
  1.2293 -proof
  1.2294 -  fix s :: "real set" and f :: "nat \<Rightarrow> real"
  1.2295 -  assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
  1.2296 -  then obtain b where b: "\<forall>n. abs (f n) \<le> b"
  1.2297 -    unfolding bounded_iff by auto
  1.2298 -  obtain l :: real and r :: "nat \<Rightarrow> nat" where
  1.2299 -    r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
  1.2300 -    using compact_real_lemma [OF b] by auto
  1.2301 -  thus "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  1.2302 -    by auto
  1.2303 -qed
  1.2304 -
  1.2305 -lemma bounded_component: "bounded s \<Longrightarrow> bounded ((\<lambda>x. x $ i) ` s)"
  1.2306 -unfolding bounded_def
  1.2307 -apply clarify
  1.2308 -apply (rule_tac x="x $ i" in exI)
  1.2309 -apply (rule_tac x="e" in exI)
  1.2310 -apply clarify
  1.2311 -apply (rule order_trans [OF dist_nth_le], simp)
  1.2312 -done
  1.2313 -
  1.2314 -lemma compact_lemma:
  1.2315 -  fixes f :: "nat \<Rightarrow> 'a::heine_borel ^ 'n::finite"
  1.2316 -  assumes "bounded s" and "\<forall>n. f n \<in> s"
  1.2317 -  shows "\<forall>d.
  1.2318 -        \<exists>l r. subseq r \<and>
  1.2319 -        (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) $ i) (l $ i) < e) sequentially)"
  1.2320 -proof
  1.2321 -  fix d::"'n set" have "finite d" by simp
  1.2322 -  thus "\<exists>l::'a ^ 'n. \<exists>r. subseq r \<and>
  1.2323 -      (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) $ i) (l $ i) < e) sequentially)"
  1.2324 -  proof(induct d) case empty thus ?case unfolding subseq_def by auto
  1.2325 -  next case (insert k d)
  1.2326 -    have s': "bounded ((\<lambda>x. x $ k) ` s)" using `bounded s` by (rule bounded_component)
  1.2327 -    obtain l1::"'a^'n" and r1 where r1:"subseq r1" and lr1:"\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) $ i) (l1 $ i) < e) sequentially"
  1.2328 -      using insert(3) by auto
  1.2329 -    have f': "\<forall>n. f (r1 n) $ k \<in> (\<lambda>x. x $ k) ` s" using `\<forall>n. f n \<in> s` by simp
  1.2330 -    obtain l2 r2 where r2:"subseq r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) $ k) ---> l2) sequentially"
  1.2331 -      using bounded_imp_convergent_subsequence[OF s' f'] unfolding o_def by auto
  1.2332 -    def r \<equiv> "r1 \<circ> r2" have r:"subseq r"
  1.2333 -      using r1 and r2 unfolding r_def o_def subseq_def by auto
  1.2334 -    moreover
  1.2335 -    def l \<equiv> "(\<chi> i. if i = k then l2 else l1$i)::'a^'n"
  1.2336 -    { fix e::real assume "e>0"
  1.2337 -      from lr1 `e>0` have N1:"eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) $ i) (l1 $ i) < e) sequentially" by blast
  1.2338 -      from lr2 `e>0` have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) $ k) l2 < e) sequentially" by (rule tendstoD)
  1.2339 -      from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) $ i) (l1 $ i) < e) sequentially"
  1.2340 -        by (rule eventually_subseq)
  1.2341 -      have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) $ i) (l $ i) < e) sequentially"
  1.2342 -        using N1' N2 by (rule eventually_elim2, simp add: l_def r_def)
  1.2343 -    }
  1.2344 -    ultimately show ?case by auto
  1.2345 -  qed
  1.2346 -qed
  1.2347 -
  1.2348 -instance "^" :: (heine_borel, finite) heine_borel
  1.2349 -proof
  1.2350 -  fix s :: "('a ^ 'b) set" and f :: "nat \<Rightarrow> 'a ^ 'b"
  1.2351 -  assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
  1.2352 -  then obtain l r where r: "subseq r"
  1.2353 -    and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>UNIV. dist (f (r n) $ i) (l $ i) < e) sequentially"
  1.2354 -    using compact_lemma [OF s f] by blast
  1.2355 -  let ?d = "UNIV::'b set"
  1.2356 -  { fix e::real assume "e>0"
  1.2357 -    hence "0 < e / (real_of_nat (card ?d))"
  1.2358 -      using zero_less_card_finite using divide_pos_pos[of e, of "real_of_nat (card ?d)"] by auto
  1.2359 -    with l have "eventually (\<lambda>n. \<forall>i. dist (f (r n) $ i) (l $ i) < e / (real_of_nat (card ?d))) sequentially"
  1.2360 -      by simp
  1.2361 -    moreover
  1.2362 -    { fix n assume n: "\<forall>i. dist (f (r n) $ i) (l $ i) < e / (real_of_nat (card ?d))"
  1.2363 -      have "dist (f (r n)) l \<le> (\<Sum>i\<in>?d. dist (f (r n) $ i) (l $ i))"
  1.2364 -        unfolding dist_vector_def using zero_le_dist by (rule setL2_le_setsum)
  1.2365 -      also have "\<dots> < (\<Sum>i\<in>?d. e / (real_of_nat (card ?d)))"
  1.2366 -        by (rule setsum_strict_mono) (simp_all add: n)
  1.2367 -      finally have "dist (f (r n)) l < e" by simp
  1.2368 -    }
  1.2369 -    ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"
  1.2370 -      by (rule eventually_elim1)
  1.2371 -  }
  1.2372 -  hence *:"((f \<circ> r) ---> l) sequentially" unfolding o_def tendsto_iff by simp
  1.2373 -  with r show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" by auto
  1.2374 -qed
  1.2375 -
  1.2376 -lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst ` s)"
  1.2377 -unfolding bounded_def
  1.2378 -apply clarify
  1.2379 -apply (rule_tac x="a" in exI)
  1.2380 -apply (rule_tac x="e" in exI)
  1.2381 -apply clarsimp
  1.2382 -apply (drule (1) bspec)
  1.2383 -apply (simp add: dist_Pair_Pair)
  1.2384 -apply (erule order_trans [OF real_sqrt_sum_squares_ge1])
  1.2385 -done
  1.2386 -
  1.2387 -lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd ` s)"
  1.2388 -unfolding bounded_def
  1.2389 -apply clarify
  1.2390 -apply (rule_tac x="b" in exI)
  1.2391 -apply (rule_tac x="e" in exI)
  1.2392 -apply clarsimp
  1.2393 -apply (drule (1) bspec)
  1.2394 -apply (simp add: dist_Pair_Pair)
  1.2395 -apply (erule order_trans [OF real_sqrt_sum_squares_ge2])
  1.2396 -done
  1.2397 -
  1.2398 -instance "*" :: (heine_borel, heine_borel) heine_borel
  1.2399 -proof
  1.2400 -  fix s :: "('a * 'b) set" and f :: "nat \<Rightarrow> 'a * 'b"
  1.2401 -  assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
  1.2402 -  from s have s1: "bounded (fst ` s)" by (rule bounded_fst)
  1.2403 -  from f have f1: "\<forall>n. fst (f n) \<in> fst ` s" by simp
  1.2404 -  obtain l1 r1 where r1: "subseq r1"
  1.2405 -    and l1: "((\<lambda>n. fst (f (r1 n))) ---> l1) sequentially"
  1.2406 -    using bounded_imp_convergent_subsequence [OF s1 f1]
  1.2407 -    unfolding o_def by fast
  1.2408 -  from s have s2: "bounded (snd ` s)" by (rule bounded_snd)
  1.2409 -  from f have f2: "\<forall>n. snd (f (r1 n)) \<in> snd ` s" by simp
  1.2410 -  obtain l2 r2 where r2: "subseq r2"
  1.2411 -    and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) ---> l2) sequentially"
  1.2412 -    using bounded_imp_convergent_subsequence [OF s2 f2]
  1.2413 -    unfolding o_def by fast
  1.2414 -  have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) ---> l1) sequentially"
  1.2415 -    using lim_subseq [OF r2 l1] unfolding o_def .
  1.2416 -  have l: "((f \<circ> (r1 \<circ> r2)) ---> (l1, l2)) sequentially"
  1.2417 -    using tendsto_Pair [OF l1' l2] unfolding o_def by simp
  1.2418 -  have r: "subseq (r1 \<circ> r2)"
  1.2419 -    using r1 r2 unfolding subseq_def by simp
  1.2420 -  show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  1.2421 -    using l r by fast
  1.2422 -qed
  1.2423 -
  1.2424 -subsection{* Completeness. *}
  1.2425 -
  1.2426 -lemma cauchy_def:
  1.2427 -  "Cauchy s \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m n. m \<ge> N \<and> n \<ge> N --> dist(s m)(s n) < e)"
  1.2428 -unfolding Cauchy_def by blast
  1.2429 -
  1.2430 -definition
  1.2431 -  complete :: "'a::metric_space set \<Rightarrow> bool" where
  1.2432 -  "complete s \<longleftrightarrow> (\<forall>f. (\<forall>n. f n \<in> s) \<and> Cauchy f
  1.2433 -                      --> (\<exists>l \<in> s. (f ---> l) sequentially))"
  1.2434 -
  1.2435 -lemma cauchy: "Cauchy s \<longleftrightarrow> (\<forall>e>0.\<exists> N::nat. \<forall>n\<ge>N. dist(s n)(s N) < e)" (is "?lhs = ?rhs")
  1.2436 -proof-
  1.2437 -  { assume ?rhs
  1.2438 -    { fix e::real
  1.2439 -      assume "e>0"
  1.2440 -      with `?rhs` obtain N where N:"\<forall>n\<ge>N. dist (s n) (s N) < e/2"
  1.2441 -        by (erule_tac x="e/2" in allE) auto
  1.2442 -      { fix n m
  1.2443 -        assume nm:"N \<le> m \<and> N \<le> n"
  1.2444 -        hence "dist (s m) (s n) < e" using N
  1.2445 -          using dist_triangle_half_l[of "s m" "s N" "e" "s n"]
  1.2446 -          by blast
  1.2447 -      }
  1.2448 -      hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < e"
  1.2449 -        by blast
  1.2450 -    }
  1.2451 -    hence ?lhs
  1.2452 -      unfolding cauchy_def
  1.2453 -      by blast
  1.2454 -  }
  1.2455 -  thus ?thesis
  1.2456 -    unfolding cauchy_def
  1.2457 -    using dist_triangle_half_l
  1.2458 -    by blast
  1.2459 -qed
  1.2460 -
  1.2461 -lemma convergent_imp_cauchy:
  1.2462 - "(s ---> l) sequentially ==> Cauchy s"
  1.2463 -proof(simp only: cauchy_def, rule, rule)
  1.2464 -  fix e::real assume "e>0" "(s ---> l) sequentially"
  1.2465 -  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
  1.2466 -  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
  1.2467 -qed
  1.2468 -
  1.2469 -lemma cauchy_imp_bounded: assumes "Cauchy s" shows "bounded {y. (\<exists>n::nat. y = s n)}"
  1.2470 -proof-
  1.2471 -  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
  1.2472 -  hence N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto
  1.2473 -  moreover
  1.2474 -  have "bounded (s ` {0..N})" using finite_imp_bounded[of "s ` {1..N}"] by auto
  1.2475 -  then obtain a where a:"\<forall>x\<in>s ` {0..N}. dist (s N) x \<le> a"
  1.2476 -    unfolding bounded_any_center [where a="s N"] by auto
  1.2477 -  ultimately show "?thesis"
  1.2478 -    unfolding bounded_any_center [where a="s N"]
  1.2479 -    apply(rule_tac x="max a 1" in exI) apply auto
  1.2480 -    apply(erule_tac x=n in allE) apply(erule_tac x=n in ballE) by auto
  1.2481 -qed
  1.2482 -
  1.2483 -lemma compact_imp_complete: assumes "compact s" shows "complete s"
  1.2484 -proof-
  1.2485 -  { fix f assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"
  1.2486 -    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
  1.2487 -
  1.2488 -    note lr' = subseq_bigger [OF lr(2)]
  1.2489 -
  1.2490 -    { fix e::real assume "e>0"
  1.2491 -      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
  1.2492 -      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
  1.2493 -      { fix n::nat assume n:"n \<ge> max N M"
  1.2494 -        have "dist ((f \<circ> r) n) l < e/2" using n M by auto
  1.2495 -        moreover have "r n \<ge> N" using lr'[of n] n by auto
  1.2496 -        hence "dist (f n) ((f \<circ> r) n) < e / 2" using N using n by auto
  1.2497 -        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)  }
  1.2498 -      hence "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast  }
  1.2499 -    hence "\<exists>l\<in>s. (f ---> l) sequentially" using `l\<in>s` unfolding Lim_sequentially by auto  }
  1.2500 -  thus ?thesis unfolding complete_def by auto
  1.2501 -qed
  1.2502 -
  1.2503 -instance heine_borel < complete_space
  1.2504 -proof
  1.2505 -  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
  1.2506 -  hence "bounded (range f)" unfolding image_def
  1.2507 -    using cauchy_imp_bounded [of f] by auto
  1.2508 -  hence "compact (closure (range f))"
  1.2509 -    using bounded_closed_imp_compact [of "closure (range f)"] by auto
  1.2510 -  hence "complete (closure (range f))"
  1.2511 -    using compact_imp_complete by auto
  1.2512 -  moreover have "\<forall>n. f n \<in> closure (range f)"
  1.2513 -    using closure_subset [of "range f"] by auto
  1.2514 -  ultimately have "\<exists>l\<in>closure (range f). (f ---> l) sequentially"
  1.2515 -    using `Cauchy f` unfolding complete_def by auto
  1.2516 -  then show "convergent f"
  1.2517 -    unfolding convergent_def LIMSEQ_conv_tendsto [symmetric] by auto
  1.2518 -qed
  1.2519 -
  1.2520 -lemma complete_univ: "complete (UNIV :: 'a::complete_space set)"
  1.2521 -proof(simp add: complete_def, rule, rule)
  1.2522 -  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
  1.2523 -  hence "convergent f" by (rule Cauchy_convergent)
  1.2524 -  hence "\<exists>l. f ----> l" unfolding convergent_def .  
  1.2525 -  thus "\<exists>l. (f ---> l) sequentially" unfolding LIMSEQ_conv_tendsto .
  1.2526 -qed
  1.2527 -
  1.2528 -lemma complete_imp_closed: assumes "complete s" shows "closed s"
  1.2529 -proof -
  1.2530 -  { fix x assume "x islimpt s"
  1.2531 -    then obtain f where f: "\<forall>n. f n \<in> s - {x}" "(f ---> x) sequentially"
  1.2532 -      unfolding islimpt_sequential by auto
  1.2533 -    then obtain l where l: "l\<in>s" "(f ---> l) sequentially"
  1.2534 -      using `complete s`[unfolded complete_def] using convergent_imp_cauchy[of f x] by auto
  1.2535 -    hence "x \<in> s"  using Lim_unique[of sequentially f l x] trivial_limit_sequentially f(2) by auto
  1.2536 -  }
  1.2537 -  thus "closed s" unfolding closed_limpt by auto
  1.2538 -qed
  1.2539 -
  1.2540 -lemma complete_eq_closed:
  1.2541 -  fixes s :: "'a::complete_space set"
  1.2542 -  shows "complete s \<longleftrightarrow> closed s" (is "?lhs = ?rhs")
  1.2543 -proof
  1.2544 -  assume ?lhs thus ?rhs by (rule complete_imp_closed)
  1.2545 -next
  1.2546 -  assume ?rhs
  1.2547 -  { fix f assume as:"\<forall>n::nat. f n \<in> s" "Cauchy f"
  1.2548 -    then obtain l where "(f ---> l) sequentially" using complete_univ[unfolded complete_def, THEN spec[where x=f]] by auto
  1.2549 -    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  }
  1.2550 -  thus ?lhs unfolding complete_def by auto
  1.2551 -qed
  1.2552 -
  1.2553 -lemma convergent_eq_cauchy:
  1.2554 -  fixes s :: "nat \<Rightarrow> 'a::complete_space"
  1.2555 -  shows "(\<exists>l. (s ---> l) sequentially) \<longleftrightarrow> Cauchy s" (is "?lhs = ?rhs")
  1.2556 -proof
  1.2557 -  assume ?lhs then obtain l where "(s ---> l) sequentially" by auto
  1.2558 -  thus ?rhs using convergent_imp_cauchy by auto
  1.2559 -next
  1.2560 -  assume ?rhs thus ?lhs using complete_univ[unfolded complete_def, THEN spec[where x=s]] by auto
  1.2561 -qed
  1.2562 -
  1.2563 -lemma convergent_imp_bounded:
  1.2564 -  fixes s :: "nat \<Rightarrow> 'a::metric_space"
  1.2565 -  shows "(s ---> l) sequentially ==> bounded (s ` (UNIV::(nat set)))"
  1.2566 -  using convergent_imp_cauchy[of s]
  1.2567 -  using cauchy_imp_bounded[of s]
  1.2568 -  unfolding image_def
  1.2569 -  by auto
  1.2570 -
  1.2571 -subsection{* Total boundedness. *}
  1.2572 -
  1.2573 -fun helper_1::"('a::metric_space set) \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> 'a" where
  1.2574 -  "helper_1 s e n = (SOME y::'a. y \<in> s \<and> (\<forall>m<n. \<not> (dist (helper_1 s e m) y < e)))"
  1.2575 -declare helper_1.simps[simp del]
  1.2576 -
  1.2577 -lemma compact_imp_totally_bounded:
  1.2578 -  assumes "compact s"
  1.2579 -  shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` k))"
  1.2580 -proof(rule, rule, rule ccontr)
  1.2581 -  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)"
  1.2582 -  def x \<equiv> "helper_1 s e"
  1.2583 -  { fix n
  1.2584 -    have "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)"
  1.2585 -    proof(induct_tac rule:nat_less_induct)
  1.2586 -      fix n  def Q \<equiv> "(\<lambda>y. y \<in> s \<and> (\<forall>m<n. \<not> dist (x m) y < e))"
  1.2587 -      assume as:"\<forall>m<n. x m \<in> s \<and> (\<forall>ma<m. \<not> dist (x ma) (x m) < e)"
  1.2588 -      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
  1.2589 -      then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x ` {0..<n}. ball x e)" unfolding subset_eq by auto
  1.2590 -      have "Q (x n)" unfolding x_def and helper_1.simps[of s e n]
  1.2591 -        apply(rule someI2[where a=z]) unfolding x_def[symmetric] and Q_def using z by auto
  1.2592 -      thus "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)" unfolding Q_def by auto
  1.2593 -    qed }
  1.2594 -  hence "\<forall>n::nat. x n \<in> s" and x:"\<forall>n. \<forall>m < n. \<not> (dist (x m) (x n) < e)" by blast+
  1.2595 -  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
  1.2596 -  from this(3) have "Cauchy (x \<circ> r)" using convergent_imp_cauchy by auto
  1.2597 -  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
  1.2598 -  show False
  1.2599 -    using N[THEN spec[where x=N], THEN spec[where x="N+1"]]
  1.2600 -    using r[unfolded subseq_def, THEN spec[where x=N], THEN spec[where x="N+1"]]
  1.2601 -    using x[THEN spec[where x="r (N+1)"], THEN spec[where x="r (N)"]] by auto
  1.2602 -qed
  1.2603 -
  1.2604 -subsection{* Heine-Borel theorem (following Burkill \& Burkill vol. 2) *}
  1.2605 -
  1.2606 -lemma heine_borel_lemma: fixes s::"'a::metric_space set"
  1.2607 -  assumes "compact s"  "s \<subseteq> (\<Union> t)"  "\<forall>b \<in> t. open b"
  1.2608 -  shows "\<exists>e>0. \<forall>x \<in> s. \<exists>b \<in> t. ball x e \<subseteq> b"
  1.2609 -proof(rule ccontr)
  1.2610 -  assume "\<not> (\<exists>e>0. \<forall>x\<in>s. \<exists>b\<in>t. ball x e \<subseteq> b)"
  1.2611 -  hence cont:"\<forall>e>0. \<exists>x\<in>s. \<forall>xa\<in>t. \<not> (ball x e \<subseteq> xa)" by auto
  1.2612 -  { fix n::nat
  1.2613 -    have "1 / real (n + 1) > 0" by auto
  1.2614 -    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 }
  1.2615 -  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
  1.2616 -  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)"
  1.2617 -    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
  1.2618 -
  1.2619 -  then obtain l r where l:"l\<in>s" and r:"subseq r" and lr:"((f \<circ> r) ---> l) sequentially"
  1.2620 -    using assms(1)[unfolded compact_def, THEN spec[where x=f]] by auto
  1.2621 -
  1.2622 -  obtain b where "l\<in>b" "b\<in>t" using assms(2) and l by auto
  1.2623 -  then obtain e where "e>0" and e:"\<forall>z. dist z l < e \<longrightarrow> z\<in>b"
  1.2624 -    using assms(3)[THEN bspec[where x=b]] unfolding open_dist by auto
  1.2625 -
  1.2626 -  then obtain N1 where N1:"\<forall>n\<ge>N1. dist ((f \<circ> r) n) l < e / 2"
  1.2627 -    using lr[unfolded Lim_sequentially, THEN spec[where x="e/2"]] by auto
  1.2628 -
  1.2629 -  obtain N2::nat where N2:"N2>0" "inverse (real N2) < e /2" using real_arch_inv[of "e/2"] and `e>0` by auto
  1.2630 -  have N2':"inverse (real (r (N1 + N2) +1 )) < e/2"
  1.2631 -    apply(rule order_less_trans) apply(rule less_imp_inverse_less) using N2
  1.2632 -    using subseq_bigger[OF r, of "N1 + N2"] by auto
  1.2633 -
  1.2634 -  def x \<equiv> "(f (r (N1 + N2)))"
  1.2635 -  have x:"\<not> ball x (inverse (real (r (N1 + N2) + 1))) \<subseteq> b" unfolding x_def
  1.2636 -    using f[THEN spec[where x="r (N1 + N2)"]] using `b\<in>t` by auto
  1.2637 -  have "\<exists>y\<in>ball x (inverse (real (r (N1 + N2) + 1))). y\<notin>b" apply(rule ccontr) using x by auto
  1.2638 -  then obtain y where y:"y \<in> ball x (inverse (real (r (N1 + N2) + 1)))" "y \<notin> b" by auto
  1.2639 -
  1.2640 -  have "dist x l < e/2" using N1 unfolding x_def o_def by auto
  1.2641 -  hence "dist y l < e" using y N2' using dist_triangle[of y l x]by (auto simp add:dist_commute)
  1.2642 -
  1.2643 -  thus False using e and `y\<notin>b` by auto
  1.2644 -qed
  1.2645 -
  1.2646 -lemma compact_imp_heine_borel: "compact s ==> (\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f)
  1.2647 -               \<longrightarrow> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f')))"
  1.2648 -proof clarify
  1.2649 -  fix f assume "compact s" " \<forall>t\<in>f. open t" "s \<subseteq> \<Union>f"
  1.2650 -  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
  1.2651 -  hence "\<forall>x\<in>s. \<exists>b. b\<in>f \<and> ball x e \<subseteq> b" by auto
  1.2652 -  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
  1.2653 -  then obtain  bb where bb:"\<forall>x\<in>s. (bb x) \<in> f \<and> ball x e \<subseteq> (bb x)" by blast
  1.2654 -
  1.2655 -  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
  1.2656 -  then obtain k where k:"finite k" "k \<subseteq> s" "s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k" by auto
  1.2657 -
  1.2658 -  have "finite (bb ` k)" using k(1) by auto
  1.2659 -  moreover
  1.2660 -  { fix x assume "x\<in>s"
  1.2661 -    hence "x\<in>\<Union>(\<lambda>x. ball x e) ` k" using k(3)  unfolding subset_eq by auto
  1.2662 -    hence "\<exists>X\<in>bb ` k. x \<in> X" using bb k(2) by blast
  1.2663 -    hence "x \<in> \<Union>(bb ` k)" using  Union_iff[of x "bb ` k"] by auto
  1.2664 -  }
  1.2665 -  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
  1.2666 -qed
  1.2667 -
  1.2668 -subsection{* Bolzano-Weierstrass property. *}
  1.2669 -
  1.2670 -lemma heine_borel_imp_bolzano_weierstrass:
  1.2671 -  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'))"
  1.2672 -          "infinite t"  "t \<subseteq> s"
  1.2673 -  shows "\<exists>x \<in> s. x islimpt t"
  1.2674 -proof(rule ccontr)
  1.2675 -  assume "\<not> (\<exists>x \<in> s. x islimpt t)"
  1.2676 -  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
  1.2677 -    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
  1.2678 -  obtain g where g:"g\<subseteq>{t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"
  1.2679 -    using assms(1)[THEN spec[where x="{t. \<exists>x. x\<in>s \<and> t = f x}"]] using f by auto
  1.2680 -  from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa" by auto
  1.2681 -  { fix x y assume "x\<in>t" "y\<in>t" "f x = f y"
  1.2682 -    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
  1.2683 -    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  }
  1.2684 -  hence "infinite (f ` t)" using assms(2) using finite_imageD[unfolded inj_on_def, of f t] by auto
  1.2685 -  moreover
  1.2686 -  { fix x assume "x\<in>t" "f x \<notin> g"
  1.2687 -    from g(3) assms(3) `x\<in>t` obtain h where "h\<in>g" and "x\<in>h" by auto
  1.2688 -    then obtain y where "y\<in>s" "h = f y" using g'[THEN bspec[where x=h]] by auto
  1.2689 -    hence "y = x" using f[THEN bspec[where x=y]] and `x\<in>t` and `x\<in>h`[unfolded `h = f y`] by auto
  1.2690 -    hence False using `f x \<notin> g` `h\<in>g` unfolding `h = f y` by auto  }
  1.2691 -  hence "f ` t \<subseteq> g" by auto
  1.2692 -  ultimately show False using g(2) using finite_subset by auto
  1.2693 -qed
  1.2694 -
  1.2695 -subsection{* Complete the chain of compactness variants. *}
  1.2696 -
  1.2697 -primrec helper_2::"(real \<Rightarrow> 'a::metric_space) \<Rightarrow> nat \<Rightarrow> 'a" where
  1.2698 -  "helper_2 beyond 0 = beyond 0" |
  1.2699 -  "helper_2 beyond (Suc n) = beyond (dist undefined (helper_2 beyond n) + 1 )"
  1.2700 -
  1.2701 -lemma bolzano_weierstrass_imp_bounded: fixes s::"'a::metric_space set"
  1.2702 -  assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
  1.2703 -  shows "bounded s"
  1.2704 -proof(rule ccontr)
  1.2705 -  assume "\<not> bounded s"
  1.2706 -  then obtain beyond where "\<forall>a. beyond a \<in>s \<and> \<not> dist undefined (beyond a) \<le> a"
  1.2707 -    unfolding bounded_any_center [where a=undefined]
  1.2708 -    apply simp using choice[of "\<lambda>a x. x\<in>s \<and> \<not> dist undefined x \<le> a"] by auto
  1.2709 -  hence beyond:"\<And>a. beyond a \<in>s" "\<And>a. dist undefined (beyond a) > a"
  1.2710 -    unfolding linorder_not_le by auto
  1.2711 -  def x \<equiv> "helper_2 beyond"
  1.2712 -
  1.2713 -  { fix m n ::nat assume "m<n"
  1.2714 -    hence "dist undefined (x m) + 1 < dist undefined (x n)"
  1.2715 -    proof(induct n)
  1.2716 -      case 0 thus ?case by auto
  1.2717 -    next
  1.2718 -      case (Suc n)
  1.2719 -      have *:"dist undefined (x n) + 1 < dist undefined (x (Suc n))"
  1.2720 -        unfolding x_def and helper_2.simps
  1.2721 -        using beyond(2)[of "dist undefined (helper_2 beyond n) + 1"] by auto
  1.2722 -      thus ?case proof(cases "m < n")
  1.2723 -        case True thus ?thesis using Suc and * by auto
  1.2724 -      next
  1.2725 -        case False hence "m = n" using Suc(2) by auto
  1.2726 -        thus ?thesis using * by auto
  1.2727 -      qed
  1.2728 -    qed  } note * = this
  1.2729 -  { fix m n ::nat assume "m\<noteq>n"
  1.2730 -    have "1 < dist (x m) (x n)"
  1.2731 -    proof(cases "m<n")
  1.2732 -      case True
  1.2733 -      hence "1 < dist undefined (x n) - dist undefined (x m)" using *[of m n] by auto
  1.2734 -      thus ?thesis using dist_triangle [of undefined "x n" "x m"] by arith
  1.2735 -    next
  1.2736 -      case False hence "n<m" using `m\<noteq>n` by auto
  1.2737 -      hence "1 < dist undefined (x m) - dist undefined (x n)" using *[of n m] by auto
  1.2738 -      thus ?thesis using dist_triangle2 [of undefined "x m" "x n"] by arith
  1.2739 -    qed  } note ** = this
  1.2740 -  { fix a b assume "x a = x b" "a \<noteq> b"
  1.2741 -    hence False using **[of a b] by auto  }
  1.2742 -  hence "inj x" unfolding inj_on_def by auto
  1.2743 -  moreover
  1.2744 -  { fix n::nat
  1.2745 -    have "x n \<in> s"
  1.2746 -    proof(cases "n = 0")
  1.2747 -      case True thus ?thesis unfolding x_def using beyond by auto
  1.2748 -    next
  1.2749 -      case False then obtain z where "n = Suc z" using not0_implies_Suc by auto
  1.2750 -      thus ?thesis unfolding x_def using beyond by auto
  1.2751 -    qed  }
  1.2752 -  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
  1.2753 -
  1.2754 -  then obtain l where "l\<in>s" and l:"l islimpt range x" using assms[THEN spec[where x="range x"]] by auto
  1.2755 -  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
  1.2756 -  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"]]
  1.2757 -    unfolding dist_nz by auto
  1.2758 -  show False using y and z and dist_triangle_half_l[of "x y" l 1 "x z"] and **[of y z] by auto
  1.2759 -qed
  1.2760 -
  1.2761 -lemma sequence_infinite_lemma:
  1.2762 -  fixes l :: "'a::metric_space" (* TODO: generalize *)
  1.2763 -  assumes "\<forall>n::nat. (f n  \<noteq> l)"  "(f ---> l) sequentially"
  1.2764 -  shows "infinite {y. (\<exists> n. y = f n)}"
  1.2765 -proof(rule ccontr)
  1.2766 -  let ?A = "(\<lambda>x. dist x l) ` {y. \<exists>n. y = f n}"
  1.2767 -  assume "\<not> infinite {y. \<exists>n. y = f n}"
  1.2768 -  hence **:"finite ?A" "?A \<noteq> {}" by auto
  1.2769 -  obtain k where k:"dist (f k) l = Min ?A" using Min_in[OF **] by auto
  1.2770 -  have "0 < Min ?A" using assms(1) unfolding dist_nz unfolding Min_gr_iff[OF **] by auto
  1.2771 -  then obtain N where "dist (f N) l < Min ?A" using assms(2)[unfolded Lim_sequentially, THEN spec[where x="Min ?A"]] by auto
  1.2772 -  moreover have "dist (f N) l \<in> ?A" by auto
  1.2773 -  ultimately show False using Min_le[OF **(1), of "dist (f N) l"] by auto
  1.2774 -qed
  1.2775 -
  1.2776 -lemma sequence_unique_limpt:
  1.2777 -  fixes l :: "'a::metric_space" (* TODO: generalize *)
  1.2778 -  assumes "\<forall>n::nat. (f n \<noteq> l)"  "(f ---> l) sequentially"  "l' islimpt {y.  (\<exists>n. y = f n)}"
  1.2779 -  shows "l' = l"
  1.2780 -proof(rule ccontr)
  1.2781 -  def e \<equiv> "dist l' l"
  1.2782 -  assume "l' \<noteq> l" hence "e>0" unfolding dist_nz e_def by auto
  1.2783 -  then obtain N::nat where N:"\<forall>n\<ge>N. dist (f n) l < e / 2"
  1.2784 -    using assms(2)[unfolded Lim_sequentially, THEN spec[where x="e/2"]] by auto
  1.2785 -  def d \<equiv> "Min (insert (e/2) ((\<lambda>n. if dist (f n) l' = 0 then e/2 else dist (f n) l') ` {0 .. N}))"
  1.2786 -  have "d>0" using `e>0` unfolding d_def e_def using zero_le_dist[of _ l', unfolded order_le_less] by auto
  1.2787 -  obtain k where k:"f k \<noteq> l'"  "dist (f k) l' < d" using `d>0` and assms(3)[unfolded islimpt_approachable, THEN spec[where x="d"]] by auto
  1.2788 -  have "k\<ge>N" using k(1)[unfolded dist_nz] using k(2)[unfolded d_def]
  1.2789 -    by force
  1.2790 -  hence "dist l' l < e" using N[THEN spec[where x=k]] using k(2)[unfolded d_def] and dist_triangle_half_r[of "f k" l' e l] by auto
  1.2791 -  thus False unfolding e_def by auto
  1.2792 -qed
  1.2793 -
  1.2794 -lemma bolzano_weierstrass_imp_closed:
  1.2795 -  fixes s :: "'a::metric_space set" (* TODO: can this be generalized? *)
  1.2796 -  assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
  1.2797 -  shows "closed s"
  1.2798 -proof-
  1.2799 -  { fix x l assume as: "\<forall>n::nat. x n \<in> s" "(x ---> l) sequentially"
  1.2800 -    hence "l \<in> s"
  1.2801 -    proof(cases "\<forall>n. x n \<noteq> l")
  1.2802 -      case False thus "l\<in>s" using as(1) by auto
  1.2803 -    next
  1.2804 -      case True note cas = this
  1.2805 -      with as(2) have "infinite {y. \<exists>n. y = x n}" using sequence_infinite_lemma[of x l] by auto
  1.2806 -      then obtain l' where "l'\<in>s" "l' islimpt {y. \<exists>n. y = x n}" using assms[THEN spec[where x="{y. \<exists>n. y = x n}"]] as(1) by auto
  1.2807 -      thus "l\<in>s" using sequence_unique_limpt[of x l l'] using as cas by auto
  1.2808 -    qed  }
  1.2809 -  thus ?thesis unfolding closed_sequential_limits by fast
  1.2810 -qed
  1.2811 -
  1.2812 -text{* Hence express everything as an equivalence.   *}
  1.2813 -
  1.2814 -lemma compact_eq_heine_borel:
  1.2815 -  fixes s :: "'a::heine_borel set"
  1.2816 -  shows "compact s \<longleftrightarrow>
  1.2817 -           (\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f)
  1.2818 -               --> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f')))" (is "?lhs = ?rhs")
  1.2819 -proof
  1.2820 -  assume ?lhs thus ?rhs using compact_imp_heine_borel[of s] by blast
  1.2821 -next
  1.2822 -  assume ?rhs
  1.2823 -  hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x\<in>s. x islimpt t)"
  1.2824 -    by (blast intro: heine_borel_imp_bolzano_weierstrass[of s])
  1.2825 -  thus ?lhs using bolzano_weierstrass_imp_bounded[of s] bolzano_weierstrass_imp_closed[of s] bounded_closed_imp_compact[of s] by blast
  1.2826 -qed
  1.2827 -
  1.2828 -lemma compact_eq_bolzano_weierstrass:
  1.2829 -  fixes s :: "'a::heine_borel set"
  1.2830 -  shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))" (is "?lhs = ?rhs")
  1.2831 -proof
  1.2832 -  assume ?lhs thus ?rhs unfolding compact_eq_heine_borel using heine_borel_imp_bolzano_weierstrass[of s] by auto
  1.2833 -next
  1.2834 -  assume ?rhs thus ?lhs using bolzano_weierstrass_imp_bounded bolzano_weierstrass_imp_closed bounded_closed_imp_compact by auto
  1.2835 -qed
  1.2836 -
  1.2837 -lemma compact_eq_bounded_closed:
  1.2838 -  fixes s :: "'a::heine_borel set"
  1.2839 -  shows "compact s \<longleftrightarrow> bounded s \<and> closed s"  (is "?lhs = ?rhs")
  1.2840 -proof
  1.2841 -  assume ?lhs thus ?rhs unfolding compact_eq_bolzano_weierstrass using bolzano_weierstrass_imp_bounded bolzano_weierstrass_imp_closed by auto
  1.2842 -next
  1.2843 -  assume ?rhs thus ?lhs using bounded_closed_imp_compact by auto
  1.2844 -qed
  1.2845 -
  1.2846 -lemma compact_imp_bounded:
  1.2847 -  fixes s :: "'a::metric_space set"
  1.2848 -  shows "compact s ==> bounded s"
  1.2849 -proof -
  1.2850 -  assume "compact s"
  1.2851 -  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')"
  1.2852 -    by (rule compact_imp_heine_borel)
  1.2853 -  hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"
  1.2854 -    using heine_borel_imp_bolzano_weierstrass[of s] by auto
  1.2855 -  thus "bounded s"
  1.2856 -    by (rule bolzano_weierstrass_imp_bounded)
  1.2857 -qed
  1.2858 -
  1.2859 -lemma compact_imp_closed:
  1.2860 -  fixes s :: "'a::metric_space set"
  1.2861 -  shows "compact s ==> closed s"
  1.2862 -proof -
  1.2863 -  assume "compact s"
  1.2864 -  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')"
  1.2865 -    by (rule compact_imp_heine_borel)
  1.2866 -  hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"
  1.2867 -    using heine_borel_imp_bolzano_weierstrass[of s] by auto
  1.2868 -  thus "closed s"
  1.2869 -    by (rule bolzano_weierstrass_imp_closed)
  1.2870 -qed
  1.2871 -
  1.2872 -text{* In particular, some common special cases. *}
  1.2873 -
  1.2874 -lemma compact_empty[simp]:
  1.2875 - "compact {}"
  1.2876 -  unfolding compact_def
  1.2877 -  by simp
  1.2878 -
  1.2879 -(* TODO: can any of the next 3 lemmas be generalized to metric spaces? *)
  1.2880 -
  1.2881 -  (* FIXME : Rename *)
  1.2882 -lemma compact_union[intro]:
  1.2883 -  fixes s t :: "'a::heine_borel set"
  1.2884 -  shows "compact s \<Longrightarrow> compact t ==> compact (s \<union> t)"
  1.2885 -  unfolding compact_eq_bounded_closed
  1.2886 -  using bounded_Un[of s t]
  1.2887 -  using closed_Un[of s t]
  1.2888 -  by simp
  1.2889 -
  1.2890 -lemma compact_inter[intro]:
  1.2891 -  fixes s t :: "'a::heine_borel set"
  1.2892 -  shows "compact s \<Longrightarrow> compact t ==> compact (s \<inter> t)"
  1.2893 -  unfolding compact_eq_bounded_closed
  1.2894 -  using bounded_Int[of s t]
  1.2895 -  using closed_Int[of s t]
  1.2896 -  by simp
  1.2897 -
  1.2898 -lemma compact_inter_closed[intro]:
  1.2899 -  fixes s t :: "'a::heine_borel set"
  1.2900 -  shows "compact s \<Longrightarrow> closed t ==> compact (s \<inter> t)"
  1.2901 -  unfolding compact_eq_bounded_closed
  1.2902 -  using closed_Int[of s t]
  1.2903 -  using bounded_subset[of "s \<inter> t" s]
  1.2904 -  by blast
  1.2905 -
  1.2906 -lemma closed_inter_compact[intro]:
  1.2907 -  fixes s t :: "'a::heine_borel set"
  1.2908 -  shows "closed s \<Longrightarrow> compact t ==> compact (s \<inter> t)"
  1.2909 -proof-
  1.2910 -  assume "closed s" "compact t"
  1.2911 -  moreover
  1.2912 -  have "s \<inter> t = t \<inter> s" by auto ultimately
  1.2913 -  show ?thesis
  1.2914 -    using compact_inter_closed[of t s]
  1.2915 -    by auto
  1.2916 -qed
  1.2917 -
  1.2918 -lemma closed_sing [simp]:
  1.2919 -  fixes a :: "'a::metric_space"
  1.2920 -  shows "closed {a}"
  1.2921 -  apply (clarsimp simp add: closed_def open_dist)
  1.2922 -  apply (rule ccontr)
  1.2923 -  apply (drule_tac x="dist x a" in spec)
  1.2924 -  apply (simp add: dist_nz dist_commute)
  1.2925 -  done
  1.2926 -
  1.2927 -lemma finite_imp_closed:
  1.2928 -  fixes s :: "'a::metric_space set"
  1.2929 -  shows "finite s ==> closed s"
  1.2930 -proof (induct set: finite)
  1.2931 -  case empty show "closed {}" by simp
  1.2932 -next
  1.2933 -  case (insert x F)
  1.2934 -  hence "closed ({x} \<union> F)" by (simp only: closed_Un closed_sing)
  1.2935 -  thus "closed (insert x F)" by simp
  1.2936 -qed
  1.2937 -
  1.2938 -lemma finite_imp_compact:
  1.2939 -  fixes s :: "'a::heine_borel set"
  1.2940 -  shows "finite s ==> compact s"
  1.2941 -  unfolding compact_eq_bounded_closed
  1.2942 -  using finite_imp_closed finite_imp_bounded
  1.2943 -  by blast
  1.2944 -
  1.2945 -lemma compact_sing [simp]: "compact {a}"
  1.2946 -  unfolding compact_def o_def subseq_def
  1.2947 -  by (auto simp add: tendsto_const)
  1.2948 -
  1.2949 -lemma compact_cball[simp]:
  1.2950 -  fixes x :: "'a::heine_borel"
  1.2951 -  shows "compact(cball x e)"
  1.2952 -  using compact_eq_bounded_closed bounded_cball closed_cball
  1.2953 -  by blast
  1.2954 -
  1.2955 -lemma compact_frontier_bounded[intro]:
  1.2956 -  fixes s :: "'a::heine_borel set"
  1.2957 -  shows "bounded s ==> compact(frontier s)"
  1.2958 -  unfolding frontier_def
  1.2959 -  using compact_eq_bounded_closed
  1.2960 -  by blast
  1.2961 -
  1.2962 -lemma compact_frontier[intro]:
  1.2963 -  fixes s :: "'a::heine_borel set"
  1.2964 -  shows "compact s ==> compact (frontier s)"
  1.2965 -  using compact_eq_bounded_closed compact_frontier_bounded
  1.2966 -  by blast
  1.2967 -
  1.2968 -lemma frontier_subset_compact:
  1.2969 -  fixes s :: "'a::heine_borel set"
  1.2970 -  shows "compact s ==> frontier s \<subseteq> s"
  1.2971 -  using frontier_subset_closed compact_eq_bounded_closed
  1.2972 -  by blast
  1.2973 -
  1.2974 -lemma open_delete:
  1.2975 -  fixes s :: "'a::metric_space set"
  1.2976 -  shows "open s ==> open(s - {x})"
  1.2977 -  using open_Diff[of s "{x}"] closed_sing
  1.2978 -  by blast
  1.2979 -
  1.2980 -text{* Finite intersection property. I could make it an equivalence in fact. *}
  1.2981 -
  1.2982 -lemma compact_imp_fip:
  1.2983 -  fixes s :: "'a::heine_borel set"
  1.2984 -  assumes "compact s"  "\<forall>t \<in> f. closed t"
  1.2985 -        "\<forall>f'. finite f' \<and> f' \<subseteq> f --> (s \<inter> (\<Inter> f') \<noteq> {})"
  1.2986 -  shows "s \<inter> (\<Inter> f) \<noteq> {}"
  1.2987 -proof
  1.2988 -  assume as:"s \<inter> (\<Inter> f) = {}"
  1.2989 -  hence "s \<subseteq> \<Union>op - UNIV ` f" by auto
  1.2990 -  moreover have "Ball (op - UNIV ` f) open" using open_Diff closed_Diff using assms(2) by auto
  1.2991 -  ultimately obtain f' where f':"f' \<subseteq> op - UNIV ` f"  "finite f'"  "s \<subseteq> \<Union>f'" using assms(1)[unfolded compact_eq_heine_borel, THEN spec[where x="(\<lambda>t. UNIV - t) ` f"]] by auto
  1.2992 -  hence "finite (op - UNIV ` f') \<and> op - UNIV ` f' \<subseteq> f" by(auto simp add: Diff_Diff_Int)
  1.2993 -  hence "s \<inter> \<Inter>op - UNIV ` f' \<noteq> {}" using assms(3)[THEN spec[where x="op - UNIV ` f'"]] by auto
  1.2994 -  thus False using f'(3) unfolding subset_eq and Union_iff by blast
  1.2995 -qed
  1.2996 -
  1.2997 -subsection{* Bounded closed nest property (proof does not use Heine-Borel).            *}
  1.2998 -
  1.2999 -lemma bounded_closed_nest:
  1.3000 -  assumes "\<forall>n. closed(s n)" "\<forall>n. (s n \<noteq> {})"
  1.3001 -  "(\<forall>m n. m \<le> n --> s n \<subseteq> s m)"  "bounded(s 0)"
  1.3002 -  shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s(n)"
  1.3003 -proof-
  1.3004 -  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
  1.3005 -  from assms(4,1) have *:"compact (s 0)" using bounded_closed_imp_compact[of "s 0"] by auto
  1.3006 -
  1.3007 -  then obtain l r where lr:"l\<in>s 0" "subseq r" "((x \<circ> r) ---> l) sequentially"
  1.3008 -    unfolding compact_def apply(erule_tac x=x in allE)  using x using assms(3) by blast
  1.3009 -
  1.3010 -  { fix n::nat
  1.3011 -    { fix e::real assume "e>0"
  1.3012 -      with lr(3) obtain N where N:"\<forall>m\<ge>N. dist ((x \<circ> r) m) l < e" unfolding Lim_sequentially by auto
  1.3013 -      hence "dist ((x \<circ> r) (max N n)) l < e" by auto
  1.3014 -      moreover
  1.3015 -      have "r (max N n) \<ge> n" using lr(2) using subseq_bigger[of r "max N n"] by auto
  1.3016 -      hence "(x \<circ> r) (max N n) \<in> s n"
  1.3017 -        using x apply(erule_tac x=n in allE)
  1.3018 -        using x apply(erule_tac x="r (max N n)" in allE)
  1.3019 -        using assms(3) apply(erule_tac x=n in allE)apply( erule_tac x="r (max N n)" in allE) by auto
  1.3020 -      ultimately have "\<exists>y\<in>s n. dist y l < e" by auto
  1.3021 -    }
  1.3022 -    hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by blast
  1.3023 -  }
  1.3024 -  thus ?thesis by auto
  1.3025 -qed
  1.3026 -
  1.3027 -text{* Decreasing case does not even need compactness, just completeness.        *}
  1.3028 -
  1.3029 -lemma decreasing_closed_nest:
  1.3030 -  assumes "\<forall>n. closed(s n)"
  1.3031 -          "\<forall>n. (s n \<noteq> {})"
  1.3032 -          "\<forall>m n. m \<le> n --> s n \<subseteq> s m"
  1.3033 -          "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y \<in> (s n). dist x y < e"
  1.3034 -  shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s n"
  1.3035 -proof-
  1.3036 -  have "\<forall>n. \<exists> x. x\<in>s n" using assms(2) by auto
  1.3037 -  hence "\<exists>t. \<forall>n. t n \<in> s n" using choice[of "\<lambda> n x. x \<in> s n"] by auto
  1.3038 -  then obtain t where t: "\<forall>n. t n \<in> s n" by auto
  1.3039 -  { fix e::real assume "e>0"
  1.3040 -    then obtain N where N:"\<forall>x\<in>s N. \<forall>y\<in>s N. dist x y < e" using assms(4) by auto
  1.3041 -    { fix m n ::nat assume "N \<le> m \<and> N \<le> n"
  1.3042 -      hence "t m \<in> s N" "t n \<in> s N" using assms(3) t unfolding  subset_eq t by blast+
  1.3043 -      hence "dist (t m) (t n) < e" using N by auto
  1.3044 -    }
  1.3045 -    hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (t m) (t n) < e" by auto
  1.3046 -  }
  1.3047 -  hence  "Cauchy t" unfolding cauchy_def by auto
  1.3048 -  then obtain l where l:"(t ---> l) sequentially" using complete_univ unfolding complete_def by auto
  1.3049 -  { fix n::nat
  1.3050 -    { fix e::real assume "e>0"
  1.3051 -      then obtain N::nat where N:"\<forall>n\<ge>N. dist (t n) l < e" using l[unfolded Lim_sequentially] by auto
  1.3052 -      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
  1.3053 -      hence "\<exists>y\<in>s n. dist y l < e" apply(rule_tac x="t (max n N)" in bexI) using N by auto
  1.3054 -    }
  1.3055 -    hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by auto
  1.3056 -  }
  1.3057 -  then show ?thesis by auto
  1.3058 -qed
  1.3059 -
  1.3060 -text{* Strengthen it to the intersection actually being a singleton.             *}
  1.3061 -
  1.3062 -lemma decreasing_closed_nest_sing:
  1.3063 -  assumes "\<forall>n. closed(s n)"
  1.3064 -          "\<forall>n. s n \<noteq> {}"
  1.3065 -          "\<forall>m n. m \<le> n --> s n \<subseteq> s m"
  1.3066 -          "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y\<in>(s n). dist x y < e"
  1.3067 -  shows "\<exists>a::'a::heine_borel. \<Inter> {t. (\<exists>n::nat. t = s n)} = {a}"
  1.3068 -proof-
  1.3069 -  obtain a where a:"\<forall>n. a \<in> s n" using decreasing_closed_nest[of s] using assms by auto
  1.3070 -  { fix b assume b:"b \<in> \<Inter>{t. \<exists>n. t = s n}"
  1.3071 -    { fix e::real assume "e>0"
  1.3072 -      hence "dist a b < e" using assms(4 )using b using a by blast
  1.3073 -    }
  1.3074 -    hence "dist a b = 0" by (metis dist_eq_0_iff dist_nz real_less_def)
  1.3075 -  }
  1.3076 -  with a have "\<Inter>{t. \<exists>n. t = s n} = {a}"  by auto
  1.3077 -  thus ?thesis by auto
  1.3078 -qed
  1.3079 -
  1.3080 -text{* Cauchy-type criteria for uniform convergence. *}
  1.3081 -
  1.3082 -lemma uniformly_convergent_eq_cauchy: fixes s::"nat \<Rightarrow> 'b \<Rightarrow> 'a::heine_borel" shows
  1.3083 - "(\<exists>l. \<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e) \<longleftrightarrow>
  1.3084 -  (\<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")
  1.3085 -proof(rule)
  1.3086 -  assume ?lhs
  1.3087 -  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
  1.3088 -  { fix e::real assume "e>0"
  1.3089 -    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
  1.3090 -    { fix n m::nat and x::"'b" assume "N \<le> m \<and> N \<le> n \<and> P x"
  1.3091 -      hence "dist (s m x) (s n x) < e"
  1.3092 -        using N[THEN spec[where x=m], THEN spec[where x=x]]
  1.3093 -        using N[THEN spec[where x=n], THEN spec[where x=x]]
  1.3094 -        using dist_triangle_half_l[of "s m x" "l x" e "s n x"] by auto  }
  1.3095 -    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  }
  1.3096 -  thus ?rhs by auto
  1.3097 -next
  1.3098 -  assume ?rhs
  1.3099 -  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
  1.3100 -  then obtain l where l:"\<forall>x. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l x) sequentially" unfolding convergent_eq_cauchy[THEN sym]
  1.3101 -    using choice[of "\<lambda>x l. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l) sequentially"] by auto
  1.3102 -  { fix e::real assume "e>0"
  1.3103 -    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"
  1.3104 -      using `?rhs`[THEN spec[where x="e/2"]] by auto
  1.3105 -    { fix x assume "P x"
  1.3106 -      then obtain M where M:"\<forall>n\<ge>M. dist (s n x) (l x) < e/2"
  1.3107 -        using l[THEN spec[where x=x], unfolded Lim_sequentially] using `e>0` by(auto elim!: allE[where x="e/2"])
  1.3108 -      fix n::nat assume "n\<ge>N"
  1.3109 -      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]]
  1.3110 -        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)  }
  1.3111 -    hence "\<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist(s n x)(l x) < e" by auto }
  1.3112 -  thus ?lhs by auto
  1.3113 -qed
  1.3114 -
  1.3115 -lemma uniformly_cauchy_imp_uniformly_convergent:
  1.3116 -  fixes s :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::heine_borel"
  1.3117 -  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"
  1.3118 -          "\<forall>x. P x --> (\<forall>e>0. \<exists>N. \<forall>n. N \<le> n --> dist(s n x)(l x) < e)"
  1.3119 -  shows "\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e"
  1.3120 -proof-
  1.3121 -  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"
  1.3122 -    using assms(1) unfolding uniformly_convergent_eq_cauchy[THEN sym] by auto
  1.3123 -  moreover
  1.3124 -  { fix x assume "P x"
  1.3125 -    hence "l x = l' x" using Lim_unique[OF trivial_limit_sequentially, of "\<lambda>n. s n x" "l x" "l' x"]
  1.3126 -      using l and assms(2) unfolding Lim_sequentially by blast  }
  1.3127 -  ultimately show ?thesis by auto
  1.3128 -qed
  1.3129 -
  1.3130 -subsection{* Define continuity over a net to take in restrictions of the set. *}
  1.3131 -
  1.3132 -definition
  1.3133 -  continuous :: "'a::t2_space net \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool" where
  1.3134 -  "continuous net f \<longleftrightarrow> (f ---> f(netlimit net)) net"
  1.3135 -
  1.3136 -lemma continuous_trivial_limit:
  1.3137 - "trivial_limit net ==> continuous net f"
  1.3138 -  unfolding continuous_def tendsto_def trivial_limit_eq by auto
  1.3139 -
  1.3140 -lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f ---> f(x)) (at x within s)"
  1.3141 -  unfolding continuous_def
  1.3142 -  unfolding tendsto_def
  1.3143 -  using netlimit_within[of x s]
  1.3144 -  by (cases "trivial_limit (at x within s)") (auto simp add: trivial_limit_eventually)
  1.3145 -
  1.3146 -lemma continuous_at: "continuous (at x) f \<longleftrightarrow> (f ---> f(x)) (at x)"
  1.3147 -  using continuous_within [of x UNIV f] by (simp add: within_UNIV)
  1.3148 -
  1.3149 -lemma continuous_at_within:
  1.3150 -  assumes "continuous (at x) f"  shows "continuous (at x within s) f"
  1.3151 -  using assms unfolding continuous_at continuous_within
  1.3152 -  by (rule Lim_at_within)
  1.3153 -
  1.3154 -text{* Derive the epsilon-delta forms, which we often use as "definitions" *}
  1.3155 -
  1.3156 -lemma continuous_within_eps_delta:
  1.3157 -  "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)"
  1.3158 -  unfolding continuous_within and Lim_within
  1.3159 -  apply auto unfolding dist_nz[THEN sym] apply(auto elim!:allE) apply(rule_tac x=d in exI) by auto
  1.3160 -
  1.3161 -lemma continuous_at_eps_delta: "continuous (at x) f \<longleftrightarrow>  (\<forall>e>0. \<exists>d>0.
  1.3162 -                           \<forall>x'. dist x' x < d --> dist(f x')(f x) < e)"
  1.3163 -  using continuous_within_eps_delta[of x UNIV f]
  1.3164 -  unfolding within_UNIV by blast
  1.3165 -
  1.3166 -text{* Versions in terms of open balls. *}
  1.3167 -
  1.3168 -lemma continuous_within_ball:
  1.3169 - "continuous (at x within s) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.
  1.3170 -                            f ` (ball x d \<inter> s) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
  1.3171 -proof
  1.3172 -  assume ?lhs
  1.3173 -  { fix e::real assume "e>0"
  1.3174 -    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"
  1.3175 -      using `?lhs`[unfolded continuous_within Lim_within] by auto
  1.3176 -    { fix y assume "y\<in>f ` (ball x d \<inter> s)"
  1.3177 -      hence "y \<in> ball (f x) e" using d(2) unfolding dist_nz[THEN sym]
  1.3178 -        apply (auto simp add: dist_commute mem_ball) apply(erule_tac x=xa in ballE) apply auto using `e>0` by auto
  1.3179 -    }
  1.3180 -    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)  }
  1.3181 -  thus ?rhs by auto
  1.3182 -next
  1.3183 -  assume ?rhs thus ?lhs unfolding continuous_within Lim_within ball_def subset_eq
  1.3184 -    apply (auto simp add: dist_commute) apply(erule_tac x=e in allE) by auto
  1.3185 -qed
  1.3186 -
  1.3187 -lemma continuous_at_ball:
  1.3188 -  "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f ` (ball x d) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
  1.3189 -proof
  1.3190 -  assume ?lhs thus ?rhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
  1.3191 -    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)
  1.3192 -    unfolding dist_nz[THEN sym] by auto
  1.3193 -next
  1.3194 -  assume ?rhs thus ?lhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
  1.3195 -    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)
  1.3196 -qed
  1.3197 -
  1.3198 -text{* For setwise continuity, just start from the epsilon-delta definitions. *}
  1.3199 -
  1.3200 -definition
  1.3201 -  continuous_on :: "'a::metric_space set \<Rightarrow> ('a \<Rightarrow> 'b::metric_space) \<Rightarrow> bool" where
  1.3202 -  "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. \<forall>e>0. \<exists>d::real>0. \<forall>x' \<in> s. dist x' x < d --> dist (f x') (f x) < e)"
  1.3203 -
  1.3204 -
  1.3205 -definition
  1.3206 -  uniformly_continuous_on ::
  1.3207 -    "'a::metric_space set \<Rightarrow> ('a \<Rightarrow> 'b::metric_space) \<Rightarrow> bool" where
  1.3208 -  "uniformly_continuous_on s f \<longleftrightarrow>
  1.3209 -        (\<forall>e>0. \<exists>d>0. \<forall>x\<in>s. \<forall> x'\<in>s. dist x' x < d
  1.3210 -                           --> dist (f x') (f x) < e)"
  1.3211 -
  1.3212 -text{* Some simple consequential lemmas. *}
  1.3213 -
  1.3214 -lemma uniformly_continuous_imp_continuous:
  1.3215 - " uniformly_continuous_on s f ==> continuous_on s f"
  1.3216 -  unfolding uniformly_continuous_on_def continuous_on_def by blast
  1.3217 -
  1.3218 -lemma continuous_at_imp_continuous_within:
  1.3219 - "continuous (at x) f ==> continuous (at x within s) f"
  1.3220 -  unfolding continuous_within continuous_at using Lim_at_within by auto
  1.3221 -
  1.3222 -lemma continuous_at_imp_continuous_on: assumes "(\<forall>x \<in> s. continuous (at x) f)"
  1.3223 -  shows "continuous_on s f"
  1.3224 -proof(simp add: continuous_at continuous_on_def, rule, rule, rule)
  1.3225 -  fix x and e::real assume "x\<in>s" "e>0"
  1.3226 -  hence "eventually (\<lambda>xa. dist (f xa) (f x) < e) (at x)" using assms unfolding continuous_at tendsto_iff by auto
  1.3227 -  then obtain d where d:"d>0" "\<forall>xa. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e" unfolding eventually_at by auto
  1.3228 -  { fix x' assume "\<not> 0 < dist x' x"
  1.3229 -    hence "x=x'"
  1.3230 -      using dist_nz[of x' x] by auto
  1.3231 -    hence "dist (f x') (f x) < e" using `e>0` by auto
  1.3232 -  }
  1.3233 -  thus "\<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e" using d by auto
  1.3234 -qed
  1.3235 -
  1.3236 -lemma continuous_on_eq_continuous_within:
  1.3237 - "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x within s) f)" (is "?lhs = ?rhs")
  1.3238 -proof
  1.3239 -  assume ?rhs
  1.3240 -  { fix x assume "x\<in>s"
  1.3241 -    fix e::real assume "e>0"
  1.3242 -    assume "\<exists>d>0. \<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e"
  1.3243 -    then obtain d where "d>0" and d:"\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e" by auto
  1.3244 -    { fix x' assume as:"x'\<in>s" "dist x' x < d"
  1.3245 -      hence "dist (f x') (f x) < e" using `e>0` d `x'\<in>s` dist_eq_0_iff[of x' x] zero_le_dist[of x' x] as(2) by (metis dist_eq_0_iff dist_nz) }
  1.3246 -    hence "\<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e" using `d>0` by auto
  1.3247 -  }
  1.3248 -  thus ?lhs using `?rhs` unfolding continuous_on_def continuous_within Lim_within by auto
  1.3249 -next
  1.3250 -  assume ?lhs
  1.3251 -  thus ?rhs unfolding continuous_on_def continuous_within Lim_within by blast
  1.3252 -qed
  1.3253 -
  1.3254 -lemma continuous_on:
  1.3255 - "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. (f ---> f(x)) (at x within s))"
  1.3256 -  by (auto simp add: continuous_on_eq_continuous_within continuous_within)
  1.3257 -
  1.3258 -lemma continuous_on_eq_continuous_at:
  1.3259 - "open s ==> (continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x) f))"
  1.3260 -  by (auto simp add: continuous_on continuous_at Lim_within_open)
  1.3261 -
  1.3262 -lemma continuous_within_subset:
  1.3263 - "continuous (at x within s) f \<Longrightarrow> t \<subseteq> s
  1.3264 -             ==> continuous (at x within t) f"
  1.3265 -  unfolding continuous_within by(metis Lim_within_subset)
  1.3266 -
  1.3267 -lemma continuous_on_subset:
  1.3268 - "continuous_on s f \<Longrightarrow> t \<subseteq> s ==> continuous_on t f"
  1.3269 -  unfolding continuous_on by (metis subset_eq Lim_within_subset)
  1.3270 -
  1.3271 -lemma continuous_on_interior:
  1.3272 - "continuous_on s f \<Longrightarrow> x \<in> interior s ==> continuous (at x) f"
  1.3273 -unfolding interior_def
  1.3274 -apply simp
  1.3275 -by (meson continuous_on_eq_continuous_at continuous_on_subset)
  1.3276 -
  1.3277 -lemma continuous_on_eq:
  1.3278 - "(\<forall>x \<in> s. f x = g x) \<Longrightarrow> continuous_on s f
  1.3279 -           ==> continuous_on s g"
  1.3280 -  by (simp add: continuous_on_def)
  1.3281 -
  1.3282 -text{* Characterization of various kinds of continuity in terms of sequences.  *}
  1.3283 -
  1.3284 -(* \<longrightarrow> could be generalized, but \<longleftarrow> requires metric space *)
  1.3285 -lemma continuous_within_sequentially:
  1.3286 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  1.3287 -  shows "continuous (at a within s) f \<longleftrightarrow>
  1.3288 -                (\<forall>x. (\<forall>n::nat. x n \<in> s) \<and> (x ---> a) sequentially
  1.3289 -                     --> ((f o x) ---> f a) sequentially)" (is "?lhs = ?rhs")
  1.3290 -proof
  1.3291 -  assume ?lhs
  1.3292 -  { 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"
  1.3293 -    fix e::real assume "e>0"
  1.3294 -    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
  1.3295 -    from x(2) `d>0` obtain N where N:"\<forall>n\<ge>N. dist (x n) a < d" by auto
  1.3296 -    hence "\<exists>N. \<forall>n\<ge>N. dist ((f \<circ> x) n) (f a) < e"
  1.3297 -      apply(rule_tac  x=N in exI) using N d  apply auto using x(1)
  1.3298 -      apply(erule_tac x=n in allE) apply(erule_tac x=n in allE)
  1.3299 -      apply(erule_tac x="x n" in ballE)  apply auto unfolding dist_nz[THEN sym] apply auto using `e>0` by auto
  1.3300 -  }
  1.3301 -  thus ?rhs unfolding continuous_within unfolding Lim_sequentially by simp
  1.3302 -next
  1.3303 -  assume ?rhs
  1.3304 -  { fix e::real assume "e>0"
  1.3305 -    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)"
  1.3306 -    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
  1.3307 -    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)"
  1.3308 -      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
  1.3309 -    { fix d::real assume "d>0"
  1.3310 -      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
  1.3311 -      then obtain N::nat where N:"inverse (real (N + 1)) < d" by auto
  1.3312 -      { fix n::nat assume n:"n\<ge>N"
  1.3313 -        hence "dist (x (inverse (real (n + 1)))) a < inverse (real (n + 1))" using x[THEN spec[where x="inverse (real (n + 1))"]] by auto
  1.3314 -        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)
  1.3315 -        ultimately have "dist (x (inverse (real (n + 1)))) a < d" by auto
  1.3316 -      }
  1.3317 -      hence "\<exists>N::nat. \<forall>n\<ge>N. dist (x (inverse (real (n + 1)))) a < d" by auto
  1.3318 -    }
  1.3319 -    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
  1.3320 -    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
  1.3321 -    hence "False" apply(erule_tac x=e in allE) using `e>0` using x by auto
  1.3322 -  }
  1.3323 -  thus ?lhs  unfolding continuous_within unfolding Lim_within unfolding Lim_sequentially by blast
  1.3324 -qed
  1.3325 -
  1.3326 -lemma continuous_at_sequentially:
  1.3327 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  1.3328 -  shows "continuous (at a) f \<longleftrightarrow> (\<forall>x. (x ---> a) sequentially
  1.3329 -                  --> ((f o x) ---> f a) sequentially)"
  1.3330 -  using continuous_within_sequentially[of a UNIV f] unfolding within_UNIV by auto
  1.3331 -
  1.3332 -lemma continuous_on_sequentially:
  1.3333 - "continuous_on s f \<longleftrightarrow>  (\<forall>x. \<forall>a \<in> s. (\<forall>n. x(n) \<in> s) \<and> (x ---> a) sequentially
  1.3334 -                    --> ((f o x) ---> f(a)) sequentially)" (is "?lhs = ?rhs")
  1.3335 -proof
  1.3336 -  assume ?rhs thus ?lhs using continuous_within_sequentially[of _ s f] unfolding continuous_on_eq_continuous_within by auto
  1.3337 -next
  1.3338 -  assume ?lhs thus ?rhs unfolding continuous_on_eq_continuous_within using continuous_within_sequentially[of _ s f] by auto
  1.3339 -qed
  1.3340 -
  1.3341 -lemma uniformly_continuous_on_sequentially:
  1.3342 -  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
  1.3343 -  shows "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>
  1.3344 -                    ((\<lambda>n. x n - y n) ---> 0) sequentially
  1.3345 -                    \<longrightarrow> ((\<lambda>n. f(x n) - f(y n)) ---> 0) sequentially)" (is "?lhs = ?rhs")
  1.3346 -proof
  1.3347 -  assume ?lhs
  1.3348 -  { fix x y assume x:"\<forall>n. x n \<in> s" and y:"\<forall>n. y n \<in> s" and xy:"((\<lambda>n. x n - y n) ---> 0) sequentially"
  1.3349 -    { fix e::real assume "e>0"
  1.3350 -      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"
  1.3351 -        using `?lhs`[unfolded uniformly_continuous_on_def, THEN spec[where x=e]] by auto
  1.3352 -      obtain N where N:"\<forall>n\<ge>N. norm (x n - y n - 0) < d" using xy[unfolded Lim_sequentially dist_norm] and `d>0` by auto
  1.3353 -      { fix n assume "n\<ge>N"
  1.3354 -        hence "norm (f (x n) - f (y n) - 0) < e"
  1.3355 -          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
  1.3356 -          unfolding dist_commute and dist_norm by simp  }
  1.3357 -      hence "\<exists>N. \<forall>n\<ge>N. norm (f (x n) - f (y n) - 0) < e"  by auto  }
  1.3358 -    hence "((\<lambda>n. f(x n) - f(y n)) ---> 0) sequentially" unfolding Lim_sequentially and dist_norm by auto  }
  1.3359 -  thus ?rhs by auto
  1.3360 -next
  1.3361 -  assume ?rhs
  1.3362 -  { assume "\<not> ?lhs"
  1.3363 -    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
  1.3364 -    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"
  1.3365 -      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
  1.3366 -      by (auto simp add: dist_commute)
  1.3367 -    def x \<equiv> "\<lambda>n::nat. fst (fa (inverse (real n + 1)))"
  1.3368 -    def y \<equiv> "\<lambda>n::nat. snd (fa (inverse (real n + 1)))"
  1.3369 -    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"
  1.3370 -      unfolding x_def and y_def using fa by auto
  1.3371 -    have 1:"\<And>(x::'a) y. dist (x - y) 0 = dist x y" unfolding dist_norm by auto
  1.3372 -    have 2:"\<And>(x::'b) y. dist (x - y) 0 = dist x y" unfolding dist_norm by auto
  1.3373 -    { fix e::real assume "e>0"
  1.3374 -      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
  1.3375 -      { fix n::nat assume "n\<ge>N"
  1.3376 -        hence "inverse (real n + 1) < inverse (real N)" using real_of_nat_ge_zero and `N\<noteq>0` by auto
  1.3377 -        also have "\<dots> < e" using N by auto
  1.3378 -        finally have "inverse (real n + 1) < e" by auto
  1.3379 -        hence "dist (x n - y n) 0 < e" unfolding 1 using xy0[THEN spec[where x=n]] by auto  }
  1.3380 -      hence "\<exists>N. \<forall>n\<ge>N. dist (x n - y n) 0 < e" by auto  }
  1.3381 -    hence "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (f (x n) - f (y n)) 0 < e" using `?rhs`[THEN spec[where x=x], THEN spec[where x=y]] and xyn unfolding Lim_sequentially by auto
  1.3382 -    hence False unfolding 2 using fxy and `e>0` by auto  }
  1.3383 -  thus ?lhs unfolding uniformly_continuous_on_def by blast
  1.3384 -qed
  1.3385 -
  1.3386 -text{* The usual transformation theorems. *}
  1.3387 -
  1.3388 -lemma continuous_transform_within:
  1.3389 -  fixes f g :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  1.3390 -  assumes "0 < d" "x \<in> s" "\<forall>x' \<in> s. dist x' x < d --> f x' = g x'"
  1.3391 -          "continuous (at x within s) f"
  1.3392 -  shows "continuous (at x within s) g"
  1.3393 -proof-
  1.3394 -  { fix e::real assume "e>0"
  1.3395 -    then obtain d' where d':"d'>0" "\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d' \<longrightarrow> dist (f xa) (f x) < e" using assms(4) unfolding continuous_within Lim_within by auto
  1.3396 -    { fix x' assume "x'\<in>s" "0 < dist x' x" "dist x' x < (min d d')"
  1.3397 -      hence "dist (f x') (g x) < e" using assms(2,3) apply(erule_tac x=x in ballE) using d' by auto  }
  1.3398 -    hence "\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < (min d d') \<longrightarrow> dist (f xa) (g x) < e" by blast
  1.3399 -    hence "\<exists>d>0. \<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (g x) < e" using `d>0` `d'>0` by(rule_tac x="min d d'" in exI)auto  }
  1.3400 -  hence "(f ---> g x) (at x within s)" unfolding Lim_within using assms(1) by auto
  1.3401 -  thus ?thesis unfolding continuous_within using Lim_transform_within[of d s x f g "g x"] using assms by blast
  1.3402 -qed
  1.3403 -
  1.3404 -lemma continuous_transform_at:
  1.3405 -  fixes f g :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  1.3406 -  assumes "0 < d" "\<forall>x'. dist x' x < d --> f x' = g x'"
  1.3407 -          "continuous (at x) f"
  1.3408 -  shows "continuous (at x) g"
  1.3409 -proof-
  1.3410 -  { fix e::real assume "e>0"
  1.3411 -    then obtain d' where d':"d'>0" "\<forall>xa. 0 < dist xa x \<and> dist xa x < d' \<longrightarrow> dist (f xa) (f x) < e" using assms(3) unfolding continuous_at Lim_at by auto
  1.3412 -    { fix x' assume "0 < dist x' x" "dist x' x < (min d d')"
  1.3413 -      hence "dist (f x') (g x) < e" using assms(2) apply(erule_tac x=x in allE) using d' by auto
  1.3414 -    }
  1.3415 -    hence "\<forall>xa. 0 < dist xa x \<and> dist xa x < (min d d') \<longrightarrow> dist (f xa) (g x) < e" by blast
  1.3416 -    hence "\<exists>d>0. \<forall>xa. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (g x) < e" using `d>0` `d'>0` by(rule_tac x="min d d'" in exI)auto
  1.3417 -  }
  1.3418 -  hence "(f ---> g x) (at x)" unfolding Lim_at using assms(1) by auto
  1.3419 -  thus ?thesis unfolding continuous_at using Lim_transform_at[of d x f g "g x"] using assms by blast
  1.3420 -qed
  1.3421 -
  1.3422 -text{* Combination results for pointwise continuity. *}
  1.3423 -
  1.3424 -lemma continuous_const: "continuous net (\<lambda>x. c)"
  1.3425 -  by (auto simp add: continuous_def Lim_const)
  1.3426 -
  1.3427 -lemma continuous_cmul:
  1.3428 -  fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  1.3429 -  shows "continuous net f ==> continuous net (\<lambda>x. c *\<^sub>R f x)"
  1.3430 -  by (auto simp add: continuous_def Lim_cmul)
  1.3431 -
  1.3432 -lemma continuous_neg:
  1.3433 -  fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  1.3434 -  shows "continuous net f ==> continuous net (\<lambda>x. -(f x))"
  1.3435 -  by (auto simp add: continuous_def Lim_neg)
  1.3436 -
  1.3437 -lemma continuous_add:
  1.3438 -  fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  1.3439 -  shows "continuous net f \<Longrightarrow> continuous net g \<Longrightarrow> continuous net (\<lambda>x. f x + g x)"
  1.3440 -  by (auto simp add: continuous_def Lim_add)
  1.3441 -
  1.3442 -lemma continuous_sub:
  1.3443 -  fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  1.3444 -  shows "continuous net f \<Longrightarrow> continuous net g \<Longrightarrow> continuous net (\<lambda>x. f x - g x)"
  1.3445 -  by (auto simp add: continuous_def Lim_sub)
  1.3446 -
  1.3447 -text{* Same thing for setwise continuity. *}
  1.3448 -
  1.3449 -lemma continuous_on_const:
  1.3450 - "continuous_on s (\<lambda>x. c)"
  1.3451 -  unfolding continuous_on_eq_continuous_within using continuous_const by blast
  1.3452 -
  1.3453 -lemma continuous_on_cmul:
  1.3454 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  1.3455 -  shows "continuous_on s f ==>  continuous_on s (\<lambda>x. c *\<^sub>R (f x))"
  1.3456 -  unfolding continuous_on_eq_continuous_within using continuous_cmul by blast
  1.3457 -
  1.3458 -lemma continuous_on_neg:
  1.3459 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  1.3460 -  shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. - f x)"
  1.3461 -  unfolding continuous_on_eq_continuous_within using continuous_neg by blast
  1.3462 -
  1.3463 -lemma continuous_on_add:
  1.3464 -  fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  1.3465 -  shows "continuous_on s f \<Longrightarrow> continuous_on s g
  1.3466 -           \<Longrightarrow> continuous_on s (\<lambda>x. f x + g x)"
  1.3467 -  unfolding continuous_on_eq_continuous_within using continuous_add by blast
  1.3468 -
  1.3469 -lemma continuous_on_sub:
  1.3470 -  fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  1.3471 -  shows "continuous_on s f \<Longrightarrow> continuous_on s g
  1.3472 -           \<Longrightarrow> continuous_on s (\<lambda>x. f x - g x)"
  1.3473 -  unfolding continuous_on_eq_continuous_within using continuous_sub by blast
  1.3474 -
  1.3475 -text{* Same thing for uniform continuity, using sequential formulations. *}
  1.3476 -
  1.3477 -lemma uniformly_continuous_on_const:
  1.3478 - "uniformly_continuous_on s (\<lambda>x. c)"
  1.3479 -  unfolding uniformly_continuous_on_def by simp
  1.3480 -
  1.3481 -lemma uniformly_continuous_on_cmul:
  1.3482 -  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
  1.3483 -    (* FIXME: generalize 'a to metric_space *)
  1.3484 -  assumes "uniformly_continuous_on s f"
  1.3485 -  shows "uniformly_continuous_on s (\<lambda>x. c *\<^sub>R f(x))"
  1.3486 -proof-
  1.3487 -  { fix x y assume "((\<lambda>n. f (x n) - f (y n)) ---> 0) sequentially"
  1.3488 -    hence "((\<lambda>n. c *\<^sub>R f (x n) - c *\<^sub>R f (y n)) ---> 0) sequentially"
  1.3489 -      using Lim_cmul[of "(\<lambda>n. f (x n) - f (y n))" 0 sequentially c]
  1.3490 -      unfolding scaleR_zero_right scaleR_right_diff_distrib by auto
  1.3491 -  }
  1.3492 -  thus ?thesis using assms unfolding uniformly_continuous_on_sequentially by auto
  1.3493 -qed
  1.3494 -
  1.3495 -lemma dist_minus:
  1.3496 -  fixes x y :: "'a::real_normed_vector"
  1.3497 -  shows "dist (- x) (- y) = dist x y"
  1.3498 -  unfolding dist_norm minus_diff_minus norm_minus_cancel ..
  1.3499 -
  1.3500 -lemma uniformly_continuous_on_neg:
  1.3501 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  1.3502 -  shows "uniformly_continuous_on s f
  1.3503 -         ==> uniformly_continuous_on s (\<lambda>x. -(f x))"
  1.3504 -  unfolding uniformly_continuous_on_def dist_minus .
  1.3505 -
  1.3506 -lemma uniformly_continuous_on_add:
  1.3507 -  fixes f g :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector" (* FIXME: generalize 'a *)
  1.3508 -  assumes "uniformly_continuous_on s f" "uniformly_continuous_on s g"
  1.3509 -  shows "uniformly_continuous_on s (\<lambda>x. f x + g x)"
  1.3510 -proof-
  1.3511 -  {  fix x y assume "((\<lambda>n. f (x n) - f (y n)) ---> 0) sequentially"
  1.3512 -                    "((\<lambda>n. g (x n) - g (y n)) ---> 0) sequentially"
  1.3513 -    hence "((\<lambda>xa. f (x xa) - f (y xa) + (g (x xa) - g (y xa))) ---> 0 + 0) sequentially"
  1.3514 -      using Lim_add[of "\<lambda> n. f (x n) - f (y n)" 0  sequentially "\<lambda> n. g (x n) - g (y n)" 0] by auto
  1.3515 -    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  }
  1.3516 -  thus ?thesis using assms unfolding uniformly_continuous_on_sequentially by auto
  1.3517 -qed
  1.3518 -
  1.3519 -lemma uniformly_continuous_on_sub:
  1.3520 -  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector" (* FIXME: generalize 'a *)
  1.3521 -  shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s g
  1.3522 -           ==> uniformly_continuous_on s  (\<lambda>x. f x - g x)"
  1.3523 -  unfolding ab_diff_minus
  1.3524 -  using uniformly_continuous_on_add[of s f "\<lambda>x. - g x"]
  1.3525 -  using uniformly_continuous_on_neg[of s g] by auto
  1.3526 -
  1.3527 -text{* Identity function is continuous in every sense. *}
  1.3528 -
  1.3529 -lemma continuous_within_id:
  1.3530 - "continuous (at a within s) (\<lambda>x. x)"
  1.3531 -  unfolding continuous_within by (rule Lim_at_within [OF Lim_ident_at])
  1.3532 -
  1.3533 -lemma continuous_at_id:
  1.3534 - "continuous (at a) (\<lambda>x. x)"
  1.3535 -  unfolding continuous_at by (rule Lim_ident_at)
  1.3536 -
  1.3537 -lemma continuous_on_id:
  1.3538 - "continuous_on s (\<lambda>x. x)"
  1.3539 -  unfolding continuous_on Lim_within by auto
  1.3540 -
  1.3541 -lemma uniformly_continuous_on_id:
  1.3542 - "uniformly_continuous_on s (\<lambda>x. x)"
  1.3543 -  unfolding uniformly_continuous_on_def by auto
  1.3544 -
  1.3545 -text{* Continuity of all kinds is preserved under composition. *}
  1.3546 -
  1.3547 -lemma continuous_within_compose:
  1.3548 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
  1.3549 -  fixes g :: "'b::metric_space \<Rightarrow> 'c::metric_space"
  1.3550 -  assumes "continuous (at x within s) f"   "continuous (at (f x) within f ` s) g"
  1.3551 -  shows "continuous (at x within s) (g o f)"
  1.3552 -proof-
  1.3553 -  { fix e::real assume "e>0"
  1.3554 -    with assms(2)[unfolded continuous_within Lim_within] obtain d  where "d>0" and d:"\<forall>xa\<in>f ` s. 0 < dist xa (f x) \<and> dist xa (f x) < d \<longrightarrow> dist (g xa) (g (f x)) < e" by auto
  1.3555 -    from assms(1)[unfolded continuous_within Lim_within] obtain d' where "d'>0" and d':"\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d' \<longrightarrow> dist (f xa) (f x) < d" using `d>0` by auto
  1.3556 -    { fix y assume as:"y\<in>s"  "0 < dist y x"  "dist y x < d'"
  1.3557 -      hence "dist (f y) (f x) < d" using d'[THEN bspec[where x=y]] by (auto simp add:dist_commute)
  1.3558 -      hence "dist (g (f y)) (g (f x)) < e" using as(1) d[THEN bspec[where x="f y"]] unfolding dist_nz[THEN sym] using `e>0` by auto   }
  1.3559 -    hence "\<exists>d>0. \<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (g (f xa)) (g (f x)) < e" using `d'>0` by auto  }
  1.3560 -  thus ?thesis unfolding continuous_within Lim_within by auto
  1.3561 -qed
  1.3562 -
  1.3563 -lemma continuous_at_compose:
  1.3564 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
  1.3565 -  fixes g :: "'b::metric_space \<Rightarrow> 'c::metric_space"
  1.3566 -  assumes "continuous (at x) f"  "continuous (at (f x)) g"
  1.3567 -  shows "continuous (at x) (g o f)"
  1.3568 -proof-
  1.3569 -  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
  1.3570 -  thus ?thesis using assms(1) using continuous_within_compose[of x UNIV f g, unfolded within_UNIV] by auto
  1.3571 -qed
  1.3572 -
  1.3573 -lemma continuous_on_compose:
  1.3574 - "continuous_on s f \<Longrightarrow> continuous_on (f ` s) g \<Longrightarrow> continuous_on s (g o f)"
  1.3575 -  unfolding continuous_on_eq_continuous_within using continuous_within_compose[of _ s f g] by auto
  1.3576 -
  1.3577 -lemma uniformly_continuous_on_compose:
  1.3578 -  assumes "uniformly_continuous_on s f"  "uniformly_continuous_on (f ` s) g"
  1.3579 -  shows "uniformly_continuous_on s (g o f)"
  1.3580 -proof-
  1.3581 -  { fix e::real assume "e>0"
  1.3582 -    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
  1.3583 -    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
  1.3584 -    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  }
  1.3585 -  thus ?thesis using assms unfolding uniformly_continuous_on_def by auto
  1.3586 -qed
  1.3587 -
  1.3588 -text{* Continuity in terms of open preimages. *}
  1.3589 -
  1.3590 -lemma continuous_at_open:
  1.3591 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
  1.3592 -  shows "continuous (at x) f \<longleftrightarrow> (\<forall>t. open t \<and> f x \<in> t --> (\<exists>s. open s \<and> x \<in> s \<and> (\<forall>x' \<in> s. (f x') \<in> t)))" (is "?lhs = ?rhs")
  1.3593 -proof
  1.3594 -  assume ?lhs
  1.3595 -  { fix t assume as: "open t" "f x \<in> t"
  1.3596 -    then obtain e where "e>0" and e:"ball (f x) e \<subseteq> t" unfolding open_contains_ball by auto
  1.3597 -
  1.3598 -    obtain d where "d>0" and d:"\<forall>y. 0 < dist y x \<and> dist y x < d \<longrightarrow> dist (f y) (f x) < e" using `e>0` using `?lhs`[unfolded continuous_at Lim_at open_dist] by auto
  1.3599 -
  1.3600 -    have "open (ball x d)" using open_ball by auto
  1.3601 -    moreover have "x \<in> ball x d" unfolding centre_in_ball using `d>0` by simp
  1.3602 -    moreover
  1.3603 -    { fix x' assume "x'\<in>ball x d" hence "f x' \<in> t"
  1.3604 -        using e[unfolded subset_eq Ball_def mem_ball, THEN spec[where x="f x'"]]    d[THEN spec[where x=x']]
  1.3605 -        unfolding mem_ball apply (auto simp add: dist_commute)
  1.3606 -        unfolding dist_nz[THEN sym] using as(2) by auto  }
  1.3607 -    hence "\<forall>x'\<in>ball x d. f x' \<in> t" by auto
  1.3608 -    ultimately have "\<exists>s. open s \<and> x \<in> s \<and> (\<forall>x'\<in>s. f x' \<in> t)"
  1.3609 -      apply(rule_tac x="ball x d" in exI) by simp  }
  1.3610 -  thus ?rhs by auto
  1.3611 -next
  1.3612 -  assume ?rhs
  1.3613 -  { fix e::real assume "e>0"
  1.3614 -    then obtain s where s: "open s"  "x \<in> s"  "\<forall>x'\<in>s. f x' \<in> ball (f x) e" using `?rhs`[unfolded continuous_at Lim_at, THEN spec[where x="ball (f x) e"]]
  1.3615 -      unfolding centre_in_ball[of "f x" e, THEN sym] by auto
  1.3616 -    then obtain d where "d>0" and d:"ball x d \<subseteq> s" unfolding open_contains_ball by auto
  1.3617 -    { fix y assume "0 < dist y x \<and> dist y x < d"
  1.3618 -      hence "dist (f y) (f x) < e" using d[unfolded subset_eq Ball_def mem_ball, THEN spec[where x=y]]
  1.3619 -        using s(3)[THEN bspec[where x=y], unfolded mem_ball] by (auto simp add: dist_commute)  }
  1.3620 -    hence "\<exists>d>0. \<forall>xa. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e" using `d>0` by auto  }
  1.3621 -  thus ?lhs unfolding continuous_at Lim_at by auto
  1.3622 -qed
  1.3623 -
  1.3624 -lemma continuous_on_open:
  1.3625 - "continuous_on s f \<longleftrightarrow>
  1.3626 -        (\<forall>t. openin (subtopology euclidean (f ` s)) t
  1.3627 -            --> openin (subtopology euclidean s) {x \<in> s. f x \<in> t})" (is "?lhs = ?rhs")
  1.3628 -proof
  1.3629 -  assume ?lhs
  1.3630 -  { fix t assume as:"openin (subtopology euclidean (f ` s)) t"
  1.3631 -    have "{x \<in> s. f x \<in> t} \<subseteq> s" using as[unfolded openin_euclidean_subtopology_iff] by auto
  1.3632 -    moreover
  1.3633 -    { fix x assume as':"x\<in>{x \<in> s. f x \<in> t}"
  1.3634 -      then obtain e where e: "e>0" "\<forall>x'\<in>f ` s. dist x' (f x) < e \<longrightarrow> x' \<in> t" using as[unfolded openin_euclidean_subtopology_iff, THEN conjunct2, THEN bspec[where x="f x"]] by auto
  1.3635 -      from this(1) obtain d where d: "d>0" "\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e" using `?lhs`[unfolded continuous_on Lim_within, THEN bspec[where x=x]] using as' by auto
  1.3636 -      have "\<exists>e>0. \<forall>x'\<in>s. dist x' x < e \<longrightarrow> x' \<in> {x \<in> s. f x \<in> t}" using d e unfolding dist_nz[THEN sym] by (rule_tac x=d in exI, auto)  }
  1.3637 -    ultimately have "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}" unfolding openin_euclidean_subtopology_iff by auto  }
  1.3638 -  thus ?rhs unfolding continuous_on Lim_within using openin by auto
  1.3639 -next
  1.3640 -  assume ?rhs
  1.3641 -  { fix e::real and x assume "x\<in>s" "e>0"
  1.3642 -    { fix xa x' assume "dist (f xa) (f x) < e" "xa \<in> s" "x' \<in> s" "dist (f xa) (f x') < e - dist (f xa) (f x)"
  1.3643 -      hence "dist (f x') (f x) < e" using dist_triangle[of "f x'" "f x" "f xa"]
  1.3644 -        by (auto simp add: dist_commute)  }
  1.3645 -    hence "ball (f x) e \<inter> f ` s \<subseteq> f ` s \<and> (\<forall>xa\<in>ball (f x) e \<inter> f ` s. \<exists>ea>0. \<forall>x'\<in>f ` s. dist x' xa < ea \<longrightarrow> x' \<in> ball (f x) e \<inter> f ` s)" apply auto
  1.3646 -      apply(rule_tac x="e - dist (f xa) (f x)" in exI) using `e>0` by (auto simp add: dist_commute)
  1.3647 -    hence "\<forall>xa\<in>{xa \<in> s. f xa \<in> ball (f x) e \<inter> f ` s}. \<exists>ea>0. \<forall>x'\<in>s. dist x' xa < ea \<longrightarrow> x' \<in> {xa \<in> s. f xa \<in> ball (f x) e \<inter> f ` s}"
  1.3648 -      using `?rhs`[unfolded openin_euclidean_subtopology_iff, THEN spec[where x="ball (f x) e \<inter> f ` s"]] by auto
  1.3649 -    hence "\<exists>d>0. \<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e" apply(erule_tac x=x in ballE) apply auto using `e>0` `x\<in>s` by (auto simp add: dist_commute)  }
  1.3650 -  thus ?lhs unfolding continuous_on Lim_within by auto
  1.3651 -qed
  1.3652 -
  1.3653 -(* ------------------------------------------------------------------------- *)
  1.3654 -(* Similarly in terms of closed sets.                                        *)
  1.3655 -(* ------------------------------------------------------------------------- *)
  1.3656 -
  1.3657 -lemma continuous_on_closed:
  1.3658 - "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")
  1.3659 -proof
  1.3660 -  assume ?lhs
  1.3661 -  { fix t
  1.3662 -    have *:"s - {x \<in> s. f x \<in> f ` s - t} = {x \<in> s. f x \<in> t}" by auto
  1.3663 -    have **:"f ` s - (f ` s - (f ` s - t)) = f ` s - t" by auto
  1.3664 -    assume as:"closedin (subtopology euclidean (f ` s)) t"
  1.3665 -    hence "closedin (subtopology euclidean (f ` s)) (f ` s - (f ` s - t))" unfolding closedin_def topspace_euclidean_subtopology unfolding ** by auto
  1.3666 -    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"]]
  1.3667 -      unfolding openin_closedin_eq topspace_euclidean_subtopology unfolding * by auto  }
  1.3668 -  thus ?rhs by auto
  1.3669 -next
  1.3670 -  assume ?rhs
  1.3671 -  { fix t
  1.3672 -    have *:"s - {x \<in> s. f x \<in> f ` s - t} = {x \<in> s. f x \<in> t}" by auto
  1.3673 -    assume as:"openin (subtopology euclidean (f ` s)) t"
  1.3674 -    hence "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}" using `?rhs`[THEN spec[where x="(f ` s) - t"]]
  1.3675 -      unfolding openin_closedin_eq topspace_euclidean_subtopology *[THEN sym] closedin_subtopology by auto }
  1.3676 -  thus ?lhs unfolding continuous_on_open by auto
  1.3677 -qed
  1.3678 -
  1.3679 -text{* Half-global and completely global cases.                                  *}
  1.3680 -
  1.3681 -lemma continuous_open_in_preimage:
  1.3682 -  assumes "continuous_on s f"  "open t"
  1.3683 -  shows "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
  1.3684 -proof-
  1.3685 -  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
  1.3686 -  have "openin (subtopology euclidean (f ` s)) (t \<inter> f ` s)"
  1.3687 -    using openin_open_Int[of t "f ` s", OF assms(2)] unfolding openin_open by auto
  1.3688 -  thus ?thesis using assms(1)[unfolded continuous_on_open, THEN spec[where x="t \<inter> f ` s"]] using * by auto
  1.3689 -qed
  1.3690 -
  1.3691 -lemma continuous_closed_in_preimage:
  1.3692 -  assumes "continuous_on s f"  "closed t"
  1.3693 -  shows "closedin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
  1.3694 -proof-
  1.3695 -  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
  1.3696 -  have "closedin (subtopology euclidean (f ` s)) (t \<inter> f ` s)"
  1.3697 -    using closedin_closed_Int[of t "f ` s", OF assms(2)] unfolding Int_commute by auto
  1.3698 -  thus ?thesis
  1.3699 -    using assms(1)[unfolded continuous_on_closed, THEN spec[where x="t \<inter> f ` s"]] using * by auto
  1.3700 -qed
  1.3701 -
  1.3702 -lemma continuous_open_preimage:
  1.3703 -  assumes "continuous_on s f" "open s" "open t"
  1.3704 -  shows "open {x \<in> s. f x \<in> t}"
  1.3705 -proof-
  1.3706 -  obtain T where T: "open T" "{x \<in> s. f x \<in> t} = s \<inter> T"
  1.3707 -    using continuous_open_in_preimage[OF assms(1,3)] unfolding openin_open by auto
  1.3708 -  thus ?thesis using open_Int[of s T, OF assms(2)] by auto
  1.3709 -qed
  1.3710 -
  1.3711 -lemma continuous_closed_preimage:
  1.3712 -  assumes "continuous_on s f" "closed s" "closed t"
  1.3713 -  shows "closed {x \<in> s. f x \<in> t}"
  1.3714 -proof-
  1.3715 -  obtain T where T: "closed T" "{x \<in> s. f x \<in> t} = s \<inter> T"
  1.3716 -    using continuous_closed_in_preimage[OF assms(1,3)] unfolding closedin_closed by auto
  1.3717 -  thus ?thesis using closed_Int[of s T, OF assms(2)] by auto
  1.3718 -qed
  1.3719 -
  1.3720 -lemma continuous_open_preimage_univ:
  1.3721 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
  1.3722 -  shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open {x. f x \<in> s}"
  1.3723 -  using continuous_open_preimage[of UNIV f s] open_UNIV continuous_at_imp_continuous_on by auto
  1.3724 -
  1.3725 -lemma continuous_closed_preimage_univ:
  1.3726 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
  1.3727 -  shows "(\<forall>x. continuous (at x) f) \<Longrightarrow> closed s ==> closed {x. f x \<in> s}"
  1.3728 -  using continuous_closed_preimage[of UNIV f s] closed_UNIV continuous_at_imp_continuous_on by auto
  1.3729 -
  1.3730 -lemma continuous_open_vimage:
  1.3731 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
  1.3732 -  shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open (f -` s)"
  1.3733 -  unfolding vimage_def by (rule continuous_open_preimage_univ)
  1.3734 -
  1.3735 -lemma continuous_closed_vimage:
  1.3736 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
  1.3737 -  shows "\<forall>x. continuous (at x) f \<Longrightarrow> closed s \<Longrightarrow> closed (f -` s)"
  1.3738 -  unfolding vimage_def by (rule continuous_closed_preimage_univ)
  1.3739 -
  1.3740 -text{* Equality of continuous functions on closure and related results.          *}
  1.3741 -
  1.3742 -lemma continuous_closed_in_preimage_constant:
  1.3743 - "continuous_on s f ==> closedin (subtopology euclidean s) {x \<in> s. f x = a}"
  1.3744 -  using continuous_closed_in_preimage[of s f "{a}"] closed_sing by auto
  1.3745 -
  1.3746 -lemma continuous_closed_preimage_constant:
  1.3747 - "continuous_on s f \<Longrightarrow> closed s ==> closed {x \<in> s. f x = a}"
  1.3748 -  using continuous_closed_preimage[of s f "{a}"] closed_sing by auto
  1.3749 -
  1.3750 -lemma continuous_constant_on_closure:
  1.3751 -  assumes "continuous_on (closure s) f"
  1.3752 -          "\<forall>x \<in> s. f x = a"
  1.3753 -  shows "\<forall>x \<in> (closure s). f x = a"
  1.3754 -    using continuous_closed_preimage_constant[of "closure s" f a]
  1.3755 -    assms closure_minimal[of s "{x \<in> closure s. f x = a}"] closure_subset unfolding subset_eq by auto
  1.3756 -
  1.3757 -lemma image_closure_subset:
  1.3758 -  assumes "continuous_on (closure s) f"  "closed t"  "(f ` s) \<subseteq> t"
  1.3759 -  shows "f ` (closure s) \<subseteq> t"
  1.3760 -proof-
  1.3761 -  have "s \<subseteq> {x \<in> closure s. f x \<in> t}" using assms(3) closure_subset by auto
  1.3762 -  moreover have "closed {x \<in> closure s. f x \<in> t}"
  1.3763 -    using continuous_closed_preimage[OF assms(1)] and assms(2) by auto
  1.3764 -  ultimately have "closure s = {x \<in> closure s . f x \<in> t}"
  1.3765 -    using closure_minimal[of s "{x \<in> closure s. f x \<in> t}"] by auto
  1.3766 -  thus ?thesis by auto
  1.3767 -qed
  1.3768 -
  1.3769 -lemma continuous_on_closure_norm_le:
  1.3770 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  1.3771 -  assumes "continuous_on (closure s) f"  "\<forall>y \<in> s. norm(f y) \<le> b"  "x \<in> (closure s)"
  1.3772 -  shows "norm(f x) \<le> b"
  1.3773 -proof-
  1.3774 -  have *:"f ` s \<subseteq> cball 0 b" using assms(2)[unfolded mem_cball_0[THEN sym]] by auto
  1.3775 -  show ?thesis
  1.3776 -    using image_closure_subset[OF assms(1) closed_cball[of 0 b] *] assms(3)
  1.3777 -    unfolding subset_eq apply(erule_tac x="f x" in ballE) by (auto simp add: dist_norm)
  1.3778 -qed
  1.3779 -
  1.3780 -text{* Making a continuous function avoid some value in a neighbourhood.         *}
  1.3781 -
  1.3782 -lemma continuous_within_avoid:
  1.3783 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
  1.3784 -  assumes "continuous (at x within s) f"  "x \<in> s"  "f x \<noteq> a"
  1.3785 -  shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e --> f y \<noteq> a"
  1.3786 -proof-
  1.3787 -  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"
  1.3788 -    using assms(1)[unfolded continuous_within Lim_within, THEN spec[where x="dist (f x) a"]] assms(3)[unfolded dist_nz] by auto
  1.3789 -  { fix y assume " y\<in>s"  "dist x y < d"
  1.3790 -    hence "f y \<noteq> a" using d[THEN bspec[where x=y]] assms(3)[unfolded dist_nz]
  1.3791 -      apply auto unfolding dist_nz[THEN sym] by (auto simp add: dist_commute) }
  1.3792 -  thus ?thesis using `d>0` by auto
  1.3793 -qed
  1.3794 -
  1.3795 -lemma continuous_at_avoid:
  1.3796 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
  1.3797 -  assumes "continuous (at x) f"  "f x \<noteq> a"
  1.3798 -  shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"
  1.3799 -using assms using continuous_within_avoid[of x UNIV f a, unfolded within_UNIV] by auto
  1.3800 -
  1.3801 -lemma continuous_on_avoid:
  1.3802 -  assumes "continuous_on s f"  "x \<in> s"  "f x \<noteq> a"
  1.3803 -  shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e \<longrightarrow> f y \<noteq> a"
  1.3804 -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
  1.3805 -
  1.3806 -lemma continuous_on_open_avoid:
  1.3807 -  assumes "continuous_on s f"  "open s"  "x \<in> s"  "f x \<noteq> a"
  1.3808 -  shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"
  1.3809 -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
  1.3810 -
  1.3811 -text{* Proving a function is constant by proving open-ness of level set.         *}
  1.3812 -
  1.3813 -lemma continuous_levelset_open_in_cases:
  1.3814 - "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>
  1.3815 -        openin (subtopology euclidean s) {x \<in> s. f x = a}
  1.3816 -        ==> (\<forall>x \<in> s. f x \<noteq> a) \<or> (\<forall>x \<in> s. f x = a)"
  1.3817 -unfolding connected_clopen using continuous_closed_in_preimage_constant by auto
  1.3818 -
  1.3819 -lemma continuous_levelset_open_in:
  1.3820 - "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>
  1.3821 -        openin (subtopology euclidean s) {x \<in> s. f x = a} \<Longrightarrow>
  1.3822 -        (\<exists>x \<in> s. f x = a)  ==> (\<forall>x \<in> s. f x = a)"
  1.3823 -using continuous_levelset_open_in_cases[of s f ]
  1.3824 -by meson
  1.3825 -
  1.3826 -lemma continuous_levelset_open:
  1.3827 -  assumes "connected s"  "continuous_on s f"  "open {x \<in> s. f x = a}"  "\<exists>x \<in> s.  f x = a"
  1.3828 -  shows "\<forall>x \<in> s. f x = a"
  1.3829 -using continuous_levelset_open_in[OF assms(1,2), of a, unfolded openin_open] using assms (3,4) by auto
  1.3830 -
  1.3831 -text{* Some arithmetical combinations (more to prove).                           *}
  1.3832 -
  1.3833 -lemma open_scaling[intro]:
  1.3834 -  fixes s :: "'a::real_normed_vector set"
  1.3835 -  assumes "c \<noteq> 0"  "open s"
  1.3836 -  shows "open((\<lambda>x. c *\<^sub>R x) ` s)"
  1.3837 -proof-
  1.3838 -  { fix x assume "x \<in> s"
  1.3839 -    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
  1.3840 -    have "e * abs c > 0" using assms(1)[unfolded zero_less_abs_iff[THEN sym]] using real_mult_order[OF `e>0`] by auto
  1.3841 -    moreover
  1.3842 -    { fix y assume "dist y (c *\<^sub>R x) < e * \<bar>c\<bar>"
  1.3843 -      hence "norm ((1 / c) *\<^sub>R y - x) < e" unfolding dist_norm
  1.3844 -        using norm_scaleR[of c "(1 / c) *\<^sub>R y - x", unfolded scaleR_right_diff_distrib, unfolded scaleR_scaleR] assms(1)
  1.3845 -          assms(1)[unfolded zero_less_abs_iff[THEN sym]] by (simp del:zero_less_abs_iff)
  1.3846 -      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  }
  1.3847 -    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  }
  1.3848 -  thus ?thesis unfolding open_dist by auto
  1.3849 -qed
  1.3850 -
  1.3851 -lemma minus_image_eq_vimage:
  1.3852 -  fixes A :: "'a::ab_group_add set"
  1.3853 -  shows "(\<lambda>x. - x) ` A = (\<lambda>x. - x) -` A"
  1.3854 -  by (auto intro!: image_eqI [where f="\<lambda>x. - x"])
  1.3855 -
  1.3856 -lemma open_negations:
  1.3857 -  fixes s :: "'a::real_normed_vector set"
  1.3858 -  shows "open s ==> open ((\<lambda> x. -x) ` s)"
  1.3859 -  unfolding scaleR_minus1_left [symmetric]
  1.3860 -  by (rule open_scaling, auto)
  1.3861 -
  1.3862 -lemma open_translation:
  1.3863 -  fixes s :: "'a::real_normed_vector set"
  1.3864 -  assumes "open s"  shows "open((\<lambda>x. a + x) ` s)"
  1.3865 -proof-
  1.3866 -  { 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  }
  1.3867 -  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
  1.3868 -  ultimately show ?thesis using continuous_open_preimage_univ[of "\<lambda>x. x - a" s] using assms by auto
  1.3869 -qed
  1.3870 -
  1.3871 -lemma open_affinity:
  1.3872 -  fixes s :: "'a::real_normed_vector set"
  1.3873 -  assumes "open s"  "c \<noteq> 0"
  1.3874 -  shows "open ((\<lambda>x. a + c *\<^sub>R x) ` s)"
  1.3875 -proof-
  1.3876 -  have *:"(\<lambda>x. a + c *\<^sub>R x) = (\<lambda>x. a + x) \<circ> (\<lambda>x. c *\<^sub>R x)" unfolding o_def ..
  1.3877 -  have "op + a ` op *\<^sub>R c ` s = (op + a \<circ> op *\<^sub>R c) ` s" by auto
  1.3878 -  thus ?thesis using assms open_translation[of "op *\<^sub>R c ` s" a] unfolding * by auto
  1.3879 -qed
  1.3880 -
  1.3881 -lemma interior_translation:
  1.3882 -  fixes s :: "'a::real_normed_vector set"
  1.3883 -  shows "interior ((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (interior s)"
  1.3884 -proof (rule set_ext, rule)
  1.3885 -  fix x assume "x \<in> interior (op + a ` s)"
  1.3886 -  then obtain e where "e>0" and e:"ball x e \<subseteq> op + a ` s" unfolding mem_interior by auto
  1.3887 -  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
  1.3888 -  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
  1.3889 -next
  1.3890 -  fix x assume "x \<in> op + a ` interior s"
  1.3891 -  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
  1.3892 -  { fix z have *:"a + y - z = y + a - z" by auto
  1.3893 -    assume "z\<in>ball x e"
  1.3894 -    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
  1.3895 -    hence "z \<in> op + a ` s" unfolding image_iff by(auto intro!: bexI[where x="z - a"])  }
  1.3896 -  hence "ball x e \<subseteq> op + a ` s" unfolding subset_eq by auto
  1.3897 -  thus "x \<in> interior (op + a ` s)" unfolding mem_interior using `e>0` by auto
  1.3898 -qed
  1.3899 -
  1.3900 -subsection {* Preservation of compactness and connectedness under continuous function.  *}
  1.3901 -
  1.3902 -lemma compact_continuous_image:
  1.3903 -  assumes "continuous_on s f"  "compact s"
  1.3904 -  shows "compact(f ` s)"
  1.3905 -proof-
  1.3906 -  { fix x assume x:"\<forall>n::nat. x n \<in> f ` s"
  1.3907 -    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
  1.3908 -    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
  1.3909 -    { fix e::real assume "e>0"
  1.3910 -      then obtain d where "d>0" and d:"\<forall>x'\<in>s. dist x' l < d \<longrightarrow> dist (f x') (f l) < e" using assms(1)[unfolded continuous_on_def, THEN bspec[where x=l], OF `l\<in>s`] by auto
  1.3911 -      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
  1.3912 -      { 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  }
  1.3913 -      hence "\<exists>N. \<forall>n\<ge>N. dist ((x \<circ> r) n) (f l) < e" by auto  }
  1.3914 -    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  }
  1.3915 -  thus ?thesis unfolding compact_def by auto
  1.3916 -qed
  1.3917 -
  1.3918 -lemma connected_continuous_image:
  1.3919 -  assumes "continuous_on s f"  "connected s"
  1.3920 -  shows "connected(f ` s)"
  1.3921 -proof-
  1.3922 -  { fix T assume as: "T \<noteq> {}"  "T \<noteq> f ` s"  "openin (subtopology euclidean (f ` s)) T"  "closedin (subtopology euclidean (f ` s)) T"
  1.3923 -    have "{x \<in> s. f x \<in> T} = {} \<or> {x \<in> s. f x \<in> T} = s"
  1.3924 -      using assms(1)[unfolded continuous_on_open, THEN spec[where x=T]]
  1.3925 -      using assms(1)[unfolded continuous_on_closed, THEN spec[where x=T]]
  1.3926 -      using assms(2)[unfolded connected_clopen, THEN spec[where x="{x \<in> s. f x \<in> T}"]] as(3,4) by auto
  1.3927 -    hence False using as(1,2)
  1.3928 -      using as(4)[unfolded closedin_def topspace_euclidean_subtopology] by auto }
  1.3929 -  thus ?thesis unfolding connected_clopen by auto
  1.3930 -qed
  1.3931 -
  1.3932 -text{* Continuity implies uniform continuity on a compact domain.                *}
  1.3933 -
  1.3934 -lemma compact_uniformly_continuous:
  1.3935 -  assumes "continuous_on s f"  "compact s"
  1.3936 -  shows "uniformly_continuous_on s f"
  1.3937 -proof-
  1.3938 -    { fix x assume x:"x\<in>s"
  1.3939 -      hence "\<forall>xa. \<exists>y. 0 < xa \<longrightarrow> (y > 0 \<and> (\<forall>x'\<in>s. dist x' x < y \<longrightarrow> dist (f x') (f x) < xa))" using assms(1)[unfolded continuous_on_def, THEN bspec[where x=x]] by auto
  1.3940 -      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  }
  1.3941 -    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
  1.3942 -    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)"
  1.3943 -      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
  1.3944 -
  1.3945 -  { fix e::real assume "e>0"
  1.3946 -
  1.3947 -    { 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  }
  1.3948 -    hence "s \<subseteq> \<Union>{ball x (d x (e / 2)) |x. x \<in> s}" unfolding subset_eq by auto
  1.3949 -    moreover
  1.3950 -    { fix b assume "b\<in>{ball x (d x (e / 2)) |x. x \<in> s}" hence "open b" by auto  }
  1.3951 -    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
  1.3952 -
  1.3953 -    { fix x y assume "x\<in>s" "y\<in>s" and as:"dist y x < ea"
  1.3954 -      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
  1.3955 -      hence "x\<in>ball z (d z (e / 2))" using `ea>0` unfolding subset_eq by auto
  1.3956 -      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`
  1.3957 -        by (auto  simp add: dist_commute)
  1.3958 -      moreover have "y\<in>ball z (d z (e / 2))" using as and `ea>0` and z[unfolded subset_eq]
  1.3959 -        by (auto simp add: dist_commute)
  1.3960 -      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`
  1.3961 -        by (auto  simp add: dist_commute)
  1.3962 -      ultimately have "dist (f y) (f x) < e" using dist_triangle_half_r[of "f z" "f x" e "f y"]
  1.3963 -        by (auto simp add: dist_commute)  }
  1.3964 -    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  }
  1.3965 -  thus ?thesis unfolding uniformly_continuous_on_def by auto
  1.3966 -qed
  1.3967 -
  1.3968 -text{* Continuity of inverse function on compact domain. *}
  1.3969 -
  1.3970 -lemma continuous_on_inverse:
  1.3971 -  fixes f :: "'a::heine_borel \<Rightarrow> 'b::heine_borel"
  1.3972 -    (* TODO: can this be generalized more? *)
  1.3973 -  assumes "continuous_on s f"  "compact s"  "\<forall>x \<in> s. g (f x) = x"
  1.3974 -  shows "continuous_on (f ` s) g"
  1.3975 -proof-
  1.3976 -  have *:"g ` f ` s = s" using assms(3) by (auto simp add: image_iff)
  1.3977 -  { fix t assume t:"closedin (subtopology euclidean (g ` f ` s)) t"
  1.3978 -    then obtain T where T: "closed T" "t = s \<inter> T" unfolding closedin_closed unfolding * by auto
  1.3979 -    have "continuous_on (s \<inter> T) f" using continuous_on_subset[OF assms(1), of "s \<inter> t"]
  1.3980 -      unfolding T(2) and Int_left_absorb by auto
  1.3981 -    moreover have "compact (s \<inter> T)"
  1.3982 -      using assms(2) unfolding compact_eq_bounded_closed
  1.3983 -      using bounded_subset[of s "s \<inter> T"] and T(1) by auto
  1.3984 -    ultimately have "closed (f ` t)" using T(1) unfolding T(2)
  1.3985 -      using compact_continuous_image [of "s \<inter> T" f] unfolding compact_eq_bounded_closed by auto
  1.3986 -    moreover have "{x \<in> f ` s. g x \<in> t} = f ` s \<inter> f ` t" using assms(3) unfolding T(2) by auto
  1.3987 -    ultimately have "closedin (subtopology euclidean (f ` s)) {x \<in> f ` s. g x \<in> t}"
  1.3988 -      unfolding closedin_closed by auto  }
  1.3989 -  thus ?thesis unfolding continuous_on_closed by auto
  1.3990 -qed
  1.3991 -
  1.3992 -subsection{* A uniformly convergent limit of continuous functions is continuous.       *}
  1.3993 -
  1.3994 -lemma norm_triangle_lt:
  1.3995 -  fixes x y :: "'a::real_normed_vector"
  1.3996 -  shows "norm x + norm y < e \<Longrightarrow> norm (x + y) < e"
  1.3997 -by (rule le_less_trans [OF norm_triangle_ineq])
  1.3998 -
  1.3999 -lemma continuous_uniform_limit:
  1.4000 -  fixes f :: "'a \<Rightarrow> 'b::metric_space \<Rightarrow> 'c::real_normed_vector"
  1.4001 -  assumes "\<not> (trivial_limit net)"  "eventually (\<lambda>n. continuous_on s (f n)) net"
  1.4002 -  "\<forall>e>0. eventually (\<lambda>n. \<forall>x \<in> s. norm(f n x - g x) < e) net"
  1.4003 -  shows "continuous_on s g"
  1.4004 -proof-
  1.4005 -  { fix x and e::real assume "x\<in>s" "e>0"
  1.4006 -    have "eventually (\<lambda>n. \<forall>x\<in>s. norm (f n x - g x) < e / 3) net" using `e>0` assms(3)[THEN spec[where x="e/3"]] by auto
  1.4007 -    then obtain n where n:"\<forall>xa\<in>s. norm (f n xa - g xa) < e / 3"  "continuous_on s (f n)"
  1.4008 -      using eventually_and[of "(\<lambda>n. \<forall>x\<in>s. norm (f n x - g x) < e / 3)" "(\<lambda>n. continuous_on s (f n))" net] assms(1,2) eventually_happens by blast
  1.4009 -    have "e / 3 > 0" using `e>0` by auto
  1.4010 -    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"
  1.4011 -      using n(2)[unfolded continuous_on_def, THEN bspec[where x=x], OF `x\<in>s`, THEN spec[where x="e/3"]] by blast
  1.4012 -    { fix y assume "y\<in>s" "dist y x < d"
  1.4013 -      hence "dist (f n y) (f n x) < e / 3" using d[THEN bspec[where x=y]] by auto
  1.4014 -      hence "norm (f n y - g x) < 2 * e / 3" using norm_triangle_lt[of "f n y - f n x" "f n x - g x" "2*e/3"]
  1.4015 -        using n(1)[THEN bspec[where x=x], OF `x\<in>s`] unfolding dist_norm unfolding ab_group_add_class.ab_diff_minus by auto
  1.4016 -      hence "dist (g y) (g x) < e" unfolding dist_norm using n(1)[THEN bspec[where x=y], OF `y\<in>s`]
  1.4017 -        unfolding norm_minus_cancel[of "f n y - g y", THEN sym] using norm_triangle_lt[of "f n y - g x" "g y - f n y" e] by (auto simp add: uminus_add_conv_diff)  }
  1.4018 -    hence "\<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (g x') (g x) < e" using `d>0` by auto  }
  1.4019 -  thus ?thesis unfolding continuous_on_def by auto
  1.4020 -qed
  1.4021 -
  1.4022 -subsection{* Topological properties of linear functions.                               *}
  1.4023 -
  1.4024 -lemma linear_lim_0:
  1.4025 -  assumes "bounded_linear f" shows "(f ---> 0) (at (0))"
  1.4026 -proof-
  1.4027 -  interpret f: bounded_linear f by fact
  1.4028 -  have "(f ---> f 0) (at 0)"
  1.4029 -    using tendsto_ident_at by (rule f.tendsto)
  1.4030 -  thus ?thesis unfolding f.zero .
  1.4031 -qed
  1.4032 -
  1.4033 -lemma linear_continuous_at:
  1.4034 -  assumes "bounded_linear f"  shows "continuous (at a) f"
  1.4035 -  unfolding continuous_at using assms
  1.4036 -  apply (rule bounded_linear.tendsto)
  1.4037 -  apply (rule tendsto_ident_at)
  1.4038 -  done
  1.4039 -
  1.4040 -lemma linear_continuous_within:
  1.4041 -  shows "bounded_linear f ==> continuous (at x within s) f"
  1.4042 -  using continuous_at_imp_continuous_within[of x f s] using linear_continuous_at[of f] by auto
  1.4043 -
  1.4044 -lemma linear_continuous_on:
  1.4045 -  shows "bounded_linear f ==> continuous_on s f"
  1.4046 -  using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto
  1.4047 -
  1.4048 -text{* Also bilinear functions, in composition form.                             *}
  1.4049 -
  1.4050 -lemma bilinear_continuous_at_compose:
  1.4051 -  shows "continuous (at x) f \<Longrightarrow> continuous (at x) g \<Longrightarrow> bounded_bilinear h
  1.4052 -        ==> continuous (at x) (\<lambda>x. h (f x) (g x))"
  1.4053 -  unfolding continuous_at using Lim_bilinear[of f "f x" "(at x)" g "g x" h] by auto
  1.4054 -
  1.4055 -lemma bilinear_continuous_within_compose:
  1.4056 -  shows "continuous (at x within s) f \<Longrightarrow> continuous (at x within s) g \<Longrightarrow> bounded_bilinear h
  1.4057 -        ==> continuous (at x within s) (\<lambda>x. h (f x) (g x))"
  1.4058 -  unfolding continuous_within using Lim_bilinear[of f "f x"] by auto
  1.4059 -
  1.4060 -lemma bilinear_continuous_on_compose:
  1.4061 -  shows "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> bounded_bilinear h
  1.4062 -             ==> continuous_on s (\<lambda>x. h (f x) (g x))"
  1.4063 -  unfolding continuous_on_eq_continuous_within apply auto apply(erule_tac x=x in ballE) apply auto apply(erule_tac x=x in ballE) apply auto
  1.4064 -  using bilinear_continuous_within_compose[of _ s f g h] by auto
  1.4065 -
  1.4066 -subsection{* Topological stuff lifted from and dropped to R                            *}
  1.4067 -
  1.4068 -
  1.4069 -lemma open_real:
  1.4070 -  fixes s :: "real set" shows
  1.4071 - "open s \<longleftrightarrow>
  1.4072 -        (\<forall>x \<in> s. \<exists>e>0. \<forall>x'. abs(x' - x) < e --> x' \<in> s)" (is "?lhs = ?rhs")
  1.4073 -  unfolding open_dist dist_norm by simp
  1.4074 -
  1.4075 -lemma islimpt_approachable_real:
  1.4076 -  fixes s :: "real set"
  1.4077 -  shows "x islimpt s \<longleftrightarrow> (\<forall>e>0.  \<exists>x'\<in> s. x' \<noteq> x \<and> abs(x' - x) < e)"
  1.4078 -  unfolding islimpt_approachable dist_norm by simp
  1.4079 -
  1.4080 -lemma closed_real:
  1.4081 -  fixes s :: "real set"
  1.4082 -  shows "closed s \<longleftrightarrow>
  1.4083 -        (\<forall>x. (\<forall>e>0.  \<exists>x' \<in> s. x' \<noteq> x \<and> abs(x' - x) < e)
  1.4084 -            --> x \<in> s)"
  1.4085 -  unfolding closed_limpt islimpt_approachable dist_norm by simp
  1.4086 -
  1.4087 -lemma continuous_at_real_range:
  1.4088 -  fixes f :: "'a::real_normed_vector \<Rightarrow> real"
  1.4089 -  shows "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.
  1.4090 -        \<forall>x'. norm(x' - x) < d --> abs(f x' - f x) < e)"
  1.4091 -  unfolding continuous_at unfolding Lim_at
  1.4092 -  unfolding dist_nz[THEN sym] unfolding dist_norm apply auto
  1.4093 -  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
  1.4094 -  apply(erule_tac x=e in allE) by auto
  1.4095 -
  1.4096 -lemma continuous_on_real_range:
  1.4097 -  fixes f :: "'a::real_normed_vector \<Rightarrow> real"
  1.4098 -  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))"
  1.4099 -  unfolding continuous_on_def dist_norm by simp
  1.4100 -
  1.4101 -lemma continuous_at_norm: "continuous (at x) norm"
  1.4102 -  unfolding continuous_at by (intro tendsto_intros)
  1.4103 -
  1.4104 -lemma continuous_on_norm: "continuous_on s norm"
  1.4105 -unfolding continuous_on by (intro ballI tendsto_intros)
  1.4106 -
  1.4107 -lemma continuous_at_component: "continuous (at a) (\<lambda>x. x $ i)"
  1.4108 -unfolding continuous_at by (intro tendsto_intros)
  1.4109 -
  1.4110 -lemma continuous_on_component: "continuous_on s (\<lambda>x. x $ i)"
  1.4111 -unfolding continuous_on by (intro ballI tendsto_intros)
  1.4112 -
  1.4113 -lemma continuous_at_infnorm: "continuous (at x) infnorm"
  1.4114 -  unfolding continuous_at Lim_at o_def unfolding dist_norm
  1.4115 -  apply auto apply (rule_tac x=e in exI) apply auto
  1.4116 -  using order_trans[OF real_abs_sub_infnorm infnorm_le_norm, of _ x] by (metis xt1(7))
  1.4117 -
  1.4118 -text{* Hence some handy theorems on distance, diameter etc. of/from a set.       *}
  1.4119 -
  1.4120 -lemma compact_attains_sup:
  1.4121 -  fixes s :: "real set"
  1.4122 -  assumes "compact s"  "s \<noteq> {}"
  1.4123 -  shows "\<exists>x \<in> s. \<forall>y \<in> s. y \<le> x"
  1.4124 -proof-
  1.4125 -  from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto
  1.4126 -  { fix e::real assume as: "\<forall>x\<in>s. x \<le> rsup s" "rsup s \<notin> s"  "0 < e" "\<forall>x'\<in>s. x' = rsup s \<or> \<not> rsup s - x' < e"
  1.4127 -    have "isLub UNIV s (rsup s)" using rsup[OF assms(2)] unfolding setle_def using as(1) by auto
  1.4128 -    moreover have "isUb UNIV s (rsup s - e)" unfolding isUb_def unfolding setle_def using as(4,2) by auto
  1.4129 -    ultimately have False using isLub_le_isUb[of UNIV s "rsup s" "rsup s - e"] using `e>0` by auto  }
  1.4130 -  thus ?thesis using bounded_has_rsup(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="rsup s"]]
  1.4131 -    apply(rule_tac x="rsup s" in bexI) by auto
  1.4132 -qed
  1.4133 -
  1.4134 -lemma compact_attains_inf:
  1.4135 -  fixes s :: "real set"
  1.4136 -  assumes "compact s" "s \<noteq> {}"  shows "\<exists>x \<in> s. \<forall>y \<in> s. x \<le> y"
  1.4137 -proof-
  1.4138 -  from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto
  1.4139 -  { fix e::real assume as: "\<forall>x\<in>s. x \<ge> rinf s"  "rinf s \<notin> s"  "0 < e"
  1.4140 -      "\<forall>x'\<in>s. x' = rinf s \<or> \<not> abs (x' - rinf s) < e"
  1.4141 -    have "isGlb UNIV s (rinf s)" using rinf[OF assms(2)] unfolding setge_def using as(1) by auto
  1.4142 -    moreover
  1.4143 -    { fix x assume "x \<in> s"
  1.4144 -      hence *:"abs (x - rinf s) = x - rinf s" using as(1)[THEN bspec[where x=x]] by auto
  1.4145 -      have "rinf s + e \<le> x" using as(4)[THEN bspec[where x=x]] using as(2) `x\<in>s` unfolding * by auto }
  1.4146 -    hence "isLb UNIV s (rinf s + e)" unfolding isLb_def and setge_def by auto
  1.4147 -    ultimately have False using isGlb_le_isLb[of UNIV s "rinf s" "rinf s + e"] using `e>0` by auto  }
  1.4148 -  thus ?thesis using bounded_has_rinf(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="rinf s"]]
  1.4149 -    apply(rule_tac x="rinf s" in bexI) by auto
  1.4150 -qed
  1.4151 -
  1.4152 -lemma continuous_attains_sup:
  1.4153 -  fixes f :: "'a::metric_space \<Rightarrow> real"
  1.4154 -  shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f
  1.4155 -        ==> (\<exists>x \<in> s. \<forall>y \<in> s.  f y \<le> f x)"
  1.4156 -  using compact_attains_sup[of "f ` s"]
  1.4157 -  using compact_continuous_image[of s f] by auto
  1.4158 -
  1.4159 -lemma continuous_attains_inf:
  1.4160 -  fixes f :: "'a::metric_space \<Rightarrow> real"
  1.4161 -  shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f
  1.4162 -        \<Longrightarrow> (\<exists>x \<in> s. \<forall>y \<in> s. f x \<le> f y)"
  1.4163 -  using compact_attains_inf[of "f ` s"]
  1.4164 -  using compact_continuous_image[of s f] by auto
  1.4165 -
  1.4166 -lemma distance_attains_sup:
  1.4167 -  assumes "compact s" "s \<noteq> {}"
  1.4168 -  shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a y \<le> dist a x"
  1.4169 -proof (rule continuous_attains_sup [OF assms])
  1.4170 -  { fix x assume "x\<in>s"
  1.4171 -    have "(dist a ---> dist a x) (at x within s)"
  1.4172 -      by (intro tendsto_dist tendsto_const Lim_at_within Lim_ident_at)
  1.4173 -  }
  1.4174 -  thus "continuous_on s (dist a)"
  1.4175 -    unfolding continuous_on ..
  1.4176 -qed
  1.4177 -
  1.4178 -text{* For *minimal* distance, we only need closure, not compactness.            *}
  1.4179 -
  1.4180 -lemma distance_attains_inf:
  1.4181 -  fixes a :: "'a::heine_borel"
  1.4182 -  assumes "closed s"  "s \<noteq> {}"
  1.4183 -  shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a x \<le> dist a y"
  1.4184 -proof-
  1.4185 -  from assms(2) obtain b where "b\<in>s" by auto
  1.4186 -  let ?B = "cball a (dist b a) \<inter> s"
  1.4187 -  have "b \<in> ?B" using `b\<in>s` by (simp add: dist_commute)
  1.4188 -  hence "?B \<noteq> {}" by auto
  1.4189 -  moreover
  1.4190 -  { fix x assume "x\<in>?B"
  1.4191 -    fix e::real assume "e>0"
  1.4192 -    { fix x' assume "x'\<in>?B" and as:"dist x' x < e"
  1.4193 -      from as have "\<bar>dist a x' - dist a x\<bar> < e"
  1.4194 -        unfolding abs_less_iff minus_diff_eq
  1.4195 -        using dist_triangle2 [of a x' x]
  1.4196 -        using dist_triangle [of a x x']
  1.4197 -        by arith
  1.4198 -    }
  1.4199 -    hence "\<exists>d>0. \<forall>x'\<in>?B. dist x' x < d \<longrightarrow> \<bar>dist a x' - dist a x\<bar> < e"
  1.4200 -      using `e>0` by auto
  1.4201 -  }
  1.4202 -  hence "continuous_on (cball a (dist b a) \<inter> s) (dist a)"
  1.4203 -    unfolding continuous_on Lim_within dist_norm real_norm_def
  1.4204 -    by fast
  1.4205 -  moreover have "compact ?B"
  1.4206 -    using compact_cball[of a "dist b a"]
  1.4207 -    unfolding compact_eq_bounded_closed
  1.4208 -    using bounded_Int and closed_Int and assms(1) by auto
  1.4209 -  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"
  1.4210 -    using continuous_attains_inf[of ?B "dist a"] by fastsimp
  1.4211 -  thus ?thesis by fastsimp
  1.4212 -qed
  1.4213 -
  1.4214 -subsection{* We can now extend limit compositions to consider the scalar multiplier.   *}
  1.4215 -
  1.4216 -lemma Lim_mul:
  1.4217 -  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1.4218 -  assumes "(c ---> d) net"  "(f ---> l) net"
  1.4219 -  shows "((\<lambda>x. c(x) *\<^sub>R f x) ---> (d *\<^sub>R l)) net"
  1.4220 -  using assms by (rule scaleR.tendsto)
  1.4221 -
  1.4222 -lemma Lim_vmul:
  1.4223 -  fixes c :: "'a \<Rightarrow> real" and v :: "'b::real_normed_vector"
  1.4224 -  shows "(c ---> d) net ==> ((\<lambda>x. c(x) *\<^sub>R v) ---> d *\<^sub>R v) net"
  1.4225 -  by (intro tendsto_intros)
  1.4226 -
  1.4227 -lemma continuous_vmul:
  1.4228 -  fixes c :: "'a::metric_space \<Rightarrow> real" and v :: "'b::real_normed_vector"
  1.4229 -  shows "continuous net c ==> continuous net (\<lambda>x. c(x) *\<^sub>R v)"
  1.4230 -  unfolding continuous_def using Lim_vmul[of c] by auto
  1.4231 -
  1.4232 -lemma continuous_mul:
  1.4233 -  fixes c :: "'a::metric_space \<Rightarrow> real"
  1.4234 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  1.4235 -  shows "continuous net c \<Longrightarrow> continuous net f
  1.4236 -             ==> continuous net (\<lambda>x. c(x) *\<^sub>R f x) "
  1.4237 -  unfolding continuous_def by (intro tendsto_intros)
  1.4238 -
  1.4239 -lemma continuous_on_vmul:
  1.4240 -  fixes c :: "'a::metric_space \<Rightarrow> real" and v :: "'b::real_normed_vector"
  1.4241 -  shows "continuous_on s c ==> continuous_on s (\<lambda>x. c(x) *\<^sub>R v)"
  1.4242 -  unfolding continuous_on_eq_continuous_within using continuous_vmul[of _ c] by auto
  1.4243 -
  1.4244 -lemma continuous_on_mul:
  1.4245 -  fixes c :: "'a::metric_space \<Rightarrow> real"
  1.4246 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  1.4247 -  shows "continuous_on s c \<Longrightarrow> continuous_on s f
  1.4248 -             ==> continuous_on s (\<lambda>x. c(x) *\<^sub>R f x)"
  1.4249 -  unfolding continuous_on_eq_continuous_within using continuous_mul[of _ c] by auto
  1.4250 -
  1.4251 -text{* And so we have continuity of inverse.                                     *}
  1.4252 -
  1.4253 -lemma Lim_inv:
  1.4254 -  fixes f :: "'a \<Rightarrow> real"
  1.4255 -  assumes "(f ---> l) (net::'a net)"  "l \<noteq> 0"
  1.4256 -  shows "((inverse o f) ---> inverse l) net"
  1.4257 -  unfolding o_def using assms by (rule tendsto_inverse)
  1.4258 -
  1.4259 -lemma continuous_inv:
  1.4260 -  fixes f :: "'a::metric_space \<Rightarrow> real"
  1.4261 -  shows "continuous net f \<Longrightarrow> f(netlimit net) \<noteq> 0
  1.4262 -           ==> continuous net (inverse o f)"
  1.4263 -  unfolding continuous_def using Lim_inv by auto
  1.4264 -
  1.4265 -lemma continuous_at_within_inv:
  1.4266 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_field"
  1.4267 -  assumes "continuous (at a within s) f" "f a \<noteq> 0"
  1.4268 -  shows "continuous (at a within s) (inverse o f)"
  1.4269 -  using assms unfolding continuous_within o_def
  1.4270 -  by (intro tendsto_intros)
  1.4271 -
  1.4272 -lemma continuous_at_inv:
  1.4273 -  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_field"
  1.4274 -  shows "continuous (at a) f \<Longrightarrow> f a \<noteq> 0
  1.4275 -         ==> continuous (at a) (inverse o f) "
  1.4276 -  using within_UNIV[THEN sym, of "at a"] using continuous_at_within_inv[of a UNIV] by auto
  1.4277 -
  1.4278 -subsection{* Preservation properties for pasted sets.                                  *}
  1.4279 -
  1.4280 -lemma bounded_pastecart:
  1.4281 -  fixes s :: "('a::real_normed_vector ^ _) set" (* FIXME: generalize to metric_space *)
  1.4282 -  assumes "bounded s" "bounded t"
  1.4283 -  shows "bounded { pastecart x y | x y . (x \<in> s \<and> y \<in> t)}"
  1.4284 -proof-
  1.4285 -  obtain a b where ab:"\<forall>x\<in>s. norm x \<le> a" "\<forall>x\<in>t. norm x \<le> b" using assms[unfolded bounded_iff] by auto
  1.4286 -  { fix x y assume "x\<in>s" "y\<in>t"
  1.4287 -    hence "norm x \<le> a" "norm y \<le> b" using ab by auto
  1.4288 -    hence "norm (pastecart x y) \<le> a + b" using norm_pastecart[of x y] by auto }
  1.4289 -  thus ?thesis unfolding bounded_iff by auto
  1.4290 -qed
  1.4291 -
  1.4292 -lemma bounded_Times:
  1.4293 -  assumes "bounded s" "bounded t" shows "bounded (s \<times> t)"
  1.4294 -proof-
  1.4295 -  obtain x y a b where "\<forall>z\<in>s. dist x z \<le> a" "\<forall>z\<in>t. dist y z \<le> b"
  1.4296 -    using assms [unfolded bounded_def] by auto
  1.4297 -  then have "\<forall>z\<in>s \<times> t. dist (x, y) z \<le> sqrt (a\<twosuperior> + b\<twosuperior>)"
  1.4298 -    by (auto simp add: dist_Pair_Pair real_sqrt_le_mono add_mono power_mono)
  1.4299 -  thus ?thesis unfolding bounded_any_center [where a="(x, y)"] by auto
  1.4300 -qed
  1.4301 -
  1.4302 -lemma closed_pastecart:
  1.4303 -  fixes s :: "(real ^ 'a::finite) set" (* FIXME: generalize *)
  1.4304 -  assumes "closed s"  "closed t"
  1.4305 -  shows "closed {pastecart x y | x y . x \<in> s \<and> y \<in> t}"
  1.4306 -proof-
  1.4307 -  { fix x l assume as:"\<forall>n::nat. x n \<in> {pastecart x y |x y. x \<in> s \<and> y \<in> t}"  "(x ---> l) sequentially"
  1.4308 -    { fix n::nat have "fstcart (x n) \<in> s" "sndcart (x n) \<in> t" using as(1)[THEN spec[where x=n]] by auto } note * = this
  1.4309 -    moreover
  1.4310 -    { fix e::real assume "e>0"
  1.4311 -      then obtain N::nat where N:"\<forall>n\<ge>N. dist (x n) l < e" using as(2)[unfolded Lim_sequentially, THEN spec[where x=e]] by auto
  1.4312 -      { fix n::nat assume "n\<ge>N"
  1.4313 -        hence "dist (fstcart (x n)) (fstcart l) < e" "dist (sndcart (x n)) (sndcart l) < e"
  1.4314 -          using N[THEN spec[where x=n]] dist_fstcart[of "x n" l] dist_sndcart[of "x n" l] by auto   }
  1.4315 -      hence "\<exists>N. \<forall>n\<ge>N. dist (fstcart (x n)) (fstcart l) < e" "\<exists>N. \<forall>n\<ge>N. dist (sndcart (x n)) (sndcart l) < e" by auto  }
  1.4316 -    ultimately have "fstcart l \<in> s" "sndcart l \<in> t"
  1.4317 -      using assms(1)[unfolded closed_sequential_limits, THEN spec[where x="\<lambda>n. fstcart (x n)"], THEN spec[where x="fstcart l"]]
  1.4318 -      using assms(2)[unfolded closed_sequential_limits, THEN spec[where x="\<lambda>n. sndcart (x n)"], THEN spec[where x="sndcart l"]]
  1.4319 -      unfolding Lim_sequentially by auto
  1.4320 -    hence "l \<in> {pastecart x y |x y. x \<in> s \<and> y \<in> t}" using pastecart_fst_snd[THEN sym, of l] by auto  }
  1.4321 -  thus ?thesis unfolding closed_sequential_limits by auto
  1.4322 -qed
  1.4323 -
  1.4324 -lemma compact_pastecart:
  1.4325 -  fixes s t :: "(real ^ _) set"
  1.4326 -  shows "compact s \<Longrightarrow> compact t ==> compact {pastecart x y | x y . x \<in> s \<and> y \<in> t}"
  1.4327 -  unfolding compact_eq_bounded_closed using bounded_pastecart[of s t] closed_pastecart[of s t] by auto
  1.4328 -
  1.4329 -lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"
  1.4330 -by (induct x) simp
  1.4331 -
  1.4332 -lemma compact_Times: "compact s \<Longrightarrow> compact t \<Longrightarrow> compact (s \<times> t)"
  1.4333 -unfolding compact_def
  1.4334 -apply clarify
  1.4335 -apply (drule_tac x="fst \<circ> f" in spec)
  1.4336 -apply (drule mp, simp add: mem_Times_iff)
  1.4337 -apply (clarify, rename_tac l1 r1)
  1.4338 -apply (drule_tac x="snd \<circ> f \<circ> r1" in spec)
  1.4339 -apply (drule mp, simp add: mem_Times_iff)
  1.4340 -apply (clarify, rename_tac l2 r2)
  1.4341 -apply (rule_tac x="(l1, l2)" in rev_bexI, simp)
  1.4342 -apply (rule_tac x="r1 \<circ> r2" in exI)
  1.4343 -apply (rule conjI, simp add: subseq_def)
  1.4344 -apply (drule_tac r=r2 in lim_subseq [COMP swap_prems_rl], assumption)
  1.4345 -apply (drule (1) tendsto_Pair) back
  1.4346 -apply (simp add: o_def)
  1.4347 -done
  1.4348 -
  1.4349 -text{* Hence some useful properties follow quite easily.                         *}
  1.4350 -
  1.4351 -lemma compact_scaling:
  1.4352 -  fixes s :: "'a::real_normed_vector set"
  1.4353 -  assumes "compact s"  shows "compact ((\<lambda>x. c *\<^sub>R x) ` s)"
  1.4354 -proof-
  1.4355 -  let ?f = "\<lambda>x. scaleR c x"
  1.4356 -  have *:"bounded_linear ?f" by (rule scaleR.bounded_linear_right)
  1.4357 -  show ?thesis using compact_continuous_image[of s ?f] continuous_at_imp_continuous_on[of s ?f]
  1.4358 -    using linear_continuous_at[OF *] assms by auto
  1.4359 -qed
  1.4360 -
  1.4361 -lemma compact_negations:
  1.4362 -  fixes s :: "'a::real_normed_vector set"
  1.4363 -  assumes "compact s"  shows "compact ((\<lambda>x. -x) ` s)"
  1.4364 -  using compact_scaling [OF assms, of "- 1"] by auto
  1.4365 -
  1.4366 -lemma compact_sums:
  1.4367 -  fixes s t :: "'a::real_normed_vector set"
  1.4368 -  assumes "compact s"  "compact t"  shows "compact {x + y | x y. x \<in> s \<and> y \<in> t}"
  1.4369 -proof-
  1.4370 -  have *:"{x + y | x y. x \<in> s \<and> y \<in> t} = (\<lambda>z. fst z + snd z) ` (s \<times> t)"
  1.4371 -    apply auto unfolding image_iff apply(rule_tac x="(xa, y)" in bexI) by auto
  1.4372 -  have "continuous_on (s \<times> t) (\<lambda>z. fst z + snd z)"
  1.4373 -    unfolding continuous_on by (rule ballI) (intro tendsto_intros)
  1.4374 -  thus ?thesis unfolding * using compact_continuous_image compact_Times [OF assms] by auto
  1.4375 -qed
  1.4376 -
  1.4377 -lemma compact_differences:
  1.4378 -  fixes s t :: "'a::real_normed_vector set"
  1.4379 -  assumes "compact s" "compact t"  shows "compact {x - y | x y. x \<in> s \<and> y \<in> t}"
  1.4380 -proof-
  1.4381 -  have "{x - y | x y. x\<in>s \<and> y \<in> t} =  {x + y | x y. x \<in> s \<and> y \<in> (uminus ` t)}"
  1.4382 -    apply auto apply(rule_tac x= xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
  1.4383 -  thus ?thesis using compact_sums[OF assms(1) compact_negations[OF assms(2)]] by auto
  1.4384 -qed
  1.4385 -
  1.4386 -lemma compact_translation:
  1.4387 -  fixes s :: "'a::real_normed_vector set"
  1.4388 -  assumes "compact s"  shows "compact ((\<lambda>x. a + x) ` s)"
  1.4389 -proof-
  1.4390 -  have "{x + y |x y. x \<in> s \<and> y \<in> {a}} = (\<lambda>x. a + x) ` s" by auto
  1.4391 -  thus ?thesis using compact_sums[OF assms compact_sing[of a]] by auto
  1.4392 -qed
  1.4393 -
  1.4394 -lemma compact_affinity:
  1.4395 -  fixes s :: "'a::real_normed_vector set"
  1.4396 -  assumes "compact s"  shows "compact ((\<lambda>x. a + c *\<^sub>R x) ` s)"
  1.4397 -proof-
  1.4398 -  have "op + a ` op *\<^sub>R c ` s = (\<lambda>x. a + c *\<^sub>R x) ` s" by auto
  1.4399 -  thus ?thesis using compact_translation[OF compact_scaling[OF assms], of a c] by auto
  1.4400 -qed
  1.4401 -
  1.4402 -text{* Hence we get the following.                                               *}
  1.4403 -
  1.4404 -lemma compact_sup_maxdistance:
  1.4405 -  fixes s :: "'a::real_normed_vector set"
  1.4406 -  assumes "compact s"  "s \<noteq> {}"
  1.4407 -  shows "\<exists>x\<in>s. \<exists>y\<in>s. \<forall>u\<in>s. \<forall>v\<in>s. norm(u - v) \<le> norm(x - y)"
  1.4408 -proof-
  1.4409 -  have "{x - y | x y . x\<in>s \<and> y\<in>s} \<noteq> {}" using `s \<noteq> {}` by auto
  1.4410 -  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"
  1.4411 -    using compact_differences[OF assms(1) assms(1)]
  1.4412 -    using distance_attains_sup[where 'a="'a", unfolded dist_norm, of "{x - y | x y . x\<in>s \<and> y\<in>s}" 0] by(auto simp add: norm_minus_cancel)
  1.4413 -  from x(1) obtain a b where "a\<in>s" "b\<in>s" "x = a - b" by auto
  1.4414 -  thus ?thesis using x(2)[unfolded `x = a - b`] by blast
  1.4415 -qed
  1.4416 -
  1.4417 -text{* We can state this in terms of diameter of a set.                          *}
  1.4418 -
  1.4419 -definition "diameter s = (if s = {} then 0::real else rsup {norm(x - y) | x y. x \<in> s \<and> y \<in> s})"
  1.4420 -  (* TODO: generalize to class metric_space *)
  1.4421 -
  1.4422 -lemma diameter_bounded:
  1.4423 -  assumes "bounded s"
  1.4424 -  shows "\<forall>x\<in>s. \<forall>y\<in>s. norm(x - y) \<le> diameter s"
  1.4425 -        "\<forall>d>0. d < diameter s --> (\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d)"
  1.4426 -proof-
  1.4427 -  let ?D = "{norm (x - y) |x y. x \<in> s \<and> y \<in> s}"
  1.4428 -  obtain a where a:"\<forall>x\<in>s. norm x \<le> a" using assms[unfolded bounded_iff] by auto
  1.4429 -  { fix x y assume "x \<in> s" "y \<in> s"
  1.4430 -    hence "norm (x - y) \<le> 2 * a" using norm_triangle_ineq[of x "-y", unfolded norm_minus_cancel] a[THEN bspec[where x=x]] a[THEN bspec[where x=y]] by (auto simp add: ring_simps)  }
  1.4431 -  note * = this
  1.4432 -  { fix x y assume "x\<in>s" "y\<in>s"  hence "s \<noteq> {}" by auto
  1.4433 -    have lub:"isLub UNIV ?D (rsup ?D)" using * rsup[of ?D] using `s\<noteq>{}` unfolding setle_def by auto
  1.4434 -    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` isLubD1[OF lub] unfolding setle_def by auto  }
  1.4435 -  moreover
  1.4436 -  { fix d::real assume "d>0" "d < diameter s"
  1.4437 -    hence "s\<noteq>{}" unfolding diameter_def by auto
  1.4438 -    hence lub:"isLub UNIV ?D (rsup ?D)" using * rsup[of ?D] unfolding setle_def by auto
  1.4439 -    have "\<exists>d' \<in> ?D. d' > d"
  1.4440 -    proof(rule ccontr)
  1.4441 -      assume "\<not> (\<exists>d'\<in>{norm (x - y) |x y. x \<in> s \<and> y \<in> s}. d < d')"
  1.4442 -      hence as:"\<forall>d'\<in>?D. d' \<le> d" apply auto apply(erule_tac x="norm (x - y)" in allE) by auto
  1.4443 -      hence "isUb UNIV ?D d" unfolding isUb_def unfolding setle_def by auto
  1.4444 -      thus False using `d < diameter s` `s\<noteq>{}` isLub_le_isUb[OF lub, of d] unfolding diameter_def  by auto
  1.4445 -    qed
  1.4446 -    hence "\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d" by auto  }
  1.4447 -  ultimately show "\<forall>x\<in>s. \<forall>y\<in>s. norm(x - y) \<le> diameter s"
  1.4448 -        "\<forall>d>0. d < diameter s --> (\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d)" by auto
  1.4449 -qed
  1.4450 -
  1.4451 -lemma diameter_bounded_bound:
  1.4452 - "bounded s \<Longrightarrow> x \<in> s \<Longrightarrow> y \<in> s ==> norm(x - y) \<le> diameter s"
  1.4453 -  using diameter_bounded by blast
  1.4454 -
  1.4455 -lemma diameter_compact_attained:
  1.4456 -  fixes s :: "'a::real_normed_vector set"
  1.4457 -  assumes "compact s"  "s \<noteq> {}"
  1.4458 -  shows "\<exists>x\<in>s. \<exists>y\<in>s. (norm(x - y) = diameter s)"
  1.4459 -proof-
  1.4460 -  have b:"bounded s" using assms(1) by (rule compact_imp_bounded)
  1.4461 -  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
  1.4462 -  hence "diameter s \<le> norm (x - y)" using rsup_le[of "{norm (x - y) |x y. x \<in> s \<and> y \<in> s}" "norm (x - y)"]
  1.4463 -    unfolding setle_def and diameter_def by auto
  1.4464 -  thus ?thesis using diameter_bounded(1)[OF b, THEN bspec[where x=x], THEN bspec[where x=y], OF xys] and xys by auto
  1.4465 -qed
  1.4466 -
  1.4467 -text{* Related results with closure as the conclusion.                           *}
  1.4468 -
  1.4469 -lemma closed_scaling:
  1.4470 -  fixes s :: "'a::real_normed_vector set"
  1.4471 -  assumes "closed s" shows "closed ((\<lambda>x. c *\<^sub>R x) ` s)"
  1.4472 -proof(cases "s={}")
  1.4473 -  case True thus ?thesis by auto
  1.4474 -next
  1.4475 -  case False
  1.4476 -  show ?thesis
  1.4477 -  proof(cases "c=0")
  1.4478 -    have *:"(\<lambda>x. 0) ` s = {0}" using `s\<noteq>{}` by auto
  1.4479 -    case True thus ?thesis apply auto unfolding * using closed_sing by auto
  1.4480 -  next
  1.4481 -    case False
  1.4482 -    { fix x l assume as:"\<forall>n::nat. x n \<in> scaleR c ` s"  "(x ---> l) sequentially"
  1.4483 -      { fix n::nat have "scaleR (1 / c) (x n) \<in> s"
  1.4484 -          using as(1)[THEN spec[where x=n]]
  1.4485 -          using `c\<noteq>0` by (auto simp add: vector_smult_assoc)
  1.4486 -      }
  1.4487 -      moreover
  1.4488 -      { fix e::real assume "e>0"
  1.4489 -        hence "0 < e *\<bar>c\<bar>"  using `c\<noteq>0` mult_pos_pos[of e "abs c"] by auto
  1.4490 -        then obtain N where "\<forall>n\<ge>N. dist (x n) l < e * \<bar>c\<bar>"
  1.4491 -          using as(2)[unfolded Lim_sequentially, THEN spec[where x="e * abs c"]] by auto
  1.4492 -        hence "\<exists>N. \<forall>n\<ge>N. dist (scaleR (1 / c) (x n)) (scaleR (1 / c) l) < e"
  1.4493 -          unfolding dist_norm unfolding scaleR_right_diff_distrib[THEN sym]
  1.4494 -          using mult_imp_div_pos_less[of "abs c" _ e] `c\<noteq>0` by auto  }
  1.4495 -      hence "((\<lambda>n. scaleR (1 / c) (x n)) ---> scaleR (1 / c) l) sequentially" unfolding Lim_sequentially by auto
  1.4496 -      ultimately have "l \<in> scaleR c ` s"
  1.4497 -        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"]]
  1.4498 -        unfolding image_iff using `c\<noteq>0` apply(rule_tac x="scaleR (1 / c) l" in bexI) by auto  }
  1.4499 -    thus ?thesis unfolding closed_sequential_limits by fast
  1.4500 -  qed
  1.4501 -qed
  1.4502 -
  1.4503 -lemma closed_negations:
  1.4504 -  fixes s :: "'a::real_normed_vector set"
  1.4505 -  assumes "closed s"  shows "closed ((\<lambda>x. -x) ` s)"
  1.4506 -  using closed_scaling[OF assms, of "- 1"] by simp
  1.4507 -
  1.4508 -lemma compact_closed_sums:
  1.4509 -  fixes s :: "'a::real_normed_vector set"
  1.4510 -  assumes "compact s"  "closed t"  shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"
  1.4511 -proof-
  1.4512 -  let ?S = "{x + y |x y. x \<in> s \<and> y \<in> t}"
  1.4513 -  { fix x l assume as:"\<forall>n. x n \<in> ?S"  "(x ---> l) sequentially"
  1.4514 -    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"
  1.4515 -      using choice[of "\<lambda>n y. x n = (fst y) + (snd y) \<and> fst y \<in> s \<and> snd y \<in> t"] by auto
  1.4516 -    obtain l' r where "l'\<in>s" and r:"subseq r" and lr:"(((\<lambda>n. fst (f n)) \<circ> r) ---> l') sequentially"
  1.4517 -      using assms(1)[unfolded compact_def, THEN spec[where x="\<lambda> n. fst (f n)"]] using f(2) by auto
  1.4518 -    have "((\<lambda>n. snd (f (r n))) ---> l - l') sequentially"
  1.4519 -      using Lim_sub[OF lim_subseq[OF r as(2)] lr] and f(1) unfolding o_def by auto
  1.4520 -    hence "l - l' \<in> t"
  1.4521 -      using assms(2)[unfolded closed_sequential_limits, THEN spec[where x="\<lambda> n. snd (f (r n))"], THEN spec[where x="l - l'"]]
  1.4522 -      using f(3) by auto
  1.4523 -    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
  1.4524 -  }
  1.4525 -  thus ?thesis unfolding closed_sequential_limits by fast
  1.4526 -qed
  1.4527 -
  1.4528 -lemma closed_compact_sums:
  1.4529 -  fixes s t :: "'a::real_normed_vector set"
  1.4530 -  assumes "closed s"  "compact t"
  1.4531 -  shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"
  1.4532 -proof-
  1.4533 -  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
  1.4534 -    apply(rule_tac x=y in exI) apply auto apply(rule_tac x=y in exI) by auto
  1.4535 -  thus ?thesis using compact_closed_sums[OF assms(2,1)] by simp
  1.4536 -qed
  1.4537 -
  1.4538 -lemma compact_closed_differences:
  1.4539 -  fixes s t :: "'a::real_normed_vector set"
  1.4540 -  assumes "compact s"  "closed t"
  1.4541 -  shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"
  1.4542 -proof-
  1.4543 -  have "{x + y |x y. x \<in> s \<and> y \<in> uminus ` t} =  {x - y |x y. x \<in> s \<and> y \<in> t}"
  1.4544 -    apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
  1.4545 -  thus ?thesis using compact_closed_sums[OF assms(1) closed_negations[OF assms(2)]] by auto
  1.4546 -qed
  1.4547 -
  1.4548 -lemma closed_compact_differences:
  1.4549 -  fixes s t :: "'a::real_normed_vector set"
  1.4550 -  assumes "closed s" "compact t"
  1.4551 -  shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"
  1.4552 -proof-
  1.4553 -  have "{x + y |x y. x \<in> s \<and> y \<in> uminus ` t} = {x - y |x y. x \<in> s \<and> y \<in> t}"
  1.4554 -    apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
  1.4555 - thus ?thesis using closed_compact_sums[OF assms(1) compact_negations[OF assms(2)]] by simp
  1.4556 -qed
  1.4557 -
  1.4558 -lemma closed_translation:
  1.4559 -  fixes a :: "'a::real_normed_vector"
  1.4560 -  assumes "closed s"  shows "closed ((\<lambda>x. a + x) ` s)"
  1.4561 -proof-
  1.4562 -  have "{a + y |y. y \<in> s} = (op + a ` s)" by auto
  1.4563 -  thus ?thesis using compact_closed_sums[OF compact_sing[of a] assms] by auto
  1.4564 -qed
  1.4565 -
  1.4566 -lemma translation_UNIV:
  1.4567 -  fixes a :: "'a::ab_group_add" shows "range (\<lambda>x. a + x) = UNIV"
  1.4568 -  apply (auto simp add: image_iff) apply(rule_tac x="x - a" in exI) by auto
  1.4569 -
  1.4570 -lemma translation_diff:
  1.4571 -  fixes a :: "'a::ab_group_add"
  1.4572 -  shows "(\<lambda>x. a + x) ` (s - t) = ((\<lambda>x. a + x) ` s) - ((\<lambda>x. a + x) ` t)"
  1.4573 -  by auto
  1.4574 -
  1.4575 -lemma closure_translation:
  1.4576 -  fixes a :: "'a::real_normed_vector"
  1.4577 -  shows "closure ((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (closure s)"
  1.4578 -proof-
  1.4579 -  have *:"op + a ` (UNIV - s) = UNIV - op + a ` s"
  1.4580 -    apply auto unfolding image_iff apply(rule_tac x="x - a" in bexI) by auto
  1.4581 -  show ?thesis unfolding closure_interior translation_diff translation_UNIV
  1.4582 -    using interior_translation[of a "UNIV - s"] unfolding * by auto
  1.4583 -qed
  1.4584 -
  1.4585 -lemma frontier_translation:
  1.4586 -  fixes a :: "'a::real_normed_vector"
  1.4587 -  shows "frontier((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (frontier s)"
  1.4588 -  unfolding frontier_def translation_diff interior_translation closure_translation by auto
  1.4589 -
  1.4590 -subsection{* Separation between points and sets.                                       *}
  1.4591 -
  1.4592 -lemma separate_point_closed:
  1.4593 -  fixes s :: "'a::heine_borel set"
  1.4594 -  shows "closed s \<Longrightarrow> a \<notin> s  ==> (\<exists>d>0. \<forall>x\<in>s. d \<le> dist a x)"
  1.4595 -proof(cases "s = {}")
  1.4596 -  case True
  1.4597 -  thus ?thesis by(auto intro!: exI[where x=1])
  1.4598 -next
  1.4599 -  case False
  1.4600 -  assume "closed s" "a \<notin> s"
  1.4601 -  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
  1.4602 -  with `x\<in>s` show ?thesis using dist_pos_lt[of a x] and`a \<notin> s` by blast
  1.4603 -qed
  1.4604 -
  1.4605 -lemma separate_compact_closed:
  1.4606 -  fixes s t :: "'a::{heine_borel, real_normed_vector} set"
  1.4607 -    (* TODO: does this generalize to heine_borel? *)
  1.4608 -  assumes "compact s" and "closed t" and "s \<inter> t = {}"
  1.4609 -  shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"
  1.4610 -proof-
  1.4611 -  have "0 \<notin> {x - y |x y. x \<in> s \<and> y \<in> t}" using assms(3) by auto
  1.4612 -  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"
  1.4613 -    using separate_point_closed[OF compact_closed_differences[OF assms(1,2)], of 0] by auto
  1.4614 -  { fix x y assume "x\<in>s" "y\<in>t"
  1.4615 -    hence "x - y \<in> {x - y |x y. x \<in> s \<and> y \<in> t}" by auto
  1.4616 -    hence "d \<le> dist (x - y) 0" using d[THEN bspec[where x="x - y"]] using dist_commute
  1.4617 -      by (auto  simp add: dist_commute)
  1.4618 -    hence "d \<le> dist x y" unfolding dist_norm by auto  }
  1.4619 -  thus ?thesis using `d>0` by auto
  1.4620 -qed
  1.4621 -
  1.4622 -lemma separate_closed_compact:
  1.4623 -  fixes s t :: "'a::{heine_borel, real_normed_vector} set"
  1.4624 -  assumes "closed s" and "compact t" and "s \<inter> t = {}"
  1.4625 -  shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"
  1.4626 -proof-
  1.4627 -  have *:"t \<inter> s = {}" using assms(3) by auto
  1.4628 -  show ?thesis using separate_compact_closed[OF assms(2,1) *]
  1.4629 -    apply auto apply(rule_tac x=d in exI) apply auto apply (erule_tac x=y in ballE)
  1.4630 -    by (auto simp add: dist_commute)
  1.4631 -qed
  1.4632 -
  1.4633 -(* A cute way of denoting open and closed intervals using overloading.       *)
  1.4634 -
  1.4635 -lemma interval: fixes a :: "'a::ord^'n::finite" shows
  1.4636 -  "{a <..< b} = {x::'a^'n. \<forall>i. a$i < x$i \<and> x$i < b$i}" and
  1.4637 -  "{a .. b} = {x::'a^'n. \<forall>i. a$i \<le> x$i \<and> x$i \<le> b$i}"
  1.4638 -  by (auto simp add: expand_set_eq vector_less_def vector_less_eq_def)
  1.4639 -
  1.4640 -lemma mem_interval: fixes a :: "'a::ord^'n::finite" shows
  1.4641 -  "x \<in> {a<..<b} \<longleftrightarrow> (\<forall>i. a$i < x$i \<and> x$i < b$i)"
  1.4642 -  "x \<in> {a .. b} \<longleftrightarrow> (\<forall>i. a$i \<le> x$i \<and> x$i \<le> b$i)"
  1.4643 -  using interval[of a b] by(auto simp add: expand_set_eq vector_less_def vector_less_eq_def)
  1.4644 -
  1.4645 -lemma mem_interval_1: fixes x :: "real^1" shows
  1.4646 - "(x \<in> {a .. b} \<longleftrightarrow> dest_vec1 a \<le> dest_vec1 x \<and> dest_vec1 x \<le> dest_vec1 b)"
  1.4647 - "(x \<in> {a<..<b} \<longleftrightarrow> dest_vec1 a < dest_vec1 x \<and> dest_vec1 x < dest_vec1 b)"
  1.4648 -by(simp_all add: Cart_eq vector_less_def vector_less_eq_def dest_vec1_def forall_1)
  1.4649 -
  1.4650 -lemma interval_eq_empty: fixes a :: "real^'n::finite" shows
  1.4651 - "({a <..< b} = {} \<longleftrightarrow> (\<exists>i. b$i \<le> a$i))" (is ?th1) and
  1.4652 - "({a  ..  b} = {} \<longleftrightarrow> (\<exists>i. b$i < a$i))" (is ?th2)
  1.4653 -proof-
  1.4654 -  { fix i x assume as:"b$i \<le> a$i" and x:"x\<in>{a <..< b}"
  1.4655 -    hence "a $ i < x $ i \<and> x $ i < b $ i" unfolding mem_interval by auto
  1.4656 -    hence "a$i < b$i" by auto
  1.4657 -    hence False using as by auto  }
  1.4658 -  moreover
  1.4659 -  { assume as:"\<forall>i. \<not> (b$i \<le> a$i)"
  1.4660 -    let ?x = "(1/2) *\<^sub>R (a + b)"
  1.4661 -    { fix i
  1.4662 -      have "a$i < b$i" using as[THEN spec[where x=i]] by auto
  1.4663 -      hence "a$i < ((1/2) *\<^sub>R (a+b)) $ i" "((1/2) *\<^sub>R (a+b)) $ i < b$i"
  1.4664 -        unfolding vector_smult_component and vector_add_component
  1.4665 -        by (auto simp add: less_divide_eq_number_of1)  }
  1.4666 -    hence "{a <..< b} \<noteq> {}" using mem_interval(1)[of "?x" a b] by auto  }
  1.4667 -  ultimately show ?th1 by blast
  1.4668 -
  1.4669 -  { fix i x assume as:"b$i < a$i" and x:"x\<in>{a .. b}"
  1.4670 -    hence "a $ i \<le> x $ i \<and> x $ i \<le> b $ i" unfolding mem_interval by auto
  1.4671 -    hence "a$i \<le> b$i" by auto
  1.4672 -    hence False using as by auto  }
  1.4673 -  moreover
  1.4674 -  { assume as:"\<forall>i. \<not> (b$i < a$i)"
  1.4675 -    let ?x = "(1/2) *\<^sub>R (a + b)"
  1.4676 -    { fix i
  1.4677 -      have "a$i \<le> b$i" using as[THEN spec[where x=i]] by auto
  1.4678 -      hence "a$i \<le> ((1/2) *\<^sub>R (a+b)) $ i" "((1/2) *\<^sub>R (a+b)) $ i \<le> b$i"
  1.4679 -        unfolding vector_smult_component and vector_add_component
  1.4680 -        by (auto simp add: less_divide_eq_number_of1)  }
  1.4681 -    hence "{a .. b} \<noteq> {}" using mem_interval(2)[of "?x" a b] by auto  }
  1.4682 -  ultimately show ?th2 by blast
  1.4683 -qed
  1.4684 -
  1.4685 -lemma interval_ne_empty: fixes a :: "real^'n::finite" shows
  1.4686 -  "{a  ..  b} \<noteq> {} \<longleftrightarrow> (\<forall>i. a$i \<le> b$i)" and
  1.4687 -  "{a <..< b} \<noteq> {} \<longleftrightarrow> (\<forall>i. a$i < b$i)"
  1.4688 -  unfolding interval_eq_empty[of a b] by (auto simp add: not_less not_le) (* BH: Why doesn't just "auto" work here? *)
  1.4689 -
  1.4690 -lemma subset_interval_imp: fixes a :: "real^'n::finite" shows
  1.4691 - "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> {c .. d} \<subseteq> {a .. b}" and
  1.4692 - "(\<forall>i. a$i < c$i \<and> d$i < b$i) \<Longrightarrow> {c .. d} \<subseteq> {a<..<b}" and
  1.4693 - "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> {c<..<d} \<subseteq> {a .. b}" and
  1.4694 - "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> {c<..<d} \<subseteq> {a<..<b}"
  1.4695 -  unfolding subset_eq[unfolded Ball_def] unfolding mem_interval
  1.4696 -  by (auto intro: order_trans less_le_trans le_less_trans less_imp_le) (* BH: Why doesn't just "auto" work here? *)
  1.4697 -
  1.4698 -lemma interval_sing: fixes a :: "'a::linorder^'n::finite" shows
  1.4699 - "{a .. a} = {a} \<and> {a<..<a} = {}"
  1.4700 -apply(auto simp add: expand_set_eq vector_less_def vector_less_eq_def Cart_eq)
  1.4701 -apply (simp add: order_eq_iff)
  1.4702 -apply (auto simp add: not_less less_imp_le)
  1.4703 -done
  1.4704 -
  1.4705 -lemma interval_open_subset_closed:  fixes a :: "'a::preorder^'n::finite" shows
  1.4706 - "{a<..<b} \<subseteq> {a .. b}"
  1.4707 -proof(simp add: subset_eq, rule)
  1.4708 -  fix x
  1.4709 -  assume x:"x \<in>{a<..<b}"
  1.4710 -  { fix i
  1.4711 -    have "a $ i \<le> x $ i"
  1.4712 -      using x order_less_imp_le[of "a$i" "x$i"]
  1.4713 -      by(simp add: expand_set_eq vector_less_def vector_less_eq_def Cart_eq)
  1.4714 -  }
  1.4715 -  moreover
  1.4716 -  { fix i
  1.4717 -    have "x $ i \<le> b $ i"
  1.4718 -      using x order_less_imp_le[of "x$i" "b$i"]
  1.4719 -      by(simp add: expand_set_eq vector_less_def vector_less_eq_def Cart_eq)
  1.4720 -  }
  1.4721 -  ultimately
  1.4722 -  show "a \<le> x \<and> x \<le> b"
  1.4723 -    by(simp add: expand_set_eq vector_less_def vector_less_eq_def Cart_eq)
  1.4724 -qed
  1.4725 -
  1.4726 -lemma subset_interval: fixes a :: "real^'n::finite" shows
  1.4727 - "{c .. d} \<subseteq> {a .. b} \<longleftrightarrow> (\<forall>i. c$i \<le> d$i) --> (\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i)" (is ?th1) and
  1.4728 - "{c .. d} \<subseteq> {a<..<b} \<longleftrightarrow> (\<forall>i. c$i \<le> d$i) --> (\<forall>i. a$i < c$i \<and> d$i < b$i)" (is ?th2) and
  1.4729 - "{c<..<d} \<subseteq> {a .. b} \<longleftrightarrow> (\<forall>i. c$i < d$i) --> (\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i)" (is ?th3) and
  1.4730 - "{c<..<d} \<subseteq> {a<..<b} \<longleftrightarrow> (\<forall>i. c$i < d$i) --> (\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i)" (is ?th4)
  1.4731 -proof-
  1.4732 -  show ?th1 unfolding subset_eq and Ball_def and mem_interval by (auto intro: order_trans)
  1.4733 -  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)
  1.4734 -  { assume as: "{c<..<d} \<subseteq> {a .. b}" "\<forall>i. c$i < d$i"
  1.4735 -    hence "{c<..<d} \<noteq> {}" unfolding interval_eq_empty by (auto, drule_tac x=i in spec, simp) (* BH: Why doesn't just "auto" work? *)
  1.4736 -    fix i
  1.4737 -    (** TODO combine the following two parts as done in the HOL_light version. **)
  1.4738 -    { let ?x = "(\<chi> j. (if j=i then ((min (a$j) (d$j))+c$j)/2 else (c$j+d$j)/2))::real^'n"
  1.4739 -      assume as2: "a$i > c$i"
  1.4740 -      { fix j
  1.4741 -        have "c $ j < ?x $ j \<and> ?x $ j < d $ j" unfolding Cart_lambda_beta
  1.4742 -          apply(cases "j=i") using as(2)[THEN spec[where x=j]]
  1.4743 -          by (auto simp add: less_divide_eq_number_of1 as2)  }
  1.4744 -      hence "?x\<in>{c<..<d}" unfolding mem_interval by auto
  1.4745 -      moreover
  1.4746 -      have "?x\<notin>{a .. b}"
  1.4747 -        unfolding mem_interval apply auto apply(rule_tac x=i in exI)
  1.4748 -        using as(2)[THEN spec[where x=i]] and as2
  1.4749 -        by (auto simp add: less_divide_eq_number_of1)
  1.4750 -      ultimately have False using as by auto  }
  1.4751 -    hence "a$i \<le> c$i" by(rule ccontr)auto
  1.4752 -    moreover
  1.4753 -    { let ?x = "(\<chi> j. (if j=i then ((max (b$j) (c$j))+d$j)/2 else (c$j+d$j)/2))::real^'n"
  1.4754 -      assume as2: "b$i < d$i"
  1.4755 -      { fix j
  1.4756 -        have "d $ j > ?x $ j \<and> ?x $ j > c $ j" unfolding Cart_lambda_beta
  1.4757 -          apply(cases "j=i") using as(2)[THEN spec[where x=j]]
  1.4758 -          by (auto simp add: less_divide_eq_number_of1 as2)  }
  1.4759 -      hence "?x\<in>{c<..<d}" unfolding mem_interval by auto
  1.4760 -      moreover
  1.4761 -      have "?x\<notin>{a .. b}"
  1.4762 -        unfolding mem_interval apply auto apply(rule_tac x=i in exI)
  1.4763 -        using as(2)[THEN spec[where x=i]] and as2
  1.4764 -        by (auto simp add: less_divide_eq_number_of1)
  1.4765 -      ultimately have False using as by auto  }
  1.4766 -    hence "b$i \<ge> d$i" by(rule ccontr)auto
  1.4767 -    ultimately
  1.4768 -    have "a$i \<le> c$i \<and> d$i \<le> b$i" by auto
  1.4769 -  } note part1 = this
  1.4770 -  thus ?th3 unfolding subset_eq and Ball_def and mem_interval apply auto apply (erule_tac x=ia in allE, simp)+ by (erule_tac x=i in allE, erule_tac x=i in allE, simp)+
  1.4771 -  { assume as:"{c<..<d} \<subseteq> {a<..<b}" "\<forall>i. c$i < d$i"
  1.4772 -    fix i
  1.4773 -    from as(1) have "{c<..<d} \<subseteq> {a..b}" using interval_open_subset_closed[of a b] by auto
  1.4774 -    hence "a$i \<le> c$i \<and> d$i \<le> b$i" using part1 and as(2) by auto  } note * = this
  1.4775 -  thus ?th4 unfolding subset_eq and Ball_def and mem_interval apply auto apply (erule_tac x=ia in allE, simp)+ by (erule_tac x=i in allE, erule_tac x=i in allE, simp)+
  1.4776 -qed
  1.4777 -
  1.4778 -lemma disjoint_interval: fixes a::"real^'n::finite" shows
  1.4779 -  "{a .. b} \<inter> {c .. d} = {} \<longleftrightarrow> (\<exists>i. (b$i < a$i \<or> d$i < c$i \<or> b$i < c$i \<or> d$i < a$i))" (is ?th1) and
  1.4780 -  "{a .. b} \<inter> {c<..<d} = {} \<longleftrightarrow> (\<exists>i. (b$i < a$i \<or> d$i \<le> c$i \<or> b$i \<le> c$i \<or> d$i \<le> a$i))" (is ?th2) and
  1.4781 -  "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> (\<exists>i. (b$i \<le> a$i \<or> d$i < c$i \<or> b$i \<le> c$i \<or> d$i \<le> a$i))" (is ?th3) and
  1.4782 -  "{a<..<b} \<inter> {c<..<d} = {} \<longleftrightarrow> (\<exists>i. (b$i \<le> a$i \<or> d$i \<le> c$i \<or> b$i \<le> c$i \<or> d$i \<le> a$i))" (is ?th4)
  1.4783 -proof-
  1.4784 -  let ?z = "(\<chi> i. ((max (a$i) (c$i)) + (min (b$i) (d$i))) / 2)::real^'n"
  1.4785 -  show ?th1 ?th2 ?th3 ?th4
  1.4786 -  unfolding expand_set_eq and Int_iff and empty_iff and mem_interval and all_conj_distrib[THEN sym] and eq_False
  1.4787 -  apply (auto elim!: allE[where x="?z"])
  1.4788 -  apply ((rule_tac x=x in exI, force) | (rule_tac x=i in exI, force))+
  1.4789 -  done
  1.4790 -qed
  1.4791 -
  1.4792 -lemma inter_interval: fixes a :: "'a::linorder^'n::finite" shows
  1.4793 - "{a .. b} \<inter> {c .. d} =  {(\<chi> i. max (a$i) (c$i)) .. (\<chi> i. min (b$i) (d$i))}"
  1.4794 -  unfolding expand_set_eq and Int_iff and mem_interval
  1.4795 -  by (auto simp add: less_divide_eq_number_of1 intro!: bexI)
  1.4796 -
  1.4797 -(* Moved interval_open_subset_closed a bit upwards *)
  1.4798 -
  1.4799 -lemma open_interval_lemma: fixes x :: "real" shows
  1.4800 - "a < x \<Longrightarrow> x < b ==> (\<exists>d>0. \<forall>x'. abs(x' - x) < d --> a < x' \<and> x' < b)"
  1.4801 -  by(rule_tac x="min (x - a) (b - x)" in exI, auto)
  1.4802 -
  1.4803 -lemma open_interval: fixes a :: "real^'n::finite" shows "open {a<..<b}"
  1.4804 -proof-
  1.4805 -  { fix x assume x:"x\<in>{a<..<b}"
  1.4806 -    { fix i
  1.4807 -      have "\<exists>d>0. \<forall>x'. abs (x' - (x$i)) < d \<longrightarrow> a$i < x' \<and> x' < b$i"
  1.4808 -        using x[unfolded mem_interval, THEN spec[where x=i]]
  1.4809 -        using open_interval_lemma[of "a$i" "x$i" "b$i"] by auto  }
  1.4810 -
  1.4811 -    hence "\<forall>i. \<exists>d>0. \<forall>x'. abs (x' - (x$i)) < d \<longrightarrow> a$i < x' \<and> x' < b$i" by auto
  1.4812 -    then obtain d where d:"\<forall>i. 0 < d i \<and> (\<forall>x'. \<bar>x' - x $ i\<bar> < d i \<longrightarrow> a $ i < x' \<and> x' < b $ i)"
  1.4813 -      using bchoice[of "UNIV" "\<lambda>i d. d>0 \<and> (\<forall>x'. \<bar>x' - x $ i\<bar> < d \<longrightarrow> a $ i < x' \<and> x' < b $ i)"] by auto
  1.4814 -
  1.4815 -    let ?d = "Min (range d)"
  1.4816 -    have **:"finite (range d)" "range d \<noteq> {}" by auto
  1.4817 -    have "?d>0" unfolding Min_gr_iff[OF **] using d by auto
  1.4818 -    moreover
  1.4819 -    { fix x' assume as:"dist x' x < ?d"
  1.4820 -      { fix i
  1.4821 -        have "\<bar>x'$i - x $ i\<bar> < d i"
  1.4822 -          using norm_bound_component_lt[OF as[unfolded dist_norm], of i]
  1.4823 -          unfolding vector_minus_component and Min_gr_iff[OF **] by auto
  1.4824 -        hence "a $ i < x' $ i" "x' $ i < b $ i" using d[THEN spec[where x=i]] by auto  }
  1.4825 -      hence "a < x' \<and> x' < b" unfolding vector_less_def by auto  }
  1.4826 -    ultimately have "\<exists>e>0. \<forall>x'. dist x' x < e \<longrightarrow> x' \<in> {a<..<b}" by (auto, rule_tac x="?d" in exI, simp)
  1.4827 -  }
  1.4828 -  thus ?thesis unfolding open_dist using open_interval_lemma by auto
  1.4829 -qed
  1.4830 -
  1.4831 -lemma closed_interval: fixes a :: "real^'n::finite" shows "closed {a .. b}"
  1.4832 -proof-
  1.4833 -  { fix x i assume as:"\<forall>e>0. \<exists>x'\<in>{a..b}. x' \<noteq> x \<and> dist x' x < e"(* and xab:"a$i > x$i \<or> b$i < x$i"*)
  1.4834 -    { assume xa:"a$i > x$i"
  1.4835 -      with as obtain y where y:"y\<in>{a..b}" "y \<noteq> x" "dist y x < a$i - x$i" by(erule_tac x="a$i - x$i" in allE)auto
  1.4836 -      hence False unfolding mem_interval and dist_norm
  1.4837 -        using component_le_norm[of "y-x" i, unfolded vector_minus_component] and xa by(auto elim!: allE[where x=i])
  1.4838 -    } hence "a$i \<le> x$i" by(rule ccontr)auto
  1.4839 -    moreover
  1.4840 -    { assume xb:"b$i < x$i"
  1.4841 -      with as obtain y where y:"y\<in>{a..b}" "y \<noteq> x" "dist y x < x$i - b$i" by(erule_tac x="x$i - b$i" in allE)auto
  1.4842 -      hence False unfolding mem_interval and dist_norm
  1.4843 -        using component_le_norm[of "y-x" i, unfolded vector_minus_component] and xb by(auto elim!: allE[where x=i])
  1.4844 -    } hence "x$i \<le> b$i" by(rule ccontr)auto
  1.4845 -    ultimately
  1.4846 -    have "a $ i \<le> x $ i \<and> x $ i \<le> b $ i" by auto }
  1.4847 -  thus ?thesis unfolding closed_limpt islimpt_approachable mem_interval by auto
  1.4848 -qed
  1.4849 -
  1.4850 -lemma interior_closed_interval: fixes a :: "real^'n::finite" shows
  1.4851 - "interior {a .. b} = {a<..<b}" (is "?L = ?R")
  1.4852 -proof(rule subset_antisym)
  1.4853 -  show "?R \<subseteq> ?L" using interior_maximal[OF interval_open_subset_closed open_interval] by auto
  1.4854 -next
  1.4855 -  { fix x assume "\<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> {a..b}"
  1.4856 -    then obtain s where s:"open s" "x \<in> s" "s \<subseteq> {a..b}" by auto
  1.4857 -    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
  1.4858 -    { fix i
  1.4859 -      have "dist (x - (e / 2) *\<^sub>R basis i) x < e"
  1.4860 -           "dist (x + (e / 2) *\<^sub>R basis i) x < e"
  1.4861 -        unfolding dist_norm apply auto
  1.4862 -        unfolding norm_minus_cancel using norm_basis[of i] and `e>0` by auto
  1.4863 -      hence "a $ i \<le> (x - (e / 2) *\<^sub>R basis i) $ i"
  1.4864 -                    "(x + (e / 2) *\<^sub>R basis i) $ i \<le> b $ i"
  1.4865 -        using e[THEN spec[where x="x - (e/2) *\<^sub>R basis i"]]
  1.4866 -        and   e[THEN spec[where x="x + (e/2) *\<^sub>R basis i"]]
  1.4867 -        unfolding mem_interval by (auto elim!: allE[where x=i])
  1.4868 -      hence "a $ i < x $ i" and "x $ i < b $ i"
  1.4869 -        unfolding vector_minus_component and vector_add_component
  1.4870 -        unfolding vector_smult_component and basis_component using `e>0` by auto   }
  1.4871 -    hence "x \<in> {a<..<b}" unfolding mem_interval by auto  }
  1.4872 -  thus "?L \<subseteq> ?R" unfolding interior_def and subset_eq by auto
  1.4873 -qed
  1.4874 -
  1.4875 -lemma bounded_closed_interval: fixes a :: "real^'n::finite" shows
  1.4876 - "bounded {a .. b}"
  1.4877 -proof-
  1.4878 -  let ?b = "\<Sum>i\<in>UNIV. \<bar>a$i\<bar> + \<bar>b$i\<bar>"
  1.4879 -  { fix x::"real^'n" assume x:"\<forall>i. a $ i \<le> x $ i \<and> x $ i \<le> b $ i"
  1.4880 -    { fix i
  1.4881 -      have "\<bar>x$i\<bar> \<le> \<bar>a$i\<bar> + \<bar>b$i\<bar>" using x[THEN spec[where x=i]] by auto  }
  1.4882 -    hence "(\<Sum>i\<in>UNIV. \<bar>x $ i\<bar>) \<le> ?b" by(rule setsum_mono)
  1.4883 -    hence "norm x \<le> ?b" using norm_le_l1[of x] by auto  }
  1.4884 -  thus ?thesis unfolding interval and bounded_iff by auto
  1.4885 -qed
  1.4886 -
  1.4887 -lemma bounded_interval: fixes a :: "real^'n::finite" shows
  1.4888 - "bounded {a .. b} \<and> bounded {a<..<b}"
  1.4889 -  using bounded_closed_interval[of a b]
  1.4890 -  using interval_open_subset_closed[of a b]
  1.4891 -  using bounded_subset[of "{a..b}" "{a<..<b}"]
  1.4892 -  by simp
  1.4893 -
  1.4894 -lemma not_interval_univ: fixes a :: "real^'n::finite" shows
  1.4895 - "({a .. b} \<noteq> UNIV) \<and> ({a<..<b} \<noteq> UNIV)"
  1.4896 -  using bounded_interval[of a b]
  1.4897 -  by auto
  1.4898 -
  1.4899 -lemma compact_interval: fixes a :: "real^'n::finite" shows
  1.4900 - "compact {a .. b}"
  1.4901 -  using bounded_closed_imp_compact using bounded_interval[of a b] using closed_interval[of a b] by auto
  1.4902 -
  1.4903 -lemma open_interval_midpoint: fixes a :: "real^'n::finite"
  1.4904 -  assumes "{a<..<b} \<noteq> {}" shows "((1/2) *\<^sub>R (a + b)) \<in> {a<..<b}"
  1.4905 -proof-
  1.4906 -  { fix i
  1.4907 -    have "a $ i < ((1 / 2) *\<^sub>R (a + b)) $ i \<and> ((1 / 2) *\<^sub>R (a + b)) $ i < b $ i"
  1.4908 -      using assms[unfolded interval_ne_empty, THEN spec[where x=i]]
  1.4909 -      unfolding vector_smult_component and vector_add_component
  1.4910 -      by(auto simp add: less_divide_eq_number_of1)  }
  1.4911 -  thus ?thesis unfolding mem_interval by auto
  1.4912 -qed
  1.4913 -
  1.4914 -lemma open_closed_interval_convex: fixes x :: "real^'n::finite"
  1.4915 -  assumes x:"x \<in> {a<..<b}" and y:"y \<in> {a .. b}" and e:"0 < e" "e \<le> 1"
  1.4916 -  shows "(e *\<^sub>R x + (1 - e) *\<^sub>R y) \<in> {a<..<b}"
  1.4917 -proof-
  1.4918 -  { fix i
  1.4919 -    have "a $ i = e * a$i + (1 - e) * a$i" unfolding left_diff_distrib by simp
  1.4920 -    also have "\<dots> < e * x $ i + (1 - e) * y $ i" apply(rule add_less_le_mono)
  1.4921 -      using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all
  1.4922 -      using x unfolding mem_interval  apply simp
  1.4923 -      using y unfolding mem_interval  apply simp
  1.4924 -      done
  1.4925 -    finally have "a $ i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) $ i" by auto
  1.4926 -    moreover {
  1.4927 -    have "b $ i = e * b$i + (1 - e) * b$i" unfolding left_diff_distrib by simp
  1.4928 -    also have "\<dots> > e * x $ i + (1 - e) * y $ i" apply(rule add_less_le_mono)
  1.4929 -      using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all
  1.4930 -      using x unfolding mem_interval  apply simp
  1.4931 -      using y unfolding mem_interval  apply simp
  1.4932 -      done
  1.4933 -    finally have "(e *\<^sub>R x + (1 - e) *\<^sub>R y) $ i < b $ i" by auto
  1.4934 -    } 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 }
  1.4935 -  thus ?thesis unfolding mem_interval by auto
  1.4936 -qed
  1.4937 -
  1.4938 -lemma closure_open_interval: fixes a :: "real^'n::finite"
  1.4939 -  assumes "{a<..<b} \<noteq> {}"
  1.4940 -  shows "closure {a<..<b} = {a .. b}"
  1.4941 -proof-
  1.4942 -  have ab:"a < b" using assms[unfolded interval_ne_empty] unfolding vector_less_def by auto
  1.4943 -  let ?c = "(1 / 2) *\<^sub>R (a + b)"
  1.4944 -  { fix x assume as:"x \<in> {a .. b}"
  1.4945 -    def f == "\<lambda>n::nat. x + (inverse (real n + 1)) *\<^sub>R (?c - x)"
  1.4946 -    { fix n assume fn:"f n < b \<longrightarrow> a < f n \<longrightarrow> f n = x" and xc:"x \<noteq> ?c"
  1.4947 -      have *:"0 < inverse (real n + 1)" "inverse (real n + 1) \<le> 1" unfolding inverse_le_1_iff by auto
  1.4948 -      have "(inverse (real n + 1)) *\<^sub>R ((1 / 2) *\<^sub>R (a + b)) + (1 - inverse (real n + 1)) *\<^sub>R x =
  1.4949 -        x + (inverse (real n + 1)) *\<^sub>R (((1 / 2) *\<^sub>R (a + b)) - x)"
  1.4950 -        by (auto simp add: algebra_simps)
  1.4951 -      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
  1.4952 -      hence False using fn unfolding f_def using xc by(auto simp add: vector_mul_lcancel vector_ssub_ldistrib)  }
  1.4953 -    moreover
  1.4954 -    { assume "\<not> (f ---> x) sequentially"
  1.4955 -      { fix e::real assume "e>0"
  1.4956 -        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
  1.4957 -        then obtain N::nat where "inverse (real (N + 1)) < e" by auto
  1.4958 -        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)
  1.4959 -        hence "\<exists>N::nat. \<forall>n\<ge>N. inverse (real n + 1) < e" by auto  }
  1.4960 -      hence "((\<lambda>n. inverse (real n + 1)) ---> 0) sequentially"
  1.4961 -        unfolding Lim_sequentially by(auto simp add: dist_norm)
  1.4962 -      hence "(f ---> x) sequentially" unfolding f_def
  1.4963 -        using Lim_add[OF Lim_const, of "\<lambda>n::nat. (inverse (real n + 1)) *\<^sub>R ((1 / 2) *\<^sub>R (a + b) - x)" 0 sequentially x]
  1.4964 -        using Lim_vmul[of "\<lambda>n::nat. inverse (real n + 1)" 0 sequentially "((1 / 2) *\<^sub>R (a + b) - x)"] by auto  }
  1.4965 -    ultimately have "x \<in> closure {a<..<b}"
  1.4966 -      using as and open_interval_midpoint[OF assms] unfolding closure_def unfolding islimpt_sequential by(cases "x=?c")auto  }
  1.4967 -  thus ?thesis using closure_minimal[OF interval_open_subset_closed closed_interval, of a b] by blast
  1.4968 -qed
  1.4969 -
  1.4970 -lemma bounded_subset_open_interval_symmetric: fixes s::"(real^'n::finite) set"
  1.4971 -  assumes "bounded s"  shows "\<exists>a. s \<subseteq> {-a<..<a}"
  1.4972 -proof-
  1.4973 -  obtain b where "b>0" and b:"\<forall>x\<in>s. norm x \<le> b" using assms[unfolded bounded_pos] by auto
  1.4974 -  def a \<equiv> "(\<chi> i. b+1)::real^'n"
  1.4975 -  { fix x assume "x\<in>s"
  1.4976 -    fix i
  1.4977 -    have "(-a)$i < x$i" and "x$i < a$i" using b[THEN bspec[where x=x], OF `x\<in>s`] and component_le_norm[of x i]
  1.4978 -      unfolding vector_uminus_component and a_def and Cart_lambda_beta by auto
  1.4979 -  }
  1.4980 -  thus ?thesis by(auto intro: exI[where x=a] simp add: vector_less_def)
  1.4981 -qed
  1.4982 -
  1.4983 -lemma bounded_subset_open_interval:
  1.4984 -  fixes s :: "(real ^ 'n::finite) set"
  1.4985 -  shows "bounded s ==> (\<exists>a b. s \<subseteq> {a<..<b})"
  1.4986 -  by (auto dest!: bounded_subset_open_interval_symmetric)
  1.4987 -
  1.4988 -lemma bounded_subset_closed_interval_symmetric:
  1.4989 -  fixes s :: "(real ^ 'n::finite) set"
  1.4990 -  assumes "bounded s" shows "\<exists>a. s \<subseteq> {-a .. a}"
  1.4991 -proof-
  1.4992 -  obtain a where "s \<subseteq> {- a<..<a}" using bounded_subset_open_interval_symmetric[OF assms] by auto
  1.4993 -  thus ?thesis using interval_open_subset_closed[of "-a" a] by auto
  1.4994 -qed
  1.4995 -
  1.4996 -lemma bounded_subset_closed_interval:
  1.4997 -  fixes s :: "(real ^ 'n::finite) set"
  1.4998 -  shows "bounded s ==> (\<exists>a b. s \<subseteq> {a .. b})"
  1.4999 -  using bounded_subset_closed_interval_symmetric[of s] by auto
  1.5000 -
  1.5001 -lemma frontier_closed_interval:
  1.5002 -  fixes a b :: "real ^ _"
  1.5003 -  shows "frontier {a .. b} = {a .. b} - {a<..<b}"
  1.5004 -  unfolding frontier_def unfolding interior_closed_interval and closure_closed[OF closed_interval] ..
  1.5005 -
  1.5006 -lemma frontier_open_interval:
  1.5007 -  fixes a b :: "real ^ _"
  1.5008 -  shows "frontier {a<..<b} = (if {a<..<b} = {} then {} else {a .. b} - {a<..<b})"
  1.5009 -proof(cases "{a<..<b} = {}")
  1.5010 -  case True thus ?thesis using frontier_empty by auto
  1.5011 -next
  1.5012 -  case False thus ?thesis unfolding frontier_def and closure_open_interval[OF False] and interior_open[OF open_interval] by auto
  1.5013 -qed
  1.5014 -
  1.5015 -lemma inter_interval_mixed_eq_empty: fixes a :: "real^'n::finite"
  1.5016 -  assumes "{c<..<d} \<noteq> {}"  shows "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> {a<..<b} \<inter> {c<..<d} = {}"
  1.5017 -  unfolding closure_open_interval[OF assms, THEN sym] unfolding open_inter_closure_eq_empty[OF open_interval] ..
  1.5018 -
  1.5019 -
  1.5020 -(* Some special cases for intervals in R^1.                                  *)
  1.5021 -
  1.5022 -lemma all_1: "(\<forall>x::1. P x) \<longleftrightarrow> P 1"
  1.5023 -  by (metis num1_eq_iff)
  1.5024 -
  1.5025 -lemma ex_1: "(\<exists>x::1. P x) \<longleftrightarrow> P 1"
  1.5026 -  by auto (metis num1_eq_iff)
  1.5027 -
  1.5028 -lemma interval_cases_1: fixes x :: "real^1" shows
  1.5029 - "x \<in> {a .. b} ==> x \<in> {a<..<b} \<or> (x = a) \<or> (x = b)"
  1.5030 -  by(simp add:  Cart_eq vector_less_def vector_less_eq_def all_1, auto)
  1.5031 -
  1.5032 -lemma in_interval_1: fixes x :: "real^1" shows
  1.5033 - "(x \<in> {a .. b} \<longleftrightarrow> dest_vec1 a \<le> dest_vec1 x \<and> dest_vec1 x \<le> dest_vec1 b) \<and>
  1.5034 -  (x \<in> {a<..<b} \<longleftrightarrow> dest_vec1 a < dest_vec1 x \<and> dest_vec1 x < dest_vec1 b)"
  1.5035 -by(simp add: Cart_eq vector_less_def vector_less_eq_def all_1 dest_vec1_def)
  1.5036 -
  1.5037 -lemma interval_eq_empty_1: fixes a :: "real^1" shows
  1.5038 -  "{a .. b} = {} \<longleftrightarrow> dest_vec1 b < dest_vec1 a"
  1.5039 -  "{a<..<b} = {} \<longleftrightarrow> dest_vec1 b \<le> dest_vec1 a"
  1.5040 -  unfolding interval_eq_empty and ex_1 and dest_vec1_def by auto
  1.5041 -
  1.5042 -lemma subset_interval_1: fixes a :: "real^1" shows
  1.5043 - "({a .. b} \<subseteq> {c .. d} \<longleftrightarrow>  dest_vec1 b < dest_vec1 a \<or>
  1.5044 -                dest_vec1 c \<le> dest_vec1 a \<and> dest_vec1 a \<le> dest_vec1 b \<and> dest_vec1 b \<le> dest_vec1 d)"
  1.5045 - "({a .. b} \<subseteq> {c<..<d} \<longleftrightarrow>  dest_vec1 b < dest_vec1 a \<or>
  1.5046 -                dest_vec1 c < dest_vec1 a \<and> dest_vec1 a \<le> dest_vec1 b \<and> dest_vec1 b < dest_vec1 d)"
  1.5047 - "({a<..<b} \<subseteq> {c .. d} \<longleftrightarrow>  dest_vec1 b \<le> dest_vec1 a \<or>
  1.5048 -                dest_vec1 c \<le> dest_vec1 a \<and> dest_vec1 a < dest_vec1 b \<and> dest_vec1 b \<le> dest_vec1 d)"
  1.5049 - "({a<..<b} \<subseteq> {c<..<d} \<longleftrightarrow> dest_vec1 b \<le> dest_vec1 a \<or>
  1.5050 -                dest_vec1 c \<le> dest_vec1 a \<and> dest_vec1 a < dest_vec1 b \<and> dest_vec1 b \<le> dest_vec1 d)"
  1.5051 -  unfolding subset_interval[of a b c d] unfolding all_1 and dest_vec1_def by auto
  1.5052 -
  1.5053 -lemma eq_interval_1: fixes a :: "real^1" shows
  1.5054 - "{a .. b} = {c .. d} \<longleftrightarrow>
  1.5055 -          dest_vec1 b < dest_vec1 a \<and> dest_vec1 d < dest_vec1 c \<or>
  1.5056 -          dest_vec1 a = dest_vec1 c \<and> dest_vec1 b = dest_vec1 d"
  1.5057 -using set_eq_subset[of "{a .. b}" "{c .. d}"]
  1.5058 -using subset_interval_1(1)[of a b c d]
  1.5059 -using subset_interval_1(1)[of c d a b]
  1.5060 -by auto (* FIXME: slow *)
  1.5061 -
  1.5062 -lemma disjoint_interval_1: fixes a :: "real^1" shows
  1.5063 -  "{a .. b} \<inter> {c .. d} = {} \<longleftrightarrow> dest_vec1 b < dest_vec1 a \<or> dest_vec1 d < dest_vec1 c  \<or>  dest_vec1 b < dest_vec1 c \<or> dest_vec1 d < dest_vec1 a"
  1.5064 -  "{a .. b} \<inter> {c<..<d} = {} \<longleftrightarrow> dest_vec1 b < dest_vec1 a \<or> dest_vec1 d \<le> dest_vec1 c  \<or>  dest_vec1 b \<le> dest_vec1 c \<or> dest_vec1 d \<le> dest_vec1 a"
  1.5065 -  "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> dest_vec1 b \<le> dest_vec1 a \<or> dest_vec1 d < dest_vec1 c  \<or>  dest_vec1 b \<le> dest_vec1 c \<or> dest_vec1 d \<le> dest_vec1 a"
  1.5066 -  "{a<..<b} \<inter> {c<..<d} = {} \<longleftrightarrow> dest_vec1 b \<le> dest_vec1 a \<or> dest_vec1 d \<le> dest_vec1 c  \<or>  dest_vec1 b \<le> dest_vec1 c \<or> dest_vec1 d \<le> dest_vec1 a"
  1.5067 -  unfolding disjoint_interval and dest_vec1_def ex_1 by auto
  1.5068 -
  1.5069 -lemma open_closed_interval_1: fixes a :: "real^1" shows
  1.5070 - "{a<..<b} = {a .. b} - {a, b}"
  1.5071 -  unfolding expand_set_eq apply simp unfolding vector_less_def and vector_less_eq_def and all_1 and dest_vec1_eq[THEN sym] and dest_vec1_def by auto
  1.5072 -
  1.5073 -lemma closed_open_interval_1: "dest_vec1 (a::real^1) \<le> dest_vec1 b ==> {a .. b} = {a<..<b} \<union> {a,b}"
  1.5074 -  unfolding expand_set_eq apply simp unfolding vector_less_def and vector_less_eq_def and all_1 and dest_vec1_eq[THEN sym] and dest_vec1_def by auto
  1.5075 -
  1.5076 -(* Some stuff for half-infinite intervals too; FIXME: notation?  *)
  1.5077 -
  1.5078 -lemma closed_interval_left: fixes b::"real^'n::finite"
  1.5079 -  shows "closed {x::real^'n. \<forall>i. x$i \<le> b$i}"
  1.5080 -proof-
  1.5081 -  { fix i
  1.5082 -    fix x::"real^'n" assume x:"\<forall>e>0. \<exists>x'\<in>{x. \<forall>i. x $ i \<le> b $ i}. x' \<noteq> x \<and> dist x' x < e"
  1.5083 -    { assume "x$i > b$i"
  1.5084 -      then obtain y where "y $ i \<le> b $ i"  "y \<noteq> x"  "dist y x < x$i - b$i" using x[THEN spec[where x="x$i - b$i"]] by auto
  1.5085 -      hence False using component_le_norm[of "y - x" i] unfolding dist_norm and vector_minus_component by auto   }
  1.5086 -    hence "x$i \<le> b$i" by(rule ccontr)auto  }
  1.5087 -  thus ?thesis unfolding closed_limpt unfolding islimpt_approachable by blast
  1.5088 -qed
  1.5089 -
  1.5090 -lemma closed_interval_right: fixes a::"real^'n::finite"
  1.5091 -  shows "closed {x::real^'n. \<forall>i. a$i \<le> x$i}"
  1.5092 -proof-
  1.5093 -  { fix i
  1.5094 -    fix x::"real^'n" assume x:"\<forall>e>0. \<exists>x'\<in>{x. \<forall>i. a $ i \<le> x $ i}. x' \<noteq> x \<and> dist x' x < e"
  1.5095 -    { assume "a$i > x$i"
  1.5096 -      then obtain y where "a $ i \<le> y $ i"  "y \<noteq> x"  "dist y x < a$i - x$i" using x[THEN spec[where x="a$i - x$i"]] by auto
  1.5097 -      hence False using component_le_norm[of "y - x" i] unfolding dist_norm and vector_minus_component by auto   }
  1.5098 -    hence "a$i \<le> x$i" by(rule ccontr)auto  }
  1.5099 -  thus ?thesis unfolding closed_limpt unfolding islimpt_approachable by blast
  1.5100 -qed
  1.5101 -
  1.5102 -subsection{* Intervals in general, including infinite and mixtures of open and closed. *}
  1.5103 -
  1.5104 -definition "is_interval s \<longleftrightarrow> (\<forall>a\<in>s. \<forall>b\<in>s. \<forall>x. (\<forall>i. ((a$i \<le> x$i \<and> x$i \<le> b$i) \<or> (b$i \<le> x$i \<and> x$i \<le> a$i)))  \<longrightarrow> x \<in> s)"
  1.5105 -
  1.5106 -lemma is_interval_interval: "is_interval {a .. b::real^'n::finite}" (is ?th1) "is_interval {a<..<b}" (is ?th2) proof - 
  1.5107 -  have *:"\<And>x y z::real. x < y \<Longrightarrow> y < z \<Longrightarrow> x < z" by auto
  1.5108 -  show ?th1 ?th2  unfolding is_interval_def mem_interval Ball_def atLeastAtMost_iff
  1.5109 -    by(meson real_le_trans le_less_trans less_le_trans *)+ qed
  1.5110 -
  1.5111 -lemma is_interval_empty:
  1.5112 - "is_interval {}"
  1.5113 -  unfolding is_interval_def
  1.5114 -  by simp
  1.5115 -
  1.5116 -lemma is_interval_univ:
  1.5117 - "is_interval UNIV"
  1.5118 -  unfolding is_interval_def
  1.5119 -  by simp
  1.5120 -
  1.5121 -subsection{* Closure of halfspaces and hyperplanes.                                    *}
  1.5122 -
  1.5123 -lemma Lim_inner:
  1.5124 -  assumes "(f ---> l) net"  shows "((\<lambda>y. inner a (f y)) ---> inner a l) net"
  1.5125 -  by (intro tendsto_intros assms)
  1.5126 -
  1.5127 -lemma continuous_at_inner: "continuous (at x) (inner a)"
  1.5128 -  unfolding continuous_at by (intro tendsto_intros)
  1.5129 -
  1.5130 -lemma continuous_on_inner:
  1.5131 -  fixes s :: "'a::real_inner set"
  1.5132 -  shows "continuous_on s (inner a)"
  1.5133 -  unfolding continuous_on by (rule ballI) (intro tendsto_intros)
  1.5134 -
  1.5135 -lemma closed_halfspace_le: "closed {x. inner a x \<le> b}"
  1.5136 -proof-
  1.5137 -  have "\<forall>x. continuous (at x) (inner a)"
  1.5138 -    unfolding continuous_at by (rule allI) (intro tendsto_intros)
  1.5139 -  hence "closed (inner a -` {..b})"
  1.5140 -    using closed_real_atMost by (rule continuous_closed_vimage)
  1.5141 -  moreover have "{x. inner a x \<le> b} = inner a -` {..b}" by auto
  1.5142 -  ultimately show ?thesis by simp
  1.5143 -qed
  1.5144 -
  1.5145 -lemma closed_halfspace_ge: "closed {x. inner a x \<ge> b}"
  1.5146 -  using closed_halfspace_le[of "-a" "-b"] unfolding inner_minus_left by auto
  1.5147 -
  1.5148 -lemma closed_hyperplane: "closed {x. inner a x = b}"
  1.5149 -proof-
  1.5150 -  have "{x. inner a x = b} = {x. inner a x \<ge> b} \<inter> {x. inner a x \<le> b}" by auto
  1.5151 -  thus ?thesis using closed_halfspace_le[of a b] and closed_halfspace_ge[of b a] using closed_Int by auto
  1.5152 -qed
  1.5153 -
  1.5154 -lemma closed_halfspace_component_le:
  1.5155 -  shows "closed {x::real^'n::finite. x$i \<le> a}"
  1.5156 -  using closed_halfspace_le[of "(basis i)::real^'n" a] unfolding inner_basis[OF assms] by auto
  1.5157 -
  1.5158 -lemma closed_halfspace_component_ge:
  1.5159 -  shows "closed {x::real^'n::finite. x$i \<ge> a}"
  1.5160 -  using closed_halfspace_ge[of a "(basis i)::real^'n"] unfolding inner_basis[OF assms] by auto
  1.5161 -
  1.5162 -text{* Openness of halfspaces.                                                   *}
  1.5163 -
  1.5164 -lemma open_halfspace_lt: "open {x. inner a x < b}"
  1.5165 -proof-
  1.5166 -  have "UNIV - {x. b \<le> inner a x} = {x. inner a x < b}" by auto
  1.5167 -  thus ?thesis using closed_halfspace_ge[unfolded closed_def Compl_eq_Diff_UNIV, of b a] by auto
  1.5168 -qed
  1.5169 -
  1.5170 -lemma open_halfspace_gt: "open {x. inner a x > b}"
  1.5171 -proof-
  1.5172 -  have "UNIV - {x. b \<ge> inner a x} = {x. inner a x > b}" by auto
  1.5173 -  thus ?thesis using closed_halfspace_le[unfolded closed_def Compl_eq_Diff_UNIV, of a b] by auto
  1.5174 -qed
  1.5175 -
  1.5176 -lemma open_halfspace_component_lt:
  1.5177 -  shows "open {x::real^'n::finite. x$i < a}"
  1.5178 -  using open_halfspace_lt[of "(basis i)::real^'n" a] unfolding inner_basis[OF assms] by auto
  1.5179 -
  1.5180 -lemma open_halfspace_component_gt:
  1.5181 -  shows "open {x::real^'n::finite. x$i  > a}"
  1.5182 -  using open_halfspace_gt[of a "(basis i)::real^'n"] unfolding inner_basis[OF assms] by auto
  1.5183 -
  1.5184 -text{* This gives a simple derivation of limit component bounds.                 *}
  1.5185 -
  1.5186 -lemma Lim_component_le: fixes f :: "'a \<Rightarrow> real^'n::finite"
  1.5187 -  assumes "(f ---> l) net" "\<not> (trivial_limit net)"  "eventually (\<lambda>x. f(x)$i \<le> b) net"
  1.5188 -  shows "l$i \<le> b"
  1.5189 -proof-
  1.5190 -  { fix x have "x \<in> {x::real^'n. inner (basis i) x \<le> b} \<longleftrightarrow> x$i \<le> b" unfolding inner_basis by auto } note * = this
  1.5191 -  show ?thesis using Lim_in_closed_set[of "{x. inner (basis i) x \<le> b}" f net l] unfolding *
  1.5192 -    using closed_halfspace_le[of "(basis i)::real^'n" b] and assms(1,2,3) by auto
  1.5193 -qed
  1.5194 -
  1.5195 -lemma Lim_component_ge: fixes f :: "'a \<Rightarrow> real^'n::finite"
  1.5196 -  assumes "(f ---> l) net"  "\<not> (trivial_limit net)"  "eventually (\<lambda>x. b \<le> (f x)$i) net"
  1.5197 -  shows "b \<le> l$i"
  1.5198 -proof-
  1.5199 -  { fix x have "x \<in> {x::real^'n. inner (basis i) x \<ge> b} \<longleftrightarrow> x$i \<ge> b" unfolding inner_basis by auto } note * = this
  1.5200 -  show ?thesis using Lim_in_closed_set[of "{x. inner (basis i) x \<ge> b}" f net l] unfolding *
  1.5201 -    using closed_halfspace_ge[of b "(basis i)::real^'n"] and assms(1,2,3) by auto
  1.5202 -qed
  1.5203 -
  1.5204 -lemma Lim_component_eq: fixes f :: "'a \<Rightarrow> real^'n::finite"
  1.5205 -  assumes net:"(f ---> l) net" "~(trivial_limit net)" and ev:"eventually (\<lambda>x. f(x)$i = b) net"
  1.5206 -  shows "l$i = b"
  1.5207 -  using ev[unfolded order_eq_iff eventually_and] using Lim_component_ge[OF net, of b i] and Lim_component_le[OF net, of i b] by auto
  1.5208 -
  1.5209 -lemma Lim_drop_le: fixes f :: "'a \<Rightarrow> real^1" shows
  1.5210 -  "(f ---> l) net \<Longrightarrow> ~(trivial_limit net) \<Longrightarrow> eventually (\<lambda>x. dest_vec1 (f x) \<le> b) net ==> dest_vec1 l \<le> b"
  1.5211 -  using Lim_component_le[of f l net 1 b] unfolding dest_vec1_def by auto
  1.5212 -
  1.5213 -lemma Lim_drop_ge: fixes f :: "'a \<Rightarrow> real^1" shows
  1.5214 - "(f ---> l) net \<Longrightarrow> ~(trivial_limit net) \<Longrightarrow> eventually (\<lambda>x. b \<le> dest_vec1 (f x)) net ==> b \<le> dest_vec1 l"
  1.5215 -  using Lim_component_ge[of f l net b 1] unfolding dest_vec1_def by auto
  1.5216 -
  1.5217 -text{* Limits relative to a union.                                               *}
  1.5218 -
  1.5219 -lemma eventually_within_Un:
  1.5220 -  "eventually P (net within (s \<union> t)) \<longleftrightarrow>
  1.5221 -    eventually P (net within s) \<and> eventually P (net within t)"
  1.5222 -  unfolding Limits.eventually_within
  1.5223 -  by (auto elim!: eventually_rev_mp)
  1.5224 -
  1.5225 -lemma Lim_within_union:
  1.5226 - "(f ---> l) (net within (s \<union> t)) \<longleftrightarrow>
  1.5227 -  (f ---> l) (net within s) \<and> (f ---> l) (net within t)"
  1.5228 -  unfolding tendsto_def
  1.5229 -  by (auto simp add: eventually_within_Un)
  1.5230 -
  1.5231 -lemma continuous_on_union:
  1.5232 -  assumes "closed s" "closed t" "continuous_on s f" "continuous_on t f"
  1.5233 -  shows "continuous_on (s \<union> t) f"
  1.5234 -  using assms unfolding continuous_on unfolding Lim_within_union
  1.5235 -  unfolding Lim unfolding trivial_limit_within unfolding closed_limpt by auto
  1.5236 -
  1.5237 -lemma continuous_on_cases:
  1.5238 -  assumes "closed s" "closed t" "continuous_on s f" "continuous_on t g"
  1.5239 -          "\<forall>x. (x\<in>s \<and> \<not> P x) \<or> (x \<in> t \<and> P x) \<longrightarrow> f x = g x"
  1.5240 -  shows "continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)"
  1.5241 -proof-
  1.5242 -  let ?h = "(\<lambda>x. if P x then f x else g x)"
  1.5243 -  have "\<forall>x\<in>s. f x = (if P x then f x else g x)" using assms(5) by auto
  1.5244 -  hence "continuous_on s ?h" using continuous_on_eq[of s f ?h] using assms(3) by auto
  1.5245 -  moreover
  1.5246 -  have "\<forall>x\<in>t. g x = (if P x then f x else g x)" using assms(5) by auto
  1.5247 -  hence "continuous_on t ?h" using continuous_on_eq[of t g ?h] using assms(4) by auto
  1.5248 -  ultimately show ?thesis using continuous_on_union[OF assms(1,2), of ?h] by auto
  1.5249 -qed
  1.5250 -
  1.5251 -
  1.5252 -text{* Some more convenient intermediate-value theorem formulations.             *}
  1.5253 -
  1.5254 -lemma connected_ivt_hyperplane:
  1.5255 -  assumes "connected s" "x \<in> s" "y \<in> s" "inner a x \<le> b" "b \<le> inner a y"
  1.5256 -  shows "\<exists>z \<in> s. inner a z = b"
  1.5257 -proof(rule ccontr)
  1.5258 -  assume as:"\<not> (\<exists>z\<in>s. inner a z = b)"
  1.5259 -  let ?A = "{x. inner a x < b}"
  1.5260 -  let ?B = "{x. inner a x > b}"
  1.5261 -  have "open ?A" "open ?B" using open_halfspace_lt and open_halfspace_gt by auto
  1.5262 -  moreover have "?A \<inter> ?B = {}" by auto
  1.5263 -  moreover have "s \<subseteq> ?A \<union> ?B" using as by auto
  1.5264 -  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
  1.5265 -qed
  1.5266 -
  1.5267 -lemma connected_ivt_component: fixes x::"real^'n::finite" shows
  1.5268 - "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)"
  1.5269 -  using connected_ivt_hyperplane[of s x y "(basis k)::real^'n" a] by (auto simp add: inner_basis)
  1.5270 -
  1.5271 -text{* Also more convenient formulations of monotone convergence.                *}
  1.5272 -
  1.5273 -lemma bounded_increasing_convergent: fixes s::"nat \<Rightarrow> real^1"
  1.5274 -  assumes "bounded {s n| n::nat. True}"  "\<forall>n. dest_vec1(s n) \<le> dest_vec1(s(Suc n))"
  1.5275 -  shows "\<exists>l. (s ---> l) sequentially"
  1.5276 -proof-
  1.5277 -  obtain a where a:"\<forall>n. \<bar>dest_vec1 (s n)\<bar> \<le>  a" using assms(1)[unfolded bounded_iff abs_dest_vec1] by auto
  1.5278 -  { fix m::nat
  1.5279 -    have "\<And> n. n\<ge>m \<longrightarrow> dest_vec1 (s m) \<le> dest_vec1 (s n)"
  1.5280 -      apply(induct_tac n) apply simp using assms(2) apply(erule_tac x="na" in allE) by(auto simp add: not_less_eq_eq)  }
  1.5281 -  hence "\<forall>m n. m \<le> n \<longrightarrow> dest_vec1 (s m) \<le> dest_vec1 (s n)" by auto
  1.5282 -  then obtain l where "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<bar>dest_vec1 (s n) - l\<bar> < e" using convergent_bounded_monotone[OF a] unfolding monoseq_def by auto
  1.5283 -  thus ?thesis unfolding Lim_sequentially apply(rule_tac x="vec1 l" in exI)
  1.5284 -    unfolding dist_norm unfolding abs_dest_vec1 and dest_vec1_sub by auto
  1.5285 -qed
  1.5286 -
  1.5287 -subsection{* Basic homeomorphism definitions.                                          *}
  1.5288 -
  1.5289 -definition "homeomorphism s t f g \<equiv>
  1.5290 -     (\<forall>x\<in>s. (g(f x) = x)) \<and> (f ` s = t) \<and> continuous_on s f \<and>
  1.5291 -     (\<forall>y\<in>t. (f(g y) = y)) \<and> (g ` t = s) \<and> continuous_on t g"
  1.5292 -
  1.5293 -definition
  1.5294 -  homeomorphic :: "'a::metric_space set \<Rightarrow> 'b::metric_space set \<Rightarrow> bool"
  1.5295 -    (infixr "homeomorphic" 60) where
  1.5296 -  homeomorphic_def: "s homeomorphic t \<equiv> (\<exists>f g. homeomorphism s t f g)"
  1.5297 -
  1.5298 -lemma homeomorphic_refl: "s homeomorphic s"
  1.5299 -  unfolding homeomorphic_def
  1.5300 -  unfolding homeomorphism_def
  1.5301 -  using continuous_on_id
  1.5302 -  apply(rule_tac x = "(\<lambda>x. x)" in exI)
  1.5303 -  apply(rule_tac x = "(\<lambda>x. x)" in exI)
  1.5304 -  by blast
  1.5305 -
  1.5306 -lemma homeomorphic_sym:
  1.5307 - "s homeomorphic t \<longleftrightarrow> t homeomorphic s"
  1.5308 -unfolding homeomorphic_def
  1.5309 -unfolding homeomorphism_def
  1.5310 -by blast (* FIXME: slow *)
  1.5311 -
  1.5312 -lemma homeomorphic_trans:
  1.5313 -  assumes "s homeomorphic t" "t homeomorphic u" shows "s homeomorphic u"
  1.5314 -proof-
  1.5315 -  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"
  1.5316 -    using assms(1) unfolding homeomorphic_def homeomorphism_def by auto
  1.5317 -  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"
  1.5318 -    using assms(2) unfolding homeomorphic_def homeomorphism_def by auto
  1.5319 -
  1.5320 -  { 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 }
  1.5321 -  moreover have "(f2 \<circ> f1) ` s = u" using fg1(2) fg2(2) by auto
  1.5322 -  moreover have "continuous_on s (f2 \<circ> f1)" using continuous_on_compose[OF fg1(3)] and fg2(3) unfolding fg1(2) by auto
  1.5323 -  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 }
  1.5324 -  moreover have "(g1 \<circ> g2) ` u = s" using fg1(5) fg2(5) by auto
  1.5325 -  moreover have "continuous_on u (g1 \<circ> g2)" using continuous_on_compose[OF fg2(6)] and fg1(6)  unfolding fg2(5) by auto
  1.5326 -  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
  1.5327 -qed
  1.5328 -
  1.5329 -lemma homeomorphic_minimal:
  1.5330 - "s homeomorphic t \<longleftrightarrow>
  1.5331 -    (\<exists>f g. (\<forall>x\<in>s. f(x) \<in> t \<and> (g(f(x)) = x)) \<and>
  1.5332 -           (\<forall>y\<in>t. g(y) \<in> s \<and> (f(g(y)) = y)) \<and>
  1.5333 -           continuous_on s f \<and> continuous_on t g)"
  1.5334 -unfolding homeomorphic_def homeomorphism_def
  1.5335 -apply auto apply (rule_tac x=f in exI) apply (rule_tac x=g in exI)
  1.5336 -apply auto apply (rule_tac x=f in exI) apply (rule_tac x=g in exI) apply auto
  1.5337 -unfolding image_iff
  1.5338 -apply(erule_tac x="g x" in ballE) apply(erule_tac x="x" in ballE)
  1.5339 -apply auto apply(rule_tac x="g x" in bexI) apply auto
  1.5340 -apply(erule_tac x="f x" in ballE) apply(erule_tac x="x" in ballE)
  1.5341 -apply auto apply(rule_tac x="f x" in bexI) by auto
  1.5342 -
  1.5343 -subsection{* Relatively weak hypotheses if a set is compact.                           *}
  1.5344 -
  1.5345 -definition "inv_on f s = (\<lambda>x. SOME y. y\<in>s \<and> f y = x)"
  1.5346 -
  1.5347 -lemma assumes "inj_on f s" "x\<in>s"
  1.5348 -  shows "inv_on f s (f x) = x"
  1.5349 - using assms unfolding inj_on_def inv_on_def by auto
  1.5350 -
  1.5351 -lemma homeomorphism_compact:
  1.5352 -  fixes f :: "'a::heine_borel \<Rightarrow> 'b::heine_borel"
  1.5353 -    (* class constraint due to continuous_on_inverse *)
  1.5354 -  assumes "compact s" "continuous_on s f"  "f ` s = t"  "inj_on f s"
  1.5355 -  shows "\<exists>g. homeomorphism s t f g"
  1.5356 -proof-
  1.5357 -  def g \<equiv> "\<lambda>x. SOME y. y\<in>s \<and> f y = x"
  1.5358 -  have g:"\<forall>x\<in>s. g (f x) = x" using assms(3) assms(4)[unfolded inj_on_def] unfolding g_def by auto
  1.5359 -  { fix y assume "y\<in>t"
  1.5360 -    then obtain x where x:"f x = y" "x\<in>s" using assms(3) by auto
  1.5361 -    hence "g (f x) = x" using g by auto
  1.5362 -    hence "f (g y) = y" unfolding x(1)[THEN sym] by auto  }
  1.5363 -  hence g':"\<forall>x\<in>t. f (g x) = x" by auto
  1.5364 -  moreover
  1.5365 -  { fix x
  1.5366 -    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"])
  1.5367 -    moreover
  1.5368 -    { assume "x\<in>g ` t"
  1.5369 -      then obtain y where y:"y\<in>t" "g y = x" by auto
  1.5370 -      then obtain x' where x':"x'\<in>s" "f x' = y" using assms(3) by auto
  1.5371 -      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 }
  1.5372 -    ultimately have "x\<in>s \<longleftrightarrow> x \<in> g ` t" by auto  }
  1.5373 -  hence "g ` t = s" by auto
  1.5374 -  ultimately
  1.5375 -  show ?thesis unfolding homeomorphism_def homeomorphic_def
  1.5376 -    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
  1.5377 -qed
  1.5378 -
  1.5379 -lemma homeomorphic_compact:
  1.5380 -  fixes f :: "'a::heine_borel \<Rightarrow> 'b::heine_borel"
  1.5381 -    (* class constraint due to continuous_on_inverse *)
  1.5382 -  shows "compact s \<Longrightarrow> continuous_on s f \<Longrightarrow> (f ` s = t) \<Longrightarrow> inj_on f s
  1.5383 -          \<Longrightarrow> s homeomorphic t"
  1.5384 -  unfolding homeomorphic_def by(metis homeomorphism_compact)
  1.5385 -
  1.5386 -text{* Preservation of topological properties.                                   *}
  1.5387 -
  1.5388 -lemma homeomorphic_compactness:
  1.5389 - "s homeomorphic t ==> (compact s \<longleftrightarrow> compact t)"
  1.5390 -unfolding homeomorphic_def homeomorphism_def
  1.5391 -by (metis compact_continuous_image)
  1.5392 -
  1.5393 -text{* Results on translation, scaling etc.                                      *}
  1.5394 -
  1.5395 -lemma homeomorphic_scaling:
  1.5396 -  fixes s :: "'a::real_normed_vector set"
  1.5397 -  assumes "c \<noteq> 0"  shows "s homeomorphic ((\<lambda>x. c *\<^sub>R x) ` s)"
  1.5398 -  unfolding homeomorphic_minimal
  1.5399 -  apply(rule_tac x="\<lambda>x. c *\<^sub>R x" in exI)
  1.5400 -  apply(rule_tac x="\<lambda>x. (1 / c) *\<^sub>R x" in exI)
  1.5401 -  using assms apply auto
  1.5402 -  using continuous_on_cmul[OF continuous_on_id] by auto
  1.5403 -
  1.5404 -lemma homeomorphic_translation:
  1.5405 -  fixes s :: "'a::real_normed_vector set"
  1.5406 -  shows "s homeomorphic ((\<lambda>x. a + x) ` s)"
  1.5407 -  unfolding homeomorphic_minimal
  1.5408 -  apply(rule_tac x="\<lambda>x. a + x" in exI)
  1.5409 -  apply(rule_tac x="\<lambda>x. -a + x" in exI)
  1.5410 -  using continuous_on_add[OF continuous_on_const continuous_on_id] by auto
  1.5411 -
  1.5412 -lemma homeomorphic_affinity:
  1.5413 -  fixes s :: "'a::real_normed_vector set"
  1.5414 -  assumes "c \<noteq> 0"  shows "s homeomorphic ((\<lambda>x. a + c *\<^sub>R x) ` s)"
  1.5415 -proof-
  1.5416 -  have *:"op + a ` op *\<^sub>R c ` s = (\<lambda>x. a + c *\<^sub>R x) ` s" by auto
  1.5417 -  show ?thesis
  1.5418 -    using homeomorphic_trans
  1.5419 -    using homeomorphic_scaling[OF assms, of s]
  1.5420 -    using homeomorphic_translation[of "(\<lambda>x. c *\<^sub>R x) ` s" a] unfolding * by auto
  1.5421 -qed
  1.5422 -
  1.5423 -lemma homeomorphic_balls:
  1.5424 -  fixes a b ::"'a::real_normed_vector" (* FIXME: generalize to metric_space *)
  1.5425 -  assumes "0 < d"  "0 < e"
  1.5426 -  shows "(ball a d) homeomorphic  (ball b e)" (is ?th)
  1.5427 -        "(cball a d) homeomorphic (cball b e)" (is ?cth)
  1.5428 -proof-
  1.5429 -  have *:"\<bar>e / d\<bar> > 0" "\<bar>d / e\<bar> >0" using assms using divide_pos_pos by auto
  1.5430 -  show ?th unfolding homeomorphic_minimal
  1.5431 -    apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
  1.5432 -    apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
  1.5433 -    using assms apply (auto simp add: dist_commute)
  1.5434 -    unfolding dist_norm
  1.5435 -    apply (auto simp add: pos_divide_less_eq mult_strict_left_mono)
  1.5436 -    unfolding continuous_on
  1.5437 -    by (intro ballI tendsto_intros, simp, assumption)+
  1.5438 -next
  1.5439 -  have *:"\<bar>e / d\<bar> > 0" "\<bar>d / e\<bar> >0" using assms using divide_pos_pos by auto
  1.5440 -  show ?cth unfolding homeomorphic_minimal
  1.5441 -    apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
  1.5442 -    apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
  1.5443 -    using assms apply (auto simp add: dist_commute)
  1.5444 -    unfolding dist_norm
  1.5445 -    apply (auto simp add: pos_divide_le_eq)
  1.5446 -    unfolding continuous_on
  1.5447 -    by (intro ballI tendsto_intros, simp, assumption)+
  1.5448 -qed
  1.5449 -
  1.5450 -text{* "Isometry" (up to constant bounds) of injective linear map etc.           *}
  1.5451 -
  1.5452 -lemma cauchy_isometric:
  1.5453 -  fixes x :: "nat \<Rightarrow> real ^ 'n::finite"
  1.5454 -  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)"
  1.5455 -  shows "Cauchy x"
  1.5456 -proof-
  1.5457 -  interpret f: bounded_linear f by fact
  1.5458 -  { fix d::real assume "d>0"
  1.5459 -    then obtain N where N:"\<forall>n\<ge>N. norm (f (x n) - f (x N)) < e * d"
  1.5460 -      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
  1.5461 -    { fix n assume "n\<ge>N"
  1.5462 -      hence "norm (f (x n - x N)) < e * d" using N[THEN spec[where x=n]] unfolding f.diff[THEN sym] by auto
  1.5463 -      moreover have "e * norm (x n - x N) \<le> norm (f (x n - x N))"
  1.5464 -        using subspace_sub[OF s, of "x n" "x N"] using xs[THEN spec[where x=N]] and xs[THEN spec[where x=n]]
  1.5465 -        using normf[THEN bspec[where x="x n - x N"]] by auto
  1.5466 -      ultimately have "norm (x n - x N) < d" using `e>0`
  1.5467 -        using mult_left_less_imp_less[of e "norm (x n - x N)" d] by auto   }
  1.5468 -    hence "\<exists>N. \<forall>n\<ge>N. norm (x n - x N) < d" by auto }
  1.5469 -  thus ?thesis unfolding cauchy and dist_norm by auto
  1.5470 -qed
  1.5471 -
  1.5472 -lemma complete_isometric_image:
  1.5473 -  fixes f :: "real ^ _ \<Rightarrow> real ^ _"
  1.5474 -  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"
  1.5475 -  shows "complete(f ` s)"
  1.5476 -proof-
  1.5477 -  { fix g assume as:"\<forall>n::nat. g n \<in> f ` s" and cfg:"Cauchy g"
  1.5478 -    then obtain x where "\<forall>n. x n \<in> s \<and> g n = f (x n)" unfolding image_iff and Bex_def
  1.5479 -      using choice[of "\<lambda> n xa. xa \<in> s \<and> g n = f xa"] by auto
  1.5480 -    hence x:"\<forall>n. x n \<in> s"  "\<forall>n. g n = f (x n)" by auto
  1.5481 -    hence "f \<circ> x = g" unfolding expand_fun_eq by auto
  1.5482 -    then obtain l where "l\<in>s" and l:"(x ---> l) sequentially"
  1.5483 -      using cs[unfolded complete_def, THEN spec[where x="x"]]
  1.5484 -      using cauchy_isometric[OF `0<e` s f normf] and cfg and x(1) by auto
  1.5485 -    hence "\<exists>l\<in>f ` s. (g ---> l) sequentially"
  1.5486 -      using linear_continuous_at[OF f, unfolded continuous_at_sequentially, THEN spec[where x=x], of l]
  1.5487 -      unfolding `f \<circ> x = g` by auto  }
  1.5488 -  thus ?thesis unfolding complete_def by auto
  1.5489 -qed
  1.5490 -
  1.5491 -lemma dist_0_norm:
  1.5492 -  fixes x :: "'a::real_normed_vector"
  1.5493 -  shows "dist 0 x = norm x"
  1.5494 -unfolding dist_norm by simp
  1.5495 -
  1.5496 -lemma injective_imp_isometric: fixes f::"real^'m::finite \<Rightarrow> real^'n::finite"
  1.5497 -  assumes s:"closed s"  "subspace s"  and f:"bounded_linear f" "\<forall>x\<in>s. (f x = 0) \<longrightarrow> (x = 0)"
  1.5498 -  shows "\<exists>e>0. \<forall>x\<in>s. norm (f x) \<ge> e * norm(x)"
  1.5499 -proof(cases "s \<subseteq> {0::real^'m}")
  1.5500 -  case True
  1.5501 -  { fix x assume "x \<in> s"
  1.5502 -    hence "x = 0" using True by auto
  1.5503 -    hence "norm x \<le> norm (f x)" by auto  }
  1.5504 -  thus ?thesis by(auto intro!: exI[where x=1])
  1.5505 -next
  1.5506 -  interpret f: bounded_linear f by fact
  1.5507 -  case False
  1.5508 -  then obtain a where a:"a\<noteq>0" "a\<in>s" by auto
  1.5509 -  from False have "s \<noteq> {}" by auto
  1.5510 -  let ?S = "{f x| x. (x \<in> s \<and> norm x = norm a)}"
  1.5511 -  let ?S' = "{x::real^'m. x\<in>s \<and> norm x = norm a}"
  1.5512 -  let ?S'' = "{x::real^'m. norm x = norm a}"
  1.5513 -
  1.5514 -  have "?S'' = frontier(cball 0 (norm a))" unfolding frontier_cball and dist_norm by (auto simp add: norm_minus_cancel)
  1.5515 -  hence "compact ?S''" using compact_frontier[OF compact_cball, of 0 "norm a"] by auto
  1.5516 -  moreover have "?S' = s \<inter> ?S''" by auto
  1.5517 -  ultimately have "compact ?S'" using closed_inter_compact[of s ?S''] using s(1) by auto
  1.5518 -  moreover have *:"f ` ?S' = ?S" by auto
  1.5519 -  ultimately have "compact ?S" using compact_continuous_image[OF linear_continuous_on[OF f(1)], of ?S'] by auto
  1.5520 -  hence "closed ?S" using compact_imp_closed by auto
  1.5521 -  moreover have "?S \<noteq> {}" using a by auto
  1.5522 -  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
  1.5523 -  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
  1.5524 -
  1.5525 -  let ?e = "norm (f b) / norm b"
  1.5526 -  have "norm b > 0" using ba and a and norm_ge_zero by auto
  1.5527 -  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
  1.5528 -  ultimately have "0 < norm (f b) / norm b" by(simp only: divide_pos_pos)
  1.5529 -  moreover
  1.5530 -  { fix x assume "x\<in>s"
  1.5531 -    hence "norm (f b) / norm b * norm x \<le> norm (f x)"
  1.5532 -    proof(cases "x=0")
  1.5533 -      case True thus "norm (f b) / norm b * norm x \<le> norm (f x)" by auto
  1.5534 -    next
  1.5535 -      case False
  1.5536 -      hence *:"0 < norm a / norm x" using `a\<noteq>0` unfolding zero_less_norm_iff[THEN sym] by(simp only: divide_pos_pos)
  1.5537 -      have "\<forall>c. \<forall>x\<in>s. c *\<^sub>R x \<in> s" using s[unfolded subspace_def smult_conv_scaleR] by auto
  1.5538 -      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
  1.5539 -      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"]]
  1.5540 -        unfolding f.scaleR and ba using `x\<noteq>0` `a\<noteq>0`
  1.5541 -        by (auto simp add: real_mult_commute pos_le_divide_eq pos_divide_le_eq)
  1.5542 -    qed }
  1.5543 -  ultimately
  1.5544 -  show ?thesis by auto
  1.5545 -qed
  1.5546 -
  1.5547 -lemma closed_injective_image_subspace:
  1.5548 -  fixes f :: "real ^ _ \<Rightarrow> real ^ _"
  1.5549 -  assumes "subspace s" "bounded_linear f" "\<forall>x\<in>s. f x = 0 --> x = 0" "closed s"
  1.5550 -  shows "closed(f ` s)"
  1.5551 -proof-
  1.5552 -  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
  1.5553 -  show ?thesis using complete_isometric_image[OF `e>0` assms(1,2) e] and assms(4)
  1.5554 -    unfolding complete_eq_closed[THEN sym] by auto
  1.5555 -qed
  1.5556 -
  1.5557 -subsection{* Some properties of a canonical subspace.                                  *}
  1.5558 -
  1.5559 -lemma subspace_substandard:
  1.5560 - "subspace {x::real^'n. (\<forall>i. P i \<longrightarrow> x$i = 0)}"
  1.5561 -  unfolding subspace_def by(auto simp add: vector_add_component vector_smult_component elim!: ballE)
  1.5562 -
  1.5563 -lemma closed_substandard:
  1.5564 - "closed {x::real^'n::finite. \<forall>i. P i --> x$i = 0}" (is "closed ?A")
  1.5565 -proof-
  1.5566 -  let ?D = "{i. P i}"
  1.5567 -  let ?Bs = "{{x::real^'n. inner (basis i) x = 0}| i. i \<in> ?D}"
  1.5568 -  { fix x
  1.5569 -    { assume "x\<in>?A"
  1.5570 -      hence x:"\<forall>i\<in>?D. x $ i = 0" by auto
  1.5571 -      hence "x\<in> \<Inter> ?Bs" by(auto simp add: inner_basis x) }
  1.5572 -    moreover
  1.5573 -    { assume x:"x\<in>\<Inter>?Bs"
  1.5574 -      { fix i assume i:"i \<in> ?D"
  1.5575 -        then obtain B where BB:"B \<in> ?Bs" and B:"B = {x::real^'n. inner (basis i) x = 0}" by auto
  1.5576 -        hence "x $ i = 0" unfolding B using x unfolding inner_basis by auto  }
  1.5577 -      hence "x\<in>?A" by auto }
  1.5578 -    ultimately have "x\<in>?A \<longleftrightarrow> x\<in> \<Inter>?Bs" by auto }
  1.5579 -  hence "?A = \<Inter> ?Bs" by auto
  1.5580 -  thus ?thesis by(auto simp add: closed_Inter closed_hyperplane)
  1.5581 -qed
  1.5582 -
  1.5583 -lemma dim_substandard:
  1.5584 -  shows "dim {x::real^'n::finite. \<forall>i. i \<notin> d \<longrightarrow> x$i = 0} = card d" (is "dim ?A = _")
  1.5585 -proof-
  1.5586 -  let ?D = "UNIV::'n set"
  1.5587 -  let ?B = "(basis::'n\<Rightarrow>real^'n) ` d"
  1.5588 -
  1.5589 -    let ?bas = "basis::'n \<Rightarrow> real^'n"
  1.5590 -
  1.5591 -  have "?B \<subseteq> ?A" by auto
  1.5592 -
  1.5593 -  moreover
  1.5594 -  { fix x::"real^'n" assume "x\<in>?A"
  1.5595 -    with finite[of d]
  1.5596 -    have "x\<in> span ?B"
  1.5597 -    proof(induct d arbitrary: x)
  1.5598 -      case empty hence "x=0" unfolding Cart_eq by auto
  1.5599 -      thus ?case using subspace_0[OF subspace_span[of "{}"]] by auto
  1.5600 -    next
  1.5601 -      case (insert k F)
  1.5602 -      hence *:"\<forall>i. i \<notin> insert k F \<longrightarrow> x $ i = 0" by auto
  1.5603 -      have **:"F \<subseteq> insert k F" by auto
  1.5604 -      def y \<equiv> "x - x$k *\<^sub>R basis k"
  1.5605 -      have y:"x = y + (x$k) *\<^sub>R basis k" unfolding y_def by auto
  1.5606 -      { fix i assume i':"i \<notin> F"
  1.5607 -        hence "y $ i = 0" unfolding y_def unfolding vector_minus_component
  1.5608 -          and vector_smult_component and basis_component
  1.5609 -          using *[THEN spec[where x=i]] by auto }
  1.5610 -      hence "y \<in> span (basis ` (insert k F))" using insert(3)
  1.5611 -        using span_mono[of "?bas ` F" "?bas ` (insert k F)"]
  1.5612 -        using image_mono[OF **, of basis] by auto
  1.5613 -      moreover
  1.5614 -      have "basis k \<in> span (?bas ` (insert k F))" by(rule span_superset, auto)
  1.5615 -      hence "x$k *\<^sub>R basis k \<in> span (?bas ` (insert k F))"
  1.5616 -        using span_mul [where 'a=real, unfolded smult_conv_scaleR] by auto
  1.5617 -      ultimately
  1.5618 -      have "y + x$k *\<^sub>R basis k \<in> span (?bas ` (insert k F))"
  1.5619 -        using span_add by auto
  1.5620 -      thus ?case using y by auto
  1.5621 -    qed
  1.5622 -  }
  1.5623 -  hence "?A \<subseteq> span ?B" by auto
  1.5624 -
  1.5625 -  moreover
  1.5626 -  { fix x assume "x \<in> ?B"
  1.5627 -    hence "x\<in>{(basis i)::real^'n |i. i \<in> ?D}" using assms by auto  }
  1.5628 -  hence "independent ?B" using independent_mono[OF independent_stdbasis, of ?B] and assms by auto
  1.5629 -
  1.5630 -  moreover
  1.5631 -  have "d \<subseteq> ?D" unfolding subset_eq using assms by auto
  1.5632 -  hence *:"inj_on (basis::'n\<Rightarrow>real^'n) d" using subset_inj_on[OF basis_inj, of "d"] by auto
  1.5633 -  have "?B hassize (card d)" unfolding hassize_def and card_image[OF *] by auto
  1.5634 -
  1.5635 -  ultimately show ?thesis using dim_unique[of "basis ` d" ?A] by auto
  1.5636 -qed
  1.5637 -
  1.5638 -text{* Hence closure and completeness of all subspaces.                          *}
  1.5639 -
  1.5640 -lemma closed_subspace_lemma: "n \<le> card (UNIV::'n::finite set) \<Longrightarrow> \<exists>A::'n set. card A = n"
  1.5641 -apply (induct n)
  1.5642 -apply (rule_tac x="{}" in exI, simp)
  1.5643 -apply clarsimp
  1.5644 -apply (subgoal_tac "\<exists>x. x \<notin> A")
  1.5645 -apply (erule exE)
  1.5646 -apply (rule_tac x="insert x A" in exI, simp)
  1.5647 -apply (subgoal_tac "A \<noteq> UNIV", auto)
  1.5648 -done
  1.5649 -
  1.5650 -lemma closed_subspace: fixes s::"(real^'n::finite) set"
  1.5651 -  assumes "subspace s" shows "closed s"
  1.5652 -proof-
  1.5653 -  have "dim s \<le> card (UNIV :: 'n set)" using dim_subset_univ by auto
  1.5654 -  then obtain d::"'n set" where t: "card d = dim s"
  1.5655 -    using closed_subspace_lemma by auto
  1.5656 -  let ?t = "{x::real^'n. \<forall>i. i \<notin> d \<longrightarrow> x$i = 0}"
  1.5657 -  obtain f where f:"bounded_linear f"  "f ` ?t = s" "inj_on f ?t"
  1.5658 -    using subspace_isomorphism[unfolded linear_conv_bounded_linear, OF subspace_substandard[of "\<lambda>i. i \<notin> d"] assms]
  1.5659 -    using dim_substandard[of d] and t by auto
  1.5660 -  interpret f: bounded_linear f by fact
  1.5661 -  have "\<forall>x\<in>?t. f x = 0 \<longrightarrow> x = 0" using f.zero using f(3)[unfolded inj_on_def]
  1.5662 -    by(erule_tac x=0 in ballE) auto
  1.5663 -  moreover have "closed ?t" using closed_substandard .
  1.5664 -  moreover have "subspace ?t" using subspace_substandard .
  1.5665 -  ultimately show ?thesis using closed_injective_image_subspace[of ?t f]
  1.5666 -    unfolding f(2) using f(1) by auto
  1.5667 -qed
  1.5668 -
  1.5669 -lemma complete_subspace:
  1.5670 -  fixes s :: "(real ^ _) set" shows "subspace s ==> complete s"
  1.5671 -  using complete_eq_closed closed_subspace
  1.5672 -  by auto
  1.5673 -
  1.5674 -lemma dim_closure:
  1.5675 -  fixes s :: "(real ^ _) set"
  1.5676 -  shows "dim(closure s) = dim s" (is "?dc = ?d")
  1.5677 -proof-
  1.5678 -  have "?dc \<le> ?d" using closure_minimal[OF span_inc, of s]
  1.5679 -    using closed_subspace[OF subspace_span, of s]
  1.5680 -    using dim_subset[of "closure s" "span s"] unfolding dim_span by auto
  1.5681 -  thus ?thesis using dim_subset[OF closure_subset, of s] by auto
  1.5682 -qed
  1.5683 -
  1.5684 -text{* Affine transformations of intervals.                                      *}
  1.5685 -
  1.5686 -lemma affinity_inverses:
  1.5687 -  assumes m0: "m \<noteq> (0::'a::field)"
  1.5688 -  shows "(\<lambda>x. m *s x + c) o (\<lambda>x. inverse(m) *s x + (-(inverse(m) *s c))) = id"
  1.5689 -  "(\<lambda>x. inverse(m) *s x + (-(inverse(m) *s c))) o (\<lambda>x. m *s x + c) = id"
  1.5690 -  using m0
  1.5691 -apply (auto simp add: expand_fun_eq vector_add_ldistrib vector_smult_assoc)
  1.5692 -by (simp add: vector_smult_lneg[symmetric] vector_smult_assoc vector_sneg_minus1[symmetric])
  1.5693 -
  1.5694 -lemma real_affinity_le:
  1.5695 - "0 < (m::'a::ordered_field) ==> (m * x + c \<le> y \<longleftrightarrow> x \<le> inverse(m) * y + -(c / m))"
  1.5696 -  by (simp add: field_simps inverse_eq_divide)
  1.5697 -
  1.5698 -lemma real_le_affinity:
  1.5699 - "0 < (m::'a::ordered_field) ==> (y \<le> m * x + c \<longleftrightarrow> inverse(m) * y + -(c / m) \<le> x)"
  1.5700 -  by (simp add: field_simps inverse_eq_divide)
  1.5701 -
  1.5702 -lemma real_affinity_lt:
  1.5703 - "0 < (m::'a::ordered_field) ==> (m * x + c < y \<longleftrightarrow> x < inverse(m) * y + -(c / m))"
  1.5704 -  by (simp add: field_simps inverse_eq_divide)
  1.5705 -
  1.5706 -lemma real_lt_affinity:
  1.5707 - "0 < (m::'a::ordered_field) ==> (y < m * x + c \<longleftrightarrow> inverse(m) * y + -(c / m) < x)"
  1.5708 -  by (simp add: field_simps inverse_eq_divide)
  1.5709 -
  1.5710 -lemma real_affinity_eq:
  1.5711 - "(m::'a::ordered_field) \<noteq> 0 ==> (m * x + c = y \<longleftrightarrow> x = inverse(m) * y + -(c / m))"
  1.5712 -  by (simp add: field_simps inverse_eq_divide)
  1.5713 -
  1.5714 -lemma real_eq_affinity:
  1.5715 - "(m::'a::ordered_field) \<noteq> 0 ==> (y = m * x + c  \<longleftrightarrow> inverse(m) * y + -(c / m) = x)"
  1.5716 -  by (simp add: field_simps inverse_eq_divide)
  1.5717 -
  1.5718 -lemma vector_affinity_eq:
  1.5719 -  assumes m0: "(m::'a::field) \<noteq> 0"
  1.5720 -  shows "m *s x + c = y \<longleftrightarrow> x = inverse m *s y + -(inverse m *s c)"
  1.5721 -proof
  1.5722 -  assume h: "m *s x + c = y"
  1.5723 -  hence "m *s x = y - c" by (simp add: ring_simps)
  1.5724 -  hence "inverse m *s (m *s x) = inverse m *s (y - c)" by simp
  1.5725 -  then show "x = inverse m *s y + - (inverse m *s c)"
  1.5726 -    using m0 by (simp add: vector_smult_assoc vector_ssub_ldistrib)
  1.5727 -next
  1.5728 -  assume h: "x = inverse m *s y + - (inverse m *s c)"
  1.5729 -  show "m *s x + c = y" unfolding h diff_minus[symmetric]
  1.5730 -    using m0 by (simp add: vector_smult_assoc vector_ssub_ldistrib)
  1.5731 -qed
  1.5732 -
  1.5733 -lemma vector_eq_affinity:
  1.5734 - "(m::'a::field) \<noteq> 0 ==> (y = m *s x + c \<longleftrightarrow> inverse(m) *s y + -(inverse(m) *s c) = x)"
  1.5735 -  using vector_affinity_eq[where m=m and x=x and y=y and c=c]
  1.5736 -  by metis
  1.5737 -
  1.5738 -lemma image_affinity_interval: fixes m::real
  1.5739 -  fixes a b c :: "real^'n::finite"
  1.5740 -  shows "(\<lambda>x. m *\<^sub>R x + c) ` {a .. b} =
  1.5741 -            (if {a .. b} = {} then {}
  1.5742 -            else (if 0 \<le> m then {m *\<^sub>R a + c .. m *\<^sub>R b + c}
  1.5743 -            else {m *\<^sub>R b + c .. m *\<^sub>R a + c}))"
  1.5744 -proof(cases "m=0")
  1.5745 -  { fix x assume "x \<le> c" "c \<le> x"
  1.5746 -    hence "x=c" unfolding vector_less_eq_def and Cart_eq by (auto intro: order_antisym) }
  1.5747 -  moreover case True
  1.5748 -  moreover have "c \<in> {m *\<^sub>R a + c..m *\<^sub>R b + c}" unfolding True by(auto simp add: vector_less_eq_def)
  1.5749 -  ultimately show ?thesis by auto
  1.5750 -next
  1.5751 -  case False
  1.5752 -  { fix y assume "a \<le> y" "y \<le> b" "m > 0"
  1.5753 -    hence "m *\<^sub>R a + c \<le> m *\<^sub>R y + c"  "m *\<^sub>R y + c \<le> m *\<^sub>R b + c"
  1.5754 -      unfolding vector_less_eq_def by(auto simp add: vector_smult_component vector_add_component)
  1.5755 -  } moreover
  1.5756 -  { fix y assume "a \<le> y" "y \<le> b" "m < 0"
  1.5757 -    hence "m *\<^sub>R b + c \<le> m *\<^sub>R y + c"  "m *\<^sub>R y + c \<le> m *\<^sub>R a + c"
  1.5758 -      unfolding vector_less_eq_def by(auto simp add: vector_smult_component vector_add_component mult_left_mono_neg elim!:ballE)
  1.5759 -  } moreover
  1.5760 -  { fix y assume "m > 0"  "m *\<^sub>R a + c \<le> y"  "y \<le> m *\<^sub>R b + c"
  1.5761 -    hence "y \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}"
  1.5762 -      unfolding image_iff Bex_def mem_interval vector_less_eq_def
  1.5763 -      apply(auto simp add: vector_smult_component vector_add_component vector_minus_component vector_smult_assoc pth_3[symmetric]
  1.5764 -        intro!: exI[where x="(1 / m) *\<^sub>R (y - c)"])
  1.5765 -      by(auto simp add: pos_le_divide_eq pos_divide_le_eq real_mult_commute diff_le_iff)
  1.5766 -  } moreover
  1.5767 -  { fix y assume "m *\<^sub>R b + c \<le> y" "y \<le> m *\<^sub>R a + c" "m < 0"
  1.5768 -    hence "y \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}"
  1.5769 -      unfolding image_iff Bex_def mem_interval vector_less_eq_def
  1.5770 -      apply(auto simp add: vector_smult_component vector_add_component vector_minus_component vector_smult_assoc pth_3[symmetric]
  1.5771 -        intro!: exI[where x="(1 / m) *\<^sub>R (y - c)"])
  1.5772 -      by(auto simp add: neg_le_divide_eq neg_divide_le_eq real_mult_commute diff_le_iff)
  1.5773 -  }
  1.5774 -  ultimately show ?thesis using False by auto
  1.5775 -qed
  1.5776 -
  1.5777 -lemma image_smult_interval:"(\<lambda>x. m *\<^sub>R (x::real^'n::finite)) ` {a..b} =
  1.5778 -  (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})"
  1.5779 -  using image_affinity_interval[of m 0 a b] by auto
  1.5780 -
  1.5781 -subsection{* Banach fixed point theorem (not really topological...) *}
  1.5782 -
  1.5783 -lemma banach_fix:
  1.5784 -  assumes s:"complete s" "s \<noteq> {}" and c:"0 \<le> c" "c < 1" and f:"(f ` s) \<subseteq> s" and
  1.5785 -          lipschitz:"\<forall>x\<in>s. \<forall>y\<in>s. dist (f x) (f y) \<le> c * dist x y"
  1.5786 -  shows "\<exists>! x\<in>s. (f x = x)"
  1.5787 -proof-
  1.5788 -  have "1 - c > 0" using c by auto
  1.5789 -
  1.5790 -  from s(2) obtain z0 where "z0 \<in> s" by auto
  1.5791 -  def z \<equiv> "\<lambda>n. (f ^^ n) z0"
  1.5792 -  { fix n::nat
  1.5793 -    have "z n \<in> s" unfolding z_def
  1.5794 -    proof(induct n) case 0 thus ?case using `z0 \<in>s` by auto
  1.5795 -    next case Suc thus ?case using f by auto qed }
  1.5796 -  note z_in_s = this
  1.5797 -
  1.5798 -  def d \<equiv> "dist (z 0) (z 1)"
  1.5799 -
  1.5800 -  have fzn:"\<And>n. f (z n) = z (Suc n)" unfolding z_def by auto
  1.5801 -  { fix n::nat
  1.5802 -    have "dist (z n) (z (Suc n)) \<le> (c ^ n) * d"
  1.5803 -    proof(induct n)
  1.5804 -      case 0 thus ?case unfolding d_def by auto
  1.5805 -    next
  1.5806 -      case (Suc m)
  1.5807 -      hence "c * dist (z m) (z (Suc m)) \<le> c ^ Suc m * d"
  1.5808 -        using `0 \<le> c` using mult_mono1_class.mult_mono1[of "dist (z m) (z (Suc m))" "c ^ m * d" c] by auto
  1.5809 -      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]
  1.5810 -        unfolding fzn and mult_le_cancel_left by auto
  1.5811 -    qed
  1.5812 -  } note cf_z = this
  1.5813 -
  1.5814 -  { fix n m::nat
  1.5815 -    have "(1 - c) * dist (z m) (z (m+n)) \<le> (c ^ m) * d * (1 - c ^ n)"
  1.5816 -    proof(induct n)
  1.5817 -      case 0 show ?case by auto
  1.5818 -    next
  1.5819 -      case (Suc k)
  1.5820 -      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))))"
  1.5821 -        using dist_triangle and c by(auto simp add: dist_triangle)
  1.5822 -      also have "\<dots> \<le> (1 - c) * (dist (z m) (z (m + k)) + c ^ (m + k) * d)"
  1.5823 -        using cf_z[of "m + k"] and c by auto
  1.5824 -      also have "\<dots> \<le> c ^ m * d * (1 - c ^ k) + (1 - c) * c ^ (m + k) * d"
  1.5825 -        using Suc by (auto simp add: ring_simps)
  1.5826 -      also have "\<dots> = (c ^ m) * (d * (1 - c ^ k) + (1 - c) * c ^ k * d)"
  1.5827 -        unfolding power_add by (auto simp add: ring_simps)
  1.5828 -      also have "\<dots> \<le> (c ^ m) * d * (1 - c ^ Suc k)"
  1.5829 -        using c by (auto simp add: ring_simps)
  1.5830 -      finally show ?case by auto
  1.5831 -    qed
  1.5832 -  } note cf_z2 = this
  1.5833 -  { fix e::real assume "e>0"
  1.5834 -    hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (z m) (z n) < e"
  1.5835 -    proof(cases "d = 0")
  1.5836 -      case True
  1.5837 -      hence "\<And>n. z n = z0" using cf_z2[of 0] and c unfolding z_def by (auto simp add: pos_prod_le[OF `1 - c > 0`])
  1.5838 -      thus ?thesis using `e>0` by auto
  1.5839 -    next
  1.5840 -      case False hence "d>0" unfolding d_def using zero_le_dist[of "z 0" "z 1"]
  1.5841 -        by (metis False d_def real_less_def)
  1.5842 -      hence "0 < e * (1 - c) / d" using `e>0` and `1-c>0`
  1.5843 -        using divide_pos_pos[of "e * (1 - c)" d] and mult_pos_pos[of e "1 - c"] by auto
  1.5844 -      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
  1.5845 -      { fix m n::nat assume "m>n" and as:"m\<ge>N" "n\<ge>N"
  1.5846 -        have *:"c ^ n \<le> c ^ N" using `n\<ge>N` and c using power_decreasing[OF `n\<ge>N`, of c] by auto
  1.5847 -        have "1 - c ^ (m - n) > 0" using c and power_strict_mono[of c 1 "m - n"] using `m>n` by auto
  1.5848 -        hence **:"d * (1 - c ^ (m - n)) / (1 - c) > 0"
  1.5849 -          using real_mult_order[OF `d>0`, of "1 - c ^ (m - n)"]
  1.5850 -          using divide_pos_pos[of "d * (1 - c ^ (m - n))" "1 - c"]
  1.5851 -          using `0 < 1 - c` by auto
  1.5852 -
  1.5853 -        have "dist (z m) (z n) \<le> c ^ n * d * (1 - c ^ (m - n)) / (1 - c)"
  1.5854 -          using cf_z2[of n "m - n"] and `m>n` unfolding pos_le_divide_eq[OF `1-c>0`]
  1.5855 -          by (auto simp add: real_mult_commute dist_commute)
  1.5856 -        also have "\<dots> \<le> c ^ N * d * (1 - c ^ (m - n)) / (1 - c)"
  1.5857 -          using mult_right_mono[OF * order_less_imp_le[OF **]]
  1.5858 -          unfolding real_mult_assoc by auto
  1.5859 -        also have "\<dots> < (e * (1 - c) / d) * d * (1 - c ^ (m - n)) / (1 - c)"
  1.5860 -          using mult_strict_right_mono[OF N **] unfolding real_mult_assoc by auto
  1.5861 -        also have "\<dots> = e * (1 - c ^ (m - n))" using c and `d>0` and `1 - c > 0` by auto
  1.5862 -        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
  1.5863 -        finally have  "dist (z m) (z n) < e" by auto
  1.5864 -      } note * = this
  1.5865 -      { fix m n::nat assume as:"N\<le>m" "N\<le>n"
  1.5866 -        hence "dist (z n) (z m) < e"
  1.5867 -        proof(cases "n = m")
  1.5868 -          case True thus ?thesis using `e>0` by auto
  1.5869 -        next
  1.5870 -          case False thus ?thesis using as and *[of n m] *[of m n] unfolding nat_neq_iff by (auto simp add: dist_commute)
  1.5871 -        qed }
  1.5872 -      thus ?thesis by auto
  1.5873 -    qed
  1.5874 -  }
  1.5875 -  hence "Cauchy z" unfolding cauchy_def by auto
  1.5876 -  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
  1.5877 -
  1.5878 -  def e \<equiv> "dist (f x) x"
  1.5879 -  have "e = 0" proof(rule ccontr)
  1.5880 -    assume "e \<noteq> 0" hence "e>0" unfolding e_def using zero_le_dist[of "f x" x]
  1.5881 -      by (metis dist_eq_0_iff dist_nz e_def)
  1.5882 -    then obtain N where N:"\<forall>n\<ge>N. dist (z n) x < e / 2"
  1.5883 -      using x[unfolded Lim_sequentially, THEN spec[where x="e/2"]] by auto
  1.5884 -    hence N':"dist (z N) x < e / 2" by auto
  1.5885 -
  1.5886 -    have *:"c * dist (z N) x \<le> dist (z N) x" unfolding mult_le_cancel_right2
  1.5887 -      using zero_le_dist[of "z N" x] and c
  1.5888 -      by (metis dist_eq_0_iff dist_nz order_less_asym real_less_def)
  1.5889 -    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]]
  1.5890 -      using z_in_s[of N] `x\<in>s` using c by auto
  1.5891 -    also have "\<dots> < e / 2" using N' and c using * by auto
  1.5892 -    finally show False unfolding fzn
  1.5893 -      using N[THEN spec[where x="Suc N"]] and dist_triangle_half_r[of "z (Suc N)" "f x" e x]
  1.5894 -      unfolding e_def by auto
  1.5895 -  qed
  1.5896 -  hence "f x = x" unfolding e_def by auto
  1.5897 -  moreover
  1.5898 -  { fix y assume "f y = y" "y\<in>s"
  1.5899 -    hence "dist x y \<le> c * dist x y" using lipschitz[THEN bspec[where x=x], THEN bspec[where x=y]]
  1.5900 -      using `x\<in>s` and `f x = x` by auto
  1.5901 -    hence "dist x y = 0" unfolding mult_le_cancel_right1
  1.5902 -      using c and zero_le_dist[of x y] by auto
  1.5903 -    hence "y = x" by auto
  1.5904 -  }
  1.5905 -  ultimately show ?thesis unfolding Bex1_def using `x\<in>s` by blast+
  1.5906 -qed
  1.5907 -
  1.5908 -subsection{* Edelstein fixed point theorem.                                            *}
  1.5909 -
  1.5910 -lemma edelstein_fix:
  1.5911 -  fixes s :: "'a::real_normed_vector set"
  1.5912 -  assumes s:"compact s" "s \<noteq> {}" and gs:"(g ` s) \<subseteq> s"
  1.5913 -      and dist:"\<forall>x\<in>s. \<forall>y\<in>s. x \<noteq> y \<longrightarrow> dist (g x) (g y) < dist x y"
  1.5914 -  shows "\<exists>! x\<in>s. g x = x"
  1.5915 -proof(cases "\<exists>x\<in>s. g x \<noteq> x")
  1.5916 -  obtain x where "x\<in>s" using s(2) by auto
  1.5917 -  case False hence g:"\<forall>x\<in>s. g x = x" by auto
  1.5918 -  { fix y assume "y\<in>s"
  1.5919 -    hence "x = y" using `x\<in>s` and dist[THEN bspec[where x=x], THEN bspec[where x=y]]
  1.5920 -      unfolding g[THEN bspec[where x=x], OF `x\<in>s`]
  1.5921 -      unfolding g[THEN bspec[where x=y], OF `y\<in>s`] by auto  }
  1.5922 -  thus ?thesis unfolding Bex1_def using `x\<in>s` and g by blast+
  1.5923 -next
  1.5924 -  case True
  1.5925 -  then obtain x where [simp]:"x\<in>s" and "g x \<noteq> x" by auto
  1.5926 -  { fix x y assume "x \<in> s" "y \<in> s"
  1.5927 -    hence "dist (g x) (g y) \<le> dist x y"
  1.5928 -      using dist[THEN bspec[where x=x], THEN bspec[where x=y]] by auto } note dist' = this
  1.5929 -  def y \<equiv> "g x"
  1.5930 -  have [simp]:"y\<in>s" unfolding y_def using gs[unfolded image_subset_iff] and `x\<in>s` by blast
  1.5931 -  def f \<equiv> "\<lambda>n. g ^^ n"
  1.5932 -  have [simp]:"\<And>n z. g (f n z) = f (Suc n) z" unfolding f_def by auto
  1.5933 -  have [simp]:"\<And>z. f 0 z = z" unfolding f_def by auto
  1.5934 -  { fix n::nat and z assume "z\<in>s"
  1.5935 -    have "f n z \<in> s" unfolding f_def
  1.5936 -    proof(induct n)
  1.5937 -      case 0 thus ?case using `z\<in>s` by simp
  1.5938 -    next
  1.5939 -      case (Suc n) thus ?case using gs[unfolded image_subset_iff] by auto
  1.5940 -    qed } note fs = this
  1.5941 -  { fix m n ::nat assume "m\<le>n"
  1.5942 -    fix w z assume "w\<in>s" "z\<in>s"
  1.5943 -    have "dist (f n w) (f n z) \<le> dist (f m w) (f m z)" using `m\<le>n`
  1.5944 -    proof(induct n)
  1.5945 -      case 0 thus ?case by auto
  1.5946 -    next
  1.5947 -      case (Suc n)
  1.5948 -      thus ?case proof(cases "m\<le>n")
  1.5949 -        case True thus ?thesis using Suc(1)
  1.5950 -          using dist'[OF fs fs, OF `w\<in>s` `z\<in>s`, of n n] by auto
  1.5951 -      next
  1.5952 -        case False hence mn:"m = Suc n" using Suc(2) by simp
  1.5953 -        show ?thesis unfolding mn  by auto
  1.5954 -      qed
  1.5955 -    qed } note distf = this
  1.5956 -
  1.5957 -  def h \<equiv> "\<lambda>n. (f n x, f n y)"
  1.5958 -  let ?s2 = "s \<times> s"
  1.5959 -  obtain l r where "l\<in>?s2" and r:"subseq r" and lr:"((h \<circ> r) ---> l) sequentially"
  1.5960 -    using compact_Times [OF s(1) s(1), unfolded compact_def, THEN spec[where x=h]] unfolding  h_def
  1.5961 -    using fs[OF `x\<in>s`] and fs[OF `y\<in>s`] by blast
  1.5962 -  def a \<equiv> "fst l" def b \<equiv> "snd l"
  1.5963 -  have lab:"l = (a, b)" unfolding a_def b_def by simp
  1.5964 -  have [simp]:"a\<in>s" "b\<in>s" unfolding a_def b_def using `l\<in>?s2` by auto
  1.5965 -
  1.5966 -  have lima:"((fst \<circ> (h \<circ> r)) ---> a) sequentially"
  1.5967 -   and limb:"((snd \<circ> (h \<circ> r)) ---> b) sequentially"
  1.5968 -    using lr
  1.5969 -    unfolding o_def a_def b_def by (simp_all add: tendsto_intros)
  1.5970 -
  1.5971 -  { fix n::nat
  1.5972 -    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
  1.5973 -    { fix x y :: 'a
  1.5974 -      have "dist (-x) (-y) = dist x y" unfolding dist_norm
  1.5975 -        using norm_minus_cancel[of "x - y"] by (auto simp add: uminus_add_conv_diff) } note ** = this
  1.5976 -
  1.5977 -    { assume as:"dist a b > dist (f n x) (f n y)"
  1.5978 -      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"
  1.5979 -        and "\<forall>m\<ge>Nb. dist (f (r m) y) b < (dist a b - dist (f n x) (f n y)) / 2"
  1.5980 -        using lima limb unfolding h_def Lim_sequentially by (fastsimp simp del: less_divide_eq_number_of1)
  1.5981 -      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)"
  1.5982 -        apply(erule_tac x="Na+Nb+n" in allE)
  1.5983 -        apply(erule_tac x="Na+Nb+n" in allE) apply simp
  1.5984 -        using dist_triangle_add_half[of a "f (r (Na + Nb + n)) x" "dist a b - dist (f n x) (f n y)"
  1.5985 -          "-b"  "- f (r (Na + Nb + n)) y"]
  1.5986 -        unfolding ** unfolding group_simps(12) by (auto simp add: dist_commute)
  1.5987 -      moreover
  1.5988 -      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)"
  1.5989 -        using distf[of n "r (Na+Nb+n)", OF _ `x\<in>s` `y\<in>s`]
  1.5990 -        using subseq_bigger[OF r, of "Na+Nb+n"]
  1.5991 -        using *[of "f (r (Na + Nb + n)) x" "f (r (Na + Nb + n)) y" "f n x" "f n y"] by auto
  1.5992 -      ultimately have False by simp
  1.5993 -    }
  1.5994 -    hence "dist a b \<le> dist (f n x) (f n y)" by(rule ccontr)auto }
  1.5995 -  note ab_fn = this
  1.5996 -
  1.5997 -  have [simp]:"a = b" proof(rule ccontr)
  1.5998 -    def e \<equiv> "dist a b - dist (g a) (g b)"
  1.5999 -    assume "a\<noteq>b" hence "e > 0" unfolding e_def using dist by fastsimp
  1.6000 -    hence "\<exists>n. dist (f n x) a < e/2 \<and> dist (f n y) b < e/2"
  1.6001 -      using lima limb unfolding Lim_sequentially
  1.6002 -      apply (auto elim!: allE[where x="e/2"]) apply(rule_tac x="r (max N Na)" in exI) unfolding h_def by fastsimp
  1.6003 -    then obtain n where n:"dist (f n x) a < e/2 \<and> dist (f n y) b < e/2" by auto
  1.6004 -    have "dist (f (Suc n) x) (g a) \<le> dist (f n x) a"
  1.6005 -      using dist[THEN bspec[where x="f n x"], THEN bspec[where x="a"]] and fs by auto
  1.6006 -    moreover have "dist (f (Suc n) y) (g b) \<le> dist (f n y) b"
  1.6007 -      using dist[THEN bspec[where x="f n y"], THEN bspec[where x="b"]] and fs by auto
  1.6008 -    ultimately have "dist (f (Suc n) x) (g a) + dist (f (Suc n) y) (g b) < e" using n by auto
  1.6009 -    thus False unfolding e_def using ab_fn[of "Suc n"] by norm
  1.6010 -  qed
  1.6011 -
  1.6012 -  have [simp]:"\<And>n. f (Suc n) x = f n y" unfolding f_def y_def by(induct_tac n)auto
  1.6013 -  { fix x y assume "x\<in>s" "y\<in>s" moreover
  1.6014 -    fix e::real assume "e>0" ultimately
  1.6015 -    have "dist y x < e \<longrightarrow> dist (g y) (g x) < e" using dist by fastsimp }
  1.6016 -  hence "continuous_on s g" unfolding continuous_on_def by auto
  1.6017 -
  1.6018 -  hence "((snd \<circ> h \<circ> r) ---> g a) sequentially" unfolding continuous_on_sequentially
  1.6019 -    apply (rule allE[where x="\<lambda>n. (fst \<circ> h \<circ> r) n"]) apply (erule ballE[where x=a])
  1.6020 -    using lima unfolding h_def o_def using fs[OF `x\<in>s`] by (auto simp add: y_def)
  1.6021 -  hence "g a = a" using Lim_unique[OF trivial_limit_sequentially limb, of "g a"]
  1.6022 -    unfolding `a=b` and o_assoc by auto
  1.6023 -  moreover
  1.6024 -  { fix x assume "x\<in>s" "g x = x" "x\<noteq>a"
  1.6025 -    hence "False" using dist[THEN bspec[where x=a], THEN bspec[where x=x]]
  1.6026 -      using `g a = a` and `a\<in>s` by auto  }
  1.6027 -  ultimately show "\<exists>!x\<in>s. g x = x" unfolding Bex1_def using `a\<in>s` by blast
  1.6028 -qed
  1.6029 -
  1.6030 -end