# Theory Path_Connected

```(*  Title:      HOL/Analysis/Path_Connected.thy
Authors:    LC Paulson and Robert Himmelmann (TU Muenchen), based on material from HOL Light
*)

section ‹Path-Connectedness›

theory Path_Connected
imports
Starlike
T1_Spaces
begin

subsection ‹Paths and Arcs›

definition✐‹tag important› path :: "(real ⇒ 'a::topological_space) ⇒ bool"
where "path g ≡ continuous_on {0..1} g"

definition✐‹tag important› pathstart :: "(real ⇒ 'a::topological_space) ⇒ 'a"
where "pathstart g ≡ g 0"

definition✐‹tag important› pathfinish :: "(real ⇒ 'a::topological_space) ⇒ 'a"
where "pathfinish g ≡ g 1"

definition✐‹tag important› path_image :: "(real ⇒ 'a::topological_space) ⇒ 'a set"
where "path_image g ≡ g ` {0 .. 1}"

definition✐‹tag important› reversepath :: "(real ⇒ 'a::topological_space) ⇒ real ⇒ 'a"
where "reversepath g ≡ (λx. g(1 - x))"

definition✐‹tag important› joinpaths :: "(real ⇒ 'a::topological_space) ⇒ (real ⇒ 'a) ⇒ real ⇒ 'a"
(infixr "+++" 75)
where "g1 +++ g2 ≡ (λx. if x ≤ 1/2 then g1 (2 * x) else g2 (2 * x - 1))"

definition✐‹tag important› loop_free :: "(real ⇒ 'a::topological_space) ⇒ bool"
where "loop_free g ≡ ∀x∈{0..1}. ∀y∈{0..1}. g x = g y ⟶ x = y ∨ x = 0 ∧ y = 1 ∨ x = 1 ∧ y = 0"

definition✐‹tag important› simple_path :: "(real ⇒ 'a::topological_space) ⇒ bool"
where "simple_path g ≡ path g ∧ loop_free g"

definition✐‹tag important› arc :: "(real ⇒ 'a :: topological_space) ⇒ bool"
where "arc g ≡ path g ∧ inj_on g {0..1}"

subsection✐‹tag unimportant›‹Invariance theorems›

lemma path_eq: "path p ⟹ (⋀t. t ∈ {0..1} ⟹ p t = q t) ⟹ path q"
using continuous_on_eq path_def by blast

lemma path_continuous_image: "path g ⟹ continuous_on (path_image g) f ⟹ path(f ∘ g)"
unfolding path_def path_image_def
using continuous_on_compose by blast

lemma continuous_on_translation_eq:
fixes g :: "'a :: real_normed_vector ⇒ 'b :: real_normed_vector"
shows "continuous_on A ((+) a ∘ g) = continuous_on A g"
proof -
have g: "g = (λx. -a + x) ∘ ((λx. a + x) ∘ g)"
by (rule ext) simp
show ?thesis
by (metis (no_types, opaque_lifting) g continuous_on_compose homeomorphism_def homeomorphism_translation)
qed

lemma path_translation_eq:
fixes g :: "real ⇒ 'a :: real_normed_vector"
shows "path((λx. a + x) ∘ g) = path g"
using continuous_on_translation_eq path_def by blast

lemma path_linear_image_eq:
fixes f :: "'a::euclidean_space ⇒ 'b::euclidean_space"
assumes "linear f" "inj f"
shows "path(f ∘ g) = path g"
proof -
from linear_injective_left_inverse [OF assms]
obtain h where h: "linear h" "h ∘ f = id"
by blast
with assms show ?thesis
by (metis comp_assoc id_comp linear_continuous_on linear_linear path_continuous_image)
qed

lemma pathstart_translation: "pathstart((λx. a + x) ∘ g) = a + pathstart g"

lemma pathstart_linear_image_eq: "linear f ⟹ pathstart(f ∘ g) = f(pathstart g)"

lemma pathfinish_translation: "pathfinish((λx. a + x) ∘ g) = a + pathfinish g"

lemma pathfinish_linear_image: "linear f ⟹ pathfinish(f ∘ g) = f(pathfinish g)"

lemma path_image_translation: "path_image((λx. a + x) ∘ g) = (λx. a + x) ` (path_image g)"

lemma path_image_linear_image: "linear f ⟹ path_image(f ∘ g) = f ` (path_image g)"

lemma reversepath_translation: "reversepath((λx. a + x) ∘ g) = (λx. a + x) ∘ reversepath g"
by (rule ext) (simp add: reversepath_def)

lemma reversepath_linear_image: "linear f ⟹ reversepath(f ∘ g) = f ∘ reversepath g"
by (rule ext) (simp add: reversepath_def)

lemma joinpaths_translation:
"((λx. a + x) ∘ g1) +++ ((λx. a + x) ∘ g2) = (λx. a + x) ∘ (g1 +++ g2)"
by (rule ext) (simp add: joinpaths_def)

lemma joinpaths_linear_image: "linear f ⟹ (f ∘ g1) +++ (f ∘ g2) = f ∘ (g1 +++ g2)"
by (rule ext) (simp add: joinpaths_def)

lemma loop_free_translation_eq:
fixes g :: "real ⇒ 'a::euclidean_space"
shows "loop_free((λx. a + x) ∘ g) = loop_free g"

lemma simple_path_translation_eq:
fixes g :: "real ⇒ 'a::euclidean_space"
shows "simple_path((λx. a + x) ∘ g) = simple_path g"
by (simp add: simple_path_def loop_free_translation_eq path_translation_eq)

lemma loop_free_linear_image_eq:
fixes f :: "'a::euclidean_space ⇒ 'b::euclidean_space"
assumes "linear f" "inj f"
shows "loop_free(f ∘ g) = loop_free g"
using assms inj_on_eq_iff [of f] by (auto simp: loop_free_def)

lemma simple_path_linear_image_eq:
fixes f :: "'a::euclidean_space ⇒ 'b::euclidean_space"
assumes "linear f" "inj f"
shows "simple_path(f ∘ g) = simple_path g"
using assms
by (simp add: loop_free_linear_image_eq path_linear_image_eq simple_path_def)

lemma arc_translation_eq:
fixes g :: "real ⇒ 'a::euclidean_space"
shows "arc((λx. a + x) ∘ g) ⟷ arc g"
by (auto simp: arc_def inj_on_def path_translation_eq)

lemma arc_linear_image_eq:
fixes f :: "'a::euclidean_space ⇒ 'b::euclidean_space"
assumes "linear f" "inj f"
shows  "arc(f ∘ g) = arc g"
using assms inj_on_eq_iff [of f]
by (auto simp: arc_def inj_on_def path_linear_image_eq)

lemma path_of_real: "path complex_of_real"
unfolding path_def by (intro continuous_intros)

lemma path_const: "path (λt. a)" for a::"'a::real_normed_vector"
unfolding path_def by (intro continuous_intros)

lemma path_minus: "path g ⟹ path (λt. - g t)" for g::"real⇒'a::real_normed_vector"
unfolding path_def by (intro continuous_intros)

lemma path_add: "⟦path f; path g⟧ ⟹ path (λt. f t + g t)" for f::"real⇒'a::real_normed_vector"
unfolding path_def by (intro continuous_intros)

lemma path_diff: "⟦path f; path g⟧ ⟹ path (λt. f t - g t)" for f::"real⇒'a::real_normed_vector"
unfolding path_def by (intro continuous_intros)

lemma path_mult: "⟦path f; path g⟧ ⟹ path (λt. f t * g t)" for f::"real⇒'a::real_normed_field"
unfolding path_def by (intro continuous_intros)

lemma pathin_iff_path_real [simp]: "pathin euclideanreal g ⟷ path g"

lemma continuous_on_path: "path f ⟹ t ⊆ {0..1} ⟹ continuous_on t f"
using continuous_on_subset path_def by blast

lemma inj_on_imp_loop_free: "inj_on g {0..1} ⟹ loop_free g"

lemma arc_imp_simple_path: "arc g ⟹ simple_path g"
by (simp add: arc_def inj_on_imp_loop_free simple_path_def)

lemma arc_imp_path: "arc g ⟹ path g"
using arc_def by blast

