src/HOL/Analysis/Homotopy.thy
changeset 69620 19d8a59481db
child 69712 dc85b5b3a532
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Analysis/Homotopy.thy	Mon Jan 07 14:57:45 2019 +0100
     1.3 @@ -0,0 +1,5159 @@
     1.4 +(*  Title:      HOL/Analysis/Path_Connected.thy
     1.5 +    Authors:    LC Paulson and Robert Himmelmann (TU Muenchen), based on material from HOL Light
     1.6 +*)
     1.7 +
     1.8 +section \<open>Homotopy of Maps\<close>
     1.9 +
    1.10 +theory Homotopy
    1.11 +  imports Path_Connected Continuum_Not_Denumerable
    1.12 +begin
    1.13 +
    1.14 +definition%important homotopic_with ::
    1.15 +  "[('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> bool, 'a set, 'b set, 'a \<Rightarrow> 'b, 'a \<Rightarrow> 'b] \<Rightarrow> bool"
    1.16 +where
    1.17 + "homotopic_with P X Y p q \<equiv>
    1.18 +   (\<exists>h:: real \<times> 'a \<Rightarrow> 'b.
    1.19 +       continuous_on ({0..1} \<times> X) h \<and>
    1.20 +       h ` ({0..1} \<times> X) \<subseteq> Y \<and>
    1.21 +       (\<forall>x. h(0, x) = p x) \<and>
    1.22 +       (\<forall>x. h(1, x) = q x) \<and>
    1.23 +       (\<forall>t \<in> {0..1}. P(\<lambda>x. h(t, x))))"
    1.24 +
    1.25 +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.
    1.26 +We often just want to require that \<open>P\<close> fixes some subset, but to include the case of a loop homotopy,
    1.27 +it is convenient to have a general property \<open>P\<close>.\<close>
    1.28 +
    1.29 +text \<open>We often want to just localize the ending function equality or whatever.\<close>
    1.30 +text%important \<open>%whitespace\<close>
    1.31 +proposition homotopic_with:
    1.32 +  fixes X :: "'a::topological_space set" and Y :: "'b::topological_space set"
    1.33 +  assumes "\<And>h k. (\<And>x. x \<in> X \<Longrightarrow> h x = k x) \<Longrightarrow> (P h \<longleftrightarrow> P k)"
    1.34 +  shows "homotopic_with P X Y p q \<longleftrightarrow>
    1.35 +           (\<exists>h :: real \<times> 'a \<Rightarrow> 'b.
    1.36 +              continuous_on ({0..1} \<times> X) h \<and>
    1.37 +              h ` ({0..1} \<times> X) \<subseteq> Y \<and>
    1.38 +              (\<forall>x \<in> X. h(0,x) = p x) \<and>
    1.39 +              (\<forall>x \<in> X. h(1,x) = q x) \<and>
    1.40 +              (\<forall>t \<in> {0..1}. P(\<lambda>x. h(t, x))))"
    1.41 +  unfolding homotopic_with_def
    1.42 +  apply (rule iffI, blast, clarify)
    1.43 +  apply (rule_tac x="\<lambda>(u,v). if v \<in> X then h(u,v) else if u = 0 then p v else q v" in exI)
    1.44 +  apply auto
    1.45 +  apply (force elim: continuous_on_eq)
    1.46 +  apply (drule_tac x=t in bspec, force)
    1.47 +  apply (subst assms; simp)
    1.48 +  done
    1.49 +
    1.50 +proposition homotopic_with_eq:
    1.51 +   assumes h: "homotopic_with P X Y f g"
    1.52 +       and f': "\<And>x. x \<in> X \<Longrightarrow> f' x = f x"
    1.53 +       and g': "\<And>x. x \<in> X \<Longrightarrow> g' x = g x"
    1.54 +       and P:  "(\<And>h k. (\<And>x. x \<in> X \<Longrightarrow> h x = k x) \<Longrightarrow> (P h \<longleftrightarrow> P k))"
    1.55 +   shows "homotopic_with P X Y f' g'"
    1.56 +  using h unfolding homotopic_with_def
    1.57 +  apply safe
    1.58 +  apply (rule_tac x="\<lambda>(u,v). if v \<in> X then h(u,v) else if u = 0 then f' v else g' v" in exI)
    1.59 +  apply (simp add: f' g', safe)
    1.60 +  apply (fastforce intro: continuous_on_eq, fastforce)
    1.61 +  apply (subst P; fastforce)
    1.62 +  done
    1.63 +
    1.64 +proposition homotopic_with_equal:
    1.65 +   assumes contf: "continuous_on X f" and fXY: "f ` X \<subseteq> Y"
    1.66 +       and gf: "\<And>x. x \<in> X \<Longrightarrow> g x = f x"
    1.67 +       and P:  "P f" "P g"
    1.68 +   shows "homotopic_with P X Y f g"
    1.69 +  unfolding homotopic_with_def
    1.70 +  apply (rule_tac x="\<lambda>(u,v). if u = 1 then g v else f v" in exI)
    1.71 +  using assms
    1.72 +  apply (intro conjI)
    1.73 +  apply (rule continuous_on_eq [where f = "f \<circ> snd"])
    1.74 +  apply (rule continuous_intros | force)+
    1.75 +  apply clarify
    1.76 +  apply (case_tac "t=1"; force)
    1.77 +  done
    1.78 +
    1.79 +
    1.80 +lemma image_Pair_const: "(\<lambda>x. (x, c)) ` A = A \<times> {c}"
    1.81 +  by auto
    1.82 +
    1.83 +lemma homotopic_constant_maps:
    1.84 +   "homotopic_with (\<lambda>x. True) s t (\<lambda>x. a) (\<lambda>x. b) \<longleftrightarrow> s = {} \<or> path_component t a b"
    1.85 +proof (cases "s = {} \<or> t = {}")
    1.86 +  case True with continuous_on_const show ?thesis
    1.87 +    by (auto simp: homotopic_with path_component_def)
    1.88 +next
    1.89 +  case False
    1.90 +  then obtain c where "c \<in> s" by blast
    1.91 +  show ?thesis
    1.92 +  proof
    1.93 +    assume "homotopic_with (\<lambda>x. True) s t (\<lambda>x. a) (\<lambda>x. b)"
    1.94 +    then obtain h :: "real \<times> 'a \<Rightarrow> 'b"
    1.95 +        where conth: "continuous_on ({0..1} \<times> s) h"
    1.96 +          and h: "h ` ({0..1} \<times> s) \<subseteq> t" "(\<forall>x\<in>s. h (0, x) = a)" "(\<forall>x\<in>s. h (1, x) = b)"
    1.97 +      by (auto simp: homotopic_with)
    1.98 +    have "continuous_on {0..1} (h \<circ> (\<lambda>t. (t, c)))"
    1.99 +      apply (rule continuous_intros conth | simp add: image_Pair_const)+
   1.100 +      apply (blast intro:  \<open>c \<in> s\<close> continuous_on_subset [OF conth])
   1.101 +      done
   1.102 +    with \<open>c \<in> s\<close> h show "s = {} \<or> path_component t a b"
   1.103 +      apply (simp_all add: homotopic_with path_component_def, auto)
   1.104 +      apply (drule_tac x="h \<circ> (\<lambda>t. (t, c))" in spec)
   1.105 +      apply (auto simp: pathstart_def pathfinish_def path_image_def path_def)
   1.106 +      done
   1.107 +  next
   1.108 +    assume "s = {} \<or> path_component t a b"
   1.109 +    with False show "homotopic_with (\<lambda>x. True) s t (\<lambda>x. a) (\<lambda>x. b)"
   1.110 +      apply (clarsimp simp: homotopic_with path_component_def pathstart_def pathfinish_def path_image_def path_def)
   1.111 +      apply (rule_tac x="g \<circ> fst" in exI)
   1.112 +      apply (rule conjI continuous_intros | force)+
   1.113 +      done
   1.114 +  qed
   1.115 +qed
   1.116 +
   1.117 +
   1.118 +subsection%unimportant\<open>Trivial properties\<close>
   1.119 +
   1.120 +lemma homotopic_with_imp_property: "homotopic_with P X Y f g \<Longrightarrow> P f \<and> P g"
   1.121 +  unfolding homotopic_with_def Ball_def
   1.122 +  apply clarify
   1.123 +  apply (frule_tac x=0 in spec)
   1.124 +  apply (drule_tac x=1 in spec, auto)
   1.125 +  done
   1.126 +
   1.127 +lemma continuous_on_o_Pair: "\<lbrakk>continuous_on (T \<times> X) h; t \<in> T\<rbrakk> \<Longrightarrow> continuous_on X (h \<circ> Pair t)"
   1.128 +  by (fast intro: continuous_intros elim!: continuous_on_subset)
   1.129 +
   1.130 +lemma homotopic_with_imp_continuous:
   1.131 +    assumes "homotopic_with P X Y f g"
   1.132 +    shows "continuous_on X f \<and> continuous_on X g"
   1.133 +proof -
   1.134 +  obtain h :: "real \<times> 'a \<Rightarrow> 'b"
   1.135 +    where conth: "continuous_on ({0..1} \<times> X) h"
   1.136 +      and h: "\<forall>x. h (0, x) = f x" "\<forall>x. h (1, x) = g x"
   1.137 +    using assms by (auto simp: homotopic_with_def)
   1.138 +  have *: "t \<in> {0..1} \<Longrightarrow> continuous_on X (h \<circ> (\<lambda>x. (t,x)))" for t
   1.139 +    by (rule continuous_intros continuous_on_subset [OF conth] | force)+
   1.140 +  show ?thesis
   1.141 +    using h *[of 0] *[of 1] by auto
   1.142 +qed
   1.143 +
   1.144 +proposition homotopic_with_imp_subset1:
   1.145 +     "homotopic_with P X Y f g \<Longrightarrow> f ` X \<subseteq> Y"
   1.146 +  by (simp add: homotopic_with_def image_subset_iff) (metis atLeastAtMost_iff order_refl zero_le_one)
   1.147 +
   1.148 +proposition homotopic_with_imp_subset2:
   1.149 +     "homotopic_with P X Y f g \<Longrightarrow> g ` X \<subseteq> Y"
   1.150 +  by (simp add: homotopic_with_def image_subset_iff) (metis atLeastAtMost_iff order_refl zero_le_one)
   1.151 +
   1.152 +proposition homotopic_with_mono:
   1.153 +    assumes hom: "homotopic_with P X Y f g"
   1.154 +        and Q: "\<And>h. \<lbrakk>continuous_on X h; image h X \<subseteq> Y \<and> P h\<rbrakk> \<Longrightarrow> Q h"
   1.155 +      shows "homotopic_with Q X Y f g"
   1.156 +  using hom
   1.157 +  apply (simp add: homotopic_with_def)
   1.158 +  apply (erule ex_forward)
   1.159 +  apply (force simp: intro!: Q dest: continuous_on_o_Pair)
   1.160 +  done
   1.161 +
   1.162 +proposition homotopic_with_subset_left:
   1.163 +     "\<lbrakk>homotopic_with P X Y f g; Z \<subseteq> X\<rbrakk> \<Longrightarrow> homotopic_with P Z Y f g"
   1.164 +  apply (simp add: homotopic_with_def)
   1.165 +  apply (fast elim!: continuous_on_subset ex_forward)
   1.166 +  done
   1.167 +
   1.168 +proposition homotopic_with_subset_right:
   1.169 +     "\<lbrakk>homotopic_with P X Y f g; Y \<subseteq> Z\<rbrakk> \<Longrightarrow> homotopic_with P X Z f g"
   1.170 +  apply (simp add: homotopic_with_def)
   1.171 +  apply (fast elim!: continuous_on_subset ex_forward)
   1.172 +  done
   1.173 +
   1.174 +proposition homotopic_with_compose_continuous_right:
   1.175 +    "\<lbrakk>homotopic_with (\<lambda>f. p (f \<circ> h)) X Y f g; continuous_on W h; h ` W \<subseteq> X\<rbrakk>
   1.176 +     \<Longrightarrow> homotopic_with p W Y (f \<circ> h) (g \<circ> h)"
   1.177 +  apply (clarsimp simp add: homotopic_with_def)
   1.178 +  apply (rename_tac k)
   1.179 +  apply (rule_tac x="k \<circ> (\<lambda>y. (fst y, h (snd y)))" in exI)
   1.180 +  apply (rule conjI continuous_intros continuous_on_compose [where f=snd and g=h, unfolded o_def] | simp)+
   1.181 +  apply (erule continuous_on_subset)
   1.182 +  apply (fastforce simp: o_def)+
   1.183 +  done
   1.184 +
   1.185 +proposition homotopic_compose_continuous_right:
   1.186 +     "\<lbrakk>homotopic_with (\<lambda>f. True) X Y f g; continuous_on W h; h ` W \<subseteq> X\<rbrakk>
   1.187 +      \<Longrightarrow> homotopic_with (\<lambda>f. True) W Y (f \<circ> h) (g \<circ> h)"
   1.188 +  using homotopic_with_compose_continuous_right by fastforce
   1.189 +
   1.190 +proposition homotopic_with_compose_continuous_left:
   1.191 +     "\<lbrakk>homotopic_with (\<lambda>f. p (h \<circ> f)) X Y f g; continuous_on Y h; h ` Y \<subseteq> Z\<rbrakk>
   1.192 +      \<Longrightarrow> homotopic_with p X Z (h \<circ> f) (h \<circ> g)"
   1.193 +  apply (clarsimp simp add: homotopic_with_def)
   1.194 +  apply (rename_tac k)
   1.195 +  apply (rule_tac x="h \<circ> k" in exI)
   1.196 +  apply (rule conjI continuous_intros continuous_on_compose [where f=snd and g=h, unfolded o_def] | simp)+
   1.197 +  apply (erule continuous_on_subset)
   1.198 +  apply (fastforce simp: o_def)+
   1.199 +  done
   1.200 +
   1.201 +proposition homotopic_compose_continuous_left:
   1.202 +   "\<lbrakk>homotopic_with (\<lambda>_. True) X Y f g;
   1.203 +     continuous_on Y h; h ` Y \<subseteq> Z\<rbrakk>
   1.204 +    \<Longrightarrow> homotopic_with (\<lambda>f. True) X Z (h \<circ> f) (h \<circ> g)"
   1.205 +  using homotopic_with_compose_continuous_left by fastforce
   1.206 +
   1.207 +proposition homotopic_with_Pair:
   1.208 +   assumes hom: "homotopic_with p s t f g" "homotopic_with p' s' t' f' g'"
   1.209 +       and q: "\<And>f g. \<lbrakk>p f; p' g\<rbrakk> \<Longrightarrow> q(\<lambda>(x,y). (f x, g y))"
   1.210 +     shows "homotopic_with q (s \<times> s') (t \<times> t')
   1.211 +                  (\<lambda>(x,y). (f x, f' y)) (\<lambda>(x,y). (g x, g' y))"
   1.212 +  using hom
   1.213 +  apply (clarsimp simp add: homotopic_with_def)
   1.214 +  apply (rename_tac k k')
   1.215 +  apply (rule_tac x="\<lambda>z. ((k \<circ> (\<lambda>x. (fst x, fst (snd x)))) z, (k' \<circ> (\<lambda>x. (fst x, snd (snd x)))) z)" in exI)
   1.216 +  apply (rule conjI continuous_intros | erule continuous_on_subset | clarsimp)+
   1.217 +  apply (auto intro!: q [unfolded case_prod_unfold])
   1.218 +  done
   1.219 +
   1.220 +lemma homotopic_on_empty [simp]: "homotopic_with (\<lambda>x. True) {} t f g"
   1.221 +  by (metis continuous_on_def empty_iff homotopic_with_equal image_subset_iff)
   1.222 +
   1.223 +
   1.224 +text\<open>Homotopy with P is an equivalence relation (on continuous functions mapping X into Y that satisfy P,
   1.225 +     though this only affects reflexivity.\<close>
   1.226 +
   1.227 +
   1.228 +proposition homotopic_with_refl:
   1.229 +   "homotopic_with P X Y f f \<longleftrightarrow> continuous_on X f \<and> image f X \<subseteq> Y \<and> P f"
   1.230 +  apply (rule iffI)
   1.231 +  using homotopic_with_imp_continuous homotopic_with_imp_property homotopic_with_imp_subset2 apply blast
   1.232 +  apply (simp add: homotopic_with_def)
   1.233 +  apply (rule_tac x="f \<circ> snd" in exI)
   1.234 +  apply (rule conjI continuous_intros | force)+
   1.235 +  done
   1.236 +
   1.237 +lemma homotopic_with_symD:
   1.238 +  fixes X :: "'a::real_normed_vector set"
   1.239 +    assumes "homotopic_with P X Y f g"
   1.240 +      shows "homotopic_with P X Y g f"
   1.241 +  using assms
   1.242 +  apply (clarsimp simp add: homotopic_with_def)
   1.243 +  apply (rename_tac h)
   1.244 +  apply (rule_tac x="h \<circ> (\<lambda>y. (1 - fst y, snd y))" in exI)
   1.245 +  apply (rule conjI continuous_intros | erule continuous_on_subset | force simp: image_subset_iff)+
   1.246 +  done
   1.247 +
   1.248 +proposition homotopic_with_sym:
   1.249 +    fixes X :: "'a::real_normed_vector set"
   1.250 +    shows "homotopic_with P X Y f g \<longleftrightarrow> homotopic_with P X Y g f"
   1.251 +  using homotopic_with_symD by blast
   1.252 +
   1.253 +lemma split_01: "{0..1::real} = {0..1/2} \<union> {1/2..1}"
   1.254 +  by force
   1.255 +
   1.256 +lemma split_01_prod: "{0..1::real} \<times> X = ({0..1/2} \<times> X) \<union> ({1/2..1} \<times> X)"
   1.257 +  by force
   1.258 +
   1.259 +proposition homotopic_with_trans:
   1.260 +    fixes X :: "'a::real_normed_vector set"
   1.261 +    assumes "homotopic_with P X Y f g" and "homotopic_with P X Y g h"
   1.262 +      shows "homotopic_with P X Y f h"
   1.263 +proof -
   1.264 +  have clo1: "closedin (subtopology euclidean ({0..1/2} \<times> X \<union> {1/2..1} \<times> X)) ({0..1/2::real} \<times> X)"
   1.265 +    apply (simp add: closedin_closed split_01_prod [symmetric])
   1.266 +    apply (rule_tac x="{0..1/2} \<times> UNIV" in exI)
   1.267 +    apply (force simp: closed_Times)
   1.268 +    done
   1.269 +  have clo2: "closedin (subtopology euclidean ({0..1/2} \<times> X \<union> {1/2..1} \<times> X)) ({1/2..1::real} \<times> X)"
   1.270 +    apply (simp add: closedin_closed split_01_prod [symmetric])
   1.271 +    apply (rule_tac x="{1/2..1} \<times> UNIV" in exI)
   1.272 +    apply (force simp: closed_Times)
   1.273 +    done
   1.274 +  { fix k1 k2:: "real \<times> 'a \<Rightarrow> 'b"
   1.275 +    assume cont: "continuous_on ({0..1} \<times> X) k1" "continuous_on ({0..1} \<times> X) k2"
   1.276 +       and Y: "k1 ` ({0..1} \<times> X) \<subseteq> Y" "k2 ` ({0..1} \<times> X) \<subseteq> Y"
   1.277 +       and geq: "\<forall>x. k1 (1, x) = g x" "\<forall>x. k2 (0, x) = g x"
   1.278 +       and k12: "\<forall>x. k1 (0, x) = f x" "\<forall>x. k2 (1, x) = h x"
   1.279 +       and P:   "\<forall>t\<in>{0..1}. P (\<lambda>x. k1 (t, x))" "\<forall>t\<in>{0..1}. P (\<lambda>x. k2 (t, x))"
   1.280 +    define k where "k y =
   1.281 +      (if fst y \<le> 1 / 2
   1.282 +       then (k1 \<circ> (\<lambda>x. (2 *\<^sub>R fst x, snd x))) y
   1.283 +       else (k2 \<circ> (\<lambda>x. (2 *\<^sub>R fst x -1, snd x))) y)" for y
   1.284 +    have keq: "k1 (2 * u, v) = k2 (2 * u - 1, v)" if "u = 1/2"  for u v
   1.285 +      by (simp add: geq that)
   1.286 +    have "continuous_on ({0..1} \<times> X) k"
   1.287 +      using cont
   1.288 +      apply (simp add: split_01_prod k_def)
   1.289 +      apply (rule clo1 clo2 continuous_on_cases_local continuous_intros | erule continuous_on_subset | simp add: linear image_subset_iff)+
   1.290 +      apply (force simp: keq)
   1.291 +      done
   1.292 +    moreover have "k ` ({0..1} \<times> X) \<subseteq> Y"
   1.293 +      using Y by (force simp: k_def)
   1.294 +    moreover have "\<forall>x. k (0, x) = f x"
   1.295 +      by (simp add: k_def k12)
   1.296 +    moreover have "(\<forall>x. k (1, x) = h x)"
   1.297 +      by (simp add: k_def k12)
   1.298 +    moreover have "\<forall>t\<in>{0..1}. P (\<lambda>x. k (t, x))"
   1.299 +      using P
   1.300 +      apply (clarsimp simp add: k_def)
   1.301 +      apply (case_tac "t \<le> 1/2", auto)
   1.302 +      done
   1.303 +    ultimately have *: "\<exists>k :: real \<times> 'a \<Rightarrow> 'b.
   1.304 +                       continuous_on ({0..1} \<times> X) k \<and> k ` ({0..1} \<times> X) \<subseteq> Y \<and>
   1.305 +                       (\<forall>x. k (0, x) = f x) \<and> (\<forall>x. k (1, x) = h x) \<and> (\<forall>t\<in>{0..1}. P (\<lambda>x. k (t, x)))"
   1.306 +      by blast
   1.307 +  } note * = this
   1.308 +  show ?thesis
   1.309 +    using assms by (auto intro: * simp add: homotopic_with_def)
   1.310 +qed
   1.311 +
   1.312 +proposition homotopic_compose:
   1.313 +      fixes s :: "'a::real_normed_vector set"
   1.314 +      shows "\<lbrakk>homotopic_with (\<lambda>x. True) s t f f'; homotopic_with (\<lambda>x. True) t u g g'\<rbrakk>
   1.315 +             \<Longrightarrow> homotopic_with (\<lambda>x. True) s u (g \<circ> f) (g' \<circ> f')"
   1.316 +  apply (rule homotopic_with_trans [where g = "g \<circ> f'"])
   1.317 +  apply (metis homotopic_compose_continuous_left homotopic_with_imp_continuous homotopic_with_imp_subset1)
   1.318 +  by (metis homotopic_compose_continuous_right homotopic_with_imp_continuous homotopic_with_imp_subset2)
   1.319 +
   1.320 +
   1.321 +text\<open>Homotopic triviality implicitly incorporates path-connectedness.\<close>
   1.322 +lemma homotopic_triviality:
   1.323 +  fixes S :: "'a::real_normed_vector set"
   1.324 +  shows  "(\<forall>f g. continuous_on S f \<and> f ` S \<subseteq> T \<and>
   1.325 +                 continuous_on S g \<and> g ` S \<subseteq> T
   1.326 +                 \<longrightarrow> homotopic_with (\<lambda>x. True) S T f g) \<longleftrightarrow>
   1.327 +          (S = {} \<or> path_connected T) \<and>
   1.328 +          (\<forall>f. continuous_on S f \<and> f ` S \<subseteq> T \<longrightarrow> (\<exists>c. homotopic_with (\<lambda>x. True) S T f (\<lambda>x. c)))"
   1.329 +          (is "?lhs = ?rhs")
   1.330 +proof (cases "S = {} \<or> T = {}")
   1.331 +  case True then show ?thesis by auto
   1.332 +next
   1.333 +  case False show ?thesis
   1.334 +  proof
   1.335 +    assume LHS [rule_format]: ?lhs
   1.336 +    have pab: "path_component T a b" if "a \<in> T" "b \<in> T" for a b
   1.337 +    proof -
   1.338 +      have "homotopic_with (\<lambda>x. True) S T (\<lambda>x. a) (\<lambda>x. b)"
   1.339 +        by (simp add: LHS continuous_on_const image_subset_iff that)
   1.340 +      then show ?thesis
   1.341 +        using False homotopic_constant_maps by blast
   1.342 +    qed
   1.343 +      moreover
   1.344 +    have "\<exists>c. homotopic_with (\<lambda>x. True) S T f (\<lambda>x. c)" if "continuous_on S f" "f ` S \<subseteq> T" for f
   1.345 +      by (metis (full_types) False LHS equals0I homotopic_constant_maps homotopic_with_imp_continuous homotopic_with_imp_subset2 pab that)
   1.346 +    ultimately show ?rhs
   1.347 +      by (simp add: path_connected_component)
   1.348 +  next
   1.349 +    assume RHS: ?rhs
   1.350 +    with False have T: "path_connected T"
   1.351 +      by blast
   1.352 +    show ?lhs
   1.353 +    proof clarify
   1.354 +      fix f g
   1.355 +      assume "continuous_on S f" "f ` S \<subseteq> T" "continuous_on S g" "g ` S \<subseteq> T"
   1.356 +      obtain c d where c: "homotopic_with (\<lambda>x. True) S T f (\<lambda>x. c)" and d: "homotopic_with (\<lambda>x. True) S T g (\<lambda>x. d)"
   1.357 +        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
   1.358 +      then have "c \<in> T" "d \<in> T"
   1.359 +        using False homotopic_with_imp_subset2 by fastforce+
   1.360 +      with T have "path_component T c d"
   1.361 +        using path_connected_component by blast
   1.362 +      then have "homotopic_with (\<lambda>x. True) S T (\<lambda>x. c) (\<lambda>x. d)"
   1.363 +        by (simp add: homotopic_constant_maps)
   1.364 +      with c d show "homotopic_with (\<lambda>x. True) S T f g"
   1.365 +        by (meson homotopic_with_symD homotopic_with_trans)
   1.366 +    qed
   1.367 +  qed
   1.368 +qed
   1.369 +
   1.370 +
   1.371 +subsection\<open>Homotopy of paths, maintaining the same endpoints\<close>
   1.372 +
   1.373 +
   1.374 +definition%important homotopic_paths :: "['a set, real \<Rightarrow> 'a, real \<Rightarrow> 'a::topological_space] \<Rightarrow> bool"
   1.375 +  where
   1.376 +     "homotopic_paths s p q \<equiv>
   1.377 +       homotopic_with (\<lambda>r. pathstart r = pathstart p \<and> pathfinish r = pathfinish p) {0..1} s p q"
   1.378 +
   1.379 +lemma homotopic_paths:
   1.380 +   "homotopic_paths s p q \<longleftrightarrow>
   1.381 +      (\<exists>h. continuous_on ({0..1} \<times> {0..1}) h \<and>
   1.382 +          h ` ({0..1} \<times> {0..1}) \<subseteq> s \<and>
   1.383 +          (\<forall>x \<in> {0..1}. h(0,x) = p x) \<and>
   1.384 +          (\<forall>x \<in> {0..1}. h(1,x) = q x) \<and>
   1.385 +          (\<forall>t \<in> {0..1::real}. pathstart(h \<circ> Pair t) = pathstart p \<and>
   1.386 +                        pathfinish(h \<circ> Pair t) = pathfinish p))"
   1.387 +  by (auto simp: homotopic_paths_def homotopic_with pathstart_def pathfinish_def)
   1.388 +
   1.389 +proposition homotopic_paths_imp_pathstart:
   1.390 +     "homotopic_paths s p q \<Longrightarrow> pathstart p = pathstart q"
   1.391 +  by (metis (mono_tags, lifting) homotopic_paths_def homotopic_with_imp_property)
   1.392 +
   1.393 +proposition homotopic_paths_imp_pathfinish:
   1.394 +     "homotopic_paths s p q \<Longrightarrow> pathfinish p = pathfinish q"
   1.395 +  by (metis (mono_tags, lifting) homotopic_paths_def homotopic_with_imp_property)
   1.396 +
   1.397 +lemma homotopic_paths_imp_path:
   1.398 +     "homotopic_paths s p q \<Longrightarrow> path p \<and> path q"
   1.399 +  using homotopic_paths_def homotopic_with_imp_continuous path_def by blast
   1.400 +
   1.401 +lemma homotopic_paths_imp_subset:
   1.402 +     "homotopic_paths s p q \<Longrightarrow> path_image p \<subseteq> s \<and> path_image q \<subseteq> s"
   1.403 +  by (simp add: homotopic_paths_def homotopic_with_imp_subset1 homotopic_with_imp_subset2 path_image_def)
   1.404 +
   1.405 +proposition homotopic_paths_refl [simp]: "homotopic_paths s p p \<longleftrightarrow> path p \<and> path_image p \<subseteq> s"
   1.406 +by (simp add: homotopic_paths_def homotopic_with_refl path_def path_image_def)
   1.407 +
   1.408 +proposition homotopic_paths_sym: "homotopic_paths s p q \<Longrightarrow> homotopic_paths s q p"
   1.409 +  by (metis (mono_tags) homotopic_paths_def homotopic_paths_imp_pathfinish homotopic_paths_imp_pathstart homotopic_with_symD)
   1.410 +
   1.411 +proposition homotopic_paths_sym_eq: "homotopic_paths s p q \<longleftrightarrow> homotopic_paths s q p"
   1.412 +  by (metis homotopic_paths_sym)
   1.413 +
   1.414 +proposition homotopic_paths_trans [trans]:
   1.415 +     "\<lbrakk>homotopic_paths s p q; homotopic_paths s q r\<rbrakk> \<Longrightarrow> homotopic_paths s p r"
   1.416 +  apply (simp add: homotopic_paths_def)
   1.417 +  apply (rule homotopic_with_trans, assumption)
   1.418 +  by (metis (mono_tags, lifting) homotopic_with_imp_property homotopic_with_mono)
   1.419 +
   1.420 +proposition homotopic_paths_eq:
   1.421 +     "\<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"
   1.422 +  apply (simp add: homotopic_paths_def)
   1.423 +  apply (rule homotopic_with_eq)
   1.424 +  apply (auto simp: path_def homotopic_with_refl pathstart_def pathfinish_def path_image_def elim: continuous_on_eq)
   1.425 +  done
   1.426 +
   1.427 +proposition homotopic_paths_reparametrize:
   1.428 +  assumes "path p"
   1.429 +      and pips: "path_image p \<subseteq> s"
   1.430 +      and contf: "continuous_on {0..1} f"
   1.431 +      and f01:"f ` {0..1} \<subseteq> {0..1}"
   1.432 +      and [simp]: "f(0) = 0" "f(1) = 1"
   1.433 +      and q: "\<And>t. t \<in> {0..1} \<Longrightarrow> q(t) = p(f t)"
   1.434 +    shows "homotopic_paths s p q"
   1.435 +proof -
   1.436 +  have contp: "continuous_on {0..1} p"
   1.437 +    by (metis \<open>path p\<close> path_def)
   1.438 +  then have "continuous_on {0..1} (p \<circ> f)"
   1.439 +    using contf continuous_on_compose continuous_on_subset f01 by blast
   1.440 +  then have "path q"
   1.441 +    by (simp add: path_def) (metis q continuous_on_cong)
   1.442 +  have piqs: "path_image q \<subseteq> s"
   1.443 +    by (metis (no_types, hide_lams) pips f01 image_subset_iff path_image_def q)
   1.444 +  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"
   1.445 +    using f01 by force
   1.446 +  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
   1.447 +    using f01 [THEN subsetD, of "f b"] by (simp add: convex_bound_le)
   1.448 +  have "homotopic_paths s q p"
   1.449 +  proof (rule homotopic_paths_trans)
   1.450 +    show "homotopic_paths s q (p \<circ> f)"
   1.451 +      using q by (force intro: homotopic_paths_eq [OF  \<open>path q\<close> piqs])
   1.452 +  next
   1.453 +    show "homotopic_paths s (p \<circ> f) p"
   1.454 +      apply (simp add: homotopic_paths_def homotopic_with_def)
   1.455 +      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)
   1.456 +      apply (rule conjI contf continuous_intros continuous_on_subset [OF contp] | simp)+
   1.457 +      using pips [unfolded path_image_def]
   1.458 +      apply (auto simp: fb0 fb1 pathstart_def pathfinish_def)
   1.459 +      done
   1.460 +  qed
   1.461 +  then show ?thesis
   1.462 +    by (simp add: homotopic_paths_sym)
   1.463 +qed
   1.464 +
   1.465 +lemma homotopic_paths_subset: "\<lbrakk>homotopic_paths s p q; s \<subseteq> t\<rbrakk> \<Longrightarrow> homotopic_paths t p q"
   1.466 +  using homotopic_paths_def homotopic_with_subset_right by blast
   1.467 +
   1.468 +
   1.469 +text\<open> A slightly ad-hoc but useful lemma in constructing homotopies.\<close>
   1.470 +lemma homotopic_join_lemma:
   1.471 +  fixes q :: "[real,real] \<Rightarrow> 'a::topological_space"
   1.472 +  assumes p: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>y. p (fst y) (snd y))"
   1.473 +      and q: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>y. q (fst y) (snd y))"
   1.474 +      and pf: "\<And>t. t \<in> {0..1} \<Longrightarrow> pathfinish(p t) = pathstart(q t)"
   1.475 +    shows "continuous_on ({0..1} \<times> {0..1}) (\<lambda>y. (p(fst y) +++ q(fst y)) (snd y))"
   1.476 +proof -
   1.477 +  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))"
   1.478 +    by (rule ext) (simp)
   1.479 +  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))"
   1.480 +    by (rule ext) (simp)
   1.481 +  show ?thesis
   1.482 +    apply (simp add: joinpaths_def)
   1.483 +    apply (rule continuous_on_cases_le)
   1.484 +    apply (simp_all only: 1 2)
   1.485 +    apply (rule continuous_intros continuous_on_subset [OF p] continuous_on_subset [OF q] | force)+
   1.486 +    using pf
   1.487 +    apply (auto simp: mult.commute pathstart_def pathfinish_def)
   1.488 +    done
   1.489 +qed
   1.490 +
   1.491 +text\<open> Congruence properties of homotopy w.r.t. path-combining operations.\<close>
   1.492 +
   1.493 +lemma homotopic_paths_reversepath_D:
   1.494 +      assumes "homotopic_paths s p q"
   1.495 +      shows   "homotopic_paths s (reversepath p) (reversepath q)"
   1.496 +  using assms
   1.497 +  apply (simp add: homotopic_paths_def homotopic_with_def, clarify)
   1.498 +  apply (rule_tac x="h \<circ> (\<lambda>x. (fst x, 1 - snd x))" in exI)
   1.499 +  apply (rule conjI continuous_intros)+
   1.500 +  apply (auto simp: reversepath_def pathstart_def pathfinish_def elim!: continuous_on_subset)
   1.501 +  done
   1.502 +
   1.503 +proposition homotopic_paths_reversepath:
   1.504 +     "homotopic_paths s (reversepath p) (reversepath q) \<longleftrightarrow> homotopic_paths s p q"
   1.505 +  using homotopic_paths_reversepath_D by force
   1.506 +
   1.507 +
   1.508 +proposition homotopic_paths_join:
   1.509 +    "\<lbrakk>homotopic_paths s p p'; homotopic_paths s q q'; pathfinish p = pathstart q\<rbrakk> \<Longrightarrow> homotopic_paths s (p +++ q) (p' +++ q')"
   1.510 +  apply (simp add: homotopic_paths_def homotopic_with_def, clarify)
   1.511 +  apply (rename_tac k1 k2)
   1.512 +  apply (rule_tac x="(\<lambda>y. ((k1 \<circ> Pair (fst y)) +++ (k2 \<circ> Pair (fst y))) (snd y))" in exI)
   1.513 +  apply (rule conjI continuous_intros homotopic_join_lemma)+
   1.514 +  apply (auto simp: joinpaths_def pathstart_def pathfinish_def path_image_def)
   1.515 +  done
   1.516 +
   1.517 +proposition homotopic_paths_continuous_image:
   1.518 +    "\<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)"
   1.519 +  unfolding homotopic_paths_def
   1.520 +  apply (rule homotopic_with_compose_continuous_left [of _ _ _ s])
   1.521 +  apply (auto simp: pathstart_def pathfinish_def elim!: homotopic_with_mono)
   1.522 +  done
   1.523 +
   1.524 +
   1.525 +subsection\<open>Group properties for homotopy of paths\<close>
   1.526 +
   1.527 +text%important\<open>So taking equivalence classes under homotopy would give the fundamental group\<close>
   1.528 +
   1.529 +proposition homotopic_paths_rid:
   1.530 +    "\<lbrakk>path p; path_image p \<subseteq> s\<rbrakk> \<Longrightarrow> homotopic_paths s (p +++ linepath (pathfinish p) (pathfinish p)) p"
   1.531 +  apply (subst homotopic_paths_sym)
   1.532 +  apply (rule homotopic_paths_reparametrize [where f = "\<lambda>t. if  t \<le> 1 / 2 then 2 *\<^sub>R t else 1"])
   1.533 +  apply (simp_all del: le_divide_eq_numeral1)
   1.534 +  apply (subst split_01)
   1.535 +  apply (rule continuous_on_cases continuous_intros | force simp: pathfinish_def joinpaths_def)+
   1.536 +  done
   1.537 +
   1.538 +proposition homotopic_paths_lid:
   1.539 +   "\<lbrakk>path p; path_image p \<subseteq> s\<rbrakk> \<Longrightarrow> homotopic_paths s (linepath (pathstart p) (pathstart p) +++ p) p"
   1.540 +  using homotopic_paths_rid [of "reversepath p" s]
   1.541 +  by (metis homotopic_paths_reversepath path_image_reversepath path_reversepath pathfinish_linepath
   1.542 +        pathfinish_reversepath reversepath_joinpaths reversepath_linepath)
   1.543 +
   1.544 +proposition homotopic_paths_assoc:
   1.545 +   "\<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;
   1.546 +     pathfinish q = pathstart r\<rbrakk>
   1.547 +    \<Longrightarrow> homotopic_paths s (p +++ (q +++ r)) ((p +++ q) +++ r)"
   1.548 +  apply (subst homotopic_paths_sym)
   1.549 +  apply (rule homotopic_paths_reparametrize
   1.550 +           [where f = "\<lambda>t. if  t \<le> 1 / 2 then inverse 2 *\<^sub>R t
   1.551 +                           else if  t \<le> 3 / 4 then t - (1 / 4)
   1.552 +                           else 2 *\<^sub>R t - 1"])
   1.553 +  apply (simp_all del: le_divide_eq_numeral1)
   1.554 +  apply (simp add: subset_path_image_join)
   1.555 +  apply (rule continuous_on_cases_1 continuous_intros)+
   1.556 +  apply (auto simp: joinpaths_def)
   1.557 +  done
   1.558 +
   1.559 +proposition homotopic_paths_rinv:
   1.560 +  assumes "path p" "path_image p \<subseteq> s"
   1.561 +    shows "homotopic_paths s (p +++ reversepath p) (linepath (pathstart p) (pathstart p))"
   1.562 +proof -
   1.563 +  have "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. (subpath 0 (fst x) p +++ reversepath (subpath 0 (fst x) p)) (snd x))"
   1.564 +    using assms
   1.565 +    apply (simp add: joinpaths_def subpath_def reversepath_def path_def del: le_divide_eq_numeral1)
   1.566 +    apply (rule continuous_on_cases_le)
   1.567 +    apply (rule_tac [2] continuous_on_compose [of _ _ p, unfolded o_def])
   1.568 +    apply (rule continuous_on_compose [of _ _ p, unfolded o_def])
   1.569 +    apply (auto intro!: continuous_intros simp del: eq_divide_eq_numeral1)
   1.570 +    apply (force elim!: continuous_on_subset simp add: mult_le_one)+
   1.571 +    done
   1.572 +  then show ?thesis
   1.573 +    using assms
   1.574 +    apply (subst homotopic_paths_sym_eq)
   1.575 +    unfolding homotopic_paths_def homotopic_with_def
   1.576 +    apply (rule_tac x="(\<lambda>y. (subpath 0 (fst y) p +++ reversepath(subpath 0 (fst y) p)) (snd y))" in exI)
   1.577 +    apply (simp add: path_defs joinpaths_def subpath_def reversepath_def)
   1.578 +    apply (force simp: mult_le_one)
   1.579 +    done
   1.580 +qed
   1.581 +
   1.582 +proposition homotopic_paths_linv:
   1.583 +  assumes "path p" "path_image p \<subseteq> s"
   1.584 +    shows "homotopic_paths s (reversepath p +++ p) (linepath (pathfinish p) (pathfinish p))"
   1.585 +  using homotopic_paths_rinv [of "reversepath p" s] assms by simp
   1.586 +
   1.587 +
   1.588 +subsection\<open>Homotopy of loops without requiring preservation of endpoints\<close>
   1.589 +
   1.590 +definition%important homotopic_loops :: "'a::topological_space set \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> bool"  where
   1.591 + "homotopic_loops s p q \<equiv>
   1.592 +     homotopic_with (\<lambda>r. pathfinish r = pathstart r) {0..1} s p q"
   1.593 +
   1.594 +lemma homotopic_loops:
   1.595 +   "homotopic_loops s p q \<longleftrightarrow>
   1.596 +      (\<exists>h. continuous_on ({0..1::real} \<times> {0..1}) h \<and>
   1.597 +          image h ({0..1} \<times> {0..1}) \<subseteq> s \<and>
   1.598 +          (\<forall>x \<in> {0..1}. h(0,x) = p x) \<and>
   1.599 +          (\<forall>x \<in> {0..1}. h(1,x) = q x) \<and>
   1.600 +          (\<forall>t \<in> {0..1}. pathfinish(h \<circ> Pair t) = pathstart(h \<circ> Pair t)))"
   1.601 +  by (simp add: homotopic_loops_def pathstart_def pathfinish_def homotopic_with)
   1.602 +
   1.603 +proposition homotopic_loops_imp_loop:
   1.604 +     "homotopic_loops s p q \<Longrightarrow> pathfinish p = pathstart p \<and> pathfinish q = pathstart q"
   1.605 +using homotopic_with_imp_property homotopic_loops_def by blast
   1.606 +
   1.607 +proposition homotopic_loops_imp_path:
   1.608 +     "homotopic_loops s p q \<Longrightarrow> path p \<and> path q"
   1.609 +  unfolding homotopic_loops_def path_def
   1.610 +  using homotopic_with_imp_continuous by blast
   1.611 +
   1.612 +proposition homotopic_loops_imp_subset:
   1.613 +     "homotopic_loops s p q \<Longrightarrow> path_image p \<subseteq> s \<and> path_image q \<subseteq> s"
   1.614 +  unfolding homotopic_loops_def path_image_def
   1.615 +  by (metis homotopic_with_imp_subset1 homotopic_with_imp_subset2)
   1.616 +
   1.617 +proposition homotopic_loops_refl:
   1.618 +     "homotopic_loops s p p \<longleftrightarrow>
   1.619 +      path p \<and> path_image p \<subseteq> s \<and> pathfinish p = pathstart p"
   1.620 +  by (simp add: homotopic_loops_def homotopic_with_refl path_image_def path_def)
   1.621 +
   1.622 +proposition homotopic_loops_sym: "homotopic_loops s p q \<Longrightarrow> homotopic_loops s q p"
   1.623 +  by (simp add: homotopic_loops_def homotopic_with_sym)
   1.624 +
   1.625 +proposition homotopic_loops_sym_eq: "homotopic_loops s p q \<longleftrightarrow> homotopic_loops s q p"
   1.626 +  by (metis homotopic_loops_sym)
   1.627 +
   1.628 +proposition homotopic_loops_trans:
   1.629 +   "\<lbrakk>homotopic_loops s p q; homotopic_loops s q r\<rbrakk> \<Longrightarrow> homotopic_loops s p r"
   1.630 +  unfolding homotopic_loops_def by (blast intro: homotopic_with_trans)
   1.631 +
   1.632 +proposition homotopic_loops_subset:
   1.633 +   "\<lbrakk>homotopic_loops s p q; s \<subseteq> t\<rbrakk> \<Longrightarrow> homotopic_loops t p q"
   1.634 +  by (simp add: homotopic_loops_def homotopic_with_subset_right)
   1.635 +
   1.636 +proposition homotopic_loops_eq:
   1.637 +   "\<lbrakk>path p; path_image p \<subseteq> s; pathfinish p = pathstart p; \<And>t. t \<in> {0..1} \<Longrightarrow> p(t) = q(t)\<rbrakk>
   1.638 +          \<Longrightarrow> homotopic_loops s p q"
   1.639 +  unfolding homotopic_loops_def
   1.640 +  apply (rule homotopic_with_eq)
   1.641 +  apply (rule homotopic_with_refl [where f = p, THEN iffD2])
   1.642 +  apply (simp_all add: path_image_def path_def pathstart_def pathfinish_def)
   1.643 +  done
   1.644 +
   1.645 +proposition homotopic_loops_continuous_image:
   1.646 +   "\<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)"
   1.647 +  unfolding homotopic_loops_def
   1.648 +  apply (rule homotopic_with_compose_continuous_left)
   1.649 +  apply (erule homotopic_with_mono)
   1.650 +  by (simp add: pathfinish_def pathstart_def)
   1.651 +
   1.652 +
   1.653 +subsection\<open>Relations between the two variants of homotopy\<close>
   1.654 +
   1.655 +proposition homotopic_paths_imp_homotopic_loops:
   1.656 +    "\<lbrakk>homotopic_paths s p q; pathfinish p = pathstart p; pathfinish q = pathstart p\<rbrakk> \<Longrightarrow> homotopic_loops s p q"
   1.657 +  by (auto simp: homotopic_paths_def homotopic_loops_def intro: homotopic_with_mono)
   1.658 +
   1.659 +proposition homotopic_loops_imp_homotopic_paths_null:
   1.660 +  assumes "homotopic_loops s p (linepath a a)"
   1.661 +    shows "homotopic_paths s p (linepath (pathstart p) (pathstart p))"
   1.662 +proof -
   1.663 +  have "path p" by (metis assms homotopic_loops_imp_path)
   1.664 +  have ploop: "pathfinish p = pathstart p" by (metis assms homotopic_loops_imp_loop)
   1.665 +  have pip: "path_image p \<subseteq> s" by (metis assms homotopic_loops_imp_subset)
   1.666 +  obtain h where conth: "continuous_on ({0..1::real} \<times> {0..1}) h"
   1.667 +             and hs: "h ` ({0..1} \<times> {0..1}) \<subseteq> s"
   1.668 +             and [simp]: "\<And>x. x \<in> {0..1} \<Longrightarrow> h(0,x) = p x"
   1.669 +             and [simp]: "\<And>x. x \<in> {0..1} \<Longrightarrow> h(1,x) = a"
   1.670 +             and ends: "\<And>t. t \<in> {0..1} \<Longrightarrow> pathfinish (h \<circ> Pair t) = pathstart (h \<circ> Pair t)"
   1.671 +    using assms by (auto simp: homotopic_loops homotopic_with)
   1.672 +  have conth0: "path (\<lambda>u. h (u, 0))"
   1.673 +    unfolding path_def
   1.674 +    apply (rule continuous_on_compose [of _ _ h, unfolded o_def])
   1.675 +    apply (force intro: continuous_intros continuous_on_subset [OF conth])+
   1.676 +    done
   1.677 +  have pih0: "path_image (\<lambda>u. h (u, 0)) \<subseteq> s"
   1.678 +    using hs by (force simp: path_image_def)
   1.679 +  have c1: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. h (fst x * snd x, 0))"
   1.680 +    apply (rule continuous_on_compose [of _ _ h, unfolded o_def])
   1.681 +    apply (force simp: mult_le_one intro: continuous_intros continuous_on_subset [OF conth])+
   1.682 +    done
   1.683 +  have c2: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. h (fst x - fst x * snd x, 0))"
   1.684 +    apply (rule continuous_on_compose [of _ _ h, unfolded o_def])
   1.685 +    apply (force simp: mult_left_le mult_le_one intro: continuous_intros continuous_on_subset [OF conth])+
   1.686 +    apply (rule continuous_on_subset [OF conth])
   1.687 +    apply (auto simp: algebra_simps add_increasing2 mult_left_le)
   1.688 +    done
   1.689 +  have [simp]: "\<And>t. \<lbrakk>0 \<le> t \<and> t \<le> 1\<rbrakk> \<Longrightarrow> h (t, 1) = h (t, 0)"
   1.690 +    using ends by (simp add: pathfinish_def pathstart_def)
   1.691 +  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
   1.692 +  proof -
   1.693 +    have "c * 3 \<le> c * (d * 4)" using that less_eq_real_def by auto
   1.694 +    with \<open>c \<le> 1\<close> show ?thesis by fastforce
   1.695 +  qed
   1.696 +  have *: "\<And>p x. (path p \<and> path(reversepath p)) \<and>
   1.697 +                  (path_image p \<subseteq> s \<and> path_image(reversepath p) \<subseteq> s) \<and>
   1.698 +                  (pathfinish p = pathstart(linepath a a +++ reversepath p) \<and>
   1.699 +                   pathstart(reversepath p) = a) \<and> pathstart p = x
   1.700 +                  \<Longrightarrow> homotopic_paths s (p +++ linepath a a +++ reversepath p) (linepath x x)"
   1.701 +    by (metis homotopic_paths_lid homotopic_paths_join
   1.702 +              homotopic_paths_trans homotopic_paths_sym homotopic_paths_rinv)
   1.703 +  have 1: "homotopic_paths s p (p +++ linepath (pathfinish p) (pathfinish p))"
   1.704 +    using \<open>path p\<close> homotopic_paths_rid homotopic_paths_sym pip by blast
   1.705 +  moreover have "homotopic_paths s (p +++ linepath (pathfinish p) (pathfinish p))
   1.706 +                                   (linepath (pathstart p) (pathstart p) +++ p +++ linepath (pathfinish p) (pathfinish p))"
   1.707 +    apply (rule homotopic_paths_sym)
   1.708 +    using homotopic_paths_lid [of "p +++ linepath (pathfinish p) (pathfinish p)" s]
   1.709 +    by (metis 1 homotopic_paths_imp_path homotopic_paths_imp_pathstart homotopic_paths_imp_subset)
   1.710 +  moreover have "homotopic_paths s (linepath (pathstart p) (pathstart p) +++ p +++ linepath (pathfinish p) (pathfinish p))
   1.711 +                                   ((\<lambda>u. h (u, 0)) +++ linepath a a +++ reversepath (\<lambda>u. h (u, 0)))"
   1.712 +    apply (simp add: homotopic_paths_def homotopic_with_def)
   1.713 +    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)
   1.714 +    apply (simp add: subpath_reversepath)
   1.715 +    apply (intro conjI homotopic_join_lemma)
   1.716 +    using ploop
   1.717 +    apply (simp_all add: path_defs joinpaths_def o_def subpath_def conth c1 c2)
   1.718 +    apply (force simp: algebra_simps mult_le_one mult_left_le intro: hs [THEN subsetD] adhoc_le)
   1.719 +    done
   1.720 +  moreover have "homotopic_paths s ((\<lambda>u. h (u, 0)) +++ linepath a a +++ reversepath (\<lambda>u. h (u, 0)))
   1.721 +                                   (linepath (pathstart p) (pathstart p))"
   1.722 +    apply (rule *)
   1.723 +    apply (simp add: pih0 pathstart_def pathfinish_def conth0)
   1.724 +    apply (simp add: reversepath_def joinpaths_def)
   1.725 +    done
   1.726 +  ultimately show ?thesis
   1.727 +    by (blast intro: homotopic_paths_trans)
   1.728 +qed
   1.729 +
   1.730 +proposition homotopic_loops_conjugate:
   1.731 +  fixes s :: "'a::real_normed_vector set"
   1.732 +  assumes "path p" "path q" and pip: "path_image p \<subseteq> s" and piq: "path_image q \<subseteq> s"
   1.733 +      and papp: "pathfinish p = pathstart q" and qloop: "pathfinish q = pathstart q"
   1.734 +    shows "homotopic_loops s (p +++ q +++ reversepath p) q"
   1.735 +proof -
   1.736 +  have contp: "continuous_on {0..1} p"  using \<open>path p\<close> [unfolded path_def] by blast
   1.737 +  have contq: "continuous_on {0..1} q"  using \<open>path q\<close> [unfolded path_def] by blast
   1.738 +  have c1: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. p ((1 - fst x) * snd x + fst x))"
   1.739 +    apply (rule continuous_on_compose [of _ _ p, unfolded o_def])
   1.740 +    apply (force simp: mult_le_one intro!: continuous_intros)
   1.741 +    apply (rule continuous_on_subset [OF contp])
   1.742 +    apply (auto simp: algebra_simps add_increasing2 mult_right_le_one_le sum_le_prod1)
   1.743 +    done
   1.744 +  have c2: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. p ((fst x - 1) * snd x + 1))"
   1.745 +    apply (rule continuous_on_compose [of _ _ p, unfolded o_def])
   1.746 +    apply (force simp: mult_le_one intro!: continuous_intros)
   1.747 +    apply (rule continuous_on_subset [OF contp])
   1.748 +    apply (auto simp: algebra_simps add_increasing2 mult_left_le_one_le)
   1.749 +    done
   1.750 +  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"
   1.751 +    using sum_le_prod1
   1.752 +    by (force simp: algebra_simps add_increasing2 mult_left_le intro: pip [unfolded path_image_def, THEN subsetD])
   1.753 +  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"
   1.754 +    apply (rule pip [unfolded path_image_def, THEN subsetD])
   1.755 +    apply (rule image_eqI, blast)
   1.756 +    apply (simp add: algebra_simps)
   1.757 +    by (metis add_mono_thms_linordered_semiring(1) affine_ineq linear mult.commute mult.left_neutral mult_right_mono not_le
   1.758 +              add.commute zero_le_numeral)
   1.759 +  have qs: "\<And>a b. \<lbrakk>4 * b \<le> 3; \<not> b * 2 \<le> 1\<rbrakk> \<Longrightarrow> q (4 * b - 2) \<in> s"
   1.760 +    using path_image_def piq by fastforce
   1.761 +  have "homotopic_loops s (p +++ q +++ reversepath p)
   1.762 +                          (linepath (pathstart q) (pathstart q) +++ q +++ linepath (pathstart q) (pathstart q))"
   1.763 +    apply (simp add: homotopic_loops_def homotopic_with_def)
   1.764 +    apply (rule_tac x="(\<lambda>y. (subpath (fst y) 1 p +++ q +++ subpath 1 (fst y) p) (snd y))" in exI)
   1.765 +    apply (simp add: subpath_refl subpath_reversepath)
   1.766 +    apply (intro conjI homotopic_join_lemma)
   1.767 +    using papp qloop
   1.768 +    apply (simp_all add: path_defs joinpaths_def o_def subpath_def c1 c2)
   1.769 +    apply (force simp: contq intro: continuous_on_compose [of _ _ q, unfolded o_def] continuous_on_id continuous_on_snd)
   1.770 +    apply (auto simp: ps1 ps2 qs)
   1.771 +    done
   1.772 +  moreover have "homotopic_loops s (linepath (pathstart q) (pathstart q) +++ q +++ linepath (pathstart q) (pathstart q)) q"
   1.773 +  proof -
   1.774 +    have "homotopic_paths s (linepath (pathfinish q) (pathfinish q) +++ q) q"
   1.775 +      using \<open>path q\<close> homotopic_paths_lid qloop piq by auto
   1.776 +    hence 1: "\<And>f. homotopic_paths s f q \<or> \<not> homotopic_paths s f (linepath (pathfinish q) (pathfinish q) +++ q)"
   1.777 +      using homotopic_paths_trans by blast
   1.778 +    hence "homotopic_paths s (linepath (pathfinish q) (pathfinish q) +++ q +++ linepath (pathfinish q) (pathfinish q)) q"
   1.779 +    proof -
   1.780 +      have "homotopic_paths s (q +++ linepath (pathfinish q) (pathfinish q)) q"
   1.781 +        by (simp add: \<open>path q\<close> homotopic_paths_rid piq)
   1.782 +      thus ?thesis
   1.783 +        by (metis (no_types) 1 \<open>path q\<close> homotopic_paths_join homotopic_paths_rinv homotopic_paths_sym
   1.784 +                  homotopic_paths_trans qloop pathfinish_linepath piq)
   1.785 +    qed
   1.786 +    thus ?thesis
   1.787 +      by (metis (no_types) qloop homotopic_loops_sym homotopic_paths_imp_homotopic_loops homotopic_paths_imp_pathfinish homotopic_paths_sym)
   1.788 +  qed
   1.789 +  ultimately show ?thesis
   1.790 +    by (blast intro: homotopic_loops_trans)
   1.791 +qed
   1.792 +
   1.793 +lemma homotopic_paths_loop_parts:
   1.794 +  assumes loops: "homotopic_loops S (p +++ reversepath q) (linepath a a)" and "path q"
   1.795 +  shows "homotopic_paths S p q"
   1.796 +proof -
   1.797 +  have paths: "homotopic_paths S (p +++ reversepath q) (linepath (pathstart p) (pathstart p))"
   1.798 +    using homotopic_loops_imp_homotopic_paths_null [OF loops] by simp
   1.799 +  then have "path p"
   1.800 +    using \<open>path q\<close> homotopic_loops_imp_path loops path_join path_join_path_ends path_reversepath by blast
   1.801 +  show ?thesis
   1.802 +  proof (cases "pathfinish p = pathfinish q")
   1.803 +    case True
   1.804 +    have pipq: "path_image p \<subseteq> S" "path_image q \<subseteq> S"
   1.805 +      by (metis Un_subset_iff paths \<open>path p\<close> \<open>path q\<close> homotopic_loops_imp_subset homotopic_paths_imp_path loops
   1.806 +           path_image_join path_image_reversepath path_imp_reversepath path_join_eq)+
   1.807 +    have "homotopic_paths S p (p +++ (linepath (pathfinish p) (pathfinish p)))"
   1.808 +      using \<open>path p\<close> \<open>path_image p \<subseteq> S\<close> homotopic_paths_rid homotopic_paths_sym by blast
   1.809 +    moreover have "homotopic_paths S (p +++ (linepath (pathfinish p) (pathfinish p))) (p +++ (reversepath q +++ q))"
   1.810 +      by (simp add: True \<open>path p\<close> \<open>path q\<close> pipq homotopic_paths_join homotopic_paths_linv homotopic_paths_sym)
   1.811 +    moreover have "homotopic_paths S (p +++ (reversepath q +++ q)) ((p +++ reversepath q) +++ q)"
   1.812 +      by (simp add: True \<open>path p\<close> \<open>path q\<close> homotopic_paths_assoc pipq)
   1.813 +    moreover have "homotopic_paths S ((p +++ reversepath q) +++ q) (linepath (pathstart p) (pathstart p) +++ q)"
   1.814 +      by (simp add: \<open>path q\<close> homotopic_paths_join paths pipq)
   1.815 +    moreover then have "homotopic_paths S (linepath (pathstart p) (pathstart p) +++ q) q"
   1.816 +      by (metis \<open>path q\<close> homotopic_paths_imp_path homotopic_paths_lid linepath_trivial path_join_path_ends pathfinish_def pipq(2))
   1.817 +    ultimately show ?thesis
   1.818 +      using homotopic_paths_trans by metis
   1.819 +  next
   1.820 +    case False
   1.821 +    then show ?thesis
   1.822 +      using \<open>path q\<close> homotopic_loops_imp_path loops path_join_path_ends by fastforce
   1.823 +  qed
   1.824 +qed
   1.825 +
   1.826 +
   1.827 +subsection%unimportant\<open>Homotopy of "nearby" function, paths and loops\<close>
   1.828 +
   1.829 +lemma homotopic_with_linear:
   1.830 +  fixes f g :: "_ \<Rightarrow> 'b::real_normed_vector"
   1.831 +  assumes contf: "continuous_on s f"
   1.832 +      and contg:"continuous_on s g"
   1.833 +      and sub: "\<And>x. x \<in> s \<Longrightarrow> closed_segment (f x) (g x) \<subseteq> t"
   1.834 +    shows "homotopic_with (\<lambda>z. True) s t f g"
   1.835 +  apply (simp add: homotopic_with_def)
   1.836 +  apply (rule_tac x="\<lambda>y. ((1 - (fst y)) *\<^sub>R f(snd y) + (fst y) *\<^sub>R g(snd y))" in exI)
   1.837 +  apply (intro conjI)
   1.838 +  apply (rule subset_refl continuous_intros continuous_on_subset [OF contf] continuous_on_compose2 [where g=f]
   1.839 +                                            continuous_on_subset [OF contg] continuous_on_compose2 [where g=g]| simp)+
   1.840 +  using sub closed_segment_def apply fastforce+
   1.841 +  done
   1.842 +
   1.843 +lemma homotopic_paths_linear:
   1.844 +  fixes g h :: "real \<Rightarrow> 'a::real_normed_vector"
   1.845 +  assumes "path g" "path h" "pathstart h = pathstart g" "pathfinish h = pathfinish g"
   1.846 +          "\<And>t. t \<in> {0..1} \<Longrightarrow> closed_segment (g t) (h t) \<subseteq> s"
   1.847 +    shows "homotopic_paths s g h"
   1.848 +  using assms
   1.849 +  unfolding path_def
   1.850 +  apply (simp add: closed_segment_def pathstart_def pathfinish_def homotopic_paths_def homotopic_with_def)
   1.851 +  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)
   1.852 +  apply (intro conjI subsetI continuous_intros; force)
   1.853 +  done
   1.854 +
   1.855 +lemma homotopic_loops_linear:
   1.856 +  fixes g h :: "real \<Rightarrow> 'a::real_normed_vector"
   1.857 +  assumes "path g" "path h" "pathfinish g = pathstart g" "pathfinish h = pathstart h"
   1.858 +          "\<And>t x. t \<in> {0..1} \<Longrightarrow> closed_segment (g t) (h t) \<subseteq> s"
   1.859 +    shows "homotopic_loops s g h"
   1.860 +  using assms
   1.861 +  unfolding path_def
   1.862 +  apply (simp add: pathstart_def pathfinish_def homotopic_loops_def homotopic_with_def)
   1.863 +  apply (rule_tac x="\<lambda>y. ((1 - (fst y)) *\<^sub>R g(snd y) + (fst y) *\<^sub>R h(snd y))" in exI)
   1.864 +  apply (auto intro!: continuous_intros intro: continuous_on_compose2 [where g=g] continuous_on_compose2 [where g=h])
   1.865 +  apply (force simp: closed_segment_def)
   1.866 +  done
   1.867 +
   1.868 +lemma homotopic_paths_nearby_explicit:
   1.869 +  assumes "path g" "path h" "pathstart h = pathstart g" "pathfinish h = pathfinish g"
   1.870 +      and no: "\<And>t x. \<lbrakk>t \<in> {0..1}; x \<notin> s\<rbrakk> \<Longrightarrow> norm(h t - g t) < norm(g t - x)"
   1.871 +    shows "homotopic_paths s g h"
   1.872 +  apply (rule homotopic_paths_linear [OF assms(1-4)])
   1.873 +  by (metis no segment_bound(1) subsetI norm_minus_commute not_le)
   1.874 +
   1.875 +lemma homotopic_loops_nearby_explicit:
   1.876 +  assumes "path g" "path h" "pathfinish g = pathstart g" "pathfinish h = pathstart h"
   1.877 +      and no: "\<And>t x. \<lbrakk>t \<in> {0..1}; x \<notin> s\<rbrakk> \<Longrightarrow> norm(h t - g t) < norm(g t - x)"
   1.878 +    shows "homotopic_loops s g h"
   1.879 +  apply (rule homotopic_loops_linear [OF assms(1-4)])
   1.880 +  by (metis no segment_bound(1) subsetI norm_minus_commute not_le)
   1.881 +
   1.882 +lemma homotopic_nearby_paths:
   1.883 +  fixes g h :: "real \<Rightarrow> 'a::euclidean_space"
   1.884 +  assumes "path g" "open s" "path_image g \<subseteq> s"
   1.885 +    shows "\<exists>e. 0 < e \<and>
   1.886 +               (\<forall>h. path h \<and>
   1.887 +                    pathstart h = pathstart g \<and> pathfinish h = pathfinish g \<and>
   1.888 +                    (\<forall>t \<in> {0..1}. norm(h t - g t) < e) \<longrightarrow> homotopic_paths s g h)"
   1.889 +proof -
   1.890 +  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"
   1.891 +    using separate_compact_closed [of "path_image g" "-s"] assms by force
   1.892 +  show ?thesis
   1.893 +    apply (intro exI conjI)
   1.894 +    using e [unfolded dist_norm]
   1.895 +    apply (auto simp: intro!: homotopic_paths_nearby_explicit assms  \<open>e > 0\<close>)
   1.896 +    by (metis atLeastAtMost_iff imageI le_less_trans not_le path_image_def)
   1.897 +qed
   1.898 +
   1.899 +lemma homotopic_nearby_loops:
   1.900 +  fixes g h :: "real \<Rightarrow> 'a::euclidean_space"
   1.901 +  assumes "path g" "open s" "path_image g \<subseteq> s" "pathfinish g = pathstart g"
   1.902 +    shows "\<exists>e. 0 < e \<and>
   1.903 +               (\<forall>h. path h \<and> pathfinish h = pathstart h \<and>
   1.904 +                    (\<forall>t \<in> {0..1}. norm(h t - g t) < e) \<longrightarrow> homotopic_loops s g h)"
   1.905 +proof -
   1.906 +  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"
   1.907 +    using separate_compact_closed [of "path_image g" "-s"] assms by force
   1.908 +  show ?thesis
   1.909 +    apply (intro exI conjI)
   1.910 +    using e [unfolded dist_norm]
   1.911 +    apply (auto simp: intro!: homotopic_loops_nearby_explicit assms  \<open>e > 0\<close>)
   1.912 +    by (metis atLeastAtMost_iff imageI le_less_trans not_le path_image_def)
   1.913 +qed
   1.914 +
   1.915 +
   1.916 +subsection\<open> Homotopy and subpaths\<close>
   1.917 +
   1.918 +lemma homotopic_join_subpaths1:
   1.919 +  assumes "path g" and pag: "path_image g \<subseteq> s"
   1.920 +      and u: "u \<in> {0..1}" and v: "v \<in> {0..1}" and w: "w \<in> {0..1}" "u \<le> v" "v \<le> w"
   1.921 +    shows "homotopic_paths s (subpath u v g +++ subpath v w g) (subpath u w g)"
   1.922 +proof -
   1.923 +  have 1: "t * 2 \<le> 1 \<Longrightarrow> u + t * (v * 2) \<le> v + t * (u * 2)" for t
   1.924 +    using affine_ineq \<open>u \<le> v\<close> by fastforce
   1.925 +  have 2: "t * 2 > 1 \<Longrightarrow> u + (2*t - 1) * v \<le> v + (2*t - 1) * w" for t
   1.926 +    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>)
   1.927 +  have t2: "\<And>t::real. t*2 = 1 \<Longrightarrow> t = 1/2" by auto
   1.928 +  show ?thesis
   1.929 +    apply (rule homotopic_paths_subset [OF _ pag])
   1.930 +    using assms
   1.931 +    apply (cases "w = u")
   1.932 +    using homotopic_paths_rinv [of "subpath u v g" "path_image g"]
   1.933 +    apply (force simp: closed_segment_eq_real_ivl image_mono path_image_def subpath_refl)
   1.934 +      apply (rule homotopic_paths_sym)
   1.935 +      apply (rule homotopic_paths_reparametrize
   1.936 +             [where f = "\<lambda>t. if  t \<le> 1 / 2
   1.937 +                             then inverse((w - u)) *\<^sub>R (2 * (v - u)) *\<^sub>R t
   1.938 +                             else inverse((w - u)) *\<^sub>R ((v - u) + (w - v) *\<^sub>R (2 *\<^sub>R t - 1))"])
   1.939 +      using \<open>path g\<close> path_subpath u w apply blast
   1.940 +      using \<open>path g\<close> path_image_subpath_subset u w(1) apply blast
   1.941 +      apply simp_all
   1.942 +      apply (subst split_01)
   1.943 +      apply (rule continuous_on_cases continuous_intros | force simp: pathfinish_def joinpaths_def)+
   1.944 +      apply (simp_all add: field_simps not_le)
   1.945 +      apply (force dest!: t2)
   1.946 +      apply (force simp: algebra_simps mult_left_mono affine_ineq dest!: 1 2)
   1.947 +      apply (simp add: joinpaths_def subpath_def)
   1.948 +      apply (force simp: algebra_simps)
   1.949 +      done
   1.950 +qed
   1.951 +
   1.952 +lemma homotopic_join_subpaths2:
   1.953 +  assumes "homotopic_paths s (subpath u v g +++ subpath v w g) (subpath u w g)"
   1.954 +    shows "homotopic_paths s (subpath w v g +++ subpath v u g) (subpath w u g)"
   1.955 +by (metis assms homotopic_paths_reversepath_D pathfinish_subpath pathstart_subpath reversepath_joinpaths reversepath_subpath)
   1.956 +
   1.957 +lemma homotopic_join_subpaths3:
   1.958 +  assumes hom: "homotopic_paths s (subpath u v g +++ subpath v w g) (subpath u w g)"
   1.959 +      and "path g" and pag: "path_image g \<subseteq> s"
   1.960 +      and u: "u \<in> {0..1}" and v: "v \<in> {0..1}" and w: "w \<in> {0..1}"
   1.961 +    shows "homotopic_paths s (subpath v w g +++ subpath w u g) (subpath v u g)"
   1.962 +proof -
   1.963 +  have "homotopic_paths s (subpath u w g +++ subpath w v g) ((subpath u v g +++ subpath v w g) +++ subpath w v g)"
   1.964 +    apply (rule homotopic_paths_join)
   1.965 +    using hom homotopic_paths_sym_eq apply blast
   1.966 +    apply (metis \<open>path g\<close> homotopic_paths_eq pag path_image_subpath_subset path_subpath subset_trans v w, simp)
   1.967 +    done
   1.968 +  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)"
   1.969 +    apply (rule homotopic_paths_sym [OF homotopic_paths_assoc])
   1.970 +    using assms by (simp_all add: path_image_subpath_subset [THEN order_trans])
   1.971 +  also have "homotopic_paths s (subpath u v g +++ subpath v w g +++ subpath w v g)
   1.972 +                               (subpath u v g +++ linepath (pathfinish (subpath u v g)) (pathfinish (subpath u v g)))"
   1.973 +    apply (rule homotopic_paths_join)
   1.974 +    apply (metis \<open>path g\<close> homotopic_paths_eq order.trans pag path_image_subpath_subset path_subpath u v)
   1.975 +    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)
   1.976 +    apply simp
   1.977 +    done
   1.978 +  also have "homotopic_paths s (subpath u v g +++ linepath (pathfinish (subpath u v g)) (pathfinish (subpath u v g))) (subpath u v g)"
   1.979 +    apply (rule homotopic_paths_rid)
   1.980 +    using \<open>path g\<close> path_subpath u v apply blast
   1.981 +    apply (meson \<open>path g\<close> order.trans pag path_image_subpath_subset u v)
   1.982 +    done
   1.983 +  finally have "homotopic_paths s (subpath u w g +++ subpath w v g) (subpath u v g)" .
   1.984 +  then show ?thesis
   1.985 +    using homotopic_join_subpaths2 by blast
   1.986 +qed
   1.987 +
   1.988 +proposition homotopic_join_subpaths:
   1.989 +   "\<lbrakk>path g; path_image g \<subseteq> s; u \<in> {0..1}; v \<in> {0..1}; w \<in> {0..1}\<rbrakk>
   1.990 +    \<Longrightarrow> homotopic_paths s (subpath u v g +++ subpath v w g) (subpath u w g)"
   1.991 +  apply (rule le_cases3 [of u v w])
   1.992 +using homotopic_join_subpaths1 homotopic_join_subpaths2 homotopic_join_subpaths3 by metis+
   1.993 +
   1.994 +text\<open>Relating homotopy of trivial loops to path-connectedness.\<close>
   1.995 +
   1.996 +lemma path_component_imp_homotopic_points:
   1.997 +    "path_component S a b \<Longrightarrow> homotopic_loops S (linepath a a) (linepath b b)"
   1.998 +apply (simp add: path_component_def homotopic_loops_def homotopic_with_def
   1.999 +                 pathstart_def pathfinish_def path_image_def path_def, clarify)
  1.1000 +apply (rule_tac x="g \<circ> fst" in exI)
  1.1001 +apply (intro conjI continuous_intros continuous_on_compose)+
  1.1002 +apply (auto elim!: continuous_on_subset)
  1.1003 +done
  1.1004 +
  1.1005 +lemma homotopic_loops_imp_path_component_value:
  1.1006 +   "\<lbrakk>homotopic_loops S p q; 0 \<le> t; t \<le> 1\<rbrakk>
  1.1007 +        \<Longrightarrow> path_component S (p t) (q t)"
  1.1008 +apply (simp add: path_component_def homotopic_loops_def homotopic_with_def
  1.1009 +                 pathstart_def pathfinish_def path_image_def path_def, clarify)
  1.1010 +apply (rule_tac x="h \<circ> (\<lambda>u. (u, t))" in exI)
  1.1011 +apply (intro conjI continuous_intros continuous_on_compose)+
  1.1012 +apply (auto elim!: continuous_on_subset)
  1.1013 +done
  1.1014 +
  1.1015 +lemma homotopic_points_eq_path_component:
  1.1016 +   "homotopic_loops S (linepath a a) (linepath b b) \<longleftrightarrow>
  1.1017 +        path_component S a b"
  1.1018 +by (auto simp: path_component_imp_homotopic_points
  1.1019 +         dest: homotopic_loops_imp_path_component_value [where t=1])
  1.1020 +
  1.1021 +lemma path_connected_eq_homotopic_points:
  1.1022 +    "path_connected S \<longleftrightarrow>
  1.1023 +      (\<forall>a b. a \<in> S \<and> b \<in> S \<longrightarrow> homotopic_loops S (linepath a a) (linepath b b))"
  1.1024 +by (auto simp: path_connected_def path_component_def homotopic_points_eq_path_component)
  1.1025 +
  1.1026 +
  1.1027 +subsection\<open>Simply connected sets\<close>
  1.1028 +
  1.1029 +text%important\<open>defined as "all loops are homotopic (as loops)\<close>
  1.1030 +
  1.1031 +definition%important simply_connected where
  1.1032 +  "simply_connected S \<equiv>
  1.1033 +        \<forall>p q. path p \<and> pathfinish p = pathstart p \<and> path_image p \<subseteq> S \<and>
  1.1034 +              path q \<and> pathfinish q = pathstart q \<and> path_image q \<subseteq> S
  1.1035 +              \<longrightarrow> homotopic_loops S p q"
  1.1036 +
  1.1037 +lemma simply_connected_empty [iff]: "simply_connected {}"
  1.1038 +  by (simp add: simply_connected_def)
  1.1039 +
  1.1040 +lemma simply_connected_imp_path_connected:
  1.1041 +  fixes S :: "_::real_normed_vector set"
  1.1042 +  shows "simply_connected S \<Longrightarrow> path_connected S"
  1.1043 +by (simp add: simply_connected_def path_connected_eq_homotopic_points)
  1.1044 +
  1.1045 +lemma simply_connected_imp_connected:
  1.1046 +  fixes S :: "_::real_normed_vector set"
  1.1047 +  shows "simply_connected S \<Longrightarrow> connected S"
  1.1048 +by (simp add: path_connected_imp_connected simply_connected_imp_path_connected)
  1.1049 +
  1.1050 +lemma simply_connected_eq_contractible_loop_any:
  1.1051 +  fixes S :: "_::real_normed_vector set"
  1.1052 +  shows "simply_connected S \<longleftrightarrow>
  1.1053 +            (\<forall>p a. path p \<and> path_image p \<subseteq> S \<and>
  1.1054 +                  pathfinish p = pathstart p \<and> a \<in> S
  1.1055 +                  \<longrightarrow> homotopic_loops S p (linepath a a))"
  1.1056 +apply (simp add: simply_connected_def)
  1.1057 +apply (rule iffI, force, clarify)
  1.1058 +apply (rule_tac q = "linepath (pathstart p) (pathstart p)" in homotopic_loops_trans)
  1.1059 +apply (fastforce simp add:)
  1.1060 +using homotopic_loops_sym apply blast
  1.1061 +done
  1.1062 +
  1.1063 +lemma simply_connected_eq_contractible_loop_some:
  1.1064 +  fixes S :: "_::real_normed_vector set"
  1.1065 +  shows "simply_connected S \<longleftrightarrow>
  1.1066 +                path_connected S \<and>
  1.1067 +                (\<forall>p. path p \<and> path_image p \<subseteq> S \<and> pathfinish p = pathstart p
  1.1068 +                    \<longrightarrow> (\<exists>a. a \<in> S \<and> homotopic_loops S p (linepath a a)))"
  1.1069 +apply (rule iffI)
  1.1070 + apply (fastforce simp: simply_connected_imp_path_connected simply_connected_eq_contractible_loop_any)
  1.1071 +apply (clarsimp simp add: simply_connected_eq_contractible_loop_any)
  1.1072 +apply (drule_tac x=p in spec)
  1.1073 +using homotopic_loops_trans path_connected_eq_homotopic_points
  1.1074 +  apply blast
  1.1075 +done
  1.1076 +
  1.1077 +lemma simply_connected_eq_contractible_loop_all:
  1.1078 +  fixes S :: "_::real_normed_vector set"
  1.1079 +  shows "simply_connected S \<longleftrightarrow>
  1.1080 +         S = {} \<or>
  1.1081 +         (\<exists>a \<in> S. \<forall>p. path p \<and> path_image p \<subseteq> S \<and> pathfinish p = pathstart p
  1.1082 +                \<longrightarrow> homotopic_loops S p (linepath a a))"
  1.1083 +        (is "?lhs = ?rhs")
  1.1084 +proof (cases "S = {}")
  1.1085 +  case True then show ?thesis by force
  1.1086 +next
  1.1087 +  case False
  1.1088 +  then obtain a where "a \<in> S" by blast
  1.1089 +  show ?thesis
  1.1090 +  proof
  1.1091 +    assume "simply_connected S"
  1.1092 +    then show ?rhs
  1.1093 +      using \<open>a \<in> S\<close> \<open>simply_connected S\<close> simply_connected_eq_contractible_loop_any
  1.1094 +      by blast
  1.1095 +  next
  1.1096 +    assume ?rhs
  1.1097 +    then show "simply_connected S"
  1.1098 +      apply (simp add: simply_connected_eq_contractible_loop_any False)
  1.1099 +      by (meson homotopic_loops_refl homotopic_loops_sym homotopic_loops_trans
  1.1100 +             path_component_imp_homotopic_points path_component_refl)
  1.1101 +  qed
  1.1102 +qed
  1.1103 +
  1.1104 +lemma simply_connected_eq_contractible_path:
  1.1105 +  fixes S :: "_::real_normed_vector set"
  1.1106 +  shows "simply_connected S \<longleftrightarrow>
  1.1107 +           path_connected S \<and>
  1.1108 +           (\<forall>p. path p \<and> path_image p \<subseteq> S \<and> pathfinish p = pathstart p
  1.1109 +            \<longrightarrow> homotopic_paths S p (linepath (pathstart p) (pathstart p)))"
  1.1110 +apply (rule iffI)
  1.1111 + apply (simp add: simply_connected_imp_path_connected)
  1.1112 + apply (metis simply_connected_eq_contractible_loop_some homotopic_loops_imp_homotopic_paths_null)
  1.1113 +by (meson homotopic_paths_imp_homotopic_loops pathfinish_linepath pathstart_in_path_image
  1.1114 +         simply_connected_eq_contractible_loop_some subset_iff)
  1.1115 +
  1.1116 +lemma simply_connected_eq_homotopic_paths:
  1.1117 +  fixes S :: "_::real_normed_vector set"
  1.1118 +  shows "simply_connected S \<longleftrightarrow>
  1.1119 +          path_connected S \<and>
  1.1120 +          (\<forall>p q. path p \<and> path_image p \<subseteq> S \<and>
  1.1121 +                path q \<and> path_image q \<subseteq> S \<and>
  1.1122 +                pathstart q = pathstart p \<and> pathfinish q = pathfinish p
  1.1123 +                \<longrightarrow> homotopic_paths S p q)"
  1.1124 +         (is "?lhs = ?rhs")
  1.1125 +proof
  1.1126 +  assume ?lhs
  1.1127 +  then have pc: "path_connected S"
  1.1128 +        and *:  "\<And>p. \<lbrakk>path p; path_image p \<subseteq> S;
  1.1129 +                       pathfinish p = pathstart p\<rbrakk>
  1.1130 +                      \<Longrightarrow> homotopic_paths S p (linepath (pathstart p) (pathstart p))"
  1.1131 +    by (auto simp: simply_connected_eq_contractible_path)
  1.1132 +  have "homotopic_paths S p q"
  1.1133 +        if "path p" "path_image p \<subseteq> S" "path q"
  1.1134 +           "path_image q \<subseteq> S" "pathstart q = pathstart p"
  1.1135 +           "pathfinish q = pathfinish p" for p q
  1.1136 +  proof -
  1.1137 +    have "homotopic_paths S p (p +++ linepath (pathfinish p) (pathfinish p))"
  1.1138 +      by (simp add: homotopic_paths_rid homotopic_paths_sym that)
  1.1139 +    also have "homotopic_paths S (p +++ linepath (pathfinish p) (pathfinish p))
  1.1140 +                                 (p +++ reversepath q +++ q)"
  1.1141 +      using that
  1.1142 +      by (metis homotopic_paths_join homotopic_paths_linv homotopic_paths_refl homotopic_paths_sym_eq pathstart_linepath)
  1.1143 +    also have "homotopic_paths S (p +++ reversepath q +++ q)
  1.1144 +                                 ((p +++ reversepath q) +++ q)"
  1.1145 +      by (simp add: that homotopic_paths_assoc)
  1.1146 +    also have "homotopic_paths S ((p +++ reversepath q) +++ q)
  1.1147 +                                 (linepath (pathstart q) (pathstart q) +++ q)"
  1.1148 +      using * [of "p +++ reversepath q"] that
  1.1149 +      by (simp add: homotopic_paths_join path_image_join)
  1.1150 +    also have "homotopic_paths S (linepath (pathstart q) (pathstart q) +++ q) q"
  1.1151 +      using that homotopic_paths_lid by blast
  1.1152 +    finally show ?thesis .
  1.1153 +  qed
  1.1154 +  then show ?rhs
  1.1155 +    by (blast intro: pc *)
  1.1156 +next
  1.1157 +  assume ?rhs
  1.1158 +  then show ?lhs
  1.1159 +    by (force simp: simply_connected_eq_contractible_path)
  1.1160 +qed
  1.1161 +
  1.1162 +proposition simply_connected_Times:
  1.1163 +  fixes S :: "'a::real_normed_vector set" and T :: "'b::real_normed_vector set"
  1.1164 +  assumes S: "simply_connected S" and T: "simply_connected T"
  1.1165 +    shows "simply_connected(S \<times> T)"
  1.1166 +proof -
  1.1167 +  have "homotopic_loops (S \<times> T) p (linepath (a, b) (a, b))"
  1.1168 +       if "path p" "path_image p \<subseteq> S \<times> T" "p 1 = p 0" "a \<in> S" "b \<in> T"
  1.1169 +       for p a b
  1.1170 +  proof -
  1.1171 +    have "path (fst \<circ> p)"
  1.1172 +      apply (rule Path_Connected.path_continuous_image [OF \<open>path p\<close>])
  1.1173 +      apply (rule continuous_intros)+
  1.1174 +      done
  1.1175 +    moreover have "path_image (fst \<circ> p) \<subseteq> S"
  1.1176 +      using that apply (simp add: path_image_def) by force
  1.1177 +    ultimately have p1: "homotopic_loops S (fst \<circ> p) (linepath a a)"
  1.1178 +      using S that
  1.1179 +      apply (simp add: simply_connected_eq_contractible_loop_any)
  1.1180 +      apply (drule_tac x="fst \<circ> p" in spec)
  1.1181 +      apply (drule_tac x=a in spec)
  1.1182 +      apply (auto simp: pathstart_def pathfinish_def)
  1.1183 +      done
  1.1184 +    have "path (snd \<circ> p)"
  1.1185 +      apply (rule Path_Connected.path_continuous_image [OF \<open>path p\<close>])
  1.1186 +      apply (rule continuous_intros)+
  1.1187 +      done
  1.1188 +    moreover have "path_image (snd \<circ> p) \<subseteq> T"
  1.1189 +      using that apply (simp add: path_image_def) by force
  1.1190 +    ultimately have p2: "homotopic_loops T (snd \<circ> p) (linepath b b)"
  1.1191 +      using T that
  1.1192 +      apply (simp add: simply_connected_eq_contractible_loop_any)
  1.1193 +      apply (drule_tac x="snd \<circ> p" in spec)
  1.1194 +      apply (drule_tac x=b in spec)
  1.1195 +      apply (auto simp: pathstart_def pathfinish_def)
  1.1196 +      done
  1.1197 +    show ?thesis
  1.1198 +      using p1 p2
  1.1199 +      apply (simp add: homotopic_loops, clarify)
  1.1200 +      apply (rename_tac h k)
  1.1201 +      apply (rule_tac x="\<lambda>z. Pair (h z) (k z)" in exI)
  1.1202 +      apply (intro conjI continuous_intros | assumption)+
  1.1203 +      apply (auto simp: pathstart_def pathfinish_def)
  1.1204 +      done
  1.1205 +  qed
  1.1206 +  with assms show ?thesis
  1.1207 +    by (simp add: simply_connected_eq_contractible_loop_any pathfinish_def pathstart_def)
  1.1208 +qed
  1.1209 +
  1.1210 +
  1.1211 +subsection\<open>Contractible sets\<close>
  1.1212 +
  1.1213 +definition%important contractible where
  1.1214 + "contractible S \<equiv> \<exists>a. homotopic_with (\<lambda>x. True) S S id (\<lambda>x. a)"
  1.1215 +
  1.1216 +proposition contractible_imp_simply_connected:
  1.1217 +  fixes S :: "_::real_normed_vector set"
  1.1218 +  assumes "contractible S" shows "simply_connected S"
  1.1219 +proof (cases "S = {}")
  1.1220 +  case True then show ?thesis by force
  1.1221 +next
  1.1222 +  case False
  1.1223 +  obtain a where a: "homotopic_with (\<lambda>x. True) S S id (\<lambda>x. a)"
  1.1224 +    using assms by (force simp: contractible_def)
  1.1225 +  then have "a \<in> S"
  1.1226 +    by (metis False homotopic_constant_maps homotopic_with_symD homotopic_with_trans path_component_mem(2))
  1.1227 +  show ?thesis
  1.1228 +    apply (simp add: simply_connected_eq_contractible_loop_all False)
  1.1229 +    apply (rule bexI [OF _ \<open>a \<in> S\<close>])
  1.1230 +    using a apply (simp add: homotopic_loops_def homotopic_with_def path_def path_image_def pathfinish_def pathstart_def, clarify)
  1.1231 +    apply (rule_tac x="(h \<circ> (\<lambda>y. (fst y, (p \<circ> snd) y)))" in exI)
  1.1232 +    apply (intro conjI continuous_on_compose continuous_intros)
  1.1233 +    apply (erule continuous_on_subset | force)+
  1.1234 +    done
  1.1235 +qed
  1.1236 +
  1.1237 +corollary contractible_imp_connected:
  1.1238 +  fixes S :: "_::real_normed_vector set"
  1.1239 +  shows "contractible S \<Longrightarrow> connected S"
  1.1240 +by (simp add: contractible_imp_simply_connected simply_connected_imp_connected)
  1.1241 +
  1.1242 +lemma contractible_imp_path_connected:
  1.1243 +  fixes S :: "_::real_normed_vector set"
  1.1244 +  shows "contractible S \<Longrightarrow> path_connected S"
  1.1245 +by (simp add: contractible_imp_simply_connected simply_connected_imp_path_connected)
  1.1246 +
  1.1247 +lemma nullhomotopic_through_contractible:
  1.1248 +  fixes S :: "_::topological_space set"
  1.1249 +  assumes f: "continuous_on S f" "f ` S \<subseteq> T"
  1.1250 +      and g: "continuous_on T g" "g ` T \<subseteq> U"
  1.1251 +      and T: "contractible T"
  1.1252 +    obtains c where "homotopic_with (\<lambda>h. True) S U (g \<circ> f) (\<lambda>x. c)"
  1.1253 +proof -
  1.1254 +  obtain b where b: "homotopic_with (\<lambda>x. True) T T id (\<lambda>x. b)"
  1.1255 +    using assms by (force simp: contractible_def)
  1.1256 +  have "homotopic_with (\<lambda>f. True) T U (g \<circ> id) (g \<circ> (\<lambda>x. b))"
  1.1257 +    by (rule homotopic_compose_continuous_left [OF b g])
  1.1258 +  then have "homotopic_with (\<lambda>f. True) S U (g \<circ> id \<circ> f) (g \<circ> (\<lambda>x. b) \<circ> f)"
  1.1259 +    by (rule homotopic_compose_continuous_right [OF _ f])
  1.1260 +  then show ?thesis
  1.1261 +    by (simp add: comp_def that)
  1.1262 +qed
  1.1263 +
  1.1264 +lemma nullhomotopic_into_contractible:
  1.1265 +  assumes f: "continuous_on S f" "f ` S \<subseteq> T"
  1.1266 +      and T: "contractible T"
  1.1267 +    obtains c where "homotopic_with (\<lambda>h. True) S T f (\<lambda>x. c)"
  1.1268 +apply (rule nullhomotopic_through_contractible [OF f, of id T])
  1.1269 +using assms
  1.1270 +apply (auto simp: continuous_on_id)
  1.1271 +done
  1.1272 +
  1.1273 +lemma nullhomotopic_from_contractible:
  1.1274 +  assumes f: "continuous_on S f" "f ` S \<subseteq> T"
  1.1275 +      and S: "contractible S"
  1.1276 +    obtains c where "homotopic_with (\<lambda>h. True) S T f (\<lambda>x. c)"
  1.1277 +apply (rule nullhomotopic_through_contractible [OF continuous_on_id _ f S, of S])
  1.1278 +using assms
  1.1279 +apply (auto simp: comp_def)
  1.1280 +done
  1.1281 +
  1.1282 +lemma homotopic_through_contractible:
  1.1283 +  fixes S :: "_::real_normed_vector set"
  1.1284 +  assumes "continuous_on S f1" "f1 ` S \<subseteq> T"
  1.1285 +          "continuous_on T g1" "g1 ` T \<subseteq> U"
  1.1286 +          "continuous_on S f2" "f2 ` S \<subseteq> T"
  1.1287 +          "continuous_on T g2" "g2 ` T \<subseteq> U"
  1.1288 +          "contractible T" "path_connected U"
  1.1289 +   shows "homotopic_with (\<lambda>h. True) S U (g1 \<circ> f1) (g2 \<circ> f2)"
  1.1290 +proof -
  1.1291 +  obtain c1 where c1: "homotopic_with (\<lambda>h. True) S U (g1 \<circ> f1) (\<lambda>x. c1)"
  1.1292 +    apply (rule nullhomotopic_through_contractible [of S f1 T g1 U])
  1.1293 +    using assms apply auto
  1.1294 +    done
  1.1295 +  obtain c2 where c2: "homotopic_with (\<lambda>h. True) S U (g2 \<circ> f2) (\<lambda>x. c2)"
  1.1296 +    apply (rule nullhomotopic_through_contractible [of S f2 T g2 U])
  1.1297 +    using assms apply auto
  1.1298 +    done
  1.1299 +  have *: "S = {} \<or> (\<exists>t. path_connected t \<and> t \<subseteq> U \<and> c2 \<in> t \<and> c1 \<in> t)"
  1.1300 +  proof (cases "S = {}")
  1.1301 +    case True then show ?thesis by force
  1.1302 +  next
  1.1303 +    case False
  1.1304 +    with c1 c2 have "c1 \<in> U" "c2 \<in> U"
  1.1305 +      using homotopic_with_imp_subset2 all_not_in_conv image_subset_iff by blast+
  1.1306 +    with \<open>path_connected U\<close> show ?thesis by blast
  1.1307 +  qed
  1.1308 +  show ?thesis
  1.1309 +    apply (rule homotopic_with_trans [OF c1])
  1.1310 +    apply (rule homotopic_with_symD)
  1.1311 +    apply (rule homotopic_with_trans [OF c2])
  1.1312 +    apply (simp add: path_component homotopic_constant_maps *)
  1.1313 +    done
  1.1314 +qed
  1.1315 +
  1.1316 +lemma homotopic_into_contractible:
  1.1317 +  fixes S :: "'a::real_normed_vector set" and T:: "'b::real_normed_vector set"
  1.1318 +  assumes f: "continuous_on S f" "f ` S \<subseteq> T"
  1.1319 +      and g: "continuous_on S g" "g ` S \<subseteq> T"
  1.1320 +      and T: "contractible T"
  1.1321 +    shows "homotopic_with (\<lambda>h. True) S T f g"
  1.1322 +using homotopic_through_contractible [of S f T id T g id]
  1.1323 +by (simp add: assms contractible_imp_path_connected continuous_on_id)
  1.1324 +
  1.1325 +lemma homotopic_from_contractible:
  1.1326 +  fixes S :: "'a::real_normed_vector set" and T:: "'b::real_normed_vector set"
  1.1327 +  assumes f: "continuous_on S f" "f ` S \<subseteq> T"
  1.1328 +      and g: "continuous_on S g" "g ` S \<subseteq> T"
  1.1329 +      and "contractible S" "path_connected T"
  1.1330 +    shows "homotopic_with (\<lambda>h. True) S T f g"
  1.1331 +using homotopic_through_contractible [of S id S f T id g]
  1.1332 +by (simp add: assms contractible_imp_path_connected continuous_on_id)
  1.1333 +
  1.1334 +lemma starlike_imp_contractible_gen:
  1.1335 +  fixes S :: "'a::real_normed_vector set"
  1.1336 +  assumes S: "starlike S"
  1.1337 +      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)"
  1.1338 +    obtains a where "homotopic_with P S S (\<lambda>x. x) (\<lambda>x. a)"
  1.1339 +proof -
  1.1340 +  obtain a where "a \<in> S" and a: "\<And>x. x \<in> S \<Longrightarrow> closed_segment a x \<subseteq> S"
  1.1341 +    using S by (auto simp: starlike_def)
  1.1342 +  have "(\<lambda>y. (1 - fst y) *\<^sub>R snd y + fst y *\<^sub>R a) ` ({0..1} \<times> S) \<subseteq> S"
  1.1343 +    apply clarify
  1.1344 +    apply (erule a [unfolded closed_segment_def, THEN subsetD], simp)
  1.1345 +    apply (metis add_diff_cancel_right' diff_ge_0_iff_ge le_add_diff_inverse pth_c(1))
  1.1346 +    done
  1.1347 +  then show ?thesis
  1.1348 +    apply (rule_tac a=a in that)
  1.1349 +    using \<open>a \<in> S\<close>
  1.1350 +    apply (simp add: homotopic_with_def)
  1.1351 +    apply (rule_tac x="\<lambda>y. (1 - (fst y)) *\<^sub>R snd y + (fst y) *\<^sub>R a" in exI)
  1.1352 +    apply (intro conjI ballI continuous_on_compose continuous_intros)
  1.1353 +    apply (simp_all add: P)
  1.1354 +    done
  1.1355 +qed
  1.1356 +
  1.1357 +lemma starlike_imp_contractible:
  1.1358 +  fixes S :: "'a::real_normed_vector set"
  1.1359 +  shows "starlike S \<Longrightarrow> contractible S"
  1.1360 +using starlike_imp_contractible_gen contractible_def by (fastforce simp: id_def)
  1.1361 +
  1.1362 +lemma contractible_UNIV [simp]: "contractible (UNIV :: 'a::real_normed_vector set)"
  1.1363 +  by (simp add: starlike_imp_contractible)
  1.1364 +
  1.1365 +lemma starlike_imp_simply_connected:
  1.1366 +  fixes S :: "'a::real_normed_vector set"
  1.1367 +  shows "starlike S \<Longrightarrow> simply_connected S"
  1.1368 +by (simp add: contractible_imp_simply_connected starlike_imp_contractible)
  1.1369 +
  1.1370 +lemma convex_imp_simply_connected:
  1.1371 +  fixes S :: "'a::real_normed_vector set"
  1.1372 +  shows "convex S \<Longrightarrow> simply_connected S"
  1.1373 +using convex_imp_starlike starlike_imp_simply_connected by blast
  1.1374 +
  1.1375 +lemma starlike_imp_path_connected:
  1.1376 +  fixes S :: "'a::real_normed_vector set"
  1.1377 +  shows "starlike S \<Longrightarrow> path_connected S"
  1.1378 +by (simp add: simply_connected_imp_path_connected starlike_imp_simply_connected)
  1.1379 +
  1.1380 +lemma starlike_imp_connected:
  1.1381 +  fixes S :: "'a::real_normed_vector set"
  1.1382 +  shows "starlike S \<Longrightarrow> connected S"
  1.1383 +by (simp add: path_connected_imp_connected starlike_imp_path_connected)
  1.1384 +
  1.1385 +lemma is_interval_simply_connected_1:
  1.1386 +  fixes S :: "real set"
  1.1387 +  shows "is_interval S \<longleftrightarrow> simply_connected S"
  1.1388 +using convex_imp_simply_connected is_interval_convex_1 is_interval_path_connected_1 simply_connected_imp_path_connected by auto
  1.1389 +
  1.1390 +lemma contractible_empty [simp]: "contractible {}"
  1.1391 +  by (simp add: contractible_def homotopic_with)
  1.1392 +
  1.1393 +lemma contractible_convex_tweak_boundary_points:
  1.1394 +  fixes S :: "'a::euclidean_space set"
  1.1395 +  assumes "convex S" and TS: "rel_interior S \<subseteq> T" "T \<subseteq> closure S"
  1.1396 +  shows "contractible T"
  1.1397 +proof (cases "S = {}")
  1.1398 +  case True
  1.1399 +  with assms show ?thesis
  1.1400 +    by (simp add: subsetCE)
  1.1401 +next
  1.1402 +  case False
  1.1403 +  show ?thesis
  1.1404 +    apply (rule starlike_imp_contractible)
  1.1405 +    apply (rule starlike_convex_tweak_boundary_points [OF \<open>convex S\<close> False TS])
  1.1406 +    done
  1.1407 +qed
  1.1408 +
  1.1409 +lemma convex_imp_contractible:
  1.1410 +  fixes S :: "'a::real_normed_vector set"
  1.1411 +  shows "convex S \<Longrightarrow> contractible S"
  1.1412 +  using contractible_empty convex_imp_starlike starlike_imp_contractible by blast
  1.1413 +
  1.1414 +lemma contractible_sing [simp]:
  1.1415 +  fixes a :: "'a::real_normed_vector"
  1.1416 +  shows "contractible {a}"
  1.1417 +by (rule convex_imp_contractible [OF convex_singleton])
  1.1418 +
  1.1419 +lemma is_interval_contractible_1:
  1.1420 +  fixes S :: "real set"
  1.1421 +  shows  "is_interval S \<longleftrightarrow> contractible S"
  1.1422 +using contractible_imp_simply_connected convex_imp_contractible is_interval_convex_1
  1.1423 +      is_interval_simply_connected_1 by auto
  1.1424 +
  1.1425 +lemma contractible_Times:
  1.1426 +  fixes S :: "'a::euclidean_space set" and T :: "'b::euclidean_space set"
  1.1427 +  assumes S: "contractible S" and T: "contractible T"
  1.1428 +  shows "contractible (S \<times> T)"
  1.1429 +proof -
  1.1430 +  obtain a h where conth: "continuous_on ({0..1} \<times> S) h"
  1.1431 +             and hsub: "h ` ({0..1} \<times> S) \<subseteq> S"
  1.1432 +             and [simp]: "\<And>x. x \<in> S \<Longrightarrow> h (0, x) = x"
  1.1433 +             and [simp]: "\<And>x. x \<in> S \<Longrightarrow>  h (1::real, x) = a"
  1.1434 +    using S by (auto simp: contractible_def homotopic_with)
  1.1435 +  obtain b k where contk: "continuous_on ({0..1} \<times> T) k"
  1.1436 +             and ksub: "k ` ({0..1} \<times> T) \<subseteq> T"
  1.1437 +             and [simp]: "\<And>x. x \<in> T \<Longrightarrow> k (0, x) = x"
  1.1438 +             and [simp]: "\<And>x. x \<in> T \<Longrightarrow>  k (1::real, x) = b"
  1.1439 +    using T by (auto simp: contractible_def homotopic_with)
  1.1440 +  show ?thesis
  1.1441 +    apply (simp add: contractible_def homotopic_with)
  1.1442 +    apply (rule exI [where x=a])
  1.1443 +    apply (rule exI [where x=b])
  1.1444 +    apply (rule exI [where x = "\<lambda>z. (h (fst z, fst(snd z)), k (fst z, snd(snd z)))"])
  1.1445 +    apply (intro conjI ballI continuous_intros continuous_on_compose2 [OF conth] continuous_on_compose2 [OF contk])
  1.1446 +    using hsub ksub
  1.1447 +    apply auto
  1.1448 +    done
  1.1449 +qed
  1.1450 +
  1.1451 +lemma homotopy_dominated_contractibility:
  1.1452 +  fixes S :: "'a::real_normed_vector set" and T :: "'b::real_normed_vector set"
  1.1453 +  assumes S: "contractible S"
  1.1454 +      and f: "continuous_on S f" "image f S \<subseteq> T"
  1.1455 +      and g: "continuous_on T g" "image g T \<subseteq> S"
  1.1456 +      and hom: "homotopic_with (\<lambda>x. True) T T (f \<circ> g) id"
  1.1457 +    shows "contractible T"
  1.1458 +proof -
  1.1459 +  obtain b where "homotopic_with (\<lambda>h. True) S T f (\<lambda>x. b)"
  1.1460 +    using nullhomotopic_from_contractible [OF f S] .
  1.1461 +  then have homg: "homotopic_with (\<lambda>x. True) T T ((\<lambda>x. b) \<circ> g) (f \<circ> g)"
  1.1462 +    by (rule homotopic_with_compose_continuous_right [OF homotopic_with_symD g])
  1.1463 +  show ?thesis
  1.1464 +    apply (simp add: contractible_def)
  1.1465 +    apply (rule exI [where x = b])
  1.1466 +    apply (rule homotopic_with_symD)
  1.1467 +    apply (rule homotopic_with_trans [OF _ hom])
  1.1468 +    using homg apply (simp add: o_def)
  1.1469 +    done
  1.1470 +qed
  1.1471 +
  1.1472 +
  1.1473 +subsection\<open>Local versions of topological properties in general\<close>
  1.1474 +
  1.1475 +definition%important locally :: "('a::topological_space set \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool"
  1.1476 +where
  1.1477 + "locally P S \<equiv>
  1.1478 +        \<forall>w x. openin (subtopology euclidean S) w \<and> x \<in> w
  1.1479 +              \<longrightarrow> (\<exists>u v. openin (subtopology euclidean S) u \<and> P v \<and>
  1.1480 +                        x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> w)"
  1.1481 +
  1.1482 +lemma locallyI:
  1.1483 +  assumes "\<And>w x. \<lbrakk>openin (subtopology euclidean S) w; x \<in> w\<rbrakk>
  1.1484 +                  \<Longrightarrow> \<exists>u v. openin (subtopology euclidean S) u \<and> P v \<and>
  1.1485 +                        x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> w"
  1.1486 +    shows "locally P S"
  1.1487 +using assms by (force simp: locally_def)
  1.1488 +
  1.1489 +lemma locallyE:
  1.1490 +  assumes "locally P S" "openin (subtopology euclidean S) w" "x \<in> w"
  1.1491 +  obtains u v where "openin (subtopology euclidean S) u"
  1.1492 +                    "P v" "x \<in> u" "u \<subseteq> v" "v \<subseteq> w"
  1.1493 +  using assms unfolding locally_def by meson
  1.1494 +
  1.1495 +lemma locally_mono:
  1.1496 +  assumes "locally P S" "\<And>t. P t \<Longrightarrow> Q t"
  1.1497 +    shows "locally Q S"
  1.1498 +by (metis assms locally_def)
  1.1499 +
  1.1500 +lemma locally_open_subset:
  1.1501 +  assumes "locally P S" "openin (subtopology euclidean S) t"
  1.1502 +    shows "locally P t"
  1.1503 +using assms
  1.1504 +apply (simp add: locally_def)
  1.1505 +apply (erule all_forward)+
  1.1506 +apply (rule impI)
  1.1507 +apply (erule impCE)
  1.1508 + using openin_trans apply blast
  1.1509 +apply (erule ex_forward)
  1.1510 +by (metis (no_types, hide_lams) Int_absorb1 Int_lower1 Int_subset_iff openin_open openin_subtopology_Int_subset)
  1.1511 +
  1.1512 +lemma locally_diff_closed:
  1.1513 +    "\<lbrakk>locally P S; closedin (subtopology euclidean S) t\<rbrakk> \<Longrightarrow> locally P (S - t)"
  1.1514 +  using locally_open_subset closedin_def by fastforce
  1.1515 +
  1.1516 +lemma locally_empty [iff]: "locally P {}"
  1.1517 +  by (simp add: locally_def openin_subtopology)
  1.1518 +
  1.1519 +lemma locally_singleton [iff]:
  1.1520 +  fixes a :: "'a::metric_space"
  1.1521 +  shows "locally P {a} \<longleftrightarrow> P {a}"
  1.1522 +apply (simp add: locally_def openin_euclidean_subtopology_iff subset_singleton_iff conj_disj_distribR cong: conj_cong)
  1.1523 +using zero_less_one by blast
  1.1524 +
  1.1525 +lemma locally_iff:
  1.1526 +    "locally P S \<longleftrightarrow>
  1.1527 +     (\<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)))"
  1.1528 +apply (simp add: le_inf_iff locally_def openin_open, safe)
  1.1529 +apply (metis IntE IntI le_inf_iff)
  1.1530 +apply (metis IntI Int_subset_iff)
  1.1531 +done
  1.1532 +
  1.1533 +lemma locally_Int:
  1.1534 +  assumes S: "locally P S" and t: "locally P t"
  1.1535 +      and P: "\<And>S t. P S \<and> P t \<Longrightarrow> P(S \<inter> t)"
  1.1536 +    shows "locally P (S \<inter> t)"
  1.1537 +using S t unfolding locally_iff
  1.1538 +apply clarify
  1.1539 +apply (drule_tac x=T in spec)+
  1.1540 +apply (drule_tac x=x in spec)+
  1.1541 +apply clarsimp
  1.1542 +apply (rename_tac U1 U2 V1 V2)
  1.1543 +apply (rule_tac x="U1 \<inter> U2" in exI)
  1.1544 +apply (simp add: open_Int)
  1.1545 +apply (rule_tac x="V1 \<inter> V2" in exI)
  1.1546 +apply (auto intro: P)
  1.1547 +done
  1.1548 +
  1.1549 +lemma locally_Times:
  1.1550 +  fixes S :: "('a::metric_space) set" and T :: "('b::metric_space) set"
  1.1551 +  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)"
  1.1552 +  shows "locally R (S \<times> T)"
  1.1553 +    unfolding locally_def
  1.1554 +proof (clarify)
  1.1555 +  fix W x y
  1.1556 +  assume W: "openin (subtopology euclidean (S \<times> T)) W" and xy: "(x, y) \<in> W"
  1.1557 +  then obtain U V where "openin (subtopology euclidean S) U" "x \<in> U"
  1.1558 +                        "openin (subtopology euclidean T) V" "y \<in> V" "U \<times> V \<subseteq> W"
  1.1559 +    using Times_in_interior_subtopology by metis
  1.1560 +  then obtain U1 U2 V1 V2
  1.1561 +         where opeS: "openin (subtopology euclidean S) U1 \<and> P U2 \<and> x \<in> U1 \<and> U1 \<subseteq> U2 \<and> U2 \<subseteq> U"
  1.1562 +           and opeT: "openin (subtopology euclidean T) V1 \<and> Q V2 \<and> y \<in> V1 \<and> V1 \<subseteq> V2 \<and> V2 \<subseteq> V"
  1.1563 +    by (meson PS QT locallyE)
  1.1564 +  with \<open>U \<times> V \<subseteq> W\<close> show "\<exists>u v. openin (subtopology euclidean (S \<times> T)) u \<and> R v \<and> (x,y) \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> W"
  1.1565 +    apply (rule_tac x="U1 \<times> V1" in exI)
  1.1566 +    apply (rule_tac x="U2 \<times> V2" in exI)
  1.1567 +    apply (auto simp: openin_Times R)
  1.1568 +    done
  1.1569 +qed
  1.1570 +
  1.1571 +
  1.1572 +proposition homeomorphism_locally_imp:
  1.1573 +  fixes S :: "'a::metric_space set" and t :: "'b::t2_space set"
  1.1574 +  assumes S: "locally P S" and hom: "homeomorphism S t f g"
  1.1575 +      and Q: "\<And>S S'. \<lbrakk>P S; homeomorphism S S' f g\<rbrakk> \<Longrightarrow> Q S'"
  1.1576 +    shows "locally Q t"
  1.1577 +proof (clarsimp simp: locally_def)
  1.1578 +  fix W y
  1.1579 +  assume "y \<in> W" and "openin (subtopology euclidean t) W"
  1.1580 +  then obtain T where T: "open T" "W = t \<inter> T"
  1.1581 +    by (force simp: openin_open)
  1.1582 +  then have "W \<subseteq> t" by auto
  1.1583 +  have f: "\<And>x. x \<in> S \<Longrightarrow> g(f x) = x" "f ` S = t" "continuous_on S f"
  1.1584 +   and g: "\<And>y. y \<in> t \<Longrightarrow> f(g y) = y" "g ` t = S" "continuous_on t g"
  1.1585 +    using hom by (auto simp: homeomorphism_def)
  1.1586 +  have gw: "g ` W = S \<inter> f -` W"
  1.1587 +    using \<open>W \<subseteq> t\<close>
  1.1588 +    apply auto
  1.1589 +    using \<open>g ` t = S\<close> \<open>W \<subseteq> t\<close> apply blast
  1.1590 +    using g \<open>W \<subseteq> t\<close> apply auto[1]
  1.1591 +    by (simp add: f rev_image_eqI)
  1.1592 +  have \<circ>: "openin (subtopology euclidean S) (g ` W)"
  1.1593 +  proof -
  1.1594 +    have "continuous_on S f"
  1.1595 +      using f(3) by blast
  1.1596 +    then show "openin (subtopology euclidean S) (g ` W)"
  1.1597 +      by (simp add: gw Collect_conj_eq \<open>openin (subtopology euclidean t) W\<close> continuous_on_open f(2))
  1.1598 +  qed
  1.1599 +  then obtain u v
  1.1600 +    where osu: "openin (subtopology euclidean S) u" and uv: "P v" "g y \<in> u" "u \<subseteq> v" "v \<subseteq> g ` W"
  1.1601 +    using S [unfolded locally_def, rule_format, of "g ` W" "g y"] \<open>y \<in> W\<close> by force
  1.1602 +  have "v \<subseteq> S" using uv by (simp add: gw)
  1.1603 +  have fv: "f ` v = t \<inter> {x. g x \<in> v}"
  1.1604 +    using \<open>f ` S = t\<close> f \<open>v \<subseteq> S\<close> by auto
  1.1605 +  have "f ` v \<subseteq> W"
  1.1606 +    using uv using Int_lower2 gw image_subsetI mem_Collect_eq subset_iff by auto
  1.1607 +  have contvf: "continuous_on v f"
  1.1608 +    using \<open>v \<subseteq> S\<close> continuous_on_subset f(3) by blast
  1.1609 +  have contvg: "continuous_on (f ` v) g"
  1.1610 +    using \<open>f ` v \<subseteq> W\<close> \<open>W \<subseteq> t\<close> continuous_on_subset [OF g(3)] by blast
  1.1611 +  have homv: "homeomorphism v (f ` v) f g"
  1.1612 +    using \<open>v \<subseteq> S\<close> \<open>W \<subseteq> t\<close> f
  1.1613 +    apply (simp add: homeomorphism_def contvf contvg, auto)
  1.1614 +    by (metis f(1) rev_image_eqI rev_subsetD)
  1.1615 +  have 1: "openin (subtopology euclidean t) (t \<inter> g -` u)"
  1.1616 +    apply (rule continuous_on_open [THEN iffD1, rule_format])
  1.1617 +    apply (rule \<open>continuous_on t g\<close>)
  1.1618 +    using \<open>g ` t = S\<close> apply (simp add: osu)
  1.1619 +    done
  1.1620 +  have 2: "\<exists>V. Q V \<and> y \<in> (t \<inter> g -` u) \<and> (t \<inter> g -` u) \<subseteq> V \<and> V \<subseteq> W"
  1.1621 +    apply (rule_tac x="f ` v" in exI)
  1.1622 +    apply (intro conjI Q [OF \<open>P v\<close> homv])
  1.1623 +    using \<open>W \<subseteq> t\<close> \<open>y \<in> W\<close>  \<open>f ` v \<subseteq> W\<close>  uv  apply (auto simp: fv)
  1.1624 +    done
  1.1625 +  show "\<exists>U. openin (subtopology euclidean t) U \<and> (\<exists>v. Q v \<and> y \<in> U \<and> U \<subseteq> v \<and> v \<subseteq> W)"
  1.1626 +    by (meson 1 2)
  1.1627 +qed
  1.1628 +
  1.1629 +lemma homeomorphism_locally:
  1.1630 +  fixes f:: "'a::metric_space \<Rightarrow> 'b::metric_space"
  1.1631 +  assumes hom: "homeomorphism S t f g"
  1.1632 +      and eq: "\<And>S t. homeomorphism S t f g \<Longrightarrow> (P S \<longleftrightarrow> Q t)"
  1.1633 +    shows "locally P S \<longleftrightarrow> locally Q t"
  1.1634 +apply (rule iffI)
  1.1635 +apply (erule homeomorphism_locally_imp [OF _ hom])
  1.1636 +apply (simp add: eq)
  1.1637 +apply (erule homeomorphism_locally_imp)
  1.1638 +using eq homeomorphism_sym homeomorphism_symD [OF hom] apply blast+
  1.1639 +done
  1.1640 +
  1.1641 +lemma homeomorphic_locally:
  1.1642 +  fixes S:: "'a::metric_space set" and T:: "'b::metric_space set"
  1.1643 +  assumes hom: "S homeomorphic T"
  1.1644 +          and iff: "\<And>X Y. X homeomorphic Y \<Longrightarrow> (P X \<longleftrightarrow> Q Y)"
  1.1645 +    shows "locally P S \<longleftrightarrow> locally Q T"
  1.1646 +proof -
  1.1647 +  obtain f g where hom: "homeomorphism S T f g"
  1.1648 +    using assms by (force simp: homeomorphic_def)
  1.1649 +  then show ?thesis
  1.1650 +    using homeomorphic_def local.iff
  1.1651 +    by (blast intro!: homeomorphism_locally)
  1.1652 +qed
  1.1653 +
  1.1654 +lemma homeomorphic_local_compactness:
  1.1655 +  fixes S:: "'a::metric_space set" and T:: "'b::metric_space set"
  1.1656 +  shows "S homeomorphic T \<Longrightarrow> locally compact S \<longleftrightarrow> locally compact T"
  1.1657 +by (simp add: homeomorphic_compactness homeomorphic_locally)
  1.1658 +
  1.1659 +lemma locally_translation:
  1.1660 +  fixes P :: "'a :: real_normed_vector set \<Rightarrow> bool"
  1.1661 +  shows
  1.1662 +   "(\<And>S. P (image (\<lambda>x. a + x) S) \<longleftrightarrow> P S)
  1.1663 +        \<Longrightarrow> locally P (image (\<lambda>x. a + x) S) \<longleftrightarrow> locally P S"
  1.1664 +apply (rule homeomorphism_locally [OF homeomorphism_translation])
  1.1665 +apply (simp add: homeomorphism_def)
  1.1666 +by metis
  1.1667 +
  1.1668 +lemma locally_injective_linear_image:
  1.1669 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1.1670 +  assumes f: "linear f" "inj f" and iff: "\<And>S. P (f ` S) \<longleftrightarrow> Q S"
  1.1671 +    shows "locally P (f ` S) \<longleftrightarrow> locally Q S"
  1.1672 +apply (rule linear_homeomorphism_image [OF f])
  1.1673 +apply (rule_tac f=g and g = f in homeomorphism_locally, assumption)
  1.1674 +by (metis iff homeomorphism_def)
  1.1675 +
  1.1676 +lemma locally_open_map_image:
  1.1677 +  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
  1.1678 +  assumes P: "locally P S"
  1.1679 +      and f: "continuous_on S f"
  1.1680 +      and oo: "\<And>t. openin (subtopology euclidean S) t
  1.1681 +                   \<Longrightarrow> openin (subtopology euclidean (f ` S)) (f ` t)"
  1.1682 +      and Q: "\<And>t. \<lbrakk>t \<subseteq> S; P t\<rbrakk> \<Longrightarrow> Q(f ` t)"
  1.1683 +    shows "locally Q (f ` S)"
  1.1684 +proof (clarsimp simp add: locally_def)
  1.1685 +  fix W y
  1.1686 +  assume oiw: "openin (subtopology euclidean (f ` S)) W" and "y \<in> W"
  1.1687 +  then have "W \<subseteq> f ` S" by (simp add: openin_euclidean_subtopology_iff)
  1.1688 +  have oivf: "openin (subtopology euclidean S) (S \<inter> f -` W)"
  1.1689 +    by (rule continuous_on_open [THEN iffD1, rule_format, OF f oiw])
  1.1690 +  then obtain x where "x \<in> S" "f x = y"
  1.1691 +    using \<open>W \<subseteq> f ` S\<close> \<open>y \<in> W\<close> by blast
  1.1692 +  then obtain U V
  1.1693 +    where "openin (subtopology euclidean S) U" "P V" "x \<in> U" "U \<subseteq> V" "V \<subseteq> S \<inter> f -` W"
  1.1694 +    using P [unfolded locally_def, rule_format, of "(S \<inter> f -` W)" x] oivf \<open>y \<in> W\<close>
  1.1695 +    by auto
  1.1696 +  then show "\<exists>X. openin (subtopology euclidean (f ` S)) X \<and> (\<exists>Y. Q Y \<and> y \<in> X \<and> X \<subseteq> Y \<and> Y \<subseteq> W)"
  1.1697 +    apply (rule_tac x="f ` U" in exI)
  1.1698 +    apply (rule conjI, blast intro!: oo)
  1.1699 +    apply (rule_tac x="f ` V" in exI)
  1.1700 +    apply (force simp: \<open>f x = y\<close> rev_image_eqI intro: Q)
  1.1701 +    done
  1.1702 +qed
  1.1703 +
  1.1704 +
  1.1705 +subsection\<open>An induction principle for connected sets\<close>
  1.1706 +
  1.1707 +proposition connected_induction:
  1.1708 +  assumes "connected S"
  1.1709 +      and opD: "\<And>T a. \<lbrakk>openin (subtopology euclidean S) T; a \<in> T\<rbrakk> \<Longrightarrow> \<exists>z. z \<in> T \<and> P z"
  1.1710 +      and opI: "\<And>a. a \<in> S
  1.1711 +             \<Longrightarrow> \<exists>T. openin (subtopology euclidean S) T \<and> a \<in> T \<and>
  1.1712 +                     (\<forall>x \<in> T. \<forall>y \<in> T. P x \<and> P y \<and> Q x \<longrightarrow> Q y)"
  1.1713 +      and etc: "a \<in> S" "b \<in> S" "P a" "P b" "Q a"
  1.1714 +    shows "Q b"
  1.1715 +proof -
  1.1716 +  have 1: "openin (subtopology euclidean S)
  1.1717 +             {b. \<exists>T. openin (subtopology euclidean S) T \<and>
  1.1718 +                     b \<in> T \<and> (\<forall>x\<in>T. P x \<longrightarrow> Q x)}"
  1.1719 +    apply (subst openin_subopen, clarify)
  1.1720 +    apply (rule_tac x=T in exI, auto)
  1.1721 +    done
  1.1722 +  have 2: "openin (subtopology euclidean S)
  1.1723 +             {b. \<exists>T. openin (subtopology euclidean S) T \<and>
  1.1724 +                     b \<in> T \<and> (\<forall>x\<in>T. P x \<longrightarrow> \<not> Q x)}"
  1.1725 +    apply (subst openin_subopen, clarify)
  1.1726 +    apply (rule_tac x=T in exI, auto)
  1.1727 +    done
  1.1728 +  show ?thesis
  1.1729 +    using \<open>connected S\<close>
  1.1730 +    apply (simp only: connected_openin HOL.not_ex HOL.de_Morgan_conj)
  1.1731 +    apply (elim disjE allE)
  1.1732 +         apply (blast intro: 1)
  1.1733 +        apply (blast intro: 2, simp_all)
  1.1734 +       apply clarify apply (metis opI)
  1.1735 +      using opD apply (blast intro: etc elim: dest:)
  1.1736 +     using opI etc apply meson+
  1.1737 +    done
  1.1738 +qed
  1.1739 +
  1.1740 +lemma connected_equivalence_relation_gen:
  1.1741 +  assumes "connected S"
  1.1742 +      and etc: "a \<in> S" "b \<in> S" "P a" "P b"
  1.1743 +      and trans: "\<And>x y z. \<lbrakk>R x y; R y z\<rbrakk> \<Longrightarrow> R x z"
  1.1744 +      and opD: "\<And>T a. \<lbrakk>openin (subtopology euclidean S) T; a \<in> T\<rbrakk> \<Longrightarrow> \<exists>z. z \<in> T \<and> P z"
  1.1745 +      and opI: "\<And>a. a \<in> S
  1.1746 +             \<Longrightarrow> \<exists>T. openin (subtopology euclidean S) T \<and> a \<in> T \<and>
  1.1747 +                     (\<forall>x \<in> T. \<forall>y \<in> T. P x \<and> P y \<longrightarrow> R x y)"
  1.1748 +    shows "R a b"
  1.1749 +proof -
  1.1750 +  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"
  1.1751 +    apply (rule connected_induction [OF \<open>connected S\<close> opD], simp_all)
  1.1752 +    by (meson trans opI)
  1.1753 +  then show ?thesis by (metis etc opI)
  1.1754 +qed
  1.1755 +
  1.1756 +lemma connected_induction_simple:
  1.1757 +  assumes "connected S"
  1.1758 +      and etc: "a \<in> S" "b \<in> S" "P a"
  1.1759 +      and opI: "\<And>a. a \<in> S
  1.1760 +             \<Longrightarrow> \<exists>T. openin (subtopology euclidean S) T \<and> a \<in> T \<and>
  1.1761 +                     (\<forall>x \<in> T. \<forall>y \<in> T. P x \<longrightarrow> P y)"
  1.1762 +    shows "P b"
  1.1763 +apply (rule connected_induction [OF \<open>connected S\<close> _, where P = "\<lambda>x. True"], blast)
  1.1764 +apply (frule opI)
  1.1765 +using etc apply simp_all
  1.1766 +done
  1.1767 +
  1.1768 +lemma connected_equivalence_relation:
  1.1769 +  assumes "connected S"
  1.1770 +      and etc: "a \<in> S" "b \<in> S"
  1.1771 +      and sym: "\<And>x y. \<lbrakk>R x y; x \<in> S; y \<in> S\<rbrakk> \<Longrightarrow> R y x"
  1.1772 +      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"
  1.1773 +      and opI: "\<And>a. a \<in> S \<Longrightarrow> \<exists>T. openin (subtopology euclidean S) T \<and> a \<in> T \<and> (\<forall>x \<in> T. R a x)"
  1.1774 +    shows "R a b"
  1.1775 +proof -
  1.1776 +  have "\<And>a b c. \<lbrakk>a \<in> S; b \<in> S; c \<in> S; R a b\<rbrakk> \<Longrightarrow> R a c"
  1.1777 +    apply (rule connected_induction_simple [OF \<open>connected S\<close>], simp_all)
  1.1778 +    by (meson local.sym local.trans opI openin_imp_subset subsetCE)
  1.1779 +  then show ?thesis by (metis etc opI)
  1.1780 +qed
  1.1781 +
  1.1782 +lemma locally_constant_imp_constant:
  1.1783 +  assumes "connected S"
  1.1784 +      and opI: "\<And>a. a \<in> S
  1.1785 +             \<Longrightarrow> \<exists>T. openin (subtopology euclidean S) T \<and> a \<in> T \<and> (\<forall>x \<in> T. f x = f a)"
  1.1786 +    shows "f constant_on S"
  1.1787 +proof -
  1.1788 +  have "\<And>x y. x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> f x = f y"
  1.1789 +    apply (rule connected_equivalence_relation [OF \<open>connected S\<close>], simp_all)
  1.1790 +    by (metis opI)
  1.1791 +  then show ?thesis
  1.1792 +    by (metis constant_on_def)
  1.1793 +qed
  1.1794 +
  1.1795 +lemma locally_constant:
  1.1796 +     "connected S \<Longrightarrow> locally (\<lambda>U. f constant_on U) S \<longleftrightarrow> f constant_on S"
  1.1797 +apply (simp add: locally_def)
  1.1798 +apply (rule iffI)
  1.1799 + apply (rule locally_constant_imp_constant, assumption)
  1.1800 + apply (metis (mono_tags, hide_lams) constant_on_def constant_on_subset openin_subtopology_self)
  1.1801 +by (meson constant_on_subset openin_imp_subset order_refl)
  1.1802 +
  1.1803 +
  1.1804 +subsection\<open>Basic properties of local compactness\<close>
  1.1805 +
  1.1806 +proposition locally_compact:
  1.1807 +  fixes s :: "'a :: metric_space set"
  1.1808 +  shows
  1.1809 +    "locally compact s \<longleftrightarrow>
  1.1810 +     (\<forall>x \<in> s. \<exists>u v. x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> s \<and>
  1.1811 +                    openin (subtopology euclidean s) u \<and> compact v)"
  1.1812 +     (is "?lhs = ?rhs")
  1.1813 +proof
  1.1814 +  assume ?lhs
  1.1815 +  then show ?rhs
  1.1816 +    apply clarify
  1.1817 +    apply (erule_tac w = "s \<inter> ball x 1" in locallyE)
  1.1818 +    by auto
  1.1819 +next
  1.1820 +  assume r [rule_format]: ?rhs
  1.1821 +  have *: "\<exists>u v.
  1.1822 +              openin (subtopology euclidean s) u \<and>
  1.1823 +              compact v \<and> x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> s \<inter> T"
  1.1824 +          if "open T" "x \<in> s" "x \<in> T" for x T
  1.1825 +  proof -
  1.1826 +    obtain u v where uv: "x \<in> u" "u \<subseteq> v" "v \<subseteq> s" "compact v" "openin (subtopology euclidean s) u"
  1.1827 +      using r [OF \<open>x \<in> s\<close>] by auto
  1.1828 +    obtain e where "e>0" and e: "cball x e \<subseteq> T"
  1.1829 +      using open_contains_cball \<open>open T\<close> \<open>x \<in> T\<close> by blast
  1.1830 +    show ?thesis
  1.1831 +      apply (rule_tac x="(s \<inter> ball x e) \<inter> u" in exI)
  1.1832 +      apply (rule_tac x="cball x e \<inter> v" in exI)
  1.1833 +      using that \<open>e > 0\<close> e uv
  1.1834 +      apply auto
  1.1835 +      done
  1.1836 +  qed
  1.1837 +  show ?lhs
  1.1838 +    apply (rule locallyI)
  1.1839 +    apply (subst (asm) openin_open)
  1.1840 +    apply (blast intro: *)
  1.1841 +    done
  1.1842 +qed
  1.1843 +
  1.1844 +lemma locally_compactE:
  1.1845 +  fixes s :: "'a :: metric_space set"
  1.1846 +  assumes "locally compact s"
  1.1847 +  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>
  1.1848 +                             openin (subtopology euclidean s) (u x) \<and> compact (v x)"
  1.1849 +using assms
  1.1850 +unfolding locally_compact by metis
  1.1851 +
  1.1852 +lemma locally_compact_alt:
  1.1853 +  fixes s :: "'a :: heine_borel set"
  1.1854 +  shows "locally compact s \<longleftrightarrow>
  1.1855 +         (\<forall>x \<in> s. \<exists>u. x \<in> u \<and>
  1.1856 +                    openin (subtopology euclidean s) u \<and> compact(closure u) \<and> closure u \<subseteq> s)"
  1.1857 +apply (simp add: locally_compact)
  1.1858 +apply (intro ball_cong ex_cong refl iffI)
  1.1859 +apply (metis bounded_subset closure_eq closure_mono compact_eq_bounded_closed dual_order.trans)
  1.1860 +by (meson closure_subset compact_closure)
  1.1861 +
  1.1862 +lemma locally_compact_Int_cball:
  1.1863 +  fixes s :: "'a :: heine_borel set"
  1.1864 +  shows "locally compact s \<longleftrightarrow> (\<forall>x \<in> s. \<exists>e. 0 < e \<and> closed(cball x e \<inter> s))"
  1.1865 +        (is "?lhs = ?rhs")
  1.1866 +proof
  1.1867 +  assume ?lhs
  1.1868 +  then show ?rhs
  1.1869 +    apply (simp add: locally_compact openin_contains_cball)
  1.1870 +    apply (clarify | assumption | drule bspec)+
  1.1871 +    by (metis (no_types, lifting)  compact_cball compact_imp_closed compact_Int inf.absorb_iff2 inf.orderE inf_sup_aci(2))
  1.1872 +next
  1.1873 +  assume ?rhs
  1.1874 +  then show ?lhs
  1.1875 +    apply (simp add: locally_compact openin_contains_cball)
  1.1876 +    apply (clarify | assumption | drule bspec)+
  1.1877 +    apply (rule_tac x="ball x e \<inter> s" in exI, simp)
  1.1878 +    apply (rule_tac x="cball x e \<inter> s" in exI)
  1.1879 +    using compact_eq_bounded_closed
  1.1880 +    apply auto
  1.1881 +    apply (metis open_ball le_infI1 mem_ball open_contains_cball_eq)
  1.1882 +    done
  1.1883 +qed
  1.1884 +
  1.1885 +lemma locally_compact_compact:
  1.1886 +  fixes s :: "'a :: heine_borel set"
  1.1887 +  shows "locally compact s \<longleftrightarrow>
  1.1888 +         (\<forall>k. k \<subseteq> s \<and> compact k
  1.1889 +              \<longrightarrow> (\<exists>u v. k \<subseteq> u \<and> u \<subseteq> v \<and> v \<subseteq> s \<and>
  1.1890 +                         openin (subtopology euclidean s) u \<and> compact v))"
  1.1891 +        (is "?lhs = ?rhs")
  1.1892 +proof
  1.1893 +  assume ?lhs
  1.1894 +  then obtain u v where
  1.1895 +    uv: "\<And>x. x \<in> s \<Longrightarrow> x \<in> u x \<and> u x \<subseteq> v x \<and> v x \<subseteq> s \<and>
  1.1896 +                             openin (subtopology euclidean s) (u x) \<and> compact (v x)"
  1.1897 +    by (metis locally_compactE)
  1.1898 +  have *: "\<exists>u v. k \<subseteq> u \<and> u \<subseteq> v \<and> v \<subseteq> s \<and> openin (subtopology euclidean s) u \<and> compact v"
  1.1899 +          if "k \<subseteq> s" "compact k" for k
  1.1900 +  proof -
  1.1901 +    have "\<And>C. (\<forall>c\<in>C. openin (subtopology euclidean k) c) \<and> k \<subseteq> \<Union>C \<Longrightarrow>
  1.1902 +                    \<exists>D\<subseteq>C. finite D \<and> k \<subseteq> \<Union>D"
  1.1903 +      using that by (simp add: compact_eq_openin_cover)
  1.1904 +    moreover have "\<forall>c \<in> (\<lambda>x. k \<inter> u x) ` k. openin (subtopology euclidean k) c"
  1.1905 +      using that by clarify (metis subsetD inf.absorb_iff2 openin_subset openin_subtopology_Int_subset topspace_euclidean_subtopology uv)
  1.1906 +    moreover have "k \<subseteq> \<Union>((\<lambda>x. k \<inter> u x) ` k)"
  1.1907 +      using that by clarsimp (meson subsetCE uv)
  1.1908 +    ultimately obtain D where "D \<subseteq> (\<lambda>x. k \<inter> u x) ` k" "finite D" "k \<subseteq> \<Union>D"
  1.1909 +      by metis
  1.1910 +    then obtain T where T: "T \<subseteq> k" "finite T" "k \<subseteq> \<Union>((\<lambda>x. k \<inter> u x) ` T)"
  1.1911 +      by (metis finite_subset_image)
  1.1912 +    have Tuv: "\<Union>(u ` T) \<subseteq> \<Union>(v ` T)"
  1.1913 +      using T that by (force simp: dest!: uv)
  1.1914 +    show ?thesis
  1.1915 +      apply (rule_tac x="\<Union>(u ` T)" in exI)
  1.1916 +      apply (rule_tac x="\<Union>(v ` T)" in exI)
  1.1917 +      apply (simp add: Tuv)
  1.1918 +      using T that
  1.1919 +      apply (auto simp: dest!: uv)
  1.1920 +      done
  1.1921 +  qed
  1.1922 +  show ?rhs
  1.1923 +    by (blast intro: *)
  1.1924 +next
  1.1925 +  assume ?rhs
  1.1926 +  then show ?lhs
  1.1927 +    apply (clarsimp simp add: locally_compact)
  1.1928 +    apply (drule_tac x="{x}" in spec, simp)
  1.1929 +    done
  1.1930 +qed
  1.1931 +
  1.1932 +lemma open_imp_locally_compact:
  1.1933 +  fixes s :: "'a :: heine_borel set"
  1.1934 +  assumes "open s"
  1.1935 +    shows "locally compact s"
  1.1936 +proof -
  1.1937 +  have *: "\<exists>u v. x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> s \<and> openin (subtopology euclidean s) u \<and> compact v"
  1.1938 +          if "x \<in> s" for x
  1.1939 +  proof -
  1.1940 +    obtain e where "e>0" and e: "cball x e \<subseteq> s"
  1.1941 +      using open_contains_cball assms \<open>x \<in> s\<close> by blast
  1.1942 +    have ope: "openin (subtopology euclidean s) (ball x e)"
  1.1943 +      by (meson e open_ball ball_subset_cball dual_order.trans open_subset)
  1.1944 +    show ?thesis
  1.1945 +      apply (rule_tac x="ball x e" in exI)
  1.1946 +      apply (rule_tac x="cball x e" in exI)
  1.1947 +      using \<open>e > 0\<close> e apply (auto simp: ope)
  1.1948 +      done
  1.1949 +  qed
  1.1950 +  show ?thesis
  1.1951 +    unfolding locally_compact
  1.1952 +    by (blast intro: *)
  1.1953 +qed
  1.1954 +
  1.1955 +lemma closed_imp_locally_compact:
  1.1956 +  fixes s :: "'a :: heine_borel set"
  1.1957 +  assumes "closed s"
  1.1958 +    shows "locally compact s"
  1.1959 +proof -
  1.1960 +  have *: "\<exists>u v. x \<in> u \<and> u \<subseteq> v \<and> v \<subseteq> s \<and>
  1.1961 +                 openin (subtopology euclidean s) u \<and> compact v"
  1.1962 +          if "x \<in> s" for x
  1.1963 +  proof -
  1.1964 +    show ?thesis
  1.1965 +      apply (rule_tac x = "s \<inter> ball x 1" in exI)
  1.1966 +      apply (rule_tac x = "s \<inter> cball x 1" in exI)
  1.1967 +      using \<open>x \<in> s\<close> assms apply auto
  1.1968 +      done
  1.1969 +  qed
  1.1970 +  show ?thesis
  1.1971 +    unfolding locally_compact
  1.1972 +    by (blast intro: *)
  1.1973 +qed
  1.1974 +
  1.1975 +lemma locally_compact_UNIV: "locally compact (UNIV :: 'a :: heine_borel set)"
  1.1976 +  by (simp add: closed_imp_locally_compact)
  1.1977 +
  1.1978 +lemma locally_compact_Int:
  1.1979 +  fixes s :: "'a :: t2_space set"
  1.1980 +  shows "\<lbrakk>locally compact s; locally compact t\<rbrakk> \<Longrightarrow> locally compact (s \<inter> t)"
  1.1981 +by (simp add: compact_Int locally_Int)
  1.1982 +
  1.1983 +lemma locally_compact_closedin:
  1.1984 +  fixes s :: "'a :: heine_borel set"
  1.1985 +  shows "\<lbrakk>closedin (subtopology euclidean s) t; locally compact s\<rbrakk>
  1.1986 +        \<Longrightarrow> locally compact t"
  1.1987 +unfolding closedin_closed
  1.1988 +using closed_imp_locally_compact locally_compact_Int by blast
  1.1989 +
  1.1990 +lemma locally_compact_delete:
  1.1991 +     fixes s :: "'a :: t1_space set"
  1.1992 +     shows "locally compact s \<Longrightarrow> locally compact (s - {a})"
  1.1993 +  by (auto simp: openin_delete locally_open_subset)
  1.1994 +
  1.1995 +lemma locally_closed:
  1.1996 +  fixes s :: "'a :: heine_borel set"
  1.1997 +  shows "locally closed s \<longleftrightarrow> locally compact s"
  1.1998 +        (is "?lhs = ?rhs")
  1.1999 +proof
  1.2000 +  assume ?lhs
  1.2001 +  then show ?rhs
  1.2002 +    apply (simp only: locally_def)
  1.2003 +    apply (erule all_forward imp_forward asm_rl exE)+
  1.2004 +    apply (rule_tac x = "u \<inter> ball x 1" in exI)
  1.2005 +    apply (rule_tac x = "v \<inter> cball x 1" in exI)
  1.2006 +    apply (force intro: openin_trans)
  1.2007 +    done
  1.2008 +next
  1.2009 +  assume ?rhs then show ?lhs
  1.2010 +    using compact_eq_bounded_closed locally_mono by blast
  1.2011 +qed
  1.2012 +
  1.2013 +lemma locally_compact_openin_Un:
  1.2014 +  fixes S :: "'a::euclidean_space set"
  1.2015 +  assumes LCS: "locally compact S" and LCT:"locally compact T"
  1.2016 +      and opS: "openin (subtopology euclidean (S \<union> T)) S"
  1.2017 +      and opT: "openin (subtopology euclidean (S \<union> T)) T"
  1.2018 +    shows "locally compact (S \<union> T)"
  1.2019 +proof -
  1.2020 +  have "\<exists>e>0. closed (cball x e \<inter> (S \<union> T))" if "x \<in> S" for x
  1.2021 +  proof -
  1.2022 +    obtain e1 where "e1 > 0" and e1: "closed (cball x e1 \<inter> S)"
  1.2023 +      using LCS \<open>x \<in> S\<close> unfolding locally_compact_Int_cball by blast
  1.2024 +    moreover obtain e2 where "e2 > 0" and e2: "cball x e2 \<inter> (S \<union> T) \<subseteq> S"
  1.2025 +      by (meson \<open>x \<in> S\<close> opS openin_contains_cball)
  1.2026 +    then have "cball x e2 \<inter> (S \<union> T) = cball x e2 \<inter> S"
  1.2027 +      by force
  1.2028 +    ultimately show ?thesis
  1.2029 +      apply (rule_tac x="min e1 e2" in exI)
  1.2030 +      apply (auto simp: cball_min_Int \<open>e2 > 0\<close> inf_assoc closed_Int)
  1.2031 +      by (metis closed_Int closed_cball inf_left_commute)
  1.2032 +  qed
  1.2033 +  moreover have "\<exists>e>0. closed (cball x e \<inter> (S \<union> T))" if "x \<in> T" for x
  1.2034 +  proof -
  1.2035 +    obtain e1 where "e1 > 0" and e1: "closed (cball x e1 \<inter> T)"
  1.2036 +      using LCT \<open>x \<in> T\<close> unfolding locally_compact_Int_cball by blast
  1.2037 +    moreover obtain e2 where "e2 > 0" and e2: "cball x e2 \<inter> (S \<union> T) \<subseteq> T"
  1.2038 +      by (meson \<open>x \<in> T\<close> opT openin_contains_cball)
  1.2039 +    then have "cball x e2 \<inter> (S \<union> T) = cball x e2 \<inter> T"
  1.2040 +      by force
  1.2041 +    ultimately show ?thesis
  1.2042 +      apply (rule_tac x="min e1 e2" in exI)
  1.2043 +      apply (auto simp: cball_min_Int \<open>e2 > 0\<close> inf_assoc closed_Int)
  1.2044 +      by (metis closed_Int closed_cball inf_left_commute)
  1.2045 +  qed
  1.2046 +  ultimately show ?thesis
  1.2047 +    by (force simp: locally_compact_Int_cball)
  1.2048 +qed
  1.2049 +
  1.2050 +lemma locally_compact_closedin_Un:
  1.2051 +  fixes S :: "'a::euclidean_space set"
  1.2052 +  assumes LCS: "locally compact S" and LCT:"locally compact T"
  1.2053 +      and clS: "closedin (subtopology euclidean (S \<union> T)) S"
  1.2054 +      and clT: "closedin (subtopology euclidean (S \<union> T)) T"
  1.2055 +    shows "locally compact (S \<union> T)"
  1.2056 +proof -
  1.2057 +  have "\<exists>e>0. closed (cball x e \<inter> (S \<union> T))" if "x \<in> S" "x \<in> T" for x
  1.2058 +  proof -
  1.2059 +    obtain e1 where "e1 > 0" and e1: "closed (cball x e1 \<inter> S)"
  1.2060 +      using LCS \<open>x \<in> S\<close> unfolding locally_compact_Int_cball by blast
  1.2061 +    moreover
  1.2062 +    obtain e2 where "e2 > 0" and e2: "closed (cball x e2 \<inter> T)"
  1.2063 +      using LCT \<open>x \<in> T\<close> unfolding locally_compact_Int_cball by blast
  1.2064 +    ultimately show ?thesis
  1.2065 +      apply (rule_tac x="min e1 e2" in exI)
  1.2066 +      apply (auto simp: cball_min_Int \<open>e2 > 0\<close> inf_assoc closed_Int Int_Un_distrib)
  1.2067 +      by (metis closed_Int closed_Un closed_cball inf_left_commute)
  1.2068 +  qed
  1.2069 +  moreover
  1.2070 +  have "\<exists>e>0. closed (cball x e \<inter> (S \<union> T))" if x: "x \<in> S" "x \<notin> T" for x
  1.2071 +  proof -
  1.2072 +    obtain e1 where "e1 > 0" and e1: "closed (cball x e1 \<inter> S)"
  1.2073 +      using LCS \<open>x \<in> S\<close> unfolding locally_compact_Int_cball by blast
  1.2074 +    moreover
  1.2075 +    obtain e2 where "e2>0" and "cball x e2 \<inter> (S \<union> T) \<subseteq> S - T"
  1.2076 +      using clT x by (fastforce simp: openin_contains_cball closedin_def)
  1.2077 +    then have "closed (cball x e2 \<inter> T)"
  1.2078 +    proof -
  1.2079 +      have "{} = T - (T - cball x e2)"
  1.2080 +        using Diff_subset Int_Diff \<open>cball x e2 \<inter> (S \<union> T) \<subseteq> S - T\<close> by auto
  1.2081 +      then show ?thesis
  1.2082 +        by (simp add: Diff_Diff_Int inf_commute)
  1.2083 +    qed
  1.2084 +    ultimately show ?thesis
  1.2085 +      apply (rule_tac x="min e1 e2" in exI)
  1.2086 +      apply (auto simp: cball_min_Int \<open>e2 > 0\<close> inf_assoc closed_Int Int_Un_distrib)
  1.2087 +      by (metis closed_Int closed_Un closed_cball inf_left_commute)
  1.2088 +  qed
  1.2089 +  moreover
  1.2090 +  have "\<exists>e>0. closed (cball x e \<inter> (S \<union> T))" if x: "x \<notin> S" "x \<in> T" for x
  1.2091 +  proof -
  1.2092 +    obtain e1 where "e1 > 0" and e1: "closed (cball x e1 \<inter> T)"
  1.2093 +      using LCT \<open>x \<in> T\<close> unfolding locally_compact_Int_cball by blast
  1.2094 +    moreover
  1.2095 +    obtain e2 where "e2>0" and "cball x e2 \<inter> (S \<union> T) \<subseteq> S \<union> T - S"
  1.2096 +      using clS x by (fastforce simp: openin_contains_cball closedin_def)
  1.2097 +    then have "closed (cball x e2 \<inter> S)"
  1.2098 +      by (metis Diff_disjoint Int_empty_right closed_empty inf.left_commute inf.orderE inf_sup_absorb)
  1.2099 +    ultimately show ?thesis
  1.2100 +      apply (rule_tac x="min e1 e2" in exI)
  1.2101 +      apply (auto simp: cball_min_Int \<open>e2 > 0\<close> inf_assoc closed_Int Int_Un_distrib)
  1.2102 +      by (metis closed_Int closed_Un closed_cball inf_left_commute)
  1.2103 +  qed
  1.2104 +  ultimately show ?thesis
  1.2105 +    by (auto simp: locally_compact_Int_cball)
  1.2106 +qed
  1.2107 +
  1.2108 +lemma locally_compact_Times:
  1.2109 +  fixes S :: "'a::euclidean_space set" and T :: "'b::euclidean_space set"
  1.2110 +  shows "\<lbrakk>locally compact S; locally compact T\<rbrakk> \<Longrightarrow> locally compact (S \<times> T)"
  1.2111 +  by (auto simp: compact_Times locally_Times)
  1.2112 +
  1.2113 +lemma locally_compact_compact_subopen:
  1.2114 +  fixes S :: "'a :: heine_borel set"
  1.2115 +  shows
  1.2116 +   "locally compact S \<longleftrightarrow>
  1.2117 +    (\<forall>K T. K \<subseteq> S \<and> compact K \<and> open T \<and> K \<subseteq> T
  1.2118 +          \<longrightarrow> (\<exists>U V. K \<subseteq> U \<and> U \<subseteq> V \<and> U \<subseteq> T \<and> V \<subseteq> S \<and>
  1.2119 +                     openin (subtopology euclidean S) U \<and> compact V))"
  1.2120 +   (is "?lhs = ?rhs")
  1.2121 +proof
  1.2122 +  assume L: ?lhs
  1.2123 +  show ?rhs
  1.2124 +  proof clarify
  1.2125 +    fix K :: "'a set" and T :: "'a set"
  1.2126 +    assume "K \<subseteq> S" and "compact K" and "open T" and "K \<subseteq> T"
  1.2127 +    obtain U V where "K \<subseteq> U" "U \<subseteq> V" "V \<subseteq> S" "compact V"
  1.2128 +                 and ope: "openin (subtopology euclidean S) U"
  1.2129 +      using L unfolding locally_compact_compact by (meson \<open>K \<subseteq> S\<close> \<open>compact K\<close>)
  1.2130 +    show "\<exists>U V. K \<subseteq> U \<and> U \<subseteq> V \<and> U \<subseteq> T \<and> V \<subseteq> S \<and>
  1.2131 +                openin (subtopology euclidean S) U \<and> compact V"
  1.2132 +    proof (intro exI conjI)
  1.2133 +      show "K \<subseteq> U \<inter> T"
  1.2134 +        by (simp add: \<open>K \<subseteq> T\<close> \<open>K \<subseteq> U\<close>)
  1.2135 +      show "U \<inter> T \<subseteq> closure(U \<inter> T)"
  1.2136 +        by (rule closure_subset)
  1.2137 +      show "closure (U \<inter> T) \<subseteq> S"
  1.2138 +        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)
  1.2139 +      show "openin (subtopology euclidean S) (U \<inter> T)"
  1.2140 +        by (simp add: \<open>open T\<close> ope openin_Int_open)
  1.2141 +      show "compact (closure (U \<inter> T))"
  1.2142 +        by (meson Int_lower1 \<open>U \<subseteq> V\<close> \<open>compact V\<close> bounded_subset compact_closure compact_eq_bounded_closed)
  1.2143 +    qed auto
  1.2144 +  qed
  1.2145 +next
  1.2146 +  assume ?rhs then show ?lhs
  1.2147 +    unfolding locally_compact_compact
  1.2148 +    by (metis open_openin openin_topspace subtopology_superset top.extremum topspace_euclidean_subtopology)
  1.2149 +qed
  1.2150 +
  1.2151 +
  1.2152 +subsection\<open>Sura-Bura's results about compact components of sets\<close>
  1.2153 +
  1.2154 +proposition Sura_Bura_compact:
  1.2155 +  fixes S :: "'a::euclidean_space set"
  1.2156 +  assumes "compact S" and C: "C \<in> components S"
  1.2157 +  shows "C = \<Inter>{T. C \<subseteq> T \<and> openin (subtopology euclidean S) T \<and>
  1.2158 +                           closedin (subtopology euclidean S) T}"
  1.2159 +         (is "C = \<Inter>?\<T>")
  1.2160 +proof
  1.2161 +  obtain x where x: "C = connected_component_set S x" and "x \<in> S"
  1.2162 +    using C by (auto simp: components_def)
  1.2163 +  have "C \<subseteq> S"
  1.2164 +    by (simp add: C in_components_subset)
  1.2165 +  have "\<Inter>?\<T> \<subseteq> connected_component_set S x"
  1.2166 +  proof (rule connected_component_maximal)
  1.2167 +    have "x \<in> C"
  1.2168 +      by (simp add: \<open>x \<in> S\<close> x)
  1.2169 +    then show "x \<in> \<Inter>?\<T>"
  1.2170 +      by blast
  1.2171 +    have clo: "closed (\<Inter>?\<T>)"
  1.2172 +      by (simp add: \<open>compact S\<close> closed_Inter closedin_compact_eq compact_imp_closed)
  1.2173 +    have False
  1.2174 +      if K1: "closedin (subtopology euclidean (\<Inter>?\<T>)) K1" and
  1.2175 +         K2: "closedin (subtopology euclidean (\<Inter>?\<T>)) K2" and
  1.2176 +         K12_Int: "K1 \<inter> K2 = {}" and K12_Un: "K1 \<union> K2 = \<Inter>?\<T>" and "K1 \<noteq> {}" "K2 \<noteq> {}"
  1.2177 +       for K1 K2
  1.2178 +    proof -
  1.2179 +      have "closed K1" "closed K2"
  1.2180 +        using closedin_closed_trans clo K1 K2 by blast+
  1.2181 +      then obtain V1 V2 where "open V1" "open V2" "K1 \<subseteq> V1" "K2 \<subseteq> V2" and V12: "V1 \<inter> V2 = {}"
  1.2182 +        using separation_normal \<open>K1 \<inter> K2 = {}\<close> by metis
  1.2183 +      have SV12_ne: "(S - (V1 \<union> V2)) \<inter> (\<Inter>?\<T>) \<noteq> {}"
  1.2184 +      proof (rule compact_imp_fip)
  1.2185 +        show "compact (S - (V1 \<union> V2))"
  1.2186 +          by (simp add: \<open>open V1\<close> \<open>open V2\<close> \<open>compact S\<close> compact_diff open_Un)
  1.2187 +        show clo\<T>: "closed T" if "T \<in> ?\<T>" for T
  1.2188 +          using that \<open>compact S\<close>
  1.2189 +          by (force intro: closedin_closed_trans simp add: compact_imp_closed)
  1.2190 +        show "(S - (V1 \<union> V2)) \<inter> \<Inter>\<F> \<noteq> {}" if "finite \<F>" and \<F>: "\<F> \<subseteq> ?\<T>" for \<F>
  1.2191 +        proof
  1.2192 +          assume djo: "(S - (V1 \<union> V2)) \<inter> \<Inter>\<F> = {}"
  1.2193 +          obtain D where opeD: "openin (subtopology euclidean S) D"
  1.2194 +                   and cloD: "closedin (subtopology euclidean S) D"
  1.2195 +                   and "C \<subseteq> D" and DV12: "D \<subseteq> V1 \<union> V2"
  1.2196 +          proof (cases "\<F> = {}")
  1.2197 +            case True
  1.2198 +            with \<open>C \<subseteq> S\<close> djo that show ?thesis
  1.2199 +              by force
  1.2200 +          next
  1.2201 +            case False show ?thesis
  1.2202 +            proof
  1.2203 +              show ope: "openin (subtopology euclidean S) (\<Inter>\<F>)"
  1.2204 +                using openin_Inter \<open>finite \<F>\<close> False \<F> by blast
  1.2205 +              then show "closedin (subtopology euclidean S) (\<Inter>\<F>)"
  1.2206 +                by (meson clo\<T> \<F> closed_Inter closed_subset openin_imp_subset subset_eq)
  1.2207 +              show "C \<subseteq> \<Inter>\<F>"
  1.2208 +                using \<F> by auto
  1.2209 +              show "\<Inter>\<F> \<subseteq> V1 \<union> V2"
  1.2210 +                using ope djo openin_imp_subset by fastforce
  1.2211 +            qed
  1.2212 +          qed
  1.2213 +          have "connected C"
  1.2214 +            by (simp add: x)
  1.2215 +          have "closed D"
  1.2216 +            using \<open>compact S\<close> cloD closedin_closed_trans compact_imp_closed by blast
  1.2217 +          have cloV1: "closedin (subtopology euclidean D) (D \<inter> closure V1)"
  1.2218 +            and cloV2: "closedin (subtopology euclidean D) (D \<inter> closure V2)"
  1.2219 +            by (simp_all add: closedin_closed_Int)
  1.2220 +          moreover have "D \<inter> closure V1 = D \<inter> V1" "D \<inter> closure V2 = D \<inter> V2"
  1.2221 +            apply safe
  1.2222 +            using \<open>D \<subseteq> V1 \<union> V2\<close> \<open>open V1\<close> \<open>open V2\<close> V12
  1.2223 +               apply (simp_all add: closure_subset [THEN subsetD] closure_iff_nhds_not_empty, blast+)
  1.2224 +            done
  1.2225 +          ultimately have cloDV1: "closedin (subtopology euclidean D) (D \<inter> V1)"
  1.2226 +                      and cloDV2:  "closedin (subtopology euclidean D) (D \<inter> V2)"
  1.2227 +            by metis+
  1.2228 +          then obtain U1 U2 where "closed U1" "closed U2"
  1.2229 +               and D1: "D \<inter> V1 = D \<inter> U1" and D2: "D \<inter> V2 = D \<inter> U2"
  1.2230 +            by (auto simp: closedin_closed)
  1.2231 +          have "D \<inter> U1 \<inter> C \<noteq> {}"
  1.2232 +          proof
  1.2233 +            assume "D \<inter> U1 \<inter> C = {}"
  1.2234 +            then have *: "C \<subseteq> D \<inter> V2"
  1.2235 +              using D1 DV12 \<open>C \<subseteq> D\<close> by auto
  1.2236 +            have "\<Inter>?\<T> \<subseteq> D \<inter> V2"
  1.2237 +              apply (rule Inter_lower)
  1.2238 +              using * apply simp
  1.2239 +              by (meson cloDV2 \<open>open V2\<close> cloD closedin_trans le_inf_iff opeD openin_Int_open)
  1.2240 +            then show False
  1.2241 +              using K1 V12 \<open>K1 \<noteq> {}\<close> \<open>K1 \<subseteq> V1\<close> closedin_imp_subset by blast
  1.2242 +          qed
  1.2243 +          moreover have "D \<inter> U2 \<inter> C \<noteq> {}"
  1.2244 +          proof
  1.2245 +            assume "D \<inter> U2 \<inter> C = {}"
  1.2246 +            then have *: "C \<subseteq> D \<inter> V1"
  1.2247 +              using D2 DV12 \<open>C \<subseteq> D\<close> by auto
  1.2248 +            have "\<Inter>?\<T> \<subseteq> D \<inter> V1"
  1.2249 +              apply (rule Inter_lower)
  1.2250 +              using * apply simp
  1.2251 +              by (meson cloDV1 \<open>open V1\<close> cloD closedin_trans le_inf_iff opeD openin_Int_open)
  1.2252 +            then show False
  1.2253 +              using K2 V12 \<open>K2 \<noteq> {}\<close> \<open>K2 \<subseteq> V2\<close> closedin_imp_subset by blast
  1.2254 +          qed
  1.2255 +          ultimately show False
  1.2256 +            using \<open>connected C\<close> unfolding connected_closed
  1.2257 +            apply (simp only: not_ex)
  1.2258 +            apply (drule_tac x="D \<inter> U1" in spec)
  1.2259 +            apply (drule_tac x="D \<inter> U2" in spec)
  1.2260 +            using \<open>C \<subseteq> D\<close> D1 D2 V12 DV12 \<open>closed U1\<close> \<open>closed U2\<close> \<open>closed D\<close>
  1.2261 +            by blast
  1.2262 +        qed
  1.2263 +      qed
  1.2264 +      show False
  1.2265 +        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)
  1.2266 +    qed
  1.2267 +    then show "connected (\<Inter>?\<T>)"
  1.2268 +      by (auto simp: connected_closedin_eq)
  1.2269 +    show "\<Inter>?\<T> \<subseteq> S"
  1.2270 +      by (fastforce simp: C in_components_subset)
  1.2271 +  qed
  1.2272 +  with x show "\<Inter>?\<T> \<subseteq> C" by simp
  1.2273 +qed auto
  1.2274 +
  1.2275 +
  1.2276 +corollary Sura_Bura_clopen_subset:
  1.2277 +  fixes S :: "'a::euclidean_space set"
  1.2278 +  assumes S: "locally compact S" and C: "C \<in> components S" and "compact C"
  1.2279 +      and U: "open U" "C \<subseteq> U"
  1.2280 +  obtains K where "openin (subtopology euclidean S) K" "compact K" "C \<subseteq> K" "K \<subseteq> U"
  1.2281 +proof (rule ccontr)
  1.2282 +  assume "\<not> thesis"
  1.2283 +  with that have neg: "\<nexists>K. openin (subtopology euclidean S) K \<and> compact K \<and> C \<subseteq> K \<and> K \<subseteq> U"
  1.2284 +    by metis
  1.2285 +  obtain V K where "C \<subseteq> V" "V \<subseteq> U" "V \<subseteq> K" "K \<subseteq> S" "compact K"
  1.2286 +               and opeSV: "openin (subtopology euclidean S) V"
  1.2287 +    using S U \<open>compact C\<close>
  1.2288 +    apply (simp add: locally_compact_compact_subopen)
  1.2289 +    by (meson C in_components_subset)
  1.2290 +  let ?\<T> = "{T. C \<subseteq> T \<and> openin (subtopology euclidean K) T \<and> compact T \<and> T \<subseteq> K}"
  1.2291 +  have CK: "C \<in> components K"
  1.2292 +    by (meson C \<open>C \<subseteq> V\<close> \<open>K \<subseteq> S\<close> \<open>V \<subseteq> K\<close> components_intermediate_subset subset_trans)
  1.2293 +  with \<open>compact K\<close>
  1.2294 +  have "C = \<Inter>{T. C \<subseteq> T \<and> openin (subtopology euclidean K) T \<and> closedin (subtopology euclidean K) T}"
  1.2295 +    by (simp add: Sura_Bura_compact)
  1.2296 +  then have Ceq: "C = \<Inter>?\<T>"
  1.2297 +    by (simp add: closedin_compact_eq \<open>compact K\<close>)
  1.2298 +  obtain W where "open W" and W: "V = S \<inter> W"
  1.2299 +    using opeSV by (auto simp: openin_open)
  1.2300 +  have "-(U \<inter> W) \<inter> \<Inter>?\<T> \<noteq> {}"
  1.2301 +  proof (rule closed_imp_fip_compact)
  1.2302 +    show "- (U \<inter> W) \<inter> \<Inter>\<F> \<noteq> {}"
  1.2303 +      if "finite \<F>" and \<F>: "\<F> \<subseteq> ?\<T>" for \<F>
  1.2304 +    proof (cases "\<F> = {}")
  1.2305 +      case True
  1.2306 +      have False if "U = UNIV" "W = UNIV"
  1.2307 +      proof -
  1.2308 +        have "V = S"
  1.2309 +          by (simp add: W \<open>W = UNIV\<close>)
  1.2310 +        with neg show False
  1.2311 +          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
  1.2312 +      qed
  1.2313 +      with True show ?thesis
  1.2314 +        by auto
  1.2315 +    next
  1.2316 +      case False
  1.2317 +      show ?thesis
  1.2318 +      proof
  1.2319 +        assume "- (U \<inter> W) \<inter> \<Inter>\<F> = {}"
  1.2320 +        then have FUW: "\<Inter>\<F> \<subseteq> U \<inter> W"
  1.2321 +          by blast
  1.2322 +        have "C \<subseteq> \<Inter>\<F>"
  1.2323 +          using \<F> by auto
  1.2324 +        moreover have "compact (\<Inter>\<F>)"
  1.2325 +          by (metis (no_types, lifting) compact_Inter False mem_Collect_eq subsetCE \<F>)
  1.2326 +        moreover have "\<Inter>\<F> \<subseteq> K"
  1.2327 +          using False that(2) by fastforce
  1.2328 +        moreover have opeKF: "openin (subtopology euclidean K) (\<Inter>\<F>)"
  1.2329 +          using False \<F> \<open>finite \<F>\<close> by blast
  1.2330 +        then have opeVF: "openin (subtopology euclidean V) (\<Inter>\<F>)"
  1.2331 +          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
  1.2332 +        then have "openin (subtopology euclidean S) (\<Inter>\<F>)"
  1.2333 +          by (metis opeSV openin_trans)
  1.2334 +        moreover have "\<Inter>\<F> \<subseteq> U"
  1.2335 +          by (meson \<open>V \<subseteq> U\<close> opeVF dual_order.trans openin_imp_subset)
  1.2336 +        ultimately show False
  1.2337 +          using neg by blast
  1.2338 +      qed
  1.2339 +    qed
  1.2340 +  qed (use \<open>open W\<close> \<open>open U\<close> in auto)
  1.2341 +  with W Ceq \<open>C \<subseteq> V\<close> \<open>C \<subseteq> U\<close> show False
  1.2342 +    by auto
  1.2343 +qed
  1.2344 +
  1.2345 +
  1.2346 +corollary Sura_Bura_clopen_subset_alt:
  1.2347 +  fixes S :: "'a::euclidean_space set"
  1.2348 +  assumes S: "locally compact S" and C: "C \<in> components S" and "compact C"
  1.2349 +      and opeSU: "openin (subtopology euclidean S) U" and "C \<subseteq> U"
  1.2350 +  obtains K where "openin (subtopology euclidean S) K" "compact K" "C \<subseteq> K" "K \<subseteq> U"
  1.2351 +proof -
  1.2352 +  obtain V where "open V" "U = S \<inter> V"
  1.2353 +    using opeSU by (auto simp: openin_open)
  1.2354 +  with \<open>C \<subseteq> U\<close> have "C \<subseteq> V"
  1.2355 +    by auto
  1.2356 +  then show ?thesis
  1.2357 +    using Sura_Bura_clopen_subset [OF S C \<open>compact C\<close> \<open>open V\<close>]
  1.2358 +    by (metis \<open>U = S \<inter> V\<close> inf.bounded_iff openin_imp_subset that)
  1.2359 +qed
  1.2360 +
  1.2361 +corollary Sura_Bura:
  1.2362 +  fixes S :: "'a::euclidean_space set"
  1.2363 +  assumes "locally compact S" "C \<in> components S" "compact C"
  1.2364 +  shows "C = \<Inter> {K. C \<subseteq> K \<and> compact K \<and> openin (subtopology euclidean S) K}"
  1.2365 +         (is "C = ?rhs")
  1.2366 +proof
  1.2367 +  show "?rhs \<subseteq> C"
  1.2368 +  proof (clarsimp, rule ccontr)
  1.2369 +    fix x
  1.2370 +    assume *: "\<forall>X. C \<subseteq> X \<and> compact X \<and> openin (subtopology euclidean S) X \<longrightarrow> x \<in> X"
  1.2371 +      and "x \<notin> C"
  1.2372 +    obtain U V where "open U" "open V" "{x} \<subseteq> U" "C \<subseteq> V" "U \<inter> V = {}"
  1.2373 +      using separation_normal [of "{x}" C]
  1.2374 +      by (metis Int_empty_left \<open>x \<notin> C\<close> \<open>compact C\<close> closed_empty closed_insert compact_imp_closed insert_disjoint(1))
  1.2375 +    have "x \<notin> V"
  1.2376 +      using \<open>U \<inter> V = {}\<close> \<open>{x} \<subseteq> U\<close> by blast
  1.2377 +    then show False
  1.2378 +      by (meson "*" Sura_Bura_clopen_subset \<open>C \<subseteq> V\<close> \<open>open V\<close> assms(1) assms(2) assms(3) subsetCE)
  1.2379 +  qed
  1.2380 +qed blast
  1.2381 +
  1.2382 +
  1.2383 +subsection\<open>Special cases of local connectedness and path connectedness\<close>
  1.2384 +
  1.2385 +lemma locally_connected_1:
  1.2386 +  assumes
  1.2387 +    "\<And>v x. \<lbrakk>openin (subtopology euclidean S) v; x \<in> v\<rbrakk>
  1.2388 +              \<Longrightarrow> \<exists>u. openin (subtopology euclidean S) u \<and>
  1.2389 +                      connected u \<and> x \<in> u \<and> u \<subseteq> v"
  1.2390 +   shows "locally connected S"
  1.2391 +apply (clarsimp simp add: locally_def)
  1.2392 +apply (drule assms; blast)
  1.2393 +done
  1.2394 +
  1.2395 +lemma locally_connected_2:
  1.2396 +  assumes "locally connected S"
  1.2397 +          "openin (subtopology euclidean S) t"
  1.2398 +          "x \<in> t"
  1.2399 +   shows "openin (subtopology euclidean S) (connected_component_set t x)"
  1.2400 +proof -
  1.2401 +  { fix y :: 'a
  1.2402 +    let ?SS = "subtopology euclidean S"
  1.2403 +    assume 1: "openin ?SS t"
  1.2404 +              "\<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))"
  1.2405 +    and "connected_component t x y"
  1.2406 +    then have "y \<in> t" and y: "y \<in> connected_component_set t x"
  1.2407 +      using connected_component_subset by blast+
  1.2408 +    obtain F where
  1.2409 +      "\<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))"
  1.2410 +      by moura
  1.2411 +    then obtain G where
  1.2412 +       "\<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)"
  1.2413 +      by moura
  1.2414 +    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"
  1.2415 +      using 1 \<open>y \<in> t\<close> by presburger
  1.2416 +    have "G y t \<subseteq> connected_component_set t y"
  1.2417 +      by (metis (no_types) * connected_component_eq_self connected_component_mono contra_subsetD)
  1.2418 +    then have "\<exists>A. openin ?SS A \<and> y \<in> A \<and> A \<subseteq> connected_component_set t x"
  1.2419 +      by (metis (no_types) * connected_component_eq dual_order.trans y)
  1.2420 +  }
  1.2421 +  then show ?thesis
  1.2422 +    using assms openin_subopen by (force simp: locally_def)
  1.2423 +qed
  1.2424 +
  1.2425 +lemma locally_connected_3:
  1.2426 +  assumes "\<And>t x. \<lbrakk>openin (subtopology euclidean S) t; x \<in> t\<rbrakk>
  1.2427 +              \<Longrightarrow> openin (subtopology euclidean S)
  1.2428 +                          (connected_component_set t x)"
  1.2429 +          "openin (subtopology euclidean S) v" "x \<in> v"
  1.2430 +   shows  "\<exists>u. openin (subtopology euclidean S) u \<and> connected u \<and> x \<in> u \<and> u \<subseteq> v"
  1.2431 +using assms connected_component_subset by fastforce
  1.2432 +
  1.2433 +lemma locally_connected:
  1.2434 +  "locally connected S \<longleftrightarrow>
  1.2435 +   (\<forall>v x. openin (subtopology euclidean S) v \<and> x \<in> v
  1.2436 +          \<longrightarrow> (\<exists>u. openin (subtopology euclidean S) u \<and> connected u \<and> x \<in> u \<and> u \<subseteq> v))"
  1.2437 +by (metis locally_connected_1 locally_connected_2 locally_connected_3)
  1.2438 +
  1.2439 +lemma locally_connected_open_connected_component:
  1.2440 +  "locally connected S \<longleftrightarrow>
  1.2441 +   (\<forall>t x. openin (subtopology euclidean S) t \<and> x \<in> t
  1.2442 +          \<longrightarrow> openin (subtopology euclidean S) (connected_component_set t x))"
  1.2443 +by (metis locally_connected_1 locally_connected_2 locally_connected_3)
  1.2444 +
  1.2445 +lemma locally_path_connected_1:
  1.2446 +  assumes
  1.2447 +    "\<And>v x. \<lbrakk>openin (subtopology euclidean S) v; x \<in> v\<rbrakk>
  1.2448 +              \<Longrightarrow> \<exists>u. openin (subtopology euclidean S) u \<and> path_connected u \<and> x \<in> u \<and> u \<subseteq> v"
  1.2449 +   shows "locally path_connected S"
  1.2450 +apply (clarsimp simp add: locally_def)
  1.2451 +apply (drule assms; blast)
  1.2452 +done
  1.2453 +
  1.2454 +lemma locally_path_connected_2:
  1.2455 +  assumes "locally path_connected S"
  1.2456 +          "openin (subtopology euclidean S) t"
  1.2457 +          "x \<in> t"
  1.2458 +   shows "openin (subtopology euclidean S) (path_component_set t x)"
  1.2459 +proof -
  1.2460 +  { fix y :: 'a
  1.2461 +    let ?SS = "subtopology euclidean S"
  1.2462 +    assume 1: "openin ?SS t"
  1.2463 +              "\<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))"
  1.2464 +    and "path_component t x y"
  1.2465 +    then have "y \<in> t" and y: "y \<in> path_component_set t x"
  1.2466 +      using path_component_mem(2) by blast+
  1.2467 +    obtain F where
  1.2468 +      "\<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))"
  1.2469 +      by moura
  1.2470 +    then obtain G where
  1.2471 +       "\<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)"
  1.2472 +      by moura
  1.2473 +    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"
  1.2474 +      using 1 \<open>y \<in> t\<close> by presburger
  1.2475 +    have "G y t \<subseteq> path_component_set t y"
  1.2476 +      using * path_component_maximal set_rev_mp by blast
  1.2477 +    then have "\<exists>A. openin ?SS A \<and> y \<in> A \<and> A \<subseteq> path_component_set t x"
  1.2478 +      by (metis "*" \<open>G y t \<subseteq> path_component_set t y\<close> dual_order.trans path_component_eq y)
  1.2479 +  }
  1.2480 +  then show ?thesis
  1.2481 +    using assms openin_subopen by (force simp: locally_def)
  1.2482 +qed
  1.2483 +
  1.2484 +lemma locally_path_connected_3:
  1.2485 +  assumes "\<And>t x. \<lbrakk>openin (subtopology euclidean S) t; x \<in> t\<rbrakk>
  1.2486 +              \<Longrightarrow> openin (subtopology euclidean S) (path_component_set t x)"
  1.2487 +          "openin (subtopology euclidean S) v" "x \<in> v"
  1.2488 +   shows  "\<exists>u. openin (subtopology euclidean S) u \<and> path_connected u \<and> x \<in> u \<and> u \<subseteq> v"
  1.2489 +proof -
  1.2490 +  have "path_component v x x"
  1.2491 +    by (meson assms(3) path_component_refl)
  1.2492 +  then show ?thesis
  1.2493 +    by (metis assms(1) assms(2) assms(3) mem_Collect_eq path_component_subset path_connected_path_component)
  1.2494 +qed
  1.2495 +
  1.2496 +proposition locally_path_connected:
  1.2497 +  "locally path_connected S \<longleftrightarrow>
  1.2498 +   (\<forall>v x. openin (subtopology euclidean S) v \<and> x \<in> v
  1.2499 +          \<longrightarrow> (\<exists>u. openin (subtopology euclidean S) u \<and> path_connected u \<and> x \<in> u \<and> u \<subseteq> v))"
  1.2500 +  by (metis locally_path_connected_1 locally_path_connected_2 locally_path_connected_3)
  1.2501 +
  1.2502 +proposition locally_path_connected_open_path_component:
  1.2503 +  "locally path_connected S \<longleftrightarrow>
  1.2504 +   (\<forall>t x. openin (subtopology euclidean S) t \<and> x \<in> t
  1.2505 +          \<longrightarrow> openin (subtopology euclidean S) (path_component_set t x))"
  1.2506 +  by (metis locally_path_connected_1 locally_path_connected_2 locally_path_connected_3)
  1.2507 +
  1.2508 +lemma locally_connected_open_component:
  1.2509 +  "locally connected S \<longleftrightarrow>
  1.2510 +   (\<forall>t c. openin (subtopology euclidean S) t \<and> c \<in> components t
  1.2511 +          \<longrightarrow> openin (subtopology euclidean S) c)"
  1.2512 +by (metis components_iff locally_connected_open_connected_component)
  1.2513 +
  1.2514 +proposition locally_connected_im_kleinen:
  1.2515 +  "locally connected S \<longleftrightarrow>
  1.2516 +   (\<forall>v x. openin (subtopology euclidean S) v \<and> x \<in> v
  1.2517 +       \<longrightarrow> (\<exists>u. openin (subtopology euclidean S) u \<and>
  1.2518 +                x \<in> u \<and> u \<subseteq> v \<and>
  1.2519 +                (\<forall>y. y \<in> u \<longrightarrow> (\<exists>c. connected c \<and> c \<subseteq> v \<and> x \<in> c \<and> y \<in> c))))"
  1.2520 +   (is "?lhs = ?rhs")
  1.2521 +proof
  1.2522 +  assume ?lhs
  1.2523 +  then show ?rhs
  1.2524 +    by (fastforce simp add: locally_connected)
  1.2525 +next
  1.2526 +  assume ?rhs
  1.2527 +  have *: "\<exists>T. openin (subtopology euclidean S) T \<and> x \<in> T \<and> T \<subseteq> c"
  1.2528 +       if "openin (subtopology euclidean S) t" and c: "c \<in> components t" and "x \<in> c" for t c x
  1.2529 +  proof -
  1.2530 +    from that \<open>?rhs\<close> [rule_format, of t x]
  1.2531 +    obtain u where u:
  1.2532 +      "openin (subtopology euclidean S) u \<and> x \<in> u \<and> u \<subseteq> t \<and>
  1.2533 +       (\<forall>y. y \<in> u \<longrightarrow> (\<exists>c. connected c \<and> c \<subseteq> t \<and> x \<in> c \<and> y \<in> c))"
  1.2534 +      using in_components_subset by auto
  1.2535 +    obtain F :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a" where
  1.2536 +      "\<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))"
  1.2537 +      by moura
  1.2538 +    then have F: "F t c \<in> t \<and> c = connected_component_set t (F t c)"
  1.2539 +      by (meson components_iff c)
  1.2540 +    obtain G :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a" where
  1.2541 +        G: "\<forall>x y. (\<exists>z. z \<in> y \<and> z \<notin> x) = (G x y \<in> y \<and> G x y \<notin> x)"
  1.2542 +      by moura
  1.2543 +     have "G c u \<notin> u \<or> G c u \<in> c"
  1.2544 +      using F by (metis (full_types) u connected_componentI connected_component_eq mem_Collect_eq that(3))
  1.2545 +    then show ?thesis
  1.2546 +      using G u by auto
  1.2547 +  qed
  1.2548 +  show ?lhs
  1.2549 +    apply (clarsimp simp add: locally_connected_open_component)
  1.2550 +    apply (subst openin_subopen)
  1.2551 +    apply (blast intro: *)
  1.2552 +    done
  1.2553 +qed
  1.2554 +
  1.2555 +proposition locally_path_connected_im_kleinen:
  1.2556 +  "locally path_connected S \<longleftrightarrow>
  1.2557 +   (\<forall>v x. openin (subtopology euclidean S) v \<and> x \<in> v
  1.2558 +       \<longrightarrow> (\<exists>u. openin (subtopology euclidean S) u \<and>
  1.2559 +                x \<in> u \<and> u \<subseteq> v \<and>
  1.2560 +                (\<forall>y. y \<in> u \<longrightarrow> (\<exists>p. path p \<and> path_image p \<subseteq> v \<and>
  1.2561 +                                pathstart p = x \<and> pathfinish p = y))))"
  1.2562 +   (is "?lhs = ?rhs")
  1.2563 +proof
  1.2564 +  assume ?lhs
  1.2565 +  then show ?rhs
  1.2566 +    apply (simp add: locally_path_connected path_connected_def)
  1.2567 +    apply (erule all_forward ex_forward imp_forward conjE | simp)+
  1.2568 +    by (meson dual_order.trans)
  1.2569 +next
  1.2570 +  assume ?rhs
  1.2571 +  have *: "\<exists>T. openin (subtopology euclidean S) T \<and>
  1.2572 +               x \<in> T \<and> T \<subseteq> path_component_set u z"
  1.2573 +       if "openin (subtopology euclidean S) u" and "z \<in> u" and c: "path_component u z x" for u z x
  1.2574 +  proof -
  1.2575 +    have "x \<in> u"
  1.2576 +      by (meson c path_component_mem(2))
  1.2577 +    with that \<open>?rhs\<close> [rule_format, of u x]
  1.2578 +    obtain U where U:
  1.2579 +      "openin (subtopology euclidean S) U \<and> x \<in> U \<and> U \<subseteq> u \<and>
  1.2580 +       (\<forall>y. y \<in> U \<longrightarrow> (\<exists>p. path p \<and> path_image p \<subseteq> u \<and> pathstart p = x \<and> pathfinish p = y))"
  1.2581 +       by blast
  1.2582 +    show ?thesis
  1.2583 +      apply (rule_tac x=U in exI)
  1.2584 +      apply (auto simp: U)
  1.2585 +      apply (metis U c path_component_trans path_component_def)
  1.2586 +      done
  1.2587 +  qed
  1.2588 +  show ?lhs
  1.2589 +    apply (clarsimp simp add: locally_path_connected_open_path_component)
  1.2590 +    apply (subst openin_subopen)
  1.2591 +    apply (blast intro: *)
  1.2592 +    done
  1.2593 +qed
  1.2594 +
  1.2595 +lemma locally_path_connected_imp_locally_connected:
  1.2596 +  "locally path_connected S \<Longrightarrow> locally connected S"
  1.2597 +using locally_mono path_connected_imp_connected by blast
  1.2598 +
  1.2599 +lemma locally_connected_components:
  1.2600 +  "\<lbrakk>locally connected S; c \<in> components S\<rbrakk> \<Longrightarrow> locally connected c"
  1.2601 +by (meson locally_connected_open_component locally_open_subset openin_subtopology_self)
  1.2602 +
  1.2603 +lemma locally_path_connected_components:
  1.2604 +  "\<lbrakk>locally path_connected S; c \<in> components S\<rbrakk> \<Longrightarrow> locally path_connected c"
  1.2605 +by (meson locally_connected_open_component locally_open_subset locally_path_connected_imp_locally_connected openin_subtopology_self)
  1.2606 +
  1.2607 +lemma locally_path_connected_connected_component:
  1.2608 +  "locally path_connected S \<Longrightarrow> locally path_connected (connected_component_set S x)"
  1.2609 +by (metis components_iff connected_component_eq_empty locally_empty locally_path_connected_components)
  1.2610 +
  1.2611 +lemma open_imp_locally_path_connected:
  1.2612 +  fixes S :: "'a :: real_normed_vector set"
  1.2613 +  shows "open S \<Longrightarrow> locally path_connected S"
  1.2614 +apply (rule locally_mono [of convex])
  1.2615 +apply (simp_all add: locally_def openin_open_eq convex_imp_path_connected)
  1.2616 +apply (meson open_ball centre_in_ball convex_ball openE order_trans)
  1.2617 +done
  1.2618 +
  1.2619 +lemma open_imp_locally_connected:
  1.2620 +  fixes S :: "'a :: real_normed_vector set"
  1.2621 +  shows "open S \<Longrightarrow> locally connected S"
  1.2622 +by (simp add: locally_path_connected_imp_locally_connected open_imp_locally_path_connected)
  1.2623 +
  1.2624 +lemma locally_path_connected_UNIV: "locally path_connected (UNIV::'a :: real_normed_vector set)"
  1.2625 +  by (simp add: open_imp_locally_path_connected)
  1.2626 +
  1.2627 +lemma locally_connected_UNIV: "locally connected (UNIV::'a :: real_normed_vector set)"
  1.2628 +  by (simp add: open_imp_locally_connected)
  1.2629 +
  1.2630 +lemma openin_connected_component_locally_connected:
  1.2631 +    "locally connected S
  1.2632 +     \<Longrightarrow> openin (subtopology euclidean S) (connected_component_set S x)"
  1.2633 +apply (simp add: locally_connected_open_connected_component)
  1.2634 +by (metis connected_component_eq_empty connected_component_subset open_empty open_subset openin_subtopology_self)
  1.2635 +
  1.2636 +lemma openin_components_locally_connected:
  1.2637 +    "\<lbrakk>locally connected S; c \<in> components S\<rbrakk> \<Longrightarrow> openin (subtopology euclidean S) c"
  1.2638 +  using locally_connected_open_component openin_subtopology_self by blast
  1.2639 +
  1.2640 +lemma openin_path_component_locally_path_connected:
  1.2641 +  "locally path_connected S
  1.2642 +        \<Longrightarrow> openin (subtopology euclidean S) (path_component_set S x)"
  1.2643 +by (metis (no_types) empty_iff locally_path_connected_2 openin_subopen openin_subtopology_self path_component_eq_empty)
  1.2644 +
  1.2645 +lemma closedin_path_component_locally_path_connected:
  1.2646 +    "locally path_connected S
  1.2647 +        \<Longrightarrow> closedin (subtopology euclidean S) (path_component_set S x)"
  1.2648 +apply  (simp add: closedin_def path_component_subset complement_path_component_Union)
  1.2649 +apply (rule openin_Union)
  1.2650 +using openin_path_component_locally_path_connected by auto
  1.2651 +
  1.2652 +lemma convex_imp_locally_path_connected:
  1.2653 +  fixes S :: "'a:: real_normed_vector set"
  1.2654 +  shows "convex S \<Longrightarrow> locally path_connected S"
  1.2655 +apply (clarsimp simp add: locally_path_connected)
  1.2656 +apply (subst (asm) openin_open)
  1.2657 +apply clarify
  1.2658 +apply (erule (1) openE)
  1.2659 +apply (rule_tac x = "S \<inter> ball x e" in exI)
  1.2660 +apply (force simp: convex_Int convex_imp_path_connected)
  1.2661 +done
  1.2662 +
  1.2663 +lemma convex_imp_locally_connected:
  1.2664 +  fixes S :: "'a:: real_normed_vector set"
  1.2665 +  shows "convex S \<Longrightarrow> locally connected S"
  1.2666 +  by (simp add: locally_path_connected_imp_locally_connected convex_imp_locally_path_connected)
  1.2667 +
  1.2668 +
  1.2669 +subsection\<open>Relations between components and path components\<close>
  1.2670 +
  1.2671 +lemma path_component_eq_connected_component:
  1.2672 +  assumes "locally path_connected S"
  1.2673 +    shows "(path_component S x = connected_component S x)"
  1.2674 +proof (cases "x \<in> S")
  1.2675 +  case True
  1.2676 +  have "openin (subtopology euclidean (connected_component_set S x)) (path_component_set S x)"
  1.2677 +    apply (rule openin_subset_trans [of S])
  1.2678 +    apply (intro conjI openin_path_component_locally_path_connected [OF assms])
  1.2679 +    using path_component_subset_connected_component   apply (auto simp: connected_component_subset)
  1.2680 +    done
  1.2681 +  moreover have "closedin (subtopology euclidean (connected_component_set S x)) (path_component_set S x)"
  1.2682 +    apply (rule closedin_subset_trans [of S])
  1.2683 +    apply (intro conjI closedin_path_component_locally_path_connected [OF assms])
  1.2684 +    using path_component_subset_connected_component   apply (auto simp: connected_component_subset)
  1.2685 +    done
  1.2686 +  ultimately have *: "path_component_set S x = connected_component_set S x"
  1.2687 +    by (metis connected_connected_component connected_clopen True path_component_eq_empty)
  1.2688 +  then show ?thesis
  1.2689 +    by blast
  1.2690 +next
  1.2691 +  case False then show ?thesis
  1.2692 +    by (metis Collect_empty_eq_bot connected_component_eq_empty path_component_eq_empty)
  1.2693 +qed
  1.2694 +
  1.2695 +lemma path_component_eq_connected_component_set:
  1.2696 +     "locally path_connected S \<Longrightarrow> (path_component_set S x = connected_component_set S x)"
  1.2697 +by (simp add: path_component_eq_connected_component)
  1.2698 +
  1.2699 +lemma locally_path_connected_path_component:
  1.2700 +     "locally path_connected S \<Longrightarrow> locally path_connected (path_component_set S x)"
  1.2701 +using locally_path_connected_connected_component path_component_eq_connected_component by fastforce
  1.2702 +
  1.2703 +lemma open_path_connected_component:
  1.2704 +  fixes S :: "'a :: real_normed_vector set"
  1.2705 +  shows "open S \<Longrightarrow> path_component S x = connected_component S x"
  1.2706 +by (simp add: path_component_eq_connected_component open_imp_locally_path_connected)
  1.2707 +
  1.2708 +lemma open_path_connected_component_set:
  1.2709 +  fixes S :: "'a :: real_normed_vector set"
  1.2710 +  shows "open S \<Longrightarrow> path_component_set S x = connected_component_set S x"
  1.2711 +by (simp add: open_path_connected_component)
  1.2712 +
  1.2713 +proposition locally_connected_quotient_image:
  1.2714 +  assumes lcS: "locally connected S"
  1.2715 +      and oo: "\<And>T. T \<subseteq> f ` S
  1.2716 +                \<Longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` T) \<longleftrightarrow>
  1.2717 +                    openin (subtopology euclidean (f ` S)) T"
  1.2718 +    shows "locally connected (f ` S)"
  1.2719 +proof (clarsimp simp: locally_connected_open_component)
  1.2720 +  fix U C
  1.2721 +  assume opefSU: "openin (subtopology euclidean (f ` S)) U" and "C \<in> components U"
  1.2722 +  then have "C \<subseteq> U" "U \<subseteq> f ` S"
  1.2723 +    by (meson in_components_subset openin_imp_subset)+
  1.2724 +  then have "openin (subtopology euclidean (f ` S)) C \<longleftrightarrow>
  1.2725 +             openin (subtopology euclidean S) (S \<inter> f -` C)"
  1.2726 +    by (auto simp: oo)
  1.2727 +  moreover have "openin (subtopology euclidean S) (S \<inter> f -` C)"
  1.2728 +  proof (subst openin_subopen, clarify)
  1.2729 +    fix x
  1.2730 +    assume "x \<in> S" "f x \<in> C"
  1.2731 +    show "\<exists>T. openin (subtopology euclidean S) T \<and> x \<in> T \<and> T \<subseteq> (S \<inter> f -` C)"
  1.2732 +    proof (intro conjI exI)
  1.2733 +      show "openin (subtopology euclidean S) (connected_component_set (S \<inter> f -` U) x)"
  1.2734 +      proof (rule ccontr)
  1.2735 +        assume **: "\<not> openin (subtopology euclidean S) (connected_component_set (S \<inter> f -` U) x)"
  1.2736 +        then have "x \<notin> (S \<inter> f -` U)"
  1.2737 +          using \<open>U \<subseteq> f ` S\<close> opefSU lcS locally_connected_2 oo by blast
  1.2738 +        with ** show False
  1.2739 +          by (metis (no_types) connected_component_eq_empty empty_iff openin_subopen)
  1.2740 +      qed
  1.2741 +    next
  1.2742 +      show "x \<in> connected_component_set (S \<inter> f -` U) x"
  1.2743 +        using \<open>C \<subseteq> U\<close> \<open>f x \<in> C\<close> \<open>x \<in> S\<close> by auto
  1.2744 +    next
  1.2745 +      have contf: "continuous_on S f"
  1.2746 +        by (simp add: continuous_on_open oo openin_imp_subset)
  1.2747 +      then have "continuous_on (connected_component_set (S \<inter> f -` U) x) f"
  1.2748 +        apply (rule continuous_on_subset)
  1.2749 +        using connected_component_subset apply blast
  1.2750 +        done
  1.2751 +      then have "connected (f ` connected_component_set (S \<inter> f -` U) x)"
  1.2752 +        by (rule connected_continuous_image [OF _ connected_connected_component])
  1.2753 +      moreover have "f ` connected_component_set (S \<inter> f -` U) x \<subseteq> U"
  1.2754 +        using connected_component_in by blast
  1.2755 +      moreover have "C \<inter> f ` connected_component_set (S \<inter> f -` U) x \<noteq> {}"
  1.2756 +        using \<open>C \<subseteq> U\<close> \<open>f x \<in> C\<close> \<open>x \<in> S\<close> by fastforce
  1.2757 +      ultimately have fC: "f ` (connected_component_set (S \<inter> f -` U) x) \<subseteq> C"
  1.2758 +        by (rule components_maximal [OF \<open>C \<in> components U\<close>])
  1.2759 +      have cUC: "connected_component_set (S \<inter> f -` U) x \<subseteq> (S \<inter> f -` C)"
  1.2760 +        using connected_component_subset fC by blast
  1.2761 +      have "connected_component_set (S \<inter> f -` U) x \<subseteq> connected_component_set (S \<inter> f -` C) x"
  1.2762 +      proof -
  1.2763 +        { assume "x \<in> connected_component_set (S \<inter> f -` U) x"
  1.2764 +          then have ?thesis
  1.2765 +            using cUC connected_component_idemp connected_component_mono by blast }
  1.2766 +        then show ?thesis
  1.2767 +          using connected_component_eq_empty by auto
  1.2768 +      qed
  1.2769 +      also have "\<dots> \<subseteq> (S \<inter> f -` C)"
  1.2770 +        by (rule connected_component_subset)
  1.2771 +      finally show "connected_component_set (S \<inter> f -` U) x \<subseteq> (S \<inter> f -` C)" .
  1.2772 +    qed
  1.2773 +  qed
  1.2774 +  ultimately show "openin (subtopology euclidean (f ` S)) C"
  1.2775 +    by metis
  1.2776 +qed
  1.2777 +
  1.2778 +text\<open>The proof resembles that above but is not identical!\<close>
  1.2779 +proposition locally_path_connected_quotient_image:
  1.2780 +  assumes lcS: "locally path_connected S"
  1.2781 +      and oo: "\<And>T. T \<subseteq> f ` S
  1.2782 +                \<Longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` T) \<longleftrightarrow> openin (subtopology euclidean (f ` S)) T"
  1.2783 +    shows "locally path_connected (f ` S)"
  1.2784 +proof (clarsimp simp: locally_path_connected_open_path_component)
  1.2785 +  fix U y
  1.2786 +  assume opefSU: "openin (subtopology euclidean (f ` S)) U" and "y \<in> U"
  1.2787 +  then have "path_component_set U y \<subseteq> U" "U \<subseteq> f ` S"
  1.2788 +    by (meson path_component_subset openin_imp_subset)+
  1.2789 +  then have "openin (subtopology euclidean (f ` S)) (path_component_set U y) \<longleftrightarrow>
  1.2790 +             openin (subtopology euclidean S) (S \<inter> f -` path_component_set U y)"
  1.2791 +  proof -
  1.2792 +    have "path_component_set U y \<subseteq> f ` S"
  1.2793 +      using \<open>U \<subseteq> f ` S\<close> \<open>path_component_set U y \<subseteq> U\<close> by blast
  1.2794 +    then show ?thesis
  1.2795 +      using oo by blast
  1.2796 +  qed
  1.2797 +  moreover have "openin (subtopology euclidean S) (S \<inter> f -` path_component_set U y)"
  1.2798 +  proof (subst openin_subopen, clarify)
  1.2799 +    fix x
  1.2800 +    assume "x \<in> S" and Uyfx: "path_component U y (f x)"
  1.2801 +    then have "f x \<in> U"
  1.2802 +      using path_component_mem by blast
  1.2803 +    show "\<exists>T. openin (subtopology euclidean S) T \<and> x \<in> T \<and> T \<subseteq> (S \<inter> f -` path_component_set U y)"
  1.2804 +    proof (intro conjI exI)
  1.2805 +      show "openin (subtopology euclidean S) (path_component_set (S \<inter> f -` U) x)"
  1.2806 +      proof (rule ccontr)
  1.2807 +        assume **: "\<not> openin (subtopology euclidean S) (path_component_set (S \<inter> f -` U) x)"
  1.2808 +        then have "x \<notin> (S \<inter> f -` U)"
  1.2809 +          by (metis (no_types, lifting) \<open>U \<subseteq> f ` S\<close> opefSU lcS oo locally_path_connected_open_path_component)
  1.2810 +        then show False
  1.2811 +          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
  1.2812 +      qed
  1.2813 +    next
  1.2814 +      show "x \<in> path_component_set (S \<inter> f -` U) x"
  1.2815 +        by (simp add: \<open>f x \<in> U\<close> \<open>x \<in> S\<close> path_component_refl)
  1.2816 +    next
  1.2817 +      have contf: "continuous_on S f"
  1.2818 +        by (simp add: continuous_on_open oo openin_imp_subset)
  1.2819 +      then have "continuous_on (path_component_set (S \<inter> f -` U) x) f"
  1.2820 +        apply (rule continuous_on_subset)
  1.2821 +        using path_component_subset apply blast
  1.2822 +        done
  1.2823 +      then have "path_connected (f ` path_component_set (S \<inter> f -` U) x)"
  1.2824 +        by (simp add: path_connected_continuous_image)
  1.2825 +      moreover have "f ` path_component_set (S \<inter> f -` U) x \<subseteq> U"
  1.2826 +        using path_component_mem by fastforce
  1.2827 +      moreover have "f x \<in> f ` path_component_set (S \<inter> f -` U) x"
  1.2828 +        by (force simp: \<open>x \<in> S\<close> \<open>f x \<in> U\<close> path_component_refl_eq)
  1.2829 +      ultimately have "f ` (path_component_set (S \<inter> f -` U) x) \<subseteq> path_component_set U (f x)"
  1.2830 +        by (meson path_component_maximal)
  1.2831 +       also have  "\<dots> \<subseteq> path_component_set U y"
  1.2832 +        by (simp add: Uyfx path_component_maximal path_component_subset path_component_sym)
  1.2833 +      finally have fC: "f ` (path_component_set (S \<inter> f -` U) x) \<subseteq> path_component_set U y" .
  1.2834 +      have cUC: "path_component_set (S \<inter> f -` U) x \<subseteq> (S \<inter> f -` path_component_set U y)"
  1.2835 +        using path_component_subset fC by blast
  1.2836 +      have "path_component_set (S \<inter> f -` U) x \<subseteq> path_component_set (S \<inter> f -` path_component_set U y) x"
  1.2837 +      proof -
  1.2838 +        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"
  1.2839 +          using cUC path_component_mono by blast
  1.2840 +        then show ?thesis
  1.2841 +          using path_component_path_component by blast
  1.2842 +      qed
  1.2843 +      also have "\<dots> \<subseteq> (S \<inter> f -` path_component_set U y)"
  1.2844 +        by (rule path_component_subset)
  1.2845 +      finally show "path_component_set (S \<inter> f -` U) x \<subseteq> (S \<inter> f -` path_component_set U y)" .
  1.2846 +    qed
  1.2847 +  qed
  1.2848 +  ultimately show "openin (subtopology euclidean (f ` S)) (path_component_set U y)"
  1.2849 +    by metis
  1.2850 +qed
  1.2851 +
  1.2852 +subsection%unimportant\<open>Components, continuity, openin, closedin\<close>
  1.2853 +
  1.2854 +lemma continuous_on_components_gen:
  1.2855 + fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
  1.2856 +  assumes "\<And>c. c \<in> components S \<Longrightarrow>
  1.2857 +              openin (subtopology euclidean S) c \<and> continuous_on c f"
  1.2858 +    shows "continuous_on S f"
  1.2859 +proof (clarsimp simp: continuous_openin_preimage_eq)
  1.2860 +  fix t :: "'b set"
  1.2861 +  assume "open t"
  1.2862 +  have *: "S \<inter> f -` t = (\<Union>c \<in> components S. c \<inter> f -` t)"
  1.2863 +    by auto
  1.2864 +  show "openin (subtopology euclidean S) (S \<inter> f -` t)"
  1.2865 +    unfolding * using \<open>open t\<close> assms continuous_openin_preimage_gen openin_trans openin_Union by blast
  1.2866 +qed
  1.2867 +
  1.2868 +lemma continuous_on_components:
  1.2869 + fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
  1.2870 +  assumes "locally connected S "
  1.2871 +          "\<And>c. c \<in> components S \<Longrightarrow> continuous_on c f"
  1.2872 +    shows "continuous_on S f"
  1.2873 +apply (rule continuous_on_components_gen)
  1.2874 +apply (auto simp: assms intro: openin_components_locally_connected)
  1.2875 +done
  1.2876 +
  1.2877 +lemma continuous_on_components_eq:
  1.2878 +    "locally connected S
  1.2879 +     \<Longrightarrow> (continuous_on S f \<longleftrightarrow> (\<forall>c \<in> components S. continuous_on c f))"
  1.2880 +by (meson continuous_on_components continuous_on_subset in_components_subset)
  1.2881 +
  1.2882 +lemma continuous_on_components_open:
  1.2883 + fixes S :: "'a::real_normed_vector set"
  1.2884 +  assumes "open S "
  1.2885 +          "\<And>c. c \<in> components S \<Longrightarrow> continuous_on c f"
  1.2886 +    shows "continuous_on S f"
  1.2887 +using continuous_on_components open_imp_locally_connected assms by blast
  1.2888 +
  1.2889 +lemma continuous_on_components_open_eq:
  1.2890 +  fixes S :: "'a::real_normed_vector set"
  1.2891 +  shows "open S \<Longrightarrow> (continuous_on S f \<longleftrightarrow> (\<forall>c \<in> components S. continuous_on c f))"
  1.2892 +using continuous_on_subset in_components_subset
  1.2893 +by (blast intro: continuous_on_components_open)
  1.2894 +
  1.2895 +lemma closedin_union_complement_components:
  1.2896 +  assumes u: "locally connected u"
  1.2897 +      and S: "closedin (subtopology euclidean u) S"
  1.2898 +      and cuS: "c \<subseteq> components(u - S)"
  1.2899 +    shows "closedin (subtopology euclidean u) (S \<union> \<Union>c)"
  1.2900 +proof -
  1.2901 +  have di: "(\<And>S t. S \<in> c \<and> t \<in> c' \<Longrightarrow> disjnt S t) \<Longrightarrow> disjnt (\<Union> c) (\<Union> c')" for c'
  1.2902 +    by (simp add: disjnt_def) blast
  1.2903 +  have "S \<subseteq> u"
  1.2904 +    using S closedin_imp_subset by blast
  1.2905 +  moreover have "u - S = \<Union>c \<union> \<Union>(components (u - S) - c)"
  1.2906 +    by (metis Diff_partition Union_components Union_Un_distrib assms(3))
  1.2907 +  moreover have "disjnt (\<Union>c) (\<Union>(components (u - S) - c))"
  1.2908 +    apply (rule di)
  1.2909 +    by (metis DiffD1 DiffD2 assms(3) components_nonoverlap disjnt_def subsetCE)
  1.2910 +  ultimately have eq: "S \<union> \<Union>c = u - (\<Union>(components(u - S) - c))"
  1.2911 +    by (auto simp: disjnt_def)
  1.2912 +  have *: "openin (subtopology euclidean u) (\<Union>(components (u - S) - c))"
  1.2913 +    apply (rule openin_Union)
  1.2914 +    apply (rule openin_trans [of "u - S"])
  1.2915 +    apply (simp add: u S locally_diff_closed openin_components_locally_connected)
  1.2916 +    apply (simp add: openin_diff S)
  1.2917 +    done
  1.2918 +  have "openin (subtopology euclidean u) (u - (u - \<Union>(components (u - S) - c)))"
  1.2919 +    apply (rule openin_diff, simp)
  1.2920 +    apply (metis closedin_diff closedin_topspace topspace_euclidean_subtopology *)
  1.2921 +    done
  1.2922 +  then show ?thesis
  1.2923 +    by (force simp: eq closedin_def)
  1.2924 +qed
  1.2925 +
  1.2926 +lemma closed_union_complement_components:
  1.2927 +  fixes S :: "'a::real_normed_vector set"
  1.2928 +  assumes S: "closed S" and c: "c \<subseteq> components(- S)"
  1.2929 +    shows "closed(S \<union> \<Union> c)"
  1.2930 +proof -
  1.2931 +  have "closedin (subtopology euclidean UNIV) (S \<union> \<Union>c)"
  1.2932 +    apply (rule closedin_union_complement_components [OF locally_connected_UNIV])
  1.2933 +    using S c apply (simp_all add: Compl_eq_Diff_UNIV)
  1.2934 +    done
  1.2935 +  then show ?thesis by simp
  1.2936 +qed
  1.2937 +
  1.2938 +lemma closedin_Un_complement_component:
  1.2939 +  fixes S :: "'a::real_normed_vector set"
  1.2940 +  assumes u: "locally connected u"
  1.2941 +      and S: "closedin (subtopology euclidean u) S"
  1.2942 +      and c: " c \<in> components(u - S)"
  1.2943 +    shows "closedin (subtopology euclidean u) (S \<union> c)"
  1.2944 +proof -
  1.2945 +  have "closedin (subtopology euclidean u) (S \<union> \<Union>{c})"
  1.2946 +    using c by (blast intro: closedin_union_complement_components [OF u S])
  1.2947 +  then show ?thesis
  1.2948 +    by simp
  1.2949 +qed
  1.2950 +
  1.2951 +lemma closed_Un_complement_component:
  1.2952 +  fixes S :: "'a::real_normed_vector set"
  1.2953 +  assumes S: "closed S" and c: " c \<in> components(-S)"
  1.2954 +    shows "closed (S \<union> c)"
  1.2955 +  by (metis Compl_eq_Diff_UNIV S c closed_closedin closedin_Un_complement_component
  1.2956 +      locally_connected_UNIV subtopology_UNIV)
  1.2957 +
  1.2958 +
  1.2959 +subsection\<open>Existence of isometry between subspaces of same dimension\<close>
  1.2960 +
  1.2961 +lemma isometry_subset_subspace:
  1.2962 +  fixes S :: "'a::euclidean_space set"
  1.2963 +    and T :: "'b::euclidean_space set"
  1.2964 +  assumes S: "subspace S"
  1.2965 +      and T: "subspace T"
  1.2966 +      and d: "dim S \<le> dim T"
  1.2967 +  obtains f where "linear f" "f ` S \<subseteq> T" "\<And>x. x \<in> S \<Longrightarrow> norm(f x) = norm x"
  1.2968 +proof -
  1.2969 +  obtain B where "B \<subseteq> S" and Borth: "pairwise orthogonal B"
  1.2970 +             and B1: "\<And>x. x \<in> B \<Longrightarrow> norm x = 1"
  1.2971 +             and "independent B" "finite B" "card B = dim S" "span B = S"
  1.2972 +    by (metis orthonormal_basis_subspace [OF S] independent_finite)
  1.2973 +  obtain C where "C \<subseteq> T" and Corth: "pairwise orthogonal C"
  1.2974 +             and C1:"\<And>x. x \<in> C \<Longrightarrow> norm x = 1"
  1.2975 +             and "independent C" "finite C" "card C = dim T" "span C = T"
  1.2976 +    by (metis orthonormal_basis_subspace [OF T] independent_finite)
  1.2977 +  obtain fb where "fb ` B \<subseteq> C" "inj_on fb B"
  1.2978 +    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)
  1.2979 +  then have pairwise_orth_fb: "pairwise (\<lambda>v j. orthogonal (fb v) (fb j)) B"
  1.2980 +    using Corth
  1.2981 +    apply (auto simp: pairwise_def orthogonal_clauses)
  1.2982 +    by (meson subsetD image_eqI inj_on_def)
  1.2983 +  obtain f where "linear f" and ffb: "\<And>x. x \<in> B \<Longrightarrow> f x = fb x"
  1.2984 +    using linear_independent_extend \<open>independent B\<close> by fastforce
  1.2985 +  have "span (f ` B) \<subseteq> span C"
  1.2986 +    by (metis \<open>fb ` B \<subseteq> C\<close> ffb image_cong span_mono)
  1.2987 +  then have "f ` S \<subseteq> T"
  1.2988 +    unfolding \<open>span B = S\<close> \<open>span C = T\<close> span_linear_image[OF \<open>linear f\<close>] .
  1.2989 +  have [simp]: "\<And>x. x \<in> B \<Longrightarrow> norm (fb x) = norm x"
  1.2990 +    using B1 C1 \<open>fb ` B \<subseteq> C\<close> by auto
  1.2991 +  have "norm (f x) = norm x" if "x \<in> S" for x
  1.2992 +  proof -
  1.2993 +    interpret linear f by fact
  1.2994 +    obtain a where x: "x = (\<Sum>v \<in> B. a v *\<^sub>R v)"
  1.2995 +      using \<open>finite B\<close> \<open>span B = S\<close> \<open>x \<in> S\<close> span_finite by fastforce
  1.2996 +    have "norm (f x)^2 = norm (\<Sum>v\<in>B. a v *\<^sub>R fb v)^2" by (simp add: sum scale ffb x)
  1.2997 +    also have "\<dots> = (\<Sum>v\<in>B. norm ((a v *\<^sub>R fb v))^2)"
  1.2998 +      apply (rule norm_sum_Pythagorean [OF \<open>finite B\<close>])
  1.2999 +      apply (rule pairwise_ortho_scaleR [OF pairwise_orth_fb])
  1.3000 +      done
  1.3001 +    also have "\<dots> = norm x ^2"
  1.3002 +      by (simp add: x pairwise_ortho_scaleR Borth norm_sum_Pythagorean [OF \<open>finite B\<close>])
  1.3003 +    finally show ?thesis
  1.3004 +      by (simp add: norm_eq_sqrt_inner)
  1.3005 +  qed
  1.3006 +  then show ?thesis
  1.3007 +    by (rule that [OF \<open>linear f\<close> \<open>f ` S \<subseteq> T\<close>])
  1.3008 +qed
  1.3009 +
  1.3010 +proposition isometries_subspaces:
  1.3011 +  fixes S :: "'a::euclidean_space set"
  1.3012 +    and T :: "'b::euclidean_space set"
  1.3013 +  assumes S: "subspace S"
  1.3014 +      and T: "subspace T"
  1.3015 +      and d: "dim S = dim T"
  1.3016 +  obtains f g where "linear f" "linear g" "f ` S = T" "g ` T = S"
  1.3017 +                    "\<And>x. x \<in> S \<Longrightarrow> norm(f x) = norm x"
  1.3018 +                    "\<And>x. x \<in> T \<Longrightarrow> norm(g x) = norm x"
  1.3019 +                    "\<And>x. x \<in> S \<Longrightarrow> g(f x) = x"
  1.3020 +                    "\<And>x. x \<in> T \<Longrightarrow> f(g x) = x"
  1.3021 +proof -
  1.3022 +  obtain B where "B \<subseteq> S" and Borth: "pairwise orthogonal B"
  1.3023 +             and B1: "\<And>x. x \<in> B \<Longrightarrow> norm x = 1"
  1.3024 +             and "independent B" "finite B" "card B = dim S" "span B = S"
  1.3025 +    by (metis orthonormal_basis_subspace [OF S] independent_finite)
  1.3026 +  obtain C where "C \<subseteq> T" and Corth: "pairwise orthogonal C"
  1.3027 +             and C1:"\<And>x. x \<in> C \<Longrightarrow> norm x = 1"
  1.3028 +             and "independent C" "finite C" "card C = dim T" "span C = T"
  1.3029 +    by (metis orthonormal_basis_subspace [OF T] independent_finite)
  1.3030 +  obtain fb where "bij_betw fb B C"
  1.3031 +    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)
  1.3032 +  then have pairwise_orth_fb: "pairwise (\<lambda>v j. orthogonal (fb v) (fb j)) B"
  1.3033 +    using Corth
  1.3034 +    apply (auto simp: pairwise_def orthogonal_clauses bij_betw_def)
  1.3035 +    by (meson subsetD image_eqI inj_on_def)
  1.3036 +  obtain f where "linear f" and ffb: "\<And>x. x \<in> B \<Longrightarrow> f x = fb x"
  1.3037 +    using linear_independent_extend \<open>independent B\<close> by fastforce
  1.3038 +  interpret f: linear f by fact
  1.3039 +  define gb where "gb \<equiv> inv_into B fb"
  1.3040 +  then have pairwise_orth_gb: "pairwise (\<lambda>v j. orthogonal (gb v) (gb j)) C"
  1.3041 +    using Borth
  1.3042 +    apply (auto simp: pairwise_def orthogonal_clauses bij_betw_def)
  1.3043 +    by (metis \<open>bij_betw fb B C\<close> bij_betw_imp_surj_on bij_betw_inv_into_right inv_into_into)
  1.3044 +  obtain g where "linear g" and ggb: "\<And>x. x \<in> C \<Longrightarrow> g x = gb x"
  1.3045 +    using linear_independent_extend \<open>independent C\<close> by fastforce
  1.3046 +  interpret g: linear g by fact
  1.3047 +  have "span (f ` B) \<subseteq> span C"
  1.3048 +    by (metis \<open>bij_betw fb B C\<close> bij_betw_imp_surj_on eq_iff ffb image_cong)
  1.3049 +  then have "f ` S \<subseteq> T"
  1.3050 +    unfolding \<open>span B = S\<close> \<open>span C = T\<close>
  1.3051 +      span_linear_image[OF \<open>linear f\<close>] .
  1.3052 +  have [simp]: "\<And>x. x \<in> B \<Longrightarrow> norm (fb x) = norm x"
  1.3053 +    using B1 C1 \<open>bij_betw fb B C\<close> bij_betw_imp_surj_on by fastforce
  1.3054 +  have f [simp]: "norm (f x) = norm x" "g (f x) = x" if "x \<in> S" for x
  1.3055 +  proof -
  1.3056 +    obtain a where x: "x = (\<Sum>v \<in> B. a v *\<^sub>R v)"
  1.3057 +      using \<open>finite B\<close> \<open>span B = S\<close> \<open>x \<in> S\<close> span_finite by fastforce
  1.3058 +    have "f x = (\<Sum>v \<in> B. f (a v *\<^sub>R v))"
  1.3059 +      using linear_sum [OF \<open>linear f\<close>] x by auto
  1.3060 +    also have "\<dots> = (\<Sum>v \<in> B. a v *\<^sub>R f v)"
  1.3061 +      by (simp add: f.sum f.scale)
  1.3062 +    also have "\<dots> = (\<Sum>v \<in> B. a v *\<^sub>R fb v)"
  1.3063 +      by (simp add: ffb cong: sum.cong)
  1.3064 +    finally have *: "f x = (\<Sum>v\<in>B. a v *\<^sub>R fb v)" .
  1.3065 +    then have "(norm (f x))\<^sup>2 = (norm (\<Sum>v\<in>B. a v *\<^sub>R fb v))\<^sup>2" by simp
  1.3066 +    also have "\<dots> = (\<Sum>v\<in>B. norm ((a v *\<^sub>R fb v))^2)"
  1.3067 +      apply (rule norm_sum_Pythagorean [OF \<open>finite B\<close>])
  1.3068 +      apply (rule pairwise_ortho_scaleR [OF pairwise_orth_fb])
  1.3069 +      done
  1.3070 +    also have "\<dots> = (norm x)\<^sup>2"
  1.3071 +      by (simp add: x pairwise_ortho_scaleR Borth norm_sum_Pythagorean [OF \<open>finite B\<close>])
  1.3072 +    finally show "norm (f x) = norm x"
  1.3073 +      by (simp add: norm_eq_sqrt_inner)
  1.3074 +    have "g (f x) = g (\<Sum>v\<in>B. a v *\<^sub>R fb v)" by (simp add: *)
  1.3075 +    also have "\<dots> = (\<Sum>v\<in>B. g (a v *\<^sub>R fb v))"
  1.3076 +      by (simp add: g.sum g.scale)
  1.3077 +    also have "\<dots> = (\<Sum>v\<in>B. a v *\<^sub>R g (fb v))"
  1.3078 +      by (simp add: g.scale)
  1.3079 +    also have "\<dots> = (\<Sum>v\<in>B. a v *\<^sub>R v)"
  1.3080 +      apply (rule sum.cong [OF refl])
  1.3081 +      using \<open>bij_betw fb B C\<close> gb_def bij_betwE bij_betw_inv_into_left gb_def ggb by fastforce
  1.3082 +    also have "\<dots> = x"
  1.3083 +      using x by blast
  1.3084 +    finally show "g (f x) = x" .
  1.3085 +  qed
  1.3086 +  have [simp]: "\<And>x. x \<in> C \<Longrightarrow> norm (gb x) = norm x"
  1.3087 +    by (metis B1 C1 \<open>bij_betw fb B C\<close> bij_betw_imp_surj_on gb_def inv_into_into)
  1.3088 +  have g [simp]: "f (g x) = x" if "x \<in> T" for x
  1.3089 +  proof -
  1.3090 +    obtain a where x: "x = (\<Sum>v \<in> C. a v *\<^sub>R v)"
  1.3091 +      using \<open>finite C\<close> \<open>span C = T\<close> \<open>x \<in> T\<close> span_finite by fastforce
  1.3092 +    have "g x = (\<Sum>v \<in> C. g (a v *\<^sub>R v))"
  1.3093 +      by (simp add: x g.sum)
  1.3094 +    also have "\<dots> = (\<Sum>v \<in> C. a v *\<^sub>R g v)"
  1.3095 +      by (simp add: g.scale)
  1.3096 +    also have "\<dots> = (\<Sum>v \<in> C. a v *\<^sub>R gb v)"
  1.3097 +      by (simp add: ggb cong: sum.cong)
  1.3098 +    finally have "f (g x) = f (\<Sum>v\<in>C. a v *\<^sub>R gb v)" by simp
  1.3099 +    also have "\<dots> = (\<Sum>v\<in>C. f (a v *\<^sub>R gb v))"
  1.3100 +      by (simp add: f.scale f.sum)
  1.3101 +    also have "\<dots> = (\<Sum>v\<in>C. a v *\<^sub>R f (gb v))"
  1.3102 +      by (simp add: f.scale f.sum)
  1.3103 +    also have "\<dots> = (\<Sum>v\<in>C. a v *\<^sub>R v)"
  1.3104 +      using \<open>bij_betw fb B C\<close>
  1.3105 +      by (simp add: bij_betw_def gb_def bij_betw_inv_into_right ffb inv_into_into)
  1.3106 +    also have "\<dots> = x"
  1.3107 +      using x by blast
  1.3108 +    finally show "f (g x) = x" .
  1.3109 +  qed
  1.3110 +  have gim: "g ` T = S"
  1.3111 +    by (metis (full_types) S T \<open>f ` S \<subseteq> T\<close> d dim_eq_span dim_image_le f(2) g.linear_axioms
  1.3112 +        image_iff linear_subspace_image span_eq_iff subset_iff)
  1.3113 +  have fim: "f ` S = T"
  1.3114 +    using \<open>g ` T = S\<close> image_iff by fastforce
  1.3115 +  have [simp]: "norm (g x) = norm x" if "x \<in> T" for x
  1.3116 +    using fim that by auto
  1.3117 +  show ?thesis
  1.3118 +    apply (rule that [OF \<open>linear f\<close> \<open>linear g\<close>])
  1.3119 +    apply (simp_all add: fim gim)
  1.3120 +    done
  1.3121 +qed
  1.3122 +
  1.3123 +corollary isometry_subspaces:
  1.3124 +  fixes S :: "'a::euclidean_space set"
  1.3125 +    and T :: "'b::euclidean_space set"
  1.3126 +  assumes S: "subspace S"
  1.3127 +      and T: "subspace T"
  1.3128 +      and d: "dim S = dim T"
  1.3129 +  obtains f where "linear f" "f ` S = T" "\<And>x. x \<in> S \<Longrightarrow> norm(f x) = norm x"
  1.3130 +using isometries_subspaces [OF assms]
  1.3131 +by metis
  1.3132 +
  1.3133 +corollary isomorphisms_UNIV_UNIV:
  1.3134 +  assumes "DIM('M) = DIM('N)"
  1.3135 +  obtains f::"'M::euclidean_space \<Rightarrow>'N::euclidean_space" and g
  1.3136 +  where "linear f" "linear g"
  1.3137 +                    "\<And>x. norm(f x) = norm x" "\<And>y. norm(g y) = norm y"
  1.3138 +                    "\<And>x. g (f x) = x" "\<And>y. f(g y) = y"
  1.3139 +  using assms by (auto intro: isometries_subspaces [of "UNIV::'M set" "UNIV::'N set"])
  1.3140 +
  1.3141 +lemma homeomorphic_subspaces:
  1.3142 +  fixes S :: "'a::euclidean_space set"
  1.3143 +    and T :: "'b::euclidean_space set"
  1.3144 +  assumes S: "subspace S"
  1.3145 +      and T: "subspace T"
  1.3146 +      and d: "dim S = dim T"
  1.3147 +    shows "S homeomorphic T"
  1.3148 +proof -
  1.3149 +  obtain f g where "linear f" "linear g" "f ` S = T" "g ` T = S"
  1.3150 +                   "\<And>x. x \<in> S \<Longrightarrow> g(f x) = x" "\<And>x. x \<in> T \<Longrightarrow> f(g x) = x"
  1.3151 +    by (blast intro: isometries_subspaces [OF assms])
  1.3152 +  then show ?thesis
  1.3153 +    apply (simp add: homeomorphic_def homeomorphism_def)
  1.3154 +    apply (rule_tac x=f in exI)
  1.3155 +    apply (rule_tac x=g in exI)
  1.3156 +    apply (auto simp: linear_continuous_on linear_conv_bounded_linear)
  1.3157 +    done
  1.3158 +qed
  1.3159 +
  1.3160 +lemma homeomorphic_affine_sets:
  1.3161 +  assumes "affine S" "affine T" "aff_dim S = aff_dim T"
  1.3162 +    shows "S homeomorphic T"
  1.3163 +proof (cases "S = {} \<or> T = {}")
  1.3164 +  case True  with assms aff_dim_empty homeomorphic_empty show ?thesis
  1.3165 +    by metis
  1.3166 +next
  1.3167 +  case False
  1.3168 +  then obtain a b where ab: "a \<in> S" "b \<in> T" by auto
  1.3169 +  then have ss: "subspace ((+) (- a) ` S)" "subspace ((+) (- b) ` T)"
  1.3170 +    using affine_diffs_subspace assms by blast+
  1.3171 +  have dd: "dim ((+) (- a) ` S) = dim ((+) (- b) ` T)"
  1.3172 +    using assms ab  by (simp add: aff_dim_eq_dim  [OF hull_inc] image_def)
  1.3173 +  have "S homeomorphic ((+) (- a) ` S)"
  1.3174 +    by (simp add: homeomorphic_translation)
  1.3175 +  also have "\<dots> homeomorphic ((+) (- b) ` T)"
  1.3176 +    by (rule homeomorphic_subspaces [OF ss dd])
  1.3177 +  also have "\<dots> homeomorphic T"
  1.3178 +    using homeomorphic_sym homeomorphic_translation by auto
  1.3179 +  finally show ?thesis .
  1.3180 +qed
  1.3181 +
  1.3182 +
  1.3183 +subsection\<open>Retracts, in a general sense, preserve (co)homotopic triviality)\<close>
  1.3184 +
  1.3185 +locale%important Retracts =
  1.3186 +  fixes s h t k
  1.3187 +  assumes conth: "continuous_on s h"
  1.3188 +      and imh: "h ` s = t"
  1.3189 +      and contk: "continuous_on t k"
  1.3190 +      and imk: "k ` t \<subseteq> s"
  1.3191 +      and idhk: "\<And>y. y \<in> t \<Longrightarrow> h(k y) = y"
  1.3192 +
  1.3193 +begin
  1.3194 +
  1.3195 +lemma homotopically_trivial_retraction_gen:
  1.3196 +  assumes P: "\<And>f. \<lbrakk>continuous_on u f; f ` u \<subseteq> t; Q f\<rbrakk> \<Longrightarrow> P(k \<circ> f)"
  1.3197 +      and Q: "\<And>f. \<lbrakk>continuous_on u f; f ` u \<subseteq> s; P f\<rbrakk> \<Longrightarrow> Q(h \<circ> f)"
  1.3198 +      and Qeq: "\<And>h k. (\<And>x. x \<in> u \<Longrightarrow> h x = k x) \<Longrightarrow> Q h = Q k"
  1.3199 +      and hom: "\<And>f g. \<lbrakk>continuous_on u f; f ` u \<subseteq> s; P f;
  1.3200 +                       continuous_on u g; g ` u \<subseteq> s; P g\<rbrakk>
  1.3201 +                       \<Longrightarrow> homotopic_with P u s f g"
  1.3202 +      and contf: "continuous_on u f" and imf: "f ` u \<subseteq> t" and Qf: "Q f"
  1.3203 +      and contg: "continuous_on u g" and img: "g ` u \<subseteq> t" and Qg: "Q g"
  1.3204 +    shows "homotopic_with Q u t f g"
  1.3205 +proof -
  1.3206 +  have feq: "\<And>x. x \<in> u \<Longrightarrow> (h \<circ> (k \<circ> f)) x = f x" using idhk imf by auto
  1.3207 +  have geq: "\<And>x. x \<in> u \<Longrightarrow> (h \<circ> (k \<circ> g)) x = g x" using idhk img by auto
  1.3208 +  have "continuous_on u (k \<circ> f)"
  1.3209 +    using contf continuous_on_compose continuous_on_subset contk imf by blast
  1.3210 +  moreover have "(k \<circ> f) ` u \<subseteq> s"
  1.3211 +    using imf imk by fastforce
  1.3212 +  moreover have "P (k \<circ> f)"
  1.3213 +    by (simp add: P Qf contf imf)
  1.3214 +  moreover have "continuous_on u (k \<circ> g)"
  1.3215 +    using contg continuous_on_compose continuous_on_subset contk img by blast
  1.3216 +  moreover have "(k \<circ> g) ` u \<subseteq> s"
  1.3217 +    using img imk by fastforce
  1.3218 +  moreover have "P (k \<circ> g)"
  1.3219 +    by (simp add: P Qg contg img)
  1.3220 +  ultimately have "homotopic_with P u s (k \<circ> f) (k \<circ> g)"
  1.3221 +    by (rule hom)
  1.3222 +  then have "homotopic_with Q u t (h \<circ> (k \<circ> f)) (h \<circ> (k \<circ> g))"
  1.3223 +    apply (rule homotopic_with_compose_continuous_left [OF homotopic_with_mono])
  1.3224 +    using Q by (auto simp: conth imh)
  1.3225 +  then show ?thesis
  1.3226 +    apply (rule homotopic_with_eq)
  1.3227 +    apply (metis feq)
  1.3228 +    apply (metis geq)
  1.3229 +    apply (metis Qeq)
  1.3230 +    done
  1.3231 +qed
  1.3232 +
  1.3233 +lemma homotopically_trivial_retraction_null_gen:
  1.3234 +  assumes P: "\<And>f. \<lbrakk>continuous_on u f; f ` u \<subseteq> t; Q f\<rbrakk> \<Longrightarrow> P(k \<circ> f)"
  1.3235 +      and Q: "\<And>f. \<lbrakk>continuous_on u f; f ` u \<subseteq> s; P f\<rbrakk> \<Longrightarrow> Q(h \<circ> f)"
  1.3236 +      and Qeq: "\<And>h k. (\<And>x. x \<in> u \<Longrightarrow> h x = k x) \<Longrightarrow> Q h = Q k"
  1.3237 +      and hom: "\<And>f. \<lbrakk>continuous_on u f; f ` u \<subseteq> s; P f\<rbrakk>
  1.3238 +                     \<Longrightarrow> \<exists>c. homotopic_with P u s f (\<lambda>x. c)"
  1.3239 +      and contf: "continuous_on u f" and imf:"f ` u \<subseteq> t" and Qf: "Q f"
  1.3240 +  obtains c where "homotopic_with Q u t f (\<lambda>x. c)"
  1.3241 +proof -
  1.3242 +  have feq: "\<And>x. x \<in> u \<Longrightarrow> (h \<circ> (k \<circ> f)) x = f x" using idhk imf by auto
  1.3243 +  have "continuous_on u (k \<circ> f)"
  1.3244 +    using contf continuous_on_compose continuous_on_subset contk imf by blast
  1.3245 +  moreover have "(k \<circ> f) ` u \<subseteq> s"
  1.3246 +    using imf imk by fastforce
  1.3247 +  moreover have "P (k \<circ> f)"
  1.3248 +    by (simp add: P Qf contf imf)
  1.3249 +  ultimately obtain c where "homotopic_with P u s (k \<circ> f) (\<lambda>x. c)"
  1.3250 +    by (metis hom)
  1.3251 +  then have "homotopic_with Q u t (h \<circ> (k \<circ> f)) (h \<circ> (\<lambda>x. c))"
  1.3252 +    apply (rule homotopic_with_compose_continuous_left [OF homotopic_with_mono])
  1.3253 +    using Q by (auto simp: conth imh)
  1.3254 +  then show ?thesis
  1.3255 +    apply (rule_tac c = "h c" in that)
  1.3256 +    apply (erule homotopic_with_eq)
  1.3257 +    apply (metis feq, simp)
  1.3258 +    apply (metis Qeq)
  1.3259 +    done
  1.3260 +qed
  1.3261 +
  1.3262 +lemma cohomotopically_trivial_retraction_gen:
  1.3263 +  assumes P: "\<And>f. \<lbrakk>continuous_on t f; f ` t \<subseteq> u; Q f\<rbrakk> \<Longrightarrow> P(f \<circ> h)"
  1.3264 +      and Q: "\<And>f. \<lbrakk>continuous_on s f; f ` s \<subseteq> u; P f\<rbrakk> \<Longrightarrow> Q(f \<circ> k)"
  1.3265 +      and Qeq: "\<And>h k. (\<And>x. x \<in> t \<Longrightarrow> h x = k x) \<Longrightarrow> Q h = Q k"
  1.3266 +      and hom: "\<And>f g. \<lbrakk>continuous_on s f; f ` s \<subseteq> u; P f;
  1.3267 +                       continuous_on s g; g ` s \<subseteq> u; P g\<rbrakk>
  1.3268 +                       \<Longrightarrow> homotopic_with P s u f g"
  1.3269 +      and contf: "continuous_on t f" and imf: "f ` t \<subseteq> u" and Qf: "Q f"
  1.3270 +      and contg: "continuous_on t g" and img: "g ` t \<subseteq> u" and Qg: "Q g"
  1.3271 +    shows "homotopic_with Q t u f g"
  1.3272 +proof -
  1.3273 +  have feq: "\<And>x. x \<in> t \<Longrightarrow> (f \<circ> h \<circ> k) x = f x" using idhk imf by auto
  1.3274 +  have geq: "\<And>x. x \<in> t \<Longrightarrow> (g \<circ> h \<circ> k) x = g x" using idhk img by auto
  1.3275 +  have "continuous_on s (f \<circ> h)"
  1.3276 +    using contf conth continuous_on_compose imh by blast
  1.3277 +  moreover have "(f \<circ> h) ` s \<subseteq> u"
  1.3278 +    using imf imh by fastforce
  1.3279 +  moreover have "P (f \<circ> h)"
  1.3280 +    by (simp add: P Qf contf imf)
  1.3281 +  moreover have "continuous_on s (g \<circ> h)"
  1.3282 +    using contg continuous_on_compose continuous_on_subset conth imh by blast
  1.3283 +  moreover have "(g \<circ> h) ` s \<subseteq> u"
  1.3284 +    using img imh by fastforce
  1.3285 +  moreover have "P (g \<circ> h)"
  1.3286 +    by (simp add: P Qg contg img)
  1.3287 +  ultimately have "homotopic_with P s u (f \<circ> h) (g \<circ> h)"
  1.3288 +    by (rule hom)
  1.3289 +  then have "homotopic_with Q t u (f \<circ> h \<circ> k) (g \<circ> h \<circ> k)"
  1.3290 +    apply (rule homotopic_with_compose_continuous_right [OF homotopic_with_mono])
  1.3291 +    using Q by (auto simp: contk imk)
  1.3292 +  then show ?thesis
  1.3293 +    apply (rule homotopic_with_eq)
  1.3294 +    apply (metis feq)
  1.3295 +    apply (metis geq)
  1.3296 +    apply (metis Qeq)
  1.3297 +    done
  1.3298 +qed
  1.3299 +
  1.3300 +lemma cohomotopically_trivial_retraction_null_gen:
  1.3301 +  assumes P: "\<And>f. \<lbrakk>continuous_on t f; f ` t \<subseteq> u; Q f\<rbrakk> \<Longrightarrow> P(f \<circ> h)"
  1.3302 +      and Q: "\<And>f. \<lbrakk>continuous_on s f; f ` s \<subseteq> u; P f\<rbrakk> \<Longrightarrow> Q(f \<circ> k)"
  1.3303 +      and Qeq: "\<And>h k. (\<And>x. x \<in> t \<Longrightarrow> h x = k x) \<Longrightarrow> Q h = Q k"
  1.3304 +      and hom: "\<And>f g. \<lbrakk>continuous_on s f; f ` s \<subseteq> u; P f\<rbrakk>
  1.3305 +                       \<Longrightarrow> \<exists>c. homotopic_with P s u f (\<lambda>x. c)"
  1.3306 +      and contf: "continuous_on t f" and imf: "f ` t \<subseteq> u" and Qf: "Q f"
  1.3307 +  obtains c where "homotopic_with Q t u f (\<lambda>x. c)"
  1.3308 +proof -
  1.3309 +  have feq: "\<And>x. x \<in> t \<Longrightarrow> (f \<circ> h \<circ> k) x = f x" using idhk imf by auto
  1.3310 +  have "continuous_on s (f \<circ> h)"
  1.3311 +    using contf conth continuous_on_compose imh by blast
  1.3312 +  moreover have "(f \<circ> h) ` s \<subseteq> u"
  1.3313 +    using imf imh by fastforce
  1.3314 +  moreover have "P (f \<circ> h)"
  1.3315 +    by (simp add: P Qf contf imf)
  1.3316 +  ultimately obtain c where "homotopic_with P s u (f \<circ> h) (\<lambda>x. c)"
  1.3317 +    by (metis hom)
  1.3318 +  then have "homotopic_with Q t u (f \<circ> h \<circ> k) ((\<lambda>x. c) \<circ> k)"
  1.3319 +    apply (rule homotopic_with_compose_continuous_right [OF homotopic_with_mono])
  1.3320 +    using Q by (auto simp: contk imk)
  1.3321 +  then show ?thesis
  1.3322 +    apply (rule_tac c = c in that)
  1.3323 +    apply (erule homotopic_with_eq)
  1.3324 +    apply (metis feq, simp)
  1.3325 +    apply (metis Qeq)
  1.3326 +    done
  1.3327 +qed
  1.3328 +
  1.3329 +end
  1.3330 +
  1.3331 +lemma simply_connected_retraction_gen:
  1.3332 +  shows "\<lbrakk>simply_connected S; continuous_on S h; h ` S = T;
  1.3333 +          continuous_on T k; k ` T \<subseteq> S; \<And>y. y \<in> T \<Longrightarrow> h(k y) = y\<rbrakk>
  1.3334 +        \<Longrightarrow> simply_connected T"
  1.3335 +apply (simp add: simply_connected_def path_def path_image_def homotopic_loops_def, clarify)
  1.3336 +apply (rule Retracts.homotopically_trivial_retraction_gen
  1.3337 +        [of S h _ k _ "\<lambda>p. pathfinish p = pathstart p"  "\<lambda>p. pathfinish p = pathstart p"])
  1.3338 +apply (simp_all add: Retracts_def pathfinish_def pathstart_def)
  1.3339 +done
  1.3340 +
  1.3341 +lemma homeomorphic_simply_connected:
  1.3342 +    "\<lbrakk>S homeomorphic T; simply_connected S\<rbrakk> \<Longrightarrow> simply_connected T"
  1.3343 +  by (auto simp: homeomorphic_def homeomorphism_def intro: simply_connected_retraction_gen)
  1.3344 +
  1.3345 +lemma homeomorphic_simply_connected_eq:
  1.3346 +    "S homeomorphic T \<Longrightarrow> (simply_connected S \<longleftrightarrow> simply_connected T)"
  1.3347 +  by (metis homeomorphic_simply_connected homeomorphic_sym)
  1.3348 +
  1.3349 +
  1.3350 +subsection\<open>Homotopy equivalence\<close>
  1.3351 +
  1.3352 +definition%important homotopy_eqv :: "'a::topological_space set \<Rightarrow> 'b::topological_space set \<Rightarrow> bool"
  1.3353 +             (infix "homotopy'_eqv" 50)
  1.3354 +  where "S homotopy_eqv T \<equiv>
  1.3355 +        \<exists>f g. continuous_on S f \<and> f ` S \<subseteq> T \<and>
  1.3356 +              continuous_on T g \<and> g ` T \<subseteq> S \<and>
  1.3357 +              homotopic_with (\<lambda>x. True) S S (g \<circ> f) id \<and>
  1.3358 +              homotopic_with (\<lambda>x. True) T T (f \<circ> g) id"
  1.3359 +
  1.3360 +lemma homeomorphic_imp_homotopy_eqv: "S homeomorphic T \<Longrightarrow> S homotopy_eqv T"
  1.3361 +  unfolding homeomorphic_def homotopy_eqv_def homeomorphism_def
  1.3362 +  by (fastforce intro!: homotopic_with_equal continuous_on_compose)
  1.3363 +
  1.3364 +lemma homotopy_eqv_refl: "S homotopy_eqv S"
  1.3365 +  by (rule homeomorphic_imp_homotopy_eqv homeomorphic_refl)+
  1.3366 +
  1.3367 +lemma homotopy_eqv_sym: "S homotopy_eqv T \<longleftrightarrow> T homotopy_eqv S"
  1.3368 +  by (auto simp: homotopy_eqv_def)
  1.3369 +
  1.3370 +lemma homotopy_eqv_trans [trans]:
  1.3371 +    fixes S :: "'a::real_normed_vector set" and U :: "'c::real_normed_vector set"
  1.3372 +  assumes ST: "S homotopy_eqv T" and TU: "T homotopy_eqv U"
  1.3373 +    shows "S homotopy_eqv U"
  1.3374 +proof -
  1.3375 +  obtain f1 g1 where f1: "continuous_on S f1" "f1 ` S \<subseteq> T"
  1.3376 +                 and g1: "continuous_on T g1" "g1 ` T \<subseteq> S"
  1.3377 +                 and hom1: "homotopic_with (\<lambda>x. True) S S (g1 \<circ> f1) id"
  1.3378 +                           "homotopic_with (\<lambda>x. True) T T (f1 \<circ> g1) id"
  1.3379 +    using ST by (auto simp: homotopy_eqv_def)
  1.3380 +  obtain f2 g2 where f2: "continuous_on T f2" "f2 ` T \<subseteq> U"
  1.3381 +                 and g2: "continuous_on U g2" "g2 ` U \<subseteq> T"
  1.3382 +                 and hom2: "homotopic_with (\<lambda>x. True) T T (g2 \<circ> f2) id"
  1.3383 +                           "homotopic_with (\<lambda>x. True) U U (f2 \<circ> g2) id"
  1.3384 +    using TU by (auto simp: homotopy_eqv_def)
  1.3385 +  have "homotopic_with (\<lambda>f. True) S T (g2 \<circ> f2 \<circ> f1) (id \<circ> f1)"
  1.3386 +    by (rule homotopic_with_compose_continuous_right hom2 f1)+
  1.3387 +  then have "homotopic_with (\<lambda>f. True) S T (g2 \<circ> (f2 \<circ> f1)) (id \<circ> f1)"
  1.3388 +    by (simp add: o_assoc)
  1.3389 +  then have "homotopic_with (\<lambda>x. True) S S
  1.3390 +         (g1 \<circ> (g2 \<circ> (f2 \<circ> f1))) (g1 \<circ> (id \<circ> f1))"
  1.3391 +    by (simp add: g1 homotopic_with_compose_continuous_left)
  1.3392 +  moreover have "homotopic_with (\<lambda>x. True) S S (g1 \<circ> id \<circ> f1) id"
  1.3393 +    using hom1 by simp
  1.3394 +  ultimately have SS: "homotopic_with (\<lambda>x. True) S S (g1 \<circ> g2 \<circ> (f2 \<circ> f1)) id"
  1.3395 +    apply (simp add: o_assoc)
  1.3396 +    apply (blast intro: homotopic_with_trans)
  1.3397 +    done
  1.3398 +  have "homotopic_with (\<lambda>f. True) U T (f1 \<circ> g1 \<circ> g2) (id \<circ> g2)"
  1.3399 +    by (rule homotopic_with_compose_continuous_right hom1 g2)+
  1.3400 +  then have "homotopic_with (\<lambda>f. True) U T (f1 \<circ> (g1 \<circ> g2)) (id \<circ> g2)"
  1.3401 +    by (simp add: o_assoc)
  1.3402 +  then have "homotopic_with (\<lambda>x. True) U U
  1.3403 +         (f2 \<circ> (f1 \<circ> (g1 \<circ> g2))) (f2 \<circ> (id \<circ> g2))"
  1.3404 +    by (simp add: f2 homotopic_with_compose_continuous_left)
  1.3405 +  moreover have "homotopic_with (\<lambda>x. True) U U (f2 \<circ> id \<circ> g2) id"
  1.3406 +    using hom2 by simp
  1.3407 +  ultimately have UU: "homotopic_with (\<lambda>x. True) U U (f2 \<circ> f1 \<circ> (g1 \<circ> g2)) id"
  1.3408 +    apply (simp add: o_assoc)
  1.3409 +    apply (blast intro: homotopic_with_trans)
  1.3410 +    done
  1.3411 +  show ?thesis
  1.3412 +    unfolding homotopy_eqv_def
  1.3413 +    apply (rule_tac x = "f2 \<circ> f1" in exI)
  1.3414 +    apply (rule_tac x = "g1 \<circ> g2" in exI)
  1.3415 +    apply (intro conjI continuous_on_compose SS UU)
  1.3416 +    using f1 f2 g1 g2  apply (force simp: elim!: continuous_on_subset)+
  1.3417 +    done
  1.3418 +qed
  1.3419 +
  1.3420 +lemma homotopy_eqv_inj_linear_image:
  1.3421 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1.3422 +  assumes "linear f" "inj f"
  1.3423 +    shows "(f ` S) homotopy_eqv S"
  1.3424 +apply (rule homeomorphic_imp_homotopy_eqv)
  1.3425 +using assms homeomorphic_sym linear_homeomorphic_image by auto
  1.3426 +
  1.3427 +lemma homotopy_eqv_translation:
  1.3428 +    fixes S :: "'a::real_normed_vector set"
  1.3429 +    shows "(+) a ` S homotopy_eqv S"
  1.3430 +  apply (rule homeomorphic_imp_homotopy_eqv)
  1.3431 +  using homeomorphic_translation homeomorphic_sym by blast
  1.3432 +
  1.3433 +lemma homotopy_eqv_homotopic_triviality_imp:
  1.3434 +  fixes S :: "'a::real_normed_vector set"
  1.3435 +    and T :: "'b::real_normed_vector set"
  1.3436 +    and U :: "'c::real_normed_vector set"
  1.3437 +  assumes "S homotopy_eqv T"
  1.3438 +      and f: "continuous_on U f" "f ` U \<subseteq> T"
  1.3439 +      and g: "continuous_on U g" "g ` U \<subseteq> T"
  1.3440 +      and homUS: "\<And>f g. \<lbrakk>continuous_on U f; f ` U \<subseteq> S;
  1.3441 +                         continuous_on U g; g ` U \<subseteq> S\<rbrakk>
  1.3442 +                         \<Longrightarrow> homotopic_with (\<lambda>x. True) U S f g"
  1.3443 +    shows "homotopic_with (\<lambda>x. True) U T f g"
  1.3444 +proof -
  1.3445 +  obtain h k where h: "continuous_on S h" "h ` S \<subseteq> T"
  1.3446 +               and k: "continuous_on T k" "k ` T \<subseteq> S"
  1.3447 +               and hom: "homotopic_with (\<lambda>x. True) S S (k \<circ> h) id"
  1.3448 +                        "homotopic_with (\<lambda>x. True) T T (h \<circ> k) id"
  1.3449 +    using assms by (auto simp: homotopy_eqv_def)
  1.3450 +  have "homotopic_with (\<lambda>f. True) U S (k \<circ> f) (k \<circ> g)"
  1.3451 +    apply (rule homUS)
  1.3452 +    using f g k
  1.3453 +    apply (safe intro!: continuous_on_compose h k f elim!: continuous_on_subset)
  1.3454 +    apply (force simp: o_def)+
  1.3455 +    done
  1.3456 +  then have "homotopic_with (\<lambda>x. True) U T (h \<circ> (k \<circ> f)) (h \<circ> (k \<circ> g))"
  1.3457 +    apply (rule homotopic_with_compose_continuous_left)
  1.3458 +    apply (simp_all add: h)
  1.3459 +    done
  1.3460 +  moreover have "homotopic_with (\<lambda>x. True) U T (h \<circ> k \<circ> f) (id \<circ> f)"
  1.3461 +    apply (rule homotopic_with_compose_continuous_right [where X=T and Y=T])
  1.3462 +    apply (auto simp: hom f)
  1.3463 +    done
  1.3464 +  moreover have "homotopic_with (\<lambda>x. True) U T (h \<circ> k \<circ> g) (id \<circ> g)"
  1.3465 +    apply (rule homotopic_with_compose_continuous_right [where X=T and Y=T])
  1.3466 +    apply (auto simp: hom g)
  1.3467 +    done
  1.3468 +  ultimately show "homotopic_with (\<lambda>x. True) U T f g"
  1.3469 +    apply (simp add: o_assoc)
  1.3470 +    using homotopic_with_trans homotopic_with_sym by blast
  1.3471 +qed
  1.3472 +
  1.3473 +lemma homotopy_eqv_homotopic_triviality:
  1.3474 +  fixes S :: "'a::real_normed_vector set"
  1.3475 +    and T :: "'b::real_normed_vector set"
  1.3476 +    and U :: "'c::real_normed_vector set"
  1.3477 +  assumes "S homotopy_eqv T"
  1.3478 +    shows "(\<forall>f g. continuous_on U f \<and> f ` U \<subseteq> S \<and>
  1.3479 +                   continuous_on U g \<and> g ` U \<subseteq> S
  1.3480 +                   \<longrightarrow> homotopic_with (\<lambda>x. True) U S f g) \<longleftrightarrow>
  1.3481 +           (\<forall>f g. continuous_on U f \<and> f ` U \<subseteq> T \<and>
  1.3482 +                  continuous_on U g \<and> g ` U \<subseteq> T
  1.3483 +                  \<longrightarrow> homotopic_with (\<lambda>x. True) U T f g)"
  1.3484 +apply (rule iffI)
  1.3485 +apply (metis assms homotopy_eqv_homotopic_triviality_imp)
  1.3486 +by (metis (no_types) assms homotopy_eqv_homotopic_triviality_imp homotopy_eqv_sym)
  1.3487 +
  1.3488 +lemma homotopy_eqv_cohomotopic_triviality_null_imp:
  1.3489 +  fixes S :: "'a::real_normed_vector set"
  1.3490 +    and T :: "'b::real_normed_vector set"
  1.3491 +    and U :: "'c::real_normed_vector set"
  1.3492 +  assumes "S homotopy_eqv T"
  1.3493 +      and f: "continuous_on T f" "f ` T \<subseteq> U"
  1.3494 +      and homSU: "\<And>f. \<lbrakk>continuous_on S f; f ` S \<subseteq> U\<rbrakk>
  1.3495 +                      \<Longrightarrow> \<exists>c. homotopic_with (\<lambda>x. True) S U f (\<lambda>x. c)"
  1.3496 +  obtains c where "homotopic_with (\<lambda>x. True) T U f (\<lambda>x. c)"
  1.3497 +proof -
  1.3498 +  obtain h k where h: "continuous_on S h" "h ` S \<subseteq> T"
  1.3499 +               and k: "continuous_on T k" "k ` T \<subseteq> S"
  1.3500 +               and hom: "homotopic_with (\<lambda>x. True) S S (k \<circ> h) id"
  1.3501 +                        "homotopic_with (\<lambda>x. True) T T (h \<circ> k) id"
  1.3502 +    using assms by (auto simp: homotopy_eqv_def)
  1.3503 +  obtain c where "homotopic_with (\<lambda>x. True) S U (f \<circ> h) (\<lambda>x. c)"
  1.3504 +    apply (rule exE [OF homSU [of "f \<circ> h"]])
  1.3505 +    apply (intro continuous_on_compose h)
  1.3506 +    using h f  apply (force elim!: continuous_on_subset)+
  1.3507 +    done
  1.3508 +  then have "homotopic_with (\<lambda>x. True) T U ((f \<circ> h) \<circ> k) ((\<lambda>x. c) \<circ> k)"
  1.3509 +    apply (rule homotopic_with_compose_continuous_right [where X=S])
  1.3510 +    using k by auto
  1.3511 +  moreover have "homotopic_with (\<lambda>x. True) T U (f \<circ> id) (f \<circ> (h \<circ> k))"
  1.3512 +    apply (rule homotopic_with_compose_continuous_left [where Y=T])
  1.3513 +      apply (simp add: hom homotopic_with_symD)
  1.3514 +     using f apply auto
  1.3515 +    done
  1.3516 +  ultimately show ?thesis
  1.3517 +    apply (rule_tac c=c in that)
  1.3518 +    apply (simp add: o_def)
  1.3519 +    using homotopic_with_trans by blast
  1.3520 +qed
  1.3521 +
  1.3522 +lemma homotopy_eqv_cohomotopic_triviality_null:
  1.3523 +  fixes S :: "'a::real_normed_vector set"
  1.3524 +    and T :: "'b::real_normed_vector set"
  1.3525 +    and U :: "'c::real_normed_vector set"
  1.3526 +  assumes "S homotopy_eqv T"
  1.3527 +    shows "(\<forall>f. continuous_on S f \<and> f ` S \<subseteq> U
  1.3528 +                \<longrightarrow> (\<exists>c. homotopic_with (\<lambda>x. True) S U f (\<lambda>x. c))) \<longleftrightarrow>
  1.3529 +           (\<forall>f. continuous_on T f \<and> f ` T \<subseteq> U
  1.3530 +                \<longrightarrow> (\<exists>c. homotopic_with (\<lambda>x. True) T U f (\<lambda>x. c)))"
  1.3531 +apply (rule iffI)
  1.3532 +apply (metis assms homotopy_eqv_cohomotopic_triviality_null_imp)
  1.3533 +by (metis assms homotopy_eqv_cohomotopic_triviality_null_imp homotopy_eqv_sym)
  1.3534 +
  1.3535 +lemma homotopy_eqv_homotopic_triviality_null_imp:
  1.3536 +  fixes S :: "'a::real_normed_vector set"
  1.3537 +    and T :: "'b::real_normed_vector set"
  1.3538 +    and U :: "'c::real_normed_vector set"
  1.3539 +  assumes "S homotopy_eqv T"
  1.3540 +      and f: "continuous_on U f" "f ` U \<subseteq> T"
  1.3541 +      and homSU: "\<And>f. \<lbrakk>continuous_on U f; f ` U \<subseteq> S\<rbrakk>
  1.3542 +                      \<Longrightarrow> \<exists>c. homotopic_with (\<lambda>x. True) U S f (\<lambda>x. c)"
  1.3543 +    shows "\<exists>c. homotopic_with (\<lambda>x. True) U T f (\<lambda>x. c)"
  1.3544 +proof -
  1.3545 +  obtain h k where h: "continuous_on S h" "h ` S \<subseteq> T"
  1.3546 +               and k: "continuous_on T k" "k ` T \<subseteq> S"
  1.3547 +               and hom: "homotopic_with (\<lambda>x. True) S S (k \<circ> h) id"
  1.3548 +                        "homotopic_with (\<lambda>x. True) T T (h \<circ> k) id"
  1.3549 +    using assms by (auto simp: homotopy_eqv_def)
  1.3550 +  obtain c::'a where "homotopic_with (\<lambda>x. True) U S (k \<circ> f) (\<lambda>x. c)"
  1.3551 +    apply (rule exE [OF homSU [of "k \<circ> f"]])
  1.3552 +    apply (intro continuous_on_compose h)
  1.3553 +    using k f  apply (force elim!: continuous_on_subset)+
  1.3554 +    done
  1.3555 +  then have "homotopic_with (\<lambda>x. True) U T (h \<circ> (k \<circ> f)) (h \<circ> (\<lambda>x. c))"
  1.3556 +    apply (rule homotopic_with_compose_continuous_left [where Y=S])
  1.3557 +    using h by auto
  1.3558 +  moreover have "homotopic_with (\<lambda>x. True) U T (id \<circ> f) ((h \<circ> k) \<circ> f)"
  1.3559 +    apply (rule homotopic_with_compose_continuous_right [where X=T])
  1.3560 +      apply (simp add: hom homotopic_with_symD)
  1.3561 +     using f apply auto
  1.3562 +    done
  1.3563 +  ultimately show ?thesis
  1.3564 +    using homotopic_with_trans by (fastforce simp add: o_def)
  1.3565 +qed
  1.3566 +
  1.3567 +lemma homotopy_eqv_homotopic_triviality_null:
  1.3568 +  fixes S :: "'a::real_normed_vector set"
  1.3569 +    and T :: "'b::real_normed_vector set"
  1.3570 +    and U :: "'c::real_normed_vector set"
  1.3571 +  assumes "S homotopy_eqv T"
  1.3572 +    shows "(\<forall>f. continuous_on U f \<and> f ` U \<subseteq> S
  1.3573 +                  \<longrightarrow> (\<exists>c. homotopic_with (\<lambda>x. True) U S f (\<lambda>x. c))) \<longleftrightarrow>
  1.3574 +           (\<forall>f. continuous_on U f \<and> f ` U \<subseteq> T
  1.3575 +                  \<longrightarrow> (\<exists>c. homotopic_with (\<lambda>x. True) U T f (\<lambda>x. c)))"
  1.3576 +apply (rule iffI)
  1.3577 +apply (metis assms homotopy_eqv_homotopic_triviality_null_imp)
  1.3578 +by (metis assms homotopy_eqv_homotopic_triviality_null_imp homotopy_eqv_sym)
  1.3579 +
  1.3580 +lemma homotopy_eqv_contractible_sets:
  1.3581 +  fixes S :: "'a::real_normed_vector set"
  1.3582 +    and T :: "'b::real_normed_vector set"
  1.3583 +  assumes "contractible S" "contractible T" "S = {} \<longleftrightarrow> T = {}"
  1.3584 +    shows "S homotopy_eqv T"
  1.3585 +proof (cases "S = {}")
  1.3586 +  case True with assms show ?thesis
  1.3587 +    by (simp add: homeomorphic_imp_homotopy_eqv)
  1.3588 +next
  1.3589 +  case False
  1.3590 +  with assms obtain a b where "a \<in> S" "b \<in> T"
  1.3591 +    by auto
  1.3592 +  then show ?thesis
  1.3593 +    unfolding homotopy_eqv_def
  1.3594 +    apply (rule_tac x="\<lambda>x. b" in exI)
  1.3595 +    apply (rule_tac x="\<lambda>x. a" in exI)
  1.3596 +    apply (intro assms conjI continuous_on_id' homotopic_into_contractible)
  1.3597 +    apply (auto simp: o_def continuous_on_const)
  1.3598 +    done
  1.3599 +qed
  1.3600 +
  1.3601 +lemma homotopy_eqv_empty1 [simp]:
  1.3602 +  fixes S :: "'a::real_normed_vector set"
  1.3603 +  shows "S homotopy_eqv ({}::'b::real_normed_vector set) \<longleftrightarrow> S = {}"
  1.3604 +apply (rule iffI)
  1.3605 +using homotopy_eqv_def apply fastforce
  1.3606 +by (simp add: homotopy_eqv_contractible_sets)
  1.3607 +
  1.3608 +lemma homotopy_eqv_empty2 [simp]:
  1.3609 +  fixes S :: "'a::real_normed_vector set"
  1.3610 +  shows "({}::'b::real_normed_vector set) homotopy_eqv S \<longleftrightarrow> S = {}"
  1.3611 +by (metis homotopy_eqv_empty1 homotopy_eqv_sym)
  1.3612 +
  1.3613 +lemma homotopy_eqv_contractibility:
  1.3614 +  fixes S :: "'a::real_normed_vector set" and T :: "'b::real_normed_vector set"
  1.3615 +  shows "S homotopy_eqv T \<Longrightarrow> (contractible S \<longleftrightarrow> contractible T)"
  1.3616 +unfolding homotopy_eqv_def
  1.3617 +by (blast intro: homotopy_dominated_contractibility)
  1.3618 +
  1.3619 +lemma homotopy_eqv_sing:
  1.3620 +  fixes S :: "'a::real_normed_vector set" and a :: "'b::real_normed_vector"
  1.3621 +  shows "S homotopy_eqv {a} \<longleftrightarrow> S \<noteq> {} \<and> contractible S"
  1.3622 +proof (cases "S = {}")
  1.3623 +  case True then show ?thesis
  1.3624 +    by simp
  1.3625 +next
  1.3626 +  case False then show ?thesis
  1.3627 +    by (metis contractible_sing empty_not_insert homotopy_eqv_contractibility homotopy_eqv_contractible_sets)
  1.3628 +qed
  1.3629 +
  1.3630 +lemma homeomorphic_contractible_eq:
  1.3631 +  fixes S :: "'a::real_normed_vector set" and T :: "'b::real_normed_vector set"
  1.3632 +  shows "S homeomorphic T \<Longrightarrow> (contractible S \<longleftrightarrow> contractible T)"
  1.3633 +by (simp add: homeomorphic_imp_homotopy_eqv homotopy_eqv_contractibility)
  1.3634 +
  1.3635 +lemma homeomorphic_contractible:
  1.3636 +  fixes S :: "'a::real_normed_vector set" and T :: "'b::real_normed_vector set"
  1.3637 +  shows "\<lbrakk>contractible S; S homeomorphic T\<rbrakk> \<Longrightarrow> contractible T"
  1.3638 +  by (metis homeomorphic_contractible_eq)
  1.3639 +
  1.3640 +
  1.3641 +subsection%unimportant\<open>Misc other results\<close>
  1.3642 +
  1.3643 +lemma bounded_connected_Compl_real:
  1.3644 +  fixes S :: "real set"
  1.3645 +  assumes "bounded S" and conn: "connected(- S)"
  1.3646 +    shows "S = {}"
  1.3647 +proof -
  1.3648 +  obtain a b where "S \<subseteq> box a b"
  1.3649 +    by (meson assms bounded_subset_box_symmetric)
  1.3650 +  then have "a \<notin> S" "b \<notin> S"
  1.3651 +    by auto
  1.3652 +  then have "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> x \<in> - S"
  1.3653 +    by (meson Compl_iff conn connected_iff_interval)
  1.3654 +  then show ?thesis
  1.3655 +    using \<open>S \<subseteq> box a b\<close> by auto
  1.3656 +qed
  1.3657 +
  1.3658 +lemma bounded_connected_Compl_1:
  1.3659 +  fixes S :: "'a::{euclidean_space} set"
  1.3660 +  assumes "bounded S" and conn: "connected(- S)" and 1: "DIM('a) = 1"
  1.3661 +    shows "S = {}"
  1.3662 +proof -
  1.3663 +  have "DIM('a) = DIM(real)"
  1.3664 +    by (simp add: "1")
  1.3665 +  then obtain f::"'a \<Rightarrow> real" and g
  1.3666 +  where "linear f" "\<And>x. norm(f x) = norm x" "\<And>x. g(f x) = x" "\<And>y. f(g y) = y"
  1.3667 +    by (rule isomorphisms_UNIV_UNIV) blast
  1.3668 +  with \<open>bounded S\<close> have "bounded (f ` S)"
  1.3669 +    using bounded_linear_image linear_linear by blast
  1.3670 +  have "connected (f ` (-S))"
  1.3671 +    using connected_linear_image assms \<open>linear f\<close> by blast
  1.3672 +  moreover have "f ` (-S) = - (f ` S)"
  1.3673 +    apply (rule bij_image_Compl_eq)
  1.3674 +    apply (auto simp: bij_def)
  1.3675 +     apply (metis \<open>\<And>x. g (f x) = x\<close> injI)
  1.3676 +    by (metis UNIV_I \<open>\<And>y. f (g y) = y\<close> image_iff)
  1.3677 +  finally have "connected (- (f ` S))"
  1.3678 +    by simp
  1.3679 +  then have "f ` S = {}"
  1.3680 +    using \<open>bounded (f ` S)\<close> bounded_connected_Compl_real by blast
  1.3681 +  then show ?thesis
  1.3682 +    by blast
  1.3683 +qed
  1.3684 +
  1.3685 +
  1.3686 +subsection%unimportant\<open>Some Uncountable Sets\<close>
  1.3687 +
  1.3688 +lemma uncountable_closed_segment:
  1.3689 +  fixes a :: "'a::real_normed_vector"
  1.3690 +  assumes "a \<noteq> b" shows "uncountable (closed_segment a b)"
  1.3691 +unfolding path_image_linepath [symmetric] path_image_def
  1.3692 +  using inj_on_linepath [OF assms] uncountable_closed_interval [of 0 1]
  1.3693 +        countable_image_inj_on by auto
  1.3694 +
  1.3695 +lemma uncountable_open_segment:
  1.3696 +  fixes a :: "'a::real_normed_vector"
  1.3697 +  assumes "a \<noteq> b" shows "uncountable (open_segment a b)"
  1.3698 +  by (simp add: assms open_segment_def uncountable_closed_segment uncountable_minus_countable)
  1.3699 +
  1.3700 +lemma uncountable_convex:
  1.3701 +  fixes a :: "'a::real_normed_vector"
  1.3702 +  assumes "convex S" "a \<in> S" "b \<in> S" "a \<noteq> b"
  1.3703 +    shows "uncountable S"
  1.3704 +proof -
  1.3705 +  have "uncountable (closed_segment a b)"
  1.3706 +    by (simp add: uncountable_closed_segment assms)
  1.3707 +  then show ?thesis
  1.3708 +    by (meson assms convex_contains_segment countable_subset)
  1.3709 +qed
  1.3710 +
  1.3711 +lemma uncountable_ball:
  1.3712 +  fixes a :: "'a::euclidean_space"
  1.3713 +  assumes "r > 0"
  1.3714 +    shows "uncountable (ball a r)"
  1.3715 +proof -
  1.3716 +  have "uncountable (open_segment a (a + r *\<^sub>R (SOME i. i \<in> Basis)))"
  1.3717 +    by (metis Basis_zero SOME_Basis add_cancel_right_right assms less_le scale_eq_0_iff uncountable_open_segment)
  1.3718 +  moreover have "open_segment a (a + r *\<^sub>R (SOME i. i \<in> Basis)) \<subseteq> ball a r"
  1.3719 +    using assms by (auto simp: in_segment algebra_simps dist_norm SOME_Basis)
  1.3720 +  ultimately show ?thesis
  1.3721 +    by (metis countable_subset)
  1.3722 +qed
  1.3723 +
  1.3724 +lemma ball_minus_countable_nonempty:
  1.3725 +  assumes "countable (A :: 'a :: euclidean_space set)" "r > 0"
  1.3726 +  shows   "ball z r - A \<noteq> {}"
  1.3727 +proof
  1.3728 +  assume *: "ball z r - A = {}"
  1.3729 +  have "uncountable (ball z r - A)"
  1.3730 +    by (intro uncountable_minus_countable assms uncountable_ball)
  1.3731 +  thus False by (subst (asm) *) auto
  1.3732 +qed
  1.3733 +
  1.3734 +lemma uncountable_cball:
  1.3735 +  fixes a :: "'a::euclidean_space"
  1.3736 +  assumes "r > 0"
  1.3737 +  shows "uncountable (cball a r)"
  1.3738 +  using assms countable_subset uncountable_ball by auto
  1.3739 +
  1.3740 +lemma pairwise_disjnt_countable:
  1.3741 +  fixes \<N> :: "nat set set"
  1.3742 +  assumes "pairwise disjnt \<N>"
  1.3743 +    shows "countable \<N>"
  1.3744 +proof -
  1.3745 +  have "inj_on (\<lambda>X. SOME n. n \<in> X) (\<N> - {{}})"
  1.3746 +    apply (clarsimp simp add: inj_on_def)
  1.3747 +    by (metis assms disjnt_insert2 insert_absorb pairwise_def subsetI subset_empty tfl_some)
  1.3748 +  then show ?thesis
  1.3749 +    by (metis countable_Diff_eq countable_def)
  1.3750 +qed
  1.3751 +
  1.3752 +lemma pairwise_disjnt_countable_Union:
  1.3753 +    assumes "countable (\<Union>\<N>)" and pwd: "pairwise disjnt \<N>"
  1.3754 +    shows "countable \<N>"
  1.3755 +proof -
  1.3756 +  obtain f :: "_ \<Rightarrow> nat" where f: "inj_on f (\<Union>\<N>)"
  1.3757 +    using assms by blast
  1.3758 +  then have "pairwise disjnt (\<Union> X \<in> \<N>. {f ` X})"
  1.3759 +    using assms by (force simp: pairwise_def disjnt_inj_on_iff [OF f])
  1.3760 +  then have "countable (\<Union> X \<in> \<N>. {f ` X})"
  1.3761 +    using pairwise_disjnt_countable by blast
  1.3762 +  then show ?thesis
  1.3763 +    by (meson pwd countable_image_inj_on disjoint_image f inj_on_image pairwise_disjnt_countable)
  1.3764 +qed
  1.3765 +
  1.3766 +lemma connected_uncountable:
  1.3767 +  fixes S :: "'a::metric_space set"
  1.3768 +  assumes "connected S" "a \<in> S" "b \<in> S" "a \<noteq> b" shows "uncountable S"
  1.3769 +proof -
  1.3770 +  have "continuous_on S (dist a)"
  1.3771 +    by (intro continuous_intros)
  1.3772 +  then have "connected (dist a ` S)"
  1.3773 +    by (metis connected_continuous_image \<open>connected S\<close>)
  1.3774 +  then have "closed_segment 0 (dist a b) \<subseteq> (dist a ` S)"
  1.3775 +    by (simp add: assms closed_segment_subset is_interval_connected_1 is_interval_convex)
  1.3776 +  then have "uncountable (dist a ` S)"
  1.3777 +    by (metis \<open>a \<noteq> b\<close> countable_subset dist_eq_0_iff uncountable_closed_segment)
  1.3778 +  then show ?thesis
  1.3779 +    by blast
  1.3780 +qed
  1.3781 +
  1.3782 +lemma path_connected_uncountable:
  1.3783 +  fixes S :: "'a::metric_space set"
  1.3784 +  assumes "path_connected S" "a \<in> S" "b \<in> S" "a \<noteq> b" shows "uncountable S"
  1.3785 +  using path_connected_imp_connected assms connected_uncountable by metis
  1.3786 +
  1.3787 +lemma connected_finite_iff_sing:
  1.3788 +  fixes S :: "'a::metric_space set"
  1.3789 +  assumes "connected S"
  1.3790 +  shows "finite S \<longleftrightarrow> S = {} \<or> (\<exists>a. S = {a})"  (is "_ = ?rhs")
  1.3791 +proof -
  1.3792 +  have "uncountable S" if "\<not> ?rhs"
  1.3793 +    using connected_uncountable assms that by blast
  1.3794 +  then show ?thesis
  1.3795 +    using uncountable_infinite by auto
  1.3796 +qed
  1.3797 +
  1.3798 +lemma connected_card_eq_iff_nontrivial:
  1.3799 +  fixes S :: "'a::metric_space set"
  1.3800 +  shows "connected S \<Longrightarrow> uncountable S \<longleftrightarrow> \<not>(\<exists>a. S \<subseteq> {a})"
  1.3801 +  apply (auto simp: countable_finite finite_subset)
  1.3802 +  by (metis connected_uncountable is_singletonI' is_singleton_the_elem subset_singleton_iff)
  1.3803 +
  1.3804 +lemma simple_path_image_uncountable:
  1.3805 +  fixes g :: "real \<Rightarrow> 'a::metric_space"
  1.3806 +  assumes "simple_path g"
  1.3807 +  shows "uncountable (path_image g)"
  1.3808 +proof -
  1.3809 +  have "g 0 \<in> path_image g" "g (1/2) \<in> path_image g"
  1.3810 +    by (simp_all add: path_defs)
  1.3811 +  moreover have "g 0 \<noteq> g (1/2)"
  1.3812 +    using assms by (fastforce simp add: simple_path_def)
  1.3813 +  ultimately show ?thesis
  1.3814 +    apply (simp add: assms connected_card_eq_iff_nontrivial connected_simple_path_image)
  1.3815 +    by blast
  1.3816 +qed
  1.3817 +
  1.3818 +lemma arc_image_uncountable:
  1.3819 +  fixes g :: "real \<Rightarrow> 'a::metric_space"
  1.3820 +  assumes "arc g"
  1.3821 +  shows "uncountable (path_image g)"
  1.3822 +  by (simp add: arc_imp_simple_path assms simple_path_image_uncountable)
  1.3823 +
  1.3824 +
  1.3825 +subsection%unimportant\<open> Some simple positive connection theorems\<close>
  1.3826 +
  1.3827 +proposition path_connected_convex_diff_countable:
  1.3828 +  fixes U :: "'a::euclidean_space set"
  1.3829 +  assumes "convex U" "\<not> collinear U" "countable S"
  1.3830 +    shows "path_connected(U - S)"
  1.3831 +proof (clarsimp simp add: path_connected_def)
  1.3832 +  fix a b
  1.3833 +  assume "a \<in> U" "a \<notin> S" "b \<in> U" "b \<notin> S"
  1.3834 +  let ?m = "midpoint a b"
  1.3835 +  show "\<exists>g. path g \<and> path_image g \<subseteq> U - S \<and> pathstart g = a \<and> pathfinish g = b"
  1.3836 +  proof (cases "a = b")
  1.3837 +    case True
  1.3838 +    then show ?thesis
  1.3839 +      by (metis DiffI \<open>a \<in> U\<close> \<open>a \<notin> S\<close> path_component_def path_component_refl)
  1.3840 +  next
  1.3841 +    case False
  1.3842 +    then have "a \<noteq> ?m" "b \<noteq> ?m"
  1.3843 +      using midpoint_eq_endpoint by fastforce+
  1.3844 +    have "?m \<in> U"
  1.3845 +      using \<open>a \<in> U\<close> \<open>b \<in> U\<close> \<open>convex U\<close> convex_contains_segment by force
  1.3846 +    obtain c where "c \<in> U" and nc_abc: "\<not> collinear {a,b,c}"
  1.3847 +      by (metis False \<open>a \<in> U\<close> \<open>b \<in> U\<close> \<open>\<not> collinear U\<close> collinear_triples insert_absorb)
  1.3848 +    have ncoll_mca: "\<not> collinear {?m,c,a}"
  1.3849 +      by (metis (full_types) \<open>a \<noteq> ?m\<close> collinear_3_trans collinear_midpoint insert_commute nc_abc)
  1.3850 +    have ncoll_mcb: "\<not> collinear {?m,c,b}"
  1.3851 +      by (metis (full_types) \<open>b \<noteq> ?m\<close> collinear_3_trans collinear_midpoint insert_commute nc_abc)
  1.3852 +    have "c \<noteq> ?m"
  1.3853 +      by (metis collinear_midpoint insert_commute nc_abc)
  1.3854 +    then have "closed_segment ?m c \<subseteq> U"
  1.3855 +      by (simp add: \<open>c \<in> U\<close> \<open>?m \<in> U\<close> \<open>convex U\<close> closed_segment_subset)
  1.3856 +    then obtain z where z: "z \<in> closed_segment ?m c"
  1.3857 +                    and disjS: "(closed_segment a z \<union> closed_segment z b) \<inter> S = {}"
  1.3858 +    proof -
  1.3859 +      have False if "closed_segment ?m c \<subseteq> {z. (closed_segment a z \<union> closed_segment z b) \<inter> S \<noteq> {}}"
  1.3860 +      proof -
  1.3861 +        have closb: "closed_segment ?m c \<subseteq>
  1.3862 +                 {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> {}}"
  1.3863 +          using that by blast
  1.3864 +        have *: "countable {z \<in> closed_segment ?m c. closed_segment z u \<inter> S \<noteq> {}}"
  1.3865 +          if "u \<in> U" "u \<notin> S" and ncoll: "\<not> collinear {?m, c, u}" for u
  1.3866 +        proof -
  1.3867 +          have **: False if x1: "x1 \<in> closed_segment ?m c" and x2: "x2 \<in> closed_segment ?m c"
  1.3868 +                            and "x1 \<noteq> x2" "x1 \<noteq> u"
  1.3869 +                            and w: "w \<in> closed_segment x1 u" "w \<in> closed_segment x2 u"
  1.3870 +                            and "w \<in> S" for x1 x2 w
  1.3871 +          proof -
  1.3872 +            have "x1 \<in> affine hull {?m,c}" "x2 \<in> affine hull {?m,c}"
  1.3873 +              using segment_as_ball x1 x2 by auto
  1.3874 +            then have coll_x1: "collinear {x1, ?m, c}" and coll_x2: "collinear {?m, c, x2}"
  1.3875 +              by (simp_all add: affine_hull_3_imp_collinear) (metis affine_hull_3_imp_collinear insert_commute)
  1.3876 +            have "\<not> collinear {x1, u, x2}"
  1.3877 +            proof
  1.3878 +              assume "collinear {x1, u, x2}"
  1.3879 +              then have "collinear {?m, c, u}"
  1.3880 +                by (metis (full_types) \<open>c \<noteq> ?m\<close> coll_x1 coll_x2 collinear_3_trans insert_commute ncoll \<open>x1 \<noteq> x2\<close>)
  1.3881 +              with ncoll show False ..
  1.3882 +            qed
  1.3883 +            then have "closed_segment x1 u \<inter> closed_segment u x2 = {u}"
  1.3884 +              by (blast intro!: Int_closed_segment)
  1.3885 +            then have "w = u"
  1.3886 +              using closed_segment_commute w by auto
  1.3887 +            show ?thesis
  1.3888 +              using \<open>u \<notin> S\<close> \<open>w = u\<close> that(7) by auto
  1.3889 +          qed
  1.3890 +          then have disj: "disjoint ((\<Union>z\<in>closed_segment ?m c. {closed_segment z u \<inter> S}))"
  1.3891 +            by (fastforce simp: pairwise_def disjnt_def)
  1.3892 +          have cou: "countable ((\<Union>z \<in> closed_segment ?m c. {closed_segment z u \<inter> S}) - {{}})"
  1.3893 +            apply (rule pairwise_disjnt_countable_Union [OF _ pairwise_subset [OF disj]])
  1.3894 +             apply (rule countable_subset [OF _ \<open>countable S\<close>], auto)
  1.3895 +            done
  1.3896 +          define f where "f \<equiv> \<lambda>X. (THE z. z \<in> closed_segment ?m c \<and> X = closed_segment z u \<inter> S)"
  1.3897 +          show ?thesis
  1.3898 +          proof (rule countable_subset [OF _ countable_image [OF cou, where f=f]], clarify)
  1.3899 +            fix x
  1.3900 +            assume x: "x \<in> closed_segment ?m c" "closed_segment x u \<inter> S \<noteq> {}"
  1.3901 +            show "x \<in> f ` ((\<Union>z\<in>closed_segment ?m c. {closed_segment z u \<inter> S}) - {{}})"
  1.3902 +            proof (rule_tac x="closed_segment x u \<inter> S" in image_eqI)
  1.3903 +              show "x = f (closed_segment x u \<inter> S)"
  1.3904 +                unfolding f_def
  1.3905 +                apply (rule the_equality [symmetric])
  1.3906 +                using x  apply (auto simp: dest: **)
  1.3907 +                done
  1.3908 +            qed (use x in auto)
  1.3909 +          qed
  1.3910 +        qed
  1.3911 +        have "uncountable (closed_segment ?m c)"
  1.3912 +          by (metis \<open>c \<noteq> ?m\<close> uncountable_closed_segment)
  1.3913 +        then show False
  1.3914 +          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]
  1.3915 +          apply (simp add: closed_segment_commute)
  1.3916 +          by (simp add: countable_subset)
  1.3917 +      qed
  1.3918 +      then show ?thesis
  1.3919 +        by (force intro: that)
  1.3920 +    qed
  1.3921 +    show ?thesis
  1.3922 +    proof (intro exI conjI)
  1.3923 +      have "path_image (linepath a z +++ linepath z b) \<subseteq> U"
  1.3924 +        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)
  1.3925 +      with disjS show "path_image (linepath a z +++ linepath z b) \<subseteq> U - S"
  1.3926 +        by (force simp: path_image_join)
  1.3927 +    qed auto
  1.3928 +  qed
  1.3929 +qed
  1.3930 +
  1.3931 +
  1.3932 +corollary connected_convex_diff_countable:
  1.3933 +  fixes U :: "'a::euclidean_space set"
  1.3934 +  assumes "convex U" "\<not> collinear U" "countable S"
  1.3935 +  shows "connected(U - S)"
  1.3936 +  by (simp add: assms path_connected_convex_diff_countable path_connected_imp_connected)
  1.3937 +
  1.3938 +lemma path_connected_punctured_convex:
  1.3939 +  assumes "convex S" and aff: "aff_dim S \<noteq> 1"
  1.3940 +    shows "path_connected(S - {a})"
  1.3941 +proof -
  1.3942 +  consider "aff_dim S = -1" | "aff_dim S = 0" | "aff_dim S \<ge> 2"
  1.3943 +    using assms aff_dim_geq [of S] by linarith
  1.3944 +  then show ?thesis
  1.3945 +  proof cases
  1.3946 +    assume "aff_dim S = -1"
  1.3947 +    then show ?thesis
  1.3948 +      by (metis aff_dim_empty empty_Diff path_connected_empty)
  1.3949 +  next
  1.3950 +    assume "aff_dim S = 0"
  1.3951 +    then show ?thesis
  1.3952 +      by (metis aff_dim_eq_0 Diff_cancel Diff_empty Diff_insert0 convex_empty convex_imp_path_connected path_connected_singleton singletonD)
  1.3953 +  next
  1.3954 +    assume ge2: "aff_dim S \<ge> 2"
  1.3955 +    then have "\<not> collinear S"
  1.3956 +    proof (clarsimp simp add: collinear_affine_hull)
  1.3957 +      fix u v
  1.3958 +      assume "S \<subseteq> affine hull {u, v}"
  1.3959 +      then have "aff_dim S \<le> aff_dim {u, v}"
  1.3960 +        by (metis (no_types) aff_dim_affine_hull aff_dim_subset)
  1.3961 +      with ge2 show False
  1.3962 +        by (metis (no_types) aff_dim_2 antisym aff not_numeral_le_zero one_le_numeral order_trans)
  1.3963 +    qed
  1.3964 +    then show ?thesis
  1.3965 +      apply (rule path_connected_convex_diff_countable [OF \<open>convex S\<close>])
  1.3966 +      by simp
  1.3967 +  qed
  1.3968 +qed
  1.3969 +
  1.3970 +lemma connected_punctured_convex:
  1.3971 +  shows "\<lbrakk>convex S; aff_dim S \<noteq> 1\<rbrakk> \<Longrightarrow> connected(S - {a})"
  1.3972 +  using path_connected_imp_connected path_connected_punctured_convex by blast
  1.3973 +
  1.3974 +lemma path_connected_complement_countable:
  1.3975 +  fixes S :: "'a::euclidean_space set"
  1.3976 +  assumes "2 \<le> DIM('a)" "countable S"
  1.3977 +  shows "path_connected(- S)"
  1.3978 +proof -
  1.3979 +  have "path_connected(UNIV - S)"
  1.3980 +    apply (rule path_connected_convex_diff_countable)
  1.3981 +    using assms by (auto simp: collinear_aff_dim [of "UNIV :: 'a set"])
  1.3982 +  then show ?thesis
  1.3983 +    by (simp add: Compl_eq_Diff_UNIV)
  1.3984 +qed
  1.3985 +
  1.3986 +proposition path_connected_openin_diff_countable:
  1.3987 +  fixes S :: "'a::euclidean_space set"
  1.3988 +  assumes "connected S" and ope: "openin (subtopology euclidean (affine hull S)) S"
  1.3989 +      and "\<not> collinear S" "countable T"
  1.3990 +    shows "path_connected(S - T)"
  1.3991 +proof (clarsimp simp add: path_connected_component)
  1.3992 +  fix x y
  1.3993 +  assume xy: "x \<in> S" "x \<notin> T" "y \<in> S" "y \<notin> T"
  1.3994 +  show "path_component (S - T) x y"
  1.3995 +  proof (rule connected_equivalence_relation_gen [OF \<open>connected S\<close>, where P = "\<lambda>x. x \<notin> T"])
  1.3996 +    show "\<exists>z. z \<in> U \<and> z \<notin> T" if opeU: "openin (subtopology euclidean S) U" and "x \<in> U" for U x
  1.3997 +    proof -
  1.3998 +      have "openin (subtopology euclidean (affine hull S)) U"
  1.3999 +        using opeU ope openin_trans by blast
  1.4000 +      with \<open>x \<in> U\<close> obtain r where Usub: "U \<subseteq> affine hull S" and "r > 0"
  1.4001 +                              and subU: "ball x r \<inter> affine hull S \<subseteq> U"
  1.4002 +        by (auto simp: openin_contains_ball)
  1.4003 +      with \<open>x \<in> U\<close> have x: "x \<in> ball x r \<inter> affine hull S"
  1.4004 +        by auto
  1.4005 +      have "\<not> S \<subseteq> {x}"
  1.4006 +        using \<open>\<not> collinear S\<close>  collinear_subset by blast
  1.4007 +      then obtain x' where "x' \<noteq> x" "x' \<in> S"
  1.4008 +        by blast
  1.4009 +      obtain y where y: "y \<noteq> x" "y \<in> ball x r \<inter> affine hull S"
  1.4010 +      proof
  1.4011 +        show "x + (r / 2 / norm(x' - x)) *\<^sub>R (x' - x) \<noteq> x"
  1.4012 +          using \<open>x' \<noteq> x\<close> \<open>r > 0\<close> by auto
  1.4013 +        show "x + (r / 2 / norm (x' - x)) *\<^sub>R (x' - x) \<in> ball x r \<inter> affine hull S"
  1.4014 +          using \<open>x' \<noteq> x\<close> \<open>r > 0\<close> \<open>x' \<in> S\<close> x
  1.4015 +          by (simp add: dist_norm mem_affine_3_minus hull_inc)
  1.4016 +      qed
  1.4017 +      have "convex (ball x r \<inter> affine hull S)"
  1.4018 +        by (simp add: affine_imp_convex convex_Int)
  1.4019 +      with x y subU have "uncountable U"
  1.4020 +        by (meson countable_subset uncountable_convex)
  1.4021 +      then have "\<not> U \<subseteq> T"
  1.4022 +        using \<open>countable T\<close> countable_subset by blast
  1.4023 +      then show ?thesis by blast
  1.4024 +    qed
  1.4025 +    show "\<exists>U. openin (subtopology euclidean S) U \<and> x \<in> U \<and>
  1.4026 +              (\<forall>x\<in>U. \<forall>y\<in>U. x \<notin> T \<and> y \<notin> T \<longrightarrow> path_component (S - T) x y)"
  1.4027 +          if "x \<in> S" for x
  1.4028 +    proof -
  1.4029 +      obtain r where Ssub: "S \<subseteq> affine hull S" and "r > 0"
  1.4030 +                 and subS: "ball x r \<inter> affine hull S \<subseteq> S"
  1.4031 +        using ope \<open>x \<in> S\<close> by (auto simp: openin_contains_ball)
  1.4032 +      then have conv: "convex (ball x r \<inter> affine hull S)"
  1.4033 +        by (simp add: affine_imp_convex convex_Int)
  1.4034 +      have "\<not> aff_dim (affine hull S) \<le> 1"
  1.4035 +        using \<open>\<not> collinear S\<close> collinear_aff_dim by auto
  1.4036 +      then have "\<not> collinear (ball x r \<inter> affine hull S)"
  1.4037 +        apply (simp add: collinear_aff_dim)
  1.4038 +        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)
  1.4039 +      then have *: "path_connected ((ball x r \<inter> affine hull S) - T)"
  1.4040 +        by (rule path_connected_convex_diff_countable [OF conv _ \<open>countable T\<close>])
  1.4041 +      have ST: "ball x r \<inter> affine hull S - T \<subseteq> S - T"
  1.4042 +        using subS by auto
  1.4043 +      show ?thesis
  1.4044 +      proof (intro exI conjI)
  1.4045 +        show "x \<in> ball x r \<inter> affine hull S"
  1.4046 +          using \<open>x \<in> S\<close> \<open>r > 0\<close> by (simp add: hull_inc)
  1.4047 +        have "openin (subtopology euclidean (affine hull S)) (ball x r \<inter> affine hull S)"
  1.4048 +          by (subst inf.commute) (simp add: openin_Int_open)
  1.4049 +        then show "openin (subtopology euclidean S) (ball x r \<inter> affine hull S)"
  1.4050 +          by (rule openin_subset_trans [OF _ subS Ssub])
  1.4051 +      qed (use * path_component_trans in \<open>auto simp: path_connected_component path_component_of_subset [OF ST]\<close>)
  1.4052 +    qed
  1.4053 +  qed (use xy path_component_trans in auto)
  1.4054 +qed
  1.4055 +
  1.4056 +corollary connected_openin_diff_countable:
  1.4057 +  fixes S :: "'a::euclidean_space set"
  1.4058 +  assumes "connected S" and ope: "openin (subtopology euclidean (affine hull S)) S"
  1.4059 +      and "\<not> collinear S" "countable T"
  1.4060 +    shows "connected(S - T)"
  1.4061 +  by (metis path_connected_imp_connected path_connected_openin_diff_countable [OF assms])
  1.4062 +
  1.4063 +corollary path_connected_open_diff_countable:
  1.4064 +  fixes S :: "'a::euclidean_space set"
  1.4065 +  assumes "2 \<le> DIM('a)" "open S" "connected S" "countable T"
  1.4066 +  shows "path_connected(S - T)"
  1.4067 +proof (cases "S = {}")
  1.4068 +  case True
  1.4069 +  then show ?thesis
  1.4070 +    by (simp add: path_connected_empty)
  1.4071 +next
  1.4072 +  case False
  1.4073 +  show ?thesis
  1.4074 +  proof (rule path_connected_openin_diff_countable)
  1.4075 +    show "openin (subtopology euclidean (affine hull S)) S"
  1.4076 +      by (simp add: assms hull_subset open_subset)
  1.4077 +    show "\<not> collinear S"
  1.4078 +      using assms False by (simp add: collinear_aff_dim aff_dim_open)
  1.4079 +  qed (simp_all add: assms)
  1.4080 +qed
  1.4081 +
  1.4082 +corollary connected_open_diff_countable:
  1.4083 +  fixes S :: "'a::euclidean_space set"
  1.4084 +  assumes "2 \<le> DIM('a)" "open S" "connected S" "countable T"
  1.4085 +  shows "connected(S - T)"
  1.4086 +by (simp add: assms path_connected_imp_connected path_connected_open_diff_countable)
  1.4087 +
  1.4088 +
  1.4089 +
  1.4090 +subsection%unimportant \<open>Self-homeomorphisms shuffling points about\<close>
  1.4091 +
  1.4092 +subsubsection%unimportant\<open>The theorem \<open>homeomorphism_moving_points_exists\<close>\<close>
  1.4093 +
  1.4094 +lemma homeomorphism_moving_point_1:
  1.4095 +  fixes a :: "'a::euclidean_space"
  1.4096 +  assumes "affine T" "a \<in> T" and u: "u \<in> ball a r \<inter> T"
  1.4097 +  obtains f g where "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f g"
  1.4098 +                    "f a = u" "\<And>x. x \<in> sphere a r \<Longrightarrow> f x = x"
  1.4099 +proof -
  1.4100 +  have nou: "norm (u - a) < r" and "u \<in> T"
  1.4101 +    using u by (auto simp: dist_norm norm_minus_commute)
  1.4102 +  then have "0 < r"
  1.4103 +    by (metis DiffD1 Diff_Diff_Int ball_eq_empty centre_in_ball not_le u)
  1.4104 +  define f where "f \<equiv> \<lambda>x. (1 - norm(x - a) / r) *\<^sub>R (u - a) + x"
  1.4105 +  have *: "False" if eq: "x + (norm y / r) *\<^sub>R u = y + (norm x / r) *\<^sub>R u"
  1.4106 +                  and nou: "norm u < r" and yx: "norm y < norm x" for x y and u::'a
  1.4107 +  proof -
  1.4108 +    have "x = y + (norm x / r - (norm y / r)) *\<^sub>R u"
  1.4109 +      using eq by (simp add: algebra_simps)
  1.4110 +    then have "norm x = norm (y + ((norm x - norm y) / r) *\<^sub>R u)"
  1.4111 +      by (metis diff_divide_distrib)
  1.4112 +    also have "\<dots> \<le> norm y + norm(((norm x - norm y) / r) *\<^sub>R u)"
  1.4113 +      using norm_triangle_ineq by blast
  1.4114 +    also have "\<dots> = norm y + (norm x - norm y) * (norm u / r)"
  1.4115 +      using yx \<open>r > 0\<close>
  1.4116 +      by (simp add: divide_simps)
  1.4117 +    also have "\<dots> < norm y + (norm x - norm y) * 1"
  1.4118 +      apply (subst add_less_cancel_left)
  1.4119 +      apply (rule mult_strict_left_mono)
  1.4120 +      using nou \<open>0 < r\<close> yx
  1.4121 +       apply (simp_all add: field_simps)
  1.4122 +      done
  1.4123 +    also have "\<dots> = norm x"
  1.4124 +      by simp
  1.4125 +    finally show False by simp
  1.4126 +  qed
  1.4127 +  have "inj f"
  1.4128 +    unfolding f_def
  1.4129 +  proof (clarsimp simp: inj_on_def)
  1.4130 +    fix x y
  1.4131 +    assume "(1 - norm (x - a) / r) *\<^sub>R (u - a) + x =
  1.4132 +            (1 - norm (y - a) / r) *\<^sub>R (u - a) + y"
  1.4133 +    then have eq: "(x - a) + (norm (y - a) / r) *\<^sub>R (u - a) = (y - a) + (norm (x - a) / r) *\<^sub>R (u - a)"
  1.4134 +      by (auto simp: algebra_simps)
  1.4135 +    show "x=y"
  1.4136 +    proof (cases "norm (x - a) = norm (y - a)")
  1.4137 +      case True
  1.4138 +      then show ?thesis
  1.4139 +        using eq by auto
  1.4140 +    next
  1.4141 +      case False
  1.4142 +      then consider "norm (x - a) < norm (y - a)" | "norm (x - a) > norm (y - a)"
  1.4143 +        by linarith
  1.4144 +      then have "False"
  1.4145 +      proof cases
  1.4146 +        case 1 show False
  1.4147 +          using * [OF _ nou 1] eq by simp
  1.4148 +      next
  1.4149 +        case 2 with * [OF eq nou] show False
  1.4150 +          by auto
  1.4151 +      qed
  1.4152 +      then show "x=y" ..
  1.4153 +    qed
  1.4154 +  qed
  1.4155 +  then have inj_onf: "inj_on f (cball a r \<inter> T)"
  1.4156 +    using inj_on_Int by fastforce
  1.4157 +  have contf: "continuous_on (cball a r \<inter> T) f"
  1.4158 +    unfolding f_def using \<open>0 < r\<close>  by (intro continuous_intros) blast
  1.4159 +  have fim: "f ` (cball a r \<inter> T) = cball a r \<inter> T"
  1.4160 +  proof
  1.4161 +    have *: "norm (y + (1 - norm y / r) *\<^sub>R u) \<le> r" if "norm y \<le> r" "norm u < r" for y u::'a
  1.4162 +    proof -
  1.4163 +      have "norm (y + (1 - norm y / r) *\<^sub>R u) \<le> norm y + norm((1 - norm y / r) *\<^sub>R u)"
  1.4164 +        using norm_triangle_ineq by blast
  1.4165 +      also have "\<dots> = norm y + abs(1 - norm y / r) * norm u"
  1.4166 +        by simp
  1.4167 +      also have "\<dots> \<le> r"
  1.4168 +      proof -
  1.4169 +        have "(r - norm u) * (r - norm y) \<ge> 0"
  1.4170 +          using that by auto
  1.4171 +        then have "r * norm u + r * norm y \<le> r * r + norm u * norm y"
  1.4172 +          by (simp add: algebra_simps)
  1.4173 +        then show ?thesis
  1.4174 +        using that \<open>0 < r\<close> by (simp add: abs_if field_simps)
  1.4175 +      qed
  1.4176 +      finally show ?thesis .
  1.4177 +    qed
  1.4178 +    have "f ` (cball a r) \<subseteq> cball a r"
  1.4179 +      apply (clarsimp simp add: dist_norm norm_minus_commute f_def)
  1.4180 +      using * by (metis diff_add_eq diff_diff_add diff_diff_eq2 norm_minus_commute nou)
  1.4181 +    moreover have "f ` T \<subseteq> T"
  1.4182 +      unfolding f_def using \<open>affine T\<close> \<open>a \<in> T\<close> \<open>u \<in> T\<close>
  1.4183 +      by (force simp: add.commute mem_affine_3_minus)
  1.4184 +    ultimately show "f ` (cball a r \<inter> T) \<subseteq> cball a r \<inter> T"
  1.4185 +      by blast
  1.4186 +  next
  1.4187 +    show "cball a r \<inter> T \<subseteq> f ` (cball a r \<inter> T)"
  1.4188 +    proof (clarsimp simp add: dist_norm norm_minus_commute)
  1.4189 +      fix x
  1.4190 +      assume x: "norm (x - a) \<le> r" and "x \<in> T"
  1.4191 +      have "\<exists>v \<in> {0..1}. ((1 - v) * r - norm ((x - a) - v *\<^sub>R (u - a))) \<bullet> 1 = 0"
  1.4192 +        by (rule ivt_decreasing_component_on_1) (auto simp: x continuous_intros)
  1.4193 +      then obtain v where "0\<le>v" "v\<le>1" and v: "(1 - v) * r = norm ((x - a) - v *\<^sub>R (u - a))"
  1.4194 +        by auto
  1.4195 +      show "x \<in> f ` (cball a r \<inter> T)"
  1.4196 +      proof (rule image_eqI)
  1.4197 +        show "x = f (x - v *\<^sub>R (u - a))"
  1.4198 +          using \<open>r > 0\<close> v by (simp add: f_def field_simps)
  1.4199 +        have "x - v *\<^sub>R (u - a) \<in> cball a r"
  1.4200 +          using \<open>r > 0\<close> v \<open>0 \<le> v\<close>
  1.4201 +          apply (simp add: field_simps dist_norm norm_minus_commute)
  1.4202 +          by (metis le_add_same_cancel2 order.order_iff_strict zero_le_mult_iff)
  1.4203 +        moreover have "x - v *\<^sub>R (u - a) \<in> T"
  1.4204 +          by (simp add: f_def \<open>affine T\<close> \<open>u \<in> T\<close> \<open>x \<in> T\<close> assms mem_affine_3_minus2)
  1.4205 +        ultimately show "x - v *\<^sub>R (u - a) \<in> cball a r \<inter> T"
  1.4206 +          by blast
  1.4207 +      qed
  1.4208 +    qed
  1.4209 +  qed
  1.4210 +  have "\<exists>g. homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f g"
  1.4211 +    apply (rule homeomorphism_compact [OF _ contf fim inj_onf])
  1.4212 +    apply (simp add: affine_closed compact_Int_closed \<open>affine T\<close>)
  1.4213 +    done
  1.4214 +  then show ?thesis
  1.4215 +    apply (rule exE)
  1.4216 +    apply (erule_tac f=f in that)
  1.4217 +    using \<open>r > 0\<close>
  1.4218 +     apply (simp_all add: f_def dist_norm norm_minus_commute)
  1.4219 +    done
  1.4220 +qed
  1.4221 +
  1.4222 +corollary%unimportant homeomorphism_moving_point_2:
  1.4223 +  fixes a :: "'a::euclidean_space"
  1.4224 +  assumes "affine T" "a \<in> T" and u: "u \<in> ball a r \<inter> T" and v: "v \<in> ball a r \<inter> T"
  1.4225 +  obtains f g where "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f g"
  1.4226 +                    "f u = v" "\<And>x. \<lbrakk>x \<in> sphere a r; x \<in> T\<rbrakk> \<Longrightarrow> f x = x"
  1.4227 +proof -
  1.4228 +  have "0 < r"
  1.4229 +    by (metis DiffD1 Diff_Diff_Int ball_eq_empty centre_in_ball not_le u)
  1.4230 +  obtain f1 g1 where hom1: "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f1 g1"
  1.4231 +                 and "f1 a = u" and f1: "\<And>x. x \<in> sphere a r \<Longrightarrow> f1 x = x"
  1.4232 +    using homeomorphism_moving_point_1 [OF \<open>affine T\<close> \<open>a \<in> T\<close> u] by blast
  1.4233 +  obtain f2 g2 where hom2: "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f2 g2"
  1.4234 +                 and "f2 a = v" and f2: "\<And>x. x \<in> sphere a r \<Longrightarrow> f2 x = x"
  1.4235 +    using homeomorphism_moving_point_1 [OF \<open>affine T\<close> \<open>a \<in> T\<close> v] by blast
  1.4236 +  show ?thesis
  1.4237 +  proof
  1.4238 +    show "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) (f2 \<circ> g1) (f1 \<circ> g2)"
  1.4239 +      by (metis homeomorphism_compose homeomorphism_symD hom1 hom2)
  1.4240 +    have "g1 u = a"
  1.4241 +      using \<open>0 < r\<close> \<open>f1 a = u\<close> assms hom1 homeomorphism_apply1 by fastforce
  1.4242 +    then show "(f2 \<circ> g1) u = v"
  1.4243 +      by (simp add: \<open>f2 a = v\<close>)
  1.4244 +    show "\<And>x. \<lbrakk>x \<in> sphere a r; x \<in> T\<rbrakk> \<Longrightarrow> (f2 \<circ> g1) x = x"
  1.4245 +      using f1 f2 hom1 homeomorphism_apply1 by fastforce
  1.4246 +  qed
  1.4247 +qed
  1.4248 +
  1.4249 +
  1.4250 +corollary%unimportant homeomorphism_moving_point_3:
  1.4251 +  fixes a :: "'a::euclidean_space"
  1.4252 +  assumes "affine T" "a \<in> T" and ST: "ball a r \<inter> T \<subseteq> S" "S \<subseteq> T"
  1.4253 +      and u: "u \<in> ball a r \<inter> T" and v: "v \<in> ball a r \<inter> T"
  1.4254 +  obtains f g where "homeomorphism S S f g"
  1.4255 +                    "f u = v" "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> ball a r \<inter> T"
  1.4256 +proof -
  1.4257 +  obtain f g where hom: "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f g"
  1.4258 +               and "f u = v" and fid: "\<And>x. \<lbrakk>x \<in> sphere a r; x \<in> T\<rbrakk> \<Longrightarrow> f x = x"
  1.4259 +    using homeomorphism_moving_point_2 [OF \<open>affine T\<close> \<open>a \<in> T\<close> u v] by blast
  1.4260 +  have gid: "\<And>x. \<lbrakk>x \<in> sphere a r; x \<in> T\<rbrakk> \<Longrightarrow> g x = x"
  1.4261 +    using fid hom homeomorphism_apply1 by fastforce
  1.4262 +  define ff where "ff \<equiv> \<lambda>x. if x \<in> ball a r \<inter> T then f x else x"
  1.4263 +  define gg where "gg \<equiv> \<lambda>x. if x \<in> ball a r \<inter> T then g x else x"
  1.4264 +  show ?thesis
  1.4265 +  proof
  1.4266 +    show "homeomorphism S S ff gg"
  1.4267 +    proof (rule homeomorphismI)
  1.4268 +      have "continuous_on ((cball a r \<inter> T) \<union> (T - ball a r)) ff"
  1.4269 +        apply (simp add: ff_def)
  1.4270 +        apply (rule continuous_on_cases)
  1.4271 +        using homeomorphism_cont1 [OF hom]
  1.4272 +            apply (auto simp: affine_closed \<open>affine T\<close> continuous_on_id fid)
  1.4273 +        done
  1.4274 +      then show "continuous_on S ff"
  1.4275 +        apply (rule continuous_on_subset)
  1.4276 +        using ST by auto
  1.4277 +      have "continuous_on ((cball a r \<inter> T) \<union> (T - ball a r)) gg"
  1.4278 +        apply (simp add: gg_def)
  1.4279 +        apply (rule continuous_on_cases)
  1.4280 +        using homeomorphism_cont2 [OF hom]
  1.4281 +            apply (auto simp: affine_closed \<open>affine T\<close> continuous_on_id gid)
  1.4282 +        done
  1.4283 +      then show "continuous_on S gg"
  1.4284 +        apply (rule continuous_on_subset)
  1.4285 +        using ST by auto
  1.4286 +      show "ff ` S \<subseteq> S"
  1.4287 +      proof (clarsimp simp add: ff_def)
  1.4288 +        fix x
  1.4289 +        assume "x \<in> S" and x: "dist a x < r" and "x \<in> T"
  1.4290 +        then have "f x \<in> cball a r \<inter> T"
  1.4291 +          using homeomorphism_image1 [OF hom] by force
  1.4292 +        then show "f x \<in> S"
  1.4293 +          using ST(1) \<open>x \<in> T\<close> gid hom homeomorphism_def x by fastforce
  1.4294 +      qed
  1.4295 +      show "gg ` S \<subseteq> S"
  1.4296 +      proof (clarsimp simp add: gg_def)
  1.4297 +        fix x
  1.4298 +        assume "x \<in> S" and x: "dist a x < r" and "x \<in> T"
  1.4299 +        then have "g x \<in> cball a r \<inter> T"
  1.4300 +          using homeomorphism_image2 [OF hom] by force
  1.4301 +        then have "g x \<in> ball a r"
  1.4302 +          using homeomorphism_apply2 [OF hom]
  1.4303 +            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)
  1.4304 +        then show "g x \<in> S"
  1.4305 +          using ST(1) \<open>g x \<in> cball a r \<inter> T\<close> by force
  1.4306 +        qed
  1.4307 +      show "\<And>x. x \<in> S \<Longrightarrow> gg (ff x) = x"
  1.4308 +        apply (simp add: ff_def gg_def)
  1.4309 +        using homeomorphism_apply1 [OF hom] homeomorphism_image1 [OF hom]
  1.4310 +        apply auto
  1.4311 +        apply (metis Int_iff homeomorphism_apply1 [OF hom] fid image_eqI less_eq_real_def mem_cball mem_sphere)
  1.4312 +        done
  1.4313 +      show "\<And>x. x \<in> S \<Longrightarrow> ff (gg x) = x"
  1.4314 +        apply (simp add: ff_def gg_def)
  1.4315 +        using homeomorphism_apply2 [OF hom] homeomorphism_image2 [OF hom]
  1.4316 +        apply auto
  1.4317 +        apply (metis Int_iff fid image_eqI less_eq_real_def mem_cball mem_sphere)
  1.4318 +        done
  1.4319 +    qed
  1.4320 +    show "ff u = v"
  1.4321 +      using u by (auto simp: ff_def \<open>f u = v\<close>)
  1.4322 +    show "{x. \<not> (ff x = x \<and> gg x = x)} \<subseteq> ball a r \<inter> T"
  1.4323 +      by (auto simp: ff_def gg_def)
  1.4324 +  qed
  1.4325 +qed
  1.4326 +
  1.4327 +
  1.4328 +proposition%unimportant homeomorphism_moving_point:
  1.4329 +  fixes a :: "'a::euclidean_space"
  1.4330 +  assumes ope: "openin (subtopology euclidean (affine hull S)) S"
  1.4331 +      and "S \<subseteq> T"
  1.4332 +      and TS: "T \<subseteq> affine hull S"
  1.4333 +      and S: "connected S" "a \<in> S" "b \<in> S"
  1.4334 +  obtains f g where "homeomorphism T T f g" "f a = b"
  1.4335 +                    "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S"
  1.4336 +                    "bounded {x. \<not> (f x = x \<and> g x = x)}"
  1.4337 +proof -
  1.4338 +  have 1: "\<exists>h k. homeomorphism T T h k \<and> h (f d) = d \<and>
  1.4339 +              {x. \<not> (h x = x \<and> k x = x)} \<subseteq> S \<and> bounded {x. \<not> (h x = x \<and> k x = x)}"
  1.4340 +        if "d \<in> S" "f d \<in> S" and homfg: "homeomorphism T T f g"
  1.4341 +        and S: "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S"
  1.4342 +        and bo: "bounded {x. \<not> (f x = x \<and> g x = x)}" for d f g
  1.4343 +  proof (intro exI conjI)
  1.4344 +    show homgf: "homeomorphism T T g f"
  1.4345 +      by (metis homeomorphism_symD homfg)
  1.4346 +    then show "g (f d) = d"
  1.4347 +      by (meson \<open>S \<subseteq> T\<close> homeomorphism_def subsetD \<open>d \<in> S\<close>)
  1.4348 +    show "{x. \<not> (g x = x \<and> f x = x)} \<subseteq> S"
  1.4349 +      using S by blast
  1.4350 +    show "bounded {x. \<not> (g x = x \<and> f x = x)}"
  1.4351 +      using bo by (simp add: conj_commute)
  1.4352 +  qed
  1.4353 +  have 2: "\<exists>f g. homeomorphism T T f g \<and> f x = f2 (f1 x) \<and>
  1.4354 +                 {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
  1.4355 +             if "x \<in> S" "f1 x \<in> S" "f2 (f1 x) \<in> S"
  1.4356 +                and hom: "homeomorphism T T f1 g1" "homeomorphism T T f2 g2"
  1.4357 +                and sub: "{x. \<not> (f1 x = x \<and> g1 x = x)} \<subseteq> S"   "{x. \<not> (f2 x = x \<and> g2 x = x)} \<subseteq> S"
  1.4358 +                and bo: "bounded {x. \<not> (f1 x = x \<and> g1 x = x)}"  "bounded {x. \<not> (f2 x = x \<and> g2 x = x)}"
  1.4359 +             for x f1 f2 g1 g2
  1.4360 +  proof (intro exI conjI)
  1.4361 +    show homgf: "homeomorphism T T (f2 \<circ> f1) (g1 \<circ> g2)"
  1.4362 +      by (metis homeomorphism_compose hom)
  1.4363 +    then show "(f2 \<circ> f1) x = f2 (f1 x)"
  1.4364 +      by force
  1.4365 +    show "{x. \<not> ((f2 \<circ> f1) x = x \<and> (g1 \<circ> g2) x = x)} \<subseteq> S"
  1.4366 +      using sub by force
  1.4367 +    have "bounded ({x. \<not>(f1 x = x \<and> g1 x = x)} \<union> {x. \<not>(f2 x = x \<and> g2 x = x)})"
  1.4368 +      using bo by simp
  1.4369 +    then show "bounded {x. \<not> ((f2 \<circ> f1) x = x \<and> (g1 \<circ> g2) x = x)}"
  1.4370 +      by (rule bounded_subset) auto
  1.4371 +  qed
  1.4372 +  have 3: "\<exists>U. openin (subtopology euclidean S) U \<and>
  1.4373 +              d \<in> U \<and>
  1.4374 +              (\<forall>x\<in>U.
  1.4375 +                  \<exists>f g. homeomorphism T T f g \<and> f d = x \<and>
  1.4376 +                        {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and>
  1.4377 +                        bounded {x. \<not> (f x = x \<and> g x = x)})"
  1.4378 +           if "d \<in> S" for d
  1.4379 +  proof -
  1.4380 +    obtain r where "r > 0" and r: "ball d r \<inter> affine hull S \<subseteq> S"
  1.4381 +      by (metis \<open>d \<in> S\<close> ope openin_contains_ball)
  1.4382 +    have *: "\<exists>f g. homeomorphism T T f g \<and> f d = e \<and>
  1.4383 +                   {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and>
  1.4384 +                   bounded {x. \<not> (f x = x \<and> g x = x)}" if "e \<in> S" "e \<in> ball d r" for e
  1.4385 +      apply (rule homeomorphism_moving_point_3 [of "affine hull S" d r T d e])
  1.4386 +      using r \<open>S \<subseteq> T\<close> TS that
  1.4387 +            apply (auto simp: \<open>d \<in> S\<close> \<open>0 < r\<close> hull_inc)
  1.4388 +      using bounded_subset by blast
  1.4389 +    show ?thesis
  1.4390 +      apply (rule_tac x="S \<inter> ball d r" in exI)
  1.4391 +      apply (intro conjI)
  1.4392 +        apply (simp add: openin_open_Int)
  1.4393 +       apply (simp add: \<open>0 < r\<close> that)
  1.4394 +      apply (blast intro: *)
  1.4395 +      done
  1.4396 +  qed
  1.4397 +  have "\<exists>f g. homeomorphism T T f g \<and> f a = b \<and>
  1.4398 +              {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
  1.4399 +    apply (rule connected_equivalence_relation [OF S], safe)
  1.4400 +      apply (blast intro: 1 2 3)+
  1.4401 +    done
  1.4402 +  then show ?thesis
  1.4403 +    using that by auto
  1.4404 +qed
  1.4405 +
  1.4406 +
  1.4407 +lemma homeomorphism_moving_points_exists_gen:
  1.4408 +  assumes K: "finite K" "\<And>i. i \<in> K \<Longrightarrow> x i \<in> S \<and> y i \<in> S"
  1.4409 +             "pairwise (\<lambda>i j. (x i \<noteq> x j) \<and> (y i \<noteq> y j)) K"
  1.4410 +      and "2 \<le> aff_dim S"
  1.4411 +      and ope: "openin (subtopology euclidean (affine hull S)) S"
  1.4412 +      and "S \<subseteq> T" "T \<subseteq> affine hull S" "connected S"
  1.4413 +  shows "\<exists>f g. homeomorphism T T f g \<and> (\<forall>i \<in> K. f(x i) = y i) \<and>
  1.4414 +               {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
  1.4415 +  using assms
  1.4416 +proof (induction K)
  1.4417 +  case empty
  1.4418 +  then show ?case
  1.4419 +    by (force simp: homeomorphism_ident)
  1.4420 +next
  1.4421 +  case (insert i K)
  1.4422 +  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"
  1.4423 +       and pw: "pairwise (\<lambda>i j. x i \<noteq> x j \<and> y i \<noteq> y j) K"
  1.4424 +       and "x i \<in> S" "y i \<in> S"
  1.4425 +       and xyS: "\<And>i. i \<in> K \<Longrightarrow> x i \<in> S \<and> y i \<in> S"
  1.4426 +    by (simp_all add: pairwise_insert)
  1.4427 +  obtain f g where homfg: "homeomorphism T T f g" and feq: "\<And>i. i \<in> K \<Longrightarrow> f(x i) = y i"
  1.4428 +               and fg_sub: "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S"
  1.4429 +               and bo_fg: "bounded {x. \<not> (f x = x \<and> g x = x)}"
  1.4430 +    using insert.IH [OF xyS pw] insert.prems by (blast intro: that)
  1.4431 +  then have "\<exists>f g. homeomorphism T T f g \<and> (\<forall>i \<in> K. f(x i) = y i) \<and>
  1.4432 +                   {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
  1.4433 +    using insert by blast
  1.4434 +  have aff_eq: "affine hull (S - y ` K) = affine hull S"
  1.4435 +    apply (rule affine_hull_Diff)
  1.4436 +    apply (auto simp: insert)
  1.4437 +    using \<open>y i \<in> S\<close> insert.hyps(2) xney xyS by fastforce
  1.4438 +  have f_in_S: "f x \<in> S" if "x \<in> S" for x
  1.4439 +    using homfg fg_sub homeomorphism_apply1 \<open>S \<subseteq> T\<close>
  1.4440 +  proof -
  1.4441 +    have "(f (f x) \<noteq> f x \<or> g (f x) \<noteq> f x) \<or> f x \<in> S"
  1.4442 +      by (metis \<open>S \<subseteq> T\<close> homfg subsetD homeomorphism_apply1 that)
  1.4443 +    then show ?thesis
  1.4444 +      using fg_sub by force
  1.4445 +  qed
  1.4446 +  obtain h k where homhk: "homeomorphism T T h k" and heq: "h (f (x i)) = y i"
  1.4447 +               and hk_sub: "{x. \<not> (h x = x \<and> k x = x)} \<subseteq> S - y ` K"
  1.4448 +               and bo_hk:  "bounded {x. \<not> (h x = x \<and> k x = x)}"
  1.4449 +  proof (rule homeomorphism_moving_point [of "S - y`K" T "f(x i)" "y i"])
  1.4450 +    show "openin (subtopology euclidean (affine hull (S - y ` K))) (S - y ` K)"
  1.4451 +      by (simp add: aff_eq openin_diff finite_imp_closedin image_subset_iff hull_inc insert xyS)
  1.4452 +    show "S - y ` K \<subseteq> T"
  1.4453 +      using \<open>S \<subseteq> T\<close> by auto
  1.4454 +    show "T \<subseteq> affine hull (S - y ` K)"
  1.4455 +      using insert by (simp add: aff_eq)
  1.4456 +    show "connected (S - y ` K)"
  1.4457 +    proof (rule connected_openin_diff_countable [OF \<open>connected S\<close> ope])
  1.4458 +      show "\<not> collinear S"
  1.4459 +        using collinear_aff_dim \<open>2 \<le> aff_dim S\<close> by force
  1.4460 +      show "countable (y ` K)"
  1.4461 +        using countable_finite insert.hyps(1) by blast
  1.4462 +    qed
  1.4463 +    show "f (x i) \<in> S - y ` K"
  1.4464 +      apply (auto simp: f_in_S \<open>x i \<in> S\<close>)
  1.4465 +        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)
  1.4466 +    show "y i \<in> S - y ` K"
  1.4467 +      using insert.hyps xney by (auto simp: \<open>y i \<in> S\<close>)
  1.4468 +  qed blast
  1.4469 +  show ?case
  1.4470 +  proof (intro exI conjI)
  1.4471 +    show "homeomorphism T T (h \<circ> f) (g \<circ> k)"
  1.4472 +      using homfg homhk homeomorphism_compose by blast
  1.4473 +    show "\<forall>i \<in> insert i K. (h \<circ> f) (x i) = y i"
  1.4474 +      using feq hk_sub by (auto simp: heq)
  1.4475 +    show "{x. \<not> ((h \<circ> f) x = x \<and> (g \<circ> k) x = x)} \<subseteq> S"
  1.4476 +      using fg_sub hk_sub by force
  1.4477 +    have "bounded ({x. \<not>(f x = x \<and> g x = x)} \<union> {x. \<not>(h x = x \<and> k x = x)})"
  1.4478 +      using bo_fg bo_hk bounded_Un by blast
  1.4479 +    then show "bounded {x. \<not> ((h \<circ> f) x = x \<and> (g \<circ> k) x = x)}"
  1.4480 +      by (rule bounded_subset) auto
  1.4481 +  qed
  1.4482 +qed
  1.4483 +
  1.4484 +proposition%unimportant homeomorphism_moving_points_exists:
  1.4485 +  fixes S :: "'a::euclidean_space set"
  1.4486 +  assumes 2: "2 \<le> DIM('a)" "open S" "connected S" "S \<subseteq> T" "finite K"
  1.4487 +      and KS: "\<And>i. i \<in> K \<Longrightarrow> x i \<in> S \<and> y i \<in> S"
  1.4488 +      and pw: "pairwise (\<lambda>i j. (x i \<noteq> x j) \<and> (y i \<noteq> y j)) K"
  1.4489 +      and S: "S \<subseteq> T" "T \<subseteq> affine hull S" "connected S"
  1.4490 +  obtains f g where "homeomorphism T T f g" "\<And>i. i \<in> K \<Longrightarrow> f(x i) = y i"
  1.4491 +                    "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S" "bounded {x. (\<not> (f x = x \<and> g x = x))}"
  1.4492 +proof (cases "S = {}")
  1.4493 +  case True
  1.4494 +  then show ?thesis
  1.4495 +    using KS homeomorphism_ident that by fastforce
  1.4496 +next
  1.4497 +  case False
  1.4498 +  then have affS: "affine hull S = UNIV"
  1.4499 +    by (simp add: affine_hull_open \<open>open S\<close>)
  1.4500 +  then have ope: "openin (subtopology euclidean (affine hull S)) S"
  1.4501 +    using \<open>open S\<close> open_openin by auto
  1.4502 +  have "2 \<le> DIM('a)" by (rule 2)
  1.4503 +  also have "\<dots> = aff_dim (UNIV :: 'a set)"
  1.4504 +    by simp
  1.4505 +  also have "\<dots> \<le> aff_dim S"
  1.4506 +    by (metis aff_dim_UNIV aff_dim_affine_hull aff_dim_le_DIM affS)
  1.4507 +  finally have "2 \<le> aff_dim S"
  1.4508 +    by linarith
  1.4509 +  then show ?thesis
  1.4510 +    using homeomorphism_moving_points_exists_gen [OF \<open>finite K\<close> KS pw _ ope S] that by fastforce
  1.4511 +qed
  1.4512 +
  1.4513 +subsubsection%unimportant\<open>The theorem \<open>homeomorphism_grouping_points_exists\<close>\<close>
  1.4514 +
  1.4515 +lemma homeomorphism_grouping_point_1:
  1.4516 +  fixes a::real and c::real
  1.4517 +  assumes "a < b" "c < d"
  1.4518 +  obtains f g where "homeomorphism (cbox a b) (cbox c d) f g" "f a = c" "f b = d"
  1.4519 +proof -
  1.4520 +  define f where "f \<equiv> \<lambda>x. ((d - c) / (b - a)) * x + (c - a * ((d - c) / (b - a)))"
  1.4521 +  have "\<exists>g. homeomorphism (cbox a b) (cbox c d) f g"
  1.4522 +  proof (rule homeomorphism_compact)
  1.4523 +    show "continuous_on (cbox a b) f"
  1.4524 +      apply (simp add: f_def)
  1.4525 +      apply (intro continuous_intros)
  1.4526 +      using assms by auto
  1.4527 +    have "f ` {a..b} = {c..d}"
  1.4528 +      unfolding f_def image_affinity_atLeastAtMost
  1.4529 +      using assms sum_sqs_eq by (auto simp: divide_simps algebra_simps)
  1.4530 +    then show "f ` cbox a b = cbox c d"
  1.4531 +      by auto
  1.4532 +    show "inj_on f (cbox a b)"
  1.4533 +      unfolding f_def inj_on_def using assms by auto
  1.4534 +  qed auto
  1.4535 +  then obtain g where "homeomorphism (cbox a b) (cbox c d) f g" ..
  1.4536 +  then show ?thesis
  1.4537 +  proof
  1.4538 +    show "f a = c"
  1.4539 +      by (simp add: f_def)
  1.4540 +    show "f b = d"
  1.4541 +      using assms sum_sqs_eq [of a b] by (auto simp: f_def divide_simps algebra_simps)
  1.4542 +  qed
  1.4543 +qed
  1.4544 +
  1.4545 +lemma homeomorphism_grouping_point_2:
  1.4546 +  fixes a::real and w::real
  1.4547 +  assumes hom_ab: "homeomorphism (cbox a b) (cbox u v) f1 g1"
  1.4548 +      and hom_bc: "homeomorphism (cbox b c) (cbox v w) f2 g2"
  1.4549 +      and "b \<in> cbox a c" "v \<in> cbox u w"
  1.4550 +      and eq: "f1 a = u" "f1 b = v" "f2 b = v" "f2 c = w"
  1.4551 + obtains f g where "homeomorphism (cbox a c) (cbox u w) f g" "f a = u" "f c = w"
  1.4552 +                   "\<And>x. x \<in> cbox a b \<Longrightarrow> f x = f1 x" "\<And>x. x \<in> cbox b c \<Longrightarrow> f x = f2 x"
  1.4553 +proof -
  1.4554 +  have le: "a \<le> b" "b \<le> c" "u \<le> v" "v \<le> w"
  1.4555 +    using assms by simp_all
  1.4556 +  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"
  1.4557 +    by auto
  1.4558 +  define f where "f \<equiv> \<lambda>x. if x \<le> b then f1 x else f2 x"
  1.4559 +  have "\<exists>g. homeomorphism (cbox a c) (cbox u w) f g"
  1.4560 +  proof (rule homeomorphism_compact)
  1.4561 +    have cf1: "continuous_on (cbox a b) f1"
  1.4562 +      using hom_ab homeomorphism_cont1 by blast
  1.4563 +    have cf2: "continuous_on (cbox b c) f2"
  1.4564 +      using hom_bc homeomorphism_cont1 by blast
  1.4565 +    show "continuous_on (cbox a c) f"
  1.4566 +      apply (simp add: f_def)
  1.4567 +      apply (rule continuous_on_cases_le [OF continuous_on_subset [OF cf1] continuous_on_subset [OF cf2]])
  1.4568 +      using le eq apply (force simp: continuous_on_id)+
  1.4569 +      done
  1.4570 +    have "f ` cbox a b = f1 ` cbox a b" "f ` cbox b c = f2 ` cbox b c"
  1.4571 +      unfolding f_def using eq by force+
  1.4572 +    then show "f ` cbox a c = cbox u w"
  1.4573 +      apply (simp only: ac uw image_Un)
  1.4574 +      by (metis hom_ab hom_bc homeomorphism_def)
  1.4575 +    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
  1.4576 +    proof -
  1.4577 +      have "f1 x \<in> cbox u v"
  1.4578 +        by (metis hom_ab homeomorphism_def image_eqI mem_box_real(2) x)
  1.4579 +      moreover have "f2 y \<in> cbox v w"
  1.4580 +        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)
  1.4581 +      moreover have "f2 y \<noteq> f2 b"
  1.4582 +        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)
  1.4583 +      ultimately show ?thesis
  1.4584 +        using le eq by simp
  1.4585 +    qed
  1.4586 +    have "inj_on f1 (cbox a b)"
  1.4587 +      by (metis (full_types) hom_ab homeomorphism_def inj_onI)
  1.4588 +    moreover have "inj_on f2 (cbox b c)"
  1.4589 +      by (metis (full_types) hom_bc homeomorphism_def inj_onI)
  1.4590 +    ultimately show "inj_on f (cbox a c)"
  1.4591 +      apply (simp (no_asm) add: inj_on_def)
  1.4592 +      apply (simp add: f_def inj_on_eq_iff)
  1.4593 +      using neq12  apply force
  1.4594 +      done
  1.4595 +  qed auto
  1.4596 +  then obtain g where "homeomorphism (cbox a c) (cbox u w) f g" ..
  1.4597 +  then show ?thesis
  1.4598 +    apply (rule that)
  1.4599 +    using eq le by (auto simp: f_def)
  1.4600 +qed
  1.4601 +
  1.4602 +lemma homeomorphism_grouping_point_3:
  1.4603 +  fixes a::real
  1.4604 +  assumes cbox_sub: "cbox c d \<subseteq> box a b" "cbox u v \<subseteq> box a b"
  1.4605 +      and box_ne: "box c d \<noteq> {}" "box u v \<noteq> {}"
  1.4606 +  obtains f g where "homeomorphism (cbox a b) (cbox a b) f g" "f a = a" "f b = b"
  1.4607 +                    "\<And>x. x \<in> cbox c d \<Longrightarrow> f x \<in> cbox u v"
  1.4608 +proof -
  1.4609 +  have less: "a < c" "a < u" "d < b" "v < b" "c < d" "u < v" "cbox c d \<noteq> {}"
  1.4610 +    using assms
  1.4611 +    by (simp_all add: cbox_sub subset_eq)
  1.4612 +  obtain f1 g1 where 1: "homeomorphism (cbox a c) (cbox a u) f1 g1"
  1.4613 +                   and f1_eq: "f1 a = a" "f1 c = u"
  1.4614 +    using homeomorphism_grouping_point_1 [OF \<open>a < c\<close> \<open>a < u\<close>] .
  1.4615 +  obtain f2 g2 where 2: "homeomorphism (cbox c d) (cbox u v) f2 g2"
  1.4616 +                   and f2_eq: "f2 c = u" "f2 d = v"
  1.4617 +    using homeomorphism_grouping_point_1 [OF \<open>c < d\<close> \<open>u < v\<close>] .
  1.4618 +  obtain f3 g3 where 3: "homeomorphism (cbox d b) (cbox v b) f3 g3"
  1.4619 +                   and f3_eq: "f3 d = v" "f3 b = b"
  1.4620 +    using homeomorphism_grouping_point_1 [OF \<open>d < b\<close> \<open>v < b\<close>] .
  1.4621 +  obtain f4 g4 where 4: "homeomorphism (cbox a d) (cbox a v) f4 g4" and "f4 a = a" "f4 d = v"
  1.4622 +                 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"
  1.4623 +    using homeomorphism_grouping_point_2 [OF 1 2] less  by (auto simp: f1_eq f2_eq)
  1.4624 +  obtain f g where fg: "homeomorphism (cbox a b) (cbox a b) f g" "f a = a" "f b = b"
  1.4625 +               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"
  1.4626 +    using homeomorphism_grouping_point_2 [OF 4 3] less by (auto simp: f4_eq f3_eq f2_eq f1_eq)
  1.4627 +  show ?thesis
  1.4628 +    apply (rule that [OF fg])
  1.4629 +    using f4_eq f_eq homeomorphism_image1 [OF 2]
  1.4630 +    apply simp
  1.4631 +    by (metis atLeastAtMost_iff box_real(1) box_real(2) cbox_sub(1) greaterThanLessThan_iff imageI less_eq_real_def subset_eq)
  1.4632 +qed
  1.4633 +
  1.4634 +
  1.4635 +lemma homeomorphism_grouping_point_4:
  1.4636 +  fixes T :: "real set"
  1.4637 +  assumes "open U" "open S" "connected S" "U \<noteq> {}" "finite K" "K \<subseteq> S" "U \<subseteq> S" "S \<subseteq> T"
  1.4638 +  obtains f g where "homeomorphism T T f g"
  1.4639 +                    "\<And>x. x \<in> K \<Longrightarrow> f x \<in> U" "{x. (\<not> (f x = x \<and> g x = x))} \<subseteq> S"
  1.4640 +                    "bounded {x. (\<not> (f x = x \<and> g x = x))}"
  1.4641 +proof -
  1.4642 +  obtain c d where "box c d \<noteq> {}" "cbox c d \<subseteq> U"
  1.4643 +  proof -
  1.4644 +    obtain u where "u \<in> U"
  1.4645 +      using \<open>U \<noteq> {}\<close> by blast
  1.4646 +    then obtain e where "e > 0" "cball u e \<subseteq> U"
  1.4647 +      using \<open>open U\<close> open_contains_cball by blast
  1.4648 +    then show ?thesis
  1.4649 +      by (rule_tac c=u and d="u+e" in that) (auto simp: dist_norm subset_iff)
  1.4650 +  qed
  1.4651 +  have "compact K"
  1.4652 +    by (simp add: \<open>finite K\<close> finite_imp_compact)
  1.4653 +  obtain a b where "box a b \<noteq> {}" "K \<subseteq> cbox a b" "cbox a b \<subseteq> S"
  1.4654 +  proof (cases "K = {}")
  1.4655 +    case True then show ?thesis
  1.4656 +      using \<open>box c d \<noteq> {}\<close> \<open>cbox c d \<subseteq> U\<close> \<open>U \<subseteq> S\<close> that by blast
  1.4657 +  next
  1.4658 +    case False
  1.4659 +    then obtain a b where "a \<in> K" "b \<in> K"
  1.4660 +            and a: "\<And>x. x \<in> K \<Longrightarrow> a \<le> x" and b: "\<And>x. x \<in> K \<Longrightarrow> x \<le> b"
  1.4661 +      using compact_attains_inf compact_attains_sup by (metis \<open>compact K\<close>)+
  1.4662 +    obtain e where "e > 0" "cball b e \<subseteq> S"
  1.4663 +      using \<open>open S\<close> open_contains_cball
  1.4664 +      by (metis \<open>b \<in> K\<close> \<open>K \<subseteq> S\<close> subsetD)
  1.4665 +    show ?thesis
  1.4666 +    proof
  1.4667 +      show "box a (b + e) \<noteq> {}"
  1.4668 +        using \<open>0 < e\<close> \<open>b \<in> K\<close> a by force
  1.4669 +      show "K \<subseteq> cbox a (b + e)"
  1.4670 +        using \<open>0 < e\<close> a b by fastforce
  1.4671 +      have "a \<in> S"
  1.4672 +        using \<open>a \<in> K\<close> assms(6) by blast
  1.4673 +      have "b + e \<in> S"
  1.4674 +        using \<open>0 < e\<close> \<open>cball b e \<subseteq> S\<close>  by (force simp: dist_norm)
  1.4675 +      show "cbox a (b + e) \<subseteq> S"
  1.4676 +        using \<open>a \<in> S\<close> \<open>b + e \<in> S\<close> \<open>connected S\<close> connected_contains_Icc by auto
  1.4677 +    qed
  1.4678 +  qed
  1.4679 +  obtain w z where "cbox w z \<subseteq> S" and sub_wz: "cbox a b \<union> cbox c d \<subseteq> box w z"
  1.4680 +  proof -
  1.4681 +    have "a \<in> S" "b \<in> S"
  1.4682 +      using \<open>box a b \<noteq> {}\<close> \<open>cbox a b \<subseteq> S\<close> by auto
  1.4683 +    moreover have "c \<in> S" "d \<in> S"
  1.4684 +      using \<open>box c d \<noteq> {}\<close> \<open>cbox c d \<subseteq> U\<close> \<open>U \<subseteq> S\<close> by force+
  1.4685 +    ultimately have "min a c \<in> S" "max b d \<in> S"
  1.4686 +      by linarith+
  1.4687 +    then obtain e1 e2 where "e1 > 0" "cball (min a c) e1 \<subseteq> S" "e2 > 0" "cball (max b d) e2 \<subseteq> S"
  1.4688 +      using \<open>open S\<close> open_contains_cball by metis
  1.4689 +    then have *: "min a c - e1 \<in> S" "max b d + e2 \<in> S"
  1.4690 +      by (auto simp: dist_norm)
  1.4691 +    show ?thesis
  1.4692 +    proof
  1.4693 +      show "cbox (min a c - e1) (max b d+ e2) \<subseteq> S"
  1.4694 +        using * \<open>connected S\<close> connected_contains_Icc by auto
  1.4695 +      show "cbox a b \<union> cbox c d \<subseteq> box (min a c - e1) (max b d + e2)"
  1.4696 +        using \<open>0 < e1\<close> \<open>0 < e2\<close> by auto
  1.4697 +    qed
  1.4698 +  qed
  1.4699 +  then
  1.4700 +  obtain f g where hom: "homeomorphism (cbox w z) (cbox w z) f g"
  1.4701 +               and "f w = w" "f z = z"
  1.4702 +               and fin: "\<And>x. x \<in> cbox a b \<Longrightarrow> f x \<in> cbox c d"
  1.4703 +    using homeomorphism_grouping_point_3 [of a b w z c d]
  1.4704 +    using \<open>box a b \<noteq> {}\<close> \<open>box c d \<noteq> {}\<close> by blast
  1.4705 +  have contfg: "continuous_on (cbox w z) f" "continuous_on (cbox w z) g"
  1.4706 +    using hom homeomorphism_def by blast+
  1.4707 +  define f' where "f' \<equiv> \<lambda>x. if x \<in> cbox w z then f x else x"
  1.4708 +  define g' where "g' \<equiv> \<lambda>x. if x \<in> cbox w z then g x else x"
  1.4709 +  show ?thesis
  1.4710 +  proof
  1.4711 +    have T: "cbox w z \<union> (T - box w z) = T"
  1.4712 +      using \<open>cbox w z \<subseteq> S\<close> \<open>S \<subseteq> T\<close> by auto
  1.4713 +    show "homeomorphism T T f' g'"
  1.4714 +    proof
  1.4715 +      have clo: "closedin (subtopology euclidean (cbox w z \<union> (T - box w z))) (T - box w z)"
  1.4716 +        by (metis Diff_Diff_Int Diff_subset T closedin_def open_box openin_open_Int topspace_euclidean_subtopology)
  1.4717 +      have "continuous_on (cbox w z \<union> (T - box w z)) f'" "continuous_on (cbox w z \<union> (T - box w z)) g'"
  1.4718 +        unfolding f'_def g'_def
  1.4719 +         apply (safe intro!: continuous_on_cases_local contfg continuous_on_id clo)
  1.4720 +         apply (simp_all add: closed_subset)
  1.4721 +        using \<open>f w = w\<close> \<open>f z = z\<close> apply force
  1.4722 +        by (metis \<open>f w = w\<close> \<open>f z = z\<close> hom homeomorphism_def less_eq_real_def mem_box_real(2))
  1.4723 +      then show "continuous_on T f'" "continuous_on T g'"
  1.4724 +        by (simp_all only: T)
  1.4725 +      show "f' ` T \<subseteq> T"
  1.4726 +        unfolding f'_def
  1.4727 +        by clarsimp (metis \<open>cbox w z \<subseteq> S\<close> \<open>S \<subseteq> T\<close> subsetD hom homeomorphism_def imageI mem_box_real(2))
  1.4728 +      show "g' ` T \<subseteq> T"
  1.4729 +        unfolding g'_def
  1.4730 +        by clarsimp (metis \<open>cbox w z \<subseteq> S\<close> \<open>S \<subseteq> T\<close> subsetD hom homeomorphism_def imageI mem_box_real(2))
  1.4731 +      show "\<And>x. x \<in> T \<Longrightarrow> g' (f' x) = x"
  1.4732 +        unfolding f'_def g'_def
  1.4733 +        using homeomorphism_apply1 [OF hom]  homeomorphism_image1 [OF hom] by fastforce
  1.4734 +      show "\<And>y. y \<in> T \<Longrightarrow> f' (g' y) = y"
  1.4735 +        unfolding f'_def g'_def
  1.4736 +        using homeomorphism_apply2 [OF hom]  homeomorphism_image2 [OF hom] by fastforce
  1.4737 +    qed
  1.4738 +    show "\<And>x. x \<in> K \<Longrightarrow> f' x \<in> U"
  1.4739 +      using fin sub_wz \<open>K \<subseteq> cbox a b\<close> \<open>cbox c d \<subseteq> U\<close> by (force simp: f'_def)
  1.4740 +    show "{x. \<not> (f' x = x \<and> g' x = x)} \<subseteq> S"
  1.4741 +      using \<open>cbox w z \<subseteq> S\<close> by (auto simp: f'_def g'_def)
  1.4742 +    show "bounded {x. \<not> (f' x = x \<and> g' x = x)}"
  1.4743 +      apply (rule bounded_subset [of "cbox w z"])
  1.4744 +      using bounded_cbox apply blast
  1.4745 +      apply (auto simp: f'_def g'_def)
  1.4746 +      done
  1.4747 +  qed
  1.4748 +qed
  1.4749 +
  1.4750 +proposition%unimportant homeomorphism_grouping_points_exists:
  1.4751 +  fixes S :: "'a::euclidean_space set"
  1.4752 +  assumes "open U" "open S" "connected S" "U \<noteq> {}" "finite K" "K \<subseteq> S" "U \<subseteq> S" "S \<subseteq> T"
  1.4753 +  obtains f g where "homeomorphism T T f g" "{x. (\<not> (f x = x \<and> g x = x))} \<subseteq> S"
  1.4754 +                    "bounded {x. (\<not> (f x = x \<and> g x = x))}" "\<And>x. x \<in> K \<Longrightarrow> f x \<in> U"
  1.4755 +proof (cases "2 \<le> DIM('a)")
  1.4756 +  case True
  1.4757 +  have TS: "T \<subseteq> affine hull S"
  1.4758 +    using affine_hull_open assms by blast
  1.4759 +  have "infinite U"
  1.4760 +    using \<open>open U\<close> \<open>U \<noteq> {}\<close> finite_imp_not_open by blast
  1.4761 +  then obtain P where "P \<subseteq> U" "finite P" "card K = card P"
  1.4762 +    using infinite_arbitrarily_large by metis
  1.4763 +  then obtain \<gamma> where \<gamma>: "bij_betw \<gamma> K P"
  1.4764 +    using \<open>finite K\<close> finite_same_card_bij by blast
  1.4765 +  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)}"
  1.4766 +  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>])
  1.4767 +    show "\<And>i. i \<in> K \<Longrightarrow> id i \<in> S \<and> \<gamma> i \<in> S"
  1.4768 +      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
  1.4769 +    show "pairwise (\<lambda>i j. id i \<noteq> id j \<and> \<gamma> i \<noteq> \<gamma> j) K"
  1.4770 +      using \<gamma> by (auto simp: pairwise_def bij_betw_def inj_on_def)
  1.4771 +  qed (use affine_hull_open assms that in auto)
  1.4772 +  then show ?thesis
  1.4773 +    using \<gamma> \<open>P \<subseteq> U\<close> bij_betwE by (fastforce simp add: intro!: that)
  1.4774 +next
  1.4775 +  case False
  1.4776 +  with DIM_positive have "DIM('a) = 1"
  1.4777 +    by (simp add: dual_order.antisym)
  1.4778 +  then obtain h::"'a \<Rightarrow>real" and j
  1.4779 +  where "linear h" "linear j"
  1.4780 +    and noh: "\<And>x. norm(h x) = norm x" and noj: "\<And>y. norm(j y) = norm y"
  1.4781 +    and hj:  "\<And>x. j(h x) = x" "\<And>y. h(j y) = y"
  1.4782 +    and ranh: "surj h"
  1.4783 +    using isomorphisms_UNIV_UNIV
  1.4784 +    by (metis (mono_tags, hide_lams) DIM_real UNIV_eq_I range_eqI)
  1.4785 +  obtain f g where hom: "homeomorphism (h ` T) (h ` T) f g"
  1.4786 +               and f: "\<And>x. x \<in> h ` K \<Longrightarrow> f x \<in> h ` U"
  1.4787 +               and sub: "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> h ` S"
  1.4788 +               and bou: "bounded {x. \<not> (f x = x \<and> g x = x)}"
  1.4789 +    apply (rule homeomorphism_grouping_point_4 [of "h ` U" "h ` S" "h ` K" "h ` T"])
  1.4790 +    by (simp_all add: assms image_mono  \<open>linear h\<close> open_surjective_linear_image connected_linear_image ranh)
  1.4791 +  have jf: "j (f (h x)) = x \<longleftrightarrow> f (h x) = h x" for x
  1.4792 +    by (metis hj)
  1.4793 +  have jg: "j (g (h x)) = x \<longleftrightarrow> g (h x) = h x" for x
  1.4794 +    by (metis hj)
  1.4795 +  have cont_hj: "continuous_on X h"  "continuous_on Y j" for X Y
  1.4796 +    by (simp_all add: \<open>linear h\<close> \<open>linear j\<close> linear_linear linear_continuous_on)
  1.4797 +  show ?thesis
  1.4798 +  proof
  1.4799 +    show "homeomorphism T T (j \<circ> f \<circ> h) (j \<circ> g \<circ> h)"
  1.4800 +    proof
  1.4801 +      show "continuous_on T (j \<circ> f \<circ> h)" "continuous_on T (j \<circ> g \<circ> h)"
  1.4802 +        using hom homeomorphism_def
  1.4803 +        by (blast intro: continuous_on_compose cont_hj)+
  1.4804 +      show "(j \<circ> f \<circ> h) ` T \<subseteq> T" "(j \<circ> g \<circ> h) ` T \<subseteq> T"
  1.4805 +        by auto (metis (mono_tags, hide_lams) hj(1) hom homeomorphism_def imageE imageI)+
  1.4806 +      show "\<And>x. x \<in> T \<Longrightarrow> (j \<circ> g \<circ> h) ((j \<circ> f \<circ> h) x) = x"
  1.4807 +        using hj hom homeomorphism_apply1 by fastforce
  1.4808 +      show "\<And>y. y \<in> T \<Longrightarrow> (j \<circ> f \<circ> h) ((j \<circ> g \<circ> h) y) = y"
  1.4809 +        using hj hom homeomorphism_apply2 by fastforce
  1.4810 +    qed
  1.4811 +    show "{x. \<not> ((j \<circ> f \<circ> h) x = x \<and> (j \<circ> g \<circ> h) x = x)} \<subseteq> S"
  1.4812 +      apply (clarsimp simp: jf jg hj)
  1.4813 +      using sub hj
  1.4814 +      apply (drule_tac c="h x" in subsetD, force)
  1.4815 +      by (metis imageE)
  1.4816 +    have "bounded (j ` {x. (\<not> (f x = x \<and> g x = x))})"
  1.4817 +      by (rule bounded_linear_image [OF bou]) (use \<open>linear j\<close> linear_conv_bounded_linear in auto)
  1.4818 +    moreover
  1.4819 +    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))}"
  1.4820 +      using hj by (auto simp: jf jg image_iff, metis+)
  1.4821 +    ultimately show "bounded {x. \<not> ((j \<circ> f \<circ> h) x = x \<and> (j \<circ> g \<circ> h) x = x)}"
  1.4822 +      by metis
  1.4823 +    show "\<And>x. x \<in> K \<Longrightarrow> (j \<circ> f \<circ> h) x \<in> U"
  1.4824 +      using f hj by fastforce
  1.4825 +  qed
  1.4826 +qed
  1.4827 +
  1.4828 +
  1.4829 +proposition%unimportant homeomorphism_grouping_points_exists_gen:
  1.4830 +  fixes S :: "'a::euclidean_space set"
  1.4831 +  assumes opeU: "openin (subtopology euclidean S) U"
  1.4832 +      and opeS: "openin (subtopology euclidean (affine hull S)) S"
  1.4833 +      and "U \<noteq> {}" "finite K" "K \<subseteq> S" and S: "S \<subseteq> T" "T \<subseteq> affine hull S" "connected S"
  1.4834 +  obtains f g where "homeomorphism T T f g" "{x. (\<not> (f x = x \<and> g x = x))} \<subseteq> S"
  1.4835 +                    "bounded {x. (\<not> (f x = x \<and> g x = x))}" "\<And>x. x \<in> K \<Longrightarrow> f x \<in> U"
  1.4836 +proof (cases "2 \<le> aff_dim S")
  1.4837 +  case True
  1.4838 +  have opeU': "openin (subtopology euclidean (affine hull S)) U"
  1.4839 +    using opeS opeU openin_trans by blast
  1.4840 +  obtain u where "u \<in> U" "u \<in> S"
  1.4841 +    using \<open>U \<noteq> {}\<close> opeU openin_imp_subset by fastforce+
  1.4842 +  have "infinite U"
  1.4843 +    apply (rule infinite_openin [OF opeU \<open>u \<in> U\<close>])
  1.4844 +    apply (rule connected_imp_perfect_aff_dim [OF \<open>connected S\<close> _ \<open>u \<in> S\<close>])
  1.4845 +    using True apply simp
  1.4846 +    done
  1.4847 +  then obtain P where "P \<subseteq> U" "finite P" "card K = card P"
  1.4848 +    using infinite_arbitrarily_large by metis
  1.4849 +  then obtain \<gamma> where \<gamma>: "bij_betw \<gamma> K P"
  1.4850 +    using \<open>finite K\<close> finite_same_card_bij by blast
  1.4851 +  have "\<exists>f g. homeomorphism T T f g \<and> (\<forall>i \<in> K. f(id i) = \<gamma> i) \<and>
  1.4852 +               {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
  1.4853 +  proof (rule homeomorphism_moving_points_exists_gen [OF \<open>finite K\<close> _ _ True opeS S])
  1.4854 +    show "\<And>i. i \<in> K \<Longrightarrow> id i \<in> S \<and> \<gamma> i \<in> S"
  1.4855 +      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)
  1.4856 +    show "pairwise (\<lambda>i j. id i \<noteq> id j \<and> \<gamma> i \<noteq> \<gamma> j) K"
  1.4857 +      using \<gamma> by (auto simp: pairwise_def bij_betw_def inj_on_def)
  1.4858 +  qed
  1.4859 +  then show ?thesis
  1.4860 +    using \<gamma> \<open>P \<subseteq> U\<close> bij_betwE by (fastforce simp add: intro!: that)
  1.4861 +next
  1.4862 +  case False
  1.4863 +  with aff_dim_geq [of S] consider "aff_dim S = -1" | "aff_dim S = 0" | "aff_dim S = 1" by linarith
  1.4864 +  then show ?thesis
  1.4865 +  proof cases
  1.4866 +    assume "aff_dim S = -1"
  1.4867 +    then have "S = {}"
  1.4868 +      using aff_dim_empty by blast
  1.4869 +    then have "False"
  1.4870 +      using \<open>U \<noteq> {}\<close> \<open>K \<subseteq> S\<close> openin_imp_subset [OF opeU] by blast
  1.4871 +    then show ?thesis ..
  1.4872 +  next
  1.4873 +    assume "aff_dim S = 0"
  1.4874 +    then obtain a where "S = {a}"
  1.4875 +      using aff_dim_eq_0 by blast
  1.4876 +    then have "K \<subseteq> U"
  1.4877 +      using \<open>U \<noteq> {}\<close> \<open>K \<subseteq> S\<close> openin_imp_subset [OF opeU] by blast
  1.4878 +    show ?thesis
  1.4879 +      apply (rule that [of id id])
  1.4880 +      using \<open>K \<subseteq> U\<close> by (auto simp: continuous_on_id intro: homeomorphismI)
  1.4881 +  next
  1.4882 +    assume "aff_dim S = 1"
  1.4883 +    then have "affine hull S homeomorphic (UNIV :: real set)"
  1.4884 +      by (auto simp: homeomorphic_affine_sets)
  1.4885 +    then obtain h::"'a\<Rightarrow>real" and j where homhj: "homeomorphism (affine hull S) UNIV h j"
  1.4886 +      using homeomorphic_def by blast
  1.4887 +    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"
  1.4888 +      by (auto simp: homeomorphism_def)
  1.4889 +    have connh: "connected (h ` S)"
  1.4890 +      by (meson Topological_Spaces.connected_continuous_image \<open>connected S\<close> homeomorphism_cont1 homeomorphism_of_subsets homhj hull_subset top_greatest)
  1.4891 +    have hUS: "h ` U \<subseteq> h ` S"
  1.4892 +      by (meson homeomorphism_imp_open_map homeomorphism_of_subsets homhj hull_subset opeS opeU open_UNIV openin_open_eq)
  1.4893 +    have opn: "openin (subtopology euclidean (affine hull S)) U \<Longrightarrow> open (h ` U)" for U
  1.4894 +      using homeomorphism_imp_open_map [OF homhj]  by simp
  1.4895 +    have "open (h ` U)" "open (h ` S)"
  1.4896 +      by (auto intro: opeS opeU openin_trans opn)
  1.4897 +    then obtain f g where hom: "homeomorphism (h ` T) (h ` T) f g"
  1.4898 +                 and f: "\<And>x. x \<in> h ` K \<Longrightarrow> f x \<in> h ` U"
  1.4899 +                 and sub: "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> h ` S"
  1.4900 +                 and bou: "bounded {x. \<not> (f x = x \<and> g x = x)}"
  1.4901 +      apply (rule homeomorphism_grouping_points_exists [of "h ` U" "h ` S" "h ` K" "h ` T"])
  1.4902 +      using assms by (auto simp: connh hUS)
  1.4903 +    have jf: "\<And>x. x \<in> affine hull S \<Longrightarrow> j (f (h x)) = x \<longleftrightarrow> f (h x) = h x"
  1.4904 +      by (metis h j)
  1.4905 +    have jg: "\<And>x. x \<in> affine hull S \<Longrightarrow> j (g (h x)) = x \<longleftrightarrow> g (h x) = h x"
  1.4906 +      by (metis h j)
  1.4907 +    have cont_hj: "continuous_on T h"  "continuous_on Y j" for Y
  1.4908 +      apply (rule continuous_on_subset [OF _ \<open>T \<subseteq> affine hull S\<close>])
  1.4909 +      using homeomorphism_def homhj apply blast
  1.4910 +      by (meson continuous_on_subset homeomorphism_def homhj top_greatest)
  1.4911 +    define f' where "f' \<equiv> \<lambda>x. if x \<in> affine hull S then (j \<circ> f \<circ> h) x else x"
  1.4912 +    define g' where "g' \<equiv> \<lambda>x. if x \<in> affine hull S then (j \<circ> g \<circ> h) x else x"
  1.4913 +    show ?thesis
  1.4914 +    proof
  1.4915 +      show "homeomorphism T T f' g'"
  1.4916 +      proof
  1.4917 +        have "continuous_on T (j \<circ> f \<circ> h)"
  1.4918 +          apply (intro continuous_on_compose cont_hj)
  1.4919 +          using hom homeomorphism_def by blast
  1.4920 +        then show "continuous_on T f'"
  1.4921 +          apply (rule continuous_on_eq)
  1.4922 +          using \<open>T \<subseteq> affine hull S\<close> f'_def by auto
  1.4923 +        have "continuous_on T (j \<circ> g \<circ> h)"
  1.4924 +          apply (intro continuous_on_compose cont_hj)
  1.4925 +          using hom homeomorphism_def by blast
  1.4926 +        then show "continuous_on T g'"
  1.4927 +          apply (rule continuous_on_eq)
  1.4928 +          using \<open>T \<subseteq> affine hull S\<close> g'_def by auto
  1.4929 +        show "f' ` T \<subseteq> T"
  1.4930 +        proof (clarsimp simp: f'_def)
  1.4931 +          fix x assume "x \<in> T"
  1.4932 +          then have "f (h x) \<in> h ` T"
  1.4933 +            by (metis (no_types) hom homeomorphism_def image_subset_iff subset_refl)
  1.4934 +          then show "j (f (h x)) \<in> T"
  1.4935 +            using \<open>T \<subseteq> affine hull S\<close> h by auto
  1.4936 +        qed
  1.4937 +        show "g' ` T \<subseteq> T"
  1.4938 +        proof (clarsimp simp: g'_def)
  1.4939 +          fix x assume "x \<in> T"
  1.4940 +          then have "g (h x) \<in> h ` T"
  1.4941 +            by (metis (no_types) hom homeomorphism_def image_subset_iff subset_refl)
  1.4942 +          then show "j (g (h x)) \<in> T"
  1.4943 +            using \<open>T \<subseteq> affine hull S\<close> h by auto
  1.4944 +        qed
  1.4945 +        show "\<And>x. x \<in> T \<Longrightarrow> g' (f' x) = x"
  1.4946 +          using h j hom homeomorphism_apply1 by (fastforce simp add: f'_def g'_def)
  1.4947 +        show "\<And>y. y \<in> T \<Longrightarrow> f' (g' y) = y"
  1.4948 +          using h j hom homeomorphism_apply2 by (fastforce simp add: f'_def g'_def)
  1.4949 +      qed
  1.4950 +    next
  1.4951 +      show "{x. \<not> (f' x = x \<and> g' x = x)} \<subseteq> S"
  1.4952 +        apply (clarsimp simp: f'_def g'_def jf jg)
  1.4953 +        apply (rule imageE [OF subsetD [OF sub]], force)
  1.4954 +        by (metis h hull_inc)
  1.4955 +    next
  1.4956 +      have "compact (j ` closure {x. \<not> (f x = x \<and> g x = x)})"
  1.4957 +        using bou by (auto simp: compact_continuous_image cont_hj)
  1.4958 +      then have "bounded (j ` {x. \<not> (f x = x \<and> g x = x)})"
  1.4959 +        by (rule bounded_closure_image [OF compact_imp_bounded])
  1.4960 +      moreover
  1.4961 +      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))}"
  1.4962 +        using h j by (auto simp: image_iff; metis)
  1.4963 +      ultimately have "bounded {x \<in> affine hull S. j (f (h x)) \<noteq> x \<or> j (g (h x)) \<noteq> x}"
  1.4964 +        by metis
  1.4965 +      then show "bounded {x. \<not> (f' x = x \<and> g' x = x)}"
  1.4966 +        by (simp add: f'_def g'_def Collect_mono bounded_subset)
  1.4967 +    next
  1.4968 +      show "f' x \<in> U" if "x \<in> K" for x
  1.4969 +      proof -
  1.4970 +        have "U \<subseteq> S"
  1.4971 +          using opeU openin_imp_subset by blast
  1.4972 +        then have "j (f (h x)) \<in> U"
  1.4973 +          using f h hull_subset that by fastforce
  1.4974 +        then show "f' x \<in> U"
  1.4975 +          using \<open>K \<subseteq> S\<close> S f'_def that by auto
  1.4976 +      qed
  1.4977 +    qed
  1.4978 +  qed
  1.4979 +qed
  1.4980 +
  1.4981 +
  1.4982 +subsection\<open>Nullhomotopic mappings\<close>
  1.4983 +
  1.4984 +text\<open> A mapping out of a sphere is nullhomotopic iff it extends to the ball.
  1.4985 +This even works out in the degenerate cases when the radius is \<open>\<le>\<close> 0, and
  1.4986 +we also don't need to explicitly assume continuity since it's already implicit
  1.4987 +in both sides of the equivalence.\<close>
  1.4988 +
  1.4989 +lemma nullhomotopic_from_lemma:
  1.4990 +  assumes contg: "continuous_on (cball a r - {a}) g"
  1.4991 +      and fa: "\<And>e. 0 < e
  1.4992 +               \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>x. x \<noteq> a \<and> norm(x - a) < d \<longrightarrow> norm(g x - f a) < e)"
  1.4993 +      and r: "\<And>x. x \<in> cball a r \<and> x \<noteq> a \<Longrightarrow> f x = g x"
  1.4994 +    shows "continuous_on (cball a r) f"
  1.4995 +proof (clarsimp simp: continuous_on_eq_continuous_within Ball_def)
  1.4996 +  fix x
  1.4997 +  assume x: "dist a x \<le> r"
  1.4998 +  show "continuous (at x within cball a r) f"
  1.4999 +  proof (cases "x=a")
  1.5000 +    case True
  1.5001 +    then show ?thesis
  1.5002 +      by (metis continuous_within_eps_delta fa dist_norm dist_self r)
  1.5003 +  next
  1.5004 +    case False
  1.5005 +    show ?thesis
  1.5006 +    proof (rule continuous_transform_within [where f=g and d = "norm(x-a)"])
  1.5007 +      have "\<exists>d>0. \<forall>x'\<in>cball a r.
  1.5008 +                      dist x' x < d \<longrightarrow> dist (g x') (g x) < e" if "e>0" for e
  1.5009 +      proof -
  1.5010 +        obtain d where "d > 0"
  1.5011 +           and d: "\<And>x'. \<lbrakk>dist x' a \<le> r; x' \<noteq> a; dist x' x < d\<rbrakk> \<Longrightarrow>
  1.5012 +                                 dist (g x') (g x) < e"
  1.5013 +          using contg False x \<open>e>0\<close>
  1.5014 +          unfolding continuous_on_iff by (fastforce simp add: dist_commute intro: that)
  1.5015 +        show ?thesis
  1.5016 +          using \<open>d > 0\<close> \<open>x \<noteq> a\<close>
  1.5017 +          by (rule_tac x="min d (norm(x - a))" in exI)
  1.5018 +             (auto simp: dist_commute dist_norm [symmetric]  intro!: d)
  1.5019 +      qed
  1.5020 +      then show "continuous (at x within cball a r) g"
  1.5021 +        using contg False by (auto simp: continuous_within_eps_delta)
  1.5022 +      show "0 < norm (x - a)"
  1.5023 +        using False by force
  1.5024 +      show "x \<in> cball a r"
  1.5025 +        by (simp add: x)
  1.5026 +      show "\<And>x'. \<lbrakk>x' \<in> cball a r; dist x' x < norm (x - a)\<rbrakk>
  1.5027 +        \<Longrightarrow> g x' = f x'"
  1.5028 +        by (metis dist_commute dist_norm less_le r)
  1.5029 +    qed
  1.5030 +  qed
  1.5031 +qed
  1.5032 +
  1.5033 +proposition nullhomotopic_from_sphere_extension:
  1.5034 +  fixes f :: "'M::euclidean_space \<Rightarrow> 'a::real_normed_vector"
  1.5035 +  shows  "(\<exists>c. homotopic_with (\<lambda>x. True) (sphere a r) S f (\<lambda>x. c)) \<longleftrightarrow>
  1.5036 +          (\<exists>g. continuous_on (cball a r) g \<and> g ` (cball a r) \<subseteq> S \<and>
  1.5037 +               (\<forall>x \<in> sphere a r. g x = f x))"
  1.5038 +         (is "?lhs = ?rhs")
  1.5039 +proof (cases r "0::real" rule: linorder_cases)
  1.5040 +  case equal
  1.5041 +  then show ?thesis
  1.5042 +    apply (auto simp: homotopic_with)
  1.5043 +    apply (rule_tac x="\<lambda>x. h (0, a)" in exI)
  1.5044 +     apply (fastforce simp add:)
  1.5045 +    using continuous_on_const by blast
  1.5046 +next
  1.5047 +  case greater
  1.5048 +  let ?P = "continuous_on {x. norm(x - a) = r} f \<and> f ` {x. norm(x - a) = r} \<subseteq> S"
  1.5049 +  have ?P if ?lhs using that
  1.5050 +  proof
  1.5051 +    fix c
  1.5052 +    assume c: "homotopic_with (\<lambda>x. True) (sphere a r) S f (\<lambda>x. c)"
  1.5053 +    then have contf: "continuous_on (sphere a r) f" and fim: "f ` sphere a r \<subseteq> S"
  1.5054 +      by (auto simp: homotopic_with_imp_subset1 homotopic_with_imp_continuous)
  1.5055 +    show ?P
  1.5056 +      using contf fim by (auto simp: sphere_def dist_norm norm_minus_commute)
  1.5057 +  qed
  1.5058 +  moreover have ?P if ?rhs using that
  1.5059 +  proof
  1.5060 +    fix g
  1.5061 +    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)"
  1.5062 +    then
  1.5063 +    show ?P
  1.5064 +      apply (safe elim!: continuous_on_eq [OF continuous_on_subset])
  1.5065 +      apply (auto simp: dist_norm norm_minus_commute)
  1.5066 +      by (metis dist_norm image_subset_iff mem_sphere norm_minus_commute sphere_cball subsetCE)
  1.5067 +  qed
  1.5068 +  moreover have ?thesis if ?P
  1.5069 +  proof
  1.5070 +    assume ?lhs
  1.5071 +    then obtain c where "homotopic_with (\<lambda>x. True) (sphere a r) S (\<lambda>x. c) f"
  1.5072 +      using homotopic_with_sym by blast
  1.5073 +    then obtain h where conth: "continuous_on ({0..1::real} \<times> sphere a r) h"
  1.5074 +                    and him: "h ` ({0..1} \<times> sphere a r) \<subseteq> S"
  1.5075 +                    and h: "\<And>x. h(0, x) = c" "\<And>x. h(1, x) = f x"
  1.5076 +      by (auto simp: homotopic_with_def)
  1.5077 +    obtain b1::'M where "b1 \<in> Basis"
  1.5078 +      using SOME_Basis by auto
  1.5079 +    have "c \<in> S"
  1.5080 +      apply (rule him [THEN subsetD])
  1.5081 +      apply (rule_tac x = "(0, a + r *\<^sub>R b1)" in image_eqI)
  1.5082 +      using h greater \<open>b1 \<in> Basis\<close>
  1.5083 +       apply (auto simp: dist_norm)
  1.5084 +      done
  1.5085 +    have uconth: "uniformly_continuous_on ({0..1::real} \<times> (sphere a r)) h"
  1.5086 +      by (force intro: compact_Times conth compact_uniformly_continuous)
  1.5087 +    let ?g = "\<lambda>x. h (norm (x - a)/r,
  1.5088 +                     a + (if x = a then r *\<^sub>R b1 else (r / norm(x - a)) *\<^sub>R (x - a)))"
  1.5089 +    let ?g' = "\<lambda>x. h (norm (x - a)/r, a + (r / norm(x - a)) *\<^sub>R (x - a))"
  1.5090 +    show ?rhs
  1.5091 +    proof (intro exI conjI)
  1.5092 +      have "continuous_on (cball a r - {a}) ?g'"
  1.5093 +        apply (rule continuous_on_compose2 [OF conth])
  1.5094 +         apply (intro continuous_intros)
  1.5095 +        using greater apply (auto simp: dist_norm norm_minus_commute)
  1.5096 +        done
  1.5097 +      then show "continuous_on (cball a r) ?g"
  1.5098 +      proof (rule nullhomotopic_from_lemma)
  1.5099 +        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
  1.5100 +        proof -
  1.5101 +          obtain d where "0 < d"
  1.5102 +             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>
  1.5103 +                        \<Longrightarrow> dist (h x') (h x) < e"
  1.5104 +            using uniformly_continuous_onE [OF uconth \<open>0 < e\<close>] by auto
  1.5105 +          have *: "norm (h (norm (x - a) / r,
  1.5106 +                         a + (r / norm (x - a)) *\<^sub>R (x - a)) - h (0, a + r *\<^sub>R b1)) < e"
  1.5107 +                   if "x \<noteq> a" "norm (x - a) < r" "norm (x - a) < d * r" for x
  1.5108 +          proof -
  1.5109 +            have "norm (h (norm (x - a) / r, a + (r / norm (x - a)) *\<^sub>R (x - a)) - h (0, a + r *\<^sub>R b1)) =
  1.5110 +                  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)))"
  1.5111 +              by (simp add: h)
  1.5112 +            also have "\<dots> < e"
  1.5113 +              apply (rule d [unfolded dist_norm])
  1.5114 +              using greater \<open>0 < d\<close> \<open>b1 \<in> Basis\<close> that
  1.5115 +                by (auto simp: dist_norm divide_simps)
  1.5116 +            finally show ?thesis .
  1.5117 +          qed
  1.5118 +          show ?thesis
  1.5119 +            apply (rule_tac x = "min r (d * r)" in exI)
  1.5120 +            using greater \<open>0 < d\<close> by (auto simp: *)
  1.5121 +        qed
  1.5122 +        show "\<And>x. x \<in> cball a r \<and> x \<noteq> a \<Longrightarrow> ?g x = ?g' x"
  1.5123 +          by auto
  1.5124 +      qed
  1.5125 +    next
  1.5126 +      show "?g ` cball a r \<subseteq> S"
  1.5127 +        using greater him \<open>c \<in> S\<close>
  1.5128 +        by (force simp: h dist_norm norm_minus_commute)
  1.5129 +    next
  1.5130 +      show "\<forall>x\<in>sphere a r. ?g x = f x"
  1.5131 +        using greater by (auto simp: h dist_norm norm_minus_commute)
  1.5132 +    qed
  1.5133 +  next
  1.5134 +    assume ?rhs
  1.5135 +    then obtain g where contg: "continuous_on (cball a r) g"
  1.5136 +                    and gim: "g ` cball a r \<subseteq> S"
  1.5137 +                    and gf: "\<forall>x \<in> sphere a r. g x = f x"
  1.5138 +      by auto
  1.5139 +    let ?h = "\<lambda>y. g (a + (fst y) *\<^sub>R (snd y - a))"
  1.5140 +    have "continuous_on ({0..1} \<times> sphere a r) ?h"
  1.5141 +      apply (rule continuous_on_compose2 [OF contg])
  1.5142 +       apply (intro continuous_intros)
  1.5143 +      apply (auto simp: dist_norm norm_minus_commute mult_left_le_one_le)
  1.5144 +      done
  1.5145 +    moreover
  1.5146 +    have "?h ` ({0..1} \<times> sphere a r) \<subseteq> S"
  1.5147 +      by (auto simp: dist_norm norm_minus_commute mult_left_le_one_le gim [THEN subsetD])
  1.5148 +    moreover
  1.5149 +    have "\<forall>x\<in>sphere a r. ?h (0, x) = g a" "\<forall>x\<in>sphere a r. ?h (1, x) = f x"
  1.5150 +      by (auto simp: dist_norm norm_minus_commute mult_left_le_one_le gf)
  1.5151 +    ultimately
  1.5152 +    show ?lhs
  1.5153 +      apply (subst homotopic_with_sym)
  1.5154 +      apply (rule_tac x="g a" in exI)
  1.5155 +      apply (auto simp: homotopic_with)
  1.5156 +      done
  1.5157 +  qed
  1.5158 +  ultimately
  1.5159 +  show ?thesis by meson
  1.5160 +qed simp
  1.5161 +
  1.5162 +end
  1.5163 \ No newline at end of file