src/HOL/Multivariate_Analysis/Path_Connected.thy
author hoelzl
Fri Feb 19 13:40:50 2016 +0100 (2016-02-19)
changeset 62378 85ed00c1fe7c
parent 62087 44841d07ef1d
child 62381 a6479cb85944
child 62390 842917225d56
permissions -rw-r--r--
generalize more theorems to support enat and ennreal
     1 (*  Title:      HOL/Multivariate_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 Convex_Euclidean_Space
     9 begin
    10 
    11 subsection \<open>Paths and Arcs\<close>
    12 
    13 definition path :: "(real \<Rightarrow> 'a::topological_space) \<Rightarrow> bool"
    14   where "path g \<longleftrightarrow> continuous_on {0..1} g"
    15 
    16 definition pathstart :: "(real \<Rightarrow> 'a::topological_space) \<Rightarrow> 'a"
    17   where "pathstart g = g 0"
    18 
    19 definition pathfinish :: "(real \<Rightarrow> 'a::topological_space) \<Rightarrow> 'a"
    20   where "pathfinish g = g 1"
    21 
    22 definition path_image :: "(real \<Rightarrow> 'a::topological_space) \<Rightarrow> 'a set"
    23   where "path_image g = g ` {0 .. 1}"
    24 
    25 definition reversepath :: "(real \<Rightarrow> 'a::topological_space) \<Rightarrow> real \<Rightarrow> 'a"
    26   where "reversepath g = (\<lambda>x. g(1 - x))"
    27 
    28 definition joinpaths :: "(real \<Rightarrow> 'a::topological_space) \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> real \<Rightarrow> 'a"
    29     (infixr "+++" 75)
    30   where "g1 +++ g2 = (\<lambda>x. if x \<le> 1/2 then g1 (2 * x) else g2 (2 * x - 1))"
    31 
    32 definition simple_path :: "(real \<Rightarrow> 'a::topological_space) \<Rightarrow> bool"
    33   where "simple_path g \<longleftrightarrow>
    34      path g \<and> (\<forall>x\<in>{0..1}. \<forall>y\<in>{0..1}. g x = g y \<longrightarrow> x = y \<or> x = 0 \<and> y = 1 \<or> x = 1 \<and> y = 0)"
    35 
    36 definition arc :: "(real \<Rightarrow> 'a :: topological_space) \<Rightarrow> bool"
    37   where "arc g \<longleftrightarrow> path g \<and> inj_on g {0..1}"
    38 
    39 
    40 subsection\<open>Invariance theorems\<close>
    41 
    42 lemma path_eq: "path p \<Longrightarrow> (\<And>t. t \<in> {0..1} \<Longrightarrow> p t = q t) \<Longrightarrow> path q"
    43   using continuous_on_eq path_def by blast
    44 
    45 lemma path_continuous_image: "path g \<Longrightarrow> continuous_on (path_image g) f \<Longrightarrow> path(f o g)"
    46   unfolding path_def path_image_def
    47   using continuous_on_compose by blast
    48 
    49 lemma path_translation_eq:
    50   fixes g :: "real \<Rightarrow> 'a :: real_normed_vector"
    51   shows "path((\<lambda>x. a + x) o g) = path g"
    52 proof -
    53   have g: "g = (\<lambda>x. -a + x) o ((\<lambda>x. a + x) o g)"
    54     by (rule ext) simp
    55   show ?thesis
    56     unfolding path_def
    57     apply safe
    58     apply (subst g)
    59     apply (rule continuous_on_compose)
    60     apply (auto intro: continuous_intros)
    61     done
    62 qed
    63 
    64 lemma path_linear_image_eq:
    65   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
    66    assumes "linear f" "inj f"
    67      shows "path(f o g) = path g"
    68 proof -
    69   from linear_injective_left_inverse [OF assms]
    70   obtain h where h: "linear h" "h \<circ> f = id"
    71     by blast
    72   then have g: "g = h o (f o g)"
    73     by (metis comp_assoc id_comp)
    74   show ?thesis
    75     unfolding path_def
    76     using h assms
    77     by (metis g continuous_on_compose linear_continuous_on linear_conv_bounded_linear)
    78 qed
    79 
    80 lemma pathstart_translation: "pathstart((\<lambda>x. a + x) o g) = a + pathstart g"
    81   by (simp add: pathstart_def)
    82 
    83 lemma pathstart_linear_image_eq: "linear f \<Longrightarrow> pathstart(f o g) = f(pathstart g)"
    84   by (simp add: pathstart_def)
    85 
    86 lemma pathfinish_translation: "pathfinish((\<lambda>x. a + x) o g) = a + pathfinish g"
    87   by (simp add: pathfinish_def)
    88 
    89 lemma pathfinish_linear_image: "linear f \<Longrightarrow> pathfinish(f o g) = f(pathfinish g)"
    90   by (simp add: pathfinish_def)
    91 
    92 lemma path_image_translation: "path_image((\<lambda>x. a + x) o g) = (\<lambda>x. a + x) ` (path_image g)"
    93   by (simp add: image_comp path_image_def)
    94 
    95 lemma path_image_linear_image: "linear f \<Longrightarrow> path_image(f o g) = f ` (path_image g)"
    96   by (simp add: image_comp path_image_def)
    97 
    98 lemma reversepath_translation: "reversepath((\<lambda>x. a + x) o g) = (\<lambda>x. a + x) o reversepath g"
    99   by (rule ext) (simp add: reversepath_def)
   100 
   101 lemma reversepath_linear_image: "linear f \<Longrightarrow> reversepath(f o g) = f o reversepath g"
   102   by (rule ext) (simp add: reversepath_def)
   103 
   104 lemma joinpaths_translation:
   105     "((\<lambda>x. a + x) o g1) +++ ((\<lambda>x. a + x) o g2) = (\<lambda>x. a + x) o (g1 +++ g2)"
   106   by (rule ext) (simp add: joinpaths_def)
   107 
   108 lemma joinpaths_linear_image: "linear f \<Longrightarrow> (f o g1) +++ (f o g2) = f o (g1 +++ g2)"
   109   by (rule ext) (simp add: joinpaths_def)
   110 
   111 lemma simple_path_translation_eq:
   112   fixes g :: "real \<Rightarrow> 'a::euclidean_space"
   113   shows "simple_path((\<lambda>x. a + x) o g) = simple_path g"
   114   by (simp add: simple_path_def path_translation_eq)
   115 
   116 lemma simple_path_linear_image_eq:
   117   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   118   assumes "linear f" "inj f"
   119     shows "simple_path(f o g) = simple_path g"
   120   using assms inj_on_eq_iff [of f]
   121   by (auto simp: path_linear_image_eq simple_path_def path_translation_eq)
   122 
   123 lemma arc_translation_eq:
   124   fixes g :: "real \<Rightarrow> 'a::euclidean_space"
   125   shows "arc((\<lambda>x. a + x) o g) = arc g"
   126   by (auto simp: arc_def inj_on_def path_translation_eq)
   127 
   128 lemma arc_linear_image_eq:
   129   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   130    assumes "linear f" "inj f"
   131      shows  "arc(f o g) = arc g"
   132   using assms inj_on_eq_iff [of f]
   133   by (auto simp: arc_def inj_on_def path_linear_image_eq)
   134 
   135 subsection\<open>Basic lemmas about paths\<close>
   136 
   137 lemma arc_imp_simple_path: "arc g \<Longrightarrow> simple_path g"
   138   by (simp add: arc_def inj_on_def simple_path_def)
   139 
   140 lemma arc_imp_path: "arc g \<Longrightarrow> path g"
   141   using arc_def by blast
   142 
   143 lemma simple_path_imp_path: "simple_path g \<Longrightarrow> path g"
   144   using simple_path_def by blast
   145 
   146 lemma simple_path_cases: "simple_path g \<Longrightarrow> arc g \<or> pathfinish g = pathstart g"
   147   unfolding simple_path_def arc_def inj_on_def pathfinish_def pathstart_def
   148   by (force)
   149 
   150 lemma simple_path_imp_arc: "simple_path g \<Longrightarrow> pathfinish g \<noteq> pathstart g \<Longrightarrow> arc g"
   151   using simple_path_cases by auto
   152 
   153 lemma arc_distinct_ends: "arc g \<Longrightarrow> pathfinish g \<noteq> pathstart g"
   154   unfolding arc_def inj_on_def pathfinish_def pathstart_def
   155   by fastforce
   156 
   157 lemma arc_simple_path: "arc g \<longleftrightarrow> simple_path g \<and> pathfinish g \<noteq> pathstart g"
   158   using arc_distinct_ends arc_imp_simple_path simple_path_cases by blast
   159 
   160 lemma simple_path_eq_arc: "pathfinish g \<noteq> pathstart g \<Longrightarrow> (simple_path g = arc g)"
   161   by (simp add: arc_simple_path)
   162 
   163 lemma path_image_nonempty [simp]: "path_image g \<noteq> {}"
   164   unfolding path_image_def image_is_empty box_eq_empty
   165   by auto
   166 
   167 lemma pathstart_in_path_image[intro]: "pathstart g \<in> path_image g"
   168   unfolding pathstart_def path_image_def
   169   by auto
   170 
   171 lemma pathfinish_in_path_image[intro]: "pathfinish g \<in> path_image g"
   172   unfolding pathfinish_def path_image_def
   173   by auto
   174 
   175 lemma connected_path_image[intro]: "path g \<Longrightarrow> connected (path_image g)"
   176   unfolding path_def path_image_def
   177   using connected_continuous_image connected_Icc by blast
   178 
   179 lemma compact_path_image[intro]: "path g \<Longrightarrow> compact (path_image g)"
   180   unfolding path_def path_image_def
   181   using compact_continuous_image connected_Icc by blast
   182 
   183 lemma reversepath_reversepath[simp]: "reversepath (reversepath g) = g"
   184   unfolding reversepath_def
   185   by auto
   186 
   187 lemma pathstart_reversepath[simp]: "pathstart (reversepath g) = pathfinish g"
   188   unfolding pathstart_def reversepath_def pathfinish_def
   189   by auto
   190 
   191 lemma pathfinish_reversepath[simp]: "pathfinish (reversepath g) = pathstart g"
   192   unfolding pathstart_def reversepath_def pathfinish_def
   193   by auto
   194 
   195 lemma pathstart_join[simp]: "pathstart (g1 +++ g2) = pathstart g1"
   196   unfolding pathstart_def joinpaths_def pathfinish_def
   197   by auto
   198 
   199 lemma pathfinish_join[simp]: "pathfinish (g1 +++ g2) = pathfinish g2"
   200   unfolding pathstart_def joinpaths_def pathfinish_def
   201   by auto
   202 
   203 lemma path_image_reversepath[simp]: "path_image (reversepath g) = path_image g"
   204 proof -
   205   have *: "\<And>g. path_image (reversepath g) \<subseteq> path_image g"
   206     unfolding path_image_def subset_eq reversepath_def Ball_def image_iff
   207     by force
   208   show ?thesis
   209     using *[of g] *[of "reversepath g"]
   210     unfolding reversepath_reversepath
   211     by auto
   212 qed
   213 
   214 lemma path_reversepath [simp]: "path (reversepath g) \<longleftrightarrow> path g"
   215 proof -
   216   have *: "\<And>g. path g \<Longrightarrow> path (reversepath g)"
   217     unfolding path_def reversepath_def
   218     apply (rule continuous_on_compose[unfolded o_def, of _ "\<lambda>x. 1 - x"])
   219     apply (intro continuous_intros)
   220     apply (rule continuous_on_subset[of "{0..1}"])
   221     apply assumption
   222     apply auto
   223     done
   224   show ?thesis
   225     using *[of "reversepath g"] *[of g]
   226     unfolding reversepath_reversepath
   227     by (rule iffI)
   228 qed
   229 
   230 lemma arc_reversepath:
   231   assumes "arc g" shows "arc(reversepath g)"
   232 proof -
   233   have injg: "inj_on g {0..1}"
   234     using assms
   235     by (simp add: arc_def)
   236   have **: "\<And>x y::real. 1-x = 1-y \<Longrightarrow> x = y"
   237     by simp
   238   show ?thesis
   239     apply (auto simp: arc_def inj_on_def path_reversepath)
   240     apply (simp add: arc_imp_path assms)
   241     apply (rule **)
   242     apply (rule inj_onD [OF injg])
   243     apply (auto simp: reversepath_def)
   244     done
   245 qed
   246 
   247 lemma simple_path_reversepath: "simple_path g \<Longrightarrow> simple_path (reversepath g)"
   248   apply (simp add: simple_path_def)
   249   apply (force simp: reversepath_def)
   250   done
   251 
   252 lemmas reversepath_simps =
   253   path_reversepath path_image_reversepath pathstart_reversepath pathfinish_reversepath
   254 
   255 lemma path_join[simp]:
   256   assumes "pathfinish g1 = pathstart g2"
   257   shows "path (g1 +++ g2) \<longleftrightarrow> path g1 \<and> path g2"
   258   unfolding path_def pathfinish_def pathstart_def
   259 proof safe
   260   assume cont: "continuous_on {0..1} (g1 +++ g2)"
   261   have g1: "continuous_on {0..1} g1 \<longleftrightarrow> continuous_on {0..1} ((g1 +++ g2) \<circ> (\<lambda>x. x / 2))"
   262     by (intro continuous_on_cong refl) (auto simp: joinpaths_def)
   263   have g2: "continuous_on {0..1} g2 \<longleftrightarrow> continuous_on {0..1} ((g1 +++ g2) \<circ> (\<lambda>x. x / 2 + 1/2))"
   264     using assms
   265     by (intro continuous_on_cong refl) (auto simp: joinpaths_def pathfinish_def pathstart_def)
   266   show "continuous_on {0..1} g1" and "continuous_on {0..1} g2"
   267     unfolding g1 g2
   268     by (auto intro!: continuous_intros continuous_on_subset[OF cont] simp del: o_apply)
   269 next
   270   assume g1g2: "continuous_on {0..1} g1" "continuous_on {0..1} g2"
   271   have 01: "{0 .. 1} = {0..1/2} \<union> {1/2 .. 1::real}"
   272     by auto
   273   {
   274     fix x :: real
   275     assume "0 \<le> x" and "x \<le> 1"
   276     then have "x \<in> (\<lambda>x. x * 2) ` {0..1 / 2}"
   277       by (intro image_eqI[where x="x/2"]) auto
   278   }
   279   note 1 = this
   280   {
   281     fix x :: real
   282     assume "0 \<le> x" and "x \<le> 1"
   283     then have "x \<in> (\<lambda>x. x * 2 - 1) ` {1 / 2..1}"
   284       by (intro image_eqI[where x="x/2 + 1/2"]) auto
   285   }
   286   note 2 = this
   287   show "continuous_on {0..1} (g1 +++ g2)"
   288     using assms
   289     unfolding joinpaths_def 01
   290     apply (intro continuous_on_cases closed_atLeastAtMost g1g2[THEN continuous_on_compose2] continuous_intros)
   291     apply (auto simp: field_simps pathfinish_def pathstart_def intro!: 1 2)
   292     done
   293 qed
   294 
   295 section \<open>Path Images\<close>
   296 
   297 lemma bounded_path_image: "path g \<Longrightarrow> bounded(path_image g)"
   298   by (simp add: compact_imp_bounded compact_path_image)
   299 
   300 lemma closed_path_image:
   301   fixes g :: "real \<Rightarrow> 'a::t2_space"
   302   shows "path g \<Longrightarrow> closed(path_image g)"
   303   by (metis compact_path_image compact_imp_closed)
   304 
   305 lemma connected_simple_path_image: "simple_path g \<Longrightarrow> connected(path_image g)"
   306   by (metis connected_path_image simple_path_imp_path)
   307 
   308 lemma compact_simple_path_image: "simple_path g \<Longrightarrow> compact(path_image g)"
   309   by (metis compact_path_image simple_path_imp_path)
   310 
   311 lemma bounded_simple_path_image: "simple_path g \<Longrightarrow> bounded(path_image g)"
   312   by (metis bounded_path_image simple_path_imp_path)
   313 
   314 lemma closed_simple_path_image:
   315   fixes g :: "real \<Rightarrow> 'a::t2_space"
   316   shows "simple_path g \<Longrightarrow> closed(path_image g)"
   317   by (metis closed_path_image simple_path_imp_path)
   318 
   319 lemma connected_arc_image: "arc g \<Longrightarrow> connected(path_image g)"
   320   by (metis connected_path_image arc_imp_path)
   321 
   322 lemma compact_arc_image: "arc g \<Longrightarrow> compact(path_image g)"
   323   by (metis compact_path_image arc_imp_path)
   324 
   325 lemma bounded_arc_image: "arc g \<Longrightarrow> bounded(path_image g)"
   326   by (metis bounded_path_image arc_imp_path)
   327 
   328 lemma closed_arc_image:
   329   fixes g :: "real \<Rightarrow> 'a::t2_space"
   330   shows "arc g \<Longrightarrow> closed(path_image g)"
   331   by (metis closed_path_image arc_imp_path)
   332 
   333 lemma path_image_join_subset: "path_image (g1 +++ g2) \<subseteq> path_image g1 \<union> path_image g2"
   334   unfolding path_image_def joinpaths_def
   335   by auto
   336 
   337 lemma subset_path_image_join:
   338   assumes "path_image g1 \<subseteq> s"
   339     and "path_image g2 \<subseteq> s"
   340   shows "path_image (g1 +++ g2) \<subseteq> s"
   341   using path_image_join_subset[of g1 g2] and assms
   342   by auto
   343 
   344 lemma path_image_join:
   345     "pathfinish g1 = pathstart g2 \<Longrightarrow> path_image(g1 +++ g2) = path_image g1 \<union> path_image g2"
   346   apply (rule subset_antisym [OF path_image_join_subset])
   347   apply (auto simp: pathfinish_def pathstart_def path_image_def joinpaths_def image_def)
   348   apply (drule sym)
   349   apply (rule_tac x="xa/2" in bexI, auto)
   350   apply (rule ccontr)
   351   apply (drule_tac x="(xa+1)/2" in bspec)
   352   apply (auto simp: field_simps)
   353   apply (drule_tac x="1/2" in bspec, auto)
   354   done
   355 
   356 lemma not_in_path_image_join:
   357   assumes "x \<notin> path_image g1"
   358     and "x \<notin> path_image g2"
   359   shows "x \<notin> path_image (g1 +++ g2)"
   360   using assms and path_image_join_subset[of g1 g2]
   361   by auto
   362 
   363 lemma pathstart_compose: "pathstart(f o p) = f(pathstart p)"
   364   by (simp add: pathstart_def)
   365 
   366 lemma pathfinish_compose: "pathfinish(f o p) = f(pathfinish p)"
   367   by (simp add: pathfinish_def)
   368 
   369 lemma path_image_compose: "path_image (f o p) = f ` (path_image p)"
   370   by (simp add: image_comp path_image_def)
   371 
   372 lemma path_compose_join: "f o (p +++ q) = (f o p) +++ (f o q)"
   373   by (rule ext) (simp add: joinpaths_def)
   374 
   375 lemma path_compose_reversepath: "f o reversepath p = reversepath(f o p)"
   376   by (rule ext) (simp add: reversepath_def)
   377 
   378 lemma joinpaths_eq:
   379   "(\<And>t. t \<in> {0..1} \<Longrightarrow> p t = p' t) \<Longrightarrow>
   380    (\<And>t. t \<in> {0..1} \<Longrightarrow> q t = q' t)
   381    \<Longrightarrow>  t \<in> {0..1} \<Longrightarrow> (p +++ q) t = (p' +++ q') t"
   382   by (auto simp: joinpaths_def)
   383 
   384 lemma simple_path_inj_on: "simple_path g \<Longrightarrow> inj_on g {0<..<1}"
   385   by (auto simp: simple_path_def path_image_def inj_on_def less_eq_real_def Ball_def)
   386 
   387 
   388 subsection\<open>Simple paths with the endpoints removed\<close>
   389 
   390 lemma simple_path_endless:
   391     "simple_path c \<Longrightarrow> path_image c - {pathstart c,pathfinish c} = c ` {0<..<1}"
   392   apply (auto simp: simple_path_def path_image_def pathstart_def pathfinish_def Ball_def Bex_def image_def)
   393   apply (metis eq_iff le_less_linear)
   394   apply (metis leD linear)
   395   using less_eq_real_def zero_le_one apply blast
   396   using less_eq_real_def zero_le_one apply blast
   397   done
   398 
   399 lemma connected_simple_path_endless:
   400     "simple_path c \<Longrightarrow> connected(path_image c - {pathstart c,pathfinish c})"
   401 apply (simp add: simple_path_endless)
   402 apply (rule connected_continuous_image)
   403 apply (meson continuous_on_subset greaterThanLessThan_subseteq_atLeastAtMost_iff le_numeral_extra(3) le_numeral_extra(4) path_def simple_path_imp_path)
   404 by auto
   405 
   406 lemma nonempty_simple_path_endless:
   407     "simple_path c \<Longrightarrow> path_image c - {pathstart c,pathfinish c} \<noteq> {}"
   408   by (simp add: simple_path_endless)
   409 
   410 
   411 subsection\<open>The operations on paths\<close>
   412 
   413 lemma path_image_subset_reversepath: "path_image(reversepath g) \<le> path_image g"
   414   by (auto simp: path_image_def reversepath_def)
   415 
   416 lemma path_imp_reversepath: "path g \<Longrightarrow> path(reversepath g)"
   417   apply (auto simp: path_def reversepath_def)
   418   using continuous_on_compose [of "{0..1}" "\<lambda>x. 1 - x" g]
   419   apply (auto simp: continuous_on_op_minus)
   420   done
   421 
   422 lemma half_bounded_equal: "1 \<le> x * 2 \<Longrightarrow> x * 2 \<le> 1 \<longleftrightarrow> x = (1/2::real)"
   423   by simp
   424 
   425 lemma continuous_on_joinpaths:
   426   assumes "continuous_on {0..1} g1" "continuous_on {0..1} g2" "pathfinish g1 = pathstart g2"
   427     shows "continuous_on {0..1} (g1 +++ g2)"
   428 proof -
   429   have *: "{0..1::real} = {0..1/2} \<union> {1/2..1}"
   430     by auto
   431   have gg: "g2 0 = g1 1"
   432     by (metis assms(3) pathfinish_def pathstart_def)
   433   have 1: "continuous_on {0..1/2} (g1 +++ g2)"
   434     apply (rule continuous_on_eq [of _ "g1 o (\<lambda>x. 2*x)"])
   435     apply (rule continuous_intros | simp add: joinpaths_def assms)+
   436     done
   437   have "continuous_on {1/2..1} (g2 o (\<lambda>x. 2*x-1))"
   438     apply (rule continuous_on_subset [of "{1/2..1}"])
   439     apply (rule continuous_intros | simp add: image_affinity_atLeastAtMost_diff assms)+
   440     done
   441   then have 2: "continuous_on {1/2..1} (g1 +++ g2)"
   442     apply (rule continuous_on_eq [of "{1/2..1}" "g2 o (\<lambda>x. 2*x-1)"])
   443     apply (rule assms continuous_intros | simp add: joinpaths_def mult.commute half_bounded_equal gg)+
   444     done
   445   show ?thesis
   446     apply (subst *)
   447     apply (rule continuous_on_union)
   448     using 1 2
   449     apply auto
   450     done
   451 qed
   452 
   453 lemma path_join_imp: "\<lbrakk>path g1; path g2; pathfinish g1 = pathstart g2\<rbrakk> \<Longrightarrow> path(g1 +++ g2)"
   454   by (simp add: path_join)
   455 
   456 lemma simple_path_join_loop:
   457   assumes "arc g1" "arc g2"
   458           "pathfinish g1 = pathstart g2"  "pathfinish g2 = pathstart g1"
   459           "path_image g1 \<inter> path_image g2 \<subseteq> {pathstart g1, pathstart g2}"
   460   shows "simple_path(g1 +++ g2)"
   461 proof -
   462   have injg1: "inj_on g1 {0..1}"
   463     using assms
   464     by (simp add: arc_def)
   465   have injg2: "inj_on g2 {0..1}"
   466     using assms
   467     by (simp add: arc_def)
   468   have g12: "g1 1 = g2 0"
   469    and g21: "g2 1 = g1 0"
   470    and sb:  "g1 ` {0..1} \<inter> g2 ` {0..1} \<subseteq> {g1 0, g2 0}"
   471     using assms
   472     by (simp_all add: arc_def pathfinish_def pathstart_def path_image_def)
   473   { fix x and y::real
   474     assume xyI: "x = 1 \<longrightarrow> y \<noteq> 0"
   475        and xy: "x \<le> 1" "0 \<le> y" " y * 2 \<le> 1" "\<not> x * 2 \<le> 1" "g2 (2 * x - 1) = g1 (2 * y)"
   476     have g1im: "g1 (2 * y) \<in> g1 ` {0..1} \<inter> g2 ` {0..1}"
   477       using xy
   478       apply simp
   479       apply (rule_tac x="2 * x - 1" in image_eqI, auto)
   480       done
   481     have False
   482       using subsetD [OF sb g1im] xy
   483       apply auto
   484       apply (drule inj_onD [OF injg1])
   485       using g21 [symmetric] xyI
   486       apply (auto dest: inj_onD [OF injg2])
   487       done
   488    } note * = this
   489   { fix x and y::real
   490     assume xy: "y \<le> 1" "0 \<le> x" "\<not> y * 2 \<le> 1" "x * 2 \<le> 1" "g1 (2 * x) = g2 (2 * y - 1)"
   491     have g1im: "g1 (2 * x) \<in> g1 ` {0..1} \<inter> g2 ` {0..1}"
   492       using xy
   493       apply simp
   494       apply (rule_tac x="2 * x" in image_eqI, auto)
   495       done
   496     have "x = 0 \<and> y = 1"
   497       using subsetD [OF sb g1im] xy
   498       apply auto
   499       apply (force dest: inj_onD [OF injg1])
   500       using  g21 [symmetric]
   501       apply (auto dest: inj_onD [OF injg2])
   502       done
   503    } note ** = this
   504   show ?thesis
   505     using assms
   506     apply (simp add: arc_def simple_path_def path_join, clarify)
   507     apply (simp add: joinpaths_def split: split_if_asm)
   508     apply (force dest: inj_onD [OF injg1])
   509     apply (metis *)
   510     apply (metis **)
   511     apply (force dest: inj_onD [OF injg2])
   512     done
   513 qed
   514 
   515 lemma arc_join:
   516   assumes "arc g1" "arc g2"
   517           "pathfinish g1 = pathstart g2"
   518           "path_image g1 \<inter> path_image g2 \<subseteq> {pathstart g2}"
   519     shows "arc(g1 +++ g2)"
   520 proof -
   521   have injg1: "inj_on g1 {0..1}"
   522     using assms
   523     by (simp add: arc_def)
   524   have injg2: "inj_on g2 {0..1}"
   525     using assms
   526     by (simp add: arc_def)
   527   have g11: "g1 1 = g2 0"
   528    and sb:  "g1 ` {0..1} \<inter> g2 ` {0..1} \<subseteq> {g2 0}"
   529     using assms
   530     by (simp_all add: arc_def pathfinish_def pathstart_def path_image_def)
   531   { fix x and y::real
   532     assume xy: "x \<le> 1" "0 \<le> y" " y * 2 \<le> 1" "\<not> x * 2 \<le> 1" "g2 (2 * x - 1) = g1 (2 * y)"
   533     have g1im: "g1 (2 * y) \<in> g1 ` {0..1} \<inter> g2 ` {0..1}"
   534       using xy
   535       apply simp
   536       apply (rule_tac x="2 * x - 1" in image_eqI, auto)
   537       done
   538     have False
   539       using subsetD [OF sb g1im] xy
   540       by (auto dest: inj_onD [OF injg2])
   541    } note * = this
   542   show ?thesis
   543     apply (simp add: arc_def inj_on_def)
   544     apply (clarsimp simp add: arc_imp_path assms path_join)
   545     apply (simp add: joinpaths_def split: split_if_asm)
   546     apply (force dest: inj_onD [OF injg1])
   547     apply (metis *)
   548     apply (metis *)
   549     apply (force dest: inj_onD [OF injg2])
   550     done
   551 qed
   552 
   553 lemma reversepath_joinpaths:
   554     "pathfinish g1 = pathstart g2 \<Longrightarrow> reversepath(g1 +++ g2) = reversepath g2 +++ reversepath g1"
   555   unfolding reversepath_def pathfinish_def pathstart_def joinpaths_def
   556   by (rule ext) (auto simp: mult.commute)
   557 
   558 
   559 section\<open>Choosing a subpath of an existing path\<close>
   560 
   561 definition subpath :: "real \<Rightarrow> real \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> real \<Rightarrow> 'a::real_normed_vector"
   562   where "subpath a b g \<equiv> \<lambda>x. g((b - a) * x + a)"
   563 
   564 lemma path_image_subpath_gen:
   565   fixes g :: "_ \<Rightarrow> 'a::real_normed_vector"
   566   shows "path_image(subpath u v g) = g ` (closed_segment u v)"
   567   apply (simp add: closed_segment_real_eq path_image_def subpath_def)
   568   apply (subst o_def [of g, symmetric])
   569   apply (simp add: image_comp [symmetric])
   570   done
   571 
   572 lemma path_image_subpath:
   573   fixes g :: "real \<Rightarrow> 'a::real_normed_vector"
   574   shows "path_image(subpath u v g) = (if u \<le> v then g ` {u..v} else g ` {v..u})"
   575   by (simp add: path_image_subpath_gen closed_segment_eq_real_ivl)
   576 
   577 lemma path_subpath [simp]:
   578   fixes g :: "real \<Rightarrow> 'a::real_normed_vector"
   579   assumes "path g" "u \<in> {0..1}" "v \<in> {0..1}"
   580     shows "path(subpath u v g)"
   581 proof -
   582   have "continuous_on {0..1} (g o (\<lambda>x. ((v-u) * x+ u)))"
   583     apply (rule continuous_intros | simp)+
   584     apply (simp add: image_affinity_atLeastAtMost [where c=u])
   585     using assms
   586     apply (auto simp: path_def continuous_on_subset)
   587     done
   588   then show ?thesis
   589     by (simp add: path_def subpath_def)
   590 qed
   591 
   592 lemma pathstart_subpath [simp]: "pathstart(subpath u v g) = g(u)"
   593   by (simp add: pathstart_def subpath_def)
   594 
   595 lemma pathfinish_subpath [simp]: "pathfinish(subpath u v g) = g(v)"
   596   by (simp add: pathfinish_def subpath_def)
   597 
   598 lemma subpath_trivial [simp]: "subpath 0 1 g = g"
   599   by (simp add: subpath_def)
   600 
   601 lemma subpath_reversepath: "subpath 1 0 g = reversepath g"
   602   by (simp add: reversepath_def subpath_def)
   603 
   604 lemma reversepath_subpath: "reversepath(subpath u v g) = subpath v u g"
   605   by (simp add: reversepath_def subpath_def algebra_simps)
   606 
   607 lemma subpath_translation: "subpath u v ((\<lambda>x. a + x) o g) = (\<lambda>x. a + x) o subpath u v g"
   608   by (rule ext) (simp add: subpath_def)
   609 
   610 lemma subpath_linear_image: "linear f \<Longrightarrow> subpath u v (f o g) = f o subpath u v g"
   611   by (rule ext) (simp add: subpath_def)
   612 
   613 lemma affine_ineq:
   614   fixes x :: "'a::linordered_idom"
   615   assumes "x \<le> 1" "v \<le> u"
   616     shows "v + x * u \<le> u + x * v"
   617 proof -
   618   have "(1-x)*(u-v) \<ge> 0"
   619     using assms by auto
   620   then show ?thesis
   621     by (simp add: algebra_simps)
   622 qed
   623 
   624 lemma sum_le_prod1:
   625   fixes a::real shows "\<lbrakk>a \<le> 1; b \<le> 1\<rbrakk> \<Longrightarrow> a + b \<le> 1 + a * b"
   626 by (metis add.commute affine_ineq less_eq_real_def mult.right_neutral)
   627 
   628 lemma simple_path_subpath_eq:
   629   "simple_path(subpath u v g) \<longleftrightarrow>
   630      path(subpath u v g) \<and> u\<noteq>v \<and>
   631      (\<forall>x y. x \<in> closed_segment u v \<and> y \<in> closed_segment u v \<and> g x = g y
   632                 \<longrightarrow> x = y \<or> x = u \<and> y = v \<or> x = v \<and> y = u)"
   633     (is "?lhs = ?rhs")
   634 proof (rule iffI)
   635   assume ?lhs
   636   then have p: "path (\<lambda>x. g ((v - u) * x + u))"
   637         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>
   638                   \<Longrightarrow> x = y \<or> x = 0 \<and> y = 1 \<or> x = 1 \<and> y = 0)"
   639     by (auto simp: simple_path_def subpath_def)
   640   { fix x y
   641     assume "x \<in> closed_segment u v" "y \<in> closed_segment u v" "g x = g y"
   642     then have "x = y \<or> x = u \<and> y = v \<or> x = v \<and> y = u"
   643     using sim [of "(x-u)/(v-u)" "(y-u)/(v-u)"] p
   644     by (auto simp: closed_segment_real_eq image_affinity_atLeastAtMost divide_simps
   645        split: split_if_asm)
   646   } moreover
   647   have "path(subpath u v g) \<and> u\<noteq>v"
   648     using sim [of "1/3" "2/3"] p
   649     by (auto simp: subpath_def)
   650   ultimately show ?rhs
   651     by metis
   652 next
   653   assume ?rhs
   654   then
   655   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"
   656    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"
   657    and ne: "u < v \<or> v < u"
   658    and psp: "path (subpath u v g)"
   659     by (auto simp: closed_segment_real_eq image_affinity_atLeastAtMost)
   660   have [simp]: "\<And>x. u + x * v = v + x * u \<longleftrightarrow> u=v \<or> x=1"
   661     by algebra
   662   show ?lhs using psp ne
   663     unfolding simple_path_def subpath_def
   664     by (fastforce simp add: algebra_simps affine_ineq mult_left_mono crossproduct_eq dest: d1 d2)
   665 qed
   666 
   667 lemma arc_subpath_eq:
   668   "arc(subpath u v g) \<longleftrightarrow> path(subpath u v g) \<and> u\<noteq>v \<and> inj_on g (closed_segment u v)"
   669     (is "?lhs = ?rhs")
   670 proof (rule iffI)
   671   assume ?lhs
   672   then have p: "path (\<lambda>x. g ((v - u) * x + u))"
   673         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>
   674                   \<Longrightarrow> x = y)"
   675     by (auto simp: arc_def inj_on_def subpath_def)
   676   { fix x y
   677     assume "x \<in> closed_segment u v" "y \<in> closed_segment u v" "g x = g y"
   678     then have "x = y"
   679     using sim [of "(x-u)/(v-u)" "(y-u)/(v-u)"] p
   680     by (force simp add: inj_on_def closed_segment_real_eq image_affinity_atLeastAtMost divide_simps
   681        split: split_if_asm)
   682   } moreover
   683   have "path(subpath u v g) \<and> u\<noteq>v"
   684     using sim [of "1/3" "2/3"] p
   685     by (auto simp: subpath_def)
   686   ultimately show ?rhs
   687     unfolding inj_on_def
   688     by metis
   689 next
   690   assume ?rhs
   691   then
   692   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"
   693    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"
   694    and ne: "u < v \<or> v < u"
   695    and psp: "path (subpath u v g)"
   696     by (auto simp: inj_on_def closed_segment_real_eq image_affinity_atLeastAtMost)
   697   show ?lhs using psp ne
   698     unfolding arc_def subpath_def inj_on_def
   699     by (auto simp: algebra_simps affine_ineq mult_left_mono crossproduct_eq dest: d1 d2)
   700 qed
   701 
   702 
   703 lemma simple_path_subpath:
   704   assumes "simple_path g" "u \<in> {0..1}" "v \<in> {0..1}" "u \<noteq> v"
   705   shows "simple_path(subpath u v g)"
   706   using assms
   707   apply (simp add: simple_path_subpath_eq simple_path_imp_path)
   708   apply (simp add: simple_path_def closed_segment_real_eq image_affinity_atLeastAtMost, fastforce)
   709   done
   710 
   711 lemma arc_simple_path_subpath:
   712     "\<lbrakk>simple_path g; u \<in> {0..1}; v \<in> {0..1}; g u \<noteq> g v\<rbrakk> \<Longrightarrow> arc(subpath u v g)"
   713   by (force intro: simple_path_subpath simple_path_imp_arc)
   714 
   715 lemma arc_subpath_arc:
   716     "\<lbrakk>arc g; u \<in> {0..1}; v \<in> {0..1}; u \<noteq> v\<rbrakk> \<Longrightarrow> arc(subpath u v g)"
   717   by (meson arc_def arc_imp_simple_path arc_simple_path_subpath inj_onD)
   718 
   719 lemma arc_simple_path_subpath_interior:
   720     "\<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)"
   721     apply (rule arc_simple_path_subpath)
   722     apply (force simp: simple_path_def)+
   723     done
   724 
   725 lemma path_image_subpath_subset:
   726     "\<lbrakk>path g; u \<in> {0..1}; v \<in> {0..1}\<rbrakk> \<Longrightarrow> path_image(subpath u v g) \<subseteq> path_image g"
   727   apply (simp add: closed_segment_real_eq image_affinity_atLeastAtMost path_image_subpath)
   728   apply (auto simp: path_image_def)
   729   done
   730 
   731 lemma join_subpaths_middle: "subpath (0) ((1 / 2)) p +++ subpath ((1 / 2)) 1 p = p"
   732   by (rule ext) (simp add: joinpaths_def subpath_def divide_simps)
   733 
   734 subsection\<open>There is a subpath to the frontier\<close>
   735 
   736 lemma subpath_to_frontier_explicit:
   737     fixes S :: "'a::metric_space set"
   738     assumes g: "path g" and "pathfinish g \<notin> S"
   739     obtains u where "0 \<le> u" "u \<le> 1"
   740                 "\<And>x. 0 \<le> x \<and> x < u \<Longrightarrow> g x \<in> interior S"
   741                 "(g u \<notin> interior S)" "(u = 0 \<or> g u \<in> closure S)"
   742 proof -
   743   have gcon: "continuous_on {0..1} g"     using g by (simp add: path_def)
   744   then have com: "compact ({0..1} \<inter> {u. g u \<in> closure (- S)})"
   745     apply (simp add: Int_commute [of "{0..1}"] compact_eq_bounded_closed closed_vimage_Int [unfolded vimage_def])
   746     using compact_eq_bounded_closed apply fastforce
   747     done
   748   have "1 \<in> {u. g u \<in> closure (- S)}"
   749     using assms by (simp add: pathfinish_def closure_def)
   750   then have dis: "{0..1} \<inter> {u. g u \<in> closure (- S)} \<noteq> {}"
   751     using atLeastAtMost_iff zero_le_one by blast
   752   then obtain u where "0 \<le> u" "u \<le> 1" and gu: "g u \<in> closure (- S)"
   753                   and umin: "\<And>t. \<lbrakk>0 \<le> t; t \<le> 1; g t \<in> closure (- S)\<rbrakk> \<Longrightarrow> u \<le> t"
   754     using compact_attains_inf [OF com dis] by fastforce
   755   then have umin': "\<And>t. \<lbrakk>0 \<le> t; t \<le> 1; t < u\<rbrakk> \<Longrightarrow>  g t \<in> S"
   756     using closure_def by fastforce
   757   { assume "u \<noteq> 0"
   758     then have "u > 0" using \<open>0 \<le> u\<close> by auto
   759     { fix e::real assume "e > 0"
   760       obtain d where "d>0" and d: "\<And>x'. \<lbrakk>x' \<in> {0..1}; dist x' u < d\<rbrakk> \<Longrightarrow> dist (g x') (g u) < e"
   761         using continuous_onD [OF gcon _ \<open>e > 0\<close>] \<open>0 \<le> _\<close> \<open>_ \<le> 1\<close> atLeastAtMost_iff by auto
   762       have *: "dist (max 0 (u - d / 2)) u < d"
   763         using \<open>0 \<le> u\<close> \<open>u \<le> 1\<close> \<open>d > 0\<close> by (simp add: dist_real_def)
   764       have "\<exists>y\<in>S. dist y (g u) < e"
   765         using \<open>0 < u\<close> \<open>u \<le> 1\<close> \<open>d > 0\<close>
   766         by (force intro: d [OF _ *] umin')
   767     }
   768     then have "g u \<in> closure S"
   769       by (simp add: frontier_def closure_approachable)
   770   }
   771   then show ?thesis
   772     apply (rule_tac u=u in that)
   773     apply (auto simp: \<open>0 \<le> u\<close> \<open>u \<le> 1\<close> gu interior_closure umin)
   774     using \<open>_ \<le> 1\<close> interior_closure umin apply fastforce
   775     done
   776 qed
   777 
   778 lemma subpath_to_frontier_strong:
   779     assumes g: "path g" and "pathfinish g \<notin> S"
   780     obtains u where "0 \<le> u" "u \<le> 1" "g u \<notin> interior S"
   781                     "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"
   782 proof -
   783   obtain u where "0 \<le> u" "u \<le> 1"
   784              and gxin: "\<And>x. 0 \<le> x \<and> x < u \<Longrightarrow> g x \<in> interior S"
   785              and gunot: "(g u \<notin> interior S)" and u0: "(u = 0 \<or> g u \<in> closure S)"
   786     using subpath_to_frontier_explicit [OF assms] by blast
   787   show ?thesis
   788     apply (rule that [OF \<open>0 \<le> u\<close> \<open>u \<le> 1\<close>])
   789     apply (simp add: gunot)
   790     using \<open>0 \<le> u\<close> u0 by (force simp: subpath_def gxin)
   791 qed
   792 
   793 lemma subpath_to_frontier:
   794     assumes g: "path g" and g0: "pathstart g \<in> closure S" and g1: "pathfinish g \<notin> S"
   795     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"
   796 proof -
   797   obtain u where "0 \<le> u" "u \<le> 1"
   798              and notin: "g u \<notin> interior S"
   799              and disj: "u = 0 \<or>
   800                         (\<forall>x. 0 \<le> x \<and> x < 1 \<longrightarrow> subpath 0 u g x \<in> interior S) \<and> g u \<in> closure S"
   801     using subpath_to_frontier_strong [OF g g1] by blast
   802   show ?thesis
   803     apply (rule that [OF \<open>0 \<le> u\<close> \<open>u \<le> 1\<close>])
   804     apply (metis DiffI disj frontier_def g0 notin pathstart_def)
   805     using \<open>0 \<le> u\<close> g0 disj
   806     apply (simp add: path_image_subpath_gen)
   807     apply (auto simp: closed_segment_eq_real_ivl pathstart_def pathfinish_def subpath_def)
   808     apply (rename_tac y)
   809     apply (drule_tac x="y/u" in spec)
   810     apply (auto split: split_if_asm)
   811     done
   812 qed
   813 
   814 lemma exists_path_subpath_to_frontier:
   815     fixes S :: "'a::real_normed_vector set"
   816     assumes "path g" "pathstart g \<in> closure S" "pathfinish g \<notin> S"
   817     obtains h where "path h" "pathstart h = pathstart g" "path_image h \<subseteq> path_image g"
   818                     "path_image h - {pathfinish h} \<subseteq> interior S"
   819                     "pathfinish h \<in> frontier S"
   820 proof -
   821   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"
   822     using subpath_to_frontier [OF assms] by blast
   823   show ?thesis
   824     apply (rule that [of "subpath 0 u g"])
   825     using assms u
   826     apply (simp_all add: path_image_subpath)
   827     apply (simp add: pathstart_def)
   828     apply (force simp: closed_segment_eq_real_ivl path_image_def)
   829     done
   830 qed
   831 
   832 lemma exists_path_subpath_to_frontier_closed:
   833     fixes S :: "'a::real_normed_vector set"
   834     assumes S: "closed S" and g: "path g" and g0: "pathstart g \<in> S" and g1: "pathfinish g \<notin> S"
   835     obtains h where "path h" "pathstart h = pathstart g" "path_image h \<subseteq> path_image g \<inter> S"
   836                     "pathfinish h \<in> frontier S"
   837 proof -
   838   obtain h where h: "path h" "pathstart h = pathstart g" "path_image h \<subseteq> path_image g"
   839                     "path_image h - {pathfinish h} \<subseteq> interior S"
   840                     "pathfinish h \<in> frontier S"
   841     using exists_path_subpath_to_frontier [OF g _ g1] closure_closed [OF S] g0 by auto
   842   show ?thesis
   843     apply (rule that [OF \<open>path h\<close>])
   844     using assms h
   845     apply auto
   846     apply (metis Diff_single_insert frontier_subset_eq insert_iff interior_subset subset_iff)
   847     done
   848 qed
   849 
   850 subsection \<open>Reparametrizing a closed curve to start at some chosen point\<close>
   851 
   852 definition shiftpath :: "real \<Rightarrow> (real \<Rightarrow> 'a::topological_space) \<Rightarrow> real \<Rightarrow> 'a"
   853   where "shiftpath a f = (\<lambda>x. if (a + x) \<le> 1 then f (a + x) else f (a + x - 1))"
   854 
   855 lemma pathstart_shiftpath: "a \<le> 1 \<Longrightarrow> pathstart (shiftpath a g) = g a"
   856   unfolding pathstart_def shiftpath_def by auto
   857 
   858 lemma pathfinish_shiftpath:
   859   assumes "0 \<le> a"
   860     and "pathfinish g = pathstart g"
   861   shows "pathfinish (shiftpath a g) = g a"
   862   using assms
   863   unfolding pathstart_def pathfinish_def shiftpath_def
   864   by auto
   865 
   866 lemma endpoints_shiftpath:
   867   assumes "pathfinish g = pathstart g"
   868     and "a \<in> {0 .. 1}"
   869   shows "pathfinish (shiftpath a g) = g a"
   870     and "pathstart (shiftpath a g) = g a"
   871   using assms
   872   by (auto intro!: pathfinish_shiftpath pathstart_shiftpath)
   873 
   874 lemma closed_shiftpath:
   875   assumes "pathfinish g = pathstart g"
   876     and "a \<in> {0..1}"
   877   shows "pathfinish (shiftpath a g) = pathstart (shiftpath a g)"
   878   using endpoints_shiftpath[OF assms]
   879   by auto
   880 
   881 lemma path_shiftpath:
   882   assumes "path g"
   883     and "pathfinish g = pathstart g"
   884     and "a \<in> {0..1}"
   885   shows "path (shiftpath a g)"
   886 proof -
   887   have *: "{0 .. 1} = {0 .. 1-a} \<union> {1-a .. 1}"
   888     using assms(3) by auto
   889   have **: "\<And>x. x + a = 1 \<Longrightarrow> g (x + a - 1) = g (x + a)"
   890     using assms(2)[unfolded pathfinish_def pathstart_def]
   891     by auto
   892   show ?thesis
   893     unfolding path_def shiftpath_def *
   894     apply (rule continuous_on_union)
   895     apply (rule closed_real_atLeastAtMost)+
   896     apply (rule continuous_on_eq[of _ "g \<circ> (\<lambda>x. a + x)"])
   897     prefer 3
   898     apply (rule continuous_on_eq[of _ "g \<circ> (\<lambda>x. a - 1 + x)"])
   899     prefer 3
   900     apply (rule continuous_intros)+
   901     prefer 2
   902     apply (rule continuous_intros)+
   903     apply (rule_tac[1-2] continuous_on_subset[OF assms(1)[unfolded path_def]])
   904     using assms(3) and **
   905     apply auto
   906     apply (auto simp add: field_simps)
   907     done
   908 qed
   909 
   910 lemma shiftpath_shiftpath:
   911   assumes "pathfinish g = pathstart g"
   912     and "a \<in> {0..1}"
   913     and "x \<in> {0..1}"
   914   shows "shiftpath (1 - a) (shiftpath a g) x = g x"
   915   using assms
   916   unfolding pathfinish_def pathstart_def shiftpath_def
   917   by auto
   918 
   919 lemma path_image_shiftpath:
   920   assumes "a \<in> {0..1}"
   921     and "pathfinish g = pathstart g"
   922   shows "path_image (shiftpath a g) = path_image g"
   923 proof -
   924   { fix x
   925     assume as: "g 1 = g 0" "x \<in> {0..1::real}" " \<forall>y\<in>{0..1} \<inter> {x. \<not> a + x \<le> 1}. g x \<noteq> g (a + y - 1)"
   926     then have "\<exists>y\<in>{0..1} \<inter> {x. a + x \<le> 1}. g x = g (a + y)"
   927     proof (cases "a \<le> x")
   928       case False
   929       then show ?thesis
   930         apply (rule_tac x="1 + x - a" in bexI)
   931         using as(1,2) and as(3)[THEN bspec[where x="1 + x - a"]] and assms(1)
   932         apply (auto simp add: field_simps atomize_not)
   933         done
   934     next
   935       case True
   936       then show ?thesis
   937         using as(1-2) and assms(1)
   938         apply (rule_tac x="x - a" in bexI)
   939         apply (auto simp add: field_simps)
   940         done
   941     qed
   942   }
   943   then show ?thesis
   944     using assms
   945     unfolding shiftpath_def path_image_def pathfinish_def pathstart_def
   946     by (auto simp add: image_iff)
   947 qed
   948 
   949 
   950 subsection \<open>Special case of straight-line paths\<close>
   951 
   952 definition linepath :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> real \<Rightarrow> 'a"
   953   where "linepath a b = (\<lambda>x. (1 - x) *\<^sub>R a + x *\<^sub>R b)"
   954 
   955 lemma pathstart_linepath[simp]: "pathstart (linepath a b) = a"
   956   unfolding pathstart_def linepath_def
   957   by auto
   958 
   959 lemma pathfinish_linepath[simp]: "pathfinish (linepath a b) = b"
   960   unfolding pathfinish_def linepath_def
   961   by auto
   962 
   963 lemma continuous_linepath_at[intro]: "continuous (at x) (linepath a b)"
   964   unfolding linepath_def
   965   by (intro continuous_intros)
   966 
   967 lemma continuous_on_linepath [intro,continuous_intros]: "continuous_on s (linepath a b)"
   968   using continuous_linepath_at
   969   by (auto intro!: continuous_at_imp_continuous_on)
   970 
   971 lemma path_linepath[intro]: "path (linepath a b)"
   972   unfolding path_def
   973   by (rule continuous_on_linepath)
   974 
   975 lemma path_image_linepath[simp]: "path_image (linepath a b) = closed_segment a b"
   976   unfolding path_image_def segment linepath_def
   977   by auto
   978 
   979 lemma reversepath_linepath[simp]: "reversepath (linepath a b) = linepath b a"
   980   unfolding reversepath_def linepath_def
   981   by auto
   982 
   983 lemma linepath_0 [simp]: "linepath 0 b x = x *\<^sub>R b"
   984   by (simp add: linepath_def)
   985 
   986 lemma arc_linepath:
   987   assumes "a \<noteq> b"
   988   shows "arc (linepath a b)"
   989 proof -
   990   {
   991     fix x y :: "real"
   992     assume "x *\<^sub>R b + y *\<^sub>R a = x *\<^sub>R a + y *\<^sub>R b"
   993     then have "(x - y) *\<^sub>R a = (x - y) *\<^sub>R b"
   994       by (simp add: algebra_simps)
   995     with assms have "x = y"
   996       by simp
   997   }
   998   then show ?thesis
   999     unfolding arc_def inj_on_def
  1000     by (simp add:  path_linepath) (force simp: algebra_simps linepath_def)
  1001 qed
  1002 
  1003 lemma simple_path_linepath[intro]: "a \<noteq> b \<Longrightarrow> simple_path (linepath a b)"
  1004   by (simp add: arc_imp_simple_path arc_linepath)
  1005 
  1006 lemma linepath_trivial [simp]: "linepath a a x = a"
  1007   by (simp add: linepath_def real_vector.scale_left_diff_distrib)
  1008 
  1009 lemma subpath_refl: "subpath a a g = linepath (g a) (g a)"
  1010   by (simp add: subpath_def linepath_def algebra_simps)
  1011 
  1012 
  1013 subsection \<open>Bounding a point away from a path\<close>
  1014 
  1015 lemma not_on_path_ball:
  1016   fixes g :: "real \<Rightarrow> 'a::heine_borel"
  1017   assumes "path g"
  1018     and "z \<notin> path_image g"
  1019   shows "\<exists>e > 0. ball z e \<inter> path_image g = {}"
  1020 proof -
  1021   obtain a where "a \<in> path_image g" "\<forall>y \<in> path_image g. dist z a \<le> dist z y"
  1022     using distance_attains_inf[OF _ path_image_nonempty, of g z]
  1023     using compact_path_image[THEN compact_imp_closed, OF assms(1)] by auto
  1024   then show ?thesis
  1025     apply (rule_tac x="dist z a" in exI)
  1026     using assms(2)
  1027     apply (auto intro!: dist_pos_lt)
  1028     done
  1029 qed
  1030 
  1031 lemma not_on_path_cball:
  1032   fixes g :: "real \<Rightarrow> 'a::heine_borel"
  1033   assumes "path g"
  1034     and "z \<notin> path_image g"
  1035   shows "\<exists>e>0. cball z e \<inter> (path_image g) = {}"
  1036 proof -
  1037   obtain e where "ball z e \<inter> path_image g = {}" "e > 0"
  1038     using not_on_path_ball[OF assms] by auto
  1039   moreover have "cball z (e/2) \<subseteq> ball z e"
  1040     using \<open>e > 0\<close> by auto
  1041   ultimately show ?thesis
  1042     apply (rule_tac x="e/2" in exI)
  1043     apply auto
  1044     done
  1045 qed
  1046 
  1047 
  1048 section \<open>Path component, considered as a "joinability" relation (from Tom Hales)\<close>
  1049 
  1050 definition "path_component s x y \<longleftrightarrow>
  1051   (\<exists>g. path g \<and> path_image g \<subseteq> s \<and> pathstart g = x \<and> pathfinish g = y)"
  1052 
  1053 abbreviation
  1054    "path_component_set s x \<equiv> Collect (path_component s x)"
  1055 
  1056 lemmas path_defs = path_def pathstart_def pathfinish_def path_image_def path_component_def
  1057 
  1058 lemma path_component_mem:
  1059   assumes "path_component s x y"
  1060   shows "x \<in> s" and "y \<in> s"
  1061   using assms
  1062   unfolding path_defs
  1063   by auto
  1064 
  1065 lemma path_component_refl:
  1066   assumes "x \<in> s"
  1067   shows "path_component s x x"
  1068   unfolding path_defs
  1069   apply (rule_tac x="\<lambda>u. x" in exI)
  1070   using assms
  1071   apply (auto intro!: continuous_intros)
  1072   done
  1073 
  1074 lemma path_component_refl_eq: "path_component s x x \<longleftrightarrow> x \<in> s"
  1075   by (auto intro!: path_component_mem path_component_refl)
  1076 
  1077 lemma path_component_sym: "path_component s x y \<Longrightarrow> path_component s y x"
  1078   using assms
  1079   unfolding path_component_def
  1080   apply (erule exE)
  1081   apply (rule_tac x="reversepath g" in exI)
  1082   apply auto
  1083   done
  1084 
  1085 lemma path_component_trans:
  1086   assumes "path_component s x y" and "path_component s y z"
  1087   shows "path_component s x z"
  1088   using assms
  1089   unfolding path_component_def
  1090   apply (elim exE)
  1091   apply (rule_tac x="g +++ ga" in exI)
  1092   apply (auto simp add: path_image_join)
  1093   done
  1094 
  1095 lemma path_component_of_subset: "s \<subseteq> t \<Longrightarrow> path_component s x y \<Longrightarrow> path_component t x y"
  1096   unfolding path_component_def by auto
  1097 
  1098 lemma path_connected_linepath:
  1099     fixes s :: "'a::real_normed_vector set"
  1100     shows "closed_segment a b \<subseteq> s \<Longrightarrow> path_component s a b"
  1101   apply (simp add: path_component_def)
  1102   apply (rule_tac x="linepath a b" in exI, auto)
  1103   done
  1104 
  1105 
  1106 text \<open>Can also consider it as a set, as the name suggests.\<close>
  1107 
  1108 lemma path_component_set:
  1109   "path_component_set s x =
  1110     {y. (\<exists>g. path g \<and> path_image g \<subseteq> s \<and> pathstart g = x \<and> pathfinish g = y)}"
  1111   by (auto simp: path_component_def)
  1112 
  1113 lemma path_component_subset: "path_component_set s x \<subseteq> s"
  1114   by (auto simp add: path_component_mem(2))
  1115 
  1116 lemma path_component_eq_empty: "path_component_set s x = {} \<longleftrightarrow> x \<notin> s"
  1117   using path_component_mem path_component_refl_eq
  1118     by fastforce
  1119 
  1120 lemma path_component_mono:
  1121      "s \<subseteq> t \<Longrightarrow> (path_component_set s x) \<subseteq> (path_component_set t x)"
  1122   by (simp add: Collect_mono path_component_of_subset)
  1123 
  1124 lemma path_component_eq:
  1125    "y \<in> path_component_set s x \<Longrightarrow> path_component_set s y = path_component_set s x"
  1126 by (metis (no_types, lifting) Collect_cong mem_Collect_eq path_component_sym path_component_trans)
  1127 
  1128 subsection \<open>Path connectedness of a space\<close>
  1129 
  1130 definition "path_connected s \<longleftrightarrow>
  1131   (\<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)"
  1132 
  1133 lemma path_connected_component: "path_connected s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. path_component s x y)"
  1134   unfolding path_connected_def path_component_def by auto
  1135 
  1136 lemma path_connected_component_set: "path_connected s \<longleftrightarrow> (\<forall>x\<in>s. path_component_set s x = s)"
  1137   unfolding path_connected_component path_component_subset
  1138   using path_component_mem by blast
  1139 
  1140 lemma path_component_maximal:
  1141      "\<lbrakk>x \<in> t; path_connected t; t \<subseteq> s\<rbrakk> \<Longrightarrow> t \<subseteq> (path_component_set s x)"
  1142   by (metis path_component_mono path_connected_component_set)
  1143 
  1144 subsection \<open>Some useful lemmas about path-connectedness\<close>
  1145 
  1146 lemma convex_imp_path_connected:
  1147   fixes s :: "'a::real_normed_vector set"
  1148   assumes "convex s"
  1149   shows "path_connected s"
  1150   unfolding path_connected_def
  1151   apply rule
  1152   apply rule
  1153   apply (rule_tac x = "linepath x y" in exI)
  1154   unfolding path_image_linepath
  1155   using assms [unfolded convex_contains_segment]
  1156   apply auto
  1157   done
  1158 
  1159 lemma path_connected_imp_connected:
  1160   assumes "path_connected s"
  1161   shows "connected s"
  1162   unfolding connected_def not_ex
  1163   apply rule
  1164   apply rule
  1165   apply (rule ccontr)
  1166   unfolding not_not
  1167   apply (elim conjE)
  1168 proof -
  1169   fix e1 e2
  1170   assume as: "open e1" "open e2" "s \<subseteq> e1 \<union> e2" "e1 \<inter> e2 \<inter> s = {}" "e1 \<inter> s \<noteq> {}" "e2 \<inter> s \<noteq> {}"
  1171   then obtain x1 x2 where obt:"x1 \<in> e1 \<inter> s" "x2 \<in> e2 \<inter> s"
  1172     by auto
  1173   then obtain g where g: "path g" "path_image g \<subseteq> s" "pathstart g = x1" "pathfinish g = x2"
  1174     using assms[unfolded path_connected_def,rule_format,of x1 x2] by auto
  1175   have *: "connected {0..1::real}"
  1176     by (auto intro!: convex_connected convex_real_interval)
  1177   have "{0..1} \<subseteq> {x \<in> {0..1}. g x \<in> e1} \<union> {x \<in> {0..1}. g x \<in> e2}"
  1178     using as(3) g(2)[unfolded path_defs] by blast
  1179   moreover have "{x \<in> {0..1}. g x \<in> e1} \<inter> {x \<in> {0..1}. g x \<in> e2} = {}"
  1180     using as(4) g(2)[unfolded path_defs]
  1181     unfolding subset_eq
  1182     by auto
  1183   moreover have "{x \<in> {0..1}. g x \<in> e1} \<noteq> {} \<and> {x \<in> {0..1}. g x \<in> e2} \<noteq> {}"
  1184     using g(3,4)[unfolded path_defs]
  1185     using obt
  1186     by (simp add: ex_in_conv [symmetric], metis zero_le_one order_refl)
  1187   ultimately show False
  1188     using *[unfolded connected_local not_ex, rule_format,
  1189       of "{x\<in>{0..1}. g x \<in> e1}" "{x\<in>{0..1}. g x \<in> e2}"]
  1190     using continuous_openin_preimage[OF g(1)[unfolded path_def] as(1)]
  1191     using continuous_openin_preimage[OF g(1)[unfolded path_def] as(2)]
  1192     by auto
  1193 qed
  1194 
  1195 lemma open_path_component:
  1196   fixes s :: "'a::real_normed_vector set"
  1197   assumes "open s"
  1198   shows "open (path_component_set s x)"
  1199   unfolding open_contains_ball
  1200 proof
  1201   fix y
  1202   assume as: "y \<in> path_component_set s x"
  1203   then have "y \<in> s"
  1204     apply -
  1205     apply (rule path_component_mem(2))
  1206     unfolding mem_Collect_eq
  1207     apply auto
  1208     done
  1209   then obtain e where e: "e > 0" "ball y e \<subseteq> s"
  1210     using assms[unfolded open_contains_ball]
  1211     by auto
  1212   show "\<exists>e > 0. ball y e \<subseteq> path_component_set s x"
  1213     apply (rule_tac x=e in exI)
  1214     apply (rule,rule \<open>e>0\<close>)
  1215     apply rule
  1216     unfolding mem_ball mem_Collect_eq
  1217   proof -
  1218     fix z
  1219     assume "dist y z < e"
  1220     then show "path_component s x z"
  1221       apply (rule_tac path_component_trans[of _ _ y])
  1222       defer
  1223       apply (rule path_component_of_subset[OF e(2)])
  1224       apply (rule convex_imp_path_connected[OF convex_ball, unfolded path_connected_component, rule_format])
  1225       using \<open>e > 0\<close> as
  1226       apply auto
  1227       done
  1228   qed
  1229 qed
  1230 
  1231 lemma open_non_path_component:
  1232   fixes s :: "'a::real_normed_vector set"
  1233   assumes "open s"
  1234   shows "open (s - path_component_set s x)"
  1235   unfolding open_contains_ball
  1236 proof
  1237   fix y
  1238   assume as: "y \<in> s - path_component_set s x"
  1239   then obtain e where e: "e > 0" "ball y e \<subseteq> s"
  1240     using assms [unfolded open_contains_ball]
  1241     by auto
  1242   show "\<exists>e>0. ball y e \<subseteq> s - path_component_set s x"
  1243     apply (rule_tac x=e in exI)
  1244     apply rule
  1245     apply (rule \<open>e>0\<close>)
  1246     apply rule
  1247     apply rule
  1248     defer
  1249   proof (rule ccontr)
  1250     fix z
  1251     assume "z \<in> ball y e" "\<not> z \<notin> path_component_set s x"
  1252     then have "y \<in> path_component_set s x"
  1253       unfolding not_not mem_Collect_eq using \<open>e>0\<close>
  1254       apply -
  1255       apply (rule path_component_trans, assumption)
  1256       apply (rule path_component_of_subset[OF e(2)])
  1257       apply (rule convex_imp_path_connected[OF convex_ball, unfolded path_connected_component, rule_format])
  1258       apply auto
  1259       done
  1260     then show False
  1261       using as by auto
  1262   qed (insert e(2), auto)
  1263 qed
  1264 
  1265 lemma connected_open_path_connected:
  1266   fixes s :: "'a::real_normed_vector set"
  1267   assumes "open s"
  1268     and "connected s"
  1269   shows "path_connected s"
  1270   unfolding path_connected_component_set
  1271 proof (rule, rule, rule path_component_subset, rule)
  1272   fix x y
  1273   assume "x \<in> s" and "y \<in> s"
  1274   show "y \<in> path_component_set s x"
  1275   proof (rule ccontr)
  1276     assume "\<not> ?thesis"
  1277     moreover have "path_component_set s x \<inter> s \<noteq> {}"
  1278       using \<open>x \<in> s\<close> path_component_eq_empty path_component_subset[of s x]
  1279       by auto
  1280     ultimately
  1281     show False
  1282       using \<open>y \<in> s\<close> open_non_path_component[OF assms(1)] open_path_component[OF assms(1)]
  1283       using assms(2)[unfolded connected_def not_ex, rule_format,
  1284         of "path_component_set s x" "s - path_component_set s x"]
  1285       by auto
  1286   qed
  1287 qed
  1288 
  1289 lemma path_connected_continuous_image:
  1290   assumes "continuous_on s f"
  1291     and "path_connected s"
  1292   shows "path_connected (f ` s)"
  1293   unfolding path_connected_def
  1294 proof (rule, rule)
  1295   fix x' y'
  1296   assume "x' \<in> f ` s" "y' \<in> f ` s"
  1297   then obtain x y where x: "x \<in> s" and y: "y \<in> s" and x': "x' = f x" and y': "y' = f y"
  1298     by auto
  1299   from x y obtain g where "path g \<and> path_image g \<subseteq> s \<and> pathstart g = x \<and> pathfinish g = y"
  1300     using assms(2)[unfolded path_connected_def] by fast
  1301   then show "\<exists>g. path g \<and> path_image g \<subseteq> f ` s \<and> pathstart g = x' \<and> pathfinish g = y'"
  1302     unfolding x' y'
  1303     apply (rule_tac x="f \<circ> g" in exI)
  1304     unfolding path_defs
  1305     apply (intro conjI continuous_on_compose continuous_on_subset[OF assms(1)])
  1306     apply auto
  1307     done
  1308 qed
  1309 
  1310 lemma path_connected_segment:
  1311     fixes a :: "'a::real_normed_vector"
  1312     shows "path_connected (closed_segment a b)"
  1313   by (simp add: convex_imp_path_connected)
  1314 
  1315 lemma path_connected_open_segment:
  1316     fixes a :: "'a::real_normed_vector"
  1317     shows "path_connected (open_segment a b)"
  1318   by (simp add: convex_imp_path_connected)
  1319 
  1320 lemma homeomorphic_path_connectedness:
  1321   "s homeomorphic t \<Longrightarrow> path_connected s \<longleftrightarrow> path_connected t"
  1322   unfolding homeomorphic_def homeomorphism_def by (metis path_connected_continuous_image)
  1323 
  1324 lemma path_connected_empty: "path_connected {}"
  1325   unfolding path_connected_def by auto
  1326 
  1327 lemma path_connected_singleton: "path_connected {a}"
  1328   unfolding path_connected_def pathstart_def pathfinish_def path_image_def
  1329   apply clarify
  1330   apply (rule_tac x="\<lambda>x. a" in exI)
  1331   apply (simp add: image_constant_conv)
  1332   apply (simp add: path_def continuous_on_const)
  1333   done
  1334 
  1335 lemma path_connected_Un:
  1336   assumes "path_connected s"
  1337     and "path_connected t"
  1338     and "s \<inter> t \<noteq> {}"
  1339   shows "path_connected (s \<union> t)"
  1340   unfolding path_connected_component
  1341 proof (rule, rule)
  1342   fix x y
  1343   assume as: "x \<in> s \<union> t" "y \<in> s \<union> t"
  1344   from assms(3) obtain z where "z \<in> s \<inter> t"
  1345     by auto
  1346   then show "path_component (s \<union> t) x y"
  1347     using as and assms(1-2)[unfolded path_connected_component]
  1348     apply -
  1349     apply (erule_tac[!] UnE)+
  1350     apply (rule_tac[2-3] path_component_trans[of _ _ z])
  1351     apply (auto simp add:path_component_of_subset [OF Un_upper1] path_component_of_subset[OF Un_upper2])
  1352     done
  1353 qed
  1354 
  1355 lemma path_connected_UNION:
  1356   assumes "\<And>i. i \<in> A \<Longrightarrow> path_connected (S i)"
  1357     and "\<And>i. i \<in> A \<Longrightarrow> z \<in> S i"
  1358   shows "path_connected (\<Union>i\<in>A. S i)"
  1359   unfolding path_connected_component
  1360 proof clarify
  1361   fix x i y j
  1362   assume *: "i \<in> A" "x \<in> S i" "j \<in> A" "y \<in> S j"
  1363   then have "path_component (S i) x z" and "path_component (S j) z y"
  1364     using assms by (simp_all add: path_connected_component)
  1365   then have "path_component (\<Union>i\<in>A. S i) x z" and "path_component (\<Union>i\<in>A. S i) z y"
  1366     using *(1,3) by (auto elim!: path_component_of_subset [rotated])
  1367   then show "path_component (\<Union>i\<in>A. S i) x y"
  1368     by (rule path_component_trans)
  1369 qed
  1370 
  1371 lemma path_component_path_image_pathstart:
  1372   assumes p: "path p" and x: "x \<in> path_image p"
  1373   shows "path_component (path_image p) (pathstart p) x"
  1374 using x
  1375 proof (clarsimp simp add: path_image_def)
  1376   fix y
  1377   assume "x = p y" and y: "0 \<le> y" "y \<le> 1"
  1378   show "path_component (p ` {0..1}) (pathstart p) (p y)"
  1379   proof (cases "y=0")
  1380     case True then show ?thesis
  1381       by (simp add: path_component_refl_eq pathstart_def)
  1382   next
  1383     case False have "continuous_on {0..1} (p o (op*y))"
  1384       apply (rule continuous_intros)+
  1385       using p [unfolded path_def] y
  1386       apply (auto simp: mult_le_one intro: continuous_on_subset [of _ p])
  1387       done
  1388     then have "path (\<lambda>u. p (y * u))"
  1389       by (simp add: path_def)
  1390     then show ?thesis
  1391       apply (simp add: path_component_def)
  1392       apply (rule_tac x = "\<lambda>u. p (y * u)" in exI)
  1393       apply (intro conjI)
  1394       using y False
  1395       apply (auto simp: mult_le_one pathstart_def pathfinish_def path_image_def)
  1396       done
  1397   qed
  1398 qed
  1399 
  1400 lemma path_connected_path_image: "path p \<Longrightarrow> path_connected(path_image p)"
  1401   unfolding path_connected_component
  1402   by (meson path_component_path_image_pathstart path_component_sym path_component_trans)
  1403 
  1404 lemma path_connected_path_component:
  1405    "path_connected (path_component_set s x)"
  1406 proof -
  1407   { fix y z
  1408     assume pa: "path_component s x y" "path_component s x z"
  1409     then have pae: "path_component_set s x = path_component_set s y"
  1410       using path_component_eq by auto
  1411     have yz: "path_component s y z"
  1412       using pa path_component_sym path_component_trans by blast
  1413     then have "\<exists>g. path g \<and> path_image g \<subseteq> path_component_set s x \<and> pathstart g = y \<and> pathfinish g = z"
  1414       apply (simp add: path_component_def, clarify)
  1415       apply (rule_tac x=g in exI)
  1416       by (simp add: pae path_component_maximal path_connected_path_image pathstart_in_path_image)
  1417   }
  1418   then show ?thesis
  1419     by (simp add: path_connected_def)
  1420 qed
  1421 
  1422 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)"
  1423   apply (intro iffI)
  1424   apply (metis path_connected_path_image path_defs(5) pathfinish_in_path_image pathstart_in_path_image)
  1425   using path_component_of_subset path_connected_component by blast
  1426 
  1427 lemma path_component_path_component [simp]:
  1428    "path_component_set (path_component_set s x) x = path_component_set s x"
  1429 proof (cases "x \<in> s")
  1430   case True show ?thesis
  1431     apply (rule subset_antisym)
  1432     apply (simp add: path_component_subset)
  1433     by (simp add: True path_component_maximal path_component_refl path_connected_path_component)
  1434 next
  1435   case False then show ?thesis
  1436     by (metis False empty_iff path_component_eq_empty)
  1437 qed
  1438 
  1439 lemma path_component_subset_connected_component:
  1440    "(path_component_set s x) \<subseteq> (connected_component_set s x)"
  1441 proof (cases "x \<in> s")
  1442   case True show ?thesis
  1443     apply (rule connected_component_maximal)
  1444     apply (auto simp: True path_component_subset path_component_refl path_connected_imp_connected path_connected_path_component)
  1445     done
  1446 next
  1447   case False then show ?thesis
  1448     using path_component_eq_empty by auto
  1449 qed
  1450 
  1451 subsection \<open>Sphere is path-connected\<close>
  1452 
  1453 lemma path_connected_punctured_universe:
  1454   assumes "2 \<le> DIM('a::euclidean_space)"
  1455   shows "path_connected (- {a::'a})"
  1456 proof -
  1457   let ?A = "{x::'a. \<exists>i\<in>Basis. x \<bullet> i < a \<bullet> i}"
  1458   let ?B = "{x::'a. \<exists>i\<in>Basis. a \<bullet> i < x \<bullet> i}"
  1459 
  1460   have A: "path_connected ?A"
  1461     unfolding Collect_bex_eq
  1462   proof (rule path_connected_UNION)
  1463     fix i :: 'a
  1464     assume "i \<in> Basis"
  1465     then show "(\<Sum>i\<in>Basis. (a \<bullet> i - 1)*\<^sub>R i) \<in> {x::'a. x \<bullet> i < a \<bullet> i}"
  1466       by simp
  1467     show "path_connected {x. x \<bullet> i < a \<bullet> i}"
  1468       using convex_imp_path_connected [OF convex_halfspace_lt, of i "a \<bullet> i"]
  1469       by (simp add: inner_commute)
  1470   qed
  1471   have B: "path_connected ?B"
  1472     unfolding Collect_bex_eq
  1473   proof (rule path_connected_UNION)
  1474     fix i :: 'a
  1475     assume "i \<in> Basis"
  1476     then show "(\<Sum>i\<in>Basis. (a \<bullet> i + 1) *\<^sub>R i) \<in> {x::'a. a \<bullet> i < x \<bullet> i}"
  1477       by simp
  1478     show "path_connected {x. a \<bullet> i < x \<bullet> i}"
  1479       using convex_imp_path_connected [OF convex_halfspace_gt, of "a \<bullet> i" i]
  1480       by (simp add: inner_commute)
  1481   qed
  1482   obtain S :: "'a set" where "S \<subseteq> Basis" and "card S = Suc (Suc 0)"
  1483     using ex_card[OF assms]
  1484     by auto
  1485   then obtain b0 b1 :: 'a where "b0 \<in> Basis" and "b1 \<in> Basis" and "b0 \<noteq> b1"
  1486     unfolding card_Suc_eq by auto
  1487   then have "a + b0 - b1 \<in> ?A \<inter> ?B"
  1488     by (auto simp: inner_simps inner_Basis)
  1489   then have "?A \<inter> ?B \<noteq> {}"
  1490     by fast
  1491   with A B have "path_connected (?A \<union> ?B)"
  1492     by (rule path_connected_Un)
  1493   also have "?A \<union> ?B = {x. \<exists>i\<in>Basis. x \<bullet> i \<noteq> a \<bullet> i}"
  1494     unfolding neq_iff bex_disj_distrib Collect_disj_eq ..
  1495   also have "\<dots> = {x. x \<noteq> a}"
  1496     unfolding euclidean_eq_iff [where 'a='a]
  1497     by (simp add: Bex_def)
  1498   also have "\<dots> = - {a}"
  1499     by auto
  1500   finally show ?thesis .
  1501 qed
  1502 
  1503 lemma path_connected_sphere:
  1504   assumes "2 \<le> DIM('a::euclidean_space)"
  1505   shows "path_connected {x::'a. norm (x - a) = r}"
  1506 proof (rule linorder_cases [of r 0])
  1507   assume "r < 0"
  1508   then have "{x::'a. norm(x - a) = r} = {}"
  1509     by auto
  1510   then show ?thesis
  1511     using path_connected_empty by simp
  1512 next
  1513   assume "r = 0"
  1514   then show ?thesis
  1515     using path_connected_singleton by simp
  1516 next
  1517   assume r: "0 < r"
  1518   have *: "{x::'a. norm(x - a) = r} = (\<lambda>x. a + r *\<^sub>R x) ` {x. norm x = 1}"
  1519     apply (rule set_eqI)
  1520     apply rule
  1521     unfolding image_iff
  1522     apply (rule_tac x="(1/r) *\<^sub>R (x - a)" in bexI)
  1523     unfolding mem_Collect_eq norm_scaleR
  1524     using r
  1525     apply (auto simp add: scaleR_right_diff_distrib)
  1526     done
  1527   have **: "{x::'a. norm x = 1} = (\<lambda>x. (1/norm x) *\<^sub>R x) ` (- {0})"
  1528     apply (rule set_eqI)
  1529     apply rule
  1530     unfolding image_iff
  1531     apply (rule_tac x=x in bexI)
  1532     unfolding mem_Collect_eq
  1533     apply (auto split: split_if_asm)
  1534     done
  1535   have "continuous_on (- {0}) (\<lambda>x::'a. 1 / norm x)"
  1536     by (auto intro!: continuous_intros)
  1537   then show ?thesis
  1538     unfolding * **
  1539     using path_connected_punctured_universe[OF assms]
  1540     by (auto intro!: path_connected_continuous_image continuous_intros)
  1541 qed
  1542 
  1543 corollary connected_sphere: "2 \<le> DIM('a::euclidean_space) \<Longrightarrow> connected {x::'a. norm (x - a) = r}"
  1544   using path_connected_sphere path_connected_imp_connected
  1545   by auto
  1546 
  1547 corollary path_connected_complement_bounded_convex:
  1548     fixes s :: "'a :: euclidean_space set"
  1549     assumes "bounded s" "convex s" and 2: "2 \<le> DIM('a)"
  1550     shows "path_connected (- s)"
  1551 proof (cases "s={}")
  1552   case True then show ?thesis
  1553     using convex_imp_path_connected by auto
  1554 next
  1555   case False
  1556   then obtain a where "a \<in> s" by auto
  1557   { fix x y assume "x \<notin> s" "y \<notin> s"
  1558     then have "x \<noteq> a" "y \<noteq> a" using \<open>a \<in> s\<close> by auto
  1559     then have bxy: "bounded(insert x (insert y s))"
  1560       by (simp add: \<open>bounded s\<close>)
  1561     then obtain B::real where B: "0 < B" and Bx: "norm (a - x) < B" and By: "norm (a - y) < B"
  1562                           and "s \<subseteq> ball a B"
  1563       using bounded_subset_ballD [OF bxy, of a] by (auto simp: dist_norm)
  1564     def C == "B / norm(x - a)"
  1565     { fix u
  1566       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"
  1567       have CC: "1 \<le> 1 + (C - 1) * u"
  1568         using \<open>x \<noteq> a\<close> \<open>0 \<le> u\<close>
  1569         apply (simp add: C_def divide_simps norm_minus_commute)
  1570         using Bx by auto
  1571       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)"
  1572         by (simp add: algebra_simps)
  1573       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)) =
  1574             (1 + (u / (1 + C * u - u))) *\<^sub>R a + ((1 / (1 + C * u - u)) + (C * u / (1 + C * u - u))) *\<^sub>R x"
  1575         by (simp add: algebra_simps)
  1576       also have "... = (1 + (u / (1 + C * u - u))) *\<^sub>R a + (1 + (u / (1 + C * u - u))) *\<^sub>R x"
  1577         using CC by (simp add: field_simps)
  1578       also have "... = x + (1 + (u / (1 + C * u - u))) *\<^sub>R a + (u / (1 + C * u - u)) *\<^sub>R x"
  1579         by (simp add: algebra_simps)
  1580       also have "... = x + ((1 / (1 + C * u - u)) *\<^sub>R a +
  1581               ((u / (1 + C * u - u)) *\<^sub>R x + (C * u / (1 + C * u - u)) *\<^sub>R a))"
  1582         using CC by (simp add: field_simps) (simp add: add_divide_distrib scaleR_add_left)
  1583       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"
  1584         by (simp add: algebra_simps)
  1585       have False
  1586         using \<open>convex s\<close>
  1587         apply (simp add: convex_alt)
  1588         apply (drule_tac x=a in bspec)
  1589          apply (rule  \<open>a \<in> s\<close>)
  1590         apply (drule_tac x="a + (1 + (C - 1) * u) *\<^sub>R (x - a)" in bspec)
  1591          using u apply (simp add: *)
  1592         apply (drule_tac x="1 / (1 + (C - 1) * u)" in spec)
  1593         using \<open>x \<noteq> a\<close> \<open>x \<notin> s\<close> \<open>0 \<le> u\<close> CC
  1594         apply (auto simp: xeq)
  1595         done
  1596     }
  1597     then have pcx: "path_component (- s) x (a + C *\<^sub>R (x - a))"
  1598       by (force simp: closed_segment_def intro!: path_connected_linepath)
  1599     def D == "B / norm(y - a)"  \<comment>\<open>massive duplication with the proof above\<close>
  1600     { fix u
  1601       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"
  1602       have DD: "1 \<le> 1 + (D - 1) * u"
  1603         using \<open>y \<noteq> a\<close> \<open>0 \<le> u\<close>
  1604         apply (simp add: D_def divide_simps norm_minus_commute)
  1605         using By by auto
  1606       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)"
  1607         by (simp add: algebra_simps)
  1608       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)) =
  1609             (1 + (u / (1 + D * u - u))) *\<^sub>R a + ((1 / (1 + D * u - u)) + (D * u / (1 + D * u - u))) *\<^sub>R y"
  1610         by (simp add: algebra_simps)
  1611       also have "... = (1 + (u / (1 + D * u - u))) *\<^sub>R a + (1 + (u / (1 + D * u - u))) *\<^sub>R y"
  1612         using DD by (simp add: field_simps)
  1613       also have "... = y + (1 + (u / (1 + D * u - u))) *\<^sub>R a + (u / (1 + D * u - u)) *\<^sub>R y"
  1614         by (simp add: algebra_simps)
  1615       also have "... = y + ((1 / (1 + D * u - u)) *\<^sub>R a +
  1616               ((u / (1 + D * u - u)) *\<^sub>R y + (D * u / (1 + D * u - u)) *\<^sub>R a))"
  1617         using DD by (simp add: field_simps) (simp add: add_divide_distrib scaleR_add_left)
  1618       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"
  1619         by (simp add: algebra_simps)
  1620       have False
  1621         using \<open>convex s\<close>
  1622         apply (simp add: convex_alt)
  1623         apply (drule_tac x=a in bspec)
  1624          apply (rule  \<open>a \<in> s\<close>)
  1625         apply (drule_tac x="a + (1 + (D - 1) * u) *\<^sub>R (y - a)" in bspec)
  1626          using u apply (simp add: *)
  1627         apply (drule_tac x="1 / (1 + (D - 1) * u)" in spec)
  1628         using \<open>y \<noteq> a\<close> \<open>y \<notin> s\<close> \<open>0 \<le> u\<close> DD
  1629         apply (auto simp: xeq)
  1630         done
  1631     }
  1632     then have pdy: "path_component (- s) y (a + D *\<^sub>R (y - a))"
  1633       by (force simp: closed_segment_def intro!: path_connected_linepath)
  1634     have pyx: "path_component (- s) (a + D *\<^sub>R (y - a)) (a + C *\<^sub>R (x - a))"
  1635       apply (rule path_component_of_subset [of "{x. norm(x - a) = B}"])
  1636        using \<open>s \<subseteq> ball a B\<close>
  1637        apply (force simp: ball_def dist_norm norm_minus_commute)
  1638       apply (rule path_connected_sphere [OF 2, of a B, simplified path_connected_component, rule_format])
  1639       using \<open>x \<noteq> a\<close>  using \<open>y \<noteq> a\<close>  B apply (auto simp: C_def D_def)
  1640       done
  1641     have "path_component (- s) x y"
  1642       by (metis path_component_trans path_component_sym pcx pdy pyx)
  1643   }
  1644   then show ?thesis
  1645     by (auto simp: path_connected_component)
  1646 qed
  1647 
  1648 
  1649 lemma connected_complement_bounded_convex:
  1650     fixes s :: "'a :: euclidean_space set"
  1651     assumes "bounded s" "convex s" "2 \<le> DIM('a)"
  1652       shows  "connected (- s)"
  1653   using path_connected_complement_bounded_convex [OF assms] path_connected_imp_connected by blast
  1654 
  1655 lemma connected_diff_ball:
  1656     fixes s :: "'a :: euclidean_space set"
  1657     assumes "connected s" "cball a r \<subseteq> s" "2 \<le> DIM('a)"
  1658       shows "connected (s - ball a r)"
  1659   apply (rule connected_diff_open_from_closed [OF ball_subset_cball])
  1660   using assms connected_sphere
  1661   apply (auto simp: cball_diff_eq_sphere dist_norm)
  1662   done
  1663 
  1664 subsection\<open>Relations between components and path components\<close>
  1665 
  1666 lemma open_connected_component:
  1667   fixes s :: "'a::real_normed_vector set"
  1668   shows "open s \<Longrightarrow> open (connected_component_set s x)"
  1669     apply (simp add: open_contains_ball, clarify)
  1670     apply (rename_tac y)
  1671     apply (drule_tac x=y in bspec)
  1672      apply (simp add: connected_component_in, clarify)
  1673     apply (rule_tac x=e in exI)
  1674     by (metis mem_Collect_eq connected_component_eq connected_component_maximal centre_in_ball connected_ball)
  1675 
  1676 corollary open_components:
  1677     fixes s :: "'a::real_normed_vector set"
  1678     shows "\<lbrakk>open u; s \<in> components u\<rbrakk> \<Longrightarrow> open s"
  1679   by (simp add: components_iff) (metis open_connected_component)
  1680 
  1681 lemma in_closure_connected_component:
  1682   fixes s :: "'a::real_normed_vector set"
  1683   assumes x: "x \<in> s" and s: "open s"
  1684   shows "x \<in> closure (connected_component_set s y) \<longleftrightarrow>  x \<in> connected_component_set s y"
  1685 proof -
  1686   { assume "x \<in> closure (connected_component_set s y)"
  1687     moreover have "x \<in> connected_component_set s x"
  1688       using x by simp
  1689     ultimately have "x \<in> connected_component_set s y"
  1690       using s by (meson Compl_disjoint closure_iff_nhds_not_empty connected_component_disjoint disjoint_eq_subset_Compl open_connected_component)
  1691   }
  1692   then show ?thesis
  1693     by (auto simp: closure_def)
  1694 qed
  1695 
  1696 subsection\<open>Existence of unbounded components\<close>
  1697 
  1698 lemma cobounded_unbounded_component:
  1699     fixes s :: "'a :: euclidean_space set"
  1700     assumes "bounded (-s)"
  1701       shows "\<exists>x. x \<in> s \<and> ~ bounded (connected_component_set s x)"
  1702 proof -
  1703   obtain i::'a where i: "i \<in> Basis"
  1704     using nonempty_Basis by blast
  1705   obtain B where B: "B>0" "-s \<subseteq> ball 0 B"
  1706     using bounded_subset_ballD [OF assms, of 0] by auto
  1707   then have *: "\<And>x. B \<le> norm x \<Longrightarrow> x \<in> s"
  1708     by (force simp add: ball_def dist_norm)
  1709   have unbounded_inner: "~ bounded {x. inner i x \<ge> B}"
  1710     apply (auto simp: bounded_def dist_norm)
  1711     apply (rule_tac x="x + (max B e + 1 + \<bar>i \<bullet> x\<bar>) *\<^sub>R i" in exI)
  1712     apply simp
  1713     using i
  1714     apply (auto simp: algebra_simps)
  1715     done
  1716   have **: "{x. B \<le> i \<bullet> x} \<subseteq> connected_component_set s (B *\<^sub>R i)"
  1717     apply (rule connected_component_maximal)
  1718     apply (auto simp: i intro: convex_connected convex_halfspace_ge [of B])
  1719     apply (rule *)
  1720     apply (rule order_trans [OF _ Basis_le_norm [OF i]])
  1721     by (simp add: inner_commute)
  1722   have "B *\<^sub>R i \<in> s"
  1723     by (rule *) (simp add: norm_Basis [OF i])
  1724   then show ?thesis
  1725     apply (rule_tac x="B *\<^sub>R i" in exI, clarify)
  1726     apply (frule bounded_subset [of _ "{x. B \<le> i \<bullet> x}", OF _ **])
  1727     using unbounded_inner apply blast
  1728     done
  1729 qed
  1730 
  1731 lemma cobounded_unique_unbounded_component:
  1732     fixes s :: "'a :: euclidean_space set"
  1733     assumes bs: "bounded (-s)" and "2 \<le> DIM('a)"
  1734         and bo: "~ bounded(connected_component_set s x)"
  1735                 "~ bounded(connected_component_set s y)"
  1736       shows "connected_component_set s x = connected_component_set s y"
  1737 proof -
  1738   obtain i::'a where i: "i \<in> Basis"
  1739     using nonempty_Basis by blast
  1740   obtain B where B: "B>0" "-s \<subseteq> ball 0 B"
  1741     using bounded_subset_ballD [OF bs, of 0] by auto
  1742   then have *: "\<And>x. B \<le> norm x \<Longrightarrow> x \<in> s"
  1743     by (force simp add: ball_def dist_norm)
  1744   have ccb: "connected (- ball 0 B :: 'a set)"
  1745     using assms by (auto intro: connected_complement_bounded_convex)
  1746   obtain x' where x': "connected_component s x x'" "norm x' > B"
  1747     using bo [unfolded bounded_def dist_norm, simplified, rule_format]
  1748     by (metis diff_zero norm_minus_commute not_less)
  1749   obtain y' where y': "connected_component s y y'" "norm y' > B"
  1750     using bo [unfolded bounded_def dist_norm, simplified, rule_format]
  1751     by (metis diff_zero norm_minus_commute not_less)
  1752   have x'y': "connected_component s x' y'"
  1753     apply (simp add: connected_component_def)
  1754     apply (rule_tac x="- ball 0 B" in exI)
  1755     using x' y'
  1756     apply (auto simp: ccb dist_norm *)
  1757     done
  1758   show ?thesis
  1759     apply (rule connected_component_eq)
  1760     using x' y' x'y'
  1761     by (metis (no_types, lifting) connected_component_eq_empty connected_component_eq_eq connected_component_idemp connected_component_in)
  1762 qed
  1763 
  1764 lemma cobounded_unbounded_components:
  1765     fixes s :: "'a :: euclidean_space set"
  1766     shows "bounded (-s) \<Longrightarrow> \<exists>c. c \<in> components s \<and> ~bounded c"
  1767   by (metis cobounded_unbounded_component components_def imageI)
  1768 
  1769 lemma cobounded_unique_unbounded_components:
  1770     fixes s :: "'a :: euclidean_space set"
  1771     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"
  1772   unfolding components_iff
  1773   by (metis cobounded_unique_unbounded_component)
  1774 
  1775 lemma cobounded_has_bounded_component:
  1776     fixes s :: "'a :: euclidean_space set"
  1777     shows "\<lbrakk>bounded (- s); ~connected s; 2 \<le> DIM('a)\<rbrakk> \<Longrightarrow> \<exists>c. c \<in> components s \<and> bounded c"
  1778   by (meson cobounded_unique_unbounded_components connected_eq_connected_components_eq)
  1779 
  1780 
  1781 section\<open>The "inside" and "outside" of a set\<close>
  1782 
  1783 text\<open>The inside comprises the points in a bounded connected component of the set's complement.
  1784   The outside comprises the points in unbounded connected component of the complement.\<close>
  1785 
  1786 definition inside where
  1787   "inside s \<equiv> {x. (x \<notin> s) \<and> bounded(connected_component_set ( - s) x)}"
  1788 
  1789 definition outside where
  1790   "outside s \<equiv> -s \<inter> {x. ~ bounded(connected_component_set (- s) x)}"
  1791 
  1792 lemma outside: "outside s = {x. ~ bounded(connected_component_set (- s) x)}"
  1793   by (auto simp: outside_def) (metis Compl_iff bounded_empty connected_component_eq_empty)
  1794 
  1795 lemma inside_no_overlap [simp]: "inside s \<inter> s = {}"
  1796   by (auto simp: inside_def)
  1797 
  1798 lemma outside_no_overlap [simp]:
  1799    "outside s \<inter> s = {}"
  1800   by (auto simp: outside_def)
  1801 
  1802 lemma inside_inter_outside [simp]: "inside s \<inter> outside s = {}"
  1803   by (auto simp: inside_def outside_def)
  1804 
  1805 lemma inside_union_outside [simp]: "inside s \<union> outside s = (- s)"
  1806   by (auto simp: inside_def outside_def)
  1807 
  1808 lemma inside_eq_outside:
  1809    "inside s = outside s \<longleftrightarrow> s = UNIV"
  1810   by (auto simp: inside_def outside_def)
  1811 
  1812 lemma inside_outside: "inside s = (- (s \<union> outside s))"
  1813   by (force simp add: inside_def outside)
  1814 
  1815 lemma outside_inside: "outside s = (- (s \<union> inside s))"
  1816   by (auto simp: inside_outside) (metis IntI equals0D outside_no_overlap)
  1817 
  1818 lemma union_with_inside: "s \<union> inside s = - outside s"
  1819   by (auto simp: inside_outside) (simp add: outside_inside)
  1820 
  1821 lemma union_with_outside: "s \<union> outside s = - inside s"
  1822   by (simp add: inside_outside)
  1823 
  1824 lemma outside_mono: "s \<subseteq> t \<Longrightarrow> outside t \<subseteq> outside s"
  1825   by (auto simp: outside bounded_subset connected_component_mono)
  1826 
  1827 lemma inside_mono: "s \<subseteq> t \<Longrightarrow> inside s - t \<subseteq> inside t"
  1828   by (auto simp: inside_def bounded_subset connected_component_mono)
  1829 
  1830 lemma segment_bound_lemma:
  1831   fixes u::real
  1832   assumes "x \<ge> B" "y \<ge> B" "0 \<le> u" "u \<le> 1"
  1833   shows "(1 - u) * x + u * y \<ge> B"
  1834 proof -
  1835   obtain dx dy where "dx \<ge> 0" "dy \<ge> 0" "x = B + dx" "y = B + dy"
  1836     using assms by auto (metis add.commute diff_add_cancel)
  1837   with \<open>0 \<le> u\<close> \<open>u \<le> 1\<close> show ?thesis
  1838     by (simp add: add_increasing2 mult_left_le field_simps)
  1839 qed
  1840 
  1841 lemma cobounded_outside:
  1842   fixes s :: "'a :: real_normed_vector set"
  1843   assumes "bounded s" shows "bounded (- outside s)"
  1844 proof -
  1845   obtain B where B: "B>0" "s \<subseteq> ball 0 B"
  1846     using bounded_subset_ballD [OF assms, of 0] by auto
  1847   { fix x::'a and C::real
  1848     assume Bno: "B \<le> norm x" and C: "0 < C"
  1849     have "\<exists>y. connected_component (- s) x y \<and> norm y > C"
  1850     proof (cases "x = 0")
  1851       case True with B Bno show ?thesis by force
  1852     next
  1853       case False with B C show ?thesis
  1854         apply (rule_tac x="((B+C)/norm x) *\<^sub>R x" in exI)
  1855         apply (simp add: connected_component_def)
  1856         apply (rule_tac x="closed_segment x (((B+C)/norm x) *\<^sub>R x)" in exI)
  1857         apply simp
  1858         apply (rule_tac y="- ball 0 B" in order_trans)
  1859          prefer 2 apply force
  1860         apply (simp add: closed_segment_def ball_def dist_norm, clarify)
  1861         apply (simp add: real_vector_class.scaleR_add_left [symmetric] divide_simps)
  1862         using segment_bound_lemma [of B "norm x" "B+C" ] Bno
  1863         by (meson le_add_same_cancel1 less_eq_real_def not_le)
  1864     qed
  1865   }
  1866   then show ?thesis
  1867     apply (simp add: outside_def assms)
  1868     apply (rule bounded_subset [OF bounded_ball [of 0 B]])
  1869     apply (force simp add: dist_norm not_less bounded_pos)
  1870     done
  1871 qed
  1872 
  1873 lemma unbounded_outside:
  1874     fixes s :: "'a::{real_normed_vector, perfect_space} set"
  1875     shows "bounded s \<Longrightarrow> ~ bounded(outside s)"
  1876   using cobounded_imp_unbounded cobounded_outside by blast
  1877 
  1878 lemma bounded_inside:
  1879     fixes s :: "'a::{real_normed_vector, perfect_space} set"
  1880     shows "bounded s \<Longrightarrow> bounded(inside s)"
  1881   by (simp add: bounded_Int cobounded_outside inside_outside)
  1882 
  1883 lemma connected_outside:
  1884     fixes s :: "'a::euclidean_space set"
  1885     assumes "bounded s" "2 \<le> DIM('a)"
  1886       shows "connected(outside s)"
  1887   apply (simp add: connected_iff_connected_component, clarify)
  1888   apply (simp add: outside)
  1889   apply (rule_tac s="connected_component_set (- s) x" in connected_component_of_subset)
  1890   apply (metis (no_types) assms cobounded_unbounded_component cobounded_unique_unbounded_component connected_component_eq_eq connected_component_idemp double_complement mem_Collect_eq)
  1891   apply clarify
  1892   apply (metis connected_component_eq_eq connected_component_in)
  1893   done
  1894 
  1895 lemma outside_connected_component_lt:
  1896     "outside s = {x. \<forall>B. \<exists>y. B < norm(y) \<and> connected_component (- s) x y}"
  1897 apply (auto simp: outside bounded_def dist_norm)
  1898 apply (metis diff_0 norm_minus_cancel not_less)
  1899 by (metis less_diff_eq norm_minus_commute norm_triangle_ineq2 order.trans pinf(6))
  1900 
  1901 lemma outside_connected_component_le:
  1902    "outside s =
  1903             {x. \<forall>B. \<exists>y. B \<le> norm(y) \<and>
  1904                          connected_component (- s) x y}"
  1905 apply (simp add: outside_connected_component_lt)
  1906 apply (simp add: Set.set_eq_iff)
  1907 by (meson gt_ex leD le_less_linear less_imp_le order.trans)
  1908 
  1909 lemma not_outside_connected_component_lt:
  1910     fixes s :: "'a::euclidean_space set"
  1911     assumes s: "bounded s" and "2 \<le> DIM('a)"
  1912       shows "- (outside s) = {x. \<forall>B. \<exists>y. B < norm(y) \<and> ~ (connected_component (- s) x y)}"
  1913 proof -
  1914   obtain B::real where B: "0 < B" and Bno: "\<And>x. x \<in> s \<Longrightarrow> norm x \<le> B"
  1915     using s [simplified bounded_pos] by auto
  1916   { fix y::'a and z::'a
  1917     assume yz: "B < norm z" "B < norm y"
  1918     have "connected_component (- cball 0 B) y z"
  1919       apply (rule connected_componentI [OF _ subset_refl])
  1920       apply (rule connected_complement_bounded_convex)
  1921       using assms yz
  1922       by (auto simp: dist_norm)
  1923     then have "connected_component (- s) y z"
  1924       apply (rule connected_component_of_subset)
  1925       apply (metis Bno Compl_anti_mono mem_cball_0 subset_iff)
  1926       done
  1927   } note cyz = this
  1928   show ?thesis
  1929     apply (auto simp: outside)
  1930     apply (metis Compl_iff bounded_iff cobounded_imp_unbounded mem_Collect_eq not_le)
  1931     apply (simp add: bounded_pos)
  1932     by (metis B connected_component_trans cyz not_le)
  1933 qed
  1934 
  1935 lemma not_outside_connected_component_le:
  1936     fixes s :: "'a::euclidean_space set"
  1937     assumes s: "bounded s"  "2 \<le> DIM('a)"
  1938       shows "- (outside s) = {x. \<forall>B. \<exists>y. B \<le> norm(y) \<and> ~ (connected_component (- s) x y)}"
  1939 apply (auto intro: less_imp_le simp: not_outside_connected_component_lt [OF assms])
  1940 by (meson gt_ex less_le_trans)
  1941 
  1942 lemma inside_connected_component_lt:
  1943     fixes s :: "'a::euclidean_space set"
  1944     assumes s: "bounded s"  "2 \<le> DIM('a)"
  1945       shows "inside s = {x. (x \<notin> s) \<and> (\<forall>B. \<exists>y. B < norm(y) \<and> ~(connected_component (- s) x y))}"
  1946   by (auto simp: inside_outside not_outside_connected_component_lt [OF assms])
  1947 
  1948 lemma inside_connected_component_le:
  1949     fixes s :: "'a::euclidean_space set"
  1950     assumes s: "bounded s"  "2 \<le> DIM('a)"
  1951       shows "inside s = {x. (x \<notin> s) \<and> (\<forall>B. \<exists>y. B \<le> norm(y) \<and> ~(connected_component (- s) x y))}"
  1952   by (auto simp: inside_outside not_outside_connected_component_le [OF assms])
  1953 
  1954 lemma inside_subset:
  1955   assumes "connected u" and "~bounded u" and "t \<union> u = - s"
  1956   shows "inside s \<subseteq> t"
  1957 apply (auto simp: inside_def)
  1958 by (metis bounded_subset [of "connected_component_set (- s) _"] connected_component_maximal
  1959        Compl_iff Un_iff assms subsetI)
  1960 
  1961 lemma frontier_interiors: "frontier s = - interior(s) - interior(-s)"
  1962   by (simp add: Int_commute frontier_def interior_closure)
  1963 
  1964 lemma connected_inter_frontier:
  1965      "\<lbrakk>connected s; s \<inter> t \<noteq> {}; s - t \<noteq> {}\<rbrakk> \<Longrightarrow> (s \<inter> frontier t \<noteq> {})"
  1966   apply (simp add: frontier_interiors connected_open_in, safe)
  1967   apply (drule_tac x="s \<inter> interior t" in spec, safe)
  1968    apply (drule_tac [2] x="s \<inter> interior (-t)" in spec)
  1969    apply (auto simp: disjoint_eq_subset_Compl dest: interior_subset [THEN subsetD])
  1970   done
  1971 
  1972 lemma connected_component_UNIV:
  1973     fixes x :: "'a::real_normed_vector"
  1974     shows "connected_component_set UNIV x = UNIV"
  1975 using connected_iff_eq_connected_component_set [of "UNIV::'a set"] connected_UNIV
  1976 by auto
  1977 
  1978 lemma connected_component_eq_UNIV:
  1979     fixes x :: "'a::real_normed_vector"
  1980     shows "connected_component_set s x = UNIV \<longleftrightarrow> s = UNIV"
  1981   using connected_component_in connected_component_UNIV by blast
  1982 
  1983 lemma components_univ [simp]: "components UNIV = {UNIV :: 'a::real_normed_vector set}"
  1984   by (auto simp: components_eq_sing_iff)
  1985 
  1986 lemma interior_inside_frontier:
  1987     fixes s :: "'a::real_normed_vector set"
  1988     assumes "bounded s"
  1989       shows "interior s \<subseteq> inside (frontier s)"
  1990 proof -
  1991   { fix x y
  1992     assume x: "x \<in> interior s" and y: "y \<notin> s"
  1993        and cc: "connected_component (- frontier s) x y"
  1994     have "connected_component_set (- frontier s) x \<inter> frontier s \<noteq> {}"
  1995       apply (rule connected_inter_frontier, simp)
  1996       apply (metis IntI cc connected_component_in connected_component_refl empty_iff interiorE mem_Collect_eq set_rev_mp x)
  1997       using  y cc
  1998       by blast
  1999     then have "bounded (connected_component_set (- frontier s) x)"
  2000       using connected_component_in by auto
  2001   }
  2002   then show ?thesis
  2003     apply (auto simp: inside_def frontier_def)
  2004     apply (rule classical)
  2005     apply (rule bounded_subset [OF assms], blast)
  2006     done
  2007 qed
  2008 
  2009 lemma inside_empty [simp]: "inside {} = ({} :: 'a :: {real_normed_vector, perfect_space} set)"
  2010   by (simp add: inside_def connected_component_UNIV)
  2011 
  2012 lemma outside_empty [simp]: "outside {} = (UNIV :: 'a :: {real_normed_vector, perfect_space} set)"
  2013 using inside_empty inside_union_outside by blast
  2014 
  2015 lemma inside_same_component:
  2016    "\<lbrakk>connected_component (- s) x y; x \<in> inside s\<rbrakk> \<Longrightarrow> y \<in> inside s"
  2017   using connected_component_eq connected_component_in
  2018   by (fastforce simp add: inside_def)
  2019 
  2020 lemma outside_same_component:
  2021    "\<lbrakk>connected_component (- s) x y; x \<in> outside s\<rbrakk> \<Longrightarrow> y \<in> outside s"
  2022   using connected_component_eq connected_component_in
  2023   by (fastforce simp add: outside_def)
  2024 
  2025 lemma convex_in_outside:
  2026   fixes s :: "'a :: {real_normed_vector, perfect_space} set"
  2027   assumes s: "convex s" and z: "z \<notin> s"
  2028     shows "z \<in> outside s"
  2029 proof (cases "s={}")
  2030   case True then show ?thesis by simp
  2031 next
  2032   case False then obtain a where "a \<in> s" by blast
  2033   with z have zna: "z \<noteq> a" by auto
  2034   { assume "bounded (connected_component_set (- s) z)"
  2035     with bounded_pos_less obtain B where "B>0" and B: "\<And>x. connected_component (- s) z x \<Longrightarrow> norm x < B"
  2036       by (metis mem_Collect_eq)
  2037     def C \<equiv> "((B + 1 + norm z) / norm (z-a))"
  2038     have "C > 0"
  2039       using \<open>0 < B\<close> zna by (simp add: C_def divide_simps add_strict_increasing)
  2040     have "\<bar>norm (z + C *\<^sub>R (z-a)) - norm (C *\<^sub>R (z-a))\<bar> \<le> norm z"
  2041       by (metis add_diff_cancel norm_triangle_ineq3)
  2042     moreover have "norm (C *\<^sub>R (z-a)) > norm z + B"
  2043       using zna \<open>B>0\<close> by (simp add: C_def le_max_iff_disj field_simps)
  2044     ultimately have C: "norm (z + C *\<^sub>R (z-a)) > B" by linarith
  2045     { fix u::real
  2046       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"
  2047       then have Cpos: "1 + u * C > 0"
  2048         by (meson \<open>0 < C\<close> add_pos_nonneg less_eq_real_def zero_le_mult_iff zero_less_one)
  2049       then have *: "(1 / (1 + u * C)) *\<^sub>R z + (u * C / (1 + u * C)) *\<^sub>R z = z"
  2050         by (simp add: scaleR_add_left [symmetric] divide_simps)
  2051       then have False
  2052         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
  2053         by (simp add: * divide_simps algebra_simps)
  2054     } note contra = this
  2055     have "connected_component (- s) z (z + C *\<^sub>R (z-a))"
  2056       apply (rule connected_componentI [OF connected_segment [of z "z + C *\<^sub>R (z-a)"]])
  2057       apply (simp add: closed_segment_def)
  2058       using contra
  2059       apply auto
  2060       done
  2061     then have False
  2062       using zna B [of "z + C *\<^sub>R (z-a)"] C
  2063       by (auto simp: divide_simps max_mult_distrib_right)
  2064   }
  2065   then show ?thesis
  2066     by (auto simp: outside_def z)
  2067 qed
  2068 
  2069 lemma outside_convex:
  2070   fixes s :: "'a :: {real_normed_vector, perfect_space} set"
  2071   assumes "convex s"
  2072     shows "outside s = - s"
  2073   by (metis ComplD assms convex_in_outside equalityI inside_union_outside subsetI sup.cobounded2)
  2074 
  2075 lemma inside_convex:
  2076   fixes s :: "'a :: {real_normed_vector, perfect_space} set"
  2077   shows "convex s \<Longrightarrow> inside s = {}"
  2078   by (simp add: inside_outside outside_convex)
  2079 
  2080 lemma outside_subset_convex:
  2081   fixes s :: "'a :: {real_normed_vector, perfect_space} set"
  2082   shows "\<lbrakk>convex t; s \<subseteq> t\<rbrakk> \<Longrightarrow> - t \<subseteq> outside s"
  2083   using outside_convex outside_mono by blast
  2084 
  2085 lemma outside_frontier_misses_closure:
  2086     fixes s :: "'a::real_normed_vector set"
  2087     assumes "bounded s"
  2088     shows  "outside(frontier s) \<subseteq> - closure s"
  2089   unfolding outside_inside Lattices.boolean_algebra_class.compl_le_compl_iff
  2090 proof -
  2091   { assume "interior s \<subseteq> inside (frontier s)"
  2092     hence "interior s \<union> inside (frontier s) = inside (frontier s)"
  2093       by (simp add: subset_Un_eq)
  2094     then have "closure s \<subseteq> frontier s \<union> inside (frontier s)"
  2095       using frontier_def by auto
  2096   }
  2097   then show "closure s \<subseteq> frontier s \<union> inside (frontier s)"
  2098     using interior_inside_frontier [OF assms] by blast
  2099 qed
  2100 
  2101 lemma outside_frontier_eq_complement_closure:
  2102   fixes s :: "'a :: {real_normed_vector, perfect_space} set"
  2103     assumes "bounded s" "convex s"
  2104       shows "outside(frontier s) = - closure s"
  2105 by (metis Diff_subset assms convex_closure frontier_def outside_frontier_misses_closure
  2106           outside_subset_convex subset_antisym)
  2107 
  2108 lemma inside_frontier_eq_interior:
  2109      fixes s :: "'a :: {real_normed_vector, perfect_space} set"
  2110      shows "\<lbrakk>bounded s; convex s\<rbrakk> \<Longrightarrow> inside(frontier s) = interior s"
  2111   apply (simp add: inside_outside outside_frontier_eq_complement_closure)
  2112   using closure_subset interior_subset
  2113   apply (auto simp add: frontier_def)
  2114   done
  2115 
  2116 lemma open_inside:
  2117     fixes s :: "'a::real_normed_vector set"
  2118     assumes "closed s"
  2119       shows "open (inside s)"
  2120 proof -
  2121   { fix x assume x: "x \<in> inside s"
  2122     have "open (connected_component_set (- s) x)"
  2123       using assms open_connected_component by blast
  2124     then obtain e where e: "e>0" and e: "\<And>y. dist y x < e \<longrightarrow> connected_component (- s) x y"
  2125       using dist_not_less_zero
  2126       apply (simp add: open_dist)
  2127       by (metis (no_types, lifting) Compl_iff connected_component_refl_eq inside_def mem_Collect_eq x)
  2128     then have "\<exists>e>0. ball x e \<subseteq> inside s"
  2129       by (metis e dist_commute inside_same_component mem_ball subsetI x)
  2130   }
  2131   then show ?thesis
  2132     by (simp add: open_contains_ball)
  2133 qed
  2134 
  2135 lemma open_outside:
  2136     fixes s :: "'a::real_normed_vector set"
  2137     assumes "closed s"
  2138       shows "open (outside s)"
  2139 proof -
  2140   { fix x assume x: "x \<in> outside s"
  2141     have "open (connected_component_set (- s) x)"
  2142       using assms open_connected_component by blast
  2143     then obtain e where e: "e>0" and e: "\<And>y. dist y x < e \<longrightarrow> connected_component (- s) x y"
  2144       using dist_not_less_zero
  2145       apply (simp add: open_dist)
  2146       by (metis Int_iff outside_def connected_component_refl_eq  x)
  2147     then have "\<exists>e>0. ball x e \<subseteq> outside s"
  2148       by (metis e dist_commute outside_same_component mem_ball subsetI x)
  2149   }
  2150   then show ?thesis
  2151     by (simp add: open_contains_ball)
  2152 qed
  2153 
  2154 lemma closure_inside_subset:
  2155     fixes s :: "'a::real_normed_vector set"
  2156     assumes "closed s"
  2157       shows "closure(inside s) \<subseteq> s \<union> inside s"
  2158 by (metis assms closure_minimal open_closed open_outside sup.cobounded2 union_with_inside)
  2159 
  2160 lemma frontier_inside_subset:
  2161     fixes s :: "'a::real_normed_vector set"
  2162     assumes "closed s"
  2163       shows "frontier(inside s) \<subseteq> s"
  2164 proof -
  2165   have "closure (inside s) \<inter> - inside s = closure (inside s) - interior (inside s)"
  2166     by (metis (no_types) Diff_Compl assms closure_closed interior_closure open_closed open_inside)
  2167   moreover have "- inside s \<inter> - outside s = s"
  2168     by (metis (no_types) compl_sup double_compl inside_union_outside)
  2169   moreover have "closure (inside s) \<subseteq> - outside s"
  2170     by (metis (no_types) assms closure_inside_subset union_with_inside)
  2171   ultimately have "closure (inside s) - interior (inside s) \<subseteq> s"
  2172     by blast
  2173   then show ?thesis
  2174     by (simp add: frontier_def open_inside interior_open)
  2175 qed
  2176 
  2177 lemma closure_outside_subset:
  2178     fixes s :: "'a::real_normed_vector set"
  2179     assumes "closed s"
  2180       shows "closure(outside s) \<subseteq> s \<union> outside s"
  2181   apply (rule closure_minimal, simp)
  2182   by (metis assms closed_open inside_outside open_inside)
  2183 
  2184 lemma frontier_outside_subset:
  2185     fixes s :: "'a::real_normed_vector set"
  2186     assumes "closed s"
  2187       shows "frontier(outside s) \<subseteq> s"
  2188   apply (simp add: frontier_def open_outside interior_open)
  2189   by (metis Diff_subset_conv assms closure_outside_subset interior_eq open_outside sup.commute)
  2190 
  2191 lemma inside_complement_unbounded_connected_empty:
  2192      "\<lbrakk>connected (- s); \<not> bounded (- s)\<rbrakk> \<Longrightarrow> inside s = {}"
  2193   apply (simp add: inside_def)
  2194   by (meson Compl_iff bounded_subset connected_component_maximal order_refl)
  2195 
  2196 lemma inside_bounded_complement_connected_empty:
  2197     fixes s :: "'a::{real_normed_vector, perfect_space} set"
  2198     shows "\<lbrakk>connected (- s); bounded s\<rbrakk> \<Longrightarrow> inside s = {}"
  2199   by (metis inside_complement_unbounded_connected_empty cobounded_imp_unbounded)
  2200 
  2201 lemma inside_inside:
  2202     assumes "s \<subseteq> inside t"
  2203     shows "inside s - t \<subseteq> inside t"
  2204 unfolding inside_def
  2205 proof clarify
  2206   fix x
  2207   assume x: "x \<notin> t" "x \<notin> s" and bo: "bounded (connected_component_set (- s) x)"
  2208   show "bounded (connected_component_set (- t) x)"
  2209   proof (cases "s \<inter> connected_component_set (- t) x = {}")
  2210     case True show ?thesis
  2211       apply (rule bounded_subset [OF bo])
  2212       apply (rule connected_component_maximal)
  2213       using x True apply auto
  2214       done
  2215   next
  2216     case False then show ?thesis
  2217       using assms [unfolded inside_def] x
  2218       apply (simp add: disjoint_iff_not_equal, clarify)
  2219       apply (drule subsetD, assumption, auto)
  2220       by (metis (no_types, hide_lams) ComplI connected_component_eq_eq)
  2221   qed
  2222 qed
  2223 
  2224 lemma inside_inside_subset: "inside(inside s) \<subseteq> s"
  2225   using inside_inside union_with_outside by fastforce
  2226 
  2227 lemma inside_outside_intersect_connected:
  2228       "\<lbrakk>connected t; inside s \<inter> t \<noteq> {}; outside s \<inter> t \<noteq> {}\<rbrakk> \<Longrightarrow> s \<inter> t \<noteq> {}"
  2229   apply (simp add: inside_def outside_def ex_in_conv [symmetric] disjoint_eq_subset_Compl, clarify)
  2230   by (metis (no_types, hide_lams) Compl_anti_mono connected_component_eq connected_component_maximal contra_subsetD double_compl)
  2231 
  2232 lemma outside_bounded_nonempty:
  2233   fixes s :: "'a :: {real_normed_vector, perfect_space} set"
  2234     assumes "bounded s" shows "outside s \<noteq> {}"
  2235   by (metis (no_types, lifting) Collect_empty_eq Collect_mem_eq Compl_eq_Diff_UNIV Diff_cancel
  2236                    Diff_disjoint UNIV_I assms ball_eq_empty bounded_diff cobounded_outside convex_ball
  2237                    double_complement order_refl outside_convex outside_def)
  2238 
  2239 lemma outside_compact_in_open:
  2240     fixes s :: "'a :: {real_normed_vector,perfect_space} set"
  2241     assumes s: "compact s" and t: "open t" and "s \<subseteq> t" "t \<noteq> {}"
  2242       shows "outside s \<inter> t \<noteq> {}"
  2243 proof -
  2244   have "outside s \<noteq> {}"
  2245     by (simp add: compact_imp_bounded outside_bounded_nonempty s)
  2246   with assms obtain a b where a: "a \<in> outside s" and b: "b \<in> t" by auto
  2247   show ?thesis
  2248   proof (cases "a \<in> t")
  2249     case True with a show ?thesis by blast
  2250   next
  2251     case False
  2252       have front: "frontier t \<subseteq> - s"
  2253         using \<open>s \<subseteq> t\<close> frontier_disjoint_eq t by auto
  2254       { fix \<gamma>
  2255         assume "path \<gamma>" and pimg_sbs: "path_image \<gamma> - {pathfinish \<gamma>} \<subseteq> interior (- t)"
  2256            and pf: "pathfinish \<gamma> \<in> frontier t" and ps: "pathstart \<gamma> = a"
  2257         def c \<equiv> "pathfinish \<gamma>"
  2258         have "c \<in> -s" unfolding c_def using front pf by blast
  2259         moreover have "open (-s)" using s compact_imp_closed by blast
  2260         ultimately obtain \<epsilon>::real where "\<epsilon> > 0" and \<epsilon>: "cball c \<epsilon> \<subseteq> -s"
  2261           using open_contains_cball[of "-s"] s by blast
  2262         then obtain d where "d \<in> t" and d: "dist d c < \<epsilon>"
  2263           using closure_approachable [of c t] pf unfolding c_def
  2264           by (metis Diff_iff frontier_def)
  2265         then have "d \<in> -s" using \<epsilon>
  2266           using dist_commute by (metis contra_subsetD mem_cball not_le not_less_iff_gr_or_eq)
  2267         have pimg_sbs_cos: "path_image \<gamma> \<subseteq> -s"
  2268           using pimg_sbs apply (auto simp: path_image_def)
  2269           apply (drule subsetD)
  2270           using \<open>c \<in> - s\<close> \<open>s \<subseteq> t\<close> interior_subset apply (auto simp: c_def)
  2271           done
  2272         have "closed_segment c d \<le> cball c \<epsilon>"
  2273           apply (simp add: segment_convex_hull)
  2274           apply (rule hull_minimal)
  2275           using  \<open>\<epsilon> > 0\<close> d apply (auto simp: dist_commute)
  2276           done
  2277         with \<epsilon> have "closed_segment c d \<subseteq> -s" by blast
  2278         moreover have con_gcd: "connected (path_image \<gamma> \<union> closed_segment c d)"
  2279           by (rule connected_Un) (auto simp: c_def \<open>path \<gamma>\<close> connected_path_image)
  2280         ultimately have "connected_component (- s) a d"
  2281           unfolding connected_component_def using pimg_sbs_cos ps by blast
  2282         then have "outside s \<inter> t \<noteq> {}"
  2283           using outside_same_component [OF _ a]  by (metis IntI \<open>d \<in> t\<close> empty_iff)
  2284       } note * = this
  2285       have pal: "pathstart (linepath a b) \<in> closure (- t)"
  2286         by (auto simp: False closure_def)
  2287       show ?thesis
  2288         by (rule exists_path_subpath_to_frontier [OF path_linepath pal _ *]) (auto simp: b)
  2289   qed
  2290 qed
  2291 
  2292 lemma inside_inside_compact_connected:
  2293     fixes s :: "'a :: euclidean_space set"
  2294     assumes s: "closed s" and t: "compact t" and "connected t" "s \<subseteq> inside t"
  2295       shows "inside s \<subseteq> inside t"
  2296 proof (cases "inside t = {}")
  2297   case True with assms show ?thesis by auto
  2298 next
  2299   case False
  2300   consider "DIM('a) = 1" | "DIM('a) \<ge> 2"
  2301     using antisym not_less_eq_eq by fastforce
  2302   then show ?thesis
  2303   proof cases
  2304     case 1 then show ?thesis
  2305              using connected_convex_1_gen assms False inside_convex by blast
  2306   next
  2307     case 2
  2308     have coms: "compact s"
  2309       using assms apply (simp add: s compact_eq_bounded_closed)
  2310        by (meson bounded_inside bounded_subset compact_imp_bounded)
  2311     then have bst: "bounded (s \<union> t)"
  2312       by (simp add: compact_imp_bounded t)
  2313     then obtain r where "0 < r" and r: "s \<union> t \<subseteq> ball 0 r"
  2314       using bounded_subset_ballD by blast
  2315     have outst: "outside s \<inter> outside t \<noteq> {}"
  2316     proof -
  2317       have "- ball 0 r \<subseteq> outside s"
  2318         apply (rule outside_subset_convex)
  2319         using r by auto
  2320       moreover have "- ball 0 r \<subseteq> outside t"
  2321         apply (rule outside_subset_convex)
  2322         using r by auto
  2323       ultimately show ?thesis
  2324         by (metis Compl_subset_Compl_iff Int_subset_iff bounded_ball inf.orderE outside_bounded_nonempty outside_no_overlap)
  2325     qed
  2326     have "s \<inter> t = {}" using assms
  2327       by (metis disjoint_iff_not_equal inside_no_overlap subsetCE)
  2328     moreover have "outside s \<inter> inside t \<noteq> {}"
  2329       by (meson False assms(4) compact_eq_bounded_closed coms open_inside outside_compact_in_open t)
  2330     ultimately have "inside s \<inter> t = {}"
  2331       using inside_outside_intersect_connected [OF \<open>connected t\<close>, of s]
  2332       by (metis "2" compact_eq_bounded_closed coms connected_outside inf.commute inside_outside_intersect_connected outst)
  2333     then show ?thesis
  2334       using inside_inside [OF \<open>s \<subseteq> inside t\<close>] by blast
  2335   qed
  2336 qed
  2337 
  2338 lemma connected_with_inside:
  2339     fixes s :: "'a :: real_normed_vector set"
  2340     assumes s: "closed s" and cons: "connected s"
  2341       shows "connected(s \<union> inside s)"
  2342 proof (cases "s \<union> inside s = UNIV")
  2343   case True with assms show ?thesis by auto
  2344 next
  2345   case False
  2346   then obtain b where b: "b \<notin> s" "b \<notin> inside s" by blast
  2347   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
  2348   using that proof
  2349     assume "a \<in> s" then show ?thesis
  2350       apply (rule_tac x=a in exI)
  2351       apply (rule_tac x="{a}" in exI)
  2352       apply (simp add:)
  2353       done
  2354   next
  2355     assume a: "a \<in> inside s"
  2356     show ?thesis
  2357       apply (rule exists_path_subpath_to_frontier [OF path_linepath [of a b], of "inside s"])
  2358       using a apply (simp add: closure_def)
  2359       apply (simp add: b)
  2360       apply (rule_tac x="pathfinish h" in exI)
  2361       apply (rule_tac x="path_image h" in exI)
  2362       apply (simp add: pathfinish_in_path_image connected_path_image, auto)
  2363       using frontier_inside_subset s apply fastforce
  2364       by (metis (no_types, lifting) frontier_inside_subset insertE insert_Diff interior_eq open_inside pathfinish_in_path_image s subsetCE)
  2365   qed
  2366   show ?thesis
  2367     apply (simp add: connected_iff_connected_component)
  2368     apply (simp add: connected_component_def)
  2369     apply (clarify dest!: *)
  2370     apply (rename_tac u u' t t')
  2371     apply (rule_tac x="(s \<union> t \<union> t')" in exI)
  2372     apply (auto simp: intro!: connected_Un cons)
  2373     done
  2374 qed
  2375 
  2376 text\<open>The proof is virtually the same as that above.\<close>
  2377 lemma connected_with_outside:
  2378     fixes s :: "'a :: real_normed_vector set"
  2379     assumes s: "closed s" and cons: "connected s"
  2380       shows "connected(s \<union> outside s)"
  2381 proof (cases "s \<union> outside s = UNIV")
  2382   case True with assms show ?thesis by auto
  2383 next
  2384   case False
  2385   then obtain b where b: "b \<notin> s" "b \<notin> outside s" by blast
  2386   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
  2387   using that proof
  2388     assume "a \<in> s" then show ?thesis
  2389       apply (rule_tac x=a in exI)
  2390       apply (rule_tac x="{a}" in exI)
  2391       apply (simp add:)
  2392       done
  2393   next
  2394     assume a: "a \<in> outside s"
  2395     show ?thesis
  2396       apply (rule exists_path_subpath_to_frontier [OF path_linepath [of a b], of "outside s"])
  2397       using a apply (simp add: closure_def)
  2398       apply (simp add: b)
  2399       apply (rule_tac x="pathfinish h" in exI)
  2400       apply (rule_tac x="path_image h" in exI)
  2401       apply (simp add: pathfinish_in_path_image connected_path_image, auto)
  2402       using frontier_outside_subset s apply fastforce
  2403       by (metis (no_types, lifting) frontier_outside_subset insertE insert_Diff interior_eq open_outside pathfinish_in_path_image s subsetCE)
  2404   qed
  2405   show ?thesis
  2406     apply (simp add: connected_iff_connected_component)
  2407     apply (simp add: connected_component_def)
  2408     apply (clarify dest!: *)
  2409     apply (rename_tac u u' t t')
  2410     apply (rule_tac x="(s \<union> t \<union> t')" in exI)
  2411     apply (auto simp: intro!: connected_Un cons)
  2412     done
  2413 qed
  2414 
  2415 lemma inside_inside_eq_empty [simp]:
  2416     fixes s :: "'a :: {real_normed_vector, perfect_space} set"
  2417     assumes s: "closed s" and cons: "connected s"
  2418       shows "inside (inside s) = {}"
  2419   by (metis (no_types) unbounded_outside connected_with_outside [OF assms] bounded_Un
  2420            inside_complement_unbounded_connected_empty unbounded_outside union_with_outside)
  2421 
  2422 lemma inside_in_components:
  2423      "inside s \<in> components (- s) \<longleftrightarrow> connected(inside s) \<and> inside s \<noteq> {}"
  2424   apply (simp add: in_components_maximal)
  2425   apply (auto intro: inside_same_component connected_componentI)
  2426   apply (metis IntI empty_iff inside_no_overlap)
  2427   done
  2428 
  2429 text\<open>The proof is virtually the same as that above.\<close>
  2430 lemma outside_in_components:
  2431      "outside s \<in> components (- s) \<longleftrightarrow> connected(outside s) \<and> outside s \<noteq> {}"
  2432   apply (simp add: in_components_maximal)
  2433   apply (auto intro: outside_same_component connected_componentI)
  2434   apply (metis IntI empty_iff outside_no_overlap)
  2435   done
  2436 
  2437 lemma bounded_unique_outside:
  2438     fixes s :: "'a :: euclidean_space set"
  2439     shows "\<lbrakk>bounded s; DIM('a) \<ge> 2\<rbrakk> \<Longrightarrow> (c \<in> components (- s) \<and> ~bounded c \<longleftrightarrow> c = outside s)"
  2440   apply (rule iffI)
  2441   apply (metis cobounded_unique_unbounded_components connected_outside double_compl outside_bounded_nonempty outside_in_components unbounded_outside)
  2442   by (simp add: connected_outside outside_bounded_nonempty outside_in_components unbounded_outside)
  2443 
  2444 section\<open> Homotopy of maps p,q : X=>Y with property P of all intermediate maps.\<close>
  2445 
  2446 text\<open> We often just want to require that it fixes some subset, but to take in
  2447   the case of a loop homotopy, it's convenient to have a general property P.\<close>
  2448 
  2449 definition homotopic_with ::
  2450   "[('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> bool, 'a set, 'b set, 'a \<Rightarrow> 'b, 'a \<Rightarrow> 'b] \<Rightarrow> bool"
  2451 where
  2452  "homotopic_with P X Y p q \<equiv>
  2453    (\<exists>h:: real \<times> 'a \<Rightarrow> 'b.
  2454        continuous_on ({0..1} \<times> X) h \<and>
  2455        h ` ({0..1} \<times> X) \<subseteq> Y \<and>
  2456        (\<forall>x. h(0, x) = p x) \<and>
  2457        (\<forall>x. h(1, x) = q x) \<and>
  2458        (\<forall>t \<in> {0..1}. P(\<lambda>x. h(t, x))))"
  2459 
  2460 
  2461 text\<open> We often want to just localize the ending function equality or whatever.\<close>
  2462 proposition homotopic_with:
  2463   fixes X :: "'a::topological_space set" and Y :: "'b::topological_space set"
  2464   assumes "\<And>h k. (\<And>x. x \<in> X \<Longrightarrow> h x = k x) \<Longrightarrow> (P h \<longleftrightarrow> P k)"
  2465   shows "homotopic_with P X Y p q \<longleftrightarrow>
  2466            (\<exists>h :: real \<times> 'a \<Rightarrow> 'b.
  2467               continuous_on ({0..1} \<times> X) h \<and>
  2468               h ` ({0..1} \<times> X) \<subseteq> Y \<and>
  2469               (\<forall>x \<in> X. h(0,x) = p x) \<and>
  2470               (\<forall>x \<in> X. h(1,x) = q x) \<and>
  2471               (\<forall>t \<in> {0..1}. P(\<lambda>x. h(t, x))))"
  2472   unfolding homotopic_with_def
  2473   apply (rule iffI, blast, clarify)
  2474   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)
  2475   apply (auto simp:)
  2476   apply (force elim: continuous_on_eq)
  2477   apply (drule_tac x=t in bspec, force)
  2478   apply (subst assms; simp)
  2479   done
  2480 
  2481 proposition homotopic_with_eq:
  2482    assumes h: "homotopic_with P X Y f g"
  2483        and f': "\<And>x. x \<in> X \<Longrightarrow> f' x = f x"
  2484        and g': "\<And>x. x \<in> X \<Longrightarrow> g' x = g x"
  2485        and P:  "(\<And>h k. (\<And>x. x \<in> X \<Longrightarrow> h x = k x) \<Longrightarrow> (P h \<longleftrightarrow> P k))"
  2486    shows "homotopic_with P X Y f' g'"
  2487   using h unfolding homotopic_with_def
  2488   apply safe
  2489   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)
  2490   apply (simp add: f' g', safe)
  2491   apply (fastforce intro: continuous_on_eq)
  2492   apply fastforce
  2493   apply (subst P; fastforce)
  2494   done
  2495 
  2496 proposition homotopic_with_equal:
  2497    assumes contf: "continuous_on X f" and fXY: "f ` X \<subseteq> Y"
  2498        and gf: "\<And>x. x \<in> X \<Longrightarrow> g x = f x"
  2499        and P:  "P f" "P g"
  2500    shows "homotopic_with P X Y f g"
  2501   unfolding homotopic_with_def
  2502   apply (rule_tac x="\<lambda>(u,v). if u = 1 then g v else f v" in exI)
  2503   using assms
  2504   apply (intro conjI)
  2505   apply (rule continuous_on_eq [where f = "f o snd"])
  2506   apply (rule continuous_intros | force)+
  2507   apply clarify
  2508   apply (case_tac "t=1"; force)
  2509   done
  2510 
  2511 
  2512 lemma image_Pair_const: "(\<lambda>x. (x, c)) ` A = A \<times> {c}"
  2513   by (auto simp:)
  2514 
  2515 lemma homotopic_constant_maps:
  2516    "homotopic_with (\<lambda>x. True) s t (\<lambda>x. a) (\<lambda>x. b) \<longleftrightarrow> s = {} \<or> path_component t a b"
  2517 proof (cases "s = {} \<or> t = {}")
  2518   case True with continuous_on_const show ?thesis
  2519     by (auto simp: homotopic_with path_component_def)
  2520 next
  2521   case False
  2522   then obtain c where "c \<in> s" by blast
  2523   show ?thesis
  2524   proof
  2525     assume "homotopic_with (\<lambda>x. True) s t (\<lambda>x. a) (\<lambda>x. b)"
  2526     then obtain h :: "real \<times> 'a \<Rightarrow> 'b"
  2527         where conth: "continuous_on ({0..1} \<times> s) h"
  2528           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)"
  2529       by (auto simp: homotopic_with)
  2530     have "continuous_on {0..1} (h \<circ> (\<lambda>t. (t, c)))"
  2531       apply (rule continuous_intros conth | simp add: image_Pair_const)+
  2532       apply (blast intro:  \<open>c \<in> s\<close> continuous_on_subset [OF conth] )
  2533       done
  2534     with \<open>c \<in> s\<close> h show "s = {} \<or> path_component t a b"
  2535       apply (simp_all add: homotopic_with path_component_def)
  2536       apply (auto simp:)
  2537       apply (drule_tac x="h o (\<lambda>t. (t, c))" in spec)
  2538       apply (auto simp: pathstart_def pathfinish_def path_image_def path_def)
  2539       done
  2540   next
  2541     assume "s = {} \<or> path_component t a b"
  2542     with False show "homotopic_with (\<lambda>x. True) s t (\<lambda>x. a) (\<lambda>x. b)"
  2543       apply (clarsimp simp: homotopic_with path_component_def pathstart_def pathfinish_def path_image_def path_def)
  2544       apply (rule_tac x="g o fst" in exI)
  2545       apply (rule conjI continuous_intros | force)+
  2546       done
  2547   qed
  2548 qed
  2549 
  2550 
  2551 subsection\<open> Trivial properties.\<close>
  2552 
  2553 lemma homotopic_with_imp_property: "homotopic_with P X Y f g \<Longrightarrow> P f \<and> P g"
  2554   unfolding homotopic_with_def Ball_def
  2555   apply clarify
  2556   apply (frule_tac x=0 in spec)
  2557   apply (drule_tac x=1 in spec)
  2558   apply (auto simp:)
  2559   done
  2560 
  2561 lemma continuous_on_o_Pair: "\<lbrakk>continuous_on (T \<times> X) h; t \<in> T\<rbrakk> \<Longrightarrow> continuous_on X (h o Pair t)"
  2562   by (fast intro: continuous_intros elim!: continuous_on_subset)
  2563 
  2564 lemma homotopic_with_imp_continuous:
  2565     assumes "homotopic_with P X Y f g"
  2566     shows "continuous_on X f \<and> continuous_on X g"
  2567 proof -
  2568   obtain h :: "real \<times> 'a \<Rightarrow> 'b"
  2569     where conth: "continuous_on ({0..1} \<times> X) h"
  2570       and h: "\<forall>x. h (0, x) = f x" "\<forall>x. h (1, x) = g x"
  2571     using assms by (auto simp: homotopic_with_def)
  2572   have *: "t \<in> {0..1} \<Longrightarrow> continuous_on X (h o (\<lambda>x. (t,x)))" for t
  2573     by (rule continuous_intros continuous_on_subset [OF conth] | force)+
  2574   show ?thesis
  2575     using h *[of 0] *[of 1] by auto
  2576 qed
  2577 
  2578 proposition homotopic_with_imp_subset1:
  2579      "homotopic_with P X Y f g \<Longrightarrow> f ` X \<subseteq> Y"
  2580   by (simp add: homotopic_with_def image_subset_iff) (metis atLeastAtMost_iff order_refl zero_le_one)
  2581 
  2582 proposition homotopic_with_imp_subset2:
  2583      "homotopic_with P X Y f g \<Longrightarrow> g ` X \<subseteq> Y"
  2584   by (simp add: homotopic_with_def image_subset_iff) (metis atLeastAtMost_iff order_refl zero_le_one)
  2585 
  2586 proposition homotopic_with_mono:
  2587     assumes hom: "homotopic_with P X Y f g"
  2588         and Q: "\<And>h. \<lbrakk>continuous_on X h; image h X \<subseteq> Y \<and> P h\<rbrakk> \<Longrightarrow> Q h"
  2589       shows "homotopic_with Q X Y f g"
  2590   using hom
  2591   apply (simp add: homotopic_with_def)
  2592   apply (erule ex_forward)
  2593   apply (force simp: intro!: Q dest: continuous_on_o_Pair)
  2594   done
  2595 
  2596 proposition homotopic_with_subset_left:
  2597      "\<lbrakk>homotopic_with P X Y f g; Z \<subseteq> X\<rbrakk> \<Longrightarrow> homotopic_with P Z Y f g"
  2598   apply (simp add: homotopic_with_def)
  2599   apply (fast elim!: continuous_on_subset ex_forward)
  2600   done
  2601 
  2602 proposition homotopic_with_subset_right:
  2603      "\<lbrakk>homotopic_with P X Y f g; Y \<subseteq> Z\<rbrakk> \<Longrightarrow> homotopic_with P X Z f g"
  2604   apply (simp add: homotopic_with_def)
  2605   apply (fast elim!: continuous_on_subset ex_forward)
  2606   done
  2607 
  2608 proposition homotopic_with_compose_continuous_right:
  2609     "\<lbrakk>homotopic_with (\<lambda>f. p (f \<circ> h)) X Y f g; continuous_on W h; h ` W \<subseteq> X\<rbrakk>
  2610      \<Longrightarrow> homotopic_with p W Y (f o h) (g o h)"
  2611   apply (clarsimp simp add: homotopic_with_def)
  2612   apply (rename_tac k)
  2613   apply (rule_tac x="k o (\<lambda>y. (fst y, h (snd y)))" in exI)
  2614   apply (rule conjI continuous_intros continuous_on_compose [where f=snd and g=h, unfolded o_def] | simp)+
  2615   apply (erule continuous_on_subset)
  2616   apply (fastforce simp: o_def)+
  2617   done
  2618 
  2619 proposition homotopic_compose_continuous_right:
  2620      "\<lbrakk>homotopic_with (\<lambda>f. True) X Y f g; continuous_on W h; h ` W \<subseteq> X\<rbrakk>
  2621       \<Longrightarrow> homotopic_with (\<lambda>f. True) W Y (f o h) (g o h)"
  2622   using homotopic_with_compose_continuous_right by fastforce
  2623 
  2624 proposition homotopic_with_compose_continuous_left:
  2625      "\<lbrakk>homotopic_with (\<lambda>f. p (h \<circ> f)) X Y f g; continuous_on Y h; h ` Y \<subseteq> Z\<rbrakk>
  2626       \<Longrightarrow> homotopic_with p X Z (h o f) (h o g)"
  2627   apply (clarsimp simp add: homotopic_with_def)
  2628   apply (rename_tac k)
  2629   apply (rule_tac x="h o k" in exI)
  2630   apply (rule conjI continuous_intros continuous_on_compose [where f=snd and g=h, unfolded o_def] | simp)+
  2631   apply (erule continuous_on_subset)
  2632   apply (fastforce simp: o_def)+
  2633   done
  2634 
  2635 proposition homotopic_compose_continuous_left:
  2636    "homotopic_with (\<lambda>f. True) X Y f g \<and>
  2637         continuous_on Y h \<and> image h Y \<subseteq> Z
  2638         \<Longrightarrow> homotopic_with (\<lambda>f. True) X Z (h o f) (h o g)"
  2639   using homotopic_with_compose_continuous_left by fastforce
  2640 
  2641 proposition homotopic_with_Pair:
  2642    assumes hom: "homotopic_with p s t f g" "homotopic_with p' s' t' f' g'"
  2643        and q: "\<And>f g. \<lbrakk>p f; p' g\<rbrakk> \<Longrightarrow> q(\<lambda>(x,y). (f x, g y))"
  2644      shows "homotopic_with q (s \<times> s') (t \<times> t')
  2645                   (\<lambda>(x,y). (f x, f' y)) (\<lambda>(x,y). (g x, g' y))"
  2646   using hom
  2647   apply (clarsimp simp add: homotopic_with_def)
  2648   apply (rename_tac k k')
  2649   apply (rule_tac x="\<lambda>z. ((k o (\<lambda>x. (fst x, fst (snd x)))) z, (k' o (\<lambda>x. (fst x, snd (snd x)))) z)" in exI)
  2650   apply (rule conjI continuous_intros | erule continuous_on_subset | clarsimp)+
  2651   apply (auto intro!: q [unfolded case_prod_unfold])
  2652   done
  2653 
  2654 lemma homotopic_on_empty: "homotopic_with (\<lambda>x. True) {} t f g"
  2655   by (metis continuous_on_def empty_iff homotopic_with_equal image_subset_iff)
  2656 
  2657 
  2658 text\<open>Homotopy with P is an equivalence relation (on continuous functions mapping X into Y that satisfy P,
  2659      though this only affects reflexivity.\<close>
  2660 
  2661 
  2662 proposition homotopic_with_refl:
  2663    "homotopic_with P X Y f f \<longleftrightarrow> continuous_on X f \<and> image f X \<subseteq> Y \<and> P f"
  2664   apply (rule iffI)
  2665   using homotopic_with_imp_continuous homotopic_with_imp_property homotopic_with_imp_subset2 apply blast
  2666   apply (simp add: homotopic_with_def)
  2667   apply (rule_tac x="f o snd" in exI)
  2668   apply (rule conjI continuous_intros | force)+
  2669   done
  2670 
  2671 lemma homotopic_with_symD:
  2672   fixes X :: "'a::real_normed_vector set"
  2673     assumes "homotopic_with P X Y f g"
  2674       shows "homotopic_with P X Y g f"
  2675   using assms
  2676   apply (clarsimp simp add: homotopic_with_def)
  2677   apply (rename_tac h)
  2678   apply (rule_tac x="h o (\<lambda>y. (1 - fst y, snd y))" in exI)
  2679   apply (rule conjI continuous_intros | erule continuous_on_subset | force simp add: image_subset_iff)+
  2680   done
  2681 
  2682 proposition homotopic_with_sym:
  2683     fixes X :: "'a::real_normed_vector set"
  2684     shows "homotopic_with P X Y f g \<longleftrightarrow> homotopic_with P X Y g f"
  2685   using homotopic_with_symD by blast
  2686 
  2687 lemma split_01: "{0..1::real} = {0..1/2} \<union> {1/2..1}"
  2688   by force
  2689 
  2690 lemma split_01_prod: "{0..1::real} \<times> X = ({0..1/2} \<times> X) \<union> ({1/2..1} \<times> X)"
  2691   by force
  2692 
  2693 proposition homotopic_with_trans:
  2694     fixes X :: "'a::real_normed_vector set"
  2695     assumes "homotopic_with P X Y f g" and "homotopic_with P X Y g h"
  2696       shows "homotopic_with P X Y f h"
  2697 proof -
  2698   have clo1: "closedin (subtopology euclidean ({0..1/2} \<times> X \<union> {1/2..1} \<times> X)) ({0..1/2::real} \<times> X)"
  2699     apply (simp add: closedin_closed split_01_prod [symmetric])
  2700     apply (rule_tac x="{0..1/2} \<times> UNIV" in exI)
  2701     apply (force simp add: closed_Times)
  2702     done
  2703   have clo2: "closedin (subtopology euclidean ({0..1/2} \<times> X \<union> {1/2..1} \<times> X)) ({1/2..1::real} \<times> X)"
  2704     apply (simp add: closedin_closed split_01_prod [symmetric])
  2705     apply (rule_tac x="{1/2..1} \<times> UNIV" in exI)
  2706     apply (force simp add: closed_Times)
  2707     done
  2708   { fix k1 k2:: "real \<times> 'a \<Rightarrow> 'b"
  2709     assume cont: "continuous_on ({0..1} \<times> X) k1" "continuous_on ({0..1} \<times> X) k2"
  2710        and Y: "k1 ` ({0..1} \<times> X) \<subseteq> Y" "k2 ` ({0..1} \<times> X) \<subseteq> Y"
  2711        and geq: "\<forall>x. k1 (1, x) = g x" "\<forall>x. k2 (0, x) = g x"
  2712        and k12: "\<forall>x. k1 (0, x) = f x" "\<forall>x. k2 (1, x) = h x"
  2713        and P:   "\<forall>t\<in>{0..1}. P (\<lambda>x. k1 (t, x))" "\<forall>t\<in>{0..1}. P (\<lambda>x. k2 (t, x))"
  2714     def k \<equiv> "\<lambda>y. if fst y \<le> 1 / 2 then (k1 o (\<lambda>x. (2 *\<^sub>R fst x, snd x))) y
  2715                                    else (k2 o (\<lambda>x. (2 *\<^sub>R fst x -1, snd x))) y"
  2716     have keq: "k1 (2 * u, v) = k2 (2 * u - 1, v)" if "u = 1/2"  for u v
  2717       by (simp add: geq that)
  2718     have "continuous_on ({0..1} \<times> X) k"
  2719       using cont
  2720       apply (simp add: split_01_prod k_def)
  2721       apply (rule clo1 clo2 continuous_on_cases_local continuous_intros | erule continuous_on_subset | simp add: linear image_subset_iff)+
  2722       apply (force simp add: keq)
  2723       done
  2724     moreover have "k ` ({0..1} \<times> X) \<subseteq> Y"
  2725       using Y by (force simp add: k_def)
  2726     moreover have "\<forall>x. k (0, x) = f x"
  2727       by (simp add: k_def k12)
  2728     moreover have "(\<forall>x. k (1, x) = h x)"
  2729       by (simp add: k_def k12)
  2730     moreover have "\<forall>t\<in>{0..1}. P (\<lambda>x. k (t, x))"
  2731       using P
  2732       apply (clarsimp simp add: k_def)
  2733       apply (case_tac "t \<le> 1/2")
  2734       apply (auto simp:)
  2735       done
  2736     ultimately have *: "\<exists>k :: real \<times> 'a \<Rightarrow> 'b.
  2737                        continuous_on ({0..1} \<times> X) k \<and> k ` ({0..1} \<times> X) \<subseteq> Y \<and>
  2738                        (\<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)))"
  2739       by blast
  2740   } note * = this
  2741   show ?thesis
  2742     using assms by (auto intro: * simp add: homotopic_with_def)
  2743 qed
  2744 
  2745 proposition homotopic_compose:
  2746       fixes s :: "'a::real_normed_vector set"
  2747       shows "\<lbrakk>homotopic_with (\<lambda>x. True) s t f f'; homotopic_with (\<lambda>x. True) t u g g'\<rbrakk>
  2748              \<Longrightarrow> homotopic_with (\<lambda>x. True) s u (g o f) (g' o f')"
  2749   apply (rule homotopic_with_trans [where g = "g o f'"])
  2750   apply (metis homotopic_compose_continuous_left homotopic_with_imp_continuous homotopic_with_imp_subset1)
  2751   by (metis homotopic_compose_continuous_right homotopic_with_imp_continuous homotopic_with_imp_subset2)
  2752 
  2753 
  2754 subsection\<open>Homotopy of paths, maintaining the same endpoints.\<close>
  2755 
  2756 
  2757 definition homotopic_paths :: "['a set, real \<Rightarrow> 'a, real \<Rightarrow> 'a::topological_space] \<Rightarrow> bool"
  2758   where
  2759      "homotopic_paths s p q \<equiv>
  2760        homotopic_with (\<lambda>r. pathstart r = pathstart p \<and> pathfinish r = pathfinish p) {0..1} s p q"
  2761 
  2762 lemma homotopic_paths:
  2763    "homotopic_paths s p q \<longleftrightarrow>
  2764       (\<exists>h. continuous_on ({0..1} \<times> {0..1}) h \<and>
  2765           h ` ({0..1} \<times> {0..1}) \<subseteq> s \<and>
  2766           (\<forall>x \<in> {0..1}. h(0,x) = p x) \<and>
  2767           (\<forall>x \<in> {0..1}. h(1,x) = q x) \<and>
  2768           (\<forall>t \<in> {0..1::real}. pathstart(h o Pair t) = pathstart p \<and>
  2769                         pathfinish(h o Pair t) = pathfinish p))"
  2770   by (auto simp: homotopic_paths_def homotopic_with pathstart_def pathfinish_def)
  2771 
  2772 proposition homotopic_paths_imp_pathstart:
  2773      "homotopic_paths s p q \<Longrightarrow> pathstart p = pathstart q"
  2774   by (metis (mono_tags, lifting) homotopic_paths_def homotopic_with_imp_property)
  2775 
  2776 proposition homotopic_paths_imp_pathfinish:
  2777      "homotopic_paths s p q \<Longrightarrow> pathfinish p = pathfinish q"
  2778   by (metis (mono_tags, lifting) homotopic_paths_def homotopic_with_imp_property)
  2779 
  2780 lemma homotopic_paths_imp_path:
  2781      "homotopic_paths s p q \<Longrightarrow> path p \<and> path q"
  2782   using homotopic_paths_def homotopic_with_imp_continuous path_def by blast
  2783 
  2784 lemma homotopic_paths_imp_subset:
  2785      "homotopic_paths s p q \<Longrightarrow> path_image p \<subseteq> s \<and> path_image q \<subseteq> s"
  2786   by (simp add: homotopic_paths_def homotopic_with_imp_subset1 homotopic_with_imp_subset2 path_image_def)
  2787 
  2788 proposition homotopic_paths_refl [simp]: "homotopic_paths s p p \<longleftrightarrow> path p \<and> path_image p \<subseteq> s"
  2789 by (simp add: homotopic_paths_def homotopic_with_refl path_def path_image_def)
  2790 
  2791 proposition homotopic_paths_sym: "homotopic_paths s p q \<Longrightarrow> homotopic_paths s q p"
  2792   by (metis (mono_tags) homotopic_paths_def homotopic_paths_imp_pathfinish homotopic_paths_imp_pathstart homotopic_with_symD)
  2793 
  2794 proposition homotopic_paths_sym_eq: "homotopic_paths s p q \<longleftrightarrow> homotopic_paths s q p"
  2795   by (metis homotopic_paths_sym)
  2796 
  2797 proposition homotopic_paths_trans [trans]:
  2798      "\<lbrakk>homotopic_paths s p q; homotopic_paths s q r\<rbrakk> \<Longrightarrow> homotopic_paths s p r"
  2799   apply (simp add: homotopic_paths_def)
  2800   apply (rule homotopic_with_trans, assumption)
  2801   by (metis (mono_tags, lifting) homotopic_with_imp_property homotopic_with_mono)
  2802 
  2803 proposition homotopic_paths_eq:
  2804      "\<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"
  2805   apply (simp add: homotopic_paths_def)
  2806   apply (rule homotopic_with_eq)
  2807   apply (auto simp: path_def homotopic_with_refl pathstart_def pathfinish_def path_image_def elim: continuous_on_eq)
  2808   done
  2809 
  2810 proposition homotopic_paths_reparametrize:
  2811   assumes "path p"
  2812       and pips: "path_image p \<subseteq> s"
  2813       and contf: "continuous_on {0..1} f"
  2814       and f01:"f ` {0..1} \<subseteq> {0..1}"
  2815       and [simp]: "f(0) = 0" "f(1) = 1"
  2816       and q: "\<And>t. t \<in> {0..1} \<Longrightarrow> q(t) = p(f t)"
  2817     shows "homotopic_paths s p q"
  2818 proof -
  2819   have contp: "continuous_on {0..1} p"
  2820     by (metis \<open>path p\<close> path_def)
  2821   then have "continuous_on {0..1} (p o f)"
  2822     using contf continuous_on_compose continuous_on_subset f01 by blast
  2823   then have "path q"
  2824     by (simp add: path_def) (metis q continuous_on_cong)
  2825   have piqs: "path_image q \<subseteq> s"
  2826     by (metis (no_types, hide_lams) pips f01 image_subset_iff path_image_def q)
  2827   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"
  2828     using f01 by force
  2829   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
  2830     using f01 [THEN subsetD, of "f b"] by (simp add: convex_bound_le)
  2831   have "homotopic_paths s q p"
  2832   proof (rule homotopic_paths_trans)
  2833     show "homotopic_paths s q (p \<circ> f)"
  2834       using q by (force intro: homotopic_paths_eq [OF  \<open>path q\<close> piqs])
  2835   next
  2836     show "homotopic_paths s (p \<circ> f) p"
  2837       apply (simp add: homotopic_paths_def homotopic_with_def)
  2838       apply (rule_tac x="p o (\<lambda>y. (1 - (fst y)) *\<^sub>R ((f o snd) y) + (fst y) *\<^sub>R snd y)"  in exI)
  2839       apply (rule conjI contf continuous_intros continuous_on_subset [OF contp] | simp)+
  2840       using pips [unfolded path_image_def]
  2841       apply (auto simp: fb0 fb1 pathstart_def pathfinish_def)
  2842       done
  2843   qed
  2844   then show ?thesis
  2845     by (simp add: homotopic_paths_sym)
  2846 qed
  2847 
  2848 lemma homotopic_paths_subset: "\<lbrakk>homotopic_paths s p q; s \<subseteq> t\<rbrakk> \<Longrightarrow> homotopic_paths t p q"
  2849   using homotopic_paths_def homotopic_with_subset_right by blast
  2850 
  2851 
  2852 text\<open> A slightly ad-hoc but useful lemma in constructing homotopies.\<close>
  2853 lemma homotopic_join_lemma:
  2854   fixes q :: "[real,real] \<Rightarrow> 'a::topological_space"
  2855   assumes p: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>y. p (fst y) (snd y))"
  2856       and q: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>y. q (fst y) (snd y))"
  2857       and pf: "\<And>t. t \<in> {0..1} \<Longrightarrow> pathfinish(p t) = pathstart(q t)"
  2858     shows "continuous_on ({0..1} \<times> {0..1}) (\<lambda>y. (p(fst y) +++ q(fst y)) (snd y))"
  2859 proof -
  2860   have 1: "(\<lambda>y. p (fst y) (2 * snd y)) = (\<lambda>y. p (fst y) (snd y)) o (\<lambda>y. (fst y, 2 * snd y))"
  2861     by (rule ext) (simp )
  2862   have 2: "(\<lambda>y. q (fst y) (2 * snd y - 1)) = (\<lambda>y. q (fst y) (snd y)) o (\<lambda>y. (fst y, 2 * snd y - 1))"
  2863     by (rule ext) (simp )
  2864   show ?thesis
  2865     apply (simp add: joinpaths_def)
  2866     apply (rule continuous_on_cases_le)
  2867     apply (simp_all only: 1 2)
  2868     apply (rule continuous_intros continuous_on_subset [OF p] continuous_on_subset [OF q] | force)+
  2869     using pf
  2870     apply (auto simp: mult.commute pathstart_def pathfinish_def)
  2871     done
  2872 qed
  2873 
  2874 text\<open> Congruence properties of homotopy w.r.t. path-combining operations.\<close>
  2875 
  2876 lemma homotopic_paths_reversepath_D:
  2877       assumes "homotopic_paths s p q"
  2878       shows   "homotopic_paths s (reversepath p) (reversepath q)"
  2879   using assms
  2880   apply (simp add: homotopic_paths_def homotopic_with_def, clarify)
  2881   apply (rule_tac x="h o (\<lambda>x. (fst x, 1 - snd x))" in exI)
  2882   apply (rule conjI continuous_intros)+
  2883   apply (auto simp: reversepath_def pathstart_def pathfinish_def elim!: continuous_on_subset)
  2884   done
  2885 
  2886 proposition homotopic_paths_reversepath:
  2887      "homotopic_paths s (reversepath p) (reversepath q) \<longleftrightarrow> homotopic_paths s p q"
  2888   using homotopic_paths_reversepath_D by force
  2889 
  2890 
  2891 proposition homotopic_paths_join:
  2892     "\<lbrakk>homotopic_paths s p p'; homotopic_paths s q q'; pathfinish p = pathstart q\<rbrakk> \<Longrightarrow> homotopic_paths s (p +++ q) (p' +++ q')"
  2893   apply (simp add: homotopic_paths_def homotopic_with_def, clarify)
  2894   apply (rename_tac k1 k2)
  2895   apply (rule_tac x="(\<lambda>y. ((k1 o Pair (fst y)) +++ (k2 o Pair (fst y))) (snd y))" in exI)
  2896   apply (rule conjI continuous_intros homotopic_join_lemma)+
  2897   apply (auto simp: joinpaths_def pathstart_def pathfinish_def path_image_def)
  2898   done
  2899 
  2900 proposition homotopic_paths_continuous_image:
  2901     "\<lbrakk>homotopic_paths s f g; continuous_on s h; h ` s \<subseteq> t\<rbrakk> \<Longrightarrow> homotopic_paths t (h o f) (h o g)"
  2902   unfolding homotopic_paths_def
  2903   apply (rule homotopic_with_compose_continuous_left [of _ _ _ s])
  2904   apply (auto simp: pathstart_def pathfinish_def elim!: homotopic_with_mono)
  2905   done
  2906 
  2907 subsection\<open>Group properties for homotopy of paths\<close>
  2908 
  2909 text\<open>So taking equivalence classes under homotopy would give the fundamental group\<close>
  2910 
  2911 proposition homotopic_paths_rid:
  2912     "\<lbrakk>path p; path_image p \<subseteq> s\<rbrakk> \<Longrightarrow> homotopic_paths s (p +++ linepath (pathfinish p) (pathfinish p)) p"
  2913   apply (subst homotopic_paths_sym)
  2914   apply (rule homotopic_paths_reparametrize [where f = "\<lambda>t. if  t \<le> 1 / 2 then 2 *\<^sub>R t else 1"])
  2915   apply (simp_all del: le_divide_eq_numeral1)
  2916   apply (subst split_01)
  2917   apply (rule continuous_on_cases continuous_intros | force simp: pathfinish_def joinpaths_def)+
  2918   done
  2919 
  2920 proposition homotopic_paths_lid:
  2921    "\<lbrakk>path p; path_image p \<subseteq> s\<rbrakk> \<Longrightarrow> homotopic_paths s (linepath (pathstart p) (pathstart p) +++ p) p"
  2922 using homotopic_paths_rid [of "reversepath p" s]
  2923   by (metis homotopic_paths_reversepath path_image_reversepath path_reversepath pathfinish_linepath
  2924         pathfinish_reversepath reversepath_joinpaths reversepath_linepath)
  2925 
  2926 proposition homotopic_paths_assoc:
  2927    "\<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;
  2928      pathfinish q = pathstart r\<rbrakk>
  2929     \<Longrightarrow> homotopic_paths s (p +++ (q +++ r)) ((p +++ q) +++ r)"
  2930   apply (subst homotopic_paths_sym)
  2931   apply (rule homotopic_paths_reparametrize
  2932            [where f = "\<lambda>t. if  t \<le> 1 / 2 then inverse 2 *\<^sub>R t
  2933                            else if  t \<le> 3 / 4 then t - (1 / 4)
  2934                            else 2 *\<^sub>R t - 1"])
  2935   apply (simp_all del: le_divide_eq_numeral1)
  2936   apply (simp add: subset_path_image_join)
  2937   apply (rule continuous_on_cases_1 continuous_intros)+
  2938   apply (auto simp: joinpaths_def)
  2939   done
  2940 
  2941 proposition homotopic_paths_rinv:
  2942   assumes "path p" "path_image p \<subseteq> s"
  2943     shows "homotopic_paths s (p +++ reversepath p) (linepath (pathstart p) (pathstart p))"
  2944 proof -
  2945   have "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. (subpath 0 (fst x) p +++ reversepath (subpath 0 (fst x) p)) (snd x))"
  2946     using assms
  2947     apply (simp add: joinpaths_def subpath_def reversepath_def path_def del: le_divide_eq_numeral1)
  2948     apply (rule continuous_on_cases_le)
  2949     apply (rule_tac [2] continuous_on_compose [of _ _ p, unfolded o_def])
  2950     apply (rule continuous_on_compose [of _ _ p, unfolded o_def])
  2951     apply (auto intro!: continuous_intros simp del: eq_divide_eq_numeral1)
  2952     apply (force elim!: continuous_on_subset simp add: mult_le_one)+
  2953     done
  2954   then show ?thesis
  2955     using assms
  2956     apply (subst homotopic_paths_sym_eq)
  2957     unfolding homotopic_paths_def homotopic_with_def
  2958     apply (rule_tac x="(\<lambda>y. (subpath 0 (fst y) p +++ reversepath(subpath 0 (fst y) p)) (snd y))" in exI)
  2959     apply (simp add: path_defs joinpaths_def subpath_def reversepath_def)
  2960     apply (force simp: mult_le_one)
  2961     done
  2962 qed
  2963 
  2964 proposition homotopic_paths_linv:
  2965   assumes "path p" "path_image p \<subseteq> s"
  2966     shows "homotopic_paths s (reversepath p +++ p) (linepath (pathfinish p) (pathfinish p))"
  2967 using homotopic_paths_rinv [of "reversepath p" s] assms by simp
  2968 
  2969 
  2970 subsection\<open> Homotopy of loops without requiring preservation of endpoints.\<close>
  2971 
  2972 definition homotopic_loops :: "'a::topological_space set \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> bool"  where
  2973  "homotopic_loops s p q \<equiv>
  2974      homotopic_with (\<lambda>r. pathfinish r = pathstart r) {0..1} s p q"
  2975 
  2976 lemma homotopic_loops:
  2977    "homotopic_loops s p q \<longleftrightarrow>
  2978       (\<exists>h. continuous_on ({0..1::real} \<times> {0..1}) h \<and>
  2979           image h ({0..1} \<times> {0..1}) \<subseteq> s \<and>
  2980           (\<forall>x \<in> {0..1}. h(0,x) = p x) \<and>
  2981           (\<forall>x \<in> {0..1}. h(1,x) = q x) \<and>
  2982           (\<forall>t \<in> {0..1}. pathfinish(h o Pair t) = pathstart(h o Pair t)))"
  2983   by (simp add: homotopic_loops_def pathstart_def pathfinish_def homotopic_with)
  2984 
  2985 proposition homotopic_loops_imp_loop:
  2986      "homotopic_loops s p q \<Longrightarrow> pathfinish p = pathstart p \<and> pathfinish q = pathstart q"
  2987 using homotopic_with_imp_property homotopic_loops_def by blast
  2988 
  2989 proposition homotopic_loops_imp_path:
  2990      "homotopic_loops s p q \<Longrightarrow> path p \<and> path q"
  2991   unfolding homotopic_loops_def path_def
  2992   using homotopic_with_imp_continuous by blast
  2993 
  2994 proposition homotopic_loops_imp_subset:
  2995      "homotopic_loops s p q \<Longrightarrow> path_image p \<subseteq> s \<and> path_image q \<subseteq> s"
  2996   unfolding homotopic_loops_def path_image_def
  2997   by (metis homotopic_with_imp_subset1 homotopic_with_imp_subset2)
  2998 
  2999 proposition homotopic_loops_refl:
  3000      "homotopic_loops s p p \<longleftrightarrow>
  3001       path p \<and> path_image p \<subseteq> s \<and> pathfinish p = pathstart p"
  3002   by (simp add: homotopic_loops_def homotopic_with_refl path_image_def path_def)
  3003 
  3004 proposition homotopic_loops_sym: "homotopic_loops s p q \<Longrightarrow> homotopic_loops s q p"
  3005   by (simp add: homotopic_loops_def homotopic_with_sym)
  3006 
  3007 proposition homotopic_loops_sym_eq: "homotopic_loops s p q \<longleftrightarrow> homotopic_loops s q p"
  3008   by (metis homotopic_loops_sym)
  3009 
  3010 proposition homotopic_loops_trans:
  3011    "\<lbrakk>homotopic_loops s p q; homotopic_loops s q r\<rbrakk> \<Longrightarrow> homotopic_loops s p r"
  3012   unfolding homotopic_loops_def by (blast intro: homotopic_with_trans)
  3013 
  3014 proposition homotopic_loops_subset:
  3015    "\<lbrakk>homotopic_loops s p q; s \<subseteq> t\<rbrakk> \<Longrightarrow> homotopic_loops t p q"
  3016   by (simp add: homotopic_loops_def homotopic_with_subset_right)
  3017 
  3018 proposition homotopic_loops_eq:
  3019    "\<lbrakk>path p; path_image p \<subseteq> s; pathfinish p = pathstart p; \<And>t. t \<in> {0..1} \<Longrightarrow> p(t) = q(t)\<rbrakk>
  3020           \<Longrightarrow> homotopic_loops s p q"
  3021   unfolding homotopic_loops_def
  3022   apply (rule homotopic_with_eq)
  3023   apply (rule homotopic_with_refl [where f = p, THEN iffD2])
  3024   apply (simp_all add: path_image_def path_def pathstart_def pathfinish_def)
  3025   done
  3026 
  3027 proposition homotopic_loops_continuous_image:
  3028    "\<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)"
  3029   unfolding homotopic_loops_def
  3030   apply (rule homotopic_with_compose_continuous_left)
  3031   apply (erule homotopic_with_mono)
  3032   by (simp add: pathfinish_def pathstart_def)
  3033 
  3034 
  3035 subsection\<open>Relations between the two variants of homotopy\<close>
  3036 
  3037 proposition homotopic_paths_imp_homotopic_loops:
  3038     "\<lbrakk>homotopic_paths s p q; pathfinish p = pathstart p; pathfinish q = pathstart p\<rbrakk> \<Longrightarrow> homotopic_loops s p q"
  3039   by (auto simp: homotopic_paths_def homotopic_loops_def intro: homotopic_with_mono)
  3040 
  3041 proposition homotopic_loops_imp_homotopic_paths_null:
  3042   assumes "homotopic_loops s p (linepath a a)"
  3043     shows "homotopic_paths s p (linepath (pathstart p) (pathstart p))"
  3044 proof -
  3045   have "path p" by (metis assms homotopic_loops_imp_path)
  3046   have ploop: "pathfinish p = pathstart p" by (metis assms homotopic_loops_imp_loop)
  3047   have pip: "path_image p \<subseteq> s" by (metis assms homotopic_loops_imp_subset)
  3048   obtain h where conth: "continuous_on ({0..1::real} \<times> {0..1}) h"
  3049              and hs: "h ` ({0..1} \<times> {0..1}) \<subseteq> s"
  3050              and [simp]: "\<And>x. x \<in> {0..1} \<Longrightarrow> h(0,x) = p x"
  3051              and [simp]: "\<And>x. x \<in> {0..1} \<Longrightarrow> h(1,x) = a"
  3052              and ends: "\<And>t. t \<in> {0..1} \<Longrightarrow> pathfinish (h \<circ> Pair t) = pathstart (h \<circ> Pair t)"
  3053     using assms by (auto simp: homotopic_loops homotopic_with)
  3054   have conth0: "path (\<lambda>u. h (u, 0))"
  3055     unfolding path_def
  3056     apply (rule continuous_on_compose [of _ _ h, unfolded o_def])
  3057     apply (force intro: continuous_intros continuous_on_subset [OF conth])+
  3058     done
  3059   have pih0: "path_image (\<lambda>u. h (u, 0)) \<subseteq> s"
  3060     using hs by (force simp: path_image_def)
  3061   have c1: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. h (fst x * snd x, 0))"
  3062     apply (rule continuous_on_compose [of _ _ h, unfolded o_def])
  3063     apply (force simp: mult_le_one intro: continuous_intros continuous_on_subset [OF conth])+
  3064     done
  3065   have c2: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. h (fst x - fst x * snd x, 0))"
  3066     apply (rule continuous_on_compose [of _ _ h, unfolded o_def])
  3067     apply (force simp: mult_left_le mult_le_one intro: continuous_intros continuous_on_subset [OF conth])+
  3068     apply (rule continuous_on_subset [OF conth])
  3069     apply (auto simp: algebra_simps add_increasing2 mult_left_le)
  3070     done
  3071   have [simp]: "\<And>t. \<lbrakk>0 \<le> t \<and> t \<le> 1\<rbrakk> \<Longrightarrow> h (t, 1) = h (t, 0)"
  3072     using ends by (simp add: pathfinish_def pathstart_def)
  3073   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
  3074   proof -
  3075     have "c * 3 \<le> c * (d * 4)" using that less_eq_real_def by auto
  3076     with \<open>c \<le> 1\<close> show ?thesis by fastforce
  3077   qed
  3078   have *: "\<And>p x. (path p \<and> path(reversepath p)) \<and>
  3079                   (path_image p \<subseteq> s \<and> path_image(reversepath p) \<subseteq> s) \<and>
  3080                   (pathfinish p = pathstart(linepath a a +++ reversepath p) \<and>
  3081                    pathstart(reversepath p) = a) \<and> pathstart p = x
  3082                   \<Longrightarrow> homotopic_paths s (p +++ linepath a a +++ reversepath p) (linepath x x)"
  3083     by (metis homotopic_paths_lid homotopic_paths_join
  3084               homotopic_paths_trans homotopic_paths_sym homotopic_paths_rinv)
  3085   have 1: "homotopic_paths s p (p +++ linepath (pathfinish p) (pathfinish p))"
  3086     using \<open>path p\<close> homotopic_paths_rid homotopic_paths_sym pip by blast
  3087   moreover have "homotopic_paths s (p +++ linepath (pathfinish p) (pathfinish p))
  3088                                    (linepath (pathstart p) (pathstart p) +++ p +++ linepath (pathfinish p) (pathfinish p))"
  3089     apply (rule homotopic_paths_sym)
  3090     using homotopic_paths_lid [of "p +++ linepath (pathfinish p) (pathfinish p)" s]
  3091     by (metis 1 homotopic_paths_imp_path homotopic_paths_imp_pathstart homotopic_paths_imp_subset)
  3092   moreover have "homotopic_paths s (linepath (pathstart p) (pathstart p) +++ p +++ linepath (pathfinish p) (pathfinish p))
  3093                                    ((\<lambda>u. h (u, 0)) +++ linepath a a +++ reversepath (\<lambda>u. h (u, 0)))"
  3094     apply (simp add: homotopic_paths_def homotopic_with_def)
  3095     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)
  3096     apply (simp add: subpath_reversepath)
  3097     apply (intro conjI homotopic_join_lemma)
  3098     using ploop
  3099     apply (simp_all add: path_defs joinpaths_def o_def subpath_def conth c1 c2)
  3100     apply (force simp: algebra_simps mult_le_one mult_left_le intro: hs [THEN subsetD] adhoc_le)
  3101     done
  3102   moreover have "homotopic_paths s ((\<lambda>u. h (u, 0)) +++ linepath a a +++ reversepath (\<lambda>u. h (u, 0)))
  3103                                    (linepath (pathstart p) (pathstart p))"
  3104     apply (rule *)
  3105     apply (simp add: pih0 pathstart_def pathfinish_def conth0)
  3106     apply (simp add: reversepath_def joinpaths_def)
  3107     done
  3108   ultimately show ?thesis
  3109     by (blast intro: homotopic_paths_trans)
  3110 qed
  3111 
  3112 proposition homotopic_loops_conjugate:
  3113   fixes s :: "'a::real_normed_vector set"
  3114   assumes "path p" "path q" and pip: "path_image p \<subseteq> s" and piq: "path_image q \<subseteq> s"
  3115       and papp: "pathfinish p = pathstart q" and qloop: "pathfinish q = pathstart q"
  3116     shows "homotopic_loops s (p +++ q +++ reversepath p) q"
  3117 proof -
  3118   have contp: "continuous_on {0..1} p"  using \<open>path p\<close> [unfolded path_def] by blast
  3119   have contq: "continuous_on {0..1} q"  using \<open>path q\<close> [unfolded path_def] by blast
  3120   have c1: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. p ((1 - fst x) * snd x + fst x))"
  3121     apply (rule continuous_on_compose [of _ _ p, unfolded o_def])
  3122     apply (force simp: mult_le_one intro!: continuous_intros)
  3123     apply (rule continuous_on_subset [OF contp])
  3124     apply (auto simp: algebra_simps add_increasing2 mult_right_le_one_le sum_le_prod1)
  3125     done
  3126   have c2: "continuous_on ({0..1} \<times> {0..1}) (\<lambda>x. p ((fst x - 1) * snd x + 1))"
  3127     apply (rule continuous_on_compose [of _ _ p, unfolded o_def])
  3128     apply (force simp: mult_le_one intro!: continuous_intros)
  3129     apply (rule continuous_on_subset [OF contp])
  3130     apply (auto simp: algebra_simps add_increasing2 mult_left_le_one_le)
  3131     done
  3132   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"
  3133     using sum_le_prod1
  3134     by (force simp: algebra_simps add_increasing2 mult_left_le intro: pip [unfolded path_image_def, THEN subsetD])
  3135   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"
  3136     apply (rule pip [unfolded path_image_def, THEN subsetD])
  3137     apply (rule image_eqI, blast)
  3138     apply (simp add: algebra_simps)
  3139     by (metis add_mono_thms_linordered_semiring(1) affine_ineq linear mult.commute mult.left_neutral mult_right_mono not_le
  3140               add.commute zero_le_numeral)
  3141   have qs: "\<And>a b. \<lbrakk>4 * b \<le> 3; \<not> b * 2 \<le> 1\<rbrakk> \<Longrightarrow> q (4 * b - 2) \<in> s"
  3142     using path_image_def piq by fastforce
  3143   have "homotopic_loops s (p +++ q +++ reversepath p)
  3144                           (linepath (pathstart q) (pathstart q) +++ q +++ linepath (pathstart q) (pathstart q))"
  3145     apply (simp add: homotopic_loops_def homotopic_with_def)
  3146     apply (rule_tac x="(\<lambda>y. (subpath (fst y) 1 p +++ q +++ subpath 1 (fst y) p) (snd y))" in exI)
  3147     apply (simp add: subpath_refl subpath_reversepath)
  3148     apply (intro conjI homotopic_join_lemma)
  3149     using papp qloop
  3150     apply (simp_all add: path_defs joinpaths_def o_def subpath_def c1 c2)
  3151     apply (force simp: contq intro: continuous_on_compose [of _ _ q, unfolded o_def] continuous_on_id continuous_on_snd)
  3152     apply (auto simp: ps1 ps2 qs)
  3153     done
  3154   moreover have "homotopic_loops s (linepath (pathstart q) (pathstart q) +++ q +++ linepath (pathstart q) (pathstart q)) q"
  3155   proof -
  3156     have "homotopic_paths s (linepath (pathfinish q) (pathfinish q) +++ q) q"
  3157       using \<open>path q\<close> homotopic_paths_lid qloop piq by auto
  3158     hence 1: "\<And>f. homotopic_paths s f q \<or> \<not> homotopic_paths s f (linepath (pathfinish q) (pathfinish q) +++ q)"
  3159       using homotopic_paths_trans by blast
  3160     hence "homotopic_paths s (linepath (pathfinish q) (pathfinish q) +++ q +++ linepath (pathfinish q) (pathfinish q)) q"
  3161     proof -
  3162       have "homotopic_paths s (q +++ linepath (pathfinish q) (pathfinish q)) q"
  3163         by (simp add: \<open>path q\<close> homotopic_paths_rid piq)
  3164       thus ?thesis
  3165         by (metis (no_types) 1 \<open>path q\<close> homotopic_paths_join homotopic_paths_rinv homotopic_paths_sym
  3166                   homotopic_paths_trans qloop pathfinish_linepath piq)
  3167     qed
  3168     thus ?thesis
  3169       by (metis (no_types) qloop homotopic_loops_sym homotopic_paths_imp_homotopic_loops homotopic_paths_imp_pathfinish homotopic_paths_sym)
  3170   qed
  3171   ultimately show ?thesis
  3172     by (blast intro: homotopic_loops_trans)
  3173 qed
  3174 
  3175 
  3176 subsection\<open> Homotopy of "nearby" function, paths and loops.\<close>
  3177 
  3178 lemma homotopic_with_linear:
  3179   fixes f g :: "_ \<Rightarrow> 'b::real_normed_vector"
  3180   assumes contf: "continuous_on s f"
  3181       and contg:"continuous_on s g"
  3182       and sub: "\<And>x. x \<in> s \<Longrightarrow> closed_segment (f x) (g x) \<subseteq> t"
  3183     shows "homotopic_with (\<lambda>z. True) s t f g"
  3184   apply (simp add: homotopic_with_def)
  3185   apply (rule_tac x="\<lambda>y. ((1 - (fst y)) *\<^sub>R f(snd y) + (fst y) *\<^sub>R g(snd y))" in exI)
  3186   apply (intro conjI)
  3187   apply (rule subset_refl continuous_intros continuous_on_subset [OF contf] continuous_on_compose2 [where g=f]
  3188                                             continuous_on_subset [OF contg] continuous_on_compose2 [where g=g]| simp)+
  3189   using sub closed_segment_def apply fastforce+
  3190   done
  3191 
  3192 lemma homotopic_paths_linear:
  3193   fixes g h :: "real \<Rightarrow> 'a::real_normed_vector"
  3194   assumes "path g" "path h" "pathstart h = pathstart g" "pathfinish h = pathfinish g"
  3195           "\<And>t x. t \<in> {0..1} \<Longrightarrow> closed_segment (g t) (h t) \<subseteq> s"
  3196     shows "homotopic_paths s g h"
  3197   using assms
  3198   unfolding path_def
  3199   apply (simp add: pathstart_def pathfinish_def homotopic_paths_def homotopic_with_def)
  3200   apply (rule_tac x="\<lambda>y. ((1 - (fst y)) *\<^sub>R g(snd y) + (fst y) *\<^sub>R h(snd y))" in exI)
  3201   apply (auto intro!: continuous_intros intro: continuous_on_compose2 [where g=g] continuous_on_compose2 [where g=h] )
  3202   apply (force simp: closed_segment_def)
  3203   apply (simp_all add: algebra_simps)
  3204   done
  3205 
  3206 lemma homotopic_loops_linear:
  3207   fixes g h :: "real \<Rightarrow> 'a::real_normed_vector"
  3208   assumes "path g" "path h" "pathfinish g = pathstart g" "pathfinish h = pathstart h"
  3209           "\<And>t x. t \<in> {0..1} \<Longrightarrow> closed_segment (g t) (h t) \<subseteq> s"
  3210     shows "homotopic_loops s g h"
  3211   using assms
  3212   unfolding path_def
  3213   apply (simp add: pathstart_def pathfinish_def homotopic_loops_def homotopic_with_def)
  3214   apply (rule_tac x="\<lambda>y. ((1 - (fst y)) *\<^sub>R g(snd y) + (fst y) *\<^sub>R h(snd y))" in exI)
  3215   apply (auto intro!: continuous_intros intro: continuous_on_compose2 [where g=g] continuous_on_compose2 [where g=h])
  3216   apply (force simp: closed_segment_def)
  3217   done
  3218 
  3219 lemma homotopic_paths_nearby_explicit:
  3220   assumes "path g" "path h" "pathstart h = pathstart g" "pathfinish h = pathfinish g"
  3221       and no: "\<And>t x. \<lbrakk>t \<in> {0..1}; x \<notin> s\<rbrakk> \<Longrightarrow> norm(h t - g t) < norm(g t - x)"
  3222     shows "homotopic_paths s g h"
  3223   apply (rule homotopic_paths_linear [OF assms(1-4)])
  3224   by (metis no segment_bound(1) subsetI norm_minus_commute not_le)
  3225 
  3226 lemma homotopic_loops_nearby_explicit:
  3227   assumes "path g" "path h" "pathfinish g = pathstart g" "pathfinish h = pathstart h"
  3228       and no: "\<And>t x. \<lbrakk>t \<in> {0..1}; x \<notin> s\<rbrakk> \<Longrightarrow> norm(h t - g t) < norm(g t - x)"
  3229     shows "homotopic_loops s g h"
  3230   apply (rule homotopic_loops_linear [OF assms(1-4)])
  3231   by (metis no segment_bound(1) subsetI norm_minus_commute not_le)
  3232 
  3233 lemma homotopic_nearby_paths:
  3234   fixes g h :: "real \<Rightarrow> 'a::euclidean_space"
  3235   assumes "path g" "open s" "path_image g \<subseteq> s"
  3236     shows "\<exists>e. 0 < e \<and>
  3237                (\<forall>h. path h \<and>
  3238                     pathstart h = pathstart g \<and> pathfinish h = pathfinish g \<and>
  3239                     (\<forall>t \<in> {0..1}. norm(h t - g t) < e) \<longrightarrow> homotopic_paths s g h)"
  3240 proof -
  3241   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"
  3242     using separate_compact_closed [of "path_image g" "-s"] assms by force
  3243   show ?thesis
  3244     apply (intro exI conjI)
  3245     using e [unfolded dist_norm]
  3246     apply (auto simp: intro!: homotopic_paths_nearby_explicit assms  \<open>e > 0\<close>)
  3247     by (metis atLeastAtMost_iff imageI le_less_trans not_le path_image_def)
  3248 qed
  3249 
  3250 lemma homotopic_nearby_loops:
  3251   fixes g h :: "real \<Rightarrow> 'a::euclidean_space"
  3252   assumes "path g" "open s" "path_image g \<subseteq> s" "pathfinish g = pathstart g"
  3253     shows "\<exists>e. 0 < e \<and>
  3254                (\<forall>h. path h \<and> pathfinish h = pathstart h \<and>
  3255                     (\<forall>t \<in> {0..1}. norm(h t - g t) < e) \<longrightarrow> homotopic_loops s g h)"
  3256 proof -
  3257   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"
  3258     using separate_compact_closed [of "path_image g" "-s"] assms by force
  3259   show ?thesis
  3260     apply (intro exI conjI)
  3261     using e [unfolded dist_norm]
  3262     apply (auto simp: intro!: homotopic_loops_nearby_explicit assms  \<open>e > 0\<close>)
  3263     by (metis atLeastAtMost_iff imageI le_less_trans not_le path_image_def)
  3264 qed
  3265 
  3266 subsection\<open> Homotopy and subpaths\<close>
  3267 
  3268 lemma homotopic_join_subpaths1:
  3269   assumes "path g" and pag: "path_image g \<subseteq> s"
  3270       and u: "u \<in> {0..1}" and v: "v \<in> {0..1}" and w: "w \<in> {0..1}" "u \<le> v" "v \<le> w"
  3271     shows "homotopic_paths s (subpath u v g +++ subpath v w g) (subpath u w g)"
  3272 proof -
  3273   have 1: "t * 2 \<le> 1 \<Longrightarrow> u + t * (v * 2) \<le> v + t * (u * 2)" for t
  3274     using affine_ineq \<open>u \<le> v\<close> by fastforce
  3275   have 2: "t * 2 > 1 \<Longrightarrow> u + (2*t - 1) * v \<le> v + (2*t - 1) * w" for t
  3276     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>)
  3277   have t2: "\<And>t::real. t*2 = 1 \<Longrightarrow> t = 1/2" by auto
  3278   show ?thesis
  3279     apply (rule homotopic_paths_subset [OF _ pag])
  3280     using assms
  3281     apply (cases "w = u")
  3282     using homotopic_paths_rinv [of "subpath u v g" "path_image g"]
  3283     apply (force simp: closed_segment_eq_real_ivl image_mono path_image_def subpath_refl)
  3284       apply (rule homotopic_paths_sym)
  3285       apply (rule homotopic_paths_reparametrize
  3286              [where f = "\<lambda>t. if  t \<le> 1 / 2
  3287                              then inverse((w - u)) *\<^sub>R (2 * (v - u)) *\<^sub>R t
  3288                              else inverse((w - u)) *\<^sub>R ((v - u) + (w - v) *\<^sub>R (2 *\<^sub>R t - 1))"])
  3289       using \<open>path g\<close> path_subpath u w apply blast
  3290       using \<open>path g\<close> path_image_subpath_subset u w(1) apply blast
  3291       apply simp_all
  3292       apply (subst split_01)
  3293       apply (rule continuous_on_cases continuous_intros | force simp: pathfinish_def joinpaths_def)+
  3294       apply (simp_all add: field_simps not_le)
  3295       apply (force dest!: t2)
  3296       apply (force simp: algebra_simps mult_left_mono affine_ineq dest!: 1 2)
  3297       apply (simp add: joinpaths_def subpath_def)
  3298       apply (force simp: algebra_simps)
  3299       done
  3300 qed
  3301 
  3302 lemma homotopic_join_subpaths2:
  3303   assumes "homotopic_paths s (subpath u v g +++ subpath v w g) (subpath u w g)"
  3304     shows "homotopic_paths s (subpath w v g +++ subpath v u g) (subpath w u g)"
  3305 by (metis assms homotopic_paths_reversepath_D pathfinish_subpath pathstart_subpath reversepath_joinpaths reversepath_subpath)
  3306 
  3307 lemma homotopic_join_subpaths3:
  3308   assumes hom: "homotopic_paths s (subpath u v g +++ subpath v w g) (subpath u w g)"
  3309       and "path g" and pag: "path_image g \<subseteq> s"
  3310       and u: "u \<in> {0..1}" and v: "v \<in> {0..1}" and w: "w \<in> {0..1}"
  3311     shows "homotopic_paths s (subpath v w g +++ subpath w u g) (subpath v u g)"
  3312 proof -
  3313   have "homotopic_paths s (subpath u w g +++ subpath w v g) ((subpath u v g +++ subpath v w g) +++ subpath w v g)"
  3314     apply (rule homotopic_paths_join)
  3315     using hom homotopic_paths_sym_eq apply blast
  3316     apply (metis \<open>path g\<close> homotopic_paths_eq pag path_image_subpath_subset path_subpath subset_trans v w)
  3317     apply (simp add:)
  3318     done
  3319   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)"
  3320     apply (rule homotopic_paths_sym [OF homotopic_paths_assoc])
  3321     using assms by (simp_all add: path_image_subpath_subset [THEN order_trans])
  3322   also have "homotopic_paths s (subpath u v g +++ subpath v w g +++ subpath w v g)
  3323                                (subpath u v g +++ linepath (pathfinish (subpath u v g)) (pathfinish (subpath u v g)))"
  3324     apply (rule homotopic_paths_join)
  3325     apply (metis \<open>path g\<close> homotopic_paths_eq order.trans pag path_image_subpath_subset path_subpath u v)
  3326     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)
  3327     apply (simp add:)
  3328     done
  3329   also have "homotopic_paths s (subpath u v g +++ linepath (pathfinish (subpath u v g)) (pathfinish (subpath u v g))) (subpath u v g)"
  3330     apply (rule homotopic_paths_rid)
  3331     using \<open>path g\<close> path_subpath u v apply blast
  3332     apply (meson \<open>path g\<close> order.trans pag path_image_subpath_subset u v)
  3333     done
  3334   finally have "homotopic_paths s (subpath u w g +++ subpath w v g) (subpath u v g)" .
  3335   then show ?thesis
  3336     using homotopic_join_subpaths2 by blast
  3337 qed
  3338 
  3339 proposition homotopic_join_subpaths:
  3340    "\<lbrakk>path g; path_image g \<subseteq> s; u \<in> {0..1}; v \<in> {0..1}; w \<in> {0..1}\<rbrakk>
  3341     \<Longrightarrow> homotopic_paths s (subpath u v g +++ subpath v w g) (subpath u w g)"
  3342 apply (rule le_cases3 [of u v w])
  3343 using homotopic_join_subpaths1 homotopic_join_subpaths2 homotopic_join_subpaths3 by metis+
  3344 
  3345 end