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