src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
changeset 50884 2b21b4e2d7cb
parent 50883 1421884baf5b
child 50897 078590669527
equal deleted inserted replaced
50883:1421884baf5b 50884:2b21b4e2d7cb
  2342   apply (frule isGlb_isLb)
  2342   apply (frule isGlb_isLb)
  2343   apply (frule_tac x = y in isGlb_isLb)
  2343   apply (frule_tac x = y in isGlb_isLb)
  2344   apply (blast intro!: order_antisym dest!: isGlb_le_isLb)
  2344   apply (blast intro!: order_antisym dest!: isGlb_le_isLb)
  2345   done
  2345   done
  2346 
  2346 
  2347 
  2347 subsection {* Compactness *}
  2348 subsection {* Equivalent versions of compactness *}
  2348 
       
  2349 subsubsection{* Open-cover compactness *}
       
  2350 
       
  2351 definition compact :: "'a::topological_space set \<Rightarrow> bool" where
       
  2352   compact_eq_heine_borel: -- "This name is used for backwards compatibility"
       
  2353     "compact S \<longleftrightarrow> (\<forall>C. (\<forall>c\<in>C. open c) \<and> S \<subseteq> \<Union>C \<longrightarrow> (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D))"
       
  2354 
       
  2355 subsubsection {* Bolzano-Weierstrass property *}
       
  2356 
       
  2357 lemma heine_borel_imp_bolzano_weierstrass:
       
  2358   assumes "compact s" "infinite t"  "t \<subseteq> s"
       
  2359   shows "\<exists>x \<in> s. x islimpt t"
       
  2360 proof(rule ccontr)
       
  2361   assume "\<not> (\<exists>x \<in> s. x islimpt t)"
       
  2362   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
       
  2363     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
       
  2364   obtain g where g:"g\<subseteq>{t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"
       
  2365     using assms(1)[unfolded compact_eq_heine_borel, THEN spec[where x="{t. \<exists>x. x\<in>s \<and> t = f x}"]] using f by auto
       
  2366   from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa" by auto
       
  2367   { fix x y assume "x\<in>t" "y\<in>t" "f x = f y"
       
  2368     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
       
  2369     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  }
       
  2370   hence "inj_on f t" unfolding inj_on_def by simp
       
  2371   hence "infinite (f ` t)" using assms(2) using finite_imageD by auto
       
  2372   moreover
       
  2373   { fix x assume "x\<in>t" "f x \<notin> g"
       
  2374     from g(3) assms(3) `x\<in>t` obtain h where "h\<in>g" and "x\<in>h" by auto
       
  2375     then obtain y where "y\<in>s" "h = f y" using g'[THEN bspec[where x=h]] by auto
       
  2376     hence "y = x" using f[THEN bspec[where x=y]] and `x\<in>t` and `x\<in>h`[unfolded `h = f y`] by auto
       
  2377     hence False using `f x \<notin> g` `h\<in>g` unfolding `h = f y` by auto  }
       
  2378   hence "f ` t \<subseteq> g" by auto
       
  2379   ultimately show False using g(2) using finite_subset by auto
       
  2380 qed
       
  2381 
       
  2382 lemma islimpt_range_imp_convergent_subsequence:
       
  2383   fixes l :: "'a :: {t1_space, first_countable_topology}"
       
  2384   assumes l: "l islimpt (range f)"
       
  2385   shows "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  2386 proof -
       
  2387   from first_countable_topology_class.countable_basis_at_decseq[of l] guess A . note A = this
       
  2388 
       
  2389   def s \<equiv> "\<lambda>n i. SOME j. i < j \<and> f j \<in> A (Suc n)"
       
  2390   { fix n i
       
  2391     have "\<exists>a. i < a \<and> f a \<in> A (Suc n) - (f ` {.. i} - {l})" (is "\<exists>a. _ \<and> _ \<in> ?A")
       
  2392       apply (rule l[THEN islimptE, of "?A"])
       
  2393       using A(2) apply fastforce
       
  2394       using A(1)
       
  2395       apply (intro open_Diff finite_imp_closed)
       
  2396       apply auto
       
  2397       apply (rule_tac x=x in exI)
       
  2398       apply auto
       
  2399       done
       
  2400     then have "\<exists>a. i < a \<and> f a \<in> A (Suc n)" by blast
       
  2401     then have "i < s n i" "f (s n i) \<in> A (Suc n)"
       
  2402       unfolding s_def by (auto intro: someI2_ex) }
       
  2403   note s = this
       
  2404   def r \<equiv> "nat_rec (s 0 0) s"
       
  2405   have "subseq r"
       
  2406     by (auto simp: r_def s subseq_Suc_iff)
       
  2407   moreover
       
  2408   have "(\<lambda>n. f (r n)) ----> l"
       
  2409   proof (rule topological_tendstoI)
       
  2410     fix S assume "open S" "l \<in> S"
       
  2411     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially" by auto
       
  2412     moreover
       
  2413     { fix i assume "Suc 0 \<le> i" then have "f (r i) \<in> A i"
       
  2414         by (cases i) (simp_all add: r_def s) }
       
  2415     then have "eventually (\<lambda>i. f (r i) \<in> A i) sequentially" by (auto simp: eventually_sequentially)
       
  2416     ultimately show "eventually (\<lambda>i. f (r i) \<in> S) sequentially"
       
  2417       by eventually_elim auto
       
  2418   qed
       
  2419   ultimately show "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"
       
  2420     by (auto simp: convergent_def comp_def)
       
  2421 qed
       
  2422 
       
  2423 lemma finite_range_imp_infinite_repeats:
       
  2424   fixes f :: "nat \<Rightarrow> 'a"
       
  2425   assumes "finite (range f)"
       
  2426   shows "\<exists>k. infinite {n. f n = k}"
       
  2427 proof -
       
  2428   { fix A :: "'a set" assume "finite A"
       
  2429     hence "\<And>f. infinite {n. f n \<in> A} \<Longrightarrow> \<exists>k. infinite {n. f n = k}"
       
  2430     proof (induct)
       
  2431       case empty thus ?case by simp
       
  2432     next
       
  2433       case (insert x A)
       
  2434      show ?case
       
  2435       proof (cases "finite {n. f n = x}")
       
  2436         case True
       
  2437         with `infinite {n. f n \<in> insert x A}`
       
  2438         have "infinite {n. f n \<in> A}" by simp
       
  2439         thus "\<exists>k. infinite {n. f n = k}" by (rule insert)
       
  2440       next
       
  2441         case False thus "\<exists>k. infinite {n. f n = k}" ..
       
  2442       qed
       
  2443     qed
       
  2444   } note H = this
       
  2445   from assms show "\<exists>k. infinite {n. f n = k}"
       
  2446     by (rule H) simp
       
  2447 qed
       
  2448 
       
  2449 lemma sequence_infinite_lemma:
       
  2450   fixes f :: "nat \<Rightarrow> 'a::t1_space"
       
  2451   assumes "\<forall>n. f n \<noteq> l" and "(f ---> l) sequentially"
       
  2452   shows "infinite (range f)"
       
  2453 proof
       
  2454   assume "finite (range f)"
       
  2455   hence "closed (range f)" by (rule finite_imp_closed)
       
  2456   hence "open (- range f)" by (rule open_Compl)
       
  2457   from assms(1) have "l \<in> - range f" by auto
       
  2458   from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"
       
  2459     using `open (- range f)` `l \<in> - range f` by (rule topological_tendstoD)
       
  2460   thus False unfolding eventually_sequentially by auto
       
  2461 qed
       
  2462 
       
  2463 lemma closure_insert:
       
  2464   fixes x :: "'a::t1_space"
       
  2465   shows "closure (insert x s) = insert x (closure s)"
       
  2466 apply (rule closure_unique)
       
  2467 apply (rule insert_mono [OF closure_subset])
       
  2468 apply (rule closed_insert [OF closed_closure])
       
  2469 apply (simp add: closure_minimal)
       
  2470 done
       
  2471 
       
  2472 lemma islimpt_insert:
       
  2473   fixes x :: "'a::t1_space"
       
  2474   shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"
       
  2475 proof
       
  2476   assume *: "x islimpt (insert a s)"
       
  2477   show "x islimpt s"
       
  2478   proof (rule islimptI)
       
  2479     fix t assume t: "x \<in> t" "open t"
       
  2480     show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"
       
  2481     proof (cases "x = a")
       
  2482       case True
       
  2483       obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"
       
  2484         using * t by (rule islimptE)
       
  2485       with `x = a` show ?thesis by auto
       
  2486     next
       
  2487       case False
       
  2488       with t have t': "x \<in> t - {a}" "open (t - {a})"
       
  2489         by (simp_all add: open_Diff)
       
  2490       obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"
       
  2491         using * t' by (rule islimptE)
       
  2492       thus ?thesis by auto
       
  2493     qed
       
  2494   qed
       
  2495 next
       
  2496   assume "x islimpt s" thus "x islimpt (insert a s)"
       
  2497     by (rule islimpt_subset) auto
       
  2498 qed
       
  2499 
       
  2500 lemma islimpt_union_finite:
       
  2501   fixes x :: "'a::t1_space"
       
  2502   shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"
       
  2503 by (induct set: finite, simp_all add: islimpt_insert)
       
  2504  
       
  2505 lemma sequence_unique_limpt:
       
  2506   fixes f :: "nat \<Rightarrow> 'a::t2_space"
       
  2507   assumes "(f ---> l) sequentially" and "l' islimpt (range f)"
       
  2508   shows "l' = l"
       
  2509 proof (rule ccontr)
       
  2510   assume "l' \<noteq> l"
       
  2511   obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"
       
  2512     using hausdorff [OF `l' \<noteq> l`] by auto
       
  2513   have "eventually (\<lambda>n. f n \<in> t) sequentially"
       
  2514     using assms(1) `open t` `l \<in> t` by (rule topological_tendstoD)
       
  2515   then obtain N where "\<forall>n\<ge>N. f n \<in> t"
       
  2516     unfolding eventually_sequentially by auto
       
  2517 
       
  2518   have "UNIV = {..<N} \<union> {N..}" by auto
       
  2519   hence "l' islimpt (f ` ({..<N} \<union> {N..}))" using assms(2) by simp
       
  2520   hence "l' islimpt (f ` {..<N} \<union> f ` {N..})" by (simp add: image_Un)
       
  2521   hence "l' islimpt (f ` {N..})" by (simp add: islimpt_union_finite)
       
  2522   then obtain y where "y \<in> f ` {N..}" "y \<in> s" "y \<noteq> l'"
       
  2523     using `l' \<in> s` `open s` by (rule islimptE)
       
  2524   then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'" by auto
       
  2525   with `\<forall>n\<ge>N. f n \<in> t` have "f n \<in> s \<inter> t" by simp
       
  2526   with `s \<inter> t = {}` show False by simp
       
  2527 qed
       
  2528 
       
  2529 lemma bolzano_weierstrass_imp_closed:
       
  2530   fixes s :: "'a::{first_countable_topology, t2_space} set"
       
  2531   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
       
  2532   shows "closed s"
       
  2533 proof-
       
  2534   { fix x l assume as: "\<forall>n::nat. x n \<in> s" "(x ---> l) sequentially"
       
  2535     hence "l \<in> s"
       
  2536     proof(cases "\<forall>n. x n \<noteq> l")
       
  2537       case False thus "l\<in>s" using as(1) by auto
       
  2538     next
       
  2539       case True note cas = this
       
  2540       with as(2) have "infinite (range x)" using sequence_infinite_lemma[of x l] by auto
       
  2541       then obtain l' where "l'\<in>s" "l' islimpt (range x)" using assms[THEN spec[where x="range x"]] as(1) by auto
       
  2542       thus "l\<in>s" using sequence_unique_limpt[of x l l'] using as cas by auto
       
  2543     qed  }
       
  2544   thus ?thesis unfolding closed_sequential_limits by fast
       
  2545 qed
       
  2546 
       
  2547 lemma compact_imp_closed:
       
  2548   fixes s :: "'a::{first_countable_topology, t2_space} set"
       
  2549   shows "compact s \<Longrightarrow> closed s"
       
  2550 proof -
       
  2551   assume "compact s"
       
  2552   hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"
       
  2553     using heine_borel_imp_bolzano_weierstrass[of s] by auto
       
  2554   thus "closed s"
       
  2555     by (rule bolzano_weierstrass_imp_closed)
       
  2556 qed
       
  2557 
       
  2558 text{* In particular, some common special cases. *}
       
  2559 
       
  2560 lemma compact_empty[simp]:
       
  2561  "compact {}"
       
  2562   unfolding compact_eq_heine_borel
       
  2563   by auto
       
  2564 
       
  2565 lemma compact_union [intro]:
       
  2566   assumes "compact s" "compact t" shows " compact (s \<union> t)"
       
  2567   unfolding compact_eq_heine_borel
       
  2568 proof (intro allI impI)
       
  2569   fix f assume *: "Ball f open \<and> s \<union> t \<subseteq> \<Union>f"
       
  2570   from * `compact s` obtain s' where "s' \<subseteq> f \<and> finite s' \<and> s \<subseteq> \<Union>s'"
       
  2571     unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f]) metis
       
  2572   moreover from * `compact t` obtain t' where "t' \<subseteq> f \<and> finite t' \<and> t \<subseteq> \<Union>t'"
       
  2573     unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f]) metis
       
  2574   ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<union> t \<subseteq> \<Union>f'"
       
  2575     by (auto intro!: exI[of _ "s' \<union> t'"])
       
  2576 qed
       
  2577 
       
  2578 lemma compact_Union [intro]: "finite S \<Longrightarrow> (\<And>T. T \<in> S \<Longrightarrow> compact T) \<Longrightarrow> compact (\<Union>S)"
       
  2579   by (induct set: finite) auto
       
  2580 
       
  2581 lemma compact_UN [intro]:
       
  2582   "finite A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> compact (B x)) \<Longrightarrow> compact (\<Union>x\<in>A. B x)"
       
  2583   unfolding SUP_def by (rule compact_Union) auto
       
  2584 
       
  2585 lemma compact_inter_closed [intro]:
       
  2586   assumes "compact s" and "closed t"
       
  2587   shows "compact (s \<inter> t)"
       
  2588   unfolding compact_eq_heine_borel
       
  2589 proof safe
       
  2590   fix C assume C: "\<forall>c\<in>C. open c" and cover: "s \<inter> t \<subseteq> \<Union>C"
       
  2591   from C `closed t` have "\<forall>c\<in>C \<union> {-t}. open c" by auto
       
  2592   moreover from cover have "s \<subseteq> \<Union>(C \<union> {-t})" by auto
       
  2593   ultimately have "\<exists>D\<subseteq>C \<union> {-t}. finite D \<and> s \<subseteq> \<Union>D"
       
  2594     using `compact s` unfolding compact_eq_heine_borel by auto
       
  2595   then guess D ..
       
  2596   then show "\<exists>D\<subseteq>C. finite D \<and> s \<inter> t \<subseteq> \<Union>D"
       
  2597     by (intro exI[of _ "D - {-t}"]) auto
       
  2598 qed
       
  2599 
       
  2600 lemma closed_inter_compact [intro]:
       
  2601   assumes "closed s" and "compact t"
       
  2602   shows "compact (s \<inter> t)"
       
  2603   using compact_inter_closed [of t s] assms
       
  2604   by (simp add: Int_commute)
       
  2605 
       
  2606 lemma compact_inter [intro]:
       
  2607   fixes s t :: "'a :: {t2_space, first_countable_topology} set"
       
  2608   assumes "compact s" and "compact t"
       
  2609   shows "compact (s \<inter> t)"
       
  2610   using assms by (intro compact_inter_closed compact_imp_closed)
       
  2611 
       
  2612 lemma compact_sing [simp]: "compact {a}"
       
  2613   unfolding compact_eq_heine_borel by auto
       
  2614 
       
  2615 lemma compact_insert [simp]:
       
  2616   assumes "compact s" shows "compact (insert x s)"
       
  2617 proof -
       
  2618   have "compact ({x} \<union> s)"
       
  2619     using compact_sing assms by (rule compact_union)
       
  2620   thus ?thesis by simp
       
  2621 qed
       
  2622 
       
  2623 lemma finite_imp_compact:
       
  2624   shows "finite s \<Longrightarrow> compact s"
       
  2625   by (induct set: finite) simp_all
       
  2626 
       
  2627 lemma open_delete:
       
  2628   fixes s :: "'a::t1_space set"
       
  2629   shows "open s \<Longrightarrow> open (s - {x})"
       
  2630   by (simp add: open_Diff)
       
  2631 
       
  2632 text{* Finite intersection property *}
       
  2633 
       
  2634 lemma inj_setminus: "inj_on uminus (A::'a set set)"
       
  2635   by (auto simp: inj_on_def)
       
  2636 
       
  2637 lemma compact_fip:
       
  2638   "compact U \<longleftrightarrow>
       
  2639     (\<forall>A. (\<forall>a\<in>A. closed a) \<longrightarrow> (\<forall>B \<subseteq> A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}) \<longrightarrow> U \<inter> \<Inter>A \<noteq> {})"
       
  2640   (is "_ \<longleftrightarrow> ?R")
       
  2641 proof (safe intro!: compact_eq_heine_borel[THEN iffD2])
       
  2642   fix A assume "compact U" and A: "\<forall>a\<in>A. closed a" "U \<inter> \<Inter>A = {}"
       
  2643     and fi: "\<forall>B \<subseteq> A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}"
       
  2644   from A have "(\<forall>a\<in>uminus`A. open a) \<and> U \<subseteq> \<Union>uminus`A"
       
  2645     by auto
       
  2646   with `compact U` obtain B where "B \<subseteq> A" "finite (uminus`B)" "U \<subseteq> \<Union>(uminus`B)"
       
  2647     unfolding compact_eq_heine_borel by (metis subset_image_iff)
       
  2648   with fi[THEN spec, of B] show False
       
  2649     by (auto dest: finite_imageD intro: inj_setminus)
       
  2650 next
       
  2651   fix A assume ?R and cover: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
       
  2652   from cover have "U \<inter> \<Inter>(uminus`A) = {}" "\<forall>a\<in>uminus`A. closed a"
       
  2653     by auto
       
  2654   with `?R` obtain B where "B \<subseteq> A" "finite (uminus`B)" "U \<inter> \<Inter>uminus`B = {}"
       
  2655     by (metis subset_image_iff)
       
  2656   then show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
       
  2657     by  (auto intro!: exI[of _ B] inj_setminus dest: finite_imageD)
       
  2658 qed
       
  2659 
       
  2660 lemma compact_imp_fip:
       
  2661   "compact s \<Longrightarrow> \<forall>t \<in> f. closed t \<Longrightarrow> \<forall>f'. finite f' \<and> f' \<subseteq> f \<longrightarrow> (s \<inter> (\<Inter> f') \<noteq> {}) \<Longrightarrow>
       
  2662     s \<inter> (\<Inter> f) \<noteq> {}"
       
  2663   unfolding compact_fip by auto
       
  2664 
       
  2665 text{*Compactness expressed with filters*}
       
  2666 
       
  2667 definition "filter_from_subbase B = Abs_filter (\<lambda>P. \<exists>X \<subseteq> B. finite X \<and> Inf X \<le> P)"
       
  2668 
       
  2669 lemma eventually_filter_from_subbase:
       
  2670   "eventually P (filter_from_subbase B) \<longleftrightarrow> (\<exists>X \<subseteq> B. finite X \<and> Inf X \<le> P)"
       
  2671     (is "_ \<longleftrightarrow> ?R P")
       
  2672   unfolding filter_from_subbase_def
       
  2673 proof (rule eventually_Abs_filter is_filter.intro)+
       
  2674   show "?R (\<lambda>x. True)"
       
  2675     by (rule exI[of _ "{}"]) (simp add: le_fun_def)
       
  2676 next
       
  2677   fix P Q assume "?R P" then guess X ..
       
  2678   moreover assume "?R Q" then guess Y ..
       
  2679   ultimately show "?R (\<lambda>x. P x \<and> Q x)"
       
  2680     by (intro exI[of _ "X \<union> Y"]) auto
       
  2681 next
       
  2682   fix P Q
       
  2683   assume "?R P" then guess X ..
       
  2684   moreover assume "\<forall>x. P x \<longrightarrow> Q x"
       
  2685   ultimately show "?R Q"
       
  2686     by (intro exI[of _ X]) auto
       
  2687 qed
       
  2688 
       
  2689 lemma eventually_filter_from_subbaseI: "P \<in> B \<Longrightarrow> eventually P (filter_from_subbase B)"
       
  2690   by (subst eventually_filter_from_subbase) (auto intro!: exI[of _ "{P}"])
       
  2691 
       
  2692 lemma filter_from_subbase_not_bot:
       
  2693   "\<forall>X \<subseteq> B. finite X \<longrightarrow> Inf X \<noteq> bot \<Longrightarrow> filter_from_subbase B \<noteq> bot"
       
  2694   unfolding trivial_limit_def eventually_filter_from_subbase by auto
       
  2695 
       
  2696 lemma closure_iff_nhds_not_empty:
       
  2697   "x \<in> closure X \<longleftrightarrow> (\<forall>A. \<forall>S\<subseteq>A. open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {})"
       
  2698 proof safe
       
  2699   assume x: "x \<in> closure X"
       
  2700   fix S A assume "open S" "x \<in> S" "X \<inter> A = {}" "S \<subseteq> A"
       
  2701   then have "x \<notin> closure (-S)" 
       
  2702     by (auto simp: closure_complement subset_eq[symmetric] intro: interiorI)
       
  2703   with x have "x \<in> closure X - closure (-S)"
       
  2704     by auto
       
  2705   also have "\<dots> \<subseteq> closure (X \<inter> S)"
       
  2706     using `open S` open_inter_closure_subset[of S X] by (simp add: closed_Compl ac_simps)
       
  2707   finally have "X \<inter> S \<noteq> {}" by auto
       
  2708   then show False using `X \<inter> A = {}` `S \<subseteq> A` by auto
       
  2709 next
       
  2710   assume "\<forall>A S. S \<subseteq> A \<longrightarrow> open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {}"
       
  2711   from this[THEN spec, of "- X", THEN spec, of "- closure X"]
       
  2712   show "x \<in> closure X"
       
  2713     by (simp add: closure_subset open_Compl)
       
  2714 qed
       
  2715 
       
  2716 lemma compact_filter:
       
  2717   "compact U \<longleftrightarrow> (\<forall>F. F \<noteq> bot \<longrightarrow> eventually (\<lambda>x. x \<in> U) F \<longrightarrow> (\<exists>x\<in>U. inf (nhds x) F \<noteq> bot))"
       
  2718 proof (intro allI iffI impI compact_fip[THEN iffD2] notI)
       
  2719   fix F assume "compact U" and F: "F \<noteq> bot" "eventually (\<lambda>x. x \<in> U) F"
       
  2720   from F have "U \<noteq> {}"
       
  2721     by (auto simp: eventually_False)
       
  2722 
       
  2723   def Z \<equiv> "closure ` {A. eventually (\<lambda>x. x \<in> A) F}"
       
  2724   then have "\<forall>z\<in>Z. closed z"
       
  2725     by auto
       
  2726   moreover 
       
  2727   have ev_Z: "\<And>z. z \<in> Z \<Longrightarrow> eventually (\<lambda>x. x \<in> z) F"
       
  2728     unfolding Z_def by (auto elim: eventually_elim1 intro: set_mp[OF closure_subset])
       
  2729   have "(\<forall>B \<subseteq> Z. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {})"
       
  2730   proof (intro allI impI)
       
  2731     fix B assume "finite B" "B \<subseteq> Z"
       
  2732     with `finite B` ev_Z have "eventually (\<lambda>x. \<forall>b\<in>B. x \<in> b) F"
       
  2733       by (auto intro!: eventually_Ball_finite)
       
  2734     with F(2) have "eventually (\<lambda>x. x \<in> U \<inter> (\<Inter>B)) F"
       
  2735       by eventually_elim auto
       
  2736     with F show "U \<inter> \<Inter>B \<noteq> {}"
       
  2737       by (intro notI) (simp add: eventually_False)
       
  2738   qed
       
  2739   ultimately have "U \<inter> \<Inter>Z \<noteq> {}"
       
  2740     using `compact U` unfolding compact_fip by blast
       
  2741   then obtain x where "x \<in> U" and x: "\<And>z. z \<in> Z \<Longrightarrow> x \<in> z" by auto
       
  2742 
       
  2743   have "\<And>P. eventually P (inf (nhds x) F) \<Longrightarrow> P \<noteq> bot"
       
  2744     unfolding eventually_inf eventually_nhds
       
  2745   proof safe
       
  2746     fix P Q R S
       
  2747     assume "eventually R F" "open S" "x \<in> S"
       
  2748     with open_inter_closure_eq_empty[of S "{x. R x}"] x[of "closure {x. R x}"]
       
  2749     have "S \<inter> {x. R x} \<noteq> {}" by (auto simp: Z_def)
       
  2750     moreover assume "Ball S Q" "\<forall>x. Q x \<and> R x \<longrightarrow> bot x"
       
  2751     ultimately show False by (auto simp: set_eq_iff)
       
  2752   qed
       
  2753   with `x \<in> U` show "\<exists>x\<in>U. inf (nhds x) F \<noteq> bot"
       
  2754     by (metis eventually_bot)
       
  2755 next
       
  2756   fix A assume A: "\<forall>a\<in>A. closed a" "\<forall>B\<subseteq>A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}" "U \<inter> \<Inter>A = {}"
       
  2757 
       
  2758   def P' \<equiv> "(\<lambda>a (x::'a). x \<in> a)"
       
  2759   then have inj_P': "\<And>A. inj_on P' A"
       
  2760     by (auto intro!: inj_onI simp: fun_eq_iff)
       
  2761   def F \<equiv> "filter_from_subbase (P' ` insert U A)"
       
  2762   have "F \<noteq> bot"
       
  2763     unfolding F_def
       
  2764   proof (safe intro!: filter_from_subbase_not_bot)
       
  2765     fix X assume "X \<subseteq> P' ` insert U A" "finite X" "Inf X = bot"
       
  2766     then obtain B where "B \<subseteq> insert U A" "finite B" and B: "Inf (P' ` B) = bot"
       
  2767       unfolding subset_image_iff by (auto intro: inj_P' finite_imageD)
       
  2768     with A(2)[THEN spec, of "B - {U}"] have "U \<inter> \<Inter>(B - {U}) \<noteq> {}" by auto
       
  2769     with B show False by (auto simp: P'_def fun_eq_iff)
       
  2770   qed
       
  2771   moreover have "eventually (\<lambda>x. x \<in> U) F"
       
  2772     unfolding F_def by (rule eventually_filter_from_subbaseI) (auto simp: P'_def)
       
  2773   moreover assume "\<forall>F. F \<noteq> bot \<longrightarrow> eventually (\<lambda>x. x \<in> U) F \<longrightarrow> (\<exists>x\<in>U. inf (nhds x) F \<noteq> bot)"
       
  2774   ultimately obtain x where "x \<in> U" and x: "inf (nhds x) F \<noteq> bot"
       
  2775     by auto
       
  2776 
       
  2777   { fix V assume "V \<in> A"
       
  2778     then have V: "eventually (\<lambda>x. x \<in> V) F"
       
  2779       by (auto simp add: F_def image_iff P'_def intro!: eventually_filter_from_subbaseI)
       
  2780     have "x \<in> closure V"
       
  2781       unfolding closure_iff_nhds_not_empty
       
  2782     proof (intro impI allI)
       
  2783       fix S A assume "open S" "x \<in> S" "S \<subseteq> A"
       
  2784       then have "eventually (\<lambda>x. x \<in> A) (nhds x)" by (auto simp: eventually_nhds)
       
  2785       with V have "eventually (\<lambda>x. x \<in> V \<inter> A) (inf (nhds x) F)"
       
  2786         by (auto simp: eventually_inf)
       
  2787       with x show "V \<inter> A \<noteq> {}"
       
  2788         by (auto simp del: Int_iff simp add: trivial_limit_def)
       
  2789     qed
       
  2790     then have "x \<in> V"
       
  2791       using `V \<in> A` A(1) by simp }
       
  2792   with `x\<in>U` have "x \<in> U \<inter> \<Inter>A" by auto
       
  2793   with `U \<inter> \<Inter>A = {}` show False by auto
       
  2794 qed
       
  2795 
       
  2796 lemma countable_compact:
       
  2797   fixes U :: "'a :: second_countable_topology set"
       
  2798   shows "compact U \<longleftrightarrow>
       
  2799     (\<forall>A. countable A \<longrightarrow> (\<forall>a\<in>A. open a) \<longrightarrow> U \<subseteq> \<Union>A \<longrightarrow> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T))"
       
  2800 proof (safe intro!: compact_eq_heine_borel[THEN iffD2])
       
  2801   fix A assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
       
  2802   assume *: "\<forall>A. countable A \<longrightarrow> (\<forall>a\<in>A. open a) \<longrightarrow> U \<subseteq> \<Union>A \<longrightarrow> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"
       
  2803   def B \<equiv> "SOME B::'a set set. countable B \<and> topological_basis B"
       
  2804   then have B: "countable B" "topological_basis B"
       
  2805     by (auto simp: countable_basis is_basis)
       
  2806 
       
  2807   moreover def C \<equiv> "{b\<in>B. \<exists>a\<in>A. b \<subseteq> a}"
       
  2808   ultimately have "countable C" "\<forall>a\<in>C. open a"
       
  2809     unfolding C_def by (auto simp: topological_basis_open)
       
  2810   moreover
       
  2811   have "\<Union>A \<subseteq> \<Union>C"
       
  2812   proof safe
       
  2813     fix x a assume "x \<in> a" "a \<in> A"
       
  2814     with topological_basisE[of B a x] B A
       
  2815     obtain b where "x \<in> b" "b \<in> B" "b \<subseteq> a" by metis
       
  2816     with `a \<in> A` show "x \<in> \<Union>C" unfolding C_def by auto
       
  2817   qed
       
  2818   then have "U \<subseteq> \<Union>C" using `U \<subseteq> \<Union>A` by auto
       
  2819   ultimately obtain T where "T\<subseteq>C" "finite T" "U \<subseteq> \<Union>T"
       
  2820     using * by metis
       
  2821   moreover then have "\<forall>t\<in>T. \<exists>a\<in>A. t \<subseteq> a"
       
  2822     by (auto simp: C_def)
       
  2823   then guess f unfolding bchoice_iff Bex_def ..
       
  2824   ultimately show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
       
  2825     unfolding C_def by (intro exI[of _ "f`T"]) fastforce
       
  2826 qed (auto simp: compact_eq_heine_borel)
  2349 
  2827 
  2350 subsubsection{* Sequential compactness *}
  2828 subsubsection{* Sequential compactness *}
  2351 
  2829 
  2352 definition
  2830 definition
  2353   compact :: "'a::metric_space set \<Rightarrow> bool" where (* TODO: generalize *)
  2831   seq_compact :: "'a::topological_space set \<Rightarrow> bool" where
  2354   "compact S \<longleftrightarrow>
  2832   "seq_compact S \<longleftrightarrow>
  2355    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>
  2833    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>
  2356        (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially))"
  2834        (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially))"
  2357 
  2835 
  2358 lemma compactI:
  2836 lemma seq_compact_imp_compact:
       
  2837   fixes U :: "'a :: second_countable_topology set"
       
  2838   assumes "seq_compact U"
       
  2839   shows "compact U"
       
  2840   unfolding countable_compact
       
  2841 proof safe
       
  2842   fix A assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A" "countable A"
       
  2843   have subseq: "\<And>X. range X \<subseteq> U \<Longrightarrow> \<exists>r x. x \<in> U \<and> subseq r \<and> (X \<circ> r) ----> x"
       
  2844     using `seq_compact U` by (fastforce simp: seq_compact_def subset_eq)
       
  2845   show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
       
  2846   proof cases
       
  2847     assume "finite A" with A show ?thesis by auto
       
  2848   next
       
  2849     assume "infinite A"
       
  2850     then have "A \<noteq> {}" by auto
       
  2851     show ?thesis
       
  2852     proof (rule ccontr)
       
  2853       assume "\<not> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"
       
  2854       then have "\<forall>T. \<exists>x. T \<subseteq> A \<and> finite T \<longrightarrow> (x \<in> U - \<Union>T)" by auto
       
  2855       then obtain X' where T: "\<And>T. T \<subseteq> A \<Longrightarrow> finite T \<Longrightarrow> X' T \<in> U - \<Union>T" by metis
       
  2856       def X \<equiv> "\<lambda>n. X' (from_nat_into A ` {.. n})"
       
  2857       have X: "\<And>n. X n \<in> U - (\<Union>i\<le>n. from_nat_into A i)"
       
  2858         using `A \<noteq> {}` unfolding X_def SUP_def by (intro T) (auto intro: from_nat_into)
       
  2859       then have "range X \<subseteq> U" by auto
       
  2860       with subseq[of X] obtain r x where "x \<in> U" and r: "subseq r" "(X \<circ> r) ----> x" by auto
       
  2861       from `x\<in>U` `U \<subseteq> \<Union>A` from_nat_into_surj[OF `countable A`]
       
  2862       obtain n where "x \<in> from_nat_into A n" by auto
       
  2863       with r(2) A(1) from_nat_into[OF `A \<noteq> {}`, of n]
       
  2864       have "eventually (\<lambda>i. X (r i) \<in> from_nat_into A n) sequentially"
       
  2865         unfolding tendsto_def by (auto simp: comp_def)
       
  2866       then obtain N where "\<And>i. N \<le> i \<Longrightarrow> X (r i) \<in> from_nat_into A n"
       
  2867         by (auto simp: eventually_sequentially)
       
  2868       moreover from X have "\<And>i. n \<le> r i \<Longrightarrow> X (r i) \<notin> from_nat_into A n"
       
  2869         by auto
       
  2870       moreover from `subseq r`[THEN seq_suble, of "max n N"] have "\<exists>i. n \<le> r i \<and> N \<le> i"
       
  2871         by (auto intro!: exI[of _ "max n N"])
       
  2872       ultimately show False
       
  2873         by auto
       
  2874     qed
       
  2875   qed
       
  2876 qed
       
  2877 
       
  2878 lemma compact_imp_seq_compact:
       
  2879   fixes U :: "'a :: first_countable_topology set"
       
  2880   assumes "compact U" shows "seq_compact U"
       
  2881   unfolding seq_compact_def
       
  2882 proof safe
       
  2883   fix X :: "nat \<Rightarrow> 'a" assume "\<forall>n. X n \<in> U"
       
  2884   then have "eventually (\<lambda>x. x \<in> U) (filtermap X sequentially)"
       
  2885     by (auto simp: eventually_filtermap)
       
  2886   moreover have "filtermap X sequentially \<noteq> bot"
       
  2887     by (simp add: trivial_limit_def eventually_filtermap)
       
  2888   ultimately obtain x where "x \<in> U" and x: "inf (nhds x) (filtermap X sequentially) \<noteq> bot" (is "?F \<noteq> _")
       
  2889     using `compact U` by (auto simp: compact_filter)
       
  2890 
       
  2891   from countable_basis_at_decseq[of x] guess A . note A = this
       
  2892   def s \<equiv> "\<lambda>n i. SOME j. i < j \<and> X j \<in> A (Suc n)"
       
  2893   { fix n i
       
  2894     have "\<exists>a. i < a \<and> X a \<in> A (Suc n)"
       
  2895     proof (rule ccontr)
       
  2896       assume "\<not> (\<exists>a>i. X a \<in> A (Suc n))"
       
  2897       then have "\<And>a. Suc i \<le> a \<Longrightarrow> X a \<notin> A (Suc n)" by auto
       
  2898       then have "eventually (\<lambda>x. x \<notin> A (Suc n)) (filtermap X sequentially)"
       
  2899         by (auto simp: eventually_filtermap eventually_sequentially)
       
  2900       moreover have "eventually (\<lambda>x. x \<in> A (Suc n)) (nhds x)"
       
  2901         using A(1,2)[of "Suc n"] by (auto simp: eventually_nhds)
       
  2902       ultimately have "eventually (\<lambda>x. False) ?F"
       
  2903         by (auto simp add: eventually_inf)
       
  2904       with x show False
       
  2905         by (simp add: eventually_False)
       
  2906     qed
       
  2907     then have "i < s n i" "X (s n i) \<in> A (Suc n)"
       
  2908       unfolding s_def by (auto intro: someI2_ex) }
       
  2909   note s = this
       
  2910   def r \<equiv> "nat_rec (s 0 0) s"
       
  2911   have "subseq r"
       
  2912     by (auto simp: r_def s subseq_Suc_iff)
       
  2913   moreover
       
  2914   have "(\<lambda>n. X (r n)) ----> x"
       
  2915   proof (rule topological_tendstoI)
       
  2916     fix S assume "open S" "x \<in> S"
       
  2917     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially" by auto
       
  2918     moreover
       
  2919     { fix i assume "Suc 0 \<le> i" then have "X (r i) \<in> A i"
       
  2920         by (cases i) (simp_all add: r_def s) }
       
  2921     then have "eventually (\<lambda>i. X (r i) \<in> A i) sequentially" by (auto simp: eventually_sequentially)
       
  2922     ultimately show "eventually (\<lambda>i. X (r i) \<in> S) sequentially"
       
  2923       by eventually_elim auto
       
  2924   qed
       
  2925   ultimately show "\<exists>x \<in> U. \<exists>r. subseq r \<and> (X \<circ> r) ----> x"
       
  2926     using `x \<in> U` by (auto simp: convergent_def comp_def)
       
  2927 qed
       
  2928 
       
  2929 lemma seq_compact_eq_compact:
       
  2930   fixes U :: "'a :: second_countable_topology set"
       
  2931   shows "seq_compact U \<longleftrightarrow> compact U"
       
  2932   using compact_imp_seq_compact seq_compact_imp_compact by blast
       
  2933 
       
  2934 lemma seq_compactI:
  2359   assumes "\<And>f. \<forall>n. f n \<in> S \<Longrightarrow> \<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially"
  2935   assumes "\<And>f. \<forall>n. f n \<in> S \<Longrightarrow> \<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially"
  2360   shows "compact S"
  2936   shows "seq_compact S"
  2361   unfolding compact_def using assms by fast
  2937   unfolding seq_compact_def using assms by fast
  2362 
  2938 
  2363 lemma compactE:
  2939 lemma seq_compactE:
  2364   assumes "compact S" "\<forall>n. f n \<in> S"
  2940   assumes "seq_compact S" "\<forall>n. f n \<in> S"
  2365   obtains l r where "l \<in> S" "subseq r" "((f \<circ> r) ---> l) sequentially"
  2941   obtains l r where "l \<in> S" "subseq r" "((f \<circ> r) ---> l) sequentially"
  2366   using assms unfolding compact_def by fast
  2942   using assms unfolding seq_compact_def by fast
       
  2943 
       
  2944 lemma bolzano_weierstrass_imp_seq_compact:
       
  2945   fixes s :: "'a::{t1_space, first_countable_topology} set"
       
  2946   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
       
  2947   shows "seq_compact s"
       
  2948 proof -
       
  2949   { fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
       
  2950     have "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  2951     proof (cases "finite (range f)")
       
  2952       case True
       
  2953       hence "\<exists>l. infinite {n. f n = l}"
       
  2954         by (rule finite_range_imp_infinite_repeats)
       
  2955       then obtain l where "infinite {n. f n = l}" ..
       
  2956       hence "\<exists>r. subseq r \<and> (\<forall>n. r n \<in> {n. f n = l})"
       
  2957         by (rule infinite_enumerate)
       
  2958       then obtain r where "subseq r" and fr: "\<forall>n. f (r n) = l" by auto
       
  2959       hence "subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  2960         unfolding o_def by (simp add: fr tendsto_const)
       
  2961       hence "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  2962         by - (rule exI)
       
  2963       from f have "\<forall>n. f (r n) \<in> s" by simp
       
  2964       hence "l \<in> s" by (simp add: fr)
       
  2965       thus "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  2966         by (rule rev_bexI) fact
       
  2967     next
       
  2968       case False
       
  2969       with f assms have "\<exists>x\<in>s. x islimpt (range f)" by auto
       
  2970       then obtain l where "l \<in> s" "l islimpt (range f)" ..
       
  2971       have "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  2972         using `l islimpt (range f)`
       
  2973         by (rule islimpt_range_imp_convergent_subsequence)
       
  2974       with `l \<in> s` show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" ..
       
  2975     qed
       
  2976   }
       
  2977   thus ?thesis unfolding seq_compact_def by auto
       
  2978 qed
  2367 
  2979 
  2368 text {*
  2980 text {*
  2369   A metric space (or topological vector space) is said to have the
  2981   A metric space (or topological vector space) is said to have the
  2370   Heine-Borel property if every closed and bounded subset is compact.
  2982   Heine-Borel property if every closed and bounded subset is compact.
  2371 *}
  2983 *}
  2373 class heine_borel = metric_space +
  2985 class heine_borel = metric_space +
  2374   assumes bounded_imp_convergent_subsequence:
  2986   assumes bounded_imp_convergent_subsequence:
  2375     "bounded s \<Longrightarrow> \<forall>n. f n \<in> s
  2987     "bounded s \<Longrightarrow> \<forall>n. f n \<in> s
  2376       \<Longrightarrow> \<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  2988       \<Longrightarrow> \<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  2377 
  2989 
  2378 lemma bounded_closed_imp_compact:
  2990 lemma bounded_closed_imp_seq_compact:
  2379   fixes s::"'a::heine_borel set"
  2991   fixes s::"'a::heine_borel set"
  2380   assumes "bounded s" and "closed s" shows "compact s"
  2992   assumes "bounded s" and "closed s" shows "seq_compact s"
  2381 proof (unfold compact_def, clarify)
  2993 proof (unfold seq_compact_def, clarify)
  2382   fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
  2994   fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
  2383   obtain l r where r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
  2995   obtain l r where r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
  2384     using bounded_imp_convergent_subsequence [OF `bounded s` `\<forall>n. f n \<in> s`] by auto
  2996     using bounded_imp_convergent_subsequence [OF `bounded s` `\<forall>n. f n \<in> s`] by auto
  2385   from f have fr: "\<forall>n. (f \<circ> r) n \<in> s" by simp
  2997   from f have fr: "\<forall>n. (f \<circ> r) n \<in> s" by simp
  2386   have "l \<in> s" using `closed s` fr l
  2998   have "l \<in> s" using `closed s` fr l
  2658     unfolding bounded_any_center [where a="s N"]
  3270     unfolding bounded_any_center [where a="s N"]
  2659     apply(rule_tac x="max a 1" in exI) apply auto
  3271     apply(rule_tac x="max a 1" in exI) apply auto
  2660     apply(erule_tac x=y in allE) apply(erule_tac x=y in ballE) by auto
  3272     apply(erule_tac x=y in allE) apply(erule_tac x=y in ballE) by auto
  2661 qed
  3273 qed
  2662 
  3274 
  2663 lemma compact_imp_complete: assumes "compact s" shows "complete s"
  3275 lemma seq_compact_imp_complete: assumes "seq_compact s" shows "complete s"
  2664 proof-
  3276 proof-
  2665   { fix f assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"
  3277   { fix f assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"
  2666     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
  3278     from as(1) obtain l r where lr: "l\<in>s" "subseq r" "((f \<circ> r) ---> l) sequentially" using assms unfolding seq_compact_def by blast
  2667 
  3279 
  2668     note lr' = subseq_bigger [OF lr(2)]
  3280     note lr' = subseq_bigger [OF lr(2)]
  2669 
  3281 
  2670     { fix e::real assume "e>0"
  3282     { fix e::real assume "e>0"
  2671       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
  3283       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
  2683 instance heine_borel < complete_space
  3295 instance heine_borel < complete_space
  2684 proof
  3296 proof
  2685   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
  3297   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
  2686   hence "bounded (range f)"
  3298   hence "bounded (range f)"
  2687     by (rule cauchy_imp_bounded)
  3299     by (rule cauchy_imp_bounded)
  2688   hence "compact (closure (range f))"
  3300   hence "seq_compact (closure (range f))"
  2689     using bounded_closed_imp_compact [of "closure (range f)"] by auto
  3301     using bounded_closed_imp_seq_compact [of "closure (range f)"] by auto
  2690   hence "complete (closure (range f))"
  3302   hence "complete (closure (range f))"
  2691     by (rule compact_imp_complete)
  3303     by (rule seq_compact_imp_complete)
  2692   moreover have "\<forall>n. f n \<in> closure (range f)"
  3304   moreover have "\<forall>n. f n \<in> closure (range f)"
  2693     using closure_subset [of "range f"] by auto
  3305     using closure_subset [of "range f"] by auto
  2694   ultimately have "\<exists>l\<in>closure (range f). (f ---> l) sequentially"
  3306   ultimately have "\<exists>l\<in>closure (range f). (f ---> l) sequentially"
  2695     using `Cauchy f` unfolding complete_def by auto
  3307     using `Cauchy f` unfolding complete_def by auto
  2696   then show "convergent f"
  3308   then show "convergent f"
  2745 
  3357 
  2746 fun helper_1::"('a::metric_space set) \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> 'a" where
  3358 fun helper_1::"('a::metric_space set) \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> 'a" where
  2747   "helper_1 s e n = (SOME y::'a. y \<in> s \<and> (\<forall>m<n. \<not> (dist (helper_1 s e m) y < e)))"
  3359   "helper_1 s e n = (SOME y::'a. y \<in> s \<and> (\<forall>m<n. \<not> (dist (helper_1 s e m) y < e)))"
  2748 declare helper_1.simps[simp del]
  3360 declare helper_1.simps[simp del]
  2749 
  3361 
  2750 lemma compact_imp_totally_bounded:
  3362 lemma seq_compact_imp_totally_bounded:
  2751   assumes "compact s"
  3363   assumes "seq_compact s"
  2752   shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` k))"
  3364   shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` k))"
  2753 proof(rule, rule, rule ccontr)
  3365 proof(rule, rule, rule ccontr)
  2754   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)"
  3366   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)"
  2755   def x \<equiv> "helper_1 s e"
  3367   def x \<equiv> "helper_1 s e"
  2756   { fix n
  3368   { fix n
  2763       have "Q (x n)" unfolding x_def and helper_1.simps[of s e n]
  3375       have "Q (x n)" unfolding x_def and helper_1.simps[of s e n]
  2764         apply(rule someI2[where a=z]) unfolding x_def[symmetric] and Q_def using z by auto
  3376         apply(rule someI2[where a=z]) unfolding x_def[symmetric] and Q_def using z by auto
  2765       thus "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)" unfolding Q_def by auto
  3377       thus "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)" unfolding Q_def by auto
  2766     qed }
  3378     qed }
  2767   hence "\<forall>n::nat. x n \<in> s" and x:"\<forall>n. \<forall>m < n. \<not> (dist (x m) (x n) < e)" by blast+
  3379   hence "\<forall>n::nat. x n \<in> s" and x:"\<forall>n. \<forall>m < n. \<not> (dist (x m) (x n) < e)" by blast+
  2768   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
  3380   then obtain l r where "l\<in>s" and r:"subseq r" and "((x \<circ> r) ---> l) sequentially" using assms(1)[unfolded seq_compact_def, THEN spec[where x=x]] by auto
  2769   from this(3) have "Cauchy (x \<circ> r)" using convergent_imp_cauchy by auto
  3381   from this(3) have "Cauchy (x \<circ> r)" using convergent_imp_cauchy by auto
  2770   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
  3382   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
  2771   show False
  3383   show False
  2772     using N[THEN spec[where x=N], THEN spec[where x="N+1"]]
  3384     using N[THEN spec[where x=N], THEN spec[where x="N+1"]]
  2773     using r[unfolded subseq_def, THEN spec[where x=N], THEN spec[where x="N+1"]]
  3385     using r[unfolded subseq_def, THEN spec[where x=N], THEN spec[where x="N+1"]]
  2777 subsubsection{* Heine-Borel theorem *}
  3389 subsubsection{* Heine-Borel theorem *}
  2778 
  3390 
  2779 text {* Following Burkill \& Burkill vol. 2. *}
  3391 text {* Following Burkill \& Burkill vol. 2. *}
  2780 
  3392 
  2781 lemma heine_borel_lemma: fixes s::"'a::metric_space set"
  3393 lemma heine_borel_lemma: fixes s::"'a::metric_space set"
  2782   assumes "compact s"  "s \<subseteq> (\<Union> t)"  "\<forall>b \<in> t. open b"
  3394   assumes "seq_compact s"  "s \<subseteq> (\<Union> t)"  "\<forall>b \<in> t. open b"
  2783   shows "\<exists>e>0. \<forall>x \<in> s. \<exists>b \<in> t. ball x e \<subseteq> b"
  3395   shows "\<exists>e>0. \<forall>x \<in> s. \<exists>b \<in> t. ball x e \<subseteq> b"
  2784 proof(rule ccontr)
  3396 proof(rule ccontr)
  2785   assume "\<not> (\<exists>e>0. \<forall>x\<in>s. \<exists>b\<in>t. ball x e \<subseteq> b)"
  3397   assume "\<not> (\<exists>e>0. \<forall>x\<in>s. \<exists>b\<in>t. ball x e \<subseteq> b)"
  2786   hence cont:"\<forall>e>0. \<exists>x\<in>s. \<forall>xa\<in>t. \<not> (ball x e \<subseteq> xa)" by auto
  3398   hence cont:"\<forall>e>0. \<exists>x\<in>s. \<forall>xa\<in>t. \<not> (ball x e \<subseteq> xa)" by auto
  2787   { fix n::nat
  3399   { fix n::nat
  2790   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
  3402   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
  2791   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)"
  3403   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)"
  2792     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
  3404     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
  2793 
  3405 
  2794   then obtain l r where l:"l\<in>s" and r:"subseq r" and lr:"((f \<circ> r) ---> l) sequentially"
  3406   then obtain l r where l:"l\<in>s" and r:"subseq r" and lr:"((f \<circ> r) ---> l) sequentially"
  2795     using assms(1)[unfolded compact_def, THEN spec[where x=f]] by auto
  3407     using assms(1)[unfolded seq_compact_def, THEN spec[where x=f]] by auto
  2796 
  3408 
  2797   obtain b where "l\<in>b" "b\<in>t" using assms(2) and l by auto
  3409   obtain b where "l\<in>b" "b\<in>t" using assms(2) and l by auto
  2798   then obtain e where "e>0" and e:"\<forall>z. dist z l < e \<longrightarrow> z\<in>b"
  3410   then obtain e where "e>0" and e:"\<forall>z. dist z l < e \<longrightarrow> z\<in>b"
  2799     using assms(3)[THEN bspec[where x=b]] unfolding open_dist by auto
  3411     using assms(3)[THEN bspec[where x=b]] unfolding open_dist by auto
  2800 
  3412 
  2816   hence "dist y l < e" using y N2' using dist_triangle[of y l x]by (auto simp add:dist_commute)
  3428   hence "dist y l < e" using y N2' using dist_triangle[of y l x]by (auto simp add:dist_commute)
  2817 
  3429 
  2818   thus False using e and `y\<notin>b` by auto
  3430   thus False using e and `y\<notin>b` by auto
  2819 qed
  3431 qed
  2820 
  3432 
  2821 lemma compact_imp_heine_borel: "compact s ==> (\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f)
  3433 lemma seq_compact_imp_heine_borel:
  2822                \<longrightarrow> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f')))"
  3434   fixes s :: "'a :: metric_space set"
       
  3435   shows "seq_compact s \<Longrightarrow> compact s"
       
  3436   unfolding compact_eq_heine_borel
  2823 proof clarify
  3437 proof clarify
  2824   fix f assume "compact s" " \<forall>t\<in>f. open t" "s \<subseteq> \<Union>f"
  3438   fix f assume "seq_compact s" " \<forall>t\<in>f. open t" "s \<subseteq> \<Union>f"
  2825   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
  3439   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
  2826   hence "\<forall>x\<in>s. \<exists>b. b\<in>f \<and> ball x e \<subseteq> b" by auto
  3440   hence "\<forall>x\<in>s. \<exists>b. b\<in>f \<and> ball x e \<subseteq> b" by auto
  2827   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
  3441   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
  2828   then obtain  bb where bb:"\<forall>x\<in>s. (bb x) \<in> f \<and> ball x e \<subseteq> (bb x)" by blast
  3442   then obtain  bb where bb:"\<forall>x\<in>s. (bb x) \<in> f \<and> ball x e \<subseteq> (bb x)" by blast
  2829 
  3443 
  2830   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
  3444   from `seq_compact s` have  "\<exists> k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k"
       
  3445     using seq_compact_imp_totally_bounded[of s] `e>0` by auto
  2831   then obtain k where k:"finite k" "k \<subseteq> s" "s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k" by auto
  3446   then obtain k where k:"finite k" "k \<subseteq> s" "s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k" by auto
  2832 
  3447 
  2833   have "finite (bb ` k)" using k(1) by auto
  3448   have "finite (bb ` k)" using k(1) by auto
  2834   moreover
  3449   moreover
  2835   { fix x assume "x\<in>s"
  3450   { fix x assume "x\<in>s"
  2838     hence "x \<in> \<Union>(bb ` k)" using  Union_iff[of x "bb ` k"] by auto
  3453     hence "x \<in> \<Union>(bb ` k)" using  Union_iff[of x "bb ` k"] by auto
  2839   }
  3454   }
  2840   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
  3455   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
  2841 qed
  3456 qed
  2842 
  3457 
  2843 subsubsection {* Bolzano-Weierstrass property *}
       
  2844 
       
  2845 lemma heine_borel_imp_bolzano_weierstrass:
       
  2846   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'))"
       
  2847           "infinite t"  "t \<subseteq> s"
       
  2848   shows "\<exists>x \<in> s. x islimpt t"
       
  2849 proof(rule ccontr)
       
  2850   assume "\<not> (\<exists>x \<in> s. x islimpt t)"
       
  2851   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
       
  2852     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
       
  2853   obtain g where g:"g\<subseteq>{t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"
       
  2854     using assms(1)[THEN spec[where x="{t. \<exists>x. x\<in>s \<and> t = f x}"]] using f by auto
       
  2855   from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa" by auto
       
  2856   { fix x y assume "x\<in>t" "y\<in>t" "f x = f y"
       
  2857     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
       
  2858     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  }
       
  2859   hence "inj_on f t" unfolding inj_on_def by simp
       
  2860   hence "infinite (f ` t)" using assms(2) using finite_imageD by auto
       
  2861   moreover
       
  2862   { fix x assume "x\<in>t" "f x \<notin> g"
       
  2863     from g(3) assms(3) `x\<in>t` obtain h where "h\<in>g" and "x\<in>h" by auto
       
  2864     then obtain y where "y\<in>s" "h = f y" using g'[THEN bspec[where x=h]] by auto
       
  2865     hence "y = x" using f[THEN bspec[where x=y]] and `x\<in>t` and `x\<in>h`[unfolded `h = f y`] by auto
       
  2866     hence False using `f x \<notin> g` `h\<in>g` unfolding `h = f y` by auto  }
       
  2867   hence "f ` t \<subseteq> g" by auto
       
  2868   ultimately show False using g(2) using finite_subset by auto
       
  2869 qed
       
  2870 
       
  2871 subsubsection {* Complete the chain of compactness variants *}
  3458 subsubsection {* Complete the chain of compactness variants *}
  2872 
       
  2873 lemma islimpt_range_imp_convergent_subsequence:
       
  2874   fixes l :: "'a :: {t1_space, first_countable_topology}"
       
  2875   assumes l: "l islimpt (range f)"
       
  2876   shows "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  2877 proof -
       
  2878   from first_countable_topology_class.countable_basis_at_decseq[of l] guess A . note A = this
       
  2879 
       
  2880   def s \<equiv> "\<lambda>n i. SOME j. i < j \<and> f j \<in> A (Suc n)"
       
  2881   { fix n i
       
  2882     have "\<exists>a. i < a \<and> f a \<in> A (Suc n)"
       
  2883       by (rule l[THEN islimptE, of "A (Suc n) - (f ` {.. i} - {l})"])
       
  2884          (auto simp: not_le[symmetric] finite_imp_closed A(1,2))
       
  2885     then have "i < s n i" "f (s n i) \<in> A (Suc n)"
       
  2886       unfolding s_def by (auto intro: someI2_ex) }
       
  2887   note s = this
       
  2888   def r \<equiv> "nat_rec (s 0 0) s"
       
  2889   have "subseq r"
       
  2890     by (auto simp: r_def s subseq_Suc_iff)
       
  2891   moreover
       
  2892   have "(\<lambda>n. f (r n)) ----> l"
       
  2893   proof (rule topological_tendstoI)
       
  2894     fix S assume "open S" "l \<in> S"
       
  2895     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially" by auto
       
  2896     moreover
       
  2897     { fix i assume "Suc 0 \<le> i" then have "f (r i) \<in> A i"
       
  2898         by (cases i) (simp_all add: r_def s) }
       
  2899     then have "eventually (\<lambda>i. f (r i) \<in> A i) sequentially" by (auto simp: eventually_sequentially)
       
  2900     ultimately show "eventually (\<lambda>i. f (r i) \<in> S) sequentially"
       
  2901       by eventually_elim auto
       
  2902   qed
       
  2903   ultimately show "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"
       
  2904     by (auto simp: convergent_def comp_def)
       
  2905 qed
       
  2906 
       
  2907 lemma finite_range_imp_infinite_repeats:
       
  2908   fixes f :: "nat \<Rightarrow> 'a"
       
  2909   assumes "finite (range f)"
       
  2910   shows "\<exists>k. infinite {n. f n = k}"
       
  2911 proof -
       
  2912   { fix A :: "'a set" assume "finite A"
       
  2913     hence "\<And>f. infinite {n. f n \<in> A} \<Longrightarrow> \<exists>k. infinite {n. f n = k}"
       
  2914     proof (induct)
       
  2915       case empty thus ?case by simp
       
  2916     next
       
  2917       case (insert x A)
       
  2918      show ?case
       
  2919       proof (cases "finite {n. f n = x}")
       
  2920         case True
       
  2921         with `infinite {n. f n \<in> insert x A}`
       
  2922         have "infinite {n. f n \<in> A}" by simp
       
  2923         thus "\<exists>k. infinite {n. f n = k}" by (rule insert)
       
  2924       next
       
  2925         case False thus "\<exists>k. infinite {n. f n = k}" ..
       
  2926       qed
       
  2927     qed
       
  2928   } note H = this
       
  2929   from assms show "\<exists>k. infinite {n. f n = k}"
       
  2930     by (rule H) simp
       
  2931 qed
       
  2932 
       
  2933 lemma bolzano_weierstrass_imp_compact:
       
  2934   fixes s :: "'a::metric_space set"
       
  2935   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
       
  2936   shows "compact s"
       
  2937 proof -
       
  2938   { fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
       
  2939     have "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  2940     proof (cases "finite (range f)")
       
  2941       case True
       
  2942       hence "\<exists>l. infinite {n. f n = l}"
       
  2943         by (rule finite_range_imp_infinite_repeats)
       
  2944       then obtain l where "infinite {n. f n = l}" ..
       
  2945       hence "\<exists>r. subseq r \<and> (\<forall>n. r n \<in> {n. f n = l})"
       
  2946         by (rule infinite_enumerate)
       
  2947       then obtain r where "subseq r" and fr: "\<forall>n. f (r n) = l" by auto
       
  2948       hence "subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  2949         unfolding o_def by (simp add: fr tendsto_const)
       
  2950       hence "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  2951         by - (rule exI)
       
  2952       from f have "\<forall>n. f (r n) \<in> s" by simp
       
  2953       hence "l \<in> s" by (simp add: fr)
       
  2954       thus "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  2955         by (rule rev_bexI) fact
       
  2956     next
       
  2957       case False
       
  2958       with f assms have "\<exists>x\<in>s. x islimpt (range f)" by auto
       
  2959       then obtain l where "l \<in> s" "l islimpt (range f)" ..
       
  2960       have "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  2961         using `l islimpt (range f)`
       
  2962         by (rule islimpt_range_imp_convergent_subsequence)
       
  2963       with `l \<in> s` show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" ..
       
  2964     qed
       
  2965   }
       
  2966   thus ?thesis unfolding compact_def by auto
       
  2967 qed
       
  2968 
  3459 
  2969 primrec helper_2::"(real \<Rightarrow> 'a::metric_space) \<Rightarrow> nat \<Rightarrow> 'a" where
  3460 primrec helper_2::"(real \<Rightarrow> 'a::metric_space) \<Rightarrow> nat \<Rightarrow> 'a" where
  2970   "helper_2 beyond 0 = beyond 0" |
  3461   "helper_2 beyond 0 = beyond 0" |
  2971   "helper_2 beyond (Suc n) = beyond (dist undefined (helper_2 beyond n) + 1 )"
  3462   "helper_2 beyond (Suc n) = beyond (dist undefined (helper_2 beyond n) + 1 )"
  2972 
  3463 
  3028   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"]]
  3519   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"]]
  3029     unfolding dist_nz by auto
  3520     unfolding dist_nz by auto
  3030   show False using y and z and dist_triangle_half_l[of "x y" l 1 "x z"] and **[of y z] by auto
  3521   show False using y and z and dist_triangle_half_l[of "x y" l 1 "x z"] and **[of y z] by auto
  3031 qed
  3522 qed
  3032 
  3523 
  3033 lemma sequence_infinite_lemma:
       
  3034   fixes f :: "nat \<Rightarrow> 'a::t1_space"
       
  3035   assumes "\<forall>n. f n \<noteq> l" and "(f ---> l) sequentially"
       
  3036   shows "infinite (range f)"
       
  3037 proof
       
  3038   assume "finite (range f)"
       
  3039   hence "closed (range f)" by (rule finite_imp_closed)
       
  3040   hence "open (- range f)" by (rule open_Compl)
       
  3041   from assms(1) have "l \<in> - range f" by auto
       
  3042   from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"
       
  3043     using `open (- range f)` `l \<in> - range f` by (rule topological_tendstoD)
       
  3044   thus False unfolding eventually_sequentially by auto
       
  3045 qed
       
  3046 
       
  3047 lemma closure_insert:
       
  3048   fixes x :: "'a::t1_space"
       
  3049   shows "closure (insert x s) = insert x (closure s)"
       
  3050 apply (rule closure_unique)
       
  3051 apply (rule insert_mono [OF closure_subset])
       
  3052 apply (rule closed_insert [OF closed_closure])
       
  3053 apply (simp add: closure_minimal)
       
  3054 done
       
  3055 
       
  3056 lemma islimpt_insert:
       
  3057   fixes x :: "'a::t1_space"
       
  3058   shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"
       
  3059 proof
       
  3060   assume *: "x islimpt (insert a s)"
       
  3061   show "x islimpt s"
       
  3062   proof (rule islimptI)
       
  3063     fix t assume t: "x \<in> t" "open t"
       
  3064     show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"
       
  3065     proof (cases "x = a")
       
  3066       case True
       
  3067       obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"
       
  3068         using * t by (rule islimptE)
       
  3069       with `x = a` show ?thesis by auto
       
  3070     next
       
  3071       case False
       
  3072       with t have t': "x \<in> t - {a}" "open (t - {a})"
       
  3073         by (simp_all add: open_Diff)
       
  3074       obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"
       
  3075         using * t' by (rule islimptE)
       
  3076       thus ?thesis by auto
       
  3077     qed
       
  3078   qed
       
  3079 next
       
  3080   assume "x islimpt s" thus "x islimpt (insert a s)"
       
  3081     by (rule islimpt_subset) auto
       
  3082 qed
       
  3083 
       
  3084 lemma islimpt_union_finite:
       
  3085   fixes x :: "'a::t1_space"
       
  3086   shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"
       
  3087 by (induct set: finite, simp_all add: islimpt_insert)
       
  3088  
       
  3089 lemma sequence_unique_limpt:
       
  3090   fixes f :: "nat \<Rightarrow> 'a::t2_space"
       
  3091   assumes "(f ---> l) sequentially" and "l' islimpt (range f)"
       
  3092   shows "l' = l"
       
  3093 proof (rule ccontr)
       
  3094   assume "l' \<noteq> l"
       
  3095   obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"
       
  3096     using hausdorff [OF `l' \<noteq> l`] by auto
       
  3097   have "eventually (\<lambda>n. f n \<in> t) sequentially"
       
  3098     using assms(1) `open t` `l \<in> t` by (rule topological_tendstoD)
       
  3099   then obtain N where "\<forall>n\<ge>N. f n \<in> t"
       
  3100     unfolding eventually_sequentially by auto
       
  3101 
       
  3102   have "UNIV = {..<N} \<union> {N..}" by auto
       
  3103   hence "l' islimpt (f ` ({..<N} \<union> {N..}))" using assms(2) by simp
       
  3104   hence "l' islimpt (f ` {..<N} \<union> f ` {N..})" by (simp add: image_Un)
       
  3105   hence "l' islimpt (f ` {N..})" by (simp add: islimpt_union_finite)
       
  3106   then obtain y where "y \<in> f ` {N..}" "y \<in> s" "y \<noteq> l'"
       
  3107     using `l' \<in> s` `open s` by (rule islimptE)
       
  3108   then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'" by auto
       
  3109   with `\<forall>n\<ge>N. f n \<in> t` have "f n \<in> s \<inter> t" by simp
       
  3110   with `s \<inter> t = {}` show False by simp
       
  3111 qed
       
  3112 
       
  3113 lemma bolzano_weierstrass_imp_closed:
       
  3114   fixes s :: "'a::metric_space set" (* TODO: can this be generalized? *)
       
  3115   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
       
  3116   shows "closed s"
       
  3117 proof-
       
  3118   { fix x l assume as: "\<forall>n::nat. x n \<in> s" "(x ---> l) sequentially"
       
  3119     hence "l \<in> s"
       
  3120     proof(cases "\<forall>n. x n \<noteq> l")
       
  3121       case False thus "l\<in>s" using as(1) by auto
       
  3122     next
       
  3123       case True note cas = this
       
  3124       with as(2) have "infinite (range x)" using sequence_infinite_lemma[of x l] by auto
       
  3125       then obtain l' where "l'\<in>s" "l' islimpt (range x)" using assms[THEN spec[where x="range x"]] as(1) by auto
       
  3126       thus "l\<in>s" using sequence_unique_limpt[of x l l'] using as cas by auto
       
  3127     qed  }
       
  3128   thus ?thesis unfolding closed_sequential_limits by fast
       
  3129 qed
       
  3130 
       
  3131 text {* Hence express everything as an equivalence. *}
  3524 text {* Hence express everything as an equivalence. *}
  3132 
  3525 
  3133 lemma compact_eq_heine_borel:
  3526 lemma compact_eq_seq_compact_metric:
  3134   fixes s :: "'a::metric_space set"
  3527   "compact (s :: 'a::metric_space set) \<longleftrightarrow> seq_compact s"
  3135   shows "compact s \<longleftrightarrow>
  3528   using compact_imp_seq_compact seq_compact_imp_heine_borel by blast
  3136            (\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f)
  3529 
  3137                --> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f')))" (is "?lhs = ?rhs")
  3530 lemma compact_def:
  3138 proof
  3531   "compact (S :: 'a::metric_space set) \<longleftrightarrow>
  3139   assume ?lhs thus ?rhs by (rule compact_imp_heine_borel)
  3532    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>
  3140 next
  3533        (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially)) "
  3141   assume ?rhs
  3534   unfolding compact_eq_seq_compact_metric seq_compact_def by auto
  3142   hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x\<in>s. x islimpt t)"
       
  3143     by (blast intro: heine_borel_imp_bolzano_weierstrass[of s])
       
  3144   thus ?lhs by (rule bolzano_weierstrass_imp_compact)
       
  3145 qed
       
  3146 
  3535 
  3147 lemma compact_eq_bolzano_weierstrass:
  3536 lemma compact_eq_bolzano_weierstrass:
  3148   fixes s :: "'a::metric_space set"
  3537   fixes s :: "'a::metric_space set"
  3149   shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))" (is "?lhs = ?rhs")
  3538   shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))" (is "?lhs = ?rhs")
  3150 proof
  3539 proof
  3151   assume ?lhs thus ?rhs unfolding compact_eq_heine_borel using heine_borel_imp_bolzano_weierstrass[of s] by auto
  3540   assume ?lhs thus ?rhs using heine_borel_imp_bolzano_weierstrass[of s] by auto
  3152 next
  3541 next
  3153   assume ?rhs thus ?lhs by (rule bolzano_weierstrass_imp_compact)
  3542   assume ?rhs thus ?lhs
       
  3543     unfolding compact_eq_seq_compact_metric by (rule bolzano_weierstrass_imp_seq_compact)
  3154 qed
  3544 qed
  3155 
  3545 
  3156 lemma nat_approx_posE:
  3546 lemma nat_approx_posE:
  3157   fixes e::real
  3547   fixes e::real
  3158   assumes "0 < e"
  3548   assumes "0 < e"
  3165   also have "\<dots> = e" by simp
  3555   also have "\<dots> = e" by simp
  3166   finally show  "\<exists>n. 1 / real (Suc n) < e" ..
  3556   finally show  "\<exists>n. 1 / real (Suc n) < e" ..
  3167 qed
  3557 qed
  3168 
  3558 
  3169 lemma compact_eq_totally_bounded:
  3559 lemma compact_eq_totally_bounded:
  3170   shows "compact s \<longleftrightarrow> complete s \<and> (\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` k)))"
  3560   "compact s \<longleftrightarrow> complete s \<and> (\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` k)))"
  3171 proof (safe intro!: compact_imp_complete)
  3561 proof (safe intro!: seq_compact_imp_complete[unfolded  compact_eq_seq_compact_metric[symmetric]])
  3172   fix e::real
  3562   fix e::real
  3173   def f \<equiv> "(\<lambda>x::'a. ball x e) ` UNIV"
  3563   def f \<equiv> "(\<lambda>x::'a. ball x e) ` UNIV"
  3174   assume "0 < e" "compact s"
  3564   assume "0 < e" "compact s"
  3175   hence "(\<forall>t\<in>f. open t) \<and> s \<subseteq> \<Union>f \<longrightarrow> (\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f')"
  3565   hence "(\<forall>t\<in>f. open t) \<and> s \<subseteq> \<Union>f \<longrightarrow> (\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f')"
  3176     by (simp add: compact_eq_heine_borel)
  3566     by (simp add: compact_eq_heine_borel)
  3303 
  3693 
  3304 lemma compact_eq_bounded_closed:
  3694 lemma compact_eq_bounded_closed:
  3305   fixes s :: "'a::heine_borel set"
  3695   fixes s :: "'a::heine_borel set"
  3306   shows "compact s \<longleftrightarrow> bounded s \<and> closed s"  (is "?lhs = ?rhs")
  3696   shows "compact s \<longleftrightarrow> bounded s \<and> closed s"  (is "?lhs = ?rhs")
  3307 proof
  3697 proof
  3308   assume ?lhs thus ?rhs unfolding compact_eq_bolzano_weierstrass using bolzano_weierstrass_imp_bounded bolzano_weierstrass_imp_closed by auto
  3698   assume ?lhs thus ?rhs
       
  3699     unfolding compact_eq_bolzano_weierstrass using bolzano_weierstrass_imp_bounded bolzano_weierstrass_imp_closed by auto
  3309 next
  3700 next
  3310   assume ?rhs thus ?lhs using bounded_closed_imp_compact by auto
  3701   assume ?rhs thus ?lhs
       
  3702     using bounded_closed_imp_seq_compact[of s] unfolding compact_eq_seq_compact_metric by auto
  3311 qed
  3703 qed
  3312 
  3704 
  3313 lemma compact_imp_bounded:
  3705 lemma compact_imp_bounded:
  3314   fixes s :: "'a::metric_space set"
  3706   fixes s :: "'a::metric_space set"
  3315   shows "compact s ==> bounded s"
  3707   shows "compact s \<Longrightarrow> bounded s"
  3316 proof -
  3708 proof -
  3317   assume "compact s"
  3709   assume "compact s"
  3318   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')"
       
  3319     by (rule compact_imp_heine_borel)
       
  3320   hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"
  3710   hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"
  3321     using heine_borel_imp_bolzano_weierstrass[of s] by auto
  3711     using heine_borel_imp_bolzano_weierstrass[of s] by auto
  3322   thus "bounded s"
  3712   thus "bounded s"
  3323     by (rule bolzano_weierstrass_imp_bounded)
  3713     by (rule bolzano_weierstrass_imp_bounded)
  3324 qed
  3714 qed
  3325 
       
  3326 lemma compact_imp_closed:
       
  3327   fixes s :: "'a::metric_space set"
       
  3328   shows "compact s ==> closed s"
       
  3329 proof -
       
  3330   assume "compact s"
       
  3331   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')"
       
  3332     by (rule compact_imp_heine_borel)
       
  3333   hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"
       
  3334     using heine_borel_imp_bolzano_weierstrass[of s] by auto
       
  3335   thus "closed s"
       
  3336     by (rule bolzano_weierstrass_imp_closed)
       
  3337 qed
       
  3338 
       
  3339 text{* In particular, some common special cases. *}
       
  3340 
       
  3341 lemma compact_empty[simp]:
       
  3342  "compact {}"
       
  3343   unfolding compact_def
       
  3344   by simp
       
  3345 
       
  3346 lemma compact_union [intro]:
       
  3347   assumes "compact s" and "compact t"
       
  3348   shows "compact (s \<union> t)"
       
  3349 proof (rule compactI)
       
  3350   fix f :: "nat \<Rightarrow> 'a"
       
  3351   assume "\<forall>n. f n \<in> s \<union> t"
       
  3352   hence "infinite {n. f n \<in> s \<union> t}" by simp
       
  3353   hence "infinite {n. f n \<in> s} \<or> infinite {n. f n \<in> t}" by simp
       
  3354   thus "\<exists>l\<in>s \<union> t. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  3355   proof
       
  3356     assume "infinite {n. f n \<in> s}"
       
  3357     from infinite_enumerate [OF this]
       
  3358     obtain q where "subseq q" "\<forall>n. (f \<circ> q) n \<in> s" by auto
       
  3359     obtain r l where "l \<in> s" "subseq r" "((f \<circ> q \<circ> r) ---> l) sequentially"
       
  3360       using `compact s` `\<forall>n. (f \<circ> q) n \<in> s` by (rule compactE)
       
  3361     hence "l \<in> s \<union> t" "subseq (q \<circ> r)" "((f \<circ> (q \<circ> r)) ---> l) sequentially"
       
  3362       using `subseq q` by (simp_all add: subseq_o o_assoc)
       
  3363     thus ?thesis by auto
       
  3364   next
       
  3365     assume "infinite {n. f n \<in> t}"
       
  3366     from infinite_enumerate [OF this]
       
  3367     obtain q where "subseq q" "\<forall>n. (f \<circ> q) n \<in> t" by auto
       
  3368     obtain r l where "l \<in> t" "subseq r" "((f \<circ> q \<circ> r) ---> l) sequentially"
       
  3369       using `compact t` `\<forall>n. (f \<circ> q) n \<in> t` by (rule compactE)
       
  3370     hence "l \<in> s \<union> t" "subseq (q \<circ> r)" "((f \<circ> (q \<circ> r)) ---> l) sequentially"
       
  3371       using `subseq q` by (simp_all add: subseq_o o_assoc)
       
  3372     thus ?thesis by auto
       
  3373   qed
       
  3374 qed
       
  3375 
       
  3376 lemma compact_Union [intro]: "finite S \<Longrightarrow> (\<And>T. T \<in> S \<Longrightarrow> compact T) \<Longrightarrow> compact (\<Union>S)"
       
  3377   by (induct set: finite) auto
       
  3378 
       
  3379 lemma compact_UN [intro]:
       
  3380   "finite A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> compact (B x)) \<Longrightarrow> compact (\<Union>x\<in>A. B x)"
       
  3381   unfolding SUP_def by (rule compact_Union) auto
       
  3382 
       
  3383 lemma compact_inter_closed [intro]:
       
  3384   assumes "compact s" and "closed t"
       
  3385   shows "compact (s \<inter> t)"
       
  3386 proof (rule compactI)
       
  3387   fix f :: "nat \<Rightarrow> 'a"
       
  3388   assume "\<forall>n. f n \<in> s \<inter> t"
       
  3389   hence "\<forall>n. f n \<in> s" and "\<forall>n. f n \<in> t" by simp_all
       
  3390   obtain l r where "l \<in> s" "subseq r" "((f \<circ> r) ---> l) sequentially"
       
  3391     using `compact s` `\<forall>n. f n \<in> s` by (rule compactE)
       
  3392   moreover
       
  3393   from `closed t` `\<forall>n. f n \<in> t` `((f \<circ> r) ---> l) sequentially` have "l \<in> t"
       
  3394     unfolding closed_sequential_limits o_def by fast
       
  3395   ultimately show "\<exists>l\<in>s \<inter> t. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
       
  3396     by auto
       
  3397 qed
       
  3398 
       
  3399 lemma closed_inter_compact [intro]:
       
  3400   assumes "closed s" and "compact t"
       
  3401   shows "compact (s \<inter> t)"
       
  3402   using compact_inter_closed [of t s] assms
       
  3403   by (simp add: Int_commute)
       
  3404 
       
  3405 lemma compact_inter [intro]:
       
  3406   assumes "compact s" and "compact t"
       
  3407   shows "compact (s \<inter> t)"
       
  3408   using assms by (intro compact_inter_closed compact_imp_closed)
       
  3409 
       
  3410 lemma compact_sing [simp]: "compact {a}"
       
  3411   unfolding compact_def o_def subseq_def
       
  3412   by (auto simp add: tendsto_const)
       
  3413 
       
  3414 lemma compact_insert [simp]:
       
  3415   assumes "compact s" shows "compact (insert x s)"
       
  3416 proof -
       
  3417   have "compact ({x} \<union> s)"
       
  3418     using compact_sing assms by (rule compact_union)
       
  3419   thus ?thesis by simp
       
  3420 qed
       
  3421 
       
  3422 lemma finite_imp_compact:
       
  3423   shows "finite s \<Longrightarrow> compact s"
       
  3424   by (induct set: finite) simp_all
       
  3425 
  3715 
  3426 lemma compact_cball[simp]:
  3716 lemma compact_cball[simp]:
  3427   fixes x :: "'a::heine_borel"
  3717   fixes x :: "'a::heine_borel"
  3428   shows "compact(cball x e)"
  3718   shows "compact(cball x e)"
  3429   using compact_eq_bounded_closed bounded_cball closed_cball
  3719   using compact_eq_bounded_closed bounded_cball closed_cball
  3446   fixes s :: "'a::heine_borel set"
  3736   fixes s :: "'a::heine_borel set"
  3447   shows "compact s ==> frontier s \<subseteq> s"
  3737   shows "compact s ==> frontier s \<subseteq> s"
  3448   using frontier_subset_closed compact_eq_bounded_closed
  3738   using frontier_subset_closed compact_eq_bounded_closed
  3449   by blast
  3739   by blast
  3450 
  3740 
  3451 lemma open_delete:
       
  3452   fixes s :: "'a::t1_space set"
       
  3453   shows "open s \<Longrightarrow> open (s - {x})"
       
  3454   by (simp add: open_Diff)
       
  3455 
       
  3456 text{* Finite intersection property. I could make it an equivalence in fact. *}
       
  3457 
       
  3458 lemma compact_imp_fip:
       
  3459   assumes "compact s"  "\<forall>t \<in> f. closed t"
       
  3460         "\<forall>f'. finite f' \<and> f' \<subseteq> f --> (s \<inter> (\<Inter> f') \<noteq> {})"
       
  3461   shows "s \<inter> (\<Inter> f) \<noteq> {}"
       
  3462 proof
       
  3463   assume as:"s \<inter> (\<Inter> f) = {}"
       
  3464   hence "s \<subseteq> \<Union> uminus ` f" by auto
       
  3465   moreover have "Ball (uminus ` f) open" using open_Diff closed_Diff using assms(2) by auto
       
  3466   ultimately obtain f' where f':"f' \<subseteq> uminus ` f"  "finite f'"  "s \<subseteq> \<Union>f'" using assms(1)[unfolded compact_eq_heine_borel, THEN spec[where x="(\<lambda>t. - t) ` f"]] by auto
       
  3467   hence "finite (uminus ` f') \<and> uminus ` f' \<subseteq> f" by(auto simp add: Diff_Diff_Int)
       
  3468   hence "s \<inter> \<Inter>uminus ` f' \<noteq> {}" using assms(3)[THEN spec[where x="uminus ` f'"]] by auto
       
  3469   thus False using f'(3) unfolding subset_eq and Union_iff by blast
       
  3470 qed
       
  3471 
       
  3472 
       
  3473 subsection {* Bounded closed nest property (proof does not use Heine-Borel) *}
  3741 subsection {* Bounded closed nest property (proof does not use Heine-Borel) *}
  3474 
  3742 
  3475 lemma bounded_closed_nest:
  3743 lemma bounded_closed_nest:
  3476   assumes "\<forall>n. closed(s n)" "\<forall>n. (s n \<noteq> {})"
  3744   assumes "\<forall>n. closed(s n)" "\<forall>n. (s n \<noteq> {})"
  3477   "(\<forall>m n. m \<le> n --> s n \<subseteq> s m)"  "bounded(s 0)"
  3745   "(\<forall>m n. m \<le> n --> s n \<subseteq> s m)"  "bounded(s 0)"
  3478   shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s(n)"
  3746   shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s(n)"
  3479 proof-
  3747 proof-
  3480   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
  3748   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
  3481   from assms(4,1) have *:"compact (s 0)" using bounded_closed_imp_compact[of "s 0"] by auto
  3749   from assms(4,1) have *:"seq_compact (s 0)" using bounded_closed_imp_seq_compact[of "s 0"] by auto
  3482 
  3750 
  3483   then obtain l r where lr:"l\<in>s 0" "subseq r" "((x \<circ> r) ---> l) sequentially"
  3751   then obtain l r where lr:"l\<in>s 0" "subseq r" "((x \<circ> r) ---> l) sequentially"
  3484     unfolding compact_def apply(erule_tac x=x in allE)  using x using assms(3) by blast
  3752     unfolding seq_compact_def apply(erule_tac x=x in allE)  using x using assms(3) by blast
  3485 
  3753 
  3486   { fix n::nat
  3754   { fix n::nat
  3487     { fix e::real assume "e>0"
  3755     { fix e::real assume "e>0"
  3488       with lr(3) obtain N where N:"\<forall>m\<ge>N. dist ((x \<circ> r) m) l < e" unfolding LIMSEQ_def by auto
  3756       with lr(3) obtain N where N:"\<forall>m\<ge>N. dist ((x \<circ> r) m) l < e" unfolding LIMSEQ_def by auto
  3489       hence "dist ((x \<circ> r) (max N n)) l < e" by auto
  3757       hence "dist ((x \<circ> r) (max N n)) l < e" by auto
  3490       moreover
  3758       moreover
  3491       have "r (max N n) \<ge> n" using lr(2) using subseq_bigger[of r "max N n"] by auto
  3759       have "r (max N n) \<ge> n" using lr(2) using subseq_bigger[of r "max N n"] by auto
  3492       hence "(x \<circ> r) (max N n) \<in> s n"
  3760       hence "(x \<circ> r) (max N n) \<in> s n"
  3493         using x apply(erule_tac x=n in allE)
  3761         using x apply(erule_tac x=n in allE)
  3494         using x apply(erule_tac x="r (max N n)" in allE)
  3762         using x apply(erule_tac x="r (max N n)" in allE)
  3495         using assms(3) apply(erule_tac x=n in allE)apply( erule_tac x="r (max N n)" in allE) by auto
  3763         using assms(3) apply(erule_tac x=n in allE) apply(erule_tac x="r (max N n)" in allE) by auto
  3496       ultimately have "\<exists>y\<in>s n. dist y l < e" by auto
  3764       ultimately have "\<exists>y\<in>s n. dist y l < e" by auto
  3497     }
  3765     }
  3498     hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by blast
  3766     hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by blast
  3499   }
  3767   }
  3500   thus ?thesis by auto
  3768   thus ?thesis by auto
  4529   by (fast elim: bounded_bilinear.tendsto)
  4797   by (fast elim: bounded_bilinear.tendsto)
  4530 
  4798 
  4531 text {* Preservation of compactness and connectedness under continuous function. *}
  4799 text {* Preservation of compactness and connectedness under continuous function. *}
  4532 
  4800 
  4533 lemma compact_continuous_image:
  4801 lemma compact_continuous_image:
       
  4802   fixes f :: "'a :: metric_space \<Rightarrow> 'b :: metric_space"
  4534   assumes "continuous_on s f"  "compact s"
  4803   assumes "continuous_on s f"  "compact s"
  4535   shows "compact(f ` s)"
  4804   shows "compact(f ` s)"
  4536 proof-
  4805 proof-
  4537   { fix x assume x:"\<forall>n::nat. x n \<in> f ` s"
  4806   { fix x assume x:"\<forall>n::nat. x n \<in> f ` s"
  4538     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
  4807     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
  4539     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
  4808     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
  4540     { fix e::real assume "e>0"
  4809     { fix e::real assume "e>0"
  4541       then obtain d where "d>0" and d:"\<forall>x'\<in>s. dist x' l < d \<longrightarrow> dist (f x') (f l) < e" using assms(1)[unfolded continuous_on_iff, THEN bspec[where x=l], OF `l\<in>s`] by auto
  4810       then obtain d where "d>0" and d:"\<forall>x'\<in>s. dist x' l < d \<longrightarrow> dist (f x') (f l) < e"
       
  4811         using assms(1)[unfolded continuous_on_iff, THEN bspec[where x=l], OF `l\<in>s`] by auto
  4542       then obtain N::nat where N:"\<forall>n\<ge>N. dist ((y \<circ> r) n) l < d" using lr[unfolded LIMSEQ_def, THEN spec[where x=d]] by auto
  4812       then obtain N::nat where N:"\<forall>n\<ge>N. dist ((y \<circ> r) n) l < d" using lr[unfolded LIMSEQ_def, THEN spec[where x=d]] by auto
  4543       { 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  }
  4813       { 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  }
  4544       hence "\<exists>N. \<forall>n\<ge>N. dist ((x \<circ> r) n) (f l) < e" by auto  }
  4814       hence "\<exists>N. \<forall>n\<ge>N. dist ((x \<circ> r) n) (f l) < e" by auto  }
  4545     hence "\<exists>l\<in>f ` s. \<exists>r. subseq r \<and> ((x \<circ> r) ---> l) sequentially" unfolding LIMSEQ_def using r lr `l\<in>s` by auto  }
  4815     hence "\<exists>l\<in>f ` s. \<exists>r. subseq r \<and> ((x \<circ> r) ---> l) sequentially" unfolding LIMSEQ_def using r lr `l\<in>s` by auto  }
  4546   thus ?thesis unfolding compact_def by auto
  4816   thus ?thesis unfolding compact_def by auto
  4577 
  4847 
  4578     { 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  }
  4848     { 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  }
  4579     hence "s \<subseteq> \<Union>{ball x (d x (e / 2)) |x. x \<in> s}" unfolding subset_eq by auto
  4849     hence "s \<subseteq> \<Union>{ball x (d x (e / 2)) |x. x \<in> s}" unfolding subset_eq by auto
  4580     moreover
  4850     moreover
  4581     { fix b assume "b\<in>{ball x (d x (e / 2)) |x. x \<in> s}" hence "open b" by auto  }
  4851     { fix b assume "b\<in>{ball x (d x (e / 2)) |x. x \<in> s}" hence "open b" by auto  }
  4582     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
  4852     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"
       
  4853       using heine_borel_lemma[OF assms(2)[unfolded compact_eq_seq_compact_metric], of "{ball x (d x (e / 2)) | x. x\<in>s }"] by auto
  4583 
  4854 
  4584     { fix x y assume "x\<in>s" "y\<in>s" and as:"dist y x < ea"
  4855     { fix x y assume "x\<in>s" "y\<in>s" and as:"dist y x < ea"
  4585       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
  4856       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
  4586       hence "x\<in>ball z (d z (e / 2))" using `ea>0` unfolding subset_eq by auto
  4857       hence "x\<in>ball z (d z (e / 2))" using `ea>0` unfolding subset_eq by auto
  4587       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`
  4858       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`
  4804 qed
  5075 qed
  4805 
  5076 
  4806 lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"
  5077 lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"
  4807 by (induct x) simp
  5078 by (induct x) simp
  4808 
  5079 
  4809 lemma compact_Times: "compact s \<Longrightarrow> compact t \<Longrightarrow> compact (s \<times> t)"
  5080 lemma seq_compact_Times: "seq_compact s \<Longrightarrow> seq_compact t \<Longrightarrow> seq_compact (s \<times> t)"
  4810 unfolding compact_def
  5081 unfolding seq_compact_def
  4811 apply clarify
  5082 apply clarify
  4812 apply (drule_tac x="fst \<circ> f" in spec)
  5083 apply (drule_tac x="fst \<circ> f" in spec)
  4813 apply (drule mp, simp add: mem_Times_iff)
  5084 apply (drule mp, simp add: mem_Times_iff)
  4814 apply (clarify, rename_tac l1 r1)
  5085 apply (clarify, rename_tac l1 r1)
  4815 apply (drule_tac x="snd \<circ> f \<circ> r1" in spec)
  5086 apply (drule_tac x="snd \<circ> f \<circ> r1" in spec)
  4820 apply (rule conjI, simp add: subseq_def)
  5091 apply (rule conjI, simp add: subseq_def)
  4821 apply (drule_tac r=r2 in lim_subseq [rotated], assumption)
  5092 apply (drule_tac r=r2 in lim_subseq [rotated], assumption)
  4822 apply (drule (1) tendsto_Pair) back
  5093 apply (drule (1) tendsto_Pair) back
  4823 apply (simp add: o_def)
  5094 apply (simp add: o_def)
  4824 done
  5095 done
       
  5096 
       
  5097 text {* Generalize to @{class topological_space} *}
       
  5098 lemma compact_Times: 
       
  5099   fixes s :: "'a::metric_space set" and t :: "'b::metric_space set"
       
  5100   shows "compact s \<Longrightarrow> compact t \<Longrightarrow> compact (s \<times> t)"
       
  5101   unfolding compact_eq_seq_compact_metric by (rule seq_compact_Times)
  4825 
  5102 
  4826 text{* Hence some useful properties follow quite easily. *}
  5103 text{* Hence some useful properties follow quite easily. *}
  4827 
  5104 
  4828 lemma compact_scaling:
  5105 lemma compact_scaling:
  4829   fixes s :: "'a::real_normed_vector set"
  5106   fixes s :: "'a::real_normed_vector set"
  5340 lemma not_interval_univ: fixes a :: "'a::ordered_euclidean_space" shows
  5617 lemma not_interval_univ: fixes a :: "'a::ordered_euclidean_space" shows
  5341  "({a .. b} \<noteq> UNIV) \<and> ({a<..<b} \<noteq> UNIV)"
  5618  "({a .. b} \<noteq> UNIV) \<and> ({a<..<b} \<noteq> UNIV)"
  5342   using bounded_interval[of a b] by auto
  5619   using bounded_interval[of a b] by auto
  5343 
  5620 
  5344 lemma compact_interval: fixes a :: "'a::ordered_euclidean_space" shows "compact {a .. b}"
  5621 lemma compact_interval: fixes a :: "'a::ordered_euclidean_space" shows "compact {a .. b}"
  5345   using bounded_closed_imp_compact[of "{a..b}"] using bounded_interval[of a b]
  5622   using bounded_closed_imp_seq_compact[of "{a..b}"] using bounded_interval[of a b]
  5346   by auto
  5623   by (auto simp: compact_eq_seq_compact_metric)
  5347 
  5624 
  5348 lemma open_interval_midpoint: fixes a :: "'a::ordered_euclidean_space"
  5625 lemma open_interval_midpoint: fixes a :: "'a::ordered_euclidean_space"
  5349   assumes "{a<..<b} \<noteq> {}" shows "((1/2) *\<^sub>R (a + b)) \<in> {a<..<b}"
  5626   assumes "{a<..<b} \<noteq> {}" shows "((1/2) *\<^sub>R (a + b)) \<in> {a<..<b}"
  5350 proof-
  5627 proof-
  5351   { fix i :: 'a assume "i\<in>Basis"
  5628   { fix i :: 'a assume "i\<in>Basis"