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