lemma arc_imp_inj_on: "arc g ⟹ inj_on g {0..1}"
by (auto simp: arc_def)

lemma simple_path_imp_path: "simple_path g ⟹ path g"
using simple_path_def by blast

lemma loop_free_cases: "loop_free g ⟹ inj_on g {0..1} ∨ pathfinish g = pathstart g"
by (force simp: inj_on_def loop_free_def pathfinish_def pathstart_def)

lemma simple_path_cases: "simple_path g ⟹ arc g ∨ pathfinish g = pathstart g"
using arc_def loop_free_cases simple_path_def by blast

lemma simple_path_imp_arc: "simple_path g ⟹ pathfinish g ≠ pathstart g ⟹ arc g"
using simple_path_cases by auto

lemma arc_distinct_ends: "arc g ⟹ pathfinish g ≠ pathstart g"
unfolding arc_def inj_on_def pathfinish_def pathstart_def
by fastforce

lemma arc_simple_path: "arc g ⟷ simple_path g ∧ pathfinish g ≠ pathstart g"
using arc_distinct_ends arc_imp_simple_path simple_path_cases by blast

lemma simple_path_eq_arc: "pathfinish g ≠ pathstart g ⟹ (simple_path g = arc g)"

lemma path_image_const [simp]: "path_image (λt. a) = {a}"
by (force simp: path_image_def)

lemma path_image_nonempty [simp]: "path_image g ≠ {}"
unfolding path_image_def image_is_empty box_eq_empty
by auto

lemma pathstart_in_path_image[intro]: "pathstart g ∈ path_image g"
unfolding pathstart_def path_image_def
by auto

lemma pathfinish_in_path_image[intro]: "pathfinish g ∈ path_image g"
unfolding pathfinish_def path_image_def
by auto

lemma connected_path_image[intro]: "path g ⟹ connected (path_image g)"
unfolding path_def path_image_def
using connected_continuous_image connected_Icc by blast

lemma compact_path_image[intro]: "path g ⟹ compact (path_image g)"
unfolding path_def path_image_def
using compact_continuous_image connected_Icc by blast

lemma reversepath_reversepath[simp]: "reversepath (reversepath g) = g"
unfolding reversepath_def
by auto

lemma pathstart_reversepath[simp]: "pathstart (reversepath g) = pathfinish g"
unfolding pathstart_def reversepath_def pathfinish_def
by auto

lemma pathfinish_reversepath[simp]: "pathfinish (reversepath g) = pathstart g"
unfolding pathstart_def reversepath_def pathfinish_def
by auto

lemma reversepath_o: "reversepath g = g ∘ (-)1"
by (auto simp: reversepath_def)

lemma pathstart_join[simp]: "pathstart (g1 +++ g2) = pathstart g1"
unfolding pathstart_def joinpaths_def pathfinish_def
by auto

lemma pathfinish_join[simp]: "pathfinish (g1 +++ g2) = pathfinish g2"
unfolding pathstart_def joinpaths_def pathfinish_def
by auto

lemma path_image_reversepath[simp]: "path_image (reversepath g) = path_image g"
proof -
have *: "⋀g. path_image (reversepath g) ⊆ path_image g"
unfolding path_image_def subset_eq reversepath_def Ball_def image_iff
by force
show ?thesis
using *[of g] *[of "reversepath g"]
unfolding reversepath_reversepath
by auto
qed

lemma path_reversepath [simp]: "path (reversepath g) ⟷ path g"
proof -
have *: "⋀g. path g ⟹ path (reversepath g)"
continuous_on_op_minus diff_zero image_diff_atLeastAtMost path_def reversepath_o)
then show ?thesis by force
qed

lemma arc_reversepath:
assumes "arc g" shows "arc(reversepath g)"
proof -
have injg: "inj_on g {0..1}"
using assms
have **: "⋀x y::real. 1-x = 1-y ⟹ x = y"
by simp
show ?thesis
using assms  by (clarsimp simp: arc_def intro!: inj_onI) (simp add: inj_onD reversepath_def **)
qed

lemma loop_free_reversepath:
assumes "loop_free g" shows "loop_free(reversepath g)"
using assms by (simp add: reversepath_def loop_free_def Ball_def) (smt (verit))

lemma simple_path_reversepath: "simple_path g ⟹ simple_path (reversepath g)"

lemmas reversepath_simps =
path_reversepath path_image_reversepath pathstart_reversepath pathfinish_reversepath

lemma path_join[simp]:
assumes "pathfinish g1 = pathstart g2"
shows "path (g1 +++ g2) ⟷ path g1 ∧ path g2"
unfolding path_def pathfinish_def pathstart_def
proof safe
assume cont: "continuous_on {0..1} (g1 +++ g2)"
have g1: "continuous_on {0..1} g1 ⟷ continuous_on {0..1} ((g1 +++ g2) ∘ (λx. x / 2))"
by (intro continuous_on_cong refl) (auto simp: joinpaths_def)
have g2: "continuous_on {0..1} g2 ⟷ continuous_on {0..1} ((g1 +++ g2) ∘ (λx. x / 2 + 1/2))"
using assms
by (intro continuous_on_cong refl) (auto simp: joinpaths_def pathfinish_def pathstart_def)
show "continuous_on {0..1} g1" and "continuous_on {0..1} g2"
unfolding g1 g2
by (auto intro!: continuous_intros continuous_on_subset[OF cont] simp del: o_apply)
next
assume g1g2: "continuous_on {0..1} g1" "continuous_on {0..1} g2"
have 01: "{0 .. 1} = {0..1/2} ∪ {1/2 .. 1::real}"
by auto
{
fix x :: real
assume "0 ≤ x" and "x ≤ 1"
then have "x ∈ (λx. x * 2) ` {0..1 / 2}"
by (intro image_eqI[where x="x/2"]) auto
}
note 1 = this
{
fix x :: real
assume "0 ≤ x" and "x ≤ 1"
then have "x ∈ (λx. x * 2 - 1) ` {1 / 2..1}"
by (intro image_eqI[where x="x/2 + 1/2"]) auto
}
note 2 = this
show "continuous_on {0..1} (g1 +++ g2)"
using assms
unfolding joinpaths_def 01
apply (intro continuous_on_cases closed_atLeastAtMost g1g2[THEN continuous_on_compose2] continuous_intros)
apply (auto simp: field_simps pathfinish_def pathstart_def intro!: 1 2)
done
qed

subsection✐‹tag unimportant› ‹Path Images›

lemma bounded_path_image: "path g ⟹ bounded(path_image g)"

lemma closed_path_image:
fixes g :: "real ⇒ 'a::t2_space"
shows "path g ⟹ closed(path_image g)"
by (metis compact_path_image compact_imp_closed)

lemma connected_simple_path_image: "simple_path g ⟹ connected(path_image g)"
by (metis connected_path_image simple_path_imp_path)

lemma compact_simple_path_image: "simple_path g ⟹ compact(path_image g)"
by (metis compact_path_image simple_path_imp_path)

lemma bounded_simple_path_image: "simple_path g ⟹ bounded(path_image g)"
by (metis bounded_path_image simple_path_imp_path)

lemma closed_simple_path_image:
fixes g :: "real ⇒ 'a::t2_space"
shows "simple_path g ⟹ closed(path_image g)"
by (metis closed_path_image simple_path_imp_path)

lemma connected_arc_image: "arc g ⟹ connected(path_image g)"
by (metis connected_path_image arc_imp_path)

lemma compact_arc_image: "arc g ⟹ compact(path_image g)"
by (metis compact_path_image arc_imp_path)

lemma bounded_arc_image: "arc g ⟹ bounded(path_image g)"
by (metis bounded_path_image arc_imp_path)

lemma closed_arc_image:
fixes g :: "real ⇒ 'a::t2_space"
shows "arc g ⟹ closed(path_image g)"
by (metis closed_path_image arc_imp_path)

lemma path_image_join_subset: "path_image (g1 +++ g2) ⊆ path_image g1 ∪ path_image g2"
unfolding path_image_def joinpaths_def
by auto

