src/HOL/Analysis/Homotopy.thy
author paulson <lp15@cam.ac.uk>
Tue Mar 26 17:01:36 2019 +0000 (7 months ago)
changeset 69986 f2d327275065
parent 69922 4a9167f377b0
child 70033 6cbc7634135c
permissions -rw-r--r--
generalised homotopic_with to topologies; homotopic_with_canon is the old version
     1 (*  Title:      HOL/Analysis/Path_Connected.thy
     2     Authors:    LC Paulson and Robert Himmelmann (TU Muenchen), based on material from HOL Light
     3 *)
     4 
     5 section \<open>Homotopy of Maps\<close>
     6 
     7 theory Homotopy
     8   imports Path_Connected Continuum_Not_Denumerable Product_Topology
     9 begin
    10 
    11 definition%important homotopic_with
    12 where
    13  "homotopic_with P X Y f g \<equiv>
    14    (\<exists>h. continuous_map (prod_topology (subtopology euclideanreal {0..1}) X) Y h \<and>
    15        (\<forall>x. h(0, x) = f x) \<and>
    16        (\<forall>x. h(1, x) = g x) \<and>
    17        (\<forall>t \<in> {0..1}. P(\<lambda>x. h(t,x))))"
    18 
    19 text\<open>\<open>p\<close>, \<open>q\<close> are functions \<open>X \<rightarrow> Y\<close>, and the property \<open>P\<close> restricts all intermediate maps.
    20 We often just want to require that \<open>P\<close> fixes some subset, but to include the case of a loop homotopy,
    21 it is convenient to have a general property \<open>P\<close>.\<close>
    22 
    23 abbreviation homotopic_with_canon ::
    24   "[('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> bool, 'a set, 'b set, 'a \<Rightarrow> 'b, 'a \<Rightarrow> 'b] \<Rightarrow> bool"
    25 where
    26  "homotopic_with_canon P S T p q \<equiv> homotopic_with P (top_of_set S) (top_of_set T) p q"
    27 
    28 lemma split_01: "{0..1::real} = {0..1/2} \<union> {1/2..1}"
    29   by force
    30 
    31 lemma split_01_prod: "{0..1::real} \<times> X = ({0..1/2} \<times> X) \<union> ({1/2..1} \<times> X)"
    32   by force
    33 
    34 lemma image_Pair_const: "(\<lambda>x. (x, c)) ` A = A \<times> {c}"
    35   by auto
    36 
    37 lemma fst_o_paired [simp]: "fst \<circ> (\<lambda>(x,y). (f x y, g x y)) = (\<lambda>(x,y). f x y)"
    38   by auto
    39 
    40 lemma snd_o_paired [simp]: "snd \<circ> (\<lambda>(x,y). (f x y, g x y)) = (\<lambda>(x,y). g x y)"
    41   by auto
    42 
    43 lemma continuous_on_o_Pair: "\<lbrakk>continuous_on (T \<times> X) h; t \<in> T\<rbrakk> \<Longrightarrow> continuous_on X (h \<circ> Pair t)"
    44   by (fast intro: continuous_intros elim!: continuous_on_subset)
    45 
    46 lemma continuous_map_o_Pair: 
    47   assumes h: "continuous_map (prod_topology X Y) Z h" and t: "t \<in> topspace X"
    48   shows "continuous_map Y Z (h \<circ> Pair t)"
    49   apply (intro continuous_map_compose [OF _ h] continuous_map_id [unfolded id_def] continuous_intros)
    50   apply (simp add: t)
    51   done
    52 
    53 subsection%unimportant\<open>Trivial properties\<close>
    54 
    55 text \<open>We often want to just localize the ending function equality or whatever.\<close>
    56 text%important \<open>%whitespace\<close>
    57 proposition homotopic_with:
    58   assumes "\<And>h k. (\<And>x. x \<in> topspace X \<Longrightarrow> h x = k x) \<Longrightarrow> (P h \<longleftrightarrow> P k)"
    59   shows "homotopic_with P X Y p q \<longleftrightarrow>
    60            (\<exists>h. continuous_map (prod_topology (subtopology euclideanreal {0..1}) X) Y h \<and>
    61               (\<forall>x \<in> topspace X. h(0,x) = p x) \<and>
    62               (\<forall>x \<in> topspace X. h(1,x) = q x) \<and>
    63               (\<forall>t \<in> {0..1}. P(\<lambda>x. h(t, x))))"
    64   unfolding homotopic_with_def
    65   apply (rule iffI, blast, clarify)
    66   apply (rule_tac x="\<lambda>(u,v). if v \<in> topspace X then h(u,v) else if u = 0 then p v else q v" in exI)
    67   apply auto
    68   using continuous_map_eq apply fastforce
    69   apply (drule_tac x=t in bspec, force)
    70   apply (subst assms; simp)
    71   done
    72 
    73 lemma homotopic_with_mono:
    74   assumes hom: "homotopic_with P X Y f g"
    75     and Q: "\<And>h. \<lbrakk>continuous_map X Y h; P h\<rbrakk> \<Longrightarrow> Q h"
    76   shows "homotopic_with Q X Y f g"
    77   using hom
    78   apply (simp add: homotopic_with_def)
    79   apply (erule ex_forward)
    80   apply (force simp: o_def dest: continuous_map_o_Pair intro: Q)
    81   done
    82 
    83 lemma homotopic_with_imp_continuous_maps:
    84     assumes "homotopic_with P X Y f g"
    85     shows "continuous_map X Y f \<and> continuous_map X Y g"
    86 proof -
    87   obtain h
    88     where conth: "continuous_map (prod_topology (subtopology euclideanreal {0..1}) X) Y h"
    89       and h: "\<forall>x. h (0, x) = f x" "\<forall>x. h (1, x) = g x"
    90     using assms by (auto simp: homotopic_with_def)
    91   have *: "t \<in> {0..1} \<Longrightarrow> continuous_map X Y (h \<circ> (\<lambda>x. (t,x)))" for t
    92     by (rule continuous_map_compose [OF _ conth]) (simp add: o_def continuous_map_pairwise)
    93   show ?thesis
    94     using h *[of 0] *[of 1] by (simp add: continuous_map_eq)
    95 qed
    96 
    97 lemma homotopic_with_imp_continuous:
    98     assumes "homotopic_with_canon P X Y f g"
    99     shows "continuous_on X f \<and> continuous_on X g"
   100   by (meson assms continuous_map_subtopology_eu homotopic_with_imp_continuous_maps)
   101 
   102 lemma homotopic_with_imp_property:
   103   assumes "homotopic_with P X Y f g"
   104   shows "P f \<and> P g"
   105 proof
   106   obtain h where h: "\<And>x. h(0, x) = f x" "\<And>x. h(1, x) = g x" and P: "\<And>t. t \<in> {0..1::real} \<Longrightarrow> P(\<lambda>x. h(t,x))"
   107     using assms by (force simp: homotopic_with_def)
   108   show "P f" "P g"
   109     using P [of 0] P [of 1] by (force simp: h)+
   110 qed
   111 
   112 lemma homotopic_with_equal:
   113   assumes "P f" "P g" and contf: "continuous_map X Y f" and fg: "\<And>x. x \<in> topspace X \<Longrightarrow> f x = g x"
   114   shows "homotopic_with P X Y f g"
   115   unfolding homotopic_with_def
   116 proof (intro exI conjI allI ballI)
   117   let ?h = "\<lambda>(t::real,x). if t = 1 then g x else f x"
   118   show "continuous_map (prod_topology (subtopology euclideanreal {0..1}) X) Y ?h"
   119   proof (rule continuous_map_eq)
   120     show "continuous_map (prod_topology (subtopology euclideanreal {0..1}) X) Y (f \<circ> snd)"
   121       by (simp add: contf continuous_map_of_snd)
   122   qed (auto simp: fg)
   123   show "P (\<lambda>x. ?h (t, x))" if "t \<in> {0..1}" for t
   124     by (cases "t = 1") (simp_all add: assms)
   125 qed auto
   126 
   127 lemma homotopic_with_imp_subset1:
   128      "homotopic_with_canon P X Y f g \<Longrightarrow> f ` X \<subseteq> Y"
   129   by (simp add: homotopic_with_def image_subset_iff) (metis atLeastAtMost_iff order_refl zero_le_one)
   130 
   131 lemma homotopic_with_imp_subset2:
   132      "homotopic_with_canon P X Y f g \<Longrightarrow> g ` X \<subseteq> Y"
   133   by (simp add: homotopic_with_def image_subset_iff) (metis atLeastAtMost_iff order_refl zero_le_one)
   134 
   135 lemma homotopic_with_subset_left:
   136      "\<lbrakk>homotopic_with_canon P X Y f g; Z \<subseteq> X\<rbrakk> \<Longrightarrow> homotopic_with_canon P Z Y f g"
   137   apply (simp add: homotopic_with_def)
   138   apply (fast elim!: continuous_on_subset ex_forward)
   139   done
   140 
   141 lemma homotopic_with_subset_right:
   142      "\<lbrakk>homotopic_with_canon P X Y f g; Y \<subseteq> Z\<rbrakk> \<Longrightarrow> homotopic_with_canon P X Z f g"
   143   apply (simp add: homotopic_with_def)
   144   apply (fast elim!: continuous_on_subset ex_forward)
   145   done
   146 
   147 subsection\<open>Homotopy with P is an equivalence relation\<close>
   148 
   149 text \<open>(on continuous functions mapping X into Y that satisfy P, though this only affects reflexivity)\<close>
   150 
   151 lemma homotopic_with_refl [simp]: "homotopic_with P X Y f f \<longleftrightarrow> continuous_map X Y f \<and> P f"
   152   by (auto simp: homotopic_with_imp_continuous_maps intro: homotopic_with_equal dest: homotopic_with_imp_property)
   153 
   154 lemma homotopic_with_symD:
   155     assumes "homotopic_with P X Y f g"
   156       shows "homotopic_with P X Y g f"
   157 proof -
   158   let ?I01 = "subtopology euclideanreal {0..1}"
   159   let ?j = "\<lambda>y. (1 - fst y, snd y)"
   160   have 1: "continuous_map (prod_topology ?I01 X) (prod_topology euclideanreal X) ?j"
   161     apply (intro continuous_intros)
   162     apply (simp_all add: prod_topology_subtopology continuous_map_from_subtopology [OF continuous_map_fst])
   163     done
   164   have *: "continuous_map (prod_topology ?I01 X) (prod_topology ?I01 X) ?j"
   165   proof -
   166     have "continuous_map (prod_topology ?I01 X) (subtopology (prod_topology euclideanreal X) ({0..1} \<times> topspace X)) ?j"
   167       by (simp add: continuous_map_into_subtopology [OF 1] image_subset_iff)
   168     then show ?thesis
   169       by (simp add: prod_topology_subtopology(1))
   170   qed
   171   show ?thesis
   172     using assms
   173     apply (clarsimp simp add: homotopic_with_def)
   174     apply (rename_tac h)
   175     apply (rule_tac x="h \<circ> (\<lambda>y. (1 - fst y, snd y))" in exI)
   176     apply (simp add: continuous_map_compose [OF *])
   177     done
   178 qed
   179 
   180 lemma homotopic_with_sym:
   181    "homotopic_with P X Y f g \<longleftrightarrow> homotopic_with P X Y g f"
   182   by (metis homotopic_with_symD)
   183 
   184 proposition homotopic_with_trans:
   185     assumes "homotopic_with P X Y f g"  "homotopic_with P X Y g h"
   186     shows "homotopic_with P X Y f h"
   187 proof -
   188   let ?X01 = "prod_topology (subtopology euclideanreal {0..1}) X"
   189   obtain k1 k2
   190     where contk1: "continuous_map ?X01 Y k1" and contk2: "continuous_map ?X01 Y k2"
   191       and k12: "\<forall>x. k1 (1, x) = g x" "\<forall>x. k2 (0, x) = g x"
   192       "\<forall>x. k1 (0, x) = f x" "\<forall>x. k2 (1, x) = h x"
   193       and P:   "\<forall>t\<in>{0..1}. P (\<lambda>x. k1 (t, x))" "\<forall>t\<in>{0..1}. P (\<lambda>x. k2 (t, x))"
   194     using assms by (auto simp: homotopic_with_def)
   195   define k where "k \<equiv> \<lambda>y. if fst y \<le> 1/2
   196                              then (k1 \<circ> (\<lambda>x. (2 *\<^sub>R fst x, snd x))) y
   197                              else (k2 \<circ> (\<lambda>x. (2 *\<^sub>R fst x -1, snd x))) y"
   198   have keq: "k1 (2 * u, v) = k2 (2 * u -1, v)" if "u = 1/2"  for u v
   199     by (simp add: k12 that)
   200   show ?thesis
   201     unfolding homotopic_with_def
   202   proof (intro exI conjI)
   203     show "continuous_map ?X01 Y k"
   204       unfolding k_def
   205     proof (rule continuous_map_cases_le)
   206       show fst: "continuous_map ?X01 euclideanreal fst"
   207         using continuous_map_fst continuous_map_in_subtopology by blast
   208       show "continuous_map ?X01 euclideanreal (\<lambda>x. 1/2)"
   209         by simp
   210       show "continuous_map (subtopology ?X01 {y \<in> topspace ?X01. fst y \<le> 1/2}) Y
   211                (k1 \<circ> (\<lambda>x. (2 *\<^sub>R fst x, snd x)))"
   212         apply (rule fst continuous_map_compose [OF _ contk1] continuous_intros continuous_map_into_subtopology | simp)+
   213           apply (intro continuous_intros fst continuous_map_from_subtopology)
   214          apply (force simp: prod_topology_subtopology)
   215         using continuous_map_snd continuous_map_from_subtopology by blast
   216       show "continuous_map (subtopology ?X01 {y \<in> topspace ?X01. 1/2 \<le> fst y}) Y
   217                (k2 \<circ> (\<lambda>x. (2 *\<^sub>R fst x -1, snd x)))"
   218         apply (rule fst continuous_map_compose [OF _ contk2] continuous_intros continuous_map_into_subtopology | simp)+
   219           apply (rule continuous_intros fst continuous_map_from_subtopology | simp)+
   220          apply (force simp: topspace_subtopology prod_topology_subtopology)
   221         using continuous_map_snd  continuous_map_from_subtopology by blast
   222       show "(k1 \<circ> (\<lambda>x. (2 *\<^sub>R fst x, snd x))) y = (k2 \<circ> (\<lambda>x. (2 *\<^sub>R fst x -1, snd x))) y"
   223         if "y \<in> topspace ?X01" and "fst y = 1/2" for y
   224         using that by (simp add: keq)
   225     qed
   226     show "\<forall>x. k (0, x) = f x"
   227       by (simp add: k12 k_def)
   228     show "\<forall>x. k (1, x) = h x"
   229       by (simp add: k12 k_def)
   230     show "\<forall>t\<in>{0..1}. P (\<lambda>x. k (t, x))"
   231       using P
   232       apply (clarsimp simp add: k_def)
   233       apply (case_tac "t \<le> 1/2", auto)
   234       done
   235   qed
   236 qed
   237 
   238 lemma homotopic_with_id2: 
   239   "(\<And>x. x \<in> topspace X \<Longrightarrow> g (f x) = x) \<Longrightarrow> homotopic_with (\<lambda>x. True) X X (g \<circ> f) id"
   240   by (metis comp_apply continuous_map_id eq_id_iff homotopic_with_equal homotopic_with_symD)
   241 
   242 subsection\<open>Continuity lemmas\<close>
   243 
   244 lemma homotopic_with_compose_continuous_map_left:
   245   "\<lbrakk>homotopic_with p X1 X2 f g; continuous_map X2 X3 h; \<And>j. p j \<Longrightarrow> q(h \<circ> j)\<rbrakk>
   246    \<Longrightarrow> homotopic_with q X1 X3 (h \<circ> f) (h \<circ> g)"
   247   unfolding homotopic_with_def
   248   apply clarify
   249   apply (rename_tac k)
   250   apply (rule_tac x="h \<circ> k" in exI)
   251   apply (rule conjI continuous_map_compose | simp add: o_def)+
   252   done
   253 
   254 lemma homotopic_compose_continuous_map_left:
   255    "\<lbrakk>homotopic_with (\<lambda>k. True) X1 X2 f g; continuous_map X2 X3 h\<rbrakk>
   256         \<Longrightarrow> homotopic_with (\<lambda>k. True) X1 X3 (h \<circ> f) (h \<circ> g)"
   257   by (simp add: homotopic_with_compose_continuous_map_left)
   258 
   259 lemma homotopic_with_compose_continuous_map_right:
   260   assumes hom: "homotopic_with p X2 X3 f g" and conth: "continuous_map X1 X2 h"
   261     and q: "\<And>j. p j \<Longrightarrow> q(j \<circ> h)"
   262   shows "homotopic_with q X1 X3 (f \<circ> h) (g \<circ> h)"
   263 proof -
   264   obtain k
   265     where contk: "continuous_map (prod_topology (subtopology euclideanreal {0..1}) X2) X3 k"
   266       and k: "\<forall>x. k (0, x) = f x" "\<forall>x. k (1, x) = g x" and p: "\<And>t. t\<in>{0..1} \<Longrightarrow> p (\<lambda>x. k (t, x))"
   267     using hom unfolding homotopic_with_def by blast
   268   have hsnd: "continuous_map (prod_topology (subtopology euclideanreal {0..1}) X1) X2 (h \<circ> snd)"
   269     by (rule continuous_map_compose [OF continuous_map_snd conth])
   270   let ?h = "k \<circ> (\<lambda>(t,x). (t,h x))"
   271   show ?thesis
   272     unfolding homotopic_with_def
   273   proof (intro exI conjI allI ballI)
   274     have "continuous_map (prod_topology (top_of_set {0..1}) X1)
   275      (prod_topology (top_of_set {0..1::real}) X2) (\<lambda>(t, x). (t, h x))"
   276       by (metis (mono_tags, lifting) case_prod_beta' comp_def continuous_map_eq continuous_map_fst continuous_map_pairedI hsnd)
   277     then show "continuous_map (prod_topology (subtopology euclideanreal {0..1}) X1) X3 ?h"
   278       by (intro conjI continuous_map_compose [OF _ contk])
   279     show "q (\<lambda>x. ?h (t, x))" if "t \<in> {0..1}" for t
   280       using q [OF p [OF that]] by (simp add: o_def)
   281   qed (auto simp: k)
   282 qed
   283 
   284 lemma homotopic_compose_continuous_map_right:
   285    "\<lbrakk>homotopic_with (\<lambda>k. True) X2 X3 f g; continuous_map X1 X2 h\<rbrakk>
   286         \<Longrightarrow> homotopic_with (\<lambda>k. True) X1 X3 (f \<circ> h) (g \<circ> h)"
   287   by (meson homotopic_with_compose_continuous_map_right)
   288 
   289 corollary homotopic_compose:
   290       shows "\<lbrakk>homotopic_with (\<lambda>x. True) X Y f f'; homotopic_with (\<lambda>x. True) Y Z g g'\<rbrakk>
   291              \<Longrightarrow> homotopic_with (\<lambda>x. True) X Z (g \<circ> f) (g' \<circ> f')"
   292   apply (rule homotopic_with_trans [where g = "g \<circ> f'"])
   293   apply (simp add: homotopic_compose_continuous_map_left homotopic_with_imp_continuous_maps)
   294   by (simp add: homotopic_compose_continuous_map_right homotopic_with_imp_continuous_maps)
   295 
   296 
   297 
   298 proposition homotopic_with_compose_continuous_right:
   299     "\<lbrakk>homotopic_with_canon (\<lambda>f. p (f \<circ> h)) X Y f g; continuous_on W h; h ` W \<subseteq> X\<rbrakk>
   300      \<Longrightarrow> homotopic_with_canon p W Y (f \<circ> h) (g \<circ> h)"
   301   apply (clarsimp simp add: homotopic_with_def)
   302   apply (rename_tac k)
   303   apply (rule_tac x="k \<circ> (\<lambda>y. (fst y, h (snd y)))" in exI)
   304   apply (rule conjI continuous_intros continuous_on_compose [where f=snd and g=h, unfolded o_def] | simp)+
   305   apply (erule continuous_on_subset)
   306   apply (fastforce simp: o_def)+
   307   done
   308 
   309 proposition homotopic_compose_continuous_right:
   310      "\<lbrakk>homotopic_with_canon (\<lambda>f. True) X Y f g; continuous_on W h; h ` W \<subseteq> X\<rbrakk>
   311       \<Longrightarrow> homotopic_with_canon (\<lambda>f. True) W Y (f \<circ> h) (g \<circ> h)"
   312   using homotopic_with_compose_continuous_right by fastforce
   313 
   314 proposition homotopic_with_compose_continuous_left:
   315      "\<lbrakk>homotopic_with_canon (\<lambda>f. p (h \<circ> f)) X Y f g; continuous_on Y h; h ` Y \<subseteq> Z\<rbrakk>
   316       \<Longrightarrow> homotopic_with_canon p X Z (h \<circ> f) (h \<circ> g)"
   317   apply (clarsimp simp add: homotopic_with_def)
   318   apply (rename_tac k)
   319   apply (rule_tac x="h \<circ> k" in exI)
   320   apply (rule conjI continuous_intros continuous_on_compose [where f=snd and g=h, unfolded o_def] | simp)+
   321   apply (erule continuous_on_subset)
   322   apply (fastforce simp: o_def)+
   323   done
   324 
   325 proposition homotopic_compose_continuous_left:
   326    "\<lbrakk>homotopic_with_canon (\<lambda>_. True) X Y f g;
   327      continuous_on Y h; h ` Y \<subseteq> Z\<rbrakk>
   328     \<Longrightarrow> homotopic_with_canon (\<lambda>f. True) X Z (h \<circ> f) (h \<circ> g)"
   329   using homotopic_with_compose_continuous_left by fastforce
   330 
   331 lemma homotopic_from_subtopology:
   332    "homotopic_with P X X' f g \<Longrightarrow> homotopic_with P (subtopology X s) X' f g"
   333   unfolding homotopic_with_def
   334   apply (erule ex_forward)
   335   by (simp add: continuous_map_from_subtopology prod_topology_subtopology(2))
   336 
   337 lemma homotopic_on_emptyI:
   338     assumes "topspace X = {}" "P f" "P g"
   339     shows "homotopic_with P X X' f g"
   340   unfolding homotopic_with_def
   341 proof (intro exI conjI ballI)
   342   show "P (\<lambda>x. (\<lambda>(t,x). if t = 0 then f x else g x) (t, x))" if "t \<in> {0..1}" for t::real
   343     by (cases "t = 0", auto simp: assms)
   344 qed (auto simp: continuous_map_atin assms)
   345 
   346 lemma homotopic_on_empty:
   347    "topspace X = {} \<Longrightarrow> (homotopic_with P X X' f g \<longleftrightarrow> P f \<and> P g)"
   348   using homotopic_on_emptyI homotopic_with_imp_property by metis
   349 
   350 lemma homotopic_with_canon_on_empty [simp]: "homotopic_with_canon (\<lambda>x. True) {} t f g"
   351   by (auto intro: homotopic_with_equal)
   352 
   353 lemma homotopic_constant_maps:
   354    "homotopic_with (\<lambda>x. True) X X' (\<lambda>x. a) (\<lambda>x. b) \<longleftrightarrow>
   355     topspace X = {} \<or> path_component_of X' a b" (is "?lhs = ?rhs")
   356 proof (cases "topspace X = {}")
   357   case False
   358   then obtain c where c: "c \<in> topspace X"
   359     by blast
   360   have "\<exists>g. continuous_map (top_of_set {0..1::real}) X' g \<and> g 0 = a \<and> g 1 = b"
   361     if "x \<in> topspace X" and hom: "homotopic_with (\<lambda>x. True) X X' (\<lambda>x. a) (\<lambda>x. b)" for x
   362   proof -
   363     obtain h :: "real \<times> 'a \<Rightarrow> 'b"
   364       where conth: "continuous_map (prod_topology (top_of_set {0..1}) X) X' h"
   365         and h: "\<And>x. h (0, x) = a" "\<And>x. h (1, x) = b"
   366       using hom by (auto simp: homotopic_with_def)
   367     have cont: "continuous_map (top_of_set {0..1}) X' (h \<circ> (\<lambda>t. (t, c)))"
   368       apply (rule continuous_map_compose [OF _ conth])
   369       apply (rule continuous_intros c | simp)+
   370       done
   371     then show ?thesis
   372       by (force simp: h)
   373   qed
   374   moreover have "homotopic_with (\<lambda>x. True) X X' (\<lambda>x. g 0) (\<lambda>x. g 1)"
   375     if "x \<in> topspace X" "a = g 0" "b = g 1" "continuous_map (top_of_set {0..1}) X' g"
   376     for x and g :: "real \<Rightarrow> 'b"
   377     unfolding homotopic_with_def
   378     by (force intro!: continuous_map_compose continuous_intros c that)
   379   ultimately show ?thesis
   380     using False by (auto simp: path_component_of_def pathin_def)
   381 qed (simp add: homotopic_on_empty)
   382 
   383 proposition homotopic_with_eq:
   384    assumes h: "homotopic_with P X Y f g"
   385        and f': "\<And>x. x \<in> topspace X \<Longrightarrow> f' x = f x"
   386        and g': "\<And>x. x \<in> topspace X \<Longrightarrow> g' x = g x"
   387        and P:  "(\<And>h k. (\<And>x. x \<in> topspace X \<Longrightarrow> h x = k x) \<Longrightarrow> P h \<longleftrightarrow> P k)"
   388    shows "homotopic_with P X Y f' g'"
   389   using h unfolding homotopic_with_def
   390   apply safe
   391   apply (rule_tac x="\<lambda>(u,v). if v \<in> topspace X then h(u,v) else if u = 0 then f' v else g' v" in exI)
   392   apply (simp add: f' g', safe)
   393   apply (fastforce intro: continuous_map_eq)
   394   apply (subst P; fastforce)
   395   done
   396 
   397 lemma homotopic_with_prod_topology:
   398   assumes "homotopic_with p X1 Y1 f f'" and "homotopic_with q X2 Y2 g g'"
   399     and r: "\<And>i j. \<lbrakk>p i; q j\<rbrakk> \<Longrightarrow> r(\<lambda>(x,y). (i x, j y))"
   400   shows "homotopic_with r (prod_topology X1 X2) (prod_topology Y1 Y2)
   401                           (\<lambda>z. (f(fst z),g(snd z))) (\<lambda>z. (f'(fst z), g'(snd z)))"
   402 proof -
   403   obtain h
   404     where h: "continuous_map (prod_topology (subtopology euclideanreal {0..1}) X1) Y1 h"
   405       and h0: "\<And>x. h (0, x) = f x"
   406       and h1: "\<And>x. h (1, x) = f' x"
   407       and p: "\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> p (\<lambda>x. h (t,x))"
   408     using assms unfolding homotopic_with_def by auto
   409   obtain k
   410     where k: "continuous_map (prod_topology (subtopology euclideanreal {0..1}) X2) Y2 k"
   411       and k0: "\<And>x. k (0, x) = g x"
   412       and k1: "\<And>x. k (1, x) = g' x"
   413       and q: "\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> q (\<lambda>x. k (t,x))"
   414     using assms unfolding homotopic_with_def by auto
   415   let ?hk = "\<lambda>(t,x,y). (h(t,x), k(t,y))"
   416   show ?thesis
   417     unfolding homotopic_with_def
   418   proof (intro conjI allI exI)
   419     show "continuous_map (prod_topology (subtopology euclideanreal {0..1}) (prod_topology X1 X2))
   420                          (prod_topology Y1 Y2) ?hk"
   421       unfolding continuous_map_pairwise case_prod_unfold
   422       by (rule conjI continuous_map_pairedI continuous_intros continuous_map_id [unfolded id_def]
   423           continuous_map_fst_of [unfolded o_def] continuous_map_snd_of [unfolded o_def]
   424           continuous_map_compose [OF _ h, unfolded o_def]
   425           continuous_map_compose [OF _ k, unfolded o_def])+
   426   next
   427     fix x
   428     show "?hk (0, x) = (f (fst x), g (snd x))" "?hk (1, x) = (f' (fst x), g' (snd x))"
   429       by (auto simp: case_prod_beta h0 k0 h1 k1)
   430   qed (auto simp: p q r)
   431 qed
   432 
   433 
   434 lemma homotopic_with_product_topology:
   435   assumes ht: "\<And>i. i \<in> I \<Longrightarrow> homotopic_with (p i) (X i) (Y i) (f i) (g i)"
   436     and pq: "\<And>h. (\<And>i. i \<in> I \<Longrightarrow> p i (h i)) \<Longrightarrow> q(\<lambda>x. (\<lambda>i\<in>I. h i (x i)))"
   437   shows "homotopic_with q (product_topology X I) (product_topology Y I)
   438                           (\<lambda>z. (\<lambda>i\<in>I. (f i) (z i))) (\<lambda>z. (\<lambda>i\<in>I. (g i) (z i)))"
   439 proof -
   440   obtain h
   441     where h: "\<And>i. i \<in> I \<Longrightarrow> continuous_map (prod_topology (subtopology euclideanreal {0..1}) (X i)) (Y i) (h i)"
   442       and h0: "\<And>i x. i \<in> I \<Longrightarrow> h i (0, x) = f i x"
   443       and h1: "\<And>i x. i \<in> I \<Longrightarrow> h i (1, x) = g i x"
   444       and p: "\<And>i t. \<lbrakk>i \<in> I; t \<in> {0..1}\<rbrakk> \<Longrightarrow> p i (\<lambda>x. h i (t,x))"
   445     using ht unfolding homotopic_with_def by metis
   446   show ?thesis
   447     unfolding homotopic_with_def
   448   proof (intro conjI allI exI)
   449     let ?h = "\<lambda>(t,z). \<lambda>i\<in>I. h i (t,z i)"
   450     have "continuous_map (prod_topology (subtopology euclideanreal {0..1}) (product_topology X I))
   451                          (Y i) (\<lambda>x. h i (fst x, snd x i))" if "i \<in> I" for i
   452       unfolding continuous_map_pairwise case_prod_unfold
   453       apply (intro conjI that  continuous_intros continuous_map_compose [OF _ h, unfolded o_def])
   454       using continuous_map_componentwise continuous_map_snd that apply fastforce
   455       done
   456     then show "continuous_map (prod_topology (subtopology euclideanreal {0..1}) (product_topology X I))
   457          (product_topology Y I) ?h"
   458       by (auto simp: continuous_map_componentwise case_prod_beta)
   459     show "?h (0, x) = (\<lambda>i\<in>I. f i (x i))" "?h (1, x) = (\<lambda>i\<in>I. g i (x i))" for x
   460       by (auto simp: case_prod_beta h0 h1)
   461     show "\<forall>t\<in>{0..1}. q (\<lambda>x. ?h (t, x))"
   462       by (force intro: p pq)
   463   qed
   464 qed
   465 
   466 text\<open>Homotopic triviality implicitly incorporates path-connectedness.\<close>
   467 lemma homotopic_triviality:
   468   shows  "(\<forall>f g. continuous_on S f \<and> f ` S \<subseteq> T \<and>
   469                  continuous_on S g \<and> g ` S \<subseteq> T
   470                  \<longrightarrow> homotopic_with_canon (\<lambda>x. True) S T f g) \<longleftrightarrow>
   471           (S = {} \<or> path_connected T) \<and>
   472           (\<forall>f. continuous_on S f \<and> f ` S \<subseteq> T \<longrightarrow> (\<exists>c. homotopic_with_canon (\<lambda>x. True) S T f (\<lambda>x. c)))"
   473           (is "?lhs = ?rhs")
   474 proof (cases "S = {} \<or> T = {}")
   475   case True then show ?thesis
   476     by (auto simp: homotopic_on_emptyI)
   477 next
   478   case False show ?thesis
   479   proof
   480     assume LHS [rule_format]: ?lhs
   481     have pab: "path_component T a b" if "a \<in> T" "b \<in> T" for a b
   482     proof -
   483       have "homotopic_with_canon (\<lambda>x. True) S T (\<lambda>x. a) (\<lambda>x. b)"
   484         by (simp add: LHS continuous_on_const image_subset_iff that)
   485       then show ?thesis
   486         using False homotopic_constant_maps [of "top_of_set S" "top_of_set T" a b] by auto
   487     qed
   488     moreover
   489     have "\<exists>c. homotopic_with_canon (\<lambda>x. True) S T f (\<lambda>x. c)" if "continuous_on S f" "f ` S \<subseteq> T" for f
   490       using False LHS continuous_on_const that by blast
   491     ultimately show ?rhs
   492       by (simp add: path_connected_component)
   493   next
   494     assume RHS: ?rhs
   495     with False have T: "path_connected T"
   496       by blast
   497     show ?lhs
   498     proof clarify
   499       fix f g
   500       assume "continuous_on S f" "f ` S \<subseteq> T" "continuous_on S g" "g ` S \<subseteq> T"
   501       obtain c d where c: "homotopic_with_canon (\<lambda>x. True) S T f (\<lambda>x. c)" and d: "homotopic_with_canon (\<lambda>x. True) S T g (\<lambda>x. d)"
   502         using False \<open>continuous_on S f\<close> \<open>f ` S \<subseteq> T\<close>  RHS \<open>continuous_on S g\<close> \<open>g ` S \<subseteq> T\<close> by blast
   503       then have "c \<in> T" "d \<in> T"
   504         using False homotopic_with_imp_continuous_maps by fastforce+
   505       with T have "path_component T c d"
   506         using path_connected_component by blast
   507       then have "homotopic_with_canon (\<lambda>x. True) S T (\<lambda>x. c) (\<lambda>x. d)"
   508         by (simp add: homotopic_constant_maps)
   509       with c d show "homotopic_with_canon (\<lambda>x. True) S T f g"
   510         by (meson homotopic_with_symD homotopic_with_trans)
   511     qed
   512   qed
   513 qed
   514 
   515 
   516 subsection\<open>Homotopy of paths, maintaining the same endpoints\<close>
   517 
   518 
   519 definition%important homotopic_paths :: "['a set, real \<Rightarrow> 'a, real \<Rightarrow> 'a::topological_space] \<Rightarrow> bool"
   520   where
   521      "homotopic_paths s p q \<equiv>
   522        homotopic_with_canon (\<lambda>r. pathstart r = pathstart p \<and> pathfinish r = pathfinish p) {0..1} s p q"
   523 
   524 lemma homotopic_paths:
   525    "homotopic_paths s p q \<longleftrightarrow>
   526       (\<exists>h. continuous_on ({0..1} \<times> {0..1}) h \<and>
   527           h ` ({0..1} \<times> {0..1}) \<subseteq> s \<and>
   528           (\<forall>x \<in> {0..1}. h(0,x) = p x) \<and>
   529           (\<forall>x \<in> {0..1}. h(1,x) = q x) \<and>
   530           (\<forall>t \<in> {0..1::real}. pathstart(h \<circ> Pair t) = pathstart p \<and>
   531                         pathfinish(h \<circ> Pair t) = pathfinish p))"
   532   by (auto simp: homotopic_paths_def homotopic_with pathstart_def pathfinish_def)
   533 
   534 proposition homotopic_paths_imp_pathstart:
   535      "homotopic_paths s p q \<Longrightarrow> pathstart p = pathstart q"
   536   by (metis (mono_tags, lifting) homotopic_paths_def homotopic_with_imp_property)
   537 
   538 proposition homotopic_paths_imp_pathfinish:
   539      "homotopic_paths s p q \<Longrightarrow> pathfinish p = pathfinish q"
   540   by (metis (mono_tags, lifting) homotopic_paths_def homotopic_with_imp_property)
   541 
   542 lemma homotopic_paths_imp_path:
   543      "homotopic_paths s p q \<Longrightarrow> path p \<and> path q"
   544   using homotopic_paths_def homotopic_with_imp_continuous_maps path_def continuous_map_subtopology_eu by blast
   545 
   546 lemma homotopic_paths_imp_subset:
   547      "homotopic_paths s p q \<Longrightarrow> path_image p \<subseteq> s \<and> path_image q \<subseteq> s"
   548   by (metis (mono_tags) continuous_map_subtopology_eu homotopic_paths_def homotopic_with_imp_continuous_maps path_image_def)
   549 
   550 proposition homotopic_paths_refl [simp]: "homotopic_paths s p p \<longleftrightarrow> path p \<and> path_image p \<subseteq> s"
   551   by (simp add: homotopic_paths_def path_def path_image_def)
   552 
   553 proposition homotopic_paths_sym: "homotopic_paths s p q \<Longrightarrow> homotopic_paths s q p"
   554   by (metis (mono_tags) homotopic_paths_def homotopic_paths_imp_pathfinish homotopic_paths_imp_pathstart homotopic_with_symD)
   555 
   556 proposition homotopic_paths_sym_eq: "homotopic_paths s p q \<longleftrightarrow> homotopic_paths s q p"
   557   by (metis homotopic_paths_sym)
   558 
   559 proposition homotopic_paths_trans [trans]:
   560   assumes "homotopic_paths s p q" "homotopic_paths s q r"
   561   shows "homotopic_paths s p r"
   562 proof -
   563   have "pathstart q = pathstart p" "pathfinish q = pathfinish p"
   564     using assms by (simp_all add: homotopic_paths_imp_pathstart homotopic_paths_imp_pathfinish)
   565   then have "homotopic_with_canon (\<lambda>f. pathstart f = pathstart p \<and> pathfinish f = pathfinish p) {0..1} s q r"
   566     using \<open>homotopic_paths s q r\<close> homotopic_paths_def by force
   567   then show ?thesis
   568     using assms homotopic_paths_def homotopic_with_trans by blast
   569 qed
   570 
   571 proposition homotopic_paths_eq:
   572      "\<lbrakk>path p; path_image p \<subseteq> s; \<And>t. t \<in> {0..1} \<Longrightarrow> p t = q t\<rbrakk> \<Longrightarrow> homotopic_paths s p q"
   573   apply (simp add: homotopic_paths_def)
   574   apply (rule homotopic_with_eq)
   575   apply (auto simp: path_def homotopic_with_refl pathstart_def pathfinish_def path_image_def elim: continuous_on_eq)
   576   done
   577 
   578 proposition homotopic_paths_reparametrize:
   579   assumes "path p"
   580       and pips: "path_image p \<subseteq> s"
   581       and contf: "continuous_on {0..1} f"
   582       and f01:"f ` {0..1} \<subseteq> {0..1}"
   583       and [simp]: "f(0) = 0" "f(1) = 1"
   584       and q: "\<And>t. t \<in> {0..1} \<Longrightarrow> q(t) = p(f t)"
   585     shows "homotopic_paths s p q"
   586 proof -
   587   have contp: "continuous_on {0..1} p"
   588     by (metis \<open>path p\<close> path_def)
   589   then have "continuous_on {0..1} (p \<circ> f)"
   590     using contf continuous_on_compose continuous_on_subset f01 by blast
   591   then have "path q"
   592     by (simp add: path_def) (metis q continuous_on_cong)
   593   have piqs: "path_image q \<subseteq> s"
   594     by (metis (no_types, hide_lams) pips f01 image_subset_iff path_image_def q)
   595   have fb0: "\<And>a b. \<lbrakk>0 \<le> a; a \<le> 1; 0 \<le> b; b \<le> 1\<rbrakk> \<Longrightarrow> 0 \<le> (1 - a) * f b + a * b"
   596     using f01 by force
   597   have fb1: "\<lbrakk>0 \<le> a; a \<le> 1; 0 \<le> b; b \<le> 1\<rbrakk> \<Longrightarrow> (1 - a) * f b + a * b \<le> 1" for a b
   598     using f01 [THEN subsetD, of "f b"] by (simp add: convex_bound_le)
   599   have "homotopic_paths s q p"
   600   proof (rule homotopic_paths_trans)
   601     show "homotopic_paths s q (p \<circ> f)"
   602       using q by (force intro: homotopic_paths_eq [OF  \<open>path q\<close> piqs])
   603   next
   604     show "homotopic_paths s (p \<circ> f) p"
   605       apply (simp add: homotopic_paths_def homotopic_with_def)
   606       apply (rule_tac x="p \<circ> (\<lambda>y. (1 - (fst y)) *\<^sub>R ((f \<circ> snd) y) + (fst y) *\<^sub>R snd y)"  in exI)
   607       apply (rule conjI contf continuous_intros continuous_on_subset [OF contp] | simp)+
   608       using pips [unfolded path_image_def]
   609       apply (auto simp: fb0 fb1 pathstart_def pathfinish_def)
   610       done
   611   qed
   612   then show ?thesis
   613     by (simp add: homotopic_paths_sym)
   614 qed
   615 
   616 lemma homotopic_paths_subset: "\<lbrakk>homotopic_paths s p q; s \<subseteq> t\<rbrakk> \<Longrightarrow> homotopic_paths t p q"
   617   unfolding homotopic_paths by fast
   618 
   619 
   620 text\<open> A slightly ad-hoc but useful lemma in constructing homotopies.\<close>
   621 lemma homotopic_join_lemma:
   622   fixes q :: "[real,real] \<Rightarrow> 'a::topological_space"
   623   assumes p: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>y. p (fst y) (snd y))"
   624       and q: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>y. q (fst y) (snd y))"
   625       and pf: "\<And>t. t \<in> {0..1} \<Longrightarrow> pathfinish(p t) = pathstart(q t)"
   626     shows "continuous_on ({0..1} \<times> {0..1}) (\<lambda>y. (p(fst y) +++ q(fst y)) (snd y))"
   627 proof -
   628   have 1: "(\<lambda>y. p (fst y) (2 * snd y)) = (\<lambda>y. p (fst y) (snd y)) \<circ> (\<lambda>y. (fst y, 2 * snd y))"
   629     by (rule ext) (simp)
   630   have 2: "(\<lambda>y. q (fst y) (2 * snd y - 1)) = (\<lambda>y. q (fst y) (snd y)) \<circ> (\<lambda>y. (fst y, 2 * snd y - 1))"
   631     by (rule ext) (simp)
   632   show ?thesis
   633     apply (simp add: joinpaths_def)
   634     apply (rule continuous_on_cases_le)
   635     apply (simp_all only: 1 2)
   636     apply (rule continuous_intros continuous_on_subset [OF p] continuous_on_subset [OF q] | force)+
   637     using pf
   638     apply (auto simp: mult.commute pathstart_def pathfinish_def)
   639     done
   640 qed
   641 
   642 text\<open> Congruence properties of homotopy w.r.t. path-combining operations.\<close>
   643 
   644 lemma homotopic_paths_reversepath_D:
   645       assumes "homotopic_paths s p q"
   646       shows   "homotopic_paths s (reversepath p) (reversepath q)"
   647   using assms
   648   apply (simp add: homotopic_paths_def homotopic_with_def, clarify)
   649   apply (rule_tac x="h \<circ> (\<lambda>x. (fst x, 1 - snd x))" in exI)
   650   apply (rule conjI continuous_intros)+
   651   apply (auto simp: reversepath_def pathstart_def pathfinish_def elim!: continuous_on_subset)
   652   done
   653 
   654 proposition homotopic_paths_reversepath:
   655      "homotopic_paths s (reversepath p) (reversepath q) \<longleftrightarrow> homotopic_paths s p q"
   656   using homotopic_paths_reversepath_D by force
   657 
   658 
   659 proposition homotopic_paths_join:
   660     "\<lbrakk>homotopic_paths s p p'; homotopic_paths s q q'; pathfinish p = pathstart q\<rbrakk> \<Longrightarrow> homotopic_paths s (p +++ q) (p' +++ q')"
   661   apply (simp add: homotopic_paths_def homotopic_with_def, clarify)
   662   apply (rename_tac k1 k2)
   663   apply (rule_tac x="(\<lambda>y. ((k1 \<circ> Pair (fst y)) +++ (k2 \<circ> Pair (fst y))) (snd y))" in exI)
   664   apply (rule conjI continuous_intros homotopic_join_lemma)+
   665   apply (auto simp: joinpaths_def pathstart_def pathfinish_def path_image_def)
   666   done
   667 
   668 proposition homotopic_paths_continuous_image:
   669     "\<lbrakk>homotopic_paths s f g; continuous_on s h; h ` s \<subseteq> t\<rbrakk> \<Longrightarrow> homotopic_paths t (h \<circ> f) (h \<circ> g)"
   670   unfolding homotopic_paths_def
   671   by (simp add: homotopic_with_compose_continuous_map_left pathfinish_compose pathstart_compose)
   672 
   673 
   674 subsection\<open>Group properties for homotopy of paths\<close>
   675 
   676 text%important\<open>So taking equivalence classes under homotopy would give the fundamental group\<close>
   677 
   678 proposition homotopic_paths_rid:
   679     "\<lbrakk>path p; path_image p \<subseteq> s\<rbrakk> \<Longrightarrow> homotopic_paths s (p +++ linepath (pathfinish p) (pathfinish p)) p"
   680   apply (subst homotopic_paths_sym)
   681   apply (rule homotopic_paths_reparametrize [where f = "\<lambda>t. if  t \<le> 1 / 2 then 2 *\<^sub>R t else 1"])
   682   apply (simp_all del: le_divide_eq_numeral1)
   683   apply (subst split_01)
   684   apply (rule continuous_on_cases continuous_intros | force simp: pathfinish_def joinpaths_def)+
   685   done
   686 
   687 proposition homotopic_paths_lid:
   688    "\<lbrakk>path p; path_image p \<subseteq> s\<rbrakk> \<Longrightarrow> homotopic_paths s (linepath (pathstart p) (pathstart p) +++ p) p"
   689   using homotopic_paths_rid [of "reversepath p" s]
   690   by (metis homotopic_paths_reversepath path_image_reversepath path_reversepath pathfinish_linepath
   691         pathfinish_reversepath reversepath_joinpaths reversepath_linepath)
   692 
   693 proposition homotopic_paths_assoc:
   694    "\<lbrakk>path p; path_image p \<subseteq> s; path q; path_image q \<subseteq> s; path r; path_image r \<subseteq> s; pathfinish p = pathstart q;
   695      pathfinish q = pathstart r\<rbrakk>
   696     \<Longrightarrow> homotopic_paths s (p +++ (q +++ r)) ((p +++ q) +++ r)"
   697   apply (subst homotopic_paths_sym)
   698   apply (rule homotopic_paths_reparametrize
   699            [where f = "\<lambda>t. if  t \<le> 1 / 2 then inverse 2 *\<^sub>R t
   700                            else if  t \<le> 3 / 4 then t - (1 / 4)
   701                            else 2 *\<^sub>R t - 1"])
   702   apply (simp_all del: le_divide_eq_numeral1)
   703   apply (simp add: subset_path_image_join)
   704   apply (rule continuous_on_cases_1 continuous_intros)+
   705   apply (auto simp: joinpaths_def)
   706   done
   707 
   708 proposition homotopic_paths_rinv:
   709   assumes "path p" "path_image p \<subseteq> s"
   710     shows "homotopic_paths s (p +++ reversepath p) (linepath (pathstart p) (pathstart p))"
   711 proof -
   712   have "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. (subpath 0 (fst x) p +++ reversepath (subpath 0 (fst x) p)) (snd x))"
   713     using assms
   714     apply (simp add: joinpaths_def subpath_def reversepath_def path_def del: le_divide_eq_numeral1)
   715     apply (rule continuous_on_cases_le)
   716     apply (rule_tac [2] continuous_on_compose [of _ _ p, unfolded o_def])
   717     apply (rule continuous_on_compose [of _ _ p, unfolded o_def])
   718     apply (auto intro!: continuous_intros simp del: eq_divide_eq_numeral1)
   719     apply (force elim!: continuous_on_subset simp add: mult_le_one)+
   720     done
   721   then show ?thesis
   722     using assms
   723     apply (subst homotopic_paths_sym_eq)
   724     unfolding homotopic_paths_def homotopic_with_def
   725     apply (rule_tac x="(\<lambda>y. (subpath 0 (fst y) p +++ reversepath(subpath 0 (fst y) p)) (snd y))" in exI)
   726     apply (simp add: path_defs joinpaths_def subpath_def reversepath_def)
   727     apply (force simp: mult_le_one)
   728     done
   729 qed
   730 
   731 proposition homotopic_paths_linv:
   732   assumes "path p" "path_image p \<subseteq> s"
   733     shows "homotopic_paths s (reversepath p +++ p) (linepath (pathfinish p) (pathfinish p))"
   734   using homotopic_paths_rinv [of "reversepath p" s] assms by simp
   735 
   736 
   737 subsection\<open>Homotopy of loops without requiring preservation of endpoints\<close>
   738 
   739 definition%important homotopic_loops :: "'a::topological_space set \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> bool"  where
   740  "homotopic_loops s p q \<equiv>
   741      homotopic_with_canon (\<lambda>r. pathfinish r = pathstart r) {0..1} s p q"
   742 
   743 lemma homotopic_loops:
   744    "homotopic_loops s p q \<longleftrightarrow>
   745       (\<exists>h. continuous_on ({0..1::real} \<times> {0..1}) h \<and>
   746           image h ({0..1} \<times> {0..1}) \<subseteq> s \<and>
   747           (\<forall>x \<in> {0..1}. h(0,x) = p x) \<and>
   748           (\<forall>x \<in> {0..1}. h(1,x) = q x) \<and>
   749           (\<forall>t \<in> {0..1}. pathfinish(h \<circ> Pair t) = pathstart(h \<circ> Pair t)))"
   750   by (simp add: homotopic_loops_def pathstart_def pathfinish_def homotopic_with)
   751 
   752 proposition homotopic_loops_imp_loop:
   753      "homotopic_loops s p q \<Longrightarrow> pathfinish p = pathstart p \<and> pathfinish q = pathstart q"
   754 using homotopic_with_imp_property homotopic_loops_def by blast
   755 
   756 proposition homotopic_loops_imp_path:
   757      "homotopic_loops s p q \<Longrightarrow> path p \<and> path q"
   758   unfolding homotopic_loops_def path_def
   759   using homotopic_with_imp_continuous_maps continuous_map_subtopology_eu by blast
   760 
   761 proposition homotopic_loops_imp_subset:
   762      "homotopic_loops s p q \<Longrightarrow> path_image p \<subseteq> s \<and> path_image q \<subseteq> s"
   763   unfolding homotopic_loops_def path_image_def
   764   by (meson continuous_map_subtopology_eu homotopic_with_imp_continuous_maps)
   765 
   766 proposition homotopic_loops_refl:
   767      "homotopic_loops s p p \<longleftrightarrow>
   768       path p \<and> path_image p \<subseteq> s \<and> pathfinish p = pathstart p"
   769   by (simp add: homotopic_loops_def path_image_def path_def)
   770 
   771 proposition homotopic_loops_sym: "homotopic_loops s p q \<Longrightarrow> homotopic_loops s q p"
   772   by (simp add: homotopic_loops_def homotopic_with_sym)
   773 
   774 proposition homotopic_loops_sym_eq: "homotopic_loops s p q \<longleftrightarrow> homotopic_loops s q p"
   775   by (metis homotopic_loops_sym)
   776 
   777 proposition homotopic_loops_trans:
   778    "\<lbrakk>homotopic_loops s p q; homotopic_loops s q r\<rbrakk> \<Longrightarrow> homotopic_loops s p r"
   779   unfolding homotopic_loops_def by (blast intro: homotopic_with_trans)
   780 
   781 proposition homotopic_loops_subset:
   782    "\<lbrakk>homotopic_loops s p q; s \<subseteq> t\<rbrakk> \<Longrightarrow> homotopic_loops t p q"
   783   by (fastforce simp add: homotopic_loops)
   784 
   785 proposition homotopic_loops_eq:
   786    "\<lbrakk>path p; path_image p \<subseteq> s; pathfinish p = pathstart p; \<And>t. t \<in> {0..1} \<Longrightarrow> p(t) = q(t)\<rbrakk>
   787           \<Longrightarrow> homotopic_loops s p q"
   788   unfolding homotopic_loops_def
   789   apply (rule homotopic_with_eq)
   790   apply (rule homotopic_with_refl [where f = p, THEN iffD2])
   791   apply (simp_all add: path_image_def path_def pathstart_def pathfinish_def)
   792   done
   793 
   794 proposition homotopic_loops_continuous_image:
   795    "\<lbrakk>homotopic_loops s f g; continuous_on s h; h ` s \<subseteq> t\<rbrakk> \<Longrightarrow> homotopic_loops t (h \<circ> f) (h \<circ> g)"
   796   unfolding homotopic_loops_def
   797   by (simp add: homotopic_with_compose_continuous_map_left pathfinish_def pathstart_def)
   798 
   799 
   800 subsection\<open>Relations between the two variants of homotopy\<close>
   801 
   802 proposition homotopic_paths_imp_homotopic_loops:
   803     "\<lbrakk>homotopic_paths s p q; pathfinish p = pathstart p; pathfinish q = pathstart p\<rbrakk> \<Longrightarrow> homotopic_loops s p q"
   804   by (auto simp: homotopic_with_def homotopic_paths_def homotopic_loops_def)
   805 
   806 proposition homotopic_loops_imp_homotopic_paths_null:
   807   assumes "homotopic_loops s p (linepath a a)"
   808     shows "homotopic_paths s p (linepath (pathstart p) (pathstart p))"
   809 proof -
   810   have "path p" by (metis assms homotopic_loops_imp_path)
   811   have ploop: "pathfinish p = pathstart p" by (metis assms homotopic_loops_imp_loop)
   812   have pip: "path_image p \<subseteq> s" by (metis assms homotopic_loops_imp_subset)
   813   obtain h where conth: "continuous_on ({0..1::real} \<times> {0..1}) h"
   814              and hs: "h ` ({0..1} \<times> {0..1}) \<subseteq> s"
   815              and [simp]: "\<And>x. x \<in> {0..1} \<Longrightarrow> h(0,x) = p x"
   816              and [simp]: "\<And>x. x \<in> {0..1} \<Longrightarrow> h(1,x) = a"
   817              and ends: "\<And>t. t \<in> {0..1} \<Longrightarrow> pathfinish (h \<circ> Pair t) = pathstart (h \<circ> Pair t)"
   818     using assms by (auto simp: homotopic_loops homotopic_with)
   819   have conth0: "path (\<lambda>u. h (u, 0))"
   820     unfolding path_def
   821     apply (rule continuous_on_compose [of _ _ h, unfolded o_def])
   822     apply (force intro: continuous_intros continuous_on_subset [OF conth])+
   823     done
   824   have pih0: "path_image (\<lambda>u. h (u, 0)) \<subseteq> s"
   825     using hs by (force simp: path_image_def)
   826   have c1: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. h (fst x * snd x, 0))"
   827     apply (rule continuous_on_compose [of _ _ h, unfolded o_def])
   828     apply (force simp: mult_le_one intro: continuous_intros continuous_on_subset [OF conth])+
   829     done
   830   have c2: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. h (fst x - fst x * snd x, 0))"
   831     apply (rule continuous_on_compose [of _ _ h, unfolded o_def])
   832     apply (force simp: mult_left_le mult_le_one intro: continuous_intros continuous_on_subset [OF conth])+
   833     apply (rule continuous_on_subset [OF conth])
   834     apply (auto simp: algebra_simps add_increasing2 mult_left_le)
   835     done
   836   have [simp]: "\<And>t. \<lbrakk>0 \<le> t \<and> t \<le> 1\<rbrakk> \<Longrightarrow> h (t, 1) = h (t, 0)"
   837     using ends by (simp add: pathfinish_def pathstart_def)
   838   have adhoc_le: "c * 4 \<le> 1 + c * (d * 4)" if "\<not> d * 4 \<le> 3" "0 \<le> c" "c \<le> 1" for c d::real
   839   proof -
   840     have "c * 3 \<le> c * (d * 4)" using that less_eq_real_def by auto
   841     with \<open>c \<le> 1\<close> show ?thesis by fastforce
   842   qed
   843   have *: "\<And>p x. (path p \<and> path(reversepath p)) \<and>
   844                   (path_image p \<subseteq> s \<and> path_image(reversepath p) \<subseteq> s) \<and>
   845                   (pathfinish p = pathstart(linepath a a +++ reversepath p) \<and>
   846                    pathstart(reversepath p) = a) \<and> pathstart p = x
   847                   \<Longrightarrow> homotopic_paths s (p +++ linepath a a +++ reversepath p) (linepath x x)"
   848     by (metis homotopic_paths_lid homotopic_paths_join
   849               homotopic_paths_trans homotopic_paths_sym homotopic_paths_rinv)
   850   have 1: "homotopic_paths s p (p +++ linepath (pathfinish p) (pathfinish p))"
   851     using \<open>path p\<close> homotopic_paths_rid homotopic_paths_sym pip by blast
   852   moreover have "homotopic_paths s (p +++ linepath (pathfinish p) (pathfinish p))
   853                                    (linepath (pathstart p) (pathstart p) +++ p +++ linepath (pathfinish p) (pathfinish p))"
   854     apply (rule homotopic_paths_sym)
   855     using homotopic_paths_lid [of "p +++ linepath (pathfinish p) (pathfinish p)" s]
   856     by (metis 1 homotopic_paths_imp_path homotopic_paths_imp_pathstart homotopic_paths_imp_subset)
   857   moreover have "homotopic_paths s (linepath (pathstart p) (pathstart p) +++ p +++ linepath (pathfinish p) (pathfinish p))
   858                                    ((\<lambda>u. h (u, 0)) +++ linepath a a +++ reversepath (\<lambda>u. h (u, 0)))"
   859     apply (simp add: homotopic_paths_def homotopic_with_def)
   860     apply (rule_tac x="\<lambda>y. (subpath 0 (fst y) (\<lambda>u. h (u, 0)) +++ (\<lambda>u. h (Pair (fst y) u)) +++ subpath (fst y) 0 (\<lambda>u. h (u, 0))) (snd y)" in exI)
   861     apply (simp add: subpath_reversepath)
   862     apply (intro conjI homotopic_join_lemma)
   863     using ploop
   864     apply (simp_all add: path_defs joinpaths_def o_def subpath_def conth c1 c2)
   865     apply (force simp: algebra_simps mult_le_one mult_left_le intro: hs [THEN subsetD] adhoc_le)
   866     done
   867   moreover have "homotopic_paths s ((\<lambda>u. h (u, 0)) +++ linepath a a +++ reversepath (\<lambda>u. h (u, 0)))
   868                                    (linepath (pathstart p) (pathstart p))"
   869     apply (rule *)
   870     apply (simp add: pih0 pathstart_def pathfinish_def conth0)
   871     apply (simp add: reversepath_def joinpaths_def)
   872     done
   873   ultimately show ?thesis
   874     by (blast intro: homotopic_paths_trans)
   875 qed
   876 
   877 proposition homotopic_loops_conjugate:
   878   fixes s :: "'a::real_normed_vector set"
   879   assumes "path p" "path q" and pip: "path_image p \<subseteq> s" and piq: "path_image q \<subseteq> s"
   880       and papp: "pathfinish p = pathstart q" and qloop: "pathfinish q = pathstart q"
   881     shows "homotopic_loops s (p +++ q +++ reversepath p) q"
   882 proof -
   883   have contp: "continuous_on {0..1} p"  using \<open>path p\<close> [unfolded path_def] by blast
   884   have contq: "continuous_on {0..1} q"  using \<open>path q\<close> [unfolded path_def] by blast
   885   have c1: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. p ((1 - fst x) * snd x + fst x))"
   886     apply (rule continuous_on_compose [of _ _ p, unfolded o_def])
   887     apply (force simp: mult_le_one intro!: continuous_intros)
   888     apply (rule continuous_on_subset [OF contp])
   889     apply (auto simp: algebra_simps add_increasing2 mult_right_le_one_le sum_le_prod1)
   890     done
   891   have c2: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. p ((fst x - 1) * snd x + 1))"
   892     apply (rule continuous_on_compose [of _ _ p, unfolded o_def])
   893     apply (force simp: mult_le_one intro!: continuous_intros)
   894     apply (rule continuous_on_subset [OF contp])
   895     apply (auto simp: algebra_simps add_increasing2 mult_left_le_one_le)
   896     done
   897   have ps1: "\<And>a b. \<lbrakk>b * 2 \<le> 1; 0 \<le> b; 0 \<le> a; a \<le> 1\<rbrakk> \<Longrightarrow> p ((1 - a) * (2 * b) + a) \<in> s"
   898     using sum_le_prod1
   899     by (force simp: algebra_simps add_increasing2 mult_left_le intro: pip [unfolded path_image_def, THEN subsetD])
   900   have ps2: "\<And>a b. \<lbrakk>\<not> 4 * b \<le> 3; b \<le> 1; 0 \<le> a; a \<le> 1\<rbrakk> \<Longrightarrow> p ((a - 1) * (4 * b - 3) + 1) \<in> s"
   901     apply (rule pip [unfolded path_image_def, THEN subsetD])
   902     apply (rule image_eqI, blast)
   903     apply (simp add: algebra_simps)
   904     by (metis add_mono_thms_linordered_semiring(1) affine_ineq linear mult.commute mult.left_neutral mult_right_mono not_le
   905               add.commute zero_le_numeral)
   906   have qs: "\<And>a b. \<lbrakk>4 * b \<le> 3; \<not> b * 2 \<le> 1\<rbrakk> \<Longrightarrow> q (4 * b - 2) \<in> s"
   907     using path_image_def piq by fastforce
   908   have "homotopic_loops s (p +++ q +++ reversepath p)
   909                           (linepath (pathstart q) (pathstart q) +++ q +++ linepath (pathstart q) (pathstart q))"
   910     apply (simp add: homotopic_loops_def homotopic_with_def)
   911     apply (rule_tac x="(\<lambda>y. (subpath (fst y) 1 p +++ q +++ subpath 1 (fst y) p) (snd y))" in exI)
   912     apply (simp add: subpath_refl subpath_reversepath)
   913     apply (intro conjI homotopic_join_lemma)
   914     using papp qloop
   915     apply (simp_all add: path_defs joinpaths_def o_def subpath_def c1 c2)
   916     apply (force simp: contq intro: continuous_on_compose [of _ _ q, unfolded o_def] continuous_on_id continuous_on_snd)
   917     apply (auto simp: ps1 ps2 qs)
   918     done
   919   moreover have "homotopic_loops s (linepath (pathstart q) (pathstart q) +++ q +++ linepath (pathstart q) (pathstart q)) q"
   920   proof -
   921     have "homotopic_paths s (linepath (pathfinish q) (pathfinish q) +++ q) q"
   922       using \<open>path q\<close> homotopic_paths_lid qloop piq by auto
   923     hence 1: "\<And>f. homotopic_paths s f q \<or> \<not> homotopic_paths s f (linepath (pathfinish q) (pathfinish q) +++ q)"
   924       using homotopic_paths_trans by blast
   925     hence "homotopic_paths s (linepath (pathfinish q) (pathfinish q) +++ q +++ linepath (pathfinish q) (pathfinish q)) q"
   926     proof -
   927       have "homotopic_paths s (q +++ linepath (pathfinish q) (pathfinish q)) q"
   928         by (simp add: \<open>path q\<close> homotopic_paths_rid piq)
   929       thus ?thesis
   930         by (metis (no_types) 1 \<open>path q\<close> homotopic_paths_join homotopic_paths_rinv homotopic_paths_sym
   931                   homotopic_paths_trans qloop pathfinish_linepath piq)
   932     qed
   933     thus ?thesis
   934       by (metis (no_types) qloop homotopic_loops_sym homotopic_paths_imp_homotopic_loops homotopic_paths_imp_pathfinish homotopic_paths_sym)
   935   qed
   936   ultimately show ?thesis
   937     by (blast intro: homotopic_loops_trans)
   938 qed
   939 
   940 lemma homotopic_paths_loop_parts:
   941   assumes loops: "homotopic_loops S (p +++ reversepath q) (linepath a a)" and "path q"
   942   shows "homotopic_paths S p q"
   943 proof -
   944   have paths: "homotopic_paths S (p +++ reversepath q) (linepath (pathstart p) (pathstart p))"
   945     using homotopic_loops_imp_homotopic_paths_null [OF loops] by simp
   946   then have "path p"
   947     using \<open>path q\<close> homotopic_loops_imp_path loops path_join path_join_path_ends path_reversepath by blast
   948   show ?thesis
   949   proof (cases "pathfinish p = pathfinish q")
   950     case True
   951     have pipq: "path_image p \<subseteq> S" "path_image q \<subseteq> S"
   952       by (metis Un_subset_iff paths \<open>path p\<close> \<open>path q\<close> homotopic_loops_imp_subset homotopic_paths_imp_path loops
   953            path_image_join path_image_reversepath path_imp_reversepath path_join_eq)+
   954     have "homotopic_paths S p (p +++ (linepath (pathfinish p) (pathfinish p)))"
   955       using \<open>path p\<close> \<open>path_image p \<subseteq> S\<close> homotopic_paths_rid homotopic_paths_sym by blast
   956     moreover have "homotopic_paths S (p +++ (linepath (pathfinish p) (pathfinish p))) (p +++ (reversepath q +++ q))"
   957       by (simp add: True \<open>path p\<close> \<open>path q\<close> pipq homotopic_paths_join homotopic_paths_linv homotopic_paths_sym)
   958     moreover have "homotopic_paths S (p +++ (reversepath q +++ q)) ((p +++ reversepath q) +++ q)"
   959       by (simp add: True \<open>path p\<close> \<open>path q\<close> homotopic_paths_assoc pipq)
   960     moreover have "homotopic_paths S ((p +++ reversepath q) +++ q) (linepath (pathstart p) (pathstart p) +++ q)"
   961       by (simp add: \<open>path q\<close> homotopic_paths_join paths pipq)
   962     moreover then have "homotopic_paths S (linepath (pathstart p) (pathstart p) +++ q) q"
   963       by (metis \<open>path q\<close> homotopic_paths_imp_path homotopic_paths_lid linepath_trivial path_join_path_ends pathfinish_def pipq(2))
   964     ultimately show ?thesis
   965       using homotopic_paths_trans by metis
   966   next
   967     case False
   968     then show ?thesis
   969       using \<open>path q\<close> homotopic_loops_imp_path loops path_join_path_ends by fastforce
   970   qed
   971 qed
   972 
   973 
   974 subsection%unimportant\<open>Homotopy of "nearby" function, paths and loops\<close>
   975 
   976 lemma homotopic_with_linear:
   977   fixes f g :: "_ \<Rightarrow> 'b::real_normed_vector"
   978   assumes contf: "continuous_on s f"
   979       and contg:"continuous_on s g"
   980       and sub: "\<And>x. x \<in> s \<Longrightarrow> closed_segment (f x) (g x) \<subseteq> t"
   981     shows "homotopic_with_canon (\<lambda>z. True) s t f g"
   982   apply (simp add: homotopic_with_def)
   983   apply (rule_tac x="\<lambda>y. ((1 - (fst y)) *\<^sub>R f(snd y) + (fst y) *\<^sub>R g(snd y))" in exI)
   984   apply (intro conjI)
   985   apply (rule subset_refl continuous_intros continuous_on_subset [OF contf] continuous_on_compose2 [where g=f]
   986                                             continuous_on_subset [OF contg] continuous_on_compose2 [where g=g]| simp)+
   987   using sub closed_segment_def apply fastforce+
   988   done
   989 
   990 lemma homotopic_paths_linear:
   991   fixes g h :: "real \<Rightarrow> 'a::real_normed_vector"
   992   assumes "path g" "path h" "pathstart h = pathstart g" "pathfinish h = pathfinish g"
   993           "\<And>t. t \<in> {0..1} \<Longrightarrow> closed_segment (g t) (h t) \<subseteq> s"
   994     shows "homotopic_paths s g h"
   995   using assms
   996   unfolding path_def
   997   apply (simp add: closed_segment_def pathstart_def pathfinish_def homotopic_paths_def homotopic_with_def)
   998   apply (rule_tac x="\<lambda>y. ((1 - (fst y)) *\<^sub>R (g \<circ> snd) y + (fst y) *\<^sub>R (h \<circ> snd) y)" in exI)
   999   apply (intro conjI subsetI continuous_intros; force)
  1000   done
  1001 
  1002 lemma homotopic_loops_linear:
  1003   fixes g h :: "real \<Rightarrow> 'a::real_normed_vector"
  1004   assumes "path g" "path h" "pathfinish g = pathstart g" "pathfinish h = pathstart h"
  1005           "\<And>t x. t \<in> {0..1} \<Longrightarrow> closed_segment (g t) (h t) \<subseteq> s"
  1006     shows "homotopic_loops s g h"
  1007   using assms
  1008   unfolding path_def
  1009   apply (simp add: pathstart_def pathfinish_def homotopic_loops_def homotopic_with_def)
  1010   apply (rule_tac x="\<lambda>y. ((1 - (fst y)) *\<^sub>R g(snd y) + (fst y) *\<^sub>R h(snd y))" in exI)
  1011   apply (auto intro!: continuous_intros intro: continuous_on_compose2 [where g=g] continuous_on_compose2 [where g=h])
  1012   apply (force simp: closed_segment_def)
  1013   done
  1014 
  1015 lemma homotopic_paths_nearby_explicit:
  1016   assumes "path g" "path h" "pathstart h = pathstart g" "pathfinish h = pathfinish g"
  1017       and no: "\<And>t x. \<lbrakk>t \<in> {0..1}; x \<notin> s\<rbrakk> \<Longrightarrow> norm(h t - g t) < norm(g t - x)"
  1018     shows "homotopic_paths s g h"
  1019   apply (rule homotopic_paths_linear [OF assms(1-4)])
  1020   by (metis no segment_bound(1) subsetI norm_minus_commute not_le)
  1021 
  1022 lemma homotopic_loops_nearby_explicit:
  1023   assumes "path g" "path h" "pathfinish g = pathstart g" "pathfinish h = pathstart h"
  1024       and no: "\<And>t x. \<lbrakk>t \<in> {0..1}; x \<notin> s\<rbrakk> \<Longrightarrow> norm(h t - g t) < norm(g t - x)"
  1025     shows "homotopic_loops s g h"
  1026   apply (rule homotopic_loops_linear [OF assms(1-4)])
  1027   by (metis no segment_bound(1) subsetI norm_minus_commute not_le)
  1028 
  1029 lemma homotopic_nearby_paths:
  1030   fixes g h :: "real \<Rightarrow> 'a::euclidean_space"
  1031   assumes "path g" "open s" "path_image g \<subseteq> s"
  1032     shows "\<exists>e. 0 < e \<and>
  1033                (\<forall>h. path h \<and>
  1034                     pathstart h = pathstart g \<and> pathfinish h = pathfinish g \<and>
  1035                     (\<forall>t \<in> {0..1}. norm(h t - g t) < e) \<longrightarrow> homotopic_paths s g h)"
  1036 proof -
  1037   obtain e where "e > 0" and e: "\<And>x y. x \<in> path_image g \<Longrightarrow> y \<in> - s \<Longrightarrow> e \<le> dist x y"
  1038     using separate_compact_closed [of "path_image g" "-s"] assms by force
  1039   show ?thesis
  1040     apply (intro exI conjI)
  1041     using e [unfolded dist_norm]
  1042     apply (auto simp: intro!: homotopic_paths_nearby_explicit assms  \<open>e > 0\<close>)
  1043     by (metis atLeastAtMost_iff imageI le_less_trans not_le path_image_def)
  1044 qed
  1045 
  1046 lemma homotopic_nearby_loops:
  1047   fixes g h :: "real \<Rightarrow> 'a::euclidean_space"
  1048   assumes "path g" "open s" "path_image g \<subseteq> s" "pathfinish g = pathstart g"
  1049     shows "\<exists>e. 0 < e \<and>
  1050                (\<forall>h. path h \<and> pathfinish h = pathstart h \<and>
  1051                     (\<forall>t \<in> {0..1}. norm(h t - g t) < e) \<longrightarrow> homotopic_loops s g h)"
  1052 proof -
  1053   obtain e where "e > 0" and e: "\<And>x y. x \<in> path_image g \<Longrightarrow> y \<in> - s \<Longrightarrow> e \<le> dist x y"
  1054     using separate_compact_closed [of "path_image g" "-s"] assms by force
  1055   show ?thesis
  1056     apply (intro exI conjI)
  1057     using e [unfolded dist_norm]
  1058     apply (auto simp: intro!: homotopic_loops_nearby_explicit assms  \<open>e > 0\<close>)
  1059     by (metis atLeastAtMost_iff imageI le_less_trans not_le path_image_def)
  1060 qed
  1061 
  1062 
  1063 subsection\<open> Homotopy and subpaths\<close>
  1064 
  1065 lemma homotopic_join_subpaths1:
  1066   assumes "path g" and pag: "path_image g \<subseteq> s"
  1067       and u: "u \<in> {0..1}" and v: "v \<in> {0..1}" and w: "w \<in> {0..1}" "u \<le> v" "v \<le> w"
  1068     shows "homotopic_paths s (subpath u v g +++ subpath v w g) (subpath u w g)"
  1069 proof -
  1070   have 1: "t * 2 \<le> 1 \<Longrightarrow> u + t * (v * 2) \<le> v + t * (u * 2)" for t
  1071     using affine_ineq \<open>u \<le> v\<close> by fastforce
  1072   have 2: "t * 2 > 1 \<Longrightarrow> u + (2*t - 1) * v \<le> v + (2*t - 1) * w" for t
  1073     by (metis add_mono_thms_linordered_semiring(1) diff_gt_0_iff_gt less_eq_real_def mult.commute mult_right_mono \<open>u \<le> v\<close> \<open>v \<le> w\<close>)
  1074   have t2: "\<And>t::real. t*2 = 1 \<Longrightarrow> t = 1/2" by auto
  1075   show ?thesis
  1076     apply (rule homotopic_paths_subset [OF _ pag])
  1077     using assms
  1078     apply (cases "w = u")
  1079     using homotopic_paths_rinv [of "subpath u v g" "path_image g"]
  1080     apply (force simp: closed_segment_eq_real_ivl image_mono path_image_def subpath_refl)
  1081       apply (rule homotopic_paths_sym)
  1082       apply (rule homotopic_paths_reparametrize
  1083              [where f = "\<lambda>t. if  t \<le> 1 / 2
  1084                              then inverse((w - u)) *\<^sub>R (2 * (v - u)) *\<^sub>R t
  1085                              else inverse((w - u)) *\<^sub>R ((v - u) + (w - v) *\<^sub>R (2 *\<^sub>R t - 1))"])
  1086       using \<open>path g\<close> path_subpath u w apply blast
  1087       using \<open>path g\<close> path_image_subpath_subset u w(1) apply blast
  1088       apply simp_all
  1089       apply (subst split_01)
  1090       apply (rule continuous_on_cases continuous_intros | force simp: pathfinish_def joinpaths_def)+
  1091       apply (simp_all add: field_simps not_le)
  1092       apply (force dest!: t2)
  1093       apply (force simp: algebra_simps mult_left_mono affine_ineq dest!: 1 2)
  1094       apply (simp add: joinpaths_def subpath_def)
  1095       apply (force simp: algebra_simps)
  1096       done
  1097 qed
  1098 
  1099 lemma homotopic_join_subpaths2:
  1100   assumes "homotopic_paths s (subpath u v g +++ subpath v w g) (subpath u w g)"
  1101     shows "homotopic_paths s (subpath w v g +++ subpath v u g) (subpath w u g)"
  1102 by (metis assms homotopic_paths_reversepath_D pathfinish_subpath pathstart_subpath reversepath_joinpaths reversepath_subpath)
  1103 
  1104 lemma homotopic_join_subpaths3:
  1105   assumes hom: "homotopic_paths s (subpath u v g +++ subpath v w g) (subpath u w g)"
  1106       and "path g" and pag: "path_image g \<subseteq> s"
  1107       and u: "u \<in> {0..1}" and v: "v \<in> {0..1}" and w: "w \<in> {0..1}"
  1108     shows "homotopic_paths s (subpath v w g +++ subpath w u g) (subpath v u g)"
  1109 proof -
  1110   have "homotopic_paths s (subpath u w g +++ subpath w v g) ((subpath u v g +++ subpath v w g) +++ subpath w v g)"
  1111     apply (rule homotopic_paths_join)
  1112     using hom homotopic_paths_sym_eq apply blast
  1113     apply (metis \<open>path g\<close> homotopic_paths_eq pag path_image_subpath_subset path_subpath subset_trans v w, simp)
  1114     done
  1115   also have "homotopic_paths s ((subpath u v g +++ subpath v w g) +++ subpath w v g) (subpath u v g +++ subpath v w g +++ subpath w v g)"
  1116     apply (rule homotopic_paths_sym [OF homotopic_paths_assoc])
  1117     using assms by (simp_all add: path_image_subpath_subset [THEN order_trans])
  1118   also have "homotopic_paths s (subpath u v g +++ subpath v w g +++ subpath w v g)
  1119                                (subpath u v g +++ linepath (pathfinish (subpath u v g)) (pathfinish (subpath u v g)))"
  1120     apply (rule homotopic_paths_join)
  1121     apply (metis \<open>path g\<close> homotopic_paths_eq order.trans pag path_image_subpath_subset path_subpath u v)
  1122     apply (metis (no_types, lifting) \<open>path g\<close> homotopic_paths_linv order_trans pag path_image_subpath_subset path_subpath pathfinish_subpath reversepath_subpath v w)
  1123     apply simp
  1124     done
  1125   also have "homotopic_paths s (subpath u v g +++ linepath (pathfinish (subpath u v g)) (pathfinish (subpath u v g))) (subpath u v g)"
  1126     apply (rule homotopic_paths_rid)
  1127     using \<open>path g\<close> path_subpath u v apply blast
  1128     apply (meson \<open>path g\<close> order.trans pag path_image_subpath_subset u v)
  1129     done
  1130   finally have "homotopic_paths s (subpath u w g +++ subpath w v g) (subpath u v g)" .
  1131   then show ?thesis
  1132     using homotopic_join_subpaths2 by blast
  1133 qed
  1134 
  1135 proposition homotopic_join_subpaths:
  1136    "\<lbrakk>path g; path_image g \<subseteq> s; u \<in> {0..1}; v \<in> {0..1}; w \<in> {0..1}\<rbrakk>
  1137     \<Longrightarrow> homotopic_paths s (subpath u v g +++ subpath v w g) (subpath u w g)"
  1138   apply (rule le_cases3 [of u v w])
  1139 using homotopic_join_subpaths1 homotopic_join_subpaths2 homotopic_join_subpaths3 by metis+
  1140 
  1141 text\<open>Relating homotopy of trivial loops to path-connectedness.\<close>
  1142 
  1143 lemma path_component_imp_homotopic_points:
  1144     "path_component S a b \<Longrightarrow> homotopic_loops S (linepath a a) (linepath b b)"
  1145 apply (simp add: path_component_def homotopic_loops_def homotopic_with_def
  1146                  pathstart_def pathfinish_def path_image_def path_def, clarify)
  1147 apply (rule_tac x="g \<circ> fst" in exI)
  1148 apply (intro conjI continuous_intros continuous_on_compose)+
  1149 apply (auto elim!: continuous_on_subset)
  1150 done
  1151 
  1152 lemma homotopic_loops_imp_path_component_value:
  1153    "\<lbrakk>homotopic_loops S p q; 0 \<le> t; t \<le> 1\<rbrakk>
  1154         \<Longrightarrow> path_component S (p t) (q t)"
  1155 apply (simp add: path_component_def homotopic_loops_def homotopic_with_def
  1156                  pathstart_def pathfinish_def path_image_def path_def, clarify)
  1157 apply (rule_tac x="h \<circ> (\<lambda>u. (u, t))" in exI)
  1158 apply (intro conjI continuous_intros continuous_on_compose)+
  1159 apply (auto elim!: continuous_on_subset)
  1160 done
  1161 
  1162 lemma homotopic_points_eq_path_component:
  1163    "homotopic_loops S (linepath a a) (linepath b b) \<longleftrightarrow>
  1164         path_component S a b"
  1165 by (auto simp: path_component_imp_homotopic_points
  1166          dest: homotopic_loops_imp_path_component_value [where t=1])
  1167 
  1168 lemma path_connected_eq_homotopic_points:
  1169     "path_connected S \<longleftrightarrow>
  1170       (\<forall>a b. a \<in> S \<and> b \<in> S \<longrightarrow> homotopic_loops S (linepath a a) (linepath b b))"
  1171 by (auto simp: path_connected_def path_component_def homotopic_points_eq_path_component)
  1172 
  1173 
  1174 subsection\<open>Simply connected sets\<close>
  1175 
  1176 text%important\<open>defined as "all loops are homotopic (as loops)\<close>
  1177 
  1178 definition%important simply_connected where
  1179   "simply_connected S \<equiv>
  1180         \<forall>p q. path p \<and> pathfinish p = pathstart p \<and> path_image p \<subseteq> S \<and>
  1181               path q \<and> pathfinish q = pathstart q \<and> path_image q \<subseteq> S
  1182               \<longrightarrow> homotopic_loops S p q"
  1183 
  1184 lemma simply_connected_empty [iff]: "simply_connected {}"
  1185   by (simp add: simply_connected_def)
  1186 
  1187 lemma simply_connected_imp_path_connected:
  1188   fixes S :: "_::real_normed_vector set"
  1189   shows "simply_connected S \<Longrightarrow> path_connected S"
  1190 by (simp add: simply_connected_def path_connected_eq_homotopic_points)
  1191 
  1192 lemma simply_connected_imp_connected:
  1193   fixes S :: "_::real_normed_vector set"
  1194   shows "simply_connected S \<Longrightarrow> connected S"
  1195 by (simp add: path_connected_imp_connected simply_connected_imp_path_connected)
  1196 
  1197 lemma simply_connected_eq_contractible_loop_any:
  1198   fixes S :: "_::real_normed_vector set"
  1199   shows "simply_connected S \<longleftrightarrow>
  1200             (\<forall>p a. path p \<and> path_image p \<subseteq> S \<and>
  1201                   pathfinish p = pathstart p \<and> a \<in> S
  1202                   \<longrightarrow> homotopic_loops S p (linepath a a))"
  1203 apply (simp add: simply_connected_def)
  1204 apply (rule iffI, force, clarify)
  1205 apply (rule_tac q = "linepath (pathstart p) (pathstart p)" in homotopic_loops_trans)
  1206 apply (fastforce simp add:)
  1207 using homotopic_loops_sym apply blast
  1208 done
  1209 
  1210 lemma simply_connected_eq_contractible_loop_some:
  1211   fixes S :: "_::real_normed_vector set"
  1212   shows "simply_connected S \<longleftrightarrow>
  1213                 path_connected S \<and>
  1214                 (\<forall>p. path p \<and> path_image p \<subseteq> S \<and> pathfinish p = pathstart p
  1215                     \<longrightarrow> (\<exists>a. a \<in> S \<and> homotopic_loops S p (linepath a a)))"
  1216 apply (rule iffI)
  1217  apply (fastforce simp: simply_connected_imp_path_connected simply_connected_eq_contractible_loop_any)
  1218 apply (clarsimp simp add: simply_connected_eq_contractible_loop_any)
  1219 apply (drule_tac x=p in spec)
  1220 using homotopic_loops_trans path_connected_eq_homotopic_points
  1221   apply blast
  1222 done
  1223 
  1224 lemma simply_connected_eq_contractible_loop_all:
  1225   fixes S :: "_::real_normed_vector set"
  1226   shows "simply_connected S \<longleftrightarrow>
  1227          S = {} \<or>
  1228          (\<exists>a \<in> S. \<forall>p. path p \<and> path_image p \<subseteq> S \<and> pathfinish p = pathstart p
  1229                 \<longrightarrow> homotopic_loops S p (linepath a a))"
  1230         (is "?lhs = ?rhs")
  1231 proof (cases "S = {}")
  1232   case True then show ?thesis by force
  1233 next
  1234   case False
  1235   then obtain a where "a \<in> S" by blast
  1236   show ?thesis
  1237   proof
  1238     assume "simply_connected S"
  1239     then show ?rhs
  1240       using \<open>a \<in> S\<close> \<open>simply_connected S\<close> simply_connected_eq_contractible_loop_any
  1241       by blast
  1242   next
  1243     assume ?rhs
  1244     then show "simply_connected S"
  1245       apply (simp add: simply_connected_eq_contractible_loop_any False)
  1246       by (meson homotopic_loops_refl homotopic_loops_sym homotopic_loops_trans
  1247              path_component_imp_homotopic_points path_component_refl)
  1248   qed
  1249 qed
  1250 
  1251 lemma simply_connected_eq_contractible_path:
  1252   fixes S :: "_::real_normed_vector set"
  1253   shows "simply_connected S \<longleftrightarrow>
  1254            path_connected S \<and>
  1255            (\<forall>p. path p \<and> path_image p \<subseteq> S \<and> pathfinish p = pathstart p
  1256             \<longrightarrow> homotopic_paths S p (linepath (pathstart p) (pathstart p)))"
  1257 apply (rule iffI)
  1258  apply (simp add: simply_connected_imp_path_connected)
  1259  apply (metis simply_connected_eq_contractible_loop_some homotopic_loops_imp_homotopic_paths_null)
  1260 by (meson homotopic_paths_imp_homotopic_loops pathfinish_linepath pathstart_in_path_image
  1261          simply_connected_eq_contractible_loop_some subset_iff)
  1262 
  1263 lemma simply_connected_eq_homotopic_paths:
  1264   fixes S :: "_::real_normed_vector set"
  1265   shows "simply_connected S \<longleftrightarrow>
  1266           path_connected S \<and>
  1267           (\<forall>p q. path p \<and> path_image p \<subseteq> S \<and>
  1268                 path q \<and> path_image q \<subseteq> S \<and>
  1269                 pathstart q = pathstart p \<and> pathfinish q = pathfinish p
  1270                 \<longrightarrow> homotopic_paths S p q)"
  1271          (is "?lhs = ?rhs")
  1272 proof
  1273   assume ?lhs
  1274   then have pc: "path_connected S"
  1275         and *:  "\<And>p. \<lbrakk>path p; path_image p \<subseteq> S;
  1276                        pathfinish p = pathstart p\<rbrakk>
  1277                       \<Longrightarrow> homotopic_paths S p (linepath (pathstart p) (pathstart p))"
  1278     by (auto simp: simply_connected_eq_contractible_path)
  1279   have "homotopic_paths S p q"
  1280         if "path p" "path_image p \<subseteq> S" "path q"
  1281            "path_image q \<subseteq> S" "pathstart q = pathstart p"
  1282            "pathfinish q = pathfinish p" for p q
  1283   proof -
  1284     have "homotopic_paths S p (p +++ linepath (pathfinish p) (pathfinish p))"
  1285       by (simp add: homotopic_paths_rid homotopic_paths_sym that)
  1286     also have "homotopic_paths S (p +++ linepath (pathfinish p) (pathfinish p))
  1287                                  (p +++ reversepath q +++ q)"
  1288       using that
  1289       by (metis homotopic_paths_join homotopic_paths_linv homotopic_paths_refl homotopic_paths_sym_eq pathstart_linepath)
  1290     also have "homotopic_paths S (p +++ reversepath q +++ q)
  1291                                  ((p +++ reversepath q) +++ q)"
  1292       by (simp add: that homotopic_paths_assoc)
  1293     also have "homotopic_paths S ((p +++ reversepath q) +++ q)
  1294                                  (linepath (pathstart q) (pathstart q) +++ q)"
  1295       using * [of "p +++ reversepath q"] that
  1296       by (simp add: homotopic_paths_join path_image_join)
  1297     also have "homotopic_paths S (linepath (pathstart q) (pathstart q) +++ q) q"
  1298       using that homotopic_paths_lid by blast
  1299     finally show ?thesis .
  1300   qed
  1301   then show ?rhs
  1302     by (blast intro: pc *)
  1303 next
  1304   assume ?rhs
  1305   then show ?lhs
  1306     by (force simp: simply_connected_eq_contractible_path)
  1307 qed
  1308 
  1309 proposition simply_connected_Times:
  1310   fixes S :: "'a::real_normed_vector set" and T :: "'b::real_normed_vector set"
  1311   assumes S: "simply_connected S" and T: "simply_connected T"
  1312     shows "simply_connected(S \<times> T)"
  1313 proof -
  1314   have "homotopic_loops (S \<times> T) p (linepath (a, b) (a, b))"
  1315        if "path p" "path_image p \<subseteq> S \<times> T" "p 1 = p 0" "a \<in> S" "b \<in> T"
  1316        for p a b
  1317   proof -
  1318     have "path (fst \<circ> p)"
  1319       apply (rule Path_Connected.path_continuous_image [OF \<open>path p\<close>])
  1320       apply (rule continuous_intros)+
  1321       done
  1322     moreover have "path_image (fst \<circ> p) \<subseteq> S"
  1323       using that apply (simp add: path_image_def) by force
  1324     ultimately have p1: "homotopic_loops S (fst \<circ> p) (linepath a a)"
  1325       using S that
  1326       apply (simp add: simply_connected_eq_contractible_loop_any)
  1327       apply (drule_tac x="fst \<circ> p" in spec)
  1328       apply (drule_tac x=a in spec)
  1329       apply (auto simp: pathstart_def pathfinish_def)
  1330       done
  1331     have "path (snd \<circ> p)"
  1332       apply (rule Path_Connected.path_continuous_image [OF \<open>path p\<close>])
  1333       apply (rule continuous_intros)+
  1334       done
  1335     moreover have "path_image (snd \<circ> p) \<subseteq> T"
  1336       using that apply (simp add: path_image_def) by force
  1337     ultimately have p2: "homotopic_loops T (snd \<circ> p) (linepath b b)"
  1338       using T that
  1339       apply (simp add: simply_connected_eq_contractible_loop_any)
  1340       apply (drule_tac x="snd \<circ> p" in spec)
  1341       apply (drule_tac x=b in spec)
  1342       apply (auto simp: pathstart_def pathfinish_def)
  1343       done
  1344     show ?thesis
  1345       using p1 p2
  1346       apply (simp add: homotopic_loops, clarify)
  1347       apply (rename_tac h k)
  1348       apply (rule_tac x="\<lambda>z. Pair (h z) (k z)" in exI)
  1349       apply (intro conjI continuous_intros | assumption)+
  1350       apply (auto simp: pathstart_def pathfinish_def)
  1351       done
  1352   qed
  1353   with assms show ?thesis
  1354     by (simp add: simply_connected_eq_contractible_loop_any pathfinish_def pathstart_def)
  1355 qed
  1356 
  1357 
  1358 subsection\<open>Contractible sets\<close>
  1359 
  1360 definition%important contractible where
  1361  "contractible S \<equiv> \<exists>a. homotopic_with_canon (\<lambda>x. True) S S id (\<lambda>x. a)"
  1362 
  1363 proposition contractible_imp_simply_connected:
  1364   fixes S :: "_::real_normed_vector set"
  1365   assumes "contractible S" shows "simply_connected S"
  1366 proof (cases "S = {}")
  1367   case True then show ?thesis by force
  1368 next
  1369   case False
  1370   obtain a where a: "homotopic_with_canon (\<lambda>x. True) S S id (\<lambda>x. a)"
  1371     using assms by (force simp: contractible_def)
  1372   then have "a \<in> S"
  1373     by (metis False homotopic_constant_maps homotopic_with_symD homotopic_with_trans path_component_in_topspace topspace_euclidean_subtopology)
  1374   show ?thesis
  1375     apply (simp add: simply_connected_eq_contractible_loop_all False)
  1376     apply (rule bexI [OF _ \<open>a \<in> S\<close>])
  1377     using a apply (simp add: homotopic_loops_def homotopic_with_def path_def path_image_def pathfinish_def pathstart_def, clarify)
  1378     apply (rule_tac x="(h \<circ> (\<lambda>y. (fst y, (p \<circ> snd) y)))" in exI)
  1379     apply (intro conjI continuous_on_compose continuous_intros)
  1380     apply (erule continuous_on_subset | force)+
  1381     done
  1382 qed
  1383 
  1384 corollary contractible_imp_connected:
  1385   fixes S :: "_::real_normed_vector set"
  1386   shows "contractible S \<Longrightarrow> connected S"
  1387 by (simp add: contractible_imp_simply_connected simply_connected_imp_connected)
  1388 
  1389 lemma contractible_imp_path_connected:
  1390   fixes S :: "_::real_normed_vector set"
  1391   shows "contractible S \<Longrightarrow> path_connected S"
  1392 by (simp add: contractible_imp_simply_connected simply_connected_imp_path_connected)
  1393 
  1394 lemma nullhomotopic_through_contractible:
  1395   fixes S :: "_::topological_space set"
  1396   assumes f: "continuous_on S f" "f ` S \<subseteq> T"
  1397       and g: "continuous_on T g" "g ` T \<subseteq> U"
  1398       and T: "contractible T"
  1399     obtains c where "homotopic_with_canon (\<lambda>h. True) S U (g \<circ> f) (\<lambda>x. c)"
  1400 proof -
  1401   obtain b where b: "homotopic_with_canon (\<lambda>x. True) T T id (\<lambda>x. b)"
  1402     using assms by (force simp: contractible_def)
  1403   have "homotopic_with_canon (\<lambda>f. True) T U (g \<circ> id) (g \<circ> (\<lambda>x. b))"
  1404     by (metis Abstract_Topology.continuous_map_subtopology_eu b g homotopic_compose_continuous_map_left)
  1405   then have "homotopic_with_canon (\<lambda>f. True) S U (g \<circ> id \<circ> f) (g \<circ> (\<lambda>x. b) \<circ> f)"
  1406     by (simp add: f homotopic_with_compose_continuous_map_right)
  1407   then show ?thesis
  1408     by (simp add: comp_def that)
  1409 qed
  1410 
  1411 lemma nullhomotopic_into_contractible:
  1412   assumes f: "continuous_on S f" "f ` S \<subseteq> T"
  1413       and T: "contractible T"
  1414     obtains c where "homotopic_with_canon (\<lambda>h. True) S T f (\<lambda>x. c)"
  1415 apply (rule nullhomotopic_through_contractible [OF f, of id T])
  1416 using assms
  1417 apply (auto simp: continuous_on_id)
  1418 done
  1419 
  1420 lemma nullhomotopic_from_contractible:
  1421   assumes f: "continuous_on S f" "f ` S \<subseteq> T"
  1422       and S: "contractible S"
  1423     obtains c where "homotopic_with_canon (\<lambda>h. True) S T f (\<lambda>x. c)"
  1424 apply (rule nullhomotopic_through_contractible [OF continuous_on_id _ f S, of S])
  1425 using assms
  1426 apply (auto simp: comp_def)
  1427 done
  1428 
  1429 lemma homotopic_through_contractible:
  1430   fixes S :: "_::real_normed_vector set"
  1431   assumes "continuous_on S f1" "f1 ` S \<subseteq> T"
  1432           "continuous_on T g1" "g1 ` T \<subseteq> U"
  1433           "continuous_on S f2" "f2 ` S \<subseteq> T"
  1434           "continuous_on T g2" "g2 ` T \<subseteq> U"
  1435           "contractible T" "path_connected U"
  1436    shows "homotopic_with_canon (\<lambda>h. True) S U (g1 \<circ> f1) (g2 \<circ> f2)"
  1437 proof -
  1438   obtain c1 where c1: "homotopic_with_canon (\<lambda>h. True) S U (g1 \<circ> f1) (\<lambda>x. c1)"
  1439     apply (rule nullhomotopic_through_contractible [of S f1 T g1 U])
  1440     using assms apply auto
  1441     done
  1442   obtain c2 where c2: "homotopic_with_canon (\<lambda>h. True) S U (g2 \<circ> f2) (\<lambda>x. c2)"
  1443     apply (rule nullhomotopic_through_contractible [of S f2 T g2 U])
  1444     using assms apply auto
  1445     done
  1446   have *: "S = {} \<or> (\<exists>t. path_connected t \<and> t \<subseteq> U \<and> c2 \<in> t \<and> c1 \<in> t)"
  1447   proof (cases "S = {}")
  1448     case True then show ?thesis by force
  1449   next
  1450     case False
  1451     with c1 c2 have "c1 \<in> U" "c2 \<in> U"
  1452       using homotopic_with_imp_continuous_maps by fastforce+
  1453     with \<open>path_connected U\<close> show ?thesis by blast
  1454   qed
  1455   show ?thesis
  1456     apply (rule homotopic_with_trans [OF c1])
  1457     apply (rule homotopic_with_symD)
  1458     apply (rule homotopic_with_trans [OF c2])
  1459     apply (simp add: path_component homotopic_constant_maps *)
  1460     done
  1461 qed
  1462 
  1463 lemma homotopic_into_contractible:
  1464   fixes S :: "'a::real_normed_vector set" and T:: "'b::real_normed_vector set"
  1465   assumes f: "continuous_on S f" "f ` S \<subseteq> T"
  1466       and g: "continuous_on S g" "g ` S \<subseteq> T"
  1467       and T: "contractible T"
  1468     shows "homotopic_with_canon (\<lambda>h. True) S T f g"
  1469 using homotopic_through_contractible [of S f T id T g id]
  1470 by (simp add: assms contractible_imp_path_connected)
  1471 
  1472 lemma homotopic_from_contractible:
  1473   fixes S :: "'a::real_normed_vector set" and T:: "'b::real_normed_vector set"
  1474   assumes f: "continuous_on S f" "f ` S \<subseteq> T"
  1475       and g: "continuous_on S g" "g ` S \<subseteq> T"
  1476       and "contractible S" "path_connected T"
  1477     shows "homotopic_with_canon (\<lambda>h. True) S T f g"
  1478 using homotopic_through_contractible [of S id S f T id g]
  1479 by (simp add: assms contractible_imp_path_connected)
  1480 
  1481 lemma starlike_imp_contractible_gen:
  1482   fixes S :: "'a::real_normed_vector set"
  1483   assumes S: "starlike S"
  1484       and P: "\<And>a T. \<lbrakk>a \<in> S; 0 \<le> T; T \<le> 1\<rbrakk> \<Longrightarrow> P(\<lambda>x. (1 - T) *\<^sub>R x + T *\<^sub>R a)"
  1485     obtains a where "homotopic_with_canon P S S (\<lambda>x. x) (\<lambda>x. a)"
  1486 proof -
  1487   obtain a where "a \<in> S" and a: "\<And>x. x \<in> S \<Longrightarrow> closed_segment a x \<subseteq> S"
  1488     using S by (auto simp: starlike_def)
  1489   have "(\<lambda>y. (1 - fst y) *\<^sub>R snd y + fst y *\<^sub>R a) ` ({0..1} \<times> S) \<subseteq> S"
  1490     apply clarify
  1491     apply (erule a [unfolded closed_segment_def, THEN subsetD], simp)
  1492     apply (metis add_diff_cancel_right' diff_ge_0_iff_ge le_add_diff_inverse pth_c(1))
  1493     done
  1494   then show ?thesis
  1495     apply (rule_tac a=a in that)
  1496     using \<open>a \<in> S\<close>
  1497     apply (simp add: homotopic_with_def)
  1498     apply (rule_tac x="\<lambda>y. (1 - (fst y)) *\<^sub>R snd y + (fst y) *\<^sub>R a" in exI)
  1499     apply (intro conjI ballI continuous_on_compose continuous_intros)
  1500     apply (simp_all add: P)
  1501     done
  1502 qed
  1503 
  1504 lemma starlike_imp_contractible:
  1505   fixes S :: "'a::real_normed_vector set"
  1506   shows "starlike S \<Longrightarrow> contractible S"
  1507 using starlike_imp_contractible_gen contractible_def by (fastforce simp: id_def)
  1508 
  1509 lemma contractible_UNIV [simp]: "contractible (UNIV :: 'a::real_normed_vector set)"
  1510   by (simp add: starlike_imp_contractible)
  1511 
  1512 lemma starlike_imp_simply_connected:
  1513   fixes S :: "'a::real_normed_vector set"
  1514   shows "starlike S \<Longrightarrow> simply_connected S"
  1515 by (simp add: contractible_imp_simply_connected starlike_imp_contractible)
  1516 
  1517 lemma convex_imp_simply_connected:
  1518   fixes S :: "'a::real_normed_vector set"
  1519   shows "convex S \<Longrightarrow> simply_connected S"
  1520 using convex_imp_starlike starlike_imp_simply_connected by blast
  1521 
  1522 lemma starlike_imp_path_connected:
  1523   fixes S :: "'a::real_normed_vector set"
  1524   shows "starlike S \<Longrightarrow> path_connected S"
  1525 by (simp add: simply_connected_imp_path_connected starlike_imp_simply_connected)
  1526 
  1527 lemma starlike_imp_connected:
  1528   fixes S :: "'a::real_normed_vector set"
  1529   shows "starlike S \<Longrightarrow> connected S"
  1530 by (simp add: path_connected_imp_connected starlike_imp_path_connected)
  1531 
  1532 lemma is_interval_simply_connected_1:
  1533   fixes S :: "real set"
  1534   shows "is_interval S \<longleftrightarrow> simply_connected S"
  1535 using convex_imp_simply_connected is_interval_convex_1 is_interval_path_connected_1 simply_connected_imp_path_connected by auto
  1536 
  1537 lemma contractible_empty [simp]: "contractible {}"
  1538   by (simp add: contractible_def homotopic_on_emptyI)
  1539 
  1540 lemma contractible_convex_tweak_boundary_points:
  1541   fixes S :: "'a::euclidean_space set"
  1542   assumes "convex S" and TS: "rel_interior S \<subseteq> T" "T \<subseteq> closure S"
  1543   shows "contractible T"
  1544 proof (cases "S = {}")
  1545   case True
  1546   with assms show ?thesis
  1547     by (simp add: subsetCE)
  1548 next
  1549   case False
  1550   show ?thesis
  1551     apply (rule starlike_imp_contractible)
  1552     apply (rule starlike_convex_tweak_boundary_points [OF \<open>convex S\<close> False TS])
  1553     done
  1554 qed
  1555 
  1556 lemma convex_imp_contractible:
  1557   fixes S :: "'a::real_normed_vector set"
  1558   shows "convex S \<Longrightarrow> contractible S"
  1559   using contractible_empty convex_imp_starlike starlike_imp_contractible by blast
  1560 
  1561 lemma contractible_sing [simp]:
  1562   fixes a :: "'a::real_normed_vector"
  1563   shows "contractible {a}"
  1564 by (rule convex_imp_contractible [OF convex_singleton])
  1565 
  1566 lemma is_interval_contractible_1:
  1567   fixes S :: "real set"
  1568   shows  "is_interval S \<longleftrightarrow> contractible S"
  1569 using contractible_imp_simply_connected convex_imp_contractible is_interval_convex_1
  1570       is_interval_simply_connected_1 by auto
  1571 
  1572 lemma contractible_Times:
  1573   fixes S :: "'a::euclidean_space set" and T :: "'b::euclidean_space set"
  1574   assumes S: "contractible S" and T: "contractible T"
  1575   shows "contractible (S \<times> T)"
  1576 proof -
  1577   obtain a h where conth: "continuous_on ({0..1} \<times> S) h"
  1578              and hsub: "h ` ({0..1} \<times> S) \<subseteq> S"
  1579              and [simp]: "\<And>x. x \<in> S \<Longrightarrow> h (0, x) = x"
  1580              and [simp]: "\<And>x. x \<in> S \<Longrightarrow>  h (1::real, x) = a"
  1581     using S by (auto simp: contractible_def homotopic_with)
  1582   obtain b k where contk: "continuous_on ({0..1} \<times> T) k"
  1583              and ksub: "k ` ({0..1} \<times> T) \<subseteq> T"
  1584              and [simp]: "\<And>x. x \<in> T \<Longrightarrow> k (0, x) = x"
  1585              and [simp]: "\<And>x. x \<in> T \<Longrightarrow>  k (1::real, x) = b"
  1586     using T by (auto simp: contractible_def homotopic_with)
  1587   show ?thesis
  1588     apply (simp add: contractible_def homotopic_with)
  1589     apply (rule exI [where x=a])
  1590     apply (rule exI [where x=b])
  1591     apply (rule exI [where x = "\<lambda>z. (h (fst z, fst(snd z)), k (fst z, snd(snd z)))"])
  1592     apply (intro conjI ballI continuous_intros continuous_on_compose2 [OF conth] continuous_on_compose2 [OF contk])
  1593     using hsub ksub
  1594     apply auto
  1595     done
  1596 qed
  1597 
  1598 
  1599 subsection\<open>Local versions of topological properties in general\<close>
  1600 
  1601 definition%important locally :: "('a::topological_space set \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool"
  1602 where
  1603  "locally P S \<equiv>
  1604         \<forall>w x. openin (top_of_set S) w \<and> x \<in> w
  1605               \<longrightarrow> (\<exists>u v. openin (top_of_set S) u \<and> P v \<and>
  1606                         x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> w)"
  1607 
  1608 lemma locallyI:
  1609   assumes "\<And>w x. \<lbrakk>openin (top_of_set S) w; x \<in> w\<rbrakk>
  1610                   \<Longrightarrow> \<exists>u v. openin (top_of_set S) u \<and> P v \<and>
  1611                         x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> w"
  1612     shows "locally P S"
  1613 using assms by (force simp: locally_def)
  1614 
  1615 lemma locallyE:
  1616   assumes "locally P S" "openin (top_of_set S) w" "x \<in> w"
  1617   obtains u v where "openin (top_of_set S) u"
  1618                     "P v" "x \<in> u" "u \<subseteq> v" "v \<subseteq> w"
  1619   using assms unfolding locally_def by meson
  1620 
  1621 lemma locally_mono:
  1622   assumes "locally P S" "\<And>t. P t \<Longrightarrow> Q t"
  1623     shows "locally Q S"
  1624 by (metis assms locally_def)
  1625 
  1626 lemma locally_open_subset:
  1627   assumes "locally P S" "openin (top_of_set S) t"
  1628     shows "locally P t"
  1629 using assms
  1630 apply (simp add: locally_def)
  1631 apply (erule all_forward)+
  1632 apply (rule impI)
  1633 apply (erule impCE)
  1634  using openin_trans apply blast
  1635 apply (erule ex_forward)
  1636 by (metis (no_types, hide_lams) Int_absorb1 Int_lower1 Int_subset_iff openin_open openin_subtopology_Int_subset)
  1637 
  1638 lemma locally_diff_closed:
  1639     "\<lbrakk>locally P S; closedin (top_of_set S) t\<rbrakk> \<Longrightarrow> locally P (S - t)"
  1640   using locally_open_subset closedin_def by fastforce
  1641 
  1642 lemma locally_empty [iff]: "locally P {}"
  1643   by (simp add: locally_def openin_subtopology)
  1644 
  1645 lemma locally_singleton [iff]:
  1646   fixes a :: "'a::metric_space"
  1647   shows "locally P {a} \<longleftrightarrow> P {a}"
  1648 apply (simp add: locally_def openin_euclidean_subtopology_iff subset_singleton_iff conj_disj_distribR cong: conj_cong)
  1649 using zero_less_one by blast
  1650 
  1651 lemma locally_iff:
  1652     "locally P S \<longleftrightarrow>
  1653      (\<forall>T x. open T \<and> x \<in> S \<inter> T \<longrightarrow> (\<exists>U. open U \<and> (\<exists>v. P v \<and> x \<in> S \<inter> U \<and> S \<inter> U \<subseteq> v \<and> v \<subseteq> S \<inter> T)))"
  1654 apply (simp add: le_inf_iff locally_def openin_open, safe)
  1655 apply (metis IntE IntI le_inf_iff)
  1656 apply (metis IntI Int_subset_iff)
  1657 done
  1658 
  1659 lemma locally_Int:
  1660   assumes S: "locally P S" and t: "locally P t"
  1661       and P: "\<And>S t. P S \<and> P t \<Longrightarrow> P(S \<inter> t)"
  1662     shows "locally P (S \<inter> t)"
  1663 using S t unfolding locally_iff
  1664 apply clarify
  1665 apply (drule_tac x=T in spec)+
  1666 apply (drule_tac x=x in spec)+
  1667 apply clarsimp
  1668 apply (rename_tac U1 U2 V1 V2)
  1669 apply (rule_tac x="U1 \<inter> U2" in exI)
  1670 apply (simp add: open_Int)
  1671 apply (rule_tac x="V1 \<inter> V2" in exI)
  1672 apply (auto intro: P)
  1673   done
  1674 
  1675 lemma locally_Times:
  1676   fixes S :: "('a::metric_space) set" and T :: "('b::metric_space) set"
  1677   assumes PS: "locally P S" and QT: "locally Q T" and R: "\<And>S T. P S \<and> Q T \<Longrightarrow> R(S \<times> T)"
  1678   shows "locally R (S \<times> T)"
  1679     unfolding locally_def
  1680 proof (clarify)
  1681   fix W x y
  1682   assume W: "openin (top_of_set (S \<times> T)) W" and xy: "(x, y) \<in> W"
  1683   then obtain U V where "openin (top_of_set S) U" "x \<in> U"
  1684                         "openin (top_of_set T) V" "y \<in> V" "U \<times> V \<subseteq> W"
  1685     using Times_in_interior_subtopology by metis
  1686   then obtain U1 U2 V1 V2
  1687          where opeS: "openin (top_of_set S) U1 \<and> P U2 \<and> x \<in> U1 \<and> U1 \<subseteq> U2 \<and> U2 \<subseteq> U"
  1688            and opeT: "openin (top_of_set T) V1 \<and> Q V2 \<and> y \<in> V1 \<and> V1 \<subseteq> V2 \<and> V2 \<subseteq> V"
  1689     by (meson PS QT locallyE)
  1690   with \<open>U \<times> V \<subseteq> W\<close> show "\<exists>u v. openin (top_of_set (S \<times> T)) u \<and> R v \<and> (x,y) \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> W"
  1691     apply (rule_tac x="U1 \<times> V1" in exI)
  1692     apply (rule_tac x="U2 \<times> V2" in exI)
  1693     apply (auto simp: openin_Times R openin_Times_eq)
  1694     done
  1695 qed
  1696 
  1697 
  1698 proposition homeomorphism_locally_imp:
  1699   fixes S :: "'a::metric_space set" and t :: "'b::t2_space set"
  1700   assumes S: "locally P S" and hom: "homeomorphism S t f g"
  1701       and Q: "\<And>S S'. \<lbrakk>P S; homeomorphism S S' f g\<rbrakk> \<Longrightarrow> Q S'"
  1702     shows "locally Q t"
  1703 proof (clarsimp simp: locally_def)
  1704   fix W y
  1705   assume "y \<in> W" and "openin (top_of_set t) W"
  1706   then obtain T where T: "open T" "W = t \<inter> T"
  1707     by (force simp: openin_open)
  1708   then have "W \<subseteq> t" by auto
  1709   have f: "\<And>x. x \<in> S \<Longrightarrow> g(f x) = x" "f ` S = t" "continuous_on S f"
  1710    and g: "\<And>y. y \<in> t \<Longrightarrow> f(g y) = y" "g ` t = S" "continuous_on t g"
  1711     using hom by (auto simp: homeomorphism_def)
  1712   have gw: "g ` W = S \<inter> f -` W"
  1713     using \<open>W \<subseteq> t\<close>
  1714     apply auto
  1715     using \<open>g ` t = S\<close> \<open>W \<subseteq> t\<close> apply blast
  1716     using g \<open>W \<subseteq> t\<close> apply auto[1]
  1717     by (simp add: f rev_image_eqI)
  1718   have \<circ>: "openin (top_of_set S) (g ` W)"
  1719   proof -
  1720     have "continuous_on S f"
  1721       using f(3) by blast
  1722     then show "openin (top_of_set S) (g ` W)"
  1723       by (simp add: gw Collect_conj_eq \<open>openin (top_of_set t) W\<close> continuous_on_open f(2))
  1724   qed
  1725   then obtain u v
  1726     where osu: "openin (top_of_set S) u" and uv: "P v" "g y \<in> u" "u \<subseteq> v" "v \<subseteq> g ` W"
  1727     using S [unfolded locally_def, rule_format, of "g ` W" "g y"] \<open>y \<in> W\<close> by force
  1728   have "v \<subseteq> S" using uv by (simp add: gw)
  1729   have fv: "f ` v = t \<inter> {x. g x \<in> v}"
  1730     using \<open>f ` S = t\<close> f \<open>v \<subseteq> S\<close> by auto
  1731   have "f ` v \<subseteq> W"
  1732     using uv using Int_lower2 gw image_subsetI mem_Collect_eq subset_iff by auto
  1733   have contvf: "continuous_on v f"
  1734     using \<open>v \<subseteq> S\<close> continuous_on_subset f(3) by blast
  1735   have contvg: "continuous_on (f ` v) g"
  1736     using \<open>f ` v \<subseteq> W\<close> \<open>W \<subseteq> t\<close> continuous_on_subset [OF g(3)] by blast
  1737   have homv: "homeomorphism v (f ` v) f g"
  1738     using \<open>v \<subseteq> S\<close> \<open>W \<subseteq> t\<close> f
  1739     apply (simp add: homeomorphism_def contvf contvg, auto)
  1740     by (metis f(1) rev_image_eqI rev_subsetD)
  1741   have 1: "openin (top_of_set t) (t \<inter> g -` u)"
  1742     apply (rule continuous_on_open [THEN iffD1, rule_format])
  1743     apply (rule \<open>continuous_on t g\<close>)
  1744     using \<open>g ` t = S\<close> apply (simp add: osu)
  1745     done
  1746   have 2: "\<exists>V. Q V \<and> y \<in> (t \<inter> g -` u) \<and> (t \<inter> g -` u) \<subseteq> V \<and> V \<subseteq> W"
  1747     apply (rule_tac x="f ` v" in exI)
  1748     apply (intro conjI Q [OF \<open>P v\<close> homv])
  1749     using \<open>W \<subseteq> t\<close> \<open>y \<in> W\<close>  \<open>f ` v \<subseteq> W\<close>  uv  apply (auto simp: fv)
  1750     done
  1751   show "\<exists>U. openin (top_of_set t) U \<and> (\<exists>v. Q v \<and> y \<in> U \<and> U \<subseteq> v \<and> v \<subseteq> W)"
  1752     by (meson 1 2)
  1753 qed
  1754 
  1755 lemma homeomorphism_locally:
  1756   fixes f:: "'a::metric_space \<Rightarrow> 'b::metric_space"
  1757   assumes hom: "homeomorphism S t f g"
  1758       and eq: "\<And>S t. homeomorphism S t f g \<Longrightarrow> (P S \<longleftrightarrow> Q t)"
  1759     shows "locally P S \<longleftrightarrow> locally Q t"
  1760 apply (rule iffI)
  1761 apply (erule homeomorphism_locally_imp [OF _ hom])
  1762 apply (simp add: eq)
  1763 apply (erule homeomorphism_locally_imp)
  1764 using eq homeomorphism_sym homeomorphism_symD [OF hom] apply blast+
  1765 done
  1766 
  1767 lemma homeomorphic_locally:
  1768   fixes S:: "'a::metric_space set" and T:: "'b::metric_space set"
  1769   assumes hom: "S homeomorphic T"
  1770           and iff: "\<And>X Y. X homeomorphic Y \<Longrightarrow> (P X \<longleftrightarrow> Q Y)"
  1771     shows "locally P S \<longleftrightarrow> locally Q T"
  1772 proof -
  1773   obtain f g where hom: "homeomorphism S T f g"
  1774     using assms by (force simp: homeomorphic_def)
  1775   then show ?thesis
  1776     using homeomorphic_def local.iff
  1777     by (blast intro!: homeomorphism_locally)
  1778 qed
  1779 
  1780 lemma homeomorphic_local_compactness:
  1781   fixes S:: "'a::metric_space set" and T:: "'b::metric_space set"
  1782   shows "S homeomorphic T \<Longrightarrow> locally compact S \<longleftrightarrow> locally compact T"
  1783 by (simp add: homeomorphic_compactness homeomorphic_locally)
  1784 
  1785 lemma locally_translation:
  1786   fixes P :: "'a :: real_normed_vector set \<Rightarrow> bool"
  1787   shows
  1788    "(\<And>S. P (image (\<lambda>x. a + x) S) \<longleftrightarrow> P S)
  1789         \<Longrightarrow> locally P (image (\<lambda>x. a + x) S) \<longleftrightarrow> locally P S"
  1790 apply (rule homeomorphism_locally [OF homeomorphism_translation])
  1791 apply (simp add: homeomorphism_def)
  1792 by metis
  1793 
  1794 lemma locally_injective_linear_image:
  1795   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1796   assumes f: "linear f" "inj f" and iff: "\<And>S. P (f ` S) \<longleftrightarrow> Q S"
  1797     shows "locally P (f ` S) \<longleftrightarrow> locally Q S"
  1798 apply (rule linear_homeomorphism_image [OF f])
  1799 apply (rule_tac f=g and g = f in homeomorphism_locally, assumption)
  1800 by (metis iff homeomorphism_def)
  1801 
  1802 lemma locally_open_map_image:
  1803   fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
  1804   assumes P: "locally P S"
  1805       and f: "continuous_on S f"
  1806       and oo: "\<And>t. openin (top_of_set S) t
  1807                    \<Longrightarrow> openin (top_of_set (f ` S)) (f ` t)"
  1808       and Q: "\<And>t. \<lbrakk>t \<subseteq> S; P t\<rbrakk> \<Longrightarrow> Q(f ` t)"
  1809     shows "locally Q (f ` S)"
  1810 proof (clarsimp simp add: locally_def)
  1811   fix W y
  1812   assume oiw: "openin (top_of_set (f ` S)) W" and "y \<in> W"
  1813   then have "W \<subseteq> f ` S" by (simp add: openin_euclidean_subtopology_iff)
  1814   have oivf: "openin (top_of_set S) (S \<inter> f -` W)"
  1815     by (rule continuous_on_open [THEN iffD1, rule_format, OF f oiw])
  1816   then obtain x where "x \<in> S" "f x = y"
  1817     using \<open>W \<subseteq> f ` S\<close> \<open>y \<in> W\<close> by blast
  1818   then obtain U V
  1819     where "openin (top_of_set S) U" "P V" "x \<in> U" "U \<subseteq> V" "V \<subseteq> S \<inter> f -` W"
  1820     using P [unfolded locally_def, rule_format, of "(S \<inter> f -` W)" x] oivf \<open>y \<in> W\<close>
  1821     by auto
  1822   then show "\<exists>X. openin (top_of_set (f ` S)) X \<and> (\<exists>Y. Q Y \<and> y \<in> X \<and> X \<subseteq> Y \<and> Y \<subseteq> W)"
  1823     apply (rule_tac x="f ` U" in exI)
  1824     apply (rule conjI, blast intro!: oo)
  1825     apply (rule_tac x="f ` V" in exI)
  1826     apply (force simp: \<open>f x = y\<close> rev_image_eqI intro: Q)
  1827     done
  1828 qed
  1829 
  1830 
  1831 subsection\<open>An induction principle for connected sets\<close>
  1832 
  1833 proposition connected_induction:
  1834   assumes "connected S"
  1835       and opD: "\<And>T a. \<lbrakk>openin (top_of_set S) T; a \<in> T\<rbrakk> \<Longrightarrow> \<exists>z. z \<in> T \<and> P z"
  1836       and opI: "\<And>a. a \<in> S
  1837              \<Longrightarrow> \<exists>T. openin (top_of_set S) T \<and> a \<in> T \<and>
  1838                      (\<forall>x \<in> T. \<forall>y \<in> T. P x \<and> P y \<and> Q x \<longrightarrow> Q y)"
  1839       and etc: "a \<in> S" "b \<in> S" "P a" "P b" "Q a"
  1840     shows "Q b"
  1841 proof -
  1842   have 1: "openin (top_of_set S)
  1843              {b. \<exists>T. openin (top_of_set S) T \<and>
  1844                      b \<in> T \<and> (\<forall>x\<in>T. P x \<longrightarrow> Q x)}"
  1845     apply (subst openin_subopen, clarify)
  1846     apply (rule_tac x=T in exI, auto)
  1847     done
  1848   have 2: "openin (top_of_set S)
  1849              {b. \<exists>T. openin (top_of_set S) T \<and>
  1850                      b \<in> T \<and> (\<forall>x\<in>T. P x \<longrightarrow> \<not> Q x)}"
  1851     apply (subst openin_subopen, clarify)
  1852     apply (rule_tac x=T in exI, auto)
  1853     done
  1854   show ?thesis
  1855     using \<open>connected S\<close>
  1856     apply (simp only: connected_openin HOL.not_ex HOL.de_Morgan_conj)
  1857     apply (elim disjE allE)
  1858          apply (blast intro: 1)
  1859         apply (blast intro: 2, simp_all)
  1860        apply clarify apply (metis opI)
  1861       using opD apply (blast intro: etc elim: dest:)
  1862      using opI etc apply meson+
  1863     done
  1864 qed
  1865 
  1866 lemma connected_equivalence_relation_gen:
  1867   assumes "connected S"
  1868       and etc: "a \<in> S" "b \<in> S" "P a" "P b"
  1869       and trans: "\<And>x y z. \<lbrakk>R x y; R y z\<rbrakk> \<Longrightarrow> R x z"
  1870       and opD: "\<And>T a. \<lbrakk>openin (top_of_set S) T; a \<in> T\<rbrakk> \<Longrightarrow> \<exists>z. z \<in> T \<and> P z"
  1871       and opI: "\<And>a. a \<in> S
  1872              \<Longrightarrow> \<exists>T. openin (top_of_set S) T \<and> a \<in> T \<and>
  1873                      (\<forall>x \<in> T. \<forall>y \<in> T. P x \<and> P y \<longrightarrow> R x y)"
  1874     shows "R a b"
  1875 proof -
  1876   have "\<And>a b c. \<lbrakk>a \<in> S; P a; b \<in> S; c \<in> S; P b; P c; R a b\<rbrakk> \<Longrightarrow> R a c"
  1877     apply (rule connected_induction [OF \<open>connected S\<close> opD], simp_all)
  1878     by (meson trans opI)
  1879   then show ?thesis by (metis etc opI)
  1880 qed
  1881 
  1882 lemma connected_induction_simple:
  1883   assumes "connected S"
  1884       and etc: "a \<in> S" "b \<in> S" "P a"
  1885       and opI: "\<And>a. a \<in> S
  1886              \<Longrightarrow> \<exists>T. openin (top_of_set S) T \<and> a \<in> T \<and>
  1887                      (\<forall>x \<in> T. \<forall>y \<in> T. P x \<longrightarrow> P y)"
  1888     shows "P b"
  1889 apply (rule connected_induction [OF \<open>connected S\<close> _, where P = "\<lambda>x. True"], blast)
  1890 apply (frule opI)
  1891 using etc apply simp_all
  1892 done
  1893 
  1894 lemma connected_equivalence_relation:
  1895   assumes "connected S"
  1896       and etc: "a \<in> S" "b \<in> S"
  1897       and sym: "\<And>x y. \<lbrakk>R x y; x \<in> S; y \<in> S\<rbrakk> \<Longrightarrow> R y x"
  1898       and trans: "\<And>x y z. \<lbrakk>R x y; R y z; x \<in> S; y \<in> S; z \<in> S\<rbrakk> \<Longrightarrow> R x z"
  1899       and opI: "\<And>a. a \<in> S \<Longrightarrow> \<exists>T. openin (top_of_set S) T \<and> a \<in> T \<and> (\<forall>x \<in> T. R a x)"
  1900     shows "R a b"
  1901 proof -
  1902   have "\<And>a b c. \<lbrakk>a \<in> S; b \<in> S; c \<in> S; R a b\<rbrakk> \<Longrightarrow> R a c"
  1903     apply (rule connected_induction_simple [OF \<open>connected S\<close>], simp_all)
  1904     by (meson local.sym local.trans opI openin_imp_subset subsetCE)
  1905   then show ?thesis by (metis etc opI)
  1906 qed
  1907 
  1908 lemma locally_constant_imp_constant:
  1909   assumes "connected S"
  1910       and opI: "\<And>a. a \<in> S
  1911              \<Longrightarrow> \<exists>T. openin (top_of_set S) T \<and> a \<in> T \<and> (\<forall>x \<in> T. f x = f a)"
  1912     shows "f constant_on S"
  1913 proof -
  1914   have "\<And>x y. x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> f x = f y"
  1915     apply (rule connected_equivalence_relation [OF \<open>connected S\<close>], simp_all)
  1916     by (metis opI)
  1917   then show ?thesis
  1918     by (metis constant_on_def)
  1919 qed
  1920 
  1921 lemma locally_constant:
  1922      "connected S \<Longrightarrow> locally (\<lambda>U. f constant_on U) S \<longleftrightarrow> f constant_on S"
  1923 apply (simp add: locally_def)
  1924 apply (rule iffI)
  1925  apply (rule locally_constant_imp_constant, assumption)
  1926  apply (metis (mono_tags, hide_lams) constant_on_def constant_on_subset openin_subtopology_self)
  1927 by (meson constant_on_subset openin_imp_subset order_refl)
  1928 
  1929 
  1930 subsection\<open>Basic properties of local compactness\<close>
  1931 
  1932 proposition locally_compact:
  1933   fixes s :: "'a :: metric_space set"
  1934   shows
  1935     "locally compact s \<longleftrightarrow>
  1936      (\<forall>x \<in> s. \<exists>u v. x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> s \<and>
  1937                     openin (top_of_set s) u \<and> compact v)"
  1938      (is "?lhs = ?rhs")
  1939 proof
  1940   assume ?lhs
  1941   then show ?rhs
  1942     apply clarify
  1943     apply (erule_tac w = "s \<inter> ball x 1" in locallyE)
  1944     by auto
  1945 next
  1946   assume r [rule_format]: ?rhs
  1947   have *: "\<exists>u v.
  1948               openin (top_of_set s) u \<and>
  1949               compact v \<and> x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> s \<inter> T"
  1950           if "open T" "x \<in> s" "x \<in> T" for x T
  1951   proof -
  1952     obtain u v where uv: "x \<in> u" "u \<subseteq> v" "v \<subseteq> s" "compact v" "openin (top_of_set s) u"
  1953       using r [OF \<open>x \<in> s\<close>] by auto
  1954     obtain e where "e>0" and e: "cball x e \<subseteq> T"
  1955       using open_contains_cball \<open>open T\<close> \<open>x \<in> T\<close> by blast
  1956     show ?thesis
  1957       apply (rule_tac x="(s \<inter> ball x e) \<inter> u" in exI)
  1958       apply (rule_tac x="cball x e \<inter> v" in exI)
  1959       using that \<open>e > 0\<close> e uv
  1960       apply auto
  1961       done
  1962   qed
  1963   show ?lhs
  1964     apply (rule locallyI)
  1965     apply (subst (asm) openin_open)
  1966     apply (blast intro: *)
  1967     done
  1968 qed
  1969 
  1970 lemma locally_compactE:
  1971   fixes s :: "'a :: metric_space set"
  1972   assumes "locally compact s"
  1973   obtains u v where "\<And>x. x \<in> s \<Longrightarrow> x \<in> u x \<and> u x \<subseteq> v x \<and> v x \<subseteq> s \<and>
  1974                              openin (top_of_set s) (u x) \<and> compact (v x)"
  1975 using assms
  1976 unfolding locally_compact by metis
  1977 
  1978 lemma locally_compact_alt:
  1979   fixes s :: "'a :: heine_borel set"
  1980   shows "locally compact s \<longleftrightarrow>
  1981          (\<forall>x \<in> s. \<exists>u. x \<in> u \<and>
  1982                     openin (top_of_set s) u \<and> compact(closure u) \<and> closure u \<subseteq> s)"
  1983 apply (simp add: locally_compact)
  1984 apply (intro ball_cong ex_cong refl iffI)
  1985 apply (metis bounded_subset closure_eq closure_mono compact_eq_bounded_closed dual_order.trans)
  1986 by (meson closure_subset compact_closure)
  1987 
  1988 lemma locally_compact_Int_cball:
  1989   fixes s :: "'a :: heine_borel set"
  1990   shows "locally compact s \<longleftrightarrow> (\<forall>x \<in> s. \<exists>e. 0 < e \<and> closed(cball x e \<inter> s))"
  1991         (is "?lhs = ?rhs")
  1992 proof
  1993   assume ?lhs
  1994   then show ?rhs
  1995     apply (simp add: locally_compact openin_contains_cball)
  1996     apply (clarify | assumption | drule bspec)+
  1997     by (metis (no_types, lifting)  compact_cball compact_imp_closed compact_Int inf.absorb_iff2 inf.orderE inf_sup_aci(2))
  1998 next
  1999   assume ?rhs
  2000   then show ?lhs
  2001     apply (simp add: locally_compact openin_contains_cball)
  2002     apply (clarify | assumption | drule bspec)+
  2003     apply (rule_tac x="ball x e \<inter> s" in exI, simp)
  2004     apply (rule_tac x="cball x e \<inter> s" in exI)
  2005     using compact_eq_bounded_closed
  2006     apply auto
  2007     apply (metis open_ball le_infI1 mem_ball open_contains_cball_eq)
  2008     done
  2009 qed
  2010 
  2011 lemma locally_compact_compact:
  2012   fixes s :: "'a :: heine_borel set"
  2013   shows "locally compact s \<longleftrightarrow>
  2014          (\<forall>k. k \<subseteq> s \<and> compact k
  2015               \<longrightarrow> (\<exists>u v. k \<subseteq> u \<and> u \<subseteq> v \<and> v \<subseteq> s \<and>
  2016                          openin (top_of_set s) u \<and> compact v))"
  2017         (is "?lhs = ?rhs")
  2018 proof
  2019   assume ?lhs
  2020   then obtain u v where
  2021     uv: "\<And>x. x \<in> s \<Longrightarrow> x \<in> u x \<and> u x \<subseteq> v x \<and> v x \<subseteq> s \<and>
  2022                              openin (top_of_set s) (u x) \<and> compact (v x)"
  2023     by (metis locally_compactE)
  2024   have *: "\<exists>u v. k \<subseteq> u \<and> u \<subseteq> v \<and> v \<subseteq> s \<and> openin (top_of_set s) u \<and> compact v"
  2025           if "k \<subseteq> s" "compact k" for k
  2026   proof -
  2027     have "\<And>C. (\<forall>c\<in>C. openin (top_of_set k) c) \<and> k \<subseteq> \<Union>C \<Longrightarrow>
  2028                     \<exists>D\<subseteq>C. finite D \<and> k \<subseteq> \<Union>D"
  2029       using that by (simp add: compact_eq_openin_cover)
  2030     moreover have "\<forall>c \<in> (\<lambda>x. k \<inter> u x) ` k. openin (top_of_set k) c"
  2031       using that by clarify (metis subsetD inf.absorb_iff2 openin_subset openin_subtopology_Int_subset topspace_euclidean_subtopology uv)
  2032     moreover have "k \<subseteq> \<Union>((\<lambda>x. k \<inter> u x) ` k)"
  2033       using that by clarsimp (meson subsetCE uv)
  2034     ultimately obtain D where "D \<subseteq> (\<lambda>x. k \<inter> u x) ` k" "finite D" "k \<subseteq> \<Union>D"
  2035       by metis
  2036     then obtain T where T: "T \<subseteq> k" "finite T" "k \<subseteq> \<Union>((\<lambda>x. k \<inter> u x) ` T)"
  2037       by (metis finite_subset_image)
  2038     have Tuv: "\<Union>(u ` T) \<subseteq> \<Union>(v ` T)"
  2039       using T that by (force simp: dest!: uv)
  2040     show ?thesis
  2041       apply (rule_tac x="\<Union>(u ` T)" in exI)
  2042       apply (rule_tac x="\<Union>(v ` T)" in exI)
  2043       apply (simp add: Tuv)
  2044       using T that
  2045       apply (auto simp: dest!: uv)
  2046       done
  2047   qed
  2048   show ?rhs
  2049     by (blast intro: *)
  2050 next
  2051   assume ?rhs
  2052   then show ?lhs
  2053     apply (clarsimp simp add: locally_compact)
  2054     apply (drule_tac x="{x}" in spec, simp)
  2055     done
  2056 qed
  2057 
  2058 lemma open_imp_locally_compact:
  2059   fixes s :: "'a :: heine_borel set"
  2060   assumes "open s"
  2061     shows "locally compact s"
  2062 proof -
  2063   have *: "\<exists>u v. x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> s \<and> openin (top_of_set s) u \<and> compact v"
  2064           if "x \<in> s" for x
  2065   proof -
  2066     obtain e where "e>0" and e: "cball x e \<subseteq> s"
  2067       using open_contains_cball assms \<open>x \<in> s\<close> by blast
  2068     have ope: "openin (top_of_set s) (ball x e)"
  2069       by (meson e open_ball ball_subset_cball dual_order.trans open_subset)
  2070     show ?thesis
  2071       apply (rule_tac x="ball x e" in exI)
  2072       apply (rule_tac x="cball x e" in exI)
  2073       using \<open>e > 0\<close> e apply (auto simp: ope)
  2074       done
  2075   qed
  2076   show ?thesis
  2077     unfolding locally_compact
  2078     by (blast intro: *)
  2079 qed
  2080 
  2081 lemma closed_imp_locally_compact:
  2082   fixes s :: "'a :: heine_borel set"
  2083   assumes "closed s"
  2084     shows "locally compact s"
  2085 proof -
  2086   have *: "\<exists>u v. x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> s \<and>
  2087                  openin (top_of_set s) u \<and> compact v"
  2088           if "x \<in> s" for x
  2089   proof -
  2090     show ?thesis
  2091       apply (rule_tac x = "s \<inter> ball x 1" in exI)
  2092       apply (rule_tac x = "s \<inter> cball x 1" in exI)
  2093       using \<open>x \<in> s\<close> assms apply auto
  2094       done
  2095   qed
  2096   show ?thesis
  2097     unfolding locally_compact
  2098     by (blast intro: *)
  2099 qed
  2100 
  2101 lemma locally_compact_UNIV: "locally compact (UNIV :: 'a :: heine_borel set)"
  2102   by (simp add: closed_imp_locally_compact)
  2103 
  2104 lemma locally_compact_Int:
  2105   fixes s :: "'a :: t2_space set"
  2106   shows "\<lbrakk>locally compact s; locally compact t\<rbrakk> \<Longrightarrow> locally compact (s \<inter> t)"
  2107 by (simp add: compact_Int locally_Int)
  2108 
  2109 lemma locally_compact_closedin:
  2110   fixes s :: "'a :: heine_borel set"
  2111   shows "\<lbrakk>closedin (top_of_set s) t; locally compact s\<rbrakk>
  2112         \<Longrightarrow> locally compact t"
  2113 unfolding closedin_closed
  2114 using closed_imp_locally_compact locally_compact_Int by blast
  2115 
  2116 lemma locally_compact_delete:
  2117      fixes s :: "'a :: t1_space set"
  2118      shows "locally compact s \<Longrightarrow> locally compact (s - {a})"
  2119   by (auto simp: openin_delete locally_open_subset)
  2120 
  2121 lemma locally_closed:
  2122   fixes s :: "'a :: heine_borel set"
  2123   shows "locally closed s \<longleftrightarrow> locally compact s"
  2124         (is "?lhs = ?rhs")
  2125 proof
  2126   assume ?lhs
  2127   then show ?rhs
  2128     apply (simp only: locally_def)
  2129     apply (erule all_forward imp_forward asm_rl exE)+
  2130     apply (rule_tac x = "u \<inter> ball x 1" in exI)
  2131     apply (rule_tac x = "v \<inter> cball x 1" in exI)
  2132     apply (force intro: openin_trans)
  2133     done
  2134 next
  2135   assume ?rhs then show ?lhs
  2136     using compact_eq_bounded_closed locally_mono by blast
  2137 qed
  2138 
  2139 lemma locally_compact_openin_Un:
  2140   fixes S :: "'a::euclidean_space set"
  2141   assumes LCS: "locally compact S" and LCT:"locally compact T"
  2142       and opS: "openin (top_of_set (S \<union> T)) S"
  2143       and opT: "openin (top_of_set (S \<union> T)) T"
  2144     shows "locally compact (S \<union> T)"
  2145 proof -
  2146   have "\<exists>e>0. closed (cball x e \<inter> (S \<union> T))" if "x \<in> S" for x
  2147   proof -
  2148     obtain e1 where "e1 > 0" and e1: "closed (cball x e1 \<inter> S)"
  2149       using LCS \<open>x \<in> S\<close> unfolding locally_compact_Int_cball by blast
  2150     moreover obtain e2 where "e2 > 0" and e2: "cball x e2 \<inter> (S \<union> T) \<subseteq> S"
  2151       by (meson \<open>x \<in> S\<close> opS openin_contains_cball)
  2152     then have "cball x e2 \<inter> (S \<union> T) = cball x e2 \<inter> S"
  2153       by force
  2154     ultimately show ?thesis
  2155       apply (rule_tac x="min e1 e2" in exI)
  2156       apply (auto simp: cball_min_Int \<open>e2 > 0\<close> inf_assoc closed_Int)
  2157       by (metis closed_Int closed_cball inf_left_commute)
  2158   qed
  2159   moreover have "\<exists>e>0. closed (cball x e \<inter> (S \<union> T))" if "x \<in> T" for x
  2160   proof -
  2161     obtain e1 where "e1 > 0" and e1: "closed (cball x e1 \<inter> T)"
  2162       using LCT \<open>x \<in> T\<close> unfolding locally_compact_Int_cball by blast
  2163     moreover obtain e2 where "e2 > 0" and e2: "cball x e2 \<inter> (S \<union> T) \<subseteq> T"
  2164       by (meson \<open>x \<in> T\<close> opT openin_contains_cball)
  2165     then have "cball x e2 \<inter> (S \<union> T) = cball x e2 \<inter> T"
  2166       by force
  2167     ultimately show ?thesis
  2168       apply (rule_tac x="min e1 e2" in exI)
  2169       apply (auto simp: cball_min_Int \<open>e2 > 0\<close> inf_assoc closed_Int)
  2170       by (metis closed_Int closed_cball inf_left_commute)
  2171   qed
  2172   ultimately show ?thesis
  2173     by (force simp: locally_compact_Int_cball)
  2174 qed
  2175 
  2176 lemma locally_compact_closedin_Un:
  2177   fixes S :: "'a::euclidean_space set"
  2178   assumes LCS: "locally compact S" and LCT:"locally compact T"
  2179       and clS: "closedin (top_of_set (S \<union> T)) S"
  2180       and clT: "closedin (top_of_set (S \<union> T)) T"
  2181     shows "locally compact (S \<union> T)"
  2182 proof -
  2183   have "\<exists>e>0. closed (cball x e \<inter> (S \<union> T))" if "x \<in> S" "x \<in> T" for x
  2184   proof -
  2185     obtain e1 where "e1 > 0" and e1: "closed (cball x e1 \<inter> S)"
  2186       using LCS \<open>x \<in> S\<close> unfolding locally_compact_Int_cball by blast
  2187     moreover
  2188     obtain e2 where "e2 > 0" and e2: "closed (cball x e2 \<inter> T)"
  2189       using LCT \<open>x \<in> T\<close> unfolding locally_compact_Int_cball by blast
  2190     ultimately show ?thesis
  2191       apply (rule_tac x="min e1 e2" in exI)
  2192       apply (auto simp: cball_min_Int \<open>e2 > 0\<close> inf_assoc closed_Int Int_Un_distrib)
  2193       by (metis closed_Int closed_Un closed_cball inf_left_commute)
  2194   qed
  2195   moreover
  2196   have "\<exists>e>0. closed (cball x e \<inter> (S \<union> T))" if x: "x \<in> S" "x \<notin> T" for x
  2197   proof -
  2198     obtain e1 where "e1 > 0" and e1: "closed (cball x e1 \<inter> S)"
  2199       using LCS \<open>x \<in> S\<close> unfolding locally_compact_Int_cball by blast
  2200     moreover
  2201     obtain e2 where "e2>0" and "cball x e2 \<inter> (S \<union> T) \<subseteq> S - T"
  2202       using clT x by (fastforce simp: openin_contains_cball closedin_def)
  2203     then have "closed (cball x e2 \<inter> T)"
  2204     proof -
  2205       have "{} = T - (T - cball x e2)"
  2206         using Diff_subset Int_Diff \<open>cball x e2 \<inter> (S \<union> T) \<subseteq> S - T\<close> by auto
  2207       then show ?thesis
  2208         by (simp add: Diff_Diff_Int inf_commute)
  2209     qed
  2210     ultimately show ?thesis
  2211       apply (rule_tac x="min e1 e2" in exI)
  2212       apply (auto simp: cball_min_Int \<open>e2 > 0\<close> inf_assoc closed_Int Int_Un_distrib)
  2213       by (metis closed_Int closed_Un closed_cball inf_left_commute)
  2214   qed
  2215   moreover
  2216   have "\<exists>e>0. closed (cball x e \<inter> (S \<union> T))" if x: "x \<notin> S" "x \<in> T" for x
  2217   proof -
  2218     obtain e1 where "e1 > 0" and e1: "closed (cball x e1 \<inter> T)"
  2219       using LCT \<open>x \<in> T\<close> unfolding locally_compact_Int_cball by blast
  2220     moreover
  2221     obtain e2 where "e2>0" and "cball x e2 \<inter> (S \<union> T) \<subseteq> S \<union> T - S"
  2222       using clS x by (fastforce simp: openin_contains_cball closedin_def)
  2223     then have "closed (cball x e2 \<inter> S)"
  2224       by (metis Diff_disjoint Int_empty_right closed_empty inf.left_commute inf.orderE inf_sup_absorb)
  2225     ultimately show ?thesis
  2226       apply (rule_tac x="min e1 e2" in exI)
  2227       apply (auto simp: cball_min_Int \<open>e2 > 0\<close> inf_assoc closed_Int Int_Un_distrib)
  2228       by (metis closed_Int closed_Un closed_cball inf_left_commute)
  2229   qed
  2230   ultimately show ?thesis
  2231     by (auto simp: locally_compact_Int_cball)
  2232 qed
  2233 
  2234 lemma locally_compact_Times:
  2235   fixes S :: "'a::euclidean_space set" and T :: "'b::euclidean_space set"
  2236   shows "\<lbrakk>locally compact S; locally compact T\<rbrakk> \<Longrightarrow> locally compact (S \<times> T)"
  2237   by (auto simp: compact_Times locally_Times)
  2238 
  2239 lemma locally_compact_compact_subopen:
  2240   fixes S :: "'a :: heine_borel set"
  2241   shows
  2242    "locally compact S \<longleftrightarrow>
  2243     (\<forall>K T. K \<subseteq> S \<and> compact K \<and> open T \<and> K \<subseteq> T
  2244           \<longrightarrow> (\<exists>U V. K \<subseteq> U \<and> U \<subseteq> V \<and> U \<subseteq> T \<and> V \<subseteq> S \<and>
  2245                      openin (top_of_set S) U \<and> compact V))"
  2246    (is "?lhs = ?rhs")
  2247 proof
  2248   assume L: ?lhs
  2249   show ?rhs
  2250   proof clarify
  2251     fix K :: "'a set" and T :: "'a set"
  2252     assume "K \<subseteq> S" and "compact K" and "open T" and "K \<subseteq> T"
  2253     obtain U V where "K \<subseteq> U" "U \<subseteq> V" "V \<subseteq> S" "compact V"
  2254                  and ope: "openin (top_of_set S) U"
  2255       using L unfolding locally_compact_compact by (meson \<open>K \<subseteq> S\<close> \<open>compact K\<close>)
  2256     show "\<exists>U V. K \<subseteq> U \<and> U \<subseteq> V \<and> U \<subseteq> T \<and> V \<subseteq> S \<and>
  2257                 openin (top_of_set S) U \<and> compact V"
  2258     proof (intro exI conjI)
  2259       show "K \<subseteq> U \<inter> T"
  2260         by (simp add: \<open>K \<subseteq> T\<close> \<open>K \<subseteq> U\<close>)
  2261       show "U \<inter> T \<subseteq> closure(U \<inter> T)"
  2262         by (rule closure_subset)
  2263       show "closure (U \<inter> T) \<subseteq> S"
  2264         by (metis \<open>U \<subseteq> V\<close> \<open>V \<subseteq> S\<close> \<open>compact V\<close> closure_closed closure_mono compact_imp_closed inf.cobounded1 subset_trans)
  2265       show "openin (top_of_set S) (U \<inter> T)"
  2266         by (simp add: \<open>open T\<close> ope openin_Int_open)
  2267       show "compact (closure (U \<inter> T))"
  2268         by (meson Int_lower1 \<open>U \<subseteq> V\<close> \<open>compact V\<close> bounded_subset compact_closure compact_eq_bounded_closed)
  2269     qed auto
  2270   qed
  2271 next
  2272   assume ?rhs then show ?lhs
  2273     unfolding locally_compact_compact
  2274     by (metis open_openin openin_topspace subtopology_superset top.extremum topspace_euclidean_subtopology)
  2275 qed
  2276 
  2277 
  2278 subsection\<open>Sura-Bura's results about compact components of sets\<close>
  2279 
  2280 proposition Sura_Bura_compact:
  2281   fixes S :: "'a::euclidean_space set"
  2282   assumes "compact S" and C: "C \<in> components S"
  2283   shows "C = \<Inter>{T. C \<subseteq> T \<and> openin (top_of_set S) T \<and>
  2284                            closedin (top_of_set S) T}"
  2285          (is "C = \<Inter>?\<T>")
  2286 proof
  2287   obtain x where x: "C = connected_component_set S x" and "x \<in> S"
  2288     using C by (auto simp: components_def)
  2289   have "C \<subseteq> S"
  2290     by (simp add: C in_components_subset)
  2291   have "\<Inter>?\<T> \<subseteq> connected_component_set S x"
  2292   proof (rule connected_component_maximal)
  2293     have "x \<in> C"
  2294       by (simp add: \<open>x \<in> S\<close> x)
  2295     then show "x \<in> \<Inter>?\<T>"
  2296       by blast
  2297     have clo: "closed (\<Inter>?\<T>)"
  2298       by (simp add: \<open>compact S\<close> closed_Inter closedin_compact_eq compact_imp_closed)
  2299     have False
  2300       if K1: "closedin (top_of_set (\<Inter>?\<T>)) K1" and
  2301          K2: "closedin (top_of_set (\<Inter>?\<T>)) K2" and
  2302          K12_Int: "K1 \<inter> K2 = {}" and K12_Un: "K1 \<union> K2 = \<Inter>?\<T>" and "K1 \<noteq> {}" "K2 \<noteq> {}"
  2303        for K1 K2
  2304     proof -
  2305       have "closed K1" "closed K2"
  2306         using closedin_closed_trans clo K1 K2 by blast+
  2307       then obtain V1 V2 where "open V1" "open V2" "K1 \<subseteq> V1" "K2 \<subseteq> V2" and V12: "V1 \<inter> V2 = {}"
  2308         using separation_normal \<open>K1 \<inter> K2 = {}\<close> by metis
  2309       have SV12_ne: "(S - (V1 \<union> V2)) \<inter> (\<Inter>?\<T>) \<noteq> {}"
  2310       proof (rule compact_imp_fip)
  2311         show "compact (S - (V1 \<union> V2))"
  2312           by (simp add: \<open>open V1\<close> \<open>open V2\<close> \<open>compact S\<close> compact_diff open_Un)
  2313         show clo\<T>: "closed T" if "T \<in> ?\<T>" for T
  2314           using that \<open>compact S\<close>
  2315           by (force intro: closedin_closed_trans simp add: compact_imp_closed)
  2316         show "(S - (V1 \<union> V2)) \<inter> \<Inter>\<F> \<noteq> {}" if "finite \<F>" and \<F>: "\<F> \<subseteq> ?\<T>" for \<F>
  2317         proof
  2318           assume djo: "(S - (V1 \<union> V2)) \<inter> \<Inter>\<F> = {}"
  2319           obtain D where opeD: "openin (top_of_set S) D"
  2320                    and cloD: "closedin (top_of_set S) D"
  2321                    and "C \<subseteq> D" and DV12: "D \<subseteq> V1 \<union> V2"
  2322           proof (cases "\<F> = {}")
  2323             case True
  2324             with \<open>C \<subseteq> S\<close> djo that show ?thesis
  2325               by force
  2326           next
  2327             case False show ?thesis
  2328             proof
  2329               show ope: "openin (top_of_set S) (\<Inter>\<F>)"
  2330                 using openin_Inter \<open>finite \<F>\<close> False \<F> by blast
  2331               then show "closedin (top_of_set S) (\<Inter>\<F>)"
  2332                 by (meson clo\<T> \<F> closed_Inter closed_subset openin_imp_subset subset_eq)
  2333               show "C \<subseteq> \<Inter>\<F>"
  2334                 using \<F> by auto
  2335               show "\<Inter>\<F> \<subseteq> V1 \<union> V2"
  2336                 using ope djo openin_imp_subset by fastforce
  2337             qed
  2338           qed
  2339           have "connected C"
  2340             by (simp add: x)
  2341           have "closed D"
  2342             using \<open>compact S\<close> cloD closedin_closed_trans compact_imp_closed by blast
  2343           have cloV1: "closedin (top_of_set D) (D \<inter> closure V1)"
  2344             and cloV2: "closedin (top_of_set D) (D \<inter> closure V2)"
  2345             by (simp_all add: closedin_closed_Int)
  2346           moreover have "D \<inter> closure V1 = D \<inter> V1" "D \<inter> closure V2 = D \<inter> V2"
  2347             apply safe
  2348             using \<open>D \<subseteq> V1 \<union> V2\<close> \<open>open V1\<close> \<open>open V2\<close> V12
  2349                apply (simp_all add: closure_subset [THEN subsetD] closure_iff_nhds_not_empty, blast+)
  2350             done
  2351           ultimately have cloDV1: "closedin (top_of_set D) (D \<inter> V1)"
  2352                       and cloDV2:  "closedin (top_of_set D) (D \<inter> V2)"
  2353             by metis+
  2354           then obtain U1 U2 where "closed U1" "closed U2"
  2355                and D1: "D \<inter> V1 = D \<inter> U1" and D2: "D \<inter> V2 = D \<inter> U2"
  2356             by (auto simp: closedin_closed)
  2357           have "D \<inter> U1 \<inter> C \<noteq> {}"
  2358           proof
  2359             assume "D \<inter> U1 \<inter> C = {}"
  2360             then have *: "C \<subseteq> D \<inter> V2"
  2361               using D1 DV12 \<open>C \<subseteq> D\<close> by auto
  2362             have "\<Inter>?\<T> \<subseteq> D \<inter> V2"
  2363               apply (rule Inter_lower)
  2364               using * apply simp
  2365               by (meson cloDV2 \<open>open V2\<close> cloD closedin_trans le_inf_iff opeD openin_Int_open)
  2366             then show False
  2367               using K1 V12 \<open>K1 \<noteq> {}\<close> \<open>K1 \<subseteq> V1\<close> closedin_imp_subset by blast
  2368           qed
  2369           moreover have "D \<inter> U2 \<inter> C \<noteq> {}"
  2370           proof
  2371             assume "D \<inter> U2 \<inter> C = {}"
  2372             then have *: "C \<subseteq> D \<inter> V1"
  2373               using D2 DV12 \<open>C \<subseteq> D\<close> by auto
  2374             have "\<Inter>?\<T> \<subseteq> D \<inter> V1"
  2375               apply (rule Inter_lower)
  2376               using * apply simp
  2377               by (meson cloDV1 \<open>open V1\<close> cloD closedin_trans le_inf_iff opeD openin_Int_open)
  2378             then show False
  2379               using K2 V12 \<open>K2 \<noteq> {}\<close> \<open>K2 \<subseteq> V2\<close> closedin_imp_subset by blast
  2380           qed
  2381           ultimately show False
  2382             using \<open>connected C\<close> unfolding connected_closed
  2383             apply (simp only: not_ex)
  2384             apply (drule_tac x="D \<inter> U1" in spec)
  2385             apply (drule_tac x="D \<inter> U2" in spec)
  2386             using \<open>C \<subseteq> D\<close> D1 D2 V12 DV12 \<open>closed U1\<close> \<open>closed U2\<close> \<open>closed D\<close>
  2387             by blast
  2388         qed
  2389       qed
  2390       show False
  2391         by (metis (full_types) DiffE UnE Un_upper2 SV12_ne \<open>K1 \<subseteq> V1\<close> \<open>K2 \<subseteq> V2\<close> disjoint_iff_not_equal subsetCE sup_ge1 K12_Un)
  2392     qed
  2393     then show "connected (\<Inter>?\<T>)"
  2394       by (auto simp: connected_closedin_eq)
  2395     show "\<Inter>?\<T> \<subseteq> S"
  2396       by (fastforce simp: C in_components_subset)
  2397   qed
  2398   with x show "\<Inter>?\<T> \<subseteq> C" by simp
  2399 qed auto
  2400 
  2401 
  2402 corollary Sura_Bura_clopen_subset:
  2403   fixes S :: "'a::euclidean_space set"
  2404   assumes S: "locally compact S" and C: "C \<in> components S" and "compact C"
  2405       and U: "open U" "C \<subseteq> U"
  2406   obtains K where "openin (top_of_set S) K" "compact K" "C \<subseteq> K" "K \<subseteq> U"
  2407 proof (rule ccontr)
  2408   assume "\<not> thesis"
  2409   with that have neg: "\<nexists>K. openin (top_of_set S) K \<and> compact K \<and> C \<subseteq> K \<and> K \<subseteq> U"
  2410     by metis
  2411   obtain V K where "C \<subseteq> V" "V \<subseteq> U" "V \<subseteq> K" "K \<subseteq> S" "compact K"
  2412                and opeSV: "openin (top_of_set S) V"
  2413     using S U \<open>compact C\<close>
  2414     apply (simp add: locally_compact_compact_subopen)
  2415     by (meson C in_components_subset)
  2416   let ?\<T> = "{T. C \<subseteq> T \<and> openin (top_of_set K) T \<and> compact T \<and> T \<subseteq> K}"
  2417   have CK: "C \<in> components K"
  2418     by (meson C \<open>C \<subseteq> V\<close> \<open>K \<subseteq> S\<close> \<open>V \<subseteq> K\<close> components_intermediate_subset subset_trans)
  2419   with \<open>compact K\<close>
  2420   have "C = \<Inter>{T. C \<subseteq> T \<and> openin (top_of_set K) T \<and> closedin (top_of_set K) T}"
  2421     by (simp add: Sura_Bura_compact)
  2422   then have Ceq: "C = \<Inter>?\<T>"
  2423     by (simp add: closedin_compact_eq \<open>compact K\<close>)
  2424   obtain W where "open W" and W: "V = S \<inter> W"
  2425     using opeSV by (auto simp: openin_open)
  2426   have "-(U \<inter> W) \<inter> \<Inter>?\<T> \<noteq> {}"
  2427   proof (rule closed_imp_fip_compact)
  2428     show "- (U \<inter> W) \<inter> \<Inter>\<F> \<noteq> {}"
  2429       if "finite \<F>" and \<F>: "\<F> \<subseteq> ?\<T>" for \<F>
  2430     proof (cases "\<F> = {}")
  2431       case True
  2432       have False if "U = UNIV" "W = UNIV"
  2433       proof -
  2434         have "V = S"
  2435           by (simp add: W \<open>W = UNIV\<close>)
  2436         with neg show False
  2437           using \<open>C \<subseteq> V\<close> \<open>K \<subseteq> S\<close> \<open>V \<subseteq> K\<close> \<open>V \<subseteq> U\<close> \<open>compact K\<close> by auto
  2438       qed
  2439       with True show ?thesis
  2440         by auto
  2441     next
  2442       case False
  2443       show ?thesis
  2444       proof
  2445         assume "- (U \<inter> W) \<inter> \<Inter>\<F> = {}"
  2446         then have FUW: "\<Inter>\<F> \<subseteq> U \<inter> W"
  2447           by blast
  2448         have "C \<subseteq> \<Inter>\<F>"
  2449           using \<F> by auto
  2450         moreover have "compact (\<Inter>\<F>)"
  2451           by (metis (no_types, lifting) compact_Inter False mem_Collect_eq subsetCE \<F>)
  2452         moreover have "\<Inter>\<F> \<subseteq> K"
  2453           using False that(2) by fastforce
  2454         moreover have opeKF: "openin (top_of_set K) (\<Inter>\<F>)"
  2455           using False \<F> \<open>finite \<F>\<close> by blast
  2456         then have opeVF: "openin (top_of_set V) (\<Inter>\<F>)"
  2457           using W \<open>K \<subseteq> S\<close> \<open>V \<subseteq> K\<close> opeKF \<open>\<Inter>\<F> \<subseteq> K\<close> FUW openin_subset_trans by fastforce
  2458         then have "openin (top_of_set S) (\<Inter>\<F>)"
  2459           by (metis opeSV openin_trans)
  2460         moreover have "\<Inter>\<F> \<subseteq> U"
  2461           by (meson \<open>V \<subseteq> U\<close> opeVF dual_order.trans openin_imp_subset)
  2462         ultimately show False
  2463           using neg by blast
  2464       qed
  2465     qed
  2466   qed (use \<open>open W\<close> \<open>open U\<close> in auto)
  2467   with W Ceq \<open>C \<subseteq> V\<close> \<open>C \<subseteq> U\<close> show False
  2468     by auto
  2469 qed
  2470 
  2471 
  2472 corollary Sura_Bura_clopen_subset_alt:
  2473   fixes S :: "'a::euclidean_space set"
  2474   assumes S: "locally compact S" and C: "C \<in> components S" and "compact C"
  2475       and opeSU: "openin (top_of_set S) U" and "C \<subseteq> U"
  2476   obtains K where "openin (top_of_set S) K" "compact K" "C \<subseteq> K" "K \<subseteq> U"
  2477 proof -
  2478   obtain V where "open V" "U = S \<inter> V"
  2479     using opeSU by (auto simp: openin_open)
  2480   with \<open>C \<subseteq> U\<close> have "C \<subseteq> V"
  2481     by auto
  2482   then show ?thesis
  2483     using Sura_Bura_clopen_subset [OF S C \<open>compact C\<close> \<open>open V\<close>]
  2484     by (metis \<open>U = S \<inter> V\<close> inf.bounded_iff openin_imp_subset that)
  2485 qed
  2486 
  2487 corollary Sura_Bura:
  2488   fixes S :: "'a::euclidean_space set"
  2489   assumes "locally compact S" "C \<in> components S" "compact C"
  2490   shows "C = \<Inter> {K. C \<subseteq> K \<and> compact K \<and> openin (top_of_set S) K}"
  2491          (is "C = ?rhs")
  2492 proof
  2493   show "?rhs \<subseteq> C"
  2494   proof (clarsimp, rule ccontr)
  2495     fix x
  2496     assume *: "\<forall>X. C \<subseteq> X \<and> compact X \<and> openin (top_of_set S) X \<longrightarrow> x \<in> X"
  2497       and "x \<notin> C"
  2498     obtain U V where "open U" "open V" "{x} \<subseteq> U" "C \<subseteq> V" "U \<inter> V = {}"
  2499       using separation_normal [of "{x}" C]
  2500       by (metis Int_empty_left \<open>x \<notin> C\<close> \<open>compact C\<close> closed_empty closed_insert compact_imp_closed insert_disjoint(1))
  2501     have "x \<notin> V"
  2502       using \<open>U \<inter> V = {}\<close> \<open>{x} \<subseteq> U\<close> by blast
  2503     then show False
  2504       by (meson "*" Sura_Bura_clopen_subset \<open>C \<subseteq> V\<close> \<open>open V\<close> assms(1) assms(2) assms(3) subsetCE)
  2505   qed
  2506 qed blast
  2507 
  2508 
  2509 subsection\<open>Special cases of local connectedness and path connectedness\<close>
  2510 
  2511 lemma locally_connected_1:
  2512   assumes
  2513     "\<And>v x. \<lbrakk>openin (top_of_set S) v; x \<in> v\<rbrakk>
  2514               \<Longrightarrow> \<exists>u. openin (top_of_set S) u \<and>
  2515                       connected u \<and> x \<in> u \<and> u \<subseteq> v"
  2516    shows "locally connected S"
  2517 apply (clarsimp simp add: locally_def)
  2518 apply (drule assms; blast)
  2519 done
  2520 
  2521 lemma locally_connected_2:
  2522   assumes "locally connected S"
  2523           "openin (top_of_set S) t"
  2524           "x \<in> t"
  2525    shows "openin (top_of_set S) (connected_component_set t x)"
  2526 proof -
  2527   { fix y :: 'a
  2528     let ?SS = "top_of_set S"
  2529     assume 1: "openin ?SS t"
  2530               "\<forall>w x. openin ?SS w \<and> x \<in> w \<longrightarrow> (\<exists>u. openin ?SS u \<and> (\<exists>v. connected v \<and> x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> w))"
  2531     and "connected_component t x y"
  2532     then have "y \<in> t" and y: "y \<in> connected_component_set t x"
  2533       using connected_component_subset by blast+
  2534     obtain F where
  2535       "\<forall>x y. (\<exists>w. openin ?SS w \<and> (\<exists>u. connected u \<and> x \<in> w \<and> w \<subseteq> u \<and> u \<subseteq> y)) = (openin ?SS (F x y) \<and> (\<exists>u. connected u \<and> x \<in> F x y \<and> F x y \<subseteq> u \<and> u \<subseteq> y))"
  2536       by moura
  2537     then obtain G where
  2538        "\<forall>a A. (\<exists>U. openin ?SS U \<and> (\<exists>V. connected V \<and> a \<in> U \<and> U \<subseteq> V \<and> V \<subseteq> A)) = (openin ?SS (F a A) \<and> connected (G a A) \<and> a \<in> F a A \<and> F a A \<subseteq> G a A \<and> G a A \<subseteq> A)"
  2539       by moura
  2540     then have *: "openin ?SS (F y t) \<and> connected (G y t) \<and> y \<in> F y t \<and> F y t \<subseteq> G y t \<and> G y t \<subseteq> t"
  2541       using 1 \<open>y \<in> t\<close> by presburger
  2542     have "G y t \<subseteq> connected_component_set t y"
  2543       by (metis (no_types) * connected_component_eq_self connected_component_mono contra_subsetD)
  2544     then have "\<exists>A. openin ?SS A \<and> y \<in> A \<and> A \<subseteq> connected_component_set t x"
  2545       by (metis (no_types) * connected_component_eq dual_order.trans y)
  2546   }
  2547   then show ?thesis
  2548     using assms openin_subopen by (force simp: locally_def)
  2549 qed
  2550 
  2551 lemma locally_connected_3:
  2552   assumes "\<And>t x. \<lbrakk>openin (top_of_set S) t; x \<in> t\<rbrakk>
  2553               \<Longrightarrow> openin (top_of_set S)
  2554                           (connected_component_set t x)"
  2555           "openin (top_of_set S) v" "x \<in> v"
  2556    shows  "\<exists>u. openin (top_of_set S) u \<and> connected u \<and> x \<in> u \<and> u \<subseteq> v"
  2557 using assms connected_component_subset by fastforce
  2558 
  2559 lemma locally_connected:
  2560   "locally connected S \<longleftrightarrow>
  2561    (\<forall>v x. openin (top_of_set S) v \<and> x \<in> v
  2562           \<longrightarrow> (\<exists>u. openin (top_of_set S) u \<and> connected u \<and> x \<in> u \<and> u \<subseteq> v))"
  2563 by (metis locally_connected_1 locally_connected_2 locally_connected_3)
  2564 
  2565 lemma locally_connected_open_connected_component:
  2566   "locally connected S \<longleftrightarrow>
  2567    (\<forall>t x. openin (top_of_set S) t \<and> x \<in> t
  2568           \<longrightarrow> openin (top_of_set S) (connected_component_set t x))"
  2569 by (metis locally_connected_1 locally_connected_2 locally_connected_3)
  2570 
  2571 lemma locally_path_connected_1:
  2572   assumes
  2573     "\<And>v x. \<lbrakk>openin (top_of_set S) v; x \<in> v\<rbrakk>
  2574               \<Longrightarrow> \<exists>u. openin (top_of_set S) u \<and> path_connected u \<and> x \<in> u \<and> u \<subseteq> v"
  2575    shows "locally path_connected S"
  2576 apply (clarsimp simp add: locally_def)
  2577 apply (drule assms; blast)
  2578 done
  2579 
  2580 lemma locally_path_connected_2:
  2581   assumes "locally path_connected S"
  2582           "openin (top_of_set S) t"
  2583           "x \<in> t"
  2584    shows "openin (top_of_set S) (path_component_set t x)"
  2585 proof -
  2586   { fix y :: 'a
  2587     let ?SS = "top_of_set S"
  2588     assume 1: "openin ?SS t"
  2589               "\<forall>w x. openin ?SS w \<and> x \<in> w \<longrightarrow> (\<exists>u. openin ?SS u \<and> (\<exists>v. path_connected v \<and> x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> w))"
  2590     and "path_component t x y"
  2591     then have "y \<in> t" and y: "y \<in> path_component_set t x"
  2592       using path_component_mem(2) by blast+
  2593     obtain F where
  2594       "\<forall>x y. (\<exists>w. openin ?SS w \<and> (\<exists>u. path_connected u \<and> x \<in> w \<and> w \<subseteq> u \<and> u \<subseteq> y)) = (openin ?SS (F x y) \<and> (\<exists>u. path_connected u \<and> x \<in> F x y \<and> F x y \<subseteq> u \<and> u \<subseteq> y))"
  2595       by moura
  2596     then obtain G where
  2597        "\<forall>a A. (\<exists>U. openin ?SS U \<and> (\<exists>V. path_connected V \<and> a \<in> U \<and> U \<subseteq> V \<and> V \<subseteq> A)) = (openin ?SS (F a A) \<and> path_connected (G a A) \<and> a \<in> F a A \<and> F a A \<subseteq> G a A \<and> G a A \<subseteq> A)"
  2598       by moura
  2599     then have *: "openin ?SS (F y t) \<and> path_connected (G y t) \<and> y \<in> F y t \<and> F y t \<subseteq> G y t \<and> G y t \<subseteq> t"
  2600       using 1 \<open>y \<in> t\<close> by presburger
  2601     have "G y t \<subseteq> path_component_set t y"
  2602       using * path_component_maximal rev_subsetD by blast
  2603     then have "\<exists>A. openin ?SS A \<and> y \<in> A \<and> A \<subseteq> path_component_set t x"
  2604       by (metis "*" \<open>G y t \<subseteq> path_component_set t y\<close> dual_order.trans path_component_eq y)
  2605   }
  2606   then show ?thesis
  2607     using assms openin_subopen by (force simp: locally_def)
  2608 qed
  2609 
  2610 lemma locally_path_connected_3:
  2611   assumes "\<And>t x. \<lbrakk>openin (top_of_set S) t; x \<in> t\<rbrakk>
  2612               \<Longrightarrow> openin (top_of_set S) (path_component_set t x)"
  2613           "openin (top_of_set S) v" "x \<in> v"
  2614    shows  "\<exists>u. openin (top_of_set S) u \<and> path_connected u \<and> x \<in> u \<and> u \<subseteq> v"
  2615 proof -
  2616   have "path_component v x x"
  2617     by (meson assms(3) path_component_refl)
  2618   then show ?thesis
  2619     by (metis assms(1) assms(2) assms(3) mem_Collect_eq path_component_subset path_connected_path_component)
  2620 qed
  2621 
  2622 proposition locally_path_connected:
  2623   "locally path_connected S \<longleftrightarrow>
  2624    (\<forall>v x. openin (top_of_set S) v \<and> x \<in> v
  2625           \<longrightarrow> (\<exists>u. openin (top_of_set S) u \<and> path_connected u \<and> x \<in> u \<and> u \<subseteq> v))"
  2626   by (metis locally_path_connected_1 locally_path_connected_2 locally_path_connected_3)
  2627 
  2628 proposition locally_path_connected_open_path_component:
  2629   "locally path_connected S \<longleftrightarrow>
  2630    (\<forall>t x. openin (top_of_set S) t \<and> x \<in> t
  2631           \<longrightarrow> openin (top_of_set S) (path_component_set t x))"
  2632   by (metis locally_path_connected_1 locally_path_connected_2 locally_path_connected_3)
  2633 
  2634 lemma locally_connected_open_component:
  2635   "locally connected S \<longleftrightarrow>
  2636    (\<forall>t c. openin (top_of_set S) t \<and> c \<in> components t
  2637           \<longrightarrow> openin (top_of_set S) c)"
  2638 by (metis components_iff locally_connected_open_connected_component)
  2639 
  2640 proposition locally_connected_im_kleinen:
  2641   "locally connected S \<longleftrightarrow>
  2642    (\<forall>v x. openin (top_of_set S) v \<and> x \<in> v
  2643        \<longrightarrow> (\<exists>u. openin (top_of_set S) u \<and>
  2644                 x \<in> u \<and> u \<subseteq> v \<and>
  2645                 (\<forall>y. y \<in> u \<longrightarrow> (\<exists>c. connected c \<and> c \<subseteq> v \<and> x \<in> c \<and> y \<in> c))))"
  2646    (is "?lhs = ?rhs")
  2647 proof
  2648   assume ?lhs
  2649   then show ?rhs
  2650     by (fastforce simp add: locally_connected)
  2651 next
  2652   assume ?rhs
  2653   have *: "\<exists>T. openin (top_of_set S) T \<and> x \<in> T \<and> T \<subseteq> c"
  2654        if "openin (top_of_set S) t" and c: "c \<in> components t" and "x \<in> c" for t c x
  2655   proof -
  2656     from that \<open>?rhs\<close> [rule_format, of t x]
  2657     obtain u where u:
  2658       "openin (top_of_set S) u \<and> x \<in> u \<and> u \<subseteq> t \<and>
  2659        (\<forall>y. y \<in> u \<longrightarrow> (\<exists>c. connected c \<and> c \<subseteq> t \<and> x \<in> c \<and> y \<in> c))"
  2660       using in_components_subset by auto
  2661     obtain F :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a" where
  2662       "\<forall>x y. (\<exists>z. z \<in> x \<and> y = connected_component_set x z) = (F x y \<in> x \<and> y = connected_component_set x (F x y))"
  2663       by moura
  2664     then have F: "F t c \<in> t \<and> c = connected_component_set t (F t c)"
  2665       by (meson components_iff c)
  2666     obtain G :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a" where
  2667         G: "\<forall>x y. (\<exists>z. z \<in> y \<and> z \<notin> x) = (G x y \<in> y \<and> G x y \<notin> x)"
  2668       by moura
  2669      have "G c u \<notin> u \<or> G c u \<in> c"
  2670       using F by (metis (full_types) u connected_componentI connected_component_eq mem_Collect_eq that(3))
  2671     then show ?thesis
  2672       using G u by auto
  2673   qed
  2674   show ?lhs
  2675     apply (clarsimp simp add: locally_connected_open_component)
  2676     apply (subst openin_subopen)
  2677     apply (blast intro: *)
  2678     done
  2679 qed
  2680 
  2681 proposition locally_path_connected_im_kleinen:
  2682   "locally path_connected S \<longleftrightarrow>
  2683    (\<forall>v x. openin (top_of_set S) v \<and> x \<in> v
  2684        \<longrightarrow> (\<exists>u. openin (top_of_set S) u \<and>
  2685                 x \<in> u \<and> u \<subseteq> v \<and>
  2686                 (\<forall>y. y \<in> u \<longrightarrow> (\<exists>p. path p \<and> path_image p \<subseteq> v \<and>
  2687                                 pathstart p = x \<and> pathfinish p = y))))"
  2688    (is "?lhs = ?rhs")
  2689 proof
  2690   assume ?lhs
  2691   then show ?rhs
  2692     apply (simp add: locally_path_connected path_connected_def)
  2693     apply (erule all_forward ex_forward imp_forward conjE | simp)+
  2694     by (meson dual_order.trans)
  2695 next
  2696   assume ?rhs
  2697   have *: "\<exists>T. openin (top_of_set S) T \<and>
  2698                x \<in> T \<and> T \<subseteq> path_component_set u z"
  2699        if "openin (top_of_set S) u" and "z \<in> u" and c: "path_component u z x" for u z x
  2700   proof -
  2701     have "x \<in> u"
  2702       by (meson c path_component_mem(2))
  2703     with that \<open>?rhs\<close> [rule_format, of u x]
  2704     obtain U where U:
  2705       "openin (top_of_set S) U \<and> x \<in> U \<and> U \<subseteq> u \<and>
  2706        (\<forall>y. y \<in> U \<longrightarrow> (\<exists>p. path p \<and> path_image p \<subseteq> u \<and> pathstart p = x \<and> pathfinish p = y))"
  2707        by blast
  2708     show ?thesis
  2709       apply (rule_tac x=U in exI)
  2710       apply (auto simp: U)
  2711       apply (metis U c path_component_trans path_component_def)
  2712       done
  2713   qed
  2714   show ?lhs
  2715     apply (clarsimp simp add: locally_path_connected_open_path_component)
  2716     apply (subst openin_subopen)
  2717     apply (blast intro: *)
  2718     done
  2719 qed
  2720 
  2721 lemma locally_path_connected_imp_locally_connected:
  2722   "locally path_connected S \<Longrightarrow> locally connected S"
  2723 using locally_mono path_connected_imp_connected by blast
  2724 
  2725 lemma locally_connected_components:
  2726   "\<lbrakk>locally connected S; c \<in> components S\<rbrakk> \<Longrightarrow> locally connected c"
  2727 by (meson locally_connected_open_component locally_open_subset openin_subtopology_self)
  2728 
  2729 lemma locally_path_connected_components:
  2730   "\<lbrakk>locally path_connected S; c \<in> components S\<rbrakk> \<Longrightarrow> locally path_connected c"
  2731 by (meson locally_connected_open_component locally_open_subset locally_path_connected_imp_locally_connected openin_subtopology_self)
  2732 
  2733 lemma locally_path_connected_connected_component:
  2734   "locally path_connected S \<Longrightarrow> locally path_connected (connected_component_set S x)"
  2735 by (metis components_iff connected_component_eq_empty locally_empty locally_path_connected_components)
  2736 
  2737 lemma open_imp_locally_path_connected:
  2738   fixes S :: "'a :: real_normed_vector set"
  2739   shows "open S \<Longrightarrow> locally path_connected S"
  2740 apply (rule locally_mono [of convex])
  2741 apply (simp_all add: locally_def openin_open_eq convex_imp_path_connected)
  2742 apply (meson open_ball centre_in_ball convex_ball openE order_trans)
  2743 done
  2744 
  2745 lemma open_imp_locally_connected:
  2746   fixes S :: "'a :: real_normed_vector set"
  2747   shows "open S \<Longrightarrow> locally connected S"
  2748 by (simp add: locally_path_connected_imp_locally_connected open_imp_locally_path_connected)
  2749 
  2750 lemma locally_path_connected_UNIV: "locally path_connected (UNIV::'a :: real_normed_vector set)"
  2751   by (simp add: open_imp_locally_path_connected)
  2752 
  2753 lemma locally_connected_UNIV: "locally connected (UNIV::'a :: real_normed_vector set)"
  2754   by (simp add: open_imp_locally_connected)
  2755 
  2756 lemma openin_connected_component_locally_connected:
  2757     "locally connected S
  2758      \<Longrightarrow> openin (top_of_set S) (connected_component_set S x)"
  2759 apply (simp add: locally_connected_open_connected_component)
  2760 by (metis connected_component_eq_empty connected_component_subset open_empty open_subset openin_subtopology_self)
  2761 
  2762 lemma openin_components_locally_connected:
  2763     "\<lbrakk>locally connected S; c \<in> components S\<rbrakk> \<Longrightarrow> openin (top_of_set S) c"
  2764   using locally_connected_open_component openin_subtopology_self by blast
  2765 
  2766 lemma openin_path_component_locally_path_connected:
  2767   "locally path_connected S
  2768         \<Longrightarrow> openin (top_of_set S) (path_component_set S x)"
  2769 by (metis (no_types) empty_iff locally_path_connected_2 openin_subopen openin_subtopology_self path_component_eq_empty)
  2770 
  2771 lemma closedin_path_component_locally_path_connected:
  2772     "locally path_connected S
  2773         \<Longrightarrow> closedin (top_of_set S) (path_component_set S x)"
  2774 apply  (simp add: closedin_def path_component_subset complement_path_component_Union)
  2775 apply (rule openin_Union)
  2776 using openin_path_component_locally_path_connected by auto
  2777 
  2778 lemma convex_imp_locally_path_connected:
  2779   fixes S :: "'a:: real_normed_vector set"
  2780   shows "convex S \<Longrightarrow> locally path_connected S"
  2781 apply (clarsimp simp add: locally_path_connected)
  2782 apply (subst (asm) openin_open)
  2783 apply clarify
  2784 apply (erule (1) openE)
  2785 apply (rule_tac x = "S \<inter> ball x e" in exI)
  2786 apply (force simp: convex_Int convex_imp_path_connected)
  2787 done
  2788 
  2789 lemma convex_imp_locally_connected:
  2790   fixes S :: "'a:: real_normed_vector set"
  2791   shows "convex S \<Longrightarrow> locally connected S"
  2792   by (simp add: locally_path_connected_imp_locally_connected convex_imp_locally_path_connected)
  2793 
  2794 
  2795 subsection\<open>Relations between components and path components\<close>
  2796 
  2797 lemma path_component_eq_connected_component:
  2798   assumes "locally path_connected S"
  2799     shows "(path_component S x = connected_component S x)"
  2800 proof (cases "x \<in> S")
  2801   case True
  2802   have "openin (top_of_set (connected_component_set S x)) (path_component_set S x)"
  2803     apply (rule openin_subset_trans [of S])
  2804     apply (intro conjI openin_path_component_locally_path_connected [OF assms])
  2805     using path_component_subset_connected_component   apply (auto simp: connected_component_subset)
  2806     done
  2807   moreover have "closedin (top_of_set (connected_component_set S x)) (path_component_set S x)"
  2808     apply (rule closedin_subset_trans [of S])
  2809     apply (intro conjI closedin_path_component_locally_path_connected [OF assms])
  2810     using path_component_subset_connected_component   apply (auto simp: connected_component_subset)
  2811     done
  2812   ultimately have *: "path_component_set S x = connected_component_set S x"
  2813     by (metis connected_connected_component connected_clopen True path_component_eq_empty)
  2814   then show ?thesis
  2815     by blast
  2816 next
  2817   case False then show ?thesis
  2818     by (metis Collect_empty_eq_bot connected_component_eq_empty path_component_eq_empty)
  2819 qed
  2820 
  2821 lemma path_component_eq_connected_component_set:
  2822      "locally path_connected S \<Longrightarrow> (path_component_set S x = connected_component_set S x)"
  2823 by (simp add: path_component_eq_connected_component)
  2824 
  2825 lemma locally_path_connected_path_component:
  2826      "locally path_connected S \<Longrightarrow> locally path_connected (path_component_set S x)"
  2827 using locally_path_connected_connected_component path_component_eq_connected_component by fastforce
  2828 
  2829 lemma open_path_connected_component:
  2830   fixes S :: "'a :: real_normed_vector set"
  2831   shows "open S \<Longrightarrow> path_component S x = connected_component S x"
  2832 by (simp add: path_component_eq_connected_component open_imp_locally_path_connected)
  2833 
  2834 lemma open_path_connected_component_set:
  2835   fixes S :: "'a :: real_normed_vector set"
  2836   shows "open S \<Longrightarrow> path_component_set S x = connected_component_set S x"
  2837 by (simp add: open_path_connected_component)
  2838 
  2839 proposition locally_connected_quotient_image:
  2840   assumes lcS: "locally connected S"
  2841       and oo: "\<And>T. T \<subseteq> f ` S
  2842                 \<Longrightarrow> openin (top_of_set S) (S \<inter> f -` T) \<longleftrightarrow>
  2843                     openin (top_of_set (f ` S)) T"
  2844     shows "locally connected (f ` S)"
  2845 proof (clarsimp simp: locally_connected_open_component)
  2846   fix U C
  2847   assume opefSU: "openin (top_of_set (f ` S)) U" and "C \<in> components U"
  2848   then have "C \<subseteq> U" "U \<subseteq> f ` S"
  2849     by (meson in_components_subset openin_imp_subset)+
  2850   then have "openin (top_of_set (f ` S)) C \<longleftrightarrow>
  2851              openin (top_of_set S) (S \<inter> f -` C)"
  2852     by (auto simp: oo)
  2853   moreover have "openin (top_of_set S) (S \<inter> f -` C)"
  2854   proof (subst openin_subopen, clarify)
  2855     fix x
  2856     assume "x \<in> S" "f x \<in> C"
  2857     show "\<exists>T. openin (top_of_set S) T \<and> x \<in> T \<and> T \<subseteq> (S \<inter> f -` C)"
  2858     proof (intro conjI exI)
  2859       show "openin (top_of_set S) (connected_component_set (S \<inter> f -` U) x)"
  2860       proof (rule ccontr)
  2861         assume **: "\<not> openin (top_of_set S) (connected_component_set (S \<inter> f -` U) x)"
  2862         then have "x \<notin> (S \<inter> f -` U)"
  2863           using \<open>U \<subseteq> f ` S\<close> opefSU lcS locally_connected_2 oo by blast
  2864         with ** show False
  2865           by (metis (no_types) connected_component_eq_empty empty_iff openin_subopen)
  2866       qed
  2867     next
  2868       show "x \<in> connected_component_set (S \<inter> f -` U) x"
  2869         using \<open>C \<subseteq> U\<close> \<open>f x \<in> C\<close> \<open>x \<in> S\<close> by auto
  2870     next
  2871       have contf: "continuous_on S f"
  2872         by (simp add: continuous_on_open oo openin_imp_subset)
  2873       then have "continuous_on (connected_component_set (S \<inter> f -` U) x) f"
  2874         apply (rule continuous_on_subset)
  2875         using connected_component_subset apply blast
  2876         done
  2877       then have "connected (f ` connected_component_set (S \<inter> f -` U) x)"
  2878         by (rule connected_continuous_image [OF _ connected_connected_component])
  2879       moreover have "f ` connected_component_set (S \<inter> f -` U) x \<subseteq> U"
  2880         using connected_component_in by blast
  2881       moreover have "C \<inter> f ` connected_component_set (S \<inter> f -` U) x \<noteq> {}"
  2882         using \<open>C \<subseteq> U\<close> \<open>f x \<in> C\<close> \<open>x \<in> S\<close> by fastforce
  2883       ultimately have fC: "f ` (connected_component_set (S \<inter> f -` U) x) \<subseteq> C"
  2884         by (rule components_maximal [OF \<open>C \<in> components U\<close>])
  2885       have cUC: "connected_component_set (S \<inter> f -` U) x \<subseteq> (S \<inter> f -` C)"
  2886         using connected_component_subset fC by blast
  2887       have "connected_component_set (S \<inter> f -` U) x \<subseteq> connected_component_set (S \<inter> f -` C) x"
  2888       proof -
  2889         { assume "x \<in> connected_component_set (S \<inter> f -` U) x"
  2890           then have ?thesis
  2891             using cUC connected_component_idemp connected_component_mono by blast }
  2892         then show ?thesis
  2893           using connected_component_eq_empty by auto
  2894       qed
  2895       also have "\<dots> \<subseteq> (S \<inter> f -` C)"
  2896         by (rule connected_component_subset)
  2897       finally show "connected_component_set (S \<inter> f -` U) x \<subseteq> (S \<inter> f -` C)" .
  2898     qed
  2899   qed
  2900   ultimately show "openin (top_of_set (f ` S)) C"
  2901     by metis
  2902 qed
  2903 
  2904 text\<open>The proof resembles that above but is not identical!\<close>
  2905 proposition locally_path_connected_quotient_image:
  2906   assumes lcS: "locally path_connected S"
  2907       and oo: "\<And>T. T \<subseteq> f ` S
  2908                 \<Longrightarrow> openin (top_of_set S) (S \<inter> f -` T) \<longleftrightarrow> openin (top_of_set (f ` S)) T"
  2909     shows "locally path_connected (f ` S)"
  2910 proof (clarsimp simp: locally_path_connected_open_path_component)
  2911   fix U y
  2912   assume opefSU: "openin (top_of_set (f ` S)) U" and "y \<in> U"
  2913   then have "path_component_set U y \<subseteq> U" "U \<subseteq> f ` S"
  2914     by (meson path_component_subset openin_imp_subset)+
  2915   then have "openin (top_of_set (f ` S)) (path_component_set U y) \<longleftrightarrow>
  2916              openin (top_of_set S) (S \<inter> f -` path_component_set U y)"
  2917   proof -
  2918     have "path_component_set U y \<subseteq> f ` S"
  2919       using \<open>U \<subseteq> f ` S\<close> \<open>path_component_set U y \<subseteq> U\<close> by blast
  2920     then show ?thesis
  2921       using oo by blast
  2922   qed
  2923   moreover have "openin (top_of_set S) (S \<inter> f -` path_component_set U y)"
  2924   proof (subst openin_subopen, clarify)
  2925     fix x
  2926     assume "x \<in> S" and Uyfx: "path_component U y (f x)"
  2927     then have "f x \<in> U"
  2928       using path_component_mem by blast
  2929     show "\<exists>T. openin (top_of_set S) T \<and> x \<in> T \<and> T \<subseteq> (S \<inter> f -` path_component_set U y)"
  2930     proof (intro conjI exI)
  2931       show "openin (top_of_set S) (path_component_set (S \<inter> f -` U) x)"
  2932       proof (rule ccontr)
  2933         assume **: "\<not> openin (top_of_set S) (path_component_set (S \<inter> f -` U) x)"
  2934         then have "x \<notin> (S \<inter> f -` U)"
  2935           by (metis (no_types, lifting) \<open>U \<subseteq> f ` S\<close> opefSU lcS oo locally_path_connected_open_path_component)
  2936         then show False
  2937           using ** \<open>path_component_set U y \<subseteq> U\<close>  \<open>x \<in> S\<close> \<open>path_component U y (f x)\<close> by blast
  2938       qed
  2939     next
  2940       show "x \<in> path_component_set (S \<inter> f -` U) x"
  2941         by (simp add: \<open>f x \<in> U\<close> \<open>x \<in> S\<close> path_component_refl)
  2942     next
  2943       have contf: "continuous_on S f"
  2944         by (simp add: continuous_on_open oo openin_imp_subset)
  2945       then have "continuous_on (path_component_set (S \<inter> f -` U) x) f"
  2946         apply (rule continuous_on_subset)
  2947         using path_component_subset apply blast
  2948         done
  2949       then have "path_connected (f ` path_component_set (S \<inter> f -` U) x)"
  2950         by (simp add: path_connected_continuous_image)
  2951       moreover have "f ` path_component_set (S \<inter> f -` U) x \<subseteq> U"
  2952         using path_component_mem by fastforce
  2953       moreover have "f x \<in> f ` path_component_set (S \<inter> f -` U) x"
  2954         by (force simp: \<open>x \<in> S\<close> \<open>f x \<in> U\<close> path_component_refl_eq)
  2955       ultimately have "f ` (path_component_set (S \<inter> f -` U) x) \<subseteq> path_component_set U (f x)"
  2956         by (meson path_component_maximal)
  2957        also have  "\<dots> \<subseteq> path_component_set U y"
  2958         by (simp add: Uyfx path_component_maximal path_component_subset path_component_sym)
  2959       finally have fC: "f ` (path_component_set (S \<inter> f -` U) x) \<subseteq> path_component_set U y" .
  2960       have cUC: "path_component_set (S \<inter> f -` U) x \<subseteq> (S \<inter> f -` path_component_set U y)"
  2961         using path_component_subset fC by blast
  2962       have "path_component_set (S \<inter> f -` U) x \<subseteq> path_component_set (S \<inter> f -` path_component_set U y) x"
  2963       proof -
  2964         have "\<And>a. path_component_set (path_component_set (S \<inter> f -` U) x) a \<subseteq> path_component_set (S \<inter> f -` path_component_set U y) a"
  2965           using cUC path_component_mono by blast
  2966         then show ?thesis
  2967           using path_component_path_component by blast
  2968       qed
  2969       also have "\<dots> \<subseteq> (S \<inter> f -` path_component_set U y)"
  2970         by (rule path_component_subset)
  2971       finally show "path_component_set (S \<inter> f -` U) x \<subseteq> (S \<inter> f -` path_component_set U y)" .
  2972     qed
  2973   qed
  2974   ultimately show "openin (top_of_set (f ` S)) (path_component_set U y)"
  2975     by metis
  2976 qed
  2977 
  2978 subsection%unimportant\<open>Components, continuity, openin, closedin\<close>
  2979 
  2980 lemma continuous_on_components_gen:
  2981  fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
  2982   assumes "\<And>c. c \<in> components S \<Longrightarrow>
  2983               openin (top_of_set S) c \<and> continuous_on c f"
  2984     shows "continuous_on S f"
  2985 proof (clarsimp simp: continuous_openin_preimage_eq)
  2986   fix t :: "'b set"
  2987   assume "open t"
  2988   have *: "S \<inter> f -` t = (\<Union>c \<in> components S. c \<inter> f -` t)"
  2989     by auto
  2990   show "openin (top_of_set S) (S \<inter> f -` t)"
  2991     unfolding * using \<open>open t\<close> assms continuous_openin_preimage_gen openin_trans openin_Union by blast
  2992 qed
  2993 
  2994 lemma continuous_on_components:
  2995  fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
  2996   assumes "locally connected S "
  2997           "\<And>c. c \<in> components S \<Longrightarrow> continuous_on c f"
  2998     shows "continuous_on S f"
  2999 apply (rule continuous_on_components_gen)
  3000 apply (auto simp: assms intro: openin_components_locally_connected)
  3001 done
  3002 
  3003 lemma continuous_on_components_eq:
  3004     "locally connected S
  3005      \<Longrightarrow> (continuous_on S f \<longleftrightarrow> (\<forall>c \<in> components S. continuous_on c f))"
  3006 by (meson continuous_on_components continuous_on_subset in_components_subset)
  3007 
  3008 lemma continuous_on_components_open:
  3009  fixes S :: "'a::real_normed_vector set"
  3010   assumes "open S "
  3011           "\<And>c. c \<in> components S \<Longrightarrow> continuous_on c f"
  3012     shows "continuous_on S f"
  3013 using continuous_on_components open_imp_locally_connected assms by blast
  3014 
  3015 lemma continuous_on_components_open_eq:
  3016   fixes S :: "'a::real_normed_vector set"
  3017   shows "open S \<Longrightarrow> (continuous_on S f \<longleftrightarrow> (\<forall>c \<in> components S. continuous_on c f))"
  3018 using continuous_on_subset in_components_subset
  3019 by (blast intro: continuous_on_components_open)
  3020 
  3021 lemma closedin_union_complement_components:
  3022   assumes u: "locally connected u"
  3023       and S: "closedin (top_of_set u) S"
  3024       and cuS: "c \<subseteq> components(u - S)"
  3025     shows "closedin (top_of_set u) (S \<union> \<Union>c)"
  3026 proof -
  3027   have di: "(\<And>S t. S \<in> c \<and> t \<in> c' \<Longrightarrow> disjnt S t) \<Longrightarrow> disjnt (\<Union> c) (\<Union> c')" for c'
  3028     by (simp add: disjnt_def) blast
  3029   have "S \<subseteq> u"
  3030     using S closedin_imp_subset by blast
  3031   moreover have "u - S = \<Union>c \<union> \<Union>(components (u - S) - c)"
  3032     by (metis Diff_partition Union_components Union_Un_distrib assms(3))
  3033   moreover have "disjnt (\<Union>c) (\<Union>(components (u - S) - c))"
  3034     apply (rule di)
  3035     by (metis DiffD1 DiffD2 assms(3) components_nonoverlap disjnt_def subsetCE)
  3036   ultimately have eq: "S \<union> \<Union>c = u - (\<Union>(components(u - S) - c))"
  3037     by (auto simp: disjnt_def)
  3038   have *: "openin (top_of_set u) (\<Union>(components (u - S) - c))"
  3039     apply (rule openin_Union)
  3040     apply (rule openin_trans [of "u - S"])
  3041     apply (simp add: u S locally_diff_closed openin_components_locally_connected)
  3042     apply (simp add: openin_diff S)
  3043     done
  3044   have "openin (top_of_set u) (u - (u - \<Union>(components (u - S) - c)))"
  3045     apply (rule openin_diff, simp)
  3046     apply (metis closedin_diff closedin_topspace topspace_euclidean_subtopology *)
  3047     done
  3048   then show ?thesis
  3049     by (force simp: eq closedin_def)
  3050 qed
  3051 
  3052 lemma closed_union_complement_components:
  3053   fixes S :: "'a::real_normed_vector set"
  3054   assumes S: "closed S" and c: "c \<subseteq> components(- S)"
  3055     shows "closed(S \<union> \<Union> c)"
  3056 proof -
  3057   have "closedin (top_of_set UNIV) (S \<union> \<Union>c)"
  3058     apply (rule closedin_union_complement_components [OF locally_connected_UNIV])
  3059     using S c apply (simp_all add: Compl_eq_Diff_UNIV)
  3060     done
  3061   then show ?thesis by simp
  3062 qed
  3063 
  3064 lemma closedin_Un_complement_component:
  3065   fixes S :: "'a::real_normed_vector set"
  3066   assumes u: "locally connected u"
  3067       and S: "closedin (top_of_set u) S"
  3068       and c: " c \<in> components(u - S)"
  3069     shows "closedin (top_of_set u) (S \<union> c)"
  3070 proof -
  3071   have "closedin (top_of_set u) (S \<union> \<Union>{c})"
  3072     using c by (blast intro: closedin_union_complement_components [OF u S])
  3073   then show ?thesis
  3074     by simp
  3075 qed
  3076 
  3077 lemma closed_Un_complement_component:
  3078   fixes S :: "'a::real_normed_vector set"
  3079   assumes S: "closed S" and c: " c \<in> components(-S)"
  3080     shows "closed (S \<union> c)"
  3081   by (metis Compl_eq_Diff_UNIV S c closed_closedin closedin_Un_complement_component
  3082       locally_connected_UNIV subtopology_UNIV)
  3083 
  3084 
  3085 subsection\<open>Existence of isometry between subspaces of same dimension\<close>
  3086 
  3087 lemma isometry_subset_subspace:
  3088   fixes S :: "'a::euclidean_space set"
  3089     and T :: "'b::euclidean_space set"
  3090   assumes S: "subspace S"
  3091       and T: "subspace T"
  3092       and d: "dim S \<le> dim T"
  3093   obtains f where "linear f" "f ` S \<subseteq> T" "\<And>x. x \<in> S \<Longrightarrow> norm(f x) = norm x"
  3094 proof -
  3095   obtain B where "B \<subseteq> S" and Borth: "pairwise orthogonal B"
  3096              and B1: "\<And>x. x \<in> B \<Longrightarrow> norm x = 1"
  3097              and "independent B" "finite B" "card B = dim S" "span B = S"
  3098     by (metis orthonormal_basis_subspace [OF S] independent_finite)
  3099   obtain C where "C \<subseteq> T" and Corth: "pairwise orthogonal C"
  3100              and C1:"\<And>x. x \<in> C \<Longrightarrow> norm x = 1"
  3101              and "independent C" "finite C" "card C = dim T" "span C = T"
  3102     by (metis orthonormal_basis_subspace [OF T] independent_finite)
  3103   obtain fb where "fb ` B \<subseteq> C" "inj_on fb B"
  3104     by (metis \<open>card B = dim S\<close> \<open>card C = dim T\<close> \<open>finite B\<close> \<open>finite C\<close> card_le_inj d)
  3105   then have pairwise_orth_fb: "pairwise (\<lambda>v j. orthogonal (fb v) (fb j)) B"
  3106     using Corth
  3107     apply (auto simp: pairwise_def orthogonal_clauses)
  3108     by (meson subsetD image_eqI inj_on_def)
  3109   obtain f where "linear f" and ffb: "\<And>x. x \<in> B \<Longrightarrow> f x = fb x"
  3110     using linear_independent_extend \<open>independent B\<close> by fastforce
  3111   have "span (f ` B) \<subseteq> span C"
  3112     by (metis \<open>fb ` B \<subseteq> C\<close> ffb image_cong span_mono)
  3113   then have "f ` S \<subseteq> T"
  3114     unfolding \<open>span B = S\<close> \<open>span C = T\<close> span_linear_image[OF \<open>linear f\<close>] .
  3115   have [simp]: "\<And>x. x \<in> B \<Longrightarrow> norm (fb x) = norm x"
  3116     using B1 C1 \<open>fb ` B \<subseteq> C\<close> by auto
  3117   have "norm (f x) = norm x" if "x \<in> S" for x
  3118   proof -
  3119     interpret linear f by fact
  3120     obtain a where x: "x = (\<Sum>v \<in> B. a v *\<^sub>R v)"
  3121       using \<open>finite B\<close> \<open>span B = S\<close> \<open>x \<in> S\<close> span_finite by fastforce
  3122     have "norm (f x)^2 = norm (\<Sum>v\<in>B. a v *\<^sub>R fb v)^2" by (simp add: sum scale ffb x)
  3123     also have "\<dots> = (\<Sum>v\<in>B. norm ((a v *\<^sub>R fb v))^2)"
  3124       apply (rule norm_sum_Pythagorean [OF \<open>finite B\<close>])
  3125       apply (rule pairwise_ortho_scaleR [OF pairwise_orth_fb])
  3126       done
  3127     also have "\<dots> = norm x ^2"
  3128       by (simp add: x pairwise_ortho_scaleR Borth norm_sum_Pythagorean [OF \<open>finite B\<close>])
  3129     finally show ?thesis
  3130       by (simp add: norm_eq_sqrt_inner)
  3131   qed
  3132   then show ?thesis
  3133     by (rule that [OF \<open>linear f\<close> \<open>f ` S \<subseteq> T\<close>])
  3134 qed
  3135 
  3136 proposition isometries_subspaces:
  3137   fixes S :: "'a::euclidean_space set"
  3138     and T :: "'b::euclidean_space set"
  3139   assumes S: "subspace S"
  3140       and T: "subspace T"
  3141       and d: "dim S = dim T"
  3142   obtains f g where "linear f" "linear g" "f ` S = T" "g ` T = S"
  3143                     "\<And>x. x \<in> S \<Longrightarrow> norm(f x) = norm x"
  3144                     "\<And>x. x \<in> T \<Longrightarrow> norm(g x) = norm x"
  3145                     "\<And>x. x \<in> S \<Longrightarrow> g(f x) = x"
  3146                     "\<And>x. x \<in> T \<Longrightarrow> f(g x) = x"
  3147 proof -
  3148   obtain B where "B \<subseteq> S" and Borth: "pairwise orthogonal B"
  3149              and B1: "\<And>x. x \<in> B \<Longrightarrow> norm x = 1"
  3150              and "independent B" "finite B" "card B = dim S" "span B = S"
  3151     by (metis orthonormal_basis_subspace [OF S] independent_finite)
  3152   obtain C where "C \<subseteq> T" and Corth: "pairwise orthogonal C"
  3153              and C1:"\<And>x. x \<in> C \<Longrightarrow> norm x = 1"
  3154              and "independent C" "finite C" "card C = dim T" "span C = T"
  3155     by (metis orthonormal_basis_subspace [OF T] independent_finite)
  3156   obtain fb where "bij_betw fb B C"
  3157     by (metis \<open>finite B\<close> \<open>finite C\<close> bij_betw_iff_card \<open>card B = dim S\<close> \<open>card C = dim T\<close> d)
  3158   then have pairwise_orth_fb: "pairwise (\<lambda>v j. orthogonal (fb v) (fb j)) B"
  3159     using Corth
  3160     apply (auto simp: pairwise_def orthogonal_clauses bij_betw_def)
  3161     by (meson subsetD image_eqI inj_on_def)
  3162   obtain f where "linear f" and ffb: "\<And>x. x \<in> B \<Longrightarrow> f x = fb x"
  3163     using linear_independent_extend \<open>independent B\<close> by fastforce
  3164   interpret f: linear f by fact
  3165   define gb where "gb \<equiv> inv_into B fb"
  3166   then have pairwise_orth_gb: "pairwise (\<lambda>v j. orthogonal (gb v) (gb j)) C"
  3167     using Borth
  3168     apply (auto simp: pairwise_def orthogonal_clauses bij_betw_def)
  3169     by (metis \<open>bij_betw fb B C\<close> bij_betw_imp_surj_on bij_betw_inv_into_right inv_into_into)
  3170   obtain g where "linear g" and ggb: "\<And>x. x \<in> C \<Longrightarrow> g x = gb x"
  3171     using linear_independent_extend \<open>independent C\<close> by fastforce
  3172   interpret g: linear g by fact
  3173   have "span (f ` B) \<subseteq> span C"
  3174     by (metis \<open>bij_betw fb B C\<close> bij_betw_imp_surj_on eq_iff ffb image_cong)
  3175   then have "f ` S \<subseteq> T"
  3176     unfolding \<open>span B = S\<close> \<open>span C = T\<close>
  3177       span_linear_image[OF \<open>linear f\<close>] .
  3178   have [simp]: "\<And>x. x \<in> B \<Longrightarrow> norm (fb x) = norm x"
  3179     using B1 C1 \<open>bij_betw fb B C\<close> bij_betw_imp_surj_on by fastforce
  3180   have f [simp]: "norm (f x) = norm x" "g (f x) = x" if "x \<in> S" for x
  3181   proof -
  3182     obtain a where x: "x = (\<Sum>v \<in> B. a v *\<^sub>R v)"
  3183       using \<open>finite B\<close> \<open>span B = S\<close> \<open>x \<in> S\<close> span_finite by fastforce
  3184     have "f x = (\<Sum>v \<in> B. f (a v *\<^sub>R v))"
  3185       using linear_sum [OF \<open>linear f\<close>] x by auto
  3186     also have "\<dots> = (\<Sum>v \<in> B. a v *\<^sub>R f v)"
  3187       by (simp add: f.sum f.scale)
  3188     also have "\<dots> = (\<Sum>v \<in> B. a v *\<^sub>R fb v)"
  3189       by (simp add: ffb cong: sum.cong)
  3190     finally have *: "f x = (\<Sum>v\<in>B. a v *\<^sub>R fb v)" .
  3191     then have "(norm (f x))\<^sup>2 = (norm (\<Sum>v\<in>B. a v *\<^sub>R fb v))\<^sup>2" by simp
  3192     also have "\<dots> = (\<Sum>v\<in>B. norm ((a v *\<^sub>R fb v))^2)"
  3193       apply (rule norm_sum_Pythagorean [OF \<open>finite B\<close>])
  3194       apply (rule pairwise_ortho_scaleR [OF pairwise_orth_fb])
  3195       done
  3196     also have "\<dots> = (norm x)\<^sup>2"
  3197       by (simp add: x pairwise_ortho_scaleR Borth norm_sum_Pythagorean [OF \<open>finite B\<close>])
  3198     finally show "norm (f x) = norm x"
  3199       by (simp add: norm_eq_sqrt_inner)
  3200     have "g (f x) = g (\<Sum>v\<in>B. a v *\<^sub>R fb v)" by (simp add: *)
  3201     also have "\<dots> = (\<Sum>v\<in>B. g (a v *\<^sub>R fb v))"
  3202       by (simp add: g.sum g.scale)
  3203     also have "\<dots> = (\<Sum>v\<in>B. a v *\<^sub>R g (fb v))"
  3204       by (simp add: g.scale)
  3205     also have "\<dots> = (\<Sum>v\<in>B. a v *\<^sub>R v)"
  3206       apply (rule sum.cong [OF refl])
  3207       using \<open>bij_betw fb B C\<close> gb_def bij_betwE bij_betw_inv_into_left gb_def ggb by fastforce
  3208     also have "\<dots> = x"
  3209       using x by blast
  3210     finally show "g (f x) = x" .
  3211   qed
  3212   have [simp]: "\<And>x. x \<in> C \<Longrightarrow> norm (gb x) = norm x"
  3213     by (metis B1 C1 \<open>bij_betw fb B C\<close> bij_betw_imp_surj_on gb_def inv_into_into)
  3214   have g [simp]: "f (g x) = x" if "x \<in> T" for x
  3215   proof -
  3216     obtain a where x: "x = (\<Sum>v \<in> C. a v *\<^sub>R v)"
  3217       using \<open>finite C\<close> \<open>span C = T\<close> \<open>x \<in> T\<close> span_finite by fastforce
  3218     have "g x = (\<Sum>v \<in> C. g (a v *\<^sub>R v))"
  3219       by (simp add: x g.sum)
  3220     also have "\<dots> = (\<Sum>v \<in> C. a v *\<^sub>R g v)"
  3221       by (simp add: g.scale)
  3222     also have "\<dots> = (\<Sum>v \<in> C. a v *\<^sub>R gb v)"
  3223       by (simp add: ggb cong: sum.cong)
  3224     finally have "f (g x) = f (\<Sum>v\<in>C. a v *\<^sub>R gb v)" by simp
  3225     also have "\<dots> = (\<Sum>v\<in>C. f (a v *\<^sub>R gb v))"
  3226       by (simp add: f.scale f.sum)
  3227     also have "\<dots> = (\<Sum>v\<in>C. a v *\<^sub>R f (gb v))"
  3228       by (simp add: f.scale f.sum)
  3229     also have "\<dots> = (\<Sum>v\<in>C. a v *\<^sub>R v)"
  3230       using \<open>bij_betw fb B C\<close>
  3231       by (simp add: bij_betw_def gb_def bij_betw_inv_into_right ffb inv_into_into)
  3232     also have "\<dots> = x"
  3233       using x by blast
  3234     finally show "f (g x) = x" .
  3235   qed
  3236   have gim: "g ` T = S"
  3237     by (metis (full_types) S T \<open>f ` S \<subseteq> T\<close> d dim_eq_span dim_image_le f(2) g.linear_axioms
  3238         image_iff linear_subspace_image span_eq_iff subset_iff)
  3239   have fim: "f ` S = T"
  3240     using \<open>g ` T = S\<close> image_iff by fastforce
  3241   have [simp]: "norm (g x) = norm x" if "x \<in> T" for x
  3242     using fim that by auto
  3243   show ?thesis
  3244     apply (rule that [OF \<open>linear f\<close> \<open>linear g\<close>])
  3245     apply (simp_all add: fim gim)
  3246     done
  3247 qed
  3248 
  3249 corollary isometry_subspaces:
  3250   fixes S :: "'a::euclidean_space set"
  3251     and T :: "'b::euclidean_space set"
  3252   assumes S: "subspace S"
  3253       and T: "subspace T"
  3254       and d: "dim S = dim T"
  3255   obtains f where "linear f" "f ` S = T" "\<And>x. x \<in> S \<Longrightarrow> norm(f x) = norm x"
  3256 using isometries_subspaces [OF assms]
  3257 by metis
  3258 
  3259 corollary isomorphisms_UNIV_UNIV:
  3260   assumes "DIM('M) = DIM('N)"
  3261   obtains f::"'M::euclidean_space \<Rightarrow>'N::euclidean_space" and g
  3262   where "linear f" "linear g"
  3263                     "\<And>x. norm(f x) = norm x" "\<And>y. norm(g y) = norm y"
  3264                     "\<And>x. g (f x) = x" "\<And>y. f(g y) = y"
  3265   using assms by (auto intro: isometries_subspaces [of "UNIV::'M set" "UNIV::'N set"])
  3266 
  3267 lemma homeomorphic_subspaces:
  3268   fixes S :: "'a::euclidean_space set"
  3269     and T :: "'b::euclidean_space set"
  3270   assumes S: "subspace S"
  3271       and T: "subspace T"
  3272       and d: "dim S = dim T"
  3273     shows "S homeomorphic T"
  3274 proof -
  3275   obtain f g where "linear f" "linear g" "f ` S = T" "g ` T = S"
  3276                    "\<And>x. x \<in> S \<Longrightarrow> g(f x) = x" "\<And>x. x \<in> T \<Longrightarrow> f(g x) = x"
  3277     by (blast intro: isometries_subspaces [OF assms])
  3278   then show ?thesis
  3279     apply (simp add: homeomorphic_def homeomorphism_def)
  3280     apply (rule_tac x=f in exI)
  3281     apply (rule_tac x=g in exI)
  3282     apply (auto simp: linear_continuous_on linear_conv_bounded_linear)
  3283     done
  3284 qed
  3285 
  3286 lemma homeomorphic_affine_sets:
  3287   assumes "affine S" "affine T" "aff_dim S = aff_dim T"
  3288     shows "S homeomorphic T"
  3289 proof (cases "S = {} \<or> T = {}")
  3290   case True  with assms aff_dim_empty homeomorphic_empty show ?thesis
  3291     by metis
  3292 next
  3293   case False
  3294   then obtain a b where ab: "a \<in> S" "b \<in> T" by auto
  3295   then have ss: "subspace ((+) (- a) ` S)" "subspace ((+) (- b) ` T)"
  3296     using affine_diffs_subspace assms by blast+
  3297   have dd: "dim ((+) (- a) ` S) = dim ((+) (- b) ` T)"
  3298     using assms ab  by (simp add: aff_dim_eq_dim  [OF hull_inc] image_def)
  3299   have "S homeomorphic ((+) (- a) ` S)"
  3300     by (fact homeomorphic_translation)
  3301   also have "\<dots> homeomorphic ((+) (- b) ` T)"
  3302     by (rule homeomorphic_subspaces [OF ss dd])
  3303   also have "\<dots> homeomorphic T"
  3304     using homeomorphic_translation [of T "- b"] by (simp add: homeomorphic_sym [of T])
  3305   finally show ?thesis .
  3306 qed
  3307 
  3308 
  3309 subsection\<open>Retracts, in a general sense, preserve (co)homotopic triviality)\<close>
  3310 
  3311 locale%important Retracts =
  3312   fixes s h t k
  3313   assumes conth: "continuous_on s h"
  3314       and imh: "h ` s = t"
  3315       and contk: "continuous_on t k"
  3316       and imk: "k ` t \<subseteq> s"
  3317       and idhk: "\<And>y. y \<in> t \<Longrightarrow> h(k y) = y"
  3318 
  3319 begin
  3320 
  3321 lemma homotopically_trivial_retraction_gen:
  3322   assumes P: "\<And>f. \<lbrakk>continuous_on u f; f ` u \<subseteq> t; Q f\<rbrakk> \<Longrightarrow> P(k \<circ> f)"
  3323       and Q: "\<And>f. \<lbrakk>continuous_on u f; f ` u \<subseteq> s; P f\<rbrakk> \<Longrightarrow> Q(h \<circ> f)"
  3324       and Qeq: "\<And>h k. (\<And>x. x \<in> u \<Longrightarrow> h x = k x) \<Longrightarrow> Q h = Q k"
  3325       and hom: "\<And>f g. \<lbrakk>continuous_on u f; f ` u \<subseteq> s; P f;
  3326                        continuous_on u g; g ` u \<subseteq> s; P g\<rbrakk>
  3327                        \<Longrightarrow> homotopic_with_canon P u s f g"
  3328       and contf: "continuous_on u f" and imf: "f ` u \<subseteq> t" and Qf: "Q f"
  3329       and contg: "continuous_on u g" and img: "g ` u \<subseteq> t" and Qg: "Q g"
  3330     shows "homotopic_with_canon Q u t f g"
  3331 proof -
  3332   have feq: "\<And>x. x \<in> u \<Longrightarrow> (h \<circ> (k \<circ> f)) x = f x" using idhk imf by auto
  3333   have geq: "\<And>x. x \<in> u \<Longrightarrow> (h \<circ> (k \<circ> g)) x = g x" using idhk img by auto
  3334   have "continuous_on u (k \<circ> f)"
  3335     using contf continuous_on_compose continuous_on_subset contk imf by blast
  3336   moreover have "(k \<circ> f) ` u \<subseteq> s"
  3337     using imf imk by fastforce
  3338   moreover have "P (k \<circ> f)"
  3339     by (simp add: P Qf contf imf)
  3340   moreover have "continuous_on u (k \<circ> g)"
  3341     using contg continuous_on_compose continuous_on_subset contk img by blast
  3342   moreover have "(k \<circ> g) ` u \<subseteq> s"
  3343     using img imk by fastforce
  3344   moreover have "P (k \<circ> g)"
  3345     by (simp add: P Qg contg img)
  3346   ultimately have "homotopic_with_canon P u s (k \<circ> f) (k \<circ> g)"
  3347     by (rule hom)
  3348   then have "homotopic_with_canon Q u t (h \<circ> (k \<circ> f)) (h \<circ> (k \<circ> g))"
  3349     apply (rule homotopic_with_compose_continuous_left [OF homotopic_with_mono])
  3350     using Q by (auto simp: conth imh)
  3351   then show ?thesis
  3352     apply (rule homotopic_with_eq)
  3353     using feq geq apply fastforce+
  3354     using Qeq topspace_euclidean_subtopology by blast
  3355 qed
  3356 
  3357 lemma homotopically_trivial_retraction_null_gen:
  3358   assumes P: "\<And>f. \<lbrakk>continuous_on u f; f ` u \<subseteq> t; Q f\<rbrakk> \<Longrightarrow> P(k \<circ> f)"
  3359       and Q: "\<And>f. \<lbrakk>continuous_on u f; f ` u \<subseteq> s; P f\<rbrakk> \<Longrightarrow> Q(h \<circ> f)"
  3360       and Qeq: "\<And>h k. (\<And>x. x \<in> u \<Longrightarrow> h x = k x) \<Longrightarrow> Q h = Q k"
  3361       and hom: "\<And>f. \<lbrakk>continuous_on u f; f ` u \<subseteq> s; P f\<rbrakk>
  3362                      \<Longrightarrow> \<exists>c. homotopic_with_canon P u s f (\<lambda>x. c)"
  3363       and contf: "continuous_on u f" and imf:"f ` u \<subseteq> t" and Qf: "Q f"
  3364   obtains c where "homotopic_with_canon Q u t f (\<lambda>x. c)"
  3365 proof -
  3366   have feq: "\<And>x. x \<in> u \<Longrightarrow> (h \<circ> (k \<circ> f)) x = f x" using idhk imf by auto
  3367   have "continuous_on u (k \<circ> f)"
  3368     using contf continuous_on_compose continuous_on_subset contk imf by blast
  3369   moreover have "(k \<circ> f) ` u \<subseteq> s"
  3370     using imf imk by fastforce
  3371   moreover have "P (k \<circ> f)"
  3372     by (simp add: P Qf contf imf)
  3373   ultimately obtain c where "homotopic_with_canon P u s (k \<circ> f) (\<lambda>x. c)"
  3374     by (metis hom)
  3375   then have "homotopic_with_canon Q u t (h \<circ> (k \<circ> f)) (h \<circ> (\<lambda>x. c))"
  3376     apply (rule homotopic_with_compose_continuous_left [OF homotopic_with_mono])
  3377     using Q by (auto simp: conth imh)
  3378   then show ?thesis
  3379     apply (rule_tac c = "h c" in that)
  3380     apply (erule homotopic_with_eq)
  3381     using feq apply auto[1]
  3382     apply simp
  3383     using Qeq topspace_euclidean_subtopology by blast
  3384 qed
  3385 
  3386 lemma cohomotopically_trivial_retraction_gen:
  3387   assumes P: "\<And>f. \<lbrakk>continuous_on t f; f ` t \<subseteq> u; Q f\<rbrakk> \<Longrightarrow> P(f \<circ> h)"
  3388       and Q: "\<And>f. \<lbrakk>continuous_on s f; f ` s \<subseteq> u; P f\<rbrakk> \<Longrightarrow> Q(f \<circ> k)"
  3389       and Qeq: "\<And>h k. (\<And>x. x \<in> t \<Longrightarrow> h x = k x) \<Longrightarrow> Q h = Q k"
  3390       and hom: "\<And>f g. \<lbrakk>continuous_on s f; f ` s \<subseteq> u; P f;
  3391                        continuous_on s g; g ` s \<subseteq> u; P g\<rbrakk>
  3392                        \<Longrightarrow> homotopic_with_canon P s u f g"
  3393       and contf: "continuous_on t f" and imf: "f ` t \<subseteq> u" and Qf: "Q f"
  3394       and contg: "continuous_on t g" and img: "g ` t \<subseteq> u" and Qg: "Q g"
  3395     shows "homotopic_with_canon Q t u f g"
  3396 proof -
  3397   have feq: "\<And>x. x \<in> t \<Longrightarrow> (f \<circ> h \<circ> k) x = f x" using idhk imf by auto
  3398   have geq: "\<And>x. x \<in> t \<Longrightarrow> (g \<circ> h \<circ> k) x = g x" using idhk img by auto
  3399   have "continuous_on s (f \<circ> h)"
  3400     using contf conth continuous_on_compose imh by blast
  3401   moreover have "(f \<circ> h) ` s \<subseteq> u"
  3402     using imf imh by fastforce
  3403   moreover have "P (f \<circ> h)"
  3404     by (simp add: P Qf contf imf)
  3405   moreover have "continuous_on s (g \<circ> h)"
  3406     using contg continuous_on_compose continuous_on_subset conth imh by blast
  3407   moreover have "(g \<circ> h) ` s \<subseteq> u"
  3408     using img imh by fastforce
  3409   moreover have "P (g \<circ> h)"
  3410     by (simp add: P Qg contg img)
  3411   ultimately have "homotopic_with_canon P s u (f \<circ> h) (g \<circ> h)"
  3412     by (rule hom)
  3413   then have "homotopic_with_canon Q t u (f \<circ> h \<circ> k) (g \<circ> h \<circ> k)"
  3414     apply (rule homotopic_with_compose_continuous_right [OF homotopic_with_mono])
  3415     using Q by (auto simp: contk imk)
  3416   then show ?thesis
  3417     apply (rule homotopic_with_eq)
  3418     using feq apply auto[1]
  3419     using geq apply auto[1]
  3420     using Qeq topspace_euclidean_subtopology by blast
  3421 qed
  3422 
  3423 lemma cohomotopically_trivial_retraction_null_gen:
  3424   assumes P: "\<And>f. \<lbrakk>continuous_on t f; f ` t \<subseteq> u; Q f\<rbrakk> \<Longrightarrow> P(f \<circ> h)"
  3425       and Q: "\<And>f. \<lbrakk>continuous_on s f; f ` s \<subseteq> u; P f\<rbrakk> \<Longrightarrow> Q(f \<circ> k)"
  3426       and Qeq: "\<And>h k. (\<And>x. x \<in> t \<Longrightarrow> h x = k x) \<Longrightarrow> Q h = Q k"
  3427       and hom: "\<And>f g. \<lbrakk>continuous_on s f; f ` s \<subseteq> u; P f\<rbrakk>
  3428                        \<Longrightarrow> \<exists>c. homotopic_with_canon P s u f (\<lambda>x. c)"
  3429       and contf: "continuous_on t f" and imf: "f ` t \<subseteq> u" and Qf: "Q f"
  3430   obtains c where "homotopic_with_canon Q t u f (\<lambda>x. c)"
  3431 proof -
  3432   have feq: "\<And>x. x \<in> t \<Longrightarrow> (f \<circ> h \<circ> k) x = f x" using idhk imf by auto
  3433   have "continuous_on s (f \<circ> h)"
  3434     using contf conth continuous_on_compose imh by blast
  3435   moreover have "(f \<circ> h) ` s \<subseteq> u"
  3436     using imf imh by fastforce
  3437   moreover have "P (f \<circ> h)"
  3438     by (simp add: P Qf contf imf)
  3439   ultimately obtain c where "homotopic_with_canon P s u (f \<circ> h) (\<lambda>x. c)"
  3440     by (metis hom)
  3441   then have "homotopic_with_canon Q t u (f \<circ> h \<circ> k) ((\<lambda>x. c) \<circ> k)"
  3442     apply (rule homotopic_with_compose_continuous_right [OF homotopic_with_mono])
  3443     using Q by (auto simp: contk imk)
  3444   then show ?thesis
  3445     apply (rule_tac c = c in that)
  3446     apply (erule homotopic_with_eq)
  3447     using feq apply auto[1]
  3448     apply simp
  3449     using Qeq topspace_euclidean_subtopology by blast
  3450 qed
  3451 
  3452 end
  3453 
  3454 lemma simply_connected_retraction_gen:
  3455   shows "\<lbrakk>simply_connected S; continuous_on S h; h ` S = T;
  3456           continuous_on T k; k ` T \<subseteq> S; \<And>y. y \<in> T \<Longrightarrow> h(k y) = y\<rbrakk>
  3457         \<Longrightarrow> simply_connected T"
  3458 apply (simp add: simply_connected_def path_def path_image_def homotopic_loops_def, clarify)
  3459 apply (rule Retracts.homotopically_trivial_retraction_gen
  3460         [of S h _ k _ "\<lambda>p. pathfinish p = pathstart p"  "\<lambda>p. pathfinish p = pathstart p"])
  3461 apply (simp_all add: Retracts_def pathfinish_def pathstart_def)
  3462 done
  3463 
  3464 lemma homeomorphic_simply_connected:
  3465     "\<lbrakk>S homeomorphic T; simply_connected S\<rbrakk> \<Longrightarrow> simply_connected T"
  3466   by (auto simp: homeomorphic_def homeomorphism_def intro: simply_connected_retraction_gen)
  3467 
  3468 lemma homeomorphic_simply_connected_eq:
  3469     "S homeomorphic T \<Longrightarrow> (simply_connected S \<longleftrightarrow> simply_connected T)"
  3470   by (metis homeomorphic_simply_connected homeomorphic_sym)
  3471 
  3472 
  3473 subsection\<open>Homotopy equivalence\<close>
  3474 
  3475 subsection\<open>Homotopy equivalence of topological spaces.\<close>
  3476 
  3477 definition%important homotopy_equivalent_space
  3478              (infix "homotopy'_equivalent'_space" 50)
  3479   where "X homotopy_equivalent_space Y \<equiv>
  3480         (\<exists>f g. continuous_map X Y f \<and>
  3481               continuous_map Y X g \<and>
  3482               homotopic_with (\<lambda>x. True) X X (g \<circ> f) id \<and>
  3483               homotopic_with (\<lambda>x. True) Y Y (f \<circ> g) id)"
  3484 
  3485 lemma homeomorphic_imp_homotopy_equivalent_space:
  3486   "X homeomorphic_space Y \<Longrightarrow> X homotopy_equivalent_space Y"
  3487   unfolding homeomorphic_space_def homotopy_equivalent_space_def
  3488   apply (erule ex_forward)+
  3489   by (simp add: homotopic_with_equal homotopic_with_sym homeomorphic_maps_def)
  3490 
  3491 lemma homotopy_equivalent_space_refl:
  3492    "X homotopy_equivalent_space X"
  3493   by (simp add: homeomorphic_imp_homotopy_equivalent_space homeomorphic_space_refl)
  3494 
  3495 lemma homotopy_equivalent_space_sym:
  3496    "X homotopy_equivalent_space Y \<longleftrightarrow> Y homotopy_equivalent_space X"
  3497   by (meson homotopy_equivalent_space_def)
  3498 
  3499 lemma homotopy_eqv_trans [trans]:
  3500   assumes 1: "X homotopy_equivalent_space Y" and 2: "Y homotopy_equivalent_space U"
  3501     shows "X homotopy_equivalent_space U"
  3502 proof -
  3503   obtain f1 g1 where f1: "continuous_map X Y f1"
  3504                  and g1: "continuous_map Y X g1"
  3505                  and hom1: "homotopic_with (\<lambda>x. True) X X (g1 \<circ> f1) id"
  3506                            "homotopic_with (\<lambda>x. True) Y Y (f1 \<circ> g1) id"
  3507     using 1 by (auto simp: homotopy_equivalent_space_def)
  3508   obtain f2 g2 where f2: "continuous_map Y U f2"
  3509                  and g2: "continuous_map U Y g2"
  3510                  and hom2: "homotopic_with (\<lambda>x. True) Y Y (g2 \<circ> f2) id"
  3511                            "homotopic_with (\<lambda>x. True) U U (f2 \<circ> g2) id"
  3512     using 2 by (auto simp: homotopy_equivalent_space_def)
  3513   have "homotopic_with (\<lambda>f. True) X Y (g2 \<circ> f2 \<circ> f1) (id \<circ> f1)"
  3514     using f1 hom2(1) homotopic_compose_continuous_map_right by blast
  3515   then have "homotopic_with (\<lambda>f. True) X Y (g2 \<circ> (f2 \<circ> f1)) (id \<circ> f1)"
  3516     by (simp add: o_assoc)
  3517   then have "homotopic_with (\<lambda>x. True) X X
  3518          (g1 \<circ> (g2 \<circ> (f2 \<circ> f1))) (g1 \<circ> (id \<circ> f1))"
  3519     by (simp add: g1 homotopic_compose_continuous_map_left)
  3520   moreover have "homotopic_with (\<lambda>x. True) X X (g1 \<circ> id \<circ> f1) id"
  3521     using hom1 by simp
  3522   ultimately have SS: "homotopic_with (\<lambda>x. True) X X (g1 \<circ> g2 \<circ> (f2 \<circ> f1)) id"
  3523     apply (simp add: o_assoc)
  3524     apply (blast intro: homotopic_with_trans)
  3525     done
  3526   have "homotopic_with (\<lambda>f. True) U Y (f1 \<circ> g1 \<circ> g2) (id \<circ> g2)"
  3527     using g2 hom1(2) homotopic_with_compose_continuous_map_right by fastforce
  3528   then have "homotopic_with (\<lambda>f. True) U Y (f1 \<circ> (g1 \<circ> g2)) (id \<circ> g2)"
  3529     by (simp add: o_assoc)
  3530   then have "homotopic_with (\<lambda>x. True) U U
  3531          (f2 \<circ> (f1 \<circ> (g1 \<circ> g2))) (f2 \<circ> (id \<circ> g2))"
  3532     by (simp add: f2 homotopic_with_compose_continuous_map_left)
  3533   moreover have "homotopic_with (\<lambda>x. True) U U (f2 \<circ> id \<circ> g2) id"
  3534     using hom2 by simp
  3535   ultimately have UU: "homotopic_with (\<lambda>x. True) U U (f2 \<circ> f1 \<circ> (g1 \<circ> g2)) id"
  3536     apply (simp add: o_assoc)
  3537     apply (blast intro: homotopic_with_trans)
  3538     done
  3539   show ?thesis
  3540     unfolding homotopy_equivalent_space_def
  3541     by (blast intro: f1 f2 g1 g2 continuous_map_compose SS UU)
  3542 qed
  3543 
  3544 lemma deformation_retraction_imp_homotopy_equivalent_space:
  3545   "\<lbrakk>homotopic_with (\<lambda>x. True) X X (s \<circ> r) id; retraction_maps X Y r s\<rbrakk>
  3546     \<Longrightarrow> X homotopy_equivalent_space Y"
  3547   unfolding homotopy_equivalent_space_def retraction_maps_def
  3548   apply (rule_tac x=r in exI)
  3549   apply (rule_tac x=s in exI)
  3550   apply (auto simp: homotopic_with_equal continuous_map_compose)
  3551   done
  3552 
  3553 lemma deformation_retract_imp_homotopy_equivalent_space:
  3554    "\<lbrakk>homotopic_with (\<lambda>x. True) X X r id; retraction_maps X Y r id\<rbrakk>
  3555     \<Longrightarrow> X homotopy_equivalent_space Y"
  3556   using deformation_retraction_imp_homotopy_equivalent_space by force
  3557 
  3558 lemma deformation_retract_of_space:
  3559   "S \<subseteq> topspace X \<and>
  3560    (\<exists>r. homotopic_with (\<lambda>x. True) X X id r \<and> retraction_maps X (subtopology X S) r id) \<longleftrightarrow>
  3561    S retract_of_space X \<and> (\<exists>f. homotopic_with (\<lambda>x. True) X X id f \<and> f ` (topspace X) \<subseteq> S)"
  3562 proof (cases "S \<subseteq> topspace X")
  3563   case True
  3564   moreover have "(\<exists>r. homotopic_with (\<lambda>x. True) X X id r \<and> retraction_maps X (subtopology X S) r id)
  3565              \<longleftrightarrow> (S retract_of_space X \<and> (\<exists>f. homotopic_with (\<lambda>x. True) X X id f \<and> f ` topspace X \<subseteq> S))"
  3566     unfolding retract_of_space_def
  3567   proof safe
  3568     fix f r
  3569     assume f: "homotopic_with (\<lambda>x. True) X X id f"
  3570       and fS: "f ` topspace X \<subseteq> S"
  3571       and r: "continuous_map X (subtopology X S) r"
  3572       and req: "\<forall>x\<in>S. r x = x"
  3573     show "\<exists>r. homotopic_with (\<lambda>x. True) X X id r \<and> retraction_maps X (subtopology X S) r id"
  3574     proof (intro exI conjI)
  3575       have "homotopic_with (\<lambda>x. True) X X f r"
  3576         proof (rule homotopic_with_eq)
  3577           show "homotopic_with (\<lambda>x. True) X X (r \<circ> f) (r \<circ> id)"
  3578             using homotopic_with_symD continuous_map_into_fulltopology f homotopic_compose_continuous_map_left r by blast
  3579           show "f x = (r \<circ> f) x" if "x \<in> topspace X" for x
  3580             using that fS req by auto
  3581         qed auto
  3582       then show "homotopic_with (\<lambda>x. True) X X id r"
  3583         by (rule homotopic_with_trans [OF f])
  3584     next
  3585       show "retraction_maps X (subtopology X S) r id"
  3586         by (simp add: r req retraction_maps_def topspace_subtopology)
  3587     qed
  3588   qed (use True in \<open>auto simp: retraction_maps_def topspace_subtopology_subset continuous_map_in_subtopology\<close>)
  3589   ultimately show ?thesis by simp
  3590 qed (auto simp: retract_of_space_def retraction_maps_def)
  3591 
  3592 
  3593 
  3594 subsection\<open>Contractible spaces\<close>
  3595 
  3596 text\<open>The definition (which agrees with "contractible" on subsets of Euclidean space)
  3597 is a little cryptic because we don't in fact assume that the constant "a" is in the space.
  3598 This forces the convention that the empty space / set is contractible, avoiding some special cases. \<close>
  3599 
  3600 definition contractible_space where
  3601   "contractible_space X \<equiv> \<exists>a. homotopic_with (\<lambda>x. True) X X id (\<lambda>x. a)"
  3602 
  3603 lemma contractible_space_top_of_set [simp]:"contractible_space (top_of_set S) \<longleftrightarrow> contractible S"
  3604   by (auto simp: contractible_space_def contractible_def)
  3605 
  3606 lemma contractible_space_empty:
  3607    "topspace X = {} \<Longrightarrow> contractible_space X"
  3608   apply (simp add: contractible_space_def homotopic_with_def)
  3609   apply (rule_tac x=undefined in exI)
  3610   apply (rule_tac x="\<lambda>(t,x). if t = 0 then x else undefined" in exI)
  3611   apply (auto simp: continuous_map_on_empty)
  3612   done
  3613 
  3614 lemma contractible_space_singleton:
  3615   "topspace X = {a} \<Longrightarrow> contractible_space X"
  3616   apply (simp add: contractible_space_def homotopic_with_def)
  3617   apply (rule_tac x=a in exI)
  3618   apply (rule_tac x="\<lambda>(t,x). if t = 0 then x else a" in exI)
  3619   apply (auto intro: continuous_map_eq [where f = "\<lambda>z. a"])
  3620   done
  3621 
  3622 lemma contractible_space_subset_singleton:
  3623    "topspace X \<subseteq> {a} \<Longrightarrow> contractible_space X"
  3624   by (meson contractible_space_empty contractible_space_singleton subset_singletonD)
  3625 
  3626 lemma contractible_space_subtopology_singleton:
  3627    "contractible_space(subtopology X {a})"
  3628   by (meson contractible_space_subset_singleton insert_subset path_connectedin_singleton path_connectedin_subtopology subsetI)
  3629 
  3630 lemma contractible_space:
  3631    "contractible_space X \<longleftrightarrow>
  3632         topspace X = {} \<or>
  3633         (\<exists>a \<in> topspace X. homotopic_with (\<lambda>x. True) X X id (\<lambda>x. a))"
  3634 proof (cases "topspace X = {}")
  3635   case False
  3636   then show ?thesis
  3637     apply (auto simp: contractible_space_def)
  3638     using homotopic_with_imp_continuous_maps by fastforce
  3639 qed (simp add: contractible_space_empty)
  3640 
  3641 lemma contractible_imp_path_connected_space:
  3642   assumes "contractible_space X" shows "path_connected_space X"
  3643 proof (cases "topspace X = {}")
  3644   case False
  3645   have *: "path_connected_space X"
  3646     if "a \<in> topspace X" and conth: "continuous_map (prod_topology (top_of_set {0..1}) X) X h"
  3647       and h: "\<forall>x. h (0, x) = x" "\<forall>x. h (1, x) = a"
  3648     for a and h :: "real \<times> 'a \<Rightarrow> 'a"
  3649   proof -
  3650     have "path_component_of X b a" if "b \<in> topspace X" for b
  3651       using that unfolding path_component_of_def
  3652       apply (rule_tac x="h \<circ> (\<lambda>x. (x,b))" in exI)
  3653       apply (simp add: h pathin_def)
  3654       apply (rule continuous_map_compose [OF _ conth])
  3655       apply (auto simp: continuous_map_pairwise intro!: continuous_intros continuous_map_compose continuous_map_id [unfolded id_def])
  3656       done
  3657   then show ?thesis
  3658     by (metis path_component_of_equiv path_connected_space_iff_path_component)
  3659   qed
  3660   show ?thesis
  3661     using assms False by (auto simp: contractible_space homotopic_with_def *)
  3662 qed (simp add: path_connected_space_topspace_empty)
  3663 
  3664 lemma contractible_imp_connected_space:
  3665    "contractible_space X \<Longrightarrow> connected_space X"
  3666   by (simp add: contractible_imp_path_connected_space path_connected_imp_connected_space)
  3667 
  3668 lemma contractible_space_alt:
  3669    "contractible_space X \<longleftrightarrow> (\<forall>a \<in> topspace X. homotopic_with (\<lambda>x. True) X X id (\<lambda>x. a))" (is "?lhs = ?rhs")
  3670 proof
  3671   assume X: ?lhs
  3672   then obtain a where a: "homotopic_with (\<lambda>x. True) X X id (\<lambda>x. a)"
  3673     by (auto simp: contractible_space_def)
  3674   show ?rhs
  3675   proof
  3676     show "homotopic_with (\<lambda>x. True) X X id (\<lambda>x. b)" if "b \<in> topspace X" for b
  3677       apply (rule homotopic_with_trans [OF a])
  3678       using homotopic_constant_maps path_connected_space_imp_path_component_of
  3679       by (metis (full_types) X a continuous_map_const contractible_imp_path_connected_space homotopic_with_imp_continuous_maps that)
  3680   qed
  3681 next
  3682   assume R: ?rhs
  3683   then show ?lhs
  3684     apply (simp add: contractible_space_def)
  3685     by (metis equals0I homotopic_on_emptyI)
  3686 qed
  3687 
  3688 
  3689 lemma compose_const [simp]: "f \<circ> (\<lambda>x. a) = (\<lambda>x. f a)" "(\<lambda>x. a) \<circ> g = (\<lambda>x. a)"
  3690   by (simp_all add: o_def)
  3691 
  3692 lemma nullhomotopic_through_contractible_space:
  3693   assumes f: "continuous_map X Y f" and g: "continuous_map Y Z g" and Y: "contractible_space Y"
  3694   obtains c where "homotopic_with (\<lambda>h. True) X Z (g \<circ> f) (\<lambda>x. c)"
  3695 proof -
  3696   obtain b where b: "homotopic_with (\<lambda>x. True) Y Y id (\<lambda>x. b)"
  3697     using Y by (auto simp: contractible_space_def)
  3698   show thesis
  3699     using homotopic_compose_continuous_map_right
  3700            [OF homotopic_compose_continuous_map_left [OF b g] f]
  3701     by (simp add: that)
  3702 qed
  3703 
  3704 lemma nullhomotopic_into_contractible_space:
  3705   assumes f: "continuous_map X Y f" and Y: "contractible_space Y"
  3706   obtains c where "homotopic_with (\<lambda>h. True) X Y f (\<lambda>x. c)"
  3707   using nullhomotopic_through_contractible_space [OF f _ Y]
  3708   by (metis continuous_map_id id_comp)
  3709 
  3710 lemma nullhomotopic_from_contractible_space:
  3711   assumes f: "continuous_map X Y f" and X: "contractible_space X"
  3712   obtains c where "homotopic_with (\<lambda>h. True) X Y f (\<lambda>x. c)"
  3713   using nullhomotopic_through_contractible_space [OF _ f X]
  3714   by (metis comp_id continuous_map_id)
  3715 
  3716 lemma homotopy_dominated_contractibility:
  3717   assumes f: "continuous_map X Y f" and g: "continuous_map Y X g"
  3718     and hom: "homotopic_with (\<lambda>x. True) Y Y (f \<circ> g) id" and X: "contractible_space X"
  3719   shows "contractible_space Y"
  3720 proof -
  3721   obtain c where c: "homotopic_with (\<lambda>h. True) X Y f (\<lambda>x. c)"
  3722     using nullhomotopic_from_contractible_space [OF f X] .
  3723   have "homotopic_with (\<lambda>x. True) Y Y (f \<circ> g) (\<lambda>x. c)"
  3724     using homotopic_compose_continuous_map_right [OF c g] by fastforce
  3725   then have "homotopic_with (\<lambda>x. True) Y Y id (\<lambda>x. c)"
  3726     using homotopic_with_trans [OF _ hom] homotopic_with_symD by blast
  3727   then show ?thesis
  3728     unfolding contractible_space_def ..
  3729 qed
  3730 
  3731 lemma homotopy_equivalent_space_contractibility:
  3732    "X homotopy_equivalent_space Y \<Longrightarrow> (contractible_space X \<longleftrightarrow> contractible_space Y)"
  3733   unfolding homotopy_equivalent_space_def
  3734   by (blast intro: homotopy_dominated_contractibility)
  3735 
  3736 lemma homeomorphic_space_contractibility:
  3737    "X homeomorphic_space Y
  3738         \<Longrightarrow> (contractible_space X \<longleftrightarrow> contractible_space Y)"
  3739   by (simp add: homeomorphic_imp_homotopy_equivalent_space homotopy_equivalent_space_contractibility)
  3740 
  3741 lemma contractible_eq_homotopy_equivalent_singleton_subtopology:
  3742    "contractible_space X \<longleftrightarrow>
  3743         topspace X = {} \<or> (\<exists>a \<in> topspace X. X homotopy_equivalent_space (subtopology X {a}))"(is "?lhs = ?rhs")
  3744 proof (cases "topspace X = {}")
  3745   case False
  3746   show ?thesis
  3747   proof
  3748     assume ?lhs
  3749     then obtain a where a: "homotopic_with (\<lambda>x. True) X X id (\<lambda>x. a)"
  3750       by (auto simp: contractible_space_def)
  3751     then have "a \<in> topspace X"
  3752       by (metis False continuous_map_const homotopic_with_imp_continuous_maps)
  3753     then have "X homotopy_equivalent_space subtopology X {a}"
  3754       unfolding homotopy_equivalent_space_def
  3755       apply (rule_tac x="\<lambda>x. a" in exI)
  3756       apply (rule_tac x=id in exI)
  3757       apply (auto simp: homotopic_with_sym topspace_subtopology_subset a)
  3758       using connectedin_absolute connectedin_sing contractible_space_alt contractible_space_subtopology_singleton by fastforce
  3759     with \<open>a \<in> topspace X\<close> show ?rhs
  3760       by blast
  3761   next
  3762     assume ?rhs
  3763     then show ?lhs
  3764       by (meson False contractible_space_subtopology_singleton homotopy_equivalent_space_contractibility)
  3765   qed
  3766 qed (simp add: contractible_space_empty)
  3767 
  3768 lemma contractible_space_retraction_map_image:
  3769   assumes "retraction_map X Y f" and X: "contractible_space X"
  3770   shows "contractible_space Y"
  3771 proof -
  3772   obtain g where f: "continuous_map X Y f" and g: "continuous_map Y X g" and fg: "\<forall>y \<in> topspace Y. f(g y) = y"
  3773     using assms by (auto simp: retraction_map_def retraction_maps_def)
  3774   obtain a where a: "homotopic_with (\<lambda>x. True) X X id (\<lambda>x. a)"
  3775     using X by (auto simp: contractible_space_def)
  3776   have "homotopic_with (\<lambda>x. True) Y Y id (\<lambda>x. f a)"
  3777   proof (rule homotopic_with_eq)
  3778     show "homotopic_with (\<lambda>x. True) Y Y (f \<circ> id \<circ> g) (f \<circ> (\<lambda>x. a) \<circ> g)"
  3779       using f g a homotopic_compose_continuous_map_left homotopic_compose_continuous_map_right by metis
  3780   qed (use fg in auto)
  3781   then show ?thesis
  3782     unfolding contractible_space_def by blast
  3783 qed
  3784 
  3785 lemma contractible_space_prod_topology:
  3786    "contractible_space(prod_topology X Y) \<longleftrightarrow>
  3787     topspace X = {} \<or> topspace Y = {} \<or> contractible_space X \<and> contractible_space Y"
  3788 proof (cases "topspace X = {} \<or> topspace Y = {}")
  3789   case True
  3790   then have "topspace (prod_topology X Y) = {}"
  3791     by simp
  3792   then show ?thesis
  3793     by (auto simp: contractible_space_empty)
  3794 next
  3795   case False
  3796   have "contractible_space(prod_topology X Y) \<longleftrightarrow> contractible_space X \<and> contractible_space Y"
  3797   proof safe
  3798     assume XY: "contractible_space (prod_topology X Y)"
  3799     with False have "retraction_map (prod_topology X Y) X fst"
  3800       by (auto simp: contractible_space False retraction_map_fst)
  3801     then show "contractible_space X"
  3802       by (rule contractible_space_retraction_map_image [OF _ XY])
  3803     have "retraction_map (prod_topology X Y) Y snd"
  3804       using False XY  by (auto simp: contractible_space False retraction_map_snd)
  3805     then show "contractible_space Y"
  3806       by (rule contractible_space_retraction_map_image [OF _ XY])
  3807   next
  3808     assume "contractible_space X" and "contractible_space Y"
  3809     with False obtain a b
  3810       where "a \<in> topspace X" and a: "homotopic_with (\<lambda>x. True) X X id (\<lambda>x. a)"
  3811         and "b \<in> topspace Y" and b: "homotopic_with (\<lambda>x. True) Y Y id (\<lambda>x. b)"
  3812       by (auto simp: contractible_space)
  3813     with False show "contractible_space (prod_topology X Y)"
  3814       apply (simp add: contractible_space)
  3815       apply (rule_tac x=a in bexI)
  3816        apply (rule_tac x=b in bexI)
  3817       using homotopic_with_prod_topology [OF a b]
  3818     	  apply (metis (no_types, lifting) case_prod_Pair case_prod_beta' eq_id_iff)
  3819     	 apply auto
  3820     	done
  3821   qed
  3822   with False show ?thesis
  3823     by auto
  3824 qed
  3825 
  3826 
  3827 
  3828 
  3829 lemma contractible_space_product_topology:
  3830   "contractible_space(product_topology X I) \<longleftrightarrow>
  3831     topspace (product_topology X I) = {} \<or> (\<forall>i \<in> I. contractible_space(X i))"
  3832 proof (cases "topspace (product_topology X I) = {}")
  3833   case False
  3834   have 1: "contractible_space (X i)"
  3835     if XI: "contractible_space (product_topology X I)" and "i \<in> I"
  3836     for i
  3837   proof (rule contractible_space_retraction_map_image [OF _ XI])
  3838     show "retraction_map (product_topology X I) (X i) (\<lambda>x. x i)"
  3839       using False by (simp add: retraction_map_product_projection \<open>i \<in> I\<close>)
  3840   qed
  3841   have 2: "contractible_space (product_topology X I)"
  3842     if "x \<in> topspace (product_topology X I)" and cs: "\<forall>i\<in>I. contractible_space (X i)"
  3843     for x :: "'a \<Rightarrow> 'b"
  3844   proof -
  3845     obtain f where f: "\<And>i. i\<in>I \<Longrightarrow> homotopic_with (\<lambda>x. True) (X i) (X i) id (\<lambda>x. f i)"
  3846       using cs unfolding contractible_space_def by metis
  3847     have "homotopic_with (\<lambda>x. True)
  3848                          (product_topology X I) (product_topology X I) id (\<lambda>x. restrict f I)"
  3849       by (rule homotopic_with_eq [OF homotopic_with_product_topology [OF f]]) (auto simp: topspace_product_topology)
  3850     then show ?thesis
  3851       by (auto simp: contractible_space_def)
  3852   qed
  3853   show ?thesis
  3854     using False 1 2 by blast
  3855 qed (simp add: contractible_space_empty)
  3856 
  3857 
  3858 lemma contractible_space_subtopology_euclideanreal [simp]:
  3859   "contractible_space(subtopology euclideanreal S) \<longleftrightarrow> is_interval S"
  3860   (is "?lhs = ?rhs")
  3861 proof
  3862   assume ?lhs
  3863   then have "path_connectedin (subtopology euclideanreal S) S"
  3864     using contractible_imp_path_connected_space path_connectedin_topspace path_connectedin_absolute
  3865     by (simp add: contractible_imp_path_connected) 
  3866   then show ?rhs
  3867     by (simp add: is_interval_path_connected_1)
  3868 next
  3869   assume ?rhs
  3870   then have "convex S"
  3871     by (simp add: is_interval_convex_1)
  3872   show ?lhs
  3873   proof (cases "S = {}")
  3874     case False
  3875     then obtain z where "z \<in> S"
  3876       by blast
  3877     show ?thesis
  3878       unfolding contractible_space_def homotopic_with_def
  3879     proof (intro exI conjI allI)
  3880       show "continuous_map (prod_topology (top_of_set {0..1}) (top_of_set S)) (top_of_set S)
  3881                            (\<lambda>(t,x). (1 - t) * x + t * z)"
  3882         apply (auto simp: case_prod_unfold)
  3883          apply (intro continuous_intros)
  3884         using  \<open>z \<in> S\<close> apply (auto intro: convexD [OF \<open>convex S\<close>, simplified])
  3885         done
  3886     qed auto
  3887   qed (simp add: contractible_space_empty)
  3888 qed
  3889 
  3890 
  3891 corollary contractible_space_euclideanreal: "contractible_space euclideanreal"
  3892 proof -
  3893   have "contractible_space (subtopology euclideanreal UNIV)"
  3894     using contractible_space_subtopology_euclideanreal by blast
  3895   then show ?thesis
  3896     by simp
  3897 qed
  3898 
  3899 
  3900 abbreviation%important homotopy_eqv :: "'a::topological_space set \<Rightarrow> 'b::topological_space set \<Rightarrow> bool"
  3901              (infix "homotopy'_eqv" 50)
  3902   where "S homotopy_eqv T \<equiv> top_of_set S homotopy_equivalent_space top_of_set T"
  3903 
  3904 
  3905 
  3906 
  3907 
  3908 lemma homeomorphic_imp_homotopy_eqv: "S homeomorphic T \<Longrightarrow> S homotopy_eqv T"
  3909   unfolding homeomorphic_def homeomorphism_def homotopy_equivalent_space_def
  3910   apply (erule ex_forward)+
  3911   apply auto
  3912    apply (metis homotopic_with_id2 topspace_euclidean_subtopology)
  3913   by (metis (full_types) homotopic_with_id2 imageE topspace_euclidean_subtopology)
  3914 
  3915 
  3916 lemma homotopy_eqv_inj_linear_image:
  3917   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  3918   assumes "linear f" "inj f"
  3919     shows "(f ` S) homotopy_eqv S"
  3920 apply (rule homeomorphic_imp_homotopy_eqv)
  3921 using assms homeomorphic_sym linear_homeomorphic_image by auto
  3922 
  3923 lemma homotopy_eqv_translation:
  3924     fixes S :: "'a::real_normed_vector set"
  3925     shows "(+) a ` S homotopy_eqv S"
  3926   apply (rule homeomorphic_imp_homotopy_eqv)
  3927   using homeomorphic_translation homeomorphic_sym by blast
  3928 
  3929 lemma homotopy_eqv_homotopic_triviality_imp:
  3930   fixes S :: "'a::real_normed_vector set"
  3931     and T :: "'b::real_normed_vector set"
  3932     and U :: "'c::real_normed_vector set"
  3933   assumes "S homotopy_eqv T"
  3934       and f: "continuous_on U f" "f ` U \<subseteq> T"
  3935       and g: "continuous_on U g" "g ` U \<subseteq> T"
  3936       and homUS: "\<And>f g. \<lbrakk>continuous_on U f; f ` U \<subseteq> S;
  3937                          continuous_on U g; g ` U \<subseteq> S\<rbrakk>
  3938                          \<Longrightarrow> homotopic_with_canon (\<lambda>x. True) U S f g"
  3939     shows "homotopic_with_canon (\<lambda>x. True) U T f g"
  3940 proof -
  3941   obtain h k where h: "continuous_on S h" "h ` S \<subseteq> T"
  3942                and k: "continuous_on T k" "k ` T \<subseteq> S"
  3943                and hom: "homotopic_with_canon (\<lambda>x. True) S S (k \<circ> h) id"
  3944                         "homotopic_with_canon (\<lambda>x. True) T T (h \<circ> k) id"
  3945     using assms by (auto simp: homotopy_equivalent_space_def)
  3946   have "homotopic_with_canon (\<lambda>f. True) U S (k \<circ> f) (k \<circ> g)"
  3947     apply (rule homUS)
  3948     using f g k
  3949     apply (safe intro!: continuous_on_compose h k f elim!: continuous_on_subset)
  3950     apply (force simp: o_def)+
  3951     done
  3952   then have "homotopic_with_canon (\<lambda>x. True) U T (h \<circ> (k \<circ> f)) (h \<circ> (k \<circ> g))"
  3953     apply (rule homotopic_with_compose_continuous_left)
  3954     apply (simp_all add: h)
  3955     done
  3956   moreover have "homotopic_with_canon (\<lambda>x. True) U T (h \<circ> k \<circ> f) (id \<circ> f)"
  3957     apply (rule homotopic_with_compose_continuous_right [where X=T and Y=T])
  3958     apply (auto simp: hom f)
  3959     done
  3960   moreover have "homotopic_with_canon (\<lambda>x. True) U T (h \<circ> k \<circ> g) (id \<circ> g)"
  3961     apply (rule homotopic_with_compose_continuous_right [where X=T and Y=T])
  3962     apply (auto simp: hom g)
  3963     done
  3964   ultimately show "homotopic_with_canon (\<lambda>x. True) U T f g"
  3965     apply (simp add: o_assoc)
  3966     using homotopic_with_trans homotopic_with_sym by blast
  3967 qed
  3968 
  3969 lemma homotopy_eqv_homotopic_triviality:
  3970   fixes S :: "'a::real_normed_vector set"
  3971     and T :: "'b::real_normed_vector set"
  3972     and U :: "'c::real_normed_vector set"
  3973   assumes "S homotopy_eqv T"
  3974     shows "(\<forall>f g. continuous_on U f \<and> f ` U \<subseteq> S \<and>
  3975                    continuous_on U g \<and> g ` U \<subseteq> S
  3976                    \<longrightarrow> homotopic_with_canon (\<lambda>x. True) U S f g) \<longleftrightarrow>
  3977            (\<forall>f g. continuous_on U f \<and> f ` U \<subseteq> T \<and>
  3978                   continuous_on U g \<and> g ` U \<subseteq> T
  3979                   \<longrightarrow> homotopic_with_canon (\<lambda>x. True) U T f g)"
  3980       (is "?lhs = ?rhs")
  3981 proof
  3982   assume ?lhs
  3983   then show ?rhs
  3984     by (metis assms homotopy_eqv_homotopic_triviality_imp)
  3985 next
  3986   assume ?rhs
  3987   moreover
  3988   have "T homotopy_eqv S"
  3989     using assms homotopy_equivalent_space_sym by blast
  3990   ultimately show ?lhs
  3991     by (blast intro: homotopy_eqv_homotopic_triviality_imp)
  3992 qed
  3993 
  3994 
  3995 lemma homotopy_eqv_cohomotopic_triviality_null_imp:
  3996   fixes S :: "'a::real_normed_vector set"
  3997     and T :: "'b::real_normed_vector set"
  3998     and U :: "'c::real_normed_vector set"
  3999   assumes "S homotopy_eqv T"
  4000       and f: "continuous_on T f" "f ` T \<subseteq> U"
  4001       and homSU: "\<And>f. \<lbrakk>continuous_on S f; f ` S \<subseteq> U\<rbrakk>
  4002                       \<Longrightarrow> \<exists>c. homotopic_with_canon (\<lambda>x. True) S U f (\<lambda>x. c)"
  4003   obtains c where "homotopic_with_canon (\<lambda>x. True) T U f (\<lambda>x. c)"
  4004 proof -
  4005   obtain h k where h: "continuous_on S h" "h ` S \<subseteq> T"
  4006                and k: "continuous_on T k" "k ` T \<subseteq> S"
  4007                and hom: "homotopic_with_canon (\<lambda>x. True) S S (k \<circ> h) id"
  4008                         "homotopic_with_canon (\<lambda>x. True) T T (h \<circ> k) id"
  4009     using assms by (auto simp: homotopy_equivalent_space_def)
  4010   obtain c where "homotopic_with_canon (\<lambda>x. True) S U (f \<circ> h) (\<lambda>x. c)"
  4011     apply (rule exE [OF homSU [of "f \<circ> h"]])
  4012     apply (intro continuous_on_compose h)
  4013     using h f  apply (force elim!: continuous_on_subset)+
  4014     done
  4015   then have "homotopic_with_canon (\<lambda>x. True) T U ((f \<circ> h) \<circ> k) ((\<lambda>x. c) \<circ> k)"
  4016     apply (rule homotopic_with_compose_continuous_right [where X=S])
  4017     using k by auto
  4018   moreover have "homotopic_with_canon (\<lambda>x. True) T U (f \<circ> id) (f \<circ> (h \<circ> k))"
  4019     apply (rule homotopic_with_compose_continuous_left [where Y=T])
  4020       apply (simp add: hom homotopic_with_symD)
  4021      using f apply auto
  4022     done
  4023   ultimately show ?thesis
  4024     apply (rule_tac c=c in that)
  4025     apply (simp add: o_def)
  4026     using homotopic_with_trans by blast
  4027 qed
  4028 
  4029 lemma homotopy_eqv_cohomotopic_triviality_null:
  4030   fixes S :: "'a::real_normed_vector set"
  4031     and T :: "'b::real_normed_vector set"
  4032     and U :: "'c::real_normed_vector set"
  4033   assumes "S homotopy_eqv T"
  4034     shows "(\<forall>f. continuous_on S f \<and> f ` S \<subseteq> U
  4035                 \<longrightarrow> (\<exists>c. homotopic_with_canon (\<lambda>x. True) S U f (\<lambda>x. c))) \<longleftrightarrow>
  4036            (\<forall>f. continuous_on T f \<and> f ` T \<subseteq> U
  4037                 \<longrightarrow> (\<exists>c. homotopic_with_canon (\<lambda>x. True) T U f (\<lambda>x. c)))"
  4038 apply (rule iffI)
  4039 apply (metis assms homotopy_eqv_cohomotopic_triviality_null_imp)
  4040 by (metis assms homotopy_eqv_cohomotopic_triviality_null_imp homotopy_equivalent_space_sym)
  4041 
  4042 lemma homotopy_eqv_homotopic_triviality_null_imp:
  4043   fixes S :: "'a::real_normed_vector set"
  4044     and T :: "'b::real_normed_vector set"
  4045     and U :: "'c::real_normed_vector set"
  4046   assumes "S homotopy_eqv T"
  4047       and f: "continuous_on U f" "f ` U \<subseteq> T"
  4048       and homSU: "\<And>f. \<lbrakk>continuous_on U f; f ` U \<subseteq> S\<rbrakk>
  4049                       \<Longrightarrow> \<exists>c. homotopic_with_canon (\<lambda>x. True) U S f (\<lambda>x. c)"
  4050     shows "\<exists>c. homotopic_with_canon (\<lambda>x. True) U T f (\<lambda>x. c)"
  4051 proof -
  4052   obtain h k where h: "continuous_on S h" "h ` S \<subseteq> T"
  4053                and k: "continuous_on T k" "k ` T \<subseteq> S"
  4054                and hom: "homotopic_with_canon (\<lambda>x. True) S S (k \<circ> h) id"
  4055                         "homotopic_with_canon (\<lambda>x. True) T T (h \<circ> k) id"
  4056     using assms by (auto simp: homotopy_equivalent_space_def)
  4057   obtain c::'a where "homotopic_with_canon (\<lambda>x. True) U S (k \<circ> f) (\<lambda>x. c)"
  4058     apply (rule exE [OF homSU [of "k \<circ> f"]])
  4059     apply (intro continuous_on_compose h)
  4060     using k f  apply (force elim!: continuous_on_subset)+
  4061     done
  4062   then have "homotopic_with_canon (\<lambda>x. True) U T (h \<circ> (k \<circ> f)) (h \<circ> (\<lambda>x. c))"
  4063     apply (rule homotopic_with_compose_continuous_left [where Y=S])
  4064     using h by auto
  4065   moreover have "homotopic_with_canon (\<lambda>x. True) U T (id \<circ> f) ((h \<circ> k) \<circ> f)"
  4066     apply (rule homotopic_with_compose_continuous_right [where X=T])
  4067       apply (simp add: hom homotopic_with_symD)
  4068      using f apply auto
  4069     done
  4070   ultimately show ?thesis
  4071     using homotopic_with_trans by (fastforce simp add: o_def)
  4072 qed
  4073 
  4074 lemma homotopy_eqv_homotopic_triviality_null:
  4075   fixes S :: "'a::real_normed_vector set"
  4076     and T :: "'b::real_normed_vector set"
  4077     and U :: "'c::real_normed_vector set"
  4078   assumes "S homotopy_eqv T"
  4079     shows "(\<forall>f. continuous_on U f \<and> f ` U \<subseteq> S
  4080                   \<longrightarrow> (\<exists>c. homotopic_with_canon (\<lambda>x. True) U S f (\<lambda>x. c))) \<longleftrightarrow>
  4081            (\<forall>f. continuous_on U f \<and> f ` U \<subseteq> T
  4082                   \<longrightarrow> (\<exists>c. homotopic_with_canon (\<lambda>x. True) U T f (\<lambda>x. c)))"
  4083 apply (rule iffI)
  4084 apply (metis assms homotopy_eqv_homotopic_triviality_null_imp)
  4085 by (metis assms homotopy_eqv_homotopic_triviality_null_imp homotopy_equivalent_space_sym)
  4086 
  4087 lemma homotopy_eqv_contractible_sets:
  4088   fixes S :: "'a::real_normed_vector set"
  4089     and T :: "'b::real_normed_vector set"
  4090   assumes "contractible S" "contractible T" "S = {} \<longleftrightarrow> T = {}"
  4091     shows "S homotopy_eqv T"
  4092 proof (cases "S = {}")
  4093   case True with assms show ?thesis
  4094     by (simp add: homeomorphic_imp_homotopy_eqv)
  4095 next
  4096   case False
  4097   with assms obtain a b where "a \<in> S" "b \<in> T"
  4098     by auto
  4099   then show ?thesis
  4100     unfolding homotopy_equivalent_space_def
  4101     apply (rule_tac x="\<lambda>x. b" in exI)
  4102     apply (rule_tac x="\<lambda>x. a" in exI)
  4103     apply (intro assms conjI continuous_on_id' homotopic_into_contractible)
  4104     apply (auto simp: o_def continuous_on_const)
  4105     done
  4106 qed
  4107 
  4108 lemma homotopy_eqv_empty1 [simp]:
  4109   fixes S :: "'a::real_normed_vector set"
  4110   shows "S homotopy_eqv ({}::'b::real_normed_vector set) \<longleftrightarrow> S = {}"
  4111   apply (rule iffI)
  4112    apply (metis Abstract_Topology.continuous_map_subtopology_eu emptyE equals0I homotopy_equivalent_space_def image_subset_iff)
  4113   by (simp add: homotopy_eqv_contractible_sets)
  4114 
  4115 lemma homotopy_eqv_empty2 [simp]:
  4116   fixes S :: "'a::real_normed_vector set"
  4117   shows "({}::'b::real_normed_vector set) homotopy_eqv S \<longleftrightarrow> S = {}"
  4118   using homotopy_equivalent_space_sym homotopy_eqv_empty1 by blast
  4119 
  4120 lemma homotopy_eqv_contractibility:
  4121   fixes S :: "'a::real_normed_vector set" and T :: "'b::real_normed_vector set"
  4122   shows "S homotopy_eqv T \<Longrightarrow> (contractible S \<longleftrightarrow> contractible T)"
  4123   by (meson contractible_space_top_of_set homotopy_equivalent_space_contractibility)
  4124 
  4125 lemma homotopy_eqv_sing:
  4126   fixes S :: "'a::real_normed_vector set" and a :: "'b::real_normed_vector"
  4127   shows "S homotopy_eqv {a} \<longleftrightarrow> S \<noteq> {} \<and> contractible S"
  4128 proof (cases "S = {}")
  4129   case True then show ?thesis
  4130     by simp
  4131 next
  4132   case False then show ?thesis
  4133     by (metis contractible_sing empty_not_insert homotopy_eqv_contractibility homotopy_eqv_contractible_sets)
  4134 qed
  4135 
  4136 lemma homeomorphic_contractible_eq:
  4137   fixes S :: "'a::real_normed_vector set" and T :: "'b::real_normed_vector set"
  4138   shows "S homeomorphic T \<Longrightarrow> (contractible S \<longleftrightarrow> contractible T)"
  4139 by (simp add: homeomorphic_imp_homotopy_eqv homotopy_eqv_contractibility)
  4140 
  4141 lemma homeomorphic_contractible:
  4142   fixes S :: "'a::real_normed_vector set" and T :: "'b::real_normed_vector set"
  4143   shows "\<lbrakk>contractible S; S homeomorphic T\<rbrakk> \<Longrightarrow> contractible T"
  4144   by (metis homeomorphic_contractible_eq)
  4145 
  4146 
  4147 subsection%unimportant\<open>Misc other results\<close>
  4148 
  4149 lemma bounded_connected_Compl_real:
  4150   fixes S :: "real set"
  4151   assumes "bounded S" and conn: "connected(- S)"
  4152     shows "S = {}"
  4153 proof -
  4154   obtain a b where "S \<subseteq> box a b"
  4155     by (meson assms bounded_subset_box_symmetric)
  4156   then have "a \<notin> S" "b \<notin> S"
  4157     by auto
  4158   then have "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> x \<in> - S"
  4159     by (meson Compl_iff conn connected_iff_interval)
  4160   then show ?thesis
  4161     using \<open>S \<subseteq> box a b\<close> by auto
  4162 qed
  4163 
  4164 corollary bounded_path_connected_Compl_real:
  4165   fixes S :: "real set"
  4166   assumes "bounded S" "path_connected(- S)" shows "S = {}"
  4167   by (simp add: assms bounded_connected_Compl_real path_connected_imp_connected)
  4168 
  4169 lemma bounded_connected_Compl_1:
  4170   fixes S :: "'a::{euclidean_space} set"
  4171   assumes "bounded S" and conn: "connected(- S)" and 1: "DIM('a) = 1"
  4172     shows "S = {}"
  4173 proof -
  4174   have "DIM('a) = DIM(real)"
  4175     by (simp add: "1")
  4176   then obtain f::"'a \<Rightarrow> real" and g
  4177   where "linear f" "\<And>x. norm(f x) = norm x" "\<And>x. g(f x) = x" "\<And>y. f(g y) = y"
  4178     by (rule isomorphisms_UNIV_UNIV) blast
  4179   with \<open>bounded S\<close> have "bounded (f ` S)"
  4180     using bounded_linear_image linear_linear by blast
  4181   have "connected (f ` (-S))"
  4182     using connected_linear_image assms \<open>linear f\<close> by blast
  4183   moreover have "f ` (-S) = - (f ` S)"
  4184     apply (rule bij_image_Compl_eq)
  4185     apply (auto simp: bij_def)
  4186      apply (metis \<open>\<And>x. g (f x) = x\<close> injI)
  4187     by (metis UNIV_I \<open>\<And>y. f (g y) = y\<close> image_iff)
  4188   finally have "connected (- (f ` S))"
  4189     by simp
  4190   then have "f ` S = {}"
  4191     using \<open>bounded (f ` S)\<close> bounded_connected_Compl_real by blast
  4192   then show ?thesis
  4193     by blast
  4194 qed
  4195 
  4196 
  4197 subsection%unimportant\<open>Some Uncountable Sets\<close>
  4198 
  4199 lemma uncountable_closed_segment:
  4200   fixes a :: "'a::real_normed_vector"
  4201   assumes "a \<noteq> b" shows "uncountable (closed_segment a b)"
  4202 unfolding path_image_linepath [symmetric] path_image_def
  4203   using inj_on_linepath [OF assms] uncountable_closed_interval [of 0 1]
  4204         countable_image_inj_on by auto
  4205 
  4206 lemma uncountable_open_segment:
  4207   fixes a :: "'a::real_normed_vector"
  4208   assumes "a \<noteq> b" shows "uncountable (open_segment a b)"
  4209   by (simp add: assms open_segment_def uncountable_closed_segment uncountable_minus_countable)
  4210 
  4211 lemma uncountable_convex:
  4212   fixes a :: "'a::real_normed_vector"
  4213   assumes "convex S" "a \<in> S" "b \<in> S" "a \<noteq> b"
  4214     shows "uncountable S"
  4215 proof -
  4216   have "uncountable (closed_segment a b)"
  4217     by (simp add: uncountable_closed_segment assms)
  4218   then show ?thesis
  4219     by (meson assms convex_contains_segment countable_subset)
  4220 qed
  4221 
  4222 lemma uncountable_ball:
  4223   fixes a :: "'a::euclidean_space"
  4224   assumes "r > 0"
  4225     shows "uncountable (ball a r)"
  4226 proof -
  4227   have "uncountable (open_segment a (a + r *\<^sub>R (SOME i. i \<in> Basis)))"
  4228     by (metis Basis_zero SOME_Basis add_cancel_right_right assms less_le scale_eq_0_iff uncountable_open_segment)
  4229   moreover have "open_segment a (a + r *\<^sub>R (SOME i. i \<in> Basis)) \<subseteq> ball a r"
  4230     using assms by (auto simp: in_segment algebra_simps dist_norm SOME_Basis)
  4231   ultimately show ?thesis
  4232     by (metis countable_subset)
  4233 qed
  4234 
  4235 lemma ball_minus_countable_nonempty:
  4236   assumes "countable (A :: 'a :: euclidean_space set)" "r > 0"
  4237   shows   "ball z r - A \<noteq> {}"
  4238 proof
  4239   assume *: "ball z r - A = {}"
  4240   have "uncountable (ball z r - A)"
  4241     by (intro uncountable_minus_countable assms uncountable_ball)
  4242   thus False by (subst (asm) *) auto
  4243 qed
  4244 
  4245 lemma uncountable_cball:
  4246   fixes a :: "'a::euclidean_space"
  4247   assumes "r > 0"
  4248   shows "uncountable (cball a r)"
  4249   using assms countable_subset uncountable_ball by auto
  4250 
  4251 lemma pairwise_disjnt_countable:
  4252   fixes \<N> :: "nat set set"
  4253   assumes "pairwise disjnt \<N>"
  4254     shows "countable \<N>"
  4255 proof -
  4256   have "inj_on (\<lambda>X. SOME n. n \<in> X) (\<N> - {{}})"
  4257     apply (clarsimp simp add: inj_on_def)
  4258     by (metis assms disjnt_insert2 insert_absorb pairwise_def subsetI subset_empty tfl_some)
  4259   then show ?thesis
  4260     by (metis countable_Diff_eq countable_def)
  4261 qed
  4262 
  4263 lemma pairwise_disjnt_countable_Union:
  4264     assumes "countable (\<Union>\<N>)" and pwd: "pairwise disjnt \<N>"
  4265     shows "countable \<N>"
  4266 proof -
  4267   obtain f :: "_ \<Rightarrow> nat" where f: "inj_on f (\<Union>\<N>)"
  4268     using assms by blast
  4269   then have "pairwise disjnt (\<Union> X \<in> \<N>. {f ` X})"
  4270     using assms by (force simp: pairwise_def disjnt_inj_on_iff [OF f])
  4271   then have "countable (\<Union> X \<in> \<N>. {f ` X})"
  4272     using pairwise_disjnt_countable by blast
  4273   then show ?thesis
  4274     by (meson pwd countable_image_inj_on disjoint_image f inj_on_image pairwise_disjnt_countable)
  4275 qed
  4276 
  4277 lemma connected_uncountable:
  4278   fixes S :: "'a::metric_space set"
  4279   assumes "connected S" "a \<in> S" "b \<in> S" "a \<noteq> b" shows "uncountable S"
  4280 proof -
  4281   have "continuous_on S (dist a)"
  4282     by (intro continuous_intros)
  4283   then have "connected (dist a ` S)"
  4284     by (metis connected_continuous_image \<open>connected S\<close>)
  4285   then have "closed_segment 0 (dist a b) \<subseteq> (dist a ` S)"
  4286     by (simp add: assms closed_segment_subset is_interval_connected_1 is_interval_convex)
  4287   then have "uncountable (dist a ` S)"
  4288     by (metis \<open>a \<noteq> b\<close> countable_subset dist_eq_0_iff uncountable_closed_segment)
  4289   then show ?thesis
  4290     by blast
  4291 qed
  4292 
  4293 lemma path_connected_uncountable:
  4294   fixes S :: "'a::metric_space set"
  4295   assumes "path_connected S" "a \<in> S" "b \<in> S" "a \<noteq> b" shows "uncountable S"
  4296   using path_connected_imp_connected assms connected_uncountable by metis
  4297 
  4298 lemma connected_finite_iff_sing:
  4299   fixes S :: "'a::metric_space set"
  4300   assumes "connected S"
  4301   shows "finite S \<longleftrightarrow> S = {} \<or> (\<exists>a. S = {a})"  (is "_ = ?rhs")
  4302 proof -
  4303   have "uncountable S" if "\<not> ?rhs"
  4304     using connected_uncountable assms that by blast
  4305   then show ?thesis
  4306     using uncountable_infinite by auto
  4307 qed
  4308 
  4309 lemma connected_card_eq_iff_nontrivial:
  4310   fixes S :: "'a::metric_space set"
  4311   shows "connected S \<Longrightarrow> uncountable S \<longleftrightarrow> \<not>(\<exists>a. S \<subseteq> {a})"
  4312   apply (auto simp: countable_finite finite_subset)
  4313   by (metis connected_uncountable is_singletonI' is_singleton_the_elem subset_singleton_iff)
  4314 
  4315 lemma simple_path_image_uncountable:
  4316   fixes g :: "real \<Rightarrow> 'a::metric_space"
  4317   assumes "simple_path g"
  4318   shows "uncountable (path_image g)"
  4319 proof -
  4320   have "g 0 \<in> path_image g" "g (1/2) \<in> path_image g"
  4321     by (simp_all add: path_defs)
  4322   moreover have "g 0 \<noteq> g (1/2)"
  4323     using assms by (fastforce simp add: simple_path_def)
  4324   ultimately show ?thesis
  4325     apply (simp add: assms connected_card_eq_iff_nontrivial connected_simple_path_image)
  4326     by blast
  4327 qed
  4328 
  4329 lemma arc_image_uncountable:
  4330   fixes g :: "real \<Rightarrow> 'a::metric_space"
  4331   assumes "arc g"
  4332   shows "uncountable (path_image g)"
  4333   by (simp add: arc_imp_simple_path assms simple_path_image_uncountable)
  4334 
  4335 
  4336 subsection%unimportant\<open> Some simple positive connection theorems\<close>
  4337 
  4338 proposition path_connected_convex_diff_countable:
  4339   fixes U :: "'a::euclidean_space set"
  4340   assumes "convex U" "\<not> collinear U" "countable S"
  4341     shows "path_connected(U - S)"
  4342 proof (clarsimp simp add: path_connected_def)
  4343   fix a b
  4344   assume "a \<in> U" "a \<notin> S" "b \<in> U" "b \<notin> S"
  4345   let ?m = "midpoint a b"
  4346   show "\<exists>g. path g \<and> path_image g \<subseteq> U - S \<and> pathstart g = a \<and> pathfinish g = b"
  4347   proof (cases "a = b")
  4348     case True
  4349     then show ?thesis
  4350       by (metis DiffI \<open>a \<in> U\<close> \<open>a \<notin> S\<close> path_component_def path_component_refl)
  4351   next
  4352     case False
  4353     then have "a \<noteq> ?m" "b \<noteq> ?m"
  4354       using midpoint_eq_endpoint by fastforce+
  4355     have "?m \<in> U"
  4356       using \<open>a \<in> U\<close> \<open>b \<in> U\<close> \<open>convex U\<close> convex_contains_segment by force
  4357     obtain c where "c \<in> U" and nc_abc: "\<not> collinear {a,b,c}"
  4358       by (metis False \<open>a \<in> U\<close> \<open>b \<in> U\<close> \<open>\<not> collinear U\<close> collinear_triples insert_absorb)
  4359     have ncoll_mca: "\<not> collinear {?m,c,a}"
  4360       by (metis (full_types) \<open>a \<noteq> ?m\<close> collinear_3_trans collinear_midpoint insert_commute nc_abc)
  4361     have ncoll_mcb: "\<not> collinear {?m,c,b}"
  4362       by (metis (full_types) \<open>b \<noteq> ?m\<close> collinear_3_trans collinear_midpoint insert_commute nc_abc)
  4363     have "c \<noteq> ?m"
  4364       by (metis collinear_midpoint insert_commute nc_abc)
  4365     then have "closed_segment ?m c \<subseteq> U"
  4366       by (simp add: \<open>c \<in> U\<close> \<open>?m \<in> U\<close> \<open>convex U\<close> closed_segment_subset)
  4367     then obtain z where z: "z \<in> closed_segment ?m c"
  4368                     and disjS: "(closed_segment a z \<union> closed_segment z b) \<inter> S = {}"
  4369     proof -
  4370       have False if "closed_segment ?m c \<subseteq> {z. (closed_segment a z \<union> closed_segment z b) \<inter> S \<noteq> {}}"
  4371       proof -
  4372         have closb: "closed_segment ?m c \<subseteq>
  4373                  {z \<in> closed_segment ?m c. closed_segment a z \<inter> S \<noteq> {}} \<union> {z \<in> closed_segment ?m c. closed_segment z b \<inter> S \<noteq> {}}"
  4374           using that by blast
  4375         have *: "countable {z \<in> closed_segment ?m c. closed_segment z u \<inter> S \<noteq> {}}"
  4376           if "u \<in> U" "u \<notin> S" and ncoll: "\<not> collinear {?m, c, u}" for u
  4377         proof -
  4378           have **: False if x1: "x1 \<in> closed_segment ?m c" and x2: "x2 \<in> closed_segment ?m c"
  4379                             and "x1 \<noteq> x2" "x1 \<noteq> u"
  4380                             and w: "w \<in> closed_segment x1 u" "w \<in> closed_segment x2 u"
  4381                             and "w \<in> S" for x1 x2 w
  4382           proof -
  4383             have "x1 \<in> affine hull {?m,c}" "x2 \<in> affine hull {?m,c}"
  4384               using segment_as_ball x1 x2 by auto
  4385             then have coll_x1: "collinear {x1, ?m, c}" and coll_x2: "collinear {?m, c, x2}"
  4386               by (simp_all add: affine_hull_3_imp_collinear) (metis affine_hull_3_imp_collinear insert_commute)
  4387             have "\<not> collinear {x1, u, x2}"
  4388             proof
  4389               assume "collinear {x1, u, x2}"
  4390               then have "collinear {?m, c, u}"
  4391                 by (metis (full_types) \<open>c \<noteq> ?m\<close> coll_x1 coll_x2 collinear_3_trans insert_commute ncoll \<open>x1 \<noteq> x2\<close>)
  4392               with ncoll show False ..
  4393             qed
  4394             then have "closed_segment x1 u \<inter> closed_segment u x2 = {u}"
  4395               by (blast intro!: Int_closed_segment)
  4396             then have "w = u"
  4397               using closed_segment_commute w by auto
  4398             show ?thesis
  4399               using \<open>u \<notin> S\<close> \<open>w = u\<close> that(7) by auto
  4400           qed
  4401           then have disj: "disjoint ((\<Union>z\<in>closed_segment ?m c. {closed_segment z u \<inter> S}))"
  4402             by (fastforce simp: pairwise_def disjnt_def)
  4403           have cou: "countable ((\<Union>z \<in> closed_segment ?m c. {closed_segment z u \<inter> S}) - {{}})"
  4404             apply (rule pairwise_disjnt_countable_Union [OF _ pairwise_subset [OF disj]])
  4405              apply (rule countable_subset [OF _ \<open>countable S\<close>], auto)
  4406             done
  4407           define f where "f \<equiv> \<lambda>X. (THE z. z \<in> closed_segment ?m c \<and> X = closed_segment z u \<inter> S)"
  4408           show ?thesis
  4409           proof (rule countable_subset [OF _ countable_image [OF cou, where f=f]], clarify)
  4410             fix x
  4411             assume x: "x \<in> closed_segment ?m c" "closed_segment x u \<inter> S \<noteq> {}"
  4412             show "x \<in> f ` ((\<Union>z\<in>closed_segment ?m c. {closed_segment z u \<inter> S}) - {{}})"
  4413             proof (rule_tac x="closed_segment x u \<inter> S" in image_eqI)
  4414               show "x = f (closed_segment x u \<inter> S)"
  4415                 unfolding f_def
  4416                 apply (rule the_equality [symmetric])
  4417                 using x  apply (auto simp: dest: **)
  4418                 done
  4419             qed (use x in auto)
  4420           qed
  4421         qed
  4422         have "uncountable (closed_segment ?m c)"
  4423           by (metis \<open>c \<noteq> ?m\<close> uncountable_closed_segment)
  4424         then show False
  4425           using closb * [OF \<open>a \<in> U\<close> \<open>a \<notin> S\<close> ncoll_mca] * [OF \<open>b \<in> U\<close> \<open>b \<notin> S\<close> ncoll_mcb]
  4426           apply (simp add: closed_segment_commute)
  4427           by (simp add: countable_subset)
  4428       qed
  4429       then show ?thesis
  4430         by (force intro: that)
  4431     qed
  4432     show ?thesis
  4433     proof (intro exI conjI)
  4434       have "path_image (linepath a z +++ linepath z b) \<subseteq> U"
  4435         by (metis \<open>a \<in> U\<close> \<open>b \<in> U\<close> \<open>closed_segment ?m c \<subseteq> U\<close> z \<open>convex U\<close> closed_segment_subset contra_subsetD path_image_linepath subset_path_image_join)
  4436       with disjS show "path_image (linepath a z +++ linepath z b) \<subseteq> U - S"
  4437         by (force simp: path_image_join)
  4438     qed auto
  4439   qed
  4440 qed
  4441 
  4442 
  4443 corollary connected_convex_diff_countable:
  4444   fixes U :: "'a::euclidean_space set"
  4445   assumes "convex U" "\<not> collinear U" "countable S"
  4446   shows "connected(U - S)"
  4447   by (simp add: assms path_connected_convex_diff_countable path_connected_imp_connected)
  4448 
  4449 lemma path_connected_punctured_convex:
  4450   assumes "convex S" and aff: "aff_dim S \<noteq> 1"
  4451     shows "path_connected(S - {a})"
  4452 proof -
  4453   consider "aff_dim S = -1" | "aff_dim S = 0" | "aff_dim S \<ge> 2"
  4454     using assms aff_dim_geq [of S] by linarith
  4455   then show ?thesis
  4456   proof cases
  4457     assume "aff_dim S = -1"
  4458     then show ?thesis
  4459       by (metis aff_dim_empty empty_Diff path_connected_empty)
  4460   next
  4461     assume "aff_dim S = 0"
  4462     then show ?thesis
  4463       by (metis aff_dim_eq_0 Diff_cancel Diff_empty Diff_insert0 convex_empty convex_imp_path_connected path_connected_singleton singletonD)
  4464   next
  4465     assume ge2: "aff_dim S \<ge> 2"
  4466     then have "\<not> collinear S"
  4467     proof (clarsimp simp add: collinear_affine_hull)
  4468       fix u v
  4469       assume "S \<subseteq> affine hull {u, v}"
  4470       then have "aff_dim S \<le> aff_dim {u, v}"
  4471         by (metis (no_types) aff_dim_affine_hull aff_dim_subset)
  4472       with ge2 show False
  4473         by (metis (no_types) aff_dim_2 antisym aff not_numeral_le_zero one_le_numeral order_trans)
  4474     qed
  4475     then show ?thesis
  4476       apply (rule path_connected_convex_diff_countable [OF \<open>convex S\<close>])
  4477       by simp
  4478   qed
  4479 qed
  4480 
  4481 lemma connected_punctured_convex:
  4482   shows "\<lbrakk>convex S; aff_dim S \<noteq> 1\<rbrakk> \<Longrightarrow> connected(S - {a})"
  4483   using path_connected_imp_connected path_connected_punctured_convex by blast
  4484 
  4485 lemma path_connected_complement_countable:
  4486   fixes S :: "'a::euclidean_space set"
  4487   assumes "2 \<le> DIM('a)" "countable S"
  4488   shows "path_connected(- S)"
  4489 proof -
  4490   have "path_connected(UNIV - S)"
  4491     apply (rule path_connected_convex_diff_countable)
  4492     using assms by (auto simp: collinear_aff_dim [of "UNIV :: 'a set"])
  4493   then show ?thesis
  4494     by (simp add: Compl_eq_Diff_UNIV)
  4495 qed
  4496 
  4497 proposition path_connected_openin_diff_countable:
  4498   fixes S :: "'a::euclidean_space set"
  4499   assumes "connected S" and ope: "openin (top_of_set (affine hull S)) S"
  4500       and "\<not> collinear S" "countable T"
  4501     shows "path_connected(S - T)"
  4502 proof (clarsimp simp add: path_connected_component)
  4503   fix x y
  4504   assume xy: "x \<in> S" "x \<notin> T" "y \<in> S" "y \<notin> T"
  4505   show "path_component (S - T) x y"
  4506   proof (rule connected_equivalence_relation_gen [OF \<open>connected S\<close>, where P = "\<lambda>x. x \<notin> T"])
  4507     show "\<exists>z. z \<in> U \<and> z \<notin> T" if opeU: "openin (top_of_set S) U" and "x \<in> U" for U x
  4508     proof -
  4509       have "openin (top_of_set (affine hull S)) U"
  4510         using opeU ope openin_trans by blast
  4511       with \<open>x \<in> U\<close> obtain r where Usub: "U \<subseteq> affine hull S" and "r > 0"
  4512                               and subU: "ball x r \<inter> affine hull S \<subseteq> U"
  4513         by (auto simp: openin_contains_ball)
  4514       with \<open>x \<in> U\<close> have x: "x \<in> ball x r \<inter> affine hull S"
  4515         by auto
  4516       have "\<not> S \<subseteq> {x}"
  4517         using \<open>\<not> collinear S\<close>  collinear_subset by blast
  4518       then obtain x' where "x' \<noteq> x" "x' \<in> S"
  4519         by blast
  4520       obtain y where y: "y \<noteq> x" "y \<in> ball x r \<inter> affine hull S"
  4521       proof
  4522         show "x + (r / 2 / norm(x' - x)) *\<^sub>R (x' - x) \<noteq> x"
  4523           using \<open>x' \<noteq> x\<close> \<open>r > 0\<close> by auto
  4524         show "x + (r / 2 / norm (x' - x)) *\<^sub>R (x' - x) \<in> ball x r \<inter> affine hull S"
  4525           using \<open>x' \<noteq> x\<close> \<open>r > 0\<close> \<open>x' \<in> S\<close> x
  4526           by (simp add: dist_norm mem_affine_3_minus hull_inc)
  4527       qed
  4528       have "convex (ball x r \<inter> affine hull S)"
  4529         by (simp add: affine_imp_convex convex_Int)
  4530       with x y subU have "uncountable U"
  4531         by (meson countable_subset uncountable_convex)
  4532       then have "\<not> U \<subseteq> T"
  4533         using \<open>countable T\<close> countable_subset by blast
  4534       then show ?thesis by blast
  4535     qed
  4536     show "\<exists>U. openin (top_of_set S) U \<and> x \<in> U \<and>
  4537               (\<forall>x\<in>U. \<forall>y\<in>U. x \<notin> T \<and> y \<notin> T \<longrightarrow> path_component (S - T) x y)"
  4538           if "x \<in> S" for x
  4539     proof -
  4540       obtain r where Ssub: "S \<subseteq> affine hull S" and "r > 0"
  4541                  and subS: "ball x r \<inter> affine hull S \<subseteq> S"
  4542         using ope \<open>x \<in> S\<close> by (auto simp: openin_contains_ball)
  4543       then have conv: "convex (ball x r \<inter> affine hull S)"
  4544         by (simp add: affine_imp_convex convex_Int)
  4545       have "\<not> aff_dim (affine hull S) \<le> 1"
  4546         using \<open>\<not> collinear S\<close> collinear_aff_dim by auto
  4547       then have "\<not> collinear (ball x r \<inter> affine hull S)"
  4548         apply (simp add: collinear_aff_dim)
  4549         by (metis (no_types, hide_lams) aff_dim_convex_Int_open IntI open_ball \<open>0 < r\<close> aff_dim_affine_hull affine_affine_hull affine_imp_convex centre_in_ball empty_iff hull_subset inf_commute subsetCE that)
  4550       then have *: "path_connected ((ball x r \<inter> affine hull S) - T)"
  4551         by (rule path_connected_convex_diff_countable [OF conv _ \<open>countable T\<close>])
  4552       have ST: "ball x r \<inter> affine hull S - T \<subseteq> S - T"
  4553         using subS by auto
  4554       show ?thesis
  4555       proof (intro exI conjI)
  4556         show "x \<in> ball x r \<inter> affine hull S"
  4557           using \<open>x \<in> S\<close> \<open>r > 0\<close> by (simp add: hull_inc)
  4558         have "openin (top_of_set (affine hull S)) (ball x r \<inter> affine hull S)"
  4559           by (subst inf.commute) (simp add: openin_Int_open)
  4560         then show "openin (top_of_set S) (ball x r \<inter> affine hull S)"
  4561           by (rule openin_subset_trans [OF _ subS Ssub])
  4562       qed (use * path_component_trans in \<open>auto simp: path_connected_component path_component_of_subset [OF ST]\<close>)
  4563     qed
  4564   qed (use xy path_component_trans in auto)
  4565 qed
  4566 
  4567 corollary connected_openin_diff_countable:
  4568   fixes S :: "'a::euclidean_space set"
  4569   assumes "connected S" and ope: "openin (top_of_set (affine hull S)) S"
  4570       and "\<not> collinear S" "countable T"
  4571     shows "connected(S - T)"
  4572   by (metis path_connected_imp_connected path_connected_openin_diff_countable [OF assms])
  4573 
  4574 corollary path_connected_open_diff_countable:
  4575   fixes S :: "'a::euclidean_space set"
  4576   assumes "2 \<le> DIM('a)" "open S" "connected S" "countable T"
  4577   shows "path_connected(S - T)"
  4578 proof (cases "S = {}")
  4579   case True
  4580   then show ?thesis
  4581     by (simp add: path_connected_empty)
  4582 next
  4583   case False
  4584   show ?thesis
  4585   proof (rule path_connected_openin_diff_countable)
  4586     show "openin (top_of_set (affine hull S)) S"
  4587       by (simp add: assms hull_subset open_subset)
  4588     show "\<not> collinear S"
  4589       using assms False by (simp add: collinear_aff_dim aff_dim_open)
  4590   qed (simp_all add: assms)
  4591 qed
  4592 
  4593 corollary connected_open_diff_countable:
  4594   fixes S :: "'a::euclidean_space set"
  4595   assumes "2 \<le> DIM('a)" "open S" "connected S" "countable T"
  4596   shows "connected(S - T)"
  4597 by (simp add: assms path_connected_imp_connected path_connected_open_diff_countable)
  4598 
  4599 
  4600 
  4601 subsection%unimportant \<open>Self-homeomorphisms shuffling points about\<close>
  4602 
  4603 subsubsection%unimportant\<open>The theorem \<open>homeomorphism_moving_points_exists\<close>\<close>
  4604 
  4605 lemma homeomorphism_moving_point_1:
  4606   fixes a :: "'a::euclidean_space"
  4607   assumes "affine T" "a \<in> T" and u: "u \<in> ball a r \<inter> T"
  4608   obtains f g where "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f g"
  4609                     "f a = u" "\<And>x. x \<in> sphere a r \<Longrightarrow> f x = x"
  4610 proof -
  4611   have nou: "norm (u - a) < r" and "u \<in> T"
  4612     using u by (auto simp: dist_norm norm_minus_commute)
  4613   then have "0 < r"
  4614     by (metis DiffD1 Diff_Diff_Int ball_eq_empty centre_in_ball not_le u)
  4615   define f where "f \<equiv> \<lambda>x. (1 - norm(x - a) / r) *\<^sub>R (u - a) + x"
  4616   have *: "False" if eq: "x + (norm y / r) *\<^sub>R u = y + (norm x / r) *\<^sub>R u"
  4617                   and nou: "norm u < r" and yx: "norm y < norm x" for x y and u::'a
  4618   proof -
  4619     have "x = y + (norm x / r - (norm y / r)) *\<^sub>R u"
  4620       using eq by (simp add: algebra_simps)
  4621     then have "norm x = norm (y + ((norm x - norm y) / r) *\<^sub>R u)"
  4622       by (metis diff_divide_distrib)
  4623     also have "\<dots> \<le> norm y + norm(((norm x - norm y) / r) *\<^sub>R u)"
  4624       using norm_triangle_ineq by blast
  4625     also have "\<dots> = norm y + (norm x - norm y) * (norm u / r)"
  4626       using yx \<open>r > 0\<close>
  4627       by (simp add: divide_simps)
  4628     also have "\<dots> < norm y + (norm x - norm y) * 1"
  4629       apply (subst add_less_cancel_left)
  4630       apply (rule mult_strict_left_mono)
  4631       using nou \<open>0 < r\<close> yx
  4632        apply (simp_all add: field_simps)
  4633       done
  4634     also have "\<dots> = norm x"
  4635       by simp
  4636     finally show False by simp
  4637   qed
  4638   have "inj f"
  4639     unfolding f_def
  4640   proof (clarsimp simp: inj_on_def)
  4641     fix x y
  4642     assume "(1 - norm (x - a) / r) *\<^sub>R (u - a) + x =
  4643             (1 - norm (y - a) / r) *\<^sub>R (u - a) + y"
  4644     then have eq: "(x - a) + (norm (y - a) / r) *\<^sub>R (u - a) = (y - a) + (norm (x - a) / r) *\<^sub>R (u - a)"
  4645       by (auto simp: algebra_simps)
  4646     show "x=y"
  4647     proof (cases "norm (x - a) = norm (y - a)")
  4648       case True
  4649       then show ?thesis
  4650         using eq by auto
  4651     next
  4652       case False
  4653       then consider "norm (x - a) < norm (y - a)" | "norm (x - a) > norm (y - a)"
  4654         by linarith
  4655       then have "False"
  4656       proof cases
  4657         case 1 show False
  4658           using * [OF _ nou 1] eq by simp
  4659       next
  4660         case 2 with * [OF eq nou] show False
  4661           by auto
  4662       qed
  4663       then show "x=y" ..
  4664     qed
  4665   qed
  4666   then have inj_onf: "inj_on f (cball a r \<inter> T)"
  4667     using inj_on_Int by fastforce
  4668   have contf: "continuous_on (cball a r \<inter> T) f"
  4669     unfolding f_def using \<open>0 < r\<close>  by (intro continuous_intros) blast
  4670   have fim: "f ` (cball a r \<inter> T) = cball a r \<inter> T"
  4671   proof
  4672     have *: "norm (y + (1 - norm y / r) *\<^sub>R u) \<le> r" if "norm y \<le> r" "norm u < r" for y u::'a
  4673     proof -
  4674       have "norm (y + (1 - norm y / r) *\<^sub>R u) \<le> norm y + norm((1 - norm y / r) *\<^sub>R u)"
  4675         using norm_triangle_ineq by blast
  4676       also have "\<dots> = norm y + abs(1 - norm y / r) * norm u"
  4677         by simp
  4678       also have "\<dots> \<le> r"
  4679       proof -
  4680         have "(r - norm u) * (r - norm y) \<ge> 0"
  4681           using that by auto
  4682         then have "r * norm u + r * norm y \<le> r * r + norm u * norm y"
  4683           by (simp add: algebra_simps)
  4684         then show ?thesis
  4685         using that \<open>0 < r\<close> by (simp add: abs_if field_simps)
  4686       qed
  4687       finally show ?thesis .
  4688     qed
  4689     have "f ` (cball a r) \<subseteq> cball a r"
  4690       apply (clarsimp simp add: dist_norm norm_minus_commute f_def)
  4691       using * by (metis diff_add_eq diff_diff_add diff_diff_eq2 norm_minus_commute nou)
  4692     moreover have "f ` T \<subseteq> T"
  4693       unfolding f_def using \<open>affine T\<close> \<open>a \<in> T\<close> \<open>u \<in> T\<close>
  4694       by (force simp: add.commute mem_affine_3_minus)
  4695     ultimately show "f ` (cball a r \<inter> T) \<subseteq> cball a r \<inter> T"
  4696       by blast
  4697   next
  4698     show "cball a r \<inter> T \<subseteq> f ` (cball a r \<inter> T)"
  4699     proof (clarsimp simp add: dist_norm norm_minus_commute)
  4700       fix x
  4701       assume x: "norm (x - a) \<le> r" and "x \<in> T"
  4702       have "\<exists>v \<in> {0..1}. ((1 - v) * r - norm ((x - a) - v *\<^sub>R (u - a))) \<bullet> 1 = 0"
  4703         by (rule ivt_decreasing_component_on_1) (auto simp: x continuous_intros)
  4704       then obtain v where "0\<le>v" "v\<le>1" and v: "(1 - v) * r = norm ((x - a) - v *\<^sub>R (u - a))"
  4705         by auto
  4706       show "x \<in> f ` (cball a r \<inter> T)"
  4707       proof (rule image_eqI)
  4708         show "x = f (x - v *\<^sub>R (u - a))"
  4709           using \<open>r > 0\<close> v by (simp add: f_def field_simps)
  4710         have "x - v *\<^sub>R (u - a) \<in> cball a r"
  4711           using \<open>r > 0\<close> v \<open>0 \<le> v\<close>
  4712           apply (simp add: field_simps dist_norm norm_minus_commute)
  4713           by (metis le_add_same_cancel2 order.order_iff_strict zero_le_mult_iff)
  4714         moreover have "x - v *\<^sub>R (u - a) \<in> T"
  4715           by (simp add: f_def \<open>affine T\<close> \<open>u \<in> T\<close> \<open>x \<in> T\<close> assms mem_affine_3_minus2)
  4716         ultimately show "x - v *\<^sub>R (u - a) \<in> cball a r \<inter> T"
  4717           by blast
  4718       qed
  4719     qed
  4720   qed
  4721   have "\<exists>g. homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f g"
  4722     apply (rule homeomorphism_compact [OF _ contf fim inj_onf])
  4723     apply (simp add: affine_closed compact_Int_closed \<open>affine T\<close>)
  4724     done
  4725   then show ?thesis
  4726     apply (rule exE)
  4727     apply (erule_tac f=f in that)
  4728     using \<open>r > 0\<close>
  4729      apply (simp_all add: f_def dist_norm norm_minus_commute)
  4730     done
  4731 qed
  4732 
  4733 corollary%unimportant homeomorphism_moving_point_2:
  4734   fixes a :: "'a::euclidean_space"
  4735   assumes "affine T" "a \<in> T" and u: "u \<in> ball a r \<inter> T" and v: "v \<in> ball a r \<inter> T"
  4736   obtains f g where "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f g"
  4737                     "f u = v" "\<And>x. \<lbrakk>x \<in> sphere a r; x \<in> T\<rbrakk> \<Longrightarrow> f x = x"
  4738 proof -
  4739   have "0 < r"
  4740     by (metis DiffD1 Diff_Diff_Int ball_eq_empty centre_in_ball not_le u)
  4741   obtain f1 g1 where hom1: "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f1 g1"
  4742                  and "f1 a = u" and f1: "\<And>x. x \<in> sphere a r \<Longrightarrow> f1 x = x"
  4743     using homeomorphism_moving_point_1 [OF \<open>affine T\<close> \<open>a \<in> T\<close> u] by blast
  4744   obtain f2 g2 where hom2: "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f2 g2"
  4745                  and "f2 a = v" and f2: "\<And>x. x \<in> sphere a r \<Longrightarrow> f2 x = x"
  4746     using homeomorphism_moving_point_1 [OF \<open>affine T\<close> \<open>a \<in> T\<close> v] by blast
  4747   show ?thesis
  4748   proof
  4749     show "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) (f2 \<circ> g1) (f1 \<circ> g2)"
  4750       by (metis homeomorphism_compose homeomorphism_symD hom1 hom2)
  4751     have "g1 u = a"
  4752       using \<open>0 < r\<close> \<open>f1 a = u\<close> assms hom1 homeomorphism_apply1 by fastforce
  4753     then show "(f2 \<circ> g1) u = v"
  4754       by (simp add: \<open>f2 a = v\<close>)
  4755     show "\<And>x. \<lbrakk>x \<in> sphere a r; x \<in> T\<rbrakk> \<Longrightarrow> (f2 \<circ> g1) x = x"
  4756       using f1 f2 hom1 homeomorphism_apply1 by fastforce
  4757   qed
  4758 qed
  4759 
  4760 
  4761 corollary%unimportant homeomorphism_moving_point_3:
  4762   fixes a :: "'a::euclidean_space"
  4763   assumes "affine T" "a \<in> T" and ST: "ball a r \<inter> T \<subseteq> S" "S \<subseteq> T"
  4764       and u: "u \<in> ball a r \<inter> T" and v: "v \<in> ball a r \<inter> T"
  4765   obtains f g where "homeomorphism S S f g"
  4766                     "f u = v" "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> ball a r \<inter> T"
  4767 proof -
  4768   obtain f g where hom: "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f g"
  4769                and "f u = v" and fid: "\<And>x. \<lbrakk>x \<in> sphere a r; x \<in> T\<rbrakk> \<Longrightarrow> f x = x"
  4770     using homeomorphism_moving_point_2 [OF \<open>affine T\<close> \<open>a \<in> T\<close> u v] by blast
  4771   have gid: "\<And>x. \<lbrakk>x \<in> sphere a r; x \<in> T\<rbrakk> \<Longrightarrow> g x = x"
  4772     using fid hom homeomorphism_apply1 by fastforce
  4773   define ff where "ff \<equiv> \<lambda>x. if x \<in> ball a r \<inter> T then f x else x"
  4774   define gg where "gg \<equiv> \<lambda>x. if x \<in> ball a r \<inter> T then g x else x"
  4775   show ?thesis
  4776   proof
  4777     show "homeomorphism S S ff gg"
  4778     proof (rule homeomorphismI)
  4779       have "continuous_on ((cball a r \<inter> T) \<union> (T - ball a r)) ff"
  4780         apply (simp add: ff_def)
  4781         apply (rule continuous_on_cases)
  4782         using homeomorphism_cont1 [OF hom]
  4783             apply (auto simp: affine_closed \<open>affine T\<close> continuous_on_id fid)
  4784         done
  4785       then show "continuous_on S ff"
  4786         apply (rule continuous_on_subset)
  4787         using ST by auto
  4788       have "continuous_on ((cball a r \<inter> T) \<union> (T - ball a r)) gg"
  4789         apply (simp add: gg_def)
  4790         apply (rule continuous_on_cases)
  4791         using homeomorphism_cont2 [OF hom]
  4792             apply (auto simp: affine_closed \<open>affine T\<close> continuous_on_id gid)
  4793         done
  4794       then show "continuous_on S gg"
  4795         apply (rule continuous_on_subset)
  4796         using ST by auto
  4797       show "ff ` S \<subseteq> S"
  4798       proof (clarsimp simp add: ff_def)
  4799         fix x
  4800         assume "x \<in> S" and x: "dist a x < r" and "x \<in> T"
  4801         then have "f x \<in> cball a r \<inter> T"
  4802           using homeomorphism_image1 [OF hom] by force
  4803         then show "f x \<in> S"
  4804           using ST(1) \<open>x \<in> T\<close> gid hom homeomorphism_def x by fastforce
  4805       qed
  4806       show "gg ` S \<subseteq> S"
  4807       proof (clarsimp simp add: gg_def)
  4808         fix x
  4809         assume "x \<in> S" and x: "dist a x < r" and "x \<in> T"
  4810         then have "g x \<in> cball a r \<inter> T"
  4811           using homeomorphism_image2 [OF hom] by force
  4812         then have "g x \<in> ball a r"
  4813           using homeomorphism_apply2 [OF hom]
  4814             by (metis Diff_Diff_Int Diff_iff  \<open>x \<in> T\<close> cball_def fid le_less mem_Collect_eq mem_ball mem_sphere x)
  4815         then show "g x \<in> S"
  4816           using ST(1) \<open>g x \<in> cball a r \<inter> T\<close> by force
  4817         qed
  4818       show "\<And>x. x \<in> S \<Longrightarrow> gg (ff x) = x"
  4819         apply (simp add: ff_def gg_def)
  4820         using homeomorphism_apply1 [OF hom] homeomorphism_image1 [OF hom]
  4821         apply auto
  4822         apply (metis Int_iff homeomorphism_apply1 [OF hom] fid image_eqI less_eq_real_def mem_cball mem_sphere)
  4823         done
  4824       show "\<And>x. x \<in> S \<Longrightarrow> ff (gg x) = x"
  4825         apply (simp add: ff_def gg_def)
  4826         using homeomorphism_apply2 [OF hom] homeomorphism_image2 [OF hom]
  4827         apply auto
  4828         apply (metis Int_iff fid image_eqI less_eq_real_def mem_cball mem_sphere)
  4829         done
  4830     qed
  4831     show "ff u = v"
  4832       using u by (auto simp: ff_def \<open>f u = v\<close>)
  4833     show "{x. \<not> (ff x = x \<and> gg x = x)} \<subseteq> ball a r \<inter> T"
  4834       by (auto simp: ff_def gg_def)
  4835   qed
  4836 qed
  4837 
  4838 
  4839 proposition%unimportant homeomorphism_moving_point:
  4840   fixes a :: "'a::euclidean_space"
  4841   assumes ope: "openin (top_of_set (affine hull S)) S"
  4842       and "S \<subseteq> T"
  4843       and TS: "T \<subseteq> affine hull S"
  4844       and S: "connected S" "a \<in> S" "b \<in> S"
  4845   obtains f g where "homeomorphism T T f g" "f a = b"
  4846                     "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S"
  4847                     "bounded {x. \<not> (f x = x \<and> g x = x)}"
  4848 proof -
  4849   have 1: "\<exists>h k. homeomorphism T T h k \<and> h (f d) = d \<and>
  4850               {x. \<not> (h x = x \<and> k x = x)} \<subseteq> S \<and> bounded {x. \<not> (h x = x \<and> k x = x)}"
  4851         if "d \<in> S" "f d \<in> S" and homfg: "homeomorphism T T f g"
  4852         and S: "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S"
  4853         and bo: "bounded {x. \<not> (f x = x \<and> g x = x)}" for d f g
  4854   proof (intro exI conjI)
  4855     show homgf: "homeomorphism T T g f"
  4856       by (metis homeomorphism_symD homfg)
  4857     then show "g (f d) = d"
  4858       by (meson \<open>S \<subseteq> T\<close> homeomorphism_def subsetD \<open>d \<in> S\<close>)
  4859     show "{x. \<not> (g x = x \<and> f x = x)} \<subseteq> S"
  4860       using S by blast
  4861     show "bounded {x. \<not> (g x = x \<and> f x = x)}"
  4862       using bo by (simp add: conj_commute)
  4863   qed
  4864   have 2: "\<exists>f g. homeomorphism T T f g \<and> f x = f2 (f1 x) \<and>
  4865                  {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
  4866              if "x \<in> S" "f1 x \<in> S" "f2 (f1 x) \<in> S"
  4867                 and hom: "homeomorphism T T f1 g1" "homeomorphism T T f2 g2"
  4868                 and sub: "{x. \<not> (f1 x = x \<and> g1 x = x)} \<subseteq> S"   "{x. \<not> (f2 x = x \<and> g2 x = x)} \<subseteq> S"
  4869                 and bo: "bounded {x. \<not> (f1 x = x \<and> g1 x = x)}"  "bounded {x. \<not> (f2 x = x \<and> g2 x = x)}"
  4870              for x f1 f2 g1 g2
  4871   proof (intro exI conjI)
  4872     show homgf: "homeomorphism T T (f2 \<circ> f1) (g1 \<circ> g2)"
  4873       by (metis homeomorphism_compose hom)
  4874     then show "(f2 \<circ> f1) x = f2 (f1 x)"
  4875       by force
  4876     show "{x. \<not> ((f2 \<circ> f1) x = x \<and> (g1 \<circ> g2) x = x)} \<subseteq> S"
  4877       using sub by force
  4878     have "bounded ({x. \<not>(f1 x = x \<and> g1 x = x)} \<union> {x. \<not>(f2 x = x \<and> g2 x = x)})"
  4879       using bo by simp
  4880     then show "bounded {x. \<not> ((f2 \<circ> f1) x = x \<and> (g1 \<circ> g2) x = x)}"
  4881       by (rule bounded_subset) auto
  4882   qed
  4883   have 3: "\<exists>U. openin (top_of_set S) U \<and>
  4884               d \<in> U \<and>
  4885               (\<forall>x\<in>U.
  4886                   \<exists>f g. homeomorphism T T f g \<and> f d = x \<and>
  4887                         {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and>
  4888                         bounded {x. \<not> (f x = x \<and> g x = x)})"
  4889            if "d \<in> S" for d
  4890   proof -
  4891     obtain r where "r > 0" and r: "ball d r \<inter> affine hull S \<subseteq> S"
  4892       by (metis \<open>d \<in> S\<close> ope openin_contains_ball)
  4893     have *: "\<exists>f g. homeomorphism T T f g \<and> f d = e \<and>
  4894                    {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and>
  4895                    bounded {x. \<not> (f x = x \<and> g x = x)}" if "e \<in> S" "e \<in> ball d r" for e
  4896       apply (rule homeomorphism_moving_point_3 [of "affine hull S" d r T d e])
  4897       using r \<open>S \<subseteq> T\<close> TS that
  4898             apply (auto simp: \<open>d \<in> S\<close> \<open>0 < r\<close> hull_inc)
  4899       using bounded_subset by blast
  4900     show ?thesis
  4901       apply (rule_tac x="S \<inter> ball d r" in exI)
  4902       apply (intro conjI)
  4903         apply (simp add: openin_open_Int)
  4904        apply (simp add: \<open>0 < r\<close> that)
  4905       apply (blast intro: *)
  4906       done
  4907   qed
  4908   have "\<exists>f g. homeomorphism T T f g \<and> f a = b \<and>
  4909               {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
  4910     apply (rule connected_equivalence_relation [OF S], safe)
  4911       apply (blast intro: 1 2 3)+
  4912     done
  4913   then show ?thesis
  4914     using that by auto
  4915 qed
  4916 
  4917 
  4918 lemma homeomorphism_moving_points_exists_gen:
  4919   assumes K: "finite K" "\<And>i. i \<in> K \<Longrightarrow> x i \<in> S \<and> y i \<in> S"
  4920              "pairwise (\<lambda>i j. (x i \<noteq> x j) \<and> (y i \<noteq> y j)) K"
  4921       and "2 \<le> aff_dim S"
  4922       and ope: "openin (top_of_set (affine hull S)) S"
  4923       and "S \<subseteq> T" "T \<subseteq> affine hull S" "connected S"
  4924   shows "\<exists>f g. homeomorphism T T f g \<and> (\<forall>i \<in> K. f(x i) = y i) \<and>
  4925                {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
  4926   using assms
  4927 proof (induction K)
  4928   case empty
  4929   then show ?case
  4930     by (force simp: homeomorphism_ident)
  4931 next
  4932   case (insert i K)
  4933   then have xney: "\<And>j. \<lbrakk>j \<in> K; j \<noteq> i\<rbrakk> \<Longrightarrow> x i \<noteq> x j \<and> y i \<noteq> y j"
  4934        and pw: "pairwise (\<lambda>i j. x i \<noteq> x j \<and> y i \<noteq> y j) K"
  4935        and "x i \<in> S" "y i \<in> S"
  4936        and xyS: "\<And>i. i \<in> K \<Longrightarrow> x i \<in> S \<and> y i \<in> S"
  4937     by (simp_all add: pairwise_insert)
  4938   obtain f g where homfg: "homeomorphism T T f g" and feq: "\<And>i. i \<in> K \<Longrightarrow> f(x i) = y i"
  4939                and fg_sub: "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S"
  4940                and bo_fg: "bounded {x. \<not> (f x = x \<and> g x = x)}"
  4941     using insert.IH [OF xyS pw] insert.prems by (blast intro: that)
  4942   then have "\<exists>f g. homeomorphism T T f g \<and> (\<forall>i \<in> K. f(x i) = y i) \<and>
  4943                    {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
  4944     using insert by blast
  4945   have aff_eq: "affine hull (S - y ` K) = affine hull S"
  4946     apply (rule affine_hull_Diff)
  4947     apply (auto simp: insert)
  4948     using \<open>y i \<in> S\<close> insert.hyps(2) xney xyS by fastforce
  4949   have f_in_S: "f x \<in> S" if "x \<in> S" for x
  4950     using homfg fg_sub homeomorphism_apply1 \<open>S \<subseteq> T\<close>
  4951   proof -
  4952     have "(f (f x) \<noteq> f x \<or> g (f x) \<noteq> f x) \<or> f x \<in> S"
  4953       by (metis \<open>S \<subseteq> T\<close> homfg subsetD homeomorphism_apply1 that)
  4954     then show ?thesis
  4955       using fg_sub by force
  4956   qed
  4957   obtain h k where homhk: "homeomorphism T T h k" and heq: "h (f (x i)) = y i"
  4958                and hk_sub: "{x. \<not> (h x = x \<and> k x = x)} \<subseteq> S - y ` K"
  4959                and bo_hk:  "bounded {x. \<not> (h x = x \<and> k x = x)}"
  4960   proof (rule homeomorphism_moving_point [of "S - y`K" T "f(x i)" "y i"])
  4961     show "openin (top_of_set (affine hull (S - y ` K))) (S - y ` K)"
  4962       by (simp add: aff_eq openin_diff finite_imp_closedin image_subset_iff hull_inc insert xyS)
  4963     show "S - y ` K \<subseteq> T"
  4964       using \<open>S \<subseteq> T\<close> by auto
  4965     show "T \<subseteq> affine hull (S - y ` K)"
  4966       using insert by (simp add: aff_eq)
  4967     show "connected (S - y ` K)"
  4968     proof (rule connected_openin_diff_countable [OF \<open>connected S\<close> ope])
  4969       show "\<not> collinear S"
  4970         using collinear_aff_dim \<open>2 \<le> aff_dim S\<close> by force
  4971       show "countable (y ` K)"
  4972         using countable_finite insert.hyps(1) by blast
  4973     qed
  4974     show "f (x i) \<in> S - y ` K"
  4975       apply (auto simp: f_in_S \<open>x i \<in> S\<close>)
  4976         by (metis feq homfg \<open>x i \<in> S\<close> homeomorphism_def \<open>S \<subseteq> T\<close> \<open>i \<notin> K\<close> subsetCE xney xyS)
  4977     show "y i \<in> S - y ` K"
  4978       using insert.hyps xney by (auto simp: \<open>y i \<in> S\<close>)
  4979   qed blast
  4980   show ?case
  4981   proof (intro exI conjI)
  4982     show "homeomorphism T T (h \<circ> f) (g \<circ> k)"
  4983       using homfg homhk homeomorphism_compose by blast
  4984     show "\<forall>i \<in> insert i K. (h \<circ> f) (x i) = y i"
  4985       using feq hk_sub by (auto simp: heq)
  4986     show "{x. \<not> ((h \<circ> f) x = x \<and> (g \<circ> k) x = x)} \<subseteq> S"
  4987       using fg_sub hk_sub by force
  4988     have "bounded ({x. \<not>(f x = x \<and> g x = x)} \<union> {x. \<not>(h x = x \<and> k x = x)})"
  4989       using bo_fg bo_hk bounded_Un by blast
  4990     then show "bounded {x. \<not> ((h \<circ> f) x = x \<and> (g \<circ> k) x = x)}"
  4991       by (rule bounded_subset) auto
  4992   qed
  4993 qed
  4994 
  4995 proposition%unimportant homeomorphism_moving_points_exists:
  4996   fixes S :: "'a::euclidean_space set"
  4997   assumes 2: "2 \<le> DIM('a)" "open S" "connected S" "S \<subseteq> T" "finite K"
  4998       and KS: "\<And>i. i \<in> K \<Longrightarrow> x i \<in> S \<and> y i \<in> S"
  4999       and pw: "pairwise (\<lambda>i j. (x i \<noteq> x j) \<and> (y i \<noteq> y j)) K"
  5000       and S: "S \<subseteq> T" "T \<subseteq> affine hull S" "connected S"
  5001   obtains f g where "homeomorphism T T f g" "\<And>i. i \<in> K \<Longrightarrow> f(x i) = y i"
  5002                     "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S" "bounded {x. (\<not> (f x = x \<and> g x = x))}"
  5003 proof (cases "S = {}")
  5004   case True
  5005   then show ?thesis
  5006     using KS homeomorphism_ident that by fastforce
  5007 next
  5008   case False
  5009   then have affS: "affine hull S = UNIV"
  5010     by (simp add: affine_hull_open \<open>open S\<close>)
  5011   then have ope: "openin (top_of_set (affine hull S)) S"
  5012     using \<open>open S\<close> open_openin by auto
  5013   have "2 \<le> DIM('a)" by (rule 2)
  5014   also have "\<dots> = aff_dim (UNIV :: 'a set)"
  5015     by simp
  5016   also have "\<dots> \<le> aff_dim S"
  5017     by (metis aff_dim_UNIV aff_dim_affine_hull aff_dim_le_DIM affS)
  5018   finally have "2 \<le> aff_dim S"
  5019     by linarith
  5020   then show ?thesis
  5021     using homeomorphism_moving_points_exists_gen [OF \<open>finite K\<close> KS pw _ ope S] that by fastforce
  5022 qed
  5023 
  5024 subsubsection%unimportant\<open>The theorem \<open>homeomorphism_grouping_points_exists\<close>\<close>
  5025 
  5026 lemma homeomorphism_grouping_point_1:
  5027   fixes a::real and c::real
  5028   assumes "a < b" "c < d"
  5029   obtains f g where "homeomorphism (cbox a b) (cbox c d) f g" "f a = c" "f b = d"
  5030 proof -
  5031   define f where "f \<equiv> \<lambda>x. ((d - c) / (b - a)) * x + (c - a * ((d - c) / (b - a)))"
  5032   have "\<exists>g. homeomorphism (cbox a b) (cbox c d) f g"
  5033   proof (rule homeomorphism_compact)
  5034     show "continuous_on (cbox a b) f"
  5035       apply (simp add: f_def)
  5036       apply (intro continuous_intros)
  5037       using assms by auto
  5038     have "f ` {a..b} = {c..d}"
  5039       unfolding f_def image_affinity_atLeastAtMost
  5040       using assms sum_sqs_eq by (auto simp: divide_simps algebra_simps)
  5041     then show "f ` cbox a b = cbox c d"
  5042       by auto
  5043     show "inj_on f (cbox a b)"
  5044       unfolding f_def inj_on_def using assms by auto
  5045   qed auto
  5046   then obtain g where "homeomorphism (cbox a b) (cbox c d) f g" ..
  5047   then show ?thesis
  5048   proof
  5049     show "f a = c"
  5050       by (simp add: f_def)
  5051     show "f b = d"
  5052       using assms sum_sqs_eq [of a b] by (auto simp: f_def divide_simps algebra_simps)
  5053   qed
  5054 qed
  5055 
  5056 lemma homeomorphism_grouping_point_2:
  5057   fixes a::real and w::real
  5058   assumes hom_ab: "homeomorphism (cbox a b) (cbox u v) f1 g1"
  5059       and hom_bc: "homeomorphism (cbox b c) (cbox v w) f2 g2"
  5060       and "b \<in> cbox a c" "v \<in> cbox u w"
  5061       and eq: "f1 a = u" "f1 b = v" "f2 b = v" "f2 c = w"
  5062  obtains f g where "homeomorphism (cbox a c) (cbox u w) f g" "f a = u" "f c = w"
  5063                    "\<And>x. x \<in> cbox a b \<Longrightarrow> f x = f1 x" "\<And>x. x \<in> cbox b c \<Longrightarrow> f x = f2 x"
  5064 proof -
  5065   have le: "a \<le> b" "b \<le> c" "u \<le> v" "v \<le> w"
  5066     using assms by simp_all
  5067   then have ac: "cbox a c = cbox a b \<union> cbox b c" and uw: "cbox u w = cbox u v \<union> cbox v w"
  5068     by auto
  5069   define f where "f \<equiv> \<lambda>x. if x \<le> b then f1 x else f2 x"
  5070   have "\<exists>g. homeomorphism (cbox a c) (cbox u w) f g"
  5071   proof (rule homeomorphism_compact)
  5072     have cf1: "continuous_on (cbox a b) f1"
  5073       using hom_ab homeomorphism_cont1 by blast
  5074     have cf2: "continuous_on (cbox b c) f2"
  5075       using hom_bc homeomorphism_cont1 by blast
  5076     show "continuous_on (cbox a c) f"
  5077       apply (simp add: f_def)
  5078       apply (rule continuous_on_cases_le [OF continuous_on_subset [OF cf1] continuous_on_subset [OF cf2]])
  5079       using le eq apply (force simp: continuous_on_id)+
  5080       done
  5081     have "f ` cbox a b = f1 ` cbox a b" "f ` cbox b c = f2 ` cbox b c"
  5082       unfolding f_def using eq by force+
  5083     then show "f ` cbox a c = cbox u w"
  5084       apply (simp only: ac uw image_Un)
  5085       by (metis hom_ab hom_bc homeomorphism_def)
  5086     have neq12: "f1 x \<noteq> f2 y" if x: "a \<le> x" "x \<le> b" and y: "b < y" "y \<le> c" for x y
  5087     proof -
  5088       have "f1 x \<in> cbox u v"
  5089         by (metis hom_ab homeomorphism_def image_eqI mem_box_real(2) x)
  5090       moreover have "f2 y \<in> cbox v w"
  5091         by (metis (full_types) hom_bc homeomorphism_def image_subset_iff mem_box_real(2) not_le not_less_iff_gr_or_eq order_refl y)
  5092       moreover have "f2 y \<noteq> f2 b"
  5093         by (metis cancel_comm_monoid_add_class.diff_cancel diff_gt_0_iff_gt hom_bc homeomorphism_def le(2) less_imp_le less_numeral_extra(3) mem_box_real(2) order_refl y)
  5094       ultimately show ?thesis
  5095         using le eq by simp
  5096     qed
  5097     have "inj_on f1 (cbox a b)"
  5098       by (metis (full_types) hom_ab homeomorphism_def inj_onI)
  5099     moreover have "inj_on f2 (cbox b c)"
  5100       by (metis (full_types) hom_bc homeomorphism_def inj_onI)
  5101     ultimately show "inj_on f (cbox a c)"
  5102       apply (simp (no_asm) add: inj_on_def)
  5103       apply (simp add: f_def inj_on_eq_iff)
  5104       using neq12  apply force
  5105       done
  5106   qed auto
  5107   then obtain g where "homeomorphism (cbox a c) (cbox u w) f g" ..
  5108   then show ?thesis
  5109     apply (rule that)
  5110     using eq le by (auto simp: f_def)
  5111 qed
  5112 
  5113 lemma homeomorphism_grouping_point_3:
  5114   fixes a::real
  5115   assumes cbox_sub: "cbox c d \<subseteq> box a b" "cbox u v \<subseteq> box a b"
  5116       and box_ne: "box c d \<noteq> {}" "box u v \<noteq> {}"
  5117   obtains f g where "homeomorphism (cbox a b) (cbox a b) f g" "f a = a" "f b = b"
  5118                     "\<And>x. x \<in> cbox c d \<Longrightarrow> f x \<in> cbox u v"
  5119 proof -
  5120   have less: "a < c" "a < u" "d < b" "v < b" "c < d" "u < v" "cbox c d \<noteq> {}"
  5121     using assms
  5122     by (simp_all add: cbox_sub subset_eq)
  5123   obtain f1 g1 where 1: "homeomorphism (cbox a c) (cbox a u) f1 g1"
  5124                    and f1_eq: "f1 a = a" "f1 c = u"
  5125     using homeomorphism_grouping_point_1 [OF \<open>a < c\<close> \<open>a < u\<close>] .
  5126   obtain f2 g2 where 2: "homeomorphism (cbox c d) (cbox u v) f2 g2"
  5127                    and f2_eq: "f2 c = u" "f2 d = v"
  5128     using homeomorphism_grouping_point_1 [OF \<open>c < d\<close> \<open>u < v\<close>] .
  5129   obtain f3 g3 where 3: "homeomorphism (cbox d b) (cbox v b) f3 g3"
  5130                    and f3_eq: "f3 d = v" "f3 b = b"
  5131     using homeomorphism_grouping_point_1 [OF \<open>d < b\<close> \<open>v < b\<close>] .
  5132   obtain f4 g4 where 4: "homeomorphism (cbox a d) (cbox a v) f4 g4" and "f4 a = a" "f4 d = v"
  5133                  and f4_eq: "\<And>x. x \<in> cbox a c \<Longrightarrow> f4 x = f1 x" "\<And>x. x \<in> cbox c d \<Longrightarrow> f4 x = f2 x"
  5134     using homeomorphism_grouping_point_2 [OF 1 2] less  by (auto simp: f1_eq f2_eq)
  5135   obtain f g where fg: "homeomorphism (cbox a b) (cbox a b) f g" "f a = a" "f b = b"
  5136                and f_eq: "\<And>x. x \<in> cbox a d \<Longrightarrow> f x = f4 x" "\<And>x. x \<in> cbox d b \<Longrightarrow> f x = f3 x"
  5137     using homeomorphism_grouping_point_2 [OF 4 3] less by (auto simp: f4_eq f3_eq f2_eq f1_eq)
  5138   show ?thesis
  5139     apply (rule that [OF fg])
  5140     using f4_eq f_eq homeomorphism_image1 [OF 2]
  5141     apply simp
  5142     by (metis atLeastAtMost_iff box_real(1) box_real(2) cbox_sub(1) greaterThanLessThan_iff imageI less_eq_real_def subset_eq)
  5143 qed
  5144 
  5145 
  5146 lemma homeomorphism_grouping_point_4:
  5147   fixes T :: "real set"
  5148   assumes "open U" "open S" "connected S" "U \<noteq> {}" "finite K" "K \<subseteq> S" "U \<subseteq> S" "S \<subseteq> T"
  5149   obtains f g where "homeomorphism T T f g"
  5150                     "\<And>x. x \<in> K \<Longrightarrow> f x \<in> U" "{x. (\<not> (f x = x \<and> g x = x))} \<subseteq> S"
  5151                     "bounded {x. (\<not> (f x = x \<and> g x = x))}"
  5152 proof -
  5153   obtain c d where "box c d \<noteq> {}" "cbox c d \<subseteq> U"
  5154   proof -
  5155     obtain u where "u \<in> U"
  5156       using \<open>U \<noteq> {}\<close> by blast
  5157     then obtain e where "e > 0" "cball u e \<subseteq> U"
  5158       using \<open>open U\<close> open_contains_cball by blast
  5159     then show ?thesis
  5160       by (rule_tac c=u and d="u+e" in that) (auto simp: dist_norm subset_iff)
  5161   qed
  5162   have "compact K"
  5163     by (simp add: \<open>finite K\<close> finite_imp_compact)
  5164   obtain a b where "box a b \<noteq> {}" "K \<subseteq> cbox a b" "cbox a b \<subseteq> S"
  5165   proof (cases "K = {}")
  5166     case True then show ?thesis
  5167       using \<open>box c d \<noteq> {}\<close> \<open>cbox c d \<subseteq> U\<close> \<open>U \<subseteq> S\<close> that by blast
  5168   next
  5169     case False
  5170     then obtain a b where "a \<in> K" "b \<in> K"
  5171             and a: "\<And>x. x \<in> K \<Longrightarrow> a \<le> x" and b: "\<And>x. x \<in> K \<Longrightarrow> x \<le> b"
  5172       using compact_attains_inf compact_attains_sup by (metis \<open>compact K\<close>)+
  5173     obtain e where "e > 0" "cball b e \<subseteq> S"
  5174       using \<open>open S\<close> open_contains_cball
  5175       by (metis \<open>b \<in> K\<close> \<open>K \<subseteq> S\<close> subsetD)
  5176     show ?thesis
  5177     proof
  5178       show "box a (b + e) \<noteq> {}"
  5179         using \<open>0 < e\<close> \<open>b \<in> K\<close> a by force
  5180       show "K \<subseteq> cbox a (b + e)"
  5181         using \<open>0 < e\<close> a b by fastforce
  5182       have "a \<in> S"
  5183         using \<open>a \<in> K\<close> assms(6) by blast
  5184       have "b + e \<in> S"
  5185         using \<open>0 < e\<close> \<open>cball b e \<subseteq> S\<close>  by (force simp: dist_norm)
  5186       show "cbox a (b + e) \<subseteq> S"
  5187         using \<open>a \<in> S\<close> \<open>b + e \<in> S\<close> \<open>connected S\<close> connected_contains_Icc by auto
  5188     qed
  5189   qed
  5190   obtain w z where "cbox w z \<subseteq> S" and sub_wz: "cbox a b \<union> cbox c d \<subseteq> box w z"
  5191   proof -
  5192     have "a \<in> S" "b \<in> S"
  5193       using \<open>box a b \<noteq> {}\<close> \<open>cbox a b \<subseteq> S\<close> by auto
  5194     moreover have "c \<in> S" "d \<in> S"
  5195       using \<open>box c d \<noteq> {}\<close> \<open>cbox c d \<subseteq> U\<close> \<open>U \<subseteq> S\<close> by force+
  5196     ultimately have "min a c \<in> S" "max b d \<in> S"
  5197       by linarith+
  5198     then obtain e1 e2 where "e1 > 0" "cball (min a c) e1 \<subseteq> S" "e2 > 0" "cball (max b d) e2 \<subseteq> S"
  5199       using \<open>open S\<close> open_contains_cball by metis
  5200     then have *: "min a c - e1 \<in> S" "max b d + e2 \<in> S"
  5201       by (auto simp: dist_norm)
  5202     show ?thesis
  5203     proof
  5204       show "cbox (min a c - e1) (max b d+ e2) \<subseteq> S"
  5205         using * \<open>connected S\<close> connected_contains_Icc by auto
  5206       show "cbox a b \<union> cbox c d \<subseteq> box (min a c - e1) (max b d + e2)"
  5207         using \<open>0 < e1\<close> \<open>0 < e2\<close> by auto
  5208     qed
  5209   qed
  5210   then
  5211   obtain f g where hom: "homeomorphism (cbox w z) (cbox w z) f g"
  5212                and "f w = w" "f z = z"
  5213                and fin: "\<And>x. x \<in> cbox a b \<Longrightarrow> f x \<in> cbox c d"
  5214     using homeomorphism_grouping_point_3 [of a b w z c d]
  5215     using \<open>box a b \<noteq> {}\<close> \<open>box c d \<noteq> {}\<close> by blast
  5216   have contfg: "continuous_on (cbox w z) f" "continuous_on (cbox w z) g"
  5217     using hom homeomorphism_def by blast+
  5218   define f' where "f' \<equiv> \<lambda>x. if x \<in> cbox w z then f x else x"
  5219   define g' where "g' \<equiv> \<lambda>x. if x \<in> cbox w z then g x else x"
  5220   show ?thesis
  5221   proof
  5222     have T: "cbox w z \<union> (T - box w z) = T"
  5223       using \<open>cbox w z \<subseteq> S\<close> \<open>S \<subseteq> T\<close> by auto
  5224     show "homeomorphism T T f' g'"
  5225     proof
  5226       have clo: "closedin (top_of_set (cbox w z \<union> (T - box w z))) (T - box w z)"
  5227         by (metis Diff_Diff_Int Diff_subset T closedin_def open_box openin_open_Int topspace_euclidean_subtopology)
  5228       have "continuous_on (cbox w z \<union> (T - box w z)) f'" "continuous_on (cbox w z \<union> (T - box w z)) g'"
  5229         unfolding f'_def g'_def
  5230          apply (safe intro!: continuous_on_cases_local contfg continuous_on_id clo)
  5231          apply (simp_all add: closed_subset)
  5232         using \<open>f w = w\<close> \<open>f z = z\<close> apply force
  5233         by (metis \<open>f w = w\<close> \<open>f z = z\<close> hom homeomorphism_def less_eq_real_def mem_box_real(2))
  5234       then show "continuous_on T f'" "continuous_on T g'"
  5235         by (simp_all only: T)
  5236       show "f' ` T \<subseteq> T"
  5237         unfolding f'_def
  5238         by clarsimp (metis \<open>cbox w z \<subseteq> S\<close> \<open>S \<subseteq> T\<close> subsetD hom homeomorphism_def imageI mem_box_real(2))
  5239       show "g' ` T \<subseteq> T"
  5240         unfolding g'_def
  5241         by clarsimp (metis \<open>cbox w z \<subseteq> S\<close> \<open>S \<subseteq> T\<close> subsetD hom homeomorphism_def imageI mem_box_real(2))
  5242       show "\<And>x. x \<in> T \<Longrightarrow> g' (f' x) = x"
  5243         unfolding f'_def g'_def
  5244         using homeomorphism_apply1 [OF hom]  homeomorphism_image1 [OF hom] by fastforce
  5245       show "\<And>y. y \<in> T \<Longrightarrow> f' (g' y) = y"
  5246         unfolding f'_def g'_def
  5247         using homeomorphism_apply2 [OF hom]  homeomorphism_image2 [OF hom] by fastforce
  5248     qed
  5249     show "\<And>x. x \<in> K \<Longrightarrow> f' x \<in> U"
  5250       using fin sub_wz \<open>K \<subseteq> cbox a b\<close> \<open>cbox c d \<subseteq> U\<close> by (force simp: f'_def)
  5251     show "{x. \<not> (f' x = x \<and> g' x = x)} \<subseteq> S"
  5252       using \<open>cbox w z \<subseteq> S\<close> by (auto simp: f'_def g'_def)
  5253     show "bounded {x. \<not> (f' x = x \<and> g' x = x)}"
  5254       apply (rule bounded_subset [of "cbox w z"])
  5255       using bounded_cbox apply blast
  5256       apply (auto simp: f'_def g'_def)
  5257       done
  5258   qed
  5259 qed
  5260 
  5261 proposition%unimportant homeomorphism_grouping_points_exists:
  5262   fixes S :: "'a::euclidean_space set"
  5263   assumes "open U" "open S" "connected S" "U \<noteq> {}" "finite K" "K \<subseteq> S" "U \<subseteq> S" "S \<subseteq> T"
  5264   obtains f g where "homeomorphism T T f g" "{x. (\<not> (f x = x \<and> g x = x))} \<subseteq> S"
  5265                     "bounded {x. (\<not> (f x = x \<and> g x = x))}" "\<And>x. x \<in> K \<Longrightarrow> f x \<in> U"
  5266 proof (cases "2 \<le> DIM('a)")
  5267   case True
  5268   have TS: "T \<subseteq> affine hull S"
  5269     using affine_hull_open assms by blast
  5270   have "infinite U"
  5271     using \<open>open U\<close> \<open>U \<noteq> {}\<close> finite_imp_not_open by blast
  5272   then obtain P where "P \<subseteq> U" "finite P" "card K = card P"
  5273     using infinite_arbitrarily_large by metis
  5274   then obtain \<gamma> where \<gamma>: "bij_betw \<gamma> K P"
  5275     using \<open>finite K\<close> finite_same_card_bij by blast
  5276   obtain f g where "homeomorphism T T f g" "\<And>i. i \<in> K \<Longrightarrow> f (id i) = \<gamma> i" "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S" "bounded {x. \<not> (f x = x \<and> g x = x)}"
  5277   proof (rule homeomorphism_moving_points_exists [OF True \<open>open S\<close> \<open>connected S\<close> \<open>S \<subseteq> T\<close> \<open>finite K\<close>])
  5278     show "\<And>i. i \<in> K \<Longrightarrow> id i \<in> S \<and> \<gamma> i \<in> S"
  5279       using \<open>P \<subseteq> U\<close> \<open>bij_betw \<gamma> K P\<close> \<open>K \<subseteq> S\<close> \<open>U \<subseteq> S\<close> bij_betwE by blast
  5280     show "pairwise (\<lambda>i j. id i \<noteq> id j \<and> \<gamma> i \<noteq> \<gamma> j) K"
  5281       using \<gamma> by (auto simp: pairwise_def bij_betw_def inj_on_def)
  5282   qed (use affine_hull_open assms that in auto)
  5283   then show ?thesis
  5284     using \<gamma> \<open>P \<subseteq> U\<close> bij_betwE by (fastforce simp add: intro!: that)
  5285 next
  5286   case False
  5287   with DIM_positive have "DIM('a) = 1"
  5288     by (simp add: dual_order.antisym)
  5289   then obtain h::"'a \<Rightarrow>real" and j
  5290   where "linear h" "linear j"
  5291     and noh: "\<And>x. norm(h x) = norm x" and noj: "\<And>y. norm(j y) = norm y"
  5292     and hj:  "\<And>x. j(h x) = x" "\<And>y. h(j y) = y"
  5293     and ranh: "surj h"
  5294     using isomorphisms_UNIV_UNIV
  5295     by (metis (mono_tags, hide_lams) DIM_real UNIV_eq_I range_eqI)
  5296   obtain f g where hom: "homeomorphism (h ` T) (h ` T) f g"
  5297                and f: "\<And>x. x \<in> h ` K \<Longrightarrow> f x \<in> h ` U"
  5298                and sub: "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> h ` S"
  5299                and bou: "bounded {x. \<not> (f x = x \<and> g x = x)}"
  5300     apply (rule homeomorphism_grouping_point_4 [of "h ` U" "h ` S" "h ` K" "h ` T"])
  5301     by (simp_all add: assms image_mono  \<open>linear h\<close> open_surjective_linear_image connected_linear_image ranh)
  5302   have jf: "j (f (h x)) = x \<longleftrightarrow> f (h x) = h x" for x
  5303     by (metis hj)
  5304   have jg: "j (g (h x)) = x \<longleftrightarrow> g (h x) = h x" for x
  5305     by (metis hj)
  5306   have cont_hj: "continuous_on X h"  "continuous_on Y j" for X Y
  5307     by (simp_all add: \<open>linear h\<close> \<open>linear j\<close> linear_linear linear_continuous_on)
  5308   show ?thesis
  5309   proof
  5310     show "homeomorphism T T (j \<circ> f \<circ> h) (j \<circ> g \<circ> h)"
  5311     proof
  5312       show "continuous_on T (j \<circ> f \<circ> h)" "continuous_on T (j \<circ> g \<circ> h)"
  5313         using hom homeomorphism_def
  5314         by (blast intro: continuous_on_compose cont_hj)+
  5315       show "(j \<circ> f \<circ> h) ` T \<subseteq> T" "(j \<circ> g \<circ> h) ` T \<subseteq> T"
  5316         by auto (metis (mono_tags, hide_lams) hj(1) hom homeomorphism_def imageE imageI)+
  5317       show "\<And>x. x \<in> T \<Longrightarrow> (j \<circ> g \<circ> h) ((j \<circ> f \<circ> h) x) = x"
  5318         using hj hom homeomorphism_apply1 by fastforce
  5319       show "\<And>y. y \<in> T \<Longrightarrow> (j \<circ> f \<circ> h) ((j \<circ> g \<circ> h) y) = y"
  5320         using hj hom homeomorphism_apply2 by fastforce
  5321     qed
  5322     show "{x. \<not> ((j \<circ> f \<circ> h) x = x \<and> (j \<circ> g \<circ> h) x = x)} \<subseteq> S"
  5323       apply (clarsimp simp: jf jg hj)
  5324       using sub hj
  5325       apply (drule_tac c="h x" in subsetD, force)
  5326       by (metis imageE)
  5327     have "bounded (j ` {x. (\<not> (f x = x \<and> g x = x))})"
  5328       by (rule bounded_linear_image [OF bou]) (use \<open>linear j\<close> linear_conv_bounded_linear in auto)
  5329     moreover
  5330     have *: "{x. \<not>((j \<circ> f \<circ> h) x = x \<and> (j \<circ> g \<circ> h) x = x)} = j ` {x. (\<not> (f x = x \<and> g x = x))}"
  5331       using hj by (auto simp: jf jg image_iff, metis+)
  5332     ultimately show "bounded {x. \<not> ((j \<circ> f \<circ> h) x = x \<and> (j \<circ> g \<circ> h) x = x)}"
  5333       by metis
  5334     show "\<And>x. x \<in> K \<Longrightarrow> (j \<circ> f \<circ> h) x \<in> U"
  5335       using f hj by fastforce
  5336   qed
  5337 qed
  5338 
  5339 
  5340 proposition%unimportant homeomorphism_grouping_points_exists_gen:
  5341   fixes S :: "'a::euclidean_space set"
  5342   assumes opeU: "openin (top_of_set S) U"
  5343       and opeS: "openin (top_of_set (affine hull S)) S"
  5344       and "U \<noteq> {}" "finite K" "K \<subseteq> S" and S: "S \<subseteq> T" "T \<subseteq> affine hull S" "connected S"
  5345   obtains f g where "homeomorphism T T f g" "{x. (\<not> (f x = x \<and> g x = x))} \<subseteq> S"
  5346                     "bounded {x. (\<not> (f x = x \<and> g x = x))}" "\<And>x. x \<in> K \<Longrightarrow> f x \<in> U"
  5347 proof (cases "2 \<le> aff_dim S")
  5348   case True
  5349   have opeU': "openin (top_of_set (affine hull S)) U"
  5350     using opeS opeU openin_trans by blast
  5351   obtain u where "u \<in> U" "u \<in> S"
  5352     using \<open>U \<noteq> {}\<close> opeU openin_imp_subset by fastforce+
  5353   have "infinite U"
  5354     apply (rule infinite_openin [OF opeU \<open>u \<in> U\<close>])
  5355     apply (rule connected_imp_perfect_aff_dim [OF \<open>connected S\<close> _ \<open>u \<in> S\<close>])
  5356     using True apply simp
  5357     done
  5358   then obtain P where "P \<subseteq> U" "finite P" "card K = card P"
  5359     using infinite_arbitrarily_large by metis
  5360   then obtain \<gamma> where \<gamma>: "bij_betw \<gamma> K P"
  5361     using \<open>finite K\<close> finite_same_card_bij by blast
  5362   have "\<exists>f g. homeomorphism T T f g \<and> (\<forall>i \<in> K. f(id i) = \<gamma> i) \<and>
  5363                {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
  5364   proof (rule homeomorphism_moving_points_exists_gen [OF \<open>finite K\<close> _ _ True opeS S])
  5365     show "\<And>i. i \<in> K \<Longrightarrow> id i \<in> S \<and> \<gamma> i \<in> S"
  5366       by (metis id_apply opeU openin_contains_cball subsetCE \<open>P \<subseteq> U\<close> \<open>bij_betw \<gamma> K P\<close> \<open>K \<subseteq> S\<close> bij_betwE)
  5367     show "pairwise (\<lambda>i j. id i \<noteq> id j \<and> \<gamma> i \<noteq> \<gamma> j) K"
  5368       using \<gamma> by (auto simp: pairwise_def bij_betw_def inj_on_def)
  5369   qed
  5370   then show ?thesis
  5371     using \<gamma> \<open>P \<subseteq> U\<close> bij_betwE by (fastforce simp add: intro!: that)
  5372 next
  5373   case False
  5374   with aff_dim_geq [of S] consider "aff_dim S = -1" | "aff_dim S = 0" | "aff_dim S = 1" by linarith
  5375   then show ?thesis
  5376   proof cases
  5377     assume "aff_dim S = -1"
  5378     then have "S = {}"
  5379       using aff_dim_empty by blast
  5380     then have "False"
  5381       using \<open>U \<noteq> {}\<close> \<open>K \<subseteq> S\<close> openin_imp_subset [OF opeU] by blast
  5382     then show ?thesis ..
  5383   next
  5384     assume "aff_dim S = 0"
  5385     then obtain a where "S = {a}"
  5386       using aff_dim_eq_0 by blast
  5387     then have "K \<subseteq> U"
  5388       using \<open>U \<noteq> {}\<close> \<open>K \<subseteq> S\<close> openin_imp_subset [OF opeU] by blast
  5389     show ?thesis
  5390       apply (rule that [of id id])
  5391       using \<open>K \<subseteq> U\<close> by (auto simp: continuous_on_id intro: homeomorphismI)
  5392   next
  5393     assume "aff_dim S = 1"
  5394     then have "affine hull S homeomorphic (UNIV :: real set)"
  5395       by (auto simp: homeomorphic_affine_sets)
  5396     then obtain h::"'a\<Rightarrow>real" and j where homhj: "homeomorphism (affine hull S) UNIV h j"
  5397       using homeomorphic_def by blast
  5398     then have h: "\<And>x. x \<in> affine hull S \<Longrightarrow> j(h(x)) = x" and j: "\<And>y. j y \<in> affine hull S \<and> h(j y) = y"
  5399       by (auto simp: homeomorphism_def)
  5400     have connh: "connected (h ` S)"
  5401       by (meson Topological_Spaces.connected_continuous_image \<open>connected S\<close> homeomorphism_cont1 homeomorphism_of_subsets homhj hull_subset top_greatest)
  5402     have hUS: "h ` U \<subseteq> h ` S"
  5403       by (meson homeomorphism_imp_open_map homeomorphism_of_subsets homhj hull_subset opeS opeU open_UNIV openin_open_eq)
  5404     have opn: "openin (top_of_set (affine hull S)) U \<Longrightarrow> open (h ` U)" for U
  5405       using homeomorphism_imp_open_map [OF homhj]  by simp
  5406     have "open (h ` U)" "open (h ` S)"
  5407       by (auto intro: opeS opeU openin_trans opn)
  5408     then obtain f g where hom: "homeomorphism (h ` T) (h ` T) f g"
  5409                  and f: "\<And>x. x \<in> h ` K \<Longrightarrow> f x \<in> h ` U"
  5410                  and sub: "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> h ` S"
  5411                  and bou: "bounded {x. \<not> (f x = x \<and> g x = x)}"
  5412       apply (rule homeomorphism_grouping_points_exists [of "h ` U" "h ` S" "h ` K" "h ` T"])
  5413       using assms by (auto simp: connh hUS)
  5414     have jf: "\<And>x. x \<in> affine hull S \<Longrightarrow> j (f (h x)) = x \<longleftrightarrow> f (h x) = h x"
  5415       by (metis h j)
  5416     have jg: "\<And>x. x \<in> affine hull S \<Longrightarrow> j (g (h x)) = x \<longleftrightarrow> g (h x) = h x"
  5417       by (metis h j)
  5418     have cont_hj: "continuous_on T h"  "continuous_on Y j" for Y
  5419       apply (rule continuous_on_subset [OF _ \<open>T \<subseteq> affine hull S\<close>])
  5420       using homeomorphism_def homhj apply blast
  5421       by (meson continuous_on_subset homeomorphism_def homhj top_greatest)
  5422     define f' where "f' \<equiv> \<lambda>x. if x \<in> affine hull S then (j \<circ> f \<circ> h) x else x"
  5423     define g' where "g' \<equiv> \<lambda>x. if x \<in> affine hull S then (j \<circ> g \<circ> h) x else x"
  5424     show ?thesis
  5425     proof
  5426       show "homeomorphism T T f' g'"
  5427       proof
  5428         have "continuous_on T (j \<circ> f \<circ> h)"
  5429           apply (intro continuous_on_compose cont_hj)
  5430           using hom homeomorphism_def by blast
  5431         then show "continuous_on T f'"
  5432           apply (rule continuous_on_eq)
  5433           using \<open>T \<subseteq> affine hull S\<close> f'_def by auto
  5434         have "continuous_on T (j \<circ> g \<circ> h)"
  5435           apply (intro continuous_on_compose cont_hj)
  5436           using hom homeomorphism_def by blast
  5437         then show "continuous_on T g'"
  5438           apply (rule continuous_on_eq)
  5439           using \<open>T \<subseteq> affine hull S\<close> g'_def by auto
  5440         show "f' ` T \<subseteq> T"
  5441         proof (clarsimp simp: f'_def)
  5442           fix x assume "x \<in> T"
  5443           then have "f (h x) \<in> h ` T"
  5444             by (metis (no_types) hom homeomorphism_def image_subset_iff subset_refl)
  5445           then show "j (f (h x)) \<in> T"
  5446             using \<open>T \<subseteq> affine hull S\<close> h by auto
  5447         qed
  5448         show "g' ` T \<subseteq> T"
  5449         proof (clarsimp simp: g'_def)
  5450           fix x assume "x \<in> T"
  5451           then have "g (h x) \<in> h ` T"
  5452             by (metis (no_types) hom homeomorphism_def image_subset_iff subset_refl)
  5453           then show "j (g (h x)) \<in> T"
  5454             using \<open>T \<subseteq> affine hull S\<close> h by auto
  5455         qed
  5456         show "\<And>x. x \<in> T \<Longrightarrow> g' (f' x) = x"
  5457           using h j hom homeomorphism_apply1 by (fastforce simp add: f'_def g'_def)
  5458         show "\<And>y. y \<in> T \<Longrightarrow> f' (g' y) = y"
  5459           using h j hom homeomorphism_apply2 by (fastforce simp add: f'_def g'_def)
  5460       qed
  5461     next
  5462       show "{x. \<not> (f' x = x \<and> g' x = x)} \<subseteq> S"
  5463         apply (clarsimp simp: f'_def g'_def jf jg)
  5464         apply (rule imageE [OF subsetD [OF sub]], force)
  5465         by (metis h hull_inc)
  5466     next
  5467       have "compact (j ` closure {x. \<not> (f x = x \<and> g x = x)})"
  5468         using bou by (auto simp: compact_continuous_image cont_hj)
  5469       then have "bounded (j ` {x. \<not> (f x = x \<and> g x = x)})"
  5470         by (rule bounded_closure_image [OF compact_imp_bounded])
  5471       moreover
  5472       have *: "{x \<in> affine hull S. j (f (h x)) \<noteq> x \<or> j (g (h x)) \<noteq> x} = j ` {x. (\<not> (f x = x \<and> g x = x))}"
  5473         using h j by (auto simp: image_iff; metis)
  5474       ultimately have "bounded {x \<in> affine hull S. j (f (h x)) \<noteq> x \<or> j (g (h x)) \<noteq> x}"
  5475         by metis
  5476       then show "bounded {x. \<not> (f' x = x \<and> g' x = x)}"
  5477         by (simp add: f'_def g'_def Collect_mono bounded_subset)
  5478     next
  5479       show "f' x \<in> U" if "x \<in> K" for x
  5480       proof -
  5481         have "U \<subseteq> S"
  5482           using opeU openin_imp_subset by blast
  5483         then have "j (f (h x)) \<in> U"
  5484           using f h hull_subset that by fastforce
  5485         then show "f' x \<in> U"
  5486           using \<open>K \<subseteq> S\<close> S f'_def that by auto
  5487       qed
  5488     qed
  5489   qed
  5490 qed
  5491 
  5492 
  5493 subsection\<open>Nullhomotopic mappings\<close>
  5494 
  5495 text\<open> A mapping out of a sphere is nullhomotopic iff it extends to the ball.
  5496 This even works out in the degenerate cases when the radius is \<open>\<le>\<close> 0, and
  5497 we also don't need to explicitly assume continuity since it's already implicit
  5498 in both sides of the equivalence.\<close>
  5499 
  5500 lemma nullhomotopic_from_lemma:
  5501   assumes contg: "continuous_on (cball a r - {a}) g"
  5502       and fa: "\<And>e. 0 < e
  5503                \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>x. x \<noteq> a \<and> norm(x - a) < d \<longrightarrow> norm(g x - f a) < e)"
  5504       and r: "\<And>x. x \<in> cball a r \<and> x \<noteq> a \<Longrightarrow> f x = g x"
  5505     shows "continuous_on (cball a r) f"
  5506 proof (clarsimp simp: continuous_on_eq_continuous_within Ball_def)
  5507   fix x
  5508   assume x: "dist a x \<le> r"
  5509   show "continuous (at x within cball a r) f"
  5510   proof (cases "x=a")
  5511     case True
  5512     then show ?thesis
  5513       by (metis continuous_within_eps_delta fa dist_norm dist_self r)
  5514   next
  5515     case False
  5516     show ?thesis
  5517     proof (rule continuous_transform_within [where f=g and d = "norm(x-a)"])
  5518       have "\<exists>d>0. \<forall>x'\<in>cball a r.
  5519                       dist x' x < d \<longrightarrow> dist (g x') (g x) < e" if "e>0" for e
  5520       proof -
  5521         obtain d where "d > 0"
  5522            and d: "\<And>x'. \<lbrakk>dist x' a \<le> r; x' \<noteq> a; dist x' x < d\<rbrakk> \<Longrightarrow>
  5523                                  dist (g x') (g x) < e"
  5524           using contg False x \<open>e>0\<close>
  5525           unfolding continuous_on_iff by (fastforce simp add: dist_commute intro: that)
  5526         show ?thesis
  5527           using \<open>d > 0\<close> \<open>x \<noteq> a\<close>
  5528           by (rule_tac x="min d (norm(x - a))" in exI)
  5529              (auto simp: dist_commute dist_norm [symmetric]  intro!: d)
  5530       qed
  5531       then show "continuous (at x within cball a r) g"
  5532         using contg False by (auto simp: continuous_within_eps_delta)
  5533       show "0 < norm (x - a)"
  5534         using False by force
  5535       show "x \<in> cball a r"
  5536         by (simp add: x)
  5537       show "\<And>x'. \<lbrakk>x' \<in> cball a r; dist x' x < norm (x - a)\<rbrakk>
  5538         \<Longrightarrow> g x' = f x'"
  5539         by (metis dist_commute dist_norm less_le r)
  5540     qed
  5541   qed
  5542 qed
  5543 
  5544 proposition nullhomotopic_from_sphere_extension:
  5545   fixes f :: "'M::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  5546   shows  "(\<exists>c. homotopic_with_canon (\<lambda>x. True) (sphere a r) S f (\<lambda>x. c)) \<longleftrightarrow>
  5547           (\<exists>g. continuous_on (cball a r) g \<and> g ` (cball a r) \<subseteq> S \<and>
  5548                (\<forall>x \<in> sphere a r. g x = f x))"
  5549          (is "?lhs = ?rhs")
  5550 proof (cases r "0::real" rule: linorder_cases)
  5551   case less
  5552   then show ?thesis
  5553     by (simp add: homotopic_on_emptyI)
  5554 next
  5555   case equal
  5556   then show ?thesis
  5557     apply (auto simp: homotopic_with)
  5558     apply (rule_tac x="\<lambda>x. h (0, a)" in exI)
  5559      apply (fastforce simp add:)
  5560     using continuous_on_const by blast
  5561 next
  5562   case greater
  5563   let ?P = "continuous_on {x. norm(x - a) = r} f \<and> f ` {x. norm(x - a) = r} \<subseteq> S"
  5564   have ?P if ?lhs using that
  5565   proof
  5566     fix c
  5567     assume c: "homotopic_with_canon (\<lambda>x. True) (sphere a r) S f (\<lambda>x. c)"
  5568     then have contf: "continuous_on (sphere a r) f" 
  5569       by (metis homotopic_with_imp_continuous)
  5570     moreover have fim: "f ` sphere a r \<subseteq> S"
  5571       by (meson continuous_map_subtopology_eu c homotopic_with_imp_continuous_maps)
  5572     show ?P
  5573       using contf fim by (auto simp: sphere_def dist_norm norm_minus_commute)
  5574   qed
  5575   moreover have ?P if ?rhs using that
  5576   proof
  5577     fix g
  5578     assume g: "continuous_on (cball a r) g \<and> g ` cball a r \<subseteq> S \<and> (\<forall>xa\<in>sphere a r. g xa = f xa)"
  5579     then
  5580     show ?P
  5581       apply (safe elim!: continuous_on_eq [OF continuous_on_subset])
  5582       apply (auto simp: dist_norm norm_minus_commute)
  5583       by (metis dist_norm image_subset_iff mem_sphere norm_minus_commute sphere_cball subsetCE)
  5584   qed
  5585   moreover have ?thesis if ?P
  5586   proof
  5587     assume ?lhs
  5588     then obtain c where "homotopic_with_canon (\<lambda>x. True) (sphere a r) S (\<lambda>x. c) f"
  5589       using homotopic_with_sym by blast
  5590     then obtain h where conth: "continuous_on ({0..1::real} \<times> sphere a r) h"
  5591                     and him: "h ` ({0..1} \<times> sphere a r) \<subseteq> S"
  5592                     and h: "\<And>x. h(0, x) = c" "\<And>x. h(1, x) = f x"
  5593       by (auto simp: homotopic_with_def)
  5594     obtain b1::'M where "b1 \<in> Basis"
  5595       using SOME_Basis by auto
  5596     have "c \<in> S"
  5597       apply (rule him [THEN subsetD])
  5598       apply (rule_tac x = "(0, a + r *\<^sub>R b1)" in image_eqI)
  5599       using h greater \<open>b1 \<in> Basis\<close>
  5600        apply (auto simp: dist_norm)
  5601       done
  5602     have uconth: "uniformly_continuous_on ({0..1::real} \<times> (sphere a r)) h"
  5603       by (force intro: compact_Times conth compact_uniformly_continuous)
  5604     let ?g = "\<lambda>x. h (norm (x - a)/r,
  5605                      a + (if x = a then r *\<^sub>R b1 else (r / norm(x - a)) *\<^sub>R (x - a)))"
  5606     let ?g' = "\<lambda>x. h (norm (x - a)/r, a + (r / norm(x - a)) *\<^sub>R (x - a))"
  5607     show ?rhs
  5608     proof (intro exI conjI)
  5609       have "continuous_on (cball a r - {a}) ?g'"
  5610         apply (rule continuous_on_compose2 [OF conth])
  5611          apply (intro continuous_intros)
  5612         using greater apply (auto simp: dist_norm norm_minus_commute)
  5613         done
  5614       then show "continuous_on (cball a r) ?g"
  5615       proof (rule nullhomotopic_from_lemma)
  5616         show "\<exists>d>0. \<forall>x. x \<noteq> a \<and> norm (x - a) < d \<longrightarrow> norm (?g' x - ?g a) < e" if "0 < e" for e
  5617         proof -
  5618           obtain d where "0 < d"
  5619              and d: "\<And>x x'. \<lbrakk>x \<in> {0..1} \<times> sphere a r; x' \<in> {0..1} \<times> sphere a r; dist x' x < d\<rbrakk>
  5620                         \<Longrightarrow> dist (h x') (h x) < e"
  5621             using uniformly_continuous_onE [OF uconth \<open>0 < e\<close>] by auto
  5622           have *: "norm (h (norm (x - a) / r,
  5623                          a + (r / norm (x - a)) *\<^sub>R (x - a)) - h (0, a + r *\<^sub>R b1)) < e"
  5624                    if "x \<noteq> a" "norm (x - a) < r" "norm (x - a) < d * r" for x
  5625           proof -
  5626             have "norm (h (norm (x - a) / r, a + (r / norm (x - a)) *\<^sub>R (x - a)) - h (0, a + r *\<^sub>R b1)) =
  5627                   norm (h (norm (x - a) / r, a + (r / norm (x - a)) *\<^sub>R (x - a)) - h (0, a + (r / norm (x - a)) *\<^sub>R (x - a)))"
  5628               by (simp add: h)
  5629             also have "\<dots> < e"
  5630               apply (rule d [unfolded dist_norm])
  5631               using greater \<open>0 < d\<close> \<open>b1 \<in> Basis\<close> that
  5632                 by (auto simp: dist_norm divide_simps)
  5633             finally show ?thesis .
  5634           qed
  5635           show ?thesis
  5636             apply (rule_tac x = "min r (d * r)" in exI)
  5637             using greater \<open>0 < d\<close> by (auto simp: *)
  5638         qed
  5639         show "\<And>x. x \<in> cball a r \<and> x \<noteq> a \<Longrightarrow> ?g x = ?g' x"
  5640           by auto
  5641       qed
  5642     next
  5643       show "?g ` cball a r \<subseteq> S"
  5644         using greater him \<open>c \<in> S\<close>
  5645         by (force simp: h dist_norm norm_minus_commute)
  5646     next
  5647       show "\<forall>x\<in>sphere a r. ?g x = f x"
  5648         using greater by (auto simp: h dist_norm norm_minus_commute)
  5649     qed
  5650   next
  5651     assume ?rhs
  5652     then obtain g where contg: "continuous_on (cball a r) g"
  5653                     and gim: "g ` cball a r \<subseteq> S"
  5654                     and gf: "\<forall>x \<in> sphere a r. g x = f x"
  5655       by auto
  5656     let ?h = "\<lambda>y. g (a + (fst y) *\<^sub>R (snd y - a))"
  5657     have "continuous_on ({0..1} \<times> sphere a r) ?h"
  5658       apply (rule continuous_on_compose2 [OF contg])
  5659        apply (intro continuous_intros)
  5660       apply (auto simp: dist_norm norm_minus_commute mult_left_le_one_le)
  5661       done
  5662     moreover
  5663     have "?h ` ({0..1} \<times> sphere a r) \<subseteq> S"
  5664       by (auto simp: dist_norm norm_minus_commute mult_left_le_one_le gim [THEN subsetD])
  5665     moreover
  5666     have "\<forall>x\<in>sphere a r. ?h (0, x) = g a" "\<forall>x\<in>sphere a r. ?h (1, x) = f x"
  5667       by (auto simp: dist_norm norm_minus_commute mult_left_le_one_le gf)
  5668     ultimately
  5669     show ?lhs
  5670       apply (subst homotopic_with_sym)
  5671       apply (rule_tac x="g a" in exI)
  5672       apply (auto simp: homotopic_with)
  5673       done
  5674   qed
  5675   ultimately
  5676   show ?thesis by meson
  5677 qed 
  5678 
  5679 end