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