lemma subset_path_image_join:
assumes "path_image g1 ⊆ s" and "path_image g2 ⊆ s"
shows "path_image (g1 +++ g2) ⊆ s"
using path_image_join_subset[of g1 g2] and assms
by auto

lemma path_image_join:
assumes "pathfinish g1 = pathstart g2"
shows "path_image(g1 +++ g2) = path_image g1 ∪ path_image g2"
proof -
have "path_image g1 ⊆ path_image (g1 +++ g2)"
proof (clarsimp simp: path_image_def joinpaths_def)
fix u::real
assume "0 ≤ u" "u ≤ 1"
then show "g1 u ∈ (λx. g1 (2 * x)) ` ({0..1} ∩ {x. x * 2 ≤ 1})"
by (rule_tac x="u/2" in image_eqI) auto
qed
moreover
have §: "g2 u ∈ (λx. g2 (2 * x - 1)) ` ({0..1} ∩ {x. ¬ x * 2 ≤ 1})"
if "0 < u" "u ≤ 1" for u
using that assms
by (rule_tac x="(u+1)/2" in image_eqI) (auto simp: field_simps pathfinish_def pathstart_def)
have "g2 0 ∈ (λx. g1 (2 * x)) ` ({0..1} ∩ {x. x * 2 ≤ 1})"
using assms
by (rule_tac x="1/2" in image_eqI) (auto simp: pathfinish_def pathstart_def)
then have "path_image g2 ⊆ path_image (g1 +++ g2)"
by (auto simp: path_image_def joinpaths_def intro!: §)
ultimately show ?thesis
using path_image_join_subset by blast
qed

lemma not_in_path_image_join:
assumes "x ∉ path_image g1" and "x ∉ path_image g2"
shows "x ∉ path_image (g1 +++ g2)"
using assms and path_image_join_subset[of g1 g2]
by auto

lemma pathstart_compose: "pathstart(f ∘ p) = f(pathstart p)"

lemma pathfinish_compose: "pathfinish(f ∘ p) = f(pathfinish p)"

lemma path_image_compose: "path_image (f ∘ p) = f ` (path_image p)"

lemma path_compose_join: "f ∘ (p +++ q) = (f ∘ p) +++ (f ∘ q)"
by (rule ext) (simp add: joinpaths_def)

lemma path_compose_reversepath: "f ∘ reversepath p = reversepath(f ∘ p)"
by (rule ext) (simp add: reversepath_def)

