Theory Cauchy_Integral_Thm

theory Cauchy_Integral_Thm
imports Complex_Transcendental Weierstrass Ordered_Euclidean_Space
section ‹Complex path integrals and Cauchy's integral theorem›

text‹By John Harrison et al.  Ported from HOL Light by L C Paulson (2015)›

theory Cauchy_Integral_Thm
imports Complex_Transcendental Weierstrass Ordered_Euclidean_Space
begin

subsection‹Homeomorphisms of arc images›

lemma homeomorphism_arc:
  fixes g :: "real ⇒ 'a::t2_space"
  assumes "arc g"
  obtains h where "homeomorphism {0..1} (path_image g) g h"
using assms by (force simp add: arc_def homeomorphism_compact path_def path_image_def)

lemma homeomorphic_arc_image_interval:
  fixes g :: "real ⇒ 'a::t2_space" and a::real
  assumes "arc g" "a < b"
  shows "(path_image g) homeomorphic {a..b}"
proof -
  have "(path_image g) homeomorphic {0..1::real}"
    by (meson assms(1) homeomorphic_def homeomorphic_sym homeomorphism_arc)
  also have "... homeomorphic {a..b}"
    using assms by (force intro: homeomorphic_closed_intervals_real)
  finally show ?thesis .
qed

lemma homeomorphic_arc_images:
  fixes g :: "real ⇒ 'a::t2_space" and h :: "real ⇒ 'b::t2_space"
  assumes "arc g" "arc h"
  shows "(path_image g) homeomorphic (path_image h)"
proof -
  have "(path_image g) homeomorphic {0..1::real}"
    by (meson assms homeomorphic_def homeomorphic_sym homeomorphism_arc)
  also have "... homeomorphic (path_image h)"
    by (meson assms homeomorphic_def homeomorphism_arc)
  finally show ?thesis .
qed

subsection ‹Piecewise differentiable functions›

definition piecewise_differentiable_on
           (infixr "piecewise'_differentiable'_on" 50)
  where "f piecewise_differentiable_on i  ≡
           continuous_on i f ∧
           (∃s. finite s ∧ (∀x ∈ i - s. f differentiable (at x within i)))"

lemma piecewise_differentiable_on_imp_continuous_on:
    "f piecewise_differentiable_on s ⟹ continuous_on s f"
by (simp add: piecewise_differentiable_on_def)

lemma piecewise_differentiable_on_subset:
    "f piecewise_differentiable_on s ⟹ t ≤ s ⟹ f piecewise_differentiable_on t"
  using continuous_on_subset
  unfolding piecewise_differentiable_on_def
  apply safe
  apply (blast intro: elim: continuous_on_subset)
  by (meson Diff_iff differentiable_within_subset subsetCE)

lemma differentiable_on_imp_piecewise_differentiable:
  fixes a:: "'a::{linorder_topology,real_normed_vector}"
  shows "f differentiable_on {a..b} ⟹ f piecewise_differentiable_on {a..b}"
  apply (simp add: piecewise_differentiable_on_def differentiable_imp_continuous_on)
  apply (rule_tac x="{a,b}" in exI, simp add: differentiable_on_def)
  done

lemma differentiable_imp_piecewise_differentiable:
    "(⋀x. x ∈ s ⟹ f differentiable (at x within s))
         ⟹ f piecewise_differentiable_on s"
by (auto simp: piecewise_differentiable_on_def differentiable_imp_continuous_on differentiable_on_def
         intro: differentiable_within_subset)

lemma piecewise_differentiable_const [iff]: "(λx. z) piecewise_differentiable_on s"
  by (simp add: differentiable_imp_piecewise_differentiable)

lemma piecewise_differentiable_compose:
    "⟦f piecewise_differentiable_on s; g piecewise_differentiable_on (f ` s);
      ⋀x. finite (s ∩ f-`{x})⟧
      ⟹ (g o f) piecewise_differentiable_on s"
  apply (simp add: piecewise_differentiable_on_def, safe)
  apply (blast intro: continuous_on_compose2)
  apply (rename_tac A B)
  apply (rule_tac x="A ∪ (⋃x∈B. s ∩ f-`{x})" in exI)
  apply (blast intro: differentiable_chain_within)
  done

lemma piecewise_differentiable_affine:
  fixes m::real
  assumes "f piecewise_differentiable_on ((λx. m *R x + c) ` s)"
  shows "(f o (λx. m *R x + c)) piecewise_differentiable_on s"
proof (cases "m = 0")
  case True
  then show ?thesis
    unfolding o_def
    by (force intro: differentiable_imp_piecewise_differentiable differentiable_const)
next
  case False
  show ?thesis
    apply (rule piecewise_differentiable_compose [OF differentiable_imp_piecewise_differentiable])
    apply (rule assms derivative_intros | simp add: False vimage_def real_vector_affinity_eq)+
    done
qed

lemma piecewise_differentiable_cases:
  fixes c::real
  assumes "f piecewise_differentiable_on {a..c}"
          "g piecewise_differentiable_on {c..b}"
           "a ≤ c" "c ≤ b" "f c = g c"
  shows "(λx. if x ≤ c then f x else g x) piecewise_differentiable_on {a..b}"
proof -
  obtain s t where st: "finite s" "finite t"
                       "∀x∈{a..c} - s. f differentiable at x within {a..c}"
                       "∀x∈{c..b} - t. g differentiable at x within {c..b}"
    using assms
    by (auto simp: piecewise_differentiable_on_def)
  have finabc: "finite ({a,b,c} ∪ (s ∪ t))"
    by (metis ‹finite s› ‹finite t› finite_Un finite_insert finite.emptyI)
  have "continuous_on {a..c} f" "continuous_on {c..b} g"
    using assms piecewise_differentiable_on_def by auto
  then have "continuous_on {a..b} (λx. if x ≤ c then f x else g x)"
    using continuous_on_cases [OF closed_real_atLeastAtMost [of a c],
                               OF closed_real_atLeastAtMost [of c b],
                               of f g "λx. x≤c"]  assms
    by (force simp: ivl_disj_un_two_touch)
  moreover
  { fix x
    assume x: "x ∈ {a..b} - ({a,b,c} ∪ (s ∪ t))"
    have "(λx. if x ≤ c then f x else g x) differentiable at x within {a..b}" (is "?diff_fg")
    proof (cases x c rule: le_cases)
      case le show ?diff_fg
        apply (rule differentiable_transform_within [where d = "dist x c" and f = f])
        using x le st
        apply (simp_all add: dist_real_def)
        apply (rule differentiable_at_withinI)
        apply (rule differentiable_within_open [where s = "{a<..<c} - s", THEN iffD1], simp_all)
        apply (blast intro: open_greaterThanLessThan finite_imp_closed)
        apply (force elim!: differentiable_subset)+
        done
    next
      case ge show ?diff_fg
        apply (rule differentiable_transform_within [where d = "dist x c" and f = g])
        using x ge st
        apply (simp_all add: dist_real_def)
        apply (rule differentiable_at_withinI)
        apply (rule differentiable_within_open [where s = "{c<..<b} - t", THEN iffD1], simp_all)
        apply (blast intro: open_greaterThanLessThan finite_imp_closed)
        apply (force elim!: differentiable_subset)+
        done
    qed
  }
  then have "∃s. finite s ∧
                 (∀x∈{a..b} - s. (λx. if x ≤ c then f x else g x) differentiable at x within {a..b})"
    by (meson finabc)
  ultimately show ?thesis
    by (simp add: piecewise_differentiable_on_def)
qed

lemma piecewise_differentiable_neg:
    "f piecewise_differentiable_on s ⟹ (λx. -(f x)) piecewise_differentiable_on s"
  by (auto simp: piecewise_differentiable_on_def continuous_on_minus)

lemma piecewise_differentiable_add:
  assumes "f piecewise_differentiable_on i"
          "g piecewise_differentiable_on i"
    shows "(λx. f x + g x) piecewise_differentiable_on i"
proof -
  obtain s t where st: "finite s" "finite t"
                       "∀x∈i - s. f differentiable at x within i"
                       "∀x∈i - t. g differentiable at x within i"
    using assms by (auto simp: piecewise_differentiable_on_def)
  then have "finite (s ∪ t) ∧ (∀x∈i - (s ∪ t). (λx. f x + g x) differentiable at x within i)"
    by auto
  moreover have "continuous_on i f" "continuous_on i g"
    using assms piecewise_differentiable_on_def by auto
  ultimately show ?thesis
    by (auto simp: piecewise_differentiable_on_def continuous_on_add)
qed

lemma piecewise_differentiable_diff:
    "⟦f piecewise_differentiable_on s;  g piecewise_differentiable_on s⟧
     ⟹ (λx. f x - g x) piecewise_differentiable_on s"
  unfolding diff_conv_add_uminus
  by (metis piecewise_differentiable_add piecewise_differentiable_neg)

lemma continuous_on_joinpaths_D1:
    "continuous_on {0..1} (g1 +++ g2) ⟹ continuous_on {0..1} g1"
  apply (rule continuous_on_eq [of _ "(g1 +++ g2) o (op*(inverse 2))"])
  apply (rule continuous_intros | simp)+
  apply (auto elim!: continuous_on_subset simp: joinpaths_def)
  done

lemma continuous_on_joinpaths_D2:
    "⟦continuous_on {0..1} (g1 +++ g2); pathfinish g1 = pathstart g2⟧ ⟹ continuous_on {0..1} g2"
  apply (rule continuous_on_eq [of _ "(g1 +++ g2) o (λx. inverse 2*x + 1/2)"])
  apply (rule continuous_intros | simp)+
  apply (auto elim!: continuous_on_subset simp add: joinpaths_def pathfinish_def pathstart_def Ball_def)
  done

lemma piecewise_differentiable_D1:
    "(g1 +++ g2) piecewise_differentiable_on {0..1} ⟹ g1 piecewise_differentiable_on {0..1}"
  apply (clarsimp simp add: piecewise_differentiable_on_def dest!: continuous_on_joinpaths_D1)
  apply (rule_tac x="insert 1 ((op*2)`s)" in exI)
  apply simp
  apply (intro ballI)
  apply (rule_tac d="dist (x/2) (1/2)" and f = "(g1 +++ g2) o (op*(inverse 2))"
       in differentiable_transform_within)
  apply (auto simp: dist_real_def joinpaths_def)
  apply (rule differentiable_chain_within derivative_intros | simp)+
  apply (rule differentiable_subset)
  apply (force simp:)+
  done

lemma piecewise_differentiable_D2:
    "⟦(g1 +++ g2) piecewise_differentiable_on {0..1}; pathfinish g1 = pathstart g2⟧
    ⟹ g2 piecewise_differentiable_on {0..1}"
  apply (clarsimp simp add: piecewise_differentiable_on_def dest!: continuous_on_joinpaths_D2)
  apply (rule_tac x="insert 0 ((λx. 2*x-1)`s)" in exI)
  apply simp
  apply (intro ballI)
  apply (rule_tac d="dist ((x+1)/2) (1/2)" and f = "(g1 +++ g2) o (λx. (x+1)/2)"
          in differentiable_transform_within)
  apply (auto simp: dist_real_def joinpaths_def abs_if field_simps split: if_split_asm)
  apply (rule differentiable_chain_within derivative_intros | simp)+
  apply (rule differentiable_subset)
  apply (force simp: divide_simps)+
  done


subsubsection‹The concept of continuously differentiable›

text ‹
John Harrison writes as follows:

``The usual assumption in complex analysis texts is that a path ‹γ› should be piecewise
continuously differentiable, which ensures that the path integral exists at least for any continuous
f, since all piecewise continuous functions are integrable. However, our notion of validity is
weaker, just piecewise differentiability... [namely] continuity plus differentiability except on a
finite set ... [Our] underlying theory of integration is the Kurzweil-Henstock theory. In contrast to
the Riemann or Lebesgue theory (but in common with a simple notion based on antiderivatives), this
can integrate all derivatives.''

"Formalizing basic complex analysis." From Insight to Proof: Festschrift in Honour of Andrzej Trybulec.
Studies in Logic, Grammar and Rhetoric 10.23 (2007): 151-165.

And indeed he does not assume that his derivatives are continuous, but the penalty is unreasonably
difficult proofs concerning winding numbers. We need a self-contained and straightforward theorem
asserting that all derivatives can be integrated before we can adopt Harrison's choice.›

definition C1_differentiable_on :: "(real ⇒ 'a::real_normed_vector) ⇒ real set ⇒ bool"
           (infix "C1'_differentiable'_on" 50)
  where
  "f C1_differentiable_on s ⟷
   (∃D. (∀x ∈ s. (f has_vector_derivative (D x)) (at x)) ∧ continuous_on s D)"

lemma C1_differentiable_on_eq:
    "f C1_differentiable_on s ⟷
     (∀x ∈ s. f differentiable at x) ∧ continuous_on s (λx. vector_derivative f (at x))"
  unfolding C1_differentiable_on_def
  apply safe
  using differentiable_def has_vector_derivative_def apply blast
  apply (erule continuous_on_eq)
  using vector_derivative_at apply fastforce
  using vector_derivative_works apply fastforce
  done

lemma C1_differentiable_on_subset:
  "f C1_differentiable_on t ⟹ s ⊆ t ⟹ f C1_differentiable_on s"
  unfolding C1_differentiable_on_def  continuous_on_eq_continuous_within
  by (blast intro:  continuous_within_subset)

lemma C1_differentiable_compose:
    "⟦f C1_differentiable_on s; g C1_differentiable_on (f ` s);
      ⋀x. finite (s ∩ f-`{x})⟧
      ⟹ (g o f) C1_differentiable_on s"
  apply (simp add: C1_differentiable_on_eq, safe)
   using differentiable_chain_at apply blast
  apply (rule continuous_on_eq [of _ "λx. vector_derivative f (at x) *R vector_derivative g (at (f x))"])
   apply (rule Limits.continuous_on_scaleR, assumption)
   apply (metis (mono_tags, lifting) continuous_on_eq continuous_at_imp_continuous_on continuous_on_compose differentiable_imp_continuous_within o_def)
  by (simp add: vector_derivative_chain_at)

lemma C1_diff_imp_diff: "f C1_differentiable_on s ⟹ f differentiable_on s"
  by (simp add: C1_differentiable_on_eq differentiable_at_imp_differentiable_on)

lemma C1_differentiable_on_ident [simp, derivative_intros]: "(λx. x) C1_differentiable_on s"
  by (auto simp: C1_differentiable_on_eq continuous_on_const)

lemma C1_differentiable_on_const [simp, derivative_intros]: "(λz. a) C1_differentiable_on s"
  by (auto simp: C1_differentiable_on_eq continuous_on_const)

lemma C1_differentiable_on_add [simp, derivative_intros]:
  "f C1_differentiable_on s ⟹ g C1_differentiable_on s ⟹ (λx. f x + g x) C1_differentiable_on s"
  unfolding C1_differentiable_on_eq  by (auto intro: continuous_intros)

lemma C1_differentiable_on_minus [simp, derivative_intros]:
  "f C1_differentiable_on s ⟹ (λx. - f x) C1_differentiable_on s"
  unfolding C1_differentiable_on_eq  by (auto intro: continuous_intros)

lemma C1_differentiable_on_diff [simp, derivative_intros]:
  "f C1_differentiable_on s ⟹ g C1_differentiable_on s ⟹ (λx. f x - g x) C1_differentiable_on s"
  unfolding C1_differentiable_on_eq  by (auto intro: continuous_intros)

lemma C1_differentiable_on_mult [simp, derivative_intros]:
  fixes f g :: "real ⇒ 'a :: real_normed_algebra"
  shows "f C1_differentiable_on s ⟹ g C1_differentiable_on s ⟹ (λx. f x * g x) C1_differentiable_on s"
  unfolding C1_differentiable_on_eq
  by (auto simp: continuous_on_add continuous_on_mult continuous_at_imp_continuous_on differentiable_imp_continuous_within)

lemma C1_differentiable_on_scaleR [simp, derivative_intros]:
  "f C1_differentiable_on s ⟹ g C1_differentiable_on s ⟹ (λx. f x *R g x) C1_differentiable_on s"
  unfolding C1_differentiable_on_eq
  by (rule continuous_intros | simp add: continuous_at_imp_continuous_on differentiable_imp_continuous_within)+


definition piecewise_C1_differentiable_on
           (infixr "piecewise'_C1'_differentiable'_on" 50)
  where "f piecewise_C1_differentiable_on i  ≡
           continuous_on i f ∧
           (∃s. finite s ∧ (f C1_differentiable_on (i - s)))"

lemma C1_differentiable_imp_piecewise:
    "f C1_differentiable_on s ⟹ f piecewise_C1_differentiable_on s"
  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq continuous_at_imp_continuous_on differentiable_imp_continuous_within)

lemma piecewise_C1_imp_differentiable:
    "f piecewise_C1_differentiable_on i ⟹ f piecewise_differentiable_on i"
  by (auto simp: piecewise_C1_differentiable_on_def piecewise_differentiable_on_def
           C1_differentiable_on_def differentiable_def has_vector_derivative_def
           intro: has_derivative_at_within)

lemma piecewise_C1_differentiable_compose:
    "⟦f piecewise_C1_differentiable_on s; g piecewise_C1_differentiable_on (f ` s);
      ⋀x. finite (s ∩ f-`{x})⟧
      ⟹ (g o f) piecewise_C1_differentiable_on s"
  apply (simp add: piecewise_C1_differentiable_on_def, safe)
  apply (blast intro: continuous_on_compose2)
  apply (rename_tac A B)
  apply (rule_tac x="A ∪ (⋃x∈B. s ∩ f-`{x})" in exI)
  apply (rule conjI, blast)
  apply (rule C1_differentiable_compose)
  apply (blast intro: C1_differentiable_on_subset)
  apply (blast intro: C1_differentiable_on_subset)
  by (simp add: Diff_Int_distrib2)

lemma piecewise_C1_differentiable_on_subset:
    "f piecewise_C1_differentiable_on s ⟹ t ≤ s ⟹ f piecewise_C1_differentiable_on t"
  by (auto simp: piecewise_C1_differentiable_on_def elim!: continuous_on_subset C1_differentiable_on_subset)

lemma C1_differentiable_imp_continuous_on:
  "f C1_differentiable_on s ⟹ continuous_on s f"
  unfolding C1_differentiable_on_eq continuous_on_eq_continuous_within
  using differentiable_at_withinI differentiable_imp_continuous_within by blast

lemma C1_differentiable_on_empty [iff]: "f C1_differentiable_on {}"
  unfolding C1_differentiable_on_def
  by auto

lemma piecewise_C1_differentiable_affine:
  fixes m::real
  assumes "f piecewise_C1_differentiable_on ((λx. m * x + c) ` s)"
  shows "(f o (λx. m *R x + c)) piecewise_C1_differentiable_on s"
proof (cases "m = 0")
  case True
  then show ?thesis
    unfolding o_def by (auto simp: piecewise_C1_differentiable_on_def continuous_on_const)
next
  case False
  show ?thesis
    apply (rule piecewise_C1_differentiable_compose [OF C1_differentiable_imp_piecewise])
    apply (rule assms derivative_intros | simp add: False vimage_def)+
    using real_vector_affinity_eq [OF False, where c=c, unfolded scaleR_conv_of_real]
    apply simp
    done
qed

lemma piecewise_C1_differentiable_cases:
  fixes c::real
  assumes "f piecewise_C1_differentiable_on {a..c}"
          "g piecewise_C1_differentiable_on {c..b}"
           "a ≤ c" "c ≤ b" "f c = g c"
  shows "(λx. if x ≤ c then f x else g x) piecewise_C1_differentiable_on {a..b}"
proof -
  obtain s t where st: "f C1_differentiable_on ({a..c} - s)"
                       "g C1_differentiable_on ({c..b} - t)"
                       "finite s" "finite t"
    using assms
    by (force simp: piecewise_C1_differentiable_on_def)
  then have f_diff: "f differentiable_on {a..<c} - s"
        and g_diff: "g differentiable_on {c<..b} - t"
    by (simp_all add: C1_differentiable_on_eq differentiable_at_withinI differentiable_on_def)
  have "continuous_on {a..c} f" "continuous_on {c..b} g"
    using assms piecewise_C1_differentiable_on_def by auto
  then have cab: "continuous_on {a..b} (λx. if x ≤ c then f x else g x)"
    using continuous_on_cases [OF closed_real_atLeastAtMost [of a c],
                               OF closed_real_atLeastAtMost [of c b],
                               of f g "λx. x≤c"]  assms
    by (force simp: ivl_disj_un_two_touch)
  { fix x
    assume x: "x ∈ {a..b} - insert c (s ∪ t)"
    have "(λx. if x ≤ c then f x else g x) differentiable at x" (is "?diff_fg")
    proof (cases x c rule: le_cases)
      case le show ?diff_fg
        apply (rule differentiable_transform_within [where f=f and d = "dist x c"])
        using x dist_real_def le st by (auto simp: C1_differentiable_on_eq)
    next
      case ge show ?diff_fg
        apply (rule differentiable_transform_within [where f=g and d = "dist x c"])
        using dist_nz x dist_real_def ge st x by (auto simp: C1_differentiable_on_eq)
    qed
  }
  then have "(∀x ∈ {a..b} - insert c (s ∪ t). (λx. if x ≤ c then f x else g x) differentiable at x)"
    by auto
  moreover
  { assume fcon: "continuous_on ({a<..<c} - s) (λx. vector_derivative f (at x))"
       and gcon: "continuous_on ({c<..<b} - t) (λx. vector_derivative g (at x))"
    have "open ({a<..<c} - s)"  "open ({c<..<b} - t)"
      using st by (simp_all add: open_Diff finite_imp_closed)
    moreover have "continuous_on ({a<..<c} - s) (λx. vector_derivative (λx. if x ≤ c then f x else g x) (at x))"
      apply (rule continuous_on_eq [OF fcon])
      apply (simp add:)
      apply (rule vector_derivative_at [symmetric])
      apply (rule_tac f=f and d="dist x c" in has_vector_derivative_transform_within)
      apply (simp_all add: dist_norm vector_derivative_works [symmetric])
      apply (metis (full_types) C1_differentiable_on_eq Diff_iff Groups.add_ac(2) add_mono_thms_linordered_field(5) atLeastAtMost_iff linorder_not_le order_less_irrefl st(1))
      apply auto
      done
    moreover have "continuous_on ({c<..<b} - t) (λx. vector_derivative (λx. if x ≤ c then f x else g x) (at x))"
      apply (rule continuous_on_eq [OF gcon])
      apply (simp add:)
      apply (rule vector_derivative_at [symmetric])
      apply (rule_tac f=g and d="dist x c" in has_vector_derivative_transform_within)
      apply (simp_all add: dist_norm vector_derivative_works [symmetric])
      apply (metis (full_types) C1_differentiable_on_eq Diff_iff Groups.add_ac(2) add_mono_thms_linordered_field(5) atLeastAtMost_iff less_irrefl not_le st(2))
      apply auto
      done
    ultimately have "continuous_on ({a<..<b} - insert c (s ∪ t))
        (λx. vector_derivative (λx. if x ≤ c then f x else g x) (at x))"
      apply (rule continuous_on_subset [OF continuous_on_open_Un], auto)
      done
  } note * = this
  have "continuous_on ({a<..<b} - insert c (s ∪ t)) (λx. vector_derivative (λx. if x ≤ c then f x else g x) (at x))"
    using st
    by (auto simp: C1_differentiable_on_eq elim!: continuous_on_subset intro: *)
  ultimately have "∃s. finite s ∧ ((λx. if x ≤ c then f x else g x) C1_differentiable_on {a..b} - s)"
    apply (rule_tac x="{a,b,c} ∪ s ∪ t" in exI)
    using st  by (auto simp: C1_differentiable_on_eq elim!: continuous_on_subset)
  with cab show ?thesis
    by (simp add: piecewise_C1_differentiable_on_def)
qed

lemma piecewise_C1_differentiable_neg:
    "f piecewise_C1_differentiable_on s ⟹ (λx. -(f x)) piecewise_C1_differentiable_on s"
  unfolding piecewise_C1_differentiable_on_def
  by (auto intro!: continuous_on_minus C1_differentiable_on_minus)

lemma piecewise_C1_differentiable_add:
  assumes "f piecewise_C1_differentiable_on i"
          "g piecewise_C1_differentiable_on i"
    shows "(λx. f x + g x) piecewise_C1_differentiable_on i"
proof -
  obtain s t where st: "finite s" "finite t"
                       "f C1_differentiable_on (i-s)"
                       "g C1_differentiable_on (i-t)"
    using assms by (auto simp: piecewise_C1_differentiable_on_def)
  then have "finite (s ∪ t) ∧ (λx. f x + g x) C1_differentiable_on i - (s ∪ t)"
    by (auto intro: C1_differentiable_on_add elim!: C1_differentiable_on_subset)
  moreover have "continuous_on i f" "continuous_on i g"
    using assms piecewise_C1_differentiable_on_def by auto
  ultimately show ?thesis
    by (auto simp: piecewise_C1_differentiable_on_def continuous_on_add)
qed

lemma piecewise_C1_differentiable_diff:
    "⟦f piecewise_C1_differentiable_on s;  g piecewise_C1_differentiable_on s⟧
     ⟹ (λx. f x - g x) piecewise_C1_differentiable_on s"
  unfolding diff_conv_add_uminus
  by (metis piecewise_C1_differentiable_add piecewise_C1_differentiable_neg)

lemma piecewise_C1_differentiable_D1:
  fixes g1 :: "real ⇒ 'a::real_normed_field"
  assumes "(g1 +++ g2) piecewise_C1_differentiable_on {0..1}"
    shows "g1 piecewise_C1_differentiable_on {0..1}"
proof -
  obtain s where "finite s"
             and co12: "continuous_on ({0..1} - s) (λx. vector_derivative (g1 +++ g2) (at x))"
             and g12D: "∀x∈{0..1} - s. g1 +++ g2 differentiable at x"
    using assms  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  then have g1D: "g1 differentiable at x" if "x ∈ {0..1} - insert 1 (op * 2 ` s)" for x
    apply (rule_tac d="dist (x/2) (1/2)" and f = "(g1 +++ g2) o (op*(inverse 2))" in differentiable_transform_within)
    using that
    apply (simp_all add: dist_real_def joinpaths_def)
    apply (rule differentiable_chain_at derivative_intros | force)+
    done
  have [simp]: "vector_derivative (g1 ∘ op * 2) (at (x/2)) = 2 *R vector_derivative g1 (at x)"
               if "x ∈ {0..1} - insert 1 (op * 2 ` s)" for x
    apply (subst vector_derivative_chain_at)
    using that
    apply (rule derivative_eq_intros g1D | simp)+
    done
  have "continuous_on ({0..1/2} - insert (1/2) s) (λx. vector_derivative (g1 +++ g2) (at x))"
    using co12 by (rule continuous_on_subset) force
  then have coDhalf: "continuous_on ({0..1/2} - insert (1/2) s) (λx. vector_derivative (g1 o op*2) (at x))"
    apply (rule continuous_on_eq [OF _ vector_derivative_at])
    apply (rule_tac f="g1 o op*2" and d="dist x (1/2)" in has_vector_derivative_transform_within)
    apply (simp_all add: dist_norm joinpaths_def vector_derivative_works [symmetric])
    apply (force intro: g1D differentiable_chain_at)
    apply auto
    done
  have "continuous_on ({0..1} - insert 1 (op * 2 ` s))
                      ((λx. 1/2 * vector_derivative (g1 o op*2) (at x)) o op*(1/2))"
    apply (rule continuous_intros)+
    using coDhalf
    apply (simp add: scaleR_conv_of_real image_set_diff image_image)
    done
  then have con_g1: "continuous_on ({0..1} - insert 1 (op * 2 ` s)) (λx. vector_derivative g1 (at x))"
    by (rule continuous_on_eq) (simp add: scaleR_conv_of_real)
  have "continuous_on {0..1} g1"
    using continuous_on_joinpaths_D1 assms piecewise_C1_differentiable_on_def by blast
  with ‹finite s› show ?thesis
    apply (clarsimp simp add: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
    apply (rule_tac x="insert 1 ((op*2)`s)" in exI)
    apply (simp add: g1D con_g1)
  done
qed

lemma piecewise_C1_differentiable_D2:
  fixes g2 :: "real ⇒ 'a::real_normed_field"
  assumes "(g1 +++ g2) piecewise_C1_differentiable_on {0..1}" "pathfinish g1 = pathstart g2"
    shows "g2 piecewise_C1_differentiable_on {0..1}"
proof -
  obtain s where "finite s"
             and co12: "continuous_on ({0..1} - s) (λx. vector_derivative (g1 +++ g2) (at x))"
             and g12D: "∀x∈{0..1} - s. g1 +++ g2 differentiable at x"
    using assms  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  then have g2D: "g2 differentiable at x" if "x ∈ {0..1} - insert 0 ((λx. 2*x-1) ` s)" for x
    apply (rule_tac d="dist ((x+1)/2) (1/2)" and f = "(g1 +++ g2) o (λx. (x+1)/2)" in differentiable_transform_within)
    using that
    apply (simp_all add: dist_real_def joinpaths_def)
    apply (auto simp: dist_real_def joinpaths_def field_simps)
    apply (rule differentiable_chain_at derivative_intros | force)+
    apply (drule_tac x= "(x + 1) / 2" in bspec, force simp: divide_simps)
    apply assumption
    done
  have [simp]: "vector_derivative (g2 ∘ (λx. 2*x-1)) (at ((x+1)/2)) = 2 *R vector_derivative g2 (at x)"
               if "x ∈ {0..1} - insert 0 ((λx. 2*x-1) ` s)" for x
    using that  by (auto simp: vector_derivative_chain_at divide_simps g2D)
  have "continuous_on ({1/2..1} - insert (1/2) s) (λx. vector_derivative (g1 +++ g2) (at x))"
    using co12 by (rule continuous_on_subset) force
  then have coDhalf: "continuous_on ({1/2..1} - insert (1/2) s) (λx. vector_derivative (g2 o (λx. 2*x-1)) (at x))"
    apply (rule continuous_on_eq [OF _ vector_derivative_at])
    apply (rule_tac f="g2 o (λx. 2*x-1)" and d="dist (3/4) ((x+1)/2)" in has_vector_derivative_transform_within)
    apply (auto simp: dist_real_def field_simps joinpaths_def vector_derivative_works [symmetric]
                intro!: g2D differentiable_chain_at)
    done
  have [simp]: "((λx. (x + 1) / 2) ` ({0..1} - insert 0 ((λx. 2 * x - 1) ` s))) = ({1/2..1} - insert (1/2) s)"
    apply (simp add: image_set_diff inj_on_def image_image)
    apply (auto simp: image_affinity_atLeastAtMost_div add_divide_distrib)
    done
  have "continuous_on ({0..1} - insert 0 ((λx. 2*x-1) ` s))
                      ((λx. 1/2 * vector_derivative (g2 ∘ (λx. 2*x-1)) (at x)) o (λx. (x+1)/2))"
    by (rule continuous_intros | simp add:  coDhalf)+
  then have con_g2: "continuous_on ({0..1} - insert 0 ((λx. 2*x-1) ` s)) (λx. vector_derivative g2 (at x))"
    by (rule continuous_on_eq) (simp add: scaleR_conv_of_real)
  have "continuous_on {0..1} g2"
    using continuous_on_joinpaths_D2 assms piecewise_C1_differentiable_on_def by blast
  with ‹finite s› show ?thesis
    apply (clarsimp simp add: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
    apply (rule_tac x="insert 0 ((λx. 2 * x - 1) ` s)" in exI)
    apply (simp add: g2D con_g2)
  done
qed

subsection ‹Valid paths, and their start and finish›

lemma Diff_Un_eq: "A - (B ∪ C) = A - B - C"
  by blast

definition valid_path :: "(real ⇒ 'a :: real_normed_vector) ⇒ bool"
  where "valid_path f ≡ f piecewise_C1_differentiable_on {0..1::real}"

definition closed_path :: "(real ⇒ 'a :: real_normed_vector) ⇒ bool"
  where "closed_path g ≡ g 0 = g 1"

subsubsection‹In particular, all results for paths apply›

lemma valid_path_imp_path: "valid_path g ⟹ path g"
by (simp add: path_def piecewise_C1_differentiable_on_def valid_path_def)

lemma connected_valid_path_image: "valid_path g ⟹ connected(path_image g)"
  by (metis connected_path_image valid_path_imp_path)

lemma compact_valid_path_image: "valid_path g ⟹ compact(path_image g)"
  by (metis compact_path_image valid_path_imp_path)

lemma bounded_valid_path_image: "valid_path g ⟹ bounded(path_image g)"
  by (metis bounded_path_image valid_path_imp_path)

lemma closed_valid_path_image: "valid_path g ⟹ closed(path_image g)"
  by (metis closed_path_image valid_path_imp_path)

proposition valid_path_compose:
  assumes "valid_path g"
      and der: "⋀x. x ∈ path_image g ⟹ ∃f'. (f has_field_derivative f') (at x)"
      and con: "continuous_on (path_image g) (deriv f)"
    shows "valid_path (f o g)"
proof -
  obtain s where "finite s" and g_diff: "g C1_differentiable_on {0..1} - s"
    using ‹valid_path g› unfolding valid_path_def piecewise_C1_differentiable_on_def by auto
  have "f ∘ g differentiable at t" when "t∈{0..1} - s" for t
    proof (rule differentiable_chain_at)
      show "g differentiable at t" using ‹valid_path g›
        by (meson C1_differentiable_on_eq ‹g C1_differentiable_on {0..1} - s› that)
    next
      have "g t∈path_image g" using that DiffD1 image_eqI path_image_def by metis
      then obtain f' where "(f has_field_derivative f') (at (g t))"
        using der by auto
      then have " (f has_derivative op * f') (at (g t))"
        using has_field_derivative_imp_has_derivative[of f f' "at (g t)"] by auto
      then show "f differentiable at (g t)" using differentiableI by auto
    qed
  moreover have "continuous_on ({0..1} - s) (λx. vector_derivative (f ∘ g) (at x))"
    proof (rule continuous_on_eq [where f = "λx. vector_derivative g (at x) * deriv f (g x)"],
        rule continuous_intros)
      show "continuous_on ({0..1} - s) (λx. vector_derivative g (at x))"
        using g_diff C1_differentiable_on_eq by auto
    next
      have "continuous_on {0..1} (λx. deriv f (g x))"
        using continuous_on_compose[OF _ con[unfolded path_image_def],unfolded comp_def]
          ‹valid_path g› piecewise_C1_differentiable_on_def valid_path_def
        by blast
      then show "continuous_on ({0..1} - s) (λx. deriv f (g x))"
        using continuous_on_subset by blast
    next
      show "vector_derivative g (at t) * deriv f (g t) = vector_derivative (f ∘ g) (at t)"
          when "t ∈ {0..1} - s" for t
        proof (rule vector_derivative_chain_at_general[symmetric])
          show "g differentiable at t" by (meson C1_differentiable_on_eq g_diff that)
        next
          have "g t∈path_image g" using that DiffD1 image_eqI path_image_def by metis
          then obtain f' where "(f has_field_derivative f') (at (g t))"
            using der by auto
          then show "∃g'. (f has_field_derivative g') (at (g t))" by auto
        qed
    qed
  ultimately have "f o g C1_differentiable_on {0..1} - s"
    using C1_differentiable_on_eq by blast
  moreover have "path (f o g)"
    proof -
      have "isCont f x" when "x∈path_image g" for x
        proof -
          obtain f' where "(f has_field_derivative f') (at x)"
            using der[rule_format] ‹x∈path_image g› by auto
          thus ?thesis using DERIV_isCont by auto
        qed
      then have "continuous_on (path_image g) f" using continuous_at_imp_continuous_on by auto
      then show ?thesis using path_continuous_image ‹valid_path g› valid_path_imp_path by auto
    qed
  ultimately show ?thesis unfolding valid_path_def piecewise_C1_differentiable_on_def path_def
    using ‹finite s› by auto
qed


subsection‹Contour Integrals along a path›

text‹This definition is for complex numbers only, and does not generalise to line integrals in a vector field›

text‹piecewise differentiable function on [0,1]›

definition has_contour_integral :: "(complex ⇒ complex) ⇒ complex ⇒ (real ⇒ complex) ⇒ bool"
           (infixr "has'_contour'_integral" 50)
  where "(f has_contour_integral i) g ≡
           ((λx. f(g x) * vector_derivative g (at x within {0..1}))
            has_integral i) {0..1}"

definition contour_integrable_on
           (infixr "contour'_integrable'_on" 50)
  where "f contour_integrable_on g ≡ ∃i. (f has_contour_integral i) g"

definition contour_integral
  where "contour_integral g f ≡ @i. (f has_contour_integral i) g ∨ ~ f contour_integrable_on g ∧ i=0"

lemma not_integrable_contour_integral: "~ f contour_integrable_on g ⟹ contour_integral g f = 0"
  unfolding contour_integrable_on_def contour_integral_def by blast

lemma contour_integral_unique: "(f has_contour_integral i) g ⟹ contour_integral g f = i"
  apply (simp add: contour_integral_def has_contour_integral_def contour_integrable_on_def)
  using has_integral_unique by blast

corollary has_contour_integral_eqpath:
     "⟦(f has_contour_integral y) p; f contour_integrable_on γ;
       contour_integral p f = contour_integral γ f⟧
      ⟹ (f has_contour_integral y) γ"
using contour_integrable_on_def contour_integral_unique by auto

lemma has_contour_integral_integral:
    "f contour_integrable_on i ⟹ (f has_contour_integral (contour_integral i f)) i"
  by (metis contour_integral_unique contour_integrable_on_def)

lemma has_contour_integral_unique:
    "(f has_contour_integral i) g ⟹ (f has_contour_integral j) g ⟹ i = j"
  using has_integral_unique
  by (auto simp: has_contour_integral_def)

lemma has_contour_integral_integrable: "(f has_contour_integral i) g ⟹ f contour_integrable_on g"
  using contour_integrable_on_def by blast

(* Show that we can forget about the localized derivative.*)

lemma vector_derivative_within_interior:
     "⟦x ∈ interior s; NO_MATCH UNIV s⟧
      ⟹ vector_derivative f (at x within s) = vector_derivative f (at x)"
  apply (simp add: vector_derivative_def has_vector_derivative_def has_derivative_def netlimit_within_interior)
  apply (subst lim_within_interior, auto)
  done

lemma has_integral_localized_vector_derivative:
    "((λx. f (g x) * vector_derivative g (at x within {a..b})) has_integral i) {a..b} ⟷
     ((λx. f (g x) * vector_derivative g (at x)) has_integral i) {a..b}"
proof -
  have "{a..b} - {a,b} = interior {a..b}"
    by (simp add: atLeastAtMost_diff_ends)
  show ?thesis
    apply (rule has_integral_spike_eq [of "{a,b}"])
    apply (auto simp: vector_derivative_within_interior)
    done
qed

lemma integrable_on_localized_vector_derivative:
    "(λx. f (g x) * vector_derivative g (at x within {a..b})) integrable_on {a..b} ⟷
     (λx. f (g x) * vector_derivative g (at x)) integrable_on {a..b}"
  by (simp add: integrable_on_def has_integral_localized_vector_derivative)

lemma has_contour_integral:
     "(f has_contour_integral i) g ⟷
      ((λx. f (g x) * vector_derivative g (at x)) has_integral i) {0..1}"
  by (simp add: has_integral_localized_vector_derivative has_contour_integral_def)

lemma contour_integrable_on:
     "f contour_integrable_on g ⟷
      (λt. f(g t) * vector_derivative g (at t)) integrable_on {0..1}"
  by (simp add: has_contour_integral integrable_on_def contour_integrable_on_def)

subsection‹Reversing a path›

lemma valid_path_imp_reverse:
  assumes "valid_path g"
    shows "valid_path(reversepath g)"
proof -
  obtain s where "finite s" "g C1_differentiable_on ({0..1} - s)"
    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
  then have "finite (op - 1 ` s)" "(reversepath g C1_differentiable_on ({0..1} - op - 1 ` s))"
    apply (auto simp: reversepath_def)
    apply (rule C1_differentiable_compose [of "λx::real. 1-x" _ g, unfolded o_def])
    apply (auto simp: C1_differentiable_on_eq)
    apply (rule continuous_intros, force)
    apply (force elim!: continuous_on_subset)
    apply (simp add: finite_vimageI inj_on_def)
    done
  then show ?thesis using assms
    by (auto simp: valid_path_def piecewise_C1_differentiable_on_def path_def [symmetric])
qed

lemma valid_path_reversepath [simp]: "valid_path(reversepath g) ⟷ valid_path g"
  using valid_path_imp_reverse by force

lemma has_contour_integral_reversepath:
  assumes "valid_path g" "(f has_contour_integral i) g"
    shows "(f has_contour_integral (-i)) (reversepath g)"
proof -
  { fix s x
    assume xs: "g C1_differentiable_on ({0..1} - s)" "x ∉ op - 1 ` s" "0 ≤ x" "x ≤ 1"
      have "vector_derivative (λx. g (1 - x)) (at x within {0..1}) =
            - vector_derivative g (at (1 - x) within {0..1})"
      proof -
        obtain f' where f': "(g has_vector_derivative f') (at (1 - x))"
          using xs
          by (force simp: has_vector_derivative_def C1_differentiable_on_def)
        have "(g o (λx. 1 - x) has_vector_derivative -1 *R f') (at x)"
          apply (rule vector_diff_chain_within)
          apply (intro vector_diff_chain_within derivative_eq_intros | simp)+
          apply (rule has_vector_derivative_at_within [OF f'])
          done
        then have mf': "((λx. g (1 - x)) has_vector_derivative -f') (at x)"
          by (simp add: o_def)
        show ?thesis
          using xs
          by (auto simp: vector_derivative_at_within_ivl [OF mf'] vector_derivative_at_within_ivl [OF f'])
      qed
  } note * = this
  have 01: "{0..1::real} = cbox 0 1"
    by simp
  show ?thesis using assms
    apply (auto simp: has_contour_integral_def)
    apply (drule has_integral_affinity01 [where m= "-1" and c=1])
    apply (auto simp: reversepath_def valid_path_def piecewise_C1_differentiable_on_def)
    apply (drule has_integral_neg)
    apply (rule_tac s = "(λx. 1 - x) ` s" in has_integral_spike_finite)
    apply (auto simp: *)
    done
qed

lemma contour_integrable_reversepath:
    "valid_path g ⟹ f contour_integrable_on g ⟹ f contour_integrable_on (reversepath g)"
  using has_contour_integral_reversepath contour_integrable_on_def by blast

lemma contour_integrable_reversepath_eq:
    "valid_path g ⟹ (f contour_integrable_on (reversepath g) ⟷ f contour_integrable_on g)"
  using contour_integrable_reversepath valid_path_reversepath by fastforce

lemma contour_integral_reversepath:
  assumes "valid_path g"
    shows "contour_integral (reversepath g) f = - (contour_integral g f)"
proof (cases "f contour_integrable_on g")
  case True then show ?thesis
    by (simp add: assms contour_integral_unique has_contour_integral_integral has_contour_integral_reversepath)
next
  case False then have "~ f contour_integrable_on (reversepath g)"
    by (simp add: assms contour_integrable_reversepath_eq)
  with False show ?thesis by (simp add: not_integrable_contour_integral)
qed


subsection‹Joining two paths together›

lemma valid_path_join:
  assumes "valid_path g1" "valid_path g2" "pathfinish g1 = pathstart g2"
    shows "valid_path(g1 +++ g2)"
proof -
  have "g1 1 = g2 0"
    using assms by (auto simp: pathfinish_def pathstart_def)
  moreover have "(g1 o (λx. 2*x)) piecewise_C1_differentiable_on {0..1/2}"
    apply (rule piecewise_C1_differentiable_compose)
    using assms
    apply (auto simp: valid_path_def piecewise_C1_differentiable_on_def continuous_on_joinpaths)
    apply (rule continuous_intros | simp)+
    apply (force intro: finite_vimageI [where h = "op*2"] inj_onI)
    done
  moreover have "(g2 o (λx. 2*x-1)) piecewise_C1_differentiable_on {1/2..1}"
    apply (rule piecewise_C1_differentiable_compose)
    using assms unfolding valid_path_def piecewise_C1_differentiable_on_def
    by (auto intro!: continuous_intros finite_vimageI [where h = "(λx. 2*x - 1)"] inj_onI
             simp: image_affinity_atLeastAtMost_diff continuous_on_joinpaths)
  ultimately show ?thesis
    apply (simp only: valid_path_def continuous_on_joinpaths joinpaths_def)
    apply (rule piecewise_C1_differentiable_cases)
    apply (auto simp: o_def)
    done
qed

lemma valid_path_join_D1:
  fixes g1 :: "real ⇒ 'a::real_normed_field"
  shows "valid_path (g1 +++ g2) ⟹ valid_path g1"
  unfolding valid_path_def
  by (rule piecewise_C1_differentiable_D1)

lemma valid_path_join_D2:
  fixes g2 :: "real ⇒ 'a::real_normed_field"
  shows "⟦valid_path (g1 +++ g2); pathfinish g1 = pathstart g2⟧ ⟹ valid_path g2"
  unfolding valid_path_def
  by (rule piecewise_C1_differentiable_D2)

lemma valid_path_join_eq [simp]:
  fixes g2 :: "real ⇒ 'a::real_normed_field"
  shows "pathfinish g1 = pathstart g2 ⟹ (valid_path(g1 +++ g2) ⟷ valid_path g1 ∧ valid_path g2)"
  using valid_path_join_D1 valid_path_join_D2 valid_path_join by blast

lemma has_contour_integral_join:
  assumes "(f has_contour_integral i1) g1" "(f has_contour_integral i2) g2"
          "valid_path g1" "valid_path g2"
    shows "(f has_contour_integral (i1 + i2)) (g1 +++ g2)"
proof -
  obtain s1 s2
    where s1: "finite s1" "∀x∈{0..1} - s1. g1 differentiable at x"
      and s2: "finite s2" "∀x∈{0..1} - s2. g2 differentiable at x"
    using assms
    by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  have 1: "((λx. f (g1 x) * vector_derivative g1 (at x)) has_integral i1) {0..1}"
   and 2: "((λx. f (g2 x) * vector_derivative g2 (at x)) has_integral i2) {0..1}"
    using assms
    by (auto simp: has_contour_integral)
  have i1: "((λx. (2*f (g1 (2*x))) * vector_derivative g1 (at (2*x))) has_integral i1) {0..1/2}"
   and i2: "((λx. (2*f (g2 (2*x - 1))) * vector_derivative g2 (at (2*x - 1))) has_integral i2) {1/2..1}"
    using has_integral_affinity01 [OF 1, where m= 2 and c=0, THEN has_integral_cmul [where c=2]]
          has_integral_affinity01 [OF 2, where m= 2 and c="-1", THEN has_integral_cmul [where c=2]]
    by (simp_all only: image_affinity_atLeastAtMost_div_diff, simp_all add: scaleR_conv_of_real mult_ac)
  have g1: "⟦0 ≤ z; z*2 < 1; z*2 ∉ s1⟧ ⟹
            vector_derivative (λx. if x*2 ≤ 1 then g1 (2*x) else g2 (2*x - 1)) (at z) =
            2 *R vector_derivative g1 (at (z*2))" for z
    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(λx. g1(2*x))" and d = "¦z - 1/2¦"]])
    apply (simp_all add: dist_real_def abs_if split: if_split_asm)
    apply (rule vector_diff_chain_at [of "λx. 2*x" 2 _ g1, simplified o_def])
    apply (simp add: has_vector_derivative_def has_derivative_def bounded_linear_mult_left)
    using s1
    apply (auto simp: algebra_simps vector_derivative_works)
    done
  have g2: "⟦1 < z*2; z ≤ 1; z*2 - 1 ∉ s2⟧ ⟹
            vector_derivative (λx. if x*2 ≤ 1 then g1 (2*x) else g2 (2*x - 1)) (at z) =
            2 *R vector_derivative g2 (at (z*2 - 1))" for z
    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(λx. g2 (2*x - 1))" and d = "¦z - 1/2¦"]])
    apply (simp_all add: dist_real_def abs_if split: if_split_asm)
    apply (rule vector_diff_chain_at [of "λx. 2*x - 1" 2 _ g2, simplified o_def])
    apply (simp add: has_vector_derivative_def has_derivative_def bounded_linear_mult_left)
    using s2
    apply (auto simp: algebra_simps vector_derivative_works)
    done
  have "((λx. f ((g1 +++ g2) x) * vector_derivative (g1 +++ g2) (at x)) has_integral i1) {0..1/2}"
    apply (rule has_integral_spike_finite [OF _ _ i1, of "insert (1/2) (op*2 -` s1)"])
    using s1
    apply (force intro: finite_vimageI [where h = "op*2"] inj_onI)
    apply (clarsimp simp add: joinpaths_def scaleR_conv_of_real mult_ac g1)
    done
  moreover have "((λx. f ((g1 +++ g2) x) * vector_derivative (g1 +++ g2) (at x)) has_integral i2) {1/2..1}"
    apply (rule has_integral_spike_finite [OF _ _ i2, of "insert (1/2) ((λx. 2*x-1) -` s2)"])
    using s2
    apply (force intro: finite_vimageI [where h = "λx. 2*x-1"] inj_onI)
    apply (clarsimp simp add: joinpaths_def scaleR_conv_of_real mult_ac g2)
    done
  ultimately
  show ?thesis
    apply (simp add: has_contour_integral)
    apply (rule has_integral_combine [where c = "1/2"], auto)
    done
qed

lemma contour_integrable_joinI:
  assumes "f contour_integrable_on g1" "f contour_integrable_on g2"
          "valid_path g1" "valid_path g2"
    shows "f contour_integrable_on (g1 +++ g2)"
  using assms
  by (meson has_contour_integral_join contour_integrable_on_def)

lemma contour_integrable_joinD1:
  assumes "f contour_integrable_on (g1 +++ g2)" "valid_path g1"
    shows "f contour_integrable_on g1"
proof -
  obtain s1
    where s1: "finite s1" "∀x∈{0..1} - s1. g1 differentiable at x"
    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  have "(λx. f ((g1 +++ g2) (x/2)) * vector_derivative (g1 +++ g2) (at (x/2))) integrable_on {0..1}"
    using assms
    apply (auto simp: contour_integrable_on)
    apply (drule integrable_on_subcbox [where a=0 and b="1/2"])
    apply (auto intro: integrable_affinity [of _ 0 "1/2::real" "1/2" 0, simplified])
    done
  then have *: "(λx. (f ((g1 +++ g2) (x/2))/2) * vector_derivative (g1 +++ g2) (at (x/2))) integrable_on {0..1}"
    by (auto dest: integrable_cmul [where c="1/2"] simp: scaleR_conv_of_real)
  have g1: "⟦0 < z; z < 1; z ∉ s1⟧ ⟹
            vector_derivative (λx. if x*2 ≤ 1 then g1 (2*x) else g2 (2*x - 1)) (at (z/2)) =
            2 *R vector_derivative g1 (at z)"  for z
    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(λx. g1(2*x))" and d = "¦(z-1)/2¦"]])
    apply (simp_all add: field_simps dist_real_def abs_if split: if_split_asm)
    apply (rule vector_diff_chain_at [of "λx. x*2" 2 _ g1, simplified o_def])
    using s1
    apply (auto simp: vector_derivative_works has_vector_derivative_def has_derivative_def bounded_linear_mult_left)
    done
  show ?thesis
    using s1
    apply (auto simp: contour_integrable_on)
    apply (rule integrable_spike_finite [of "{0,1} ∪ s1", OF _ _ *])
    apply (auto simp: joinpaths_def scaleR_conv_of_real g1)
    done
qed

lemma contour_integrable_joinD2:
  assumes "f contour_integrable_on (g1 +++ g2)" "valid_path g2"
    shows "f contour_integrable_on g2"
proof -
  obtain s2
    where s2: "finite s2" "∀x∈{0..1} - s2. g2 differentiable at x"
    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  have "(λx. f ((g1 +++ g2) (x/2 + 1/2)) * vector_derivative (g1 +++ g2) (at (x/2 + 1/2))) integrable_on {0..1}"
    using assms
    apply (auto simp: contour_integrable_on)
    apply (drule integrable_on_subcbox [where a="1/2" and b=1], auto)
    apply (drule integrable_affinity [of _ "1/2::real" 1 "1/2" "1/2", simplified])
    apply (simp add: image_affinity_atLeastAtMost_diff)
    done
  then have *: "(λx. (f ((g1 +++ g2) (x/2 + 1/2))/2) * vector_derivative (g1 +++ g2) (at (x/2 + 1/2)))
                integrable_on {0..1}"
    by (auto dest: integrable_cmul [where c="1/2"] simp: scaleR_conv_of_real)
  have g2: "⟦0 < z; z < 1; z ∉ s2⟧ ⟹
            vector_derivative (λx. if x*2 ≤ 1 then g1 (2*x) else g2 (2*x - 1)) (at (z/2+1/2)) =
            2 *R vector_derivative g2 (at z)" for z
    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(λx. g2(2*x-1))" and d = "¦z/2¦"]])
    apply (simp_all add: field_simps dist_real_def abs_if split: if_split_asm)
    apply (rule vector_diff_chain_at [of "λx. x*2-1" 2 _ g2, simplified o_def])
    using s2
    apply (auto simp: has_vector_derivative_def has_derivative_def bounded_linear_mult_left
                      vector_derivative_works add_divide_distrib)
    done
  show ?thesis
    using s2
    apply (auto simp: contour_integrable_on)
    apply (rule integrable_spike_finite [of "{0,1} ∪ s2", OF _ _ *])
    apply (auto simp: joinpaths_def scaleR_conv_of_real g2)
    done
qed

lemma contour_integrable_join [simp]:
  shows
    "⟦valid_path g1; valid_path g2⟧
     ⟹ f contour_integrable_on (g1 +++ g2) ⟷ f contour_integrable_on g1 ∧ f contour_integrable_on g2"
using contour_integrable_joinD1 contour_integrable_joinD2 contour_integrable_joinI by blast

lemma contour_integral_join [simp]:
  shows
    "⟦f contour_integrable_on g1; f contour_integrable_on g2; valid_path g1; valid_path g2⟧
        ⟹ contour_integral (g1 +++ g2) f = contour_integral g1 f + contour_integral g2 f"
  by (simp add: has_contour_integral_integral has_contour_integral_join contour_integral_unique)


subsection‹Shifting the starting point of a (closed) path›

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 valid_path_shiftpath [intro]:
  assumes "valid_path g" "pathfinish g = pathstart g" "a ∈ {0..1}"
    shows "valid_path(shiftpath a g)"
  using assms
  apply (auto simp: valid_path_def shiftpath_alt_def)
  apply (rule piecewise_C1_differentiable_cases)
  apply (auto simp: algebra_simps)
  apply (rule piecewise_C1_differentiable_affine [of g 1 a, simplified o_def scaleR_one])
  apply (auto simp: pathfinish_def pathstart_def elim: piecewise_C1_differentiable_on_subset)
  apply (rule piecewise_C1_differentiable_affine [of g 1 "a-1", simplified o_def scaleR_one algebra_simps])
  apply (auto simp: pathfinish_def pathstart_def elim: piecewise_C1_differentiable_on_subset)
  done

lemma has_contour_integral_shiftpath:
  assumes f: "(f has_contour_integral i) g" "valid_path g"
      and a: "a ∈ {0..1}"
    shows "(f has_contour_integral i) (shiftpath a g)"
proof -
  obtain s
    where s: "finite s" and g: "∀x∈{0..1} - s. g differentiable at x"
    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  have *: "((λx. f (g x) * vector_derivative g (at x)) has_integral i) {0..1}"
    using assms by (auto simp: has_contour_integral)
  then have i: "i = integral {a..1} (λx. f (g x) * vector_derivative g (at x)) +
                    integral {0..a} (λx. f (g x) * vector_derivative g (at x))"
    apply (rule has_integral_unique)
    apply (subst add.commute)
    apply (subst Integration.integral_combine)
    using assms * integral_unique by auto
  { fix x
    have "0 ≤ x ⟹ x + a < 1 ⟹ x ∉ (λx. x - a) ` s ⟹
         vector_derivative (shiftpath a g) (at x) = vector_derivative g (at (x + a))"
      unfolding shiftpath_def
      apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(λx. g(a+x))" and d = "dist(1-a) x"]])
        apply (auto simp: field_simps dist_real_def abs_if split: if_split_asm)
      apply (rule vector_diff_chain_at [of "λx. x+a" 1 _ g, simplified o_def scaleR_one])
       apply (intro derivative_eq_intros | simp)+
      using g
       apply (drule_tac x="x+a" in bspec)
      using a apply (auto simp: has_vector_derivative_def vector_derivative_works image_def add.commute)
      done
  } note vd1 = this
  { fix x
    have "1 < x + a ⟹ x ≤ 1 ⟹ x ∉ (λx. x - a + 1) ` s ⟹
          vector_derivative (shiftpath a g) (at x) = vector_derivative g (at (x + a - 1))"
      unfolding shiftpath_def
      apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(λx. g(a+x-1))" and d = "dist (1-a) x"]])
        apply (auto simp: field_simps dist_real_def abs_if split: if_split_asm)
      apply (rule vector_diff_chain_at [of "λx. x+a-1" 1 _ g, simplified o_def scaleR_one])
       apply (intro derivative_eq_intros | simp)+
      using g
      apply (drule_tac x="x+a-1" in bspec)
      using a apply (auto simp: has_vector_derivative_def vector_derivative_works image_def add.commute)
      done
  } note vd2 = this
  have va1: "(λx. f (g x) * vector_derivative g (at x)) integrable_on ({a..1})"
    using * a   by (fastforce intro: integrable_subinterval_real)
  have v0a: "(λx. f (g x) * vector_derivative g (at x)) integrable_on ({0..a})"
    apply (rule integrable_subinterval_real)
    using * a by auto
  have "((λx. f (shiftpath a g x) * vector_derivative (shiftpath a g) (at x))
        has_integral  integral {a..1} (λx. f (g x) * vector_derivative g (at x)))  {0..1 - a}"
    apply (rule has_integral_spike_finite
             [where s = "{1-a} ∪ (λx. x-a) ` s" and f = "λx. f(g(a+x)) * vector_derivative g (at(a+x))"])
      using s apply blast
     using a apply (auto simp: algebra_simps vd1)
     apply (force simp: shiftpath_def add.commute)
    using has_integral_affinity [where m=1 and c=a, simplified, OF integrable_integral [OF va1]]
    apply (simp add: image_affinity_atLeastAtMost_diff [where m=1 and c=a, simplified] add.commute)
    done
  moreover
  have "((λx. f (shiftpath a g x) * vector_derivative (shiftpath a g) (at x))
        has_integral  integral {0..a} (λx. f (g x) * vector_derivative g (at x)))  {1 - a..1}"
    apply (rule has_integral_spike_finite
             [where s = "{1-a} ∪ (λx. x-a+1) ` s" and f = "λx. f(g(a+x-1)) * vector_derivative g (at(a+x-1))"])
      using s apply blast
     using a apply (auto simp: algebra_simps vd2)
     apply (force simp: shiftpath_def add.commute)
    using has_integral_affinity [where m=1 and c="a-1", simplified, OF integrable_integral [OF v0a]]
    apply (simp add: image_affinity_atLeastAtMost [where m=1 and c="1-a", simplified])
    apply (simp add: algebra_simps)
    done
  ultimately show ?thesis
    using a
    by (auto simp: i has_contour_integral intro: has_integral_combine [where c = "1-a"])
qed

lemma has_contour_integral_shiftpath_D:
  assumes "(f has_contour_integral i) (shiftpath a g)"
          "valid_path g" "pathfinish g = pathstart g" "a ∈ {0..1}"
    shows "(f has_contour_integral i) g"
proof -
  obtain s
    where s: "finite s" and g: "∀x∈{0..1} - s. g differentiable at x"
    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  { fix x
    assume x: "0 < x" "x < 1" "x ∉ s"
    then have gx: "g differentiable at x"
      using g by auto
    have "vector_derivative g (at x within {0..1}) =
          vector_derivative (shiftpath (1 - a) (shiftpath a g)) (at x within {0..1})"
      apply (rule vector_derivative_at_within_ivl
                  [OF has_vector_derivative_transform_within_open
                      [where f = "(shiftpath (1 - a) (shiftpath a g))" and s = "{0<..<1}-s"]])
      using s g assms x
      apply (auto simp: finite_imp_closed open_Diff shiftpath_shiftpath
                        vector_derivative_within_interior vector_derivative_works [symmetric])
      apply (rule differentiable_transform_within [OF gx, of "min x (1-x)"])
      apply (auto simp: dist_real_def shiftpath_shiftpath abs_if split: if_split_asm)
      done
  } note vd = this
  have fi: "(f has_contour_integral i) (shiftpath (1 - a) (shiftpath a g))"
    using assms  by (auto intro!: has_contour_integral_shiftpath)
  show ?thesis
    apply (simp add: has_contour_integral_def)
    apply (rule has_integral_spike_finite [of "{0,1} ∪ s", OF _ _  fi [unfolded has_contour_integral_def]])
    using s assms vd
    apply (auto simp: Path_Connected.shiftpath_shiftpath)
    done
qed

lemma has_contour_integral_shiftpath_eq:
  assumes "valid_path g" "pathfinish g = pathstart g" "a ∈ {0..1}"
    shows "(f has_contour_integral i) (shiftpath a g) ⟷ (f has_contour_integral i) g"
  using assms has_contour_integral_shiftpath has_contour_integral_shiftpath_D by blast

lemma contour_integrable_on_shiftpath_eq:
  assumes "valid_path g" "pathfinish g = pathstart g" "a ∈ {0..1}"
    shows "f contour_integrable_on (shiftpath a g) ⟷ f contour_integrable_on g"
using assms contour_integrable_on_def has_contour_integral_shiftpath_eq by auto

lemma contour_integral_shiftpath:
  assumes "valid_path g" "pathfinish g = pathstart g" "a ∈ {0..1}"
    shows "contour_integral (shiftpath a g) f = contour_integral g f"
   using assms
   by (simp add: contour_integral_def contour_integrable_on_def has_contour_integral_shiftpath_eq)


subsection‹More about straight-line paths›

lemma has_vector_derivative_linepath_within:
    "(linepath a b has_vector_derivative (b - a)) (at x within s)"
apply (simp add: linepath_def has_vector_derivative_def algebra_simps)
apply (rule derivative_eq_intros | simp)+
done

lemma vector_derivative_linepath_within:
    "x ∈ {0..1} ⟹ vector_derivative (linepath a b) (at x within {0..1}) = b - a"
  apply (rule vector_derivative_within_closed_interval [of 0 "1::real", simplified])
  apply (auto simp: has_vector_derivative_linepath_within)
  done

lemma vector_derivative_linepath_at [simp]: "vector_derivative (linepath a b) (at x) = b - a"
  by (simp add: has_vector_derivative_linepath_within vector_derivative_at)

lemma valid_path_linepath [iff]: "valid_path (linepath a b)"
  apply (simp add: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq continuous_on_linepath)
  apply (rule_tac x="{}" in exI)
  apply (simp add: differentiable_on_def differentiable_def)
  using has_vector_derivative_def has_vector_derivative_linepath_within
  apply (fastforce simp add: continuous_on_eq_continuous_within)
  done

lemma has_contour_integral_linepath:
  shows "(f has_contour_integral i) (linepath a b) ⟷
         ((λx. f(linepath a b x) * (b - a)) has_integral i) {0..1}"
  by (simp add: has_contour_integral vector_derivative_linepath_at)

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: "a ∈ convex hull s"
        and b: "b ∈ convex hull s"
        and x: "0≤x" "x≤1"
       shows "linepath a b x ∈ convex hull s"
  apply (rule closed_segment_subset_convex_hull [OF a b, THEN subsetD])
  using x
  apply (auto simp: linepath_image_01 [symmetric])
  done

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

lemma Im_linepath: "Im(linepath (of_real a) (of_real b) x) = 0"
  by (simp add: linepath_def)

lemma has_contour_integral_trivial [iff]: "(f has_contour_integral 0) (linepath a a)"
  by (simp add: has_contour_integral_linepath)

lemma contour_integral_trivial [simp]: "contour_integral (linepath a a) f = 0"
  using has_contour_integral_trivial contour_integral_unique by blast


subsection‹Relation to subpath construction›

lemma valid_path_subpath:
  fixes g :: "real ⇒ 'a :: real_normed_vector"
  assumes "valid_path g" "u ∈ {0..1}" "v ∈ {0..1}"
    shows "valid_path(subpath u v g)"
proof (cases "v=u")
  case True
  then show ?thesis
    unfolding valid_path_def subpath_def
    by (force intro: C1_differentiable_on_const C1_differentiable_imp_piecewise)
next
  case False
  have "(g o (λx. ((v-u) * x + u))) piecewise_C1_differentiable_on {0..1}"
    apply (rule piecewise_C1_differentiable_compose)
    apply (simp add: C1_differentiable_imp_piecewise)
     apply (simp add: image_affinity_atLeastAtMost)
    using assms False
    apply (auto simp: algebra_simps valid_path_def piecewise_C1_differentiable_on_subset)
    apply (subst Int_commute)
    apply (auto simp: inj_on_def algebra_simps crossproduct_eq finite_vimage_IntI)
    done
  then show ?thesis
    by (auto simp: o_def valid_path_def subpath_def)
qed

lemma has_contour_integral_subpath_refl [iff]: "(f has_contour_integral 0) (subpath u u g)"
  by (simp add: has_contour_integral subpath_def)

lemma contour_integrable_subpath_refl [iff]: "f contour_integrable_on (subpath u u g)"
  using has_contour_integral_subpath_refl contour_integrable_on_def by blast

lemma contour_integral_subpath_refl [simp]: "contour_integral (subpath u u g) f = 0"
  by (simp add: has_contour_integral_subpath_refl contour_integral_unique)

lemma has_contour_integral_subpath:
  assumes f: "f contour_integrable_on g" and g: "valid_path g"
      and uv: "u ∈ {0..1}" "v ∈ {0..1}" "u ≤ v"
    shows "(f has_contour_integral  integral {u..v} (λx. f(g x) * vector_derivative g (at x)))
           (subpath u v g)"
proof (cases "v=u")
  case True
  then show ?thesis
    using f   by (simp add: contour_integrable_on_def subpath_def has_contour_integral)
next
  case False
  obtain s where s: "⋀x. x ∈ {0..1} - s ⟹ g differentiable at x" and fs: "finite s"
    using g unfolding piecewise_C1_differentiable_on_def C1_differentiable_on_eq valid_path_def by blast
  have *: "((λx. f (g ((v - u) * x + u)) * vector_derivative g (at ((v - u) * x + u)))
            has_integral (1 / (v - u)) * integral {u..v} (λt. f (g t) * vector_derivative g (at t)))
           {0..1}"
    using f uv
    apply (simp add: contour_integrable_on subpath_def has_contour_integral)
    apply (drule integrable_on_subcbox [where a=u and b=v, simplified])
    apply (simp_all add: has_integral_integral)
    apply (drule has_integral_affinity [where m="v-u" and c=u, simplified])
    apply (simp_all add: False image_affinity_atLeastAtMost_div_diff scaleR_conv_of_real)
    apply (simp add: divide_simps False)
    done
  { fix x
    have "x ∈ {0..1} ⟹
           x ∉ (λt. (v-u) *R t + u) -` s ⟹
           vector_derivative (λx. g ((v-u) * x + u)) (at x) = (v-u) *R vector_derivative g (at ((v-u) * x + u))"
      apply (rule vector_derivative_at [OF vector_diff_chain_at [simplified o_def]])
      apply (intro derivative_eq_intros | simp)+
      apply (cut_tac s [of "(v - u) * x + u"])
      using uv mult_left_le [of x "v-u"]
      apply (auto simp:  vector_derivative_works)
      done
  } note vd = this
  show ?thesis
    apply (cut_tac has_integral_cmul [OF *, where c = "v-u"])
    using fs assms
    apply (simp add: False subpath_def has_contour_integral)
    apply (rule_tac s = "(λt. ((v-u) *R t + u)) -` s" in has_integral_spike_finite)
    apply (auto simp: inj_on_def False finite_vimageI vd scaleR_conv_of_real)
    done
qed

lemma contour_integrable_subpath:
  assumes "f contour_integrable_on g" "valid_path g" "u ∈ {0..1}" "v ∈ {0..1}"
    shows "f contour_integrable_on (subpath u v g)"
  apply (cases u v rule: linorder_class.le_cases)
   apply (metis contour_integrable_on_def has_contour_integral_subpath [OF assms])
  apply (subst reversepath_subpath [symmetric])
  apply (rule contour_integrable_reversepath)
   using assms apply (blast intro: valid_path_subpath)
  apply (simp add: contour_integrable_on_def)
  using assms apply (blast intro: has_contour_integral_subpath)
  done

lemma has_integral_integrable_integral: "(f has_integral i) s ⟷ f integrable_on s ∧ integral s f = i"
  by blast

lemma has_integral_contour_integral_subpath:
  assumes "f contour_integrable_on g" "valid_path g" "u ∈ {0..1}" "v ∈ {0..1}" "u ≤ v"
    shows "(((λx. f(g x) * vector_derivative g (at x)))
            has_integral  contour_integral (subpath u v g) f) {u..v}"
  using assms
  apply (auto simp: has_integral_integrable_integral)
  apply (rule integrable_on_subcbox [where a=u and b=v and s = "{0..1}", simplified])
  apply (auto simp: contour_integral_unique [OF has_contour_integral_subpath] contour_integrable_on)
  done

lemma contour_integral_subcontour_integral:
  assumes "f contour_integrable_on g" "valid_path g" "u ∈ {0..1}" "v ∈ {0..1}" "u ≤ v"
    shows "contour_integral (subpath u v g) f =
           integral {u..v} (λx. f(g x) * vector_derivative g (at x))"
  using assms has_contour_integral_subpath contour_integral_unique by blast

lemma contour_integral_subpath_combine_less:
  assumes "f contour_integrable_on g" "valid_path g" "u ∈ {0..1}" "v ∈ {0..1}" "w ∈ {0..1}"
          "u<v" "v<w"
    shows "contour_integral (subpath u v g) f + contour_integral (subpath v w g) f =
           contour_integral (subpath u w g) f"
  using assms apply (auto simp: contour_integral_subcontour_integral)
  apply (rule integral_combine, auto)
  apply (rule integrable_on_subcbox [where a=u and b=w and s = "{0..1}", simplified])
  apply (auto simp: contour_integrable_on)
  done

lemma contour_integral_subpath_combine:
  assumes "f contour_integrable_on g" "valid_path g" "u ∈ {0..1}" "v ∈ {0..1}" "w ∈ {0..1}"
    shows "contour_integral (subpath u v g) f + contour_integral (subpath v w g) f =
           contour_integral (subpath u w g) f"
proof (cases "u≠v ∧ v≠w ∧ u≠w")
  case True
    have *: "subpath v u g = reversepath(subpath u v g) ∧
             subpath w u g = reversepath(subpath u w g) ∧
             subpath w v g = reversepath(subpath v w g)"
      by (auto simp: reversepath_subpath)
    have "u < v ∧ v < w ∨
          u < w ∧ w < v ∨
          v < u ∧ u < w ∨
          v < w ∧ w < u ∨
          w < u ∧ u < v ∨
          w < v ∧ v < u"
      using True assms by linarith
    with assms show ?thesis
      using contour_integral_subpath_combine_less [of f g u v w]
            contour_integral_subpath_combine_less [of f g u w v]
            contour_integral_subpath_combine_less [of f g v u w]
            contour_integral_subpath_combine_less [of f g v w u]
            contour_integral_subpath_combine_less [of f g w u v]
            contour_integral_subpath_combine_less [of f g w v u]
      apply simp
      apply (elim disjE)
      apply (auto simp: * contour_integral_reversepath contour_integrable_subpath
                   valid_path_reversepath valid_path_subpath algebra_simps)
      done
next
  case False
  then show ?thesis
    apply (auto simp: contour_integral_subpath_refl)
    using assms
    by (metis eq_neg_iff_add_eq_0 contour_integrable_subpath contour_integral_reversepath reversepath_subpath valid_path_subpath)
qed

lemma contour_integral_integral:
     "contour_integral g f = integral {0..1} (λx. f (g x) * vector_derivative g (at x))"
  by (simp add: contour_integral_def integral_def has_contour_integral contour_integrable_on)


text‹Cauchy's theorem where there's a primitive›

lemma contour_integral_primitive_lemma:
  fixes f :: "complex ⇒ complex" and g :: "real ⇒ complex"
  assumes "a ≤ b"
      and "⋀x. x ∈ s ⟹ (f has_field_derivative f' x) (at x within s)"
      and "g piecewise_differentiable_on {a..b}"  "⋀x. x ∈ {a..b} ⟹ g x ∈ s"
    shows "((λx. f'(g x) * vector_derivative g (at x within {a..b}))
             has_integral (f(g b) - f(g a))) {a..b}"
proof -
  obtain k where k: "finite k" "∀x∈{a..b} - k. g differentiable (at x within {a..b})" and cg: "continuous_on {a..b} g"
    using assms by (auto simp: piecewise_differentiable_on_def)
  have cfg: "continuous_on {a..b} (λx. f (g x))"
    apply (rule continuous_on_compose [OF cg, unfolded o_def])
    using assms
    apply (metis field_differentiable_def field_differentiable_imp_continuous_at continuous_on_eq_continuous_within continuous_on_subset image_subset_iff)
    done
  { fix x::real
    assume a: "a < x" and b: "x < b" and xk: "x ∉ k"
    then have "g differentiable at x within {a..b}"
      using k by (simp add: differentiable_at_withinI)
    then have "(g has_vector_derivative vector_derivative g (at x within {a..b})) (at x within {a..b})"
      by (simp add: vector_derivative_works has_field_derivative_def scaleR_conv_of_real)
    then have gdiff: "(g has_derivative (λu. u * vector_derivative g (at x within {a..b}))) (at x within {a..b})"
      by (simp add: has_vector_derivative_def scaleR_conv_of_real)
    have "(f has_field_derivative (f' (g x))) (at (g x) within g ` {a..b})"
      using assms by (metis a atLeastAtMost_iff b DERIV_subset image_subset_iff less_eq_real_def)
    then have fdiff: "(f has_derivative op * (f' (g x))) (at (g x) within g ` {a..b})"
      by (simp add: has_field_derivative_def)
    have "((λx. f (g x)) has_vector_derivative f' (g x) * vector_derivative g (at x within {a..b})) (at x within {a..b})"
      using diff_chain_within [OF gdiff fdiff]
      by (simp add: has_vector_derivative_def scaleR_conv_of_real o_def mult_ac)
  } note * = this
  show ?thesis
    apply (rule fundamental_theorem_of_calculus_interior_strong)
    using k assms cfg *
    apply (auto simp: at_within_closed_interval)
    done
qed

lemma contour_integral_primitive:
  assumes "⋀x. x ∈ s ⟹ (f has_field_derivative f' x) (at x within s)"
      and "valid_path g" "path_image g ⊆ s"
    shows "(f' has_contour_integral (f(pathfinish g) - f(pathstart g))) g"
  using assms
  apply (simp add: valid_path_def path_image_def pathfinish_def pathstart_def has_contour_integral_def)
  apply (auto intro!: piecewise_C1_imp_differentiable contour_integral_primitive_lemma [of 0 1 s])
  done

corollary Cauchy_theorem_primitive:
  assumes "⋀x. x ∈ s ⟹ (f has_field_derivative f' x) (at x within s)"
      and "valid_path g"  "path_image g ⊆ s" "pathfinish g = pathstart g"
    shows "(f' has_contour_integral 0) g"
  using assms
  by (metis diff_self contour_integral_primitive)


text‹Existence of path integral for continuous function›
lemma contour_integrable_continuous_linepath:
  assumes "continuous_on (closed_segment a b) f"
  shows "f contour_integrable_on (linepath a b)"
proof -
  have "continuous_on {0..1} ((λx. f x * (b - a)) o linepath a b)"
    apply (rule continuous_on_compose [OF continuous_on_linepath], simp add: linepath_image_01)
    apply (rule continuous_intros | simp add: assms)+
    done
  then show ?thesis
    apply (simp add: contour_integrable_on_def has_contour_integral_def integrable_on_def [symmetric])
    apply (rule integrable_continuous [of 0 "1::real", simplified])
    apply (rule continuous_on_eq [where f = "λx. f(linepath a b x)*(b - a)"])
    apply (auto simp: vector_derivative_linepath_within)
    done
qed

lemma has_field_der_id: "((λx. x2 / 2) has_field_derivative x) (at x)"
  by (rule has_derivative_imp_has_field_derivative)
     (rule derivative_intros | simp)+

lemma contour_integral_id [simp]: "contour_integral (linepath a b) (λy. y) = (b^2 - a^2)/2"
  apply (rule contour_integral_unique)
  using contour_integral_primitive [of UNIV "λx. x^2/2" "λx. x" "linepath a b"]
  apply (auto simp: field_simps has_field_der_id)
  done

lemma contour_integrable_on_const [iff]: "(λx. c) contour_integrable_on (linepath a b)"
  by (simp add: continuous_on_const contour_integrable_continuous_linepath)

lemma contour_integrable_on_id [iff]: "(λx. x) contour_integrable_on (linepath a b)"
  by (simp add: continuous_on_id contour_integrable_continuous_linepath)


subsection‹Arithmetical combining theorems›

lemma has_contour_integral_neg:
    "(f has_contour_integral i) g ⟹ ((λx. -(f x)) has_contour_integral (-i)) g"
  by (simp add: has_integral_neg has_contour_integral_def)

lemma has_contour_integral_add:
    "⟦(f1 has_contour_integral i1) g; (f2 has_contour_integral i2) g⟧
     ⟹ ((λx. f1 x + f2 x) has_contour_integral (i1 + i2)) g"
  by (simp add: has_integral_add has_contour_integral_def algebra_simps)

lemma has_contour_integral_diff:
  "⟦(f1 has_contour_integral i1) g; (f2 has_contour_integral i2) g⟧
         ⟹ ((λx. f1 x - f2 x) has_contour_integral (i1 - i2)) g"
  by (simp add: has_integral_sub has_contour_integral_def algebra_simps)

lemma has_contour_integral_lmul:
  "(f has_contour_integral i) g ⟹ ((λx. c * (f x)) has_contour_integral (c*i)) g"
apply (simp add: has_contour_integral_def)
apply (drule has_integral_mult_right)
apply (simp add: algebra_simps)
done

lemma has_contour_integral_rmul:
  "(f has_contour_integral i) g ⟹ ((λx. (f x) * c) has_contour_integral (i*c)) g"
apply (drule has_contour_integral_lmul)
apply (simp add: mult.commute)
done

lemma has_contour_integral_div:
  "(f has_contour_integral i) g ⟹ ((λx. f x/c) has_contour_integral (i/c)) g"
  by (simp add: field_class.field_divide_inverse) (metis has_contour_integral_rmul)

lemma has_contour_integral_eq:
    "⟦(f has_contour_integral y) p; ⋀x. x ∈ path_image p ⟹ f x = g x⟧ ⟹ (g has_contour_integral y) p"
apply (simp add: path_image_def has_contour_integral_def)
by (metis (no_types, lifting) image_eqI has_integral_eq)

lemma has_contour_integral_bound_linepath:
  assumes "(f has_contour_integral i) (linepath a b)"
          "0 ≤ B" "⋀x. x ∈ closed_segment a b ⟹ norm(f x) ≤ B"
    shows "norm i ≤ B * norm(b - a)"
proof -
  { fix x::real
    assume x: "0 ≤ x" "x ≤ 1"
  have "norm (f (linepath a b x)) *
        norm (vector_derivative (linepath a b) (at x within {0..1})) ≤ B * norm (b - a)"
    by (auto intro: mult_mono simp: assms linepath_in_path of_real_linepath vector_derivative_linepath_within x)
  } note * = this
  have "norm i ≤ (B * norm (b - a)) * content (cbox 0 (1::real))"
    apply (rule has_integral_bound
       [of _ "λx. f (linepath a b x) * vector_derivative (linepath a b) (at x within {0..1})"])
    using assms * unfolding has_contour_integral_def
    apply (auto simp: norm_mult)
    done
  then show ?thesis
    by (auto simp: content_real)
qed

(*UNUSED
lemma has_contour_integral_bound_linepath_strong:
  fixes a :: real and f :: "complex ⇒ real"
  assumes "(f has_contour_integral i) (linepath a b)"
          "finite k"
          "0 ≤ B" "⋀x::real. x ∈ closed_segment a b - k ⟹ norm(f x) ≤ B"
    shows "norm i ≤ B*norm(b - a)"
*)

lemma has_contour_integral_const_linepath: "((λx. c) has_contour_integral c*(b - a))(linepath a b)"
  unfolding has_contour_integral_linepath
  by (metis content_real diff_0_right has_integral_const_real lambda_one of_real_1 scaleR_conv_of_real zero_le_one)

lemma has_contour_integral_0: "((λx. 0) has_contour_integral 0) g"
  by (simp add: has_contour_integral_def)

lemma has_contour_integral_is_0:
    "(⋀z. z ∈ path_image g ⟹ f z = 0) ⟹ (f has_contour_integral 0) g"
  by (rule has_contour_integral_eq [OF has_contour_integral_0]) auto

lemma has_contour_integral_setsum:
    "⟦finite s; ⋀a. a ∈ s ⟹ (f a has_contour_integral i a) p⟧
     ⟹ ((λx. setsum (λa. f a x) s) has_contour_integral setsum i s) p"
  by (induction s rule: finite_induct) (auto simp: has_contour_integral_0 has_contour_integral_add)


subsection ‹Operations on path integrals›

lemma contour_integral_const_linepath [simp]: "contour_integral (linepath a b) (λx. c) = c*(b - a)"
  by (rule contour_integral_unique [OF has_contour_integral_const_linepath])

lemma contour_integral_neg:
    "f contour_integrable_on g ⟹ contour_integral g (λx. -(f x)) = -(contour_integral g f)"
  by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_neg)

lemma contour_integral_add:
    "f1 contour_integrable_on g ⟹ f2 contour_integrable_on g ⟹ contour_integral g (λx. f1 x + f2 x) =
                contour_integral g f1 + contour_integral g f2"
  by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_add)

lemma contour_integral_diff:
    "f1 contour_integrable_on g ⟹ f2 contour_integrable_on g ⟹ contour_integral g (λx. f1 x - f2 x) =
                contour_integral g f1 - contour_integral g f2"
  by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_diff)

lemma contour_integral_lmul:
  shows "f contour_integrable_on g
           ⟹ contour_integral g (λx. c * f x) = c*contour_integral g f"
  by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_lmul)

lemma contour_integral_rmul:
  shows "f contour_integrable_on g
        ⟹ contour_integral g (λx. f x * c) = contour_integral g f * c"
  by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_rmul)

lemma contour_integral_div:
  shows "f contour_integrable_on g
        ⟹ contour_integral g (λx. f x / c) = contour_integral g f / c"
  by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_div)

lemma contour_integral_eq:
    "(⋀x. x ∈ path_image p ⟹ f x = g x) ⟹ contour_integral p f = contour_integral p g"
  apply (simp add: contour_integral_def)
  using has_contour_integral_eq
  by (metis contour_integral_unique has_contour_integral_integrable has_contour_integral_integral)

lemma contour_integral_eq_0:
    "(⋀z. z ∈ path_image g ⟹ f z = 0) ⟹ contour_integral g f = 0"
  by (simp add: has_contour_integral_is_0 contour_integral_unique)

lemma contour_integral_bound_linepath:
  shows
    "⟦f contour_integrable_on (linepath a b);
      0 ≤ B; ⋀x. x ∈ closed_segment a b ⟹ norm(f x) ≤ B⟧
     ⟹ norm(contour_integral (linepath a b) f) ≤ B*norm(b - a)"
  apply (rule has_contour_integral_bound_linepath [of f])
  apply (auto simp: has_contour_integral_integral)
  done

lemma contour_integral_0 [simp]: "contour_integral g (λx. 0) = 0"
  by (simp add: contour_integral_unique has_contour_integral_0)

lemma contour_integral_setsum:
    "⟦finite s; ⋀a. a ∈ s ⟹ (f a) contour_integrable_on p⟧
     ⟹ contour_integral p (λx. setsum (λa. f a x) s) = setsum (λa. contour_integral p (f a)) s"
  by (auto simp: contour_integral_unique has_contour_integral_setsum has_contour_integral_integral)

lemma contour_integrable_eq:
    "⟦f contour_integrable_on p; ⋀x. x ∈ path_image p ⟹ f x = g x⟧ ⟹ g contour_integrable_on p"
  unfolding contour_integrable_on_def
  by (metis has_contour_integral_eq)


subsection ‹Arithmetic theorems for path integrability›

lemma contour_integrable_neg:
    "f contour_integrable_on g ⟹ (λx. -(f x)) contour_integrable_on g"
  using has_contour_integral_neg contour_integrable_on_def by blast

lemma contour_integrable_add:
    "⟦f1 contour_integrable_on g; f2 contour_integrable_on g⟧ ⟹ (λx. f1 x + f2 x) contour_integrable_on g"
  using has_contour_integral_add contour_integrable_on_def
  by fastforce

lemma contour_integrable_diff:
    "⟦f1 contour_integrable_on g; f2 contour_integrable_on g⟧ ⟹ (λx. f1 x - f2 x) contour_integrable_on g"
  using has_contour_integral_diff contour_integrable_on_def
  by fastforce

lemma contour_integrable_lmul:
    "f contour_integrable_on g ⟹ (λx. c * f x) contour_integrable_on g"
  using has_contour_integral_lmul contour_integrable_on_def
  by fastforce

lemma contour_integrable_rmul:
    "f contour_integrable_on g ⟹ (λx. f x * c) contour_integrable_on g"
  using has_contour_integral_rmul contour_integrable_on_def
  by fastforce

lemma contour_integrable_div:
    "f contour_integrable_on g ⟹ (λx. f x / c) contour_integrable_on g"
  using has_contour_integral_div contour_integrable_on_def
  by fastforce

lemma contour_integrable_setsum:
    "⟦finite s; ⋀a. a ∈ s ⟹ (f a) contour_integrable_on p⟧
     ⟹ (λx. setsum (λa. f a x) s) contour_integrable_on p"
   unfolding contour_integrable_on_def
   by (metis has_contour_integral_setsum)


subsection‹Reversing a path integral›

lemma has_contour_integral_reverse_linepath:
    "(f has_contour_integral i) (linepath a b)
     ⟹ (f has_contour_integral (-i)) (linepath b a)"
  using has_contour_integral_reversepath valid_path_linepath by fastforce

lemma contour_integral_reverse_linepath:
    "continuous_on (closed_segment a b) f
     ⟹ contour_integral (linepath a b) f = - (contour_integral(linepath b a) f)"
apply (rule contour_integral_unique)
apply (rule has_contour_integral_reverse_linepath)
by (simp add: closed_segment_commute contour_integrable_continuous_linepath has_contour_integral_integral)


(* Splitting a path integral in a flat way.*)

lemma has_contour_integral_split:
  assumes f: "(f has_contour_integral i) (linepath a c)" "(f has_contour_integral j) (linepath c b)"
      and k: "0 ≤ k" "k ≤ 1"
      and c: "c - a = k *R (b - a)"
    shows "(f has_contour_integral (i + j)) (linepath a b)"
proof (cases "k = 0 ∨ k = 1")
  case True
  then show ?thesis
    using assms
    apply auto
    apply (metis add.left_neutral has_contour_integral_trivial has_contour_integral_unique)
    apply (metis add.right_neutral has_contour_integral_trivial has_contour_integral_unique)
    done
next
  case False
  then have k: "0 < k" "k < 1" "complex_of_real k ≠ 1"
    using assms apply auto
    using of_real_eq_iff by fastforce
  have c': "c = k *R (b - a) + a"
    by (metis diff_add_cancel c)
  have bc: "(b - c) = (1 - k) *R (b - a)"
    by (simp add: algebra_simps c')
  { assume *: "((λx. f ((1 - x) *R a + x *R c) * (c - a)) has_integral i) {0..1}"
    have **: "⋀x. ((k - x) / k) *R a + (x / k) *R c = (1 - x) *R a + x *R b"
      using False
      apply (simp add: c' algebra_simps)
      apply (simp add: real_vector.scale_left_distrib [symmetric] divide_simps)
      done
    have "((λx. f ((1 - x) *R a + x *R b) * (b - a)) has_integral i) {0..k}"
      using * k
      apply -
      apply (drule has_integral_affinity [of _ _ 0 "1::real" "inverse k" "0", simplified])
      apply (simp_all add: divide_simps mult.commute [of _ "k"] image_affinity_atLeastAtMost ** c)
      apply (drule Integration.has_integral_cmul [where c = "inverse k"])
      apply (simp add: Integration.has_integral_cmul)
      done
  } note fi = this
  { assume *: "((λx. f ((1 - x) *R c + x *R b) * (b - c)) has_integral j) {0..1}"
    have **: "⋀x. (((1 - x) / (1 - k)) *R c + ((x - k) / (1 - k)) *R b) = ((1 - x) *R a + x *R b)"
      using k
      apply (simp add: c' field_simps)
      apply (simp add: scaleR_conv_of_real divide_simps)
      apply (simp add: field_simps)
      done
    have "((λx. f ((1 - x) *R a + x *R b) * (b - a)) has_integral j) {k..1}"
      using * k
      apply -
      apply (drule has_integral_affinity [of _ _ 0 "1::real" "inverse(1 - k)" "-(k/(1 - k))", simplified])
      apply (simp_all add: divide_simps mult.commute [of _ "1-k"] image_affinity_atLeastAtMost ** bc)
      apply (drule Integration.has_integral_cmul [where k = "(1 - k) *R j" and c = "inverse (1 - k)"])
      apply (simp add: Integration.has_integral_cmul)
      done
  } note fj = this
  show ?thesis
    using f k
    apply (simp add: has_contour_integral_linepath)
    apply (simp add: linepath_def)
    apply (rule has_integral_combine [OF _ _ fi fj], simp_all)
    done
qed

lemma continuous_on_closed_segment_transform:
  assumes f: "continuous_on (closed_segment a b) f"
      and k: "0 ≤ k" "k ≤ 1"
      and c: "c - a = k *R (b - a)"
    shows "continuous_on (closed_segment a c) f"
proof -
  have c': "c = (1 - k) *R a + k *R b"
    using c by (simp add: algebra_simps)
  show "continuous_on (closed_segment a c) f"
    apply (rule continuous_on_subset [OF f])
    apply (simp add: segment_convex_hull)
    apply (rule convex_hull_subset)
    using assms
    apply (auto simp: hull_inc c' Convex.convexD_alt)
    done
qed

lemma contour_integral_split:
  assumes f: "continuous_on (closed_segment a b) f"
      and k: "0 ≤ k" "k ≤ 1"
      and c: "c - a = k *R (b - a)"
    shows "contour_integral(linepath a b) f = contour_integral(linepath a c) f + contour_integral(linepath c b) f"
proof -
  have c': "c = (1 - k) *R a + k *R b"
    using c by (simp add: algebra_simps)
  have *: "continuous_on (closed_segment a c) f" "continuous_on (closed_segment c b) f"
    apply (rule_tac [!] continuous_on_subset [OF f])
    apply (simp_all add: segment_convex_hull)
    apply (rule_tac [!] convex_hull_subset)
    using assms
    apply (auto simp: hull_inc c' Convex.convexD_alt)
    done
  show ?thesis
    apply (rule contour_integral_unique)
    apply (rule has_contour_integral_split [OF has_contour_integral_integral has_contour_integral_integral k c])
    apply (rule contour_integrable_continuous_linepath *)+
    done
qed

lemma contour_integral_split_linepath:
  assumes f: "continuous_on (closed_segment a b) f"
      and c: "c ∈ closed_segment a b"
    shows "contour_integral(linepath a b) f = contour_integral(linepath a c) f + contour_integral(linepath c b) f"
  using c
  by (auto simp: closed_segment_def algebra_simps intro!: contour_integral_split [OF f])

(* The special case of midpoints used in the main quadrisection.*)

lemma has_contour_integral_midpoint:
  assumes "(f has_contour_integral i) (linepath a (midpoint a b))"
          "(f has_contour_integral j) (linepath (midpoint a b) b)"
    shows "(f has_contour_integral (i + j)) (linepath a b)"
  apply (rule has_contour_integral_split [where c = "midpoint a b" and k = "1/2"])
  using assms
  apply (auto simp: midpoint_def algebra_simps scaleR_conv_of_real)
  done

lemma contour_integral_midpoint:
   "continuous_on (closed_segment a b) f
    ⟹ contour_integral (linepath a b) f =
        contour_integral (linepath a (midpoint a b)) f + contour_integral (linepath (midpoint a b) b) f"
  apply (rule contour_integral_split [where c = "midpoint a b" and k = "1/2"])
  using assms
  apply (auto simp: midpoint_def algebra_simps scaleR_conv_of_real)
  done


text‹A couple of special case lemmas that are useful below›

lemma triangle_linear_has_chain_integral:
    "((λx. m*x + d) has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  apply (rule Cauchy_theorem_primitive [of UNIV "λx. m/2 * x^2 + d*x"])
  apply (auto intro!: derivative_eq_intros)
  done

lemma has_chain_integral_chain_integral3:
     "(f has_contour_integral i) (linepath a b +++ linepath b c +++ linepath c d)
      ⟹ contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c d) f = i"
  apply (subst contour_integral_unique [symmetric], assumption)
  apply (drule has_contour_integral_integrable)
  apply (simp add: valid_path_join)
  done

lemma has_chain_integral_chain_integral4:
     "(f has_contour_integral i) (linepath a b +++ linepath b c +++ linepath c d +++ linepath d e)
      ⟹ contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c d) f + contour_integral (linepath d e) f = i"
  apply (subst contour_integral_unique [symmetric], assumption)
  apply (drule has_contour_integral_integrable)
  apply (simp add: valid_path_join)
  done

subsection‹Reversing the order in a double path integral›

text‹The condition is stronger than needed but it's often true in typical situations›

lemma fst_im_cbox [simp]: "cbox c d ≠ {} ⟹ (fst ` cbox (a,c) (b,d)) = cbox a b"
  by (auto simp: cbox_Pair_eq)

lemma snd_im_cbox [simp]: "cbox a b ≠ {} ⟹ (snd ` cbox (a,c) (b,d)) = cbox c d"
  by (auto simp: cbox_Pair_eq)

lemma contour_integral_swap:
  assumes fcon:  "continuous_on (path_image g × path_image h) (λ(y1,y2). f y1 y2)"
      and vp:    "valid_path g" "valid_path h"
      and gvcon: "continuous_on {0..1} (λt. vector_derivative g (at t))"
      and hvcon: "continuous_on {0..1} (λt. vector_derivative h (at t))"
  shows "contour_integral g (λw. contour_integral h (f w)) =
         contour_integral h (λz. contour_integral g (λw. f w z))"
proof -
  have gcon: "continuous_on {0..1} g" and hcon: "continuous_on {0..1} h"
    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
  have fgh1: "⋀x. (λt. f (g x) (h t)) = (λ(y1,y2). f y1 y2) o (λt. (g x, h t))"
    by (rule ext) simp
  have fgh2: "⋀x. (λt. f (g t) (h x)) = (λ(y1,y2). f y1 y2) o (λt. (g t, h x))"
    by (rule ext) simp
  have fcon_im1: "⋀x. 0 ≤ x ⟹ x ≤ 1 ⟹ continuous_on ((λt. (g x, h t)) ` {0..1}) (λ(x, y). f x y)"
    by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
  have fcon_im2: "⋀x. 0 ≤ x ⟹ x ≤ 1 ⟹ continuous_on ((λt. (g t, h x)) ` {0..1}) (λ(x, y). f x y)"
    by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
  have vdg: "⋀y. y ∈ {0..1} ⟹ (λx. f (g x) (h y) * vector_derivative g (at x)) integrable_on {0..1}"
    apply (rule integrable_continuous_real)
    apply (rule continuous_on_mult [OF _ gvcon])
    apply (subst fgh2)
    apply (rule fcon_im2 gcon continuous_intros | simp)+
    done
  have "(λz. vector_derivative g (at (fst z))) = (λx. vector_derivative g (at x)) o fst"
    by auto
  then have gvcon': "continuous_on (cbox (0, 0) (1, 1::real)) (λx. vector_derivative g (at (fst x)))"
    apply (rule ssubst)
    apply (rule continuous_intros | simp add: gvcon)+
    done
  have "(λz. vector_derivative h (at (snd z))) = (λx. vector_derivative h (at x)) o snd"
    by auto
  then have hvcon': "continuous_on (cbox (0, 0) (1::real, 1)) (λx. vector_derivative h (at (snd x)))"
    apply (rule ssubst)
    apply (rule continuous_intros | simp add: hvcon)+
    done
  have "(λx. f (g (fst x)) (h (snd x))) = (λ(y1,y2). f y1 y2) o (λw. ((g o fst) w, (h o snd) w))"
    by auto
  then have fgh: "continuous_on (cbox (0, 0) (1, 1)) (λx. f (g (fst x)) (h (snd x)))"
    apply (rule ssubst)
    apply (rule gcon hcon continuous_intros | simp)+
    apply (auto simp: path_image_def intro: continuous_on_subset [OF fcon])
    done
  have "integral {0..1} (λx. contour_integral h (f (g x)) * vector_derivative g (at x)) =
        integral {0..1} (λx. contour_integral h (λy. f (g x) y * vector_derivative g (at x)))"
    apply (rule integral_cong [OF contour_integral_rmul [symmetric]])
    apply (clarsimp simp: contour_integrable_on)
    apply (rule integrable_continuous_real)
    apply (rule continuous_on_mult [OF _ hvcon])
    apply (subst fgh1)
    apply (rule fcon_im1 hcon continuous_intros | simp)+
    done
  also have "... = integral {0..1}
                     (λy. contour_integral g (λx. f x (h y) * vector_derivative h (at y)))"
    apply (simp only: contour_integral_integral)
    apply (subst integral_swap_continuous [where 'a = real and 'b = real, of 0 0 1 1, simplified])
     apply (rule fgh gvcon' hvcon' continuous_intros | simp add: split_def)+
    unfolding integral_mult_left [symmetric]
    apply (simp only: mult_ac)
    done
  also have "... = contour_integral h (λz. contour_integral g (λw. f w z))"
    apply (simp add: contour_integral_integral)
    apply (rule integral_cong)
    unfolding integral_mult_left [symmetric]
    apply (simp add: algebra_simps)
    done
  finally show ?thesis
    by (simp add: contour_integral_integral)
qed


subsection‹The key quadrisection step›

lemma norm_sum_half:
  assumes "norm(a + b) >= e"
    shows "norm a >= e/2 ∨ norm b >= e/2"
proof -
  have "e ≤ norm (- a - b)"
    by (simp add: add.commute assms norm_minus_commute)
  thus ?thesis
    using norm_triangle_ineq4 order_trans by fastforce
qed

lemma norm_sum_lemma:
  assumes "e ≤ norm (a + b + c + d)"
    shows "e / 4 ≤ norm a ∨ e / 4 ≤ norm b ∨ e / 4 ≤ norm c ∨ e / 4 ≤ norm d"
proof -
  have "e ≤ norm ((a + b) + (c + d))" using assms
    by (simp add: algebra_simps)
  then show ?thesis
    by (auto dest!: norm_sum_half)
qed

lemma Cauchy_theorem_quadrisection:
  assumes f: "continuous_on (convex hull {a,b,c}) f"
      and dist: "dist a b ≤ K" "dist b c ≤ K" "dist c a ≤ K"
      and e: "e * K^2 ≤
              norm (contour_integral(linepath a b) f + contour_integral(linepath b c) f + contour_integral(linepath c a) f)"
  shows "∃a' b' c'.
           a' ∈ convex hull {a,b,c} ∧ b' ∈ convex hull {a,b,c} ∧ c' ∈ convex hull {a,b,c} ∧
           dist a' b' ≤ K/2  ∧  dist b' c' ≤ K/2  ∧  dist c' a' ≤ K/2  ∧
           e * (K/2)^2 ≤ norm(contour_integral(linepath a' b') f + contour_integral(linepath b' c') f + contour_integral(linepath c' a') f)"
proof -
  note divide_le_eq_numeral1 [simp del]
  def a'  "midpoint b c"
  def b'  "midpoint c a"
  def c'  "midpoint a b"
  have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
    using f continuous_on_subset segments_subset_convex_hull by metis+
  have fcont': "continuous_on (closed_segment c' b') f"
               "continuous_on (closed_segment a' c') f"
               "continuous_on (closed_segment b' a') f"
    unfolding a'_def b'_def c'_def
    apply (rule continuous_on_subset [OF f],
           metis midpoints_in_convex_hull convex_hull_subset hull_subset insert_subset segment_convex_hull)+
    done
  let ?pathint = "λx y. contour_integral(linepath x y) f"
  have *: "?pathint a b + ?pathint b c + ?pathint c a =
          (?pathint a c' + ?pathint c' b' + ?pathint b' a) +
          (?pathint a' c' + ?pathint c' b + ?pathint b a') +
          (?pathint a' c + ?pathint c b' + ?pathint b' a') +
          (?pathint a' b' + ?pathint b' c' + ?pathint c' a')"
    apply (simp add: fcont' contour_integral_reverse_linepath)
    apply (simp add: a'_def b'_def c'_def contour_integral_midpoint fabc)
    done
  have [simp]: "⋀x y. cmod (x * 2 - y * 2) = cmod (x - y) * 2"
    by (metis left_diff_distrib mult.commute norm_mult_numeral1)
  have [simp]: "⋀x y. cmod (x - y) = cmod (y - x)"
    by (simp add: norm_minus_commute)
  consider "e * K2 / 4 ≤ cmod (?pathint a c' + ?pathint c' b' + ?pathint b' a)" |
           "e * K2 / 4 ≤ cmod (?pathint a' c' + ?pathint c' b + ?pathint b a')" |
           "e * K2 / 4 ≤ cmod (?pathint a' c + ?pathint c b' + ?pathint b' a')" |
           "e * K2 / 4 ≤ cmod (?pathint a' b' + ?pathint b' c' + ?pathint c' a')"
    using assms
    apply (simp only: *)
    apply (blast intro: that dest!: norm_sum_lemma)
    done
  then show ?thesis
  proof cases
    case 1 then show ?thesis
      apply (rule_tac x=a in exI)
      apply (rule exI [where x=c'])
      apply (rule exI [where x=b'])
      using assms
      apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
      done
  next
    case 2 then show ?thesis
      apply (rule_tac x=a' in exI)
      apply (rule exI [where x=c'])
      apply (rule exI [where x=b])
      using assms
      apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
      done
  next
    case 3 then show ?thesis
      apply (rule_tac x=a' in exI)
      apply (rule exI [where x=c])
      apply (rule exI [where x=b'])
      using assms
      apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
      done
  next
    case 4 then show ?thesis
      apply (rule_tac x=a' in exI)
      apply (rule exI [where x=b'])
      apply (rule exI [where x=c'])
      using assms
      apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
      done
  qed
qed

subsection‹Cauchy's theorem for triangles›

lemma triangle_points_closer:
  fixes a::complex
  shows "⟦x ∈ convex hull {a,b,c};  y ∈ convex hull {a,b,c}⟧
         ⟹ norm(x - y) ≤ norm(a - b) ∨
             norm(x - y) ≤ norm(b - c) ∨
             norm(x - y) ≤ norm(c - a)"
  using simplex_extremal_le [of "{a,b,c}"]
  by (auto simp: norm_minus_commute)

lemma holomorphic_point_small_triangle:
  assumes x: "x ∈ s"
      and f: "continuous_on s f"
      and cd: "f field_differentiable (at x within s)"
      and e: "0 < e"
    shows "∃k>0. ∀a b c. dist a b ≤ k ∧ dist b c ≤ k ∧ dist c a ≤ k ∧
              x ∈ convex hull {a,b,c} ∧ convex hull {a,b,c} ⊆ s
              ⟶ norm(contour_integral(linepath a b) f + contour_integral(linepath b c) f +
                       contour_integral(linepath c a) f)
                  ≤ e*(dist a b + dist b c + dist c a)^2"
           (is "∃k>0. ∀a b c. _ ⟶ ?normle a b c")
proof -
  have le_of_3: "⋀a x y z. ⟦0 ≤ x*y; 0 ≤ x*z; 0 ≤ y*z; a ≤ (e*(x + y + z))*x + (e*(x + y + z))*y + (e*(x + y + z))*z⟧
                     ⟹ a ≤ e*(x + y + z)^2"
    by (simp add: algebra_simps power2_eq_square)
  have disj_le: "⟦x ≤ a ∨ x ≤ b ∨ x ≤ c; 0 ≤ a; 0 ≤ b; 0 ≤ c⟧ ⟹ x ≤ a + b + c"
             for x::real and a b c
    by linarith
  have fabc: "f contour_integrable_on linepath a b" "f contour_integrable_on linepath b c" "f contour_integrable_on linepath c a"
              if "convex hull {a, b, c} ⊆ s" for a b c
    using segments_subset_convex_hull that
    by (metis continuous_on_subset f contour_integrable_continuous_linepath)+
  note path_bound = has_contour_integral_bound_linepath [simplified norm_minus_commute, OF has_contour_integral_integral]
  { fix f' a b c d
    assume d: "0 < d"
       and f': "⋀y. ⟦cmod (y - x) ≤ d; y ∈ s⟧ ⟹ cmod (f y - f x - f' * (y - x)) ≤ e * cmod (y - x)"
       and le: "cmod (a - b) ≤ d" "cmod (b - c) ≤ d" "cmod (c - a) ≤ d"
       and xc: "x ∈ convex hull {a, b, c}"
       and s: "convex hull {a, b, c} ⊆ s"
    have pa: "contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f =
              contour_integral (linepath a b) (λy. f y - f x - f'*(y - x)) +
              contour_integral (linepath b c) (λy. f y - f x - f'*(y - x)) +
              contour_integral (linepath c a) (λy. f y - f x - f'*(y - x))"
      apply (simp add: contour_integral_diff contour_integral_lmul contour_integrable_lmul contour_integrable_diff fabc [OF s])
      apply (simp add: field_simps)
      done
    { fix y
      assume yc: "y ∈ convex hull {a,b,c}"
      have "cmod (f y - f x - f' * (y - x)) ≤ e*norm(y - x)"
        apply (rule f')
        apply (metis triangle_points_closer [OF xc yc] le norm_minus_commute order_trans)
        using s yc by blast
      also have "... ≤ e * (cmod (a - b) + cmod (b - c) + cmod (c - a))"
        by (simp add: yc e xc disj_le [OF triangle_points_closer])
      finally have "cmod (f y - f x - f' * (y - x)) ≤ e * (cmod (a - b) + cmod (b - c) + cmod (c - a))" .
    } note cm_le = this
    have "?normle a b c"
      apply (simp add: dist_norm pa)
      apply (rule le_of_3)
      using f' xc s e
      apply simp_all
      apply (intro norm_triangle_le add_mono path_bound)
      apply (simp_all add: contour_integral_diff contour_integral_lmul contour_integrable_lmul contour_integrable_diff fabc)
      apply (blast intro: cm_le elim: dest: segments_subset_convex_hull [THEN subsetD])+
      done
  } note * = this
  show ?thesis
    using cd e
    apply (simp add: field_differentiable_def has_field_derivative_def has_derivative_within_alt approachable_lt_le2 Ball_def)
    apply (clarify dest!: spec mp)
    using *
    apply (simp add: dist_norm, blast)
    done
qed


(* Hence the most basic theorem for a triangle.*)
locale Chain =
  fixes x0 At Follows
  assumes At0: "At x0 0"
      and AtSuc: "⋀x n. At x n ⟹ ∃x'. At x' (Suc n) ∧ Follows x' x"
begin
  primrec f where
    "f 0 = x0"
  | "f (Suc n) = (SOME x. At x (Suc n) ∧ Follows x (f n))"

  lemma At: "At (f n) n"
  proof (induct n)
    case 0 show ?case
      by (simp add: At0)
  next
    case (Suc n) show ?case
      by (metis (no_types, lifting) AtSuc [OF Suc] f.simps(2) someI_ex)
  qed

  lemma Follows: "Follows (f(Suc n)) (f n)"
    by (metis (no_types, lifting) AtSuc [OF At [of n]] f.simps(2) someI_ex)

  declare f.simps(2) [simp del]
end

lemma Chain3:
  assumes At0: "At x0 y0 z0 0"
      and AtSuc: "⋀x y z n. At x y z n ⟹ ∃x' y' z'. At x' y' z' (Suc n) ∧ Follows x' y' z' x y z"
  obtains f g h where
    "f 0 = x0" "g 0 = y0" "h 0 = z0"
                      "⋀n. At (f n) (g n) (h n) n"
                       "⋀n. Follows (f(Suc n)) (g(Suc n)) (h(Suc n)) (f n) (g n) (h n)"
proof -
  interpret three: Chain "(x0,y0,z0)" "λ(x,y,z). At x y z" "λ(x',y',z'). λ(x,y,z). Follows x' y' z' x y z"
    apply unfold_locales
    using At0 AtSuc by auto
  show ?thesis
  apply (rule that [of "λn. fst (three.f n)"  "λn. fst (snd (three.f n))" "λn. snd (snd (three.f n))"])
  apply simp_all
  using three.At three.Follows
  apply (simp_all add: split_beta')
  done
qed

lemma Cauchy_theorem_triangle:
  assumes "f holomorphic_on (convex hull {a,b,c})"
    shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
proof -
  have contf: "continuous_on (convex hull {a,b,c}) f"
    by (metis assms holomorphic_on_imp_continuous_on)
  let ?pathint = "λx y. contour_integral(linepath x y) f"
  { fix y::complex
    assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
       and ynz: "y ≠ 0"
    def K  "1 + max (dist a b) (max (dist b c) (dist c a))"
    def e  "norm y / K^2"
    have K1: "K ≥ 1"  by (simp add: K_def max.coboundedI1)
    then have K: "K > 0" by linarith
    have [iff]: "dist a b ≤ K" "dist b c ≤ K" "dist c a ≤ K"
      by (simp_all add: K_def)
    have e: "e > 0"
      unfolding e_def using ynz K1 by simp
    def At  "λx y z n. convex hull {x,y,z} ⊆ convex hull {a,b,c} ∧
                         dist x y ≤ K/2^n ∧ dist y z ≤ K/2^n ∧ dist z x ≤ K/2^n ∧
                         norm(?pathint x y + ?pathint y z + ?pathint z x) ≥ e*(K/2^n)^2"
    have At0: "At a b c 0"
      using fy
      by (simp add: At_def e_def has_chain_integral_chain_integral3)
    { fix x y z n
      assume At: "At x y z n"
      then have contf': "continuous_on (convex hull {x,y,z}) f"
        using contf At_def continuous_on_subset by blast
      have "∃x' y' z'. At x' y' z' (Suc n) ∧ convex hull {x',y',z'} ⊆ convex hull {x,y,z}"
        using At
        apply (simp add: At_def)
        using  Cauchy_theorem_quadrisection [OF contf', of "K/2^n" e]
        apply clarsimp
        apply (rule_tac x="a'" in exI)
        apply (rule_tac x="b'" in exI)
        apply (rule_tac x="c'" in exI)
        apply (simp add: algebra_simps)
        apply (meson convex_hull_subset empty_subsetI insert_subset subsetCE)
        done
    } note AtSuc = this
    obtain fa fb fc
      where f0 [simp]: "fa 0 = a" "fb 0 = b" "fc 0 = c"
        and cosb: "⋀n. convex hull {fa n, fb n, fc n} ⊆ convex hull {a,b,c}"
        and dist: "⋀n. dist (fa n) (fb n) ≤ K/2^n"
                  "⋀n. dist (fb n) (fc n) ≤ K/2^n"
                  "⋀n. dist (fc n) (fa n) ≤ K/2^n"
        and no: "⋀n. norm(?pathint (fa n) (fb n) +
                           ?pathint (fb n) (fc n) +
                           ?pathint (fc n) (fa n)) ≥ e * (K/2^n)^2"
        and conv_le: "⋀n. convex hull {fa(Suc n), fb(Suc n), fc(Suc n)} ⊆ convex hull {fa n, fb n, fc n}"
      apply (rule Chain3 [of At, OF At0 AtSuc])
      apply (auto simp: At_def)
      done
    have "∃x. ∀n. x ∈ convex hull {fa n, fb n, fc n}"
      apply (rule bounded_closed_nest)
      apply (simp_all add: compact_imp_closed finite_imp_compact_convex_hull finite_imp_bounded_convex_hull)
      apply (rule allI)
      apply (rule transitive_stepwise_le)
      apply (auto simp: conv_le)
      done
    then obtain x where x: "⋀n. x ∈ convex hull {fa n, fb n, fc n}" by auto
    then have xin: "x ∈ convex hull {a,b,c}"
      using assms f0 by blast
    then have fx: "f field_differentiable at x within (convex hull {a,b,c})"
      using assms holomorphic_on_def by blast
    { fix k n
      assume k: "0 < k"
         and le:
            "⋀x' y' z'.
               ⟦dist x' y' ≤ k; dist y' z' ≤ k; dist z' x' ≤ k;
                x ∈ convex hull {x',y',z'};
                convex hull {x',y',z'} ⊆ convex hull {a,b,c}⟧
               ⟹
               cmod (?pathint x' y' + ?pathint y' z' + ?pathint z' x') * 10
                     ≤ e * (dist x' y' + dist y' z' + dist z' x')2"
         and Kk: "K / k < 2 ^ n"
      have "K / 2 ^ n < k" using Kk k
        by (auto simp: field_simps)
      then have DD: "dist (fa n) (fb n) ≤ k" "dist (fb n) (fc n) ≤ k" "dist (fc n) (fa n) ≤ k"
        using dist [of n]  k
        by linarith+
      have dle: "(dist (fa n) (fb n) + dist (fb n) (fc n) + dist (fc n) (fa n))2
               ≤ (3 * K / 2 ^ n)2"
        using dist [of n] e K
        by (simp add: abs_le_square_iff [symmetric])
      have less10: "⋀x y::real. 0 < x ⟹ y ≤ 9*x ⟹ y < x*10"
        by linarith
      have "e * (dist (fa n) (fb n) + dist (fb n) (fc n) + dist (fc n) (fa n))2 ≤ e * (3 * K / 2 ^ n)2"
        using ynz dle e mult_le_cancel_left_pos by blast
      also have "... <
          cmod (?pathint (fa n) (fb n) + ?pathint (fb n) (fc n) + ?pathint (fc n) (fa n)) * 10"
        using no [of n] e K
        apply (simp add: e_def field_simps)
        apply (simp only: zero_less_norm_iff [symmetric])
        done
      finally have False
        using le [OF DD x cosb] by auto
    } then
    have ?thesis
      using holomorphic_point_small_triangle [OF xin contf fx, of "e/10"] e
      apply clarsimp
      apply (rule_tac y1="K/k" in exE [OF real_arch_pow[of 2]])
      apply force+
      done
  }
  moreover have "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
    by simp (meson contf continuous_on_subset contour_integrable_continuous_linepath segments_subset_convex_hull(1)
                   segments_subset_convex_hull(3) segments_subset_convex_hull(5))
  ultimately show ?thesis
    using has_contour_integral_integral by fastforce
qed


subsection‹Version needing function holomorphic in interior only›

lemma Cauchy_theorem_flat_lemma:
  assumes f: "continuous_on (convex hull {a,b,c}) f"
      and c: "c - a = k *R (b - a)"
      and k: "0 ≤ k"
    shows "contour_integral (linepath a b) f + contour_integral (linepath b c) f +
          contour_integral (linepath c a) f = 0"
proof -
  have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
    using f continuous_on_subset segments_subset_convex_hull by metis+
  show ?thesis
  proof (cases "k ≤ 1")
    case True show ?thesis
      by (simp add: contour_integral_split [OF fabc(1) k True c] contour_integral_reverse_linepath fabc)
  next
    case False then show ?thesis
      using fabc c
      apply (subst contour_integral_split [of a c f "1/k" b, symmetric])
      apply (metis closed_segment_commute fabc(3))
      apply (auto simp: k contour_integral_reverse_linepath)
      done
  qed
qed

lemma Cauchy_theorem_flat:
  assumes f: "continuous_on (convex hull {a,b,c}) f"
      and c: "c - a = k *R (b - a)"
    shows "contour_integral (linepath a b) f +
           contour_integral (linepath b c) f +
           contour_integral (linepath c a) f = 0"
proof (cases "0 ≤ k")
  case True with assms show ?thesis
    by (blast intro: Cauchy_theorem_flat_lemma)
next
  case False
  have "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
    using f continuous_on_subset segments_subset_convex_hull by metis+
  moreover have "contour_integral (linepath b a) f + contour_integral (linepath a c) f +
        contour_integral (linepath c b) f = 0"
    apply (rule Cauchy_theorem_flat_lemma [of b a c f "1-k"])
    using False c
    apply (auto simp: f insert_commute scaleR_conv_of_real algebra_simps)
    done
  ultimately show ?thesis
    apply (auto simp: contour_integral_reverse_linepath)
    using add_eq_0_iff by force
qed


lemma Cauchy_theorem_triangle_interior:
  assumes contf: "continuous_on (convex hull {a,b,c}) f"
      and holf:  "f holomorphic_on interior (convex hull {a,b,c})"
     shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
proof -
  have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
    using contf continuous_on_subset segments_subset_convex_hull by metis+
  have "bounded (f ` (convex hull {a,b,c}))"
    by (simp add: compact_continuous_image compact_convex_hull compact_imp_bounded contf)
  then obtain B where "0 < B" and Bnf: "⋀x. x ∈ convex hull {a,b,c} ⟹ norm (f x) ≤ B"
     by (auto simp: dest!: bounded_pos [THEN iffD1])
  have "bounded (convex hull {a,b,c})"
    by (simp add: bounded_convex_hull)
  then obtain C where C: "0 < C" and Cno: "⋀y. y ∈ convex hull {a,b,c} ⟹ norm y < C"
    using bounded_pos_less by blast
  then have diff_2C: "norm(x - y) ≤ 2*C"
           if x: "x ∈ convex hull {a, b, c}" and y: "y ∈ convex hull {a, b, c}" for x y
  proof -
    have "cmod x ≤ C"
      using x by (meson Cno not_le not_less_iff_gr_or_eq)
    hence "cmod (x - y) ≤ C + C"
      using y by (meson Cno add_mono_thms_linordered_field(4) less_eq_real_def norm_triangle_ineq4 order_trans)
    thus "cmod (x - y) ≤ 2 * C"
      by (metis mult_2)
  qed
  have contf': "continuous_on (convex hull {b,a,c}) f"
    using contf by (simp add: insert_commute)
  { fix y::complex
    assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
       and ynz: "y ≠ 0"
    have pi_eq_y: "contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f = y"
      by (rule has_chain_integral_chain_integral3 [OF fy])
    have ?thesis
    proof (cases "c=a ∨ a=b ∨ b=c")
      case True then show ?thesis
        using Cauchy_theorem_flat [OF contf, of 0]
        using has_chain_integral_chain_integral3 [OF fy] ynz
        by (force simp: fabc contour_integral_reverse_linepath)
    next
      case False
      then have car3: "card {a, b, c} = Suc (DIM(complex))"
        by auto
      { assume "interior(convex hull {a,b,c}) = {}"
        then have "collinear{a,b,c}"
          using interior_convex_hull_eq_empty [OF car3]
          by (simp add: collinear_3_eq_affine_dependent)
        then have "False"
          using False
          apply (clarsimp simp add: collinear_3 collinear_lemma)
          apply (drule Cauchy_theorem_flat [OF contf'])
          using pi_eq_y ynz
          apply (simp add: fabc add_eq_0_iff contour_integral_reverse_linepath)
          done
      }
      then obtain d where d: "d ∈ interior (convex hull {a, b, c})"
        by blast
      { fix d1
        assume d1_pos: "0 < d1"
           and d1: "⋀x x'. ⟦x∈convex hull {a, b, c}; x'∈convex hull {a, b, c}; cmod (x' - x) < d1⟧
                           ⟹ cmod (f x' - f x) < cmod y / (24 * C)"
        def e       "min 1 (min (d1/(4*C)) ((norm y / 24 / C) / B))"
        def shrink  "λx. x - e *R (x - d)"
        let ?pathint = "λx y. contour_integral(linepath x y) f"
        have e: "0 < e" "e ≤ 1" "e ≤ d1 / (4 * C)" "e ≤ cmod y / 24 / C / B"
          using d1_pos ‹C>0› ‹B>0› ynz by (simp_all add: e_def)
        then have eCB: "24 * e * C * B ≤ cmod y"
          using ‹C>0› ‹B>0›  by (simp add: field_simps)
        have e_le_d1: "e * (4 * C) ≤ d1"
          using e ‹C>0› by (simp add: field_simps)
        have "shrink a ∈ interior(convex hull {a,b,c})"
             "shrink b ∈ interior(convex hull {a,b,c})"
             "shrink c ∈ interior(convex hull {a,b,c})"
          using d e by (auto simp: hull_inc mem_interior_convex_shrink shrink_def)
        then have fhp0: "(f has_contour_integral 0)
                (linepath (shrink a) (shrink b) +++ linepath (shrink b) (shrink c) +++ linepath (shrink c) (shrink a))"
          by (simp add: Cauchy_theorem_triangle holomorphic_on_subset [OF holf] hull_minimal convex_interior)
        then have f_0_shrink: "?pathint (shrink a) (shrink b) + ?pathint (shrink b) (shrink c) + ?pathint (shrink c) (shrink a) = 0"
          by (simp add: has_chain_integral_chain_integral3)
        have fpi_abc: "f contour_integrable_on linepath (shrink a) (shrink b)"
                      "f contour_integrable_on linepath (shrink b) (shrink c)"
                      "f contour_integrable_on linepath (shrink c) (shrink a)"
          using fhp0  by (auto simp: valid_path_join dest: has_contour_integral_integrable)
        have cmod_shr: "⋀x y. cmod (shrink y - shrink x - (y - x)) = e * cmod (x - y)"
          using e by (simp add: shrink_def real_vector.scale_right_diff_distrib [symmetric])
        have sh_eq: "⋀a b d::complex. (b - e *R (b - d)) - (a - e *R (a - d)) - (b - a) = e *R (a - b)"
          by (simp add: algebra_simps)
        have "cmod y / (24 * C) ≤ cmod y / cmod (b - a) / 12"
          using False ‹C>0› diff_2C [of b a] ynz
          by (auto simp: divide_simps hull_inc)
        have less_C: "⟦u ∈ convex hull {a, b, c}; 0 ≤ x; x ≤ 1⟧ ⟹ x * cmod u < C" for x u
          apply (cases "x=0", simp add: ‹0<C›)
          using Cno [of u] mult_left_le_one_le [of "cmod u" x] le_less_trans norm_ge_zero by blast
        { fix u v
          assume uv: "u ∈ convex hull {a, b, c}" "v ∈ convex hull {a, b, c}" "u≠v"
             and fpi_uv: "f contour_integrable_on linepath (shrink u) (shrink v)"
          have shr_uv: "shrink u ∈ interior(convex hull {a,b,c})"
                       "shrink v ∈ interior(convex hull {a,b,c})"
            using d e uv
            by (auto simp: hull_inc mem_interior_convex_shrink shrink_def)
          have cmod_fuv: "⋀x. 0≤x ⟹ x≤1 ⟹ cmod (f (linepath (shrink u) (shrink v) x)) ≤ B"
            using shr_uv by (blast intro: Bnf linepath_in_convex_hull interior_subset [THEN subsetD])
          have By_uv: "B * (12 * (e * cmod (u - v))) ≤ cmod y"
            apply (rule order_trans [OF _ eCB])
            using e ‹B>0› diff_2C [of u v] uv
            by (auto simp: field_simps)
          { fix x::real   assume x: "0≤x" "x≤1"
            have cmod_less_4C: "cmod ((1 - x) *R u - (1 - x) *R d) + cmod (x *R v - x *R d) < (C+C) + (C+C)"
              apply (rule add_strict_mono; rule norm_triangle_half_l [of _ 0])
              using uv x d interior_subset
              apply (auto simp: hull_inc intro!: less_C)
              done
            have ll: "linepath (shrink u) (shrink v) x - linepath u v x = -e * ((1 - x) *R (u - d) + x *R (v - d))"
              by (simp add: linepath_def shrink_def algebra_simps scaleR_conv_of_real)
            have cmod_less_dt: "cmod (linepath (shrink u) (shrink v) x - linepath u v x) < d1"
              using ‹e>0›
              apply (simp add: ll norm_mult scaleR_diff_right)
              apply (rule less_le_trans [OF _ e_le_d1])
              using cmod_less_4C
              apply (force intro: norm_triangle_lt)
              done
            have "cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) < cmod y / (24 * C)"
              using x uv shr_uv cmod_less_dt
              by (auto simp: hull_inc intro: d1 interior_subset [THEN subsetD] linepath_in_convex_hull)
            also have "... ≤ cmod y / cmod (v - u) / 12"
              using False uv ‹C>0› diff_2C [of v u] ynz
              by (auto simp: divide_simps hull_inc)
            finally have "cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) ≤ cmod y / cmod (v - u) / 12"
              by simp
            then have cmod_12_le: "cmod (v - u) * cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) * 12 ≤ cmod y"
              using uv False by (auto simp: field_simps)
            have "cmod (f (linepath (shrink u) (shrink v) x)) * cmod (shrink v - shrink u - (v - u)) +
                  cmod (v - u) * cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x))
                  ≤ cmod y / 6"
              apply (rule order_trans [of _ "B*((norm y / 24 / C / B)*2*C) + (2*C)*(norm y /24 / C)"])
              apply (rule add_mono [OF mult_mono])
              using By_uv e ‹0 < B› ‹0 < C› x ynz
              apply (simp_all add: cmod_fuv cmod_shr cmod_12_le hull_inc)
              apply (simp add: field_simps)
              done
          } note cmod_diff_le = this
          have f_uv: "continuous_on (closed_segment u v) f"
            by (blast intro: uv continuous_on_subset [OF contf closed_segment_subset_convex_hull])
          have **: "⋀f' x' f x::complex. f'*x' - f*x = f'*(x' - x) + x*(f' - f)"
            by (simp add: algebra_simps)
          have "norm (?pathint (shrink u) (shrink v) - ?pathint u v) ≤ norm y / 6"
            apply (rule order_trans)
            apply (rule has_integral_bound
                    [of "B*(norm y /24/C/B)*2*C + (2*C)*(norm y/24/C)"
                        "λx. f(linepath (shrink u) (shrink v) x) * (shrink v - shrink u) - f(linepath u v x)*(v - u)"
                        _ 0 1 ])
            using ynz ‹0 < B› ‹0 < C›
            apply (simp_all del: le_divide_eq_numeral1)
            apply (simp add: has_integral_sub has_contour_integral_linepath [symmetric] has_contour_integral_integral
                             fpi_uv f_uv contour_integrable_continuous_linepath, clarify)
            apply (simp only: **)
            apply (simp add: norm_triangle_le norm_mult cmod_diff_le del: le_divide_eq_numeral1)
            done
          } note * = this
          have "norm (?pathint (shrink a) (shrink b) - ?pathint a b) ≤ norm y / 6"
            using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
          moreover
          have "norm (?pathint (shrink b) (shrink c) - ?pathint b c) ≤ norm y / 6"
            using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
          moreover
          have "norm (?pathint (shrink c) (shrink a) - ?pathint c a) ≤ norm y / 6"
            using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
          ultimately
          have "norm((?pathint (shrink a) (shrink b) - ?pathint a b) +
                     (?pathint (shrink b) (shrink c) - ?pathint b c) + (?pathint (shrink c) (shrink a) - ?pathint c a))
                ≤ norm y / 6 + norm y / 6 + norm y / 6"
            by (metis norm_triangle_le add_mono)
          also have "... = norm y / 2"
            by simp
          finally have "norm((?pathint (shrink a) (shrink b) + ?pathint (shrink b) (shrink c) + ?pathint (shrink c) (shrink a)) -
                          (?pathint a b + ?pathint b c + ?pathint c a))
                ≤ norm y / 2"
            by (simp add: algebra_simps)
          then
          have "norm(?pathint a b + ?pathint b c + ?pathint c a) ≤ norm y / 2"
            by (simp add: f_0_shrink) (metis (mono_tags) add.commute minus_add_distrib norm_minus_cancel uminus_add_conv_diff)
          then have "False"
            using pi_eq_y ynz by auto
        }
        moreover have "uniformly_continuous_on (convex hull {a,b,c}) f"
          by (simp add: contf compact_convex_hull compact_uniformly_continuous)
        ultimately have "False"
          unfolding uniformly_continuous_on_def
          by (force simp: ynz ‹0 < C› dist_norm)
        then show ?thesis ..
      qed
  }
  moreover have "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
    using fabc contour_integrable_continuous_linepath by auto
  ultimately show ?thesis
    using has_contour_integral_integral by fastforce
qed


subsection‹Version allowing finite number of exceptional points›

lemma Cauchy_theorem_triangle_cofinite:
  assumes "continuous_on (convex hull {a,b,c}) f"
      and "finite s"
      and "(⋀x. x ∈ interior(convex hull {a,b,c}) - s ⟹ f field_differentiable (at x))"
     shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
using assms
proof (induction "card s" arbitrary: a b c s rule: less_induct)
  case (less s a b c)
  show ?case
  proof (cases "s={}")
    case True with less show ?thesis
      by (fastforce simp: holomorphic_on_def field_differentiable_at_within
                    Cauchy_theorem_triangle_interior)
  next
    case False
    then obtain d s' where d: "s = insert d s'" "d ∉ s'"
      by (meson Set.set_insert all_not_in_conv)
    then show ?thesis
    proof (cases "d ∈ convex hull {a,b,c}")
      case False
      show "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
        apply (rule less.hyps [of "s'"])
        using False d ‹finite s› interior_subset
        apply (auto intro!: less.prems)
        done
    next
      case True
      have *: "convex hull {a, b, d} ⊆ convex hull {a, b, c}"
        by (meson True hull_subset insert_subset convex_hull_subset)
      have abd: "(f has_contour_integral 0) (linepath a b +++ linepath b d +++ linepath d a)"
        apply (rule less.hyps [of "s'"])
        using True d  ‹finite s› not_in_interior_convex_hull_3
        apply (auto intro!: less.prems continuous_on_subset [OF  _ *])
        apply (metis * insert_absorb insert_subset interior_mono)
        done
      have *: "convex hull {b, c, d} ⊆ convex hull {a, b, c}"
        by (meson True hull_subset insert_subset convex_hull_subset)
      have bcd: "(f has_contour_integral 0) (linepath b c +++ linepath c d +++ linepath d b)"
        apply (rule less.hyps [of "s'"])
        using True d  ‹finite s› not_in_interior_convex_hull_3
        apply (auto intro!: less.prems continuous_on_subset [OF _ *])
        apply (metis * insert_absorb insert_subset interior_mono)
        done
      have *: "convex hull {c, a, d} ⊆ convex hull {a, b, c}"
        by (meson True hull_subset insert_subset convex_hull_subset)
      have cad: "(f has_contour_integral 0) (linepath c a +++ linepath a d +++ linepath d c)"
        apply (rule less.hyps [of "s'"])
        using True d  ‹finite s› not_in_interior_convex_hull_3
        apply (auto intro!: less.prems continuous_on_subset [OF _ *])
        apply (metis * insert_absorb insert_subset interior_mono)
        done
      have "f contour_integrable_on linepath a b"
        using less.prems
        by (metis continuous_on_subset insert_commute contour_integrable_continuous_linepath segments_subset_convex_hull(3))
      moreover have "f contour_integrable_on linepath b c"
        using less.prems
        by (metis continuous_on_subset contour_integrable_continuous_linepath segments_subset_convex_hull(3))
      moreover have "f contour_integrable_on linepath c a"
        using less.prems
        by (metis continuous_on_subset insert_commute contour_integrable_continuous_linepath segments_subset_convex_hull(3))
      ultimately have fpi: "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
        by auto
      { fix y::complex
        assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
           and ynz: "y ≠ 0"
        have cont_ad: "continuous_on (closed_segment a d) f"
          by (meson "*" continuous_on_subset less.prems(1) segments_subset_convex_hull(3))
        have cont_bd: "continuous_on (closed_segment b d) f"
          by (meson True closed_segment_subset_convex_hull continuous_on_subset hull_subset insert_subset less.prems(1))
        have cont_cd: "continuous_on (closed_segment c d) f"
          by (meson "*" continuous_on_subset less.prems(1) segments_subset_convex_hull(2))
        have "contour_integral  (linepath a b) f = - (contour_integral (linepath b d) f + (contour_integral (linepath d a) f))"
                "contour_integral  (linepath b c) f = - (contour_integral (linepath c d) f + (contour_integral (linepath d b) f))"
                "contour_integral  (linepath c a) f = - (contour_integral (linepath a d) f + contour_integral (linepath d c) f)"
            using has_chain_integral_chain_integral3 [OF abd]
                  has_chain_integral_chain_integral3 [OF bcd]
                  has_chain_integral_chain_integral3 [OF cad]
            by (simp_all add: algebra_simps add_eq_0_iff)
        then have ?thesis
          using cont_ad cont_bd cont_cd fy has_chain_integral_chain_integral3 contour_integral_reverse_linepath by fastforce
      }
      then show ?thesis
        using fpi contour_integrable_on_def by blast
    qed
  qed
qed


subsection‹Cauchy's theorem for an open starlike set›

lemma starlike_convex_subset:
  assumes s: "a ∈ s" "closed_segment b c ⊆ s" and subs: "⋀x. x ∈ s ⟹ closed_segment a x ⊆ s"
    shows "convex hull {a,b,c} ⊆ s"
      using s
      apply (clarsimp simp add: convex_hull_insert [of "{b,c}" a] segment_convex_hull)
      apply (meson subs convexD convex_closed_segment ends_in_segment(1) ends_in_segment(2) subsetCE)
      done

lemma triangle_contour_integrals_starlike_primitive:
  assumes contf: "continuous_on s f"
      and s: "a ∈ s" "open s"
      and x: "x ∈ s"
      and subs: "⋀y. y ∈ s ⟹ closed_segment a y ⊆ s"
      and zer: "⋀b c. closed_segment b c ⊆ s
                   ⟹ contour_integral (linepath a b) f + contour_integral (linepath b c) f +
                       contour_integral (linepath c a) f = 0"
    shows "((λx. contour_integral(linepath a x) f) has_field_derivative f x) (at x)"
proof -
  let ?pathint = "λx y. contour_integral(linepath x y) f"
  { fix e y
    assume e: "0 < e" and bxe: "ball x e ⊆ s" and close: "cmod (y - x) < e"
    have y: "y ∈ s"
      using bxe close  by (force simp: dist_norm norm_minus_commute)
    have cont_ayf: "continuous_on (closed_segment a y) f"
      using contf continuous_on_subset subs y by blast
    have xys: "closed_segment x y ⊆ s"
      apply (rule order_trans [OF _ bxe])
      using close
      by (auto simp: dist_norm ball_def norm_minus_commute dest: segment_bound)
    have "?pathint a y - ?pathint a x = ?pathint x y"
      using zer [OF xys]  contour_integral_reverse_linepath [OF cont_ayf]  add_eq_0_iff by force
  } note [simp] = this
  { fix e::real
    assume e: "0 < e"
    have cont_atx: "continuous (at x) f"
      using x s contf continuous_on_eq_continuous_at by blast
    then obtain d1 where d1: "d1>0" and d1_less: "⋀y. cmod (y - x) < d1 ⟹ cmod (f y - f x) < e/2"
      unfolding continuous_at Lim_at dist_norm  using e
      by (drule_tac x="e/2" in spec) force
    obtain d2 where d2: "d2>0" "ball x d2 ⊆ s" using  ‹open s› x
      by (auto simp: open_contains_ball)
    have dpos: "min d1 d2 > 0" using d1 d2 by simp
    { fix y
      assume yx: "y ≠ x" and close: "cmod (y - x) < min d1 d2"
      have y: "y ∈ s"
        using d2 close  by (force simp: dist_norm norm_minus_commute)
      have fxy: "f contour_integrable_on linepath x y"
        apply (rule contour_integrable_continuous_linepath)
        apply (rule continuous_on_subset [OF contf])
        using close d2
        apply (auto simp: dist_norm norm_minus_commute dest!: segment_bound(1))
        done
      then obtain i where i: "(f has_contour_integral i) (linepath x y)"
        by (auto simp: contour_integrable_on_def)
      then have "((λw. f w - f x) has_contour_integral (i - f x * (y - x))) (linepath x y)"
        by (rule has_contour_integral_diff [OF _ has_contour_integral_const_linepath])
      then have "cmod (i - f x * (y - x)) ≤ e / 2 * cmod (y - x)"
        apply (rule has_contour_integral_bound_linepath [where B = "e/2"])
        using e apply simp
        apply (rule d1_less [THEN less_imp_le])
        using close segment_bound
        apply force
        done
      also have "... < e * cmod (y - x)"
        by (simp add: e yx)
      finally have "cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
        using i yx  by (simp add: contour_integral_unique divide_less_eq)
    }
    then have "∃d>0. ∀y. y ≠ x ∧ cmod (y-x) < d ⟶ cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
      using dpos by blast
  }
  then have *: "(λy. (?pathint x y - f x * (y - x)) /R cmod (y - x)) ─x→ 0"
    by (simp add: Lim_at dist_norm inverse_eq_divide)
  show ?thesis
    apply (simp add: has_field_derivative_def has_derivative_at bounded_linear_mult_right)
    apply (rule Lim_transform [OF * Lim_eventually])
    apply (simp add: inverse_eq_divide [symmetric] eventually_at)
    using ‹open s› x
    apply (force simp: dist_norm open_contains_ball)
    done
qed

(** Existence of a primitive.*)

lemma holomorphic_starlike_primitive:
  fixes f :: "complex ⇒ complex"
  assumes contf: "continuous_on s f"
      and s: "starlike s" and os: "open s"
      and k: "finite k"
      and fcd: "⋀x. x ∈ s - k ⟹ f field_differentiable at x"
    shows "∃g. ∀x ∈ s. (g has_field_derivative f x) (at x)"
proof -
  obtain a where a: "a∈s" and a_cs: "⋀x. x∈s ⟹ closed_segment a x ⊆ s"
    using s by (auto simp: starlike_def)
  { fix x b c
    assume "x ∈ s" "closed_segment b c ⊆ s"
    then have abcs: "convex hull {a, b, c} ⊆ s"
      by (simp add: a a_cs starlike_convex_subset)
    then have *: "continuous_on (convex hull {a, b, c}) f"
      by (simp add: continuous_on_subset [OF contf])
    have "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
      apply (rule Cauchy_theorem_triangle_cofinite [OF _ k])
      using abcs apply (simp add: continuous_on_subset [OF contf])
      using * abcs interior_subset apply (auto intro: fcd)
      done
  } note 0 = this
  show ?thesis
    apply (intro exI ballI)
    apply (rule triangle_contour_integrals_starlike_primitive [OF contf a os], assumption)
    apply (metis a_cs)
    apply (metis has_chain_integral_chain_integral3 0)
    done
qed

lemma Cauchy_theorem_starlike:
 "⟦open s; starlike s; finite k; continuous_on s f;
   ⋀x. x ∈ s - k ⟹ f field_differentiable at x;
   valid_path g; path_image g ⊆ s; pathfinish g = pathstart g⟧
   ⟹ (f has_contour_integral 0)  g"
  by (metis holomorphic_starlike_primitive Cauchy_theorem_primitive at_within_open)

lemma Cauchy_theorem_starlike_simple:
  "⟦open s; starlike s; f holomorphic_on s; valid_path g; path_image g ⊆ s; pathfinish g = pathstart g⟧
   ⟹ (f has_contour_integral 0) g"
apply (rule Cauchy_theorem_starlike [OF _ _ finite.emptyI])
apply (simp_all add: holomorphic_on_imp_continuous_on)
apply (metis at_within_open holomorphic_on_def)
done


subsection‹Cauchy's theorem for a convex set›

text‹For a convex set we can avoid assuming openness and boundary analyticity›

lemma triangle_contour_integrals_convex_primitive:
  assumes contf: "continuous_on s f"
      and s: "a ∈ s" "convex s"
      and x: "x ∈ s"
      and zer: "⋀b c. ⟦b ∈ s; c ∈ s⟧
                   ⟹ contour_integral (linepath a b) f + contour_integral (linepath b c) f +
                       contour_integral (linepath c a) f = 0"
    shows "((λx. contour_integral(linepath a x) f) has_field_derivative f x) (at x within s)"
proof -
  let ?pathint = "λx y. contour_integral(linepath x y) f"
  { fix y
    assume y: "y ∈ s"
    have cont_ayf: "continuous_on (closed_segment a y) f"
      using s y  by (meson contf continuous_on_subset convex_contains_segment)
    have xys: "closed_segment x y ⊆ s"  (*?*)
      using convex_contains_segment s x y by auto
    have "?pathint a y - ?pathint a x = ?pathint x y"
      using zer [OF x y]  contour_integral_reverse_linepath [OF cont_ayf]  add_eq_0_iff by force
  } note [simp] = this
  { fix e::real
    assume e: "0 < e"
    have cont_atx: "continuous (at x within s) f"
      using x s contf  by (simp add: continuous_on_eq_continuous_within)
    then obtain d1 where d1: "d1>0" and d1_less: "⋀y. ⟦y ∈ s; cmod (y - x) < d1⟧ ⟹ cmod (f y - f x) < e/2"
      unfolding continuous_within Lim_within dist_norm using e
      by (drule_tac x="e/2" in spec) force
    { fix y
      assume yx: "y ≠ x" and close: "cmod (y - x) < d1" and y: "y ∈ s"
      have fxy: "f contour_integrable_on linepath x y"
        using convex_contains_segment s x y
        by (blast intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
      then obtain i where i: "(f has_contour_integral i) (linepath x y)"
        by (auto simp: contour_integrable_on_def)
      then have "((λw. f w - f x) has_contour_integral (i - f x * (y - x))) (linepath x y)"
        by (rule has_contour_integral_diff [OF _ has_contour_integral_const_linepath])
      then have "cmod (i - f x * (y - x)) ≤ e / 2 * cmod (y - x)"
        apply (rule has_contour_integral_bound_linepath [where B = "e/2"])
        using e apply simp
        apply (rule d1_less [THEN less_imp_le])
        using convex_contains_segment s(2) x y apply blast
        using close segment_bound(1) apply fastforce
        done
      also have "... < e * cmod (y - x)"
        by (simp add: e yx)
      finally have "cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
        using i yx  by (simp add: contour_integral_unique divide_less_eq)
    }
    then have "∃d>0. ∀y∈s. y ≠ x ∧ cmod (y-x) < d ⟶ cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
      using d1 by blast
  }
  then have *: "((λy. (contour_integral (linepath x y) f - f x * (y - x)) /R cmod (y - x)) ⤏ 0) (at x within s)"
    by (simp add: Lim_within dist_norm inverse_eq_divide)
  show ?thesis
    apply (simp add: has_field_derivative_def has_derivative_within bounded_linear_mult_right)
    apply (rule Lim_transform [OF * Lim_eventually])
    using linordered_field_no_ub
    apply (force simp: inverse_eq_divide [symmetric] eventually_at)
    done
qed

lemma contour_integral_convex_primitive:
  "⟦convex s; continuous_on s f;
    ⋀a b c. ⟦a ∈ s; b ∈ s; c ∈ s⟧ ⟹ (f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)⟧
         ⟹ ∃g. ∀x ∈ s. (g has_field_derivative f x) (at x within s)"
  apply (cases "s={}")
  apply (simp_all add: ex_in_conv [symmetric])
  apply (blast intro: triangle_contour_integrals_convex_primitive has_chain_integral_chain_integral3)
  done

lemma holomorphic_convex_primitive:
  fixes f :: "complex ⇒ complex"
  shows
  "⟦convex s; finite k; continuous_on s f;
    ⋀x. x ∈ interior s - k ⟹ f field_differentiable at x⟧
   ⟹ ∃g. ∀x ∈ s. (g has_field_derivative f x) (at x within s)"
apply (rule contour_integral_convex_primitive [OF _ _ Cauchy_theorem_triangle_cofinite])
prefer 3
apply (erule continuous_on_subset)
apply (simp add: subset_hull continuous_on_subset, assumption+)
by (metis Diff_iff convex_contains_segment insert_absorb insert_subset interior_mono segment_convex_hull subset_hull)

lemma Cauchy_theorem_convex:
    "⟦continuous_on s f; convex s; finite k;
      ⋀x. x ∈ interior s - k ⟹ f field_differentiable at x;
     valid_path g; path_image g ⊆ s;
     pathfinish g = pathstart g⟧ ⟹ (f has_contour_integral 0) g"
  by (metis holomorphic_convex_primitive Cauchy_theorem_primitive)

lemma Cauchy_theorem_convex_simple:
    "⟦f holomorphic_on s; convex s;
     valid_path g; path_image g ⊆ s;
     pathfinish g = pathstart g⟧ ⟹ (f has_contour_integral 0) g"
  apply (rule Cauchy_theorem_convex)
  apply (simp_all add: holomorphic_on_imp_continuous_on)
  apply (rule finite.emptyI)
  using at_within_interior holomorphic_on_def interior_subset by fastforce


text‹In particular for a disc›
lemma Cauchy_theorem_disc:
    "⟦finite k; continuous_on (cball a e) f;
      ⋀x. x ∈ ball a e - k ⟹ f field_differentiable at x;
     valid_path g; path_image g ⊆ cball a e;
     pathfinish g = pathstart g⟧ ⟹ (f has_contour_integral 0) g"
  apply (rule Cauchy_theorem_convex)
  apply (auto simp: convex_cball interior_cball)
  done

lemma Cauchy_theorem_disc_simple:
    "⟦f holomorphic_on (ball a e); valid_path g; path_image g ⊆ ball a e;
     pathfinish g = pathstart g⟧ ⟹ (f has_contour_integral 0) g"
by (simp add: Cauchy_theorem_convex_simple)


subsection‹Generalize integrability to local primitives›

lemma contour_integral_local_primitive_lemma:
  fixes f :: "complex⇒complex"
  shows
    "⟦g piecewise_differentiable_on {a..b};
      ⋀x. x ∈ s ⟹ (f has_field_derivative f' x) (at x within s);
      ⋀x. x ∈ {a..b} ⟹ g x ∈ s⟧
     ⟹ (λx. f' (g x) * vector_derivative g (at x within {a..b}))
            integrable_on {a..b}"
  apply (cases "cbox a b = {}", force)
  apply (simp add: integrable_on_def)
  apply (rule exI)
  apply (rule contour_integral_primitive_lemma, assumption+)
  using atLeastAtMost_iff by blast

lemma contour_integral_local_primitive_any:
  fixes f :: "complex ⇒ complex"
  assumes gpd: "g piecewise_differentiable_on {a..b}"
      and dh: "⋀x. x ∈ s
               ⟹ ∃d h. 0 < d ∧
                         (∀y. norm(y - x) < d ⟶ (h has_field_derivative f y) (at y within s))"
      and gs: "⋀x. x ∈ {a..b} ⟹ g x ∈ s"
  shows "(λx. f(g x) * vector_derivative g (at x)) integrable_on {a..b}"
proof -
  { fix x
    assume x: "a ≤ x" "x ≤ b"
    obtain d h where d: "0 < d"
               and h: "(⋀y. norm(y - g x) < d ⟹ (h has_field_derivative f y) (at y within s))"
      using x gs dh by (metis atLeastAtMost_iff)
    have "continuous_on {a..b} g" using gpd piecewise_differentiable_on_def by blast
    then obtain e where e: "e>0" and lessd: "⋀x'. x' ∈ {a..b} ⟹ ¦x' - x¦ < e ⟹ cmod (g x' - g x) < d"
      using x d
      apply (auto simp: dist_norm continuous_on_iff)
      apply (drule_tac x=x in bspec)
      using x apply simp
      apply (drule_tac x=d in spec, auto)
      done
    have "∃d>0. ∀u v. u ≤ x ∧ x ≤ v ∧ {u..v} ⊆ ball x d ∧ (u ≤ v ⟶ a ≤ u ∧ v ≤ b) ⟶
                          (λx. f (g x) * vector_derivative g (at x)) integrable_on {u..v}"
      apply (rule_tac x=e in exI)
      using e
      apply (simp add: integrable_on_localized_vector_derivative [symmetric], clarify)
      apply (rule_tac f = h and s = "g ` {u..v}" in contour_integral_local_primitive_lemma)
        apply (meson atLeastatMost_subset_iff gpd piecewise_differentiable_on_subset)
       apply (force simp: ball_def dist_norm intro: lessd gs DERIV_subset [OF h], force)
      done
  } then
  show ?thesis
    by (force simp: intro!: integrable_on_little_subintervals [of a b, simplified])
qed

lemma contour_integral_local_primitive:
  fixes f :: "complex ⇒ complex"
  assumes g: "valid_path g" "path_image g ⊆ s"
      and dh: "⋀x. x ∈ s
               ⟹ ∃d h. 0 < d ∧
                         (∀y. norm(y - x) < d ⟶ (h has_field_derivative f y) (at y within s))"
  shows "f contour_integrable_on g"
  using g
  apply (simp add: valid_path_def path_image_def contour_integrable_on_def has_contour_integral_def
            has_integral_localized_vector_derivative integrable_on_def [symmetric])
  using contour_integral_local_primitive_any [OF _ dh]
  by (meson image_subset_iff piecewise_C1_imp_differentiable)


text‹In particular if a function is holomorphic›

lemma contour_integrable_holomorphic:
  assumes contf: "continuous_on s f"
      and os: "open s"
      and k: "finite k"
      and g: "valid_path g" "path_image g ⊆ s"
      and fcd: "⋀x. x ∈ s - k ⟹ f field_differentiable at x"
    shows "f contour_integrable_on g"
proof -
  { fix z
    assume z: "z ∈ s"
    obtain d where d: "d>0" "ball z d ⊆ s" using  ‹open s› z
      by (auto simp: open_contains_ball)
    then have contfb: "continuous_on (ball z d) f"
      using contf continuous_on_subset by blast
    obtain h where "∀y∈ball z d. (h has_field_derivative f y) (at y within ball z d)"
      using holomorphic_convex_primitive [OF convex_ball k contfb fcd] d
            interior_subset by force
    then have "∀y∈ball z d. (h has_field_derivative f y) (at y within s)"
      by (metis Topology_Euclidean_Space.open_ball at_within_open d(2) os subsetCE)
    then have "∃h. (∀y. cmod (y - z) < d ⟶ (h has_field_derivative f y) (at y within s))"
      by (force simp: dist_norm norm_minus_commute)
    then have "∃d h. 0 < d ∧ (∀y. cmod (y - z) < d ⟶ (h has_field_derivative f y) (at y within s))"
      using d by blast
  }
  then show ?thesis
    by (rule contour_integral_local_primitive [OF g])
qed

lemma contour_integrable_holomorphic_simple:
  assumes fh: "f holomorphic_on s"
      and os: "open s"
      and g: "valid_path g" "path_image g ⊆ s"
    shows "f contour_integrable_on g"
  apply (rule contour_integrable_holomorphic [OF _ os Finite_Set.finite.emptyI g])
  apply (simp add: fh holomorphic_on_imp_continuous_on)
  using fh  by (simp add: field_differentiable_def holomorphic_on_open os)

lemma continuous_on_inversediff:
  fixes z:: "'a::real_normed_field" shows "z ∉ s ⟹ continuous_on s (λw. 1 / (w - z))"
  by (rule continuous_intros | force)+

corollary contour_integrable_inversediff:
    "⟦valid_path g; z ∉ path_image g⟧ ⟹ (λw. 1 / (w-z)) contour_integrable_on g"
apply (rule contour_integrable_holomorphic_simple [of _ "UNIV-{z}"])
apply (auto simp: holomorphic_on_open open_delete intro!: derivative_eq_intros)
done

text‹Key fact that path integral is the same for a "nearby" path. This is the
 main lemma for the homotopy form of Cauchy's theorem and is also useful
 if we want "without loss of generality" to assume some nice properties of a
 path (e.g. smoothness). It can also be used to define the integrals of
 analytic functions over arbitrary continuous paths. This is just done for
 winding numbers now.
›

text‹A technical definition to avoid duplication of similar proofs,
     for paths joined at the ends versus looping paths›
definition linked_paths :: "bool ⇒ (real ⇒ 'a) ⇒ (real ⇒ 'a::topological_space) ⇒ bool"
  where "linked_paths atends g h ==
        (if atends then pathstart h = pathstart g ∧ pathfinish h = pathfinish g
                   else pathfinish g = pathstart g ∧ pathfinish h = pathstart h)"

text‹This formulation covers two cases: @{term g} and @{term h} share their
      start and end points; @{term g} and @{term h} both loop upon themselves.›
lemma contour_integral_nearby:
  assumes os: "open s" and p: "path p" "path_image p ⊆ s"
    shows
       "∃d. 0 < d ∧
            (∀g h. valid_path g ∧ valid_path h ∧
                  (∀t ∈ {0..1}. norm(g t - p t) < d ∧ norm(h t - p t) < d) ∧
                  linked_paths atends g h
                  ⟶ path_image g ⊆ s ∧ path_image h ⊆ s ∧
                      (∀f. f holomorphic_on s ⟶ contour_integral h f = contour_integral g f))"
proof -
  have "∀z. ∃e. z ∈ path_image p ⟶ 0 < e ∧ ball z e ⊆ s"
    using open_contains_ball os p(2) by blast
  then obtain ee where ee: "⋀z. z ∈ path_image p ⟹ 0 < ee z ∧ ball z (ee z) ⊆ s"
    by metis
  def cover  "(λz. ball z (ee z/3)) ` (path_image p)"
  have "compact (path_image p)"
    by (metis p(1) compact_path_image)
  moreover have "path_image p ⊆ (⋃c∈path_image p. ball c (ee c / 3))"
    using ee by auto
  ultimately have "∃D ⊆ cover. finite D ∧ path_image p ⊆ ⋃D"
    by (simp add: compact_eq_heine_borel cover_def)
  then obtain D where D: "D ⊆ cover" "finite D" "path_image p ⊆ ⋃D"
    by blast
  then obtain k where k: "k ⊆ {0..1}" "finite k" and D_eq: "D = ((λz. ball z (ee z / 3)) ∘ p) ` k"
    apply (simp add: cover_def path_image_def image_comp)
    apply (blast dest!: finite_subset_image [OF ‹finite D›])
    done
  then have kne: "k ≠ {}"
    using D by auto
  have pi: "⋀i. i ∈ k ⟹ p i ∈ path_image p"
    using k  by (auto simp: path_image_def)
  then have eepi: "⋀i. i ∈ k ⟹ 0 < ee((p i))"
    by (metis ee)
  def e  "Min((ee o p) ` k)"
  have fin_eep: "finite ((ee o p) ` k)"
    using k  by blast
  have enz: "0 < e"
    using ee k  by (simp add: kne e_def Min_gr_iff [OF fin_eep] eepi)
  have "uniformly_continuous_on {0..1} p"
    using p  by (simp add: path_def compact_uniformly_continuous)
  then obtain d::real where d: "d>0"
          and de: "⋀x x'. ¦x' - x¦ < d ⟹ x∈{0..1} ⟹ x'∈{0..1} ⟹ cmod (p x' - p x) < e/3"
    unfolding uniformly_continuous_on_def dist_norm real_norm_def
    by (metis divide_pos_pos enz zero_less_numeral)
  then obtain N::nat where N: "N>0" "inverse N < d"
    using real_arch_inverse [of d]   by auto
  { fix g h
    assume g: "valid_path g" and gp: "∀t∈{0..1}. cmod (g t - p t) < e / 3"
       and h: "valid_path h" and hp: "∀t∈{0..1}. cmod (h t - p t) < e / 3"
       and joins: "linked_paths atends g h"
    { fix t::real
      assume t: "0 ≤ t" "t ≤ 1"
      then obtain u where u: "u ∈ k" and ptu: "p t ∈ ball(p u) (ee(p u) / 3)"
        using ‹path_image p ⊆ ⋃D› D_eq by (force simp: path_image_def)
      then have ele: "e ≤ ee (p u)" using fin_eep
        by (simp add: e_def)
      have "cmod (g t - p t) < e / 3" "cmod (h t - p t) < e / 3"
        using gp hp t by auto
      with ele have "cmod (g t - p t) < ee (p u) / 3"
                    "cmod (h t - p t) < ee (p u) / 3"
        by linarith+
      then have "g t ∈ ball(p u) (ee(p u))"  "h t ∈ ball(p u) (ee(p u))"
        using norm_diff_triangle_ineq [of "g t" "p t" "p t" "p u"]
              norm_diff_triangle_ineq [of "h t" "p t" "p t" "p u"] ptu eepi u
        by (force simp: dist_norm ball_def norm_minus_commute)+
      then have "g t ∈ s" "h t ∈ s" using ee u k
        by (auto simp: path_image_def ball_def)
    }
    then have ghs: "path_image g ⊆ s" "path_image h ⊆ s"
      by (auto simp: path_image_def)
    moreover
    { fix f
      assume fhols: "f holomorphic_on s"
      then have fpa: "f contour_integrable_on g"  "f contour_integrable_on h"
        using g ghs h holomorphic_on_imp_continuous_on os contour_integrable_holomorphic_simple
        by blast+
      have contf: "continuous_on s f"
        by (simp add: fhols holomorphic_on_imp_continuous_on)
      { fix z
        assume z: "z ∈ path_image p"
        have "f holomorphic_on ball z (ee z)"
          using fhols ee z holomorphic_on_subset by blast
        then have "∃ff. (∀w ∈ ball z (ee z). (ff has_field_derivative f w) (at w))"
          using holomorphic_convex_primitive [of "ball z (ee z)" "{}" f, simplified]
          by (metis open_ball at_within_open holomorphic_on_def holomorphic_on_imp_continuous_on mem_ball)
      }
      then obtain ff where ff:
            "⋀z w. ⟦z ∈ path_image p; w ∈ ball z (ee z)⟧ ⟹ (ff z has_field_derivative f w) (at w)"
        by metis
      { fix n
        assume n: "n ≤ N"
        then have "contour_integral(subpath 0 (n/N) h) f - contour_integral(subpath 0 (n/N) g) f =
                   contour_integral(linepath (g(n/N)) (h(n/N))) f - contour_integral(linepath (g 0) (h 0)) f"
        proof (induct n)
          case 0 show ?case by simp
        next
          case (Suc n)
          obtain t where t: "t ∈ k" and "p (n/N) ∈ ball(p t) (ee(p t) / 3)"
            using ‹path_image p ⊆ ⋃D› [THEN subsetD, where c="p (n/N)"] D_eq N Suc.prems
            by (force simp: path_image_def)
          then have ptu: "cmod (p t - p (n/N)) < ee (p t) / 3"
            by (simp add: dist_norm)
          have e3le: "e/3 ≤ ee (p t) / 3"  using fin_eep t
            by (simp add: e_def)
          { fix x
            assume x: "n/N ≤ x" "x ≤ (1 + n)/N"
            then have nN01: "0 ≤ n/N" "(1 + n)/N ≤ 1"
              using Suc.prems by auto
            then have x01: "0 ≤ x" "x ≤ 1"
              using x by linarith+
            have "cmod (p t - p x)  < ee (p t) / 3 + e/3"
              apply (rule norm_diff_triangle_less [OF ptu de])
              using x N x01 Suc.prems
              apply (auto simp: field_simps)
              done
            then have ptx: "cmod (p t - p x) < 2*ee (p t)/3"
              using e3le eepi [OF t] by simp
            have "cmod (p t - g x) < 2*ee (p t)/3 + e/3 "
              apply (rule norm_diff_triangle_less [OF ptx])
              using gp x01 by (simp add: norm_minus_commute)
            also have "... ≤ ee (p t)"
              using e3le eepi [OF t] by simp
            finally have gg: "cmod (p t - g x) < ee (p t)" .
            have "cmod (p t - h x) < 2*ee (p t)/3 + e/3 "
              apply (rule norm_diff_triangle_less [OF ptx])
              using hp x01 by (simp add: norm_minus_commute)
            also have "... ≤ ee (p t)"
              using e3le eepi [OF t] by simp
            finally have "cmod (p t - g x) < ee (p t)"
                         "cmod (p t - h x) < ee (p t)"
              using gg by auto
          } note ptgh_ee = this
          have pi_hgn: "path_image (linepath (h (n/N)) (g (n/N))) ⊆ ball (p t) (ee (p t))"
            using ptgh_ee [of "n/N"] Suc.prems
            by (auto simp: field_simps dist_norm dest: segment_furthest_le [where y="p t"])
          then have gh_ns: "closed_segment (g (n/N)) (h (n/N)) ⊆ s"
            using ‹N>0› Suc.prems
            apply (simp add: path_image_join field_simps closed_segment_commute)
            apply (erule order_trans)
            apply (simp add: ee pi t)
            done
          have pi_ghn': "path_image (linepath (g ((1 + n) / N)) (h ((1 + n) / N)))
                  ⊆ ball (p t) (ee (p t))"
            using ptgh_ee [of "(1+n)/N"] Suc.prems
            by (auto simp: field_simps dist_norm dest: segment_furthest_le [where y="p t"])
          then have gh_n's: "closed_segment (g ((1 + n) / N)) (h ((1 + n) / N)) ⊆ s"
            using ‹N>0› Suc.prems ee pi t
            by (auto simp: Path_Connected.path_image_join field_simps)
          have pi_subset_ball:
                "path_image (subpath (n/N) ((1+n) / N) g +++ linepath (g ((1+n) / N)) (h ((1+n) / N)) +++
                             subpath ((1+n) / N) (n/N) h +++ linepath (h (n/N)) (g (n/N)))
                 ⊆ ball (p t) (ee (p t))"
            apply (intro subset_path_image_join pi_hgn pi_ghn')
            using ‹N>0› Suc.prems
            apply (auto simp: path_image_subpath dist_norm field_simps closed_segment_eq_real_ivl ptgh_ee)
            done
          have pi0: "(f has_contour_integral 0)
                       (subpath (n/ N) ((Suc n)/N) g +++ linepath(g ((Suc n) / N)) (h((Suc n) / N)) +++
                        subpath ((Suc n) / N) (n/N) h +++ linepath(h (n/N)) (g (n/N)))"
            apply (rule Cauchy_theorem_primitive [of "ball(p t) (ee(p t))" "ff (p t)" "f"])
            apply (metis ff open_ball at_within_open pi t)
            apply (intro valid_path_join)
            using Suc.prems pi_subset_ball apply (simp_all add: valid_path_subpath g h)
            done
          have fpa1: "f contour_integrable_on subpath (real n / real N) (real (Suc n) / real N) g"
            using Suc.prems by (simp add: contour_integrable_subpath g fpa)
          have fpa2: "f contour_integrable_on linepath (g (real (Suc n) / real N)) (h (real (Suc n) / real N))"
            using gh_n's
            by (auto intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
          have fpa3: "f contour_integrable_on linepath (h (real n / real N)) (g (real n / real N))"
            using gh_ns
            by (auto simp: closed_segment_commute intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
          have eq0: "contour_integral (subpath (n/N) ((Suc n) / real N) g) f +
                     contour_integral (linepath (g ((Suc n) / N)) (h ((Suc n) / N))) f +
                     contour_integral (subpath ((Suc n) / N) (n/N) h) f +
                     contour_integral (linepath (h (n/N)) (g (n/N))) f = 0"
            using contour_integral_unique [OF pi0] Suc.prems
            by (simp add: g h fpa valid_path_subpath contour_integrable_subpath
                          fpa1 fpa2 fpa3 algebra_simps del: of_nat_Suc)
          have *: "⋀hn he hn' gn gd gn' hgn ghn gh0 ghn'.
                    ⟦hn - gn = ghn - gh0;
                     gd + ghn' + he + hgn = (0::complex);
                     hn - he = hn'; gn + gd = gn'; hgn = -ghn⟧ ⟹ hn' - gn' = ghn' - gh0"
            by (auto simp: algebra_simps)
          have "contour_integral (subpath 0 (n/N) h) f - contour_integral (subpath ((Suc n) / N) (n/N) h) f =
                contour_integral (subpath 0 (n/N) h) f + contour_integral (subpath (n/N) ((Suc n) / N) h) f"
            unfolding reversepath_subpath [symmetric, of "((Suc n) / N)"]
            using Suc.prems by (simp add: h fpa contour_integral_reversepath valid_path_subpath contour_integrable_subpath)
          also have "... = contour_integral (subpath 0 ((Suc n) / N) h) f"
            using Suc.prems by (simp add: contour_integral_subpath_combine h fpa)
          finally have pi0_eq:
               "contour_integral (subpath 0 (n/N) h) f - contour_integral (subpath ((Suc n) / N) (n/N) h) f =
                contour_integral (subpath 0 ((Suc n) / N) h) f" .
          show ?case
            apply (rule * [OF Suc.hyps eq0 pi0_eq])
            using Suc.prems
            apply (simp_all add: g h fpa contour_integral_subpath_combine
                     contour_integral_reversepath [symmetric] contour_integrable_continuous_linepath
                     continuous_on_subset [OF contf gh_ns])
            done
      qed
      } note ind = this
      have "contour_integral h f = contour_integral g f"
        using ind [OF order_refl] N joins
        by (simp add: linked_paths_def pathstart_def pathfinish_def split: if_split_asm)
    }
    ultimately
    have "path_image g ⊆ s ∧ path_image h ⊆ s ∧ (∀f. f holomorphic_on s ⟶ contour_integral h f = contour_integral g f)"
      by metis
  } note * = this
  show ?thesis
    apply (rule_tac x="e/3" in exI)
    apply (rule conjI)
    using enz apply simp
    apply (clarsimp simp only: ball_conj_distrib)
    apply (rule *; assumption)
    done
qed


lemma
  assumes "open s" "path p" "path_image p ⊆ s"
    shows contour_integral_nearby_ends:
      "∃d. 0 < d ∧
              (∀g h. valid_path g ∧ valid_path h ∧
                    (∀t ∈ {0..1}. norm(g t - p t) < d ∧ norm(h t - p t) < d) ∧
                    pathstart h = pathstart g ∧ pathfinish h = pathfinish g
                    ⟶ path_image g ⊆ s ∧
                        path_image h ⊆ s ∧
                        (∀f. f holomorphic_on s
                            ⟶ contour_integral h f = contour_integral g f))"
    and contour_integral_nearby_loops:
      "∃d. 0 < d ∧
              (∀g h. valid_path g ∧ valid_path h ∧
                    (∀t ∈ {0..1}. norm(g t - p t) < d ∧ norm(h t - p t) < d) ∧
                    pathfinish g = pathstart g ∧ pathfinish h = pathstart h
                    ⟶ path_image g ⊆ s ∧
                        path_image h ⊆ s ∧
                        (∀f. f holomorphic_on s
                            ⟶ contour_integral h f = contour_integral g f))"
  using contour_integral_nearby [OF assms, where atends=True]
  using contour_integral_nearby [OF assms, where atends=False]
  unfolding linked_paths_def by simp_all

corollary differentiable_polynomial_function:
  fixes p :: "real ⇒ 'a::euclidean_space"
  shows "polynomial_function p ⟹ p differentiable_on s"
by (meson has_vector_derivative_polynomial_function differentiable_at_imp_differentiable_on differentiable_def has_vector_derivative_def)

lemma C1_differentiable_polynomial_function:
  fixes p :: "real ⇒ 'a::euclidean_space"
  shows "polynomial_function p ⟹ p C1_differentiable_on s"
  by (metis continuous_on_polymonial_function C1_differentiable_on_def  has_vector_derivative_polynomial_function)

lemma valid_path_polynomial_function:
  fixes p :: "real ⇒ 'a::euclidean_space"
  shows "polynomial_function p ⟹ valid_path p"
by (force simp: valid_path_def piecewise_C1_differentiable_on_def continuous_on_polymonial_function C1_differentiable_polynomial_function)

lemma valid_path_subpath_trivial [simp]:
    fixes g :: "real ⇒ 'a::euclidean_space"
    shows "z ≠ g x ⟹ valid_path (subpath x x g)"
  by (simp add: subpath_def valid_path_polynomial_function)

lemma contour_integral_bound_exists:
assumes s: "open s"
    and g: "valid_path g"
    and pag: "path_image g ⊆ s"
  shows "∃L. 0 < L ∧
       (∀f B. f holomorphic_on s ∧ (∀z ∈ s. norm(f z) ≤ B)
         ⟶ norm(contour_integral g f) ≤ L*B)"
proof -
have "path g" using g
  by (simp add: valid_path_imp_path)
then obtain d::real and p
  where d: "0 < d"
    and p: "polynomial_function p" "path_image p ⊆ s"
    and pi: "⋀f. f holomorphic_on s ⟹ contour_integral g f = contour_integral p f"
  using contour_integral_nearby_ends [OF s ‹path g› pag]
  apply clarify
  apply (drule_tac x=g in spec)
  apply (simp only: assms)
  apply (force simp: valid_path_polynomial_function dest: path_approx_polynomial_function)
  done
then obtain p' where p': "polynomial_function p'"
         "⋀x. (p has_vector_derivative (p' x)) (at x)"
  using has_vector_derivative_polynomial_function by force
then have "bounded(p' ` {0..1})"
  using continuous_on_polymonial_function
  by (force simp: intro!: compact_imp_bounded compact_continuous_image)
then obtain L where L: "L>0" and nop': "⋀x. x ∈ {0..1} ⟹ norm (p' x) ≤ L"
  by (force simp: bounded_pos)
{ fix f B
  assume f: "f holomorphic_on s"
     and B: "⋀z. z∈s ⟹ cmod (f z) ≤ B"
  then have "f contour_integrable_on p ∧ valid_path p"
    using p s
    by (blast intro: valid_path_polynomial_function contour_integrable_holomorphic_simple holomorphic_on_imp_continuous_on)
  moreover have "⋀x. x ∈ {0..1} ⟹ cmod (vector_derivative p (at x)) * cmod (f (p x)) ≤ L * B"
    apply (rule mult_mono)
    apply (subst Derivative.vector_derivative_at; force intro: p' nop')
    using L B p
    apply (auto simp: path_image_def image_subset_iff)
    done
  ultimately have "cmod (contour_integral g f) ≤ L * B"
    apply (simp add: pi [OF f])
    apply (simp add: contour_integral_integral)
    apply (rule order_trans [OF integral_norm_bound_integral])
    apply (auto simp: mult.commute integral_norm_bound_integral contour_integrable_on [symmetric] norm_mult)
    done
} then
show ?thesis
  by (force simp: L contour_integral_integral)
qed

subsection‹Constancy of a function from a connected set into a finite, disconnected or discrete set›

text‹Still missing: versions for a set that is smaller than R, or countable.›

lemma continuous_disconnected_range_constant:
  assumes s: "connected s"
      and conf: "continuous_on s f"
      and fim: "f ` s ⊆ t"
      and cct: "⋀y. y ∈ t ⟹ connected_component_set t y = {y}"
    shows "∃a. ∀x ∈ s. f x = a"
proof (cases "s = {}")
  case True then show ?thesis by force
next
  case False
  { fix x assume "x ∈ s"
    then have "f ` s ⊆ {f x}"
    by (metis connected_continuous_image conf connected_component_maximal fim image_subset_iff rev_image_eqI s cct)
  }
  with False show ?thesis
    by blast
qed

lemma discrete_subset_disconnected:
  fixes s :: "'a::topological_space set"
  fixes t :: "'b::real_normed_vector set"
  assumes conf: "continuous_on s f"
      and no: "⋀x. x ∈ s ⟹ ∃e>0. ∀y. y ∈ s ∧ f y ≠ f x ⟶ e ≤ norm (f y - f x)"
   shows "f ` s ⊆ {y. connected_component_set (f ` s) y = {y}}"
proof -
  { fix x assume x: "x ∈ s"
    then obtain e where "e>0" and ele: "⋀y. ⟦y ∈ s; f y ≠ f x⟧ ⟹ e ≤ norm (f y - f x)"
      using conf no [OF x] by auto
    then have e2: "0 ≤ e / 2"
      by simp
    have "f y = f x" if "y ∈ s" and ccs: "f y ∈ connected_component_set (f ` s) (f x)" for y
      apply (rule ccontr)
      using connected_closed [of "connected_component_set (f ` s) (f x)"] ‹e>0›
      apply (simp add: del: ex_simps)
      apply (drule spec [where x="cball (f x) (e / 2)"])
      apply (drule spec [where x="- ball(f x) e"])
      apply (auto simp: dist_norm open_closed [symmetric] simp del: le_divide_eq_numeral1 dest!: connected_component_in)
        apply (metis diff_self e2 ele norm_minus_commute norm_zero not_less)
       using centre_in_cball connected_component_refl_eq e2 x apply blast
      using ccs
      apply (force simp: cball_def dist_norm norm_minus_commute dest: ele [OF ‹y ∈ s›])
      done
    moreover have "connected_component_set (f ` s) (f x) ⊆ f ` s"
      by (auto simp: connected_component_in)
    ultimately have "connected_component_set (f ` s) (f x) = {f x}"
      by (auto simp: x)
  }
  with assms show ?thesis
    by blast
qed

lemma finite_implies_discrete:
  fixes s :: "'a::topological_space set"
  assumes "finite (f ` s)"
  shows "(∀x ∈ s. ∃e>0. ∀y. y ∈ s ∧ f y ≠ f x ⟶ e ≤ norm (f y - f x))"
proof -
  have "∃e>0. ∀y. y ∈ s ∧ f y ≠ f x ⟶ e ≤ norm (f y - f x)" if "x ∈ s" for x
  proof (cases "f ` s - {f x} = {}")
    case True
    with zero_less_numeral show ?thesis
      by (fastforce simp add: Set.image_subset_iff cong: conj_cong)
  next
    case False
    then obtain z where z: "z ∈ s" "f z ≠ f x"
      by blast
    have finn: "finite {norm (z - f x) |z. z ∈ f ` s - {f x}}"
      using assms by simp
    then have *: "0 < Inf{norm(z - f x) | z. z ∈ f ` s - {f x}}"
      apply (rule finite_imp_less_Inf)
      using z apply force+
      done
    show ?thesis
      by (force intro!: * cInf_le_finite [OF finn])
  qed
  with assms show ?thesis
    by blast
qed

text‹This proof requires the existence of two separate values of the range type.›
lemma finite_range_constant_imp_connected:
  assumes "⋀f::'a::topological_space ⇒ 'b::real_normed_algebra_1.
              ⟦continuous_on s f; finite(f ` s)⟧ ⟹ ∃a. ∀x ∈ s. f x = a"
    shows "connected s"
proof -
  { fix t u
    assume clt: "closedin (subtopology euclidean s) t"
       and clu: "closedin (subtopology euclidean s) u"
       and tue: "t ∩ u = {}" and tus: "t ∪ u = s"
    have conif: "continuous_on s (λx. if x ∈ t then 0 else 1)"
      apply (subst tus [symmetric])
      apply (rule continuous_on_cases_local)
      using clt clu tue
      apply (auto simp: tus continuous_on_const)
      done
    have fi: "finite ((λx. if x ∈ t then 0 else 1) ` s)"
      by (rule finite_subset [of _ "{0,1}"]) auto
    have "t = {} ∨ u = {}"
      using assms [OF conif fi] tus [symmetric]
      by (auto simp: Ball_def) (metis IntI empty_iff one_neq_zero tue)
  }
  then show ?thesis
    by (simp add: connected_closedin_eq)
qed

lemma continuous_disconnected_range_constant_eq:
      "(connected s ⟷
           (∀f::'a::topological_space ⇒ 'b::real_normed_algebra_1.
            ∀t. continuous_on s f ∧ f ` s ⊆ t ∧ (∀y ∈ t. connected_component_set t y = {y})
            ⟶ (∃a::'b. ∀x ∈ s. f x = a)))" (is ?thesis1)
  and continuous_discrete_range_constant_eq:
      "(connected s ⟷
         (∀f::'a::topological_space ⇒ 'b::real_normed_algebra_1.
          continuous_on s f ∧
          (∀x ∈ s. ∃e. 0 < e ∧ (∀y. y ∈ s ∧ (f y ≠ f x) ⟶ e ≤ norm(f y - f x)))
          ⟶ (∃a::'b. ∀x ∈ s. f x = a)))" (is ?thesis2)
  and continuous_finite_range_constant_eq:
      "(connected s ⟷
         (∀f::'a::topological_space ⇒ 'b::real_normed_algebra_1.
          continuous_on s f ∧ finite (f ` s)
          ⟶ (∃a::'b. ∀x ∈ s. f x = a)))" (is ?thesis3)
proof -
  have *: "⋀s t u v. ⟦s ⟹ t; t ⟹ u; u ⟹ v; v ⟹ s⟧
    ⟹ (s ⟷ t) ∧ (s ⟷ u) ∧ (s ⟷ v)"
    by blast
  have "?thesis1 ∧ ?thesis2 ∧ ?thesis3"
    apply (rule *)
    using continuous_disconnected_range_constant apply metis
    apply clarify
    apply (frule discrete_subset_disconnected; blast)
    apply (blast dest: finite_implies_discrete)
    apply (blast intro!: finite_range_constant_imp_connected)
    done
  then show ?thesis1 ?thesis2 ?thesis3
    by blast+
qed

lemma continuous_discrete_range_constant:
  fixes f :: "'a::topological_space ⇒ 'b::real_normed_algebra_1"
  assumes s: "connected s"
      and "continuous_on s f"
      and "⋀x. x ∈ s ⟹ ∃e>0. ∀y. y ∈ s ∧ f y ≠ f x ⟶ e ≤ norm (f y - f x)"
    shows "∃a. ∀x ∈ s. f x = a"
  using continuous_discrete_range_constant_eq [THEN iffD1, OF s] assms
  by blast

lemma continuous_finite_range_constant:
  fixes f :: "'a::topological_space ⇒ 'b::real_normed_algebra_1"
  assumes "connected s"
      and "continuous_on s f"
      and "finite (f ` s)"
    shows "∃a. ∀x ∈ s. f x = a"
  using assms continuous_finite_range_constant_eq
  by blast


text‹We can treat even non-rectifiable paths as having a "length" for bounds on analytic functions in open sets.›

subsection‹Winding Numbers›

definition winding_number:: "[real ⇒ complex, complex] ⇒ complex" where
  "winding_number γ z ≡
    @n. ∀e > 0. ∃p. valid_path p ∧ z ∉ path_image p ∧
                    pathstart p = pathstart γ ∧
                    pathfinish p = pathfinish γ ∧
                    (∀t ∈ {0..1}. norm(γ t - p t) < e) ∧
                    contour_integral p (λw. 1/(w - z)) = 2 * pi * ii * n"

lemma winding_number:
  assumes "path γ" "z ∉ path_image γ" "0 < e"
    shows "∃p. valid_path p ∧ z ∉ path_image p ∧
               pathstart p = pathstart γ ∧
               pathfinish p = pathfinish γ ∧
               (∀t ∈ {0..1}. norm (γ t - p t) < e) ∧
               contour_integral p (λw. 1/(w - z)) = 2 * pi * ii * winding_number γ z"
proof -
  have "path_image γ ⊆ UNIV - {z}"
    using assms by blast
  then obtain d
    where d: "d>0"
      and pi_eq: "⋀h1 h2. valid_path h1 ∧ valid_path h2 ∧
                    (∀t∈{0..1}. cmod (h1 t - γ t) < d ∧ cmod (h2 t - γ t) < d) ∧
                    pathstart h2 = pathstart h1 ∧ pathfinish h2 = pathfinish h1 ⟶
                      path_image h1 ⊆ UNIV - {z} ∧ path_image h2 ⊆ UNIV - {z} ∧
                      (∀f. f holomorphic_on UNIV - {z} ⟶ contour_integral h2 f = contour_integral h1 f)"
    using contour_integral_nearby_ends [of "UNIV - {z}" γ] assms by (auto simp: open_delete)
  then obtain h where h: "polynomial_function h ∧ pathstart h = pathstart γ ∧ pathfinish h = pathfinish γ ∧
                          (∀t ∈ {0..1}. norm(h t - γ t) < d/2)"
    using path_approx_polynomial_function [OF ‹path γ›, of "d/2"] d by auto
  def nn  "1/(2* pi*ii) * contour_integral h (λw. 1/(w - z))"
  have "∃n. ∀e > 0. ∃p. valid_path p ∧ z ∉ path_image p ∧
                        pathstart p = pathstart γ ∧  pathfinish p = pathfinish γ ∧
                        (∀t ∈ {0..1}. norm(γ t - p t) < e) ∧
                        contour_integral p (λw. 1/(w - z)) = 2 * pi * ii * n"
                    (is "∃n. ∀e > 0. ?PP e n")
    proof (rule_tac x=nn in exI, clarify)
      fix e::real
      assume e: "e>0"
      obtain p where p: "polynomial_function p ∧
            pathstart p = pathstart γ ∧ pathfinish p = pathfinish γ ∧ (∀t∈{0..1}. cmod (p t - γ t) < min e (d / 2))"
        using path_approx_polynomial_function [OF ‹path γ›, of "min e (d/2)"] d ‹0<e› by auto
      have "(λw. 1 / (w - z)) holomorphic_on UNIV - {z}"
        by (auto simp: intro!: holomorphic_intros)
      then show "?PP e nn"
        apply (rule_tac x=p in exI)
        using pi_eq [of h p] h p d
        apply (auto simp: valid_path_polynomial_function norm_minus_commute nn_def)
        done
    qed
  then show ?thesis
    unfolding winding_number_def
    apply (rule someI2_ex)
    apply (blast intro: ‹0<e›)
    done
qed

lemma winding_number_unique:
  assumes γ: "path γ" "z ∉ path_image γ"
      and pi:
        "⋀e. e>0 ⟹ ∃p. valid_path p ∧ z ∉ path_image p ∧
                          pathstart p = pathstart γ ∧ pathfinish p = pathfinish γ ∧
                          (∀t ∈ {0..1}. norm (γ t - p t) < e) ∧
                          contour_integral p (λw. 1/(w - z)) = 2 * pi * ii * n"
   shows "winding_number γ z = n"
proof -
  have "path_image γ ⊆ UNIV - {z}"
    using assms by blast
  then obtain e
    where e: "e>0"
      and pi_eq: "⋀h1 h2 f. ⟦valid_path h1; valid_path h2;
                    (∀t∈{0..1}. cmod (h1 t - γ t) < e ∧ cmod (h2 t - γ t) < e);
                    pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1; f holomorphic_on UNIV - {z}⟧ ⟹
                    contour_integral h2 f = contour_integral h1 f"
    using contour_integral_nearby_ends [of "UNIV - {z}" γ] assms  by (auto simp: open_delete)
  obtain p where p:
     "valid_path p ∧ z ∉ path_image p ∧
      pathstart p = pathstart γ ∧ pathfinish p = pathfinish γ ∧
      (∀t ∈ {0..1}. norm (γ t - p t) < e) ∧
      contour_integral p (λw. 1/(w - z)) = 2 * pi * ii * n"
    using pi [OF e] by blast
  obtain q where q:
     "valid_path q ∧ z ∉ path_image q ∧
      pathstart q = pathstart γ ∧ pathfinish q = pathfinish γ ∧
      (∀t∈{0..1}. cmod (γ t - q t) < e) ∧ contour_integral q (λw. 1 / (w - z)) = complex_of_real (2 * pi) * 𝗂 * winding_number γ z"
    using winding_number [OF γ e] by blast
  have "2 * complex_of_real pi * 𝗂 * n = contour_integral p (λw. 1 / (w - z))"
    using p by auto
  also have "... = contour_integral q (λw. 1 / (w - z))"
    apply (rule pi_eq)
    using p q
    by (auto simp: valid_path_polynomial_function norm_minus_commute intro!: holomorphic_intros)
  also have "... = 2 * complex_of_real pi * 𝗂 * winding_number γ z"
    using q by auto
  finally have "2 * complex_of_real pi * 𝗂 * n = 2 * complex_of_real pi * 𝗂 * winding_number γ z" .
  then show ?thesis
    by simp
qed

lemma winding_number_unique_loop:
  assumes γ: "path γ" "z ∉ path_image γ"
      and loop: "pathfinish γ = pathstart γ"
      and pi:
        "⋀e. e>0 ⟹ ∃p. valid_path p ∧ z ∉ path_image p ∧
                           pathfinish p = pathstart p ∧
                           (∀t ∈ {0..1}. norm (γ t - p t) < e) ∧
                           contour_integral p (λw. 1/(w - z)) = 2 * pi * ii * n"
   shows "winding_number γ z = n"
proof -
  have "path_image γ ⊆ UNIV - {z}"
    using assms by blast
  then obtain e
    where e: "e>0"
      and pi_eq: "⋀h1 h2 f. ⟦valid_path h1; valid_path h2;
                    (∀t∈{0..1}. cmod (h1 t - γ t) < e ∧ cmod (h2 t - γ t) < e);
                    pathfinish h1 = pathstart h1; pathfinish h2 = pathstart h2; f holomorphic_on UNIV - {z}⟧ ⟹
                    contour_integral h2 f = contour_integral h1 f"
    using contour_integral_nearby_loops [of "UNIV - {z}" γ] assms  by (auto simp: open_delete)
  obtain p where p:
     "valid_path p ∧ z ∉ path_image p ∧
      pathfinish p = pathstart p ∧
      (∀t ∈ {0..1}. norm (γ t - p t) < e) ∧
      contour_integral p (λw. 1/(w - z)) = 2 * pi * ii * n"
    using pi [OF e] by blast
  obtain q where q:
     "valid_path q ∧ z ∉ path_image q ∧
      pathstart q = pathstart γ ∧ pathfinish q = pathfinish γ ∧
      (∀t∈{0..1}. cmod (γ t - q t) < e) ∧ contour_integral q (λw. 1 / (w - z)) = complex_of_real (2 * pi) * 𝗂 * winding_number γ z"
    using winding_number [OF γ e] by blast
  have "2 * complex_of_real pi * 𝗂 * n = contour_integral p (λw. 1 / (w - z))"
    using p by auto
  also have "... = contour_integral q (λw. 1 / (w - z))"
    apply (rule pi_eq)
    using p q loop
    by (auto simp: valid_path_polynomial_function norm_minus_commute intro!: holomorphic_intros)
  also have "... = 2 * complex_of_real pi * 𝗂 * winding_number γ z"
    using q by auto
  finally have "2 * complex_of_real pi * 𝗂 * n = 2 * complex_of_real pi * 𝗂 * winding_number γ z" .
  then show ?thesis
    by simp
qed

lemma winding_number_valid_path:
  assumes "valid_path γ" "z ∉ path_image γ"
    shows "winding_number γ z = 1/(2*pi*ii) * contour_integral γ (λw. 1/(w - z))"
  using assms by (auto simp: valid_path_imp_path intro!: winding_number_unique)

lemma has_contour_integral_winding_number:
  assumes γ: "valid_path γ" "z ∉ path_image γ"
    shows "((λw. 1/(w - z)) has_contour_integral (2*pi*ii*winding_number γ z)) γ"
by (simp add: winding_number_valid_path has_contour_integral_integral contour_integrable_inversediff assms)

lemma winding_number_trivial [simp]: "z ≠ a ⟹ winding_number(linepath a a) z = 0"
  by (simp add: winding_number_valid_path)

lemma winding_number_subpath_trivial [simp]: "z ≠ g x ⟹ winding_number (subpath x x g) z = 0"
  by (simp add: path_image_subpath winding_number_valid_path)

lemma winding_number_join:
  assumes g1: "path g1" "z ∉ path_image g1"
      and g2: "path g2" "z ∉ path_image g2"
      and "pathfinish g1 = pathstart g2"
    shows "winding_number(g1 +++ g2) z = winding_number g1 z + winding_number g2 z"
  apply (rule winding_number_unique)
  using assms apply (simp_all add: not_in_path_image_join)
  apply (frule winding_number [OF g2])
  apply (frule winding_number [OF g1], clarify)
  apply (rename_tac p2 p1)
  apply (rule_tac x="p1+++p2" in exI)
  apply (simp add: not_in_path_image_join contour_integrable_inversediff algebra_simps)
  apply (auto simp: joinpaths_def)
  done

lemma winding_number_reversepath:
  assumes "path γ" "z ∉ path_image γ"
    shows "winding_number(reversepath γ) z = - (winding_number γ z)"
  apply (rule winding_number_unique)
  using assms
  apply simp_all
  apply (frule winding_number [OF assms], clarify)
  apply (rule_tac x="reversepath p" in exI)
  apply (simp add: contour_integral_reversepath contour_integrable_inversediff valid_path_imp_reverse)
  apply (auto simp: reversepath_def)
  done

lemma winding_number_shiftpath:
  assumes γ: "path γ" "z ∉ path_image γ"
      and "pathfinish γ = pathstart γ" "a ∈ {0..1}"
    shows "winding_number(shiftpath a γ) z = winding_number γ z"
  apply (rule winding_number_unique_loop)
  using assms
  apply (simp_all add: path_shiftpath path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath)
  apply (frule winding_number [OF γ], clarify)
  apply (rule_tac x="shiftpath a p" in exI)
  apply (simp add: contour_integral_shiftpath path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath valid_path_shiftpath)
  apply (auto simp: shiftpath_def)
  done

lemma winding_number_split_linepath:
  assumes "c ∈ closed_segment a b" "z ∉ closed_segment a b"
    shows "winding_number(linepath a b) z = winding_number(linepath a c) z + winding_number(linepath c b) z"
proof -
  have "z ∉ closed_segment a c" "z ∉ closed_segment c b"
    using assms  apply (meson convex_contains_segment convex_segment ends_in_segment(1) subsetCE)
    using assms  by (meson convex_contains_segment convex_segment ends_in_segment(2) subsetCE)
  then show ?thesis
    using assms
    by (simp add: winding_number_valid_path contour_integral_split_linepath [symmetric] continuous_on_inversediff field_simps)
qed

lemma winding_number_cong:
   "(⋀t. ⟦0 ≤ t; t ≤ 1⟧ ⟹ p t = q t) ⟹ winding_number p z = winding_number q z"
  by (simp add: winding_number_def pathstart_def pathfinish_def)

lemma winding_number_offset: "winding_number p z = winding_number (λw. p w - z) 0"
  apply (simp add: winding_number_def contour_integral_integral path_image_def valid_path_def pathstart_def pathfinish_def)
  apply (intro ext arg_cong [where f = Eps] arg_cong [where f = All] imp_cong refl, safe)
  apply (rename_tac g)
  apply (rule_tac x="λt. g t - z" in exI)
  apply (force simp: vector_derivative_def has_vector_derivative_diff_const piecewise_C1_differentiable_diff C1_differentiable_imp_piecewise)
  apply (rename_tac g)
  apply (rule_tac x="λt. g t + z" in exI)
  apply (simp add: piecewise_C1_differentiable_add vector_derivative_def has_vector_derivative_add_const C1_differentiable_imp_piecewise)
  apply (force simp: algebra_simps)
  done

(* A combined theorem deducing several things piecewise.*)
lemma winding_number_join_pos_combined:
     "⟦valid_path γ1; z ∉ path_image γ1; 0 < Re(winding_number γ1 z);
       valid_path γ2; z ∉ path_image γ2; 0 < Re(winding_number γ2 z); pathfinish γ1 = pathstart γ2⟧
      ⟹ valid_path(γ1 +++ γ2) ∧ z ∉ path_image(γ1 +++ γ2) ∧ 0 < Re(winding_number(γ1 +++ γ2) z)"
  by (simp add: valid_path_join path_image_join winding_number_join valid_path_imp_path)


(* Useful sufficient conditions for the winding number to be positive etc.*)

lemma Re_winding_number:
    "⟦valid_path γ; z ∉ path_image γ⟧
     ⟹ Re(winding_number γ z) = Im(contour_integral γ (λw. 1/(w - z))) / (2*pi)"
by (simp add: winding_number_valid_path field_simps Re_divide power2_eq_square)

lemma winding_number_pos_le:
  assumes γ: "valid_path γ" "z ∉ path_image γ"
      and ge: "⋀x. ⟦0 < x; x < 1⟧ ⟹ 0 ≤ Im (vector_derivative γ (at x) * cnj(γ x - z))"
    shows "0 ≤ Re(winding_number γ z)"
proof -
  have *: "0 ≤ Im (vector_derivative γ (at x) / (γ x - z))" if x: "0 < x" "x < 1" for x
    using ge by (simp add: Complex.Im_divide algebra_simps x)
  show ?thesis
    apply (simp add: Re_winding_number [OF γ] field_simps)
    apply (rule has_integral_component_nonneg
             [of ii "λx. if x ∈ {0<..<1}
                         then 1/(γ x - z) * vector_derivative γ (at x) else 0", simplified])
      prefer 3 apply (force simp: *)
     apply (simp add: Basis_complex_def)
    apply (rule has_integral_spike_interior [of 0 1 _ "λx. 1/(γ x - z) * vector_derivative γ (at x)"])
    apply simp
    apply (simp only: box_real)
    apply (subst has_contour_integral [symmetric])
    using γ
    apply (simp add: contour_integrable_inversediff has_contour_integral_integral)
    done
qed

lemma winding_number_pos_lt_lemma:
  assumes γ: "valid_path γ" "z ∉ path_image γ"
      and e: "0 < e"
      and ge: "⋀x. ⟦0 < x; x < 1⟧ ⟹ e ≤ Im (vector_derivative γ (at x) / (γ x - z))"
    shows "0 < Re(winding_number γ z)"
proof -
  have "e ≤ Im (contour_integral γ (λw. 1 / (w - z)))"
    apply (rule has_integral_component_le
             [of ii "λx. ii*e" "ii*e" "{0..1}"
                    "λx. if x ∈ {0<..<1} then 1/(γ x - z) * vector_derivative γ (at x) else ii*e"
                    "contour_integral γ (λw. 1/(w - z))", simplified])
    using e
    apply (simp_all add: Basis_complex_def)
    using has_integral_const_real [of _ 0 1] apply force
    apply (rule has_integral_spike_interior [of 0 1 _ "λx. 1/(γ x - z) * vector_derivative γ (at x)", simplified box_real])
    apply simp
    apply (subst has_contour_integral [symmetric])
    using γ
    apply (simp_all add: contour_integrable_inversediff has_contour_integral_integral ge)
    done
  with e show ?thesis
    by (simp add: Re_winding_number [OF γ] field_simps)
qed

lemma winding_number_pos_lt:
  assumes γ: "valid_path γ" "z ∉ path_image γ"
      and e: "0 < e"
      and ge: "⋀x. ⟦0 < x; x < 1⟧ ⟹ e ≤ Im (vector_derivative γ (at x) * cnj(γ x - z))"
    shows "0 < Re (winding_number γ z)"
proof -
  have bm: "bounded ((λw. w - z) ` (path_image γ))"
    using bounded_translation [of _ "-z"] γ by (simp add: bounded_valid_path_image)
  then obtain B where B: "B > 0" and Bno: "⋀x. x ∈ (λw. w - z) ` (path_image γ) ⟹ norm x ≤ B"
    using bounded_pos [THEN iffD1, OF bm] by blast
  { fix x::real  assume x: "0 < x" "x < 1"
    then have B2: "cmod (γ x - z)^2 ≤ B^2" using Bno [of "γ x - z"]
      by (simp add: path_image_def power2_eq_square mult_mono')
    with x have "γ x ≠ z" using γ
      using path_image_def by fastforce
    then have "e / B2 ≤ Im (vector_derivative γ (at x) * cnj (γ x - z)) / (cmod (γ x - z))2"
      using B ge [OF x] B2 e
      apply (rule_tac y="e / (cmod (γ x - z))2" in order_trans)
      apply (auto simp: divide_left_mono divide_right_mono)
      done
    then have "e / B2 ≤ Im (vector_derivative γ (at x) / (γ x - z))"
      by (simp add: Im_divide_Reals complex_div_cnj [of _ "γ x - z" for x] del: complex_cnj_diff times_complex.sel)
  } note * = this
  show ?thesis
    using e B by (simp add: * winding_number_pos_lt_lemma [OF γ, of "e/B^2"])
qed

subsection‹The winding number is an integer›

text‹Proof from the book Complex Analysis by Lars V. Ahlfors, Chapter 4, section 2.1,
     Also on page 134 of Serge Lang's book with the name title, etc.›

lemma exp_fg:
  fixes z::complex
  assumes g: "(g has_vector_derivative g') (at x within s)"
      and f: "(f has_vector_derivative (g' / (g x - z))) (at x within s)"
      and z: "g x ≠ z"
    shows "((λx. exp(-f x) * (g x - z)) has_vector_derivative 0) (at x within s)"
proof -
  have *: "(exp o (λx. (- f x)) has_vector_derivative - (g' / (g x - z)) * exp (- f x)) (at x within s)"
    using assms unfolding has_vector_derivative_def scaleR_conv_of_real
    by (auto intro!: derivative_eq_intros)
  show ?thesis
    apply (rule has_vector_derivative_eq_rhs)
    apply (rule bounded_bilinear.has_vector_derivative [OF bounded_bilinear_mult])
    using z
    apply (auto simp: intro!: derivative_eq_intros * [unfolded o_def] g)
    done
qed

lemma winding_number_exp_integral:
  fixes z::complex
  assumes γ: "γ piecewise_C1_differentiable_on {a..b}"
      and ab: "a ≤ b"
      and z: "z ∉ γ ` {a..b}"
    shows "(λx. vector_derivative γ (at x) / (γ x - z)) integrable_on {a..b}"
          (is "?thesis1")
          "exp (- (integral {a..b} (λx. vector_derivative γ (at x) / (γ x - z)))) * (γ b - z) = γ a - z"
          (is "?thesis2")
proof -
  let ?Dγ = "λx. vector_derivative γ (at x)"
  have [simp]: "⋀x. a ≤ x ⟹ x ≤ b ⟹ γ x ≠ z"
    using z by force
  have cong: "continuous_on {a..b} γ"
    using γ by (simp add: piecewise_C1_differentiable_on_def)
  obtain k where fink: "finite k" and g_C1_diff: "γ C1_differentiable_on ({a..b} - k)"
    using γ by (force simp: piecewise_C1_differentiable_on_def)
  have o: "open ({a<..<b} - k)"
    using ‹finite k› by (simp add: finite_imp_closed open_Diff)
  moreover have "{a<..<b} - k ⊆ {a..b} - k"
    by force
  ultimately have g_diff_at: "⋀x. ⟦x ∉ k; x ∈ {a<..<b}⟧ ⟹ γ differentiable at x"
    by (metis Diff_iff differentiable_on_subset C1_diff_imp_diff [OF g_C1_diff] differentiable_on_def differentiable_within_open)
  { fix w
    assume "w ≠ z"
    have "continuous_on (ball w (cmod (w - z))) (λw. 1 / (w - z))"
      by (auto simp: dist_norm intro!: continuous_intros)
    moreover have "⋀x. cmod (w - x) < cmod (w - z) ⟹ ∃f'. ((λw. 1 / (w - z)) has_field_derivative f') (at x)"
      by (auto simp: intro!: derivative_eq_intros)
    ultimately have "∃h. ∀y. norm(y - w) < norm(w - z) ⟶ (h has_field_derivative 1/(y - z)) (at y)"
      using holomorphic_convex_primitive [of "ball w (norm(w - z))" "{}" "λw. 1/(w - z)"]
      by (simp add: field_differentiable_def Ball_def dist_norm at_within_open_NO_MATCH norm_minus_commute)
  }
  then obtain h where h: "⋀w y. w ≠ z ⟹ norm(y - w) < norm(w - z) ⟹ (h w has_field_derivative 1/(y - z)) (at y)"
    by meson
  have exy: "∃y. ((λx. inverse (γ x - z) * ?Dγ x) has_integral y) {a..b}"
    unfolding integrable_on_def [symmetric]
    apply (rule contour_integral_local_primitive_any [OF piecewise_C1_imp_differentiable [OF γ], of "-{z}"])
    apply (rename_tac w)
    apply (rule_tac x="norm(w - z)" in exI)
    apply (simp_all add: inverse_eq_divide)
    apply (metis has_field_derivative_at_within h)
    done
  have vg_int: "(λx. ?Dγ x / (γ x - z)) integrable_on {a..b}"
    unfolding box_real [symmetric] divide_inverse_commute
    by (auto intro!: exy integrable_subinterval simp add: integrable_on_def ab)
  with ab show ?thesis1
    by (simp add: divide_inverse_commute integral_def integrable_on_def)
  { fix t
    assume t: "t ∈ {a..b}"
    have cball: "continuous_on (ball (γ t) (dist (γ t) z)) (λx. inverse (x - z))"
        using z by (auto intro!: continuous_intros simp: dist_norm)
    have icd: "⋀x. cmod (γ t - x) < cmod (γ t - z) ⟹ (λw. inverse (w - z)) field_differentiable at x"
      unfolding field_differentiable_def by (force simp: intro!: derivative_eq_intros)
    obtain h where h: "⋀x. cmod (γ t - x) < cmod (γ t - z) ⟹
                       (h has_field_derivative inverse (x - z)) (at x within {y. cmod (γ t - y) < cmod (γ t - z)})"
      using holomorphic_convex_primitive [where f = "λw. inverse(w - z)", OF convex_ball finite.emptyI cball icd]
      by simp (auto simp: ball_def dist_norm that)
    { fix x D
      assume x: "x ∉ k" "a < x" "x < b"
      then have "x ∈ interior ({a..b} - k)"
        using open_subset_interior [OF o] by fastforce
      then have con: "isCont (λx. ?Dγ x) x"
        using g_C1_diff x by (auto simp: C1_differentiable_on_eq intro: continuous_on_interior)
      then have con_vd: "continuous (at x within {a..b}) (λx. ?Dγ x)"
        by (rule continuous_at_imp_continuous_within)
      have gdx: "γ differentiable at x"
        using x by (simp add: g_diff_at)
      have "((λc. exp (- integral {a..c} (λx. vector_derivative γ (at x) / (γ x - z))) * (γ c - z)) has_derivative (λh. 0))
          (at x within {a..b})"
        using x gdx t
        apply (clarsimp simp add: differentiable_iff_scaleR)
        apply (rule exp_fg [unfolded has_vector_derivative_def, simplified], blast intro: has_derivative_at_within)
        apply (simp_all add: has_vector_derivative_def [symmetric])
        apply (rule has_vector_derivative_eq_rhs [OF integral_has_vector_derivative_continuous_at])
        apply (rule con_vd continuous_intros cong vg_int | simp add: continuous_at_imp_continuous_within has_vector_derivative_continuous vector_derivative_at)+
        done
      } note * = this
    have "exp (- (integral {a..t} (λx. ?Dγ x / (γ x - z)))) * (γ t - z) =γ a - z"
      apply (rule has_derivative_zero_unique_strong_interval [of "{a,b} ∪ k" a b])
      using t
      apply (auto intro!: * continuous_intros fink cong indefinite_integral_continuous [OF vg_int]  simp add: ab)+
      done
   }
  with ab show ?thesis2
    by (simp add: divide_inverse_commute integral_def)
qed

corollary winding_number_exp_2pi:
    "⟦path p; z ∉ path_image p⟧
     ⟹ pathfinish p - z = exp (2 * pi * ii * winding_number p z) * (pathstart p - z)"
using winding_number [of p z 1] unfolding valid_path_def path_image_def pathstart_def pathfinish_def
  by (force dest: winding_number_exp_integral(2) [of _ 0 1 z] simp: field_simps contour_integral_integral exp_minus)


subsection‹The version with complex integers and equality›

lemma integer_winding_number_eq:
  assumes γ: "path γ" and z: "z ∉ path_image γ"
  shows "winding_number γ z ∈ ℤ ⟷ pathfinish γ = pathstart γ"
proof -
  have *: "⋀i::complex. ⋀g0 g1. ⟦i ≠ 0; g0 ≠ z; (g1 - z) / i = g0 - z⟧ ⟹ (i = 1 ⟷ g1 = g0)"
      by (simp add: field_simps) algebra
  obtain p where p: "valid_path p" "z ∉ path_image p"
                    "pathstart p = pathstart γ" "pathfinish p = pathfinish γ"
                    "contour_integral p (λw. 1 / (w - z)) = complex_of_real (2 * pi) * 𝗂 * winding_number γ z"
    using winding_number [OF assms, of 1] by auto
  have [simp]: "(winding_number γ z ∈ ℤ) = (exp (contour_integral p (λw. 1 / (w - z))) = 1)"
      using p by (simp add: exp_eq_1 complex_is_Int_iff)
  have "winding_number p z ∈ ℤ ⟷ pathfinish p = pathstart p"
    using p z
    apply (simp add: winding_number_valid_path valid_path_def path_image_def pathstart_def pathfinish_def)
    using winding_number_exp_integral(2) [of p 0 1 z]
    apply (simp add: field_simps contour_integral_integral exp_minus)
    apply (rule *)
    apply (auto simp: path_image_def field_simps)
    done
  then show ?thesis using p
    by (auto simp: winding_number_valid_path)
qed

theorem integer_winding_number:
  "⟦path γ; pathfinish γ = pathstart γ; z ∉ path_image γ⟧ ⟹ winding_number γ z ∈ ℤ"
by (metis integer_winding_number_eq)


text‹If the winding number's magnitude is at least one, then the path must contain points in every direction.*)
   We can thus bound the winding number of a path that doesn't intersect a given ray. ›

lemma winding_number_pos_meets:
  fixes z::complex
  assumes γ: "valid_path γ" and z: "z ∉ path_image γ" and 1: "Re (winding_number γ z) ≥ 1"
      and w: "w ≠ z"
  shows "∃a::real. 0 < a ∧ z + a*(w - z) ∈ path_image γ"
proof -
  have [simp]: "⋀x. 0 ≤ x ⟹ x ≤ 1 ⟹ γ x ≠ z"
    using z by (auto simp: path_image_def)
  have [simp]: "z ∉ γ ` {0..1}"
    using path_image_def z by auto
  have gpd: "γ piecewise_C1_differentiable_on {0..1}"
    using γ valid_path_def by blast
  def r  "(w - z) / (γ 0 - z)"
  have [simp]: "r ≠ 0"
    using w z by (auto simp: r_def)
  have "Arg r ≤ 2*pi"
    by (simp add: Arg less_eq_real_def)
  also have "... ≤ Im (integral {0..1} (λx. vector_derivative γ (at x) / (γ x - z)))"
    using 1
    apply (simp add: winding_number_valid_path [OF γ z] Cauchy_Integral_Thm.contour_integral_integral)
    apply (simp add: Complex.Re_divide field_simps power2_eq_square)
    done
  finally have "Arg r ≤ Im (integral {0..1} (λx. vector_derivative γ (at x) / (γ x - z)))" .
  then have "∃t. t ∈ {0..1} ∧ Im(integral {0..t} (λx. vector_derivative γ (at x)/(γ x - z))) = Arg r"
    apply (simp add:)
    apply (rule Topological_Spaces.IVT')
    apply (simp_all add: Complex_Transcendental.Arg_ge_0)
    apply (intro continuous_intros indefinite_integral_continuous winding_number_exp_integral [OF gpd]; simp)
    done
  then obtain t where t:     "t ∈ {0..1}"
                  and eqArg: "Im (integral {0..t} (λx. vector_derivative γ (at x)/(γ x - z))) = Arg r"
    by blast
  def i  "integral {0..t} (λx. vector_derivative γ (at x) / (γ x - z))"
  have iArg: "Arg r = Im i"
    using eqArg by (simp add: i_def)
  have gpdt: "γ piecewise_C1_differentiable_on {0..t}"
    by (metis atLeastAtMost_iff atLeastatMost_subset_iff order_refl piecewise_C1_differentiable_on_subset gpd t)
  have "exp (- i) * (γ t - z) = γ 0 - z"
    unfolding i_def
    apply (rule winding_number_exp_integral [OF gpdt])
    using t z unfolding path_image_def
    apply force+
    done
  then have *: "γ t - z = exp i * (γ 0 - z)"
    by (simp add: exp_minus field_simps)
  then have "(w - z) = r * (γ 0 - z)"
    by (simp add: r_def)
  then have "z + complex_of_real (exp (Re i)) * (w - z) / complex_of_real (cmod r) = γ t"
    apply (simp add:)
    apply (subst Complex_Transcendental.Arg_eq [of r])
    apply (simp add: iArg)
    using *
    apply (simp add: exp_eq_polar field_simps)
    done
  with t show ?thesis
    by (rule_tac x="exp(Re i) / norm r" in exI) (auto simp: path_image_def)
qed

lemma winding_number_big_meets:
  fixes z::complex
  assumes γ: "valid_path γ" and z: "z ∉ path_image γ" and "¦Re (winding_number γ z)¦ ≥ 1"
      and w: "w ≠ z"
  shows "∃a::real. 0 < a ∧ z + a*(w - z) ∈ path_image γ"
proof -
  { assume "Re (winding_number γ z) ≤ - 1"
    then have "Re (winding_number (reversepath γ) z) ≥ 1"
      by (simp add: γ valid_path_imp_path winding_number_reversepath z)
    moreover have "valid_path (reversepath γ)"
      using γ valid_path_imp_reverse by auto
    moreover have "z ∉ path_image (reversepath γ)"
      by (simp add: z)
    ultimately have "∃a::real. 0 < a ∧ z + a*(w - z) ∈ path_image (reversepath γ)"
      using winding_number_pos_meets w by blast
    then have ?thesis
      by simp
  }
  then show ?thesis
    using assms
    by (simp add: Groups.abs_if_class.abs_if winding_number_pos_meets split: if_split_asm)
qed

lemma winding_number_less_1:
  fixes z::complex
  shows
  "⟦valid_path γ; z ∉ path_image γ; w ≠ z;
    ⋀a::real. 0 < a ⟹ z + a*(w - z) ∉ path_image γ⟧
   ⟹ ¦Re(winding_number γ z)¦ < 1"
   by (auto simp: not_less dest: winding_number_big_meets)

text‹One way of proving that WN=1 for a loop.›
lemma winding_number_eq_1:
  fixes z::complex
  assumes γ: "valid_path γ" and z: "z ∉ path_image γ" and loop: "pathfinish γ = pathstart γ"
      and 0: "0 < Re(winding_number γ z)" and 2: "Re(winding_number γ z) < 2"
  shows "winding_number γ z = 1"
proof -
  have "winding_number γ z ∈ Ints"
    by (simp add: γ integer_winding_number loop valid_path_imp_path z)
  then show ?thesis
    using 0 2 by (auto simp: Ints_def)
qed


subsection‹Continuity of winding number and invariance on connected sets.›

lemma continuous_at_winding_number:
  fixes z::complex
  assumes γ: "path γ" and z: "z ∉ path_image γ"
  shows "continuous (at z) (winding_number γ)"
proof -
  obtain e where "e>0" and cbg: "cball z e ⊆ - path_image γ"
    using open_contains_cball [of "- path_image γ"]  z
    by (force simp: closed_def [symmetric] closed_path_image [OF γ])
  then have ppag: "path_image γ ⊆ - cball z (e/2)"
    by (force simp: cball_def dist_norm)
  have oc: "open (- cball z (e / 2))"
    by (simp add: closed_def [symmetric])
  obtain d where "d>0" and pi_eq:
    "⋀h1 h2. ⟦valid_path h1; valid_path h2;
              (∀t∈{0..1}. cmod (h1 t - γ t) < d ∧ cmod (h2 t - γ t) < d);
              pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1⟧
             ⟹
               path_image h1 ⊆ - cball z (e / 2) ∧
               path_image h2 ⊆ - cball z (e / 2) ∧
               (∀f. f holomorphic_on - cball z (e / 2) ⟶ contour_integral h2 f = contour_integral h1 f)"
    using contour_integral_nearby_ends [OF oc γ ppag] by metis
  obtain p where p: "valid_path p" "z ∉ path_image p"
                    "pathstart p = pathstart γ ∧ pathfinish p = pathfinish γ"
              and pg: "⋀t. t∈{0..1} ⟹ cmod (γ t - p t) < min d e / 2"
              and pi: "contour_integral p (λx. 1 / (x - z)) = complex_of_real (2 * pi) * 𝗂 * winding_number γ z"
    using winding_number [OF γ z, of "min d e / 2"] ‹d>0› ‹e>0› by auto
  { fix w
    assume d2: "cmod (w - z) < d/2" and e2: "cmod (w - z) < e/2"
    then have wnotp: "w ∉ path_image p"
      using cbg ‹d>0› ‹e>0›
      apply (simp add: path_image_def cball_def dist_norm, clarify)
      apply (frule pg)
      apply (drule_tac c="γ x" in subsetD)
      apply (auto simp: less_eq_real_def norm_minus_commute norm_triangle_half_l)
      done
    have wnotg: "w ∉ path_image γ"
      using cbg e2 ‹e>0› by (force simp: dist_norm norm_minus_commute)
    { fix k::real
      assume k: "k>0"
      then obtain q where q: "valid_path q" "w ∉ path_image q"
                             "pathstart q = pathstart γ ∧ pathfinish q = pathfinish γ"
                    and qg: "⋀t. t ∈ {0..1} ⟹ cmod (γ t - q t) < min k (min d e) / 2"
                    and qi: "contour_integral q (λu. 1 / (u - w)) = complex_of_real (2 * pi) * 𝗂 * winding_number γ w"
        using winding_number [OF γ wnotg, of "min k (min d e) / 2"] ‹d>0› ‹e>0› k
        by (force simp: min_divide_distrib_right)
      have "contour_integral p (λu. 1 / (u - w)) = contour_integral q (λu. 1 / (u - w))"
        apply (rule pi_eq [OF ‹valid_path q› ‹valid_path p›, THEN conjunct2, THEN conjunct2, rule_format])
        apply (frule pg)
        apply (frule qg)
        using p q ‹d>0› e2
        apply (auto simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
        done
      then have "contour_integral p (λx. 1 / (x - w)) = complex_of_real (2 * pi) * 𝗂 * winding_number γ w"
        by (simp add: pi qi)
    } note pip = this
    have "path p"
      using p by (simp add: valid_path_imp_path)
    then have "winding_number p w = winding_number γ w"
      apply (rule winding_number_unique [OF _ wnotp])
      apply (rule_tac x=p in exI)
      apply (simp add: p wnotp min_divide_distrib_right pip)
      done
  } note wnwn = this
  obtain pe where "pe>0" and cbp: "cball z (3 / 4 * pe) ⊆ - path_image p"
    using p open_contains_cball [of "- path_image p"]
    by (force simp: closed_def [symmetric] closed_path_image [OF valid_path_imp_path])
  obtain L
    where "L>0"
      and L: "⋀f B. ⟦f holomorphic_on - cball z (3 / 4 * pe);
                      ∀z ∈ - cball z (3 / 4 * pe). cmod (f z) ≤ B⟧ ⟹
                      cmod (contour_integral p f) ≤ L * B"
    using contour_integral_bound_exists [of "- cball z (3/4*pe)" p] cbp ‹valid_path p› by force
  { fix e::real and w::complex
    assume e: "0 < e" and w: "cmod (w - z) < pe/4" "cmod (w - z) < e * pe2 / (8 * L)"
    then have [simp]: "w ∉ path_image p"
      using cbp p(2) ‹0 < pe›
      by (force simp: dist_norm norm_minus_commute path_image_def cball_def)
    have [simp]: "contour_integral p (λx. 1/(x - w)) - contour_integral p (λx. 1/(x - z)) =
                  contour_integral p (λx. 1/(x - w) - 1/(x - z))"
      by (simp add: p contour_integrable_inversediff contour_integral_diff)
    { fix x
      assume pe: "3/4 * pe < cmod (z - x)"
      have "cmod (w - x) < pe/4 + cmod (z - x)"
        by (meson add_less_cancel_right norm_diff_triangle_le order_refl order_trans_rules(21) w(1))
      then have wx: "cmod (w - x) < 4/3 * cmod (z - x)" using pe by simp
      have "cmod (z - x) ≤ cmod (z - w) + cmod (w - x)"
        using norm_diff_triangle_le by blast
      also have "... < pe/4 + cmod (w - x)"
        using w by (simp add: norm_minus_commute)
      finally have "pe/2 < cmod (w - x)"
        using pe by auto
      then have "(pe/2)^2 < cmod (w - x) ^ 2"
        apply (rule power_strict_mono)
        using ‹pe>0› by auto
      then have pe2: "pe^2 < 4 * cmod (w - x) ^ 2"
        by (simp add: power_divide)
      have "8 * L * cmod (w - z) < e * pe2"
        using w ‹L>0› by (simp add: field_simps)
      also have "... < e * 4 * cmod (w - x) * cmod (w - x)"
        using pe2 ‹e>0› by (simp add: power2_eq_square)
      also have "... < e * 4 * cmod (w - x) * (4/3 * cmod (z - x))"
        using wx
        apply (rule mult_strict_left_mono)
        using pe2 e not_less_iff_gr_or_eq by fastforce
      finally have "L * cmod (w - z) < 2/3 * e * cmod (w - x) * cmod (z - x)"
        by simp
      also have "... ≤ e * cmod (w - x) * cmod (z - x)"
         using e by simp
      finally have Lwz: "L * cmod (w - z) < e * cmod (w - x) * cmod (z - x)" .
      have "L * cmod (1 / (x - w) - 1 / (x - z)) ≤ e"
        apply (cases "x=z ∨ x=w")
        using pe ‹pe>0› w ‹L>0›
        apply (force simp: norm_minus_commute)
        using wx w(2) ‹L>0› pe pe2 Lwz
        apply (auto simp: divide_simps mult_less_0_iff norm_minus_commute norm_divide norm_mult power2_eq_square)
        done
    } note L_cmod_le = this
    have *: "cmod (contour_integral p (λx. 1 / (x - w) - 1 / (x - z))) ≤ L * (e * pe2 / L / 4 * (inverse (pe / 2))2)"
      apply (rule L)
      using ‹pe>0› w
      apply (force simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
      using ‹pe>0› w ‹L>0›
      apply (auto simp: cball_def dist_norm field_simps L_cmod_le  simp del: less_divide_eq_numeral1 le_divide_eq_numeral1)
      done
    have "cmod (contour_integral p (λx. 1 / (x - w)) - contour_integral p (λx. 1 / (x - z))) < 2*e"
      apply (simp add:)
      apply (rule le_less_trans [OF *])
      using ‹L>0› e
      apply (force simp: field_simps)
      done
    then have "cmod (winding_number p w - winding_number p z) < e"
      using pi_ge_two e
      by (force simp: winding_number_valid_path p field_simps norm_divide norm_mult intro: less_le_trans)
  } note cmod_wn_diff = this
  then have "isCont (winding_number p) z"
    apply (simp add: continuous_at_eps_delta, clarify)
    apply (rule_tac x="min (pe/4) (e/2*pe^2/L/4)" in exI)
    using ‹pe>0› ‹L>0›
    apply (simp add: dist_norm cmod_wn_diff)
    done
  then show ?thesis
    apply (rule continuous_transform_within [where d = "min d e / 2"])
    apply (auto simp: ‹d>0› ‹e>0› dist_norm wnwn)
    done
qed

corollary continuous_on_winding_number:
    "path γ ⟹ continuous_on (- path_image γ) (λw. winding_number γ w)"
  by (simp add: continuous_at_imp_continuous_on continuous_at_winding_number)


subsection‹The winding number is constant on a connected region›

lemma winding_number_constant:
  assumes γ: "path γ" and loop: "pathfinish γ = pathstart γ" and cs: "connected s" and sg: "s ∩ path_image γ = {}"
    shows "∃k. ∀z ∈ s. winding_number γ z = k"
proof -
  { fix y z
    assume ne: "winding_number γ y ≠ winding_number γ z"
    assume "y ∈ s" "z ∈ s"
    then have "winding_number γ y ∈ ℤ"  "winding_number γ z ∈  ℤ"
      using integer_winding_number [OF γ loop] sg ‹y ∈ s› by auto
    with ne have "1 ≤ cmod (winding_number γ y - winding_number γ z)"
      by (auto simp: Ints_def of_int_diff [symmetric] simp del: of_int_diff)
  } note * = this
  show ?thesis
    apply (rule continuous_discrete_range_constant [OF cs])
    using continuous_on_winding_number [OF γ] sg
    apply (metis Diff_Compl Diff_eq_empty_iff continuous_on_subset)
    apply (rule_tac x=1 in exI)
    apply (auto simp: *)
    done
qed

lemma winding_number_eq:
     "⟦path γ; pathfinish γ = pathstart γ; w ∈ s; z ∈ s; connected s; s ∩ path_image γ = {}⟧
      ⟹ winding_number γ w = winding_number γ z"
using winding_number_constant by fastforce

lemma open_winding_number_levelsets:
  assumes γ: "path γ" and loop: "pathfinish γ = pathstart γ"
    shows "open {z. z ∉ path_image γ ∧ winding_number γ z = k}"
proof -
  have op: "open (- path_image γ)"
    by (simp add: closed_path_image γ open_Compl)
  { fix z assume z: "z ∉ path_image γ" and k: "k = winding_number γ z"
    obtain e where e: "e>0" "ball z e ⊆ - path_image γ"
      using open_contains_ball [of "- path_image γ"] op z
      by blast
    have "∃e>0. ∀y. dist y z < e ⟶ y ∉ path_image γ ∧ winding_number γ y = winding_number γ z"
      apply (rule_tac x=e in exI)
      using e apply (simp add: dist_norm ball_def norm_minus_commute)
      apply (auto simp: dist_norm norm_minus_commute intro!: winding_number_eq [OF assms, where s = "ball z e"])
      done
  } then
  show ?thesis
    by (auto simp: open_dist)
qed

subsection‹Winding number is zero "outside" a curve, in various senses›

lemma winding_number_zero_in_outside:
  assumes γ: "path γ" and loop: "pathfinish γ = pathstart γ" and z: "z ∈ outside (path_image γ)"
    shows "winding_number γ z = 0"
proof -
  obtain B::real where "0 < B" and B: "path_image γ ⊆ ball 0 B"
    using bounded_subset_ballD [OF bounded_path_image [OF γ]] by auto
  obtain w::complex where w: "w ∉ ball 0 (B + 1)"
    by (metis abs_of_nonneg le_less less_irrefl mem_ball_0 norm_of_real)
  have "- ball 0 (B + 1) ⊆ outside (path_image γ)"
    apply (rule outside_subset_convex)
    using B subset_ball by auto
  then have wout: "w ∈ outside (path_image γ)"
    using w by blast
  moreover obtain k where "⋀z. z ∈ outside (path_image γ) ⟹ winding_number γ z = k"
    using winding_number_constant [OF γ loop, of "outside(path_image γ)"] connected_outside
    by (metis DIM_complex bounded_path_image dual_order.refl γ outside_no_overlap)
  ultimately have "winding_number γ z = winding_number γ w"
    using z by blast
  also have "... = 0"
  proof -
    have wnot: "w ∉ path_image γ"  using wout by (simp add: outside_def)
    { fix e::real assume "0<e"
      obtain p where p: "polynomial_function p" "pathstart p = pathstart γ" "pathfinish p = pathfinish γ"
                 and pg1: "(⋀t. ⟦0 ≤ t; t ≤ 1⟧ ⟹ cmod (p t - γ t) < 1)"
                 and pge: "(⋀t. ⟦0 ≤ t; t ≤ 1⟧ ⟹ cmod (p t - γ t) < e)"
        using path_approx_polynomial_function [OF γ, of "min 1 e"] ‹e>0› by force
      have pip: "path_image p ⊆ ball 0 (B + 1)"
        using B
        apply (clarsimp simp add: path_image_def dist_norm ball_def)
        apply (frule (1) pg1)
        apply (fastforce dest: norm_add_less)
        done
      then have "w ∉ path_image p"  using w by blast
      then have "∃p. valid_path p ∧ w ∉ path_image p ∧
                     pathstart p = pathstart γ ∧ pathfinish p = pathfinish γ ∧
                     (∀t∈{0..1}. cmod (γ t - p t) < e) ∧ contour_integral p (λwa. 1 / (wa - w)) = 0"
        apply (rule_tac x=p in exI)
        apply (simp add: p valid_path_polynomial_function)
        apply (intro conjI)
        using pge apply (simp add: norm_minus_commute)
        apply (rule contour_integral_unique [OF Cauchy_theorem_convex_simple [OF _ convex_ball [of 0 "B+1"]]])
        apply (rule holomorphic_intros | simp add: dist_norm)+
        using mem_ball_0 w apply blast
        using p apply (simp_all add: valid_path_polynomial_function loop pip)
        done
    }
    then show ?thesis
      by (auto intro: winding_number_unique [OF γ] simp add: wnot)
  qed
  finally show ?thesis .
qed

lemma winding_number_zero_outside:
    "⟦path γ; convex s; pathfinish γ = pathstart γ; z ∉ s; path_image γ ⊆ s⟧ ⟹ winding_number γ z = 0"
  by (meson convex_in_outside outside_mono subsetCE winding_number_zero_in_outside)

lemma winding_number_zero_at_infinity:
  assumes γ: "path γ" and loop: "pathfinish γ = pathstart γ"
    shows "∃B. ∀z. B ≤ norm z ⟶ winding_number γ z = 0"
proof -
  obtain B::real where "0 < B" and B: "path_image γ ⊆ ball 0 B"
    using bounded_subset_ballD [OF bounded_path_image [OF γ]] by auto
  then show ?thesis
    apply (rule_tac x="B+1" in exI, clarify)
    apply (rule winding_number_zero_outside [OF γ convex_cball [of 0 B] loop])
    apply (meson less_add_one mem_cball_0 not_le order_trans)
    using ball_subset_cball by blast
qed

lemma winding_number_zero_point:
    "⟦path γ; convex s; pathfinish γ = pathstart γ; open s; path_image γ ⊆ s⟧
     ⟹ ∃z. z ∈ s ∧ winding_number γ z = 0"
  using outside_compact_in_open [of "path_image γ" s] path_image_nonempty winding_number_zero_in_outside
  by (fastforce simp add: compact_path_image)


text‹If a path winds round a set, it winds rounds its inside.›
lemma winding_number_around_inside:
  assumes γ: "path γ" and loop: "pathfinish γ = pathstart γ"
      and cls: "closed s" and cos: "connected s" and s_disj: "s ∩ path_image γ = {}"
      and z: "z ∈ s" and wn_nz: "winding_number γ z ≠ 0" and w: "w ∈ s ∪ inside s"
    shows "winding_number γ w = winding_number γ z"
proof -
  have ssb: "s ⊆ inside(path_image γ)"
  proof
    fix x :: complex
    assume "x ∈ s"
    hence "x ∉ path_image γ"
      by (meson disjoint_iff_not_equal s_disj)
    thus "x ∈ inside (path_image γ)"
      using ‹x ∈ s› by (metis (no_types) ComplI UnE cos γ loop s_disj union_with_outside winding_number_eq winding_number_zero_in_outside wn_nz z)
qed
  show ?thesis
    apply (rule winding_number_eq [OF γ loop w])
    using z apply blast
    apply (simp add: cls connected_with_inside cos)
    apply (simp add: Int_Un_distrib2 s_disj, safe)
    by (meson ssb inside_inside_compact_connected [OF cls, of "path_image γ"] compact_path_image connected_path_image contra_subsetD disjoint_iff_not_equal γ inside_no_overlap)
 qed


text‹Bounding a WN by 1/2 for a path and point in opposite halfspaces.›
lemma winding_number_subpath_continuous:
  assumes γ: "valid_path γ" and z: "z ∉ path_image γ"
    shows "continuous_on {0..1} (λx. winding_number(subpath 0 x γ) z)"
proof -
  have *: "integral {0..x} (λt. vector_derivative γ (at t) / (γ t - z)) / (2 * of_real pi * 𝗂) =
         winding_number (subpath 0 x γ) z"
         if x: "0 ≤ x" "x ≤ 1" for x
  proof -
    have "integral {0..x} (λt. vector_derivative γ (at t) / (γ t - z)) / (2 * of_real pi * 𝗂) =
          1 / (2*pi*ii) * contour_integral (subpath 0 x γ) (λw. 1/(w - z))"
      using assms x
      apply (simp add: contour_integral_subcontour_integral [OF contour_integrable_inversediff])
      done
    also have "... = winding_number (subpath 0 x γ) z"
      apply (subst winding_number_valid_path)
      using assms x
      apply (simp_all add: path_image_subpath valid_path_subpath)
      by (force simp: path_image_def)
    finally show ?thesis .
  qed
  show ?thesis
    apply (rule continuous_on_eq
                 [where f = "λx. 1 / (2*pi*ii) *
                                 integral {0..x} (λt. 1/(γ t - z) * vector_derivative γ (at t))"])
    apply (rule continuous_intros)+
    apply (rule indefinite_integral_continuous)
    apply (rule contour_integrable_inversediff [OF assms, unfolded contour_integrable_on])
      using assms
    apply (simp add: *)
    done
qed

lemma winding_number_ivt_pos:
    assumes γ: "valid_path γ" and z: "z ∉ path_image γ" and "0 ≤ w" "w ≤ Re(winding_number γ z)"
      shows "∃t ∈ {0..1}. Re(winding_number(subpath 0 t γ) z) = w"
  apply (rule ivt_increasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right])
  apply (simp add:)
  apply (rule winding_number_subpath_continuous [OF γ z])
  using assms
  apply (auto simp: path_image_def image_def)
  done

lemma winding_number_ivt_neg:
    assumes γ: "valid_path γ" and z: "z ∉ path_image γ" and "Re(winding_number γ z) ≤ w" "w ≤ 0"
      shows "∃t ∈ {0..1}. Re(winding_number(subpath 0 t γ) z) = w"
  apply (rule ivt_decreasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right])
  apply (simp add:)
  apply (rule winding_number_subpath_continuous [OF γ z])
  using assms
  apply (auto simp: path_image_def image_def)
  done

lemma winding_number_ivt_abs:
    assumes γ: "valid_path γ" and z: "z ∉ path_image γ" and "0 ≤ w" "w ≤ ¦Re(winding_number γ z)¦"
      shows "∃t ∈ {0..1}. ¦Re (winding_number (subpath 0 t γ) z)¦ = w"
  using assms winding_number_ivt_pos [of γ z w] winding_number_ivt_neg [of γ z "-w"]
  by force

lemma winding_number_lt_half_lemma:
  assumes γ: "valid_path γ" and z: "z ∉ path_image γ" and az: "a ∙ z ≤ b" and pag: "path_image γ ⊆ {w. a ∙ w > b}"
    shows "Re(winding_number γ z) < 1/2"
proof -
  { assume "Re(winding_number γ z) ≥ 1/2"
    then obtain t::real where t: "0 ≤ t" "t ≤ 1" and sub12: "Re (winding_number (subpath 0 t γ) z) = 1/2"
      using winding_number_ivt_pos [OF γ z, of "1/2"] by auto
    have gt: "γ t - z = - (of_real (exp (- (2 * pi * Im (winding_number (subpath 0 t γ) z)))) * (γ 0 - z))"
      using winding_number_exp_2pi [of "subpath 0 t γ" z]
      apply (simp add: t γ valid_path_imp_path)
      using closed_segment_eq_real_ivl path_image_def t z by (fastforce simp: path_image_subpath Euler sub12)
    have "b < a ∙ γ 0"
    proof -
      have "γ 0 ∈ {c. b < a ∙ c}"
        by (metis (no_types) pag atLeastAtMost_iff image_subset_iff order_refl path_image_def zero_le_one)
      thus ?thesis
        by blast
    qed
    moreover have "b < a ∙ γ t"
    proof -
      have "γ t ∈ {c. b < a ∙ c}"
        by (metis (no_types) pag atLeastAtMost_iff image_subset_iff path_image_def t)
      thus ?thesis
        by blast
    qed
    ultimately have "0 < a ∙ (γ 0 - z)" "0 < a ∙ (γ t - z)" using az
      by (simp add: inner_diff_right)+
    then have False
      by (simp add: gt inner_mult_right mult_less_0_iff)
  }
  then show ?thesis by force
qed

lemma winding_number_lt_half:
  assumes "valid_path γ" "a ∙ z ≤ b" "path_image γ ⊆ {w. a ∙ w > b}"
    shows "¦Re (winding_number γ z)¦ < 1/2"
proof -
  have "z ∉ path_image γ" using assms by auto
  with assms show ?thesis
    apply (simp add: winding_number_lt_half_lemma abs_if del: less_divide_eq_numeral1)
    apply (metis complex_inner_1_right winding_number_lt_half_lemma [OF valid_path_imp_reverse, of γ z a b]
                 winding_number_reversepath valid_path_imp_path inner_minus_left path_image_reversepath)
    done
qed

lemma winding_number_le_half:
  assumes γ: "valid_path γ" and z: "z ∉ path_image γ"
      and anz: "a ≠ 0" and azb: "a ∙ z ≤ b" and pag: "path_image γ ⊆ {w. a ∙ w ≥ b}"
    shows "¦Re (winding_number γ z)¦ ≤ 1/2"
proof -
  { assume wnz_12: "¦Re (winding_number γ z)¦ > 1/2"
    have "isCont (winding_number γ) z"
      by (metis continuous_at_winding_number valid_path_imp_path γ z)
    then obtain d where "d>0" and d: "⋀x'. dist x' z < d ⟹ dist (winding_number γ x') (winding_number γ z) < ¦Re(winding_number γ z)¦ - 1/2"
      using continuous_at_eps_delta wnz_12 diff_gt_0_iff_gt by blast
    def z'  "z - (d / (2 * cmod a)) *R a"
    have *: "a ∙ z' ≤ b - d / 3 * cmod a"
      unfolding z'_def inner_mult_right' divide_inverse
      apply (simp add: divide_simps algebra_simps dot_square_norm power2_eq_square anz)
      apply (metis ‹0 < d› add_increasing azb less_eq_real_def mult_nonneg_nonneg mult_right_mono norm_ge_zero norm_numeral)
      done
    have "cmod (winding_number γ z' - winding_number γ z) < ¦Re (winding_number γ z)¦ - 1/2"
      using d [of z'] anz ‹d>0› by (simp add: dist_norm z'_def)
    then have "1/2 < ¦Re (winding_number γ z)¦ - cmod (winding_number γ z' - winding_number γ z)"
      by simp
    then have "1/2 < ¦Re (winding_number γ z)¦ - ¦Re (winding_number γ z') - Re (winding_number γ z)¦"
      using abs_Re_le_cmod [of "winding_number γ z' - winding_number γ z"] by simp
    then have wnz_12': "¦Re (winding_number γ z')¦ > 1/2"
      by linarith
    moreover have "¦Re (winding_number γ z')¦ < 1/2"
      apply (rule winding_number_lt_half [OF γ *])
      using azb ‹d>0› pag
      apply (auto simp: add_strict_increasing anz divide_simps algebra_simps dest!: subsetD)
      done
    ultimately have False
      by simp
  }
  then show ?thesis by force
qed

lemma winding_number_lt_half_linepath: "z ∉ closed_segment a b ⟹ ¦Re (winding_number (linepath a b) z)¦ < 1/2"
  using separating_hyperplane_closed_point [of "closed_segment a b" z]
  apply auto
  apply (simp add: closed_segment_def)
  apply (drule less_imp_le)
  apply (frule winding_number_lt_half [OF valid_path_linepath [of a b]])
  apply (auto simp: segment)
  done


text‹ Positivity of WN for a linepath.›
lemma winding_number_linepath_pos_lt:
    assumes "0 < Im ((b - a) * cnj (b - z))"
      shows "0 < Re(winding_number(linepath a b) z)"
proof -
  have z: "z ∉ path_image (linepath a b)"
    using assms
    by (simp add: closed_segment_def) (force simp: algebra_simps)
  show ?thesis
    apply (rule winding_number_pos_lt [OF valid_path_linepath z assms])
    apply (simp add: linepath_def algebra_simps)
    done
qed


subsection‹Cauchy's integral formula, again for a convex enclosing set.›

lemma Cauchy_integral_formula_weak:
    assumes s: "convex s" and "finite k" and conf: "continuous_on s f"
        and fcd: "(⋀x. x ∈ interior s - k ⟹ f field_differentiable at x)"
        and z: "z ∈ interior s - k" and vpg: "valid_path γ"
        and pasz: "path_image γ ⊆ s - {z}" and loop: "pathfinish γ = pathstart γ"
      shows "((λw. f w / (w - z)) has_contour_integral (2*pi * ii * winding_number γ z * f z)) γ"
proof -
  obtain f' where f': "(f has_field_derivative f') (at z)"
    using fcd [OF z] by (auto simp: field_differentiable_def)
  have pas: "path_image γ ⊆ s" and znotin: "z ∉ path_image γ" using pasz by blast+
  have c: "continuous (at x within s) (λw. if w = z then f' else (f w - f z) / (w - z))" if "x ∈ s" for x
  proof (cases "x = z")
    case True then show ?thesis
      apply (simp add: continuous_within)
      apply (rule Lim_transform_away_within [of _ "z+1" _ "λw::complex. (f w - f z)/(w - z)"])
      using has_field_derivative_at_within DERIV_within_iff f'
      apply (fastforce simp add:)+
      done
  next
    case False
    then have dxz: "dist x z > 0" by auto
    have cf: "continuous (at x within s) f"
      using conf continuous_on_eq_continuous_within that by blast
    have "continuous (at x within s) (λw. (f w - f z) / (w - z))"
      by (rule cf continuous_intros | simp add: False)+
    then show ?thesis
      apply (rule continuous_transform_within [OF _ dxz that, of "λw::complex. (f w - f z)/(w - z)"])
      apply (force simp: dist_commute)
      done
  qed
  have fink': "finite (insert z k)" using ‹finite k› by blast
  have *: "((λw. if w = z then f' else (f w - f z) / (w - z)) has_contour_integral 0) γ"
    apply (rule Cauchy_theorem_convex [OF _ s fink' _ vpg pas loop])
    using c apply (force simp: continuous_on_eq_continuous_within)
    apply (rename_tac w)
    apply (rule_tac d="dist w z" and f = "λw. (f w - f z)/(w - z)" in field_differentiable_transform_within)
    apply (simp_all add: dist_pos_lt dist_commute)
    apply (metis less_irrefl)
    apply (rule derivative_intros fcd | simp)+
    done
  show ?thesis
    apply (rule has_contour_integral_eq)
    using znotin has_contour_integral_add [OF has_contour_integral_lmul [OF has_contour_integral_winding_number [OF vpg znotin], of "f z"] *]
    apply (auto simp: mult_ac divide_simps)
    done
qed

theorem Cauchy_integral_formula_convex_simple:
    "⟦convex s; f holomorphic_on s; z ∈ interior s; valid_path γ; path_image γ ⊆ s - {z};
      pathfinish γ = pathstart γ⟧
     ⟹ ((λw. f w / (w - z)) has_contour_integral (2*pi * ii * winding_number γ z * f z)) γ"
  apply (rule Cauchy_integral_formula_weak [where k = "{}"])
  using holomorphic_on_imp_continuous_on
  by auto (metis at_within_interior holomorphic_on_def interiorE subsetCE)


subsection‹Homotopy forms of Cauchy's theorem›

proposition Cauchy_theorem_homotopic:
    assumes hom: "if atends then homotopic_paths s g h else homotopic_loops s g h"
        and "open s" and f: "f holomorphic_on s"
        and vpg: "valid_path g" and vph: "valid_path h"
    shows "contour_integral g f = contour_integral h f"
proof -
  have pathsf: "linked_paths atends g h"
    using hom  by (auto simp: linked_paths_def homotopic_paths_imp_pathstart homotopic_paths_imp_pathfinish homotopic_loops_imp_loop)
  obtain k :: "real × real ⇒ complex"
    where contk: "continuous_on ({0..1} × {0..1}) k"
      and ks: "k ` ({0..1} × {0..1}) ⊆ s"
      and k [simp]: "∀x. k (0, x) = g x" "∀x. k (1, x) = h x"
      and ksf: "∀t∈{0..1}. linked_paths atends g (λx. k (t, x))"
      using hom pathsf by (auto simp: linked_paths_def homotopic_paths_def homotopic_loops_def homotopic_with_def split: if_split_asm)
  have ucontk: "uniformly_continuous_on ({0..1} × {0..1}) k"
    by (blast intro: compact_Times compact_uniformly_continuous [OF contk])
  { fix t::real assume t: "t ∈ {0..1}"
    have pak: "path (k o (λu. (t, u)))"
      unfolding path_def
      apply (rule continuous_intros continuous_on_subset [OF contk])+
      using t by force
    have pik: "path_image (k ∘ Pair t) ⊆ s"
      using ks t by (auto simp: path_image_def)
    obtain e where "e>0" and e:
         "⋀g h. ⟦valid_path g; valid_path h;
                  ∀u∈{0..1}. cmod (g u - (k ∘ Pair t) u) < e ∧ cmod (h u - (k ∘ Pair t) u) < e;
                  linked_paths atends g h⟧
                 ⟹ contour_integral h f = contour_integral g f"
      using contour_integral_nearby [OF ‹open s› pak pik, of atends] f by metis
    obtain d where "d>0" and d:
        "⋀x x'. ⟦x ∈ {0..1} × {0..1}; x' ∈ {0..1} × {0..1}; norm (x'-x) < d⟧ ⟹ norm (k x' - k x) < e/4"
      by (rule uniformly_continuous_onE [OF ucontk, of "e/4"]) (auto simp: dist_norm ‹e>0›)
    { fix t1 t2
      assume t1: "0 ≤ t1" "t1 ≤ 1" and t2: "0 ≤ t2" "t2 ≤ 1" and ltd: "¦t1 - t¦ < d" "¦t2 - t¦ < d"
      have no2: "⋀g1 k1 kt. ⟦norm(g1 - k1) < e/4; norm(k1 - kt) < e/4⟧ ⟹ norm(g1 - kt) < e"
        using ‹e > 0›
        apply (rule_tac y = k1 in norm_triangle_half_l)
        apply (auto simp: norm_minus_commute intro: order_less_trans)
        done
      have "∃d>0. ∀g1 g2. valid_path g1 ∧ valid_path g2 ∧
                          (∀u∈{0..1}. cmod (g1 u - k (t1, u)) < d ∧ cmod (g2 u - k (t2, u)) < d) ∧
                          linked_paths atends g1 g2 ⟶
                          contour_integral g2 f = contour_integral g1 f"
        apply (rule_tac x="e/4" in exI)
        using t t1 t2 ltd ‹e > 0›
        apply (auto intro!: e simp: d no2 simp del: less_divide_eq_numeral1)
        done
    }
    then have "∃e. 0 < e ∧
              (∀t1 t2. t1 ∈ {0..1} ∧ t2 ∈ {0..1} ∧ ¦t1 - t¦ < e ∧ ¦t2 - t¦ < e
                ⟶ (∃d. 0 < d ∧
                     (∀g1 g2. valid_path g1 ∧ valid_path g2 ∧
                       (∀u ∈ {0..1}.
                          norm(g1 u - k((t1,u))) < d ∧ norm(g2 u - k((t2,u))) < d) ∧
                          linked_paths atends g1 g2
                          ⟶ contour_integral g2 f = contour_integral g1 f)))"
      by (rule_tac x=d in exI) (simp add: ‹d > 0›)
  }
  then obtain ee where ee:
       "⋀t. t ∈ {0..1} ⟹ ee t > 0 ∧
          (∀t1 t2. t1 ∈ {0..1} ⟶ t2 ∈ {0..1} ⟶ ¦t1 - t¦ < ee t ⟶ ¦t2 - t¦ < ee t
            ⟶ (∃d. 0 < d ∧
                 (∀g1 g2. valid_path g1 ∧ valid_path g2 ∧
                   (∀u ∈ {0..1}.
                      norm(g1 u - k((t1,u))) < d ∧ norm(g2 u - k((t2,u))) < d) ∧
                      linked_paths atends g1 g2
                      ⟶ contour_integral g2 f = contour_integral g1 f)))"
    by metis
  note ee_rule = ee [THEN conjunct2, rule_format]
  def C  "(λt. ball t (ee t / 3)) ` {0..1}"
  have "∀t ∈ C. open t" by (simp add: C_def)
  moreover have "{0..1} ⊆ ⋃C"
    using ee [THEN conjunct1] by (auto simp: C_def dist_norm)
  ultimately obtain C' where C': "C' ⊆ C" "finite C'" and C'01: "{0..1} ⊆ ⋃C'"
    by (rule compactE [OF compact_interval])
  def kk  "{t ∈ {0..1}. ball t (ee t / 3) ∈ C'}"
  have kk01: "kk ⊆ {0..1}" by (auto simp: kk_def)
  def e  "Min (ee ` kk)"
  have C'_eq: "C' = (λt. ball t (ee t / 3)) ` kk"
    using C' by (auto simp: kk_def C_def)
  have ee_pos[simp]: "⋀t. t ∈ {0..1} ⟹ ee t > 0"
    by (simp add: kk_def ee)
  moreover have "finite kk"
    using ‹finite C'› kk01 by (force simp: C'_eq inj_on_def ball_eq_ball_iff dest: ee_pos finite_imageD)
  moreover have "kk ≠ {}" using ‹{0..1} ⊆ ⋃C'› C'_eq by force
  ultimately have "e > 0"
    using finite_less_Inf_iff [of "ee ` kk" 0] kk01 by (force simp: e_def)
  then obtain N::nat where "N > 0" and N: "1/N < e/3"
    by (meson divide_pos_pos nat_approx_posE zero_less_Suc zero_less_numeral)
  have e_le_ee: "⋀i. i ∈ kk ⟹ e ≤ ee i"
    using ‹finite kk› by (simp add: e_def Min_le_iff [of "ee ` kk"])
  have plus: "∃t ∈ kk. x ∈ ball t (ee t / 3)" if "x ∈ {0..1}" for x
    using C' subsetD [OF C'01 that]  unfolding C'_eq by blast
  have [OF order_refl]:
      "∃d. 0 < d ∧ (∀j. valid_path j ∧ (∀u ∈ {0..1}. norm(j u - k (n/N, u)) < d) ∧ linked_paths atends g j
                        ⟶ contour_integral j f = contour_integral g f)"
       if "n ≤ N" for n
  using that
  proof (induct n)
    case 0 show ?case using ee_rule [of 0 0 0]
      apply clarsimp
      apply (rule_tac x=d in exI, safe)
      by (metis diff_self vpg norm_zero)
  next
    case (Suc n)
    then have N01: "n/N ∈ {0..1}" "(Suc n)/N ∈ {0..1}"  by auto
    then obtain t where t: "t ∈ kk" "n/N ∈ ball t (ee t / 3)"
      using plus [of "n/N"] by blast
    then have nN_less: "¦n/N - t¦ < ee t"
      by (simp add: dist_norm del: less_divide_eq_numeral1)
    have n'N_less: "¦real (Suc n) / real N - t¦ < ee t"
      using t N ‹N > 0› e_le_ee [of t]
      by (simp add: dist_norm add_divide_distrib abs_diff_less_iff del: less_divide_eq_numeral1) (simp add: field_simps)
    have t01: "t ∈ {0..1}" using ‹kk ⊆ {0..1}› ‹t ∈ kk› by blast
    obtain d1 where "d1 > 0" and d1:
        "⋀g1 g2. ⟦valid_path g1; valid_path g2;
                   ∀u∈{0..1}. cmod (g1 u - k (n/N, u)) < d1 ∧ cmod (g2 u - k ((Suc n) / N, u)) < d1;
                   linked_paths atends g1 g2⟧
                   ⟹ contour_integral g2 f = contour_integral g1 f"
      using ee [THEN conjunct2, rule_format, OF t01 N01 nN_less n'N_less] by fastforce
    have "n ≤ N" using Suc.prems by auto
    with Suc.hyps
    obtain d2 where "d2 > 0"
      and d2: "⋀j. ⟦valid_path j; ∀u∈{0..1}. cmod (j u - k (n/N, u)) < d2; linked_paths atends g j⟧
                     ⟹ contour_integral j f = contour_integral g f"
        by auto
    have "continuous_on {0..1} (k o (λu. (n/N, u)))"
      apply (rule continuous_intros continuous_on_subset [OF contk])+
      using N01 by auto
    then have pkn: "path (λu. k (n/N, u))"
      by (simp add: path_def)
    have min12: "min d1 d2 > 0" by (simp add: ‹0 < d1› ‹0 < d2›)
    obtain p where "polynomial_function p"
        and psf: "pathstart p = pathstart (λu. k (n/N, u))"
                 "pathfinish p = pathfinish (λu. k (n/N, u))"
        and pk_le:  "⋀t. t∈{0..1} ⟹ cmod (p t - k (n/N, t)) < min d1 d2"
      using path_approx_polynomial_function [OF pkn min12] by blast
    then have vpp: "valid_path p" using valid_path_polynomial_function by blast
    have lpa: "linked_paths atends g p"
      by (metis (mono_tags, lifting) N01(1) ksf linked_paths_def pathfinish_def pathstart_def psf)
    show ?case
      apply (rule_tac x="min d1 d2" in exI)
      apply (simp add: ‹0 < d1› ‹0 < d2›, clarify)
      apply (rule_tac s="contour_integral p f" in trans)
      using pk_le N01(1) ksf pathfinish_def pathstart_def
      apply (force intro!: vpp d1 simp add: linked_paths_def psf ksf)
      using pk_le N01 apply (force intro!: vpp d2 lpa simp add: linked_paths_def psf ksf)
      done
  qed
  then obtain d where "0 < d"
                       "⋀j. valid_path j ∧ (∀u ∈ {0..1}. norm(j u - k (1,u)) < d) ∧
                            linked_paths atends g j
                            ⟹ contour_integral j f = contour_integral g f"
    using ‹N>0› by auto
  then have "linked_paths atends g h ⟹ contour_integral h f = contour_integral g f"
    using ‹N>0› vph by fastforce
  then show ?thesis
    by (simp add: pathsf)
qed

proposition Cauchy_theorem_homotopic_paths:
    assumes hom: "homotopic_paths s g h"
        and "open s" and f: "f holomorphic_on s"
        and vpg: "valid_path g" and vph: "valid_path h"
    shows "contour_integral g f = contour_integral h f"
  using Cauchy_theorem_homotopic [of True s g h] assms by simp

proposition Cauchy_theorem_homotopic_loops:
    assumes hom: "homotopic_loops s g h"
        and "open s" and f: "f holomorphic_on s"
        and vpg: "valid_path g" and vph: "valid_path h"
    shows "contour_integral g f = contour_integral h f"
  using Cauchy_theorem_homotopic [of False s g h] assms by simp

lemma has_contour_integral_newpath:
    "⟦(f has_contour_integral y) h; f contour_integrable_on g; contour_integral g f = contour_integral h f⟧
     ⟹ (f has_contour_integral y) g"
  using has_contour_integral_integral contour_integral_unique by auto

lemma Cauchy_theorem_null_homotopic:
     "⟦f holomorphic_on s; open s; valid_path g; homotopic_loops s g (linepath a a)⟧ ⟹ (f has_contour_integral 0) g"
  apply (rule has_contour_integral_newpath [where h = "linepath a a"], simp)
  using contour_integrable_holomorphic_simple
    apply (blast dest: holomorphic_on_imp_continuous_on homotopic_loops_imp_subset)
  by (simp add: Cauchy_theorem_homotopic_loops)



subsection‹More winding number properties›

text‹including the fact that it's +-1 inside a simple closed curve.›

lemma winding_number_homotopic_paths:
    assumes "homotopic_paths (-{z}) g h"
      shows "winding_number g z = winding_number h z"
proof -
  have "path g" "path h" using homotopic_paths_imp_path [OF assms] by auto
  moreover have pag: "z ∉ path_image g" and pah: "z ∉ path_image h"
    using homotopic_paths_imp_subset [OF assms] by auto
  ultimately obtain d e where "d > 0" "e > 0"
      and d: "⋀p. ⟦path p; pathstart p = pathstart g; pathfinish p = pathfinish g; ∀t∈{0..1}. norm (p t - g t) < d⟧
            ⟹ homotopic_paths (-{z}) g p"
      and e: "⋀q. ⟦path q; pathstart q = pathstart h; pathfinish q = pathfinish h; ∀t∈{0..1}. norm (q t - h t) < e⟧
            ⟹ homotopic_paths (-{z}) h q"
    using homotopic_nearby_paths [of g "-{z}"] homotopic_nearby_paths [of h "-{z}"] by force
  obtain p where p:
       "valid_path p" "z ∉ path_image p"
       "pathstart p = pathstart g" "pathfinish p = pathfinish g"
       and gp_less:"∀t∈{0..1}. cmod (g t - p t) < d"
       and pap: "contour_integral p (λw. 1 / (w - z)) = complex_of_real (2 * pi) * 𝗂 * winding_number g z"
    using winding_number [OF ‹path g› pag ‹0 < d›] by blast
  obtain q where q:
       "valid_path q" "z ∉ path_image q"
       "pathstart q = pathstart h" "pathfinish q = pathfinish h"
       and hq_less: "∀t∈{0..1}. cmod (h t - q t) < e"
       and paq:  "contour_integral q (λw. 1 / (w - z)) = complex_of_real (2 * pi) * 𝗂 * winding_number h z"
    using winding_number [OF ‹path h› pah ‹0 < e›] by blast
  have gp: "homotopic_paths (- {z}) g p"
    by (simp add: d p valid_path_imp_path norm_minus_commute gp_less)
  have hq: "homotopic_paths (- {z}) h q"
    by (simp add: e q valid_path_imp_path norm_minus_commute hq_less)
  have "contour_integral p (λw. 1/(w - z)) = contour_integral q (λw. 1/(w - z))"
    apply (rule Cauchy_theorem_homotopic_paths [of "-{z}"])
    apply (blast intro: homotopic_paths_trans homotopic_paths_sym gp hq assms)
    apply (auto intro!: holomorphic_intros simp: p q)
    done
  then show ?thesis
    by (simp add: pap paq)
qed

lemma winding_number_homotopic_loops:
    assumes "homotopic_loops (-{z}) g h"
      shows "winding_number g z = winding_number h z"
proof -
  have "path g" "path h" using homotopic_loops_imp_path [OF assms] by auto
  moreover have pag: "z ∉ path_image g" and pah: "z ∉ path_image h"
    using homotopic_loops_imp_subset [OF assms] by auto
  moreover have gloop: "pathfinish g = pathstart g" and hloop: "pathfinish h = pathstart h"
    using homotopic_loops_imp_loop [OF assms] by auto
  ultimately obtain d e where "d > 0" "e > 0"
      and d: "⋀p. ⟦path p; pathfinish p = pathstart p; ∀t∈{0..1}. norm (p t - g t) < d⟧
            ⟹ homotopic_loops (-{z}) g p"
      and e: "⋀q. ⟦path q; pathfinish q = pathstart q; ∀t∈{0..1}. norm (q t - h t) < e⟧
            ⟹ homotopic_loops (-{z}) h q"
    using homotopic_nearby_loops [of g "-{z}"] homotopic_nearby_loops [of h "-{z}"] by force
  obtain p where p:
       "valid_path p" "z ∉ path_image p"
       "pathstart p = pathstart g" "pathfinish p = pathfinish g"
       and gp_less:"∀t∈{0..1}. cmod (g t - p t) < d"
       and pap: "contour_integral p (λw. 1 / (w - z)) = complex_of_real (2 * pi) * 𝗂 * winding_number g z"
    using winding_number [OF ‹path g› pag ‹0 < d›] by blast
  obtain q where q:
       "valid_path q" "z ∉ path_image q"
       "pathstart q = pathstart h" "pathfinish q = pathfinish h"
       and hq_less: "∀t∈{0..1}. cmod (h t - q t) < e"
       and paq:  "contour_integral q (λw. 1 / (w - z)) = complex_of_real (2 * pi) * 𝗂 * winding_number h z"
    using winding_number [OF ‹path h› pah ‹0 < e›] by blast
  have gp: "homotopic_loops (- {z}) g p"
    by (simp add: gloop d gp_less norm_minus_commute p valid_path_imp_path)
  have hq: "homotopic_loops (- {z}) h q"
    by (simp add: e hloop hq_less norm_minus_commute q valid_path_imp_path)
  have "contour_integral p (λw. 1/(w - z)) = contour_integral q (λw. 1/(w - z))"
    apply (rule Cauchy_theorem_homotopic_loops [of "-{z}"])
    apply (blast intro: homotopic_loops_trans homotopic_loops_sym gp hq assms)
    apply (auto intro!: holomorphic_intros simp: p q)
    done
  then show ?thesis
    by (simp add: pap paq)
qed

lemma winding_number_paths_linear_eq:
  "⟦path g; path h; pathstart h = pathstart g; pathfinish h = pathfinish g;
    ⋀t. t ∈ {0..1} ⟹ z ∉ closed_segment (g t) (h t)⟧
        ⟹ winding_number h z = winding_number g z"
  by (blast intro: sym homotopic_paths_linear winding_number_homotopic_paths elim: )

lemma winding_number_loops_linear_eq:
  "⟦path g; path h; pathfinish g = pathstart g; pathfinish h = pathstart h;
    ⋀t. t ∈ {0..1} ⟹ z ∉ closed_segment (g t) (h t)⟧
        ⟹ winding_number h z = winding_number g z"
  by (blast intro: sym homotopic_loops_linear winding_number_homotopic_loops elim: )

lemma winding_number_nearby_paths_eq:
     "⟦path g; path h;
      pathstart h = pathstart g; pathfinish h = pathfinish g;
      ⋀t. t ∈ {0..1} ⟹ norm(h t - g t) < norm(g t - z)⟧
      ⟹ winding_number h z = winding_number g z"
  by (metis segment_bound(2) norm_minus_commute not_le winding_number_paths_linear_eq)

lemma winding_number_nearby_loops_eq:
     "⟦path g; path h;
      pathfinish g = pathstart g;
        pathfinish h = pathstart h;
      ⋀t. t ∈ {0..1} ⟹ norm(h t - g t) < norm(g t - z)⟧
      ⟹ winding_number h z = winding_number g z"
  by (metis segment_bound(2) norm_minus_commute not_le winding_number_loops_linear_eq)


proposition winding_number_subpath_combine:
    "⟦path g; z ∉ path_image g;
      u ∈ {0..1}; v ∈ {0..1}; w ∈ {0..1}⟧
      ⟹ winding_number (subpath u v g) z + winding_number (subpath v w g) z =
          winding_number (subpath u w g) z"
apply (rule trans [OF winding_number_join [THEN sym]
                      winding_number_homotopic_paths [OF homotopic_join_subpaths]])
apply (auto dest: path_image_subpath_subset)
done


subsection‹Partial circle path›

definition part_circlepath :: "[complex, real, real, real, real] ⇒ complex"
  where "part_circlepath z r s t ≡ λx. z + of_real r * exp (ii * of_real (linepath s t x))"

lemma pathstart_part_circlepath [simp]:
     "pathstart(part_circlepath z r s t) = z + r*exp(ii * s)"
by (metis part_circlepath_def pathstart_def pathstart_linepath)

lemma pathfinish_part_circlepath [simp]:
     "pathfinish(part_circlepath z r s t) = z + r*exp(ii*t)"
by (metis part_circlepath_def pathfinish_def pathfinish_linepath)

proposition has_vector_derivative_part_circlepath [derivative_intros]:
    "((part_circlepath z r s t) has_vector_derivative
      (ii * r * (of_real t - of_real s) * exp(ii * linepath s t x)))
     (at x within X)"
  apply (simp add: part_circlepath_def linepath_def scaleR_conv_of_real)
  apply (rule has_vector_derivative_real_complex)
  apply (rule derivative_eq_intros | simp)+
  done

corollary vector_derivative_part_circlepath:
    "vector_derivative (part_circlepath z r s t) (at x) =
       ii * r * (of_real t - of_real s) * exp(ii * linepath s t x)"
  using has_vector_derivative_part_circlepath vector_derivative_at by blast

corollary vector_derivative_part_circlepath01:
    "⟦0 ≤ x; x ≤ 1⟧
     ⟹ vector_derivative (part_circlepath z r s t) (at x within {0..1}) =
          ii * r * (of_real t - of_real s) * exp(ii * linepath s t x)"
  using has_vector_derivative_part_circlepath
  by (auto simp: vector_derivative_at_within_ivl)

lemma valid_path_part_circlepath [simp]: "valid_path (part_circlepath z r s t)"
  apply (simp add: valid_path_def)
  apply (rule C1_differentiable_imp_piecewise)
  apply (auto simp: C1_differentiable_on_eq vector_derivative_works vector_derivative_part_circlepath has_vector_derivative_part_circlepath
              intro!: continuous_intros)
  done

lemma path_part_circlepath [simp]: "path (part_circlepath z r s t)"
  by (simp add: valid_path_imp_path)

proposition path_image_part_circlepath:
  assumes "s ≤ t"
    shows "path_image (part_circlepath z r s t) = {z + r * exp(ii * of_real x) | x. s ≤ x ∧ x ≤ t}"
proof -
  { fix z::real
    assume "0 ≤ z" "z ≤ 1"
    with ‹s ≤ t› have "∃x. (exp (𝗂 * linepath s t z) = exp (𝗂 * of_real x)) ∧ s ≤ x ∧ x ≤ t"
      apply (rule_tac x="(1 - z) * s + z * t" in exI)
      apply (simp add: linepath_def scaleR_conv_of_real algebra_simps)
      apply (rule conjI)
      using mult_right_mono apply blast
      using affine_ineq  by (metis "mult.commute")
  }
  moreover
  { fix z
    assume "s ≤ z" "z ≤ t"
    then have "z + of_real r * exp (𝗂 * of_real z) ∈ (λx. z + of_real r * exp (𝗂 * linepath s t x)) ` {0..1}"
      apply (rule_tac x="(z - s)/(t - s)" in image_eqI)
      apply (simp add: linepath_def scaleR_conv_of_real divide_simps exp_eq)
      apply (auto simp: algebra_simps divide_simps)
      done
  }
  ultimately show ?thesis
    by (fastforce simp add: path_image_def part_circlepath_def)
qed

corollary path_image_part_circlepath_subset:
    "⟦s ≤ t; 0 ≤ r⟧ ⟹ path_image(part_circlepath z r s t) ⊆ sphere z r"
by (auto simp: path_image_part_circlepath sphere_def dist_norm algebra_simps norm_mult)

proposition in_path_image_part_circlepath:
  assumes "w ∈ path_image(part_circlepath z r s t)" "s ≤ t" "0 ≤ r"
    shows "norm(w - z) = r"
proof -
  have "w ∈ {c. dist z c = r}"
    by (metis (no_types) path_image_part_circlepath_subset sphere_def subset_eq assms)
  thus ?thesis
    by (simp add: dist_norm norm_minus_commute)
qed

proposition finite_bounded_log: "finite {z::complex. norm z ≤ b ∧ exp z = w}"
proof (cases "w = 0")
  case True then show ?thesis by auto
next
  case False
  have *: "finite {x. cmod (complex_of_real (2 * real_of_int x * pi) * 𝗂) ≤ b + cmod (Ln w)}"
    apply (simp add: norm_mult finite_int_iff_bounded_le)
    apply (rule_tac x="⌊(b + cmod (Ln w)) / (2*pi)⌋" in exI)
    apply (auto simp: divide_simps le_floor_iff)
    done
  have [simp]: "⋀P f. {z. P z ∧ (∃n. z = f n)} = f ` {n. P (f n)}"
    by blast
  show ?thesis
    apply (subst exp_Ln [OF False, symmetric])
    apply (simp add: exp_eq)
    using norm_add_leD apply (fastforce intro: finite_subset [OF _ *])
    done
qed

lemma finite_bounded_log2:
  fixes a::complex
    assumes "a ≠ 0"
    shows "finite {z. norm z ≤ b ∧ exp(a*z) = w}"
proof -
  have *: "finite ((λz. z / a) ` {z. cmod z ≤ b * cmod a ∧ exp z = w})"
    by (rule finite_imageI [OF finite_bounded_log])
  show ?thesis
    by (rule finite_subset [OF _ *]) (force simp: assms norm_mult)
qed

proposition has_contour_integral_bound_part_circlepath_strong:
  assumes fi: "(f has_contour_integral i) (part_circlepath z r s t)"
      and "finite k" and le: "0 ≤ B" "0 < r" "s ≤ t"
      and B: "⋀x. x ∈ path_image(part_circlepath z r s t) - k ⟹ norm(f x) ≤ B"
    shows "cmod i ≤ B * r * (t - s)"
proof -
  consider "s = t" | "s < t" using ‹s ≤ t› by linarith
  then show ?thesis
  proof cases
    case 1 with fi [unfolded has_contour_integral]
    have "i = 0"  by (simp add: vector_derivative_part_circlepath)
    with assms show ?thesis by simp
  next
    case 2
    have [simp]: "¦r¦ = r" using ‹r > 0› by linarith
    have [simp]: "cmod (complex_of_real t - complex_of_real s) = t-s"
      by (metis "2" abs_of_pos diff_gt_0_iff_gt norm_of_real of_real_diff)
    have "finite (part_circlepath z r s t -` {y} ∩ {0..1})" if "y ∈ k" for y
    proof -
      def w  "(y - z)/of_real r / exp(ii * of_real s)"
      have fin: "finite (of_real -` {z. cmod z ≤ 1 ∧ exp (𝗂 * complex_of_real (t - s) * z) = w})"
        apply (rule finite_vimageI [OF finite_bounded_log2])
        using ‹s < t› apply (auto simp: inj_of_real)
        done
      show ?thesis
        apply (simp add: part_circlepath_def linepath_def vimage_def)
        apply (rule finite_subset [OF _ fin])
        using le
        apply (auto simp: w_def algebra_simps scaleR_conv_of_real exp_add exp_diff)
        done
    qed
    then have fin01: "finite ((part_circlepath z r s t) -` k ∩ {0..1})"
      by (rule finite_finite_vimage_IntI [OF ‹finite k›])
    have **: "((λx. if (part_circlepath z r s t x) ∈ k then 0
                    else f(part_circlepath z r s t x) *
                       vector_derivative (part_circlepath z r s t) (at x)) has_integral i)  {0..1}"
      apply (rule has_integral_spike
              [where f = "λx. f(part_circlepath z r s t x) * vector_derivative (part_circlepath z r s t) (at x)"])
      apply (rule negligible_finite [OF fin01])
      using fi has_contour_integral
      apply auto
      done
    have *: "⋀x. ⟦0 ≤ x; x ≤ 1; part_circlepath z r s t x ∉ k⟧ ⟹ cmod (f (part_circlepath z r s t x)) ≤ B"
      by (auto intro!: B [unfolded path_image_def image_def, simplified])
    show ?thesis
      apply (rule has_integral_bound [where 'a=real, simplified, OF _ **, simplified])
      using assms apply force
      apply (simp add: norm_mult vector_derivative_part_circlepath)
      using le * "2" ‹r > 0› by auto
  qed
qed

corollary has_contour_integral_bound_part_circlepath:
      "⟦(f has_contour_integral i) (part_circlepath z r s t);
        0 ≤ B; 0 < r; s ≤ t;
        ⋀x. x ∈ path_image(part_circlepath z r s t) ⟹ norm(f x) ≤ B⟧
       ⟹ norm i ≤ B*r*(t - s)"
  by (auto intro: has_contour_integral_bound_part_circlepath_strong)

proposition contour_integrable_continuous_part_circlepath:
     "continuous_on (path_image (part_circlepath z r s t)) f
      ⟹ f contour_integrable_on (part_circlepath z r s t)"
  apply (simp add: contour_integrable_on has_contour_integral_def vector_derivative_part_circlepath path_image_def)
  apply (rule integrable_continuous_real)
  apply (fast intro: path_part_circlepath [unfolded path_def] continuous_intros continuous_on_compose2 [where g=f, OF _ _ order_refl])
  done

proposition winding_number_part_circlepath_pos_less:
  assumes "s < t" and no: "norm(w - z) < r"
    shows "0 < Re (winding_number(part_circlepath z r s t) w)"
proof -
  have "0 < r" by (meson no norm_not_less_zero not_le order.strict_trans2)
  note valid_path_part_circlepath
  moreover have " w ∉ path_image (part_circlepath z r s t)"
    using assms by (auto simp: path_image_def image_def part_circlepath_def norm_mult linepath_def)
  moreover have "0 < r * (t - s) * (r - cmod (w - z))"
    using assms by (metis ‹0 < r› diff_gt_0_iff_gt mult_pos_pos)
  ultimately show ?thesis
    apply (rule winding_number_pos_lt [where e = "r*(t - s)*(r - norm(w - z))"])
    apply (simp add: vector_derivative_part_circlepath right_diff_distrib [symmetric] mult_ac)
    apply (rule mult_left_mono)+
    using Re_Im_le_cmod [of "w-z" "linepath s t x" for x]
    apply (simp add: exp_Euler cos_of_real sin_of_real part_circlepath_def algebra_simps cos_squared_eq [unfolded power2_eq_square])
    using assms ‹0 < r› by auto
qed

proposition simple_path_part_circlepath:
    "simple_path(part_circlepath z r s t) ⟷ (r ≠ 0 ∧ s ≠ t ∧ ¦s - t¦ ≤ 2*pi)"
proof (cases "r = 0 ∨ s = t")
  case True
  then show ?thesis
    apply (rule disjE)
    apply (force simp: part_circlepath_def simple_path_def intro: bexI [where x = "1/4"] bexI [where x = "1/3"])+
    done
next
  case False then have "r ≠ 0" "s ≠ t" by auto
  have *: "⋀x y z s t. ii*((1 - x) * s + x * t) = ii*(((1 - y) * s + y * t)) + z  ⟷ ii*(x - y) * (t - s) = z"
    by (simp add: algebra_simps)
  have abs01: "⋀x y::real. 0 ≤ x ∧ x ≤ 1 ∧ 0 ≤ y ∧ y ≤ 1
                      ⟹ (x = y ∨ x = 0 ∧ y = 1 ∨ x = 1 ∧ y = 0 ⟷ ¦x - y¦ ∈ {0,1})"
    by auto
  have abs_away: "⋀P. (∀x∈{0..1}. ∀y∈{0..1}. P ¦x - y¦) ⟷ (∀x::real. 0 ≤ x ∧ x ≤ 1 ⟶ P x)"
    by force
  have **: "⋀x y. (∃n. (complex_of_real x - of_real y) * (of_real t - of_real s) = 2 * (of_int n * of_real pi)) ⟷
                  (∃n. ¦x - y¦ * (t - s) = 2 * (of_int n * pi))"
    by (force simp: algebra_simps abs_if dest: arg_cong [where f=Re] arg_cong [where f=complex_of_real]
                    intro: exI [where x = "-n" for n])
  have 1: "∀x. 0 ≤ x ∧ x ≤ 1 ⟶ (∃n. x * (t - s) = 2 * (real_of_int n * pi)) ⟶ x = 0 ∨ x = 1 ⟹ ¦s - t¦ ≤ 2 * pi"
    apply (rule ccontr)
    apply (drule_tac x="2*pi / ¦t - s¦" in spec)
    using False
    apply (simp add: abs_minus_commute divide_simps)
    apply (frule_tac x=1 in spec)
    apply (drule_tac x="-1" in spec)
    apply (simp add:)
    done
  have 2: "¦s - t¦ = ¦2 * (real_of_int n * pi) / x¦" if "x ≠ 0" "x * (t - s) = 2 * (real_of_int n * pi)" for x n
  proof -
    have "t-s = 2 * (real_of_int n * pi)/x"
      using that by (simp add: field_simps)
    then show ?thesis by (metis abs_minus_commute)
  qed
  show ?thesis using False
    apply (simp add: simple_path_def path_part_circlepath)
    apply (simp add: part_circlepath_def linepath_def exp_eq  * ** abs01  del: Set.insert_iff)
    apply (subst abs_away)
    apply (auto simp: 1)
    apply (rule ccontr)
    apply (auto simp: 2 divide_simps abs_mult dest: of_int_leD)
    done
qed

proposition arc_part_circlepath:
  assumes "r ≠ 0" "s ≠ t" "¦s - t¦ < 2*pi"
    shows "arc (part_circlepath z r s t)"
proof -
  have *: "x = y" if eq: "𝗂 * (linepath s t x) = 𝗂 * (linepath s t y) + 2 * of_int n * complex_of_real pi * 𝗂"
                  and x: "x ∈ {0..1}" and y: "y ∈ {0..1}" for x y n
    proof -
      have "(linepath s t x) = (linepath s t y) + 2 * of_int n * complex_of_real pi"
        by (metis add_divide_eq_iff complex_i_not_zero mult.commute nonzero_mult_divide_cancel_left eq)
      then have "s*y + t*x = s*x + (t*y + of_int n * (pi * 2))"
        by (force simp: algebra_simps linepath_def dest: arg_cong [where f=Re])
      then have st: "x ≠ y ⟹ (s-t) = (of_int n * (pi * 2) / (y-x))"
        by (force simp: field_simps)
      show ?thesis
        apply (rule ccontr)
        using assms x y
        apply (simp add: st abs_mult field_simps)
        using st
        apply (auto simp: dest: of_int_lessD)
        done
    qed
  show ?thesis
    using assms
    apply (simp add: arc_def)
    apply (simp add: part_circlepath_def inj_on_def exp_eq)
    apply (blast intro: *)
    done
qed


subsection‹Special case of one complete circle›

definition circlepath :: "[complex, real, real] ⇒ complex"
  where "circlepath z r ≡ part_circlepath z r 0 (2*pi)"

lemma circlepath: "circlepath z r = (λx. z + r * exp(2 * of_real pi * ii * of_real x))"
  by (simp add: circlepath_def part_circlepath_def linepath_def algebra_simps)

lemma pathstart_circlepath [simp]: "pathstart (circlepath z r) = z + r"
  by (simp add: circlepath_def)

lemma pathfinish_circlepath [simp]: "pathfinish (circlepath z r) = z + r"
  by (simp add: circlepath_def) (metis exp_two_pi_i mult.commute)

lemma circlepath_minus: "circlepath z (-r) x = circlepath z r (x + 1/2)"
proof -
  have "z + of_real r * exp (2 * pi * 𝗂 * (x + 1 / 2)) =
        z + of_real r * exp (2 * pi * 𝗂 * x + pi * 𝗂)"
    by (simp add: divide_simps) (simp add: algebra_simps)
  also have "... = z - r * exp (2 * pi * 𝗂 * x)"
    by (simp add: exp_add)
  finally show ?thesis
    by (simp add: circlepath path_image_def sphere_def dist_norm)
qed

lemma circlepath_add1: "circlepath z r (x+1) = circlepath z r x"
  using circlepath_minus [of z r "x+1/2"] circlepath_minus [of z "-r" x]
  by (simp add: add.commute)

lemma circlepath_add_half: "circlepath z r (x + 1/2) = circlepath z r (x - 1/2)"
  using circlepath_add1 [of z r "x-1/2"]
  by (simp add: add.commute)

lemma path_image_circlepath_minus_subset:
     "path_image (circlepath z (-r)) ⊆ path_image (circlepath z r)"
  apply (simp add: path_image_def image_def circlepath_minus, clarify)
  apply (case_tac "xa ≤ 1/2", force)
  apply (force simp add: circlepath_add_half)+
  done

lemma path_image_circlepath_minus: "path_image (circlepath z (-r)) = path_image (circlepath z r)"
  using path_image_circlepath_minus_subset by fastforce

proposition has_vector_derivative_circlepath [derivative_intros]:
 "((circlepath z r) has_vector_derivative (2 * pi * ii * r * exp (2 * of_real pi * ii * of_real x)))
   (at x within X)"
  apply (simp add: circlepath_def scaleR_conv_of_real)
  apply (rule derivative_eq_intros)
  apply (simp add: algebra_simps)
  done

corollary vector_derivative_circlepath:
   "vector_derivative (circlepath z r) (at x) =
    2 * pi * ii * r * exp(2 * of_real pi * ii * x)"
using has_vector_derivative_circlepath vector_derivative_at by blast

corollary vector_derivative_circlepath01:
    "⟦0 ≤ x; x ≤ 1⟧
     ⟹ vector_derivative (circlepath z r) (at x within {0..1}) =
          2 * pi * ii * r * exp(2 * of_real pi * ii * x)"
  using has_vector_derivative_circlepath
  by (auto simp: vector_derivative_at_within_ivl)

lemma valid_path_circlepath [simp]: "valid_path (circlepath z r)"
  by (simp add: circlepath_def)

lemma path_circlepath [simp]: "path (circlepath z r)"
  by (simp add: valid_path_imp_path)

lemma path_image_circlepath_nonneg:
  assumes "0 ≤ r" shows "path_image (circlepath z r) = sphere z r"
proof -
  have *: "x ∈ (λu. z + (cmod (x - z)) * exp (𝗂 * (of_real u * (of_real pi * 2)))) ` {0..1}" for x
  proof (cases "x = z")
    case True then show ?thesis by force
  next
    case False
    def w  "x - z"
    then have "w ≠ 0" by (simp add: False)
    have **: "⋀t. ⟦Re w = cos t * cmod w; Im w = sin t * cmod w⟧ ⟹ w = of_real (cmod w) * exp (𝗂 * t)"
      using cis_conv_exp complex_eq_iff by auto
    show ?thesis
      apply (rule sincos_total_2pi [of "Re(w/of_real(norm w))" "Im(w/of_real(norm w))"])
      apply (simp add: divide_simps ‹w ≠ 0› cmod_power2 [symmetric])
      apply (rule_tac x="t / (2*pi)" in image_eqI)
      apply (simp add: divide_simps ‹w ≠ 0›)
      using False **
      apply (auto simp: w_def)
      done
  qed
  show ?thesis
    unfolding circlepath path_image_def sphere_def dist_norm
    by (force simp: assms algebra_simps norm_mult norm_minus_commute intro: *)
qed

proposition path_image_circlepath [simp]:
    "path_image (circlepath z r) = sphere z ¦r¦"
  using path_image_circlepath_minus
  by (force simp add: path_image_circlepath_nonneg abs_if)

lemma has_contour_integral_bound_circlepath_strong:
      "⟦(f has_contour_integral i) (circlepath z r);
        finite k; 0 ≤ B; 0 < r;
        ⋀x. ⟦norm(x - z) = r; x ∉ k⟧ ⟹ norm(f x) ≤ B⟧
        ⟹ norm i ≤ B*(2*pi*r)"
  unfolding circlepath_def
  by (auto simp: algebra_simps in_path_image_part_circlepath dest!: has_contour_integral_bound_part_circlepath_strong)

corollary has_contour_integral_bound_circlepath:
      "⟦(f has_contour_integral i) (circlepath z r);
        0 ≤ B; 0 < r; ⋀x. norm(x - z) = r ⟹ norm(f x) ≤ B⟧
        ⟹ norm i ≤ B*(2*pi*r)"
  by (auto intro: has_contour_integral_bound_circlepath_strong)

proposition contour_integrable_continuous_circlepath:
    "continuous_on (path_image (circlepath z r)) f
     ⟹ f contour_integrable_on (circlepath z r)"
  by (simp add: circlepath_def contour_integrable_continuous_part_circlepath)

lemma simple_path_circlepath: "simple_path(circlepath z r) ⟷ (r ≠ 0)"
  by (simp add: circlepath_def simple_path_part_circlepath)

lemma notin_path_image_circlepath [simp]: "cmod (w - z) < r ⟹ w ∉ path_image (circlepath z r)"
  by (simp add: sphere_def dist_norm norm_minus_commute)

proposition contour_integral_circlepath:
     "0 < r ⟹ contour_integral (circlepath z r) (λw. 1 / (w - z)) = 2 * complex_of_real pi * 𝗂"
  apply (rule contour_integral_unique)
  apply (simp add: has_contour_integral_def)
  apply (subst has_integral_cong)
  apply (simp add: vector_derivative_circlepath01)
  using has_integral_const_real [of _ 0 1]
  apply (force simp: circlepath)
  done

lemma winding_number_circlepath_centre: "0 < r ⟹ winding_number (circlepath z r) z = 1"
  apply (rule winding_number_unique_loop)
  apply (simp_all add: sphere_def valid_path_imp_path)
  apply (rule_tac x="circlepath z r" in exI)
  apply (simp add: sphere_def contour_integral_circlepath)
  done

proposition winding_number_circlepath:
  assumes "norm(w - z) < r" shows "winding_number(circlepath z r) w = 1"
proof (cases "w = z")
  case True then show ?thesis
    using assms winding_number_circlepath_centre by auto
next
  case False
  have [simp]: "r > 0"
    using assms le_less_trans norm_ge_zero by blast
  def r'  "norm(w - z)"
  have "r' < r"
    by (simp add: assms r'_def)
  have disjo: "cball z r' ∩ sphere z r = {}"
    using ‹r' < r› by (force simp: cball_def sphere_def)
  have "winding_number(circlepath z r) w = winding_number(circlepath z r) z"
    apply (rule winding_number_around_inside [where s = "cball z r'"])
    apply (simp_all add: disjo order.strict_implies_order winding_number_circlepath_centre)
    apply (simp_all add: False r'_def dist_norm norm_minus_commute)
    done
  also have "... = 1"
    by (simp add: winding_number_circlepath_centre)
  finally show ?thesis .
qed


text‹ Hence the Cauchy formula for points inside a circle.›

theorem Cauchy_integral_circlepath:
  assumes "continuous_on (cball z r) f" "f holomorphic_on (ball z r)" "norm(w - z) < r"
  shows "((λu. f u/(u - w)) has_contour_integral (2 * of_real pi * ii * f w))
         (circlepath z r)"
proof -
  have "r > 0"
    using assms le_less_trans norm_ge_zero by blast
  have "((λu. f u / (u - w)) has_contour_integral (2 * pi) * 𝗂 * winding_number (circlepath z r) w * f w)
        (circlepath z r)"
    apply (rule Cauchy_integral_formula_weak [where s = "cball z r" and k = "{}"])
    using assms  ‹r > 0›
    apply (simp_all add: dist_norm norm_minus_commute)
    apply (metis at_within_interior dist_norm holomorphic_on_def interior_ball mem_ball norm_minus_commute)
    apply (simp add: cball_def sphere_def dist_norm, clarify)
    apply (simp add:)
    by (metis dist_commute dist_norm less_irrefl)
  then show ?thesis
    by (simp add: winding_number_circlepath assms)
qed

corollary Cauchy_integral_circlepath_simple:
  assumes "f holomorphic_on cball z r" "norm(w - z) < r"
  shows "((λu. f u/(u - w)) has_contour_integral (2 * of_real pi * ii * f w))
         (circlepath z r)"
using assms by (force simp: holomorphic_on_imp_continuous_on holomorphic_on_subset Cauchy_integral_circlepath)


lemma no_bounded_connected_component_imp_winding_number_zero:
  assumes g: "path g" "path_image g ⊆ s" "pathfinish g = pathstart g" "z ∉ s"
      and nb: "⋀z. bounded (connected_component_set (- s) z) ⟶ z ∈ s"
  shows "winding_number g z = 0"
apply (rule winding_number_zero_in_outside)
apply (simp_all add: assms)
by (metis nb [of z] ‹path_image g ⊆ s› ‹z ∉ s› contra_subsetD mem_Collect_eq outside outside_mono)

lemma no_bounded_path_component_imp_winding_number_zero:
  assumes g: "path g" "path_image g ⊆ s" "pathfinish g = pathstart g" "z ∉ s"
      and nb: "⋀z. bounded (path_component_set (- s) z) ⟶ z ∈ s"
  shows "winding_number g z = 0"
apply (rule no_bounded_connected_component_imp_winding_number_zero [OF g])
by (simp add: bounded_subset nb path_component_subset_connected_component)


subsection‹ Uniform convergence of path integral›

text‹Uniform convergence when the derivative of the path is bounded, and in particular for the special case of a circle.›

proposition contour_integral_uniform_limit:
  assumes ev_fint: "eventually (λn::'a. (f n) contour_integrable_on γ) F"
      and ev_no: "⋀e. 0 < e ⟹ eventually (λn. ∀x ∈ path_image γ. norm(f n x - l x) < e) F"
      and noleB: "⋀t. t ∈ {0..1} ⟹ norm (vector_derivative γ (at t)) ≤ B"
      and γ: "valid_path γ"
      and [simp]: "~ (trivial_limit F)"
  shows "l contour_integrable_on γ" "((λn. contour_integral γ (f n)) ⤏ contour_integral γ l) F"
proof -
  have "0 ≤ B" by (meson noleB [of 0] atLeastAtMost_iff norm_ge_zero order_refl order_trans zero_le_one)
  { fix e::real
    assume "0 < e"
    then have eB: "0 < e / (¦B¦ + 1)" by simp
    obtain a where fga: "⋀x. x ∈ {0..1} ⟹ cmod (f a (γ x) - l (γ x)) < e / (¦B¦ + 1)"
               and inta: "(λt. f a (γ t) * vector_derivative γ (at t)) integrable_on {0..1}"
      using eventually_happens [OF eventually_conj [OF ev_no [OF eB] ev_fint]]
      by (fastforce simp: contour_integrable_on path_image_def)
    have Ble: "B * e / (¦B¦ + 1) ≤ e"
      using ‹0 ≤ B›  ‹0 < e› by (simp add: divide_simps)
    have "∃h. (∀x∈{0..1}. cmod (l (γ x) * vector_derivative γ (at x) - h x) ≤ e) ∧ h integrable_on {0..1}"
      apply (rule_tac x="λx. f (a::'a) (γ x) * vector_derivative γ (at x)" in exI)
      apply (intro inta conjI ballI)
      apply (rule order_trans [OF _ Ble])
      apply (frule noleB)
      apply (frule fga)
      using ‹0 ≤ B›  ‹0 < e›
      apply (simp add: norm_mult left_diff_distrib [symmetric] norm_minus_commute divide_simps)
      apply (drule (1) mult_mono [OF less_imp_le])
      apply (simp_all add: mult_ac)
      done
  }
  then show lintg: "l contour_integrable_on γ"
    apply (simp add: contour_integrable_on)
    apply (blast intro: integrable_uniform_limit_real)
    done
  { fix e::real
    def B'  "B+1"
    have B': "B' > 0" "B' > B" using  ‹0 ≤ B› by (auto simp: B'_def)
    assume "0 < e"
    then have ev_no': "∀F n in F. ∀x∈path_image γ. 2 * cmod (f n x - l x) < e / B'"
      using ev_no [of "e / B' / 2"] B' by (simp add: field_simps)
    have ie: "integral {0..1::real} (λx. e / 2) < e" using ‹0 < e› by simp
    have *: "cmod (f x (γ t) * vector_derivative γ (at t) - l (γ t) * vector_derivative γ (at t)) ≤ e / 2"
             if t: "t∈{0..1}" and leB': "2 * cmod (f x (γ t) - l (γ t)) < e / B'" for x t
    proof -
      have "2 * cmod (f x (γ t) - l (γ t)) * cmod (vector_derivative γ (at t)) ≤ e * (B/ B')"
        using mult_mono [OF less_imp_le [OF leB'] noleB] B' ‹0 < e› t by auto
      also have "... < e"
        by (simp add: B' ‹0 < e› mult_imp_div_pos_less)
      finally have "2 * cmod (f x (γ t) - l (γ t)) * cmod (vector_derivative γ (at t)) < e" .
      then show ?thesis
        by (simp add: left_diff_distrib [symmetric] norm_mult)
    qed
    have "∀F x in F. dist (contour_integral γ (f x)) (contour_integral γ l) < e"
      apply (rule eventually_mono [OF eventually_conj [OF ev_no' ev_fint]])
      apply (simp add: dist_norm contour_integrable_on path_image_def contour_integral_integral)
      apply (simp add: lintg integral_diff [symmetric] contour_integrable_on [symmetric], clarify)
      apply (rule le_less_trans [OF integral_norm_bound_integral ie])
      apply (simp add: lintg integrable_diff contour_integrable_on [symmetric])
      apply (blast intro: *)+
      done
  }
  then show "((λn. contour_integral γ (f n)) ⤏ contour_integral γ l) F"
    by (rule tendstoI)
qed

proposition contour_integral_uniform_limit_circlepath:
  assumes ev_fint: "eventually (λn::'a. (f n) contour_integrable_on (circlepath z r)) F"
      and ev_no: "⋀e. 0 < e ⟹ eventually (λn. ∀x ∈ path_image (circlepath z r). norm(f n x - l x) < e) F"
      and [simp]: "~ (trivial_limit F)" "0 < r"
  shows "l contour_integrable_on (circlepath z r)" "((λn. contour_integral (circlepath z r) (f n)) ⤏ contour_integral (circlepath z r) l) F"
by (auto simp: vector_derivative_circlepath norm_mult intro: contour_integral_uniform_limit assms)


subsection‹ General stepping result for derivative formulas.›

lemma sum_sqs_eq:
  fixes x::"'a::idom" shows "x * x + y * y = x * (y * 2) ⟹ y = x"
  by algebra

proposition Cauchy_next_derivative:
  assumes "continuous_on (path_image γ) f'"
      and leB: "⋀t. t ∈ {0..1} ⟹ norm (vector_derivative γ (at t)) ≤ B"
      and int: "⋀w. w ∈ s - path_image γ ⟹ ((λu. f' u / (u - w)^k) has_contour_integral f w) γ"
      and k: "k ≠ 0"
      and "open s"
      and γ: "valid_path γ"
      and w: "w ∈ s - path_image γ"
    shows "(λu. f' u / (u - w)^(Suc k)) contour_integrable_on γ"
      and "(f has_field_derivative (k * contour_integral γ (λu. f' u/(u - w)^(Suc k))))
           (at w)"  (is "?thes2")
proof -
  have "open (s - path_image γ)" using ‹open s› closed_valid_path_image γ by blast
  then obtain d where "d>0" and d: "ball w d ⊆ s - path_image γ" using w
    using open_contains_ball by blast
  have [simp]: "⋀n. cmod (1 + of_nat n) = 1 + of_nat n"
    by (metis norm_of_nat of_nat_Suc)
  have 1: "∀F n in at w. (λx. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k)
                         contour_integrable_on γ"
    apply (simp add: eventually_at)
    apply (rule_tac x=d in exI)
    apply (simp add: ‹d > 0› dist_norm field_simps, clarify)
    apply (rule contour_integrable_div [OF contour_integrable_diff])
    using int w d
    apply (force simp:  dist_norm norm_minus_commute intro!: has_contour_integral_integrable)+
    done
  have bim_g: "bounded (image f' (path_image γ))"
    by (simp add: compact_imp_bounded compact_continuous_image compact_valid_path_image assms)
  then obtain C where "C > 0" and C: "⋀x. ⟦0 ≤ x; x ≤ 1⟧ ⟹ cmod (f' (γ x)) ≤ C"
    by (force simp: bounded_pos path_image_def)
  have twom: "∀F n in at w.
               ∀x∈path_image γ.
                cmod ((inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / k - inverse (x - w) ^ Suc k) < e"
         if "0 < e" for e
  proof -
    have *: "cmod ((inverse (x - u) ^ k - inverse (x - w) ^ k) / ((u - w) * k) - inverse (x - w) ^ Suc k)   < e"
            if x: "x ∈ path_image γ" and "u ≠ w" and uwd: "cmod (u - w) < d/2"
                and uw_less: "cmod (u - w) < e * (d / 2) ^ (k+2) / (1 + real k)"
            for u x
    proof -
      def ff  "λn::nat. λw. if n = 0 then inverse(x - w)^k
                              else if n = 1 then k / (x - w)^(Suc k)
                              else (k * of_real(Suc k)) / (x - w)^(k + 2)"
      have km1: "⋀z::complex. z ≠ 0 ⟹ z ^ (k - Suc 0) = z ^ k / z"
        by (simp add: field_simps) (metis Suc_pred ‹k ≠ 0› neq0_conv power_Suc)
      have ff1: "(ff i has_field_derivative ff (Suc i) z) (at z within ball w (d / 2))"
              if "z ∈ ball w (d / 2)" "i ≤ 1" for i z
      proof -
        have "z ∉ path_image γ"
          using ‹x ∈ path_image γ› d that ball_divide_subset_numeral by blast
        then have xz[simp]: "x ≠ z" using ‹x ∈ path_image γ› by blast
        then have neq: "x * x + z * z ≠ x * (z * 2)"
          by (blast intro: dest!: sum_sqs_eq)
        with xz have "⋀v. v ≠ 0 ⟹ (x * x + z * z) * v ≠ (x * (z * 2) * v)" by auto
        then have neqq: "⋀v. v ≠ 0 ⟹ x * (x * v) + z * (z * v) ≠ x * (z * (2 * v))"
          by (simp add: algebra_simps)
        show ?thesis using ‹i ≤ 1›
          apply (simp add: ff_def dist_norm Nat.le_Suc_eq km1, safe)
          apply (rule derivative_eq_intros | simp add: km1 | simp add: field_simps neq neqq)+
          done
      qed
      { fix a::real and b::real assume ab: "a > 0" "b > 0"
        then have "k * (1 + real k) * (1 / a) ≤ k * (1 + real k) * (4 / b) ⟷ b ≤ 4 * a"
          apply (subst mult_le_cancel_left_pos)
          using ‹k ≠ 0›
          apply (auto simp: divide_simps)
          done
        with ab have "real k * (1 + real k) / a ≤ (real k * 4 + real k * real k * 4) / b ⟷ b ≤ 4 * a"
          by (simp add: field_simps)
      } note canc = this
      have ff2: "cmod (ff (Suc 1) v) ≤ real (k * (k + 1)) / (d / 2) ^ (k + 2)"
                if "v ∈ ball w (d / 2)" for v
      proof -
        have "d/2 ≤ cmod (x - v)" using d x that
          apply (simp add: dist_norm path_image_def ball_def not_less [symmetric] del: divide_const_simps, clarify)
          apply (drule subsetD)
           prefer 2 apply blast
          apply (metis norm_minus_commute norm_triangle_half_r CollectI)
          done
        then have "d ≤ cmod (x - v) * 2"
          by (simp add: divide_simps)
        then have dpow_le: "d ^ (k+2) ≤ (cmod (x - v) * 2) ^ (k+2)"
          using ‹0 < d› order_less_imp_le power_mono by blast
        have "x ≠ v" using that
          using ‹x ∈ path_image γ› ball_divide_subset_numeral d by fastforce
        then show ?thesis
        using ‹d > 0›
        apply (simp add: ff_def norm_mult norm_divide norm_power dist_norm canc)
        using dpow_le
        apply (simp add: algebra_simps divide_simps mult_less_0_iff)
        done
      qed
      have ub: "u ∈ ball w (d / 2)"
        using uwd by (simp add: dist_commute dist_norm)
      have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
                  ≤ (real k * 4 + real k * real k * 4) * (cmod (u - w) * cmod (u - w)) / (d * (d * (d / 2) ^ k))"
        using complex_taylor [OF _ ff1 ff2 _ ub, of w, simplified]
        by (simp add: ff_def ‹0 < d›)
      then have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
                  ≤ (cmod (u - w) * real k) * (1 + real k) * cmod (u - w) / (d / 2) ^ (k+2)"
        by (simp add: field_simps)
      then have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
                 / (cmod (u - w) * real k)
                  ≤ (1 + real k) * cmod (u - w) / (d / 2) ^ (k+2)"
        using ‹k ≠ 0› ‹u ≠ w› by (simp add: mult_ac zero_less_mult_iff pos_divide_le_eq)
      also have "... < e"
        using uw_less ‹0 < d› by (simp add: mult_ac divide_simps)
      finally have e: "cmod (inverse (x-u)^k - (inverse (x-w)^k + of_nat k * (u-w) / ((x-w) * (x-w)^k)))
                        / cmod ((u - w) * real k)   <   e"
        by (simp add: norm_mult)
      have "x ≠ u"
        using uwd ‹0 < d› x d by (force simp: dist_norm ball_def norm_minus_commute)
      show ?thesis
        apply (rule le_less_trans [OF _ e])
        using ‹k ≠ 0› ‹x ≠ u›  ‹u ≠ w›
        apply (simp add: field_simps norm_divide [symmetric])
        done
    qed
    show ?thesis
      unfolding eventually_at
      apply (rule_tac x = "min (d/2) ((e*(d/2)^(k + 2))/(Suc k))" in exI)
      apply (force simp: ‹d > 0› dist_norm that simp del: power_Suc intro: *)
      done
  qed
  have 2: "∀F n in at w.
              ∀x∈path_image γ.
               cmod (f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k - f' x / (x - w) ^ Suc k) < e"
          if "0 < e" for e
  proof -
    have *: "cmod (f' (γ x) * (inverse (γ x - u) ^ k - inverse (γ x - w) ^ k) / ((u - w) * k) -
                        f' (γ x) / ((γ x - w) * (γ x - w) ^ k)) < e"
              if ec: "cmod ((inverse (γ x - u) ^ k - inverse (γ x - w) ^ k) / ((u - w) * k) -
                      inverse (γ x - w) * inverse (γ x - w) ^ k) < e / C"
                 and x: "0 ≤ x" "x ≤ 1"
              for u x
    proof (cases "(f' (γ x)) = 0")
      case True then show ?thesis by (simp add: ‹0 < e›)
    next
      case False
      have "cmod (f' (γ x) * (inverse (γ x - u) ^ k - inverse (γ x - w) ^ k) / ((u - w) * k) -
                        f' (γ x) / ((γ x - w) * (γ x - w) ^ k)) =
            cmod (f' (γ x) * ((inverse (γ x - u) ^ k - inverse (γ x - w) ^ k) / ((u - w) * k) -
                             inverse (γ x - w) * inverse (γ x - w) ^ k))"
        by (simp add: field_simps)
      also have "... = cmod (f' (γ x)) *
                       cmod ((inverse (γ x - u) ^ k - inverse (γ x - w) ^ k) / ((u - w) * k) -
                             inverse (γ x - w) * inverse (γ x - w) ^ k)"
        by (simp add: norm_mult)
      also have "... < cmod (f' (γ x)) * (e/C)"
        apply (rule mult_strict_left_mono [OF ec])
        using False by simp
      also have "... ≤ e" using C
        by (metis False ‹0 < e› frac_le less_eq_real_def mult.commute pos_le_divide_eq x zero_less_norm_iff)
      finally show ?thesis .
    qed
    show ?thesis
      using twom [OF divide_pos_pos [OF that ‹C > 0›]]   unfolding path_image_def
      by (force intro: * elim: eventually_mono)
  qed
  show "(λu. f' u / (u - w) ^ (Suc k)) contour_integrable_on γ"
    by (rule contour_integral_uniform_limit [OF 1 2 leB γ]) auto
  have *: "(λn. contour_integral γ (λx. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / k))
           ─w→ contour_integral γ (λu. f' u / (u - w) ^ (Suc k))"
    by (rule contour_integral_uniform_limit [OF 1 2 leB γ]) auto
  have **: "contour_integral γ (λx. f' x * (inverse (x - u) ^ k - inverse (x - w) ^ k) / ((u - w) * k)) =
              (f u - f w) / (u - w) / k"
           if "dist u w < d" for u
    apply (rule contour_integral_unique)
    apply (simp add: diff_divide_distrib algebra_simps)
    apply (rule has_contour_integral_diff; rule has_contour_integral_div; simp add: field_simps; rule int)
    apply (metis contra_subsetD d dist_commute mem_ball that)
    apply (rule w)
    done
  show ?thes2
    apply (simp add: DERIV_within_iff del: power_Suc)
    apply (rule Lim_transform_within [OF tendsto_mult_left [OF *] ‹0 < d› ])
    apply (simp add: ‹k ≠ 0› **)
    done
qed

corollary Cauchy_next_derivative_circlepath:
  assumes contf: "continuous_on (path_image (circlepath z r)) f"
      and int: "⋀w. w ∈ ball z r ⟹ ((λu. f u / (u - w)^k) has_contour_integral g w) (circlepath z r)"
      and k: "k ≠ 0"
      and w: "w ∈ ball z r"
    shows "(λu. f u / (u - w)^(Suc k)) contour_integrable_on (circlepath z r)"
           (is "?thes1")
      and "(g has_field_derivative (k * contour_integral (circlepath z r) (λu. f u/(u - w)^(Suc k)))) (at w)"
           (is "?thes2")
proof -
  have "r > 0" using w
    using ball_eq_empty by fastforce
  have wim: "w ∈ ball z r - path_image (circlepath z r)"
    using w by (auto simp: dist_norm)
  show ?thes1 ?thes2
    by (rule Cauchy_next_derivative [OF contf _ int k open_ball valid_path_circlepath wim, where B = "2 * pi * ¦r¦"];
        auto simp: vector_derivative_circlepath norm_mult)+
qed


text‹ In particular, the first derivative formula.›

proposition Cauchy_derivative_integral_circlepath:
  assumes contf: "continuous_on (cball z r) f"
      and holf: "f holomorphic_on ball z r"
      and w: "w ∈ ball z r"
    shows "(λu. f u/(u - w)^2) contour_integrable_on (circlepath z r)"
           (is "?thes1")
      and "(f has_field_derivative (1 / (2 * of_real pi * ii) * contour_integral(circlepath z r) (λu. f u / (u - w)^2))) (at w)"
           (is "?thes2")
proof -
  have [simp]: "r ≥ 0" using w
    using ball_eq_empty by fastforce
  have f: "continuous_on (path_image (circlepath z r)) f"
    by (rule continuous_on_subset [OF contf]) (force simp add: cball_def sphere_def)
  have int: "⋀w. dist z w < r ⟹
                 ((λu. f u / (u - w)) has_contour_integral (λx. 2 * of_real pi * ii * f x) w) (circlepath z r)"
    by (rule Cauchy_integral_circlepath [OF contf holf]) (simp add: dist_norm norm_minus_commute)
  show ?thes1
    apply (simp add: power2_eq_square)
    apply (rule Cauchy_next_derivative_circlepath [OF f _ _ w, where k=1, simplified])
    apply (blast intro: int)
    done
  have "((λx. 2 * of_real pi * 𝗂 * f x) has_field_derivative contour_integral (circlepath z r) (λu. f u / (u - w)^2)) (at w)"
    apply (simp add: power2_eq_square)
    apply (rule Cauchy_next_derivative_circlepath [OF f _ _ w, where k=1 and g = "λx. 2 * of_real pi * ii * f x", simplified])
    apply (blast intro: int)
    done
  then have fder: "(f has_field_derivative contour_integral (circlepath z r) (λu. f u / (u - w)^2) / (2 * of_real pi * 𝗂)) (at w)"
    by (rule DERIV_cdivide [where f = "λx. 2 * of_real pi * 𝗂 * f x" and c = "2 * of_real pi * 𝗂", simplified])
  show ?thes2
    by simp (rule fder)
qed

subsection‹ Existence of all higher derivatives.›

proposition derivative_is_holomorphic:
  assumes "open s"
      and fder: "⋀z. z ∈ s ⟹ (f has_field_derivative f' z) (at z)"
    shows "f' holomorphic_on s"
proof -
  have *: "∃h. (f' has_field_derivative h) (at z)" if "z ∈ s" for z
  proof -
    obtain r where "r > 0" and r: "cball z r ⊆ s"
      using open_contains_cball ‹z ∈ s› ‹open s› by blast
    then have holf_cball: "f holomorphic_on cball z r"
      apply (simp add: holomorphic_on_def)
      using field_differentiable_at_within field_differentiable_def fder by blast
    then have "continuous_on (path_image (circlepath z r)) f"
      using ‹r > 0› by (force elim: holomorphic_on_subset [THEN holomorphic_on_imp_continuous_on])
    then have contfpi: "continuous_on (path_image (circlepath z r)) (λx. 1/(2 * of_real pi*ii) * f x)"
      by (auto intro: continuous_intros)+
    have contf_cball: "continuous_on (cball z r) f" using holf_cball
      by (simp add: holomorphic_on_imp_continuous_on holomorphic_on_subset)
    have holf_ball: "f holomorphic_on ball z r" using holf_cball
      using ball_subset_cball holomorphic_on_subset by blast
    { fix w  assume w: "w ∈ ball z r"
      have intf: "(λu. f u / (u - w)2) contour_integrable_on circlepath z r"
        by (blast intro: w Cauchy_derivative_integral_circlepath [OF contf_cball holf_ball])
      have fder': "(f has_field_derivative 1 / (2 * of_real pi * 𝗂) * contour_integral (circlepath z r) (λu. f u / (u - w)2))
                  (at w)"
        by (blast intro: w Cauchy_derivative_integral_circlepath [OF contf_cball holf_ball])
      have f'_eq: "f' w = contour_integral (circlepath z r) (λu. f u / (u - w)2) / (2 * of_real pi * 𝗂)"
        using fder' ball_subset_cball r w by (force intro: DERIV_unique [OF fder])
      have "((λu. f u / (u - w)2 / (2 * of_real pi * 𝗂)) has_contour_integral
                contour_integral (circlepath z r) (λu. f u / (u - w)2) / (2 * of_real pi * 𝗂))
                (circlepath z r)"
        by (rule Cauchy_Integral_Thm.has_contour_integral_div [OF has_contour_integral_integral [OF intf]])
      then have "((λu. f u / (2 * of_real pi * 𝗂 * (u - w)2)) has_contour_integral
                contour_integral (circlepath z r) (λu. f u / (u - w)2) / (2 * of_real pi * 𝗂))
                (circlepath z r)"
        by (simp add: algebra_simps)
      then have "((λu. f u / (2 * of_real pi * 𝗂 * (u - w)2)) has_contour_integral f' w) (circlepath z r)"
        by (simp add: f'_eq)
    } note * = this
    show ?thesis
      apply (rule exI)
      apply (rule Cauchy_next_derivative_circlepath [OF contfpi, of 2 f', simplified])
      apply (simp_all add: ‹0 < r› * dist_norm)
      done
  qed
  show ?thesis
    by (simp add: holomorphic_on_open [OF ‹open s›] *)
qed

lemma holomorphic_deriv [holomorphic_intros]:
    "⟦f holomorphic_on s; open s⟧ ⟹ (deriv f) holomorphic_on s"
by (metis DERIV_deriv_iff_field_differentiable at_within_open derivative_is_holomorphic holomorphic_on_def)

lemma analytic_deriv: "f analytic_on s ⟹ (deriv f) analytic_on s"
  using analytic_on_holomorphic holomorphic_deriv by auto

lemma holomorphic_higher_deriv [holomorphic_intros]: "⟦f holomorphic_on s; open s⟧ ⟹ (deriv ^^ n) f holomorphic_on s"
  by (induction n) (auto simp: holomorphic_deriv)

lemma analytic_higher_deriv: "f analytic_on s ⟹ (deriv ^^ n) f analytic_on s"
  unfolding analytic_on_def using holomorphic_higher_deriv by blast

lemma has_field_derivative_higher_deriv:
     "⟦f holomorphic_on s; open s; x ∈ s⟧
      ⟹ ((deriv ^^ n) f has_field_derivative (deriv ^^ (Suc n)) f x) (at x)"
by (metis (no_types, hide_lams) DERIV_deriv_iff_field_differentiable at_within_open comp_apply
         funpow.simps(2) holomorphic_higher_deriv holomorphic_on_def)

lemma valid_path_compose_holomorphic:
  assumes "valid_path g" and holo:"f holomorphic_on s" and "open s" "path_image g ⊆ s"
  shows "valid_path (f o g)"
proof (rule valid_path_compose[OF ‹valid_path g›])
  fix x assume "x ∈ path_image g"
  then show "∃f'. (f has_field_derivative f') (at x)"
    using holo holomorphic_on_open[OF ‹open s›] ‹path_image g ⊆ s› by auto
next
  have "deriv f holomorphic_on s"
    using holomorphic_deriv holo ‹open s› by auto
  then show "continuous_on (path_image g) (deriv f)"
    using assms(4) holomorphic_on_imp_continuous_on holomorphic_on_subset by auto
qed


subsection‹ Morera's theorem.›

lemma Morera_local_triangle_ball:
  assumes "⋀z. z ∈ s
          ⟹ ∃e a. 0 < e ∧ z ∈ ball a e ∧ continuous_on (ball a e) f ∧
                    (∀b c. closed_segment b c ⊆ ball a e
                           ⟶ contour_integral (linepath a b) f +
                               contour_integral (linepath b c) f +
                               contour_integral (linepath c a) f = 0)"
  shows "f analytic_on s"
proof -
  { fix z  assume "z ∈ s"
    with assms obtain e a where
            "0 < e" and z: "z ∈ ball a e" and contf: "continuous_on (ball a e) f"
        and 0: "⋀b c. closed_segment b c ⊆ ball a e
                      ⟹ contour_integral (linepath a b) f +
                          contour_integral (linepath b c) f +
                          contour_integral (linepath c a) f = 0"
      by fastforce
    have az: "dist a z < e" using mem_ball z by blast
    have sb_ball: "ball z (e - dist a z) ⊆ ball a e"
      by (simp add: dist_commute ball_subset_ball_iff)
    have "∃e>0. f holomorphic_on ball z e"
      apply (rule_tac x="e - dist a z" in exI)
      apply (simp add: az)
      apply (rule holomorphic_on_subset [OF _ sb_ball])
      apply (rule derivative_is_holomorphic[OF open_ball])
      apply (rule triangle_contour_integrals_starlike_primitive [OF contf _ open_ball, of a])
         apply (simp_all add: 0 ‹0 < e›)
      apply (meson ‹0 < e› centre_in_ball convex_ball convex_contains_segment mem_ball)
      done
  }
  then show ?thesis
    by (simp add: analytic_on_def)
qed

lemma Morera_local_triangle:
  assumes "⋀z. z ∈ s
          ⟹ ∃t. open t ∧ z ∈ t ∧ continuous_on t f ∧
                  (∀a b c. convex hull {a,b,c} ⊆ t
                              ⟶ contour_integral (linepath a b) f +
                                  contour_integral (linepath b c) f +
                                  contour_integral (linepath c a) f = 0)"
  shows "f analytic_on s"
proof -
  { fix z  assume "z ∈ s"
    with assms obtain t where
            "open t" and z: "z ∈ t" and contf: "continuous_on t f"
        and 0: "⋀a b c. convex hull {a,b,c} ⊆ t
                      ⟹ contour_integral (linepath a b) f +
                          contour_integral (linepath b c) f +
                          contour_integral (linepath c a) f = 0"
      by force
    then obtain e where "e>0" and e: "ball z e ⊆ t"
      using open_contains_ball by blast
    have [simp]: "continuous_on (ball z e) f" using contf
      using continuous_on_subset e by blast
    have "∃e a. 0 < e ∧
               z ∈ ball a e ∧
               continuous_on (ball a e) f ∧
               (∀b c. closed_segment b c ⊆ ball a e ⟶
                      contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f = 0)"
      apply (rule_tac x=e in exI)
      apply (rule_tac x=z in exI)
      apply (simp add: ‹e > 0›, clarify)
      apply (rule 0)
      apply (meson z ‹0 < e› centre_in_ball closed_segment_subset convex_ball dual_order.trans e starlike_convex_subset)
      done
  }
  then show ?thesis
    by (simp add: Morera_local_triangle_ball)
qed

proposition Morera_triangle:
    "⟦continuous_on s f; open s;
      ⋀a b c. convex hull {a,b,c} ⊆ s
              ⟶ contour_integral (linepath a b) f +
                  contour_integral (linepath b c) f +
                  contour_integral (linepath c a) f = 0⟧
     ⟹ f analytic_on s"
  using Morera_local_triangle by blast



subsection‹ Combining theorems for higher derivatives including Leibniz rule.›

lemma higher_deriv_linear [simp]:
    "(deriv ^^ n) (λw. c*w) = (λz. if n = 0 then c*z else if n = 1 then c else 0)"
  by (induction n) (auto simp: deriv_const deriv_linear)

lemma higher_deriv_const [simp]: "(deriv ^^ n) (λw. c) = (λw. if n=0 then c else 0)"
  by (induction n) (auto simp: deriv_const)

lemma higher_deriv_ident [simp]:
     "(deriv ^^ n) (λw. w) z = (if n = 0 then z else if n = 1 then 1 else 0)"
  apply (induction n, simp)
  apply (metis higher_deriv_linear lambda_one)
  done

corollary higher_deriv_id [simp]:
     "(deriv ^^ n) id z = (if n = 0 then z else if n = 1 then 1 else 0)"
  by (simp add: id_def)

lemma has_complex_derivative_funpow_1:
     "⟦(f has_field_derivative 1) (at z); f z = z⟧ ⟹ (f^^n has_field_derivative 1) (at z)"
  apply (induction n)
  apply auto
  apply (metis DERIV_ident DERIV_transform_at id_apply zero_less_one)
  by (metis DERIV_chain comp_funpow comp_id funpow_swap1 mult.right_neutral)

proposition higher_deriv_uminus:
  assumes "f holomorphic_on s" "open s" and z: "z ∈ s"
    shows "(deriv ^^ n) (λw. -(f w)) z = - ((deriv ^^ n) f z)"
using z
proof (induction n arbitrary: z)
  case 0 then show ?case by simp
next
  case (Suc n z)
  have *: "((deriv ^^ n) f has_field_derivative deriv ((deriv ^^ n) f) z) (at z)"
    using Suc.prems assms has_field_derivative_higher_deriv by auto
  show ?case
    apply simp
    apply (rule DERIV_imp_deriv)
    apply (rule DERIV_transform_within_open [of "λw. -((deriv ^^ n) f w)"])
    apply (rule derivative_eq_intros | rule * refl assms Suc)+
    apply (simp add: Suc)
    done
qed

proposition higher_deriv_add:
  fixes z::complex
  assumes "f holomorphic_on s" "g holomorphic_on s" "open s" and z: "z ∈ s"
    shows "(deriv ^^ n) (λw. f w + g w) z = (deriv ^^ n) f z + (deriv ^^ n) g z"
using z
proof (induction n arbitrary: z)
  case 0 then show ?case by simp
next
  case (Suc n z)
  have *: "((deriv ^^ n) f has_field_derivative deriv ((deriv ^^ n) f) z) (at z)"
          "((deriv ^^ n) g has_field_derivative deriv ((deriv ^^ n) g) z) (at z)"
    using Suc.prems assms has_field_derivative_higher_deriv by auto
  show ?case
    apply simp
    apply (rule DERIV_imp_deriv)
    apply (rule DERIV_transform_within_open [of "λw. (deriv ^^ n) f w + (deriv ^^ n) g w"])
    apply (rule derivative_eq_intros | rule * refl assms Suc)+
    apply (simp add: Suc)
    done
qed

corollary higher_deriv_diff:
  fixes z::complex
  assumes "f holomorphic_on s" "g holomorphic_on s" "open s" and z: "z ∈ s"
    shows "(deriv ^^ n) (λw. f w - g w) z = (deriv ^^ n) f z - (deriv ^^ n) g z"
  apply (simp only: Groups.group_add_class.diff_conv_add_uminus higher_deriv_add)
  apply (subst higher_deriv_add)
  using assms holomorphic_on_minus apply (auto simp: higher_deriv_uminus)
  done


lemma bb: "Suc n choose k = (n choose k) + (if k = 0 then 0 else (n choose (k - 1)))"
  by (simp add: Binomial.binomial.simps)

proposition higher_deriv_mult:
  fixes z::complex
  assumes "f holomorphic_on s" "g holomorphic_on s" "open s" and z: "z ∈ s"
    shows "(deriv ^^ n) (λw. f w * g w) z =
           (∑i = 0..n. of_nat (n choose i) * (deriv ^^ i) f z * (deriv ^^ (n - i)) g z)"
using z
proof (induction n arbitrary: z)
  case 0 then show ?case by simp
next
  case (Suc n z)
  have *: "⋀n. ((deriv ^^ n) f has_field_derivative deriv ((deriv ^^ n) f) z) (at z)"
          "⋀n. ((deriv ^^ n) g has_field_derivative deriv ((deriv ^^ n) g) z) (at z)"
    using Suc.prems assms has_field_derivative_higher_deriv by auto
  have sumeq: "(∑i = 0..n.
               of_nat (n choose i) * (deriv ((deriv ^^ i) f) z * (deriv ^^ (n - i)) g z + deriv ((deriv ^^ (n - i)) g) z * (deriv ^^ i) f z)) =
            g z * deriv ((deriv ^^ n) f) z + (∑i = 0..n. (deriv ^^ i) f z * (of_nat (Suc n choose i) * (deriv ^^ (Suc n - i)) g z))"
    apply (simp add: bb distrib_right algebra_simps setsum.distrib)
    apply (subst (4) setsum_Suc_reindex)
    apply (auto simp: algebra_simps Suc_diff_le intro: setsum.cong)
    done
  show ?case
    apply (simp only: funpow.simps o_apply)
    apply (rule DERIV_imp_deriv)
    apply (rule DERIV_transform_within_open
             [of "λw. (∑i = 0..n. of_nat (n choose i) * (deriv ^^ i) f w * (deriv ^^ (n - i)) g w)"])
    apply (simp add: algebra_simps)
    apply (rule DERIV_cong [OF DERIV_setsum])
    apply (rule DERIV_cmult)
    apply (auto simp: intro: DERIV_mult * sumeq ‹open s› Suc.prems Suc.IH [symmetric])
    done
qed


proposition higher_deriv_transform_within_open:
  fixes z::complex
  assumes "f holomorphic_on s" "g holomorphic_on s" "open s" and z: "z ∈ s"
      and fg: "⋀w. w ∈ s ⟹ f w = g w"
    shows "(deriv ^^ i) f z = (deriv ^^ i) g z"
using z
by (induction i arbitrary: z)
   (auto simp: fg intro: complex_derivative_transform_within_open holomorphic_higher_deriv assms)

proposition higher_deriv_compose_linear:
  fixes z::complex
  assumes f: "f holomorphic_on t" and s: "open s" and t: "open t" and z: "z ∈ s"
      and fg: "⋀w. w ∈ s ⟹ u * w ∈ t"
    shows "(deriv ^^ n) (λw. f (u * w)) z = u^n * (deriv ^^ n) f (u * z)"
using z
proof (induction n arbitrary: z)
  case 0 then show ?case by simp
next
  case (Suc n z)
  have holo0: "f holomorphic_on op * u ` s"
    by (meson fg f holomorphic_on_subset image_subset_iff)
  have holo1: "(λw. f (u * w)) holomorphic_on s"
    apply (rule holomorphic_on_compose [where g=f, unfolded o_def])
    apply (rule holo0 holomorphic_intros)+
    done
  have holo2: "(λz. u ^ n * (deriv ^^ n) f (u * z)) holomorphic_on s"
    apply (rule holomorphic_intros)+
    apply (rule holomorphic_on_compose [where g="(deriv ^^ n) f", unfolded o_def])
    apply (rule holomorphic_intros)
    apply (rule holomorphic_on_subset [where s=t])
    apply (rule holomorphic_intros assms)+
    apply (blast intro: fg)
    done
  have "deriv ((deriv ^^ n) (λw. f (u * w))) z = deriv (λz. u^n * (deriv ^^ n) f (u*z)) z"
    apply (rule complex_derivative_transform_within_open [OF _ holo2 s Suc.prems])
    apply (rule holomorphic_higher_deriv [OF holo1 s])
    apply (simp add: Suc.IH)
    done
  also have "... = u^n * deriv (λz. (deriv ^^ n) f (u * z)) z"
    apply (rule deriv_cmult)
    apply (rule analytic_on_imp_differentiable_at [OF _ Suc.prems])
    apply (rule analytic_on_compose_gen [where g="(deriv ^^ n) f" and t=t, unfolded o_def])
    apply (simp add: analytic_on_linear)
    apply (simp add: analytic_on_open f holomorphic_higher_deriv t)
    apply (blast intro: fg)
    done
  also have "... = u * u ^ n * deriv ((deriv ^^ n) f) (u * z)"
      apply (subst complex_derivative_chain [where g = "(deriv ^^ n) f" and f = "op*u", unfolded o_def])
      apply (rule derivative_intros)
      using Suc.prems field_differentiable_def f fg has_field_derivative_higher_deriv t apply blast
      apply (simp add: deriv_linear)
      done
  finally show ?case
    by simp
qed

lemma higher_deriv_add_at:
  assumes "f analytic_on {z}" "g analytic_on {z}"
    shows "(deriv ^^ n) (λw. f w + g w) z = (deriv ^^ n) f z + (deriv ^^ n) g z"
proof -
  have "f analytic_on {z} ∧ g analytic_on {z}"
    using assms by blast
  with higher_deriv_add show ?thesis
    by (auto simp: analytic_at_two)
qed

lemma higher_deriv_diff_at:
  assumes "f analytic_on {z}" "g analytic_on {z}"
    shows "(deriv ^^ n) (λw. f w - g w) z = (deriv ^^ n) f z - (deriv ^^ n) g z"
proof -
  have "f analytic_on {z} ∧ g analytic_on {z}"
    using assms by blast
  with higher_deriv_diff show ?thesis
    by (auto simp: analytic_at_two)
qed

lemma higher_deriv_uminus_at:
   "f analytic_on {z}  ⟹ (deriv ^^ n) (λw. -(f w)) z = - ((deriv ^^ n) f z)"
  using higher_deriv_uminus
    by (auto simp: analytic_at)

lemma higher_deriv_mult_at:
  assumes "f analytic_on {z}" "g analytic_on {z}"
    shows "(deriv ^^ n) (λw. f w * g w) z =
           (∑i = 0..n. of_nat (n choose i) * (deriv ^^ i) f z * (deriv ^^ (n - i)) g z)"
proof -
  have "f analytic_on {z} ∧ g analytic_on {z}"
    using assms by blast
  with higher_deriv_mult show ?thesis
    by (auto simp: analytic_at_two)
qed


text‹ Nonexistence of isolated singularities and a stronger integral formula.›

proposition no_isolated_singularity:
  fixes z::complex
  assumes f: "continuous_on s f" and holf: "f holomorphic_on (s - k)" and s: "open s" and k: "finite k"
    shows "f holomorphic_on s"
proof -
  { fix z
    assume "z ∈ s" and cdf: "⋀x. x∈s - k ⟹ f field_differentiable at x"
    have "f field_differentiable at z"
    proof (cases "z ∈ k")
      case False then show ?thesis by (blast intro: cdf ‹z ∈ s›)
    next
      case True
      with finite_set_avoid [OF k, of z]
      obtain d where "d>0" and d: "⋀x. ⟦x∈k; x ≠ z⟧ ⟹ d ≤ dist z x"
        by blast
      obtain e where "e>0" and e: "ball z e ⊆ s"
        using  s ‹z ∈ s› by (force simp add: open_contains_ball)
      have fde: "continuous_on (ball z (min d e)) f"
        by (metis Int_iff ball_min_Int continuous_on_subset e f subsetI)
      have "∃g. ∀w ∈ ball z (min d e). (g has_field_derivative f w) (at w within ball z (min d e))"
        apply (rule contour_integral_convex_primitive [OF convex_ball fde])
        apply (rule Cauchy_theorem_triangle_cofinite [OF _ k])
         apply (metis continuous_on_subset [OF fde] closed_segment_subset convex_ball starlike_convex_subset)
        apply (rule cdf)
        apply (metis Diff_iff Int_iff ball_min_Int bot_least contra_subsetD convex_ball e insert_subset
               interior_mono interior_subset subset_hull)
        done
      then have "f holomorphic_on ball z (min d e)"
        by (metis open_ball at_within_open derivative_is_holomorphic)
      then show ?thesis
        unfolding holomorphic_on_def
        by (metis open_ball ‹0 < d› ‹0 < e› at_within_open centre_in_ball min_less_iff_conj)
    qed
  }
  with holf s k show ?thesis
    by (simp add: holomorphic_on_open open_Diff finite_imp_closed field_differentiable_def [symmetric])
qed

proposition Cauchy_integral_formula_convex:
    assumes s: "convex s" and k: "finite k" and contf: "continuous_on s f"
        and fcd: "(⋀x. x ∈ interior s - k ⟹ f field_differentiable at x)"
        and z: "z ∈ interior s" and vpg: "valid_path γ"
        and pasz: "path_image γ ⊆ s - {z}" and loop: "pathfinish γ = pathstart γ"
      shows "((λw. f w / (w - z)) has_contour_integral (2*pi * ii * winding_number γ z * f z)) γ"
  apply (rule Cauchy_integral_formula_weak [OF s finite.emptyI contf])
  apply (simp add: holomorphic_on_open [symmetric] field_differentiable_def)
  using no_isolated_singularity [where s = "interior s"]
  apply (metis k contf fcd holomorphic_on_open field_differentiable_def continuous_on_subset
               has_field_derivative_at_within holomorphic_on_def interior_subset open_interior)
  using assms
  apply auto
  done


text‹ Formula for higher derivatives.›

proposition Cauchy_has_contour_integral_higher_derivative_circlepath:
  assumes contf: "continuous_on (cball z r) f"
      and holf: "f holomorphic_on ball z r"
      and w: "w ∈ ball z r"
    shows "((λu. f u / (u - w) ^ (Suc k)) has_contour_integral ((2 * pi * ii) / (fact k) * (deriv ^^ k) f w))
           (circlepath z r)"
using w
proof (induction k arbitrary: w)
  case 0 then show ?case
    using assms by (auto simp: Cauchy_integral_circlepath dist_commute dist_norm)
next
  case (Suc k)
  have [simp]: "r > 0" using w
    using ball_eq_empty by fastforce
  have f: "continuous_on (path_image (circlepath z r)) f"
    by (rule continuous_on_subset [OF contf]) (force simp add: cball_def sphere_def less_imp_le)
  obtain X where X: "((λu. f u / (u - w) ^ Suc (Suc k)) has_contour_integral X) (circlepath z r)"
    using Cauchy_next_derivative_circlepath(1) [OF f Suc.IH _ Suc.prems]
    by (auto simp: contour_integrable_on_def)
  then have con: "contour_integral (circlepath z r) ((λu. f u / (u - w) ^ Suc (Suc k))) = X"
    by (rule contour_integral_unique)
  have "⋀n. ((deriv ^^ n) f has_field_derivative deriv ((deriv ^^ n) f) w) (at w)"
    using Suc.prems assms has_field_derivative_higher_deriv by auto
  then have dnf_diff: "⋀n. (deriv ^^ n) f field_differentiable (at w)"
    by (force simp add: field_differentiable_def)
  have "deriv (λw. complex_of_real (2 * pi) * 𝗂 / (fact k) * (deriv ^^ k) f w) w =
          of_nat (Suc k) * contour_integral (circlepath z r) (λu. f u / (u - w) ^ Suc (Suc k))"
    by (force intro!: DERIV_imp_deriv Cauchy_next_derivative_circlepath [OF f Suc.IH _ Suc.prems])
  also have "... = of_nat (Suc k) * X"
    by (simp only: con)
  finally have "deriv (λw. ((2 * pi) * 𝗂 / (fact k)) * (deriv ^^ k) f w) w = of_nat (Suc k) * X" .
  then have "((2 * pi) * 𝗂 / (fact k)) * deriv (λw. (deriv ^^ k) f w) w = of_nat (Suc k) * X"
    by (metis deriv_cmult dnf_diff)
  then have "deriv (λw. (deriv ^^ k) f w) w = of_nat (Suc k) * X / ((2 * pi) * 𝗂 / (fact k))"
    by (simp add: field_simps)
  then show ?case
  using of_nat_eq_0_iff X by fastforce
qed

proposition Cauchy_higher_derivative_integral_circlepath:
  assumes contf: "continuous_on (cball z r) f"
      and holf: "f holomorphic_on ball z r"
      and w: "w ∈ ball z r"
    shows "(λu. f u / (u - w)^(Suc k)) contour_integrable_on (circlepath z r)"
           (is "?thes1")
      and "(deriv ^^ k) f w = (fact k) / (2 * pi * ii) * contour_integral(circlepath z r) (λu. f u/(u - w)^(Suc k))"
           (is "?thes2")
proof -
  have *: "((λu. f u / (u - w) ^ Suc k) has_contour_integral (2 * pi) * 𝗂 / (fact k) * (deriv ^^ k) f w)
           (circlepath z r)"
    using Cauchy_has_contour_integral_higher_derivative_circlepath [OF assms]
    by simp
  show ?thes1 using *
    using contour_integrable_on_def by blast
  show ?thes2
    unfolding contour_integral_unique [OF *] by (simp add: divide_simps)
qed

corollary Cauchy_contour_integral_circlepath:
  assumes "continuous_on (cball z r) f" "f holomorphic_on ball z r" "w ∈ ball z r"
    shows "contour_integral(circlepath z r) (λu. f u/(u - w)^(Suc k)) = (2 * pi * ii) * (deriv ^^ k) f w / (fact k)"
by (simp add: Cauchy_higher_derivative_integral_circlepath [OF assms])

corollary Cauchy_contour_integral_circlepath_2:
  assumes "continuous_on (cball z r) f" "f holomorphic_on ball z r" "w ∈ ball z r"
    shows "contour_integral(circlepath z r) (λu. f u/(u - w)^2) = (2 * pi * ii) * deriv f w"
  using Cauchy_contour_integral_circlepath [OF assms, of 1]
  by (simp add: power2_eq_square)


subsection‹A holomorphic function is analytic, i.e. has local power series.›

theorem holomorphic_power_series:
  assumes holf: "f holomorphic_on ball z r"
      and w: "w ∈ ball z r"
    shows "((λn. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
proof -
  have fh': "f holomorphic_on cball z ((r + dist w z) / 2)"
     apply (rule holomorphic_on_subset [OF holf])
     apply (clarsimp simp del: divide_const_simps)
     apply (metis add.commute dist_commute le_less_trans mem_ball real_gt_half_sum w)
     done
  ‹Replacing @{term r} and the original (weak) premises›
  obtain r where "0 < r" and holfc: "f holomorphic_on cball z r" and w: "w ∈ ball z r"
    apply (rule that [of "(r + dist w z) / 2"])
      apply (simp_all add: fh')
     apply (metis add_0_iff ball_eq_empty dist_nz dist_self empty_iff not_less pos_add_strict w)
    apply (metis add_less_cancel_right dist_commute mem_ball mult_2_right w)
    done
  then have holf: "f holomorphic_on ball z r" and contf: "continuous_on (cball z r) f"
    using ball_subset_cball holomorphic_on_subset apply blast
    by (simp add: holfc holomorphic_on_imp_continuous_on)
  have cint: "⋀k. (λu. f u / (u - z) ^ Suc k) contour_integrable_on circlepath z r"
    apply (rule Cauchy_higher_derivative_integral_circlepath [OF contf holf])
    apply (simp add: ‹0 < r›)
    done
  obtain B where "0 < B" and B: "⋀u. u ∈ cball z r ⟹ norm(f u) ≤ B"
    by (metis (no_types) bounded_pos compact_cball compact_continuous_image compact_imp_bounded contf image_eqI)
  obtain k where k: "0 < k" "k ≤ r" and wz_eq: "norm(w - z) = r - k"
             and kle: "⋀u. norm(u - z) = r ⟹ k ≤ norm(u - w)"
    apply (rule_tac k = "r - dist z w" in that)
    using w
    apply (auto simp: dist_norm norm_minus_commute)
    by (metis add_diff_eq diff_add_cancel norm_diff_ineq norm_minus_commute)
  have *: "∀F n in sequentially. ∀x∈path_image (circlepath z r).
                norm ((∑k<n. (w - z) ^ k * (f x / (x - z) ^ Suc k)) - f x / (x - w)) < e"
          if "0 < e" for e
  proof -
    have rr: "0 ≤ (r - k) / r" "(r - k) / r < 1" using  k by auto
    obtain n where n: "((r - k) / r) ^ n < e / B * k"
      using real_arch_pow_inv [of "e/B*k" "(r - k)/r"] ‹0 < e› ‹0 < B› k by force
    have "norm ((∑k<N. (w - z) ^ k * f u / (u - z) ^ Suc k) - f u / (u - w)) < e"
         if "n ≤ N" and r: "r = dist z u"  for N u
    proof -
      have N: "((r - k) / r) ^ N < e / B * k"
        apply (rule le_less_trans [OF power_decreasing n])
        using  ‹n ≤ N› k by auto
      have u [simp]: "(u ≠ z) ∧ (u ≠ w)"
        using ‹0 < r› r w by auto
      have wzu_not1: "(w - z) / (u - z) ≠ 1"
        by (metis (no_types) dist_norm divide_eq_1_iff less_irrefl mem_ball norm_minus_commute r w)
      have "norm ((∑k<N. (w - z) ^ k * f u / (u - z) ^ Suc k) * (u - w) - f u)
            = norm ((∑k<N. (((w - z) / (u - z)) ^ k)) * f u * (u - w) / (u - z) - f u)"
        unfolding setsum_left_distrib setsum_divide_distrib power_divide by (simp add: algebra_simps)
      also have "... = norm ((((w - z) / (u - z)) ^ N - 1) * (u - w) / (((w - z) / (u - z) - 1) * (u - z)) - 1) * norm (f u)"
        using ‹0 < B›
        apply (auto simp: geometric_sum [OF wzu_not1])
        apply (simp add: field_simps norm_mult [symmetric])
        done
      also have "... = norm ((u-z) ^ N * (w - u) - ((w - z) ^ N - (u-z) ^ N) * (u-w)) / (r ^ N * norm (u-w)) * norm (f u)"
        using ‹0 < r› r by (simp add: divide_simps norm_mult norm_divide norm_power dist_norm norm_minus_commute)
      also have "... = norm ((w - z) ^ N * (w - u)) / (r ^ N * norm (u - w)) * norm (f u)"
        by (simp add: algebra_simps)
      also have "... = norm (w - z) ^ N * norm (f u) / r ^ N"
        by (simp add: norm_mult norm_power norm_minus_commute)
      also have "... ≤ (((r - k)/r)^N) * B"
        using ‹0 < r› w k
        apply (simp add: divide_simps)
        apply (rule mult_mono [OF power_mono])
        apply (auto simp: norm_divide wz_eq norm_power dist_norm norm_minus_commute B r)
        done
      also have "... < e * k"
        using ‹0 < B› N by (simp add: divide_simps)
      also have "... ≤ e * norm (u - w)"
        using r kle ‹0 < e› by (simp add: dist_commute dist_norm)
      finally show ?thesis
        by (simp add: divide_simps norm_divide del: power_Suc)
    qed
    with ‹0 < r› show ?thesis
      by (auto simp: mult_ac less_imp_le eventually_sequentially Ball_def)
  qed
  have eq: "∀F x in sequentially.
             contour_integral (circlepath z r) (λu. ∑k<x. (w - z) ^ k * (f u / (u - z) ^ Suc k)) =
             (∑k<x. contour_integral (circlepath z r) (λu. f u / (u - z) ^ Suc k) * (w - z) ^ k)"
    apply (rule eventuallyI)
    apply (subst contour_integral_setsum, simp)
    using contour_integrable_lmul [OF cint, of "(w - z) ^ a" for a] apply (simp add: field_simps)
    apply (simp only: contour_integral_lmul cint algebra_simps)
    done
  have "(λk. contour_integral (circlepath z r) (λu. f u/(u - z)^(Suc k)) * (w - z)^k)
        sums contour_integral (circlepath z r) (λu. f u/(u - w))"
    unfolding sums_def
    apply (rule Lim_transform_eventually [OF eq])
    apply (rule contour_integral_uniform_limit_circlepath [OF eventuallyI *])
    apply (rule contour_integrable_setsum, simp)
    apply (rule contour_integrable_lmul)
    apply (rule Cauchy_higher_derivative_integral_circlepath [OF contf holf])
    using ‹0 < r›
    apply auto
    done
  then have "(λk. contour_integral (circlepath z r) (λu. f u/(u - z)^(Suc k)) * (w - z)^k)
             sums (2 * of_real pi * ii * f w)"
    using w by (auto simp: dist_commute dist_norm contour_integral_unique [OF Cauchy_integral_circlepath_simple [OF holfc]])
  then have "(λk. contour_integral (circlepath z r) (λu. f u / (u - z) ^ Suc k) * (w - z)^k / (𝗂 * (of_real pi * 2)))
            sums ((2 * of_real pi * ii * f w) / (𝗂 * (complex_of_real pi * 2)))"
    by (rule sums_divide)
  then have "(λn. (w - z) ^ n * contour_integral (circlepath z r) (λu. f u / (u - z) ^ Suc n) / (𝗂 * (of_real pi * 2)))
            sums f w"
    by (simp add: field_simps)
  then show ?thesis
    by (simp add: field_simps ‹0 < r› Cauchy_higher_derivative_integral_circlepath [OF contf holf])
qed


subsection‹The Liouville theorem and the Fundamental Theorem of Algebra.›

text‹ These weak Liouville versions don't even need the derivative formula.›

lemma Liouville_weak_0:
  assumes holf: "f holomorphic_on UNIV" and inf: "(f ⤏ 0) at_infinity"
    shows "f z = 0"
proof (rule ccontr)
  assume fz: "f z ≠ 0"
  with inf [unfolded Lim_at_infinity, rule_format, of "norm(f z)/2"]
  obtain B where B: "⋀x. B ≤ cmod x ⟹ norm (f x) * 2 < cmod (f z)"
    by (auto simp: dist_norm)
  def R  "1 + ¦B¦ + norm z"
  have "R > 0" unfolding R_def 
  proof -
    have "0 ≤ cmod z + ¦B¦"
      by (metis (full_types) add_nonneg_nonneg norm_ge_zero real_norm_def)
    then show "0 < 1 + ¦B¦ + cmod z"
      by linarith
  qed 
  have *: "((λu. f u / (u - z)) has_contour_integral 2 * complex_of_real pi * 𝗂 * f z) (circlepath z R)"
    apply (rule Cauchy_integral_circlepath)
    using ‹R > 0› apply (auto intro: holomorphic_on_subset [OF holf] holomorphic_on_imp_continuous_on)+
    done
  have "cmod (x - z) = R ⟹ cmod (f x) * 2 ≤ cmod (f z)" for x
    apply (simp add: R_def)
    apply (rule less_imp_le)
    apply (rule B)
    using norm_triangle_ineq4 [of x z]
    apply (auto simp:)
    done
  with  ‹R > 0› fz show False
    using has_contour_integral_bound_circlepath [OF *, of "norm(f z)/2/R"]
    by (auto simp: norm_mult norm_divide divide_simps)
qed

proposition Liouville_weak:
  assumes "f holomorphic_on UNIV" and "(f ⤏ l) at_infinity"
    shows "f z = l"
  using Liouville_weak_0 [of "λz. f z - l"]
  by (simp add: assms holomorphic_on_const holomorphic_on_diff LIM_zero)


proposition Liouville_weak_inverse:
  assumes "f holomorphic_on UNIV" and unbounded: "⋀B. eventually (λx. norm (f x) ≥ B) at_infinity"
    obtains z where "f z = 0"
proof -
  { assume f: "⋀z. f z ≠ 0"
    have 1: "(λx. 1 / f x) holomorphic_on UNIV"
      by (simp add: holomorphic_on_divide holomorphic_on_const assms f)
    have 2: "((λx. 1 / f x) ⤏ 0) at_infinity"
      apply (rule tendstoI [OF eventually_mono])
      apply (rule_tac B="2/e" in unbounded)
      apply (simp add: dist_norm norm_divide divide_simps mult_ac)
      done
    have False
      using Liouville_weak_0 [OF 1 2] f by simp
  }
  then show ?thesis
    using that by blast
qed


text‹ In particular we get the Fundamental Theorem of Algebra.›

theorem fundamental_theorem_of_algebra:
    fixes a :: "nat ⇒ complex"
  assumes "a 0 = 0 ∨ (∃i ∈ {1..n}. a i ≠ 0)"
  obtains z where "(∑i≤n. a i * z^i) = 0"
using assms
proof (elim disjE bexE)
  assume "a 0 = 0" then show ?thesis
    by (auto simp: that [of 0])
next
  fix i
  assume i: "i ∈ {1..n}" and nz: "a i ≠ 0"
  have 1: "(λz. ∑i≤n. a i * z^i) holomorphic_on UNIV"
    by (rule holomorphic_intros)+
  show ?thesis
    apply (rule Liouville_weak_inverse [OF 1])
    apply (rule polyfun_extremal)
    apply (rule nz)
    using i that
    apply (auto simp:)
    done
qed


subsection‹ Weierstrass convergence theorem.›

proposition holomorphic_uniform_limit:
  assumes cont: "eventually (λn. continuous_on (cball z r) (f n) ∧ (f n) holomorphic_on ball z r) F"
      and lim: "⋀e. 0 < e ⟹ eventually (λn. ∀x ∈ cball z r. norm(f n x - g x) < e) F"
      and F:  "~ trivial_limit F"
  obtains "continuous_on (cball z r) g" "g holomorphic_on ball z r"
proof (cases r "0::real" rule: linorder_cases)
  case less then show ?thesis by (force simp add: ball_empty less_imp_le continuous_on_def holomorphic_on_def intro: that)
next
  case equal then show ?thesis
    by (force simp add: holomorphic_on_def continuous_on_sing intro: that)
next
  case greater
  have contg: "continuous_on (cball z r) g"
    using cont
    by (fastforce simp: eventually_conj_iff dist_norm intro: eventually_mono [OF lim] continuous_uniform_limit [OF F])
  have 1: "continuous_on (path_image (circlepath z r)) (λx. 1 / (2 * complex_of_real pi * 𝗂) * g x)"
    apply (rule continuous_intros continuous_on_subset [OF contg])+
    using ‹0 < r› by auto
  have 2: "((λu. 1 / (2 * of_real pi * 𝗂) * g u / (u - w) ^ 1) has_contour_integral g w) (circlepath z r)"
       if w: "w ∈ ball z r" for w
  proof -
    def d  "(r - norm(w - z))"
    have "0 < d"  "d ≤ r" using w by (auto simp: norm_minus_commute d_def dist_norm)
    have dle: "⋀u. cmod (z - u) = r ⟹ d ≤ cmod (u - w)"
      unfolding d_def by (metis add_diff_eq diff_add_cancel norm_diff_ineq norm_minus_commute)
    have ev_int: "∀F n in F. (λu. f n u / (u - w)) contour_integrable_on circlepath z r"
      apply (rule eventually_mono [OF cont])
      using w
      apply (auto intro: Cauchy_higher_derivative_integral_circlepath [where k=0, simplified])
      done
    have ev_less: "∀F n in F. ∀x∈path_image (circlepath z r). cmod (f n x / (x - w) - g x / (x - w)) < e"
         if "e > 0" for e
      using greater ‹0 < d› ‹0 < e›
      apply (simp add: norm_divide diff_divide_distrib [symmetric] divide_simps)
      apply (rule_tac e1="e * d" in eventually_mono [OF lim])
      apply (force simp: dist_norm intro: dle mult_left_mono less_le_trans)+
      done
    have g_cint: "(λu. g u/(u - w)) contour_integrable_on circlepath z r"
      by (rule contour_integral_uniform_limit_circlepath [OF ev_int ev_less F ‹0 < r›])
    have cif_tends_cig: "((λn. contour_integral(circlepath z r) (λu. f n u / (u - w))) ⤏ contour_integral(circlepath z r) (λu. g u/(u - w))) F"
      by (rule contour_integral_uniform_limit_circlepath [OF ev_int ev_less F ‹0 < r›])
    have f_tends_cig: "((λn. 2 * of_real pi * ii * f n w) ⤏ contour_integral (circlepath z r) (λu. g u / (u - w))) F"
      apply (rule Lim_transform_eventually [where f = "λn. contour_integral (circlepath z r) (λu. f n u/(u - w))"])
      apply (rule eventually_mono [OF cont])
      apply (rule contour_integral_unique [OF Cauchy_integral_circlepath])
      using w
      apply (auto simp: norm_minus_commute dist_norm cif_tends_cig)
      done
    have "((λn. 2 * of_real pi * 𝗂 * f n w) ⤏ 2 * of_real pi * 𝗂 * g w) F"
      apply (rule tendsto_mult_left [OF tendstoI])
      apply (rule eventually_mono [OF lim], assumption)
      using w
      apply (force simp add: dist_norm)
      done
    then have "((λu. g u / (u - w)) has_contour_integral 2 * of_real pi * 𝗂 * g w) (circlepath z r)"
      using has_contour_integral_integral [OF g_cint] tendsto_unique [OF F f_tends_cig] w
      by (force simp add: dist_norm)
    then have "((λu. g u / (2 * of_real pi * 𝗂 * (u - w))) has_contour_integral g w) (circlepath z r)"
      using has_contour_integral_div [where c = "2 * of_real pi * 𝗂"]
      by (force simp add: field_simps)
    then show ?thesis
      by (simp add: dist_norm)
  qed
  show ?thesis
    using Cauchy_next_derivative_circlepath(2) [OF 1 2, simplified]
    by (fastforce simp add: holomorphic_on_open contg intro: that)
qed


text‹ Version showing that the limit is the limit of the derivatives.›

proposition has_complex_derivative_uniform_limit:
  fixes z::complex
  assumes cont: "eventually (λn. continuous_on (cball z r) (f n) ∧
                               (∀w ∈ ball z r. ((f n) has_field_derivative (f' n w)) (at w))) F"
      and lim: "⋀e. 0 < e ⟹ eventually (λn. ∀x ∈ cball z r. norm(f n x - g x) < e) F"
      and F:  "~ trivial_limit F" and "0 < r"
  obtains g' where
      "continuous_on (cball z r) g"
      "⋀w. w ∈ ball z r ⟹ (g has_field_derivative (g' w)) (at w) ∧ ((λn. f' n w) ⤏ g' w) F"
proof -
  let ?conint = "contour_integral (circlepath z r)"
  have g: "continuous_on (cball z r) g" "g holomorphic_on ball z r"
    by (rule holomorphic_uniform_limit [OF eventually_mono [OF cont] lim F];
             auto simp: holomorphic_on_open field_differentiable_def)+
  then obtain g' where g': "⋀x. x ∈ ball z r ⟹ (g has_field_derivative g' x) (at x)"
    using DERIV_deriv_iff_has_field_derivative
    by (fastforce simp add: holomorphic_on_open)
  then have derg: "⋀x. x ∈ ball z r ⟹ deriv g x = g' x"
    by (simp add: DERIV_imp_deriv)
  have tends_f'n_g': "((λn. f' n w) ⤏ g' w) F" if w: "w ∈ ball z r" for w
  proof -
    have eq_f': "?conint (λx. f n x / (x - w)2) - ?conint (λx. g x / (x - w)2) = (f' n w - g' w) * (2 * of_real pi * 𝗂)"
             if cont_fn: "continuous_on (cball z r) (f n)"
             and fnd: "⋀w. w ∈ ball z r ⟹ (f n has_field_derivative f' n w) (at w)" for n
    proof -
      have hol_fn: "f n holomorphic_on ball z r"
        using fnd by (force simp add: holomorphic_on_open)
      have "(f n has_field_derivative 1 / (2 * of_real pi * 𝗂) * ?conint (λu. f n u / (u - w)2)) (at w)"
        by (rule Cauchy_derivative_integral_circlepath [OF cont_fn hol_fn w])
      then have f': "f' n w = 1 / (2 * of_real pi * 𝗂) * ?conint (λu. f n u / (u - w)2)"
        using DERIV_unique [OF fnd] w by blast
      show ?thesis
        by (simp add: f' Cauchy_contour_integral_circlepath_2 [OF g w] derg [OF w] divide_simps)
    qed
    def d  "(r - norm(w - z))^2"
    have "d > 0"
      using w by (simp add: dist_commute dist_norm d_def)
    have dle: "⋀y. r = cmod (z - y) ⟹ d ≤ cmod ((y - w)2)"
      apply (simp add: d_def norm_power)
      apply (rule power_mono)
      apply (metis add_diff_eq diff_add_cancel norm_diff_ineq norm_minus_commute)
      apply (metis diff_ge_0_iff_ge dist_commute dist_norm less_eq_real_def mem_ball w)
      done
    have 1: "∀F n in F. (λx. f n x / (x - w)2) contour_integrable_on circlepath z r"
      by (force simp add: holomorphic_on_open intro: w Cauchy_derivative_integral_circlepath eventually_mono [OF cont])
    have 2: "0 < e ⟹ ∀F n in F. ∀x ∈ path_image (circlepath z r). cmod (f n x / (x - w)2 - g x / (x - w)2) < e" for e
      using ‹r > 0›
      apply (simp add: diff_divide_distrib [symmetric] norm_divide divide_simps sphere_def)
      apply (rule eventually_mono [OF lim, of "e*d"])
      apply (simp add: ‹0 < d›)
      apply (force simp add: dist_norm dle intro: less_le_trans)
      done
    have "((λn. contour_integral (circlepath z r) (λx. f n x / (x - w)2))
             ⤏ contour_integral (circlepath z r) ((λx. g x / (x - w)2))) F"
      by (rule Cauchy_Integral_Thm.contour_integral_uniform_limit_circlepath [OF 1 2 F ‹0 < r›])
    then have tendsto_0: "((λn. 1 / (2 * of_real pi * 𝗂) * (?conint (λx. f n x / (x - w)2) - ?conint (λx. g x / (x - w)2))) ⤏ 0) F"
      using Lim_null by (force intro!: tendsto_mult_right_zero)
    have "((λn. f' n w - g' w) ⤏ 0) F"
      apply (rule Lim_transform_eventually [OF _ tendsto_0])
      apply (force simp add: divide_simps intro: eq_f' eventually_mono [OF cont])
      done
    then show ?thesis using Lim_null by blast
  qed
  obtain g' where "⋀w. w ∈ ball z r ⟹ (g has_field_derivative (g' w)) (at w) ∧ ((λn. f' n w) ⤏ g' w) F"
      by (blast intro: tends_f'n_g' g' )
  then show ?thesis using g
    using that by blast
qed


subsection‹Some more simple/convenient versions for applications.›

lemma holomorphic_uniform_sequence:
  assumes s: "open s"
      and hol_fn: "⋀n. (f n) holomorphic_on s"
      and to_g: "⋀x. x ∈ s
                     ⟹ ∃d. 0 < d ∧ cball x d ⊆ s ∧
                             (∀e. 0 < e ⟶ eventually (λn. ∀y ∈ cball x d. norm(f n y - g y) < e) sequentially)"
  shows "g holomorphic_on s"
proof -
  have "∃f'. (g has_field_derivative f') (at z)" if "z ∈ s" for z
  proof -
    obtain r where "0 < r" and r: "cball z r ⊆ s"
               and fg: "∀e. 0 < e ⟶ eventually (λn. ∀y ∈ cball z r. norm(f n y - g y) < e) sequentially"
      using to_g [OF ‹z ∈ s›] by blast
    have *: "∀F n in sequentially. continuous_on (cball z r) (f n) ∧ f n holomorphic_on ball z r"
      apply (intro eventuallyI conjI)
      using hol_fn holomorphic_on_imp_continuous_on holomorphic_on_subset r apply blast
      apply (metis hol_fn holomorphic_on_subset interior_cball interior_subset r)
      done
    show ?thesis
      apply (rule holomorphic_uniform_limit [OF *])
      using ‹0 < r› centre_in_ball fg
      apply (auto simp: holomorphic_on_open)
      done
  qed
  with s show ?thesis
    by (simp add: holomorphic_on_open)
qed

lemma has_complex_derivative_uniform_sequence:
  fixes s :: "complex set"
  assumes s: "open s"
      and hfd: "⋀n x. x ∈ s ⟹ ((f n) has_field_derivative f' n x) (at x)"
      and to_g: "⋀x. x ∈ s
             ⟹ ∃d. 0 < d ∧ cball x d ⊆ s ∧
                     (∀e. 0 < e ⟶ eventually (λn. ∀y ∈ cball x d. norm(f n y - g y) < e) sequentially)"
  shows "∃g'. ∀x ∈ s. (g has_field_derivative g' x) (at x) ∧ ((λn. f' n x) ⤏ g' x) sequentially"
proof -
  have y: "∃y. (g has_field_derivative y) (at z) ∧ (λn. f' n z) ⇢ y" if "z ∈ s" for z
  proof -
    obtain r where "0 < r" and r: "cball z r ⊆ s"
               and fg: "∀e. 0 < e ⟶ eventually (λn. ∀y ∈ cball z r. norm(f n y - g y) < e) sequentially"
      using to_g [OF ‹z ∈ s›] by blast
    have *: "∀F n in sequentially. continuous_on (cball z r) (f n) ∧
                                   (∀w ∈ ball z r. ((f n) has_field_derivative (f' n w)) (at w))"
      apply (intro eventuallyI conjI)
      apply (meson hfd holomorphic_on_imp_continuous_on holomorphic_on_open holomorphic_on_subset r s)
      using ball_subset_cball hfd r apply blast
      done
    show ?thesis
      apply (rule has_complex_derivative_uniform_limit [OF *, of g])
      using ‹0 < r› centre_in_ball fg
      apply force+
      done
  qed
  show ?thesis
    by (rule bchoice) (blast intro: y)
qed


subsection‹On analytic functions defined by a series.›

lemma series_and_derivative_comparison:
  fixes s :: "complex set"
  assumes s: "open s"
      and h: "summable h"
      and hfd: "⋀n x. x ∈ s ⟹ (f n has_field_derivative f' n x) (at x)"
      and to_g: "⋀n x. ⟦N ≤ n; x ∈ s⟧ ⟹ norm(f n x) ≤ h n"
  obtains g g' where "∀x ∈ s. ((λn. f n x) sums g x) ∧ ((λn. f' n x) sums g' x) ∧ (g has_field_derivative g' x) (at x)"
proof -
  obtain g where g: "⋀e. e>0 ⟹ ∃N. ∀n x. N ≤ n ∧ x ∈ s ⟶ dist (∑n<n. f n x) (g x) < e"
    using series_comparison_uniform [OF h to_g, of N s] by force
  have *: "∃d>0. cball x d ⊆ s ∧ (∀e>0. ∀F n in sequentially. ∀y∈cball x d. cmod ((∑a<n. f a y) - g y) < e)"
         if "x ∈ s" for x
  proof -
    obtain d where "d>0" and d: "cball x d ⊆ s"
      using open_contains_cball [of "s"] ‹x ∈ s› s by blast
    then show ?thesis
      apply (rule_tac x=d in exI)
      apply (auto simp: dist_norm eventually_sequentially)
      apply (metis g contra_subsetD dist_norm)
      done
  qed
  have "(∀x∈s. (λn. ∑i<n. f i x) ⇢ g x)"
    using g by (force simp add: lim_sequentially)
  moreover have "∃g'. ∀x∈s. (g has_field_derivative g' x) (at x) ∧ (λn. ∑i<n. f' i x) ⇢ g' x"
    by (rule has_complex_derivative_uniform_sequence [OF s]) (auto intro: * hfd DERIV_setsum)+
  ultimately show ?thesis
    by (force simp add: sums_def  conj_commute intro: that)
qed

text‹A version where we only have local uniform/comparative convergence.›

lemma series_and_derivative_comparison_local:
  fixes s :: "complex set"
  assumes s: "open s"
      and hfd: "⋀n x. x ∈ s ⟹ (f n has_field_derivative f' n x) (at x)"
      and to_g: "⋀x. x ∈ s ⟹
                      ∃d h N. 0 < d ∧ summable h ∧ (∀n y. N ≤ n ∧ y ∈ ball x d ⟶ norm(f n y) ≤ h n)"
  shows "∃g g'. ∀x ∈ s. ((λn. f n x) sums g x) ∧ ((λn. f' n x) sums g' x) ∧ (g has_field_derivative g' x) (at x)"
proof -
  have "∃y. (λn. f n z) sums (∑n. f n z) ∧ (λn. f' n z) sums y ∧ ((λx. ∑n. f n x) has_field_derivative y) (at z)"
       if "z ∈ s" for z
  proof -
    obtain d h N where "0 < d" "summable h" and le_h: "⋀n y. ⟦N ≤ n; y ∈ ball z d⟧ ⟹ norm(f n y) ≤ h n"
      using to_g ‹z ∈ s› by meson
    then obtain r where "r>0" and r: "ball z r ⊆ ball z d ∩ s" using ‹z ∈ s› s
      by (metis Int_iff open_ball centre_in_ball open_Int open_contains_ball_eq)
    have 1: "open (ball z d ∩ s)"
      by (simp add: open_Int s)
    have 2: "⋀n x. x ∈ ball z d ∩ s ⟹ (f n has_field_derivative f' n x) (at x)"
      by (auto simp: hfd)
    obtain g g' where gg': "∀x ∈ ball z d ∩ s. ((λn. f n x) sums g x) ∧
                                    ((λn. f' n x) sums g' x) ∧ (g has_field_derivative g' x) (at x)"
      by (auto intro: le_h series_and_derivative_comparison [OF 1 ‹summable h› hfd])
    then have "(λn. f' n z) sums g' z"
      by (meson ‹0 < r› centre_in_ball contra_subsetD r)
    moreover have "(λn. f n z) sums (∑n. f n z)"
      by (metis summable_comparison_test' summable_sums centre_in_ball ‹0 < d› ‹summable h› le_h)
    moreover have "((λx. ∑n. f n x) has_field_derivative g' z) (at z)"
      apply (rule_tac f=g in DERIV_transform_at [OF _ ‹0 < r›])
      apply (simp add: gg' ‹z ∈ s› ‹0 < d›)
      apply (metis (full_types) contra_subsetD dist_commute gg' mem_ball r sums_unique)
      done
    ultimately show ?thesis by auto
  qed
  then show ?thesis
    by (rule_tac x="λx. suminf  (λn. f n x)" in exI) meson
qed


text‹Sometimes convenient to compare with a complex series of positive reals. (?)›

lemma series_and_derivative_comparison_complex:
  fixes s :: "complex set"
  assumes s: "open s"
      and hfd: "⋀n x. x ∈ s ⟹ (f n has_field_derivative f' n x) (at x)"
      and to_g: "⋀x. x ∈ s ⟹
                      ∃d h N. 0 < d ∧ summable h ∧ range h ⊆ nonneg_Reals ∧ (∀n y. N ≤ n ∧ y ∈ ball x d ⟶ cmod(f n y) ≤ cmod (h n))"
  shows "∃g g'. ∀x ∈ s. ((λn. f n x) sums g x) ∧ ((λn. f' n x) sums g' x) ∧ (g has_field_derivative g' x) (at x)"
apply (rule series_and_derivative_comparison_local [OF s hfd], assumption)
apply (frule to_g)
apply (erule ex_forward)
apply (erule exE)
apply (rule_tac x="Re o h" in exI)
apply (erule ex_forward)
apply (simp add: summable_Re o_def )
apply (elim conjE all_forward)
apply (simp add: nonneg_Reals_cmod_eq_Re image_subset_iff)
done


text‹In particular, a power series is analytic inside circle of convergence.›

lemma power_series_and_derivative_0:
  fixes a :: "nat ⇒ complex" and r::real
  assumes "summable (λn. a n * r^n)"
    shows "∃g g'. ∀z. cmod z < r ⟶
             ((λn. a n * z^n) sums g z) ∧ ((λn. of_nat n * a n * z^(n - 1)) sums g' z) ∧ (g has_field_derivative g' z) (at z)"
proof (cases "0 < r")
  case True
    have der: "⋀n z. ((λx. a n * x ^ n) has_field_derivative of_nat n * a n * z ^ (n - 1)) (at z)"
      by (rule derivative_eq_intros | simp)+
    have y_le: "⟦cmod (z - y) * 2 < r - cmod z⟧ ⟹ cmod y ≤ cmod (of_real r + of_real (cmod z)) / 2" for z y
      using ‹r > 0›
      apply (auto simp: algebra_simps norm_mult norm_divide norm_power of_real_add [symmetric] simp del: of_real_add)
      using norm_triangle_ineq2 [of y z]
      apply (simp only: diff_le_eq norm_minus_commute mult_2)
      done
    have "summable (λn. a n * complex_of_real r ^ n)"
      using assms ‹r > 0› by simp
    moreover have "⋀z. cmod z < r ⟹ cmod ((of_real r + of_real (cmod z)) / 2) < cmod (of_real r)"
      using ‹r > 0›
      by (simp add: of_real_add [symmetric] del: of_real_add)
    ultimately have sum: "⋀z. cmod z < r ⟹ summable (λn. of_real (cmod (a n)) * ((of_real r + complex_of_real (cmod z)) / 2) ^ n)"
      by (rule power_series_conv_imp_absconv_weak)
    have "∃g g'. ∀z ∈ ball 0 r. (λn.  (a n) * z ^ n) sums g z ∧
               (λn. of_nat n * (a n) * z ^ (n - 1)) sums g' z ∧ (g has_field_derivative g' z) (at z)"
      apply (rule series_and_derivative_comparison_complex [OF open_ball der])
      apply (rule_tac x="(r - norm z)/2" in exI)
      apply (simp add: dist_norm)
      apply (rule_tac x="λn. of_real(norm(a n)*((r + norm z)/2)^n)" in exI)
      using ‹r > 0›
      apply (auto simp: sum nonneg_Reals_divide_I)
      apply (rule_tac x=0 in exI)
      apply (force simp: norm_mult norm_divide norm_power intro!: mult_left_mono power_mono y_le)
      done
  then show ?thesis
    by (simp add: dist_0_norm ball_def)
next
  case False then show ?thesis
    apply (simp add: not_less)
    using less_le_trans norm_not_less_zero by blast
qed

proposition power_series_and_derivative:
  fixes a :: "nat ⇒ complex" and r::real
  assumes "summable (λn. a n * r^n)"
    obtains g g' where "∀z ∈ ball w r.
             ((λn. a n * (z - w) ^ n) sums g z) ∧ ((λn. of_nat n * a n * (z - w) ^ (n - 1)) sums g' z) ∧
              (g has_field_derivative g' z) (at z)"
  using power_series_and_derivative_0 [OF assms]
  apply clarify
  apply (rule_tac g="(λz. g(z - w))" in that)
  using DERIV_shift [where z="-w"]
  apply (auto simp: norm_minus_commute Ball_def dist_norm)
  done

proposition power_series_holomorphic:
  assumes "⋀w. w ∈ ball z r ⟹ ((λn. a n*(w - z)^n) sums f w)"
    shows "f holomorphic_on ball z r"
proof -
  have "∃f'. (f has_field_derivative f') (at w)" if w: "dist z w < r" for w
  proof -
    have inb: "z + complex_of_real ((dist z w + r) / 2) ∈ ball z r"
    proof -
      have wz: "cmod (w - z) < r" using w
        by (auto simp: divide_simps dist_norm norm_minus_commute)
      then have "0 ≤ r"
        by (meson less_eq_real_def norm_ge_zero order_trans)
      show ?thesis
        using w by (simp add: dist_norm ‹0≤r› of_real_add [symmetric] del: of_real_add)
    qed
    have sum: "summable (λn. a n * of_real (((cmod (z - w) + r) / 2) ^ n))"
      using assms [OF inb] by (force simp add: summable_def dist_norm)
    obtain g g' where gg': "⋀u. u ∈ ball z ((cmod (z - w) + r) / 2) ⟹
                               (λn. a n * (u - z) ^ n) sums g u ∧
                               (λn. of_nat n * a n * (u - z) ^ (n - 1)) sums g' u ∧ (g has_field_derivative g' u) (at u)"
      by (rule power_series_and_derivative [OF sum, of z]) fastforce
    have [simp]: "g u = f u" if "cmod (u - w) < (r - cmod (z - w)) / 2" for u
    proof -
      have less: "cmod (z - u) * 2 < cmod (z - w) + r"
        using that dist_triangle2 [of z u w]
        by (simp add: dist_norm [symmetric] algebra_simps)
      show ?thesis
        apply (rule sums_unique2 [of "λn. a n*(u - z)^n"])
        using gg' [of u] less w
        apply (auto simp: assms dist_norm)
        done
    qed
    show ?thesis
      apply (rule_tac x="g' w" in exI)
      apply (rule DERIV_transform_at [where f=g and d="(r - norm(z - w))/2"])
      using w gg' [of w]
      apply (auto simp: dist_norm)
      done
  qed
  then show ?thesis by (simp add: holomorphic_on_open)
qed

corollary holomorphic_iff_power_series:
     "f holomorphic_on ball z r ⟷
      (∀w ∈ ball z r. (λn. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
  apply (intro iffI ballI)
   using holomorphic_power_series  apply force
  apply (rule power_series_holomorphic [where a = "λn. (deriv ^^ n) f z / (fact n)"])
  apply force
  done

corollary power_series_analytic:
     "(⋀w. w ∈ ball z r ⟹ (λn. a n*(w - z)^n) sums f w) ⟹ f analytic_on ball z r"
  by (force simp add: analytic_on_open intro!: power_series_holomorphic)

corollary analytic_iff_power_series:
     "f analytic_on ball z r ⟷
      (∀w ∈ ball z r. (λn. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
  by (simp add: analytic_on_open holomorphic_iff_power_series)


subsection‹Equality between holomorphic functions, on open ball then connected set.›

lemma holomorphic_fun_eq_on_ball:
   "⟦f holomorphic_on ball z r; g holomorphic_on ball z r;
     w ∈ ball z r;
     ⋀n. (deriv ^^ n) f z = (deriv ^^ n) g z⟧
     ⟹ f w = g w"
  apply (rule sums_unique2 [of "λn. (deriv ^^ n) f z / (fact n) * (w - z)^n"])
  apply (auto simp: holomorphic_iff_power_series)
  done

lemma holomorphic_fun_eq_0_on_ball:
   "⟦f holomorphic_on ball z r;  w ∈ ball z r;
     ⋀n. (deriv ^^ n) f z = 0⟧
     ⟹ f w = 0"
  apply (rule sums_unique2 [of "λn. (deriv ^^ n) f z / (fact n) * (w - z)^n"])
  apply (auto simp: holomorphic_iff_power_series)
  done

lemma holomorphic_fun_eq_0_on_connected:
  assumes holf: "f holomorphic_on s" and "open s"
      and cons: "connected s"
      and der: "⋀n. (deriv ^^ n) f z = 0"
      and "z ∈ s" "w ∈ s"
    shows "f w = 0"
proof -
  have *: "⋀x e. ⟦ ∀xa. (deriv ^^ xa) f x = 0;  ball x e ⊆ s⟧
           ⟹ ball x e ⊆ (⋂n. {w ∈ s. (deriv ^^ n) f w = 0})"
    apply auto
    apply (rule holomorphic_fun_eq_0_on_ball [OF holomorphic_higher_deriv])
    apply (rule holomorphic_on_subset [OF holf], simp_all)
    by (metis funpow_add o_apply)
  have 1: "openin (subtopology euclidean s) (⋂n. {w ∈ s. (deriv ^^ n) f w = 0})"
    apply (rule open_subset, force)
    using ‹open s›
    apply (simp add: open_contains_ball Ball_def)
    apply (erule all_forward)
    using "*" by auto blast+
  have 2: "closedin (subtopology euclidean s) (⋂n. {w ∈ s. (deriv ^^ n) f w = 0})"
    using assms
    by (auto intro: continuous_closedin_preimage_constant holomorphic_on_imp_continuous_on holomorphic_higher_deriv)
  obtain e where "e>0" and e: "ball w e ⊆ s" using openE [OF ‹open s› ‹w ∈ s›] .
  then have holfb: "f holomorphic_on ball w e"
    using holf holomorphic_on_subset by blast
  have 3: "(⋂n. {w ∈ s. (deriv ^^ n) f w = 0}) = s ⟹ f w = 0"
    using ‹e>0› e by (force intro: holomorphic_fun_eq_0_on_ball [OF holfb])
  show ?thesis
    using cons der ‹z ∈ s›
    apply (simp add: connected_clopen)
    apply (drule_tac x="⋂n. {w ∈ s. (deriv ^^ n) f w = 0}" in spec)
    apply (auto simp: 1 2 3)
    done
qed

lemma holomorphic_fun_eq_on_connected:
  assumes "f holomorphic_on s" "g holomorphic_on s" and "open s"  "connected s"
      and "⋀n. (deriv ^^ n) f z = (deriv ^^ n) g z"
      and "z ∈ s" "w ∈ s"
    shows "f w = g w"
  apply (rule holomorphic_fun_eq_0_on_connected [of "λx. f x - g x" s z, simplified])
  apply (intro assms holomorphic_intros)
  using assms apply simp_all
  apply (subst higher_deriv_diff, auto)
  done

lemma holomorphic_fun_eq_const_on_connected:
  assumes holf: "f holomorphic_on s" and "open s"
      and cons: "connected s"
      and der: "⋀n. 0 < n ⟹ (deriv ^^ n) f z = 0"
      and "z ∈ s" "w ∈ s"
    shows "f w = f z"
  apply (rule holomorphic_fun_eq_0_on_connected [of "λw. f w - f z" s z, simplified])
  apply (intro assms holomorphic_intros)
  using assms apply simp_all
  apply (subst higher_deriv_diff)
  apply (intro holomorphic_intros | simp)+
  done


subsection‹Some basic lemmas about poles/singularities.›

lemma pole_lemma:
  assumes holf: "f holomorphic_on s" and a: "a ∈ interior s"
    shows "(λz. if z = a then deriv f a
                 else (f z - f a) / (z - a)) holomorphic_on s" (is "?F holomorphic_on s")
proof -
  have F1: "?F field_differentiable (at u within s)" if "u ∈ s" "u ≠ a" for u
  proof -
    have fcd: "f field_differentiable at u within s"
      using holf holomorphic_on_def by (simp add: ‹u ∈ s›)
    have cd: "(λz. (f z - f a) / (z - a)) field_differentiable at u within s"
      by (rule fcd derivative_intros | simp add: that)+
    have "0 < dist a u" using that dist_nz by blast
    then show ?thesis
      by (rule field_differentiable_transform_within [OF _ _ _ cd]) (auto simp: ‹u ∈ s›)
  qed
  have F2: "?F field_differentiable at a" if "0 < e" "ball a e ⊆ s" for e
  proof -
    have holfb: "f holomorphic_on ball a e"
      by (rule holomorphic_on_subset [OF holf ‹ball a e ⊆ s›])
    have 2: "?F holomorphic_on ball a e - {a}"
      apply (rule holomorphic_on_subset [where s = "s - {a}"])
      apply (simp add: holomorphic_on_def field_differentiable_def [symmetric])
      using mem_ball that
      apply (auto intro: F1 field_differentiable_within_subset)
      done
    have "isCont (λz. if z = a then deriv f a else (f z - f a) / (z - a)) x"
            if "dist a x < e" for x
    proof (cases "x=a")
      case True then show ?thesis
      using holfb ‹0 < e›
      apply (simp add: holomorphic_on_open field_differentiable_def [symmetric])
      apply (drule_tac x=a in bspec)
      apply (auto simp: DERIV_deriv_iff_field_differentiable [symmetric] continuous_at DERIV_iff2
                elim: rev_iffD1 [OF _ LIM_equal])
      done
    next
      case False with 2 that show ?thesis
        by (force simp: holomorphic_on_open open_Diff field_differentiable_def [symmetric] field_differentiable_imp_continuous_at)
    qed
    then have 1: "continuous_on (ball a e) ?F"
      by (clarsimp simp:  continuous_on_eq_continuous_at)
    have "?F holomorphic_on ball a e"
      by (auto intro: no_isolated_singularity [OF 1 2])
    with that show ?thesis
      by (simp add: holomorphic_on_open field_differentiable_def [symmetric]
                    field_differentiable_at_within)
  qed
  show ?thesis
  proof
    fix x assume "x ∈ s" show "?F field_differentiable at x within s"
    proof (cases "x=a")
      case True then show ?thesis
      using a by (auto simp: mem_interior intro: field_differentiable_at_within F2)
    next
      case False with F1 ‹x ∈ s›
      show ?thesis by blast
    qed
  qed
qed

proposition pole_theorem:
  assumes holg: "g holomorphic_on s" and a: "a ∈ interior s"
      and eq: "⋀z. z ∈ s - {a} ⟹ g z = (z - a) * f z"
    shows "(λz. if z = a then deriv g a
                 else f z - g a/(z - a)) holomorphic_on s"
  using pole_lemma [OF holg a]
  by (rule holomorphic_transform) (simp add: eq divide_simps)

lemma pole_lemma_open:
  assumes "f holomorphic_on s" "open s"
    shows "(λz. if z = a then deriv f a else (f z - f a)/(z - a)) holomorphic_on s"
proof (cases "a ∈ s")
  case True with assms interior_eq pole_lemma
    show ?thesis by fastforce
next
  case False with assms show ?thesis
    apply (simp add: holomorphic_on_def field_differentiable_def [symmetric], clarify)
    apply (rule field_differentiable_transform_within [where f = "λz. (f z - f a)/(z - a)" and d = 1])
    apply (rule derivative_intros | force)+
    done
qed

proposition pole_theorem_open:
  assumes holg: "g holomorphic_on s" and s: "open s"
      and eq: "⋀z. z ∈ s - {a} ⟹ g z = (z - a) * f z"
    shows "(λz. if z = a then deriv g a
                 else f z - g a/(z - a)) holomorphic_on s"
  using pole_lemma_open [OF holg s]
  by (rule holomorphic_transform) (auto simp: eq divide_simps)

proposition pole_theorem_0:
  assumes holg: "g holomorphic_on s" and a: "a ∈ interior s"
      and eq: "⋀z. z ∈ s - {a} ⟹ g z = (z - a) * f z"
      and [simp]: "f a = deriv g a" "g a = 0"
    shows "f holomorphic_on s"
  using pole_theorem [OF holg a eq]
  by (rule holomorphic_transform) (auto simp: eq divide_simps)

proposition pole_theorem_open_0:
  assumes holg: "g holomorphic_on s" and s: "open s"
      and eq: "⋀z. z ∈ s - {a} ⟹ g z = (z - a) * f z"
      and [simp]: "f a = deriv g a" "g a = 0"
    shows "f holomorphic_on s"
  using pole_theorem_open [OF holg s eq]
  by (rule holomorphic_transform) (auto simp: eq divide_simps)

lemma pole_theorem_analytic:
  assumes g: "g analytic_on s"
      and eq: "⋀z. z ∈ s
             ⟹ ∃d. 0 < d ∧ (∀w ∈ ball z d - {a}. g w = (w - a) * f w)"
    shows "(λz. if z = a then deriv g a
                 else f z - g a/(z - a)) analytic_on s"
using g
apply (simp add: analytic_on_def Ball_def)
apply (safe elim!: all_forward dest!: eq)
apply (rule_tac x="min d e" in exI, simp)
apply (rule pole_theorem_open)
apply (auto simp: holomorphic_on_subset subset_ball)
done

lemma pole_theorem_analytic_0:
  assumes g: "g analytic_on s"
      and eq: "⋀z. z ∈ s ⟹ ∃d. 0 < d ∧ (∀w ∈ ball z d - {a}. g w = (w - a) * f w)"
      and [simp]: "f a = deriv g a" "g a = 0"
    shows "f analytic_on s"
proof -
  have [simp]: "(λz. if z = a then deriv g a else f z - g a / (z - a)) = f"
    by auto
  show ?thesis
    using pole_theorem_analytic [OF g eq] by simp
qed

lemma pole_theorem_analytic_open_superset:
  assumes g: "g analytic_on s" and "s ⊆ t" "open t"
      and eq: "⋀z. z ∈ t - {a} ⟹ g z = (z - a) * f z"
    shows "(λz. if z = a then deriv g a
                 else f z - g a/(z - a)) analytic_on s"
  apply (rule pole_theorem_analytic [OF g])
  apply (rule openE [OF ‹open t›])
  using assms eq by auto

lemma pole_theorem_analytic_open_superset_0:
  assumes g: "g analytic_on s" "s ⊆ t" "open t" "⋀z. z ∈ t - {a} ⟹ g z = (z - a) * f z"
      and [simp]: "f a = deriv g a" "g a = 0"
    shows "f analytic_on s"
proof -
  have [simp]: "(λz. if z = a then deriv g a else f z - g a / (z - a)) = f"
    by auto
  have "(λz. if z = a then deriv g a else f z - g a/(z - a)) analytic_on s"
    by (rule pole_theorem_analytic_open_superset [OF g])
  then show ?thesis by simp
qed



subsection‹General, homology form of Cauchy's theorem.›

text‹Proof is based on Dixon's, as presented in Lang's "Complex Analysis" book (page 147).›

lemma contour_integral_continuous_on_linepath_2D:
  assumes "open u" and cont_dw: "⋀w. w ∈ u ⟹ F w contour_integrable_on (linepath a b)"
      and cond_uu: "continuous_on (u × u) (λ(x,y). F x y)"
      and abu: "closed_segment a b ⊆ u"
    shows "continuous_on u (λw. contour_integral (linepath a b) (F w))"
proof -
  have *: "∃d>0. ∀x'∈u. dist x' w < d ⟶
                         dist (contour_integral (linepath a b) (F x'))
                              (contour_integral (linepath a b) (F w)) ≤ ε"
          if "w ∈ u" "0 < ε" "a ≠ b" for w ε
  proof -
    obtain δ where "δ>0" and δ: "cball w δ ⊆ u" using open_contains_cball ‹open u› ‹w ∈ u› by force
    let ?TZ = "{(t,z) |t z. t ∈ cball w δ ∧ z ∈ closed_segment a b}"
    have "uniformly_continuous_on ?TZ (λ(x,y). F x y)"
      apply (rule compact_uniformly_continuous)
      apply (rule continuous_on_subset[OF cond_uu])
      using abu δ
      apply (auto simp: compact_Times simp del: mem_cball)
      done
    then obtain η where "η>0"
        and η: "⋀x x'. ⟦x∈?TZ; x'∈?TZ; dist x' x < η⟧ ⟹
                         dist ((λ(x,y). F x y) x') ((λ(x,y). F x y) x) < ε/norm(b - a)"
      apply (rule uniformly_continuous_onE [where e = "ε/norm(b - a)"])
      using ‹0 < ε› ‹a ≠ b› by auto
    have η: "⟦norm (w - x1) ≤ δ;   x2 ∈ closed_segment a b;
              norm (w - x1') ≤ δ;  x2' ∈ closed_segment a b; norm ((x1', x2') - (x1, x2)) < η⟧
              ⟹ norm (F x1' x2' - F x1 x2) ≤ ε / cmod (b - a)"
             for x1 x2 x1' x2'
      using η [of "(x1,x2)" "(x1',x2')"] by (force simp add: dist_norm)
    have le_ee: "cmod (contour_integral (linepath a b) (λx. F x' x - F w x)) ≤ ε"
                if "x' ∈ u" "cmod (x' - w) < δ" "cmod (x' - w) < η"  for x'
    proof -
      have "cmod (contour_integral (linepath a b) (λx. F x' x - F w x)) ≤ ε/norm(b - a) * norm(b - a)"
        apply (rule has_contour_integral_bound_linepath [OF has_contour_integral_integral _ η])
        apply (rule contour_integrable_diff [OF cont_dw cont_dw])
        using ‹0 < ε› ‹a ≠ b› ‹0 < δ› ‹w ∈ u› that
        apply (auto simp: norm_minus_commute)
        done
      also have "... = ε" using ‹a ≠ b› by simp
      finally show ?thesis .
    qed
    show ?thesis
      apply (rule_tac x="min δ η" in exI)
      using ‹0 < δ› ‹0 < η›
      apply (auto simp: dist_norm contour_integral_diff [OF cont_dw cont_dw, symmetric] ‹w ∈ u› intro: le_ee)
      done
  qed
  show ?thesis
    apply (rule continuous_onI)
    apply (cases "a=b")
    apply (auto intro: *)
    done
qed

text‹This version has @{term"polynomial_function γ"} as an additional assumption.›
lemma Cauchy_integral_formula_global_weak:
    assumes u: "open u" and holf: "f holomorphic_on u"
        and z: "z ∈ u" and γ: "polynomial_function γ"
        and pasz: "path_image γ ⊆ u - {z}" and loop: "pathfinish γ = pathstart γ"
        and zero: "⋀w. w ∉ u ⟹ winding_number γ w = 0"
      shows "((λw. f w / (w - z)) has_contour_integral (2*pi * ii * winding_number γ z * f z)) γ"
proof -
  obtain γ' where pfγ': "polynomial_function γ'" and γ': "⋀x. (γ has_vector_derivative (γ' x)) (at x)"
    using has_vector_derivative_polynomial_function [OF γ] by blast
  then have "bounded(path_image γ')"
    by (simp add: path_image_def compact_imp_bounded compact_continuous_image continuous_on_polymonial_function)
  then obtain B where "B>0" and B: "⋀x. x ∈ path_image γ' ⟹ norm x ≤ B"
    using bounded_pos by force
  def d  "λz w. if w = z then deriv f z else (f w - f z)/(w - z)"
  def v  "{w. w ∉ path_image γ ∧ winding_number γ w = 0}"
  have "path γ" "valid_path γ" using γ
    by (auto simp: path_polynomial_function valid_path_polynomial_function)
  then have ov: "open v"
    by (simp add: v_def open_winding_number_levelsets loop)
  have uv_Un: "u ∪ v = UNIV"
    using pasz zero by (auto simp: v_def)
  have conf: "continuous_on u f"
    by (metis holf holomorphic_on_imp_continuous_on)
  have hol_d: "(d y) holomorphic_on u" if "y ∈ u" for y
  proof -
    have *: "(λc. if c = y then deriv f y else (f c - f y) / (c - y)) holomorphic_on u"
      by (simp add: holf pole_lemma_open u)
    then have "isCont (λx. if x = y then deriv f y else (f x - f y) / (x - y)) y"
      using at_within_open field_differentiable_imp_continuous_at holomorphic_on_def that u by fastforce
    then have "continuous_on u (d y)"
      apply (simp add: d_def continuous_on_eq_continuous_at u, clarify)
      using * holomorphic_on_def
      by (meson field_differentiable_within_open field_differentiable_imp_continuous_at u)
    moreover have "d y holomorphic_on u - {y}"
      apply (simp add: d_def holomorphic_on_open u open_delete field_differentiable_def [symmetric])
      apply (intro ballI)
      apply (rename_tac w)
      apply (rule_tac d="dist w y" and f = "λw. (f w - f y)/(w - y)" in field_differentiable_transform_within)
      apply (auto simp: dist_pos_lt dist_commute intro!: derivative_intros)
      using analytic_on_imp_differentiable_at analytic_on_open holf u apply blast
      done
    ultimately show ?thesis
      by (rule no_isolated_singularity) (auto simp: u)
  qed
  have cint_fxy: "(λx. (f x - f y) / (x - y)) contour_integrable_on γ" if "y ∉ path_image γ" for y
    apply (rule contour_integrable_holomorphic_simple [where s = "u-{y}"])
    using ‹valid_path γ› pasz
    apply (auto simp: u open_delete)
    apply (rule continuous_intros holomorphic_intros continuous_on_subset [OF conf] holomorphic_on_subset [OF holf] |
                force simp add: that)+
    done
  def h  "λz. if z ∈ u then contour_integral γ (d z) else contour_integral γ (λw. f w/(w - z))"
  have U: "⋀z. z ∈ u ⟹ ((d z) has_contour_integral h z) γ"
    apply (simp add: h_def)
    apply (rule has_contour_integral_integral [OF contour_integrable_holomorphic_simple [where s=u]])
    using u pasz ‹valid_path γ›
    apply (auto intro: holomorphic_on_imp_continuous_on hol_d)
    done
  have V: "((λw. f w / (w - z)) has_contour_integral h z) γ" if z: "z ∈ v" for z
  proof -
    have 0: "0 = (f z) * 2 * of_real (2 * pi) * 𝗂 * winding_number γ z"
      using v_def z by auto
    then have "((λx. 1 / (x - z)) has_contour_integral 0) γ"
     using z v_def  has_contour_integral_winding_number [OF ‹valid_path γ›] by fastforce
    then have "((λx. f z * (1 / (x - z))) has_contour_integral 0) γ"
      using has_contour_integral_lmul by fastforce
    then have "((λx. f z / (x - z)) has_contour_integral 0) γ"
      by (simp add: divide_simps)
    moreover have "((λx. (f x - f z) / (x - z)) has_contour_integral contour_integral γ (d z)) γ"
      using z
      apply (auto simp: v_def)
      apply (metis (no_types, lifting) contour_integrable_eq d_def has_contour_integral_eq has_contour_integral_integral cint_fxy)
      done
    ultimately have *: "((λx. f z / (x - z) + (f x - f z) / (x - z)) has_contour_integral (0 + contour_integral γ (d z))) γ"
      by (rule has_contour_integral_add)
    have "((λw. f w / (w - z)) has_contour_integral contour_integral γ (d z)) γ"
            if  "z ∈ u"
      using * by (auto simp: divide_simps has_contour_integral_eq)
    moreover have "((λw. f w / (w - z)) has_contour_integral contour_integral γ (λw. f w / (w - z))) γ"
            if "z ∉ u"
      apply (rule has_contour_integral_integral [OF contour_integrable_holomorphic_simple [where s=u]])
      using u pasz ‹valid_path γ› that
      apply (auto intro: holomorphic_on_imp_continuous_on hol_d)
      apply (rule continuous_intros conf holomorphic_intros holf | force)+
      done
    ultimately show ?thesis
      using z by (simp add: h_def)
  qed
  have znot: "z ∉ path_image γ"
    using pasz by blast
  obtain d0 where "d0>0" and d0: "⋀x y. x ∈ path_image γ ⟹ y ∈ - u ⟹ d0 ≤ dist x y"
    using separate_compact_closed [of "path_image γ" "-u"] pasz u
    by (fastforce simp add: ‹path γ› compact_path_image)
  obtain dd where "0 < dd" and dd: "{y + k | y k. y ∈ path_image γ ∧ k ∈ ball 0 dd} ⊆ u"
    apply (rule that [of "d0/2"])
    using ‹0 < d0›
    apply (auto simp: dist_norm dest: d0)
    done
  have "⋀x x'. ⟦x ∈ path_image γ; dist x x' * 2 < dd⟧ ⟹ ∃y k. x' = y + k ∧ y ∈ path_image γ ∧ dist 0 k * 2 ≤ dd"
    apply (rule_tac x=x in exI)
    apply (rule_tac x="x'-x" in exI)
    apply (force simp add: dist_norm)
    done
  then have 1: "path_image γ ⊆ interior {y + k |y k. y ∈ path_image γ ∧ k ∈ cball 0 (dd / 2)}"
    apply (clarsimp simp add: mem_interior)
    using ‹0 < dd›
    apply (rule_tac x="dd/2" in exI, auto)
    done
  obtain t where "compact t" and subt: "path_image γ ⊆ interior t" and t: "t ⊆ u"
    apply (rule that [OF _ 1])
    apply (fastforce simp add: ‹valid_path γ› compact_valid_path_image intro!: compact_sums)
    apply (rule order_trans [OF _ dd])
    using ‹0 < dd› by fastforce
  obtain L where "L>0"
           and L: "⋀f B. ⟦f holomorphic_on interior t; ⋀z. z∈interior t ⟹ cmod (f z) ≤ B⟧ ⟹
                         cmod (contour_integral γ f) ≤ L * B"
      using contour_integral_bound_exists [OF open_interior ‹valid_path γ› subt]
      by blast
  have "bounded(f ` t)"
    by (meson ‹compact t› compact_continuous_image compact_imp_bounded conf continuous_on_subset t)
  then obtain D where "D>0" and D: "⋀x. x ∈ t ⟹ norm (f x) ≤ D"
    by (auto simp: bounded_pos)
  obtain C where "C>0" and C: "⋀x. x ∈ t ⟹ norm x ≤ C"
    using ‹compact t› bounded_pos compact_imp_bounded by force
  have "dist (h y) 0 ≤ e" if "0 < e" and le: "D * L / e + C ≤ cmod y" for e y
  proof -
    have "D * L / e > 0"  using ‹D>0› ‹L>0› ‹e>0› by simp
    with le have ybig: "norm y > C" by force
    with C have "y ∉ t"  by force
    then have ynot: "y ∉ path_image γ"
      using subt interior_subset by blast
    have [simp]: "winding_number γ y = 0"
      apply (rule winding_number_zero_outside [of _ "cball 0 C"])
      using ybig interior_subset subt
      apply (force simp add: loop ‹path γ› dist_norm intro!: C)+
      done
    have [simp]: "h y = contour_integral γ (λw. f w/(w - y))"
      by (rule contour_integral_unique [symmetric]) (simp add: v_def ynot V)
    have holint: "(λw. f w / (w - y)) holomorphic_on interior t"
      apply (rule holomorphic_on_divide)
      using holf holomorphic_on_subset interior_subset t apply blast
      apply (rule holomorphic_intros)+
      using ‹y ∉ t› interior_subset by auto
    have leD: "cmod (f z / (z - y)) ≤ D * (e / L / D)" if z: "z ∈ interior t" for z
    proof -
      have "D * L / e + cmod z ≤ cmod y"
        using le C [of z] z using interior_subset by force
      then have DL2: "D * L / e ≤ cmod (z - y)"
        using norm_triangle_ineq2 [of y z] by (simp add: norm_minus_commute)
      have "cmod (f z / (z - y)) = cmod (f z) * inverse (cmod (z - y))"
        by (simp add: norm_mult norm_inverse Fields.field_class.field_divide_inverse)
      also have "... ≤ D * (e / L / D)"
        apply (rule mult_mono)
        using that D interior_subset apply blast
        using ‹L>0› ‹e>0› ‹D>0› DL2
        apply (auto simp: norm_divide divide_simps algebra_simps)
        done
      finally show ?thesis .
    qed
    have "dist (h y) 0 = cmod (contour_integral γ (λw. f w / (w - y)))"
      by (simp add: dist_norm)
    also have "... ≤ L * (D * (e / L / D))"
      by (rule L [OF holint leD])
    also have "... = e"
      using  ‹L>0› ‹0 < D› by auto
    finally show ?thesis .
  qed
  then have "(h ⤏ 0) at_infinity"
    by (meson Lim_at_infinityI)
  moreover have "h holomorphic_on UNIV"
  proof -
    have con_ff: "continuous (at (x,z)) (λ(x,y). (f y - f x) / (y - x))"
                 if "x ∈ u" "z ∈ u" "x ≠ z" for x z
      using that conf
      apply (simp add: split_def continuous_on_eq_continuous_at u)
      apply (simp | rule continuous_intros continuous_within_compose2 [where g=f])+
      done
    have con_fstsnd: "continuous_on UNIV (λx. (fst x - snd x) ::complex)"
      by (rule continuous_intros)+
    have open_uu_Id: "open (u × u - Id)"
      apply (rule open_Diff)
      apply (simp add: open_Times u)
      using continuous_closed_preimage_constant [OF con_fstsnd closed_UNIV, of 0]
      apply (auto simp: Id_fstsnd_eq algebra_simps)
      done
    have con_derf: "continuous (at z) (deriv f)" if "z ∈ u" for z
      apply (rule continuous_on_interior [of u])
      apply (simp add: holf holomorphic_deriv holomorphic_on_imp_continuous_on u)
      by (simp add: interior_open that u)
    have tendsto_f': "((λ(x,y). if y = x then deriv f (x)
                                else (f (y) - f (x)) / (y - x)) ⤏ deriv f x)
                      (at (x, x) within u × u)" if "x ∈ u" for x
    proof (rule Lim_withinI)
      fix e::real assume "0 < e"
      obtain k1 where "k1>0" and k1: "⋀x'. norm (x' - x) ≤ k1 ⟹ norm (deriv f x' - deriv f x) < e"
        using ‹0 < e› continuous_within_E [OF con_derf [OF ‹x ∈ u›]]
        by (metis UNIV_I dist_norm)
      obtain k2 where "k2>0" and k2: "ball x k2 ⊆ u" by (blast intro: openE [OF u] ‹x ∈ u›)
      have neq: "norm ((f z' - f x') / (z' - x') - deriv f x) ≤ e"
                    if "z' ≠ x'" and less_k1: "norm (x'-x, z'-x) < k1" and less_k2: "norm (x'-x, z'-x) < k2"
                 for x' z'
      proof -
        have cs_less: "w ∈ closed_segment x' z' ⟹ cmod (w - x) ≤ norm (x'-x, z'-x)" for w
          apply (drule segment_furthest_le [where y=x])
          by (metis (no_types) dist_commute dist_norm norm_fst_le norm_snd_le order_trans)
        have derf_le: "w ∈ closed_segment x' z' ⟹ z' ≠ x' ⟹ cmod (deriv f w - deriv f x) ≤ e" for w
          by (blast intro: cs_less less_k1 k1 [unfolded divide_const_simps dist_norm] less_imp_le le_less_trans)
        have f_has_der: "⋀x. x ∈ u ⟹ (f has_field_derivative deriv f x) (at x within u)"
          by (metis DERIV_deriv_iff_field_differentiable at_within_open holf holomorphic_on_def u)
        have "closed_segment x' z' ⊆ u"
          by (rule order_trans [OF _ k2]) (simp add: cs_less  le_less_trans [OF _ less_k2] dist_complex_def norm_minus_commute subset_iff)
        then have cint_derf: "(deriv f has_contour_integral f z' - f x') (linepath x' z')"
          using contour_integral_primitive [OF f_has_der valid_path_linepath] pasz  by simp
        then have *: "((λx. deriv f x / (z' - x')) has_contour_integral (f z' - f x') / (z' - x')) (linepath x' z')"
          by (rule has_contour_integral_div)
        have "norm ((f z' - f x') / (z' - x') - deriv f x) ≤ e/norm(z' - x') * norm(z' - x')"
          apply (rule has_contour_integral_bound_linepath [OF has_contour_integral_diff [OF *]])
          using has_contour_integral_div [where c = "z' - x'", OF has_contour_integral_const_linepath [of "deriv f x" z' x']]
                 ‹e > 0›  ‹z' ≠ x'›
          apply (auto simp: norm_divide divide_simps derf_le)
          done
        also have "... ≤ e" using ‹0 < e› by simp
        finally show ?thesis .
      qed
      show "∃d>0. ∀xa∈u × u.
                  0 < dist xa (x, x) ∧ dist xa (x, x) < d ⟶
                  dist (case xa of (x, y) ⇒ if y = x then deriv f x else (f y - f x) / (y - x)) (deriv f x) ≤ e"
        apply (rule_tac x="min k1 k2" in exI)
        using ‹k1>0› ‹k2>0› ‹e>0›
        apply (force simp: dist_norm neq intro: dual_order.strict_trans2 k1 less_imp_le norm_fst_le)
        done
    qed
    have con_pa_f: "continuous_on (path_image γ) f"
      by (meson holf holomorphic_on_imp_continuous_on holomorphic_on_subset interior_subset subt t)
    have le_B: "⋀t. t ∈ {0..1} ⟹ cmod (vector_derivative γ (at t)) ≤ B"
      apply (rule B)
      using γ' using path_image_def vector_derivative_at by fastforce
    have f_has_cint: "⋀w. w ∈ v - path_image γ ⟹ ((λu. f u / (u - w) ^ 1) has_contour_integral h w) γ"
      by (simp add: V)
    have cond_uu: "continuous_on (u × u) (λ(x,y). d x y)"
      apply (simp add: continuous_on_eq_continuous_within d_def continuous_within tendsto_f')
      apply (simp add: tendsto_within_open_NO_MATCH open_Times u, clarify)
      apply (rule Lim_transform_within_open [OF _ open_uu_Id, where f = "(λ(x,y). (f y - f x) / (y - x))"])
      using con_ff
      apply (auto simp: continuous_within)
      done
    have hol_dw: "(λz. d z w) holomorphic_on u" if "w ∈ u" for w
    proof -
      have "continuous_on u ((λ(x,y). d x y) o (λz. (w,z)))"
        by (rule continuous_on_compose continuous_intros continuous_on_subset [OF cond_uu] | force intro: that)+
      then have *: "continuous_on u (λz. if w = z then deriv f z else (f w - f z) / (w - z))"
        by (rule rev_iffD1 [OF _ continuous_on_cong [OF refl]]) (simp add: d_def field_simps)
      have **: "⋀x. ⟦x ∈ u; x ≠ w⟧ ⟹ (λz. if w = z then deriv f z else (f w - f z) / (w - z)) field_differentiable at x"
        apply (rule_tac f = "λx. (f w - f x)/(w - x)" and d = "dist x w" in field_differentiable_transform_within)
        apply (rule u derivative_intros holomorphic_on_imp_differentiable_at [OF holf] | force simp add: dist_commute)+
        done
      show ?thesis
        unfolding d_def
        apply (rule no_isolated_singularity [OF * _ u, where k = "{w}"])
        apply (auto simp: field_differentiable_def [symmetric] holomorphic_on_open open_Diff u **)
        done
    qed
    { fix a b
      assume abu: "closed_segment a b ⊆ u"
      then have "⋀w. w ∈ u ⟹ (λz. d z w) contour_integrable_on (linepath a b)"
        by (metis hol_dw continuous_on_subset contour_integrable_continuous_linepath holomorphic_on_imp_continuous_on)
      then have cont_cint_d: "continuous_on u (λw. contour_integral (linepath a b) (λz. d z w))"
        apply (rule contour_integral_continuous_on_linepath_2D [OF ‹open u› _ _ abu])
        apply (auto simp: intro: continuous_on_swap_args cond_uu)
        done
      have cont_cint_dγ: "continuous_on {0..1} ((λw. contour_integral (linepath a b) (λz. d z w)) o γ)"
        apply (rule continuous_on_compose)
        using ‹path γ› path_def pasz
        apply (auto intro!: continuous_on_subset [OF cont_cint_d])
        apply (force simp add: path_image_def)
        done
      have cint_cint: "(λw. contour_integral (linepath a b) (λz. d z w)) contour_integrable_on γ"
        apply (simp add: contour_integrable_on)
        apply (rule integrable_continuous_real)
        apply (rule continuous_on_mult [OF cont_cint_dγ [unfolded o_def]])
        using pfγ'
        by (simp add: continuous_on_polymonial_function vector_derivative_at [OF γ'])
      have "contour_integral (linepath a b) h = contour_integral (linepath a b) (λz. contour_integral γ (d z))"
        using abu  by (force simp add: h_def intro: contour_integral_eq)
      also have "... =  contour_integral γ (λw. contour_integral (linepath a b) (λz. d z w))"
        apply (rule contour_integral_swap)
        apply (rule continuous_on_subset [OF cond_uu])
        using abu pasz ‹valid_path γ›
        apply (auto intro!: continuous_intros)
        by (metis γ' continuous_on_eq path_def path_polynomial_function pfγ' vector_derivative_at)
      finally have cint_h_eq:
          "contour_integral (linepath a b) h =
                    contour_integral γ (λw. contour_integral (linepath a b) (λz. d z w))" .
      note cint_cint cint_h_eq
    } note cint_h = this
    have conthu: "continuous_on u h"
    proof (simp add: continuous_on_sequentially, clarify)
      fix a x
      assume x: "x ∈ u" and au: "∀n. a n ∈ u" and ax: "a ⇢ x"
      then have A1: "∀F n in sequentially. d (a n) contour_integrable_on γ"
        by (meson U contour_integrable_on_def eventuallyI)
      obtain dd where "dd>0" and dd: "cball x dd ⊆ u" using open_contains_cball u x by force
      have A2: "∀F n in sequentially. ∀xa∈path_image γ. cmod (d (a n) xa - d x xa) < ee" if "0 < ee" for ee
      proof -
        let ?ddpa = "{(w,z) |w z. w ∈ cball x dd ∧ z ∈ path_image γ}"
        have "uniformly_continuous_on ?ddpa (λ(x,y). d x y)"
          apply (rule compact_uniformly_continuous [OF continuous_on_subset[OF cond_uu]])
          using dd pasz ‹valid_path γ›
          apply (auto simp: compact_Times compact_valid_path_image simp del: mem_cball)
          done
        then obtain kk where "kk>0"
            and kk: "⋀x x'. ⟦x∈?ddpa; x'∈?ddpa; dist x' x < kk⟧ ⟹
                             dist ((λ(x,y). d x y) x') ((λ(x,y). d x y) x) < ee"
          apply (rule uniformly_continuous_onE [where e = ee])
          using ‹0 < ee› by auto
        have kk: "⟦norm (w - x) ≤ dd; z ∈ path_image γ; norm ((w, z) - (x, z)) < kk⟧ ⟹ norm (d w z - d x z) < ee"
                 for  w z
          using ‹dd>0› kk [of "(x,z)" "(w,z)"] by (force simp add: norm_minus_commute dist_norm)
        show ?thesis
          using ax unfolding lim_sequentially eventually_sequentially
          apply (drule_tac x="min dd kk" in spec)
          using ‹dd > 0› ‹kk > 0›
          apply (fastforce simp: kk dist_norm)
          done
      qed
      have tendsto_hx: "(λn. contour_integral γ (d (a n))) ⇢ h x"
        apply (simp add: contour_integral_unique [OF U, symmetric] x)
        apply (rule contour_integral_uniform_limit [OF A1 A2 le_B])
        apply (auto simp: ‹valid_path γ›)
        done
      then show "(h ∘ a) ⇢ h x"
        by (simp add: h_def x au o_def)
    qed
    show ?thesis
    proof (simp add: holomorphic_on_open field_differentiable_def [symmetric], clarify)
      fix z0
      consider "z0 ∈ v" | "z0 ∈ u" using uv_Un by blast
      then show "h field_differentiable at z0"
      proof cases
        assume "z0 ∈ v" then show ?thesis
          using Cauchy_next_derivative [OF con_pa_f le_B f_has_cint _ ov] V f_has_cint ‹valid_path γ›
          by (auto simp: field_differentiable_def v_def)
      next
        assume "z0 ∈ u" then
        obtain e where "e>0" and e: "ball z0 e ⊆ u" by (blast intro: openE [OF u])
        have *: "contour_integral (linepath a b) h + contour_integral (linepath b c) h + contour_integral (linepath c a) h = 0"
                if abc_subset: "convex hull {a, b, c} ⊆ ball z0 e"  for a b c
        proof -
          have *: "⋀x1 x2 z. z ∈ u ⟹ closed_segment x1 x2 ⊆ u ⟹ (λw. d w z) contour_integrable_on linepath x1 x2"
            using  hol_dw holomorphic_on_imp_continuous_on u
            by (auto intro!: contour_integrable_holomorphic_simple)
          have abc: "closed_segment a b ⊆ u"  "closed_segment b c ⊆ u"  "closed_segment c a ⊆ u"
            using that e segments_subset_convex_hull by fastforce+
          have eq0: "⋀w. w ∈ u ⟹ contour_integral (linepath a b +++ linepath b c +++ linepath c a) (λz. d z w) = 0"
            apply (rule contour_integral_unique [OF Cauchy_theorem_triangle])
            apply (rule holomorphic_on_subset [OF hol_dw])
            using e abc_subset by auto
          have "contour_integral γ
                   (λx. contour_integral (linepath a b) (λz. d z x) +
                        (contour_integral (linepath b c) (λz. d z x) +
                         contour_integral (linepath c a) (λz. d z x)))  =  0"
            apply (rule contour_integral_eq_0)
            using abc pasz u
            apply (subst contour_integral_join [symmetric], auto intro: eq0 *)+
            done
          then show ?thesis
            by (simp add: cint_h abc contour_integrable_add contour_integral_add [symmetric] add_ac)
        qed
        show ?thesis
          using e ‹e > 0›
          by (auto intro!: holomorphic_on_imp_differentiable_at [OF _ open_ball] analytic_imp_holomorphic
                           Morera_triangle continuous_on_subset [OF conthu] *)
      qed
    qed
  qed
  ultimately have [simp]: "h z = 0" for z
    by (meson Liouville_weak)
  have "((λw. 1 / (w - z)) has_contour_integral complex_of_real (2 * pi) * 𝗂 * winding_number γ z) γ"
    by (rule has_contour_integral_winding_number [OF ‹valid_path γ› znot])
  then have "((λw. f z * (1 / (w - z))) has_contour_integral complex_of_real (2 * pi) * 𝗂 * winding_number γ z * f z) γ"
    by (metis mult.commute has_contour_integral_lmul)
  then have 1: "((λw. f z / (w - z)) has_contour_integral complex_of_real (2 * pi) * 𝗂 * winding_number γ z * f z) γ"
    by (simp add: divide_simps)
  moreover have 2: "((λw. (f w - f z) / (w - z)) has_contour_integral 0) γ"
    using U [OF z] pasz d_def by (force elim: has_contour_integral_eq [where g = "λw. (f w - f z)/(w - z)"])
  show ?thesis
    using has_contour_integral_add [OF 1 2]  by (simp add: diff_divide_distrib)
qed


theorem Cauchy_integral_formula_global:
    assumes s: "open s" and holf: "f holomorphic_on s"
        and z: "z ∈ s" and vpg: "valid_path γ"
        and pasz: "path_image γ ⊆ s - {z}" and loop: "pathfinish γ = pathstart γ"
        and zero: "⋀w. w ∉ s ⟹ winding_number γ w = 0"
      shows "((λw. f w / (w - z)) has_contour_integral (2*pi * ii * winding_number γ z * f z)) γ"
proof -
  have "path γ" using vpg by (blast intro: valid_path_imp_path)
  have hols: "(λw. f w / (w - z)) holomorphic_on s - {z}" "(λw. 1 / (w - z)) holomorphic_on s - {z}"
    by (rule holomorphic_intros holomorphic_on_subset [OF holf] | force)+
  then have cint_fw: "(λw. f w / (w - z)) contour_integrable_on γ"
    by (meson contour_integrable_holomorphic_simple holomorphic_on_imp_continuous_on open_delete s vpg pasz)
  obtain d where "d>0"
      and d: "⋀g h. ⟦valid_path g; valid_path h; ∀t∈{0..1}. cmod (g t - γ t) < d ∧ cmod (h t - γ t) < d;
                     pathstart h = pathstart g ∧ pathfinish h = pathfinish g⟧
                     ⟹ path_image h ⊆ s - {z} ∧ (∀f. f holomorphic_on s - {z} ⟶ contour_integral h f = contour_integral g f)"
    using contour_integral_nearby_ends [OF _ ‹path γ› pasz] s by (simp add: open_Diff) metis
  obtain p where polyp: "polynomial_function p"
             and ps: "pathstart p = pathstart γ" and pf: "pathfinish p = pathfinish γ" and led: "∀t∈{0..1}. cmod (p t - γ t) < d"
    using path_approx_polynomial_function [OF ‹path γ› ‹d > 0›] by blast
  then have ploop: "pathfinish p = pathstart p" using loop by auto
  have vpp: "valid_path p"  using polyp valid_path_polynomial_function by blast
  have [simp]: "z ∉ path_image γ" using pasz by blast
  have paps: "path_image p ⊆ s - {z}" and cint_eq: "(⋀f. f holomorphic_on s - {z} ⟹ contour_integral p f = contour_integral γ f)"
    using pf ps led d [OF vpg vpp] ‹d > 0› by auto
  have wn_eq: "winding_number p z = winding_number γ z"
    using vpp paps
    by (simp add: subset_Diff_insert vpg valid_path_polynomial_function winding_number_valid_path cint_eq hols)
  have "winding_number p w = winding_number γ w" if "w ∉ s" for w
  proof -
    have hol: "(λv. 1 / (v - w)) holomorphic_on s - {z}"
      using that by (force intro: holomorphic_intros holomorphic_on_subset [OF holf])
   have "w ∉ path_image p" "w ∉ path_image γ" using paps pasz that by auto
   then show ?thesis
    using vpp vpg by (simp add: subset_Diff_insert valid_path_polynomial_function winding_number_valid_path cint_eq [OF hol])
  qed
  then have wn0: "⋀w. w ∉ s ⟹ winding_number p w = 0"
    by (simp add: zero)
  show ?thesis
    using Cauchy_integral_formula_global_weak [OF s holf z polyp paps ploop wn0] hols
    by (metis wn_eq cint_eq has_contour_integral_eqpath cint_fw cint_eq)
qed

theorem Cauchy_theorem_global:
    assumes s: "open s" and holf: "f holomorphic_on s"
        and vpg: "valid_path γ" and loop: "pathfinish γ = pathstart γ"
        and pas: "path_image γ ⊆ s"
        and zero: "⋀w. w ∉ s ⟹ winding_number γ w = 0"
      shows "(f has_contour_integral 0) γ"
proof -
  obtain z where "z ∈ s" and znot: "z ∉ path_image γ"
  proof -
    have "compact (path_image γ)"
      using compact_valid_path_image vpg by blast
    then have "path_image γ ≠ s"
      by (metis (no_types) compact_open path_image_nonempty s)
    with pas show ?thesis by (blast intro: that)
  qed
  then have pasz: "path_image γ ⊆ s - {z}" using pas by blast
  have hol: "(λw. (w - z) * f w) holomorphic_on s"
    by (rule holomorphic_intros holf)+
  show ?thesis
    using Cauchy_integral_formula_global [OF s hol ‹z ∈ s› vpg pasz loop zero]
    by (auto simp: znot elim!: has_contour_integral_eq)
qed

corollary Cauchy_theorem_global_outside:
    assumes "open s" "f holomorphic_on s" "valid_path γ"  "pathfinish γ = pathstart γ" "path_image γ ⊆ s"
            "⋀w. w ∉ s ⟹ w ∈ outside(path_image γ)"
      shows "(f has_contour_integral 0) γ"
by (metis Cauchy_theorem_global assms winding_number_zero_in_outside valid_path_imp_path)


end