lemma joinpaths_eq:
"(⋀t. t ∈ {0..1} ⟹ p t = p' t) ⟹
(⋀t. t ∈ {0..1} ⟹ q t = q' t)
⟹  t ∈ {0..1} ⟹ (p +++ q) t = (p' +++ q') t"
by (auto simp: joinpaths_def)

lemma loop_free_inj_on: "loop_free g ⟹ inj_on g {0<..<1}"
by (force simp: inj_on_def loop_free_def)

lemma simple_path_inj_on: "simple_path g ⟹ inj_on g {0<..<1}"
using loop_free_inj_on simple_path_def by auto

subsection✐‹tag unimportant›‹Simple paths with the endpoints removed›

lemma simple_path_endless:
assumes "simple_path c"
shows "path_image c - {pathstart c,pathfinish c} = c ` {0<..<1}" (is "?lhs = ?rhs")
proof
show "?lhs ⊆ ?rhs"
using less_eq_real_def by (auto simp: path_image_def pathstart_def pathfinish_def)
show "?rhs ⊆ ?lhs"
using assms
apply (simp add: image_subset_iff path_image_def pathstart_def pathfinish_def simple_path_def loop_free_def Ball_def)
by (smt (verit))
qed

lemma connected_simple_path_endless:
assumes "simple_path c"
shows "connected(path_image c - {pathstart c,pathfinish c})"
proof -
have "continuous_on {0<..<1} c"
using assms by (simp add: simple_path_def continuous_on_path path_def subset_iff)
then have "connected (c ` {0<..<1})"
using connected_Ioo connected_continuous_image by blast
then show ?thesis
using assms by (simp add: simple_path_endless)
qed

lemma nonempty_simple_path_endless:
"simple_path c ⟹ path_image c - {pathstart c,pathfinish c} ≠ {}"

subsection✐‹tag unimportant›‹The operations on paths›

lemma path_image_subset_reversepath: "path_image(reversepath g) ≤ path_image g"
by simp

lemma path_imp_reversepath: "path g ⟹ path(reversepath g)"
by simp

lemma half_bounded_equal: "1 ≤ x * 2 ⟹ x * 2 ≤ 1 ⟷ x = (1/2::real)"
by simp

lemma continuous_on_joinpaths:
assumes "continuous_on {0..1} g1" "continuous_on {0..1} g2" "pathfinish g1 = pathstart g2"
shows "continuous_on {0..1} (g1 +++ g2)"
using assms path_def path_join by blast

lemma path_join_imp: "⟦path g1; path g2; pathfinish g1 = pathstart g2⟧ ⟹ path(g1 +++ g2)"
by simp

lemma arc_join:
assumes "arc g1" "arc g2"
"pathfinish g1 = pathstart g2"
"path_image g1 ∩ path_image g2 ⊆ {pathstart g2}"
shows "arc(g1 +++ g2)"
proof -
have injg1: "inj_on g1 {0..1}"
using assms
have injg2: "inj_on g2 {0..1}"
using assms
have g11: "g1 1 = g2 0"
and sb:  "g1 ` {0..1} ∩ g2 ` {0..1} ⊆ {g2 0}"
using assms
by (simp_all add: arc_def pathfinish_def pathstart_def path_image_def)
{ fix x and y::real
assume xy: "g2 (2 * x - 1) = g1 (2 * y)" "x ≤ 1" "0 ≤ y" " y * 2 ≤ 1" "¬ x * 2 ≤ 1"
then have "g1 (2 * y) = g2 0"
using sb by force
then have False
using xy inj_onD injg2 by fastforce
} note * = this
have "inj_on (g1 +++ g2) {0..1}"
using inj_onD [OF injg1] inj_onD [OF injg2] *
by (simp add: inj_on_def joinpaths_def Ball_def) (smt (verit))
then show ?thesis
using arc_def assms path_join_imp by blast
qed

lemma simple_path_join_loop:
assumes "arc g1" "arc g2"
"pathfinish g1 = pathstart g2"  "pathfinish g2 = pathstart g1"
"path_image g1 ∩ path_image g2 ⊆ {pathstart g1, pathstart g2}"
shows "simple_path(g1 +++ g2)"
proof -
have injg1: "inj_on g1 {0..1}" and injg2: "inj_on g2 {0..1}"
using assms by (auto simp add: arc_def)
have g12: "g1 1 = g2 0"
and g21: "g2 1 = g1 0"
and sb:  "g1 ` {0..1} ∩ g2 ` {0..1} ⊆ {g1 0, g2 0}"
using assms
by (simp_all add: arc_def pathfinish_def pathstart_def path_image_def)
{ fix x and y::real
assume g2_eq: "g2 (2 * x - 1) = g1 (2 * y)"
and xyI: "x ≠ 1 ∨ y ≠ 0"
and xy: "x ≤ 1" "0 ≤ y" " y * 2 ≤ 1" "¬ x * 2 ≤ 1"
then consider "g1 (2 * y) = g1 0" | "g1 (2 * y) = g2 0"
using sb by force
then have False
proof cases
case 1
then have "y = 0"
using xy g2_eq by (auto dest!: inj_onD [OF injg1])
then show ?thesis
using xy g2_eq xyI by (auto dest: inj_onD [OF injg2] simp flip: g21)
next
case 2
then have "2*x = 1"
using g2_eq g12 inj_onD [OF injg2] atLeastAtMost_iff xy(1) xy(4) by fastforce
with xy show False by auto
qed
} note * = this
have "loop_free(g1 +++ g2)"
using inj_onD [OF injg1] inj_onD [OF injg2] *
by (simp add: loop_free_def joinpaths_def Ball_def) (smt (verit))
then show ?thesis
by (simp add: arc_imp_path assms simple_path_def)
qed

lemma reversepath_joinpaths:
"pathfinish g1 = pathstart g2 ⟹ reversepath(g1 +++ g2) = reversepath g2 +++ reversepath g1"
unfolding reversepath_def pathfinish_def pathstart_def joinpaths_def
by (rule ext) (auto simp: mult.commute)

subsection✐‹tag unimportant›‹Some reversed and "if and only if" versions of joining theorems›

lemma path_join_path_ends:
fixes g1 :: "real ⇒ 'a::metric_space"
assumes "path(g1 +++ g2)" "path g2"
shows "pathfinish g1 = pathstart g2"
proof (rule ccontr)
define e where "e = dist (g1 1) (g2 0)"
assume Neg: "pathfinish g1 ≠ pathstart g2"
then have "0 < dist (pathfinish g1) (pathstart g2)"
by auto
then have "e > 0"
by (metis e_def pathfinish_def pathstart_def)
then have "∀e>0. ∃d>0. ∀x'∈{0..1}. dist x' 0 < d ⟶ dist (g2 x') (g2 0) < e"
using ‹path g2› atLeastAtMost_iff zero_le_one unfolding path_def continuous_on_iff
by blast
then obtain d1 where "d1 > 0"
and d1: "⋀x'. ⟦x'∈{0..1}; norm x' < d1⟧ ⟹ dist (g2 x') (g2 0) < e/2"
by (metis ‹0 < e› half_gt_zero_iff norm_conv_dist)
obtain d2 where "d2 > 0"
and d2: "⋀x'. ⟦x'∈{0..1}; dist x' (1/2) < d2⟧
⟹ dist ((g1 +++ g2) x') (g1 1) < e/2"
using assms(1) ‹e > 0› unfolding path_def continuous_on_iff
apply (drule_tac x="1/2" in bspec, simp)
apply (drule_tac x="e/2" in spec, force simp: joinpaths_def)
done
have int01_1: "min (1/2) (min d1 d2) / 2 ∈ {0..1}"
using ‹d1 > 0› ‹d2 > 0› by (simp add: min_def)
have dist1: "norm (min (1 / 2) (min d1 d2) / 2) < d1"
using ‹d1 > 0› ‹d2 > 0› by (simp add: min_def dist_norm)
have int01_2: "1/2 + min (1/2) (min d1 d2) / 4 ∈ {0..1}"
using ‹d1 > 0› ‹d2 > 0› by (simp add: min_def)
have dist2: "dist (1 / 2 + min (1 / 2) (min d1 d2) / 4) (1 / 2) < d2"
using ‹d1 > 0› ‹d2 > 0› by (simp add: min_def dist_norm)
have [simp]: "¬ min (1 / 2) (min d1 d2) ≤ 0"
using ‹d1 > 0› ‹d2 > 0› by (simp add: min_def)
have "dist (g2 (min (1 / 2) (min d1 d2) / 2)) (g1 1) < e/2"
"dist (g2 (min (1 / 2) (min d1 d2) / 2)) (g2 0) < e/2"
using d1 [OF int01_1 dist1] d2 [OF int01_2 dist2] by (simp_all add: joinpaths_def)
then have "dist (g1 1) (g2 0) < e/2 + e/2"
using dist_triangle_half_r e_def by blast
then show False
qed

lemma path_join_eq [simp]:
fixes g1 :: "real ⇒ 'a::metric_space"
assumes "path g1" "path g2"
shows "path(g1 +++ g2) ⟷ pathfinish g1 = pathstart g2"
using assms by (metis path_join_path_ends path_join_imp)

lemma simple_path_joinE:
assumes "simple_path(g1 +++ g2)" and "pathfinish g1 = pathstart g2"
obtains "arc g1" "arc g2"
"path_image g1 ∩ path_image g2 ⊆ {pathstart g1, pathstart g2}"
proof -
have *: "⋀x y. ⟦0 ≤ x; x ≤ 1; 0 ≤ y; y ≤ 1; (g1 +++ g2) x = (g1 +++ g2) y⟧
⟹ x = y ∨ x = 0 ∧ y = 1 ∨ x = 1 ∧ y = 0"
using assms by (simp add: simple_path_def loop_free_def)
have "path g1"
using assms path_join simple_path_imp_path by blast
moreover have "inj_on g1 {0..1}"
proof (clarsimp simp: inj_on_def)
fix x y
assume "g1 x = g1 y" "0 ≤ x" "x ≤ 1" "0 ≤ y" "y ≤ 1"
then show "x = y"
using * [of "x/2" "y/2"] by (simp add: joinpaths_def split_ifs)
qed
ultimately have "arc g1"
using assms  by (simp add: arc_def)
have [simp]: "g2 0 = g1 1"
using assms by (metis pathfinish_def pathstart_def)
have "path g2"
using assms path_join simple_path_imp_path by blast
moreover have "inj_on g2 {0..1}"
proof (clarsimp simp: inj_on_def)
fix x y
assume "g2 x = g2 y" "0 ≤ x" "x ≤ 1" "0 ≤ y" "y ≤ 1"
then show "x = y"
using * [of "(x+1) / 2" "(y+1) / 2"]
by (force simp: joinpaths_def split_ifs field_split_simps)
qed
ultimately have "arc g2"
using assms  by (simp add: arc_def)
have "g2 y = g1 0 ∨ g2 y = g1 1"
if "g1 x = g2 y" "0 ≤ x" "x ≤ 1" "0 ≤ y" "y ≤ 1" for x y
using * [of "x / 2" "(y + 1) / 2"] that
by (auto simp: joinpaths_def split_ifs field_split_simps)
then have "path_image g1 ∩ path_image g2 ⊆ {pathstart g1, pathstart g2}"
by (fastforce simp: pathstart_def pathfinish_def path_image_def)
with ‹arc g1› ‹arc g2› show ?thesis using that by blast
qed

lemma simple_path_join_loop_eq:
assumes "pathfinish g2 = pathstart g1" "pathfinish g1 = pathstart g2"
shows "simple_path(g1 +++ g2) ⟷
arc g1 ∧ arc g2 ∧ path_image g1 ∩ path_image g2 ⊆ {pathstart g1, pathstart g2}"
by (metis assms simple_path_joinE simple_path_join_loop)

lemma arc_join_eq:
assumes "pathfinish g1 = pathstart g2"
shows "arc(g1 +++ g2) ⟷
arc g1 ∧ arc g2 ∧ path_image g1 ∩ path_image g2 ⊆ {pathstart g2}"
(is "?lhs = ?rhs")
proof
assume ?lhs then show ?rhs
using reversepath_simps assms
by (smt (verit, ccfv_threshold) Int_commute arc_distinct_ends arc_imp_simple_path arc_reversepath
in_mono insertE pathfinish_join reversepath_joinpaths simple_path_joinE subsetI)
next
assume ?rhs then show ?lhs
using assms
by (fastforce simp: pathfinish_def pathstart_def intro!: arc_join)
qed

lemma arc_join_eq_alt:
"pathfinish g1 = pathstart g2
⟹ (arc(g1 +++ g2) ⟷
arc g1 ∧ arc g2 ∧ path_image g1 ∩ path_image g2 = {pathstart g2})"
using pathfinish_in_path_image by (fastforce simp: arc_join_eq)

subsection✐‹tag unimportant›‹The joining of paths is associative›

lemma path_assoc:
"⟦pathfinish p = pathstart q; pathfinish q = pathstart r⟧
⟹ path(p +++ (q +++ r)) ⟷ path((p +++ q) +++ r)"
by simp

lemma simple_path_assoc:
assumes "pathfinish p = pathstart q" "pathfinish q = pathstart r"
shows "simple_path (p +++ (q +++ r)) ⟷ simple_path ((p +++ q) +++ r)"
proof (cases "pathstart p = pathfinish r")
case True show ?thesis
proof
assume "simple_path (p +++ q +++ r)"
with assms True show "simple_path ((p +++ q) +++ r)"
by (fastforce simp add: simple_path_join_loop_eq arc_join_eq path_image_join
dest: arc_distinct_ends [of r])
next
assume 0: "simple_path ((p +++ q) +++ r)"
with assms True have q: "pathfinish r ∉ path_image q"
using arc_distinct_ends
by (fastforce simp add: simple_path_join_loop_eq arc_join_eq path_image_join)
have "pathstart r ∉ path_image p"
using assms
by (metis 0 IntI arc_distinct_ends arc_join_eq_alt empty_iff insert_iff
pathfinish_in_path_image pathfinish_join simple_path_joinE)
with assms 0 q True show "simple_path (p +++ q +++ r)"
by (auto simp: simple_path_join_loop_eq arc_join_eq path_image_join
dest!: subsetD [OF _ IntI])
qed
next
case False
{ fix x :: 'a
assume a: "path_image p ∩ path_image q ⊆ {pathstart q}"
"(path_image p ∪ path_image q) ∩ path_image r ⊆ {pathstart r}"
"x ∈ path_image p" "x ∈ path_image r"
have "pathstart r ∈ path_image q"
by (metis assms(2) pathfinish_in_path_image)
with a have "x = pathstart q"
by blast
}
with False assms show ?thesis
by (auto simp: simple_path_eq_arc simple_path_join_loop_eq arc_join_eq path_image_join)
qed

lemma arc_assoc:
"⟦pathfinish p = pathstart q; pathfinish q = pathstart r⟧
⟹ arc(p +++ (q +++ r)) ⟷ arc((p +++ q) +++ r)"

subsubsection✐‹tag unimportant›‹Symmetry and loops›

lemma path_sym:
"⟦pathfinish p = pathstart q; pathfinish q = pathstart p⟧ ⟹ path(p +++ q) ⟷ path(q +++ p)"
by auto

lemma simple_path_sym:
"⟦pathfinish p = pathstart q; pathfinish q = pathstart p⟧
⟹ simple_path(p +++ q) ⟷ simple_path(q +++ p)"
by (metis (full_types) inf_commute insert_commute simple_path_joinE simple_path_join_loop)

lemma path_image_sym:
"⟦pathfinish p = pathstart q; pathfinish q = pathstart p⟧
⟹ path_image(p +++ q) = path_image(q +++ p)"

subsection‹Subpath›

definition✐‹tag important› subpath :: "real ⇒ real ⇒ (real ⇒ 'a) ⇒ real ⇒ 'a::real_normed_vector"
where "subpath a b g ≡ λx. g((b - a) * x + a)"

lemma path_image_subpath_gen:
fixes g :: "_ ⇒ 'a::real_normed_vector"
shows "path_image(subpath u v g) = g ` (closed_segment u v)"
by (auto simp add: closed_segment_real_eq path_image_def subpath_def)

lemma path_image_subpath:
fixes g :: "real ⇒ 'a::real_normed_vector"
shows "path_image(subpath u v g) = (if u ≤ v then g ` {u..v} else g ` {v..u})"

lemma path_image_subpath_commute:
fixes g :: "real ⇒ 'a::real_normed_vector"
shows "path_image(subpath u v g) = path_image(subpath v u g)"

lemma path_subpath [simp]:
fixes g :: "real ⇒ 'a::real_normed_vector"
assumes "path g" "u ∈ {0..1}" "v ∈ {0..1}"
shows "path(subpath u v g)"
proof -
have "continuous_on {u..v} g" "continuous_on {v..u} g"
using assms continuous_on_path by fastforce+
then have "continuous_on {0..1} (g ∘ (λx. ((v-u) * x+ u)))"
by (intro continuous_intros; simp add: image_affinity_atLeastAtMost [where c=u])
then show ?thesis
qed

lemma pathstart_subpath [simp]: "pathstart(subpath u v g) = g(u)"

lemma pathfinish_subpath [simp]: "pathfinish(subpath u v g) = g(v)"

lemma subpath_trivial [simp]: "subpath 0 1 g = g"

lemma subpath_reversepath: "subpath 1 0 g = reversepath g"

lemma reversepath_subpath: "reversepath(subpath u v g) = subpath v u g"
by (simp add: reversepath_def subpath_def algebra_simps)

lemma subpath_translation: "subpath u v ((λx. a + x) ∘ g) = (λx. a + x) ∘ subpath u v g"
by (rule ext) (simp add: subpath_def)

lemma subpath_image: "subpath u v (f ∘ g) = f ∘ subpath u v g"
by (rule ext) (simp add: subpath_def)

lemma affine_ineq:
fixes x :: "'a::linordered_idom"
assumes "x ≤ 1" "v ≤ u"
shows "v + x * u ≤ u + x * v"
proof -
have "(1-x)*(u-v) ≥ 0"
using assms by auto
then show ?thesis
qed

lemma sum_le_prod1:
fixes a::real shows "⟦a ≤ 1; b ≤ 1⟧ ⟹ a + b ≤ 1 + a * b"

lemma simple_path_subpath_eq:
"simple_path(subpath u v g) ⟷
path(subpath u v g) ∧ u≠v ∧
(∀x y. x ∈ closed_segment u v ∧ y ∈ closed_segment u v ∧ g x = g y
⟶ x = y ∨ x = u ∧ y = v ∨ x = v ∧ y = u)"
(is "?lhs = ?rhs")
proof
assume ?lhs
then have p: "path (λx. g ((v - u) * x + u))"
and sim: "(⋀x y. ⟦x∈{0..1}; y∈{0..1}; g ((v - u) * x + u) = g ((v - u) * y + u)⟧
⟹ x = y ∨ x = 0 ∧ y = 1 ∨ x = 1 ∧ y = 0)"
by (auto simp: simple_path_def loop_free_def subpath_def)
{ fix x y
assume "x ∈ closed_segment u v" "y ∈ closed_segment u v" "g x = g y"
then have "x = y ∨ x = u ∧ y = v ∨ x = v ∧ y = u"
using sim [of "(x-u)/(v-u)" "(y-u)/(v-u)"] p
by (auto split: if_split_asm simp add: closed_segment_real_eq image_affinity_atLeastAtMost)
} moreover
have "path(subpath u v g) ∧ u≠v"
using sim [of "1/3" "2/3"] p
by (auto simp: subpath_def)
ultimately show ?rhs
by metis
next
assume ?rhs
then
have d1: "⋀x y. ⟦g x = g y; u ≤ x; x ≤ v; u ≤ y; y ≤ v⟧ ⟹ x = y ∨ x = u ∧ y = v ∨ x = v ∧ y = u"
and d2: "⋀x y. ⟦g x = g y; v ≤ x; x ≤ u; v ≤ y; y ≤ u⟧ ⟹ x = y ∨ x = u ∧ y = v ∨ x = v ∧ y = u"
and ne: "u < v ∨ v < u"
and psp: "path (subpath u v g)"
by (auto simp: closed_segment_real_eq image_affinity_atLeastAtMost)
have [simp]: "⋀x. u + x * v = v + x * u ⟷ u=v ∨ x=1"
by algebra
show ?lhs using psp ne
unfolding simple_path_def loop_free_def subpath_def
by (fastforce simp add: algebra_simps affine_ineq mult_left_mono crossproduct_eq dest: d1 d2)
qed

lemma arc_subpath_eq:
"arc(subpath u v g) ⟷ path(subpath u v g) ∧ u≠v ∧ inj_on g (closed_segment u v)"
by (smt (verit, best) arc_simple_path closed_segment_commute ends_in_segment(2) inj_on_def pathfinish_subpath pathstart_subpath simple_path_subpath_eq)

lemma simple_path_subpath:
assumes "simple_path g" "u ∈ {0..1}" "v ∈ {0..1}" "u ≠ v"
shows "simple_path(subpath u v g)"
using assms
apply (simp add: simple_path_def loop_free_def closed_segment_real_eq image_affinity_atLeastAtMost, fastforce)
done

lemma arc_simple_path_subpath:
"⟦simple_path g; u ∈ {0..1}; v ∈ {0..1}; g u ≠ g v⟧ ⟹ arc(subpath u v g)"
by (force intro: simple_path_subpath simple_path_imp_arc)

lemma arc_subpath_arc:
"⟦arc g; u ∈ {0..1}; v ∈ {0..1}; u ≠ v⟧ ⟹ arc(subpath u v g)"
by (meson arc_def arc_imp_simple_path arc_simple_path_subpath inj_onD)

lemma arc_simple_path_subpath_interior:
"⟦simple_path g; u ∈ {0..1}; v ∈ {0..1}; u ≠ v; ¦u-v¦ < 1⟧ ⟹ arc(subpath u v g)"
by (force simp: simple_path_def loop_free_def intro: arc_simple_path_subpath)

lemma path_image_subpath_subset:
"⟦u ∈ {0..1}; v ∈ {0..1}⟧ ⟹ path_image(subpath u v g) ⊆ path_image g"
by (metis atLeastAtMost_iff atLeastatMost_subset_iff path_image_def path_image_subpath subset_image_iff)

lemma join_subpaths_middle: "subpath (0) ((1 / 2)) p +++ subpath ((1 / 2)) 1 p = p"
by (rule ext) (simp add: joinpaths_def subpath_def field_split_simps)

subsection✐‹tag unimportant›‹There is a subpath to the frontier›

lemma subpath_to_frontier_explicit:
fixes S :: "'a::metric_space set"
assumes g: "path g" and "pathfinish g ∉ S"
obtains u where "0 ≤ u" "u ≤ 1"
"⋀x. 0 ≤ x ∧ x < u ⟹ g x ∈ interior S"
"(g u ∉ interior S)" "(u = 0 ∨ g u ∈ closure S)"
proof -
have gcon: "continuous_on {0..1} g"
using g by (simp add: path_def)
moreover have "bounded ({u. g u ∈ closure (- S)} ∩ {0..1})"
using compact_eq_bounded_closed by fastforce
ultimately have com: "compact ({0..1} ∩ {u. g u ∈ closure (- S)})"
using closed_vimage_Int
by (metis (full_types) Int_commute closed_atLeastAtMost closed_closure compact_eq_bounded_closed vimage_def)
have "1 ∈ {u. g u ∈ closure (- S)}"
using assms by (simp add: pathfinish_def closure_def)
then have dis: "{0..1} ∩ {u. g u ∈ closure (- S)} ≠ {}"
using atLeastAtMost_iff zero_le_one by blast
then obtain u where "0 ≤ u" "u ≤ 1" and gu: "g u ∈ closure (- S)"
and umin: "⋀t. ⟦0 ≤ t; t ≤ 1; g t ∈ closure (- S)⟧ ⟹ u ≤ t"
using compact_attains_inf [OF com dis] by fastforce
then have umin': "⋀t. ⟦0 ≤ t; t ≤ 1; t < u⟧ ⟹  g t ∈ S"
using closure_def by fastforce
have §: "g u ∈ closure S" if "u ≠ 0"
proof -
have "u > 0" using that ‹0 ≤ u› by auto
{ fix e::real assume "e > 0"
obtain d where "d>0" and d: "⋀x'. ⟦x' ∈ {0..1}; dist x' u ≤ d⟧ ⟹ dist (g x') (g u) < e"
using continuous_onE [OF gcon _ ‹e > 0›] ‹0 ≤ _› ‹_ ≤ 1› atLeastAtMost_iff by auto
have *: "dist (max 0 (u - d / 2)) u ≤ d"
using ‹0 ≤ u› ‹u ≤ 1› ‹d > 0› by (simp add: dist_real_def)
have "∃y∈S. dist y (g u) < e"
using ‹0 < u› ‹u ≤ 1› ‹d > 0›
by (force intro: d [OF _ *] umin')
}
then show ?thesis
qed
show ?thesis
proof
show "⋀x. 0 ≤ x ∧ x < u ⟹ g x ∈ interior S"
using ‹u ≤ 1› interior_closure umin by fastforce
show "g u ∉ interior S"
qed (use ‹0 ≤ u› ‹u ≤ 1› § in auto)
qed

lemma subpath_to_frontier_strong:
assumes g: "path g" and "pathfinish g ∉ S"
obtains u where "0 ≤ u" "u ≤ 1" "g u ∉ interior S"
"u = 0 ∨ (∀x. 0 ≤ x ∧ x < 1 ⟶ subpath 0 u g x ∈ interior S)  ∧  g u ∈ closure S"
proof -
obtain u where "0 ≤ u" "u ≤ 1"
and gxin: "⋀x. 0 ≤ x ∧ x < u ⟹ g x ∈ interior S"
and gunot: "(g u ∉ interior S)" and u0: "(u = 0 ∨ g u ∈ closure S)"
using subpath_to_frontier_explicit [OF assms] by blast
show ?thesis
proof
show "g u ∉ interior S"
using gunot by blast
qed (use ‹0 ≤ u› ‹u ≤ 1› u0 in ‹(force simp: subpath_def gxin)+›)
qed

lemma subpath_to_frontier:
assumes g: "path g" and g0: "pathstart g ∈ closure S" and g1: "pathfinish g ∉ S"
obtains u where "0 ≤ u" "u ≤ 1" "g u ∈ frontier S" "path_image(subpath 0 u g) - {g u} ⊆ interior S"
proof -
obtain u where "0 ≤ u" "u ≤ 1"
and notin: "g u ∉ interior S"
and disj: "u = 0 ∨
(∀x. 0 ≤ x ∧ x < 1 ⟶ subpath 0 u g x ∈ interior S) ∧ g u ∈ closure S"
(is "_ ∨ ?P")
using subpath_to_frontier_strong [OF g g1] by blast
show ?thesis
proof
show "g u ∈ frontier S"
by (metis DiffI disj frontier_def g0 notin pathstart_def)
show "path_image (subpath 0 u g) - {g u} ⊆ interior S"
using disj
proof
assume "u = 0"
then show ?thesis
next
assume P: ?P
show ?thesis
fix y
assume y: "y ∈ closed_segment 0 u" "g y ∉ interior S"
with ‹0 ≤ u› have "0 ≤ y" "y ≤ u"
by (auto simp: closed_segment_eq_real_ivl split: if_split_asm)
then have "y=u ∨ subpath 0 u g (y/u) ∈ interior S"
using P less_eq_real_def by force
then show "g y = g u"
using y by (auto simp: subpath_def split: if_split_asm)
qed
qed
qed (use ‹0 ≤ u› ‹u ≤ 1› in auto)
qed

lemma exists_path_subpath_to_frontier:
fixes S :: "'a::real_normed_vector set"
assumes "path g" "pathstart g ∈ closure S" "pathfinish g ∉ S"
obtains h where "path h" "pathstart h = pathstart g" "path_image h ⊆ path_image g"
"path_image h - {pathfinish h} ⊆ interior S"
"pathfinish h ∈ frontier S"
proof -
obtain u where u: "0 ≤ u" "u ≤ 1" "g u ∈ frontier S" "(path_image(subpath 0 u g) - {g u}) ⊆ interior S"
using subpath_to_frontier [OF assms] by blast
show ?thesis
proof
show "path_image (subpath 0 u g) ⊆ path_image g"
show "pathstart (subpath 0 u g) = pathstart g"
by (metis pathstart_def pathstart_subpath)
qed (use assms u in ‹auto simp: path_image_subpath›)
qed

lemma exists_path_subpath_to_frontier_closed:
fixes S :: "'a::real_normed_vector set"
assumes S: "closed S" and g: "path g" and g0: "pathstart g ∈ S" and g1: "pathfinish g ∉ S"
obtains h where "path h" "pathstart h = pathstart g" "path_image h ⊆ path_image g ∩ S"
"pathfinish h ∈ frontier S"
by (smt (verit, del_insts) Diff_iff Int_iff S closure_closed exists_path_subpath_to_frontier
frontier_def g g0 g1 interior_subset singletonD subset_eq)

subsection ‹Shift Path to Start at Some Given Point›

definition✐‹tag important› shiftpath :: "real ⇒ (real ⇒ 'a::topological_space) ⇒ real ⇒ 'a"
where "shiftpath a f = (λx. if (a + x) ≤ 1 then f (a + x) else f (a + x - 1))"

lemma shiftpath_alt_def: "shiftpath a f = (λx. if x ≤ 1-a then f (a + x) else f (a + x - 1))"
by (auto simp: shiftpath_def)

lemma pathstart_shiftpath: "a ≤ 1 ⟹ pathstart (shiftpath a g) = g a"
unfolding pathstart_def shiftpath_def by auto

lemma pathfinish_shiftpath:
assumes "0 ≤ a"
and "pathfinish g = pathstart g"
shows "pathfinish (shiftpath a g) = g a"
using assms
unfolding pathstart_def pathfinish_def shiftpath_def
by auto

lemma endpoints_shiftpath:
assumes "pathfinish g = pathstart g"
and "a ∈ {0 .. 1}"
shows "pathfinish (shiftpath a g) = g a"
and "pathstart (shiftpath a g) = g a"
using assms
by (auto intro!: pathfinish_shiftpath pathstart_shiftpath)

lemma closed_shiftpath:
assumes "pathfinish g = pathstart g"
and "a ∈ {0..1}"
shows "pathfinish (shiftpath a g) = pathstart (shiftpath a g)"
using endpoints_shiftpath[OF assms]
by auto

lemma path_shiftpath:
assumes "path g"
and "pathfinish g = pathstart g"
and "a ∈ {0..1}"
shows "path (shiftpath a g)"
proof -
have *: "{0 .. 1} = {0 .. 1-a} ∪ {1-a .. 1}"
using assms(3) by auto
have **: "⋀x. x + a = 1 ⟹ g (x + a - 1) = g (x + a)"
by (smt (verit, best) assms(2) pathfinish_def pathstart_def)
show ?thesis
unfolding path_def shiftpath_def *
proof (rule continuous_on_closed_Un)
have contg: "continuous_on {0..1} g"
using ‹path g› path_def by blast
show "continuous_on {0..1-a} (λx. if a + x ≤ 1 then g (a + x) else g (a + x - 1))"
proof (rule continuous_on_eq)
show "continuous_on {0..1-a} (g ∘ (+) a)"
by (intro continuous_intros continuous_on_subset [OF contg]) (use ‹a ∈ {0..1}› in auto)
qed auto
show "continuous_on {1-a..1} (λx. if a + x ≤ 1 then g (a + x) else g (a + x - 1))"
proof (rule continuous_on_eq)
show "continuous_on {1-a..1} (g ∘ (+) (a - 1))"
by (intro continuous_intros continuous_on_subset [OF contg]) (use ‹a ∈ {0..1}› in auto)
qed auto
qed

lemma shiftpath_shiftpath:
assumes "pathfinish g = pathstart g"
and "a ∈ {0..1}"
and "x ∈ {0..1}"
shows "shiftpath (1 - a) (shiftpath a g) x = g x"
using assms
unfolding pathfinish_def pathstart_def shiftpath_def
by auto

lemma path_image_shiftpath:
assumes a: "a ∈ {0..1}"
and "pathfinish g = pathstart g"
shows "path_image (shiftpath a g) = path_image g"
proof -
{ fix x
assume g: "g 1 = g 0" "x ∈ {0..1::real}" and gne: "⋀y. y∈{0..1} ∩ {x. ¬ a + x ≤ 1} ⟹ g x ≠ g (a + y - 1)"
then have "∃y∈{0..1} ∩ {x. a + x ≤ 1}. g x = g (a + y)"
proof (cases "a ≤ x")
case False
then show ?thesis
apply (rule_tac x="1 + x - a" in bexI)
using g gne[of "1 + x - a"] a by (force simp: field_simps)+
next
case True
then show ?thesis
using g a  by (rule_tac x="x - a" in bexI) (auto simp: field_simps)
qed
}
then show ?thesis
using assms
unfolding shiftpath_def path_image_def pathfinish_def pathstart_def
by (auto simp: image_iff)
qed

lemma loop_free_shiftpath:
assumes "loop_free g" "pathfinish g = pathstart g" and a: "0 ≤ a" "a ≤ 1"
shows "loop_free (shiftpath a g)"
unfolding loop_free_def
proof (intro conjI impI ballI)
show "x = y ∨ x = 0 ∧ y = 1 ∨ x = 1 ∧ y = 0"
if "x ∈ {0..1}" "y ∈ {0..1}" "shiftpath a g x = shiftpath a g y" for x y
using that a assms unfolding shiftpath_def loop_free_def
by (smt (verit, ccfv_threshold) atLeastAtMost_iff)
qed

lemma simple_path_shiftpath:
assumes "simple_path g" "pathfinish g = pathstart g" and a: "0 ≤ a" "a ≤ 1"
shows "simple_path (shiftpath a g)"
using assms loop_free_shiftpath path_shiftpath simple_path_def by fastforce

subsection ‹Straight-Line Paths›

definition✐‹tag important› linepath :: "'a::real_normed_vector ⇒ 'a ⇒ real ⇒ 'a"
where "linepath a b = (λx. (1 - x) *⇩R a + x *⇩R b)"

lemma pathstart_linepath[simp]: "pathstart (linepath a b) = a"
unfolding pathstart_def linepath_def
by auto

lemma pathfinish_linepath[simp]: "pathfinish (linepath a b) = b"
unfolding pathfinish_def linepath_def
by auto

lemma linepath_inner: "linepath a b x ∙ v = linepath (a ∙ v) (b ∙ v) x"

lemma Re_linepath': "Re (linepath a b x) = linepath (Re a) (Re b) x"

lemma Im_linepath': "Im (linepath a b x) = linepath (Im a) (Im b) x"

lemma linepath_0': "linepath a b 0 = a"

lemma linepath_1': "linepath a b 1 = b"

lemma continuous_linepath_at[intro]: "continuous (at x) (linepath a b)"
unfolding linepath_def
by (intro continuous_intros)

lemma continuous_on_linepath [intro,continuous_intros]: "continuous_on s (linepath a b)"
using continuous_linepath_at
by (auto intro!: continuous_at_imp_continuous_on)

lemma path_linepath[iff]: "path (linepath a b)"
unfolding path_def
by (rule continuous_on_linepath)

lemma path_image_linepath[simp]: "path_image (linepath a b) = closed_segment a b"
unfolding path_image_def segment linepath_def
by auto

lemma reversepath_linepath[simp]: "reversepath (linepath a b) = linepath b a"
unfolding reversepath_def linepath_def
by auto

lemma linepath_0 [simp]: "linepath 0 b x = x *⇩R b"

lemma linepath_cnj: "cnj (linepath a b x) = linepath (cnj a) (cnj b) x"

lemma arc_linepath:
assumes "a ≠ b" shows [simp]: "arc (linepath a b)"
proof -
{
fix x y :: "real"
assume "x *⇩R b + y *⇩R a = x *⇩R a + y *⇩R b"
then have "(x - y) *⇩R a = (x - y) *⇩R b"
with assms have "x = y"
by simp
}
then show ?thesis
unfolding arc_def inj_on_def
by (fastforce simp: algebra_simps linepath_def)
qed

lemma simple_path_linepath[intro]: "a ≠ b ⟹ simple_path (linepath a b)"

lemma linepath_trivial [simp]: "linepath a a x = a"

lemma linepath_refl: "linepath a a = (λx. a)"
by auto

lemma subpath_refl: "subpath a a g = linepath (g a) (g a)"
by (simp add: subpath_def linepath_def algebra_simps)

lemma linepath_of_real: "(linepath (of_real a) (of_real b) x) = of_real ((1 - x)*a + x*b)"

lemma of_real_linepath: "of_real (linepath a b x) = linepath (of_real a) (of_real b) x"
by (metis linepath_of_real mult.right_neutral of_real_def real_scaleR_def)

lemma inj_on_linepath:
assumes "a ≠ b" shows "inj_on (linepath a b) {0..1}"
using arc_imp_inj_on arc_linepath assms by blast

lemma linepath_le_1:
fixes a::"'a::linordered_idom" shows "⟦a ≤ 1; b ≤ 1; 0 ≤ u; u ≤ 1⟧ ⟹ (1 - u) * a + u * b ≤ 1"
using mult_left_le [of a "1-u"] mult_left_le [of b u] by auto

lemma linepath_in_path:
shows "x ∈ {0..1} ⟹ linepath a b x ∈ closed_segment a b"
by (auto simp: segment linepath_def)

lemma linepath_image_01: "linepath a b ` {0..1} = closed_segment a b"
by (auto simp: segment linepath_def)

lemma linepath_in_convex_hull:
fixes x::real
assumes "a ∈ convex hull S"
and "b ∈ convex hull S"
and "0≤x" "x≤1"
shows "linepath a b x ∈ convex hull S"
by (meson assms atLeastAtMost_iff convex_contains_segment convex_convex_hull linepath_in_path subset_eq)

lemma Re_linepath: "Re(linepath (of_real a) (of_real b) x) = (1 - x)*a + x*b"

lemma Im_linepath: "Im(linepath (of_real a) (of_real b) x) = 0"

lemma bounded_linear_linepath:
assumes "bounded_linear f"
shows   "f (linepath a b x) = linepath (f a) (f b) x"
proof -
interpret f: bounded_linear f by fact
qed

lemma bounded_linear_linepath':
assumes "bounded_linear f"
shows   "f ∘ linepath a b = linepath (f a) (f b)"
using bounded_linear_linepath[OF assms] by (simp add: fun_eq_iff)

lemma linepath_cnj': "cnj ∘ linepath a b = linepath (cnj a) (cnj b)"

lemma differentiable_linepath [intro]: "linepath a b differentiable at x within A"
by (auto simp: linepath_def)

lemma has_vector_derivative_linepath_within:
"(linepath a b has_vector_derivative (b - a)) (at x within S)"
by (force intro: derivative_eq_intros simp add: linepath_def has_vector_derivative_def algebra_simps)

subsection✐‹tag unimportant›‹Segments via convex hulls›

lemma segments_subset_convex_hull:
"closed_segment a b ⊆ (convex hull {a,b,c})"
"closed_segment a c ⊆ (convex hull {a,b,c})"
"closed_segment b c ⊆ (convex hull {a,b,c})"
"closed_segment b a ⊆ (convex hull {a,b,c})"
"closed_segment c a ⊆ (convex hull {a,b,c})"
"closed_segment c b ⊆ (convex hull {a,b,c})"
by (auto simp: segment_convex_hull linepath_of_real  elim!: rev_subsetD [OF _ hull_mono])

lemma midpoints_in_convex_hull:
assumes "x ∈ convex hull s" "y ∈ convex hull s"
shows "midpoint x y ∈ convex hull s"
using assms closed_segment_subset_convex_hull csegment_midpoint_subset by blast

lemma not_in_interior_convex_hull_3:
fixes a :: "complex"
shows "a ∉ interior(convex hull {a,b,c})"
"b ∉ interior(convex hull {a,b,c})"
"c ∉ interior(convex hull {a,b,c})"
by (auto simp: card_insert_le_m1 not_in_interior_convex_hull)

lemma midpoint_in_closed_segment [simp]: "midpoint a b ∈ closed_segment a b"
using midpoints_in_convex_hull segment_convex_hull by blast

lemma midpoint_in_open_segment [simp]: "midpoint a b ∈ open_segment a b ⟷ a ≠ b"

lemma continuous_IVT_local_extremum:
fixes f :: "'a::euclidean_space ⇒ real"
assumes contf: "continuous_on (closed_segment a b) f"
and ab: "a ≠ b" "f a = f b"
obtains z where "z ∈ open_segment a b"
"(∀w ∈ closed_segment a b. (f w) ≤ (f z)) ∨
(∀w ∈ closed_segment a b. (f z) ≤ (f w))"
proof -
obtain c where "c ∈ closed_segment a b" and c: "⋀y. y ∈ closed_segment a b ⟹ f y ≤ f c"
using continuous_attains_sup [of "closed_segment a b" f] contf by auto
moreover
obtain d where "d ∈ closed_segment a b" and d: "⋀y. y ∈ closed_segment a b ⟹ f d ≤ f y"
using continuous_attains_inf [of "closed_segment a b" f] contf by auto
ultimately show ?thesis
by (smt (verit) UnE ab closed_segment_eq_open empty_iff insert_iff midpoint_in_open_segment that)
qed

text‹An injective map into R is also an open map w.r.T. the universe, and conversely. ›
proposition injective_eq_1d_open_map_UNIV:
fixes f :: "real ⇒ real"
assumes contf: "continuous_on S f" and S: "is_interval S"
shows "inj_on f S ⟷ (∀T. open T ∧ T ⊆ S ⟶ open(f ` T))"
(is "?lhs = ?rhs")
proof safe
fix T
assume injf: ?lhs and "open T" and "T ⊆ S"
have "∃U. open U ∧ f x ∈ U ∧ U ⊆ f ` T" if "x ∈ T" for x
proof -
obtain δ where "δ > 0" and δ: "cball x δ ⊆ T"
using ‹open T› ‹x ∈ T› open_contains_cball_eq by blast
show ?thesis
proof (intro exI conjI)
have "closed_segment (x-δ) (x+δ) = {x-δ..x+δ}"
using ‹0 < δ› by (auto simp: closed_segment_eq_real_ivl)
also have "… ⊆ S"
using δ ‹T ⊆ S› by (auto simp: dist_norm subset_eq)
finally have "f ` (open_segment (x-δ) (x+δ)) = open_segment (f (x-δ)) (f (x+δ))"
using continuous_injective_image_open_segment_1
by (metis continuous_on_subset [OF contf] inj_on_subset [OF injf])
then show "open (f ` {x-δ<..<x+δ})"
using ‹0 < δ› by (simp add: open_segment_eq_real_ivl)
show "f x ∈ f ` {x - δ<..<x + δ}"
by (auto simp: ‹δ > 0›)
show "f ` {x - δ<..<x + δ} ⊆ f ` T"
using δ by (auto simp: dist_norm subset_iff)
qed
qed
with open_subopen show "open (f ` T)"
by blast
next
assume R: ?rhs
have False if xy: "x ∈ S" "y ∈ S" and "f x = f y" "x ≠ y" for x y
proof -
have "open (f ` open_segment x y)"
using R
by (metis S convex_contains_open_segment is_interval_convex open_greaterThanLessThan open_segment_eq_real_ivl xy)
moreover
have "continuous_on (closed_segment x y) f"
by (meson S closed_segment_subset contf continuous_on_subset is_interval_convex that)
then obtain ξ where "ξ ∈ open_segment x y"
and ξ: "(∀w ∈ closed_segment x y. (f w) ≤ (f ξ)) ∨
(∀w ∈ closed_segment x y. (f ξ) ≤ (f w))"
using continuous_IVT_local_extremum [of x y f] ‹f x = f y› ‹x ≠ y› by blast
ultimately obtain e where "e>0" and e: "⋀u. dist u (f ξ) < e ⟹ u ∈ f ` open_segment x y"
using open_dist by (metis image_eqI)
have fin: "f ξ + (e/2) ∈ f ` open_segment x y" "f ξ - (e/2) ∈ f ` open_segment x y"
using e [of "f ξ + (e/2)"] e [of "f ξ - (e/2)"] ‹e > 0› by (auto simp: dist_norm)
show ?thesis
using ξ ‹0 < e› fin open_closed_segment by fastforce
qed
then show ?lhs
by (force simp: inj_on_def)
qed

subsection✐‹tag unimportant› ‹Bounding a point away from a path›

lemma not_on_path_ball:
fixes g :: "real ⇒ 'a::heine_borel"
assumes "path g"
and z: "z ∉ path_image g"
shows "∃e > 0. ball z e ∩ path_image g = {}"
proof -
have "closed (path_image g)"
by (simp add: ‹path g› closed_path_image)
then obtain a where "a ∈ path_image g" "∀y ∈ path_image g. dist z a ≤ dist z y"
by (auto intro: distance_attains_inf[OF _ path_image_nonempty, of g z])
then show ?thesis
by (rule_tac x="dist z a" in exI) (use dist_commute z in auto)
qed

lemma not_on_path_cball:
fixes g :: "real ⇒ 'a::heine_borel"
assumes "path g"
and "z ∉ path_image g"
shows "∃e>0. cball z e ∩ (path_image g) = {}"
by (smt (verit, ccfv_threshold) open_ball assms centre_in_ball inf.orderE inf_assoc
inf_bot_right not_on_path_ball open_contains_cball_eq)

subsection ‹Path component›

text ‹Original formalization by Tom Hales›

definition✐‹tag important› "path_component S x y ≡
(∃g. ```