Split off new HOL-Complex_Analysis session from HOL-Analysis
authorManuel Eberl <eberlm@in.tum.de>
Sat, 30 Nov 2019 13:47:33 +0100
changeset 71189 954ee5acaae0
parent 71181 8331063570d6
child 71190 8b8f9d3b3fac
Split off new HOL-Complex_Analysis session from HOL-Analysis
src/HOL/Analysis/Analysis.thy
src/HOL/Analysis/Cauchy_Integral_Theorem.thy
src/HOL/Analysis/Change_Of_Vars.thy
src/HOL/Analysis/Complex_Analysis_Basics.thy
src/HOL/Analysis/Conformal_Mappings.thy
src/HOL/Analysis/Derivative.thy
src/HOL/Analysis/FPS_Convergence.thy
src/HOL/Analysis/Gamma_Function.thy
src/HOL/Analysis/Great_Picard.thy
src/HOL/Analysis/Line_Segment.thy
src/HOL/Analysis/Path_Connected.thy
src/HOL/Analysis/Smooth_Paths.thy
src/HOL/Analysis/Vitali_Covering_Theorem.thy
src/HOL/Analysis/Winding_Numbers.thy
src/HOL/Complex_Analysis/Cauchy_Integral_Theorem.thy
src/HOL/Complex_Analysis/Complex_Analysis.thy
src/HOL/Complex_Analysis/Conformal_Mappings.thy
src/HOL/Complex_Analysis/Great_Picard.thy
src/HOL/Complex_Analysis/Riemann_Mapping.thy
src/HOL/Complex_Analysis/Winding_Numbers.thy
src/HOL/Complex_Analysis/document/root.bib
src/HOL/Complex_Analysis/document/root.tex
src/HOL/ROOT
--- a/src/HOL/Analysis/Analysis.thy	Wed Nov 27 16:54:33 2019 +0000
+++ b/src/HOL/Analysis/Analysis.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -35,16 +35,14 @@
   Weierstrass_Theorems
   Polytope
   Jordan_Curve
-  Winding_Numbers
-  Riemann_Mapping
   Poly_Roots
-  Conformal_Mappings
-  FPS_Convergence
   Generalised_Binomial_Theorem
   Gamma_Function
   Change_Of_Vars
   Multivariate_Analysis
   Simplex_Content
+  FPS_Convergence
+  Smooth_Paths
 begin
 
 end
--- a/src/HOL/Analysis/Cauchy_Integral_Theorem.thy	Wed Nov 27 16:54:33 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7847 +0,0 @@
-section \<open>Complex Path Integrals and Cauchy's Integral Theorem\<close>
-
-text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2015)\<close>
-
-theory Cauchy_Integral_Theorem
-imports
-  Complex_Transcendental
-  Henstock_Kurzweil_Integration
-  Weierstrass_Theorems
-  Retracts
-begin
-
-lemma leibniz_rule_holomorphic:
-  fixes f::"complex \<Rightarrow> 'b::euclidean_space \<Rightarrow> complex"
-  assumes "\<And>x t. x \<in> U \<Longrightarrow> t \<in> cbox a b \<Longrightarrow> ((\<lambda>x. f x t) has_field_derivative fx x t) (at x within U)"
-  assumes "\<And>x. x \<in> U \<Longrightarrow> (f x) integrable_on cbox a b"
-  assumes "continuous_on (U \<times> (cbox a b)) (\<lambda>(x, t). fx x t)"
-  assumes "convex U"
-  shows "(\<lambda>x. integral (cbox a b) (f x)) holomorphic_on U"
-  using leibniz_rule_field_differentiable[OF assms(1-3) _ assms(4)]
-  by (auto simp: holomorphic_on_def)
-
-lemma Ln_measurable [measurable]: "Ln \<in> measurable borel borel"
-proof -
-  have *: "Ln (-of_real x) = of_real (ln x) + \<i> * pi" if "x > 0" for x
-    using that by (subst Ln_minus) (auto simp: Ln_of_real)
-  have **: "Ln (of_real x) = of_real (ln (-x)) + \<i> * pi" if "x < 0" for x
-    using *[of "-x"] that by simp
-  have cont: "(\<lambda>x. indicat_real (- \<real>\<^sub>\<le>\<^sub>0) x *\<^sub>R Ln x) \<in> borel_measurable borel"
-    by (intro borel_measurable_continuous_on_indicator continuous_intros) auto
-  have "(\<lambda>x. if x \<in> \<real>\<^sub>\<le>\<^sub>0 then ln (-Re x) + \<i> * pi else indicator (-\<real>\<^sub>\<le>\<^sub>0) x *\<^sub>R Ln x) \<in> borel \<rightarrow>\<^sub>M borel"
-    (is "?f \<in> _") by (rule measurable_If_set[OF _ cont]) auto
-  hence "(\<lambda>x. if x = 0 then Ln 0 else ?f x) \<in> borel \<rightarrow>\<^sub>M borel" by measurable
-  also have "(\<lambda>x. if x = 0 then Ln 0 else ?f x) = Ln"
-    by (auto simp: fun_eq_iff ** nonpos_Reals_def)
-  finally show ?thesis .
-qed
-
-lemma powr_complex_measurable [measurable]:
-  assumes [measurable]: "f \<in> measurable M borel" "g \<in> measurable M borel"
-  shows   "(\<lambda>x. f x powr g x :: complex) \<in> measurable M borel"
-  using assms by (simp add: powr_def)
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Homeomorphisms of arc images\<close>
-
-lemma homeomorphism_arc:
-  fixes g :: "real \<Rightarrow> 'a::t2_space"
-  assumes "arc g"
-  obtains h where "homeomorphism {0..1} (path_image g) g h"
-using assms by (force simp: arc_def homeomorphism_compact path_def path_image_def)
-
-lemma homeomorphic_arc_image_interval:
-  fixes g :: "real \<Rightarrow> '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 "\<dots> homeomorphic {a..b}"
-    using assms by (force intro: homeomorphic_closed_intervals_real)
-  finally show ?thesis .
-qed
-
-lemma homeomorphic_arc_images:
-  fixes g :: "real \<Rightarrow> 'a::t2_space" and h :: "real \<Rightarrow> '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 "\<dots> homeomorphic (path_image h)"
-    by (meson assms homeomorphic_def homeomorphism_arc)
-  finally show ?thesis .
-qed
-
-lemma path_connected_arc_complement:
-  fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
-  assumes "arc \<gamma>" "2 \<le> DIM('a)"
-  shows "path_connected(- path_image \<gamma>)"
-proof -
-  have "path_image \<gamma> homeomorphic {0..1::real}"
-    by (simp add: assms homeomorphic_arc_image_interval)
-  then
-  show ?thesis
-    apply (rule path_connected_complement_homeomorphic_convex_compact)
-      apply (auto simp: assms)
-    done
-qed
-
-lemma connected_arc_complement:
-  fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
-  assumes "arc \<gamma>" "2 \<le> DIM('a)"
-  shows "connected(- path_image \<gamma>)"
-  by (simp add: assms path_connected_arc_complement path_connected_imp_connected)
-
-lemma inside_arc_empty:
-  fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
-  assumes "arc \<gamma>"
-    shows "inside(path_image \<gamma>) = {}"
-proof (cases "DIM('a) = 1")
-  case True
-  then show ?thesis
-    using assms connected_arc_image connected_convex_1_gen inside_convex by blast
-next
-  case False
-  show ?thesis
-  proof (rule inside_bounded_complement_connected_empty)
-    show "connected (- path_image \<gamma>)"
-      apply (rule connected_arc_complement [OF assms])
-      using False
-      by (metis DIM_ge_Suc0 One_nat_def Suc_1 not_less_eq_eq order_class.order.antisym)
-    show "bounded (path_image \<gamma>)"
-      by (simp add: assms bounded_arc_image)
-  qed
-qed
-
-lemma inside_simple_curve_imp_closed:
-  fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
-    shows "\<lbrakk>simple_path \<gamma>; x \<in> inside(path_image \<gamma>)\<rbrakk> \<Longrightarrow> pathfinish \<gamma> = pathstart \<gamma>"
-  using arc_simple_path  inside_arc_empty by blast
-
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Piecewise differentiable functions\<close>
-
-definition piecewise_differentiable_on
-           (infixr "piecewise'_differentiable'_on" 50)
-  where "f piecewise_differentiable_on i  \<equiv>
-           continuous_on i f \<and>
-           (\<exists>S. finite S \<and> (\<forall>x \<in> i - S. f differentiable (at x within i)))"
-
-lemma piecewise_differentiable_on_imp_continuous_on:
-    "f piecewise_differentiable_on S \<Longrightarrow> continuous_on S f"
-by (simp add: piecewise_differentiable_on_def)
-
-lemma piecewise_differentiable_on_subset:
-    "f piecewise_differentiable_on S \<Longrightarrow> T \<le> S \<Longrightarrow> f piecewise_differentiable_on T"
-  using continuous_on_subset
-  unfolding piecewise_differentiable_on_def
-  apply safe
-  apply (blast 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} \<Longrightarrow> 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:
-    "(\<And>x. x \<in> S \<Longrightarrow> f differentiable (at x within S))
-         \<Longrightarrow> 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]: "(\<lambda>x. z) piecewise_differentiable_on S"
-  by (simp add: differentiable_imp_piecewise_differentiable)
-
-lemma piecewise_differentiable_compose:
-    "\<lbrakk>f piecewise_differentiable_on S; g piecewise_differentiable_on (f ` S);
-      \<And>x. finite (S \<inter> f-`{x})\<rbrakk>
-      \<Longrightarrow> (g \<circ> 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 \<union> (\<Union>x\<in>B. S \<inter> f-`{x})" in exI)
-  apply (blast intro!: differentiable_chain_within)
-  done
-
-lemma piecewise_differentiable_affine:
-  fixes m::real
-  assumes "f piecewise_differentiable_on ((\<lambda>x. m *\<^sub>R x + c) ` S)"
-  shows "(f \<circ> (\<lambda>x. m *\<^sub>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 \<le> c" "c \<le> b" "f c = g c"
-  shows "(\<lambda>x. if x \<le> c then f x else g x) piecewise_differentiable_on {a..b}"
-proof -
-  obtain S T where st: "finite S" "finite T"
-               and fd: "\<And>x. x \<in> {a..c} - S \<Longrightarrow> f differentiable at x within {a..c}"
-               and gd: "\<And>x. x \<in> {c..b} - T \<Longrightarrow> g differentiable at x within {c..b}"
-    using assms
-    by (auto simp: piecewise_differentiable_on_def)
-  have finabc: "finite ({a,b,c} \<union> (S \<union> T))"
-    by (metis \<open>finite S\<close> \<open>finite T\<close> 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} (\<lambda>x. if x \<le> 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 "\<lambda>x. x\<le>c"]  assms
-    by (force simp: ivl_disj_un_two_touch)
-  moreover
-  { fix x
-    assume x: "x \<in> {a..b} - ({a,b,c} \<union> (S \<union> T))"
-    have "(\<lambda>x. if x \<le> 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
-      proof (rule differentiable_transform_within [where d = "dist x c"])
-        have "f differentiable at x"
-          using x le fd [of x] at_within_interior [of x "{a..c}"] by simp
-        then show "f differentiable at x within {a..b}"
-          by (simp add: differentiable_at_withinI)
-      qed (use x le st dist_real_def in auto)
-    next
-      case ge show ?diff_fg
-      proof (rule differentiable_transform_within [where d = "dist x c"])
-        have "g differentiable at x"
-          using x ge gd [of x] at_within_interior [of x "{c..b}"] by simp
-        then show "g differentiable at x within {a..b}"
-          by (simp add: differentiable_at_withinI)
-      qed (use x ge st dist_real_def in auto)
-    qed
-  }
-  then have "\<exists>S. finite S \<and>
-                 (\<forall>x\<in>{a..b} - S. (\<lambda>x. if x \<le> 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 \<Longrightarrow> (\<lambda>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 "(\<lambda>x. f x + g x) piecewise_differentiable_on i"
-proof -
-  obtain S T where st: "finite S" "finite T"
-                       "\<forall>x\<in>i - S. f differentiable at x within i"
-                       "\<forall>x\<in>i - T. g differentiable at x within i"
-    using assms by (auto simp: piecewise_differentiable_on_def)
-  then have "finite (S \<union> T) \<and> (\<forall>x\<in>i - (S \<union> T). (\<lambda>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:
-    "\<lbrakk>f piecewise_differentiable_on S;  g piecewise_differentiable_on S\<rbrakk>
-     \<Longrightarrow> (\<lambda>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) \<Longrightarrow> continuous_on {0..1} g1"
-  apply (rule continuous_on_eq [of _ "(g1 +++ g2) \<circ> ((*)(inverse 2))"])
-  apply (rule continuous_intros | simp)+
-  apply (auto elim!: continuous_on_subset simp: joinpaths_def)
-  done
-
-lemma continuous_on_joinpaths_D2:
-    "\<lbrakk>continuous_on {0..1} (g1 +++ g2); pathfinish g1 = pathstart g2\<rbrakk> \<Longrightarrow> continuous_on {0..1} g2"
-  apply (rule continuous_on_eq [of _ "(g1 +++ g2) \<circ> (\<lambda>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:
-  assumes "(g1 +++ g2) piecewise_differentiable_on {0..1}"
-  shows "g1 piecewise_differentiable_on {0..1}"
-proof -
-  obtain S where cont: "continuous_on {0..1} g1" and "finite S"
-    and S: "\<And>x. x \<in> {0..1} - S \<Longrightarrow> g1 +++ g2 differentiable at x within {0..1}"
-    using assms unfolding piecewise_differentiable_on_def
-    by (blast dest!: continuous_on_joinpaths_D1)
-  show ?thesis
-    unfolding piecewise_differentiable_on_def
-  proof (intro exI conjI ballI cont)
-    show "finite (insert 1 (((*)2) ` S))"
-      by (simp add: \<open>finite S\<close>)
-    show "g1 differentiable at x within {0..1}" if "x \<in> {0..1} - insert 1 ((*) 2 ` S)" for x
-    proof (rule_tac d="dist (x/2) (1/2)" in differentiable_transform_within)
-      have "g1 +++ g2 differentiable at (x / 2) within {0..1/2}"
-        by (rule differentiable_subset [OF S [of "x/2"]] | use that in force)+
-      then show "g1 +++ g2 \<circ> (*) (inverse 2) differentiable at x within {0..1}"
-        using image_affinity_atLeastAtMost_div [of 2 0 "0::real" 1]
-        by (auto intro: differentiable_chain_within)
-    qed (use that in \<open>auto simp: joinpaths_def\<close>)
-  qed
-qed
-
-lemma piecewise_differentiable_D2:
-  assumes "(g1 +++ g2) piecewise_differentiable_on {0..1}" and eq: "pathfinish g1 = pathstart g2"
-  shows "g2 piecewise_differentiable_on {0..1}"
-proof -
-  have [simp]: "g1 1 = g2 0"
-    using eq by (simp add: pathfinish_def pathstart_def)
-  obtain S where cont: "continuous_on {0..1} g2" and "finite S"
-    and S: "\<And>x. x \<in> {0..1} - S \<Longrightarrow> g1 +++ g2 differentiable at x within {0..1}"
-    using assms unfolding piecewise_differentiable_on_def
-    by (blast dest!: continuous_on_joinpaths_D2)
-  show ?thesis
-    unfolding piecewise_differentiable_on_def
-  proof (intro exI conjI ballI cont)
-    show "finite (insert 0 ((\<lambda>x. 2*x-1)`S))"
-      by (simp add: \<open>finite S\<close>)
-    show "g2 differentiable at x within {0..1}" if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1)`S)" for x
-    proof (rule_tac d="dist ((x+1)/2) (1/2)" in differentiable_transform_within)
-      have x2: "(x + 1) / 2 \<notin> S"
-        using that
-        apply (clarsimp simp: image_iff)
-        by (metis add.commute add_diff_cancel_left' mult_2 field_sum_of_halves)
-      have "g1 +++ g2 \<circ> (\<lambda>x. (x+1) / 2) differentiable at x within {0..1}"
-        by (rule differentiable_chain_within differentiable_subset [OF S [of "(x+1)/2"]] | use x2 that in force)+
-      then show "g1 +++ g2 \<circ> (\<lambda>x. (x+1) / 2) differentiable at x within {0..1}"
-        by (auto intro: differentiable_chain_within)
-      show "(g1 +++ g2 \<circ> (\<lambda>x. (x + 1) / 2)) x' = g2 x'" if "x' \<in> {0..1}" "dist x' x < dist ((x + 1) / 2) (1/2)" for x'
-      proof -
-        have [simp]: "(2*x'+2)/2 = x'+1"
-          by (simp add: field_split_simps)
-        show ?thesis
-          using that by (auto simp: joinpaths_def)
-      qed
-    qed (use that in \<open>auto simp: joinpaths_def\<close>)
-  qed
-qed
-
-
-subsection\<open>The concept of continuously differentiable\<close>
-
-text \<open>
-John Harrison writes as follows:
-
-``The usual assumption in complex analysis texts is that a path \<open>\<gamma>\<close> 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\ldots{} [namely] continuity plus differentiability except on a
-finite set\ldots{} [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.\<close>
-
-definition\<^marker>\<open>tag important\<close> C1_differentiable_on :: "(real \<Rightarrow> 'a::real_normed_vector) \<Rightarrow> real set \<Rightarrow> bool"
-           (infix "C1'_differentiable'_on" 50)
-  where
-  "f C1_differentiable_on S \<longleftrightarrow>
-   (\<exists>D. (\<forall>x \<in> S. (f has_vector_derivative (D x)) (at x)) \<and> continuous_on S D)"
-
-lemma C1_differentiable_on_eq:
-    "f C1_differentiable_on S \<longleftrightarrow>
-     (\<forall>x \<in> S. f differentiable at x) \<and> continuous_on S (\<lambda>x. vector_derivative f (at x))"
-     (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then show ?rhs
-    unfolding C1_differentiable_on_def
-    by (metis (no_types, lifting) continuous_on_eq  differentiableI_vector vector_derivative_at)
-next
-  assume ?rhs
-  then show ?lhs
-    using C1_differentiable_on_def vector_derivative_works by fastforce
-qed
-
-lemma C1_differentiable_on_subset:
-  "f C1_differentiable_on T \<Longrightarrow> S \<subseteq> T \<Longrightarrow> 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:
-  assumes fg: "f C1_differentiable_on S" "g C1_differentiable_on (f ` S)" and fin: "\<And>x. finite (S \<inter> f-`{x})"
-  shows "(g \<circ> f) C1_differentiable_on S"
-proof -
-  have "\<And>x. x \<in> S \<Longrightarrow> g \<circ> f differentiable at x"
-    by (meson C1_differentiable_on_eq assms differentiable_chain_at imageI)
-  moreover have "continuous_on S (\<lambda>x. vector_derivative (g \<circ> f) (at x))"
-  proof (rule continuous_on_eq [of _ "\<lambda>x. vector_derivative f (at x) *\<^sub>R vector_derivative g (at (f x))"])
-    show "continuous_on S (\<lambda>x. vector_derivative f (at x) *\<^sub>R vector_derivative g (at (f x)))"
-      using fg
-      apply (clarsimp simp add: C1_differentiable_on_eq)
-      apply (rule Limits.continuous_on_scaleR, assumption)
-      by (metis (mono_tags, lifting) continuous_at_imp_continuous_on continuous_on_compose continuous_on_cong differentiable_imp_continuous_within o_def)
-    show "\<And>x. x \<in> S \<Longrightarrow> vector_derivative f (at x) *\<^sub>R vector_derivative g (at (f x)) = vector_derivative (g \<circ> f) (at x)"
-      by (metis (mono_tags, hide_lams) C1_differentiable_on_eq fg imageI vector_derivative_chain_at)
-  qed
-  ultimately show ?thesis
-    by (simp add: C1_differentiable_on_eq)
-qed
-
-lemma C1_diff_imp_diff: "f C1_differentiable_on S \<Longrightarrow> f differentiable_on S"
-  by (simp add: C1_differentiable_on_eq differentiable_at_imp_differentiable_on)
-
-lemma C1_differentiable_on_ident [simp, derivative_intros]: "(\<lambda>x. x) C1_differentiable_on S"
-  by (auto simp: C1_differentiable_on_eq)
-
-lemma C1_differentiable_on_const [simp, derivative_intros]: "(\<lambda>z. a) C1_differentiable_on S"
-  by (auto simp: C1_differentiable_on_eq)
-
-lemma C1_differentiable_on_add [simp, derivative_intros]:
-  "f C1_differentiable_on S \<Longrightarrow> g C1_differentiable_on S \<Longrightarrow> (\<lambda>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 \<Longrightarrow> (\<lambda>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 \<Longrightarrow> g C1_differentiable_on S \<Longrightarrow> (\<lambda>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 \<Rightarrow> 'a :: real_normed_algebra"
-  shows "f C1_differentiable_on S \<Longrightarrow> g C1_differentiable_on S \<Longrightarrow> (\<lambda>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 \<Longrightarrow> g C1_differentiable_on S \<Longrightarrow> (\<lambda>x. f x *\<^sub>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\<^marker>\<open>tag important\<close> piecewise_C1_differentiable_on
-           (infixr "piecewise'_C1'_differentiable'_on" 50)
-  where "f piecewise_C1_differentiable_on i  \<equiv>
-           continuous_on i f \<and>
-           (\<exists>S. finite S \<and> (f C1_differentiable_on (i - S)))"
-
-lemma C1_differentiable_imp_piecewise:
-    "f C1_differentiable_on S \<Longrightarrow> 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 \<Longrightarrow> 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_withinI)
-
-lemma piecewise_C1_differentiable_compose:
-  assumes fg: "f piecewise_C1_differentiable_on S" "g piecewise_C1_differentiable_on (f ` S)" and fin: "\<And>x. finite (S \<inter> f-`{x})"
-  shows "(g \<circ> f) piecewise_C1_differentiable_on S"
-proof -
-  have "continuous_on S (\<lambda>x. g (f x))"
-    by (metis continuous_on_compose2 fg order_refl piecewise_C1_differentiable_on_def)
-  moreover have "\<exists>T. finite T \<and> g \<circ> f C1_differentiable_on S - T"
-  proof -
-    obtain F where "finite F" and F: "f C1_differentiable_on S - F" and f: "f piecewise_C1_differentiable_on S"
-      using fg by (auto simp: piecewise_C1_differentiable_on_def)
-    obtain G where "finite G" and G: "g C1_differentiable_on f ` S - G" and g: "g piecewise_C1_differentiable_on f ` S"
-      using fg by (auto simp: piecewise_C1_differentiable_on_def)
-    show ?thesis
-    proof (intro exI conjI)
-      show "finite (F \<union> (\<Union>x\<in>G. S \<inter> f-`{x}))"
-        using fin by (auto simp only: Int_Union \<open>finite F\<close> \<open>finite G\<close> finite_UN finite_imageI)
-      show "g \<circ> f C1_differentiable_on S - (F \<union> (\<Union>x\<in>G. S \<inter> f -` {x}))"
-        apply (rule C1_differentiable_compose)
-          apply (blast intro: C1_differentiable_on_subset [OF F])
-          apply (blast intro: C1_differentiable_on_subset [OF G])
-        by (simp add:  C1_differentiable_on_subset G Diff_Int_distrib2 fin)
-    qed
-  qed
-  ultimately show ?thesis
-    by (simp add: piecewise_C1_differentiable_on_def)
-qed
-
-lemma piecewise_C1_differentiable_on_subset:
-    "f piecewise_C1_differentiable_on S \<Longrightarrow> T \<le> S \<Longrightarrow> 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 \<Longrightarrow> 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 ((\<lambda>x. m * x + c) ` S)"
-  shows "(f \<circ> (\<lambda>x. m *\<^sub>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)
-next
-  case False
-  have *: "\<And>x. finite (S \<inter> {y. m * y + c = x})"
-    using False not_finite_existsD by fastforce
-  show ?thesis
-    apply (rule piecewise_C1_differentiable_compose [OF C1_differentiable_imp_piecewise])
-    apply (rule * assms derivative_intros | simp add: False vimage_def)+
-    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 \<le> c" "c \<le> b" "f c = g c"
-  shows "(\<lambda>x. if x \<le> 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} (\<lambda>x. if x \<le> 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 "\<lambda>x. x\<le>c"]  assms
-    by (force simp: ivl_disj_un_two_touch)
-  { fix x
-    assume x: "x \<in> {a..b} - insert c (S \<union> T)"
-    have "(\<lambda>x. if x \<le> 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 "(\<forall>x \<in> {a..b} - insert c (S \<union> T). (\<lambda>x. if x \<le> c then f x else g x) differentiable at x)"
-    by auto
-  moreover
-  { assume fcon: "continuous_on ({a<..<c} - S) (\<lambda>x. vector_derivative f (at x))"
-       and gcon: "continuous_on ({c<..<b} - T) (\<lambda>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) (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
-    proof -
-      have "((\<lambda>x. if x \<le> c then f x else g x) has_vector_derivative vector_derivative f (at x))            (at x)"
-        if "a < x" "x < c" "x \<notin> S" for x
-      proof -
-        have f: "f differentiable at x"
-          by (meson C1_differentiable_on_eq Diff_iff atLeastAtMost_iff less_eq_real_def st(1) that)
-        show ?thesis
-          using that
-          apply (rule_tac f=f and d="dist x c" in has_vector_derivative_transform_within)
-             apply (auto simp: dist_norm vector_derivative_works [symmetric] f)
-          done
-      qed
-      then show ?thesis
-        by (metis (no_types, lifting) continuous_on_eq [OF fcon] DiffE greaterThanLessThan_iff vector_derivative_at)
-    qed
-    moreover have "continuous_on ({c<..<b} - T) (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
-    proof -
-      have "((\<lambda>x. if x \<le> c then f x else g x) has_vector_derivative vector_derivative g (at x))            (at x)"
-        if "c < x" "x < b" "x \<notin> T" for x
-      proof -
-        have g: "g differentiable at x"
-          by (metis C1_differentiable_on_eq DiffD1 DiffI atLeastAtMost_diff_ends greaterThanLessThan_iff st(2) that)
-        show ?thesis
-          using that
-          apply (rule_tac f=g and d="dist x c" in has_vector_derivative_transform_within)
-             apply (auto simp: dist_norm vector_derivative_works [symmetric] g)
-          done
-      qed
-      then show ?thesis
-        by (metis (no_types, lifting) continuous_on_eq [OF gcon] DiffE greaterThanLessThan_iff vector_derivative_at)
-    qed
-    ultimately have "continuous_on ({a<..<b} - insert c (S \<union> T))
-        (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
-      by (rule continuous_on_subset [OF continuous_on_open_Un], auto)
-  } note * = this
-  have "continuous_on ({a<..<b} - insert c (S \<union> T)) (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> 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 "\<exists>S. finite S \<and> ((\<lambda>x. if x \<le> c then f x else g x) C1_differentiable_on {a..b} - S)"
-    apply (rule_tac x="{a,b,c} \<union> S \<union> 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 \<Longrightarrow> (\<lambda>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 "(\<lambda>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 \<union> t) \<and> (\<lambda>x. f x + g x) C1_differentiable_on i - (S \<union> 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:
-    "\<lbrakk>f piecewise_C1_differentiable_on S;  g piecewise_C1_differentiable_on S\<rbrakk>
-     \<Longrightarrow> (\<lambda>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 \<Rightarrow> '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) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
-             and g12D: "\<forall>x\<in>{0..1} - S. g1 +++ g2 differentiable at x"
-    using assms  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
-  have g1D: "g1 differentiable at x" if "x \<in> {0..1} - insert 1 ((*) 2 ` S)" for x
-  proof (rule differentiable_transform_within)
-    show "g1 +++ g2 \<circ> (*) (inverse 2) differentiable at x"
-      using that g12D
-      apply (simp only: joinpaths_def)
-      by (rule differentiable_chain_at derivative_intros | force)+
-    show "\<And>x'. \<lbrakk>dist x' x < dist (x/2) (1/2)\<rbrakk>
-          \<Longrightarrow> (g1 +++ g2 \<circ> (*) (inverse 2)) x' = g1 x'"
-      using that by (auto simp: dist_real_def joinpaths_def)
-  qed (use that in \<open>auto simp: dist_real_def\<close>)
-  have [simp]: "vector_derivative (g1 \<circ> (*) 2) (at (x/2)) = 2 *\<^sub>R vector_derivative g1 (at x)"
-               if "x \<in> {0..1} - insert 1 ((*) 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) (\<lambda>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) (\<lambda>x. vector_derivative (g1 \<circ> (*)2) (at x))"
-  proof (rule continuous_on_eq [OF _ vector_derivative_at])
-    show "(g1 +++ g2 has_vector_derivative vector_derivative (g1 \<circ> (*) 2) (at x)) (at x)"
-      if "x \<in> {0..1/2} - insert (1/2) S" for x
-    proof (rule has_vector_derivative_transform_within)
-      show "(g1 \<circ> (*) 2 has_vector_derivative vector_derivative (g1 \<circ> (*) 2) (at x)) (at x)"
-        using that
-        by (force intro: g1D differentiable_chain_at simp: vector_derivative_works [symmetric])
-      show "\<And>x'. \<lbrakk>dist x' x < dist x (1/2)\<rbrakk> \<Longrightarrow> (g1 \<circ> (*) 2) x' = (g1 +++ g2) x'"
-        using that by (auto simp: dist_norm joinpaths_def)
-    qed (use that in \<open>auto simp: dist_norm\<close>)
-  qed
-  have "continuous_on ({0..1} - insert 1 ((*) 2 ` S))
-                      ((\<lambda>x. 1/2 * vector_derivative (g1 \<circ> (*)2) (at x)) \<circ> (*)(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 ((*) 2 ` S)) (\<lambda>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 \<open>finite S\<close> show ?thesis
-    apply (clarsimp simp add: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
-    apply (rule_tac x="insert 1 (((*)2)`S)" in exI)
-    apply (simp add: g1D con_g1)
-  done
-qed
-
-lemma piecewise_C1_differentiable_D2:
-  fixes g2 :: "real \<Rightarrow> '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) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
-             and g12D: "\<forall>x\<in>{0..1} - S. g1 +++ g2 differentiable at x"
-    using assms  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
-  have g2D: "g2 differentiable at x" if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1) ` S)" for x
-  proof (rule differentiable_transform_within)
-    show "g1 +++ g2 \<circ> (\<lambda>x. (x + 1) / 2) differentiable at x"
-      using g12D that
-      apply (simp only: joinpaths_def)
-      apply (drule_tac x= "(x+1) / 2" in bspec, force simp: field_split_simps)
-      apply (rule differentiable_chain_at derivative_intros | force)+
-      done
-    show "\<And>x'. dist x' x < dist ((x + 1) / 2) (1/2) \<Longrightarrow> (g1 +++ g2 \<circ> (\<lambda>x. (x + 1) / 2)) x' = g2 x'"
-      using that by (auto simp: dist_real_def joinpaths_def field_simps)
-    qed (use that in \<open>auto simp: dist_norm\<close>)
-  have [simp]: "vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at ((x+1)/2)) = 2 *\<^sub>R vector_derivative g2 (at x)"
-               if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1) ` S)" for x
-    using that  by (auto simp: vector_derivative_chain_at field_split_simps g2D)
-  have "continuous_on ({1/2..1} - insert (1/2) S) (\<lambda>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) (\<lambda>x. vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at x))"
-  proof (rule continuous_on_eq [OF _ vector_derivative_at])
-    show "(g1 +++ g2 has_vector_derivative vector_derivative (g2 \<circ> (\<lambda>x. 2 * x - 1)) (at x))
-          (at x)"
-      if "x \<in> {1 / 2..1} - insert (1 / 2) S" for x
-    proof (rule_tac f="g2 \<circ> (\<lambda>x. 2*x-1)" and d="dist (3/4) ((x+1)/2)" in has_vector_derivative_transform_within)
-      show "(g2 \<circ> (\<lambda>x. 2 * x - 1) has_vector_derivative vector_derivative (g2 \<circ> (\<lambda>x. 2 * x - 1)) (at x))
-            (at x)"
-        using that by (force intro: g2D differentiable_chain_at simp: vector_derivative_works [symmetric])
-      show "\<And>x'. \<lbrakk>dist x' x < dist (3 / 4) ((x + 1) / 2)\<rbrakk> \<Longrightarrow> (g2 \<circ> (\<lambda>x. 2 * x - 1)) x' = (g1 +++ g2) x'"
-        using that by (auto simp: dist_norm joinpaths_def add_divide_distrib)
-    qed (use that in \<open>auto simp: dist_norm\<close>)
-  qed
-  have [simp]: "((\<lambda>x. (x+1) / 2) ` ({0..1} - insert 0 ((\<lambda>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 ((\<lambda>x. 2*x-1) ` S))
-                      ((\<lambda>x. 1/2 * vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at x)) \<circ> (\<lambda>x. (x+1)/2))"
-    by (rule continuous_intros | simp add:  coDhalf)+
-  then have con_g2: "continuous_on ({0..1} - insert 0 ((\<lambda>x. 2*x-1) ` S)) (\<lambda>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 \<open>finite S\<close> show ?thesis
-    apply (clarsimp simp add: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
-    apply (rule_tac x="insert 0 ((\<lambda>x. 2 * x - 1) ` S)" in exI)
-    apply (simp add: g2D con_g2)
-  done
-qed
-
-subsection \<open>Valid paths, and their start and finish\<close>
-
-definition\<^marker>\<open>tag important\<close> valid_path :: "(real \<Rightarrow> 'a :: real_normed_vector) \<Rightarrow> bool"
-  where "valid_path f \<equiv> f piecewise_C1_differentiable_on {0..1::real}"
-
-definition closed_path :: "(real \<Rightarrow> 'a :: real_normed_vector) \<Rightarrow> bool"
-  where "closed_path g \<equiv> g 0 = g 1"
-
-text\<open>In particular, all results for paths apply\<close>
-
-lemma valid_path_imp_path: "valid_path g \<Longrightarrow> path g"
-  by (simp add: path_def piecewise_C1_differentiable_on_def valid_path_def)
-
-lemma connected_valid_path_image: "valid_path g \<Longrightarrow> connected(path_image g)"
-  by (metis connected_path_image valid_path_imp_path)
-
-lemma compact_valid_path_image: "valid_path g \<Longrightarrow> compact(path_image g)"
-  by (metis compact_path_image valid_path_imp_path)
-
-lemma bounded_valid_path_image: "valid_path g \<Longrightarrow> bounded(path_image g)"
-  by (metis bounded_path_image valid_path_imp_path)
-
-lemma closed_valid_path_image: "valid_path g \<Longrightarrow> closed(path_image g)"
-  by (metis closed_path_image valid_path_imp_path)
-
-lemma valid_path_compose:
-  assumes "valid_path g"
-      and der: "\<And>x. x \<in> path_image g \<Longrightarrow> f field_differentiable (at x)"
-      and con: "continuous_on (path_image g) (deriv f)"
-    shows "valid_path (f \<circ> g)"
-proof -
-  obtain S where "finite S" and g_diff: "g C1_differentiable_on {0..1} - S"
-    using \<open>valid_path g\<close> unfolding valid_path_def piecewise_C1_differentiable_on_def by auto
-  have "f \<circ> g differentiable at t" when "t\<in>{0..1} - S" for t
-    proof (rule differentiable_chain_at)
-      show "g differentiable at t" using \<open>valid_path g\<close>
-        by (meson C1_differentiable_on_eq \<open>g C1_differentiable_on {0..1} - S\<close> that)
-    next
-      have "g t\<in>path_image g" using that DiffD1 image_eqI path_image_def by metis
-      then show "f differentiable at (g t)"
-        using der[THEN field_differentiable_imp_differentiable] by auto
-    qed
-  moreover have "continuous_on ({0..1} - S) (\<lambda>x. vector_derivative (f \<circ> g) (at x))"
-    proof (rule continuous_on_eq [where f = "\<lambda>x. vector_derivative g (at x) * deriv f (g x)"],
-        rule continuous_intros)
-      show "continuous_on ({0..1} - S) (\<lambda>x. vector_derivative g (at x))"
-        using g_diff C1_differentiable_on_eq by auto
-    next
-      have "continuous_on {0..1} (\<lambda>x. deriv f (g x))"
-        using continuous_on_compose[OF _ con[unfolded path_image_def],unfolded comp_def]
-          \<open>valid_path g\<close> piecewise_C1_differentiable_on_def valid_path_def
-        by blast
-      then show "continuous_on ({0..1} - S) (\<lambda>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 \<circ> g) (at t)"
-          when "t \<in> {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\<in>path_image g" using that DiffD1 image_eqI path_image_def by metis
-          then show "f field_differentiable at (g t)" using der by auto
-        qed
-    qed
-  ultimately have "f \<circ> g C1_differentiable_on {0..1} - S"
-    using C1_differentiable_on_eq by blast
-  moreover have "path (f \<circ> g)"
-    apply (rule path_continuous_image[OF valid_path_imp_path[OF \<open>valid_path g\<close>]])
-    using der
-    by (simp add: continuous_at_imp_continuous_on field_differentiable_imp_continuous_at)
-  ultimately show ?thesis unfolding valid_path_def piecewise_C1_differentiable_on_def path_def
-    using \<open>finite S\<close> by auto
-qed
-  
-lemma valid_path_uminus_comp[simp]:
-  fixes g::"real \<Rightarrow> 'a ::real_normed_field"
-  shows "valid_path (uminus \<circ> g) \<longleftrightarrow> valid_path g"
-proof 
-  show "valid_path g \<Longrightarrow> valid_path (uminus \<circ> g)" for g::"real \<Rightarrow> 'a"
-    by (auto intro!: valid_path_compose derivative_intros simp add: deriv_linear[of "-1",simplified])  
-  then show "valid_path g" when "valid_path (uminus \<circ> g)"
-    by (metis fun.map_comp group_add_class.minus_comp_minus id_comp that)
-qed
-
-lemma valid_path_offset[simp]:
-  shows "valid_path (\<lambda>t. g t - z) \<longleftrightarrow> valid_path g"  
-proof 
-  show *: "valid_path (g::real\<Rightarrow>'a) \<Longrightarrow> valid_path (\<lambda>t. g t - z)" for g z
-    unfolding valid_path_def
-    by (fastforce intro:derivative_intros C1_differentiable_imp_piecewise piecewise_C1_differentiable_diff)
-  show "valid_path (\<lambda>t. g t - z) \<Longrightarrow> valid_path g"
-    using *[of "\<lambda>t. g t - z" "-z",simplified] .
-qed
-  
-
-subsection\<open>Contour Integrals along a path\<close>
-
-text\<open>This definition is for complex numbers only, and does not generalise to line integrals in a vector field\<close>
-
-text\<open>piecewise differentiable function on [0,1]\<close>
-
-definition\<^marker>\<open>tag important\<close> has_contour_integral :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> (real \<Rightarrow> complex) \<Rightarrow> bool"
-           (infixr "has'_contour'_integral" 50)
-  where "(f has_contour_integral i) g \<equiv>
-           ((\<lambda>x. f(g x) * vector_derivative g (at x within {0..1}))
-            has_integral i) {0..1}"
-
-definition\<^marker>\<open>tag important\<close> contour_integrable_on
-           (infixr "contour'_integrable'_on" 50)
-  where "f contour_integrable_on g \<equiv> \<exists>i. (f has_contour_integral i) g"
-
-definition\<^marker>\<open>tag important\<close> contour_integral
-  where "contour_integral g f \<equiv> SOME i. (f has_contour_integral i) g \<or> \<not> f contour_integrable_on g \<and> i=0"
-
-lemma not_integrable_contour_integral: "\<not> f contour_integrable_on g \<Longrightarrow> 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 \<Longrightarrow> 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
-
-lemma has_contour_integral_eqpath:
-     "\<lbrakk>(f has_contour_integral y) p; f contour_integrable_on \<gamma>;
-       contour_integral p f = contour_integral \<gamma> f\<rbrakk>
-      \<Longrightarrow> (f has_contour_integral y) \<gamma>"
-using contour_integrable_on_def contour_integral_unique by auto
-
-lemma has_contour_integral_integral:
-    "f contour_integrable_on i \<Longrightarrow> (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 \<Longrightarrow> (f has_contour_integral j) g \<Longrightarrow> i = j"
-  using has_integral_unique
-  by (auto simp: has_contour_integral_def)
-
-lemma has_contour_integral_integrable: "(f has_contour_integral i) g \<Longrightarrow> f contour_integrable_on g"
-  using contour_integrable_on_def by blast
-
-text\<open>Show that we can forget about the localized derivative.\<close>
-
-lemma has_integral_localized_vector_derivative:
-    "((\<lambda>x. f (g x) * vector_derivative g (at x within {a..b})) has_integral i) {a..b} \<longleftrightarrow>
-     ((\<lambda>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: at_within_interior [of _ "{a..b}"])
-    done
-qed
-
-lemma integrable_on_localized_vector_derivative:
-    "(\<lambda>x. f (g x) * vector_derivative g (at x within {a..b})) integrable_on {a..b} \<longleftrightarrow>
-     (\<lambda>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 \<longleftrightarrow>
-      ((\<lambda>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 \<longleftrightarrow>
-      (\<lambda>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\<^marker>\<open>tag unimportant\<close> \<open>Reversing a path\<close>
-
-lemma valid_path_imp_reverse:
-  assumes "valid_path g"
-    shows "valid_path(reversepath g)"
-proof -
-  obtain S where "finite S" and S: "g C1_differentiable_on ({0..1} - S)"
-    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
-  then have "finite ((-) 1 ` S)"
-    by auto
-  moreover have "(reversepath g C1_differentiable_on ({0..1} - (-) 1 ` S))"
-    unfolding reversepath_def
-    apply (rule C1_differentiable_compose [of "\<lambda>x::real. 1-x" _ g, unfolded o_def])
-    using S
-    by (force simp: finite_vimageI inj_on_def C1_differentiable_on_eq elim!: continuous_on_subset)+
-  ultimately 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) \<longleftrightarrow> valid_path g"
-  using valid_path_imp_reverse by force
-
-lemma has_contour_integral_reversepath:
-  assumes "valid_path g" and f: "(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 \<notin> (-) 1 ` S" "0 \<le> x" "x \<le> 1"
-    have "vector_derivative (\<lambda>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 \<circ> (\<lambda>x. 1 - x) has_vector_derivative -1 *\<^sub>R f') (at x)"
-        by (intro vector_diff_chain_within has_vector_derivative_at_within [OF f'] derivative_eq_intros | simp)+
-      then have mf': "((\<lambda>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
-  obtain S where S: "continuous_on {0..1} g" "finite S" "g C1_differentiable_on {0..1} - S"
-    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
-  have "((\<lambda>x. - (f (g (1 - x)) * vector_derivative g (at (1 - x) within {0..1}))) has_integral -i)
-       {0..1}"
-    using has_integral_affinity01 [where m= "-1" and c=1, OF f [unfolded has_contour_integral_def]]
-    by (simp add: has_integral_neg)
-  then show ?thesis
-    using S
-    apply (clarsimp simp: reversepath_def has_contour_integral_def)
-    apply (rule_tac S = "(\<lambda>x. 1 - x) ` S" in has_integral_spike_finite)
-      apply (auto simp: *)
-    done
-qed
-
-lemma contour_integrable_reversepath:
-    "valid_path g \<Longrightarrow> f contour_integrable_on g \<Longrightarrow> 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 \<Longrightarrow> (f contour_integrable_on (reversepath g) \<longleftrightarrow> 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 "\<not> 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\<^marker>\<open>tag unimportant\<close> \<open>Joining two paths together\<close>
-
-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 \<circ> (\<lambda>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 (force intro: finite_vimageI [where h = "(*)2"] inj_onI)
-    done
-  moreover have "(g2 \<circ> (\<lambda>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 = "(\<lambda>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 \<Rightarrow> 'a::real_normed_field"
-  shows "valid_path (g1 +++ g2) \<Longrightarrow> valid_path g1"
-  unfolding valid_path_def
-  by (rule piecewise_C1_differentiable_D1)
-
-lemma valid_path_join_D2:
-  fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
-  shows "\<lbrakk>valid_path (g1 +++ g2); pathfinish g1 = pathstart g2\<rbrakk> \<Longrightarrow> valid_path g2"
-  unfolding valid_path_def
-  by (rule piecewise_C1_differentiable_D2)
-
-lemma valid_path_join_eq [simp]:
-  fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
-  shows "pathfinish g1 = pathstart g2 \<Longrightarrow> (valid_path(g1 +++ g2) \<longleftrightarrow> valid_path g1 \<and> 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" "\<forall>x\<in>{0..1} - s1. g1 differentiable at x"
-      and s2: "finite s2" "\<forall>x\<in>{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: "((\<lambda>x. f (g1 x) * vector_derivative g1 (at x)) has_integral i1) {0..1}"
-   and 2: "((\<lambda>x. f (g2 x) * vector_derivative g2 (at x)) has_integral i2) {0..1}"
-    using assms
-    by (auto simp: has_contour_integral)
-  have i1: "((\<lambda>x. (2*f (g1 (2*x))) * vector_derivative g1 (at (2*x))) has_integral i1) {0..1/2}"
-   and i2: "((\<lambda>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: "\<lbrakk>0 \<le> z; z*2 < 1; z*2 \<notin> s1\<rbrakk> \<Longrightarrow>
-            vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at z) =
-            2 *\<^sub>R vector_derivative g1 (at (z*2))" for z
-    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g1(2*x))" and d = "\<bar>z - 1/2\<bar>"]])
-    apply (simp_all add: dist_real_def abs_if split: if_split_asm)
-    apply (rule vector_diff_chain_at [of "\<lambda>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: "\<lbrakk>1 < z*2; z \<le> 1; z*2 - 1 \<notin> s2\<rbrakk> \<Longrightarrow>
-            vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at z) =
-            2 *\<^sub>R vector_derivative g2 (at (z*2 - 1))" for z
-    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g2 (2*x - 1))" and d = "\<bar>z - 1/2\<bar>"]])
-    apply (simp_all add: dist_real_def abs_if split: if_split_asm)
-    apply (rule vector_diff_chain_at [of "\<lambda>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 "((\<lambda>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) ((*)2 -` s1)"])
-    using s1
-    apply (force intro: finite_vimageI [where h = "(*)2"] inj_onI)
-    apply (clarsimp simp add: joinpaths_def scaleR_conv_of_real mult_ac g1)
-    done
-  moreover have "((\<lambda>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) ((\<lambda>x. 2*x-1) -` s2)"])
-    using s2
-    apply (force intro: finite_vimageI [where h = "\<lambda>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" "\<forall>x\<in>{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 "(\<lambda>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 *: "(\<lambda>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: "\<lbrakk>0 < z; z < 1; z \<notin> s1\<rbrakk> \<Longrightarrow>
-            vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at (z/2)) =
-            2 *\<^sub>R vector_derivative g1 (at z)"  for z
-    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g1(2*x))" and d = "\<bar>(z-1)/2\<bar>"]])
-    apply (simp_all add: field_simps dist_real_def abs_if split: if_split_asm)
-    apply (rule vector_diff_chain_at [of "\<lambda>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} \<union> 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" "\<forall>x\<in>{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 "(\<lambda>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 *: "(\<lambda>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: "\<lbrakk>0 < z; z < 1; z \<notin> s2\<rbrakk> \<Longrightarrow>
-            vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at (z/2+1/2)) =
-            2 *\<^sub>R vector_derivative g2 (at z)" for z
-    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g2(2*x-1))" and d = "\<bar>z/2\<bar>"]])
-    apply (simp_all add: field_simps dist_real_def abs_if split: if_split_asm)
-    apply (rule vector_diff_chain_at [of "\<lambda>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} \<union> s2", OF _ _ *])
-    apply (auto simp: joinpaths_def scaleR_conv_of_real g2)
-    done
-qed
-
-lemma contour_integrable_join [simp]:
-  shows
-    "\<lbrakk>valid_path g1; valid_path g2\<rbrakk>
-     \<Longrightarrow> f contour_integrable_on (g1 +++ g2) \<longleftrightarrow> f contour_integrable_on g1 \<and> f contour_integrable_on g2"
-using contour_integrable_joinD1 contour_integrable_joinD2 contour_integrable_joinI by blast
-
-lemma contour_integral_join [simp]:
-  shows
-    "\<lbrakk>f contour_integrable_on g1; f contour_integrable_on g2; valid_path g1; valid_path g2\<rbrakk>
-        \<Longrightarrow> 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\<^marker>\<open>tag unimportant\<close> \<open>Shifting the starting point of a (closed) path\<close>
-
-lemma shiftpath_alt_def: "shiftpath a f = (\<lambda>x. if x \<le> 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 \<in> {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 \<in> {0..1}"
-    shows "(f has_contour_integral i) (shiftpath a g)"
-proof -
-  obtain s
-    where s: "finite s" and g: "\<forall>x\<in>{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 *: "((\<lambda>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} (\<lambda>x. f (g x) * vector_derivative g (at x)) +
-                    integral {0..a} (\<lambda>x. f (g x) * vector_derivative g (at x))"
-    apply (rule has_integral_unique)
-    apply (subst add.commute)
-    apply (subst integral_combine)
-    using assms * integral_unique by auto
-  { fix x
-    have "0 \<le> x \<Longrightarrow> x + a < 1 \<Longrightarrow> x \<notin> (\<lambda>x. x - a) ` s \<Longrightarrow>
-         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 = "(\<lambda>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 "\<lambda>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 \<Longrightarrow> x \<le> 1 \<Longrightarrow> x \<notin> (\<lambda>x. x - a + 1) ` s \<Longrightarrow>
-          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 = "(\<lambda>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 "\<lambda>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: "(\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on ({a..1})"
-    using * a   by (fastforce intro: integrable_subinterval_real)
-  have v0a: "(\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on ({0..a})"
-    apply (rule integrable_subinterval_real)
-    using * a by auto
-  have "((\<lambda>x. f (shiftpath a g x) * vector_derivative (shiftpath a g) (at x))
-        has_integral  integral {a..1} (\<lambda>x. f (g x) * vector_derivative g (at x)))  {0..1 - a}"
-    apply (rule has_integral_spike_finite
-             [where S = "{1-a} \<union> (\<lambda>x. x-a) ` s" and f = "\<lambda>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 "((\<lambda>x. f (shiftpath a g x) * vector_derivative (shiftpath a g) (at x))
-        has_integral  integral {0..a} (\<lambda>x. f (g x) * vector_derivative g (at x)))  {1 - a..1}"
-    apply (rule has_integral_spike_finite
-             [where S = "{1-a} \<union> (\<lambda>x. x-a+1) ` s" and f = "\<lambda>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 \<in> {0..1}"
-    shows "(f has_contour_integral i) g"
-proof -
-  obtain s
-    where s: "finite s" and g: "\<forall>x\<in>{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 \<notin> 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
-                        at_within_interior [of _ "{0..1}"] 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} \<union> 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 \<in> {0..1}"
-    shows "(f has_contour_integral i) (shiftpath a g) \<longleftrightarrow> (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 \<in> {0..1}"
-    shows "f contour_integrable_on (shiftpath a g) \<longleftrightarrow> 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 \<in> {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\<^marker>\<open>tag unimportant\<close> \<open>More about straight-line paths\<close>
-
-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 \<in> {0..1} \<Longrightarrow> vector_derivative (linepath a b) (at x within {0..1}) = b - a"
-  apply (rule vector_derivative_within_cbox [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) \<longleftrightarrow>
-         ((\<lambda>x. f(linepath a b x) * (b - a)) has_integral i) {0..1}"
-  by (simp add: has_contour_integral)
-
-lemma linepath_in_path:
-  shows "x \<in> {0..1} \<Longrightarrow> linepath a b x \<in> 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 \<in> convex hull s"
-        and b: "b \<in> convex hull s"
-        and x: "0\<le>x" "x\<le>1"
-       shows "linepath a b x \<in> 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 has_contour_integral_trivial_iff [simp]: "(f has_contour_integral i) (linepath a a) \<longleftrightarrow> i=0"
-  using has_contour_integral_unique by blast
-
-lemma contour_integral_trivial [simp]: "contour_integral (linepath a a) f = 0"
-  using has_contour_integral_trivial contour_integral_unique by blast
-
-lemma differentiable_linepath [intro]: "linepath a b differentiable at x within A"
-  by (auto simp: linepath_def)
-
-lemma bounded_linear_linepath:
-  assumes "bounded_linear f"
-  shows   "f (linepath a b x) = linepath (f a) (f b) x"
-proof -
-  interpret f: bounded_linear f by fact
-  show ?thesis by (simp add: linepath_def f.add f.scale)
-qed
-
-lemma bounded_linear_linepath':
-  assumes "bounded_linear f"
-  shows   "f \<circ> linepath a b = linepath (f a) (f b)"
-  using bounded_linear_linepath[OF assms] by (simp add: fun_eq_iff)
-
-lemma cnj_linepath: "cnj (linepath a b x) = linepath (cnj a) (cnj b) x"
-  by (simp add: linepath_def)
-
-lemma cnj_linepath': "cnj \<circ> linepath a b = linepath (cnj a) (cnj b)"
-  by (simp add: linepath_def fun_eq_iff)
-
-subsection\<open>Relation to subpath construction\<close>
-
-lemma valid_path_subpath:
-  fixes g :: "real \<Rightarrow> 'a :: real_normed_vector"
-  assumes "valid_path g" "u \<in> {0..1}" "v \<in> {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 \<circ> (\<lambda>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: contour_integral_unique)
-
-lemma has_contour_integral_subpath:
-  assumes f: "f contour_integrable_on g" and g: "valid_path g"
-      and uv: "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
-    shows "(f has_contour_integral  integral {u..v} (\<lambda>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: "\<And>x. x \<in> {0..1} - s \<Longrightarrow> 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 *: "((\<lambda>x. f (g ((v - u) * x + u)) * vector_derivative g (at ((v - u) * x + u)))
-            has_integral (1 / (v - u)) * integral {u..v} (\<lambda>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 \<in> {0..1} \<Longrightarrow>
-           x \<notin> (\<lambda>t. (v-u) *\<^sub>R t + u) -` s \<Longrightarrow>
-           vector_derivative (\<lambda>x. g ((v-u) * x + u)) (at x) = (v-u) *\<^sub>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 = "(\<lambda>t. ((v-u) *\<^sub>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 \<in> {0..1}" "v \<in> {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_contour_integral_subpath:
-  assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
-    shows "(((\<lambda>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 \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
-    shows "contour_integral (subpath u v g) f =
-           integral {u..v} (\<lambda>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 \<in> {0..1}" "v \<in> {0..1}" "w \<in> {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 \<in> {0..1}" "v \<in> {0..1}" "w \<in> {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\<noteq>v \<and> v\<noteq>w \<and> u\<noteq>w")
-  case True
-    have *: "subpath v u g = reversepath(subpath u v g) \<and>
-             subpath w u g = reversepath(subpath u w g) \<and>
-             subpath w v g = reversepath(subpath v w g)"
-      by (auto simp: reversepath_subpath)
-    have "u < v \<and> v < w \<or>
-          u < w \<and> w < v \<or>
-          v < u \<and> u < w \<or>
-          v < w \<and> w < u \<or>
-          w < u \<and> u < v \<or>
-          w < v \<and> 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_subpath algebra_simps)
-      done
-next
-  case False
-  then show ?thesis
-    apply (auto)
-    using assms
-    by (metis eq_neg_iff_add_eq_0 contour_integral_reversepath reversepath_subpath valid_path_subpath)
-qed
-
-lemma contour_integral_integral:
-     "contour_integral g f = integral {0..1} (\<lambda>x. f (g x) * vector_derivative g (at x))"
-  by (simp add: contour_integral_def integral_def has_contour_integral contour_integrable_on)
-
-lemma contour_integral_cong:
-  assumes "g = g'" "\<And>x. x \<in> path_image g \<Longrightarrow> f x = f' x"
-  shows   "contour_integral g f = contour_integral g' f'"
-  unfolding contour_integral_integral using assms
-  by (intro integral_cong) (auto simp: path_image_def)
-
-
-text \<open>Contour integral along a segment on the real axis\<close>
-
-lemma has_contour_integral_linepath_Reals_iff:
-  fixes a b :: complex and f :: "complex \<Rightarrow> complex"
-  assumes "a \<in> Reals" "b \<in> Reals" "Re a < Re b"
-  shows   "(f has_contour_integral I) (linepath a b) \<longleftrightarrow>
-             ((\<lambda>x. f (of_real x)) has_integral I) {Re a..Re b}"
-proof -
-  from assms have [simp]: "of_real (Re a) = a" "of_real (Re b) = b"
-    by (simp_all add: complex_eq_iff)
-  from assms have "a \<noteq> b" by auto
-  have "((\<lambda>x. f (of_real x)) has_integral I) (cbox (Re a) (Re b)) \<longleftrightarrow>
-          ((\<lambda>x. f (a + b * of_real x - a * of_real x)) has_integral I /\<^sub>R (Re b - Re a)) {0..1}"
-    by (subst has_integral_affinity_iff [of "Re b - Re a" _ "Re a", symmetric])
-       (insert assms, simp_all add: field_simps scaleR_conv_of_real)
-  also have "(\<lambda>x. f (a + b * of_real x - a * of_real x)) =
-               (\<lambda>x. (f (a + b * of_real x - a * of_real x) * (b - a)) /\<^sub>R (Re b - Re a))"
-    using \<open>a \<noteq> b\<close> by (auto simp: field_simps fun_eq_iff scaleR_conv_of_real)
-  also have "(\<dots> has_integral I /\<^sub>R (Re b - Re a)) {0..1} \<longleftrightarrow> 
-               ((\<lambda>x. f (linepath a b x) * (b - a)) has_integral I) {0..1}" using assms
-    by (subst has_integral_cmul_iff) (auto simp: linepath_def scaleR_conv_of_real algebra_simps)
-  also have "\<dots> \<longleftrightarrow> (f has_contour_integral I) (linepath a b)" unfolding has_contour_integral_def
-    by (intro has_integral_cong) (simp add: vector_derivative_linepath_within)
-  finally show ?thesis by simp
-qed
-
-lemma contour_integrable_linepath_Reals_iff:
-  fixes a b :: complex and f :: "complex \<Rightarrow> complex"
-  assumes "a \<in> Reals" "b \<in> Reals" "Re a < Re b"
-  shows   "(f contour_integrable_on linepath a b) \<longleftrightarrow>
-             (\<lambda>x. f (of_real x)) integrable_on {Re a..Re b}"
-  using has_contour_integral_linepath_Reals_iff[OF assms, of f]
-  by (auto simp: contour_integrable_on_def integrable_on_def)
-
-lemma contour_integral_linepath_Reals_eq:
-  fixes a b :: complex and f :: "complex \<Rightarrow> complex"
-  assumes "a \<in> Reals" "b \<in> Reals" "Re a < Re b"
-  shows   "contour_integral (linepath a b) f = integral {Re a..Re b} (\<lambda>x. f (of_real x))"
-proof (cases "f contour_integrable_on linepath a b")
-  case True
-  thus ?thesis using has_contour_integral_linepath_Reals_iff[OF assms, of f]
-    using has_contour_integral_integral has_contour_integral_unique by blast
-next
-  case False
-  thus ?thesis using contour_integrable_linepath_Reals_iff[OF assms, of f]
-    by (simp add: not_integrable_contour_integral not_integrable_integral)
-qed
-
-
-
-text\<open>Cauchy's theorem where there's a primitive\<close>
-
-lemma contour_integral_primitive_lemma:
-  fixes f :: "complex \<Rightarrow> complex" and g :: "real \<Rightarrow> complex"
-  assumes "a \<le> b"
-      and "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
-      and "g piecewise_differentiable_on {a..b}"  "\<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s"
-    shows "((\<lambda>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" "\<forall>x\<in>{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} (\<lambda>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 \<notin> 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 (\<lambda>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 (*) (f' (g x))) (at (g x) within g ` {a..b})"
-      by (simp add: has_field_derivative_def)
-    have "((\<lambda>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_Icc_at)
-    done
-qed
-
-lemma contour_integral_primitive:
-  assumes "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
-      and "valid_path g" "path_image g \<subseteq> 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 "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
-      and "valid_path g"  "path_image g \<subseteq> s" "pathfinish g = pathstart g"
-    shows "(f' has_contour_integral 0) g"
-  using assms
-  by (metis diff_self contour_integral_primitive)
-
-text\<open>Existence of path integral for continuous function\<close>
-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} ((\<lambda>x. f x * (b - a)) \<circ> 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 = "\<lambda>x. f(linepath a b x)*(b - a)"])
-    apply (auto simp: vector_derivative_linepath_within)
-    done
-qed
-
-lemma has_field_der_id: "((\<lambda>x. x\<^sup>2 / 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) (\<lambda>y. y) = (b^2 - a^2)/2"
-  apply (rule contour_integral_unique)
-  using contour_integral_primitive [of UNIV "\<lambda>x. x^2/2" "\<lambda>x. x" "linepath a b"]
-  apply (auto simp: field_simps has_field_der_id)
-  done
-
-lemma contour_integrable_on_const [iff]: "(\<lambda>x. c) contour_integrable_on (linepath a b)"
-  by (simp add: contour_integrable_continuous_linepath)
-
-lemma contour_integrable_on_id [iff]: "(\<lambda>x. x) contour_integrable_on (linepath a b)"
-  by (simp add: contour_integrable_continuous_linepath)
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Arithmetical combining theorems\<close>
-
-lemma has_contour_integral_neg:
-    "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. -(f x)) has_contour_integral (-i)) g"
-  by (simp add: has_integral_neg has_contour_integral_def)
-
-lemma has_contour_integral_add:
-    "\<lbrakk>(f1 has_contour_integral i1) g; (f2 has_contour_integral i2) g\<rbrakk>
-     \<Longrightarrow> ((\<lambda>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:
-  "\<lbrakk>(f1 has_contour_integral i1) g; (f2 has_contour_integral i2) g\<rbrakk>
-         \<Longrightarrow> ((\<lambda>x. f1 x - f2 x) has_contour_integral (i1 - i2)) g"
-  by (simp add: has_integral_diff has_contour_integral_def algebra_simps)
-
-lemma has_contour_integral_lmul:
-  "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>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 \<Longrightarrow> ((\<lambda>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 \<Longrightarrow> ((\<lambda>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:
-    "\<lbrakk>(f has_contour_integral y) p; \<And>x. x \<in> path_image p \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> (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 \<le> B" "\<And>x. x \<in> closed_segment a b \<Longrightarrow> norm(f x) \<le> B"
-    shows "norm i \<le> B * norm(b - a)"
-proof -
-  { fix x::real
-    assume x: "0 \<le> x" "x \<le> 1"
-  have "norm (f (linepath a b x)) *
-        norm (vector_derivative (linepath a b) (at x within {0..1})) \<le> 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 \<le> (B * norm (b - a)) * content (cbox 0 (1::real))"
-    apply (rule has_integral_bound
-       [of _ "\<lambda>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 \<Rightarrow> real"
-  assumes "(f has_contour_integral i) (linepath a b)"
-          "finite k"
-          "0 \<le> B" "\<And>x::real. x \<in> closed_segment a b - k \<Longrightarrow> norm(f x) \<le> B"
-    shows "norm i \<le> B*norm(b - a)"
-*)
-
-lemma has_contour_integral_const_linepath: "((\<lambda>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: "((\<lambda>x. 0) has_contour_integral 0) g"
-  by (simp add: has_contour_integral_def)
-
-lemma has_contour_integral_is_0:
-    "(\<And>z. z \<in> path_image g \<Longrightarrow> f z = 0) \<Longrightarrow> (f has_contour_integral 0) g"
-  by (rule has_contour_integral_eq [OF has_contour_integral_0]) auto
-
-lemma has_contour_integral_sum:
-    "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a has_contour_integral i a) p\<rbrakk>
-     \<Longrightarrow> ((\<lambda>x. sum (\<lambda>a. f a x) s) has_contour_integral sum i s) p"
-  by (induction s rule: finite_induct) (auto simp: has_contour_integral_0 has_contour_integral_add)
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Operations on path integrals\<close>
-
-lemma contour_integral_const_linepath [simp]: "contour_integral (linepath a b) (\<lambda>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 \<Longrightarrow> contour_integral g (\<lambda>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 \<Longrightarrow> f2 contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>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 \<Longrightarrow> f2 contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>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
-           \<Longrightarrow> contour_integral g (\<lambda>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
-        \<Longrightarrow> contour_integral g (\<lambda>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
-        \<Longrightarrow> contour_integral g (\<lambda>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:
-    "(\<And>x. x \<in> path_image p \<Longrightarrow> f x = g x) \<Longrightarrow> 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:
-    "(\<And>z. z \<in> path_image g \<Longrightarrow> f z = 0) \<Longrightarrow> contour_integral g f = 0"
-  by (simp add: has_contour_integral_is_0 contour_integral_unique)
-
-lemma contour_integral_bound_linepath:
-  shows
-    "\<lbrakk>f contour_integrable_on (linepath a b);
-      0 \<le> B; \<And>x. x \<in> closed_segment a b \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
-     \<Longrightarrow> norm(contour_integral (linepath a b) f) \<le> 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 (\<lambda>x. 0) = 0"
-  by (simp add: contour_integral_unique has_contour_integral_0)
-
-lemma contour_integral_sum:
-    "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a) contour_integrable_on p\<rbrakk>
-     \<Longrightarrow> contour_integral p (\<lambda>x. sum (\<lambda>a. f a x) s) = sum (\<lambda>a. contour_integral p (f a)) s"
-  by (auto simp: contour_integral_unique has_contour_integral_sum has_contour_integral_integral)
-
-lemma contour_integrable_eq:
-    "\<lbrakk>f contour_integrable_on p; \<And>x. x \<in> path_image p \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> g contour_integrable_on p"
-  unfolding contour_integrable_on_def
-  by (metis has_contour_integral_eq)
-
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Arithmetic theorems for path integrability\<close>
-
-lemma contour_integrable_neg:
-    "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. -(f x)) contour_integrable_on g"
-  using has_contour_integral_neg contour_integrable_on_def by blast
-
-lemma contour_integrable_add:
-    "\<lbrakk>f1 contour_integrable_on g; f2 contour_integrable_on g\<rbrakk> \<Longrightarrow> (\<lambda>x. f1 x + f2 x) contour_integrable_on g"
-  using has_contour_integral_add contour_integrable_on_def
-  by fastforce
-
-lemma contour_integrable_diff:
-    "\<lbrakk>f1 contour_integrable_on g; f2 contour_integrable_on g\<rbrakk> \<Longrightarrow> (\<lambda>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 \<Longrightarrow> (\<lambda>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 \<Longrightarrow> (\<lambda>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 \<Longrightarrow> (\<lambda>x. f x / c) contour_integrable_on g"
-  using has_contour_integral_div contour_integrable_on_def
-  by fastforce
-
-lemma contour_integrable_sum:
-    "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a) contour_integrable_on p\<rbrakk>
-     \<Longrightarrow> (\<lambda>x. sum (\<lambda>a. f a x) s) contour_integrable_on p"
-   unfolding contour_integrable_on_def
-   by (metis has_contour_integral_sum)
-
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Reversing a path integral\<close>
-
-lemma has_contour_integral_reverse_linepath:
-    "(f has_contour_integral i) (linepath a b)
-     \<Longrightarrow> (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
-     \<Longrightarrow> 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 \<le> k" "k \<le> 1"
-      and c: "c - a = k *\<^sub>R (b - a)"
-    shows "(f has_contour_integral (i + j)) (linepath a b)"
-proof (cases "k = 0 \<or> k = 1")
-  case True
-  then show ?thesis
-    using assms by auto
-next
-  case False
-  then have k: "0 < k" "k < 1" "complex_of_real k \<noteq> 1"
-    using assms by auto
-  have c': "c = k *\<^sub>R (b - a) + a"
-    by (metis diff_add_cancel c)
-  have bc: "(b - c) = (1 - k) *\<^sub>R (b - a)"
-    by (simp add: algebra_simps c')
-  { assume *: "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R c) * (c - a)) has_integral i) {0..1}"
-    have **: "\<And>x. ((k - x) / k) *\<^sub>R a + (x / k) *\<^sub>R c = (1 - x) *\<^sub>R a + x *\<^sub>R b"
-      using False apply (simp add: c' algebra_simps)
-      apply (simp add: real_vector.scale_left_distrib [symmetric] field_split_simps)
-      done
-    have "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R b) * (b - a)) has_integral i) {0..k}"
-      using k has_integral_affinity01 [OF *, of "inverse k" "0"]
-      apply (simp add: divide_simps mult.commute [of _ "k"] image_affinity_atLeastAtMost ** c)
-      apply (auto dest: has_integral_cmul [where c = "inverse k"])
-      done
-  } note fi = this
-  { assume *: "((\<lambda>x. f ((1 - x) *\<^sub>R c + x *\<^sub>R b) * (b - c)) has_integral j) {0..1}"
-    have **: "\<And>x. (((1 - x) / (1 - k)) *\<^sub>R c + ((x - k) / (1 - k)) *\<^sub>R b) = ((1 - x) *\<^sub>R a + x *\<^sub>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 "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R b) * (b - a)) has_integral j) {k..1}"
-      using k has_integral_affinity01 [OF *, of "inverse(1 - k)" "-(k/(1 - k))"]
-      apply (simp add: divide_simps mult.commute [of _ "1-k"] image_affinity_atLeastAtMost ** bc)
-      apply (auto dest: has_integral_cmul [where k = "(1 - k) *\<^sub>R j" and c = "inverse (1 - k)"])
-      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 \<le> k" "k \<le> 1"
-      and c: "c - a = k *\<^sub>R (b - a)"
-    shows "continuous_on (closed_segment a c) f"
-proof -
-  have c': "c = (1 - k) *\<^sub>R a + k *\<^sub>R b"
-    using c by (simp add: algebra_simps)
-  have "closed_segment a c \<subseteq> closed_segment a b"
-    by (metis c' ends_in_segment(1) in_segment(1) k subset_closed_segment)
-  then show "continuous_on (closed_segment a c) f"
-    by (rule continuous_on_subset [OF f])
-qed
-
-lemma contour_integral_split:
-  assumes f: "continuous_on (closed_segment a b) f"
-      and k: "0 \<le> k" "k \<le> 1"
-      and c: "c - a = k *\<^sub>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) *\<^sub>R a + k *\<^sub>R b"
-    using c by (simp add: algebra_simps)
-  have "closed_segment a c \<subseteq> closed_segment a b"
-    by (metis c' ends_in_segment(1) in_segment(1) k subset_closed_segment)
-  moreover have "closed_segment c b \<subseteq> closed_segment a b"
-    by (metis c' ends_in_segment(2) in_segment(1) k subset_closed_segment)
-  ultimately
-  have *: "continuous_on (closed_segment a c) f" "continuous_on (closed_segment c b) f"
-    by (auto intro: continuous_on_subset [OF f])
-  show ?thesis
-    by (rule contour_integral_unique) (meson "*" c contour_integrable_continuous_linepath has_contour_integral_integral has_contour_integral_split k)
-qed
-
-lemma contour_integral_split_linepath:
-  assumes f: "continuous_on (closed_segment a b) f"
-      and c: "c \<in> 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])
-
-text\<open>The special case of midpoints used in the main quadrisection\<close>
-
-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
-    \<Longrightarrow> 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"])
-  apply (auto simp: midpoint_def algebra_simps scaleR_conv_of_real)
-  done
-
-
-text\<open>A couple of special case lemmas that are useful below\<close>
-
-lemma triangle_linear_has_chain_integral:
-    "((\<lambda>x. m*x + d) has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
-  apply (rule Cauchy_theorem_primitive [of UNIV "\<lambda>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)
-      \<Longrightarrow> 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)
-      \<Longrightarrow> 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\<open>Reversing the order in a double path integral\<close>
-
-text\<open>The condition is stronger than needed but it's often true in typical situations\<close>
-
-lemma fst_im_cbox [simp]: "cbox c d \<noteq> {} \<Longrightarrow> (fst ` cbox (a,c) (b,d)) = cbox a b"
-  by (auto simp: cbox_Pair_eq)
-
-lemma snd_im_cbox [simp]: "cbox a b \<noteq> {} \<Longrightarrow> (snd ` cbox (a,c) (b,d)) = cbox c d"
-  by (auto simp: cbox_Pair_eq)
-
-proposition contour_integral_swap:
-  assumes fcon:  "continuous_on (path_image g \<times> path_image h) (\<lambda>(y1,y2). f y1 y2)"
-      and vp:    "valid_path g" "valid_path h"
-      and gvcon: "continuous_on {0..1} (\<lambda>t. vector_derivative g (at t))"
-      and hvcon: "continuous_on {0..1} (\<lambda>t. vector_derivative h (at t))"
-  shows "contour_integral g (\<lambda>w. contour_integral h (f w)) =
-         contour_integral h (\<lambda>z. contour_integral g (\<lambda>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: "\<And>x. (\<lambda>t. f (g x) (h t)) = (\<lambda>(y1,y2). f y1 y2) \<circ> (\<lambda>t. (g x, h t))"
-    by (rule ext) simp
-  have fgh2: "\<And>x. (\<lambda>t. f (g t) (h x)) = (\<lambda>(y1,y2). f y1 y2) \<circ> (\<lambda>t. (g t, h x))"
-    by (rule ext) simp
-  have fcon_im1: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> continuous_on ((\<lambda>t. (g x, h t)) ` {0..1}) (\<lambda>(x, y). f x y)"
-    by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
-  have fcon_im2: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> continuous_on ((\<lambda>t. (g t, h x)) ` {0..1}) (\<lambda>(x, y). f x y)"
-    by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
-  have "\<And>y. y \<in> {0..1} \<Longrightarrow> continuous_on {0..1} (\<lambda>x. f (g x) (h y))"
-    by (subst fgh2) (rule fcon_im2 gcon continuous_intros | simp)+
-  then have vdg: "\<And>y. y \<in> {0..1} \<Longrightarrow> (\<lambda>x. f (g x) (h y) * vector_derivative g (at x)) integrable_on {0..1}"
-    using continuous_on_mult gvcon integrable_continuous_real by blast
-  have "(\<lambda>z. vector_derivative g (at (fst z))) = (\<lambda>x. vector_derivative g (at x)) \<circ> fst"
-    by auto
-  then have gvcon': "continuous_on (cbox (0, 0) (1, 1::real)) (\<lambda>x. vector_derivative g (at (fst x)))"
-    apply (rule ssubst)
-    apply (rule continuous_intros | simp add: gvcon)+
-    done
-  have "(\<lambda>z. vector_derivative h (at (snd z))) = (\<lambda>x. vector_derivative h (at x)) \<circ> snd"
-    by auto
-  then have hvcon': "continuous_on (cbox (0, 0) (1::real, 1)) (\<lambda>x. vector_derivative h (at (snd x)))"
-    apply (rule ssubst)
-    apply (rule continuous_intros | simp add: hvcon)+
-    done
-  have "(\<lambda>x. f (g (fst x)) (h (snd x))) = (\<lambda>(y1,y2). f y1 y2) \<circ> (\<lambda>w. ((g \<circ> fst) w, (h \<circ> snd) w))"
-    by auto
-  then have fgh: "continuous_on (cbox (0, 0) (1, 1)) (\<lambda>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} (\<lambda>x. contour_integral h (f (g x)) * vector_derivative g (at x)) =
-        integral {0..1} (\<lambda>x. contour_integral h (\<lambda>y. f (g x) y * vector_derivative g (at x)))"
-  proof (rule integral_cong [OF contour_integral_rmul [symmetric]])
-    show "\<And>x. x \<in> {0..1} \<Longrightarrow> f (g x) contour_integrable_on h"
-      unfolding 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
-  qed
-  also have "\<dots> = integral {0..1}
-                     (\<lambda>y. contour_integral g (\<lambda>x. f x (h y) * vector_derivative h (at y)))"
-    unfolding 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 "\<dots> = contour_integral h (\<lambda>z. contour_integral g (\<lambda>w. f w z))"
-    unfolding 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\<^marker>\<open>tag unimportant\<close> \<open>The key quadrisection step\<close>
-
-lemma norm_sum_half:
-  assumes "norm(a + b) \<ge> e"
-    shows "norm a \<ge> e/2 \<or> norm b \<ge> e/2"
-proof -
-  have "e \<le> 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 \<le> norm (a + b + c + d)"
-    shows "e / 4 \<le> norm a \<or> e / 4 \<le> norm b \<or> e / 4 \<le> norm c \<or> e / 4 \<le> norm d"
-proof -
-  have "e \<le> 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 \<le> K" "dist b c \<le> K" "dist c a \<le> K"
-      and e: "e * K^2 \<le>
-              norm (contour_integral(linepath a b) f + contour_integral(linepath b c) f + contour_integral(linepath c a) f)"
-  shows "\<exists>a' b' c'.
-           a' \<in> convex hull {a,b,c} \<and> b' \<in> convex hull {a,b,c} \<and> c' \<in> convex hull {a,b,c} \<and>
-           dist a' b' \<le> K/2  \<and>  dist b' c' \<le> K/2  \<and>  dist c' a' \<le> K/2  \<and>
-           e * (K/2)^2 \<le> norm(contour_integral(linepath a' b') f + contour_integral(linepath b' c') f + contour_integral(linepath c' a') f)"
-         (is "\<exists>x y z. ?\<Phi> x y z")
-proof -
-  note divide_le_eq_numeral1 [simp del]
-  define a' where "a' = midpoint b c"
-  define b' where "b' = midpoint c a"
-  define c' where "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
-    by (rule continuous_on_subset [OF f],
-           metis midpoints_in_convex_hull convex_hull_subset hull_subset insert_subset segment_convex_hull)+
-  let ?pathint = "\<lambda>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')"
-    by (simp add: fcont' contour_integral_reverse_linepath) (simp add: a'_def b'_def c'_def contour_integral_midpoint fabc)
-  have [simp]: "\<And>x y. cmod (x * 2 - y * 2) = cmod (x - y) * 2"
-    by (metis left_diff_distrib mult.commute norm_mult_numeral1)
-  have [simp]: "\<And>x y. cmod (x - y) = cmod (y - x)"
-    by (simp add: norm_minus_commute)
-  consider "e * K\<^sup>2 / 4 \<le> cmod (?pathint a c' + ?pathint c' b' + ?pathint b' a)" |
-           "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' c' + ?pathint c' b + ?pathint b a')" |
-           "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' c + ?pathint c b' + ?pathint b' a')" |
-           "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' b' + ?pathint b' c' + ?pathint c' a')"
-    using assms unfolding * by (blast intro: that dest!: norm_sum_lemma)
-  then show ?thesis
-  proof cases
-    case 1 then have "?\<Phi> a c' b'"
-      using assms
-      apply (clarsimp simp: c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
-      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real field_split_simps)
-      done
-    then show ?thesis by blast
-  next
-    case 2 then  have "?\<Phi> a' c' b"
-      using assms
-      apply (clarsimp simp: a'_def c'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
-      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real field_split_simps)
-      done
-    then show ?thesis by blast
-  next
-    case 3 then have "?\<Phi> a' c b'"
-      using assms
-      apply (clarsimp simp: a'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
-      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real field_split_simps)
-      done
-    then show ?thesis by blast
-  next
-    case 4 then have "?\<Phi> a' b' c'"
-      using assms
-      apply (clarsimp 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 field_split_simps)
-      done
-    then show ?thesis by blast
-  qed
-qed
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Cauchy's theorem for triangles\<close>
-
-lemma triangle_points_closer:
-  fixes a::complex
-  shows "\<lbrakk>x \<in> convex hull {a,b,c};  y \<in> convex hull {a,b,c}\<rbrakk>
-         \<Longrightarrow> norm(x - y) \<le> norm(a - b) \<or>
-             norm(x - y) \<le> norm(b - c) \<or>
-             norm(x - y) \<le> 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 \<in> S"
-      and f: "continuous_on S f"
-      and cd: "f field_differentiable (at x within S)"
-      and e: "0 < e"
-    shows "\<exists>k>0. \<forall>a b c. dist a b \<le> k \<and> dist b c \<le> k \<and> dist c a \<le> k \<and>
-              x \<in> convex hull {a,b,c} \<and> convex hull {a,b,c} \<subseteq> S
-              \<longrightarrow> norm(contour_integral(linepath a b) f + contour_integral(linepath b c) f +
-                       contour_integral(linepath c a) f)
-                  \<le> e*(dist a b + dist b c + dist c a)^2"
-           (is "\<exists>k>0. \<forall>a b c. _ \<longrightarrow> ?normle a b c")
-proof -
-  have le_of_3: "\<And>a x y z. \<lbrakk>0 \<le> x*y; 0 \<le> x*z; 0 \<le> y*z; a \<le> (e*(x + y + z))*x + (e*(x + y + z))*y + (e*(x + y + z))*z\<rbrakk>
-                     \<Longrightarrow> a \<le> e*(x + y + z)^2"
-    by (simp add: algebra_simps power2_eq_square)
-  have disj_le: "\<lbrakk>x \<le> a \<or> x \<le> b \<or> x \<le> c; 0 \<le> a; 0 \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> x \<le> 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} \<subseteq> 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': "\<And>y. \<lbrakk>cmod (y - x) \<le> d; y \<in> S\<rbrakk> \<Longrightarrow> cmod (f y - f x - f' * (y - x)) \<le> e * cmod (y - x)"
-       and le: "cmod (a - b) \<le> d" "cmod (b - c) \<le> d" "cmod (c - a) \<le> d"
-       and xc: "x \<in> convex hull {a, b, c}"
-       and S: "convex hull {a, b, c} \<subseteq> 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) (\<lambda>y. f y - f x - f'*(y - x)) +
-              contour_integral (linepath b c) (\<lambda>y. f y - f x - f'*(y - x)) +
-              contour_integral (linepath c a) (\<lambda>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 \<in> convex hull {a,b,c}"
-      have "cmod (f y - f x - f' * (y - x)) \<le> e*norm(y - x)"
-      proof (rule f')
-        show "cmod (y - x) \<le> d"
-          by (metis triangle_points_closer [OF xc yc] le norm_minus_commute order_trans)
-      qed (use S yc in blast)
-      also have "\<dots> \<le> 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)) \<le> e * (cmod (a - b) + cmod (b - c) + cmod (c - a))" .
-    } note cm_le = this
-    have "?normle a b c"
-      unfolding 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 * unfolding dist_norm
-    apply blast
-    done
-qed
-
-
-text\<open>Hence the most basic theorem for a triangle.\<close>
-
-locale Chain =
-  fixes x0 At Follows
-  assumes At0: "At x0 0"
-      and AtSuc: "\<And>x n. At x n \<Longrightarrow> \<exists>x'. At x' (Suc n) \<and> Follows x' x"
-begin
-  primrec f where
-    "f 0 = x0"
-  | "f (Suc n) = (SOME x. At x (Suc n) \<and> 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: "\<And>x y z n. At x y z n \<Longrightarrow> \<exists>x' y' z'. At x' y' z' (Suc n) \<and> Follows x' y' z' x y z"
-  obtains f g h where
-    "f 0 = x0" "g 0 = y0" "h 0 = z0"
-                      "\<And>n. At (f n) (g n) (h n) n"
-                       "\<And>n. Follows (f(Suc n)) (g(Suc n)) (h(Suc n)) (f n) (g n) (h n)"
-proof -
-  interpret three: Chain "(x0,y0,z0)" "\<lambda>(x,y,z). At x y z" "\<lambda>(x',y',z'). \<lambda>(x,y,z). Follows x' y' z' x y z"
-    apply unfold_locales
-    using At0 AtSuc by auto
-  show ?thesis
-  apply (rule that [of "\<lambda>n. fst (three.f n)"  "\<lambda>n. fst (snd (three.f n))" "\<lambda>n. snd (snd (three.f n))"])
-  using three.At three.Follows
-  apply simp_all
-  apply (simp_all add: split_beta')
-  done
-qed
-
-proposition\<^marker>\<open>tag unimportant\<close> 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 = "\<lambda>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 \<noteq> 0"
-    define K where "K = 1 + max (dist a b) (max (dist b c) (dist c a))"
-    define e where "e = norm y / K^2"
-    have K1: "K \<ge> 1"  by (simp add: K_def max.coboundedI1)
-    then have K: "K > 0" by linarith
-    have [iff]: "dist a b \<le> K" "dist b c \<le> K" "dist c a \<le> K"
-      by (simp_all add: K_def)
-    have e: "e > 0"
-      unfolding e_def using ynz K1 by simp
-    define At where "At x y z n \<longleftrightarrow>
-        convex hull {x,y,z} \<subseteq> convex hull {a,b,c} \<and>
-        dist x y \<le> K/2^n \<and> dist y z \<le> K/2^n \<and> dist z x \<le> K/2^n \<and>
-        norm(?pathint x y + ?pathint y z + ?pathint z x) \<ge> e*(K/2^n)^2"
-      for x y z n
-    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 metis
-      have "\<exists>x' y' z'. At x' y' z' (Suc n) \<and> convex hull {x',y',z'} \<subseteq> convex hull {x,y,z}"
-        using At Cauchy_theorem_quadrisection [OF contf', of "K/2^n" e]
-        apply (simp add: At_def 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: "\<And>n. convex hull {fa n, fb n, fc n} \<subseteq> convex hull {a,b,c}"
-        and dist: "\<And>n. dist (fa n) (fb n) \<le> K/2^n"
-                  "\<And>n. dist (fb n) (fc n) \<le> K/2^n"
-                  "\<And>n. dist (fc n) (fa n) \<le> K/2^n"
-        and no: "\<And>n. norm(?pathint (fa n) (fb n) +
-                           ?pathint (fb n) (fc n) +
-                           ?pathint (fc n) (fa n)) \<ge> e * (K/2^n)^2"
-        and conv_le: "\<And>n. convex hull {fa(Suc n), fb(Suc n), fc(Suc n)} \<subseteq> convex hull {fa n, fb n, fc n}"
-      apply (rule Chain3 [of At, OF At0 AtSuc])
-      apply (auto simp: At_def)
-      done
-    obtain x where x: "\<And>n. x \<in> convex hull {fa n, fb n, fc n}"
-    proof (rule bounded_closed_nest)
-      show "\<And>n. closed (convex hull {fa n, fb n, fc n})"
-        by (simp add: compact_imp_closed finite_imp_compact_convex_hull)
-      show "\<And>m n. m \<le> n \<Longrightarrow> convex hull {fa n, fb n, fc n} \<subseteq> convex hull {fa m, fb m, fc m}"
-        by (erule transitive_stepwise_le) (auto simp: conv_le)
-    qed (fastforce intro: finite_imp_bounded_convex_hull)+
-    then have xin: "x \<in> 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:
-            "\<And>x' y' z'.
-               \<lbrakk>dist x' y' \<le> k; dist y' z' \<le> k; dist z' x' \<le> k;
-                x \<in> convex hull {x',y',z'};
-                convex hull {x',y',z'} \<subseteq> convex hull {a,b,c}\<rbrakk>
-               \<Longrightarrow>
-               cmod (?pathint x' y' + ?pathint y' z' + ?pathint z' x') * 10
-                     \<le> e * (dist x' y' + dist y' z' + dist z' x')\<^sup>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) \<le> k" "dist (fb n) (fc n) \<le> k" "dist (fc n) (fa n) \<le> 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))\<^sup>2
-               \<le> (3 * K / 2 ^ n)\<^sup>2"
-        using dist [of n] e K
-        by (simp add: abs_le_square_iff [symmetric])
-      have less10: "\<And>x y::real. 0 < x \<Longrightarrow> y \<le> 9*x \<Longrightarrow> y < x*10"
-        by linarith
-      have "e * (dist (fa n) (fb n) + dist (fb n) (fc n) + dist (fc n) (fa n))\<^sup>2 \<le> e * (3 * K / 2 ^ n)\<^sup>2"
-        using ynz dle e mult_le_cancel_left_pos by blast
-      also have "\<dots> <
-          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]], 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\<^marker>\<open>tag unimportant\<close> \<open>Version needing function holomorphic in interior only\<close>
-
-lemma Cauchy_theorem_flat_lemma:
-  assumes f: "continuous_on (convex hull {a,b,c}) f"
-      and c: "c - a = k *\<^sub>R (b - a)"
-      and k: "0 \<le> 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 \<le> 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 *\<^sub>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 \<le> 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: "\<And>x. x \<in> convex hull {a,b,c} \<Longrightarrow> norm (f x) \<le> 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: "\<And>y. y \<in> convex hull {a,b,c} \<Longrightarrow> norm y < C"
-    using bounded_pos_less by blast
-  then have diff_2C: "norm(x - y) \<le> 2*C"
-           if x: "x \<in> convex hull {a, b, c}" and y: "y \<in> convex hull {a, b, c}" for x y
-  proof -
-    have "cmod x \<le> C"
-      using x by (meson Cno not_le not_less_iff_gr_or_eq)
-    hence "cmod (x - y) \<le> 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) \<le> 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 \<noteq> 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 \<or> a=b \<or> 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)
-        with False obtain d where "c \<noteq> a" "a \<noteq> b" "b \<noteq> c" "c - b = d *\<^sub>R (a - b)"
-          by (auto simp: collinear_3 collinear_lemma)
-        then have "False"
-          using False Cauchy_theorem_flat [OF contf'] pi_eq_y ynz
-          by (simp add: fabc add_eq_0_iff contour_integral_reverse_linepath)
-      }
-      then obtain d where d: "d \<in> interior (convex hull {a, b, c})"
-        by blast
-      { fix d1
-        assume d1_pos: "0 < d1"
-           and d1: "\<And>x x'. \<lbrakk>x\<in>convex hull {a, b, c}; x'\<in>convex hull {a, b, c}; cmod (x' - x) < d1\<rbrakk>
-                           \<Longrightarrow> cmod (f x' - f x) < cmod y / (24 * C)"
-        define e where "e = min 1 (min (d1/(4*C)) ((norm y / 24 / C) / B))"
-        define shrink where "shrink x = x - e *\<^sub>R (x - d)" for x
-        let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
-        have e: "0 < e" "e \<le> 1" "e \<le> d1 / (4 * C)" "e \<le> cmod y / 24 / C / B"
-          using d1_pos \<open>C>0\<close> \<open>B>0\<close> ynz by (simp_all add: e_def)
-        then have eCB: "24 * e * C * B \<le> cmod y"
-          using \<open>C>0\<close> \<open>B>0\<close>  by (simp add: field_simps)
-        have e_le_d1: "e * (4 * C) \<le> d1"
-          using e \<open>C>0\<close> by (simp add: field_simps)
-        have "shrink a \<in> interior(convex hull {a,b,c})"
-             "shrink b \<in> interior(convex hull {a,b,c})"
-             "shrink c \<in> 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)
-        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: "\<And>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: "\<And>a b d::complex. (b - e *\<^sub>R (b - d)) - (a - e *\<^sub>R (a - d)) - (b - a) = e *\<^sub>R (a - b)"
-          by (simp add: algebra_simps)
-        have "cmod y / (24 * C) \<le> cmod y / cmod (b - a) / 12"
-          using False \<open>C>0\<close> diff_2C [of b a] ynz
-          by (auto simp: field_split_simps hull_inc)
-        have less_C: "\<lbrakk>u \<in> convex hull {a, b, c}; 0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> x * cmod u < C" for x u
-          apply (cases "x=0", simp add: \<open>0<C\<close>)
-          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 \<in> convex hull {a, b, c}" "v \<in> convex hull {a, b, c}" "u\<noteq>v"
-             and fpi_uv: "f contour_integrable_on linepath (shrink u) (shrink v)"
-          have shr_uv: "shrink u \<in> interior(convex hull {a,b,c})"
-                       "shrink v \<in> interior(convex hull {a,b,c})"
-            using d e uv
-            by (auto simp: hull_inc mem_interior_convex_shrink shrink_def)
-          have cmod_fuv: "\<And>x. 0\<le>x \<Longrightarrow> x\<le>1 \<Longrightarrow> cmod (f (linepath (shrink u) (shrink v) x)) \<le> 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))) \<le> cmod y"
-            apply (rule order_trans [OF _ eCB])
-            using e \<open>B>0\<close> diff_2C [of u v] uv
-            by (auto simp: field_simps)
-          { fix x::real   assume x: "0\<le>x" "x\<le>1"
-            have cmod_less_4C: "cmod ((1 - x) *\<^sub>R u - (1 - x) *\<^sub>R d) + cmod (x *\<^sub>R v - x *\<^sub>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) *\<^sub>R (u - d) + x *\<^sub>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"
-              apply (simp only: ll norm_mult scaleR_diff_right)
-              using \<open>e>0\<close> cmod_less_4C apply (force intro: norm_triangle_lt less_le_trans [OF _ e_le_d1])
-              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 "\<dots> \<le> cmod y / cmod (v - u) / 12"
-              using False uv \<open>C>0\<close> diff_2C [of v u] ynz
-              by (auto simp: field_split_simps hull_inc)
-            finally have "cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) \<le> 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 \<le> 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))
-                          \<le> B * (cmod y / 24 / C / B * 2 * C) + 2 * C * (cmod y / 24 / C)"
-              apply (rule add_mono [OF mult_mono])
-              using By_uv e \<open>0 < B\<close> \<open>0 < C\<close> x apply (simp_all add: cmod_fuv cmod_shr cmod_12_le)
-              apply (simp add: field_simps)
-              done
-            also have "\<dots> \<le> cmod y / 6"
-              by simp
-            finally 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))
-                          \<le> cmod y / 6" .
-          } 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 **: "\<And>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)
-                \<le> (B*(norm y /24/C/B)*2*C + (2*C)*(norm y/24/C)) * content (cbox 0 (1::real))"
-            apply (rule has_integral_bound
-                    [of _ "\<lambda>x. f(linepath (shrink u) (shrink v) x) * (shrink v - shrink u) - f(linepath u v x)*(v - u)"
-                        _ 0 1])
-            using ynz \<open>0 < B\<close> \<open>0 < C\<close>
-              apply (simp_all del: le_divide_eq_numeral1)
-            apply (simp add: has_integral_diff has_contour_integral_linepath [symmetric] has_contour_integral_integral
-                fpi_uv f_uv contour_integrable_continuous_linepath)
-            apply (auto simp: ** norm_triangle_le norm_mult cmod_diff_le simp del: le_divide_eq_numeral1)
-            done
-          also have "\<dots> \<le> norm y / 6"
-            by simp
-          finally have "norm (?pathint (shrink u) (shrink v) - ?pathint u v) \<le> norm y / 6" .
-          } note * = this
-          have "norm (?pathint (shrink a) (shrink b) - ?pathint a b) \<le> 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) \<le> 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) \<le> 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))
-                \<le> norm y / 6 + norm y / 6 + norm y / 6"
-            by (metis norm_triangle_le add_mono)
-          also have "\<dots> = 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))
-                \<le> norm y / 2"
-            by (simp add: algebra_simps)
-          then
-          have "norm(?pathint a b + ?pathint b c + ?pathint c a) \<le> 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 \<open>0 < C\<close> 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\<^marker>\<open>tag unimportant\<close> \<open>Version allowing finite number of exceptional points\<close>
-
-proposition\<^marker>\<open>tag unimportant\<close> Cauchy_theorem_triangle_cofinite:
-  assumes "continuous_on (convex hull {a,b,c}) f"
-      and "finite S"
-      and "(\<And>x. x \<in> interior(convex hull {a,b,c}) - S \<Longrightarrow> 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 \<notin> S'"
-      by (meson Set.set_insert all_not_in_conv)
-    then show ?thesis
-    proof (cases "d \<in> convex hull {a,b,c}")
-      case False
-      show "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
-      proof (rule less.hyps)
-        show "\<And>x. x \<in> interior (convex hull {a, b, c}) - S' \<Longrightarrow> f field_differentiable at x"
-        using False d interior_subset by (auto intro!: less.prems)
-    qed (use d less.prems in auto)
-    next
-      case True
-      have *: "convex hull {a, b, d} \<subseteq> 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)"
-      proof (rule less.hyps)
-        show "\<And>x. x \<in> interior (convex hull {a, b, d}) - S' \<Longrightarrow> f field_differentiable at x"
-          using d not_in_interior_convex_hull_3
-          by (clarsimp intro!: less.prems) (metis * insert_absorb insert_subset interior_mono)
-      qed (use d continuous_on_subset [OF  _ *] less.prems in auto)
-      have *: "convex hull {b, c, d} \<subseteq> 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)"
-      proof (rule less.hyps)
-        show "\<And>x. x \<in> interior (convex hull {b, c, d}) - S' \<Longrightarrow> f field_differentiable at x"
-          using d not_in_interior_convex_hull_3
-          by (clarsimp intro!: less.prems) (metis * insert_absorb insert_subset interior_mono)
-      qed (use d continuous_on_subset [OF  _ *] less.prems in auto)
-      have *: "convex hull {c, a, d} \<subseteq> 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)"
-      proof (rule less.hyps)
-        show "\<And>x. x \<in> interior (convex hull {c, a, d}) - S' \<Longrightarrow> f field_differentiable at x"
-          using d not_in_interior_convex_hull_3
-          by (clarsimp intro!: less.prems) (metis * insert_absorb insert_subset interior_mono)
-      qed (use d continuous_on_subset [OF  _ *] less.prems in auto)
-      have "f contour_integrable_on linepath a b"
-        using less.prems abd contour_integrable_joinD1 contour_integrable_on_def by blast
-      moreover have "f contour_integrable_on linepath b c"
-        using less.prems bcd contour_integrable_joinD1 contour_integrable_on_def by blast
-      moreover have "f contour_integrable_on linepath c a"
-        using less.prems cad contour_integrable_joinD1 contour_integrable_on_def by blast
-      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 \<noteq> 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\<^marker>\<open>tag unimportant\<close> \<open>Cauchy's theorem for an open starlike set\<close>
-
-lemma starlike_convex_subset:
-  assumes S: "a \<in> S" "closed_segment b c \<subseteq> S" and subs: "\<And>x. x \<in> S \<Longrightarrow> closed_segment a x \<subseteq> S"
-    shows "convex hull {a,b,c} \<subseteq> 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 \<in> S" "open S"
-      and x: "x \<in> S"
-      and subs: "\<And>y. y \<in> S \<Longrightarrow> closed_segment a y \<subseteq> S"
-      and zer: "\<And>b c. closed_segment b c \<subseteq> S
-                   \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f +
-                       contour_integral (linepath c a) f = 0"
-    shows "((\<lambda>x. contour_integral(linepath a x) f) has_field_derivative f x) (at x)"
-proof -
-  let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
-  { fix e y
-    assume e: "0 < e" and bxe: "ball x e \<subseteq> S" and close: "cmod (y - x) < e"
-    have y: "y \<in> 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 \<subseteq> 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: "\<And>y. cmod (y - x) < d1 \<Longrightarrow> 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 \<subseteq> S" using  \<open>open S\<close> x
-      by (auto simp: open_contains_ball)
-    have dpos: "min d1 d2 > 0" using d1 d2 by simp
-    { fix y
-      assume yx: "y \<noteq> x" and close: "cmod (y - x) < min d1 d2"
-      have y: "y \<in> S"
-        using d2 close  by (force simp: dist_norm norm_minus_commute)
-      have "closed_segment x y \<subseteq> S"
-        using close d2  by (auto simp: dist_norm norm_minus_commute dest!: segment_bound(1))
-      then have fxy: "f contour_integrable_on linepath x y"
-        by (metis 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 "((\<lambda>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)) \<le> e / 2 * cmod (y - x)"
-      proof (rule has_contour_integral_bound_linepath)
-        show "\<And>u. u \<in> closed_segment x y \<Longrightarrow> cmod (f u - f x) \<le> e / 2"
-          by (meson close d1_less le_less_trans less_imp_le min.strict_boundedE segment_bound1)
-      qed (use e in simp)
-      also have "\<dots> < 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 "\<exists>d>0. \<forall>y. y \<noteq> x \<and> cmod (y-x) < d \<longrightarrow> cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
-      using dpos by blast
-  }
-  then have *: "(\<lambda>y. (?pathint x y - f x * (y - x)) /\<^sub>R cmod (y - x)) \<midarrow>x\<rightarrow> 0"
-    by (simp add: Lim_at dist_norm inverse_eq_divide)
-  show ?thesis
-    apply (simp add: has_field_derivative_def has_derivative_at2 bounded_linear_mult_right)
-    apply (rule Lim_transform [OF * tendsto_eventually])
-    using \<open>open S\<close> x apply (force simp: dist_norm open_contains_ball inverse_eq_divide [symmetric] eventually_at)
-    done
-qed
-
-(** Existence of a primitive.*)
-lemma holomorphic_starlike_primitive:
-  fixes f :: "complex \<Rightarrow> complex"
-  assumes contf: "continuous_on S f"
-      and S: "starlike S" and os: "open S"
-      and k: "finite k"
-      and fcd: "\<And>x. x \<in> S - k \<Longrightarrow> f field_differentiable at x"
-    shows "\<exists>g. \<forall>x \<in> S. (g has_field_derivative f x) (at x)"
-proof -
-  obtain a where a: "a\<in>S" and a_cs: "\<And>x. x\<in>S \<Longrightarrow> closed_segment a x \<subseteq> S"
-    using S by (auto simp: starlike_def)
-  { fix x b c
-    assume "x \<in> S" "closed_segment b c \<subseteq> S"
-    then have abcs: "convex hull {a, b, c} \<subseteq> 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])
-    then have "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
-      using abcs interior_subset by (force intro: fcd Cauchy_theorem_triangle_cofinite [OF _ k])
-  } 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:
- "\<lbrakk>open S; starlike S; finite k; continuous_on S f;
-   \<And>x. x \<in> S - k \<Longrightarrow> f field_differentiable at x;
-   valid_path g; path_image g \<subseteq> S; pathfinish g = pathstart g\<rbrakk>
-   \<Longrightarrow> (f has_contour_integral 0)  g"
-  by (metis holomorphic_starlike_primitive Cauchy_theorem_primitive at_within_open)
-
-lemma Cauchy_theorem_starlike_simple:
-  "\<lbrakk>open S; starlike S; f holomorphic_on S; valid_path g; path_image g \<subseteq> S; pathfinish g = pathstart g\<rbrakk>
-   \<Longrightarrow> (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\<open>Cauchy's theorem for a convex set\<close>
-
-text\<open>For a convex set we can avoid assuming openness and boundary analyticity\<close>
-
-lemma triangle_contour_integrals_convex_primitive:
-  assumes contf: "continuous_on S f"
-      and S: "a \<in> S" "convex S"
-      and x: "x \<in> S"
-      and zer: "\<And>b c. \<lbrakk>b \<in> S; c \<in> S\<rbrakk>
-                   \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f +
-                       contour_integral (linepath c a) f = 0"
-    shows "((\<lambda>x. contour_integral(linepath a x) f) has_field_derivative f x) (at x within S)"
-proof -
-  let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
-  { fix y
-    assume y: "y \<in> 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 \<subseteq> 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: "\<And>y. \<lbrakk>y \<in> S; cmod (y - x) < d1\<rbrakk> \<Longrightarrow> 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 \<noteq> x" and close: "cmod (y - x) < d1" and y: "y \<in> 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 "((\<lambda>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)) \<le> e / 2 * cmod (y - x)"
-      proof (rule has_contour_integral_bound_linepath)
-        show "\<And>u. u \<in> closed_segment x y \<Longrightarrow> cmod (f u - f x) \<le> e / 2"
-          by (meson assms(3) close convex_contains_segment d1_less le_less_trans less_imp_le segment_bound1 subset_iff x y)
-      qed (use e in simp)
-      also have "\<dots> < 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 "\<exists>d>0. \<forall>y\<in>S. y \<noteq> x \<and> cmod (y-x) < d \<longrightarrow> cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
-      using d1 by blast
-  }
-  then have *: "((\<lambda>y. (contour_integral (linepath x y) f - f x * (y - x)) /\<^sub>R cmod (y - x)) \<longlongrightarrow> 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 * tendsto_eventually])
-    using linordered_field_no_ub
-    apply (force simp: inverse_eq_divide [symmetric] eventually_at)
-    done
-qed
-
-lemma contour_integral_convex_primitive:
-  assumes "convex S" "continuous_on S f"
-          "\<And>a b c. \<lbrakk>a \<in> S; b \<in> S; c \<in> S\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
-  obtains g where "\<And>x. x \<in> S \<Longrightarrow> (g has_field_derivative f x) (at x within S)"
-proof (cases "S={}")
-  case False
-  with assms that show ?thesis
-    by (blast intro: triangle_contour_integrals_convex_primitive has_chain_integral_chain_integral3)
-qed auto
-
-lemma holomorphic_convex_primitive:
-  fixes f :: "complex \<Rightarrow> complex"
-  assumes "convex S" "finite K" and contf: "continuous_on S f"
-    and fd: "\<And>x. x \<in> interior S - K \<Longrightarrow> f field_differentiable at x"
-  obtains g where "\<And>x. x \<in> S \<Longrightarrow> (g has_field_derivative f x) (at x within S)"
-proof (rule contour_integral_convex_primitive [OF \<open>convex S\<close> contf Cauchy_theorem_triangle_cofinite])
-  have *: "convex hull {a, b, c} \<subseteq> S" if "a \<in> S" "b \<in> S" "c \<in> S" for a b c
-    by (simp add: \<open>convex S\<close> hull_minimal that)
-  show "continuous_on (convex hull {a, b, c}) f" if "a \<in> S" "b \<in> S" "c \<in> S" for a b c
-    by (meson "*" contf continuous_on_subset that)
-  show "f field_differentiable at x" if "a \<in> S" "b \<in> S" "c \<in> S" "x \<in> interior (convex hull {a, b, c}) - K" for a b c x
-    by (metis "*" DiffD1 DiffD2 DiffI fd interior_mono subsetCE that)
-qed (use assms in \<open>force+\<close>)
-
-lemma holomorphic_convex_primitive':
-  fixes f :: "complex \<Rightarrow> complex"
-  assumes "convex S" and "open S" and "f holomorphic_on S"
-  obtains g where "\<And>x. x \<in> S \<Longrightarrow> (g has_field_derivative f x) (at x within S)"
-proof (rule holomorphic_convex_primitive)
-  fix x assume "x \<in> interior S - {}"
-  with assms show "f field_differentiable at x"
-    by (auto intro!: holomorphic_on_imp_differentiable_at simp: interior_open)
-qed (use assms in \<open>auto intro: holomorphic_on_imp_continuous_on\<close>)
-
-corollary\<^marker>\<open>tag unimportant\<close> Cauchy_theorem_convex:
-    "\<lbrakk>continuous_on S f; convex S; finite K;
-      \<And>x. x \<in> interior S - K \<Longrightarrow> f field_differentiable at x;
-      valid_path g; path_image g \<subseteq> S; pathfinish g = pathstart g\<rbrakk>
-     \<Longrightarrow> (f has_contour_integral 0) g"
-  by (metis holomorphic_convex_primitive Cauchy_theorem_primitive)
-
-corollary Cauchy_theorem_convex_simple:
-    "\<lbrakk>f holomorphic_on S; convex S;
-     valid_path g; path_image g \<subseteq> S;
-     pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
-  apply (rule Cauchy_theorem_convex [where K = "{}"])
-  apply (simp_all add: holomorphic_on_imp_continuous_on)
-  using at_within_interior holomorphic_on_def interior_subset by fastforce
-
-text\<open>In particular for a disc\<close>
-corollary\<^marker>\<open>tag unimportant\<close> Cauchy_theorem_disc:
-    "\<lbrakk>finite K; continuous_on (cball a e) f;
-      \<And>x. x \<in> ball a e - K \<Longrightarrow> f field_differentiable at x;
-     valid_path g; path_image g \<subseteq> cball a e;
-     pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
-  by (auto intro: Cauchy_theorem_convex)
-
-corollary\<^marker>\<open>tag unimportant\<close> Cauchy_theorem_disc_simple:
-    "\<lbrakk>f holomorphic_on (ball a e); valid_path g; path_image g \<subseteq> ball a e;
-     pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
-by (simp add: Cauchy_theorem_convex_simple)
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Generalize integrability to local primitives\<close>
-
-lemma contour_integral_local_primitive_lemma:
-  fixes f :: "complex\<Rightarrow>complex"
-  shows
-    "\<lbrakk>g piecewise_differentiable_on {a..b};
-      \<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s);
-      \<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s\<rbrakk>
-     \<Longrightarrow> (\<lambda>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 \<Rightarrow> complex"
-  assumes gpd: "g piecewise_differentiable_on {a..b}"
-      and dh: "\<And>x. x \<in> s
-               \<Longrightarrow> \<exists>d h. 0 < d \<and>
-                         (\<forall>y. norm(y - x) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
-      and gs: "\<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s"
-  shows "(\<lambda>x. f(g x) * vector_derivative g (at x)) integrable_on {a..b}"
-proof -
-  { fix x
-    assume x: "a \<le> x" "x \<le> b"
-    obtain d h where d: "0 < d"
-               and h: "(\<And>y. norm(y - g x) < d \<Longrightarrow> (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: "\<And>x'. x' \<in> {a..b} \<Longrightarrow> \<bar>x' - x\<bar> < e \<Longrightarrow> 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 "\<exists>d>0. \<forall>u v. u \<le> x \<and> x \<le> v \<and> {u..v} \<subseteq> ball x d \<and> (u \<le> v \<longrightarrow> a \<le> u \<and> v \<le> b) \<longrightarrow>
-                          (\<lambda>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 \<Rightarrow> complex"
-  assumes g: "valid_path g" "path_image g \<subseteq> s"
-      and dh: "\<And>x. x \<in> s
-               \<Longrightarrow> \<exists>d h. 0 < d \<and>
-                         (\<forall>y. norm(y - x) < d \<longrightarrow> (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\<open>In particular if a function is holomorphic\<close>
-
-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 \<subseteq> s"
-      and fcd: "\<And>x. x \<in> s - k \<Longrightarrow> f field_differentiable at x"
-    shows "f contour_integrable_on g"
-proof -
-  { fix z
-    assume z: "z \<in> s"
-    obtain d where "d>0" and d: "ball z d \<subseteq> s" using  \<open>open s\<close> 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 "\<forall>y\<in>ball z d. (h has_field_derivative f y) (at y within ball z d)"
-      by (metis holomorphic_convex_primitive [OF convex_ball k contfb fcd] d interior_subset Diff_iff subsetD)
-    then have "\<forall>y\<in>ball z d. (h has_field_derivative f y) (at y within s)"
-      by (metis open_ball at_within_open d os subsetCE)
-    then have "\<exists>h. (\<forall>y. cmod (y - z) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
-      by (force simp: dist_norm norm_minus_commute)
-    then have "\<exists>d h. 0 < d \<and> (\<forall>y. cmod (y - z) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
-      using \<open>0 < d\<close> 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 \<subseteq> 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 \<notin> S \<Longrightarrow> continuous_on S (\<lambda>w. 1 / (w - z))"
-  by (rule continuous_intros | force)+
-
-lemma contour_integrable_inversediff:
-    "\<lbrakk>valid_path g; z \<notin> path_image g\<rbrakk> \<Longrightarrow> (\<lambda>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\<open>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.
-\<close>
-
-text\<open>A technical definition to avoid duplication of similar proofs,
-     for paths joined at the ends versus looping paths\<close>
-definition linked_paths :: "bool \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> (real \<Rightarrow> 'a::topological_space) \<Rightarrow> bool"
-  where "linked_paths atends g h ==
-        (if atends then pathstart h = pathstart g \<and> pathfinish h = pathfinish g
-                   else pathfinish g = pathstart g \<and> pathfinish h = pathstart h)"
-
-text\<open>This formulation covers two cases: \<^term>\<open>g\<close> and \<^term>\<open>h\<close> share their
-      start and end points; \<^term>\<open>g\<close> and \<^term>\<open>h\<close> both loop upon themselves.\<close>
-lemma contour_integral_nearby:
-  assumes os: "open S" and p: "path p" "path_image p \<subseteq> S"
-  shows "\<exists>d. 0 < d \<and>
-            (\<forall>g h. valid_path g \<and> valid_path h \<and>
-                  (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
-                  linked_paths atends g h
-                  \<longrightarrow> path_image g \<subseteq> S \<and> path_image h \<subseteq> S \<and>
-                      (\<forall>f. f holomorphic_on S \<longrightarrow> contour_integral h f = contour_integral g f))"
-proof -
-  have "\<forall>z. \<exists>e. z \<in> path_image p \<longrightarrow> 0 < e \<and> ball z e \<subseteq> S"
-    using open_contains_ball os p(2) by blast
-  then obtain ee where ee: "\<And>z. z \<in> path_image p \<Longrightarrow> 0 < ee z \<and> ball z (ee z) \<subseteq> S"
-    by metis
-  define cover where "cover = (\<lambda>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 \<subseteq> (\<Union>c\<in>path_image p. ball c (ee c / 3))"
-    using ee by auto
-  ultimately have "\<exists>D \<subseteq> cover. finite D \<and> path_image p \<subseteq> \<Union>D"
-    by (simp add: compact_eq_Heine_Borel cover_def)
-  then obtain D where D: "D \<subseteq> cover" "finite D" "path_image p \<subseteq> \<Union>D"
-    by blast
-  then obtain k where k: "k \<subseteq> {0..1}" "finite k" and D_eq: "D = ((\<lambda>z. ball z (ee z / 3)) \<circ> p) ` k"
-    apply (simp add: cover_def path_image_def image_comp)
-    apply (blast dest!: finite_subset_image [OF \<open>finite D\<close>])
-    done
-  then have kne: "k \<noteq> {}"
-    using D by auto
-  have pi: "\<And>i. i \<in> k \<Longrightarrow> p i \<in> path_image p"
-    using k  by (auto simp: path_image_def)
-  then have eepi: "\<And>i. i \<in> k \<Longrightarrow> 0 < ee((p i))"
-    by (metis ee)
-  define e where "e = Min((ee \<circ> p) ` k)"
-  have fin_eep: "finite ((ee \<circ> p) ` k)"
-    using k  by blast
-  have "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: "\<And>x x'. \<bar>x' - x\<bar> < d \<Longrightarrow> x\<in>{0..1} \<Longrightarrow> x'\<in>{0..1} \<Longrightarrow> cmod (p x' - p x) < e/3"
-    unfolding uniformly_continuous_on_def dist_norm real_norm_def
-    by (metis divide_pos_pos \<open>0 < e\<close> zero_less_numeral)
-  then obtain N::nat where N: "N>0" "inverse N < d"
-    using real_arch_inverse [of d]   by auto
-  show ?thesis
-  proof (intro exI conjI allI; clarify?)
-    show "e/3 > 0"
-      using \<open>0 < e\<close> by simp
-    fix g h
-    assume g: "valid_path g" and ghp: "\<forall>t\<in>{0..1}. cmod (g t - p t) < e / 3 \<and>  cmod (h t - p t) < e / 3"
-       and h: "valid_path h"
-       and joins: "linked_paths atends g h"
-    { fix t::real
-      assume t: "0 \<le> t" "t \<le> 1"
-      then obtain u where u: "u \<in> k" and ptu: "p t \<in> ball(p u) (ee(p u) / 3)"
-        using \<open>path_image p \<subseteq> \<Union>D\<close> D_eq by (force simp: path_image_def)
-      then have ele: "e \<le> 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 ghp 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 \<in> ball(p u) (ee(p u))"  "h t \<in> 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 \<in> S" "h t \<in> S" using ee u k
-        by (auto simp: path_image_def ball_def)
-    }
-    then have ghs: "path_image g \<subseteq> S" "path_image h \<subseteq> 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 \<in> path_image p"
-        have "f holomorphic_on ball z (ee z)"
-          using fhols ee z holomorphic_on_subset by blast
-        then have "\<exists>ff. (\<forall>w \<in> 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:
-            "\<And>z w. \<lbrakk>z \<in> path_image p; w \<in> ball z (ee z)\<rbrakk> \<Longrightarrow> (ff z has_field_derivative f w) (at w)"
-        by metis
-      { fix n
-        assume n: "n \<le> 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 \<in> k" and "p (n/N) \<in> ball(p t) (ee(p t) / 3)"
-            using \<open>path_image p \<subseteq> \<Union>D\<close> [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 \<le> ee (p t) / 3"  using fin_eep t
-            by (simp add: e_def)
-          { fix x
-            assume x: "n/N \<le> x" "x \<le> (1 + n)/N"
-            then have nN01: "0 \<le> n/N" "(1 + n)/N \<le> 1"
-              using Suc.prems by auto
-            then have x01: "0 \<le> x" "x \<le> 1"
-              using x by linarith+
-            have "cmod (p t - p x)  < ee (p t) / 3 + e/3"
-            proof (rule norm_diff_triangle_less [OF ptu de])
-              show "\<bar>real n / real N - x\<bar> < d"
-                using x N by (auto simp: field_simps)
-            qed (use x01 Suc.prems in auto)
-            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 ghp x01 by (simp add: norm_minus_commute)
-            also have "\<dots> \<le> 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 ghp x01 by (simp add: norm_minus_commute)
-            also have "\<dots> \<le> 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 "closed_segment (g (real n / real N)) (h (real n / real N)) = path_image (linepath (h (n/N)) (g (n/N)))"
-            by (simp add: closed_segment_commute)
-          also have pi_hgn: "\<dots> \<subseteq> 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"])
-          finally have gh_ns: "closed_segment (g (n/N)) (h (n/N)) \<subseteq> S"
-            using ee pi t by blast
-          have pi_ghn': "path_image (linepath (g ((1 + n) / N)) (h ((1 + n) / N))) \<subseteq> 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)) \<subseteq> S"
-            using \<open>N>0\<close> 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)))
-                 \<subseteq> ball (p t) (ee (p t))"
-            apply (intro subset_path_image_join pi_hgn pi_ghn')
-            using \<open>N>0\<close> 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)
-            using Suc.prems pi_subset_ball apply (simp_all add: valid_path_join 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 *: "\<And>hn he hn' gn gd gn' hgn ghn gh0 ghn'.
-                    \<lbrakk>hn - gn = ghn - gh0;
-                     gd + ghn' + he + hgn = (0::complex);
-                     hn - he = hn'; gn + gd = gn'; hgn = -ghn\<rbrakk> \<Longrightarrow> 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 "\<dots> = 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
-    show "path_image g \<subseteq> S \<and> path_image h \<subseteq> S \<and> (\<forall>f. f holomorphic_on S \<longrightarrow> contour_integral h f = contour_integral g f)"
-      by metis
-  qed
-qed
-
-
-lemma
-  assumes "open S" "path p" "path_image p \<subseteq> S"
-    shows contour_integral_nearby_ends:
-      "\<exists>d. 0 < d \<and>
-              (\<forall>g h. valid_path g \<and> valid_path h \<and>
-                    (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
-                    pathstart h = pathstart g \<and> pathfinish h = pathfinish g
-                    \<longrightarrow> path_image g \<subseteq> S \<and>
-                        path_image h \<subseteq> S \<and>
-                        (\<forall>f. f holomorphic_on S
-                            \<longrightarrow> contour_integral h f = contour_integral g f))"
-    and contour_integral_nearby_loops:
-      "\<exists>d. 0 < d \<and>
-              (\<forall>g h. valid_path g \<and> valid_path h \<and>
-                    (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
-                    pathfinish g = pathstart g \<and> pathfinish h = pathstart h
-                    \<longrightarrow> path_image g \<subseteq> S \<and>
-                        path_image h \<subseteq> S \<and>
-                        (\<forall>f. f holomorphic_on S
-                            \<longrightarrow> 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
-
-lemma C1_differentiable_polynomial_function:
-  fixes p :: "real \<Rightarrow> 'a::euclidean_space"
-  shows "polynomial_function p \<Longrightarrow> 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 \<Rightarrow> 'a::euclidean_space"
-  shows "polynomial_function p \<Longrightarrow> 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 \<Rightarrow> 'a::euclidean_space"
-    shows "z \<noteq> g x \<Longrightarrow> 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 \<subseteq> S"
-  shows "\<exists>L. 0 < L \<and>
-             (\<forall>f B. f holomorphic_on S \<and> (\<forall>z \<in> S. norm(f z) \<le> B)
-               \<longrightarrow> norm(contour_integral g f) \<le> 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 \<subseteq> S"
-      and pi: "\<And>f. f holomorphic_on S \<Longrightarrow> contour_integral g f = contour_integral p f"
-    using contour_integral_nearby_ends [OF S \<open>path g\<close> 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'"
-    "\<And>x. (p has_vector_derivative (p' x)) (at x)"
-    by (blast intro: has_vector_derivative_polynomial_function that)
-  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': "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> norm (p' x) \<le> L"
-    by (force simp: bounded_pos)
-  { fix f B
-    assume f: "f holomorphic_on S" and B: "\<And>z. z\<in>S \<Longrightarrow> cmod (f z) \<le> B"
-    then have "f contour_integrable_on p \<and> valid_path p"
-      using p S
-      by (blast intro: valid_path_polynomial_function contour_integrable_holomorphic_simple holomorphic_on_imp_continuous_on)
-    moreover have "cmod (vector_derivative p (at x)) * cmod (f (p x)) \<le> L * B" if "0 \<le> x" "x \<le> 1" for x
-    proof (rule mult_mono)
-      show "cmod (vector_derivative p (at x)) \<le> L"
-        by (metis nop' p'(2) that vector_derivative_at)
-      show "cmod (f (p x)) \<le> B"
-        by (metis B atLeastAtMost_iff imageI p(2) path_defs(4) subset_eq that)
-    qed (use \<open>L>0\<close> in auto)
-    ultimately have "cmod (contour_integral g f) \<le> L * B"
-      apply (simp only: pi [OF f])
-      apply (simp only: 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
-
-text\<open>We can treat even non-rectifiable paths as having a "length" for bounds on analytic functions in open sets.\<close>
-
-subsection \<open>Winding Numbers\<close>
-
-definition\<^marker>\<open>tag important\<close> winding_number_prop :: "[real \<Rightarrow> complex, complex, real, real \<Rightarrow> complex, complex] \<Rightarrow> bool" where
-  "winding_number_prop \<gamma> z e p n \<equiv>
-      valid_path p \<and> z \<notin> path_image p \<and>
-      pathstart p = pathstart \<gamma> \<and>
-      pathfinish p = pathfinish \<gamma> \<and>
-      (\<forall>t \<in> {0..1}. norm(\<gamma> t - p t) < e) \<and>
-      contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
-
-definition\<^marker>\<open>tag important\<close> winding_number:: "[real \<Rightarrow> complex, complex] \<Rightarrow> complex" where
-  "winding_number \<gamma> z \<equiv> SOME n. \<forall>e > 0. \<exists>p. winding_number_prop \<gamma> z e p n"
-
-
-lemma winding_number:
-  assumes "path \<gamma>" "z \<notin> path_image \<gamma>" "0 < e"
-    shows "\<exists>p. winding_number_prop \<gamma> z e p (winding_number \<gamma> z)"
-proof -
-  have "path_image \<gamma> \<subseteq> UNIV - {z}"
-    using assms by blast
-  then obtain d
-    where d: "d>0"
-      and pi_eq: "\<And>h1 h2. valid_path h1 \<and> valid_path h2 \<and>
-                    (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < d \<and> cmod (h2 t - \<gamma> t) < d) \<and>
-                    pathstart h2 = pathstart h1 \<and> pathfinish h2 = pathfinish h1 \<longrightarrow>
-                      path_image h1 \<subseteq> UNIV - {z} \<and> path_image h2 \<subseteq> UNIV - {z} \<and>
-                      (\<forall>f. f holomorphic_on UNIV - {z} \<longrightarrow> contour_integral h2 f = contour_integral h1 f)"
-    using contour_integral_nearby_ends [of "UNIV - {z}" \<gamma>] assms by (auto simp: open_delete)
-  then obtain h where h: "polynomial_function h \<and> pathstart h = pathstart \<gamma> \<and> pathfinish h = pathfinish \<gamma> \<and>
-                          (\<forall>t \<in> {0..1}. norm(h t - \<gamma> t) < d/2)"
-    using path_approx_polynomial_function [OF \<open>path \<gamma>\<close>, of "d/2"] d by auto
-  define nn where "nn = 1/(2* pi*\<i>) * contour_integral h (\<lambda>w. 1/(w - z))"
-  have "\<exists>n. \<forall>e > 0. \<exists>p. winding_number_prop \<gamma> z e p n"
-    proof (rule_tac x=nn in exI, clarify)
-      fix e::real
-      assume e: "e>0"
-      obtain p where p: "polynomial_function p \<and>
-            pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and> (\<forall>t\<in>{0..1}. cmod (p t - \<gamma> t) < min e (d/2))"
-        using path_approx_polynomial_function [OF \<open>path \<gamma>\<close>, of "min e (d/2)"] d \<open>0<e\<close> by auto
-      have "(\<lambda>w. 1 / (w - z)) holomorphic_on UNIV - {z}"
-        by (auto simp: intro!: holomorphic_intros)
-      then show "\<exists>p. winding_number_prop \<gamma> z e p 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 winding_number_prop_def)
-        done
-    qed
-  then show ?thesis
-    unfolding winding_number_def by (rule someI2_ex) (blast intro: \<open>0<e\<close>)
-qed
-
-lemma winding_number_unique:
-  assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
-      and pi: "\<And>e. e>0 \<Longrightarrow> \<exists>p. winding_number_prop \<gamma> z e p n"
-   shows "winding_number \<gamma> z = n"
-proof -
-  have "path_image \<gamma> \<subseteq> UNIV - {z}"
-    using assms by blast
-  then obtain e
-    where e: "e>0"
-      and pi_eq: "\<And>h1 h2 f. \<lbrakk>valid_path h1; valid_path h2;
-                    (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < e \<and> cmod (h2 t - \<gamma> t) < e);
-                    pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1; f holomorphic_on UNIV - {z}\<rbrakk> \<Longrightarrow>
-                    contour_integral h2 f = contour_integral h1 f"
-    using contour_integral_nearby_ends [of "UNIV - {z}" \<gamma>] assms  by (auto simp: open_delete)
-  obtain p where p: "winding_number_prop \<gamma> z e p n"
-    using pi [OF e] by blast
-  obtain q where q: "winding_number_prop \<gamma> z e q (winding_number \<gamma> z)"
-    using winding_number [OF \<gamma> e] by blast
-  have "2 * complex_of_real pi * \<i> * n = contour_integral p (\<lambda>w. 1 / (w - z))"
-    using p by (auto simp: winding_number_prop_def)
-  also have "\<dots> = contour_integral q (\<lambda>w. 1 / (w - z))"
-  proof (rule pi_eq)
-    show "(\<lambda>w. 1 / (w - z)) holomorphic_on UNIV - {z}"
-      by (auto intro!: holomorphic_intros)
-  qed (use p q in \<open>auto simp: winding_number_prop_def norm_minus_commute\<close>)
-  also have "\<dots> = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z"
-    using q by (auto simp: winding_number_prop_def)
-  finally have "2 * complex_of_real pi * \<i> * n = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z" .
-  then show ?thesis
-    by simp
-qed
-
-(*NB not winding_number_prop here due to the loop in p*)
-lemma winding_number_unique_loop:
-  assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
-      and loop: "pathfinish \<gamma> = pathstart \<gamma>"
-      and pi:
-        "\<And>e. e>0 \<Longrightarrow> \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
-                           pathfinish p = pathstart p \<and>
-                           (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
-                           contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
-   shows "winding_number \<gamma> z = n"
-proof -
-  have "path_image \<gamma> \<subseteq> UNIV - {z}"
-    using assms by blast
-  then obtain e
-    where e: "e>0"
-      and pi_eq: "\<And>h1 h2 f. \<lbrakk>valid_path h1; valid_path h2;
-                    (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < e \<and> cmod (h2 t - \<gamma> t) < e);
-                    pathfinish h1 = pathstart h1; pathfinish h2 = pathstart h2; f holomorphic_on UNIV - {z}\<rbrakk> \<Longrightarrow>
-                    contour_integral h2 f = contour_integral h1 f"
-    using contour_integral_nearby_loops [of "UNIV - {z}" \<gamma>] assms  by (auto simp: open_delete)
-  obtain p where p:
-     "valid_path p \<and> z \<notin> path_image p \<and> pathfinish p = pathstart p \<and>
-      (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
-      contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
-    using pi [OF e] by blast
-  obtain q where q: "winding_number_prop \<gamma> z e q (winding_number \<gamma> z)"
-    using winding_number [OF \<gamma> e] by blast
-  have "2 * complex_of_real pi * \<i> * n = contour_integral p (\<lambda>w. 1 / (w - z))"
-    using p by auto
-  also have "\<dots> = contour_integral q (\<lambda>w. 1 / (w - z))"
-  proof (rule pi_eq)
-    show "(\<lambda>w. 1 / (w - z)) holomorphic_on UNIV - {z}"
-      by (auto intro!: holomorphic_intros)
-  qed (use p q loop in \<open>auto simp: winding_number_prop_def norm_minus_commute\<close>)
-  also have "\<dots> = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z"
-    using q by (auto simp: winding_number_prop_def)
-  finally have "2 * complex_of_real pi * \<i> * n = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z" .
-  then show ?thesis
-    by simp
-qed
-
-proposition winding_number_valid_path:
-  assumes "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
-  shows "winding_number \<gamma> z = 1/(2*pi*\<i>) * contour_integral \<gamma> (\<lambda>w. 1/(w - z))"
-  by (rule winding_number_unique)
-  (use assms in \<open>auto simp: valid_path_imp_path winding_number_prop_def\<close>)
-
-proposition has_contour_integral_winding_number:
-  assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
-    shows "((\<lambda>w. 1/(w - z)) has_contour_integral (2*pi*\<i>*winding_number \<gamma> z)) \<gamma>"
-by (simp add: winding_number_valid_path has_contour_integral_integral contour_integrable_inversediff assms)
-
-lemma winding_number_trivial [simp]: "z \<noteq> a \<Longrightarrow> winding_number(linepath a a) z = 0"
-  by (simp add: winding_number_valid_path)
-
-lemma winding_number_subpath_trivial [simp]: "z \<noteq> g x \<Longrightarrow> winding_number (subpath x x g) z = 0"
-  by (simp add: path_image_subpath winding_number_valid_path)
-
-lemma winding_number_join:
-  assumes \<gamma>1: "path \<gamma>1" "z \<notin> path_image \<gamma>1"
-      and \<gamma>2: "path \<gamma>2" "z \<notin> path_image \<gamma>2"
-      and "pathfinish \<gamma>1 = pathstart \<gamma>2"
-    shows "winding_number(\<gamma>1 +++ \<gamma>2) z = winding_number \<gamma>1 z + winding_number \<gamma>2 z"
-proof (rule winding_number_unique)
-  show "\<exists>p. winding_number_prop (\<gamma>1 +++ \<gamma>2) z e p
-              (winding_number \<gamma>1 z + winding_number \<gamma>2 z)" if "e > 0" for e
-  proof -
-    obtain p1 where "winding_number_prop \<gamma>1 z e p1 (winding_number \<gamma>1 z)"
-      using \<open>0 < e\<close> \<gamma>1 winding_number by blast
-    moreover
-    obtain p2 where "winding_number_prop \<gamma>2 z e p2 (winding_number \<gamma>2 z)"
-      using \<open>0 < e\<close> \<gamma>2 winding_number by blast
-    ultimately
-    have "winding_number_prop (\<gamma>1+++\<gamma>2) z e (p1+++p2) (winding_number \<gamma>1 z + winding_number \<gamma>2 z)"
-      using assms
-      apply (simp add: winding_number_prop_def not_in_path_image_join contour_integrable_inversediff algebra_simps)
-      apply (auto simp: joinpaths_def)
-      done
-    then show ?thesis
-      by blast
-  qed
-qed (use assms in \<open>auto simp: not_in_path_image_join\<close>)
-
-lemma winding_number_reversepath:
-  assumes "path \<gamma>" "z \<notin> path_image \<gamma>"
-    shows "winding_number(reversepath \<gamma>) z = - (winding_number \<gamma> z)"
-proof (rule winding_number_unique)
-  show "\<exists>p. winding_number_prop (reversepath \<gamma>) z e p (- winding_number \<gamma> z)" if "e > 0" for e
-  proof -
-    obtain p where "winding_number_prop \<gamma> z e p (winding_number \<gamma> z)"
-      using \<open>0 < e\<close> assms winding_number by blast
-    then have "winding_number_prop (reversepath \<gamma>) z e (reversepath p) (- winding_number \<gamma> z)"
-      using assms
-      apply (simp add: winding_number_prop_def contour_integral_reversepath contour_integrable_inversediff valid_path_imp_reverse)
-      apply (auto simp: reversepath_def)
-      done
-    then show ?thesis
-      by blast
-  qed
-qed (use assms in auto)
-
-lemma winding_number_shiftpath:
-  assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
-      and "pathfinish \<gamma> = pathstart \<gamma>" "a \<in> {0..1}"
-    shows "winding_number(shiftpath a \<gamma>) z = winding_number \<gamma> z"
-proof (rule winding_number_unique_loop)
-  show "\<exists>p. valid_path p \<and> z \<notin> path_image p \<and> pathfinish p = pathstart p \<and>
-            (\<forall>t\<in>{0..1}. cmod (shiftpath a \<gamma> t - p t) < e) \<and>
-            contour_integral p (\<lambda>w. 1 / (w - z)) =
-            complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
-    if "e > 0" for e
-  proof -
-    obtain p where "winding_number_prop \<gamma> z e p (winding_number \<gamma> z)"
-      using \<open>0 < e\<close> assms winding_number by blast
-    then show ?thesis
-      apply (rule_tac x="shiftpath a p" in exI)
-      using assms that
-      apply (auto simp: winding_number_prop_def path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath contour_integral_shiftpath)
-      apply (simp add: shiftpath_def)
-      done
-  qed
-qed (use assms in \<open>auto simp: path_shiftpath path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath\<close>)
-
-lemma winding_number_split_linepath:
-  assumes "c \<in> closed_segment a b" "z \<notin> 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 \<notin> closed_segment a c" "z \<notin> closed_segment c b"
-    using assms  by (meson convex_contains_segment convex_segment ends_in_segment 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:
-   "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> p t = q t) \<Longrightarrow> winding_number p z = winding_number q z"
-  by (simp add: winding_number_def winding_number_prop_def pathstart_def pathfinish_def)
-
-lemma winding_number_constI:
-  assumes "c\<noteq>z" "\<And>t. \<lbrakk>0\<le>t; t\<le>1\<rbrakk> \<Longrightarrow> g t = c" 
-  shows "winding_number g z = 0"
-proof -
-  have "winding_number g z = winding_number (linepath c c) z"
-    apply (rule winding_number_cong)
-    using assms unfolding linepath_def by auto
-  moreover have "winding_number (linepath c c) z =0"
-    apply (rule winding_number_trivial)
-    using assms by auto
-  ultimately show ?thesis by auto
-qed
-
-lemma winding_number_offset: "winding_number p z = winding_number (\<lambda>w. p w - z) 0"
-  unfolding winding_number_def
-proof (intro ext arg_cong [where f = Eps] arg_cong [where f = All] imp_cong refl, safe)
-  fix n e g
-  assume "0 < e" and g: "winding_number_prop p z e g n"
-  then show "\<exists>r. winding_number_prop (\<lambda>w. p w - z) 0 e r n"
-    by (rule_tac x="\<lambda>t. g t - z" in exI)
-       (force simp: winding_number_prop_def contour_integral_integral valid_path_def path_defs
-                vector_derivative_def has_vector_derivative_diff_const piecewise_C1_differentiable_diff C1_differentiable_imp_piecewise)
-next
-  fix n e g
-  assume "0 < e" and g: "winding_number_prop (\<lambda>w. p w - z) 0 e g n"
-  then show "\<exists>r. winding_number_prop p z e r n"
-    apply (rule_tac x="\<lambda>t. g t + z" in exI)
-    apply (simp add: winding_number_prop_def contour_integral_integral valid_path_def path_defs
-        piecewise_C1_differentiable_add vector_derivative_def has_vector_derivative_add_const C1_differentiable_imp_piecewise)
-    apply (force simp: algebra_simps)
-    done
-qed
-
-subsubsection\<^marker>\<open>tag unimportant\<close> \<open>Some lemmas about negating a path\<close>
-
-lemma valid_path_negatepath: "valid_path \<gamma> \<Longrightarrow> valid_path (uminus \<circ> \<gamma>)"
-   unfolding o_def using piecewise_C1_differentiable_neg valid_path_def by blast
-
-lemma has_contour_integral_negatepath:
-  assumes \<gamma>: "valid_path \<gamma>" and cint: "((\<lambda>z. f (- z)) has_contour_integral - i) \<gamma>"
-  shows "(f has_contour_integral i) (uminus \<circ> \<gamma>)"
-proof -
-  obtain S where cont: "continuous_on {0..1} \<gamma>" and "finite S" and diff: "\<gamma> C1_differentiable_on {0..1} - S"
-    using \<gamma> by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
-  have "((\<lambda>x. - (f (- \<gamma> x) * vector_derivative \<gamma> (at x within {0..1}))) has_integral i) {0..1}"
-    using cint by (auto simp: has_contour_integral_def dest: has_integral_neg)
-  then
-  have "((\<lambda>x. f (- \<gamma> x) * vector_derivative (uminus \<circ> \<gamma>) (at x within {0..1})) has_integral i) {0..1}"
-  proof (rule rev_iffD1 [OF _ has_integral_spike_eq])
-    show "negligible S"
-      by (simp add: \<open>finite S\<close> negligible_finite)
-    show "f (- \<gamma> x) * vector_derivative (uminus \<circ> \<gamma>) (at x within {0..1}) =
-         - (f (- \<gamma> x) * vector_derivative \<gamma> (at x within {0..1}))"
-      if "x \<in> {0..1} - S" for x
-    proof -
-      have "vector_derivative (uminus \<circ> \<gamma>) (at x within cbox 0 1) = - vector_derivative \<gamma> (at x within cbox 0 1)"
-      proof (rule vector_derivative_within_cbox)
-        show "(uminus \<circ> \<gamma> has_vector_derivative - vector_derivative \<gamma> (at x within cbox 0 1)) (at x within cbox 0 1)"
-          using that unfolding o_def
-          by (metis C1_differentiable_on_eq UNIV_I diff differentiable_subset has_vector_derivative_minus subsetI that vector_derivative_works)
-      qed (use that in auto)
-      then show ?thesis
-        by simp
-    qed
-  qed
-  then show ?thesis by (simp add: has_contour_integral_def)
-qed
-
-lemma winding_number_negatepath:
-  assumes \<gamma>: "valid_path \<gamma>" and 0: "0 \<notin> path_image \<gamma>"
-  shows "winding_number(uminus \<circ> \<gamma>) 0 = winding_number \<gamma> 0"
-proof -
-  have "(/) 1 contour_integrable_on \<gamma>"
-    using "0" \<gamma> contour_integrable_inversediff by fastforce
-  then have "((\<lambda>z. 1/z) has_contour_integral contour_integral \<gamma> ((/) 1)) \<gamma>"
-    by (rule has_contour_integral_integral)
-  then have "((\<lambda>z. 1 / - z) has_contour_integral - contour_integral \<gamma> ((/) 1)) \<gamma>"
-    using has_contour_integral_neg by auto
-  then show ?thesis
-    using assms
-    apply (simp add: winding_number_valid_path valid_path_negatepath image_def path_defs)
-    apply (simp add: contour_integral_unique has_contour_integral_negatepath)
-    done
-qed
-
-lemma contour_integrable_negatepath:
-  assumes \<gamma>: "valid_path \<gamma>" and pi: "(\<lambda>z. f (- z)) contour_integrable_on \<gamma>"
-  shows "f contour_integrable_on (uminus \<circ> \<gamma>)"
-  by (metis \<gamma> add.inverse_inverse contour_integrable_on_def has_contour_integral_negatepath pi)
-
-(* A combined theorem deducing several things piecewise.*)
-lemma winding_number_join_pos_combined:
-     "\<lbrakk>valid_path \<gamma>1; z \<notin> path_image \<gamma>1; 0 < Re(winding_number \<gamma>1 z);
-       valid_path \<gamma>2; z \<notin> path_image \<gamma>2; 0 < Re(winding_number \<gamma>2 z); pathfinish \<gamma>1 = pathstart \<gamma>2\<rbrakk>
-      \<Longrightarrow> valid_path(\<gamma>1 +++ \<gamma>2) \<and> z \<notin> path_image(\<gamma>1 +++ \<gamma>2) \<and> 0 < Re(winding_number(\<gamma>1 +++ \<gamma>2) z)"
-  by (simp add: valid_path_join path_image_join winding_number_join valid_path_imp_path)
-
-
-subsubsection\<^marker>\<open>tag unimportant\<close> \<open>Useful sufficient conditions for the winding number to be positive\<close>
-
-lemma Re_winding_number:
-    "\<lbrakk>valid_path \<gamma>; z \<notin> path_image \<gamma>\<rbrakk>
-     \<Longrightarrow> Re(winding_number \<gamma> z) = Im(contour_integral \<gamma> (\<lambda>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 \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
-      and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> 0 \<le> Im (vector_derivative \<gamma> (at x) * cnj(\<gamma> x - z))"
-    shows "0 \<le> Re(winding_number \<gamma> z)"
-proof -
-  have ge0: "0 \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))" if x: "0 < x" "x < 1" for x
-    using ge by (simp add: Complex.Im_divide algebra_simps x)
-  let ?vd = "\<lambda>x. 1 / (\<gamma> x - z) * vector_derivative \<gamma> (at x)"
-  let ?int = "\<lambda>z. contour_integral \<gamma> (\<lambda>w. 1 / (w - z))"
-  have hi: "(?vd has_integral ?int z) (cbox 0 1)"
-    unfolding box_real
-    apply (subst has_contour_integral [symmetric])
-    using \<gamma> by (simp add: contour_integrable_inversediff has_contour_integral_integral)
-  have "0 \<le> Im (?int z)"
-  proof (rule has_integral_component_nonneg [of \<i>, simplified])
-    show "\<And>x. x \<in> cbox 0 1 \<Longrightarrow> 0 \<le> Im (if 0 < x \<and> x < 1 then ?vd x else 0)"
-      by (force simp: ge0)
-    show "((\<lambda>x. if 0 < x \<and> x < 1 then ?vd x else 0) has_integral ?int z) (cbox 0 1)"
-      by (rule has_integral_spike_interior [OF hi]) simp
-  qed
-  then show ?thesis
-    by (simp add: Re_winding_number [OF \<gamma>] field_simps)
-qed
-
-lemma winding_number_pos_lt_lemma:
-  assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
-      and e: "0 < e"
-      and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> e \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
-    shows "0 < Re(winding_number \<gamma> z)"
-proof -
-  let ?vd = "\<lambda>x. 1 / (\<gamma> x - z) * vector_derivative \<gamma> (at x)"
-  let ?int = "\<lambda>z. contour_integral \<gamma> (\<lambda>w. 1 / (w - z))"
-  have hi: "(?vd has_integral ?int z) (cbox 0 1)"
-    unfolding box_real
-    apply (subst has_contour_integral [symmetric])
-    using \<gamma> by (simp add: contour_integrable_inversediff has_contour_integral_integral)
-  have "e \<le> Im (contour_integral \<gamma> (\<lambda>w. 1 / (w - z)))"
-  proof (rule has_integral_component_le [of \<i> "\<lambda>x. \<i>*e" "\<i>*e" "{0..1}", simplified])
-    show "((\<lambda>x. if 0 < x \<and> x < 1 then ?vd x else \<i> * complex_of_real e) has_integral ?int z) {0..1}"
-      by (rule has_integral_spike_interior [OF hi, simplified box_real]) (use e in simp)
-    show "\<And>x. 0 \<le> x \<and> x \<le> 1 \<Longrightarrow>
-              e \<le> Im (if 0 < x \<and> x < 1 then ?vd x else \<i> * complex_of_real e)"
-      by (simp add: ge)
-  qed (use has_integral_const_real [of _ 0 1] in auto)
-  with e show ?thesis
-    by (simp add: Re_winding_number [OF \<gamma>] field_simps)
-qed
-
-lemma winding_number_pos_lt:
-  assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
-      and e: "0 < e"
-      and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> e \<le> Im (vector_derivative \<gamma> (at x) * cnj(\<gamma> x - z))"
-    shows "0 < Re (winding_number \<gamma> z)"
-proof -
-  have bm: "bounded ((\<lambda>w. w - z) ` (path_image \<gamma>))"
-    using bounded_translation [of _ "-z"] \<gamma> by (simp add: bounded_valid_path_image)
-  then obtain B where B: "B > 0" and Bno: "\<And>x. x \<in> (\<lambda>w. w - z) ` (path_image \<gamma>) \<Longrightarrow> norm x \<le> B"
-    using bounded_pos [THEN iffD1, OF bm] by blast
-  { fix x::real  assume x: "0 < x" "x < 1"
-    then have B2: "cmod (\<gamma> x - z)^2 \<le> B^2" using Bno [of "\<gamma> x - z"]
-      by (simp add: path_image_def power2_eq_square mult_mono')
-    with x have "\<gamma> x \<noteq> z" using \<gamma>
-      using path_image_def by fastforce
-    then have "e / B\<^sup>2 \<le> Im (vector_derivative \<gamma> (at x) * cnj (\<gamma> x - z)) / (cmod (\<gamma> x - z))\<^sup>2"
-      using B ge [OF x] B2 e
-      apply (rule_tac y="e / (cmod (\<gamma> x - z))\<^sup>2" in order_trans)
-      apply (auto simp: divide_left_mono divide_right_mono)
-      done
-    then have "e / B\<^sup>2 \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
-      by (simp add: complex_div_cnj [of _ "\<gamma> 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 \<gamma>, of "e/B^2"])
-qed
-
-subsection\<open>The winding number is an integer\<close>
-
-text\<open>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.\<close>
-
-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 \<noteq> z"
-    shows "((\<lambda>x. exp(-f x) * (g x - z)) has_vector_derivative 0) (at x within s)"
-proof -
-  have *: "(exp \<circ> (\<lambda>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)
-    using z
-    apply (auto intro!: derivative_eq_intros * [unfolded o_def] g)
-    done
-qed
-
-lemma winding_number_exp_integral:
-  fixes z::complex
-  assumes \<gamma>: "\<gamma> piecewise_C1_differentiable_on {a..b}"
-      and ab: "a \<le> b"
-      and z: "z \<notin> \<gamma> ` {a..b}"
-    shows "(\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)) integrable_on {a..b}"
-          (is "?thesis1")
-          "exp (- (integral {a..b} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))) * (\<gamma> b - z) = \<gamma> a - z"
-          (is "?thesis2")
-proof -
-  let ?D\<gamma> = "\<lambda>x. vector_derivative \<gamma> (at x)"
-  have [simp]: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> \<gamma> x \<noteq> z"
-    using z by force
-  have cong: "continuous_on {a..b} \<gamma>"
-    using \<gamma> by (simp add: piecewise_C1_differentiable_on_def)
-  obtain k where fink: "finite k" and g_C1_diff: "\<gamma> C1_differentiable_on ({a..b} - k)"
-    using \<gamma> by (force simp: piecewise_C1_differentiable_on_def)
-  have \<circ>: "open ({a<..<b} - k)"
-    using \<open>finite k\<close> by (simp add: finite_imp_closed open_Diff)
-  moreover have "{a<..<b} - k \<subseteq> {a..b} - k"
-    by force
-  ultimately have g_diff_at: "\<And>x. \<lbrakk>x \<notin> k; x \<in> {a<..<b}\<rbrakk> \<Longrightarrow> \<gamma> differentiable at x"
-    by (metis Diff_iff differentiable_on_subset C1_diff_imp_diff [OF g_C1_diff] differentiable_on_def at_within_open)
-  { fix w
-    assume "w \<noteq> z"
-    have "continuous_on (ball w (cmod (w - z))) (\<lambda>w. 1 / (w - z))"
-      by (auto simp: dist_norm intro!: continuous_intros)
-    moreover have "\<And>x. cmod (w - x) < cmod (w - z) \<Longrightarrow> \<exists>f'. ((\<lambda>w. 1 / (w - z)) has_field_derivative f') (at x)"
-      by (auto simp: intro!: derivative_eq_intros)
-    ultimately have "\<exists>h. \<forall>y. norm(y - w) < norm(w - z) \<longrightarrow> (h has_field_derivative 1/(y - z)) (at y)"
-      using holomorphic_convex_primitive [of "ball w (norm(w - z))" "{}" "\<lambda>w. 1/(w - z)"]
-      by (force simp: field_differentiable_def Ball_def dist_norm at_within_open_NO_MATCH norm_minus_commute)
-  }
-  then obtain h where h: "\<And>w y. w \<noteq> z \<Longrightarrow> norm(y - w) < norm(w - z) \<Longrightarrow> (h w has_field_derivative 1/(y - z)) (at y)"
-    by meson
-  have exy: "\<exists>y. ((\<lambda>x. inverse (\<gamma> x - z) * ?D\<gamma> x) has_integral y) {a..b}"
-    unfolding integrable_on_def [symmetric]
-  proof (rule contour_integral_local_primitive_any [OF piecewise_C1_imp_differentiable [OF \<gamma>]])
-    show "\<exists>d h. 0 < d \<and>
-               (\<forall>y. cmod (y - w) < d \<longrightarrow> (h has_field_derivative inverse (y - z))(at y within - {z}))"
-          if "w \<in> - {z}" for w
-      apply (rule_tac x="norm(w - z)" in exI)
-      using that inverse_eq_divide has_field_derivative_at_within h
-      by (metis Compl_insert DiffD2 insertCI right_minus_eq zero_less_norm_iff)
-  qed simp
-  have vg_int: "(\<lambda>x. ?D\<gamma> x / (\<gamma> 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 \<in> {a..b}"
-    have cball: "continuous_on (ball (\<gamma> t) (dist (\<gamma> t) z)) (\<lambda>x. inverse (x - z))"
-        using z by (auto intro!: continuous_intros simp: dist_norm)
-    have icd: "\<And>x. cmod (\<gamma> t - x) < cmod (\<gamma> t - z) \<Longrightarrow> (\<lambda>w. inverse (w - z)) field_differentiable at x"
-      unfolding field_differentiable_def by (force simp: intro!: derivative_eq_intros)
-    obtain h where h: "\<And>x. cmod (\<gamma> t - x) < cmod (\<gamma> t - z) \<Longrightarrow>
-                       (h has_field_derivative inverse (x - z)) (at x within {y. cmod (\<gamma> t - y) < cmod (\<gamma> t - z)})"
-      using holomorphic_convex_primitive [where f = "\<lambda>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 \<notin> k" "a < x" "x < b"
-      then have "x \<in> interior ({a..b} - k)"
-        using open_subset_interior [OF \<circ>] by fastforce
-      then have con: "isCont ?D\<gamma> 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}) (\<lambda>x. ?D\<gamma> x)"
-        by (rule continuous_at_imp_continuous_within)
-      have gdx: "\<gamma> differentiable at x"
-        using x by (simp add: g_diff_at)
-      have "\<And>d. \<lbrakk>x \<notin> k; a < x; x < b;
-          (\<gamma> has_vector_derivative d) (at x); a \<le> t; t \<le> b\<rbrakk>
-         \<Longrightarrow> ((\<lambda>x. integral {a..x}
-                     (\<lambda>x. ?D\<gamma> x /
-                           (\<gamma> x - z))) has_vector_derivative
-              d / (\<gamma> x - z))
-              (at x within {a..b})"
-        apply (rule has_vector_derivative_eq_rhs)
-         apply (rule integral_has_vector_derivative_continuous_at [where S = "{}", simplified])
-        apply (rule con_vd continuous_intros cong vg_int | simp add: continuous_at_imp_continuous_within has_vector_derivative_continuous vector_derivative_at)+
-        done
-      then have "((\<lambda>c. exp (- integral {a..c} (\<lambda>x. ?D\<gamma> x / (\<gamma> x - z))) * (\<gamma> c - z)) has_derivative (\<lambda>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_withinI)
-        apply (simp_all add: has_vector_derivative_def [symmetric])
-        done
-      } note * = this
-    have "exp (- (integral {a..t} (\<lambda>x. ?D\<gamma> x / (\<gamma> x - z)))) * (\<gamma> t - z) =\<gamma> a - z"
-      apply (rule has_derivative_zero_unique_strong_interval [of "{a,b} \<union> k" a b])
-      using t
-      apply (auto intro!: * continuous_intros fink cong indefinite_integral_continuous_1 [OF vg_int]  simp add: ab)+
-      done
-   }
-  with ab show ?thesis2
-    by (simp add: divide_inverse_commute integral_def)
-qed
-
-lemma winding_number_exp_2pi:
-    "\<lbrakk>path p; z \<notin> path_image p\<rbrakk>
-     \<Longrightarrow> pathfinish p - z = exp (2 * pi * \<i> * 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 winding_number_prop_def
-  by (force dest: winding_number_exp_integral(2) [of _ 0 1 z] simp: field_simps contour_integral_integral exp_minus)
-
-lemma integer_winding_number_eq:
-  assumes \<gamma>: "path \<gamma>" and z: "z \<notin> path_image \<gamma>"
-  shows "winding_number \<gamma> z \<in> \<int> \<longleftrightarrow> pathfinish \<gamma> = pathstart \<gamma>"
-proof -
-  obtain p where p: "valid_path p" "z \<notin> path_image p"
-                    "pathstart p = pathstart \<gamma>" "pathfinish p = pathfinish \<gamma>"
-           and eq: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
-    using winding_number [OF assms, of 1] unfolding winding_number_prop_def by auto
-  then have wneq: "winding_number \<gamma> z = winding_number p z"
-      using eq winding_number_valid_path by force
-  have iff: "(winding_number \<gamma> z \<in> \<int>) \<longleftrightarrow> (exp (contour_integral p (\<lambda>w. 1 / (w - z))) = 1)"
-    using eq by (simp add: exp_eq_1 complex_is_Int_iff)
-  have "exp (contour_integral p (\<lambda>w. 1 / (w - z))) = (\<gamma> 1 - z) / (\<gamma> 0 - z)"
-    using p winding_number_exp_integral(2) [of p 0 1 z]
-    apply (simp add: valid_path_def path_defs contour_integral_integral exp_minus field_split_simps)
-    by (metis path_image_def pathstart_def pathstart_in_path_image)
-  then have "winding_number p z \<in> \<int> \<longleftrightarrow> pathfinish p = pathstart p"
-    using p wneq iff by (auto simp: path_defs)
-  then show ?thesis using p eq
-    by (auto simp: winding_number_valid_path)
-qed
-
-theorem integer_winding_number:
-  "\<lbrakk>path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> path_image \<gamma>\<rbrakk> \<Longrightarrow> winding_number \<gamma> z \<in> \<int>"
-by (metis integer_winding_number_eq)
-
-
-text\<open>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. \<close>
-
-lemma winding_number_pos_meets:
-  fixes z::complex
-  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and 1: "Re (winding_number \<gamma> z) \<ge> 1"
-      and w: "w \<noteq> z"
-  shows "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image \<gamma>"
-proof -
-  have [simp]: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> \<gamma> x \<noteq> z"
-    using z by (auto simp: path_image_def)
-  have [simp]: "z \<notin> \<gamma> ` {0..1}"
-    using path_image_def z by auto
-  have gpd: "\<gamma> piecewise_C1_differentiable_on {0..1}"
-    using \<gamma> valid_path_def by blast
-  define r where "r = (w - z) / (\<gamma> 0 - z)"
-  have [simp]: "r \<noteq> 0"
-    using w z by (auto simp: r_def)
-  have cont: "continuous_on {0..1}
-     (\<lambda>x. Im (integral {0..x} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z))))"
-    by (intro continuous_intros indefinite_integral_continuous_1 winding_number_exp_integral [OF gpd]; simp)
-  have "Arg2pi r \<le> 2*pi"
-    by (simp add: Arg2pi less_eq_real_def)
-  also have "\<dots> \<le> Im (integral {0..1} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))"
-    using 1
-    apply (simp add: winding_number_valid_path [OF \<gamma> z] contour_integral_integral)
-    apply (simp add: Complex.Re_divide field_simps power2_eq_square)
-    done
-  finally have "Arg2pi r \<le> Im (integral {0..1} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))" .
-  then have "\<exists>t. t \<in> {0..1} \<and> Im(integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x)/(\<gamma> x - z))) = Arg2pi r"
-    by (simp add: Arg2pi_ge_0 cont IVT')
-  then obtain t where t:     "t \<in> {0..1}"
-                  and eqArg: "Im (integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x)/(\<gamma> x - z))) = Arg2pi r"
-    by blast
-  define i where "i = integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
-  have iArg: "Arg2pi r = Im i"
-    using eqArg by (simp add: i_def)
-  have gpdt: "\<gamma> 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) * (\<gamma> t - z) = \<gamma> 0 - z"
-    unfolding i_def
-    apply (rule winding_number_exp_integral [OF gpdt])
-    using t z unfolding path_image_def by force+
-  then have *: "\<gamma> t - z = exp i * (\<gamma> 0 - z)"
-    by (simp add: exp_minus field_simps)
-  then have "(w - z) = r * (\<gamma> 0 - z)"
-    by (simp add: r_def)
-  then have "z + complex_of_real (exp (Re i)) * (w - z) / complex_of_real (cmod r) = \<gamma> t"
-    apply simp
-    apply (subst Complex_Transcendental.Arg2pi_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 \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "\<bar>Re (winding_number \<gamma> z)\<bar> \<ge> 1"
-      and w: "w \<noteq> z"
-  shows "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image \<gamma>"
-proof -
-  { assume "Re (winding_number \<gamma> z) \<le> - 1"
-    then have "Re (winding_number (reversepath \<gamma>) z) \<ge> 1"
-      by (simp add: \<gamma> valid_path_imp_path winding_number_reversepath z)
-    moreover have "valid_path (reversepath \<gamma>)"
-      using \<gamma> valid_path_imp_reverse by auto
-    moreover have "z \<notin> path_image (reversepath \<gamma>)"
-      by (simp add: z)
-    ultimately have "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image (reversepath \<gamma>)"
-      using winding_number_pos_meets w by blast
-    then have ?thesis
-      by simp
-  }
-  then show ?thesis
-    using assms
-    by (simp add: abs_if winding_number_pos_meets split: if_split_asm)
-qed
-
-lemma winding_number_less_1:
-  fixes z::complex
-  shows
-  "\<lbrakk>valid_path \<gamma>; z \<notin> path_image \<gamma>; w \<noteq> z;
-    \<And>a::real. 0 < a \<Longrightarrow> z + a*(w - z) \<notin> path_image \<gamma>\<rbrakk>
-   \<Longrightarrow> Re(winding_number \<gamma> z) < 1"
-   by (auto simp: not_less dest: winding_number_big_meets)
-
-text\<open>One way of proving that WN=1 for a loop.\<close>
-lemma winding_number_eq_1:
-  fixes z::complex
-  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
-      and 0: "0 < Re(winding_number \<gamma> z)" and 2: "Re(winding_number \<gamma> z) < 2"
-  shows "winding_number \<gamma> z = 1"
-proof -
-  have "winding_number \<gamma> z \<in> Ints"
-    by (simp add: \<gamma> integer_winding_number loop valid_path_imp_path z)
-  then show ?thesis
-    using 0 2 by (auto simp: Ints_def)
-qed
-
-subsection\<open>Continuity of winding number and invariance on connected sets\<close>
-
-lemma continuous_at_winding_number:
-  fixes z::complex
-  assumes \<gamma>: "path \<gamma>" and z: "z \<notin> path_image \<gamma>"
-  shows "continuous (at z) (winding_number \<gamma>)"
-proof -
-  obtain e where "e>0" and cbg: "cball z e \<subseteq> - path_image \<gamma>"
-    using open_contains_cball [of "- path_image \<gamma>"]  z
-    by (force simp: closed_def [symmetric] closed_path_image [OF \<gamma>])
-  then have ppag: "path_image \<gamma> \<subseteq> - 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:
-    "\<And>h1 h2. \<lbrakk>valid_path h1; valid_path h2;
-              (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < d \<and> cmod (h2 t - \<gamma> t) < d);
-              pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1\<rbrakk>
-             \<Longrightarrow>
-               path_image h1 \<subseteq> - cball z (e / 2) \<and>
-               path_image h2 \<subseteq> - cball z (e / 2) \<and>
-               (\<forall>f. f holomorphic_on - cball z (e / 2) \<longrightarrow> contour_integral h2 f = contour_integral h1 f)"
-    using contour_integral_nearby_ends [OF oc \<gamma> ppag] by metis
-  obtain p where p: "valid_path p" "z \<notin> path_image p"
-                    "pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma>"
-              and pg: "\<And>t. t\<in>{0..1} \<Longrightarrow> cmod (\<gamma> t - p t) < min d e / 2"
-              and pi: "contour_integral p (\<lambda>x. 1 / (x - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
-    using winding_number [OF \<gamma> z, of "min d e / 2"] \<open>d>0\<close> \<open>e>0\<close> by (auto simp: winding_number_prop_def)
-  { fix w
-    assume d2: "cmod (w - z) < d/2" and e2: "cmod (w - z) < e/2"
-    then have wnotp: "w \<notin> path_image p"
-      using cbg \<open>d>0\<close> \<open>e>0\<close>
-      apply (simp add: path_image_def cball_def dist_norm, clarify)
-      apply (frule pg)
-      apply (drule_tac c="\<gamma> x" in subsetD)
-      apply (auto simp: less_eq_real_def norm_minus_commute norm_triangle_half_l)
-      done
-    have wnotg: "w \<notin> path_image \<gamma>"
-      using cbg e2 \<open>e>0\<close> by (force simp: dist_norm norm_minus_commute)
-    { fix k::real
-      assume k: "k>0"
-      then obtain q where q: "valid_path q" "w \<notin> path_image q"
-                             "pathstart q = pathstart \<gamma> \<and> pathfinish q = pathfinish \<gamma>"
-                    and qg: "\<And>t. t \<in> {0..1} \<Longrightarrow> cmod (\<gamma> t - q t) < min k (min d e) / 2"
-                    and qi: "contour_integral q (\<lambda>u. 1 / (u - w)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> w"
-        using winding_number [OF \<gamma> wnotg, of "min k (min d e) / 2"] \<open>d>0\<close> \<open>e>0\<close> k
-        by (force simp: min_divide_distrib_right winding_number_prop_def)
-      have "contour_integral p (\<lambda>u. 1 / (u - w)) = contour_integral q (\<lambda>u. 1 / (u - w))"
-        apply (rule pi_eq [OF \<open>valid_path q\<close> \<open>valid_path p\<close>, THEN conjunct2, THEN conjunct2, rule_format])
-        apply (frule pg)
-        apply (frule qg)
-        using p q \<open>d>0\<close> e2
-        apply (auto simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
-        done
-      then have "contour_integral p (\<lambda>x. 1 / (x - w)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> 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 \<gamma> 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 winding_number_prop_def)
-      done
-  } note wnwn = this
-  obtain pe where "pe>0" and cbp: "cball z (3 / 4 * pe) \<subseteq> - 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: "\<And>f B. \<lbrakk>f holomorphic_on - cball z (3 / 4 * pe);
-                      \<forall>z \<in> - cball z (3 / 4 * pe). cmod (f z) \<le> B\<rbrakk> \<Longrightarrow>
-                      cmod (contour_integral p f) \<le> L * B"
-    using contour_integral_bound_exists [of "- cball z (3/4*pe)" p] cbp \<open>valid_path p\<close> by force
-  { fix e::real and w::complex
-    assume e: "0 < e" and w: "cmod (w - z) < pe/4" "cmod (w - z) < e * pe\<^sup>2 / (8 * L)"
-    then have [simp]: "w \<notin> path_image p"
-      using cbp p(2) \<open>0 < pe\<close>
-      by (force simp: dist_norm norm_minus_commute path_image_def cball_def)
-    have [simp]: "contour_integral p (\<lambda>x. 1/(x - w)) - contour_integral p (\<lambda>x. 1/(x - z)) =
-                  contour_integral p (\<lambda>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) \<le> cmod (z - w) + cmod (w - x)"
-        using norm_diff_triangle_le by blast
-      also have "\<dots> < 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 \<open>pe>0\<close> by auto
-      then have pe2: "pe^2 < 4 * cmod (w - x) ^ 2"
-        by (simp add: power_divide)
-      have "8 * L * cmod (w - z) < e * pe\<^sup>2"
-        using w \<open>L>0\<close> by (simp add: field_simps)
-      also have "\<dots> < e * 4 * cmod (w - x) * cmod (w - x)"
-        using pe2 \<open>e>0\<close> by (simp add: power2_eq_square)
-      also have "\<dots> < 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 "\<dots> \<le> 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)) \<le> e"
-        apply (cases "x=z \<or> x=w")
-        using pe \<open>pe>0\<close> w \<open>L>0\<close>
-        apply (force simp: norm_minus_commute)
-        using wx w(2) \<open>L>0\<close> 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 (\<lambda>x. 1 / (x - w) - 1 / (x - z))) \<le> L * (e * pe\<^sup>2 / L / 4 * (inverse (pe / 2))\<^sup>2)"
-      apply (rule L)
-      using \<open>pe>0\<close> w
-      apply (force simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
-      using \<open>pe>0\<close> w \<open>L>0\<close>
-      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 (\<lambda>x. 1 / (x - w)) - contour_integral p (\<lambda>x. 1 / (x - z))) < 2*e"
-      apply simp
-      apply (rule le_less_trans [OF *])
-      using \<open>L>0\<close> 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 \<open>pe>0\<close> \<open>L>0\<close>
-    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: \<open>d>0\<close> \<open>e>0\<close> dist_norm wnwn)
-    done
-qed
-
-corollary continuous_on_winding_number:
-    "path \<gamma> \<Longrightarrow> continuous_on (- path_image \<gamma>) (\<lambda>w. winding_number \<gamma> w)"
-  by (simp add: continuous_at_imp_continuous_on continuous_at_winding_number)
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>The winding number is constant on a connected region\<close>
-
-lemma winding_number_constant:
-  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and cs: "connected S" and sg: "S \<inter> path_image \<gamma> = {}"
-  shows "winding_number \<gamma> constant_on S"
-proof -
-  have *: "1 \<le> cmod (winding_number \<gamma> y - winding_number \<gamma> z)"
-      if ne: "winding_number \<gamma> y \<noteq> winding_number \<gamma> z" and "y \<in> S" "z \<in> S" for y z
-  proof -
-    have "winding_number \<gamma> y \<in> \<int>"  "winding_number \<gamma> z \<in>  \<int>"
-      using that integer_winding_number [OF \<gamma> loop] sg \<open>y \<in> S\<close> by auto
-    with ne show ?thesis
-      by (auto simp: Ints_def simp flip: of_int_diff)
-  qed
-  have cont: "continuous_on S (\<lambda>w. winding_number \<gamma> w)"
-    using continuous_on_winding_number [OF \<gamma>] sg
-    by (meson continuous_on_subset disjoint_eq_subset_Compl)
-  show ?thesis
-    using "*" zero_less_one
-    by (blast intro: continuous_discrete_range_constant [OF cs cont])
-qed
-
-lemma winding_number_eq:
-     "\<lbrakk>path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; w \<in> S; z \<in> S; connected S; S \<inter> path_image \<gamma> = {}\<rbrakk>
-      \<Longrightarrow> winding_number \<gamma> w = winding_number \<gamma> z"
-  using winding_number_constant by (metis constant_on_def)
-
-lemma open_winding_number_levelsets:
-  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
-    shows "open {z. z \<notin> path_image \<gamma> \<and> winding_number \<gamma> z = k}"
-proof -
-  have opn: "open (- path_image \<gamma>)"
-    by (simp add: closed_path_image \<gamma> open_Compl)
-  { fix z assume z: "z \<notin> path_image \<gamma>" and k: "k = winding_number \<gamma> z"
-    obtain e where e: "e>0" "ball z e \<subseteq> - path_image \<gamma>"
-      using open_contains_ball [of "- path_image \<gamma>"] opn z
-      by blast
-    have "\<exists>e>0. \<forall>y. dist y z < e \<longrightarrow> y \<notin> path_image \<gamma> \<and> winding_number \<gamma> y = winding_number \<gamma> 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\<open>Winding number is zero "outside" a curve\<close>
-
-proposition winding_number_zero_in_outside:
-  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and z: "z \<in> outside (path_image \<gamma>)"
-    shows "winding_number \<gamma> z = 0"
-proof -
-  obtain B::real where "0 < B" and B: "path_image \<gamma> \<subseteq> ball 0 B"
-    using bounded_subset_ballD [OF bounded_path_image [OF \<gamma>]] by auto
-  obtain w::complex where w: "w \<notin> ball 0 (B + 1)"
-    by (metis abs_of_nonneg le_less less_irrefl mem_ball_0 norm_of_real)
-  have "- ball 0 (B + 1) \<subseteq> outside (path_image \<gamma>)"
-    apply (rule outside_subset_convex)
-    using B subset_ball by auto
-  then have wout: "w \<in> outside (path_image \<gamma>)"
-    using w by blast
-  moreover have "winding_number \<gamma> constant_on outside (path_image \<gamma>)"
-    using winding_number_constant [OF \<gamma> loop, of "outside(path_image \<gamma>)"] connected_outside
-    by (metis DIM_complex bounded_path_image dual_order.refl \<gamma> outside_no_overlap)
-  ultimately have "winding_number \<gamma> z = winding_number \<gamma> w"
-    by (metis (no_types, hide_lams) constant_on_def z)
-  also have "\<dots> = 0"
-  proof -
-    have wnot: "w \<notin> path_image \<gamma>"  using wout by (simp add: outside_def)
-    { fix e::real assume "0<e"
-      obtain p where p: "polynomial_function p" "pathstart p = pathstart \<gamma>" "pathfinish p = pathfinish \<gamma>"
-                 and pg1: "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> cmod (p t - \<gamma> t) < 1)"
-                 and pge: "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> cmod (p t - \<gamma> t) < e)"
-        using path_approx_polynomial_function [OF \<gamma>, of "min 1 e"] \<open>e>0\<close> by force
-      have pip: "path_image p \<subseteq> 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 \<notin> path_image p"  using w by blast
-      then have "\<exists>p. valid_path p \<and> w \<notin> path_image p \<and>
-                     pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and>
-                     (\<forall>t\<in>{0..1}. cmod (\<gamma> t - p t) < e) \<and> contour_integral p (\<lambda>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 \<gamma>] simp add: winding_number_prop_def wnot)
-  qed
-  finally show ?thesis .
-qed
-
-corollary\<^marker>\<open>tag unimportant\<close> winding_number_zero_const: "a \<noteq> z \<Longrightarrow> winding_number (\<lambda>t. a) z = 0"
-  by (rule winding_number_zero_in_outside)
-     (auto simp: pathfinish_def pathstart_def path_polynomial_function)
-
-corollary\<^marker>\<open>tag unimportant\<close> winding_number_zero_outside:
-    "\<lbrakk>path \<gamma>; convex s; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> s; path_image \<gamma> \<subseteq> s\<rbrakk> \<Longrightarrow> winding_number \<gamma> z = 0"
-  by (meson convex_in_outside outside_mono subsetCE winding_number_zero_in_outside)
-
-lemma winding_number_zero_at_infinity:
-  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
-    shows "\<exists>B. \<forall>z. B \<le> norm z \<longrightarrow> winding_number \<gamma> z = 0"
-proof -
-  obtain B::real where "0 < B" and B: "path_image \<gamma> \<subseteq> ball 0 B"
-    using bounded_subset_ballD [OF bounded_path_image [OF \<gamma>]] by auto
-  then show ?thesis
-    apply (rule_tac x="B+1" in exI, clarify)
-    apply (rule winding_number_zero_outside [OF \<gamma> 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:
-    "\<lbrakk>path \<gamma>; convex s; pathfinish \<gamma> = pathstart \<gamma>; open s; path_image \<gamma> \<subseteq> s\<rbrakk>
-     \<Longrightarrow> \<exists>z. z \<in> s \<and> winding_number \<gamma> z = 0"
-  using outside_compact_in_open [of "path_image \<gamma>" s] path_image_nonempty winding_number_zero_in_outside
-  by (fastforce simp add: compact_path_image)
-
-
-text\<open>If a path winds round a set, it winds rounds its inside.\<close>
-lemma winding_number_around_inside:
-  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
-      and cls: "closed s" and cos: "connected s" and s_disj: "s \<inter> path_image \<gamma> = {}"
-      and z: "z \<in> s" and wn_nz: "winding_number \<gamma> z \<noteq> 0" and w: "w \<in> s \<union> inside s"
-    shows "winding_number \<gamma> w = winding_number \<gamma> z"
-proof -
-  have ssb: "s \<subseteq> inside(path_image \<gamma>)"
-  proof
-    fix x :: complex
-    assume "x \<in> s"
-    hence "x \<notin> path_image \<gamma>"
-      by (meson disjoint_iff_not_equal s_disj)
-    thus "x \<in> inside (path_image \<gamma>)"
-      using \<open>x \<in> s\<close> by (metis (no_types) ComplI UnE cos \<gamma> 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 \<gamma> 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 \<gamma>"] compact_path_image connected_path_image contra_subsetD disjoint_iff_not_equal \<gamma> inside_no_overlap)
- qed
-
-
-text\<open>Bounding a WN by 1/2 for a path and point in opposite halfspaces.\<close>
-lemma winding_number_subpath_continuous:
-  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>"
-    shows "continuous_on {0..1} (\<lambda>x. winding_number(subpath 0 x \<gamma>) z)"
-proof -
-  have *: "integral {0..x} (\<lambda>t. vector_derivative \<gamma> (at t) / (\<gamma> t - z)) / (2 * of_real pi * \<i>) =
-         winding_number (subpath 0 x \<gamma>) z"
-         if x: "0 \<le> x" "x \<le> 1" for x
-  proof -
-    have "integral {0..x} (\<lambda>t. vector_derivative \<gamma> (at t) / (\<gamma> t - z)) / (2 * of_real pi * \<i>) =
-          1 / (2*pi*\<i>) * contour_integral (subpath 0 x \<gamma>) (\<lambda>w. 1/(w - z))"
-      using assms x
-      apply (simp add: contour_integral_subcontour_integral [OF contour_integrable_inversediff])
-      done
-    also have "\<dots> = winding_number (subpath 0 x \<gamma>) 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 = "\<lambda>x. 1 / (2*pi*\<i>) *
-                                 integral {0..x} (\<lambda>t. 1/(\<gamma> t - z) * vector_derivative \<gamma> (at t))"])
-    apply (rule continuous_intros)+
-    apply (rule indefinite_integral_continuous_1)
-    apply (rule contour_integrable_inversediff [OF assms, unfolded contour_integrable_on])
-      using assms
-    apply (simp add: *)
-    done
-qed
-
-lemma winding_number_ivt_pos:
-    assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "0 \<le> w" "w \<le> Re(winding_number \<gamma> z)"
-      shows "\<exists>t \<in> {0..1}. Re(winding_number(subpath 0 t \<gamma>) z) = w"
-  apply (rule ivt_increasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right], simp)
-  apply (rule winding_number_subpath_continuous [OF \<gamma> z])
-  using assms
-  apply (auto simp: path_image_def image_def)
-  done
-
-lemma winding_number_ivt_neg:
-    assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "Re(winding_number \<gamma> z) \<le> w" "w \<le> 0"
-      shows "\<exists>t \<in> {0..1}. Re(winding_number(subpath 0 t \<gamma>) z) = w"
-  apply (rule ivt_decreasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right], simp)
-  apply (rule winding_number_subpath_continuous [OF \<gamma> z])
-  using assms
-  apply (auto simp: path_image_def image_def)
-  done
-
-lemma winding_number_ivt_abs:
-    assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "0 \<le> w" "w \<le> \<bar>Re(winding_number \<gamma> z)\<bar>"
-      shows "\<exists>t \<in> {0..1}. \<bar>Re (winding_number (subpath 0 t \<gamma>) z)\<bar> = w"
-  using assms winding_number_ivt_pos [of \<gamma> z w] winding_number_ivt_neg [of \<gamma> z "-w"]
-  by force
-
-lemma winding_number_lt_half_lemma:
-  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and az: "a \<bullet> z \<le> b" and pag: "path_image \<gamma> \<subseteq> {w. a \<bullet> w > b}"
-    shows "Re(winding_number \<gamma> z) < 1/2"
-proof -
-  { assume "Re(winding_number \<gamma> z) \<ge> 1/2"
-    then obtain t::real where t: "0 \<le> t" "t \<le> 1" and sub12: "Re (winding_number (subpath 0 t \<gamma>) z) = 1/2"
-      using winding_number_ivt_pos [OF \<gamma> z, of "1/2"] by auto
-    have gt: "\<gamma> t - z = - (of_real (exp (- (2 * pi * Im (winding_number (subpath 0 t \<gamma>) z)))) * (\<gamma> 0 - z))"
-      using winding_number_exp_2pi [of "subpath 0 t \<gamma>" z]
-      apply (simp add: t \<gamma> 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 \<bullet> \<gamma> 0"
-    proof -
-      have "\<gamma> 0 \<in> {c. b < a \<bullet> 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 \<bullet> \<gamma> t"
-    proof -
-      have "\<gamma> t \<in> {c. b < a \<bullet> c}"
-        by (metis (no_types) pag atLeastAtMost_iff image_subset_iff path_image_def t)
-      thus ?thesis
-        by blast
-    qed
-    ultimately have "0 < a \<bullet> (\<gamma> 0 - z)" "0 < a \<bullet> (\<gamma> 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 \<gamma>" "a \<bullet> z \<le> b" "path_image \<gamma> \<subseteq> {w. a \<bullet> w > b}"
-    shows "\<bar>Re (winding_number \<gamma> z)\<bar> < 1/2"
-proof -
-  have "z \<notin> path_image \<gamma>" 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 \<gamma> z a b]
-                 winding_number_reversepath valid_path_imp_path inner_minus_left path_image_reversepath)
-    done
-qed
-
-lemma winding_number_le_half:
-  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>"
-      and anz: "a \<noteq> 0" and azb: "a \<bullet> z \<le> b" and pag: "path_image \<gamma> \<subseteq> {w. a \<bullet> w \<ge> b}"
-    shows "\<bar>Re (winding_number \<gamma> z)\<bar> \<le> 1/2"
-proof -
-  { assume wnz_12: "\<bar>Re (winding_number \<gamma> z)\<bar> > 1/2"
-    have "isCont (winding_number \<gamma>) z"
-      by (metis continuous_at_winding_number valid_path_imp_path \<gamma> z)
-    then obtain d where "d>0" and d: "\<And>x'. dist x' z < d \<Longrightarrow> dist (winding_number \<gamma> x') (winding_number \<gamma> z) < \<bar>Re(winding_number \<gamma> z)\<bar> - 1/2"
-      using continuous_at_eps_delta wnz_12 diff_gt_0_iff_gt by blast
-    define z' where "z' = z - (d / (2 * cmod a)) *\<^sub>R a"
-    have *: "a \<bullet> z' \<le> b - d / 3 * cmod a"
-      unfolding z'_def inner_mult_right' divide_inverse
-      apply (simp add: field_split_simps algebra_simps dot_square_norm power2_eq_square anz)
-      apply (metis \<open>0 < d\<close> add_increasing azb less_eq_real_def mult_nonneg_nonneg mult_right_mono norm_ge_zero norm_numeral)
-      done
-    have "cmod (winding_number \<gamma> z' - winding_number \<gamma> z) < \<bar>Re (winding_number \<gamma> z)\<bar> - 1/2"
-      using d [of z'] anz \<open>d>0\<close> by (simp add: dist_norm z'_def)
-    then have "1/2 < \<bar>Re (winding_number \<gamma> z)\<bar> - cmod (winding_number \<gamma> z' - winding_number \<gamma> z)"
-      by simp
-    then have "1/2 < \<bar>Re (winding_number \<gamma> z)\<bar> - \<bar>Re (winding_number \<gamma> z') - Re (winding_number \<gamma> z)\<bar>"
-      using abs_Re_le_cmod [of "winding_number \<gamma> z' - winding_number \<gamma> z"] by simp
-    then have wnz_12': "\<bar>Re (winding_number \<gamma> z')\<bar> > 1/2"
-      by linarith
-    moreover have "\<bar>Re (winding_number \<gamma> z')\<bar> < 1/2"
-      apply (rule winding_number_lt_half [OF \<gamma> *])
-      using azb \<open>d>0\<close> pag
-      apply (auto simp: add_strict_increasing anz field_split_simps dest!: subsetD)
-      done
-    ultimately have False
-      by simp
-  }
-  then show ?thesis by force
-qed
-
-lemma winding_number_lt_half_linepath: "z \<notin> closed_segment a b \<Longrightarrow> \<bar>Re (winding_number (linepath a b) z)\<bar> < 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\<open> Positivity of WN for a linepath.\<close>
-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 \<notin> 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\<open>Cauchy's integral formula, again for a convex enclosing set\<close>
-
-lemma Cauchy_integral_formula_weak:
-    assumes s: "convex s" and "finite k" and conf: "continuous_on s f"
-        and fcd: "(\<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x)"
-        and z: "z \<in> interior s - k" and vpg: "valid_path \<gamma>"
-        and pasz: "path_image \<gamma> \<subseteq> s - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
-      shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
-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 \<gamma> \<subseteq> s" and znotin: "z \<notin> path_image \<gamma>" using pasz by blast+
-  have c: "continuous (at x within s) (\<lambda>w. if w = z then f' else (f w - f z) / (w - z))" if "x \<in> 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" _ "\<lambda>w::complex. (f w - f z)/(w - z)"])
-      using has_field_derivative_at_within has_field_derivative_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) (\<lambda>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 "\<lambda>w::complex. (f w - f z)/(w - z)"])
-      apply (force simp: dist_commute)
-      done
-  qed
-  have fink': "finite (insert z k)" using \<open>finite k\<close> by blast
-  have *: "((\<lambda>w. if w = z then f' else (f w - f z) / (w - z)) has_contour_integral 0) \<gamma>"
-    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 = "\<lambda>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: ac_simps divide_simps)
-    done
-qed
-
-theorem Cauchy_integral_formula_convex_simple:
-    "\<lbrakk>convex s; f holomorphic_on s; z \<in> interior s; valid_path \<gamma>; path_image \<gamma> \<subseteq> s - {z};
-      pathfinish \<gamma> = pathstart \<gamma>\<rbrakk>
-     \<Longrightarrow> ((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
-  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\<open>Homotopy forms of Cauchy's theorem\<close>
-
-lemma 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 \<times> real \<Rightarrow> complex"
-    where contk: "continuous_on ({0..1} \<times> {0..1}) k"
-      and ks: "k ` ({0..1} \<times> {0..1}) \<subseteq> s"
-      and k [simp]: "\<forall>x. k (0, x) = g x" "\<forall>x. k (1, x) = h x"
-      and ksf: "\<forall>t\<in>{0..1}. linked_paths atends g (\<lambda>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} \<times> {0..1}) k"
-    by (blast intro: compact_Times compact_uniformly_continuous [OF contk])
-  { fix t::real assume t: "t \<in> {0..1}"
-    have pak: "path (k \<circ> (\<lambda>u. (t, u)))"
-      unfolding path_def
-      apply (rule continuous_intros continuous_on_subset [OF contk])+
-      using t by force
-    have pik: "path_image (k \<circ> Pair t) \<subseteq> s"
-      using ks t by (auto simp: path_image_def)
-    obtain e where "e>0" and e:
-         "\<And>g h. \<lbrakk>valid_path g; valid_path h;
-                  \<forall>u\<in>{0..1}. cmod (g u - (k \<circ> Pair t) u) < e \<and> cmod (h u - (k \<circ> Pair t) u) < e;
-                  linked_paths atends g h\<rbrakk>
-                 \<Longrightarrow> contour_integral h f = contour_integral g f"
-      using contour_integral_nearby [OF \<open>open s\<close> pak pik, of atends] f by metis
-    obtain d where "d>0" and d:
-        "\<And>x x'. \<lbrakk>x \<in> {0..1} \<times> {0..1}; x' \<in> {0..1} \<times> {0..1}; norm (x'-x) < d\<rbrakk> \<Longrightarrow> norm (k x' - k x) < e/4"
-      by (rule uniformly_continuous_onE [OF ucontk, of "e/4"]) (auto simp: dist_norm \<open>e>0\<close>)
-    { fix t1 t2
-      assume t1: "0 \<le> t1" "t1 \<le> 1" and t2: "0 \<le> t2" "t2 \<le> 1" and ltd: "\<bar>t1 - t\<bar> < d" "\<bar>t2 - t\<bar> < d"
-      have no2: "\<And>g1 k1 kt. \<lbrakk>norm(g1 - k1) < e/4; norm(k1 - kt) < e/4\<rbrakk> \<Longrightarrow> norm(g1 - kt) < e"
-        using \<open>e > 0\<close>
-        apply (rule_tac y = k1 in norm_triangle_half_l)
-        apply (auto simp: norm_minus_commute intro: order_less_trans)
-        done
-      have "\<exists>d>0. \<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
-                          (\<forall>u\<in>{0..1}. cmod (g1 u - k (t1, u)) < d \<and> cmod (g2 u - k (t2, u)) < d) \<and>
-                          linked_paths atends g1 g2 \<longrightarrow>
-                          contour_integral g2 f = contour_integral g1 f"
-        apply (rule_tac x="e/4" in exI)
-        using t t1 t2 ltd \<open>e > 0\<close>
-        apply (auto intro!: e simp: d no2 simp del: less_divide_eq_numeral1)
-        done
-    }
-    then have "\<exists>e. 0 < e \<and>
-              (\<forall>t1 t2. t1 \<in> {0..1} \<and> t2 \<in> {0..1} \<and> \<bar>t1 - t\<bar> < e \<and> \<bar>t2 - t\<bar> < e
-                \<longrightarrow> (\<exists>d. 0 < d \<and>
-                     (\<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
-                       (\<forall>u \<in> {0..1}.
-                          norm(g1 u - k((t1,u))) < d \<and> norm(g2 u - k((t2,u))) < d) \<and>
-                          linked_paths atends g1 g2
-                          \<longrightarrow> contour_integral g2 f = contour_integral g1 f)))"
-      by (rule_tac x=d in exI) (simp add: \<open>d > 0\<close>)
-  }
-  then obtain ee where ee:
-       "\<And>t. t \<in> {0..1} \<Longrightarrow> ee t > 0 \<and>
-          (\<forall>t1 t2. t1 \<in> {0..1} \<longrightarrow> t2 \<in> {0..1} \<longrightarrow> \<bar>t1 - t\<bar> < ee t \<longrightarrow> \<bar>t2 - t\<bar> < ee t
-            \<longrightarrow> (\<exists>d. 0 < d \<and>
-                 (\<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
-                   (\<forall>u \<in> {0..1}.
-                      norm(g1 u - k((t1,u))) < d \<and> norm(g2 u - k((t2,u))) < d) \<and>
-                      linked_paths atends g1 g2
-                      \<longrightarrow> contour_integral g2 f = contour_integral g1 f)))"
-    by metis
-  note ee_rule = ee [THEN conjunct2, rule_format]
-  define C where "C = (\<lambda>t. ball t (ee t / 3)) ` {0..1}"
-  obtain C' where C': "C' \<subseteq> C" "finite C'" and C'01: "{0..1} \<subseteq> \<Union>C'"
-  proof (rule compactE [OF compact_interval])
-    show "{0..1} \<subseteq> \<Union>C"
-      using ee [THEN conjunct1] by (auto simp: C_def dist_norm)
-  qed (use C_def in auto)
-  define kk where "kk = {t \<in> {0..1}. ball t (ee t / 3) \<in> C'}"
-  have kk01: "kk \<subseteq> {0..1}" by (auto simp: kk_def)
-  define e where "e = Min (ee ` kk)"
-  have C'_eq: "C' = (\<lambda>t. ball t (ee t / 3)) ` kk"
-    using C' by (auto simp: kk_def C_def)
-  have ee_pos[simp]: "\<And>t. t \<in> {0..1} \<Longrightarrow> ee t > 0"
-    by (simp add: kk_def ee)
-  moreover have "finite kk"
-    using \<open>finite C'\<close> kk01 by (force simp: C'_eq inj_on_def ball_eq_ball_iff dest: ee_pos finite_imageD)
-  moreover have "kk \<noteq> {}" using \<open>{0..1} \<subseteq> \<Union>C'\<close> 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: "\<And>i. i \<in> kk \<Longrightarrow> e \<le> ee i"
-    using \<open>finite kk\<close> by (simp add: e_def Min_le_iff [of "ee ` kk"])
-  have plus: "\<exists>t \<in> kk. x \<in> ball t (ee t / 3)" if "x \<in> {0..1}" for x
-    using C' subsetD [OF C'01 that]  unfolding C'_eq by blast
-  have [OF order_refl]:
-      "\<exists>d. 0 < d \<and> (\<forall>j. valid_path j \<and> (\<forall>u \<in> {0..1}. norm(j u - k (n/N, u)) < d) \<and> linked_paths atends g j
-                        \<longrightarrow> contour_integral j f = contour_integral g f)"
-       if "n \<le> 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 \<in> {0..1}" "(Suc n)/N \<in> {0..1}"  by auto
-    then obtain t where t: "t \<in> kk" "n/N \<in> ball t (ee t / 3)"
-      using plus [of "n/N"] by blast
-    then have nN_less: "\<bar>n/N - t\<bar> < ee t"
-      by (simp add: dist_norm del: less_divide_eq_numeral1)
-    have n'N_less: "\<bar>real (Suc n) / real N - t\<bar> < ee t"
-      using t N \<open>N > 0\<close> 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 \<in> {0..1}" using \<open>kk \<subseteq> {0..1}\<close> \<open>t \<in> kk\<close> by blast
-    obtain d1 where "d1 > 0" and d1:
-        "\<And>g1 g2. \<lbrakk>valid_path g1; valid_path g2;
-                   \<forall>u\<in>{0..1}. cmod (g1 u - k (n/N, u)) < d1 \<and> cmod (g2 u - k ((Suc n) / N, u)) < d1;
-                   linked_paths atends g1 g2\<rbrakk>
-                   \<Longrightarrow> 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 \<le> N" using Suc.prems by auto
-    with Suc.hyps
-    obtain d2 where "d2 > 0"
-      and d2: "\<And>j. \<lbrakk>valid_path j; \<forall>u\<in>{0..1}. cmod (j u - k (n/N, u)) < d2; linked_paths atends g j\<rbrakk>
-                     \<Longrightarrow> contour_integral j f = contour_integral g f"
-        by auto
-    have "continuous_on {0..1} (k \<circ> (\<lambda>u. (n/N, u)))"
-      apply (rule continuous_intros continuous_on_subset [OF contk])+
-      using N01 by auto
-    then have pkn: "path (\<lambda>u. k (n/N, u))"
-      by (simp add: path_def)
-    have min12: "min d1 d2 > 0" by (simp add: \<open>0 < d1\<close> \<open>0 < d2\<close>)
-    obtain p where "polynomial_function p"
-        and psf: "pathstart p = pathstart (\<lambda>u. k (n/N, u))"
-                 "pathfinish p = pathfinish (\<lambda>u. k (n/N, u))"
-        and pk_le:  "\<And>t. t\<in>{0..1} \<Longrightarrow> 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
-    proof (intro exI; safe)
-      fix j
-      assume "valid_path j" "linked_paths atends g j"
-        and "\<forall>u\<in>{0..1}. cmod (j u - k (real (Suc n) / real N, u)) < min d1 d2"
-      then have "contour_integral j f = contour_integral p f"
-        using pk_le N01(1) ksf by (force intro!: vpp d1 simp add: linked_paths_def psf)
-      also have "... = contour_integral g f"
-        using pk_le by (force intro!: vpp d2 lpa)
-      finally show "contour_integral j f = contour_integral g f" .
-    qed (simp add: \<open>0 < d1\<close> \<open>0 < d2\<close>)
-  qed
-  then obtain d where "0 < d"
-                       "\<And>j. valid_path j \<and> (\<forall>u \<in> {0..1}. norm(j u - k (1,u)) < d) \<and> linked_paths atends g j
-                            \<Longrightarrow> contour_integral j f = contour_integral g f"
-    using \<open>N>0\<close> by auto
-  then have "linked_paths atends g h \<Longrightarrow> contour_integral h f = contour_integral g f"
-    using \<open>N>0\<close> 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:
-    "\<lbrakk>(f has_contour_integral y) h; f contour_integrable_on g; contour_integral g f = contour_integral h f\<rbrakk>
-     \<Longrightarrow> (f has_contour_integral y) g"
-  using has_contour_integral_integral contour_integral_unique by auto
-
-lemma Cauchy_theorem_null_homotopic:
-     "\<lbrakk>f holomorphic_on s; open s; valid_path g; homotopic_loops s g (linepath a a)\<rbrakk> \<Longrightarrow> (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\<^marker>\<open>tag unimportant\<close> \<open>More winding number properties\<close>
-
-text\<open>including the fact that it's +-1 inside a simple closed curve.\<close>
-
-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 \<notin> path_image g" and pah: "z \<notin> path_image h"
-    using homotopic_paths_imp_subset [OF assms] by auto
-  ultimately obtain d e where "d > 0" "e > 0"
-      and d: "\<And>p. \<lbrakk>path p; pathstart p = pathstart g; pathfinish p = pathfinish g; \<forall>t\<in>{0..1}. norm (p t - g t) < d\<rbrakk>
-            \<Longrightarrow> homotopic_paths (-{z}) g p"
-      and e: "\<And>q. \<lbrakk>path q; pathstart q = pathstart h; pathfinish q = pathfinish h; \<forall>t\<in>{0..1}. norm (q t - h t) < e\<rbrakk>
-            \<Longrightarrow> 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 \<notin> path_image p"
-       "pathstart p = pathstart g" "pathfinish p = pathfinish g"
-       and gp_less:"\<forall>t\<in>{0..1}. cmod (g t - p t) < d"
-       and pap: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number g z"
-    using winding_number [OF \<open>path g\<close> pag \<open>0 < d\<close>] unfolding winding_number_prop_def by blast
-  obtain q where q:
-       "valid_path q" "z \<notin> path_image q"
-       "pathstart q = pathstart h" "pathfinish q = pathfinish h"
-       and hq_less: "\<forall>t\<in>{0..1}. cmod (h t - q t) < e"
-       and paq:  "contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number h z"
-    using winding_number [OF \<open>path h\<close> pah \<open>0 < e\<close>] unfolding winding_number_prop_def by blast
-  have "homotopic_paths (- {z}) g p"
-    by (simp add: d p valid_path_imp_path norm_minus_commute gp_less)
-  moreover have "homotopic_paths (- {z}) h q"
-    by (simp add: e q valid_path_imp_path norm_minus_commute hq_less)
-  ultimately have "homotopic_paths (- {z}) p q"
-    by (blast intro: homotopic_paths_trans homotopic_paths_sym assms)
-  then have "contour_integral p (\<lambda>w. 1/(w - z)) = contour_integral q (\<lambda>w. 1/(w - z))"
-    by (rule Cauchy_theorem_homotopic_paths) (auto intro!: holomorphic_intros simp: p q)
-  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 \<notin> path_image g" and pah: "z \<notin> 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: "\<And>p. \<lbrakk>path p; pathfinish p = pathstart p; \<forall>t\<in>{0..1}. norm (p t - g t) < d\<rbrakk>
-            \<Longrightarrow> homotopic_loops (-{z}) g p"
-      and e: "\<And>q. \<lbrakk>path q; pathfinish q = pathstart q; \<forall>t\<in>{0..1}. norm (q t - h t) < e\<rbrakk>
-            \<Longrightarrow> 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 \<notin> path_image p"
-       "pathstart p = pathstart g" "pathfinish p = pathfinish g"
-       and gp_less:"\<forall>t\<in>{0..1}. cmod (g t - p t) < d"
-       and pap: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number g z"
-    using winding_number [OF \<open>path g\<close> pag \<open>0 < d\<close>] unfolding winding_number_prop_def by blast
-  obtain q where q:
-       "valid_path q" "z \<notin> path_image q"
-       "pathstart q = pathstart h" "pathfinish q = pathfinish h"
-       and hq_less: "\<forall>t\<in>{0..1}. cmod (h t - q t) < e"
-       and paq:  "contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number h z"
-    using winding_number [OF \<open>path h\<close> pah \<open>0 < e\<close>] unfolding winding_number_prop_def 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 (\<lambda>w. 1/(w - z)) = contour_integral q (\<lambda>w. 1/(w - z))"
-  proof (rule Cauchy_theorem_homotopic_loops)
-    show "homotopic_loops (- {z}) p q"
-      by (blast intro: homotopic_loops_trans homotopic_loops_sym gp hq assms)
-  qed (auto intro!: holomorphic_intros simp: p q)
-  then show ?thesis
-    by (simp add: pap paq)
-qed
-
-lemma winding_number_paths_linear_eq:
-  "\<lbrakk>path g; path h; pathstart h = pathstart g; pathfinish h = pathfinish g;
-    \<And>t. t \<in> {0..1} \<Longrightarrow> z \<notin> closed_segment (g t) (h t)\<rbrakk>
-        \<Longrightarrow> winding_number h z = winding_number g z"
-  by (blast intro: sym homotopic_paths_linear winding_number_homotopic_paths)
-
-lemma winding_number_loops_linear_eq:
-  "\<lbrakk>path g; path h; pathfinish g = pathstart g; pathfinish h = pathstart h;
-    \<And>t. t \<in> {0..1} \<Longrightarrow> z \<notin> closed_segment (g t) (h t)\<rbrakk>
-        \<Longrightarrow> winding_number h z = winding_number g z"
-  by (blast intro: sym homotopic_loops_linear winding_number_homotopic_loops)
-
-lemma winding_number_nearby_paths_eq:
-     "\<lbrakk>path g; path h; pathstart h = pathstart g; pathfinish h = pathfinish g;
-      \<And>t. t \<in> {0..1} \<Longrightarrow> norm(h t - g t) < norm(g t - z)\<rbrakk>
-      \<Longrightarrow> 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:
-     "\<lbrakk>path g; path h; pathfinish g = pathstart g; pathfinish h = pathstart h;
-      \<And>t. t \<in> {0..1} \<Longrightarrow> norm(h t - g t) < norm(g t - z)\<rbrakk>
-      \<Longrightarrow> winding_number h z = winding_number g z"
-  by (metis segment_bound(2) norm_minus_commute not_le winding_number_loops_linear_eq)
-
-
-lemma winding_number_subpath_combine:
-    "\<lbrakk>path g; z \<notin> path_image g;
-      u \<in> {0..1}; v \<in> {0..1}; w \<in> {0..1}\<rbrakk>
-      \<Longrightarrow> 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]])
-  using path_image_subpath_subset by auto
-
-subsection\<open>Partial circle path\<close>
-
-definition\<^marker>\<open>tag important\<close> part_circlepath :: "[complex, real, real, real, real] \<Rightarrow> complex"
-  where "part_circlepath z r s t \<equiv> \<lambda>x. z + of_real r * exp (\<i> * of_real (linepath s t x))"
-
-lemma pathstart_part_circlepath [simp]:
-     "pathstart(part_circlepath z r s t) = z + r*exp(\<i> * 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(\<i>*t)"
-by (metis part_circlepath_def pathfinish_def pathfinish_linepath)
-
-lemma reversepath_part_circlepath[simp]:
-    "reversepath (part_circlepath z r s t) = part_circlepath z r t s"
-  unfolding part_circlepath_def reversepath_def linepath_def 
-  by (auto simp:algebra_simps)
-    
-lemma has_vector_derivative_part_circlepath [derivative_intros]:
-    "((part_circlepath z r s t) has_vector_derivative
-      (\<i> * r * (of_real t - of_real s) * exp(\<i> * 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_field)
-  apply (rule derivative_eq_intros | simp)+
-  done
-
-lemma differentiable_part_circlepath:
-  "part_circlepath c r a b differentiable at x within A"
-  using has_vector_derivative_part_circlepath[of c r a b x A] differentiableI_vector by blast
-
-lemma vector_derivative_part_circlepath:
-    "vector_derivative (part_circlepath z r s t) (at x) =
-       \<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)"
-  using has_vector_derivative_part_circlepath vector_derivative_at by blast
-
-lemma vector_derivative_part_circlepath01:
-    "\<lbrakk>0 \<le> x; x \<le> 1\<rbrakk>
-     \<Longrightarrow> vector_derivative (part_circlepath z r s t) (at x within {0..1}) =
-          \<i> * r * (of_real t - of_real s) * exp(\<i> * 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 \<le> t"
-    shows "path_image (part_circlepath z r s t) = {z + r * exp(\<i> * of_real x) | x. s \<le> x \<and> x \<le> t}"
-proof -
-  { fix z::real
-    assume "0 \<le> z" "z \<le> 1"
-    with \<open>s \<le> t\<close> have "\<exists>x. (exp (\<i> * linepath s t z) = exp (\<i> * of_real x)) \<and> s \<le> x \<and> x \<le> 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 \<le> z" "z \<le> t"
-    then have "z + of_real r * exp (\<i> * of_real z) \<in> (\<lambda>x. z + of_real r * exp (\<i> * 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: field_split_simps)
-      done
-  }
-  ultimately show ?thesis
-    by (fastforce simp add: path_image_def part_circlepath_def)
-qed
-
-lemma path_image_part_circlepath':
-  "path_image (part_circlepath z r s t) = (\<lambda>x. z + r * cis x) ` closed_segment s t"
-proof -
-  have "path_image (part_circlepath z r s t) = 
-          (\<lambda>x. z + r * exp(\<i> * of_real x)) ` linepath s t ` {0..1}"
-    by (simp add: image_image path_image_def part_circlepath_def)
-  also have "linepath s t ` {0..1} = closed_segment s t"
-    by (rule linepath_image_01)
-  finally show ?thesis by (simp add: cis_conv_exp)
-qed
-
-lemma path_image_part_circlepath_subset:
-    "\<lbrakk>s \<le> t; 0 \<le> r\<rbrakk> \<Longrightarrow> path_image(part_circlepath z r s t) \<subseteq> sphere z r"
-by (auto simp: path_image_part_circlepath sphere_def dist_norm algebra_simps norm_mult)
-
-lemma in_path_image_part_circlepath:
-  assumes "w \<in> path_image(part_circlepath z r s t)" "s \<le> t" "0 \<le> r"
-    shows "norm(w - z) = r"
-proof -
-  have "w \<in> {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
-
-lemma path_image_part_circlepath_subset':
-  assumes "r \<ge> 0"
-  shows   "path_image (part_circlepath z r s t) \<subseteq> sphere z r"
-proof (cases "s \<le> t")
-  case True
-  thus ?thesis using path_image_part_circlepath_subset[of s t r z] assms by simp
-next
-  case False
-  thus ?thesis using path_image_part_circlepath_subset[of t s r z] assms
-    by (subst reversepath_part_circlepath [symmetric], subst path_image_reversepath) simp_all
-qed
-
-lemma part_circlepath_cnj: "cnj (part_circlepath c r a b x) = part_circlepath (cnj c) r (-a) (-b) x"
-  by (simp add: part_circlepath_def exp_cnj linepath_def algebra_simps)
-
-lemma contour_integral_bound_part_circlepath:
-  assumes "f contour_integrable_on part_circlepath c r a b"
-  assumes "B \<ge> 0" "r \<ge> 0" "\<And>x. x \<in> path_image (part_circlepath c r a b) \<Longrightarrow> norm (f x) \<le> B"
-  shows   "norm (contour_integral (part_circlepath c r a b) f) \<le> B * r * \<bar>b - a\<bar>"
-proof -
-  let ?I = "integral {0..1} (\<lambda>x. f (part_circlepath c r a b x) * \<i> * of_real (r * (b - a)) *
-              exp (\<i> * linepath a b x))"
-  have "norm ?I \<le> integral {0..1} (\<lambda>x::real. B * 1 * (r * \<bar>b - a\<bar>) * 1)"
-  proof (rule integral_norm_bound_integral, goal_cases)
-    case 1
-    with assms(1) show ?case
-      by (simp add: contour_integrable_on vector_derivative_part_circlepath mult_ac)
-  next
-    case (3 x)
-    with assms(2-) show ?case unfolding norm_mult norm_of_real abs_mult
-      by (intro mult_mono) (auto simp: path_image_def)
-  qed auto
-  also have "?I = contour_integral (part_circlepath c r a b) f"
-    by (simp add: contour_integral_integral vector_derivative_part_circlepath mult_ac)
-  finally show ?thesis by simp
-qed
-
-lemma has_contour_integral_part_circlepath_iff:
-  assumes "a < b"
-  shows "(f has_contour_integral I) (part_circlepath c r a b) \<longleftrightarrow>
-           ((\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) has_integral I) {a..b}"
-proof -
-  have "(f has_contour_integral I) (part_circlepath c r a b) \<longleftrightarrow>
-          ((\<lambda>x. f (part_circlepath c r a b x) * vector_derivative (part_circlepath c r a b)
-           (at x within {0..1})) has_integral I) {0..1}"
-    unfolding has_contour_integral_def ..
-  also have "\<dots> \<longleftrightarrow> ((\<lambda>x. f (part_circlepath c r a b x) * r * (b - a) * \<i> *
-                            cis (linepath a b x)) has_integral I) {0..1}"
-    by (intro has_integral_cong, subst vector_derivative_part_circlepath01)
-       (simp_all add: cis_conv_exp)
-  also have "\<dots> \<longleftrightarrow> ((\<lambda>x. f (c + r * exp (\<i> * linepath (of_real a) (of_real b) x)) *
-                       r * \<i> * exp (\<i> * linepath (of_real a) (of_real b) x) *
-                       vector_derivative (linepath (of_real a) (of_real b)) 
-                         (at x within {0..1})) has_integral I) {0..1}"
-    by (intro has_integral_cong, subst vector_derivative_linepath_within)
-       (auto simp: part_circlepath_def cis_conv_exp of_real_linepath [symmetric])
-  also have "\<dots> \<longleftrightarrow> ((\<lambda>z. f (c + r * exp (\<i> * z)) * r * \<i> * exp (\<i> * z)) has_contour_integral I)
-                      (linepath (of_real a) (of_real b))"
-    by (simp add: has_contour_integral_def)
-  also have "\<dots> \<longleftrightarrow> ((\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) has_integral I) {a..b}" using assms
-    by (subst has_contour_integral_linepath_Reals_iff) (simp_all add: cis_conv_exp)
-  finally show ?thesis .
-qed
-
-lemma contour_integrable_part_circlepath_iff:
-  assumes "a < b"
-  shows "f contour_integrable_on (part_circlepath c r a b) \<longleftrightarrow>
-           (\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) integrable_on {a..b}"
-  using assms by (auto simp: contour_integrable_on_def integrable_on_def 
-                             has_contour_integral_part_circlepath_iff)
-
-lemma contour_integral_part_circlepath_eq:
-  assumes "a < b"
-  shows "contour_integral (part_circlepath c r a b) f =
-           integral {a..b} (\<lambda>t. f (c + r * cis t) * r * \<i> * cis t)"
-proof (cases "f contour_integrable_on part_circlepath c r a b")
-  case True
-  hence "(\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) integrable_on {a..b}" 
-    using assms by (simp add: contour_integrable_part_circlepath_iff)
-  with True show ?thesis
-    using has_contour_integral_part_circlepath_iff[OF assms]
-          contour_integral_unique has_integral_integrable_integral by blast
-next
-  case False
-  hence "\<not>(\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) integrable_on {a..b}" 
-    using assms by (simp add: contour_integrable_part_circlepath_iff)
-  with False show ?thesis
-    by (simp add: not_integrable_contour_integral not_integrable_integral)
-qed
-
-lemma contour_integral_part_circlepath_reverse:
-  "contour_integral (part_circlepath c r a b) f = -contour_integral (part_circlepath c r b a) f"
-  by (subst reversepath_part_circlepath [symmetric], subst contour_integral_reversepath) simp_all
-
-lemma contour_integral_part_circlepath_reverse':
-  "b < a \<Longrightarrow> contour_integral (part_circlepath c r a b) f = 
-               -contour_integral (part_circlepath c r b a) f"
-  by (rule contour_integral_part_circlepath_reverse)
-
-lemma finite_bounded_log: "finite {z::complex. norm z \<le> b \<and> 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) * \<i>) \<le> b + cmod (Ln w)}"
-    apply (simp add: norm_mult finite_int_iff_bounded_le)
-    apply (rule_tac x="\<lfloor>(b + cmod (Ln w)) / (2*pi)\<rfloor>" in exI)
-    apply (auto simp: field_split_simps le_floor_iff)
-    done
-  have [simp]: "\<And>P f. {z. P z \<and> (\<exists>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 \<noteq> 0"
-    shows "finite {z. norm z \<le> b \<and> exp(a*z) = w}"
-proof -
-  have *: "finite ((\<lambda>z. z / a) ` {z. cmod z \<le> b * cmod a \<and> exp z = w})"
-    by (rule finite_imageI [OF finite_bounded_log])
-  show ?thesis
-    by (rule finite_subset [OF _ *]) (force simp: assms norm_mult)
-qed
-
-lemma 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 \<le> B" "0 < r" "s \<le> t"
-      and B: "\<And>x. x \<in> path_image(part_circlepath z r s t) - k \<Longrightarrow> norm(f x) \<le> B"
-    shows "cmod i \<le> B * r * (t - s)"
-proof -
-  consider "s = t" | "s < t" using \<open>s \<le> t\<close> 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]: "\<bar>r\<bar> = r" using \<open>r > 0\<close> 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} \<inter> {0..1})" if "y \<in> k" for y
-    proof -
-      define w where "w = (y - z)/of_real r / exp(\<i> * of_real s)"
-      have fin: "finite (of_real -` {z. cmod z \<le> 1 \<and> exp (\<i> * complex_of_real (t - s) * z) = w})"
-        apply (rule finite_vimageI [OF finite_bounded_log2])
-        using \<open>s < t\<close> 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 \<inter> {0..1})"
-      by (rule finite_finite_vimage_IntI [OF \<open>finite k\<close>])
-    have **: "((\<lambda>x. if (part_circlepath z r s t x) \<in> 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}"
-      by (rule has_integral_spike [OF negligible_finite [OF fin01]])  (use fi has_contour_integral in auto)
-    have *: "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1; part_circlepath z r s t x \<notin> k\<rbrakk> \<Longrightarrow> cmod (f (part_circlepath z r s t x)) \<le> 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" \<open>r > 0\<close> by auto
-  qed
-qed
-
-lemma has_contour_integral_bound_part_circlepath:
-      "\<lbrakk>(f has_contour_integral i) (part_circlepath z r s t);
-        0 \<le> B; 0 < r; s \<le> t;
-        \<And>x. x \<in> path_image(part_circlepath z r s t) \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
-       \<Longrightarrow> norm i \<le> B*r*(t - s)"
-  by (auto intro: has_contour_integral_bound_part_circlepath_strong)
-
-lemma contour_integrable_continuous_part_circlepath:
-     "continuous_on (path_image (part_circlepath z r s t)) f
-      \<Longrightarrow> 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 \<notin> 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 \<open>0 < r\<close> 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 \<open>0 < r\<close> by auto
-qed
-
-lemma simple_path_part_circlepath:
-    "simple_path(part_circlepath z r s t) \<longleftrightarrow> (r \<noteq> 0 \<and> s \<noteq> t \<and> \<bar>s - t\<bar> \<le> 2*pi)"
-proof (cases "r = 0 \<or> s = t")
-  case True
-  then show ?thesis
-    unfolding part_circlepath_def simple_path_def
-    by (rule disjE) (force intro: bexI [where x = "1/4"] bexI [where x = "1/3"])+
-next
-  case False then have "r \<noteq> 0" "s \<noteq> t" by auto
-  have *: "\<And>x y z s t. \<i>*((1 - x) * s + x * t) = \<i>*(((1 - y) * s + y * t)) + z  \<longleftrightarrow> \<i>*(x - y) * (t - s) = z"
-    by (simp add: algebra_simps)
-  have abs01: "\<And>x y::real. 0 \<le> x \<and> x \<le> 1 \<and> 0 \<le> y \<and> y \<le> 1
-                      \<Longrightarrow> (x = y \<or> x = 0 \<and> y = 1 \<or> x = 1 \<and> y = 0 \<longleftrightarrow> \<bar>x - y\<bar> \<in> {0,1})"
-    by auto
-  have **: "\<And>x y. (\<exists>n. (complex_of_real x - of_real y) * (of_real t - of_real s) = 2 * (of_int n * of_real pi)) \<longleftrightarrow>
-                  (\<exists>n. \<bar>x - y\<bar> * (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: "\<bar>s - t\<bar> \<le> 2 * pi"
-    if "\<And>x. 0 \<le> x \<and> x \<le> 1 \<Longrightarrow> (\<exists>n. x * (t - s) = 2 * (real_of_int n * pi)) \<longrightarrow> x = 0 \<or> x = 1"
-  proof (rule ccontr)
-    assume "\<not> \<bar>s - t\<bar> \<le> 2 * pi"
-    then have *: "\<And>n. t - s \<noteq> of_int n * \<bar>s - t\<bar>"
-      using False that [of "2*pi / \<bar>t - s\<bar>"]
-      by (simp add: abs_minus_commute divide_simps)
-    show False
-      using * [of 1] * [of "-1"] by auto
-  qed
-  have 2: "\<bar>s - t\<bar> = \<bar>2 * (real_of_int n * pi) / x\<bar>" if "x \<noteq> 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
-  have abs_away: "\<And>P. (\<forall>x\<in>{0..1}. \<forall>y\<in>{0..1}. P \<bar>x - y\<bar>) \<longleftrightarrow> (\<forall>x::real. 0 \<le> x \<and> x \<le> 1 \<longrightarrow> P x)"
-    by force
-  show ?thesis using False
-    apply (simp add: simple_path_def)
-    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 field_split_simps abs_mult dest: of_int_leD)
-    done
-qed
-
-lemma arc_part_circlepath:
-  assumes "r \<noteq> 0" "s \<noteq> t" "\<bar>s - t\<bar> < 2*pi"
-    shows "arc (part_circlepath z r s t)"
-proof -
-  have *: "x = y" if eq: "\<i> * (linepath s t x) = \<i> * (linepath s t y) + 2 * of_int n * complex_of_real pi * \<i>"
-    and x: "x \<in> {0..1}" and y: "y \<in> {0..1}" for x y n
-  proof (rule ccontr)
-    assume "x \<noteq> y"
-    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_div_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])
-    with \<open>x \<noteq> y\<close> have st: "s-t = (of_int n * (pi * 2) / (y-x))"
-      by (force simp: field_simps)
-    have "\<bar>real_of_int n\<bar> < \<bar>y - x\<bar>"
-      using assms \<open>x \<noteq> y\<close> by (simp add: st abs_mult field_simps)
-    then show False
-      using assms x y st by (auto dest: of_int_lessD)
-  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\<open>Special case of one complete circle\<close>
-
-definition\<^marker>\<open>tag important\<close> circlepath :: "[complex, real, real] \<Rightarrow> complex"
-  where "circlepath z r \<equiv> part_circlepath z r 0 (2*pi)"
-
-lemma circlepath: "circlepath z r = (\<lambda>x. z + r * exp(2 * of_real pi * \<i> * 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 * \<i> * (x + 1/2)) =
-        z + of_real r * exp (2 * pi * \<i> * x + pi * \<i>)"
-    by (simp add: divide_simps) (simp add: algebra_simps)
-  also have "\<dots> = z - r * exp (2 * pi * \<i> * 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)) \<subseteq> path_image (circlepath z r)"
-  apply (simp add: path_image_def image_def circlepath_minus, clarify)
-  apply (case_tac "xa \<le> 1/2", force)
-  apply (force simp: 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
-
-lemma has_vector_derivative_circlepath [derivative_intros]:
- "((circlepath z r) has_vector_derivative (2 * pi * \<i> * r * exp (2 * of_real pi * \<i> * 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
-
-lemma vector_derivative_circlepath:
-   "vector_derivative (circlepath z r) (at x) =
-    2 * pi * \<i> * r * exp(2 * of_real pi * \<i> * x)"
-using has_vector_derivative_circlepath vector_derivative_at by blast
-
-lemma vector_derivative_circlepath01:
-    "\<lbrakk>0 \<le> x; x \<le> 1\<rbrakk>
-     \<Longrightarrow> vector_derivative (circlepath z r) (at x within {0..1}) =
-          2 * pi * \<i> * r * exp(2 * of_real pi * \<i> * 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 \<le> r" shows "path_image (circlepath z r) = sphere z r"
-proof -
-  have *: "x \<in> (\<lambda>u. z + (cmod (x - z)) * exp (\<i> * (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
-    define w where "w = x - z"
-    then have "w \<noteq> 0" by (simp add: False)
-    have **: "\<And>t. \<lbrakk>Re w = cos t * cmod w; Im w = sin t * cmod w\<rbrakk> \<Longrightarrow> w = of_real (cmod w) * exp (\<i> * 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 \<open>w \<noteq> 0\<close> cmod_power2 [symmetric])
-      apply (rule_tac x="t / (2*pi)" in image_eqI)
-      apply (simp add: field_simps \<open>w \<noteq> 0\<close>)
-      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
-
-lemma path_image_circlepath [simp]:
-    "path_image (circlepath z r) = sphere z \<bar>r\<bar>"
-  using path_image_circlepath_minus
-  by (force simp: path_image_circlepath_nonneg abs_if)
-
-lemma has_contour_integral_bound_circlepath_strong:
-      "\<lbrakk>(f has_contour_integral i) (circlepath z r);
-        finite k; 0 \<le> B; 0 < r;
-        \<And>x. \<lbrakk>norm(x - z) = r; x \<notin> k\<rbrakk> \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
-        \<Longrightarrow> norm i \<le> 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)
-
-lemma has_contour_integral_bound_circlepath:
-      "\<lbrakk>(f has_contour_integral i) (circlepath z r);
-        0 \<le> B; 0 < r; \<And>x. norm(x - z) = r \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
-        \<Longrightarrow> norm i \<le> B*(2*pi*r)"
-  by (auto intro: has_contour_integral_bound_circlepath_strong)
-
-lemma contour_integrable_continuous_circlepath:
-    "continuous_on (path_image (circlepath z r)) f
-     \<Longrightarrow> 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) \<longleftrightarrow> (r \<noteq> 0)"
-  by (simp add: circlepath_def simple_path_part_circlepath)
-
-lemma notin_path_image_circlepath [simp]: "cmod (w - z) < r \<Longrightarrow> w \<notin> path_image (circlepath z r)"
-  by (simp add: sphere_def dist_norm norm_minus_commute)
-
-lemma contour_integral_circlepath:
-  assumes "r > 0"
-  shows "contour_integral (circlepath z r) (\<lambda>w. 1 / (w - z)) = 2 * complex_of_real pi * \<i>"
-proof (rule contour_integral_unique)
-  show "((\<lambda>w. 1 / (w - z)) has_contour_integral 2 * complex_of_real pi * \<i>) (circlepath z r)"
-    unfolding has_contour_integral_def using assms
-    apply (subst has_integral_cong)
-     apply (simp add: vector_derivative_circlepath01)
-    using has_integral_const_real [of _ 0 1] apply (force simp: circlepath)
-    done
-qed
-
-lemma winding_number_circlepath_centre: "0 < r \<Longrightarrow> 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
-  define r' where "r' = norm(w - z)"
-  have "r' < r"
-    by (simp add: assms r'_def)
-  have disjo: "cball z r' \<inter> sphere z r = {}"
-    using \<open>r' < r\<close> by (force simp: cball_def sphere_def)
-  have "winding_number(circlepath z r) w = winding_number(circlepath z r) z"
-  proof (rule winding_number_around_inside [where s = "cball z r'"])
-    show "winding_number (circlepath z r) z \<noteq> 0"
-      by (simp add: winding_number_circlepath_centre)
-    show "cball z r' \<inter> path_image (circlepath z r) = {}"
-      by (simp add: disjo less_eq_real_def)
-  qed (auto simp: r'_def dist_norm norm_minus_commute)
-  also have "\<dots> = 1"
-    by (simp add: winding_number_circlepath_centre)
-  finally show ?thesis .
-qed
-
-
-text\<open> Hence the Cauchy formula for points inside a circle.\<close>
-
-theorem Cauchy_integral_circlepath:
-  assumes contf: "continuous_on (cball z r) f" and holf: "f holomorphic_on (ball z r)" and wz: "norm(w - z) < r"
-  shows "((\<lambda>u. f u/(u - w)) has_contour_integral (2 * of_real pi * \<i> * f w))
-         (circlepath z r)"
-proof -
-  have "r > 0"
-    using assms le_less_trans norm_ge_zero by blast
-  have "((\<lambda>u. f u / (u - w)) has_contour_integral (2 * pi) * \<i> * winding_number (circlepath z r) w * f w)
-        (circlepath z r)"
-  proof (rule Cauchy_integral_formula_weak [where s = "cball z r" and k = "{}"])
-    show "\<And>x. x \<in> interior (cball z r) - {} \<Longrightarrow>
-         f field_differentiable at x"
-      using holf holomorphic_on_imp_differentiable_at by auto
-    have "w \<notin> sphere z r"
-      by simp (metis dist_commute dist_norm not_le order_refl wz)
-    then show "path_image (circlepath z r) \<subseteq> cball z r - {w}"
-      using \<open>r > 0\<close> by (auto simp add: cball_def sphere_def)
-  qed (use wz in \<open>simp_all add: dist_norm norm_minus_commute contf\<close>)
-  then show ?thesis
-    by (simp add: winding_number_circlepath assms)
-qed
-
-corollary\<^marker>\<open>tag unimportant\<close> Cauchy_integral_circlepath_simple:
-  assumes "f holomorphic_on cball z r" "norm(w - z) < r"
-  shows "((\<lambda>u. f u/(u - w)) has_contour_integral (2 * of_real pi * \<i> * 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 \<subseteq> s" "pathfinish g = pathstart g" "z \<notin> s"
-      and nb: "\<And>z. bounded (connected_component_set (- s) z) \<longrightarrow> z \<in> s"
-  shows "winding_number g z = 0"
-apply (rule winding_number_zero_in_outside)
-apply (simp_all add: assms)
-by (metis nb [of z] \<open>path_image g \<subseteq> s\<close> \<open>z \<notin> s\<close> contra_subsetD mem_Collect_eq outside outside_mono)
-
-lemma no_bounded_path_component_imp_winding_number_zero:
-  assumes g: "path g" "path_image g \<subseteq> s" "pathfinish g = pathstart g" "z \<notin> s"
-      and nb: "\<And>z. bounded (path_component_set (- s) z) \<longrightarrow> z \<in> 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\<open> Uniform convergence of path integral\<close>
-
-text\<open>Uniform convergence when the derivative of the path is bounded, and in particular for the special case of a circle.\<close>
-
-proposition contour_integral_uniform_limit:
-  assumes ev_fint: "eventually (\<lambda>n::'a. (f n) contour_integrable_on \<gamma>) F"
-      and ul_f: "uniform_limit (path_image \<gamma>) f l F"
-      and noleB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
-      and \<gamma>: "valid_path \<gamma>"
-      and [simp]: "\<not> trivial_limit F"
-  shows "l contour_integrable_on \<gamma>" "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
-proof -
-  have "0 \<le> 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 "0 < e / (\<bar>B\<bar> + 1)" by simp
-    then have "\<forall>\<^sub>F n in F. \<forall>x\<in>path_image \<gamma>. cmod (f n x - l x) < e / (\<bar>B\<bar> + 1)"
-      using ul_f [unfolded uniform_limit_iff dist_norm] by auto
-    with ev_fint
-    obtain a where fga: "\<And>x. x \<in> {0..1} \<Longrightarrow> cmod (f a (\<gamma> x) - l (\<gamma> x)) < e / (\<bar>B\<bar> + 1)"
-               and inta: "(\<lambda>t. f a (\<gamma> t) * vector_derivative \<gamma> (at t)) integrable_on {0..1}"
-      using eventually_happens [OF eventually_conj]
-      by (fastforce simp: contour_integrable_on path_image_def)
-    have Ble: "B * e / (\<bar>B\<bar> + 1) \<le> e"
-      using \<open>0 \<le> B\<close>  \<open>0 < e\<close> by (simp add: field_split_simps)
-    have "\<exists>h. (\<forall>x\<in>{0..1}. cmod (l (\<gamma> x) * vector_derivative \<gamma> (at x) - h x) \<le> e) \<and> h integrable_on {0..1}"
-    proof (intro exI conjI ballI)
-      show "cmod (l (\<gamma> x) * vector_derivative \<gamma> (at x) - f a (\<gamma> x) * vector_derivative \<gamma> (at x)) \<le> e"
-        if "x \<in> {0..1}" for x
-        apply (rule order_trans [OF _ Ble])
-        using noleB [OF that] fga [OF that] \<open>0 \<le> B\<close> \<open>0 < e\<close>
-        apply (simp add: norm_mult left_diff_distrib [symmetric] norm_minus_commute divide_simps)
-        apply (fastforce simp: mult_ac dest: mult_mono [OF less_imp_le])
-        done
-    qed (rule inta)
-  }
-  then show lintg: "l contour_integrable_on \<gamma>"
-    unfolding contour_integrable_on by (metis (mono_tags, lifting)integrable_uniform_limit_real)
-  { fix e::real
-    define B' where "B' = B + 1"
-    have B': "B' > 0" "B' > B" using  \<open>0 \<le> B\<close> by (auto simp: B'_def)
-    assume "0 < e"
-    then have ev_no': "\<forall>\<^sub>F n in F. \<forall>x\<in>path_image \<gamma>. 2 * cmod (f n x - l x) < e / B'"
-      using ul_f [unfolded uniform_limit_iff dist_norm, rule_format, of "e / B' / 2"] B'
-        by (simp add: field_simps)
-    have ie: "integral {0..1::real} (\<lambda>x. e / 2) < e" using \<open>0 < e\<close> by simp
-    have *: "cmod (f x (\<gamma> t) * vector_derivative \<gamma> (at t) - l (\<gamma> t) * vector_derivative \<gamma> (at t)) \<le> e / 2"
-             if t: "t\<in>{0..1}" and leB': "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) < e / B'" for x t
-    proof -
-      have "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) * cmod (vector_derivative \<gamma> (at t)) \<le> e * (B/ B')"
-        using mult_mono [OF less_imp_le [OF leB'] noleB] B' \<open>0 < e\<close> t by auto
-      also have "\<dots> < e"
-        by (simp add: B' \<open>0 < e\<close> mult_imp_div_pos_less)
-      finally have "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) * cmod (vector_derivative \<gamma> (at t)) < e" .
-      then show ?thesis
-        by (simp add: left_diff_distrib [symmetric] norm_mult)
-    qed
-    have le_e: "\<And>x. \<lbrakk>\<forall>xa\<in>{0..1}. 2 * cmod (f x (\<gamma> xa) - l (\<gamma> xa)) < e / B'; f x contour_integrable_on \<gamma>\<rbrakk>
-         \<Longrightarrow> cmod (integral {0..1}
-                    (\<lambda>u. f x (\<gamma> u) * vector_derivative \<gamma> (at u) - l (\<gamma> u) * vector_derivative \<gamma> (at u))) < e"
-      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
-    have "\<forall>\<^sub>F x in F. dist (contour_integral \<gamma> (f x)) (contour_integral \<gamma> 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] le_e)
-      done
-  }
-  then show "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
-    by (rule tendstoI)
-qed
-
-corollary\<^marker>\<open>tag unimportant\<close> contour_integral_uniform_limit_circlepath:
-  assumes "\<forall>\<^sub>F n::'a in F. (f n) contour_integrable_on (circlepath z r)"
-      and "uniform_limit (sphere z r) f l F"
-      and "\<not> trivial_limit F" "0 < r"
-    shows "l contour_integrable_on (circlepath z r)"
-          "((\<lambda>n. contour_integral (circlepath z r) (f n)) \<longlongrightarrow> contour_integral (circlepath z r) l) F"
-  using assms by (auto simp: vector_derivative_circlepath norm_mult intro!: contour_integral_uniform_limit)
-
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>General stepping result for derivative formulas\<close>
-
-lemma Cauchy_next_derivative:
-  assumes "continuous_on (path_image \<gamma>) f'"
-      and leB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
-      and int: "\<And>w. w \<in> s - path_image \<gamma> \<Longrightarrow> ((\<lambda>u. f' u / (u - w)^k) has_contour_integral f w) \<gamma>"
-      and k: "k \<noteq> 0"
-      and "open s"
-      and \<gamma>: "valid_path \<gamma>"
-      and w: "w \<in> s - path_image \<gamma>"
-    shows "(\<lambda>u. f' u / (u - w)^(Suc k)) contour_integrable_on \<gamma>"
-      and "(f has_field_derivative (k * contour_integral \<gamma> (\<lambda>u. f' u/(u - w)^(Suc k))))
-           (at w)"  (is "?thes2")
-proof -
-  have "open (s - path_image \<gamma>)" using \<open>open s\<close> closed_valid_path_image \<gamma> by blast
-  then obtain d where "d>0" and d: "ball w d \<subseteq> s - path_image \<gamma>" using w
-    using open_contains_ball by blast
-  have [simp]: "\<And>n. cmod (1 + of_nat n) = 1 + of_nat n"
-    by (metis norm_of_nat of_nat_Suc)
-  have cint: "\<And>x. \<lbrakk>x \<noteq> w; cmod (x - w) < d\<rbrakk>
-         \<Longrightarrow> (\<lambda>z. (f' z / (z - x) ^ k - f' z / (z - w) ^ k) / (x * k - w * k)) contour_integrable_on \<gamma>"
-    apply (rule contour_integrable_div [OF contour_integrable_diff])
-    using int w d
-    by (force simp: dist_norm norm_minus_commute intro!: has_contour_integral_integrable)+
-  have 1: "\<forall>\<^sub>F n in at w. (\<lambda>x. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k)
-                         contour_integrable_on \<gamma>"
-    unfolding eventually_at
-    apply (rule_tac x=d in exI)
-    apply (simp add: \<open>d > 0\<close> dist_norm field_simps cint)
-    done
-  have bim_g: "bounded (image f' (path_image \<gamma>))"
-    by (simp add: compact_imp_bounded compact_continuous_image compact_valid_path_image assms)
-  then obtain C where "C > 0" and C: "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> cmod (f' (\<gamma> x)) \<le> C"
-    by (force simp: bounded_pos path_image_def)
-  have twom: "\<forall>\<^sub>F n in at w.
-               \<forall>x\<in>path_image \<gamma>.
-                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 \<in> path_image \<gamma>" and "u \<noteq> 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 -
-      define ff where [abs_def]:
-        "ff n 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))" for n :: nat and w
-      have km1: "\<And>z::complex. z \<noteq> 0 \<Longrightarrow> z ^ (k - Suc 0) = z ^ k / z"
-        by (simp add: field_simps) (metis Suc_pred \<open>k \<noteq> 0\<close> neq0_conv power_Suc)
-      have ff1: "(ff i has_field_derivative ff (Suc i) z) (at z within ball w (d/2))"
-              if "z \<in> ball w (d/2)" "i \<le> 1" for i z
-      proof -
-        have "z \<notin> path_image \<gamma>"
-          using \<open>x \<in> path_image \<gamma>\<close> d that ball_divide_subset_numeral by blast
-        then have xz[simp]: "x \<noteq> z" using \<open>x \<in> path_image \<gamma>\<close> by blast
-        then have neq: "x * x + z * z \<noteq> x * (z * 2)"
-          by (blast intro: dest!: sum_sqs_eq)
-        with xz have "\<And>v. v \<noteq> 0 \<Longrightarrow> (x * x + z * z) * v \<noteq> (x * (z * 2) * v)" by auto
-        then have neqq: "\<And>v. v \<noteq> 0 \<Longrightarrow> x * (x * v) + z * (z * v) \<noteq> x * (z * (2 * v))"
-          by (simp add: algebra_simps)
-        show ?thesis using \<open>i \<le> 1\<close>
-          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) \<le> k * (1 + real k) * (4 / b) \<longleftrightarrow> b \<le> 4 * a"
-          by (subst mult_le_cancel_left_pos)
-            (use \<open>k \<noteq> 0\<close> in \<open>auto simp: divide_simps\<close>)
-        with ab have "real k * (1 + real k) / a \<le> (real k * 4 + real k * real k * 4) / b \<longleftrightarrow> b \<le> 4 * a"
-          by (simp add: field_simps)
-      } note canc = this
-      have ff2: "cmod (ff (Suc 1) v) \<le> real (k * (k + 1)) / (d/2) ^ (k + 2)"
-                if "v \<in> ball w (d/2)" for v
-      proof -
-        have lessd: "\<And>z. cmod (\<gamma> z - v) < d/2 \<Longrightarrow> cmod (w - \<gamma> z) < d"
-          by (metis that norm_minus_commute norm_triangle_half_r dist_norm mem_ball)
-        have "d/2 \<le> cmod (x - v)" using d x that
-          using lessd d x
-          by (auto simp add: dist_norm path_image_def ball_def not_less [symmetric] del: divide_const_simps)
-        then have "d \<le> cmod (x - v) * 2"
-          by (simp add: field_split_simps)
-        then have dpow_le: "d ^ (k+2) \<le> (cmod (x - v) * 2) ^ (k+2)"
-          using \<open>0 < d\<close> order_less_imp_le power_mono by blast
-        have "x \<noteq> v" using that
-          using \<open>x \<in> path_image \<gamma>\<close> ball_divide_subset_numeral d by fastforce
-        then show ?thesis
-        using \<open>d > 0\<close> apply (simp add: ff_def norm_mult norm_divide norm_power dist_norm canc)
-        using dpow_le apply (simp add: field_split_simps)
-        done
-      qed
-      have ub: "u \<in> 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)))
-                  \<le> (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 \<open>0 < d\<close>)
-      then have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
-                  \<le> (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)
-                  \<le> (1 + real k) * cmod (u - w) / (d/2) ^ (k+2)"
-        using \<open>k \<noteq> 0\<close> \<open>u \<noteq> w\<close> by (simp add: mult_ac zero_less_mult_iff pos_divide_le_eq)
-      also have "\<dots> < e"
-        using uw_less \<open>0 < d\<close> 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 \<noteq> u"
-        using uwd \<open>0 < d\<close> x d by (force simp: dist_norm ball_def norm_minus_commute)
-      show ?thesis
-        apply (rule le_less_trans [OF _ e])
-        using \<open>k \<noteq> 0\<close> \<open>x \<noteq> u\<close> \<open>u \<noteq> w\<close>
-        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: \<open>d > 0\<close> dist_norm that simp del: power_Suc intro: *)
-      done
-  qed
-  have 2: "uniform_limit (path_image \<gamma>) (\<lambda>n x. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k) (\<lambda>x. f' x / (x - w) ^ Suc k) (at w)"
-    unfolding uniform_limit_iff dist_norm
-  proof clarify
-    fix e::real
-    assume "0 < e"
-    have *: "cmod (f' (\<gamma> x) * (inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
-                        f' (\<gamma> x) / ((\<gamma> x - w) * (\<gamma> x - w) ^ k)) < e"
-              if ec: "cmod ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
-                      inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k) < e / C"
-                 and x: "0 \<le> x" "x \<le> 1"
-              for u x
-    proof (cases "(f' (\<gamma> x)) = 0")
-      case True then show ?thesis by (simp add: \<open>0 < e\<close>)
-    next
-      case False
-      have "cmod (f' (\<gamma> x) * (inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
-                        f' (\<gamma> x) / ((\<gamma> x - w) * (\<gamma> x - w) ^ k)) =
-            cmod (f' (\<gamma> x) * ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
-                             inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k))"
-        by (simp add: field_simps)
-      also have "\<dots> = cmod (f' (\<gamma> x)) *
-                       cmod ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
-                             inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k)"
-        by (simp add: norm_mult)
-      also have "\<dots> < cmod (f' (\<gamma> x)) * (e/C)"
-        using False mult_strict_left_mono [OF ec] by force
-      also have "\<dots> \<le> e" using C
-        by (metis False \<open>0 < e\<close> frac_le less_eq_real_def mult.commute pos_le_divide_eq x zero_less_norm_iff)
-      finally show ?thesis .
-    qed
-    show "\<forall>\<^sub>F n in at w.
-              \<forall>x\<in>path_image \<gamma>.
-               cmod (f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k - f' x / (x - w) ^ Suc k) < e"
-      using twom [OF divide_pos_pos [OF \<open>0 < e\<close> \<open>C > 0\<close>]]   unfolding path_image_def
-      by (force intro: * elim: eventually_mono)
-  qed
-  show "(\<lambda>u. f' u / (u - w) ^ (Suc k)) contour_integrable_on \<gamma>"
-    by (rule contour_integral_uniform_limit [OF 1 2 leB \<gamma>]) auto
-  have *: "(\<lambda>n. contour_integral \<gamma> (\<lambda>x. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / k))
-           \<midarrow>w\<rightarrow> contour_integral \<gamma> (\<lambda>u. f' u / (u - w) ^ (Suc k))"
-    by (rule contour_integral_uniform_limit [OF 1 2 leB \<gamma>]) auto
-  have **: "contour_integral \<gamma> (\<lambda>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
-  proof -
-    have u: "u \<in> s - path_image \<gamma>"
-      by (metis subsetD d dist_commute mem_ball that)
-    show ?thesis
-      apply (rule contour_integral_unique)
-      apply (simp add: diff_divide_distrib algebra_simps)
-      apply (intro has_contour_integral_diff has_contour_integral_div)
-      using u w apply (simp_all add: field_simps int)
-      done
-  qed
-  show ?thes2
-    apply (simp add: has_field_derivative_iff del: power_Suc)
-    apply (rule Lim_transform_within [OF tendsto_mult_left [OF *] \<open>0 < d\<close> ])
-    apply (simp add: \<open>k \<noteq> 0\<close> **)
-    done
-qed
-
-lemma Cauchy_next_derivative_circlepath:
-  assumes contf: "continuous_on (path_image (circlepath z r)) f"
-      and int: "\<And>w. w \<in> ball z r \<Longrightarrow> ((\<lambda>u. f u / (u - w)^k) has_contour_integral g w) (circlepath z r)"
-      and k: "k \<noteq> 0"
-      and w: "w \<in> ball z r"
-    shows "(\<lambda>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) (\<lambda>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 \<in> 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 * \<bar>r\<bar>"];
-        auto simp: vector_derivative_circlepath norm_mult)+
-qed
-
-
-text\<open> In particular, the first derivative formula.\<close>
-
-lemma Cauchy_derivative_integral_circlepath:
-  assumes contf: "continuous_on (cball z r) f"
-      and holf: "f holomorphic_on ball z r"
-      and w: "w \<in> ball z r"
-    shows "(\<lambda>u. f u/(u - w)^2) contour_integrable_on (circlepath z r)"
-           (is "?thes1")
-      and "(f has_field_derivative (1 / (2 * of_real pi * \<i>) * contour_integral(circlepath z r) (\<lambda>u. f u / (u - w)^2))) (at w)"
-           (is "?thes2")
-proof -
-  have [simp]: "r \<ge> 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: cball_def sphere_def)
-  have int: "\<And>w. dist z w < r \<Longrightarrow>
-                 ((\<lambda>u. f u / (u - w)) has_contour_integral (\<lambda>x. 2 * of_real pi * \<i> * 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 "((\<lambda>x. 2 * of_real pi * \<i> * f x) has_field_derivative contour_integral (circlepath z r) (\<lambda>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 = "\<lambda>x. 2 * of_real pi * \<i> * f x", simplified])
-    apply (blast intro: int)
-    done
-  then have fder: "(f has_field_derivative contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)^2) / (2 * of_real pi * \<i>)) (at w)"
-    by (rule DERIV_cdivide [where f = "\<lambda>x. 2 * of_real pi * \<i> * f x" and c = "2 * of_real pi * \<i>", simplified])
-  show ?thes2
-    by simp (rule fder)
-qed
-
-subsection\<open>Existence of all higher derivatives\<close>
-
-proposition derivative_is_holomorphic:
-  assumes "open S"
-      and fder: "\<And>z. z \<in> S \<Longrightarrow> (f has_field_derivative f' z) (at z)"
-    shows "f' holomorphic_on S"
-proof -
-  have *: "\<exists>h. (f' has_field_derivative h) (at z)" if "z \<in> S" for z
-  proof -
-    obtain r where "r > 0" and r: "cball z r \<subseteq> S"
-      using open_contains_cball \<open>z \<in> S\<close> \<open>open S\<close> 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 \<open>r > 0\<close> by (force elim: holomorphic_on_subset [THEN holomorphic_on_imp_continuous_on])
-    then have contfpi: "continuous_on (path_image (circlepath z r)) (\<lambda>x. 1/(2 * of_real pi*\<i>) * 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 \<in> ball z r"
-      have intf: "(\<lambda>u. f u / (u - w)\<^sup>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 * \<i>) * contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>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) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>)"
-        using fder' ball_subset_cball r w by (force intro: DERIV_unique [OF fder])
-      have "((\<lambda>u. f u / (u - w)\<^sup>2 / (2 * of_real pi * \<i>)) has_contour_integral
-                contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>))
-                (circlepath z r)"
-        by (rule has_contour_integral_div [OF has_contour_integral_integral [OF intf]])
-      then have "((\<lambda>u. f u / (2 * of_real pi * \<i> * (u - w)\<^sup>2)) has_contour_integral
-                contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>))
-                (circlepath z r)"
-        by (simp add: algebra_simps)
-      then have "((\<lambda>u. f u / (2 * of_real pi * \<i> * (u - w)\<^sup>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: \<open>0 < r\<close> * dist_norm)
-      done
-  qed
-  show ?thesis
-    by (simp add: holomorphic_on_open [OF \<open>open S\<close>] *)
-qed
-
-lemma holomorphic_deriv [holomorphic_intros]:
-    "\<lbrakk>f holomorphic_on S; open S\<rbrakk> \<Longrightarrow> (deriv f) holomorphic_on S"
-by (metis DERIV_deriv_iff_field_differentiable at_within_open derivative_is_holomorphic holomorphic_on_def)
-
-lemma analytic_deriv [analytic_intros]: "f analytic_on S \<Longrightarrow> (deriv f) analytic_on S"
-  using analytic_on_holomorphic holomorphic_deriv by auto
-
-lemma holomorphic_higher_deriv [holomorphic_intros]: "\<lbrakk>f holomorphic_on S; open S\<rbrakk> \<Longrightarrow> (deriv ^^ n) f holomorphic_on S"
-  by (induction n) (auto simp: holomorphic_deriv)
-
-lemma analytic_higher_deriv [analytic_intros]: "f analytic_on S \<Longrightarrow> (deriv ^^ n) f analytic_on S"
-  unfolding analytic_on_def using holomorphic_higher_deriv by blast
-
-lemma has_field_derivative_higher_deriv:
-     "\<lbrakk>f holomorphic_on S; open S; x \<in> S\<rbrakk>
-      \<Longrightarrow> ((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 \<subseteq> S"
-  shows "valid_path (f \<circ> g)"
-proof (rule valid_path_compose[OF \<open>valid_path g\<close>])
-  fix x assume "x \<in> path_image g"
-  then show "f field_differentiable at x"
-    using analytic_on_imp_differentiable_at analytic_on_open assms holo by blast
-next
-  have "deriv f holomorphic_on S"
-    using holomorphic_deriv holo \<open>open S\<close> 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\<open>Morera's theorem\<close>
-
-lemma Morera_local_triangle_ball:
-  assumes "\<And>z. z \<in> S
-          \<Longrightarrow> \<exists>e a. 0 < e \<and> z \<in> ball a e \<and> continuous_on (ball a e) f \<and>
-                    (\<forall>b c. closed_segment b c \<subseteq> ball a e
-                           \<longrightarrow> 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 \<in> S"
-    with assms obtain e a where
-            "0 < e" and z: "z \<in> ball a e" and contf: "continuous_on (ball a e) f"
-        and 0: "\<And>b c. closed_segment b c \<subseteq> ball a e
-                      \<Longrightarrow> 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) \<subseteq> ball a e"
-      by (simp add: dist_commute ball_subset_ball_iff)
-    have "\<exists>e>0. f holomorphic_on ball z e"
-    proof (intro exI conjI)
-      have sub_ball: "\<And>y. dist a y < e \<Longrightarrow> closed_segment a y \<subseteq> ball a e"
-        by (meson \<open>0 < e\<close> centre_in_ball convex_ball convex_contains_segment mem_ball)
-      show "f holomorphic_on ball z (e - dist a z)"
-        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 \<open>0 < e\<close> sub_ball)
-        done
-    qed (simp add: az)
-  }
-  then show ?thesis
-    by (simp add: analytic_on_def)
-qed
-
-lemma Morera_local_triangle:
-  assumes "\<And>z. z \<in> S
-          \<Longrightarrow> \<exists>t. open t \<and> z \<in> t \<and> continuous_on t f \<and>
-                  (\<forall>a b c. convex hull {a,b,c} \<subseteq> t
-                              \<longrightarrow> 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 \<in> S"
-    with assms obtain t where
-            "open t" and z: "z \<in> t" and contf: "continuous_on t f"
-        and 0: "\<And>a b c. convex hull {a,b,c} \<subseteq> t
-                      \<Longrightarrow> 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 \<subseteq> 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 eq0: "\<And>b c. closed_segment b c \<subseteq> ball z e \<Longrightarrow>
-                         contour_integral (linepath z b) f +
-                         contour_integral (linepath b c) f +
-                         contour_integral (linepath c z) f = 0"
-      by (meson 0 z \<open>0 < e\<close> centre_in_ball closed_segment_subset convex_ball dual_order.trans e starlike_convex_subset)
-    have "\<exists>e a. 0 < e \<and> z \<in> ball a e \<and> continuous_on (ball a e) f \<and>
-                (\<forall>b c. closed_segment b c \<subseteq> ball a e \<longrightarrow>
-                       contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f = 0)"
-      using \<open>e > 0\<close> eq0 by force
-  }
-  then show ?thesis
-    by (simp add: Morera_local_triangle_ball)
-qed
-
-proposition Morera_triangle:
-    "\<lbrakk>continuous_on S f; open S;
-      \<And>a b c. convex hull {a,b,c} \<subseteq> S
-              \<longrightarrow> contour_integral (linepath a b) f +
-                  contour_integral (linepath b c) f +
-                  contour_integral (linepath c a) f = 0\<rbrakk>
-     \<Longrightarrow> f analytic_on S"
-  using Morera_local_triangle by blast
-
-subsection\<open>Combining theorems for higher derivatives including Leibniz rule\<close>
-
-lemma higher_deriv_linear [simp]:
-    "(deriv ^^ n) (\<lambda>w. c*w) = (\<lambda>z. if n = 0 then c*z else if n = 1 then c else 0)"
-  by (induction n) auto
-
-lemma higher_deriv_const [simp]: "(deriv ^^ n) (\<lambda>w. c) = (\<lambda>w. if n=0 then c else 0)"
-  by (induction n) auto
-
-lemma higher_deriv_ident [simp]:
-     "(deriv ^^ n) (\<lambda>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
-
-lemma 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:
-     "\<lbrakk>(f has_field_derivative 1) (at z); f z = z\<rbrakk> \<Longrightarrow> (f^^n has_field_derivative 1) (at z)"
-  apply (induction n, auto)
-  apply (simp add: id_def)
-  by (metis DERIV_chain comp_funpow comp_id funpow_swap1 mult.right_neutral)
-
-lemma higher_deriv_uminus:
-  assumes "f holomorphic_on S" "open S" and z: "z \<in> S"
-    shows "(deriv ^^ n) (\<lambda>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
-  have "((deriv ^^ n) (\<lambda>w. - f w) has_field_derivative - deriv ((deriv ^^ n) f) z) (at z)"
-    apply (rule has_field_derivative_transform_within_open [of "\<lambda>w. -((deriv ^^ n) f w)"])
-       apply (rule derivative_eq_intros | rule * refl assms)+
-     apply (auto simp add: Suc)
-    done
-  then show ?case
-    by (simp add: DERIV_imp_deriv)
-qed
-
-lemma higher_deriv_add:
-  fixes z::complex
-  assumes "f holomorphic_on S" "g holomorphic_on S" "open S" and z: "z \<in> S"
-    shows "(deriv ^^ n) (\<lambda>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
-  have "((deriv ^^ n) (\<lambda>w. f w + g w) has_field_derivative
-        deriv ((deriv ^^ n) f) z + deriv ((deriv ^^ n) g) z) (at z)"
-    apply (rule has_field_derivative_transform_within_open [of "\<lambda>w. (deriv ^^ n) f w + (deriv ^^ n) g w"])
-       apply (rule derivative_eq_intros | rule * refl assms)+
-     apply (auto simp add: Suc)
-    done
-  then show ?case
-    by (simp add: DERIV_imp_deriv)
-qed
-
-lemma higher_deriv_diff:
-  fixes z::complex
-  assumes "f holomorphic_on S" "g holomorphic_on S" "open S" and z: "z \<in> S"
-    shows "(deriv ^^ n) (\<lambda>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 (cases k) simp_all
-
-lemma higher_deriv_mult:
-  fixes z::complex
-  assumes "f holomorphic_on S" "g holomorphic_on S" "open S" and z: "z \<in> S"
-    shows "(deriv ^^ n) (\<lambda>w. f w * g w) z =
-           (\<Sum>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 *: "\<And>n. ((deriv ^^ n) f has_field_derivative deriv ((deriv ^^ n) f) z) (at z)"
-          "\<And>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: "(\<Sum>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 + (\<Sum>i = 0..n. (deriv ^^ i) f z * (of_nat (Suc n choose i) * (deriv ^^ (Suc n - i)) g z))"
-    apply (simp add: bb algebra_simps sum.distrib)
-    apply (subst (4) sum_Suc_reindex)
-    apply (auto simp: algebra_simps Suc_diff_le intro: sum.cong)
-    done
-  have "((deriv ^^ n) (\<lambda>w. f w * g w) has_field_derivative
-         (\<Sum>i = 0..Suc n. (Suc n choose i) * (deriv ^^ i) f z * (deriv ^^ (Suc n - i)) g z))
-        (at z)"
-    apply (rule has_field_derivative_transform_within_open
-        [of "\<lambda>w. (\<Sum>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_sum])
-        apply (rule DERIV_cmult)
-        apply (auto intro: DERIV_mult * sumeq \<open>open S\<close> Suc.prems Suc.IH [symmetric])
-    done
-  then show ?case
-    unfolding funpow.simps o_apply
-    by (simp add: DERIV_imp_deriv)
-qed
-
-lemma higher_deriv_transform_within_open:
-  fixes z::complex
-  assumes "f holomorphic_on S" "g holomorphic_on S" "open S" and z: "z \<in> S"
-      and fg: "\<And>w. w \<in> S \<Longrightarrow> 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)
-
-lemma higher_deriv_compose_linear:
-  fixes z::complex
-  assumes f: "f holomorphic_on T" and S: "open S" and T: "open T" and z: "z \<in> S"
-      and fg: "\<And>w. w \<in> S \<Longrightarrow> u * w \<in> T"
-    shows "(deriv ^^ n) (\<lambda>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 (*) u ` S"
-    by (meson fg f holomorphic_on_subset image_subset_iff)
-  have holo2: "(deriv ^^ n) f holomorphic_on (*) u ` S"
-    by (meson f fg holomorphic_higher_deriv holomorphic_on_subset image_subset_iff T)
-  have holo3: "(\<lambda>z. u ^ n * (deriv ^^ n) f (u * z)) holomorphic_on S"
-    by (intro holo2 holomorphic_on_compose [where g="(deriv ^^ n) f", unfolded o_def] holomorphic_intros)
-  have holo1: "(\<lambda>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 "deriv ((deriv ^^ n) (\<lambda>w. f (u * w))) z = deriv (\<lambda>z. u^n * (deriv ^^ n) f (u*z)) z"
-    apply (rule complex_derivative_transform_within_open [OF _ holo3 S Suc.prems])
-    apply (rule holomorphic_higher_deriv [OF holo1 S])
-    apply (simp add: Suc.IH)
-    done
-  also have "\<dots> = u^n * deriv (\<lambda>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)
-     apply (simp add: analytic_on_open f holomorphic_higher_deriv T)
-    apply (blast intro: fg)
-    done
-  also have "\<dots> = u * u ^ n * deriv ((deriv ^^ n) f) (u * z)"
-      apply (subst complex_derivative_chain [where g = "(deriv ^^ n) f" and f = "(*) 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)
-      done
-  finally show ?case
-    by simp
-qed
-
-lemma higher_deriv_add_at:
-  assumes "f analytic_on {z}" "g analytic_on {z}"
-    shows "(deriv ^^ n) (\<lambda>w. f w + g w) z = (deriv ^^ n) f z + (deriv ^^ n) g z"
-proof -
-  have "f analytic_on {z} \<and> 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) (\<lambda>w. f w - g w) z = (deriv ^^ n) f z - (deriv ^^ n) g z"
-proof -
-  have "f analytic_on {z} \<and> 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}  \<Longrightarrow> (deriv ^^ n) (\<lambda>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) (\<lambda>w. f w * g w) z =
-           (\<Sum>i = 0..n. of_nat (n choose i) * (deriv ^^ i) f z * (deriv ^^ (n - i)) g z)"
-proof -
-  have "f analytic_on {z} \<and> g analytic_on {z}"
-    using assms by blast
-  with higher_deriv_mult show ?thesis
-    by (auto simp: analytic_at_two)
-qed
-
-
-text\<open> Nonexistence of isolated singularities and a stronger integral formula.\<close>
-
-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 \<in> S" and cdf: "\<And>x. x \<in> S - K \<Longrightarrow> f field_differentiable at x"
-    have "f field_differentiable at z"
-    proof (cases "z \<in> K")
-      case False then show ?thesis by (blast intro: cdf \<open>z \<in> S\<close>)
-    next
-      case True
-      with finite_set_avoid [OF K, of z]
-      obtain d where "d>0" and d: "\<And>x. \<lbrakk>x\<in>K; x \<noteq> z\<rbrakk> \<Longrightarrow> d \<le> dist z x"
-        by blast
-      obtain e where "e>0" and e: "ball z e \<subseteq> S"
-        using  S \<open>z \<in> S\<close> by (force simp: 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 cont: "{a,b,c} \<subseteq> ball z (min d e) \<Longrightarrow> continuous_on (convex hull {a, b, c}) f" for a b c
-        by (simp add: hull_minimal continuous_on_subset [OF fde])
-      have fd: "\<lbrakk>{a,b,c} \<subseteq> ball z (min d e); x \<in> interior (convex hull {a, b, c}) - K\<rbrakk>
-            \<Longrightarrow> f field_differentiable at x" for a b c x
-        by (metis cdf Diff_iff Int_iff ball_min_Int subsetD convex_ball e interior_mono interior_subset subset_hull)
-      obtain g where "\<And>w. w \<in> ball z (min d e) \<Longrightarrow> (g has_field_derivative f w) (at w within ball z (min d e))"
-        apply (rule contour_integral_convex_primitive
-                     [OF convex_ball fde Cauchy_theorem_triangle_cofinite [OF _ K]])
-        using cont fd by auto
-      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 \<open>0 < d\<close> \<open>0 < e\<close> 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
-
-lemma no_isolated_singularity':
-  fixes z::complex
-  assumes f: "\<And>z. z \<in> K \<Longrightarrow> (f \<longlongrightarrow> f z) (at z within S)"
-      and holf: "f holomorphic_on (S - K)" and S: "open S" and K: "finite K"
-    shows "f holomorphic_on S"
-proof (rule no_isolated_singularity[OF _ assms(2-)])
-  show "continuous_on S f" unfolding continuous_on_def
-  proof
-    fix z assume z: "z \<in> S"
-    show "(f \<longlongrightarrow> f z) (at z within S)"
-    proof (cases "z \<in> K")
-      case False
-      from holf have "continuous_on (S - K) f"
-        by (rule holomorphic_on_imp_continuous_on)
-      with z False have "(f \<longlongrightarrow> f z) (at z within (S - K))"
-        by (simp add: continuous_on_def)
-      also from z K S False have "at z within (S - K) = at z within S"
-        by (subst (1 2) at_within_open) (auto intro: finite_imp_closed)
-      finally show "(f \<longlongrightarrow> f z) (at z within S)" .
-    qed (insert assms z, simp_all)
-  qed
-qed
-
-proposition Cauchy_integral_formula_convex:
-  assumes S: "convex S" and K: "finite K" and contf: "continuous_on S f"
-    and fcd: "(\<And>x. x \<in> interior S - K \<Longrightarrow> f field_differentiable at x)"
-    and z: "z \<in> interior S" and vpg: "valid_path \<gamma>"
-    and pasz: "path_image \<gamma> \<subseteq> S - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
-  shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
-proof -
-  have *: "\<And>x. x \<in> interior S \<Longrightarrow> f field_differentiable at x"
-    unfolding holomorphic_on_open [symmetric] field_differentiable_def
-    using no_isolated_singularity [where S = "interior S"]
-    by (meson K contf continuous_at_imp_continuous_on continuous_on_interior fcd
-          field_differentiable_at_within field_differentiable_def holomorphic_onI
-          holomorphic_on_imp_differentiable_at open_interior)
-  show ?thesis
-    by (rule Cauchy_integral_formula_weak [OF S finite.emptyI contf]) (use * assms in auto)
-qed
-
-text\<open> Formula for higher derivatives.\<close>
-
-lemma 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 \<in> ball z r"
-    shows "((\<lambda>u. f u / (u - w) ^ (Suc k)) has_contour_integral ((2 * pi * \<i>) / (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: cball_def sphere_def less_imp_le)
-  obtain X where X: "((\<lambda>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) ((\<lambda>u. f u / (u - w) ^ Suc (Suc k))) = X"
-    by (rule contour_integral_unique)
-  have "\<And>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: "\<And>n. (deriv ^^ n) f field_differentiable (at w)"
-    by (force simp: field_differentiable_def)
-  have "deriv (\<lambda>w. complex_of_real (2 * pi) * \<i> / (fact k) * (deriv ^^ k) f w) w =
-          of_nat (Suc k) * contour_integral (circlepath z r) (\<lambda>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 "\<dots> = of_nat (Suc k) * X"
-    by (simp only: con)
-  finally have "deriv (\<lambda>w. ((2 * pi) * \<i> / (fact k)) * (deriv ^^ k) f w) w = of_nat (Suc k) * X" .
-  then have "((2 * pi) * \<i> / (fact k)) * deriv (\<lambda>w. (deriv ^^ k) f w) w = of_nat (Suc k) * X"
-    by (metis deriv_cmult dnf_diff)
-  then have "deriv (\<lambda>w. (deriv ^^ k) f w) w = of_nat (Suc k) * X / ((2 * pi) * \<i> / (fact k))"
-    by (simp add: field_simps)
-  then show ?case
-  using of_nat_eq_0_iff X by fastforce
-qed
-
-lemma Cauchy_higher_derivative_integral_circlepath:
-  assumes contf: "continuous_on (cball z r) f"
-      and holf: "f holomorphic_on ball z r"
-      and w: "w \<in> ball z r"
-    shows "(\<lambda>u. f u / (u - w)^(Suc k)) contour_integrable_on (circlepath z r)"
-           (is "?thes1")
-      and "(deriv ^^ k) f w = (fact k) / (2 * pi * \<i>) * contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k))"
-           (is "?thes2")
-proof -
-  have *: "((\<lambda>u. f u / (u - w) ^ Suc k) has_contour_integral (2 * pi) * \<i> / (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: field_split_simps)
-qed
-
-corollary Cauchy_contour_integral_circlepath:
-  assumes "continuous_on (cball z r) f" "f holomorphic_on ball z r" "w \<in> ball z r"
-    shows "contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k)) = (2 * pi * \<i>) * (deriv ^^ k) f w / (fact k)"
-by (simp add: Cauchy_higher_derivative_integral_circlepath [OF assms])
-
-lemma Cauchy_contour_integral_circlepath_2:
-  assumes "continuous_on (cball z r) f" "f holomorphic_on ball z r" "w \<in> ball z r"
-    shows "contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^2) = (2 * pi * \<i>) * deriv f w"
-  using Cauchy_contour_integral_circlepath [OF assms, of 1]
-  by (simp add: power2_eq_square)
-
-
-subsection\<open>A holomorphic function is analytic, i.e. has local power series\<close>
-
-theorem holomorphic_power_series:
-  assumes holf: "f holomorphic_on ball z r"
-      and w: "w \<in> ball z r"
-    shows "((\<lambda>n. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
-proof -
-  \<comment> \<open>Replacing \<^term>\<open>r\<close> and the original (weak) premises with stronger ones\<close>
-  obtain r where "r > 0" and holfc: "f holomorphic_on cball z r" and w: "w \<in> ball z r"
-  proof
-    have "cball z ((r + dist w z) / 2) \<subseteq> ball z r"
-      using w by (simp add: dist_commute field_sum_of_halves subset_eq)
-    then show "f holomorphic_on cball z ((r + dist w z) / 2)"
-      by (rule holomorphic_on_subset [OF holf])
-    have "r > 0"
-      using w by clarsimp (metis dist_norm le_less_trans norm_ge_zero)
-    then show "0 < (r + dist w z) / 2"
-      by simp (use zero_le_dist [of w z] in linarith)
-  qed (use w in \<open>auto simp: dist_commute\<close>)
-  then have holf: "f holomorphic_on ball z r"
-    using ball_subset_cball holomorphic_on_subset by blast
-  have contf: "continuous_on (cball z r) f"
-    by (simp add: holfc holomorphic_on_imp_continuous_on)
-  have cint: "\<And>k. (\<lambda>u. f u / (u - z) ^ Suc k) contour_integrable_on circlepath z r"
-    by (rule Cauchy_higher_derivative_integral_circlepath [OF contf holf]) (simp add: \<open>0 < r\<close>)
-  obtain B where "0 < B" and B: "\<And>u. u \<in> cball z r \<Longrightarrow> norm(f u) \<le> 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 \<le> r" and wz_eq: "norm(w - z) = r - k"
-             and kle: "\<And>u. norm(u - z) = r \<Longrightarrow> k \<le> norm(u - w)"
-  proof
-    show "\<And>u. cmod (u - z) = r \<Longrightarrow> r - dist z w \<le> cmod (u - w)"
-      by (metis add_diff_eq diff_add_cancel dist_norm norm_diff_ineq)
-  qed (use w in \<open>auto simp: dist_norm norm_minus_commute\<close>)
-  have ul: "uniform_limit (sphere z r) (\<lambda>n x. (\<Sum>k<n. (w - z) ^ k * (f x / (x - z) ^ Suc k))) (\<lambda>x. f x / (x - w)) sequentially"
-    unfolding uniform_limit_iff dist_norm
-  proof clarify
-    fix e::real
-    assume "0 < e"
-    have rr: "0 \<le> (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"] \<open>0 < e\<close> \<open>0 < B\<close> k by force
-    have "norm ((\<Sum>k<N. (w - z) ^ k * f u / (u - z) ^ Suc k) - f u / (u - w)) < e"
-         if "n \<le> 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  \<open>n \<le> N\<close> k by auto
-      have u [simp]: "(u \<noteq> z) \<and> (u \<noteq> w)"
-        using \<open>0 < r\<close> r w by auto
-      have wzu_not1: "(w - z) / (u - z) \<noteq> 1"
-        by (metis (no_types) dist_norm divide_eq_1_iff less_irrefl mem_ball norm_minus_commute r w)
-      have "norm ((\<Sum>k<N. (w - z) ^ k * f u / (u - z) ^ Suc k) * (u - w) - f u)
-            = norm ((\<Sum>k<N. (((w - z) / (u - z)) ^ k)) * f u * (u - w) / (u - z) - f u)"
-        unfolding sum_distrib_right sum_divide_distrib power_divide by (simp add: algebra_simps)
-      also have "\<dots> = norm ((((w - z) / (u - z)) ^ N - 1) * (u - w) / (((w - z) / (u - z) - 1) * (u - z)) - 1) * norm (f u)"
-        using \<open>0 < B\<close>
-        apply (auto simp: geometric_sum [OF wzu_not1])
-        apply (simp add: field_simps norm_mult [symmetric])
-        done
-      also have "\<dots> = norm ((u-z) ^ N * (w - u) - ((w - z) ^ N - (u-z) ^ N) * (u-w)) / (r ^ N * norm (u-w)) * norm (f u)"
-        using \<open>0 < r\<close> r by (simp add: divide_simps norm_mult norm_divide norm_power dist_norm norm_minus_commute)
-      also have "\<dots> = norm ((w - z) ^ N * (w - u)) / (r ^ N * norm (u - w)) * norm (f u)"
-        by (simp add: algebra_simps)
-      also have "\<dots> = norm (w - z) ^ N * norm (f u) / r ^ N"
-        by (simp add: norm_mult norm_power norm_minus_commute)
-      also have "\<dots> \<le> (((r - k)/r)^N) * B"
-        using \<open>0 < r\<close> 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 "\<dots> < e * k"
-        using \<open>0 < B\<close> N by (simp add: divide_simps)
-      also have "\<dots> \<le> e * norm (u - w)"
-        using r kle \<open>0 < e\<close> by (simp add: dist_commute dist_norm)
-      finally show ?thesis
-        by (simp add: field_split_simps norm_divide del: power_Suc)
-    qed
-    with \<open>0 < r\<close> show "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>sphere z r.
-                norm ((\<Sum>k<n. (w - z) ^ k * (f x / (x - z) ^ Suc k)) - f x / (x - w)) < e"
-      by (auto simp: mult_ac less_imp_le eventually_sequentially Ball_def)
-  qed
-  have eq: "\<forall>\<^sub>F x in sequentially.
-             contour_integral (circlepath z r) (\<lambda>u. \<Sum>k<x. (w - z) ^ k * (f u / (u - z) ^ Suc k)) =
-             (\<Sum>k<x. contour_integral (circlepath z r) (\<lambda>u. f u / (u - z) ^ Suc k) * (w - z) ^ k)"
-    apply (rule eventuallyI)
-    apply (subst contour_integral_sum, 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 cic: "\<And>u. (\<lambda>y. \<Sum>k<u. (w - z) ^ k * (f y / (y - z) ^ Suc k)) contour_integrable_on circlepath z r"
-    apply (intro contour_integrable_sum contour_integrable_lmul, simp)
-    using \<open>0 < r\<close> by (force intro!: Cauchy_higher_derivative_integral_circlepath [OF contf holf])
-  have "(\<lambda>k. contour_integral (circlepath z r) (\<lambda>u. f u/(u - z)^(Suc k)) * (w - z)^k)
-        sums contour_integral (circlepath z r) (\<lambda>u. f u/(u - w))"
-    unfolding sums_def
-    apply (intro Lim_transform_eventually [OF _ eq] contour_integral_uniform_limit_circlepath [OF eventuallyI ul] cic)
-    using \<open>0 < r\<close> apply auto
-    done
-  then have "(\<lambda>k. contour_integral (circlepath z r) (\<lambda>u. f u/(u - z)^(Suc k)) * (w - z)^k)
-             sums (2 * of_real pi * \<i> * f w)"
-    using w by (auto simp: dist_commute dist_norm contour_integral_unique [OF Cauchy_integral_circlepath_simple [OF holfc]])
-  then have "(\<lambda>k. contour_integral (circlepath z r) (\<lambda>u. f u / (u - z) ^ Suc k) * (w - z)^k / (\<i> * (of_real pi * 2)))
-            sums ((2 * of_real pi * \<i> * f w) / (\<i> * (complex_of_real pi * 2)))"
-    by (rule sums_divide)
-  then have "(\<lambda>n. (w - z) ^ n * contour_integral (circlepath z r) (\<lambda>u. f u / (u - z) ^ Suc n) / (\<i> * (of_real pi * 2)))
-            sums f w"
-    by (simp add: field_simps)
-  then show ?thesis
-    by (simp add: field_simps \<open>0 < r\<close> Cauchy_higher_derivative_integral_circlepath [OF contf holf])
-qed
-
-
-subsection\<open>The Liouville theorem and the Fundamental Theorem of Algebra\<close>
-
-text\<open> These weak Liouville versions don't even need the derivative formula.\<close>
-
-lemma Liouville_weak_0:
-  assumes holf: "f holomorphic_on UNIV" and inf: "(f \<longlongrightarrow> 0) at_infinity"
-    shows "f z = 0"
-proof (rule ccontr)
-  assume fz: "f z \<noteq> 0"
-  with inf [unfolded Lim_at_infinity, rule_format, of "norm(f z)/2"]
-  obtain B where B: "\<And>x. B \<le> cmod x \<Longrightarrow> norm (f x) * 2 < cmod (f z)"
-    by (auto simp: dist_norm)
-  define R where "R = 1 + \<bar>B\<bar> + norm z"
-  have "R > 0" unfolding R_def
-  proof -
-    have "0 \<le> cmod z + \<bar>B\<bar>"
-      by (metis (full_types) add_nonneg_nonneg norm_ge_zero real_norm_def)
-    then show "0 < 1 + \<bar>B\<bar> + cmod z"
-      by linarith
-  qed
-  have *: "((\<lambda>u. f u / (u - z)) has_contour_integral 2 * complex_of_real pi * \<i> * f z) (circlepath z R)"
-    apply (rule Cauchy_integral_circlepath)
-    using \<open>R > 0\<close> apply (auto intro: holomorphic_on_subset [OF holf] holomorphic_on_imp_continuous_on)+
-    done
-  have "cmod (x - z) = R \<Longrightarrow> cmod (f x) * 2 < cmod (f z)" for x
-    unfolding R_def
-    by (rule B) (use norm_triangle_ineq4 [of x z] in auto)
-  with \<open>R > 0\<close> fz show False
-    using has_contour_integral_bound_circlepath [OF *, of "norm(f z)/2/R"]
-    by (auto simp: less_imp_le norm_mult norm_divide field_split_simps)
-qed
-
-proposition Liouville_weak:
-  assumes "f holomorphic_on UNIV" and "(f \<longlongrightarrow> l) at_infinity"
-    shows "f z = l"
-  using Liouville_weak_0 [of "\<lambda>z. f z - l"]
-  by (simp add: assms holomorphic_on_diff LIM_zero)
-
-proposition Liouville_weak_inverse:
-  assumes "f holomorphic_on UNIV" and unbounded: "\<And>B. eventually (\<lambda>x. norm (f x) \<ge> B) at_infinity"
-    obtains z where "f z = 0"
-proof -
-  { assume f: "\<And>z. f z \<noteq> 0"
-    have 1: "(\<lambda>x. 1 / f x) holomorphic_on UNIV"
-      by (simp add: holomorphic_on_divide assms f)
-    have 2: "((\<lambda>x. 1 / f x) \<longlongrightarrow> 0) at_infinity"
-      apply (rule tendstoI [OF eventually_mono])
-      apply (rule_tac B="2/e" in unbounded)
-      apply (simp add: dist_norm norm_divide field_split_simps)
-      done
-    have False
-      using Liouville_weak_0 [OF 1 2] f by simp
-  }
-  then show ?thesis
-    using that by blast
-qed
-
-text\<open> In particular we get the Fundamental Theorem of Algebra.\<close>
-
-theorem fundamental_theorem_of_algebra:
-    fixes a :: "nat \<Rightarrow> complex"
-  assumes "a 0 = 0 \<or> (\<exists>i \<in> {1..n}. a i \<noteq> 0)"
-  obtains z where "(\<Sum>i\<le>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 \<in> {1..n}" and nz: "a i \<noteq> 0"
-  have 1: "(\<lambda>z. \<Sum>i\<le>n. a i * z^i) holomorphic_on UNIV"
-    by (rule holomorphic_intros)+
-  show thesis
-  proof (rule Liouville_weak_inverse [OF 1])
-    show "\<forall>\<^sub>F x in at_infinity. B \<le> cmod (\<Sum>i\<le>n. a i * x ^ i)" for B
-      using i polyfun_extremal nz by force
-  qed (use that in auto)
-qed
-
-subsection\<open>Weierstrass convergence theorem\<close>
-
-lemma holomorphic_uniform_limit:
-  assumes cont: "eventually (\<lambda>n. continuous_on (cball z r) (f n) \<and> (f n) holomorphic_on ball z r) F"
-      and ulim: "uniform_limit (cball z r) f g F"
-      and F:  "\<not> 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: ball_empty less_imp_le continuous_on_def holomorphic_on_def intro: that)
-next
-  case equal then show ?thesis
-    by (force simp: holomorphic_on_def intro: that)
-next
-  case greater
-  have contg: "continuous_on (cball z r) g"
-    using cont uniform_limit_theorem [OF eventually_mono ulim F]  by blast
-  have "path_image (circlepath z r) \<subseteq> cball z r"
-    using \<open>0 < r\<close> by auto
-  then have 1: "continuous_on (path_image (circlepath z r)) (\<lambda>x. 1 / (2 * complex_of_real pi * \<i>) * g x)"
-    by (intro continuous_intros continuous_on_subset [OF contg])
-  have 2: "((\<lambda>u. 1 / (2 * of_real pi * \<i>) * g u / (u - w) ^ 1) has_contour_integral g w) (circlepath z r)"
-       if w: "w \<in> ball z r" for w
-  proof -
-    define d where "d = (r - norm(w - z))"
-    have "0 < d"  "d \<le> r" using w by (auto simp: norm_minus_commute d_def dist_norm)
-    have dle: "\<And>u. cmod (z - u) = r \<Longrightarrow> d \<le> cmod (u - w)"
-      unfolding d_def by (metis add_diff_eq diff_add_cancel norm_diff_ineq norm_minus_commute)
-    have ev_int: "\<forall>\<^sub>F n in F. (\<lambda>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 ul_less: "uniform_limit (sphere z r) (\<lambda>n x. f n x / (x - w)) (\<lambda>x. g x / (x - w)) F"
-      using greater \<open>0 < d\<close>
-      apply (clarsimp simp add: uniform_limit_iff dist_norm norm_divide diff_divide_distrib [symmetric] divide_simps)
-      apply (rule_tac e1="e * d" in eventually_mono [OF uniform_limitD [OF ulim]])
-       apply (force simp: dist_norm intro: dle mult_left_mono less_le_trans)+
-      done
-    have g_cint: "(\<lambda>u. g u/(u - w)) contour_integrable_on circlepath z r"
-      by (rule contour_integral_uniform_limit_circlepath [OF ev_int ul_less F \<open>0 < r\<close>])
-    have cif_tends_cig: "((\<lambda>n. contour_integral(circlepath z r) (\<lambda>u. f n u / (u - w))) \<longlongrightarrow> contour_integral(circlepath z r) (\<lambda>u. g u/(u - w))) F"
-      by (rule contour_integral_uniform_limit_circlepath [OF ev_int ul_less F \<open>0 < r\<close>])
-    have f_tends_cig: "((\<lambda>n. 2 * of_real pi * \<i> * f n w) \<longlongrightarrow> contour_integral (circlepath z r) (\<lambda>u. g u / (u - w))) F"
-    proof (rule Lim_transform_eventually)
-      show "\<forall>\<^sub>F x in F. contour_integral (circlepath z r) (\<lambda>u. f x u / (u - w))
-                     = 2 * of_real pi * \<i> * f x w"
-        apply (rule eventually_mono [OF cont contour_integral_unique [OF Cauchy_integral_circlepath]])
-        using w\<open>0 < d\<close> d_def by auto
-    qed (auto simp: cif_tends_cig)
-    have "\<And>e. 0 < e \<Longrightarrow> \<forall>\<^sub>F n in F. dist (f n w) (g w) < e"
-      by (rule eventually_mono [OF uniform_limitD [OF ulim]]) (use w in auto)
-    then have "((\<lambda>n. 2 * of_real pi * \<i> * f n w) \<longlongrightarrow> 2 * of_real pi * \<i> * g w) F"
-      by (rule tendsto_mult_left [OF tendstoI])
-    then have "((\<lambda>u. g u / (u - w)) has_contour_integral 2 * of_real pi * \<i> * g w) (circlepath z r)"
-      using has_contour_integral_integral [OF g_cint] tendsto_unique [OF F f_tends_cig] w
-      by fastforce
-    then have "((\<lambda>u. g u / (2 * of_real pi * \<i> * (u - w))) has_contour_integral g w) (circlepath z r)"
-      using has_contour_integral_div [where c = "2 * of_real pi * \<i>"]
-      by (force simp: 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\<open> Version showing that the limit is the limit of the derivatives.\<close>
-
-proposition has_complex_derivative_uniform_limit:
-  fixes z::complex
-  assumes cont: "eventually (\<lambda>n. continuous_on (cball z r) (f n) \<and>
-                               (\<forall>w \<in> ball z r. ((f n) has_field_derivative (f' n w)) (at w))) F"
-      and ulim: "uniform_limit (cball z r) f g F"
-      and F:  "\<not> trivial_limit F" and "0 < r"
-  obtains g' where
-      "continuous_on (cball z r) g"
-      "\<And>w. w \<in> ball z r \<Longrightarrow> (g has_field_derivative (g' w)) (at w) \<and> ((\<lambda>n. f' n w) \<longlongrightarrow> 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] ulim F];
-             auto simp: holomorphic_on_open field_differentiable_def)+
-  then obtain g' where g': "\<And>x. x \<in> ball z r \<Longrightarrow> (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: "\<And>x. x \<in> ball z r \<Longrightarrow> deriv g x = g' x"
-    by (simp add: DERIV_imp_deriv)
-  have tends_f'n_g': "((\<lambda>n. f' n w) \<longlongrightarrow> g' w) F" if w: "w \<in> ball z r" for w
-  proof -
-    have eq_f': "?conint (\<lambda>x. f n x / (x - w)\<^sup>2) - ?conint (\<lambda>x. g x / (x - w)\<^sup>2) = (f' n w - g' w) * (2 * of_real pi * \<i>)"
-             if cont_fn: "continuous_on (cball z r) (f n)"
-             and fnd: "\<And>w. w \<in> ball z r \<Longrightarrow> (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: holomorphic_on_open)
-      have "(f n has_field_derivative 1 / (2 * of_real pi * \<i>) * ?conint (\<lambda>u. f n u / (u - w)\<^sup>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 * \<i>) * ?conint (\<lambda>u. f n u / (u - w)\<^sup>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] field_split_simps)
-    qed
-    define d where "d = (r - norm(w - z))^2"
-    have "d > 0"
-      using w by (simp add: dist_commute dist_norm d_def)
-    have dle: "d \<le> cmod ((y - w)\<^sup>2)" if "r = cmod (z - y)" for y
-    proof -
-      have "w \<in> ball z (cmod (z - y))"
-        using that w by fastforce
-      then have "cmod (w - z) \<le> cmod (z - y)"
-        by (simp add: dist_complex_def norm_minus_commute)
-      moreover have "cmod (z - y) - cmod (w - z) \<le> cmod (y - w)"
-        by (metis diff_add_cancel diff_add_eq_diff_diff_swap norm_minus_commute norm_triangle_ineq2)
-      ultimately show ?thesis
-        using that by (simp add: d_def norm_power power_mono)
-    qed
-    have 1: "\<forall>\<^sub>F n in F. (\<lambda>x. f n x / (x - w)\<^sup>2) contour_integrable_on circlepath z r"
-      by (force simp: holomorphic_on_open intro: w Cauchy_derivative_integral_circlepath eventually_mono [OF cont])
-    have 2: "uniform_limit (sphere z r) (\<lambda>n x. f n x / (x - w)\<^sup>2) (\<lambda>x. g x / (x - w)\<^sup>2) F"
-      unfolding uniform_limit_iff
-    proof clarify
-      fix e::real
-      assume "0 < e"
-      with \<open>r > 0\<close> show "\<forall>\<^sub>F n in F. \<forall>x\<in>sphere z r. dist (f n x / (x - w)\<^sup>2) (g x / (x - w)\<^sup>2) < e"
-        apply (simp add: norm_divide field_split_simps sphere_def dist_norm)
-        apply (rule eventually_mono [OF uniform_limitD [OF ulim], of "e*d"])
-         apply (simp add: \<open>0 < d\<close>)
-        apply (force simp: dist_norm dle intro: less_le_trans)
-        done
-    qed
-    have "((\<lambda>n. contour_integral (circlepath z r) (\<lambda>x. f n x / (x - w)\<^sup>2))
-             \<longlongrightarrow> contour_integral (circlepath z r) ((\<lambda>x. g x / (x - w)\<^sup>2))) F"
-      by (rule contour_integral_uniform_limit_circlepath [OF 1 2 F \<open>0 < r\<close>])
-    then have tendsto_0: "((\<lambda>n. 1 / (2 * of_real pi * \<i>) * (?conint (\<lambda>x. f n x / (x - w)\<^sup>2) - ?conint (\<lambda>x. g x / (x - w)\<^sup>2))) \<longlongrightarrow> 0) F"
-      using Lim_null by (force intro!: tendsto_mult_right_zero)
-    have "((\<lambda>n. f' n w - g' w) \<longlongrightarrow> 0) F"
-      apply (rule Lim_transform_eventually [OF tendsto_0])
-      apply (force simp: divide_simps intro: eq_f' eventually_mono [OF cont])
-      done
-    then show ?thesis using Lim_null by blast
-  qed
-  obtain g' where "\<And>w. w \<in> ball z r \<Longrightarrow> (g has_field_derivative (g' w)) (at w) \<and> ((\<lambda>n. f' n w) \<longlongrightarrow> g' w) F"
-      by (blast intro: tends_f'n_g' g')
-  then show ?thesis using g
-    using that by blast
-qed
-
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Some more simple/convenient versions for applications\<close>
-
-lemma holomorphic_uniform_sequence:
-  assumes S: "open S"
-      and hol_fn: "\<And>n. (f n) holomorphic_on S"
-      and ulim_g: "\<And>x. x \<in> S \<Longrightarrow> \<exists>d. 0 < d \<and> cball x d \<subseteq> S \<and> uniform_limit (cball x d) f g sequentially"
-  shows "g holomorphic_on S"
-proof -
-  have "\<exists>f'. (g has_field_derivative f') (at z)" if "z \<in> S" for z
-  proof -
-    obtain r where "0 < r" and r: "cball z r \<subseteq> S"
-               and ul: "uniform_limit (cball z r) f g sequentially"
-      using ulim_g [OF \<open>z \<in> S\<close>] by blast
-    have *: "\<forall>\<^sub>F n in sequentially. continuous_on (cball z r) (f n) \<and> f n holomorphic_on ball z r"
-    proof (intro eventuallyI conjI)
-      show "continuous_on (cball z r) (f x)" for x
-        using hol_fn holomorphic_on_imp_continuous_on holomorphic_on_subset r by blast
-      show "f x holomorphic_on ball z r" for x
-        by (metis hol_fn holomorphic_on_subset interior_cball interior_subset r)
-    qed
-    show ?thesis
-      apply (rule holomorphic_uniform_limit [OF *])
-      using \<open>0 < r\<close> centre_in_ball ul
-      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: "\<And>n x. x \<in> S \<Longrightarrow> ((f n) has_field_derivative f' n x) (at x)"
-      and ulim_g: "\<And>x. x \<in> S
-             \<Longrightarrow> \<exists>d. 0 < d \<and> cball x d \<subseteq> S \<and> uniform_limit (cball x d) f g sequentially"
-  shows "\<exists>g'. \<forall>x \<in> S. (g has_field_derivative g' x) (at x) \<and> ((\<lambda>n. f' n x) \<longlongrightarrow> g' x) sequentially"
-proof -
-  have y: "\<exists>y. (g has_field_derivative y) (at z) \<and> (\<lambda>n. f' n z) \<longlonglongrightarrow> y" if "z \<in> S" for z
-  proof -
-    obtain r where "0 < r" and r: "cball z r \<subseteq> S"
-               and ul: "uniform_limit (cball z r) f g sequentially"
-      using ulim_g [OF \<open>z \<in> S\<close>] by blast
-    have *: "\<forall>\<^sub>F n in sequentially. continuous_on (cball z r) (f n) \<and>
-                                   (\<forall>w \<in> ball z r. ((f n) has_field_derivative (f' n w)) (at w))"
-    proof (intro eventuallyI conjI ballI)
-      show "continuous_on (cball z r) (f x)" for x
-        by (meson S continuous_on_subset hfd holomorphic_on_imp_continuous_on holomorphic_on_open r)
-      show "w \<in> ball z r \<Longrightarrow> (f x has_field_derivative f' x w) (at w)" for w x
-        using ball_subset_cball hfd r by blast
-    qed
-    show ?thesis
-      by (rule has_complex_derivative_uniform_limit [OF *, of g]) (use \<open>0 < r\<close> ul in \<open>force+\<close>)
-  qed
-  show ?thesis
-    by (rule bchoice) (blast intro: y)
-qed
-
-subsection\<open>On analytic functions defined by a series\<close>
-
-lemma series_and_derivative_comparison:
-  fixes S :: "complex set"
-  assumes S: "open S"
-      and h: "summable h"
-      and hfd: "\<And>n x. x \<in> S \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
-      and to_g: "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>S. norm (f n x) \<le> h n"
-  obtains g g' where "\<forall>x \<in> S. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
-proof -
-  obtain g where g: "uniform_limit S (\<lambda>n x. \<Sum>i<n. f i x) g sequentially"
-    using Weierstrass_m_test_ev [OF to_g h]  by force
-  have *: "\<exists>d>0. cball x d \<subseteq> S \<and> uniform_limit (cball x d) (\<lambda>n x. \<Sum>i<n. f i x) g sequentially"
-         if "x \<in> S" for x
-  proof -
-    obtain d where "d>0" and d: "cball x d \<subseteq> S"
-      using open_contains_cball [of "S"] \<open>x \<in> S\<close> S by blast
-    show ?thesis
-    proof (intro conjI exI)
-      show "uniform_limit (cball x d) (\<lambda>n x. \<Sum>i<n. f i x) g sequentially"
-        using d g uniform_limit_on_subset by (force simp: dist_norm eventually_sequentially)
-    qed (use \<open>d > 0\<close> d in auto)
-  qed
-  have "\<And>x. x \<in> S \<Longrightarrow> (\<lambda>n. \<Sum>i<n. f i x) \<longlonglongrightarrow> g x"
-    by (metis tendsto_uniform_limitI [OF g])
-  moreover have "\<exists>g'. \<forall>x\<in>S. (g has_field_derivative g' x) (at x) \<and> (\<lambda>n. \<Sum>i<n. f' i x) \<longlonglongrightarrow> g' x"
-    by (rule has_complex_derivative_uniform_sequence [OF S]) (auto intro: * hfd DERIV_sum)+
-  ultimately show ?thesis
-    by (metis sums_def that)
-qed
-
-text\<open>A version where we only have local uniform/comparative convergence.\<close>
-
-lemma series_and_derivative_comparison_local:
-  fixes S :: "complex set"
-  assumes S: "open S"
-      and hfd: "\<And>n x. x \<in> S \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
-      and to_g: "\<And>x. x \<in> S \<Longrightarrow> \<exists>d h. 0 < d \<and> summable h \<and> (\<forall>\<^sub>F n in sequentially. \<forall>y\<in>ball x d \<inter> S. norm (f n y) \<le> h n)"
-  shows "\<exists>g g'. \<forall>x \<in> S. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
-proof -
-  have "\<exists>y. (\<lambda>n. f n z) sums (\<Sum>n. f n z) \<and> (\<lambda>n. f' n z) sums y \<and> ((\<lambda>x. \<Sum>n. f n x) has_field_derivative y) (at z)"
-       if "z \<in> S" for z
-  proof -
-    obtain d h where "0 < d" "summable h" and le_h: "\<forall>\<^sub>F n in sequentially. \<forall>y\<in>ball z d \<inter> S. norm (f n y) \<le> h n"
-      using to_g \<open>z \<in> S\<close> by meson
-    then obtain r where "r>0" and r: "ball z r \<subseteq> ball z d \<inter> S" using \<open>z \<in> S\<close> S
-      by (metis Int_iff open_ball centre_in_ball open_Int open_contains_ball_eq)
-    have 1: "open (ball z d \<inter> S)"
-      by (simp add: open_Int S)
-    have 2: "\<And>n x. x \<in> ball z d \<inter> S \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
-      by (auto simp: hfd)
-    obtain g g' where gg': "\<forall>x \<in> ball z d \<inter> S. ((\<lambda>n. f n x) sums g x) \<and>
-                                    ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
-      by (auto intro: le_h series_and_derivative_comparison [OF 1 \<open>summable h\<close> hfd])
-    then have "(\<lambda>n. f' n z) sums g' z"
-      by (meson \<open>0 < r\<close> centre_in_ball contra_subsetD r)
-    moreover have "(\<lambda>n. f n z) sums (\<Sum>n. f n z)"
-      using  summable_sums centre_in_ball \<open>0 < d\<close> \<open>summable h\<close> le_h
-      by (metis (full_types) Int_iff gg' summable_def that)
-    moreover have "((\<lambda>x. \<Sum>n. f n x) has_field_derivative g' z) (at z)"
-    proof (rule has_field_derivative_transform_within)
-      show "\<And>x. dist x z < r \<Longrightarrow> g x = (\<Sum>n. f n x)"
-        by (metis subsetD dist_commute gg' mem_ball r sums_unique)
-    qed (use \<open>0 < r\<close> gg' \<open>z \<in> S\<close> \<open>0 < d\<close> in auto)
-    ultimately show ?thesis by auto
-  qed
-  then show ?thesis
-    by (rule_tac x="\<lambda>x. suminf (\<lambda>n. f n x)" in exI) meson
-qed
-
-
-text\<open>Sometimes convenient to compare with a complex series of positive reals. (?)\<close>
-
-lemma series_and_derivative_comparison_complex:
-  fixes S :: "complex set"
-  assumes S: "open S"
-      and hfd: "\<And>n x. x \<in> S \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
-      and to_g: "\<And>x. x \<in> S \<Longrightarrow> \<exists>d h. 0 < d \<and> summable h \<and> range h \<subseteq> \<real>\<^sub>\<ge>\<^sub>0 \<and> (\<forall>\<^sub>F n in sequentially. \<forall>y\<in>ball x d \<inter> S. cmod(f n y) \<le> cmod (h n))"
-  shows "\<exists>g g'. \<forall>x \<in> S. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
-apply (rule series_and_derivative_comparison_local [OF S hfd], assumption)
-apply (rule ex_forward [OF to_g], assumption)
-apply (erule exE)
-apply (rule_tac x="Re \<circ> h" in exI)
-apply (force simp: summable_Re o_def nonneg_Reals_cmod_eq_Re image_subset_iff)
-done
-
-text\<open>Sometimes convenient to compare with a complex series of positive reals. (?)\<close>
-lemma series_differentiable_comparison_complex:
-  fixes S :: "complex set"
-  assumes S: "open S"
-    and hfd: "\<And>n x. x \<in> S \<Longrightarrow> f n field_differentiable (at x)"
-    and to_g: "\<And>x. x \<in> S \<Longrightarrow> \<exists>d h. 0 < d \<and> summable h \<and> range h \<subseteq> \<real>\<^sub>\<ge>\<^sub>0 \<and> (\<forall>\<^sub>F n in sequentially. \<forall>y\<in>ball x d \<inter> S. cmod(f n y) \<le> cmod (h n))"
-  obtains g where "\<forall>x \<in> S. ((\<lambda>n. f n x) sums g x) \<and> g field_differentiable (at x)"
-proof -
-  have hfd': "\<And>n x. x \<in> S \<Longrightarrow> (f n has_field_derivative deriv (f n) x) (at x)"
-    using hfd field_differentiable_derivI by blast
-  have "\<exists>g g'. \<forall>x \<in> S. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. deriv (f n) x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
-    by (metis series_and_derivative_comparison_complex [OF S hfd' to_g])
-  then show ?thesis
-    using field_differentiable_def that by blast
-qed
-
-text\<open>In particular, a power series is analytic inside circle of convergence.\<close>
-
-lemma power_series_and_derivative_0:
-  fixes a :: "nat \<Rightarrow> complex" and r::real
-  assumes "summable (\<lambda>n. a n * r^n)"
-    shows "\<exists>g g'. \<forall>z. cmod z < r \<longrightarrow>
-             ((\<lambda>n. a n * z^n) sums g z) \<and> ((\<lambda>n. of_nat n * a n * z^(n - 1)) sums g' z) \<and> (g has_field_derivative g' z) (at z)"
-proof (cases "0 < r")
-  case True
-    have der: "\<And>n z. ((\<lambda>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: "\<lbrakk>cmod (z - y) * 2 < r - cmod z\<rbrakk> \<Longrightarrow> cmod y \<le> cmod (of_real r + of_real (cmod z)) / 2" for z y
-      using \<open>r > 0\<close>
-      apply (auto simp: algebra_simps norm_mult norm_divide norm_power simp flip: of_real_add)
-      using norm_triangle_ineq2 [of y z]
-      apply (simp only: diff_le_eq norm_minus_commute mult_2)
-      done
-    have "summable (\<lambda>n. a n * complex_of_real r ^ n)"
-      using assms \<open>r > 0\<close> by simp
-    moreover have "\<And>z. cmod z < r \<Longrightarrow> cmod ((of_real r + of_real (cmod z)) / 2) < cmod (of_real r)"
-      using \<open>r > 0\<close>
-      by (simp flip: of_real_add)
-    ultimately have sum: "\<And>z. cmod z < r \<Longrightarrow> summable (\<lambda>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 "\<exists>g g'. \<forall>z \<in> ball 0 r. (\<lambda>n.  (a n) * z ^ n) sums g z \<and>
-               (\<lambda>n. of_nat n * (a n) * z ^ (n - 1)) sums g' z \<and> (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 (rule_tac x="\<lambda>n. of_real(norm(a n)*((r + norm z)/2)^n)" in exI)
-      using \<open>r > 0\<close>
-      apply (auto simp: sum eventually_sequentially norm_mult norm_power dist_norm intro!: mult_left_mono power_mono y_le)
-      done
-  then show ?thesis
-    by (simp add: 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\<^marker>\<open>tag unimportant\<close> power_series_and_derivative:
-  fixes a :: "nat \<Rightarrow> complex" and r::real
-  assumes "summable (\<lambda>n. a n * r^n)"
-    obtains g g' where "\<forall>z \<in> ball w r.
-             ((\<lambda>n. a n * (z - w) ^ n) sums g z) \<and> ((\<lambda>n. of_nat n * a n * (z - w) ^ (n - 1)) sums g' z) \<and>
-              (g has_field_derivative g' z) (at z)"
-  using power_series_and_derivative_0 [OF assms]
-  apply clarify
-  apply (rule_tac g="(\<lambda>z. g(z - w))" in that)
-  using DERIV_shift [where z="-w"]
-  apply (auto simp: norm_minus_commute Ball_def dist_norm)
-  done
-
-proposition\<^marker>\<open>tag unimportant\<close> power_series_holomorphic:
-  assumes "\<And>w. w \<in> ball z r \<Longrightarrow> ((\<lambda>n. a n*(w - z)^n) sums f w)"
-    shows "f holomorphic_on ball z r"
-proof -
-  have "\<exists>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) \<in> ball z r"
-    proof -
-      have wz: "cmod (w - z) < r" using w
-        by (auto simp: field_split_simps dist_norm norm_minus_commute)
-      then have "0 \<le> r"
-        by (meson less_eq_real_def norm_ge_zero order_trans)
-      show ?thesis
-        using w by (simp add: dist_norm \<open>0\<le>r\<close> flip: of_real_add)
-    qed
-    have sum: "summable (\<lambda>n. a n * of_real (((cmod (z - w) + r) / 2) ^ n))"
-      using assms [OF inb] by (force simp: summable_def dist_norm)
-    obtain g g' where gg': "\<And>u. u \<in> ball z ((cmod (z - w) + r) / 2) \<Longrightarrow>
-                               (\<lambda>n. a n * (u - z) ^ n) sums g u \<and>
-                               (\<lambda>n. of_nat n * a n * (u - z) ^ (n - 1)) sums g' u \<and> (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 "\<lambda>n. a n*(u - z)^n"])
-        using gg' [of u] less w
-        apply (auto simp: assms dist_norm)
-        done
-    qed
-    have "(f has_field_derivative g' w) (at w)"
-      by (rule has_field_derivative_transform_within [where d="(r - norm(z - w))/2"])
-      (use w gg' [of w] in \<open>(force simp: dist_norm)+\<close>)
-    then show ?thesis ..
-  qed
-  then show ?thesis by (simp add: holomorphic_on_open)
-qed
-
-corollary holomorphic_iff_power_series:
-     "f holomorphic_on ball z r \<longleftrightarrow>
-      (\<forall>w \<in> ball z r. (\<lambda>n. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
-  apply (intro iffI ballI holomorphic_power_series, assumption+)
-  apply (force intro: power_series_holomorphic [where a = "\<lambda>n. (deriv ^^ n) f z / (fact n)"])
-  done
-
-lemma power_series_analytic:
-     "(\<And>w. w \<in> ball z r \<Longrightarrow> (\<lambda>n. a n*(w - z)^n) sums f w) \<Longrightarrow> f analytic_on ball z r"
-  by (force simp: analytic_on_open intro!: power_series_holomorphic)
-
-lemma analytic_iff_power_series:
-     "f analytic_on ball z r \<longleftrightarrow>
-      (\<forall>w \<in> ball z r. (\<lambda>n. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
-  by (simp add: analytic_on_open holomorphic_iff_power_series)
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Equality between holomorphic functions, on open ball then connected set\<close>
-
-lemma holomorphic_fun_eq_on_ball:
-   "\<lbrakk>f holomorphic_on ball z r; g holomorphic_on ball z r;
-     w \<in> ball z r;
-     \<And>n. (deriv ^^ n) f z = (deriv ^^ n) g z\<rbrakk>
-     \<Longrightarrow> f w = g w"
-  apply (rule sums_unique2 [of "\<lambda>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:
-   "\<lbrakk>f holomorphic_on ball z r;  w \<in> ball z r;
-     \<And>n. (deriv ^^ n) f z = 0\<rbrakk>
-     \<Longrightarrow> f w = 0"
-  apply (rule sums_unique2 [of "\<lambda>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: "\<And>n. (deriv ^^ n) f z = 0"
-      and "z \<in> S" "w \<in> S"
-    shows "f w = 0"
-proof -
-  have *: "ball x e \<subseteq> (\<Inter>n. {w \<in> S. (deriv ^^ n) f w = 0})"
-    if "\<forall>u. (deriv ^^ u) f x = 0" "ball x e \<subseteq> S" for x e
-  proof -
-    have "\<And>x' n. dist x x' < e \<Longrightarrow> (deriv ^^ n) f x' = 0"
-      apply (rule holomorphic_fun_eq_0_on_ball [OF holomorphic_higher_deriv])
-         apply (rule holomorphic_on_subset [OF holf])
-      using that apply simp_all
-      by (metis funpow_add o_apply)
-    with that show ?thesis by auto
-  qed
-  have 1: "openin (top_of_set S) (\<Inter>n. {w \<in> S. (deriv ^^ n) f w = 0})"
-    apply (rule open_subset, force)
-    using \<open>open S\<close>
-    apply (simp add: open_contains_ball Ball_def)
-    apply (erule all_forward)
-    using "*" by auto blast+
-  have 2: "closedin (top_of_set S) (\<Inter>n. {w \<in> 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 \<subseteq> S" using openE [OF \<open>open S\<close> \<open>w \<in> S\<close>] .
-  then have holfb: "f holomorphic_on ball w e"
-    using holf holomorphic_on_subset by blast
-  have 3: "(\<Inter>n. {w \<in> S. (deriv ^^ n) f w = 0}) = S \<Longrightarrow> f w = 0"
-    using \<open>e>0\<close> e by (force intro: holomorphic_fun_eq_0_on_ball [OF holfb])
-  show ?thesis
-    using cons der \<open>z \<in> S\<close>
-    apply (simp add: connected_clopen)
-    apply (drule_tac x="\<Inter>n. {w \<in> 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 "\<And>n. (deriv ^^ n) f z = (deriv ^^ n) g z"
-      and "z \<in> S" "w \<in> S"
-    shows "f w = g w"
-proof (rule holomorphic_fun_eq_0_on_connected [of "\<lambda>x. f x - g x" S z, simplified])
-  show "(\<lambda>x. f x - g x) holomorphic_on S"
-    by (intro assms holomorphic_intros)
-  show "\<And>n. (deriv ^^ n) (\<lambda>x. f x - g x) z = 0"
-    using assms higher_deriv_diff by auto
-qed (use assms in auto)
-
-lemma holomorphic_fun_eq_const_on_connected:
-  assumes holf: "f holomorphic_on S" and "open S"
-      and cons: "connected S"
-      and der: "\<And>n. 0 < n \<Longrightarrow> (deriv ^^ n) f z = 0"
-      and "z \<in> S" "w \<in> S"
-    shows "f w = f z"
-proof (rule holomorphic_fun_eq_0_on_connected [of "\<lambda>w. f w - f z" S z, simplified])
-  show "(\<lambda>w. f w - f z) holomorphic_on S"
-    by (intro assms holomorphic_intros)
-  show "\<And>n. (deriv ^^ n) (\<lambda>w. f w - f z) z = 0"
-    by (subst higher_deriv_diff) (use assms in \<open>auto intro: holomorphic_intros\<close>)
-qed (use assms in auto)
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Some basic lemmas about poles/singularities\<close>
-
-lemma pole_lemma:
-  assumes holf: "f holomorphic_on S" and a: "a \<in> interior S"
-    shows "(\<lambda>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 \<in> S" "u \<noteq> a" for u
-  proof -
-    have fcd: "f field_differentiable at u within S"
-      using holf holomorphic_on_def by (simp add: \<open>u \<in> S\<close>)
-    have cd: "(\<lambda>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: \<open>u \<in> S\<close>)
-  qed
-  have F2: "?F field_differentiable at a" if "0 < e" "ball a e \<subseteq> S" for e
-  proof -
-    have holfb: "f holomorphic_on ball a e"
-      by (rule holomorphic_on_subset [OF holf \<open>ball a e \<subseteq> S\<close>])
-    have 2: "?F holomorphic_on ball a e - {a}"
-      apply (simp add: holomorphic_on_def flip: field_differentiable_def)
-      using mem_ball that
-      apply (auto intro: F1 field_differentiable_within_subset)
-      done
-    have "isCont (\<lambda>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 have "f field_differentiable at a"
-        using holfb \<open>0 < e\<close> holomorphic_on_imp_differentiable_at by auto
-      with True show ?thesis
-        by (auto simp: continuous_at has_field_derivative_iff simp flip: DERIV_deriv_iff_field_differentiable
-                elim: rev_iffD1 [OF _ LIM_equal])
-    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 \<in> 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 \<open>x \<in> S\<close>
-      show ?thesis by blast
-    qed
-  qed
-qed
-
-lemma pole_theorem:
-  assumes holg: "g holomorphic_on S" and a: "a \<in> interior S"
-      and eq: "\<And>z. z \<in> S - {a} \<Longrightarrow> g z = (z - a) * f z"
-    shows "(\<lambda>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 field_split_simps)
-
-lemma pole_lemma_open:
-  assumes "f holomorphic_on S" "open S"
-    shows "(\<lambda>z. if z = a then deriv f a else (f z - f a)/(z - a)) holomorphic_on S"
-proof (cases "a \<in> 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 = "\<lambda>z. (f z - f a)/(z - a)" and d = 1])
-    apply (rule derivative_intros | force)+
-    done
-qed
-
-lemma pole_theorem_open:
-  assumes holg: "g holomorphic_on S" and S: "open S"
-      and eq: "\<And>z. z \<in> S - {a} \<Longrightarrow> g z = (z - a) * f z"
-    shows "(\<lambda>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)
-
-lemma pole_theorem_0:
-  assumes holg: "g holomorphic_on S" and a: "a \<in> interior S"
-      and eq: "\<And>z. z \<in> S - {a} \<Longrightarrow> 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 field_split_simps)
-
-lemma pole_theorem_open_0:
-  assumes holg: "g holomorphic_on S" and S: "open S"
-      and eq: "\<And>z. z \<in> S - {a} \<Longrightarrow> 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 field_split_simps)
-
-lemma pole_theorem_analytic:
-  assumes g: "g analytic_on S"
-      and eq: "\<And>z. z \<in> S
-             \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>w \<in> ball z d - {a}. g w = (w - a) * f w)"
-    shows "(\<lambda>z. if z = a then deriv g a else f z - g a/(z - a)) analytic_on S" (is "?F analytic_on S")
-  unfolding analytic_on_def
-proof
-  fix x
-  assume "x \<in> S"
-  with g obtain e where "0 < e" and e: "g holomorphic_on ball x e"
-    by (auto simp add: analytic_on_def)
-  obtain d where "0 < d" and d: "\<And>w. w \<in> ball x d - {a} \<Longrightarrow> g w = (w - a) * f w"
-    using \<open>x \<in> S\<close> eq by blast
-  have "?F holomorphic_on ball x (min d e)"
-    using d e \<open>x \<in> S\<close> by (fastforce simp: holomorphic_on_subset subset_ball intro!: pole_theorem_open)
-  then show "\<exists>e>0. ?F holomorphic_on ball x e"
-    using \<open>0 < d\<close> \<open>0 < e\<close> not_le by fastforce
-qed
-
-lemma pole_theorem_analytic_0:
-  assumes g: "g analytic_on S"
-      and eq: "\<And>z. z \<in> S \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>w \<in> 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]: "(\<lambda>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 \<subseteq> T" "open T"
-      and eq: "\<And>z. z \<in> T - {a} \<Longrightarrow> g z = (z - a) * f z"
-    shows "(\<lambda>z. if z = a then deriv g a
-                 else f z - g a/(z - a)) analytic_on S"
-proof (rule pole_theorem_analytic [OF g])
-  fix z
-  assume "z \<in> S"
-  then obtain e where "0 < e" and e: "ball z e \<subseteq> T"
-    using assms openE by blast
-  then show "\<exists>d>0. \<forall>w\<in>ball z d - {a}. g w = (w - a) * f w"
-    using eq by auto
-qed
-
-lemma pole_theorem_analytic_open_superset_0:
-  assumes g: "g analytic_on S" "S \<subseteq> T" "open T" "\<And>z. z \<in> T - {a} \<Longrightarrow> g z = (z - a) * f z"
-      and [simp]: "f a = deriv g a" "g a = 0"
-    shows "f analytic_on S"
-proof -
-  have [simp]: "(\<lambda>z. if z = a then deriv g a else f z - g a / (z - a)) = f"
-    by auto
-  have "(\<lambda>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\<open>General, homology form of Cauchy's theorem\<close>
-
-text\<open>Proof is based on Dixon's, as presented in Lang's "Complex Analysis" book (page 147).\<close>
-
-lemma contour_integral_continuous_on_linepath_2D:
-  assumes "open U" and cont_dw: "\<And>w. w \<in> U \<Longrightarrow> F w contour_integrable_on (linepath a b)"
-      and cond_uu: "continuous_on (U \<times> U) (\<lambda>(x,y). F x y)"
-      and abu: "closed_segment a b \<subseteq> U"
-    shows "continuous_on U (\<lambda>w. contour_integral (linepath a b) (F w))"
-proof -
-  have *: "\<exists>d>0. \<forall>x'\<in>U. dist x' w < d \<longrightarrow>
-                         dist (contour_integral (linepath a b) (F x'))
-                              (contour_integral (linepath a b) (F w)) \<le> \<epsilon>"
-          if "w \<in> U" "0 < \<epsilon>" "a \<noteq> b" for w \<epsilon>
-  proof -
-    obtain \<delta> where "\<delta>>0" and \<delta>: "cball w \<delta> \<subseteq> U" using open_contains_cball \<open>open U\<close> \<open>w \<in> U\<close> by force
-    let ?TZ = "cball w \<delta>  \<times> closed_segment a b"
-    have "uniformly_continuous_on ?TZ (\<lambda>(x,y). F x y)"
-    proof (rule compact_uniformly_continuous)
-      show "continuous_on ?TZ (\<lambda>(x,y). F x y)"
-        by (rule continuous_on_subset[OF cond_uu]) (use SigmaE \<delta> abu in blast)
-      show "compact ?TZ"
-        by (simp add: compact_Times)
-    qed
-    then obtain \<eta> where "\<eta>>0"
-        and \<eta>: "\<And>x x'. \<lbrakk>x\<in>?TZ; x'\<in>?TZ; dist x' x < \<eta>\<rbrakk> \<Longrightarrow>
-                         dist ((\<lambda>(x,y). F x y) x') ((\<lambda>(x,y). F x y) x) < \<epsilon>/norm(b - a)"
-      apply (rule uniformly_continuous_onE [where e = "\<epsilon>/norm(b - a)"])
-      using \<open>0 < \<epsilon>\<close> \<open>a \<noteq> b\<close> by auto
-    have \<eta>: "\<lbrakk>norm (w - x1) \<le> \<delta>;   x2 \<in> closed_segment a b;
-              norm (w - x1') \<le> \<delta>;  x2' \<in> closed_segment a b; norm ((x1', x2') - (x1, x2)) < \<eta>\<rbrakk>
-              \<Longrightarrow> norm (F x1' x2' - F x1 x2) \<le> \<epsilon> / cmod (b - a)"
-             for x1 x2 x1' x2'
-      using \<eta> [of "(x1,x2)" "(x1',x2')"] by (force simp: dist_norm)
-    have le_ee: "cmod (contour_integral (linepath a b) (\<lambda>x. F x' x - F w x)) \<le> \<epsilon>"
-                if "x' \<in> U" "cmod (x' - w) < \<delta>" "cmod (x' - w) < \<eta>"  for x'
-    proof -
-      have "(\<lambda>x. F x' x - F w x) contour_integrable_on linepath a b"
-        by (simp add: \<open>w \<in> U\<close> cont_dw contour_integrable_diff that)
-      then have "cmod (contour_integral (linepath a b) (\<lambda>x. F x' x - F w x)) \<le> \<epsilon>/norm(b - a) * norm(b - a)"
-        apply (rule has_contour_integral_bound_linepath [OF has_contour_integral_integral _ \<eta>])
-        using \<open>0 < \<epsilon>\<close> \<open>0 < \<delta>\<close> that apply (auto simp: norm_minus_commute)
-        done
-      also have "\<dots> = \<epsilon>" using \<open>a \<noteq> b\<close> by simp
-      finally show ?thesis .
-    qed
-    show ?thesis
-      apply (rule_tac x="min \<delta> \<eta>" in exI)
-      using \<open>0 < \<delta>\<close> \<open>0 < \<eta>\<close>
-      apply (auto simp: dist_norm contour_integral_diff [OF cont_dw cont_dw, symmetric] \<open>w \<in> U\<close> intro: le_ee)
-      done
-  qed
-  show ?thesis
-  proof (cases "a=b")
-    case True
-    then show ?thesis by simp
-  next
-    case False
-    show ?thesis
-      by (rule continuous_onI) (use False in \<open>auto intro: *\<close>)
-  qed
-qed
-
-text\<open>This version has \<^term>\<open>polynomial_function \<gamma>\<close> as an additional assumption.\<close>
-lemma Cauchy_integral_formula_global_weak:
-  assumes "open U" and holf: "f holomorphic_on U"
-        and z: "z \<in> U" and \<gamma>: "polynomial_function \<gamma>"
-        and pasz: "path_image \<gamma> \<subseteq> U - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
-        and zero: "\<And>w. w \<notin> U \<Longrightarrow> winding_number \<gamma> w = 0"
-      shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
-proof -
-  obtain \<gamma>' where pf\<gamma>': "polynomial_function \<gamma>'" and \<gamma>': "\<And>x. (\<gamma> has_vector_derivative (\<gamma>' x)) (at x)"
-    using has_vector_derivative_polynomial_function [OF \<gamma>] by blast
-  then have "bounded(path_image \<gamma>')"
-    by (simp add: path_image_def compact_imp_bounded compact_continuous_image continuous_on_polymonial_function)
-  then obtain B where "B>0" and B: "\<And>x. x \<in> path_image \<gamma>' \<Longrightarrow> norm x \<le> B"
-    using bounded_pos by force
-  define d where [abs_def]: "d z w = (if w = z then deriv f z else (f w - f z)/(w - z))" for z w
-  define v where "v = {w. w \<notin> path_image \<gamma> \<and> winding_number \<gamma> w = 0}"
-  have "path \<gamma>" "valid_path \<gamma>" using \<gamma>
-    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 \<union> 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 \<in> U" for y
-  proof -
-    have *: "(\<lambda>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 \<open>open U\<close>)
-    then have "isCont (\<lambda>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 \<open>open U\<close> by fastforce
-    then have "continuous_on U (d y)"
-      apply (simp add: d_def continuous_on_eq_continuous_at \<open>open U\<close>, clarify)
-      using * holomorphic_on_def
-      by (meson field_differentiable_within_open field_differentiable_imp_continuous_at \<open>open U\<close>)
-    moreover have "d y holomorphic_on U - {y}"
-    proof -
-      have "\<And>w. w \<in> U - {y} \<Longrightarrow>
-                 (\<lambda>w. if w = y then deriv f y else (f w - f y) / (w - y)) field_differentiable at w"
-        apply (rule_tac d="dist w y" and f = "\<lambda>w. (f w - f y)/(w - y)" in field_differentiable_transform_within)
-           apply (auto simp: dist_pos_lt dist_commute intro!: derivative_intros)
-        using \<open>open U\<close> holf holomorphic_on_imp_differentiable_at by blast
-      then show ?thesis
-        unfolding field_differentiable_def by (simp add: d_def holomorphic_on_open \<open>open U\<close> open_delete)
-    qed
-    ultimately show ?thesis
-      by (rule no_isolated_singularity) (auto simp: \<open>open U\<close>)
-  qed
-  have cint_fxy: "(\<lambda>x. (f x - f y) / (x - y)) contour_integrable_on \<gamma>" if "y \<notin> path_image \<gamma>" for y
-  proof (rule contour_integrable_holomorphic_simple [where S = "U-{y}"])
-    show "(\<lambda>x. (f x - f y) / (x - y)) holomorphic_on U - {y}"
-      by (force intro: holomorphic_intros holomorphic_on_subset [OF holf])
-    show "path_image \<gamma> \<subseteq> U - {y}"
-      using pasz that by blast
-  qed (auto simp: \<open>open U\<close> open_delete \<open>valid_path \<gamma>\<close>)
-  define h where
-    "h z = (if z \<in> U then contour_integral \<gamma> (d z) else contour_integral \<gamma> (\<lambda>w. f w/(w - z)))" for z
-  have U: "((d z) has_contour_integral h z) \<gamma>" if "z \<in> U" for z
-  proof -
-    have "d z holomorphic_on U"
-      by (simp add: hol_d that)
-    with that show ?thesis
-    apply (simp add: h_def)
-      by (meson Diff_subset \<open>open U\<close> \<open>valid_path \<gamma>\<close> contour_integrable_holomorphic_simple has_contour_integral_integral pasz subset_trans)
-  qed
-  have V: "((\<lambda>w. f w / (w - z)) has_contour_integral h z) \<gamma>" if z: "z \<in> v" for z
-  proof -
-    have 0: "0 = (f z) * 2 * of_real (2 * pi) * \<i> * winding_number \<gamma> z"
-      using v_def z by auto
-    then have "((\<lambda>x. 1 / (x - z)) has_contour_integral 0) \<gamma>"
-     using z v_def  has_contour_integral_winding_number [OF \<open>valid_path \<gamma>\<close>] by fastforce
-    then have "((\<lambda>x. f z * (1 / (x - z))) has_contour_integral 0) \<gamma>"
-      using has_contour_integral_lmul by fastforce
-    then have "((\<lambda>x. f z / (x - z)) has_contour_integral 0) \<gamma>"
-      by (simp add: field_split_simps)
-    moreover have "((\<lambda>x. (f x - f z) / (x - z)) has_contour_integral contour_integral \<gamma> (d z)) \<gamma>"
-      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 *: "((\<lambda>x. f z / (x - z) + (f x - f z) / (x - z)) has_contour_integral (0 + contour_integral \<gamma> (d z))) \<gamma>"
-      by (rule has_contour_integral_add)
-    have "((\<lambda>w. f w / (w - z)) has_contour_integral contour_integral \<gamma> (d z)) \<gamma>"
-            if  "z \<in> U"
-      using * by (auto simp: divide_simps has_contour_integral_eq)
-    moreover have "((\<lambda>w. f w / (w - z)) has_contour_integral contour_integral \<gamma> (\<lambda>w. f w / (w - z))) \<gamma>"
-            if "z \<notin> U"
-      apply (rule has_contour_integral_integral [OF contour_integrable_holomorphic_simple [where S=U]])
-      using U pasz \<open>valid_path \<gamma>\<close> that
-      apply (auto intro: holomorphic_on_imp_continuous_on hol_d)
-       apply (rule continuous_intros conf holomorphic_intros holf assms | force)+
-      done
-    ultimately show ?thesis
-      using z by (simp add: h_def)
-  qed
-  have znot: "z \<notin> path_image \<gamma>"
-    using pasz by blast
-  obtain d0 where "d0>0" and d0: "\<And>x y. x \<in> path_image \<gamma> \<Longrightarrow> y \<in> - U \<Longrightarrow> d0 \<le> dist x y"
-    using separate_compact_closed [of "path_image \<gamma>" "-U"] pasz \<open>open U\<close>
-    by (fastforce simp add: \<open>path \<gamma>\<close> compact_path_image)
-  obtain dd where "0 < dd" and dd: "{y + k | y k. y \<in> path_image \<gamma> \<and> k \<in> ball 0 dd} \<subseteq> U"
-    apply (rule that [of "d0/2"])
-    using \<open>0 < d0\<close>
-    apply (auto simp: dist_norm dest: d0)
-    done
-  have "\<And>x x'. \<lbrakk>x \<in> path_image \<gamma>; dist x x' * 2 < dd\<rbrakk> \<Longrightarrow> \<exists>y k. x' = y + k \<and> y \<in> path_image \<gamma> \<and> dist 0 k * 2 \<le> dd"
-    apply (rule_tac x=x in exI)
-    apply (rule_tac x="x'-x" in exI)
-    apply (force simp: dist_norm)
-    done
-  then have 1: "path_image \<gamma> \<subseteq> interior {y + k |y k. y \<in> path_image \<gamma> \<and> k \<in> cball 0 (dd / 2)}"
-    apply (clarsimp simp add: mem_interior)
-    using \<open>0 < dd\<close>
-    apply (rule_tac x="dd/2" in exI, auto)
-    done
-  obtain T where "compact T" and subt: "path_image \<gamma> \<subseteq> interior T" and T: "T \<subseteq> U"
-    apply (rule that [OF _ 1])
-    apply (fastforce simp add: \<open>valid_path \<gamma>\<close> compact_valid_path_image intro!: compact_sums)
-    apply (rule order_trans [OF _ dd])
-    using \<open>0 < dd\<close> by fastforce
-  obtain L where "L>0"
-           and L: "\<And>f B. \<lbrakk>f holomorphic_on interior T; \<And>z. z\<in>interior T \<Longrightarrow> cmod (f z) \<le> B\<rbrakk> \<Longrightarrow>
-                         cmod (contour_integral \<gamma> f) \<le> L * B"
-      using contour_integral_bound_exists [OF open_interior \<open>valid_path \<gamma>\<close> subt]
-      by blast
-  have "bounded(f ` T)"
-    by (meson \<open>compact T\<close> compact_continuous_image compact_imp_bounded conf continuous_on_subset T)
-  then obtain D where "D>0" and D: "\<And>x. x \<in> T \<Longrightarrow> norm (f x) \<le> D"
-    by (auto simp: bounded_pos)
-  obtain C where "C>0" and C: "\<And>x. x \<in> T \<Longrightarrow> norm x \<le> C"
-    using \<open>compact T\<close> bounded_pos compact_imp_bounded by force
-  have "dist (h y) 0 \<le> e" if "0 < e" and le: "D * L / e + C \<le> cmod y" for e y
-  proof -
-    have "D * L / e > 0"  using \<open>D>0\<close> \<open>L>0\<close> \<open>e>0\<close> by simp
-    with le have ybig: "norm y > C" by force
-    with C have "y \<notin> T"  by force
-    then have ynot: "y \<notin> path_image \<gamma>"
-      using subt interior_subset by blast
-    have [simp]: "winding_number \<gamma> y = 0"
-      apply (rule winding_number_zero_outside [of _ "cball 0 C"])
-      using ybig interior_subset subt
-      apply (force simp: loop \<open>path \<gamma>\<close> dist_norm intro!: C)+
-      done
-    have [simp]: "h y = contour_integral \<gamma> (\<lambda>w. f w/(w - y))"
-      by (rule contour_integral_unique [symmetric]) (simp add: v_def ynot V)
-    have holint: "(\<lambda>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 \<open>y \<notin> T\<close> interior_subset by auto
-    have leD: "cmod (f z / (z - y)) \<le> D * (e / L / D)" if z: "z \<in> interior T" for z
-    proof -
-      have "D * L / e + cmod z \<le> cmod y"
-        using le C [of z] z using interior_subset by force
-      then have DL2: "D * L / e \<le> 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 "\<dots> \<le> D * (e / L / D)"
-        apply (rule mult_mono)
-        using that D interior_subset apply blast
-        using \<open>L>0\<close> \<open>e>0\<close> \<open>D>0\<close> DL2
-        apply (auto simp: norm_divide field_split_simps)
-        done
-      finally show ?thesis .
-    qed
-    have "dist (h y) 0 = cmod (contour_integral \<gamma> (\<lambda>w. f w / (w - y)))"
-      by (simp add: dist_norm)
-    also have "\<dots> \<le> L * (D * (e / L / D))"
-      by (rule L [OF holint leD])
-    also have "\<dots> = e"
-      using  \<open>L>0\<close> \<open>0 < D\<close> by auto
-    finally show ?thesis .
-  qed
-  then have "(h \<longlongrightarrow> 0) at_infinity"
-    by (meson Lim_at_infinityI)
-  moreover have "h holomorphic_on UNIV"
-  proof -
-    have con_ff: "continuous (at (x,z)) (\<lambda>(x,y). (f y - f x) / (y - x))"
-                 if "x \<in> U" "z \<in> U" "x \<noteq> z" for x z
-      using that conf
-      apply (simp add: split_def continuous_on_eq_continuous_at \<open>open U\<close>)
-      apply (simp | rule continuous_intros continuous_within_compose2 [where g=f])+
-      done
-    have con_fstsnd: "continuous_on UNIV (\<lambda>x. (fst x - snd x) ::complex)"
-      by (rule continuous_intros)+
-    have open_uu_Id: "open (U \<times> U - Id)"
-      apply (rule open_Diff)
-      apply (simp add: open_Times \<open>open U\<close>)
-      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 \<in> U" for z
-      apply (rule continuous_on_interior [of U])
-      apply (simp add: holf holomorphic_deriv holomorphic_on_imp_continuous_on \<open>open U\<close>)
-      by (simp add: interior_open that \<open>open U\<close>)
-    have tendsto_f': "((\<lambda>(x,y). if y = x then deriv f (x)
-                                else (f (y) - f (x)) / (y - x)) \<longlongrightarrow> deriv f x)
-                      (at (x, x) within U \<times> U)" if "x \<in> U" for x
-    proof (rule Lim_withinI)
-      fix e::real assume "0 < e"
-      obtain k1 where "k1>0" and k1: "\<And>x'. norm (x' - x) \<le> k1 \<Longrightarrow> norm (deriv f x' - deriv f x) < e"
-        using \<open>0 < e\<close> continuous_within_E [OF con_derf [OF \<open>x \<in> U\<close>]]
-        by (metis UNIV_I dist_norm)
-      obtain k2 where "k2>0" and k2: "ball x k2 \<subseteq> U"
-        by (blast intro: openE [OF \<open>open U\<close>] \<open>x \<in> U\<close>)
-      have neq: "norm ((f z' - f x') / (z' - x') - deriv f x) \<le> e"
-                    if "z' \<noteq> 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 \<in> closed_segment x' z' \<Longrightarrow> cmod (w - x) \<le> 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 \<in> closed_segment x' z' \<Longrightarrow> z' \<noteq> x' \<Longrightarrow> cmod (deriv f w - deriv f x) \<le> 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: "\<And>x. x \<in> U \<Longrightarrow> (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 \<open>open U\<close>)
-        have "closed_segment x' z' \<subseteq> 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 *: "((\<lambda>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) \<le> 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']]
-                 \<open>e > 0\<close>  \<open>z' \<noteq> x'\<close>
-          apply (auto simp: norm_divide divide_simps derf_le)
-          done
-        also have "\<dots> \<le> e" using \<open>0 < e\<close> by simp
-        finally show ?thesis .
-      qed
-      show "\<exists>d>0. \<forall>xa\<in>U \<times> U.
-                  0 < dist xa (x, x) \<and> dist xa (x, x) < d \<longrightarrow>
-                  dist (case xa of (x, y) \<Rightarrow> if y = x then deriv f x else (f y - f x) / (y - x)) (deriv f x) \<le> e"
-        apply (rule_tac x="min k1 k2" in exI)
-        using \<open>k1>0\<close> \<open>k2>0\<close> \<open>e>0\<close>
-        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 \<gamma>) f"
-      by (meson holf holomorphic_on_imp_continuous_on holomorphic_on_subset interior_subset subt T)
-    have le_B: "\<And>T. T \<in> {0..1} \<Longrightarrow> cmod (vector_derivative \<gamma> (at T)) \<le> B"
-      apply (rule B)
-      using \<gamma>' using path_image_def vector_derivative_at by fastforce
-    have f_has_cint: "\<And>w. w \<in> v - path_image \<gamma> \<Longrightarrow> ((\<lambda>u. f u / (u - w) ^ 1) has_contour_integral h w) \<gamma>"
-      by (simp add: V)
-    have cond_uu: "continuous_on (U \<times> U) (\<lambda>(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 \<open>open U\<close>, clarify)
-      apply (rule Lim_transform_within_open [OF _ open_uu_Id, where f = "(\<lambda>(x,y). (f y - f x) / (y - x))"])
-      using con_ff
-      apply (auto simp: continuous_within)
-      done
-    have hol_dw: "(\<lambda>z. d z w) holomorphic_on U" if "w \<in> U" for w
-    proof -
-      have "continuous_on U ((\<lambda>(x,y). d x y) \<circ> (\<lambda>z. (w,z)))"
-        by (rule continuous_on_compose continuous_intros continuous_on_subset [OF cond_uu] | force intro: that)+
-      then have *: "continuous_on U (\<lambda>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 **: "\<And>x. \<lbrakk>x \<in> U; x \<noteq> w\<rbrakk> \<Longrightarrow> (\<lambda>z. if w = z then deriv f z else (f w - f z) / (w - z)) field_differentiable at x"
-        apply (rule_tac f = "\<lambda>x. (f w - f x)/(w - x)" and d = "dist x w" in field_differentiable_transform_within)
-        apply (rule \<open>open U\<close> derivative_intros holomorphic_on_imp_differentiable_at [OF holf] | force simp: dist_commute)+
-        done
-      show ?thesis
-        unfolding d_def
-        apply (rule no_isolated_singularity [OF * _ \<open>open U\<close>, where K = "{w}"])
-        apply (auto simp: field_differentiable_def [symmetric] holomorphic_on_open open_Diff \<open>open U\<close> **)
-        done
-    qed
-    { fix a b
-      assume abu: "closed_segment a b \<subseteq> U"
-      then have "\<And>w. w \<in> U \<Longrightarrow> (\<lambda>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 (\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w))"
-        apply (rule contour_integral_continuous_on_linepath_2D [OF \<open>open U\<close> _ _ abu])
-        apply (auto intro: continuous_on_swap_args cond_uu)
-        done
-      have cont_cint_d\<gamma>: "continuous_on {0..1} ((\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w)) \<circ> \<gamma>)"
-      proof (rule continuous_on_compose)
-        show "continuous_on {0..1} \<gamma>"
-          using \<open>path \<gamma>\<close> path_def by blast
-        show "continuous_on (\<gamma> ` {0..1}) (\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w))"
-          using pasz unfolding path_image_def
-          by (auto intro!: continuous_on_subset [OF cont_cint_d])
-      qed
-      have cint_cint: "(\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w)) contour_integrable_on \<gamma>"
-        apply (simp add: contour_integrable_on)
-        apply (rule integrable_continuous_real)
-        apply (rule continuous_on_mult [OF cont_cint_d\<gamma> [unfolded o_def]])
-        using pf\<gamma>'
-        by (simp add: continuous_on_polymonial_function vector_derivative_at [OF \<gamma>'])
-      have "contour_integral (linepath a b) h = contour_integral (linepath a b) (\<lambda>z. contour_integral \<gamma> (d z))"
-        using abu  by (force simp: h_def intro: contour_integral_eq)
-      also have "\<dots> =  contour_integral \<gamma> (\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w))"
-        apply (rule contour_integral_swap)
-        apply (rule continuous_on_subset [OF cond_uu])
-        using abu pasz \<open>valid_path \<gamma>\<close>
-        apply (auto intro!: continuous_intros)
-        by (metis \<gamma>' continuous_on_eq path_def path_polynomial_function pf\<gamma>' vector_derivative_at)
-      finally have cint_h_eq:
-          "contour_integral (linepath a b) h =
-                    contour_integral \<gamma> (\<lambda>w. contour_integral (linepath a b) (\<lambda>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 \<in> U" and au: "\<forall>n. a n \<in> U" and ax: "a \<longlonglongrightarrow> x"
-      then have A1: "\<forall>\<^sub>F n in sequentially. d (a n) contour_integrable_on \<gamma>"
-        by (meson U contour_integrable_on_def eventuallyI)
-      obtain dd where "dd>0" and dd: "cball x dd \<subseteq> U" using open_contains_cball \<open>open U\<close> x by force
-      have A2: "uniform_limit (path_image \<gamma>) (\<lambda>n. d (a n)) (d x) sequentially"
-        unfolding uniform_limit_iff dist_norm
-      proof clarify
-        fix ee::real
-        assume "0 < ee"
-        show "\<forall>\<^sub>F n in sequentially. \<forall>\<xi>\<in>path_image \<gamma>. cmod (d (a n) \<xi> - d x \<xi>) < ee"
-        proof -
-          let ?ddpa = "{(w,z) |w z. w \<in> cball x dd \<and> z \<in> path_image \<gamma>}"
-          have "uniformly_continuous_on ?ddpa (\<lambda>(x,y). d x y)"
-            apply (rule compact_uniformly_continuous [OF continuous_on_subset[OF cond_uu]])
-            using dd pasz \<open>valid_path \<gamma>\<close>
-             apply (auto simp: compact_Times compact_valid_path_image simp del: mem_cball)
-            done
-          then obtain kk where "kk>0"
-            and kk: "\<And>x x'. \<lbrakk>x \<in> ?ddpa; x' \<in> ?ddpa; dist x' x < kk\<rbrakk> \<Longrightarrow>
-                             dist ((\<lambda>(x,y). d x y) x') ((\<lambda>(x,y). d x y) x) < ee"
-            by (rule uniformly_continuous_onE [where e = ee]) (use \<open>0 < ee\<close> in auto)
-          have kk: "\<lbrakk>norm (w - x) \<le> dd; z \<in> path_image \<gamma>; norm ((w, z) - (x, z)) < kk\<rbrakk> \<Longrightarrow> norm (d w z - d x z) < ee"
-            for  w z
-            using \<open>dd>0\<close> kk [of "(x,z)" "(w,z)"] by (force simp: norm_minus_commute dist_norm)
-          show ?thesis
-            using ax unfolding lim_sequentially eventually_sequentially
-            apply (drule_tac x="min dd kk" in spec)
-            using \<open>dd > 0\<close> \<open>kk > 0\<close>
-            apply (fastforce simp: kk dist_norm)
-            done
-        qed
-      qed
-      have "(\<lambda>n. contour_integral \<gamma> (d (a n))) \<longlonglongrightarrow> contour_integral \<gamma> (d x)"
-        by (rule contour_integral_uniform_limit [OF A1 A2 le_B]) (auto simp: \<open>valid_path \<gamma>\<close>)
-      then have tendsto_hx: "(\<lambda>n. contour_integral \<gamma> (d (a n))) \<longlonglongrightarrow> h x"
-        by (simp add: h_def x)
-      then show "(h \<circ> a) \<longlonglongrightarrow> 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 \<in> v" | "z0 \<in> U" using uv_Un by blast
-      then show "h field_differentiable at z0"
-      proof cases
-        assume "z0 \<in> v" then show ?thesis
-          using Cauchy_next_derivative [OF con_pa_f le_B f_has_cint _ ov] V f_has_cint \<open>valid_path \<gamma>\<close>
-          by (auto simp: field_differentiable_def v_def)
-      next
-        assume "z0 \<in> U" then
-        obtain e where "e>0" and e: "ball z0 e \<subseteq> U" by (blast intro: openE [OF \<open>open U\<close>])
-        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} \<subseteq> ball z0 e"  for a b c
-        proof -
-          have *: "\<And>x1 x2 z. z \<in> U \<Longrightarrow> closed_segment x1 x2 \<subseteq> U \<Longrightarrow> (\<lambda>w. d w z) contour_integrable_on linepath x1 x2"
-            using  hol_dw holomorphic_on_imp_continuous_on \<open>open U\<close>
-            by (auto intro!: contour_integrable_holomorphic_simple)
-          have abc: "closed_segment a b \<subseteq> U"  "closed_segment b c \<subseteq> U"  "closed_segment c a \<subseteq> U"
-            using that e segments_subset_convex_hull by fastforce+
-          have eq0: "\<And>w. w \<in> U \<Longrightarrow> contour_integral (linepath a b +++ linepath b c +++ linepath c a) (\<lambda>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 \<gamma>
-                   (\<lambda>x. contour_integral (linepath a b) (\<lambda>z. d z x) +
-                        (contour_integral (linepath b c) (\<lambda>z. d z x) +
-                         contour_integral (linepath c a) (\<lambda>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 \<open>e > 0\<close>
-          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 "((\<lambda>w. 1 / (w - z)) has_contour_integral complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z) \<gamma>"
-    by (rule has_contour_integral_winding_number [OF \<open>valid_path \<gamma>\<close> znot])
-  then have "((\<lambda>w. f z * (1 / (w - z))) has_contour_integral complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z * f z) \<gamma>"
-    by (metis mult.commute has_contour_integral_lmul)
-  then have 1: "((\<lambda>w. f z / (w - z)) has_contour_integral complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z * f z) \<gamma>"
-    by (simp add: field_split_simps)
-  moreover have 2: "((\<lambda>w. (f w - f z) / (w - z)) has_contour_integral 0) \<gamma>"
-    using U [OF z] pasz d_def by (force elim: has_contour_integral_eq [where g = "\<lambda>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 \<in> S" and vpg: "valid_path \<gamma>"
-        and pasz: "path_image \<gamma> \<subseteq> S - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
-        and zero: "\<And>w. w \<notin> S \<Longrightarrow> winding_number \<gamma> w = 0"
-      shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
-proof -
-  have "path \<gamma>" using vpg by (blast intro: valid_path_imp_path)
-  have hols: "(\<lambda>w. f w / (w - z)) holomorphic_on S - {z}" "(\<lambda>w. 1 / (w - z)) holomorphic_on S - {z}"
-    by (rule holomorphic_intros holomorphic_on_subset [OF holf] | force)+
-  then have cint_fw: "(\<lambda>w. f w / (w - z)) contour_integrable_on \<gamma>"
-    by (meson contour_integrable_holomorphic_simple holomorphic_on_imp_continuous_on open_delete S vpg pasz)
-  obtain d where "d>0"
-      and d: "\<And>g h. \<lbrakk>valid_path g; valid_path h; \<forall>t\<in>{0..1}. cmod (g t - \<gamma> t) < d \<and> cmod (h t - \<gamma> t) < d;
-                     pathstart h = pathstart g \<and> pathfinish h = pathfinish g\<rbrakk>
-                     \<Longrightarrow> path_image h \<subseteq> S - {z} \<and> (\<forall>f. f holomorphic_on S - {z} \<longrightarrow> contour_integral h f = contour_integral g f)"
-    using contour_integral_nearby_ends [OF _ \<open>path \<gamma>\<close> pasz] S by (simp add: open_Diff) metis
-  obtain p where polyp: "polynomial_function p"
-             and ps: "pathstart p = pathstart \<gamma>" and pf: "pathfinish p = pathfinish \<gamma>" and led: "\<forall>t\<in>{0..1}. cmod (p t - \<gamma> t) < d"
-    using path_approx_polynomial_function [OF \<open>path \<gamma>\<close> \<open>d > 0\<close>] 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 \<notin> path_image \<gamma>" using pasz by blast
-  have paps: "path_image p \<subseteq> S - {z}" and cint_eq: "(\<And>f. f holomorphic_on S - {z} \<Longrightarrow> contour_integral p f = contour_integral \<gamma> f)"
-    using pf ps led d [OF vpg vpp] \<open>d > 0\<close> by auto
-  have wn_eq: "winding_number p z = winding_number \<gamma> 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 \<gamma> w" if "w \<notin> S" for w
-  proof -
-    have hol: "(\<lambda>v. 1 / (v - w)) holomorphic_on S - {z}"
-      using that by (force intro: holomorphic_intros holomorphic_on_subset [OF holf])
-   have "w \<notin> path_image p" "w \<notin> path_image \<gamma>" 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: "\<And>w. w \<notin> S \<Longrightarrow> 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 \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
-        and pas: "path_image \<gamma> \<subseteq> S"
-        and zero: "\<And>w. w \<notin> S \<Longrightarrow> winding_number \<gamma> w = 0"
-      shows "(f has_contour_integral 0) \<gamma>"
-proof -
-  obtain z where "z \<in> S" and znot: "z \<notin> path_image \<gamma>"
-  proof -
-    have "compact (path_image \<gamma>)"
-      using compact_valid_path_image vpg by blast
-    then have "path_image \<gamma> \<noteq> 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 \<gamma> \<subseteq> S - {z}" using pas by blast
-  have hol: "(\<lambda>w. (w - z) * f w) holomorphic_on S"
-    by (rule holomorphic_intros holf)+
-  show ?thesis
-    using Cauchy_integral_formula_global [OF S hol \<open>z \<in> S\<close> 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 \<gamma>"  "pathfinish \<gamma> = pathstart \<gamma>" "path_image \<gamma> \<subseteq> S"
-            "\<And>w. w \<notin> S \<Longrightarrow> w \<in> outside(path_image \<gamma>)"
-      shows "(f has_contour_integral 0) \<gamma>"
-by (metis Cauchy_theorem_global assms winding_number_zero_in_outside valid_path_imp_path)
-
-lemma simply_connected_imp_winding_number_zero:
-  assumes "simply_connected S" "path g"
-           "path_image g \<subseteq> S" "pathfinish g = pathstart g" "z \<notin> S"
-    shows "winding_number g z = 0"
-proof -
-  have hom: "homotopic_loops S g (linepath (pathstart g) (pathstart g))"
-    by (meson assms homotopic_paths_imp_homotopic_loops pathfinish_linepath simply_connected_eq_contractible_path)
-  then have "homotopic_paths (- {z}) g (linepath (pathstart g) (pathstart g))"
-    by (meson \<open>z \<notin> S\<close> homotopic_loops_imp_homotopic_paths_null homotopic_paths_subset subset_Compl_singleton)
-  then have "winding_number g z = winding_number(linepath (pathstart g) (pathstart g)) z"
-    by (rule winding_number_homotopic_paths)
-  also have "\<dots> = 0"
-    using assms by (force intro: winding_number_trivial)
-  finally show ?thesis .
-qed
-
-lemma Cauchy_theorem_simply_connected:
-  assumes "open S" "simply_connected S" "f holomorphic_on S" "valid_path g"
-           "path_image g \<subseteq> S" "pathfinish g = pathstart g"
-    shows "(f has_contour_integral 0) g"
-using assms
-apply (simp add: simply_connected_eq_contractible_path)
-apply (auto intro!: Cauchy_theorem_null_homotopic [where a = "pathstart g"]
-                         homotopic_paths_imp_homotopic_loops)
-using valid_path_imp_path by blast
-
-proposition\<^marker>\<open>tag unimportant\<close> holomorphic_logarithm_exists:
-  assumes A: "convex A" "open A"
-      and f: "f holomorphic_on A" "\<And>x. x \<in> A \<Longrightarrow> f x \<noteq> 0"
-      and z0: "z0 \<in> A"
-    obtains g where "g holomorphic_on A" and "\<And>x. x \<in> A \<Longrightarrow> exp (g x) = f x"
-proof -
-  note f' = holomorphic_derivI [OF f(1) A(2)]
-  obtain g where g: "\<And>x. x \<in> A \<Longrightarrow> (g has_field_derivative deriv f x / f x) (at x)"
-  proof (rule holomorphic_convex_primitive' [OF A])
-    show "(\<lambda>x. deriv f x / f x) holomorphic_on A"
-      by (intro holomorphic_intros f A)
-  qed (auto simp: A at_within_open[of _ A])
-  define h where "h = (\<lambda>x. -g z0 + ln (f z0) + g x)"
-  from g and A have g_holo: "g holomorphic_on A"
-    by (auto simp: holomorphic_on_def at_within_open[of _ A] field_differentiable_def)
-  hence h_holo: "h holomorphic_on A"
-    by (auto simp: h_def intro!: holomorphic_intros)
-  have "\<exists>c. \<forall>x\<in>A. f x / exp (h x) - 1 = c"
-  proof (rule has_field_derivative_zero_constant, goal_cases)
-    case (2 x)
-    note [simp] = at_within_open[OF _ \<open>open A\<close>]
-    from 2 and z0 and f show ?case
-      by (auto simp: h_def exp_diff field_simps intro!: derivative_eq_intros g f')
-  qed fact+
-  then obtain c where c: "\<And>x. x \<in> A \<Longrightarrow> f x / exp (h x) - 1 = c"
-    by blast
-  from c[OF z0] and z0 and f have "c = 0"
-    by (simp add: h_def)
-  with c have "\<And>x. x \<in> A \<Longrightarrow> exp (h x) = f x" by simp
-  from that[OF h_holo this] show ?thesis .
-qed
-
-end
--- a/src/HOL/Analysis/Change_Of_Vars.thy	Wed Nov 27 16:54:33 2019 +0000
+++ b/src/HOL/Analysis/Change_Of_Vars.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -3388,7 +3388,7 @@
 next
   case False
   then obtain h where h: "\<And>x. x \<in> S \<Longrightarrow> h (g x) = x" "linear h"
-    using assms det_nz_iff_inj linear_injective_isomorphism by blast
+    using assms det_nz_iff_inj linear_injective_isomorphism by metis
   show ?thesis
   proof (rule has_absolute_integral_change_of_variables_invertible)
     show "(g has_derivative g) (at x within S)" for x
--- a/src/HOL/Analysis/Complex_Analysis_Basics.thy	Wed Nov 27 16:54:33 2019 +0000
+++ b/src/HOL/Analysis/Complex_Analysis_Basics.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -114,42 +114,6 @@
   assumes "eventually (\<lambda>x. norm(f x) \<le> Re(g x)) F" "(g \<longlongrightarrow> 0) F" shows "(f \<longlongrightarrow> 0) F"
   by (rule Lim_null_comparison[OF assms(1)] tendsto_eq_intros assms(2))+ simp
 
-lemma closed_segment_same_Re:
-  assumes "Re a = Re b"
-  shows   "closed_segment a b = {z. Re z = Re a \<and> Im z \<in> closed_segment (Im a) (Im b)}"
-proof safe
-  fix z assume "z \<in> closed_segment a b"
-  then obtain u where u: "u \<in> {0..1}" "z = a + of_real u * (b - a)"
-    by (auto simp: closed_segment_def scaleR_conv_of_real algebra_simps)
-  from assms show "Re z = Re a" by (auto simp: u)
-  from u(1) show "Im z \<in> closed_segment (Im a) (Im b)"
-    by (force simp: u closed_segment_def algebra_simps)
-next
-  fix z assume [simp]: "Re z = Re a" and "Im z \<in> closed_segment (Im a) (Im b)"
-  then obtain u where u: "u \<in> {0..1}" "Im z = Im a + of_real u * (Im b - Im a)"
-    by (auto simp: closed_segment_def scaleR_conv_of_real algebra_simps)
-  from u(1) show "z \<in> closed_segment a b" using assms
-    by (force simp: u closed_segment_def algebra_simps scaleR_conv_of_real complex_eq_iff)
-qed
-
-lemma closed_segment_same_Im:
-  assumes "Im a = Im b"
-  shows   "closed_segment a b = {z. Im z = Im a \<and> Re z \<in> closed_segment (Re a) (Re b)}"
-proof safe
-  fix z assume "z \<in> closed_segment a b"
-  then obtain u where u: "u \<in> {0..1}" "z = a + of_real u * (b - a)"
-    by (auto simp: closed_segment_def scaleR_conv_of_real algebra_simps)
-  from assms show "Im z = Im a" by (auto simp: u)
-  from u(1) show "Re z \<in> closed_segment (Re a) (Re b)"
-    by (force simp: u closed_segment_def algebra_simps)
-next
-  fix z assume [simp]: "Im z = Im a" and "Re z \<in> closed_segment (Re a) (Re b)"
-  then obtain u where u: "u \<in> {0..1}" "Re z = Re a + of_real u * (Re b - Re a)"
-    by (auto simp: closed_segment_def scaleR_conv_of_real algebra_simps)
-  from u(1) show "z \<in> closed_segment a b" using assms
-    by (force simp: u closed_segment_def algebra_simps scaleR_conv_of_real complex_eq_iff)
-qed
-
 subsection\<open>Holomorphic functions\<close>
 
 definition\<^marker>\<open>tag important\<close> holomorphic_on :: "[complex \<Rightarrow> complex, complex set] \<Rightarrow> bool"
@@ -310,74 +274,11 @@
   finally show \<dots> .
 qed (insert assms, auto)
 
-lemma DERIV_deriv_iff_field_differentiable:
-  "DERIV f x :> deriv f x \<longleftrightarrow> f field_differentiable at x"
-  unfolding field_differentiable_def by (metis DERIV_imp_deriv)
-
 lemma holomorphic_derivI:
      "\<lbrakk>f holomorphic_on S; open S; x \<in> S\<rbrakk>
       \<Longrightarrow> (f has_field_derivative deriv f x) (at x within T)"
 by (metis DERIV_deriv_iff_field_differentiable at_within_open  holomorphic_on_def has_field_derivative_at_within)
 
-lemma complex_derivative_chain:
-  "f field_differentiable at x \<Longrightarrow> g field_differentiable at (f x)
-    \<Longrightarrow> deriv (g o f) x = deriv g (f x) * deriv f x"
-  by (metis DERIV_deriv_iff_field_differentiable DERIV_chain DERIV_imp_deriv)
-
-lemma deriv_linear [simp]: "deriv (\<lambda>w. c * w) = (\<lambda>z. c)"
-  by (metis DERIV_imp_deriv DERIV_cmult_Id)
-
-lemma deriv_ident [simp]: "deriv (\<lambda>w. w) = (\<lambda>z. 1)"
-  by (metis DERIV_imp_deriv DERIV_ident)
-
-lemma deriv_id [simp]: "deriv id = (\<lambda>z. 1)"
-  by (simp add: id_def)
-
-lemma deriv_const [simp]: "deriv (\<lambda>w. c) = (\<lambda>z. 0)"
-  by (metis DERIV_imp_deriv DERIV_const)
-
-lemma deriv_add [simp]:
-  "\<lbrakk>f field_differentiable at z; g field_differentiable at z\<rbrakk>
-   \<Longrightarrow> deriv (\<lambda>w. f w + g w) z = deriv f z + deriv g z"
-  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
-  by (auto intro!: DERIV_imp_deriv derivative_intros)
-
-lemma deriv_diff [simp]:
-  "\<lbrakk>f field_differentiable at z; g field_differentiable at z\<rbrakk>
-   \<Longrightarrow> deriv (\<lambda>w. f w - g w) z = deriv f z - deriv g z"
-  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
-  by (auto intro!: DERIV_imp_deriv derivative_intros)
-
-lemma deriv_mult [simp]:
-  "\<lbrakk>f field_differentiable at z; g field_differentiable at z\<rbrakk>
-   \<Longrightarrow> deriv (\<lambda>w. f w * g w) z = f z * deriv g z + deriv f z * g z"
-  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
-  by (auto intro!: DERIV_imp_deriv derivative_eq_intros)
-
-lemma deriv_cmult:
-  "f field_differentiable at z \<Longrightarrow> deriv (\<lambda>w. c * f w) z = c * deriv f z"
-  by simp
-
-lemma deriv_cmult_right:
-  "f field_differentiable at z \<Longrightarrow> deriv (\<lambda>w. f w * c) z = deriv f z * c"
-  by simp
-
-lemma deriv_inverse [simp]:
-  "\<lbrakk>f field_differentiable at z; f z \<noteq> 0\<rbrakk>
-   \<Longrightarrow> deriv (\<lambda>w. inverse (f w)) z = - deriv f z / f z ^ 2"
-  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
-  by (safe intro!: DERIV_imp_deriv derivative_eq_intros) (auto simp: field_split_simps power2_eq_square)
-
-lemma deriv_divide [simp]:
-  "\<lbrakk>f field_differentiable at z; g field_differentiable at z; g z \<noteq> 0\<rbrakk>
-   \<Longrightarrow> deriv (\<lambda>w. f w / g w) z = (deriv f z * g z - f z * deriv g z) / g z ^ 2"
-  by (simp add: field_class.field_divide_inverse field_differentiable_inverse)
-     (simp add: field_split_simps power2_eq_square)
-
-lemma deriv_cdivide_right:
-  "f field_differentiable at z \<Longrightarrow> deriv (\<lambda>w. f w / c) z = deriv f z / c"
-  by (simp add: field_class.field_divide_inverse)
-
 lemma complex_derivative_transform_within_open:
   "\<lbrakk>f holomorphic_on s; g holomorphic_on s; open s; z \<in> s; \<And>w. w \<in> s \<Longrightarrow> f w = g w\<rbrakk>
    \<Longrightarrow> deriv f z = deriv g z"
@@ -385,19 +286,6 @@
   by (rule DERIV_imp_deriv)
      (metis DERIV_deriv_iff_field_differentiable has_field_derivative_transform_within_open at_within_open)
 
-lemma deriv_compose_linear:
-  "f field_differentiable at (c * z) \<Longrightarrow> deriv (\<lambda>w. f (c * w)) z = c * deriv f (c * z)"
-apply (rule DERIV_imp_deriv)
-  unfolding DERIV_deriv_iff_field_differentiable [symmetric]
-  by (metis (full_types) DERIV_chain2 DERIV_cmult_Id mult.commute)
-
-
-lemma nonzero_deriv_nonconstant:
-  assumes df: "DERIV f \<xi> :> df" and S: "open S" "\<xi> \<in> S" and "df \<noteq> 0"
-    shows "\<not> f constant_on S"
-unfolding constant_on_def
-by (metis \<open>df \<noteq> 0\<close> has_field_derivative_transform_within_open [OF df S] DERIV_const DERIV_unique)
-
 lemma holomorphic_nonconstant:
   assumes holf: "f holomorphic_on S" and "open S" "\<xi> \<in> S" "deriv f \<xi> \<noteq> 0"
     shows "\<not> f constant_on S"
@@ -615,7 +503,7 @@
     by (simp add: algebra_simps)
   also have "... = deriv (g o f) w"
     using assms
-    by (metis analytic_on_imp_differentiable_at analytic_on_open complex_derivative_chain image_subset_iff)
+    by (metis analytic_on_imp_differentiable_at analytic_on_open deriv_chain image_subset_iff)
   also have "... = deriv id w"
   proof (rule complex_derivative_transform_within_open [where s=S])
     show "g \<circ> f holomorphic_on S"
--- a/src/HOL/Analysis/Conformal_Mappings.thy	Wed Nov 27 16:54:33 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5085 +0,0 @@
-section \<open>Conformal Mappings and Consequences of Cauchy's Integral Theorem\<close>
-
-text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2016)\<close>
-
-text\<open>Also Cauchy's residue theorem by Wenda Li (2016)\<close>
-
-theory Conformal_Mappings
-imports Cauchy_Integral_Theorem
-
-begin
-
-(* FIXME mv to Cauchy_Integral_Theorem.thy *)
-subsection\<open>Cauchy's inequality and more versions of Liouville\<close>
-
-lemma Cauchy_higher_deriv_bound:
-    assumes holf: "f holomorphic_on (ball z r)"
-        and contf: "continuous_on (cball z r) f"
-        and fin : "\<And>w. w \<in> ball z r \<Longrightarrow> f w \<in> ball y B0"
-        and "0 < r" and "0 < n"
-      shows "norm ((deriv ^^ n) f z) \<le> (fact n) * B0 / r^n"
-proof -
-  have "0 < B0" using \<open>0 < r\<close> fin [of z]
-    by (metis ball_eq_empty ex_in_conv fin not_less)
-  have le_B0: "\<And>w. cmod (w - z) \<le> r \<Longrightarrow> cmod (f w - y) \<le> B0"
-    apply (rule continuous_on_closure_norm_le [of "ball z r" "\<lambda>w. f w - y"])
-    apply (auto simp: \<open>0 < r\<close>  dist_norm norm_minus_commute)
-    apply (rule continuous_intros contf)+
-    using fin apply (simp add: dist_commute dist_norm less_eq_real_def)
-    done
-  have "(deriv ^^ n) f z = (deriv ^^ n) (\<lambda>w. f w) z - (deriv ^^ n) (\<lambda>w. y) z"
-    using \<open>0 < n\<close> by simp
-  also have "... = (deriv ^^ n) (\<lambda>w. f w - y) z"
-    by (rule higher_deriv_diff [OF holf, symmetric]) (auto simp: \<open>0 < r\<close>)
-  finally have "(deriv ^^ n) f z = (deriv ^^ n) (\<lambda>w. f w - y) z" .
-  have contf': "continuous_on (cball z r) (\<lambda>u. f u - y)"
-    by (rule contf continuous_intros)+
-  have holf': "(\<lambda>u. (f u - y)) holomorphic_on (ball z r)"
-    by (simp add: holf holomorphic_on_diff)
-  define a where "a = (2 * pi)/(fact n)"
-  have "0 < a"  by (simp add: a_def)
-  have "B0/r^(Suc n)*2 * pi * r = a*((fact n)*B0/r^n)"
-    using \<open>0 < r\<close> by (simp add: a_def field_split_simps)
-  have der_dif: "(deriv ^^ n) (\<lambda>w. f w - y) z = (deriv ^^ n) f z"
-    using \<open>0 < r\<close> \<open>0 < n\<close>
-    by (auto simp: higher_deriv_diff [OF holf holomorphic_on_const])
-  have "norm ((2 * of_real pi * \<i>)/(fact n) * (deriv ^^ n) (\<lambda>w. f w - y) z)
-        \<le> (B0/r^(Suc n)) * (2 * pi * r)"
-    apply (rule has_contour_integral_bound_circlepath [of "(\<lambda>u. (f u - y)/(u - z)^(Suc n))" _ z])
-    using Cauchy_has_contour_integral_higher_derivative_circlepath [OF contf' holf']
-    using \<open>0 < B0\<close> \<open>0 < r\<close>
-    apply (auto simp: norm_divide norm_mult norm_power divide_simps le_B0)
-    done
-  then show ?thesis
-    using \<open>0 < r\<close>
-    by (auto simp: norm_divide norm_mult norm_power field_simps der_dif le_B0)
-qed
-
-lemma Cauchy_inequality:
-    assumes holf: "f holomorphic_on (ball \<xi> r)"
-        and contf: "continuous_on (cball \<xi> r) f"
-        and "0 < r"
-        and nof: "\<And>x. norm(\<xi>-x) = r \<Longrightarrow> norm(f x) \<le> B"
-      shows "norm ((deriv ^^ n) f \<xi>) \<le> (fact n) * B / r^n"
-proof -
-  obtain x where "norm (\<xi>-x) = r"
-    by (metis abs_of_nonneg add_diff_cancel_left' \<open>0 < r\<close> diff_add_cancel
-                 dual_order.strict_implies_order norm_of_real)
-  then have "0 \<le> B"
-    by (metis nof norm_not_less_zero not_le order_trans)
-  have  "((\<lambda>u. f u / (u - \<xi>) ^ Suc n) has_contour_integral (2 * pi) * \<i> / fact n * (deriv ^^ n) f \<xi>)
-         (circlepath \<xi> r)"
-    apply (rule Cauchy_has_contour_integral_higher_derivative_circlepath [OF contf holf])
-    using \<open>0 < r\<close> by simp
-  then have "norm ((2 * pi * \<i>)/(fact n) * (deriv ^^ n) f \<xi>) \<le> (B / r^(Suc n)) * (2 * pi * r)"
-    apply (rule has_contour_integral_bound_circlepath)
-    using \<open>0 \<le> B\<close> \<open>0 < r\<close>
-    apply (simp_all add: norm_divide norm_power nof frac_le norm_minus_commute del: power_Suc)
-    done
-  then show ?thesis using \<open>0 < r\<close>
-    by (simp add: norm_divide norm_mult field_simps)
-qed
-
-lemma Liouville_polynomial:
-    assumes holf: "f holomorphic_on UNIV"
-        and nof: "\<And>z. A \<le> norm z \<Longrightarrow> norm(f z) \<le> B * norm z ^ n"
-      shows "f \<xi> = (\<Sum>k\<le>n. (deriv^^k) f 0 / fact k * \<xi> ^ k)"
-proof (cases rule: le_less_linear [THEN disjE])
-  assume "B \<le> 0"
-  then have "\<And>z. A \<le> norm z \<Longrightarrow> norm(f z) = 0"
-    by (metis nof less_le_trans zero_less_mult_iff neqE norm_not_less_zero norm_power not_le)
-  then have f0: "(f \<longlongrightarrow> 0) at_infinity"
-    using Lim_at_infinity by force
-  then have [simp]: "f = (\<lambda>w. 0)"
-    using Liouville_weak [OF holf, of 0]
-    by (simp add: eventually_at_infinity f0) meson
-  show ?thesis by simp
-next
-  assume "0 < B"
-  have "((\<lambda>k. (deriv ^^ k) f 0 / (fact k) * (\<xi> - 0)^k) sums f \<xi>)"
-    apply (rule holomorphic_power_series [where r = "norm \<xi> + 1"])
-    using holf holomorphic_on_subset apply auto
-    done
-  then have sumsf: "((\<lambda>k. (deriv ^^ k) f 0 / (fact k) * \<xi>^k) sums f \<xi>)" by simp
-  have "(deriv ^^ k) f 0 / fact k * \<xi> ^ k = 0" if "k>n" for k
-  proof (cases "(deriv ^^ k) f 0 = 0")
-    case True then show ?thesis by simp
-  next
-    case False
-    define w where "w = complex_of_real (fact k * B / cmod ((deriv ^^ k) f 0) + (\<bar>A\<bar> + 1))"
-    have "1 \<le> abs (fact k * B / cmod ((deriv ^^ k) f 0) + (\<bar>A\<bar> + 1))"
-      using \<open>0 < B\<close> by simp
-    then have wge1: "1 \<le> norm w"
-      by (metis norm_of_real w_def)
-    then have "w \<noteq> 0" by auto
-    have kB: "0 < fact k * B"
-      using \<open>0 < B\<close> by simp
-    then have "0 \<le> fact k * B / cmod ((deriv ^^ k) f 0)"
-      by simp
-    then have wgeA: "A \<le> cmod w"
-      by (simp only: w_def norm_of_real)
-    have "fact k * B / cmod ((deriv ^^ k) f 0) < abs (fact k * B / cmod ((deriv ^^ k) f 0) + (\<bar>A\<bar> + 1))"
-      using \<open>0 < B\<close> by simp
-    then have wge: "fact k * B / cmod ((deriv ^^ k) f 0) < norm w"
-      by (metis norm_of_real w_def)
-    then have "fact k * B / norm w < cmod ((deriv ^^ k) f 0)"
-      using False by (simp add: field_split_simps mult.commute split: if_split_asm)
-    also have "... \<le> fact k * (B * norm w ^ n) / norm w ^ k"
-      apply (rule Cauchy_inequality)
-         using holf holomorphic_on_subset apply force
-        using holf holomorphic_on_imp_continuous_on holomorphic_on_subset apply blast
-       using \<open>w \<noteq> 0\<close> apply simp
-       by (metis nof wgeA dist_0_norm dist_norm)
-    also have "... = fact k * (B * 1 / cmod w ^ (k-n))"
-      apply (simp only: mult_cancel_left times_divide_eq_right [symmetric])
-      using \<open>k>n\<close> \<open>w \<noteq> 0\<close> \<open>0 < B\<close> apply (simp add: field_split_simps semiring_normalization_rules)
-      done
-    also have "... = fact k * B / cmod w ^ (k-n)"
-      by simp
-    finally have "fact k * B / cmod w < fact k * B / cmod w ^ (k - n)" .
-    then have "1 / cmod w < 1 / cmod w ^ (k - n)"
-      by (metis kB divide_inverse inverse_eq_divide mult_less_cancel_left_pos)
-    then have "cmod w ^ (k - n) < cmod w"
-      by (metis frac_le le_less_trans norm_ge_zero norm_one not_less order_refl wge1 zero_less_one)
-    with self_le_power [OF wge1] have False
-      by (meson diff_is_0_eq not_gr0 not_le that)
-    then show ?thesis by blast
-  qed
-  then have "(deriv ^^ (k + Suc n)) f 0 / fact (k + Suc n) * \<xi> ^ (k + Suc n) = 0" for k
-    using not_less_eq by blast
-  then have "(\<lambda>i. (deriv ^^ (i + Suc n)) f 0 / fact (i + Suc n) * \<xi> ^ (i + Suc n)) sums 0"
-    by (rule sums_0)
-  with sums_split_initial_segment [OF sumsf, where n = "Suc n"]
-  show ?thesis
-    using atLeast0AtMost lessThan_Suc_atMost sums_unique2 by fastforce
-qed
-
-text\<open>Every bounded entire function is a constant function.\<close>
-theorem Liouville_theorem:
-    assumes holf: "f holomorphic_on UNIV"
-        and bf: "bounded (range f)"
-    obtains c where "\<And>z. f z = c"
-proof -
-  obtain B where "\<And>z. cmod (f z) \<le> B"
-    by (meson bf bounded_pos rangeI)
-  then show ?thesis
-    using Liouville_polynomial [OF holf, of 0 B 0, simplified] that by blast
-qed
-
-text\<open>A holomorphic function f has only isolated zeros unless f is 0.\<close>
-
-lemma powser_0_nonzero:
-  fixes a :: "nat \<Rightarrow> 'a::{real_normed_field,banach}"
-  assumes r: "0 < r"
-      and sm: "\<And>x. norm (x - \<xi>) < r \<Longrightarrow> (\<lambda>n. a n * (x - \<xi>) ^ n) sums (f x)"
-      and [simp]: "f \<xi> = 0"
-      and m0: "a m \<noteq> 0" and "m>0"
-  obtains s where "0 < s" and "\<And>z. z \<in> cball \<xi> s - {\<xi>} \<Longrightarrow> f z \<noteq> 0"
-proof -
-  have "r \<le> conv_radius a"
-    using sm sums_summable by (auto simp: le_conv_radius_iff [where \<xi>=\<xi>])
-  obtain m where am: "a m \<noteq> 0" and az [simp]: "(\<And>n. n<m \<Longrightarrow> a n = 0)"
-    apply (rule_tac m = "LEAST n. a n \<noteq> 0" in that)
-    using m0
-    apply (rule LeastI2)
-    apply (fastforce intro:  dest!: not_less_Least)+
-    done
-  define b where "b i = a (i+m) / a m" for i
-  define g where "g x = suminf (\<lambda>i. b i * (x - \<xi>) ^ i)" for x
-  have [simp]: "b 0 = 1"
-    by (simp add: am b_def)
-  { fix x::'a
-    assume "norm (x - \<xi>) < r"
-    then have "(\<lambda>n. (a m * (x - \<xi>)^m) * (b n * (x - \<xi>)^n)) sums (f x)"
-      using am az sm sums_zero_iff_shift [of m "(\<lambda>n. a n * (x - \<xi>) ^ n)" "f x"]
-      by (simp add: b_def monoid_mult_class.power_add algebra_simps)
-    then have "x \<noteq> \<xi> \<Longrightarrow> (\<lambda>n. b n * (x - \<xi>)^n) sums (f x / (a m * (x - \<xi>)^m))"
-      using am by (simp add: sums_mult_D)
-  } note bsums = this
-  then have  "norm (x - \<xi>) < r \<Longrightarrow> summable (\<lambda>n. b n * (x - \<xi>)^n)" for x
-    using sums_summable by (cases "x=\<xi>") auto
-  then have "r \<le> conv_radius b"
-    by (simp add: le_conv_radius_iff [where \<xi>=\<xi>])
-  then have "r/2 < conv_radius b"
-    using not_le order_trans r by fastforce
-  then have "continuous_on (cball \<xi> (r/2)) g"
-    using powser_continuous_suminf [of "r/2" b \<xi>] by (simp add: g_def)
-  then obtain s where "s>0"  "\<And>x. \<lbrakk>norm (x - \<xi>) \<le> s; norm (x - \<xi>) \<le> r/2\<rbrakk> \<Longrightarrow> dist (g x) (g \<xi>) < 1/2"
-    apply (rule continuous_onE [where x=\<xi> and e = "1/2"])
-    using r apply (auto simp: norm_minus_commute dist_norm)
-    done
-  moreover have "g \<xi> = 1"
-    by (simp add: g_def)
-  ultimately have gnz: "\<And>x. \<lbrakk>norm (x - \<xi>) \<le> s; norm (x - \<xi>) \<le> r/2\<rbrakk> \<Longrightarrow> (g x) \<noteq> 0"
-    by fastforce
-  have "f x \<noteq> 0" if "x \<noteq> \<xi>" "norm (x - \<xi>) \<le> s" "norm (x - \<xi>) \<le> r/2" for x
-    using bsums [of x] that gnz [of x]
-    apply (auto simp: g_def)
-    using r sums_iff by fastforce
-  then show ?thesis
-    apply (rule_tac s="min s (r/2)" in that)
-    using \<open>0 < r\<close> \<open>0 < s\<close> by (auto simp: dist_commute dist_norm)
-qed
-
-subsection \<open>Analytic continuation\<close>
-
-proposition isolated_zeros:
-  assumes holf: "f holomorphic_on S"
-      and "open S" "connected S" "\<xi> \<in> S" "f \<xi> = 0" "\<beta> \<in> S" "f \<beta> \<noteq> 0"
-    obtains r where "0 < r" and "ball \<xi> r \<subseteq> S" and
-        "\<And>z. z \<in> ball \<xi> r - {\<xi>} \<Longrightarrow> f z \<noteq> 0"
-proof -
-  obtain r where "0 < r" and r: "ball \<xi> r \<subseteq> S"
-    using \<open>open S\<close> \<open>\<xi> \<in> S\<close> open_contains_ball_eq by blast
-  have powf: "((\<lambda>n. (deriv ^^ n) f \<xi> / (fact n) * (z - \<xi>)^n) sums f z)" if "z \<in> ball \<xi> r" for z
-    apply (rule holomorphic_power_series [OF _ that])
-    apply (rule holomorphic_on_subset [OF holf r])
-    done
-  obtain m where m: "(deriv ^^ m) f \<xi> / (fact m) \<noteq> 0"
-    using holomorphic_fun_eq_0_on_connected [OF holf \<open>open S\<close> \<open>connected S\<close> _ \<open>\<xi> \<in> S\<close> \<open>\<beta> \<in> S\<close>] \<open>f \<beta> \<noteq> 0\<close>
-    by auto
-  then have "m \<noteq> 0" using assms(5) funpow_0 by fastforce
-  obtain s where "0 < s" and s: "\<And>z. z \<in> cball \<xi> s - {\<xi>} \<Longrightarrow> f z \<noteq> 0"
-    apply (rule powser_0_nonzero [OF \<open>0 < r\<close> powf \<open>f \<xi> = 0\<close> m])
-    using \<open>m \<noteq> 0\<close> by (auto simp: dist_commute dist_norm)
-  have "0 < min r s"  by (simp add: \<open>0 < r\<close> \<open>0 < s\<close>)
-  then show ?thesis
-    apply (rule that)
-    using r s by auto
-qed
-
-proposition analytic_continuation:
-  assumes holf: "f holomorphic_on S"
-      and "open S" and "connected S"
-      and "U \<subseteq> S" and "\<xi> \<in> S"
-      and "\<xi> islimpt U"
-      and fU0 [simp]: "\<And>z. z \<in> U \<Longrightarrow> f z = 0"
-      and "w \<in> S"
-    shows "f w = 0"
-proof -
-  obtain e where "0 < e" and e: "cball \<xi> e \<subseteq> S"
-    using \<open>open S\<close> \<open>\<xi> \<in> S\<close> open_contains_cball_eq by blast
-  define T where "T = cball \<xi> e \<inter> U"
-  have contf: "continuous_on (closure T) f"
-    by (metis T_def closed_cball closure_minimal e holf holomorphic_on_imp_continuous_on
-              holomorphic_on_subset inf.cobounded1)
-  have fT0 [simp]: "\<And>x. x \<in> T \<Longrightarrow> f x = 0"
-    by (simp add: T_def)
-  have "\<And>r. \<lbrakk>\<forall>e>0. \<exists>x'\<in>U. x' \<noteq> \<xi> \<and> dist x' \<xi> < e; 0 < r\<rbrakk> \<Longrightarrow> \<exists>x'\<in>cball \<xi> e \<inter> U. x' \<noteq> \<xi> \<and> dist x' \<xi> < r"
-    by (metis \<open>0 < e\<close> IntI dist_commute less_eq_real_def mem_cball min_less_iff_conj)
-  then have "\<xi> islimpt T" using \<open>\<xi> islimpt U\<close>
-    by (auto simp: T_def islimpt_approachable)
-  then have "\<xi> \<in> closure T"
-    by (simp add: closure_def)
-  then have "f \<xi> = 0"
-    by (auto simp: continuous_constant_on_closure [OF contf])
-  show ?thesis
-    apply (rule ccontr)
-    apply (rule isolated_zeros [OF holf \<open>open S\<close> \<open>connected S\<close> \<open>\<xi> \<in> S\<close> \<open>f \<xi> = 0\<close> \<open>w \<in> S\<close>], assumption)
-    by (metis open_ball \<open>\<xi> islimpt T\<close> centre_in_ball fT0 insertE insert_Diff islimptE)
-qed
-
-corollary analytic_continuation_open:
-  assumes "open s" and "open s'" and "s \<noteq> {}" and "connected s'"
-      and "s \<subseteq> s'"
-  assumes "f holomorphic_on s'" and "g holomorphic_on s'"
-      and "\<And>z. z \<in> s \<Longrightarrow> f z = g z"
-  assumes "z \<in> s'"
-  shows   "f z = g z"
-proof -
-  from \<open>s \<noteq> {}\<close> obtain \<xi> where "\<xi> \<in> s" by auto
-  with \<open>open s\<close> have \<xi>: "\<xi> islimpt s"
-    by (intro interior_limit_point) (auto simp: interior_open)
-  have "f z - g z = 0"
-    by (rule analytic_continuation[of "\<lambda>z. f z - g z" s' s \<xi>])
-       (insert assms \<open>\<xi> \<in> s\<close> \<xi>, auto intro: holomorphic_intros)
-  thus ?thesis by simp
-qed
-
-subsection\<open>Open mapping theorem\<close>
-
-lemma holomorphic_contract_to_zero:
-  assumes contf: "continuous_on (cball \<xi> r) f"
-      and holf: "f holomorphic_on ball \<xi> r"
-      and "0 < r"
-      and norm_less: "\<And>z. norm(\<xi> - z) = r \<Longrightarrow> norm(f \<xi>) < norm(f z)"
-  obtains z where "z \<in> ball \<xi> r" "f z = 0"
-proof -
-  { assume fnz: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w \<noteq> 0"
-    then have "0 < norm (f \<xi>)"
-      by (simp add: \<open>0 < r\<close>)
-    have fnz': "\<And>w. w \<in> cball \<xi> r \<Longrightarrow> f w \<noteq> 0"
-      by (metis norm_less dist_norm fnz less_eq_real_def mem_ball mem_cball norm_not_less_zero norm_zero)
-    have "frontier(cball \<xi> r) \<noteq> {}"
-      using \<open>0 < r\<close> by simp
-    define g where [abs_def]: "g z = inverse (f z)" for z
-    have contg: "continuous_on (cball \<xi> r) g"
-      unfolding g_def using contf continuous_on_inverse fnz' by blast
-    have holg: "g holomorphic_on ball \<xi> r"
-      unfolding g_def using fnz holf holomorphic_on_inverse by blast
-    have "frontier (cball \<xi> r) \<subseteq> cball \<xi> r"
-      by (simp add: subset_iff)
-    then have contf': "continuous_on (frontier (cball \<xi> r)) f"
-          and contg': "continuous_on (frontier (cball \<xi> r)) g"
-      by (blast intro: contf contg continuous_on_subset)+
-    have froc: "frontier(cball \<xi> r) \<noteq> {}"
-      using \<open>0 < r\<close> by simp
-    moreover have "continuous_on (frontier (cball \<xi> r)) (norm o f)"
-      using contf' continuous_on_compose continuous_on_norm_id by blast
-    ultimately obtain w where w: "w \<in> frontier(cball \<xi> r)"
-                          and now: "\<And>x. x \<in> frontier(cball \<xi> r) \<Longrightarrow> norm (f w) \<le> norm (f x)"
-      apply (rule bexE [OF continuous_attains_inf [OF compact_frontier [OF compact_cball]]])
-      apply simp
-      done
-    then have fw: "0 < norm (f w)"
-      by (simp add: fnz')
-    have "continuous_on (frontier (cball \<xi> r)) (norm o g)"
-      using contg' continuous_on_compose continuous_on_norm_id by blast
-    then obtain v where v: "v \<in> frontier(cball \<xi> r)"
-               and nov: "\<And>x. x \<in> frontier(cball \<xi> r) \<Longrightarrow> norm (g v) \<ge> norm (g x)"
-      apply (rule bexE [OF continuous_attains_sup [OF compact_frontier [OF compact_cball] froc]])
-      apply simp
-      done
-    then have fv: "0 < norm (f v)"
-      by (simp add: fnz')
-    have "norm ((deriv ^^ 0) g \<xi>) \<le> fact 0 * norm (g v) / r ^ 0"
-      by (rule Cauchy_inequality [OF holg contg \<open>0 < r\<close>]) (simp add: dist_norm nov)
-    then have "cmod (g \<xi>) \<le> norm (g v)"
-      by simp
-    with w have wr: "norm (\<xi> - w) = r" and nfw: "norm (f w) \<le> norm (f \<xi>)"
-      apply (simp_all add: dist_norm)
-      by (metis \<open>0 < cmod (f \<xi>)\<close> g_def less_imp_inverse_less norm_inverse not_le now order_trans v)
-    with fw have False
-      using norm_less by force
-  }
-  with that show ?thesis by blast
-qed
-
-theorem open_mapping_thm:
-  assumes holf: "f holomorphic_on S"
-      and S: "open S" and "connected S"
-      and "open U" and "U \<subseteq> S"
-      and fne: "\<not> f constant_on S"
-    shows "open (f ` U)"
-proof -
-  have *: "open (f ` U)"
-          if "U \<noteq> {}" and U: "open U" "connected U" and "f holomorphic_on U" and fneU: "\<And>x. \<exists>y \<in> U. f y \<noteq> x"
-          for U
-  proof (clarsimp simp: open_contains_ball)
-    fix \<xi> assume \<xi>: "\<xi> \<in> U"
-    show "\<exists>e>0. ball (f \<xi>) e \<subseteq> f ` U"
-    proof -
-      have hol: "(\<lambda>z. f z - f \<xi>) holomorphic_on U"
-        by (rule holomorphic_intros that)+
-      obtain s where "0 < s" and sbU: "ball \<xi> s \<subseteq> U"
-                 and sne: "\<And>z. z \<in> ball \<xi> s - {\<xi>} \<Longrightarrow> (\<lambda>z. f z - f \<xi>) z \<noteq> 0"
-        using isolated_zeros [OF hol U \<xi>]  by (metis fneU right_minus_eq)
-      obtain r where "0 < r" and r: "cball \<xi> r \<subseteq> ball \<xi> s"
-        apply (rule_tac r="s/2" in that)
-        using \<open>0 < s\<close> by auto
-      have "cball \<xi> r \<subseteq> U"
-        using sbU r by blast
-      then have frsbU: "frontier (cball \<xi> r) \<subseteq> U"
-        using Diff_subset frontier_def order_trans by fastforce
-      then have cof: "compact (frontier(cball \<xi> r))"
-        by blast
-      have frne: "frontier (cball \<xi> r) \<noteq> {}"
-        using \<open>0 < r\<close> by auto
-      have contfr: "continuous_on (frontier (cball \<xi> r)) (\<lambda>z. norm (f z - f \<xi>))"
-        by (metis continuous_on_norm continuous_on_subset frsbU hol holomorphic_on_imp_continuous_on)
-      obtain w where "norm (\<xi> - w) = r"
-                 and w: "(\<And>z. norm (\<xi> - z) = r \<Longrightarrow> norm (f w - f \<xi>) \<le> norm(f z - f \<xi>))"
-        apply (rule bexE [OF continuous_attains_inf [OF cof frne contfr]])
-        apply (simp add: dist_norm)
-        done
-      moreover define \<epsilon> where "\<epsilon> \<equiv> norm (f w - f \<xi>) / 3"
-      ultimately have "0 < \<epsilon>"
-        using \<open>0 < r\<close> dist_complex_def r sne by auto
-      have "ball (f \<xi>) \<epsilon> \<subseteq> f ` U"
-      proof
-        fix \<gamma>
-        assume \<gamma>: "\<gamma> \<in> ball (f \<xi>) \<epsilon>"
-        have *: "cmod (\<gamma> - f \<xi>) < cmod (\<gamma> - f z)" if "cmod (\<xi> - z) = r" for z
-        proof -
-          have lt: "cmod (f w - f \<xi>) / 3 < cmod (\<gamma> - f z)"
-            using w [OF that] \<gamma>
-            using dist_triangle2 [of "f \<xi>" "\<gamma>"  "f z"] dist_triangle2 [of "f \<xi>" "f z" \<gamma>]
-            by (simp add: \<epsilon>_def dist_norm norm_minus_commute)
-          show ?thesis
-            by (metis \<epsilon>_def dist_commute dist_norm less_trans lt mem_ball \<gamma>)
-       qed
-       have "continuous_on (cball \<xi> r) (\<lambda>z. \<gamma> - f z)"
-          apply (rule continuous_intros)+
-          using \<open>cball \<xi> r \<subseteq> U\<close> \<open>f holomorphic_on U\<close>
-          apply (blast intro: continuous_on_subset holomorphic_on_imp_continuous_on)
-          done
-        moreover have "(\<lambda>z. \<gamma> - f z) holomorphic_on ball \<xi> r"
-          apply (rule holomorphic_intros)+
-          apply (metis \<open>cball \<xi> r \<subseteq> U\<close> \<open>f holomorphic_on U\<close> holomorphic_on_subset interior_cball interior_subset)
-          done
-        ultimately obtain z where "z \<in> ball \<xi> r" "\<gamma> - f z = 0"
-          apply (rule holomorphic_contract_to_zero)
-          apply (blast intro!: \<open>0 < r\<close> *)+
-          done
-        then show "\<gamma> \<in> f ` U"
-          using \<open>cball \<xi> r \<subseteq> U\<close> by fastforce
-      qed
-      then show ?thesis using  \<open>0 < \<epsilon>\<close> by blast
-    qed
-  qed
-  have "open (f ` X)" if "X \<in> components U" for X
-  proof -
-    have holfU: "f holomorphic_on U"
-      using \<open>U \<subseteq> S\<close> holf holomorphic_on_subset by blast
-    have "X \<noteq> {}"
-      using that by (simp add: in_components_nonempty)
-    moreover have "open X"
-      using that \<open>open U\<close> open_components by auto
-    moreover have "connected X"
-      using that in_components_maximal by blast
-    moreover have "f holomorphic_on X"
-      by (meson that holfU holomorphic_on_subset in_components_maximal)
-    moreover have "\<exists>y\<in>X. f y \<noteq> x" for x
-    proof (rule ccontr)
-      assume not: "\<not> (\<exists>y\<in>X. f y \<noteq> x)"
-      have "X \<subseteq> S"
-        using \<open>U \<subseteq> S\<close> in_components_subset that by blast
-      obtain w where w: "w \<in> X" using \<open>X \<noteq> {}\<close> by blast
-      have wis: "w islimpt X"
-        using w \<open>open X\<close> interior_eq by auto
-      have hol: "(\<lambda>z. f z - x) holomorphic_on S"
-        by (simp add: holf holomorphic_on_diff)
-      with fne [unfolded constant_on_def]
-           analytic_continuation[OF hol S \<open>connected S\<close> \<open>X \<subseteq> S\<close> _ wis] not \<open>X \<subseteq> S\<close> w
-      show False by auto
-    qed
-    ultimately show ?thesis
-      by (rule *)
-  qed
-  then have "open (f ` \<Union>(components U))"
-    by (metis (no_types, lifting) imageE image_Union open_Union)
-  then show ?thesis
-    by force
-qed
-
-text\<open>No need for \<^term>\<open>S\<close> to be connected. But the nonconstant condition is stronger.\<close>
-corollary\<^marker>\<open>tag unimportant\<close> open_mapping_thm2:
-  assumes holf: "f holomorphic_on S"
-      and S: "open S"
-      and "open U" "U \<subseteq> S"
-      and fnc: "\<And>X. \<lbrakk>open X; X \<subseteq> S; X \<noteq> {}\<rbrakk> \<Longrightarrow> \<not> f constant_on X"
-    shows "open (f ` U)"
-proof -
-  have "S = \<Union>(components S)" by simp
-  with \<open>U \<subseteq> S\<close> have "U = (\<Union>C \<in> components S. C \<inter> U)" by auto
-  then have "f ` U = (\<Union>C \<in> components S. f ` (C \<inter> U))"
-    using image_UN by fastforce
-  moreover
-  { fix C assume "C \<in> components S"
-    with S \<open>C \<in> components S\<close> open_components in_components_connected
-    have C: "open C" "connected C" by auto
-    have "C \<subseteq> S"
-      by (metis \<open>C \<in> components S\<close> in_components_maximal)
-    have nf: "\<not> f constant_on C"
-      apply (rule fnc)
-      using C \<open>C \<subseteq> S\<close> \<open>C \<in> components S\<close> in_components_nonempty by auto
-    have "f holomorphic_on C"
-      by (metis holf holomorphic_on_subset \<open>C \<subseteq> S\<close>)
-    then have "open (f ` (C \<inter> U))"
-      apply (rule open_mapping_thm [OF _ C _ _ nf])
-      apply (simp add: C \<open>open U\<close> open_Int, blast)
-      done
-  } ultimately show ?thesis
-    by force
-qed
-
-corollary\<^marker>\<open>tag unimportant\<close> open_mapping_thm3:
-  assumes holf: "f holomorphic_on S"
-      and "open S" and injf: "inj_on f S"
-    shows  "open (f ` S)"
-apply (rule open_mapping_thm2 [OF holf])
-using assms
-apply (simp_all add:)
-using injective_not_constant subset_inj_on by blast
-
-subsection\<open>Maximum modulus principle\<close>
-
-text\<open>If \<^term>\<open>f\<close> is holomorphic, then its norm (modulus) cannot exhibit a true local maximum that is
-   properly within the domain of \<^term>\<open>f\<close>.\<close>
-
-proposition maximum_modulus_principle:
-  assumes holf: "f holomorphic_on S"
-      and S: "open S" and "connected S"
-      and "open U" and "U \<subseteq> S" and "\<xi> \<in> U"
-      and no: "\<And>z. z \<in> U \<Longrightarrow> norm(f z) \<le> norm(f \<xi>)"
-    shows "f constant_on S"
-proof (rule ccontr)
-  assume "\<not> f constant_on S"
-  then have "open (f ` U)"
-    using open_mapping_thm assms by blast
-  moreover have "\<not> open (f ` U)"
-  proof -
-    have "\<exists>t. cmod (f \<xi> - t) < e \<and> t \<notin> f ` U" if "0 < e" for e
-      apply (rule_tac x="if 0 < Re(f \<xi>) then f \<xi> + (e/2) else f \<xi> - (e/2)" in exI)
-      using that
-      apply (simp add: dist_norm)
-      apply (fastforce simp: cmod_Re_le_iff dest!: no dest: sym)
-      done
-    then show ?thesis
-      unfolding open_contains_ball by (metis \<open>\<xi> \<in> U\<close> contra_subsetD dist_norm imageI mem_ball)
-  qed
-  ultimately show False
-    by blast
-qed
-
-proposition maximum_modulus_frontier:
-  assumes holf: "f holomorphic_on (interior S)"
-      and contf: "continuous_on (closure S) f"
-      and bos: "bounded S"
-      and leB: "\<And>z. z \<in> frontier S \<Longrightarrow> norm(f z) \<le> B"
-      and "\<xi> \<in> S"
-    shows "norm(f \<xi>) \<le> B"
-proof -
-  have "compact (closure S)" using bos
-    by (simp add: bounded_closure compact_eq_bounded_closed)
-  moreover have "continuous_on (closure S) (cmod \<circ> f)"
-    using contf continuous_on_compose continuous_on_norm_id by blast
-  ultimately obtain z where zin: "z \<in> closure S" and z: "\<And>y. y \<in> closure S \<Longrightarrow> (cmod \<circ> f) y \<le> (cmod \<circ> f) z"
-    using continuous_attains_sup [of "closure S" "norm o f"] \<open>\<xi> \<in> S\<close> by auto
-  then consider "z \<in> frontier S" | "z \<in> interior S" using frontier_def by auto
-  then have "norm(f z) \<le> B"
-  proof cases
-    case 1 then show ?thesis using leB by blast
-  next
-    case 2
-    have zin: "z \<in> connected_component_set (interior S) z"
-      by (simp add: 2)
-    have "f constant_on (connected_component_set (interior S) z)"
-      apply (rule maximum_modulus_principle [OF _ _ _ _ _ zin])
-      apply (metis connected_component_subset holf holomorphic_on_subset)
-      apply (simp_all add: open_connected_component)
-      by (metis closure_subset comp_eq_dest_lhs  interior_subset subsetCE z connected_component_in)
-    then obtain c where c: "\<And>w. w \<in> connected_component_set (interior S) z \<Longrightarrow> f w = c"
-      by (auto simp: constant_on_def)
-    have "f ` closure(connected_component_set (interior S) z) \<subseteq> {c}"
-      apply (rule image_closure_subset)
-      apply (meson closure_mono connected_component_subset contf continuous_on_subset interior_subset)
-      using c
-      apply auto
-      done
-    then have cc: "\<And>w. w \<in> closure(connected_component_set (interior S) z) \<Longrightarrow> f w = c" by blast
-    have "frontier(connected_component_set (interior S) z) \<noteq> {}"
-      apply (simp add: frontier_eq_empty)
-      by (metis "2" bos bounded_interior connected_component_eq_UNIV connected_component_refl not_bounded_UNIV)
-    then obtain w where w: "w \<in> frontier(connected_component_set (interior S) z)"
-       by auto
-    then have "norm (f z) = norm (f w)"  by (simp add: "2" c cc frontier_def)
-    also have "... \<le> B"
-      apply (rule leB)
-      using w
-using frontier_interior_subset frontier_of_connected_component_subset by blast
-    finally show ?thesis .
-  qed
-  then show ?thesis
-    using z \<open>\<xi> \<in> S\<close> closure_subset by fastforce
-qed
-
-corollary\<^marker>\<open>tag unimportant\<close> maximum_real_frontier:
-  assumes holf: "f holomorphic_on (interior S)"
-      and contf: "continuous_on (closure S) f"
-      and bos: "bounded S"
-      and leB: "\<And>z. z \<in> frontier S \<Longrightarrow> Re(f z) \<le> B"
-      and "\<xi> \<in> S"
-    shows "Re(f \<xi>) \<le> B"
-using maximum_modulus_frontier [of "exp o f" S "exp B"]
-      Transcendental.continuous_on_exp holomorphic_on_compose holomorphic_on_exp assms
-by auto
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Factoring out a zero according to its order\<close>
-
-lemma holomorphic_factor_order_of_zero:
-  assumes holf: "f holomorphic_on S"
-      and os: "open S"
-      and "\<xi> \<in> S" "0 < n"
-      and dnz: "(deriv ^^ n) f \<xi> \<noteq> 0"
-      and dfz: "\<And>i. \<lbrakk>0 < i; i < n\<rbrakk> \<Longrightarrow> (deriv ^^ i) f \<xi> = 0"
-   obtains g r where "0 < r"
-                "g holomorphic_on ball \<xi> r"
-                "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w - f \<xi> = (w - \<xi>)^n * g w"
-                "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
-proof -
-  obtain r where "r>0" and r: "ball \<xi> r \<subseteq> S" using assms by (blast elim!: openE)
-  then have holfb: "f holomorphic_on ball \<xi> r"
-    using holf holomorphic_on_subset by blast
-  define g where "g w = suminf (\<lambda>i. (deriv ^^ (i + n)) f \<xi> / (fact(i + n)) * (w - \<xi>)^i)" for w
-  have sumsg: "(\<lambda>i. (deriv ^^ (i + n)) f \<xi> / (fact(i + n)) * (w - \<xi>)^i) sums g w"
-   and feq: "f w - f \<xi> = (w - \<xi>)^n * g w"
-       if w: "w \<in> ball \<xi> r" for w
-  proof -
-    define powf where "powf = (\<lambda>i. (deriv ^^ i) f \<xi>/(fact i) * (w - \<xi>)^i)"
-    have sing: "{..<n} - {i. powf i = 0} = (if f \<xi> = 0 then {} else {0})"
-      unfolding powf_def using \<open>0 < n\<close> dfz by (auto simp: dfz; metis funpow_0 not_gr0)
-    have "powf sums f w"
-      unfolding powf_def by (rule holomorphic_power_series [OF holfb w])
-    moreover have "(\<Sum>i<n. powf i) = f \<xi>"
-      apply (subst Groups_Big.comm_monoid_add_class.sum.setdiff_irrelevant [symmetric])
-      apply simp
-      apply (simp only: dfz sing)
-      apply (simp add: powf_def)
-      done
-    ultimately have fsums: "(\<lambda>i. powf (i+n)) sums (f w - f \<xi>)"
-      using w sums_iff_shift' by metis
-    then have *: "summable (\<lambda>i. (w - \<xi>) ^ n * ((deriv ^^ (i + n)) f \<xi> * (w - \<xi>) ^ i / fact (i + n)))"
-      unfolding powf_def using sums_summable
-      by (auto simp: power_add mult_ac)
-    have "summable (\<lambda>i. (deriv ^^ (i + n)) f \<xi> * (w - \<xi>) ^ i / fact (i + n))"
-    proof (cases "w=\<xi>")
-      case False then show ?thesis
-        using summable_mult [OF *, of "1 / (w - \<xi>) ^ n"] by simp
-    next
-      case True then show ?thesis
-        by (auto simp: Power.semiring_1_class.power_0_left intro!: summable_finite [of "{0}"]
-                 split: if_split_asm)
-    qed
-    then show sumsg: "(\<lambda>i. (deriv ^^ (i + n)) f \<xi> / (fact(i + n)) * (w - \<xi>)^i) sums g w"
-      by (simp add: summable_sums_iff g_def)
-    show "f w - f \<xi> = (w - \<xi>)^n * g w"
-      apply (rule sums_unique2)
-      apply (rule fsums [unfolded powf_def])
-      using sums_mult [OF sumsg, of "(w - \<xi>) ^ n"]
-      by (auto simp: power_add mult_ac)
-  qed
-  then have holg: "g holomorphic_on ball \<xi> r"
-    by (meson sumsg power_series_holomorphic)
-  then have contg: "continuous_on (ball \<xi> r) g"
-    by (blast intro: holomorphic_on_imp_continuous_on)
-  have "g \<xi> \<noteq> 0"
-    using dnz unfolding g_def
-    by (subst suminf_finite [of "{0}"]) auto
-  obtain d where "0 < d" and d: "\<And>w. w \<in> ball \<xi> d \<Longrightarrow> g w \<noteq> 0"
-    apply (rule exE [OF continuous_on_avoid [OF contg _ \<open>g \<xi> \<noteq> 0\<close>]])
-    using \<open>0 < r\<close>
-    apply force
-    by (metis \<open>0 < r\<close> less_trans mem_ball not_less_iff_gr_or_eq)
-  show ?thesis
-    apply (rule that [where g=g and r ="min r d"])
-    using \<open>0 < r\<close> \<open>0 < d\<close> holg
-    apply (auto simp: feq holomorphic_on_subset subset_ball d)
-    done
-qed
-
-
-lemma holomorphic_factor_order_of_zero_strong:
-  assumes holf: "f holomorphic_on S" "open S"  "\<xi> \<in> S" "0 < n"
-      and "(deriv ^^ n) f \<xi> \<noteq> 0"
-      and "\<And>i. \<lbrakk>0 < i; i < n\<rbrakk> \<Longrightarrow> (deriv ^^ i) f \<xi> = 0"
-   obtains g r where "0 < r"
-                "g holomorphic_on ball \<xi> r"
-                "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w - f \<xi> = ((w - \<xi>) * g w) ^ n"
-                "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
-proof -
-  obtain g r where "0 < r"
-               and holg: "g holomorphic_on ball \<xi> r"
-               and feq: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w - f \<xi> = (w - \<xi>)^n * g w"
-               and gne: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
-    by (auto intro: holomorphic_factor_order_of_zero [OF assms])
-  have con: "continuous_on (ball \<xi> r) (\<lambda>z. deriv g z / g z)"
-    by (rule continuous_intros) (auto simp: gne holg holomorphic_deriv holomorphic_on_imp_continuous_on)
-  have cd: "\<And>x. dist \<xi> x < r \<Longrightarrow> (\<lambda>z. deriv g z / g z) field_differentiable at x"
-    apply (rule derivative_intros)+
-    using holg mem_ball apply (blast intro: holomorphic_deriv holomorphic_on_imp_differentiable_at)
-    apply (metis open_ball at_within_open holg holomorphic_on_def mem_ball)
-    using gne mem_ball by blast
-  obtain h where h: "\<And>x. x \<in> ball \<xi> r \<Longrightarrow> (h has_field_derivative deriv g x / g x) (at x)"
-    apply (rule exE [OF holomorphic_convex_primitive [of "ball \<xi> r" "{}" "\<lambda>z. deriv g z / g z"]])
-    apply (auto simp: con cd)
-    apply (metis open_ball at_within_open mem_ball)
-    done
-  then have "continuous_on (ball \<xi> r) h"
-    by (metis open_ball holomorphic_on_imp_continuous_on holomorphic_on_open)
-  then have con: "continuous_on (ball \<xi> r) (\<lambda>x. exp (h x) / g x)"
-    by (auto intro!: continuous_intros simp add: holg holomorphic_on_imp_continuous_on gne)
-  have 0: "dist \<xi> x < r \<Longrightarrow> ((\<lambda>x. exp (h x) / g x) has_field_derivative 0) (at x)" for x
-    apply (rule h derivative_eq_intros | simp)+
-    apply (rule DERIV_deriv_iff_field_differentiable [THEN iffD2])
-    using holg apply (auto simp: holomorphic_on_imp_differentiable_at gne h)
-    done
-  obtain c where c: "\<And>x. x \<in> ball \<xi> r \<Longrightarrow> exp (h x) / g x = c"
-    by (rule DERIV_zero_connected_constant [of "ball \<xi> r" "{}" "\<lambda>x. exp(h x) / g x"]) (auto simp: con 0)
-  have hol: "(\<lambda>z. exp ((Ln (inverse c) + h z) / of_nat n)) holomorphic_on ball \<xi> r"
-    apply (rule holomorphic_on_compose [unfolded o_def, where g = exp])
-    apply (rule holomorphic_intros)+
-    using h holomorphic_on_open apply blast
-    apply (rule holomorphic_intros)+
-    using \<open>0 < n\<close> apply simp
-    apply (rule holomorphic_intros)+
-    done
-  show ?thesis
-    apply (rule that [where g="\<lambda>z. exp((Ln(inverse c) + h z)/n)" and r =r])
-    using \<open>0 < r\<close> \<open>0 < n\<close>
-    apply (auto simp: feq power_mult_distrib exp_divide_power_eq c [symmetric])
-    apply (rule hol)
-    apply (simp add: Transcendental.exp_add gne)
-    done
-qed
-
-
-lemma
-  fixes k :: "'a::wellorder"
-  assumes a_def: "a == LEAST x. P x" and P: "P k"
-  shows def_LeastI: "P a" and def_Least_le: "a \<le> k"
-unfolding a_def
-by (rule LeastI Least_le; rule P)+
-
-lemma holomorphic_factor_zero_nonconstant:
-  assumes holf: "f holomorphic_on S" and S: "open S" "connected S"
-      and "\<xi> \<in> S" "f \<xi> = 0"
-      and nonconst: "\<not> f constant_on S"
-   obtains g r n
-      where "0 < n"  "0 < r"  "ball \<xi> r \<subseteq> S"
-            "g holomorphic_on ball \<xi> r"
-            "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w = (w - \<xi>)^n * g w"
-            "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
-proof (cases "\<forall>n>0. (deriv ^^ n) f \<xi> = 0")
-  case True then show ?thesis
-    using holomorphic_fun_eq_const_on_connected [OF holf S _ \<open>\<xi> \<in> S\<close>] nonconst by (simp add: constant_on_def)
-next
-  case False
-  then obtain n0 where "n0 > 0" and n0: "(deriv ^^ n0) f \<xi> \<noteq> 0" by blast
-  obtain r0 where "r0 > 0" "ball \<xi> r0 \<subseteq> S" using S openE \<open>\<xi> \<in> S\<close> by auto
-  define n where "n \<equiv> LEAST n. (deriv ^^ n) f \<xi> \<noteq> 0"
-  have n_ne: "(deriv ^^ n) f \<xi> \<noteq> 0"
-    by (rule def_LeastI [OF n_def]) (rule n0)
-  then have "0 < n" using \<open>f \<xi> = 0\<close>
-    using funpow_0 by fastforce
-  have n_min: "\<And>k. k < n \<Longrightarrow> (deriv ^^ k) f \<xi> = 0"
-    using def_Least_le [OF n_def] not_le by blast
-  then obtain g r1
-    where  "0 < r1" "g holomorphic_on ball \<xi> r1"
-           "\<And>w. w \<in> ball \<xi> r1 \<Longrightarrow> f w = (w - \<xi>) ^ n * g w"
-           "\<And>w. w \<in> ball \<xi> r1 \<Longrightarrow> g w \<noteq> 0"
-    by (auto intro: holomorphic_factor_order_of_zero [OF holf \<open>open S\<close> \<open>\<xi> \<in> S\<close> \<open>n > 0\<close> n_ne] simp: \<open>f \<xi> = 0\<close>)
-  then show ?thesis
-    apply (rule_tac g=g and r="min r0 r1" and n=n in that)
-    using \<open>0 < n\<close> \<open>0 < r0\<close> \<open>0 < r1\<close> \<open>ball \<xi> r0 \<subseteq> S\<close>
-    apply (auto simp: subset_ball intro: holomorphic_on_subset)
-    done
-qed
-
-
-lemma holomorphic_lower_bound_difference:
-  assumes holf: "f holomorphic_on S" and S: "open S" "connected S"
-      and "\<xi> \<in> S" and "\<phi> \<in> S"
-      and fne: "f \<phi> \<noteq> f \<xi>"
-   obtains k n r
-      where "0 < k"  "0 < r"
-            "ball \<xi> r \<subseteq> S"
-            "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> k * norm(w - \<xi>)^n \<le> norm(f w - f \<xi>)"
-proof -
-  define n where "n = (LEAST n. 0 < n \<and> (deriv ^^ n) f \<xi> \<noteq> 0)"
-  obtain n0 where "0 < n0" and n0: "(deriv ^^ n0) f \<xi> \<noteq> 0"
-    using fne holomorphic_fun_eq_const_on_connected [OF holf S] \<open>\<xi> \<in> S\<close> \<open>\<phi> \<in> S\<close> by blast
-  then have "0 < n" and n_ne: "(deriv ^^ n) f \<xi> \<noteq> 0"
-    unfolding n_def by (metis (mono_tags, lifting) LeastI)+
-  have n_min: "\<And>k. \<lbrakk>0 < k; k < n\<rbrakk> \<Longrightarrow> (deriv ^^ k) f \<xi> = 0"
-    unfolding n_def by (blast dest: not_less_Least)
-  then obtain g r
-    where "0 < r" and holg: "g holomorphic_on ball \<xi> r"
-      and fne: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w - f \<xi> = (w - \<xi>) ^ n * g w"
-      and gnz: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
-      by (auto intro: holomorphic_factor_order_of_zero  [OF holf \<open>open S\<close> \<open>\<xi> \<in> S\<close> \<open>n > 0\<close> n_ne])
-  obtain e where "e>0" and e: "ball \<xi> e \<subseteq> S" using assms by (blast elim!: openE)
-  then have holfb: "f holomorphic_on ball \<xi> e"
-    using holf holomorphic_on_subset by blast
-  define d where "d = (min e r) / 2"
-  have "0 < d" using \<open>0 < r\<close> \<open>0 < e\<close> by (simp add: d_def)
-  have "d < r"
-    using \<open>0 < r\<close> by (auto simp: d_def)
-  then have cbb: "cball \<xi> d \<subseteq> ball \<xi> r"
-    by (auto simp: cball_subset_ball_iff)
-  then have "g holomorphic_on cball \<xi> d"
-    by (rule holomorphic_on_subset [OF holg])
-  then have "closed (g ` cball \<xi> d)"
-    by (simp add: compact_imp_closed compact_continuous_image holomorphic_on_imp_continuous_on)
-  moreover have "g ` cball \<xi> d \<noteq> {}"
-    using \<open>0 < d\<close> by auto
-  ultimately obtain x where x: "x \<in> g ` cball \<xi> d" and "\<And>y. y \<in> g ` cball \<xi> d \<Longrightarrow> dist 0 x \<le> dist 0 y"
-    by (rule distance_attains_inf) blast
-  then have leg: "\<And>w. w \<in> cball \<xi> d \<Longrightarrow> norm x \<le> norm (g w)"
-    by auto
-  have "ball \<xi> d \<subseteq> cball \<xi> d" by auto
-  also have "... \<subseteq> ball \<xi> e" using \<open>0 < d\<close> d_def by auto
-  also have "... \<subseteq> S" by (rule e)
-  finally have dS: "ball \<xi> d \<subseteq> S" .
-  moreover have "x \<noteq> 0" using gnz x \<open>d < r\<close> by auto
-  ultimately show ?thesis
-    apply (rule_tac k="norm x" and n=n and r=d in that)
-    using \<open>d < r\<close> leg
-    apply (auto simp: \<open>0 < d\<close> fne norm_mult norm_power algebra_simps mult_right_mono)
-    done
-qed
-
-lemma
-  assumes holf: "f holomorphic_on (S - {\<xi>})" and \<xi>: "\<xi> \<in> interior S"
-    shows holomorphic_on_extend_lim:
-          "(\<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S - {\<xi>}. g z = f z)) \<longleftrightarrow>
-           ((\<lambda>z. (z - \<xi>) * f z) \<longlongrightarrow> 0) (at \<xi>)"
-          (is "?P = ?Q")
-     and holomorphic_on_extend_bounded:
-          "(\<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S - {\<xi>}. g z = f z)) \<longleftrightarrow>
-           (\<exists>B. eventually (\<lambda>z. norm(f z) \<le> B) (at \<xi>))"
-          (is "?P = ?R")
-proof -
-  obtain \<delta> where "0 < \<delta>" and \<delta>: "ball \<xi> \<delta> \<subseteq> S"
-    using \<xi> mem_interior by blast
-  have "?R" if holg: "g holomorphic_on S" and gf: "\<And>z. z \<in> S - {\<xi>} \<Longrightarrow> g z = f z" for g
-  proof -
-    have *: "\<forall>\<^sub>F z in at \<xi>. dist (g z) (g \<xi>) < 1 \<longrightarrow> cmod (f z) \<le> cmod (g \<xi>) + 1"
-      apply (simp add: eventually_at)
-      apply (rule_tac x="\<delta>" in exI)
-      using \<delta> \<open>0 < \<delta>\<close>
-      apply (clarsimp simp:)
-      apply (drule_tac c=x in subsetD)
-      apply (simp add: dist_commute)
-      by (metis DiffI add.commute diff_le_eq dist_norm gf le_less_trans less_eq_real_def norm_triangle_ineq2 singletonD)
-    have "continuous_on (interior S) g"
-      by (meson continuous_on_subset holg holomorphic_on_imp_continuous_on interior_subset)
-    then have "\<And>x. x \<in> interior S \<Longrightarrow> (g \<longlongrightarrow> g x) (at x)"
-      using continuous_on_interior continuous_within holg holomorphic_on_imp_continuous_on by blast
-    then have "(g \<longlongrightarrow> g \<xi>) (at \<xi>)"
-      by (simp add: \<xi>)
-    then show ?thesis
-      apply (rule_tac x="norm(g \<xi>) + 1" in exI)
-      apply (rule eventually_mp [OF * tendstoD [where e=1]], auto)
-      done
-  qed
-  moreover have "?Q" if "\<forall>\<^sub>F z in at \<xi>. cmod (f z) \<le> B" for B
-    by (rule lim_null_mult_right_bounded [OF _ that]) (simp add: LIM_zero)
-  moreover have "?P" if "(\<lambda>z. (z - \<xi>) * f z) \<midarrow>\<xi>\<rightarrow> 0"
-  proof -
-    define h where [abs_def]: "h z = (z - \<xi>)^2 * f z" for z
-    have h0: "(h has_field_derivative 0) (at \<xi>)"
-      apply (simp add: h_def has_field_derivative_iff)
-      apply (rule Lim_transform_within [OF that, of 1])
-      apply (auto simp: field_split_simps power2_eq_square)
-      done
-    have holh: "h holomorphic_on S"
-    proof (simp add: holomorphic_on_def, clarify)
-      fix z assume "z \<in> S"
-      show "h field_differentiable at z within S"
-      proof (cases "z = \<xi>")
-        case True then show ?thesis
-          using field_differentiable_at_within field_differentiable_def h0 by blast
-      next
-        case False
-        then have "f field_differentiable at z within S"
-          using holomorphic_onD [OF holf, of z] \<open>z \<in> S\<close>
-          unfolding field_differentiable_def has_field_derivative_iff
-          by (force intro: exI [where x="dist \<xi> z"] elim: Lim_transform_within_set [unfolded eventually_at])
-        then show ?thesis
-          by (simp add: h_def power2_eq_square derivative_intros)
-      qed
-    qed
-    define g where [abs_def]: "g z = (if z = \<xi> then deriv h \<xi> else (h z - h \<xi>) / (z - \<xi>))" for z
-    have holg: "g holomorphic_on S"
-      unfolding g_def by (rule pole_lemma [OF holh \<xi>])
-    show ?thesis
-      apply (rule_tac x="\<lambda>z. if z = \<xi> then deriv g \<xi> else (g z - g \<xi>)/(z - \<xi>)" in exI)
-      apply (rule conjI)
-      apply (rule pole_lemma [OF holg \<xi>])
-      apply (auto simp: g_def power2_eq_square divide_simps)
-      using h0 apply (simp add: h0 DERIV_imp_deriv h_def power2_eq_square)
-      done
-  qed
-  ultimately show "?P = ?Q" and "?P = ?R"
-    by meson+
-qed
-
-lemma pole_at_infinity:
-  assumes holf: "f holomorphic_on UNIV" and lim: "((inverse o f) \<longlongrightarrow> l) at_infinity"
-  obtains a n where "\<And>z. f z = (\<Sum>i\<le>n. a i * z^i)"
-proof (cases "l = 0")
-  case False
-  with tendsto_inverse [OF lim] show ?thesis
-    apply (rule_tac a="(\<lambda>n. inverse l)" and n=0 in that)
-    apply (simp add: Liouville_weak [OF holf, of "inverse l"])
-    done
-next
-  case True
-  then have [simp]: "l = 0" .
-  show ?thesis
-  proof (cases "\<exists>r. 0 < r \<and> (\<forall>z \<in> ball 0 r - {0}. f(inverse z) \<noteq> 0)")
-    case True
-      then obtain r where "0 < r" and r: "\<And>z. z \<in> ball 0 r - {0} \<Longrightarrow> f(inverse z) \<noteq> 0"
-             by auto
-      have 1: "inverse \<circ> f \<circ> inverse holomorphic_on ball 0 r - {0}"
-        by (rule holomorphic_on_compose holomorphic_intros holomorphic_on_subset [OF holf] | force simp: r)+
-      have 2: "0 \<in> interior (ball 0 r)"
-        using \<open>0 < r\<close> by simp
-      have "\<exists>B. 0<B \<and> eventually (\<lambda>z. cmod ((inverse \<circ> f \<circ> inverse) z) \<le> B) (at 0)"
-        apply (rule exI [where x=1])
-        apply simp
-        using tendstoD [OF lim [unfolded lim_at_infinity_0] zero_less_one]
-        apply (rule eventually_mono)
-        apply (simp add: dist_norm)
-        done
-      with holomorphic_on_extend_bounded [OF 1 2]
-      obtain g where holg: "g holomorphic_on ball 0 r"
-                 and geq: "\<And>z. z \<in> ball 0 r - {0} \<Longrightarrow> g z = (inverse \<circ> f \<circ> inverse) z"
-        by meson
-      have ifi0: "(inverse \<circ> f \<circ> inverse) \<midarrow>0\<rightarrow> 0"
-        using \<open>l = 0\<close> lim lim_at_infinity_0 by blast
-      have g2g0: "g \<midarrow>0\<rightarrow> g 0"
-        using \<open>0 < r\<close> centre_in_ball continuous_at continuous_on_eq_continuous_at holg
-        by (blast intro: holomorphic_on_imp_continuous_on)
-      have g2g1: "g \<midarrow>0\<rightarrow> 0"
-        apply (rule Lim_transform_within_open [OF ifi0 open_ball [of 0 r]])
-        using \<open>0 < r\<close> by (auto simp: geq)
-      have [simp]: "g 0 = 0"
-        by (rule tendsto_unique [OF _ g2g0 g2g1]) simp
-      have "ball 0 r - {0::complex} \<noteq> {}"
-        using \<open>0 < r\<close>
-        apply (clarsimp simp: ball_def dist_norm)
-        apply (drule_tac c="of_real r/2" in subsetD, auto)
-        done
-      then obtain w::complex where "w \<noteq> 0" and w: "norm w < r" by force
-      then have "g w \<noteq> 0" by (simp add: geq r)
-      obtain B n e where "0 < B" "0 < e" "e \<le> r"
-                     and leg: "\<And>w. norm w < e \<Longrightarrow> B * cmod w ^ n \<le> cmod (g w)"
-        apply (rule holomorphic_lower_bound_difference [OF holg open_ball connected_ball, of 0 w])
-        using \<open>0 < r\<close> w \<open>g w \<noteq> 0\<close> by (auto simp: ball_subset_ball_iff)
-      have "cmod (f z) \<le> cmod z ^ n / B" if "2/e \<le> cmod z" for z
-      proof -
-        have ize: "inverse z \<in> ball 0 e - {0}" using that \<open>0 < e\<close>
-          by (auto simp: norm_divide field_split_simps algebra_simps)
-        then have [simp]: "z \<noteq> 0" and izr: "inverse z \<in> ball 0 r - {0}" using  \<open>e \<le> r\<close>
-          by auto
-        then have [simp]: "f z \<noteq> 0"
-          using r [of "inverse z"] by simp
-        have [simp]: "f z = inverse (g (inverse z))"
-          using izr geq [of "inverse z"] by simp
-        show ?thesis using ize leg [of "inverse z"]  \<open>0 < B\<close>  \<open>0 < e\<close>
-          by (simp add: field_split_simps norm_divide algebra_simps)
-      qed
-      then show ?thesis
-        apply (rule_tac a = "\<lambda>k. (deriv ^^ k) f 0 / (fact k)" and n=n in that)
-        apply (rule_tac A = "2/e" and B = "1/B" in Liouville_polynomial [OF holf], simp)
-        done
-  next
-    case False
-    then have fi0: "\<And>r. r > 0 \<Longrightarrow> \<exists>z\<in>ball 0 r - {0}. f (inverse z) = 0"
-      by simp
-    have fz0: "f z = 0" if "0 < r" and lt1: "\<And>x. x \<noteq> 0 \<Longrightarrow> cmod x < r \<Longrightarrow> inverse (cmod (f (inverse x))) < 1"
-              for z r
-    proof -
-      have f0: "(f \<longlongrightarrow> 0) at_infinity"
-      proof -
-        have DIM_complex[intro]: "2 \<le> DIM(complex)"  \<comment> \<open>should not be necessary!\<close>
-          by simp
-        from lt1 have "f (inverse x) \<noteq> 0 \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> cmod x < r \<Longrightarrow> 1 < cmod (f (inverse x))" for x
-          using one_less_inverse by force
-        then have **: "cmod (f (inverse x)) \<le> 1 \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> cmod x < r \<Longrightarrow> f (inverse x) = 0" for x
-          by force
-        then have *: "(f \<circ> inverse) ` (ball 0 r - {0}) \<subseteq> {0} \<union> - ball 0 1"
-          by force
-        have "continuous_on (inverse ` (ball 0 r - {0})) f"
-          using continuous_on_subset holf holomorphic_on_imp_continuous_on by blast
-        then have "connected ((f \<circ> inverse) ` (ball 0 r - {0}))"
-          apply (intro connected_continuous_image continuous_intros)
-          apply (force intro: connected_punctured_ball)+
-          done
-        then have "{0} \<inter> (f \<circ> inverse) ` (ball 0 r - {0}) = {} \<or> - ball 0 1 \<inter> (f \<circ> inverse) ` (ball 0 r - {0}) = {}"
-          by (rule connected_closedD) (use * in auto)
-        then have "w \<noteq> 0 \<Longrightarrow> cmod w < r \<Longrightarrow> f (inverse w) = 0" for w
-          using fi0 **[of w] \<open>0 < r\<close>
-          apply (auto simp add: inf.commute [of "- ball 0 1"] Diff_eq [symmetric] image_subset_iff dest: less_imp_le)
-           apply fastforce
-          apply (drule bspec [of _ _ w])
-           apply (auto dest: less_imp_le)
-          done
-        then show ?thesis
-          apply (simp add: lim_at_infinity_0)
-          apply (rule tendsto_eventually)
-          apply (simp add: eventually_at)
-          apply (rule_tac x=r in exI)
-          apply (simp add: \<open>0 < r\<close> dist_norm)
-          done
-      qed
-      obtain w where "w \<in> ball 0 r - {0}" and "f (inverse w) = 0"
-        using False \<open>0 < r\<close> by blast
-      then show ?thesis
-        by (auto simp: f0 Liouville_weak [OF holf, of 0])
-    qed
-    show ?thesis
-      apply (rule that [of "\<lambda>n. 0" 0])
-      using lim [unfolded lim_at_infinity_0]
-      apply (simp add: Lim_at dist_norm norm_inverse)
-      apply (drule_tac x=1 in spec)
-      using fz0 apply auto
-      done
-    qed
-qed
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>Entire proper functions are precisely the non-trivial polynomials\<close>
-
-lemma proper_map_polyfun:
-    fixes c :: "nat \<Rightarrow> 'a::{real_normed_div_algebra,heine_borel}"
-  assumes "closed S" and "compact K" and c: "c i \<noteq> 0" "1 \<le> i" "i \<le> n"
-    shows "compact (S \<inter> {z. (\<Sum>i\<le>n. c i * z^i) \<in> K})"
-proof -
-  obtain B where "B > 0" and B: "\<And>x. x \<in> K \<Longrightarrow> norm x \<le> B"
-    by (metis compact_imp_bounded \<open>compact K\<close> bounded_pos)
-  have *: "norm x \<le> b"
-            if "\<And>x. b \<le> norm x \<Longrightarrow> B + 1 \<le> norm (\<Sum>i\<le>n. c i * x ^ i)"
-               "(\<Sum>i\<le>n. c i * x ^ i) \<in> K"  for b x
-  proof -
-    have "norm (\<Sum>i\<le>n. c i * x ^ i) \<le> B"
-      using B that by blast
-    moreover have "\<not> B + 1 \<le> B"
-      by simp
-    ultimately show "norm x \<le> b"
-      using that by (metis (no_types) less_eq_real_def not_less order_trans)
-  qed
-  have "bounded {z. (\<Sum>i\<le>n. c i * z ^ i) \<in> K}"
-    using polyfun_extremal [where c=c and B="B+1", OF c]
-    by (auto simp: bounded_pos eventually_at_infinity_pos *)
-  moreover have "closed ((\<lambda>z. (\<Sum>i\<le>n. c i * z ^ i)) -` K)"
-    apply (intro allI continuous_closed_vimage continuous_intros)
-    using \<open>compact K\<close> compact_eq_bounded_closed by blast
-  ultimately show ?thesis
-    using closed_Int_compact [OF \<open>closed S\<close>] compact_eq_bounded_closed
-    by (auto simp add: vimage_def)
-qed
-
-lemma proper_map_polyfun_univ:
-    fixes c :: "nat \<Rightarrow> 'a::{real_normed_div_algebra,heine_borel}"
-  assumes "compact K" "c i \<noteq> 0" "1 \<le> i" "i \<le> n"
-    shows "compact ({z. (\<Sum>i\<le>n. c i * z^i) \<in> K})"
-  using proper_map_polyfun [of UNIV K c i n] assms by simp
-
-lemma proper_map_polyfun_eq:
-  assumes "f holomorphic_on UNIV"
-    shows "(\<forall>k. compact k \<longrightarrow> compact {z. f z \<in> k}) \<longleftrightarrow>
-           (\<exists>c n. 0 < n \<and> (c n \<noteq> 0) \<and> f = (\<lambda>z. \<Sum>i\<le>n. c i * z^i))"
-          (is "?lhs = ?rhs")
-proof
-  assume compf [rule_format]: ?lhs
-  have 2: "\<exists>k. 0 < k \<and> a k \<noteq> 0 \<and> f = (\<lambda>z. \<Sum>i \<le> k. a i * z ^ i)"
-        if "\<And>z. f z = (\<Sum>i\<le>n. a i * z ^ i)" for a n
-  proof (cases "\<forall>i\<le>n. 0<i \<longrightarrow> a i = 0")
-    case True
-    then have [simp]: "\<And>z. f z = a 0"
-      by (simp add: that sum.atMost_shift)
-    have False using compf [of "{a 0}"] by simp
-    then show ?thesis ..
-  next
-    case False
-    then obtain k where k: "0 < k" "k\<le>n" "a k \<noteq> 0" by force
-    define m where "m = (GREATEST k. k\<le>n \<and> a k \<noteq> 0)"
-    have m: "m\<le>n \<and> a m \<noteq> 0"
-      unfolding m_def
-      apply (rule GreatestI_nat [where b = n])
-      using k apply auto
-      done
-    have [simp]: "a i = 0" if "m < i" "i \<le> n" for i
-      using Greatest_le_nat [where b = "n" and P = "\<lambda>k. k\<le>n \<and> a k \<noteq> 0"]
-      using m_def not_le that by auto
-    have "k \<le> m"
-      unfolding m_def
-      apply (rule Greatest_le_nat [where b = "n"])
-      using k apply auto
-      done
-    with k m show ?thesis
-      by (rule_tac x=m in exI) (auto simp: that comm_monoid_add_class.sum.mono_neutral_right)
-  qed
-  have "((inverse \<circ> f) \<longlongrightarrow> 0) at_infinity"
-  proof (rule Lim_at_infinityI)
-    fix e::real assume "0 < e"
-    with compf [of "cball 0 (inverse e)"]
-    show "\<exists>B. \<forall>x. B \<le> cmod x \<longrightarrow> dist ((inverse \<circ> f) x) 0 \<le> e"
-      apply simp
-      apply (clarsimp simp add: compact_eq_bounded_closed bounded_pos norm_inverse)
-      apply (rule_tac x="b+1" in exI)
-      apply (metis inverse_inverse_eq less_add_same_cancel2 less_imp_inverse_less add.commute not_le not_less_iff_gr_or_eq order_trans zero_less_one)
-      done
-  qed
-  then show ?rhs
-    apply (rule pole_at_infinity [OF assms])
-    using 2 apply blast
-    done
-next
-  assume ?rhs
-  then obtain c n where "0 < n" "c n \<noteq> 0" "f = (\<lambda>z. \<Sum>i\<le>n. c i * z ^ i)" by blast
-  then have "compact {z. f z \<in> k}" if "compact k" for k
-    by (auto intro: proper_map_polyfun_univ [OF that])
-  then show ?lhs by blast
-qed
-
-subsection \<open>Relating invertibility and nonvanishing of derivative\<close>
-
-lemma has_complex_derivative_locally_injective:
-  assumes holf: "f holomorphic_on S"
-      and S: "\<xi> \<in> S" "open S"
-      and dnz: "deriv f \<xi> \<noteq> 0"
-  obtains r where "r > 0" "ball \<xi> r \<subseteq> S" "inj_on f (ball \<xi> r)"
-proof -
-  have *: "\<exists>d>0. \<forall>x. dist \<xi> x < d \<longrightarrow> onorm (\<lambda>v. deriv f x * v - deriv f \<xi> * v) < e" if "e > 0" for e
-  proof -
-    have contdf: "continuous_on S (deriv f)"
-      by (simp add: holf holomorphic_deriv holomorphic_on_imp_continuous_on \<open>open S\<close>)
-    obtain \<delta> where "\<delta>>0" and \<delta>: "\<And>x. \<lbrakk>x \<in> S; dist x \<xi> \<le> \<delta>\<rbrakk> \<Longrightarrow> cmod (deriv f x - deriv f \<xi>) \<le> e/2"
-      using continuous_onE [OF contdf \<open>\<xi> \<in> S\<close>, of "e/2"] \<open>0 < e\<close>
-      by (metis dist_complex_def half_gt_zero less_imp_le)
-    obtain \<epsilon> where "\<epsilon>>0" "ball \<xi> \<epsilon> \<subseteq> S"
-      by (metis openE [OF \<open>open S\<close> \<open>\<xi> \<in> S\<close>])
-    with \<open>\<delta>>0\<close> have "\<exists>\<delta>>0. \<forall>x. dist \<xi> x < \<delta> \<longrightarrow> onorm (\<lambda>v. deriv f x * v - deriv f \<xi> * v) \<le> e/2"
-      apply (rule_tac x="min \<delta> \<epsilon>" in exI)
-      apply (intro conjI allI impI Operator_Norm.onorm_le)
-      apply simp
-      apply (simp only: Rings.ring_class.left_diff_distrib [symmetric] norm_mult)
-      apply (rule mult_right_mono [OF \<delta>])
-      apply (auto simp: dist_commute Rings.ordered_semiring_class.mult_right_mono \<delta>)
-      done
-    with \<open>e>0\<close> show ?thesis by force
-  qed
-  have "inj ((*) (deriv f \<xi>))"
-    using dnz by simp
-  then obtain g' where g': "linear g'" "g' \<circ> (*) (deriv f \<xi>) = id"
-    using linear_injective_left_inverse [of "(*) (deriv f \<xi>)"]
-    by (auto simp: linear_times)
-  show ?thesis
-    apply (rule has_derivative_locally_injective [OF S, where f=f and f' = "\<lambda>z h. deriv f z * h" and g' = g'])
-    using g' *
-    apply (simp_all add: linear_conv_bounded_linear that)
-    using DERIV_deriv_iff_field_differentiable has_field_derivative_imp_has_derivative holf
-        holomorphic_on_imp_differentiable_at \<open>open S\<close> apply blast
-    done
-qed
-
-lemma has_complex_derivative_locally_invertible:
-  assumes holf: "f holomorphic_on S"
-      and S: "\<xi> \<in> S" "open S"
-      and dnz: "deriv f \<xi> \<noteq> 0"
-  obtains r where "r > 0" "ball \<xi> r \<subseteq> S" "open (f `  (ball \<xi> r))" "inj_on f (ball \<xi> r)"
-proof -
-  obtain r where "r > 0" "ball \<xi> r \<subseteq> S" "inj_on f (ball \<xi> r)"
-    by (blast intro: that has_complex_derivative_locally_injective [OF assms])
-  then have \<xi>: "\<xi> \<in> ball \<xi> r" by simp
-  then have nc: "\<not> f constant_on ball \<xi> r"
-    using \<open>inj_on f (ball \<xi> r)\<close> injective_not_constant by fastforce
-  have holf': "f holomorphic_on ball \<xi> r"
-    using \<open>ball \<xi> r \<subseteq> S\<close> holf holomorphic_on_subset by blast
-  have "open (f ` ball \<xi> r)"
-    apply (rule open_mapping_thm [OF holf'])
-    using nc apply auto
-    done
-  then show ?thesis
-    using \<open>0 < r\<close> \<open>ball \<xi> r \<subseteq> S\<close> \<open>inj_on f (ball \<xi> r)\<close> that  by blast
-qed
-
-lemma holomorphic_injective_imp_regular:
-  assumes holf: "f holomorphic_on S"
-      and "open S" and injf: "inj_on f S"
-      and "\<xi> \<in> S"
-    shows "deriv f \<xi> \<noteq> 0"
-proof -
-  obtain r where "r>0" and r: "ball \<xi> r \<subseteq> S" using assms by (blast elim!: openE)
-  have holf': "f holomorphic_on ball \<xi> r"
-    using \<open>ball \<xi> r \<subseteq> S\<close> holf holomorphic_on_subset by blast
-  show ?thesis
-  proof (cases "\<forall>n>0. (deriv ^^ n) f \<xi> = 0")
-    case True
-    have fcon: "f w = f \<xi>" if "w \<in> ball \<xi> r" for w
-      apply (rule holomorphic_fun_eq_const_on_connected [OF holf'])
-      using True \<open>0 < r\<close> that by auto
-    have False
-      using fcon [of "\<xi> + r/2"] \<open>0 < r\<close> r injf unfolding inj_on_def
-      by (metis \<open>\<xi> \<in> S\<close> contra_subsetD dist_commute fcon mem_ball perfect_choose_dist)
-    then show ?thesis ..
-  next
-    case False
-    then obtain n0 where n0: "n0 > 0 \<and> (deriv ^^ n0) f \<xi> \<noteq> 0" by blast
-    define n where [abs_def]: "n = (LEAST n. n > 0 \<and> (deriv ^^ n) f \<xi> \<noteq> 0)"
-    have n_ne: "n > 0" "(deriv ^^ n) f \<xi> \<noteq> 0"
-      using def_LeastI [OF n_def n0] by auto
-    have n_min: "\<And>k. 0 < k \<Longrightarrow> k < n \<Longrightarrow> (deriv ^^ k) f \<xi> = 0"
-      using def_Least_le [OF n_def] not_le by auto
-    obtain g \<delta> where "0 < \<delta>"
-             and holg: "g holomorphic_on ball \<xi> \<delta>"
-             and fd: "\<And>w. w \<in> ball \<xi> \<delta> \<Longrightarrow> f w - f \<xi> = ((w - \<xi>) * g w) ^ n"
-             and gnz: "\<And>w. w \<in> ball \<xi> \<delta> \<Longrightarrow> g w \<noteq> 0"
-      apply (rule holomorphic_factor_order_of_zero_strong [OF holf \<open>open S\<close> \<open>\<xi> \<in> S\<close> n_ne])
-      apply (blast intro: n_min)+
-      done
-    show ?thesis
-    proof (cases "n=1")
-      case True
-      with n_ne show ?thesis by auto
-    next
-      case False
-      have holgw: "(\<lambda>w. (w - \<xi>) * g w) holomorphic_on ball \<xi> (min r \<delta>)"
-        apply (rule holomorphic_intros)+
-        using holg by (simp add: holomorphic_on_subset subset_ball)
-      have gd: "\<And>w. dist \<xi> w < \<delta> \<Longrightarrow> (g has_field_derivative deriv g w) (at w)"
-        using holg
-        by (simp add: DERIV_deriv_iff_field_differentiable holomorphic_on_def at_within_open_NO_MATCH)
-      have *: "\<And>w. w \<in> ball \<xi> (min r \<delta>)
-            \<Longrightarrow> ((\<lambda>w. (w - \<xi>) * g w) has_field_derivative ((w - \<xi>) * deriv g w + g w))
-                (at w)"
-        by (rule gd derivative_eq_intros | simp)+
-      have [simp]: "deriv (\<lambda>w. (w - \<xi>) * g w) \<xi> \<noteq> 0"
-        using * [of \<xi>] \<open>0 < \<delta>\<close> \<open>0 < r\<close> by (simp add: DERIV_imp_deriv gnz)
-      obtain T where "\<xi> \<in> T" "open T" and Tsb: "T \<subseteq> ball \<xi> (min r \<delta>)" and oimT: "open ((\<lambda>w. (w - \<xi>) * g w) ` T)"
-        apply (rule has_complex_derivative_locally_invertible [OF holgw, of \<xi>])
-        using \<open>0 < r\<close> \<open>0 < \<delta>\<close>
-        apply (simp_all add:)
-        by (meson open_ball centre_in_ball)
-      define U where "U = (\<lambda>w. (w - \<xi>) * g w) ` T"
-      have "open U" by (metis oimT U_def)
-      have "0 \<in> U"
-        apply (auto simp: U_def)
-        apply (rule image_eqI [where x = \<xi>])
-        apply (auto simp: \<open>\<xi> \<in> T\<close>)
-        done
-      then obtain \<epsilon> where "\<epsilon>>0" and \<epsilon>: "cball 0 \<epsilon> \<subseteq> U"
-        using \<open>open U\<close> open_contains_cball by blast
-      then have "\<epsilon> * exp(2 * of_real pi * \<i> * (0/n)) \<in> cball 0 \<epsilon>"
-                "\<epsilon> * exp(2 * of_real pi * \<i> * (1/n)) \<in> cball 0 \<epsilon>"
-        by (auto simp: norm_mult)
-      with \<epsilon> have "\<epsilon> * exp(2 * of_real pi * \<i> * (0/n)) \<in> U"
-                  "\<epsilon> * exp(2 * of_real pi * \<i> * (1/n)) \<in> U" by blast+
-      then obtain y0 y1 where "y0 \<in> T" and y0: "(y0 - \<xi>) * g y0 = \<epsilon> * exp(2 * of_real pi * \<i> * (0/n))"
-                          and "y1 \<in> T" and y1: "(y1 - \<xi>) * g y1 = \<epsilon> * exp(2 * of_real pi * \<i> * (1/n))"
-        by (auto simp: U_def)
-      then have "y0 \<in> ball \<xi> \<delta>" "y1 \<in> ball \<xi> \<delta>" using Tsb by auto
-      moreover have "y0 \<noteq> y1"
-        using y0 y1 \<open>\<epsilon> > 0\<close> complex_root_unity_eq_1 [of n 1] \<open>n > 0\<close> False by auto
-      moreover have "T \<subseteq> S"
-        by (meson Tsb min.cobounded1 order_trans r subset_ball)
-      ultimately have False
-        using inj_onD [OF injf, of y0 y1] \<open>y0 \<in> T\<close> \<open>y1 \<in> T\<close>
-        using fd [of y0] fd [of y1] complex_root_unity [of n 1] n_ne
-        apply (simp add: y0 y1 power_mult_distrib)
-        apply (force simp: algebra_simps)
-        done
-      then show ?thesis ..
-    qed
-  qed
-qed
-
-text\<open>Hence a nice clean inverse function theorem\<close>
-
-lemma has_field_derivative_inverse_strong:
-  fixes f :: "'a::{euclidean_space,real_normed_field} \<Rightarrow> 'a"
-  shows "\<lbrakk>DERIV f x :> f'; f' \<noteq> 0; open S; x \<in> S; continuous_on S f;
-         \<And>z. z \<in> S \<Longrightarrow> g (f z) = z\<rbrakk>
-         \<Longrightarrow> DERIV g (f x) :> inverse (f')"
-  unfolding has_field_derivative_def
-  by (rule has_derivative_inverse_strong [of S x f g]) auto
-
-lemma has_field_derivative_inverse_strong_x:
-  fixes f :: "'a::{euclidean_space,real_normed_field} \<Rightarrow> 'a"
-  shows  "\<lbrakk>DERIV f (g y) :> f'; f' \<noteq> 0; open S; continuous_on S f; g y \<in> S; f(g y) = y;
-           \<And>z. z \<in> S \<Longrightarrow> g (f z) = z\<rbrakk>
-          \<Longrightarrow> DERIV g y :> inverse (f')"
-  unfolding has_field_derivative_def
-  by (rule has_derivative_inverse_strong_x [of S g y f]) auto
-
-proposition holomorphic_has_inverse:
-  assumes holf: "f holomorphic_on S"
-      and "open S" and injf: "inj_on f S"
-  obtains g where "g holomorphic_on (f ` S)"
-                  "\<And>z. z \<in> S \<Longrightarrow> deriv f z * deriv g (f z) = 1"
-                  "\<And>z. z \<in> S \<Longrightarrow> g(f z) = z"
-proof -
-  have ofs: "open (f ` S)"
-    by (rule open_mapping_thm3 [OF assms])
-  have contf: "continuous_on S f"
-    by (simp add: holf holomorphic_on_imp_continuous_on)
-  have *: "(the_inv_into S f has_field_derivative inverse (deriv f z)) (at (f z))" if "z \<in> S" for z
-  proof -
-    have 1: "(f has_field_derivative deriv f z) (at z)"
-      using DERIV_deriv_iff_field_differentiable \<open>z \<in> S\<close> \<open>open S\<close> holf holomorphic_on_imp_differentiable_at
-      by blast
-    have 2: "deriv f z \<noteq> 0"
-      using \<open>z \<in> S\<close> \<open>open S\<close> holf holomorphic_injective_imp_regular injf by blast
-    show ?thesis
-      apply (rule has_field_derivative_inverse_strong [OF 1 2 \<open>open S\<close> \<open>z \<in> S\<close>])
-       apply (simp add: holf holomorphic_on_imp_continuous_on)
-      by (simp add: injf the_inv_into_f_f)
-  qed
-  show ?thesis
-    proof
-      show "the_inv_into S f holomorphic_on f ` S"
-        by (simp add: holomorphic_on_open ofs) (blast intro: *)
-    next
-      fix z assume "z \<in> S"
-      have "deriv f z \<noteq> 0"
-        using \<open>z \<in> S\<close> \<open>open S\<close> holf holomorphic_injective_imp_regular injf by blast
-      then show "deriv f z * deriv (the_inv_into S f) (f z) = 1"
-        using * [OF \<open>z \<in> S\<close>]  by (simp add: DERIV_imp_deriv)
-    next
-      fix z assume "z \<in> S"
-      show "the_inv_into S f (f z) = z"
-        by (simp add: \<open>z \<in> S\<close> injf the_inv_into_f_f)
-  qed
-qed
-
-subsection\<open>The Schwarz Lemma\<close>
-
-lemma Schwarz1:
-  assumes holf: "f holomorphic_on S"
-      and contf: "continuous_on (closure S) f"
-      and S: "open S" "connected S"
-      and boS: "bounded S"
-      and "S \<noteq> {}"
-  obtains w where "w \<in> frontier S"
-       "\<And>z. z \<in> closure S \<Longrightarrow> norm (f z) \<le> norm (f w)"
-proof -
-  have connf: "continuous_on (closure S) (norm o f)"
-    using contf continuous_on_compose continuous_on_norm_id by blast
-  have coc: "compact (closure S)"
-    by (simp add: \<open>bounded S\<close> bounded_closure compact_eq_bounded_closed)
-  then obtain x where x: "x \<in> closure S" and xmax: "\<And>z. z \<in> closure S \<Longrightarrow> norm(f z) \<le> norm(f x)"
-    apply (rule bexE [OF continuous_attains_sup [OF _ _ connf]])
-    using \<open>S \<noteq> {}\<close> apply auto
-    done
-  then show ?thesis
-  proof (cases "x \<in> frontier S")
-    case True
-    then show ?thesis using that xmax by blast
-  next
-    case False
-    then have "x \<in> S"
-      using \<open>open S\<close> frontier_def interior_eq x by auto
-    then have "f constant_on S"
-      apply (rule maximum_modulus_principle [OF holf S \<open>open S\<close> order_refl])
-      using closure_subset apply (blast intro: xmax)
-      done
-    then have "f constant_on (closure S)"
-      by (rule constant_on_closureI [OF _ contf])
-    then obtain c where c: "\<And>x. x \<in> closure S \<Longrightarrow> f x = c"
-      by (meson constant_on_def)
-    obtain w where "w \<in> frontier S"
-      by (metis coc all_not_in_conv assms(6) closure_UNIV frontier_eq_empty not_compact_UNIV)
-    then show ?thesis
-      by (simp add: c frontier_def that)
-  qed
-qed
-
-lemma Schwarz2:
- "\<lbrakk>f holomorphic_on ball 0 r;
-    0 < s; ball w s \<subseteq> ball 0 r;
-    \<And>z. norm (w-z) < s \<Longrightarrow> norm(f z) \<le> norm(f w)\<rbrakk>
-    \<Longrightarrow> f constant_on ball 0 r"
-by (rule maximum_modulus_principle [where U = "ball w s" and \<xi> = w]) (simp_all add: dist_norm)
-
-lemma Schwarz3:
-  assumes holf: "f holomorphic_on (ball 0 r)" and [simp]: "f 0 = 0"
-  obtains h where "h holomorphic_on (ball 0 r)" and "\<And>z. norm z < r \<Longrightarrow> f z = z * (h z)" and "deriv f 0 = h 0"
-proof -
-  define h where "h z = (if z = 0 then deriv f 0 else f z / z)" for z
-  have d0: "deriv f 0 = h 0"
-    by (simp add: h_def)
-  moreover have "h holomorphic_on (ball 0 r)"
-    by (rule pole_theorem_open_0 [OF holf, of 0]) (auto simp: h_def)
-  moreover have "norm z < r \<Longrightarrow> f z = z * h z" for z
-    by (simp add: h_def)
-  ultimately show ?thesis
-    using that by blast
-qed
-
-proposition Schwarz_Lemma:
-  assumes holf: "f holomorphic_on (ball 0 1)" and [simp]: "f 0 = 0"
-      and no: "\<And>z. norm z < 1 \<Longrightarrow> norm (f z) < 1"
-      and \<xi>: "norm \<xi> < 1"
-    shows "norm (f \<xi>) \<le> norm \<xi>" and "norm(deriv f 0) \<le> 1"
-      and "((\<exists>z. norm z < 1 \<and> z \<noteq> 0 \<and> norm(f z) = norm z)
-            \<or> norm(deriv f 0) = 1)
-           \<Longrightarrow> \<exists>\<alpha>. (\<forall>z. norm z < 1 \<longrightarrow> f z = \<alpha> * z) \<and> norm \<alpha> = 1"
-      (is "?P \<Longrightarrow> ?Q")
-proof -
-  obtain h where holh: "h holomorphic_on (ball 0 1)"
-             and fz_eq: "\<And>z. norm z < 1 \<Longrightarrow> f z = z * (h z)" and df0: "deriv f 0 = h 0"
-    by (rule Schwarz3 [OF holf]) auto
-  have noh_le: "norm (h z) \<le> 1" if z: "norm z < 1" for z
-  proof -
-    have "norm (h z) < a" if a: "1 < a" for a
-    proof -
-      have "max (inverse a) (norm z) < 1"
-        using z a by (simp_all add: inverse_less_1_iff)
-      then obtain r where r: "max (inverse a) (norm z) < r" and "r < 1"
-        using Rats_dense_in_real by blast
-      then have nzr: "norm z < r" and ira: "inverse r < a"
-        using z a less_imp_inverse_less by force+
-      then have "0 < r"
-        by (meson norm_not_less_zero not_le order.strict_trans2)
-      have holh': "h holomorphic_on ball 0 r"
-        by (meson holh \<open>r < 1\<close> holomorphic_on_subset less_eq_real_def subset_ball)
-      have conth': "continuous_on (cball 0 r) h"
-        by (meson \<open>r < 1\<close> dual_order.trans holh holomorphic_on_imp_continuous_on holomorphic_on_subset mem_ball_0 mem_cball_0 not_less subsetI)
-      obtain w where w: "norm w = r" and lenw: "\<And>z. norm z < r \<Longrightarrow> norm(h z) \<le> norm(h w)"
-        apply (rule Schwarz1 [OF holh']) using conth' \<open>0 < r\<close> by auto
-      have "h w = f w / w" using fz_eq \<open>r < 1\<close> nzr w by auto
-      then have "cmod (h z) < inverse r"
-        by (metis \<open>0 < r\<close> \<open>r < 1\<close> divide_strict_right_mono inverse_eq_divide
-                  le_less_trans lenw no norm_divide nzr w)
-      then show ?thesis using ira by linarith
-    qed
-    then show "norm (h z) \<le> 1"
-      using not_le by blast
-  qed
-  show "cmod (f \<xi>) \<le> cmod \<xi>"
-  proof (cases "\<xi> = 0")
-    case True then show ?thesis by auto
-  next
-    case False
-    then show ?thesis
-      by (simp add: noh_le fz_eq \<xi> mult_left_le norm_mult)
-  qed
-  show no_df0: "norm(deriv f 0) \<le> 1"
-    by (simp add: \<open>\<And>z. cmod z < 1 \<Longrightarrow> cmod (h z) \<le> 1\<close> df0)
-  show "?Q" if "?P"
-    using that
-  proof
-    assume "\<exists>z. cmod z < 1 \<and> z \<noteq> 0 \<and> cmod (f z) = cmod z"
-    then obtain \<gamma> where \<gamma>: "cmod \<gamma> < 1" "\<gamma> \<noteq> 0" "cmod (f \<gamma>) = cmod \<gamma>" by blast
-    then have [simp]: "norm (h \<gamma>) = 1"
-      by (simp add: fz_eq norm_mult)
-    have "ball \<gamma> (1 - cmod \<gamma>) \<subseteq> ball 0 1"
-      by (simp add: ball_subset_ball_iff)
-    moreover have "\<And>z. cmod (\<gamma> - z) < 1 - cmod \<gamma> \<Longrightarrow> cmod (h z) \<le> cmod (h \<gamma>)"
-      apply (simp add: algebra_simps)
-      by (metis add_diff_cancel_left' diff_diff_eq2 le_less_trans noh_le norm_triangle_ineq4)
-    ultimately obtain c where c: "\<And>z. norm z < 1 \<Longrightarrow> h z = c"
-      using Schwarz2 [OF holh, of "1 - norm \<gamma>" \<gamma>, unfolded constant_on_def] \<gamma> by auto
-    then have "norm c = 1"
-      using \<gamma> by force
-    with c show ?thesis
-      using fz_eq by auto
-  next
-    assume [simp]: "cmod (deriv f 0) = 1"
-    then obtain c where c: "\<And>z. norm z < 1 \<Longrightarrow> h z = c"
-      using Schwarz2 [OF holh zero_less_one, of 0, unfolded constant_on_def] df0 noh_le
-      by auto
-    moreover have "norm c = 1"  using df0 c by auto
-    ultimately show ?thesis
-      using fz_eq by auto
-  qed
-qed
-
-corollary Schwarz_Lemma':
-  assumes holf: "f holomorphic_on (ball 0 1)" and [simp]: "f 0 = 0"
-      and no: "\<And>z. norm z < 1 \<Longrightarrow> norm (f z) < 1"
-    shows "((\<forall>\<xi>. norm \<xi> < 1 \<longrightarrow> norm (f \<xi>) \<le> norm \<xi>)
-            \<and> norm(deriv f 0) \<le> 1)
-            \<and> (((\<exists>z. norm z < 1 \<and> z \<noteq> 0 \<and> norm(f z) = norm z)
-              \<or> norm(deriv f 0) = 1)
-              \<longrightarrow> (\<exists>\<alpha>. (\<forall>z. norm z < 1 \<longrightarrow> f z = \<alpha> * z) \<and> norm \<alpha> = 1))"
-  using Schwarz_Lemma [OF assms]
-  by (metis (no_types) norm_eq_zero zero_less_one)
-
-subsection\<open>The Schwarz reflection principle\<close>
-
-lemma hol_pal_lem0:
-  assumes "d \<bullet> a \<le> k" "k \<le> d \<bullet> b"
-  obtains c where
-     "c \<in> closed_segment a b" "d \<bullet> c = k"
-     "\<And>z. z \<in> closed_segment a c \<Longrightarrow> d \<bullet> z \<le> k"
-     "\<And>z. z \<in> closed_segment c b \<Longrightarrow> k \<le> d \<bullet> z"
-proof -
-  obtain c where cin: "c \<in> closed_segment a b" and keq: "k = d \<bullet> c"
-    using connected_ivt_hyperplane [of "closed_segment a b" a b d k]
-    by (auto simp: assms)
-  have "closed_segment a c \<subseteq> {z. d \<bullet> z \<le> k}"  "closed_segment c b \<subseteq> {z. k \<le> d \<bullet> z}"
-    unfolding segment_convex_hull using assms keq
-    by (auto simp: convex_halfspace_le convex_halfspace_ge hull_minimal)
-  then show ?thesis using cin that by fastforce
-qed
-
-lemma hol_pal_lem1:
-  assumes "convex S" "open S"
-      and abc: "a \<in> S" "b \<in> S" "c \<in> S"
-          "d \<noteq> 0" and lek: "d \<bullet> a \<le> k" "d \<bullet> b \<le> k" "d \<bullet> c \<le> k"
-      and holf1: "f holomorphic_on {z. z \<in> S \<and> d \<bullet> z < k}"
-      and contf: "continuous_on S f"
-    shows "contour_integral (linepath a b) f +
-           contour_integral (linepath b c) f +
-           contour_integral (linepath c a) f = 0"
-proof -
-  have "interior (convex hull {a, b, c}) \<subseteq> interior(S \<inter> {x. d \<bullet> x \<le> k})"
-    apply (rule interior_mono)
-    apply (rule hull_minimal)
-     apply (simp add: abc lek)
-    apply (rule convex_Int [OF \<open>convex S\<close> convex_halfspace_le])
-    done
-  also have "... \<subseteq> {z \<in> S. d \<bullet> z < k}"
-    by (force simp: interior_open [OF \<open>open S\<close>] \<open>d \<noteq> 0\<close>)
-  finally have *: "interior (convex hull {a, b, c}) \<subseteq> {z \<in> S. d \<bullet> z < k}" .
-  have "continuous_on (convex hull {a,b,c}) f"
-    using \<open>convex S\<close> contf abc continuous_on_subset subset_hull
-    by fastforce
-  moreover have "f holomorphic_on interior (convex hull {a,b,c})"
-    by (rule holomorphic_on_subset [OF holf1 *])
-  ultimately show ?thesis
-    using Cauchy_theorem_triangle_interior has_chain_integral_chain_integral3
-      by blast
-qed
-
-lemma hol_pal_lem2:
-  assumes S: "convex S" "open S"
-      and abc: "a \<in> S" "b \<in> S" "c \<in> S"
-      and "d \<noteq> 0" and lek: "d \<bullet> a \<le> k" "d \<bullet> b \<le> k"
-      and holf1: "f holomorphic_on {z. z \<in> S \<and> d \<bullet> z < k}"
-      and holf2: "f holomorphic_on {z. z \<in> S \<and> k < d \<bullet> z}"
-      and contf: "continuous_on S f"
-    shows "contour_integral (linepath a b) f +
-           contour_integral (linepath b c) f +
-           contour_integral (linepath c a) f = 0"
-proof (cases "d \<bullet> c \<le> k")
-  case True show ?thesis
-    by (rule hol_pal_lem1 [OF S abc \<open>d \<noteq> 0\<close> lek True holf1 contf])
-next
-  case False
-  then have "d \<bullet> c > k" by force
-  obtain a' where a': "a' \<in> closed_segment b c" and "d \<bullet> a' = k"
-     and ba': "\<And>z. z \<in> closed_segment b a' \<Longrightarrow> d \<bullet> z \<le> k"
-     and a'c: "\<And>z. z \<in> closed_segment a' c \<Longrightarrow> k \<le> d \<bullet> z"
-    apply (rule hol_pal_lem0 [of d b k c, OF \<open>d \<bullet> b \<le> k\<close>])
-    using False by auto
-  obtain b' where b': "b' \<in> closed_segment a c" and "d \<bullet> b' = k"
-     and ab': "\<And>z. z \<in> closed_segment a b' \<Longrightarrow> d \<bullet> z \<le> k"
-     and b'c: "\<And>z. z \<in> closed_segment b' c \<Longrightarrow> k \<le> d \<bullet> z"
-    apply (rule hol_pal_lem0 [of d a k c, OF \<open>d \<bullet> a \<le> k\<close>])
-    using False by auto
-  have a'b': "a' \<in> S \<and> b' \<in> S"
-    using a' abc b' convex_contains_segment \<open>convex S\<close> by auto
-  have "continuous_on (closed_segment c a) f"
-    by (meson abc contf continuous_on_subset convex_contains_segment \<open>convex S\<close>)
-  then have 1: "contour_integral (linepath c a) f =
-                contour_integral (linepath c b') f + contour_integral (linepath b' a) f"
-    apply (rule contour_integral_split_linepath)
-    using b' by (simp add: closed_segment_commute)
-  have "continuous_on (closed_segment b c) f"
-    by (meson abc contf continuous_on_subset convex_contains_segment \<open>convex S\<close>)
-  then have 2: "contour_integral (linepath b c) f =
-                contour_integral (linepath b a') f + contour_integral (linepath a' c) f"
-    by (rule contour_integral_split_linepath [OF _ a'])
-  have 3: "contour_integral (reversepath (linepath b' a')) f =
-                - contour_integral (linepath b' a') f"
-    by (rule contour_integral_reversepath [OF valid_path_linepath])
-  have fcd_le: "f field_differentiable at x"
-               if "x \<in> interior S \<and> x \<in> interior {x. d \<bullet> x \<le> k}" for x
-  proof -
-    have "f holomorphic_on S \<inter> {c. d \<bullet> c < k}"
-      by (metis (no_types) Collect_conj_eq Collect_mem_eq holf1)
-    then have "\<exists>C D. x \<in> interior C \<inter> interior D \<and> f holomorphic_on interior C \<inter> interior D"
-      using that
-      by (metis Collect_mem_eq Int_Collect \<open>d \<noteq> 0\<close> interior_halfspace_le interior_open \<open>open S\<close>)
-    then show "f field_differentiable at x"
-      by (metis at_within_interior holomorphic_on_def interior_Int interior_interior)
-  qed
-  have ab_le: "\<And>x. x \<in> closed_segment a b \<Longrightarrow> d \<bullet> x \<le> k"
-  proof -
-    fix x :: complex
-    assume "x \<in> closed_segment a b"
-    then have "\<And>C. x \<in> C \<or> b \<notin> C \<or> a \<notin> C \<or> \<not> convex C"
-      by (meson contra_subsetD convex_contains_segment)
-    then show "d \<bullet> x \<le> k"
-      by (metis lek convex_halfspace_le mem_Collect_eq)
-  qed
-  have "continuous_on (S \<inter> {x. d \<bullet> x \<le> k}) f" using contf
-    by (simp add: continuous_on_subset)
-  then have "(f has_contour_integral 0)
-         (linepath a b +++ linepath b a' +++ linepath a' b' +++ linepath b' a)"
-    apply (rule Cauchy_theorem_convex [where K = "{}"])
-    apply (simp_all add: path_image_join convex_Int convex_halfspace_le \<open>convex S\<close> fcd_le ab_le
-                closed_segment_subset abc a'b' ba')
-    by (metis \<open>d \<bullet> a' = k\<close> \<open>d \<bullet> b' = k\<close> convex_contains_segment convex_halfspace_le lek(1) mem_Collect_eq order_refl)
-  then have 4: "contour_integral (linepath a b) f +
-                contour_integral (linepath b a') f +
-                contour_integral (linepath a' b') f +
-                contour_integral (linepath b' a) f = 0"
-    by (rule has_chain_integral_chain_integral4)
-  have fcd_ge: "f field_differentiable at x"
-               if "x \<in> interior S \<and> x \<in> interior {x. k \<le> d \<bullet> x}" for x
-  proof -
-    have f2: "f holomorphic_on S \<inter> {c. k < d \<bullet> c}"
-      by (metis (full_types) Collect_conj_eq Collect_mem_eq holf2)
-    have f3: "interior S = S"
-      by (simp add: interior_open \<open>open S\<close>)
-    then have "x \<in> S \<inter> interior {c. k \<le> d \<bullet> c}"
-      using that by simp
-    then show "f field_differentiable at x"
-      using f3 f2 unfolding holomorphic_on_def
-      by (metis (no_types) \<open>d \<noteq> 0\<close> at_within_interior interior_Int interior_halfspace_ge interior_interior)
-  qed
-  have "continuous_on (S \<inter> {x. k \<le> d \<bullet> x}) f" using contf
-    by (simp add: continuous_on_subset)
-  then have "(f has_contour_integral 0) (linepath a' c +++ linepath c b' +++ linepath b' a')"
-    apply (rule Cauchy_theorem_convex [where K = "{}"])
-    apply (simp_all add: path_image_join convex_Int convex_halfspace_ge \<open>convex S\<close>
-                      fcd_ge closed_segment_subset abc a'b' a'c)
-    by (metis \<open>d \<bullet> a' = k\<close> b'c closed_segment_commute convex_contains_segment
-              convex_halfspace_ge ends_in_segment(2) mem_Collect_eq order_refl)
-  then have 5: "contour_integral (linepath a' c) f + contour_integral (linepath c b') f + contour_integral (linepath b' a') f = 0"
-    by (rule has_chain_integral_chain_integral3)
-  show ?thesis
-    using 1 2 3 4 5 by (metis add.assoc eq_neg_iff_add_eq_0 reversepath_linepath)
-qed
-
-lemma hol_pal_lem3:
-  assumes S: "convex S" "open S"
-      and abc: "a \<in> S" "b \<in> S" "c \<in> S"
-      and "d \<noteq> 0" and lek: "d \<bullet> a \<le> k"
-      and holf1: "f holomorphic_on {z. z \<in> S \<and> d \<bullet> z < k}"
-      and holf2: "f holomorphic_on {z. z \<in> S \<and> k < d \<bullet> z}"
-      and contf: "continuous_on S f"
-    shows "contour_integral (linepath a b) f +
-           contour_integral (linepath b c) f +
-           contour_integral (linepath c a) f = 0"
-proof (cases "d \<bullet> b \<le> k")
-  case True show ?thesis
-    by (rule hol_pal_lem2 [OF S abc \<open>d \<noteq> 0\<close> lek True holf1 holf2 contf])
-next
-  case False
-  show ?thesis
-  proof (cases "d \<bullet> c \<le> k")
-    case True
-    have "contour_integral (linepath c a) f +
-          contour_integral (linepath a b) f +
-          contour_integral (linepath b c) f = 0"
-      by (rule hol_pal_lem2 [OF S \<open>c \<in> S\<close> \<open>a \<in> S\<close> \<open>b \<in> S\<close> \<open>d \<noteq> 0\<close> \<open>d \<bullet> c \<le> k\<close> lek holf1 holf2 contf])
-    then show ?thesis
-      by (simp add: algebra_simps)
-  next
-    case False
-    have "contour_integral (linepath b c) f +
-          contour_integral (linepath c a) f +
-          contour_integral (linepath a b) f = 0"
-      apply (rule hol_pal_lem2 [OF S \<open>b \<in> S\<close> \<open>c \<in> S\<close> \<open>a \<in> S\<close>, of "-d" "-k"])
-      using \<open>d \<noteq> 0\<close> \<open>\<not> d \<bullet> b \<le> k\<close> False by (simp_all add: holf1 holf2 contf)
-    then show ?thesis
-      by (simp add: algebra_simps)
-  qed
-qed
-
-lemma hol_pal_lem4:
-  assumes S: "convex S" "open S"
-      and abc: "a \<in> S" "b \<in> S" "c \<in> S" and "d \<noteq> 0"
-      and holf1: "f holomorphic_on {z. z \<in> S \<and> d \<bullet> z < k}"
-      and holf2: "f holomorphic_on {z. z \<in> S \<and> k < d \<bullet> z}"
-      and contf: "continuous_on S f"
-    shows "contour_integral (linepath a b) f +
-           contour_integral (linepath b c) f +
-           contour_integral (linepath c a) f = 0"
-proof (cases "d \<bullet> a \<le> k")
-  case True show ?thesis
-    by (rule hol_pal_lem3 [OF S abc \<open>d \<noteq> 0\<close> True holf1 holf2 contf])
-next
-  case False
-  show ?thesis
-    apply (rule hol_pal_lem3 [OF S abc, of "-d" "-k"])
-    using \<open>d \<noteq> 0\<close> False by (simp_all add: holf1 holf2 contf)
-qed
-
-lemma holomorphic_on_paste_across_line:
-  assumes S: "open S" and "d \<noteq> 0"
-      and holf1: "f holomorphic_on (S \<inter> {z. d \<bullet> z < k})"
-      and holf2: "f holomorphic_on (S \<inter> {z. k < d \<bullet> z})"
-      and contf: "continuous_on S f"
-    shows "f holomorphic_on S"
-proof -
-  have *: "\<exists>t. open t \<and> p \<in> t \<and> continuous_on t f \<and>
-               (\<forall>a b c. convex hull {a, b, c} \<subseteq> t \<longrightarrow>
-                         contour_integral (linepath a b) f +
-                         contour_integral (linepath b c) f +
-                         contour_integral (linepath c a) f = 0)"
-          if "p \<in> S" for p
-  proof -
-    obtain e where "e>0" and e: "ball p e \<subseteq> S"
-      using \<open>p \<in> S\<close> openE S by blast
-    then have "continuous_on (ball p e) f"
-      using contf continuous_on_subset by blast
-    moreover have "f holomorphic_on {z. dist p z < e \<and> d \<bullet> z < k}"
-      apply (rule holomorphic_on_subset [OF holf1])
-      using e by auto
-    moreover have "f holomorphic_on {z. dist p z < e \<and> k < d \<bullet> z}"
-      apply (rule holomorphic_on_subset [OF holf2])
-      using e by auto
-    ultimately show ?thesis
-      apply (rule_tac x="ball p e" in exI)
-      using \<open>e > 0\<close> e \<open>d \<noteq> 0\<close>
-      apply (simp add:, clarify)
-      apply (rule hol_pal_lem4 [of "ball p e" _ _ _ d _ k])
-      apply (auto simp: subset_hull)
-      done
-  qed
-  show ?thesis
-    by (blast intro: * Morera_local_triangle analytic_imp_holomorphic)
-qed
-
-proposition Schwarz_reflection:
-  assumes "open S" and cnjs: "cnj ` S \<subseteq> S"
-      and  holf: "f holomorphic_on (S \<inter> {z. 0 < Im z})"
-      and contf: "continuous_on (S \<inter> {z. 0 \<le> Im z}) f"
-      and f: "\<And>z. \<lbrakk>z \<in> S; z \<in> \<real>\<rbrakk> \<Longrightarrow> (f z) \<in> \<real>"
-    shows "(\<lambda>z. if 0 \<le> Im z then f z else cnj(f(cnj z))) holomorphic_on S"
-proof -
-  have 1: "(\<lambda>z. if 0 \<le> Im z then f z else cnj (f (cnj z))) holomorphic_on (S \<inter> {z. 0 < Im z})"
-    by (force intro: iffD1 [OF holomorphic_cong [OF refl] holf])
-  have cont_cfc: "continuous_on (S \<inter> {z. Im z \<le> 0}) (cnj o f o cnj)"
-    apply (intro continuous_intros continuous_on_compose continuous_on_subset [OF contf])
-    using cnjs apply auto
-    done
-  have "cnj \<circ> f \<circ> cnj field_differentiable at x within S \<inter> {z. Im z < 0}"
-        if "x \<in> S" "Im x < 0" "f field_differentiable at (cnj x) within S \<inter> {z. 0 < Im z}" for x
-    using that
-    apply (simp add: field_differentiable_def has_field_derivative_iff Lim_within dist_norm, clarify)
-    apply (rule_tac x="cnj f'" in exI)
-    apply (elim all_forward ex_forward conj_forward imp_forward asm_rl, clarify)
-    apply (drule_tac x="cnj xa" in bspec)
-    using cnjs apply force
-    apply (metis complex_cnj_cnj complex_cnj_diff complex_cnj_divide complex_mod_cnj)
-    done
-  then have hol_cfc: "(cnj o f o cnj) holomorphic_on (S \<inter> {z. Im z < 0})"
-    using holf cnjs
-    by (force simp: holomorphic_on_def)
-  have 2: "(\<lambda>z. if 0 \<le> Im z then f z else cnj (f (cnj z))) holomorphic_on (S \<inter> {z. Im z < 0})"
-    apply (rule iffD1 [OF holomorphic_cong [OF refl]])
-    using hol_cfc by auto
-  have [simp]: "(S \<inter> {z. 0 \<le> Im z}) \<union> (S \<inter> {z. Im z \<le> 0}) = S"
-    by force
-  have "continuous_on ((S \<inter> {z. 0 \<le> Im z}) \<union> (S \<inter> {z. Im z \<le> 0}))
-                       (\<lambda>z. if 0 \<le> Im z then f z else cnj (f (cnj z)))"
-    apply (rule continuous_on_cases_local)
-    using cont_cfc contf
-    apply (simp_all add: closedin_closed_Int closed_halfspace_Im_le closed_halfspace_Im_ge)
-    using f Reals_cnj_iff complex_is_Real_iff apply auto
-    done
-  then have 3: "continuous_on S (\<lambda>z. if 0 \<le> Im z then f z else cnj (f (cnj z)))"
-    by force
-  show ?thesis
-    apply (rule holomorphic_on_paste_across_line [OF \<open>open S\<close>, of "- \<i>" _ 0])
-    using 1 2 3
-    apply auto
-    done
-qed
-
-subsection\<open>Bloch's theorem\<close>
-
-lemma Bloch_lemma_0:
-  assumes holf: "f holomorphic_on cball 0 r" and "0 < r"
-      and [simp]: "f 0 = 0"
-      and le: "\<And>z. norm z < r \<Longrightarrow> norm(deriv f z) \<le> 2 * norm(deriv f 0)"
-    shows "ball 0 ((3 - 2 * sqrt 2) * r * norm(deriv f 0)) \<subseteq> f ` ball 0 r"
-proof -
-  have "sqrt 2 < 3/2"
-    by (rule real_less_lsqrt) (auto simp: power2_eq_square)
-  then have sq3: "0 < 3 - 2 * sqrt 2" by simp
-  show ?thesis
-  proof (cases "deriv f 0 = 0")
-    case True then show ?thesis by simp
-  next
-    case False
-    define C where "C = 2 * norm(deriv f 0)"
-    have "0 < C" using False by (simp add: C_def)
-    have holf': "f holomorphic_on ball 0 r" using holf
-      using ball_subset_cball holomorphic_on_subset by blast
-    then have holdf': "deriv f holomorphic_on ball 0 r"
-      by (rule holomorphic_deriv [OF _ open_ball])
-    have "Le1": "norm(deriv f z - deriv f 0) \<le> norm z / (r - norm z) * C"
-                if "norm z < r" for z
-    proof -
-      have T1: "norm(deriv f z - deriv f 0) \<le> norm z / (R - norm z) * C"
-              if R: "norm z < R" "R < r" for R
-      proof -
-        have "0 < R" using R
-          by (metis less_trans norm_zero zero_less_norm_iff)
-        have df_le: "\<And>x. norm x < r \<Longrightarrow> norm (deriv f x) \<le> C"
-          using le by (simp add: C_def)
-        have hol_df: "deriv f holomorphic_on cball 0 R"
-          apply (rule holomorphic_on_subset) using R holdf' by auto
-        have *: "((\<lambda>w. deriv f w / (w - z)) has_contour_integral 2 * pi * \<i> * deriv f z) (circlepath 0 R)"
-                 if "norm z < R" for z
-          using \<open>0 < R\<close> that Cauchy_integral_formula_convex_simple [OF convex_cball hol_df, of _ "circlepath 0 R"]
-          by (force simp: winding_number_circlepath)
-        have **: "((\<lambda>x. deriv f x / (x - z) - deriv f x / x) has_contour_integral
-                   of_real (2 * pi) * \<i> * (deriv f z - deriv f 0))
-                  (circlepath 0 R)"
-           using has_contour_integral_diff [OF * [of z] * [of 0]] \<open>0 < R\<close> that
-           by (simp add: algebra_simps)
-        have [simp]: "\<And>x. norm x = R \<Longrightarrow> x \<noteq> z"  using that(1) by blast
-        have "norm (deriv f x / (x - z) - deriv f x / x)
-                     \<le> C * norm z / (R * (R - norm z))"
-                  if "norm x = R" for x
-        proof -
-          have [simp]: "norm (deriv f x * x - deriv f x * (x - z)) =
-                        norm (deriv f x) * norm z"
-            by (simp add: norm_mult right_diff_distrib')
-          show ?thesis
-            using  \<open>0 < R\<close> \<open>0 < C\<close> R that
-            apply (simp add: norm_mult norm_divide divide_simps)
-            using df_le norm_triangle_ineq2 \<open>0 < C\<close> apply (auto intro!: mult_mono)
-            done
-        qed
-        then show ?thesis
-          using has_contour_integral_bound_circlepath
-                  [OF **, of "C * norm z/(R*(R - norm z))"]
-                \<open>0 < R\<close> \<open>0 < C\<close> R
-          apply (simp add: norm_mult norm_divide)
-          apply (simp add: divide_simps mult.commute)
-          done
-      qed
-      obtain r' where r': "norm z < r'" "r' < r"
-        using Rats_dense_in_real [of "norm z" r] \<open>norm z < r\<close> by blast
-      then have [simp]: "closure {r'<..<r} = {r'..r}" by simp
-      show ?thesis
-        apply (rule continuous_ge_on_closure
-                 [where f = "\<lambda>r. norm z / (r - norm z) * C" and s = "{r'<..<r}",
-                  OF _ _ T1])
-        apply (intro continuous_intros)
-        using that r'
-        apply (auto simp: not_le)
-        done
-    qed
-    have "*": "(norm z - norm z^2/(r - norm z)) * norm(deriv f 0) \<le> norm(f z)"
-              if r: "norm z < r" for z
-    proof -
-      have 1: "\<And>x. x \<in> ball 0 r \<Longrightarrow>
-              ((\<lambda>z. f z - deriv f 0 * z) has_field_derivative deriv f x - deriv f 0)
-               (at x within ball 0 r)"
-        by (rule derivative_eq_intros holomorphic_derivI holf' | simp)+
-      have 2: "closed_segment 0 z \<subseteq> ball 0 r"
-        by (metis \<open>0 < r\<close> convex_ball convex_contains_segment dist_self mem_ball mem_ball_0 that)
-      have 3: "(\<lambda>t. (norm z)\<^sup>2 * t / (r - norm z) * C) integrable_on {0..1}"
-        apply (rule integrable_on_cmult_right [where 'b=real, simplified])
-        apply (rule integrable_on_cdivide [where 'b=real, simplified])
-        apply (rule integrable_on_cmult_left [where 'b=real, simplified])
-        apply (rule ident_integrable_on)
-        done
-      have 4: "norm (deriv f (x *\<^sub>R z) - deriv f 0) * norm z \<le> norm z * norm z * x * C / (r - norm z)"
-              if x: "0 \<le> x" "x \<le> 1" for x
-      proof -
-        have [simp]: "x * norm z < r"
-          using r x by (meson le_less_trans mult_le_cancel_right2 norm_not_less_zero)
-        have "norm (deriv f (x *\<^sub>R z) - deriv f 0) \<le> norm (x *\<^sub>R z) / (r - norm (x *\<^sub>R z)) * C"
-          apply (rule Le1) using r x \<open>0 < r\<close> by simp
-        also have "... \<le> norm (x *\<^sub>R z) / (r - norm z) * C"
-          using r x \<open>0 < r\<close>
-          apply (simp add: field_split_simps)
-          by (simp add: \<open>0 < C\<close> mult.assoc mult_left_le_one_le ordered_comm_semiring_class.comm_mult_left_mono)
-        finally have "norm (deriv f (x *\<^sub>R z) - deriv f 0) * norm z \<le> norm (x *\<^sub>R z)  / (r - norm z) * C * norm z"
-          by (rule mult_right_mono) simp
-        with x show ?thesis by (simp add: algebra_simps)
-      qed
-      have le_norm: "abc \<le> norm d - e \<Longrightarrow> norm(f - d) \<le> e \<Longrightarrow> abc \<le> norm f" for abc d e and f::complex
-        by (metis add_diff_cancel_left' add_diff_eq diff_left_mono norm_diff_ineq order_trans)
-      have "norm (integral {0..1} (\<lambda>x. (deriv f (x *\<^sub>R z) - deriv f 0) * z))
-            \<le> integral {0..1} (\<lambda>t. (norm z)\<^sup>2 * t / (r - norm z) * C)"
-        apply (rule integral_norm_bound_integral)
-        using contour_integral_primitive [OF 1, of "linepath 0 z"] 2
-        apply (simp add: has_contour_integral_linepath has_integral_integrable_integral)
-        apply (rule 3)
-        apply (simp add: norm_mult power2_eq_square 4)
-        done
-      then have int_le: "norm (f z - deriv f 0 * z) \<le> (norm z)\<^sup>2 * norm(deriv f 0) / ((r - norm z))"
-        using contour_integral_primitive [OF 1, of "linepath 0 z"] 2
-        apply (simp add: has_contour_integral_linepath has_integral_integrable_integral C_def)
-        done
-      show ?thesis
-        apply (rule le_norm [OF _ int_le])
-        using \<open>norm z < r\<close>
-        apply (simp add: power2_eq_square divide_simps C_def norm_mult)
-        proof -
-          have "norm z * (norm (deriv f 0) * (r - norm z - norm z)) \<le> norm z * (norm (deriv f 0) * (r - norm z) - norm (deriv f 0) * norm z)"
-            by (simp add: algebra_simps)
-          then show "(norm z * (r - norm z) - norm z * norm z) * norm (deriv f 0) \<le> norm (deriv f 0) * norm z * (r - norm z) - norm z * norm z * norm (deriv f 0)"
-            by (simp add: algebra_simps)
-        qed
-    qed
-    have sq201 [simp]: "0 < (1 - sqrt 2 / 2)" "(1 - sqrt 2 / 2)  < 1"
-      by (auto simp:  sqrt2_less_2)
-    have 1: "continuous_on (closure (ball 0 ((1 - sqrt 2 / 2) * r))) f"
-      apply (rule continuous_on_subset [OF holomorphic_on_imp_continuous_on [OF holf]])
-      apply (subst closure_ball)
-      using \<open>0 < r\<close> mult_pos_pos sq201
-      apply (auto simp: cball_subset_cball_iff)
-      done
-    have 2: "open (f ` interior (ball 0 ((1 - sqrt 2 / 2) * r)))"
-      apply (rule open_mapping_thm [OF holf' open_ball connected_ball], force)
-      using \<open>0 < r\<close> mult_pos_pos sq201 apply (simp add: ball_subset_ball_iff)
-      using False \<open>0 < r\<close> centre_in_ball holf' holomorphic_nonconstant by blast
-    have "ball 0 ((3 - 2 * sqrt 2) * r * norm (deriv f 0)) =
-          ball (f 0) ((3 - 2 * sqrt 2) * r * norm (deriv f 0))"
-      by simp
-    also have "...  \<subseteq> f ` ball 0 ((1 - sqrt 2 / 2) * r)"
-    proof -
-      have 3: "(3 - 2 * sqrt 2) * r * norm (deriv f 0) \<le> norm (f z)"
-           if "norm z = (1 - sqrt 2 / 2) * r" for z
-        apply (rule order_trans [OF _ *])
-        using  \<open>0 < r\<close>
-        apply (simp_all add: field_simps  power2_eq_square that)
-        apply (simp add: mult.assoc [symmetric])
-        done
-      show ?thesis
-        apply (rule ball_subset_open_map_image [OF 1 2 _ bounded_ball])
-        using \<open>0 < r\<close> sq201 3 apply simp_all
-        using C_def \<open>0 < C\<close> sq3 apply force
-        done
-     qed
-    also have "...  \<subseteq> f ` ball 0 r"
-      apply (rule image_subsetI [OF imageI], simp)
-      apply (erule less_le_trans)
-      using \<open>0 < r\<close> apply (auto simp: field_simps)
-      done
-    finally show ?thesis .
-  qed
-qed
-
-lemma Bloch_lemma:
-  assumes holf: "f holomorphic_on cball a r" and "0 < r"
-      and le: "\<And>z. z \<in> ball a r \<Longrightarrow> norm(deriv f z) \<le> 2 * norm(deriv f a)"
-    shows "ball (f a) ((3 - 2 * sqrt 2) * r * norm(deriv f a)) \<subseteq> f ` ball a r"
-proof -
-  have fz: "(\<lambda>z. f (a + z)) = f o (\<lambda>z. (a + z))"
-    by (simp add: o_def)
-  have hol0: "(\<lambda>z. f (a + z)) holomorphic_on cball 0 r"
-    unfolding fz by (intro holomorphic_intros holf holomorphic_on_compose | simp)+
-  then have [simp]: "\<And>x. norm x < r \<Longrightarrow> (\<lambda>z. f (a + z)) field_differentiable at x"
-    by (metis open_ball at_within_open ball_subset_cball diff_0 dist_norm holomorphic_on_def holomorphic_on_subset mem_ball norm_minus_cancel)
-  have [simp]: "\<And>z. norm z < r \<Longrightarrow> f field_differentiable at (a + z)"
-    by (metis holf open_ball add_diff_cancel_left' dist_complex_def holomorphic_on_imp_differentiable_at holomorphic_on_subset interior_cball interior_subset mem_ball norm_minus_commute)
-  then have [simp]: "f field_differentiable at a"
-    by (metis add.comm_neutral \<open>0 < r\<close> norm_eq_zero)
-  have hol1: "(\<lambda>z. f (a + z) - f a) holomorphic_on cball 0 r"
-    by (intro holomorphic_intros hol0)
-  then have "ball 0 ((3 - 2 * sqrt 2) * r * norm (deriv (\<lambda>z. f (a + z) - f a) 0))
-             \<subseteq> (\<lambda>z. f (a + z) - f a) ` ball 0 r"
-    apply (rule Bloch_lemma_0)
-    apply (simp_all add: \<open>0 < r\<close>)
-    apply (simp add: fz complex_derivative_chain)
-    apply (simp add: dist_norm le)
-    done
-  then show ?thesis
-    apply clarify
-    apply (drule_tac c="x - f a" in subsetD)
-     apply (force simp: fz \<open>0 < r\<close> dist_norm complex_derivative_chain field_differentiable_compose)+
-    done
-qed
-
-proposition Bloch_unit:
-  assumes holf: "f holomorphic_on ball a 1" and [simp]: "deriv f a = 1"
-  obtains b r where "1/12 < r" and "ball b r \<subseteq> f ` (ball a 1)"
-proof -
-  define r :: real where "r = 249/256"
-  have "0 < r" "r < 1" by (auto simp: r_def)
-  define g where "g z = deriv f z * of_real(r - norm(z - a))" for z
-  have "deriv f holomorphic_on ball a 1"
-    by (rule holomorphic_deriv [OF holf open_ball])
-  then have "continuous_on (ball a 1) (deriv f)"
-    using holomorphic_on_imp_continuous_on by blast
-  then have "continuous_on (cball a r) (deriv f)"
-    by (rule continuous_on_subset) (simp add: cball_subset_ball_iff \<open>r < 1\<close>)
-  then have "continuous_on (cball a r) g"
-    by (simp add: g_def continuous_intros)
-  then have 1: "compact (g ` cball a r)"
-    by (rule compact_continuous_image [OF _ compact_cball])
-  have 2: "g ` cball a r \<noteq> {}"
-    using \<open>r > 0\<close> by auto
-  obtain p where pr: "p \<in> cball a r"
-             and pge: "\<And>y. y \<in> cball a r \<Longrightarrow> norm (g y) \<le> norm (g p)"
-    using distance_attains_sup [OF 1 2, of 0] by force
-  define t where "t = (r - norm(p - a)) / 2"
-  have "norm (p - a) \<noteq> r"
-    using pge [of a] \<open>r > 0\<close> by (auto simp: g_def norm_mult)
-  then have "norm (p - a) < r" using pr
-    by (simp add: norm_minus_commute dist_norm)
-  then have "0 < t"
-    by (simp add: t_def)
-  have cpt: "cball p t \<subseteq> ball a r"
-    using \<open>0 < t\<close> by (simp add: cball_subset_ball_iff dist_norm t_def field_simps)
-  have gen_le_dfp: "norm (deriv f y) * (r - norm (y - a)) / (r - norm (p - a)) \<le> norm (deriv f p)"
-            if "y \<in> cball a r" for y
-  proof -
-    have [simp]: "norm (y - a) \<le> r"
-      using that by (simp add: dist_norm norm_minus_commute)
-    have "norm (g y) \<le> norm (g p)"
-      using pge [OF that] by simp
-    then have "norm (deriv f y) * abs (r - norm (y - a)) \<le> norm (deriv f p) * abs (r - norm (p - a))"
-      by (simp only: dist_norm g_def norm_mult norm_of_real)
-    with that \<open>norm (p - a) < r\<close> show ?thesis
-      by (simp add: dist_norm field_split_simps)
-  qed
-  have le_norm_dfp: "r / (r - norm (p - a)) \<le> norm (deriv f p)"
-    using gen_le_dfp [of a] \<open>r > 0\<close> by auto
-  have 1: "f holomorphic_on cball p t"
-    apply (rule holomorphic_on_subset [OF holf])
-    using cpt \<open>r < 1\<close> order_subst1 subset_ball by auto
-  have 2: "norm (deriv f z) \<le> 2 * norm (deriv f p)" if "z \<in> ball p t" for z
-  proof -
-    have z: "z \<in> cball a r"
-      by (meson ball_subset_cball subsetD cpt that)
-    then have "norm(z - a) < r"
-      by (metis ball_subset_cball contra_subsetD cpt dist_norm mem_ball norm_minus_commute that)
-    have "norm (deriv f z) * (r - norm (z - a)) / (r - norm (p - a)) \<le> norm (deriv f p)"
-      using gen_le_dfp [OF z] by simp
-    with \<open>norm (z - a) < r\<close> \<open>norm (p - a) < r\<close>
-    have "norm (deriv f z) \<le> (r - norm (p - a)) / (r - norm (z - a)) * norm (deriv f p)"
-       by (simp add: field_simps)
-    also have "... \<le> 2 * norm (deriv f p)"
-      apply (rule mult_right_mono)
-      using that \<open>norm (p - a) < r\<close> \<open>norm(z - a) < r\<close>
-      apply (simp_all add: field_simps t_def dist_norm [symmetric])
-      using dist_triangle3 [of z a p] by linarith
-    finally show ?thesis .
-  qed
-  have sqrt2: "sqrt 2 < 2113/1494"
-    by (rule real_less_lsqrt) (auto simp: power2_eq_square)
-  then have sq3: "0 < 3 - 2 * sqrt 2" by simp
-  have "1 / 12 / ((3 - 2 * sqrt 2) / 2) < r"
-    using sq3 sqrt2 by (auto simp: field_simps r_def)
-  also have "... \<le> cmod (deriv f p) * (r - cmod (p - a))"
-    using \<open>norm (p - a) < r\<close> le_norm_dfp   by (simp add: pos_divide_le_eq)
-  finally have "1 / 12 < cmod (deriv f p) * (r - cmod (p - a)) * ((3 - 2 * sqrt 2) / 2)"
-    using pos_divide_less_eq half_gt_zero_iff sq3 by blast
-  then have **: "1 / 12 < (3 - 2 * sqrt 2) * t * norm (deriv f p)"
-    using sq3 by (simp add: mult.commute t_def)
-  have "ball (f p) ((3 - 2 * sqrt 2) * t * norm (deriv f p)) \<subseteq> f ` ball p t"
-    by (rule Bloch_lemma [OF 1 \<open>0 < t\<close> 2])
-  also have "... \<subseteq> f ` ball a 1"
-    apply (rule image_mono)
-    apply (rule order_trans [OF ball_subset_cball])
-    apply (rule order_trans [OF cpt])
-    using \<open>0 < t\<close> \<open>r < 1\<close> apply (simp add: ball_subset_ball_iff dist_norm)
-    done
-  finally have "ball (f p) ((3 - 2 * sqrt 2) * t * norm (deriv f p)) \<subseteq> f ` ball a 1" .
-  with ** show ?thesis
-    by (rule that)
-qed
-
-theorem Bloch:
-  assumes holf: "f holomorphic_on ball a r" and "0 < r"
-      and r': "r' \<le> r * norm (deriv f a) / 12"
-  obtains b where "ball b r' \<subseteq> f ` (ball a r)"
-proof (cases "deriv f a = 0")
-  case True with r' show ?thesis
-    using ball_eq_empty that by fastforce
-next
-  case False
-  define C where "C = deriv f a"
-  have "0 < norm C" using False by (simp add: C_def)
-  have dfa: "f field_differentiable at a"
-    apply (rule holomorphic_on_imp_differentiable_at [OF holf])
-    using \<open>0 < r\<close> by auto
-  have fo: "(\<lambda>z. f (a + of_real r * z)) = f o (\<lambda>z. (a + of_real r * z))"
-    by (simp add: o_def)
-  have holf': "f holomorphic_on (\<lambda>z. a + complex_of_real r * z) ` ball 0 1"
-    apply (rule holomorphic_on_subset [OF holf])
-    using \<open>0 < r\<close> apply (force simp: dist_norm norm_mult)
-    done
-  have 1: "(\<lambda>z. f (a + r * z) / (C * r)) holomorphic_on ball 0 1"
-    apply (rule holomorphic_intros holomorphic_on_compose holf' | simp add: fo)+
-    using \<open>0 < r\<close> by (simp add: C_def False)
-  have "((\<lambda>z. f (a + of_real r * z) / (C * of_real r)) has_field_derivative
-        (deriv f (a + of_real r * z) / C)) (at z)"
-       if "norm z < 1" for z
-  proof -
-    have *: "((\<lambda>x. f (a + of_real r * x)) has_field_derivative
-           (deriv f (a + of_real r * z) * of_real r)) (at z)"
-      apply (simp add: fo)
-      apply (rule DERIV_chain [OF field_differentiable_derivI])
-      apply (rule holomorphic_on_imp_differentiable_at [OF holf], simp)
-      using \<open>0 < r\<close> apply (simp add: dist_norm norm_mult that)
-      apply (rule derivative_eq_intros | simp)+
-      done
-    show ?thesis
-      apply (rule derivative_eq_intros * | simp)+
-      using \<open>0 < r\<close> by (auto simp: C_def False)
-  qed
-  have 2: "deriv (\<lambda>z. f (a + of_real r * z) / (C * of_real r)) 0 = 1"
-    apply (subst deriv_cdivide_right)
-    apply (simp add: field_differentiable_def fo)
-    apply (rule exI)
-    apply (rule DERIV_chain [OF field_differentiable_derivI])
-    apply (simp add: dfa)
-    apply (rule derivative_eq_intros | simp add: C_def False fo)+
-    using \<open>0 < r\<close>
-    apply (simp add: C_def False fo)
-    apply (simp add: derivative_intros dfa complex_derivative_chain)
-    done
-  have sb1: "(*) (C * r) ` (\<lambda>z. f (a + of_real r * z) / (C * r)) ` ball 0 1
-             \<subseteq> f ` ball a r"
-    using \<open>0 < r\<close> by (auto simp: dist_norm norm_mult C_def False)
-  have sb2: "ball (C * r * b) r' \<subseteq> (*) (C * r) ` ball b t"
-             if "1 / 12 < t" for b t
-  proof -
-    have *: "r * cmod (deriv f a) / 12 \<le> r * (t * cmod (deriv f a))"
-      using that \<open>0 < r\<close> less_eq_real_def mult.commute mult.right_neutral mult_left_mono norm_ge_zero times_divide_eq_right
-      by auto
-    show ?thesis
-      apply clarify
-      apply (rule_tac x="x / (C * r)" in image_eqI)
-      using \<open>0 < r\<close>
-      apply (simp_all add: dist_norm norm_mult norm_divide C_def False field_simps)
-      apply (erule less_le_trans)
-      apply (rule order_trans [OF r' *])
-      done
-  qed
-  show ?thesis
-    apply (rule Bloch_unit [OF 1 2])
-    apply (rename_tac t)
-    apply (rule_tac b="(C * of_real r) * b" in that)
-    apply (drule image_mono [where f = "\<lambda>z. (C * of_real r) * z"])
-    using sb1 sb2
-    apply force
-    done
-qed
-
-corollary Bloch_general:
-  assumes holf: "f holomorphic_on s" and "a \<in> s"
-      and tle: "\<And>z. z \<in> frontier s \<Longrightarrow> t \<le> dist a z"
-      and rle: "r \<le> t * norm(deriv f a) / 12"
-  obtains b where "ball b r \<subseteq> f ` s"
-proof -
-  consider "r \<le> 0" | "0 < t * norm(deriv f a) / 12" using rle by force
-  then show ?thesis
-  proof cases
-    case 1 then show ?thesis
-      by (simp add: ball_empty that)
-  next
-    case 2
-    show ?thesis
-    proof (cases "deriv f a = 0")
-      case True then show ?thesis
-        using rle by (simp add: ball_empty that)
-    next
-      case False
-      then have "t > 0"
-        using 2 by (force simp: zero_less_mult_iff)
-      have "\<not> ball a t \<subseteq> s \<Longrightarrow> ball a t \<inter> frontier s \<noteq> {}"
-        apply (rule connected_Int_frontier [of "ball a t" s], simp_all)
-        using \<open>0 < t\<close> \<open>a \<in> s\<close> centre_in_ball apply blast
-        done
-      with tle have *: "ball a t \<subseteq> s" by fastforce
-      then have 1: "f holomorphic_on ball a t"
-        using holf using holomorphic_on_subset by blast
-      show ?thesis
-        apply (rule Bloch [OF 1 \<open>t > 0\<close> rle])
-        apply (rule_tac b=b in that)
-        using * apply force
-        done
-    qed
-  qed
-qed
-
-subsection \<open>Cauchy's residue theorem\<close>
-
-text\<open>Wenda Li and LC Paulson (2016). A Formal Proof of Cauchy's Residue Theorem.
-    Interactive Theorem Proving\<close>
-
-definition\<^marker>\<open>tag important\<close> residue :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> complex" where
-  "residue f z = (SOME int. \<exists>e>0. \<forall>\<epsilon>>0. \<epsilon><e
-    \<longrightarrow> (f has_contour_integral 2*pi* \<i> *int) (circlepath z \<epsilon>))"
-
-lemma Eps_cong:
-  assumes "\<And>x. P x = Q x"
-  shows   "Eps P = Eps Q"
-  using ext[of P Q, OF assms] by simp
-
-lemma residue_cong:
-  assumes eq: "eventually (\<lambda>z. f z = g z) (at z)" and "z = z'"
-  shows   "residue f z = residue g z'"
-proof -
-  from assms have eq': "eventually (\<lambda>z. g z = f z) (at z)"
-    by (simp add: eq_commute)
-  let ?P = "\<lambda>f c e. (\<forall>\<epsilon>>0. \<epsilon> < e \<longrightarrow>
-   (f has_contour_integral of_real (2 * pi) * \<i> * c) (circlepath z \<epsilon>))"
-  have "residue f z = residue g z" unfolding residue_def
-  proof (rule Eps_cong)
-    fix c :: complex
-    have "\<exists>e>0. ?P g c e"
-      if "\<exists>e>0. ?P f c e" and "eventually (\<lambda>z. f z = g z) (at z)" for f g
-    proof -
-      from that(1) obtain e where e: "e > 0" "?P f c e"
-        by blast
-      from that(2) obtain e' where e': "e' > 0" "\<And>z'. z' \<noteq> z \<Longrightarrow> dist z' z < e' \<Longrightarrow> f z' = g z'"
-        unfolding eventually_at by blast
-      have "?P g c (min e e')"
-      proof (intro allI exI impI, goal_cases)
-        case (1 \<epsilon>)
-        hence "(f has_contour_integral of_real (2 * pi) * \<i> * c) (circlepath z \<epsilon>)"
-          using e(2) by auto
-        thus ?case
-        proof (rule has_contour_integral_eq)
-          fix z' assume "z' \<in> path_image (circlepath z \<epsilon>)"
-          hence "dist z' z < e'" and "z' \<noteq> z"
-            using 1 by (auto simp: dist_commute)
-          with e'(2)[of z'] show "f z' = g z'" by simp
-        qed
-      qed
-      moreover from e and e' have "min e e' > 0" by auto
-      ultimately show ?thesis by blast
-    qed
-    from this[OF _ eq] and this[OF _ eq']
-      show "(\<exists>e>0. ?P f c e) \<longleftrightarrow> (\<exists>e>0. ?P g c e)"
-      by blast
-  qed
-  with assms show ?thesis by simp
-qed
-
-lemma contour_integral_circlepath_eq:
-  assumes "open s" and f_holo:"f holomorphic_on (s-{z})" and "0<e1" "e1\<le>e2"
-    and e2_cball:"cball z e2 \<subseteq> s"
-  shows
-    "f contour_integrable_on circlepath z e1"
-    "f contour_integrable_on circlepath z e2"
-    "contour_integral (circlepath z e2) f = contour_integral (circlepath z e1) f"
-proof -
-  define l where "l \<equiv> linepath (z+e2) (z+e1)"
-  have [simp]:"valid_path l" "pathstart l=z+e2" "pathfinish l=z+e1" unfolding l_def by auto
-  have "e2>0" using \<open>e1>0\<close> \<open>e1\<le>e2\<close> by auto
-  have zl_img:"z\<notin>path_image l"
-    proof
-      assume "z \<in> path_image l"
-      then have "e2 \<le> cmod (e2 - e1)"
-        using segment_furthest_le[of z "z+e2" "z+e1" "z+e2",simplified] \<open>e1>0\<close> \<open>e2>0\<close> unfolding l_def
-        by (auto simp add:closed_segment_commute)
-      thus False using \<open>e2>0\<close> \<open>e1>0\<close> \<open>e1\<le>e2\<close>
-        apply (subst (asm) norm_of_real)
-        by auto
-    qed
-  define g where "g \<equiv> circlepath z e2 +++ l +++ reversepath (circlepath z e1) +++ reversepath l"
-  show [simp]: "f contour_integrable_on circlepath z e2" "f contour_integrable_on (circlepath z e1)"
-    proof -
-      show "f contour_integrable_on circlepath z e2"
-        apply (intro contour_integrable_continuous_circlepath[OF
-                continuous_on_subset[OF holomorphic_on_imp_continuous_on[OF f_holo]]])
-        using \<open>e2>0\<close> e2_cball by auto
-      show "f contour_integrable_on (circlepath z e1)"
-        apply (intro contour_integrable_continuous_circlepath[OF
-                      continuous_on_subset[OF holomorphic_on_imp_continuous_on[OF f_holo]]])
-        using \<open>e1>0\<close> \<open>e1\<le>e2\<close> e2_cball by auto
-    qed
-  have [simp]:"f contour_integrable_on l"
-    proof -
-      have "closed_segment (z + e2) (z + e1) \<subseteq> cball z e2" using \<open>e2>0\<close> \<open>e1>0\<close> \<open>e1\<le>e2\<close>
-        by (intro closed_segment_subset,auto simp add:dist_norm)
-      hence "closed_segment (z + e2) (z + e1) \<subseteq> s - {z}" using zl_img e2_cball unfolding l_def
-        by auto
-      then show "f contour_integrable_on l" unfolding l_def
-        apply (intro contour_integrable_continuous_linepath[OF
-                      continuous_on_subset[OF holomorphic_on_imp_continuous_on[OF f_holo]]])
-        by auto
-    qed
-  let ?ig="\<lambda>g. contour_integral g f"
-  have "(f has_contour_integral 0) g"
-    proof (rule Cauchy_theorem_global[OF _ f_holo])
-      show "open (s - {z})" using \<open>open s\<close> by auto
-      show "valid_path g" unfolding g_def l_def by auto
-      show "pathfinish g = pathstart g" unfolding g_def l_def by auto
-    next
-      have path_img:"path_image g \<subseteq> cball z e2"
-        proof -
-          have "closed_segment (z + e2) (z + e1) \<subseteq> cball z e2" using \<open>e2>0\<close> \<open>e1>0\<close> \<open>e1\<le>e2\<close>
-            by (intro closed_segment_subset,auto simp add:dist_norm)
-          moreover have "sphere z \<bar>e1\<bar> \<subseteq> cball z e2" using \<open>e2>0\<close> \<open>e1\<le>e2\<close> \<open>e1>0\<close> by auto
-          ultimately show ?thesis unfolding g_def l_def using \<open>e2>0\<close>
-            by (simp add: path_image_join closed_segment_commute)
-        qed
-      show "path_image g \<subseteq> s - {z}"
-        proof -
-          have "z\<notin>path_image g" using zl_img
-            unfolding g_def l_def by (auto simp add: path_image_join closed_segment_commute)
-          moreover note \<open>cball z e2 \<subseteq> s\<close> and path_img
-          ultimately show ?thesis by auto
-        qed
-      show "winding_number g w = 0" when"w \<notin> s - {z}" for w
-        proof -
-          have "winding_number g w = 0" when "w\<notin>s" using that e2_cball
-            apply (intro winding_number_zero_outside[OF _ _ _ _ path_img])
-            by (auto simp add:g_def l_def)
-          moreover have "winding_number g z=0"
-            proof -
-              let ?Wz="\<lambda>g. winding_number g z"
-              have "?Wz g = ?Wz (circlepath z e2) + ?Wz l + ?Wz (reversepath (circlepath z e1))
-                  + ?Wz (reversepath l)"
-                using \<open>e2>0\<close> \<open>e1>0\<close> zl_img unfolding g_def l_def
-                by (subst winding_number_join,auto simp add:path_image_join closed_segment_commute)+
-              also have "... = ?Wz (circlepath z e2) + ?Wz (reversepath (circlepath z e1))"
-                using zl_img
-                apply (subst (2) winding_number_reversepath)
-                by (auto simp add:l_def closed_segment_commute)
-              also have "... = 0"
-                proof -
-                  have "?Wz (circlepath z e2) = 1" using \<open>e2>0\<close>
-                    by (auto intro: winding_number_circlepath_centre)
-                  moreover have "?Wz (reversepath (circlepath z e1)) = -1" using \<open>e1>0\<close>
-                    apply (subst winding_number_reversepath)
-                    by (auto intro: winding_number_circlepath_centre)
-                  ultimately show ?thesis by auto
-                qed
-              finally show ?thesis .
-            qed
-          ultimately show ?thesis using that by auto
-        qed
-    qed
-  then have "0 = ?ig g" using contour_integral_unique by simp
-  also have "... = ?ig (circlepath z e2) + ?ig l + ?ig (reversepath (circlepath z e1))
-      + ?ig (reversepath l)"
-    unfolding g_def
-    by (auto simp add:contour_integrable_reversepath_eq)
-  also have "... = ?ig (circlepath z e2)  - ?ig (circlepath z e1)"
-    by (auto simp add:contour_integral_reversepath)
-  finally show "contour_integral (circlepath z e2) f = contour_integral (circlepath z e1) f"
-    by simp
-qed
-
-lemma base_residue:
-  assumes "open s" "z\<in>s" "r>0" and f_holo:"f holomorphic_on (s - {z})"
-    and r_cball:"cball z r \<subseteq> s"
-  shows "(f has_contour_integral 2 * pi * \<i> * (residue f z)) (circlepath z r)"
-proof -
-  obtain e where "e>0" and e_cball:"cball z e \<subseteq> s"
-    using open_contains_cball[of s] \<open>open s\<close> \<open>z\<in>s\<close> by auto
-  define c where "c \<equiv> 2 * pi * \<i>"
-  define i where "i \<equiv> contour_integral (circlepath z e) f / c"
-  have "(f has_contour_integral c*i) (circlepath z \<epsilon>)" when "\<epsilon>>0" "\<epsilon><e" for \<epsilon>
-    proof -
-      have "contour_integral (circlepath z e) f = contour_integral (circlepath z \<epsilon>) f"
-          "f contour_integrable_on circlepath z \<epsilon>"
-          "f contour_integrable_on circlepath z e"
-        using \<open>\<epsilon><e\<close>
-        by (intro contour_integral_circlepath_eq[OF \<open>open s\<close> f_holo \<open>\<epsilon>>0\<close> _ e_cball],auto)+
-      then show ?thesis unfolding i_def c_def
-        by (auto intro:has_contour_integral_integral)
-    qed
-  then have "\<exists>e>0. \<forall>\<epsilon>>0. \<epsilon><e \<longrightarrow> (f has_contour_integral c * (residue f z)) (circlepath z \<epsilon>)"
-    unfolding residue_def c_def
-    apply (rule_tac someI[of _ i],intro  exI[where x=e])
-    by (auto simp add:\<open>e>0\<close> c_def)
-  then obtain e' where "e'>0"
-      and e'_def:"\<forall>\<epsilon>>0. \<epsilon><e' \<longrightarrow> (f has_contour_integral c * (residue f z)) (circlepath z \<epsilon>)"
-    by auto
-  let ?int="\<lambda>e. contour_integral (circlepath z e) f"
-  define  \<epsilon> where "\<epsilon> \<equiv> Min {r,e'} / 2"
-  have "\<epsilon>>0" "\<epsilon>\<le>r" "\<epsilon><e'" using \<open>r>0\<close> \<open>e'>0\<close> unfolding \<epsilon>_def by auto
-  have "(f has_contour_integral c * (residue f z)) (circlepath z \<epsilon>)"
-    using e'_def[rule_format,OF \<open>\<epsilon>>0\<close> \<open>\<epsilon><e'\<close>] .
-  then show ?thesis unfolding c_def
-    using contour_integral_circlepath_eq[OF \<open>open s\<close> f_holo \<open>\<epsilon>>0\<close> \<open>\<epsilon>\<le>r\<close> r_cball]
-    by (auto elim: has_contour_integral_eqpath[of _ _ "circlepath z \<epsilon>" "circlepath z r"])
-qed
-
-lemma residue_holo:
-  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s"
-  shows "residue f z = 0"
-proof -
-  define c where "c \<equiv> 2 * pi * \<i>"
-  obtain e where "e>0" and e_cball:"cball z e \<subseteq> s" using \<open>open s\<close> \<open>z\<in>s\<close>
-    using open_contains_cball_eq by blast
-  have "(f has_contour_integral c*residue f z) (circlepath z e)"
-    using f_holo
-    by (auto intro: base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c_def])
-  moreover have "(f has_contour_integral 0) (circlepath z e)"
-    using f_holo e_cball \<open>e>0\<close>
-    by (auto intro: Cauchy_theorem_convex_simple[of _ "cball z e"])
-  ultimately have "c*residue f z =0"
-    using has_contour_integral_unique by blast
-  thus ?thesis unfolding c_def  by auto
-qed
-
-lemma residue_const:"residue (\<lambda>_. c) z = 0"
-  by (intro residue_holo[of "UNIV::complex set"],auto intro:holomorphic_intros)
-
-lemma residue_add:
-  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
-      and g_holo:"g holomorphic_on s - {z}"
-  shows "residue (\<lambda>z. f z + g z) z= residue f z + residue g z"
-proof -
-  define c where "c \<equiv> 2 * pi * \<i>"
-  define fg where "fg \<equiv> (\<lambda>z. f z+g z)"
-  obtain e where "e>0" and e_cball:"cball z e \<subseteq> s" using \<open>open s\<close> \<open>z\<in>s\<close>
-    using open_contains_cball_eq by blast
-  have "(fg has_contour_integral c * residue fg z) (circlepath z e)"
-    unfolding fg_def using f_holo g_holo
-    apply (intro base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c_def])
-    by (auto intro:holomorphic_intros)
-  moreover have "(fg has_contour_integral c*residue f z + c* residue g z) (circlepath z e)"
-    unfolding fg_def using f_holo g_holo
-    by (auto intro: has_contour_integral_add base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c_def])
-  ultimately have "c*(residue f z + residue g z) = c * residue fg z"
-    using has_contour_integral_unique by (auto simp add:distrib_left)
-  thus ?thesis unfolding fg_def
-    by (auto simp add:c_def)
-qed
-
-lemma residue_lmul:
-  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
-  shows "residue (\<lambda>z. c * (f z)) z= c * residue f z"
-proof (cases "c=0")
-  case True
-  thus ?thesis using residue_const by auto
-next
-  case False
-  define c' where "c' \<equiv> 2 * pi * \<i>"
-  define f' where "f' \<equiv> (\<lambda>z. c * (f z))"
-  obtain e where "e>0" and e_cball:"cball z e \<subseteq> s" using \<open>open s\<close> \<open>z\<in>s\<close>
-    using open_contains_cball_eq by blast
-  have "(f' has_contour_integral c' * residue f' z) (circlepath z e)"
-    unfolding f'_def using f_holo
-    apply (intro base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c'_def])
-    by (auto intro:holomorphic_intros)
-  moreover have "(f' has_contour_integral c * (c' * residue f z)) (circlepath z e)"
-    unfolding f'_def using f_holo
-    by (auto intro: has_contour_integral_lmul
-      base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c'_def])
-  ultimately have "c' * residue f' z  = c * (c' * residue f z)"
-    using has_contour_integral_unique by auto
-  thus ?thesis unfolding f'_def c'_def using False
-    by (auto simp add:field_simps)
-qed
-
-lemma residue_rmul:
-  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
-  shows "residue (\<lambda>z. (f z) * c) z= residue f z * c"
-using residue_lmul[OF assms,of c] by (auto simp add:algebra_simps)
-
-lemma residue_div:
-  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
-  shows "residue (\<lambda>z. (f z) / c) z= residue f z / c "
-using residue_lmul[OF assms,of "1/c"] by (auto simp add:algebra_simps)
-
-lemma residue_neg:
-  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
-  shows "residue (\<lambda>z. - (f z)) z= - residue f z"
-using residue_lmul[OF assms,of "-1"] by auto
-
-lemma residue_diff:
-  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
-      and g_holo:"g holomorphic_on s - {z}"
-  shows "residue (\<lambda>z. f z - g z) z= residue f z - residue g z"
-using residue_add[OF assms(1,2,3),of "\<lambda>z. - g z"] residue_neg[OF assms(1,2,4)]
-by (auto intro:holomorphic_intros g_holo)
-
-lemma residue_simple:
-  assumes "open s" "z\<in>s" and f_holo:"f holomorphic_on s"
-  shows "residue (\<lambda>w. f w / (w - z)) z = f z"
-proof -
-  define c where "c \<equiv> 2 * pi * \<i>"
-  define f' where "f' \<equiv> \<lambda>w. f w / (w - z)"
-  obtain e where "e>0" and e_cball:"cball z e \<subseteq> s" using \<open>open s\<close> \<open>z\<in>s\<close>
-    using open_contains_cball_eq by blast
-  have "(f' has_contour_integral c * f z) (circlepath z e)"
-    unfolding f'_def c_def using \<open>e>0\<close> f_holo e_cball
-    by (auto intro!: Cauchy_integral_circlepath_simple holomorphic_intros)
-  moreover have "(f' has_contour_integral c * residue f' z) (circlepath z e)"
-    unfolding f'_def using f_holo
-    apply (intro base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c_def])
-    by (auto intro!:holomorphic_intros)
-  ultimately have "c * f z = c * residue f' z"
-    using has_contour_integral_unique by blast
-  thus ?thesis unfolding c_def f'_def  by auto
-qed
-
-lemma residue_simple':
-  assumes s: "open s" "z \<in> s" and holo: "f holomorphic_on (s - {z})"
-      and lim: "((\<lambda>w. f w * (w - z)) \<longlongrightarrow> c) (at z)"
-  shows   "residue f z = c"
-proof -
-  define g where "g = (\<lambda>w. if w = z then c else f w * (w - z))"
-  from holo have "(\<lambda>w. f w * (w - z)) holomorphic_on (s - {z})" (is "?P")
-    by (force intro: holomorphic_intros)
-  also have "?P \<longleftrightarrow> g holomorphic_on (s - {z})"
-    by (intro holomorphic_cong refl) (simp_all add: g_def)
-  finally have *: "g holomorphic_on (s - {z})" .
-
-  note lim
-  also have "(\<lambda>w. f w * (w - z)) \<midarrow>z\<rightarrow> c \<longleftrightarrow> g \<midarrow>z\<rightarrow> g z"
-    by (intro filterlim_cong refl) (simp_all add: g_def [abs_def] eventually_at_filter)
-  finally have **: "g \<midarrow>z\<rightarrow> g z" .
-
-  have g_holo: "g holomorphic_on s"
-    by (rule no_isolated_singularity'[where K = "{z}"])
-       (insert assms * **, simp_all add: at_within_open_NO_MATCH)
-  from s and this have "residue (\<lambda>w. g w / (w - z)) z = g z"
-    by (rule residue_simple)
-  also have "\<forall>\<^sub>F za in at z. g za / (za - z) = f za"
-    unfolding eventually_at by (auto intro!: exI[of _ 1] simp: field_simps g_def)
-  hence "residue (\<lambda>w. g w / (w - z)) z = residue f z"
-    by (intro residue_cong refl)
-  finally show ?thesis
-    by (simp add: g_def)
-qed
-
-lemma residue_holomorphic_over_power:
-  assumes "open A" "z0 \<in> A" "f holomorphic_on A"
-  shows   "residue (\<lambda>z. f z / (z - z0) ^ Suc n) z0 = (deriv ^^ n) f z0 / fact n"
-proof -
-  let ?f = "\<lambda>z. f z / (z - z0) ^ Suc n"
-  from assms(1,2) obtain r where r: "r > 0" "cball z0 r \<subseteq> A"
-    by (auto simp: open_contains_cball)
-  have "(?f has_contour_integral 2 * pi * \<i> * residue ?f z0) (circlepath z0 r)"
-    using r assms by (intro base_residue[of A]) (auto intro!: holomorphic_intros)
-  moreover have "(?f has_contour_integral 2 * pi * \<i> / fact n * (deriv ^^ n) f z0) (circlepath z0 r)"
-    using assms r
-    by (intro Cauchy_has_contour_integral_higher_derivative_circlepath)
-       (auto intro!: holomorphic_on_subset[OF assms(3)] holomorphic_on_imp_continuous_on)
-  ultimately have "2 * pi * \<i> * residue ?f z0 = 2 * pi * \<i> / fact n * (deriv ^^ n) f z0"
-    by (rule has_contour_integral_unique)
-  thus ?thesis by (simp add: field_simps)
-qed
-
-lemma residue_holomorphic_over_power':
-  assumes "open A" "0 \<in> A" "f holomorphic_on A"
-  shows   "residue (\<lambda>z. f z / z ^ Suc n) 0 = (deriv ^^ n) f 0 / fact n"
-  using residue_holomorphic_over_power[OF assms] by simp
-
-lemma get_integrable_path:
-  assumes "open s" "connected (s-pts)" "finite pts" "f holomorphic_on (s-pts) " "a\<in>s-pts" "b\<in>s-pts"
-  obtains g where "valid_path g" "pathstart g = a" "pathfinish g = b"
-    "path_image g \<subseteq> s-pts" "f contour_integrable_on g" using assms
-proof (induct arbitrary:s thesis a rule:finite_induct[OF \<open>finite pts\<close>])
-  case 1
-  obtain g where "valid_path g" "path_image g \<subseteq> s" "pathstart g = a" "pathfinish g = b"
-    using connected_open_polynomial_connected[OF \<open>open s\<close>,of a b ] \<open>connected (s - {})\<close>
-      valid_path_polynomial_function "1.prems"(6) "1.prems"(7) by auto
-  moreover have "f contour_integrable_on g"
-    using contour_integrable_holomorphic_simple[OF _ \<open>open s\<close> \<open>valid_path g\<close> \<open>path_image g \<subseteq> s\<close>,of f]
-      \<open>f holomorphic_on s - {}\<close>
-    by auto
-  ultimately show ?case using "1"(1)[of g] by auto
-next
-  case idt:(2 p pts)
-  obtain e where "e>0" and e:"\<forall>w\<in>ball a e. w \<in> s \<and> (w \<noteq> a \<longrightarrow> w \<notin> insert p pts)"
-    using finite_ball_avoid[OF \<open>open s\<close> \<open>finite (insert p pts)\<close>, of a]
-      \<open>a \<in> s - insert p pts\<close>
-    by auto
-  define a' where "a' \<equiv> a+e/2"
-  have "a'\<in>s-{p} -pts"  using e[rule_format,of "a+e/2"] \<open>e>0\<close>
-    by (auto simp add:dist_complex_def a'_def)
-  then obtain g' where g'[simp]:"valid_path g'" "pathstart g' = a'" "pathfinish g' = b"
-    "path_image g' \<subseteq> s - {p} - pts" "f contour_integrable_on g'"
-    using idt.hyps(3)[of a' "s-{p}"] idt.prems idt.hyps(1)
-    by (metis Diff_insert2 open_delete)
-  define g where "g \<equiv> linepath a a' +++ g'"
-  have "valid_path g" unfolding g_def by (auto intro: valid_path_join)
-  moreover have "pathstart g = a" and  "pathfinish g = b" unfolding g_def by auto
-  moreover have "path_image g \<subseteq> s - insert p pts" unfolding g_def
-    proof (rule subset_path_image_join)
-      have "closed_segment a a' \<subseteq> ball a e" using \<open>e>0\<close>
-        by (auto dest!:segment_bound1 simp:a'_def dist_complex_def norm_minus_commute)
-      then show "path_image (linepath a a') \<subseteq> s - insert p pts" using e idt(9)
-        by auto
-    next
-      show "path_image g' \<subseteq> s - insert p pts" using g'(4) by blast
-    qed
-  moreover have "f contour_integrable_on g"
-    proof -
-      have "closed_segment a a' \<subseteq> ball a e" using \<open>e>0\<close>
-        by (auto dest!:segment_bound1 simp:a'_def dist_complex_def norm_minus_commute)
-      then have "continuous_on (closed_segment a a') f"
-        using e idt.prems(6) holomorphic_on_imp_continuous_on[OF idt.prems(5)]
-        apply (elim continuous_on_subset)
-        by auto
-      then have "f contour_integrable_on linepath a a'"
-        using contour_integrable_continuous_linepath by auto
-      then show ?thesis unfolding g_def
-        apply (rule contour_integrable_joinI)
-        by (auto simp add: \<open>e>0\<close>)
-    qed
-  ultimately show ?case using idt.prems(1)[of g] by auto
-qed
-
-lemma Cauchy_theorem_aux:
-  assumes "open s" "connected (s-pts)" "finite pts" "pts \<subseteq> s" "f holomorphic_on s-pts"
-          "valid_path g" "pathfinish g = pathstart g" "path_image g \<subseteq> s-pts"
-          "\<forall>z. (z \<notin> s) \<longrightarrow> winding_number g z  = 0"
-          "\<forall>p\<in>s. h p>0 \<and> (\<forall>w\<in>cball p (h p). w\<in>s \<and> (w\<noteq>p \<longrightarrow> w \<notin> pts))"
-  shows "contour_integral g f = (\<Sum>p\<in>pts. winding_number g p * contour_integral (circlepath p (h p)) f)"
-    using assms
-proof (induct arbitrary:s g rule:finite_induct[OF \<open>finite pts\<close>])
-  case 1
-  then show ?case by (simp add: Cauchy_theorem_global contour_integral_unique)
-next
-  case (2 p pts)
-  note fin[simp] = \<open>finite (insert p pts)\<close>
-    and connected = \<open>connected (s - insert p pts)\<close>
-    and valid[simp] = \<open>valid_path g\<close>
-    and g_loop[simp] = \<open>pathfinish g = pathstart g\<close>
-    and holo[simp]= \<open>f holomorphic_on s - insert p pts\<close>
-    and path_img = \<open>path_image g \<subseteq> s - insert p pts\<close>
-    and winding = \<open>\<forall>z. z \<notin> s \<longrightarrow> winding_number g z = 0\<close>
-    and h = \<open>\<forall>pa\<in>s. 0 < h pa \<and> (\<forall>w\<in>cball pa (h pa). w \<in> s \<and> (w \<noteq> pa \<longrightarrow> w \<notin> insert p pts))\<close>
-  have "h p>0" and "p\<in>s"
-    and h_p: "\<forall>w\<in>cball p (h p). w \<in> s \<and> (w \<noteq> p \<longrightarrow> w \<notin> insert p pts)"
-    using h \<open>insert p pts \<subseteq> s\<close> by auto
-  obtain pg where pg[simp]: "valid_path pg" "pathstart pg = pathstart g" "pathfinish pg=p+h p"
-      "path_image pg \<subseteq> s-insert p pts" "f contour_integrable_on pg"
-    proof -
-      have "p + h p\<in>cball p (h p)" using h[rule_format,of p]
-        by (simp add: \<open>p \<in> s\<close> dist_norm)
-      then have "p + h p \<in> s - insert p pts" using h[rule_format,of p] \<open>insert p pts \<subseteq> s\<close>
-        by fastforce
-      moreover have "pathstart g \<in> s - insert p pts " using path_img by auto
-      ultimately show ?thesis
-        using get_integrable_path[OF \<open>open s\<close> connected fin holo,of "pathstart g" "p+h p"] that
-        by blast
-    qed
-  obtain n::int where "n=winding_number g p"
-    using integer_winding_number[OF _ g_loop,of p] valid path_img
-    by (metis DiffD2 Ints_cases insertI1 subset_eq valid_path_imp_path)
-  define p_circ where "p_circ \<equiv> circlepath p (h p)"
-  define p_circ_pt where "p_circ_pt \<equiv> linepath (p+h p) (p+h p)"
-  define n_circ where "n_circ \<equiv> \<lambda>n. ((+++) p_circ ^^ n) p_circ_pt"
-  define cp where "cp \<equiv> if n\<ge>0 then reversepath (n_circ (nat n)) else n_circ (nat (- n))"
-  have n_circ:"valid_path (n_circ k)"
-      "winding_number (n_circ k) p = k"
-      "pathstart (n_circ k) = p + h p" "pathfinish (n_circ k) = p + h p"
-      "path_image (n_circ k) =  (if k=0 then {p + h p} else sphere p (h p))"
-      "p \<notin> path_image (n_circ k)"
-      "\<And>p'. p'\<notin>s - pts \<Longrightarrow> winding_number (n_circ k) p'=0 \<and> p'\<notin>path_image (n_circ k)"
-      "f contour_integrable_on (n_circ k)"
-      "contour_integral (n_circ k) f = k *  contour_integral p_circ f"
-      for k
-    proof (induct k)
-      case 0
-      show "valid_path (n_circ 0)"
-        and "path_image (n_circ 0) =  (if 0=0 then {p + h p} else sphere p (h p))"
-        and "winding_number (n_circ 0) p = of_nat 0"
-        and "pathstart (n_circ 0) = p + h p"
-        and "pathfinish (n_circ 0) = p + h p"
-        and "p \<notin> path_image (n_circ 0)"
-        unfolding n_circ_def p_circ_pt_def using \<open>h p > 0\<close>
-        by (auto simp add: dist_norm)
-      show "winding_number (n_circ 0) p'=0 \<and> p'\<notin>path_image (n_circ 0)" when "p'\<notin>s- pts" for p'
-        unfolding n_circ_def p_circ_pt_def
-        apply (auto intro!:winding_number_trivial)
-        by (metis Diff_iff pathfinish_in_path_image pg(3) pg(4) subsetCE subset_insertI that)+
-      show "f contour_integrable_on (n_circ 0)"
-        unfolding n_circ_def p_circ_pt_def
-        by (auto intro!:contour_integrable_continuous_linepath simp add:continuous_on_sing)
-      show "contour_integral (n_circ 0) f = of_nat 0  *  contour_integral p_circ f"
-        unfolding n_circ_def p_circ_pt_def by auto
-    next
-      case (Suc k)
-      have n_Suc:"n_circ (Suc k) = p_circ +++ n_circ k" unfolding n_circ_def by auto
-      have pcirc:"p \<notin> path_image p_circ" "valid_path p_circ" "pathfinish p_circ = pathstart (n_circ k)"
-        using Suc(3) unfolding p_circ_def using \<open>h p > 0\<close> by (auto simp add: p_circ_def)
-      have pcirc_image:"path_image p_circ \<subseteq> s - insert p pts"
-        proof -
-          have "path_image p_circ \<subseteq> cball p (h p)" using \<open>0 < h p\<close> p_circ_def by auto
-          then show ?thesis using h_p pcirc(1) by auto
-        qed
-      have pcirc_integrable:"f contour_integrable_on p_circ"
-        by (auto simp add:p_circ_def intro!: pcirc_image[unfolded p_circ_def]
-          contour_integrable_continuous_circlepath holomorphic_on_imp_continuous_on
-          holomorphic_on_subset[OF holo])
-      show "valid_path (n_circ (Suc k))"
-        using valid_path_join[OF pcirc(2) Suc(1) pcirc(3)] unfolding n_circ_def by auto
-      show "path_image (n_circ (Suc k))
-          = (if Suc k = 0 then {p + complex_of_real (h p)} else sphere p (h p))"
-        proof -
-          have "path_image p_circ = sphere p (h p)"
-            unfolding p_circ_def using \<open>0 < h p\<close> by auto
-          then show ?thesis unfolding n_Suc  using Suc.hyps(5)  \<open>h p>0\<close>
-            by (auto simp add:  path_image_join[OF pcirc(3)]  dist_norm)
-        qed
-      then show "p \<notin> path_image (n_circ (Suc k))" using \<open>h p>0\<close> by auto
-      show "winding_number (n_circ (Suc k)) p = of_nat (Suc k)"
-        proof -
-          have "winding_number p_circ p = 1"
-            by (simp add: \<open>h p > 0\<close> p_circ_def winding_number_circlepath_centre)
-          moreover have "p \<notin> path_image (n_circ k)" using Suc(5) \<open>h p>0\<close> by auto
-          then have "winding_number (p_circ +++ n_circ k) p
-              = winding_number p_circ p + winding_number (n_circ k) p"
-            using  valid_path_imp_path Suc.hyps(1) Suc.hyps(2) pcirc
-            apply (intro winding_number_join)
-            by auto
-          ultimately show ?thesis using Suc(2) unfolding n_circ_def
-            by auto
-        qed
-      show "pathstart (n_circ (Suc k)) = p + h p"
-        by (simp add: n_circ_def p_circ_def)
-      show "pathfinish (n_circ (Suc k)) = p + h p"
-        using Suc(4) unfolding n_circ_def by auto
-      show "winding_number (n_circ (Suc k)) p'=0 \<and>  p'\<notin>path_image (n_circ (Suc k))" when "p'\<notin>s-pts" for p'
-        proof -
-          have " p' \<notin> path_image p_circ" using \<open>p \<in> s\<close> h p_circ_def that using pcirc_image by blast
-          moreover have "p' \<notin> path_image (n_circ k)"
-            using Suc.hyps(7) that by blast
-          moreover have "winding_number p_circ p' = 0"
-            proof -
-              have "path_image p_circ \<subseteq> cball p (h p)"
-                using h unfolding p_circ_def using \<open>p \<in> s\<close> by fastforce
-              moreover have "p'\<notin>cball p (h p)" using \<open>p \<in> s\<close> h that "2.hyps"(2) by fastforce
-              ultimately show ?thesis unfolding p_circ_def
-                apply (intro winding_number_zero_outside)
-                by auto
-            qed
-          ultimately show ?thesis
-            unfolding n_Suc
-            apply (subst winding_number_join)
-            by (auto simp: valid_path_imp_path pcirc Suc that not_in_path_image_join Suc.hyps(7)[OF that])
-        qed
-      show "f contour_integrable_on (n_circ (Suc k))"
-        unfolding n_Suc
-        by (rule contour_integrable_joinI[OF pcirc_integrable Suc(8) pcirc(2) Suc(1)])
-      show "contour_integral (n_circ (Suc k)) f = (Suc k) *  contour_integral p_circ f"
-        unfolding n_Suc
-        by (auto simp add:contour_integral_join[OF pcirc_integrable Suc(8) pcirc(2) Suc(1)]
-          Suc(9) algebra_simps)
-    qed
-  have cp[simp]:"pathstart cp = p + h p"  "pathfinish cp = p + h p"
-         "valid_path cp" "path_image cp \<subseteq> s - insert p pts"
-         "winding_number cp p = - n"
-         "\<And>p'. p'\<notin>s - pts \<Longrightarrow> winding_number cp p'=0 \<and> p' \<notin> path_image cp"
-         "f contour_integrable_on cp"
-         "contour_integral cp f = - n * contour_integral p_circ f"
-    proof -
-      show "pathstart cp = p + h p" and "pathfinish cp = p + h p" and "valid_path cp"
-        using n_circ unfolding cp_def by auto
-    next
-      have "sphere p (h p) \<subseteq>  s - insert p pts"
-        using h[rule_format,of p] \<open>insert p pts \<subseteq> s\<close> by force
-      moreover  have "p + complex_of_real (h p) \<in> s - insert p pts"
-        using pg(3) pg(4) by (metis pathfinish_in_path_image subsetCE)
-      ultimately show "path_image cp \<subseteq>  s - insert p pts" unfolding cp_def
-        using n_circ(5)  by auto
-    next
-      show "winding_number cp p = - n"
-        unfolding cp_def using winding_number_reversepath n_circ \<open>h p>0\<close>
-        by (auto simp: valid_path_imp_path)
-    next
-      show "winding_number cp p'=0 \<and> p' \<notin> path_image cp" when "p'\<notin>s - pts" for p'
-        unfolding cp_def
-        apply (auto)
-        apply (subst winding_number_reversepath)
-        by (auto simp add: valid_path_imp_path n_circ(7)[OF that] n_circ(1))
-    next
-      show "f contour_integrable_on cp" unfolding cp_def
-        using contour_integrable_reversepath_eq n_circ(1,8) by auto
-    next
-      show "contour_integral cp f = - n * contour_integral p_circ f"
-        unfolding cp_def using contour_integral_reversepath[OF n_circ(1)] n_circ(9)
-        by auto
-    qed
-  define g' where "g' \<equiv> g +++ pg +++ cp +++ (reversepath pg)"
-  have "contour_integral g' f = (\<Sum>p\<in>pts. winding_number g' p * contour_integral (circlepath p (h p)) f)"
-    proof (rule "2.hyps"(3)[of "s-{p}" "g'",OF _ _ \<open>finite pts\<close> ])
-      show "connected (s - {p} - pts)" using connected by (metis Diff_insert2)
-      show "open (s - {p})" using \<open>open s\<close> by auto
-      show " pts \<subseteq> s - {p}" using \<open>insert p pts \<subseteq> s\<close> \<open> p \<notin> pts\<close>  by blast
-      show "f holomorphic_on s - {p} - pts" using holo \<open>p \<notin> pts\<close> by (metis Diff_insert2)
-      show "valid_path g'"
-        unfolding g'_def cp_def using n_circ valid pg g_loop
-        by (auto intro!:valid_path_join )
-      show "pathfinish g' = pathstart g'"
-        unfolding g'_def cp_def using pg(2) by simp
-      show "path_image g' \<subseteq> s - {p} - pts"
-        proof -
-          define s' where "s' \<equiv> s - {p} - pts"
-          have s':"s' = s-insert p pts " unfolding s'_def by auto
-          then show ?thesis using path_img pg(4) cp(4)
-            unfolding g'_def
-            apply (fold s'_def s')
-            apply (intro subset_path_image_join)
-            by auto
-        qed
-      note path_join_imp[simp]
-      show "\<forall>z. z \<notin> s - {p} \<longrightarrow> winding_number g' z = 0"
-        proof clarify
-          fix z assume z:"z\<notin>s - {p}"
-          have "winding_number (g +++ pg +++ cp +++ reversepath pg) z = winding_number g z
-              + winding_number (pg +++ cp +++ (reversepath pg)) z"
-            proof (rule winding_number_join)
-              show "path g" using \<open>valid_path g\<close> by (simp add: valid_path_imp_path)
-              show "z \<notin> path_image g" using z path_img by auto
-              show "path (pg +++ cp +++ reversepath pg)" using pg(3) cp
-                by (simp add: valid_path_imp_path)
-            next
-              have "path_image (pg +++ cp +++ reversepath pg) \<subseteq> s - insert p pts"
-                using pg(4) cp(4) by (auto simp:subset_path_image_join)
-              then show "z \<notin> path_image (pg +++ cp +++ reversepath pg)" using z by auto
-            next
-              show "pathfinish g = pathstart (pg +++ cp +++ reversepath pg)" using g_loop by auto
-            qed
-          also have "... = winding_number g z + (winding_number pg z
-              + winding_number (cp +++ (reversepath pg)) z)"
-            proof (subst add_left_cancel,rule winding_number_join)
-              show "path pg" and "path (cp +++ reversepath pg)"
-               and "pathfinish pg = pathstart (cp +++ reversepath pg)"
-                by (auto simp add: valid_path_imp_path)
-              show "z \<notin> path_image pg" using pg(4) z by blast
-              show "z \<notin> path_image (cp +++ reversepath pg)" using z
-                by (metis Diff_iff \<open>z \<notin> path_image pg\<close> contra_subsetD cp(4) insertI1
-                  not_in_path_image_join path_image_reversepath singletonD)
-            qed
-          also have "... = winding_number g z + (winding_number pg z
-              + (winding_number cp z + winding_number (reversepath pg) z))"
-            apply (auto intro!:winding_number_join simp: valid_path_imp_path)
-            apply (metis Diff_iff contra_subsetD cp(4) insertI1 singletonD z)
-            by (metis Diff_insert2 Diff_subset contra_subsetD pg(4) z)
-          also have "... = winding_number g z + winding_number cp z"
-            apply (subst winding_number_reversepath)
-            apply (auto simp: valid_path_imp_path)
-            by (metis Diff_iff contra_subsetD insertI1 pg(4) singletonD z)
-          finally have "winding_number g' z = winding_number g z + winding_number cp z"
-            unfolding g'_def .
-          moreover have "winding_number g z + winding_number cp z = 0"
-            using winding z \<open>n=winding_number g p\<close> by auto
-          ultimately show "winding_number g' z = 0" unfolding g'_def by auto
-        qed
-      show "\<forall>pa\<in>s - {p}. 0 < h pa \<and> (\<forall>w\<in>cball pa (h pa). w \<in> s - {p} \<and> (w \<noteq> pa \<longrightarrow> w \<notin> pts))"
-        using h by fastforce
-    qed
-  moreover have "contour_integral g' f = contour_integral g f
-      - winding_number g p * contour_integral p_circ f"
-    proof -
-      have "contour_integral g' f =  contour_integral g f
-        + contour_integral (pg +++ cp +++ reversepath pg) f"
-        unfolding g'_def
-        apply (subst contour_integral_join)
-        by (auto simp add:open_Diff[OF \<open>open s\<close>,OF finite_imp_closed[OF fin]]
-          intro!: contour_integrable_holomorphic_simple[OF holo _ _ path_img]
-          contour_integrable_reversepath)
-      also have "... = contour_integral g f + contour_integral pg f
-          + contour_integral (cp +++ reversepath pg) f"
-        apply (subst contour_integral_join)
-        by (auto simp add:contour_integrable_reversepath)
-      also have "... = contour_integral g f + contour_integral pg f
-          + contour_integral cp f + contour_integral (reversepath pg) f"
-        apply (subst contour_integral_join)
-        by (auto simp add:contour_integrable_reversepath)
-      also have "... = contour_integral g f + contour_integral cp f"
-        using contour_integral_reversepath
-        by (auto simp add:contour_integrable_reversepath)
-      also have "... = contour_integral g f - winding_number g p * contour_integral p_circ f"
-        using \<open>n=winding_number g p\<close> by auto
-      finally show ?thesis .
-    qed
-  moreover have "winding_number g' p' = winding_number g p'" when "p'\<in>pts" for p'
-    proof -
-      have [simp]: "p' \<notin> path_image g" "p' \<notin> path_image pg" "p'\<notin>path_image cp"
-        using "2.prems"(8) that
-        apply blast
-        apply (metis Diff_iff Diff_insert2 contra_subsetD pg(4) that)
-        by (meson DiffD2 cp(4) rev_subsetD subset_insertI that)
-      have "winding_number g' p' = winding_number g p'
-          + winding_number (pg +++ cp +++ reversepath pg) p'" unfolding g'_def
-        apply (subst winding_number_join)
-        apply (simp_all add: valid_path_imp_path)
-        apply (intro not_in_path_image_join)
-        by auto
-      also have "... = winding_number g p' + winding_number pg p'
-          + winding_number (cp +++ reversepath pg) p'"
-        apply (subst winding_number_join)
-        apply (simp_all add: valid_path_imp_path)
-        apply (intro not_in_path_image_join)
-        by auto
-      also have "... = winding_number g p' + winding_number pg p'+ winding_number cp p'
-          + winding_number (reversepath pg) p'"
-        apply (subst winding_number_join)
-        by (simp_all add: valid_path_imp_path)
-      also have "... = winding_number g p' + winding_number cp p'"
-        apply (subst winding_number_reversepath)
-        by (simp_all add: valid_path_imp_path)
-      also have "... = winding_number g p'" using that by auto
-      finally show ?thesis .
-    qed
-  ultimately show ?case unfolding p_circ_def
-    apply (subst (asm) sum.cong[OF refl,
-        of pts _ "\<lambda>p. winding_number g p * contour_integral (circlepath p (h p)) f"])
-    by (auto simp add:sum.insert[OF \<open>finite pts\<close> \<open>p\<notin>pts\<close>] algebra_simps)
-qed
-
-lemma Cauchy_theorem_singularities:
-  assumes "open s" "connected s" "finite pts" and
-          holo:"f holomorphic_on s-pts" and
-          "valid_path g" and
-          loop:"pathfinish g = pathstart g" and
-          "path_image g \<subseteq> s-pts" and
-          homo:"\<forall>z. (z \<notin> s) \<longrightarrow> winding_number g z  = 0" and
-          avoid:"\<forall>p\<in>s. h p>0 \<and> (\<forall>w\<in>cball p (h p). w\<in>s \<and> (w\<noteq>p \<longrightarrow> w \<notin> pts))"
-  shows "contour_integral g f = (\<Sum>p\<in>pts. winding_number g p * contour_integral (circlepath p (h p)) f)"
-    (is "?L=?R")
-proof -
-  define circ where "circ \<equiv> \<lambda>p. winding_number g p * contour_integral (circlepath p (h p)) f"
-  define pts1 where "pts1 \<equiv> pts \<inter> s"
-  define pts2 where "pts2 \<equiv> pts - pts1"
-  have "pts=pts1 \<union> pts2" "pts1 \<inter> pts2 = {}" "pts2 \<inter> s={}" "pts1\<subseteq>s"
-    unfolding pts1_def pts2_def by auto
-  have "contour_integral g f =  (\<Sum>p\<in>pts1. circ p)" unfolding circ_def
-    proof (rule Cauchy_theorem_aux[OF \<open>open s\<close> _ _ \<open>pts1\<subseteq>s\<close> _ \<open>valid_path g\<close> loop _ homo])
-      have "finite pts1" unfolding pts1_def using \<open>finite pts\<close> by auto
-      then show "connected (s - pts1)"
-        using \<open>open s\<close> \<open>connected s\<close> connected_open_delete_finite[of s] by auto
-    next
-      show "finite pts1" using \<open>pts = pts1 \<union> pts2\<close> assms(3) by auto
-      show "f holomorphic_on s - pts1" by (metis Diff_Int2 Int_absorb holo pts1_def)
-      show "path_image g \<subseteq> s - pts1" using assms(7) pts1_def by auto
-      show "\<forall>p\<in>s. 0 < h p \<and> (\<forall>w\<in>cball p (h p). w \<in> s \<and> (w \<noteq> p \<longrightarrow> w \<notin> pts1))"
-        by (simp add: avoid pts1_def)
-    qed
-  moreover have "sum circ pts2=0"
-    proof -
-      have "winding_number g p=0" when "p\<in>pts2" for p
-        using  \<open>pts2 \<inter> s={}\<close> that homo[rule_format,of p] by auto
-      thus ?thesis unfolding circ_def
-        apply (intro sum.neutral)
-        by auto
-    qed
-  moreover have "?R=sum circ pts1 + sum circ pts2"
-    unfolding circ_def
-    using sum.union_disjoint[OF _ _ \<open>pts1 \<inter> pts2 = {}\<close>] \<open>finite pts\<close> \<open>pts=pts1 \<union> pts2\<close>
-    by blast
-  ultimately show ?thesis
-    apply (fold circ_def)
-    by auto
-qed
-
-theorem Residue_theorem:
-  fixes s pts::"complex set" and f::"complex \<Rightarrow> complex"
-    and g::"real \<Rightarrow> complex"
-  assumes "open s" "connected s" "finite pts" and
-          holo:"f holomorphic_on s-pts" and
-          "valid_path g" and
-          loop:"pathfinish g = pathstart g" and
-          "path_image g \<subseteq> s-pts" and
-          homo:"\<forall>z. (z \<notin> s) \<longrightarrow> winding_number g z  = 0"
-  shows "contour_integral g f = 2 * pi * \<i> *(\<Sum>p\<in>pts. winding_number g p * residue f p)"
-proof -
-  define c where "c \<equiv>  2 * pi * \<i>"
-  obtain h where avoid:"\<forall>p\<in>s. h p>0 \<and> (\<forall>w\<in>cball p (h p). w\<in>s \<and> (w\<noteq>p \<longrightarrow> w \<notin> pts))"
-    using finite_cball_avoid[OF \<open>open s\<close> \<open>finite pts\<close>] by metis
-  have "contour_integral g f
-      = (\<Sum>p\<in>pts. winding_number g p * contour_integral (circlepath p (h p)) f)"
-    using Cauchy_theorem_singularities[OF assms avoid] .
-  also have "... = (\<Sum>p\<in>pts.  c * winding_number g p * residue f p)"
-    proof (intro sum.cong)
-      show "pts = pts" by simp
-    next
-      fix x assume "x \<in> pts"
-      show "winding_number g x * contour_integral (circlepath x (h x)) f
-          = c * winding_number g x * residue f x"
-        proof (cases "x\<in>s")
-          case False
-          then have "winding_number g x=0" using homo by auto
-          thus ?thesis by auto
-        next
-          case True
-          have "contour_integral (circlepath x (h x)) f = c* residue f x"
-            using \<open>x\<in>pts\<close> \<open>finite pts\<close> avoid[rule_format,OF True]
-            apply (intro base_residue[of "s-(pts-{x})",THEN contour_integral_unique,folded c_def])
-            by (auto intro:holomorphic_on_subset[OF holo] open_Diff[OF \<open>open s\<close> finite_imp_closed])
-          then show ?thesis by auto
-        qed
-    qed
-  also have "... = c * (\<Sum>p\<in>pts. winding_number g p * residue f p)"
-    by (simp add: sum_distrib_left algebra_simps)
-  finally show ?thesis unfolding c_def .
-qed
-
-subsection \<open>Non-essential singular points\<close>
-
-definition\<^marker>\<open>tag important\<close> is_pole ::
-  "('a::topological_space \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a \<Rightarrow> bool" where
-  "is_pole f a =  (LIM x (at a). f x :> at_infinity)"
-
-lemma is_pole_cong:
-  assumes "eventually (\<lambda>x. f x = g x) (at a)" "a=b"
-  shows "is_pole f a \<longleftrightarrow> is_pole g b"
-  unfolding is_pole_def using assms by (intro filterlim_cong,auto)
-
-lemma is_pole_transform:
-  assumes "is_pole f a" "eventually (\<lambda>x. f x = g x) (at a)" "a=b"
-  shows "is_pole g b"
-  using is_pole_cong assms by auto
-
-lemma is_pole_tendsto:
-  fixes f::"('a::topological_space \<Rightarrow> 'b::real_normed_div_algebra)"
-  shows "is_pole f x \<Longrightarrow> ((inverse o f) \<longlongrightarrow> 0) (at x)"
-unfolding is_pole_def
-by (auto simp add:filterlim_inverse_at_iff[symmetric] comp_def filterlim_at)
-
-lemma is_pole_inverse_holomorphic:
-  assumes "open s"
-    and f_holo:"f holomorphic_on (s-{z})"
-    and pole:"is_pole f z"
-    and non_z:"\<forall>x\<in>s-{z}. f x\<noteq>0"
-  shows "(\<lambda>x. if x=z then 0 else inverse (f x)) holomorphic_on s"
-proof -
-  define g where "g \<equiv> \<lambda>x. if x=z then 0 else inverse (f x)"
-  have "isCont g z" unfolding isCont_def  using is_pole_tendsto[OF pole]
-    apply (subst Lim_cong_at[where b=z and y=0 and g="inverse \<circ> f"])
-    by (simp_all add:g_def)
-  moreover have "continuous_on (s-{z}) f" using f_holo holomorphic_on_imp_continuous_on by auto
-  hence "continuous_on (s-{z}) (inverse o f)" unfolding comp_def
-    by (auto elim!:continuous_on_inverse simp add:non_z)
-  hence "continuous_on (s-{z}) g" unfolding g_def
-    apply (subst continuous_on_cong[where t="s-{z}" and g="inverse o f"])
-    by auto
-  ultimately have "continuous_on s g" using open_delete[OF \<open>open s\<close>] \<open>open s\<close>
-    by (auto simp add:continuous_on_eq_continuous_at)
-  moreover have "(inverse o f) holomorphic_on (s-{z})"
-    unfolding comp_def using f_holo
-    by (auto elim!:holomorphic_on_inverse simp add:non_z)
-  hence "g holomorphic_on (s-{z})"
-    apply (subst holomorphic_cong[where t="s-{z}" and g="inverse o f"])
-    by (auto simp add:g_def)
-  ultimately show ?thesis unfolding g_def using \<open>open s\<close>
-    by (auto elim!: no_isolated_singularity)
-qed
-
-lemma not_is_pole_holomorphic:
-  assumes "open A" "x \<in> A" "f holomorphic_on A"
-  shows   "\<not>is_pole f x"
-proof -
-  have "continuous_on A f" by (intro holomorphic_on_imp_continuous_on) fact
-  with assms have "isCont f x" by (simp add: continuous_on_eq_continuous_at)
-  hence "f \<midarrow>x\<rightarrow> f x" by (simp add: isCont_def)
-  thus "\<not>is_pole f x" unfolding is_pole_def
-    using not_tendsto_and_filterlim_at_infinity[of "at x" f "f x"] by auto
-qed
-
-lemma is_pole_inverse_power: "n > 0 \<Longrightarrow> is_pole (\<lambda>z::complex. 1 / (z - a) ^ n) a"
-  unfolding is_pole_def inverse_eq_divide [symmetric]
-  by (intro filterlim_compose[OF filterlim_inverse_at_infinity] tendsto_intros)
-     (auto simp: filterlim_at eventually_at intro!: exI[of _ 1] tendsto_eq_intros)
-
-lemma is_pole_inverse: "is_pole (\<lambda>z::complex. 1 / (z - a)) a"
-  using is_pole_inverse_power[of 1 a] by simp
-
-lemma is_pole_divide:
-  fixes f :: "'a :: t2_space \<Rightarrow> 'b :: real_normed_field"
-  assumes "isCont f z" "filterlim g (at 0) (at z)" "f z \<noteq> 0"
-  shows   "is_pole (\<lambda>z. f z / g z) z"
-proof -
-  have "filterlim (\<lambda>z. f z * inverse (g z)) at_infinity (at z)"
-    by (intro tendsto_mult_filterlim_at_infinity[of _ "f z"]
-                 filterlim_compose[OF filterlim_inverse_at_infinity])+
-       (insert assms, auto simp: isCont_def)
-  thus ?thesis by (simp add: field_split_simps is_pole_def)
-qed
-
-lemma is_pole_basic:
-  assumes "f holomorphic_on A" "open A" "z \<in> A" "f z \<noteq> 0" "n > 0"
-  shows   "is_pole (\<lambda>w. f w / (w - z) ^ n) z"
-proof (rule is_pole_divide)
-  have "continuous_on A f" by (rule holomorphic_on_imp_continuous_on) fact
-  with assms show "isCont f z" by (auto simp: continuous_on_eq_continuous_at)
-  have "filterlim (\<lambda>w. (w - z) ^ n) (nhds 0) (at z)"
-    using assms by (auto intro!: tendsto_eq_intros)
-  thus "filterlim (\<lambda>w. (w - z) ^ n) (at 0) (at z)"
-    by (intro filterlim_atI tendsto_eq_intros)
-       (insert assms, auto simp: eventually_at_filter)
-qed fact+
-
-lemma is_pole_basic':
-  assumes "f holomorphic_on A" "open A" "0 \<in> A" "f 0 \<noteq> 0" "n > 0"
-  shows   "is_pole (\<lambda>w. f w / w ^ n) 0"
-  using is_pole_basic[of f A 0] assms by simp
-
-text \<open>The proposition
-              \<^term>\<open>\<exists>x. ((f::complex\<Rightarrow>complex) \<longlongrightarrow> x) (at z) \<or> is_pole f z\<close>
-can be interpreted as the complex function \<^term>\<open>f\<close> has a non-essential singularity at \<^term>\<open>z\<close>
-(i.e. the singularity is either removable or a pole).\<close>
-definition not_essential::"[complex \<Rightarrow> complex, complex] \<Rightarrow> bool" where
-  "not_essential f z = (\<exists>x. f\<midarrow>z\<rightarrow>x \<or> is_pole f z)"
-
-definition isolated_singularity_at::"[complex \<Rightarrow> complex, complex] \<Rightarrow> bool" where
-  "isolated_singularity_at f z = (\<exists>r>0. f analytic_on ball z r-{z})"
-
-named_theorems singularity_intros "introduction rules for singularities"
-
-lemma holomorphic_factor_unique:
-  fixes f::"complex \<Rightarrow> complex" and z::complex and r::real and m n::int
-  assumes "r>0" "g z\<noteq>0" "h z\<noteq>0"
-    and asm:"\<forall>w\<in>ball z r-{z}. f w = g w * (w-z) powr n \<and> g w\<noteq>0 \<and> f w =  h w * (w - z) powr m \<and> h w\<noteq>0"
-    and g_holo:"g holomorphic_on ball z r" and h_holo:"h holomorphic_on ball z r"
-  shows "n=m"
-proof -
-  have [simp]:"at z within ball z r \<noteq> bot" using \<open>r>0\<close>
-      by (auto simp add:at_within_ball_bot_iff)
-  have False when "n>m"
-  proof -
-    have "(h \<longlongrightarrow> 0) (at z within ball z r)"
-    proof (rule Lim_transform_within[OF _ \<open>r>0\<close>, where f="\<lambda>w. (w - z) powr (n - m) * g w"])
-      have "\<forall>w\<in>ball z r-{z}. h w = (w-z)powr(n-m) * g w"
-        using \<open>n>m\<close> asm \<open>r>0\<close>
-        apply (auto simp add:field_simps powr_diff)
-        by force
-      then show "\<lbrakk>x' \<in> ball z r; 0 < dist x' z;dist x' z < r\<rbrakk>
-            \<Longrightarrow> (x' - z) powr (n - m) * g x' = h x'" for x' by auto
-    next
-      define F where "F \<equiv> at z within ball z r"
-      define f' where "f' \<equiv> \<lambda>x. (x - z) powr (n-m)"
-      have "f' z=0" using \<open>n>m\<close> unfolding f'_def by auto
-      moreover have "continuous F f'" unfolding f'_def F_def continuous_def
-        apply (subst Lim_ident_at)
-        using \<open>n>m\<close> by (auto intro!:tendsto_powr_complex_0 tendsto_eq_intros)
-      ultimately have "(f' \<longlongrightarrow> 0) F" unfolding F_def
-        by (simp add: continuous_within)
-      moreover have "(g \<longlongrightarrow> g z) F"
-        using holomorphic_on_imp_continuous_on[OF g_holo,unfolded continuous_on_def] \<open>r>0\<close>
-        unfolding F_def by auto
-      ultimately show " ((\<lambda>w. f' w * g w) \<longlongrightarrow> 0) F" using tendsto_mult by fastforce
-    qed
-    moreover have "(h \<longlongrightarrow> h z) (at z within ball z r)"
-      using holomorphic_on_imp_continuous_on[OF h_holo]
-      by (auto simp add:continuous_on_def \<open>r>0\<close>)
-    ultimately have "h z=0" by (auto intro!: tendsto_unique)
-    thus False using \<open>h z\<noteq>0\<close> by auto
-  qed
-  moreover have False when "m>n"
-  proof -
-    have "(g \<longlongrightarrow> 0) (at z within ball z r)"
-    proof (rule Lim_transform_within[OF _ \<open>r>0\<close>, where f="\<lambda>w. (w - z) powr (m - n) * h w"])
-      have "\<forall>w\<in>ball z r -{z}. g w = (w-z) powr (m-n) * h w" using \<open>m>n\<close> asm
-        apply (auto simp add:field_simps powr_diff)
-        by force
-      then show "\<lbrakk>x' \<in> ball z r; 0 < dist x' z;dist x' z < r\<rbrakk>
-            \<Longrightarrow> (x' - z) powr (m - n) * h x' = g x'" for x' by auto
-    next
-      define F where "F \<equiv> at z within ball z r"
-      define f' where "f' \<equiv>\<lambda>x. (x - z) powr (m-n)"
-      have "f' z=0" using \<open>m>n\<close> unfolding f'_def by auto
-      moreover have "continuous F f'" unfolding f'_def F_def continuous_def
-        apply (subst Lim_ident_at)
-        using \<open>m>n\<close> by (auto intro!:tendsto_powr_complex_0 tendsto_eq_intros)
-      ultimately have "(f' \<longlongrightarrow> 0) F" unfolding F_def
-        by (simp add: continuous_within)
-      moreover have "(h \<longlongrightarrow> h z) F"
-        using holomorphic_on_imp_continuous_on[OF h_holo,unfolded continuous_on_def] \<open>r>0\<close>
-        unfolding F_def by auto
-      ultimately show " ((\<lambda>w. f' w * h w) \<longlongrightarrow> 0) F" using tendsto_mult by fastforce
-    qed
-    moreover have "(g \<longlongrightarrow> g z) (at z within ball z r)"
-      using holomorphic_on_imp_continuous_on[OF g_holo]
-      by (auto simp add:continuous_on_def \<open>r>0\<close>)
-    ultimately have "g z=0" by (auto intro!: tendsto_unique)
-    thus False using \<open>g z\<noteq>0\<close> by auto
-  qed
-  ultimately show "n=m" by fastforce
-qed
-
-lemma holomorphic_factor_puncture:
-  assumes f_iso:"isolated_singularity_at f z"
-      and "not_essential f z" \<comment> \<open>\<^term>\<open>f\<close> has either a removable singularity or a pole at \<^term>\<open>z\<close>\<close>
-      and non_zero:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0" \<comment> \<open>\<^term>\<open>f\<close> will not be constantly zero in a neighbour of \<^term>\<open>z\<close>\<close>
-  shows "\<exists>!n::int. \<exists>g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z\<noteq>0
-          \<and> (\<forall>w\<in>cball z r-{z}. f w = g w * (w-z) powr n \<and> g w\<noteq>0)"
-proof -
-  define P where "P = (\<lambda>f n g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z\<noteq>0
-          \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w-z) powr (of_int n)  \<and> g w\<noteq>0))"
-  have imp_unique:"\<exists>!n::int. \<exists>g r. P f n g r" when "\<exists>n g r. P f n g r"
-  proof (rule ex_ex1I[OF that])
-    fix n1 n2 :: int
-    assume g1_asm:"\<exists>g1 r1. P f n1 g1 r1" and g2_asm:"\<exists>g2 r2. P f n2 g2 r2"
-    define fac where "fac \<equiv> \<lambda>n g r. \<forall>w\<in>cball z r-{z}. f w = g w * (w - z) powr (of_int n) \<and> g w \<noteq> 0"
-    obtain g1 r1 where "0 < r1" and g1_holo: "g1 holomorphic_on cball z r1" and "g1 z\<noteq>0"
-        and "fac n1 g1 r1" using g1_asm unfolding P_def fac_def by auto
-    obtain g2 r2 where "0 < r2" and g2_holo: "g2 holomorphic_on cball z r2" and "g2 z\<noteq>0"
-        and "fac n2 g2 r2" using g2_asm unfolding P_def fac_def by auto
-    define r where "r \<equiv> min r1 r2"
-    have "r>0" using \<open>r1>0\<close> \<open>r2>0\<close> unfolding r_def by auto
-    moreover have "\<forall>w\<in>ball z r-{z}. f w = g1 w * (w-z) powr n1 \<and> g1 w\<noteq>0
-        \<and> f w = g2 w * (w - z) powr n2  \<and> g2 w\<noteq>0"
-      using \<open>fac n1 g1 r1\<close> \<open>fac n2 g2 r2\<close>   unfolding fac_def r_def
-      by fastforce
-    ultimately show "n1=n2" using g1_holo g2_holo \<open>g1 z\<noteq>0\<close> \<open>g2 z\<noteq>0\<close>
-      apply (elim holomorphic_factor_unique)
-      by (auto simp add:r_def)
-  qed
-
-  have P_exist:"\<exists> n g r. P h n g r" when
-      "\<exists>z'. (h \<longlongrightarrow> z') (at z)" "isolated_singularity_at h z"  "\<exists>\<^sub>Fw in (at z). h w\<noteq>0"
-    for h
-  proof -
-    from that(2) obtain r where "r>0" "h analytic_on ball z r - {z}"
-      unfolding isolated_singularity_at_def by auto
-    obtain z' where "(h \<longlongrightarrow> z') (at z)" using \<open>\<exists>z'. (h \<longlongrightarrow> z') (at z)\<close> by auto
-    define h' where "h'=(\<lambda>x. if x=z then z' else h x)"
-    have "h' holomorphic_on ball z r"
-      apply (rule no_isolated_singularity'[of "{z}"])
-      subgoal by (metis LIM_equal Lim_at_imp_Lim_at_within \<open>h \<midarrow>z\<rightarrow> z'\<close> empty_iff h'_def insert_iff)
-      subgoal using \<open>h analytic_on ball z r - {z}\<close> analytic_imp_holomorphic h'_def holomorphic_transform
-        by fastforce
-      by auto
-    have ?thesis when "z'=0"
-    proof -
-      have "h' z=0" using that unfolding h'_def by auto
-      moreover have "\<not> h' constant_on ball z r"
-        using \<open>\<exists>\<^sub>Fw in (at z). h w\<noteq>0\<close> unfolding constant_on_def frequently_def eventually_at h'_def
-        apply simp
-        by (metis \<open>0 < r\<close> centre_in_ball dist_commute mem_ball that)
-      moreover note \<open>h' holomorphic_on ball z r\<close>
-      ultimately obtain g r1 n where "0 < n" "0 < r1" "ball z r1 \<subseteq> ball z r" and
-          g:"g holomorphic_on ball z r1"
-          "\<And>w. w \<in> ball z r1 \<Longrightarrow> h' w = (w - z) ^ n * g w"
-          "\<And>w. w \<in> ball z r1 \<Longrightarrow> g w \<noteq> 0"
-        using holomorphic_factor_zero_nonconstant[of _ "ball z r" z thesis,simplified,
-                OF \<open>h' holomorphic_on ball z r\<close> \<open>r>0\<close> \<open>h' z=0\<close> \<open>\<not> h' constant_on ball z r\<close>]
-        by (auto simp add:dist_commute)
-      define rr where "rr=r1/2"
-      have "P h' n g rr"
-        unfolding P_def rr_def
-        using \<open>n>0\<close> \<open>r1>0\<close> g by (auto simp add:powr_nat)
-      then have "P h n g rr"
-        unfolding h'_def P_def by auto
-      then show ?thesis unfolding P_def by blast
-    qed
-    moreover have ?thesis when "z'\<noteq>0"
-    proof -
-      have "h' z\<noteq>0" using that unfolding h'_def by auto
-      obtain r1 where "r1>0" "cball z r1 \<subseteq> ball z r" "\<forall>x\<in>cball z r1. h' x\<noteq>0"
-      proof -
-        have "isCont h' z" "h' z\<noteq>0"
-          by (auto simp add: Lim_cong_within \<open>h \<midarrow>z\<rightarrow> z'\<close> \<open>z'\<noteq>0\<close> continuous_at h'_def)
-        then obtain r2 where r2:"r2>0" "\<forall>x\<in>ball z r2. h' x\<noteq>0"
-          using continuous_at_avoid[of z h' 0 ] unfolding ball_def by auto
-        define r1 where "r1=min r2 r / 2"
-        have "0 < r1" "cball z r1 \<subseteq> ball z r"
-          using \<open>r2>0\<close> \<open>r>0\<close> unfolding r1_def by auto
-        moreover have "\<forall>x\<in>cball z r1. h' x \<noteq> 0"
-          using r2 unfolding r1_def by simp
-        ultimately show ?thesis using that by auto
-      qed
-      then have "P h' 0 h' r1" using \<open>h' holomorphic_on ball z r\<close> unfolding P_def by auto
-      then have "P h 0 h' r1" unfolding P_def h'_def by auto
-      then show ?thesis unfolding P_def by blast
-    qed
-    ultimately show ?thesis by auto
-  qed
-
-  have ?thesis when "\<exists>x. (f \<longlongrightarrow> x) (at z)"
-    apply (rule_tac imp_unique[unfolded P_def])
-    using P_exist[OF that(1) f_iso non_zero] unfolding P_def .
-  moreover have ?thesis when "is_pole f z"
-  proof (rule imp_unique[unfolded P_def])
-    obtain e where [simp]:"e>0" and e_holo:"f holomorphic_on ball z e - {z}" and e_nz: "\<forall>x\<in>ball z e-{z}. f x\<noteq>0"
-    proof -
-      have "\<forall>\<^sub>F z in at z. f z \<noteq> 0"
-        using \<open>is_pole f z\<close> filterlim_at_infinity_imp_eventually_ne unfolding is_pole_def
-        by auto
-      then obtain e1 where e1:"e1>0" "\<forall>x\<in>ball z e1-{z}. f x\<noteq>0"
-        using that eventually_at[of "\<lambda>x. f x\<noteq>0" z UNIV,simplified] by (auto simp add:dist_commute)
-      obtain e2 where e2:"e2>0" "f holomorphic_on ball z e2 - {z}"
-        using f_iso analytic_imp_holomorphic unfolding isolated_singularity_at_def by auto
-      define e where "e=min e1 e2"
-      show ?thesis
-        apply (rule that[of e])
-        using  e1 e2 unfolding e_def by auto
-    qed
-
-    define h where "h \<equiv> \<lambda>x. inverse (f x)"
-
-    have "\<exists>n g r. P h n g r"
-    proof -
-      have "h \<midarrow>z\<rightarrow> 0"
-        using Lim_transform_within_open assms(2) h_def is_pole_tendsto that by fastforce
-      moreover have "\<exists>\<^sub>Fw in (at z). h w\<noteq>0"
-        using non_zero
-        apply (elim frequently_rev_mp)
-        unfolding h_def eventually_at by (auto intro:exI[where x=1])
-      moreover have "isolated_singularity_at h z"
-        unfolding isolated_singularity_at_def h_def
-        apply (rule exI[where x=e])
-        using e_holo e_nz \<open>e>0\<close> by (metis open_ball analytic_on_open
-            holomorphic_on_inverse open_delete)
-      ultimately show ?thesis
-        using P_exist[of h] by auto
-    qed
-    then obtain n g r
-      where "0 < r" and
-            g_holo:"g holomorphic_on cball z r" and "g z\<noteq>0" and
-            g_fac:"(\<forall>w\<in>cball z r-{z}. h w = g w * (w - z) powr of_int n  \<and> g w \<noteq> 0)"
-      unfolding P_def by auto
-    have "P f (-n) (inverse o g) r"
-    proof -
-      have "f w = inverse (g w) * (w - z) powr of_int (- n)" when "w\<in>cball z r - {z}" for w
-        using g_fac[rule_format,of w] that unfolding h_def
-        apply (auto simp add:powr_minus )
-        by (metis inverse_inverse_eq inverse_mult_distrib)
-      then show ?thesis
-        unfolding P_def comp_def
-        using \<open>r>0\<close> g_holo g_fac \<open>g z\<noteq>0\<close> by (auto intro:holomorphic_intros)
-    qed
-    then show "\<exists>x g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z \<noteq> 0
-                  \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int x  \<and> g w \<noteq> 0)"
-      unfolding P_def by blast
-  qed
-  ultimately show ?thesis using \<open>not_essential f z\<close> unfolding not_essential_def  by presburger
-qed
-
-lemma not_essential_transform:
-  assumes "not_essential g z"
-  assumes "\<forall>\<^sub>F w in (at z). g w = f w"
-  shows "not_essential f z"
-  using assms unfolding not_essential_def
-  by (simp add: filterlim_cong is_pole_cong)
-
-lemma isolated_singularity_at_transform:
-  assumes "isolated_singularity_at g z"
-  assumes "\<forall>\<^sub>F w in (at z). g w = f w"
-  shows "isolated_singularity_at f z"
-proof -
-  obtain r1 where "r1>0" and r1:"g analytic_on ball z r1 - {z}"
-    using assms(1) unfolding isolated_singularity_at_def by auto
-  obtain r2 where "r2>0" and r2:" \<forall>x. x \<noteq> z \<and> dist x z < r2 \<longrightarrow> g x = f x"
-    using assms(2) unfolding eventually_at by auto
-  define r3 where "r3=min r1 r2"
-  have "r3>0" unfolding r3_def using \<open>r1>0\<close> \<open>r2>0\<close> by auto
-  moreover have "f analytic_on ball z r3 - {z}"
-  proof -
-    have "g holomorphic_on ball z r3 - {z}"
-      using r1 unfolding r3_def by (subst (asm) analytic_on_open,auto)
-    then have "f holomorphic_on ball z r3 - {z}"
-      using r2 unfolding r3_def
-      by (auto simp add:dist_commute elim!:holomorphic_transform)
-    then show ?thesis by (subst analytic_on_open,auto)
-  qed
-  ultimately show ?thesis unfolding isolated_singularity_at_def by auto
-qed
-
-lemma not_essential_powr[singularity_intros]:
-  assumes "LIM w (at z). f w :> (at x)"
-  shows "not_essential (\<lambda>w. (f w) powr (of_int n)) z"
-proof -
-  define fp where "fp=(\<lambda>w. (f w) powr (of_int n))"
-  have ?thesis when "n>0"
-  proof -
-    have "(\<lambda>w.  (f w) ^ (nat n)) \<midarrow>z\<rightarrow> x ^ nat n"
-      using that assms unfolding filterlim_at by (auto intro!:tendsto_eq_intros)
-    then have "fp \<midarrow>z\<rightarrow> x ^ nat n" unfolding fp_def
-      apply (elim Lim_transform_within[where d=1],simp)
-      by (metis less_le powr_0 powr_of_int that zero_less_nat_eq zero_power)
-    then show ?thesis unfolding not_essential_def fp_def by auto
-  qed
-  moreover have ?thesis when "n=0"
-  proof -
-    have "fp \<midarrow>z\<rightarrow> 1 "
-      apply (subst tendsto_cong[where g="\<lambda>_.1"])
-      using that filterlim_at_within_not_equal[OF assms,of 0] unfolding fp_def by auto
-    then show ?thesis unfolding fp_def not_essential_def by auto
-  qed
-  moreover have ?thesis when "n<0"
-  proof (cases "x=0")
-    case True
-    have "LIM w (at z). inverse ((f w) ^ (nat (-n))) :> at_infinity"
-      apply (subst filterlim_inverse_at_iff[symmetric],simp)
-      apply (rule filterlim_atI)
-      subgoal using assms True that unfolding filterlim_at by (auto intro!:tendsto_eq_intros)
-      subgoal using filterlim_at_within_not_equal[OF assms,of 0]
-        by (eventually_elim,insert that,auto)
-      done
-    then have "LIM w (at z). fp w :> at_infinity"
-    proof (elim filterlim_mono_eventually)
-      show "\<forall>\<^sub>F x in at z. inverse (f x ^ nat (- n)) = fp x"
-        using filterlim_at_within_not_equal[OF assms,of 0] unfolding fp_def
-        apply eventually_elim
-        using powr_of_int that by auto
-    qed auto
-    then show ?thesis unfolding fp_def not_essential_def is_pole_def by auto
-  next
-    case False
-    let ?xx= "inverse (x ^ (nat (-n)))"
-    have "(\<lambda>w. inverse ((f w) ^ (nat (-n)))) \<midarrow>z\<rightarrow>?xx"
-      using assms False unfolding filterlim_at by (auto intro!:tendsto_eq_intros)
-    then have "fp \<midarrow>z\<rightarrow>?xx"
-      apply (elim Lim_transform_within[where d=1],simp)
-      unfolding fp_def by (metis inverse_zero nat_mono_iff nat_zero_as_int neg_0_less_iff_less
-          not_le power_eq_0_iff powr_0 powr_of_int that)
-    then show ?thesis unfolding fp_def not_essential_def by auto
-  qed
-  ultimately show ?thesis by linarith
-qed
-
-lemma isolated_singularity_at_powr[singularity_intros]:
-  assumes "isolated_singularity_at f z" "\<forall>\<^sub>F w in (at z). f w\<noteq>0"
-  shows "isolated_singularity_at (\<lambda>w. (f w) powr (of_int n)) z"
-proof -
-  obtain r1 where "r1>0" "f analytic_on ball z r1 - {z}"
-    using assms(1) unfolding isolated_singularity_at_def by auto
-  then have r1:"f holomorphic_on ball z r1 - {z}"
-    using analytic_on_open[of "ball z r1-{z}" f] by blast
-  obtain r2 where "r2>0" and r2:"\<forall>w. w \<noteq> z \<and> dist w z < r2 \<longrightarrow> f w \<noteq> 0"
-    using assms(2) unfolding eventually_at by auto
-  define r3 where "r3=min r1 r2"
-  have "(\<lambda>w. (f w) powr of_int n) holomorphic_on ball z r3 - {z}"
-    apply (rule holomorphic_on_powr_of_int)
-    subgoal unfolding r3_def using r1 by auto
-    subgoal unfolding r3_def using r2 by (auto simp add:dist_commute)
-    done
-  moreover have "r3>0" unfolding r3_def using \<open>0 < r1\<close> \<open>0 < r2\<close> by linarith
-  ultimately show ?thesis unfolding isolated_singularity_at_def
-    apply (subst (asm) analytic_on_open[symmetric])
-    by auto
-qed
-
-lemma non_zero_neighbour:
-  assumes f_iso:"isolated_singularity_at f z"
-      and f_ness:"not_essential f z"
-      and f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
-    shows "\<forall>\<^sub>F w in (at z). f w\<noteq>0"
-proof -
-  obtain fn fp fr where [simp]:"fp z \<noteq> 0" and "fr > 0"
-          and fr: "fp holomorphic_on cball z fr"
-                  "\<forall>w\<in>cball z fr - {z}. f w = fp w * (w - z) powr of_int fn \<and> fp w \<noteq> 0"
-    using holomorphic_factor_puncture[OF f_iso f_ness f_nconst,THEN ex1_implies_ex] by auto
-  have "f w \<noteq> 0" when " w \<noteq> z" "dist w z < fr" for w
-  proof -
-    have "f w = fp w * (w - z) powr of_int fn" "fp w \<noteq> 0"
-      using fr(2)[rule_format, of w] using that by (auto simp add:dist_commute)
-    moreover have "(w - z) powr of_int fn \<noteq>0"
-      unfolding powr_eq_0_iff using \<open>w\<noteq>z\<close> by auto
-    ultimately show ?thesis by auto
-  qed
-  then show ?thesis using \<open>fr>0\<close> unfolding eventually_at by auto
-qed
-
-lemma non_zero_neighbour_pole:
-  assumes "is_pole f z"
-  shows "\<forall>\<^sub>F w in (at z). f w\<noteq>0"
-  using assms filterlim_at_infinity_imp_eventually_ne[of f "at z" 0]
-  unfolding is_pole_def by auto
-
-lemma non_zero_neighbour_alt:
-  assumes holo: "f holomorphic_on S"
-      and "open S" "connected S" "z \<in> S"  "\<beta> \<in> S" "f \<beta> \<noteq> 0"
-    shows "\<forall>\<^sub>F w in (at z). f w\<noteq>0 \<and> w\<in>S"
-proof (cases "f z = 0")
-  case True
-  from isolated_zeros[OF holo \<open>open S\<close> \<open>connected S\<close> \<open>z \<in> S\<close> True \<open>\<beta> \<in> S\<close> \<open>f \<beta> \<noteq> 0\<close>]
-  obtain r where "0 < r" "ball z r \<subseteq> S" "\<forall>w \<in> ball z r - {z}.f w \<noteq> 0" by metis
-  then show ?thesis unfolding eventually_at
-    apply (rule_tac x=r in exI)
-    by (auto simp add:dist_commute)
-next
-  case False
-  obtain r1 where r1:"r1>0" "\<forall>y. dist z y < r1 \<longrightarrow> f y \<noteq> 0"
-    using continuous_at_avoid[of z f, OF _ False] assms(2,4) continuous_on_eq_continuous_at
-      holo holomorphic_on_imp_continuous_on by blast
-  obtain r2 where r2:"r2>0" "ball z r2 \<subseteq> S"
-    using assms(2) assms(4) openE by blast
-  show ?thesis unfolding eventually_at
-    apply (rule_tac x="min r1 r2" in exI)
-    using r1 r2 by (auto simp add:dist_commute)
-qed
-
-lemma not_essential_times[singularity_intros]:
-  assumes f_ness:"not_essential f z" and g_ness:"not_essential g z"
-  assumes f_iso:"isolated_singularity_at f z" and g_iso:"isolated_singularity_at g z"
-  shows "not_essential (\<lambda>w. f w * g w) z"
-proof -
-  define fg where "fg = (\<lambda>w. f w * g w)"
-  have ?thesis when "\<not> ((\<exists>\<^sub>Fw in (at z). f w\<noteq>0) \<and> (\<exists>\<^sub>Fw in (at z). g w\<noteq>0))"
-  proof -
-    have "\<forall>\<^sub>Fw in (at z). fg w=0"
-      using that[unfolded frequently_def, simplified] unfolding fg_def
-      by (auto elim: eventually_rev_mp)
-    from tendsto_cong[OF this] have "fg \<midarrow>z\<rightarrow>0" by auto
-    then show ?thesis unfolding not_essential_def fg_def by auto
-  qed
-  moreover have ?thesis when f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0" and g_nconst:"\<exists>\<^sub>Fw in (at z). g w\<noteq>0"
-  proof -
-    obtain fn fp fr where [simp]:"fp z \<noteq> 0" and "fr > 0"
-          and fr: "fp holomorphic_on cball z fr"
-                  "\<forall>w\<in>cball z fr - {z}. f w = fp w * (w - z) powr of_int fn \<and> fp w \<noteq> 0"
-      using holomorphic_factor_puncture[OF f_iso f_ness f_nconst,THEN ex1_implies_ex] by auto
-    obtain gn gp gr where [simp]:"gp z \<noteq> 0" and "gr > 0"
-          and gr: "gp holomorphic_on cball z gr"
-                  "\<forall>w\<in>cball z gr - {z}. g w = gp w * (w - z) powr of_int gn \<and> gp w \<noteq> 0"
-      using holomorphic_factor_puncture[OF g_iso g_ness g_nconst,THEN ex1_implies_ex] by auto
-
-    define r1 where "r1=(min fr gr)"
-    have "r1>0" unfolding r1_def using  \<open>fr>0\<close> \<open>gr>0\<close> by auto
-    have fg_times:"fg w = (fp w * gp w) * (w - z) powr (of_int (fn+gn))" and fgp_nz:"fp w*gp w\<noteq>0"
-      when "w\<in>ball z r1 - {z}" for w
-    proof -
-      have "f w = fp w * (w - z) powr of_int fn" "fp w\<noteq>0"
-        using fr(2)[rule_format,of w] that unfolding r1_def by auto
-      moreover have "g w = gp w * (w - z) powr of_int gn" "gp w \<noteq> 0"
-        using gr(2)[rule_format, of w] that unfolding r1_def by auto
-      ultimately show "fg w = (fp w * gp w) * (w - z) powr (of_int (fn+gn))" "fp w*gp w\<noteq>0"
-        unfolding fg_def by (auto simp add:powr_add)
-    qed
-
-    have [intro]: "fp \<midarrow>z\<rightarrow>fp z" "gp \<midarrow>z\<rightarrow>gp z"
-        using fr(1) \<open>fr>0\<close> gr(1) \<open>gr>0\<close>
-        by (meson open_ball ball_subset_cball centre_in_ball
-            continuous_on_eq_continuous_at continuous_within holomorphic_on_imp_continuous_on
-            holomorphic_on_subset)+
-    have ?thesis when "fn+gn>0"
-    proof -
-      have "(\<lambda>w. (fp w * gp w) * (w - z) ^ (nat (fn+gn))) \<midarrow>z\<rightarrow>0"
-        using that by (auto intro!:tendsto_eq_intros)
-      then have "fg \<midarrow>z\<rightarrow> 0"
-        apply (elim Lim_transform_within[OF _ \<open>r1>0\<close>])
-        by (metis (no_types, hide_lams) Diff_iff cball_trivial dist_commute dist_self
-              eq_iff_diff_eq_0 fg_times less_le linorder_not_le mem_ball mem_cball powr_of_int
-              that)
-      then show ?thesis unfolding not_essential_def fg_def by auto
-    qed
-    moreover have ?thesis when "fn+gn=0"
-    proof -
-      have "(\<lambda>w. fp w * gp w) \<midarrow>z\<rightarrow>fp z*gp z"
-        using that by (auto intro!:tendsto_eq_intros)
-      then have "fg \<midarrow>z\<rightarrow> fp z*gp z"
-        apply (elim Lim_transform_within[OF _ \<open>r1>0\<close>])
-        apply (subst fg_times)
-        by (auto simp add:dist_commute that)
-      then show ?thesis unfolding not_essential_def fg_def by auto
-    qed
-    moreover have ?thesis when "fn+gn<0"
-    proof -
-      have "LIM w (at z). fp w * gp w / (w-z)^nat (-(fn+gn)) :> at_infinity"
-        apply (rule filterlim_divide_at_infinity)
-        apply (insert that, auto intro!:tendsto_eq_intros filterlim_atI)
-        using eventually_at_topological by blast
-      then have "is_pole fg z" unfolding is_pole_def
-        apply (elim filterlim_transform_within[OF _ _ \<open>r1>0\<close>],simp)
-        apply (subst fg_times,simp add:dist_commute)
-        apply (subst powr_of_int)
-        using that by (auto simp add:field_split_simps)
-      then show ?thesis unfolding not_essential_def fg_def by auto
-    qed
-    ultimately show ?thesis unfolding not_essential_def fg_def by fastforce
-  qed
-  ultimately show ?thesis by auto
-qed
-
-lemma not_essential_inverse[singularity_intros]:
-  assumes f_ness:"not_essential f z"
-  assumes f_iso:"isolated_singularity_at f z"
-  shows "not_essential (\<lambda>w. inverse (f w)) z"
-proof -
-  define vf where "vf = (\<lambda>w. inverse (f w))"
-  have ?thesis when "\<not>(\<exists>\<^sub>Fw in (at z). f w\<noteq>0)"
-  proof -
-    have "\<forall>\<^sub>Fw in (at z). f w=0"
-      using that[unfolded frequently_def, simplified] by (auto elim: eventually_rev_mp)
-    then have "\<forall>\<^sub>Fw in (at z). vf w=0"
-      unfolding vf_def by auto
-    from tendsto_cong[OF this] have "vf \<midarrow>z\<rightarrow>0" unfolding vf_def by auto
-    then show ?thesis unfolding not_essential_def vf_def by auto
-  qed
-  moreover have ?thesis when "is_pole f z"
-  proof -
-    have "vf \<midarrow>z\<rightarrow>0"
-      using that filterlim_at filterlim_inverse_at_iff unfolding is_pole_def vf_def by blast
-    then show ?thesis unfolding not_essential_def vf_def by auto
-  qed
-  moreover have ?thesis when "\<exists>x. f\<midarrow>z\<rightarrow>x " and f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
-  proof -
-    from that obtain fz where fz:"f\<midarrow>z\<rightarrow>fz" by auto
-    have ?thesis when "fz=0"
-    proof -
-      have "(\<lambda>w. inverse (vf w)) \<midarrow>z\<rightarrow>0"
-        using fz that unfolding vf_def by auto
-      moreover have "\<forall>\<^sub>F w in at z. inverse (vf w) \<noteq> 0"
-        using non_zero_neighbour[OF f_iso f_ness f_nconst]
-        unfolding vf_def by auto
-      ultimately have "is_pole vf z"
-        using filterlim_inverse_at_iff[of vf "at z"] unfolding filterlim_at is_pole_def by auto
-      then show ?thesis unfolding not_essential_def vf_def by auto
-    qed
-    moreover have ?thesis when "fz\<noteq>0"
-    proof -
-      have "vf \<midarrow>z\<rightarrow>inverse fz"
-        using fz that unfolding vf_def by (auto intro:tendsto_eq_intros)
-      then show ?thesis unfolding not_essential_def vf_def by auto
-    qed
-    ultimately show ?thesis by auto
-  qed
-  ultimately show ?thesis using f_ness unfolding not_essential_def by auto
-qed
-
-lemma isolated_singularity_at_inverse[singularity_intros]:
-  assumes f_iso:"isolated_singularity_at f z"
-      and f_ness:"not_essential f z"
-  shows "isolated_singularity_at (\<lambda>w. inverse (f w)) z"
-proof -
-  define vf where "vf = (\<lambda>w. inverse (f w))"
-  have ?thesis when "\<not>(\<exists>\<^sub>Fw in (at z). f w\<noteq>0)"
-  proof -
-    have "\<forall>\<^sub>Fw in (at z). f w=0"
-      using that[unfolded frequently_def, simplified] by (auto elim: eventually_rev_mp)
-    then have "\<forall>\<^sub>Fw in (at z). vf w=0"
-      unfolding vf_def by auto
-    then obtain d1 where "d1>0" and d1:"\<forall>x. x \<noteq> z \<and> dist x z < d1 \<longrightarrow> vf x = 0"
-      unfolding eventually_at by auto
-    then have "vf holomorphic_on ball z d1-{z}"
-      apply (rule_tac holomorphic_transform[of "\<lambda>_. 0"])
-      by (auto simp add:dist_commute)
-    then have "vf analytic_on ball z d1 - {z}"
-      by (simp add: analytic_on_open open_delete)
-    then show ?thesis using \<open>d1>0\<close> unfolding isolated_singularity_at_def vf_def by auto
-  qed
-  moreover have ?thesis when f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
-  proof -
-    have "\<forall>\<^sub>F w in at z. f w \<noteq> 0" using non_zero_neighbour[OF f_iso f_ness f_nconst] .
-    then obtain d1 where d1:"d1>0" "\<forall>x. x \<noteq> z \<and> dist x z < d1 \<longrightarrow> f x \<noteq> 0"
-      unfolding eventually_at by auto
-    obtain d2 where "d2>0" and d2:"f analytic_on ball z d2 - {z}"
-      using f_iso unfolding isolated_singularity_at_def by auto
-    define d3 where "d3=min d1 d2"
-    have "d3>0" unfolding d3_def using \<open>d1>0\<close> \<open>d2>0\<close> by auto
-    moreover have "vf analytic_on ball z d3 - {z}"
-      unfolding vf_def
-      apply (rule analytic_on_inverse)
-      subgoal using d2 unfolding d3_def by (elim analytic_on_subset) auto
-      subgoal for w using d1 unfolding d3_def by (auto simp add:dist_commute)
-      done
-    ultimately show ?thesis unfolding isolated_singularity_at_def vf_def by auto
-  qed
-  ultimately show ?thesis by auto
-qed
-
-lemma not_essential_divide[singularity_intros]:
-  assumes f_ness:"not_essential f z" and g_ness:"not_essential g z"
-  assumes f_iso:"isolated_singularity_at f z" and g_iso:"isolated_singularity_at g z"
-  shows "not_essential (\<lambda>w. f w / g w) z"
-proof -
-  have "not_essential (\<lambda>w. f w * inverse (g w)) z"
-    apply (rule not_essential_times[where g="\<lambda>w. inverse (g w)"])
-    using assms by (auto intro: isolated_singularity_at_inverse not_essential_inverse)
-  then show ?thesis by (simp add:field_simps)
-qed
-
-lemma
-  assumes f_iso:"isolated_singularity_at f z"
-      and g_iso:"isolated_singularity_at g z"
-    shows isolated_singularity_at_times[singularity_intros]:
-              "isolated_singularity_at (\<lambda>w. f w * g w) z" and
-          isolated_singularity_at_add[singularity_intros]:
-              "isolated_singularity_at (\<lambda>w. f w + g w) z"
-proof -
-  obtain d1 d2 where "d1>0" "d2>0"
-      and d1:"f analytic_on ball z d1 - {z}" and d2:"g analytic_on ball z d2 - {z}"
-    using f_iso g_iso unfolding isolated_singularity_at_def by auto
-  define d3 where "d3=min d1 d2"
-  have "d3>0" unfolding d3_def using \<open>d1>0\<close> \<open>d2>0\<close> by auto
-
-  have "(\<lambda>w. f w * g w) analytic_on ball z d3 - {z}"
-    apply (rule analytic_on_mult)
-    using d1 d2 unfolding d3_def by (auto elim:analytic_on_subset)
-  then show "isolated_singularity_at (\<lambda>w. f w * g w) z"
-    using \<open>d3>0\<close> unfolding isolated_singularity_at_def by auto
-  have "(\<lambda>w. f w + g w) analytic_on ball z d3 - {z}"
-    apply (rule analytic_on_add)
-    using d1 d2 unfolding d3_def by (auto elim:analytic_on_subset)
-  then show "isolated_singularity_at (\<lambda>w. f w + g w) z"
-    using \<open>d3>0\<close> unfolding isolated_singularity_at_def by auto
-qed
-
-lemma isolated_singularity_at_uminus[singularity_intros]:
-  assumes f_iso:"isolated_singularity_at f z"
-  shows "isolated_singularity_at (\<lambda>w. - f w) z"
-  using assms unfolding isolated_singularity_at_def using analytic_on_neg by blast
-
-lemma isolated_singularity_at_id[singularity_intros]:
-     "isolated_singularity_at (\<lambda>w. w) z"
-  unfolding isolated_singularity_at_def by (simp add: gt_ex)
-
-lemma isolated_singularity_at_minus[singularity_intros]:
-  assumes f_iso:"isolated_singularity_at f z"
-      and g_iso:"isolated_singularity_at g z"
-    shows "isolated_singularity_at (\<lambda>w. f w - g w) z"
-  using isolated_singularity_at_uminus[THEN isolated_singularity_at_add[OF f_iso,of "\<lambda>w. - g w"]
-        ,OF g_iso] by simp
-
-lemma isolated_singularity_at_divide[singularity_intros]:
-  assumes f_iso:"isolated_singularity_at f z"
-      and g_iso:"isolated_singularity_at g z"
-      and g_ness:"not_essential g z"
-    shows "isolated_singularity_at (\<lambda>w. f w / g w) z"
-  using isolated_singularity_at_inverse[THEN isolated_singularity_at_times[OF f_iso,
-          of "\<lambda>w. inverse (g w)"],OF g_iso g_ness] by (simp add:field_simps)
-
-lemma isolated_singularity_at_const[singularity_intros]:
-    "isolated_singularity_at (\<lambda>w. c) z"
-  unfolding isolated_singularity_at_def by (simp add: gt_ex)
-
-lemma isolated_singularity_at_holomorphic:
-  assumes "f holomorphic_on s-{z}" "open s" "z\<in>s"
-  shows "isolated_singularity_at f z"
-  using assms unfolding isolated_singularity_at_def
-  by (metis analytic_on_holomorphic centre_in_ball insert_Diff openE open_delete subset_insert_iff)
-
-subsubsection \<open>The order of non-essential singularities (i.e. removable singularities or poles)\<close>
-
-
-definition\<^marker>\<open>tag important\<close> zorder :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> int" where
-  "zorder f z = (THE n. (\<exists>h r. r>0 \<and> h holomorphic_on cball z r \<and> h z\<noteq>0
-                   \<and> (\<forall>w\<in>cball z r - {z}. f w =  h w * (w-z) powr (of_int n)
-                   \<and> h w \<noteq>0)))"
-
-definition\<^marker>\<open>tag important\<close> zor_poly
-    ::"[complex \<Rightarrow> complex, complex] \<Rightarrow> complex \<Rightarrow> complex" where
-  "zor_poly f z = (SOME h. \<exists>r. r > 0 \<and> h holomorphic_on cball z r \<and> h z \<noteq> 0
-                   \<and> (\<forall>w\<in>cball z r - {z}. f w =  h w * (w - z) powr (zorder f z)
-                   \<and> h w \<noteq>0))"
-
-lemma zorder_exist:
-  fixes f::"complex \<Rightarrow> complex" and z::complex
-  defines "n\<equiv>zorder f z" and "g\<equiv>zor_poly f z"
-  assumes f_iso:"isolated_singularity_at f z"
-      and f_ness:"not_essential f z"
-      and f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
-  shows "g z\<noteq>0 \<and> (\<exists>r. r>0 \<and> g holomorphic_on cball z r
-    \<and> (\<forall>w\<in>cball z r - {z}. f w  = g w * (w-z) powr n  \<and> g w \<noteq>0))"
-proof -
-  define P where "P = (\<lambda>n g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z\<noteq>0
-          \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w-z) powr (of_int n) \<and> g w\<noteq>0))"
-  have "\<exists>!n. \<exists>g r. P n g r"
-    using holomorphic_factor_puncture[OF assms(3-)] unfolding P_def by auto
-  then have "\<exists>g r. P n g r"
-    unfolding n_def P_def zorder_def
-    by (drule_tac theI',argo)
-  then have "\<exists>r. P n g r"
-    unfolding P_def zor_poly_def g_def n_def
-    by (drule_tac someI_ex,argo)
-  then obtain r1 where "P n g r1" by auto
-  then show ?thesis unfolding P_def by auto
-qed
-
-lemma
-  fixes f::"complex \<Rightarrow> complex" and z::complex
-  assumes f_iso:"isolated_singularity_at f z"
-      and f_ness:"not_essential f z"
-      and f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
-    shows zorder_inverse: "zorder (\<lambda>w. inverse (f w)) z = - zorder f z"
-      and zor_poly_inverse: "\<forall>\<^sub>Fw in (at z). zor_poly (\<lambda>w. inverse (f w)) z w
-                                                = inverse (zor_poly f z w)"
-proof -
-  define vf where "vf = (\<lambda>w. inverse (f w))"
-  define fn vfn where
-    "fn = zorder f z"  and "vfn = zorder vf z"
-  define fp vfp where
-    "fp = zor_poly f z" and "vfp = zor_poly vf z"
-
-  obtain fr where [simp]:"fp z \<noteq> 0" and "fr > 0"
-          and fr: "fp holomorphic_on cball z fr"
-                  "\<forall>w\<in>cball z fr - {z}. f w = fp w * (w - z) powr of_int fn \<and> fp w \<noteq> 0"
-    using zorder_exist[OF f_iso f_ness f_nconst,folded fn_def fp_def]
-    by auto
-  have fr_inverse: "vf w = (inverse (fp w)) * (w - z) powr (of_int (-fn))"
-        and fr_nz: "inverse (fp w)\<noteq>0"
-    when "w\<in>ball z fr - {z}" for w
-  proof -
-    have "f w = fp w * (w - z) powr of_int fn" "fp w\<noteq>0"
-      using fr(2)[rule_format,of w] that by auto
-    then show "vf w = (inverse (fp w)) * (w - z) powr (of_int (-fn))" "inverse (fp w)\<noteq>0"
-      unfolding vf_def by (auto simp add:powr_minus)
-  qed
-  obtain vfr where [simp]:"vfp z \<noteq> 0" and "vfr>0" and vfr:"vfp holomorphic_on cball z vfr"
-      "(\<forall>w\<in>cball z vfr - {z}. vf w = vfp w * (w - z) powr of_int vfn \<and> vfp w \<noteq> 0)"
-  proof -
-    have "isolated_singularity_at vf z"
-      using isolated_singularity_at_inverse[OF f_iso f_ness] unfolding vf_def .
-    moreover have "not_essential vf z"
-      using not_essential_inverse[OF f_ness f_iso] unfolding vf_def .
-    moreover have "\<exists>\<^sub>F w in at z. vf w \<noteq> 0"
-      using f_nconst unfolding vf_def by (auto elim:frequently_elim1)
-    ultimately show ?thesis using zorder_exist[of vf z, folded vfn_def vfp_def] that by auto
-  qed
-
-
-  define r1 where "r1 = min fr vfr"
-  have "r1>0" using \<open>fr>0\<close> \<open>vfr>0\<close> unfolding r1_def by simp
-  show "vfn = - fn"
-    apply (rule holomorphic_factor_unique[of r1 vfp z "\<lambda>w. inverse (fp w)" vf])
-    subgoal using \<open>r1>0\<close> by simp
-    subgoal by simp
-    subgoal by simp
-    subgoal
-    proof (rule ballI)
-      fix w assume "w \<in> ball z r1 - {z}"
-      then have "w \<in> ball z fr - {z}" "w \<in> cball z vfr - {z}"  unfolding r1_def by auto
-      from fr_inverse[OF this(1)] fr_nz[OF this(1)] vfr(2)[rule_format,OF this(2)]
-      show "vf w = vfp w * (w - z) powr of_int vfn \<and> vfp w \<noteq> 0
-              \<and> vf w = inverse (fp w) * (w - z) powr of_int (- fn) \<and> inverse (fp w) \<noteq> 0" by auto
-    qed
-    subgoal using vfr(1) unfolding r1_def by (auto intro!:holomorphic_intros)
-    subgoal using fr unfolding r1_def by (auto intro!:holomorphic_intros)
-    done
-
-  have "vfp w = inverse (fp w)" when "w\<in>ball z r1-{z}" for w
-  proof -
-    have "w \<in> ball z fr - {z}" "w \<in> cball z vfr - {z}"  "w\<noteq>z" using that unfolding r1_def by auto
-    from fr_inverse[OF this(1)] fr_nz[OF this(1)] vfr(2)[rule_format,OF this(2)] \<open>vfn = - fn\<close> \<open>w\<noteq>z\<close>
-    show ?thesis by auto
-  qed
-  then show "\<forall>\<^sub>Fw in (at z). vfp w = inverse (fp w)"
-    unfolding eventually_at using \<open>r1>0\<close>
-    apply (rule_tac x=r1 in exI)
-    by (auto simp add:dist_commute)
-qed
-
-lemma
-  fixes f g::"complex \<Rightarrow> complex" and z::complex
-  assumes f_iso:"isolated_singularity_at f z" and g_iso:"isolated_singularity_at g z"
-      and f_ness:"not_essential f z" and g_ness:"not_essential g z"
-      and fg_nconst: "\<exists>\<^sub>Fw in (at z). f w * g w\<noteq> 0"
-  shows zorder_times:"zorder (\<lambda>w. f w * g w) z = zorder f z + zorder g z" and
-        zor_poly_times:"\<forall>\<^sub>Fw in (at z). zor_poly (\<lambda>w. f w * g w) z w
-                                                  = zor_poly f z w *zor_poly g z w"
-proof -
-  define fg where "fg = (\<lambda>w. f w * g w)"
-  define fn gn fgn where
-    "fn = zorder f z" and "gn = zorder g z" and "fgn = zorder fg z"
-  define fp gp fgp where
-    "fp = zor_poly f z" and "gp = zor_poly g z" and "fgp = zor_poly fg z"
-  have f_nconst:"\<exists>\<^sub>Fw in (at z). f w \<noteq> 0" and g_nconst:"\<exists>\<^sub>Fw in (at z).g w\<noteq> 0"
-    using fg_nconst by (auto elim!:frequently_elim1)
-  obtain fr where [simp]:"fp z \<noteq> 0" and "fr > 0"
-          and fr: "fp holomorphic_on cball z fr"
-                  "\<forall>w\<in>cball z fr - {z}. f w = fp w * (w - z) powr of_int fn \<and> fp w \<noteq> 0"
-    using zorder_exist[OF f_iso f_ness f_nconst,folded fp_def fn_def] by auto
-  obtain gr where [simp]:"gp z \<noteq> 0" and "gr > 0"
-          and gr: "gp holomorphic_on cball z gr"
-                  "\<forall>w\<in>cball z gr - {z}. g w = gp w * (w - z) powr of_int gn \<and> gp w \<noteq> 0"
-    using zorder_exist[OF g_iso g_ness g_nconst,folded gn_def gp_def] by auto
-  define r1 where "r1=min fr gr"
-  have "r1>0" unfolding r1_def using \<open>fr>0\<close> \<open>gr>0\<close> by auto
-  have fg_times:"fg w = (fp w * gp w) * (w - z) powr (of_int (fn+gn))" and fgp_nz:"fp w*gp w\<noteq>0"
-    when "w\<in>ball z r1 - {z}" for w
-  proof -
-    have "f w = fp w * (w - z) powr of_int fn" "fp w\<noteq>0"
-      using fr(2)[rule_format,of w] that unfolding r1_def by auto
-    moreover have "g w = gp w * (w - z) powr of_int gn" "gp w \<noteq> 0"
-      using gr(2)[rule_format, of w] that unfolding r1_def by auto
-    ultimately show "fg w = (fp w * gp w) * (w - z) powr (of_int (fn+gn))" "fp w*gp w\<noteq>0"
-      unfolding fg_def by (auto simp add:powr_add)
-  qed
-
-  obtain fgr where [simp]:"fgp z \<noteq> 0" and "fgr > 0"
-          and fgr: "fgp holomorphic_on cball z fgr"
-                  "\<forall>w\<in>cball z fgr - {z}. fg w = fgp w * (w - z) powr of_int fgn \<and> fgp w \<noteq> 0"
-  proof -
-    have "fgp z \<noteq> 0 \<and> (\<exists>r>0. fgp holomorphic_on cball z r
-            \<and> (\<forall>w\<in>cball z r - {z}. fg w = fgp w * (w - z) powr of_int fgn \<and> fgp w \<noteq> 0))"
-      apply (rule zorder_exist[of fg z, folded fgn_def fgp_def])
-      subgoal unfolding fg_def using isolated_singularity_at_times[OF f_iso g_iso] .
-      subgoal unfolding fg_def using not_essential_times[OF f_ness g_ness f_iso g_iso] .
-      subgoal unfolding fg_def using fg_nconst .
-      done
-    then show ?thesis using that by blast
-  qed
-  define r2 where "r2 = min fgr r1"
-  have "r2>0" using \<open>r1>0\<close> \<open>fgr>0\<close> unfolding r2_def by simp
-  show "fgn = fn + gn "
-    apply (rule holomorphic_factor_unique[of r2 fgp z "\<lambda>w. fp w * gp w" fg])
-    subgoal using \<open>r2>0\<close> by simp
-    subgoal by simp
-    subgoal by simp
-    subgoal
-    proof (rule ballI)
-      fix w assume "w \<in> ball z r2 - {z}"
-      then have "w \<in> ball z r1 - {z}" "w \<in> cball z fgr - {z}"  unfolding r2_def by auto
-      from fg_times[OF this(1)] fgp_nz[OF this(1)] fgr(2)[rule_format,OF this(2)]
-      show "fg w = fgp w * (w - z) powr of_int fgn \<and> fgp w \<noteq> 0
-              \<and> fg w = fp w * gp w * (w - z) powr of_int (fn + gn) \<and> fp w * gp w \<noteq> 0" by auto
-    qed
-    subgoal using fgr(1) unfolding r2_def r1_def by (auto intro!:holomorphic_intros)
-    subgoal using fr(1) gr(1) unfolding r2_def r1_def by (auto intro!:holomorphic_intros)
-    done
-
-  have "fgp w = fp w *gp w" when "w\<in>ball z r2-{z}" for w
-  proof -
-    have "w \<in> ball z r1 - {z}" "w \<in> cball z fgr - {z}" "w\<noteq>z" using that  unfolding r2_def by auto
-    from fg_times[OF this(1)] fgp_nz[OF this(1)] fgr(2)[rule_format,OF this(2)] \<open>fgn = fn + gn\<close> \<open>w\<noteq>z\<close>
-    show ?thesis by auto
-  qed
-  then show "\<forall>\<^sub>Fw in (at z). fgp w = fp w * gp w"
-    using \<open>r2>0\<close> unfolding eventually_at by (auto simp add:dist_commute)
-qed
-
-lemma
-  fixes f g::"complex \<Rightarrow> complex" and z::complex
-  assumes f_iso:"isolated_singularity_at f z" and g_iso:"isolated_singularity_at g z"
-      and f_ness:"not_essential f z" and g_ness:"not_essential g z"
-      and fg_nconst: "\<exists>\<^sub>Fw in (at z). f w * g w\<noteq> 0"
-  shows zorder_divide:"zorder (\<lambda>w. f w / g w) z = zorder f z - zorder g z" and
-        zor_poly_divide:"\<forall>\<^sub>Fw in (at z). zor_poly (\<lambda>w. f w / g w) z w
-                                                  = zor_poly f z w  / zor_poly g z w"
-proof -
-  have f_nconst:"\<exists>\<^sub>Fw in (at z). f w \<noteq> 0" and g_nconst:"\<exists>\<^sub>Fw in (at z).g w\<noteq> 0"
-    using fg_nconst by (auto elim!:frequently_elim1)
-  define vg where "vg=(\<lambda>w. inverse (g w))"
-  have "zorder (\<lambda>w. f w * vg w) z = zorder f z + zorder vg z"
-    apply (rule zorder_times[OF f_iso _ f_ness,of vg])
-    subgoal unfolding vg_def using isolated_singularity_at_inverse[OF g_iso g_ness] .
-    subgoal unfolding vg_def using not_essential_inverse[OF g_ness g_iso] .
-    subgoal unfolding vg_def using fg_nconst by (auto elim!:frequently_elim1)
-    done
-  then show "zorder (\<lambda>w. f w / g w) z = zorder f z - zorder g z"
-    using zorder_inverse[OF g_iso g_ness g_nconst,folded vg_def] unfolding vg_def
-    by (auto simp add:field_simps)
-
-  have "\<forall>\<^sub>F w in at z. zor_poly (\<lambda>w. f w * vg w) z w = zor_poly f z w * zor_poly vg z w"
-    apply (rule zor_poly_times[OF f_iso _ f_ness,of vg])
-    subgoal unfolding vg_def using isolated_singularity_at_inverse[OF g_iso g_ness] .
-    subgoal unfolding vg_def using not_essential_inverse[OF g_ness g_iso] .
-    subgoal unfolding vg_def using fg_nconst by (auto elim!:frequently_elim1)
-    done
-  then show "\<forall>\<^sub>Fw in (at z). zor_poly (\<lambda>w. f w / g w) z w = zor_poly f z w  / zor_poly g z w"
-    using zor_poly_inverse[OF g_iso g_ness g_nconst,folded vg_def] unfolding vg_def
-    apply eventually_elim
-    by (auto simp add:field_simps)
-qed
-
-lemma zorder_exist_zero:
-  fixes f::"complex \<Rightarrow> complex" and z::complex
-  defines "n\<equiv>zorder f z" and "g\<equiv>zor_poly f z"
-  assumes  holo: "f holomorphic_on s" and
-          "open s" "connected s" "z\<in>s"
-      and non_const: "\<exists>w\<in>s. f w \<noteq> 0"
-  shows "(if f z=0 then n > 0 else n=0) \<and> (\<exists>r. r>0 \<and> cball z r \<subseteq> s \<and> g holomorphic_on cball z r
-    \<and> (\<forall>w\<in>cball z r. f w  = g w * (w-z) ^ nat n  \<and> g w \<noteq>0))"
-proof -
-  obtain r where "g z \<noteq> 0" and r: "r>0" "cball z r \<subseteq> s" "g holomorphic_on cball z r"
-            "(\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
-  proof -
-    have "g z \<noteq> 0 \<and> (\<exists>r>0. g holomorphic_on cball z r
-            \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0))"
-    proof (rule zorder_exist[of f z,folded g_def n_def])
-      show "isolated_singularity_at f z" unfolding isolated_singularity_at_def
-        using holo assms(4,6)
-        by (meson Diff_subset open_ball analytic_on_holomorphic holomorphic_on_subset openE)
-      show "not_essential f z" unfolding not_essential_def
-        using assms(4,6) at_within_open continuous_on holo holomorphic_on_imp_continuous_on
-        by fastforce
-      have "\<forall>\<^sub>F w in at z. f w \<noteq> 0 \<and> w\<in>s"
-      proof -
-        obtain w where "w\<in>s" "f w\<noteq>0" using non_const by auto
-        then show ?thesis
-          by (rule non_zero_neighbour_alt[OF holo \<open>open s\<close> \<open>connected s\<close> \<open>z\<in>s\<close>])
-      qed
-      then show "\<exists>\<^sub>F w in at z. f w \<noteq> 0"
-        apply (elim eventually_frequentlyE)
-        by auto
-    qed
-    then obtain r1 where "g z \<noteq> 0" "r1>0" and r1:"g holomorphic_on cball z r1"
-            "(\<forall>w\<in>cball z r1 - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
-      by auto
-    obtain r2 where r2: "r2>0" "cball z r2 \<subseteq> s"
-      using assms(4,6) open_contains_cball_eq by blast
-    define r3 where "r3=min r1 r2"
-    have "r3>0" "cball z r3 \<subseteq> s" using \<open>r1>0\<close> r2 unfolding r3_def by auto
-    moreover have "g holomorphic_on cball z r3"
-      using r1(1) unfolding r3_def by auto
-    moreover have "(\<forall>w\<in>cball z r3 - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
-      using r1(2) unfolding r3_def by auto
-    ultimately show ?thesis using that[of r3] \<open>g z\<noteq>0\<close> by auto
-  qed
-
-  have if_0:"if f z=0 then n > 0 else n=0"
-  proof -
-    have "f\<midarrow> z \<rightarrow> f z"
-      by (metis assms(4,6,7) at_within_open continuous_on holo holomorphic_on_imp_continuous_on)
-    then have "(\<lambda>w. g w * (w - z) powr of_int n) \<midarrow>z\<rightarrow> f z"
-      apply (elim Lim_transform_within_open[where s="ball z r"])
-      using r by auto
-    moreover have "g \<midarrow>z\<rightarrow>g z"
-      by (metis (mono_tags, lifting) open_ball at_within_open_subset
-          ball_subset_cball centre_in_ball continuous_on holomorphic_on_imp_continuous_on r(1,3) subsetCE)
-    ultimately have "(\<lambda>w. (g w * (w - z) powr of_int n) / g w) \<midarrow>z\<rightarrow> f z/g z"
-      apply (rule_tac tendsto_divide)
-      using \<open>g z\<noteq>0\<close> by auto
-    then have powr_tendsto:"(\<lambda>w. (w - z) powr of_int n) \<midarrow>z\<rightarrow> f z/g z"
-      apply (elim Lim_transform_within_open[where s="ball z r"])
-      using r by auto
-
-    have ?thesis when "n\<ge>0" "f z=0"
-    proof -
-      have "(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> f z/g z"
-        using powr_tendsto
-        apply (elim Lim_transform_within[where d=r])
-        by (auto simp add: powr_of_int \<open>n\<ge>0\<close> \<open>r>0\<close>)
-      then have *:"(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> 0" using \<open>f z=0\<close> by simp
-      moreover have False when "n=0"
-      proof -
-        have "(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> 1"
-          using \<open>n=0\<close> by auto
-        then show False using * using LIM_unique zero_neq_one by blast
-      qed
-      ultimately show ?thesis using that by fastforce
-    qed
-    moreover have ?thesis when "n\<ge>0" "f z\<noteq>0"
-    proof -
-      have False when "n>0"
-      proof -
-        have "(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> f z/g z"
-          using powr_tendsto
-          apply (elim Lim_transform_within[where d=r])
-          by (auto simp add: powr_of_int \<open>n\<ge>0\<close> \<open>r>0\<close>)
-        moreover have "(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> 0"
-          using \<open>n>0\<close> by (auto intro!:tendsto_eq_intros)
-        ultimately show False using \<open>f z\<noteq>0\<close> \<open>g z\<noteq>0\<close> using LIM_unique divide_eq_0_iff by blast
-      qed
-      then show ?thesis using that by force
-    qed
-    moreover have False when "n<0"
-    proof -
-      have "(\<lambda>w. inverse ((w - z) ^ nat (- n))) \<midarrow>z\<rightarrow> f z/g z"
-           "(\<lambda>w.((w - z) ^ nat (- n))) \<midarrow>z\<rightarrow> 0"
-        subgoal  using powr_tendsto powr_of_int that
-          by (elim Lim_transform_within_open[where s=UNIV],auto)
-        subgoal using that by (auto intro!:tendsto_eq_intros)
-        done
-      from tendsto_mult[OF this,simplified]
-      have "(\<lambda>x. inverse ((x - z) ^ nat (- n)) * (x - z) ^ nat (- n)) \<midarrow>z\<rightarrow> 0" .
-      then have "(\<lambda>x. 1::complex) \<midarrow>z\<rightarrow> 0"
-        by (elim Lim_transform_within_open[where s=UNIV],auto)
-      then show False using LIM_const_eq by fastforce
-    qed
-    ultimately show ?thesis by fastforce
-  qed
-  moreover have "f w  = g w * (w-z) ^ nat n  \<and> g w \<noteq>0" when "w\<in>cball z r" for w
-  proof (cases "w=z")
-    case True
-    then have "f \<midarrow>z\<rightarrow>f w"
-      using assms(4,6) at_within_open continuous_on holo holomorphic_on_imp_continuous_on by fastforce
-    then have "(\<lambda>w. g w * (w-z) ^ nat n) \<midarrow>z\<rightarrow>f w"
-    proof (elim Lim_transform_within[OF _ \<open>r>0\<close>])
-      fix x assume "0 < dist x z" "dist x z < r"
-      then have "x \<in> cball z r - {z}" "x\<noteq>z"
-        unfolding cball_def by (auto simp add: dist_commute)
-      then have "f x = g x * (x - z) powr of_int n"
-        using r(4)[rule_format,of x] by simp
-      also have "... = g x * (x - z) ^ nat n"
-        apply (subst powr_of_int)
-        using if_0 \<open>x\<noteq>z\<close> by (auto split:if_splits)
-      finally show "f x = g x * (x - z) ^ nat n" .
-    qed
-    moreover have "(\<lambda>w. g w * (w-z) ^ nat n) \<midarrow>z\<rightarrow> g w * (w-z) ^ nat n"
-      using True apply (auto intro!:tendsto_eq_intros)
-      by (metis open_ball at_within_open_subset ball_subset_cball centre_in_ball
-          continuous_on holomorphic_on_imp_continuous_on r(1) r(3) that)
-    ultimately have "f w = g w * (w-z) ^ nat n" using LIM_unique by blast
-    then show ?thesis using \<open>g z\<noteq>0\<close> True by auto
-  next
-    case False
-    then have "f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0"
-      using r(4) that by auto
-    then show ?thesis using False if_0 powr_of_int by (auto split:if_splits)
-  qed
-  ultimately show ?thesis using r by auto
-qed
-
-lemma zorder_exist_pole:
-  fixes f::"complex \<Rightarrow> complex" and z::complex
-  defines "n\<equiv>zorder f z" and "g\<equiv>zor_poly f z"
-  assumes  holo: "f holomorphic_on s-{z}" and
-          "open s" "z\<in>s"
-      and "is_pole f z"
-  shows "n < 0 \<and> g z\<noteq>0 \<and> (\<exists>r. r>0 \<and> cball z r \<subseteq> s \<and> g holomorphic_on cball z r
-    \<and> (\<forall>w\<in>cball z r - {z}. f w  = g w / (w-z) ^ nat (- n) \<and> g w \<noteq>0))"
-proof -
-  obtain r where "g z \<noteq> 0" and r: "r>0" "cball z r \<subseteq> s" "g holomorphic_on cball z r"
-            "(\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
-  proof -
-    have "g z \<noteq> 0 \<and> (\<exists>r>0. g holomorphic_on cball z r
-            \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0))"
-    proof (rule zorder_exist[of f z,folded g_def n_def])
-      show "isolated_singularity_at f z" unfolding isolated_singularity_at_def
-        using holo assms(4,5)
-        by (metis analytic_on_holomorphic centre_in_ball insert_Diff openE open_delete subset_insert_iff)
-      show "not_essential f z" unfolding not_essential_def
-        using assms(4,6) at_within_open continuous_on holo holomorphic_on_imp_continuous_on
-        by fastforce
-      from non_zero_neighbour_pole[OF \<open>is_pole f z\<close>] show "\<exists>\<^sub>F w in at z. f w \<noteq> 0"
-        apply (elim eventually_frequentlyE)
-        by auto
-    qed
-    then obtain r1 where "g z \<noteq> 0" "r1>0" and r1:"g holomorphic_on cball z r1"
-            "(\<forall>w\<in>cball z r1 - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
-      by auto
-    obtain r2 where r2: "r2>0" "cball z r2 \<subseteq> s"
-      using assms(4,5) open_contains_cball_eq by metis
-    define r3 where "r3=min r1 r2"
-    have "r3>0" "cball z r3 \<subseteq> s" using \<open>r1>0\<close> r2 unfolding r3_def by auto
-    moreover have "g holomorphic_on cball z r3"
-      using r1(1) unfolding r3_def by auto
-    moreover have "(\<forall>w\<in>cball z r3 - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
-      using r1(2) unfolding r3_def by auto
-    ultimately show ?thesis using that[of r3] \<open>g z\<noteq>0\<close> by auto
-  qed
-
-  have "n<0"
-  proof (rule ccontr)
-    assume " \<not> n < 0"
-    define c where "c=(if n=0 then g z else 0)"
-    have [simp]:"g \<midarrow>z\<rightarrow> g z"
-      by (metis open_ball at_within_open ball_subset_cball centre_in_ball
-            continuous_on holomorphic_on_imp_continuous_on holomorphic_on_subset r(1) r(3) )
-    have "\<forall>\<^sub>F x in at z. f x = g x * (x - z) ^ nat n"
-      unfolding eventually_at_topological
-      apply (rule_tac exI[where x="ball z r"])
-      using r powr_of_int \<open>\<not> n < 0\<close> by auto
-    moreover have "(\<lambda>x. g x * (x - z) ^ nat n) \<midarrow>z\<rightarrow>c"
-    proof (cases "n=0")
-      case True
-      then show ?thesis unfolding c_def by simp
-    next
-      case False
-      then have "(\<lambda>x. (x - z) ^ nat n) \<midarrow>z\<rightarrow> 0" using \<open>\<not> n < 0\<close>
-        by (auto intro!:tendsto_eq_intros)
-      from tendsto_mult[OF _ this,of g "g z",simplified]
-      show ?thesis unfolding c_def using False by simp
-    qed
-    ultimately have "f \<midarrow>z\<rightarrow>c" using tendsto_cong by fast
-    then show False using \<open>is_pole f z\<close> at_neq_bot not_tendsto_and_filterlim_at_infinity
-      unfolding is_pole_def by blast
-  qed
-  moreover have "\<forall>w\<in>cball z r - {z}. f w  = g w / (w-z) ^ nat (- n) \<and> g w \<noteq>0"
-    using r(4) \<open>n<0\<close> powr_of_int
-    by (metis Diff_iff divide_inverse eq_iff_diff_eq_0 insert_iff linorder_not_le)
-  ultimately show ?thesis using r(1-3) \<open>g z\<noteq>0\<close> by auto
-qed
-
-lemma zorder_eqI:
-  assumes "open s" "z \<in> s" "g holomorphic_on s" "g z \<noteq> 0"
-  assumes fg_eq:"\<And>w. \<lbrakk>w \<in> s;w\<noteq>z\<rbrakk> \<Longrightarrow> f w = g w * (w - z) powr n"
-  shows   "zorder f z = n"
-proof -
-  have "continuous_on s g" by (rule holomorphic_on_imp_continuous_on) fact
-  moreover have "open (-{0::complex})" by auto
-  ultimately have "open ((g -` (-{0})) \<inter> s)"
-    unfolding continuous_on_open_vimage[OF \<open>open s\<close>] by blast
-  moreover from assms have "z \<in> (g -` (-{0})) \<inter> s" by auto
-  ultimately obtain r where r: "r > 0" "cball z r \<subseteq>  s \<inter> (g -` (-{0}))"
-    unfolding open_contains_cball by blast
-
-  let ?gg= "(\<lambda>w. g w * (w - z) powr n)"
-  define P where "P = (\<lambda>n g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z\<noteq>0
-          \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w-z) powr (of_int n) \<and> g w\<noteq>0))"
-  have "P n g r"
-    unfolding P_def using r assms(3,4,5) by auto
-  then have "\<exists>g r. P n g r" by auto
-  moreover have unique: "\<exists>!n. \<exists>g r. P n g r" unfolding P_def
-  proof (rule holomorphic_factor_puncture)
-    have "ball z r-{z} \<subseteq> s" using r using ball_subset_cball by blast
-    then have "?gg holomorphic_on ball z r-{z}"
-      using \<open>g holomorphic_on s\<close> r by (auto intro!: holomorphic_intros)
-    then have "f holomorphic_on ball z r - {z}"
-      apply (elim holomorphic_transform)
-      using fg_eq \<open>ball z r-{z} \<subseteq> s\<close> by auto
-    then show "isolated_singularity_at f z" unfolding isolated_singularity_at_def
-      using analytic_on_open open_delete r(1) by blast
-  next
-    have "not_essential ?gg z"
-    proof (intro singularity_intros)
-      show "not_essential g z"
-        by (meson \<open>continuous_on s g\<close> assms(1) assms(2) continuous_on_eq_continuous_at
-            isCont_def not_essential_def)
-      show " \<forall>\<^sub>F w in at z. w - z \<noteq> 0" by (simp add: eventually_at_filter)
-      then show "LIM w at z. w - z :> at 0"
-        unfolding filterlim_at by (auto intro:tendsto_eq_intros)
-      show "isolated_singularity_at g z"
-        by (meson Diff_subset open_ball analytic_on_holomorphic
-            assms(1,2,3) holomorphic_on_subset isolated_singularity_at_def openE)
-    qed
-    then show "not_essential f z"
-      apply (elim not_essential_transform)
-      unfolding eventually_at using assms(1,2) assms(5)[symmetric]
-      by (metis dist_commute mem_ball openE subsetCE)
-    show "\<exists>\<^sub>F w in at z. f w \<noteq> 0" unfolding frequently_at
-    proof (rule,rule)
-      fix d::real assume "0 < d"
-      define z' where "z'=z+min d r / 2"
-      have "z' \<noteq> z" " dist z' z < d "
-        unfolding z'_def using \<open>d>0\<close> \<open>r>0\<close>
-        by (auto simp add:dist_norm)
-      moreover have "f z' \<noteq> 0"
-      proof (subst fg_eq[OF _ \<open>z'\<noteq>z\<close>])
-        have "z' \<in> cball z r" unfolding z'_def using \<open>r>0\<close> \<open>d>0\<close> by (auto simp add:dist_norm)
-        then show " z' \<in> s" using r(2) by blast
-        show "g z' * (z' - z) powr of_int n \<noteq> 0"
-          using P_def \<open>P n g r\<close> \<open>z' \<in> cball z r\<close> calculation(1) by auto
-      qed
-      ultimately show "\<exists>x\<in>UNIV. x \<noteq> z \<and> dist x z < d \<and> f x \<noteq> 0" by auto
-    qed
-  qed
-  ultimately have "(THE n. \<exists>g r. P n g r) = n"
-    by (rule_tac the1_equality)
-  then show ?thesis unfolding zorder_def P_def by blast
-qed
-
-lemma residue_pole_order:
-  fixes f::"complex \<Rightarrow> complex" and z::complex
-  defines "n \<equiv> nat (- zorder f z)" and "h \<equiv> zor_poly f z"
-  assumes f_iso:"isolated_singularity_at f z"
-    and pole:"is_pole f z"
-  shows "residue f z = ((deriv ^^ (n - 1)) h z / fact (n-1))"
-proof -
-  define g where "g \<equiv> \<lambda>x. if x=z then 0 else inverse (f x)"
-  obtain e where [simp]:"e>0" and f_holo:"f holomorphic_on ball z e - {z}"
-    using f_iso analytic_imp_holomorphic unfolding isolated_singularity_at_def by blast
-  obtain r where "0 < n" "0 < r" and r_cball:"cball z r \<subseteq> ball z e" and h_holo: "h holomorphic_on cball z r"
-      and h_divide:"(\<forall>w\<in>cball z r. (w\<noteq>z \<longrightarrow> f w = h w / (w - z) ^ n) \<and> h w \<noteq> 0)"
-  proof -
-    obtain r where r:"zorder f z < 0" "h z \<noteq> 0" "r>0" "cball z r \<subseteq> ball z e" "h holomorphic_on cball z r"
-        "(\<forall>w\<in>cball z r - {z}. f w = h w / (w - z) ^ n \<and> h w \<noteq> 0)"
-      using zorder_exist_pole[OF f_holo,simplified,OF \<open>is_pole f z\<close>,folded n_def h_def] by auto
-    have "n>0" using \<open>zorder f z < 0\<close> unfolding n_def by simp
-    moreover have "(\<forall>w\<in>cball z r. (w\<noteq>z \<longrightarrow> f w = h w / (w - z) ^ n) \<and> h w \<noteq> 0)"
-      using \<open>h z\<noteq>0\<close> r(6) by blast
-    ultimately show ?thesis using r(3,4,5) that by blast
-  qed
-  have r_nonzero:"\<And>w. w \<in> ball z r - {z} \<Longrightarrow> f w \<noteq> 0"
-    using h_divide by simp
-  define c where "c \<equiv> 2 * pi * \<i>"
-  define der_f where "der_f \<equiv> ((deriv ^^ (n - 1)) h z / fact (n-1))"
-  define h' where "h' \<equiv> \<lambda>u. h u / (u - z) ^ n"
-  have "(h' has_contour_integral c / fact (n - 1) * (deriv ^^ (n - 1)) h z) (circlepath z r)"
-    unfolding h'_def
-    proof (rule Cauchy_has_contour_integral_higher_derivative_circlepath[of z r h z "n-1",
-        folded c_def Suc_pred'[OF \<open>n>0\<close>]])
-      show "continuous_on (cball z r) h" using holomorphic_on_imp_continuous_on h_holo by simp
-      show "h holomorphic_on ball z r" using h_holo by auto
-      show " z \<in> ball z r" using \<open>r>0\<close> by auto
-    qed
-  then have "(h' has_contour_integral c * der_f) (circlepath z r)" unfolding der_f_def by auto
-  then have "(f has_contour_integral c * der_f) (circlepath z r)"
-    proof (elim has_contour_integral_eq)
-      fix x assume "x \<in> path_image (circlepath z r)"
-      hence "x\<in>cball z r - {z}" using \<open>r>0\<close> by auto
-      then show "h' x = f x" using h_divide unfolding h'_def by auto
-    qed
-  moreover have "(f has_contour_integral c * residue f z) (circlepath z r)"
-    using base_residue[of \<open>ball z e\<close> z,simplified,OF \<open>r>0\<close> f_holo r_cball,folded c_def]
-    unfolding c_def by simp
-  ultimately have "c * der_f =  c * residue f z" using has_contour_integral_unique by blast
-  hence "der_f = residue f z" unfolding c_def by auto
-  thus ?thesis unfolding der_f_def by auto
-qed
-
-lemma simple_zeroI:
-  assumes "open s" "z \<in> s" "g holomorphic_on s" "g z \<noteq> 0"
-  assumes "\<And>w. w \<in> s \<Longrightarrow> f w = g w * (w - z)"
-  shows   "zorder f z = 1"
-  using assms(1-4) by (rule zorder_eqI) (use assms(5) in auto)
-
-lemma higher_deriv_power:
-  shows   "(deriv ^^ j) (\<lambda>w. (w - z) ^ n) w =
-             pochhammer (of_nat (Suc n - j)) j * (w - z) ^ (n - j)"
-proof (induction j arbitrary: w)
-  case 0
-  thus ?case by auto
-next
-  case (Suc j w)
-  have "(deriv ^^ Suc j) (\<lambda>w. (w - z) ^ n) w = deriv ((deriv ^^ j) (\<lambda>w. (w - z) ^ n)) w"
-    by simp
-  also have "(deriv ^^ j) (\<lambda>w. (w - z) ^ n) =
-               (\<lambda>w. pochhammer (of_nat (Suc n - j)) j * (w - z) ^ (n - j))"
-    using Suc by (intro Suc.IH ext)
-  also {
-    have "(\<dots> has_field_derivative of_nat (n - j) *
-               pochhammer (of_nat (Suc n - j)) j * (w - z) ^ (n - Suc j)) (at w)"
-      using Suc.prems by (auto intro!: derivative_eq_intros)
-    also have "of_nat (n - j) * pochhammer (of_nat (Suc n - j)) j =
-                 pochhammer (of_nat (Suc n - Suc j)) (Suc j)"
-      by (cases "Suc j \<le> n", subst pochhammer_rec)
-         (insert Suc.prems, simp_all add: algebra_simps Suc_diff_le pochhammer_0_left)
-    finally have "deriv (\<lambda>w. pochhammer (of_nat (Suc n - j)) j * (w - z) ^ (n - j)) w =
-                    \<dots> * (w - z) ^ (n - Suc j)"
-      by (rule DERIV_imp_deriv)
-  }
-  finally show ?case .
-qed
-
-lemma zorder_zero_eqI:
-  assumes  f_holo:"f holomorphic_on s" and "open s" "z \<in> s"
-  assumes zero: "\<And>i. i < nat n \<Longrightarrow> (deriv ^^ i) f z = 0"
-  assumes nz: "(deriv ^^ nat n) f z \<noteq> 0" and "n\<ge>0"
-  shows   "zorder f z = n"
-proof -
-  obtain r where [simp]:"r>0" and "ball z r \<subseteq> s"
-    using \<open>open s\<close> \<open>z\<in>s\<close> openE by blast
-  have nz':"\<exists>w\<in>ball z r. f w \<noteq> 0"
-  proof (rule ccontr)
-    assume "\<not> (\<exists>w\<in>ball z r. f w \<noteq> 0)"
-    then have "eventually (\<lambda>u. f u = 0) (nhds z)"
-      using \<open>r>0\<close> unfolding eventually_nhds
-      apply (rule_tac x="ball z r" in exI)
-      by auto
-    then have "(deriv ^^ nat n) f z = (deriv ^^ nat n) (\<lambda>_. 0) z"
-      by (intro higher_deriv_cong_ev) auto
-    also have "(deriv ^^ nat n) (\<lambda>_. 0) z = 0"
-      by (induction n) simp_all
-    finally show False using nz by contradiction
-  qed
-
-  define zn g where "zn = zorder f z" and "g = zor_poly f z"
-  obtain e where e_if:"if f z = 0 then 0 < zn else zn = 0" and
-            [simp]:"e>0" and "cball z e \<subseteq> ball z r" and
-            g_holo:"g holomorphic_on cball z e" and
-            e_fac:"(\<forall>w\<in>cball z e. f w = g w * (w - z) ^ nat zn \<and> g w \<noteq> 0)"
-  proof -
-    have "f holomorphic_on ball z r"
-      using f_holo \<open>ball z r \<subseteq> s\<close> by auto
-    from that zorder_exist_zero[of f "ball z r" z,simplified,OF this nz',folded zn_def g_def]
-    show ?thesis by blast
-  qed
-  from this(1,2,5) have "zn\<ge>0" "g z\<noteq>0"
-    subgoal by (auto split:if_splits)
-    subgoal using \<open>0 < e\<close> ball_subset_cball centre_in_ball e_fac by blast
-    done
-
-  define A where "A = (\<lambda>i. of_nat (i choose (nat zn)) * fact (nat zn) * (deriv ^^ (i - nat zn)) g z)"
-  have deriv_A:"(deriv ^^ i) f z = (if zn \<le> int i then A i else 0)" for i
-  proof -
-    have "eventually (\<lambda>w. w \<in> ball z e) (nhds z)"
-      using \<open>cball z e \<subseteq> ball z r\<close> \<open>e>0\<close> by (intro eventually_nhds_in_open) auto
-    hence "eventually (\<lambda>w. f w = (w - z) ^ (nat zn) * g w) (nhds z)"
-      apply eventually_elim
-      by (use e_fac in auto)
-    hence "(deriv ^^ i) f z = (deriv ^^ i) (\<lambda>w. (w - z) ^ nat zn * g w) z"
-      by (intro higher_deriv_cong_ev) auto
-    also have "\<dots> = (\<Sum>j=0..i. of_nat (i choose j) *
-                       (deriv ^^ j) (\<lambda>w. (w - z) ^ nat zn) z * (deriv ^^ (i - j)) g z)"
-      using g_holo \<open>e>0\<close>
-      by (intro higher_deriv_mult[of _ "ball z e"]) (auto intro!: holomorphic_intros)
-    also have "\<dots> = (\<Sum>j=0..i. if j = nat zn then
-                    of_nat (i choose nat zn) * fact (nat zn) * (deriv ^^ (i - nat zn)) g z else 0)"
-    proof (intro sum.cong refl, goal_cases)
-      case (1 j)
-      have "(deriv ^^ j) (\<lambda>w. (w - z) ^ nat zn) z =
-              pochhammer (of_nat (Suc (nat zn) - j)) j * 0 ^ (nat zn - j)"
-        by (subst higher_deriv_power) auto
-      also have "\<dots> = (if j = nat zn then fact j else 0)"
-        by (auto simp: not_less pochhammer_0_left pochhammer_fact)
-      also have "of_nat (i choose j) * \<dots> * (deriv ^^ (i - j)) g z =
-                   (if j = nat zn then of_nat (i choose (nat zn)) * fact (nat zn)
-                        * (deriv ^^ (i - nat zn)) g z else 0)"
-        by simp
-      finally show ?case .
-    qed
-    also have "\<dots> = (if i \<ge> zn then A i else 0)"
-      by (auto simp: A_def)
-    finally show "(deriv ^^ i) f z = \<dots>" .
-  qed
-
-  have False when "n<zn"
-  proof -
-    have "(deriv ^^ nat n) f z = 0"
-      using deriv_A[of "nat n"] that \<open>n\<ge>0\<close> by auto
-    with nz show False by auto
-  qed
-  moreover have "n\<le>zn"
-  proof -
-    have "g z \<noteq> 0" using e_fac[rule_format,of z] \<open>e>0\<close> by simp
-    then have "(deriv ^^ nat zn) f z \<noteq> 0"
-      using deriv_A[of "nat zn"] by(auto simp add:A_def)
-    then have "nat zn \<ge> nat n" using zero[of "nat zn"] by linarith
-    moreover have "zn\<ge>0" using e_if by (auto split:if_splits)
-    ultimately show ?thesis using nat_le_eq_zle by blast
-  qed
-  ultimately show ?thesis unfolding zn_def by fastforce
-qed
-
-lemma
-  assumes "eventually (\<lambda>z. f z = g z) (at z)" "z = z'"
-  shows zorder_cong:"zorder f z = zorder g z'" and zor_poly_cong:"zor_poly f z = zor_poly g z'"
-proof -
-  define P where "P = (\<lambda>ff n h r. 0 < r \<and> h holomorphic_on cball z r \<and> h z\<noteq>0
-                    \<and> (\<forall>w\<in>cball z r - {z}. ff w = h w * (w-z) powr (of_int n) \<and> h w\<noteq>0))"
-  have "(\<exists>r. P f n h r) = (\<exists>r. P g n h r)" for n h
-  proof -
-    have *: "\<exists>r. P g n h r" if "\<exists>r. P f n h r" and "eventually (\<lambda>x. f x = g x) (at z)" for f g
-    proof -
-      from that(1) obtain r1 where r1_P:"P f n h r1" by auto
-      from that(2) obtain r2 where "r2>0" and r2_dist:"\<forall>x. x \<noteq> z \<and> dist x z \<le> r2 \<longrightarrow> f x = g x"
-        unfolding eventually_at_le by auto
-      define r where "r=min r1 r2"
-      have "r>0" "h z\<noteq>0" using r1_P \<open>r2>0\<close> unfolding r_def P_def by auto
-      moreover have "h holomorphic_on cball z r"
-        using r1_P unfolding P_def r_def by auto
-      moreover have "g w = h w * (w - z) powr of_int n \<and> h w \<noteq> 0" when "w\<in>cball z r - {z}" for w
-      proof -
-        have "f w = h w * (w - z) powr of_int n \<and> h w \<noteq> 0"
-          using r1_P that unfolding P_def r_def by auto
-        moreover have "f w=g w" using r2_dist[rule_format,of w] that unfolding r_def
-          by (simp add: dist_commute)
-        ultimately show ?thesis by simp
-      qed
-      ultimately show ?thesis unfolding P_def by auto
-    qed
-    from assms have eq': "eventually (\<lambda>z. g z = f z) (at z)"
-      by (simp add: eq_commute)
-    show ?thesis
-      by (rule iffI[OF *[OF _ assms(1)] *[OF _ eq']])
-  qed
-  then show "zorder f z = zorder g z'" "zor_poly f z = zor_poly g z'"
-      using \<open>z=z'\<close> unfolding P_def zorder_def zor_poly_def by auto
-qed
-
-lemma zorder_nonzero_div_power:
-  assumes "open s" "z \<in> s" "f holomorphic_on s" "f z \<noteq> 0" "n > 0"
-  shows  "zorder (\<lambda>w. f w / (w - z) ^ n) z = - n"
-  apply (rule zorder_eqI[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>f holomorphic_on s\<close> \<open>f z\<noteq>0\<close>])
-  apply (subst powr_of_int)
-  using \<open>n>0\<close> by (auto simp add:field_simps)
-
-lemma zor_poly_eq:
-  assumes "isolated_singularity_at f z" "not_essential f z" "\<exists>\<^sub>F w in at z. f w \<noteq> 0"
-  shows "eventually (\<lambda>w. zor_poly f z w = f w * (w - z) powr - zorder f z) (at z)"
-proof -
-  obtain r where r:"r>0"
-       "(\<forall>w\<in>cball z r - {z}. f w = zor_poly f z w * (w - z) powr of_int (zorder f z))"
-    using zorder_exist[OF assms] by blast
-  then have *: "\<forall>w\<in>ball z r - {z}. zor_poly f z w = f w * (w - z) powr - zorder f z"
-    by (auto simp: field_simps powr_minus)
-  have "eventually (\<lambda>w. w \<in> ball z r - {z}) (at z)"
-    using r eventually_at_ball'[of r z UNIV] by auto
-  thus ?thesis by eventually_elim (insert *, auto)
-qed
-
-lemma zor_poly_zero_eq:
-  assumes "f holomorphic_on s" "open s" "connected s" "z \<in> s" "\<exists>w\<in>s. f w \<noteq> 0"
-  shows "eventually (\<lambda>w. zor_poly f z w = f w / (w - z) ^ nat (zorder f z)) (at z)"
-proof -
-  obtain r where r:"r>0"
-       "(\<forall>w\<in>cball z r - {z}. f w = zor_poly f z w * (w - z) ^ nat (zorder f z))"
-    using zorder_exist_zero[OF assms] by auto
-  then have *: "\<forall>w\<in>ball z r - {z}. zor_poly f z w = f w / (w - z) ^ nat (zorder f z)"
-    by (auto simp: field_simps powr_minus)
-  have "eventually (\<lambda>w. w \<in> ball z r - {z}) (at z)"
-    using r eventually_at_ball'[of r z UNIV] by auto
-  thus ?thesis by eventually_elim (insert *, auto)
-qed
-
-lemma zor_poly_pole_eq:
-  assumes f_iso:"isolated_singularity_at f z" "is_pole f z"
-  shows "eventually (\<lambda>w. zor_poly f z w = f w * (w - z) ^ nat (- zorder f z)) (at z)"
-proof -
-  obtain e where [simp]:"e>0" and f_holo:"f holomorphic_on ball z e - {z}"
-    using f_iso analytic_imp_holomorphic unfolding isolated_singularity_at_def by blast
-  obtain r where r:"r>0"
-       "(\<forall>w\<in>cball z r - {z}. f w = zor_poly f z w / (w - z) ^ nat (- zorder f z))"
-    using zorder_exist_pole[OF f_holo,simplified,OF \<open>is_pole f z\<close>] by auto
-  then have *: "\<forall>w\<in>ball z r - {z}. zor_poly f z w = f w * (w - z) ^ nat (- zorder f z)"
-    by (auto simp: field_simps)
-  have "eventually (\<lambda>w. w \<in> ball z r - {z}) (at z)"
-    using r eventually_at_ball'[of r z UNIV] by auto
-  thus ?thesis by eventually_elim (insert *, auto)
-qed
-
-lemma zor_poly_eqI:
-  fixes f :: "complex \<Rightarrow> complex" and z0 :: complex
-  defines "n \<equiv> zorder f z0"
-  assumes "isolated_singularity_at f z0" "not_essential f z0" "\<exists>\<^sub>F w in at z0. f w \<noteq> 0"
-  assumes lim: "((\<lambda>x. f (g x) * (g x - z0) powr - n) \<longlongrightarrow> c) F"
-  assumes g: "filterlim g (at z0) F" and "F \<noteq> bot"
-  shows   "zor_poly f z0 z0 = c"
-proof -
-  from zorder_exist[OF assms(2-4)] obtain r where
-    r: "r > 0" "zor_poly f z0 holomorphic_on cball z0 r"
-       "\<And>w. w \<in> cball z0 r - {z0} \<Longrightarrow> f w = zor_poly f z0 w * (w - z0) powr n"
-    unfolding n_def by blast
-  from r(1) have "eventually (\<lambda>w. w \<in> ball z0 r \<and> w \<noteq> z0) (at z0)"
-    using eventually_at_ball'[of r z0 UNIV] by auto
-  hence "eventually (\<lambda>w. zor_poly f z0 w = f w * (w - z0) powr - n) (at z0)"
-    by eventually_elim (insert r, auto simp: field_simps powr_minus)
-  moreover have "continuous_on (ball z0 r) (zor_poly f z0)"
-    using r by (intro holomorphic_on_imp_continuous_on) auto
-  with r(1,2) have "isCont (zor_poly f z0) z0"
-    by (auto simp: continuous_on_eq_continuous_at)
-  hence "(zor_poly f z0 \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
-    unfolding isCont_def .
-  ultimately have "((\<lambda>w. f w * (w - z0) powr - n) \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
-    by (blast intro: Lim_transform_eventually)
-  hence "((\<lambda>x. f (g x) * (g x - z0) powr - n) \<longlongrightarrow> zor_poly f z0 z0) F"
-    by (rule filterlim_compose[OF _ g])
-  from tendsto_unique[OF \<open>F \<noteq> bot\<close> this lim] show ?thesis .
-qed
-
-lemma zor_poly_zero_eqI:
-  fixes f :: "complex \<Rightarrow> complex" and z0 :: complex
-  defines "n \<equiv> zorder f z0"
-  assumes "f holomorphic_on A" "open A" "connected A" "z0 \<in> A" "\<exists>z\<in>A. f z \<noteq> 0"
-  assumes lim: "((\<lambda>x. f (g x) / (g x - z0) ^ nat n) \<longlongrightarrow> c) F"
-  assumes g: "filterlim g (at z0) F" and "F \<noteq> bot"
-  shows   "zor_poly f z0 z0 = c"
-proof -
-  from zorder_exist_zero[OF assms(2-6)] obtain r where
-    r: "r > 0" "cball z0 r \<subseteq> A" "zor_poly f z0 holomorphic_on cball z0 r"
-       "\<And>w. w \<in> cball z0 r \<Longrightarrow> f w = zor_poly f z0 w * (w - z0) ^ nat n"
-    unfolding n_def by blast
-  from r(1) have "eventually (\<lambda>w. w \<in> ball z0 r \<and> w \<noteq> z0) (at z0)"
-    using eventually_at_ball'[of r z0 UNIV] by auto
-  hence "eventually (\<lambda>w. zor_poly f z0 w = f w / (w - z0) ^ nat n) (at z0)"
-    by eventually_elim (insert r, auto simp: field_simps)
-  moreover have "continuous_on (ball z0 r) (zor_poly f z0)"
-    using r by (intro holomorphic_on_imp_continuous_on) auto
-  with r(1,2) have "isCont (zor_poly f z0) z0"
-    by (auto simp: continuous_on_eq_continuous_at)
-  hence "(zor_poly f z0 \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
-    unfolding isCont_def .
-  ultimately have "((\<lambda>w. f w / (w - z0) ^ nat n) \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
-    by (blast intro: Lim_transform_eventually)
-  hence "((\<lambda>x. f (g x) / (g x - z0) ^ nat n) \<longlongrightarrow> zor_poly f z0 z0) F"
-    by (rule filterlim_compose[OF _ g])
-  from tendsto_unique[OF \<open>F \<noteq> bot\<close> this lim] show ?thesis .
-qed
-
-lemma zor_poly_pole_eqI:
-  fixes f :: "complex \<Rightarrow> complex" and z0 :: complex
-  defines "n \<equiv> zorder f z0"
-  assumes f_iso:"isolated_singularity_at f z0" and "is_pole f z0"
-  assumes lim: "((\<lambda>x. f (g x) * (g x - z0) ^ nat (-n)) \<longlongrightarrow> c) F"
-  assumes g: "filterlim g (at z0) F" and "F \<noteq> bot"
-  shows   "zor_poly f z0 z0 = c"
-proof -
-  obtain r where r: "r > 0"  "zor_poly f z0 holomorphic_on cball z0 r"
-  proof -
-    have "\<exists>\<^sub>F w in at z0. f w \<noteq> 0"
-      using non_zero_neighbour_pole[OF \<open>is_pole f z0\<close>] by (auto elim:eventually_frequentlyE)
-    moreover have "not_essential f z0" unfolding not_essential_def using \<open>is_pole f z0\<close> by simp
-    ultimately show ?thesis using that zorder_exist[OF f_iso,folded n_def] by auto
-  qed
-  from r(1) have "eventually (\<lambda>w. w \<in> ball z0 r \<and> w \<noteq> z0) (at z0)"
-    using eventually_at_ball'[of r z0 UNIV] by auto
-  have "eventually (\<lambda>w. zor_poly f z0 w = f w * (w - z0) ^ nat (-n)) (at z0)"
-    using zor_poly_pole_eq[OF f_iso \<open>is_pole f z0\<close>] unfolding n_def .
-  moreover have "continuous_on (ball z0 r) (zor_poly f z0)"
-    using r by (intro holomorphic_on_imp_continuous_on) auto
-  with r(1,2) have "isCont (zor_poly f z0) z0"
-    by (auto simp: continuous_on_eq_continuous_at)
-  hence "(zor_poly f z0 \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
-    unfolding isCont_def .
-  ultimately have "((\<lambda>w. f w * (w - z0) ^ nat (-n)) \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
-    by (blast intro: Lim_transform_eventually)
-  hence "((\<lambda>x. f (g x) * (g x - z0) ^ nat (-n)) \<longlongrightarrow> zor_poly f z0 z0) F"
-    by (rule filterlim_compose[OF _ g])
-  from tendsto_unique[OF \<open>F \<noteq> bot\<close> this lim] show ?thesis .
-qed
-
-lemma residue_simple_pole:
-  assumes "isolated_singularity_at f z0"
-  assumes "is_pole f z0" "zorder f z0 = - 1"
-  shows   "residue f z0 = zor_poly f z0 z0"
-  using assms by (subst residue_pole_order) simp_all
-
-lemma residue_simple_pole_limit:
-  assumes "isolated_singularity_at f z0"
-  assumes "is_pole f z0" "zorder f z0 = - 1"
-  assumes "((\<lambda>x. f (g x) * (g x - z0)) \<longlongrightarrow> c) F"
-  assumes "filterlim g (at z0) F" "F \<noteq> bot"
-  shows   "residue f z0 = c"
-proof -
-  have "residue f z0 = zor_poly f z0 z0"
-    by (rule residue_simple_pole assms)+
-  also have "\<dots> = c"
-    apply (rule zor_poly_pole_eqI)
-    using assms by auto
-  finally show ?thesis .
-qed
-
-lemma lhopital_complex_simple:
-  assumes "(f has_field_derivative f') (at z)"
-  assumes "(g has_field_derivative g') (at z)"
-  assumes "f z = 0" "g z = 0" "g' \<noteq> 0" "f' / g' = c"
-  shows   "((\<lambda>w. f w / g w) \<longlongrightarrow> c) (at z)"
-proof -
-  have "eventually (\<lambda>w. w \<noteq> z) (at z)"
-    by (auto simp: eventually_at_filter)
-  hence "eventually (\<lambda>w. ((f w - f z) / (w - z)) / ((g w - g z) / (w - z)) = f w / g w) (at z)"
-    by eventually_elim (simp add: assms field_split_simps)
-  moreover have "((\<lambda>w. ((f w - f z) / (w - z)) / ((g w - g z) / (w - z))) \<longlongrightarrow> f' / g') (at z)"
-    by (intro tendsto_divide has_field_derivativeD assms)
-  ultimately have "((\<lambda>w. f w / g w) \<longlongrightarrow> f' / g') (at z)"
-    by (blast intro: Lim_transform_eventually)
-  with assms show ?thesis by simp
-qed
-
-lemma
-  assumes f_holo:"f holomorphic_on s" and g_holo:"g holomorphic_on s"
-          and "open s" "connected s" "z \<in> s"
-  assumes g_deriv:"(g has_field_derivative g') (at z)"
-  assumes "f z \<noteq> 0" "g z = 0" "g' \<noteq> 0"
-  shows   porder_simple_pole_deriv: "zorder (\<lambda>w. f w / g w) z = - 1"
-    and   residue_simple_pole_deriv: "residue (\<lambda>w. f w / g w) z = f z / g'"
-proof -
-  have [simp]:"isolated_singularity_at f z" "isolated_singularity_at g z"
-    using isolated_singularity_at_holomorphic[OF _ \<open>open s\<close> \<open>z\<in>s\<close>] f_holo g_holo
-    by (meson Diff_subset holomorphic_on_subset)+
-  have [simp]:"not_essential f z" "not_essential g z"
-    unfolding not_essential_def using f_holo g_holo assms(3,5)
-    by (meson continuous_on_eq_continuous_at continuous_within holomorphic_on_imp_continuous_on)+
-  have g_nconst:"\<exists>\<^sub>F w in at z. g w \<noteq>0 "
-  proof (rule ccontr)
-    assume "\<not> (\<exists>\<^sub>F w in at z. g w \<noteq> 0)"
-    then have "\<forall>\<^sub>F w in nhds z. g w = 0"
-      unfolding eventually_at eventually_nhds frequently_at using \<open>g z = 0\<close>
-      by (metis open_ball UNIV_I centre_in_ball dist_commute mem_ball)
-    then have "deriv g z = deriv (\<lambda>_. 0) z"
-      by (intro deriv_cong_ev) auto
-    then have "deriv g z = 0" by auto
-    then have "g' = 0" using g_deriv DERIV_imp_deriv by blast
-    then show False using \<open>g'\<noteq>0\<close> by auto
-  qed
-
-  have "zorder (\<lambda>w. f w / g w) z = zorder f z - zorder g z"
-  proof -
-    have "\<forall>\<^sub>F w in at z. f w \<noteq>0 \<and> w\<in>s"
-      apply (rule non_zero_neighbour_alt)
-      using assms by auto
-    with g_nconst have "\<exists>\<^sub>F w in at z. f w * g w \<noteq> 0"
-      by (elim frequently_rev_mp eventually_rev_mp,auto)
-    then show ?thesis using zorder_divide[of f z g] by auto
-  qed
-  moreover have "zorder f z=0"
-    apply (rule zorder_zero_eqI[OF f_holo \<open>open s\<close> \<open>z\<in>s\<close>])
-    using \<open>f z\<noteq>0\<close> by auto
-  moreover have "zorder g z=1"
-    apply (rule zorder_zero_eqI[OF g_holo \<open>open s\<close> \<open>z\<in>s\<close>])
-    subgoal using assms(8) by auto
-    subgoal using DERIV_imp_deriv assms(9) g_deriv by auto
-    subgoal by simp
-    done
-  ultimately show "zorder (\<lambda>w. f w / g w) z = - 1" by auto
-
-  show "residue (\<lambda>w. f w / g w) z = f z / g'"
-  proof (rule residue_simple_pole_limit[where g=id and F="at z",simplified])
-    show "zorder (\<lambda>w. f w / g w) z = - 1" by fact
-    show "isolated_singularity_at (\<lambda>w. f w / g w) z"
-      by (auto intro: singularity_intros)
-    show "is_pole (\<lambda>w. f w / g w) z"
-    proof (rule is_pole_divide)
-      have "\<forall>\<^sub>F x in at z. g x \<noteq> 0"
-        apply (rule non_zero_neighbour)
-        using g_nconst by auto
-      moreover have "g \<midarrow>z\<rightarrow> 0"
-        using DERIV_isCont assms(8) continuous_at g_deriv by force
-      ultimately show "filterlim g (at 0) (at z)" unfolding filterlim_at by simp
-      show "isCont f z"
-        using assms(3,5) continuous_on_eq_continuous_at f_holo holomorphic_on_imp_continuous_on
-        by auto
-      show "f z \<noteq> 0" by fact
-    qed
-    show "filterlim id (at z) (at z)" by (simp add: filterlim_iff)
-    have "((\<lambda>w. (f w * (w - z)) / g w) \<longlongrightarrow> f z / g') (at z)"
-    proof (rule lhopital_complex_simple)
-      show "((\<lambda>w. f w * (w - z)) has_field_derivative f z) (at z)"
-        using assms by (auto intro!: derivative_eq_intros holomorphic_derivI[OF f_holo])
-      show "(g has_field_derivative g') (at z)" by fact
-    qed (insert assms, auto)
-    then show "((\<lambda>w. (f w / g w) * (w - z)) \<longlongrightarrow> f z / g') (at z)"
-      by (simp add: field_split_simps)
-  qed
-qed
-
-subsection \<open>The argument principle\<close>
-
-theorem argument_principle:
-  fixes f::"complex \<Rightarrow> complex" and poles s:: "complex set"
-  defines "pz \<equiv> {w. f w = 0 \<or> w \<in> poles}" \<comment> \<open>\<^term>\<open>pz\<close> is the set of poles and zeros\<close>
-  assumes "open s" and
-          "connected s" and
-          f_holo:"f holomorphic_on s-poles" and
-          h_holo:"h holomorphic_on s" and
-          "valid_path g" and
-          loop:"pathfinish g = pathstart g" and
-          path_img:"path_image g \<subseteq> s - pz" and
-          homo:"\<forall>z. (z \<notin> s) \<longrightarrow> winding_number g z = 0" and
-          finite:"finite pz" and
-          poles:"\<forall>p\<in>poles. is_pole f p"
-  shows "contour_integral g (\<lambda>x. deriv f x * h x / f x) = 2 * pi * \<i> *
-          (\<Sum>p\<in>pz. winding_number g p * h p * zorder f p)"
-    (is "?L=?R")
-proof -
-  define c where "c \<equiv> 2 * complex_of_real pi * \<i> "
-  define ff where "ff \<equiv> (\<lambda>x. deriv f x * h x / f x)"
-  define cont where "cont \<equiv> \<lambda>ff p e. (ff has_contour_integral c * zorder f p * h p ) (circlepath p e)"
-  define avoid where "avoid \<equiv> \<lambda>p e. \<forall>w\<in>cball p e. w \<in> s \<and> (w \<noteq> p \<longrightarrow> w \<notin> pz)"
-
-  have "\<exists>e>0. avoid p e \<and> (p\<in>pz \<longrightarrow> cont ff p e)" when "p\<in>s" for p
-  proof -
-    obtain e1 where "e1>0" and e1_avoid:"avoid p e1"
-      using finite_cball_avoid[OF \<open>open s\<close> finite] \<open>p\<in>s\<close> unfolding avoid_def by auto
-    have "\<exists>e2>0. cball p e2 \<subseteq> ball p e1 \<and> cont ff p e2" when "p\<in>pz"
-    proof -
-      define po where "po \<equiv> zorder f p"
-      define pp where "pp \<equiv> zor_poly f p"
-      define f' where "f' \<equiv> \<lambda>w. pp w * (w - p) powr po"
-      define ff' where "ff' \<equiv> (\<lambda>x. deriv f' x * h x / f' x)"
-      obtain r where "pp p\<noteq>0" "r>0" and
-          "r<e1" and
-          pp_holo:"pp holomorphic_on cball p r" and
-          pp_po:"(\<forall>w\<in>cball p r-{p}. f w = pp w * (w - p) powr po \<and> pp w \<noteq> 0)"
-      proof -
-        have "isolated_singularity_at f p"
-        proof -
-          have "f holomorphic_on ball p e1 - {p}"
-            apply (intro holomorphic_on_subset[OF f_holo])
-            using e1_avoid \<open>p\<in>pz\<close> unfolding avoid_def pz_def by force
-          then show ?thesis unfolding isolated_singularity_at_def
-            using \<open>e1>0\<close> analytic_on_open open_delete by blast
-        qed
-        moreover have "not_essential f p"
-        proof (cases "is_pole f p")
-          case True
-          then show ?thesis unfolding not_essential_def by auto
-        next
-          case False
-          then have "p\<in>s-poles" using \<open>p\<in>s\<close> poles unfolding pz_def by auto
-          moreover have "open (s-poles)"
-            using \<open>open s\<close>
-            apply (elim open_Diff)
-            apply (rule finite_imp_closed)
-            using finite unfolding pz_def by simp
-          ultimately have "isCont f p"
-            using holomorphic_on_imp_continuous_on[OF f_holo] continuous_on_eq_continuous_at
-            by auto
-          then show ?thesis unfolding isCont_def not_essential_def by auto
-        qed
-        moreover have "\<exists>\<^sub>F w in at p. f w \<noteq> 0 "
-        proof (rule ccontr)
-          assume "\<not> (\<exists>\<^sub>F w in at p. f w \<noteq> 0)"
-          then have "\<forall>\<^sub>F w in at p. f w= 0" unfolding frequently_def by auto
-          then obtain rr where "rr>0" "\<forall>w\<in>ball p rr - {p}. f w =0"
-            unfolding eventually_at by (auto simp add:dist_commute)
-          then have "ball p rr - {p} \<subseteq> {w\<in>ball p rr-{p}. f w=0}" by blast
-          moreover have "infinite (ball p rr - {p})" using \<open>rr>0\<close> using finite_imp_not_open by fastforce
-          ultimately have "infinite {w\<in>ball p rr-{p}. f w=0}" using infinite_super by blast
-          then have "infinite pz"
-            unfolding pz_def infinite_super by auto
-          then show False using \<open>finite pz\<close> by auto
-        qed
-        ultimately obtain r where "pp p \<noteq> 0" and r:"r>0" "pp holomorphic_on cball p r"
-                  "(\<forall>w\<in>cball p r - {p}. f w = pp w * (w - p) powr of_int po \<and> pp w \<noteq> 0)"
-          using zorder_exist[of f p,folded po_def pp_def] by auto
-        define r1 where "r1=min r e1 / 2"
-        have "r1<e1" unfolding r1_def using \<open>e1>0\<close> \<open>r>0\<close> by auto
-        moreover have "r1>0" "pp holomorphic_on cball p r1"
-                  "(\<forall>w\<in>cball p r1 - {p}. f w = pp w * (w - p) powr of_int po \<and> pp w \<noteq> 0)"
-          unfolding r1_def using \<open>e1>0\<close> r by auto
-        ultimately show ?thesis using that \<open>pp p\<noteq>0\<close> by auto
-      qed
-
-      define e2 where "e2 \<equiv> r/2"
-      have "e2>0" using \<open>r>0\<close> unfolding e2_def by auto
-      define anal where "anal \<equiv> \<lambda>w. deriv pp w * h w / pp w"
-      define prin where "prin \<equiv> \<lambda>w. po * h w / (w - p)"
-      have "((\<lambda>w.  prin w + anal w) has_contour_integral c * po * h p) (circlepath p e2)"
-      proof (rule has_contour_integral_add[of _ _ _ _ 0,simplified])
-        have "ball p r \<subseteq> s"
-          using \<open>r<e1\<close> avoid_def ball_subset_cball e1_avoid by (simp add: subset_eq)
-        then have "cball p e2 \<subseteq> s"
-          using \<open>r>0\<close> unfolding e2_def by auto
-        then have "(\<lambda>w. po * h w) holomorphic_on cball p e2"
-          using h_holo by (auto intro!: holomorphic_intros)
-        then show "(prin has_contour_integral c * po * h p ) (circlepath p e2)"
-          using Cauchy_integral_circlepath_simple[folded c_def, of "\<lambda>w. po * h w"] \<open>e2>0\<close>
-          unfolding prin_def by (auto simp add: mult.assoc)
-        have "anal holomorphic_on ball p r" unfolding anal_def
-          using pp_holo h_holo pp_po \<open>ball p r \<subseteq> s\<close> \<open>pp p\<noteq>0\<close>
-          by (auto intro!: holomorphic_intros)
-        then show "(anal has_contour_integral 0) (circlepath p e2)"
-          using e2_def \<open>r>0\<close>
-          by (auto elim!: Cauchy_theorem_disc_simple)
-      qed
-      then have "cont ff' p e2" unfolding cont_def po_def
-      proof (elim has_contour_integral_eq)
-        fix w assume "w \<in> path_image (circlepath p e2)"
-        then have "w\<in>ball p r" and "w\<noteq>p" unfolding e2_def using \<open>r>0\<close> by auto
-        define wp where "wp \<equiv> w-p"
-        have "wp\<noteq>0" and "pp w \<noteq>0"
-          unfolding wp_def using \<open>w\<noteq>p\<close> \<open>w\<in>ball p r\<close> pp_po by auto
-        moreover have der_f':"deriv f' w = po * pp w * (w-p) powr (po - 1) + deriv pp w * (w-p) powr po"
-        proof (rule DERIV_imp_deriv)
-          have "(pp has_field_derivative (deriv pp w)) (at w)"
-            using DERIV_deriv_iff_has_field_derivative pp_holo \<open>w\<noteq>p\<close>
-            by (meson open_ball \<open>w \<in> ball p r\<close> ball_subset_cball holomorphic_derivI holomorphic_on_subset)
-          then show " (f' has_field_derivative of_int po * pp w * (w - p) powr of_int (po - 1)
-                  + deriv pp w * (w - p) powr of_int po) (at w)"
-            unfolding f'_def using \<open>w\<noteq>p\<close>
-            by (auto intro!: derivative_eq_intros DERIV_cong[OF has_field_derivative_powr_of_int])
-        qed
-        ultimately show "prin w + anal w = ff' w"
-          unfolding ff'_def prin_def anal_def
-          apply simp
-          apply (unfold f'_def)
-          apply (fold wp_def)
-          apply (auto simp add:field_simps)
-          by (metis (no_types, lifting) diff_add_cancel mult.commute powr_add powr_to_1)
-      qed
-      then have "cont ff p e2" unfolding cont_def
-      proof (elim has_contour_integral_eq)
-        fix w assume "w \<in> path_image (circlepath p e2)"
-        then have "w\<in>ball p r" and "w\<noteq>p" unfolding e2_def using \<open>r>0\<close> by auto
-        have "deriv f' w =  deriv f w"
-        proof (rule complex_derivative_transform_within_open[where s="ball p r - {p}"])
-          show "f' holomorphic_on ball p r - {p}" unfolding f'_def using pp_holo
-            by (auto intro!: holomorphic_intros)
-        next
-          have "ball p e1 - {p} \<subseteq> s - poles"
-            using ball_subset_cball e1_avoid[unfolded avoid_def] unfolding pz_def
-            by auto
-          then have "ball p r - {p} \<subseteq> s - poles"
-            apply (elim dual_order.trans)
-            using \<open>r<e1\<close> by auto
-          then show "f holomorphic_on ball p r - {p}" using f_holo
-            by auto
-        next
-          show "open (ball p r - {p})" by auto
-          show "w \<in> ball p r - {p}" using \<open>w\<in>ball p r\<close> \<open>w\<noteq>p\<close> by auto
-        next
-          fix x assume "x \<in> ball p r - {p}"
-          then show "f' x = f x"
-            using pp_po unfolding f'_def by auto
-        qed
-        moreover have " f' w  =  f w "
-          using \<open>w \<in> ball p r\<close> ball_subset_cball subset_iff pp_po \<open>w\<noteq>p\<close>
-          unfolding f'_def by auto
-        ultimately show "ff' w = ff w"
-          unfolding ff'_def ff_def by simp
-      qed
-      moreover have "cball p e2 \<subseteq> ball p e1"
-        using \<open>0 < r\<close> \<open>r<e1\<close> e2_def by auto
-      ultimately show ?thesis using \<open>e2>0\<close> by auto
-    qed
-    then obtain e2 where e2:"p\<in>pz \<longrightarrow> e2>0 \<and> cball p e2 \<subseteq> ball p e1 \<and> cont ff p e2"
-      by auto
-    define e4 where "e4 \<equiv> if p\<in>pz then e2 else  e1"
-    have "e4>0" using e2 \<open>e1>0\<close> unfolding e4_def by auto
-    moreover have "avoid p e4" using e2 \<open>e1>0\<close> e1_avoid unfolding e4_def avoid_def by auto
-    moreover have "p\<in>pz \<longrightarrow> cont ff p e4"
-      by (auto simp add: e2 e4_def)
-    ultimately show ?thesis by auto
-  qed
-  then obtain get_e where get_e:"\<forall>p\<in>s. get_e p>0 \<and> avoid p (get_e p)
-      \<and> (p\<in>pz \<longrightarrow> cont ff p (get_e p))"
-    by metis
-  define ci where "ci \<equiv> \<lambda>p. contour_integral (circlepath p (get_e p)) ff"
-  define w where "w \<equiv> \<lambda>p. winding_number g p"
-  have "contour_integral g ff = (\<Sum>p\<in>pz. w p * ci p)" unfolding ci_def w_def
-  proof (rule Cauchy_theorem_singularities[OF \<open>open s\<close> \<open>connected s\<close> finite _ \<open>valid_path g\<close> loop
-        path_img homo])
-    have "open (s - pz)" using open_Diff[OF _ finite_imp_closed[OF finite]] \<open>open s\<close> by auto
-    then show "ff holomorphic_on s - pz" unfolding ff_def using f_holo h_holo
-      by (auto intro!: holomorphic_intros simp add:pz_def)
-  next
-    show "\<forall>p\<in>s. 0 < get_e p \<and> (\<forall>w\<in>cball p (get_e p). w \<in> s \<and> (w \<noteq> p \<longrightarrow> w \<notin> pz))"
-      using get_e using avoid_def by blast
-  qed
-  also have "... = (\<Sum>p\<in>pz. c * w p * h p * zorder f p)"
-  proof (rule sum.cong[of pz pz,simplified])
-    fix p assume "p \<in> pz"
-    show "w p * ci p = c * w p * h p * (zorder f p)"
-    proof (cases "p\<in>s")
-      assume "p \<in> s"
-      have "ci p = c * h p * (zorder f p)" unfolding ci_def
-        apply (rule contour_integral_unique)
-        using get_e \<open>p\<in>s\<close> \<open>p\<in>pz\<close> unfolding cont_def by (metis mult.assoc mult.commute)
-      thus ?thesis by auto
-    next
-      assume "p\<notin>s"
-      then have "w p=0" using homo unfolding w_def by auto
-      then show ?thesis by auto
-    qed
-  qed
-  also have "... = c*(\<Sum>p\<in>pz. w p * h p * zorder f p)"
-    unfolding sum_distrib_left by (simp add:algebra_simps)
-  finally have "contour_integral g ff = c * (\<Sum>p\<in>pz. w p * h p * of_int (zorder f p))" .
-  then show ?thesis unfolding ff_def c_def w_def by simp
-qed
-
-subsection \<open>Rouche's theorem \<close>
-
-theorem Rouche_theorem:
-  fixes f g::"complex \<Rightarrow> complex" and s:: "complex set"
-  defines "fg\<equiv>(\<lambda>p. f p + g p)"
-  defines "zeros_fg\<equiv>{p. fg p = 0}" and "zeros_f\<equiv>{p. f p = 0}"
-  assumes
-    "open s" and "connected s" and
-    "finite zeros_fg" and
-    "finite zeros_f" and
-    f_holo:"f holomorphic_on s" and
-    g_holo:"g holomorphic_on s" and
-    "valid_path \<gamma>" and
-    loop:"pathfinish \<gamma> = pathstart \<gamma>" and
-    path_img:"path_image \<gamma> \<subseteq> s " and
-    path_less:"\<forall>z\<in>path_image \<gamma>. cmod(f z) > cmod(g z)" and
-    homo:"\<forall>z. (z \<notin> s) \<longrightarrow> winding_number \<gamma> z = 0"
-  shows "(\<Sum>p\<in>zeros_fg. winding_number \<gamma> p * zorder fg p)
-          = (\<Sum>p\<in>zeros_f. winding_number \<gamma> p * zorder f p)"
-proof -
-  have path_fg:"path_image \<gamma> \<subseteq> s - zeros_fg"
-  proof -
-    have False when "z\<in>path_image \<gamma>" and "f z + g z=0" for z
-    proof -
-      have "cmod (f z) > cmod (g z)" using \<open>z\<in>path_image \<gamma>\<close> path_less by auto
-      moreover have "f z = - g z"  using \<open>f z + g z =0\<close> by (simp add: eq_neg_iff_add_eq_0)
-      then have "cmod (f z) = cmod (g z)" by auto
-      ultimately show False by auto
-    qed
-    then show ?thesis unfolding zeros_fg_def fg_def using path_img by auto
-  qed
-  have path_f:"path_image \<gamma> \<subseteq> s - zeros_f"
-  proof -
-    have False when "z\<in>path_image \<gamma>" and "f z =0" for z
-    proof -
-      have "cmod (g z) < cmod (f z) " using \<open>z\<in>path_image \<gamma>\<close> path_less by auto
-      then have "cmod (g z) < 0" using \<open>f z=0\<close> by auto
-      then show False by auto
-    qed
-    then show ?thesis unfolding zeros_f_def using path_img by auto
-  qed
-  define w where "w \<equiv> \<lambda>p. winding_number \<gamma> p"
-  define c where "c \<equiv> 2 * complex_of_real pi * \<i>"
-  define h where "h \<equiv> \<lambda>p. g p / f p + 1"
-  obtain spikes
-    where "finite spikes" and spikes: "\<forall>x\<in>{0..1} - spikes. \<gamma> differentiable at x"
-    using \<open>valid_path \<gamma>\<close>
-    by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
-  have h_contour:"((\<lambda>x. deriv h x / h x) has_contour_integral 0) \<gamma>"
-  proof -
-    have outside_img:"0 \<in> outside (path_image (h o \<gamma>))"
-    proof -
-      have "h p \<in> ball 1 1" when "p\<in>path_image \<gamma>" for p
-      proof -
-        have "cmod (g p/f p) <1" using path_less[rule_format,OF that]
-          apply (cases "cmod (f p) = 0")
-          by (auto simp add: norm_divide)
-        then show ?thesis unfolding h_def by (auto simp add:dist_complex_def)
-      qed
-      then have "path_image (h o \<gamma>) \<subseteq> ball 1 1"
-        by (simp add: image_subset_iff path_image_compose)
-      moreover have " (0::complex) \<notin> ball 1 1" by (simp add: dist_norm)
-      ultimately show "?thesis"
-        using  convex_in_outside[of "ball 1 1" 0] outside_mono by blast
-    qed
-    have valid_h:"valid_path (h \<circ> \<gamma>)"
-    proof (rule valid_path_compose_holomorphic[OF \<open>valid_path \<gamma>\<close> _ _ path_f])
-      show "h holomorphic_on s - zeros_f"
-        unfolding h_def using f_holo g_holo
-        by (auto intro!: holomorphic_intros simp add:zeros_f_def)
-    next
-      show "open (s - zeros_f)" using \<open>finite zeros_f\<close> \<open>open s\<close> finite_imp_closed
-        by auto
-    qed
-    have "((\<lambda>z. 1/z) has_contour_integral 0) (h \<circ> \<gamma>)"
-    proof -
-      have "0 \<notin> path_image (h \<circ> \<gamma>)" using outside_img by (simp add: outside_def)
-      then have "((\<lambda>z. 1/z) has_contour_integral c * winding_number (h \<circ> \<gamma>) 0) (h \<circ> \<gamma>)"
-        using has_contour_integral_winding_number[of "h o \<gamma>" 0,simplified] valid_h
-        unfolding c_def by auto
-      moreover have "winding_number (h o \<gamma>) 0 = 0"
-      proof -
-        have "0 \<in> outside (path_image (h \<circ> \<gamma>))" using outside_img .
-        moreover have "path (h o \<gamma>)"
-          using valid_h  by (simp add: valid_path_imp_path)
-        moreover have "pathfinish (h o \<gamma>) = pathstart (h o \<gamma>)"
-          by (simp add: loop pathfinish_compose pathstart_compose)
-        ultimately show ?thesis using winding_number_zero_in_outside by auto
-      qed
-      ultimately show ?thesis by auto
-    qed
-    moreover have "vector_derivative (h \<circ> \<gamma>) (at x) = vector_derivative \<gamma> (at x) * deriv h (\<gamma> x)"
-      when "x\<in>{0..1} - spikes" for x
-    proof (rule vector_derivative_chain_at_general)
-      show "\<gamma> differentiable at x" using that \<open>valid_path \<gamma>\<close> spikes by auto
-    next
-      define der where "der \<equiv> \<lambda>p. (deriv g p * f p - g p * deriv f p)/(f p * f p)"
-      define t where "t \<equiv> \<gamma> x"
-      have "f t\<noteq>0" unfolding zeros_f_def t_def
-        by (metis DiffD1 image_eqI norm_not_less_zero norm_zero path_defs(4) path_less that)
-      moreover have "t\<in>s"
-        using contra_subsetD path_image_def path_fg t_def that by fastforce
-      ultimately have "(h has_field_derivative der t) (at t)"
-        unfolding h_def der_def using g_holo f_holo \<open>open s\<close>
-        by (auto intro!: holomorphic_derivI derivative_eq_intros)
-      then show "h field_differentiable at (\<gamma> x)"
-        unfolding t_def field_differentiable_def by blast
-    qed
-    then have " ((/) 1 has_contour_integral 0) (h \<circ> \<gamma>)
-                  = ((\<lambda>x. deriv h x / h x) has_contour_integral 0) \<gamma>"
-      unfolding has_contour_integral
-      apply (intro has_integral_spike_eq[OF negligible_finite, OF \<open>finite spikes\<close>])
-      by auto
-    ultimately show ?thesis by auto
-  qed
-  then have "contour_integral \<gamma> (\<lambda>x. deriv h x / h x) = 0"
-    using  contour_integral_unique by simp
-  moreover have "contour_integral \<gamma> (\<lambda>x. deriv fg x / fg x) = contour_integral \<gamma> (\<lambda>x. deriv f x / f x)
-      + contour_integral \<gamma> (\<lambda>p. deriv h p / h p)"
-  proof -
-    have "(\<lambda>p. deriv f p / f p) contour_integrable_on \<gamma>"
-    proof (rule contour_integrable_holomorphic_simple[OF _ _ \<open>valid_path \<gamma>\<close> path_f])
-      show "open (s - zeros_f)" using finite_imp_closed[OF \<open>finite zeros_f\<close>] \<open>open s\<close>
-        by auto
-      then show "(\<lambda>p. deriv f p / f p) holomorphic_on s - zeros_f"
-        using f_holo
-        by (auto intro!: holomorphic_intros simp add:zeros_f_def)
-    qed
-    moreover have "(\<lambda>p. deriv h p / h p) contour_integrable_on \<gamma>"
-      using h_contour
-      by (simp add: has_contour_integral_integrable)
-    ultimately have "contour_integral \<gamma> (\<lambda>x. deriv f x / f x + deriv h x / h x) =
-                        contour_integral \<gamma> (\<lambda>p. deriv f p / f p) + contour_integral \<gamma> (\<lambda>p. deriv h p / h p)"
-      using contour_integral_add[of "(\<lambda>p. deriv f p / f p)" \<gamma> "(\<lambda>p. deriv h p / h p)" ]
-      by auto
-    moreover have "deriv fg p / fg p =  deriv f p / f p + deriv h p / h p"
-                      when "p\<in> path_image \<gamma>" for p
-    proof -
-      have "fg p\<noteq>0" and "f p\<noteq>0" using path_f path_fg that unfolding zeros_f_def zeros_fg_def
-        by auto
-      have "h p\<noteq>0"
-      proof (rule ccontr)
-        assume "\<not> h p \<noteq> 0"
-        then have "g p / f p= -1" unfolding h_def by (simp add: add_eq_0_iff2)
-        then have "cmod (g p/f p) = 1" by auto
-        moreover have "cmod (g p/f p) <1" using path_less[rule_format,OF that]
-          apply (cases "cmod (f p) = 0")
-          by (auto simp add: norm_divide)
-        ultimately show False by auto
-      qed
-      have der_fg:"deriv fg p =  deriv f p + deriv g p" unfolding fg_def
-        using f_holo g_holo holomorphic_on_imp_differentiable_at[OF _  \<open>open s\<close>] path_img that
-        by auto
-      have der_h:"deriv h p = (deriv g p * f p - g p * deriv f p)/(f p * f p)"
-      proof -
-        define der where "der \<equiv> \<lambda>p. (deriv g p * f p - g p * deriv f p)/(f p * f p)"
-        have "p\<in>s" using path_img that by auto
-        then have "(h has_field_derivative der p) (at p)"
-          unfolding h_def der_def using g_holo f_holo \<open>open s\<close> \<open>f p\<noteq>0\<close>
-          by (auto intro!: derivative_eq_intros holomorphic_derivI)
-        then show ?thesis unfolding der_def using DERIV_imp_deriv by auto
-      qed
-      show ?thesis
-        apply (simp only:der_fg der_h)
-        apply (auto simp add:field_simps \<open>h p\<noteq>0\<close> \<open>f p\<noteq>0\<close> \<open>fg p\<noteq>0\<close>)
-        by (auto simp add:field_simps h_def \<open>f p\<noteq>0\<close> fg_def)
-    qed
-    then have "contour_integral \<gamma> (\<lambda>p. deriv fg p / fg p)
-                  = contour_integral \<gamma> (\<lambda>p. deriv f p / f p + deriv h p / h p)"
-      by (elim contour_integral_eq)
-    ultimately show ?thesis by auto
-  qed
-  moreover have "contour_integral \<gamma> (\<lambda>x. deriv fg x / fg x) = c * (\<Sum>p\<in>zeros_fg. w p * zorder fg p)"
-    unfolding c_def zeros_fg_def w_def
-  proof (rule argument_principle[OF \<open>open s\<close> \<open>connected s\<close> _ _ \<open>valid_path \<gamma>\<close> loop _ homo
-        , of _ "{}" "\<lambda>_. 1",simplified])
-    show "fg holomorphic_on s" unfolding fg_def using f_holo g_holo holomorphic_on_add by auto
-    show "path_image \<gamma> \<subseteq> s - {p. fg p = 0}" using path_fg unfolding zeros_fg_def .
-    show " finite {p. fg p = 0}" using \<open>finite zeros_fg\<close> unfolding zeros_fg_def .
-  qed
-  moreover have "contour_integral \<gamma> (\<lambda>x. deriv f x / f x) = c * (\<Sum>p\<in>zeros_f. w p * zorder f p)"
-    unfolding c_def zeros_f_def w_def
-  proof (rule argument_principle[OF \<open>open s\<close> \<open>connected s\<close> _ _ \<open>valid_path \<gamma>\<close> loop _ homo
-        , of _ "{}" "\<lambda>_. 1",simplified])
-    show "f holomorphic_on s" using f_holo g_holo holomorphic_on_add by auto
-    show "path_image \<gamma> \<subseteq> s - {p. f p = 0}" using path_f unfolding zeros_f_def .
-    show " finite {p. f p = 0}" using \<open>finite zeros_f\<close> unfolding zeros_f_def .
-  qed
-  ultimately have " c* (\<Sum>p\<in>zeros_fg. w p * (zorder fg p)) = c* (\<Sum>p\<in>zeros_f. w p * (zorder f p))"
-    by auto
-  then show ?thesis unfolding c_def using w_def by auto
-qed
-
-end
--- a/src/HOL/Analysis/Derivative.thy	Wed Nov 27 16:54:33 2019 +0000
+++ b/src/HOL/Analysis/Derivative.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -9,6 +9,7 @@
   imports
     Bounded_Linear_Function
     Line_Segment
+    Convex_Euclidean_Space
 begin
 
 declare bounded_linear_inner_left [intro]
@@ -2297,6 +2298,86 @@
   apply (rule vector_derivative_at [OF field_vector_diff_chain_at])
   using assms vector_derivative_works by (auto simp: field_differentiable_derivI)
 
+lemma DERIV_deriv_iff_field_differentiable:
+  "DERIV f x :> deriv f x \<longleftrightarrow> f field_differentiable at x"
+  unfolding field_differentiable_def by (metis DERIV_imp_deriv)
+
+lemma deriv_chain:
+  "f field_differentiable at x \<Longrightarrow> g field_differentiable at (f x)
+    \<Longrightarrow> deriv (g o f) x = deriv g (f x) * deriv f x"
+  by (metis DERIV_deriv_iff_field_differentiable DERIV_chain DERIV_imp_deriv)
+
+lemma deriv_linear [simp]: "deriv (\<lambda>w. c * w) = (\<lambda>z. c)"
+  by (metis DERIV_imp_deriv DERIV_cmult_Id)
+
+lemma deriv_uminus [simp]: "deriv (\<lambda>w. -w) = (\<lambda>z. -1)"
+  using deriv_linear[of "-1"] by (simp del: deriv_linear)
+
+lemma deriv_ident [simp]: "deriv (\<lambda>w. w) = (\<lambda>z. 1)"
+  by (metis DERIV_imp_deriv DERIV_ident)
+
+lemma deriv_id [simp]: "deriv id = (\<lambda>z. 1)"
+  by (simp add: id_def)
+
+lemma deriv_const [simp]: "deriv (\<lambda>w. c) = (\<lambda>z. 0)"
+  by (metis DERIV_imp_deriv DERIV_const)
+
+lemma deriv_add [simp]:
+  "\<lbrakk>f field_differentiable at z; g field_differentiable at z\<rbrakk>
+   \<Longrightarrow> deriv (\<lambda>w. f w + g w) z = deriv f z + deriv g z"
+  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
+  by (auto intro!: DERIV_imp_deriv derivative_intros)
+
+lemma deriv_diff [simp]:
+  "\<lbrakk>f field_differentiable at z; g field_differentiable at z\<rbrakk>
+   \<Longrightarrow> deriv (\<lambda>w. f w - g w) z = deriv f z - deriv g z"
+  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
+  by (auto intro!: DERIV_imp_deriv derivative_intros)
+
+lemma deriv_mult [simp]:
+  "\<lbrakk>f field_differentiable at z; g field_differentiable at z\<rbrakk>
+   \<Longrightarrow> deriv (\<lambda>w. f w * g w) z = f z * deriv g z + deriv f z * g z"
+  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
+  by (auto intro!: DERIV_imp_deriv derivative_eq_intros)
+
+lemma deriv_cmult:
+  "f field_differentiable at z \<Longrightarrow> deriv (\<lambda>w. c * f w) z = c * deriv f z"
+  by simp
+
+lemma deriv_cmult_right:
+  "f field_differentiable at z \<Longrightarrow> deriv (\<lambda>w. f w * c) z = deriv f z * c"
+  by simp
+
+lemma deriv_inverse [simp]:
+  "\<lbrakk>f field_differentiable at z; f z \<noteq> 0\<rbrakk>
+   \<Longrightarrow> deriv (\<lambda>w. inverse (f w)) z = - deriv f z / f z ^ 2"
+  unfolding DERIV_deriv_iff_field_differentiable[symmetric]
+  by (safe intro!: DERIV_imp_deriv derivative_eq_intros) (auto simp: field_split_simps power2_eq_square)
+
+lemma deriv_divide [simp]:
+  "\<lbrakk>f field_differentiable at z; g field_differentiable at z; g z \<noteq> 0\<rbrakk>
+   \<Longrightarrow> deriv (\<lambda>w. f w / g w) z = (deriv f z * g z - f z * deriv g z) / g z ^ 2"
+  by (simp add: field_class.field_divide_inverse field_differentiable_inverse)
+     (simp add: field_split_simps power2_eq_square)
+
+lemma deriv_cdivide_right:
+  "f field_differentiable at z \<Longrightarrow> deriv (\<lambda>w. f w / c) z = deriv f z / c"
+  by (simp add: field_class.field_divide_inverse)
+
+lemma deriv_compose_linear:
+  "f field_differentiable at (c * z) \<Longrightarrow> deriv (\<lambda>w. f (c * w)) z = c * deriv f (c * z)"
+apply (rule DERIV_imp_deriv)
+  unfolding DERIV_deriv_iff_field_differentiable [symmetric]
+  by (metis (full_types) DERIV_chain2 DERIV_cmult_Id mult.commute)
+
+
+lemma nonzero_deriv_nonconstant:
+  assumes df: "DERIV f \<xi> :> df" and S: "open S" "\<xi> \<in> S" and "df \<noteq> 0"
+    shows "\<not> f constant_on S"
+unfolding constant_on_def
+by (metis \<open>df \<noteq> 0\<close> has_field_derivative_transform_within_open [OF df S] DERIV_const DERIV_unique)
+
+
 subsection \<open>Relation between convexity and derivative\<close>
 
 (* TODO: Generalise to real vector spaces? *)
@@ -2959,4 +3040,436 @@
   qed auto
 qed
 
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Piecewise differentiable functions\<close>
+
+definition piecewise_differentiable_on
+           (infixr "piecewise'_differentiable'_on" 50)
+  where "f piecewise_differentiable_on i  \<equiv>
+           continuous_on i f \<and>
+           (\<exists>S. finite S \<and> (\<forall>x \<in> i - S. f differentiable (at x within i)))"
+
+lemma piecewise_differentiable_on_imp_continuous_on:
+    "f piecewise_differentiable_on S \<Longrightarrow> continuous_on S f"
+by (simp add: piecewise_differentiable_on_def)
+
+lemma piecewise_differentiable_on_subset:
+    "f piecewise_differentiable_on S \<Longrightarrow> T \<le> S \<Longrightarrow> f piecewise_differentiable_on T"
+  using continuous_on_subset
+  unfolding piecewise_differentiable_on_def
+  apply safe
+  apply (blast 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} \<Longrightarrow> 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:
+    "(\<And>x. x \<in> S \<Longrightarrow> f differentiable (at x within S))
+         \<Longrightarrow> 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]: "(\<lambda>x. z) piecewise_differentiable_on S"
+  by (simp add: differentiable_imp_piecewise_differentiable)
+
+lemma piecewise_differentiable_compose:
+    "\<lbrakk>f piecewise_differentiable_on S; g piecewise_differentiable_on (f ` S);
+      \<And>x. finite (S \<inter> f-`{x})\<rbrakk>
+      \<Longrightarrow> (g \<circ> 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 \<union> (\<Union>x\<in>B. S \<inter> f-`{x})" in exI)
+  apply (blast intro!: differentiable_chain_within)
+  done
+
+lemma piecewise_differentiable_affine:
+  fixes m::real
+  assumes "f piecewise_differentiable_on ((\<lambda>x. m *\<^sub>R x + c) ` S)"
+  shows "(f \<circ> (\<lambda>x. m *\<^sub>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 \<le> c" "c \<le> b" "f c = g c"
+  shows "(\<lambda>x. if x \<le> c then f x else g x) piecewise_differentiable_on {a..b}"
+proof -
+  obtain S T where st: "finite S" "finite T"
+               and fd: "\<And>x. x \<in> {a..c} - S \<Longrightarrow> f differentiable at x within {a..c}"
+               and gd: "\<And>x. x \<in> {c..b} - T \<Longrightarrow> g differentiable at x within {c..b}"
+    using assms
+    by (auto simp: piecewise_differentiable_on_def)
+  have finabc: "finite ({a,b,c} \<union> (S \<union> T))"
+    by (metis \<open>finite S\<close> \<open>finite T\<close> 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} (\<lambda>x. if x \<le> 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 "\<lambda>x. x\<le>c"]  assms
+    by (force simp: ivl_disj_un_two_touch)
+  moreover
+  { fix x
+    assume x: "x \<in> {a..b} - ({a,b,c} \<union> (S \<union> T))"
+    have "(\<lambda>x. if x \<le> 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
+      proof (rule differentiable_transform_within [where d = "dist x c"])
+        have "f differentiable at x"
+          using x le fd [of x] at_within_interior [of x "{a..c}"] by simp
+        then show "f differentiable at x within {a..b}"
+          by (simp add: differentiable_at_withinI)
+      qed (use x le st dist_real_def in auto)
+    next
+      case ge show ?diff_fg
+      proof (rule differentiable_transform_within [where d = "dist x c"])
+        have "g differentiable at x"
+          using x ge gd [of x] at_within_interior [of x "{c..b}"] by simp
+        then show "g differentiable at x within {a..b}"
+          by (simp add: differentiable_at_withinI)
+      qed (use x ge st dist_real_def in auto)
+    qed
+  }
+  then have "\<exists>S. finite S \<and>
+                 (\<forall>x\<in>{a..b} - S. (\<lambda>x. if x \<le> 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 \<Longrightarrow> (\<lambda>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 "(\<lambda>x. f x + g x) piecewise_differentiable_on i"
+proof -
+  obtain S T where st: "finite S" "finite T"
+                       "\<forall>x\<in>i - S. f differentiable at x within i"
+                       "\<forall>x\<in>i - T. g differentiable at x within i"
+    using assms by (auto simp: piecewise_differentiable_on_def)
+  then have "finite (S \<union> T) \<and> (\<forall>x\<in>i - (S \<union> T). (\<lambda>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:
+    "\<lbrakk>f piecewise_differentiable_on S;  g piecewise_differentiable_on S\<rbrakk>
+     \<Longrightarrow> (\<lambda>x. f x - g x) piecewise_differentiable_on S"
+  unfolding diff_conv_add_uminus
+  by (metis piecewise_differentiable_add piecewise_differentiable_neg)
+
+
+subsection\<open>The concept of continuously differentiable\<close>
+
+text \<open>
+John Harrison writes as follows:
+
+``The usual assumption in complex analysis texts is that a path \<open>\<gamma>\<close> 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\ldots{} [namely] continuity plus differentiability except on a
+finite set\ldots{} [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.\<close>
+
+definition\<^marker>\<open>tag important\<close> C1_differentiable_on :: "(real \<Rightarrow> 'a::real_normed_vector) \<Rightarrow> real set \<Rightarrow> bool"
+           (infix "C1'_differentiable'_on" 50)
+  where
+  "f C1_differentiable_on S \<longleftrightarrow>
+   (\<exists>D. (\<forall>x \<in> S. (f has_vector_derivative (D x)) (at x)) \<and> continuous_on S D)"
+
+lemma C1_differentiable_on_eq:
+    "f C1_differentiable_on S \<longleftrightarrow>
+     (\<forall>x \<in> S. f differentiable at x) \<and> continuous_on S (\<lambda>x. vector_derivative f (at x))"
+     (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  then show ?rhs
+    unfolding C1_differentiable_on_def
+    by (metis (no_types, lifting) continuous_on_eq  differentiableI_vector vector_derivative_at)
+next
+  assume ?rhs
+  then show ?lhs
+    using C1_differentiable_on_def vector_derivative_works by fastforce
+qed
+
+lemma C1_differentiable_on_subset:
+  "f C1_differentiable_on T \<Longrightarrow> S \<subseteq> T \<Longrightarrow> 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:
+  assumes fg: "f C1_differentiable_on S" "g C1_differentiable_on (f ` S)" and fin: "\<And>x. finite (S \<inter> f-`{x})"
+  shows "(g \<circ> f) C1_differentiable_on S"
+proof -
+  have "\<And>x. x \<in> S \<Longrightarrow> g \<circ> f differentiable at x"
+    by (meson C1_differentiable_on_eq assms differentiable_chain_at imageI)
+  moreover have "continuous_on S (\<lambda>x. vector_derivative (g \<circ> f) (at x))"
+  proof (rule continuous_on_eq [of _ "\<lambda>x. vector_derivative f (at x) *\<^sub>R vector_derivative g (at (f x))"])
+    show "continuous_on S (\<lambda>x. vector_derivative f (at x) *\<^sub>R vector_derivative g (at (f x)))"
+      using fg
+      apply (clarsimp simp add: C1_differentiable_on_eq)
+      apply (rule Limits.continuous_on_scaleR, assumption)
+      by (metis (mono_tags, lifting) continuous_at_imp_continuous_on continuous_on_compose continuous_on_cong differentiable_imp_continuous_within o_def)
+    show "\<And>x. x \<in> S \<Longrightarrow> vector_derivative f (at x) *\<^sub>R vector_derivative g (at (f x)) = vector_derivative (g \<circ> f) (at x)"
+      by (metis (mono_tags, hide_lams) C1_differentiable_on_eq fg imageI vector_derivative_chain_at)
+  qed
+  ultimately show ?thesis
+    by (simp add: C1_differentiable_on_eq)
+qed
+
+lemma C1_diff_imp_diff: "f C1_differentiable_on S \<Longrightarrow> f differentiable_on S"
+  by (simp add: C1_differentiable_on_eq differentiable_at_imp_differentiable_on)
+
+lemma C1_differentiable_on_ident [simp, derivative_intros]: "(\<lambda>x. x) C1_differentiable_on S"
+  by (auto simp: C1_differentiable_on_eq)
+
+lemma C1_differentiable_on_const [simp, derivative_intros]: "(\<lambda>z. a) C1_differentiable_on S"
+  by (auto simp: C1_differentiable_on_eq)
+
+lemma C1_differentiable_on_add [simp, derivative_intros]:
+  "f C1_differentiable_on S \<Longrightarrow> g C1_differentiable_on S \<Longrightarrow> (\<lambda>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 \<Longrightarrow> (\<lambda>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 \<Longrightarrow> g C1_differentiable_on S \<Longrightarrow> (\<lambda>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 \<Rightarrow> 'a :: real_normed_algebra"
+  shows "f C1_differentiable_on S \<Longrightarrow> g C1_differentiable_on S \<Longrightarrow> (\<lambda>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 \<Longrightarrow> g C1_differentiable_on S \<Longrightarrow> (\<lambda>x. f x *\<^sub>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\<^marker>\<open>tag important\<close> piecewise_C1_differentiable_on
+           (infixr "piecewise'_C1'_differentiable'_on" 50)
+  where "f piecewise_C1_differentiable_on i  \<equiv>
+           continuous_on i f \<and>
+           (\<exists>S. finite S \<and> (f C1_differentiable_on (i - S)))"
+
+lemma C1_differentiable_imp_piecewise:
+    "f C1_differentiable_on S \<Longrightarrow> 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 \<Longrightarrow> 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_withinI)
+
+lemma piecewise_C1_differentiable_compose:
+  assumes fg: "f piecewise_C1_differentiable_on S" "g piecewise_C1_differentiable_on (f ` S)" and fin: "\<And>x. finite (S \<inter> f-`{x})"
+  shows "(g \<circ> f) piecewise_C1_differentiable_on S"
+proof -
+  have "continuous_on S (\<lambda>x. g (f x))"
+    by (metis continuous_on_compose2 fg order_refl piecewise_C1_differentiable_on_def)
+  moreover have "\<exists>T. finite T \<and> g \<circ> f C1_differentiable_on S - T"
+  proof -
+    obtain F where "finite F" and F: "f C1_differentiable_on S - F" and f: "f piecewise_C1_differentiable_on S"
+      using fg by (auto simp: piecewise_C1_differentiable_on_def)
+    obtain G where "finite G" and G: "g C1_differentiable_on f ` S - G" and g: "g piecewise_C1_differentiable_on f ` S"
+      using fg by (auto simp: piecewise_C1_differentiable_on_def)
+    show ?thesis
+    proof (intro exI conjI)
+      show "finite (F \<union> (\<Union>x\<in>G. S \<inter> f-`{x}))"
+        using fin by (auto simp only: Int_Union \<open>finite F\<close> \<open>finite G\<close> finite_UN finite_imageI)
+      show "g \<circ> f C1_differentiable_on S - (F \<union> (\<Union>x\<in>G. S \<inter> f -` {x}))"
+        apply (rule C1_differentiable_compose)
+          apply (blast intro: C1_differentiable_on_subset [OF F])
+          apply (blast intro: C1_differentiable_on_subset [OF G])
+        by (simp add:  C1_differentiable_on_subset G Diff_Int_distrib2 fin)
+    qed
+  qed
+  ultimately show ?thesis
+    by (simp add: piecewise_C1_differentiable_on_def)
+qed
+
+lemma piecewise_C1_differentiable_on_subset:
+    "f piecewise_C1_differentiable_on S \<Longrightarrow> T \<le> S \<Longrightarrow> 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 \<Longrightarrow> 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 ((\<lambda>x. m * x + c) ` S)"
+  shows "(f \<circ> (\<lambda>x. m *\<^sub>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)
+next
+  case False
+  have *: "\<And>x. finite (S \<inter> {y. m * y + c = x})"
+    using False not_finite_existsD by fastforce
+  show ?thesis
+    apply (rule piecewise_C1_differentiable_compose [OF C1_differentiable_imp_piecewise])
+    apply (rule * assms derivative_intros | simp add: False vimage_def)+
+    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 \<le> c" "c \<le> b" "f c = g c"
+  shows "(\<lambda>x. if x \<le> 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} (\<lambda>x. if x \<le> 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 "\<lambda>x. x\<le>c"]  assms
+    by (force simp: ivl_disj_un_two_touch)
+  { fix x
+    assume x: "x \<in> {a..b} - insert c (S \<union> T)"
+    have "(\<lambda>x. if x \<le> 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 "(\<forall>x \<in> {a..b} - insert c (S \<union> T). (\<lambda>x. if x \<le> c then f x else g x) differentiable at x)"
+    by auto
+  moreover
+  { assume fcon: "continuous_on ({a<..<c} - S) (\<lambda>x. vector_derivative f (at x))"
+       and gcon: "continuous_on ({c<..<b} - T) (\<lambda>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) (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
+    proof -
+      have "((\<lambda>x. if x \<le> c then f x else g x) has_vector_derivative vector_derivative f (at x))            (at x)"
+        if "a < x" "x < c" "x \<notin> S" for x
+      proof -
+        have f: "f differentiable at x"
+          by (meson C1_differentiable_on_eq Diff_iff atLeastAtMost_iff less_eq_real_def st(1) that)
+        show ?thesis
+          using that
+          apply (rule_tac f=f and d="dist x c" in has_vector_derivative_transform_within)
+             apply (auto simp: dist_norm vector_derivative_works [symmetric] f)
+          done
+      qed
+      then show ?thesis
+        by (metis (no_types, lifting) continuous_on_eq [OF fcon] DiffE greaterThanLessThan_iff vector_derivative_at)
+    qed
+    moreover have "continuous_on ({c<..<b} - T) (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
+    proof -
+      have "((\<lambda>x. if x \<le> c then f x else g x) has_vector_derivative vector_derivative g (at x))            (at x)"
+        if "c < x" "x < b" "x \<notin> T" for x
+      proof -
+        have g: "g differentiable at x"
+          by (metis C1_differentiable_on_eq DiffD1 DiffI atLeastAtMost_diff_ends greaterThanLessThan_iff st(2) that)
+        show ?thesis
+          using that
+          apply (rule_tac f=g and d="dist x c" in has_vector_derivative_transform_within)
+             apply (auto simp: dist_norm vector_derivative_works [symmetric] g)
+          done
+      qed
+      then show ?thesis
+        by (metis (no_types, lifting) continuous_on_eq [OF gcon] DiffE greaterThanLessThan_iff vector_derivative_at)
+    qed
+    ultimately have "continuous_on ({a<..<b} - insert c (S \<union> T))
+        (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
+      by (rule continuous_on_subset [OF continuous_on_open_Un], auto)
+  } note * = this
+  have "continuous_on ({a<..<b} - insert c (S \<union> T)) (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> 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 "\<exists>S. finite S \<and> ((\<lambda>x. if x \<le> c then f x else g x) C1_differentiable_on {a..b} - S)"
+    apply (rule_tac x="{a,b,c} \<union> S \<union> 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 \<Longrightarrow> (\<lambda>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 "(\<lambda>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 \<union> t) \<and> (\<lambda>x. f x + g x) C1_differentiable_on i - (S \<union> 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:
+    "\<lbrakk>f piecewise_C1_differentiable_on S;  g piecewise_C1_differentiable_on S\<rbrakk>
+     \<Longrightarrow> (\<lambda>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)
+
 end
--- a/src/HOL/Analysis/FPS_Convergence.thy	Wed Nov 27 16:54:33 2019 +0000
+++ b/src/HOL/Analysis/FPS_Convergence.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -9,13 +9,20 @@
 
 theory FPS_Convergence
 imports
-  Conformal_Mappings
   Generalised_Binomial_Theorem
   "HOL-Computational_Algebra.Formal_Power_Series"
 begin
 
+text \<open>
+  In this theory, we will connect formal power series (which are algebraic objects) with analytic
+  functions. This will become more important in complex analysis, and indeed some of the less
+  trivial results will only be proven there.
+\<close>
+
 subsection\<^marker>\<open>tag unimportant\<close> \<open>Balls with extended real radius\<close>
 
+(* TODO: This should probably go somewhere else *)
+
 text \<open>
   The following is a variant of \<^const>\<open>ball\<close> that also allows an infinite radius.
 \<close>
@@ -61,9 +68,6 @@
 definition\<^marker>\<open>tag important\<close> eval_fps :: "'a :: {banach, real_normed_div_algebra} fps \<Rightarrow> 'a \<Rightarrow> 'a" where
   "eval_fps f z = (\<Sum>n. fps_nth f n * z ^ n)"
 
-definition\<^marker>\<open>tag important\<close> fps_expansion :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> complex fps" where
-  "fps_expansion f z0 = Abs_fps (\<lambda>n. (deriv ^^ n) f z0 / fact n)"
-
 lemma norm_summable_fps:
   fixes f :: "'a :: {banach, real_normed_div_algebra} fps"
   shows "norm z < fps_conv_radius f \<Longrightarrow> summable (\<lambda>n. norm (fps_nth f n * z ^ n))"
@@ -81,38 +85,6 @@
   using assms unfolding eval_fps_def fps_conv_radius_def
   by (intro summable_sums summable_in_conv_radius) simp_all
 
-lemma
-  fixes r :: ereal
-  assumes "f holomorphic_on eball z0 r"
-  shows   conv_radius_fps_expansion: "fps_conv_radius (fps_expansion f z0) \<ge> r"
-    and   eval_fps_expansion: "\<And>z. z \<in> eball z0 r \<Longrightarrow> eval_fps (fps_expansion f z0) (z - z0) = f z"
-    and   eval_fps_expansion': "\<And>z. norm z < r \<Longrightarrow> eval_fps (fps_expansion f z0) z = f (z0 + z)"
-proof -
-  have "(\<lambda>n. fps_nth (fps_expansion f z0) n * (z - z0) ^ n) sums f z"
-    if "z \<in> ball z0 r'" "ereal r' < r" for z r'
-  proof -
-    from that(2) have "ereal r' \<le> r" by simp
-    from assms(1) and this have "f holomorphic_on ball z0 r'"
-      by (rule holomorphic_on_subset[OF _ ball_eball_mono])
-    from holomorphic_power_series [OF this that(1)] 
-      show ?thesis by (simp add: fps_expansion_def)
-  qed
-  hence *: "(\<lambda>n. fps_nth (fps_expansion f z0) n * (z - z0) ^ n) sums f z"
-    if "z \<in> eball z0 r" for z
-    using that by (subst (asm) eball_conv_UNION_balls) blast
-  show "fps_conv_radius (fps_expansion f z0) \<ge> r" unfolding fps_conv_radius_def
-  proof (rule conv_radius_geI_ex)
-    fix r' :: real assume r': "r' > 0" "ereal r' < r"
-    thus "\<exists>z. norm z = r' \<and> summable (\<lambda>n. fps_nth (fps_expansion f z0) n * z ^ n)"
-      using *[of "z0 + of_real r'"]
-      by (intro exI[of _ "of_real r'"]) (auto simp: summable_def dist_norm)
-  qed
-  show "eval_fps (fps_expansion f z0) (z - z0) = f z" if "z \<in> eball z0 r" for z
-    using *[OF that] by (simp add: eval_fps_def sums_iff)
-  show "eval_fps (fps_expansion f z0) z = f (z0 + z)" if "ereal (norm z) < r" for z
-    using *[of "z0 + z"] and that by (simp add: eval_fps_def sums_iff dist_norm)
-qed
-
 lemma continuous_on_eval_fps:
   fixes f :: "'a :: {banach, real_normed_div_algebra} fps"
   shows "continuous_on (eball 0 (fps_conv_radius f)) (eval_fps f)"
@@ -615,181 +587,14 @@
   shows "eval_fps (fps_exp c) z = exp (c * z)" unfolding eval_fps_def exp_def
   by (simp add: eval_fps_def exp_def scaleR_conv_of_real field_split_simps power_mult_distrib)
 
-lemma
-  fixes f :: "complex fps" and r :: ereal
-  assumes "\<And>z. ereal (norm z) < r \<Longrightarrow> eval_fps f z \<noteq> 0"
-  shows   fps_conv_radius_inverse: "fps_conv_radius (inverse f) \<ge> min r (fps_conv_radius f)"
-    and   eval_fps_inverse: "\<And>z. ereal (norm z) < fps_conv_radius f \<Longrightarrow> ereal (norm z) < r \<Longrightarrow> 
-                               eval_fps (inverse f) z = inverse (eval_fps f z)"
-proof -
-  define R where "R = min (fps_conv_radius f) r"
-  have *: "fps_conv_radius (inverse f) \<ge> min r (fps_conv_radius f) \<and> 
-          (\<forall>z\<in>eball 0 (min (fps_conv_radius f) r). eval_fps (inverse f) z = inverse (eval_fps f z))"
-  proof (cases "min r (fps_conv_radius f) > 0")
-    case True
-    define f' where "f' = fps_expansion (\<lambda>z. inverse (eval_fps f z)) 0"
-    have holo: "(\<lambda>z. inverse (eval_fps f z)) holomorphic_on eball 0 (min r (fps_conv_radius f))"
-      using assms by (intro holomorphic_intros) auto
-    from holo have radius: "fps_conv_radius f' \<ge> min r (fps_conv_radius f)"
-      unfolding f'_def by (rule conv_radius_fps_expansion)
-    have eval_f': "eval_fps f' z = inverse (eval_fps f z)" 
-      if "norm z < fps_conv_radius f" "norm z < r" for z
-      using that unfolding f'_def by (subst eval_fps_expansion'[OF holo]) auto
-  
-    have "f * f' = 1"
-    proof (rule eval_fps_eqD)
-      from radius and True have "0 < min (fps_conv_radius f) (fps_conv_radius f')"
-        by (auto simp: min_def split: if_splits)
-      also have "\<dots> \<le> fps_conv_radius (f * f')" by (rule fps_conv_radius_mult)
-      finally show "\<dots> > 0" .
-    next
-      from True have "R > 0" by (auto simp: R_def)
-      hence "eventually (\<lambda>z. z \<in> eball 0 R) (nhds 0)"
-        by (intro eventually_nhds_in_open) (auto simp: zero_ereal_def)
-      thus "eventually (\<lambda>z. eval_fps (f * f') z = eval_fps 1 z) (nhds 0)"
-      proof eventually_elim
-        case (elim z)
-        hence "eval_fps (f * f') z = eval_fps f z * eval_fps f' z"
-          using radius by (intro eval_fps_mult) 
-                          (auto simp: R_def min_def split: if_splits intro: less_trans)
-        also have "eval_fps f' z = inverse (eval_fps f z)"
-          using elim by (intro eval_f') (auto simp: R_def)
-        also from elim have "eval_fps f z \<noteq> 0"
-          by (intro assms) (auto simp: R_def)
-        hence "eval_fps f z * inverse (eval_fps f z) = eval_fps 1 z" 
-          by simp
-        finally show "eval_fps (f * f') z = eval_fps 1 z" .
-      qed
-    qed simp_all
-    hence "f' = inverse f"
-      by (intro fps_inverse_unique [symmetric]) (simp_all add: mult_ac)
-    with eval_f' and radius show ?thesis by simp
-  next
-    case False
-    hence *: "eball 0 R = {}" 
-      by (intro eball_empty) (auto simp: R_def min_def split: if_splits)
-    show ?thesis
-    proof safe
-      from False have "min r (fps_conv_radius f) \<le> 0"
-        by (simp add: min_def)
-      also have "0 \<le> fps_conv_radius (inverse f)"
-        by (simp add: fps_conv_radius_def conv_radius_nonneg)
-      finally show "min r (fps_conv_radius f) \<le> \<dots>" .
-    qed (unfold * [unfolded R_def], auto)
-  qed
-
-  from * show "fps_conv_radius (inverse f) \<ge> min r (fps_conv_radius f)" by blast
-  from * show "eval_fps (inverse f) z = inverse (eval_fps f z)" 
-    if "ereal (norm z) < fps_conv_radius f" "ereal (norm z) < r" for z
-    using that by auto
-qed
-
-lemma
-  fixes f g :: "complex fps" and r :: ereal
-  defines "R \<equiv> Min {r, fps_conv_radius f, fps_conv_radius g}"
-  assumes "fps_conv_radius f > 0" "fps_conv_radius g > 0" "r > 0"
-  assumes nz: "\<And>z. z \<in> eball 0 r \<Longrightarrow> eval_fps g z \<noteq> 0"
-  shows   fps_conv_radius_divide': "fps_conv_radius (f / g) \<ge> R"
-    and   eval_fps_divide':
-            "ereal (norm z) < R \<Longrightarrow> eval_fps (f / g) z = eval_fps f z / eval_fps g z"
-proof -
-  from nz[of 0] and \<open>r > 0\<close> have nz': "fps_nth g 0 \<noteq> 0" 
-    by (auto simp: eval_fps_at_0 zero_ereal_def)
-  have "R \<le> min r (fps_conv_radius g)"
-    by (auto simp: R_def intro: min.coboundedI2)
-  also have "min r (fps_conv_radius g) \<le> fps_conv_radius (inverse g)"
-    by (intro fps_conv_radius_inverse assms) (auto simp: zero_ereal_def)
-  finally have radius: "fps_conv_radius (inverse g) \<ge> R" .
-  have "R \<le> min (fps_conv_radius f) (fps_conv_radius (inverse g))"
-    by (intro radius min.boundedI) (auto simp: R_def intro: min.coboundedI1 min.coboundedI2)
-  also have "\<dots> \<le> fps_conv_radius (f * inverse g)"
-    by (rule fps_conv_radius_mult)
-  also have "f * inverse g = f / g"
-    by (intro fps_divide_unit [symmetric] nz')
-  finally show "fps_conv_radius (f / g) \<ge> R" .
-
-  assume z: "ereal (norm z) < R"
-  have "eval_fps (f * inverse g) z = eval_fps f z * eval_fps (inverse g) z"
-    using radius by (intro eval_fps_mult less_le_trans[OF z])
-                    (auto simp: R_def intro: min.coboundedI1 min.coboundedI2)
-  also have "eval_fps (inverse g) z = inverse (eval_fps g z)" using \<open>r > 0\<close>
-    by (intro eval_fps_inverse[where r = r] less_le_trans[OF z] nz)
-       (auto simp: R_def intro: min.coboundedI1 min.coboundedI2)
-  also have "f * inverse g = f / g" by fact
-  finally show "eval_fps (f / g) z = eval_fps f z / eval_fps g z" by (simp add: field_split_simps)
-qed
-
-lemma
-  fixes f g :: "complex fps" and r :: ereal
-  defines "R \<equiv> Min {r, fps_conv_radius f, fps_conv_radius g}"
-  assumes "subdegree g \<le> subdegree f"
-  assumes "fps_conv_radius f > 0" "fps_conv_radius g > 0" "r > 0"
-  assumes "\<And>z. z \<in> eball 0 r \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> eval_fps g z \<noteq> 0"
-  shows   fps_conv_radius_divide: "fps_conv_radius (f / g) \<ge> R"
-    and   eval_fps_divide:
-            "ereal (norm z) < R \<Longrightarrow> c = fps_nth f (subdegree g) / fps_nth g (subdegree g) \<Longrightarrow>
-               eval_fps (f / g) z = (if z = 0 then c else eval_fps f z / eval_fps g z)"
-proof -
-  define f' g' where "f' = fps_shift (subdegree g) f" and "g' = fps_shift (subdegree g) g"
-  have f_eq: "f = f' * fps_X ^ subdegree g" and g_eq: "g = g' * fps_X ^ subdegree g"
-    unfolding f'_def g'_def by (rule subdegree_decompose' le_refl | fact)+
-  have subdegree: "subdegree f' = subdegree f - subdegree g" "subdegree g' = 0"
-    using assms(2) by (simp_all add: f'_def g'_def)
-  have [simp]: "fps_conv_radius f' = fps_conv_radius f" "fps_conv_radius g' = fps_conv_radius g"
-    by (simp_all add: f'_def g'_def)
-  have [simp]: "fps_nth f' 0 = fps_nth f (subdegree g)"
-               "fps_nth g' 0 = fps_nth g (subdegree g)" by (simp_all add: f'_def g'_def)
-  have g_nz: "g \<noteq> 0"
-  proof -
-    define z :: complex where "z = (if r = \<infinity> then 1 else of_real (real_of_ereal r / 2))"
-    from \<open>r > 0\<close> have "z \<in> eball 0 r"
-      by (cases r) (auto simp: z_def eball_def)
-    moreover have "z \<noteq> 0" using \<open>r > 0\<close> 
-      by (cases r) (auto simp: z_def)
-    ultimately have "eval_fps g z \<noteq> 0" by (rule assms(6))
-    thus "g \<noteq> 0" by auto
-  qed
-  have fg: "f / g = f' * inverse g'"
-    by (subst f_eq, subst (2) g_eq) (insert g_nz, simp add: fps_divide_unit)
-
-  have g'_nz: "eval_fps g' z \<noteq> 0" if z: "norm z < min r (fps_conv_radius g)" for z
-  proof (cases "z = 0")
-    case False
-    with assms and z have "eval_fps g z \<noteq> 0" by auto
-    also from z have "eval_fps g z = eval_fps g' z * z ^ subdegree g"
-      by (subst g_eq) (auto simp: eval_fps_mult)
-    finally show ?thesis by auto
-  qed (insert \<open>g \<noteq> 0\<close>, auto simp: g'_def eval_fps_at_0)
-
-  have "R \<le> min (min r (fps_conv_radius g)) (fps_conv_radius g')"
-    by (auto simp: R_def min.coboundedI1 min.coboundedI2)
-  also have "\<dots> \<le> fps_conv_radius (inverse g')"
-    using g'_nz by (rule fps_conv_radius_inverse)
-  finally have conv_radius_inv: "R \<le> fps_conv_radius (inverse g')" .
-  hence "R \<le> fps_conv_radius (f' * inverse g')"
-    by (intro order.trans[OF _ fps_conv_radius_mult])
-       (auto simp: R_def intro: min.coboundedI1 min.coboundedI2)
-  thus "fps_conv_radius (f / g) \<ge> R" by (simp add: fg)
-
-  fix z c :: complex assume z: "ereal (norm z) < R"
-  assume c: "c = fps_nth f (subdegree g) / fps_nth g (subdegree g)"
-  show "eval_fps (f / g) z = (if z = 0 then c else eval_fps f z / eval_fps g z)"
-  proof (cases "z = 0")
-    case False
-    from z and conv_radius_inv have "ereal (norm z) < fps_conv_radius (inverse g')"
-      by simp
-    with z have "eval_fps (f / g) z = eval_fps f' z * eval_fps (inverse g') z"
-      unfolding fg by (subst eval_fps_mult) (auto simp: R_def)
-    also have "eval_fps (inverse g') z = inverse (eval_fps g' z)"
-      using z by (intro eval_fps_inverse[of "min r (fps_conv_radius g')"] g'_nz) (auto simp: R_def)
-    also have "eval_fps f' z * \<dots> = eval_fps f z / eval_fps g z"
-      using z False assms(2) by (simp add: f'_def g'_def eval_fps_shift R_def)
-    finally show ?thesis using False by simp
-  qed (simp_all add: eval_fps_at_0 fg field_simps c)
-qed
+text \<open>
+  The case of division is more complicated and will therefore not be handled here.
+  Handling division becomes much more easy using complex analysis, and we will do so once
+  that is available.
+\<close>
 
 
-subsection \<open>Power series expansion of complex functions\<close>
+subsection \<open>Power series expansions of analytic functions\<close>
 
 text \<open>
   This predicate contains the notion that the given formal power series converges
@@ -831,25 +636,6 @@
   finally show ?thesis .
 qed
 
-lemma has_fps_expansion_fps_expansion [intro]:
-  assumes "open A" "0 \<in> A" "f holomorphic_on A"
-  shows   "f has_fps_expansion fps_expansion f 0"
-proof -
-  from assms(1,2) obtain r where r: "r > 0 " "ball 0 r \<subseteq> A"
-    by (auto simp: open_contains_ball)
-  have holo: "f holomorphic_on eball 0 (ereal r)" 
-    using r(2) and assms(3) by auto
-  from r(1) have "0 < ereal r" by simp
-  also have "r \<le> fps_conv_radius (fps_expansion f 0)"
-    using holo by (intro conv_radius_fps_expansion) auto
-  finally have "\<dots> > 0" .
-  moreover have "eventually (\<lambda>z. z \<in> ball 0 r) (nhds 0)"
-    using r(1) by (intro eventually_nhds_in_open) auto
-  hence "eventually (\<lambda>z. eval_fps (fps_expansion f 0) z = f z) (nhds 0)"
-    by eventually_elim (subst eval_fps_expansion'[OF holo], auto)
-  ultimately show ?thesis using r(1) by (auto simp: has_fps_expansion_def)
-qed
-
 lemma has_fps_expansion_imp_continuous:
   fixes F :: "'a::{real_normed_field,banach} fps"
   assumes "f has_fps_expansion F"
@@ -1146,35 +932,7 @@
   finally show ?thesis by simp
 qed
 
-lemma fps_conv_radius_tan:
-  fixes c :: complex
-  assumes "c \<noteq> 0"
-  shows   "fps_conv_radius (fps_tan c) \<ge> pi / (2 * norm c)"
-proof -
-  have "fps_conv_radius (fps_tan c) \<ge> 
-          Min {pi / (2 * norm c), fps_conv_radius (fps_sin c), fps_conv_radius (fps_cos c)}"
-    unfolding fps_tan_def
-  proof (rule fps_conv_radius_divide)
-    fix z :: complex assume "z \<in> eball 0 (pi / (2 * norm c))"
-    with cos_eq_zero_imp_norm_ge[of "c*z"] assms 
-      show "eval_fps (fps_cos  c) z \<noteq> 0" by (auto simp: norm_mult field_simps)
-  qed (insert assms, auto)
-  thus ?thesis by (simp add: min_def)
-qed
 
-lemma eval_fps_tan:
-  fixes c :: complex
-  assumes "norm z < pi / (2 * norm c)"
-  shows   "eval_fps (fps_tan c) z = tan (c * z)"
-proof (cases "c = 0")
-  case False
-  show ?thesis unfolding fps_tan_def
-  proof (subst eval_fps_divide'[where r = "pi / (2 * norm c)"])
-    fix z :: complex assume "z \<in> eball 0 (pi / (2 * norm c))"
-    with cos_eq_zero_imp_norm_ge[of "c*z"] assms 
-      show "eval_fps (fps_cos  c) z \<noteq> 0" using False by (auto simp: norm_mult field_simps)
-    qed (insert False assms, auto simp: field_simps tan_def)
-qed simp_all
 
 lemma eval_fps_binomial:
   fixes c :: complex
@@ -1295,17 +1053,4 @@
     by (intro that[of ?s']) (auto simp: has_fps_expansion_def zero_ereal_def)
 qed
 
-theorem residue_fps_expansion_over_power_at_0:
-  assumes "f has_fps_expansion F"
-  shows   "residue (\<lambda>z. f z / z ^ Suc n) 0 = fps_nth F n"
-proof -
-  from has_fps_expansion_imp_holomorphic[OF assms] guess s . note s = this
-  have "residue (\<lambda>z. f z / (z - 0) ^ Suc n) 0 = (deriv ^^ n) f 0 / fact n"
-    using assms s unfolding has_fps_expansion_def
-    by (intro residue_holomorphic_over_power[of s]) (auto simp: zero_ereal_def)
-  also from assms have "\<dots> = fps_nth F n"
-    by (subst fps_nth_fps_expansion) auto
-  finally show ?thesis by simp
-qed
-
-end
+end
\ No newline at end of file
--- a/src/HOL/Analysis/Gamma_Function.thy	Wed Nov 27 16:54:33 2019 +0000
+++ b/src/HOL/Analysis/Gamma_Function.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -6,7 +6,6 @@
 
 theory Gamma_Function
   imports
-  Conformal_Mappings
   Equivalence_Lebesgue_Henstock_Integration
   Summation_Tests
   Harmonic_Numbers
@@ -2065,7 +2064,208 @@
     by (simp add: field_split_simps Gamma_eq_zero_iff ring_distribs exp_diff exp_of_real ac_simps)
 qed
 
-theorem Gamma_reflection_complex:
+text \<open>
+  The following lemma is somewhat annoying. With a little bit of complex analysis
+  (Cauchy's integral theorem, to be exact), this would be completely trivial. However,
+  we want to avoid depending on the complex analysis session at this point, so we prove it
+  the hard way.
+\<close>
+private lemma Gamma_reflection_aux:
+  defines "h \<equiv> \<lambda>z::complex. if z \<in> \<int> then 0 else
+                 (of_real pi * cot (of_real pi*z) + Digamma z - Digamma (1 - z))"
+  defines "a \<equiv> complex_of_real pi"
+  obtains h' where "continuous_on UNIV h'" "\<And>z. (h has_field_derivative (h' z)) (at z)"
+proof -
+  define f where "f n = a * of_real (cos_coeff (n+1) - sin_coeff (n+2))" for n
+  define F where "F z = (if z = 0 then 0 else (cos (a*z) - sin (a*z)/(a*z)) / z)" for z
+  define g where "g n = complex_of_real (sin_coeff (n+1))" for n
+  define G where "G z = (if z = 0 then 1 else sin (a*z)/(a*z))" for z
+  have a_nz: "a \<noteq> 0" unfolding a_def by simp
+
+  have "(\<lambda>n. f n * (a*z)^n) sums (F z) \<and> (\<lambda>n. g n * (a*z)^n) sums (G z)"
+    if "abs (Re z) < 1" for z
+  proof (cases "z = 0"; rule conjI)
+    assume "z \<noteq> 0"
+    note z = this that
+
+    from z have sin_nz: "sin (a*z) \<noteq> 0" unfolding a_def by (auto simp: sin_eq_0)
+    have "(\<lambda>n. of_real (sin_coeff n) * (a*z)^n) sums (sin (a*z))" using sin_converges[of "a*z"]
+      by (simp add: scaleR_conv_of_real)
+    from sums_split_initial_segment[OF this, of 1]
+      have "(\<lambda>n. (a*z) * of_real (sin_coeff (n+1)) * (a*z)^n) sums (sin (a*z))" by (simp add: mult_ac)
+    from sums_mult[OF this, of "inverse (a*z)"] z a_nz
+      have A: "(\<lambda>n. g n * (a*z)^n) sums (sin (a*z)/(a*z))"
+      by (simp add: field_simps g_def)
+    with z show "(\<lambda>n. g n * (a*z)^n) sums (G z)" by (simp add: G_def)
+    from A z a_nz sin_nz have g_nz: "(\<Sum>n. g n * (a*z)^n) \<noteq> 0" by (simp add: sums_iff g_def)
+
+    have [simp]: "sin_coeff (Suc 0) = 1" by (simp add: sin_coeff_def)
+    from sums_split_initial_segment[OF sums_diff[OF cos_converges[of "a*z"] A], of 1]
+    have "(\<lambda>n. z * f n * (a*z)^n) sums (cos (a*z) - sin (a*z) / (a*z))"
+      by (simp add: mult_ac scaleR_conv_of_real ring_distribs f_def g_def)
+    from sums_mult[OF this, of "inverse z"] z assms
+      show "(\<lambda>n. f n * (a*z)^n) sums (F z)" by (simp add: divide_simps mult_ac f_def F_def)
+  next
+    assume z: "z = 0"
+    have "(\<lambda>n. f n * (a * z) ^ n) sums f 0" using powser_sums_zero[of f] z by simp
+    with z show "(\<lambda>n. f n * (a * z) ^ n) sums (F z)"
+      by (simp add: f_def F_def sin_coeff_def cos_coeff_def)
+    have "(\<lambda>n. g n * (a * z) ^ n) sums g 0" using powser_sums_zero[of g] z by simp
+    with z show "(\<lambda>n. g n * (a * z) ^ n) sums (G z)"
+      by (simp add: g_def G_def sin_coeff_def cos_coeff_def)
+  qed
+  note sums = conjunct1[OF this] conjunct2[OF this]
+
+  define h2 where [abs_def]:
+    "h2 z = (\<Sum>n. f n * (a*z)^n) / (\<Sum>n. g n * (a*z)^n) + Digamma (1 + z) - Digamma (1 - z)" for z
+  define POWSER where [abs_def]: "POWSER f z = (\<Sum>n. f n * (z^n :: complex))" for f z
+  define POWSER' where [abs_def]: "POWSER' f z = (\<Sum>n. diffs f n * (z^n))" for f and z :: complex
+  define h2' where [abs_def]:
+    "h2' z = a * (POWSER g (a*z) * POWSER' f (a*z) - POWSER f (a*z) * POWSER' g (a*z)) /
+      (POWSER g (a*z))^2 + Polygamma 1 (1 + z) + Polygamma 1 (1 - z)" for z
+
+  have h_eq: "h t = h2 t" if "abs (Re t) < 1" for t
+  proof -
+    from that have t: "t \<in> \<int> \<longleftrightarrow> t = 0" by (auto elim!: Ints_cases simp: dist_0_norm)
+    hence "h t = a*cot (a*t) - 1/t + Digamma (1 + t) - Digamma (1 - t)"
+      unfolding h_def using Digamma_plus1[of t] by (force simp: field_simps a_def)
+    also have "a*cot (a*t) - 1/t = (F t) / (G t)"
+      using t by (auto simp add: divide_simps sin_eq_0 cot_def a_def F_def G_def)
+    also have "\<dots> = (\<Sum>n. f n * (a*t)^n) / (\<Sum>n. g n * (a*t)^n)"
+      using sums[of t] that by (simp add: sums_iff dist_0_norm)
+    finally show "h t = h2 t" by (simp only: h2_def)
+  qed
+
+  let ?A = "{z. abs (Re z) < 1}"
+  have "open ({z. Re z < 1} \<inter> {z. Re z > -1})"
+    using open_halfspace_Re_gt open_halfspace_Re_lt by auto
+  also have "({z. Re z < 1} \<inter> {z. Re z > -1}) = {z. abs (Re z) < 1}" by auto
+  finally have open_A: "open ?A" .
+  hence [simp]: "interior ?A = ?A" by (simp add: interior_open)
+
+  have summable_f: "summable (\<lambda>n. f n * z^n)" for z
+    by (rule powser_inside, rule sums_summable, rule sums[of "\<i> * of_real (norm z + 1) / a"])
+       (simp_all add: norm_mult a_def del: of_real_add)
+  have summable_g: "summable (\<lambda>n. g n * z^n)" for z
+    by (rule powser_inside, rule sums_summable, rule sums[of "\<i> * of_real (norm z + 1) / a"])
+       (simp_all add: norm_mult a_def del: of_real_add)
+  have summable_fg': "summable (\<lambda>n. diffs f n * z^n)" "summable (\<lambda>n. diffs g n * z^n)" for z
+    by (intro termdiff_converges_all summable_f summable_g)+
+  have "(POWSER f has_field_derivative (POWSER' f z)) (at z)"
+               "(POWSER g has_field_derivative (POWSER' g z)) (at z)" for z
+    unfolding POWSER_def POWSER'_def
+    by (intro termdiffs_strong_converges_everywhere summable_f summable_g)+
+  note derivs = this[THEN DERIV_chain2[OF _ DERIV_cmult[OF DERIV_ident]], unfolded POWSER_def]
+  have "isCont (POWSER f) z" "isCont (POWSER g) z" "isCont (POWSER' f) z" "isCont (POWSER' g) z"
+    for z unfolding POWSER_def POWSER'_def
+    by (intro isCont_powser_converges_everywhere summable_f summable_g summable_fg')+
+  note cont = this[THEN isCont_o2[rotated], unfolded POWSER_def POWSER'_def]
+
+  {
+    fix z :: complex assume z: "abs (Re z) < 1"
+    define d where "d = \<i> * of_real (norm z + 1)"
+    have d: "abs (Re d) < 1" "norm z < norm d" by (simp_all add: d_def norm_mult del: of_real_add)
+    have "eventually (\<lambda>z. h z = h2 z) (nhds z)"
+      using eventually_nhds_in_nhd[of z ?A] using h_eq z
+      by (auto elim!: eventually_mono simp: dist_0_norm)
+
+    moreover from sums(2)[OF z] z have nz: "(\<Sum>n. g n * (a * z) ^ n) \<noteq> 0"
+      unfolding G_def by (auto simp: sums_iff sin_eq_0 a_def)
+    have A: "z \<in> \<int> \<longleftrightarrow> z = 0" using z by (auto elim!: Ints_cases)
+    have no_int: "1 + z \<in> \<int> \<longleftrightarrow> z = 0" using z Ints_diff[of "1+z" 1] A
+      by (auto elim!: nonpos_Ints_cases)
+    have no_int': "1 - z \<in> \<int> \<longleftrightarrow> z = 0" using z Ints_diff[of 1 "1-z"] A
+      by (auto elim!: nonpos_Ints_cases)
+    from no_int no_int' have no_int: "1 - z \<notin> \<int>\<^sub>\<le>\<^sub>0" "1 + z \<notin> \<int>\<^sub>\<le>\<^sub>0" by auto
+    have "(h2 has_field_derivative h2' z) (at z)" unfolding h2_def
+      by (rule DERIV_cong, (rule derivative_intros refl derivs[unfolded POWSER_def] nz no_int)+)
+         (auto simp: h2'_def POWSER_def field_simps power2_eq_square)
+    ultimately have deriv: "(h has_field_derivative h2' z) (at z)"
+      by (subst DERIV_cong_ev[OF refl _ refl])
+
+    from sums(2)[OF z] z have "(\<Sum>n. g n * (a * z) ^ n) \<noteq> 0"
+      unfolding G_def by (auto simp: sums_iff a_def sin_eq_0)
+    hence "isCont h2' z" using no_int unfolding h2'_def[abs_def] POWSER_def POWSER'_def
+      by (intro continuous_intros cont
+            continuous_on_compose2[OF _ continuous_on_Polygamma[of "{z. Re z > 0}"]]) auto
+    note deriv and this
+  } note A = this
+
+  interpret h: periodic_fun_simple' h
+  proof
+    fix z :: complex
+    show "h (z + 1) = h z"
+    proof (cases "z \<in> \<int>")
+      assume z: "z \<notin> \<int>"
+      hence A: "z + 1 \<notin> \<int>" "z \<noteq> 0" using Ints_diff[of "z+1" 1] by auto
+      hence "Digamma (z + 1) - Digamma (-z) = Digamma z - Digamma (-z + 1)"
+        by (subst (1 2) Digamma_plus1) simp_all
+      with A z show "h (z + 1) = h z"
+        by (simp add: h_def sin_plus_pi cos_plus_pi ring_distribs cot_def)
+    qed (simp add: h_def)
+  qed
+
+  have h2'_eq: "h2' (z - 1) = h2' z" if z: "Re z > 0" "Re z < 1" for z
+  proof -
+    have "((\<lambda>z. h (z - 1)) has_field_derivative h2' (z - 1)) (at z)"
+      by (rule DERIV_cong, rule DERIV_chain'[OF _ A(1)])
+         (insert z, auto intro!: derivative_eq_intros)
+    hence "(h has_field_derivative h2' (z - 1)) (at z)" by (subst (asm) h.minus_1)
+    moreover from z have "(h has_field_derivative h2' z) (at z)" by (intro A) simp_all
+    ultimately show "h2' (z - 1) = h2' z" by (rule DERIV_unique)
+  qed
+
+  define h2'' where "h2'' z = h2' (z - of_int \<lfloor>Re z\<rfloor>)" for z
+  have deriv: "(h has_field_derivative h2'' z) (at z)" for z
+  proof -
+    fix z :: complex
+    have B: "\<bar>Re z - real_of_int \<lfloor>Re z\<rfloor>\<bar> < 1" by linarith
+    have "((\<lambda>t. h (t - of_int \<lfloor>Re z\<rfloor>)) has_field_derivative h2'' z) (at z)"
+      unfolding h2''_def by (rule DERIV_cong, rule DERIV_chain'[OF _ A(1)])
+                            (insert B, auto intro!: derivative_intros)
+    thus "(h has_field_derivative h2'' z) (at z)" by (simp add: h.minus_of_int)
+  qed
+
+  have cont: "continuous_on UNIV h2''"
+  proof (intro continuous_at_imp_continuous_on ballI)
+    fix z :: complex
+    define r where "r = \<lfloor>Re z\<rfloor>"
+    define A where "A = {t. of_int r - 1 < Re t \<and> Re t < of_int r + 1}"
+    have "continuous_on A (\<lambda>t. h2' (t - of_int r))" unfolding A_def
+      by (intro continuous_at_imp_continuous_on isCont_o2[OF _ A(2)] ballI continuous_intros)
+         (simp_all add: abs_real_def)
+    moreover have "h2'' t = h2' (t - of_int r)" if t: "t \<in> A" for t
+    proof (cases "Re t \<ge> of_int r")
+      case True
+      from t have "of_int r - 1 < Re t" "Re t < of_int r + 1" by (simp_all add: A_def)
+      with True have "\<lfloor>Re t\<rfloor> = \<lfloor>Re z\<rfloor>" unfolding r_def by linarith
+      thus ?thesis by (auto simp: r_def h2''_def)
+    next
+      case False
+      from t have t: "of_int r - 1 < Re t" "Re t < of_int r + 1" by (simp_all add: A_def)
+      with False have t': "\<lfloor>Re t\<rfloor> = \<lfloor>Re z\<rfloor> - 1" unfolding r_def by linarith
+      moreover from t False have "h2' (t - of_int r + 1 - 1) = h2' (t - of_int r + 1)"
+        by (intro h2'_eq) simp_all
+      ultimately show ?thesis by (auto simp: r_def h2''_def algebra_simps t')
+    qed
+    ultimately have "continuous_on A h2''" by (subst continuous_on_cong[OF refl])
+    moreover {
+      have "open ({t. of_int r - 1 < Re t} \<inter> {t. of_int r + 1 > Re t})"
+        by (intro open_Int open_halfspace_Re_gt open_halfspace_Re_lt)
+      also have "{t. of_int r - 1 < Re t} \<inter> {t. of_int r + 1 > Re t} = A"
+        unfolding A_def by blast
+      finally have "open A" .
+    }
+    ultimately have C: "isCont h2'' t" if "t \<in> A" for t using that
+      by (subst (asm) continuous_on_eq_continuous_at) auto
+    have "of_int r - 1 < Re z" "Re z  < of_int r + 1" unfolding r_def by linarith+
+    thus "isCont h2'' z" by (intro C) (simp_all add: A_def)
+  qed
+
+  from that[OF cont deriv] show ?thesis .
+qed
+
+lemma Gamma_reflection_complex:
   fixes z :: complex
   shows "Gamma z * Gamma (1 - z) = of_real pi / sin (of_real pi * z)"
 proof -
@@ -2074,7 +2274,7 @@
   let ?h = "\<lambda>z::complex. (of_real pi * cot (of_real pi*z) + Digamma z - Digamma (1 - z))"
   define h where [abs_def]: "h z = (if z \<in> \<int> then 0 else ?h z)" for z :: complex
 
-  \<comment> \<open>\<^term>\<open>g\<close> is periodic with period 1.\<close>
+  \<comment> \<open>@{term g} is periodic with period 1.\<close>
   interpret g: periodic_fun_simple' g
   proof
     fix z :: complex
@@ -2094,8 +2294,8 @@
     qed (simp add: g_def)
   qed
 
-  \<comment> \<open>\<^term>\<open>g\<close> is entire.\<close>
-  have g_g' [derivative_intros]: "(g has_field_derivative (h z * g z)) (at z)" for z :: complex
+  \<comment> \<open>@{term g} is entire.\<close>
+  have g_g': "(g has_field_derivative (h z * g z)) (at z)" for z :: complex
   proof (cases "z \<in> \<int>")
     let ?h' = "\<lambda>z. Beta z (1 - z) * ((Digamma z - Digamma (1 - z)) * sin (z * of_real pi) +
                      of_real pi * cos (z * of_real pi))"
@@ -2144,10 +2344,6 @@
     finally show "(g has_field_derivative (h z * g z)) (at z)" by (simp add: z h_def)
   qed
 
-  have g_holo [holomorphic_intros]: "g holomorphic_on A" for A
-    by (rule holomorphic_on_subset[of _ UNIV])
-       (force simp: holomorphic_on_open intro!: derivative_intros)+
-
   have g_eq: "g (z/2) * g ((z+1)/2) = Gamma (1/2)^2 * g z" if "Re z > -1" "Re z < 2" for z
   proof (cases "z \<in> \<int>")
     case True
@@ -2208,9 +2404,6 @@
   unfolding g_def using Ints_diff[of 1 "1 - z"]
     by (auto simp: Gamma_eq_zero_iff sin_eq_0 dest!: nonpos_Ints_Int)
 
-  have h_altdef: "h z = deriv g z / g z" for z :: complex
-    using DERIV_imp_deriv[OF g_g', of z] by simp
-
   have h_eq: "h z = (h (z/2) + h ((z+1)/2)) / 2" for z
   proof -
     have "((\<lambda>t. g (t/2) * g ((t+1)/2)) has_field_derivative
@@ -2230,16 +2423,9 @@
     thus "h z = (h (z/2) + h ((z+1)/2)) / 2" by simp
   qed
 
-  have h_holo [holomorphic_intros]: "h holomorphic_on A" for A
-    unfolding h_altdef [abs_def]
-    by (rule holomorphic_on_subset[of _ UNIV]) (auto intro!: holomorphic_intros)
-  define h' where "h' = deriv h"
-  have h_h': "(h has_field_derivative h' z) (at z)" for z unfolding h'_def
-    by (auto intro!: holomorphic_derivI[of _ UNIV] holomorphic_intros)
-  have h'_holo [holomorphic_intros]: "h' holomorphic_on A" for A unfolding h'_def
-    by (rule holomorphic_on_subset[of _ UNIV]) (auto intro!: holomorphic_intros)
-  have h'_cont: "continuous_on UNIV h'"
-    by (intro holomorphic_on_imp_continuous_on holomorphic_intros)
+  obtain h' where h'_cont: "continuous_on UNIV h'" and
+                  h_h': "\<And>z. (h has_field_derivative h' z) (at z)"
+     unfolding h_def by (erule Gamma_reflection_aux)
 
   have h'_eq: "h' z = (h' (z/2) + h' ((z+1)/2)) / 4" for z
   proof -
@@ -2307,13 +2493,16 @@
     unfolding g_def using that by (auto intro!: Reals_mult Gamma_complex_real)
   have h_real: "h z \<in> \<real>" if "z \<in> \<real>" for z
     unfolding h_def using that by (auto intro!: Reals_mult Reals_add Reals_diff Polygamma_Real)
+  have g_nz: "g z \<noteq> 0" for z unfolding g_def using Ints_diff[of 1 "1-z"]
+    by (auto simp: Gamma_eq_zero_iff sin_eq_0)
 
   from h'_zero h_h'_2 have "\<exists>c. \<forall>z\<in>UNIV. h z = c"
-    by (intro has_field_derivative_zero_constant) simp_all
+    by (intro has_field_derivative_zero_constant) (simp_all add: dist_0_norm)
   then obtain c where c: "\<And>z. h z = c" by auto
   have "\<exists>u. u \<in> closed_segment 0 1 \<and> Re (g 1) - Re (g 0) = Re (h u * g u * (1 - 0))"
     by (intro complex_mvt_line g_g')
-  then guess u by (elim exE conjE) note u = this
+  then obtain u where u: "u \<in> closed_segment 0 1" "Re (g 1) - Re (g 0) = Re (h u * g u)"
+    by auto
   from u(1) have u': "u \<in> \<real>" unfolding closed_segment_def
     by (auto simp: scaleR_conv_of_real)
   from u' g_real[of u] g_nz[of u] have "Re (g u) \<noteq> 0" by (auto elim!: Reals_cases)
@@ -2330,7 +2519,7 @@
   show ?thesis
   proof (cases "z \<in> \<int>")
     case False
-    with \<open>g z = pi\<close> show ?thesis by (auto simp: g_def field_split_simps)
+    with \<open>g z = pi\<close> show ?thesis by (auto simp: g_def divide_simps)
   next
     case True
     then obtain n where n: "z = of_int n" by (elim Ints_cases)
@@ -2446,20 +2635,6 @@
   finally show "(\<lambda>z. inverse (rGamma z / (z + of_nat n))) \<midarrow> (- of_nat n) \<rightarrow> ?c" .
 qed
 
-lemma is_pole_Gamma: "is_pole Gamma (- of_nat n)"
-  unfolding is_pole_def using Gamma_poles .
-
-lemma Gamme_residue:
-  "residue Gamma (- of_nat n) = (-1) ^ n / fact n"
-proof (rule residue_simple')
-  show "open (- (\<int>\<^sub>\<le>\<^sub>0 - {-of_nat n}) :: complex set)"
-    by (intro open_Compl closed_subset_Ints) auto
-  show "Gamma holomorphic_on (- (\<int>\<^sub>\<le>\<^sub>0 - {-of_nat n}) - {- of_nat n})"
-    by (rule holomorphic_Gamma) auto
-  show "(\<lambda>w. Gamma w * (w - - of_nat n)) \<midarrow>- of_nat n \<rightarrow> (- 1) ^ n / fact n"
-    using Gamma_residues[of n] by simp
-qed auto
-
 
 subsection \<open>Alternative definitions\<close>
 
--- a/src/HOL/Analysis/Great_Picard.thy	Wed Nov 27 16:54:33 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1848 +0,0 @@
-section \<open>The Great Picard Theorem and its Applications\<close>
-
-text\<open>Ported from HOL Light (cauchy.ml) by L C Paulson, 2017\<close>
-
-theory Great_Picard
-  imports Conformal_Mappings Further_Topology
-
-begin
-  
-subsection\<open>Schottky's theorem\<close>
-
-lemma Schottky_lemma0:
-  assumes holf: "f holomorphic_on S" and cons: "contractible S" and "a \<in> S"
-      and f: "\<And>z. z \<in> S \<Longrightarrow> f z \<noteq> 1 \<and> f z \<noteq> -1"
-  obtains g where "g holomorphic_on S"
-                  "norm(g a) \<le> 1 + norm(f a) / 3"
-                  "\<And>z. z \<in> S \<Longrightarrow> f z = cos(of_real pi * g z)"
-proof -
-  obtain g where holg: "g holomorphic_on S" and g: "norm(g a) \<le> pi + norm(f a)"
-             and f_eq_cos: "\<And>z. z \<in> S \<Longrightarrow> f z = cos(g z)"
-    using contractible_imp_holomorphic_arccos_bounded [OF assms]
-    by blast
-  show ?thesis
-  proof
-    show "(\<lambda>z. g z / pi) holomorphic_on S"
-      by (auto intro: holomorphic_intros holg)
-    have "3 \<le> pi"
-      using pi_approx by force
-    have "3 * norm(g a) \<le> 3 * (pi + norm(f a))"
-      using g by auto
-    also have "... \<le>  pi * 3 + pi * cmod (f a)"
-      using \<open>3 \<le> pi\<close> by (simp add: mult_right_mono algebra_simps)
-    finally show "cmod (g a / complex_of_real pi) \<le> 1 + cmod (f a) / 3"
-      by (simp add: field_simps norm_divide)
-    show "\<And>z. z \<in> S \<Longrightarrow> f z = cos (complex_of_real pi * (g z / complex_of_real pi))"
-      by (simp add: f_eq_cos)
-  qed
-qed
-
-
-lemma Schottky_lemma1:
-  fixes n::nat
-  assumes "0 < n"
-  shows "0 < n + sqrt(real n ^ 2 - 1)"
-proof -
-  have "(n-1)^2 \<le> n^2 - 1"
-    using assms by (simp add: algebra_simps power2_eq_square)
-  then have "real (n - 1) \<le> sqrt (real (n\<^sup>2 - 1))"
-    by (metis of_nat_le_iff of_nat_power real_le_rsqrt)
-  then have "n-1 \<le> sqrt(real n ^ 2 - 1)"
-    by (simp add: Suc_leI assms of_nat_diff)
-  then show ?thesis
-    using assms by linarith
-qed
-
-
-lemma Schottky_lemma2:
-  fixes x::real
-  assumes "0 \<le> x"
-  obtains n where "0 < n" "\<bar>x - ln (real n + sqrt ((real n)\<^sup>2 - 1)) / pi\<bar> < 1/2"
-proof -
-  obtain n0::nat where "0 < n0" "ln(n0 + sqrt(real n0 ^ 2 - 1)) / pi \<le> x"
-  proof
-    show "ln(real 1 + sqrt(real 1 ^ 2 - 1))/pi \<le> x"
-      by (auto simp: assms)
-  qed auto
-  moreover
-  obtain M::nat where "\<And>n. \<lbrakk>0 < n; ln(n + sqrt(real n ^ 2 - 1)) / pi \<le> x\<rbrakk> \<Longrightarrow> n \<le> M"
-  proof
-    fix n::nat
-    assume "0 < n" "ln (n + sqrt ((real n)\<^sup>2 - 1)) / pi \<le> x"
-    then have "ln (n + sqrt ((real n)\<^sup>2 - 1)) \<le> x * pi"
-      by (simp add: field_split_simps)
-    then have *: "exp (ln (n + sqrt ((real n)\<^sup>2 - 1))) \<le> exp (x * pi)"
-      by blast
-    have 0: "0 \<le> sqrt ((real n)\<^sup>2 - 1)"
-      using \<open>0 < n\<close> by auto
-    have "n + sqrt ((real n)\<^sup>2 - 1) = exp (ln (n + sqrt ((real n)\<^sup>2 - 1)))"
-      by (simp add: Suc_leI \<open>0 < n\<close> add_pos_nonneg real_of_nat_ge_one_iff)
-    also have "... \<le> exp (x * pi)"
-      using "*" by blast
-    finally have "real n \<le> exp (x * pi)"
-      using 0 by linarith
-    then show "n \<le> nat (ceiling (exp(x * pi)))"
-      by linarith
-  qed
-  ultimately obtain n where
-     "0 < n" and le_x: "ln(n + sqrt(real n ^ 2 - 1)) / pi \<le> x"
-             and le_n: "\<And>k. \<lbrakk>0 < k; ln(k + sqrt(real k ^ 2 - 1)) / pi \<le> x\<rbrakk> \<Longrightarrow> k \<le> n"
-    using bounded_Max_nat [of "\<lambda>n. 0<n \<and> ln (n + sqrt ((real n)\<^sup>2 - 1)) / pi \<le> x"] by metis
-  define a where "a \<equiv> ln(n + sqrt(real n ^ 2 - 1)) / pi"
-  define b where "b \<equiv> ln (1 + real n + sqrt ((1 + real n)\<^sup>2 - 1)) / pi"
-  have le_xa: "a \<le> x"
-   and le_na: "\<And>k. \<lbrakk>0 < k; ln(k + sqrt(real k ^ 2 - 1)) / pi \<le> x\<rbrakk> \<Longrightarrow> k \<le> n"
-    using le_x le_n by (auto simp: a_def)
-  moreover have "x < b"
-    using le_n [of "Suc n"] by (force simp: b_def)
-  moreover have "b - a < 1"
-  proof -
-    have "ln (1 + real n + sqrt ((1 + real n)\<^sup>2 - 1)) - ln (real n + sqrt ((real n)\<^sup>2 - 1)) =
-         ln ((1 + real n + sqrt ((1 + real n)\<^sup>2 - 1)) / (real n + sqrt ((real n)\<^sup>2 - 1)))"
-      by (simp add: \<open>0 < n\<close> Schottky_lemma1 add_pos_nonneg ln_div [symmetric])
-    also have "... \<le> 3"
-    proof (cases "n = 1")
-      case True
-      have "sqrt 3 \<le> 2"
-        by (simp add: real_le_lsqrt)
-      then have "(2 + sqrt 3) \<le> 4"
-        by simp
-      also have "... \<le> exp 3"
-        using exp_ge_add_one_self [of "3::real"] by simp
-      finally have "ln (2 + sqrt 3) \<le> 3"
-        by (metis add_nonneg_nonneg add_pos_nonneg dbl_def dbl_inc_def dbl_inc_simps(3)
-            dbl_simps(3) exp_gt_zero ln_exp ln_le_cancel_iff real_sqrt_ge_0_iff zero_le_one zero_less_one)
-      then show ?thesis
-        by (simp add: True)
-    next
-      case False with \<open>0 < n\<close> have "1 < n" "2 \<le> n"
-        by linarith+
-      then have 1: "1 \<le> real n * real n"
-        by (metis less_imp_le_nat one_le_power power2_eq_square real_of_nat_ge_one_iff)
-      have *: "4 + (m+2) * 2 \<le> (m+2) * ((m+2) * 3)" for m::nat
-        by simp
-      have "4 + n * 2 \<le> n * (n * 3)"
-        using * [of "n-2"]  \<open>2 \<le> n\<close>
-        by (metis le_add_diff_inverse2)
-      then have **: "4 + real n * 2 \<le> real n * (real n * 3)"
-        by (metis (mono_tags, hide_lams) of_nat_le_iff of_nat_add of_nat_mult of_nat_numeral)
-      have "sqrt ((1 + real n)\<^sup>2 - 1) \<le> 2 * sqrt ((real n)\<^sup>2 - 1)"
-        by (auto simp: real_le_lsqrt power2_eq_square algebra_simps 1 **)
-      then
-      have "((1 + real n + sqrt ((1 + real n)\<^sup>2 - 1)) / (real n + sqrt ((real n)\<^sup>2 - 1))) \<le> 2"
-        using Schottky_lemma1 \<open>0 < n\<close>  by (simp add: field_split_simps)
-      then have "ln ((1 + real n + sqrt ((1 + real n)\<^sup>2 - 1)) / (real n + sqrt ((real n)\<^sup>2 - 1))) \<le> ln 2"
-        apply (subst ln_le_cancel_iff)
-        using Schottky_lemma1 \<open>0 < n\<close> by auto (force simp: field_split_simps)
-      also have "... \<le> 3"
-        using ln_add_one_self_le_self [of 1] by auto
-      finally show ?thesis .
-    qed
-    also have "... < pi"
-      using pi_approx by simp
-    finally show ?thesis
-      by (simp add: a_def b_def field_split_simps)
-  qed
-  ultimately have "\<bar>x - a\<bar> < 1/2 \<or> \<bar>x - b\<bar> < 1/2"
-    by (auto simp: abs_if)
-  then show thesis
-  proof
-    assume "\<bar>x - a\<bar> < 1 / 2"
-    then show ?thesis
-      by (rule_tac n=n in that) (auto simp: a_def \<open>0 < n\<close>)
-  next
-    assume "\<bar>x - b\<bar> < 1 / 2"
-    then show ?thesis
-      by (rule_tac n="Suc n" in that) (auto simp: b_def \<open>0 < n\<close>)
-  qed
-qed
-
-
-lemma Schottky_lemma3:
-  fixes z::complex
-  assumes "z \<in> (\<Union>m \<in> Ints. \<Union>n \<in> {0<..}. {Complex m (ln(n + sqrt(real n ^ 2 - 1)) / pi)})
-             \<union> (\<Union>m \<in> Ints. \<Union>n \<in> {0<..}. {Complex m (-ln(n + sqrt(real n ^ 2 - 1)) / pi)})"
-  shows "cos(pi * cos(pi * z)) = 1 \<or> cos(pi * cos(pi * z)) = -1"
-proof -
-  have sqrt2 [simp]: "complex_of_real (sqrt x) * complex_of_real (sqrt x) = x" if "x \<ge> 0" for x::real
-    by (metis abs_of_nonneg of_real_mult real_sqrt_mult_self that)
-  have 1: "\<exists>k. exp (\<i> * (of_int m * complex_of_real pi) -
-                 (ln (real n + sqrt ((real n)\<^sup>2 - 1)))) +
-            inverse
-             (exp (\<i> * (of_int m * complex_of_real pi) -
-                    (ln (real n + sqrt ((real n)\<^sup>2 - 1))))) = of_int k * 2"
-         if "n > 0" for m n
-  proof -
-    have eeq: "e \<noteq> 0 \<Longrightarrow> e + inverse e = n*2 \<longleftrightarrow> inverse e^2 - 2 * n*inverse e + 1 = 0" for n e::complex
-      by (auto simp: field_simps power2_eq_square)
-    have [simp]: "1 \<le> real n * real n"
-      by (metis One_nat_def add.commute nat_less_real_le of_nat_1 of_nat_Suc one_le_power power2_eq_square that)
-    show ?thesis
-      apply (simp add: eeq)
-      using Schottky_lemma1 [OF that]
-      apply (auto simp: eeq exp_diff exp_Euler exp_of_real algebra_simps sin_int_times_real cos_int_times_real)
-       apply (rule_tac x="int n" in exI)
-       apply (auto simp: power2_eq_square algebra_simps)
-       apply (rule_tac x="- int n" in exI)
-      apply (auto simp: power2_eq_square algebra_simps)
-      done
-  qed
-  have 2: "\<exists>k. exp (\<i> * (of_int m * complex_of_real pi) +
-                 (ln (real n + sqrt ((real n)\<^sup>2 - 1)))) +
-            inverse
-             (exp (\<i> * (of_int m * complex_of_real pi) +
-                    (ln (real n + sqrt ((real n)\<^sup>2 - 1))))) = of_int k * 2"
-            if "n > 0" for m n
-  proof -
-    have eeq: "e \<noteq> 0 \<Longrightarrow> e + inverse e = n*2 \<longleftrightarrow> e^2 - 2 * n*e + 1 = 0" for n e::complex
-      by (auto simp: field_simps power2_eq_square)
-    have [simp]: "1 \<le> real n * real n"
-      by (metis One_nat_def add.commute nat_less_real_le of_nat_1 of_nat_Suc one_le_power power2_eq_square that)
-    show ?thesis
-      apply (simp add: eeq)
-      using Schottky_lemma1 [OF that]
-      apply (auto simp: exp_add exp_Euler exp_of_real algebra_simps sin_int_times_real cos_int_times_real)
-       apply (rule_tac x="int n" in exI)
-       apply (auto simp: power2_eq_square algebra_simps)
-       apply (rule_tac x="- int n" in exI)
-      apply (auto simp: power2_eq_square algebra_simps)
-      done
-  qed
-  have "\<exists>x. cos (complex_of_real pi * z) = of_int x"
-    using assms
-    apply safe
-      apply (auto simp: Ints_def cos_exp_eq exp_minus Complex_eq)
-     apply (auto simp: algebra_simps dest: 1 2)
-      done
-  then have "sin(pi * cos(pi * z)) ^ 2 = 0"
-    by (simp add: Complex_Transcendental.sin_eq_0)
-  then have "1 - cos(pi * cos(pi * z)) ^ 2 = 0"
-    by (simp add: sin_squared_eq)
-  then show ?thesis
-    using power2_eq_1_iff by auto
-qed
-
-
-theorem Schottky:
-  assumes holf: "f holomorphic_on cball 0 1"
-      and nof0: "norm(f 0) \<le> r"
-      and not01: "\<And>z. z \<in> cball 0 1 \<Longrightarrow> \<not>(f z = 0 \<or> f z = 1)"
-      and "0 < t" "t < 1" "norm z \<le> t"
-    shows "norm(f z) \<le> exp(pi * exp(pi * (2 + 2 * r + 12 * t / (1 - t))))"
-proof -
-  obtain h where holf: "h holomorphic_on cball 0 1"
-             and nh0: "norm (h 0) \<le> 1 + norm(2 * f 0 - 1) / 3"
-             and h:   "\<And>z. z \<in> cball 0 1 \<Longrightarrow> 2 * f z - 1 = cos(of_real pi * h z)"
-  proof (rule Schottky_lemma0 [of "\<lambda>z. 2 * f z - 1" "cball 0 1" 0])
-    show "(\<lambda>z. 2 * f z - 1) holomorphic_on cball 0 1"
-      by (intro holomorphic_intros holf)
-    show "contractible (cball (0::complex) 1)"
-      by (auto simp: convex_imp_contractible)
-    show "\<And>z. z \<in> cball 0 1 \<Longrightarrow> 2 * f z - 1 \<noteq> 1 \<and> 2 * f z - 1 \<noteq> - 1"
-      using not01 by force
-  qed auto
-  obtain g where holg: "g holomorphic_on cball 0 1"
-             and ng0:  "norm(g 0) \<le> 1 + norm(h 0) / 3"
-             and g:    "\<And>z. z \<in> cball 0 1 \<Longrightarrow> h z = cos(of_real pi * g z)"
-  proof (rule Schottky_lemma0 [OF holf convex_imp_contractible, of 0])
-    show "\<And>z. z \<in> cball 0 1 \<Longrightarrow> h z \<noteq> 1 \<and> h z \<noteq> - 1"
-      using h not01 by fastforce+
-  qed auto
-  have g0_2_f0: "norm(g 0) \<le> 2 + norm(f 0)"
-  proof -
-    have "cmod (2 * f 0 - 1) \<le> cmod (2 * f 0) + 1"
-      by (metis norm_one norm_triangle_ineq4)
-    also have "... \<le> 2 + cmod (f 0) * 3"
-      by simp
-    finally have "1 + norm(2 * f 0 - 1) / 3 \<le> (2 + norm(f 0) - 1) * 3"
-      apply (simp add: field_split_simps)
-      using norm_ge_zero [of "f 0 * 2 - 1"]
-      by linarith
-    with nh0 have "norm(h 0) \<le> (2 + norm(f 0) - 1) * 3"
-      by linarith
-    then have "1 + norm(h 0) / 3 \<le> 2 + norm(f 0)"
-      by simp
-    with ng0 show ?thesis
-      by auto
-  qed
-  have "z \<in> ball 0 1"
-    using assms by auto
-  have norm_g_12: "norm(g z - g 0) \<le> (12 * t) / (1 - t)"
-  proof -
-    obtain g' where g': "\<And>x. x \<in> cball 0 1 \<Longrightarrow> (g has_field_derivative g' x) (at x within cball 0 1)"
-      using holg [unfolded holomorphic_on_def field_differentiable_def] by metis
-    have int_g': "(g' has_contour_integral g z - g 0) (linepath 0 z)"
-      using contour_integral_primitive [OF g' valid_path_linepath, of 0 z]
-      using \<open>z \<in> ball 0 1\<close> segment_bound1 by fastforce
-    have "cmod (g' w) \<le> 12 / (1 - t)" if "w \<in> closed_segment 0 z" for w
-    proof -
-      have w: "w \<in> ball 0 1"
-        using segment_bound [OF that] \<open>z \<in> ball 0 1\<close> by simp
-      have ttt: "\<And>z. z \<in> frontier (cball 0 1) \<Longrightarrow> 1 - t \<le> dist w z"
-        using \<open>norm z \<le> t\<close> segment_bound1 [OF \<open>w \<in> closed_segment 0 z\<close>]
-        apply (simp add: dist_complex_def)
-        by (metis diff_left_mono dist_commute dist_complex_def norm_triangle_ineq2 order_trans)
-      have *: "\<lbrakk>\<And>b. (\<exists>w \<in> T \<union> U. w \<in> ball b 1); \<And>x. x \<in> D \<Longrightarrow> g x \<notin> T \<union> U\<rbrakk> \<Longrightarrow> \<nexists>b. ball b 1 \<subseteq> g ` D" for T U D
-        by force
-      have "\<nexists>b. ball b 1 \<subseteq> g ` cball 0 1"
-      proof (rule *)
-        show "(\<exists>w \<in> (\<Union>m \<in> Ints. \<Union>n \<in> {0<..}. {Complex m (ln(n + sqrt(real n ^ 2 - 1)) / pi)}) \<union>
-                    (\<Union>m \<in> Ints. \<Union>n \<in> {0<..}. {Complex m (-ln(n + sqrt(real n ^ 2 - 1)) / pi)}). w \<in> ball b 1)" for b
-        proof -
-          obtain m where m: "m \<in> \<int>" "\<bar>Re b - m\<bar> \<le> 1/2"
-            by (metis Ints_of_int abs_minus_commute of_int_round_abs_le)
-          show ?thesis
-          proof (cases "0::real" "Im b" rule: le_cases)
-            case le
-            then obtain n where "0 < n" and n: "\<bar>Im b - ln (n + sqrt ((real n)\<^sup>2 - 1)) / pi\<bar> < 1/2"
-              using Schottky_lemma2 [of "Im b"] by blast
-            have "dist b (Complex m (Im b)) \<le> 1/2"
-              by (metis cancel_comm_monoid_add_class.diff_cancel cmod_eq_Re complex.sel(1) complex.sel(2) dist_norm m(2) minus_complex.code)
-            moreover
-            have "dist (Complex m (Im b)) (Complex m (ln (n + sqrt ((real n)\<^sup>2 - 1)) / pi)) < 1/2"
-              using n by (simp add: complex_norm cmod_eq_Re complex_diff dist_norm del: Complex_eq)
-            ultimately have "dist b (Complex m (ln (real n + sqrt ((real n)\<^sup>2 - 1)) / pi)) < 1"
-              by (simp add: dist_triangle_lt [of b "Complex m (Im b)"] dist_commute)
-            with le m \<open>0 < n\<close> show ?thesis
-              apply (rule_tac x = "Complex m (ln (real n + sqrt ((real n)\<^sup>2 - 1)) / pi)" in bexI)
-               apply (simp_all del: Complex_eq greaterThan_0)
-              by blast
-          next
-            case ge
-            then obtain n where "0 < n" and n: "\<bar>- Im b - ln (real n + sqrt ((real n)\<^sup>2 - 1)) / pi\<bar> < 1/2"
-              using Schottky_lemma2 [of "- Im b"] by auto
-            have "dist b (Complex m (Im b)) \<le> 1/2"
-              by (metis cancel_comm_monoid_add_class.diff_cancel cmod_eq_Re complex.sel(1) complex.sel(2) dist_norm m(2) minus_complex.code)
-            moreover
-            have "dist (Complex m (- ln (n + sqrt ((real n)\<^sup>2 - 1)) / pi)) (Complex m (Im b)) < 1/2"
-              using n
-              apply (simp add: complex_norm cmod_eq_Re complex_diff dist_norm del: Complex_eq)
-              by (metis add.commute add_uminus_conv_diff)
-            ultimately have "dist b (Complex m (- ln (real n + sqrt ((real n)\<^sup>2 - 1)) / pi)) < 1"
-              by (simp add: dist_triangle_lt [of b "Complex m (Im b)"] dist_commute)
-            with ge m \<open>0 < n\<close> show ?thesis
-              apply (rule_tac x = "Complex m (- ln (real n + sqrt ((real n)\<^sup>2 - 1)) / pi)" in bexI)
-               apply (simp_all del: Complex_eq greaterThan_0)
-              by blast
-          qed
-        qed
-        show "g v \<notin> (\<Union>m \<in> Ints. \<Union>n \<in> {0<..}. {Complex m (ln(n + sqrt(real n ^ 2 - 1)) / pi)}) \<union>
-                    (\<Union>m \<in> Ints. \<Union>n \<in> {0<..}. {Complex m (-ln(n + sqrt(real n ^ 2 - 1)) / pi)})"
-             if "v \<in> cball 0 1" for v
-          using not01 [OF that]
-          by (force simp: g [OF that, symmetric] h [OF that, symmetric] dest!: Schottky_lemma3 [of "g v"])
-      qed
-      then have 12: "(1 - t) * cmod (deriv g w) / 12 < 1"
-        using Bloch_general [OF holg _ ttt, of 1] w by force
-      have "g field_differentiable at w within cball 0 1"
-        using holg w by (simp add: holomorphic_on_def)
-      then have "g field_differentiable at w within ball 0 1"
-        using ball_subset_cball field_differentiable_within_subset by blast
-      with w have der_gw: "(g has_field_derivative deriv g w) (at w)"
-        by (simp add: field_differentiable_within_open [of _ "ball 0 1"] field_differentiable_derivI)
-      with DERIV_unique [OF der_gw] g' w have "deriv g w = g' w"
-        by (metis open_ball at_within_open ball_subset_cball has_field_derivative_subset subsetCE)
-      then show "cmod (g' w) \<le> 12 / (1 - t)"
-        using g' 12 \<open>t < 1\<close> by (simp add: field_simps)
-    qed
-    then have "cmod (g z - g 0) \<le> 12 / (1 - t) * cmod z"
-      using has_contour_integral_bound_linepath [OF int_g', of "12/(1 - t)"] assms
-      by simp
-    with \<open>cmod z \<le> t\<close> \<open>t < 1\<close> show ?thesis
-      by (simp add: field_split_simps)
-  qed
-  have fz: "f z = (1 + cos(of_real pi * h z)) / 2"
-    using h \<open>z \<in> ball 0 1\<close> by (auto simp: field_simps)
-  have "cmod (f z) \<le> exp (cmod (complex_of_real pi * h z))"
-    by (simp add: fz mult.commute norm_cos_plus1_le)
-  also have "... \<le> exp (pi * exp (pi * (2 + 2 * r + 12 * t / (1 - t))))"
-  proof (simp add: norm_mult)
-    have "cmod (g z - g 0) \<le> 12 * t / (1 - t)"
-      using norm_g_12 \<open>t < 1\<close> by (simp add: norm_mult)
-    then have "cmod (g z) - cmod (g 0) \<le> 12 * t / (1 - t)"
-      using norm_triangle_ineq2 order_trans by blast
-    then have *: "cmod (g z) \<le> 2 + 2 * r + 12 * t / (1 - t)"
-      using g0_2_f0 norm_ge_zero [of "f 0"] nof0
-        by linarith
-    have "cmod (h z) \<le> exp (cmod (complex_of_real pi * g z))"
-      using \<open>z \<in> ball 0 1\<close> by (simp add: g norm_cos_le)
-    also have "... \<le> exp (pi * (2 + 2 * r + 12 * t / (1 - t)))"
-      using \<open>t < 1\<close> nof0 * by (simp add: norm_mult)
-    finally show "cmod (h z) \<le> exp (pi * (2 + 2 * r + 12 * t / (1 - t)))" .
-  qed
-  finally show ?thesis .
-qed
-
-  
-subsection\<open>The Little Picard Theorem\<close>
-
-theorem Landau_Picard:
-  obtains R
-    where "\<And>z. 0 < R z"
-          "\<And>f. \<lbrakk>f holomorphic_on cball 0 (R(f 0));
-                 \<And>z. norm z \<le> R(f 0) \<Longrightarrow> f z \<noteq> 0 \<and> f z \<noteq> 1\<rbrakk> \<Longrightarrow> norm(deriv f 0) < 1"
-proof -
-  define R where "R \<equiv> \<lambda>z. 3 * exp(pi * exp(pi*(2 + 2 * cmod z + 12)))"
-  show ?thesis
-  proof
-    show Rpos: "\<And>z. 0 < R z"
-      by (auto simp: R_def)
-    show "norm(deriv f 0) < 1"
-         if holf: "f holomorphic_on cball 0 (R(f 0))"
-         and Rf:  "\<And>z. norm z \<le> R(f 0) \<Longrightarrow> f z \<noteq> 0 \<and> f z \<noteq> 1" for f
-    proof -
-      let ?r = "R(f 0)"
-      define g where "g \<equiv> f \<circ> (\<lambda>z. of_real ?r * z)"
-      have "0 < ?r"
-        using Rpos by blast
-      have holg: "g holomorphic_on cball 0 1"
-        unfolding g_def
-        apply (intro holomorphic_intros holomorphic_on_compose holomorphic_on_subset [OF holf])
-        using Rpos by (auto simp: less_imp_le norm_mult)
-      have *: "norm(g z) \<le> exp(pi * exp(pi * (2 + 2 * norm (f 0) + 12 * t / (1 - t))))"
-           if "0 < t" "t < 1" "norm z \<le> t" for t z
-      proof (rule Schottky [OF holg])
-        show "cmod (g 0) \<le> cmod (f 0)"
-          by (simp add: g_def)
-        show "\<And>z. z \<in> cball 0 1 \<Longrightarrow> \<not> (g z = 0 \<or> g z = 1)"
-          using Rpos by (simp add: g_def less_imp_le norm_mult Rf)
-      qed (auto simp: that)
-      have C1: "g holomorphic_on ball 0 (1 / 2)"
-        by (rule holomorphic_on_subset [OF holg]) auto
-      have C2: "continuous_on (cball 0 (1 / 2)) g"
-        by (meson cball_divide_subset_numeral holg holomorphic_on_imp_continuous_on holomorphic_on_subset)
-      have C3: "cmod (g z) \<le> R (f 0) / 3" if "cmod (0 - z) = 1/2" for z
-      proof -
-        have "norm(g z) \<le> exp(pi * exp(pi * (2 + 2 * norm (f 0) + 12)))"
-          using * [of "1/2"] that by simp
-        also have "... = ?r / 3"
-          by (simp add: R_def)
-        finally show ?thesis .
-      qed
-      then have cmod_g'_le: "cmod (deriv g 0) * 3 \<le> R (f 0) * 2"
-        using Cauchy_inequality [OF C1 C2 _ C3, of 1] by simp
-      have holf': "f holomorphic_on ball 0 (R(f 0))"
-        by (rule holomorphic_on_subset [OF holf]) auto
-      then have fd0: "f field_differentiable at 0"
-        by (rule holomorphic_on_imp_differentiable_at [OF _ open_ball])
-           (auto simp: Rpos [of "f 0"])
-      have g_eq: "deriv g 0 = of_real ?r * deriv f 0"
-        apply (rule DERIV_imp_deriv)
-        apply (simp add: g_def)
-        by (metis DERIV_chain DERIV_cmult_Id fd0 field_differentiable_derivI mult.commute mult_zero_right)
-      show ?thesis
-        using cmod_g'_le Rpos [of "f 0"]  by (simp add: g_eq norm_mult)
-    qed
-  qed
-qed
-
-lemma little_Picard_01:
-  assumes holf: "f holomorphic_on UNIV" and f01: "\<And>z. f z \<noteq> 0 \<and> f z \<noteq> 1"
-  obtains c where "f = (\<lambda>x. c)"
-proof -
-  obtain R
-    where Rpos: "\<And>z. 0 < R z"
-      and R:    "\<And>h. \<lbrakk>h holomorphic_on cball 0 (R(h 0));
-                      \<And>z. norm z \<le> R(h 0) \<Longrightarrow> h z \<noteq> 0 \<and> h z \<noteq> 1\<rbrakk> \<Longrightarrow> norm(deriv h 0) < 1"
-    using Landau_Picard by metis
-  have contf: "continuous_on UNIV f"
-    by (simp add: holf holomorphic_on_imp_continuous_on)
-  show ?thesis
-  proof (cases "\<forall>x. deriv f x = 0")
-    case True
-    obtain c where "\<And>x. f(x) = c"
-      apply (rule DERIV_zero_connected_constant [OF connected_UNIV open_UNIV finite.emptyI contf])
-       apply (metis True DiffE holf holomorphic_derivI open_UNIV, auto)
-      done
-    then show ?thesis
-      using that by auto
-  next
-    case False
-    then obtain w where w: "deriv f w \<noteq> 0" by auto
-    define fw where "fw \<equiv> (f \<circ> (\<lambda>z. w + z / deriv f w))"
-    have norm_let1: "norm(deriv fw 0) < 1"
-    proof (rule R)
-      show "fw holomorphic_on cball 0 (R (fw 0))"
-        unfolding fw_def
-        by (intro holomorphic_intros holomorphic_on_compose w holomorphic_on_subset [OF holf] subset_UNIV)
-      show "fw z \<noteq> 0 \<and> fw z \<noteq> 1" if "cmod z \<le> R (fw 0)" for z
-        using f01 by (simp add: fw_def)
-    qed
-    have "(fw has_field_derivative deriv f w * inverse (deriv f w)) (at 0)"
-      apply (simp add: fw_def)
-      apply (rule DERIV_chain)
-      using holf holomorphic_derivI apply force
-      apply (intro derivative_eq_intros w)
-          apply (auto simp: field_simps)
-      done
-    then show ?thesis
-      using norm_let1 w by (simp add: DERIV_imp_deriv)
-  qed
-qed
-
-
-theorem little_Picard:
-  assumes holf: "f holomorphic_on UNIV"
-      and "a \<noteq> b" "range f \<inter> {a,b} = {}"
-    obtains c where "f = (\<lambda>x. c)"
-proof -
-  let ?g = "\<lambda>x. 1/(b - a)*(f x - b) + 1"
-  obtain c where "?g = (\<lambda>x. c)"
-  proof (rule little_Picard_01)
-    show "?g holomorphic_on UNIV"
-      by (intro holomorphic_intros holf)
-    show "\<And>z. ?g z \<noteq> 0 \<and> ?g z \<noteq> 1"
-      using assms by (auto simp: field_simps)
-  qed auto
-  then have "?g x = c" for x
-    by meson
-  then have "f x = c * (b-a) + a" for x
-    using assms by (auto simp: field_simps)
-  then show ?thesis
-    using that by blast
-qed
-
-
-text\<open>A couple of little applications of Little Picard\<close>
-
-lemma holomorphic_periodic_fixpoint:
-  assumes holf: "f holomorphic_on UNIV"
-      and "p \<noteq> 0" and per: "\<And>z. f(z + p) = f z"
-  obtains x where "f x = x"
-proof -
-  have False if non: "\<And>x. f x \<noteq> x"
-  proof -
-    obtain c where "(\<lambda>z. f z - z) = (\<lambda>z. c)"
-    proof (rule little_Picard)
-      show "(\<lambda>z. f z - z) holomorphic_on UNIV"
-        by (simp add: holf holomorphic_on_diff)
-      show "range (\<lambda>z. f z - z) \<inter> {p,0} = {}"
-          using assms non by auto (metis add.commute diff_eq_eq)
-      qed (auto simp: assms)
-    with per show False
-      by (metis add.commute add_cancel_left_left \<open>p \<noteq> 0\<close> diff_add_cancel)
-  qed
-  then show ?thesis
-    using that by blast
-qed
-
-
-lemma holomorphic_involution_point:
-  assumes holfU: "f holomorphic_on UNIV" and non: "\<And>a. f \<noteq> (\<lambda>x. a + x)"
-  obtains x where "f(f x) = x"
-proof -
-  { assume non_ff [simp]: "\<And>x. f(f x) \<noteq> x"
-    then have non_fp [simp]: "f z \<noteq> z" for z
-      by metis
-    have holf: "f holomorphic_on X" for X
-      using assms holomorphic_on_subset by blast
-    obtain c where c: "(\<lambda>x. (f(f x) - x)/(f x - x)) = (\<lambda>x. c)"
-    proof (rule little_Picard_01)
-      show "(\<lambda>x. (f(f x) - x)/(f x - x)) holomorphic_on UNIV"
-        apply (intro holomorphic_intros holf holomorphic_on_compose [unfolded o_def, OF holf])
-        using non_fp by auto
-    qed auto
-    then obtain "c \<noteq> 0" "c \<noteq> 1"
-      by (metis (no_types, hide_lams) non_ff diff_zero divide_eq_0_iff right_inverse_eq)
-    have eq: "f(f x) - c * f x = x*(1 - c)" for x
-      using fun_cong [OF c, of x] by (simp add: field_simps)
-    have df_times_dff: "deriv f z * (deriv f (f z) - c) = 1 * (1 - c)" for z
-    proof (rule DERIV_unique)
-      show "((\<lambda>x. f (f x) - c * f x) has_field_derivative
-              deriv f z * (deriv f (f z) - c)) (at z)"
-        apply (intro derivative_eq_intros)
-            apply (rule DERIV_chain [unfolded o_def, of f])
-             apply (auto simp: algebra_simps intro!: holomorphic_derivI [OF holfU])
-        done
-      show "((\<lambda>x. f (f x) - c * f x) has_field_derivative 1 * (1 - c)) (at z)"
-        by (simp add: eq mult_commute_abs)
-    qed
-    { fix z::complex
-      obtain k where k: "deriv f \<circ> f = (\<lambda>x. k)"
-      proof (rule little_Picard)
-        show "(deriv f \<circ> f) holomorphic_on UNIV"
-          by (meson holfU holomorphic_deriv holomorphic_on_compose holomorphic_on_subset open_UNIV subset_UNIV)
-        obtain "deriv f (f x) \<noteq> 0" "deriv f (f x) \<noteq> c"  for x
-          using df_times_dff \<open>c \<noteq> 1\<close> eq_iff_diff_eq_0
-          by (metis lambda_one mult_zero_left mult_zero_right)
-        then show "range (deriv f \<circ> f) \<inter> {0,c} = {}"
-          by force
-      qed (use \<open>c \<noteq> 0\<close> in auto)
-      have "\<not> f constant_on UNIV"
-        by (meson UNIV_I non_ff constant_on_def)
-      with holf open_mapping_thm have "open(range f)"
-        by blast
-      obtain l where l: "\<And>x. f x - k * x = l"
-      proof (rule DERIV_zero_connected_constant [of UNIV "{}" "\<lambda>x. f x - k * x"], simp_all)
-        have "deriv f w - k = 0" for w
-        proof (rule analytic_continuation [OF _ open_UNIV connected_UNIV subset_UNIV, of "\<lambda>z. deriv f z - k" "f z" "range f" w])
-          show "(\<lambda>z. deriv f z - k) holomorphic_on UNIV"
-            by (intro holomorphic_intros holf open_UNIV)
-          show "f z islimpt range f"
-            by (metis (no_types, lifting) IntI UNIV_I \<open>open (range f)\<close> image_eqI inf.absorb_iff2 inf_aci(1) islimpt_UNIV islimpt_eq_acc_point open_Int top_greatest)
-          show "\<And>z. z \<in> range f \<Longrightarrow> deriv f z - k = 0"
-            by (metis comp_def diff_self image_iff k)
-        qed auto
-        moreover
-        have "((\<lambda>x. f x - k * x) has_field_derivative deriv f x - k) (at x)" for x
-          by (metis DERIV_cmult_Id Deriv.field_differentiable_diff UNIV_I field_differentiable_derivI holf holomorphic_on_def)
-        ultimately
-        show "\<forall>x. ((\<lambda>x. f x - k * x) has_field_derivative 0) (at x)"
-          by auto
-        show "continuous_on UNIV (\<lambda>x. f x - k * x)"
-          by (simp add: continuous_on_diff holf holomorphic_on_imp_continuous_on)
-      qed (auto simp: connected_UNIV)
-      have False
-      proof (cases "k=1")
-        case True
-        then have "\<exists>x. k * x + l \<noteq> a + x" for a
-          using l non [of a] ext [of f "(+) a"]
-          by (metis add.commute diff_eq_eq)
-        with True show ?thesis by auto
-      next
-        case False
-        have "\<And>x. (1 - k) * x \<noteq> f 0"
-          using l [of 0] apply (simp add: algebra_simps)
-          by (metis diff_add_cancel l mult.commute non_fp)
-        then show False
-          by (metis False eq_iff_diff_eq_0 mult.commute nonzero_mult_div_cancel_right times_divide_eq_right)
-      qed
-    }
-  }
-  then show thesis
-    using that by blast
-qed
-
-
-subsection\<open>The Arzelà--Ascoli theorem\<close>
-
-lemma subsequence_diagonalization_lemma:
-  fixes P :: "nat \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> bool"
-  assumes sub: "\<And>i r. \<exists>k. strict_mono (k :: nat \<Rightarrow> nat) \<and> P i (r \<circ> k)"
-      and P_P:  "\<And>i r::nat \<Rightarrow> 'a. \<And>k1 k2 N.
-                   \<lbrakk>P i (r \<circ> k1); \<And>j. N \<le> j \<Longrightarrow> \<exists>j'. j \<le> j' \<and> k2 j = k1 j'\<rbrakk> \<Longrightarrow> P i (r \<circ> k2)"
-   obtains k where "strict_mono (k :: nat \<Rightarrow> nat)" "\<And>i. P i (r \<circ> k)"
-proof -
-  obtain kk where "\<And>i r. strict_mono (kk i r :: nat \<Rightarrow> nat) \<and> P i (r \<circ> (kk i r))"
-    using sub by metis
-  then have sub_kk: "\<And>i r. strict_mono (kk i r)" and P_kk: "\<And>i r. P i (r \<circ> (kk i r))"
-    by auto
-  define rr where "rr \<equiv> rec_nat (kk 0 r) (\<lambda>n x. x \<circ> kk (Suc n) (r \<circ> x))"
-  then have [simp]: "rr 0 = kk 0 r" "\<And>n. rr(Suc n) = rr n \<circ> kk (Suc n) (r \<circ> rr n)"
-    by auto
-  show thesis
-  proof
-    have sub_rr: "strict_mono (rr i)" for i
-      using sub_kk  by (induction i) (auto simp: strict_mono_def o_def)
-    have P_rr: "P i (r \<circ> rr i)" for i
-      using P_kk  by (induction i) (auto simp: o_def)
-    have "i \<le> i+d \<Longrightarrow> rr i n \<le> rr (i+d) n" for d i n
-    proof (induction d)
-      case 0 then show ?case
-        by simp
-    next
-      case (Suc d) then show ?case
-        apply simp
-          using seq_suble [OF sub_kk] order_trans strict_mono_less_eq [OF sub_rr] by blast
-    qed
-    then have "\<And>i j n. i \<le> j \<Longrightarrow> rr i n \<le> rr j n"
-      by (metis le_iff_add)
-    show "strict_mono (\<lambda>n. rr n n)"
-      apply (simp add: strict_mono_Suc_iff)
-      by (meson lessI less_le_trans seq_suble strict_monoD sub_kk sub_rr)
-    have "\<exists>j. i \<le> j \<and> rr (n+d) i = rr n j" for d n i
-      apply (induction d arbitrary: i, auto)
-      by (meson order_trans seq_suble sub_kk)
-    then have "\<And>m n i. n \<le> m \<Longrightarrow> \<exists>j. i \<le> j \<and> rr m i = rr n j"
-      by (metis le_iff_add)
-    then show "P i (r \<circ> (\<lambda>n. rr n n))" for i
-      by (meson P_rr P_P)
-  qed
-qed
-
-lemma function_convergent_subsequence:
-  fixes f :: "[nat,'a] \<Rightarrow> 'b::{real_normed_vector,heine_borel}"
-  assumes "countable S" and M: "\<And>n::nat. \<And>x. x \<in> S \<Longrightarrow> norm(f n x) \<le> M"
-   obtains k where "strict_mono (k::nat\<Rightarrow>nat)" "\<And>x. x \<in> S \<Longrightarrow> \<exists>l. (\<lambda>n. f (k n) x) \<longlonglongrightarrow> l"
-proof (cases "S = {}")
-  case True
-  then show ?thesis
-    using strict_mono_id that by fastforce
-next
-  case False
-  with \<open>countable S\<close> obtain \<sigma> :: "nat \<Rightarrow> 'a" where \<sigma>: "S = range \<sigma>"
-    using uncountable_def by blast
-  obtain k where "strict_mono k" and k: "\<And>i. \<exists>l. (\<lambda>n. (f \<circ> k) n (\<sigma> i)) \<longlonglongrightarrow> l"
-  proof (rule subsequence_diagonalization_lemma
-      [of "\<lambda>i r. \<exists>l. ((\<lambda>n. (f \<circ> r) n (\<sigma> i)) \<longlongrightarrow> l) sequentially" id])
-    show "\<exists>k::nat\<Rightarrow>nat. strict_mono k \<and> (\<exists>l. (\<lambda>n. (f \<circ> (r \<circ> k)) n (\<sigma> i)) \<longlonglongrightarrow> l)" for i r
-    proof -
-      have "f (r n) (\<sigma> i) \<in> cball 0 M" for n
-        by (simp add: \<sigma> M)
-      then show ?thesis
-        using compact_def [of "cball (0::'b) M"] apply simp
-        apply (drule_tac x="(\<lambda>n. f (r n) (\<sigma> i))" in spec)
-        apply (force simp: o_def)
-        done
-    qed
-    show "\<And>i r k1 k2 N.
-               \<lbrakk>\<exists>l. (\<lambda>n. (f \<circ> (r \<circ> k1)) n (\<sigma> i)) \<longlonglongrightarrow> l; \<And>j. N \<le> j \<Longrightarrow> \<exists>j'\<ge>j. k2 j = k1 j'\<rbrakk>
-               \<Longrightarrow> \<exists>l. (\<lambda>n. (f \<circ> (r \<circ> k2)) n (\<sigma> i)) \<longlonglongrightarrow> l"
-      apply (simp add: lim_sequentially)
-      apply (erule ex_forward all_forward imp_forward)+
-        apply auto
-      by (metis (no_types, hide_lams) le_cases order_trans)
-  qed auto
-  with \<sigma> that show ?thesis
-    by force
-qed
-
-
-theorem Arzela_Ascoli:
-  fixes \<F> :: "[nat,'a::euclidean_space] \<Rightarrow> 'b::{real_normed_vector,heine_borel}"
-  assumes "compact S"
-      and M: "\<And>n x. x \<in> S \<Longrightarrow> norm(\<F> n x) \<le> M"
-      and equicont:
-          "\<And>x e. \<lbrakk>x \<in> S; 0 < e\<rbrakk>
-                 \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>n y. y \<in> S \<and> norm(x - y) < d \<longrightarrow> norm(\<F> n x - \<F> n y) < e)"
-  obtains g k where "continuous_on S g" "strict_mono (k :: nat \<Rightarrow> nat)"
-                    "\<And>e. 0 < e \<Longrightarrow> \<exists>N. \<forall>n x. n \<ge> N \<and> x \<in> S \<longrightarrow> norm(\<F>(k n) x - g x) < e"
-proof -
-  have UEQ: "\<And>e. 0 < e \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>n. \<forall>x \<in> S. \<forall>x' \<in> S. dist x' x < d \<longrightarrow> dist (\<F> n x') (\<F> n x) < e)"
-    apply (rule compact_uniformly_equicontinuous [OF \<open>compact S\<close>, of "range \<F>"])
-    using equicont by (force simp: dist_commute dist_norm)+
-  have "continuous_on S g"
-       if "\<And>e. 0 < e \<Longrightarrow> \<exists>N. \<forall>n x. n \<ge> N \<and> x \<in> S \<longrightarrow> norm(\<F>(r n) x - g x) < e"
-       for g:: "'a \<Rightarrow> 'b" and r :: "nat \<Rightarrow> nat"
-  proof (rule uniform_limit_theorem [of _ "\<F> \<circ> r"])
-    show "\<forall>\<^sub>F n in sequentially. continuous_on S ((\<F> \<circ> r) n)"
-      apply (simp add: eventually_sequentially)
-      apply (rule_tac x=0 in exI)
-      using UEQ apply (force simp: continuous_on_iff)
-      done
-    show "uniform_limit S (\<F> \<circ> r) g sequentially"
-      apply (simp add: uniform_limit_iff eventually_sequentially)
-        by (metis dist_norm that)
-  qed auto
-  moreover
-  obtain R where "countable R" "R \<subseteq> S" and SR: "S \<subseteq> closure R"
-    by (metis separable that)
-  obtain k where "strict_mono k" and k: "\<And>x. x \<in> R \<Longrightarrow> \<exists>l. (\<lambda>n. \<F> (k n) x) \<longlonglongrightarrow> l"
-    apply (rule function_convergent_subsequence [OF \<open>countable R\<close> M])
-    using \<open>R \<subseteq> S\<close> apply force+
-    done
-  then have Cauchy: "Cauchy ((\<lambda>n. \<F> (k n) x))" if "x \<in> R" for x
-    using convergent_eq_Cauchy that by blast
-  have "\<exists>N. \<forall>m n x. N \<le> m \<and> N \<le> n \<and> x \<in> S \<longrightarrow> dist ((\<F> \<circ> k) m x) ((\<F> \<circ> k) n x) < e"
-    if "0 < e" for e
-  proof -
-    obtain d where "0 < d"
-      and d: "\<And>n. \<forall>x \<in> S. \<forall>x' \<in> S. dist x' x < d \<longrightarrow> dist (\<F> n x') (\<F> n x) < e/3"
-      by (metis UEQ \<open>0 < e\<close> divide_pos_pos zero_less_numeral)
-    obtain T where "T \<subseteq> R" and "finite T" and T: "S \<subseteq> (\<Union>c\<in>T. ball c d)"
-    proof (rule compactE_image [OF  \<open>compact S\<close>, of R "(\<lambda>x. ball x d)"])
-      have "closure R \<subseteq> (\<Union>c\<in>R. ball c d)"
-        apply clarsimp
-        using \<open>0 < d\<close> closure_approachable by blast
-      with SR show "S \<subseteq> (\<Union>c\<in>R. ball c d)"
-        by auto
-    qed auto
-    have "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (\<F> (k m) x) (\<F> (k n) x) < e/3" if "x \<in> R" for x
-      using Cauchy \<open>0 < e\<close> that unfolding Cauchy_def
-      by (metis less_divide_eq_numeral1(1) mult_zero_left)
-    then obtain MF where MF: "\<And>x m n. \<lbrakk>x \<in> R; m \<ge> MF x; n \<ge> MF x\<rbrakk> \<Longrightarrow> norm (\<F> (k m) x - \<F> (k n) x) < e/3"
-      using dist_norm by metis
-    have "dist ((\<F> \<circ> k) m x) ((\<F> \<circ> k) n x) < e"
-         if m: "Max (MF ` T) \<le> m" and n: "Max (MF ` T) \<le> n" "x \<in> S" for m n x
-    proof -
-      obtain t where "t \<in> T" and t: "x \<in> ball t d"
-        using \<open>x \<in> S\<close> T by auto
-      have "norm(\<F> (k m) t - \<F> (k m) x) < e / 3"
-        by (metis \<open>R \<subseteq> S\<close> \<open>T \<subseteq> R\<close> \<open>t \<in> T\<close> d dist_norm mem_ball subset_iff t \<open>x \<in> S\<close>)
-      moreover
-      have "norm(\<F> (k n) t - \<F> (k n) x) < e / 3"
-        by (metis \<open>R \<subseteq> S\<close> \<open>T \<subseteq> R\<close> \<open>t \<in> T\<close> subsetD d dist_norm mem_ball t \<open>x \<in> S\<close>)
-      moreover
-      have "norm(\<F> (k m) t - \<F> (k n) t) < e / 3"
-      proof (rule MF)
-        show "t \<in> R"
-          using \<open>T \<subseteq> R\<close> \<open>t \<in> T\<close> by blast
-        show "MF t \<le> m" "MF t \<le> n"
-          by (meson Max_ge \<open>finite T\<close> \<open>t \<in> T\<close> finite_imageI imageI le_trans m n)+
-      qed
-      ultimately
-      show ?thesis
-        unfolding dist_norm [symmetric] o_def
-          by (metis dist_triangle_third dist_commute)
-    qed
-    then show ?thesis
-      by force
-  qed
-  then have "\<exists>g. \<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x \<in> S. norm(\<F>(k n) x - g x) < e"
-    using uniformly_convergent_eq_cauchy [of "\<lambda>x. x \<in> S" "\<F> \<circ> k"]
-    apply (simp add: o_def dist_norm)
-    by meson
-  ultimately show thesis
-    by (metis that \<open>strict_mono k\<close>)
-qed
-
-
-
-subsubsection\<^marker>\<open>tag important\<close>\<open>Montel's theorem\<close>
-
-text\<open>a sequence of holomorphic functions uniformly bounded
-on compact subsets of an open set S has a subsequence that converges to a
-holomorphic function, and converges \emph{uniformly} on compact subsets of S.\<close>
-
-
-theorem Montel:
-  fixes \<F> :: "[nat,complex] \<Rightarrow> complex"
-  assumes "open S"
-      and \<H>: "\<And>h. h \<in> \<H> \<Longrightarrow> h holomorphic_on S"
-      and bounded: "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> \<exists>B. \<forall>h \<in> \<H>. \<forall> z \<in> K. norm(h z) \<le> B"
-      and rng_f: "range \<F> \<subseteq> \<H>"
-  obtains g r
-    where "g holomorphic_on S" "strict_mono (r :: nat \<Rightarrow> nat)"
-          "\<And>x. x \<in> S \<Longrightarrow> ((\<lambda>n. \<F> (r n) x) \<longlongrightarrow> g x) sequentially"
-          "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> uniform_limit K (\<F> \<circ> r) g sequentially"        
-proof -
-  obtain K where comK: "\<And>n. compact(K n)" and KS: "\<And>n::nat. K n \<subseteq> S"
-             and subK: "\<And>X. \<lbrakk>compact X; X \<subseteq> S\<rbrakk> \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. X \<subseteq> K n"
-    using open_Union_compact_subsets [OF \<open>open S\<close>] by metis
-  then have "\<And>i. \<exists>B. \<forall>h \<in> \<H>. \<forall> z \<in> K i. norm(h z) \<le> B"
-    by (simp add: bounded)
-  then obtain B where B: "\<And>i h z. \<lbrakk>h \<in> \<H>; z \<in> K i\<rbrakk> \<Longrightarrow> norm(h z) \<le> B i"
-    by metis
-  have *: "\<exists>r g. strict_mono (r::nat\<Rightarrow>nat) \<and> (\<forall>e > 0. \<exists>N. \<forall>n\<ge>N. \<forall>x \<in> K i. norm((\<F> \<circ> r) n x - g x) < e)"
-        if "\<And>n. \<F> n \<in> \<H>" for \<F> i
-  proof -
-    obtain g k where "continuous_on (K i) g" "strict_mono (k::nat\<Rightarrow>nat)"
-                    "\<And>e. 0 < e \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. \<forall>x \<in> K i. norm(\<F>(k n) x - g x) < e"
-    proof (rule Arzela_Ascoli [of "K i" "\<F>" "B i"])
-      show "\<exists>d>0. \<forall>n y. y \<in> K i \<and> cmod (z - y) < d \<longrightarrow> cmod (\<F> n z - \<F> n y) < e"
-             if z: "z \<in> K i" and "0 < e" for z e
-      proof -
-        obtain r where "0 < r" and r: "cball z r \<subseteq> S"
-          using z KS [of i] \<open>open S\<close> by (force simp: open_contains_cball)
-        have "cball z (2 / 3 * r) \<subseteq> cball z r"
-          using \<open>0 < r\<close> by (simp add: cball_subset_cball_iff)
-        then have z23S: "cball z (2 / 3 * r) \<subseteq> S"
-          using r by blast
-        obtain M where "0 < M" and M: "\<And>n w. dist z w \<le> 2/3 * r \<Longrightarrow> norm(\<F> n w) \<le> M"
-        proof -
-          obtain N where N: "\<forall>n\<ge>N. cball z (2/3 * r) \<subseteq> K n"
-            using subK compact_cball [of z "(2 / 3 * r)"] z23S by force
-          have "cmod (\<F> n w) \<le> \<bar>B N\<bar> + 1" if "dist z w \<le> 2 / 3 * r" for n w
-          proof -
-            have "w \<in> K N"
-              using N mem_cball that by blast
-            then have "cmod (\<F> n w) \<le> B N"
-              using B \<open>\<And>n. \<F> n \<in> \<H>\<close> by blast
-            also have "... \<le> \<bar>B N\<bar> + 1"
-              by simp
-            finally show ?thesis .
-          qed
-          then show ?thesis
-            by (rule_tac M="\<bar>B N\<bar> + 1" in that) auto
-        qed
-        have "cmod (\<F> n z - \<F> n y) < e"
-              if "y \<in> K i" and y_near_z: "cmod (z - y) < r/3" "cmod (z - y) < e * r / (6 * M)"
-              for n y
-        proof -
-          have "((\<lambda>w. \<F> n w / (w - \<xi>)) has_contour_integral
-                    (2 * pi) * \<i> * winding_number (circlepath z (2 / 3 * r)) \<xi> * \<F> n \<xi>)
-                (circlepath z (2 / 3 * r))"
-             if "dist \<xi> z < (2 / 3 * r)" for \<xi>
-          proof (rule Cauchy_integral_formula_convex_simple)
-            have "\<F> n holomorphic_on S"
-              by (simp add: \<H> \<open>\<And>n. \<F> n \<in> \<H>\<close>)
-            with z23S show "\<F> n holomorphic_on cball z (2 / 3 * r)"
-              using holomorphic_on_subset by blast
-          qed (use that \<open>0 < r\<close> in \<open>auto simp: dist_commute\<close>)
-          then have *: "((\<lambda>w. \<F> n w / (w - \<xi>)) has_contour_integral (2 * pi) * \<i> * \<F> n \<xi>)
-                     (circlepath z (2 / 3 * r))"
-             if "dist \<xi> z < (2 / 3 * r)" for \<xi>
-            using that by (simp add: winding_number_circlepath dist_norm)
-           have y: "((\<lambda>w. \<F> n w / (w - y)) has_contour_integral (2 * pi) * \<i> * \<F> n y)
-                 (circlepath z (2 / 3 * r))"
-             apply (rule *)
-             using that \<open>0 < r\<close> by (simp only: dist_norm norm_minus_commute)
-           have z: "((\<lambda>w. \<F> n w / (w - z)) has_contour_integral (2 * pi) * \<i> * \<F> n z)
-                 (circlepath z (2 / 3 * r))"
-             apply (rule *)
-             using \<open>0 < r\<close> by simp
-           have le_er: "cmod (\<F> n x / (x - y) - \<F> n x / (x - z)) \<le> e / r"
-                if "cmod (x - z) = r/3 + r/3" for x
-           proof -
-             have "\<not> (cmod (x - y) < r/3)"
-               using y_near_z(1) that \<open>M > 0\<close> \<open>r > 0\<close>
-               by (metis (full_types) norm_diff_triangle_less norm_minus_commute order_less_irrefl)
-             then have r4_le_xy: "r/4 \<le> cmod (x - y)"
-               using \<open>r > 0\<close> by simp
-             then have neq: "x \<noteq> y" "x \<noteq> z"
-               using that \<open>r > 0\<close> by (auto simp: field_split_simps norm_minus_commute)
-             have leM: "cmod (\<F> n x) \<le> M"
-               by (simp add: M dist_commute dist_norm that)
-             have "cmod (\<F> n x / (x - y) - \<F> n x / (x - z)) = cmod (\<F> n x) * cmod (1 / (x - y) - 1 / (x - z))"
-               by (metis (no_types, lifting) divide_inverse mult.left_neutral norm_mult right_diff_distrib')
-             also have "... = cmod (\<F> n x) * cmod ((y - z) / ((x - y) * (x - z)))"
-               using neq by (simp add: field_split_simps)
-             also have "... = cmod (\<F> n x) * (cmod (y - z) / (cmod(x - y) * (2/3 * r)))"
-               by (simp add: norm_mult norm_divide that)
-             also have "... \<le> M * (cmod (y - z) / (cmod(x - y) * (2/3 * r)))"
-               apply (rule mult_mono)
-                  apply (rule leM)
-                 using \<open>r > 0\<close> \<open>M > 0\<close> neq by auto
-               also have "... < M * ((e * r / (6 * M)) / (cmod(x - y) * (2/3 * r)))"
-                 unfolding mult_less_cancel_left
-                 using y_near_z(2) \<open>M > 0\<close> \<open>r > 0\<close> neq
-                 apply (simp add: field_simps mult_less_0_iff norm_minus_commute)
-                 done
-             also have "... \<le> e/r"
-               using \<open>e > 0\<close> \<open>r > 0\<close> r4_le_xy by (simp add: field_split_simps)
-             finally show ?thesis by simp
-           qed
-           have "(2 * pi) * cmod (\<F> n y - \<F> n z) = cmod ((2 * pi) * \<i> * \<F> n y - (2 * pi) * \<i> * \<F> n z)"
-             by (simp add: right_diff_distrib [symmetric] norm_mult)
-           also have "cmod ((2 * pi) * \<i> * \<F> n y - (2 * pi) * \<i> * \<F> n z) \<le> e / r * (2 * pi * (2 / 3 * r))"
-             apply (rule has_contour_integral_bound_circlepath [OF has_contour_integral_diff [OF y z], of "e/r"])
-             using \<open>e > 0\<close> \<open>r > 0\<close> le_er by auto
-           also have "... = (2 * pi) * e * ((2 / 3))"
-             using \<open>r > 0\<close> by (simp add: field_split_simps)
-           finally have "cmod (\<F> n y - \<F> n z) \<le> e * (2 / 3)"
-             by simp
-           also have "... < e"
-             using \<open>e > 0\<close> by simp
-           finally show ?thesis by (simp add: norm_minus_commute)
-        qed
-        then show ?thesis
-          apply (rule_tac x="min (r/3) ((e * r)/(6 * M))" in exI)
-          using \<open>0 < e\<close> \<open>0 < r\<close> \<open>0 < M\<close> by simp
-      qed
-      show "\<And>n x.  x \<in> K i \<Longrightarrow> cmod (\<F> n x) \<le> B i"
-        using B \<open>\<And>n. \<F> n \<in> \<H>\<close> by blast
-    qed (use comK in \<open>fastforce+\<close>)
-    then show ?thesis
-      by fastforce
-  qed
-  have "\<exists>k g. strict_mono (k::nat\<Rightarrow>nat) \<and> (\<forall>e > 0. \<exists>N. \<forall>n\<ge>N. \<forall>x \<in> K i. norm((\<F> \<circ> r \<circ> k) n x - g x) < e)"
-         for i r
-    apply (rule *)
-    using rng_f by auto
-  then have **: "\<And>i r. \<exists>k. strict_mono (k::nat\<Rightarrow>nat) \<and> (\<exists>g. \<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x \<in> K i. norm((\<F> \<circ> (r \<circ> k)) n x - g x) < e)"
-    by (force simp: o_assoc)
-  obtain k :: "nat \<Rightarrow> nat" where "strict_mono k"
-             and "\<And>i. \<exists>g. \<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x\<in>K i. cmod ((\<F> \<circ> (id \<circ> k)) n x - g x) < e"
-    apply (rule subsequence_diagonalization_lemma [OF **, of id])
-     apply (erule ex_forward all_forward imp_forward)+
-      apply auto
-    apply (rule_tac x="max N Na" in exI, fastforce+)
-    done
-  then have lt_e: "\<And>i. \<exists>g. \<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x\<in>K i. cmod ((\<F> \<circ> k) n x - g x) < e"
-    by simp
-  have "\<exists>l. \<forall>e>0. \<exists>N. \<forall>n\<ge>N. norm(\<F> (k n) z - l) < e" if "z \<in> S" for z
-  proof -
-    obtain G where G: "\<And>i e. e > 0 \<Longrightarrow> \<exists>M. \<forall>n\<ge>M. \<forall>x\<in>K i. cmod ((\<F> \<circ> k) n x - G i x) < e"
-      using lt_e by metis
-    obtain N where "\<And>n. n \<ge> N \<Longrightarrow> z \<in> K n"
-      using subK [of "{z}"] that \<open>z \<in> S\<close> by auto
-    moreover have "\<And>e. e > 0 \<Longrightarrow> \<exists>M. \<forall>n\<ge>M. \<forall>x\<in>K N. cmod ((\<F> \<circ> k) n x - G N x) < e"
-      using G by auto
-    ultimately show ?thesis
-      by (metis comp_apply order_refl)
-  qed
-  then obtain g where g: "\<And>z e. \<lbrakk>z \<in> S; e > 0\<rbrakk> \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. norm(\<F> (k n) z - g z) < e"
-    by metis
-  show ?thesis
-  proof
-    show g_lim: "\<And>x. x \<in> S \<Longrightarrow> (\<lambda>n. \<F> (k n) x) \<longlonglongrightarrow> g x"
-      by (simp add: lim_sequentially g dist_norm)    
-    have dg_le_e: "\<exists>N. \<forall>n\<ge>N. \<forall>x\<in>T. cmod (\<F> (k n) x - g x) < e"
-      if T: "compact T" "T \<subseteq> S" and "0 < e" for T e
-    proof -
-      obtain N where N: "\<And>n. n \<ge> N \<Longrightarrow> T \<subseteq> K n"
-        using subK [OF T] by blast
-      obtain h where h: "\<And>e. e>0 \<Longrightarrow> \<exists>M. \<forall>n\<ge>M. \<forall>x\<in>K N. cmod ((\<F> \<circ> k) n x - h x) < e"
-        using lt_e by blast
-      have geq: "g w = h w" if "w \<in> T" for w
-        apply (rule LIMSEQ_unique [of "\<lambda>n. \<F>(k n) w"])
-        using \<open>T \<subseteq> S\<close> g_lim that apply blast
-        using h N that by (force simp: lim_sequentially dist_norm)
-      show ?thesis
-        using T h N \<open>0 < e\<close> by (fastforce simp add: geq)
-    qed
-    then show "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk>
-         \<Longrightarrow> uniform_limit K (\<F> \<circ> k) g sequentially"
-      by (simp add: uniform_limit_iff dist_norm eventually_sequentially)
-    show "g holomorphic_on S"
-    proof (rule holomorphic_uniform_sequence [OF \<open>open S\<close> \<H>])
-      show "\<And>n. (\<F> \<circ> k) n \<in> \<H>"
-        by (simp add: range_subsetD rng_f)
-      show "\<exists>d>0. cball z d \<subseteq> S \<and> uniform_limit (cball z d) (\<lambda>n. (\<F> \<circ> k) n) g sequentially"
-        if "z \<in> S" for z
-      proof -
-        obtain d where d: "d>0" "cball z d \<subseteq> S"
-          using \<open>open S\<close> \<open>z \<in> S\<close> open_contains_cball by blast
-        then have "uniform_limit (cball z d) (\<F> \<circ> k) g sequentially"
-          using dg_le_e compact_cball by (auto simp: uniform_limit_iff eventually_sequentially dist_norm)
-        with d show ?thesis by blast
-      qed
-    qed
-  qed (auto simp: \<open>strict_mono k\<close>)
-qed
-
-
-
-subsection\<open>Some simple but useful cases of Hurwitz's theorem\<close>
-
-proposition Hurwitz_no_zeros:
-  assumes S: "open S" "connected S"
-      and holf: "\<And>n::nat. \<F> n holomorphic_on S"
-      and holg: "g holomorphic_on S"
-      and ul_g: "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> uniform_limit K \<F> g sequentially"
-      and nonconst: "\<not> g constant_on S"
-      and nz: "\<And>n z. z \<in> S \<Longrightarrow> \<F> n z \<noteq> 0"
-      and "z0 \<in> S"
-      shows "g z0 \<noteq> 0"
-proof
-  assume g0: "g z0 = 0"
-  obtain h r m
-    where "0 < m" "0 < r" and subS: "ball z0 r \<subseteq> S"
-      and holh: "h holomorphic_on ball z0 r"
-      and geq:  "\<And>w. w \<in> ball z0 r \<Longrightarrow> g w = (w - z0)^m * h w"
-      and hnz:  "\<And>w. w \<in> ball z0 r \<Longrightarrow> h w \<noteq> 0"
-    by (blast intro: holomorphic_factor_zero_nonconstant [OF holg S \<open>z0 \<in> S\<close> g0 nonconst])
-  then have holf0: "\<F> n holomorphic_on ball z0 r" for n
-    by (meson holf holomorphic_on_subset)
-  have *: "((\<lambda>z. deriv (\<F> n) z / \<F> n z) has_contour_integral 0) (circlepath z0 (r/2))" for n
-  proof (rule Cauchy_theorem_disc_simple [of _ z0 r])
-    show "(\<lambda>z. deriv (\<F> n) z / \<F> n z) holomorphic_on ball z0 r"
-      apply (intro holomorphic_intros holomorphic_deriv holf holf0 open_ball nz)
-      using \<open>ball z0 r \<subseteq> S\<close> by blast
-  qed (use \<open>0 < r\<close> in auto)
-  have hol_dg: "deriv g holomorphic_on S"
-    by (simp add: \<open>open S\<close> holg holomorphic_deriv)
-  have "continuous_on (sphere z0 (r/2)) (deriv g)"
-    apply (intro holomorphic_on_imp_continuous_on holomorphic_on_subset [OF hol_dg])
-    using \<open>0 < r\<close> subS by auto
-  then have "compact (deriv g ` (sphere z0 (r/2)))"
-    by (rule compact_continuous_image [OF _ compact_sphere])
-  then have bo_dg: "bounded (deriv g ` (sphere z0 (r/2)))"
-    using compact_imp_bounded by blast
-  have "continuous_on (sphere z0 (r/2)) (cmod \<circ> g)"
-    apply (intro continuous_intros holomorphic_on_imp_continuous_on holomorphic_on_subset [OF holg])
-    using \<open>0 < r\<close> subS by auto
-  then have "compact ((cmod \<circ> g) ` sphere z0 (r/2))"
-    by (rule compact_continuous_image [OF _ compact_sphere])
-  moreover have "(cmod \<circ> g) ` sphere z0 (r/2) \<noteq> {}"
-    using \<open>0 < r\<close> by auto
-  ultimately obtain b where b: "b \<in> (cmod \<circ> g) ` sphere z0 (r/2)"
-                               "\<And>t. t \<in> (cmod \<circ> g) ` sphere z0 (r/2) \<Longrightarrow> b \<le> t"
-    using compact_attains_inf [of "(norm \<circ> g) ` (sphere z0 (r/2))"] by blast
-  have "(\<lambda>n. contour_integral (circlepath z0 (r/2)) (\<lambda>z. deriv (\<F> n) z / \<F> n z)) \<longlonglongrightarrow>
-        contour_integral (circlepath z0 (r/2)) (\<lambda>z. deriv g z / g z)"
-  proof (rule contour_integral_uniform_limit_circlepath)
-    show "\<forall>\<^sub>F n in sequentially. (\<lambda>z. deriv (\<F> n) z / \<F> n z) contour_integrable_on circlepath z0 (r/2)"
-      using * contour_integrable_on_def eventually_sequentiallyI by meson
-    show "uniform_limit (sphere z0 (r/2)) (\<lambda>n z. deriv (\<F> n) z / \<F> n z) (\<lambda>z. deriv g z / g z) sequentially"
-    proof (rule uniform_lim_divide [OF _ _ bo_dg])
-      show "uniform_limit (sphere z0 (r/2)) (\<lambda>a. deriv (\<F> a)) (deriv g) sequentially"
-      proof (rule uniform_limitI)
-        fix e::real
-        assume "0 < e"
-        have *: "dist (deriv (\<F> n) w) (deriv g w) < e"
-          if e8: "\<And>x. dist z0 x \<le> 3 * r / 4 \<Longrightarrow> dist (\<F> n x) (g x) * 8 < r * e"
-          and w: "dist w z0 = r/2"  for n w
-        proof -
-          have "ball w (r/4) \<subseteq> ball z0 r"  "cball w (r/4) \<subseteq> ball z0 r"
-            using \<open>0 < r\<close> by (simp_all add: ball_subset_ball_iff cball_subset_ball_iff w)
-          with subS have wr4_sub: "ball w (r/4) \<subseteq> S" "cball w (r/4) \<subseteq> S" by force+
-          moreover
-          have "(\<lambda>z. \<F> n z - g z) holomorphic_on S"
-            by (intro holomorphic_intros holf holg)
-          ultimately have hol: "(\<lambda>z. \<F> n z - g z) holomorphic_on ball w (r/4)"
-            and cont: "continuous_on (cball w (r / 4)) (\<lambda>z. \<F> n z - g z)"
-            using holomorphic_on_subset by (blast intro: holomorphic_on_imp_continuous_on)+
-          have "w \<in> S"
-            using \<open>0 < r\<close> wr4_sub by auto
-          have "\<And>y. dist w y < r / 4 \<Longrightarrow> dist z0 y \<le> 3 * r / 4"
-            apply (rule dist_triangle_le [where z=w])
-            using w by (simp add: dist_commute)
-          with e8 have in_ball: "\<And>y. y \<in> ball w (r/4) \<Longrightarrow> \<F> n y - g y \<in> ball 0 (r/4 * e/2)"
-            by (simp add: dist_norm [symmetric])
-          have "\<F> n field_differentiable at w"
-            by (metis holomorphic_on_imp_differentiable_at \<open>w \<in> S\<close> holf \<open>open S\<close>)
-          moreover
-          have "g field_differentiable at w"
-            using \<open>w \<in> S\<close> \<open>open S\<close> holg holomorphic_on_imp_differentiable_at by auto
-          moreover
-          have "cmod (deriv (\<lambda>w. \<F> n w - g w) w) * 2 \<le> e"
-            apply (rule Cauchy_higher_deriv_bound [OF hol cont in_ball, of 1, simplified])
-            using \<open>r > 0\<close> by auto
-          ultimately have "dist (deriv (\<F> n) w) (deriv g w) \<le> e/2"
-            by (simp add: dist_norm)
-          then show ?thesis
-            using \<open>e > 0\<close> by auto
-        qed
-        have "cball z0 (3 * r / 4) \<subseteq> ball z0 r"
-          by (simp add: cball_subset_ball_iff \<open>0 < r\<close>)
-        with subS have "uniform_limit (cball z0 (3 * r/4)) \<F> g sequentially"
-          by (force intro: ul_g)
-        then have "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>cball z0 (3 * r / 4). dist (\<F> n x) (g x) < r / 4 * e / 2"
-          using \<open>0 < e\<close> \<open>0 < r\<close> by (force simp: intro!: uniform_limitD)
-        then show "\<forall>\<^sub>F n in sequentially. \<forall>x \<in> sphere z0 (r/2). dist (deriv (\<F> n) x) (deriv g x) < e"
-          apply (simp add: eventually_sequentially)
-          apply (elim ex_forward all_forward imp_forward asm_rl)
-          using * apply (force simp: dist_commute)
-          done
-      qed
-      show "uniform_limit (sphere z0 (r/2)) \<F> g sequentially"
-      proof (rule uniform_limitI)
-        fix e::real
-        assume "0 < e"
-        have "sphere z0 (r/2) \<subseteq> ball z0 r"
-          using \<open>0 < r\<close> by auto
-        with subS have "uniform_limit (sphere z0 (r/2)) \<F> g sequentially"
-          by (force intro: ul_g)
-        then show "\<forall>\<^sub>F n in sequentially. \<forall>x \<in> sphere z0 (r/2). dist (\<F> n x) (g x) < e"
-          apply (rule uniform_limitD)
-          using \<open>0 < e\<close> by force
-      qed
-      show "b > 0" "\<And>x. x \<in> sphere z0 (r/2) \<Longrightarrow> b \<le> cmod (g x)"
-        using b \<open>0 < r\<close> by (fastforce simp: geq hnz)+
-    qed
-  qed (use \<open>0 < r\<close> in auto)
-  then have "(\<lambda>n. 0) \<longlonglongrightarrow> contour_integral (circlepath z0 (r/2)) (\<lambda>z. deriv g z / g z)"
-    by (simp add: contour_integral_unique [OF *])
-  then have "contour_integral (circlepath z0 (r/2)) (\<lambda>z. deriv g z / g z) = 0"
-    by (simp add: LIMSEQ_const_iff)
-  moreover
-  have "contour_integral (circlepath z0 (r/2)) (\<lambda>z. deriv g z / g z) =
-        contour_integral (circlepath z0 (r/2)) (\<lambda>z. m / (z - z0) + deriv h z / h z)"
-  proof (rule contour_integral_eq, use \<open>0 < r\<close> in simp)
-    fix w
-    assume w: "dist z0 w * 2 = r"
-    then have w_inb: "w \<in> ball z0 r"
-      using \<open>0 < r\<close> by auto
-    have h_der: "(h has_field_derivative deriv h w) (at w)"
-      using holh holomorphic_derivI w_inb by blast
-    have "deriv g w = ((of_nat m * h w + deriv h w * (w - z0)) * (w - z0) ^ m) / (w - z0)"
-         if "r = dist z0 w * 2" "w \<noteq> z0"
-    proof -
-      have "((\<lambda>w. (w - z0) ^ m * h w) has_field_derivative
-            (m * h w + deriv h w * (w - z0)) * (w - z0) ^ m / (w - z0)) (at w)"
-        apply (rule derivative_eq_intros h_der refl)+
-        using that \<open>m > 0\<close> \<open>0 < r\<close> apply (simp add: divide_simps distrib_right)
-        apply (metis Suc_pred mult.commute power_Suc)
-        done
-      then show ?thesis
-        apply (rule DERIV_imp_deriv [OF has_field_derivative_transform_within_open [where S = "ball z0 r"]])
-        using that \<open>m > 0\<close> \<open>0 < r\<close>
-          apply (simp_all add: hnz geq)
-        done
-    qed
-    with \<open>0 < r\<close> \<open>0 < m\<close> w w_inb show "deriv g w / g w = of_nat m / (w - z0) + deriv h w / h w"
-      by (auto simp: geq field_split_simps hnz)
-  qed
-  moreover
-  have "contour_integral (circlepath z0 (r/2)) (\<lambda>z. m / (z - z0) + deriv h z / h z) =
-        2 * of_real pi * \<i> * m + 0"
-  proof (rule contour_integral_unique [OF has_contour_integral_add])
-    show "((\<lambda>x. m / (x - z0)) has_contour_integral 2 * of_real pi * \<i> * m) (circlepath z0 (r/2))"
-      by (force simp: \<open>0 < r\<close> intro: Cauchy_integral_circlepath_simple)
-    show "((\<lambda>x. deriv h x / h x) has_contour_integral 0) (circlepath z0 (r/2))"
-      apply (rule Cauchy_theorem_disc_simple [of _ z0 r])
-      using hnz holh holomorphic_deriv holomorphic_on_divide \<open>0 < r\<close>
-         apply force+
-      done
-  qed
-  ultimately show False using \<open>0 < m\<close> by auto
-qed
-
-corollary Hurwitz_injective:
-  assumes S: "open S" "connected S"
-      and holf: "\<And>n::nat. \<F> n holomorphic_on S"
-      and holg: "g holomorphic_on S"
-      and ul_g: "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> uniform_limit K \<F> g sequentially"
-      and nonconst: "\<not> g constant_on S"
-      and inj: "\<And>n. inj_on (\<F> n) S"
-    shows "inj_on g S"
-proof -
-  have False if z12: "z1 \<in> S" "z2 \<in> S" "z1 \<noteq> z2" "g z2 = g z1" for z1 z2
-  proof -
-    obtain z0 where "z0 \<in> S" and z0: "g z0 \<noteq> g z2"
-      using constant_on_def nonconst by blast
-    have "(\<lambda>z. g z - g z1) holomorphic_on S"
-      by (intro holomorphic_intros holg)
-    then obtain r where "0 < r" "ball z2 r \<subseteq> S" "\<And>z. dist z2 z < r \<and> z \<noteq> z2 \<Longrightarrow> g z \<noteq> g z1"
-      apply (rule isolated_zeros [of "\<lambda>z. g z - g z1" S z2 z0])
-      using S \<open>z0 \<in> S\<close> z0 z12 by auto
-    have "g z2 - g z1 \<noteq> 0"
-    proof (rule Hurwitz_no_zeros [of "S - {z1}" "\<lambda>n z. \<F> n z - \<F> n z1" "\<lambda>z. g z - g z1"])
-      show "open (S - {z1})"
-        by (simp add: S open_delete)
-      show "connected (S - {z1})"
-        by (simp add: connected_open_delete [OF S])
-      show "\<And>n. (\<lambda>z. \<F> n z - \<F> n z1) holomorphic_on S - {z1}"
-        by (intro holomorphic_intros holomorphic_on_subset [OF holf]) blast
-      show "(\<lambda>z. g z - g z1) holomorphic_on S - {z1}"
-        by (intro holomorphic_intros holomorphic_on_subset [OF holg]) blast
-      show "uniform_limit K (\<lambda>n z. \<F> n z - \<F> n z1) (\<lambda>z. g z - g z1) sequentially"
-           if "compact K" "K \<subseteq> S - {z1}" for K
-      proof (rule uniform_limitI)
-        fix e::real
-        assume "e > 0"
-        have "uniform_limit K \<F> g sequentially"
-          using that ul_g by fastforce
-        then have K: "\<forall>\<^sub>F n in sequentially. \<forall>x \<in> K. dist (\<F> n x) (g x) < e/2"
-          using \<open>0 < e\<close> by (force simp: intro!: uniform_limitD)
-        have "uniform_limit {z1} \<F> g sequentially"
-          by (simp add: ul_g z12)
-        then have "\<forall>\<^sub>F n in sequentially. \<forall>x \<in> {z1}. dist (\<F> n x) (g x) < e/2"
-          using \<open>0 < e\<close> by (force simp: intro!: uniform_limitD)
-        then have z1: "\<forall>\<^sub>F n in sequentially. dist (\<F> n z1) (g z1) < e/2"
-          by simp
-        have "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>K. dist (\<F> n x - \<F> n z1) (g x - g z1) < e/2 + e/2"
-          apply (rule eventually_mono [OF eventually_conj [OF K z1]])
-          apply (simp add: dist_norm algebra_simps del: divide_const_simps)
-          by (metis add.commute dist_commute dist_norm dist_triangle_add_half)
-        have "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>K. dist (\<F> n x - \<F> n z1) (g x - g z1) < e/2 + e/2"
-          using eventually_conj [OF K z1]
-          apply (rule eventually_mono)
-          by (metis (no_types, hide_lams) diff_add_eq diff_diff_eq2 dist_commute dist_norm dist_triangle_add_half field_sum_of_halves)
-        then
-        show "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>K. dist (\<F> n x - \<F> n z1) (g x - g z1) < e"
-          by simp
-      qed
-      show "\<not> (\<lambda>z. g z - g z1) constant_on S - {z1}"
-        unfolding constant_on_def
-        by (metis Diff_iff \<open>z0 \<in> S\<close> empty_iff insert_iff right_minus_eq z0 z12)
-      show "\<And>n z. z \<in> S - {z1} \<Longrightarrow> \<F> n z - \<F> n z1 \<noteq> 0"
-        by (metis DiffD1 DiffD2 eq_iff_diff_eq_0 inj inj_onD insertI1 \<open>z1 \<in> S\<close>)
-      show "z2 \<in> S - {z1}"
-        using \<open>z2 \<in> S\<close> \<open>z1 \<noteq> z2\<close> by auto
-    qed
-    with z12 show False by auto
-  qed
-  then show ?thesis by (auto simp: inj_on_def)
-qed
-
-
-
-subsection\<open>The Great Picard theorem\<close>
-
-lemma GPicard1:
-  assumes S: "open S" "connected S" and "w \<in> S" "0 < r" "Y \<subseteq> X"
-      and holX: "\<And>h. h \<in> X \<Longrightarrow> h holomorphic_on S"
-      and X01:  "\<And>h z. \<lbrakk>h \<in> X; z \<in> S\<rbrakk> \<Longrightarrow> h z \<noteq> 0 \<and> h z \<noteq> 1"
-      and r:    "\<And>h. h \<in> Y \<Longrightarrow> norm(h w) \<le> r"
-  obtains B Z where "0 < B" "open Z" "w \<in> Z" "Z \<subseteq> S" "\<And>h z. \<lbrakk>h \<in> Y; z \<in> Z\<rbrakk> \<Longrightarrow> norm(h z) \<le> B"
-proof -
-  obtain e where "e > 0" and e: "cball w e \<subseteq> S"
-    using assms open_contains_cball_eq by blast
-  show ?thesis
-  proof
-    show "0 < exp(pi * exp(pi * (2 + 2 * r + 12)))"
-      by simp
-    show "ball w (e / 2) \<subseteq> S"
-      using e ball_divide_subset_numeral ball_subset_cball by blast
-    show "cmod (h z) \<le> exp (pi * exp (pi * (2 + 2 * r + 12)))"
-         if "h \<in> Y" "z \<in> ball w (e / 2)" for h z
-    proof -
-      have "h \<in> X"
-        using \<open>Y \<subseteq> X\<close> \<open>h \<in> Y\<close>  by blast
-      with holX have "h holomorphic_on S" 
-        by auto
-      then have "h holomorphic_on cball w e"
-        by (metis e holomorphic_on_subset)
-      then have hol_h_o: "(h \<circ> (\<lambda>z. (w + of_real e * z))) holomorphic_on cball 0 1"
-        apply (intro holomorphic_intros holomorphic_on_compose)
-        apply (erule holomorphic_on_subset)
-        using that \<open>e > 0\<close> by (auto simp: dist_norm norm_mult)
-      have norm_le_r: "cmod ((h \<circ> (\<lambda>z. w + complex_of_real e * z)) 0) \<le> r"
-        by (auto simp: r \<open>h \<in> Y\<close>)
-      have le12: "norm (of_real(inverse e) * (z - w)) \<le> 1/2"
-        using that \<open>e > 0\<close> by (simp add: inverse_eq_divide dist_norm norm_minus_commute norm_divide)
-      have non01: "\<And>z::complex. cmod z \<le> 1 \<Longrightarrow> h (w + e * z) \<noteq> 0 \<and> h (w + e * z) \<noteq> 1"
-        apply (rule X01 [OF \<open>h \<in> X\<close>])
-          apply (rule subsetD [OF e])
-        using \<open>0 < e\<close>  by (auto simp: dist_norm norm_mult)
-      have "cmod (h z) \<le> cmod (h (w + of_real e * (inverse e * (z - w))))"
-        using \<open>0 < e\<close> by (simp add: field_split_simps)
-      also have "... \<le> exp (pi * exp (pi * (14 + 2 * r)))"
-        using r [OF \<open>h \<in> Y\<close>] Schottky [OF hol_h_o norm_le_r _ _ _ le12] non01 by auto
-      finally
-      show ?thesis by simp
-    qed
-  qed (use \<open>e > 0\<close> in auto)
-qed 
-
-lemma GPicard2:
-  assumes "S \<subseteq> T" "connected T" "S \<noteq> {}" "open S" "\<And>x. \<lbrakk>x islimpt S; x \<in> T\<rbrakk> \<Longrightarrow> x \<in> S"
-    shows "S = T"
-  by (metis assms open_subset connected_clopen closedin_limpt)
-
-    
-lemma GPicard3:
-  assumes S: "open S" "connected S" "w \<in> S" and "Y \<subseteq> X"
-      and holX: "\<And>h. h \<in> X \<Longrightarrow> h holomorphic_on S"
-      and X01:  "\<And>h z. \<lbrakk>h \<in> X; z \<in> S\<rbrakk> \<Longrightarrow> h z \<noteq> 0 \<and> h z \<noteq> 1"
-      and no_hw_le1: "\<And>h. h \<in> Y \<Longrightarrow> norm(h w) \<le> 1"
-      and "compact K" "K \<subseteq> S"
-  obtains B where "\<And>h z. \<lbrakk>h \<in> Y; z \<in> K\<rbrakk> \<Longrightarrow> norm(h z) \<le> B"
-proof -
-  define U where "U \<equiv> {z \<in> S. \<exists>B Z. 0 < B \<and> open Z \<and> z \<in> Z \<and> Z \<subseteq> S \<and>
-                               (\<forall>h z'. h \<in> Y \<and> z' \<in> Z \<longrightarrow> norm(h z') \<le> B)}"
-  then have "U \<subseteq> S" by blast
-  have "U = S"
-  proof (rule GPicard2 [OF \<open>U \<subseteq> S\<close> \<open>connected S\<close>])
-    show "U \<noteq> {}"
-    proof -
-      obtain B Z where "0 < B" "open Z" "w \<in> Z" "Z \<subseteq> S" 
-        and  "\<And>h z. \<lbrakk>h \<in> Y; z \<in> Z\<rbrakk> \<Longrightarrow> norm(h z) \<le> B"
-        apply (rule GPicard1 [OF S zero_less_one \<open>Y \<subseteq> X\<close> holX])
-        using no_hw_le1 X01 by force+
-      then show ?thesis
-        unfolding U_def using \<open>w \<in> S\<close> by blast
-    qed
-    show "open U"
-      unfolding open_subopen [of U] by (auto simp: U_def)
-    fix v
-    assume v: "v islimpt U" "v \<in> S"
-    have "\<not> (\<forall>r>0. \<exists>h\<in>Y. r < cmod (h v))"
-    proof
-      assume "\<forall>r>0. \<exists>h\<in>Y. r < cmod (h v)"
-      then have "\<forall>n. \<exists>h\<in>Y. Suc n < cmod (h v)"
-        by simp
-      then obtain \<F> where FY: "\<And>n. \<F> n \<in> Y" and ltF: "\<And>n. Suc n < cmod (\<F> n v)"
-        by metis
-      define \<G> where "\<G> \<equiv> \<lambda>n z. inverse(\<F> n z)"
-      have hol\<G>: "\<G> n holomorphic_on S" for n
-        apply (simp add: \<G>_def)
-        using FY X01 \<open>Y \<subseteq> X\<close> holX apply (blast intro: holomorphic_on_inverse)
-        done
-      have \<G>not0: "\<G> n z \<noteq> 0" and \<G>not1: "\<G> n z \<noteq> 1" if "z \<in> S" for n z
-        using FY X01 \<open>Y \<subseteq> X\<close> that by (force simp: \<G>_def)+
-      have \<G>_le1: "cmod (\<G> n v) \<le> 1" for n 
-        using less_le_trans linear ltF 
-        by (fastforce simp add: \<G>_def norm_inverse inverse_le_1_iff)
-      define W where "W \<equiv> {h. h holomorphic_on S \<and> (\<forall>z \<in> S. h z \<noteq> 0 \<and> h z \<noteq> 1)}"
-      obtain B Z where "0 < B" "open Z" "v \<in> Z" "Z \<subseteq> S" 
-                   and B: "\<And>h z. \<lbrakk>h \<in> range \<G>; z \<in> Z\<rbrakk> \<Longrightarrow> norm(h z) \<le> B"
-        apply (rule GPicard1 [OF \<open>open S\<close> \<open>connected S\<close> \<open>v \<in> S\<close> zero_less_one, of "range \<G>" W])
-        using hol\<G> \<G>not0 \<G>not1 \<G>_le1 by (force simp: W_def)+
-      then obtain e where "e > 0" and e: "ball v e \<subseteq> Z"
-        by (meson open_contains_ball)
-      obtain h j where holh: "h holomorphic_on ball v e" and "strict_mono j"
-                   and lim:  "\<And>x. x \<in> ball v e \<Longrightarrow> (\<lambda>n. \<G> (j n) x) \<longlonglongrightarrow> h x"
-                   and ulim: "\<And>K. \<lbrakk>compact K; K \<subseteq> ball v e\<rbrakk>
-                                  \<Longrightarrow> uniform_limit K (\<G> \<circ> j) h sequentially"
-      proof (rule Montel)
-        show "\<And>h. h \<in> range \<G> \<Longrightarrow> h holomorphic_on ball v e"
-          by (metis \<open>Z \<subseteq> S\<close> e hol\<G> holomorphic_on_subset imageE)
-        show "\<And>K. \<lbrakk>compact K; K \<subseteq> ball v e\<rbrakk> \<Longrightarrow> \<exists>B. \<forall>h\<in>range \<G>. \<forall>z\<in>K. cmod (h z) \<le> B"
-          using B e by blast
-      qed auto
-      have "h v = 0"
-      proof (rule LIMSEQ_unique)
-        show "(\<lambda>n. \<G> (j n) v) \<longlonglongrightarrow> h v"
-          using \<open>e > 0\<close> lim by simp
-        have lt_Fj: "real x \<le> cmod (\<F> (j x) v)" for x
-          by (metis of_nat_Suc ltF \<open>strict_mono j\<close> add.commute less_eq_real_def less_le_trans nat_le_real_less seq_suble)
-        show "(\<lambda>n. \<G> (j n) v) \<longlonglongrightarrow> 0"
-        proof (rule Lim_null_comparison [OF eventually_sequentiallyI lim_inverse_n])
-          show "cmod (\<G> (j x) v) \<le> inverse (real x)" if "1 \<le> x" for x
-            using that by (simp add: \<G>_def norm_inverse_le_norm [OF lt_Fj])
-        qed        
-      qed
-      have "h v \<noteq> 0"
-      proof (rule Hurwitz_no_zeros [of "ball v e" "\<G> \<circ> j" h])
-        show "\<And>n. (\<G> \<circ> j) n holomorphic_on ball v e"
-          using \<open>Z \<subseteq> S\<close> e hol\<G> by force
-        show "\<And>n z. z \<in> ball v e \<Longrightarrow> (\<G> \<circ> j) n z \<noteq> 0"
-          using \<G>not0 \<open>Z \<subseteq> S\<close> e by fastforce
-        show "\<not> h constant_on ball v e"
-        proof (clarsimp simp: constant_on_def)
-          fix c
-          have False if "\<And>z. dist v z < e \<Longrightarrow> h z = c"  
-          proof -
-            have "h v = c"
-              by (simp add: \<open>0 < e\<close> that)
-            obtain y where "y \<in> U" "y \<noteq> v" and y: "dist y v < e"
-              using v \<open>e > 0\<close> by (auto simp: islimpt_approachable)
-            then obtain C T where "y \<in> S" "C > 0" "open T" "y \<in> T" "T \<subseteq> S"
-              and "\<And>h z'. \<lbrakk>h \<in> Y; z' \<in> T\<rbrakk> \<Longrightarrow> cmod (h z') \<le> C"
-              using \<open>y \<in> U\<close> by (auto simp: U_def)
-            then have le_C: "\<And>n. cmod (\<F> n y) \<le> C"
-              using FY by blast                
-            have "\<forall>\<^sub>F n in sequentially. dist (\<G> (j n) y) (h y) < inverse C"
-              using uniform_limitD [OF ulim [of "{y}"], of "inverse C"] \<open>C > 0\<close> y
-              by (simp add: dist_commute)
-            then obtain n where "dist (\<G> (j n) y) (h y) < inverse C"
-              by (meson eventually_at_top_linorder order_refl)
-            moreover
-            have "h y = h v"
-              by (metis \<open>h v = c\<close> dist_commute that y)
-            ultimately have "norm (\<G> (j n) y) < inverse C"
-              by (simp add: \<open>h v = 0\<close>)
-            then have "C < norm (\<F> (j n) y)"
-              apply (simp add: \<G>_def)
-              by (metis FY X01 \<open>0 < C\<close> \<open>y \<in> S\<close> \<open>Y \<subseteq> X\<close> inverse_less_iff_less norm_inverse subsetD zero_less_norm_iff)
-            show False
-              using \<open>C < cmod (\<F> (j n) y)\<close> le_C not_less by blast
-          qed
-          then show "\<exists>x\<in>ball v e. h x \<noteq> c" by force
-        qed
-        show "h holomorphic_on ball v e"
-          by (simp add: holh)
-        show "\<And>K. \<lbrakk>compact K; K \<subseteq> ball v e\<rbrakk> \<Longrightarrow> uniform_limit K (\<G> \<circ> j) h sequentially"
-          by (simp add: ulim)
-      qed (use \<open>e > 0\<close> in auto)
-      with \<open>h v = 0\<close> show False by blast
-    qed
-    then show "v \<in> U"
-      apply (clarsimp simp add: U_def v)
-      apply (rule GPicard1[OF \<open>open S\<close> \<open>connected S\<close> \<open>v \<in> S\<close> _ \<open>Y \<subseteq> X\<close> holX])
-      using X01 no_hw_le1 apply (meson | force simp: not_less)+
-      done
-  qed
-  have "\<And>x. x \<in> K \<longrightarrow> x \<in> U"
-    using \<open>U = S\<close> \<open>K \<subseteq> S\<close> by blast
-  then have "\<And>x. x \<in> K \<longrightarrow> (\<exists>B Z. 0 < B \<and> open Z \<and> x \<in> Z \<and> 
-                               (\<forall>h z'. h \<in> Y \<and> z' \<in> Z \<longrightarrow> norm(h z') \<le> B))"
-    unfolding U_def by blast
-  then obtain F Z where F: "\<And>x. x \<in> K \<Longrightarrow> open (Z x) \<and> x \<in> Z x \<and> 
-                               (\<forall>h z'. h \<in> Y \<and> z' \<in> Z x \<longrightarrow> norm(h z') \<le> F x)"
-    by metis
-  then obtain L where "L \<subseteq> K" "finite L" and L: "K \<subseteq> (\<Union>c \<in> L. Z c)"
-    by (auto intro: compactE_image [OF \<open>compact K\<close>, of K Z])
-  then have *: "\<And>x h z'. \<lbrakk>x \<in> L; h \<in> Y \<and> z' \<in> Z x\<rbrakk> \<Longrightarrow> cmod (h z') \<le> F x"
-    using F by blast
-  have "\<exists>B. \<forall>h z. h \<in> Y \<and> z \<in> K \<longrightarrow> norm(h z) \<le> B"
-  proof (cases "L = {}")
-    case True with L show ?thesis by simp
-  next
-    case False
-    with \<open>finite L\<close> show ?thesis 
-      apply (rule_tac x = "Max (F ` L)" in exI)
-      apply (simp add: linorder_class.Max_ge_iff)
-      using * F  by (metis L UN_E subsetD)
-  qed
-  with that show ?thesis by metis
-qed
-
-
-lemma GPicard4:
-  assumes "0 < k" and holf: "f holomorphic_on (ball 0 k - {0})" 
-      and AE: "\<And>e. \<lbrakk>0 < e; e < k\<rbrakk> \<Longrightarrow> \<exists>d. 0 < d \<and> d < e \<and> (\<forall>z \<in> sphere 0 d. norm(f z) \<le> B)"
-  obtains \<epsilon> where "0 < \<epsilon>" "\<epsilon> < k" "\<And>z. z \<in> ball 0 \<epsilon> - {0} \<Longrightarrow> norm(f z) \<le> B"
-proof -
-  obtain \<epsilon> where "0 < \<epsilon>" "\<epsilon> < k/2" and \<epsilon>: "\<And>z. norm z = \<epsilon> \<Longrightarrow> norm(f z) \<le> B"
-    using AE [of "k/2"] \<open>0 < k\<close> by auto
-  show ?thesis
-  proof
-    show "\<epsilon> < k"
-      using \<open>0 < k\<close> \<open>\<epsilon> < k/2\<close> by auto
-    show "cmod (f \<xi>) \<le> B" if \<xi>: "\<xi> \<in> ball 0 \<epsilon> - {0}" for \<xi>
-    proof -
-      obtain d where "0 < d" "d < norm \<xi>" and d: "\<And>z. norm z = d \<Longrightarrow> norm(f z) \<le> B"
-        using AE [of "norm \<xi>"] \<open>\<epsilon> < k\<close> \<xi> by auto
-      have [simp]: "closure (cball 0 \<epsilon> - ball 0 d) = cball 0 \<epsilon> - ball 0 d"
-        by (blast intro!: closure_closed)
-      have [simp]: "interior (cball 0 \<epsilon> - ball 0 d) = ball 0 \<epsilon> - cball (0::complex) d"
-        using \<open>0 < \<epsilon>\<close> \<open>0 < d\<close> by (simp add: interior_diff)
-      have *: "norm(f w) \<le> B" if "w \<in> cball 0 \<epsilon> - ball 0 d" for w
-      proof (rule maximum_modulus_frontier [of f "cball 0 \<epsilon> - ball 0 d"])
-        show "f holomorphic_on interior (cball 0 \<epsilon> - ball 0 d)"
-          apply (rule holomorphic_on_subset [OF holf])
-          using \<open>\<epsilon> < k\<close> \<open>0 < d\<close> that by auto
-        show "continuous_on (closure (cball 0 \<epsilon> - ball 0 d)) f"
-          apply (rule holomorphic_on_imp_continuous_on)
-          apply (rule holomorphic_on_subset [OF holf])
-          using \<open>0 < d\<close> \<open>\<epsilon> < k\<close> by auto
-        show "\<And>z. z \<in> frontier (cball 0 \<epsilon> - ball 0 d) \<Longrightarrow> cmod (f z) \<le> B"
-          apply (simp add: frontier_def)
-          using \<epsilon> d less_eq_real_def by blast
-      qed (use that in auto)
-      show ?thesis
-        using * \<open>d < cmod \<xi>\<close> that by auto
-    qed
-  qed (use \<open>0 < \<epsilon>\<close> in auto)
-qed
-  
-
-lemma GPicard5:
-  assumes holf: "f holomorphic_on (ball 0 1 - {0})"
-      and f01:  "\<And>z. z \<in> ball 0 1 - {0} \<Longrightarrow> f z \<noteq> 0 \<and> f z \<noteq> 1"
-  obtains e B where "0 < e" "e < 1" "0 < B" 
-                    "(\<forall>z \<in> ball 0 e - {0}. norm(f z) \<le> B) \<or>
-                     (\<forall>z \<in> ball 0 e - {0}. norm(f z) \<ge> B)"
-proof -
-  have [simp]: "1 + of_nat n \<noteq> (0::complex)" for n
-    using of_nat_eq_0_iff by fastforce
-  have [simp]: "cmod (1 + of_nat n) = 1 + of_nat n" for n
-    by (metis norm_of_nat of_nat_Suc)
-  have *: "(\<lambda>x::complex. x / of_nat (Suc n)) ` (ball 0 1 - {0}) \<subseteq> ball 0 1 - {0}" for n
-    by (auto simp: norm_divide field_split_simps split: if_split_asm)
-  define h where "h \<equiv> \<lambda>n z::complex. f (z / (Suc n))"
-  have holh: "(h n) holomorphic_on ball 0 1 - {0}" for n
-    unfolding h_def
-  proof (rule holomorphic_on_compose_gen [unfolded o_def, OF _ holf *])
-    show "(\<lambda>x. x / of_nat (Suc n)) holomorphic_on ball 0 1 - {0}"
-      by (intro holomorphic_intros) auto
-  qed
-  have h01: "\<And>n z. z \<in> ball 0 1 - {0} \<Longrightarrow> h n z \<noteq> 0 \<and> h n z \<noteq> 1" 
-    unfolding h_def
-    apply (rule f01)
-    using * by force
-  obtain w where w: "w \<in> ball 0 1 - {0::complex}"
-    by (rule_tac w = "1/2" in that) auto
-  consider "infinite {n. norm(h n w) \<le> 1}" | "infinite {n. 1 \<le> norm(h n w)}"
-    by (metis (mono_tags, lifting) infinite_nat_iff_unbounded_le le_cases mem_Collect_eq)
-  then show ?thesis
-  proof cases
-    case 1
-    with infinite_enumerate obtain r :: "nat \<Rightarrow> nat" 
-      where "strict_mono r" and r: "\<And>n. r n \<in> {n. norm(h n w) \<le> 1}"
-      by blast
-    obtain B where B: "\<And>j z. \<lbrakk>norm z = 1/2; j \<in> range (h \<circ> r)\<rbrakk> \<Longrightarrow> norm(j z) \<le> B"
-    proof (rule GPicard3 [OF _ _ w, where K = "sphere 0 (1/2)"])  
-      show "range (h \<circ> r) \<subseteq> 
-            {g. g holomorphic_on ball 0 1 - {0} \<and> (\<forall>z\<in>ball 0 1 - {0}. g z \<noteq> 0 \<and> g z \<noteq> 1)}"
-        apply clarsimp
-        apply (intro conjI holomorphic_intros holomorphic_on_compose holh)
-        using h01 apply auto
-        done
-      show "connected (ball 0 1 - {0::complex})"
-        by (simp add: connected_open_delete)
-    qed (use r in auto)        
-    have normf_le_B: "cmod(f z) \<le> B" if "norm z = 1 / (2 * (1 + of_nat (r n)))" for z n
-    proof -
-      have *: "\<And>w. norm w = 1/2 \<Longrightarrow> cmod((f (w / (1 + of_nat (r n))))) \<le> B"
-        using B by (auto simp: h_def o_def)
-      have half: "norm (z * (1 + of_nat (r n))) = 1/2"
-        by (simp add: norm_mult divide_simps that)
-      show ?thesis
-        using * [OF half] by simp
-    qed
-    obtain \<epsilon> where "0 < \<epsilon>" "\<epsilon> < 1" "\<And>z. z \<in> ball 0 \<epsilon> - {0} \<Longrightarrow> cmod(f z) \<le> B"
-    proof (rule GPicard4 [OF zero_less_one holf, of B])
-      fix e::real
-      assume "0 < e" "e < 1"
-      obtain n where "(1/e - 2) / 2 < real n"
-        using reals_Archimedean2 by blast
-      also have "... \<le> r n"
-        using \<open>strict_mono r\<close> by (simp add: seq_suble)
-      finally have "(1/e - 2) / 2 < real (r n)" .
-      with \<open>0 < e\<close> have e: "e > 1 / (2 + 2 * real (r n))"
-        by (simp add: field_simps)
-      show "\<exists>d>0. d < e \<and> (\<forall>z\<in>sphere 0 d. cmod (f z) \<le> B)"
-        apply (rule_tac x="1 / (2 * (1 + of_nat (r n)))" in exI)
-        using normf_le_B by (simp add: e)
-    qed blast
-    then have \<epsilon>: "cmod (f z) \<le> \<bar>B\<bar> + 1" if "cmod z < \<epsilon>" "z \<noteq> 0" for z
-      using that by fastforce
-    have "0 < \<bar>B\<bar> + 1"
-      by simp
-    then show ?thesis
-      apply (rule that [OF \<open>0 < \<epsilon>\<close> \<open>\<epsilon> < 1\<close>])
-      using \<epsilon> by auto 
-  next
-    case 2
-    with infinite_enumerate obtain r :: "nat \<Rightarrow> nat" 
-      where "strict_mono r" and r: "\<And>n. r n \<in> {n. norm(h n w) \<ge> 1}"
-      by blast
-    obtain B where B: "\<And>j z. \<lbrakk>norm z = 1/2; j \<in> range (\<lambda>n. inverse \<circ> h (r n))\<rbrakk> \<Longrightarrow> norm(j z) \<le> B"
-    proof (rule GPicard3 [OF _ _ w, where K = "sphere 0 (1/2)"])  
-      show "range (\<lambda>n. inverse \<circ> h (r n)) \<subseteq> 
-            {g. g holomorphic_on ball 0 1 - {0} \<and> (\<forall>z\<in>ball 0 1 - {0}. g z \<noteq> 0 \<and> g z \<noteq> 1)}"
-        apply clarsimp
-        apply (intro conjI holomorphic_intros holomorphic_on_compose_gen [unfolded o_def, OF _ holh] holomorphic_on_compose)
-        using h01 apply auto
-        done
-      show "connected (ball 0 1 - {0::complex})"
-        by (simp add: connected_open_delete)
-      show "\<And>j. j \<in> range (\<lambda>n. inverse \<circ> h (r n)) \<Longrightarrow> cmod (j w) \<le> 1"
-        using r norm_inverse_le_norm by fastforce
-    qed (use r in auto)        
-    have norm_if_le_B: "cmod(inverse (f z)) \<le> B" if "norm z = 1 / (2 * (1 + of_nat (r n)))" for z n
-    proof -
-      have *: "inverse (cmod((f (z / (1 + of_nat (r n)))))) \<le> B" if "norm z = 1/2" for z
-        using B [OF that] by (force simp: norm_inverse h_def)
-      have half: "norm (z * (1 + of_nat (r n))) = 1/2"
-        by (simp add: norm_mult divide_simps that)
-      show ?thesis
-        using * [OF half] by (simp add: norm_inverse)
-    qed
-    have hol_if: "(inverse \<circ> f) holomorphic_on (ball 0 1 - {0})"
-      by (metis (no_types, lifting) holf comp_apply f01 holomorphic_on_inverse holomorphic_transform)
-    obtain \<epsilon> where "0 < \<epsilon>" "\<epsilon> < 1" and leB: "\<And>z. z \<in> ball 0 \<epsilon> - {0} \<Longrightarrow> cmod((inverse \<circ> f) z) \<le> B"
-    proof (rule GPicard4 [OF zero_less_one hol_if, of B])
-      fix e::real
-      assume "0 < e" "e < 1"
-      obtain n where "(1/e - 2) / 2 < real n"
-        using reals_Archimedean2 by blast
-      also have "... \<le> r n"
-        using \<open>strict_mono r\<close> by (simp add: seq_suble)
-      finally have "(1/e - 2) / 2 < real (r n)" .
-      with \<open>0 < e\<close> have e: "e > 1 / (2 + 2 * real (r n))"
-        by (simp add: field_simps)
-      show "\<exists>d>0. d < e \<and> (\<forall>z\<in>sphere 0 d. cmod ((inverse \<circ> f) z) \<le> B)"
-        apply (rule_tac x="1 / (2 * (1 + of_nat (r n)))" in exI)
-        using norm_if_le_B by (simp add: e)
-    qed blast
-    have \<epsilon>: "cmod (f z) \<ge> inverse B" and "B > 0" if "cmod z < \<epsilon>" "z \<noteq> 0" for z
-    proof -
-      have "inverse (cmod (f z)) \<le> B"
-        using leB that by (simp add: norm_inverse)
-      moreover
-      have "f z \<noteq> 0"
-        using \<open>\<epsilon> < 1\<close> f01 that by auto
-      ultimately show "cmod (f z) \<ge> inverse B"
-        by (simp add: norm_inverse inverse_le_imp_le)
-      show "B > 0"
-        using \<open>f z \<noteq> 0\<close> \<open>inverse (cmod (f z)) \<le> B\<close> not_le order.trans by fastforce
-    qed
-    then have "B > 0"
-      by (metis \<open>0 < \<epsilon>\<close> dense leI order.asym vector_choose_size)
-    then have "inverse B > 0"
-      by (simp add: field_split_simps)
-    then show ?thesis
-      apply (rule that [OF \<open>0 < \<epsilon>\<close> \<open>\<epsilon> < 1\<close>])
-      using \<epsilon> by auto 
-  qed
-qed
-
-  
-lemma GPicard6:
-  assumes "open M" "z \<in> M" "a \<noteq> 0" and holf: "f holomorphic_on (M - {z})"
-      and f0a: "\<And>w. w \<in> M - {z} \<Longrightarrow> f w \<noteq> 0 \<and> f w \<noteq> a"
-  obtains r where "0 < r" "ball z r \<subseteq> M" 
-                  "bounded(f ` (ball z r - {z})) \<or>
-                   bounded((inverse \<circ> f) ` (ball z r - {z}))"
-proof -
-  obtain r where "0 < r" and r: "ball z r \<subseteq> M"
-    using assms openE by blast 
-  let ?g = "\<lambda>w. f (z + of_real r * w) / a"
-  obtain e B where "0 < e" "e < 1" "0 < B" 
-    and B: "(\<forall>z \<in> ball 0 e - {0}. norm(?g z) \<le> B) \<or> (\<forall>z \<in> ball 0 e - {0}. norm(?g z) \<ge> B)"
-  proof (rule GPicard5)
-    show "?g holomorphic_on ball 0 1 - {0}"
-      apply (intro holomorphic_intros holomorphic_on_compose_gen [unfolded o_def, OF _ holf])
-      using \<open>0 < r\<close> \<open>a \<noteq> 0\<close> r
-      by (auto simp: dist_norm norm_mult subset_eq)
-    show "\<And>w. w \<in> ball 0 1 - {0} \<Longrightarrow> f (z + of_real r * w) / a \<noteq> 0 \<and> f (z + of_real r * w) / a \<noteq> 1"
-      apply (simp add: field_split_simps \<open>a \<noteq> 0\<close>)
-      apply (rule f0a)
-      using \<open>0 < r\<close> r by (auto simp: dist_norm norm_mult subset_eq)
-  qed
-  show ?thesis
-  proof
-    show "0 < e*r"
-      by (simp add: \<open>0 < e\<close> \<open>0 < r\<close>)
-    have "ball z (e * r) \<subseteq> ball z r"
-      by (simp add: \<open>0 < r\<close> \<open>e < 1\<close> order.strict_implies_order subset_ball)
-    then show "ball z (e * r) \<subseteq> M"
-      using r by blast
-    consider "\<And>z. z \<in> ball 0 e - {0} \<Longrightarrow> norm(?g z) \<le> B" | "\<And>z. z \<in> ball 0 e - {0} \<Longrightarrow> norm(?g z) \<ge> B"
-      using B by blast
-    then show "bounded (f ` (ball z (e * r) - {z})) \<or>
-          bounded ((inverse \<circ> f) ` (ball z (e * r) - {z}))"
-    proof cases
-      case 1
-      have "\<lbrakk>dist z w < e * r; w \<noteq> z\<rbrakk> \<Longrightarrow> cmod (f w) \<le> B * norm a" for w
-        using \<open>a \<noteq> 0\<close> \<open>0 < r\<close> 1 [of "(w - z) / r"]
-        by (simp add: norm_divide dist_norm field_split_simps)
-      then show ?thesis
-        by (force simp: intro!: boundedI)
-    next
-      case 2
-      have "\<lbrakk>dist z w < e * r; w \<noteq> z\<rbrakk> \<Longrightarrow> cmod (f w) \<ge> B * norm a" for w
-        using \<open>a \<noteq> 0\<close> \<open>0 < r\<close> 2 [of "(w - z) / r"]
-        by (simp add: norm_divide dist_norm field_split_simps)
-      then have "\<lbrakk>dist z w < e * r; w \<noteq> z\<rbrakk> \<Longrightarrow> inverse (cmod (f w)) \<le> inverse (B * norm a)" for w
-        by (metis \<open>0 < B\<close> \<open>a \<noteq> 0\<close> mult_pos_pos norm_inverse norm_inverse_le_norm zero_less_norm_iff)
-      then show ?thesis 
-        by (force simp: norm_inverse intro!: boundedI)
-    qed
-  qed
-qed
-  
-
-theorem great_Picard:
-  assumes "open M" "z \<in> M" "a \<noteq> b" and holf: "f holomorphic_on (M - {z})"
-      and fab: "\<And>w. w \<in> M - {z} \<Longrightarrow> f w \<noteq> a \<and> f w \<noteq> b"
-  obtains l where "(f \<longlongrightarrow> l) (at z) \<or> ((inverse \<circ> f) \<longlongrightarrow> l) (at z)"
-proof -
-  obtain r where "0 < r" and zrM: "ball z r \<subseteq> M" 
-             and r: "bounded((\<lambda>z. f z - a) ` (ball z r - {z})) \<or>
-                     bounded((inverse \<circ> (\<lambda>z. f z - a)) ` (ball z r - {z}))"
-  proof (rule GPicard6 [OF \<open>open M\<close> \<open>z \<in> M\<close>])
-    show "b - a \<noteq> 0"
-      using assms by auto
-    show "(\<lambda>z. f z - a) holomorphic_on M - {z}"
-      by (intro holomorphic_intros holf)
-  qed (use fab in auto)
-  have holfb: "f holomorphic_on ball z r - {z}"
-    apply (rule holomorphic_on_subset [OF holf])
-    using zrM by auto
-  have holfb_i: "(\<lambda>z. inverse(f z - a)) holomorphic_on ball z r - {z}"
-    apply (intro holomorphic_intros holfb)
-    using fab zrM by fastforce
-  show ?thesis
-    using r
-  proof              
-    assume "bounded ((\<lambda>z. f z - a) ` (ball z r - {z}))"
-    then obtain B where B: "\<And>w. w \<in> (\<lambda>z. f z - a) ` (ball z r - {z}) \<Longrightarrow> norm w \<le> B"
-      by (force simp: bounded_iff)
-    have "\<forall>\<^sub>F w in at z. cmod (f w - a) \<le> B"
-      apply (simp add: eventually_at)
-      apply (rule_tac x=r in exI)
-      using \<open>0 < r\<close> by (auto simp: dist_commute intro!: B)
-    then have "\<exists>B. \<forall>\<^sub>F w in at z. cmod (f w) \<le> B"
-      apply (rule_tac x="B + norm a" in exI)
-        apply (erule eventually_mono)
-      by (metis add.commute add_le_cancel_right norm_triangle_sub order.trans)
-    then obtain g where holg: "g holomorphic_on ball z r" and gf: "\<And>w. w \<in> ball z r - {z} \<Longrightarrow> g w = f w"
-      using \<open>0 < r\<close> holomorphic_on_extend_bounded [OF holfb] by auto
-    then have "g \<midarrow>z\<rightarrow> g z"
-      apply (simp add: continuous_at [symmetric])
-      using \<open>0 < r\<close> centre_in_ball field_differentiable_imp_continuous_at holomorphic_on_imp_differentiable_at by blast
-    then have "(f \<longlongrightarrow> g z) (at z)"
-      apply (rule Lim_transform_within_open [of g "g z" z UNIV "ball z r"])
-      using  \<open>0 < r\<close> by (auto simp: gf)
-    then show ?thesis
-      using that by blast
-  next
-    assume "bounded((inverse \<circ> (\<lambda>z. f z - a)) ` (ball z r - {z}))"
-    then obtain B where B: "\<And>w. w \<in> (inverse \<circ> (\<lambda>z. f z - a)) ` (ball z r - {z}) \<Longrightarrow> norm w \<le> B"
-      by (force simp: bounded_iff)
-    have "\<forall>\<^sub>F w in at z. cmod (inverse (f w - a)) \<le> B"
-      apply (simp add: eventually_at)
-      apply (rule_tac x=r in exI)
-      using \<open>0 < r\<close> by (auto simp: dist_commute intro!: B)
-    then have "\<exists>B. \<forall>\<^sub>F z in at z. cmod (inverse (f z - a)) \<le> B"
-      by blast
-    then obtain g where holg: "g holomorphic_on ball z r" and gf: "\<And>w. w \<in> ball z r - {z} \<Longrightarrow> g w = inverse (f w - a)"
-      using \<open>0 < r\<close> holomorphic_on_extend_bounded [OF holfb_i] by auto
-    then have gz: "g \<midarrow>z\<rightarrow> g z"
-      apply (simp add: continuous_at [symmetric])
-      using \<open>0 < r\<close> centre_in_ball field_differentiable_imp_continuous_at holomorphic_on_imp_differentiable_at by blast
-    have gnz: "\<And>w. w \<in> ball z r - {z} \<Longrightarrow> g w \<noteq> 0"
-      using gf fab zrM by fastforce
-    show ?thesis
-    proof (cases "g z = 0")
-      case True
-      have *: "\<lbrakk>g \<noteq> 0; inverse g = f - a\<rbrakk> \<Longrightarrow> g / (1 + a * g) = inverse f" for f g::complex
-        by (auto simp: field_simps)
-      have "(inverse \<circ> f) \<midarrow>z\<rightarrow> 0"
-      proof (rule Lim_transform_within_open [of "\<lambda>w. g w / (1 + a * g w)" _ _ UNIV "ball z r"])
-        show "(\<lambda>w. g w / (1 + a * g w)) \<midarrow>z\<rightarrow> 0"
-          using True by (auto simp: intro!: tendsto_eq_intros gz)
-        show "\<And>x. \<lbrakk>x \<in> ball z r; x \<noteq> z\<rbrakk> \<Longrightarrow> g x / (1 + a * g x) = (inverse \<circ> f) x"
-          using * gf gnz by simp
-      qed (use \<open>0 < r\<close> in auto)
-      with that show ?thesis by blast
-    next
-      case False
-      show ?thesis
-      proof (cases "1 + a * g z = 0")
-        case True
-        have "(f \<longlongrightarrow> 0) (at z)"
-        proof (rule Lim_transform_within_open [of "\<lambda>w. (1 + a * g w) / g w" _ _ _ "ball z r"])
-          show "(\<lambda>w. (1 + a * g w) / g w) \<midarrow>z\<rightarrow> 0"
-            apply (rule tendsto_eq_intros refl gz \<open>g z \<noteq> 0\<close>)+
-            by (simp add: True)
-          show "\<And>x. \<lbrakk>x \<in> ball z r; x \<noteq> z\<rbrakk> \<Longrightarrow> (1 + a * g x) / g x = f x"
-            using fab fab zrM by (fastforce simp add: gf field_split_simps)
-        qed (use \<open>0 < r\<close> in auto)
-        then show ?thesis
-          using that by blast 
-      next
-        case False
-        have *: "\<lbrakk>g \<noteq> 0; inverse g = f - a\<rbrakk> \<Longrightarrow> g / (1 + a * g) = inverse f" for f g::complex
-          by (auto simp: field_simps)
-        have "(inverse \<circ> f) \<midarrow>z\<rightarrow> g z / (1 + a * g z)"
-        proof (rule Lim_transform_within_open [of "\<lambda>w. g w / (1 + a * g w)" _ _ UNIV "ball z r"])
-          show "(\<lambda>w. g w / (1 + a * g w)) \<midarrow>z\<rightarrow> g z / (1 + a * g z)"
-            using False by (auto simp: False intro!: tendsto_eq_intros gz)
-          show "\<And>x. \<lbrakk>x \<in> ball z r; x \<noteq> z\<rbrakk> \<Longrightarrow> g x / (1 + a * g x) = (inverse \<circ> f) x"
-            using * gf gnz by simp
-        qed (use \<open>0 < r\<close> in auto)
-        with that show ?thesis by blast
-      qed
-    qed 
-  qed
-qed
-
-
-corollary great_Picard_alt:
-  assumes M: "open M" "z \<in> M" and holf: "f holomorphic_on (M - {z})"
-    and non: "\<And>l. \<not> (f \<longlongrightarrow> l) (at z)" "\<And>l. \<not> ((inverse \<circ> f) \<longlongrightarrow> l) (at z)"
-  obtains a where "- {a} \<subseteq> f ` (M - {z})"
-  apply (simp add: subset_iff image_iff)
-  by (metis great_Picard [OF M _ holf] non)
-    
-
-corollary great_Picard_infinite:
-  assumes M: "open M" "z \<in> M" and holf: "f holomorphic_on (M - {z})"
-    and non: "\<And>l. \<not> (f \<longlongrightarrow> l) (at z)" "\<And>l. \<not> ((inverse \<circ> f) \<longlongrightarrow> l) (at z)"
-  obtains a where "\<And>w. w \<noteq> a \<Longrightarrow> infinite {x. x \<in> M - {z} \<and> f x = w}"
-proof -
-  have False if "a \<noteq> b" and ab: "finite {x. x \<in> M - {z} \<and> f x = a}" "finite {x. x \<in> M - {z} \<and> f x = b}" for a b
-  proof -
-    have finab: "finite {x. x \<in> M - {z} \<and> f x \<in> {a,b}}"
-      using finite_UnI [OF ab]  unfolding mem_Collect_eq insert_iff empty_iff
-      by (simp add: conj_disj_distribL)
-    obtain r where "0 < r" and zrM: "ball z r \<subseteq> M" and r: "\<And>x. \<lbrakk>x \<in> M - {z}; f x \<in> {a,b}\<rbrakk> \<Longrightarrow> x \<notin> ball z r"
-    proof -
-      obtain e where "e > 0" and e: "ball z e \<subseteq> M"
-        using assms openE by blast
-      show ?thesis
-      proof (cases "{x \<in> M - {z}. f x \<in> {a, b}} = {}")
-        case True
-        then show ?thesis
-          apply (rule_tac r=e in that)
-          using e \<open>e > 0\<close> by auto
-      next
-        case False
-        let ?r = "min e (Min (dist z ` {x \<in> M - {z}. f x \<in> {a,b}}))"
-        show ?thesis
-        proof
-          show "0 < ?r"
-            using min_less_iff_conj Min_gr_iff finab False \<open>0 < e\<close> by auto
-          have "ball z ?r \<subseteq> ball z e"
-            by (simp add: subset_ball)
-          with e show "ball z ?r \<subseteq> M" by blast
-          show "\<And>x. \<lbrakk>x \<in> M - {z}; f x \<in> {a, b}\<rbrakk> \<Longrightarrow> x \<notin> ball z ?r"
-            using min_less_iff_conj Min_gr_iff finab False \<open>0 < e\<close> by auto
-        qed
-      qed
-    qed
-    have holfb: "f holomorphic_on (ball z r - {z})"
-      apply (rule holomorphic_on_subset [OF holf])
-       using zrM by auto
-     show ?thesis
-       apply (rule great_Picard [OF open_ball _ \<open>a \<noteq> b\<close> holfb])
-      using non \<open>0 < r\<close> r zrM by auto
-  qed
-  with that show thesis
-    by meson
-qed
-
-theorem Casorati_Weierstrass:
-  assumes "open M" "z \<in> M" "f holomorphic_on (M - {z})"
-      and "\<And>l. \<not> (f \<longlongrightarrow> l) (at z)" "\<And>l. \<not> ((inverse \<circ> f) \<longlongrightarrow> l) (at z)"
-  shows "closure(f ` (M - {z})) = UNIV"
-proof -
-  obtain a where a: "- {a} \<subseteq> f ` (M - {z})"
-    using great_Picard_alt [OF assms] .
-  have "UNIV = closure(- {a})"
-    by (simp add: closure_interior)
-  also have "... \<subseteq> closure(f ` (M - {z}))"
-    by (simp add: a closure_mono)
-  finally show ?thesis
-    by blast 
-qed
-  
-end
--- a/src/HOL/Analysis/Line_Segment.thy	Wed Nov 27 16:54:33 2019 +0000
+++ b/src/HOL/Analysis/Line_Segment.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -576,6 +576,42 @@
   fixes u::real shows "closed_segment u v = (\<lambda>x. (v - u) * x + u) ` {0..1}"
   by (simp add: add.commute [of u] image_affinity_atLeastAtMost [where c=u] closed_segment_eq_real_ivl)
 
+lemma closed_segment_same_Re:
+  assumes "Re a = Re b"
+  shows   "closed_segment a b = {z. Re z = Re a \<and> Im z \<in> closed_segment (Im a) (Im b)}"
+proof safe
+  fix z assume "z \<in> closed_segment a b"
+  then obtain u where u: "u \<in> {0..1}" "z = a + of_real u * (b - a)"
+    by (auto simp: closed_segment_def scaleR_conv_of_real algebra_simps)
+  from assms show "Re z = Re a" by (auto simp: u)
+  from u(1) show "Im z \<in> closed_segment (Im a) (Im b)"
+    by (force simp: u closed_segment_def algebra_simps)
+next
+  fix z assume [simp]: "Re z = Re a" and "Im z \<in> closed_segment (Im a) (Im b)"
+  then obtain u where u: "u \<in> {0..1}" "Im z = Im a + of_real u * (Im b - Im a)"
+    by (auto simp: closed_segment_def scaleR_conv_of_real algebra_simps)
+  from u(1) show "z \<in> closed_segment a b" using assms
+    by (force simp: u closed_segment_def algebra_simps scaleR_conv_of_real complex_eq_iff)
+qed
+
+lemma closed_segment_same_Im:
+  assumes "Im a = Im b"
+  shows   "closed_segment a b = {z. Im z = Im a \<and> Re z \<in> closed_segment (Re a) (Re b)}"
+proof safe
+  fix z assume "z \<in> closed_segment a b"
+  then obtain u where u: "u \<in> {0..1}" "z = a + of_real u * (b - a)"
+    by (auto simp: closed_segment_def scaleR_conv_of_real algebra_simps)
+  from assms show "Im z = Im a" by (auto simp: u)
+  from u(1) show "Re z \<in> closed_segment (Re a) (Re b)"
+    by (force simp: u closed_segment_def algebra_simps)
+next
+  fix z assume [simp]: "Im z = Im a" and "Re z \<in> closed_segment (Re a) (Re b)"
+  then obtain u where u: "u \<in> {0..1}" "Re z = Re a + of_real u * (Re b - Re a)"
+    by (auto simp: closed_segment_def scaleR_conv_of_real algebra_simps)
+  from u(1) show "z \<in> closed_segment a b" using assms
+    by (force simp: u closed_segment_def algebra_simps scaleR_conv_of_real complex_eq_iff)
+qed
+
 lemma dist_in_closed_segment:
   fixes a :: "'a :: euclidean_space"
   assumes "x \<in> closed_segment a b"
--- a/src/HOL/Analysis/Path_Connected.thy	Wed Nov 27 16:54:33 2019 +0000
+++ b/src/HOL/Analysis/Path_Connected.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -1063,6 +1063,9 @@
 definition\<^marker>\<open>tag important\<close> shiftpath :: "real \<Rightarrow> (real \<Rightarrow> 'a::topological_space) \<Rightarrow> real \<Rightarrow> 'a"
   where "shiftpath a f = (\<lambda>x. if (a + x) \<le> 1 then f (a + x) else f (a + x - 1))"
 
+lemma shiftpath_alt_def: "shiftpath a f = (\<lambda>x. if x \<le> 1-a then f (a + x) else f (a + x - 1))"
+  by (auto simp: shiftpath_def)
+
 lemma pathstart_shiftpath: "a \<le> 1 \<Longrightarrow> pathstart (shiftpath a g) = g a"
   unfolding pathstart_def shiftpath_def by auto
 
@@ -1273,6 +1276,55 @@
   fixes a::"'a::linordered_idom" shows "\<lbrakk>a \<le> 1; b \<le> 1; 0 \<le> u; u \<le> 1\<rbrakk> \<Longrightarrow> (1 - u) * a + u * b \<le> 1"
   using mult_left_le [of a "1-u"] mult_left_le [of b u] by auto
 
+lemma linepath_in_path:
+  shows "x \<in> {0..1} \<Longrightarrow> linepath a b x \<in> 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 \<in> convex hull s"
+        and b: "b \<in> convex hull s"
+        and x: "0\<le>x" "x\<le>1"
+       shows "linepath a b x \<in> 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 bounded_linear_linepath:
+  assumes "bounded_linear f"
+  shows   "f (linepath a b x) = linepath (f a) (f b) x"
+proof -
+  interpret f: bounded_linear f by fact
+  show ?thesis by (simp add: linepath_def f.add f.scale)
+qed
+
+lemma bounded_linear_linepath':
+  assumes "bounded_linear f"
+  shows   "f \<circ> linepath a b = linepath (f a) (f b)"
+  using bounded_linear_linepath[OF assms] by (simp add: fun_eq_iff)
+
+lemma linepath_cnj': "cnj \<circ> linepath a b = linepath (cnj a) (cnj b)"
+  by (simp add: linepath_def fun_eq_iff)
+
+lemma differentiable_linepath [intro]: "linepath a b differentiable at x within A"
+  by (auto simp: linepath_def)
+
+lemma has_vector_derivative_linepath_within:
+    "(linepath a b has_vector_derivative (b - a)) (at x within s)"
+apply (simp add: linepath_def has_vector_derivative_def algebra_simps)
+apply (rule derivative_eq_intros | simp)+
+done
+
 
 subsection\<^marker>\<open>tag unimportant\<close>\<open>Segments via convex hulls\<close>
 
@@ -4003,4 +4055,60 @@
   shows "\<exists>g. homeomorphism S T f g"
   using assms injective_into_1d_eq_homeomorphism by blast
 
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Rectangular paths\<close>
+
+definition\<^marker>\<open>tag unimportant\<close> rectpath where
+  "rectpath a1 a3 = (let a2 = Complex (Re a3) (Im a1); a4 = Complex (Re a1) (Im a3)
+                      in linepath a1 a2 +++ linepath a2 a3 +++ linepath a3 a4 +++ linepath a4 a1)"
+
+lemma path_rectpath [simp, intro]: "path (rectpath a b)"
+  by (simp add: Let_def rectpath_def)
+
+lemma pathstart_rectpath [simp]: "pathstart (rectpath a1 a3) = a1"
+  by (simp add: rectpath_def Let_def)
+
+lemma pathfinish_rectpath [simp]: "pathfinish (rectpath a1 a3) = a1"
+  by (simp add: rectpath_def Let_def)
+
+lemma simple_path_rectpath [simp, intro]:
+  assumes "Re a1 \<noteq> Re a3" "Im a1 \<noteq> Im a3"
+  shows   "simple_path (rectpath a1 a3)"
+  unfolding rectpath_def Let_def using assms
+  by (intro simple_path_join_loop arc_join arc_linepath)
+     (auto simp: complex_eq_iff path_image_join closed_segment_same_Re closed_segment_same_Im)
+
+lemma path_image_rectpath:
+  assumes "Re a1 \<le> Re a3" "Im a1 \<le> Im a3"
+  shows "path_image (rectpath a1 a3) =
+           {z. Re z \<in> {Re a1, Re a3} \<and> Im z \<in> {Im a1..Im a3}} \<union>
+           {z. Im z \<in> {Im a1, Im a3} \<and> Re z \<in> {Re a1..Re a3}}" (is "?lhs = ?rhs")
+proof -
+  define a2 a4 where "a2 = Complex (Re a3) (Im a1)" and "a4 = Complex (Re a1) (Im a3)"
+  have "?lhs = closed_segment a1 a2 \<union> closed_segment a2 a3 \<union>
+                  closed_segment a4 a3 \<union> closed_segment a1 a4"
+    by (simp_all add: rectpath_def Let_def path_image_join closed_segment_commute
+                      a2_def a4_def Un_assoc)
+  also have "\<dots> = ?rhs" using assms
+    by (auto simp: rectpath_def Let_def path_image_join a2_def a4_def
+          closed_segment_same_Re closed_segment_same_Im closed_segment_eq_real_ivl)
+  finally show ?thesis .
+qed
+
+lemma path_image_rectpath_subset_cbox:
+  assumes "Re a \<le> Re b" "Im a \<le> Im b"
+  shows   "path_image (rectpath a b) \<subseteq> cbox a b"
+  using assms by (auto simp: path_image_rectpath in_cbox_complex_iff)
+
+lemma path_image_rectpath_inter_box:
+  assumes "Re a \<le> Re b" "Im a \<le> Im b"
+  shows   "path_image (rectpath a b) \<inter> box a b = {}"
+  using assms by (auto simp: path_image_rectpath in_box_complex_iff)
+
+lemma path_image_rectpath_cbox_minus_box:
+  assumes "Re a \<le> Re b" "Im a \<le> Im b"
+  shows   "path_image (rectpath a b) = cbox a b - box a b"
+  using assms by (auto simp: path_image_rectpath in_cbox_complex_iff
+                             in_box_complex_iff)
+
 end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Analysis/Smooth_Paths.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -0,0 +1,490 @@
+(*
+  Material originally from HOL Light, ported by Larry Paulson, moved here by Manuel Eberl
+*)
+section\<^marker>\<open>tag unimportant\<close> \<open>Smooth paths\<close>
+theory Smooth_Paths
+  imports
+  Retracts
+begin
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Homeomorphisms of arc images\<close>
+
+lemma homeomorphism_arc:
+  fixes g :: "real \<Rightarrow> 'a::t2_space"
+  assumes "arc g"
+  obtains h where "homeomorphism {0..1} (path_image g) g h"
+using assms by (force simp: arc_def homeomorphism_compact path_def path_image_def)
+
+lemma homeomorphic_arc_image_interval:
+  fixes g :: "real \<Rightarrow> '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 "\<dots> homeomorphic {a..b}"
+    using assms by (force intro: homeomorphic_closed_intervals_real)
+  finally show ?thesis .
+qed
+
+lemma homeomorphic_arc_images:
+  fixes g :: "real \<Rightarrow> 'a::t2_space" and h :: "real \<Rightarrow> '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 "\<dots> homeomorphic (path_image h)"
+    by (meson assms homeomorphic_def homeomorphism_arc)
+  finally show ?thesis .
+qed
+
+lemma path_connected_arc_complement:
+  fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
+  assumes "arc \<gamma>" "2 \<le> DIM('a)"
+  shows "path_connected(- path_image \<gamma>)"
+proof -
+  have "path_image \<gamma> homeomorphic {0..1::real}"
+    by (simp add: assms homeomorphic_arc_image_interval)
+  then
+  show ?thesis
+    apply (rule path_connected_complement_homeomorphic_convex_compact)
+      apply (auto simp: assms)
+    done
+qed
+
+lemma connected_arc_complement:
+  fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
+  assumes "arc \<gamma>" "2 \<le> DIM('a)"
+  shows "connected(- path_image \<gamma>)"
+  by (simp add: assms path_connected_arc_complement path_connected_imp_connected)
+
+lemma inside_arc_empty:
+  fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
+  assumes "arc \<gamma>"
+    shows "inside(path_image \<gamma>) = {}"
+proof (cases "DIM('a) = 1")
+  case True
+  then show ?thesis
+    using assms connected_arc_image connected_convex_1_gen inside_convex by blast
+next
+  case False
+  show ?thesis
+  proof (rule inside_bounded_complement_connected_empty)
+    show "connected (- path_image \<gamma>)"
+      apply (rule connected_arc_complement [OF assms])
+      using False
+      by (metis DIM_ge_Suc0 One_nat_def Suc_1 not_less_eq_eq order_class.order.antisym)
+    show "bounded (path_image \<gamma>)"
+      by (simp add: assms bounded_arc_image)
+  qed
+qed
+
+lemma inside_simple_curve_imp_closed:
+  fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
+    shows "\<lbrakk>simple_path \<gamma>; x \<in> inside(path_image \<gamma>)\<rbrakk> \<Longrightarrow> pathfinish \<gamma> = pathstart \<gamma>"
+  using arc_simple_path  inside_arc_empty by blast
+
+
+subsection \<open>Piecewise differentiability of paths\<close>
+
+lemma continuous_on_joinpaths_D1:
+    "continuous_on {0..1} (g1 +++ g2) \<Longrightarrow> continuous_on {0..1} g1"
+  apply (rule continuous_on_eq [of _ "(g1 +++ g2) \<circ> ((*)(inverse 2))"])
+  apply (rule continuous_intros | simp)+
+  apply (auto elim!: continuous_on_subset simp: joinpaths_def)
+  done
+
+lemma continuous_on_joinpaths_D2:
+    "\<lbrakk>continuous_on {0..1} (g1 +++ g2); pathfinish g1 = pathstart g2\<rbrakk> \<Longrightarrow> continuous_on {0..1} g2"
+  apply (rule continuous_on_eq [of _ "(g1 +++ g2) \<circ> (\<lambda>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:
+  assumes "(g1 +++ g2) piecewise_differentiable_on {0..1}"
+  shows "g1 piecewise_differentiable_on {0..1}"
+proof -
+  obtain S where cont: "continuous_on {0..1} g1" and "finite S"
+    and S: "\<And>x. x \<in> {0..1} - S \<Longrightarrow> g1 +++ g2 differentiable at x within {0..1}"
+    using assms unfolding piecewise_differentiable_on_def
+    by (blast dest!: continuous_on_joinpaths_D1)
+  show ?thesis
+    unfolding piecewise_differentiable_on_def
+  proof (intro exI conjI ballI cont)
+    show "finite (insert 1 (((*)2) ` S))"
+      by (simp add: \<open>finite S\<close>)
+    show "g1 differentiable at x within {0..1}" if "x \<in> {0..1} - insert 1 ((*) 2 ` S)" for x
+    proof (rule_tac d="dist (x/2) (1/2)" in differentiable_transform_within)
+      have "g1 +++ g2 differentiable at (x / 2) within {0..1/2}"
+        by (rule differentiable_subset [OF S [of "x/2"]] | use that in force)+
+      then show "g1 +++ g2 \<circ> (*) (inverse 2) differentiable at x within {0..1}"
+        using image_affinity_atLeastAtMost_div [of 2 0 "0::real" 1]
+        by (auto intro: differentiable_chain_within)
+    qed (use that in \<open>auto simp: joinpaths_def\<close>)
+  qed
+qed
+
+lemma piecewise_differentiable_D2:
+  assumes "(g1 +++ g2) piecewise_differentiable_on {0..1}" and eq: "pathfinish g1 = pathstart g2"
+  shows "g2 piecewise_differentiable_on {0..1}"
+proof -
+  have [simp]: "g1 1 = g2 0"
+    using eq by (simp add: pathfinish_def pathstart_def)
+  obtain S where cont: "continuous_on {0..1} g2" and "finite S"
+    and S: "\<And>x. x \<in> {0..1} - S \<Longrightarrow> g1 +++ g2 differentiable at x within {0..1}"
+    using assms unfolding piecewise_differentiable_on_def
+    by (blast dest!: continuous_on_joinpaths_D2)
+  show ?thesis
+    unfolding piecewise_differentiable_on_def
+  proof (intro exI conjI ballI cont)
+    show "finite (insert 0 ((\<lambda>x. 2*x-1)`S))"
+      by (simp add: \<open>finite S\<close>)
+    show "g2 differentiable at x within {0..1}" if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1)`S)" for x
+    proof (rule_tac d="dist ((x+1)/2) (1/2)" in differentiable_transform_within)
+      have x2: "(x + 1) / 2 \<notin> S"
+        using that
+        apply (clarsimp simp: image_iff)
+        by (metis add.commute add_diff_cancel_left' mult_2 field_sum_of_halves)
+      have "g1 +++ g2 \<circ> (\<lambda>x. (x+1) / 2) differentiable at x within {0..1}"
+        by (rule differentiable_chain_within differentiable_subset [OF S [of "(x+1)/2"]] | use x2 that in force)+
+      then show "g1 +++ g2 \<circ> (\<lambda>x. (x+1) / 2) differentiable at x within {0..1}"
+        by (auto intro: differentiable_chain_within)
+      show "(g1 +++ g2 \<circ> (\<lambda>x. (x + 1) / 2)) x' = g2 x'" if "x' \<in> {0..1}" "dist x' x < dist ((x + 1) / 2) (1/2)" for x'
+      proof -
+        have [simp]: "(2*x'+2)/2 = x'+1"
+          by (simp add: field_split_simps)
+        show ?thesis
+          using that by (auto simp: joinpaths_def)
+      qed
+    qed (use that in \<open>auto simp: joinpaths_def\<close>)
+  qed
+qed
+
+lemma piecewise_C1_differentiable_D1:
+  fixes g1 :: "real \<Rightarrow> '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) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
+             and g12D: "\<forall>x\<in>{0..1} - S. g1 +++ g2 differentiable at x"
+    using assms  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
+  have g1D: "g1 differentiable at x" if "x \<in> {0..1} - insert 1 ((*) 2 ` S)" for x
+  proof (rule differentiable_transform_within)
+    show "g1 +++ g2 \<circ> (*) (inverse 2) differentiable at x"
+      using that g12D
+      apply (simp only: joinpaths_def)
+      by (rule differentiable_chain_at derivative_intros | force)+
+    show "\<And>x'. \<lbrakk>dist x' x < dist (x/2) (1/2)\<rbrakk>
+          \<Longrightarrow> (g1 +++ g2 \<circ> (*) (inverse 2)) x' = g1 x'"
+      using that by (auto simp: dist_real_def joinpaths_def)
+  qed (use that in \<open>auto simp: dist_real_def\<close>)
+  have [simp]: "vector_derivative (g1 \<circ> (*) 2) (at (x/2)) = 2 *\<^sub>R vector_derivative g1 (at x)"
+               if "x \<in> {0..1} - insert 1 ((*) 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) (\<lambda>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) (\<lambda>x. vector_derivative (g1 \<circ> (*)2) (at x))"
+  proof (rule continuous_on_eq [OF _ vector_derivative_at])
+    show "(g1 +++ g2 has_vector_derivative vector_derivative (g1 \<circ> (*) 2) (at x)) (at x)"
+      if "x \<in> {0..1/2} - insert (1/2) S" for x
+    proof (rule has_vector_derivative_transform_within)
+      show "(g1 \<circ> (*) 2 has_vector_derivative vector_derivative (g1 \<circ> (*) 2) (at x)) (at x)"
+        using that
+        by (force intro: g1D differentiable_chain_at simp: vector_derivative_works [symmetric])
+      show "\<And>x'. \<lbrakk>dist x' x < dist x (1/2)\<rbrakk> \<Longrightarrow> (g1 \<circ> (*) 2) x' = (g1 +++ g2) x'"
+        using that by (auto simp: dist_norm joinpaths_def)
+    qed (use that in \<open>auto simp: dist_norm\<close>)
+  qed
+  have "continuous_on ({0..1} - insert 1 ((*) 2 ` S))
+                      ((\<lambda>x. 1/2 * vector_derivative (g1 \<circ> (*)2) (at x)) \<circ> (*)(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 ((*) 2 ` S)) (\<lambda>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 \<open>finite S\<close> show ?thesis
+    apply (clarsimp simp add: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
+    apply (rule_tac x="insert 1 (((*)2)`S)" in exI)
+    apply (simp add: g1D con_g1)
+  done
+qed
+
+lemma piecewise_C1_differentiable_D2:
+  fixes g2 :: "real \<Rightarrow> '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) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
+             and g12D: "\<forall>x\<in>{0..1} - S. g1 +++ g2 differentiable at x"
+    using assms  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
+  have g2D: "g2 differentiable at x" if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1) ` S)" for x
+  proof (rule differentiable_transform_within)
+    show "g1 +++ g2 \<circ> (\<lambda>x. (x + 1) / 2) differentiable at x"
+      using g12D that
+      apply (simp only: joinpaths_def)
+      apply (drule_tac x= "(x+1) / 2" in bspec, force simp: field_split_simps)
+      apply (rule differentiable_chain_at derivative_intros | force)+
+      done
+    show "\<And>x'. dist x' x < dist ((x + 1) / 2) (1/2) \<Longrightarrow> (g1 +++ g2 \<circ> (\<lambda>x. (x + 1) / 2)) x' = g2 x'"
+      using that by (auto simp: dist_real_def joinpaths_def field_simps)
+    qed (use that in \<open>auto simp: dist_norm\<close>)
+  have [simp]: "vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at ((x+1)/2)) = 2 *\<^sub>R vector_derivative g2 (at x)"
+               if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1) ` S)" for x
+    using that  by (auto simp: vector_derivative_chain_at field_split_simps g2D)
+  have "continuous_on ({1/2..1} - insert (1/2) S) (\<lambda>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) (\<lambda>x. vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at x))"
+  proof (rule continuous_on_eq [OF _ vector_derivative_at])
+    show "(g1 +++ g2 has_vector_derivative vector_derivative (g2 \<circ> (\<lambda>x. 2 * x - 1)) (at x))
+          (at x)"
+      if "x \<in> {1 / 2..1} - insert (1 / 2) S" for x
+    proof (rule_tac f="g2 \<circ> (\<lambda>x. 2*x-1)" and d="dist (3/4) ((x+1)/2)" in has_vector_derivative_transform_within)
+      show "(g2 \<circ> (\<lambda>x. 2 * x - 1) has_vector_derivative vector_derivative (g2 \<circ> (\<lambda>x. 2 * x - 1)) (at x))
+            (at x)"
+        using that by (force intro: g2D differentiable_chain_at simp: vector_derivative_works [symmetric])
+      show "\<And>x'. \<lbrakk>dist x' x < dist (3 / 4) ((x + 1) / 2)\<rbrakk> \<Longrightarrow> (g2 \<circ> (\<lambda>x. 2 * x - 1)) x' = (g1 +++ g2) x'"
+        using that by (auto simp: dist_norm joinpaths_def add_divide_distrib)
+    qed (use that in \<open>auto simp: dist_norm\<close>)
+  qed
+  have [simp]: "((\<lambda>x. (x+1) / 2) ` ({0..1} - insert 0 ((\<lambda>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 ((\<lambda>x. 2*x-1) ` S))
+                      ((\<lambda>x. 1/2 * vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at x)) \<circ> (\<lambda>x. (x+1)/2))"
+    by (rule continuous_intros | simp add:  coDhalf)+
+  then have con_g2: "continuous_on ({0..1} - insert 0 ((\<lambda>x. 2*x-1) ` S)) (\<lambda>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 \<open>finite S\<close> show ?thesis
+    apply (clarsimp simp add: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
+    apply (rule_tac x="insert 0 ((\<lambda>x. 2 * x - 1) ` S)" in exI)
+    apply (simp add: g2D con_g2)
+  done
+qed
+
+
+subsection \<open>Valid paths, and their start and finish\<close>
+
+definition\<^marker>\<open>tag important\<close> valid_path :: "(real \<Rightarrow> 'a :: real_normed_vector) \<Rightarrow> bool"
+  where "valid_path f \<equiv> f piecewise_C1_differentiable_on {0..1::real}"
+
+definition closed_path :: "(real \<Rightarrow> 'a :: real_normed_vector) \<Rightarrow> bool"
+  where "closed_path g \<equiv> g 0 = g 1"
+
+text\<open>In particular, all results for paths apply\<close>
+
+lemma valid_path_imp_path: "valid_path g \<Longrightarrow> path g"
+  by (simp add: path_def piecewise_C1_differentiable_on_def valid_path_def)
+
+lemma connected_valid_path_image: "valid_path g \<Longrightarrow> connected(path_image g)"
+  by (metis connected_path_image valid_path_imp_path)
+
+lemma compact_valid_path_image: "valid_path g \<Longrightarrow> compact(path_image g)"
+  by (metis compact_path_image valid_path_imp_path)
+
+lemma bounded_valid_path_image: "valid_path g \<Longrightarrow> bounded(path_image g)"
+  by (metis bounded_path_image valid_path_imp_path)
+
+lemma closed_valid_path_image: "valid_path g \<Longrightarrow> closed(path_image g)"
+  by (metis closed_path_image valid_path_imp_path)
+
+lemma valid_path_compose:
+  assumes "valid_path g"
+      and der: "\<And>x. x \<in> path_image g \<Longrightarrow> f field_differentiable (at x)"
+      and con: "continuous_on (path_image g) (deriv f)"
+    shows "valid_path (f \<circ> g)"
+proof -
+  obtain S where "finite S" and g_diff: "g C1_differentiable_on {0..1} - S"
+    using \<open>valid_path g\<close> unfolding valid_path_def piecewise_C1_differentiable_on_def by auto
+  have "f \<circ> g differentiable at t" when "t\<in>{0..1} - S" for t
+    proof (rule differentiable_chain_at)
+      show "g differentiable at t" using \<open>valid_path g\<close>
+        by (meson C1_differentiable_on_eq \<open>g C1_differentiable_on {0..1} - S\<close> that)
+    next
+      have "g t\<in>path_image g" using that DiffD1 image_eqI path_image_def by metis
+      then show "f differentiable at (g t)"
+        using der[THEN field_differentiable_imp_differentiable] by auto
+    qed
+  moreover have "continuous_on ({0..1} - S) (\<lambda>x. vector_derivative (f \<circ> g) (at x))"
+    proof (rule continuous_on_eq [where f = "\<lambda>x. vector_derivative g (at x) * deriv f (g x)"],
+        rule continuous_intros)
+      show "continuous_on ({0..1} - S) (\<lambda>x. vector_derivative g (at x))"
+        using g_diff C1_differentiable_on_eq by auto
+    next
+      have "continuous_on {0..1} (\<lambda>x. deriv f (g x))"
+        using continuous_on_compose[OF _ con[unfolded path_image_def],unfolded comp_def]
+          \<open>valid_path g\<close> piecewise_C1_differentiable_on_def valid_path_def
+        by blast
+      then show "continuous_on ({0..1} - S) (\<lambda>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 \<circ> g) (at t)"
+          when "t \<in> {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\<in>path_image g" using that DiffD1 image_eqI path_image_def by metis
+          then show "f field_differentiable at (g t)" using der by auto
+        qed
+    qed
+  ultimately have "f \<circ> g C1_differentiable_on {0..1} - S"
+    using C1_differentiable_on_eq by blast
+  moreover have "path (f \<circ> g)"
+    apply (rule path_continuous_image[OF valid_path_imp_path[OF \<open>valid_path g\<close>]])
+    using der
+    by (simp add: continuous_at_imp_continuous_on field_differentiable_imp_continuous_at)
+  ultimately show ?thesis unfolding valid_path_def piecewise_C1_differentiable_on_def path_def
+    using \<open>finite S\<close> by auto
+qed
+  
+lemma valid_path_uminus_comp[simp]:
+  fixes g::"real \<Rightarrow> 'a ::real_normed_field"
+  shows "valid_path (uminus \<circ> g) \<longleftrightarrow> valid_path g"
+proof 
+  show "valid_path g \<Longrightarrow> valid_path (uminus \<circ> g)" for g::"real \<Rightarrow> 'a"
+    by (auto intro!: valid_path_compose derivative_intros)  
+  then show "valid_path g" when "valid_path (uminus \<circ> g)"
+    by (metis fun.map_comp group_add_class.minus_comp_minus id_comp that)
+qed
+
+lemma valid_path_offset[simp]:
+  shows "valid_path (\<lambda>t. g t - z) \<longleftrightarrow> valid_path g"  
+proof 
+  show *: "valid_path (g::real\<Rightarrow>'a) \<Longrightarrow> valid_path (\<lambda>t. g t - z)" for g z
+    unfolding valid_path_def
+    by (fastforce intro:derivative_intros C1_differentiable_imp_piecewise piecewise_C1_differentiable_diff)
+  show "valid_path (\<lambda>t. g t - z) \<Longrightarrow> valid_path g"
+    using *[of "\<lambda>t. g t - z" "-z",simplified] .
+qed
+
+lemma valid_path_imp_reverse:
+  assumes "valid_path g"
+    shows "valid_path(reversepath g)"
+proof -
+  obtain S where "finite S" and S: "g C1_differentiable_on ({0..1} - S)"
+    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
+  then have "finite ((-) 1 ` S)"
+    by auto
+  moreover have "(reversepath g C1_differentiable_on ({0..1} - (-) 1 ` S))"
+    unfolding reversepath_def
+    apply (rule C1_differentiable_compose [of "\<lambda>x::real. 1-x" _ g, unfolded o_def])
+    using S
+    by (force simp: finite_vimageI inj_on_def C1_differentiable_on_eq elim!: continuous_on_subset)+
+  ultimately 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) \<longleftrightarrow> valid_path g"
+  using valid_path_imp_reverse by force
+
+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 \<circ> (\<lambda>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 (force intro: finite_vimageI [where h = "(*)2"] inj_onI)
+    done
+  moreover have "(g2 \<circ> (\<lambda>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 = "(\<lambda>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 \<Rightarrow> 'a::real_normed_field"
+  shows "valid_path (g1 +++ g2) \<Longrightarrow> valid_path g1"
+  unfolding valid_path_def
+  by (rule piecewise_C1_differentiable_D1)
+
+lemma valid_path_join_D2:
+  fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
+  shows "\<lbrakk>valid_path (g1 +++ g2); pathfinish g1 = pathstart g2\<rbrakk> \<Longrightarrow> valid_path g2"
+  unfolding valid_path_def
+  by (rule piecewise_C1_differentiable_D2)
+
+lemma valid_path_join_eq [simp]:
+  fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
+  shows "pathfinish g1 = pathstart g2 \<Longrightarrow> (valid_path(g1 +++ g2) \<longleftrightarrow> valid_path g1 \<and> valid_path g2)"
+  using valid_path_join_D1 valid_path_join_D2 valid_path_join by blast
+
+lemma valid_path_shiftpath [intro]:
+  assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {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 vector_derivative_linepath_within:
+    "x \<in> {0..1} \<Longrightarrow> vector_derivative (linepath a b) (at x within {0..1}) = b - a"
+  apply (rule vector_derivative_within_cbox [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 valid_path_subpath:
+  fixes g :: "real \<Rightarrow> 'a :: real_normed_vector"
+  assumes "valid_path g" "u \<in> {0..1}" "v \<in> {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 \<circ> (\<lambda>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 valid_path_rectpath [simp, intro]: "valid_path (rectpath a b)"
+  by (simp add: Let_def rectpath_def)
+
+end
\ No newline at end of file
--- a/src/HOL/Analysis/Vitali_Covering_Theorem.thy	Wed Nov 27 16:54:33 2019 +0000
+++ b/src/HOL/Analysis/Vitali_Covering_Theorem.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -584,7 +584,7 @@
       have "\<exists>U. case p of (x,d) \<Rightarrow> S \<inter> ball x d \<subseteq> U \<and>
                         U \<in> lmeasurable \<and> ?\<mu> U < e / ?\<mu> (ball z 1) * ?\<mu> (ball x d)"
         if "p \<in> C" for p
-        using that Csub by auto
+        using that Csub unfolding case_prod_unfold by blast
       then obtain U where U:
                 "\<And>p. p \<in> C \<Longrightarrow>
                        case p of (x,d) \<Rightarrow> S \<inter> ball x d \<subseteq> U p \<and>
--- a/src/HOL/Analysis/Winding_Numbers.thy	Wed Nov 27 16:54:33 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1211 +0,0 @@
-section \<open>Winding Numbers\<close>
-
-text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2017)\<close>
-
-theory Winding_Numbers
-imports
-  Polytope
-  Jordan_Curve
-  Riemann_Mapping
-begin
-
-lemma simply_connected_inside_simple_path:
-  fixes p :: "real \<Rightarrow> complex"
-  shows "simple_path p \<Longrightarrow> simply_connected(inside(path_image p))"
-  using Jordan_inside_outside connected_simple_path_image inside_simple_curve_imp_closed simply_connected_eq_frontier_properties
-  by fastforce
-
-lemma simply_connected_Int:
-  fixes S :: "complex set"
-  assumes "open S" "open T" "simply_connected S" "simply_connected T" "connected (S \<inter> T)"
-  shows "simply_connected (S \<inter> T)"
-  using assms by (force simp: simply_connected_eq_winding_number_zero open_Int)
-
-subsection\<open>Winding number for a triangle\<close>
-
-lemma wn_triangle1:
-  assumes "0 \<in> interior(convex hull {a,b,c})"
-    shows "\<not> (Im(a/b) \<le> 0 \<and> 0 \<le> Im(b/c))"
-proof -
-  { assume 0: "Im(a/b) \<le> 0" "0 \<le> Im(b/c)"
-    have "0 \<notin> interior (convex hull {a,b,c})"
-    proof (cases "a=0 \<or> b=0 \<or> c=0")
-      case True then show ?thesis
-        by (auto simp: not_in_interior_convex_hull_3)
-    next
-      case False
-      then have "b \<noteq> 0" by blast
-      { fix x y::complex and u::real
-        assume eq_f': "Im x * Re b \<le> Im b * Re x" "Im y * Re b \<le> Im b * Re y" "0 \<le> u" "u \<le> 1"
-        then have "((1 - u) * Im x) * Re b \<le> Im b * ((1 - u) * Re x)"
-          by (simp add: mult_left_mono mult.assoc mult.left_commute [of "Im b"])
-        then have "((1 - u) * Im x + u * Im y) * Re b \<le> Im b * ((1 - u) * Re x + u * Re y)"
-          using eq_f' ordered_comm_semiring_class.comm_mult_left_mono
-          by (fastforce simp add: algebra_simps)
-      }
-      with False 0 have "convex hull {a,b,c} \<le> {z. Im z * Re b \<le> Im b * Re z}"
-        apply (simp add: Complex.Im_divide divide_simps complex_neq_0 [symmetric])
-        apply (simp add: algebra_simps)
-        apply (rule hull_minimal)
-        apply (auto simp: algebra_simps convex_alt)
-        done
-      moreover have "0 \<notin> interior({z. Im z * Re b \<le> Im b * Re z})"
-      proof
-        assume "0 \<in> interior {z. Im z * Re b \<le> Im b * Re z}"
-        then obtain e where "e>0" and e: "ball 0 e \<subseteq> {z. Im z * Re b \<le> Im b * Re z}"
-          by (meson mem_interior)
-        define z where "z \<equiv> - sgn (Im b) * (e/3) + sgn (Re b) * (e/3) * \<i>"
-        have "z \<in> ball 0 e"
-          using \<open>e>0\<close>
-          apply (simp add: z_def dist_norm)
-          apply (rule le_less_trans [OF norm_triangle_ineq4])
-          apply (simp add: norm_mult abs_sgn_eq)
-          done
-        then have "z \<in> {z. Im z * Re b \<le> Im b * Re z}"
-          using e by blast
-        then show False
-          using \<open>e>0\<close> \<open>b \<noteq> 0\<close>
-          apply (simp add: z_def dist_norm sgn_if less_eq_real_def mult_less_0_iff complex.expand split: if_split_asm)
-          apply (auto simp: algebra_simps)
-          apply (meson less_asym less_trans mult_pos_pos neg_less_0_iff_less)
-          by (metis less_asym mult_pos_pos neg_less_0_iff_less)
-      qed
-      ultimately show ?thesis
-        using interior_mono by blast
-    qed
-  } with assms show ?thesis by blast
-qed
-
-lemma wn_triangle2_0:
-  assumes "0 \<in> interior(convex hull {a,b,c})"
-  shows
-       "0 < Im((b - a) * cnj (b)) \<and>
-        0 < Im((c - b) * cnj (c)) \<and>
-        0 < Im((a - c) * cnj (a))
-        \<or>
-        Im((b - a) * cnj (b)) < 0 \<and>
-        0 < Im((b - c) * cnj (b)) \<and>
-        0 < Im((a - b) * cnj (a)) \<and>
-        0 < Im((c - a) * cnj (c))"
-proof -
-  have [simp]: "{b,c,a} = {a,b,c}" "{c,a,b} = {a,b,c}" by auto
-  show ?thesis
-    using wn_triangle1 [OF assms] wn_triangle1 [of b c a] wn_triangle1 [of c a b] assms
-    by (auto simp: algebra_simps Im_complex_div_gt_0 Im_complex_div_lt_0 not_le not_less)
-qed
-
-lemma wn_triangle2:
-  assumes "z \<in> interior(convex hull {a,b,c})"
-   shows "0 < Im((b - a) * cnj (b - z)) \<and>
-          0 < Im((c - b) * cnj (c - z)) \<and>
-          0 < Im((a - c) * cnj (a - z))
-          \<or>
-          Im((b - a) * cnj (b - z)) < 0 \<and>
-          0 < Im((b - c) * cnj (b - z)) \<and>
-          0 < Im((a - b) * cnj (a - z)) \<and>
-          0 < Im((c - a) * cnj (c - z))"
-proof -
-  have 0: "0 \<in> interior(convex hull {a-z, b-z, c-z})"
-    using assms convex_hull_translation [of "-z" "{a,b,c}"]
-                interior_translation [of "-z"]
-    by (simp cong: image_cong_simp)
-  show ?thesis using wn_triangle2_0 [OF 0]
-    by simp
-qed
-
-lemma wn_triangle3:
-  assumes z: "z \<in> interior(convex hull {a,b,c})"
-      and "0 < Im((b-a) * cnj (b-z))"
-          "0 < Im((c-b) * cnj (c-z))"
-          "0 < Im((a-c) * cnj (a-z))"
-    shows "winding_number (linepath a b +++ linepath b c +++ linepath c a) z = 1"
-proof -
-  have znot[simp]: "z \<notin> closed_segment a b" "z \<notin> closed_segment b c" "z \<notin> closed_segment c a"
-    using z interior_of_triangle [of a b c]
-    by (auto simp: closed_segment_def)
-  have gt0: "0 < Re (winding_number (linepath a b +++ linepath b c +++ linepath c a) z)"
-    using assms
-    by (simp add: winding_number_linepath_pos_lt path_image_join winding_number_join_pos_combined)
-  have lt2: "Re (winding_number (linepath a b +++ linepath b c +++ linepath c a) z) < 2"
-    using winding_number_lt_half_linepath [of _ a b]
-    using winding_number_lt_half_linepath [of _ b c]
-    using winding_number_lt_half_linepath [of _ c a] znot
-    apply (fastforce simp add: winding_number_join path_image_join)
-    done
-  show ?thesis
-    by (rule winding_number_eq_1) (simp_all add: path_image_join gt0 lt2)
-qed
-
-proposition winding_number_triangle:
-  assumes z: "z \<in> interior(convex hull {a,b,c})"
-    shows "winding_number(linepath a b +++ linepath b c +++ linepath c a) z =
-           (if 0 < Im((b - a) * cnj (b - z)) then 1 else -1)"
-proof -
-  have [simp]: "{a,c,b} = {a,b,c}"  by auto
-  have znot[simp]: "z \<notin> closed_segment a b" "z \<notin> closed_segment b c" "z \<notin> closed_segment c a"
-    using z interior_of_triangle [of a b c]
-    by (auto simp: closed_segment_def)
-  then have [simp]: "z \<notin> closed_segment b a" "z \<notin> closed_segment c b" "z \<notin> closed_segment a c"
-    using closed_segment_commute by blast+
-  have *: "winding_number (linepath a b +++ linepath b c +++ linepath c a) z =
-            winding_number (reversepath (linepath a c +++ linepath c b +++ linepath b a)) z"
-    by (simp add: reversepath_joinpaths winding_number_join not_in_path_image_join)
-  show ?thesis
-    using wn_triangle2 [OF z] apply (rule disjE)
-    apply (simp add: wn_triangle3 z)
-    apply (simp add: path_image_join winding_number_reversepath * wn_triangle3 z)
-    done
-qed
-
-subsection\<open>Winding numbers for simple closed paths\<close>
-
-lemma winding_number_from_innerpath:
-  assumes "simple_path c1" and c1: "pathstart c1 = a" "pathfinish c1 = b"
-      and "simple_path c2" and c2: "pathstart c2 = a" "pathfinish c2 = b"
-      and "simple_path c" and c: "pathstart c = a" "pathfinish c = b"
-      and c1c2: "path_image c1 \<inter> path_image c2 = {a,b}"
-      and c1c:  "path_image c1 \<inter> path_image c = {a,b}"
-      and c2c:  "path_image c2 \<inter> path_image c = {a,b}"
-      and ne_12: "path_image c \<inter> inside(path_image c1 \<union> path_image c2) \<noteq> {}"
-      and z: "z \<in> inside(path_image c1 \<union> path_image c)"
-      and wn_d: "winding_number (c1 +++ reversepath c) z = d"
-      and "a \<noteq> b" "d \<noteq> 0"
-  obtains "z \<in> inside(path_image c1 \<union> path_image c2)" "winding_number (c1 +++ reversepath c2) z = d"
-proof -
-  obtain 0: "inside(path_image c1 \<union> path_image c) \<inter> inside(path_image c2 \<union> path_image c) = {}"
-     and 1: "inside(path_image c1 \<union> path_image c) \<union> inside(path_image c2 \<union> path_image c) \<union>
-             (path_image c - {a,b}) = inside(path_image c1 \<union> path_image c2)"
-    by (rule split_inside_simple_closed_curve
-              [OF \<open>simple_path c1\<close> c1 \<open>simple_path c2\<close> c2 \<open>simple_path c\<close> c \<open>a \<noteq> b\<close> c1c2 c1c c2c ne_12])
-  have znot: "z \<notin> path_image c"  "z \<notin> path_image c1" "z \<notin> path_image c2"
-    using union_with_outside z 1 by auto
-  have wn_cc2: "winding_number (c +++ reversepath c2) z = 0"
-    apply (rule winding_number_zero_in_outside)
-    apply (simp_all add: \<open>simple_path c2\<close> c c2 \<open>simple_path c\<close> simple_path_imp_path path_image_join)
-    by (metis "0" ComplI UnE disjoint_iff_not_equal sup.commute union_with_inside z znot)
-  show ?thesis
-  proof
-    show "z \<in> inside (path_image c1 \<union> path_image c2)"
-      using "1" z by blast
-    have "winding_number c1 z - winding_number c z = d "
-      using assms znot
-      by (metis wn_d winding_number_join simple_path_imp_path winding_number_reversepath add.commute path_image_reversepath path_reversepath pathstart_reversepath uminus_add_conv_diff)
-    then show "winding_number (c1 +++ reversepath c2) z = d"
-      using wn_cc2 by (simp add: winding_number_join simple_path_imp_path assms znot winding_number_reversepath)
-  qed
-qed
-
-lemma simple_closed_path_wn1:
-  fixes a::complex and e::real
-  assumes "0 < e"
-    and sp_pl: "simple_path(p +++ linepath (a - e) (a + e))"
-    and psp:   "pathstart p = a + e"
-    and pfp:   "pathfinish p = a - e"
-    and disj:  "ball a e \<inter> path_image p = {}"
-obtains z where "z \<in> inside (path_image (p +++ linepath (a - e) (a + e)))"
-                "cmod (winding_number (p +++ linepath (a - e) (a + e)) z) = 1"
-proof -
-  have "arc p" and arc_lp: "arc (linepath (a - e) (a + e))"
-    and pap: "path_image p \<inter> path_image (linepath (a - e) (a + e)) \<subseteq> {pathstart p, a-e}"
-    using simple_path_join_loop_eq [of "linepath (a - e) (a + e)" p] assms by auto
-  have mid_eq_a: "midpoint (a - e) (a + e) = a"
-    by (simp add: midpoint_def)
-  then have "a \<in> path_image(p +++ linepath (a - e) (a + e))"
-    apply (simp add: assms path_image_join)
-    by (metis midpoint_in_closed_segment)
-  have "a \<in> frontier(inside (path_image(p +++ linepath (a - e) (a + e))))"
-    apply (simp add: assms Jordan_inside_outside)
-    apply (simp_all add: assms path_image_join)
-    by (metis mid_eq_a midpoint_in_closed_segment)
-  with \<open>0 < e\<close> obtain c where c: "c \<in> inside (path_image(p +++ linepath (a - e) (a + e)))"
-                  and dac: "dist a c < e"
-    by (auto simp: frontier_straddle)
-  then have "c \<notin> path_image(p +++ linepath (a - e) (a + e))"
-    using inside_no_overlap by blast
-  then have "c \<notin> path_image p"
-            "c \<notin> closed_segment (a - of_real e) (a + of_real e)"
-    by (simp_all add: assms path_image_join)
-  with \<open>0 < e\<close> dac have "c \<notin> affine hull {a - of_real e, a + of_real e}"
-    by (simp add: segment_as_ball not_le)
-  with \<open>0 < e\<close> have *: "\<not> collinear {a - e, c,a + e}"
-    using collinear_3_affine_hull [of "a-e" "a+e"] by (auto simp: insert_commute)
-  have 13: "1/3 + 1/3 + 1/3 = (1::real)" by simp
-  have "(1/3) *\<^sub>R (a - of_real e) + (1/3) *\<^sub>R c + (1/3) *\<^sub>R (a + of_real e) \<in> interior(convex hull {a - e, c, a + e})"
-    using interior_convex_hull_3_minimal [OF * DIM_complex]
-    by clarsimp (metis 13 zero_less_divide_1_iff zero_less_numeral)
-  then obtain z where z: "z \<in> interior(convex hull {a - e, c, a + e})" by force
-  have [simp]: "z \<notin> closed_segment (a - e) c"
-    by (metis DIM_complex Diff_iff IntD2 inf_sup_absorb interior_of_triangle z)
-  have [simp]: "z \<notin> closed_segment (a + e) (a - e)"
-    by (metis DIM_complex DiffD2 Un_iff interior_of_triangle z)
-  have [simp]: "z \<notin> closed_segment c (a + e)"
-    by (metis (no_types, lifting) DIM_complex DiffD2 Un_insert_right inf_sup_aci(5) insertCI interior_of_triangle mk_disjoint_insert z)
-  show thesis
-  proof
-    have "norm (winding_number (linepath (a - e) c +++ linepath c (a + e) +++ linepath (a + e) (a - e)) z) = 1"
-      using winding_number_triangle [OF z] by simp
-    have zin: "z \<in> inside (path_image (linepath (a + e) (a - e)) \<union> path_image p)"
-      and zeq: "winding_number (linepath (a + e) (a - e) +++ reversepath p) z =
-                winding_number (linepath (a - e) c +++ linepath c (a + e) +++ linepath (a + e) (a - e)) z"
-    proof (rule winding_number_from_innerpath
-        [of "linepath (a + e) (a - e)" "a+e" "a-e" p
-          "linepath (a + e) c +++ linepath c (a - e)" z
-          "winding_number (linepath (a - e)  c +++ linepath  c (a + e) +++ linepath (a + e) (a - e)) z"])
-      show sp_aec: "simple_path (linepath (a + e) c +++ linepath c (a - e))"
-      proof (rule arc_imp_simple_path [OF arc_join])
-        show "arc (linepath (a + e) c)"
-          by (metis \<open>c \<notin> path_image p\<close> arc_linepath pathstart_in_path_image psp)
-        show "arc (linepath c (a - e))"
-          by (metis \<open>c \<notin> path_image p\<close> arc_linepath pathfinish_in_path_image pfp)
-        show "path_image (linepath (a + e) c) \<inter> path_image (linepath c (a - e)) \<subseteq> {pathstart (linepath c (a - e))}"
-          by clarsimp (metis "*" IntI Int_closed_segment closed_segment_commute singleton_iff)
-      qed auto
-      show "simple_path p"
-        using \<open>arc p\<close> arc_simple_path by blast
-      show sp_ae2: "simple_path (linepath (a + e) (a - e))"
-        using \<open>arc p\<close> arc_distinct_ends pfp psp by fastforce
-      show pa: "pathfinish (linepath (a + e) (a - e)) = a - e"
-           "pathstart (linepath (a + e) c +++ linepath c (a - e)) = a + e"
-           "pathfinish (linepath (a + e) c +++ linepath c (a - e)) = a - e"
-           "pathstart p = a + e" "pathfinish p = a - e"
-           "pathstart (linepath (a + e) (a - e)) = a + e"
-        by (simp_all add: assms)
-      show 1: "path_image (linepath (a + e) (a - e)) \<inter> path_image p = {a + e, a - e}"
-      proof
-        show "path_image (linepath (a + e) (a - e)) \<inter> path_image p \<subseteq> {a + e, a - e}"
-          using pap closed_segment_commute psp segment_convex_hull by fastforce
-        show "{a + e, a - e} \<subseteq> path_image (linepath (a + e) (a - e)) \<inter> path_image p"
-          using pap pathfinish_in_path_image pathstart_in_path_image pfp psp by fastforce
-      qed
-      show 2: "path_image (linepath (a + e) (a - e)) \<inter> path_image (linepath (a + e) c +++ linepath c (a - e)) =
-               {a + e, a - e}"  (is "?lhs = ?rhs")
-      proof
-        have "\<not> collinear {c, a + e, a - e}"
-          using * by (simp add: insert_commute)
-        then have "convex hull {a + e, a - e} \<inter> convex hull {a + e, c} = {a + e}"
-                  "convex hull {a + e, a - e} \<inter> convex hull {c, a - e} = {a - e}"
-          by (metis (full_types) Int_closed_segment insert_commute segment_convex_hull)+
-        then show "?lhs \<subseteq> ?rhs"
-          by (metis Int_Un_distrib equalityD1 insert_is_Un path_image_join path_image_linepath path_join_eq path_linepath segment_convex_hull simple_path_def sp_aec)
-        show "?rhs \<subseteq> ?lhs"
-          using segment_convex_hull by (simp add: path_image_join)
-      qed
-      have "path_image p \<inter> path_image (linepath (a + e) c) \<subseteq> {a + e}"
-      proof (clarsimp simp: path_image_join)
-        fix x
-        assume "x \<in> path_image p" and x_ac: "x \<in> closed_segment (a + e) c"
-        then have "dist x a \<ge> e"
-          by (metis IntI all_not_in_conv disj dist_commute mem_ball not_less)
-        with x_ac dac \<open>e > 0\<close> show "x = a + e"
-          by (auto simp: norm_minus_commute dist_norm closed_segment_eq_open dest: open_segment_furthest_le [where y=a])
-      qed
-      moreover
-      have "path_image p \<inter> path_image (linepath c (a - e)) \<subseteq> {a - e}"
-      proof (clarsimp simp: path_image_join)
-        fix x
-        assume "x \<in> path_image p" and x_ac: "x \<in> closed_segment c (a - e)"
-        then have "dist x a \<ge> e"
-          by (metis IntI all_not_in_conv disj dist_commute mem_ball not_less)
-        with x_ac dac \<open>e > 0\<close> show "x = a - e"
-          by (auto simp: norm_minus_commute dist_norm closed_segment_eq_open dest: open_segment_furthest_le [where y=a])
-      qed
-      ultimately
-      have "path_image p \<inter> path_image (linepath (a + e) c +++ linepath c (a - e)) \<subseteq> {a + e, a - e}"
-        by (force simp: path_image_join)
-      then show 3: "path_image p \<inter> path_image (linepath (a + e) c +++ linepath c (a - e)) = {a + e, a - e}"
-        apply (rule equalityI)
-        apply (clarsimp simp: path_image_join)
-        apply (metis pathstart_in_path_image psp pathfinish_in_path_image pfp)
-        done
-      show 4: "path_image (linepath (a + e) c +++ linepath c (a - e)) \<inter>
-               inside (path_image (linepath (a + e) (a - e)) \<union> path_image p) \<noteq> {}"
-        apply (clarsimp simp: path_image_join segment_convex_hull disjoint_iff_not_equal)
-        by (metis (no_types, hide_lams) UnI1 Un_commute c closed_segment_commute ends_in_segment(1) path_image_join
-                  path_image_linepath pathstart_linepath pfp segment_convex_hull)
-      show zin_inside: "z \<in> inside (path_image (linepath (a + e) (a - e)) \<union>
-                                    path_image (linepath (a + e) c +++ linepath c (a - e)))"
-        apply (simp add: path_image_join)
-        by (metis z inside_of_triangle DIM_complex Un_commute closed_segment_commute)
-      show 5: "winding_number
-             (linepath (a + e) (a - e) +++ reversepath (linepath (a + e) c +++ linepath c (a - e))) z =
-            winding_number (linepath (a - e) c +++ linepath c (a + e) +++ linepath (a + e) (a - e)) z"
-        by (simp add: reversepath_joinpaths path_image_join winding_number_join)
-      show 6: "winding_number (linepath (a - e) c +++ linepath c (a + e) +++ linepath (a + e) (a - e)) z \<noteq> 0"
-        by (simp add: winding_number_triangle z)
-      show "winding_number (linepath (a + e) (a - e) +++ reversepath p) z =
-            winding_number (linepath (a - e) c +++ linepath c (a + e) +++ linepath (a + e) (a - e)) z"
-        by (metis 1 2 3 4 5 6 pa sp_aec sp_ae2 \<open>arc p\<close> \<open>simple_path p\<close> arc_distinct_ends winding_number_from_innerpath zin_inside)
-    qed (use assms \<open>e > 0\<close> in auto)
-    show "z \<in> inside (path_image (p +++ linepath (a - e) (a + e)))"
-      using zin by (simp add: assms path_image_join Un_commute closed_segment_commute)
-    then have "cmod (winding_number (p +++ linepath (a - e) (a + e)) z) =
-               cmod ((winding_number(reversepath (p +++ linepath (a - e) (a + e))) z))"
-      apply (subst winding_number_reversepath)
-      using simple_path_imp_path sp_pl apply blast
-       apply (metis IntI emptyE inside_no_overlap)
-      by (simp add: inside_def)
-    also have "... = cmod (winding_number(linepath (a + e) (a - e) +++ reversepath p) z)"
-      by (simp add: pfp reversepath_joinpaths)
-    also have "... = cmod (winding_number (linepath (a - e) c +++ linepath c (a + e) +++ linepath (a + e) (a - e)) z)"
-      by (simp add: zeq)
-    also have "... = 1"
-      using z by (simp add: interior_of_triangle winding_number_triangle)
-    finally show "cmod (winding_number (p +++ linepath (a - e) (a + e)) z) = 1" .
-  qed
-qed
-
-lemma simple_closed_path_wn2:
-  fixes a::complex and d e::real
-  assumes "0 < d" "0 < e"
-    and sp_pl: "simple_path(p +++ linepath (a - d) (a + e))"
-    and psp:   "pathstart p = a + e"
-    and pfp:   "pathfinish p = a - d"
-obtains z where "z \<in> inside (path_image (p +++ linepath (a - d) (a + e)))"
-                "cmod (winding_number (p +++ linepath (a - d) (a + e)) z) = 1"
-proof -
-  have [simp]: "a + of_real x \<in> closed_segment (a - \<alpha>) (a - \<beta>) \<longleftrightarrow> x \<in> closed_segment (-\<alpha>) (-\<beta>)" for x \<alpha> \<beta>::real
-    using closed_segment_translation_eq [of a]
-    by (metis (no_types, hide_lams) add_uminus_conv_diff of_real_minus of_real_closed_segment)
-  have [simp]: "a - of_real x \<in> closed_segment (a + \<alpha>) (a + \<beta>) \<longleftrightarrow> -x \<in> closed_segment \<alpha> \<beta>" for x \<alpha> \<beta>::real
-    by (metis closed_segment_translation_eq diff_conv_add_uminus of_real_closed_segment of_real_minus)
-  have "arc p" and arc_lp: "arc (linepath (a - d) (a + e))" and "path p"
-    and pap: "path_image p \<inter> closed_segment (a - d) (a + e) \<subseteq> {a+e, a-d}"
-    using simple_path_join_loop_eq [of "linepath (a - d) (a + e)" p] assms arc_imp_path  by auto
-  have "0 \<in> closed_segment (-d) e"
-    using \<open>0 < d\<close> \<open>0 < e\<close> closed_segment_eq_real_ivl by auto
-  then have "a \<in> path_image (linepath (a - d) (a + e))"
-    using of_real_closed_segment [THEN iffD2]
-    by (force dest: closed_segment_translation_eq [of a, THEN iffD2] simp del: of_real_closed_segment)
-  then have "a \<notin> path_image p"
-    using \<open>0 < d\<close> \<open>0 < e\<close> pap by auto
-  then obtain k where "0 < k" and k: "ball a k \<inter> (path_image p) = {}"
-    using \<open>0 < e\<close> \<open>path p\<close> not_on_path_ball by blast
-  define kde where "kde \<equiv> (min k (min d e)) / 2"
-  have "0 < kde" "kde < k" "kde < d" "kde < e"
-    using \<open>0 < k\<close> \<open>0 < d\<close> \<open>0 < e\<close> by (auto simp: kde_def)
-  let ?q = "linepath (a + kde) (a + e) +++ p +++ linepath (a - d) (a - kde)"
-  have "- kde \<in> closed_segment (-d) e"
-    using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>kde < e\<close> closed_segment_eq_real_ivl by auto
-  then have a_diff_kde: "a - kde \<in> closed_segment (a - d) (a + e)"
-    using of_real_closed_segment [THEN iffD2]
-    by (force dest: closed_segment_translation_eq [of a, THEN iffD2] simp del: of_real_closed_segment)
-  then have clsub2: "closed_segment (a - d) (a - kde) \<subseteq> closed_segment (a - d) (a + e)"
-    by (simp add: subset_closed_segment)
-  then have "path_image p \<inter> closed_segment (a - d) (a - kde) \<subseteq> {a + e, a - d}"
-    using pap by force
-  moreover
-  have "a + e \<notin> path_image p \<inter> closed_segment (a - d) (a - kde)"
-    using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>0 < e\<close> by (auto simp: closed_segment_eq_real_ivl)
-  ultimately have sub_a_diff_d: "path_image p \<inter> closed_segment (a - d) (a - kde) \<subseteq> {a - d}"
-    by blast
-  have "kde \<in> closed_segment (-d) e"
-    using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>kde < e\<close> closed_segment_eq_real_ivl by auto
-  then have a_diff_kde: "a + kde \<in> closed_segment (a - d) (a + e)"
-    using of_real_closed_segment [THEN iffD2]
-    by (force dest: closed_segment_translation_eq [of "a", THEN iffD2] simp del: of_real_closed_segment)
-  then have clsub1: "closed_segment (a + kde) (a + e) \<subseteq> closed_segment (a - d) (a + e)"
-    by (simp add: subset_closed_segment)
-  then have "closed_segment (a + kde) (a + e) \<inter> path_image p \<subseteq> {a + e, a - d}"
-    using pap by force
-  moreover
-  have "closed_segment (a + kde) (a + e) \<inter> closed_segment (a - d) (a - kde) = {}"
-  proof (clarsimp intro!: equals0I)
-    fix y
-    assume y1: "y \<in> closed_segment (a + kde) (a + e)"
-       and y2: "y \<in> closed_segment (a - d) (a - kde)"
-    obtain u where u: "y = a + of_real u" and "0 < u"
-      using y1 \<open>0 < kde\<close> \<open>kde < e\<close> \<open>0 < e\<close> apply (clarsimp simp: in_segment)
-      apply (rule_tac u = "(1 - u)*kde + u*e" in that)
-       apply (auto simp: scaleR_conv_of_real algebra_simps)
-      by (meson le_less_trans less_add_same_cancel2 less_eq_real_def mult_left_mono)
-    moreover
-    obtain v where v: "y = a + of_real v" and "v \<le> 0"
-      using y2 \<open>0 < kde\<close> \<open>0 < d\<close> \<open>0 < e\<close> apply (clarsimp simp: in_segment)
-      apply (rule_tac v = "- ((1 - u)*d + u*kde)" in that)
-       apply (force simp: scaleR_conv_of_real algebra_simps)
-      by (meson less_eq_real_def neg_le_0_iff_le segment_bound_lemma)
-    ultimately show False
-      by auto
-  qed
-  moreover have "a - d \<notin> closed_segment (a + kde) (a + e)"
-    using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>0 < e\<close> by (auto simp: closed_segment_eq_real_ivl)
-  ultimately have sub_a_plus_e:
-    "closed_segment (a + kde) (a + e) \<inter> (path_image p \<union> closed_segment (a - d) (a - kde))
-       \<subseteq> {a + e}"
-    by auto
-  have "kde \<in> closed_segment (-kde) e"
-    using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>kde < e\<close> closed_segment_eq_real_ivl by auto
-  then have a_add_kde: "a + kde \<in> closed_segment (a - kde) (a + e)"
-    using of_real_closed_segment [THEN iffD2]
-    by (force dest: closed_segment_translation_eq [of "a", THEN iffD2] simp del: of_real_closed_segment)
-  have "closed_segment (a - kde) (a + kde) \<inter> closed_segment (a + kde) (a + e) = {a + kde}"
-    by (metis a_add_kde Int_closed_segment)
-  moreover
-  have "path_image p \<inter> closed_segment (a - kde) (a + kde) = {}"
-  proof (rule equals0I, clarify)
-    fix y  assume "y \<in> path_image p" "y \<in> closed_segment (a - kde) (a + kde)"
-    with equals0D [OF k, of y] \<open>0 < kde\<close> \<open>kde < k\<close> show False
-      by (auto simp: dist_norm dest: dist_decreases_closed_segment [where c=a])
-  qed
-  moreover
-  have "- kde \<in> closed_segment (-d) kde"
-    using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>kde < e\<close> closed_segment_eq_real_ivl by auto
-  then have a_diff_kde': "a - kde \<in> closed_segment (a - d) (a + kde)"
-    using of_real_closed_segment [THEN iffD2]
-    by (force dest: closed_segment_translation_eq [of a, THEN iffD2] simp del: of_real_closed_segment)
-  then have "closed_segment (a - d) (a - kde) \<inter> closed_segment (a - kde) (a + kde) = {a - kde}"
-    by (metis Int_closed_segment)
-  ultimately
-  have pa_subset_pm_kde: "path_image ?q \<inter> closed_segment (a - kde) (a + kde) \<subseteq> {a - kde, a + kde}"
-    by (auto simp: path_image_join assms)
-  have ge_kde1: "\<exists>y. x = a + y \<and> y \<ge> kde" if "x \<in> closed_segment (a + kde) (a + e)" for x
-    using that \<open>kde < e\<close> mult_le_cancel_left
-    apply (auto simp: in_segment)
-    apply (rule_tac x="(1-u)*kde + u*e" in exI)
-    apply (fastforce simp: algebra_simps scaleR_conv_of_real)
-    done
-  have ge_kde2: "\<exists>y. x = a + y \<and> y \<le> -kde" if "x \<in> closed_segment (a - d) (a - kde)" for x
-    using that \<open>kde < d\<close> affine_ineq
-    apply (auto simp: in_segment)
-    apply (rule_tac x="- ((1-u)*d + u*kde)" in exI)
-    apply (fastforce simp: algebra_simps scaleR_conv_of_real)
-    done
-  have notin_paq: "x \<notin> path_image ?q" if "dist a x < kde" for x
-    using that using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>kde < k\<close>
-    apply (auto simp: path_image_join assms dist_norm dest!: ge_kde1 ge_kde2)
-    by (meson k disjoint_iff_not_equal le_less_trans less_eq_real_def mem_ball that)
-  obtain z where zin: "z \<in> inside (path_image (?q +++ linepath (a - kde) (a + kde)))"
-           and z1: "cmod (winding_number (?q +++ linepath (a - kde) (a + kde)) z) = 1"
-  proof (rule simple_closed_path_wn1 [of kde ?q a])
-    show "simple_path (?q +++ linepath (a - kde) (a + kde))"
-    proof (intro simple_path_join_loop conjI)
-      show "arc ?q"
-      proof (rule arc_join)
-        show "arc (linepath (a + kde) (a + e))"
-          using \<open>kde < e\<close> \<open>arc p\<close> by (force simp: pfp)
-        show "arc (p +++ linepath (a - d) (a - kde))"
-          using \<open>kde < d\<close> \<open>kde < e\<close> \<open>arc p\<close> sub_a_diff_d by (force simp: pfp intro: arc_join)
-      qed (auto simp: psp pfp path_image_join sub_a_plus_e)
-      show "arc (linepath (a - kde) (a + kde))"
-        using \<open>0 < kde\<close> by auto
-    qed (use pa_subset_pm_kde in auto)
-  qed (use \<open>0 < kde\<close> notin_paq in auto)
-  have eq: "path_image (?q +++ linepath (a - kde) (a + kde)) = path_image (p +++ linepath (a - d) (a + e))"
-            (is "?lhs = ?rhs")
-  proof
-    show "?lhs \<subseteq> ?rhs"
-      using clsub1 clsub2 apply (auto simp: path_image_join assms)
-      by (meson subsetCE subset_closed_segment)
-    show "?rhs \<subseteq> ?lhs"
-      apply (simp add: path_image_join assms Un_ac)
-        by (metis Un_closed_segment Un_assoc a_diff_kde a_diff_kde' le_supI2 subset_refl)
-    qed
-  show thesis
-  proof
-    show zzin: "z \<in> inside (path_image (p +++ linepath (a - d) (a + e)))"
-      by (metis eq zin)
-    then have znotin: "z \<notin> path_image p"
-      by (metis ComplD Un_iff inside_Un_outside path_image_join pathfinish_linepath pathstart_reversepath pfp reversepath_linepath)
-    have znotin_de: "z \<notin> closed_segment (a - d) (a + kde)"
-      by (metis ComplD Un_iff Un_closed_segment a_diff_kde inside_Un_outside path_image_join path_image_linepath pathstart_linepath pfp zzin)
-    have "winding_number (linepath (a - d) (a + e)) z =
-          winding_number (linepath (a - d) (a + kde)) z + winding_number (linepath (a + kde) (a + e)) z"
-      apply (rule winding_number_split_linepath)
-      apply (simp add: a_diff_kde)
-      by (metis ComplD Un_iff inside_Un_outside path_image_join path_image_linepath pathstart_linepath pfp zzin)
-    also have "... = winding_number (linepath (a + kde) (a + e)) z +
-                     (winding_number (linepath (a - d) (a - kde)) z +
-                      winding_number (linepath (a - kde) (a + kde)) z)"
-      by (simp add: winding_number_split_linepath [of "a-kde", symmetric] znotin_de a_diff_kde')
-    finally have "winding_number (p +++ linepath (a - d) (a + e)) z =
-                    winding_number p z + winding_number (linepath (a + kde) (a + e)) z +
-                   (winding_number (linepath (a - d) (a - kde)) z +
-                    winding_number (linepath (a - kde) (a + kde)) z)"
-      by (metis (no_types, lifting) ComplD Un_iff \<open>arc p\<close> add.assoc arc_imp_path eq path_image_join path_join_path_ends path_linepath simple_path_imp_path sp_pl union_with_outside winding_number_join zin)
-    also have "... = winding_number ?q z + winding_number (linepath (a - kde) (a + kde)) z"
-      using \<open>path p\<close> znotin assms zzin clsub1
-      apply (subst winding_number_join, auto)
-      apply (metis (no_types, hide_lams) ComplD Un_iff contra_subsetD inside_Un_outside path_image_join path_image_linepath pathstart_linepath)
-      apply (metis Un_iff Un_closed_segment a_diff_kde' not_in_path_image_join path_image_linepath znotin_de)
-      by (metis Un_iff Un_closed_segment a_diff_kde' path_image_linepath path_linepath pathstart_linepath winding_number_join znotin_de)
-    also have "... = winding_number (?q +++ linepath (a - kde) (a + kde)) z"
-      using \<open>path p\<close> assms zin
-      apply (subst winding_number_join [symmetric], auto)
-      apply (metis ComplD Un_iff path_image_join pathfinish_join pathfinish_linepath pathstart_linepath union_with_outside)
-      by (metis Un_iff Un_closed_segment a_diff_kde' znotin_de)
-    finally have "winding_number (p +++ linepath (a - d) (a + e)) z =
-                  winding_number (?q +++ linepath (a - kde) (a + kde)) z" .
-    then show "cmod (winding_number (p +++ linepath (a - d) (a + e)) z) = 1"
-      by (simp add: z1)
-  qed
-qed
-
-lemma simple_closed_path_wn3:
-  fixes p :: "real \<Rightarrow> complex"
-  assumes "simple_path p" and loop: "pathfinish p = pathstart p"
-  obtains z where "z \<in> inside (path_image p)" "cmod (winding_number p z) = 1"
-proof -
-  have ins: "inside(path_image p) \<noteq> {}" "open(inside(path_image p))"
-            "connected(inside(path_image p))"
-   and out: "outside(path_image p) \<noteq> {}" "open(outside(path_image p))"
-            "connected(outside(path_image p))"
-   and bo:  "bounded(inside(path_image p))" "\<not> bounded(outside(path_image p))"
-   and ins_out: "inside(path_image p) \<inter> outside(path_image p) = {}"
-                "inside(path_image p) \<union> outside(path_image p) = - path_image p"
-   and fro: "frontier(inside(path_image p)) = path_image p"
-            "frontier(outside(path_image p)) = path_image p"
-    using Jordan_inside_outside [OF assms] by auto
-  obtain a where a: "a \<in> inside(path_image p)"
-    using \<open>inside (path_image p) \<noteq> {}\<close> by blast
-  obtain d::real where "0 < d" and d_fro: "a - d \<in> frontier (inside (path_image p))"
-                 and d_int: "\<And>\<epsilon>. \<lbrakk>0 \<le> \<epsilon>; \<epsilon> < d\<rbrakk> \<Longrightarrow> (a - \<epsilon>) \<in> inside (path_image p)"
-    apply (rule ray_to_frontier [of "inside (path_image p)" a "-1"])
-    using \<open>bounded (inside (path_image p))\<close> \<open>open (inside (path_image p))\<close> a interior_eq
-       apply (auto simp: of_real_def)
-    done
-  obtain e::real where "0 < e" and e_fro: "a + e \<in> frontier (inside (path_image p))"
-    and e_int: "\<And>\<epsilon>. \<lbrakk>0 \<le> \<epsilon>; \<epsilon> < e\<rbrakk> \<Longrightarrow> (a + \<epsilon>) \<in> inside (path_image p)"
-    apply (rule ray_to_frontier [of "inside (path_image p)" a 1])
-    using \<open>bounded (inside (path_image p))\<close> \<open>open (inside (path_image p))\<close> a interior_eq
-       apply (auto simp: of_real_def)
-    done
-  obtain t0 where "0 \<le> t0" "t0 \<le> 1" and pt: "p t0 = a - d"
-    using a d_fro fro by (auto simp: path_image_def)
-  obtain q where "simple_path q" and q_ends: "pathstart q = a - d" "pathfinish q = a - d"
-    and q_eq_p: "path_image q = path_image p"
-    and wn_q_eq_wn_p: "\<And>z. z \<in> inside(path_image p) \<Longrightarrow> winding_number q z = winding_number p z"
-  proof
-    show "simple_path (shiftpath t0 p)"
-      by (simp add: pathstart_shiftpath pathfinish_shiftpath
-          simple_path_shiftpath path_image_shiftpath \<open>0 \<le> t0\<close> \<open>t0 \<le> 1\<close> assms)
-    show "pathstart (shiftpath t0 p) = a - d"
-      using pt by (simp add: \<open>t0 \<le> 1\<close> pathstart_shiftpath)
-    show "pathfinish (shiftpath t0 p) = a - d"
-      by (simp add: \<open>0 \<le> t0\<close> loop pathfinish_shiftpath pt)
-    show "path_image (shiftpath t0 p) = path_image p"
-      by (simp add: \<open>0 \<le> t0\<close> \<open>t0 \<le> 1\<close> loop path_image_shiftpath)
-    show "winding_number (shiftpath t0 p) z = winding_number p z"
-      if "z \<in> inside (path_image p)" for z
-      by (metis ComplD Un_iff \<open>0 \<le> t0\<close> \<open>t0 \<le> 1\<close> \<open>simple_path p\<close> atLeastAtMost_iff inside_Un_outside
-          loop simple_path_imp_path that winding_number_shiftpath)
-  qed
-  have ad_not_ae: "a - d \<noteq> a + e"
-    by (metis \<open>0 < d\<close> \<open>0 < e\<close> add.left_inverse add_left_cancel add_uminus_conv_diff
-        le_add_same_cancel2 less_eq_real_def not_less of_real_add of_real_def of_real_eq_0_iff pt)
-  have ad_ae_q: "{a - d, a + e} \<subseteq> path_image q"
-    using \<open>path_image q = path_image p\<close> d_fro e_fro fro(1) by auto
-  have ada: "open_segment (a - d) a \<subseteq> inside (path_image p)"
-  proof (clarsimp simp: in_segment)
-    fix u::real assume "0 < u" "u < 1"
-    with d_int have "a - (1 - u) * d \<in> inside (path_image p)"
-      by (metis \<open>0 < d\<close> add.commute diff_add_cancel left_diff_distrib' less_add_same_cancel2 less_eq_real_def mult.left_neutral zero_less_mult_iff)
-    then show "(1 - u) *\<^sub>R (a - d) + u *\<^sub>R a \<in> inside (path_image p)"
-      by (simp add: diff_add_eq of_real_def real_vector.scale_right_diff_distrib)
-  qed
-  have aae: "open_segment a (a + e) \<subseteq> inside (path_image p)"
-  proof (clarsimp simp: in_segment)
-    fix u::real assume "0 < u" "u < 1"
-    with e_int have "a + u * e \<in> inside (path_image p)"
-      by (meson \<open>0 < e\<close> less_eq_real_def mult_less_cancel_right2 not_less zero_less_mult_iff)
-    then show "(1 - u) *\<^sub>R a + u *\<^sub>R (a + e) \<in> inside (path_image p)"
-      apply (simp add: algebra_simps)
-      by (simp add: diff_add_eq of_real_def real_vector.scale_right_diff_distrib)
-  qed
-  have "complex_of_real (d * d + (e * e + d * (e + e))) \<noteq> 0"
-    using ad_not_ae
-    by (metis \<open>0 < d\<close> \<open>0 < e\<close> add_strict_left_mono less_add_same_cancel1 not_sum_squares_lt_zero
-        of_real_eq_0_iff zero_less_double_add_iff_zero_less_single_add zero_less_mult_iff)
-  then have a_in_de: "a \<in> open_segment (a - d) (a + e)"
-    using ad_not_ae \<open>0 < d\<close> \<open>0 < e\<close>
-    apply (auto simp: in_segment algebra_simps scaleR_conv_of_real)
-    apply (rule_tac x="d / (d+e)" in exI)
-    apply (auto simp: field_simps)
-    done
-  then have "open_segment (a - d) (a + e) \<subseteq> inside (path_image p)"
-    using ada a aae Un_open_segment [of a "a-d" "a+e"] by blast
-  then have "path_image q \<inter> open_segment (a - d) (a + e) = {}"
-    using inside_no_overlap by (fastforce simp: q_eq_p)
-  with ad_ae_q have paq_Int_cs: "path_image q \<inter> closed_segment (a - d) (a + e) = {a - d, a + e}"
-    by (simp add: closed_segment_eq_open)
-  obtain t where "0 \<le> t" "t \<le> 1" and qt: "q t = a + e"
-    using a e_fro fro ad_ae_q by (auto simp: path_defs)
-  then have "t \<noteq> 0"
-    by (metis ad_not_ae pathstart_def q_ends(1))
-  then have "t \<noteq> 1"
-    by (metis ad_not_ae pathfinish_def q_ends(2) qt)
-  have q01: "q 0 = a - d" "q 1 = a - d"
-    using q_ends by (auto simp: pathstart_def pathfinish_def)
-  obtain z where zin: "z \<in> inside (path_image (subpath t 0 q +++ linepath (a - d) (a + e)))"
-             and z1: "cmod (winding_number (subpath t 0 q +++ linepath (a - d) (a + e)) z) = 1"
-  proof (rule simple_closed_path_wn2 [of d e "subpath t 0 q" a], simp_all add: q01)
-    show "simple_path (subpath t 0 q +++ linepath (a - d) (a + e))"
-    proof (rule simple_path_join_loop, simp_all add: qt q01)
-      have "inj_on q (closed_segment t 0)"
-        using \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close> \<open>t \<noteq> 0\<close> \<open>t \<noteq> 1\<close>
-        by (fastforce simp: simple_path_def inj_on_def closed_segment_eq_real_ivl)
-      then show "arc (subpath t 0 q)"
-        using \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close> \<open>t \<noteq> 0\<close>
-        by (simp add: arc_subpath_eq simple_path_imp_path)
-      show "arc (linepath (a - d) (a + e))"
-        by (simp add: ad_not_ae)
-      show "path_image (subpath t 0 q) \<inter> closed_segment (a - d) (a + e) \<subseteq> {a + e, a - d}"
-        using qt paq_Int_cs  \<open>simple_path q\<close> \<open>0 \<le> t\<close> \<open>t \<le> 1\<close>
-        by (force simp: dest: rev_subsetD [OF _ path_image_subpath_subset] intro: simple_path_imp_path)
-    qed
-  qed (auto simp: \<open>0 < d\<close> \<open>0 < e\<close> qt)
-  have pa01_Un: "path_image (subpath 0 t q) \<union> path_image (subpath 1 t q) = path_image q"
-    unfolding path_image_subpath
-    using \<open>0 \<le> t\<close> \<open>t \<le> 1\<close> by (force simp: path_image_def image_iff)
-  with paq_Int_cs have pa_01q:
-        "(path_image (subpath 0 t q) \<union> path_image (subpath 1 t q)) \<inter> closed_segment (a - d) (a + e) = {a - d, a + e}"
-    by metis
-  have z_notin_ed: "z \<notin> closed_segment (a + e) (a - d)"
-    using zin q01 by (simp add: path_image_join closed_segment_commute inside_def)
-  have z_notin_0t: "z \<notin> path_image (subpath 0 t q)"
-    by (metis (no_types, hide_lams) IntI Un_upper1 subsetD empty_iff inside_no_overlap path_image_join
-        path_image_subpath_commute pathfinish_subpath pathstart_def pathstart_linepath q_ends(1) qt subpath_trivial zin)
-  have [simp]: "- winding_number (subpath t 0 q) z = winding_number (subpath 0 t q) z"
-    by (metis \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close> atLeastAtMost_iff zero_le_one
-              path_image_subpath_commute path_subpath real_eq_0_iff_le_ge_0
-              reversepath_subpath simple_path_imp_path winding_number_reversepath z_notin_0t)
-  obtain z_in_q: "z \<in> inside(path_image q)"
-     and wn_q: "winding_number (subpath 0 t q +++ subpath t 1 q) z = - winding_number (subpath t 0 q +++ linepath (a - d) (a + e)) z"
-  proof (rule winding_number_from_innerpath
-          [of "subpath 0 t q" "a-d" "a+e" "subpath 1 t q" "linepath (a - d) (a + e)"
-            z "- winding_number (subpath t 0 q +++ linepath (a - d) (a + e)) z"],
-         simp_all add: q01 qt pa01_Un reversepath_subpath)
-    show "simple_path (subpath 0 t q)" "simple_path (subpath 1 t q)"
-      by (simp_all add: \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close> \<open>t \<noteq> 0\<close> \<open>t \<noteq> 1\<close> simple_path_subpath)
-    show "simple_path (linepath (a - d) (a + e))"
-      using ad_not_ae by blast
-    show "path_image (subpath 0 t q) \<inter> path_image (subpath 1 t q) = {a - d, a + e}"  (is "?lhs = ?rhs")
-    proof
-      show "?lhs \<subseteq> ?rhs"
-        using \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close> \<open>t \<noteq> 1\<close> q_ends qt q01
-        by (force simp: pathfinish_def qt simple_path_def path_image_subpath)
-      show "?rhs \<subseteq> ?lhs"
-        using \<open>0 \<le> t\<close> \<open>t \<le> 1\<close> q01 qt by (force simp: path_image_subpath)
-    qed
-    show "path_image (subpath 0 t q) \<inter> closed_segment (a - d) (a + e) = {a - d, a + e}" (is "?lhs = ?rhs")
-    proof
-      show "?lhs \<subseteq> ?rhs"  using paq_Int_cs pa01_Un by fastforce
-      show "?rhs \<subseteq> ?lhs"  using \<open>0 \<le> t\<close> \<open>t \<le> 1\<close> q01 qt by (force simp: path_image_subpath)
-    qed
-    show "path_image (subpath 1 t q) \<inter> closed_segment (a - d) (a + e) = {a - d, a + e}" (is "?lhs = ?rhs")
-    proof
-      show "?lhs \<subseteq> ?rhs"  by (auto simp: pa_01q [symmetric])
-      show "?rhs \<subseteq> ?lhs"  using \<open>0 \<le> t\<close> \<open>t \<le> 1\<close> q01 qt by (force simp: path_image_subpath)
-    qed
-    show "closed_segment (a - d) (a + e) \<inter> inside (path_image q) \<noteq> {}"
-      using a a_in_de open_closed_segment pa01_Un q_eq_p by fastforce
-    show "z \<in> inside (path_image (subpath 0 t q) \<union> closed_segment (a - d) (a + e))"
-      by (metis path_image_join path_image_linepath path_image_subpath_commute pathfinish_subpath pathstart_linepath q01(1) zin)
-    show "winding_number (subpath 0 t q +++ linepath (a + e) (a - d)) z =
-      - winding_number (subpath t 0 q +++ linepath (a - d) (a + e)) z"
-      using z_notin_ed z_notin_0t \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close>
-      by (simp add: simple_path_imp_path qt q01 path_image_subpath_commute closed_segment_commute winding_number_join winding_number_reversepath [symmetric])
-    show "- d \<noteq> e"
-      using ad_not_ae by auto
-    show "winding_number (subpath t 0 q +++ linepath (a - d) (a + e)) z \<noteq> 0"
-      using z1 by auto
-  qed
-  show ?thesis
-  proof
-    show "z \<in> inside (path_image p)"
-      using q_eq_p z_in_q by auto
-    then have [simp]: "z \<notin> path_image q"
-      by (metis disjoint_iff_not_equal inside_no_overlap q_eq_p)
-    have [simp]: "z \<notin> path_image (subpath 1 t q)"
-      using inside_def pa01_Un z_in_q by fastforce
-    have "winding_number(subpath 0 t q +++ subpath t 1 q) z = winding_number(subpath 0 1 q) z"
-      using z_notin_0t \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close>
-      by (simp add: simple_path_imp_path qt path_image_subpath_commute winding_number_join winding_number_subpath_combine)
-    with wn_q have "winding_number (subpath t 0 q +++ linepath (a - d) (a + e)) z = - winding_number q z"
-      by auto
-    with z1 have "cmod (winding_number q z) = 1"
-      by simp
-    with z1 wn_q_eq_wn_p show "cmod (winding_number p z) = 1"
-      using z1 wn_q_eq_wn_p  by (simp add: \<open>z \<in> inside (path_image p)\<close>)
-    qed
-qed
-
-proposition simple_closed_path_winding_number_inside:
-  assumes "simple_path \<gamma>"
-  obtains "\<And>z. z \<in> inside(path_image \<gamma>) \<Longrightarrow> winding_number \<gamma> z = 1"
-        | "\<And>z. z \<in> inside(path_image \<gamma>) \<Longrightarrow> winding_number \<gamma> z = -1"
-proof (cases "pathfinish \<gamma> = pathstart \<gamma>")
-  case True
-  have "path \<gamma>"
-    by (simp add: assms simple_path_imp_path)
-  then have const: "winding_number \<gamma> constant_on inside(path_image \<gamma>)"
-  proof (rule winding_number_constant)
-    show "connected (inside(path_image \<gamma>))"
-      by (simp add: Jordan_inside_outside True assms)
-  qed (use inside_no_overlap True in auto)
-  obtain z where zin: "z \<in> inside (path_image \<gamma>)" and z1: "cmod (winding_number \<gamma> z) = 1"
-    using simple_closed_path_wn3 [of \<gamma>] True assms by blast
-  have "winding_number \<gamma> z \<in> \<int>"
-    using zin integer_winding_number [OF \<open>path \<gamma>\<close> True] inside_def by blast
-  with z1 consider "winding_number \<gamma> z = 1" | "winding_number \<gamma> z = -1"
-    apply (auto simp: Ints_def abs_if split: if_split_asm)
-    by (metis of_int_1 of_int_eq_iff of_int_minus)
-  with that const zin show ?thesis
-    unfolding constant_on_def by metis
-next
-  case False
-  then show ?thesis
-    using inside_simple_curve_imp_closed assms that(2) by blast
-qed
-
-lemma simple_closed_path_abs_winding_number_inside:
-  assumes "simple_path \<gamma>" "z \<in> inside(path_image \<gamma>)"
-    shows "\<bar>Re (winding_number \<gamma> z)\<bar> = 1"
-  by (metis assms norm_minus_cancel norm_one one_complex.simps(1) real_norm_def simple_closed_path_winding_number_inside uminus_complex.simps(1))
-
-lemma simple_closed_path_norm_winding_number_inside:
-  assumes "simple_path \<gamma>" "z \<in> inside(path_image \<gamma>)"
-  shows "norm (winding_number \<gamma> z) = 1"
-proof -
-  have "pathfinish \<gamma> = pathstart \<gamma>"
-    using assms inside_simple_curve_imp_closed by blast
-  with assms integer_winding_number have "winding_number \<gamma> z \<in> \<int>"
-    by (simp add: inside_def simple_path_def)
-  then show ?thesis
-    by (metis assms norm_minus_cancel norm_one simple_closed_path_winding_number_inside)
-qed
-
-lemma simple_closed_path_winding_number_cases:
-   "\<lbrakk>simple_path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> path_image \<gamma>\<rbrakk> \<Longrightarrow> winding_number \<gamma> z \<in> {-1,0,1}"
-apply (simp add: inside_Un_outside [of "path_image \<gamma>", symmetric, unfolded set_eq_iff Set.Compl_iff] del: inside_Un_outside)
-   apply (rule simple_closed_path_winding_number_inside)
-  using simple_path_def winding_number_zero_in_outside by blast+
-
-lemma simple_closed_path_winding_number_pos:
-   "\<lbrakk>simple_path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> path_image \<gamma>; 0 < Re(winding_number \<gamma> z)\<rbrakk>
-    \<Longrightarrow> winding_number \<gamma> z = 1"
-using simple_closed_path_winding_number_cases
-  by fastforce
-
-subsection \<open>Winding number for rectangular paths\<close>
-
-definition\<^marker>\<open>tag important\<close> rectpath where
-  "rectpath a1 a3 = (let a2 = Complex (Re a3) (Im a1); a4 = Complex (Re a1) (Im a3)
-                      in linepath a1 a2 +++ linepath a2 a3 +++ linepath a3 a4 +++ linepath a4 a1)"
-
-lemma path_rectpath [simp, intro]: "path (rectpath a b)"
-  by (simp add: Let_def rectpath_def)
-
-lemma valid_path_rectpath [simp, intro]: "valid_path (rectpath a b)"
-  by (simp add: Let_def rectpath_def)
-
-lemma pathstart_rectpath [simp]: "pathstart (rectpath a1 a3) = a1"
-  by (simp add: rectpath_def Let_def)
-
-lemma pathfinish_rectpath [simp]: "pathfinish (rectpath a1 a3) = a1"
-  by (simp add: rectpath_def Let_def)
-
-lemma simple_path_rectpath [simp, intro]:
-  assumes "Re a1 \<noteq> Re a3" "Im a1 \<noteq> Im a3"
-  shows   "simple_path (rectpath a1 a3)"
-  unfolding rectpath_def Let_def using assms
-  by (intro simple_path_join_loop arc_join arc_linepath)
-     (auto simp: complex_eq_iff path_image_join closed_segment_same_Re closed_segment_same_Im)
-
-lemma path_image_rectpath:
-  assumes "Re a1 \<le> Re a3" "Im a1 \<le> Im a3"
-  shows "path_image (rectpath a1 a3) =
-           {z. Re z \<in> {Re a1, Re a3} \<and> Im z \<in> {Im a1..Im a3}} \<union>
-           {z. Im z \<in> {Im a1, Im a3} \<and> Re z \<in> {Re a1..Re a3}}" (is "?lhs = ?rhs")
-proof -
-  define a2 a4 where "a2 = Complex (Re a3) (Im a1)" and "a4 = Complex (Re a1) (Im a3)"
-  have "?lhs = closed_segment a1 a2 \<union> closed_segment a2 a3 \<union>
-                  closed_segment a4 a3 \<union> closed_segment a1 a4"
-    by (simp_all add: rectpath_def Let_def path_image_join closed_segment_commute
-                      a2_def a4_def Un_assoc)
-  also have "\<dots> = ?rhs" using assms
-    by (auto simp: rectpath_def Let_def path_image_join a2_def a4_def
-          closed_segment_same_Re closed_segment_same_Im closed_segment_eq_real_ivl)
-  finally show ?thesis .
-qed
-
-lemma path_image_rectpath_subset_cbox:
-  assumes "Re a \<le> Re b" "Im a \<le> Im b"
-  shows   "path_image (rectpath a b) \<subseteq> cbox a b"
-  using assms by (auto simp: path_image_rectpath in_cbox_complex_iff)
-
-lemma path_image_rectpath_inter_box:
-  assumes "Re a \<le> Re b" "Im a \<le> Im b"
-  shows   "path_image (rectpath a b) \<inter> box a b = {}"
-  using assms by (auto simp: path_image_rectpath in_box_complex_iff)
-
-lemma path_image_rectpath_cbox_minus_box:
-  assumes "Re a \<le> Re b" "Im a \<le> Im b"
-  shows   "path_image (rectpath a b) = cbox a b - box a b"
-  using assms by (auto simp: path_image_rectpath in_cbox_complex_iff
-                             in_box_complex_iff)
-
-proposition winding_number_rectpath:
-  assumes "z \<in> box a1 a3"
-  shows   "winding_number (rectpath a1 a3) z = 1"
-proof -
-  from assms have less: "Re a1 < Re a3" "Im a1 < Im a3"
-    by (auto simp: in_box_complex_iff)
-  define a2 a4 where "a2 = Complex (Re a3) (Im a1)" and "a4 = Complex (Re a1) (Im a3)"
-  let ?l1 = "linepath a1 a2" and ?l2 = "linepath a2 a3"
-  and ?l3 = "linepath a3 a4" and ?l4 = "linepath a4 a1"
-  from assms and less have "z \<notin> path_image (rectpath a1 a3)"
-    by (auto simp: path_image_rectpath_cbox_minus_box)
-  also have "path_image (rectpath a1 a3) =
-               path_image ?l1 \<union> path_image ?l2 \<union> path_image ?l3 \<union> path_image ?l4"
-    by (simp add: rectpath_def Let_def path_image_join Un_assoc a2_def a4_def)
-  finally have "z \<notin> \<dots>" .
-  moreover have "\<forall>l\<in>{?l1,?l2,?l3,?l4}. Re (winding_number l z) > 0"
-    unfolding ball_simps HOL.simp_thms a2_def a4_def
-    by (intro conjI; (rule winding_number_linepath_pos_lt;
-          (insert assms, auto simp: a2_def a4_def in_box_complex_iff mult_neg_neg))+)
-  ultimately have "Re (winding_number (rectpath a1 a3) z) > 0"
-    by (simp add: winding_number_join path_image_join rectpath_def Let_def a2_def a4_def)
-  thus "winding_number (rectpath a1 a3) z = 1" using assms less
-    by (intro simple_closed_path_winding_number_pos simple_path_rectpath)
-       (auto simp: path_image_rectpath_cbox_minus_box)
-qed
-
-proposition winding_number_rectpath_outside:
-  assumes "Re a1 \<le> Re a3" "Im a1 \<le> Im a3"
-  assumes "z \<notin> cbox a1 a3"
-  shows   "winding_number (rectpath a1 a3) z = 0"
-  using assms by (intro winding_number_zero_outside[OF _ _ _ assms(3)]
-                     path_image_rectpath_subset_cbox) simp_all
-
-text\<open>A per-function version for continuous logs, a kind of monodromy\<close>
-proposition\<^marker>\<open>tag unimportant\<close> winding_number_compose_exp:
-  assumes "path p"
-  shows "winding_number (exp \<circ> p) 0 = (pathfinish p - pathstart p) / (2 * of_real pi * \<i>)"
-proof -
-  obtain e where "0 < e" and e: "\<And>t. t \<in> {0..1} \<Longrightarrow> e \<le> norm(exp(p t))"
-  proof
-    have "closed (path_image (exp \<circ> p))"
-      by (simp add: assms closed_path_image holomorphic_on_exp holomorphic_on_imp_continuous_on path_continuous_image)
-    then show "0 < setdist {0} (path_image (exp \<circ> p))"
-      by (metis exp_not_eq_zero imageE image_comp infdist_eq_setdist infdist_pos_not_in_closed path_defs(4) path_image_nonempty)
-  next
-    fix t::real
-    assume "t \<in> {0..1}"
-    have "setdist {0} (path_image (exp \<circ> p)) \<le> dist 0 (exp (p t))"
-      apply (rule setdist_le_dist)
-      using \<open>t \<in> {0..1}\<close> path_image_def by fastforce+
-    then show "setdist {0} (path_image (exp \<circ> p)) \<le> cmod (exp (p t))"
-      by simp
-  qed
-  have "bounded (path_image p)"
-    by (simp add: assms bounded_path_image)
-  then obtain B where "0 < B" and B: "path_image p \<subseteq> cball 0 B"
-    by (meson bounded_pos mem_cball_0 subsetI)
-  let ?B = "cball (0::complex) (B+1)"
-  have "uniformly_continuous_on ?B exp"
-    using holomorphic_on_exp holomorphic_on_imp_continuous_on
-    by (force intro: compact_uniformly_continuous)
-  then obtain d where "d > 0"
-        and d: "\<And>x x'. \<lbrakk>x\<in>?B; x'\<in>?B; dist x' x < d\<rbrakk> \<Longrightarrow> norm (exp x' - exp x) < e"
-    using \<open>e > 0\<close> by (auto simp: uniformly_continuous_on_def dist_norm)
-  then have "min 1 d > 0"
-    by force
-  then obtain g where pfg: "polynomial_function g"  and "g 0 = p 0" "g 1 = p 1"
-           and gless: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm(g t - p t) < min 1 d"
-    using path_approx_polynomial_function [OF \<open>path p\<close>] \<open>d > 0\<close> \<open>0 < e\<close>
-    unfolding pathfinish_def pathstart_def by meson
-  have "winding_number (exp \<circ> p) 0 = winding_number (exp \<circ> g) 0"
-  proof (rule winding_number_nearby_paths_eq [symmetric])
-    show "path (exp \<circ> p)" "path (exp \<circ> g)"
-      by (simp_all add: pfg assms holomorphic_on_exp holomorphic_on_imp_continuous_on path_continuous_image path_polynomial_function)
-  next
-    fix t :: "real"
-    assume t: "t \<in> {0..1}"
-    with gless have "norm(g t - p t) < 1"
-      using min_less_iff_conj by blast
-    moreover have ptB: "norm (p t) \<le> B"
-      using B t by (force simp: path_image_def)
-    ultimately have "cmod (g t) \<le> B + 1"
-      by (meson add_mono_thms_linordered_field(4) le_less_trans less_imp_le norm_triangle_sub)
-    with ptB gless t have "cmod ((exp \<circ> g) t - (exp \<circ> p) t) < e"
-      by (auto simp: dist_norm d)
-    with e t show "cmod ((exp \<circ> g) t - (exp \<circ> p) t) < cmod ((exp \<circ> p) t - 0)"
-      by fastforce
-  qed (use \<open>g 0 = p 0\<close> \<open>g 1 = p 1\<close> in \<open>auto simp: pathfinish_def pathstart_def\<close>)
-  also have "... = 1 / (of_real (2 * pi) * \<i>) * contour_integral (exp \<circ> g) (\<lambda>w. 1 / (w - 0))"
-  proof (rule winding_number_valid_path)
-    have "continuous_on (path_image g) (deriv exp)"
-      by (metis DERIV_exp DERIV_imp_deriv continuous_on_cong holomorphic_on_exp holomorphic_on_imp_continuous_on)
-    then show "valid_path (exp \<circ> g)"
-      by (simp add: field_differentiable_within_exp pfg valid_path_compose valid_path_polynomial_function)
-    show "0 \<notin> path_image (exp \<circ> g)"
-      by (auto simp: path_image_def)
-  qed
-  also have "... = 1 / (of_real (2 * pi) * \<i>) * integral {0..1} (\<lambda>x. vector_derivative g (at x))"
-  proof (simp add: contour_integral_integral, rule integral_cong)
-    fix t :: "real"
-    assume t: "t \<in> {0..1}"
-    show "vector_derivative (exp \<circ> g) (at t) / exp (g t) = vector_derivative g (at t)"
-    proof -
-      have "(exp \<circ> g has_vector_derivative vector_derivative (exp \<circ> g) (at t)) (at t)"
-        by (meson DERIV_exp differentiable_def field_vector_diff_chain_at has_vector_derivative_def
-            has_vector_derivative_polynomial_function pfg vector_derivative_works)
-      moreover have "(exp \<circ> g has_vector_derivative vector_derivative g (at t) * exp (g t)) (at t)"
-        apply (rule field_vector_diff_chain_at)
-        apply (metis has_vector_derivative_polynomial_function pfg vector_derivative_at)
-        using DERIV_exp has_field_derivative_def apply blast
-        done
-      ultimately show ?thesis
-        by (simp add: divide_simps, rule vector_derivative_unique_at)
-    qed
-  qed
-  also have "... = (pathfinish p - pathstart p) / (2 * of_real pi * \<i>)"
-  proof -
-    have "((\<lambda>x. vector_derivative g (at x)) has_integral g 1 - g 0) {0..1}"
-      apply (rule fundamental_theorem_of_calculus [OF zero_le_one])
-      by (metis has_vector_derivative_at_within has_vector_derivative_polynomial_function pfg vector_derivative_at)
-    then show ?thesis
-    apply (simp add: pathfinish_def pathstart_def)
-      using \<open>g 0 = p 0\<close> \<open>g 1 = p 1\<close> by auto
-  qed
-  finally show ?thesis .
-qed
-
-subsection\<^marker>\<open>tag unimportant\<close> \<open>The winding number defines a continuous logarithm for the path itself\<close>
-
-lemma winding_number_as_continuous_log:
-  assumes "path p" and \<zeta>: "\<zeta> \<notin> path_image p"
-  obtains q where "path q"
-                  "pathfinish q - pathstart q = 2 * of_real pi * \<i> * winding_number p \<zeta>"
-                  "\<And>t. t \<in> {0..1} \<Longrightarrow> p t = \<zeta> + exp(q t)"
-proof -
-  let ?q = "\<lambda>t. 2 * of_real pi * \<i> * winding_number(subpath 0 t p) \<zeta> + Ln(pathstart p - \<zeta>)"
-  show ?thesis
-  proof
-    have *: "continuous (at t within {0..1}) (\<lambda>x. winding_number (subpath 0 x p) \<zeta>)"
-      if t: "t \<in> {0..1}" for t
-    proof -
-      let ?B = "ball (p t) (norm(p t - \<zeta>))"
-      have "p t \<noteq> \<zeta>"
-        using path_image_def that \<zeta> by blast
-      then have "simply_connected ?B"
-        by (simp add: convex_imp_simply_connected)
-      then have "\<And>f::complex\<Rightarrow>complex. continuous_on ?B f \<and> (\<forall>\<zeta> \<in> ?B. f \<zeta> \<noteq> 0)
-                  \<longrightarrow> (\<exists>g. continuous_on ?B g \<and> (\<forall>\<zeta> \<in> ?B. f \<zeta> = exp (g \<zeta>)))"
-        by (simp add: simply_connected_eq_continuous_log)
-      moreover have "continuous_on ?B (\<lambda>w. w - \<zeta>)"
-        by (intro continuous_intros)
-      moreover have "(\<forall>z \<in> ?B. z - \<zeta> \<noteq> 0)"
-        by (auto simp: dist_norm)
-      ultimately obtain g where contg: "continuous_on ?B g"
-        and geq: "\<And>z. z \<in> ?B \<Longrightarrow> z - \<zeta> = exp (g z)" by blast
-      obtain d where "0 < d" and d:
-        "\<And>x. \<lbrakk>x \<in> {0..1}; dist x t < d\<rbrakk> \<Longrightarrow> dist (p x) (p t) < cmod (p t - \<zeta>)"
-        using \<open>path p\<close> t unfolding path_def continuous_on_iff
-        by (metis \<open>p t \<noteq> \<zeta>\<close> right_minus_eq zero_less_norm_iff)
-      have "((\<lambda>x. winding_number (\<lambda>w. subpath 0 x p w - \<zeta>) 0 -
-                  winding_number (\<lambda>w. subpath 0 t p w - \<zeta>) 0) \<longlongrightarrow> 0)
-            (at t within {0..1})"
-      proof (rule Lim_transform_within [OF _ \<open>d > 0\<close>])
-        have "continuous (at t within {0..1}) (g o p)"
-        proof (rule continuous_within_compose)
-          show "continuous (at t within {0..1}) p"
-            using \<open>path p\<close> continuous_on_eq_continuous_within path_def that by blast
-          show "continuous (at (p t) within p ` {0..1}) g"
-            by (metis (no_types, lifting) open_ball UNIV_I \<open>p t \<noteq> \<zeta>\<close> centre_in_ball contg continuous_on_eq_continuous_at continuous_within_topological right_minus_eq zero_less_norm_iff)
-        qed
-        with LIM_zero have "((\<lambda>u. (g (subpath t u p 1) - g (subpath t u p 0))) \<longlongrightarrow> 0) (at t within {0..1})"
-          by (auto simp: subpath_def continuous_within o_def)
-        then show "((\<lambda>u.  (g (subpath t u p 1) - g (subpath t u p 0)) / (2 * of_real pi * \<i>)) \<longlongrightarrow> 0)
-           (at t within {0..1})"
-          by (simp add: tendsto_divide_zero)
-        show "(g (subpath t u p 1) - g (subpath t u p 0)) / (2 * of_real pi * \<i>) =
-              winding_number (\<lambda>w. subpath 0 u p w - \<zeta>) 0 - winding_number (\<lambda>w. subpath 0 t p w - \<zeta>) 0"
-          if "u \<in> {0..1}" "0 < dist u t" "dist u t < d" for u
-        proof -
-          have "closed_segment t u \<subseteq> {0..1}"
-            using closed_segment_eq_real_ivl t that by auto
-          then have piB: "path_image(subpath t u p) \<subseteq> ?B"
-            apply (clarsimp simp add: path_image_subpath_gen)
-            by (metis subsetD le_less_trans \<open>dist u t < d\<close> d dist_commute dist_in_closed_segment)
-          have *: "path (g \<circ> subpath t u p)"
-            apply (rule path_continuous_image)
-            using \<open>path p\<close> t that apply auto[1]
-            using piB contg continuous_on_subset by blast
-          have "(g (subpath t u p 1) - g (subpath t u p 0)) / (2 * of_real pi * \<i>)
-              =  winding_number (exp \<circ> g \<circ> subpath t u p) 0"
-            using winding_number_compose_exp [OF *]
-            by (simp add: pathfinish_def pathstart_def o_assoc)
-          also have "... = winding_number (\<lambda>w. subpath t u p w - \<zeta>) 0"
-          proof (rule winding_number_cong)
-            have "exp(g y) = y - \<zeta>" if "y \<in> (subpath t u p) ` {0..1}" for y
-              by (metis that geq path_image_def piB subset_eq)
-            then show "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> (exp \<circ> g \<circ> subpath t u p) x = subpath t u p x - \<zeta>"
-              by auto
-          qed
-          also have "... = winding_number (\<lambda>w. subpath 0 u p w - \<zeta>) 0 -
-                           winding_number (\<lambda>w. subpath 0 t p w - \<zeta>) 0"
-            apply (simp add: winding_number_offset [symmetric])
-            using winding_number_subpath_combine [OF \<open>path p\<close> \<zeta>, of 0 t u] \<open>t \<in> {0..1}\<close> \<open>u \<in> {0..1}\<close>
-            by (simp add: add.commute eq_diff_eq)
-          finally show ?thesis .
-        qed
-      qed
-      then show ?thesis
-        by (subst winding_number_offset) (simp add: continuous_within LIM_zero_iff)
-    qed
-    show "path ?q"
-      unfolding path_def
-      by (intro continuous_intros) (simp add: continuous_on_eq_continuous_within *)
-
-    have "\<zeta> \<noteq> p 0"
-      by (metis \<zeta> pathstart_def pathstart_in_path_image)
-    then show "pathfinish ?q - pathstart ?q = 2 * of_real pi * \<i> * winding_number p \<zeta>"
-      by (simp add: pathfinish_def pathstart_def)
-    show "p t = \<zeta> + exp (?q t)" if "t \<in> {0..1}" for t
-    proof -
-      have "path (subpath 0 t p)"
-        using \<open>path p\<close> that by auto
-      moreover
-      have "\<zeta> \<notin> path_image (subpath 0 t p)"
-        using \<zeta> [unfolded path_image_def] that by (auto simp: path_image_subpath)
-      ultimately show ?thesis
-        using winding_number_exp_2pi [of "subpath 0 t p" \<zeta>] \<open>\<zeta> \<noteq> p 0\<close>
-        by (auto simp: exp_add algebra_simps pathfinish_def pathstart_def subpath_def)
-    qed
-  qed
-qed
-
-subsection \<open>Winding number equality is the same as path/loop homotopy in C - {0}\<close>
-
-lemma winding_number_homotopic_loops_null_eq:
-  assumes "path p" and \<zeta>: "\<zeta> \<notin> path_image p"
-  shows "winding_number p \<zeta> = 0 \<longleftrightarrow> (\<exists>a. homotopic_loops (-{\<zeta>}) p (\<lambda>t. a))"
-    (is "?lhs = ?rhs")
-proof
-  assume [simp]: ?lhs
-  obtain q where "path q"
-             and qeq:  "pathfinish q - pathstart q = 2 * of_real pi * \<i> * winding_number p \<zeta>"
-             and peq: "\<And>t. t \<in> {0..1} \<Longrightarrow> p t = \<zeta> + exp(q t)"
-    using winding_number_as_continuous_log [OF assms] by blast
-  have *: "homotopic_with_canon (\<lambda>r. pathfinish r = pathstart r)
-                       {0..1} (-{\<zeta>}) ((\<lambda>w. \<zeta> + exp w) \<circ> q) ((\<lambda>w. \<zeta> + exp w) \<circ> (\<lambda>t. 0))"
-  proof (rule homotopic_with_compose_continuous_left)
-    show "homotopic_with_canon (\<lambda>f. pathfinish ((\<lambda>w. \<zeta> + exp w) \<circ> f) = pathstart ((\<lambda>w. \<zeta> + exp w) \<circ> f))
-              {0..1} UNIV q (\<lambda>t. 0)"
-    proof (rule homotopic_with_mono, simp_all add: pathfinish_def pathstart_def)
-      have "homotopic_loops UNIV q (\<lambda>t. 0)"
-        by (rule homotopic_loops_linear) (use qeq \<open>path q\<close> in \<open>auto simp: path_defs\<close>)
-      then have "homotopic_with (\<lambda>r. r 1 = r 0) (top_of_set {0..1}) euclidean q (\<lambda>t. 0)"
-        by (simp add: homotopic_loops_def pathfinish_def pathstart_def)
-      then show "homotopic_with (\<lambda>h. exp (h 1) = exp (h 0)) (top_of_set {0..1}) euclidean q (\<lambda>t. 0)"
-        by (rule homotopic_with_mono) simp
-    qed
-    show "continuous_on UNIV (\<lambda>w. \<zeta> + exp w)"
-      by (rule continuous_intros)+
-    show "range (\<lambda>w. \<zeta> + exp w) \<subseteq> -{\<zeta>}"
-      by auto
-  qed
-  then have "homotopic_with_canon (\<lambda>r. pathfinish r = pathstart r) {0..1} (-{\<zeta>}) p (\<lambda>x. \<zeta> + 1)"
-    by (rule homotopic_with_eq) (auto simp: o_def peq pathfinish_def pathstart_def)
-  then have "homotopic_loops (-{\<zeta>}) p (\<lambda>t. \<zeta> + 1)"
-    by (simp add: homotopic_loops_def)
-  then show ?rhs ..
-next
-  assume ?rhs
-  then obtain a where "homotopic_loops (-{\<zeta>}) p (\<lambda>t. a)" ..
-  then have "winding_number p \<zeta> = winding_number (\<lambda>t. a) \<zeta>" "a \<noteq> \<zeta>"
-    using winding_number_homotopic_loops homotopic_loops_imp_subset by (force simp:)+
-  moreover have "winding_number (\<lambda>t. a) \<zeta> = 0"
-    by (metis winding_number_zero_const \<open>a \<noteq> \<zeta>\<close>)
-  ultimately show ?lhs by metis
-qed
-
-lemma winding_number_homotopic_paths_null_explicit_eq:
-  assumes "path p" and \<zeta>: "\<zeta> \<notin> path_image p"
-  shows "winding_number p \<zeta> = 0 \<longleftrightarrow> homotopic_paths (-{\<zeta>}) p (linepath (pathstart p) (pathstart p))"
-        (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then show ?rhs
-    apply (auto simp: winding_number_homotopic_loops_null_eq [OF assms])
-    apply (rule homotopic_loops_imp_homotopic_paths_null)
-    apply (simp add: linepath_refl)
-    done
-next
-  assume ?rhs
-  then show ?lhs
-    by (metis \<zeta> pathstart_in_path_image winding_number_homotopic_paths winding_number_trivial)
-qed
-
-lemma winding_number_homotopic_paths_null_eq:
-  assumes "path p" and \<zeta>: "\<zeta> \<notin> path_image p"
-  shows "winding_number p \<zeta> = 0 \<longleftrightarrow> (\<exists>a. homotopic_paths (-{\<zeta>}) p (\<lambda>t. a))"
-    (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then show ?rhs
-    by (auto simp: winding_number_homotopic_paths_null_explicit_eq [OF assms] linepath_refl)
-next
-  assume ?rhs
-  then show ?lhs
-    by (metis \<zeta> homotopic_paths_imp_pathfinish pathfinish_def pathfinish_in_path_image winding_number_homotopic_paths winding_number_zero_const)
-qed
-
-proposition winding_number_homotopic_paths_eq:
-  assumes "path p" and \<zeta>p: "\<zeta> \<notin> path_image p"
-      and "path q" and \<zeta>q: "\<zeta> \<notin> path_image q"
-      and qp: "pathstart q = pathstart p" "pathfinish q = pathfinish p"
-    shows "winding_number p \<zeta> = winding_number q \<zeta> \<longleftrightarrow> homotopic_paths (-{\<zeta>}) p q"
-    (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then have "winding_number (p +++ reversepath q) \<zeta> = 0"
-    using assms by (simp add: winding_number_join winding_number_reversepath)
-  moreover
-  have "path (p +++ reversepath q)" "\<zeta> \<notin> path_image (p +++ reversepath q)"
-    using assms by (auto simp: not_in_path_image_join)
-  ultimately obtain a where "homotopic_paths (- {\<zeta>}) (p +++ reversepath q) (linepath a a)"
-    using winding_number_homotopic_paths_null_explicit_eq by blast
-  then show ?rhs
-    using homotopic_paths_imp_pathstart assms
-    by (fastforce simp add: dest: homotopic_paths_imp_homotopic_loops homotopic_paths_loop_parts)
-next
-  assume ?rhs
-  then show ?lhs
-    by (simp add: winding_number_homotopic_paths)
-qed
-
-lemma winding_number_homotopic_loops_eq:
-  assumes "path p" and \<zeta>p: "\<zeta> \<notin> path_image p"
-      and "path q" and \<zeta>q: "\<zeta> \<notin> path_image q"
-      and loops: "pathfinish p = pathstart p" "pathfinish q = pathstart q"
-    shows "winding_number p \<zeta> = winding_number q \<zeta> \<longleftrightarrow> homotopic_loops (-{\<zeta>}) p q"
-    (is "?lhs = ?rhs")
-proof
-  assume L: ?lhs
-  have "pathstart p \<noteq> \<zeta>" "pathstart q \<noteq> \<zeta>"
-    using \<zeta>p \<zeta>q by blast+
-  moreover have "path_connected (-{\<zeta>})"
-    by (simp add: path_connected_punctured_universe)
-  ultimately obtain r where "path r" and rim: "path_image r \<subseteq> -{\<zeta>}"
-                        and pas: "pathstart r = pathstart p" and paf: "pathfinish r = pathstart q"
-    by (auto simp: path_connected_def)
-  then have "pathstart r \<noteq> \<zeta>" by blast
-  have "homotopic_loops (- {\<zeta>}) p (r +++ q +++ reversepath r)"
-  proof (rule homotopic_paths_imp_homotopic_loops)
-    show "homotopic_paths (- {\<zeta>}) p (r +++ q +++ reversepath r)"
-      by (metis (mono_tags, hide_lams) \<open>path r\<close> L \<zeta>p \<zeta>q \<open>path p\<close> \<open>path q\<close> homotopic_loops_conjugate loops not_in_path_image_join paf pas path_image_reversepath path_imp_reversepath path_join_eq pathfinish_join pathfinish_reversepath  pathstart_join pathstart_reversepath rim subset_Compl_singleton winding_number_homotopic_loops winding_number_homotopic_paths_eq)
-  qed (use loops pas in auto)
-  moreover have "homotopic_loops (- {\<zeta>}) (r +++ q +++ reversepath r) q"
-    using rim \<zeta>q by (auto simp: homotopic_loops_conjugate paf \<open>path q\<close> \<open>path r\<close> loops)
-  ultimately show ?rhs
-    using homotopic_loops_trans by metis
-next
-  assume ?rhs
-  then show ?lhs
-    by (simp add: winding_number_homotopic_loops)
-qed
-
-end
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Complex_Analysis/Cauchy_Integral_Theorem.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -0,0 +1,7159 @@
+section \<open>Complex Path Integrals and Cauchy's Integral Theorem\<close>
+
+text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2015)\<close>
+
+theory Cauchy_Integral_Theorem
+imports
+  "HOL-Analysis.Analysis"
+begin
+
+lemma leibniz_rule_holomorphic:
+  fixes f::"complex \<Rightarrow> 'b::euclidean_space \<Rightarrow> complex"
+  assumes "\<And>x t. x \<in> U \<Longrightarrow> t \<in> cbox a b \<Longrightarrow> ((\<lambda>x. f x t) has_field_derivative fx x t) (at x within U)"
+  assumes "\<And>x. x \<in> U \<Longrightarrow> (f x) integrable_on cbox a b"
+  assumes "continuous_on (U \<times> (cbox a b)) (\<lambda>(x, t). fx x t)"
+  assumes "convex U"
+  shows "(\<lambda>x. integral (cbox a b) (f x)) holomorphic_on U"
+  using leibniz_rule_field_differentiable[OF assms(1-3) _ assms(4)]
+  by (auto simp: holomorphic_on_def)
+
+lemma Ln_measurable [measurable]: "Ln \<in> measurable borel borel"
+proof -
+  have *: "Ln (-of_real x) = of_real (ln x) + \<i> * pi" if "x > 0" for x
+    using that by (subst Ln_minus) (auto simp: Ln_of_real)
+  have **: "Ln (of_real x) = of_real (ln (-x)) + \<i> * pi" if "x < 0" for x
+    using *[of "-x"] that by simp
+  have cont: "(\<lambda>x. indicat_real (- \<real>\<^sub>\<le>\<^sub>0) x *\<^sub>R Ln x) \<in> borel_measurable borel"
+    by (intro borel_measurable_continuous_on_indicator continuous_intros) auto
+  have "(\<lambda>x. if x \<in> \<real>\<^sub>\<le>\<^sub>0 then ln (-Re x) + \<i> * pi else indicator (-\<real>\<^sub>\<le>\<^sub>0) x *\<^sub>R Ln x) \<in> borel \<rightarrow>\<^sub>M borel"
+    (is "?f \<in> _") by (rule measurable_If_set[OF _ cont]) auto
+  hence "(\<lambda>x. if x = 0 then Ln 0 else ?f x) \<in> borel \<rightarrow>\<^sub>M borel" by measurable
+  also have "(\<lambda>x. if x = 0 then Ln 0 else ?f x) = Ln"
+    by (auto simp: fun_eq_iff ** nonpos_Reals_def)
+  finally show ?thesis .
+qed
+
+lemma powr_complex_measurable [measurable]:
+  assumes [measurable]: "f \<in> measurable M borel" "g \<in> measurable M borel"
+  shows   "(\<lambda>x. f x powr g x :: complex) \<in> measurable M borel"
+  using assms by (simp add: powr_def) 
+
+subsection\<open>Contour Integrals along a path\<close>
+
+text\<open>This definition is for complex numbers only, and does not generalise to line integrals in a vector field\<close>
+
+text\<open>piecewise differentiable function on [0,1]\<close>
+
+definition\<^marker>\<open>tag important\<close> has_contour_integral :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> (real \<Rightarrow> complex) \<Rightarrow> bool"
+           (infixr "has'_contour'_integral" 50)
+  where "(f has_contour_integral i) g \<equiv>
+           ((\<lambda>x. f(g x) * vector_derivative g (at x within {0..1}))
+            has_integral i) {0..1}"
+
+definition\<^marker>\<open>tag important\<close> contour_integrable_on
+           (infixr "contour'_integrable'_on" 50)
+  where "f contour_integrable_on g \<equiv> \<exists>i. (f has_contour_integral i) g"
+
+definition\<^marker>\<open>tag important\<close> contour_integral
+  where "contour_integral g f \<equiv> SOME i. (f has_contour_integral i) g \<or> \<not> f contour_integrable_on g \<and> i=0"
+
+lemma not_integrable_contour_integral: "\<not> f contour_integrable_on g \<Longrightarrow> 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 \<Longrightarrow> 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
+
+lemma has_contour_integral_eqpath:
+     "\<lbrakk>(f has_contour_integral y) p; f contour_integrable_on \<gamma>;
+       contour_integral p f = contour_integral \<gamma> f\<rbrakk>
+      \<Longrightarrow> (f has_contour_integral y) \<gamma>"
+using contour_integrable_on_def contour_integral_unique by auto
+
+lemma has_contour_integral_integral:
+    "f contour_integrable_on i \<Longrightarrow> (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 \<Longrightarrow> (f has_contour_integral j) g \<Longrightarrow> i = j"
+  using has_integral_unique
+  by (auto simp: has_contour_integral_def)
+
+lemma has_contour_integral_integrable: "(f has_contour_integral i) g \<Longrightarrow> f contour_integrable_on g"
+  using contour_integrable_on_def by blast
+
+text\<open>Show that we can forget about the localized derivative.\<close>
+
+lemma has_integral_localized_vector_derivative:
+    "((\<lambda>x. f (g x) * vector_derivative g (at x within {a..b})) has_integral i) {a..b} \<longleftrightarrow>
+     ((\<lambda>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: at_within_interior [of _ "{a..b}"])
+    done
+qed
+
+lemma integrable_on_localized_vector_derivative:
+    "(\<lambda>x. f (g x) * vector_derivative g (at x within {a..b})) integrable_on {a..b} \<longleftrightarrow>
+     (\<lambda>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 \<longleftrightarrow>
+      ((\<lambda>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 \<longleftrightarrow>
+      (\<lambda>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\<^marker>\<open>tag unimportant\<close> \<open>Reversing a path\<close>
+
+
+
+lemma has_contour_integral_reversepath:
+  assumes "valid_path g" and f: "(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 \<notin> (-) 1 ` S" "0 \<le> x" "x \<le> 1"
+    have "vector_derivative (\<lambda>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 \<circ> (\<lambda>x. 1 - x) has_vector_derivative -1 *\<^sub>R f') (at x)"
+        by (intro vector_diff_chain_within has_vector_derivative_at_within [OF f'] derivative_eq_intros | simp)+
+      then have mf': "((\<lambda>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
+  obtain S where S: "continuous_on {0..1} g" "finite S" "g C1_differentiable_on {0..1} - S"
+    using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
+  have "((\<lambda>x. - (f (g (1 - x)) * vector_derivative g (at (1 - x) within {0..1}))) has_integral -i)
+       {0..1}"
+    using has_integral_affinity01 [where m= "-1" and c=1, OF f [unfolded has_contour_integral_def]]
+    by (simp add: has_integral_neg)
+  then show ?thesis
+    using S
+    apply (clarsimp simp: reversepath_def has_contour_integral_def)
+    apply (rule_tac S = "(\<lambda>x. 1 - x) ` S" in has_integral_spike_finite)
+      apply (auto simp: *)
+    done
+qed
+
+lemma contour_integrable_reversepath:
+    "valid_path g \<Longrightarrow> f contour_integrable_on g \<Longrightarrow> 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 \<Longrightarrow> (f contour_integrable_on (reversepath g) \<longleftrightarrow> 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 "\<not> 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\<^marker>\<open>tag unimportant\<close> \<open>Joining two paths together\<close>
+
+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" "\<forall>x\<in>{0..1} - s1. g1 differentiable at x"
+      and s2: "finite s2" "\<forall>x\<in>{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: "((\<lambda>x. f (g1 x) * vector_derivative g1 (at x)) has_integral i1) {0..1}"
+   and 2: "((\<lambda>x. f (g2 x) * vector_derivative g2 (at x)) has_integral i2) {0..1}"
+    using assms
+    by (auto simp: has_contour_integral)
+  have i1: "((\<lambda>x. (2*f (g1 (2*x))) * vector_derivative g1 (at (2*x))) has_integral i1) {0..1/2}"
+   and i2: "((\<lambda>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: "\<lbrakk>0 \<le> z; z*2 < 1; z*2 \<notin> s1\<rbrakk> \<Longrightarrow>
+            vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at z) =
+            2 *\<^sub>R vector_derivative g1 (at (z*2))" for z
+    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g1(2*x))" and d = "\<bar>z - 1/2\<bar>"]])
+    apply (simp_all add: dist_real_def abs_if split: if_split_asm)
+    apply (rule vector_diff_chain_at [of "\<lambda>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: "\<lbrakk>1 < z*2; z \<le> 1; z*2 - 1 \<notin> s2\<rbrakk> \<Longrightarrow>
+            vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at z) =
+            2 *\<^sub>R vector_derivative g2 (at (z*2 - 1))" for z
+    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g2 (2*x - 1))" and d = "\<bar>z - 1/2\<bar>"]])
+    apply (simp_all add: dist_real_def abs_if split: if_split_asm)
+    apply (rule vector_diff_chain_at [of "\<lambda>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 "((\<lambda>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) ((*)2 -` s1)"])
+    using s1
+    apply (force intro: finite_vimageI [where h = "(*)2"] inj_onI)
+    apply (clarsimp simp add: joinpaths_def scaleR_conv_of_real mult_ac g1)
+    done
+  moreover have "((\<lambda>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) ((\<lambda>x. 2*x-1) -` s2)"])
+    using s2
+    apply (force intro: finite_vimageI [where h = "\<lambda>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" "\<forall>x\<in>{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 "(\<lambda>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 *: "(\<lambda>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: "\<lbrakk>0 < z; z < 1; z \<notin> s1\<rbrakk> \<Longrightarrow>
+            vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at (z/2)) =
+            2 *\<^sub>R vector_derivative g1 (at z)"  for z
+    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g1(2*x))" and d = "\<bar>(z-1)/2\<bar>"]])
+    apply (simp_all add: field_simps dist_real_def abs_if split: if_split_asm)
+    apply (rule vector_diff_chain_at [of "\<lambda>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} \<union> 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" "\<forall>x\<in>{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 "(\<lambda>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 *: "(\<lambda>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: "\<lbrakk>0 < z; z < 1; z \<notin> s2\<rbrakk> \<Longrightarrow>
+            vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at (z/2+1/2)) =
+            2 *\<^sub>R vector_derivative g2 (at z)" for z
+    apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g2(2*x-1))" and d = "\<bar>z/2\<bar>"]])
+    apply (simp_all add: field_simps dist_real_def abs_if split: if_split_asm)
+    apply (rule vector_diff_chain_at [of "\<lambda>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} \<union> s2", OF _ _ *])
+    apply (auto simp: joinpaths_def scaleR_conv_of_real g2)
+    done
+qed
+
+lemma contour_integrable_join [simp]:
+  shows
+    "\<lbrakk>valid_path g1; valid_path g2\<rbrakk>
+     \<Longrightarrow> f contour_integrable_on (g1 +++ g2) \<longleftrightarrow> f contour_integrable_on g1 \<and> f contour_integrable_on g2"
+using contour_integrable_joinD1 contour_integrable_joinD2 contour_integrable_joinI by blast
+
+lemma contour_integral_join [simp]:
+  shows
+    "\<lbrakk>f contour_integrable_on g1; f contour_integrable_on g2; valid_path g1; valid_path g2\<rbrakk>
+        \<Longrightarrow> 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\<^marker>\<open>tag unimportant\<close> \<open>Shifting the starting point of a (closed) path\<close>
+
+lemma has_contour_integral_shiftpath:
+  assumes f: "(f has_contour_integral i) g" "valid_path g"
+      and a: "a \<in> {0..1}"
+    shows "(f has_contour_integral i) (shiftpath a g)"
+proof -
+  obtain s
+    where s: "finite s" and g: "\<forall>x\<in>{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 *: "((\<lambda>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} (\<lambda>x. f (g x) * vector_derivative g (at x)) +
+                    integral {0..a} (\<lambda>x. f (g x) * vector_derivative g (at x))"
+    apply (rule has_integral_unique)
+    apply (subst add.commute)
+    apply (subst Henstock_Kurzweil_Integration.integral_combine)
+    using assms * integral_unique by auto
+  { fix x
+    have "0 \<le> x \<Longrightarrow> x + a < 1 \<Longrightarrow> x \<notin> (\<lambda>x. x - a) ` s \<Longrightarrow>
+         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 = "(\<lambda>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 "\<lambda>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 \<Longrightarrow> x \<le> 1 \<Longrightarrow> x \<notin> (\<lambda>x. x - a + 1) ` s \<Longrightarrow>
+          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 = "(\<lambda>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 "\<lambda>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: "(\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on ({a..1})"
+    using * a   by (fastforce intro: integrable_subinterval_real)
+  have v0a: "(\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on ({0..a})"
+    apply (rule integrable_subinterval_real)
+    using * a by auto
+  have "((\<lambda>x. f (shiftpath a g x) * vector_derivative (shiftpath a g) (at x))
+        has_integral  integral {a..1} (\<lambda>x. f (g x) * vector_derivative g (at x)))  {0..1 - a}"
+    apply (rule has_integral_spike_finite
+             [where S = "{1-a} \<union> (\<lambda>x. x-a) ` s" and f = "\<lambda>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 "((\<lambda>x. f (shiftpath a g x) * vector_derivative (shiftpath a g) (at x))
+        has_integral  integral {0..a} (\<lambda>x. f (g x) * vector_derivative g (at x)))  {1 - a..1}"
+    apply (rule has_integral_spike_finite
+             [where S = "{1-a} \<union> (\<lambda>x. x-a+1) ` s" and f = "\<lambda>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 \<in> {0..1}"
+    shows "(f has_contour_integral i) g"
+proof -
+  obtain s
+    where s: "finite s" and g: "\<forall>x\<in>{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 \<notin> 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
+                        at_within_interior [of _ "{0..1}"] 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} \<union> 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 \<in> {0..1}"
+    shows "(f has_contour_integral i) (shiftpath a g) \<longleftrightarrow> (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 \<in> {0..1}"
+    shows "f contour_integrable_on (shiftpath a g) \<longleftrightarrow> 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 \<in> {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\<^marker>\<open>tag unimportant\<close> \<open>More about straight-line paths\<close>
+
+lemma has_contour_integral_linepath:
+  shows "(f has_contour_integral i) (linepath a b) \<longleftrightarrow>
+         ((\<lambda>x. f(linepath a b x) * (b - a)) has_integral i) {0..1}"
+  by (simp add: has_contour_integral)
+
+lemma has_contour_integral_trivial [iff]: "(f has_contour_integral 0) (linepath a a)"
+  by (simp add: has_contour_integral_linepath)
+
+lemma has_contour_integral_trivial_iff [simp]: "(f has_contour_integral i) (linepath a a) \<longleftrightarrow> i=0"
+  using has_contour_integral_unique by blast
+
+lemma contour_integral_trivial [simp]: "contour_integral (linepath a a) f = 0"
+  using has_contour_integral_trivial contour_integral_unique by blast
+
+
+subsection\<open>Relation to subpath construction\<close>
+
+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: contour_integral_unique)
+
+lemma has_contour_integral_subpath:
+  assumes f: "f contour_integrable_on g" and g: "valid_path g"
+      and uv: "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
+    shows "(f has_contour_integral  integral {u..v} (\<lambda>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: "\<And>x. x \<in> {0..1} - s \<Longrightarrow> 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 *: "((\<lambda>x. f (g ((v - u) * x + u)) * vector_derivative g (at ((v - u) * x + u)))
+            has_integral (1 / (v - u)) * integral {u..v} (\<lambda>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 \<in> {0..1} \<Longrightarrow>
+           x \<notin> (\<lambda>t. (v-u) *\<^sub>R t + u) -` s \<Longrightarrow>
+           vector_derivative (\<lambda>x. g ((v-u) * x + u)) (at x) = (v-u) *\<^sub>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 = "(\<lambda>t. ((v-u) *\<^sub>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 \<in> {0..1}" "v \<in> {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_contour_integral_subpath:
+  assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
+    shows "(((\<lambda>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 \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
+    shows "contour_integral (subpath u v g) f =
+           integral {u..v} (\<lambda>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 \<in> {0..1}" "v \<in> {0..1}" "w \<in> {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 Henstock_Kurzweil_Integration.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 \<in> {0..1}" "v \<in> {0..1}" "w \<in> {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\<noteq>v \<and> v\<noteq>w \<and> u\<noteq>w")
+  case True
+    have *: "subpath v u g = reversepath(subpath u v g) \<and>
+             subpath w u g = reversepath(subpath u w g) \<and>
+             subpath w v g = reversepath(subpath v w g)"
+      by (auto simp: reversepath_subpath)
+    have "u < v \<and> v < w \<or>
+          u < w \<and> w < v \<or>
+          v < u \<and> u < w \<or>
+          v < w \<and> w < u \<or>
+          w < u \<and> u < v \<or>
+          w < v \<and> 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_subpath algebra_simps)
+      done
+next
+  case False
+  then show ?thesis
+    apply (auto)
+    using assms
+    by (metis eq_neg_iff_add_eq_0 contour_integral_reversepath reversepath_subpath valid_path_subpath)
+qed
+
+lemma contour_integral_integral:
+     "contour_integral g f = integral {0..1} (\<lambda>x. f (g x) * vector_derivative g (at x))"
+  by (simp add: contour_integral_def integral_def has_contour_integral contour_integrable_on)
+
+lemma contour_integral_cong:
+  assumes "g = g'" "\<And>x. x \<in> path_image g \<Longrightarrow> f x = f' x"
+  shows   "contour_integral g f = contour_integral g' f'"
+  unfolding contour_integral_integral using assms
+  by (intro integral_cong) (auto simp: path_image_def)
+
+
+text \<open>Contour integral along a segment on the real axis\<close>
+
+lemma has_contour_integral_linepath_Reals_iff:
+  fixes a b :: complex and f :: "complex \<Rightarrow> complex"
+  assumes "a \<in> Reals" "b \<in> Reals" "Re a < Re b"
+  shows   "(f has_contour_integral I) (linepath a b) \<longleftrightarrow>
+             ((\<lambda>x. f (of_real x)) has_integral I) {Re a..Re b}"
+proof -
+  from assms have [simp]: "of_real (Re a) = a" "of_real (Re b) = b"
+    by (simp_all add: complex_eq_iff)
+  from assms have "a \<noteq> b" by auto
+  have "((\<lambda>x. f (of_real x)) has_integral I) (cbox (Re a) (Re b)) \<longleftrightarrow>
+          ((\<lambda>x. f (a + b * of_real x - a * of_real x)) has_integral I /\<^sub>R (Re b - Re a)) {0..1}"
+    by (subst has_integral_affinity_iff [of "Re b - Re a" _ "Re a", symmetric])
+       (insert assms, simp_all add: field_simps scaleR_conv_of_real)
+  also have "(\<lambda>x. f (a + b * of_real x - a * of_real x)) =
+               (\<lambda>x. (f (a + b * of_real x - a * of_real x) * (b - a)) /\<^sub>R (Re b - Re a))"
+    using \<open>a \<noteq> b\<close> by (auto simp: field_simps fun_eq_iff scaleR_conv_of_real)
+  also have "(\<dots> has_integral I /\<^sub>R (Re b - Re a)) {0..1} \<longleftrightarrow> 
+               ((\<lambda>x. f (linepath a b x) * (b - a)) has_integral I) {0..1}" using assms
+    by (subst has_integral_cmul_iff) (auto simp: linepath_def scaleR_conv_of_real algebra_simps)
+  also have "\<dots> \<longleftrightarrow> (f has_contour_integral I) (linepath a b)" unfolding has_contour_integral_def
+    by (intro has_integral_cong) (simp add: vector_derivative_linepath_within)
+  finally show ?thesis by simp
+qed
+
+lemma contour_integrable_linepath_Reals_iff:
+  fixes a b :: complex and f :: "complex \<Rightarrow> complex"
+  assumes "a \<in> Reals" "b \<in> Reals" "Re a < Re b"
+  shows   "(f contour_integrable_on linepath a b) \<longleftrightarrow>
+             (\<lambda>x. f (of_real x)) integrable_on {Re a..Re b}"
+  using has_contour_integral_linepath_Reals_iff[OF assms, of f]
+  by (auto simp: contour_integrable_on_def integrable_on_def)
+
+lemma contour_integral_linepath_Reals_eq:
+  fixes a b :: complex and f :: "complex \<Rightarrow> complex"
+  assumes "a \<in> Reals" "b \<in> Reals" "Re a < Re b"
+  shows   "contour_integral (linepath a b) f = integral {Re a..Re b} (\<lambda>x. f (of_real x))"
+proof (cases "f contour_integrable_on linepath a b")
+  case True
+  thus ?thesis using has_contour_integral_linepath_Reals_iff[OF assms, of f]
+    using has_contour_integral_integral has_contour_integral_unique by blast
+next
+  case False
+  thus ?thesis using contour_integrable_linepath_Reals_iff[OF assms, of f]
+    by (simp add: not_integrable_contour_integral not_integrable_integral)
+qed
+
+
+
+text\<open>Cauchy's theorem where there's a primitive\<close>
+
+lemma contour_integral_primitive_lemma:
+  fixes f :: "complex \<Rightarrow> complex" and g :: "real \<Rightarrow> complex"
+  assumes "a \<le> b"
+      and "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
+      and "g piecewise_differentiable_on {a..b}"  "\<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s"
+    shows "((\<lambda>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" "\<forall>x\<in>{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} (\<lambda>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 \<notin> 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 (\<lambda>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 (*) (f' (g x))) (at (g x) within g ` {a..b})"
+      by (simp add: has_field_derivative_def)
+    have "((\<lambda>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_Icc_at)
+    done
+qed
+
+lemma contour_integral_primitive:
+  assumes "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
+      and "valid_path g" "path_image g \<subseteq> 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 "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
+      and "valid_path g"  "path_image g \<subseteq> s" "pathfinish g = pathstart g"
+    shows "(f' has_contour_integral 0) g"
+  using assms
+  by (metis diff_self contour_integral_primitive)
+
+text\<open>Existence of path integral for continuous function\<close>
+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} ((\<lambda>x. f x * (b - a)) \<circ> 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 = "\<lambda>x. f(linepath a b x)*(b - a)"])
+    apply (auto simp: vector_derivative_linepath_within)
+    done
+qed
+
+lemma has_field_der_id: "((\<lambda>x. x\<^sup>2 / 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) (\<lambda>y. y) = (b^2 - a^2)/2"
+  apply (rule contour_integral_unique)
+  using contour_integral_primitive [of UNIV "\<lambda>x. x^2/2" "\<lambda>x. x" "linepath a b"]
+  apply (auto simp: field_simps has_field_der_id)
+  done
+
+lemma contour_integrable_on_const [iff]: "(\<lambda>x. c) contour_integrable_on (linepath a b)"
+  by (simp add: contour_integrable_continuous_linepath)
+
+lemma contour_integrable_on_id [iff]: "(\<lambda>x. x) contour_integrable_on (linepath a b)"
+  by (simp add: contour_integrable_continuous_linepath)
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Arithmetical combining theorems\<close>
+
+lemma has_contour_integral_neg:
+    "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. -(f x)) has_contour_integral (-i)) g"
+  by (simp add: has_integral_neg has_contour_integral_def)
+
+lemma has_contour_integral_add:
+    "\<lbrakk>(f1 has_contour_integral i1) g; (f2 has_contour_integral i2) g\<rbrakk>
+     \<Longrightarrow> ((\<lambda>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:
+  "\<lbrakk>(f1 has_contour_integral i1) g; (f2 has_contour_integral i2) g\<rbrakk>
+         \<Longrightarrow> ((\<lambda>x. f1 x - f2 x) has_contour_integral (i1 - i2)) g"
+  by (simp add: has_integral_diff has_contour_integral_def algebra_simps)
+
+lemma has_contour_integral_lmul:
+  "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>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 \<Longrightarrow> ((\<lambda>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 \<Longrightarrow> ((\<lambda>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:
+    "\<lbrakk>(f has_contour_integral y) p; \<And>x. x \<in> path_image p \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> (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 \<le> B" "\<And>x. x \<in> closed_segment a b \<Longrightarrow> norm(f x) \<le> B"
+    shows "norm i \<le> B * norm(b - a)"
+proof -
+  { fix x::real
+    assume x: "0 \<le> x" "x \<le> 1"
+  have "norm (f (linepath a b x)) *
+        norm (vector_derivative (linepath a b) (at x within {0..1})) \<le> 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 \<le> (B * norm (b - a)) * content (cbox 0 (1::real))"
+    apply (rule has_integral_bound
+       [of _ "\<lambda>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 \<Rightarrow> real"
+  assumes "(f has_contour_integral i) (linepath a b)"
+          "finite k"
+          "0 \<le> B" "\<And>x::real. x \<in> closed_segment a b - k \<Longrightarrow> norm(f x) \<le> B"
+    shows "norm i \<le> B*norm(b - a)"
+*)
+
+lemma has_contour_integral_const_linepath: "((\<lambda>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: "((\<lambda>x. 0) has_contour_integral 0) g"
+  by (simp add: has_contour_integral_def)
+
+lemma has_contour_integral_is_0:
+    "(\<And>z. z \<in> path_image g \<Longrightarrow> f z = 0) \<Longrightarrow> (f has_contour_integral 0) g"
+  by (rule has_contour_integral_eq [OF has_contour_integral_0]) auto
+
+lemma has_contour_integral_sum:
+    "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a has_contour_integral i a) p\<rbrakk>
+     \<Longrightarrow> ((\<lambda>x. sum (\<lambda>a. f a x) s) has_contour_integral sum i s) p"
+  by (induction s rule: finite_induct) (auto simp: has_contour_integral_0 has_contour_integral_add)
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Operations on path integrals\<close>
+
+lemma contour_integral_const_linepath [simp]: "contour_integral (linepath a b) (\<lambda>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 \<Longrightarrow> contour_integral g (\<lambda>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 \<Longrightarrow> f2 contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>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 \<Longrightarrow> f2 contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>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
+           \<Longrightarrow> contour_integral g (\<lambda>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
+        \<Longrightarrow> contour_integral g (\<lambda>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
+        \<Longrightarrow> contour_integral g (\<lambda>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:
+    "(\<And>x. x \<in> path_image p \<Longrightarrow> f x = g x) \<Longrightarrow> 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:
+    "(\<And>z. z \<in> path_image g \<Longrightarrow> f z = 0) \<Longrightarrow> contour_integral g f = 0"
+  by (simp add: has_contour_integral_is_0 contour_integral_unique)
+
+lemma contour_integral_bound_linepath:
+  shows
+    "\<lbrakk>f contour_integrable_on (linepath a b);
+      0 \<le> B; \<And>x. x \<in> closed_segment a b \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
+     \<Longrightarrow> norm(contour_integral (linepath a b) f) \<le> 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 (\<lambda>x. 0) = 0"
+  by (simp add: contour_integral_unique has_contour_integral_0)
+
+lemma contour_integral_sum:
+    "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a) contour_integrable_on p\<rbrakk>
+     \<Longrightarrow> contour_integral p (\<lambda>x. sum (\<lambda>a. f a x) s) = sum (\<lambda>a. contour_integral p (f a)) s"
+  by (auto simp: contour_integral_unique has_contour_integral_sum has_contour_integral_integral)
+
+lemma contour_integrable_eq:
+    "\<lbrakk>f contour_integrable_on p; \<And>x. x \<in> path_image p \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> g contour_integrable_on p"
+  unfolding contour_integrable_on_def
+  by (metis has_contour_integral_eq)
+
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Arithmetic theorems for path integrability\<close>
+
+lemma contour_integrable_neg:
+    "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. -(f x)) contour_integrable_on g"
+  using has_contour_integral_neg contour_integrable_on_def by blast
+
+lemma contour_integrable_add:
+    "\<lbrakk>f1 contour_integrable_on g; f2 contour_integrable_on g\<rbrakk> \<Longrightarrow> (\<lambda>x. f1 x + f2 x) contour_integrable_on g"
+  using has_contour_integral_add contour_integrable_on_def
+  by fastforce
+
+lemma contour_integrable_diff:
+    "\<lbrakk>f1 contour_integrable_on g; f2 contour_integrable_on g\<rbrakk> \<Longrightarrow> (\<lambda>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 \<Longrightarrow> (\<lambda>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 \<Longrightarrow> (\<lambda>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 \<Longrightarrow> (\<lambda>x. f x / c) contour_integrable_on g"
+  using has_contour_integral_div contour_integrable_on_def
+  by fastforce
+
+lemma contour_integrable_sum:
+    "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a) contour_integrable_on p\<rbrakk>
+     \<Longrightarrow> (\<lambda>x. sum (\<lambda>a. f a x) s) contour_integrable_on p"
+   unfolding contour_integrable_on_def
+   by (metis has_contour_integral_sum)
+
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Reversing a path integral\<close>
+
+lemma has_contour_integral_reverse_linepath:
+    "(f has_contour_integral i) (linepath a b)
+     \<Longrightarrow> (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
+     \<Longrightarrow> 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 \<le> k" "k \<le> 1"
+      and c: "c - a = k *\<^sub>R (b - a)"
+    shows "(f has_contour_integral (i + j)) (linepath a b)"
+proof (cases "k = 0 \<or> k = 1")
+  case True
+  then show ?thesis
+    using assms by auto
+next
+  case False
+  then have k: "0 < k" "k < 1" "complex_of_real k \<noteq> 1"
+    using assms by auto
+  have c': "c = k *\<^sub>R (b - a) + a"
+    by (metis diff_add_cancel c)
+  have bc: "(b - c) = (1 - k) *\<^sub>R (b - a)"
+    by (simp add: algebra_simps c')
+  { assume *: "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R c) * (c - a)) has_integral i) {0..1}"
+    have **: "\<And>x. ((k - x) / k) *\<^sub>R a + (x / k) *\<^sub>R c = (1 - x) *\<^sub>R a + x *\<^sub>R b"
+      using False apply (simp add: c' algebra_simps)
+      apply (simp add: real_vector.scale_left_distrib [symmetric] field_split_simps)
+      done
+    have "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R b) * (b - a)) has_integral i) {0..k}"
+      using k has_integral_affinity01 [OF *, of "inverse k" "0"]
+      apply (simp add: divide_simps mult.commute [of _ "k"] image_affinity_atLeastAtMost ** c)
+      apply (auto dest: has_integral_cmul [where c = "inverse k"])
+      done
+  } note fi = this
+  { assume *: "((\<lambda>x. f ((1 - x) *\<^sub>R c + x *\<^sub>R b) * (b - c)) has_integral j) {0..1}"
+    have **: "\<And>x. (((1 - x) / (1 - k)) *\<^sub>R c + ((x - k) / (1 - k)) *\<^sub>R b) = ((1 - x) *\<^sub>R a + x *\<^sub>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 "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R b) * (b - a)) has_integral j) {k..1}"
+      using k has_integral_affinity01 [OF *, of "inverse(1 - k)" "-(k/(1 - k))"]
+      apply (simp add: divide_simps mult.commute [of _ "1-k"] image_affinity_atLeastAtMost ** bc)
+      apply (auto dest: has_integral_cmul [where k = "(1 - k) *\<^sub>R j" and c = "inverse (1 - k)"])
+      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 \<le> k" "k \<le> 1"
+      and c: "c - a = k *\<^sub>R (b - a)"
+    shows "continuous_on (closed_segment a c) f"
+proof -
+  have c': "c = (1 - k) *\<^sub>R a + k *\<^sub>R b"
+    using c by (simp add: algebra_simps)
+  have "closed_segment a c \<subseteq> closed_segment a b"
+    by (metis c' ends_in_segment(1) in_segment(1) k subset_closed_segment)
+  then show "continuous_on (closed_segment a c) f"
+    by (rule continuous_on_subset [OF f])
+qed
+
+lemma contour_integral_split:
+  assumes f: "continuous_on (closed_segment a b) f"
+      and k: "0 \<le> k" "k \<le> 1"
+      and c: "c - a = k *\<^sub>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) *\<^sub>R a + k *\<^sub>R b"
+    using c by (simp add: algebra_simps)
+  have "closed_segment a c \<subseteq> closed_segment a b"
+    by (metis c' ends_in_segment(1) in_segment(1) k subset_closed_segment)
+  moreover have "closed_segment c b \<subseteq> closed_segment a b"
+    by (metis c' ends_in_segment(2) in_segment(1) k subset_closed_segment)
+  ultimately
+  have *: "continuous_on (closed_segment a c) f" "continuous_on (closed_segment c b) f"
+    by (auto intro: continuous_on_subset [OF f])
+  show ?thesis
+    by (rule contour_integral_unique) (meson "*" c contour_integrable_continuous_linepath has_contour_integral_integral has_contour_integral_split k)
+qed
+
+lemma contour_integral_split_linepath:
+  assumes f: "continuous_on (closed_segment a b) f"
+      and c: "c \<in> 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])
+
+text\<open>The special case of midpoints used in the main quadrisection\<close>
+
+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
+    \<Longrightarrow> 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"])
+  apply (auto simp: midpoint_def algebra_simps scaleR_conv_of_real)
+  done
+
+
+text\<open>A couple of special case lemmas that are useful below\<close>
+
+lemma triangle_linear_has_chain_integral:
+    "((\<lambda>x. m*x + d) has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
+  apply (rule Cauchy_theorem_primitive [of UNIV "\<lambda>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)
+      \<Longrightarrow> 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)
+      \<Longrightarrow> 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\<open>Reversing the order in a double path integral\<close>
+
+text\<open>The condition is stronger than needed but it's often true in typical situations\<close>
+
+lemma fst_im_cbox [simp]: "cbox c d \<noteq> {} \<Longrightarrow> (fst ` cbox (a,c) (b,d)) = cbox a b"
+  by (auto simp: cbox_Pair_eq)
+
+lemma snd_im_cbox [simp]: "cbox a b \<noteq> {} \<Longrightarrow> (snd ` cbox (a,c) (b,d)) = cbox c d"
+  by (auto simp: cbox_Pair_eq)
+
+proposition contour_integral_swap:
+  assumes fcon:  "continuous_on (path_image g \<times> path_image h) (\<lambda>(y1,y2). f y1 y2)"
+      and vp:    "valid_path g" "valid_path h"
+      and gvcon: "continuous_on {0..1} (\<lambda>t. vector_derivative g (at t))"
+      and hvcon: "continuous_on {0..1} (\<lambda>t. vector_derivative h (at t))"
+  shows "contour_integral g (\<lambda>w. contour_integral h (f w)) =
+         contour_integral h (\<lambda>z. contour_integral g (\<lambda>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: "\<And>x. (\<lambda>t. f (g x) (h t)) = (\<lambda>(y1,y2). f y1 y2) \<circ> (\<lambda>t. (g x, h t))"
+    by (rule ext) simp
+  have fgh2: "\<And>x. (\<lambda>t. f (g t) (h x)) = (\<lambda>(y1,y2). f y1 y2) \<circ> (\<lambda>t. (g t, h x))"
+    by (rule ext) simp
+  have fcon_im1: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> continuous_on ((\<lambda>t. (g x, h t)) ` {0..1}) (\<lambda>(x, y). f x y)"
+    by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
+  have fcon_im2: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> continuous_on ((\<lambda>t. (g t, h x)) ` {0..1}) (\<lambda>(x, y). f x y)"
+    by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
+  have "\<And>y. y \<in> {0..1} \<Longrightarrow> continuous_on {0..1} (\<lambda>x. f (g x) (h y))"
+    by (subst fgh2) (rule fcon_im2 gcon continuous_intros | simp)+
+  then have vdg: "\<And>y. y \<in> {0..1} \<Longrightarrow> (\<lambda>x. f (g x) (h y) * vector_derivative g (at x)) integrable_on {0..1}"
+    using continuous_on_mult gvcon integrable_continuous_real by blast
+  have "(\<lambda>z. vector_derivative g (at (fst z))) = (\<lambda>x. vector_derivative g (at x)) \<circ> fst"
+    by auto
+  then have gvcon': "continuous_on (cbox (0, 0) (1, 1::real)) (\<lambda>x. vector_derivative g (at (fst x)))"
+    apply (rule ssubst)
+    apply (rule continuous_intros | simp add: gvcon)+
+    done
+  have "(\<lambda>z. vector_derivative h (at (snd z))) = (\<lambda>x. vector_derivative h (at x)) \<circ> snd"
+    by auto
+  then have hvcon': "continuous_on (cbox (0, 0) (1::real, 1)) (\<lambda>x. vector_derivative h (at (snd x)))"
+    apply (rule ssubst)
+    apply (rule continuous_intros | simp add: hvcon)+
+    done
+  have "(\<lambda>x. f (g (fst x)) (h (snd x))) = (\<lambda>(y1,y2). f y1 y2) \<circ> (\<lambda>w. ((g \<circ> fst) w, (h \<circ> snd) w))"
+    by auto
+  then have fgh: "continuous_on (cbox (0, 0) (1, 1)) (\<lambda>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} (\<lambda>x. contour_integral h (f (g x)) * vector_derivative g (at x)) =
+        integral {0..1} (\<lambda>x. contour_integral h (\<lambda>y. f (g x) y * vector_derivative g (at x)))"
+  proof (rule integral_cong [OF contour_integral_rmul [symmetric]])
+    show "\<And>x. x \<in> {0..1} \<Longrightarrow> f (g x) contour_integrable_on h"
+      unfolding 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
+  qed
+  also have "\<dots> = integral {0..1}
+                     (\<lambda>y. contour_integral g (\<lambda>x. f x (h y) * vector_derivative h (at y)))"
+    unfolding 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 "\<dots> = contour_integral h (\<lambda>z. contour_integral g (\<lambda>w. f w z))"
+    unfolding 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\<^marker>\<open>tag unimportant\<close> \<open>The key quadrisection step\<close>
+
+lemma norm_sum_half:
+  assumes "norm(a + b) \<ge> e"
+    shows "norm a \<ge> e/2 \<or> norm b \<ge> e/2"
+proof -
+  have "e \<le> 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 \<le> norm (a + b + c + d)"
+    shows "e / 4 \<le> norm a \<or> e / 4 \<le> norm b \<or> e / 4 \<le> norm c \<or> e / 4 \<le> norm d"
+proof -
+  have "e \<le> 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 \<le> K" "dist b c \<le> K" "dist c a \<le> K"
+      and e: "e * K^2 \<le>
+              norm (contour_integral(linepath a b) f + contour_integral(linepath b c) f + contour_integral(linepath c a) f)"
+  shows "\<exists>a' b' c'.
+           a' \<in> convex hull {a,b,c} \<and> b' \<in> convex hull {a,b,c} \<and> c' \<in> convex hull {a,b,c} \<and>
+           dist a' b' \<le> K/2  \<and>  dist b' c' \<le> K/2  \<and>  dist c' a' \<le> K/2  \<and>
+           e * (K/2)^2 \<le> norm(contour_integral(linepath a' b') f + contour_integral(linepath b' c') f + contour_integral(linepath c' a') f)"
+         (is "\<exists>x y z. ?\<Phi> x y z")
+proof -
+  note divide_le_eq_numeral1 [simp del]
+  define a' where "a' = midpoint b c"
+  define b' where "b' = midpoint c a"
+  define c' where "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
+    by (rule continuous_on_subset [OF f],
+           metis midpoints_in_convex_hull convex_hull_subset hull_subset insert_subset segment_convex_hull)+
+  let ?pathint = "\<lambda>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')"
+    by (simp add: fcont' contour_integral_reverse_linepath) (simp add: a'_def b'_def c'_def contour_integral_midpoint fabc)
+  have [simp]: "\<And>x y. cmod (x * 2 - y * 2) = cmod (x - y) * 2"
+    by (metis left_diff_distrib mult.commute norm_mult_numeral1)
+  have [simp]: "\<And>x y. cmod (x - y) = cmod (y - x)"
+    by (simp add: norm_minus_commute)
+  consider "e * K\<^sup>2 / 4 \<le> cmod (?pathint a c' + ?pathint c' b' + ?pathint b' a)" |
+           "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' c' + ?pathint c' b + ?pathint b a')" |
+           "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' c + ?pathint c b' + ?pathint b' a')" |
+           "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' b' + ?pathint b' c' + ?pathint c' a')"
+    using assms unfolding * by (blast intro: that dest!: norm_sum_lemma)
+  then show ?thesis
+  proof cases
+    case 1 then have "?\<Phi> a c' b'"
+      using assms
+      apply (clarsimp simp: c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
+      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real field_split_simps)
+      done
+    then show ?thesis by blast
+  next
+    case 2 then  have "?\<Phi> a' c' b"
+      using assms
+      apply (clarsimp simp: a'_def c'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
+      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real field_split_simps)
+      done
+    then show ?thesis by blast
+  next
+    case 3 then have "?\<Phi> a' c b'"
+      using assms
+      apply (clarsimp simp: a'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
+      apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real field_split_simps)
+      done
+    then show ?thesis by blast
+  next
+    case 4 then have "?\<Phi> a' b' c'"
+      using assms
+      apply (clarsimp 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 field_split_simps)
+      done
+    then show ?thesis by blast
+  qed
+qed
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Cauchy's theorem for triangles\<close>
+
+lemma triangle_points_closer:
+  fixes a::complex
+  shows "\<lbrakk>x \<in> convex hull {a,b,c};  y \<in> convex hull {a,b,c}\<rbrakk>
+         \<Longrightarrow> norm(x - y) \<le> norm(a - b) \<or>
+             norm(x - y) \<le> norm(b - c) \<or>
+             norm(x - y) \<le> 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 \<in> S"
+      and f: "continuous_on S f"
+      and cd: "f field_differentiable (at x within S)"
+      and e: "0 < e"
+    shows "\<exists>k>0. \<forall>a b c. dist a b \<le> k \<and> dist b c \<le> k \<and> dist c a \<le> k \<and>
+              x \<in> convex hull {a,b,c} \<and> convex hull {a,b,c} \<subseteq> S
+              \<longrightarrow> norm(contour_integral(linepath a b) f + contour_integral(linepath b c) f +
+                       contour_integral(linepath c a) f)
+                  \<le> e*(dist a b + dist b c + dist c a)^2"
+           (is "\<exists>k>0. \<forall>a b c. _ \<longrightarrow> ?normle a b c")
+proof -
+  have le_of_3: "\<And>a x y z. \<lbrakk>0 \<le> x*y; 0 \<le> x*z; 0 \<le> y*z; a \<le> (e*(x + y + z))*x + (e*(x + y + z))*y + (e*(x + y + z))*z\<rbrakk>
+                     \<Longrightarrow> a \<le> e*(x + y + z)^2"
+    by (simp add: algebra_simps power2_eq_square)
+  have disj_le: "\<lbrakk>x \<le> a \<or> x \<le> b \<or> x \<le> c; 0 \<le> a; 0 \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> x \<le> 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} \<subseteq> 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': "\<And>y. \<lbrakk>cmod (y - x) \<le> d; y \<in> S\<rbrakk> \<Longrightarrow> cmod (f y - f x - f' * (y - x)) \<le> e * cmod (y - x)"
+       and le: "cmod (a - b) \<le> d" "cmod (b - c) \<le> d" "cmod (c - a) \<le> d"
+       and xc: "x \<in> convex hull {a, b, c}"
+       and S: "convex hull {a, b, c} \<subseteq> 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) (\<lambda>y. f y - f x - f'*(y - x)) +
+              contour_integral (linepath b c) (\<lambda>y. f y - f x - f'*(y - x)) +
+              contour_integral (linepath c a) (\<lambda>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 \<in> convex hull {a,b,c}"
+      have "cmod (f y - f x - f' * (y - x)) \<le> e*norm(y - x)"
+      proof (rule f')
+        show "cmod (y - x) \<le> d"
+          by (metis triangle_points_closer [OF xc yc] le norm_minus_commute order_trans)
+      qed (use S yc in blast)
+      also have "\<dots> \<le> 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)) \<le> e * (cmod (a - b) + cmod (b - c) + cmod (c - a))" .
+    } note cm_le = this
+    have "?normle a b c"
+      unfolding 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 * unfolding dist_norm
+    apply blast
+    done
+qed
+
+
+text\<open>Hence the most basic theorem for a triangle.\<close>
+
+locale Chain =
+  fixes x0 At Follows
+  assumes At0: "At x0 0"
+      and AtSuc: "\<And>x n. At x n \<Longrightarrow> \<exists>x'. At x' (Suc n) \<and> Follows x' x"
+begin
+  primrec f where
+    "f 0 = x0"
+  | "f (Suc n) = (SOME x. At x (Suc n) \<and> 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: "\<And>x y z n. At x y z n \<Longrightarrow> \<exists>x' y' z'. At x' y' z' (Suc n) \<and> Follows x' y' z' x y z"
+  obtains f g h where
+    "f 0 = x0" "g 0 = y0" "h 0 = z0"
+                      "\<And>n. At (f n) (g n) (h n) n"
+                       "\<And>n. Follows (f(Suc n)) (g(Suc n)) (h(Suc n)) (f n) (g n) (h n)"
+proof -
+  interpret three: Chain "(x0,y0,z0)" "\<lambda>(x,y,z). At x y z" "\<lambda>(x',y',z'). \<lambda>(x,y,z). Follows x' y' z' x y z"
+    apply unfold_locales
+    using At0 AtSuc by auto
+  show ?thesis
+  apply (rule that [of "\<lambda>n. fst (three.f n)"  "\<lambda>n. fst (snd (three.f n))" "\<lambda>n. snd (snd (three.f n))"])
+  using three.At three.Follows
+  apply simp_all
+  apply (simp_all add: split_beta')
+  done
+qed
+
+proposition\<^marker>\<open>tag unimportant\<close> 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 = "\<lambda>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 \<noteq> 0"
+    define K where "K = 1 + max (dist a b) (max (dist b c) (dist c a))"
+    define e where "e = norm y / K^2"
+    have K1: "K \<ge> 1"  by (simp add: K_def max.coboundedI1)
+    then have K: "K > 0" by linarith
+    have [iff]: "dist a b \<le> K" "dist b c \<le> K" "dist c a \<le> K"
+      by (simp_all add: K_def)
+    have e: "e > 0"
+      unfolding e_def using ynz K1 by simp
+    define At where "At x y z n \<longleftrightarrow>
+        convex hull {x,y,z} \<subseteq> convex hull {a,b,c} \<and>
+        dist x y \<le> K/2^n \<and> dist y z \<le> K/2^n \<and> dist z x \<le> K/2^n \<and>
+        norm(?pathint x y + ?pathint y z + ?pathint z x) \<ge> e*(K/2^n)^2"
+      for x y z n
+    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 metis
+      have "\<exists>x' y' z'. At x' y' z' (Suc n) \<and> convex hull {x',y',z'} \<subseteq> convex hull {x,y,z}"
+        using At Cauchy_theorem_quadrisection [OF contf', of "K/2^n" e]
+        apply (simp add: At_def 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: "\<And>n. convex hull {fa n, fb n, fc n} \<subseteq> convex hull {a,b,c}"
+        and dist: "\<And>n. dist (fa n) (fb n) \<le> K/2^n"
+                  "\<And>n. dist (fb n) (fc n) \<le> K/2^n"
+                  "\<And>n. dist (fc n) (fa n) \<le> K/2^n"
+        and no: "\<And>n. norm(?pathint (fa n) (fb n) +
+                           ?pathint (fb n) (fc n) +
+                           ?pathint (fc n) (fa n)) \<ge> e * (K/2^n)^2"
+        and conv_le: "\<And>n. convex hull {fa(Suc n), fb(Suc n), fc(Suc n)} \<subseteq> convex hull {fa n, fb n, fc n}"
+      apply (rule Chain3 [of At, OF At0 AtSuc])
+      apply (auto simp: At_def)
+      done
+    obtain x where x: "\<And>n. x \<in> convex hull {fa n, fb n, fc n}"
+    proof (rule bounded_closed_nest)
+      show "\<And>n. closed (convex hull {fa n, fb n, fc n})"
+        by (simp add: compact_imp_closed finite_imp_compact_convex_hull)
+      show "\<And>m n. m \<le> n \<Longrightarrow> convex hull {fa n, fb n, fc n} \<subseteq> convex hull {fa m, fb m, fc m}"
+        by (erule transitive_stepwise_le) (auto simp: conv_le)
+    qed (fastforce intro: finite_imp_bounded_convex_hull)+
+    then have xin: "x \<in> 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:
+            "\<And>x' y' z'.
+               \<lbrakk>dist x' y' \<le> k; dist y' z' \<le> k; dist z' x' \<le> k;
+                x \<in> convex hull {x',y',z'};
+                convex hull {x',y',z'} \<subseteq> convex hull {a,b,c}\<rbrakk>
+               \<Longrightarrow>
+               cmod (?pathint x' y' + ?pathint y' z' + ?pathint z' x') * 10
+                     \<le> e * (dist x' y' + dist y' z' + dist z' x')\<^sup>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) \<le> k" "dist (fb n) (fc n) \<le> k" "dist (fc n) (fa n) \<le> 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))\<^sup>2
+               \<le> (3 * K / 2 ^ n)\<^sup>2"
+        using dist [of n] e K
+        by (simp add: abs_le_square_iff [symmetric])
+      have less10: "\<And>x y::real. 0 < x \<Longrightarrow> y \<le> 9*x \<Longrightarrow> y < x*10"
+        by linarith
+      have "e * (dist (fa n) (fb n) + dist (fb n) (fc n) + dist (fc n) (fa n))\<^sup>2 \<le> e * (3 * K / 2 ^ n)\<^sup>2"
+        using ynz dle e mult_le_cancel_left_pos by blast
+      also have "\<dots> <
+          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]], 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\<^marker>\<open>tag unimportant\<close> \<open>Version needing function holomorphic in interior only\<close>
+
+lemma Cauchy_theorem_flat_lemma:
+  assumes f: "continuous_on (convex hull {a,b,c}) f"
+      and c: "c - a = k *\<^sub>R (b - a)"
+      and k: "0 \<le> 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 \<le> 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 *\<^sub>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 \<le> 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: "\<And>x. x \<in> convex hull {a,b,c} \<Longrightarrow> norm (f x) \<le> 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: "\<And>y. y \<in> convex hull {a,b,c} \<Longrightarrow> norm y < C"
+    using bounded_pos_less by blast
+  then have diff_2C: "norm(x - y) \<le> 2*C"
+           if x: "x \<in> convex hull {a, b, c}" and y: "y \<in> convex hull {a, b, c}" for x y
+  proof -
+    have "cmod x \<le> C"
+      using x by (meson Cno not_le not_less_iff_gr_or_eq)
+    hence "cmod (x - y) \<le> 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) \<le> 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 \<noteq> 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 \<or> a=b \<or> 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)
+        with False obtain d where "c \<noteq> a" "a \<noteq> b" "b \<noteq> c" "c - b = d *\<^sub>R (a - b)"
+          by (auto simp: collinear_3 collinear_lemma)
+        then have "False"
+          using False Cauchy_theorem_flat [OF contf'] pi_eq_y ynz
+          by (simp add: fabc add_eq_0_iff contour_integral_reverse_linepath)
+      }
+      then obtain d where d: "d \<in> interior (convex hull {a, b, c})"
+        by blast
+      { fix d1
+        assume d1_pos: "0 < d1"
+           and d1: "\<And>x x'. \<lbrakk>x\<in>convex hull {a, b, c}; x'\<in>convex hull {a, b, c}; cmod (x' - x) < d1\<rbrakk>
+                           \<Longrightarrow> cmod (f x' - f x) < cmod y / (24 * C)"
+        define e where "e = min 1 (min (d1/(4*C)) ((norm y / 24 / C) / B))"
+        define shrink where "shrink x = x - e *\<^sub>R (x - d)" for x
+        let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
+        have e: "0 < e" "e \<le> 1" "e \<le> d1 / (4 * C)" "e \<le> cmod y / 24 / C / B"
+          using d1_pos \<open>C>0\<close> \<open>B>0\<close> ynz by (simp_all add: e_def)
+        then have eCB: "24 * e * C * B \<le> cmod y"
+          using \<open>C>0\<close> \<open>B>0\<close>  by (simp add: field_simps)
+        have e_le_d1: "e * (4 * C) \<le> d1"
+          using e \<open>C>0\<close> by (simp add: field_simps)
+        have "shrink a \<in> interior(convex hull {a,b,c})"
+             "shrink b \<in> interior(convex hull {a,b,c})"
+             "shrink c \<in> 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)
+        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: "\<And>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: "\<And>a b d::complex. (b - e *\<^sub>R (b - d)) - (a - e *\<^sub>R (a - d)) - (b - a) = e *\<^sub>R (a - b)"
+          by (simp add: algebra_simps)
+        have "cmod y / (24 * C) \<le> cmod y / cmod (b - a) / 12"
+          using False \<open>C>0\<close> diff_2C [of b a] ynz
+          by (auto simp: field_split_simps hull_inc)
+        have less_C: "\<lbrakk>u \<in> convex hull {a, b, c}; 0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> x * cmod u < C" for x u
+          apply (cases "x=0", simp add: \<open>0<C\<close>)
+          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 \<in> convex hull {a, b, c}" "v \<in> convex hull {a, b, c}" "u\<noteq>v"
+             and fpi_uv: "f contour_integrable_on linepath (shrink u) (shrink v)"
+          have shr_uv: "shrink u \<in> interior(convex hull {a,b,c})"
+                       "shrink v \<in> interior(convex hull {a,b,c})"
+            using d e uv
+            by (auto simp: hull_inc mem_interior_convex_shrink shrink_def)
+          have cmod_fuv: "\<And>x. 0\<le>x \<Longrightarrow> x\<le>1 \<Longrightarrow> cmod (f (linepath (shrink u) (shrink v) x)) \<le> 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))) \<le> cmod y"
+            apply (rule order_trans [OF _ eCB])
+            using e \<open>B>0\<close> diff_2C [of u v] uv
+            by (auto simp: field_simps)
+          { fix x::real   assume x: "0\<le>x" "x\<le>1"
+            have cmod_less_4C: "cmod ((1 - x) *\<^sub>R u - (1 - x) *\<^sub>R d) + cmod (x *\<^sub>R v - x *\<^sub>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) *\<^sub>R (u - d) + x *\<^sub>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"
+              apply (simp only: ll norm_mult scaleR_diff_right)
+              using \<open>e>0\<close> cmod_less_4C apply (force intro: norm_triangle_lt less_le_trans [OF _ e_le_d1])
+              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 "\<dots> \<le> cmod y / cmod (v - u) / 12"
+              using False uv \<open>C>0\<close> diff_2C [of v u] ynz
+              by (auto simp: field_split_simps hull_inc)
+            finally have "cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) \<le> 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 \<le> 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))
+                          \<le> B * (cmod y / 24 / C / B * 2 * C) + 2 * C * (cmod y / 24 / C)"
+              apply (rule add_mono [OF mult_mono])
+              using By_uv e \<open>0 < B\<close> \<open>0 < C\<close> x apply (simp_all add: cmod_fuv cmod_shr cmod_12_le)
+              apply (simp add: field_simps)
+              done
+            also have "\<dots> \<le> cmod y / 6"
+              by simp
+            finally 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))
+                          \<le> cmod y / 6" .
+          } 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 **: "\<And>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)
+                \<le> (B*(norm y /24/C/B)*2*C + (2*C)*(norm y/24/C)) * content (cbox 0 (1::real))"
+            apply (rule has_integral_bound
+                    [of _ "\<lambda>x. f(linepath (shrink u) (shrink v) x) * (shrink v - shrink u) - f(linepath u v x)*(v - u)"
+                        _ 0 1])
+            using ynz \<open>0 < B\<close> \<open>0 < C\<close>
+              apply (simp_all del: le_divide_eq_numeral1)
+            apply (simp add: has_integral_diff has_contour_integral_linepath [symmetric] has_contour_integral_integral
+                fpi_uv f_uv contour_integrable_continuous_linepath)
+            apply (auto simp: ** norm_triangle_le norm_mult cmod_diff_le simp del: le_divide_eq_numeral1)
+            done
+          also have "\<dots> \<le> norm y / 6"
+            by simp
+          finally have "norm (?pathint (shrink u) (shrink v) - ?pathint u v) \<le> norm y / 6" .
+          } note * = this
+          have "norm (?pathint (shrink a) (shrink b) - ?pathint a b) \<le> 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) \<le> 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) \<le> 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))
+                \<le> norm y / 6 + norm y / 6 + norm y / 6"
+            by (metis norm_triangle_le add_mono)
+          also have "\<dots> = 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))
+                \<le> norm y / 2"
+            by (simp add: algebra_simps)
+          then
+          have "norm(?pathint a b + ?pathint b c + ?pathint c a) \<le> 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
+        }
+        note * = this
+        have "uniformly_continuous_on (convex hull {a,b,c}) f"
+          by (simp add: contf compact_convex_hull compact_uniformly_continuous)
+        moreover have "norm y / (24 * C) > 0"
+          using ynz \<open>C > 0\<close> by auto
+        ultimately obtain \<delta> where "\<delta> > 0" and
+          "\<forall>x\<in>convex hull {a, b, c}. \<forall>x'\<in>convex hull {a, b, c}.
+             dist x' x < \<delta> \<longrightarrow> dist (f x') (f x) < cmod y / (24 * C)"
+          using \<open>C > 0\<close> ynz unfolding uniformly_continuous_on_def dist_norm by blast
+        hence False using *[of \<delta>] by (auto simp: 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\<^marker>\<open>tag unimportant\<close> \<open>Version allowing finite number of exceptional points\<close>
+
+proposition\<^marker>\<open>tag unimportant\<close> Cauchy_theorem_triangle_cofinite:
+  assumes "continuous_on (convex hull {a,b,c}) f"
+      and "finite S"
+      and "(\<And>x. x \<in> interior(convex hull {a,b,c}) - S \<Longrightarrow> 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 \<notin> S'"
+      by (meson Set.set_insert all_not_in_conv)
+    then show ?thesis
+    proof (cases "d \<in> convex hull {a,b,c}")
+      case False
+      show "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
+      proof (rule less.hyps)
+        show "\<And>x. x \<in> interior (convex hull {a, b, c}) - S' \<Longrightarrow> f field_differentiable at x"
+        using False d interior_subset by (auto intro!: less.prems)
+    qed (use d less.prems in auto)
+    next
+      case True
+      have *: "convex hull {a, b, d} \<subseteq> 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)"
+      proof (rule less.hyps)
+        show "\<And>x. x \<in> interior (convex hull {a, b, d}) - S' \<Longrightarrow> f field_differentiable at x"
+          using d not_in_interior_convex_hull_3
+          by (clarsimp intro!: less.prems) (metis * insert_absorb insert_subset interior_mono)
+      qed (use d continuous_on_subset [OF  _ *] less.prems in auto)
+      have *: "convex hull {b, c, d} \<subseteq> 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)"
+      proof (rule less.hyps)
+        show "\<And>x. x \<in> interior (convex hull {b, c, d}) - S' \<Longrightarrow> f field_differentiable at x"
+          using d not_in_interior_convex_hull_3
+          by (clarsimp intro!: less.prems) (metis * insert_absorb insert_subset interior_mono)
+      qed (use d continuous_on_subset [OF  _ *] less.prems in auto)
+      have *: "convex hull {c, a, d} \<subseteq> 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)"
+      proof (rule less.hyps)
+        show "\<And>x. x \<in> interior (convex hull {c, a, d}) - S' \<Longrightarrow> f field_differentiable at x"
+          using d not_in_interior_convex_hull_3
+          by (clarsimp intro!: less.prems) (metis * insert_absorb insert_subset interior_mono)
+      qed (use d continuous_on_subset [OF  _ *] less.prems in auto)
+      have "f contour_integrable_on linepath a b"
+        using less.prems abd contour_integrable_joinD1 contour_integrable_on_def by blast
+      moreover have "f contour_integrable_on linepath b c"
+        using less.prems bcd contour_integrable_joinD1 contour_integrable_on_def by blast
+      moreover have "f contour_integrable_on linepath c a"
+        using less.prems cad contour_integrable_joinD1 contour_integrable_on_def by blast
+      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 \<noteq> 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\<^marker>\<open>tag unimportant\<close> \<open>Cauchy's theorem for an open starlike set\<close>
+
+lemma starlike_convex_subset:
+  assumes S: "a \<in> S" "closed_segment b c \<subseteq> S" and subs: "\<And>x. x \<in> S \<Longrightarrow> closed_segment a x \<subseteq> S"
+    shows "convex hull {a,b,c} \<subseteq> 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 \<in> S" "open S"
+      and x: "x \<in> S"
+      and subs: "\<And>y. y \<in> S \<Longrightarrow> closed_segment a y \<subseteq> S"
+      and zer: "\<And>b c. closed_segment b c \<subseteq> S
+                   \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f +
+                       contour_integral (linepath c a) f = 0"
+    shows "((\<lambda>x. contour_integral(linepath a x) f) has_field_derivative f x) (at x)"
+proof -
+  let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
+  { fix e y
+    assume e: "0 < e" and bxe: "ball x e \<subseteq> S" and close: "cmod (y - x) < e"
+    have y: "y \<in> 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 \<subseteq> 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: "\<And>y. cmod (y - x) < d1 \<Longrightarrow> 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 \<subseteq> S" using  \<open>open S\<close> x
+      by (auto simp: open_contains_ball)
+    have dpos: "min d1 d2 > 0" using d1 d2 by simp
+    { fix y
+      assume yx: "y \<noteq> x" and close: "cmod (y - x) < min d1 d2"
+      have y: "y \<in> S"
+        using d2 close  by (force simp: dist_norm norm_minus_commute)
+      have "closed_segment x y \<subseteq> S"
+        using close d2  by (auto simp: dist_norm norm_minus_commute dest!: segment_bound(1))
+      then have fxy: "f contour_integrable_on linepath x y"
+        by (metis 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 "((\<lambda>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)) \<le> e / 2 * cmod (y - x)"
+      proof (rule has_contour_integral_bound_linepath)
+        show "\<And>u. u \<in> closed_segment x y \<Longrightarrow> cmod (f u - f x) \<le> e / 2"
+          by (meson close d1_less le_less_trans less_imp_le min.strict_boundedE segment_bound1)
+      qed (use e in simp)
+      also have "\<dots> < 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 "\<exists>d>0. \<forall>y. y \<noteq> x \<and> cmod (y-x) < d \<longrightarrow> cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
+      using dpos by blast
+  }
+  then have *: "(\<lambda>y. (?pathint x y - f x * (y - x)) /\<^sub>R cmod (y - x)) \<midarrow>x\<rightarrow> 0"
+    by (simp add: Lim_at dist_norm inverse_eq_divide)
+  show ?thesis
+    apply (simp add: has_field_derivative_def has_derivative_at2 bounded_linear_mult_right)
+    apply (rule Lim_transform [OF * tendsto_eventually])
+    using \<open>open S\<close> x apply (force simp: dist_norm open_contains_ball inverse_eq_divide [symmetric] eventually_at)
+    done
+qed
+
+(** Existence of a primitive.*)
+lemma holomorphic_starlike_primitive:
+  fixes f :: "complex \<Rightarrow> complex"
+  assumes contf: "continuous_on S f"
+      and S: "starlike S" and os: "open S"
+      and k: "finite k"
+      and fcd: "\<And>x. x \<in> S - k \<Longrightarrow> f field_differentiable at x"
+    shows "\<exists>g. \<forall>x \<in> S. (g has_field_derivative f x) (at x)"
+proof -
+  obtain a where a: "a\<in>S" and a_cs: "\<And>x. x\<in>S \<Longrightarrow> closed_segment a x \<subseteq> S"
+    using S by (auto simp: starlike_def)
+  { fix x b c
+    assume "x \<in> S" "closed_segment b c \<subseteq> S"
+    then have abcs: "convex hull {a, b, c} \<subseteq> 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])
+    then have "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
+      using abcs interior_subset by (force intro: fcd Cauchy_theorem_triangle_cofinite [OF _ k])
+  } 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:
+ "\<lbrakk>open S; starlike S; finite k; continuous_on S f;
+   \<And>x. x \<in> S - k \<Longrightarrow> f field_differentiable at x;
+   valid_path g; path_image g \<subseteq> S; pathfinish g = pathstart g\<rbrakk>
+   \<Longrightarrow> (f has_contour_integral 0)  g"
+  by (metis holomorphic_starlike_primitive Cauchy_theorem_primitive at_within_open)
+
+lemma Cauchy_theorem_starlike_simple:
+  "\<lbrakk>open S; starlike S; f holomorphic_on S; valid_path g; path_image g \<subseteq> S; pathfinish g = pathstart g\<rbrakk>
+   \<Longrightarrow> (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\<open>Cauchy's theorem for a convex set\<close>
+
+text\<open>For a convex set we can avoid assuming openness and boundary analyticity\<close>
+
+lemma triangle_contour_integrals_convex_primitive:
+  assumes contf: "continuous_on S f"
+      and S: "a \<in> S" "convex S"
+      and x: "x \<in> S"
+      and zer: "\<And>b c. \<lbrakk>b \<in> S; c \<in> S\<rbrakk>
+                   \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f +
+                       contour_integral (linepath c a) f = 0"
+    shows "((\<lambda>x. contour_integral(linepath a x) f) has_field_derivative f x) (at x within S)"
+proof -
+  let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
+  { fix y
+    assume y: "y \<in> 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 \<subseteq> 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: "\<And>y. \<lbrakk>y \<in> S; cmod (y - x) < d1\<rbrakk> \<Longrightarrow> 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 \<noteq> x" and close: "cmod (y - x) < d1" and y: "y \<in> 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 "((\<lambda>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)) \<le> e / 2 * cmod (y - x)"
+      proof (rule has_contour_integral_bound_linepath)
+        show "\<And>u. u \<in> closed_segment x y \<Longrightarrow> cmod (f u - f x) \<le> e / 2"
+          by (meson assms(3) close convex_contains_segment d1_less le_less_trans less_imp_le segment_bound1 subset_iff x y)
+      qed (use e in simp)
+      also have "\<dots> < 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 "\<exists>d>0. \<forall>y\<in>S. y \<noteq> x \<and> cmod (y-x) < d \<longrightarrow> cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
+      using d1 by blast
+  }
+  then have *: "((\<lambda>y. (contour_integral (linepath x y) f - f x * (y - x)) /\<^sub>R cmod (y - x)) \<longlongrightarrow> 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 * tendsto_eventually])
+    using linordered_field_no_ub
+    apply (force simp: inverse_eq_divide [symmetric] eventually_at)
+    done
+qed
+
+lemma contour_integral_convex_primitive:
+  assumes "convex S" "continuous_on S f"
+          "\<And>a b c. \<lbrakk>a \<in> S; b \<in> S; c \<in> S\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
+  obtains g where "\<And>x. x \<in> S \<Longrightarrow> (g has_field_derivative f x) (at x within S)"
+proof (cases "S={}")
+  case False
+  with assms that show ?thesis
+    by (blast intro: triangle_contour_integrals_convex_primitive has_chain_integral_chain_integral3)
+qed auto
+
+lemma holomorphic_convex_primitive:
+  fixes f :: "complex \<Rightarrow> complex"
+  assumes "convex S" "finite K" and contf: "continuous_on S f"
+    and fd: "\<And>x. x \<in> interior S - K \<Longrightarrow> f field_differentiable at x"
+  obtains g where "\<And>x. x \<in> S \<Longrightarrow> (g has_field_derivative f x) (at x within S)"
+proof (rule contour_integral_convex_primitive [OF \<open>convex S\<close> contf Cauchy_theorem_triangle_cofinite])
+  have *: "convex hull {a, b, c} \<subseteq> S" if "a \<in> S" "b \<in> S" "c \<in> S" for a b c
+    by (simp add: \<open>convex S\<close> hull_minimal that)
+  show "continuous_on (convex hull {a, b, c}) f" if "a \<in> S" "b \<in> S" "c \<in> S" for a b c
+    by (meson "*" contf continuous_on_subset that)
+  show "f field_differentiable at x" if "a \<in> S" "b \<in> S" "c \<in> S" "x \<in> interior (convex hull {a, b, c}) - K" for a b c x
+    by (metis "*" DiffD1 DiffD2 DiffI fd interior_mono subsetCE that)
+qed (use assms in \<open>force+\<close>)
+
+lemma holomorphic_convex_primitive':
+  fixes f :: "complex \<Rightarrow> complex"
+  assumes "convex S" and "open S" and "f holomorphic_on S"
+  obtains g where "\<And>x. x \<in> S \<Longrightarrow> (g has_field_derivative f x) (at x within S)"
+proof (rule holomorphic_convex_primitive)
+  fix x assume "x \<in> interior S - {}"
+  with assms show "f field_differentiable at x"
+    by (auto intro!: holomorphic_on_imp_differentiable_at simp: interior_open)
+qed (use assms in \<open>auto intro: holomorphic_on_imp_continuous_on\<close>)
+
+corollary\<^marker>\<open>tag unimportant\<close> Cauchy_theorem_convex:
+    "\<lbrakk>continuous_on S f; convex S; finite K;
+      \<And>x. x \<in> interior S - K \<Longrightarrow> f field_differentiable at x;
+      valid_path g; path_image g \<subseteq> S; pathfinish g = pathstart g\<rbrakk>
+     \<Longrightarrow> (f has_contour_integral 0) g"
+  by (metis holomorphic_convex_primitive Cauchy_theorem_primitive)
+
+corollary Cauchy_theorem_convex_simple:
+    "\<lbrakk>f holomorphic_on S; convex S;
+     valid_path g; path_image g \<subseteq> S;
+     pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
+  apply (rule Cauchy_theorem_convex [where K = "{}"])
+  apply (simp_all add: holomorphic_on_imp_continuous_on)
+  using at_within_interior holomorphic_on_def interior_subset by fastforce
+
+text\<open>In particular for a disc\<close>
+corollary\<^marker>\<open>tag unimportant\<close> Cauchy_theorem_disc:
+    "\<lbrakk>finite K; continuous_on (cball a e) f;
+      \<And>x. x \<in> ball a e - K \<Longrightarrow> f field_differentiable at x;
+     valid_path g; path_image g \<subseteq> cball a e;
+     pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
+  by (auto intro: Cauchy_theorem_convex)
+
+corollary\<^marker>\<open>tag unimportant\<close> Cauchy_theorem_disc_simple:
+    "\<lbrakk>f holomorphic_on (ball a e); valid_path g; path_image g \<subseteq> ball a e;
+     pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
+by (simp add: Cauchy_theorem_convex_simple)
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Generalize integrability to local primitives\<close>
+
+lemma contour_integral_local_primitive_lemma:
+  fixes f :: "complex\<Rightarrow>complex"
+  shows
+    "\<lbrakk>g piecewise_differentiable_on {a..b};
+      \<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s);
+      \<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s\<rbrakk>
+     \<Longrightarrow> (\<lambda>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 \<Rightarrow> complex"
+  assumes gpd: "g piecewise_differentiable_on {a..b}"
+      and dh: "\<And>x. x \<in> s
+               \<Longrightarrow> \<exists>d h. 0 < d \<and>
+                         (\<forall>y. norm(y - x) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
+      and gs: "\<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s"
+  shows "(\<lambda>x. f(g x) * vector_derivative g (at x)) integrable_on {a..b}"
+proof -
+  { fix x
+    assume x: "a \<le> x" "x \<le> b"
+    obtain d h where d: "0 < d"
+               and h: "(\<And>y. norm(y - g x) < d \<Longrightarrow> (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: "\<And>x'. x' \<in> {a..b} \<Longrightarrow> \<bar>x' - x\<bar> < e \<Longrightarrow> 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 "\<exists>d>0. \<forall>u v. u \<le> x \<and> x \<le> v \<and> {u..v} \<subseteq> ball x d \<and> (u \<le> v \<longrightarrow> a \<le> u \<and> v \<le> b) \<longrightarrow>
+                          (\<lambda>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 \<Rightarrow> complex"
+  assumes g: "valid_path g" "path_image g \<subseteq> s"
+      and dh: "\<And>x. x \<in> s
+               \<Longrightarrow> \<exists>d h. 0 < d \<and>
+                         (\<forall>y. norm(y - x) < d \<longrightarrow> (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\<open>In particular if a function is holomorphic\<close>
+
+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 \<subseteq> s"
+      and fcd: "\<And>x. x \<in> s - k \<Longrightarrow> f field_differentiable at x"
+    shows "f contour_integrable_on g"
+proof -
+  { fix z
+    assume z: "z \<in> s"
+    obtain d where "d>0" and d: "ball z d \<subseteq> s" using  \<open>open s\<close> 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 "\<forall>y\<in>ball z d. (h has_field_derivative f y) (at y within ball z d)"
+      by (metis holomorphic_convex_primitive [OF convex_ball k contfb fcd] d interior_subset Diff_iff subsetD)
+    then have "\<forall>y\<in>ball z d. (h has_field_derivative f y) (at y within s)"
+      by (metis open_ball at_within_open d os subsetCE)
+    then have "\<exists>h. (\<forall>y. cmod (y - z) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
+      by (force simp: dist_norm norm_minus_commute)
+    then have "\<exists>d h. 0 < d \<and> (\<forall>y. cmod (y - z) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
+      using \<open>0 < d\<close> 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 \<subseteq> 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 \<notin> S \<Longrightarrow> continuous_on S (\<lambda>w. 1 / (w - z))"
+  by (rule continuous_intros | force)+
+
+lemma contour_integrable_inversediff:
+    "\<lbrakk>valid_path g; z \<notin> path_image g\<rbrakk> \<Longrightarrow> (\<lambda>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\<open>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.
+\<close>
+
+text\<open>A technical definition to avoid duplication of similar proofs,
+     for paths joined at the ends versus looping paths\<close>
+definition linked_paths :: "bool \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> (real \<Rightarrow> 'a::topological_space) \<Rightarrow> bool"
+  where "linked_paths atends g h ==
+        (if atends then pathstart h = pathstart g \<and> pathfinish h = pathfinish g
+                   else pathfinish g = pathstart g \<and> pathfinish h = pathstart h)"
+
+text\<open>This formulation covers two cases: \<^term>\<open>g\<close> and \<^term>\<open>h\<close> share their
+      start and end points; \<^term>\<open>g\<close> and \<^term>\<open>h\<close> both loop upon themselves.\<close>
+lemma contour_integral_nearby:
+  assumes os: "open S" and p: "path p" "path_image p \<subseteq> S"
+  shows "\<exists>d. 0 < d \<and>
+            (\<forall>g h. valid_path g \<and> valid_path h \<and>
+                  (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
+                  linked_paths atends g h
+                  \<longrightarrow> path_image g \<subseteq> S \<and> path_image h \<subseteq> S \<and>
+                      (\<forall>f. f holomorphic_on S \<longrightarrow> contour_integral h f = contour_integral g f))"
+proof -
+  have "\<forall>z. \<exists>e. z \<in> path_image p \<longrightarrow> 0 < e \<and> ball z e \<subseteq> S"
+    using open_contains_ball os p(2) by blast
+  then obtain ee where ee: "\<And>z. z \<in> path_image p \<Longrightarrow> 0 < ee z \<and> ball z (ee z) \<subseteq> S"
+    by metis
+  define cover where "cover = (\<lambda>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 \<subseteq> (\<Union>c\<in>path_image p. ball c (ee c / 3))"
+    using ee by auto
+  ultimately have "\<exists>D \<subseteq> cover. finite D \<and> path_image p \<subseteq> \<Union>D"
+    by (simp add: compact_eq_Heine_Borel cover_def)
+  then obtain D where D: "D \<subseteq> cover" "finite D" "path_image p \<subseteq> \<Union>D"
+    by blast
+  then obtain k where k: "k \<subseteq> {0..1}" "finite k" and D_eq: "D = ((\<lambda>z. ball z (ee z / 3)) \<circ> p) ` k"
+    apply (simp add: cover_def path_image_def image_comp)
+    apply (blast dest!: finite_subset_image [OF \<open>finite D\<close>])
+    done
+  then have kne: "k \<noteq> {}"
+    using D by auto
+  have pi: "\<And>i. i \<in> k \<Longrightarrow> p i \<in> path_image p"
+    using k  by (auto simp: path_image_def)
+  then have eepi: "\<And>i. i \<in> k \<Longrightarrow> 0 < ee((p i))"
+    by (metis ee)
+  define e where "e = Min((ee \<circ> p) ` k)"
+  have fin_eep: "finite ((ee \<circ> p) ` k)"
+    using k  by blast
+  have "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: "\<And>x x'. \<bar>x' - x\<bar> < d \<Longrightarrow> x\<in>{0..1} \<Longrightarrow> x'\<in>{0..1} \<Longrightarrow> cmod (p x' - p x) < e/3"
+    unfolding uniformly_continuous_on_def dist_norm real_norm_def
+    by (metis divide_pos_pos \<open>0 < e\<close> zero_less_numeral)
+  then obtain N::nat where N: "N>0" "inverse N < d"
+    using real_arch_inverse [of d]   by auto
+  show ?thesis
+  proof (intro exI conjI allI; clarify?)
+    show "e/3 > 0"
+      using \<open>0 < e\<close> by simp
+    fix g h
+    assume g: "valid_path g" and ghp: "\<forall>t\<in>{0..1}. cmod (g t - p t) < e / 3 \<and>  cmod (h t - p t) < e / 3"
+       and h: "valid_path h"
+       and joins: "linked_paths atends g h"
+    { fix t::real
+      assume t: "0 \<le> t" "t \<le> 1"
+      then obtain u where u: "u \<in> k" and ptu: "p t \<in> ball(p u) (ee(p u) / 3)"
+        using \<open>path_image p \<subseteq> \<Union>D\<close> D_eq by (force simp: path_image_def)
+      then have ele: "e \<le> 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 ghp 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 \<in> ball(p u) (ee(p u))"  "h t \<in> 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 \<in> S" "h t \<in> S" using ee u k
+        by (auto simp: path_image_def ball_def)
+    }
+    then have ghs: "path_image g \<subseteq> S" "path_image h \<subseteq> 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 \<in> path_image p"
+        have "f holomorphic_on ball z (ee z)"
+          using fhols ee z holomorphic_on_subset by blast
+        then have "\<exists>ff. (\<forall>w \<in> 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:
+            "\<And>z w. \<lbrakk>z \<in> path_image p; w \<in> ball z (ee z)\<rbrakk> \<Longrightarrow> (ff z has_field_derivative f w) (at w)"
+        by metis
+      { fix n
+        assume n: "n \<le> 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 \<in> k" and "p (n/N) \<in> ball(p t) (ee(p t) / 3)"
+            using \<open>path_image p \<subseteq> \<Union>D\<close> [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 \<le> ee (p t) / 3"  using fin_eep t
+            by (simp add: e_def)
+          { fix x
+            assume x: "n/N \<le> x" "x \<le> (1 + n)/N"
+            then have nN01: "0 \<le> n/N" "(1 + n)/N \<le> 1"
+              using Suc.prems by auto
+            then have x01: "0 \<le> x" "x \<le> 1"
+              using x by linarith+
+            have "cmod (p t - p x)  < ee (p t) / 3 + e/3"
+            proof (rule norm_diff_triangle_less [OF ptu de])
+              show "\<bar>real n / real N - x\<bar> < d"
+                using x N by (auto simp: field_simps)
+            qed (use x01 Suc.prems in auto)
+            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 ghp x01 by (simp add: norm_minus_commute)
+            also have "\<dots> \<le> 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 ghp x01 by (simp add: norm_minus_commute)
+            also have "\<dots> \<le> 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 "closed_segment (g (real n / real N)) (h (real n / real N)) = path_image (linepath (h (n/N)) (g (n/N)))"
+            by (simp add: closed_segment_commute)
+          also have pi_hgn: "\<dots> \<subseteq> 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"])
+          finally have gh_ns: "closed_segment (g (n/N)) (h (n/N)) \<subseteq> S"
+            using ee pi t by blast
+          have pi_ghn': "path_image (linepath (g ((1 + n) / N)) (h ((1 + n) / N))) \<subseteq> 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)) \<subseteq> S"
+            using \<open>N>0\<close> 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)))
+                 \<subseteq> ball (p t) (ee (p t))"
+            apply (intro subset_path_image_join pi_hgn pi_ghn')
+            using \<open>N>0\<close> 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)
+            using Suc.prems pi_subset_ball apply (simp_all add: valid_path_join 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 *: "\<And>hn he hn' gn gd gn' hgn ghn gh0 ghn'.
+                    \<lbrakk>hn - gn = ghn - gh0;
+                     gd + ghn' + he + hgn = (0::complex);
+                     hn - he = hn'; gn + gd = gn'; hgn = -ghn\<rbrakk> \<Longrightarrow> 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 "\<dots> = 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
+    show "path_image g \<subseteq> S \<and> path_image h \<subseteq> S \<and> (\<forall>f. f holomorphic_on S \<longrightarrow> contour_integral h f = contour_integral g f)"
+      by metis
+  qed
+qed
+
+
+lemma
+  assumes "open S" "path p" "path_image p \<subseteq> S"
+    shows contour_integral_nearby_ends:
+      "\<exists>d. 0 < d \<and>
+              (\<forall>g h. valid_path g \<and> valid_path h \<and>
+                    (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
+                    pathstart h = pathstart g \<and> pathfinish h = pathfinish g
+                    \<longrightarrow> path_image g \<subseteq> S \<and>
+                        path_image h \<subseteq> S \<and>
+                        (\<forall>f. f holomorphic_on S
+                            \<longrightarrow> contour_integral h f = contour_integral g f))"
+    and contour_integral_nearby_loops:
+      "\<exists>d. 0 < d \<and>
+              (\<forall>g h. valid_path g \<and> valid_path h \<and>
+                    (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
+                    pathfinish g = pathstart g \<and> pathfinish h = pathstart h
+                    \<longrightarrow> path_image g \<subseteq> S \<and>
+                        path_image h \<subseteq> S \<and>
+                        (\<forall>f. f holomorphic_on S
+                            \<longrightarrow> 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
+
+lemma C1_differentiable_polynomial_function:
+  fixes p :: "real \<Rightarrow> 'a::euclidean_space"
+  shows "polynomial_function p \<Longrightarrow> 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 \<Rightarrow> 'a::euclidean_space"
+  shows "polynomial_function p \<Longrightarrow> 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 \<Rightarrow> 'a::euclidean_space"
+    shows "z \<noteq> g x \<Longrightarrow> 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 \<subseteq> S"
+  shows "\<exists>L. 0 < L \<and>
+             (\<forall>f B. f holomorphic_on S \<and> (\<forall>z \<in> S. norm(f z) \<le> B)
+               \<longrightarrow> norm(contour_integral g f) \<le> 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 \<subseteq> S"
+      and pi: "\<And>f. f holomorphic_on S \<Longrightarrow> contour_integral g f = contour_integral p f"
+    using contour_integral_nearby_ends [OF S \<open>path g\<close> 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'"
+    "\<And>x. (p has_vector_derivative (p' x)) (at x)"
+    by (blast intro: has_vector_derivative_polynomial_function that)
+  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': "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> norm (p' x) \<le> L"
+    by (force simp: bounded_pos)
+  { fix f B
+    assume f: "f holomorphic_on S" and B: "\<And>z. z\<in>S \<Longrightarrow> cmod (f z) \<le> B"
+    then have "f contour_integrable_on p \<and> valid_path p"
+      using p S
+      by (blast intro: valid_path_polynomial_function contour_integrable_holomorphic_simple holomorphic_on_imp_continuous_on)
+    moreover have "cmod (vector_derivative p (at x)) * cmod (f (p x)) \<le> L * B" if "0 \<le> x" "x \<le> 1" for x
+    proof (rule mult_mono)
+      show "cmod (vector_derivative p (at x)) \<le> L"
+        by (metis nop' p'(2) that vector_derivative_at)
+      show "cmod (f (p x)) \<le> B"
+        by (metis B atLeastAtMost_iff imageI p(2) path_defs(4) subset_eq that)
+    qed (use \<open>L>0\<close> in auto)
+    ultimately have "cmod (contour_integral g f) \<le> L * B"
+      apply (simp only: pi [OF f])
+      apply (simp only: 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 using \<open>L > 0\<close>
+    by (intro exI[of _ L]) auto
+qed
+
+text\<open>We can treat even non-rectifiable paths as having a "length" for bounds on analytic functions in open sets.\<close>
+
+subsection \<open>Winding Numbers\<close>
+
+definition\<^marker>\<open>tag important\<close> winding_number_prop :: "[real \<Rightarrow> complex, complex, real, real \<Rightarrow> complex, complex] \<Rightarrow> bool" where
+  "winding_number_prop \<gamma> z e p n \<equiv>
+      valid_path p \<and> z \<notin> path_image p \<and>
+      pathstart p = pathstart \<gamma> \<and>
+      pathfinish p = pathfinish \<gamma> \<and>
+      (\<forall>t \<in> {0..1}. norm(\<gamma> t - p t) < e) \<and>
+      contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
+
+definition\<^marker>\<open>tag important\<close> winding_number:: "[real \<Rightarrow> complex, complex] \<Rightarrow> complex" where
+  "winding_number \<gamma> z \<equiv> SOME n. \<forall>e > 0. \<exists>p. winding_number_prop \<gamma> z e p n"
+
+
+lemma winding_number:
+  assumes "path \<gamma>" "z \<notin> path_image \<gamma>" "0 < e"
+    shows "\<exists>p. winding_number_prop \<gamma> z e p (winding_number \<gamma> z)"
+proof -
+  have "path_image \<gamma> \<subseteq> UNIV - {z}"
+    using assms by blast
+  then obtain d
+    where d: "d>0"
+      and pi_eq: "\<And>h1 h2. valid_path h1 \<and> valid_path h2 \<and>
+                    (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < d \<and> cmod (h2 t - \<gamma> t) < d) \<and>
+                    pathstart h2 = pathstart h1 \<and> pathfinish h2 = pathfinish h1 \<longrightarrow>
+                      path_image h1 \<subseteq> UNIV - {z} \<and> path_image h2 \<subseteq> UNIV - {z} \<and>
+                      (\<forall>f. f holomorphic_on UNIV - {z} \<longrightarrow> contour_integral h2 f = contour_integral h1 f)"
+    using contour_integral_nearby_ends [of "UNIV - {z}" \<gamma>] assms by (auto simp: open_delete)
+  then obtain h where h: "polynomial_function h \<and> pathstart h = pathstart \<gamma> \<and> pathfinish h = pathfinish \<gamma> \<and>
+                          (\<forall>t \<in> {0..1}. norm(h t - \<gamma> t) < d/2)"
+    using path_approx_polynomial_function [OF \<open>path \<gamma>\<close>, of "d/2"] d by auto
+  define nn where "nn = 1/(2* pi*\<i>) * contour_integral h (\<lambda>w. 1/(w - z))"
+  have "\<exists>n. \<forall>e > 0. \<exists>p. winding_number_prop \<gamma> z e p n"
+    proof (rule_tac x=nn in exI, clarify)
+      fix e::real
+      assume e: "e>0"
+      obtain p where p: "polynomial_function p \<and>
+            pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and> (\<forall>t\<in>{0..1}. cmod (p t - \<gamma> t) < min e (d/2))"
+        using path_approx_polynomial_function [OF \<open>path \<gamma>\<close>, of "min e (d/2)"] d \<open>0<e\<close> by auto
+      have "(\<lambda>w. 1 / (w - z)) holomorphic_on UNIV - {z}"
+        by (auto simp: intro!: holomorphic_intros)
+      then show "\<exists>p. winding_number_prop \<gamma> z e p 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 winding_number_prop_def)
+        done
+    qed
+  then show ?thesis
+    unfolding winding_number_def by (rule someI2_ex) (blast intro: \<open>0<e\<close>)
+qed
+
+lemma winding_number_unique:
+  assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
+      and pi: "\<And>e. e>0 \<Longrightarrow> \<exists>p. winding_number_prop \<gamma> z e p n"
+   shows "winding_number \<gamma> z = n"
+proof -
+  have "path_image \<gamma> \<subseteq> UNIV - {z}"
+    using assms by blast
+  then obtain e
+    where e: "e>0"
+      and pi_eq: "\<And>h1 h2 f. \<lbrakk>valid_path h1; valid_path h2;
+                    (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < e \<and> cmod (h2 t - \<gamma> t) < e);
+                    pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1; f holomorphic_on UNIV - {z}\<rbrakk> \<Longrightarrow>
+                    contour_integral h2 f = contour_integral h1 f"
+    using contour_integral_nearby_ends [of "UNIV - {z}" \<gamma>] assms  by (auto simp: open_delete)
+  obtain p where p: "winding_number_prop \<gamma> z e p n"
+    using pi [OF e] by blast
+  obtain q where q: "winding_number_prop \<gamma> z e q (winding_number \<gamma> z)"
+    using winding_number [OF \<gamma> e] by blast
+  have "2 * complex_of_real pi * \<i> * n = contour_integral p (\<lambda>w. 1 / (w - z))"
+    using p by (auto simp: winding_number_prop_def)
+  also have "\<dots> = contour_integral q (\<lambda>w. 1 / (w - z))"
+  proof (rule pi_eq)
+    show "(\<lambda>w. 1 / (w - z)) holomorphic_on UNIV - {z}"
+      by (auto intro!: holomorphic_intros)
+  qed (use p q in \<open>auto simp: winding_number_prop_def norm_minus_commute\<close>)
+  also have "\<dots> = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z"
+    using q by (auto simp: winding_number_prop_def)
+  finally have "2 * complex_of_real pi * \<i> * n = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z" .
+  then show ?thesis
+    by simp
+qed
+
+(*NB not winding_number_prop here due to the loop in p*)
+lemma winding_number_unique_loop:
+  assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
+      and loop: "pathfinish \<gamma> = pathstart \<gamma>"
+      and pi:
+        "\<And>e. e>0 \<Longrightarrow> \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
+                           pathfinish p = pathstart p \<and>
+                           (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
+                           contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
+   shows "winding_number \<gamma> z = n"
+proof -
+  have "path_image \<gamma> \<subseteq> UNIV - {z}"
+    using assms by blast
+  then obtain e
+    where e: "e>0"
+      and pi_eq: "\<And>h1 h2 f. \<lbrakk>valid_path h1; valid_path h2;
+                    (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < e \<and> cmod (h2 t - \<gamma> t) < e);
+                    pathfinish h1 = pathstart h1; pathfinish h2 = pathstart h2; f holomorphic_on UNIV - {z}\<rbrakk> \<Longrightarrow>
+                    contour_integral h2 f = contour_integral h1 f"
+    using contour_integral_nearby_loops [of "UNIV - {z}" \<gamma>] assms  by (auto simp: open_delete)
+  obtain p where p:
+     "valid_path p \<and> z \<notin> path_image p \<and> pathfinish p = pathstart p \<and>
+      (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
+      contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
+    using pi [OF e] by blast
+  obtain q where q: "winding_number_prop \<gamma> z e q (winding_number \<gamma> z)"
+    using winding_number [OF \<gamma> e] by blast
+  have "2 * complex_of_real pi * \<i> * n = contour_integral p (\<lambda>w. 1 / (w - z))"
+    using p by auto
+  also have "\<dots> = contour_integral q (\<lambda>w. 1 / (w - z))"
+  proof (rule pi_eq)
+    show "(\<lambda>w. 1 / (w - z)) holomorphic_on UNIV - {z}"
+      by (auto intro!: holomorphic_intros)
+  qed (use p q loop in \<open>auto simp: winding_number_prop_def norm_minus_commute\<close>)
+  also have "\<dots> = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z"
+    using q by (auto simp: winding_number_prop_def)
+  finally have "2 * complex_of_real pi * \<i> * n = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z" .
+  then show ?thesis
+    by simp
+qed
+
+proposition winding_number_valid_path:
+  assumes "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
+  shows "winding_number \<gamma> z = 1/(2*pi*\<i>) * contour_integral \<gamma> (\<lambda>w. 1/(w - z))"
+  by (rule winding_number_unique)
+  (use assms in \<open>auto simp: valid_path_imp_path winding_number_prop_def\<close>)
+
+proposition has_contour_integral_winding_number:
+  assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
+    shows "((\<lambda>w. 1/(w - z)) has_contour_integral (2*pi*\<i>*winding_number \<gamma> z)) \<gamma>"
+by (simp add: winding_number_valid_path has_contour_integral_integral contour_integrable_inversediff assms)
+
+lemma winding_number_trivial [simp]: "z \<noteq> a \<Longrightarrow> winding_number(linepath a a) z = 0"
+  by (simp add: winding_number_valid_path)
+
+lemma winding_number_subpath_trivial [simp]: "z \<noteq> g x \<Longrightarrow> winding_number (subpath x x g) z = 0"
+  by (simp add: path_image_subpath winding_number_valid_path)
+
+lemma winding_number_join:
+  assumes \<gamma>1: "path \<gamma>1" "z \<notin> path_image \<gamma>1"
+      and \<gamma>2: "path \<gamma>2" "z \<notin> path_image \<gamma>2"
+      and "pathfinish \<gamma>1 = pathstart \<gamma>2"
+    shows "winding_number(\<gamma>1 +++ \<gamma>2) z = winding_number \<gamma>1 z + winding_number \<gamma>2 z"
+proof (rule winding_number_unique)
+  show "\<exists>p. winding_number_prop (\<gamma>1 +++ \<gamma>2) z e p
+              (winding_number \<gamma>1 z + winding_number \<gamma>2 z)" if "e > 0" for e
+  proof -
+    obtain p1 where "winding_number_prop \<gamma>1 z e p1 (winding_number \<gamma>1 z)"
+      using \<open>0 < e\<close> \<gamma>1 winding_number by blast
+    moreover
+    obtain p2 where "winding_number_prop \<gamma>2 z e p2 (winding_number \<gamma>2 z)"
+      using \<open>0 < e\<close> \<gamma>2 winding_number by blast
+    ultimately
+    have "winding_number_prop (\<gamma>1+++\<gamma>2) z e (p1+++p2) (winding_number \<gamma>1 z + winding_number \<gamma>2 z)"
+      using assms
+      apply (simp add: winding_number_prop_def not_in_path_image_join contour_integrable_inversediff algebra_simps)
+      apply (auto simp: joinpaths_def)
+      done
+    then show ?thesis
+      by blast
+  qed
+qed (use assms in \<open>auto simp: not_in_path_image_join\<close>)
+
+lemma winding_number_reversepath:
+  assumes "path \<gamma>" "z \<notin> path_image \<gamma>"
+    shows "winding_number(reversepath \<gamma>) z = - (winding_number \<gamma> z)"
+proof (rule winding_number_unique)
+  show "\<exists>p. winding_number_prop (reversepath \<gamma>) z e p (- winding_number \<gamma> z)" if "e > 0" for e
+  proof -
+    obtain p where "winding_number_prop \<gamma> z e p (winding_number \<gamma> z)"
+      using \<open>0 < e\<close> assms winding_number by blast
+    then have "winding_number_prop (reversepath \<gamma>) z e (reversepath p) (- winding_number \<gamma> z)"
+      using assms
+      apply (simp add: winding_number_prop_def contour_integral_reversepath contour_integrable_inversediff valid_path_imp_reverse)
+      apply (auto simp: reversepath_def)
+      done
+    then show ?thesis
+      by blast
+  qed
+qed (use assms in auto)
+
+lemma winding_number_shiftpath:
+  assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
+      and "pathfinish \<gamma> = pathstart \<gamma>" "a \<in> {0..1}"
+    shows "winding_number(shiftpath a \<gamma>) z = winding_number \<gamma> z"
+proof (rule winding_number_unique_loop)
+  show "\<exists>p. valid_path p \<and> z \<notin> path_image p \<and> pathfinish p = pathstart p \<and>
+            (\<forall>t\<in>{0..1}. cmod (shiftpath a \<gamma> t - p t) < e) \<and>
+            contour_integral p (\<lambda>w. 1 / (w - z)) =
+            complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
+    if "e > 0" for e
+  proof -
+    obtain p where "winding_number_prop \<gamma> z e p (winding_number \<gamma> z)"
+      using \<open>0 < e\<close> assms winding_number by blast
+    then show ?thesis
+      apply (rule_tac x="shiftpath a p" in exI)
+      using assms that
+      apply (auto simp: winding_number_prop_def path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath contour_integral_shiftpath)
+      apply (simp add: shiftpath_def)
+      done
+  qed
+qed (use assms in \<open>auto simp: path_shiftpath path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath\<close>)
+
+lemma winding_number_split_linepath:
+  assumes "c \<in> closed_segment a b" "z \<notin> 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 \<notin> closed_segment a c" "z \<notin> closed_segment c b"
+    using assms  by (meson convex_contains_segment convex_segment ends_in_segment 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:
+   "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> p t = q t) \<Longrightarrow> winding_number p z = winding_number q z"
+  by (simp add: winding_number_def winding_number_prop_def pathstart_def pathfinish_def)
+
+lemma winding_number_constI:
+  assumes "c\<noteq>z" "\<And>t. \<lbrakk>0\<le>t; t\<le>1\<rbrakk> \<Longrightarrow> g t = c" 
+  shows "winding_number g z = 0"
+proof -
+  have "winding_number g z = winding_number (linepath c c) z"
+    apply (rule winding_number_cong)
+    using assms unfolding linepath_def by auto
+  moreover have "winding_number (linepath c c) z =0"
+    apply (rule winding_number_trivial)
+    using assms by auto
+  ultimately show ?thesis by auto
+qed
+
+lemma winding_number_offset: "winding_number p z = winding_number (\<lambda>w. p w - z) 0"
+  unfolding winding_number_def
+proof (intro ext arg_cong [where f = Eps] arg_cong [where f = All] imp_cong refl, safe)
+  fix n e g
+  assume "0 < e" and g: "winding_number_prop p z e g n"
+  then show "\<exists>r. winding_number_prop (\<lambda>w. p w - z) 0 e r n"
+    by (rule_tac x="\<lambda>t. g t - z" in exI)
+       (force simp: winding_number_prop_def contour_integral_integral valid_path_def path_defs
+                vector_derivative_def has_vector_derivative_diff_const piecewise_C1_differentiable_diff C1_differentiable_imp_piecewise)
+next
+  fix n e g
+  assume "0 < e" and g: "winding_number_prop (\<lambda>w. p w - z) 0 e g n"
+  then show "\<exists>r. winding_number_prop p z e r n"
+    apply (rule_tac x="\<lambda>t. g t + z" in exI)
+    apply (simp add: winding_number_prop_def contour_integral_integral valid_path_def path_defs
+        piecewise_C1_differentiable_add vector_derivative_def has_vector_derivative_add_const C1_differentiable_imp_piecewise)
+    apply (force simp: algebra_simps)
+    done
+qed
+
+subsubsection\<^marker>\<open>tag unimportant\<close> \<open>Some lemmas about negating a path\<close>
+
+lemma valid_path_negatepath: "valid_path \<gamma> \<Longrightarrow> valid_path (uminus \<circ> \<gamma>)"
+   unfolding o_def using piecewise_C1_differentiable_neg valid_path_def by blast
+
+lemma has_contour_integral_negatepath:
+  assumes \<gamma>: "valid_path \<gamma>" and cint: "((\<lambda>z. f (- z)) has_contour_integral - i) \<gamma>"
+  shows "(f has_contour_integral i) (uminus \<circ> \<gamma>)"
+proof -
+  obtain S where cont: "continuous_on {0..1} \<gamma>" and "finite S" and diff: "\<gamma> C1_differentiable_on {0..1} - S"
+    using \<gamma> by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
+  have "((\<lambda>x. - (f (- \<gamma> x) * vector_derivative \<gamma> (at x within {0..1}))) has_integral i) {0..1}"
+    using cint by (auto simp: has_contour_integral_def dest: has_integral_neg)
+  then
+  have "((\<lambda>x. f (- \<gamma> x) * vector_derivative (uminus \<circ> \<gamma>) (at x within {0..1})) has_integral i) {0..1}"
+  proof (rule rev_iffD1 [OF _ has_integral_spike_eq])
+    show "negligible S"
+      by (simp add: \<open>finite S\<close> negligible_finite)
+    show "f (- \<gamma> x) * vector_derivative (uminus \<circ> \<gamma>) (at x within {0..1}) =
+         - (f (- \<gamma> x) * vector_derivative \<gamma> (at x within {0..1}))"
+      if "x \<in> {0..1} - S" for x
+    proof -
+      have "vector_derivative (uminus \<circ> \<gamma>) (at x within cbox 0 1) = - vector_derivative \<gamma> (at x within cbox 0 1)"
+      proof (rule vector_derivative_within_cbox)
+        show "(uminus \<circ> \<gamma> has_vector_derivative - vector_derivative \<gamma> (at x within cbox 0 1)) (at x within cbox 0 1)"
+          using that unfolding o_def
+          by (metis C1_differentiable_on_eq UNIV_I diff differentiable_subset has_vector_derivative_minus subsetI that vector_derivative_works)
+      qed (use that in auto)
+      then show ?thesis
+        by simp
+    qed
+  qed
+  then show ?thesis by (simp add: has_contour_integral_def)
+qed
+
+lemma winding_number_negatepath:
+  assumes \<gamma>: "valid_path \<gamma>" and 0: "0 \<notin> path_image \<gamma>"
+  shows "winding_number(uminus \<circ> \<gamma>) 0 = winding_number \<gamma> 0"
+proof -
+  have "(/) 1 contour_integrable_on \<gamma>"
+    using "0" \<gamma> contour_integrable_inversediff by fastforce
+  then have "((\<lambda>z. 1/z) has_contour_integral contour_integral \<gamma> ((/) 1)) \<gamma>"
+    by (rule has_contour_integral_integral)
+  then have "((\<lambda>z. 1 / - z) has_contour_integral - contour_integral \<gamma> ((/) 1)) \<gamma>"
+    using has_contour_integral_neg by auto
+  then show ?thesis
+    using assms
+    apply (simp add: winding_number_valid_path valid_path_negatepath image_def path_defs)
+    apply (simp add: contour_integral_unique has_contour_integral_negatepath)
+    done
+qed
+
+lemma contour_integrable_negatepath:
+  assumes \<gamma>: "valid_path \<gamma>" and pi: "(\<lambda>z. f (- z)) contour_integrable_on \<gamma>"
+  shows "f contour_integrable_on (uminus \<circ> \<gamma>)"
+  by (metis \<gamma> add.inverse_inverse contour_integrable_on_def has_contour_integral_negatepath pi)
+
+(* A combined theorem deducing several things piecewise.*)
+lemma winding_number_join_pos_combined:
+     "\<lbrakk>valid_path \<gamma>1; z \<notin> path_image \<gamma>1; 0 < Re(winding_number \<gamma>1 z);
+       valid_path \<gamma>2; z \<notin> path_image \<gamma>2; 0 < Re(winding_number \<gamma>2 z); pathfinish \<gamma>1 = pathstart \<gamma>2\<rbrakk>
+      \<Longrightarrow> valid_path(\<gamma>1 +++ \<gamma>2) \<and> z \<notin> path_image(\<gamma>1 +++ \<gamma>2) \<and> 0 < Re(winding_number(\<gamma>1 +++ \<gamma>2) z)"
+  by (simp add: valid_path_join path_image_join winding_number_join valid_path_imp_path)
+
+
+subsubsection\<^marker>\<open>tag unimportant\<close> \<open>Useful sufficient conditions for the winding number to be positive\<close>
+
+lemma Re_winding_number:
+    "\<lbrakk>valid_path \<gamma>; z \<notin> path_image \<gamma>\<rbrakk>
+     \<Longrightarrow> Re(winding_number \<gamma> z) = Im(contour_integral \<gamma> (\<lambda>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 \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
+      and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> 0 \<le> Im (vector_derivative \<gamma> (at x) * cnj(\<gamma> x - z))"
+    shows "0 \<le> Re(winding_number \<gamma> z)"
+proof -
+  have ge0: "0 \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))" if x: "0 < x" "x < 1" for x
+    using ge by (simp add: Complex.Im_divide algebra_simps x)
+  let ?vd = "\<lambda>x. 1 / (\<gamma> x - z) * vector_derivative \<gamma> (at x)"
+  let ?int = "\<lambda>z. contour_integral \<gamma> (\<lambda>w. 1 / (w - z))"
+  have hi: "(?vd has_integral ?int z) (cbox 0 1)"
+    unfolding box_real
+    apply (subst has_contour_integral [symmetric])
+    using \<gamma> by (simp add: contour_integrable_inversediff has_contour_integral_integral)
+  have "0 \<le> Im (?int z)"
+  proof (rule has_integral_component_nonneg [of \<i>, simplified])
+    show "\<And>x. x \<in> cbox 0 1 \<Longrightarrow> 0 \<le> Im (if 0 < x \<and> x < 1 then ?vd x else 0)"
+      by (force simp: ge0)
+    show "((\<lambda>x. if 0 < x \<and> x < 1 then ?vd x else 0) has_integral ?int z) (cbox 0 1)"
+      by (rule has_integral_spike_interior [OF hi]) simp
+  qed
+  then show ?thesis
+    by (simp add: Re_winding_number [OF \<gamma>] field_simps)
+qed
+
+lemma winding_number_pos_lt_lemma:
+  assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
+      and e: "0 < e"
+      and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> e \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
+    shows "0 < Re(winding_number \<gamma> z)"
+proof -
+  let ?vd = "\<lambda>x. 1 / (\<gamma> x - z) * vector_derivative \<gamma> (at x)"
+  let ?int = "\<lambda>z. contour_integral \<gamma> (\<lambda>w. 1 / (w - z))"
+  have hi: "(?vd has_integral ?int z) (cbox 0 1)"
+    unfolding box_real
+    apply (subst has_contour_integral [symmetric])
+    using \<gamma> by (simp add: contour_integrable_inversediff has_contour_integral_integral)
+  have "e \<le> Im (contour_integral \<gamma> (\<lambda>w. 1 / (w - z)))"
+  proof (rule has_integral_component_le [of \<i> "\<lambda>x. \<i>*e" "\<i>*e" "{0..1}", simplified])
+    show "((\<lambda>x. if 0 < x \<and> x < 1 then ?vd x else \<i> * complex_of_real e) has_integral ?int z) {0..1}"
+      by (rule has_integral_spike_interior [OF hi, simplified box_real]) (use e in simp)
+    show "\<And>x. 0 \<le> x \<and> x \<le> 1 \<Longrightarrow>
+              e \<le> Im (if 0 < x \<and> x < 1 then ?vd x else \<i> * complex_of_real e)"
+      by (simp add: ge)
+  qed (use has_integral_const_real [of _ 0 1] in auto)
+  with e show ?thesis
+    by (simp add: Re_winding_number [OF \<gamma>] field_simps)
+qed
+
+lemma winding_number_pos_lt:
+  assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
+      and e: "0 < e"
+      and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> e \<le> Im (vector_derivative \<gamma> (at x) * cnj(\<gamma> x - z))"
+    shows "0 < Re (winding_number \<gamma> z)"
+proof -
+  have bm: "bounded ((\<lambda>w. w - z) ` (path_image \<gamma>))"
+    using bounded_translation [of _ "-z"] \<gamma> by (simp add: bounded_valid_path_image)
+  then obtain B where B: "B > 0" and Bno: "\<And>x. x \<in> (\<lambda>w. w - z) ` (path_image \<gamma>) \<Longrightarrow> norm x \<le> B"
+    using bounded_pos [THEN iffD1, OF bm] by blast
+  { fix x::real  assume x: "0 < x" "x < 1"
+    then have B2: "cmod (\<gamma> x - z)^2 \<le> B^2" using Bno [of "\<gamma> x - z"]
+      by (simp add: path_image_def power2_eq_square mult_mono')
+    with x have "\<gamma> x \<noteq> z" using \<gamma>
+      using path_image_def by fastforce
+    then have "e / B\<^sup>2 \<le> Im (vector_derivative \<gamma> (at x) * cnj (\<gamma> x - z)) / (cmod (\<gamma> x - z))\<^sup>2"
+      using B ge [OF x] B2 e
+      apply (rule_tac y="e / (cmod (\<gamma> x - z))\<^sup>2" in order_trans)
+      apply (auto simp: divide_left_mono divide_right_mono)
+      done
+    then have "e / B\<^sup>2 \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
+      by (simp add: complex_div_cnj [of _ "\<gamma> 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 \<gamma>, of "e/B^2"])
+qed
+
+subsection\<open>The winding number is an integer\<close>
+
+text\<open>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.\<close>
+
+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 \<noteq> z"
+    shows "((\<lambda>x. exp(-f x) * (g x - z)) has_vector_derivative 0) (at x within s)"
+proof -
+  have *: "(exp \<circ> (\<lambda>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)
+    using z
+    apply (auto intro!: derivative_eq_intros * [unfolded o_def] g)
+    done
+qed
+
+lemma winding_number_exp_integral:
+  fixes z::complex
+  assumes \<gamma>: "\<gamma> piecewise_C1_differentiable_on {a..b}"
+      and ab: "a \<le> b"
+      and z: "z \<notin> \<gamma> ` {a..b}"
+    shows "(\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)) integrable_on {a..b}"
+          (is "?thesis1")
+          "exp (- (integral {a..b} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))) * (\<gamma> b - z) = \<gamma> a - z"
+          (is "?thesis2")
+proof -
+  let ?D\<gamma> = "\<lambda>x. vector_derivative \<gamma> (at x)"
+  have [simp]: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> \<gamma> x \<noteq> z"
+    using z by force
+  have cong: "continuous_on {a..b} \<gamma>"
+    using \<gamma> by (simp add: piecewise_C1_differentiable_on_def)
+  obtain k where fink: "finite k" and g_C1_diff: "\<gamma> C1_differentiable_on ({a..b} - k)"
+    using \<gamma> by (force simp: piecewise_C1_differentiable_on_def)
+  have \<circ>: "open ({a<..<b} - k)"
+    using \<open>finite k\<close> by (simp add: finite_imp_closed open_Diff)
+  moreover have "{a<..<b} - k \<subseteq> {a..b} - k"
+    by force
+  ultimately have g_diff_at: "\<And>x. \<lbrakk>x \<notin> k; x \<in> {a<..<b}\<rbrakk> \<Longrightarrow> \<gamma> differentiable at x"
+    by (metis Diff_iff differentiable_on_subset C1_diff_imp_diff [OF g_C1_diff] differentiable_on_def at_within_open)
+  { fix w
+    assume "w \<noteq> z"
+    have "continuous_on (ball w (cmod (w - z))) (\<lambda>w. 1 / (w - z))"
+      by (auto simp: dist_norm intro!: continuous_intros)
+    moreover have "\<And>x. cmod (w - x) < cmod (w - z) \<Longrightarrow> \<exists>f'. ((\<lambda>w. 1 / (w - z)) has_field_derivative f') (at x)"
+      by (auto simp: intro!: derivative_eq_intros)
+    ultimately have "\<exists>h. \<forall>y. norm(y - w) < norm(w - z) \<longrightarrow> (h has_field_derivative 1/(y - z)) (at y)"
+      using holomorphic_convex_primitive [of "ball w (norm(w - z))" "{}" "\<lambda>w. 1/(w - z)"]
+      by (force simp: field_differentiable_def Ball_def dist_norm at_within_open_NO_MATCH norm_minus_commute)
+  }
+  then obtain h where h: "\<And>w y. w \<noteq> z \<Longrightarrow> norm(y - w) < norm(w - z) \<Longrightarrow> (h w has_field_derivative 1/(y - z)) (at y)"
+    by meson
+  have exy: "\<exists>y. ((\<lambda>x. inverse (\<gamma> x - z) * ?D\<gamma> x) has_integral y) {a..b}"
+    unfolding integrable_on_def [symmetric]
+  proof (rule contour_integral_local_primitive_any [OF piecewise_C1_imp_differentiable [OF \<gamma>]])
+    show "\<exists>d h. 0 < d \<and>
+               (\<forall>y. cmod (y - w) < d \<longrightarrow> (h has_field_derivative inverse (y - z))(at y within - {z}))"
+          if "w \<in> - {z}" for w
+      apply (rule_tac x="norm(w - z)" in exI)
+      using that inverse_eq_divide has_field_derivative_at_within h
+      by (metis Compl_insert DiffD2 insertCI right_minus_eq zero_less_norm_iff)
+  qed simp
+  have vg_int: "(\<lambda>x. ?D\<gamma> x / (\<gamma> 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 \<in> {a..b}"
+    have cball: "continuous_on (ball (\<gamma> t) (dist (\<gamma> t) z)) (\<lambda>x. inverse (x - z))"
+        using z by (auto intro!: continuous_intros simp: dist_norm)
+    have icd: "\<And>x. cmod (\<gamma> t - x) < cmod (\<gamma> t - z) \<Longrightarrow> (\<lambda>w. inverse (w - z)) field_differentiable at x"
+      unfolding field_differentiable_def by (force simp: intro!: derivative_eq_intros)
+    obtain h where h: "\<And>x. cmod (\<gamma> t - x) < cmod (\<gamma> t - z) \<Longrightarrow>
+                       (h has_field_derivative inverse (x - z)) (at x within {y. cmod (\<gamma> t - y) < cmod (\<gamma> t - z)})"
+      using holomorphic_convex_primitive [where f = "\<lambda>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 \<notin> k" "a < x" "x < b"
+      then have "x \<in> interior ({a..b} - k)"
+        using open_subset_interior [OF \<circ>] by fastforce
+      then have con: "isCont ?D\<gamma> 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}) (\<lambda>x. ?D\<gamma> x)"
+        by (rule continuous_at_imp_continuous_within)
+      have gdx: "\<gamma> differentiable at x"
+        using x by (simp add: g_diff_at)
+      have "\<And>d. \<lbrakk>x \<notin> k; a < x; x < b;
+          (\<gamma> has_vector_derivative d) (at x); a \<le> t; t \<le> b\<rbrakk>
+         \<Longrightarrow> ((\<lambda>x. integral {a..x}
+                     (\<lambda>x. ?D\<gamma> x /
+                           (\<gamma> x - z))) has_vector_derivative
+              d / (\<gamma> x - z))
+              (at x within {a..b})"
+        apply (rule has_vector_derivative_eq_rhs)
+         apply (rule integral_has_vector_derivative_continuous_at [where S = "{}", simplified])
+        apply (rule con_vd continuous_intros cong vg_int | simp add: continuous_at_imp_continuous_within has_vector_derivative_continuous vector_derivative_at)+
+        done
+      then have "((\<lambda>c. exp (- integral {a..c} (\<lambda>x. ?D\<gamma> x / (\<gamma> x - z))) * (\<gamma> c - z)) has_derivative (\<lambda>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_withinI)
+        apply (simp_all add: has_vector_derivative_def [symmetric])
+        done
+      } note * = this
+    have "exp (- (integral {a..t} (\<lambda>x. ?D\<gamma> x / (\<gamma> x - z)))) * (\<gamma> t - z) =\<gamma> a - z"
+      apply (rule has_derivative_zero_unique_strong_interval [of "{a,b} \<union> k" a b])
+      using t
+      apply (auto intro!: * continuous_intros fink cong indefinite_integral_continuous_1 [OF vg_int]  simp add: ab)+
+      done
+   }
+  with ab show ?thesis2
+    by (simp add: divide_inverse_commute integral_def)
+qed
+
+lemma winding_number_exp_2pi:
+    "\<lbrakk>path p; z \<notin> path_image p\<rbrakk>
+     \<Longrightarrow> pathfinish p - z = exp (2 * pi * \<i> * 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 winding_number_prop_def
+  by (force dest: winding_number_exp_integral(2) [of _ 0 1 z] simp: field_simps contour_integral_integral exp_minus)
+
+lemma integer_winding_number_eq:
+  assumes \<gamma>: "path \<gamma>" and z: "z \<notin> path_image \<gamma>"
+  shows "winding_number \<gamma> z \<in> \<int> \<longleftrightarrow> pathfinish \<gamma> = pathstart \<gamma>"
+proof -
+  obtain p where p: "valid_path p" "z \<notin> path_image p"
+                    "pathstart p = pathstart \<gamma>" "pathfinish p = pathfinish \<gamma>"
+           and eq: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
+    using winding_number [OF assms, of 1] unfolding winding_number_prop_def by auto
+  then have wneq: "winding_number \<gamma> z = winding_number p z"
+      using eq winding_number_valid_path by force
+  have iff: "(winding_number \<gamma> z \<in> \<int>) \<longleftrightarrow> (exp (contour_integral p (\<lambda>w. 1 / (w - z))) = 1)"
+    using eq by (simp add: exp_eq_1 complex_is_Int_iff)
+  have "exp (contour_integral p (\<lambda>w. 1 / (w - z))) = (\<gamma> 1 - z) / (\<gamma> 0 - z)"
+    using p winding_number_exp_integral(2) [of p 0 1 z]
+    apply (simp add: valid_path_def path_defs contour_integral_integral exp_minus field_split_simps)
+    by (metis path_image_def pathstart_def pathstart_in_path_image)
+  then have "winding_number p z \<in> \<int> \<longleftrightarrow> pathfinish p = pathstart p"
+    using p wneq iff by (auto simp: path_defs)
+  then show ?thesis using p eq
+    by (auto simp: winding_number_valid_path)
+qed
+
+theorem integer_winding_number:
+  "\<lbrakk>path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> path_image \<gamma>\<rbrakk> \<Longrightarrow> winding_number \<gamma> z \<in> \<int>"
+by (metis integer_winding_number_eq)
+
+
+text\<open>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. \<close>
+
+lemma winding_number_pos_meets:
+  fixes z::complex
+  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and 1: "Re (winding_number \<gamma> z) \<ge> 1"
+      and w: "w \<noteq> z"
+  shows "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image \<gamma>"
+proof -
+  have [simp]: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> \<gamma> x \<noteq> z"
+    using z by (auto simp: path_image_def)
+  have [simp]: "z \<notin> \<gamma> ` {0..1}"
+    using path_image_def z by auto
+  have gpd: "\<gamma> piecewise_C1_differentiable_on {0..1}"
+    using \<gamma> valid_path_def by blast
+  define r where "r = (w - z) / (\<gamma> 0 - z)"
+  have [simp]: "r \<noteq> 0"
+    using w z by (auto simp: r_def)
+  have cont: "continuous_on {0..1}
+     (\<lambda>x. Im (integral {0..x} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z))))"
+    by (intro continuous_intros indefinite_integral_continuous_1 winding_number_exp_integral [OF gpd]; simp)
+  have "Arg2pi r \<le> 2*pi"
+    by (simp add: Arg2pi less_eq_real_def)
+  also have "\<dots> \<le> Im (integral {0..1} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))"
+    using 1
+    apply (simp add: winding_number_valid_path [OF \<gamma> z] contour_integral_integral)
+    apply (simp add: Complex.Re_divide field_simps power2_eq_square)
+    done
+  finally have "Arg2pi r \<le> Im (integral {0..1} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))" .
+  then have "\<exists>t. t \<in> {0..1} \<and> Im(integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x)/(\<gamma> x - z))) = Arg2pi r"
+    by (simp add: Arg2pi_ge_0 cont IVT')
+  then obtain t where t:     "t \<in> {0..1}"
+                  and eqArg: "Im (integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x)/(\<gamma> x - z))) = Arg2pi r"
+    by blast
+  define i where "i = integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
+  have iArg: "Arg2pi r = Im i"
+    using eqArg by (simp add: i_def)
+  have gpdt: "\<gamma> 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) * (\<gamma> t - z) = \<gamma> 0 - z"
+    unfolding i_def
+    apply (rule winding_number_exp_integral [OF gpdt])
+    using t z unfolding path_image_def by force+
+  then have *: "\<gamma> t - z = exp i * (\<gamma> 0 - z)"
+    by (simp add: exp_minus field_simps)
+  then have "(w - z) = r * (\<gamma> 0 - z)"
+    by (simp add: r_def)
+  then have "z + complex_of_real (exp (Re i)) * (w - z) / complex_of_real (cmod r) = \<gamma> t"
+    apply simp
+    apply (subst Complex_Transcendental.Arg2pi_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 \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "\<bar>Re (winding_number \<gamma> z)\<bar> \<ge> 1"
+      and w: "w \<noteq> z"
+  shows "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image \<gamma>"
+proof -
+  { assume "Re (winding_number \<gamma> z) \<le> - 1"
+    then have "Re (winding_number (reversepath \<gamma>) z) \<ge> 1"
+      by (simp add: \<gamma> valid_path_imp_path winding_number_reversepath z)
+    moreover have "valid_path (reversepath \<gamma>)"
+      using \<gamma> valid_path_imp_reverse by auto
+    moreover have "z \<notin> path_image (reversepath \<gamma>)"
+      by (simp add: z)
+    ultimately have "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image (reversepath \<gamma>)"
+      using winding_number_pos_meets w by blast
+    then have ?thesis
+      by simp
+  }
+  then show ?thesis
+    using assms
+    by (simp add: abs_if winding_number_pos_meets split: if_split_asm)
+qed
+
+lemma winding_number_less_1:
+  fixes z::complex
+  shows
+  "\<lbrakk>valid_path \<gamma>; z \<notin> path_image \<gamma>; w \<noteq> z;
+    \<And>a::real. 0 < a \<Longrightarrow> z + a*(w - z) \<notin> path_image \<gamma>\<rbrakk>
+   \<Longrightarrow> Re(winding_number \<gamma> z) < 1"
+   by (auto simp: not_less dest: winding_number_big_meets)
+
+text\<open>One way of proving that WN=1 for a loop.\<close>
+lemma winding_number_eq_1:
+  fixes z::complex
+  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
+      and 0: "0 < Re(winding_number \<gamma> z)" and 2: "Re(winding_number \<gamma> z) < 2"
+  shows "winding_number \<gamma> z = 1"
+proof -
+  have "winding_number \<gamma> z \<in> Ints"
+    by (simp add: \<gamma> integer_winding_number loop valid_path_imp_path z)
+  then show ?thesis
+    using 0 2 by (auto simp: Ints_def)
+qed
+
+subsection\<open>Continuity of winding number and invariance on connected sets\<close>
+
+lemma continuous_at_winding_number:
+  fixes z::complex
+  assumes \<gamma>: "path \<gamma>" and z: "z \<notin> path_image \<gamma>"
+  shows "continuous (at z) (winding_number \<gamma>)"
+proof -
+  obtain e where "e>0" and cbg: "cball z e \<subseteq> - path_image \<gamma>"
+    using open_contains_cball [of "- path_image \<gamma>"]  z
+    by (force simp: closed_def [symmetric] closed_path_image [OF \<gamma>])
+  then have ppag: "path_image \<gamma> \<subseteq> - 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:
+    "\<And>h1 h2. \<lbrakk>valid_path h1; valid_path h2;
+              (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < d \<and> cmod (h2 t - \<gamma> t) < d);
+              pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1\<rbrakk>
+             \<Longrightarrow>
+               path_image h1 \<subseteq> - cball z (e / 2) \<and>
+               path_image h2 \<subseteq> - cball z (e / 2) \<and>
+               (\<forall>f. f holomorphic_on - cball z (e / 2) \<longrightarrow> contour_integral h2 f = contour_integral h1 f)"
+    using contour_integral_nearby_ends [OF oc \<gamma> ppag] by metis
+  obtain p where p: "valid_path p" "z \<notin> path_image p"
+                    "pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma>"
+              and pg: "\<And>t. t\<in>{0..1} \<Longrightarrow> cmod (\<gamma> t - p t) < min d e / 2"
+              and pi: "contour_integral p (\<lambda>x. 1 / (x - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
+    using winding_number [OF \<gamma> z, of "min d e / 2"] \<open>d>0\<close> \<open>e>0\<close> by (auto simp: winding_number_prop_def)
+  { fix w
+    assume d2: "cmod (w - z) < d/2" and e2: "cmod (w - z) < e/2"
+    then have wnotp: "w \<notin> path_image p"
+      using cbg \<open>d>0\<close> \<open>e>0\<close>
+      apply (simp add: path_image_def cball_def dist_norm, clarify)
+      apply (frule pg)
+      apply (drule_tac c="\<gamma> x" in subsetD)
+      apply (auto simp: less_eq_real_def norm_minus_commute norm_triangle_half_l)
+      done
+    have wnotg: "w \<notin> path_image \<gamma>"
+      using cbg e2 \<open>e>0\<close> by (force simp: dist_norm norm_minus_commute)
+    { fix k::real
+      assume k: "k>0"
+      then obtain q where q: "valid_path q" "w \<notin> path_image q"
+                             "pathstart q = pathstart \<gamma> \<and> pathfinish q = pathfinish \<gamma>"
+                    and qg: "\<And>t. t \<in> {0..1} \<Longrightarrow> cmod (\<gamma> t - q t) < min k (min d e) / 2"
+                    and qi: "contour_integral q (\<lambda>u. 1 / (u - w)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> w"
+        using winding_number [OF \<gamma> wnotg, of "min k (min d e) / 2"] \<open>d>0\<close> \<open>e>0\<close> k
+        by (force simp: min_divide_distrib_right winding_number_prop_def)
+      have "contour_integral p (\<lambda>u. 1 / (u - w)) = contour_integral q (\<lambda>u. 1 / (u - w))"
+        apply (rule pi_eq [OF \<open>valid_path q\<close> \<open>valid_path p\<close>, THEN conjunct2, THEN conjunct2, rule_format])
+        apply (frule pg)
+        apply (frule qg)
+        using p q \<open>d>0\<close> e2
+        apply (auto simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
+        done
+      then have "contour_integral p (\<lambda>x. 1 / (x - w)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> 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 \<gamma> 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 winding_number_prop_def)
+      done
+  } note wnwn = this
+  obtain pe where "pe>0" and cbp: "cball z (3 / 4 * pe) \<subseteq> - 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: "\<And>f B. \<lbrakk>f holomorphic_on - cball z (3 / 4 * pe);
+                      \<forall>z \<in> - cball z (3 / 4 * pe). cmod (f z) \<le> B\<rbrakk> \<Longrightarrow>
+                      cmod (contour_integral p f) \<le> L * B"
+    using contour_integral_bound_exists [of "- cball z (3/4*pe)" p] cbp \<open>valid_path p\<close> by blast
+  { fix e::real and w::complex
+    assume e: "0 < e" and w: "cmod (w - z) < pe/4" "cmod (w - z) < e * pe\<^sup>2 / (8 * L)"
+    then have [simp]: "w \<notin> path_image p"
+      using cbp p(2) \<open>0 < pe\<close>
+      by (force simp: dist_norm norm_minus_commute path_image_def cball_def)
+    have [simp]: "contour_integral p (\<lambda>x. 1/(x - w)) - contour_integral p (\<lambda>x. 1/(x - z)) =
+                  contour_integral p (\<lambda>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) \<le> cmod (z - w) + cmod (w - x)"
+        using norm_diff_triangle_le by blast
+      also have "\<dots> < 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 \<open>pe>0\<close> by auto
+      then have pe2: "pe^2 < 4 * cmod (w - x) ^ 2"
+        by (simp add: power_divide)
+      have "8 * L * cmod (w - z) < e * pe\<^sup>2"
+        using w \<open>L>0\<close> by (simp add: field_simps)
+      also have "\<dots> < e * 4 * cmod (w - x) * cmod (w - x)"
+        using pe2 \<open>e>0\<close> by (simp add: power2_eq_square)
+      also have "\<dots> < 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 "\<dots> \<le> 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)) \<le> e"
+        apply (cases "x=z \<or> x=w")
+        using pe \<open>pe>0\<close> w \<open>L>0\<close>
+        apply (force simp: norm_minus_commute)
+        using wx w(2) \<open>L>0\<close> 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 (\<lambda>x. 1 / (x - w) - 1 / (x - z))) \<le> L * (e * pe\<^sup>2 / L / 4 * (inverse (pe / 2))\<^sup>2)"
+      apply (rule L)
+      using \<open>pe>0\<close> w
+      apply (force simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
+      using \<open>pe>0\<close> w \<open>L>0\<close>
+      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 (\<lambda>x. 1 / (x - w)) - contour_integral p (\<lambda>x. 1 / (x - z))) < 2*e"
+      apply simp
+      apply (rule le_less_trans [OF *])
+      using \<open>L>0\<close> 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 \<open>pe>0\<close> \<open>L>0\<close>
+    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: \<open>d>0\<close> \<open>e>0\<close> dist_norm wnwn)
+    done
+qed
+
+corollary continuous_on_winding_number:
+    "path \<gamma> \<Longrightarrow> continuous_on (- path_image \<gamma>) (\<lambda>w. winding_number \<gamma> w)"
+  by (simp add: continuous_at_imp_continuous_on continuous_at_winding_number)
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>The winding number is constant on a connected region\<close>
+
+lemma winding_number_constant:
+  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and cs: "connected S" and sg: "S \<inter> path_image \<gamma> = {}"
+  shows "winding_number \<gamma> constant_on S"
+proof -
+  have *: "1 \<le> cmod (winding_number \<gamma> y - winding_number \<gamma> z)"
+      if ne: "winding_number \<gamma> y \<noteq> winding_number \<gamma> z" and "y \<in> S" "z \<in> S" for y z
+  proof -
+    have "winding_number \<gamma> y \<in> \<int>"  "winding_number \<gamma> z \<in>  \<int>"
+      using that integer_winding_number [OF \<gamma> loop] sg \<open>y \<in> S\<close> by auto
+    with ne show ?thesis
+      by (auto simp: Ints_def simp flip: of_int_diff)
+  qed
+  have cont: "continuous_on S (\<lambda>w. winding_number \<gamma> w)"
+    using continuous_on_winding_number [OF \<gamma>] sg
+    by (meson continuous_on_subset disjoint_eq_subset_Compl)
+  show ?thesis
+    using "*" zero_less_one
+    by (blast intro: continuous_discrete_range_constant [OF cs cont])
+qed
+
+lemma winding_number_eq:
+     "\<lbrakk>path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; w \<in> S; z \<in> S; connected S; S \<inter> path_image \<gamma> = {}\<rbrakk>
+      \<Longrightarrow> winding_number \<gamma> w = winding_number \<gamma> z"
+  using winding_number_constant by (metis constant_on_def)
+
+lemma open_winding_number_levelsets:
+  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
+    shows "open {z. z \<notin> path_image \<gamma> \<and> winding_number \<gamma> z = k}"
+proof -
+  have opn: "open (- path_image \<gamma>)"
+    by (simp add: closed_path_image \<gamma> open_Compl)
+  { fix z assume z: "z \<notin> path_image \<gamma>" and k: "k = winding_number \<gamma> z"
+    obtain e where e: "e>0" "ball z e \<subseteq> - path_image \<gamma>"
+      using open_contains_ball [of "- path_image \<gamma>"] opn z
+      by blast
+    have "\<exists>e>0. \<forall>y. dist y z < e \<longrightarrow> y \<notin> path_image \<gamma> \<and> winding_number \<gamma> y = winding_number \<gamma> 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\<open>Winding number is zero "outside" a curve\<close>
+
+proposition winding_number_zero_in_outside:
+  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and z: "z \<in> outside (path_image \<gamma>)"
+    shows "winding_number \<gamma> z = 0"
+proof -
+  obtain B::real where "0 < B" and B: "path_image \<gamma> \<subseteq> ball 0 B"
+    using bounded_subset_ballD [OF bounded_path_image [OF \<gamma>]] by auto
+  obtain w::complex where w: "w \<notin> ball 0 (B + 1)"
+    by (metis abs_of_nonneg le_less less_irrefl mem_ball_0 norm_of_real)
+  have "- ball 0 (B + 1) \<subseteq> outside (path_image \<gamma>)"
+    apply (rule outside_subset_convex)
+    using B subset_ball by auto
+  then have wout: "w \<in> outside (path_image \<gamma>)"
+    using w by blast
+  moreover have "winding_number \<gamma> constant_on outside (path_image \<gamma>)"
+    using winding_number_constant [OF \<gamma> loop, of "outside(path_image \<gamma>)"] connected_outside
+    by (metis DIM_complex bounded_path_image dual_order.refl \<gamma> outside_no_overlap)
+  ultimately have "winding_number \<gamma> z = winding_number \<gamma> w"
+    by (metis (no_types, hide_lams) constant_on_def z)
+  also have "\<dots> = 0"
+  proof -
+    have wnot: "w \<notin> path_image \<gamma>"  using wout by (simp add: outside_def)
+    { fix e::real assume "0<e"
+      obtain p where p: "polynomial_function p" "pathstart p = pathstart \<gamma>" "pathfinish p = pathfinish \<gamma>"
+                 and pg1: "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> cmod (p t - \<gamma> t) < 1)"
+                 and pge: "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> cmod (p t - \<gamma> t) < e)"
+        using path_approx_polynomial_function [OF \<gamma>, of "min 1 e"] \<open>e>0\<close> by force
+      have pip: "path_image p \<subseteq> 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 \<notin> path_image p"  using w by blast
+      then have "\<exists>p. valid_path p \<and> w \<notin> path_image p \<and>
+                     pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and>
+                     (\<forall>t\<in>{0..1}. cmod (\<gamma> t - p t) < e) \<and> contour_integral p (\<lambda>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 \<gamma>] simp add: winding_number_prop_def wnot)
+  qed
+  finally show ?thesis .
+qed
+
+corollary\<^marker>\<open>tag unimportant\<close> winding_number_zero_const: "a \<noteq> z \<Longrightarrow> winding_number (\<lambda>t. a) z = 0"
+  by (rule winding_number_zero_in_outside)
+     (auto simp: pathfinish_def pathstart_def path_polynomial_function)
+
+corollary\<^marker>\<open>tag unimportant\<close> winding_number_zero_outside:
+    "\<lbrakk>path \<gamma>; convex s; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> s; path_image \<gamma> \<subseteq> s\<rbrakk> \<Longrightarrow> winding_number \<gamma> z = 0"
+  by (meson convex_in_outside outside_mono subsetCE winding_number_zero_in_outside)
+
+lemma winding_number_zero_at_infinity:
+  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
+    shows "\<exists>B. \<forall>z. B \<le> norm z \<longrightarrow> winding_number \<gamma> z = 0"
+proof -
+  obtain B::real where "0 < B" and B: "path_image \<gamma> \<subseteq> ball 0 B"
+    using bounded_subset_ballD [OF bounded_path_image [OF \<gamma>]] by auto
+  then show ?thesis
+    apply (rule_tac x="B+1" in exI, clarify)
+    apply (rule winding_number_zero_outside [OF \<gamma> 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:
+    "\<lbrakk>path \<gamma>; convex s; pathfinish \<gamma> = pathstart \<gamma>; open s; path_image \<gamma> \<subseteq> s\<rbrakk>
+     \<Longrightarrow> \<exists>z. z \<in> s \<and> winding_number \<gamma> z = 0"
+  using outside_compact_in_open [of "path_image \<gamma>" s] path_image_nonempty winding_number_zero_in_outside
+  by (fastforce simp add: compact_path_image)
+
+
+text\<open>If a path winds round a set, it winds rounds its inside.\<close>
+lemma winding_number_around_inside:
+  assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
+      and cls: "closed s" and cos: "connected s" and s_disj: "s \<inter> path_image \<gamma> = {}"
+      and z: "z \<in> s" and wn_nz: "winding_number \<gamma> z \<noteq> 0" and w: "w \<in> s \<union> inside s"
+    shows "winding_number \<gamma> w = winding_number \<gamma> z"
+proof -
+  have ssb: "s \<subseteq> inside(path_image \<gamma>)"
+  proof
+    fix x :: complex
+    assume "x \<in> s"
+    hence "x \<notin> path_image \<gamma>"
+      by (meson disjoint_iff_not_equal s_disj)
+    thus "x \<in> inside (path_image \<gamma>)"
+      using \<open>x \<in> s\<close> by (metis (no_types) ComplI UnE cos \<gamma> 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 \<gamma> 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 \<gamma>"] compact_path_image connected_path_image contra_subsetD disjoint_iff_not_equal \<gamma> inside_no_overlap)
+ qed
+
+
+text\<open>Bounding a WN by 1/2 for a path and point in opposite halfspaces.\<close>
+lemma winding_number_subpath_continuous:
+  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>"
+    shows "continuous_on {0..1} (\<lambda>x. winding_number(subpath 0 x \<gamma>) z)"
+proof -
+  have *: "integral {0..x} (\<lambda>t. vector_derivative \<gamma> (at t) / (\<gamma> t - z)) / (2 * of_real pi * \<i>) =
+         winding_number (subpath 0 x \<gamma>) z"
+         if x: "0 \<le> x" "x \<le> 1" for x
+  proof -
+    have "integral {0..x} (\<lambda>t. vector_derivative \<gamma> (at t) / (\<gamma> t - z)) / (2 * of_real pi * \<i>) =
+          1 / (2*pi*\<i>) * contour_integral (subpath 0 x \<gamma>) (\<lambda>w. 1/(w - z))"
+      using assms x
+      apply (simp add: contour_integral_subcontour_integral [OF contour_integrable_inversediff])
+      done
+    also have "\<dots> = winding_number (subpath 0 x \<gamma>) 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 = "\<lambda>x. 1 / (2*pi*\<i>) *
+                                 integral {0..x} (\<lambda>t. 1/(\<gamma> t - z) * vector_derivative \<gamma> (at t))"])
+    apply (rule continuous_intros)+
+    apply (rule indefinite_integral_continuous_1)
+    apply (rule contour_integrable_inversediff [OF assms, unfolded contour_integrable_on])
+      using assms
+    apply (simp add: *)
+    done
+qed
+
+lemma winding_number_ivt_pos:
+    assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "0 \<le> w" "w \<le> Re(winding_number \<gamma> z)"
+      shows "\<exists>t \<in> {0..1}. Re(winding_number(subpath 0 t \<gamma>) z) = w"
+  apply (rule ivt_increasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right], simp)
+  apply (rule winding_number_subpath_continuous [OF \<gamma> z])
+  using assms
+  apply (auto simp: path_image_def image_def)
+  done
+
+lemma winding_number_ivt_neg:
+    assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "Re(winding_number \<gamma> z) \<le> w" "w \<le> 0"
+      shows "\<exists>t \<in> {0..1}. Re(winding_number(subpath 0 t \<gamma>) z) = w"
+  apply (rule ivt_decreasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right], simp)
+  apply (rule winding_number_subpath_continuous [OF \<gamma> z])
+  using assms
+  apply (auto simp: path_image_def image_def)
+  done
+
+lemma winding_number_ivt_abs:
+    assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "0 \<le> w" "w \<le> \<bar>Re(winding_number \<gamma> z)\<bar>"
+      shows "\<exists>t \<in> {0..1}. \<bar>Re (winding_number (subpath 0 t \<gamma>) z)\<bar> = w"
+  using assms winding_number_ivt_pos [of \<gamma> z w] winding_number_ivt_neg [of \<gamma> z "-w"]
+  by force
+
+lemma winding_number_lt_half_lemma:
+  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and az: "a \<bullet> z \<le> b" and pag: "path_image \<gamma> \<subseteq> {w. a \<bullet> w > b}"
+    shows "Re(winding_number \<gamma> z) < 1/2"
+proof -
+  { assume "Re(winding_number \<gamma> z) \<ge> 1/2"
+    then obtain t::real where t: "0 \<le> t" "t \<le> 1" and sub12: "Re (winding_number (subpath 0 t \<gamma>) z) = 1/2"
+      using winding_number_ivt_pos [OF \<gamma> z, of "1/2"] by auto
+    have gt: "\<gamma> t - z = - (of_real (exp (- (2 * pi * Im (winding_number (subpath 0 t \<gamma>) z)))) * (\<gamma> 0 - z))"
+      using winding_number_exp_2pi [of "subpath 0 t \<gamma>" z]
+      apply (simp add: t \<gamma> 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 \<bullet> \<gamma> 0"
+    proof -
+      have "\<gamma> 0 \<in> {c. b < a \<bullet> 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 \<bullet> \<gamma> t"
+    proof -
+      have "\<gamma> t \<in> {c. b < a \<bullet> c}"
+        by (metis (no_types) pag atLeastAtMost_iff image_subset_iff path_image_def t)
+      thus ?thesis
+        by blast
+    qed
+    ultimately have "0 < a \<bullet> (\<gamma> 0 - z)" "0 < a \<bullet> (\<gamma> 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 \<gamma>" "a \<bullet> z \<le> b" "path_image \<gamma> \<subseteq> {w. a \<bullet> w > b}"
+    shows "\<bar>Re (winding_number \<gamma> z)\<bar> < 1/2"
+proof -
+  have "z \<notin> path_image \<gamma>" 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 \<gamma> z a b]
+                 winding_number_reversepath valid_path_imp_path inner_minus_left path_image_reversepath)
+    done
+qed
+
+lemma winding_number_le_half:
+  assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>"
+      and anz: "a \<noteq> 0" and azb: "a \<bullet> z \<le> b" and pag: "path_image \<gamma> \<subseteq> {w. a \<bullet> w \<ge> b}"
+    shows "\<bar>Re (winding_number \<gamma> z)\<bar> \<le> 1/2"
+proof -
+  { assume wnz_12: "\<bar>Re (winding_number \<gamma> z)\<bar> > 1/2"
+    have "isCont (winding_number \<gamma>) z"
+      by (metis continuous_at_winding_number valid_path_imp_path \<gamma> z)
+    then obtain d where "d>0" and d: "\<And>x'. dist x' z < d \<Longrightarrow> dist (winding_number \<gamma> x') (winding_number \<gamma> z) < \<bar>Re(winding_number \<gamma> z)\<bar> - 1/2"
+      using continuous_at_eps_delta wnz_12 diff_gt_0_iff_gt by blast
+    define z' where "z' = z - (d / (2 * cmod a)) *\<^sub>R a"
+    have *: "a \<bullet> z' \<le> b - d / 3 * cmod a"
+      unfolding z'_def inner_mult_right' divide_inverse
+      apply (simp add: field_split_simps algebra_simps dot_square_norm power2_eq_square anz)
+      apply (metis \<open>0 < d\<close> add_increasing azb less_eq_real_def mult_nonneg_nonneg mult_right_mono norm_ge_zero norm_numeral)
+      done
+    have "cmod (winding_number \<gamma> z' - winding_number \<gamma> z) < \<bar>Re (winding_number \<gamma> z)\<bar> - 1/2"
+      using d [of z'] anz \<open>d>0\<close> by (simp add: dist_norm z'_def)
+    then have "1/2 < \<bar>Re (winding_number \<gamma> z)\<bar> - cmod (winding_number \<gamma> z' - winding_number \<gamma> z)"
+      by simp
+    then have "1/2 < \<bar>Re (winding_number \<gamma> z)\<bar> - \<bar>Re (winding_number \<gamma> z') - Re (winding_number \<gamma> z)\<bar>"
+      using abs_Re_le_cmod [of "winding_number \<gamma> z' - winding_number \<gamma> z"] by simp
+    then have wnz_12': "\<bar>Re (winding_number \<gamma> z')\<bar> > 1/2"
+      by linarith
+    moreover have "\<bar>Re (winding_number \<gamma> z')\<bar> < 1/2"
+      apply (rule winding_number_lt_half [OF \<gamma> *])
+      using azb \<open>d>0\<close> pag
+      apply (auto simp: add_strict_increasing anz field_split_simps dest!: subsetD)
+      done
+    ultimately have False
+      by simp
+  }
+  then show ?thesis by force
+qed
+
+lemma winding_number_lt_half_linepath: "z \<notin> closed_segment a b \<Longrightarrow> \<bar>Re (winding_number (linepath a b) z)\<bar> < 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\<open> Positivity of WN for a linepath.\<close>
+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 \<notin> 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\<open>Cauchy's integral formula, again for a convex enclosing set\<close>
+
+lemma Cauchy_integral_formula_weak:
+    assumes s: "convex s" and "finite k" and conf: "continuous_on s f"
+        and fcd: "(\<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x)"
+        and z: "z \<in> interior s - k" and vpg: "valid_path \<gamma>"
+        and pasz: "path_image \<gamma> \<subseteq> s - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
+      shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
+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 \<gamma> \<subseteq> s" and znotin: "z \<notin> path_image \<gamma>" using pasz by blast+
+  have c: "continuous (at x within s) (\<lambda>w. if w = z then f' else (f w - f z) / (w - z))" if "x \<in> 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" _ "\<lambda>w::complex. (f w - f z)/(w - z)"])
+      using has_field_derivative_at_within has_field_derivative_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) (\<lambda>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 "\<lambda>w::complex. (f w - f z)/(w - z)"])
+      apply (force simp: dist_commute)
+      done
+  qed
+  have fink': "finite (insert z k)" using \<open>finite k\<close> by blast
+  have *: "((\<lambda>w. if w = z then f' else (f w - f z) / (w - z)) has_contour_integral 0) \<gamma>"
+    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 = "\<lambda>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: ac_simps divide_simps)
+    done
+qed
+
+theorem Cauchy_integral_formula_convex_simple:
+    "\<lbrakk>convex s; f holomorphic_on s; z \<in> interior s; valid_path \<gamma>; path_image \<gamma> \<subseteq> s - {z};
+      pathfinish \<gamma> = pathstart \<gamma>\<rbrakk>
+     \<Longrightarrow> ((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
+  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\<open>Homotopy forms of Cauchy's theorem\<close>
+
+lemma 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 \<times> real \<Rightarrow> complex"
+    where contk: "continuous_on ({0..1} \<times> {0..1}) k"
+      and ks: "k ` ({0..1} \<times> {0..1}) \<subseteq> s"
+      and k [simp]: "\<forall>x. k (0, x) = g x" "\<forall>x. k (1, x) = h x"
+      and ksf: "\<forall>t\<in>{0..1}. linked_paths atends g (\<lambda>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} \<times> {0..1}) k"
+    by (blast intro: compact_Times compact_uniformly_continuous [OF contk])
+  { fix t::real assume t: "t \<in> {0..1}"
+    have pak: "path (k \<circ> (\<lambda>u. (t, u)))"
+      unfolding path_def
+      apply (rule continuous_intros continuous_on_subset [OF contk])+
+      using t by force
+    have pik: "path_image (k \<circ> Pair t) \<subseteq> s"
+      using ks t by (auto simp: path_image_def)
+    obtain e where "e>0" and e:
+         "\<And>g h. \<lbrakk>valid_path g; valid_path h;
+                  \<forall>u\<in>{0..1}. cmod (g u - (k \<circ> Pair t) u) < e \<and> cmod (h u - (k \<circ> Pair t) u) < e;
+                  linked_paths atends g h\<rbrakk>
+                 \<Longrightarrow> contour_integral h f = contour_integral g f"
+      using contour_integral_nearby [OF \<open>open s\<close> pak pik, of atends] f by metis
+    obtain d where "d>0" and d:
+        "\<And>x x'. \<lbrakk>x \<in> {0..1} \<times> {0..1}; x' \<in> {0..1} \<times> {0..1}; norm (x'-x) < d\<rbrakk> \<Longrightarrow> norm (k x' - k x) < e/4"
+      by (rule uniformly_continuous_onE [OF ucontk, of "e/4"]) (auto simp: dist_norm \<open>e>0\<close>)
+    { fix t1 t2
+      assume t1: "0 \<le> t1" "t1 \<le> 1" and t2: "0 \<le> t2" "t2 \<le> 1" and ltd: "\<bar>t1 - t\<bar> < d" "\<bar>t2 - t\<bar> < d"
+      have no2: "\<And>g1 k1 kt. \<lbrakk>norm(g1 - k1) < e/4; norm(k1 - kt) < e/4\<rbrakk> \<Longrightarrow> norm(g1 - kt) < e"
+        using \<open>e > 0\<close>
+        apply (rule_tac y = k1 in norm_triangle_half_l)
+        apply (auto simp: norm_minus_commute intro: order_less_trans)
+        done
+      have "\<exists>d>0. \<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
+                          (\<forall>u\<in>{0..1}. cmod (g1 u - k (t1, u)) < d \<and> cmod (g2 u - k (t2, u)) < d) \<and>
+                          linked_paths atends g1 g2 \<longrightarrow>
+                          contour_integral g2 f = contour_integral g1 f"
+        apply (rule_tac x="e/4" in exI)
+        using t t1 t2 ltd \<open>e > 0\<close>
+        apply (auto intro!: e simp: d no2 simp del: less_divide_eq_numeral1)
+        done
+    }
+    then have "\<exists>e. 0 < e \<and>
+              (\<forall>t1 t2. t1 \<in> {0..1} \<and> t2 \<in> {0..1} \<and> \<bar>t1 - t\<bar> < e \<and> \<bar>t2 - t\<bar> < e
+                \<longrightarrow> (\<exists>d. 0 < d \<and>
+                     (\<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
+                       (\<forall>u \<in> {0..1}.
+                          norm(g1 u - k((t1,u))) < d \<and> norm(g2 u - k((t2,u))) < d) \<and>
+                          linked_paths atends g1 g2
+                          \<longrightarrow> contour_integral g2 f = contour_integral g1 f)))"
+      by (rule_tac x=d in exI) (simp add: \<open>d > 0\<close>)
+  }
+  then obtain ee where ee:
+       "\<And>t. t \<in> {0..1} \<Longrightarrow> ee t > 0 \<and>
+          (\<forall>t1 t2. t1 \<in> {0..1} \<longrightarrow> t2 \<in> {0..1} \<longrightarrow> \<bar>t1 - t\<bar> < ee t \<longrightarrow> \<bar>t2 - t\<bar> < ee t
+            \<longrightarrow> (\<exists>d. 0 < d \<and>
+                 (\<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
+                   (\<forall>u \<in> {0..1}.
+                      norm(g1 u - k((t1,u))) < d \<and> norm(g2 u - k((t2,u))) < d) \<and>
+                      linked_paths atends g1 g2
+                      \<longrightarrow> contour_integral g2 f = contour_integral g1 f)))"
+    by metis
+  note ee_rule = ee [THEN conjunct2, rule_format]
+  define C where "C = (\<lambda>t. ball t (ee t / 3)) ` {0..1}"
+  obtain C' where C': "C' \<subseteq> C" "finite C'" and C'01: "{0..1} \<subseteq> \<Union>C'"
+  proof (rule compactE [OF compact_interval])
+    show "{0..1} \<subseteq> \<Union>C"
+      using ee [THEN conjunct1] by (auto simp: C_def dist_norm)
+  qed (use C_def in auto)
+  define kk where "kk = {t \<in> {0..1}. ball t (ee t / 3) \<in> C'}"
+  have kk01: "kk \<subseteq> {0..1}" by (auto simp: kk_def)
+  define e where "e = Min (ee ` kk)"
+  have C'_eq: "C' = (\<lambda>t. ball t (ee t / 3)) ` kk"
+    using C' by (auto simp: kk_def C_def)
+  have ee_pos[simp]: "\<And>t. t \<in> {0..1} \<Longrightarrow> ee t > 0"
+    by (simp add: kk_def ee)
+  moreover have "finite kk"
+    using \<open>finite C'\<close> kk01 by (force simp: C'_eq inj_on_def ball_eq_ball_iff dest: ee_pos finite_imageD)
+  moreover have "kk \<noteq> {}" using \<open>{0..1} \<subseteq> \<Union>C'\<close> 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: "\<And>i. i \<in> kk \<Longrightarrow> e \<le> ee i"
+    using \<open>finite kk\<close> by (simp add: e_def Min_le_iff [of "ee ` kk"])
+  have plus: "\<exists>t \<in> kk. x \<in> ball t (ee t / 3)" if "x \<in> {0..1}" for x
+    using C' subsetD [OF C'01 that]  unfolding C'_eq by blast
+  have [OF order_refl]:
+      "\<exists>d. 0 < d \<and> (\<forall>j. valid_path j \<and> (\<forall>u \<in> {0..1}. norm(j u - k (n/N, u)) < d) \<and> linked_paths atends g j
+                        \<longrightarrow> contour_integral j f = contour_integral g f)"
+       if "n \<le> 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 \<in> {0..1}" "(Suc n)/N \<in> {0..1}"  by auto
+    then obtain t where t: "t \<in> kk" "n/N \<in> ball t (ee t / 3)"
+      using plus [of "n/N"] by blast
+    then have nN_less: "\<bar>n/N - t\<bar> < ee t"
+      by (simp add: dist_norm del: less_divide_eq_numeral1)
+    have n'N_less: "\<bar>real (Suc n) / real N - t\<bar> < ee t"
+      using t N \<open>N > 0\<close> 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 \<in> {0..1}" using \<open>kk \<subseteq> {0..1}\<close> \<open>t \<in> kk\<close> by blast
+    obtain d1 where "d1 > 0" and d1:
+        "\<And>g1 g2. \<lbrakk>valid_path g1; valid_path g2;
+                   \<forall>u\<in>{0..1}. cmod (g1 u - k (n/N, u)) < d1 \<and> cmod (g2 u - k ((Suc n) / N, u)) < d1;
+                   linked_paths atends g1 g2\<rbrakk>
+                   \<Longrightarrow> 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 \<le> N" using Suc.prems by auto
+    with Suc.hyps
+    obtain d2 where "d2 > 0"
+      and d2: "\<And>j. \<lbrakk>valid_path j; \<forall>u\<in>{0..1}. cmod (j u - k (n/N, u)) < d2; linked_paths atends g j\<rbrakk>
+                     \<Longrightarrow> contour_integral j f = contour_integral g f"
+        by auto
+    have "continuous_on {0..1} (k \<circ> (\<lambda>u. (n/N, u)))"
+      apply (rule continuous_intros continuous_on_subset [OF contk])+
+      using N01 by auto
+    then have pkn: "path (\<lambda>u. k (n/N, u))"
+      by (simp add: path_def)
+    have min12: "min d1 d2 > 0" by (simp add: \<open>0 < d1\<close> \<open>0 < d2\<close>)
+    obtain p where "polynomial_function p"
+        and psf: "pathstart p = pathstart (\<lambda>u. k (n/N, u))"
+                 "pathfinish p = pathfinish (\<lambda>u. k (n/N, u))"
+        and pk_le:  "\<And>t. t\<in>{0..1} \<Longrightarrow> 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
+    proof (intro exI; safe)
+      fix j
+      assume "valid_path j" "linked_paths atends g j"
+        and "\<forall>u\<in>{0..1}. cmod (j u - k (real (Suc n) / real N, u)) < min d1 d2"
+      then have "contour_integral j f = contour_integral p f"
+        using pk_le N01(1) ksf by (force intro!: vpp d1 simp add: linked_paths_def psf)
+      also have "... = contour_integral g f"
+        using pk_le by (force intro!: vpp d2 lpa)
+      finally show "contour_integral j f = contour_integral g f" .
+    qed (simp add: \<open>0 < d1\<close> \<open>0 < d2\<close>)
+  qed
+  then obtain d where "0 < d"
+                       "\<And>j. valid_path j \<and> (\<forall>u \<in> {0..1}. norm(j u - k (1,u)) < d) \<and> linked_paths atends g j
+                            \<Longrightarrow> contour_integral j f = contour_integral g f"
+    using \<open>N>0\<close> by auto
+  then have "linked_paths atends g h \<Longrightarrow> contour_integral h f = contour_integral g f"
+    using \<open>N>0\<close> 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:
+    "\<lbrakk>(f has_contour_integral y) h; f contour_integrable_on g; contour_integral g f = contour_integral h f\<rbrakk>
+     \<Longrightarrow> (f has_contour_integral y) g"
+  using has_contour_integral_integral contour_integral_unique by auto
+
+lemma Cauchy_theorem_null_homotopic:
+     "\<lbrakk>f holomorphic_on s; open s; valid_path g; homotopic_loops s g (linepath a a)\<rbrakk> \<Longrightarrow> (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\<^marker>\<open>tag unimportant\<close> \<open>More winding number properties\<close>
+
+text\<open>including the fact that it's +-1 inside a simple closed curve.\<close>
+
+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 \<notin> path_image g" and pah: "z \<notin> path_image h"
+    using homotopic_paths_imp_subset [OF assms] by auto
+  ultimately obtain d e where "d > 0" "e > 0"
+      and d: "\<And>p. \<lbrakk>path p; pathstart p = pathstart g; pathfinish p = pathfinish g; \<forall>t\<in>{0..1}. norm (p t - g t) < d\<rbrakk>
+            \<Longrightarrow> homotopic_paths (-{z}) g p"
+      and e: "\<And>q. \<lbrakk>path q; pathstart q = pathstart h; pathfinish q = pathfinish h; \<forall>t\<in>{0..1}. norm (q t - h t) < e\<rbrakk>
+            \<Longrightarrow> 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 \<notin> path_image p"
+       "pathstart p = pathstart g" "pathfinish p = pathfinish g"
+       and gp_less:"\<forall>t\<in>{0..1}. cmod (g t - p t) < d"
+       and pap: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number g z"
+    using winding_number [OF \<open>path g\<close> pag \<open>0 < d\<close>] unfolding winding_number_prop_def by blast
+  obtain q where q:
+       "valid_path q" "z \<notin> path_image q"
+       "pathstart q = pathstart h" "pathfinish q = pathfinish h"
+       and hq_less: "\<forall>t\<in>{0..1}. cmod (h t - q t) < e"
+       and paq:  "contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number h z"
+    using winding_number [OF \<open>path h\<close> pah \<open>0 < e\<close>] unfolding winding_number_prop_def by blast
+  have "homotopic_paths (- {z}) g p"
+    by (simp add: d p valid_path_imp_path norm_minus_commute gp_less)
+  moreover have "homotopic_paths (- {z}) h q"
+    by (simp add: e q valid_path_imp_path norm_minus_commute hq_less)
+  ultimately have "homotopic_paths (- {z}) p q"
+    by (blast intro: homotopic_paths_trans homotopic_paths_sym assms)
+  then have "contour_integral p (\<lambda>w. 1/(w - z)) = contour_integral q (\<lambda>w. 1/(w - z))"
+    by (rule Cauchy_theorem_homotopic_paths) (auto intro!: holomorphic_intros simp: p q)
+  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 \<notin> path_image g" and pah: "z \<notin> 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: "\<And>p. \<lbrakk>path p; pathfinish p = pathstart p; \<forall>t\<in>{0..1}. norm (p t - g t) < d\<rbrakk>
+            \<Longrightarrow> homotopic_loops (-{z}) g p"
+      and e: "\<And>q. \<lbrakk>path q; pathfinish q = pathstart q; \<forall>t\<in>{0..1}. norm (q t - h t) < e\<rbrakk>
+            \<Longrightarrow> 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 \<notin> path_image p"
+       "pathstart p = pathstart g" "pathfinish p = pathfinish g"
+       and gp_less:"\<forall>t\<in>{0..1}. cmod (g t - p t) < d"
+       and pap: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number g z"
+    using winding_number [OF \<open>path g\<close> pag \<open>0 < d\<close>] unfolding winding_number_prop_def by blast
+  obtain q where q:
+       "valid_path q" "z \<notin> path_image q"
+       "pathstart q = pathstart h" "pathfinish q = pathfinish h"
+       and hq_less: "\<forall>t\<in>{0..1}. cmod (h t - q t) < e"
+       and paq:  "contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number h z"
+    using winding_number [OF \<open>path h\<close> pah \<open>0 < e\<close>] unfolding winding_number_prop_def 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 (\<lambda>w. 1/(w - z)) = contour_integral q (\<lambda>w. 1/(w - z))"
+  proof (rule Cauchy_theorem_homotopic_loops)
+    show "homotopic_loops (- {z}) p q"
+      by (blast intro: homotopic_loops_trans homotopic_loops_sym gp hq assms)
+  qed (auto intro!: holomorphic_intros simp: p q)
+  then show ?thesis
+    by (simp add: pap paq)
+qed
+
+lemma winding_number_paths_linear_eq:
+  "\<lbrakk>path g; path h; pathstart h = pathstart g; pathfinish h = pathfinish g;
+    \<And>t. t \<in> {0..1} \<Longrightarrow> z \<notin> closed_segment (g t) (h t)\<rbrakk>
+        \<Longrightarrow> winding_number h z = winding_number g z"
+  by (blast intro: sym homotopic_paths_linear winding_number_homotopic_paths)
+
+lemma winding_number_loops_linear_eq:
+  "\<lbrakk>path g; path h; pathfinish g = pathstart g; pathfinish h = pathstart h;
+    \<And>t. t \<in> {0..1} \<Longrightarrow> z \<notin> closed_segment (g t) (h t)\<rbrakk>
+        \<Longrightarrow> winding_number h z = winding_number g z"
+  by (blast intro: sym homotopic_loops_linear winding_number_homotopic_loops)
+
+lemma winding_number_nearby_paths_eq:
+     "\<lbrakk>path g; path h; pathstart h = pathstart g; pathfinish h = pathfinish g;
+      \<And>t. t \<in> {0..1} \<Longrightarrow> norm(h t - g t) < norm(g t - z)\<rbrakk>
+      \<Longrightarrow> 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:
+     "\<lbrakk>path g; path h; pathfinish g = pathstart g; pathfinish h = pathstart h;
+      \<And>t. t \<in> {0..1} \<Longrightarrow> norm(h t - g t) < norm(g t - z)\<rbrakk>
+      \<Longrightarrow> winding_number h z = winding_number g z"
+  by (metis segment_bound(2) norm_minus_commute not_le winding_number_loops_linear_eq)
+
+
+lemma winding_number_subpath_combine:
+    "\<lbrakk>path g; z \<notin> path_image g;
+      u \<in> {0..1}; v \<in> {0..1}; w \<in> {0..1}\<rbrakk>
+      \<Longrightarrow> 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]])
+  using path_image_subpath_subset by auto
+
+subsection\<open>Partial circle path\<close>
+
+definition\<^marker>\<open>tag important\<close> part_circlepath :: "[complex, real, real, real, real] \<Rightarrow> complex"
+  where "part_circlepath z r s t \<equiv> \<lambda>x. z + of_real r * exp (\<i> * of_real (linepath s t x))"
+
+lemma pathstart_part_circlepath [simp]:
+     "pathstart(part_circlepath z r s t) = z + r*exp(\<i> * 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(\<i>*t)"
+by (metis part_circlepath_def pathfinish_def pathfinish_linepath)
+
+lemma reversepath_part_circlepath[simp]:
+    "reversepath (part_circlepath z r s t) = part_circlepath z r t s"
+  unfolding part_circlepath_def reversepath_def linepath_def 
+  by (auto simp:algebra_simps)
+    
+lemma has_vector_derivative_part_circlepath [derivative_intros]:
+    "((part_circlepath z r s t) has_vector_derivative
+      (\<i> * r * (of_real t - of_real s) * exp(\<i> * 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_field)
+  apply (rule derivative_eq_intros | simp)+
+  done
+
+lemma differentiable_part_circlepath:
+  "part_circlepath c r a b differentiable at x within A"
+  using has_vector_derivative_part_circlepath[of c r a b x A] differentiableI_vector by blast
+
+lemma vector_derivative_part_circlepath:
+    "vector_derivative (part_circlepath z r s t) (at x) =
+       \<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)"
+  using has_vector_derivative_part_circlepath vector_derivative_at by blast
+
+lemma vector_derivative_part_circlepath01:
+    "\<lbrakk>0 \<le> x; x \<le> 1\<rbrakk>
+     \<Longrightarrow> vector_derivative (part_circlepath z r s t) (at x within {0..1}) =
+          \<i> * r * (of_real t - of_real s) * exp(\<i> * 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 \<le> t"
+    shows "path_image (part_circlepath z r s t) = {z + r * exp(\<i> * of_real x) | x. s \<le> x \<and> x \<le> t}"
+proof -
+  { fix z::real
+    assume "0 \<le> z" "z \<le> 1"
+    with \<open>s \<le> t\<close> have "\<exists>x. (exp (\<i> * linepath s t z) = exp (\<i> * of_real x)) \<and> s \<le> x \<and> x \<le> 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 \<le> z" "z \<le> t"
+    then have "z + of_real r * exp (\<i> * of_real z) \<in> (\<lambda>x. z + of_real r * exp (\<i> * 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: field_split_simps)
+      done
+  }
+  ultimately show ?thesis
+    by (fastforce simp add: path_image_def part_circlepath_def)
+qed
+
+lemma path_image_part_circlepath':
+  "path_image (part_circlepath z r s t) = (\<lambda>x. z + r * cis x) ` closed_segment s t"
+proof -
+  have "path_image (part_circlepath z r s t) = 
+          (\<lambda>x. z + r * exp(\<i> * of_real x)) ` linepath s t ` {0..1}"
+    by (simp add: image_image path_image_def part_circlepath_def)
+  also have "linepath s t ` {0..1} = closed_segment s t"
+    by (rule linepath_image_01)
+  finally show ?thesis by (simp add: cis_conv_exp)
+qed
+
+lemma path_image_part_circlepath_subset:
+    "\<lbrakk>s \<le> t; 0 \<le> r\<rbrakk> \<Longrightarrow> path_image(part_circlepath z r s t) \<subseteq> sphere z r"
+by (auto simp: path_image_part_circlepath sphere_def dist_norm algebra_simps norm_mult)
+
+lemma in_path_image_part_circlepath:
+  assumes "w \<in> path_image(part_circlepath z r s t)" "s \<le> t" "0 \<le> r"
+    shows "norm(w - z) = r"
+proof -
+  have "w \<in> {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
+
+lemma path_image_part_circlepath_subset':
+  assumes "r \<ge> 0"
+  shows   "path_image (part_circlepath z r s t) \<subseteq> sphere z r"
+proof (cases "s \<le> t")
+  case True
+  thus ?thesis using path_image_part_circlepath_subset[of s t r z] assms by simp
+next
+  case False
+  thus ?thesis using path_image_part_circlepath_subset[of t s r z] assms
+    by (subst reversepath_part_circlepath [symmetric], subst path_image_reversepath) simp_all
+qed
+
+lemma part_circlepath_cnj: "cnj (part_circlepath c r a b x) = part_circlepath (cnj c) r (-a) (-b) x"
+  by (simp add: part_circlepath_def exp_cnj linepath_def algebra_simps)
+
+lemma contour_integral_bound_part_circlepath:
+  assumes "f contour_integrable_on part_circlepath c r a b"
+  assumes "B \<ge> 0" "r \<ge> 0" "\<And>x. x \<in> path_image (part_circlepath c r a b) \<Longrightarrow> norm (f x) \<le> B"
+  shows   "norm (contour_integral (part_circlepath c r a b) f) \<le> B * r * \<bar>b - a\<bar>"
+proof -
+  let ?I = "integral {0..1} (\<lambda>x. f (part_circlepath c r a b x) * \<i> * of_real (r * (b - a)) *
+              exp (\<i> * linepath a b x))"
+  have "norm ?I \<le> integral {0..1} (\<lambda>x::real. B * 1 * (r * \<bar>b - a\<bar>) * 1)"
+  proof (rule integral_norm_bound_integral, goal_cases)
+    case 1
+    with assms(1) show ?case
+      by (simp add: contour_integrable_on vector_derivative_part_circlepath mult_ac)
+  next
+    case (3 x)
+    with assms(2-) show ?case unfolding norm_mult norm_of_real abs_mult
+      by (intro mult_mono) (auto simp: path_image_def)
+  qed auto
+  also have "?I = contour_integral (part_circlepath c r a b) f"
+    by (simp add: contour_integral_integral vector_derivative_part_circlepath mult_ac)
+  finally show ?thesis by simp
+qed
+
+lemma has_contour_integral_part_circlepath_iff:
+  assumes "a < b"
+  shows "(f has_contour_integral I) (part_circlepath c r a b) \<longleftrightarrow>
+           ((\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) has_integral I) {a..b}"
+proof -
+  have "(f has_contour_integral I) (part_circlepath c r a b) \<longleftrightarrow>
+          ((\<lambda>x. f (part_circlepath c r a b x) * vector_derivative (part_circlepath c r a b)
+           (at x within {0..1})) has_integral I) {0..1}"
+    unfolding has_contour_integral_def ..
+  also have "\<dots> \<longleftrightarrow> ((\<lambda>x. f (part_circlepath c r a b x) * r * (b - a) * \<i> *
+                            cis (linepath a b x)) has_integral I) {0..1}"
+    by (intro has_integral_cong, subst vector_derivative_part_circlepath01)
+       (simp_all add: cis_conv_exp)
+  also have "\<dots> \<longleftrightarrow> ((\<lambda>x. f (c + r * exp (\<i> * linepath (of_real a) (of_real b) x)) *
+                       r * \<i> * exp (\<i> * linepath (of_real a) (of_real b) x) *
+                       vector_derivative (linepath (of_real a) (of_real b)) 
+                         (at x within {0..1})) has_integral I) {0..1}"
+    by (intro has_integral_cong, subst vector_derivative_linepath_within)
+       (auto simp: part_circlepath_def cis_conv_exp of_real_linepath [symmetric])
+  also have "\<dots> \<longleftrightarrow> ((\<lambda>z. f (c + r * exp (\<i> * z)) * r * \<i> * exp (\<i> * z)) has_contour_integral I)
+                      (linepath (of_real a) (of_real b))"
+    by (simp add: has_contour_integral_def)
+  also have "\<dots> \<longleftrightarrow> ((\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) has_integral I) {a..b}" using assms
+    by (subst has_contour_integral_linepath_Reals_iff) (simp_all add: cis_conv_exp)
+  finally show ?thesis .
+qed
+
+lemma contour_integrable_part_circlepath_iff:
+  assumes "a < b"
+  shows "f contour_integrable_on (part_circlepath c r a b) \<longleftrightarrow>
+           (\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) integrable_on {a..b}"
+  using assms by (auto simp: contour_integrable_on_def integrable_on_def 
+                             has_contour_integral_part_circlepath_iff)
+
+lemma contour_integral_part_circlepath_eq:
+  assumes "a < b"
+  shows "contour_integral (part_circlepath c r a b) f =
+           integral {a..b} (\<lambda>t. f (c + r * cis t) * r * \<i> * cis t)"
+proof (cases "f contour_integrable_on part_circlepath c r a b")
+  case True
+  hence "(\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) integrable_on {a..b}" 
+    using assms by (simp add: contour_integrable_part_circlepath_iff)
+  with True show ?thesis
+    using has_contour_integral_part_circlepath_iff[OF assms]
+          contour_integral_unique has_integral_integrable_integral by blast
+next
+  case False
+  hence "\<not>(\<lambda>t. f (c + r * cis t) * r * \<i> * cis t) integrable_on {a..b}" 
+    using assms by (simp add: contour_integrable_part_circlepath_iff)
+  with False show ?thesis
+    by (simp add: not_integrable_contour_integral not_integrable_integral)
+qed
+
+lemma contour_integral_part_circlepath_reverse:
+  "contour_integral (part_circlepath c r a b) f = -contour_integral (part_circlepath c r b a) f"
+  by (subst reversepath_part_circlepath [symmetric], subst contour_integral_reversepath) simp_all
+
+lemma contour_integral_part_circlepath_reverse':
+  "b < a \<Longrightarrow> contour_integral (part_circlepath c r a b) f = 
+               -contour_integral (part_circlepath c r b a) f"
+  by (rule contour_integral_part_circlepath_reverse)
+
+lemma finite_bounded_log: "finite {z::complex. norm z \<le> b \<and> 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) * \<i>) \<le> b + cmod (Ln w)}"
+    apply (simp add: norm_mult finite_int_iff_bounded_le)
+    apply (rule_tac x="\<lfloor>(b + cmod (Ln w)) / (2*pi)\<rfloor>" in exI)
+    apply (auto simp: field_split_simps le_floor_iff)
+    done
+  have [simp]: "\<And>P f. {z. P z \<and> (\<exists>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 \<noteq> 0"
+    shows "finite {z. norm z \<le> b \<and> exp(a*z) = w}"
+proof -
+  have *: "finite ((\<lambda>z. z / a) ` {z. cmod z \<le> b * cmod a \<and> exp z = w})"
+    by (rule finite_imageI [OF finite_bounded_log])
+  show ?thesis
+    by (rule finite_subset [OF _ *]) (force simp: assms norm_mult)
+qed
+
+lemma 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 \<le> B" "0 < r" "s \<le> t"
+      and B: "\<And>x. x \<in> path_image(part_circlepath z r s t) - k \<Longrightarrow> norm(f x) \<le> B"
+    shows "cmod i \<le> B * r * (t - s)"
+proof -
+  consider "s = t" | "s < t" using \<open>s \<le> t\<close> 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]: "\<bar>r\<bar> = r" using \<open>r > 0\<close> 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} \<inter> {0..1})" if "y \<in> k" for y
+    proof -
+      define w where "w = (y - z)/of_real r / exp(\<i> * of_real s)"
+      have fin: "finite (of_real -` {z. cmod z \<le> 1 \<and> exp (\<i> * complex_of_real (t - s) * z) = w})"
+        apply (rule finite_vimageI [OF finite_bounded_log2])
+        using \<open>s < t\<close> 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 \<inter> {0..1})"
+      by (rule finite_finite_vimage_IntI [OF \<open>finite k\<close>])
+    have **: "((\<lambda>x. if (part_circlepath z r s t x) \<in> 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}"
+      by (rule has_integral_spike [OF negligible_finite [OF fin01]])  (use fi has_contour_integral in auto)
+    have *: "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1; part_circlepath z r s t x \<notin> k\<rbrakk> \<Longrightarrow> cmod (f (part_circlepath z r s t x)) \<le> 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" \<open>r > 0\<close> by auto
+  qed
+qed
+
+lemma has_contour_integral_bound_part_circlepath:
+      "\<lbrakk>(f has_contour_integral i) (part_circlepath z r s t);
+        0 \<le> B; 0 < r; s \<le> t;
+        \<And>x. x \<in> path_image(part_circlepath z r s t) \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
+       \<Longrightarrow> norm i \<le> B*r*(t - s)"
+  by (auto intro: has_contour_integral_bound_part_circlepath_strong)
+
+lemma contour_integrable_continuous_part_circlepath:
+     "continuous_on (path_image (part_circlepath z r s t)) f
+      \<Longrightarrow> 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 \<notin> 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 \<open>0 < r\<close> 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 \<open>0 < r\<close> by auto
+qed
+
+lemma simple_path_part_circlepath:
+    "simple_path(part_circlepath z r s t) \<longleftrightarrow> (r \<noteq> 0 \<and> s \<noteq> t \<and> \<bar>s - t\<bar> \<le> 2*pi)"
+proof (cases "r = 0 \<or> s = t")
+  case True
+  then show ?thesis
+    unfolding part_circlepath_def simple_path_def
+    by (rule disjE) (force intro: bexI [where x = "1/4"] bexI [where x = "1/3"])+
+next
+  case False then have "r \<noteq> 0" "s \<noteq> t" by auto
+  have *: "\<And>x y z s t. \<i>*((1 - x) * s + x * t) = \<i>*(((1 - y) * s + y * t)) + z  \<longleftrightarrow> \<i>*(x - y) * (t - s) = z"
+    by (simp add: algebra_simps)
+  have abs01: "\<And>x y::real. 0 \<le> x \<and> x \<le> 1 \<and> 0 \<le> y \<and> y \<le> 1
+                      \<Longrightarrow> (x = y \<or> x = 0 \<and> y = 1 \<or> x = 1 \<and> y = 0 \<longleftrightarrow> \<bar>x - y\<bar> \<in> {0,1})"
+    by auto
+  have **: "\<And>x y. (\<exists>n. (complex_of_real x - of_real y) * (of_real t - of_real s) = 2 * (of_int n * of_real pi)) \<longleftrightarrow>
+                  (\<exists>n. \<bar>x - y\<bar> * (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: "\<bar>s - t\<bar> \<le> 2 * pi"
+    if "\<And>x. 0 \<le> x \<and> x \<le> 1 \<Longrightarrow> (\<exists>n. x * (t - s) = 2 * (real_of_int n * pi)) \<longrightarrow> x = 0 \<or> x = 1"
+  proof (rule ccontr)
+    assume "\<not> \<bar>s - t\<bar> \<le> 2 * pi"
+    then have *: "\<And>n. t - s \<noteq> of_int n * \<bar>s - t\<bar>"
+      using False that [of "2*pi / \<bar>t - s\<bar>"]
+      by (simp add: abs_minus_commute divide_simps)
+    show False
+      using * [of 1] * [of "-1"] by auto
+  qed
+  have 2: "\<bar>s - t\<bar> = \<bar>2 * (real_of_int n * pi) / x\<bar>" if "x \<noteq> 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
+  have abs_away: "\<And>P. (\<forall>x\<in>{0..1}. \<forall>y\<in>{0..1}. P \<bar>x - y\<bar>) \<longleftrightarrow> (\<forall>x::real. 0 \<le> x \<and> x \<le> 1 \<longrightarrow> P x)"
+    by force
+  show ?thesis using False
+    apply (simp add: simple_path_def)
+    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 field_split_simps abs_mult dest: of_int_leD)
+    done
+qed
+
+lemma arc_part_circlepath:
+  assumes "r \<noteq> 0" "s \<noteq> t" "\<bar>s - t\<bar> < 2*pi"
+    shows "arc (part_circlepath z r s t)"
+proof -
+  have *: "x = y" if eq: "\<i> * (linepath s t x) = \<i> * (linepath s t y) + 2 * of_int n * complex_of_real pi * \<i>"
+    and x: "x \<in> {0..1}" and y: "y \<in> {0..1}" for x y n
+  proof (rule ccontr)
+    assume "x \<noteq> y"
+    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_div_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])
+    with \<open>x \<noteq> y\<close> have st: "s-t = (of_int n * (pi * 2) / (y-x))"
+      by (force simp: field_simps)
+    have "\<bar>real_of_int n\<bar> < \<bar>y - x\<bar>"
+      using assms \<open>x \<noteq> y\<close> by (simp add: st abs_mult field_simps)
+    then show False
+      using assms x y st by (auto dest: of_int_lessD)
+  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\<open>Special case of one complete circle\<close>
+
+definition\<^marker>\<open>tag important\<close> circlepath :: "[complex, real, real] \<Rightarrow> complex"
+  where "circlepath z r \<equiv> part_circlepath z r 0 (2*pi)"
+
+lemma circlepath: "circlepath z r = (\<lambda>x. z + r * exp(2 * of_real pi * \<i> * 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 * \<i> * (x + 1/2)) =
+        z + of_real r * exp (2 * pi * \<i> * x + pi * \<i>)"
+    by (simp add: divide_simps) (simp add: algebra_simps)
+  also have "\<dots> = z - r * exp (2 * pi * \<i> * 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)) \<subseteq> path_image (circlepath z r)"
+  apply (simp add: path_image_def image_def circlepath_minus, clarify)
+  apply (case_tac "xa \<le> 1/2", force)
+  apply (force simp: 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
+
+lemma has_vector_derivative_circlepath [derivative_intros]:
+ "((circlepath z r) has_vector_derivative (2 * pi * \<i> * r * exp (2 * of_real pi * \<i> * 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
+
+lemma vector_derivative_circlepath:
+   "vector_derivative (circlepath z r) (at x) =
+    2 * pi * \<i> * r * exp(2 * of_real pi * \<i> * x)"
+using has_vector_derivative_circlepath vector_derivative_at by blast
+
+lemma vector_derivative_circlepath01:
+    "\<lbrakk>0 \<le> x; x \<le> 1\<rbrakk>
+     \<Longrightarrow> vector_derivative (circlepath z r) (at x within {0..1}) =
+          2 * pi * \<i> * r * exp(2 * of_real pi * \<i> * 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 \<le> r" shows "path_image (circlepath z r) = sphere z r"
+proof -
+  have *: "x \<in> (\<lambda>u. z + (cmod (x - z)) * exp (\<i> * (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
+    define w where "w = x - z"
+    then have "w \<noteq> 0" by (simp add: False)
+    have **: "\<And>t. \<lbrakk>Re w = cos t * cmod w; Im w = sin t * cmod w\<rbrakk> \<Longrightarrow> w = of_real (cmod w) * exp (\<i> * 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 \<open>w \<noteq> 0\<close> cmod_power2 [symmetric])
+      apply (rule_tac x="t / (2*pi)" in image_eqI)
+      apply (simp add: field_simps \<open>w \<noteq> 0\<close>)
+      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
+
+lemma path_image_circlepath [simp]:
+    "path_image (circlepath z r) = sphere z \<bar>r\<bar>"
+  using path_image_circlepath_minus
+  by (force simp: path_image_circlepath_nonneg abs_if)
+
+lemma has_contour_integral_bound_circlepath_strong:
+      "\<lbrakk>(f has_contour_integral i) (circlepath z r);
+        finite k; 0 \<le> B; 0 < r;
+        \<And>x. \<lbrakk>norm(x - z) = r; x \<notin> k\<rbrakk> \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
+        \<Longrightarrow> norm i \<le> 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)
+
+lemma has_contour_integral_bound_circlepath:
+      "\<lbrakk>(f has_contour_integral i) (circlepath z r);
+        0 \<le> B; 0 < r; \<And>x. norm(x - z) = r \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
+        \<Longrightarrow> norm i \<le> B*(2*pi*r)"
+  by (auto intro: has_contour_integral_bound_circlepath_strong)
+
+lemma contour_integrable_continuous_circlepath:
+    "continuous_on (path_image (circlepath z r)) f
+     \<Longrightarrow> 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) \<longleftrightarrow> (r \<noteq> 0)"
+  by (simp add: circlepath_def simple_path_part_circlepath)
+
+lemma notin_path_image_circlepath [simp]: "cmod (w - z) < r \<Longrightarrow> w \<notin> path_image (circlepath z r)"
+  by (simp add: sphere_def dist_norm norm_minus_commute)
+
+lemma contour_integral_circlepath:
+  assumes "r > 0"
+  shows "contour_integral (circlepath z r) (\<lambda>w. 1 / (w - z)) = 2 * complex_of_real pi * \<i>"
+proof (rule contour_integral_unique)
+  show "((\<lambda>w. 1 / (w - z)) has_contour_integral 2 * complex_of_real pi * \<i>) (circlepath z r)"
+    unfolding has_contour_integral_def using assms
+    apply (subst has_integral_cong)
+     apply (simp add: vector_derivative_circlepath01)
+    using has_integral_const_real [of _ 0 1] apply (force simp: circlepath)
+    done
+qed
+
+lemma winding_number_circlepath_centre: "0 < r \<Longrightarrow> 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
+  define r' where "r' = norm(w - z)"
+  have "r' < r"
+    by (simp add: assms r'_def)
+  have disjo: "cball z r' \<inter> sphere z r = {}"
+    using \<open>r' < r\<close> by (force simp: cball_def sphere_def)
+  have "winding_number(circlepath z r) w = winding_number(circlepath z r) z"
+  proof (rule winding_number_around_inside [where s = "cball z r'"])
+    show "winding_number (circlepath z r) z \<noteq> 0"
+      by (simp add: winding_number_circlepath_centre)
+    show "cball z r' \<inter> path_image (circlepath z r) = {}"
+      by (simp add: disjo less_eq_real_def)
+  qed (auto simp: r'_def dist_norm norm_minus_commute)
+  also have "\<dots> = 1"
+    by (simp add: winding_number_circlepath_centre)
+  finally show ?thesis .
+qed
+
+
+text\<open> Hence the Cauchy formula for points inside a circle.\<close>
+
+theorem Cauchy_integral_circlepath:
+  assumes contf: "continuous_on (cball z r) f" and holf: "f holomorphic_on (ball z r)" and wz: "norm(w - z) < r"
+  shows "((\<lambda>u. f u/(u - w)) has_contour_integral (2 * of_real pi * \<i> * f w))
+         (circlepath z r)"
+proof -
+  have "r > 0"
+    using assms le_less_trans norm_ge_zero by blast
+  have "((\<lambda>u. f u / (u - w)) has_contour_integral (2 * pi) * \<i> * winding_number (circlepath z r) w * f w)
+        (circlepath z r)"
+  proof (rule Cauchy_integral_formula_weak [where s = "cball z r" and k = "{}"])
+    show "\<And>x. x \<in> interior (cball z r) - {} \<Longrightarrow>
+         f field_differentiable at x"
+      using holf holomorphic_on_imp_differentiable_at by auto
+    have "w \<notin> sphere z r"
+      by simp (metis dist_commute dist_norm not_le order_refl wz)
+    then show "path_image (circlepath z r) \<subseteq> cball z r - {w}"
+      using \<open>r > 0\<close> by (auto simp add: cball_def sphere_def)
+  qed (use wz in \<open>simp_all add: dist_norm norm_minus_commute contf\<close>)
+  then show ?thesis
+    by (simp add: winding_number_circlepath assms)
+qed
+
+corollary\<^marker>\<open>tag unimportant\<close> Cauchy_integral_circlepath_simple:
+  assumes "f holomorphic_on cball z r" "norm(w - z) < r"
+  shows "((\<lambda>u. f u/(u - w)) has_contour_integral (2 * of_real pi * \<i> * 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 \<subseteq> s" "pathfinish g = pathstart g" "z \<notin> s"
+      and nb: "\<And>z. bounded (connected_component_set (- s) z) \<longrightarrow> z \<in> s"
+  shows "winding_number g z = 0"
+apply (rule winding_number_zero_in_outside)
+apply (simp_all add: assms)
+by (metis nb [of z] \<open>path_image g \<subseteq> s\<close> \<open>z \<notin> s\<close> contra_subsetD mem_Collect_eq outside outside_mono)
+
+lemma no_bounded_path_component_imp_winding_number_zero:
+  assumes g: "path g" "path_image g \<subseteq> s" "pathfinish g = pathstart g" "z \<notin> s"
+      and nb: "\<And>z. bounded (path_component_set (- s) z) \<longrightarrow> z \<in> 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\<open> Uniform convergence of path integral\<close>
+
+text\<open>Uniform convergence when the derivative of the path is bounded, and in particular for the special case of a circle.\<close>
+
+proposition contour_integral_uniform_limit:
+  assumes ev_fint: "eventually (\<lambda>n::'a. (f n) contour_integrable_on \<gamma>) F"
+      and ul_f: "uniform_limit (path_image \<gamma>) f l F"
+      and noleB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
+      and \<gamma>: "valid_path \<gamma>"
+      and [simp]: "\<not> trivial_limit F"
+  shows "l contour_integrable_on \<gamma>" "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
+proof -
+  have "0 \<le> 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 "0 < e / (\<bar>B\<bar> + 1)" by simp
+    then have "\<forall>\<^sub>F n in F. \<forall>x\<in>path_image \<gamma>. cmod (f n x - l x) < e / (\<bar>B\<bar> + 1)"
+      using ul_f [unfolded uniform_limit_iff dist_norm] by auto
+    with ev_fint
+    obtain a where fga: "\<And>x. x \<in> {0..1} \<Longrightarrow> cmod (f a (\<gamma> x) - l (\<gamma> x)) < e / (\<bar>B\<bar> + 1)"
+               and inta: "(\<lambda>t. f a (\<gamma> t) * vector_derivative \<gamma> (at t)) integrable_on {0..1}"
+      using eventually_happens [OF eventually_conj]
+      by (fastforce simp: contour_integrable_on path_image_def)
+    have Ble: "B * e / (\<bar>B\<bar> + 1) \<le> e"
+      using \<open>0 \<le> B\<close>  \<open>0 < e\<close> by (simp add: field_split_simps)
+    have "\<exists>h. (\<forall>x\<in>{0..1}. cmod (l (\<gamma> x) * vector_derivative \<gamma> (at x) - h x) \<le> e) \<and> h integrable_on {0..1}"
+    proof (intro exI conjI ballI)
+      show "cmod (l (\<gamma> x) * vector_derivative \<gamma> (at x) - f a (\<gamma> x) * vector_derivative \<gamma> (at x)) \<le> e"
+        if "x \<in> {0..1}" for x
+        apply (rule order_trans [OF _ Ble])
+        using noleB [OF that] fga [OF that] \<open>0 \<le> B\<close> \<open>0 < e\<close>
+        apply (simp add: norm_mult left_diff_distrib [symmetric] norm_minus_commute divide_simps)
+        apply (fastforce simp: mult_ac dest: mult_mono [OF less_imp_le])
+        done
+    qed (rule inta)
+  }
+  then show lintg: "l contour_integrable_on \<gamma>"
+    unfolding contour_integrable_on by (metis (mono_tags, lifting)integrable_uniform_limit_real)
+  { fix e::real
+    define B' where "B' = B + 1"
+    have B': "B' > 0" "B' > B" using  \<open>0 \<le> B\<close> by (auto simp: B'_def)
+    assume "0 < e"
+    then have ev_no': "\<forall>\<^sub>F n in F. \<forall>x\<in>path_image \<gamma>. 2 * cmod (f n x - l x) < e / B'"
+      using ul_f [unfolded uniform_limit_iff dist_norm, rule_format, of "e / B' / 2"] B'
+        by (simp add: field_simps)
+    have ie: "integral {0..1::real} (\<lambda>x. e / 2) < e" using \<open>0 < e\<close> by simp
+    have *: "cmod (f x (\<gamma> t) * vector_derivative \<gamma> (at t) - l (\<gamma> t) * vector_derivative \<gamma> (at t)) \<le> e / 2"
+             if t: "t\<in>{0..1}" and leB': "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) < e / B'" for x t
+    proof -
+      have "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) * cmod (vector_derivative \<gamma> (at t)) \<le> e * (B/ B')"
+        using mult_mono [OF less_imp_le [OF leB'] noleB] B' \<open>0 < e\<close> t by auto
+      also have "\<dots> < e"
+        by (simp add: B' \<open>0 < e\<close> mult_imp_div_pos_less)
+      finally have "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) * cmod (vector_derivative \<gamma> (at t)) < e" .
+      then show ?thesis
+        by (simp add: left_diff_distrib [symmetric] norm_mult)
+    qed
+    have le_e: "\<And>x. \<lbrakk>\<forall>xa\<in>{0..1}. 2 * cmod (f x (\<gamma> xa) - l (\<gamma> xa)) < e / B'; f x contour_integrable_on \<gamma>\<rbrakk>
+         \<Longrightarrow> cmod (integral {0..1}
+                    (\<lambda>u. f x (\<gamma> u) * vector_derivative \<gamma> (at u) - l (\<gamma> u) * vector_derivative \<gamma> (at u))) < e"
+      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
+    have "\<forall>\<^sub>F x in F. dist (contour_integral \<gamma> (f x)) (contour_integral \<gamma> 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] le_e)
+      done
+  }
+  then show "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
+    by (rule tendstoI)
+qed
+
+corollary\<^marker>\<open>tag unimportant\<close> contour_integral_uniform_limit_circlepath:
+  assumes "\<forall>\<^sub>F n::'a in F. (f n) contour_integrable_on (circlepath z r)"
+      and "uniform_limit (sphere z r) f l F"
+      and "\<not> trivial_limit F" "0 < r"
+    shows "l contour_integrable_on (circlepath z r)"
+          "((\<lambda>n. contour_integral (circlepath z r) (f n)) \<longlongrightarrow> contour_integral (circlepath z r) l) F"
+  using assms by (auto simp: vector_derivative_circlepath norm_mult intro!: contour_integral_uniform_limit)
+
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>General stepping result for derivative formulas\<close>
+
+lemma Cauchy_next_derivative:
+  assumes "continuous_on (path_image \<gamma>) f'"
+      and leB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
+      and int: "\<And>w. w \<in> s - path_image \<gamma> \<Longrightarrow> ((\<lambda>u. f' u / (u - w)^k) has_contour_integral f w) \<gamma>"
+      and k: "k \<noteq> 0"
+      and "open s"
+      and \<gamma>: "valid_path \<gamma>"
+      and w: "w \<in> s - path_image \<gamma>"
+    shows "(\<lambda>u. f' u / (u - w)^(Suc k)) contour_integrable_on \<gamma>"
+      and "(f has_field_derivative (k * contour_integral \<gamma> (\<lambda>u. f' u/(u - w)^(Suc k))))
+           (at w)"  (is "?thes2")
+proof -
+  have "open (s - path_image \<gamma>)" using \<open>open s\<close> closed_valid_path_image \<gamma> by blast
+  then obtain d where "d>0" and d: "ball w d \<subseteq> s - path_image \<gamma>" using w
+    using open_contains_ball by blast
+  have [simp]: "\<And>n. cmod (1 + of_nat n) = 1 + of_nat n"
+    by (metis norm_of_nat of_nat_Suc)
+  have cint: "\<And>x. \<lbrakk>x \<noteq> w; cmod (x - w) < d\<rbrakk>
+         \<Longrightarrow> (\<lambda>z. (f' z / (z - x) ^ k - f' z / (z - w) ^ k) / (x * k - w * k)) contour_integrable_on \<gamma>"
+    apply (rule contour_integrable_div [OF contour_integrable_diff])
+    using int w d
+    by (force simp: dist_norm norm_minus_commute intro!: has_contour_integral_integrable)+
+  have 1: "\<forall>\<^sub>F n in at w. (\<lambda>x. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k)
+                         contour_integrable_on \<gamma>"
+    unfolding eventually_at
+    apply (rule_tac x=d in exI)
+    apply (simp add: \<open>d > 0\<close> dist_norm field_simps cint)
+    done
+  have bim_g: "bounded (image f' (path_image \<gamma>))"
+    by (simp add: compact_imp_bounded compact_continuous_image compact_valid_path_image assms)
+  then obtain C where "C > 0" and C: "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> cmod (f' (\<gamma> x)) \<le> C"
+    by (force simp: bounded_pos path_image_def)
+  have twom: "\<forall>\<^sub>F n in at w.
+               \<forall>x\<in>path_image \<gamma>.
+                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 \<in> path_image \<gamma>" and "u \<noteq> 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 -
+      define ff where [abs_def]:
+        "ff n 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))" for n :: nat and w
+      have km1: "\<And>z::complex. z \<noteq> 0 \<Longrightarrow> z ^ (k - Suc 0) = z ^ k / z"
+        by (simp add: field_simps) (metis Suc_pred \<open>k \<noteq> 0\<close> neq0_conv power_Suc)
+      have ff1: "(ff i has_field_derivative ff (Suc i) z) (at z within ball w (d/2))"
+              if "z \<in> ball w (d/2)" "i \<le> 1" for i z
+      proof -
+        have "z \<notin> path_image \<gamma>"
+          using \<open>x \<in> path_image \<gamma>\<close> d that ball_divide_subset_numeral by blast
+        then have xz[simp]: "x \<noteq> z" using \<open>x \<in> path_image \<gamma>\<close> by blast
+        then have neq: "x * x + z * z \<noteq> x * (z * 2)"
+          by (blast intro: dest!: sum_sqs_eq)
+        with xz have "\<And>v. v \<noteq> 0 \<Longrightarrow> (x * x + z * z) * v \<noteq> (x * (z * 2) * v)" by auto
+        then have neqq: "\<And>v. v \<noteq> 0 \<Longrightarrow> x * (x * v) + z * (z * v) \<noteq> x * (z * (2 * v))"
+          by (simp add: algebra_simps)
+        show ?thesis using \<open>i \<le> 1\<close>
+          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) \<le> k * (1 + real k) * (4 / b) \<longleftrightarrow> b \<le> 4 * a"
+          by (subst mult_le_cancel_left_pos)
+            (use \<open>k \<noteq> 0\<close> in \<open>auto simp: divide_simps\<close>)
+        with ab have "real k * (1 + real k) / a \<le> (real k * 4 + real k * real k * 4) / b \<longleftrightarrow> b \<le> 4 * a"
+          by (simp add: field_simps)
+      } note canc = this
+      have ff2: "cmod (ff (Suc 1) v) \<le> real (k * (k + 1)) / (d/2) ^ (k + 2)"
+                if "v \<in> ball w (d/2)" for v
+      proof -
+        have lessd: "\<And>z. cmod (\<gamma> z - v) < d/2 \<Longrightarrow> cmod (w - \<gamma> z) < d"
+          by (metis that norm_minus_commute norm_triangle_half_r dist_norm mem_ball)
+        have "d/2 \<le> cmod (x - v)" using d x that
+          using lessd d x
+          by (auto simp add: dist_norm path_image_def ball_def not_less [symmetric] del: divide_const_simps)
+        then have "d \<le> cmod (x - v) * 2"
+          by (simp add: field_split_simps)
+        then have dpow_le: "d ^ (k+2) \<le> (cmod (x - v) * 2) ^ (k+2)"
+          using \<open>0 < d\<close> order_less_imp_le power_mono by blast
+        have "x \<noteq> v" using that
+          using \<open>x \<in> path_image \<gamma>\<close> ball_divide_subset_numeral d by fastforce
+        then show ?thesis
+        using \<open>d > 0\<close> apply (simp add: ff_def norm_mult norm_divide norm_power dist_norm canc)
+        using dpow_le apply (simp add: field_split_simps)
+        done
+      qed
+      have ub: "u \<in> 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)))
+                  \<le> (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 \<open>0 < d\<close>)
+      then have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
+                  \<le> (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)
+                  \<le> (1 + real k) * cmod (u - w) / (d/2) ^ (k+2)"
+        using \<open>k \<noteq> 0\<close> \<open>u \<noteq> w\<close> by (simp add: mult_ac zero_less_mult_iff pos_divide_le_eq)
+      also have "\<dots> < e"
+        using uw_less \<open>0 < d\<close> 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 \<noteq> u"
+        using uwd \<open>0 < d\<close> x d by (force simp: dist_norm ball_def norm_minus_commute)
+      show ?thesis
+        apply (rule le_less_trans [OF _ e])
+        using \<open>k \<noteq> 0\<close> \<open>x \<noteq> u\<close> \<open>u \<noteq> w\<close>
+        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: \<open>d > 0\<close> dist_norm that simp del: power_Suc intro: *)
+      done
+  qed
+  have 2: "uniform_limit (path_image \<gamma>) (\<lambda>n x. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k) (\<lambda>x. f' x / (x - w) ^ Suc k) (at w)"
+    unfolding uniform_limit_iff dist_norm
+  proof clarify
+    fix e::real
+    assume "0 < e"
+    have *: "cmod (f' (\<gamma> x) * (inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
+                        f' (\<gamma> x) / ((\<gamma> x - w) * (\<gamma> x - w) ^ k)) < e"
+              if ec: "cmod ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
+                      inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k) < e / C"
+                 and x: "0 \<le> x" "x \<le> 1"
+              for u x
+    proof (cases "(f' (\<gamma> x)) = 0")
+      case True then show ?thesis by (simp add: \<open>0 < e\<close>)
+    next
+      case False
+      have "cmod (f' (\<gamma> x) * (inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
+                        f' (\<gamma> x) / ((\<gamma> x - w) * (\<gamma> x - w) ^ k)) =
+            cmod (f' (\<gamma> x) * ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
+                             inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k))"
+        by (simp add: field_simps)
+      also have "\<dots> = cmod (f' (\<gamma> x)) *
+                       cmod ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
+                             inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k)"
+        by (simp add: norm_mult)
+      also have "\<dots> < cmod (f' (\<gamma> x)) * (e/C)"
+        using False mult_strict_left_mono [OF ec] by force
+      also have "\<dots> \<le> e" using C
+        by (metis False \<open>0 < e\<close> frac_le less_eq_real_def mult.commute pos_le_divide_eq x zero_less_norm_iff)
+      finally show ?thesis .
+    qed
+    show "\<forall>\<^sub>F n in at w.
+              \<forall>x\<in>path_image \<gamma>.
+               cmod (f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k - f' x / (x - w) ^ Suc k) < e"
+      using twom [OF divide_pos_pos [OF \<open>0 < e\<close> \<open>C > 0\<close>]]   unfolding path_image_def
+      by (force intro: * elim: eventually_mono)
+  qed
+  show "(\<lambda>u. f' u / (u - w) ^ (Suc k)) contour_integrable_on \<gamma>"
+    by (rule contour_integral_uniform_limit [OF 1 2 leB \<gamma>]) auto
+  have *: "(\<lambda>n. contour_integral \<gamma> (\<lambda>x. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / k))
+           \<midarrow>w\<rightarrow> contour_integral \<gamma> (\<lambda>u. f' u / (u - w) ^ (Suc k))"
+    by (rule contour_integral_uniform_limit [OF 1 2 leB \<gamma>]) auto
+  have **: "contour_integral \<gamma> (\<lambda>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
+  proof -
+    have u: "u \<in> s - path_image \<gamma>"
+      by (metis subsetD d dist_commute mem_ball that)
+    show ?thesis
+      apply (rule contour_integral_unique)
+      apply (simp add: diff_divide_distrib algebra_simps)
+      apply (intro has_contour_integral_diff has_contour_integral_div)
+      using u w apply (simp_all add: field_simps int)
+      done
+  qed
+  show ?thes2
+    apply (simp add: has_field_derivative_iff del: power_Suc)
+    apply (rule Lim_transform_within [OF tendsto_mult_left [OF *] \<open>0 < d\<close> ])
+    apply (simp add: \<open>k \<noteq> 0\<close> **)
+    done
+qed
+
+lemma Cauchy_next_derivative_circlepath:
+  assumes contf: "continuous_on (path_image (circlepath z r)) f"
+      and int: "\<And>w. w \<in> ball z r \<Longrightarrow> ((\<lambda>u. f u / (u - w)^k) has_contour_integral g w) (circlepath z r)"
+      and k: "k \<noteq> 0"
+      and w: "w \<in> ball z r"
+    shows "(\<lambda>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) (\<lambda>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 \<in> 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 * \<bar>r\<bar>"];
+        auto simp: vector_derivative_circlepath norm_mult)+
+qed
+
+
+text\<open> In particular, the first derivative formula.\<close>
+
+lemma Cauchy_derivative_integral_circlepath:
+  assumes contf: "continuous_on (cball z r) f"
+      and holf: "f holomorphic_on ball z r"
+      and w: "w \<in> ball z r"
+    shows "(\<lambda>u. f u/(u - w)^2) contour_integrable_on (circlepath z r)"
+           (is "?thes1")
+      and "(f has_field_derivative (1 / (2 * of_real pi * \<i>) * contour_integral(circlepath z r) (\<lambda>u. f u / (u - w)^2))) (at w)"
+           (is "?thes2")
+proof -
+  have [simp]: "r \<ge> 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: cball_def sphere_def)
+  have int: "\<And>w. dist z w < r \<Longrightarrow>
+                 ((\<lambda>u. f u / (u - w)) has_contour_integral (\<lambda>x. 2 * of_real pi * \<i> * 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 "((\<lambda>x. 2 * of_real pi * \<i> * f x) has_field_derivative contour_integral (circlepath z r) (\<lambda>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 = "\<lambda>x. 2 * of_real pi * \<i> * f x", simplified])
+    apply (blast intro: int)
+    done
+  then have fder: "(f has_field_derivative contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)^2) / (2 * of_real pi * \<i>)) (at w)"
+    by (rule DERIV_cdivide [where f = "\<lambda>x. 2 * of_real pi * \<i> * f x" and c = "2 * of_real pi * \<i>", simplified])
+  show ?thes2
+    by simp (rule fder)
+qed
+
+subsection\<open>Existence of all higher derivatives\<close>
+
+proposition derivative_is_holomorphic:
+  assumes "open S"
+      and fder: "\<And>z. z \<in> S \<Longrightarrow> (f has_field_derivative f' z) (at z)"
+    shows "f' holomorphic_on S"
+proof -
+  have *: "\<exists>h. (f' has_field_derivative h) (at z)" if "z \<in> S" for z
+  proof -
+    obtain r where "r > 0" and r: "cball z r \<subseteq> S"
+      using open_contains_cball \<open>z \<in> S\<close> \<open>open S\<close> 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 \<open>r > 0\<close> by (force elim: holomorphic_on_subset [THEN holomorphic_on_imp_continuous_on])
+    then have contfpi: "continuous_on (path_image (circlepath z r)) (\<lambda>x. 1/(2 * of_real pi*\<i>) * 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 \<in> ball z r"
+      have intf: "(\<lambda>u. f u / (u - w)\<^sup>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 * \<i>) * contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>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) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>)"
+        using fder' ball_subset_cball r w by (force intro: DERIV_unique [OF fder])
+      have "((\<lambda>u. f u / (u - w)\<^sup>2 / (2 * of_real pi * \<i>)) has_contour_integral
+                contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>))
+                (circlepath z r)"
+        by (rule has_contour_integral_div [OF has_contour_integral_integral [OF intf]])
+      then have "((\<lambda>u. f u / (2 * of_real pi * \<i> * (u - w)\<^sup>2)) has_contour_integral
+                contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>))
+                (circlepath z r)"
+        by (simp add: algebra_simps)
+      then have "((\<lambda>u. f u / (2 * of_real pi * \<i> * (u - w)\<^sup>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: \<open>0 < r\<close> * dist_norm)
+      done
+  qed
+  show ?thesis
+    by (simp add: holomorphic_on_open [OF \<open>open S\<close>] *)
+qed
+
+lemma holomorphic_deriv [holomorphic_intros]:
+    "\<lbrakk>f holomorphic_on S; open S\<rbrakk> \<Longrightarrow> (deriv f) holomorphic_on S"
+by (metis DERIV_deriv_iff_field_differentiable at_within_open derivative_is_holomorphic holomorphic_on_def)
+
+lemma analytic_deriv [analytic_intros]: "f analytic_on S \<Longrightarrow> (deriv f) analytic_on S"
+  using analytic_on_holomorphic holomorphic_deriv by auto
+
+lemma holomorphic_higher_deriv [holomorphic_intros]: "\<lbrakk>f holomorphic_on S; open S\<rbrakk> \<Longrightarrow> (deriv ^^ n) f holomorphic_on S"
+  by (induction n) (auto simp: holomorphic_deriv)
+
+lemma analytic_higher_deriv [analytic_intros]: "f analytic_on S \<Longrightarrow> (deriv ^^ n) f analytic_on S"
+  unfolding analytic_on_def using holomorphic_higher_deriv by blast
+
+lemma has_field_derivative_higher_deriv:
+     "\<lbrakk>f holomorphic_on S; open S; x \<in> S\<rbrakk>
+      \<Longrightarrow> ((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 \<subseteq> S"
+  shows "valid_path (f \<circ> g)"
+proof (rule valid_path_compose[OF \<open>valid_path g\<close>])
+  fix x assume "x \<in> path_image g"
+  then show "f field_differentiable at x"
+    using analytic_on_imp_differentiable_at analytic_on_open assms holo by blast
+next
+  have "deriv f holomorphic_on S"
+    using holomorphic_deriv holo \<open>open S\<close> 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\<open>Morera's theorem\<close>
+
+lemma Morera_local_triangle_ball:
+  assumes "\<And>z. z \<in> S
+          \<Longrightarrow> \<exists>e a. 0 < e \<and> z \<in> ball a e \<and> continuous_on (ball a e) f \<and>
+                    (\<forall>b c. closed_segment b c \<subseteq> ball a e
+                           \<longrightarrow> 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 \<in> S"
+    with assms obtain e a where
+            "0 < e" and z: "z \<in> ball a e" and contf: "continuous_on (ball a e) f"
+        and 0: "\<And>b c. closed_segment b c \<subseteq> ball a e
+                      \<Longrightarrow> contour_integral (linepath a b) f +
+                          contour_integral (linepath b c) f +
+                          contour_integral (linepath c a) f = 0"
+      by blast
+    have az: "dist a z < e" using mem_ball z by blast
+    have sb_ball: "ball z (e - dist a z) \<subseteq> ball a e"
+      by (simp add: dist_commute ball_subset_ball_iff)
+    have "\<exists>e>0. f holomorphic_on ball z e"
+    proof (intro exI conjI)
+      have sub_ball: "\<And>y. dist a y < e \<Longrightarrow> closed_segment a y \<subseteq> ball a e"
+        by (meson \<open>0 < e\<close> centre_in_ball convex_ball convex_contains_segment mem_ball)
+      show "f holomorphic_on ball z (e - dist a z)"
+        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 \<open>0 < e\<close> sub_ball)
+        done
+    qed (simp add: az)
+  }
+  then show ?thesis
+    by (simp add: analytic_on_def)
+qed
+
+lemma Morera_local_triangle:
+  assumes "\<And>z. z \<in> S
+          \<Longrightarrow> \<exists>t. open t \<and> z \<in> t \<and> continuous_on t f \<and>
+                  (\<forall>a b c. convex hull {a,b,c} \<subseteq> t
+                              \<longrightarrow> 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 \<in> S"
+    with assms obtain t where
+            "open t" and z: "z \<in> t" and contf: "continuous_on t f"
+        and 0: "\<And>a b c. convex hull {a,b,c} \<subseteq> t
+                      \<Longrightarrow> 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 \<subseteq> 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 eq0: "\<And>b c. closed_segment b c \<subseteq> ball z e \<Longrightarrow>
+                         contour_integral (linepath z b) f +
+                         contour_integral (linepath b c) f +
+                         contour_integral (linepath c z) f = 0"
+      by (meson 0 z \<open>0 < e\<close> centre_in_ball closed_segment_subset convex_ball dual_order.trans e starlike_convex_subset)
+    have "\<exists>e a. 0 < e \<and> z \<in> ball a e \<and> continuous_on (ball a e) f \<and>
+                (\<forall>b c. closed_segment b c \<subseteq> ball a e \<longrightarrow>
+                       contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f = 0)"
+      using \<open>e > 0\<close> eq0 by force
+  }
+  then show ?thesis
+    by (simp add: Morera_local_triangle_ball)
+qed
+
+proposition Morera_triangle:
+    "\<lbrakk>continuous_on S f; open S;
+      \<And>a b c. convex hull {a,b,c} \<subseteq> S
+              \<longrightarrow> contour_integral (linepath a b) f +
+                  contour_integral (linepath b c) f +
+                  contour_integral (linepath c a) f = 0\<rbrakk>
+     \<Longrightarrow> f analytic_on S"
+  using Morera_local_triangle by blast
+
+subsection\<open>Combining theorems for higher derivatives including Leibniz rule\<close>
+
+lemma higher_deriv_linear [simp]:
+    "(deriv ^^ n) (\<lambda>w. c*w) = (\<lambda>z. if n = 0 then c*z else if n = 1 then c else 0)"
+  by (induction n) auto
+
+lemma higher_deriv_const [simp]: "(deriv ^^ n) (\<lambda>w. c) = (\<lambda>w. if n=0 then c else 0)"
+  by (induction n) auto
+
+lemma higher_deriv_ident [simp]:
+     "(deriv ^^ n) (\<lambda>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
+
+lemma 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:
+     "\<lbrakk>(f has_field_derivative 1) (at z); f z = z\<rbrakk> \<Longrightarrow> (f^^n has_field_derivative 1) (at z)"
+  apply (induction n, auto)
+  apply (simp add: id_def)
+  by (metis DERIV_chain comp_funpow comp_id funpow_swap1 mult.right_neutral)
+
+lemma higher_deriv_uminus:
+  assumes "f holomorphic_on S" "open S" and z: "z \<in> S"
+    shows "(deriv ^^ n) (\<lambda>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
+  have "((deriv ^^ n) (\<lambda>w. - f w) has_field_derivative - deriv ((deriv ^^ n) f) z) (at z)"
+    apply (rule has_field_derivative_transform_within_open [of "\<lambda>w. -((deriv ^^ n) f w)"])
+       apply (rule derivative_eq_intros | rule * refl assms)+
+     apply (auto simp add: Suc)
+    done
+  then show ?case
+    by (simp add: DERIV_imp_deriv)
+qed
+
+lemma higher_deriv_add:
+  fixes z::complex
+  assumes "f holomorphic_on S" "g holomorphic_on S" "open S" and z: "z \<in> S"
+    shows "(deriv ^^ n) (\<lambda>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
+  have "((deriv ^^ n) (\<lambda>w. f w + g w) has_field_derivative
+        deriv ((deriv ^^ n) f) z + deriv ((deriv ^^ n) g) z) (at z)"
+    apply (rule has_field_derivative_transform_within_open [of "\<lambda>w. (deriv ^^ n) f w + (deriv ^^ n) g w"])
+       apply (rule derivative_eq_intros | rule * refl assms)+
+     apply (auto simp add: Suc)
+    done
+  then show ?case
+    by (simp add: DERIV_imp_deriv)
+qed
+
+lemma higher_deriv_diff:
+  fixes z::complex
+  assumes "f holomorphic_on S" "g holomorphic_on S" "open S" and z: "z \<in> S"
+    shows "(deriv ^^ n) (\<lambda>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 (cases k) simp_all
+
+lemma higher_deriv_mult:
+  fixes z::complex
+  assumes "f holomorphic_on S" "g holomorphic_on S" "open S" and z: "z \<in> S"
+    shows "(deriv ^^ n) (\<lambda>w. f w * g w) z =
+           (\<Sum>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 *: "\<And>n. ((deriv ^^ n) f has_field_derivative deriv ((deriv ^^ n) f) z) (at z)"
+          "\<And>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: "(\<Sum>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 + (\<Sum>i = 0..n. (deriv ^^ i) f z * (of_nat (Suc n choose i) * (deriv ^^ (Suc n - i)) g z))"
+    apply (simp add: bb algebra_simps sum.distrib)
+    apply (subst (4) sum_Suc_reindex)
+    apply (auto simp: algebra_simps Suc_diff_le intro: sum.cong)
+    done
+  have "((deriv ^^ n) (\<lambda>w. f w * g w) has_field_derivative
+         (\<Sum>i = 0..Suc n. (Suc n choose i) * (deriv ^^ i) f z * (deriv ^^ (Suc n - i)) g z))
+        (at z)"
+    apply (rule has_field_derivative_transform_within_open
+        [of "\<lambda>w. (\<Sum>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_sum])
+        apply (rule DERIV_cmult)
+        apply (auto intro: DERIV_mult * sumeq \<open>open S\<close> Suc.prems Suc.IH [symmetric])
+    done
+  then show ?case
+    unfolding funpow.simps o_apply
+    by (simp add: DERIV_imp_deriv)
+qed
+
+lemma higher_deriv_transform_within_open:
+  fixes z::complex
+  assumes "f holomorphic_on S" "g holomorphic_on S" "open S" and z: "z \<in> S"
+      and fg: "\<And>w. w \<in> S \<Longrightarrow> 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)
+
+lemma higher_deriv_compose_linear:
+  fixes z::complex
+  assumes f: "f holomorphic_on T" and S: "open S" and T: "open T" and z: "z \<in> S"
+      and fg: "\<And>w. w \<in> S \<Longrightarrow> u * w \<in> T"
+    shows "(deriv ^^ n) (\<lambda>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 (*) u ` S"
+    by (meson fg f holomorphic_on_subset image_subset_iff)
+  have holo2: "(deriv ^^ n) f holomorphic_on (*) u ` S"
+    by (meson f fg holomorphic_higher_deriv holomorphic_on_subset image_subset_iff T)
+  have holo3: "(\<lambda>z. u ^ n * (deriv ^^ n) f (u * z)) holomorphic_on S"
+    by (intro holo2 holomorphic_on_compose [where g="(deriv ^^ n) f", unfolded o_def] holomorphic_intros)
+  have holo1: "(\<lambda>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 "deriv ((deriv ^^ n) (\<lambda>w. f (u * w))) z = deriv (\<lambda>z. u^n * (deriv ^^ n) f (u*z)) z"
+    apply (rule complex_derivative_transform_within_open [OF _ holo3 S Suc.prems])
+    apply (rule holomorphic_higher_deriv [OF holo1 S])
+    apply (simp add: Suc.IH)
+    done
+  also have "\<dots> = u^n * deriv (\<lambda>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)
+     apply (simp add: analytic_on_open f holomorphic_higher_deriv T)
+    apply (blast intro: fg)
+    done
+  also have "\<dots> = u * u ^ n * deriv ((deriv ^^ n) f) (u * z)"
+      apply (subst deriv_chain [where g = "(deriv ^^ n) f" and f = "(*) 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)
+      done
+  finally show ?case
+    by simp
+qed
+
+lemma higher_deriv_add_at:
+  assumes "f analytic_on {z}" "g analytic_on {z}"
+    shows "(deriv ^^ n) (\<lambda>w. f w + g w) z = (deriv ^^ n) f z + (deriv ^^ n) g z"
+proof -
+  have "f analytic_on {z} \<and> 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) (\<lambda>w. f w - g w) z = (deriv ^^ n) f z - (deriv ^^ n) g z"
+proof -
+  have "f analytic_on {z} \<and> 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}  \<Longrightarrow> (deriv ^^ n) (\<lambda>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) (\<lambda>w. f w * g w) z =
+           (\<Sum>i = 0..n. of_nat (n choose i) * (deriv ^^ i) f z * (deriv ^^ (n - i)) g z)"
+proof -
+  have "f analytic_on {z} \<and> g analytic_on {z}"
+    using assms by blast
+  with higher_deriv_mult show ?thesis
+    by (auto simp: analytic_at_two)
+qed
+
+
+text\<open> Nonexistence of isolated singularities and a stronger integral formula.\<close>
+
+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 \<in> S" and cdf: "\<And>x. x \<in> S - K \<Longrightarrow> f field_differentiable at x"
+    have "f field_differentiable at z"
+    proof (cases "z \<in> K")
+      case False then show ?thesis by (blast intro: cdf \<open>z \<in> S\<close>)
+    next
+      case True
+      with finite_set_avoid [OF K, of z]
+      obtain d where "d>0" and d: "\<And>x. \<lbrakk>x\<in>K; x \<noteq> z\<rbrakk> \<Longrightarrow> d \<le> dist z x"
+        by blast
+      obtain e where "e>0" and e: "ball z e \<subseteq> S"
+        using  S \<open>z \<in> S\<close> by (force simp: 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 cont: "{a,b,c} \<subseteq> ball z (min d e) \<Longrightarrow> continuous_on (convex hull {a, b, c}) f" for a b c
+        by (simp add: hull_minimal continuous_on_subset [OF fde])
+      have fd: "\<lbrakk>{a,b,c} \<subseteq> ball z (min d e); x \<in> interior (convex hull {a, b, c}) - K\<rbrakk>
+            \<Longrightarrow> f field_differentiable at x" for a b c x
+        by (metis cdf Diff_iff Int_iff ball_min_Int subsetD convex_ball e interior_mono interior_subset subset_hull)
+      obtain g where "\<And>w. w \<in> ball z (min d e) \<Longrightarrow> (g has_field_derivative f w) (at w within ball z (min d e))"
+        apply (rule contour_integral_convex_primitive
+                     [OF convex_ball fde Cauchy_theorem_triangle_cofinite [OF _ K]])
+        using cont fd by auto
+      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 \<open>0 < d\<close> \<open>0 < e\<close> 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
+
+lemma no_isolated_singularity':
+  fixes z::complex
+  assumes f: "\<And>z. z \<in> K \<Longrightarrow> (f \<longlongrightarrow> f z) (at z within S)"
+      and holf: "f holomorphic_on (S - K)" and S: "open S" and K: "finite K"
+    shows "f holomorphic_on S"
+proof (rule no_isolated_singularity[OF _ assms(2-)])
+  show "continuous_on S f" unfolding continuous_on_def
+  proof
+    fix z assume z: "z \<in> S"
+    show "(f \<longlongrightarrow> f z) (at z within S)"
+    proof (cases "z \<in> K")
+      case False
+      from holf have "continuous_on (S - K) f"
+        by (rule holomorphic_on_imp_continuous_on)
+      with z False have "(f \<longlongrightarrow> f z) (at z within (S - K))"
+        by (simp add: continuous_on_def)
+      also from z K S False have "at z within (S - K) = at z within S"
+        by (subst (1 2) at_within_open) (auto intro: finite_imp_closed)
+      finally show "(f \<longlongrightarrow> f z) (at z within S)" .
+    qed (insert assms z, simp_all)
+  qed
+qed
+
+proposition Cauchy_integral_formula_convex:
+  assumes S: "convex S" and K: "finite K" and contf: "continuous_on S f"
+    and fcd: "(\<And>x. x \<in> interior S - K \<Longrightarrow> f field_differentiable at x)"
+    and z: "z \<in> interior S" and vpg: "valid_path \<gamma>"
+    and pasz: "path_image \<gamma> \<subseteq> S - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
+  shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
+proof -
+  have *: "\<And>x. x \<in> interior S \<Longrightarrow> f field_differentiable at x"
+    unfolding holomorphic_on_open [symmetric] field_differentiable_def
+    using no_isolated_singularity [where S = "interior S"]
+    by (meson K contf continuous_at_imp_continuous_on continuous_on_interior fcd
+          field_differentiable_at_within field_differentiable_def holomorphic_onI
+          holomorphic_on_imp_differentiable_at open_interior)
+  show ?thesis
+    by (rule Cauchy_integral_formula_weak [OF S finite.emptyI contf]) (use * assms in auto)
+qed
+
+text\<open> Formula for higher derivatives.\<close>
+
+lemma 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 \<in> ball z r"
+    shows "((\<lambda>u. f u / (u - w) ^ (Suc k)) has_contour_integral ((2 * pi * \<i>) / (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: cball_def sphere_def less_imp_le)
+  obtain X where X: "((\<lambda>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) ((\<lambda>u. f u / (u - w) ^ Suc (Suc k))) = X"
+    by (rule contour_integral_unique)
+  have "\<And>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: "\<And>n. (deriv ^^ n) f field_differentiable (at w)"
+    by (force simp: field_differentiable_def)
+  have "deriv (\<lambda>w. complex_of_real (2 * pi) * \<i> / (fact k) * (deriv ^^ k) f w) w =
+          of_nat (Suc k) * contour_integral (circlepath z r) (\<lambda>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 "\<dots> = of_nat (Suc k) * X"
+    by (simp only: con)
+  finally have "deriv (\<lambda>w. ((2 * pi) * \<i> / (fact k)) * (deriv ^^ k) f w) w = of_nat (Suc k) * X" .
+  then have "((2 * pi) * \<i> / (fact k)) * deriv (\<lambda>w. (deriv ^^ k) f w) w = of_nat (Suc k) * X"
+    by (metis deriv_cmult dnf_diff)
+  then have "deriv (\<lambda>w. (deriv ^^ k) f w) w = of_nat (Suc k) * X / ((2 * pi) * \<i> / (fact k))"
+    by (simp add: field_simps)
+  then show ?case
+  using of_nat_eq_0_iff X by fastforce
+qed
+
+lemma Cauchy_higher_derivative_integral_circlepath:
+  assumes contf: "continuous_on (cball z r) f"
+      and holf: "f holomorphic_on ball z r"
+      and w: "w \<in> ball z r"
+    shows "(\<lambda>u. f u / (u - w)^(Suc k)) contour_integrable_on (circlepath z r)"
+           (is "?thes1")
+      and "(deriv ^^ k) f w = (fact k) / (2 * pi * \<i>) * contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k))"
+           (is "?thes2")
+proof -
+  have *: "((\<lambda>u. f u / (u - w) ^ Suc k) has_contour_integral (2 * pi) * \<i> / (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: field_split_simps)
+qed
+
+corollary Cauchy_contour_integral_circlepath:
+  assumes "continuous_on (cball z r) f" "f holomorphic_on ball z r" "w \<in> ball z r"
+    shows "contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k)) = (2 * pi * \<i>) * (deriv ^^ k) f w / (fact k)"
+by (simp add: Cauchy_higher_derivative_integral_circlepath [OF assms])
+
+lemma Cauchy_contour_integral_circlepath_2:
+  assumes "continuous_on (cball z r) f" "f holomorphic_on ball z r" "w \<in> ball z r"
+    shows "contour_integral(circlepath z r) (\<lambda>u. f u/(u - w)^2) = (2 * pi * \<i>) * deriv f w"
+  using Cauchy_contour_integral_circlepath [OF assms, of 1]
+  by (simp add: power2_eq_square)
+
+
+subsection\<open>A holomorphic function is analytic, i.e. has local power series\<close>
+
+theorem holomorphic_power_series:
+  assumes holf: "f holomorphic_on ball z r"
+      and w: "w \<in> ball z r"
+    shows "((\<lambda>n. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
+proof -
+  \<comment> \<open>Replacing \<^term>\<open>r\<close> and the original (weak) premises with stronger ones\<close>
+  obtain r where "r > 0" and holfc: "f holomorphic_on cball z r" and w: "w \<in> ball z r"
+  proof
+    have "cball z ((r + dist w z) / 2) \<subseteq> ball z r"
+      using w by (simp add: dist_commute field_sum_of_halves subset_eq)
+    then show "f holomorphic_on cball z ((r + dist w z) / 2)"
+      by (rule holomorphic_on_subset [OF holf])
+    have "r > 0"
+      using w by clarsimp (metis dist_norm le_less_trans norm_ge_zero)
+    then show "0 < (r + dist w z) / 2"
+      by simp (use zero_le_dist [of w z] in linarith)
+  qed (use w in \<open>auto simp: dist_commute\<close>)
+  then have holf: "f holomorphic_on ball z r"
+    using ball_subset_cball holomorphic_on_subset by blast
+  have contf: "continuous_on (cball z r) f"
+    by (simp add: holfc holomorphic_on_imp_continuous_on)
+  have cint: "\<And>k. (\<lambda>u. f u / (u - z) ^ Suc k) contour_integrable_on circlepath z r"
+    by (rule Cauchy_higher_derivative_integral_circlepath [OF contf holf]) (simp add: \<open>0 < r\<close>)
+  obtain B where "0 < B" and B: "\<And>u. u \<in> cball z r \<Longrightarrow> norm(f u) \<le> 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 \<le> r" and wz_eq: "norm(w - z) = r - k"
+             and kle: "\<And>u. norm(u - z) = r \<Longrightarrow> k \<le> norm(u - w)"
+  proof
+    show "\<And>u. cmod (u - z) = r \<Longrightarrow> r - dist z w \<le> cmod (u - w)"
+      by (metis add_diff_eq diff_add_cancel dist_norm norm_diff_ineq)
+  qed (use w in \<open>auto simp: dist_norm norm_minus_commute\<close>)
+  have ul: "uniform_limit (sphere z r) (\<lambda>n x. (\<Sum>k<n. (w - z) ^ k * (f x / (x - z) ^ Suc k))) (\<lambda>x. f x / (x - w)) sequentially"
+    unfolding uniform_limit_iff dist_norm
+  proof clarify
+    fix e::real
+    assume "0 < e"
+    have rr: "0 \<le> (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"] \<open>0 < e\<close> \<open>0 < B\<close> k by force
+    have "norm ((\<Sum>k<N. (w - z) ^ k * f u / (u - z) ^ Suc k) - f u / (u - w)) < e"
+         if "n \<le> 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  \<open>n \<le> N\<close> k by auto
+      have u [simp]: "(u \<noteq> z) \<and> (u \<noteq> w)"
+        using \<open>0 < r\<close> r w by auto
+      have wzu_not1: "(w - z) / (u - z) \<noteq> 1"
+        by (metis (no_types) dist_norm divide_eq_1_iff less_irrefl mem_ball norm_minus_commute r w)
+      have "norm ((\<Sum>k<N. (w - z) ^ k * f u / (u - z) ^ Suc k) * (u - w) - f u)
+            = norm ((\<Sum>k<N. (((w - z) / (u - z)) ^ k)) * f u * (u - w) / (u - z) - f u)"
+        unfolding sum_distrib_right sum_divide_distrib power_divide by (simp add: algebra_simps)
+      also have "\<dots> = norm ((((w - z) / (u - z)) ^ N - 1) * (u - w) / (((w - z) / (u - z) - 1) * (u - z)) - 1) * norm (f u)"
+        using \<open>0 < B\<close>
+        apply (auto simp: geometric_sum [OF wzu_not1])
+        apply (simp add: field_simps norm_mult [symmetric])
+        done
+      also have "\<dots> = norm ((u-z) ^ N * (w - u) - ((w - z) ^ N - (u-z) ^ N) * (u-w)) / (r ^ N * norm (u-w)) * norm (f u)"
+        using \<open>0 < r\<close> r by (simp add: divide_simps norm_mult norm_divide norm_power dist_norm norm_minus_commute)
+      also have "\<dots> = norm ((w - z) ^ N * (w - u)) / (r ^ N * norm (u - w)) * norm (f u)"
+        by (simp add: algebra_simps)
+      also have "\<dots> = norm (w - z) ^ N * norm (f u) / r ^ N"
+        by (simp add: norm_mult norm_power norm_minus_commute)
+      also have "\<dots> \<le> (((r - k)/r)^N) * B"
+        using \<open>0 < r\<close> 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 "\<dots> < e * k"
+        using \<open>0 < B\<close> N by (simp add: divide_simps)
+      also have "\<dots> \<le> e * norm (u - w)"
+        using r kle \<open>0 < e\<close> by (simp add: dist_commute dist_norm)
+      finally show ?thesis
+        by (simp add: field_split_simps norm_divide del: power_Suc)
+    qed
+    with \<open>0 < r\<close> show "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>sphere z r.
+                norm ((\<Sum>k<n. (w - z) ^ k * (f x / (x - z) ^ Suc k)) - f x / (x - w)) < e"
+      by (auto simp: mult_ac less_imp_le eventually_sequentially Ball_def)
+  qed
+  have eq: "\<forall>\<^sub>F x in sequentially.
+             contour_integral (circlepath z r) (\<lambda>u. \<Sum>k<x. (w - z) ^ k * (f u / (u - z) ^ Suc k)) =
+             (\<Sum>k<x. contour_integral (circlepath z r) (\<lambda>u. f u / (u - z) ^ Suc k) * (w - z) ^ k)"
+    apply (rule eventuallyI)
+    apply (subst contour_integral_sum, 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 cic: "\<And>u. (\<lambda>y. \<Sum>k<u. (w - z) ^ k * (f y / (y - z) ^ Suc k)) contour_integrable_on circlepath z r"
+    apply (intro contour_integrable_sum contour_integrable_lmul, simp)
+    using \<open>0 < r\<close> by (force intro!: Cauchy_higher_derivative_integral_circlepath [OF contf holf])
+  have "(\<lambda>k. contour_integral (circlepath z r) (\<lambda>u. f u/(u - z)^(Suc k)) * (w - z)^k)
+        sums contour_integral (circlepath z r) (\<lambda>u. f u/(u - w))"
+    unfolding sums_def
+    apply (intro Lim_transform_eventually [OF _ eq] contour_integral_uniform_limit_circlepath [OF eventuallyI ul] cic)
+    using \<open>0 < r\<close> apply auto
+    done
+  then have "(\<lambda>k. contour_integral (circlepath z r) (\<lambda>u. f u/(u - z)^(Suc k)) * (w - z)^k)
+             sums (2 * of_real pi * \<i> * f w)"
+    using w by (auto simp: dist_commute dist_norm contour_integral_unique [OF Cauchy_integral_circlepath_simple [OF holfc]])
+  then have "(\<lambda>k. contour_integral (circlepath z r) (\<lambda>u. f u / (u - z) ^ Suc k) * (w - z)^k / (\<i> * (of_real pi * 2)))
+            sums ((2 * of_real pi * \<i> * f w) / (\<i> * (complex_of_real pi * 2)))"
+    by (rule sums_divide)
+  then have "(\<lambda>n. (w - z) ^ n * contour_integral (circlepath z r) (\<lambda>u. f u / (u - z) ^ Suc n) / (\<i> * (of_real pi * 2)))
+            sums f w"
+    by (simp add: field_simps)
+  then show ?thesis
+    by (simp add: field_simps \<open>0 < r\<close> Cauchy_higher_derivative_integral_circlepath [OF contf holf])
+qed
+
+
+subsection\<open>The Liouville theorem and the Fundamental Theorem of Algebra\<close>
+
+text\<open> These weak Liouville versions don't even need the derivative formula.\<close>
+
+lemma Liouville_weak_0:
+  assumes holf: "f holomorphic_on UNIV" and inf: "(f \<longlongrightarrow> 0) at_infinity"
+    shows "f z = 0"
+proof (rule ccontr)
+  assume fz: "f z \<noteq> 0"
+  with inf [unfolded Lim_at_infinity, rule_format, of "norm(f z)/2"]
+  obtain B where B: "\<And>x. B \<le> cmod x \<Longrightarrow> norm (f x) * 2 < cmod (f z)"
+    by (auto simp: dist_norm)
+  define R where "R = 1 + \<bar>B\<bar> + norm z"
+  have "R > 0" unfolding R_def
+  proof -
+    have "0 \<le> cmod z + \<bar>B\<bar>"
+      by (metis (full_types) add_nonneg_nonneg norm_ge_zero real_norm_def)
+    then show "0 < 1 + \<bar>B\<bar> + cmod z"
+      by linarith
+  qed
+  have *: "((\<lambda>u. f u / (u - z)) has_contour_integral 2 * complex_of_real pi * \<i> * f z) (circlepath z R)"
+    apply (rule Cauchy_integral_circlepath)
+    using \<open>R > 0\<close> apply (auto intro: holomorphic_on_subset [OF holf] holomorphic_on_imp_continuous_on)+
+    done
+  have "cmod (x - z) = R \<Longrightarrow> cmod (f x) * 2 < cmod (f z)" for x
+    unfolding R_def
+    by (rule B) (use norm_triangle_ineq4 [of x z] in auto)
+  with \<open>R > 0\<close> fz show False
+    using has_contour_integral_bound_circlepath [OF *, of "norm(f z)/2/R"]
+    by (auto simp: less_imp_le norm_mult norm_divide field_split_simps)
+qed
+
+proposition Liouville_weak:
+  assumes "f holomorphic_on UNIV" and "(f \<longlongrightarrow> l) at_infinity"
+    shows "f z = l"
+  using Liouville_weak_0 [of "\<lambda>z. f z - l"]
+  by (simp add: assms holomorphic_on_diff LIM_zero)
+
+proposition Liouville_weak_inverse:
+  assumes "f holomorphic_on UNIV" and unbounded: "\<And>B. eventually (\<lambda>x. norm (f x) \<ge> B) at_infinity"
+    obtains z where "f z = 0"
+proof -
+  { assume f: "\<And>z. f z \<noteq> 0"
+    have 1: "(\<lambda>x. 1 / f x) holomorphic_on UNIV"
+      by (simp add: holomorphic_on_divide assms f)
+    have 2: "((\<lambda>x. 1 / f x) \<longlongrightarrow> 0) at_infinity"
+      apply (rule tendstoI [OF eventually_mono])
+      apply (rule_tac B="2/e" in unbounded)
+      apply (simp add: dist_norm norm_divide field_split_simps)
+      done
+    have False
+      using Liouville_weak_0 [OF 1 2] f by simp
+  }
+  then show ?thesis
+    using that by blast
+qed
+
+text\<open> In particular we get the Fundamental Theorem of Algebra.\<close>
+
+theorem fundamental_theorem_of_algebra:
+    fixes a :: "nat \<Rightarrow> complex"
+  assumes "a 0 = 0 \<or> (\<exists>i \<in> {1..n}. a i \<noteq> 0)"
+  obtains z where "(\<Sum>i\<le>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 \<in> {1..n}" and nz: "a i \<noteq> 0"
+  have 1: "(\<lambda>z. \<Sum>i\<le>n. a i * z^i) holomorphic_on UNIV"
+    by (rule holomorphic_intros)+
+  show thesis
+  proof (rule Liouville_weak_inverse [OF 1])
+    show "\<forall>\<^sub>F x in at_infinity. B \<le> cmod (\<Sum>i\<le>n. a i * x ^ i)" for B
+      using i nz by (intro polyfun_extremal exI[of _ i]) auto
+  qed (use that in auto)
+qed
+
+subsection\<open>Weierstrass convergence theorem\<close>
+
+lemma holomorphic_uniform_limit:
+  assumes cont: "eventually (\<lambda>n. continuous_on (cball z r) (f n) \<and> (f n) holomorphic_on ball z r) F"
+      and ulim: "uniform_limit (cball z r) f g F"
+      and F:  "\<not> 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: ball_empty less_imp_le continuous_on_def holomorphic_on_def intro: that)
+next
+  case equal then show ?thesis
+    by (force simp: holomorphic_on_def intro: that)
+next
+  case greater
+  have contg: "continuous_on (cball z r) g"
+    using cont uniform_limit_theorem [OF eventually_mono ulim F]  by blast
+  have "path_image (circlepath z r) \<subseteq> cball z r"
+    using \<open>0 < r\<close> by auto
+  then have 1: "continuous_on (path_image (circlepath z r)) (\<lambda>x. 1 / (2 * complex_of_real pi * \<i>) * g x)"
+    by (intro continuous_intros continuous_on_subset [OF contg])
+  have 2: "((\<lambda>u. 1 / (2 * of_real pi * \<i>) * g u / (u - w) ^ 1) has_contour_integral g w) (circlepath z r)"
+       if w: "w \<in> ball z r" for w
+  proof -
+    define d where "d = (r - norm(w - z))"
+    have "0 < d"  "d \<le> r" using w by (auto simp: norm_minus_commute d_def dist_norm)
+    have dle: "\<And>u. cmod (z - u) = r \<Longrightarrow> d \<le> cmod (u - w)"
+      unfolding d_def by (metis add_diff_eq diff_add_cancel norm_diff_ineq norm_minus_commute)
+    have ev_int: "\<forall>\<^sub>F n in F. (\<lambda>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 ul_less: "uniform_limit (sphere z r) (\<lambda>n x. f n x / (x - w)) (\<lambda>x. g x / (x - w)) F"
+      using greater \<open>0 < d\<close>
+      apply (clarsimp simp add: uniform_limit_iff dist_norm norm_divide diff_divide_distrib [symmetric] divide_simps)
+      apply (rule_tac e1="e * d" in eventually_mono [OF uniform_limitD [OF ulim]])
+       apply (force simp: dist_norm intro: dle mult_left_mono less_le_trans)+
+      done
+    have g_cint: "(\<lambda>u. g u/(u - w)) contour_integrable_on circlepath z r"
+      by (rule contour_integral_uniform_limit_circlepath [OF ev_int ul_less F \<open>0 < r\<close>])
+    have cif_tends_cig: "((\<lambda>n. contour_integral(circlepath z r) (\<lambda>u. f n u / (u - w))) \<longlongrightarrow> contour_integral(circlepath z r) (\<lambda>u. g u/(u - w))) F"
+      by (rule contour_integral_uniform_limit_circlepath [OF ev_int ul_less F \<open>0 < r\<close>])
+    have f_tends_cig: "((\<lambda>n. 2 * of_real pi * \<i> * f n w) \<longlongrightarrow> contour_integral (circlepath z r) (\<lambda>u. g u / (u - w))) F"
+    proof (rule Lim_transform_eventually)
+      show "\<forall>\<^sub>F x in F. contour_integral (circlepath z r) (\<lambda>u. f x u / (u - w))
+                     = 2 * of_real pi * \<i> * f x w"
+        apply (rule eventually_mono [OF cont contour_integral_unique [OF Cauchy_integral_circlepath]])
+        using w\<open>0 < d\<close> d_def by auto
+    qed (auto simp: cif_tends_cig)
+    have "\<And>e. 0 < e \<Longrightarrow> \<forall>\<^sub>F n in F. dist (f n w) (g w) < e"
+      by (rule eventually_mono [OF uniform_limitD [OF ulim]]) (use w in auto)
+    then have "((\<lambda>n. 2 * of_real pi * \<i> * f n w) \<longlongrightarrow> 2 * of_real pi * \<i> * g w) F"
+      by (rule tendsto_mult_left [OF tendstoI])
+    then have "((\<lambda>u. g u / (u - w)) has_contour_integral 2 * of_real pi * \<i> * g w) (circlepath z r)"
+      using has_contour_integral_integral [OF g_cint] tendsto_unique [OF F f_tends_cig] w
+      by fastforce
+    then have "((\<lambda>u. g u / (2 * of_real pi * \<i> * (u - w))) has_contour_integral g w) (circlepath z r)"
+      using has_contour_integral_div [where c = "2 * of_real pi * \<i>"]
+      by (force simp: 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\<open> Version showing that the limit is the limit of the derivatives.\<close>
+
+proposition has_complex_derivative_uniform_limit:
+  fixes z::complex
+  assumes cont: "eventually (\<lambda>n. continuous_on (cball z r) (f n) \<and>
+                               (\<forall>w \<in> ball z r. ((f n) has_field_derivative (f' n w)) (at w))) F"
+      and ulim: "uniform_limit (cball z r) f g F"
+      and F:  "\<not> trivial_limit F" and "0 < r"
+  obtains g' where
+      "continuous_on (cball z r) g"
+      "\<And>w. w \<in> ball z r \<Longrightarrow> (g has_field_derivative (g' w)) (at w) \<and> ((\<lambda>n. f' n w) \<longlongrightarrow> 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] ulim F];
+             auto simp: holomorphic_on_open field_differentiable_def)+
+  then obtain g' where g': "\<And>x. x \<in> ball z r \<Longrightarrow> (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: "\<And>x. x \<in> ball z r \<Longrightarrow> deriv g x = g' x"
+    by (simp add: DERIV_imp_deriv)
+  have tends_f'n_g': "((\<lambda>n. f' n w) \<longlongrightarrow> g' w) F" if w: "w \<in> ball z r" for w
+  proof -
+    have eq_f': "?conint (\<lambda>x. f n x / (x - w)\<^sup>2) - ?conint (\<lambda>x. g x / (x - w)\<^sup>2) = (f' n w - g' w) * (2 * of_real pi * \<i>)"
+             if cont_fn: "continuous_on (cball z r) (f n)"
+             and fnd: "\<And>w. w \<in> ball z r \<Longrightarrow> (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: holomorphic_on_open)
+      have "(f n has_field_derivative 1 / (2 * of_real pi * \<i>) * ?conint (\<lambda>u. f n u / (u - w)\<^sup>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 * \<i>) * ?conint (\<lambda>u. f n u / (u - w)\<^sup>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] field_split_simps)
+    qed
+    define d where "d = (r - norm(w - z))^2"
+    have "d > 0"
+      using w by (simp add: dist_commute dist_norm d_def)
+    have dle: "d \<le> cmod ((y - w)\<^sup>2)" if "r = cmod (z - y)" for y
+    proof -
+      have "w \<in> ball z (cmod (z - y))"
+        using that w by fastforce
+      then have "cmod (w - z) \<le> cmod (z - y)"
+        by (simp add: dist_complex_def norm_minus_commute)
+      moreover have "cmod (z - y) - cmod (w - z) \<le> cmod (y - w)"
+        by (metis diff_add_cancel diff_add_eq_diff_diff_swap norm_minus_commute norm_triangle_ineq2)
+      ultimately show ?thesis
+        using that by (simp add: d_def norm_power power_mono)
+    qed
+    have 1: "\<forall>\<^sub>F n in F. (\<lambda>x. f n x / (x - w)\<^sup>2) contour_integrable_on circlepath z r"
+      by (force simp: holomorphic_on_open intro: w Cauchy_derivative_integral_circlepath eventually_mono [OF cont])
+    have 2: "uniform_limit (sphere z r) (\<lambda>n x. f n x / (x - w)\<^sup>2) (\<lambda>x. g x / (x - w)\<^sup>2) F"
+      unfolding uniform_limit_iff
+    proof clarify
+      fix e::real
+      assume "0 < e"
+      with \<open>r > 0\<close> show "\<forall>\<^sub>F n in F. \<forall>x\<in>sphere z r. dist (f n x / (x - w)\<^sup>2) (g x / (x - w)\<^sup>2) < e"
+        apply (simp add: norm_divide field_split_simps sphere_def dist_norm)
+        apply (rule eventually_mono [OF uniform_limitD [OF ulim], of "e*d"])
+         apply (simp add: \<open>0 < d\<close>)
+        apply (force simp: dist_norm dle intro: less_le_trans)
+        done
+    qed
+    have "((\<lambda>n. contour_integral (circlepath z r) (\<lambda>x. f n x / (x - w)\<^sup>2))
+             \<longlongrightarrow> contour_integral (circlepath z r) ((\<lambda>x. g x / (x - w)\<^sup>2))) F"
+      by (rule contour_integral_uniform_limit_circlepath [OF 1 2 F \<open>0 < r\<close>])
+    then have tendsto_0: "((\<lambda>n. 1 / (2 * of_real pi * \<i>) * (?conint (\<lambda>x. f n x / (x - w)\<^sup>2) - ?conint (\<lambda>x. g x / (x - w)\<^sup>2))) \<longlongrightarrow> 0) F"
+      using Lim_null by (force intro!: tendsto_mult_right_zero)
+    have "((\<lambda>n. f' n w - g' w) \<longlongrightarrow> 0) F"
+      apply (rule Lim_transform_eventually [OF tendsto_0])
+      apply (force simp: divide_simps intro: eq_f' eventually_mono [OF cont])
+      done
+    then show ?thesis using Lim_null by blast
+  qed
+  obtain g' where "\<And>w. w \<in> ball z r \<Longrightarrow> (g has_field_derivative (g' w)) (at w) \<and> ((\<lambda>n. f' n w) \<longlongrightarrow> g' w) F"
+      by (blast intro: tends_f'n_g' g')
+  then show ?thesis using g
+    using that by blast
+qed
+
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Some more simple/convenient versions for applications\<close>
+
+lemma holomorphic_uniform_sequence:
+  assumes S: "open S"
+      and hol_fn: "\<And>n. (f n) holomorphic_on S"
+      and ulim_g: "\<And>x. x \<in> S \<Longrightarrow> \<exists>d. 0 < d \<and> cball x d \<subseteq> S \<and> uniform_limit (cball x d) f g sequentially"
+  shows "g holomorphic_on S"
+proof -
+  have "\<exists>f'. (g has_field_derivative f') (at z)" if "z \<in> S" for z
+  proof -
+    obtain r where "0 < r" and r: "cball z r \<subseteq> S"
+               and ul: "uniform_limit (cball z r) f g sequentially"
+      using ulim_g [OF \<open>z \<in> S\<close>] by blast
+    have *: "\<forall>\<^sub>F n in sequentially. continuous_on (cball z r) (f n) \<and> f n holomorphic_on ball z r"
+    proof (intro eventuallyI conjI)
+      show "continuous_on (cball z r) (f x)" for x
+        using hol_fn holomorphic_on_imp_continuous_on holomorphic_on_subset r by blast
+      show "f x holomorphic_on ball z r" for x
+        by (metis hol_fn holomorphic_on_subset interior_cball interior_subset r)
+    qed
+    show ?thesis
+      apply (rule holomorphic_uniform_limit [OF *])
+      using \<open>0 < r\<close> centre_in_ball ul
+      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: "\<And>n x. x \<in> S \<Longrightarrow> ((f n) has_field_derivative f' n x) (at x)"
+      and ulim_g: "\<And>x. x \<in> S
+             \<Longrightarrow> \<exists>d. 0 < d \<and> cball x d \<subseteq> S \<and> uniform_limit (cball x d) f g sequentially"
+  shows "\<exists>g'. \<forall>x \<in> S. (g has_field_derivative g' x) (at x) \<and> ((\<lambda>n. f' n x) \<longlongrightarrow> g' x) sequentially"
+proof -
+  have y: "\<exists>y. (g has_field_derivative y) (at z) \<and> (\<lambda>n. f' n z) \<longlonglongrightarrow> y" if "z \<in> S" for z
+  proof -
+    obtain r where "0 < r" and r: "cball z r \<subseteq> S"
+               and ul: "uniform_limit (cball z r) f g sequentially"
+      using ulim_g [OF \<open>z \<in> S\<close>] by blast
+    have *: "\<forall>\<^sub>F n in sequentially. continuous_on (cball z r) (f n) \<and>
+                                   (\<forall>w \<in> ball z r. ((f n) has_field_derivative (f' n w)) (at w))"
+    proof (intro eventuallyI conjI ballI)
+      show "continuous_on (cball z r) (f x)" for x
+        by (meson S continuous_on_subset hfd holomorphic_on_imp_continuous_on holomorphic_on_open r)
+      show "w \<in> ball z r \<Longrightarrow> (f x has_field_derivative f' x w) (at w)" for w x
+        using ball_subset_cball hfd r by blast
+    qed
+    show ?thesis
+      by (rule has_complex_derivative_uniform_limit [OF *, of g]) (use \<open>0 < r\<close> ul in \<open>force+\<close>)
+  qed
+  show ?thesis
+    by (rule bchoice) (blast intro: y)
+qed
+
+subsection\<open>On analytic functions defined by a series\<close>
+
+lemma series_and_derivative_comparison:
+  fixes S :: "complex set"
+  assumes S: "open S"
+      and h: "summable h"
+      and hfd: "\<And>n x. x \<in> S \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
+      and to_g: "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>S. norm (f n x) \<le> h n"
+  obtains g g' where "\<forall>x \<in> S. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
+proof -
+  obtain g where g: "uniform_limit S (\<lambda>n x. \<Sum>i<n. f i x) g sequentially"
+    using Weierstrass_m_test_ev [OF to_g h]  by force
+  have *: "\<exists>d>0. cball x d \<subseteq> S \<and> uniform_limit (cball x d) (\<lambda>n x. \<Sum>i<n. f i x) g sequentially"
+         if "x \<in> S" for x
+  proof -
+    obtain d where "d>0" and d: "cball x d \<subseteq> S"
+      using open_contains_cball [of "S"] \<open>x \<in> S\<close> S by blast
+    show ?thesis
+    proof (intro conjI exI)
+      show "uniform_limit (cball x d) (\<lambda>n x. \<Sum>i<n. f i x) g sequentially"
+        using d g uniform_limit_on_subset by (force simp: dist_norm eventually_sequentially)
+    qed (use \<open>d > 0\<close> d in auto)
+  qed
+  have "\<And>x. x \<in> S \<Longrightarrow> (\<lambda>n. \<Sum>i<n. f i x) \<longlonglongrightarrow> g x"
+    by (metis tendsto_uniform_limitI [OF g])
+  moreover have "\<exists>g'. \<forall>x\<in>S. (g has_field_derivative g' x) (at x) \<and> (\<lambda>n. \<Sum>i<n. f' i x) \<longlonglongrightarrow> g' x"
+    by (rule has_complex_derivative_uniform_sequence [OF S]) (auto intro: * hfd DERIV_sum)+
+  ultimately show ?thesis
+    by (metis sums_def that)
+qed
+
+text\<open>A version where we only have local uniform/comparative convergence.\<close>
+
+lemma series_and_derivative_comparison_local:
+  fixes S :: "complex set"
+  assumes S: "open S"
+      and hfd: "\<And>n x. x \<in> S \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
+      and to_g: "\<And>x. x \<in> S \<Longrightarrow> \<exists>d h. 0 < d \<and> summable h \<and> (\<forall>\<^sub>F n in sequentially. \<forall>y\<in>ball x d \<inter> S. norm (f n y) \<le> h n)"
+  shows "\<exists>g g'. \<forall>x \<in> S. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
+proof -
+  have "\<exists>y. (\<lambda>n. f n z) sums (\<Sum>n. f n z) \<and> (\<lambda>n. f' n z) sums y \<and> ((\<lambda>x. \<Sum>n. f n x) has_field_derivative y) (at z)"
+       if "z \<in> S" for z
+  proof -
+    obtain d h where "0 < d" "summable h" and le_h: "\<forall>\<^sub>F n in sequentially. \<forall>y\<in>ball z d \<inter> S. norm (f n y) \<le> h n"
+      using to_g \<open>z \<in> S\<close> by meson
+    then obtain r where "r>0" and r: "ball z r \<subseteq> ball z d \<inter> S" using \<open>z \<in> S\<close> S
+      by (metis Int_iff open_ball centre_in_ball open_Int open_contains_ball_eq)
+    have 1: "open (ball z d \<inter> S)"
+      by (simp add: open_Int S)
+    have 2: "\<And>n x. x \<in> ball z d \<inter> S \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
+      by (auto simp: hfd)
+    obtain g g' where gg': "\<forall>x \<in> ball z d \<inter> S. ((\<lambda>n. f n x) sums g x) \<and>
+                                    ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
+      by (auto intro: le_h series_and_derivative_comparison [OF 1 \<open>summable h\<close> hfd])
+    then have "(\<lambda>n. f' n z) sums g' z"
+      by (meson \<open>0 < r\<close> centre_in_ball contra_subsetD r)
+    moreover have "(\<lambda>n. f n z) sums (\<Sum>n. f n z)"
+      using  summable_sums centre_in_ball \<open>0 < d\<close> \<open>summable h\<close> le_h
+      by (metis (full_types) Int_iff gg' summable_def that)
+    moreover have "((\<lambda>x. \<Sum>n. f n x) has_field_derivative g' z) (at z)"
+    proof (rule has_field_derivative_transform_within)
+      show "\<And>x. dist x z < r \<Longrightarrow> g x = (\<Sum>n. f n x)"
+        by (metis subsetD dist_commute gg' mem_ball r sums_unique)
+    qed (use \<open>0 < r\<close> gg' \<open>z \<in> S\<close> \<open>0 < d\<close> in auto)
+    ultimately show ?thesis by auto
+  qed
+  then show ?thesis
+    by (rule_tac x="\<lambda>x. suminf (\<lambda>n. f n x)" in exI) meson
+qed
+
+
+text\<open>Sometimes convenient to compare with a complex series of positive reals. (?)\<close>
+
+lemma series_and_derivative_comparison_complex:
+  fixes S :: "complex set"
+  assumes S: "open S"
+      and hfd: "\<And>n x. x \<in> S \<Longrightarrow> (f n has_field_derivative f' n x) (at x)"
+      and to_g: "\<And>x. x \<in> S \<Longrightarrow> \<exists>d h. 0 < d \<and> summable h \<and> range h \<subseteq> \<real>\<^sub>\<ge>\<^sub>0 \<and> (\<forall>\<^sub>F n in sequentially. \<forall>y\<in>ball x d \<inter> S. cmod(f n y) \<le> cmod (h n))"
+  shows "\<exists>g g'. \<forall>x \<in> S. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. f' n x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
+apply (rule series_and_derivative_comparison_local [OF S hfd], assumption)
+apply (rule ex_forward [OF to_g], assumption)
+apply (erule exE)
+apply (rule_tac x="Re \<circ> h" in exI)
+apply (force simp: summable_Re o_def nonneg_Reals_cmod_eq_Re image_subset_iff)
+done
+
+text\<open>Sometimes convenient to compare with a complex series of positive reals. (?)\<close>
+lemma series_differentiable_comparison_complex:
+  fixes S :: "complex set"
+  assumes S: "open S"
+    and hfd: "\<And>n x. x \<in> S \<Longrightarrow> f n field_differentiable (at x)"
+    and to_g: "\<And>x. x \<in> S \<Longrightarrow> \<exists>d h. 0 < d \<and> summable h \<and> range h \<subseteq> \<real>\<^sub>\<ge>\<^sub>0 \<and> (\<forall>\<^sub>F n in sequentially. \<forall>y\<in>ball x d \<inter> S. cmod(f n y) \<le> cmod (h n))"
+  obtains g where "\<forall>x \<in> S. ((\<lambda>n. f n x) sums g x) \<and> g field_differentiable (at x)"
+proof -
+  have hfd': "\<And>n x. x \<in> S \<Longrightarrow> (f n has_field_derivative deriv (f n) x) (at x)"
+    using hfd field_differentiable_derivI by blast
+  have "\<exists>g g'. \<forall>x \<in> S. ((\<lambda>n. f n x) sums g x) \<and> ((\<lambda>n. deriv (f n) x) sums g' x) \<and> (g has_field_derivative g' x) (at x)"
+    by (metis series_and_derivative_comparison_complex [OF S hfd' to_g])
+  then show ?thesis
+    using field_differentiable_def that by blast
+qed
+
+text\<open>In particular, a power series is analytic inside circle of convergence.\<close>
+
+lemma power_series_and_derivative_0:
+  fixes a :: "nat \<Rightarrow> complex" and r::real
+  assumes "summable (\<lambda>n. a n * r^n)"
+    shows "\<exists>g g'. \<forall>z. cmod z < r \<longrightarrow>
+             ((\<lambda>n. a n * z^n) sums g z) \<and> ((\<lambda>n. of_nat n * a n * z^(n - 1)) sums g' z) \<and> (g has_field_derivative g' z) (at z)"
+proof (cases "0 < r")
+  case True
+    have der: "\<And>n z. ((\<lambda>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: "\<lbrakk>cmod (z - y) * 2 < r - cmod z\<rbrakk> \<Longrightarrow> cmod y \<le> cmod (of_real r + of_real (cmod z)) / 2" for z y
+      using \<open>r > 0\<close>
+      apply (auto simp: algebra_simps norm_mult norm_divide norm_power simp flip: of_real_add)
+      using norm_triangle_ineq2 [of y z]
+      apply (simp only: diff_le_eq norm_minus_commute mult_2)
+      done
+    have "summable (\<lambda>n. a n * complex_of_real r ^ n)"
+      using assms \<open>r > 0\<close> by simp
+    moreover have "\<And>z. cmod z < r \<Longrightarrow> cmod ((of_real r + of_real (cmod z)) / 2) < cmod (of_real r)"
+      using \<open>r > 0\<close>
+      by (simp flip: of_real_add)
+    ultimately have sum: "\<And>z. cmod z < r \<Longrightarrow> summable (\<lambda>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 "\<exists>g g'. \<forall>z \<in> ball 0 r. (\<lambda>n.  (a n) * z ^ n) sums g z \<and>
+               (\<lambda>n. of_nat n * (a n) * z ^ (n - 1)) sums g' z \<and> (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 (rule_tac x="\<lambda>n. of_real(norm(a n)*((r + norm z)/2)^n)" in exI)
+      using \<open>r > 0\<close>
+      apply (auto simp: sum eventually_sequentially norm_mult norm_power dist_norm intro!: mult_left_mono power_mono y_le)
+      done
+  then show ?thesis
+    by (simp add: 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\<^marker>\<open>tag unimportant\<close> power_series_and_derivative:
+  fixes a :: "nat \<Rightarrow> complex" and r::real
+  assumes "summable (\<lambda>n. a n * r^n)"
+    obtains g g' where "\<forall>z \<in> ball w r.
+             ((\<lambda>n. a n * (z - w) ^ n) sums g z) \<and> ((\<lambda>n. of_nat n * a n * (z - w) ^ (n - 1)) sums g' z) \<and>
+              (g has_field_derivative g' z) (at z)"
+  using power_series_and_derivative_0 [OF assms]
+  apply clarify
+  apply (rule_tac g="(\<lambda>z. g(z - w))" in that)
+  using DERIV_shift [where z="-w"]
+  apply (auto simp: norm_minus_commute Ball_def dist_norm)
+  done
+
+proposition\<^marker>\<open>tag unimportant\<close> power_series_holomorphic:
+  assumes "\<And>w. w \<in> ball z r \<Longrightarrow> ((\<lambda>n. a n*(w - z)^n) sums f w)"
+    shows "f holomorphic_on ball z r"
+proof -
+  have "\<exists>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) \<in> ball z r"
+    proof -
+      have wz: "cmod (w - z) < r" using w
+        by (auto simp: field_split_simps dist_norm norm_minus_commute)
+      then have "0 \<le> r"
+        by (meson less_eq_real_def norm_ge_zero order_trans)
+      show ?thesis
+        using w by (simp add: dist_norm \<open>0\<le>r\<close> flip: of_real_add)
+    qed
+    have sum: "summable (\<lambda>n. a n * of_real (((cmod (z - w) + r) / 2) ^ n))"
+      using assms [OF inb] by (force simp: summable_def dist_norm)
+    obtain g g' where gg': "\<And>u. u \<in> ball z ((cmod (z - w) + r) / 2) \<Longrightarrow>
+                               (\<lambda>n. a n * (u - z) ^ n) sums g u \<and>
+                               (\<lambda>n. of_nat n * a n * (u - z) ^ (n - 1)) sums g' u \<and> (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 "\<lambda>n. a n*(u - z)^n"])
+        using gg' [of u] less w
+        apply (auto simp: assms dist_norm)
+        done
+    qed
+    have "(f has_field_derivative g' w) (at w)"
+      by (rule has_field_derivative_transform_within [where d="(r - norm(z - w))/2"])
+      (use w gg' [of w] in \<open>(force simp: dist_norm)+\<close>)
+    then show ?thesis ..
+  qed
+  then show ?thesis by (simp add: holomorphic_on_open)
+qed
+
+corollary holomorphic_iff_power_series:
+     "f holomorphic_on ball z r \<longleftrightarrow>
+      (\<forall>w \<in> ball z r. (\<lambda>n. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
+  apply (intro iffI ballI holomorphic_power_series, assumption+)
+  apply (force intro: power_series_holomorphic [where a = "\<lambda>n. (deriv ^^ n) f z / (fact n)"])
+  done
+
+lemma power_series_analytic:
+     "(\<And>w. w \<in> ball z r \<Longrightarrow> (\<lambda>n. a n*(w - z)^n) sums f w) \<Longrightarrow> f analytic_on ball z r"
+  by (force simp: analytic_on_open intro!: power_series_holomorphic)
+
+lemma analytic_iff_power_series:
+     "f analytic_on ball z r \<longleftrightarrow>
+      (\<forall>w \<in> ball z r. (\<lambda>n. (deriv ^^ n) f z / (fact n) * (w - z)^n) sums f w)"
+  by (simp add: analytic_on_open holomorphic_iff_power_series)
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Equality between holomorphic functions, on open ball then connected set\<close>
+
+lemma holomorphic_fun_eq_on_ball:
+   "\<lbrakk>f holomorphic_on ball z r; g holomorphic_on ball z r;
+     w \<in> ball z r;
+     \<And>n. (deriv ^^ n) f z = (deriv ^^ n) g z\<rbrakk>
+     \<Longrightarrow> f w = g w"
+  apply (rule sums_unique2 [of "\<lambda>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:
+   "\<lbrakk>f holomorphic_on ball z r;  w \<in> ball z r;
+     \<And>n. (deriv ^^ n) f z = 0\<rbrakk>
+     \<Longrightarrow> f w = 0"
+  apply (rule sums_unique2 [of "\<lambda>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: "\<And>n. (deriv ^^ n) f z = 0"
+      and "z \<in> S" "w \<in> S"
+    shows "f w = 0"
+proof -
+  have *: "ball x e \<subseteq> (\<Inter>n. {w \<in> S. (deriv ^^ n) f w = 0})"
+    if "\<forall>u. (deriv ^^ u) f x = 0" "ball x e \<subseteq> S" for x e
+  proof -
+    have "\<And>x' n. dist x x' < e \<Longrightarrow> (deriv ^^ n) f x' = 0"
+      apply (rule holomorphic_fun_eq_0_on_ball [OF holomorphic_higher_deriv])
+         apply (rule holomorphic_on_subset [OF holf])
+      using that apply simp_all
+      by (metis funpow_add o_apply)
+    with that show ?thesis by auto
+  qed
+  have 1: "openin (top_of_set S) (\<Inter>n. {w \<in> S. (deriv ^^ n) f w = 0})"
+    apply (rule open_subset, force)
+    using \<open>open S\<close>
+    apply (simp add: open_contains_ball Ball_def)
+    apply (erule all_forward)
+    using "*" by auto blast+
+  have 2: "closedin (top_of_set S) (\<Inter>n. {w \<in> 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 \<subseteq> S" using openE [OF \<open>open S\<close> \<open>w \<in> S\<close>] .
+  then have holfb: "f holomorphic_on ball w e"
+    using holf holomorphic_on_subset by blast
+  have 3: "(\<Inter>n. {w \<in> S. (deriv ^^ n) f w = 0}) = S \<Longrightarrow> f w = 0"
+    using \<open>e>0\<close> e by (force intro: holomorphic_fun_eq_0_on_ball [OF holfb])
+  show ?thesis
+    using cons der \<open>z \<in> S\<close>
+    apply (simp add: connected_clopen)
+    apply (drule_tac x="\<Inter>n. {w \<in> 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 "\<And>n. (deriv ^^ n) f z = (deriv ^^ n) g z"
+      and "z \<in> S" "w \<in> S"
+    shows "f w = g w"
+proof (rule holomorphic_fun_eq_0_on_connected [of "\<lambda>x. f x - g x" S z, simplified])
+  show "(\<lambda>x. f x - g x) holomorphic_on S"
+    by (intro assms holomorphic_intros)
+  show "\<And>n. (deriv ^^ n) (\<lambda>x. f x - g x) z = 0"
+    using assms higher_deriv_diff by auto
+qed (use assms in auto)
+
+lemma holomorphic_fun_eq_const_on_connected:
+  assumes holf: "f holomorphic_on S" and "open S"
+      and cons: "connected S"
+      and der: "\<And>n. 0 < n \<Longrightarrow> (deriv ^^ n) f z = 0"
+      and "z \<in> S" "w \<in> S"
+    shows "f w = f z"
+proof (rule holomorphic_fun_eq_0_on_connected [of "\<lambda>w. f w - f z" S z, simplified])
+  show "(\<lambda>w. f w - f z) holomorphic_on S"
+    by (intro assms holomorphic_intros)
+  show "\<And>n. (deriv ^^ n) (\<lambda>w. f w - f z) z = 0"
+    by (subst higher_deriv_diff) (use assms in \<open>auto intro: holomorphic_intros\<close>)
+qed (use assms in auto)
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Some basic lemmas about poles/singularities\<close>
+
+lemma pole_lemma:
+  assumes holf: "f holomorphic_on S" and a: "a \<in> interior S"
+    shows "(\<lambda>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 \<in> S" "u \<noteq> a" for u
+  proof -
+    have fcd: "f field_differentiable at u within S"
+      using holf holomorphic_on_def by (simp add: \<open>u \<in> S\<close>)
+    have cd: "(\<lambda>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: \<open>u \<in> S\<close>)
+  qed
+  have F2: "?F field_differentiable at a" if "0 < e" "ball a e \<subseteq> S" for e
+  proof -
+    have holfb: "f holomorphic_on ball a e"
+      by (rule holomorphic_on_subset [OF holf \<open>ball a e \<subseteq> S\<close>])
+    have 2: "?F holomorphic_on ball a e - {a}"
+      apply (simp add: holomorphic_on_def flip: field_differentiable_def)
+      using mem_ball that
+      apply (auto intro: F1 field_differentiable_within_subset)
+      done
+    have "isCont (\<lambda>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 have "f field_differentiable at a"
+        using holfb \<open>0 < e\<close> holomorphic_on_imp_differentiable_at by auto
+      with True show ?thesis
+        by (auto simp: continuous_at has_field_derivative_iff simp flip: DERIV_deriv_iff_field_differentiable
+                elim: rev_iffD1 [OF _ LIM_equal])
+    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 \<in> 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 \<open>x \<in> S\<close>
+      show ?thesis by blast
+    qed
+  qed
+qed
+
+lemma pole_theorem:
+  assumes holg: "g holomorphic_on S" and a: "a \<in> interior S"
+      and eq: "\<And>z. z \<in> S - {a} \<Longrightarrow> g z = (z - a) * f z"
+    shows "(\<lambda>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 field_split_simps)
+
+lemma pole_lemma_open:
+  assumes "f holomorphic_on S" "open S"
+    shows "(\<lambda>z. if z = a then deriv f a else (f z - f a)/(z - a)) holomorphic_on S"
+proof (cases "a \<in> 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 = "\<lambda>z. (f z - f a)/(z - a)" and d = 1])
+    apply (rule derivative_intros | force)+
+    done
+qed
+
+lemma pole_theorem_open:
+  assumes holg: "g holomorphic_on S" and S: "open S"
+      and eq: "\<And>z. z \<in> S - {a} \<Longrightarrow> g z = (z - a) * f z"
+    shows "(\<lambda>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)
+
+lemma pole_theorem_0:
+  assumes holg: "g holomorphic_on S" and a: "a \<in> interior S"
+      and eq: "\<And>z. z \<in> S - {a} \<Longrightarrow> 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 field_split_simps)
+
+lemma pole_theorem_open_0:
+  assumes holg: "g holomorphic_on S" and S: "open S"
+      and eq: "\<And>z. z \<in> S - {a} \<Longrightarrow> 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 field_split_simps)
+
+lemma pole_theorem_analytic:
+  assumes g: "g analytic_on S"
+      and eq: "\<And>z. z \<in> S
+             \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>w \<in> ball z d - {a}. g w = (w - a) * f w)"
+    shows "(\<lambda>z. if z = a then deriv g a else f z - g a/(z - a)) analytic_on S" (is "?F analytic_on S")
+  unfolding analytic_on_def
+proof
+  fix x
+  assume "x \<in> S"
+  with g obtain e where "0 < e" and e: "g holomorphic_on ball x e"
+    by (auto simp add: analytic_on_def)
+  obtain d where "0 < d" and d: "\<And>w. w \<in> ball x d - {a} \<Longrightarrow> g w = (w - a) * f w"
+    using \<open>x \<in> S\<close> eq by blast
+  have "?F holomorphic_on ball x (min d e)"
+    using d e \<open>x \<in> S\<close> by (fastforce simp: holomorphic_on_subset subset_ball intro!: pole_theorem_open)
+  then show "\<exists>e>0. ?F holomorphic_on ball x e"
+    using \<open>0 < d\<close> \<open>0 < e\<close> not_le by fastforce
+qed
+
+lemma pole_theorem_analytic_0:
+  assumes g: "g analytic_on S"
+      and eq: "\<And>z. z \<in> S \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>w \<in> 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]: "(\<lambda>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 \<subseteq> T" "open T"
+      and eq: "\<And>z. z \<in> T - {a} \<Longrightarrow> g z = (z - a) * f z"
+    shows "(\<lambda>z. if z = a then deriv g a
+                 else f z - g a/(z - a)) analytic_on S"
+proof (rule pole_theorem_analytic [OF g])
+  fix z
+  assume "z \<in> S"
+  then obtain e where "0 < e" and e: "ball z e \<subseteq> T"
+    using assms openE by blast
+  then show "\<exists>d>0. \<forall>w\<in>ball z d - {a}. g w = (w - a) * f w"
+    using eq by auto
+qed
+
+lemma pole_theorem_analytic_open_superset_0:
+  assumes g: "g analytic_on S" "S \<subseteq> T" "open T" "\<And>z. z \<in> T - {a} \<Longrightarrow> g z = (z - a) * f z"
+      and [simp]: "f a = deriv g a" "g a = 0"
+    shows "f analytic_on S"
+proof -
+  have [simp]: "(\<lambda>z. if z = a then deriv g a else f z - g a / (z - a)) = f"
+    by auto
+  have "(\<lambda>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\<open>General, homology form of Cauchy's theorem\<close>
+
+text\<open>Proof is based on Dixon's, as presented in Lang's "Complex Analysis" book (page 147).\<close>
+
+lemma contour_integral_continuous_on_linepath_2D:
+  assumes "open U" and cont_dw: "\<And>w. w \<in> U \<Longrightarrow> F w contour_integrable_on (linepath a b)"
+      and cond_uu: "continuous_on (U \<times> U) (\<lambda>(x,y). F x y)"
+      and abu: "closed_segment a b \<subseteq> U"
+    shows "continuous_on U (\<lambda>w. contour_integral (linepath a b) (F w))"
+proof -
+  have *: "\<exists>d>0. \<forall>x'\<in>U. dist x' w < d \<longrightarrow>
+                         dist (contour_integral (linepath a b) (F x'))
+                              (contour_integral (linepath a b) (F w)) \<le> \<epsilon>"
+          if "w \<in> U" "0 < \<epsilon>" "a \<noteq> b" for w \<epsilon>
+  proof -
+    obtain \<delta> where "\<delta>>0" and \<delta>: "cball w \<delta> \<subseteq> U" using open_contains_cball \<open>open U\<close> \<open>w \<in> U\<close> by force
+    let ?TZ = "cball w \<delta>  \<times> closed_segment a b"
+    have "uniformly_continuous_on ?TZ (\<lambda>(x,y). F x y)"
+    proof (rule compact_uniformly_continuous)
+      show "continuous_on ?TZ (\<lambda>(x,y). F x y)"
+        by (rule continuous_on_subset[OF cond_uu]) (use SigmaE \<delta> abu in blast)
+      show "compact ?TZ"
+        by (simp add: compact_Times)
+    qed
+    then obtain \<eta> where "\<eta>>0"
+        and \<eta>: "\<And>x x'. \<lbrakk>x\<in>?TZ; x'\<in>?TZ; dist x' x < \<eta>\<rbrakk> \<Longrightarrow>
+                         dist ((\<lambda>(x,y). F x y) x') ((\<lambda>(x,y). F x y) x) < \<epsilon>/norm(b - a)"
+      apply (rule uniformly_continuous_onE [where e = "\<epsilon>/norm(b - a)"])
+      using \<open>0 < \<epsilon>\<close> \<open>a \<noteq> b\<close> by auto
+    have \<eta>: "\<lbrakk>norm (w - x1) \<le> \<delta>;   x2 \<in> closed_segment a b;
+              norm (w - x1') \<le> \<delta>;  x2' \<in> closed_segment a b; norm ((x1', x2') - (x1, x2)) < \<eta>\<rbrakk>
+              \<Longrightarrow> norm (F x1' x2' - F x1 x2) \<le> \<epsilon> / cmod (b - a)"
+             for x1 x2 x1' x2'
+      using \<eta> [of "(x1,x2)" "(x1',x2')"] by (force simp: dist_norm)
+    have le_ee: "cmod (contour_integral (linepath a b) (\<lambda>x. F x' x - F w x)) \<le> \<epsilon>"
+                if "x' \<in> U" "cmod (x' - w) < \<delta>" "cmod (x' - w) < \<eta>"  for x'
+    proof -
+      have "(\<lambda>x. F x' x - F w x) contour_integrable_on linepath a b"
+        by (simp add: \<open>w \<in> U\<close> cont_dw contour_integrable_diff that)
+      then have "cmod (contour_integral (linepath a b) (\<lambda>x. F x' x - F w x)) \<le> \<epsilon>/norm(b - a) * norm(b - a)"
+        apply (rule has_contour_integral_bound_linepath [OF has_contour_integral_integral _ \<eta>])
+        using \<open>0 < \<epsilon>\<close> \<open>0 < \<delta>\<close> that apply (auto simp: norm_minus_commute)
+        done
+      also have "\<dots> = \<epsilon>" using \<open>a \<noteq> b\<close> by simp
+      finally show ?thesis .
+    qed
+    show ?thesis
+      apply (rule_tac x="min \<delta> \<eta>" in exI)
+      using \<open>0 < \<delta>\<close> \<open>0 < \<eta>\<close>
+      apply (auto simp: dist_norm contour_integral_diff [OF cont_dw cont_dw, symmetric] \<open>w \<in> U\<close> intro: le_ee)
+      done
+  qed
+  show ?thesis
+  proof (cases "a=b")
+    case True
+    then show ?thesis by simp
+  next
+    case False
+    show ?thesis
+      by (rule continuous_onI) (use False in \<open>auto intro: *\<close>)
+  qed
+qed
+
+text\<open>This version has \<^term>\<open>polynomial_function \<gamma>\<close> as an additional assumption.\<close>
+lemma Cauchy_integral_formula_global_weak:
+  assumes "open U" and holf: "f holomorphic_on U"
+        and z: "z \<in> U" and \<gamma>: "polynomial_function \<gamma>"
+        and pasz: "path_image \<gamma> \<subseteq> U - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
+        and zero: "\<And>w. w \<notin> U \<Longrightarrow> winding_number \<gamma> w = 0"
+      shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
+proof -
+  obtain \<gamma>' where pf\<gamma>': "polynomial_function \<gamma>'" and \<gamma>': "\<And>x. (\<gamma> has_vector_derivative (\<gamma>' x)) (at x)"
+    using has_vector_derivative_polynomial_function [OF \<gamma>] by blast
+  then have "bounded(path_image \<gamma>')"
+    by (simp add: path_image_def compact_imp_bounded compact_continuous_image continuous_on_polymonial_function)
+  then obtain B where "B>0" and B: "\<And>x. x \<in> path_image \<gamma>' \<Longrightarrow> norm x \<le> B"
+    using bounded_pos by force
+  define d where [abs_def]: "d z w = (if w = z then deriv f z else (f w - f z)/(w - z))" for z w
+  define v where "v = {w. w \<notin> path_image \<gamma> \<and> winding_number \<gamma> w = 0}"
+  have "path \<gamma>" "valid_path \<gamma>" using \<gamma>
+    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 \<union> 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 \<in> U" for y
+  proof -
+    have *: "(\<lambda>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 \<open>open U\<close>)
+    then have "isCont (\<lambda>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 \<open>open U\<close> by fastforce
+    then have "continuous_on U (d y)"
+      apply (simp add: d_def continuous_on_eq_continuous_at \<open>open U\<close>, clarify)
+      using * holomorphic_on_def
+      by (meson field_differentiable_within_open field_differentiable_imp_continuous_at \<open>open U\<close>)
+    moreover have "d y holomorphic_on U - {y}"
+    proof -
+      have "\<And>w. w \<in> U - {y} \<Longrightarrow>
+                 (\<lambda>w. if w = y then deriv f y else (f w - f y) / (w - y)) field_differentiable at w"
+        apply (rule_tac d="dist w y" and f = "\<lambda>w. (f w - f y)/(w - y)" in field_differentiable_transform_within)
+           apply (auto simp: dist_pos_lt dist_commute intro!: derivative_intros)
+        using \<open>open U\<close> holf holomorphic_on_imp_differentiable_at by blast
+      then show ?thesis
+        unfolding field_differentiable_def by (simp add: d_def holomorphic_on_open \<open>open U\<close> open_delete)
+    qed
+    ultimately show ?thesis
+      by (rule no_isolated_singularity) (auto simp: \<open>open U\<close>)
+  qed
+  have cint_fxy: "(\<lambda>x. (f x - f y) / (x - y)) contour_integrable_on \<gamma>" if "y \<notin> path_image \<gamma>" for y
+  proof (rule contour_integrable_holomorphic_simple [where S = "U-{y}"])
+    show "(\<lambda>x. (f x - f y) / (x - y)) holomorphic_on U - {y}"
+      by (force intro: holomorphic_intros holomorphic_on_subset [OF holf])
+    show "path_image \<gamma> \<subseteq> U - {y}"
+      using pasz that by blast
+  qed (auto simp: \<open>open U\<close> open_delete \<open>valid_path \<gamma>\<close>)
+  define h where
+    "h z = (if z \<in> U then contour_integral \<gamma> (d z) else contour_integral \<gamma> (\<lambda>w. f w/(w - z)))" for z
+  have U: "((d z) has_contour_integral h z) \<gamma>" if "z \<in> U" for z
+  proof -
+    have "d z holomorphic_on U"
+      by (simp add: hol_d that)
+    with that show ?thesis
+    apply (simp add: h_def)
+      by (meson Diff_subset \<open>open U\<close> \<open>valid_path \<gamma>\<close> contour_integrable_holomorphic_simple has_contour_integral_integral pasz subset_trans)
+  qed
+  have V: "((\<lambda>w. f w / (w - z)) has_contour_integral h z) \<gamma>" if z: "z \<in> v" for z
+  proof -
+    have 0: "0 = (f z) * 2 * of_real (2 * pi) * \<i> * winding_number \<gamma> z"
+      using v_def z by auto
+    then have "((\<lambda>x. 1 / (x - z)) has_contour_integral 0) \<gamma>"
+     using z v_def  has_contour_integral_winding_number [OF \<open>valid_path \<gamma>\<close>] by fastforce
+    then have "((\<lambda>x. f z * (1 / (x - z))) has_contour_integral 0) \<gamma>"
+      using has_contour_integral_lmul by fastforce
+    then have "((\<lambda>x. f z / (x - z)) has_contour_integral 0) \<gamma>"
+      by (simp add: field_split_simps)
+    moreover have "((\<lambda>x. (f x - f z) / (x - z)) has_contour_integral contour_integral \<gamma> (d z)) \<gamma>"
+      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 *: "((\<lambda>x. f z / (x - z) + (f x - f z) / (x - z)) has_contour_integral (0 + contour_integral \<gamma> (d z))) \<gamma>"
+      by (rule has_contour_integral_add)
+    have "((\<lambda>w. f w / (w - z)) has_contour_integral contour_integral \<gamma> (d z)) \<gamma>"
+            if  "z \<in> U"
+      using * by (auto simp: divide_simps has_contour_integral_eq)
+    moreover have "((\<lambda>w. f w / (w - z)) has_contour_integral contour_integral \<gamma> (\<lambda>w. f w / (w - z))) \<gamma>"
+            if "z \<notin> U"
+      apply (rule has_contour_integral_integral [OF contour_integrable_holomorphic_simple [where S=U]])
+      using U pasz \<open>valid_path \<gamma>\<close> that
+      apply (auto intro: holomorphic_on_imp_continuous_on hol_d)
+       apply (rule continuous_intros conf holomorphic_intros holf assms | force)+
+      done
+    ultimately show ?thesis
+      using z by (simp add: h_def)
+  qed
+  have znot: "z \<notin> path_image \<gamma>"
+    using pasz by blast
+  obtain d0 where "d0>0" and d0: "\<And>x y. x \<in> path_image \<gamma> \<Longrightarrow> y \<in> - U \<Longrightarrow> d0 \<le> dist x y"
+    using separate_compact_closed [of "path_image \<gamma>" "-U"] pasz \<open>open U\<close> \<open>path \<gamma>\<close> compact_path_image
+    by blast    
+  obtain dd where "0 < dd" and dd: "{y + k | y k. y \<in> path_image \<gamma> \<and> k \<in> ball 0 dd} \<subseteq> U"
+    apply (rule that [of "d0/2"])
+    using \<open>0 < d0\<close>
+    apply (auto simp: dist_norm dest: d0)
+    done
+  have "\<And>x x'. \<lbrakk>x \<in> path_image \<gamma>; dist x x' * 2 < dd\<rbrakk> \<Longrightarrow> \<exists>y k. x' = y + k \<and> y \<in> path_image \<gamma> \<and> dist 0 k * 2 \<le> dd"
+    apply (rule_tac x=x in exI)
+    apply (rule_tac x="x'-x" in exI)
+    apply (force simp: dist_norm)
+    done
+  then have 1: "path_image \<gamma> \<subseteq> interior {y + k |y k. y \<in> path_image \<gamma> \<and> k \<in> cball 0 (dd / 2)}"
+    apply (clarsimp simp add: mem_interior)
+    using \<open>0 < dd\<close>
+    apply (rule_tac x="dd/2" in exI, auto)
+    done
+  obtain T where "compact T" and subt: "path_image \<gamma> \<subseteq> interior T" and T: "T \<subseteq> U"
+    apply (rule that [OF _ 1])
+    apply (fastforce simp add: \<open>valid_path \<gamma>\<close> compact_valid_path_image intro!: compact_sums)
+    apply (rule order_trans [OF _ dd])
+    using \<open>0 < dd\<close> by fastforce
+  obtain L where "L>0"
+           and L: "\<And>f B. \<lbrakk>f holomorphic_on interior T; \<And>z. z\<in>interior T \<Longrightarrow> cmod (f z) \<le> B\<rbrakk> \<Longrightarrow>
+                         cmod (contour_integral \<gamma> f) \<le> L * B"
+      using contour_integral_bound_exists [OF open_interior \<open>valid_path \<gamma>\<close> subt]
+      by blast
+  have "bounded(f ` T)"
+    by (meson \<open>compact T\<close> compact_continuous_image compact_imp_bounded conf continuous_on_subset T)
+  then obtain D where "D>0" and D: "\<And>x. x \<in> T \<Longrightarrow> norm (f x) \<le> D"
+    by (auto simp: bounded_pos)
+  obtain C where "C>0" and C: "\<And>x. x \<in> T \<Longrightarrow> norm x \<le> C"
+    using \<open>compact T\<close> bounded_pos compact_imp_bounded by force
+  have "dist (h y) 0 \<le> e" if "0 < e" and le: "D * L / e + C \<le> cmod y" for e y
+  proof -
+    have "D * L / e > 0"  using \<open>D>0\<close> \<open>L>0\<close> \<open>e>0\<close> by simp
+    with le have ybig: "norm y > C" by force
+    with C have "y \<notin> T"  by force
+    then have ynot: "y \<notin> path_image \<gamma>"
+      using subt interior_subset by blast
+    have [simp]: "winding_number \<gamma> y = 0"
+      apply (rule winding_number_zero_outside [of _ "cball 0 C"])
+      using ybig interior_subset subt
+      apply (force simp: loop \<open>path \<gamma>\<close> dist_norm intro!: C)+
+      done
+    have [simp]: "h y = contour_integral \<gamma> (\<lambda>w. f w/(w - y))"
+      by (rule contour_integral_unique [symmetric]) (simp add: v_def ynot V)
+    have holint: "(\<lambda>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 \<open>y \<notin> T\<close> interior_subset by auto
+    have leD: "cmod (f z / (z - y)) \<le> D * (e / L / D)" if z: "z \<in> interior T" for z
+    proof -
+      have "D * L / e + cmod z \<le> cmod y"
+        using le C [of z] z using interior_subset by force
+      then have DL2: "D * L / e \<le> 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 "\<dots> \<le> D * (e / L / D)"
+        apply (rule mult_mono)
+        using that D interior_subset apply blast
+        using \<open>L>0\<close> \<open>e>0\<close> \<open>D>0\<close> DL2
+        apply (auto simp: norm_divide field_split_simps)
+        done
+      finally show ?thesis .
+    qed
+    have "dist (h y) 0 = cmod (contour_integral \<gamma> (\<lambda>w. f w / (w - y)))"
+      by (simp add: dist_norm)
+    also have "\<dots> \<le> L * (D * (e / L / D))"
+      by (rule L [OF holint leD])
+    also have "\<dots> = e"
+      using  \<open>L>0\<close> \<open>0 < D\<close> by auto
+    finally show ?thesis .
+  qed
+  then have "(h \<longlongrightarrow> 0) at_infinity"
+    by (meson Lim_at_infinityI)
+  moreover have "h holomorphic_on UNIV"
+  proof -
+    have con_ff: "continuous (at (x,z)) (\<lambda>(x,y). (f y - f x) / (y - x))"
+                 if "x \<in> U" "z \<in> U" "x \<noteq> z" for x z
+      using that conf
+      apply (simp add: split_def continuous_on_eq_continuous_at \<open>open U\<close>)
+      apply (simp | rule continuous_intros continuous_within_compose2 [where g=f])+
+      done
+    have con_fstsnd: "continuous_on UNIV (\<lambda>x. (fst x - snd x) ::complex)"
+      by (rule continuous_intros)+
+    have open_uu_Id: "open (U \<times> U - Id)"
+      apply (rule open_Diff)
+      apply (simp add: open_Times \<open>open U\<close>)
+      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 \<in> U" for z
+      apply (rule continuous_on_interior [of U])
+      apply (simp add: holf holomorphic_deriv holomorphic_on_imp_continuous_on \<open>open U\<close>)
+      by (simp add: interior_open that \<open>open U\<close>)
+    have tendsto_f': "((\<lambda>(x,y). if y = x then deriv f (x)
+                                else (f (y) - f (x)) / (y - x)) \<longlongrightarrow> deriv f x)
+                      (at (x, x) within U \<times> U)" if "x \<in> U" for x
+    proof (rule Lim_withinI)
+      fix e::real assume "0 < e"
+      obtain k1 where "k1>0" and k1: "\<And>x'. norm (x' - x) \<le> k1 \<Longrightarrow> norm (deriv f x' - deriv f x) < e"
+        using \<open>0 < e\<close> continuous_within_E [OF con_derf [OF \<open>x \<in> U\<close>]]
+        by (metis UNIV_I dist_norm)
+      obtain k2 where "k2>0" and k2: "ball x k2 \<subseteq> U"
+        by (blast intro: openE [OF \<open>open U\<close>] \<open>x \<in> U\<close>)
+      have neq: "norm ((f z' - f x') / (z' - x') - deriv f x) \<le> e"
+                    if "z' \<noteq> 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 \<in> closed_segment x' z' \<Longrightarrow> cmod (w - x) \<le> 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 \<in> closed_segment x' z' \<Longrightarrow> z' \<noteq> x' \<Longrightarrow> cmod (deriv f w - deriv f x) \<le> 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: "\<And>x. x \<in> U \<Longrightarrow> (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 \<open>open U\<close>)
+        have "closed_segment x' z' \<subseteq> 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 *: "((\<lambda>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) \<le> 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']]
+                 \<open>e > 0\<close>  \<open>z' \<noteq> x'\<close>
+          apply (auto simp: norm_divide divide_simps derf_le)
+          done
+        also have "\<dots> \<le> e" using \<open>0 < e\<close> by simp
+        finally show ?thesis .
+      qed
+      show "\<exists>d>0. \<forall>xa\<in>U \<times> U.
+                  0 < dist xa (x, x) \<and> dist xa (x, x) < d \<longrightarrow>
+                  dist (case xa of (x, y) \<Rightarrow> if y = x then deriv f x else (f y - f x) / (y - x)) (deriv f x) \<le> e"
+        apply (rule_tac x="min k1 k2" in exI)
+        using \<open>k1>0\<close> \<open>k2>0\<close> \<open>e>0\<close>
+        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 \<gamma>) f"
+      by (meson holf holomorphic_on_imp_continuous_on holomorphic_on_subset interior_subset subt T)
+    have le_B: "\<And>T. T \<in> {0..1} \<Longrightarrow> cmod (vector_derivative \<gamma> (at T)) \<le> B"
+      apply (rule B)
+      using \<gamma>' using path_image_def vector_derivative_at by fastforce
+    have f_has_cint: "\<And>w. w \<in> v - path_image \<gamma> \<Longrightarrow> ((\<lambda>u. f u / (u - w) ^ 1) has_contour_integral h w) \<gamma>"
+      by (simp add: V)
+    have cond_uu: "continuous_on (U \<times> U) (\<lambda>(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 \<open>open U\<close>, clarify)
+      apply (rule Lim_transform_within_open [OF _ open_uu_Id, where f = "(\<lambda>(x,y). (f y - f x) / (y - x))"])
+      using con_ff
+      apply (auto simp: continuous_within)
+      done
+    have hol_dw: "(\<lambda>z. d z w) holomorphic_on U" if "w \<in> U" for w
+    proof -
+      have "continuous_on U ((\<lambda>(x,y). d x y) \<circ> (\<lambda>z. (w,z)))"
+        by (rule continuous_on_compose continuous_intros continuous_on_subset [OF cond_uu] | force intro: that)+
+      then have *: "continuous_on U (\<lambda>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 **: "\<And>x. \<lbrakk>x \<in> U; x \<noteq> w\<rbrakk> \<Longrightarrow> (\<lambda>z. if w = z then deriv f z else (f w - f z) / (w - z)) field_differentiable at x"
+        apply (rule_tac f = "\<lambda>x. (f w - f x)/(w - x)" and d = "dist x w" in field_differentiable_transform_within)
+        apply (rule \<open>open U\<close> derivative_intros holomorphic_on_imp_differentiable_at [OF holf] | force simp: dist_commute)+
+        done
+      show ?thesis
+        unfolding d_def
+        apply (rule no_isolated_singularity [OF * _ \<open>open U\<close>, where K = "{w}"])
+        apply (auto simp: field_differentiable_def [symmetric] holomorphic_on_open open_Diff \<open>open U\<close> **)
+        done
+    qed
+    { fix a b
+      assume abu: "closed_segment a b \<subseteq> U"
+      then have "\<And>w. w \<in> U \<Longrightarrow> (\<lambda>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 (\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w))"
+        apply (rule contour_integral_continuous_on_linepath_2D [OF \<open>open U\<close> _ _ abu])
+        apply (auto intro: continuous_on_swap_args cond_uu)
+        done
+      have cont_cint_d\<gamma>: "continuous_on {0..1} ((\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w)) \<circ> \<gamma>)"
+      proof (rule continuous_on_compose)
+        show "continuous_on {0..1} \<gamma>"
+          using \<open>path \<gamma>\<close> path_def by blast
+        show "continuous_on (\<gamma> ` {0..1}) (\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w))"
+          using pasz unfolding path_image_def
+          by (auto intro!: continuous_on_subset [OF cont_cint_d])
+      qed
+      have cint_cint: "(\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w)) contour_integrable_on \<gamma>"
+        apply (simp add: contour_integrable_on)
+        apply (rule integrable_continuous_real)
+        apply (rule continuous_on_mult [OF cont_cint_d\<gamma> [unfolded o_def]])
+        using pf\<gamma>'
+        by (simp add: continuous_on_polymonial_function vector_derivative_at [OF \<gamma>'])
+      have "contour_integral (linepath a b) h = contour_integral (linepath a b) (\<lambda>z. contour_integral \<gamma> (d z))"
+        using abu  by (force simp: h_def intro: contour_integral_eq)
+      also have "\<dots> =  contour_integral \<gamma> (\<lambda>w. contour_integral (linepath a b) (\<lambda>z. d z w))"
+        apply (rule contour_integral_swap)
+        apply (rule continuous_on_subset [OF cond_uu])
+        using abu pasz \<open>valid_path \<gamma>\<close>
+        apply (auto intro!: continuous_intros)
+        by (metis \<gamma>' continuous_on_eq path_def path_polynomial_function pf\<gamma>' vector_derivative_at)
+      finally have cint_h_eq:
+          "contour_integral (linepath a b) h =
+                    contour_integral \<gamma> (\<lambda>w. contour_integral (linepath a b) (\<lambda>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 \<in> U" and au: "\<forall>n. a n \<in> U" and ax: "a \<longlonglongrightarrow> x"
+      then have A1: "\<forall>\<^sub>F n in sequentially. d (a n) contour_integrable_on \<gamma>"
+        by (meson U contour_integrable_on_def eventuallyI)
+      obtain dd where "dd>0" and dd: "cball x dd \<subseteq> U" using open_contains_cball \<open>open U\<close> x by force
+      have A2: "uniform_limit (path_image \<gamma>) (\<lambda>n. d (a n)) (d x) sequentially"
+        unfolding uniform_limit_iff dist_norm
+      proof clarify
+        fix ee::real
+        assume "0 < ee"
+        show "\<forall>\<^sub>F n in sequentially. \<forall>\<xi>\<in>path_image \<gamma>. cmod (d (a n) \<xi> - d x \<xi>) < ee"
+        proof -
+          let ?ddpa = "{(w,z) |w z. w \<in> cball x dd \<and> z \<in> path_image \<gamma>}"
+          have "uniformly_continuous_on ?ddpa (\<lambda>(x,y). d x y)"
+            apply (rule compact_uniformly_continuous [OF continuous_on_subset[OF cond_uu]])
+            using dd pasz \<open>valid_path \<gamma>\<close>
+             apply (auto simp: compact_Times compact_valid_path_image simp del: mem_cball)
+            done
+          then obtain kk where "kk>0"
+            and kk: "\<And>x x'. \<lbrakk>x \<in> ?ddpa; x' \<in> ?ddpa; dist x' x < kk\<rbrakk> \<Longrightarrow>
+                             dist ((\<lambda>(x,y). d x y) x') ((\<lambda>(x,y). d x y) x) < ee"
+            by (rule uniformly_continuous_onE [where e = ee]) (use \<open>0 < ee\<close> in auto)
+          have kk: "\<lbrakk>norm (w - x) \<le> dd; z \<in> path_image \<gamma>; norm ((w, z) - (x, z)) < kk\<rbrakk> \<Longrightarrow> norm (d w z - d x z) < ee"
+            for  w z
+            using \<open>dd>0\<close> kk [of "(x,z)" "(w,z)"] by (force simp: norm_minus_commute dist_norm)
+          show ?thesis
+            using ax unfolding lim_sequentially eventually_sequentially
+            apply (drule_tac x="min dd kk" in spec)
+            using \<open>dd > 0\<close> \<open>kk > 0\<close>
+            apply (fastforce simp: kk dist_norm)
+            done
+        qed
+      qed
+      have "(\<lambda>n. contour_integral \<gamma> (d (a n))) \<longlonglongrightarrow> contour_integral \<gamma> (d x)"
+        by (rule contour_integral_uniform_limit [OF A1 A2 le_B]) (auto simp: \<open>valid_path \<gamma>\<close>)
+      then have tendsto_hx: "(\<lambda>n. contour_integral \<gamma> (d (a n))) \<longlonglongrightarrow> h x"
+        by (simp add: h_def x)
+      then show "(h \<circ> a) \<longlonglongrightarrow> 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 \<in> v" | "z0 \<in> U" using uv_Un by blast
+      then show "h field_differentiable at z0"
+      proof cases
+        assume "z0 \<in> v" then show ?thesis
+          using Cauchy_next_derivative [OF con_pa_f le_B f_has_cint _ ov] V f_has_cint \<open>valid_path \<gamma>\<close>
+          by (auto simp: field_differentiable_def v_def)
+      next
+        assume "z0 \<in> U" then
+        obtain e where "e>0" and e: "ball z0 e \<subseteq> U" by (blast intro: openE [OF \<open>open U\<close>])
+        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} \<subseteq> ball z0 e"  for a b c
+        proof -
+          have *: "\<And>x1 x2 z. z \<in> U \<Longrightarrow> closed_segment x1 x2 \<subseteq> U \<Longrightarrow> (\<lambda>w. d w z) contour_integrable_on linepath x1 x2"
+            using  hol_dw holomorphic_on_imp_continuous_on \<open>open U\<close>
+            by (auto intro!: contour_integrable_holomorphic_simple)
+          have abc: "closed_segment a b \<subseteq> U"  "closed_segment b c \<subseteq> U"  "closed_segment c a \<subseteq> U"
+            using that e segments_subset_convex_hull by fastforce+
+          have eq0: "\<And>w. w \<in> U \<Longrightarrow> contour_integral (linepath a b +++ linepath b c +++ linepath c a) (\<lambda>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 \<gamma>
+                   (\<lambda>x. contour_integral (linepath a b) (\<lambda>z. d z x) +
+                        (contour_integral (linepath b c) (\<lambda>z. d z x) +
+                         contour_integral (linepath c a) (\<lambda>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 \<open>e > 0\<close>
+          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 "((\<lambda>w. 1 / (w - z)) has_contour_integral complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z) \<gamma>"
+    by (rule has_contour_integral_winding_number [OF \<open>valid_path \<gamma>\<close> znot])
+  then have "((\<lambda>w. f z * (1 / (w - z))) has_contour_integral complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z * f z) \<gamma>"
+    by (metis mult.commute has_contour_integral_lmul)
+  then have 1: "((\<lambda>w. f z / (w - z)) has_contour_integral complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z * f z) \<gamma>"
+    by (simp add: field_split_simps)
+  moreover have 2: "((\<lambda>w. (f w - f z) / (w - z)) has_contour_integral 0) \<gamma>"
+    using U [OF z] pasz d_def by (force elim: has_contour_integral_eq [where g = "\<lambda>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 \<in> S" and vpg: "valid_path \<gamma>"
+        and pasz: "path_image \<gamma> \<subseteq> S - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
+        and zero: "\<And>w. w \<notin> S \<Longrightarrow> winding_number \<gamma> w = 0"
+      shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
+proof -
+  have "path \<gamma>" using vpg by (blast intro: valid_path_imp_path)
+  have hols: "(\<lambda>w. f w / (w - z)) holomorphic_on S - {z}" "(\<lambda>w. 1 / (w - z)) holomorphic_on S - {z}"
+    by (rule holomorphic_intros holomorphic_on_subset [OF holf] | force)+
+  then have cint_fw: "(\<lambda>w. f w / (w - z)) contour_integrable_on \<gamma>"
+    by (meson contour_integrable_holomorphic_simple holomorphic_on_imp_continuous_on open_delete S vpg pasz)
+  obtain d where "d>0"
+      and d: "\<And>g h. \<lbrakk>valid_path g; valid_path h; \<forall>t\<in>{0..1}. cmod (g t - \<gamma> t) < d \<and> cmod (h t - \<gamma> t) < d;
+                     pathstart h = pathstart g \<and> pathfinish h = pathfinish g\<rbrakk>
+                     \<Longrightarrow> path_image h \<subseteq> S - {z} \<and> (\<forall>f. f holomorphic_on S - {z} \<longrightarrow> contour_integral h f = contour_integral g f)"
+    using contour_integral_nearby_ends [OF _ \<open>path \<gamma>\<close> pasz] S by (simp add: open_Diff) metis
+  obtain p where polyp: "polynomial_function p"
+             and ps: "pathstart p = pathstart \<gamma>" and pf: "pathfinish p = pathfinish \<gamma>" and led: "\<forall>t\<in>{0..1}. cmod (p t - \<gamma> t) < d"
+    using path_approx_polynomial_function [OF \<open>path \<gamma>\<close> \<open>d > 0\<close>] 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 \<notin> path_image \<gamma>" using pasz by blast
+  have paps: "path_image p \<subseteq> S - {z}" and cint_eq: "(\<And>f. f holomorphic_on S - {z} \<Longrightarrow> contour_integral p f = contour_integral \<gamma> f)"
+    using pf ps led d [OF vpg vpp] \<open>d > 0\<close> by auto
+  have wn_eq: "winding_number p z = winding_number \<gamma> 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 \<gamma> w" if "w \<notin> S" for w
+  proof -
+    have hol: "(\<lambda>v. 1 / (v - w)) holomorphic_on S - {z}"
+      using that by (force intro: holomorphic_intros holomorphic_on_subset [OF holf])
+   have "w \<notin> path_image p" "w \<notin> path_image \<gamma>" 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: "\<And>w. w \<notin> S \<Longrightarrow> 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 \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
+        and pas: "path_image \<gamma> \<subseteq> S"
+        and zero: "\<And>w. w \<notin> S \<Longrightarrow> winding_number \<gamma> w = 0"
+      shows "(f has_contour_integral 0) \<gamma>"
+proof -
+  obtain z where "z \<in> S" and znot: "z \<notin> path_image \<gamma>"
+  proof -
+    have "compact (path_image \<gamma>)"
+      using compact_valid_path_image vpg by blast
+    then have "path_image \<gamma> \<noteq> 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 \<gamma> \<subseteq> S - {z}" using pas by blast
+  have hol: "(\<lambda>w. (w - z) * f w) holomorphic_on S"
+    by (rule holomorphic_intros holf)+
+  show ?thesis
+    using Cauchy_integral_formula_global [OF S hol \<open>z \<in> S\<close> 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 \<gamma>"  "pathfinish \<gamma> = pathstart \<gamma>" "path_image \<gamma> \<subseteq> S"
+            "\<And>w. w \<notin> S \<Longrightarrow> w \<in> outside(path_image \<gamma>)"
+      shows "(f has_contour_integral 0) \<gamma>"
+by (metis Cauchy_theorem_global assms winding_number_zero_in_outside valid_path_imp_path)
+
+lemma simply_connected_imp_winding_number_zero:
+  assumes "simply_connected S" "path g"
+           "path_image g \<subseteq> S" "pathfinish g = pathstart g" "z \<notin> S"
+    shows "winding_number g z = 0"
+proof -
+  have hom: "homotopic_loops S g (linepath (pathstart g) (pathstart g))"
+    by (meson assms homotopic_paths_imp_homotopic_loops pathfinish_linepath simply_connected_eq_contractible_path)
+  then have "homotopic_paths (- {z}) g (linepath (pathstart g) (pathstart g))"
+    by (meson \<open>z \<notin> S\<close> homotopic_loops_imp_homotopic_paths_null homotopic_paths_subset subset_Compl_singleton)
+  then have "winding_number g z = winding_number(linepath (pathstart g) (pathstart g)) z"
+    by (rule winding_number_homotopic_paths)
+  also have "\<dots> = 0"
+    using assms by (force intro: winding_number_trivial)
+  finally show ?thesis .
+qed
+
+lemma Cauchy_theorem_simply_connected:
+  assumes "open S" "simply_connected S" "f holomorphic_on S" "valid_path g"
+           "path_image g \<subseteq> S" "pathfinish g = pathstart g"
+    shows "(f has_contour_integral 0) g"
+using assms
+apply (simp add: simply_connected_eq_contractible_path)
+apply (auto intro!: Cauchy_theorem_null_homotopic [where a = "pathstart g"]
+                         homotopic_paths_imp_homotopic_loops)
+using valid_path_imp_path by blast
+
+proposition\<^marker>\<open>tag unimportant\<close> holomorphic_logarithm_exists:
+  assumes A: "convex A" "open A"
+      and f: "f holomorphic_on A" "\<And>x. x \<in> A \<Longrightarrow> f x \<noteq> 0"
+      and z0: "z0 \<in> A"
+    obtains g where "g holomorphic_on A" and "\<And>x. x \<in> A \<Longrightarrow> exp (g x) = f x"
+proof -
+  note f' = holomorphic_derivI [OF f(1) A(2)]
+  obtain g where g: "\<And>x. x \<in> A \<Longrightarrow> (g has_field_derivative deriv f x / f x) (at x)"
+  proof (rule holomorphic_convex_primitive' [OF A])
+    show "(\<lambda>x. deriv f x / f x) holomorphic_on A"
+      by (intro holomorphic_intros f A)
+  qed (auto simp: A at_within_open[of _ A])
+  define h where "h = (\<lambda>x. -g z0 + ln (f z0) + g x)"
+  from g and A have g_holo: "g holomorphic_on A"
+    by (auto simp: holomorphic_on_def at_within_open[of _ A] field_differentiable_def)
+  hence h_holo: "h holomorphic_on A"
+    by (auto simp: h_def intro!: holomorphic_intros)
+  have "\<exists>c. \<forall>x\<in>A. f x / exp (h x) - 1 = c"
+  proof (rule has_field_derivative_zero_constant, goal_cases)
+    case (2 x)
+    note [simp] = at_within_open[OF _ \<open>open A\<close>]
+    from 2 and z0 and f show ?case
+      by (auto simp: h_def exp_diff field_simps intro!: derivative_eq_intros g f')
+  qed fact+
+  then obtain c where c: "\<And>x. x \<in> A \<Longrightarrow> f x / exp (h x) - 1 = c"
+    by blast
+  from c[OF z0] and z0 and f have "c = 0"
+    by (simp add: h_def)
+  with c have "\<And>x. x \<in> A \<Longrightarrow> exp (h x) = f x" by simp
+  from that[OF h_holo this] show ?thesis .
+qed
+
+subsection \<open>Complex functions and power series\<close>
+
+text \<open>
+  The following defines the power series expansion of a complex function at a given point
+  (assuming that it is analytic at that point).
+\<close>
+definition\<^marker>\<open>tag important\<close> fps_expansion :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> complex fps" where
+  "fps_expansion f z0 = Abs_fps (\<lambda>n. (deriv ^^ n) f z0 / fact n)"
+
+lemma
+  fixes r :: ereal
+  assumes "f holomorphic_on eball z0 r"
+  shows   conv_radius_fps_expansion: "fps_conv_radius (fps_expansion f z0) \<ge> r"
+    and   eval_fps_expansion: "\<And>z. z \<in> eball z0 r \<Longrightarrow> eval_fps (fps_expansion f z0) (z - z0) = f z"
+    and   eval_fps_expansion': "\<And>z. norm z < r \<Longrightarrow> eval_fps (fps_expansion f z0) z = f (z0 + z)"
+proof -
+  have "(\<lambda>n. fps_nth (fps_expansion f z0) n * (z - z0) ^ n) sums f z"
+    if "z \<in> ball z0 r'" "ereal r' < r" for z r'
+  proof -
+    from that(2) have "ereal r' \<le> r" by simp
+    from assms(1) and this have "f holomorphic_on ball z0 r'"
+      by (rule holomorphic_on_subset[OF _ ball_eball_mono])
+    from holomorphic_power_series [OF this that(1)] 
+      show ?thesis by (simp add: fps_expansion_def)
+  qed
+  hence *: "(\<lambda>n. fps_nth (fps_expansion f z0) n * (z - z0) ^ n) sums f z"
+    if "z \<in> eball z0 r" for z
+    using that by (subst (asm) eball_conv_UNION_balls) blast
+  show "fps_conv_radius (fps_expansion f z0) \<ge> r" unfolding fps_conv_radius_def
+  proof (rule conv_radius_geI_ex)
+    fix r' :: real assume r': "r' > 0" "ereal r' < r"
+    thus "\<exists>z. norm z = r' \<and> summable (\<lambda>n. fps_nth (fps_expansion f z0) n * z ^ n)"
+      using *[of "z0 + of_real r'"]
+      by (intro exI[of _ "of_real r'"]) (auto simp: summable_def dist_norm)
+  qed
+  show "eval_fps (fps_expansion f z0) (z - z0) = f z" if "z \<in> eball z0 r" for z
+    using *[OF that] by (simp add: eval_fps_def sums_iff)
+  show "eval_fps (fps_expansion f z0) z = f (z0 + z)" if "ereal (norm z) < r" for z
+    using *[of "z0 + z"] and that by (simp add: eval_fps_def sums_iff dist_norm)
+qed
+
+
+text \<open>
+  We can now show several more facts about power series expansions (at least in the complex case)
+  with relative ease that would have been trickier without complex analysis.
+\<close>
+lemma
+  fixes f :: "complex fps" and r :: ereal
+  assumes "\<And>z. ereal (norm z) < r \<Longrightarrow> eval_fps f z \<noteq> 0"
+  shows   fps_conv_radius_inverse: "fps_conv_radius (inverse f) \<ge> min r (fps_conv_radius f)"
+    and   eval_fps_inverse: "\<And>z. ereal (norm z) < fps_conv_radius f \<Longrightarrow> ereal (norm z) < r \<Longrightarrow> 
+                               eval_fps (inverse f) z = inverse (eval_fps f z)"
+proof -
+  define R where "R = min (fps_conv_radius f) r"
+  have *: "fps_conv_radius (inverse f) \<ge> min r (fps_conv_radius f) \<and> 
+          (\<forall>z\<in>eball 0 (min (fps_conv_radius f) r). eval_fps (inverse f) z = inverse (eval_fps f z))"
+  proof (cases "min r (fps_conv_radius f) > 0")
+    case True
+    define f' where "f' = fps_expansion (\<lambda>z. inverse (eval_fps f z)) 0"
+    have holo: "(\<lambda>z. inverse (eval_fps f z)) holomorphic_on eball 0 (min r (fps_conv_radius f))"
+      using assms by (intro holomorphic_intros) auto
+    from holo have radius: "fps_conv_radius f' \<ge> min r (fps_conv_radius f)"
+      unfolding f'_def by (rule conv_radius_fps_expansion)
+    have eval_f': "eval_fps f' z = inverse (eval_fps f z)" 
+      if "norm z < fps_conv_radius f" "norm z < r" for z
+      using that unfolding f'_def by (subst eval_fps_expansion'[OF holo]) auto
+  
+    have "f * f' = 1"
+    proof (rule eval_fps_eqD)
+      from radius and True have "0 < min (fps_conv_radius f) (fps_conv_radius f')"
+        by (auto simp: min_def split: if_splits)
+      also have "\<dots> \<le> fps_conv_radius (f * f')" by (rule fps_conv_radius_mult)
+      finally show "\<dots> > 0" .
+    next
+      from True have "R > 0" by (auto simp: R_def)
+      hence "eventually (\<lambda>z. z \<in> eball 0 R) (nhds 0)"
+        by (intro eventually_nhds_in_open) (auto simp: zero_ereal_def)
+      thus "eventually (\<lambda>z. eval_fps (f * f') z = eval_fps 1 z) (nhds 0)"
+      proof eventually_elim
+        case (elim z)
+        hence "eval_fps (f * f') z = eval_fps f z * eval_fps f' z"
+          using radius by (intro eval_fps_mult) 
+                          (auto simp: R_def min_def split: if_splits intro: less_trans)
+        also have "eval_fps f' z = inverse (eval_fps f z)"
+          using elim by (intro eval_f') (auto simp: R_def)
+        also from elim have "eval_fps f z \<noteq> 0"
+          by (intro assms) (auto simp: R_def)
+        hence "eval_fps f z * inverse (eval_fps f z) = eval_fps 1 z" 
+          by simp
+        finally show "eval_fps (f * f') z = eval_fps 1 z" .
+      qed
+    qed simp_all
+    hence "f' = inverse f"
+      by (intro fps_inverse_unique [symmetric]) (simp_all add: mult_ac)
+    with eval_f' and radius show ?thesis by simp
+  next
+    case False
+    hence *: "eball 0 R = {}" 
+      by (intro eball_empty) (auto simp: R_def min_def split: if_splits)
+    show ?thesis
+    proof safe
+      from False have "min r (fps_conv_radius f) \<le> 0"
+        by (simp add: min_def)
+      also have "0 \<le> fps_conv_radius (inverse f)"
+        by (simp add: fps_conv_radius_def conv_radius_nonneg)
+      finally show "min r (fps_conv_radius f) \<le> \<dots>" .
+    qed (unfold * [unfolded R_def], auto)
+  qed
+
+  from * show "fps_conv_radius (inverse f) \<ge> min r (fps_conv_radius f)" by blast
+  from * show "eval_fps (inverse f) z = inverse (eval_fps f z)" 
+    if "ereal (norm z) < fps_conv_radius f" "ereal (norm z) < r" for z
+    using that by auto
+qed
+
+lemma
+  fixes f g :: "complex fps" and r :: ereal
+  defines "R \<equiv> Min {r, fps_conv_radius f, fps_conv_radius g}"
+  assumes "fps_conv_radius f > 0" "fps_conv_radius g > 0" "r > 0"
+  assumes nz: "\<And>z. z \<in> eball 0 r \<Longrightarrow> eval_fps g z \<noteq> 0"
+  shows   fps_conv_radius_divide': "fps_conv_radius (f / g) \<ge> R"
+    and   eval_fps_divide':
+            "ereal (norm z) < R \<Longrightarrow> eval_fps (f / g) z = eval_fps f z / eval_fps g z"
+proof -
+  from nz[of 0] and \<open>r > 0\<close> have nz': "fps_nth g 0 \<noteq> 0" 
+    by (auto simp: eval_fps_at_0 zero_ereal_def)
+  have "R \<le> min r (fps_conv_radius g)"
+    by (auto simp: R_def intro: min.coboundedI2)
+  also have "min r (fps_conv_radius g) \<le> fps_conv_radius (inverse g)"
+    by (intro fps_conv_radius_inverse assms) (auto simp: zero_ereal_def)
+  finally have radius: "fps_conv_radius (inverse g) \<ge> R" .
+  have "R \<le> min (fps_conv_radius f) (fps_conv_radius (inverse g))"
+    by (intro radius min.boundedI) (auto simp: R_def intro: min.coboundedI1 min.coboundedI2)
+  also have "\<dots> \<le> fps_conv_radius (f * inverse g)"
+    by (rule fps_conv_radius_mult)
+  also have "f * inverse g = f / g"
+    by (intro fps_divide_unit [symmetric] nz')
+  finally show "fps_conv_radius (f / g) \<ge> R" .
+
+  assume z: "ereal (norm z) < R"
+  have "eval_fps (f * inverse g) z = eval_fps f z * eval_fps (inverse g) z"
+    using radius by (intro eval_fps_mult less_le_trans[OF z])
+                    (auto simp: R_def intro: min.coboundedI1 min.coboundedI2)
+  also have "eval_fps (inverse g) z = inverse (eval_fps g z)" using \<open>r > 0\<close>
+    by (intro eval_fps_inverse[where r = r] less_le_trans[OF z] nz)
+       (auto simp: R_def intro: min.coboundedI1 min.coboundedI2)
+  also have "f * inverse g = f / g" by fact
+  finally show "eval_fps (f / g) z = eval_fps f z / eval_fps g z" by (simp add: field_split_simps)
+qed
+
+lemma
+  fixes f g :: "complex fps" and r :: ereal
+  defines "R \<equiv> Min {r, fps_conv_radius f, fps_conv_radius g}"
+  assumes "subdegree g \<le> subdegree f"
+  assumes "fps_conv_radius f > 0" "fps_conv_radius g > 0" "r > 0"
+  assumes "\<And>z. z \<in> eball 0 r \<Longrightarrow> z \<noteq> 0 \<Longrightarrow> eval_fps g z \<noteq> 0"
+  shows   fps_conv_radius_divide: "fps_conv_radius (f / g) \<ge> R"
+    and   eval_fps_divide:
+            "ereal (norm z) < R \<Longrightarrow> c = fps_nth f (subdegree g) / fps_nth g (subdegree g) \<Longrightarrow>
+               eval_fps (f / g) z = (if z = 0 then c else eval_fps f z / eval_fps g z)"
+proof -
+  define f' g' where "f' = fps_shift (subdegree g) f" and "g' = fps_shift (subdegree g) g"
+  have f_eq: "f = f' * fps_X ^ subdegree g" and g_eq: "g = g' * fps_X ^ subdegree g"
+    unfolding f'_def g'_def by (rule subdegree_decompose' le_refl | fact)+
+  have subdegree: "subdegree f' = subdegree f - subdegree g" "subdegree g' = 0"
+    using assms(2) by (simp_all add: f'_def g'_def)
+  have [simp]: "fps_conv_radius f' = fps_conv_radius f" "fps_conv_radius g' = fps_conv_radius g"
+    by (simp_all add: f'_def g'_def)
+  have [simp]: "fps_nth f' 0 = fps_nth f (subdegree g)"
+               "fps_nth g' 0 = fps_nth g (subdegree g)" by (simp_all add: f'_def g'_def)
+  have g_nz: "g \<noteq> 0"
+  proof -
+    define z :: complex where "z = (if r = \<infinity> then 1 else of_real (real_of_ereal r / 2))"
+    from \<open>r > 0\<close> have "z \<in> eball 0 r"
+      by (cases r) (auto simp: z_def eball_def)
+    moreover have "z \<noteq> 0" using \<open>r > 0\<close> 
+      by (cases r) (auto simp: z_def)
+    ultimately have "eval_fps g z \<noteq> 0" by (rule assms(6))
+    thus "g \<noteq> 0" by auto
+  qed
+  have fg: "f / g = f' * inverse g'"
+    by (subst f_eq, subst (2) g_eq) (insert g_nz, simp add: fps_divide_unit)
+
+  have g'_nz: "eval_fps g' z \<noteq> 0" if z: "norm z < min r (fps_conv_radius g)" for z
+  proof (cases "z = 0")
+    case False
+    with assms and z have "eval_fps g z \<noteq> 0" by auto
+    also from z have "eval_fps g z = eval_fps g' z * z ^ subdegree g"
+      by (subst g_eq) (auto simp: eval_fps_mult)
+    finally show ?thesis by auto
+  qed (insert \<open>g \<noteq> 0\<close>, auto simp: g'_def eval_fps_at_0)
+
+  have "R \<le> min (min r (fps_conv_radius g)) (fps_conv_radius g')"
+    by (auto simp: R_def min.coboundedI1 min.coboundedI2)
+  also have "\<dots> \<le> fps_conv_radius (inverse g')"
+    using g'_nz by (rule fps_conv_radius_inverse)
+  finally have conv_radius_inv: "R \<le> fps_conv_radius (inverse g')" .
+  hence "R \<le> fps_conv_radius (f' * inverse g')"
+    by (intro order.trans[OF _ fps_conv_radius_mult])
+       (auto simp: R_def intro: min.coboundedI1 min.coboundedI2)
+  thus "fps_conv_radius (f / g) \<ge> R" by (simp add: fg)
+
+  fix z c :: complex assume z: "ereal (norm z) < R"
+  assume c: "c = fps_nth f (subdegree g) / fps_nth g (subdegree g)"
+  show "eval_fps (f / g) z = (if z = 0 then c else eval_fps f z / eval_fps g z)"
+  proof (cases "z = 0")
+    case False
+    from z and conv_radius_inv have "ereal (norm z) < fps_conv_radius (inverse g')"
+      by simp
+    with z have "eval_fps (f / g) z = eval_fps f' z * eval_fps (inverse g') z"
+      unfolding fg by (subst eval_fps_mult) (auto simp: R_def)
+    also have "eval_fps (inverse g') z = inverse (eval_fps g' z)"
+      using z by (intro eval_fps_inverse[of "min r (fps_conv_radius g')"] g'_nz) (auto simp: R_def)
+    also have "eval_fps f' z * \<dots> = eval_fps f z / eval_fps g z"
+      using z False assms(2) by (simp add: f'_def g'_def eval_fps_shift R_def)
+    finally show ?thesis using False by simp
+  qed (simp_all add: eval_fps_at_0 fg field_simps c)
+qed
+
+lemma has_fps_expansion_fps_expansion [intro]:
+  assumes "open A" "0 \<in> A" "f holomorphic_on A"
+  shows   "f has_fps_expansion fps_expansion f 0"
+proof -
+  from assms(1,2) obtain r where r: "r > 0 " "ball 0 r \<subseteq> A"
+    by (auto simp: open_contains_ball)
+  have holo: "f holomorphic_on eball 0 (ereal r)" 
+    using r(2) and assms(3) by auto
+  from r(1) have "0 < ereal r" by simp
+  also have "r \<le> fps_conv_radius (fps_expansion f 0)"
+    using holo by (intro conv_radius_fps_expansion) auto
+  finally have "\<dots> > 0" .
+  moreover have "eventually (\<lambda>z. z \<in> ball 0 r) (nhds 0)"
+    using r(1) by (intro eventually_nhds_in_open) auto
+  hence "eventually (\<lambda>z. eval_fps (fps_expansion f 0) z = f z) (nhds 0)"
+    by eventually_elim (subst eval_fps_expansion'[OF holo], auto)
+  ultimately show ?thesis using r(1) by (auto simp: has_fps_expansion_def)
+qed
+
+lemma fps_conv_radius_tan:
+  fixes c :: complex
+  assumes "c \<noteq> 0"
+  shows   "fps_conv_radius (fps_tan c) \<ge> pi / (2 * norm c)"
+proof -
+  have "fps_conv_radius (fps_tan c) \<ge> 
+          Min {pi / (2 * norm c), fps_conv_radius (fps_sin c), fps_conv_radius (fps_cos c)}"
+    unfolding fps_tan_def
+  proof (rule fps_conv_radius_divide)
+    fix z :: complex assume "z \<in> eball 0 (pi / (2 * norm c))"
+    with cos_eq_zero_imp_norm_ge[of "c*z"] assms 
+      show "eval_fps (fps_cos  c) z \<noteq> 0" by (auto simp: norm_mult field_simps)
+  qed (insert assms, auto)
+  thus ?thesis by (simp add: min_def)
+qed
+
+lemma eval_fps_tan:
+  fixes c :: complex
+  assumes "norm z < pi / (2 * norm c)"
+  shows   "eval_fps (fps_tan c) z = tan (c * z)"
+proof (cases "c = 0")
+  case False
+  show ?thesis unfolding fps_tan_def
+  proof (subst eval_fps_divide'[where r = "pi / (2 * norm c)"])
+    fix z :: complex assume "z \<in> eball 0 (pi / (2 * norm c))"
+    with cos_eq_zero_imp_norm_ge[of "c*z"] assms 
+      show "eval_fps (fps_cos  c) z \<noteq> 0" using False by (auto simp: norm_mult field_simps)
+    qed (insert False assms, auto simp: field_simps tan_def)
+qed simp_all
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Complex_Analysis/Complex_Analysis.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -0,0 +1,6 @@
+theory Complex_Analysis
+  imports
+  Winding_Numbers
+begin
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Complex_Analysis/Conformal_Mappings.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -0,0 +1,5116 @@
+section \<open>Conformal Mappings and Consequences of Cauchy's Integral Theorem\<close>
+
+text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2016)\<close>
+
+text\<open>Also Cauchy's residue theorem by Wenda Li (2016)\<close>
+
+theory Conformal_Mappings
+imports Cauchy_Integral_Theorem
+
+begin
+
+(* FIXME mv to Cauchy_Integral_Theorem.thy *)
+subsection\<open>Cauchy's inequality and more versions of Liouville\<close>
+
+lemma Cauchy_higher_deriv_bound:
+    assumes holf: "f holomorphic_on (ball z r)"
+        and contf: "continuous_on (cball z r) f"
+        and fin : "\<And>w. w \<in> ball z r \<Longrightarrow> f w \<in> ball y B0"
+        and "0 < r" and "0 < n"
+      shows "norm ((deriv ^^ n) f z) \<le> (fact n) * B0 / r^n"
+proof -
+  have "0 < B0" using \<open>0 < r\<close> fin [of z]
+    by (metis ball_eq_empty ex_in_conv fin not_less)
+  have le_B0: "\<And>w. cmod (w - z) \<le> r \<Longrightarrow> cmod (f w - y) \<le> B0"
+    apply (rule continuous_on_closure_norm_le [of "ball z r" "\<lambda>w. f w - y"])
+    apply (auto simp: \<open>0 < r\<close>  dist_norm norm_minus_commute)
+    apply (rule continuous_intros contf)+
+    using fin apply (simp add: dist_commute dist_norm less_eq_real_def)
+    done
+  have "(deriv ^^ n) f z = (deriv ^^ n) (\<lambda>w. f w) z - (deriv ^^ n) (\<lambda>w. y) z"
+    using \<open>0 < n\<close> by simp
+  also have "... = (deriv ^^ n) (\<lambda>w. f w - y) z"
+    by (rule higher_deriv_diff [OF holf, symmetric]) (auto simp: \<open>0 < r\<close>)
+  finally have "(deriv ^^ n) f z = (deriv ^^ n) (\<lambda>w. f w - y) z" .
+  have contf': "continuous_on (cball z r) (\<lambda>u. f u - y)"
+    by (rule contf continuous_intros)+
+  have holf': "(\<lambda>u. (f u - y)) holomorphic_on (ball z r)"
+    by (simp add: holf holomorphic_on_diff)
+  define a where "a = (2 * pi)/(fact n)"
+  have "0 < a"  by (simp add: a_def)
+  have "B0/r^(Suc n)*2 * pi * r = a*((fact n)*B0/r^n)"
+    using \<open>0 < r\<close> by (simp add: a_def field_split_simps)
+  have der_dif: "(deriv ^^ n) (\<lambda>w. f w - y) z = (deriv ^^ n) f z"
+    using \<open>0 < r\<close> \<open>0 < n\<close>
+    by (auto simp: higher_deriv_diff [OF holf holomorphic_on_const])
+  have "norm ((2 * of_real pi * \<i>)/(fact n) * (deriv ^^ n) (\<lambda>w. f w - y) z)
+        \<le> (B0/r^(Suc n)) * (2 * pi * r)"
+    apply (rule has_contour_integral_bound_circlepath [of "(\<lambda>u. (f u - y)/(u - z)^(Suc n))" _ z])
+    using Cauchy_has_contour_integral_higher_derivative_circlepath [OF contf' holf']
+    using \<open>0 < B0\<close> \<open>0 < r\<close>
+    apply (auto simp: norm_divide norm_mult norm_power divide_simps le_B0)
+    done
+  then show ?thesis
+    using \<open>0 < r\<close>
+    by (auto simp: norm_divide norm_mult norm_power field_simps der_dif le_B0)
+qed
+
+lemma Cauchy_inequality:
+    assumes holf: "f holomorphic_on (ball \<xi> r)"
+        and contf: "continuous_on (cball \<xi> r) f"
+        and "0 < r"
+        and nof: "\<And>x. norm(\<xi>-x) = r \<Longrightarrow> norm(f x) \<le> B"
+      shows "norm ((deriv ^^ n) f \<xi>) \<le> (fact n) * B / r^n"
+proof -
+  obtain x where "norm (\<xi>-x) = r"
+    by (metis abs_of_nonneg add_diff_cancel_left' \<open>0 < r\<close> diff_add_cancel
+                 dual_order.strict_implies_order norm_of_real)
+  then have "0 \<le> B"
+    by (metis nof norm_not_less_zero not_le order_trans)
+  have  "((\<lambda>u. f u / (u - \<xi>) ^ Suc n) has_contour_integral (2 * pi) * \<i> / fact n * (deriv ^^ n) f \<xi>)
+         (circlepath \<xi> r)"
+    apply (rule Cauchy_has_contour_integral_higher_derivative_circlepath [OF contf holf])
+    using \<open>0 < r\<close> by simp
+  then have "norm ((2 * pi * \<i>)/(fact n) * (deriv ^^ n) f \<xi>) \<le> (B / r^(Suc n)) * (2 * pi * r)"
+    apply (rule has_contour_integral_bound_circlepath)
+    using \<open>0 \<le> B\<close> \<open>0 < r\<close>
+    apply (simp_all add: norm_divide norm_power nof frac_le norm_minus_commute del: power_Suc)
+    done
+  then show ?thesis using \<open>0 < r\<close>
+    by (simp add: norm_divide norm_mult field_simps)
+qed
+
+lemma Liouville_polynomial:
+    assumes holf: "f holomorphic_on UNIV"
+        and nof: "\<And>z. A \<le> norm z \<Longrightarrow> norm(f z) \<le> B * norm z ^ n"
+      shows "f \<xi> = (\<Sum>k\<le>n. (deriv^^k) f 0 / fact k * \<xi> ^ k)"
+proof (cases rule: le_less_linear [THEN disjE])
+  assume "B \<le> 0"
+  then have "\<And>z. A \<le> norm z \<Longrightarrow> norm(f z) = 0"
+    by (metis nof less_le_trans zero_less_mult_iff neqE norm_not_less_zero norm_power not_le)
+  then have f0: "(f \<longlongrightarrow> 0) at_infinity"
+    using Lim_at_infinity by force
+  then have [simp]: "f = (\<lambda>w. 0)"
+    using Liouville_weak [OF holf, of 0]
+    by (simp add: eventually_at_infinity f0) meson
+  show ?thesis by simp
+next
+  assume "0 < B"
+  have "((\<lambda>k. (deriv ^^ k) f 0 / (fact k) * (\<xi> - 0)^k) sums f \<xi>)"
+    apply (rule holomorphic_power_series [where r = "norm \<xi> + 1"])
+    using holf holomorphic_on_subset apply auto
+    done
+  then have sumsf: "((\<lambda>k. (deriv ^^ k) f 0 / (fact k) * \<xi>^k) sums f \<xi>)" by simp
+  have "(deriv ^^ k) f 0 / fact k * \<xi> ^ k = 0" if "k>n" for k
+  proof (cases "(deriv ^^ k) f 0 = 0")
+    case True then show ?thesis by simp
+  next
+    case False
+    define w where "w = complex_of_real (fact k * B / cmod ((deriv ^^ k) f 0) + (\<bar>A\<bar> + 1))"
+    have "1 \<le> abs (fact k * B / cmod ((deriv ^^ k) f 0) + (\<bar>A\<bar> + 1))"
+      using \<open>0 < B\<close> by simp
+    then have wge1: "1 \<le> norm w"
+      by (metis norm_of_real w_def)
+    then have "w \<noteq> 0" by auto
+    have kB: "0 < fact k * B"
+      using \<open>0 < B\<close> by simp
+    then have "0 \<le> fact k * B / cmod ((deriv ^^ k) f 0)"
+      by simp
+    then have wgeA: "A \<le> cmod w"
+      by (simp only: w_def norm_of_real)
+    have "fact k * B / cmod ((deriv ^^ k) f 0) < abs (fact k * B / cmod ((deriv ^^ k) f 0) + (\<bar>A\<bar> + 1))"
+      using \<open>0 < B\<close> by simp
+    then have wge: "fact k * B / cmod ((deriv ^^ k) f 0) < norm w"
+      by (metis norm_of_real w_def)
+    then have "fact k * B / norm w < cmod ((deriv ^^ k) f 0)"
+      using False by (simp add: field_split_simps mult.commute split: if_split_asm)
+    also have "... \<le> fact k * (B * norm w ^ n) / norm w ^ k"
+      apply (rule Cauchy_inequality)
+         using holf holomorphic_on_subset apply force
+        using holf holomorphic_on_imp_continuous_on holomorphic_on_subset apply blast
+       using \<open>w \<noteq> 0\<close> apply simp
+       by (metis nof wgeA dist_0_norm dist_norm)
+    also have "... = fact k * (B * 1 / cmod w ^ (k-n))"
+      apply (simp only: mult_cancel_left times_divide_eq_right [symmetric])
+      using \<open>k>n\<close> \<open>w \<noteq> 0\<close> \<open>0 < B\<close> apply (simp add: field_split_simps semiring_normalization_rules)
+      done
+    also have "... = fact k * B / cmod w ^ (k-n)"
+      by simp
+    finally have "fact k * B / cmod w < fact k * B / cmod w ^ (k - n)" .
+    then have "1 / cmod w < 1 / cmod w ^ (k - n)"
+      by (metis kB divide_inverse inverse_eq_divide mult_less_cancel_left_pos)
+    then have "cmod w ^ (k - n) < cmod w"
+      by (metis frac_le le_less_trans norm_ge_zero norm_one not_less order_refl wge1 zero_less_one)
+    with self_le_power [OF wge1] have False
+      by (meson diff_is_0_eq not_gr0 not_le that)
+    then show ?thesis by blast
+  qed
+  then have "(deriv ^^ (k + Suc n)) f 0 / fact (k + Suc n) * \<xi> ^ (k + Suc n) = 0" for k
+    using not_less_eq by blast
+  then have "(\<lambda>i. (deriv ^^ (i + Suc n)) f 0 / fact (i + Suc n) * \<xi> ^ (i + Suc n)) sums 0"
+    by (rule sums_0)
+  with sums_split_initial_segment [OF sumsf, where n = "Suc n"]
+  show ?thesis
+    using atLeast0AtMost lessThan_Suc_atMost sums_unique2 by fastforce
+qed
+
+text\<open>Every bounded entire function is a constant function.\<close>
+theorem Liouville_theorem:
+    assumes holf: "f holomorphic_on UNIV"
+        and bf: "bounded (range f)"
+    obtains c where "\<And>z. f z = c"
+proof -
+  obtain B where "\<And>z. cmod (f z) \<le> B"
+    by (meson bf bounded_pos rangeI)
+  then show ?thesis
+    using Liouville_polynomial [OF holf, of 0 B 0, simplified] that by blast
+qed
+
+text\<open>A holomorphic function f has only isolated zeros unless f is 0.\<close>
+
+lemma powser_0_nonzero:
+  fixes a :: "nat \<Rightarrow> 'a::{real_normed_field,banach}"
+  assumes r: "0 < r"
+      and sm: "\<And>x. norm (x - \<xi>) < r \<Longrightarrow> (\<lambda>n. a n * (x - \<xi>) ^ n) sums (f x)"
+      and [simp]: "f \<xi> = 0"
+      and m0: "a m \<noteq> 0" and "m>0"
+  obtains s where "0 < s" and "\<And>z. z \<in> cball \<xi> s - {\<xi>} \<Longrightarrow> f z \<noteq> 0"
+proof -
+  have "r \<le> conv_radius a"
+    using sm sums_summable by (auto simp: le_conv_radius_iff [where \<xi>=\<xi>])
+  obtain m where am: "a m \<noteq> 0" and az [simp]: "(\<And>n. n<m \<Longrightarrow> a n = 0)"
+    apply (rule_tac m = "LEAST n. a n \<noteq> 0" in that)
+    using m0
+    apply (rule LeastI2)
+    apply (fastforce intro:  dest!: not_less_Least)+
+    done
+  define b where "b i = a (i+m) / a m" for i
+  define g where "g x = suminf (\<lambda>i. b i * (x - \<xi>) ^ i)" for x
+  have [simp]: "b 0 = 1"
+    by (simp add: am b_def)
+  { fix x::'a
+    assume "norm (x - \<xi>) < r"
+    then have "(\<lambda>n. (a m * (x - \<xi>)^m) * (b n * (x - \<xi>)^n)) sums (f x)"
+      using am az sm sums_zero_iff_shift [of m "(\<lambda>n. a n * (x - \<xi>) ^ n)" "f x"]
+      by (simp add: b_def monoid_mult_class.power_add algebra_simps)
+    then have "x \<noteq> \<xi> \<Longrightarrow> (\<lambda>n. b n * (x - \<xi>)^n) sums (f x / (a m * (x - \<xi>)^m))"
+      using am by (simp add: sums_mult_D)
+  } note bsums = this
+  then have  "norm (x - \<xi>) < r \<Longrightarrow> summable (\<lambda>n. b n * (x - \<xi>)^n)" for x
+    using sums_summable by (cases "x=\<xi>") auto
+  then have "r \<le> conv_radius b"
+    by (simp add: le_conv_radius_iff [where \<xi>=\<xi>])
+  then have "r/2 < conv_radius b"
+    using not_le order_trans r by fastforce
+  then have "continuous_on (cball \<xi> (r/2)) g"
+    using powser_continuous_suminf [of "r/2" b \<xi>] by (simp add: g_def)
+  then obtain s where "s>0"  "\<And>x. \<lbrakk>norm (x - \<xi>) \<le> s; norm (x - \<xi>) \<le> r/2\<rbrakk> \<Longrightarrow> dist (g x) (g \<xi>) < 1/2"
+    apply (rule continuous_onE [where x=\<xi> and e = "1/2"])
+    using r apply (auto simp: norm_minus_commute dist_norm)
+    done
+  moreover have "g \<xi> = 1"
+    by (simp add: g_def)
+  ultimately have gnz: "\<And>x. \<lbrakk>norm (x - \<xi>) \<le> s; norm (x - \<xi>) \<le> r/2\<rbrakk> \<Longrightarrow> (g x) \<noteq> 0"
+    by fastforce
+  have "f x \<noteq> 0" if "x \<noteq> \<xi>" "norm (x - \<xi>) \<le> s" "norm (x - \<xi>) \<le> r/2" for x
+    using bsums [of x] that gnz [of x]
+    apply (auto simp: g_def)
+    using r sums_iff by fastforce
+  then show ?thesis
+    apply (rule_tac s="min s (r/2)" in that)
+    using \<open>0 < r\<close> \<open>0 < s\<close> by (auto simp: dist_commute dist_norm)
+qed
+
+subsection \<open>Analytic continuation\<close>
+
+proposition isolated_zeros:
+  assumes holf: "f holomorphic_on S"
+      and "open S" "connected S" "\<xi> \<in> S" "f \<xi> = 0" "\<beta> \<in> S" "f \<beta> \<noteq> 0"
+    obtains r where "0 < r" and "ball \<xi> r \<subseteq> S" and
+        "\<And>z. z \<in> ball \<xi> r - {\<xi>} \<Longrightarrow> f z \<noteq> 0"
+proof -
+  obtain r where "0 < r" and r: "ball \<xi> r \<subseteq> S"
+    using \<open>open S\<close> \<open>\<xi> \<in> S\<close> open_contains_ball_eq by blast
+  have powf: "((\<lambda>n. (deriv ^^ n) f \<xi> / (fact n) * (z - \<xi>)^n) sums f z)" if "z \<in> ball \<xi> r" for z
+    apply (rule holomorphic_power_series [OF _ that])
+    apply (rule holomorphic_on_subset [OF holf r])
+    done
+  obtain m where m: "(deriv ^^ m) f \<xi> / (fact m) \<noteq> 0"
+    using holomorphic_fun_eq_0_on_connected [OF holf \<open>open S\<close> \<open>connected S\<close> _ \<open>\<xi> \<in> S\<close> \<open>\<beta> \<in> S\<close>] \<open>f \<beta> \<noteq> 0\<close>
+    by auto
+  then have "m \<noteq> 0" using assms(5) funpow_0 by fastforce
+  obtain s where "0 < s" and s: "\<And>z. z \<in> cball \<xi> s - {\<xi>} \<Longrightarrow> f z \<noteq> 0"
+    apply (rule powser_0_nonzero [OF \<open>0 < r\<close> powf \<open>f \<xi> = 0\<close> m])
+    using \<open>m \<noteq> 0\<close> by (auto simp: dist_commute dist_norm)
+  have "0 < min r s"  by (simp add: \<open>0 < r\<close> \<open>0 < s\<close>)
+  then show ?thesis
+    apply (rule that)
+    using r s by auto
+qed
+
+proposition analytic_continuation:
+  assumes holf: "f holomorphic_on S"
+      and "open S" and "connected S"
+      and "U \<subseteq> S" and "\<xi> \<in> S"
+      and "\<xi> islimpt U"
+      and fU0 [simp]: "\<And>z. z \<in> U \<Longrightarrow> f z = 0"
+      and "w \<in> S"
+    shows "f w = 0"
+proof -
+  obtain e where "0 < e" and e: "cball \<xi> e \<subseteq> S"
+    using \<open>open S\<close> \<open>\<xi> \<in> S\<close> open_contains_cball_eq by blast
+  define T where "T = cball \<xi> e \<inter> U"
+  have contf: "continuous_on (closure T) f"
+    by (metis T_def closed_cball closure_minimal e holf holomorphic_on_imp_continuous_on
+              holomorphic_on_subset inf.cobounded1)
+  have fT0 [simp]: "\<And>x. x \<in> T \<Longrightarrow> f x = 0"
+    by (simp add: T_def)
+  have "\<And>r. \<lbrakk>\<forall>e>0. \<exists>x'\<in>U. x' \<noteq> \<xi> \<and> dist x' \<xi> < e; 0 < r\<rbrakk> \<Longrightarrow> \<exists>x'\<in>cball \<xi> e \<inter> U. x' \<noteq> \<xi> \<and> dist x' \<xi> < r"
+    by (metis \<open>0 < e\<close> IntI dist_commute less_eq_real_def mem_cball min_less_iff_conj)
+  then have "\<xi> islimpt T" using \<open>\<xi> islimpt U\<close>
+    by (auto simp: T_def islimpt_approachable)
+  then have "\<xi> \<in> closure T"
+    by (simp add: closure_def)
+  then have "f \<xi> = 0"
+    by (auto simp: continuous_constant_on_closure [OF contf])
+  show ?thesis
+    apply (rule ccontr)
+    apply (rule isolated_zeros [OF holf \<open>open S\<close> \<open>connected S\<close> \<open>\<xi> \<in> S\<close> \<open>f \<xi> = 0\<close> \<open>w \<in> S\<close>], assumption)
+    by (metis open_ball \<open>\<xi> islimpt T\<close> centre_in_ball fT0 insertE insert_Diff islimptE)
+qed
+
+corollary analytic_continuation_open:
+  assumes "open s" and "open s'" and "s \<noteq> {}" and "connected s'"
+      and "s \<subseteq> s'"
+  assumes "f holomorphic_on s'" and "g holomorphic_on s'"
+      and "\<And>z. z \<in> s \<Longrightarrow> f z = g z"
+  assumes "z \<in> s'"
+  shows   "f z = g z"
+proof -
+  from \<open>s \<noteq> {}\<close> obtain \<xi> where "\<xi> \<in> s" by auto
+  with \<open>open s\<close> have \<xi>: "\<xi> islimpt s"
+    by (intro interior_limit_point) (auto simp: interior_open)
+  have "f z - g z = 0"
+    by (rule analytic_continuation[of "\<lambda>z. f z - g z" s' s \<xi>])
+       (insert assms \<open>\<xi> \<in> s\<close> \<xi>, auto intro: holomorphic_intros)
+  thus ?thesis by simp
+qed
+
+subsection\<open>Open mapping theorem\<close>
+
+lemma holomorphic_contract_to_zero:
+  assumes contf: "continuous_on (cball \<xi> r) f"
+      and holf: "f holomorphic_on ball \<xi> r"
+      and "0 < r"
+      and norm_less: "\<And>z. norm(\<xi> - z) = r \<Longrightarrow> norm(f \<xi>) < norm(f z)"
+  obtains z where "z \<in> ball \<xi> r" "f z = 0"
+proof -
+  { assume fnz: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w \<noteq> 0"
+    then have "0 < norm (f \<xi>)"
+      by (simp add: \<open>0 < r\<close>)
+    have fnz': "\<And>w. w \<in> cball \<xi> r \<Longrightarrow> f w \<noteq> 0"
+      by (metis norm_less dist_norm fnz less_eq_real_def mem_ball mem_cball norm_not_less_zero norm_zero)
+    have "frontier(cball \<xi> r) \<noteq> {}"
+      using \<open>0 < r\<close> by simp
+    define g where [abs_def]: "g z = inverse (f z)" for z
+    have contg: "continuous_on (cball \<xi> r) g"
+      unfolding g_def using contf continuous_on_inverse fnz' by blast
+    have holg: "g holomorphic_on ball \<xi> r"
+      unfolding g_def using fnz holf holomorphic_on_inverse by blast
+    have "frontier (cball \<xi> r) \<subseteq> cball \<xi> r"
+      by (simp add: subset_iff)
+    then have contf': "continuous_on (frontier (cball \<xi> r)) f"
+          and contg': "continuous_on (frontier (cball \<xi> r)) g"
+      by (blast intro: contf contg continuous_on_subset)+
+    have froc: "frontier(cball \<xi> r) \<noteq> {}"
+      using \<open>0 < r\<close> by simp
+    moreover have "continuous_on (frontier (cball \<xi> r)) (norm o f)"
+      using contf' continuous_on_compose continuous_on_norm_id by blast
+    ultimately obtain w where w: "w \<in> frontier(cball \<xi> r)"
+                          and now: "\<And>x. x \<in> frontier(cball \<xi> r) \<Longrightarrow> norm (f w) \<le> norm (f x)"
+      apply (rule bexE [OF continuous_attains_inf [OF compact_frontier [OF compact_cball]]])
+      apply simp
+      done
+    then have fw: "0 < norm (f w)"
+      by (simp add: fnz')
+    have "continuous_on (frontier (cball \<xi> r)) (norm o g)"
+      using contg' continuous_on_compose continuous_on_norm_id by blast
+    then obtain v where v: "v \<in> frontier(cball \<xi> r)"
+               and nov: "\<And>x. x \<in> frontier(cball \<xi> r) \<Longrightarrow> norm (g v) \<ge> norm (g x)"
+      apply (rule bexE [OF continuous_attains_sup [OF compact_frontier [OF compact_cball] froc]])
+      apply simp
+      done
+    then have fv: "0 < norm (f v)"
+      by (simp add: fnz')
+    have "norm ((deriv ^^ 0) g \<xi>) \<le> fact 0 * norm (g v) / r ^ 0"
+      by (rule Cauchy_inequality [OF holg contg \<open>0 < r\<close>]) (simp add: dist_norm nov)
+    then have "cmod (g \<xi>) \<le> norm (g v)"
+      by simp
+    with w have wr: "norm (\<xi> - w) = r" and nfw: "norm (f w) \<le> norm (f \<xi>)"
+      apply (simp_all add: dist_norm)
+      by (metis \<open>0 < cmod (f \<xi>)\<close> g_def less_imp_inverse_less norm_inverse not_le now order_trans v)
+    with fw have False
+      using norm_less by force
+  }
+  with that show ?thesis by blast
+qed
+
+theorem open_mapping_thm:
+  assumes holf: "f holomorphic_on S"
+      and S: "open S" and "connected S"
+      and "open U" and "U \<subseteq> S"
+      and fne: "\<not> f constant_on S"
+    shows "open (f ` U)"
+proof -
+  have *: "open (f ` U)"
+          if "U \<noteq> {}" and U: "open U" "connected U" and "f holomorphic_on U" and fneU: "\<And>x. \<exists>y \<in> U. f y \<noteq> x"
+          for U
+  proof (clarsimp simp: open_contains_ball)
+    fix \<xi> assume \<xi>: "\<xi> \<in> U"
+    show "\<exists>e>0. ball (f \<xi>) e \<subseteq> f ` U"
+    proof -
+      have hol: "(\<lambda>z. f z - f \<xi>) holomorphic_on U"
+        by (rule holomorphic_intros that)+
+      obtain s where "0 < s" and sbU: "ball \<xi> s \<subseteq> U"
+                 and sne: "\<And>z. z \<in> ball \<xi> s - {\<xi>} \<Longrightarrow> (\<lambda>z. f z - f \<xi>) z \<noteq> 0"
+        using isolated_zeros [OF hol U \<xi>]  by (metis fneU right_minus_eq)
+      obtain r where "0 < r" and r: "cball \<xi> r \<subseteq> ball \<xi> s"
+        apply (rule_tac r="s/2" in that)
+        using \<open>0 < s\<close> by auto
+      have "cball \<xi> r \<subseteq> U"
+        using sbU r by blast
+      then have frsbU: "frontier (cball \<xi> r) \<subseteq> U"
+        using Diff_subset frontier_def order_trans by fastforce
+      then have cof: "compact (frontier(cball \<xi> r))"
+        by blast
+      have frne: "frontier (cball \<xi> r) \<noteq> {}"
+        using \<open>0 < r\<close> by auto
+      have contfr: "continuous_on (frontier (cball \<xi> r)) (\<lambda>z. norm (f z - f \<xi>))"
+        by (metis continuous_on_norm continuous_on_subset frsbU hol holomorphic_on_imp_continuous_on)
+      obtain w where "norm (\<xi> - w) = r"
+                 and w: "(\<And>z. norm (\<xi> - z) = r \<Longrightarrow> norm (f w - f \<xi>) \<le> norm(f z - f \<xi>))"
+        apply (rule bexE [OF continuous_attains_inf [OF cof frne contfr]])
+        apply (simp add: dist_norm)
+        done
+      moreover define \<epsilon> where "\<epsilon> \<equiv> norm (f w - f \<xi>) / 3"
+      ultimately have "0 < \<epsilon>"
+        using \<open>0 < r\<close> dist_complex_def r sne by auto
+      have "ball (f \<xi>) \<epsilon> \<subseteq> f ` U"
+      proof
+        fix \<gamma>
+        assume \<gamma>: "\<gamma> \<in> ball (f \<xi>) \<epsilon>"
+        have *: "cmod (\<gamma> - f \<xi>) < cmod (\<gamma> - f z)" if "cmod (\<xi> - z) = r" for z
+        proof -
+          have lt: "cmod (f w - f \<xi>) / 3 < cmod (\<gamma> - f z)"
+            using w [OF that] \<gamma>
+            using dist_triangle2 [of "f \<xi>" "\<gamma>"  "f z"] dist_triangle2 [of "f \<xi>" "f z" \<gamma>]
+            by (simp add: \<epsilon>_def dist_norm norm_minus_commute)
+          show ?thesis
+            by (metis \<epsilon>_def dist_commute dist_norm less_trans lt mem_ball \<gamma>)
+       qed
+       have "continuous_on (cball \<xi> r) (\<lambda>z. \<gamma> - f z)"
+          apply (rule continuous_intros)+
+          using \<open>cball \<xi> r \<subseteq> U\<close> \<open>f holomorphic_on U\<close>
+          apply (blast intro: continuous_on_subset holomorphic_on_imp_continuous_on)
+          done
+        moreover have "(\<lambda>z. \<gamma> - f z) holomorphic_on ball \<xi> r"
+          apply (rule holomorphic_intros)+
+          apply (metis \<open>cball \<xi> r \<subseteq> U\<close> \<open>f holomorphic_on U\<close> holomorphic_on_subset interior_cball interior_subset)
+          done
+        ultimately obtain z where "z \<in> ball \<xi> r" "\<gamma> - f z = 0"
+          apply (rule holomorphic_contract_to_zero)
+          apply (blast intro!: \<open>0 < r\<close> *)+
+          done
+        then show "\<gamma> \<in> f ` U"
+          using \<open>cball \<xi> r \<subseteq> U\<close> by fastforce
+      qed
+      then show ?thesis using  \<open>0 < \<epsilon>\<close> by blast
+    qed
+  qed
+  have "open (f ` X)" if "X \<in> components U" for X
+  proof -
+    have holfU: "f holomorphic_on U"
+      using \<open>U \<subseteq> S\<close> holf holomorphic_on_subset by blast
+    have "X \<noteq> {}"
+      using that by (simp add: in_components_nonempty)
+    moreover have "open X"
+      using that \<open>open U\<close> open_components by auto
+    moreover have "connected X"
+      using that in_components_maximal by blast
+    moreover have "f holomorphic_on X"
+      by (meson that holfU holomorphic_on_subset in_components_maximal)
+    moreover have "\<exists>y\<in>X. f y \<noteq> x" for x
+    proof (rule ccontr)
+      assume not: "\<not> (\<exists>y\<in>X. f y \<noteq> x)"
+      have "X \<subseteq> S"
+        using \<open>U \<subseteq> S\<close> in_components_subset that by blast
+      obtain w where w: "w \<in> X" using \<open>X \<noteq> {}\<close> by blast
+      have wis: "w islimpt X"
+        using w \<open>open X\<close> interior_eq by auto
+      have hol: "(\<lambda>z. f z - x) holomorphic_on S"
+        by (simp add: holf holomorphic_on_diff)
+      with fne [unfolded constant_on_def]
+           analytic_continuation[OF hol S \<open>connected S\<close> \<open>X \<subseteq> S\<close> _ wis] not \<open>X \<subseteq> S\<close> w
+      show False by auto
+    qed
+    ultimately show ?thesis
+      by (rule *)
+  qed
+  then have "open (f ` \<Union>(components U))"
+    by (metis (no_types, lifting) imageE image_Union open_Union)
+  then show ?thesis
+    by force
+qed
+
+text\<open>No need for \<^term>\<open>S\<close> to be connected. But the nonconstant condition is stronger.\<close>
+corollary\<^marker>\<open>tag unimportant\<close> open_mapping_thm2:
+  assumes holf: "f holomorphic_on S"
+      and S: "open S"
+      and "open U" "U \<subseteq> S"
+      and fnc: "\<And>X. \<lbrakk>open X; X \<subseteq> S; X \<noteq> {}\<rbrakk> \<Longrightarrow> \<not> f constant_on X"
+    shows "open (f ` U)"
+proof -
+  have "S = \<Union>(components S)" by simp
+  with \<open>U \<subseteq> S\<close> have "U = (\<Union>C \<in> components S. C \<inter> U)" by auto
+  then have "f ` U = (\<Union>C \<in> components S. f ` (C \<inter> U))"
+    using image_UN by fastforce
+  moreover
+  { fix C assume "C \<in> components S"
+    with S \<open>C \<in> components S\<close> open_components in_components_connected
+    have C: "open C" "connected C" by auto
+    have "C \<subseteq> S"
+      by (metis \<open>C \<in> components S\<close> in_components_maximal)
+    have nf: "\<not> f constant_on C"
+      apply (rule fnc)
+      using C \<open>C \<subseteq> S\<close> \<open>C \<in> components S\<close> in_components_nonempty by auto
+    have "f holomorphic_on C"
+      by (metis holf holomorphic_on_subset \<open>C \<subseteq> S\<close>)
+    then have "open (f ` (C \<inter> U))"
+      apply (rule open_mapping_thm [OF _ C _ _ nf])
+      apply (simp add: C \<open>open U\<close> open_Int, blast)
+      done
+  } ultimately show ?thesis
+    by force
+qed
+
+corollary\<^marker>\<open>tag unimportant\<close> open_mapping_thm3:
+  assumes holf: "f holomorphic_on S"
+      and "open S" and injf: "inj_on f S"
+    shows  "open (f ` S)"
+apply (rule open_mapping_thm2 [OF holf])
+using assms
+apply (simp_all add:)
+using injective_not_constant subset_inj_on by blast
+
+subsection\<open>Maximum modulus principle\<close>
+
+text\<open>If \<^term>\<open>f\<close> is holomorphic, then its norm (modulus) cannot exhibit a true local maximum that is
+   properly within the domain of \<^term>\<open>f\<close>.\<close>
+
+proposition maximum_modulus_principle:
+  assumes holf: "f holomorphic_on S"
+      and S: "open S" and "connected S"
+      and "open U" and "U \<subseteq> S" and "\<xi> \<in> U"
+      and no: "\<And>z. z \<in> U \<Longrightarrow> norm(f z) \<le> norm(f \<xi>)"
+    shows "f constant_on S"
+proof (rule ccontr)
+  assume "\<not> f constant_on S"
+  then have "open (f ` U)"
+    using open_mapping_thm assms by blast
+  moreover have "\<not> open (f ` U)"
+  proof -
+    have "\<exists>t. cmod (f \<xi> - t) < e \<and> t \<notin> f ` U" if "0 < e" for e
+      apply (rule_tac x="if 0 < Re(f \<xi>) then f \<xi> + (e/2) else f \<xi> - (e/2)" in exI)
+      using that
+      apply (simp add: dist_norm)
+      apply (fastforce simp: cmod_Re_le_iff dest!: no dest: sym)
+      done
+    then show ?thesis
+      unfolding open_contains_ball by (metis \<open>\<xi> \<in> U\<close> contra_subsetD dist_norm imageI mem_ball)
+  qed
+  ultimately show False
+    by blast
+qed
+
+proposition maximum_modulus_frontier:
+  assumes holf: "f holomorphic_on (interior S)"
+      and contf: "continuous_on (closure S) f"
+      and bos: "bounded S"
+      and leB: "\<And>z. z \<in> frontier S \<Longrightarrow> norm(f z) \<le> B"
+      and "\<xi> \<in> S"
+    shows "norm(f \<xi>) \<le> B"
+proof -
+  have "compact (closure S)" using bos
+    by (simp add: bounded_closure compact_eq_bounded_closed)
+  moreover have "continuous_on (closure S) (cmod \<circ> f)"
+    using contf continuous_on_compose continuous_on_norm_id by blast
+  ultimately obtain z where zin: "z \<in> closure S" and z: "\<And>y. y \<in> closure S \<Longrightarrow> (cmod \<circ> f) y \<le> (cmod \<circ> f) z"
+    using continuous_attains_sup [of "closure S" "norm o f"] \<open>\<xi> \<in> S\<close> by auto
+  then consider "z \<in> frontier S" | "z \<in> interior S" using frontier_def by auto
+  then have "norm(f z) \<le> B"
+  proof cases
+    case 1 then show ?thesis using leB by blast
+  next
+    case 2
+    have zin: "z \<in> connected_component_set (interior S) z"
+      by (simp add: 2)
+    have "f constant_on (connected_component_set (interior S) z)"
+      apply (rule maximum_modulus_principle [OF _ _ _ _ _ zin])
+      apply (metis connected_component_subset holf holomorphic_on_subset)
+      apply (simp_all add: open_connected_component)
+      by (metis closure_subset comp_eq_dest_lhs  interior_subset subsetCE z connected_component_in)
+    then obtain c where c: "\<And>w. w \<in> connected_component_set (interior S) z \<Longrightarrow> f w = c"
+      by (auto simp: constant_on_def)
+    have "f ` closure(connected_component_set (interior S) z) \<subseteq> {c}"
+      apply (rule image_closure_subset)
+      apply (meson closure_mono connected_component_subset contf continuous_on_subset interior_subset)
+      using c
+      apply auto
+      done
+    then have cc: "\<And>w. w \<in> closure(connected_component_set (interior S) z) \<Longrightarrow> f w = c" by blast
+    have "frontier(connected_component_set (interior S) z) \<noteq> {}"
+      apply (simp add: frontier_eq_empty)
+      by (metis "2" bos bounded_interior connected_component_eq_UNIV connected_component_refl not_bounded_UNIV)
+    then obtain w where w: "w \<in> frontier(connected_component_set (interior S) z)"
+       by auto
+    then have "norm (f z) = norm (f w)"  by (simp add: "2" c cc frontier_def)
+    also have "... \<le> B"
+      apply (rule leB)
+      using w
+using frontier_interior_subset frontier_of_connected_component_subset by blast
+    finally show ?thesis .
+  qed
+  then show ?thesis
+    using z \<open>\<xi> \<in> S\<close> closure_subset by fastforce
+qed
+
+corollary\<^marker>\<open>tag unimportant\<close> maximum_real_frontier:
+  assumes holf: "f holomorphic_on (interior S)"
+      and contf: "continuous_on (closure S) f"
+      and bos: "bounded S"
+      and leB: "\<And>z. z \<in> frontier S \<Longrightarrow> Re(f z) \<le> B"
+      and "\<xi> \<in> S"
+    shows "Re(f \<xi>) \<le> B"
+using maximum_modulus_frontier [of "exp o f" S "exp B"]
+      Transcendental.continuous_on_exp holomorphic_on_compose holomorphic_on_exp assms
+by auto
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Factoring out a zero according to its order\<close>
+
+lemma holomorphic_factor_order_of_zero:
+  assumes holf: "f holomorphic_on S"
+      and os: "open S"
+      and "\<xi> \<in> S" "0 < n"
+      and dnz: "(deriv ^^ n) f \<xi> \<noteq> 0"
+      and dfz: "\<And>i. \<lbrakk>0 < i; i < n\<rbrakk> \<Longrightarrow> (deriv ^^ i) f \<xi> = 0"
+   obtains g r where "0 < r"
+                "g holomorphic_on ball \<xi> r"
+                "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w - f \<xi> = (w - \<xi>)^n * g w"
+                "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
+proof -
+  obtain r where "r>0" and r: "ball \<xi> r \<subseteq> S" using assms by (blast elim!: openE)
+  then have holfb: "f holomorphic_on ball \<xi> r"
+    using holf holomorphic_on_subset by blast
+  define g where "g w = suminf (\<lambda>i. (deriv ^^ (i + n)) f \<xi> / (fact(i + n)) * (w - \<xi>)^i)" for w
+  have sumsg: "(\<lambda>i. (deriv ^^ (i + n)) f \<xi> / (fact(i + n)) * (w - \<xi>)^i) sums g w"
+   and feq: "f w - f \<xi> = (w - \<xi>)^n * g w"
+       if w: "w \<in> ball \<xi> r" for w
+  proof -
+    define powf where "powf = (\<lambda>i. (deriv ^^ i) f \<xi>/(fact i) * (w - \<xi>)^i)"
+    have sing: "{..<n} - {i. powf i = 0} = (if f \<xi> = 0 then {} else {0})"
+      unfolding powf_def using \<open>0 < n\<close> dfz by (auto simp: dfz; metis funpow_0 not_gr0)
+    have "powf sums f w"
+      unfolding powf_def by (rule holomorphic_power_series [OF holfb w])
+    moreover have "(\<Sum>i<n. powf i) = f \<xi>"
+      apply (subst Groups_Big.comm_monoid_add_class.sum.setdiff_irrelevant [symmetric])
+      apply simp
+      apply (simp only: dfz sing)
+      apply (simp add: powf_def)
+      done
+    ultimately have fsums: "(\<lambda>i. powf (i+n)) sums (f w - f \<xi>)"
+      using w sums_iff_shift' by metis
+    then have *: "summable (\<lambda>i. (w - \<xi>) ^ n * ((deriv ^^ (i + n)) f \<xi> * (w - \<xi>) ^ i / fact (i + n)))"
+      unfolding powf_def using sums_summable
+      by (auto simp: power_add mult_ac)
+    have "summable (\<lambda>i. (deriv ^^ (i + n)) f \<xi> * (w - \<xi>) ^ i / fact (i + n))"
+    proof (cases "w=\<xi>")
+      case False then show ?thesis
+        using summable_mult [OF *, of "1 / (w - \<xi>) ^ n"] by simp
+    next
+      case True then show ?thesis
+        by (auto simp: Power.semiring_1_class.power_0_left intro!: summable_finite [of "{0}"]
+                 split: if_split_asm)
+    qed
+    then show sumsg: "(\<lambda>i. (deriv ^^ (i + n)) f \<xi> / (fact(i + n)) * (w - \<xi>)^i) sums g w"
+      by (simp add: summable_sums_iff g_def)
+    show "f w - f \<xi> = (w - \<xi>)^n * g w"
+      apply (rule sums_unique2)
+      apply (rule fsums [unfolded powf_def])
+      using sums_mult [OF sumsg, of "(w - \<xi>) ^ n"]
+      by (auto simp: power_add mult_ac)
+  qed
+  then have holg: "g holomorphic_on ball \<xi> r"
+    by (meson sumsg power_series_holomorphic)
+  then have contg: "continuous_on (ball \<xi> r) g"
+    by (blast intro: holomorphic_on_imp_continuous_on)
+  have "g \<xi> \<noteq> 0"
+    using dnz unfolding g_def
+    by (subst suminf_finite [of "{0}"]) auto
+  obtain d where "0 < d" and d: "\<And>w. w \<in> ball \<xi> d \<Longrightarrow> g w \<noteq> 0"
+    apply (rule exE [OF continuous_on_avoid [OF contg _ \<open>g \<xi> \<noteq> 0\<close>]])
+    using \<open>0 < r\<close>
+    apply force
+    by (metis \<open>0 < r\<close> less_trans mem_ball not_less_iff_gr_or_eq)
+  show ?thesis
+    apply (rule that [where g=g and r ="min r d"])
+    using \<open>0 < r\<close> \<open>0 < d\<close> holg
+    apply (auto simp: feq holomorphic_on_subset subset_ball d)
+    done
+qed
+
+
+lemma holomorphic_factor_order_of_zero_strong:
+  assumes holf: "f holomorphic_on S" "open S"  "\<xi> \<in> S" "0 < n"
+      and "(deriv ^^ n) f \<xi> \<noteq> 0"
+      and "\<And>i. \<lbrakk>0 < i; i < n\<rbrakk> \<Longrightarrow> (deriv ^^ i) f \<xi> = 0"
+   obtains g r where "0 < r"
+                "g holomorphic_on ball \<xi> r"
+                "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w - f \<xi> = ((w - \<xi>) * g w) ^ n"
+                "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
+proof -
+  obtain g r where "0 < r"
+               and holg: "g holomorphic_on ball \<xi> r"
+               and feq: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w - f \<xi> = (w - \<xi>)^n * g w"
+               and gne: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
+    by (auto intro: holomorphic_factor_order_of_zero [OF assms])
+  have con: "continuous_on (ball \<xi> r) (\<lambda>z. deriv g z / g z)"
+    by (rule continuous_intros) (auto simp: gne holg holomorphic_deriv holomorphic_on_imp_continuous_on)
+  have cd: "\<And>x. dist \<xi> x < r \<Longrightarrow> (\<lambda>z. deriv g z / g z) field_differentiable at x"
+    apply (rule derivative_intros)+
+    using holg mem_ball apply (blast intro: holomorphic_deriv holomorphic_on_imp_differentiable_at)
+    apply (metis open_ball at_within_open holg holomorphic_on_def mem_ball)
+    using gne mem_ball by blast
+  obtain h where h: "\<And>x. x \<in> ball \<xi> r \<Longrightarrow> (h has_field_derivative deriv g x / g x) (at x)"
+    apply (rule exE [OF holomorphic_convex_primitive [of "ball \<xi> r" "{}" "\<lambda>z. deriv g z / g z"]])
+    apply (auto simp: con cd)
+    apply (metis open_ball at_within_open mem_ball)
+    done
+  then have "continuous_on (ball \<xi> r) h"
+    by (metis open_ball holomorphic_on_imp_continuous_on holomorphic_on_open)
+  then have con: "continuous_on (ball \<xi> r) (\<lambda>x. exp (h x) / g x)"
+    by (auto intro!: continuous_intros simp add: holg holomorphic_on_imp_continuous_on gne)
+  have 0: "dist \<xi> x < r \<Longrightarrow> ((\<lambda>x. exp (h x) / g x) has_field_derivative 0) (at x)" for x
+    apply (rule h derivative_eq_intros | simp)+
+    apply (rule DERIV_deriv_iff_field_differentiable [THEN iffD2])
+    using holg apply (auto simp: holomorphic_on_imp_differentiable_at gne h)
+    done
+  obtain c where c: "\<And>x. x \<in> ball \<xi> r \<Longrightarrow> exp (h x) / g x = c"
+    by (rule DERIV_zero_connected_constant [of "ball \<xi> r" "{}" "\<lambda>x. exp(h x) / g x"]) (auto simp: con 0)
+  have hol: "(\<lambda>z. exp ((Ln (inverse c) + h z) / of_nat n)) holomorphic_on ball \<xi> r"
+    apply (rule holomorphic_on_compose [unfolded o_def, where g = exp])
+    apply (rule holomorphic_intros)+
+    using h holomorphic_on_open apply blast
+    apply (rule holomorphic_intros)+
+    using \<open>0 < n\<close> apply simp
+    apply (rule holomorphic_intros)+
+    done
+  show ?thesis
+    apply (rule that [where g="\<lambda>z. exp((Ln(inverse c) + h z)/n)" and r =r])
+    using \<open>0 < r\<close> \<open>0 < n\<close>
+    apply (auto simp: feq power_mult_distrib exp_divide_power_eq c [symmetric])
+    apply (rule hol)
+    apply (simp add: Transcendental.exp_add gne)
+    done
+qed
+
+
+lemma
+  fixes k :: "'a::wellorder"
+  assumes a_def: "a == LEAST x. P x" and P: "P k"
+  shows def_LeastI: "P a" and def_Least_le: "a \<le> k"
+unfolding a_def
+by (rule LeastI Least_le; rule P)+
+
+lemma holomorphic_factor_zero_nonconstant:
+  assumes holf: "f holomorphic_on S" and S: "open S" "connected S"
+      and "\<xi> \<in> S" "f \<xi> = 0"
+      and nonconst: "\<not> f constant_on S"
+   obtains g r n
+      where "0 < n"  "0 < r"  "ball \<xi> r \<subseteq> S"
+            "g holomorphic_on ball \<xi> r"
+            "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w = (w - \<xi>)^n * g w"
+            "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
+proof (cases "\<forall>n>0. (deriv ^^ n) f \<xi> = 0")
+  case True then show ?thesis
+    using holomorphic_fun_eq_const_on_connected [OF holf S _ \<open>\<xi> \<in> S\<close>] nonconst by (simp add: constant_on_def)
+next
+  case False
+  then obtain n0 where "n0 > 0" and n0: "(deriv ^^ n0) f \<xi> \<noteq> 0" by blast
+  obtain r0 where "r0 > 0" "ball \<xi> r0 \<subseteq> S" using S openE \<open>\<xi> \<in> S\<close> by auto
+  define n where "n \<equiv> LEAST n. (deriv ^^ n) f \<xi> \<noteq> 0"
+  have n_ne: "(deriv ^^ n) f \<xi> \<noteq> 0"
+    by (rule def_LeastI [OF n_def]) (rule n0)
+  then have "0 < n" using \<open>f \<xi> = 0\<close>
+    using funpow_0 by fastforce
+  have n_min: "\<And>k. k < n \<Longrightarrow> (deriv ^^ k) f \<xi> = 0"
+    using def_Least_le [OF n_def] not_le by blast
+  then obtain g r1
+    where  "0 < r1" "g holomorphic_on ball \<xi> r1"
+           "\<And>w. w \<in> ball \<xi> r1 \<Longrightarrow> f w = (w - \<xi>) ^ n * g w"
+           "\<And>w. w \<in> ball \<xi> r1 \<Longrightarrow> g w \<noteq> 0"
+    by (auto intro: holomorphic_factor_order_of_zero [OF holf \<open>open S\<close> \<open>\<xi> \<in> S\<close> \<open>n > 0\<close> n_ne] simp: \<open>f \<xi> = 0\<close>)
+  then show ?thesis
+    apply (rule_tac g=g and r="min r0 r1" and n=n in that)
+    using \<open>0 < n\<close> \<open>0 < r0\<close> \<open>0 < r1\<close> \<open>ball \<xi> r0 \<subseteq> S\<close>
+    apply (auto simp: subset_ball intro: holomorphic_on_subset)
+    done
+qed
+
+
+lemma holomorphic_lower_bound_difference:
+  assumes holf: "f holomorphic_on S" and S: "open S" "connected S"
+      and "\<xi> \<in> S" and "\<phi> \<in> S"
+      and fne: "f \<phi> \<noteq> f \<xi>"
+   obtains k n r
+      where "0 < k"  "0 < r"
+            "ball \<xi> r \<subseteq> S"
+            "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> k * norm(w - \<xi>)^n \<le> norm(f w - f \<xi>)"
+proof -
+  define n where "n = (LEAST n. 0 < n \<and> (deriv ^^ n) f \<xi> \<noteq> 0)"
+  obtain n0 where "0 < n0" and n0: "(deriv ^^ n0) f \<xi> \<noteq> 0"
+    using fne holomorphic_fun_eq_const_on_connected [OF holf S] \<open>\<xi> \<in> S\<close> \<open>\<phi> \<in> S\<close> by blast
+  then have "0 < n" and n_ne: "(deriv ^^ n) f \<xi> \<noteq> 0"
+    unfolding n_def by (metis (mono_tags, lifting) LeastI)+
+  have n_min: "\<And>k. \<lbrakk>0 < k; k < n\<rbrakk> \<Longrightarrow> (deriv ^^ k) f \<xi> = 0"
+    unfolding n_def by (blast dest: not_less_Least)
+  then obtain g r
+    where "0 < r" and holg: "g holomorphic_on ball \<xi> r"
+      and fne: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> f w - f \<xi> = (w - \<xi>) ^ n * g w"
+      and gnz: "\<And>w. w \<in> ball \<xi> r \<Longrightarrow> g w \<noteq> 0"
+      by (auto intro: holomorphic_factor_order_of_zero  [OF holf \<open>open S\<close> \<open>\<xi> \<in> S\<close> \<open>n > 0\<close> n_ne])
+  obtain e where "e>0" and e: "ball \<xi> e \<subseteq> S" using assms by (blast elim!: openE)
+  then have holfb: "f holomorphic_on ball \<xi> e"
+    using holf holomorphic_on_subset by blast
+  define d where "d = (min e r) / 2"
+  have "0 < d" using \<open>0 < r\<close> \<open>0 < e\<close> by (simp add: d_def)
+  have "d < r"
+    using \<open>0 < r\<close> by (auto simp: d_def)
+  then have cbb: "cball \<xi> d \<subseteq> ball \<xi> r"
+    by (auto simp: cball_subset_ball_iff)
+  then have "g holomorphic_on cball \<xi> d"
+    by (rule holomorphic_on_subset [OF holg])
+  then have "closed (g ` cball \<xi> d)"
+    by (simp add: compact_imp_closed compact_continuous_image holomorphic_on_imp_continuous_on)
+  moreover have "g ` cball \<xi> d \<noteq> {}"
+    using \<open>0 < d\<close> by auto
+  ultimately obtain x where x: "x \<in> g ` cball \<xi> d" and "\<And>y. y \<in> g ` cball \<xi> d \<Longrightarrow> dist 0 x \<le> dist 0 y"
+    by (rule distance_attains_inf) blast
+  then have leg: "\<And>w. w \<in> cball \<xi> d \<Longrightarrow> norm x \<le> norm (g w)"
+    by auto
+  have "ball \<xi> d \<subseteq> cball \<xi> d" by auto
+  also have "... \<subseteq> ball \<xi> e" using \<open>0 < d\<close> d_def by auto
+  also have "... \<subseteq> S" by (rule e)
+  finally have dS: "ball \<xi> d \<subseteq> S" .
+  moreover have "x \<noteq> 0" using gnz x \<open>d < r\<close> by auto
+  ultimately show ?thesis
+    apply (rule_tac k="norm x" and n=n and r=d in that)
+    using \<open>d < r\<close> leg
+    apply (auto simp: \<open>0 < d\<close> fne norm_mult norm_power algebra_simps mult_right_mono)
+    done
+qed
+
+lemma
+  assumes holf: "f holomorphic_on (S - {\<xi>})" and \<xi>: "\<xi> \<in> interior S"
+    shows holomorphic_on_extend_lim:
+          "(\<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S - {\<xi>}. g z = f z)) \<longleftrightarrow>
+           ((\<lambda>z. (z - \<xi>) * f z) \<longlongrightarrow> 0) (at \<xi>)"
+          (is "?P = ?Q")
+     and holomorphic_on_extend_bounded:
+          "(\<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S - {\<xi>}. g z = f z)) \<longleftrightarrow>
+           (\<exists>B. eventually (\<lambda>z. norm(f z) \<le> B) (at \<xi>))"
+          (is "?P = ?R")
+proof -
+  obtain \<delta> where "0 < \<delta>" and \<delta>: "ball \<xi> \<delta> \<subseteq> S"
+    using \<xi> mem_interior by blast
+  have "?R" if holg: "g holomorphic_on S" and gf: "\<And>z. z \<in> S - {\<xi>} \<Longrightarrow> g z = f z" for g
+  proof -
+    have *: "\<forall>\<^sub>F z in at \<xi>. dist (g z) (g \<xi>) < 1 \<longrightarrow> cmod (f z) \<le> cmod (g \<xi>) + 1"
+      apply (simp add: eventually_at)
+      apply (rule_tac x="\<delta>" in exI)
+      using \<delta> \<open>0 < \<delta>\<close>
+      apply (clarsimp simp:)
+      apply (drule_tac c=x in subsetD)
+      apply (simp add: dist_commute)
+      by (metis DiffI add.commute diff_le_eq dist_norm gf le_less_trans less_eq_real_def norm_triangle_ineq2 singletonD)
+    have "continuous_on (interior S) g"
+      by (meson continuous_on_subset holg holomorphic_on_imp_continuous_on interior_subset)
+    then have "\<And>x. x \<in> interior S \<Longrightarrow> (g \<longlongrightarrow> g x) (at x)"
+      using continuous_on_interior continuous_within holg holomorphic_on_imp_continuous_on by blast
+    then have "(g \<longlongrightarrow> g \<xi>) (at \<xi>)"
+      by (simp add: \<xi>)
+    then show ?thesis
+      apply (rule_tac x="norm(g \<xi>) + 1" in exI)
+      apply (rule eventually_mp [OF * tendstoD [where e=1]], auto)
+      done
+  qed
+  moreover have "?Q" if "\<forall>\<^sub>F z in at \<xi>. cmod (f z) \<le> B" for B
+    by (rule lim_null_mult_right_bounded [OF _ that]) (simp add: LIM_zero)
+  moreover have "?P" if "(\<lambda>z. (z - \<xi>) * f z) \<midarrow>\<xi>\<rightarrow> 0"
+  proof -
+    define h where [abs_def]: "h z = (z - \<xi>)^2 * f z" for z
+    have h0: "(h has_field_derivative 0) (at \<xi>)"
+      apply (simp add: h_def has_field_derivative_iff)
+      apply (rule Lim_transform_within [OF that, of 1])
+      apply (auto simp: field_split_simps power2_eq_square)
+      done
+    have holh: "h holomorphic_on S"
+    proof (simp add: holomorphic_on_def, clarify)
+      fix z assume "z \<in> S"
+      show "h field_differentiable at z within S"
+      proof (cases "z = \<xi>")
+        case True then show ?thesis
+          using field_differentiable_at_within field_differentiable_def h0 by blast
+      next
+        case False
+        then have "f field_differentiable at z within S"
+          using holomorphic_onD [OF holf, of z] \<open>z \<in> S\<close>
+          unfolding field_differentiable_def has_field_derivative_iff
+          by (force intro: exI [where x="dist \<xi> z"] elim: Lim_transform_within_set [unfolded eventually_at])
+        then show ?thesis
+          by (simp add: h_def power2_eq_square derivative_intros)
+      qed
+    qed
+    define g where [abs_def]: "g z = (if z = \<xi> then deriv h \<xi> else (h z - h \<xi>) / (z - \<xi>))" for z
+    have holg: "g holomorphic_on S"
+      unfolding g_def by (rule pole_lemma [OF holh \<xi>])
+    show ?thesis
+      apply (rule_tac x="\<lambda>z. if z = \<xi> then deriv g \<xi> else (g z - g \<xi>)/(z - \<xi>)" in exI)
+      apply (rule conjI)
+      apply (rule pole_lemma [OF holg \<xi>])
+      apply (auto simp: g_def power2_eq_square divide_simps)
+      using h0 apply (simp add: h0 DERIV_imp_deriv h_def power2_eq_square)
+      done
+  qed
+  ultimately show "?P = ?Q" and "?P = ?R"
+    by meson+
+qed
+
+lemma pole_at_infinity:
+  assumes holf: "f holomorphic_on UNIV" and lim: "((inverse o f) \<longlongrightarrow> l) at_infinity"
+  obtains a n where "\<And>z. f z = (\<Sum>i\<le>n. a i * z^i)"
+proof (cases "l = 0")
+  case False
+  with tendsto_inverse [OF lim] show ?thesis
+    apply (rule_tac a="(\<lambda>n. inverse l)" and n=0 in that)
+    apply (simp add: Liouville_weak [OF holf, of "inverse l"])
+    done
+next
+  case True
+  then have [simp]: "l = 0" .
+  show ?thesis
+  proof (cases "\<exists>r. 0 < r \<and> (\<forall>z \<in> ball 0 r - {0}. f(inverse z) \<noteq> 0)")
+    case True
+      then obtain r where "0 < r" and r: "\<And>z. z \<in> ball 0 r - {0} \<Longrightarrow> f(inverse z) \<noteq> 0"
+             by auto
+      have 1: "inverse \<circ> f \<circ> inverse holomorphic_on ball 0 r - {0}"
+        by (rule holomorphic_on_compose holomorphic_intros holomorphic_on_subset [OF holf] | force simp: r)+
+      have 2: "0 \<in> interior (ball 0 r)"
+        using \<open>0 < r\<close> by simp
+      have "\<exists>B. 0<B \<and> eventually (\<lambda>z. cmod ((inverse \<circ> f \<circ> inverse) z) \<le> B) (at 0)"
+        apply (rule exI [where x=1])
+        apply simp
+        using tendstoD [OF lim [unfolded lim_at_infinity_0] zero_less_one]
+        apply (rule eventually_mono)
+        apply (simp add: dist_norm)
+        done
+      with holomorphic_on_extend_bounded [OF 1 2]
+      obtain g where holg: "g holomorphic_on ball 0 r"
+                 and geq: "\<And>z. z \<in> ball 0 r - {0} \<Longrightarrow> g z = (inverse \<circ> f \<circ> inverse) z"
+        by meson
+      have ifi0: "(inverse \<circ> f \<circ> inverse) \<midarrow>0\<rightarrow> 0"
+        using \<open>l = 0\<close> lim lim_at_infinity_0 by blast
+      have g2g0: "g \<midarrow>0\<rightarrow> g 0"
+        using \<open>0 < r\<close> centre_in_ball continuous_at continuous_on_eq_continuous_at holg
+        by (blast intro: holomorphic_on_imp_continuous_on)
+      have g2g1: "g \<midarrow>0\<rightarrow> 0"
+        apply (rule Lim_transform_within_open [OF ifi0 open_ball [of 0 r]])
+        using \<open>0 < r\<close> by (auto simp: geq)
+      have [simp]: "g 0 = 0"
+        by (rule tendsto_unique [OF _ g2g0 g2g1]) simp
+      have "ball 0 r - {0::complex} \<noteq> {}"
+        using \<open>0 < r\<close>
+        apply (clarsimp simp: ball_def dist_norm)
+        apply (drule_tac c="of_real r/2" in subsetD, auto)
+        done
+      then obtain w::complex where "w \<noteq> 0" and w: "norm w < r" by force
+      then have "g w \<noteq> 0" by (simp add: geq r)
+      obtain B n e where "0 < B" "0 < e" "e \<le> r"
+                     and leg: "\<And>w. norm w < e \<Longrightarrow> B * cmod w ^ n \<le> cmod (g w)"
+        apply (rule holomorphic_lower_bound_difference [OF holg open_ball connected_ball, of 0 w])
+        using \<open>0 < r\<close> w \<open>g w \<noteq> 0\<close> by (auto simp: ball_subset_ball_iff)
+      have "cmod (f z) \<le> cmod z ^ n / B" if "2/e \<le> cmod z" for z
+      proof -
+        have ize: "inverse z \<in> ball 0 e - {0}" using that \<open>0 < e\<close>
+          by (auto simp: norm_divide field_split_simps algebra_simps)
+        then have [simp]: "z \<noteq> 0" and izr: "inverse z \<in> ball 0 r - {0}" using  \<open>e \<le> r\<close>
+          by auto
+        then have [simp]: "f z \<noteq> 0"
+          using r [of "inverse z"] by simp
+        have [simp]: "f z = inverse (g (inverse z))"
+          using izr geq [of "inverse z"] by simp
+        show ?thesis using ize leg [of "inverse z"]  \<open>0 < B\<close>  \<open>0 < e\<close>
+          by (simp add: field_split_simps norm_divide algebra_simps)
+      qed
+      then show ?thesis
+        apply (rule_tac a = "\<lambda>k. (deriv ^^ k) f 0 / (fact k)" and n=n in that)
+        apply (rule_tac A = "2/e" and B = "1/B" in Liouville_polynomial [OF holf], simp)
+        done
+  next
+    case False
+    then have fi0: "\<And>r. r > 0 \<Longrightarrow> \<exists>z\<in>ball 0 r - {0}. f (inverse z) = 0"
+      by simp
+    have fz0: "f z = 0" if "0 < r" and lt1: "\<And>x. x \<noteq> 0 \<Longrightarrow> cmod x < r \<Longrightarrow> inverse (cmod (f (inverse x))) < 1"
+              for z r
+    proof -
+      have f0: "(f \<longlongrightarrow> 0) at_infinity"
+      proof -
+        have DIM_complex[intro]: "2 \<le> DIM(complex)"  \<comment> \<open>should not be necessary!\<close>
+          by simp
+        have "f (inverse x) \<noteq> 0 \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> cmod x < r \<Longrightarrow> 1 < cmod (f (inverse x))" for x
+          using lt1[of x] by (auto simp: field_simps)
+        then have **: "cmod (f (inverse x)) \<le> 1 \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> cmod x < r \<Longrightarrow> f (inverse x) = 0" for x
+          by force
+        then have *: "(f \<circ> inverse) ` (ball 0 r - {0}) \<subseteq> {0} \<union> - ball 0 1"
+          by force
+        have "continuous_on (inverse ` (ball 0 r - {0})) f"
+          using continuous_on_subset holf holomorphic_on_imp_continuous_on by blast
+        then have "connected ((f \<circ> inverse) ` (ball 0 r - {0}))"
+          apply (intro connected_continuous_image continuous_intros)
+          apply (force intro: connected_punctured_ball)+
+          done
+        then have "{0} \<inter> (f \<circ> inverse) ` (ball 0 r - {0}) = {} \<or> - ball 0 1 \<inter> (f \<circ> inverse) ` (ball 0 r - {0}) = {}"
+          by (rule connected_closedD) (use * in auto)
+        then have "w \<noteq> 0 \<Longrightarrow> cmod w < r \<Longrightarrow> f (inverse w) = 0" for w
+          using fi0 **[of w] \<open>0 < r\<close>
+          apply (auto simp add: inf.commute [of "- ball 0 1"] Diff_eq [symmetric] image_subset_iff dest: less_imp_le)
+           apply fastforce
+          apply (drule bspec [of _ _ w])
+           apply (auto dest: less_imp_le)
+          done
+        then show ?thesis
+          apply (simp add: lim_at_infinity_0)
+          apply (rule tendsto_eventually)
+          apply (simp add: eventually_at)
+          apply (rule_tac x=r in exI)
+          apply (simp add: \<open>0 < r\<close> dist_norm)
+          done
+      qed
+      obtain w where "w \<in> ball 0 r - {0}" and "f (inverse w) = 0"
+        using False \<open>0 < r\<close> by blast
+      then show ?thesis
+        by (auto simp: f0 Liouville_weak [OF holf, of 0])
+    qed
+    show ?thesis
+      apply (rule that [of "\<lambda>n. 0" 0])
+      using lim [unfolded lim_at_infinity_0]
+      apply (simp add: Lim_at dist_norm norm_inverse)
+      apply (drule_tac x=1 in spec)
+      using fz0 apply auto
+      done
+    qed
+qed
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>Entire proper functions are precisely the non-trivial polynomials\<close>
+
+lemma proper_map_polyfun:
+    fixes c :: "nat \<Rightarrow> 'a::{real_normed_div_algebra,heine_borel}"
+  assumes "closed S" and "compact K" and c: "c i \<noteq> 0" "1 \<le> i" "i \<le> n"
+    shows "compact (S \<inter> {z. (\<Sum>i\<le>n. c i * z^i) \<in> K})"
+proof -
+  obtain B where "B > 0" and B: "\<And>x. x \<in> K \<Longrightarrow> norm x \<le> B"
+    by (metis compact_imp_bounded \<open>compact K\<close> bounded_pos)
+  have *: "norm x \<le> b"
+            if "\<And>x. b \<le> norm x \<Longrightarrow> B + 1 \<le> norm (\<Sum>i\<le>n. c i * x ^ i)"
+               "(\<Sum>i\<le>n. c i * x ^ i) \<in> K"  for b x
+  proof -
+    have "norm (\<Sum>i\<le>n. c i * x ^ i) \<le> B"
+      using B that by blast
+    moreover have "\<not> B + 1 \<le> B"
+      by simp
+    ultimately show "norm x \<le> b"
+      using that by (metis (no_types) less_eq_real_def not_less order_trans)
+  qed
+  have "bounded {z. (\<Sum>i\<le>n. c i * z ^ i) \<in> K}"
+    using Limits.polyfun_extremal [where c=c and B="B+1", OF c]
+    by (auto simp: bounded_pos eventually_at_infinity_pos *)
+  moreover have "closed ((\<lambda>z. (\<Sum>i\<le>n. c i * z ^ i)) -` K)"
+    apply (intro allI continuous_closed_vimage continuous_intros)
+    using \<open>compact K\<close> compact_eq_bounded_closed by blast
+  ultimately show ?thesis
+    using closed_Int_compact [OF \<open>closed S\<close>] compact_eq_bounded_closed
+    by (auto simp add: vimage_def)
+qed
+
+lemma proper_map_polyfun_univ:
+    fixes c :: "nat \<Rightarrow> 'a::{real_normed_div_algebra,heine_borel}"
+  assumes "compact K" "c i \<noteq> 0" "1 \<le> i" "i \<le> n"
+    shows "compact ({z. (\<Sum>i\<le>n. c i * z^i) \<in> K})"
+  using proper_map_polyfun [of UNIV K c i n] assms by simp
+
+lemma proper_map_polyfun_eq:
+  assumes "f holomorphic_on UNIV"
+    shows "(\<forall>k. compact k \<longrightarrow> compact {z. f z \<in> k}) \<longleftrightarrow>
+           (\<exists>c n. 0 < n \<and> (c n \<noteq> 0) \<and> f = (\<lambda>z. \<Sum>i\<le>n. c i * z^i))"
+          (is "?lhs = ?rhs")
+proof
+  assume compf [rule_format]: ?lhs
+  have 2: "\<exists>k. 0 < k \<and> a k \<noteq> 0 \<and> f = (\<lambda>z. \<Sum>i \<le> k. a i * z ^ i)"
+        if "\<And>z. f z = (\<Sum>i\<le>n. a i * z ^ i)" for a n
+  proof (cases "\<forall>i\<le>n. 0<i \<longrightarrow> a i = 0")
+    case True
+    then have [simp]: "\<And>z. f z = a 0"
+      by (simp add: that sum.atMost_shift)
+    have False using compf [of "{a 0}"] by simp
+    then show ?thesis ..
+  next
+    case False
+    then obtain k where k: "0 < k" "k\<le>n" "a k \<noteq> 0" by force
+    define m where "m = (GREATEST k. k\<le>n \<and> a k \<noteq> 0)"
+    have m: "m\<le>n \<and> a m \<noteq> 0"
+      unfolding m_def
+      apply (rule GreatestI_nat [where b = n])
+      using k apply auto
+      done
+    have [simp]: "a i = 0" if "m < i" "i \<le> n" for i
+      using Greatest_le_nat [where b = "n" and P = "\<lambda>k. k\<le>n \<and> a k \<noteq> 0"]
+      using m_def not_le that by auto
+    have "k \<le> m"
+      unfolding m_def
+      apply (rule Greatest_le_nat [where b = "n"])
+      using k apply auto
+      done
+    with k m show ?thesis
+      by (rule_tac x=m in exI) (auto simp: that comm_monoid_add_class.sum.mono_neutral_right)
+  qed
+  have "((inverse \<circ> f) \<longlongrightarrow> 0) at_infinity"
+  proof (rule Lim_at_infinityI)
+    fix e::real assume "0 < e"
+    with compf [of "cball 0 (inverse e)"]
+    show "\<exists>B. \<forall>x. B \<le> cmod x \<longrightarrow> dist ((inverse \<circ> f) x) 0 \<le> e"
+      apply simp
+      apply (clarsimp simp add: compact_eq_bounded_closed bounded_pos norm_inverse)
+      apply (rule_tac x="b+1" in exI)
+      apply (metis inverse_inverse_eq less_add_same_cancel2 less_imp_inverse_less add.commute not_le not_less_iff_gr_or_eq order_trans zero_less_one)
+      done
+  qed
+  then show ?rhs
+    apply (rule pole_at_infinity [OF assms])
+    using 2 apply blast
+    done
+next
+  assume ?rhs
+  then obtain c n where "0 < n" "c n \<noteq> 0" "f = (\<lambda>z. \<Sum>i\<le>n. c i * z ^ i)" by blast
+  then have "compact {z. f z \<in> k}" if "compact k" for k
+    by (auto intro: proper_map_polyfun_univ [OF that])
+  then show ?lhs by blast
+qed
+
+subsection \<open>Relating invertibility and nonvanishing of derivative\<close>
+
+lemma has_complex_derivative_locally_injective:
+  assumes holf: "f holomorphic_on S"
+      and S: "\<xi> \<in> S" "open S"
+      and dnz: "deriv f \<xi> \<noteq> 0"
+  obtains r where "r > 0" "ball \<xi> r \<subseteq> S" "inj_on f (ball \<xi> r)"
+proof -
+  have *: "\<exists>d>0. \<forall>x. dist \<xi> x < d \<longrightarrow> onorm (\<lambda>v. deriv f x * v - deriv f \<xi> * v) < e" if "e > 0" for e
+  proof -
+    have contdf: "continuous_on S (deriv f)"
+      by (simp add: holf holomorphic_deriv holomorphic_on_imp_continuous_on \<open>open S\<close>)
+    obtain \<delta> where "\<delta>>0" and \<delta>: "\<And>x. \<lbrakk>x \<in> S; dist x \<xi> \<le> \<delta>\<rbrakk> \<Longrightarrow> cmod (deriv f x - deriv f \<xi>) \<le> e/2"
+      using continuous_onE [OF contdf \<open>\<xi> \<in> S\<close>, of "e/2"] \<open>0 < e\<close>
+      by (metis dist_complex_def half_gt_zero less_imp_le)
+    obtain \<epsilon> where "\<epsilon>>0" "ball \<xi> \<epsilon> \<subseteq> S"
+      by (metis openE [OF \<open>open S\<close> \<open>\<xi> \<in> S\<close>])
+    with \<open>\<delta>>0\<close> have "\<exists>\<delta>>0. \<forall>x. dist \<xi> x < \<delta> \<longrightarrow> onorm (\<lambda>v. deriv f x * v - deriv f \<xi> * v) \<le> e/2"
+      apply (rule_tac x="min \<delta> \<epsilon>" in exI)
+      apply (intro conjI allI impI Operator_Norm.onorm_le)
+      apply simp
+      apply (simp only: Rings.ring_class.left_diff_distrib [symmetric] norm_mult)
+      apply (rule mult_right_mono [OF \<delta>])
+      apply (auto simp: dist_commute Rings.ordered_semiring_class.mult_right_mono \<delta>)
+      done
+    with \<open>e>0\<close> show ?thesis by force
+  qed
+  have "inj ((*) (deriv f \<xi>))"
+    using dnz by simp
+  then obtain g' where g': "linear g'" "g' \<circ> (*) (deriv f \<xi>) = id"
+    using linear_injective_left_inverse [of "(*) (deriv f \<xi>)"]
+    by (auto simp: linear_times)
+  show ?thesis
+    apply (rule has_derivative_locally_injective [OF S, where f=f and f' = "\<lambda>z h. deriv f z * h" and g' = g'])
+    using g' *
+    apply (simp_all add: linear_conv_bounded_linear that)
+    using DERIV_deriv_iff_field_differentiable has_field_derivative_imp_has_derivative holf
+        holomorphic_on_imp_differentiable_at \<open>open S\<close> apply blast
+    done
+qed
+
+lemma has_complex_derivative_locally_invertible:
+  assumes holf: "f holomorphic_on S"
+      and S: "\<xi> \<in> S" "open S"
+      and dnz: "deriv f \<xi> \<noteq> 0"
+  obtains r where "r > 0" "ball \<xi> r \<subseteq> S" "open (f `  (ball \<xi> r))" "inj_on f (ball \<xi> r)"
+proof -
+  obtain r where "r > 0" "ball \<xi> r \<subseteq> S" "inj_on f (ball \<xi> r)"
+    by (blast intro: that has_complex_derivative_locally_injective [OF assms])
+  then have \<xi>: "\<xi> \<in> ball \<xi> r" by simp
+  then have nc: "\<not> f constant_on ball \<xi> r"
+    using \<open>inj_on f (ball \<xi> r)\<close> injective_not_constant by fastforce
+  have holf': "f holomorphic_on ball \<xi> r"
+    using \<open>ball \<xi> r \<subseteq> S\<close> holf holomorphic_on_subset by blast
+  have "open (f ` ball \<xi> r)"
+    apply (rule open_mapping_thm [OF holf'])
+    using nc apply auto
+    done
+  then show ?thesis
+    using \<open>0 < r\<close> \<open>ball \<xi> r \<subseteq> S\<close> \<open>inj_on f (ball \<xi> r)\<close> that  by blast
+qed
+
+lemma holomorphic_injective_imp_regular:
+  assumes holf: "f holomorphic_on S"
+      and "open S" and injf: "inj_on f S"
+      and "\<xi> \<in> S"
+    shows "deriv f \<xi> \<noteq> 0"
+proof -
+  obtain r where "r>0" and r: "ball \<xi> r \<subseteq> S" using assms by (blast elim!: openE)
+  have holf': "f holomorphic_on ball \<xi> r"
+    using \<open>ball \<xi> r \<subseteq> S\<close> holf holomorphic_on_subset by blast
+  show ?thesis
+  proof (cases "\<forall>n>0. (deriv ^^ n) f \<xi> = 0")
+    case True
+    have fcon: "f w = f \<xi>" if "w \<in> ball \<xi> r" for w
+      apply (rule holomorphic_fun_eq_const_on_connected [OF holf'])
+      using True \<open>0 < r\<close> that by auto
+    have False
+      using fcon [of "\<xi> + r/2"] \<open>0 < r\<close> r injf unfolding inj_on_def
+      by (metis \<open>\<xi> \<in> S\<close> contra_subsetD dist_commute fcon mem_ball perfect_choose_dist)
+    then show ?thesis ..
+  next
+    case False
+    then obtain n0 where n0: "n0 > 0 \<and> (deriv ^^ n0) f \<xi> \<noteq> 0" by blast
+    define n where [abs_def]: "n = (LEAST n. n > 0 \<and> (deriv ^^ n) f \<xi> \<noteq> 0)"
+    have n_ne: "n > 0" "(deriv ^^ n) f \<xi> \<noteq> 0"
+      using def_LeastI [OF n_def n0] by auto
+    have n_min: "\<And>k. 0 < k \<Longrightarrow> k < n \<Longrightarrow> (deriv ^^ k) f \<xi> = 0"
+      using def_Least_le [OF n_def] not_le by auto
+    obtain g \<delta> where "0 < \<delta>"
+             and holg: "g holomorphic_on ball \<xi> \<delta>"
+             and fd: "\<And>w. w \<in> ball \<xi> \<delta> \<Longrightarrow> f w - f \<xi> = ((w - \<xi>) * g w) ^ n"
+             and gnz: "\<And>w. w \<in> ball \<xi> \<delta> \<Longrightarrow> g w \<noteq> 0"
+      apply (rule holomorphic_factor_order_of_zero_strong [OF holf \<open>open S\<close> \<open>\<xi> \<in> S\<close> n_ne])
+      apply (blast intro: n_min)+
+      done
+    show ?thesis
+    proof (cases "n=1")
+      case True
+      with n_ne show ?thesis by auto
+    next
+      case False
+      have holgw: "(\<lambda>w. (w - \<xi>) * g w) holomorphic_on ball \<xi> (min r \<delta>)"
+        apply (rule holomorphic_intros)+
+        using holg by (simp add: holomorphic_on_subset subset_ball)
+      have gd: "\<And>w. dist \<xi> w < \<delta> \<Longrightarrow> (g has_field_derivative deriv g w) (at w)"
+        using holg
+        by (simp add: DERIV_deriv_iff_field_differentiable holomorphic_on_def at_within_open_NO_MATCH)
+      have *: "\<And>w. w \<in> ball \<xi> (min r \<delta>)
+            \<Longrightarrow> ((\<lambda>w. (w - \<xi>) * g w) has_field_derivative ((w - \<xi>) * deriv g w + g w))
+                (at w)"
+        by (rule gd derivative_eq_intros | simp)+
+      have [simp]: "deriv (\<lambda>w. (w - \<xi>) * g w) \<xi> \<noteq> 0"
+        using * [of \<xi>] \<open>0 < \<delta>\<close> \<open>0 < r\<close> by (simp add: DERIV_imp_deriv gnz)
+      obtain T where "\<xi> \<in> T" "open T" and Tsb: "T \<subseteq> ball \<xi> (min r \<delta>)" and oimT: "open ((\<lambda>w. (w - \<xi>) * g w) ` T)"
+        apply (rule has_complex_derivative_locally_invertible [OF holgw, of \<xi>])
+        using \<open>0 < r\<close> \<open>0 < \<delta>\<close>
+        apply (simp_all add:)
+        by (meson open_ball centre_in_ball)
+      define U where "U = (\<lambda>w. (w - \<xi>) * g w) ` T"
+      have "open U" by (metis oimT U_def)
+      have "0 \<in> U"
+        apply (auto simp: U_def)
+        apply (rule image_eqI [where x = \<xi>])
+        apply (auto simp: \<open>\<xi> \<in> T\<close>)
+        done
+      then obtain \<epsilon> where "\<epsilon>>0" and \<epsilon>: "cball 0 \<epsilon> \<subseteq> U"
+        using \<open>open U\<close> open_contains_cball by blast
+      then have "\<epsilon> * exp(2 * of_real pi * \<i> * (0/n)) \<in> cball 0 \<epsilon>"
+                "\<epsilon> * exp(2 * of_real pi * \<i> * (1/n)) \<in> cball 0 \<epsilon>"
+        by (auto simp: norm_mult)
+      with \<epsilon> have "\<epsilon> * exp(2 * of_real pi * \<i> * (0/n)) \<in> U"
+                  "\<epsilon> * exp(2 * of_real pi * \<i> * (1/n)) \<in> U" by blast+
+      then obtain y0 y1 where "y0 \<in> T" and y0: "(y0 - \<xi>) * g y0 = \<epsilon> * exp(2 * of_real pi * \<i> * (0/n))"
+                          and "y1 \<in> T" and y1: "(y1 - \<xi>) * g y1 = \<epsilon> * exp(2 * of_real pi * \<i> * (1/n))"
+        by (auto simp: U_def)
+      then have "y0 \<in> ball \<xi> \<delta>" "y1 \<in> ball \<xi> \<delta>" using Tsb by auto
+      moreover have "y0 \<noteq> y1"
+        using y0 y1 \<open>\<epsilon> > 0\<close> complex_root_unity_eq_1 [of n 1] \<open>n > 0\<close> False by auto
+      moreover have "T \<subseteq> S"
+        by (meson Tsb min.cobounded1 order_trans r subset_ball)
+      ultimately have False
+        using inj_onD [OF injf, of y0 y1] \<open>y0 \<in> T\<close> \<open>y1 \<in> T\<close>
+        using fd [of y0] fd [of y1] complex_root_unity [of n 1] n_ne
+        apply (simp add: y0 y1 power_mult_distrib)
+        apply (force simp: algebra_simps)
+        done
+      then show ?thesis ..
+    qed
+  qed
+qed
+
+text\<open>Hence a nice clean inverse function theorem\<close>
+
+lemma has_field_derivative_inverse_strong:
+  fixes f :: "'a::{euclidean_space,real_normed_field} \<Rightarrow> 'a"
+  shows "\<lbrakk>DERIV f x :> f'; f' \<noteq> 0; open S; x \<in> S; continuous_on S f;
+         \<And>z. z \<in> S \<Longrightarrow> g (f z) = z\<rbrakk>
+         \<Longrightarrow> DERIV g (f x) :> inverse (f')"
+  unfolding has_field_derivative_def
+  by (rule has_derivative_inverse_strong [of S x f g]) auto
+
+lemma has_field_derivative_inverse_strong_x:
+  fixes f :: "'a::{euclidean_space,real_normed_field} \<Rightarrow> 'a"
+  shows  "\<lbrakk>DERIV f (g y) :> f'; f' \<noteq> 0; open S; continuous_on S f; g y \<in> S; f(g y) = y;
+           \<And>z. z \<in> S \<Longrightarrow> g (f z) = z\<rbrakk>
+          \<Longrightarrow> DERIV g y :> inverse (f')"
+  unfolding has_field_derivative_def
+  by (rule has_derivative_inverse_strong_x [of S g y f]) auto
+
+proposition holomorphic_has_inverse:
+  assumes holf: "f holomorphic_on S"
+      and "open S" and injf: "inj_on f S"
+  obtains g where "g holomorphic_on (f ` S)"
+                  "\<And>z. z \<in> S \<Longrightarrow> deriv f z * deriv g (f z) = 1"
+                  "\<And>z. z \<in> S \<Longrightarrow> g(f z) = z"
+proof -
+  have ofs: "open (f ` S)"
+    by (rule open_mapping_thm3 [OF assms])
+  have contf: "continuous_on S f"
+    by (simp add: holf holomorphic_on_imp_continuous_on)
+  have *: "(the_inv_into S f has_field_derivative inverse (deriv f z)) (at (f z))" if "z \<in> S" for z
+  proof -
+    have 1: "(f has_field_derivative deriv f z) (at z)"
+      using DERIV_deriv_iff_field_differentiable \<open>z \<in> S\<close> \<open>open S\<close> holf holomorphic_on_imp_differentiable_at
+      by blast
+    have 2: "deriv f z \<noteq> 0"
+      using \<open>z \<in> S\<close> \<open>open S\<close> holf holomorphic_injective_imp_regular injf by blast
+    show ?thesis
+      apply (rule has_field_derivative_inverse_strong [OF 1 2 \<open>open S\<close> \<open>z \<in> S\<close>])
+       apply (simp add: holf holomorphic_on_imp_continuous_on)
+      by (simp add: injf the_inv_into_f_f)
+  qed
+  show ?thesis
+    proof
+      show "the_inv_into S f holomorphic_on f ` S"
+        by (simp add: holomorphic_on_open ofs) (blast intro: *)
+    next
+      fix z assume "z \<in> S"
+      have "deriv f z \<noteq> 0"
+        using \<open>z \<in> S\<close> \<open>open S\<close> holf holomorphic_injective_imp_regular injf by blast
+      then show "deriv f z * deriv (the_inv_into S f) (f z) = 1"
+        using * [OF \<open>z \<in> S\<close>]  by (simp add: DERIV_imp_deriv)
+    next
+      fix z assume "z \<in> S"
+      show "the_inv_into S f (f z) = z"
+        by (simp add: \<open>z \<in> S\<close> injf the_inv_into_f_f)
+  qed
+qed
+
+subsection\<open>The Schwarz Lemma\<close>
+
+lemma Schwarz1:
+  assumes holf: "f holomorphic_on S"
+      and contf: "continuous_on (closure S) f"
+      and S: "open S" "connected S"
+      and boS: "bounded S"
+      and "S \<noteq> {}"
+  obtains w where "w \<in> frontier S"
+       "\<And>z. z \<in> closure S \<Longrightarrow> norm (f z) \<le> norm (f w)"
+proof -
+  have connf: "continuous_on (closure S) (norm o f)"
+    using contf continuous_on_compose continuous_on_norm_id by blast
+  have coc: "compact (closure S)"
+    by (simp add: \<open>bounded S\<close> bounded_closure compact_eq_bounded_closed)
+  then obtain x where x: "x \<in> closure S" and xmax: "\<And>z. z \<in> closure S \<Longrightarrow> norm(f z) \<le> norm(f x)"
+    apply (rule bexE [OF continuous_attains_sup [OF _ _ connf]])
+    using \<open>S \<noteq> {}\<close> apply auto
+    done
+  then show ?thesis
+  proof (cases "x \<in> frontier S")
+    case True
+    then show ?thesis using that xmax by blast
+  next
+    case False
+    then have "x \<in> S"
+      using \<open>open S\<close> frontier_def interior_eq x by auto
+    then have "f constant_on S"
+      apply (rule maximum_modulus_principle [OF holf S \<open>open S\<close> order_refl])
+      using closure_subset apply (blast intro: xmax)
+      done
+    then have "f constant_on (closure S)"
+      by (rule constant_on_closureI [OF _ contf])
+    then obtain c where c: "\<And>x. x \<in> closure S \<Longrightarrow> f x = c"
+      by (meson constant_on_def)
+    obtain w where "w \<in> frontier S"
+      by (metis coc all_not_in_conv assms(6) closure_UNIV frontier_eq_empty not_compact_UNIV)
+    then show ?thesis
+      by (simp add: c frontier_def that)
+  qed
+qed
+
+lemma Schwarz2:
+ "\<lbrakk>f holomorphic_on ball 0 r;
+    0 < s; ball w s \<subseteq> ball 0 r;
+    \<And>z. norm (w-z) < s \<Longrightarrow> norm(f z) \<le> norm(f w)\<rbrakk>
+    \<Longrightarrow> f constant_on ball 0 r"
+by (rule maximum_modulus_principle [where U = "ball w s" and \<xi> = w]) (simp_all add: dist_norm)
+
+lemma Schwarz3:
+  assumes holf: "f holomorphic_on (ball 0 r)" and [simp]: "f 0 = 0"
+  obtains h where "h holomorphic_on (ball 0 r)" and "\<And>z. norm z < r \<Longrightarrow> f z = z * (h z)" and "deriv f 0 = h 0"
+proof -
+  define h where "h z = (if z = 0 then deriv f 0 else f z / z)" for z
+  have d0: "deriv f 0 = h 0"
+    by (simp add: h_def)
+  moreover have "h holomorphic_on (ball 0 r)"
+    by (rule pole_theorem_open_0 [OF holf, of 0]) (auto simp: h_def)
+  moreover have "norm z < r \<Longrightarrow> f z = z * h z" for z
+    by (simp add: h_def)
+  ultimately show ?thesis
+    using that by blast
+qed
+
+proposition Schwarz_Lemma:
+  assumes holf: "f holomorphic_on (ball 0 1)" and [simp]: "f 0 = 0"
+      and no: "\<And>z. norm z < 1 \<Longrightarrow> norm (f z) < 1"
+      and \<xi>: "norm \<xi> < 1"
+    shows "norm (f \<xi>) \<le> norm \<xi>" and "norm(deriv f 0) \<le> 1"
+      and "((\<exists>z. norm z < 1 \<and> z \<noteq> 0 \<and> norm(f z) = norm z)
+            \<or> norm(deriv f 0) = 1)
+           \<Longrightarrow> \<exists>\<alpha>. (\<forall>z. norm z < 1 \<longrightarrow> f z = \<alpha> * z) \<and> norm \<alpha> = 1"
+      (is "?P \<Longrightarrow> ?Q")
+proof -
+  obtain h where holh: "h holomorphic_on (ball 0 1)"
+             and fz_eq: "\<And>z. norm z < 1 \<Longrightarrow> f z = z * (h z)" and df0: "deriv f 0 = h 0"
+    by (rule Schwarz3 [OF holf]) auto
+  have noh_le: "norm (h z) \<le> 1" if z: "norm z < 1" for z
+  proof -
+    have "norm (h z) < a" if a: "1 < a" for a
+    proof -
+      have "max (inverse a) (norm z) < 1"
+        using z a by (simp_all add: inverse_less_1_iff)
+      then obtain r where r: "max (inverse a) (norm z) < r" and "r < 1"
+        using Rats_dense_in_real by blast
+      then have nzr: "norm z < r" and ira: "inverse r < a"
+        using z a less_imp_inverse_less by force+
+      then have "0 < r"
+        by (meson norm_not_less_zero not_le order.strict_trans2)
+      have holh': "h holomorphic_on ball 0 r"
+        by (meson holh \<open>r < 1\<close> holomorphic_on_subset less_eq_real_def subset_ball)
+      have conth': "continuous_on (cball 0 r) h"
+        by (meson \<open>r < 1\<close> dual_order.trans holh holomorphic_on_imp_continuous_on holomorphic_on_subset mem_ball_0 mem_cball_0 not_less subsetI)
+      obtain w where w: "norm w = r" and lenw: "\<And>z. norm z < r \<Longrightarrow> norm(h z) \<le> norm(h w)"
+        apply (rule Schwarz1 [OF holh']) using conth' \<open>0 < r\<close> by auto
+      have "h w = f w / w" using fz_eq \<open>r < 1\<close> nzr w by auto
+      then have "cmod (h z) < inverse r"
+        by (metis \<open>0 < r\<close> \<open>r < 1\<close> divide_strict_right_mono inverse_eq_divide
+                  le_less_trans lenw no norm_divide nzr w)
+      then show ?thesis using ira by linarith
+    qed
+    then show "norm (h z) \<le> 1"
+      using not_le by blast
+  qed
+  show "cmod (f \<xi>) \<le> cmod \<xi>"
+  proof (cases "\<xi> = 0")
+    case True then show ?thesis by auto
+  next
+    case False
+    then show ?thesis
+      by (simp add: noh_le fz_eq \<xi> mult_left_le norm_mult)
+  qed
+  show no_df0: "norm(deriv f 0) \<le> 1"
+    by (simp add: \<open>\<And>z. cmod z < 1 \<Longrightarrow> cmod (h z) \<le> 1\<close> df0)
+  show "?Q" if "?P"
+    using that
+  proof
+    assume "\<exists>z. cmod z < 1 \<and> z \<noteq> 0 \<and> cmod (f z) = cmod z"
+    then obtain \<gamma> where \<gamma>: "cmod \<gamma> < 1" "\<gamma> \<noteq> 0" "cmod (f \<gamma>) = cmod \<gamma>" by blast
+    then have [simp]: "norm (h \<gamma>) = 1"
+      by (simp add: fz_eq norm_mult)
+    have "ball \<gamma> (1 - cmod \<gamma>) \<subseteq> ball 0 1"
+      by (simp add: ball_subset_ball_iff)
+    moreover have "\<And>z. cmod (\<gamma> - z) < 1 - cmod \<gamma> \<Longrightarrow> cmod (h z) \<le> cmod (h \<gamma>)"
+      apply (simp add: algebra_simps)
+      by (metis add_diff_cancel_left' diff_diff_eq2 le_less_trans noh_le norm_triangle_ineq4)
+    ultimately obtain c where c: "\<And>z. norm z < 1 \<Longrightarrow> h z = c"
+      using Schwarz2 [OF holh, of "1 - norm \<gamma>" \<gamma>, unfolded constant_on_def] \<gamma> by auto
+    then have "norm c = 1"
+      using \<gamma> by force
+    with c show ?thesis
+      using fz_eq by auto
+  next
+    assume [simp]: "cmod (deriv f 0) = 1"
+    then obtain c where c: "\<And>z. norm z < 1 \<Longrightarrow> h z = c"
+      using Schwarz2 [OF holh zero_less_one, of 0, unfolded constant_on_def] df0 noh_le
+      by auto
+    moreover have "norm c = 1"  using df0 c by auto
+    ultimately show ?thesis
+      using fz_eq by auto
+  qed
+qed
+
+corollary Schwarz_Lemma':
+  assumes holf: "f holomorphic_on (ball 0 1)" and [simp]: "f 0 = 0"
+      and no: "\<And>z. norm z < 1 \<Longrightarrow> norm (f z) < 1"
+    shows "((\<forall>\<xi>. norm \<xi> < 1 \<longrightarrow> norm (f \<xi>) \<le> norm \<xi>)
+            \<and> norm(deriv f 0) \<le> 1)
+            \<and> (((\<exists>z. norm z < 1 \<and> z \<noteq> 0 \<and> norm(f z) = norm z)
+              \<or> norm(deriv f 0) = 1)
+              \<longrightarrow> (\<exists>\<alpha>. (\<forall>z. norm z < 1 \<longrightarrow> f z = \<alpha> * z) \<and> norm \<alpha> = 1))"
+  using Schwarz_Lemma [OF assms]
+  by (metis (no_types) norm_eq_zero zero_less_one)
+
+subsection\<open>The Schwarz reflection principle\<close>
+
+lemma hol_pal_lem0:
+  assumes "d \<bullet> a \<le> k" "k \<le> d \<bullet> b"
+  obtains c where
+     "c \<in> closed_segment a b" "d \<bullet> c = k"
+     "\<And>z. z \<in> closed_segment a c \<Longrightarrow> d \<bullet> z \<le> k"
+     "\<And>z. z \<in> closed_segment c b \<Longrightarrow> k \<le> d \<bullet> z"
+proof -
+  obtain c where cin: "c \<in> closed_segment a b" and keq: "k = d \<bullet> c"
+    using connected_ivt_hyperplane [of "closed_segment a b" a b d k]
+    by (auto simp: assms)
+  have "closed_segment a c \<subseteq> {z. d \<bullet> z \<le> k}"  "closed_segment c b \<subseteq> {z. k \<le> d \<bullet> z}"
+    unfolding segment_convex_hull using assms keq
+    by (auto simp: convex_halfspace_le convex_halfspace_ge hull_minimal)
+  then show ?thesis using cin that by fastforce
+qed
+
+lemma hol_pal_lem1:
+  assumes "convex S" "open S"
+      and abc: "a \<in> S" "b \<in> S" "c \<in> S"
+          "d \<noteq> 0" and lek: "d \<bullet> a \<le> k" "d \<bullet> b \<le> k" "d \<bullet> c \<le> k"
+      and holf1: "f holomorphic_on {z. z \<in> S \<and> d \<bullet> z < k}"
+      and contf: "continuous_on S f"
+    shows "contour_integral (linepath a b) f +
+           contour_integral (linepath b c) f +
+           contour_integral (linepath c a) f = 0"
+proof -
+  have "interior (convex hull {a, b, c}) \<subseteq> interior(S \<inter> {x. d \<bullet> x \<le> k})"
+    apply (rule interior_mono)
+    apply (rule hull_minimal)
+     apply (simp add: abc lek)
+    apply (rule convex_Int [OF \<open>convex S\<close> convex_halfspace_le])
+    done
+  also have "... \<subseteq> {z \<in> S. d \<bullet> z < k}"
+    by (force simp: interior_open [OF \<open>open S\<close>] \<open>d \<noteq> 0\<close>)
+  finally have *: "interior (convex hull {a, b, c}) \<subseteq> {z \<in> S. d \<bullet> z < k}" .
+  have "continuous_on (convex hull {a,b,c}) f"
+    using \<open>convex S\<close> contf abc continuous_on_subset subset_hull
+    by fastforce
+  moreover have "f holomorphic_on interior (convex hull {a,b,c})"
+    by (rule holomorphic_on_subset [OF holf1 *])
+  ultimately show ?thesis
+    using Cauchy_theorem_triangle_interior has_chain_integral_chain_integral3
+      by blast
+qed
+
+lemma hol_pal_lem2:
+  assumes S: "convex S" "open S"
+      and abc: "a \<in> S" "b \<in> S" "c \<in> S"
+      and "d \<noteq> 0" and lek: "d \<bullet> a \<le> k" "d \<bullet> b \<le> k"
+      and holf1: "f holomorphic_on {z. z \<in> S \<and> d \<bullet> z < k}"
+      and holf2: "f holomorphic_on {z. z \<in> S \<and> k < d \<bullet> z}"
+      and contf: "continuous_on S f"
+    shows "contour_integral (linepath a b) f +
+           contour_integral (linepath b c) f +
+           contour_integral (linepath c a) f = 0"
+proof (cases "d \<bullet> c \<le> k")
+  case True show ?thesis
+    by (rule hol_pal_lem1 [OF S abc \<open>d \<noteq> 0\<close> lek True holf1 contf])
+next
+  case False
+  then have "d \<bullet> c > k" by force
+  obtain a' where a': "a' \<in> closed_segment b c" and "d \<bullet> a' = k"
+     and ba': "\<And>z. z \<in> closed_segment b a' \<Longrightarrow> d \<bullet> z \<le> k"
+     and a'c: "\<And>z. z \<in> closed_segment a' c \<Longrightarrow> k \<le> d \<bullet> z"
+    apply (rule hol_pal_lem0 [of d b k c, OF \<open>d \<bullet> b \<le> k\<close>])
+    using False by auto
+  obtain b' where b': "b' \<in> closed_segment a c" and "d \<bullet> b' = k"
+     and ab': "\<And>z. z \<in> closed_segment a b' \<Longrightarrow> d \<bullet> z \<le> k"
+     and b'c: "\<And>z. z \<in> closed_segment b' c \<Longrightarrow> k \<le> d \<bullet> z"
+    apply (rule hol_pal_lem0 [of d a k c, OF \<open>d \<bullet> a \<le> k\<close>])
+    using False by auto
+  have a'b': "a' \<in> S \<and> b' \<in> S"
+    using a' abc b' convex_contains_segment \<open>convex S\<close> by auto
+  have "continuous_on (closed_segment c a) f"
+    by (meson abc contf continuous_on_subset convex_contains_segment \<open>convex S\<close>)
+  then have 1: "contour_integral (linepath c a) f =
+                contour_integral (linepath c b') f + contour_integral (linepath b' a) f"
+    apply (rule contour_integral_split_linepath)
+    using b' by (simp add: closed_segment_commute)
+  have "continuous_on (closed_segment b c) f"
+    by (meson abc contf continuous_on_subset convex_contains_segment \<open>convex S\<close>)
+  then have 2: "contour_integral (linepath b c) f =
+                contour_integral (linepath b a') f + contour_integral (linepath a' c) f"
+    by (rule contour_integral_split_linepath [OF _ a'])
+  have 3: "contour_integral (reversepath (linepath b' a')) f =
+                - contour_integral (linepath b' a') f"
+    by (rule contour_integral_reversepath [OF valid_path_linepath])
+  have fcd_le: "f field_differentiable at x"
+               if "x \<in> interior S \<and> x \<in> interior {x. d \<bullet> x \<le> k}" for x
+  proof -
+    have "f holomorphic_on S \<inter> {c. d \<bullet> c < k}"
+      by (metis (no_types) Collect_conj_eq Collect_mem_eq holf1)
+    then have "\<exists>C D. x \<in> interior C \<inter> interior D \<and> f holomorphic_on interior C \<inter> interior D"
+      using that
+      by (metis Collect_mem_eq Int_Collect \<open>d \<noteq> 0\<close> interior_halfspace_le interior_open \<open>open S\<close>)
+    then show "f field_differentiable at x"
+      by (metis at_within_interior holomorphic_on_def interior_Int interior_interior)
+  qed
+  have ab_le: "\<And>x. x \<in> closed_segment a b \<Longrightarrow> d \<bullet> x \<le> k"
+  proof -
+    fix x :: complex
+    assume "x \<in> closed_segment a b"
+    then have "\<And>C. x \<in> C \<or> b \<notin> C \<or> a \<notin> C \<or> \<not> convex C"
+      by (meson contra_subsetD convex_contains_segment)
+    then show "d \<bullet> x \<le> k"
+      by (metis lek convex_halfspace_le mem_Collect_eq)
+  qed
+  have "continuous_on (S \<inter> {x. d \<bullet> x \<le> k}) f" using contf
+    by (simp add: continuous_on_subset)
+  then have "(f has_contour_integral 0)
+         (linepath a b +++ linepath b a' +++ linepath a' b' +++ linepath b' a)"
+    apply (rule Cauchy_theorem_convex [where K = "{}"])
+    apply (simp_all add: path_image_join convex_Int convex_halfspace_le \<open>convex S\<close> fcd_le ab_le
+                closed_segment_subset abc a'b' ba')
+    by (metis \<open>d \<bullet> a' = k\<close> \<open>d \<bullet> b' = k\<close> convex_contains_segment convex_halfspace_le lek(1) mem_Collect_eq order_refl)
+  then have 4: "contour_integral (linepath a b) f +
+                contour_integral (linepath b a') f +
+                contour_integral (linepath a' b') f +
+                contour_integral (linepath b' a) f = 0"
+    by (rule has_chain_integral_chain_integral4)
+  have fcd_ge: "f field_differentiable at x"
+               if "x \<in> interior S \<and> x \<in> interior {x. k \<le> d \<bullet> x}" for x
+  proof -
+    have f2: "f holomorphic_on S \<inter> {c. k < d \<bullet> c}"
+      by (metis (full_types) Collect_conj_eq Collect_mem_eq holf2)
+    have f3: "interior S = S"
+      by (simp add: interior_open \<open>open S\<close>)
+    then have "x \<in> S \<inter> interior {c. k \<le> d \<bullet> c}"
+      using that by simp
+    then show "f field_differentiable at x"
+      using f3 f2 unfolding holomorphic_on_def
+      by (metis (no_types) \<open>d \<noteq> 0\<close> at_within_interior interior_Int interior_halfspace_ge interior_interior)
+  qed
+  have "continuous_on (S \<inter> {x. k \<le> d \<bullet> x}) f" using contf
+    by (simp add: continuous_on_subset)
+  then have "(f has_contour_integral 0) (linepath a' c +++ linepath c b' +++ linepath b' a')"
+    apply (rule Cauchy_theorem_convex [where K = "{}"])
+    apply (simp_all add: path_image_join convex_Int convex_halfspace_ge \<open>convex S\<close>
+                      fcd_ge closed_segment_subset abc a'b' a'c)
+    by (metis \<open>d \<bullet> a' = k\<close> b'c closed_segment_commute convex_contains_segment
+              convex_halfspace_ge ends_in_segment(2) mem_Collect_eq order_refl)
+  then have 5: "contour_integral (linepath a' c) f + contour_integral (linepath c b') f + contour_integral (linepath b' a') f = 0"
+    by (rule has_chain_integral_chain_integral3)
+  show ?thesis
+    using 1 2 3 4 5 by (metis add.assoc eq_neg_iff_add_eq_0 reversepath_linepath)
+qed
+
+lemma hol_pal_lem3:
+  assumes S: "convex S" "open S"
+      and abc: "a \<in> S" "b \<in> S" "c \<in> S"
+      and "d \<noteq> 0" and lek: "d \<bullet> a \<le> k"
+      and holf1: "f holomorphic_on {z. z \<in> S \<and> d \<bullet> z < k}"
+      and holf2: "f holomorphic_on {z. z \<in> S \<and> k < d \<bullet> z}"
+      and contf: "continuous_on S f"
+    shows "contour_integral (linepath a b) f +
+           contour_integral (linepath b c) f +
+           contour_integral (linepath c a) f = 0"
+proof (cases "d \<bullet> b \<le> k")
+  case True show ?thesis
+    by (rule hol_pal_lem2 [OF S abc \<open>d \<noteq> 0\<close> lek True holf1 holf2 contf])
+next
+  case False
+  show ?thesis
+  proof (cases "d \<bullet> c \<le> k")
+    case True
+    have "contour_integral (linepath c a) f +
+          contour_integral (linepath a b) f +
+          contour_integral (linepath b c) f = 0"
+      by (rule hol_pal_lem2 [OF S \<open>c \<in> S\<close> \<open>a \<in> S\<close> \<open>b \<in> S\<close> \<open>d \<noteq> 0\<close> \<open>d \<bullet> c \<le> k\<close> lek holf1 holf2 contf])
+    then show ?thesis
+      by (simp add: algebra_simps)
+  next
+    case False
+    have "contour_integral (linepath b c) f +
+          contour_integral (linepath c a) f +
+          contour_integral (linepath a b) f = 0"
+      apply (rule hol_pal_lem2 [OF S \<open>b \<in> S\<close> \<open>c \<in> S\<close> \<open>a \<in> S\<close>, of "-d" "-k"])
+      using \<open>d \<noteq> 0\<close> \<open>\<not> d \<bullet> b \<le> k\<close> False by (simp_all add: holf1 holf2 contf)
+    then show ?thesis
+      by (simp add: algebra_simps)
+  qed
+qed
+
+lemma hol_pal_lem4:
+  assumes S: "convex S" "open S"
+      and abc: "a \<in> S" "b \<in> S" "c \<in> S" and "d \<noteq> 0"
+      and holf1: "f holomorphic_on {z. z \<in> S \<and> d \<bullet> z < k}"
+      and holf2: "f holomorphic_on {z. z \<in> S \<and> k < d \<bullet> z}"
+      and contf: "continuous_on S f"
+    shows "contour_integral (linepath a b) f +
+           contour_integral (linepath b c) f +
+           contour_integral (linepath c a) f = 0"
+proof (cases "d \<bullet> a \<le> k")
+  case True show ?thesis
+    by (rule hol_pal_lem3 [OF S abc \<open>d \<noteq> 0\<close> True holf1 holf2 contf])
+next
+  case False
+  show ?thesis
+    apply (rule hol_pal_lem3 [OF S abc, of "-d" "-k"])
+    using \<open>d \<noteq> 0\<close> False by (simp_all add: holf1 holf2 contf)
+qed
+
+lemma holomorphic_on_paste_across_line:
+  assumes S: "open S" and "d \<noteq> 0"
+      and holf1: "f holomorphic_on (S \<inter> {z. d \<bullet> z < k})"
+      and holf2: "f holomorphic_on (S \<inter> {z. k < d \<bullet> z})"
+      and contf: "continuous_on S f"
+    shows "f holomorphic_on S"
+proof -
+  have *: "\<exists>t. open t \<and> p \<in> t \<and> continuous_on t f \<and>
+               (\<forall>a b c. convex hull {a, b, c} \<subseteq> t \<longrightarrow>
+                         contour_integral (linepath a b) f +
+                         contour_integral (linepath b c) f +
+                         contour_integral (linepath c a) f = 0)"
+          if "p \<in> S" for p
+  proof -
+    obtain e where "e>0" and e: "ball p e \<subseteq> S"
+      using \<open>p \<in> S\<close> openE S by blast
+    then have "continuous_on (ball p e) f"
+      using contf continuous_on_subset by blast
+    moreover have "f holomorphic_on {z. dist p z < e \<and> d \<bullet> z < k}"
+      apply (rule holomorphic_on_subset [OF holf1])
+      using e by auto
+    moreover have "f holomorphic_on {z. dist p z < e \<and> k < d \<bullet> z}"
+      apply (rule holomorphic_on_subset [OF holf2])
+      using e by auto
+    ultimately show ?thesis
+      apply (rule_tac x="ball p e" in exI)
+      using \<open>e > 0\<close> e \<open>d \<noteq> 0\<close>
+      apply (simp add:, clarify)
+      apply (rule hol_pal_lem4 [of "ball p e" _ _ _ d _ k])
+      apply (auto simp: subset_hull)
+      done
+  qed
+  show ?thesis
+    by (blast intro: * Morera_local_triangle analytic_imp_holomorphic)
+qed
+
+proposition Schwarz_reflection:
+  assumes "open S" and cnjs: "cnj ` S \<subseteq> S"
+      and  holf: "f holomorphic_on (S \<inter> {z. 0 < Im z})"
+      and contf: "continuous_on (S \<inter> {z. 0 \<le> Im z}) f"
+      and f: "\<And>z. \<lbrakk>z \<in> S; z \<in> \<real>\<rbrakk> \<Longrightarrow> (f z) \<in> \<real>"
+    shows "(\<lambda>z. if 0 \<le> Im z then f z else cnj(f(cnj z))) holomorphic_on S"
+proof -
+  have 1: "(\<lambda>z. if 0 \<le> Im z then f z else cnj (f (cnj z))) holomorphic_on (S \<inter> {z. 0 < Im z})"
+    by (force intro: iffD1 [OF holomorphic_cong [OF refl] holf])
+  have cont_cfc: "continuous_on (S \<inter> {z. Im z \<le> 0}) (cnj o f o cnj)"
+    apply (intro continuous_intros continuous_on_compose continuous_on_subset [OF contf])
+    using cnjs apply auto
+    done
+  have "cnj \<circ> f \<circ> cnj field_differentiable at x within S \<inter> {z. Im z < 0}"
+        if "x \<in> S" "Im x < 0" "f field_differentiable at (cnj x) within S \<inter> {z. 0 < Im z}" for x
+    using that
+    apply (simp add: field_differentiable_def has_field_derivative_iff Lim_within dist_norm, clarify)
+    apply (rule_tac x="cnj f'" in exI)
+    apply (elim all_forward ex_forward conj_forward imp_forward asm_rl, clarify)
+    apply (drule_tac x="cnj xa" in bspec)
+    using cnjs apply force
+    apply (metis complex_cnj_cnj complex_cnj_diff complex_cnj_divide complex_mod_cnj)
+    done
+  then have hol_cfc: "(cnj o f o cnj) holomorphic_on (S \<inter> {z. Im z < 0})"
+    using holf cnjs
+    by (force simp: holomorphic_on_def)
+  have 2: "(\<lambda>z. if 0 \<le> Im z then f z else cnj (f (cnj z))) holomorphic_on (S \<inter> {z. Im z < 0})"
+    apply (rule iffD1 [OF holomorphic_cong [OF refl]])
+    using hol_cfc by auto
+  have [simp]: "(S \<inter> {z. 0 \<le> Im z}) \<union> (S \<inter> {z. Im z \<le> 0}) = S"
+    by force
+  have "continuous_on ((S \<inter> {z. 0 \<le> Im z}) \<union> (S \<inter> {z. Im z \<le> 0}))
+                       (\<lambda>z. if 0 \<le> Im z then f z else cnj (f (cnj z)))"
+    apply (rule continuous_on_cases_local)
+    using cont_cfc contf
+    apply (simp_all add: closedin_closed_Int closed_halfspace_Im_le closed_halfspace_Im_ge)
+    using f Reals_cnj_iff complex_is_Real_iff apply auto
+    done
+  then have 3: "continuous_on S (\<lambda>z. if 0 \<le> Im z then f z else cnj (f (cnj z)))"
+    by force
+  show ?thesis
+    apply (rule holomorphic_on_paste_across_line [OF \<open>open S\<close>, of "- \<i>" _ 0])
+    using 1 2 3
+    apply auto
+    done
+qed
+
+subsection\<open>Bloch's theorem\<close>
+
+lemma Bloch_lemma_0:
+  assumes holf: "f holomorphic_on cball 0 r" and "0 < r"
+      and [simp]: "f 0 = 0"
+      and le: "\<And>z. norm z < r \<Longrightarrow> norm(deriv f z) \<le> 2 * norm(deriv f 0)"
+    shows "ball 0 ((3 - 2 * sqrt 2) * r * norm(deriv f 0)) \<subseteq> f ` ball 0 r"
+proof -
+  have "sqrt 2 < 3/2"
+    by (rule real_less_lsqrt) (auto simp: power2_eq_square)
+  then have sq3: "0 < 3 - 2 * sqrt 2" by simp
+  show ?thesis
+  proof (cases "deriv f 0 = 0")
+    case True then show ?thesis by simp
+  next
+    case False
+    define C where "C = 2 * norm(deriv f 0)"
+    have "0 < C" using False by (simp add: C_def)
+    have holf': "f holomorphic_on ball 0 r" using holf
+      using ball_subset_cball holomorphic_on_subset by blast
+    then have holdf': "deriv f holomorphic_on ball 0 r"
+      by (rule holomorphic_deriv [OF _ open_ball])
+    have "Le1": "norm(deriv f z - deriv f 0) \<le> norm z / (r - norm z) * C"
+                if "norm z < r" for z
+    proof -
+      have T1: "norm(deriv f z - deriv f 0) \<le> norm z / (R - norm z) * C"
+              if R: "norm z < R" "R < r" for R
+      proof -
+        have "0 < R" using R
+          by (metis less_trans norm_zero zero_less_norm_iff)
+        have df_le: "\<And>x. norm x < r \<Longrightarrow> norm (deriv f x) \<le> C"
+          using le by (simp add: C_def)
+        have hol_df: "deriv f holomorphic_on cball 0 R"
+          apply (rule holomorphic_on_subset) using R holdf' by auto
+        have *: "((\<lambda>w. deriv f w / (w - z)) has_contour_integral 2 * pi * \<i> * deriv f z) (circlepath 0 R)"
+                 if "norm z < R" for z
+          using \<open>0 < R\<close> that Cauchy_integral_formula_convex_simple [OF convex_cball hol_df, of _ "circlepath 0 R"]
+          by (force simp: winding_number_circlepath)
+        have **: "((\<lambda>x. deriv f x / (x - z) - deriv f x / x) has_contour_integral
+                   of_real (2 * pi) * \<i> * (deriv f z - deriv f 0))
+                  (circlepath 0 R)"
+           using has_contour_integral_diff [OF * [of z] * [of 0]] \<open>0 < R\<close> that
+           by (simp add: algebra_simps)
+        have [simp]: "\<And>x. norm x = R \<Longrightarrow> x \<noteq> z"  using that(1) by blast
+        have "norm (deriv f x / (x - z) - deriv f x / x)
+                     \<le> C * norm z / (R * (R - norm z))"
+                  if "norm x = R" for x
+        proof -
+          have [simp]: "norm (deriv f x * x - deriv f x * (x - z)) =
+                        norm (deriv f x) * norm z"
+            by (simp add: norm_mult right_diff_distrib')
+          show ?thesis
+            using  \<open>0 < R\<close> \<open>0 < C\<close> R that
+            apply (simp add: norm_mult norm_divide divide_simps)
+            using df_le norm_triangle_ineq2 \<open>0 < C\<close> apply (auto intro!: mult_mono)
+            done
+        qed
+        then show ?thesis
+          using has_contour_integral_bound_circlepath
+                  [OF **, of "C * norm z/(R*(R - norm z))"]
+                \<open>0 < R\<close> \<open>0 < C\<close> R
+          apply (simp add: norm_mult norm_divide)
+          apply (simp add: divide_simps mult.commute)
+          done
+      qed
+      obtain r' where r': "norm z < r'" "r' < r"
+        using Rats_dense_in_real [of "norm z" r] \<open>norm z < r\<close> by blast
+      then have [simp]: "closure {r'<..<r} = {r'..r}" by simp
+      show ?thesis
+        apply (rule continuous_ge_on_closure
+                 [where f = "\<lambda>r. norm z / (r - norm z) * C" and s = "{r'<..<r}",
+                  OF _ _ T1])
+        apply (intro continuous_intros)
+        using that r'
+        apply (auto simp: not_le)
+        done
+    qed
+    have "*": "(norm z - norm z^2/(r - norm z)) * norm(deriv f 0) \<le> norm(f z)"
+              if r: "norm z < r" for z
+    proof -
+      have 1: "\<And>x. x \<in> ball 0 r \<Longrightarrow>
+              ((\<lambda>z. f z - deriv f 0 * z) has_field_derivative deriv f x - deriv f 0)
+               (at x within ball 0 r)"
+        by (rule derivative_eq_intros holomorphic_derivI holf' | simp)+
+      have 2: "closed_segment 0 z \<subseteq> ball 0 r"
+        by (metis \<open>0 < r\<close> convex_ball convex_contains_segment dist_self mem_ball mem_ball_0 that)
+      have 3: "(\<lambda>t. (norm z)\<^sup>2 * t / (r - norm z) * C) integrable_on {0..1}"
+        apply (rule integrable_on_cmult_right [where 'b=real, simplified])
+        apply (rule integrable_on_cdivide [where 'b=real, simplified])
+        apply (rule integrable_on_cmult_left [where 'b=real, simplified])
+        apply (rule ident_integrable_on)
+        done
+      have 4: "norm (deriv f (x *\<^sub>R z) - deriv f 0) * norm z \<le> norm z * norm z * x * C / (r - norm z)"
+              if x: "0 \<le> x" "x \<le> 1" for x
+      proof -
+        have [simp]: "x * norm z < r"
+          using r x by (meson le_less_trans mult_le_cancel_right2 norm_not_less_zero)
+        have "norm (deriv f (x *\<^sub>R z) - deriv f 0) \<le> norm (x *\<^sub>R z) / (r - norm (x *\<^sub>R z)) * C"
+          apply (rule Le1) using r x \<open>0 < r\<close> by simp
+        also have "... \<le> norm (x *\<^sub>R z) / (r - norm z) * C"
+          using r x \<open>0 < r\<close>
+          apply (simp add: field_split_simps)
+          by (simp add: \<open>0 < C\<close> mult.assoc mult_left_le_one_le ordered_comm_semiring_class.comm_mult_left_mono)
+        finally have "norm (deriv f (x *\<^sub>R z) - deriv f 0) * norm z \<le> norm (x *\<^sub>R z)  / (r - norm z) * C * norm z"
+          by (rule mult_right_mono) simp
+        with x show ?thesis by (simp add: algebra_simps)
+      qed
+      have le_norm: "abc \<le> norm d - e \<Longrightarrow> norm(f - d) \<le> e \<Longrightarrow> abc \<le> norm f" for abc d e and f::complex
+        by (metis add_diff_cancel_left' add_diff_eq diff_left_mono norm_diff_ineq order_trans)
+      have "norm (integral {0..1} (\<lambda>x. (deriv f (x *\<^sub>R z) - deriv f 0) * z))
+            \<le> integral {0..1} (\<lambda>t. (norm z)\<^sup>2 * t / (r - norm z) * C)"
+        apply (rule integral_norm_bound_integral)
+        using contour_integral_primitive [OF 1, of "linepath 0 z"] 2
+        apply (simp add: has_contour_integral_linepath has_integral_integrable_integral)
+        apply (rule 3)
+        apply (simp add: norm_mult power2_eq_square 4)
+        done
+      then have int_le: "norm (f z - deriv f 0 * z) \<le> (norm z)\<^sup>2 * norm(deriv f 0) / ((r - norm z))"
+        using contour_integral_primitive [OF 1, of "linepath 0 z"] 2
+        apply (simp add: has_contour_integral_linepath has_integral_integrable_integral C_def)
+        done
+      show ?thesis
+        apply (rule le_norm [OF _ int_le])
+        using \<open>norm z < r\<close>
+        apply (simp add: power2_eq_square divide_simps C_def norm_mult)
+        proof -
+          have "norm z * (norm (deriv f 0) * (r - norm z - norm z)) \<le> norm z * (norm (deriv f 0) * (r - norm z) - norm (deriv f 0) * norm z)"
+            by (simp add: algebra_simps)
+          then show "(norm z * (r - norm z) - norm z * norm z) * norm (deriv f 0) \<le> norm (deriv f 0) * norm z * (r - norm z) - norm z * norm z * norm (deriv f 0)"
+            by (simp add: algebra_simps)
+        qed
+    qed
+    have sq201 [simp]: "0 < (1 - sqrt 2 / 2)" "(1 - sqrt 2 / 2)  < 1"
+      by (auto simp:  sqrt2_less_2)
+    have 1: "continuous_on (closure (ball 0 ((1 - sqrt 2 / 2) * r))) f"
+      apply (rule continuous_on_subset [OF holomorphic_on_imp_continuous_on [OF holf]])
+      apply (subst closure_ball)
+      using \<open>0 < r\<close> mult_pos_pos sq201
+      apply (auto simp: cball_subset_cball_iff)
+      done
+    have 2: "open (f ` interior (ball 0 ((1 - sqrt 2 / 2) * r)))"
+      apply (rule open_mapping_thm [OF holf' open_ball connected_ball], force)
+      using \<open>0 < r\<close> mult_pos_pos sq201 apply (simp add: ball_subset_ball_iff)
+      using False \<open>0 < r\<close> centre_in_ball holf' holomorphic_nonconstant by blast
+    have "ball 0 ((3 - 2 * sqrt 2) * r * norm (deriv f 0)) =
+          ball (f 0) ((3 - 2 * sqrt 2) * r * norm (deriv f 0))"
+      by simp
+    also have "...  \<subseteq> f ` ball 0 ((1 - sqrt 2 / 2) * r)"
+    proof -
+      have 3: "(3 - 2 * sqrt 2) * r * norm (deriv f 0) \<le> norm (f z)"
+           if "norm z = (1 - sqrt 2 / 2) * r" for z
+        apply (rule order_trans [OF _ *])
+        using  \<open>0 < r\<close>
+        apply (simp_all add: field_simps  power2_eq_square that)
+        apply (simp add: mult.assoc [symmetric])
+        done
+      show ?thesis
+        apply (rule ball_subset_open_map_image [OF 1 2 _ bounded_ball])
+        using \<open>0 < r\<close> sq201 3 apply simp_all
+        using C_def \<open>0 < C\<close> sq3 apply force
+        done
+     qed
+    also have "...  \<subseteq> f ` ball 0 r"
+      apply (rule image_subsetI [OF imageI], simp)
+      apply (erule less_le_trans)
+      using \<open>0 < r\<close> apply (auto simp: field_simps)
+      done
+    finally show ?thesis .
+  qed
+qed
+
+lemma Bloch_lemma:
+  assumes holf: "f holomorphic_on cball a r" and "0 < r"
+      and le: "\<And>z. z \<in> ball a r \<Longrightarrow> norm(deriv f z) \<le> 2 * norm(deriv f a)"
+    shows "ball (f a) ((3 - 2 * sqrt 2) * r * norm(deriv f a)) \<subseteq> f ` ball a r"
+proof -
+  have fz: "(\<lambda>z. f (a + z)) = f o (\<lambda>z. (a + z))"
+    by (simp add: o_def)
+  have hol0: "(\<lambda>z. f (a + z)) holomorphic_on cball 0 r"
+    unfolding fz by (intro holomorphic_intros holf holomorphic_on_compose | simp)+
+  then have [simp]: "\<And>x. norm x < r \<Longrightarrow> (\<lambda>z. f (a + z)) field_differentiable at x"
+    by (metis open_ball at_within_open ball_subset_cball diff_0 dist_norm holomorphic_on_def holomorphic_on_subset mem_ball norm_minus_cancel)
+  have [simp]: "\<And>z. norm z < r \<Longrightarrow> f field_differentiable at (a + z)"
+    by (metis holf open_ball add_diff_cancel_left' dist_complex_def holomorphic_on_imp_differentiable_at holomorphic_on_subset interior_cball interior_subset mem_ball norm_minus_commute)
+  then have [simp]: "f field_differentiable at a"
+    by (metis add.comm_neutral \<open>0 < r\<close> norm_eq_zero)
+  have hol1: "(\<lambda>z. f (a + z) - f a) holomorphic_on cball 0 r"
+    by (intro holomorphic_intros hol0)
+  then have "ball 0 ((3 - 2 * sqrt 2) * r * norm (deriv (\<lambda>z. f (a + z) - f a) 0))
+             \<subseteq> (\<lambda>z. f (a + z) - f a) ` ball 0 r"
+    apply (rule Bloch_lemma_0)
+    apply (simp_all add: \<open>0 < r\<close>)
+    apply (simp add: fz deriv_chain)
+    apply (simp add: dist_norm le)
+    done
+  then show ?thesis
+    apply clarify
+    apply (drule_tac c="x - f a" in subsetD)
+     apply (force simp: fz \<open>0 < r\<close> dist_norm deriv_chain field_differentiable_compose)+
+    done
+qed
+
+proposition Bloch_unit:
+  assumes holf: "f holomorphic_on ball a 1" and [simp]: "deriv f a = 1"
+  obtains b r where "1/12 < r" and "ball b r \<subseteq> f ` (ball a 1)"
+proof -
+  define r :: real where "r = 249/256"
+  have "0 < r" "r < 1" by (auto simp: r_def)
+  define g where "g z = deriv f z * of_real(r - norm(z - a))" for z
+  have "deriv f holomorphic_on ball a 1"
+    by (rule holomorphic_deriv [OF holf open_ball])
+  then have "continuous_on (ball a 1) (deriv f)"
+    using holomorphic_on_imp_continuous_on by blast
+  then have "continuous_on (cball a r) (deriv f)"
+    by (rule continuous_on_subset) (simp add: cball_subset_ball_iff \<open>r < 1\<close>)
+  then have "continuous_on (cball a r) g"
+    by (simp add: g_def continuous_intros)
+  then have 1: "compact (g ` cball a r)"
+    by (rule compact_continuous_image [OF _ compact_cball])
+  have 2: "g ` cball a r \<noteq> {}"
+    using \<open>r > 0\<close> by auto
+  obtain p where pr: "p \<in> cball a r"
+             and pge: "\<And>y. y \<in> cball a r \<Longrightarrow> norm (g y) \<le> norm (g p)"
+    using distance_attains_sup [OF 1 2, of 0] by force
+  define t where "t = (r - norm(p - a)) / 2"
+  have "norm (p - a) \<noteq> r"
+    using pge [of a] \<open>r > 0\<close> by (auto simp: g_def norm_mult)
+  then have "norm (p - a) < r" using pr
+    by (simp add: norm_minus_commute dist_norm)
+  then have "0 < t"
+    by (simp add: t_def)
+  have cpt: "cball p t \<subseteq> ball a r"
+    using \<open>0 < t\<close> by (simp add: cball_subset_ball_iff dist_norm t_def field_simps)
+  have gen_le_dfp: "norm (deriv f y) * (r - norm (y - a)) / (r - norm (p - a)) \<le> norm (deriv f p)"
+            if "y \<in> cball a r" for y
+  proof -
+    have [simp]: "norm (y - a) \<le> r"
+      using that by (simp add: dist_norm norm_minus_commute)
+    have "norm (g y) \<le> norm (g p)"
+      using pge [OF that] by simp
+    then have "norm (deriv f y) * abs (r - norm (y - a)) \<le> norm (deriv f p) * abs (r - norm (p - a))"
+      by (simp only: dist_norm g_def norm_mult norm_of_real)
+    with that \<open>norm (p - a) < r\<close> show ?thesis
+      by (simp add: dist_norm field_split_simps)
+  qed
+  have le_norm_dfp: "r / (r - norm (p - a)) \<le> norm (deriv f p)"
+    using gen_le_dfp [of a] \<open>r > 0\<close> by auto
+  have 1: "f holomorphic_on cball p t"
+    apply (rule holomorphic_on_subset [OF holf])
+    using cpt \<open>r < 1\<close> order_subst1 subset_ball by auto
+  have 2: "norm (deriv f z) \<le> 2 * norm (deriv f p)" if "z \<in> ball p t" for z
+  proof -
+    have z: "z \<in> cball a r"
+      by (meson ball_subset_cball subsetD cpt that)
+    then have "norm(z - a) < r"
+      by (metis ball_subset_cball contra_subsetD cpt dist_norm mem_ball norm_minus_commute that)
+    have "norm (deriv f z) * (r - norm (z - a)) / (r - norm (p - a)) \<le> norm (deriv f p)"
+      using gen_le_dfp [OF z] by simp
+    with \<open>norm (z - a) < r\<close> \<open>norm (p - a) < r\<close>
+    have "norm (deriv f z) \<le> (r - norm (p - a)) / (r - norm (z - a)) * norm (deriv f p)"
+       by (simp add: field_simps)
+    also have "... \<le> 2 * norm (deriv f p)"
+      apply (rule mult_right_mono)
+      using that \<open>norm (p - a) < r\<close> \<open>norm(z - a) < r\<close>
+      apply (simp_all add: field_simps t_def dist_norm [symmetric])
+      using dist_triangle3 [of z a p] by linarith
+    finally show ?thesis .
+  qed
+  have sqrt2: "sqrt 2 < 2113/1494"
+    by (rule real_less_lsqrt) (auto simp: power2_eq_square)
+  then have sq3: "0 < 3 - 2 * sqrt 2" by simp
+  have "1 / 12 / ((3 - 2 * sqrt 2) / 2) < r"
+    using sq3 sqrt2 by (auto simp: field_simps r_def)
+  also have "... \<le> cmod (deriv f p) * (r - cmod (p - a))"
+    using \<open>norm (p - a) < r\<close> le_norm_dfp   by (simp add: pos_divide_le_eq)
+  finally have "1 / 12 < cmod (deriv f p) * (r - cmod (p - a)) * ((3 - 2 * sqrt 2) / 2)"
+    using pos_divide_less_eq half_gt_zero_iff sq3 by blast
+  then have **: "1 / 12 < (3 - 2 * sqrt 2) * t * norm (deriv f p)"
+    using sq3 by (simp add: mult.commute t_def)
+  have "ball (f p) ((3 - 2 * sqrt 2) * t * norm (deriv f p)) \<subseteq> f ` ball p t"
+    by (rule Bloch_lemma [OF 1 \<open>0 < t\<close> 2])
+  also have "... \<subseteq> f ` ball a 1"
+    apply (rule image_mono)
+    apply (rule order_trans [OF ball_subset_cball])
+    apply (rule order_trans [OF cpt])
+    using \<open>0 < t\<close> \<open>r < 1\<close> apply (simp add: ball_subset_ball_iff dist_norm)
+    done
+  finally have "ball (f p) ((3 - 2 * sqrt 2) * t * norm (deriv f p)) \<subseteq> f ` ball a 1" .
+  with ** show ?thesis
+    by (rule that)
+qed
+
+theorem Bloch:
+  assumes holf: "f holomorphic_on ball a r" and "0 < r"
+      and r': "r' \<le> r * norm (deriv f a) / 12"
+  obtains b where "ball b r' \<subseteq> f ` (ball a r)"
+proof (cases "deriv f a = 0")
+  case True with r' show ?thesis
+    using ball_eq_empty that by fastforce
+next
+  case False
+  define C where "C = deriv f a"
+  have "0 < norm C" using False by (simp add: C_def)
+  have dfa: "f field_differentiable at a"
+    apply (rule holomorphic_on_imp_differentiable_at [OF holf])
+    using \<open>0 < r\<close> by auto
+  have fo: "(\<lambda>z. f (a + of_real r * z)) = f o (\<lambda>z. (a + of_real r * z))"
+    by (simp add: o_def)
+  have holf': "f holomorphic_on (\<lambda>z. a + complex_of_real r * z) ` ball 0 1"
+    apply (rule holomorphic_on_subset [OF holf])
+    using \<open>0 < r\<close> apply (force simp: dist_norm norm_mult)
+    done
+  have 1: "(\<lambda>z. f (a + r * z) / (C * r)) holomorphic_on ball 0 1"
+    apply (rule holomorphic_intros holomorphic_on_compose holf' | simp add: fo)+
+    using \<open>0 < r\<close> by (simp add: C_def False)
+  have "((\<lambda>z. f (a + of_real r * z) / (C * of_real r)) has_field_derivative
+        (deriv f (a + of_real r * z) / C)) (at z)"
+       if "norm z < 1" for z
+  proof -
+    have *: "((\<lambda>x. f (a + of_real r * x)) has_field_derivative
+           (deriv f (a + of_real r * z) * of_real r)) (at z)"
+      apply (simp add: fo)
+      apply (rule DERIV_chain [OF field_differentiable_derivI])
+      apply (rule holomorphic_on_imp_differentiable_at [OF holf], simp)
+      using \<open>0 < r\<close> apply (simp add: dist_norm norm_mult that)
+      apply (rule derivative_eq_intros | simp)+
+      done
+    show ?thesis
+      apply (rule derivative_eq_intros * | simp)+
+      using \<open>0 < r\<close> by (auto simp: C_def False)
+  qed
+  have 2: "deriv (\<lambda>z. f (a + of_real r * z) / (C * of_real r)) 0 = 1"
+    apply (subst deriv_cdivide_right)
+    apply (simp add: field_differentiable_def fo)
+    apply (rule exI)
+    apply (rule DERIV_chain [OF field_differentiable_derivI])
+    apply (simp add: dfa)
+    apply (rule derivative_eq_intros | simp add: C_def False fo)+
+    using \<open>0 < r\<close>
+    apply (simp add: C_def False fo)
+    apply (simp add: derivative_intros dfa deriv_chain)
+    done
+  have sb1: "(*) (C * r) ` (\<lambda>z. f (a + of_real r * z) / (C * r)) ` ball 0 1
+             \<subseteq> f ` ball a r"
+    using \<open>0 < r\<close> by (auto simp: dist_norm norm_mult C_def False)
+  have sb2: "ball (C * r * b) r' \<subseteq> (*) (C * r) ` ball b t"
+             if "1 / 12 < t" for b t
+  proof -
+    have *: "r * cmod (deriv f a) / 12 \<le> r * (t * cmod (deriv f a))"
+      using that \<open>0 < r\<close> less_eq_real_def mult.commute mult.right_neutral mult_left_mono norm_ge_zero times_divide_eq_right
+      by auto
+    show ?thesis
+      apply clarify
+      apply (rule_tac x="x / (C * r)" in image_eqI)
+      using \<open>0 < r\<close>
+      apply (simp_all add: dist_norm norm_mult norm_divide C_def False field_simps)
+      apply (erule less_le_trans)
+      apply (rule order_trans [OF r' *])
+      done
+  qed
+  show ?thesis
+    apply (rule Bloch_unit [OF 1 2])
+    apply (rename_tac t)
+    apply (rule_tac b="(C * of_real r) * b" in that)
+    apply (drule image_mono [where f = "\<lambda>z. (C * of_real r) * z"])
+    using sb1 sb2
+    apply force
+    done
+qed
+
+corollary Bloch_general:
+  assumes holf: "f holomorphic_on s" and "a \<in> s"
+      and tle: "\<And>z. z \<in> frontier s \<Longrightarrow> t \<le> dist a z"
+      and rle: "r \<le> t * norm(deriv f a) / 12"
+  obtains b where "ball b r \<subseteq> f ` s"
+proof -
+  consider "r \<le> 0" | "0 < t * norm(deriv f a) / 12" using rle by force
+  then show ?thesis
+  proof cases
+    case 1 then show ?thesis
+      by (simp add: ball_empty that)
+  next
+    case 2
+    show ?thesis
+    proof (cases "deriv f a = 0")
+      case True then show ?thesis
+        using rle by (simp add: ball_empty that)
+    next
+      case False
+      then have "t > 0"
+        using 2 by (force simp: zero_less_mult_iff)
+      have "\<not> ball a t \<subseteq> s \<Longrightarrow> ball a t \<inter> frontier s \<noteq> {}"
+        apply (rule connected_Int_frontier [of "ball a t" s], simp_all)
+        using \<open>0 < t\<close> \<open>a \<in> s\<close> centre_in_ball apply blast
+        done
+      with tle have *: "ball a t \<subseteq> s" by fastforce
+      then have 1: "f holomorphic_on ball a t"
+        using holf using holomorphic_on_subset by blast
+      show ?thesis
+        apply (rule Bloch [OF 1 \<open>t > 0\<close> rle])
+        apply (rule_tac b=b in that)
+        using * apply force
+        done
+    qed
+  qed
+qed
+
+subsection \<open>Cauchy's residue theorem\<close>
+
+text\<open>Wenda Li and LC Paulson (2016). A Formal Proof of Cauchy's Residue Theorem.
+    Interactive Theorem Proving\<close>
+
+definition\<^marker>\<open>tag important\<close> residue :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> complex" where
+  "residue f z = (SOME int. \<exists>e>0. \<forall>\<epsilon>>0. \<epsilon><e
+    \<longrightarrow> (f has_contour_integral 2*pi* \<i> *int) (circlepath z \<epsilon>))"
+
+lemma Eps_cong:
+  assumes "\<And>x. P x = Q x"
+  shows   "Eps P = Eps Q"
+  using ext[of P Q, OF assms] by simp
+
+lemma residue_cong:
+  assumes eq: "eventually (\<lambda>z. f z = g z) (at z)" and "z = z'"
+  shows   "residue f z = residue g z'"
+proof -
+  from assms have eq': "eventually (\<lambda>z. g z = f z) (at z)"
+    by (simp add: eq_commute)
+  let ?P = "\<lambda>f c e. (\<forall>\<epsilon>>0. \<epsilon> < e \<longrightarrow>
+   (f has_contour_integral of_real (2 * pi) * \<i> * c) (circlepath z \<epsilon>))"
+  have "residue f z = residue g z" unfolding residue_def
+  proof (rule Eps_cong)
+    fix c :: complex
+    have "\<exists>e>0. ?P g c e"
+      if "\<exists>e>0. ?P f c e" and "eventually (\<lambda>z. f z = g z) (at z)" for f g
+    proof -
+      from that(1) obtain e where e: "e > 0" "?P f c e"
+        by blast
+      from that(2) obtain e' where e': "e' > 0" "\<And>z'. z' \<noteq> z \<Longrightarrow> dist z' z < e' \<Longrightarrow> f z' = g z'"
+        unfolding eventually_at by blast
+      have "?P g c (min e e')"
+      proof (intro allI exI impI, goal_cases)
+        case (1 \<epsilon>)
+        hence "(f has_contour_integral of_real (2 * pi) * \<i> * c) (circlepath z \<epsilon>)"
+          using e(2) by auto
+        thus ?case
+        proof (rule has_contour_integral_eq)
+          fix z' assume "z' \<in> path_image (circlepath z \<epsilon>)"
+          hence "dist z' z < e'" and "z' \<noteq> z"
+            using 1 by (auto simp: dist_commute)
+          with e'(2)[of z'] show "f z' = g z'" by simp
+        qed
+      qed
+      moreover from e and e' have "min e e' > 0" by auto
+      ultimately show ?thesis by blast
+    qed
+    from this[OF _ eq] and this[OF _ eq']
+      show "(\<exists>e>0. ?P f c e) \<longleftrightarrow> (\<exists>e>0. ?P g c e)"
+      by blast
+  qed
+  with assms show ?thesis by simp
+qed
+
+lemma contour_integral_circlepath_eq:
+  assumes "open s" and f_holo:"f holomorphic_on (s-{z})" and "0<e1" "e1\<le>e2"
+    and e2_cball:"cball z e2 \<subseteq> s"
+  shows
+    "f contour_integrable_on circlepath z e1"
+    "f contour_integrable_on circlepath z e2"
+    "contour_integral (circlepath z e2) f = contour_integral (circlepath z e1) f"
+proof -
+  define l where "l \<equiv> linepath (z+e2) (z+e1)"
+  have [simp]:"valid_path l" "pathstart l=z+e2" "pathfinish l=z+e1" unfolding l_def by auto
+  have "e2>0" using \<open>e1>0\<close> \<open>e1\<le>e2\<close> by auto
+  have zl_img:"z\<notin>path_image l"
+    proof
+      assume "z \<in> path_image l"
+      then have "e2 \<le> cmod (e2 - e1)"
+        using segment_furthest_le[of z "z+e2" "z+e1" "z+e2",simplified] \<open>e1>0\<close> \<open>e2>0\<close> unfolding l_def
+        by (auto simp add:closed_segment_commute)
+      thus False using \<open>e2>0\<close> \<open>e1>0\<close> \<open>e1\<le>e2\<close>
+        apply (subst (asm) norm_of_real)
+        by auto
+    qed
+  define g where "g \<equiv> circlepath z e2 +++ l +++ reversepath (circlepath z e1) +++ reversepath l"
+  show [simp]: "f contour_integrable_on circlepath z e2" "f contour_integrable_on (circlepath z e1)"
+    proof -
+      show "f contour_integrable_on circlepath z e2"
+        apply (intro contour_integrable_continuous_circlepath[OF
+                continuous_on_subset[OF holomorphic_on_imp_continuous_on[OF f_holo]]])
+        using \<open>e2>0\<close> e2_cball by auto
+      show "f contour_integrable_on (circlepath z e1)"
+        apply (intro contour_integrable_continuous_circlepath[OF
+                      continuous_on_subset[OF holomorphic_on_imp_continuous_on[OF f_holo]]])
+        using \<open>e1>0\<close> \<open>e1\<le>e2\<close> e2_cball by auto
+    qed
+  have [simp]:"f contour_integrable_on l"
+    proof -
+      have "closed_segment (z + e2) (z + e1) \<subseteq> cball z e2" using \<open>e2>0\<close> \<open>e1>0\<close> \<open>e1\<le>e2\<close>
+        by (intro closed_segment_subset,auto simp add:dist_norm)
+      hence "closed_segment (z + e2) (z + e1) \<subseteq> s - {z}" using zl_img e2_cball unfolding l_def
+        by auto
+      then show "f contour_integrable_on l" unfolding l_def
+        apply (intro contour_integrable_continuous_linepath[OF
+                      continuous_on_subset[OF holomorphic_on_imp_continuous_on[OF f_holo]]])
+        by auto
+    qed
+  let ?ig="\<lambda>g. contour_integral g f"
+  have "(f has_contour_integral 0) g"
+    proof (rule Cauchy_theorem_global[OF _ f_holo])
+      show "open (s - {z})" using \<open>open s\<close> by auto
+      show "valid_path g" unfolding g_def l_def by auto
+      show "pathfinish g = pathstart g" unfolding g_def l_def by auto
+    next
+      have path_img:"path_image g \<subseteq> cball z e2"
+        proof -
+          have "closed_segment (z + e2) (z + e1) \<subseteq> cball z e2" using \<open>e2>0\<close> \<open>e1>0\<close> \<open>e1\<le>e2\<close>
+            by (intro closed_segment_subset,auto simp add:dist_norm)
+          moreover have "sphere z \<bar>e1\<bar> \<subseteq> cball z e2" using \<open>e2>0\<close> \<open>e1\<le>e2\<close> \<open>e1>0\<close> by auto
+          ultimately show ?thesis unfolding g_def l_def using \<open>e2>0\<close>
+            by (simp add: path_image_join closed_segment_commute)
+        qed
+      show "path_image g \<subseteq> s - {z}"
+        proof -
+          have "z\<notin>path_image g" using zl_img
+            unfolding g_def l_def by (auto simp add: path_image_join closed_segment_commute)
+          moreover note \<open>cball z e2 \<subseteq> s\<close> and path_img
+          ultimately show ?thesis by auto
+        qed
+      show "winding_number g w = 0" when"w \<notin> s - {z}" for w
+        proof -
+          have "winding_number g w = 0" when "w\<notin>s" using that e2_cball
+            apply (intro winding_number_zero_outside[OF _ _ _ _ path_img])
+            by (auto simp add:g_def l_def)
+          moreover have "winding_number g z=0"
+            proof -
+              let ?Wz="\<lambda>g. winding_number g z"
+              have "?Wz g = ?Wz (circlepath z e2) + ?Wz l + ?Wz (reversepath (circlepath z e1))
+                  + ?Wz (reversepath l)"
+                using \<open>e2>0\<close> \<open>e1>0\<close> zl_img unfolding g_def l_def
+                by (subst winding_number_join,auto simp add:path_image_join closed_segment_commute)+
+              also have "... = ?Wz (circlepath z e2) + ?Wz (reversepath (circlepath z e1))"
+                using zl_img
+                apply (subst (2) winding_number_reversepath)
+                by (auto simp add:l_def closed_segment_commute)
+              also have "... = 0"
+                proof -
+                  have "?Wz (circlepath z e2) = 1" using \<open>e2>0\<close>
+                    by (auto intro: winding_number_circlepath_centre)
+                  moreover have "?Wz (reversepath (circlepath z e1)) = -1" using \<open>e1>0\<close>
+                    apply (subst winding_number_reversepath)
+                    by (auto intro: winding_number_circlepath_centre)
+                  ultimately show ?thesis by auto
+                qed
+              finally show ?thesis .
+            qed
+          ultimately show ?thesis using that by auto
+        qed
+    qed
+  then have "0 = ?ig g" using contour_integral_unique by simp
+  also have "... = ?ig (circlepath z e2) + ?ig l + ?ig (reversepath (circlepath z e1))
+      + ?ig (reversepath l)"
+    unfolding g_def
+    by (auto simp add:contour_integrable_reversepath_eq)
+  also have "... = ?ig (circlepath z e2)  - ?ig (circlepath z e1)"
+    by (auto simp add:contour_integral_reversepath)
+  finally show "contour_integral (circlepath z e2) f = contour_integral (circlepath z e1) f"
+    by simp
+qed
+
+lemma base_residue:
+  assumes "open s" "z\<in>s" "r>0" and f_holo:"f holomorphic_on (s - {z})"
+    and r_cball:"cball z r \<subseteq> s"
+  shows "(f has_contour_integral 2 * pi * \<i> * (residue f z)) (circlepath z r)"
+proof -
+  obtain e where "e>0" and e_cball:"cball z e \<subseteq> s"
+    using open_contains_cball[of s] \<open>open s\<close> \<open>z\<in>s\<close> by auto
+  define c where "c \<equiv> 2 * pi * \<i>"
+  define i where "i \<equiv> contour_integral (circlepath z e) f / c"
+  have "(f has_contour_integral c*i) (circlepath z \<epsilon>)" when "\<epsilon>>0" "\<epsilon><e" for \<epsilon>
+    proof -
+      have "contour_integral (circlepath z e) f = contour_integral (circlepath z \<epsilon>) f"
+          "f contour_integrable_on circlepath z \<epsilon>"
+          "f contour_integrable_on circlepath z e"
+        using \<open>\<epsilon><e\<close>
+        by (intro contour_integral_circlepath_eq[OF \<open>open s\<close> f_holo \<open>\<epsilon>>0\<close> _ e_cball],auto)+
+      then show ?thesis unfolding i_def c_def
+        by (auto intro:has_contour_integral_integral)
+    qed
+  then have "\<exists>e>0. \<forall>\<epsilon>>0. \<epsilon><e \<longrightarrow> (f has_contour_integral c * (residue f z)) (circlepath z \<epsilon>)"
+    unfolding residue_def c_def
+    apply (rule_tac someI[of _ i],intro  exI[where x=e])
+    by (auto simp add:\<open>e>0\<close> c_def)
+  then obtain e' where "e'>0"
+      and e'_def:"\<forall>\<epsilon>>0. \<epsilon><e' \<longrightarrow> (f has_contour_integral c * (residue f z)) (circlepath z \<epsilon>)"
+    by auto
+  let ?int="\<lambda>e. contour_integral (circlepath z e) f"
+  define  \<epsilon> where "\<epsilon> \<equiv> Min {r,e'} / 2"
+  have "\<epsilon>>0" "\<epsilon>\<le>r" "\<epsilon><e'" using \<open>r>0\<close> \<open>e'>0\<close> unfolding \<epsilon>_def by auto
+  have "(f has_contour_integral c * (residue f z)) (circlepath z \<epsilon>)"
+    using e'_def[rule_format,OF \<open>\<epsilon>>0\<close> \<open>\<epsilon><e'\<close>] .
+  then show ?thesis unfolding c_def
+    using contour_integral_circlepath_eq[OF \<open>open s\<close> f_holo \<open>\<epsilon>>0\<close> \<open>\<epsilon>\<le>r\<close> r_cball]
+    by (auto elim: has_contour_integral_eqpath[of _ _ "circlepath z \<epsilon>" "circlepath z r"])
+qed
+
+lemma residue_holo:
+  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s"
+  shows "residue f z = 0"
+proof -
+  define c where "c \<equiv> 2 * pi * \<i>"
+  obtain e where "e>0" and e_cball:"cball z e \<subseteq> s" using \<open>open s\<close> \<open>z\<in>s\<close>
+    using open_contains_cball_eq by blast
+  have "(f has_contour_integral c*residue f z) (circlepath z e)"
+    using f_holo
+    by (auto intro: base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c_def])
+  moreover have "(f has_contour_integral 0) (circlepath z e)"
+    using f_holo e_cball \<open>e>0\<close>
+    by (auto intro: Cauchy_theorem_convex_simple[of _ "cball z e"])
+  ultimately have "c*residue f z =0"
+    using has_contour_integral_unique by blast
+  thus ?thesis unfolding c_def  by auto
+qed
+
+lemma residue_const:"residue (\<lambda>_. c) z = 0"
+  by (intro residue_holo[of "UNIV::complex set"],auto intro:holomorphic_intros)
+
+lemma residue_add:
+  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
+      and g_holo:"g holomorphic_on s - {z}"
+  shows "residue (\<lambda>z. f z + g z) z= residue f z + residue g z"
+proof -
+  define c where "c \<equiv> 2 * pi * \<i>"
+  define fg where "fg \<equiv> (\<lambda>z. f z+g z)"
+  obtain e where "e>0" and e_cball:"cball z e \<subseteq> s" using \<open>open s\<close> \<open>z\<in>s\<close>
+    using open_contains_cball_eq by blast
+  have "(fg has_contour_integral c * residue fg z) (circlepath z e)"
+    unfolding fg_def using f_holo g_holo
+    apply (intro base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c_def])
+    by (auto intro:holomorphic_intros)
+  moreover have "(fg has_contour_integral c*residue f z + c* residue g z) (circlepath z e)"
+    unfolding fg_def using f_holo g_holo
+    by (auto intro: has_contour_integral_add base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c_def])
+  ultimately have "c*(residue f z + residue g z) = c * residue fg z"
+    using has_contour_integral_unique by (auto simp add:distrib_left)
+  thus ?thesis unfolding fg_def
+    by (auto simp add:c_def)
+qed
+
+lemma residue_lmul:
+  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
+  shows "residue (\<lambda>z. c * (f z)) z= c * residue f z"
+proof (cases "c=0")
+  case True
+  thus ?thesis using residue_const by auto
+next
+  case False
+  define c' where "c' \<equiv> 2 * pi * \<i>"
+  define f' where "f' \<equiv> (\<lambda>z. c * (f z))"
+  obtain e where "e>0" and e_cball:"cball z e \<subseteq> s" using \<open>open s\<close> \<open>z\<in>s\<close>
+    using open_contains_cball_eq by blast
+  have "(f' has_contour_integral c' * residue f' z) (circlepath z e)"
+    unfolding f'_def using f_holo
+    apply (intro base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c'_def])
+    by (auto intro:holomorphic_intros)
+  moreover have "(f' has_contour_integral c * (c' * residue f z)) (circlepath z e)"
+    unfolding f'_def using f_holo
+    by (auto intro: has_contour_integral_lmul
+      base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c'_def])
+  ultimately have "c' * residue f' z  = c * (c' * residue f z)"
+    using has_contour_integral_unique by auto
+  thus ?thesis unfolding f'_def c'_def using False
+    by (auto simp add:field_simps)
+qed
+
+lemma residue_rmul:
+  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
+  shows "residue (\<lambda>z. (f z) * c) z= residue f z * c"
+using residue_lmul[OF assms,of c] by (auto simp add:algebra_simps)
+
+lemma residue_div:
+  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
+  shows "residue (\<lambda>z. (f z) / c) z= residue f z / c "
+using residue_lmul[OF assms,of "1/c"] by (auto simp add:algebra_simps)
+
+lemma residue_neg:
+  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
+  shows "residue (\<lambda>z. - (f z)) z= - residue f z"
+using residue_lmul[OF assms,of "-1"] by auto
+
+lemma residue_diff:
+  assumes "open s" "z \<in> s" and f_holo: "f holomorphic_on s - {z}"
+      and g_holo:"g holomorphic_on s - {z}"
+  shows "residue (\<lambda>z. f z - g z) z= residue f z - residue g z"
+using residue_add[OF assms(1,2,3),of "\<lambda>z. - g z"] residue_neg[OF assms(1,2,4)]
+by (auto intro:holomorphic_intros g_holo)
+
+lemma residue_simple:
+  assumes "open s" "z\<in>s" and f_holo:"f holomorphic_on s"
+  shows "residue (\<lambda>w. f w / (w - z)) z = f z"
+proof -
+  define c where "c \<equiv> 2 * pi * \<i>"
+  define f' where "f' \<equiv> \<lambda>w. f w / (w - z)"
+  obtain e where "e>0" and e_cball:"cball z e \<subseteq> s" using \<open>open s\<close> \<open>z\<in>s\<close>
+    using open_contains_cball_eq by blast
+  have "(f' has_contour_integral c * f z) (circlepath z e)"
+    unfolding f'_def c_def using \<open>e>0\<close> f_holo e_cball
+    by (auto intro!: Cauchy_integral_circlepath_simple holomorphic_intros)
+  moreover have "(f' has_contour_integral c * residue f' z) (circlepath z e)"
+    unfolding f'_def using f_holo
+    apply (intro base_residue[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>e>0\<close> _ e_cball,folded c_def])
+    by (auto intro!:holomorphic_intros)
+  ultimately have "c * f z = c * residue f' z"
+    using has_contour_integral_unique by blast
+  thus ?thesis unfolding c_def f'_def  by auto
+qed
+
+lemma residue_simple':
+  assumes s: "open s" "z \<in> s" and holo: "f holomorphic_on (s - {z})"
+      and lim: "((\<lambda>w. f w * (w - z)) \<longlongrightarrow> c) (at z)"
+  shows   "residue f z = c"
+proof -
+  define g where "g = (\<lambda>w. if w = z then c else f w * (w - z))"
+  from holo have "(\<lambda>w. f w * (w - z)) holomorphic_on (s - {z})" (is "?P")
+    by (force intro: holomorphic_intros)
+  also have "?P \<longleftrightarrow> g holomorphic_on (s - {z})"
+    by (intro holomorphic_cong refl) (simp_all add: g_def)
+  finally have *: "g holomorphic_on (s - {z})" .
+
+  note lim
+  also have "(\<lambda>w. f w * (w - z)) \<midarrow>z\<rightarrow> c \<longleftrightarrow> g \<midarrow>z\<rightarrow> g z"
+    by (intro filterlim_cong refl) (simp_all add: g_def [abs_def] eventually_at_filter)
+  finally have **: "g \<midarrow>z\<rightarrow> g z" .
+
+  have g_holo: "g holomorphic_on s"
+    by (rule no_isolated_singularity'[where K = "{z}"])
+       (insert assms * **, simp_all add: at_within_open_NO_MATCH)
+  from s and this have "residue (\<lambda>w. g w / (w - z)) z = g z"
+    by (rule residue_simple)
+  also have "\<forall>\<^sub>F za in at z. g za / (za - z) = f za"
+    unfolding eventually_at by (auto intro!: exI[of _ 1] simp: field_simps g_def)
+  hence "residue (\<lambda>w. g w / (w - z)) z = residue f z"
+    by (intro residue_cong refl)
+  finally show ?thesis
+    by (simp add: g_def)
+qed
+
+lemma residue_holomorphic_over_power:
+  assumes "open A" "z0 \<in> A" "f holomorphic_on A"
+  shows   "residue (\<lambda>z. f z / (z - z0) ^ Suc n) z0 = (deriv ^^ n) f z0 / fact n"
+proof -
+  let ?f = "\<lambda>z. f z / (z - z0) ^ Suc n"
+  from assms(1,2) obtain r where r: "r > 0" "cball z0 r \<subseteq> A"
+    by (auto simp: open_contains_cball)
+  have "(?f has_contour_integral 2 * pi * \<i> * residue ?f z0) (circlepath z0 r)"
+    using r assms by (intro base_residue[of A]) (auto intro!: holomorphic_intros)
+  moreover have "(?f has_contour_integral 2 * pi * \<i> / fact n * (deriv ^^ n) f z0) (circlepath z0 r)"
+    using assms r
+    by (intro Cauchy_has_contour_integral_higher_derivative_circlepath)
+       (auto intro!: holomorphic_on_subset[OF assms(3)] holomorphic_on_imp_continuous_on)
+  ultimately have "2 * pi * \<i> * residue ?f z0 = 2 * pi * \<i> / fact n * (deriv ^^ n) f z0"
+    by (rule has_contour_integral_unique)
+  thus ?thesis by (simp add: field_simps)
+qed
+
+lemma residue_holomorphic_over_power':
+  assumes "open A" "0 \<in> A" "f holomorphic_on A"
+  shows   "residue (\<lambda>z. f z / z ^ Suc n) 0 = (deriv ^^ n) f 0 / fact n"
+  using residue_holomorphic_over_power[OF assms] by simp
+
+theorem residue_fps_expansion_over_power_at_0:
+  assumes "f has_fps_expansion F"
+  shows   "residue (\<lambda>z. f z / z ^ Suc n) 0 = fps_nth F n"
+proof -
+  from has_fps_expansion_imp_holomorphic[OF assms] guess s . note s = this
+  have "residue (\<lambda>z. f z / (z - 0) ^ Suc n) 0 = (deriv ^^ n) f 0 / fact n"
+    using assms s unfolding has_fps_expansion_def
+    by (intro residue_holomorphic_over_power[of s]) (auto simp: zero_ereal_def)
+  also from assms have "\<dots> = fps_nth F n"
+    by (subst fps_nth_fps_expansion) auto
+  finally show ?thesis by simp
+qed
+
+lemma get_integrable_path:
+  assumes "open s" "connected (s-pts)" "finite pts" "f holomorphic_on (s-pts) " "a\<in>s-pts" "b\<in>s-pts"
+  obtains g where "valid_path g" "pathstart g = a" "pathfinish g = b"
+    "path_image g \<subseteq> s-pts" "f contour_integrable_on g" using assms
+proof (induct arbitrary:s thesis a rule:finite_induct[OF \<open>finite pts\<close>])
+  case 1
+  obtain g where "valid_path g" "path_image g \<subseteq> s" "pathstart g = a" "pathfinish g = b"
+    using connected_open_polynomial_connected[OF \<open>open s\<close>,of a b ] \<open>connected (s - {})\<close>
+      valid_path_polynomial_function "1.prems"(6) "1.prems"(7) by auto
+  moreover have "f contour_integrable_on g"
+    using contour_integrable_holomorphic_simple[OF _ \<open>open s\<close> \<open>valid_path g\<close> \<open>path_image g \<subseteq> s\<close>,of f]
+      \<open>f holomorphic_on s - {}\<close>
+    by auto
+  ultimately show ?case using "1"(1)[of g] by auto
+next
+  case idt:(2 p pts)
+  obtain e where "e>0" and e:"\<forall>w\<in>ball a e. w \<in> s \<and> (w \<noteq> a \<longrightarrow> w \<notin> insert p pts)"
+    using finite_ball_avoid[OF \<open>open s\<close> \<open>finite (insert p pts)\<close>, of a]
+      \<open>a \<in> s - insert p pts\<close>
+    by auto
+  define a' where "a' \<equiv> a+e/2"
+  have "a'\<in>s-{p} -pts"  using e[rule_format,of "a+e/2"] \<open>e>0\<close>
+    by (auto simp add:dist_complex_def a'_def)
+  then obtain g' where g'[simp]:"valid_path g'" "pathstart g' = a'" "pathfinish g' = b"
+    "path_image g' \<subseteq> s - {p} - pts" "f contour_integrable_on g'"
+    using idt.hyps(3)[of a' "s-{p}"] idt.prems idt.hyps(1)
+    by (metis Diff_insert2 open_delete)
+  define g where "g \<equiv> linepath a a' +++ g'"
+  have "valid_path g" unfolding g_def by (auto intro: valid_path_join)
+  moreover have "pathstart g = a" and  "pathfinish g = b" unfolding g_def by auto
+  moreover have "path_image g \<subseteq> s - insert p pts" unfolding g_def
+    proof (rule subset_path_image_join)
+      have "closed_segment a a' \<subseteq> ball a e" using \<open>e>0\<close>
+        by (auto dest!:segment_bound1 simp:a'_def dist_complex_def norm_minus_commute)
+      then show "path_image (linepath a a') \<subseteq> s - insert p pts" using e idt(9)
+        by auto
+    next
+      show "path_image g' \<subseteq> s - insert p pts" using g'(4) by blast
+    qed
+  moreover have "f contour_integrable_on g"
+    proof -
+      have "closed_segment a a' \<subseteq> ball a e" using \<open>e>0\<close>
+        by (auto dest!:segment_bound1 simp:a'_def dist_complex_def norm_minus_commute)
+      then have "continuous_on (closed_segment a a') f"
+        using e idt.prems(6) holomorphic_on_imp_continuous_on[OF idt.prems(5)]
+        apply (elim continuous_on_subset)
+        by auto
+      then have "f contour_integrable_on linepath a a'"
+        using contour_integrable_continuous_linepath by auto
+      then show ?thesis unfolding g_def
+        apply (rule contour_integrable_joinI)
+        by (auto simp add: \<open>e>0\<close>)
+    qed
+  ultimately show ?case using idt.prems(1)[of g] by auto
+qed
+
+lemma Cauchy_theorem_aux:
+  assumes "open s" "connected (s-pts)" "finite pts" "pts \<subseteq> s" "f holomorphic_on s-pts"
+          "valid_path g" "pathfinish g = pathstart g" "path_image g \<subseteq> s-pts"
+          "\<forall>z. (z \<notin> s) \<longrightarrow> winding_number g z  = 0"
+          "\<forall>p\<in>s. h p>0 \<and> (\<forall>w\<in>cball p (h p). w\<in>s \<and> (w\<noteq>p \<longrightarrow> w \<notin> pts))"
+  shows "contour_integral g f = (\<Sum>p\<in>pts. winding_number g p * contour_integral (circlepath p (h p)) f)"
+    using assms
+proof (induct arbitrary:s g rule:finite_induct[OF \<open>finite pts\<close>])
+  case 1
+  then show ?case by (simp add: Cauchy_theorem_global contour_integral_unique)
+next
+  case (2 p pts)
+  note fin[simp] = \<open>finite (insert p pts)\<close>
+    and connected = \<open>connected (s - insert p pts)\<close>
+    and valid[simp] = \<open>valid_path g\<close>
+    and g_loop[simp] = \<open>pathfinish g = pathstart g\<close>
+    and holo[simp]= \<open>f holomorphic_on s - insert p pts\<close>
+    and path_img = \<open>path_image g \<subseteq> s - insert p pts\<close>
+    and winding = \<open>\<forall>z. z \<notin> s \<longrightarrow> winding_number g z = 0\<close>
+    and h = \<open>\<forall>pa\<in>s. 0 < h pa \<and> (\<forall>w\<in>cball pa (h pa). w \<in> s \<and> (w \<noteq> pa \<longrightarrow> w \<notin> insert p pts))\<close>
+  have "h p>0" and "p\<in>s"
+    and h_p: "\<forall>w\<in>cball p (h p). w \<in> s \<and> (w \<noteq> p \<longrightarrow> w \<notin> insert p pts)"
+    using h \<open>insert p pts \<subseteq> s\<close> by auto
+  obtain pg where pg[simp]: "valid_path pg" "pathstart pg = pathstart g" "pathfinish pg=p+h p"
+      "path_image pg \<subseteq> s-insert p pts" "f contour_integrable_on pg"
+    proof -
+      have "p + h p\<in>cball p (h p)" using h[rule_format,of p]
+        by (simp add: \<open>p \<in> s\<close> dist_norm)
+      then have "p + h p \<in> s - insert p pts" using h[rule_format,of p] \<open>insert p pts \<subseteq> s\<close>
+        by fastforce
+      moreover have "pathstart g \<in> s - insert p pts " using path_img by auto
+      ultimately show ?thesis
+        using get_integrable_path[OF \<open>open s\<close> connected fin holo,of "pathstart g" "p+h p"] that
+        by blast
+    qed
+  obtain n::int where "n=winding_number g p"
+    using integer_winding_number[OF _ g_loop,of p] valid path_img
+    by (metis DiffD2 Ints_cases insertI1 subset_eq valid_path_imp_path)
+  define p_circ where "p_circ \<equiv> circlepath p (h p)"
+  define p_circ_pt where "p_circ_pt \<equiv> linepath (p+h p) (p+h p)"
+  define n_circ where "n_circ \<equiv> \<lambda>n. ((+++) p_circ ^^ n) p_circ_pt"
+  define cp where "cp \<equiv> if n\<ge>0 then reversepath (n_circ (nat n)) else n_circ (nat (- n))"
+  have n_circ:"valid_path (n_circ k)"
+      "winding_number (n_circ k) p = k"
+      "pathstart (n_circ k) = p + h p" "pathfinish (n_circ k) = p + h p"
+      "path_image (n_circ k) =  (if k=0 then {p + h p} else sphere p (h p))"
+      "p \<notin> path_image (n_circ k)"
+      "\<And>p'. p'\<notin>s - pts \<Longrightarrow> winding_number (n_circ k) p'=0 \<and> p'\<notin>path_image (n_circ k)"
+      "f contour_integrable_on (n_circ k)"
+      "contour_integral (n_circ k) f = k *  contour_integral p_circ f"
+      for k
+    proof (induct k)
+      case 0
+      show "valid_path (n_circ 0)"
+        and "path_image (n_circ 0) =  (if 0=0 then {p + h p} else sphere p (h p))"
+        and "winding_number (n_circ 0) p = of_nat 0"
+        and "pathstart (n_circ 0) = p + h p"
+        and "pathfinish (n_circ 0) = p + h p"
+        and "p \<notin> path_image (n_circ 0)"
+        unfolding n_circ_def p_circ_pt_def using \<open>h p > 0\<close>
+        by (auto simp add: dist_norm)
+      show "winding_number (n_circ 0) p'=0 \<and> p'\<notin>path_image (n_circ 0)" when "p'\<notin>s- pts" for p'
+        unfolding n_circ_def p_circ_pt_def
+        apply (auto intro!:winding_number_trivial)
+        by (metis Diff_iff pathfinish_in_path_image pg(3) pg(4) subsetCE subset_insertI that)+
+      show "f contour_integrable_on (n_circ 0)"
+        unfolding n_circ_def p_circ_pt_def
+        by (auto intro!:contour_integrable_continuous_linepath simp add:continuous_on_sing)
+      show "contour_integral (n_circ 0) f = of_nat 0  *  contour_integral p_circ f"
+        unfolding n_circ_def p_circ_pt_def by auto
+    next
+      case (Suc k)
+      have n_Suc:"n_circ (Suc k) = p_circ +++ n_circ k" unfolding n_circ_def by auto
+      have pcirc:"p \<notin> path_image p_circ" "valid_path p_circ" "pathfinish p_circ = pathstart (n_circ k)"
+        using Suc(3) unfolding p_circ_def using \<open>h p > 0\<close> by (auto simp add: p_circ_def)
+      have pcirc_image:"path_image p_circ \<subseteq> s - insert p pts"
+        proof -
+          have "path_image p_circ \<subseteq> cball p (h p)" using \<open>0 < h p\<close> p_circ_def by auto
+          then show ?thesis using h_p pcirc(1) by auto
+        qed
+      have pcirc_integrable:"f contour_integrable_on p_circ"
+        by (auto simp add:p_circ_def intro!: pcirc_image[unfolded p_circ_def]
+          contour_integrable_continuous_circlepath holomorphic_on_imp_continuous_on
+          holomorphic_on_subset[OF holo])
+      show "valid_path (n_circ (Suc k))"
+        using valid_path_join[OF pcirc(2) Suc(1) pcirc(3)] unfolding n_circ_def by auto
+      show "path_image (n_circ (Suc k))
+          = (if Suc k = 0 then {p + complex_of_real (h p)} else sphere p (h p))"
+        proof -
+          have "path_image p_circ = sphere p (h p)"
+            unfolding p_circ_def using \<open>0 < h p\<close> by auto
+          then show ?thesis unfolding n_Suc  using Suc.hyps(5)  \<open>h p>0\<close>
+            by (auto simp add:  path_image_join[OF pcirc(3)]  dist_norm)
+        qed
+      then show "p \<notin> path_image (n_circ (Suc k))" using \<open>h p>0\<close> by auto
+      show "winding_number (n_circ (Suc k)) p = of_nat (Suc k)"
+        proof -
+          have "winding_number p_circ p = 1"
+            by (simp add: \<open>h p > 0\<close> p_circ_def winding_number_circlepath_centre)
+          moreover have "p \<notin> path_image (n_circ k)" using Suc(5) \<open>h p>0\<close> by auto
+          then have "winding_number (p_circ +++ n_circ k) p
+              = winding_number p_circ p + winding_number (n_circ k) p"
+            using  valid_path_imp_path Suc.hyps(1) Suc.hyps(2) pcirc
+            apply (intro winding_number_join)
+            by auto
+          ultimately show ?thesis using Suc(2) unfolding n_circ_def
+            by auto
+        qed
+      show "pathstart (n_circ (Suc k)) = p + h p"
+        by (simp add: n_circ_def p_circ_def)
+      show "pathfinish (n_circ (Suc k)) = p + h p"
+        using Suc(4) unfolding n_circ_def by auto
+      show "winding_number (n_circ (Suc k)) p'=0 \<and>  p'\<notin>path_image (n_circ (Suc k))" when "p'\<notin>s-pts" for p'
+        proof -
+          have " p' \<notin> path_image p_circ" using \<open>p \<in> s\<close> h p_circ_def that using pcirc_image by blast
+          moreover have "p' \<notin> path_image (n_circ k)"
+            using Suc.hyps(7) that by blast
+          moreover have "winding_number p_circ p' = 0"
+            proof -
+              have "path_image p_circ \<subseteq> cball p (h p)"
+                using h unfolding p_circ_def using \<open>p \<in> s\<close> by fastforce
+              moreover have "p'\<notin>cball p (h p)" using \<open>p \<in> s\<close> h that "2.hyps"(2) by fastforce
+              ultimately show ?thesis unfolding p_circ_def
+                apply (intro winding_number_zero_outside)
+                by auto
+            qed
+          ultimately show ?thesis
+            unfolding n_Suc
+            apply (subst winding_number_join)
+            by (auto simp: valid_path_imp_path pcirc Suc that not_in_path_image_join Suc.hyps(7)[OF that])
+        qed
+      show "f contour_integrable_on (n_circ (Suc k))"
+        unfolding n_Suc
+        by (rule contour_integrable_joinI[OF pcirc_integrable Suc(8) pcirc(2) Suc(1)])
+      show "contour_integral (n_circ (Suc k)) f = (Suc k) *  contour_integral p_circ f"
+        unfolding n_Suc
+        by (auto simp add:contour_integral_join[OF pcirc_integrable Suc(8) pcirc(2) Suc(1)]
+          Suc(9) algebra_simps)
+    qed
+  have cp[simp]:"pathstart cp = p + h p"  "pathfinish cp = p + h p"
+         "valid_path cp" "path_image cp \<subseteq> s - insert p pts"
+         "winding_number cp p = - n"
+         "\<And>p'. p'\<notin>s - pts \<Longrightarrow> winding_number cp p'=0 \<and> p' \<notin> path_image cp"
+         "f contour_integrable_on cp"
+         "contour_integral cp f = - n * contour_integral p_circ f"
+    proof -
+      show "pathstart cp = p + h p" and "pathfinish cp = p + h p" and "valid_path cp"
+        using n_circ unfolding cp_def by auto
+    next
+      have "sphere p (h p) \<subseteq>  s - insert p pts"
+        using h[rule_format,of p] \<open>insert p pts \<subseteq> s\<close> by force
+      moreover  have "p + complex_of_real (h p) \<in> s - insert p pts"
+        using pg(3) pg(4) by (metis pathfinish_in_path_image subsetCE)
+      ultimately show "path_image cp \<subseteq>  s - insert p pts" unfolding cp_def
+        using n_circ(5)  by auto
+    next
+      show "winding_number cp p = - n"
+        unfolding cp_def using winding_number_reversepath n_circ \<open>h p>0\<close>
+        by (auto simp: valid_path_imp_path)
+    next
+      show "winding_number cp p'=0 \<and> p' \<notin> path_image cp" when "p'\<notin>s - pts" for p'
+        unfolding cp_def
+        apply (auto)
+        apply (subst winding_number_reversepath)
+        by (auto simp add: valid_path_imp_path n_circ(7)[OF that] n_circ(1))
+    next
+      show "f contour_integrable_on cp" unfolding cp_def
+        using contour_integrable_reversepath_eq n_circ(1,8) by auto
+    next
+      show "contour_integral cp f = - n * contour_integral p_circ f"
+        unfolding cp_def using contour_integral_reversepath[OF n_circ(1)] n_circ(9)
+        by auto
+    qed
+  define g' where "g' \<equiv> g +++ pg +++ cp +++ (reversepath pg)"
+  have "contour_integral g' f = (\<Sum>p\<in>pts. winding_number g' p * contour_integral (circlepath p (h p)) f)"
+    proof (rule "2.hyps"(3)[of "s-{p}" "g'",OF _ _ \<open>finite pts\<close> ])
+      show "connected (s - {p} - pts)" using connected by (metis Diff_insert2)
+      show "open (s - {p})" using \<open>open s\<close> by auto
+      show " pts \<subseteq> s - {p}" using \<open>insert p pts \<subseteq> s\<close> \<open> p \<notin> pts\<close>  by blast
+      show "f holomorphic_on s - {p} - pts" using holo \<open>p \<notin> pts\<close> by (metis Diff_insert2)
+      show "valid_path g'"
+        unfolding g'_def cp_def using n_circ valid pg g_loop
+        by (auto intro!:valid_path_join )
+      show "pathfinish g' = pathstart g'"
+        unfolding g'_def cp_def using pg(2) by simp
+      show "path_image g' \<subseteq> s - {p} - pts"
+        proof -
+          define s' where "s' \<equiv> s - {p} - pts"
+          have s':"s' = s-insert p pts " unfolding s'_def by auto
+          then show ?thesis using path_img pg(4) cp(4)
+            unfolding g'_def
+            apply (fold s'_def s')
+            apply (intro subset_path_image_join)
+            by auto
+        qed
+      note path_join_imp[simp]
+      show "\<forall>z. z \<notin> s - {p} \<longrightarrow> winding_number g' z = 0"
+        proof clarify
+          fix z assume z:"z\<notin>s - {p}"
+          have "winding_number (g +++ pg +++ cp +++ reversepath pg) z = winding_number g z
+              + winding_number (pg +++ cp +++ (reversepath pg)) z"
+            proof (rule winding_number_join)
+              show "path g" using \<open>valid_path g\<close> by (simp add: valid_path_imp_path)
+              show "z \<notin> path_image g" using z path_img by auto
+              show "path (pg +++ cp +++ reversepath pg)" using pg(3) cp
+                by (simp add: valid_path_imp_path)
+            next
+              have "path_image (pg +++ cp +++ reversepath pg) \<subseteq> s - insert p pts"
+                using pg(4) cp(4) by (auto simp:subset_path_image_join)
+              then show "z \<notin> path_image (pg +++ cp +++ reversepath pg)" using z by auto
+            next
+              show "pathfinish g = pathstart (pg +++ cp +++ reversepath pg)" using g_loop by auto
+            qed
+          also have "... = winding_number g z + (winding_number pg z
+              + winding_number (cp +++ (reversepath pg)) z)"
+            proof (subst add_left_cancel,rule winding_number_join)
+              show "path pg" and "path (cp +++ reversepath pg)"
+               and "pathfinish pg = pathstart (cp +++ reversepath pg)"
+                by (auto simp add: valid_path_imp_path)
+              show "z \<notin> path_image pg" using pg(4) z by blast
+              show "z \<notin> path_image (cp +++ reversepath pg)" using z
+                by (metis Diff_iff \<open>z \<notin> path_image pg\<close> contra_subsetD cp(4) insertI1
+                  not_in_path_image_join path_image_reversepath singletonD)
+            qed
+          also have "... = winding_number g z + (winding_number pg z
+              + (winding_number cp z + winding_number (reversepath pg) z))"
+            apply (auto intro!:winding_number_join simp: valid_path_imp_path)
+            apply (metis Diff_iff contra_subsetD cp(4) insertI1 singletonD z)
+            by (metis Diff_insert2 Diff_subset contra_subsetD pg(4) z)
+          also have "... = winding_number g z + winding_number cp z"
+            apply (subst winding_number_reversepath)
+            apply (auto simp: valid_path_imp_path)
+            by (metis Diff_iff contra_subsetD insertI1 pg(4) singletonD z)
+          finally have "winding_number g' z = winding_number g z + winding_number cp z"
+            unfolding g'_def .
+          moreover have "winding_number g z + winding_number cp z = 0"
+            using winding z \<open>n=winding_number g p\<close> by auto
+          ultimately show "winding_number g' z = 0" unfolding g'_def by auto
+        qed
+      show "\<forall>pa\<in>s - {p}. 0 < h pa \<and> (\<forall>w\<in>cball pa (h pa). w \<in> s - {p} \<and> (w \<noteq> pa \<longrightarrow> w \<notin> pts))"
+        using h by fastforce
+    qed
+  moreover have "contour_integral g' f = contour_integral g f
+      - winding_number g p * contour_integral p_circ f"
+    proof -
+      have "contour_integral g' f =  contour_integral g f
+        + contour_integral (pg +++ cp +++ reversepath pg) f"
+        unfolding g'_def
+        apply (subst contour_integral_join)
+        by (auto simp add:open_Diff[OF \<open>open s\<close>,OF finite_imp_closed[OF fin]]
+          intro!: contour_integrable_holomorphic_simple[OF holo _ _ path_img]
+          contour_integrable_reversepath)
+      also have "... = contour_integral g f + contour_integral pg f
+          + contour_integral (cp +++ reversepath pg) f"
+        apply (subst contour_integral_join)
+        by (auto simp add:contour_integrable_reversepath)
+      also have "... = contour_integral g f + contour_integral pg f
+          + contour_integral cp f + contour_integral (reversepath pg) f"
+        apply (subst contour_integral_join)
+        by (auto simp add:contour_integrable_reversepath)
+      also have "... = contour_integral g f + contour_integral cp f"
+        using contour_integral_reversepath
+        by (auto simp add:contour_integrable_reversepath)
+      also have "... = contour_integral g f - winding_number g p * contour_integral p_circ f"
+        using \<open>n=winding_number g p\<close> by auto
+      finally show ?thesis .
+    qed
+  moreover have "winding_number g' p' = winding_number g p'" when "p'\<in>pts" for p'
+    proof -
+      have [simp]: "p' \<notin> path_image g" "p' \<notin> path_image pg" "p'\<notin>path_image cp"
+        using "2.prems"(8) that
+        apply blast
+        apply (metis Diff_iff Diff_insert2 contra_subsetD pg(4) that)
+        by (meson DiffD2 cp(4) rev_subsetD subset_insertI that)
+      have "winding_number g' p' = winding_number g p'
+          + winding_number (pg +++ cp +++ reversepath pg) p'" unfolding g'_def
+        apply (subst winding_number_join)
+        apply (simp_all add: valid_path_imp_path)
+        apply (intro not_in_path_image_join)
+        by auto
+      also have "... = winding_number g p' + winding_number pg p'
+          + winding_number (cp +++ reversepath pg) p'"
+        apply (subst winding_number_join)
+        apply (simp_all add: valid_path_imp_path)
+        apply (intro not_in_path_image_join)
+        by auto
+      also have "... = winding_number g p' + winding_number pg p'+ winding_number cp p'
+          + winding_number (reversepath pg) p'"
+        apply (subst winding_number_join)
+        by (simp_all add: valid_path_imp_path)
+      also have "... = winding_number g p' + winding_number cp p'"
+        apply (subst winding_number_reversepath)
+        by (simp_all add: valid_path_imp_path)
+      also have "... = winding_number g p'" using that by auto
+      finally show ?thesis .
+    qed
+  ultimately show ?case unfolding p_circ_def
+    apply (subst (asm) sum.cong[OF refl,
+        of pts _ "\<lambda>p. winding_number g p * contour_integral (circlepath p (h p)) f"])
+    by (auto simp add:sum.insert[OF \<open>finite pts\<close> \<open>p\<notin>pts\<close>] algebra_simps)
+qed
+
+lemma Cauchy_theorem_singularities:
+  assumes "open s" "connected s" "finite pts" and
+          holo:"f holomorphic_on s-pts" and
+          "valid_path g" and
+          loop:"pathfinish g = pathstart g" and
+          "path_image g \<subseteq> s-pts" and
+          homo:"\<forall>z. (z \<notin> s) \<longrightarrow> winding_number g z  = 0" and
+          avoid:"\<forall>p\<in>s. h p>0 \<and> (\<forall>w\<in>cball p (h p). w\<in>s \<and> (w\<noteq>p \<longrightarrow> w \<notin> pts))"
+  shows "contour_integral g f = (\<Sum>p\<in>pts. winding_number g p * contour_integral (circlepath p (h p)) f)"
+    (is "?L=?R")
+proof -
+  define circ where "circ \<equiv> \<lambda>p. winding_number g p * contour_integral (circlepath p (h p)) f"
+  define pts1 where "pts1 \<equiv> pts \<inter> s"
+  define pts2 where "pts2 \<equiv> pts - pts1"
+  have "pts=pts1 \<union> pts2" "pts1 \<inter> pts2 = {}" "pts2 \<inter> s={}" "pts1\<subseteq>s"
+    unfolding pts1_def pts2_def by auto
+  have "contour_integral g f =  (\<Sum>p\<in>pts1. circ p)" unfolding circ_def
+    proof (rule Cauchy_theorem_aux[OF \<open>open s\<close> _ _ \<open>pts1\<subseteq>s\<close> _ \<open>valid_path g\<close> loop _ homo])
+      have "finite pts1" unfolding pts1_def using \<open>finite pts\<close> by auto
+      then show "connected (s - pts1)"
+        using \<open>open s\<close> \<open>connected s\<close> connected_open_delete_finite[of s] by auto
+    next
+      show "finite pts1" using \<open>pts = pts1 \<union> pts2\<close> assms(3) by auto
+      show "f holomorphic_on s - pts1" by (metis Diff_Int2 Int_absorb holo pts1_def)
+      show "path_image g \<subseteq> s - pts1" using assms(7) pts1_def by auto
+      show "\<forall>p\<in>s. 0 < h p \<and> (\<forall>w\<in>cball p (h p). w \<in> s \<and> (w \<noteq> p \<longrightarrow> w \<notin> pts1))"
+        by (simp add: avoid pts1_def)
+    qed
+  moreover have "sum circ pts2=0"
+    proof -
+      have "winding_number g p=0" when "p\<in>pts2" for p
+        using  \<open>pts2 \<inter> s={}\<close> that homo[rule_format,of p] by auto
+      thus ?thesis unfolding circ_def
+        apply (intro sum.neutral)
+        by auto
+    qed
+  moreover have "?R=sum circ pts1 + sum circ pts2"
+    unfolding circ_def
+    using sum.union_disjoint[OF _ _ \<open>pts1 \<inter> pts2 = {}\<close>] \<open>finite pts\<close> \<open>pts=pts1 \<union> pts2\<close>
+    by blast
+  ultimately show ?thesis
+    apply (fold circ_def)
+    by auto
+qed
+
+theorem Residue_theorem:
+  fixes s pts::"complex set" and f::"complex \<Rightarrow> complex"
+    and g::"real \<Rightarrow> complex"
+  assumes "open s" "connected s" "finite pts" and
+          holo:"f holomorphic_on s-pts" and
+          "valid_path g" and
+          loop:"pathfinish g = pathstart g" and
+          "path_image g \<subseteq> s-pts" and
+          homo:"\<forall>z. (z \<notin> s) \<longrightarrow> winding_number g z  = 0"
+  shows "contour_integral g f = 2 * pi * \<i> *(\<Sum>p\<in>pts. winding_number g p * residue f p)"
+proof -
+  define c where "c \<equiv>  2 * pi * \<i>"
+  obtain h where avoid:"\<forall>p\<in>s. h p>0 \<and> (\<forall>w\<in>cball p (h p). w\<in>s \<and> (w\<noteq>p \<longrightarrow> w \<notin> pts))"
+    using finite_cball_avoid[OF \<open>open s\<close> \<open>finite pts\<close>] by metis
+  have "contour_integral g f
+      = (\<Sum>p\<in>pts. winding_number g p * contour_integral (circlepath p (h p)) f)"
+    using Cauchy_theorem_singularities[OF assms avoid] .
+  also have "... = (\<Sum>p\<in>pts.  c * winding_number g p * residue f p)"
+    proof (intro sum.cong)
+      show "pts = pts" by simp
+    next
+      fix x assume "x \<in> pts"
+      show "winding_number g x * contour_integral (circlepath x (h x)) f
+          = c * winding_number g x * residue f x"
+        proof (cases "x\<in>s")
+          case False
+          then have "winding_number g x=0" using homo by auto
+          thus ?thesis by auto
+        next
+          case True
+          have "contour_integral (circlepath x (h x)) f = c* residue f x"
+            using \<open>x\<in>pts\<close> \<open>finite pts\<close> avoid[rule_format,OF True]
+            apply (intro base_residue[of "s-(pts-{x})",THEN contour_integral_unique,folded c_def])
+            by (auto intro:holomorphic_on_subset[OF holo] open_Diff[OF \<open>open s\<close> finite_imp_closed])
+          then show ?thesis by auto
+        qed
+    qed
+  also have "... = c * (\<Sum>p\<in>pts. winding_number g p * residue f p)"
+    by (simp add: sum_distrib_left algebra_simps)
+  finally show ?thesis unfolding c_def .
+qed
+
+subsection \<open>Non-essential singular points\<close>
+
+definition\<^marker>\<open>tag important\<close> is_pole ::
+  "('a::topological_space \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a \<Rightarrow> bool" where
+  "is_pole f a =  (LIM x (at a). f x :> at_infinity)"
+
+lemma is_pole_cong:
+  assumes "eventually (\<lambda>x. f x = g x) (at a)" "a=b"
+  shows "is_pole f a \<longleftrightarrow> is_pole g b"
+  unfolding is_pole_def using assms by (intro filterlim_cong,auto)
+
+lemma is_pole_transform:
+  assumes "is_pole f a" "eventually (\<lambda>x. f x = g x) (at a)" "a=b"
+  shows "is_pole g b"
+  using is_pole_cong assms by auto
+
+lemma is_pole_tendsto:
+  fixes f::"('a::topological_space \<Rightarrow> 'b::real_normed_div_algebra)"
+  shows "is_pole f x \<Longrightarrow> ((inverse o f) \<longlongrightarrow> 0) (at x)"
+unfolding is_pole_def
+by (auto simp add:filterlim_inverse_at_iff[symmetric] comp_def filterlim_at)
+
+lemma is_pole_inverse_holomorphic:
+  assumes "open s"
+    and f_holo:"f holomorphic_on (s-{z})"
+    and pole:"is_pole f z"
+    and non_z:"\<forall>x\<in>s-{z}. f x\<noteq>0"
+  shows "(\<lambda>x. if x=z then 0 else inverse (f x)) holomorphic_on s"
+proof -
+  define g where "g \<equiv> \<lambda>x. if x=z then 0 else inverse (f x)"
+  have "isCont g z" unfolding isCont_def  using is_pole_tendsto[OF pole]
+    apply (subst Lim_cong_at[where b=z and y=0 and g="inverse \<circ> f"])
+    by (simp_all add:g_def)
+  moreover have "continuous_on (s-{z}) f" using f_holo holomorphic_on_imp_continuous_on by auto
+  hence "continuous_on (s-{z}) (inverse o f)" unfolding comp_def
+    by (auto elim!:continuous_on_inverse simp add:non_z)
+  hence "continuous_on (s-{z}) g" unfolding g_def
+    apply (subst continuous_on_cong[where t="s-{z}" and g="inverse o f"])
+    by auto
+  ultimately have "continuous_on s g" using open_delete[OF \<open>open s\<close>] \<open>open s\<close>
+    by (auto simp add:continuous_on_eq_continuous_at)
+  moreover have "(inverse o f) holomorphic_on (s-{z})"
+    unfolding comp_def using f_holo
+    by (auto elim!:holomorphic_on_inverse simp add:non_z)
+  hence "g holomorphic_on (s-{z})"
+    apply (subst holomorphic_cong[where t="s-{z}" and g="inverse o f"])
+    by (auto simp add:g_def)
+  ultimately show ?thesis unfolding g_def using \<open>open s\<close>
+    by (auto elim!: no_isolated_singularity)
+qed
+
+lemma not_is_pole_holomorphic:
+  assumes "open A" "x \<in> A" "f holomorphic_on A"
+  shows   "\<not>is_pole f x"
+proof -
+  have "continuous_on A f" by (intro holomorphic_on_imp_continuous_on) fact
+  with assms have "isCont f x" by (simp add: continuous_on_eq_continuous_at)
+  hence "f \<midarrow>x\<rightarrow> f x" by (simp add: isCont_def)
+  thus "\<not>is_pole f x" unfolding is_pole_def
+    using not_tendsto_and_filterlim_at_infinity[of "at x" f "f x"] by auto
+qed
+
+lemma is_pole_inverse_power: "n > 0 \<Longrightarrow> is_pole (\<lambda>z::complex. 1 / (z - a) ^ n) a"
+  unfolding is_pole_def inverse_eq_divide [symmetric]
+  by (intro filterlim_compose[OF filterlim_inverse_at_infinity] tendsto_intros)
+     (auto simp: filterlim_at eventually_at intro!: exI[of _ 1] tendsto_eq_intros)
+
+lemma is_pole_inverse: "is_pole (\<lambda>z::complex. 1 / (z - a)) a"
+  using is_pole_inverse_power[of 1 a] by simp
+
+lemma is_pole_divide:
+  fixes f :: "'a :: t2_space \<Rightarrow> 'b :: real_normed_field"
+  assumes "isCont f z" "filterlim g (at 0) (at z)" "f z \<noteq> 0"
+  shows   "is_pole (\<lambda>z. f z / g z) z"
+proof -
+  have "filterlim (\<lambda>z. f z * inverse (g z)) at_infinity (at z)"
+    by (intro tendsto_mult_filterlim_at_infinity[of _ "f z"]
+                 filterlim_compose[OF filterlim_inverse_at_infinity])+
+       (insert assms, auto simp: isCont_def)
+  thus ?thesis by (simp add: field_split_simps is_pole_def)
+qed
+
+lemma is_pole_basic:
+  assumes "f holomorphic_on A" "open A" "z \<in> A" "f z \<noteq> 0" "n > 0"
+  shows   "is_pole (\<lambda>w. f w / (w - z) ^ n) z"
+proof (rule is_pole_divide)
+  have "continuous_on A f" by (rule holomorphic_on_imp_continuous_on) fact
+  with assms show "isCont f z" by (auto simp: continuous_on_eq_continuous_at)
+  have "filterlim (\<lambda>w. (w - z) ^ n) (nhds 0) (at z)"
+    using assms by (auto intro!: tendsto_eq_intros)
+  thus "filterlim (\<lambda>w. (w - z) ^ n) (at 0) (at z)"
+    by (intro filterlim_atI tendsto_eq_intros)
+       (insert assms, auto simp: eventually_at_filter)
+qed fact+
+
+lemma is_pole_basic':
+  assumes "f holomorphic_on A" "open A" "0 \<in> A" "f 0 \<noteq> 0" "n > 0"
+  shows   "is_pole (\<lambda>w. f w / w ^ n) 0"
+  using is_pole_basic[of f A 0] assms by simp
+
+text \<open>The proposition
+              \<^term>\<open>\<exists>x. ((f::complex\<Rightarrow>complex) \<longlongrightarrow> x) (at z) \<or> is_pole f z\<close>
+can be interpreted as the complex function \<^term>\<open>f\<close> has a non-essential singularity at \<^term>\<open>z\<close>
+(i.e. the singularity is either removable or a pole).\<close>
+definition not_essential::"[complex \<Rightarrow> complex, complex] \<Rightarrow> bool" where
+  "not_essential f z = (\<exists>x. f\<midarrow>z\<rightarrow>x \<or> is_pole f z)"
+
+definition isolated_singularity_at::"[complex \<Rightarrow> complex, complex] \<Rightarrow> bool" where
+  "isolated_singularity_at f z = (\<exists>r>0. f analytic_on ball z r-{z})"
+
+named_theorems singularity_intros "introduction rules for singularities"
+
+lemma holomorphic_factor_unique:
+  fixes f::"complex \<Rightarrow> complex" and z::complex and r::real and m n::int
+  assumes "r>0" "g z\<noteq>0" "h z\<noteq>0"
+    and asm:"\<forall>w\<in>ball z r-{z}. f w = g w * (w-z) powr n \<and> g w\<noteq>0 \<and> f w =  h w * (w - z) powr m \<and> h w\<noteq>0"
+    and g_holo:"g holomorphic_on ball z r" and h_holo:"h holomorphic_on ball z r"
+  shows "n=m"
+proof -
+  have [simp]:"at z within ball z r \<noteq> bot" using \<open>r>0\<close>
+      by (auto simp add:at_within_ball_bot_iff)
+  have False when "n>m"
+  proof -
+    have "(h \<longlongrightarrow> 0) (at z within ball z r)"
+    proof (rule Lim_transform_within[OF _ \<open>r>0\<close>, where f="\<lambda>w. (w - z) powr (n - m) * g w"])
+      have "\<forall>w\<in>ball z r-{z}. h w = (w-z)powr(n-m) * g w"
+        using \<open>n>m\<close> asm \<open>r>0\<close>
+        apply (auto simp add:field_simps powr_diff)
+        by force
+      then show "\<lbrakk>x' \<in> ball z r; 0 < dist x' z;dist x' z < r\<rbrakk>
+            \<Longrightarrow> (x' - z) powr (n - m) * g x' = h x'" for x' by auto
+    next
+      define F where "F \<equiv> at z within ball z r"
+      define f' where "f' \<equiv> \<lambda>x. (x - z) powr (n-m)"
+      have "f' z=0" using \<open>n>m\<close> unfolding f'_def by auto
+      moreover have "continuous F f'" unfolding f'_def F_def continuous_def
+        apply (subst Lim_ident_at)
+        using \<open>n>m\<close> by (auto intro!:tendsto_powr_complex_0 tendsto_eq_intros)
+      ultimately have "(f' \<longlongrightarrow> 0) F" unfolding F_def
+        by (simp add: continuous_within)
+      moreover have "(g \<longlongrightarrow> g z) F"
+        using holomorphic_on_imp_continuous_on[OF g_holo,unfolded continuous_on_def] \<open>r>0\<close>
+        unfolding F_def by auto
+      ultimately show " ((\<lambda>w. f' w * g w) \<longlongrightarrow> 0) F" using tendsto_mult by fastforce
+    qed
+    moreover have "(h \<longlongrightarrow> h z) (at z within ball z r)"
+      using holomorphic_on_imp_continuous_on[OF h_holo]
+      by (auto simp add:continuous_on_def \<open>r>0\<close>)
+    ultimately have "h z=0" by (auto intro!: tendsto_unique)
+    thus False using \<open>h z\<noteq>0\<close> by auto
+  qed
+  moreover have False when "m>n"
+  proof -
+    have "(g \<longlongrightarrow> 0) (at z within ball z r)"
+    proof (rule Lim_transform_within[OF _ \<open>r>0\<close>, where f="\<lambda>w. (w - z) powr (m - n) * h w"])
+      have "\<forall>w\<in>ball z r -{z}. g w = (w-z) powr (m-n) * h w" using \<open>m>n\<close> asm
+        apply (auto simp add:field_simps powr_diff)
+        by force
+      then show "\<lbrakk>x' \<in> ball z r; 0 < dist x' z;dist x' z < r\<rbrakk>
+            \<Longrightarrow> (x' - z) powr (m - n) * h x' = g x'" for x' by auto
+    next
+      define F where "F \<equiv> at z within ball z r"
+      define f' where "f' \<equiv>\<lambda>x. (x - z) powr (m-n)"
+      have "f' z=0" using \<open>m>n\<close> unfolding f'_def by auto
+      moreover have "continuous F f'" unfolding f'_def F_def continuous_def
+        apply (subst Lim_ident_at)
+        using \<open>m>n\<close> by (auto intro!:tendsto_powr_complex_0 tendsto_eq_intros)
+      ultimately have "(f' \<longlongrightarrow> 0) F" unfolding F_def
+        by (simp add: continuous_within)
+      moreover have "(h \<longlongrightarrow> h z) F"
+        using holomorphic_on_imp_continuous_on[OF h_holo,unfolded continuous_on_def] \<open>r>0\<close>
+        unfolding F_def by auto
+      ultimately show " ((\<lambda>w. f' w * h w) \<longlongrightarrow> 0) F" using tendsto_mult by fastforce
+    qed
+    moreover have "(g \<longlongrightarrow> g z) (at z within ball z r)"
+      using holomorphic_on_imp_continuous_on[OF g_holo]
+      by (auto simp add:continuous_on_def \<open>r>0\<close>)
+    ultimately have "g z=0" by (auto intro!: tendsto_unique)
+    thus False using \<open>g z\<noteq>0\<close> by auto
+  qed
+  ultimately show "n=m" by fastforce
+qed
+
+lemma holomorphic_factor_puncture:
+  assumes f_iso:"isolated_singularity_at f z"
+      and "not_essential f z" \<comment> \<open>\<^term>\<open>f\<close> has either a removable singularity or a pole at \<^term>\<open>z\<close>\<close>
+      and non_zero:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0" \<comment> \<open>\<^term>\<open>f\<close> will not be constantly zero in a neighbour of \<^term>\<open>z\<close>\<close>
+  shows "\<exists>!n::int. \<exists>g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z\<noteq>0
+          \<and> (\<forall>w\<in>cball z r-{z}. f w = g w * (w-z) powr n \<and> g w\<noteq>0)"
+proof -
+  define P where "P = (\<lambda>f n g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z\<noteq>0
+          \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w-z) powr (of_int n)  \<and> g w\<noteq>0))"
+  have imp_unique:"\<exists>!n::int. \<exists>g r. P f n g r" when "\<exists>n g r. P f n g r"
+  proof (rule ex_ex1I[OF that])
+    fix n1 n2 :: int
+    assume g1_asm:"\<exists>g1 r1. P f n1 g1 r1" and g2_asm:"\<exists>g2 r2. P f n2 g2 r2"
+    define fac where "fac \<equiv> \<lambda>n g r. \<forall>w\<in>cball z r-{z}. f w = g w * (w - z) powr (of_int n) \<and> g w \<noteq> 0"
+    obtain g1 r1 where "0 < r1" and g1_holo: "g1 holomorphic_on cball z r1" and "g1 z\<noteq>0"
+        and "fac n1 g1 r1" using g1_asm unfolding P_def fac_def by auto
+    obtain g2 r2 where "0 < r2" and g2_holo: "g2 holomorphic_on cball z r2" and "g2 z\<noteq>0"
+        and "fac n2 g2 r2" using g2_asm unfolding P_def fac_def by auto
+    define r where "r \<equiv> min r1 r2"
+    have "r>0" using \<open>r1>0\<close> \<open>r2>0\<close> unfolding r_def by auto
+    moreover have "\<forall>w\<in>ball z r-{z}. f w = g1 w * (w-z) powr n1 \<and> g1 w\<noteq>0
+        \<and> f w = g2 w * (w - z) powr n2  \<and> g2 w\<noteq>0"
+      using \<open>fac n1 g1 r1\<close> \<open>fac n2 g2 r2\<close>   unfolding fac_def r_def
+      by fastforce
+    ultimately show "n1=n2" using g1_holo g2_holo \<open>g1 z\<noteq>0\<close> \<open>g2 z\<noteq>0\<close>
+      apply (elim holomorphic_factor_unique)
+      by (auto simp add:r_def)
+  qed
+
+  have P_exist:"\<exists> n g r. P h n g r" when
+      "\<exists>z'. (h \<longlongrightarrow> z') (at z)" "isolated_singularity_at h z"  "\<exists>\<^sub>Fw in (at z). h w\<noteq>0"
+    for h
+  proof -
+    from that(2) obtain r where "r>0" "h analytic_on ball z r - {z}"
+      unfolding isolated_singularity_at_def by auto
+    obtain z' where "(h \<longlongrightarrow> z') (at z)" using \<open>\<exists>z'. (h \<longlongrightarrow> z') (at z)\<close> by auto
+    define h' where "h'=(\<lambda>x. if x=z then z' else h x)"
+    have "h' holomorphic_on ball z r"
+      apply (rule no_isolated_singularity'[of "{z}"])
+      subgoal by (metis LIM_equal Lim_at_imp_Lim_at_within \<open>h \<midarrow>z\<rightarrow> z'\<close> empty_iff h'_def insert_iff)
+      subgoal using \<open>h analytic_on ball z r - {z}\<close> analytic_imp_holomorphic h'_def holomorphic_transform
+        by fastforce
+      by auto
+    have ?thesis when "z'=0"
+    proof -
+      have "h' z=0" using that unfolding h'_def by auto
+      moreover have "\<not> h' constant_on ball z r"
+        using \<open>\<exists>\<^sub>Fw in (at z). h w\<noteq>0\<close> unfolding constant_on_def frequently_def eventually_at h'_def
+        apply simp
+        by (metis \<open>0 < r\<close> centre_in_ball dist_commute mem_ball that)
+      moreover note \<open>h' holomorphic_on ball z r\<close>
+      ultimately obtain g r1 n where "0 < n" "0 < r1" "ball z r1 \<subseteq> ball z r" and
+          g:"g holomorphic_on ball z r1"
+          "\<And>w. w \<in> ball z r1 \<Longrightarrow> h' w = (w - z) ^ n * g w"
+          "\<And>w. w \<in> ball z r1 \<Longrightarrow> g w \<noteq> 0"
+        using holomorphic_factor_zero_nonconstant[of _ "ball z r" z thesis,simplified,
+                OF \<open>h' holomorphic_on ball z r\<close> \<open>r>0\<close> \<open>h' z=0\<close> \<open>\<not> h' constant_on ball z r\<close>]
+        by (auto simp add:dist_commute)
+      define rr where "rr=r1/2"
+      have "P h' n g rr"
+        unfolding P_def rr_def
+        using \<open>n>0\<close> \<open>r1>0\<close> g by (auto simp add:powr_nat)
+      then have "P h n g rr"
+        unfolding h'_def P_def by auto
+      then show ?thesis unfolding P_def by blast
+    qed
+    moreover have ?thesis when "z'\<noteq>0"
+    proof -
+      have "h' z\<noteq>0" using that unfolding h'_def by auto
+      obtain r1 where "r1>0" "cball z r1 \<subseteq> ball z r" "\<forall>x\<in>cball z r1. h' x\<noteq>0"
+      proof -
+        have "isCont h' z" "h' z\<noteq>0"
+          by (auto simp add: Lim_cong_within \<open>h \<midarrow>z\<rightarrow> z'\<close> \<open>z'\<noteq>0\<close> continuous_at h'_def)
+        then obtain r2 where r2:"r2>0" "\<forall>x\<in>ball z r2. h' x\<noteq>0"
+          using continuous_at_avoid[of z h' 0 ] unfolding ball_def by auto
+        define r1 where "r1=min r2 r / 2"
+        have "0 < r1" "cball z r1 \<subseteq> ball z r"
+          using \<open>r2>0\<close> \<open>r>0\<close> unfolding r1_def by auto
+        moreover have "\<forall>x\<in>cball z r1. h' x \<noteq> 0"
+          using r2 unfolding r1_def by simp
+        ultimately show ?thesis using that by auto
+      qed
+      then have "P h' 0 h' r1" using \<open>h' holomorphic_on ball z r\<close> unfolding P_def by auto
+      then have "P h 0 h' r1" unfolding P_def h'_def by auto
+      then show ?thesis unfolding P_def by blast
+    qed
+    ultimately show ?thesis by auto
+  qed
+
+  have ?thesis when "\<exists>x. (f \<longlongrightarrow> x) (at z)"
+    apply (rule_tac imp_unique[unfolded P_def])
+    using P_exist[OF that(1) f_iso non_zero] unfolding P_def .
+  moreover have ?thesis when "is_pole f z"
+  proof (rule imp_unique[unfolded P_def])
+    obtain e where [simp]:"e>0" and e_holo:"f holomorphic_on ball z e - {z}" and e_nz: "\<forall>x\<in>ball z e-{z}. f x\<noteq>0"
+    proof -
+      have "\<forall>\<^sub>F z in at z. f z \<noteq> 0"
+        using \<open>is_pole f z\<close> filterlim_at_infinity_imp_eventually_ne unfolding is_pole_def
+        by auto
+      then obtain e1 where e1:"e1>0" "\<forall>x\<in>ball z e1-{z}. f x\<noteq>0"
+        using that eventually_at[of "\<lambda>x. f x\<noteq>0" z UNIV,simplified] by (auto simp add:dist_commute)
+      obtain e2 where e2:"e2>0" "f holomorphic_on ball z e2 - {z}"
+        using f_iso analytic_imp_holomorphic unfolding isolated_singularity_at_def by auto
+      define e where "e=min e1 e2"
+      show ?thesis
+        apply (rule that[of e])
+        using  e1 e2 unfolding e_def by auto
+    qed
+
+    define h where "h \<equiv> \<lambda>x. inverse (f x)"
+
+    have "\<exists>n g r. P h n g r"
+    proof -
+      have "h \<midarrow>z\<rightarrow> 0"
+        using Lim_transform_within_open assms(2) h_def is_pole_tendsto that by fastforce
+      moreover have "\<exists>\<^sub>Fw in (at z). h w\<noteq>0"
+        using non_zero
+        apply (elim frequently_rev_mp)
+        unfolding h_def eventually_at by (auto intro:exI[where x=1])
+      moreover have "isolated_singularity_at h z"
+        unfolding isolated_singularity_at_def h_def
+        apply (rule exI[where x=e])
+        using e_holo e_nz \<open>e>0\<close> by (metis open_ball analytic_on_open
+            holomorphic_on_inverse open_delete)
+      ultimately show ?thesis
+        using P_exist[of h] by auto
+    qed
+    then obtain n g r
+      where "0 < r" and
+            g_holo:"g holomorphic_on cball z r" and "g z\<noteq>0" and
+            g_fac:"(\<forall>w\<in>cball z r-{z}. h w = g w * (w - z) powr of_int n  \<and> g w \<noteq> 0)"
+      unfolding P_def by auto
+    have "P f (-n) (inverse o g) r"
+    proof -
+      have "f w = inverse (g w) * (w - z) powr of_int (- n)" when "w\<in>cball z r - {z}" for w
+        using g_fac[rule_format,of w] that unfolding h_def
+        apply (auto simp add:powr_minus )
+        by (metis inverse_inverse_eq inverse_mult_distrib)
+      then show ?thesis
+        unfolding P_def comp_def
+        using \<open>r>0\<close> g_holo g_fac \<open>g z\<noteq>0\<close> by (auto intro:holomorphic_intros)
+    qed
+    then show "\<exists>x g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z \<noteq> 0
+                  \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int x  \<and> g w \<noteq> 0)"
+      unfolding P_def by blast
+  qed
+  ultimately show ?thesis using \<open>not_essential f z\<close> unfolding not_essential_def  by presburger
+qed
+
+lemma not_essential_transform:
+  assumes "not_essential g z"
+  assumes "\<forall>\<^sub>F w in (at z). g w = f w"
+  shows "not_essential f z"
+  using assms unfolding not_essential_def
+  by (simp add: filterlim_cong is_pole_cong)
+
+lemma isolated_singularity_at_transform:
+  assumes "isolated_singularity_at g z"
+  assumes "\<forall>\<^sub>F w in (at z). g w = f w"
+  shows "isolated_singularity_at f z"
+proof -
+  obtain r1 where "r1>0" and r1:"g analytic_on ball z r1 - {z}"
+    using assms(1) unfolding isolated_singularity_at_def by auto
+  obtain r2 where "r2>0" and r2:" \<forall>x. x \<noteq> z \<and> dist x z < r2 \<longrightarrow> g x = f x"
+    using assms(2) unfolding eventually_at by auto
+  define r3 where "r3=min r1 r2"
+  have "r3>0" unfolding r3_def using \<open>r1>0\<close> \<open>r2>0\<close> by auto
+  moreover have "f analytic_on ball z r3 - {z}"
+  proof -
+    have "g holomorphic_on ball z r3 - {z}"
+      using r1 unfolding r3_def by (subst (asm) analytic_on_open,auto)
+    then have "f holomorphic_on ball z r3 - {z}"
+      using r2 unfolding r3_def
+      by (auto simp add:dist_commute elim!:holomorphic_transform)
+    then show ?thesis by (subst analytic_on_open,auto)
+  qed
+  ultimately show ?thesis unfolding isolated_singularity_at_def by auto
+qed
+
+lemma not_essential_powr[singularity_intros]:
+  assumes "LIM w (at z). f w :> (at x)"
+  shows "not_essential (\<lambda>w. (f w) powr (of_int n)) z"
+proof -
+  define fp where "fp=(\<lambda>w. (f w) powr (of_int n))"
+  have ?thesis when "n>0"
+  proof -
+    have "(\<lambda>w.  (f w) ^ (nat n)) \<midarrow>z\<rightarrow> x ^ nat n"
+      using that assms unfolding filterlim_at by (auto intro!:tendsto_eq_intros)
+    then have "fp \<midarrow>z\<rightarrow> x ^ nat n" unfolding fp_def
+      apply (elim Lim_transform_within[where d=1],simp)
+      by (metis less_le powr_0 powr_of_int that zero_less_nat_eq zero_power)
+    then show ?thesis unfolding not_essential_def fp_def by auto
+  qed
+  moreover have ?thesis when "n=0"
+  proof -
+    have "fp \<midarrow>z\<rightarrow> 1 "
+      apply (subst tendsto_cong[where g="\<lambda>_.1"])
+      using that filterlim_at_within_not_equal[OF assms,of 0] unfolding fp_def by auto
+    then show ?thesis unfolding fp_def not_essential_def by auto
+  qed
+  moreover have ?thesis when "n<0"
+  proof (cases "x=0")
+    case True
+    have "LIM w (at z). inverse ((f w) ^ (nat (-n))) :> at_infinity"
+      apply (subst filterlim_inverse_at_iff[symmetric],simp)
+      apply (rule filterlim_atI)
+      subgoal using assms True that unfolding filterlim_at by (auto intro!:tendsto_eq_intros)
+      subgoal using filterlim_at_within_not_equal[OF assms,of 0]
+        by (eventually_elim,insert that,auto)
+      done
+    then have "LIM w (at z). fp w :> at_infinity"
+    proof (elim filterlim_mono_eventually)
+      show "\<forall>\<^sub>F x in at z. inverse (f x ^ nat (- n)) = fp x"
+        using filterlim_at_within_not_equal[OF assms,of 0] unfolding fp_def
+        apply eventually_elim
+        using powr_of_int that by auto
+    qed auto
+    then show ?thesis unfolding fp_def not_essential_def is_pole_def by auto
+  next
+    case False
+    let ?xx= "inverse (x ^ (nat (-n)))"
+    have "(\<lambda>w. inverse ((f w) ^ (nat (-n)))) \<midarrow>z\<rightarrow>?xx"
+      using assms False unfolding filterlim_at by (auto intro!:tendsto_eq_intros)
+    then have "fp \<midarrow>z\<rightarrow>?xx"
+      apply (elim Lim_transform_within[where d=1],simp)
+      unfolding fp_def by (metis inverse_zero nat_mono_iff nat_zero_as_int neg_0_less_iff_less
+          not_le power_eq_0_iff powr_0 powr_of_int that)
+    then show ?thesis unfolding fp_def not_essential_def by auto
+  qed
+  ultimately show ?thesis by linarith
+qed
+
+lemma isolated_singularity_at_powr[singularity_intros]:
+  assumes "isolated_singularity_at f z" "\<forall>\<^sub>F w in (at z). f w\<noteq>0"
+  shows "isolated_singularity_at (\<lambda>w. (f w) powr (of_int n)) z"
+proof -
+  obtain r1 where "r1>0" "f analytic_on ball z r1 - {z}"
+    using assms(1) unfolding isolated_singularity_at_def by auto
+  then have r1:"f holomorphic_on ball z r1 - {z}"
+    using analytic_on_open[of "ball z r1-{z}" f] by blast
+  obtain r2 where "r2>0" and r2:"\<forall>w. w \<noteq> z \<and> dist w z < r2 \<longrightarrow> f w \<noteq> 0"
+    using assms(2) unfolding eventually_at by auto
+  define r3 where "r3=min r1 r2"
+  have "(\<lambda>w. (f w) powr of_int n) holomorphic_on ball z r3 - {z}"
+    apply (rule holomorphic_on_powr_of_int)
+    subgoal unfolding r3_def using r1 by auto
+    subgoal unfolding r3_def using r2 by (auto simp add:dist_commute)
+    done
+  moreover have "r3>0" unfolding r3_def using \<open>0 < r1\<close> \<open>0 < r2\<close> by linarith
+  ultimately show ?thesis unfolding isolated_singularity_at_def
+    apply (subst (asm) analytic_on_open[symmetric])
+    by auto
+qed
+
+lemma non_zero_neighbour:
+  assumes f_iso:"isolated_singularity_at f z"
+      and f_ness:"not_essential f z"
+      and f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
+    shows "\<forall>\<^sub>F w in (at z). f w\<noteq>0"
+proof -
+  obtain fn fp fr where [simp]:"fp z \<noteq> 0" and "fr > 0"
+          and fr: "fp holomorphic_on cball z fr"
+                  "\<forall>w\<in>cball z fr - {z}. f w = fp w * (w - z) powr of_int fn \<and> fp w \<noteq> 0"
+    using holomorphic_factor_puncture[OF f_iso f_ness f_nconst,THEN ex1_implies_ex] by auto
+  have "f w \<noteq> 0" when " w \<noteq> z" "dist w z < fr" for w
+  proof -
+    have "f w = fp w * (w - z) powr of_int fn" "fp w \<noteq> 0"
+      using fr(2)[rule_format, of w] using that by (auto simp add:dist_commute)
+    moreover have "(w - z) powr of_int fn \<noteq>0"
+      unfolding powr_eq_0_iff using \<open>w\<noteq>z\<close> by auto
+    ultimately show ?thesis by auto
+  qed
+  then show ?thesis using \<open>fr>0\<close> unfolding eventually_at by auto
+qed
+
+lemma non_zero_neighbour_pole:
+  assumes "is_pole f z"
+  shows "\<forall>\<^sub>F w in (at z). f w\<noteq>0"
+  using assms filterlim_at_infinity_imp_eventually_ne[of f "at z" 0]
+  unfolding is_pole_def by auto
+
+lemma non_zero_neighbour_alt:
+  assumes holo: "f holomorphic_on S"
+      and "open S" "connected S" "z \<in> S"  "\<beta> \<in> S" "f \<beta> \<noteq> 0"
+    shows "\<forall>\<^sub>F w in (at z). f w\<noteq>0 \<and> w\<in>S"
+proof (cases "f z = 0")
+  case True
+  from isolated_zeros[OF holo \<open>open S\<close> \<open>connected S\<close> \<open>z \<in> S\<close> True \<open>\<beta> \<in> S\<close> \<open>f \<beta> \<noteq> 0\<close>]
+  obtain r where "0 < r" "ball z r \<subseteq> S" "\<forall>w \<in> ball z r - {z}.f w \<noteq> 0" by metis
+  then show ?thesis unfolding eventually_at
+    apply (rule_tac x=r in exI)
+    by (auto simp add:dist_commute)
+next
+  case False
+  obtain r1 where r1:"r1>0" "\<forall>y. dist z y < r1 \<longrightarrow> f y \<noteq> 0"
+    using continuous_at_avoid[of z f, OF _ False] assms(2,4) continuous_on_eq_continuous_at
+      holo holomorphic_on_imp_continuous_on by blast
+  obtain r2 where r2:"r2>0" "ball z r2 \<subseteq> S"
+    using assms(2) assms(4) openE by blast
+  show ?thesis unfolding eventually_at
+    apply (rule_tac x="min r1 r2" in exI)
+    using r1 r2 by (auto simp add:dist_commute)
+qed
+
+lemma not_essential_times[singularity_intros]:
+  assumes f_ness:"not_essential f z" and g_ness:"not_essential g z"
+  assumes f_iso:"isolated_singularity_at f z" and g_iso:"isolated_singularity_at g z"
+  shows "not_essential (\<lambda>w. f w * g w) z"
+proof -
+  define fg where "fg = (\<lambda>w. f w * g w)"
+  have ?thesis when "\<not> ((\<exists>\<^sub>Fw in (at z). f w\<noteq>0) \<and> (\<exists>\<^sub>Fw in (at z). g w\<noteq>0))"
+  proof -
+    have "\<forall>\<^sub>Fw in (at z). fg w=0"
+      using that[unfolded frequently_def, simplified] unfolding fg_def
+      by (auto elim: eventually_rev_mp)
+    from tendsto_cong[OF this] have "fg \<midarrow>z\<rightarrow>0" by auto
+    then show ?thesis unfolding not_essential_def fg_def by auto
+  qed
+  moreover have ?thesis when f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0" and g_nconst:"\<exists>\<^sub>Fw in (at z). g w\<noteq>0"
+  proof -
+    obtain fn fp fr where [simp]:"fp z \<noteq> 0" and "fr > 0"
+          and fr: "fp holomorphic_on cball z fr"
+                  "\<forall>w\<in>cball z fr - {z}. f w = fp w * (w - z) powr of_int fn \<and> fp w \<noteq> 0"
+      using holomorphic_factor_puncture[OF f_iso f_ness f_nconst,THEN ex1_implies_ex] by auto
+    obtain gn gp gr where [simp]:"gp z \<noteq> 0" and "gr > 0"
+          and gr: "gp holomorphic_on cball z gr"
+                  "\<forall>w\<in>cball z gr - {z}. g w = gp w * (w - z) powr of_int gn \<and> gp w \<noteq> 0"
+      using holomorphic_factor_puncture[OF g_iso g_ness g_nconst,THEN ex1_implies_ex] by auto
+
+    define r1 where "r1=(min fr gr)"
+    have "r1>0" unfolding r1_def using  \<open>fr>0\<close> \<open>gr>0\<close> by auto
+    have fg_times:"fg w = (fp w * gp w) * (w - z) powr (of_int (fn+gn))" and fgp_nz:"fp w*gp w\<noteq>0"
+      when "w\<in>ball z r1 - {z}" for w
+    proof -
+      have "f w = fp w * (w - z) powr of_int fn" "fp w\<noteq>0"
+        using fr(2)[rule_format,of w] that unfolding r1_def by auto
+      moreover have "g w = gp w * (w - z) powr of_int gn" "gp w \<noteq> 0"
+        using gr(2)[rule_format, of w] that unfolding r1_def by auto
+      ultimately show "fg w = (fp w * gp w) * (w - z) powr (of_int (fn+gn))" "fp w*gp w\<noteq>0"
+        unfolding fg_def by (auto simp add:powr_add)
+    qed
+
+    have [intro]: "fp \<midarrow>z\<rightarrow>fp z" "gp \<midarrow>z\<rightarrow>gp z"
+        using fr(1) \<open>fr>0\<close> gr(1) \<open>gr>0\<close>
+        by (meson open_ball ball_subset_cball centre_in_ball
+            continuous_on_eq_continuous_at continuous_within holomorphic_on_imp_continuous_on
+            holomorphic_on_subset)+
+    have ?thesis when "fn+gn>0"
+    proof -
+      have "(\<lambda>w. (fp w * gp w) * (w - z) ^ (nat (fn+gn))) \<midarrow>z\<rightarrow>0"
+        using that by (auto intro!:tendsto_eq_intros)
+      then have "fg \<midarrow>z\<rightarrow> 0"
+        apply (elim Lim_transform_within[OF _ \<open>r1>0\<close>])
+        by (metis (no_types, hide_lams) Diff_iff cball_trivial dist_commute dist_self
+              eq_iff_diff_eq_0 fg_times less_le linorder_not_le mem_ball mem_cball powr_of_int
+              that)
+      then show ?thesis unfolding not_essential_def fg_def by auto
+    qed
+    moreover have ?thesis when "fn+gn=0"
+    proof -
+      have "(\<lambda>w. fp w * gp w) \<midarrow>z\<rightarrow>fp z*gp z"
+        using that by (auto intro!:tendsto_eq_intros)
+      then have "fg \<midarrow>z\<rightarrow> fp z*gp z"
+        apply (elim Lim_transform_within[OF _ \<open>r1>0\<close>])
+        apply (subst fg_times)
+        by (auto simp add:dist_commute that)
+      then show ?thesis unfolding not_essential_def fg_def by auto
+    qed
+    moreover have ?thesis when "fn+gn<0"
+    proof -
+      have "LIM w (at z). fp w * gp w / (w-z)^nat (-(fn+gn)) :> at_infinity"
+        apply (rule filterlim_divide_at_infinity)
+        apply (insert that, auto intro!:tendsto_eq_intros filterlim_atI)
+        using eventually_at_topological by blast
+      then have "is_pole fg z" unfolding is_pole_def
+        apply (elim filterlim_transform_within[OF _ _ \<open>r1>0\<close>],simp)
+        apply (subst fg_times,simp add:dist_commute)
+        apply (subst powr_of_int)
+        using that by (auto simp add:field_split_simps)
+      then show ?thesis unfolding not_essential_def fg_def by auto
+    qed
+    ultimately show ?thesis unfolding not_essential_def fg_def by fastforce
+  qed
+  ultimately show ?thesis by auto
+qed
+
+lemma not_essential_inverse[singularity_intros]:
+  assumes f_ness:"not_essential f z"
+  assumes f_iso:"isolated_singularity_at f z"
+  shows "not_essential (\<lambda>w. inverse (f w)) z"
+proof -
+  define vf where "vf = (\<lambda>w. inverse (f w))"
+  have ?thesis when "\<not>(\<exists>\<^sub>Fw in (at z). f w\<noteq>0)"
+  proof -
+    have "\<forall>\<^sub>Fw in (at z). f w=0"
+      using that[unfolded frequently_def, simplified] by (auto elim: eventually_rev_mp)
+    then have "\<forall>\<^sub>Fw in (at z). vf w=0"
+      unfolding vf_def by auto
+    from tendsto_cong[OF this] have "vf \<midarrow>z\<rightarrow>0" unfolding vf_def by auto
+    then show ?thesis unfolding not_essential_def vf_def by auto
+  qed
+  moreover have ?thesis when "is_pole f z"
+  proof -
+    have "vf \<midarrow>z\<rightarrow>0"
+      using that filterlim_at filterlim_inverse_at_iff unfolding is_pole_def vf_def by blast
+    then show ?thesis unfolding not_essential_def vf_def by auto
+  qed
+  moreover have ?thesis when "\<exists>x. f\<midarrow>z\<rightarrow>x " and f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
+  proof -
+    from that obtain fz where fz:"f\<midarrow>z\<rightarrow>fz" by auto
+    have ?thesis when "fz=0"
+    proof -
+      have "(\<lambda>w. inverse (vf w)) \<midarrow>z\<rightarrow>0"
+        using fz that unfolding vf_def by auto
+      moreover have "\<forall>\<^sub>F w in at z. inverse (vf w) \<noteq> 0"
+        using non_zero_neighbour[OF f_iso f_ness f_nconst]
+        unfolding vf_def by auto
+      ultimately have "is_pole vf z"
+        using filterlim_inverse_at_iff[of vf "at z"] unfolding filterlim_at is_pole_def by auto
+      then show ?thesis unfolding not_essential_def vf_def by auto
+    qed
+    moreover have ?thesis when "fz\<noteq>0"
+    proof -
+      have "vf \<midarrow>z\<rightarrow>inverse fz"
+        using fz that unfolding vf_def by (auto intro:tendsto_eq_intros)
+      then show ?thesis unfolding not_essential_def vf_def by auto
+    qed
+    ultimately show ?thesis by auto
+  qed
+  ultimately show ?thesis using f_ness unfolding not_essential_def by auto
+qed
+
+lemma isolated_singularity_at_inverse[singularity_intros]:
+  assumes f_iso:"isolated_singularity_at f z"
+      and f_ness:"not_essential f z"
+  shows "isolated_singularity_at (\<lambda>w. inverse (f w)) z"
+proof -
+  define vf where "vf = (\<lambda>w. inverse (f w))"
+  have ?thesis when "\<not>(\<exists>\<^sub>Fw in (at z). f w\<noteq>0)"
+  proof -
+    have "\<forall>\<^sub>Fw in (at z). f w=0"
+      using that[unfolded frequently_def, simplified] by (auto elim: eventually_rev_mp)
+    then have "\<forall>\<^sub>Fw in (at z). vf w=0"
+      unfolding vf_def by auto
+    then obtain d1 where "d1>0" and d1:"\<forall>x. x \<noteq> z \<and> dist x z < d1 \<longrightarrow> vf x = 0"
+      unfolding eventually_at by auto
+    then have "vf holomorphic_on ball z d1-{z}"
+      apply (rule_tac holomorphic_transform[of "\<lambda>_. 0"])
+      by (auto simp add:dist_commute)
+    then have "vf analytic_on ball z d1 - {z}"
+      by (simp add: analytic_on_open open_delete)
+    then show ?thesis using \<open>d1>0\<close> unfolding isolated_singularity_at_def vf_def by auto
+  qed
+  moreover have ?thesis when f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
+  proof -
+    have "\<forall>\<^sub>F w in at z. f w \<noteq> 0" using non_zero_neighbour[OF f_iso f_ness f_nconst] .
+    then obtain d1 where d1:"d1>0" "\<forall>x. x \<noteq> z \<and> dist x z < d1 \<longrightarrow> f x \<noteq> 0"
+      unfolding eventually_at by auto
+    obtain d2 where "d2>0" and d2:"f analytic_on ball z d2 - {z}"
+      using f_iso unfolding isolated_singularity_at_def by auto
+    define d3 where "d3=min d1 d2"
+    have "d3>0" unfolding d3_def using \<open>d1>0\<close> \<open>d2>0\<close> by auto
+    moreover have "vf analytic_on ball z d3 - {z}"
+      unfolding vf_def
+      apply (rule analytic_on_inverse)
+      subgoal using d2 unfolding d3_def by (elim analytic_on_subset) auto
+      subgoal for w using d1 unfolding d3_def by (auto simp add:dist_commute)
+      done
+    ultimately show ?thesis unfolding isolated_singularity_at_def vf_def by auto
+  qed
+  ultimately show ?thesis by auto
+qed
+
+lemma not_essential_divide[singularity_intros]:
+  assumes f_ness:"not_essential f z" and g_ness:"not_essential g z"
+  assumes f_iso:"isolated_singularity_at f z" and g_iso:"isolated_singularity_at g z"
+  shows "not_essential (\<lambda>w. f w / g w) z"
+proof -
+  have "not_essential (\<lambda>w. f w * inverse (g w)) z"
+    apply (rule not_essential_times[where g="\<lambda>w. inverse (g w)"])
+    using assms by (auto intro: isolated_singularity_at_inverse not_essential_inverse)
+  then show ?thesis by (simp add:field_simps)
+qed
+
+lemma
+  assumes f_iso:"isolated_singularity_at f z"
+      and g_iso:"isolated_singularity_at g z"
+    shows isolated_singularity_at_times[singularity_intros]:
+              "isolated_singularity_at (\<lambda>w. f w * g w) z" and
+          isolated_singularity_at_add[singularity_intros]:
+              "isolated_singularity_at (\<lambda>w. f w + g w) z"
+proof -
+  obtain d1 d2 where "d1>0" "d2>0"
+      and d1:"f analytic_on ball z d1 - {z}" and d2:"g analytic_on ball z d2 - {z}"
+    using f_iso g_iso unfolding isolated_singularity_at_def by auto
+  define d3 where "d3=min d1 d2"
+  have "d3>0" unfolding d3_def using \<open>d1>0\<close> \<open>d2>0\<close> by auto
+
+  have "(\<lambda>w. f w * g w) analytic_on ball z d3 - {z}"
+    apply (rule analytic_on_mult)
+    using d1 d2 unfolding d3_def by (auto elim:analytic_on_subset)
+  then show "isolated_singularity_at (\<lambda>w. f w * g w) z"
+    using \<open>d3>0\<close> unfolding isolated_singularity_at_def by auto
+  have "(\<lambda>w. f w + g w) analytic_on ball z d3 - {z}"
+    apply (rule analytic_on_add)
+    using d1 d2 unfolding d3_def by (auto elim:analytic_on_subset)
+  then show "isolated_singularity_at (\<lambda>w. f w + g w) z"
+    using \<open>d3>0\<close> unfolding isolated_singularity_at_def by auto
+qed
+
+lemma isolated_singularity_at_uminus[singularity_intros]:
+  assumes f_iso:"isolated_singularity_at f z"
+  shows "isolated_singularity_at (\<lambda>w. - f w) z"
+  using assms unfolding isolated_singularity_at_def using analytic_on_neg by blast
+
+lemma isolated_singularity_at_id[singularity_intros]:
+     "isolated_singularity_at (\<lambda>w. w) z"
+  unfolding isolated_singularity_at_def by (simp add: gt_ex)
+
+lemma isolated_singularity_at_minus[singularity_intros]:
+  assumes f_iso:"isolated_singularity_at f z"
+      and g_iso:"isolated_singularity_at g z"
+    shows "isolated_singularity_at (\<lambda>w. f w - g w) z"
+  using isolated_singularity_at_uminus[THEN isolated_singularity_at_add[OF f_iso,of "\<lambda>w. - g w"]
+        ,OF g_iso] by simp
+
+lemma isolated_singularity_at_divide[singularity_intros]:
+  assumes f_iso:"isolated_singularity_at f z"
+      and g_iso:"isolated_singularity_at g z"
+      and g_ness:"not_essential g z"
+    shows "isolated_singularity_at (\<lambda>w. f w / g w) z"
+  using isolated_singularity_at_inverse[THEN isolated_singularity_at_times[OF f_iso,
+          of "\<lambda>w. inverse (g w)"],OF g_iso g_ness] by (simp add:field_simps)
+
+lemma isolated_singularity_at_const[singularity_intros]:
+    "isolated_singularity_at (\<lambda>w. c) z"
+  unfolding isolated_singularity_at_def by (simp add: gt_ex)
+
+lemma isolated_singularity_at_holomorphic:
+  assumes "f holomorphic_on s-{z}" "open s" "z\<in>s"
+  shows "isolated_singularity_at f z"
+  using assms unfolding isolated_singularity_at_def
+  by (metis analytic_on_holomorphic centre_in_ball insert_Diff openE open_delete subset_insert_iff)
+
+subsubsection \<open>The order of non-essential singularities (i.e. removable singularities or poles)\<close>
+
+
+definition\<^marker>\<open>tag important\<close> zorder :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> int" where
+  "zorder f z = (THE n. (\<exists>h r. r>0 \<and> h holomorphic_on cball z r \<and> h z\<noteq>0
+                   \<and> (\<forall>w\<in>cball z r - {z}. f w =  h w * (w-z) powr (of_int n)
+                   \<and> h w \<noteq>0)))"
+
+definition\<^marker>\<open>tag important\<close> zor_poly
+    ::"[complex \<Rightarrow> complex, complex] \<Rightarrow> complex \<Rightarrow> complex" where
+  "zor_poly f z = (SOME h. \<exists>r. r > 0 \<and> h holomorphic_on cball z r \<and> h z \<noteq> 0
+                   \<and> (\<forall>w\<in>cball z r - {z}. f w =  h w * (w - z) powr (zorder f z)
+                   \<and> h w \<noteq>0))"
+
+lemma zorder_exist:
+  fixes f::"complex \<Rightarrow> complex" and z::complex
+  defines "n\<equiv>zorder f z" and "g\<equiv>zor_poly f z"
+  assumes f_iso:"isolated_singularity_at f z"
+      and f_ness:"not_essential f z"
+      and f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
+  shows "g z\<noteq>0 \<and> (\<exists>r. r>0 \<and> g holomorphic_on cball z r
+    \<and> (\<forall>w\<in>cball z r - {z}. f w  = g w * (w-z) powr n  \<and> g w \<noteq>0))"
+proof -
+  define P where "P = (\<lambda>n g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z\<noteq>0
+          \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w-z) powr (of_int n) \<and> g w\<noteq>0))"
+  have "\<exists>!n. \<exists>g r. P n g r"
+    using holomorphic_factor_puncture[OF assms(3-)] unfolding P_def by auto
+  then have "\<exists>g r. P n g r"
+    unfolding n_def P_def zorder_def
+    by (drule_tac theI',argo)
+  then have "\<exists>r. P n g r"
+    unfolding P_def zor_poly_def g_def n_def
+    by (drule_tac someI_ex,argo)
+  then obtain r1 where "P n g r1" by auto
+  then show ?thesis unfolding P_def by auto
+qed
+
+lemma
+  fixes f::"complex \<Rightarrow> complex" and z::complex
+  assumes f_iso:"isolated_singularity_at f z"
+      and f_ness:"not_essential f z"
+      and f_nconst:"\<exists>\<^sub>Fw in (at z). f w\<noteq>0"
+    shows zorder_inverse: "zorder (\<lambda>w. inverse (f w)) z = - zorder f z"
+      and zor_poly_inverse: "\<forall>\<^sub>Fw in (at z). zor_poly (\<lambda>w. inverse (f w)) z w
+                                                = inverse (zor_poly f z w)"
+proof -
+  define vf where "vf = (\<lambda>w. inverse (f w))"
+  define fn vfn where
+    "fn = zorder f z"  and "vfn = zorder vf z"
+  define fp vfp where
+    "fp = zor_poly f z" and "vfp = zor_poly vf z"
+
+  obtain fr where [simp]:"fp z \<noteq> 0" and "fr > 0"
+          and fr: "fp holomorphic_on cball z fr"
+                  "\<forall>w\<in>cball z fr - {z}. f w = fp w * (w - z) powr of_int fn \<and> fp w \<noteq> 0"
+    using zorder_exist[OF f_iso f_ness f_nconst,folded fn_def fp_def]
+    by auto
+  have fr_inverse: "vf w = (inverse (fp w)) * (w - z) powr (of_int (-fn))"
+        and fr_nz: "inverse (fp w)\<noteq>0"
+    when "w\<in>ball z fr - {z}" for w
+  proof -
+    have "f w = fp w * (w - z) powr of_int fn" "fp w\<noteq>0"
+      using fr(2)[rule_format,of w] that by auto
+    then show "vf w = (inverse (fp w)) * (w - z) powr (of_int (-fn))" "inverse (fp w)\<noteq>0"
+      unfolding vf_def by (auto simp add:powr_minus)
+  qed
+  obtain vfr where [simp]:"vfp z \<noteq> 0" and "vfr>0" and vfr:"vfp holomorphic_on cball z vfr"
+      "(\<forall>w\<in>cball z vfr - {z}. vf w = vfp w * (w - z) powr of_int vfn \<and> vfp w \<noteq> 0)"
+  proof -
+    have "isolated_singularity_at vf z"
+      using isolated_singularity_at_inverse[OF f_iso f_ness] unfolding vf_def .
+    moreover have "not_essential vf z"
+      using not_essential_inverse[OF f_ness f_iso] unfolding vf_def .
+    moreover have "\<exists>\<^sub>F w in at z. vf w \<noteq> 0"
+      using f_nconst unfolding vf_def by (auto elim:frequently_elim1)
+    ultimately show ?thesis using zorder_exist[of vf z, folded vfn_def vfp_def] that by auto
+  qed
+
+
+  define r1 where "r1 = min fr vfr"
+  have "r1>0" using \<open>fr>0\<close> \<open>vfr>0\<close> unfolding r1_def by simp
+  show "vfn = - fn"
+    apply (rule holomorphic_factor_unique[of r1 vfp z "\<lambda>w. inverse (fp w)" vf])
+    subgoal using \<open>r1>0\<close> by simp
+    subgoal by simp
+    subgoal by simp
+    subgoal
+    proof (rule ballI)
+      fix w assume "w \<in> ball z r1 - {z}"
+      then have "w \<in> ball z fr - {z}" "w \<in> cball z vfr - {z}"  unfolding r1_def by auto
+      from fr_inverse[OF this(1)] fr_nz[OF this(1)] vfr(2)[rule_format,OF this(2)]
+      show "vf w = vfp w * (w - z) powr of_int vfn \<and> vfp w \<noteq> 0
+              \<and> vf w = inverse (fp w) * (w - z) powr of_int (- fn) \<and> inverse (fp w) \<noteq> 0" by auto
+    qed
+    subgoal using vfr(1) unfolding r1_def by (auto intro!:holomorphic_intros)
+    subgoal using fr unfolding r1_def by (auto intro!:holomorphic_intros)
+    done
+
+  have "vfp w = inverse (fp w)" when "w\<in>ball z r1-{z}" for w
+  proof -
+    have "w \<in> ball z fr - {z}" "w \<in> cball z vfr - {z}"  "w\<noteq>z" using that unfolding r1_def by auto
+    from fr_inverse[OF this(1)] fr_nz[OF this(1)] vfr(2)[rule_format,OF this(2)] \<open>vfn = - fn\<close> \<open>w\<noteq>z\<close>
+    show ?thesis by auto
+  qed
+  then show "\<forall>\<^sub>Fw in (at z). vfp w = inverse (fp w)"
+    unfolding eventually_at using \<open>r1>0\<close>
+    apply (rule_tac x=r1 in exI)
+    by (auto simp add:dist_commute)
+qed
+
+lemma
+  fixes f g::"complex \<Rightarrow> complex" and z::complex
+  assumes f_iso:"isolated_singularity_at f z" and g_iso:"isolated_singularity_at g z"
+      and f_ness:"not_essential f z" and g_ness:"not_essential g z"
+      and fg_nconst: "\<exists>\<^sub>Fw in (at z). f w * g w\<noteq> 0"
+  shows zorder_times:"zorder (\<lambda>w. f w * g w) z = zorder f z + zorder g z" and
+        zor_poly_times:"\<forall>\<^sub>Fw in (at z). zor_poly (\<lambda>w. f w * g w) z w
+                                                  = zor_poly f z w *zor_poly g z w"
+proof -
+  define fg where "fg = (\<lambda>w. f w * g w)"
+  define fn gn fgn where
+    "fn = zorder f z" and "gn = zorder g z" and "fgn = zorder fg z"
+  define fp gp fgp where
+    "fp = zor_poly f z" and "gp = zor_poly g z" and "fgp = zor_poly fg z"
+  have f_nconst:"\<exists>\<^sub>Fw in (at z). f w \<noteq> 0" and g_nconst:"\<exists>\<^sub>Fw in (at z).g w\<noteq> 0"
+    using fg_nconst by (auto elim!:frequently_elim1)
+  obtain fr where [simp]:"fp z \<noteq> 0" and "fr > 0"
+          and fr: "fp holomorphic_on cball z fr"
+                  "\<forall>w\<in>cball z fr - {z}. f w = fp w * (w - z) powr of_int fn \<and> fp w \<noteq> 0"
+    using zorder_exist[OF f_iso f_ness f_nconst,folded fp_def fn_def] by auto
+  obtain gr where [simp]:"gp z \<noteq> 0" and "gr > 0"
+          and gr: "gp holomorphic_on cball z gr"
+                  "\<forall>w\<in>cball z gr - {z}. g w = gp w * (w - z) powr of_int gn \<and> gp w \<noteq> 0"
+    using zorder_exist[OF g_iso g_ness g_nconst,folded gn_def gp_def] by auto
+  define r1 where "r1=min fr gr"
+  have "r1>0" unfolding r1_def using \<open>fr>0\<close> \<open>gr>0\<close> by auto
+  have fg_times:"fg w = (fp w * gp w) * (w - z) powr (of_int (fn+gn))" and fgp_nz:"fp w*gp w\<noteq>0"
+    when "w\<in>ball z r1 - {z}" for w
+  proof -
+    have "f w = fp w * (w - z) powr of_int fn" "fp w\<noteq>0"
+      using fr(2)[rule_format,of w] that unfolding r1_def by auto
+    moreover have "g w = gp w * (w - z) powr of_int gn" "gp w \<noteq> 0"
+      using gr(2)[rule_format, of w] that unfolding r1_def by auto
+    ultimately show "fg w = (fp w * gp w) * (w - z) powr (of_int (fn+gn))" "fp w*gp w\<noteq>0"
+      unfolding fg_def by (auto simp add:powr_add)
+  qed
+
+  obtain fgr where [simp]:"fgp z \<noteq> 0" and "fgr > 0"
+          and fgr: "fgp holomorphic_on cball z fgr"
+                  "\<forall>w\<in>cball z fgr - {z}. fg w = fgp w * (w - z) powr of_int fgn \<and> fgp w \<noteq> 0"
+  proof -
+    have "fgp z \<noteq> 0 \<and> (\<exists>r>0. fgp holomorphic_on cball z r
+            \<and> (\<forall>w\<in>cball z r - {z}. fg w = fgp w * (w - z) powr of_int fgn \<and> fgp w \<noteq> 0))"
+      apply (rule zorder_exist[of fg z, folded fgn_def fgp_def])
+      subgoal unfolding fg_def using isolated_singularity_at_times[OF f_iso g_iso] .
+      subgoal unfolding fg_def using not_essential_times[OF f_ness g_ness f_iso g_iso] .
+      subgoal unfolding fg_def using fg_nconst .
+      done
+    then show ?thesis using that by blast
+  qed
+  define r2 where "r2 = min fgr r1"
+  have "r2>0" using \<open>r1>0\<close> \<open>fgr>0\<close> unfolding r2_def by simp
+  show "fgn = fn + gn "
+    apply (rule holomorphic_factor_unique[of r2 fgp z "\<lambda>w. fp w * gp w" fg])
+    subgoal using \<open>r2>0\<close> by simp
+    subgoal by simp
+    subgoal by simp
+    subgoal
+    proof (rule ballI)
+      fix w assume "w \<in> ball z r2 - {z}"
+      then have "w \<in> ball z r1 - {z}" "w \<in> cball z fgr - {z}"  unfolding r2_def by auto
+      from fg_times[OF this(1)] fgp_nz[OF this(1)] fgr(2)[rule_format,OF this(2)]
+      show "fg w = fgp w * (w - z) powr of_int fgn \<and> fgp w \<noteq> 0
+              \<and> fg w = fp w * gp w * (w - z) powr of_int (fn + gn) \<and> fp w * gp w \<noteq> 0" by auto
+    qed
+    subgoal using fgr(1) unfolding r2_def r1_def by (auto intro!:holomorphic_intros)
+    subgoal using fr(1) gr(1) unfolding r2_def r1_def by (auto intro!:holomorphic_intros)
+    done
+
+  have "fgp w = fp w *gp w" when "w\<in>ball z r2-{z}" for w
+  proof -
+    have "w \<in> ball z r1 - {z}" "w \<in> cball z fgr - {z}" "w\<noteq>z" using that  unfolding r2_def by auto
+    from fg_times[OF this(1)] fgp_nz[OF this(1)] fgr(2)[rule_format,OF this(2)] \<open>fgn = fn + gn\<close> \<open>w\<noteq>z\<close>
+    show ?thesis by auto
+  qed
+  then show "\<forall>\<^sub>Fw in (at z). fgp w = fp w * gp w"
+    using \<open>r2>0\<close> unfolding eventually_at by (auto simp add:dist_commute)
+qed
+
+lemma
+  fixes f g::"complex \<Rightarrow> complex" and z::complex
+  assumes f_iso:"isolated_singularity_at f z" and g_iso:"isolated_singularity_at g z"
+      and f_ness:"not_essential f z" and g_ness:"not_essential g z"
+      and fg_nconst: "\<exists>\<^sub>Fw in (at z). f w * g w\<noteq> 0"
+  shows zorder_divide:"zorder (\<lambda>w. f w / g w) z = zorder f z - zorder g z" and
+        zor_poly_divide:"\<forall>\<^sub>Fw in (at z). zor_poly (\<lambda>w. f w / g w) z w
+                                                  = zor_poly f z w  / zor_poly g z w"
+proof -
+  have f_nconst:"\<exists>\<^sub>Fw in (at z). f w \<noteq> 0" and g_nconst:"\<exists>\<^sub>Fw in (at z).g w\<noteq> 0"
+    using fg_nconst by (auto elim!:frequently_elim1)
+  define vg where "vg=(\<lambda>w. inverse (g w))"
+  have "zorder (\<lambda>w. f w * vg w) z = zorder f z + zorder vg z"
+    apply (rule zorder_times[OF f_iso _ f_ness,of vg])
+    subgoal unfolding vg_def using isolated_singularity_at_inverse[OF g_iso g_ness] .
+    subgoal unfolding vg_def using not_essential_inverse[OF g_ness g_iso] .
+    subgoal unfolding vg_def using fg_nconst by (auto elim!:frequently_elim1)
+    done
+  then show "zorder (\<lambda>w. f w / g w) z = zorder f z - zorder g z"
+    using zorder_inverse[OF g_iso g_ness g_nconst,folded vg_def] unfolding vg_def
+    by (auto simp add:field_simps)
+
+  have "\<forall>\<^sub>F w in at z. zor_poly (\<lambda>w. f w * vg w) z w = zor_poly f z w * zor_poly vg z w"
+    apply (rule zor_poly_times[OF f_iso _ f_ness,of vg])
+    subgoal unfolding vg_def using isolated_singularity_at_inverse[OF g_iso g_ness] .
+    subgoal unfolding vg_def using not_essential_inverse[OF g_ness g_iso] .
+    subgoal unfolding vg_def using fg_nconst by (auto elim!:frequently_elim1)
+    done
+  then show "\<forall>\<^sub>Fw in (at z). zor_poly (\<lambda>w. f w / g w) z w = zor_poly f z w  / zor_poly g z w"
+    using zor_poly_inverse[OF g_iso g_ness g_nconst,folded vg_def] unfolding vg_def
+    apply eventually_elim
+    by (auto simp add:field_simps)
+qed
+
+lemma zorder_exist_zero:
+  fixes f::"complex \<Rightarrow> complex" and z::complex
+  defines "n\<equiv>zorder f z" and "g\<equiv>zor_poly f z"
+  assumes  holo: "f holomorphic_on s" and
+          "open s" "connected s" "z\<in>s"
+      and non_const: "\<exists>w\<in>s. f w \<noteq> 0"
+  shows "(if f z=0 then n > 0 else n=0) \<and> (\<exists>r. r>0 \<and> cball z r \<subseteq> s \<and> g holomorphic_on cball z r
+    \<and> (\<forall>w\<in>cball z r. f w  = g w * (w-z) ^ nat n  \<and> g w \<noteq>0))"
+proof -
+  obtain r where "g z \<noteq> 0" and r: "r>0" "cball z r \<subseteq> s" "g holomorphic_on cball z r"
+            "(\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
+  proof -
+    have "g z \<noteq> 0 \<and> (\<exists>r>0. g holomorphic_on cball z r
+            \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0))"
+    proof (rule zorder_exist[of f z,folded g_def n_def])
+      show "isolated_singularity_at f z" unfolding isolated_singularity_at_def
+        using holo assms(4,6)
+        by (meson Diff_subset open_ball analytic_on_holomorphic holomorphic_on_subset openE)
+      show "not_essential f z" unfolding not_essential_def
+        using assms(4,6) at_within_open continuous_on holo holomorphic_on_imp_continuous_on
+        by fastforce
+      have "\<forall>\<^sub>F w in at z. f w \<noteq> 0 \<and> w\<in>s"
+      proof -
+        obtain w where "w\<in>s" "f w\<noteq>0" using non_const by auto
+        then show ?thesis
+          by (rule non_zero_neighbour_alt[OF holo \<open>open s\<close> \<open>connected s\<close> \<open>z\<in>s\<close>])
+      qed
+      then show "\<exists>\<^sub>F w in at z. f w \<noteq> 0"
+        apply (elim eventually_frequentlyE)
+        by auto
+    qed
+    then obtain r1 where "g z \<noteq> 0" "r1>0" and r1:"g holomorphic_on cball z r1"
+            "(\<forall>w\<in>cball z r1 - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
+      by auto
+    obtain r2 where r2: "r2>0" "cball z r2 \<subseteq> s"
+      using assms(4,6) open_contains_cball_eq by blast
+    define r3 where "r3=min r1 r2"
+    have "r3>0" "cball z r3 \<subseteq> s" using \<open>r1>0\<close> r2 unfolding r3_def by auto
+    moreover have "g holomorphic_on cball z r3"
+      using r1(1) unfolding r3_def by auto
+    moreover have "(\<forall>w\<in>cball z r3 - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
+      using r1(2) unfolding r3_def by auto
+    ultimately show ?thesis using that[of r3] \<open>g z\<noteq>0\<close> by auto
+  qed
+
+  have if_0:"if f z=0 then n > 0 else n=0"
+  proof -
+    have "f\<midarrow> z \<rightarrow> f z"
+      by (metis assms(4,6,7) at_within_open continuous_on holo holomorphic_on_imp_continuous_on)
+    then have "(\<lambda>w. g w * (w - z) powr of_int n) \<midarrow>z\<rightarrow> f z"
+      apply (elim Lim_transform_within_open[where s="ball z r"])
+      using r by auto
+    moreover have "g \<midarrow>z\<rightarrow>g z"
+      by (metis (mono_tags, lifting) open_ball at_within_open_subset
+          ball_subset_cball centre_in_ball continuous_on holomorphic_on_imp_continuous_on r(1,3) subsetCE)
+    ultimately have "(\<lambda>w. (g w * (w - z) powr of_int n) / g w) \<midarrow>z\<rightarrow> f z/g z"
+      apply (rule_tac tendsto_divide)
+      using \<open>g z\<noteq>0\<close> by auto
+    then have powr_tendsto:"(\<lambda>w. (w - z) powr of_int n) \<midarrow>z\<rightarrow> f z/g z"
+      apply (elim Lim_transform_within_open[where s="ball z r"])
+      using r by auto
+
+    have ?thesis when "n\<ge>0" "f z=0"
+    proof -
+      have "(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> f z/g z"
+        using powr_tendsto
+        apply (elim Lim_transform_within[where d=r])
+        by (auto simp add: powr_of_int \<open>n\<ge>0\<close> \<open>r>0\<close>)
+      then have *:"(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> 0" using \<open>f z=0\<close> by simp
+      moreover have False when "n=0"
+      proof -
+        have "(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> 1"
+          using \<open>n=0\<close> by auto
+        then show False using * using LIM_unique zero_neq_one by blast
+      qed
+      ultimately show ?thesis using that by fastforce
+    qed
+    moreover have ?thesis when "n\<ge>0" "f z\<noteq>0"
+    proof -
+      have False when "n>0"
+      proof -
+        have "(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> f z/g z"
+          using powr_tendsto
+          apply (elim Lim_transform_within[where d=r])
+          by (auto simp add: powr_of_int \<open>n\<ge>0\<close> \<open>r>0\<close>)
+        moreover have "(\<lambda>w. (w - z) ^ nat n) \<midarrow>z\<rightarrow> 0"
+          using \<open>n>0\<close> by (auto intro!:tendsto_eq_intros)
+        ultimately show False using \<open>f z\<noteq>0\<close> \<open>g z\<noteq>0\<close> using LIM_unique divide_eq_0_iff by blast
+      qed
+      then show ?thesis using that by force
+    qed
+    moreover have False when "n<0"
+    proof -
+      have "(\<lambda>w. inverse ((w - z) ^ nat (- n))) \<midarrow>z\<rightarrow> f z/g z"
+           "(\<lambda>w.((w - z) ^ nat (- n))) \<midarrow>z\<rightarrow> 0"
+        subgoal  using powr_tendsto powr_of_int that
+          by (elim Lim_transform_within_open[where s=UNIV],auto)
+        subgoal using that by (auto intro!:tendsto_eq_intros)
+        done
+      from tendsto_mult[OF this,simplified]
+      have "(\<lambda>x. inverse ((x - z) ^ nat (- n)) * (x - z) ^ nat (- n)) \<midarrow>z\<rightarrow> 0" .
+      then have "(\<lambda>x. 1::complex) \<midarrow>z\<rightarrow> 0"
+        by (elim Lim_transform_within_open[where s=UNIV],auto)
+      then show False using LIM_const_eq by fastforce
+    qed
+    ultimately show ?thesis by fastforce
+  qed
+  moreover have "f w  = g w * (w-z) ^ nat n  \<and> g w \<noteq>0" when "w\<in>cball z r" for w
+  proof (cases "w=z")
+    case True
+    then have "f \<midarrow>z\<rightarrow>f w"
+      using assms(4,6) at_within_open continuous_on holo holomorphic_on_imp_continuous_on by fastforce
+    then have "(\<lambda>w. g w * (w-z) ^ nat n) \<midarrow>z\<rightarrow>f w"
+    proof (elim Lim_transform_within[OF _ \<open>r>0\<close>])
+      fix x assume "0 < dist x z" "dist x z < r"
+      then have "x \<in> cball z r - {z}" "x\<noteq>z"
+        unfolding cball_def by (auto simp add: dist_commute)
+      then have "f x = g x * (x - z) powr of_int n"
+        using r(4)[rule_format,of x] by simp
+      also have "... = g x * (x - z) ^ nat n"
+        apply (subst powr_of_int)
+        using if_0 \<open>x\<noteq>z\<close> by (auto split:if_splits)
+      finally show "f x = g x * (x - z) ^ nat n" .
+    qed
+    moreover have "(\<lambda>w. g w * (w-z) ^ nat n) \<midarrow>z\<rightarrow> g w * (w-z) ^ nat n"
+      using True apply (auto intro!:tendsto_eq_intros)
+      by (metis open_ball at_within_open_subset ball_subset_cball centre_in_ball
+          continuous_on holomorphic_on_imp_continuous_on r(1) r(3) that)
+    ultimately have "f w = g w * (w-z) ^ nat n" using LIM_unique by blast
+    then show ?thesis using \<open>g z\<noteq>0\<close> True by auto
+  next
+    case False
+    then have "f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0"
+      using r(4) that by auto
+    then show ?thesis using False if_0 powr_of_int by (auto split:if_splits)
+  qed
+  ultimately show ?thesis using r by auto
+qed
+
+lemma zorder_exist_pole:
+  fixes f::"complex \<Rightarrow> complex" and z::complex
+  defines "n\<equiv>zorder f z" and "g\<equiv>zor_poly f z"
+  assumes  holo: "f holomorphic_on s-{z}" and
+          "open s" "z\<in>s"
+      and "is_pole f z"
+  shows "n < 0 \<and> g z\<noteq>0 \<and> (\<exists>r. r>0 \<and> cball z r \<subseteq> s \<and> g holomorphic_on cball z r
+    \<and> (\<forall>w\<in>cball z r - {z}. f w  = g w / (w-z) ^ nat (- n) \<and> g w \<noteq>0))"
+proof -
+  obtain r where "g z \<noteq> 0" and r: "r>0" "cball z r \<subseteq> s" "g holomorphic_on cball z r"
+            "(\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
+  proof -
+    have "g z \<noteq> 0 \<and> (\<exists>r>0. g holomorphic_on cball z r
+            \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0))"
+    proof (rule zorder_exist[of f z,folded g_def n_def])
+      show "isolated_singularity_at f z" unfolding isolated_singularity_at_def
+        using holo assms(4,5)
+        by (metis analytic_on_holomorphic centre_in_ball insert_Diff openE open_delete subset_insert_iff)
+      show "not_essential f z" unfolding not_essential_def
+        using assms(4,6) at_within_open continuous_on holo holomorphic_on_imp_continuous_on
+        by fastforce
+      from non_zero_neighbour_pole[OF \<open>is_pole f z\<close>] show "\<exists>\<^sub>F w in at z. f w \<noteq> 0"
+        apply (elim eventually_frequentlyE)
+        by auto
+    qed
+    then obtain r1 where "g z \<noteq> 0" "r1>0" and r1:"g holomorphic_on cball z r1"
+            "(\<forall>w\<in>cball z r1 - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
+      by auto
+    obtain r2 where r2: "r2>0" "cball z r2 \<subseteq> s"
+      using assms(4,5) open_contains_cball_eq by metis
+    define r3 where "r3=min r1 r2"
+    have "r3>0" "cball z r3 \<subseteq> s" using \<open>r1>0\<close> r2 unfolding r3_def by auto
+    moreover have "g holomorphic_on cball z r3"
+      using r1(1) unfolding r3_def by auto
+    moreover have "(\<forall>w\<in>cball z r3 - {z}. f w = g w * (w - z) powr of_int n \<and> g w \<noteq> 0)"
+      using r1(2) unfolding r3_def by auto
+    ultimately show ?thesis using that[of r3] \<open>g z\<noteq>0\<close> by auto
+  qed
+
+  have "n<0"
+  proof (rule ccontr)
+    assume " \<not> n < 0"
+    define c where "c=(if n=0 then g z else 0)"
+    have [simp]:"g \<midarrow>z\<rightarrow> g z"
+      by (metis open_ball at_within_open ball_subset_cball centre_in_ball
+            continuous_on holomorphic_on_imp_continuous_on holomorphic_on_subset r(1) r(3) )
+    have "\<forall>\<^sub>F x in at z. f x = g x * (x - z) ^ nat n"
+      unfolding eventually_at_topological
+      apply (rule_tac exI[where x="ball z r"])
+      using r powr_of_int \<open>\<not> n < 0\<close> by auto
+    moreover have "(\<lambda>x. g x * (x - z) ^ nat n) \<midarrow>z\<rightarrow>c"
+    proof (cases "n=0")
+      case True
+      then show ?thesis unfolding c_def by simp
+    next
+      case False
+      then have "(\<lambda>x. (x - z) ^ nat n) \<midarrow>z\<rightarrow> 0" using \<open>\<not> n < 0\<close>
+        by (auto intro!:tendsto_eq_intros)
+      from tendsto_mult[OF _ this,of g "g z",simplified]
+      show ?thesis unfolding c_def using False by simp
+    qed
+    ultimately have "f \<midarrow>z\<rightarrow>c" using tendsto_cong by fast
+    then show False using \<open>is_pole f z\<close> at_neq_bot not_tendsto_and_filterlim_at_infinity
+      unfolding is_pole_def by blast
+  qed
+  moreover have "\<forall>w\<in>cball z r - {z}. f w  = g w / (w-z) ^ nat (- n) \<and> g w \<noteq>0"
+    using r(4) \<open>n<0\<close> powr_of_int
+    by (metis Diff_iff divide_inverse eq_iff_diff_eq_0 insert_iff linorder_not_le)
+  ultimately show ?thesis using r(1-3) \<open>g z\<noteq>0\<close> by auto
+qed
+
+lemma zorder_eqI:
+  assumes "open s" "z \<in> s" "g holomorphic_on s" "g z \<noteq> 0"
+  assumes fg_eq:"\<And>w. \<lbrakk>w \<in> s;w\<noteq>z\<rbrakk> \<Longrightarrow> f w = g w * (w - z) powr n"
+  shows   "zorder f z = n"
+proof -
+  have "continuous_on s g" by (rule holomorphic_on_imp_continuous_on) fact
+  moreover have "open (-{0::complex})" by auto
+  ultimately have "open ((g -` (-{0})) \<inter> s)"
+    unfolding continuous_on_open_vimage[OF \<open>open s\<close>] by blast
+  moreover from assms have "z \<in> (g -` (-{0})) \<inter> s" by auto
+  ultimately obtain r where r: "r > 0" "cball z r \<subseteq>  s \<inter> (g -` (-{0}))"
+    unfolding open_contains_cball by blast
+
+  let ?gg= "(\<lambda>w. g w * (w - z) powr n)"
+  define P where "P = (\<lambda>n g r. 0 < r \<and> g holomorphic_on cball z r \<and> g z\<noteq>0
+          \<and> (\<forall>w\<in>cball z r - {z}. f w = g w * (w-z) powr (of_int n) \<and> g w\<noteq>0))"
+  have "P n g r"
+    unfolding P_def using r assms(3,4,5) by auto
+  then have "\<exists>g r. P n g r" by auto
+  moreover have unique: "\<exists>!n. \<exists>g r. P n g r" unfolding P_def
+  proof (rule holomorphic_factor_puncture)
+    have "ball z r-{z} \<subseteq> s" using r using ball_subset_cball by blast
+    then have "?gg holomorphic_on ball z r-{z}"
+      using \<open>g holomorphic_on s\<close> r by (auto intro!: holomorphic_intros)
+    then have "f holomorphic_on ball z r - {z}"
+      apply (elim holomorphic_transform)
+      using fg_eq \<open>ball z r-{z} \<subseteq> s\<close> by auto
+    then show "isolated_singularity_at f z" unfolding isolated_singularity_at_def
+      using analytic_on_open open_delete r(1) by blast
+  next
+    have "not_essential ?gg z"
+    proof (intro singularity_intros)
+      show "not_essential g z"
+        by (meson \<open>continuous_on s g\<close> assms(1) assms(2) continuous_on_eq_continuous_at
+            isCont_def not_essential_def)
+      show " \<forall>\<^sub>F w in at z. w - z \<noteq> 0" by (simp add: eventually_at_filter)
+      then show "LIM w at z. w - z :> at 0"
+        unfolding filterlim_at by (auto intro:tendsto_eq_intros)
+      show "isolated_singularity_at g z"
+        by (meson Diff_subset open_ball analytic_on_holomorphic
+            assms(1,2,3) holomorphic_on_subset isolated_singularity_at_def openE)
+    qed
+    then show "not_essential f z"
+      apply (elim not_essential_transform)
+      unfolding eventually_at using assms(1,2) assms(5)[symmetric]
+      by (metis dist_commute mem_ball openE subsetCE)
+    show "\<exists>\<^sub>F w in at z. f w \<noteq> 0" unfolding frequently_at
+    proof (rule,rule)
+      fix d::real assume "0 < d"
+      define z' where "z'=z+min d r / 2"
+      have "z' \<noteq> z" " dist z' z < d "
+        unfolding z'_def using \<open>d>0\<close> \<open>r>0\<close>
+        by (auto simp add:dist_norm)
+      moreover have "f z' \<noteq> 0"
+      proof (subst fg_eq[OF _ \<open>z'\<noteq>z\<close>])
+        have "z' \<in> cball z r" unfolding z'_def using \<open>r>0\<close> \<open>d>0\<close> by (auto simp add:dist_norm)
+        then show " z' \<in> s" using r(2) by blast
+        show "g z' * (z' - z) powr of_int n \<noteq> 0"
+          using P_def \<open>P n g r\<close> \<open>z' \<in> cball z r\<close> calculation(1) by auto
+      qed
+      ultimately show "\<exists>x\<in>UNIV. x \<noteq> z \<and> dist x z < d \<and> f x \<noteq> 0" by auto
+    qed
+  qed
+  ultimately have "(THE n. \<exists>g r. P n g r) = n"
+    by (rule_tac the1_equality)
+  then show ?thesis unfolding zorder_def P_def by blast
+qed
+
+lemma residue_pole_order:
+  fixes f::"complex \<Rightarrow> complex" and z::complex
+  defines "n \<equiv> nat (- zorder f z)" and "h \<equiv> zor_poly f z"
+  assumes f_iso:"isolated_singularity_at f z"
+    and pole:"is_pole f z"
+  shows "residue f z = ((deriv ^^ (n - 1)) h z / fact (n-1))"
+proof -
+  define g where "g \<equiv> \<lambda>x. if x=z then 0 else inverse (f x)"
+  obtain e where [simp]:"e>0" and f_holo:"f holomorphic_on ball z e - {z}"
+    using f_iso analytic_imp_holomorphic unfolding isolated_singularity_at_def by blast
+  obtain r where "0 < n" "0 < r" and r_cball:"cball z r \<subseteq> ball z e" and h_holo: "h holomorphic_on cball z r"
+      and h_divide:"(\<forall>w\<in>cball z r. (w\<noteq>z \<longrightarrow> f w = h w / (w - z) ^ n) \<and> h w \<noteq> 0)"
+  proof -
+    obtain r where r:"zorder f z < 0" "h z \<noteq> 0" "r>0" "cball z r \<subseteq> ball z e" "h holomorphic_on cball z r"
+        "(\<forall>w\<in>cball z r - {z}. f w = h w / (w - z) ^ n \<and> h w \<noteq> 0)"
+      using zorder_exist_pole[OF f_holo,simplified,OF \<open>is_pole f z\<close>,folded n_def h_def] by auto
+    have "n>0" using \<open>zorder f z < 0\<close> unfolding n_def by simp
+    moreover have "(\<forall>w\<in>cball z r. (w\<noteq>z \<longrightarrow> f w = h w / (w - z) ^ n) \<and> h w \<noteq> 0)"
+      using \<open>h z\<noteq>0\<close> r(6) by blast
+    ultimately show ?thesis using r(3,4,5) that by blast
+  qed
+  have r_nonzero:"\<And>w. w \<in> ball z r - {z} \<Longrightarrow> f w \<noteq> 0"
+    using h_divide by simp
+  define c where "c \<equiv> 2 * pi * \<i>"
+  define der_f where "der_f \<equiv> ((deriv ^^ (n - 1)) h z / fact (n-1))"
+  define h' where "h' \<equiv> \<lambda>u. h u / (u - z) ^ n"
+  have "(h' has_contour_integral c / fact (n - 1) * (deriv ^^ (n - 1)) h z) (circlepath z r)"
+    unfolding h'_def
+    proof (rule Cauchy_has_contour_integral_higher_derivative_circlepath[of z r h z "n-1",
+        folded c_def Suc_pred'[OF \<open>n>0\<close>]])
+      show "continuous_on (cball z r) h" using holomorphic_on_imp_continuous_on h_holo by simp
+      show "h holomorphic_on ball z r" using h_holo by auto
+      show " z \<in> ball z r" using \<open>r>0\<close> by auto
+    qed
+  then have "(h' has_contour_integral c * der_f) (circlepath z r)" unfolding der_f_def by auto
+  then have "(f has_contour_integral c * der_f) (circlepath z r)"
+    proof (elim has_contour_integral_eq)
+      fix x assume "x \<in> path_image (circlepath z r)"
+      hence "x\<in>cball z r - {z}" using \<open>r>0\<close> by auto
+      then show "h' x = f x" using h_divide unfolding h'_def by auto
+    qed
+  moreover have "(f has_contour_integral c * residue f z) (circlepath z r)"
+    using base_residue[of \<open>ball z e\<close> z,simplified,OF \<open>r>0\<close> f_holo r_cball,folded c_def]
+    unfolding c_def by simp
+  ultimately have "c * der_f =  c * residue f z" using has_contour_integral_unique by blast
+  hence "der_f = residue f z" unfolding c_def by auto
+  thus ?thesis unfolding der_f_def by auto
+qed
+
+lemma simple_zeroI:
+  assumes "open s" "z \<in> s" "g holomorphic_on s" "g z \<noteq> 0"
+  assumes "\<And>w. w \<in> s \<Longrightarrow> f w = g w * (w - z)"
+  shows   "zorder f z = 1"
+  using assms(1-4) by (rule zorder_eqI) (use assms(5) in auto)
+
+lemma higher_deriv_power:
+  shows   "(deriv ^^ j) (\<lambda>w. (w - z) ^ n) w =
+             pochhammer (of_nat (Suc n - j)) j * (w - z) ^ (n - j)"
+proof (induction j arbitrary: w)
+  case 0
+  thus ?case by auto
+next
+  case (Suc j w)
+  have "(deriv ^^ Suc j) (\<lambda>w. (w - z) ^ n) w = deriv ((deriv ^^ j) (\<lambda>w. (w - z) ^ n)) w"
+    by simp
+  also have "(deriv ^^ j) (\<lambda>w. (w - z) ^ n) =
+               (\<lambda>w. pochhammer (of_nat (Suc n - j)) j * (w - z) ^ (n - j))"
+    using Suc by (intro Suc.IH ext)
+  also {
+    have "(\<dots> has_field_derivative of_nat (n - j) *
+               pochhammer (of_nat (Suc n - j)) j * (w - z) ^ (n - Suc j)) (at w)"
+      using Suc.prems by (auto intro!: derivative_eq_intros)
+    also have "of_nat (n - j) * pochhammer (of_nat (Suc n - j)) j =
+                 pochhammer (of_nat (Suc n - Suc j)) (Suc j)"
+      by (cases "Suc j \<le> n", subst pochhammer_rec)
+         (insert Suc.prems, simp_all add: algebra_simps Suc_diff_le pochhammer_0_left)
+    finally have "deriv (\<lambda>w. pochhammer (of_nat (Suc n - j)) j * (w - z) ^ (n - j)) w =
+                    \<dots> * (w - z) ^ (n - Suc j)"
+      by (rule DERIV_imp_deriv)
+  }
+  finally show ?case .
+qed
+
+lemma zorder_zero_eqI:
+  assumes  f_holo:"f holomorphic_on s" and "open s" "z \<in> s"
+  assumes zero: "\<And>i. i < nat n \<Longrightarrow> (deriv ^^ i) f z = 0"
+  assumes nz: "(deriv ^^ nat n) f z \<noteq> 0" and "n\<ge>0"
+  shows   "zorder f z = n"
+proof -
+  obtain r where [simp]:"r>0" and "ball z r \<subseteq> s"
+    using \<open>open s\<close> \<open>z\<in>s\<close> openE by blast
+  have nz':"\<exists>w\<in>ball z r. f w \<noteq> 0"
+  proof (rule ccontr)
+    assume "\<not> (\<exists>w\<in>ball z r. f w \<noteq> 0)"
+    then have "eventually (\<lambda>u. f u = 0) (nhds z)"
+      using \<open>r>0\<close> unfolding eventually_nhds
+      apply (rule_tac x="ball z r" in exI)
+      by auto
+    then have "(deriv ^^ nat n) f z = (deriv ^^ nat n) (\<lambda>_. 0) z"
+      by (intro higher_deriv_cong_ev) auto
+    also have "(deriv ^^ nat n) (\<lambda>_. 0) z = 0"
+      by (induction n) simp_all
+    finally show False using nz by contradiction
+  qed
+
+  define zn g where "zn = zorder f z" and "g = zor_poly f z"
+  obtain e where e_if:"if f z = 0 then 0 < zn else zn = 0" and
+            [simp]:"e>0" and "cball z e \<subseteq> ball z r" and
+            g_holo:"g holomorphic_on cball z e" and
+            e_fac:"(\<forall>w\<in>cball z e. f w = g w * (w - z) ^ nat zn \<and> g w \<noteq> 0)"
+  proof -
+    have "f holomorphic_on ball z r"
+      using f_holo \<open>ball z r \<subseteq> s\<close> by auto
+    from that zorder_exist_zero[of f "ball z r" z,simplified,OF this nz',folded zn_def g_def]
+    show ?thesis by blast
+  qed
+  from this(1,2,5) have "zn\<ge>0" "g z\<noteq>0"
+    subgoal by (auto split:if_splits)
+    subgoal using \<open>0 < e\<close> ball_subset_cball centre_in_ball e_fac by blast
+    done
+
+  define A where "A = (\<lambda>i. of_nat (i choose (nat zn)) * fact (nat zn) * (deriv ^^ (i - nat zn)) g z)"
+  have deriv_A:"(deriv ^^ i) f z = (if zn \<le> int i then A i else 0)" for i
+  proof -
+    have "eventually (\<lambda>w. w \<in> ball z e) (nhds z)"
+      using \<open>cball z e \<subseteq> ball z r\<close> \<open>e>0\<close> by (intro eventually_nhds_in_open) auto
+    hence "eventually (\<lambda>w. f w = (w - z) ^ (nat zn) * g w) (nhds z)"
+      apply eventually_elim
+      by (use e_fac in auto)
+    hence "(deriv ^^ i) f z = (deriv ^^ i) (\<lambda>w. (w - z) ^ nat zn * g w) z"
+      by (intro higher_deriv_cong_ev) auto
+    also have "\<dots> = (\<Sum>j=0..i. of_nat (i choose j) *
+                       (deriv ^^ j) (\<lambda>w. (w - z) ^ nat zn) z * (deriv ^^ (i - j)) g z)"
+      using g_holo \<open>e>0\<close>
+      by (intro higher_deriv_mult[of _ "ball z e"]) (auto intro!: holomorphic_intros)
+    also have "\<dots> = (\<Sum>j=0..i. if j = nat zn then
+                    of_nat (i choose nat zn) * fact (nat zn) * (deriv ^^ (i - nat zn)) g z else 0)"
+    proof (intro sum.cong refl, goal_cases)
+      case (1 j)
+      have "(deriv ^^ j) (\<lambda>w. (w - z) ^ nat zn) z =
+              pochhammer (of_nat (Suc (nat zn) - j)) j * 0 ^ (nat zn - j)"
+        by (subst higher_deriv_power) auto
+      also have "\<dots> = (if j = nat zn then fact j else 0)"
+        by (auto simp: not_less pochhammer_0_left pochhammer_fact)
+      also have "of_nat (i choose j) * \<dots> * (deriv ^^ (i - j)) g z =
+                   (if j = nat zn then of_nat (i choose (nat zn)) * fact (nat zn)
+                        * (deriv ^^ (i - nat zn)) g z else 0)"
+        by simp
+      finally show ?case .
+    qed
+    also have "\<dots> = (if i \<ge> zn then A i else 0)"
+      by (auto simp: A_def)
+    finally show "(deriv ^^ i) f z = \<dots>" .
+  qed
+
+  have False when "n<zn"
+  proof -
+    have "(deriv ^^ nat n) f z = 0"
+      using deriv_A[of "nat n"] that \<open>n\<ge>0\<close> by auto
+    with nz show False by auto
+  qed
+  moreover have "n\<le>zn"
+  proof -
+    have "g z \<noteq> 0" using e_fac[rule_format,of z] \<open>e>0\<close> by simp
+    then have "(deriv ^^ nat zn) f z \<noteq> 0"
+      using deriv_A[of "nat zn"] by(auto simp add:A_def)
+    then have "nat zn \<ge> nat n" using zero[of "nat zn"] by linarith
+    moreover have "zn\<ge>0" using e_if by (auto split:if_splits)
+    ultimately show ?thesis using nat_le_eq_zle by blast
+  qed
+  ultimately show ?thesis unfolding zn_def by fastforce
+qed
+
+lemma
+  assumes "eventually (\<lambda>z. f z = g z) (at z)" "z = z'"
+  shows zorder_cong:"zorder f z = zorder g z'" and zor_poly_cong:"zor_poly f z = zor_poly g z'"
+proof -
+  define P where "P = (\<lambda>ff n h r. 0 < r \<and> h holomorphic_on cball z r \<and> h z\<noteq>0
+                    \<and> (\<forall>w\<in>cball z r - {z}. ff w = h w * (w-z) powr (of_int n) \<and> h w\<noteq>0))"
+  have "(\<exists>r. P f n h r) = (\<exists>r. P g n h r)" for n h
+  proof -
+    have *: "\<exists>r. P g n h r" if "\<exists>r. P f n h r" and "eventually (\<lambda>x. f x = g x) (at z)" for f g
+    proof -
+      from that(1) obtain r1 where r1_P:"P f n h r1" by auto
+      from that(2) obtain r2 where "r2>0" and r2_dist:"\<forall>x. x \<noteq> z \<and> dist x z \<le> r2 \<longrightarrow> f x = g x"
+        unfolding eventually_at_le by auto
+      define r where "r=min r1 r2"
+      have "r>0" "h z\<noteq>0" using r1_P \<open>r2>0\<close> unfolding r_def P_def by auto
+      moreover have "h holomorphic_on cball z r"
+        using r1_P unfolding P_def r_def by auto
+      moreover have "g w = h w * (w - z) powr of_int n \<and> h w \<noteq> 0" when "w\<in>cball z r - {z}" for w
+      proof -
+        have "f w = h w * (w - z) powr of_int n \<and> h w \<noteq> 0"
+          using r1_P that unfolding P_def r_def by auto
+        moreover have "f w=g w" using r2_dist[rule_format,of w] that unfolding r_def
+          by (simp add: dist_commute)
+        ultimately show ?thesis by simp
+      qed
+      ultimately show ?thesis unfolding P_def by auto
+    qed
+    from assms have eq': "eventually (\<lambda>z. g z = f z) (at z)"
+      by (simp add: eq_commute)
+    show ?thesis
+      by (rule iffI[OF *[OF _ assms(1)] *[OF _ eq']])
+  qed
+  then show "zorder f z = zorder g z'" "zor_poly f z = zor_poly g z'"
+      using \<open>z=z'\<close> unfolding P_def zorder_def zor_poly_def by auto
+qed
+
+lemma zorder_nonzero_div_power:
+  assumes "open s" "z \<in> s" "f holomorphic_on s" "f z \<noteq> 0" "n > 0"
+  shows  "zorder (\<lambda>w. f w / (w - z) ^ n) z = - n"
+  apply (rule zorder_eqI[OF \<open>open s\<close> \<open>z\<in>s\<close> \<open>f holomorphic_on s\<close> \<open>f z\<noteq>0\<close>])
+  apply (subst powr_of_int)
+  using \<open>n>0\<close> by (auto simp add:field_simps)
+
+lemma zor_poly_eq:
+  assumes "isolated_singularity_at f z" "not_essential f z" "\<exists>\<^sub>F w in at z. f w \<noteq> 0"
+  shows "eventually (\<lambda>w. zor_poly f z w = f w * (w - z) powr - zorder f z) (at z)"
+proof -
+  obtain r where r:"r>0"
+       "(\<forall>w\<in>cball z r - {z}. f w = zor_poly f z w * (w - z) powr of_int (zorder f z))"
+    using zorder_exist[OF assms] by blast
+  then have *: "\<forall>w\<in>ball z r - {z}. zor_poly f z w = f w * (w - z) powr - zorder f z"
+    by (auto simp: field_simps powr_minus)
+  have "eventually (\<lambda>w. w \<in> ball z r - {z}) (at z)"
+    using r eventually_at_ball'[of r z UNIV] by auto
+  thus ?thesis by eventually_elim (insert *, auto)
+qed
+
+lemma zor_poly_zero_eq:
+  assumes "f holomorphic_on s" "open s" "connected s" "z \<in> s" "\<exists>w\<in>s. f w \<noteq> 0"
+  shows "eventually (\<lambda>w. zor_poly f z w = f w / (w - z) ^ nat (zorder f z)) (at z)"
+proof -
+  obtain r where r:"r>0"
+       "(\<forall>w\<in>cball z r - {z}. f w = zor_poly f z w * (w - z) ^ nat (zorder f z))"
+    using zorder_exist_zero[OF assms] by auto
+  then have *: "\<forall>w\<in>ball z r - {z}. zor_poly f z w = f w / (w - z) ^ nat (zorder f z)"
+    by (auto simp: field_simps powr_minus)
+  have "eventually (\<lambda>w. w \<in> ball z r - {z}) (at z)"
+    using r eventually_at_ball'[of r z UNIV] by auto
+  thus ?thesis by eventually_elim (insert *, auto)
+qed
+
+lemma zor_poly_pole_eq:
+  assumes f_iso:"isolated_singularity_at f z" "is_pole f z"
+  shows "eventually (\<lambda>w. zor_poly f z w = f w * (w - z) ^ nat (- zorder f z)) (at z)"
+proof -
+  obtain e where [simp]:"e>0" and f_holo:"f holomorphic_on ball z e - {z}"
+    using f_iso analytic_imp_holomorphic unfolding isolated_singularity_at_def by blast
+  obtain r where r:"r>0"
+       "(\<forall>w\<in>cball z r - {z}. f w = zor_poly f z w / (w - z) ^ nat (- zorder f z))"
+    using zorder_exist_pole[OF f_holo,simplified,OF \<open>is_pole f z\<close>] by auto
+  then have *: "\<forall>w\<in>ball z r - {z}. zor_poly f z w = f w * (w - z) ^ nat (- zorder f z)"
+    by (auto simp: field_simps)
+  have "eventually (\<lambda>w. w \<in> ball z r - {z}) (at z)"
+    using r eventually_at_ball'[of r z UNIV] by auto
+  thus ?thesis by eventually_elim (insert *, auto)
+qed
+
+lemma zor_poly_eqI:
+  fixes f :: "complex \<Rightarrow> complex" and z0 :: complex
+  defines "n \<equiv> zorder f z0"
+  assumes "isolated_singularity_at f z0" "not_essential f z0" "\<exists>\<^sub>F w in at z0. f w \<noteq> 0"
+  assumes lim: "((\<lambda>x. f (g x) * (g x - z0) powr - n) \<longlongrightarrow> c) F"
+  assumes g: "filterlim g (at z0) F" and "F \<noteq> bot"
+  shows   "zor_poly f z0 z0 = c"
+proof -
+  from zorder_exist[OF assms(2-4)] obtain r where
+    r: "r > 0" "zor_poly f z0 holomorphic_on cball z0 r"
+       "\<And>w. w \<in> cball z0 r - {z0} \<Longrightarrow> f w = zor_poly f z0 w * (w - z0) powr n"
+    unfolding n_def by blast
+  from r(1) have "eventually (\<lambda>w. w \<in> ball z0 r \<and> w \<noteq> z0) (at z0)"
+    using eventually_at_ball'[of r z0 UNIV] by auto
+  hence "eventually (\<lambda>w. zor_poly f z0 w = f w * (w - z0) powr - n) (at z0)"
+    by eventually_elim (insert r, auto simp: field_simps powr_minus)
+  moreover have "continuous_on (ball z0 r) (zor_poly f z0)"
+    using r by (intro holomorphic_on_imp_continuous_on) auto
+  with r(1,2) have "isCont (zor_poly f z0) z0"
+    by (auto simp: continuous_on_eq_continuous_at)
+  hence "(zor_poly f z0 \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
+    unfolding isCont_def .
+  ultimately have "((\<lambda>w. f w * (w - z0) powr - n) \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
+    by (blast intro: Lim_transform_eventually)
+  hence "((\<lambda>x. f (g x) * (g x - z0) powr - n) \<longlongrightarrow> zor_poly f z0 z0) F"
+    by (rule filterlim_compose[OF _ g])
+  from tendsto_unique[OF \<open>F \<noteq> bot\<close> this lim] show ?thesis .
+qed
+
+lemma zor_poly_zero_eqI:
+  fixes f :: "complex \<Rightarrow> complex" and z0 :: complex
+  defines "n \<equiv> zorder f z0"
+  assumes "f holomorphic_on A" "open A" "connected A" "z0 \<in> A" "\<exists>z\<in>A. f z \<noteq> 0"
+  assumes lim: "((\<lambda>x. f (g x) / (g x - z0) ^ nat n) \<longlongrightarrow> c) F"
+  assumes g: "filterlim g (at z0) F" and "F \<noteq> bot"
+  shows   "zor_poly f z0 z0 = c"
+proof -
+  from zorder_exist_zero[OF assms(2-6)] obtain r where
+    r: "r > 0" "cball z0 r \<subseteq> A" "zor_poly f z0 holomorphic_on cball z0 r"
+       "\<And>w. w \<in> cball z0 r \<Longrightarrow> f w = zor_poly f z0 w * (w - z0) ^ nat n"
+    unfolding n_def by blast
+  from r(1) have "eventually (\<lambda>w. w \<in> ball z0 r \<and> w \<noteq> z0) (at z0)"
+    using eventually_at_ball'[of r z0 UNIV] by auto
+  hence "eventually (\<lambda>w. zor_poly f z0 w = f w / (w - z0) ^ nat n) (at z0)"
+    by eventually_elim (insert r, auto simp: field_simps)
+  moreover have "continuous_on (ball z0 r) (zor_poly f z0)"
+    using r by (intro holomorphic_on_imp_continuous_on) auto
+  with r(1,2) have "isCont (zor_poly f z0) z0"
+    by (auto simp: continuous_on_eq_continuous_at)
+  hence "(zor_poly f z0 \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
+    unfolding isCont_def .
+  ultimately have "((\<lambda>w. f w / (w - z0) ^ nat n) \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
+    by (blast intro: Lim_transform_eventually)
+  hence "((\<lambda>x. f (g x) / (g x - z0) ^ nat n) \<longlongrightarrow> zor_poly f z0 z0) F"
+    by (rule filterlim_compose[OF _ g])
+  from tendsto_unique[OF \<open>F \<noteq> bot\<close> this lim] show ?thesis .
+qed
+
+lemma zor_poly_pole_eqI:
+  fixes f :: "complex \<Rightarrow> complex" and z0 :: complex
+  defines "n \<equiv> zorder f z0"
+  assumes f_iso:"isolated_singularity_at f z0" and "is_pole f z0"
+  assumes lim: "((\<lambda>x. f (g x) * (g x - z0) ^ nat (-n)) \<longlongrightarrow> c) F"
+  assumes g: "filterlim g (at z0) F" and "F \<noteq> bot"
+  shows   "zor_poly f z0 z0 = c"
+proof -
+  obtain r where r: "r > 0"  "zor_poly f z0 holomorphic_on cball z0 r"
+  proof -
+    have "\<exists>\<^sub>F w in at z0. f w \<noteq> 0"
+      using non_zero_neighbour_pole[OF \<open>is_pole f z0\<close>] by (auto elim:eventually_frequentlyE)
+    moreover have "not_essential f z0" unfolding not_essential_def using \<open>is_pole f z0\<close> by simp
+    ultimately show ?thesis using that zorder_exist[OF f_iso,folded n_def] by auto
+  qed
+  from r(1) have "eventually (\<lambda>w. w \<in> ball z0 r \<and> w \<noteq> z0) (at z0)"
+    using eventually_at_ball'[of r z0 UNIV] by auto
+  have "eventually (\<lambda>w. zor_poly f z0 w = f w * (w - z0) ^ nat (-n)) (at z0)"
+    using zor_poly_pole_eq[OF f_iso \<open>is_pole f z0\<close>] unfolding n_def .
+  moreover have "continuous_on (ball z0 r) (zor_poly f z0)"
+    using r by (intro holomorphic_on_imp_continuous_on) auto
+  with r(1,2) have "isCont (zor_poly f z0) z0"
+    by (auto simp: continuous_on_eq_continuous_at)
+  hence "(zor_poly f z0 \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
+    unfolding isCont_def .
+  ultimately have "((\<lambda>w. f w * (w - z0) ^ nat (-n)) \<longlongrightarrow> zor_poly f z0 z0) (at z0)"
+    by (blast intro: Lim_transform_eventually)
+  hence "((\<lambda>x. f (g x) * (g x - z0) ^ nat (-n)) \<longlongrightarrow> zor_poly f z0 z0) F"
+    by (rule filterlim_compose[OF _ g])
+  from tendsto_unique[OF \<open>F \<noteq> bot\<close> this lim] show ?thesis .
+qed
+
+lemma residue_simple_pole:
+  assumes "isolated_singularity_at f z0"
+  assumes "is_pole f z0" "zorder f z0 = - 1"
+  shows   "residue f z0 = zor_poly f z0 z0"
+  using assms by (subst residue_pole_order) simp_all
+
+lemma residue_simple_pole_limit:
+  assumes "isolated_singularity_at f z0"
+  assumes "is_pole f z0" "zorder f z0 = - 1"
+  assumes "((\<lambda>x. f (g x) * (g x - z0)) \<longlongrightarrow> c) F"
+  assumes "filterlim g (at z0) F" "F \<noteq> bot"
+  shows   "residue f z0 = c"
+proof -
+  have "residue f z0 = zor_poly f z0 z0"
+    by (rule residue_simple_pole assms)+
+  also have "\<dots> = c"
+    apply (rule zor_poly_pole_eqI)
+    using assms by auto
+  finally show ?thesis .
+qed
+
+lemma lhopital_complex_simple:
+  assumes "(f has_field_derivative f') (at z)"
+  assumes "(g has_field_derivative g') (at z)"
+  assumes "f z = 0" "g z = 0" "g' \<noteq> 0" "f' / g' = c"
+  shows   "((\<lambda>w. f w / g w) \<longlongrightarrow> c) (at z)"
+proof -
+  have "eventually (\<lambda>w. w \<noteq> z) (at z)"
+    by (auto simp: eventually_at_filter)
+  hence "eventually (\<lambda>w. ((f w - f z) / (w - z)) / ((g w - g z) / (w - z)) = f w / g w) (at z)"
+    by eventually_elim (simp add: assms field_split_simps)
+  moreover have "((\<lambda>w. ((f w - f z) / (w - z)) / ((g w - g z) / (w - z))) \<longlongrightarrow> f' / g') (at z)"
+    by (intro tendsto_divide has_field_derivativeD assms)
+  ultimately have "((\<lambda>w. f w / g w) \<longlongrightarrow> f' / g') (at z)"
+    by (blast intro: Lim_transform_eventually)
+  with assms show ?thesis by simp
+qed
+
+lemma
+  assumes f_holo:"f holomorphic_on s" and g_holo:"g holomorphic_on s"
+          and "open s" "connected s" "z \<in> s"
+  assumes g_deriv:"(g has_field_derivative g') (at z)"
+  assumes "f z \<noteq> 0" "g z = 0" "g' \<noteq> 0"
+  shows   porder_simple_pole_deriv: "zorder (\<lambda>w. f w / g w) z = - 1"
+    and   residue_simple_pole_deriv: "residue (\<lambda>w. f w / g w) z = f z / g'"
+proof -
+  have [simp]:"isolated_singularity_at f z" "isolated_singularity_at g z"
+    using isolated_singularity_at_holomorphic[OF _ \<open>open s\<close> \<open>z\<in>s\<close>] f_holo g_holo
+    by (meson Diff_subset holomorphic_on_subset)+
+  have [simp]:"not_essential f z" "not_essential g z"
+    unfolding not_essential_def using f_holo g_holo assms(3,5)
+    by (meson continuous_on_eq_continuous_at continuous_within holomorphic_on_imp_continuous_on)+
+  have g_nconst:"\<exists>\<^sub>F w in at z. g w \<noteq>0 "
+  proof (rule ccontr)
+    assume "\<not> (\<exists>\<^sub>F w in at z. g w \<noteq> 0)"
+    then have "\<forall>\<^sub>F w in nhds z. g w = 0"
+      unfolding eventually_at eventually_nhds frequently_at using \<open>g z = 0\<close>
+      by (metis open_ball UNIV_I centre_in_ball dist_commute mem_ball)
+    then have "deriv g z = deriv (\<lambda>_. 0) z"
+      by (intro deriv_cong_ev) auto
+    then have "deriv g z = 0" by auto
+    then have "g' = 0" using g_deriv DERIV_imp_deriv by blast
+    then show False using \<open>g'\<noteq>0\<close> by auto
+  qed
+
+  have "zorder (\<lambda>w. f w / g w) z = zorder f z - zorder g z"
+  proof -
+    have "\<forall>\<^sub>F w in at z. f w \<noteq>0 \<and> w\<in>s"
+      apply (rule non_zero_neighbour_alt)
+      using assms by auto
+    with g_nconst have "\<exists>\<^sub>F w in at z. f w * g w \<noteq> 0"
+      by (elim frequently_rev_mp eventually_rev_mp,auto)
+    then show ?thesis using zorder_divide[of f z g] by auto
+  qed
+  moreover have "zorder f z=0"
+    apply (rule zorder_zero_eqI[OF f_holo \<open>open s\<close> \<open>z\<in>s\<close>])
+    using \<open>f z\<noteq>0\<close> by auto
+  moreover have "zorder g z=1"
+    apply (rule zorder_zero_eqI[OF g_holo \<open>open s\<close> \<open>z\<in>s\<close>])
+    subgoal using assms(8) by auto
+    subgoal using DERIV_imp_deriv assms(9) g_deriv by auto
+    subgoal by simp
+    done
+  ultimately show "zorder (\<lambda>w. f w / g w) z = - 1" by auto
+
+  show "residue (\<lambda>w. f w / g w) z = f z / g'"
+  proof (rule residue_simple_pole_limit[where g=id and F="at z",simplified])
+    show "zorder (\<lambda>w. f w / g w) z = - 1" by fact
+    show "isolated_singularity_at (\<lambda>w. f w / g w) z"
+      by (auto intro: singularity_intros)
+    show "is_pole (\<lambda>w. f w / g w) z"
+    proof (rule is_pole_divide)
+      have "\<forall>\<^sub>F x in at z. g x \<noteq> 0"
+        apply (rule non_zero_neighbour)
+        using g_nconst by auto
+      moreover have "g \<midarrow>z\<rightarrow> 0"
+        using DERIV_isCont assms(8) continuous_at g_deriv by force
+      ultimately show "filterlim g (at 0) (at z)" unfolding filterlim_at by simp
+      show "isCont f z"
+        using assms(3,5) continuous_on_eq_continuous_at f_holo holomorphic_on_imp_continuous_on
+        by auto
+      show "f z \<noteq> 0" by fact
+    qed
+    show "filterlim id (at z) (at z)" by (simp add: filterlim_iff)
+    have "((\<lambda>w. (f w * (w - z)) / g w) \<longlongrightarrow> f z / g') (at z)"
+    proof (rule lhopital_complex_simple)
+      show "((\<lambda>w. f w * (w - z)) has_field_derivative f z) (at z)"
+        using assms by (auto intro!: derivative_eq_intros holomorphic_derivI[OF f_holo])
+      show "(g has_field_derivative g') (at z)" by fact
+    qed (insert assms, auto)
+    then show "((\<lambda>w. (f w / g w) * (w - z)) \<longlongrightarrow> f z / g') (at z)"
+      by (simp add: field_split_simps)
+  qed
+qed
+
+subsection \<open>The argument principle\<close>
+
+theorem argument_principle:
+  fixes f::"complex \<Rightarrow> complex" and poles s:: "complex set"
+  defines "pz \<equiv> {w. f w = 0 \<or> w \<in> poles}" \<comment> \<open>\<^term>\<open>pz\<close> is the set of poles and zeros\<close>
+  assumes "open s" and
+          "connected s" and
+          f_holo:"f holomorphic_on s-poles" and
+          h_holo:"h holomorphic_on s" and
+          "valid_path g" and
+          loop:"pathfinish g = pathstart g" and
+          path_img:"path_image g \<subseteq> s - pz" and
+          homo:"\<forall>z. (z \<notin> s) \<longrightarrow> winding_number g z = 0" and
+          finite:"finite pz" and
+          poles:"\<forall>p\<in>poles. is_pole f p"
+  shows "contour_integral g (\<lambda>x. deriv f x * h x / f x) = 2 * pi * \<i> *
+          (\<Sum>p\<in>pz. winding_number g p * h p * zorder f p)"
+    (is "?L=?R")
+proof -
+  define c where "c \<equiv> 2 * complex_of_real pi * \<i> "
+  define ff where "ff \<equiv> (\<lambda>x. deriv f x * h x / f x)"
+  define cont where "cont \<equiv> \<lambda>ff p e. (ff has_contour_integral c * zorder f p * h p ) (circlepath p e)"
+  define avoid where "avoid \<equiv> \<lambda>p e. \<forall>w\<in>cball p e. w \<in> s \<and> (w \<noteq> p \<longrightarrow> w \<notin> pz)"
+
+  have "\<exists>e>0. avoid p e \<and> (p\<in>pz \<longrightarrow> cont ff p e)" when "p\<in>s" for p
+  proof -
+    obtain e1 where "e1>0" and e1_avoid:"avoid p e1"
+      using finite_cball_avoid[OF \<open>open s\<close> finite] \<open>p\<in>s\<close> unfolding avoid_def by auto
+    have "\<exists>e2>0. cball p e2 \<subseteq> ball p e1 \<and> cont ff p e2" when "p\<in>pz"
+    proof -
+      define po where "po \<equiv> zorder f p"
+      define pp where "pp \<equiv> zor_poly f p"
+      define f' where "f' \<equiv> \<lambda>w. pp w * (w - p) powr po"
+      define ff' where "ff' \<equiv> (\<lambda>x. deriv f' x * h x / f' x)"
+      obtain r where "pp p\<noteq>0" "r>0" and
+          "r<e1" and
+          pp_holo:"pp holomorphic_on cball p r" and
+          pp_po:"(\<forall>w\<in>cball p r-{p}. f w = pp w * (w - p) powr po \<and> pp w \<noteq> 0)"
+      proof -
+        have "isolated_singularity_at f p"
+        proof -
+          have "f holomorphic_on ball p e1 - {p}"
+            apply (intro holomorphic_on_subset[OF f_holo])
+            using e1_avoid \<open>p\<in>pz\<close> unfolding avoid_def pz_def by force
+          then show ?thesis unfolding isolated_singularity_at_def
+            using \<open>e1>0\<close> analytic_on_open open_delete by blast
+        qed
+        moreover have "not_essential f p"
+        proof (cases "is_pole f p")
+          case True
+          then show ?thesis unfolding not_essential_def by auto
+        next
+          case False
+          then have "p\<in>s-poles" using \<open>p\<in>s\<close> poles unfolding pz_def by auto
+          moreover have "open (s-poles)"
+            using \<open>open s\<close>
+            apply (elim open_Diff)
+            apply (rule finite_imp_closed)
+            using finite unfolding pz_def by simp
+          ultimately have "isCont f p"
+            using holomorphic_on_imp_continuous_on[OF f_holo] continuous_on_eq_continuous_at
+            by auto
+          then show ?thesis unfolding isCont_def not_essential_def by auto
+        qed
+        moreover have "\<exists>\<^sub>F w in at p. f w \<noteq> 0 "
+        proof (rule ccontr)
+          assume "\<not> (\<exists>\<^sub>F w in at p. f w \<noteq> 0)"
+          then have "\<forall>\<^sub>F w in at p. f w= 0" unfolding frequently_def by auto
+          then obtain rr where "rr>0" "\<forall>w\<in>ball p rr - {p}. f w =0"
+            unfolding eventually_at by (auto simp add:dist_commute)
+          then have "ball p rr - {p} \<subseteq> {w\<in>ball p rr-{p}. f w=0}" by blast
+          moreover have "infinite (ball p rr - {p})" using \<open>rr>0\<close> using finite_imp_not_open by fastforce
+          ultimately have "infinite {w\<in>ball p rr-{p}. f w=0}" using infinite_super by blast
+          then have "infinite pz"
+            unfolding pz_def infinite_super by auto
+          then show False using \<open>finite pz\<close> by auto
+        qed
+        ultimately obtain r where "pp p \<noteq> 0" and r:"r>0" "pp holomorphic_on cball p r"
+                  "(\<forall>w\<in>cball p r - {p}. f w = pp w * (w - p) powr of_int po \<and> pp w \<noteq> 0)"
+          using zorder_exist[of f p,folded po_def pp_def] by auto
+        define r1 where "r1=min r e1 / 2"
+        have "r1<e1" unfolding r1_def using \<open>e1>0\<close> \<open>r>0\<close> by auto
+        moreover have "r1>0" "pp holomorphic_on cball p r1"
+                  "(\<forall>w\<in>cball p r1 - {p}. f w = pp w * (w - p) powr of_int po \<and> pp w \<noteq> 0)"
+          unfolding r1_def using \<open>e1>0\<close> r by auto
+        ultimately show ?thesis using that \<open>pp p\<noteq>0\<close> by auto
+      qed
+
+      define e2 where "e2 \<equiv> r/2"
+      have "e2>0" using \<open>r>0\<close> unfolding e2_def by auto
+      define anal where "anal \<equiv> \<lambda>w. deriv pp w * h w / pp w"
+      define prin where "prin \<equiv> \<lambda>w. po * h w / (w - p)"
+      have "((\<lambda>w.  prin w + anal w) has_contour_integral c * po * h p) (circlepath p e2)"
+      proof (rule has_contour_integral_add[of _ _ _ _ 0,simplified])
+        have "ball p r \<subseteq> s"
+          using \<open>r<e1\<close> avoid_def ball_subset_cball e1_avoid by (simp add: subset_eq)
+        then have "cball p e2 \<subseteq> s"
+          using \<open>r>0\<close> unfolding e2_def by auto
+        then have "(\<lambda>w. po * h w) holomorphic_on cball p e2"
+          using h_holo by (auto intro!: holomorphic_intros)
+        then show "(prin has_contour_integral c * po * h p ) (circlepath p e2)"
+          using Cauchy_integral_circlepath_simple[folded c_def, of "\<lambda>w. po * h w"] \<open>e2>0\<close>
+          unfolding prin_def by (auto simp add: mult.assoc)
+        have "anal holomorphic_on ball p r" unfolding anal_def
+          using pp_holo h_holo pp_po \<open>ball p r \<subseteq> s\<close> \<open>pp p\<noteq>0\<close>
+          by (auto intro!: holomorphic_intros)
+        then show "(anal has_contour_integral 0) (circlepath p e2)"
+          using e2_def \<open>r>0\<close>
+          by (auto elim!: Cauchy_theorem_disc_simple)
+      qed
+      then have "cont ff' p e2" unfolding cont_def po_def
+      proof (elim has_contour_integral_eq)
+        fix w assume "w \<in> path_image (circlepath p e2)"
+        then have "w\<in>ball p r" and "w\<noteq>p" unfolding e2_def using \<open>r>0\<close> by auto
+        define wp where "wp \<equiv> w-p"
+        have "wp\<noteq>0" and "pp w \<noteq>0"
+          unfolding wp_def using \<open>w\<noteq>p\<close> \<open>w\<in>ball p r\<close> pp_po by auto
+        moreover have der_f':"deriv f' w = po * pp w * (w-p) powr (po - 1) + deriv pp w * (w-p) powr po"
+        proof (rule DERIV_imp_deriv)
+          have "(pp has_field_derivative (deriv pp w)) (at w)"
+            using DERIV_deriv_iff_has_field_derivative pp_holo \<open>w\<noteq>p\<close>
+            by (meson open_ball \<open>w \<in> ball p r\<close> ball_subset_cball holomorphic_derivI holomorphic_on_subset)
+          then show " (f' has_field_derivative of_int po * pp w * (w - p) powr of_int (po - 1)
+                  + deriv pp w * (w - p) powr of_int po) (at w)"
+            unfolding f'_def using \<open>w\<noteq>p\<close>
+            by (auto intro!: derivative_eq_intros DERIV_cong[OF has_field_derivative_powr_of_int])
+        qed
+        ultimately show "prin w + anal w = ff' w"
+          unfolding ff'_def prin_def anal_def
+          apply simp
+          apply (unfold f'_def)
+          apply (fold wp_def)
+          apply (auto simp add:field_simps)
+          by (metis (no_types, lifting) diff_add_cancel mult.commute powr_add powr_to_1)
+      qed
+      then have "cont ff p e2" unfolding cont_def
+      proof (elim has_contour_integral_eq)
+        fix w assume "w \<in> path_image (circlepath p e2)"
+        then have "w\<in>ball p r" and "w\<noteq>p" unfolding e2_def using \<open>r>0\<close> by auto
+        have "deriv f' w =  deriv f w"
+        proof (rule complex_derivative_transform_within_open[where s="ball p r - {p}"])
+          show "f' holomorphic_on ball p r - {p}" unfolding f'_def using pp_holo
+            by (auto intro!: holomorphic_intros)
+        next
+          have "ball p e1 - {p} \<subseteq> s - poles"
+            using ball_subset_cball e1_avoid[unfolded avoid_def] unfolding pz_def
+            by auto
+          then have "ball p r - {p} \<subseteq> s - poles"
+            apply (elim dual_order.trans)
+            using \<open>r<e1\<close> by auto
+          then show "f holomorphic_on ball p r - {p}" using f_holo
+            by auto
+        next
+          show "open (ball p r - {p})" by auto
+          show "w \<in> ball p r - {p}" using \<open>w\<in>ball p r\<close> \<open>w\<noteq>p\<close> by auto
+        next
+          fix x assume "x \<in> ball p r - {p}"
+          then show "f' x = f x"
+            using pp_po unfolding f'_def by auto
+        qed
+        moreover have " f' w  =  f w "
+          using \<open>w \<in> ball p r\<close> ball_subset_cball subset_iff pp_po \<open>w\<noteq>p\<close>
+          unfolding f'_def by auto
+        ultimately show "ff' w = ff w"
+          unfolding ff'_def ff_def by simp
+      qed
+      moreover have "cball p e2 \<subseteq> ball p e1"
+        using \<open>0 < r\<close> \<open>r<e1\<close> e2_def by auto
+      ultimately show ?thesis using \<open>e2>0\<close> by auto
+    qed
+    then obtain e2 where e2:"p\<in>pz \<longrightarrow> e2>0 \<and> cball p e2 \<subseteq> ball p e1 \<and> cont ff p e2"
+      by auto
+    define e4 where "e4 \<equiv> if p\<in>pz then e2 else  e1"
+    have "e4>0" using e2 \<open>e1>0\<close> unfolding e4_def by auto
+    moreover have "avoid p e4" using e2 \<open>e1>0\<close> e1_avoid unfolding e4_def avoid_def by auto
+    moreover have "p\<in>pz \<longrightarrow> cont ff p e4"
+      by (auto simp add: e2 e4_def)
+    ultimately show ?thesis by auto
+  qed
+  then obtain get_e where get_e:"\<forall>p\<in>s. get_e p>0 \<and> avoid p (get_e p)
+      \<and> (p\<in>pz \<longrightarrow> cont ff p (get_e p))"
+    by metis
+  define ci where "ci \<equiv> \<lambda>p. contour_integral (circlepath p (get_e p)) ff"
+  define w where "w \<equiv> \<lambda>p. winding_number g p"
+  have "contour_integral g ff = (\<Sum>p\<in>pz. w p * ci p)" unfolding ci_def w_def
+  proof (rule Cauchy_theorem_singularities[OF \<open>open s\<close> \<open>connected s\<close> finite _ \<open>valid_path g\<close> loop
+        path_img homo])
+    have "open (s - pz)" using open_Diff[OF _ finite_imp_closed[OF finite]] \<open>open s\<close> by auto
+    then show "ff holomorphic_on s - pz" unfolding ff_def using f_holo h_holo
+      by (auto intro!: holomorphic_intros simp add:pz_def)
+  next
+    show "\<forall>p\<in>s. 0 < get_e p \<and> (\<forall>w\<in>cball p (get_e p). w \<in> s \<and> (w \<noteq> p \<longrightarrow> w \<notin> pz))"
+      using get_e using avoid_def by blast
+  qed
+  also have "... = (\<Sum>p\<in>pz. c * w p * h p * zorder f p)"
+  proof (rule sum.cong[of pz pz,simplified])
+    fix p assume "p \<in> pz"
+    show "w p * ci p = c * w p * h p * (zorder f p)"
+    proof (cases "p\<in>s")
+      assume "p \<in> s"
+      have "ci p = c * h p * (zorder f p)" unfolding ci_def
+        apply (rule contour_integral_unique)
+        using get_e \<open>p\<in>s\<close> \<open>p\<in>pz\<close> unfolding cont_def by (metis mult.assoc mult.commute)
+      thus ?thesis by auto
+    next
+      assume "p\<notin>s"
+      then have "w p=0" using homo unfolding w_def by auto
+      then show ?thesis by auto
+    qed
+  qed
+  also have "... = c*(\<Sum>p\<in>pz. w p * h p * zorder f p)"
+    unfolding sum_distrib_left by (simp add:algebra_simps)
+  finally have "contour_integral g ff = c * (\<Sum>p\<in>pz. w p * h p * of_int (zorder f p))" .
+  then show ?thesis unfolding ff_def c_def w_def by simp
+qed
+
+subsection \<open>Rouche's theorem \<close>
+
+theorem Rouche_theorem:
+  fixes f g::"complex \<Rightarrow> complex" and s:: "complex set"
+  defines "fg\<equiv>(\<lambda>p. f p + g p)"
+  defines "zeros_fg\<equiv>{p. fg p = 0}" and "zeros_f\<equiv>{p. f p = 0}"
+  assumes
+    "open s" and "connected s" and
+    "finite zeros_fg" and
+    "finite zeros_f" and
+    f_holo:"f holomorphic_on s" and
+    g_holo:"g holomorphic_on s" and
+    "valid_path \<gamma>" and
+    loop:"pathfinish \<gamma> = pathstart \<gamma>" and
+    path_img:"path_image \<gamma> \<subseteq> s " and
+    path_less:"\<forall>z\<in>path_image \<gamma>. cmod(f z) > cmod(g z)" and
+    homo:"\<forall>z. (z \<notin> s) \<longrightarrow> winding_number \<gamma> z = 0"
+  shows "(\<Sum>p\<in>zeros_fg. winding_number \<gamma> p * zorder fg p)
+          = (\<Sum>p\<in>zeros_f. winding_number \<gamma> p * zorder f p)"
+proof -
+  have path_fg:"path_image \<gamma> \<subseteq> s - zeros_fg"
+  proof -
+    have False when "z\<in>path_image \<gamma>" and "f z + g z=0" for z
+    proof -
+      have "cmod (f z) > cmod (g z)" using \<open>z\<in>path_image \<gamma>\<close> path_less by auto
+      moreover have "f z = - g z"  using \<open>f z + g z =0\<close> by (simp add: eq_neg_iff_add_eq_0)
+      then have "cmod (f z) = cmod (g z)" by auto
+      ultimately show False by auto
+    qed
+    then show ?thesis unfolding zeros_fg_def fg_def using path_img by auto
+  qed
+  have path_f:"path_image \<gamma> \<subseteq> s - zeros_f"
+  proof -
+    have False when "z\<in>path_image \<gamma>" and "f z =0" for z
+    proof -
+      have "cmod (g z) < cmod (f z) " using \<open>z\<in>path_image \<gamma>\<close> path_less by auto
+      then have "cmod (g z) < 0" using \<open>f z=0\<close> by auto
+      then show False by auto
+    qed
+    then show ?thesis unfolding zeros_f_def using path_img by auto
+  qed
+  define w where "w \<equiv> \<lambda>p. winding_number \<gamma> p"
+  define c where "c \<equiv> 2 * complex_of_real pi * \<i>"
+  define h where "h \<equiv> \<lambda>p. g p / f p + 1"
+  obtain spikes
+    where "finite spikes" and spikes: "\<forall>x\<in>{0..1} - spikes. \<gamma> differentiable at x"
+    using \<open>valid_path \<gamma>\<close>
+    by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
+  have h_contour:"((\<lambda>x. deriv h x / h x) has_contour_integral 0) \<gamma>"
+  proof -
+    have outside_img:"0 \<in> outside (path_image (h o \<gamma>))"
+    proof -
+      have "h p \<in> ball 1 1" when "p\<in>path_image \<gamma>" for p
+      proof -
+        have "cmod (g p/f p) <1" using path_less[rule_format,OF that]
+          apply (cases "cmod (f p) = 0")
+          by (auto simp add: norm_divide)
+        then show ?thesis unfolding h_def by (auto simp add:dist_complex_def)
+      qed
+      then have "path_image (h o \<gamma>) \<subseteq> ball 1 1"
+        by (simp add: image_subset_iff path_image_compose)
+      moreover have " (0::complex) \<notin> ball 1 1" by (simp add: dist_norm)
+      ultimately show "?thesis"
+        using  convex_in_outside[of "ball 1 1" 0] outside_mono by blast
+    qed
+    have valid_h:"valid_path (h \<circ> \<gamma>)"
+    proof (rule valid_path_compose_holomorphic[OF \<open>valid_path \<gamma>\<close> _ _ path_f])
+      show "h holomorphic_on s - zeros_f"
+        unfolding h_def using f_holo g_holo
+        by (auto intro!: holomorphic_intros simp add:zeros_f_def)
+    next
+      show "open (s - zeros_f)" using \<open>finite zeros_f\<close> \<open>open s\<close> finite_imp_closed
+        by auto
+    qed
+    have "((\<lambda>z. 1/z) has_contour_integral 0) (h \<circ> \<gamma>)"
+    proof -
+      have "0 \<notin> path_image (h \<circ> \<gamma>)" using outside_img by (simp add: outside_def)
+      then have "((\<lambda>z. 1/z) has_contour_integral c * winding_number (h \<circ> \<gamma>) 0) (h \<circ> \<gamma>)"
+        using has_contour_integral_winding_number[of "h o \<gamma>" 0,simplified] valid_h
+        unfolding c_def by auto
+      moreover have "winding_number (h o \<gamma>) 0 = 0"
+      proof -
+        have "0 \<in> outside (path_image (h \<circ> \<gamma>))" using outside_img .
+        moreover have "path (h o \<gamma>)"
+          using valid_h  by (simp add: valid_path_imp_path)
+        moreover have "pathfinish (h o \<gamma>) = pathstart (h o \<gamma>)"
+          by (simp add: loop pathfinish_compose pathstart_compose)
+        ultimately show ?thesis using winding_number_zero_in_outside by auto
+      qed
+      ultimately show ?thesis by auto
+    qed
+    moreover have "vector_derivative (h \<circ> \<gamma>) (at x) = vector_derivative \<gamma> (at x) * deriv h (\<gamma> x)"
+      when "x\<in>{0..1} - spikes" for x
+    proof (rule vector_derivative_chain_at_general)
+      show "\<gamma> differentiable at x" using that \<open>valid_path \<gamma>\<close> spikes by auto
+    next
+      define der where "der \<equiv> \<lambda>p. (deriv g p * f p - g p * deriv f p)/(f p * f p)"
+      define t where "t \<equiv> \<gamma> x"
+      have "f t\<noteq>0" unfolding zeros_f_def t_def
+        by (metis DiffD1 image_eqI norm_not_less_zero norm_zero path_defs(4) path_less that)
+      moreover have "t\<in>s"
+        using contra_subsetD path_image_def path_fg t_def that by fastforce
+      ultimately have "(h has_field_derivative der t) (at t)"
+        unfolding h_def der_def using g_holo f_holo \<open>open s\<close>
+        by (auto intro!: holomorphic_derivI derivative_eq_intros)
+      then show "h field_differentiable at (\<gamma> x)"
+        unfolding t_def field_differentiable_def by blast
+    qed
+    then have " ((/) 1 has_contour_integral 0) (h \<circ> \<gamma>)
+                  = ((\<lambda>x. deriv h x / h x) has_contour_integral 0) \<gamma>"
+      unfolding has_contour_integral
+      apply (intro has_integral_spike_eq[OF negligible_finite, OF \<open>finite spikes\<close>])
+      by auto
+    ultimately show ?thesis by auto
+  qed
+  then have "contour_integral \<gamma> (\<lambda>x. deriv h x / h x) = 0"
+    using  contour_integral_unique by simp
+  moreover have "contour_integral \<gamma> (\<lambda>x. deriv fg x / fg x) = contour_integral \<gamma> (\<lambda>x. deriv f x / f x)
+      + contour_integral \<gamma> (\<lambda>p. deriv h p / h p)"
+  proof -
+    have "(\<lambda>p. deriv f p / f p) contour_integrable_on \<gamma>"
+    proof (rule contour_integrable_holomorphic_simple[OF _ _ \<open>valid_path \<gamma>\<close> path_f])
+      show "open (s - zeros_f)" using finite_imp_closed[OF \<open>finite zeros_f\<close>] \<open>open s\<close>
+        by auto
+      then show "(\<lambda>p. deriv f p / f p) holomorphic_on s - zeros_f"
+        using f_holo
+        by (auto intro!: holomorphic_intros simp add:zeros_f_def)
+    qed
+    moreover have "(\<lambda>p. deriv h p / h p) contour_integrable_on \<gamma>"
+      using h_contour
+      by (simp add: has_contour_integral_integrable)
+    ultimately have "contour_integral \<gamma> (\<lambda>x. deriv f x / f x + deriv h x / h x) =
+                        contour_integral \<gamma> (\<lambda>p. deriv f p / f p) + contour_integral \<gamma> (\<lambda>p. deriv h p / h p)"
+      using contour_integral_add[of "(\<lambda>p. deriv f p / f p)" \<gamma> "(\<lambda>p. deriv h p / h p)" ]
+      by auto
+    moreover have "deriv fg p / fg p =  deriv f p / f p + deriv h p / h p"
+                      when "p\<in> path_image \<gamma>" for p
+    proof -
+      have "fg p\<noteq>0" and "f p\<noteq>0" using path_f path_fg that unfolding zeros_f_def zeros_fg_def
+        by auto
+      have "h p\<noteq>0"
+      proof (rule ccontr)
+        assume "\<not> h p \<noteq> 0"
+        then have "g p / f p= -1" unfolding h_def by (simp add: add_eq_0_iff2)
+        then have "cmod (g p/f p) = 1" by auto
+        moreover have "cmod (g p/f p) <1" using path_less[rule_format,OF that]
+          apply (cases "cmod (f p) = 0")
+          by (auto simp add: norm_divide)
+        ultimately show False by auto
+      qed
+      have der_fg:"deriv fg p =  deriv f p + deriv g p" unfolding fg_def
+        using f_holo g_holo holomorphic_on_imp_differentiable_at[OF _  \<open>open s\<close>] path_img that
+        by auto
+      have der_h:"deriv h p = (deriv g p * f p - g p * deriv f p)/(f p * f p)"
+      proof -
+        define der where "der \<equiv> \<lambda>p. (deriv g p * f p - g p * deriv f p)/(f p * f p)"
+        have "p\<in>s" using path_img that by auto
+        then have "(h has_field_derivative der p) (at p)"
+          unfolding h_def der_def using g_holo f_holo \<open>open s\<close> \<open>f p\<noteq>0\<close>
+          by (auto intro!: derivative_eq_intros holomorphic_derivI)
+        then show ?thesis unfolding der_def using DERIV_imp_deriv by auto
+      qed
+      show ?thesis
+        apply (simp only:der_fg der_h)
+        apply (auto simp add:field_simps \<open>h p\<noteq>0\<close> \<open>f p\<noteq>0\<close> \<open>fg p\<noteq>0\<close>)
+        by (auto simp add:field_simps h_def \<open>f p\<noteq>0\<close> fg_def)
+    qed
+    then have "contour_integral \<gamma> (\<lambda>p. deriv fg p / fg p)
+                  = contour_integral \<gamma> (\<lambda>p. deriv f p / f p + deriv h p / h p)"
+      by (elim contour_integral_eq)
+    ultimately show ?thesis by auto
+  qed
+  moreover have "contour_integral \<gamma> (\<lambda>x. deriv fg x / fg x) = c * (\<Sum>p\<in>zeros_fg. w p * zorder fg p)"
+    unfolding c_def zeros_fg_def w_def
+  proof (rule argument_principle[OF \<open>open s\<close> \<open>connected s\<close> _ _ \<open>valid_path \<gamma>\<close> loop _ homo
+        , of _ "{}" "\<lambda>_. 1",simplified])
+    show "fg holomorphic_on s" unfolding fg_def using f_holo g_holo holomorphic_on_add by auto
+    show "path_image \<gamma> \<subseteq> s - {p. fg p = 0}" using path_fg unfolding zeros_fg_def .
+    show " finite {p. fg p = 0}" using \<open>finite zeros_fg\<close> unfolding zeros_fg_def .
+  qed
+  moreover have "contour_integral \<gamma> (\<lambda>x. deriv f x / f x) = c * (\<Sum>p\<in>zeros_f. w p * zorder f p)"
+    unfolding c_def zeros_f_def w_def
+  proof (rule argument_principle[OF \<open>open s\<close> \<open>connected s\<close> _ _ \<open>valid_path \<gamma>\<close> loop _ homo
+        , of _ "{}" "\<lambda>_. 1",simplified])
+    show "f holomorphic_on s" using f_holo g_holo holomorphic_on_add by auto
+    show "path_image \<gamma> \<subseteq> s - {p. f p = 0}" using path_f unfolding zeros_f_def .
+    show " finite {p. f p = 0}" using \<open>finite zeros_f\<close> unfolding zeros_f_def .
+  qed
+  ultimately have " c* (\<Sum>p\<in>zeros_fg. w p * (zorder fg p)) = c* (\<Sum>p\<in>zeros_f. w p * (zorder f p))"
+    by auto
+  then show ?thesis unfolding c_def using w_def by auto
+qed
+
+
+subsection \<open>Poles and residues of some well-known functions\<close>
+
+(* TODO: add more material here for other functions *)
+lemma is_pole_Gamma: "is_pole Gamma (-of_nat n)"
+  unfolding is_pole_def using Gamma_poles .
+
+lemma Gamme_residue:
+  "residue Gamma (-of_nat n) = (-1) ^ n / fact n"
+proof (rule residue_simple')
+  show "open (- (\<int>\<^sub>\<le>\<^sub>0 - {-of_nat n}) :: complex set)"
+    by (intro open_Compl closed_subset_Ints) auto
+  show "Gamma holomorphic_on (- (\<int>\<^sub>\<le>\<^sub>0 - {-of_nat n}) - {- of_nat n})"
+    by (rule holomorphic_Gamma) auto
+  show "(\<lambda>w. Gamma w * (w - (-of_nat n))) \<midarrow>(-of_nat n)\<rightarrow> (- 1) ^ n / fact n"
+    using Gamma_residues[of n] by simp
+qed auto
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Complex_Analysis/Great_Picard.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -0,0 +1,1862 @@
+section \<open>The Great Picard Theorem and its Applications\<close>
+
+text\<open>Ported from HOL Light (cauchy.ml) by L C Paulson, 2017\<close>
+
+theory Great_Picard
+  imports Conformal_Mappings
+
+begin
+  
+subsection\<open>Schottky's theorem\<close>
+
+lemma Schottky_lemma0:
+  assumes holf: "f holomorphic_on S" and cons: "contractible S" and "a \<in> S"
+      and f: "\<And>z. z \<in> S \<Longrightarrow> f z \<noteq> 1 \<and> f z \<noteq> -1"
+  obtains g where "g holomorphic_on S"
+                  "norm(g a) \<le> 1 + norm(f a) / 3"
+                  "\<And>z. z \<in> S \<Longrightarrow> f z = cos(of_real pi * g z)"
+proof -
+  obtain g where holg: "g holomorphic_on S" and g: "norm(g a) \<le> pi + norm(f a)"
+             and f_eq_cos: "\<And>z. z \<in> S \<Longrightarrow> f z = cos(g z)"
+    using contractible_imp_holomorphic_arccos_bounded [OF assms]
+    by blast
+  show ?thesis
+  proof
+    show "(\<lambda>z. g z / pi) holomorphic_on S"
+      by (auto intro: holomorphic_intros holg)
+    have "3 \<le> pi"
+      using pi_approx by force
+    have "3 * norm(g a) \<le> 3 * (pi + norm(f a))"
+      using g by auto
+    also have "... \<le>  pi * 3 + pi * cmod (f a)"
+      using \<open>3 \<le> pi\<close> by (simp add: mult_right_mono algebra_simps)
+    finally show "cmod (g a / complex_of_real pi) \<le> 1 + cmod (f a) / 3"
+      by (simp add: field_simps norm_divide)
+    show "\<And>z. z \<in> S \<Longrightarrow> f z = cos (complex_of_real pi * (g z / complex_of_real pi))"
+      by (simp add: f_eq_cos)
+  qed
+qed
+
+
+lemma Schottky_lemma1:
+  fixes n::nat
+  assumes "0 < n"
+  shows "0 < n + sqrt(real n ^ 2 - 1)"
+proof -
+  have "(n-1)^2 \<le> n^2 - 1"
+    using assms by (simp add: algebra_simps power2_eq_square)
+  then have "real (n - 1) \<le> sqrt (real (n\<^sup>2 - 1))"
+    by (metis of_nat_le_iff of_nat_power real_le_rsqrt)
+  then have "n-1 \<le> sqrt(real n ^ 2 - 1)"
+    by (simp add: Suc_leI assms of_nat_diff)
+  then show ?thesis
+    using assms by linarith
+qed
+
+
+lemma Schottky_lemma2:
+  fixes x::real
+  assumes "0 \<le> x"
+  obtains n where "0 < n" "\<bar>x - ln (real n + sqrt ((real n)\<^sup>2 - 1)) / pi\<bar> < 1/2"
+proof -
+  obtain n0::nat where "0 < n0" "ln(n0 + sqrt(real n0 ^ 2 - 1)) / pi \<le> x"
+  proof
+    show "ln(real 1 + sqrt(real 1 ^ 2 - 1))/pi \<le> x"
+      by (auto simp: assms)
+  qed auto
+  moreover
+  obtain M::nat where "\<And>n. \<lbrakk>0 < n; ln(n + sqrt(real n ^ 2 - 1)) / pi \<le> x\<rbrakk> \<Longrightarrow> n \<le> M"
+  proof
+    fix n::nat
+    assume "0 < n" "ln (n + sqrt ((real n)\<^sup>2 - 1)) / pi \<le> x"
+    then have "ln (n + sqrt ((real n)\<^sup>2 - 1)) \<le> x * pi"
+      by (simp add: field_split_simps)
+    then have *: "exp (ln (n + sqrt ((real n)\<^sup>2 - 1))) \<le> exp (x * pi)"
+      by blast
+    have 0: "0 \<le> sqrt ((real n)\<^sup>2 - 1)"
+      using \<open>0 < n\<close> by auto
+    have "n + sqrt ((real n)\<^sup>2 - 1) = exp (ln (n + sqrt ((real n)\<^sup>2 - 1)))"
+      by (simp add: Suc_leI \<open>0 < n\<close> add_pos_nonneg real_of_nat_ge_one_iff)
+    also have "... \<le> exp (x * pi)"
+      using "*" by blast
+    finally have "real n \<le> exp (x * pi)"
+      using 0 by linarith
+    then show "n \<le> nat (ceiling (exp(x * pi)))"
+      by linarith
+  qed
+  ultimately obtain n where
+     "0 < n" and le_x: "ln(n + sqrt(real n ^ 2 - 1)) / pi \<le> x"
+             and le_n: "\<And>k. \<lbrakk>0 < k; ln(k + sqrt(real k ^ 2 - 1)) / pi \<le> x\<rbrakk> \<Longrightarrow> k \<le> n"
+    using bounded_Max_nat [of "\<lambda>n. 0<n \<and> ln (n + sqrt ((real n)\<^sup>2 - 1)) / pi \<le> x"] by metis
+  define a where "a \<equiv> ln(n + sqrt(real n ^ 2 - 1)) / pi"
+  define b where "b \<equiv> ln (1 + real n + sqrt ((1 + real n)\<^sup>2 - 1)) / pi"
+  have le_xa: "a \<le> x"
+   and le_na: "\<And>k. \<lbrakk>0 < k; ln(k + sqrt(real k ^ 2 - 1)) / pi \<le> x\<rbrakk> \<Longrightarrow> k \<le> n"
+    using le_x le_n by (auto simp: a_def)
+  moreover have "x < b"
+    using le_n [of "Suc n"] by (force simp: b_def)
+  moreover have "b - a < 1"
+  proof -
+    have "ln (1 + real n + sqrt ((1 + real n)\<^sup>2 - 1)) - ln (real n + sqrt ((real n)\<^sup>2 - 1)) =
+         ln ((1 + real n + sqrt ((1 + real n)\<^sup>2 - 1)) / (real n + sqrt ((real n)\<^sup>2 - 1)))"
+      by (simp add: \<open>0 < n\<close> Schottky_lemma1 add_pos_nonneg ln_div [symmetric])
+    also have "... \<le> 3"
+    proof (cases "n = 1")
+      case True
+      have "sqrt 3 \<le> 2"
+        by (simp add: real_le_lsqrt)
+      then have "(2 + sqrt 3) \<le> 4"
+        by simp
+      also have "... \<le> exp 3"
+        using exp_ge_add_one_self [of "3::real"] by simp
+      finally have "ln (2 + sqrt 3) \<le> 3"
+        by (metis add_nonneg_nonneg add_pos_nonneg dbl_def dbl_inc_def dbl_inc_simps(3)
+            dbl_simps(3) exp_gt_zero ln_exp ln_le_cancel_iff real_sqrt_ge_0_iff zero_le_one zero_less_one)
+      then show ?thesis
+        by (simp add: True)
+    next
+      case False with \<open>0 < n\<close> have "1 < n" "2 \<le> n"
+        by linarith+
+      then have 1: "1 \<le> real n * real n"
+        by (metis less_imp_le_nat one_le_power power2_eq_square real_of_nat_ge_one_iff)
+      have *: "4 + (m+2) * 2 \<le> (m+2) * ((m+2) * 3)" for m::nat
+        by simp
+      have "4 + n * 2 \<le> n * (n * 3)"
+        using * [of "n-2"]  \<open>2 \<le> n\<close>
+        by (metis le_add_diff_inverse2)
+      then have **: "4 + real n * 2 \<le> real n * (real n * 3)"
+        by (metis (mono_tags, hide_lams) of_nat_le_iff of_nat_add of_nat_mult of_nat_numeral)
+      have "sqrt ((1 + real n)\<^sup>2 - 1) \<le> 2 * sqrt ((real n)\<^sup>2 - 1)"
+        by (auto simp: real_le_lsqrt power2_eq_square algebra_simps 1 **)
+      then
+      have "((1 + real n + sqrt ((1 + real n)\<^sup>2 - 1)) / (real n + sqrt ((real n)\<^sup>2 - 1))) \<le> 2"
+        using Schottky_lemma1 \<open>0 < n\<close>  by (simp add: field_split_simps)
+      then have "ln ((1 + real n + sqrt ((1 + real n)\<^sup>2 - 1)) / (real n + sqrt ((real n)\<^sup>2 - 1))) \<le> ln 2"
+        apply (subst ln_le_cancel_iff)
+        using Schottky_lemma1 \<open>0 < n\<close> by auto (force simp: field_split_simps)
+      also have "... \<le> 3"
+        using ln_add_one_self_le_self [of 1] by auto
+      finally show ?thesis .
+    qed
+    also have "... < pi"
+      using pi_approx by simp
+    finally show ?thesis
+      by (simp add: a_def b_def field_split_simps)
+  qed
+  ultimately have "\<bar>x - a\<bar> < 1/2 \<or> \<bar>x - b\<bar> < 1/2"
+    by (auto simp: abs_if)
+  then show thesis
+  proof
+    assume "\<bar>x - a\<bar> < 1 / 2"
+    then show ?thesis
+      by (rule_tac n=n in that) (auto simp: a_def \<open>0 < n\<close>)
+  next
+    assume "\<bar>x - b\<bar> < 1 / 2"
+    then show ?thesis
+      by (rule_tac n="Suc n" in that) (auto simp: b_def \<open>0 < n\<close>)
+  qed
+qed
+
+
+lemma Schottky_lemma3:
+  fixes z::complex
+  assumes "z \<in> (\<Union>m \<in> Ints. \<Union>n \<in> {0<..}. {Complex m (ln(n + sqrt(real n ^ 2 - 1)) / pi)})
+             \<union> (\<Union>m \<in> Ints. \<Union>n \<in> {0<..}. {Complex m (-ln(n + sqrt(real n ^ 2 - 1)) / pi)})"
+  shows "cos(pi * cos(pi * z)) = 1 \<or> cos(pi * cos(pi * z)) = -1"
+proof -
+  have sqrt2 [simp]: "complex_of_real (sqrt x) * complex_of_real (sqrt x) = x" if "x \<ge> 0" for x::real
+    by (metis abs_of_nonneg of_real_mult real_sqrt_mult_self that)
+  have 1: "\<exists>k. exp (\<i> * (of_int m * complex_of_real pi) -
+                 (ln (real n + sqrt ((real n)\<^sup>2 - 1)))) +
+            inverse
+             (exp (\<i> * (of_int m * complex_of_real pi) -
+                    (ln (real n + sqrt ((real n)\<^sup>2 - 1))))) = of_int k * 2"
+         if "n > 0" for m n
+  proof -
+    have eeq: "e \<noteq> 0 \<Longrightarrow> e + inverse e = n*2 \<longleftrightarrow> inverse e^2 - 2 * n*inverse e + 1 = 0" for n e::complex
+      by (auto simp: field_simps power2_eq_square)
+    have [simp]: "1 \<le> real n * real n"
+      by (metis One_nat_def add.commute nat_less_real_le of_nat_1 of_nat_Suc one_le_power power2_eq_square that)
+    show ?thesis
+      apply (simp add: eeq)
+      using Schottky_lemma1 [OF that]
+      apply (auto simp: eeq exp_diff exp_Euler exp_of_real algebra_simps sin_int_times_real cos_int_times_real)
+       apply (rule_tac x="int n" in exI)
+       apply (auto simp: power2_eq_square algebra_simps)
+       apply (rule_tac x="- int n" in exI)
+      apply (auto simp: power2_eq_square algebra_simps)
+      done
+  qed
+  have 2: "\<exists>k. exp (\<i> * (of_int m * complex_of_real pi) +
+                 (ln (real n + sqrt ((real n)\<^sup>2 - 1)))) +
+            inverse
+             (exp (\<i> * (of_int m * complex_of_real pi) +
+                    (ln (real n + sqrt ((real n)\<^sup>2 - 1))))) = of_int k * 2"
+            if "n > 0" for m n
+  proof -
+    have eeq: "e \<noteq> 0 \<Longrightarrow> e + inverse e = n*2 \<longleftrightarrow> e^2 - 2 * n*e + 1 = 0" for n e::complex
+      by (auto simp: field_simps power2_eq_square)
+    have [simp]: "1 \<le> real n * real n"
+      by (metis One_nat_def add.commute nat_less_real_le of_nat_1 of_nat_Suc one_le_power power2_eq_square that)
+    show ?thesis
+      apply (simp add: eeq)
+      using Schottky_lemma1 [OF that]
+      apply (auto simp: exp_add exp_Euler exp_of_real algebra_simps sin_int_times_real cos_int_times_real)
+       apply (rule_tac x="int n" in exI)
+       apply (auto simp: power2_eq_square algebra_simps)
+       apply (rule_tac x="- int n" in exI)
+      apply (auto simp: power2_eq_square algebra_simps)
+      done
+  qed
+  have "\<exists>x. cos (complex_of_real pi * z) = of_int x"
+    using assms
+    apply safe
+      apply (auto simp: Ints_def cos_exp_eq exp_minus Complex_eq)
+     apply (auto simp: algebra_simps dest: 1 2)
+      done
+  then have "sin(pi * cos(pi * z)) ^ 2 = 0"
+    by (simp add: Complex_Transcendental.sin_eq_0)
+  then have "1 - cos(pi * cos(pi * z)) ^ 2 = 0"
+    by (simp add: sin_squared_eq)
+  then show ?thesis
+    using power2_eq_1_iff by auto
+qed
+
+
+theorem Schottky:
+  assumes holf: "f holomorphic_on cball 0 1"
+      and nof0: "norm(f 0) \<le> r"
+      and not01: "\<And>z. z \<in> cball 0 1 \<Longrightarrow> \<not>(f z = 0 \<or> f z = 1)"
+      and "0 < t" "t < 1" "norm z \<le> t"
+    shows "norm(f z) \<le> exp(pi * exp(pi * (2 + 2 * r + 12 * t / (1 - t))))"
+proof -
+  obtain h where holf: "h holomorphic_on cball 0 1"
+             and nh0: "norm (h 0) \<le> 1 + norm(2 * f 0 - 1) / 3"
+             and h:   "\<And>z. z \<in> cball 0 1 \<Longrightarrow> 2 * f z - 1 = cos(of_real pi * h z)"
+  proof (rule Schottky_lemma0 [of "\<lambda>z. 2 * f z - 1" "cball 0 1" 0])
+    show "(\<lambda>z. 2 * f z - 1) holomorphic_on cball 0 1"
+      by (intro holomorphic_intros holf)
+    show "contractible (cball (0::complex) 1)"
+      by (auto simp: convex_imp_contractible)
+    show "\<And>z. z \<in> cball 0 1 \<Longrightarrow> 2 * f z - 1 \<noteq> 1 \<and> 2 * f z - 1 \<noteq> - 1"
+      using not01 by force
+  qed auto
+  obtain g where holg: "g holomorphic_on cball 0 1"
+             and ng0:  "norm(g 0) \<le> 1 + norm(h 0) / 3"
+             and g:    "\<And>z. z \<in> cball 0 1 \<Longrightarrow> h z = cos(of_real pi * g z)"
+  proof (rule Schottky_lemma0 [OF holf convex_imp_contractible, of 0])
+    show "\<And>z. z \<in> cball 0 1 \<Longrightarrow> h z \<noteq> 1 \<and> h z \<noteq> - 1"
+      using h not01 by fastforce+
+  qed auto
+  have g0_2_f0: "norm(g 0) \<le> 2 + norm(f 0)"
+  proof -
+    have "cmod (2 * f 0 - 1) \<le> cmod (2 * f 0) + 1"
+      by (metis norm_one norm_triangle_ineq4)
+    also have "... \<le> 2 + cmod (f 0) * 3"
+      by simp
+    finally have "1 + norm(2 * f 0 - 1) / 3 \<le> (2 + norm(f 0) - 1) * 3"
+      apply (simp add: field_split_simps)
+      using norm_ge_zero [of "f 0 * 2 - 1"]
+      by linarith
+    with nh0 have "norm(h 0) \<le> (2 + norm(f 0) - 1) * 3"
+      by linarith
+    then have "1 + norm(h 0) / 3 \<le> 2 + norm(f 0)"
+      by simp
+    with ng0 show ?thesis
+      by auto
+  qed
+  have "z \<in> ball 0 1"
+    using assms by auto
+  have norm_g_12: "norm(g z - g 0) \<le> (12 * t) / (1 - t)"
+  proof -
+    obtain g' where g': "\<And>x. x \<in> cball 0 1 \<Longrightarrow> (g has_field_derivative g' x) (at x within cball 0 1)"
+      using holg [unfolded holomorphic_on_def field_differentiable_def] by metis
+    have int_g': "(g' has_contour_integral g z - g 0) (linepath 0 z)"
+      using contour_integral_primitive [OF g' valid_path_linepath, of 0 z]
+      using \<open>z \<in> ball 0 1\<close> segment_bound1 by fastforce
+    have "cmod (g' w) \<le> 12 / (1 - t)" if "w \<in> closed_segment 0 z" for w
+    proof -
+      have w: "w \<in> ball 0 1"
+        using segment_bound [OF that] \<open>z \<in> ball 0 1\<close> by simp
+      have ttt: "\<And>z. z \<in> frontier (cball 0 1) \<Longrightarrow> 1 - t \<le> dist w z"
+        using \<open>norm z \<le> t\<close> segment_bound1 [OF \<open>w \<in> closed_segment 0 z\<close>]
+        apply (simp add: dist_complex_def)
+        by (metis diff_left_mono dist_commute dist_complex_def norm_triangle_ineq2 order_trans)
+      have *: "\<lbrakk>\<And>b. (\<exists>w \<in> T \<union> U. w \<in> ball b 1); \<And>x. x \<in> D \<Longrightarrow> g x \<notin> T \<union> U\<rbrakk> \<Longrightarrow> \<nexists>b. ball b 1 \<subseteq> g ` D" for T U D
+        by force
+      have "\<nexists>b. ball b 1 \<subseteq> g ` cball 0 1"
+      proof (rule *)
+        show "(\<exists>w \<in> (\<Union>m \<in> Ints. \<Union>n \<in> {0<..}. {Complex m (ln(n + sqrt(real n ^ 2 - 1)) / pi)}) \<union>
+                    (\<Union>m \<in> Ints. \<Union>n \<in> {0<..}. {Complex m (-ln(n + sqrt(real n ^ 2 - 1)) / pi)}). w \<in> ball b 1)" for b
+        proof -
+          obtain m where m: "m \<in> \<int>" "\<bar>Re b - m\<bar> \<le> 1/2"
+            by (metis Ints_of_int abs_minus_commute of_int_round_abs_le)
+          show ?thesis
+          proof (cases "0::real" "Im b" rule: le_cases)
+            case le
+            then obtain n where "0 < n" and n: "\<bar>Im b - ln (n + sqrt ((real n)\<^sup>2 - 1)) / pi\<bar> < 1/2"
+              using Schottky_lemma2 [of "Im b"] by blast
+            have "dist b (Complex m (Im b)) \<le> 1/2"
+              by (metis cancel_comm_monoid_add_class.diff_cancel cmod_eq_Re complex.sel(1) complex.sel(2) dist_norm m(2) minus_complex.code)
+            moreover
+            have "dist (Complex m (Im b)) (Complex m (ln (n + sqrt ((real n)\<^sup>2 - 1)) / pi)) < 1/2"
+              using n by (simp add: complex_norm cmod_eq_Re complex_diff dist_norm del: Complex_eq)
+            ultimately have "dist b (Complex m (ln (real n + sqrt ((real n)\<^sup>2 - 1)) / pi)) < 1"
+              by (simp add: dist_triangle_lt [of b "Complex m (Im b)"] dist_commute)
+            with le m \<open>0 < n\<close> show ?thesis
+              apply (rule_tac x = "Complex m (ln (real n + sqrt ((real n)\<^sup>2 - 1)) / pi)" in bexI)
+               apply (simp_all del: Complex_eq greaterThan_0)
+              by blast
+          next
+            case ge
+            then obtain n where "0 < n" and n: "\<bar>- Im b - ln (real n + sqrt ((real n)\<^sup>2 - 1)) / pi\<bar> < 1/2"
+              using Schottky_lemma2 [of "- Im b"] by auto
+            have "dist b (Complex m (Im b)) \<le> 1/2"
+              by (metis cancel_comm_monoid_add_class.diff_cancel cmod_eq_Re complex.sel(1) complex.sel(2) dist_norm m(2) minus_complex.code)
+            moreover
+            have "dist (Complex m (- ln (n + sqrt ((real n)\<^sup>2 - 1)) / pi)) (Complex m (Im b)) < 1/2"
+              using n
+              apply (simp add: complex_norm cmod_eq_Re complex_diff dist_norm del: Complex_eq)
+              by (metis add.commute add_uminus_conv_diff)
+            ultimately have "dist b (Complex m (- ln (real n + sqrt ((real n)\<^sup>2 - 1)) / pi)) < 1"
+              by (simp add: dist_triangle_lt [of b "Complex m (Im b)"] dist_commute)
+            with ge m \<open>0 < n\<close> show ?thesis
+              apply (rule_tac x = "Complex m (- ln (real n + sqrt ((real n)\<^sup>2 - 1)) / pi)" in bexI)
+               apply (simp_all del: Complex_eq greaterThan_0)
+              by blast
+          qed
+        qed
+        show "g v \<notin> (\<Union>m \<in> Ints. \<Union>n \<in> {0<..}. {Complex m (ln(n + sqrt(real n ^ 2 - 1)) / pi)}) \<union>
+                    (\<Union>m \<in> Ints. \<Union>n \<in> {0<..}. {Complex m (-ln(n + sqrt(real n ^ 2 - 1)) / pi)})"
+             if "v \<in> cball 0 1" for v
+          using not01 [OF that]
+          by (force simp: g [OF that, symmetric] h [OF that, symmetric] dest!: Schottky_lemma3 [of "g v"])
+      qed
+      then have 12: "(1 - t) * cmod (deriv g w) / 12 < 1"
+        using Bloch_general [OF holg _ ttt, of 1] w by force
+      have "g field_differentiable at w within cball 0 1"
+        using holg w by (simp add: holomorphic_on_def)
+      then have "g field_differentiable at w within ball 0 1"
+        using ball_subset_cball field_differentiable_within_subset by blast
+      with w have der_gw: "(g has_field_derivative deriv g w) (at w)"
+        by (simp add: field_differentiable_within_open [of _ "ball 0 1"] field_differentiable_derivI)
+      with DERIV_unique [OF der_gw] g' w have "deriv g w = g' w"
+        by (metis open_ball at_within_open ball_subset_cball has_field_derivative_subset subsetCE)
+      then show "cmod (g' w) \<le> 12 / (1 - t)"
+        using g' 12 \<open>t < 1\<close> by (simp add: field_simps)
+    qed
+    then have "cmod (g z - g 0) \<le> 12 / (1 - t) * cmod z"
+      using has_contour_integral_bound_linepath [OF int_g', of "12/(1 - t)"] assms
+      by simp
+    with \<open>cmod z \<le> t\<close> \<open>t < 1\<close> show ?thesis
+      by (simp add: field_split_simps)
+  qed
+  have fz: "f z = (1 + cos(of_real pi * h z)) / 2"
+    using h \<open>z \<in> ball 0 1\<close> by (auto simp: field_simps)
+  have "cmod (f z) \<le> exp (cmod (complex_of_real pi * h z))"
+    by (simp add: fz mult.commute norm_cos_plus1_le)
+  also have "... \<le> exp (pi * exp (pi * (2 + 2 * r + 12 * t / (1 - t))))"
+  proof (simp add: norm_mult)
+    have "cmod (g z - g 0) \<le> 12 * t / (1 - t)"
+      using norm_g_12 \<open>t < 1\<close> by (simp add: norm_mult)
+    then have "cmod (g z) - cmod (g 0) \<le> 12 * t / (1 - t)"
+      using norm_triangle_ineq2 order_trans by blast
+    then have *: "cmod (g z) \<le> 2 + 2 * r + 12 * t / (1 - t)"
+      using g0_2_f0 norm_ge_zero [of "f 0"] nof0
+        by linarith
+    have "cmod (h z) \<le> exp (cmod (complex_of_real pi * g z))"
+      using \<open>z \<in> ball 0 1\<close> by (simp add: g norm_cos_le)
+    also have "... \<le> exp (pi * (2 + 2 * r + 12 * t / (1 - t)))"
+      using \<open>t < 1\<close> nof0 * by (simp add: norm_mult)
+    finally show "cmod (h z) \<le> exp (pi * (2 + 2 * r + 12 * t / (1 - t)))" .
+  qed
+  finally show ?thesis .
+qed
+
+  
+subsection\<open>The Little Picard Theorem\<close>
+
+theorem Landau_Picard:
+  obtains R
+    where "\<And>z. 0 < R z"
+          "\<And>f. \<lbrakk>f holomorphic_on cball 0 (R(f 0));
+                 \<And>z. norm z \<le> R(f 0) \<Longrightarrow> f z \<noteq> 0 \<and> f z \<noteq> 1\<rbrakk> \<Longrightarrow> norm(deriv f 0) < 1"
+proof -
+  define R where "R \<equiv> \<lambda>z. 3 * exp(pi * exp(pi*(2 + 2 * cmod z + 12)))"
+  show ?thesis
+  proof
+    show Rpos: "\<And>z. 0 < R z"
+      by (auto simp: R_def)
+    show "norm(deriv f 0) < 1"
+         if holf: "f holomorphic_on cball 0 (R(f 0))"
+         and Rf:  "\<And>z. norm z \<le> R(f 0) \<Longrightarrow> f z \<noteq> 0 \<and> f z \<noteq> 1" for f
+    proof -
+      let ?r = "R(f 0)"
+      define g where "g \<equiv> f \<circ> (\<lambda>z. of_real ?r * z)"
+      have "0 < ?r"
+        using Rpos by blast
+      have holg: "g holomorphic_on cball 0 1"
+        unfolding g_def
+        apply (intro holomorphic_intros holomorphic_on_compose holomorphic_on_subset [OF holf])
+        using Rpos by (auto simp: less_imp_le norm_mult)
+      have *: "norm(g z) \<le> exp(pi * exp(pi * (2 + 2 * norm (f 0) + 12 * t / (1 - t))))"
+           if "0 < t" "t < 1" "norm z \<le> t" for t z
+      proof (rule Schottky [OF holg])
+        show "cmod (g 0) \<le> cmod (f 0)"
+          by (simp add: g_def)
+        show "\<And>z. z \<in> cball 0 1 \<Longrightarrow> \<not> (g z = 0 \<or> g z = 1)"
+          using Rpos by (simp add: g_def less_imp_le norm_mult Rf)
+      qed (auto simp: that)
+      have C1: "g holomorphic_on ball 0 (1 / 2)"
+        by (rule holomorphic_on_subset [OF holg]) auto
+      have C2: "continuous_on (cball 0 (1 / 2)) g"
+        by (meson cball_divide_subset_numeral holg holomorphic_on_imp_continuous_on holomorphic_on_subset)
+      have C3: "cmod (g z) \<le> R (f 0) / 3" if "cmod (0 - z) = 1/2" for z
+      proof -
+        have "norm(g z) \<le> exp(pi * exp(pi * (2 + 2 * norm (f 0) + 12)))"
+          using * [of "1/2"] that by simp
+        also have "... = ?r / 3"
+          by (simp add: R_def)
+        finally show ?thesis .
+      qed
+      then have cmod_g'_le: "cmod (deriv g 0) * 3 \<le> R (f 0) * 2"
+        using Cauchy_inequality [OF C1 C2 _ C3, of 1] by simp
+      have holf': "f holomorphic_on ball 0 (R(f 0))"
+        by (rule holomorphic_on_subset [OF holf]) auto
+      then have fd0: "f field_differentiable at 0"
+        by (rule holomorphic_on_imp_differentiable_at [OF _ open_ball])
+           (auto simp: Rpos [of "f 0"])
+      have g_eq: "deriv g 0 = of_real ?r * deriv f 0"
+        apply (rule DERIV_imp_deriv)
+        apply (simp add: g_def)
+        by (metis DERIV_chain DERIV_cmult_Id fd0 field_differentiable_derivI mult.commute mult_zero_right)
+      show ?thesis
+        using cmod_g'_le Rpos [of "f 0"]  by (simp add: g_eq norm_mult)
+    qed
+  qed
+qed
+
+lemma little_Picard_01:
+  assumes holf: "f holomorphic_on UNIV" and f01: "\<And>z. f z \<noteq> 0 \<and> f z \<noteq> 1"
+  obtains c where "f = (\<lambda>x. c)"
+proof -
+  obtain R
+    where Rpos: "\<And>z. 0 < R z"
+      and R:    "\<And>h. \<lbrakk>h holomorphic_on cball 0 (R(h 0));
+                      \<And>z. norm z \<le> R(h 0) \<Longrightarrow> h z \<noteq> 0 \<and> h z \<noteq> 1\<rbrakk> \<Longrightarrow> norm(deriv h 0) < 1"
+    using Landau_Picard by metis
+  have contf: "continuous_on UNIV f"
+    by (simp add: holf holomorphic_on_imp_continuous_on)
+  show ?thesis
+  proof (cases "\<forall>x. deriv f x = 0")
+    case True
+    obtain c where "\<And>x. f(x) = c"
+      apply (rule DERIV_zero_connected_constant [OF connected_UNIV open_UNIV finite.emptyI contf])
+       apply (metis True DiffE holf holomorphic_derivI open_UNIV, auto)
+      done
+    then show ?thesis
+      using that by auto
+  next
+    case False
+    then obtain w where w: "deriv f w \<noteq> 0" by auto
+    define fw where "fw \<equiv> (f \<circ> (\<lambda>z. w + z / deriv f w))"
+    have norm_let1: "norm(deriv fw 0) < 1"
+    proof (rule R)
+      show "fw holomorphic_on cball 0 (R (fw 0))"
+        unfolding fw_def
+        by (intro holomorphic_intros holomorphic_on_compose w holomorphic_on_subset [OF holf] subset_UNIV)
+      show "fw z \<noteq> 0 \<and> fw z \<noteq> 1" if "cmod z \<le> R (fw 0)" for z
+        using f01 by (simp add: fw_def)
+    qed
+    have "(fw has_field_derivative deriv f w * inverse (deriv f w)) (at 0)"
+      apply (simp add: fw_def)
+      apply (rule DERIV_chain)
+      using holf holomorphic_derivI apply force
+      apply (intro derivative_eq_intros w)
+          apply (auto simp: field_simps)
+      done
+    then show ?thesis
+      using norm_let1 w by (simp add: DERIV_imp_deriv)
+  qed
+qed
+
+
+theorem little_Picard:
+  assumes holf: "f holomorphic_on UNIV"
+      and "a \<noteq> b" "range f \<inter> {a,b} = {}"
+    obtains c where "f = (\<lambda>x. c)"
+proof -
+  let ?g = "\<lambda>x. 1/(b - a)*(f x - b) + 1"
+  obtain c where "?g = (\<lambda>x. c)"
+  proof (rule little_Picard_01)
+    show "?g holomorphic_on UNIV"
+      by (intro holomorphic_intros holf)
+    show "\<And>z. ?g z \<noteq> 0 \<and> ?g z \<noteq> 1"
+      using assms by (auto simp: field_simps)
+  qed auto
+  then have "?g x = c" for x
+    by meson
+  then have "f x = c * (b-a) + a" for x
+    using assms by (auto simp: field_simps)
+  then show ?thesis
+    using that by blast
+qed
+
+
+text\<open>A couple of little applications of Little Picard\<close>
+
+lemma holomorphic_periodic_fixpoint:
+  assumes holf: "f holomorphic_on UNIV"
+      and "p \<noteq> 0" and per: "\<And>z. f(z + p) = f z"
+  obtains x where "f x = x"
+proof -
+  have False if non: "\<And>x. f x \<noteq> x"
+  proof -
+    obtain c where "(\<lambda>z. f z - z) = (\<lambda>z. c)"
+    proof (rule little_Picard)
+      show "(\<lambda>z. f z - z) holomorphic_on UNIV"
+        by (simp add: holf holomorphic_on_diff)
+      show "range (\<lambda>z. f z - z) \<inter> {p,0} = {}"
+          using assms non by auto (metis add.commute diff_eq_eq)
+      qed (auto simp: assms)
+    with per show False
+      by (metis add.commute add_cancel_left_left \<open>p \<noteq> 0\<close> diff_add_cancel)
+  qed
+  then show ?thesis
+    using that by blast
+qed
+
+
+lemma holomorphic_involution_point:
+  assumes holfU: "f holomorphic_on UNIV" and non: "\<And>a. f \<noteq> (\<lambda>x. a + x)"
+  obtains x where "f(f x) = x"
+proof -
+  { assume non_ff [simp]: "\<And>x. f(f x) \<noteq> x"
+    then have non_fp [simp]: "f z \<noteq> z" for z
+      by metis
+    have holf: "f holomorphic_on X" for X
+      using assms holomorphic_on_subset by blast
+    obtain c where c: "(\<lambda>x. (f(f x) - x)/(f x - x)) = (\<lambda>x. c)"
+    proof (rule little_Picard_01)
+      show "(\<lambda>x. (f(f x) - x)/(f x - x)) holomorphic_on UNIV"
+        apply (intro holomorphic_intros holf holomorphic_on_compose [unfolded o_def, OF holf])
+        using non_fp by auto
+    qed auto
+    then obtain "c \<noteq> 0" "c \<noteq> 1"
+      by (metis (no_types, hide_lams) non_ff diff_zero divide_eq_0_iff right_inverse_eq)
+    have eq: "f(f x) - c * f x = x*(1 - c)" for x
+      using fun_cong [OF c, of x] by (simp add: field_simps)
+    have df_times_dff: "deriv f z * (deriv f (f z) - c) = 1 * (1 - c)" for z
+    proof (rule DERIV_unique)
+      show "((\<lambda>x. f (f x) - c * f x) has_field_derivative
+              deriv f z * (deriv f (f z) - c)) (at z)"
+        apply (intro derivative_eq_intros)
+            apply (rule DERIV_chain [unfolded o_def, of f])
+             apply (auto simp: algebra_simps intro!: holomorphic_derivI [OF holfU])
+        done
+      show "((\<lambda>x. f (f x) - c * f x) has_field_derivative 1 * (1 - c)) (at z)"
+        by (simp add: eq mult_commute_abs)
+    qed
+    { fix z::complex
+      obtain k where k: "deriv f \<circ> f = (\<lambda>x. k)"
+      proof (rule little_Picard)
+        show "(deriv f \<circ> f) holomorphic_on UNIV"
+          by (meson holfU holomorphic_deriv holomorphic_on_compose holomorphic_on_subset open_UNIV subset_UNIV)
+        obtain "deriv f (f x) \<noteq> 0" "deriv f (f x) \<noteq> c"  for x
+          using df_times_dff \<open>c \<noteq> 1\<close> eq_iff_diff_eq_0
+          by (metis lambda_one mult_zero_left mult_zero_right)
+        then show "range (deriv f \<circ> f) \<inter> {0,c} = {}"
+          by force
+      qed (use \<open>c \<noteq> 0\<close> in auto)
+      have "\<not> f constant_on UNIV"
+        by (meson UNIV_I non_ff constant_on_def)
+      with holf open_mapping_thm have "open(range f)"
+        by blast
+      obtain l where l: "\<And>x. f x - k * x = l"
+      proof (rule DERIV_zero_connected_constant [of UNIV "{}" "\<lambda>x. f x - k * x"], simp_all)
+        have "deriv f w - k = 0" for w
+        proof (rule analytic_continuation [OF _ open_UNIV connected_UNIV subset_UNIV, of "\<lambda>z. deriv f z - k" "f z" "range f" w])
+          show "(\<lambda>z. deriv f z - k) holomorphic_on UNIV"
+            by (intro holomorphic_intros holf open_UNIV)
+          show "f z islimpt range f"
+            by (metis (no_types, lifting) IntI UNIV_I \<open>open (range f)\<close> image_eqI inf.absorb_iff2 inf_aci(1) islimpt_UNIV islimpt_eq_acc_point open_Int top_greatest)
+          show "\<And>z. z \<in> range f \<Longrightarrow> deriv f z - k = 0"
+            by (metis comp_def diff_self image_iff k)
+        qed auto
+        moreover
+        have "((\<lambda>x. f x - k * x) has_field_derivative deriv f x - k) (at x)" for x
+          by (metis DERIV_cmult_Id Deriv.field_differentiable_diff UNIV_I field_differentiable_derivI holf holomorphic_on_def)
+        ultimately
+        show "\<forall>x. ((\<lambda>x. f x - k * x) has_field_derivative 0) (at x)"
+          by auto
+        show "continuous_on UNIV (\<lambda>x. f x - k * x)"
+          by (simp add: continuous_on_diff holf holomorphic_on_imp_continuous_on)
+      qed (auto simp: connected_UNIV)
+      have False
+      proof (cases "k=1")
+        case True
+        then have "\<exists>x. k * x + l \<noteq> a + x" for a
+          using l non [of a] ext [of f "(+) a"]
+          by (metis add.commute diff_eq_eq)
+        with True show ?thesis by auto
+      next
+        case False
+        have "\<And>x. (1 - k) * x \<noteq> f 0"
+          using l [of 0] apply (simp add: algebra_simps)
+          by (metis diff_add_cancel l mult.commute non_fp)
+        then show False
+          by (metis False eq_iff_diff_eq_0 mult.commute nonzero_mult_div_cancel_right times_divide_eq_right)
+      qed
+    }
+  }
+  then show thesis
+    using that by blast
+qed
+
+
+subsection\<open>The Arzelà--Ascoli theorem\<close>
+
+lemma subsequence_diagonalization_lemma:
+  fixes P :: "nat \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> bool"
+  assumes sub: "\<And>i r. \<exists>k. strict_mono (k :: nat \<Rightarrow> nat) \<and> P i (r \<circ> k)"
+      and P_P:  "\<And>i r::nat \<Rightarrow> 'a. \<And>k1 k2 N.
+                   \<lbrakk>P i (r \<circ> k1); \<And>j. N \<le> j \<Longrightarrow> \<exists>j'. j \<le> j' \<and> k2 j = k1 j'\<rbrakk> \<Longrightarrow> P i (r \<circ> k2)"
+   obtains k where "strict_mono (k :: nat \<Rightarrow> nat)" "\<And>i. P i (r \<circ> k)"
+proof -
+  obtain kk where "\<And>i r. strict_mono (kk i r :: nat \<Rightarrow> nat) \<and> P i (r \<circ> (kk i r))"
+    using sub by metis
+  then have sub_kk: "\<And>i r. strict_mono (kk i r)" and P_kk: "\<And>i r. P i (r \<circ> (kk i r))"
+    by auto
+  define rr where "rr \<equiv> rec_nat (kk 0 r) (\<lambda>n x. x \<circ> kk (Suc n) (r \<circ> x))"
+  then have [simp]: "rr 0 = kk 0 r" "\<And>n. rr(Suc n) = rr n \<circ> kk (Suc n) (r \<circ> rr n)"
+    by auto
+  show thesis
+  proof
+    have sub_rr: "strict_mono (rr i)" for i
+      using sub_kk  by (induction i) (auto simp: strict_mono_def o_def)
+    have P_rr: "P i (r \<circ> rr i)" for i
+      using P_kk  by (induction i) (auto simp: o_def)
+    have "i \<le> i+d \<Longrightarrow> rr i n \<le> rr (i+d) n" for d i n
+    proof (induction d)
+      case 0 then show ?case
+        by simp
+    next
+      case (Suc d) then show ?case
+        apply simp
+          using seq_suble [OF sub_kk] order_trans strict_mono_less_eq [OF sub_rr] by blast
+    qed
+    then have "\<And>i j n. i \<le> j \<Longrightarrow> rr i n \<le> rr j n"
+      by (metis le_iff_add)
+    show "strict_mono (\<lambda>n. rr n n)"
+      apply (simp add: strict_mono_Suc_iff)
+      by (meson lessI less_le_trans seq_suble strict_monoD sub_kk sub_rr)
+    have "\<exists>j. i \<le> j \<and> rr (n+d) i = rr n j" for d n i
+      apply (induction d arbitrary: i, auto)
+      by (meson order_trans seq_suble sub_kk)
+    then have "\<And>m n i. n \<le> m \<Longrightarrow> \<exists>j. i \<le> j \<and> rr m i = rr n j"
+      by (metis le_iff_add)
+    then show "P i (r \<circ> (\<lambda>n. rr n n))" for i
+      by (meson P_rr P_P)
+  qed
+qed
+
+lemma function_convergent_subsequence:
+  fixes f :: "[nat,'a] \<Rightarrow> 'b::{real_normed_vector,heine_borel}"
+  assumes "countable S" and M: "\<And>n::nat. \<And>x. x \<in> S \<Longrightarrow> norm(f n x) \<le> M"
+   obtains k where "strict_mono (k::nat\<Rightarrow>nat)" "\<And>x. x \<in> S \<Longrightarrow> \<exists>l. (\<lambda>n. f (k n) x) \<longlonglongrightarrow> l"
+proof (cases "S = {}")
+  case True
+  then show ?thesis
+    using strict_mono_id that by fastforce
+next
+  case False
+  with \<open>countable S\<close> obtain \<sigma> :: "nat \<Rightarrow> 'a" where \<sigma>: "S = range \<sigma>"
+    using uncountable_def by blast
+  obtain k where "strict_mono k" and k: "\<And>i. \<exists>l. (\<lambda>n. (f \<circ> k) n (\<sigma> i)) \<longlonglongrightarrow> l"
+  proof (rule subsequence_diagonalization_lemma
+      [of "\<lambda>i r. \<exists>l. ((\<lambda>n. (f \<circ> r) n (\<sigma> i)) \<longlongrightarrow> l) sequentially" id])
+    show "\<exists>k::nat\<Rightarrow>nat. strict_mono k \<and> (\<exists>l. (\<lambda>n. (f \<circ> (r \<circ> k)) n (\<sigma> i)) \<longlonglongrightarrow> l)" for i r
+    proof -
+      have "f (r n) (\<sigma> i) \<in> cball 0 M" for n
+        by (simp add: \<sigma> M)
+      then show ?thesis
+        using compact_def [of "cball (0::'b) M"] apply simp
+        apply (drule_tac x="(\<lambda>n. f (r n) (\<sigma> i))" in spec)
+        apply (force simp: o_def)
+        done
+    qed
+    show "\<And>i r k1 k2 N.
+               \<lbrakk>\<exists>l. (\<lambda>n. (f \<circ> (r \<circ> k1)) n (\<sigma> i)) \<longlonglongrightarrow> l; \<And>j. N \<le> j \<Longrightarrow> \<exists>j'\<ge>j. k2 j = k1 j'\<rbrakk>
+               \<Longrightarrow> \<exists>l. (\<lambda>n. (f \<circ> (r \<circ> k2)) n (\<sigma> i)) \<longlonglongrightarrow> l"
+      apply (simp add: lim_sequentially)
+      apply (erule ex_forward all_forward imp_forward)+
+        apply auto
+      by (metis (no_types, hide_lams) le_cases order_trans)
+  qed auto
+  with \<sigma> that show ?thesis
+    by force
+qed
+
+
+theorem Arzela_Ascoli:
+  fixes \<F> :: "[nat,'a::euclidean_space] \<Rightarrow> 'b::{real_normed_vector,heine_borel}"
+  assumes "compact S"
+      and M: "\<And>n x. x \<in> S \<Longrightarrow> norm(\<F> n x) \<le> M"
+      and equicont:
+          "\<And>x e. \<lbrakk>x \<in> S; 0 < e\<rbrakk>
+                 \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>n y. y \<in> S \<and> norm(x - y) < d \<longrightarrow> norm(\<F> n x - \<F> n y) < e)"
+  obtains g k where "continuous_on S g" "strict_mono (k :: nat \<Rightarrow> nat)"
+                    "\<And>e. 0 < e \<Longrightarrow> \<exists>N. \<forall>n x. n \<ge> N \<and> x \<in> S \<longrightarrow> norm(\<F>(k n) x - g x) < e"
+proof -
+  have UEQ: "\<And>e. 0 < e \<Longrightarrow> \<exists>d. 0 < d \<and> (\<forall>n. \<forall>x \<in> S. \<forall>x' \<in> S. dist x' x < d \<longrightarrow> dist (\<F> n x') (\<F> n x) < e)"
+    apply (rule compact_uniformly_equicontinuous [OF \<open>compact S\<close>, of "range \<F>"])
+    using equicont by (force simp: dist_commute dist_norm)+
+  have "continuous_on S g"
+       if "\<And>e. 0 < e \<Longrightarrow> \<exists>N. \<forall>n x. n \<ge> N \<and> x \<in> S \<longrightarrow> norm(\<F>(r n) x - g x) < e"
+       for g:: "'a \<Rightarrow> 'b" and r :: "nat \<Rightarrow> nat"
+  proof (rule uniform_limit_theorem [of _ "\<F> \<circ> r"])
+    show "\<forall>\<^sub>F n in sequentially. continuous_on S ((\<F> \<circ> r) n)"
+      apply (simp add: eventually_sequentially)
+      apply (rule_tac x=0 in exI)
+      using UEQ apply (force simp: continuous_on_iff)
+      done
+    show "uniform_limit S (\<F> \<circ> r) g sequentially"
+      apply (simp add: uniform_limit_iff eventually_sequentially)
+        by (metis dist_norm that)
+  qed auto
+  moreover
+  obtain R where "countable R" "R \<subseteq> S" and SR: "S \<subseteq> closure R"
+    by (metis separable that)
+  obtain k where "strict_mono k" and k: "\<And>x. x \<in> R \<Longrightarrow> \<exists>l. (\<lambda>n. \<F> (k n) x) \<longlonglongrightarrow> l"
+    apply (rule function_convergent_subsequence [OF \<open>countable R\<close> M])
+    using \<open>R \<subseteq> S\<close> apply force+
+    done
+  then have Cauchy: "Cauchy ((\<lambda>n. \<F> (k n) x))" if "x \<in> R" for x
+    using convergent_eq_Cauchy that by blast
+  have "\<exists>N. \<forall>m n x. N \<le> m \<and> N \<le> n \<and> x \<in> S \<longrightarrow> dist ((\<F> \<circ> k) m x) ((\<F> \<circ> k) n x) < e"
+    if "0 < e" for e
+  proof -
+    obtain d where "0 < d"
+      and d: "\<And>n. \<forall>x \<in> S. \<forall>x' \<in> S. dist x' x < d \<longrightarrow> dist (\<F> n x') (\<F> n x) < e/3"
+      by (metis UEQ \<open>0 < e\<close> divide_pos_pos zero_less_numeral)
+    obtain T where "T \<subseteq> R" and "finite T" and T: "S \<subseteq> (\<Union>c\<in>T. ball c d)"
+    proof (rule compactE_image [OF  \<open>compact S\<close>, of R "(\<lambda>x. ball x d)"])
+      have "closure R \<subseteq> (\<Union>c\<in>R. ball c d)"
+        apply clarsimp
+        using \<open>0 < d\<close> closure_approachable by blast
+      with SR show "S \<subseteq> (\<Union>c\<in>R. ball c d)"
+        by auto
+    qed auto
+    have "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (\<F> (k m) x) (\<F> (k n) x) < e/3" if "x \<in> R" for x
+      using Cauchy \<open>0 < e\<close> that unfolding Cauchy_def
+      by (metis less_divide_eq_numeral1(1) mult_zero_left)
+    then obtain MF where MF: "\<And>x m n. \<lbrakk>x \<in> R; m \<ge> MF x; n \<ge> MF x\<rbrakk> \<Longrightarrow> norm (\<F> (k m) x - \<F> (k n) x) < e/3"
+      using dist_norm by metis
+    have "dist ((\<F> \<circ> k) m x) ((\<F> \<circ> k) n x) < e"
+         if m: "Max (MF ` T) \<le> m" and n: "Max (MF ` T) \<le> n" "x \<in> S" for m n x
+    proof -
+      obtain t where "t \<in> T" and t: "x \<in> ball t d"
+        using \<open>x \<in> S\<close> T by auto
+      have "norm(\<F> (k m) t - \<F> (k m) x) < e / 3"
+        by (metis \<open>R \<subseteq> S\<close> \<open>T \<subseteq> R\<close> \<open>t \<in> T\<close> d dist_norm mem_ball subset_iff t \<open>x \<in> S\<close>)
+      moreover
+      have "norm(\<F> (k n) t - \<F> (k n) x) < e / 3"
+        by (metis \<open>R \<subseteq> S\<close> \<open>T \<subseteq> R\<close> \<open>t \<in> T\<close> subsetD d dist_norm mem_ball t \<open>x \<in> S\<close>)
+      moreover
+      have "norm(\<F> (k m) t - \<F> (k n) t) < e / 3"
+      proof (rule MF)
+        show "t \<in> R"
+          using \<open>T \<subseteq> R\<close> \<open>t \<in> T\<close> by blast
+        show "MF t \<le> m" "MF t \<le> n"
+          by (meson Max_ge \<open>finite T\<close> \<open>t \<in> T\<close> finite_imageI imageI le_trans m n)+
+      qed
+      ultimately
+      show ?thesis
+        unfolding dist_norm [symmetric] o_def
+          by (metis dist_triangle_third dist_commute)
+    qed
+    then show ?thesis
+      by force
+  qed
+  then have "\<exists>g. \<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x \<in> S. norm(\<F>(k n) x - g x) < e"
+    using uniformly_convergent_eq_cauchy [of "\<lambda>x. x \<in> S" "\<F> \<circ> k"]
+    apply (simp add: o_def dist_norm)
+    by meson
+  ultimately show thesis
+    by (metis that \<open>strict_mono k\<close>)
+qed
+
+
+
+subsubsection\<^marker>\<open>tag important\<close>\<open>Montel's theorem\<close>
+
+text\<open>a sequence of holomorphic functions uniformly bounded
+on compact subsets of an open set S has a subsequence that converges to a
+holomorphic function, and converges \emph{uniformly} on compact subsets of S.\<close>
+
+
+theorem Montel:
+  fixes \<F> :: "[nat,complex] \<Rightarrow> complex"
+  assumes "open S"
+      and \<H>: "\<And>h. h \<in> \<H> \<Longrightarrow> h holomorphic_on S"
+      and bounded: "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> \<exists>B. \<forall>h \<in> \<H>. \<forall> z \<in> K. norm(h z) \<le> B"
+      and rng_f: "range \<F> \<subseteq> \<H>"
+  obtains g r
+    where "g holomorphic_on S" "strict_mono (r :: nat \<Rightarrow> nat)"
+          "\<And>x. x \<in> S \<Longrightarrow> ((\<lambda>n. \<F> (r n) x) \<longlongrightarrow> g x) sequentially"
+          "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> uniform_limit K (\<F> \<circ> r) g sequentially"        
+proof -
+  obtain K where comK: "\<And>n. compact(K n)" and KS: "\<And>n::nat. K n \<subseteq> S"
+             and subK: "\<And>X. \<lbrakk>compact X; X \<subseteq> S\<rbrakk> \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. X \<subseteq> K n"
+    using open_Union_compact_subsets [OF \<open>open S\<close>] by metis
+  then have "\<And>i. \<exists>B. \<forall>h \<in> \<H>. \<forall> z \<in> K i. norm(h z) \<le> B"
+    by (simp add: bounded)
+  then obtain B where B: "\<And>i h z. \<lbrakk>h \<in> \<H>; z \<in> K i\<rbrakk> \<Longrightarrow> norm(h z) \<le> B i"
+    by metis
+  have *: "\<exists>r g. strict_mono (r::nat\<Rightarrow>nat) \<and> (\<forall>e > 0. \<exists>N. \<forall>n\<ge>N. \<forall>x \<in> K i. norm((\<F> \<circ> r) n x - g x) < e)"
+        if "\<And>n. \<F> n \<in> \<H>" for \<F> i
+  proof -
+    obtain g k where "continuous_on (K i) g" "strict_mono (k::nat\<Rightarrow>nat)"
+                    "\<And>e. 0 < e \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. \<forall>x \<in> K i. norm(\<F>(k n) x - g x) < e"
+    proof (rule Arzela_Ascoli [of "K i" "\<F>" "B i"])
+      show "\<exists>d>0. \<forall>n y. y \<in> K i \<and> cmod (z - y) < d \<longrightarrow> cmod (\<F> n z - \<F> n y) < e"
+             if z: "z \<in> K i" and "0 < e" for z e
+      proof -
+        obtain r where "0 < r" and r: "cball z r \<subseteq> S"
+          using z KS [of i] \<open>open S\<close> by (force simp: open_contains_cball)
+        have "cball z (2 / 3 * r) \<subseteq> cball z r"
+          using \<open>0 < r\<close> by (simp add: cball_subset_cball_iff)
+        then have z23S: "cball z (2 / 3 * r) \<subseteq> S"
+          using r by blast
+        obtain M where "0 < M" and M: "\<And>n w. dist z w \<le> 2/3 * r \<Longrightarrow> norm(\<F> n w) \<le> M"
+        proof -
+          obtain N where N: "\<forall>n\<ge>N. cball z (2/3 * r) \<subseteq> K n"
+            using subK compact_cball [of z "(2 / 3 * r)"] z23S by force
+          have "cmod (\<F> n w) \<le> \<bar>B N\<bar> + 1" if "dist z w \<le> 2 / 3 * r" for n w
+          proof -
+            have "w \<in> K N"
+              using N mem_cball that by blast
+            then have "cmod (\<F> n w) \<le> B N"
+              using B \<open>\<And>n. \<F> n \<in> \<H>\<close> by blast
+            also have "... \<le> \<bar>B N\<bar> + 1"
+              by simp
+            finally show ?thesis .
+          qed
+          then show ?thesis
+            by (rule_tac M="\<bar>B N\<bar> + 1" in that) auto
+        qed
+        have "cmod (\<F> n z - \<F> n y) < e"
+              if "y \<in> K i" and y_near_z: "cmod (z - y) < r/3" "cmod (z - y) < e * r / (6 * M)"
+              for n y
+        proof -
+          have "((\<lambda>w. \<F> n w / (w - \<xi>)) has_contour_integral
+                    (2 * pi) * \<i> * winding_number (circlepath z (2 / 3 * r)) \<xi> * \<F> n \<xi>)
+                (circlepath z (2 / 3 * r))"
+             if "dist \<xi> z < (2 / 3 * r)" for \<xi>
+          proof (rule Cauchy_integral_formula_convex_simple)
+            have "\<F> n holomorphic_on S"
+              by (simp add: \<H> \<open>\<And>n. \<F> n \<in> \<H>\<close>)
+            with z23S show "\<F> n holomorphic_on cball z (2 / 3 * r)"
+              using holomorphic_on_subset by blast
+          qed (use that \<open>0 < r\<close> in \<open>auto simp: dist_commute\<close>)
+          then have *: "((\<lambda>w. \<F> n w / (w - \<xi>)) has_contour_integral (2 * pi) * \<i> * \<F> n \<xi>)
+                     (circlepath z (2 / 3 * r))"
+             if "dist \<xi> z < (2 / 3 * r)" for \<xi>
+            using that by (simp add: winding_number_circlepath dist_norm)
+           have y: "((\<lambda>w. \<F> n w / (w - y)) has_contour_integral (2 * pi) * \<i> * \<F> n y)
+                 (circlepath z (2 / 3 * r))"
+             apply (rule *)
+             using that \<open>0 < r\<close> by (simp only: dist_norm norm_minus_commute)
+           have z: "((\<lambda>w. \<F> n w / (w - z)) has_contour_integral (2 * pi) * \<i> * \<F> n z)
+                 (circlepath z (2 / 3 * r))"
+             apply (rule *)
+             using \<open>0 < r\<close> by simp
+           have le_er: "cmod (\<F> n x / (x - y) - \<F> n x / (x - z)) \<le> e / r"
+                if "cmod (x - z) = r/3 + r/3" for x
+           proof -
+             have "\<not> (cmod (x - y) < r/3)"
+               using y_near_z(1) that \<open>M > 0\<close> \<open>r > 0\<close>
+               by (metis (full_types) norm_diff_triangle_less norm_minus_commute order_less_irrefl)
+             then have r4_le_xy: "r/4 \<le> cmod (x - y)"
+               using \<open>r > 0\<close> by simp
+             then have neq: "x \<noteq> y" "x \<noteq> z"
+               using that \<open>r > 0\<close> by (auto simp: field_split_simps norm_minus_commute)
+             have leM: "cmod (\<F> n x) \<le> M"
+               by (simp add: M dist_commute dist_norm that)
+             have "cmod (\<F> n x / (x - y) - \<F> n x / (x - z)) = cmod (\<F> n x) * cmod (1 / (x - y) - 1 / (x - z))"
+               by (metis (no_types, lifting) divide_inverse mult.left_neutral norm_mult right_diff_distrib')
+             also have "... = cmod (\<F> n x) * cmod ((y - z) / ((x - y) * (x - z)))"
+               using neq by (simp add: field_split_simps)
+             also have "... = cmod (\<F> n x) * (cmod (y - z) / (cmod(x - y) * (2/3 * r)))"
+               by (simp add: norm_mult norm_divide that)
+             also have "... \<le> M * (cmod (y - z) / (cmod(x - y) * (2/3 * r)))"
+               apply (rule mult_mono)
+                  apply (rule leM)
+                 using \<open>r > 0\<close> \<open>M > 0\<close> neq by auto
+               also have "... < M * ((e * r / (6 * M)) / (cmod(x - y) * (2/3 * r)))"
+                 unfolding mult_less_cancel_left
+                 using y_near_z(2) \<open>M > 0\<close> \<open>r > 0\<close> neq
+                 apply (simp add: field_simps mult_less_0_iff norm_minus_commute)
+                 done
+             also have "... \<le> e/r"
+               using \<open>e > 0\<close> \<open>r > 0\<close> r4_le_xy by (simp add: field_split_simps)
+             finally show ?thesis by simp
+           qed
+           have "(2 * pi) * cmod (\<F> n y - \<F> n z) = cmod ((2 * pi) * \<i> * \<F> n y - (2 * pi) * \<i> * \<F> n z)"
+             by (simp add: right_diff_distrib [symmetric] norm_mult)
+           also have "cmod ((2 * pi) * \<i> * \<F> n y - (2 * pi) * \<i> * \<F> n z) \<le> e / r * (2 * pi * (2 / 3 * r))"
+             apply (rule has_contour_integral_bound_circlepath [OF has_contour_integral_diff [OF y z], of "e/r"])
+             using \<open>e > 0\<close> \<open>r > 0\<close> le_er by auto
+           also have "... = (2 * pi) * e * ((2 / 3))"
+             using \<open>r > 0\<close> by (simp add: field_split_simps)
+           finally have "cmod (\<F> n y - \<F> n z) \<le> e * (2 / 3)"
+             by simp
+           also have "... < e"
+             using \<open>e > 0\<close> by simp
+           finally show ?thesis by (simp add: norm_minus_commute)
+        qed
+        then show ?thesis
+          apply (rule_tac x="min (r/3) ((e * r)/(6 * M))" in exI)
+          using \<open>0 < e\<close> \<open>0 < r\<close> \<open>0 < M\<close> by simp
+      qed
+      show "\<And>n x.  x \<in> K i \<Longrightarrow> cmod (\<F> n x) \<le> B i"
+        using B \<open>\<And>n. \<F> n \<in> \<H>\<close> by blast
+    next
+      fix g :: "complex \<Rightarrow> complex" and k :: "nat \<Rightarrow> nat"
+      assume *: "\<And>(g::complex\<Rightarrow>complex) (k::nat\<Rightarrow>nat). continuous_on (K i) g \<Longrightarrow>
+                  strict_mono k \<Longrightarrow>
+                  (\<And>e. 0 < e \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. \<forall>x\<in>K i. cmod (\<F> (k n) x - g x) < e) \<Longrightarrow> thesis"
+           "continuous_on (K i) g"
+           "strict_mono k"
+           "\<And>e. 0 < e \<Longrightarrow> \<exists>N. \<forall>n x. N \<le> n \<and> x \<in> K i \<longrightarrow> cmod (\<F> (k n) x - g x) < e"
+      show ?thesis
+        by (rule *(1)[OF *(2,3)], drule *(4)) auto
+    qed (use comK in simp_all)
+    then show ?thesis
+      by auto
+  qed
+  have "\<exists>k g. strict_mono (k::nat\<Rightarrow>nat) \<and> (\<forall>e > 0. \<exists>N. \<forall>n\<ge>N. \<forall>x \<in> K i. norm((\<F> \<circ> r \<circ> k) n x - g x) < e)"
+         for i r
+    apply (rule *)
+    using rng_f by auto
+  then have **: "\<And>i r. \<exists>k. strict_mono (k::nat\<Rightarrow>nat) \<and> (\<exists>g. \<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x \<in> K i. norm((\<F> \<circ> (r \<circ> k)) n x - g x) < e)"
+    by (force simp: o_assoc)
+  obtain k :: "nat \<Rightarrow> nat" where "strict_mono k"
+             and "\<And>i. \<exists>g. \<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x\<in>K i. cmod ((\<F> \<circ> (id \<circ> k)) n x - g x) < e"
+    (* TODO: clean up this mess *)
+    apply (rule subsequence_diagonalization_lemma [OF **, of id id])
+     apply (erule ex_forward all_forward imp_forward)+
+      apply force
+     apply (erule exE)
+    apply (rename_tac i r k1 k2 N g e Na)
+     apply (rule_tac x="max N Na" in exI)
+     apply fastforce+
+    done
+  then have lt_e: "\<And>i. \<exists>g. \<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<forall>x\<in>K i. cmod ((\<F> \<circ> k) n x - g x) < e"
+    by simp
+  have "\<exists>l. \<forall>e>0. \<exists>N. \<forall>n\<ge>N. norm(\<F> (k n) z - l) < e" if "z \<in> S" for z
+  proof -
+    obtain G where G: "\<And>i e. e > 0 \<Longrightarrow> \<exists>M. \<forall>n\<ge>M. \<forall>x\<in>K i. cmod ((\<F> \<circ> k) n x - G i x) < e"
+      using lt_e by metis
+    obtain N where "\<And>n. n \<ge> N \<Longrightarrow> z \<in> K n"
+      using subK [of "{z}"] that \<open>z \<in> S\<close> by auto
+    moreover have "\<And>e. e > 0 \<Longrightarrow> \<exists>M. \<forall>n\<ge>M. \<forall>x\<in>K N. cmod ((\<F> \<circ> k) n x - G N x) < e"
+      using G by auto
+    ultimately show ?thesis
+      by (metis comp_apply order_refl)
+  qed
+  then obtain g where g: "\<And>z e. \<lbrakk>z \<in> S; e > 0\<rbrakk> \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. norm(\<F> (k n) z - g z) < e"
+    by metis
+  show ?thesis
+  proof
+    show g_lim: "\<And>x. x \<in> S \<Longrightarrow> (\<lambda>n. \<F> (k n) x) \<longlonglongrightarrow> g x"
+      by (simp add: lim_sequentially g dist_norm)    
+    have dg_le_e: "\<exists>N. \<forall>n\<ge>N. \<forall>x\<in>T. cmod (\<F> (k n) x - g x) < e"
+      if T: "compact T" "T \<subseteq> S" and "0 < e" for T e
+    proof -
+      obtain N where N: "\<And>n. n \<ge> N \<Longrightarrow> T \<subseteq> K n"
+        using subK [OF T] by blast
+      obtain h where h: "\<And>e. e>0 \<Longrightarrow> \<exists>M. \<forall>n\<ge>M. \<forall>x\<in>K N. cmod ((\<F> \<circ> k) n x - h x) < e"
+        using lt_e by blast
+      have geq: "g w = h w" if "w \<in> T" for w
+        apply (rule LIMSEQ_unique [of "\<lambda>n. \<F>(k n) w"])
+        using \<open>T \<subseteq> S\<close> g_lim that apply blast
+        using h N that by (force simp: lim_sequentially dist_norm)
+      show ?thesis
+        using T h N \<open>0 < e\<close> by (fastforce simp add: geq)
+    qed
+    then show "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk>
+         \<Longrightarrow> uniform_limit K (\<F> \<circ> k) g sequentially"
+      by (simp add: uniform_limit_iff dist_norm eventually_sequentially)
+    show "g holomorphic_on S"
+    proof (rule holomorphic_uniform_sequence [OF \<open>open S\<close> \<H>])
+      show "\<And>n. (\<F> \<circ> k) n \<in> \<H>"
+        by (simp add: range_subsetD rng_f)
+      show "\<exists>d>0. cball z d \<subseteq> S \<and> uniform_limit (cball z d) (\<lambda>n. (\<F> \<circ> k) n) g sequentially"
+        if "z \<in> S" for z
+      proof -
+        obtain d where d: "d>0" "cball z d \<subseteq> S"
+          using \<open>open S\<close> \<open>z \<in> S\<close> open_contains_cball by blast
+        then have "uniform_limit (cball z d) (\<F> \<circ> k) g sequentially"
+          using dg_le_e compact_cball by (auto simp: uniform_limit_iff eventually_sequentially dist_norm)
+        with d show ?thesis by blast
+      qed
+    qed
+  qed (auto simp: \<open>strict_mono k\<close>)
+qed
+
+
+
+subsection\<open>Some simple but useful cases of Hurwitz's theorem\<close>
+
+proposition Hurwitz_no_zeros:
+  assumes S: "open S" "connected S"
+      and holf: "\<And>n::nat. \<F> n holomorphic_on S"
+      and holg: "g holomorphic_on S"
+      and ul_g: "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> uniform_limit K \<F> g sequentially"
+      and nonconst: "\<not> g constant_on S"
+      and nz: "\<And>n z. z \<in> S \<Longrightarrow> \<F> n z \<noteq> 0"
+      and "z0 \<in> S"
+      shows "g z0 \<noteq> 0"
+proof
+  assume g0: "g z0 = 0"
+  obtain h r m
+    where "0 < m" "0 < r" and subS: "ball z0 r \<subseteq> S"
+      and holh: "h holomorphic_on ball z0 r"
+      and geq:  "\<And>w. w \<in> ball z0 r \<Longrightarrow> g w = (w - z0)^m * h w"
+      and hnz:  "\<And>w. w \<in> ball z0 r \<Longrightarrow> h w \<noteq> 0"
+    by (blast intro: holomorphic_factor_zero_nonconstant [OF holg S \<open>z0 \<in> S\<close> g0 nonconst])
+  then have holf0: "\<F> n holomorphic_on ball z0 r" for n
+    by (meson holf holomorphic_on_subset)
+  have *: "((\<lambda>z. deriv (\<F> n) z / \<F> n z) has_contour_integral 0) (circlepath z0 (r/2))" for n
+  proof (rule Cauchy_theorem_disc_simple [of _ z0 r])
+    show "(\<lambda>z. deriv (\<F> n) z / \<F> n z) holomorphic_on ball z0 r"
+      apply (intro holomorphic_intros holomorphic_deriv holf holf0 open_ball nz)
+      using \<open>ball z0 r \<subseteq> S\<close> by blast
+  qed (use \<open>0 < r\<close> in auto)
+  have hol_dg: "deriv g holomorphic_on S"
+    by (simp add: \<open>open S\<close> holg holomorphic_deriv)
+  have "continuous_on (sphere z0 (r/2)) (deriv g)"
+    apply (intro holomorphic_on_imp_continuous_on holomorphic_on_subset [OF hol_dg])
+    using \<open>0 < r\<close> subS by auto
+  then have "compact (deriv g ` (sphere z0 (r/2)))"
+    by (rule compact_continuous_image [OF _ compact_sphere])
+  then have bo_dg: "bounded (deriv g ` (sphere z0 (r/2)))"
+    using compact_imp_bounded by blast
+  have "continuous_on (sphere z0 (r/2)) (cmod \<circ> g)"
+    apply (intro continuous_intros holomorphic_on_imp_continuous_on holomorphic_on_subset [OF holg])
+    using \<open>0 < r\<close> subS by auto
+  then have "compact ((cmod \<circ> g) ` sphere z0 (r/2))"
+    by (rule compact_continuous_image [OF _ compact_sphere])
+  moreover have "(cmod \<circ> g) ` sphere z0 (r/2) \<noteq> {}"
+    using \<open>0 < r\<close> by auto
+  ultimately obtain b where b: "b \<in> (cmod \<circ> g) ` sphere z0 (r/2)"
+                               "\<And>t. t \<in> (cmod \<circ> g) ` sphere z0 (r/2) \<Longrightarrow> b \<le> t"
+    using compact_attains_inf [of "(norm \<circ> g) ` (sphere z0 (r/2))"] by blast
+  have "(\<lambda>n. contour_integral (circlepath z0 (r/2)) (\<lambda>z. deriv (\<F> n) z / \<F> n z)) \<longlonglongrightarrow>
+        contour_integral (circlepath z0 (r/2)) (\<lambda>z. deriv g z / g z)"
+  proof (rule contour_integral_uniform_limit_circlepath)
+    show "\<forall>\<^sub>F n in sequentially. (\<lambda>z. deriv (\<F> n) z / \<F> n z) contour_integrable_on circlepath z0 (r/2)"
+      using * contour_integrable_on_def eventually_sequentiallyI by meson
+    show "uniform_limit (sphere z0 (r/2)) (\<lambda>n z. deriv (\<F> n) z / \<F> n z) (\<lambda>z. deriv g z / g z) sequentially"
+    proof (rule uniform_lim_divide [OF _ _ bo_dg])
+      show "uniform_limit (sphere z0 (r/2)) (\<lambda>a. deriv (\<F> a)) (deriv g) sequentially"
+      proof (rule uniform_limitI)
+        fix e::real
+        assume "0 < e"
+        have *: "dist (deriv (\<F> n) w) (deriv g w) < e"
+          if e8: "\<And>x. dist z0 x \<le> 3 * r / 4 \<Longrightarrow> dist (\<F> n x) (g x) * 8 < r * e"
+          and w: "dist w z0 = r/2"  for n w
+        proof -
+          have "ball w (r/4) \<subseteq> ball z0 r"  "cball w (r/4) \<subseteq> ball z0 r"
+            using \<open>0 < r\<close> by (simp_all add: ball_subset_ball_iff cball_subset_ball_iff w)
+          with subS have wr4_sub: "ball w (r/4) \<subseteq> S" "cball w (r/4) \<subseteq> S" by force+
+          moreover
+          have "(\<lambda>z. \<F> n z - g z) holomorphic_on S"
+            by (intro holomorphic_intros holf holg)
+          ultimately have hol: "(\<lambda>z. \<F> n z - g z) holomorphic_on ball w (r/4)"
+            and cont: "continuous_on (cball w (r / 4)) (\<lambda>z. \<F> n z - g z)"
+            using holomorphic_on_subset by (blast intro: holomorphic_on_imp_continuous_on)+
+          have "w \<in> S"
+            using \<open>0 < r\<close> wr4_sub by auto
+          have "\<And>y. dist w y < r / 4 \<Longrightarrow> dist z0 y \<le> 3 * r / 4"
+            apply (rule dist_triangle_le [where z=w])
+            using w by (simp add: dist_commute)
+          with e8 have in_ball: "\<And>y. y \<in> ball w (r/4) \<Longrightarrow> \<F> n y - g y \<in> ball 0 (r/4 * e/2)"
+            by (simp add: dist_norm [symmetric])
+          have "\<F> n field_differentiable at w"
+            by (metis holomorphic_on_imp_differentiable_at \<open>w \<in> S\<close> holf \<open>open S\<close>)
+          moreover
+          have "g field_differentiable at w"
+            using \<open>w \<in> S\<close> \<open>open S\<close> holg holomorphic_on_imp_differentiable_at by auto
+          moreover
+          have "cmod (deriv (\<lambda>w. \<F> n w - g w) w) * 2 \<le> e"
+            apply (rule Cauchy_higher_deriv_bound [OF hol cont in_ball, of 1, simplified])
+            using \<open>r > 0\<close> by auto
+          ultimately have "dist (deriv (\<F> n) w) (deriv g w) \<le> e/2"
+            by (simp add: dist_norm)
+          then show ?thesis
+            using \<open>e > 0\<close> by auto
+        qed
+        have "cball z0 (3 * r / 4) \<subseteq> ball z0 r"
+          by (simp add: cball_subset_ball_iff \<open>0 < r\<close>)
+        with subS have "uniform_limit (cball z0 (3 * r/4)) \<F> g sequentially"
+          by (force intro: ul_g)
+        then have "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>cball z0 (3 * r / 4). dist (\<F> n x) (g x) < r / 4 * e / 2"
+          using \<open>0 < e\<close> \<open>0 < r\<close> by (force simp: intro!: uniform_limitD)
+        then show "\<forall>\<^sub>F n in sequentially. \<forall>x \<in> sphere z0 (r/2). dist (deriv (\<F> n) x) (deriv g x) < e"
+          apply (simp add: eventually_sequentially)
+          apply (elim ex_forward all_forward imp_forward asm_rl)
+          using * apply (force simp: dist_commute)
+          done
+      qed
+      show "uniform_limit (sphere z0 (r/2)) \<F> g sequentially"
+      proof (rule uniform_limitI)
+        fix e::real
+        assume "0 < e"
+        have "sphere z0 (r/2) \<subseteq> ball z0 r"
+          using \<open>0 < r\<close> by auto
+        with subS have "uniform_limit (sphere z0 (r/2)) \<F> g sequentially"
+          by (force intro: ul_g)
+        then show "\<forall>\<^sub>F n in sequentially. \<forall>x \<in> sphere z0 (r/2). dist (\<F> n x) (g x) < e"
+          apply (rule uniform_limitD)
+          using \<open>0 < e\<close> by force
+      qed
+      show "b > 0" "\<And>x. x \<in> sphere z0 (r/2) \<Longrightarrow> b \<le> cmod (g x)"
+        using b \<open>0 < r\<close> by (fastforce simp: geq hnz)+
+    qed
+  qed (use \<open>0 < r\<close> in auto)
+  then have "(\<lambda>n. 0) \<longlonglongrightarrow> contour_integral (circlepath z0 (r/2)) (\<lambda>z. deriv g z / g z)"
+    by (simp add: contour_integral_unique [OF *])
+  then have "contour_integral (circlepath z0 (r/2)) (\<lambda>z. deriv g z / g z) = 0"
+    by (simp add: LIMSEQ_const_iff)
+  moreover
+  have "contour_integral (circlepath z0 (r/2)) (\<lambda>z. deriv g z / g z) =
+        contour_integral (circlepath z0 (r/2)) (\<lambda>z. m / (z - z0) + deriv h z / h z)"
+  proof (rule contour_integral_eq, use \<open>0 < r\<close> in simp)
+    fix w
+    assume w: "dist z0 w * 2 = r"
+    then have w_inb: "w \<in> ball z0 r"
+      using \<open>0 < r\<close> by auto
+    have h_der: "(h has_field_derivative deriv h w) (at w)"
+      using holh holomorphic_derivI w_inb by blast
+    have "deriv g w = ((of_nat m * h w + deriv h w * (w - z0)) * (w - z0) ^ m) / (w - z0)"
+         if "r = dist z0 w * 2" "w \<noteq> z0"
+    proof -
+      have "((\<lambda>w. (w - z0) ^ m * h w) has_field_derivative
+            (m * h w + deriv h w * (w - z0)) * (w - z0) ^ m / (w - z0)) (at w)"
+        apply (rule derivative_eq_intros h_der refl)+
+        using that \<open>m > 0\<close> \<open>0 < r\<close> apply (simp add: divide_simps distrib_right)
+        apply (metis Suc_pred mult.commute power_Suc)
+        done
+      then show ?thesis
+        apply (rule DERIV_imp_deriv [OF has_field_derivative_transform_within_open [where S = "ball z0 r"]])
+        using that \<open>m > 0\<close> \<open>0 < r\<close>
+          apply (simp_all add: hnz geq)
+        done
+    qed
+    with \<open>0 < r\<close> \<open>0 < m\<close> w w_inb show "deriv g w / g w = of_nat m / (w - z0) + deriv h w / h w"
+      by (auto simp: geq field_split_simps hnz)
+  qed
+  moreover
+  have "contour_integral (circlepath z0 (r/2)) (\<lambda>z. m / (z - z0) + deriv h z / h z) =
+        2 * of_real pi * \<i> * m + 0"
+  proof (rule contour_integral_unique [OF has_contour_integral_add])
+    show "((\<lambda>x. m / (x - z0)) has_contour_integral 2 * of_real pi * \<i> * m) (circlepath z0 (r/2))"
+      by (force simp: \<open>0 < r\<close> intro: Cauchy_integral_circlepath_simple)
+    show "((\<lambda>x. deriv h x / h x) has_contour_integral 0) (circlepath z0 (r/2))"
+      apply (rule Cauchy_theorem_disc_simple [of _ z0 r])
+      using hnz holh holomorphic_deriv holomorphic_on_divide \<open>0 < r\<close>
+         apply force+
+      done
+  qed
+  ultimately show False using \<open>0 < m\<close> by auto
+qed
+
+corollary Hurwitz_injective:
+  assumes S: "open S" "connected S"
+      and holf: "\<And>n::nat. \<F> n holomorphic_on S"
+      and holg: "g holomorphic_on S"
+      and ul_g: "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> uniform_limit K \<F> g sequentially"
+      and nonconst: "\<not> g constant_on S"
+      and inj: "\<And>n. inj_on (\<F> n) S"
+    shows "inj_on g S"
+proof -
+  have False if z12: "z1 \<in> S" "z2 \<in> S" "z1 \<noteq> z2" "g z2 = g z1" for z1 z2
+  proof -
+    obtain z0 where "z0 \<in> S" and z0: "g z0 \<noteq> g z2"
+      using constant_on_def nonconst by blast
+    have "(\<lambda>z. g z - g z1) holomorphic_on S"
+      by (intro holomorphic_intros holg)
+    then obtain r where "0 < r" "ball z2 r \<subseteq> S" "\<And>z. dist z2 z < r \<and> z \<noteq> z2 \<Longrightarrow> g z \<noteq> g z1"
+      apply (rule isolated_zeros [of "\<lambda>z. g z - g z1" S z2 z0])
+      using S \<open>z0 \<in> S\<close> z0 z12 by auto
+    have "g z2 - g z1 \<noteq> 0"
+    proof (rule Hurwitz_no_zeros [of "S - {z1}" "\<lambda>n z. \<F> n z - \<F> n z1" "\<lambda>z. g z - g z1"])
+      show "open (S - {z1})"
+        by (simp add: S open_delete)
+      show "connected (S - {z1})"
+        by (simp add: connected_open_delete [OF S])
+      show "\<And>n. (\<lambda>z. \<F> n z - \<F> n z1) holomorphic_on S - {z1}"
+        by (intro holomorphic_intros holomorphic_on_subset [OF holf]) blast
+      show "(\<lambda>z. g z - g z1) holomorphic_on S - {z1}"
+        by (intro holomorphic_intros holomorphic_on_subset [OF holg]) blast
+      show "uniform_limit K (\<lambda>n z. \<F> n z - \<F> n z1) (\<lambda>z. g z - g z1) sequentially"
+           if "compact K" "K \<subseteq> S - {z1}" for K
+      proof (rule uniform_limitI)
+        fix e::real
+        assume "e > 0"
+        have "uniform_limit K \<F> g sequentially"
+          using that ul_g by fastforce
+        then have K: "\<forall>\<^sub>F n in sequentially. \<forall>x \<in> K. dist (\<F> n x) (g x) < e/2"
+          using \<open>0 < e\<close> by (force simp: intro!: uniform_limitD)
+        have "uniform_limit {z1} \<F> g sequentially"
+          by (simp add: ul_g z12)
+        then have "\<forall>\<^sub>F n in sequentially. \<forall>x \<in> {z1}. dist (\<F> n x) (g x) < e/2"
+          using \<open>0 < e\<close> by (force simp: intro!: uniform_limitD)
+        then have z1: "\<forall>\<^sub>F n in sequentially. dist (\<F> n z1) (g z1) < e/2"
+          by simp
+        have "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>K. dist (\<F> n x - \<F> n z1) (g x - g z1) < e/2 + e/2"
+          apply (rule eventually_mono [OF eventually_conj [OF K z1]])
+          apply (simp add: dist_norm algebra_simps del: divide_const_simps)
+          by (metis add.commute dist_commute dist_norm dist_triangle_add_half)
+        have "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>K. dist (\<F> n x - \<F> n z1) (g x - g z1) < e/2 + e/2"
+          using eventually_conj [OF K z1]
+          apply (rule eventually_mono)
+          by (metis (no_types, hide_lams) diff_add_eq diff_diff_eq2 dist_commute dist_norm dist_triangle_add_half field_sum_of_halves)
+        then
+        show "\<forall>\<^sub>F n in sequentially. \<forall>x\<in>K. dist (\<F> n x - \<F> n z1) (g x - g z1) < e"
+          by simp
+      qed
+      show "\<not> (\<lambda>z. g z - g z1) constant_on S - {z1}"
+        unfolding constant_on_def
+        by (metis Diff_iff \<open>z0 \<in> S\<close> empty_iff insert_iff right_minus_eq z0 z12)
+      show "\<And>n z. z \<in> S - {z1} \<Longrightarrow> \<F> n z - \<F> n z1 \<noteq> 0"
+        by (metis DiffD1 DiffD2 eq_iff_diff_eq_0 inj inj_onD insertI1 \<open>z1 \<in> S\<close>)
+      show "z2 \<in> S - {z1}"
+        using \<open>z2 \<in> S\<close> \<open>z1 \<noteq> z2\<close> by auto
+    qed
+    with z12 show False by auto
+  qed
+  then show ?thesis by (auto simp: inj_on_def)
+qed
+
+
+
+subsection\<open>The Great Picard theorem\<close>
+
+lemma GPicard1:
+  assumes S: "open S" "connected S" and "w \<in> S" "0 < r" "Y \<subseteq> X"
+      and holX: "\<And>h. h \<in> X \<Longrightarrow> h holomorphic_on S"
+      and X01:  "\<And>h z. \<lbrakk>h \<in> X; z \<in> S\<rbrakk> \<Longrightarrow> h z \<noteq> 0 \<and> h z \<noteq> 1"
+      and r:    "\<And>h. h \<in> Y \<Longrightarrow> norm(h w) \<le> r"
+  obtains B Z where "0 < B" "open Z" "w \<in> Z" "Z \<subseteq> S" "\<And>h z. \<lbrakk>h \<in> Y; z \<in> Z\<rbrakk> \<Longrightarrow> norm(h z) \<le> B"
+proof -
+  obtain e where "e > 0" and e: "cball w e \<subseteq> S"
+    using assms open_contains_cball_eq by blast
+  show ?thesis
+  proof
+    show "0 < exp(pi * exp(pi * (2 + 2 * r + 12)))"
+      by simp
+    show "ball w (e / 2) \<subseteq> S"
+      using e ball_divide_subset_numeral ball_subset_cball by blast
+    show "cmod (h z) \<le> exp (pi * exp (pi * (2 + 2 * r + 12)))"
+         if "h \<in> Y" "z \<in> ball w (e / 2)" for h z
+    proof -
+      have "h \<in> X"
+        using \<open>Y \<subseteq> X\<close> \<open>h \<in> Y\<close>  by blast
+      with holX have "h holomorphic_on S" 
+        by auto
+      then have "h holomorphic_on cball w e"
+        by (metis e holomorphic_on_subset)
+      then have hol_h_o: "(h \<circ> (\<lambda>z. (w + of_real e * z))) holomorphic_on cball 0 1"
+        apply (intro holomorphic_intros holomorphic_on_compose)
+        apply (erule holomorphic_on_subset)
+        using that \<open>e > 0\<close> by (auto simp: dist_norm norm_mult)
+      have norm_le_r: "cmod ((h \<circ> (\<lambda>z. w + complex_of_real e * z)) 0) \<le> r"
+        by (auto simp: r \<open>h \<in> Y\<close>)
+      have le12: "norm (of_real(inverse e) * (z - w)) \<le> 1/2"
+        using that \<open>e > 0\<close> by (simp add: inverse_eq_divide dist_norm norm_minus_commute norm_divide)
+      have non01: "\<And>z::complex. cmod z \<le> 1 \<Longrightarrow> h (w + e * z) \<noteq> 0 \<and> h (w + e * z) \<noteq> 1"
+        apply (rule X01 [OF \<open>h \<in> X\<close>])
+          apply (rule subsetD [OF e])
+        using \<open>0 < e\<close>  by (auto simp: dist_norm norm_mult)
+      have "cmod (h z) \<le> cmod (h (w + of_real e * (inverse e * (z - w))))"
+        using \<open>0 < e\<close> by (simp add: field_split_simps)
+      also have "... \<le> exp (pi * exp (pi * (14 + 2 * r)))"
+        using r [OF \<open>h \<in> Y\<close>] Schottky [OF hol_h_o norm_le_r _ _ _ le12] non01 by auto
+      finally
+      show ?thesis by simp
+    qed
+  qed (use \<open>e > 0\<close> in auto)
+qed 
+
+lemma GPicard2:
+  assumes "S \<subseteq> T" "connected T" "S \<noteq> {}" "open S" "\<And>x. \<lbrakk>x islimpt S; x \<in> T\<rbrakk> \<Longrightarrow> x \<in> S"
+    shows "S = T"
+  by (metis assms open_subset connected_clopen closedin_limpt)
+
+    
+lemma GPicard3:
+  assumes S: "open S" "connected S" "w \<in> S" and "Y \<subseteq> X"
+      and holX: "\<And>h. h \<in> X \<Longrightarrow> h holomorphic_on S"
+      and X01:  "\<And>h z. \<lbrakk>h \<in> X; z \<in> S\<rbrakk> \<Longrightarrow> h z \<noteq> 0 \<and> h z \<noteq> 1"
+      and no_hw_le1: "\<And>h. h \<in> Y \<Longrightarrow> norm(h w) \<le> 1"
+      and "compact K" "K \<subseteq> S"
+  obtains B where "\<And>h z. \<lbrakk>h \<in> Y; z \<in> K\<rbrakk> \<Longrightarrow> norm(h z) \<le> B"
+proof -
+  define U where "U \<equiv> {z \<in> S. \<exists>B Z. 0 < B \<and> open Z \<and> z \<in> Z \<and> Z \<subseteq> S \<and>
+                               (\<forall>h z'. h \<in> Y \<and> z' \<in> Z \<longrightarrow> norm(h z') \<le> B)}"
+  then have "U \<subseteq> S" by blast
+  have "U = S"
+  proof (rule GPicard2 [OF \<open>U \<subseteq> S\<close> \<open>connected S\<close>])
+    show "U \<noteq> {}"
+    proof -
+      obtain B Z where "0 < B" "open Z" "w \<in> Z" "Z \<subseteq> S" 
+        and  "\<And>h z. \<lbrakk>h \<in> Y; z \<in> Z\<rbrakk> \<Longrightarrow> norm(h z) \<le> B"
+        apply (rule GPicard1 [OF S zero_less_one \<open>Y \<subseteq> X\<close> holX])
+        using no_hw_le1 X01 by force+
+      then show ?thesis
+        unfolding U_def using \<open>w \<in> S\<close> by blast
+    qed
+    show "open U"
+      unfolding open_subopen [of U] by (auto simp: U_def)
+    fix v
+    assume v: "v islimpt U" "v \<in> S"
+    have "\<not> (\<forall>r>0. \<exists>h\<in>Y. r < cmod (h v))"
+    proof
+      assume "\<forall>r>0. \<exists>h\<in>Y. r < cmod (h v)"
+      then have "\<forall>n. \<exists>h\<in>Y. Suc n < cmod (h v)"
+        by simp
+      then obtain \<F> where FY: "\<And>n. \<F> n \<in> Y" and ltF: "\<And>n. Suc n < cmod (\<F> n v)"
+        by metis
+      define \<G> where "\<G> \<equiv> \<lambda>n z. inverse(\<F> n z)"
+      have hol\<G>: "\<G> n holomorphic_on S" for n
+        apply (simp add: \<G>_def)
+        using FY X01 \<open>Y \<subseteq> X\<close> holX apply (blast intro: holomorphic_on_inverse)
+        done
+      have \<G>not0: "\<G> n z \<noteq> 0" and \<G>not1: "\<G> n z \<noteq> 1" if "z \<in> S" for n z
+        using FY X01 \<open>Y \<subseteq> X\<close> that by (force simp: \<G>_def)+
+      have \<G>_le1: "cmod (\<G> n v) \<le> 1" for n 
+        using less_le_trans linear ltF 
+        by (fastforce simp add: \<G>_def norm_inverse inverse_le_1_iff)
+      define W where "W \<equiv> {h. h holomorphic_on S \<and> (\<forall>z \<in> S. h z \<noteq> 0 \<and> h z \<noteq> 1)}"
+      obtain B Z where "0 < B" "open Z" "v \<in> Z" "Z \<subseteq> S" 
+                   and B: "\<And>h z. \<lbrakk>h \<in> range \<G>; z \<in> Z\<rbrakk> \<Longrightarrow> norm(h z) \<le> B"
+        apply (rule GPicard1 [OF \<open>open S\<close> \<open>connected S\<close> \<open>v \<in> S\<close> zero_less_one, of "range \<G>" W])
+        using hol\<G> \<G>not0 \<G>not1 \<G>_le1 by (force simp: W_def)+
+      then obtain e where "e > 0" and e: "ball v e \<subseteq> Z"
+        by (meson open_contains_ball)
+      obtain h j where holh: "h holomorphic_on ball v e" and "strict_mono j"
+                   and lim:  "\<And>x. x \<in> ball v e \<Longrightarrow> (\<lambda>n. \<G> (j n) x) \<longlonglongrightarrow> h x"
+                   and ulim: "\<And>K. \<lbrakk>compact K; K \<subseteq> ball v e\<rbrakk>
+                                  \<Longrightarrow> uniform_limit K (\<G> \<circ> j) h sequentially"
+      proof (rule Montel)
+        show "\<And>h. h \<in> range \<G> \<Longrightarrow> h holomorphic_on ball v e"
+          by (metis \<open>Z \<subseteq> S\<close> e hol\<G> holomorphic_on_subset imageE)
+        show "\<And>K. \<lbrakk>compact K; K \<subseteq> ball v e\<rbrakk> \<Longrightarrow> \<exists>B. \<forall>h\<in>range \<G>. \<forall>z\<in>K. cmod (h z) \<le> B"
+          using B e by blast
+      qed auto
+      have "h v = 0"
+      proof (rule LIMSEQ_unique)
+        show "(\<lambda>n. \<G> (j n) v) \<longlonglongrightarrow> h v"
+          using \<open>e > 0\<close> lim by simp
+        have lt_Fj: "real x \<le> cmod (\<F> (j x) v)" for x
+          by (metis of_nat_Suc ltF \<open>strict_mono j\<close> add.commute less_eq_real_def less_le_trans nat_le_real_less seq_suble)
+        show "(\<lambda>n. \<G> (j n) v) \<longlonglongrightarrow> 0"
+        proof (rule Lim_null_comparison [OF eventually_sequentiallyI lim_inverse_n])
+          show "cmod (\<G> (j x) v) \<le> inverse (real x)" if "1 \<le> x" for x
+            using that by (simp add: \<G>_def norm_inverse_le_norm [OF lt_Fj])
+        qed        
+      qed
+      have "h v \<noteq> 0"
+      proof (rule Hurwitz_no_zeros [of "ball v e" "\<G> \<circ> j" h])
+        show "\<And>n. (\<G> \<circ> j) n holomorphic_on ball v e"
+          using \<open>Z \<subseteq> S\<close> e hol\<G> by force
+        show "\<And>n z. z \<in> ball v e \<Longrightarrow> (\<G> \<circ> j) n z \<noteq> 0"
+          using \<G>not0 \<open>Z \<subseteq> S\<close> e by fastforce
+        show "\<not> h constant_on ball v e"
+        proof (clarsimp simp: constant_on_def)
+          fix c
+          have False if "\<And>z. dist v z < e \<Longrightarrow> h z = c"  
+          proof -
+            have "h v = c"
+              by (simp add: \<open>0 < e\<close> that)
+            obtain y where "y \<in> U" "y \<noteq> v" and y: "dist y v < e"
+              using v \<open>e > 0\<close> by (auto simp: islimpt_approachable)
+            then obtain C T where "y \<in> S" "C > 0" "open T" "y \<in> T" "T \<subseteq> S"
+              and "\<And>h z'. \<lbrakk>h \<in> Y; z' \<in> T\<rbrakk> \<Longrightarrow> cmod (h z') \<le> C"
+              using \<open>y \<in> U\<close> by (auto simp: U_def)
+            then have le_C: "\<And>n. cmod (\<F> n y) \<le> C"
+              using FY by blast                
+            have "\<forall>\<^sub>F n in sequentially. dist (\<G> (j n) y) (h y) < inverse C"
+              using uniform_limitD [OF ulim [of "{y}"], of "inverse C"] \<open>C > 0\<close> y
+              by (simp add: dist_commute)
+            then obtain n where "dist (\<G> (j n) y) (h y) < inverse C"
+              by (meson eventually_at_top_linorder order_refl)
+            moreover
+            have "h y = h v"
+              by (metis \<open>h v = c\<close> dist_commute that y)
+            ultimately have "norm (\<G> (j n) y) < inverse C"
+              by (simp add: \<open>h v = 0\<close>)
+            then have "C < norm (\<F> (j n) y)"
+              apply (simp add: \<G>_def)
+              by (metis FY X01 \<open>0 < C\<close> \<open>y \<in> S\<close> \<open>Y \<subseteq> X\<close> inverse_less_iff_less norm_inverse subsetD zero_less_norm_iff)
+            show False
+              using \<open>C < cmod (\<F> (j n) y)\<close> le_C not_less by blast
+          qed
+          then show "\<exists>x\<in>ball v e. h x \<noteq> c" by force
+        qed
+        show "h holomorphic_on ball v e"
+          by (simp add: holh)
+        show "\<And>K. \<lbrakk>compact K; K \<subseteq> ball v e\<rbrakk> \<Longrightarrow> uniform_limit K (\<G> \<circ> j) h sequentially"
+          by (simp add: ulim)
+      qed (use \<open>e > 0\<close> in auto)
+      with \<open>h v = 0\<close> show False by blast
+    qed
+    then show "v \<in> U"
+      apply (clarsimp simp add: U_def v)
+      apply (rule GPicard1[OF \<open>open S\<close> \<open>connected S\<close> \<open>v \<in> S\<close> _ \<open>Y \<subseteq> X\<close> holX])
+      using X01 no_hw_le1 apply (meson | force simp: not_less)+
+      done
+  qed
+  have "\<And>x. x \<in> K \<longrightarrow> x \<in> U"
+    using \<open>U = S\<close> \<open>K \<subseteq> S\<close> by blast
+  then have "\<And>x. x \<in> K \<longrightarrow> (\<exists>B Z. 0 < B \<and> open Z \<and> x \<in> Z \<and> 
+                               (\<forall>h z'. h \<in> Y \<and> z' \<in> Z \<longrightarrow> norm(h z') \<le> B))"
+    unfolding U_def by blast
+  then obtain F Z where F: "\<And>x. x \<in> K \<Longrightarrow> open (Z x) \<and> x \<in> Z x \<and> 
+                               (\<forall>h z'. h \<in> Y \<and> z' \<in> Z x \<longrightarrow> norm(h z') \<le> F x)"
+    by metis
+  then obtain L where "L \<subseteq> K" "finite L" and L: "K \<subseteq> (\<Union>c \<in> L. Z c)"
+    by (auto intro: compactE_image [OF \<open>compact K\<close>, of K Z])
+  then have *: "\<And>x h z'. \<lbrakk>x \<in> L; h \<in> Y \<and> z' \<in> Z x\<rbrakk> \<Longrightarrow> cmod (h z') \<le> F x"
+    using F by blast
+  have "\<exists>B. \<forall>h z. h \<in> Y \<and> z \<in> K \<longrightarrow> norm(h z) \<le> B"
+  proof (cases "L = {}")
+    case True with L show ?thesis by simp
+  next
+    case False
+    with \<open>finite L\<close> show ?thesis 
+      apply (rule_tac x = "Max (F ` L)" in exI)
+      apply (simp add: linorder_class.Max_ge_iff)
+      using * F  by (metis L UN_E subsetD)
+  qed
+  with that show ?thesis by metis
+qed
+
+
+lemma GPicard4:
+  assumes "0 < k" and holf: "f holomorphic_on (ball 0 k - {0})" 
+      and AE: "\<And>e. \<lbrakk>0 < e; e < k\<rbrakk> \<Longrightarrow> \<exists>d. 0 < d \<and> d < e \<and> (\<forall>z \<in> sphere 0 d. norm(f z) \<le> B)"
+  obtains \<epsilon> where "0 < \<epsilon>" "\<epsilon> < k" "\<And>z. z \<in> ball 0 \<epsilon> - {0} \<Longrightarrow> norm(f z) \<le> B"
+proof -
+  obtain \<epsilon> where "0 < \<epsilon>" "\<epsilon> < k/2" and \<epsilon>: "\<And>z. norm z = \<epsilon> \<Longrightarrow> norm(f z) \<le> B"
+    using AE [of "k/2"] \<open>0 < k\<close> by auto
+  show ?thesis
+  proof
+    show "\<epsilon> < k"
+      using \<open>0 < k\<close> \<open>\<epsilon> < k/2\<close> by auto
+    show "cmod (f \<xi>) \<le> B" if \<xi>: "\<xi> \<in> ball 0 \<epsilon> - {0}" for \<xi>
+    proof -
+      obtain d where "0 < d" "d < norm \<xi>" and d: "\<And>z. norm z = d \<Longrightarrow> norm(f z) \<le> B"
+        using AE [of "norm \<xi>"] \<open>\<epsilon> < k\<close> \<xi> by auto
+      have [simp]: "closure (cball 0 \<epsilon> - ball 0 d) = cball 0 \<epsilon> - ball 0 d"
+        by (blast intro!: closure_closed)
+      have [simp]: "interior (cball 0 \<epsilon> - ball 0 d) = ball 0 \<epsilon> - cball (0::complex) d"
+        using \<open>0 < \<epsilon>\<close> \<open>0 < d\<close> by (simp add: interior_diff)
+      have *: "norm(f w) \<le> B" if "w \<in> cball 0 \<epsilon> - ball 0 d" for w
+      proof (rule maximum_modulus_frontier [of f "cball 0 \<epsilon> - ball 0 d"])
+        show "f holomorphic_on interior (cball 0 \<epsilon> - ball 0 d)"
+          apply (rule holomorphic_on_subset [OF holf])
+          using \<open>\<epsilon> < k\<close> \<open>0 < d\<close> that by auto
+        show "continuous_on (closure (cball 0 \<epsilon> - ball 0 d)) f"
+          apply (rule holomorphic_on_imp_continuous_on)
+          apply (rule holomorphic_on_subset [OF holf])
+          using \<open>0 < d\<close> \<open>\<epsilon> < k\<close> by auto
+        show "\<And>z. z \<in> frontier (cball 0 \<epsilon> - ball 0 d) \<Longrightarrow> cmod (f z) \<le> B"
+          apply (simp add: frontier_def)
+          using \<epsilon> d less_eq_real_def by blast
+      qed (use that in auto)
+      show ?thesis
+        using * \<open>d < cmod \<xi>\<close> that by auto
+    qed
+  qed (use \<open>0 < \<epsilon>\<close> in auto)
+qed
+  
+
+lemma GPicard5:
+  assumes holf: "f holomorphic_on (ball 0 1 - {0})"
+      and f01:  "\<And>z. z \<in> ball 0 1 - {0} \<Longrightarrow> f z \<noteq> 0 \<and> f z \<noteq> 1"
+  obtains e B where "0 < e" "e < 1" "0 < B" 
+                    "(\<forall>z \<in> ball 0 e - {0}. norm(f z) \<le> B) \<or>
+                     (\<forall>z \<in> ball 0 e - {0}. norm(f z) \<ge> B)"
+proof -
+  have [simp]: "1 + of_nat n \<noteq> (0::complex)" for n
+    using of_nat_eq_0_iff by fastforce
+  have [simp]: "cmod (1 + of_nat n) = 1 + of_nat n" for n
+    by (metis norm_of_nat of_nat_Suc)
+  have *: "(\<lambda>x::complex. x / of_nat (Suc n)) ` (ball 0 1 - {0}) \<subseteq> ball 0 1 - {0}" for n
+    by (auto simp: norm_divide field_split_simps split: if_split_asm)
+  define h where "h \<equiv> \<lambda>n z::complex. f (z / (Suc n))"
+  have holh: "(h n) holomorphic_on ball 0 1 - {0}" for n
+    unfolding h_def
+  proof (rule holomorphic_on_compose_gen [unfolded o_def, OF _ holf *])
+    show "(\<lambda>x. x / of_nat (Suc n)) holomorphic_on ball 0 1 - {0}"
+      by (intro holomorphic_intros) auto
+  qed
+  have h01: "\<And>n z. z \<in> ball 0 1 - {0} \<Longrightarrow> h n z \<noteq> 0 \<and> h n z \<noteq> 1" 
+    unfolding h_def
+    apply (rule f01)
+    using * by force
+  obtain w where w: "w \<in> ball 0 1 - {0::complex}"
+    by (rule_tac w = "1/2" in that) auto
+  consider "infinite {n. norm(h n w) \<le> 1}" | "infinite {n. 1 \<le> norm(h n w)}"
+    by (metis (mono_tags, lifting) infinite_nat_iff_unbounded_le le_cases mem_Collect_eq)
+  then show ?thesis
+  proof cases
+    case 1
+    with infinite_enumerate obtain r :: "nat \<Rightarrow> nat" 
+      where "strict_mono r" and r: "\<And>n. r n \<in> {n. norm(h n w) \<le> 1}"
+      by blast
+    obtain B where B: "\<And>j z. \<lbrakk>norm z = 1/2; j \<in> range (h \<circ> r)\<rbrakk> \<Longrightarrow> norm(j z) \<le> B"
+    proof (rule GPicard3 [OF _ _ w, where K = "sphere 0 (1/2)"])  
+      show "range (h \<circ> r) \<subseteq> 
+            {g. g holomorphic_on ball 0 1 - {0} \<and> (\<forall>z\<in>ball 0 1 - {0}. g z \<noteq> 0 \<and> g z \<noteq> 1)}"
+        apply clarsimp
+        apply (intro conjI holomorphic_intros holomorphic_on_compose holh)
+        using h01 apply auto
+        done
+      show "connected (ball 0 1 - {0::complex})"
+        by (simp add: connected_open_delete)
+    qed (use r in auto)        
+    have normf_le_B: "cmod(f z) \<le> B" if "norm z = 1 / (2 * (1 + of_nat (r n)))" for z n
+    proof -
+      have *: "\<And>w. norm w = 1/2 \<Longrightarrow> cmod((f (w / (1 + of_nat (r n))))) \<le> B"
+        using B by (auto simp: h_def o_def)
+      have half: "norm (z * (1 + of_nat (r n))) = 1/2"
+        by (simp add: norm_mult divide_simps that)
+      show ?thesis
+        using * [OF half] by simp
+    qed
+    obtain \<epsilon> where "0 < \<epsilon>" "\<epsilon> < 1" "\<And>z. z \<in> ball 0 \<epsilon> - {0} \<Longrightarrow> cmod(f z) \<le> B"
+    proof (rule GPicard4 [OF zero_less_one holf, of B])
+      fix e::real
+      assume "0 < e" "e < 1"
+      obtain n where "(1/e - 2) / 2 < real n"
+        using reals_Archimedean2 by blast
+      also have "... \<le> r n"
+        using \<open>strict_mono r\<close> by (simp add: seq_suble)
+      finally have "(1/e - 2) / 2 < real (r n)" .
+      with \<open>0 < e\<close> have e: "e > 1 / (2 + 2 * real (r n))"
+        by (simp add: field_simps)
+      show "\<exists>d>0. d < e \<and> (\<forall>z\<in>sphere 0 d. cmod (f z) \<le> B)"
+        apply (rule_tac x="1 / (2 * (1 + of_nat (r n)))" in exI)
+        using normf_le_B by (simp add: e)
+    qed blast
+    then have \<epsilon>: "cmod (f z) \<le> \<bar>B\<bar> + 1" if "cmod z < \<epsilon>" "z \<noteq> 0" for z
+      using that by fastforce
+    have "0 < \<bar>B\<bar> + 1"
+      by simp
+    then show ?thesis
+      apply (rule that [OF \<open>0 < \<epsilon>\<close> \<open>\<epsilon> < 1\<close>])
+      using \<epsilon> by auto 
+  next
+    case 2
+    with infinite_enumerate obtain r :: "nat \<Rightarrow> nat" 
+      where "strict_mono r" and r: "\<And>n. r n \<in> {n. norm(h n w) \<ge> 1}"
+      by blast
+    obtain B where B: "\<And>j z. \<lbrakk>norm z = 1/2; j \<in> range (\<lambda>n. inverse \<circ> h (r n))\<rbrakk> \<Longrightarrow> norm(j z) \<le> B"
+    proof (rule GPicard3 [OF _ _ w, where K = "sphere 0 (1/2)"])  
+      show "range (\<lambda>n. inverse \<circ> h (r n)) \<subseteq> 
+            {g. g holomorphic_on ball 0 1 - {0} \<and> (\<forall>z\<in>ball 0 1 - {0}. g z \<noteq> 0 \<and> g z \<noteq> 1)}"
+        apply clarsimp
+        apply (intro conjI holomorphic_intros holomorphic_on_compose_gen [unfolded o_def, OF _ holh] holomorphic_on_compose)
+        using h01 apply auto
+        done
+      show "connected (ball 0 1 - {0::complex})"
+        by (simp add: connected_open_delete)
+      show "\<And>j. j \<in> range (\<lambda>n. inverse \<circ> h (r n)) \<Longrightarrow> cmod (j w) \<le> 1"
+        using r norm_inverse_le_norm by fastforce
+    qed (use r in auto)        
+    have norm_if_le_B: "cmod(inverse (f z)) \<le> B" if "norm z = 1 / (2 * (1 + of_nat (r n)))" for z n
+    proof -
+      have *: "inverse (cmod((f (z / (1 + of_nat (r n)))))) \<le> B" if "norm z = 1/2" for z
+        using B [OF that] by (force simp: norm_inverse h_def)
+      have half: "norm (z * (1 + of_nat (r n))) = 1/2"
+        by (simp add: norm_mult divide_simps that)
+      show ?thesis
+        using * [OF half] by (simp add: norm_inverse)
+    qed
+    have hol_if: "(inverse \<circ> f) holomorphic_on (ball 0 1 - {0})"
+      by (metis (no_types, lifting) holf comp_apply f01 holomorphic_on_inverse holomorphic_transform)
+    obtain \<epsilon> where "0 < \<epsilon>" "\<epsilon> < 1" and leB: "\<And>z. z \<in> ball 0 \<epsilon> - {0} \<Longrightarrow> cmod((inverse \<circ> f) z) \<le> B"
+    proof (rule GPicard4 [OF zero_less_one hol_if, of B])
+      fix e::real
+      assume "0 < e" "e < 1"
+      obtain n where "(1/e - 2) / 2 < real n"
+        using reals_Archimedean2 by blast
+      also have "... \<le> r n"
+        using \<open>strict_mono r\<close> by (simp add: seq_suble)
+      finally have "(1/e - 2) / 2 < real (r n)" .
+      with \<open>0 < e\<close> have e: "e > 1 / (2 + 2 * real (r n))"
+        by (simp add: field_simps)
+      show "\<exists>d>0. d < e \<and> (\<forall>z\<in>sphere 0 d. cmod ((inverse \<circ> f) z) \<le> B)"
+        apply (rule_tac x="1 / (2 * (1 + of_nat (r n)))" in exI)
+        using norm_if_le_B by (simp add: e)
+    qed blast
+    have \<epsilon>: "cmod (f z) \<ge> inverse B" and "B > 0" if "cmod z < \<epsilon>" "z \<noteq> 0" for z
+    proof -
+      have "inverse (cmod (f z)) \<le> B"
+        using leB that by (simp add: norm_inverse)
+      moreover
+      have "f z \<noteq> 0"
+        using \<open>\<epsilon> < 1\<close> f01 that by auto
+      ultimately show "cmod (f z) \<ge> inverse B"
+        by (simp add: norm_inverse inverse_le_imp_le)
+      show "B > 0"
+        using \<open>f z \<noteq> 0\<close> \<open>inverse (cmod (f z)) \<le> B\<close> not_le order.trans by fastforce
+    qed
+    then have "B > 0"
+      by (metis \<open>0 < \<epsilon>\<close> dense leI order.asym vector_choose_size)
+    then have "inverse B > 0"
+      by (simp add: field_split_simps)
+    then show ?thesis
+      apply (rule that [OF \<open>0 < \<epsilon>\<close> \<open>\<epsilon> < 1\<close>])
+      using \<epsilon> by auto 
+  qed
+qed
+
+  
+lemma GPicard6:
+  assumes "open M" "z \<in> M" "a \<noteq> 0" and holf: "f holomorphic_on (M - {z})"
+      and f0a: "\<And>w. w \<in> M - {z} \<Longrightarrow> f w \<noteq> 0 \<and> f w \<noteq> a"
+  obtains r where "0 < r" "ball z r \<subseteq> M" 
+                  "bounded(f ` (ball z r - {z})) \<or>
+                   bounded((inverse \<circ> f) ` (ball z r - {z}))"
+proof -
+  obtain r where "0 < r" and r: "ball z r \<subseteq> M"
+    using assms openE by blast 
+  let ?g = "\<lambda>w. f (z + of_real r * w) / a"
+  obtain e B where "0 < e" "e < 1" "0 < B" 
+    and B: "(\<forall>z \<in> ball 0 e - {0}. norm(?g z) \<le> B) \<or> (\<forall>z \<in> ball 0 e - {0}. norm(?g z) \<ge> B)"
+  proof (rule GPicard5)
+    show "?g holomorphic_on ball 0 1 - {0}"
+      apply (intro holomorphic_intros holomorphic_on_compose_gen [unfolded o_def, OF _ holf])
+      using \<open>0 < r\<close> \<open>a \<noteq> 0\<close> r
+      by (auto simp: dist_norm norm_mult subset_eq)
+    show "\<And>w. w \<in> ball 0 1 - {0} \<Longrightarrow> f (z + of_real r * w) / a \<noteq> 0 \<and> f (z + of_real r * w) / a \<noteq> 1"
+      apply (simp add: field_split_simps \<open>a \<noteq> 0\<close>)
+      apply (rule f0a)
+      using \<open>0 < r\<close> r by (auto simp: dist_norm norm_mult subset_eq)
+  qed
+  show ?thesis
+  proof
+    show "0 < e*r"
+      by (simp add: \<open>0 < e\<close> \<open>0 < r\<close>)
+    have "ball z (e * r) \<subseteq> ball z r"
+      by (simp add: \<open>0 < r\<close> \<open>e < 1\<close> order.strict_implies_order subset_ball)
+    then show "ball z (e * r) \<subseteq> M"
+      using r by blast
+    consider "\<And>z. z \<in> ball 0 e - {0} \<Longrightarrow> norm(?g z) \<le> B" | "\<And>z. z \<in> ball 0 e - {0} \<Longrightarrow> norm(?g z) \<ge> B"
+      using B by blast
+    then show "bounded (f ` (ball z (e * r) - {z})) \<or>
+          bounded ((inverse \<circ> f) ` (ball z (e * r) - {z}))"
+    proof cases
+      case 1
+      have "\<lbrakk>dist z w < e * r; w \<noteq> z\<rbrakk> \<Longrightarrow> cmod (f w) \<le> B * norm a" for w
+        using \<open>a \<noteq> 0\<close> \<open>0 < r\<close> 1 [of "(w - z) / r"]
+        by (simp add: norm_divide dist_norm field_split_simps)
+      then show ?thesis
+        by (force simp: intro!: boundedI)
+    next
+      case 2
+      have "\<lbrakk>dist z w < e * r; w \<noteq> z\<rbrakk> \<Longrightarrow> cmod (f w) \<ge> B * norm a" for w
+        using \<open>a \<noteq> 0\<close> \<open>0 < r\<close> 2 [of "(w - z) / r"]
+        by (simp add: norm_divide dist_norm field_split_simps)
+      then have "\<lbrakk>dist z w < e * r; w \<noteq> z\<rbrakk> \<Longrightarrow> inverse (cmod (f w)) \<le> inverse (B * norm a)" for w
+        by (metis \<open>0 < B\<close> \<open>a \<noteq> 0\<close> mult_pos_pos norm_inverse norm_inverse_le_norm zero_less_norm_iff)
+      then show ?thesis 
+        by (force simp: norm_inverse intro!: boundedI)
+    qed
+  qed
+qed
+  
+
+theorem great_Picard:
+  assumes "open M" "z \<in> M" "a \<noteq> b" and holf: "f holomorphic_on (M - {z})"
+      and fab: "\<And>w. w \<in> M - {z} \<Longrightarrow> f w \<noteq> a \<and> f w \<noteq> b"
+  obtains l where "(f \<longlongrightarrow> l) (at z) \<or> ((inverse \<circ> f) \<longlongrightarrow> l) (at z)"
+proof -
+  obtain r where "0 < r" and zrM: "ball z r \<subseteq> M" 
+             and r: "bounded((\<lambda>z. f z - a) ` (ball z r - {z})) \<or>
+                     bounded((inverse \<circ> (\<lambda>z. f z - a)) ` (ball z r - {z}))"
+  proof (rule GPicard6 [OF \<open>open M\<close> \<open>z \<in> M\<close>])
+    show "b - a \<noteq> 0"
+      using assms by auto
+    show "(\<lambda>z. f z - a) holomorphic_on M - {z}"
+      by (intro holomorphic_intros holf)
+  qed (use fab in auto)
+  have holfb: "f holomorphic_on ball z r - {z}"
+    apply (rule holomorphic_on_subset [OF holf])
+    using zrM by auto
+  have holfb_i: "(\<lambda>z. inverse(f z - a)) holomorphic_on ball z r - {z}"
+    apply (intro holomorphic_intros holfb)
+    using fab zrM by fastforce
+  show ?thesis
+    using r
+  proof              
+    assume "bounded ((\<lambda>z. f z - a) ` (ball z r - {z}))"
+    then obtain B where B: "\<And>w. w \<in> (\<lambda>z. f z - a) ` (ball z r - {z}) \<Longrightarrow> norm w \<le> B"
+      by (force simp: bounded_iff)
+    have "\<forall>\<^sub>F w in at z. cmod (f w - a) \<le> B"
+      apply (simp add: eventually_at)
+      apply (rule_tac x=r in exI)
+      using \<open>0 < r\<close> by (auto simp: dist_commute intro!: B)
+    then have "\<exists>B. \<forall>\<^sub>F w in at z. cmod (f w) \<le> B"
+      apply (rule_tac x="B + norm a" in exI)
+        apply (erule eventually_mono)
+      by (metis add.commute add_le_cancel_right norm_triangle_sub order.trans)
+    then obtain g where holg: "g holomorphic_on ball z r" and gf: "\<And>w. w \<in> ball z r - {z} \<Longrightarrow> g w = f w"
+      using \<open>0 < r\<close> holomorphic_on_extend_bounded [OF holfb] by auto
+    then have "g \<midarrow>z\<rightarrow> g z"
+      apply (simp add: continuous_at [symmetric])
+      using \<open>0 < r\<close> centre_in_ball field_differentiable_imp_continuous_at holomorphic_on_imp_differentiable_at by blast
+    then have "(f \<longlongrightarrow> g z) (at z)"
+      apply (rule Lim_transform_within_open [of g "g z" z UNIV "ball z r"])
+      using  \<open>0 < r\<close> by (auto simp: gf)
+    then show ?thesis
+      using that by blast
+  next
+    assume "bounded((inverse \<circ> (\<lambda>z. f z - a)) ` (ball z r - {z}))"
+    then obtain B where B: "\<And>w. w \<in> (inverse \<circ> (\<lambda>z. f z - a)) ` (ball z r - {z}) \<Longrightarrow> norm w \<le> B"
+      by (force simp: bounded_iff)
+    have "\<forall>\<^sub>F w in at z. cmod (inverse (f w - a)) \<le> B"
+      apply (simp add: eventually_at)
+      apply (rule_tac x=r in exI)
+      using \<open>0 < r\<close> by (auto simp: dist_commute intro!: B)
+    then have "\<exists>B. \<forall>\<^sub>F z in at z. cmod (inverse (f z - a)) \<le> B"
+      by blast
+    then obtain g where holg: "g holomorphic_on ball z r" and gf: "\<And>w. w \<in> ball z r - {z} \<Longrightarrow> g w = inverse (f w - a)"
+      using \<open>0 < r\<close> holomorphic_on_extend_bounded [OF holfb_i] by auto
+    then have gz: "g \<midarrow>z\<rightarrow> g z"
+      apply (simp add: continuous_at [symmetric])
+      using \<open>0 < r\<close> centre_in_ball field_differentiable_imp_continuous_at holomorphic_on_imp_differentiable_at by blast
+    have gnz: "\<And>w. w \<in> ball z r - {z} \<Longrightarrow> g w \<noteq> 0"
+      using gf fab zrM by fastforce
+    show ?thesis
+    proof (cases "g z = 0")
+      case True
+      have *: "\<lbrakk>g \<noteq> 0; inverse g = f - a\<rbrakk> \<Longrightarrow> g / (1 + a * g) = inverse f" for f g::complex
+        by (auto simp: field_simps)
+      have "(inverse \<circ> f) \<midarrow>z\<rightarrow> 0"
+      proof (rule Lim_transform_within_open [of "\<lambda>w. g w / (1 + a * g w)" _ _ UNIV "ball z r"])
+        show "(\<lambda>w. g w / (1 + a * g w)) \<midarrow>z\<rightarrow> 0"
+          using True by (auto simp: intro!: tendsto_eq_intros gz)
+        show "\<And>x. \<lbrakk>x \<in> ball z r; x \<noteq> z\<rbrakk> \<Longrightarrow> g x / (1 + a * g x) = (inverse \<circ> f) x"
+          using * gf gnz by simp
+      qed (use \<open>0 < r\<close> in auto)
+      with that show ?thesis by blast
+    next
+      case False
+      show ?thesis
+      proof (cases "1 + a * g z = 0")
+        case True
+        have "(f \<longlongrightarrow> 0) (at z)"
+        proof (rule Lim_transform_within_open [of "\<lambda>w. (1 + a * g w) / g w" _ _ _ "ball z r"])
+          show "(\<lambda>w. (1 + a * g w) / g w) \<midarrow>z\<rightarrow> 0"
+            apply (rule tendsto_eq_intros refl gz \<open>g z \<noteq> 0\<close>)+
+            by (simp add: True)
+          show "\<And>x. \<lbrakk>x \<in> ball z r; x \<noteq> z\<rbrakk> \<Longrightarrow> (1 + a * g x) / g x = f x"
+            using fab fab zrM by (fastforce simp add: gf field_split_simps)
+        qed (use \<open>0 < r\<close> in auto)
+        then show ?thesis
+          using that by blast 
+      next
+        case False
+        have *: "\<lbrakk>g \<noteq> 0; inverse g = f - a\<rbrakk> \<Longrightarrow> g / (1 + a * g) = inverse f" for f g::complex
+          by (auto simp: field_simps)
+        have "(inverse \<circ> f) \<midarrow>z\<rightarrow> g z / (1 + a * g z)"
+        proof (rule Lim_transform_within_open [of "\<lambda>w. g w / (1 + a * g w)" _ _ UNIV "ball z r"])
+          show "(\<lambda>w. g w / (1 + a * g w)) \<midarrow>z\<rightarrow> g z / (1 + a * g z)"
+            using False by (auto simp: False intro!: tendsto_eq_intros gz)
+          show "\<And>x. \<lbrakk>x \<in> ball z r; x \<noteq> z\<rbrakk> \<Longrightarrow> g x / (1 + a * g x) = (inverse \<circ> f) x"
+            using * gf gnz by simp
+        qed (use \<open>0 < r\<close> in auto)
+        with that show ?thesis by blast
+      qed
+    qed 
+  qed
+qed
+
+
+corollary great_Picard_alt:
+  assumes M: "open M" "z \<in> M" and holf: "f holomorphic_on (M - {z})"
+    and non: "\<And>l. \<not> (f \<longlongrightarrow> l) (at z)" "\<And>l. \<not> ((inverse \<circ> f) \<longlongrightarrow> l) (at z)"
+  obtains a where "- {a} \<subseteq> f ` (M - {z})"
+  apply (simp add: subset_iff image_iff)
+  by (metis great_Picard [OF M _ holf] non)
+    
+
+corollary great_Picard_infinite:
+  assumes M: "open M" "z \<in> M" and holf: "f holomorphic_on (M - {z})"
+    and non: "\<And>l. \<not> (f \<longlongrightarrow> l) (at z)" "\<And>l. \<not> ((inverse \<circ> f) \<longlongrightarrow> l) (at z)"
+  obtains a where "\<And>w. w \<noteq> a \<Longrightarrow> infinite {x. x \<in> M - {z} \<and> f x = w}"
+proof -
+  have False if "a \<noteq> b" and ab: "finite {x. x \<in> M - {z} \<and> f x = a}" "finite {x. x \<in> M - {z} \<and> f x = b}" for a b
+  proof -
+    have finab: "finite {x. x \<in> M - {z} \<and> f x \<in> {a,b}}"
+      using finite_UnI [OF ab]  unfolding mem_Collect_eq insert_iff empty_iff
+      by (simp add: conj_disj_distribL)
+    obtain r where "0 < r" and zrM: "ball z r \<subseteq> M" and r: "\<And>x. \<lbrakk>x \<in> M - {z}; f x \<in> {a,b}\<rbrakk> \<Longrightarrow> x \<notin> ball z r"
+    proof -
+      obtain e where "e > 0" and e: "ball z e \<subseteq> M"
+        using assms openE by blast
+      show ?thesis
+      proof (cases "{x \<in> M - {z}. f x \<in> {a, b}} = {}")
+        case True
+        then show ?thesis
+          apply (rule_tac r=e in that)
+          using e \<open>e > 0\<close> by auto
+      next
+        case False
+        let ?r = "min e (Min (dist z ` {x \<in> M - {z}. f x \<in> {a,b}}))"
+        show ?thesis
+        proof
+          show "0 < ?r"
+            using min_less_iff_conj Min_gr_iff finab False \<open>0 < e\<close> by auto
+          have "ball z ?r \<subseteq> ball z e"
+            by (simp add: subset_ball)
+          with e show "ball z ?r \<subseteq> M" by blast
+          show "\<And>x. \<lbrakk>x \<in> M - {z}; f x \<in> {a, b}\<rbrakk> \<Longrightarrow> x \<notin> ball z ?r"
+            using min_less_iff_conj Min_gr_iff finab False \<open>0 < e\<close> by auto
+        qed
+      qed
+    qed
+    have holfb: "f holomorphic_on (ball z r - {z})"
+      apply (rule holomorphic_on_subset [OF holf])
+       using zrM by auto
+     show ?thesis
+       apply (rule great_Picard [OF open_ball _ \<open>a \<noteq> b\<close> holfb])
+      using non \<open>0 < r\<close> r zrM by auto
+  qed
+  with that show thesis
+    by meson
+qed
+
+theorem Casorati_Weierstrass:
+  assumes "open M" "z \<in> M" "f holomorphic_on (M - {z})"
+      and "\<And>l. \<not> (f \<longlongrightarrow> l) (at z)" "\<And>l. \<not> ((inverse \<circ> f) \<longlongrightarrow> l) (at z)"
+  shows "closure(f ` (M - {z})) = UNIV"
+proof -
+  obtain a where a: "- {a} \<subseteq> f ` (M - {z})"
+    using great_Picard_alt [OF assms] .
+  have "UNIV = closure(- {a})"
+    by (simp add: closure_interior)
+  also have "... \<subseteq> closure(f ` (M - {z}))"
+    by (simp add: a closure_mono)
+  finally show ?thesis
+    by blast 
+qed
+  
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Complex_Analysis/Riemann_Mapping.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -0,0 +1,1489 @@
+(*  Title:      HOL/Analysis/Riemann_Mapping.thy
+    Authors:    LC Paulson, based on material from HOL Light
+*)
+
+section \<open>Moebius functions, Equivalents of Simply Connected Sets, Riemann Mapping Theorem\<close>
+
+theory Riemann_Mapping
+imports Great_Picard
+begin
+
+subsection\<open>Moebius functions are biholomorphisms of the unit disc\<close>
+
+definition\<^marker>\<open>tag important\<close> Moebius_function :: "[real,complex,complex] \<Rightarrow> complex" where
+  "Moebius_function \<equiv> \<lambda>t w z. exp(\<i> * of_real t) * (z - w) / (1 - cnj w * z)"
+
+lemma Moebius_function_simple:
+   "Moebius_function 0 w z = (z - w) / (1 - cnj w * z)"
+  by (simp add: Moebius_function_def)
+
+lemma Moebius_function_eq_zero:
+   "Moebius_function t w w = 0"
+  by (simp add: Moebius_function_def)
+
+lemma Moebius_function_of_zero:
+   "Moebius_function t w 0 = - exp(\<i> * of_real t) * w"
+  by (simp add: Moebius_function_def)
+
+lemma Moebius_function_norm_lt_1:
+  assumes w1: "norm w < 1" and z1: "norm z < 1"
+  shows "norm (Moebius_function t w z) < 1"
+proof -
+  have "1 - cnj w * z \<noteq> 0"
+    by (metis complex_cnj_cnj complex_mod_sqrt_Re_mult_cnj mult.commute mult_less_cancel_right1 norm_ge_zero norm_mult norm_one order.asym right_minus_eq w1 z1)
+  then have VV: "1 - w * cnj z \<noteq> 0"
+    by (metis complex_cnj_cnj complex_cnj_mult complex_cnj_one right_minus_eq)
+  then have "1 - norm (Moebius_function t w z) ^ 2 =
+         ((1 - norm w ^ 2) / (norm (1 - cnj w * z) ^ 2)) * (1 - norm z ^ 2)"
+    apply (cases w)
+    apply (cases z)
+    apply (simp add: Moebius_function_def divide_simps norm_divide norm_mult)
+    apply (simp add: complex_norm complex_diff complex_mult one_complex.code complex_cnj)
+    apply (auto simp: algebra_simps power2_eq_square)
+    done
+  then have "1 - (cmod (Moebius_function t w z))\<^sup>2 = (1 - cmod (w * w)) / (cmod (1 - cnj w * z))\<^sup>2 * (1 - cmod (z * z))"
+    by (simp add: norm_mult power2_eq_square)
+  moreover have "0 < 1 - cmod (z * z)"
+    by (metis (no_types) z1 diff_gt_0_iff_gt mult.left_neutral norm_mult_less)
+  ultimately have "0 < 1 - norm (Moebius_function t w z) ^ 2"
+    using \<open>1 - cnj w * z \<noteq> 0\<close> w1 norm_mult_less by fastforce
+  then show ?thesis
+    using linorder_not_less by fastforce
+qed
+
+lemma Moebius_function_holomorphic:
+  assumes "norm w < 1"
+  shows "Moebius_function t w holomorphic_on ball 0 1"
+proof -
+  have *: "1 - z * w \<noteq> 0" if "norm z < 1" for z
+  proof -
+    have "norm (1::complex) \<noteq> norm (z * w)"
+      using assms that norm_mult_less by fastforce
+    then show ?thesis by auto
+  qed
+  show ?thesis
+  apply (simp add: Moebius_function_def)
+  apply (intro holomorphic_intros)
+  using assms *
+  by (metis complex_cnj_cnj complex_cnj_mult complex_cnj_one complex_mod_cnj mem_ball_0 mult.commute right_minus_eq)
+qed
+
+lemma Moebius_function_compose:
+  assumes meq: "-w1 = w2" and "norm w1 < 1" "norm z < 1"
+  shows "Moebius_function 0 w1 (Moebius_function 0 w2 z) = z"
+proof -
+  have "norm w2 < 1"
+    using assms by auto
+  then have "-w1 = z" if "cnj w2 * z = 1"
+    by (metis assms(3) complex_mod_cnj less_irrefl mult.right_neutral norm_mult_less norm_one that)
+  moreover have "z=0" if "1 - cnj w2 * z = cnj w1 * (z - w2)"
+  proof -
+    have "w2 * cnj w2 = 1"
+      using that meq by (auto simp: algebra_simps)
+    then show "z = 0"
+      by (metis (no_types) \<open>cmod w2 < 1\<close> complex_mod_cnj less_irrefl mult.right_neutral norm_mult_less norm_one)
+  qed
+  moreover have "z - w2 - w1 * (1 - cnj w2 * z) = z * (1 - cnj w2 * z - cnj w1 * (z - w2))"
+    using meq by (fastforce simp: algebra_simps)
+  ultimately
+  show ?thesis
+    by (simp add: Moebius_function_def divide_simps norm_divide norm_mult)
+qed
+
+lemma ball_biholomorphism_exists:
+  assumes "a \<in> ball 0 1"
+  obtains f g where "f a = 0"
+                "f holomorphic_on ball 0 1" "f ` ball 0 1 \<subseteq> ball 0 1"
+                "g holomorphic_on ball 0 1" "g ` ball 0 1 \<subseteq> ball 0 1"
+                "\<And>z. z \<in> ball 0 1 \<Longrightarrow> f (g z) = z"
+                "\<And>z. z \<in> ball 0 1 \<Longrightarrow> g (f z) = z"
+proof
+  show "Moebius_function 0 a holomorphic_on ball 0 1"  "Moebius_function 0 (-a) holomorphic_on ball 0 1"
+    using Moebius_function_holomorphic assms mem_ball_0 by auto
+  show "Moebius_function 0 a a = 0"
+    by (simp add: Moebius_function_eq_zero)
+  show "Moebius_function 0 a ` ball 0 1 \<subseteq> ball 0 1"
+       "Moebius_function 0 (- a) ` ball 0 1 \<subseteq> ball 0 1"
+    using Moebius_function_norm_lt_1 assms by auto
+  show "Moebius_function 0 a (Moebius_function 0 (- a) z) = z"
+       "Moebius_function 0 (- a) (Moebius_function 0 a z) = z" if "z \<in> ball 0 1" for z
+    using Moebius_function_compose assms that by auto
+qed
+
+
+subsection\<open>A big chain of equivalents of simple connectedness for an open set\<close>
+
+lemma biholomorphic_to_disc_aux:
+  assumes "open S" "connected S" "0 \<in> S" and S01: "S \<subseteq> ball 0 1"
+      and prev: "\<And>f. \<lbrakk>f holomorphic_on S; \<And>z. z \<in> S \<Longrightarrow> f z \<noteq> 0; inj_on f S\<rbrakk>
+               \<Longrightarrow> \<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S. f z = (g z)\<^sup>2)"
+  shows "\<exists>f g. f holomorphic_on S \<and> g holomorphic_on ball 0 1 \<and>
+               (\<forall>z \<in> S. f z \<in> ball 0 1 \<and> g(f z) = z) \<and>
+               (\<forall>z \<in> ball 0 1. g z \<in> S \<and> f(g z) = z)"
+proof -
+  define F where "F \<equiv> {h. h holomorphic_on S \<and> h ` S \<subseteq> ball 0 1 \<and> h 0 = 0 \<and> inj_on h S}"
+  have idF: "id \<in> F"
+    using S01 by (auto simp: F_def)
+  then have "F \<noteq> {}"
+    by blast
+  have imF_ne: "((\<lambda>h. norm(deriv h 0)) ` F) \<noteq> {}"
+    using idF by auto
+  have holF: "\<And>h. h \<in> F \<Longrightarrow> h holomorphic_on S"
+    by (auto simp: F_def)
+  obtain f where "f \<in> F" and normf: "\<And>h. h \<in> F \<Longrightarrow> norm(deriv h 0) \<le> norm(deriv f 0)"
+  proof -
+    obtain r where "r > 0" and r: "ball 0 r \<subseteq> S"
+      using \<open>open S\<close> \<open>0 \<in> S\<close> openE by auto
+    have bdd: "bdd_above ((\<lambda>h. norm(deriv h 0)) ` F)"
+    proof (intro bdd_aboveI exI ballI, clarify)
+      show "norm (deriv f 0) \<le> 1 / r" if "f \<in> F" for f
+      proof -
+        have r01: "(*) (complex_of_real r) ` ball 0 1 \<subseteq> S"
+          using that \<open>r > 0\<close> by (auto simp: norm_mult r [THEN subsetD])
+        then have "f holomorphic_on (*) (complex_of_real r) ` ball 0 1"
+          using holomorphic_on_subset [OF holF] by (simp add: that)
+        then have holf: "f \<circ> (\<lambda>z. (r * z)) holomorphic_on (ball 0 1)"
+          by (intro holomorphic_intros holomorphic_on_compose)
+        have f0: "(f \<circ> (*) (complex_of_real r)) 0 = 0"
+          using F_def that by auto
+        have "f ` S \<subseteq> ball 0 1"
+          using F_def that by blast
+        with r01 have fr1: "\<And>z. norm z < 1 \<Longrightarrow> norm ((f \<circ> (*)(of_real r))z) < 1"
+          by force
+        have *: "((\<lambda>w. f (r * w)) has_field_derivative deriv f (r * z) * r) (at z)"
+          if "z \<in> ball 0 1" for z::complex
+        proof (rule DERIV_chain' [where g=f])
+          show "(f has_field_derivative deriv f (of_real r * z)) (at (of_real r * z))"
+            apply (rule holomorphic_derivI [OF holF \<open>open S\<close>])
+             apply (rule \<open>f \<in> F\<close>)
+            by (meson imageI r01 subset_iff that)
+        qed simp
+        have df0: "((\<lambda>w. f (r * w)) has_field_derivative deriv f 0 * r) (at 0)"
+          using * [of 0] by simp
+        have deq: "deriv (\<lambda>x. f (complex_of_real r * x)) 0 = deriv f 0 * complex_of_real r"
+          using DERIV_imp_deriv df0 by blast
+        have "norm (deriv (f \<circ> (*) (complex_of_real r)) 0) \<le> 1"
+          by (auto intro: Schwarz_Lemma [OF holf f0 fr1, of 0])
+        with \<open>r > 0\<close> show ?thesis
+          by (simp add: deq norm_mult divide_simps o_def)
+      qed
+    qed
+    define l where "l \<equiv> SUP h\<in>F. norm (deriv h 0)"
+    have eql: "norm (deriv f 0) = l" if le: "l \<le> norm (deriv f 0)" and "f \<in> F" for f
+      apply (rule order_antisym [OF _ le])
+      using \<open>f \<in> F\<close> bdd cSUP_upper by (fastforce simp: l_def)
+    obtain \<F> where \<F>in: "\<And>n. \<F> n \<in> F" and \<F>lim: "(\<lambda>n. norm (deriv (\<F> n) 0)) \<longlonglongrightarrow> l"
+    proof -
+      have "\<exists>f. f \<in> F \<and> \<bar>norm (deriv f 0) - l\<bar> < 1 / (Suc n)" for n
+      proof -
+        obtain f where "f \<in> F" and f: "l < norm (deriv f 0) + 1/(Suc n)"
+          using cSup_least [OF imF_ne, of "l - 1/(Suc n)"] by (fastforce simp add: l_def)
+        then have "\<bar>norm (deriv f 0) - l\<bar> < 1 / (Suc n)"
+          by (fastforce simp add: abs_if not_less eql)
+        with \<open>f \<in> F\<close> show ?thesis
+          by blast
+      qed
+      then obtain \<F> where fF: "\<And>n. (\<F> n) \<in> F"
+        and fless:  "\<And>n. \<bar>norm (deriv (\<F> n) 0) - l\<bar> < 1 / (Suc n)"
+        by metis
+      have "(\<lambda>n. norm (deriv (\<F> n) 0)) \<longlonglongrightarrow> l"
+      proof (rule metric_LIMSEQ_I)
+        fix e::real
+        assume "e > 0"
+        then obtain N::nat where N: "e > 1/(Suc N)"
+          using nat_approx_posE by blast
+        show "\<exists>N. \<forall>n\<ge>N. dist (norm (deriv (\<F> n) 0)) l < e"
+        proof (intro exI allI impI)
+          fix n assume "N \<le> n"
+          have "dist (norm (deriv (\<F> n) 0)) l < 1 / (Suc n)"
+            using fless by (simp add: dist_norm)
+          also have "... < e"
+            using N \<open>N \<le> n\<close> inverse_of_nat_le le_less_trans by blast
+          finally show "dist (norm (deriv (\<F> n) 0)) l < e" .
+        qed
+      qed
+      with fF show ?thesis
+        using that by blast
+    qed
+    have "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> \<exists>B. \<forall>h\<in>F. \<forall>z\<in>K. norm (h z) \<le> B"
+      by (rule_tac x=1 in exI) (force simp: F_def)
+    moreover have "range \<F> \<subseteq> F"
+      using \<open>\<And>n. \<F> n \<in> F\<close> by blast
+    ultimately obtain f and r :: "nat \<Rightarrow> nat"
+      where holf: "f holomorphic_on S" and r: "strict_mono r"
+        and limf: "\<And>x. x \<in> S \<Longrightarrow> (\<lambda>n. \<F> (r n) x) \<longlonglongrightarrow> f x"
+        and ulimf: "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> uniform_limit K (\<F> \<circ> r) f sequentially"
+      using Montel [of S F \<F>, OF \<open>open S\<close> holF] by auto+
+    have der: "\<And>n x. x \<in> S \<Longrightarrow> ((\<F> \<circ> r) n has_field_derivative ((\<lambda>n. deriv (\<F> n)) \<circ> r) n x) (at x)"
+      using \<open>\<And>n. \<F> n \<in> F\<close> \<open>open S\<close> holF holomorphic_derivI by fastforce
+    have ulim: "\<And>x. x \<in> S \<Longrightarrow> \<exists>d>0. cball x d \<subseteq> S \<and> uniform_limit (cball x d) (\<F> \<circ> r) f sequentially"
+      by (meson ulimf \<open>open S\<close> compact_cball open_contains_cball)
+    obtain f' :: "complex\<Rightarrow>complex" where f': "(f has_field_derivative f' 0) (at 0)"
+      and tof'0: "(\<lambda>n. ((\<lambda>n. deriv (\<F> n)) \<circ> r) n 0) \<longlonglongrightarrow> f' 0"
+      using has_complex_derivative_uniform_sequence [OF \<open>open S\<close> der ulim] \<open>0 \<in> S\<close> by metis
+    then have derf0: "deriv f 0 = f' 0"
+      by (simp add: DERIV_imp_deriv)
+    have "f field_differentiable (at 0)"
+      using field_differentiable_def f' by blast
+    have "(\<lambda>x.  (norm (deriv (\<F> (r x)) 0))) \<longlonglongrightarrow> norm (deriv f 0)"
+      using isCont_tendsto_compose [OF continuous_norm [OF continuous_ident] tof'0] derf0 by auto
+    with LIMSEQ_subseq_LIMSEQ [OF \<F>lim r] have no_df0: "norm(deriv f 0) = l"
+      by (force simp: o_def intro: tendsto_unique)
+    have nonconstf: "\<not> f constant_on S"
+    proof -
+      have False if "\<And>x. x \<in> S \<Longrightarrow> f x = c" for c
+      proof -
+        have "deriv f 0 = 0"
+          by (metis that \<open>open S\<close> \<open>0 \<in> S\<close> DERIV_imp_deriv [OF has_field_derivative_transform_within_open [OF DERIV_const]])
+        with no_df0 have "l = 0"
+          by auto
+        with eql [OF _ idF] show False by auto
+      qed
+      then show ?thesis
+        by (meson constant_on_def)
+    qed
+    show ?thesis
+    proof
+      show "f \<in> F"
+        unfolding F_def
+      proof (intro CollectI conjI holf)
+        have "norm(f z) \<le> 1" if "z \<in> S" for z
+        proof (intro Lim_norm_ubound [OF _ limf] always_eventually allI that)
+          fix n
+          have "\<F> (r n) \<in> F"
+            by (simp add: \<F>in)
+          then show "norm (\<F> (r n) z) \<le> 1"
+            using that by (auto simp: F_def)
+        qed simp
+        then have fless1: "norm(f z) < 1" if "z \<in> S" for z
+          using maximum_modulus_principle [OF holf \<open>open S\<close> \<open>connected S\<close> \<open>open S\<close>] nonconstf that
+          by fastforce
+        then show "f ` S \<subseteq> ball 0 1"
+          by auto
+        have "(\<lambda>n. \<F> (r n) 0) \<longlonglongrightarrow> 0"
+          using \<F>in by (auto simp: F_def)
+        then show "f 0 = 0"
+          using tendsto_unique [OF _ limf ] \<open>0 \<in> S\<close> trivial_limit_sequentially by blast
+        show "inj_on f S"
+        proof (rule Hurwitz_injective [OF \<open>open S\<close> \<open>connected S\<close> _ holf])
+          show "\<And>n. (\<F> \<circ> r) n holomorphic_on S"
+            by (simp add: \<F>in holF)
+          show "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> uniform_limit K(\<F> \<circ> r) f sequentially"
+            by (metis ulimf)
+          show "\<not> f constant_on S"
+            using nonconstf by auto
+          show "\<And>n. inj_on ((\<F> \<circ> r) n) S"
+            using \<F>in by (auto simp: F_def)
+        qed
+      qed
+      show "\<And>h. h \<in> F \<Longrightarrow> norm (deriv h 0) \<le> norm (deriv f 0)"
+        by (metis eql le_cases no_df0)
+    qed
+  qed
+  have holf: "f holomorphic_on S" and injf: "inj_on f S" and f01: "f ` S \<subseteq> ball 0 1"
+    using \<open>f \<in> F\<close> by (auto simp: F_def)
+  obtain g where holg: "g holomorphic_on (f ` S)"
+             and derg: "\<And>z. z \<in> S \<Longrightarrow> deriv f z * deriv g (f z) = 1"
+             and gf: "\<And>z. z \<in> S \<Longrightarrow> g(f z) = z"
+    using holomorphic_has_inverse [OF holf \<open>open S\<close> injf] by metis
+  have "ball 0 1 \<subseteq> f ` S"
+  proof
+    fix a::complex
+    assume a: "a \<in> ball 0 1"
+    have False if "\<And>x. x \<in> S \<Longrightarrow> f x \<noteq> a"
+    proof -
+      obtain h k where "h a = 0"
+        and holh: "h holomorphic_on ball 0 1" and h01: "h ` ball 0 1 \<subseteq> ball 0 1"
+        and holk: "k holomorphic_on ball 0 1" and k01: "k ` ball 0 1 \<subseteq> ball 0 1"
+        and hk: "\<And>z. z \<in> ball 0 1 \<Longrightarrow> h (k z) = z"
+        and kh: "\<And>z. z \<in> ball 0 1 \<Longrightarrow> k (h z) = z"
+        using ball_biholomorphism_exists [OF a] by blast
+      have nf1: "\<And>z. z \<in> S \<Longrightarrow> norm(f z) < 1"
+        using \<open>f \<in> F\<close> by (auto simp: F_def)
+      have 1: "h \<circ> f holomorphic_on S"
+        using F_def \<open>f \<in> F\<close> holh holomorphic_on_compose holomorphic_on_subset by blast
+      have 2: "\<And>z. z \<in> S \<Longrightarrow> (h \<circ> f) z \<noteq> 0"
+        by (metis \<open>h a = 0\<close> a comp_eq_dest_lhs nf1 kh mem_ball_0 that)
+      have 3: "inj_on (h \<circ> f) S"
+        by (metis (no_types, lifting) F_def \<open>f \<in> F\<close> comp_inj_on inj_on_inverseI injf kh mem_Collect_eq subset_inj_on)
+      obtain \<psi> where hol\<psi>: "\<psi> holomorphic_on ((h \<circ> f) ` S)"
+        and \<psi>2: "\<And>z. z \<in> S  \<Longrightarrow> \<psi>(h (f z)) ^ 2 = h(f z)"
+      proof (rule exE [OF prev [OF 1 2 3]], safe)
+        fix \<theta>
+        assume hol\<theta>: "\<theta> holomorphic_on S" and \<theta>2: "(\<forall>z\<in>S. (h \<circ> f) z = (\<theta> z)\<^sup>2)"
+        show thesis
+        proof
+          show "(\<theta> \<circ> g \<circ> k) holomorphic_on (h \<circ> f) ` S"
+          proof (intro holomorphic_on_compose)
+            show "k holomorphic_on (h \<circ> f) ` S"
+              apply (rule holomorphic_on_subset [OF holk])
+              using f01 h01 by force
+            show "g holomorphic_on k ` (h \<circ> f) ` S"
+              apply (rule holomorphic_on_subset [OF holg])
+              by (auto simp: kh nf1)
+            show "\<theta> holomorphic_on g ` k ` (h \<circ> f) ` S"
+              apply (rule holomorphic_on_subset [OF hol\<theta>])
+              by (auto simp: gf kh nf1)
+          qed
+          show "((\<theta> \<circ> g \<circ> k) (h (f z)))\<^sup>2 = h (f z)" if "z \<in> S" for z
+          proof -
+            have "f z \<in> ball 0 1"
+              by (simp add: nf1 that)
+            then have "(\<theta> (g (k (h (f z)))))\<^sup>2 = (\<theta> (g (f z)))\<^sup>2"
+              by (metis kh)
+            also have "... = h (f z)"
+              using \<theta>2 gf that by auto
+            finally show ?thesis
+              by (simp add: o_def)
+          qed
+        qed
+      qed
+      have norm\<psi>1: "norm(\<psi> (h (f z))) < 1" if "z \<in> S" for z
+      proof -
+        have "norm (\<psi> (h (f z)) ^ 2) < 1"
+          by (metis (no_types) that DIM_complex \<psi>2 h01 image_subset_iff mem_ball_0 nf1)
+        then show ?thesis
+          by (metis le_less_trans mult_less_cancel_left2 norm_ge_zero norm_power not_le power2_eq_square)
+      qed
+      then have \<psi>01: "\<psi> (h (f 0)) \<in> ball 0 1"
+        by (simp add: \<open>0 \<in> S\<close>)
+      obtain p q where p0: "p (\<psi> (h (f 0))) = 0"
+        and holp: "p holomorphic_on ball 0 1" and p01: "p ` ball 0 1 \<subseteq> ball 0 1"
+        and holq: "q holomorphic_on ball 0 1" and q01: "q ` ball 0 1 \<subseteq> ball 0 1"
+        and pq: "\<And>z. z \<in> ball 0 1 \<Longrightarrow> p (q z) = z"
+        and qp: "\<And>z. z \<in> ball 0 1 \<Longrightarrow> q (p z) = z"
+        using ball_biholomorphism_exists [OF \<psi>01] by metis
+      have "p \<circ> \<psi> \<circ> h \<circ> f \<in> F"
+        unfolding F_def
+      proof (intro CollectI conjI holf)
+        show "p \<circ> \<psi> \<circ> h \<circ> f holomorphic_on S"
+        proof (intro holomorphic_on_compose holf)
+          show "h holomorphic_on f ` S"
+            apply (rule holomorphic_on_subset [OF holh])
+            using f01 by force
+          show "\<psi> holomorphic_on h ` f ` S"
+            apply (rule holomorphic_on_subset [OF hol\<psi>])
+            by auto
+          show "p holomorphic_on \<psi> ` h ` f ` S"
+            apply (rule holomorphic_on_subset [OF holp])
+            by (auto simp: norm\<psi>1)
+        qed
+        show "(p \<circ> \<psi> \<circ> h \<circ> f) ` S \<subseteq> ball 0 1"
+          apply clarsimp
+          by (meson norm\<psi>1 p01 image_subset_iff mem_ball_0)
+        show "(p \<circ> \<psi> \<circ> h \<circ> f) 0 = 0"
+          by (simp add: \<open>p (\<psi> (h (f 0))) = 0\<close>)
+        show "inj_on (p \<circ> \<psi> \<circ> h \<circ> f) S"
+          unfolding inj_on_def o_def
+          by (metis \<psi>2 dist_0_norm gf kh mem_ball nf1 norm\<psi>1 qp)
+      qed
+      then have le_norm_df0: "norm (deriv (p \<circ> \<psi> \<circ> h \<circ> f) 0) \<le> norm (deriv f 0)"
+        by (rule normf)
+      have 1: "k \<circ> power2 \<circ> q holomorphic_on ball 0 1"
+      proof (intro holomorphic_on_compose holq)
+        show "power2 holomorphic_on q ` ball 0 1"
+          using holomorphic_on_subset holomorphic_on_power
+          by (blast intro: holomorphic_on_ident)
+        show "k holomorphic_on power2 ` q ` ball 0 1"
+          apply (rule holomorphic_on_subset [OF holk])
+          using q01 by (auto simp: norm_power abs_square_less_1)
+      qed
+      have 2: "(k \<circ> power2 \<circ> q) 0 = 0"
+        using p0 F_def \<open>f \<in> F\<close> \<psi>01 \<psi>2 \<open>0 \<in> S\<close> kh qp by force
+      have 3: "norm ((k \<circ> power2 \<circ> q) z) < 1" if "norm z < 1" for z
+      proof -
+        have "norm ((power2 \<circ> q) z) < 1"
+          using that q01 by (force simp: norm_power abs_square_less_1)
+        with k01 show ?thesis
+          by fastforce
+      qed
+      have False if c: "\<forall>z. norm z < 1 \<longrightarrow> (k \<circ> power2 \<circ> q) z = c * z" and "norm c = 1" for c
+      proof -
+        have "c \<noteq> 0" using that by auto
+        have "norm (p(1/2)) < 1" "norm (p(-1/2)) < 1"
+          using p01 by force+
+        then have "(k \<circ> power2 \<circ> q) (p(1/2)) = c * p(1/2)" "(k \<circ> power2 \<circ> q) (p(-1/2)) = c * p(-1/2)"
+          using c by force+
+        then have "p (1/2) = p (- (1/2))"
+          by (auto simp: \<open>c \<noteq> 0\<close> qp o_def)
+        then have "q (p (1/2)) = q (p (- (1/2)))"
+          by simp
+        then have "1/2 = - (1/2::complex)"
+          by (auto simp: qp)
+        then show False
+          by simp
+      qed
+      moreover
+      have False if "norm (deriv (k \<circ> power2 \<circ> q) 0) \<noteq> 1" "norm (deriv (k \<circ> power2 \<circ> q) 0) \<le> 1"
+        and le: "\<And>\<xi>. norm \<xi> < 1 \<Longrightarrow> norm ((k \<circ> power2 \<circ> q) \<xi>) \<le> norm \<xi>"
+      proof -
+        have "norm (deriv (k \<circ> power2 \<circ> q) 0) < 1"
+          using that by simp
+        moreover have eq: "deriv f 0 = deriv (k \<circ> (\<lambda>z. z ^ 2) \<circ> q) 0 * deriv (p \<circ> \<psi> \<circ> h \<circ> f) 0"
+        proof (intro DERIV_imp_deriv has_field_derivative_transform_within_open [OF DERIV_chain])
+          show "(k \<circ> power2 \<circ> q has_field_derivative deriv (k \<circ> power2 \<circ> q) 0) (at ((p \<circ> \<psi> \<circ> h \<circ> f) 0))"
+            using "1" holomorphic_derivI p0 by auto
+          show "(p \<circ> \<psi> \<circ> h \<circ> f has_field_derivative deriv (p \<circ> \<psi> \<circ> h \<circ> f) 0) (at 0)"
+            using \<open>p \<circ> \<psi> \<circ> h \<circ> f \<in> F\<close> \<open>open S\<close> \<open>0 \<in> S\<close> holF holomorphic_derivI by blast
+          show "\<And>x. x \<in> S \<Longrightarrow> (k \<circ> power2 \<circ> q \<circ> (p \<circ> \<psi> \<circ> h \<circ> f)) x = f x"
+            using \<psi>2 f01 kh norm\<psi>1 qp by auto
+        qed (use assms in simp_all)
+        ultimately have "cmod (deriv (p \<circ> \<psi> \<circ> h \<circ> f) 0) \<le> 0"
+          using le_norm_df0
+          by (metis linorder_not_le mult.commute mult_less_cancel_left2 norm_mult)
+        moreover have "1 \<le> norm (deriv f 0)"
+          using normf [of id] by (simp add: idF)
+        ultimately show False
+          by (simp add: eq)
+      qed
+      ultimately show ?thesis
+        using Schwarz_Lemma [OF 1 2 3] norm_one by blast
+    qed
+    then show "a \<in> f ` S"
+      by blast
+  qed
+  then have "f ` S = ball 0 1"
+    using F_def \<open>f \<in> F\<close> by blast
+  then show ?thesis
+    apply (rule_tac x=f in exI)
+    apply (rule_tac x=g in exI)
+    using holf holg derg gf by safe force+
+qed
+
+
+locale SC_Chain =
+  fixes S :: "complex set"
+  assumes openS: "open S"
+begin
+
+lemma winding_number_zero:
+  assumes "simply_connected S"
+  shows "connected S \<and>
+         (\<forall>\<gamma> z. path \<gamma> \<and> path_image \<gamma> \<subseteq> S \<and>
+                   pathfinish \<gamma> = pathstart \<gamma> \<and> z \<notin> S \<longrightarrow> winding_number \<gamma> z = 0)"
+  using assms
+  by (auto simp: simply_connected_imp_connected simply_connected_imp_winding_number_zero)
+
+lemma contour_integral_zero:
+  assumes "valid_path g" "path_image g \<subseteq> S" "pathfinish g = pathstart g" "f holomorphic_on S"
+         "\<And>\<gamma> z. \<lbrakk>path \<gamma>; path_image \<gamma> \<subseteq> S; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> S\<rbrakk> \<Longrightarrow> winding_number \<gamma> z = 0"
+  shows "(f has_contour_integral 0) g"
+  using assms by (meson Cauchy_theorem_global openS valid_path_imp_path)
+
+lemma global_primitive:
+  assumes "connected S" and holf: "f holomorphic_on S"
+  and prev: "\<And>\<gamma> f. \<lbrakk>valid_path \<gamma>; path_image \<gamma> \<subseteq> S; pathfinish \<gamma> = pathstart \<gamma>; f holomorphic_on S\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) \<gamma>"
+  shows "\<exists>h. \<forall>z \<in> S. (h has_field_derivative f z) (at z)"
+proof (cases "S = {}")
+case True then show ?thesis
+    by simp
+next
+  case False
+  then obtain a where "a \<in> S"
+    by blast
+  show ?thesis
+  proof (intro exI ballI)
+    fix x assume "x \<in> S"
+    then obtain d where "d > 0" and d: "cball x d \<subseteq> S"
+      using openS open_contains_cball_eq by blast
+    let ?g = "\<lambda>z. (SOME g. polynomial_function g \<and> path_image g \<subseteq> S \<and> pathstart g = a \<and> pathfinish g = z)"
+    show "((\<lambda>z. contour_integral (?g z) f) has_field_derivative f x)
+          (at x)"
+    proof (simp add: has_field_derivative_def has_derivative_at2 bounded_linear_mult_right, rule Lim_transform)
+      show "(\<lambda>y. inverse(norm(y - x)) *\<^sub>R (contour_integral(linepath x y) f - f x * (y - x))) \<midarrow>x\<rightarrow> 0"
+      proof (clarsimp simp add: Lim_at)
+        fix e::real assume "e > 0"
+        moreover have "continuous (at x) f"
+          using openS \<open>x \<in> S\<close> holf continuous_on_eq_continuous_at holomorphic_on_imp_continuous_on by auto
+        ultimately obtain d1 where "d1 > 0"
+             and d1: "\<And>x'. dist x' x < d1 \<Longrightarrow> dist (f x') (f x) < e/2"
+          unfolding continuous_at_eps_delta
+          by (metis less_divide_eq_numeral1(1) mult_zero_left)
+        obtain d2 where "d2 > 0" and d2: "ball x d2 \<subseteq> S"
+          using openS \<open>x \<in> S\<close> open_contains_ball_eq by blast
+        have "inverse (norm (y - x)) * norm (contour_integral (linepath x y) f - f x * (y - x)) < e"
+          if "0 < d1" "0 < d2" "y \<noteq> x" "dist y x < d1" "dist y x < d2" for y
+        proof -
+          have "f contour_integrable_on linepath x y"
+          proof (rule contour_integrable_continuous_linepath [OF continuous_on_subset])
+            show "continuous_on S f"
+              by (simp add: holf holomorphic_on_imp_continuous_on)
+            have "closed_segment x y \<subseteq> ball x d2"
+              by (meson dist_commute_lessI dist_in_closed_segment le_less_trans mem_ball subsetI that(5))
+            with d2 show "closed_segment x y \<subseteq> S"
+              by blast
+          qed
+          then obtain z where z: "(f has_contour_integral z) (linepath x y)"
+            by (force simp: contour_integrable_on_def)
+          have con: "((\<lambda>w. f x) has_contour_integral f x * (y - x)) (linepath x y)"
+            using has_contour_integral_const_linepath [of "f x" y x] by metis
+          have "norm (z - f x * (y - x)) \<le> (e/2) * norm (y - x)"
+          proof (rule has_contour_integral_bound_linepath)
+            show "((\<lambda>w. f w - f x) has_contour_integral z - f x * (y - x)) (linepath x y)"
+              by (rule has_contour_integral_diff [OF z con])
+            show "\<And>w. w \<in> closed_segment x y \<Longrightarrow> norm (f w - f x) \<le> e/2"
+              by (metis d1 dist_norm less_le_trans not_less not_less_iff_gr_or_eq segment_bound1 that(4))
+          qed (use \<open>e > 0\<close> in auto)
+          with \<open>e > 0\<close> have "inverse (norm (y - x)) * norm (z - f x * (y - x)) \<le> e/2"
+            by (simp add: field_split_simps)
+          also have "... < e"
+            using \<open>e > 0\<close> by simp
+          finally show ?thesis
+            by (simp add: contour_integral_unique [OF z])
+        qed
+        with  \<open>d1 > 0\<close> \<open>d2 > 0\<close>
+        show "\<exists>d>0. \<forall>z. z \<noteq> x \<and> dist z x < d \<longrightarrow>
+                 inverse (norm (z - x)) * norm (contour_integral (linepath x z) f - f x * (z - x)) < e"
+          by (rule_tac x="min d1 d2" in exI) auto
+      qed
+    next
+      have *: "(1 / norm (y - x)) *\<^sub>R (contour_integral (?g y) f -
+               (contour_integral (?g x) f + f x * (y - x))) =
+               (contour_integral (linepath x y) f - f x * (y - x)) /\<^sub>R norm (y - x)"
+        if "0 < d" "y \<noteq> x" and yx: "dist y x < d" for y
+      proof -
+        have "y \<in> S"
+          by (metis subsetD d dist_commute less_eq_real_def mem_cball yx)
+        have gxy: "polynomial_function (?g x) \<and> path_image (?g x) \<subseteq> S \<and> pathstart (?g x) = a \<and> pathfinish (?g x) = x"
+                  "polynomial_function (?g y) \<and> path_image (?g y) \<subseteq> S \<and> pathstart (?g y) = a \<and> pathfinish (?g y) = y"
+          using someI_ex [OF connected_open_polynomial_connected [OF openS \<open>connected S\<close> \<open>a \<in> S\<close>]] \<open>x \<in> S\<close> \<open>y \<in> S\<close>
+          by meson+
+        then have vp: "valid_path (?g x)" "valid_path (?g y)"
+          by (simp_all add: valid_path_polynomial_function)
+        have f0: "(f has_contour_integral 0) ((?g x) +++ linepath x y +++ reversepath (?g y))"
+        proof (rule prev)
+          show "valid_path ((?g x) +++ linepath x y +++ reversepath (?g y))"
+            using gxy vp by (auto simp: valid_path_join)
+          have "closed_segment x y \<subseteq> cball x d"
+            using  yx by (auto simp: dist_commute dest!: dist_in_closed_segment)
+          then have "closed_segment x y \<subseteq> S"
+            using d by blast
+          then show "path_image ((?g x) +++ linepath x y +++ reversepath (?g y)) \<subseteq> S"
+            using gxy by (auto simp: path_image_join)
+        qed (use gxy holf in auto)
+        then have fintxy: "f contour_integrable_on linepath x y"
+          by (metis (no_types, lifting) contour_integrable_joinD1 contour_integrable_joinD2 gxy(2) has_contour_integral_integrable pathfinish_linepath pathstart_reversepath valid_path_imp_reverse valid_path_join valid_path_linepath vp(2))
+        have fintgx: "f contour_integrable_on (?g x)" "f contour_integrable_on (?g y)"
+          using openS contour_integrable_holomorphic_simple gxy holf vp by blast+
+        show ?thesis
+          apply (clarsimp simp add: divide_simps)
+          using contour_integral_unique [OF f0]
+          apply (simp add: fintxy gxy contour_integrable_reversepath contour_integral_reversepath fintgx vp)
+          apply (simp add: algebra_simps)
+          done
+      qed
+      show "(\<lambda>z. (1 / norm (z - x)) *\<^sub>R
+                 (contour_integral (?g z) f - (contour_integral (?g x) f + f x * (z - x))) -
+                 (contour_integral (linepath x z) f - f x * (z - x)) /\<^sub>R norm (z - x))
+            \<midarrow>x\<rightarrow> 0"
+        apply (rule tendsto_eventually)
+        apply (simp add: eventually_at)
+        apply (rule_tac x=d in exI)
+        using \<open>d > 0\<close> * by simp
+    qed
+  qed
+qed
+
+lemma holomorphic_log:
+  assumes "connected S" and holf: "f holomorphic_on S" and nz: "\<And>z. z \<in> S \<Longrightarrow> f z \<noteq> 0"
+  and prev: "\<And>f. f holomorphic_on S \<Longrightarrow> \<exists>h. \<forall>z \<in> S. (h has_field_derivative f z) (at z)"
+  shows "\<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S. f z = exp(g z))"
+proof -
+  have "(\<lambda>z. deriv f z / f z) holomorphic_on S"
+    by (simp add: openS holf holomorphic_deriv holomorphic_on_divide nz)
+  then obtain g where g: "\<And>z. z \<in> S \<Longrightarrow> (g has_field_derivative deriv f z / f z) (at z)"
+    using prev [of "\<lambda>z. deriv f z / f z"] by metis
+  have hfd: "\<And>x. x \<in> S \<Longrightarrow> ((\<lambda>z. exp (g z) / f z) has_field_derivative 0) (at x)"
+    apply (rule derivative_eq_intros g| simp)+
+      apply (subst DERIV_deriv_iff_field_differentiable)
+    using openS holf holomorphic_on_imp_differentiable_at nz apply auto
+    done
+  obtain c where c: "\<And>x. x \<in> S \<Longrightarrow> exp (g x) / f x = c"
+  proof (rule DERIV_zero_connected_constant[OF \<open>connected S\<close> openS finite.emptyI])
+    show "continuous_on S (\<lambda>z. exp (g z) / f z)"
+      by (metis (full_types) openS g continuous_on_divide continuous_on_exp holf holomorphic_on_imp_continuous_on holomorphic_on_open nz)
+    then show "\<forall>x\<in>S - {}. ((\<lambda>z. exp (g z) / f z) has_field_derivative 0) (at x)"
+      using hfd by (blast intro: DERIV_zero_connected_constant [OF \<open>connected S\<close> openS finite.emptyI, of "\<lambda>z. exp(g z) / f z"])
+  qed auto
+  show ?thesis
+  proof (intro exI ballI conjI)
+    show "(\<lambda>z. Ln(inverse c) + g z) holomorphic_on S"
+      apply (intro holomorphic_intros)
+      using openS g holomorphic_on_open by blast
+    fix z :: complex
+    assume "z \<in> S"
+    then have "exp (g z) / c = f z"
+      by (metis c divide_divide_eq_right exp_not_eq_zero nonzero_mult_div_cancel_left)
+    moreover have "1 / c \<noteq> 0"
+      using \<open>z \<in> S\<close> c nz by fastforce
+    ultimately show "f z = exp (Ln (inverse c) + g z)"
+      by (simp add: exp_add inverse_eq_divide)
+  qed
+qed
+
+lemma holomorphic_sqrt:
+  assumes holf: "f holomorphic_on S" and nz: "\<And>z. z \<in> S \<Longrightarrow> f z \<noteq> 0"
+  and prev: "\<And>f. \<lbrakk>f holomorphic_on S; \<forall>z \<in> S. f z \<noteq> 0\<rbrakk> \<Longrightarrow> \<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S. f z = exp(g z))"
+  shows "\<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S. f z = (g z)\<^sup>2)"
+proof -
+  obtain g where holg: "g holomorphic_on S" and g: "\<And>z. z \<in> S \<Longrightarrow> f z = exp (g z)"
+    using prev [of f] holf nz by metis
+  show ?thesis
+  proof (intro exI ballI conjI)
+    show "(\<lambda>z. exp(g z/2)) holomorphic_on S"
+      by (intro holomorphic_intros) (auto simp: holg)
+    show "\<And>z. z \<in> S \<Longrightarrow> f z = (exp (g z/2))\<^sup>2"
+      by (metis (no_types) g exp_double nonzero_mult_div_cancel_left times_divide_eq_right zero_neq_numeral)
+  qed
+qed
+
+lemma biholomorphic_to_disc:
+  assumes "connected S" and S: "S \<noteq> {}" "S \<noteq> UNIV"
+  and prev: "\<And>f. \<lbrakk>f holomorphic_on S; \<forall>z \<in> S. f z \<noteq> 0\<rbrakk> \<Longrightarrow> \<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S. f z = (g z)\<^sup>2)"
+  shows "\<exists>f g. f holomorphic_on S \<and> g holomorphic_on ball 0 1 \<and>
+                   (\<forall>z \<in> S. f z \<in> ball 0 1 \<and> g(f z) = z) \<and>
+                   (\<forall>z \<in> ball 0 1. g z \<in> S \<and> f(g z) = z)"
+proof -
+  obtain a b where "a \<in> S" "b \<notin> S"
+    using S by blast
+  then obtain \<delta> where "\<delta> > 0" and \<delta>: "ball a \<delta> \<subseteq> S"
+    using openS openE by blast
+  obtain g where holg: "g holomorphic_on S" and eqg: "\<And>z. z \<in> S \<Longrightarrow> z - b = (g z)\<^sup>2"
+  proof (rule exE [OF prev [of "\<lambda>z. z - b"]])
+    show "(\<lambda>z. z - b) holomorphic_on S"
+      by (intro holomorphic_intros)
+  qed (use \<open>b \<notin> S\<close> in auto)
+  have "\<not> g constant_on S"
+  proof -
+    have "(a + \<delta>/2) \<in> ball a \<delta>" "a + (\<delta>/2) \<noteq> a"
+      using \<open>\<delta> > 0\<close> by (simp_all add: dist_norm)
+    then show ?thesis
+      unfolding constant_on_def
+      using eqg [of a] eqg [of "a + \<delta>/2"] \<open>a \<in> S\<close> \<delta>
+      by (metis diff_add_cancel subset_eq)
+  qed
+  then have "open (g ` ball a \<delta>)"
+    using open_mapping_thm [of g S "ball a \<delta>", OF holg openS \<open>connected S\<close>] \<delta> by blast
+  then obtain r where "r > 0" and r: "ball (g a) r \<subseteq> (g ` ball a \<delta>)"
+    by (metis \<open>0 < \<delta>\<close> centre_in_ball imageI openE)
+  have g_not_r: "g z \<notin> ball (-(g a)) r" if "z \<in> S" for z
+  proof
+    assume "g z \<in> ball (-(g a)) r"
+    then have "- g z \<in> ball (g a) r"
+      by (metis add.inverse_inverse dist_minus mem_ball)
+    with r have "- g z \<in> (g ` ball a \<delta>)"
+      by blast
+    then obtain w where w: "- g z = g w" "dist a w < \<delta>"
+      by auto
+    then have "w \<in> ball a \<delta>"
+      by simp
+    then have "w \<in> S"
+      using \<delta> by blast
+    then have "w = z"
+      by (metis diff_add_cancel eqg power_minus_Bit0 that w(1))
+    then have "g z = 0"
+      using \<open>- g z = g w\<close> by auto
+    with eqg [OF that] have "z = b"
+      by auto
+    with that \<open>b \<notin> S\<close> show False
+      by simp
+  qed
+  then have nz: "\<And>z. z \<in> S \<Longrightarrow> g z + g a \<noteq> 0"
+    by (metis \<open>0 < r\<close> add.commute add_diff_cancel_left' centre_in_ball diff_0)
+  let ?f = "\<lambda>z. (r/3) / (g z + g a) - (r/3) / (g a + g a)"
+  obtain h where holh: "h holomorphic_on S" and "h a = 0" and h01: "h ` S \<subseteq> ball 0 1" and "inj_on h S"
+  proof
+    show "?f holomorphic_on S"
+      by (intro holomorphic_intros holg nz)
+    have 3: "\<lbrakk>norm x \<le> 1/3; norm y \<le> 1/3\<rbrakk> \<Longrightarrow> norm(x - y) < 1" for x y::complex
+      using norm_triangle_ineq4 [of x y] by simp
+    have "norm ((r/3) / (g z + g a) - (r/3) / (g a + g a)) < 1" if "z \<in> S" for z
+      apply (rule 3)
+      unfolding norm_divide
+      using \<open>r > 0\<close> g_not_r [OF \<open>z \<in> S\<close>] g_not_r [OF \<open>a \<in> S\<close>]
+      by (simp_all add: field_split_simps dist_commute dist_norm)
+  then show "?f ` S \<subseteq> ball 0 1"
+    by auto
+    show "inj_on ?f S"
+      using \<open>r > 0\<close> eqg apply (clarsimp simp: inj_on_def)
+      by (metis diff_add_cancel)
+  qed auto
+  obtain k where holk: "k holomorphic_on (h ` S)"
+             and derk: "\<And>z. z \<in> S \<Longrightarrow> deriv h z * deriv k (h z) = 1"
+             and kh: "\<And>z. z \<in> S \<Longrightarrow> k(h z) = z"
+    using holomorphic_has_inverse [OF holh openS \<open>inj_on h S\<close>] by metis
+
+  have 1: "open (h ` S)"
+    by (simp add: \<open>inj_on h S\<close> holh openS open_mapping_thm3)
+  have 2: "connected (h ` S)"
+    by (simp add: connected_continuous_image \<open>connected S\<close> holh holomorphic_on_imp_continuous_on)
+  have 3: "0 \<in> h ` S"
+    using \<open>a \<in> S\<close> \<open>h a = 0\<close> by auto
+  have 4: "\<exists>g. g holomorphic_on h ` S \<and> (\<forall>z\<in>h ` S. f z = (g z)\<^sup>2)"
+    if holf: "f holomorphic_on h ` S" and nz: "\<And>z. z \<in> h ` S \<Longrightarrow> f z \<noteq> 0" "inj_on f (h ` S)" for f
+  proof -
+    obtain g where holg: "g holomorphic_on S" and eqg: "\<And>z. z \<in> S \<Longrightarrow> (f \<circ> h) z = (g z)\<^sup>2"
+    proof -
+      have "f \<circ> h holomorphic_on S"
+        by (simp add: holh holomorphic_on_compose holf)
+      moreover have "\<forall>z\<in>S. (f \<circ> h) z \<noteq> 0"
+        by (simp add: nz)
+      ultimately show thesis
+        using prev that by blast
+    qed
+    show ?thesis
+    proof (intro exI conjI)
+      show "g \<circ> k holomorphic_on h ` S"
+      proof -
+        have "k ` h ` S \<subseteq> S"
+          by (simp add: \<open>\<And>z. z \<in> S \<Longrightarrow> k (h z) = z\<close> image_subset_iff)
+        then show ?thesis
+          by (meson holg holk holomorphic_on_compose holomorphic_on_subset)
+      qed
+      show "\<forall>z\<in>h ` S. f z = ((g \<circ> k) z)\<^sup>2"
+        using eqg kh by auto
+    qed
+  qed
+  obtain f g where f: "f holomorphic_on h ` S" and g: "g holomorphic_on ball 0 1"
+       and gf: "\<forall>z\<in>h ` S. f z \<in> ball 0 1 \<and> g (f z) = z"  and fg:"\<forall>z\<in>ball 0 1. g z \<in> h ` S \<and> f (g z) = z"
+    using biholomorphic_to_disc_aux [OF 1 2 3 h01 4] by blast
+  show ?thesis
+  proof (intro exI conjI)
+    show "f \<circ> h holomorphic_on S"
+      by (simp add: f holh holomorphic_on_compose)
+    show "k \<circ> g holomorphic_on ball 0 1"
+      by (metis holomorphic_on_subset image_subset_iff fg holk g holomorphic_on_compose)
+  qed (use fg gf kh in auto)
+qed
+
+lemma homeomorphic_to_disc:
+  assumes S: "S \<noteq> {}"
+    and prev: "S = UNIV \<or>
+               (\<exists>f g. f holomorphic_on S \<and> g holomorphic_on ball 0 1 \<and>
+                     (\<forall>z \<in> S. f z \<in> ball 0 1 \<and> g(f z) = z) \<and>
+                     (\<forall>z \<in> ball 0 1. g z \<in> S \<and> f(g z) = z))" (is "_ \<or> ?P")
+  shows "S homeomorphic ball (0::complex) 1"
+  using prev
+proof
+  assume "S = UNIV" then show ?thesis
+    using homeomorphic_ball01_UNIV homeomorphic_sym by blast
+next
+  assume ?P
+  then show ?thesis
+    unfolding homeomorphic_minimal
+    using holomorphic_on_imp_continuous_on by blast
+qed
+
+lemma homeomorphic_to_disc_imp_simply_connected:
+  assumes "S = {} \<or> S homeomorphic ball (0::complex) 1"
+  shows "simply_connected S"
+  using assms homeomorphic_simply_connected_eq convex_imp_simply_connected by auto
+
+end
+
+proposition
+  assumes "open S"
+  shows simply_connected_eq_winding_number_zero:
+         "simply_connected S \<longleftrightarrow>
+           connected S \<and>
+           (\<forall>g z. path g \<and> path_image g \<subseteq> S \<and>
+                 pathfinish g = pathstart g \<and> (z \<notin> S)
+                 \<longrightarrow> winding_number g z = 0)" (is "?wn0")
+    and simply_connected_eq_contour_integral_zero:
+         "simply_connected S \<longleftrightarrow>
+           connected S \<and>
+           (\<forall>g f. valid_path g \<and> path_image g \<subseteq> S \<and>
+                 pathfinish g = pathstart g \<and> f holomorphic_on S
+               \<longrightarrow> (f has_contour_integral 0) g)" (is "?ci0")
+    and simply_connected_eq_global_primitive:
+         "simply_connected S \<longleftrightarrow>
+           connected S \<and>
+           (\<forall>f. f holomorphic_on S \<longrightarrow>
+                (\<exists>h. \<forall>z. z \<in> S \<longrightarrow> (h has_field_derivative f z) (at z)))" (is "?gp")
+    and simply_connected_eq_holomorphic_log:
+         "simply_connected S \<longleftrightarrow>
+           connected S \<and>
+           (\<forall>f. f holomorphic_on S \<and> (\<forall>z \<in> S. f z \<noteq> 0)
+               \<longrightarrow> (\<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S. f z = exp(g z))))" (is "?log")
+    and simply_connected_eq_holomorphic_sqrt:
+         "simply_connected S \<longleftrightarrow>
+           connected S \<and>
+           (\<forall>f. f holomorphic_on S \<and> (\<forall>z \<in> S. f z \<noteq> 0)
+                \<longrightarrow> (\<exists>g. g holomorphic_on S \<and> (\<forall>z \<in> S.  f z = (g z)\<^sup>2)))" (is "?sqrt")
+    and simply_connected_eq_biholomorphic_to_disc:
+         "simply_connected S \<longleftrightarrow>
+           S = {} \<or> S = UNIV \<or>
+           (\<exists>f g. f holomorphic_on S \<and> g holomorphic_on ball 0 1 \<and>
+                 (\<forall>z \<in> S. f z \<in> ball 0 1 \<and> g(f z) = z) \<and>
+                 (\<forall>z \<in> ball 0 1. g z \<in> S \<and> f(g z) = z))" (is "?bih")
+    and simply_connected_eq_homeomorphic_to_disc:
+          "simply_connected S \<longleftrightarrow> S = {} \<or> S homeomorphic ball (0::complex) 1" (is "?disc")
+proof -
+  interpret SC_Chain
+    using assms by (simp add: SC_Chain_def)
+  have "?wn0 \<and> ?ci0 \<and> ?gp \<and> ?log \<and> ?sqrt \<and> ?bih \<and> ?disc"
+proof -
+  have *: "\<lbrakk>\<alpha> \<Longrightarrow> \<beta>; \<beta> \<Longrightarrow> \<gamma>; \<gamma> \<Longrightarrow> \<delta>; \<delta> \<Longrightarrow> \<zeta>; \<zeta> \<Longrightarrow> \<eta>; \<eta> \<Longrightarrow> \<theta>; \<theta> \<Longrightarrow> \<xi>; \<xi> \<Longrightarrow> \<alpha>\<rbrakk>
+        \<Longrightarrow> (\<alpha> \<longleftrightarrow> \<beta>) \<and> (\<alpha> \<longleftrightarrow> \<gamma>) \<and> (\<alpha> \<longleftrightarrow> \<delta>) \<and> (\<alpha> \<longleftrightarrow> \<zeta>) \<and>
+            (\<alpha> \<longleftrightarrow> \<eta>) \<and> (\<alpha> \<longleftrightarrow> \<theta>) \<and> (\<alpha> \<longleftrightarrow> \<xi>)" for \<alpha> \<beta> \<gamma> \<delta> \<zeta> \<eta> \<theta> \<xi>
+    by blast
+  show ?thesis
+    apply (rule *)
+    using winding_number_zero apply metis
+    using contour_integral_zero apply metis
+    using global_primitive apply metis
+    using holomorphic_log apply metis
+    using holomorphic_sqrt apply simp
+    using biholomorphic_to_disc apply blast
+    using homeomorphic_to_disc apply blast
+    using homeomorphic_to_disc_imp_simply_connected apply blast
+    done
+qed
+  then show ?wn0 ?ci0 ?gp ?log ?sqrt ?bih ?disc
+    by safe
+qed
+
+corollary contractible_eq_simply_connected_2d:
+  fixes S :: "complex set"
+  shows "open S \<Longrightarrow> (contractible S \<longleftrightarrow> simply_connected S)"
+  apply safe
+   apply (simp add: contractible_imp_simply_connected)
+  using convex_imp_contractible homeomorphic_contractible_eq simply_connected_eq_homeomorphic_to_disc by auto
+
+subsection\<open>A further chain of equivalences about components of the complement of a simply connected set\<close>
+
+text\<open>(following 1.35 in Burckel'S book)\<close>
+
+context SC_Chain
+begin
+
+lemma frontier_properties:
+  assumes "simply_connected S"
+  shows "if bounded S then connected(frontier S)
+         else \<forall>C \<in> components(frontier S). \<not> bounded C"
+proof -
+  have "S = {} \<or> S homeomorphic ball (0::complex) 1"
+    using simply_connected_eq_homeomorphic_to_disc assms openS by blast
+  then show ?thesis
+  proof
+    assume "S = {}"
+    then have "bounded S"
+      by simp
+    with \<open>S = {}\<close> show ?thesis
+      by simp
+  next
+    assume S01: "S homeomorphic ball (0::complex) 1"
+    then obtain g f
+      where gim: "g ` S = ball 0 1" and fg: "\<And>x. x \<in> S \<Longrightarrow> f(g x) = x"
+        and fim: "f ` ball 0 1 = S" and gf: "\<And>y. cmod y < 1 \<Longrightarrow> g(f y) = y"
+        and contg: "continuous_on S g" and contf: "continuous_on (ball 0 1) f"
+      by (fastforce simp: homeomorphism_def homeomorphic_def)
+    define D where "D \<equiv> \<lambda>n. ball (0::complex) (1 - 1/(of_nat n + 2))"
+    define A where "A \<equiv> \<lambda>n. {z::complex. 1 - 1/(of_nat n + 2) < norm z \<and> norm z < 1}"
+    define X where "X \<equiv> \<lambda>n::nat. closure(f ` A n)"
+    have D01: "D n \<subseteq> ball 0 1" for n
+      by (simp add: D_def ball_subset_ball_iff)
+    have A01: "A n \<subseteq> ball 0 1" for n
+      by (auto simp: A_def)
+    have cloX: "closed(X n)" for n
+      by (simp add: X_def)
+    have Xsubclo: "X n \<subseteq> closure S" for n
+      unfolding X_def by (metis A01 closure_mono fim image_mono)
+    have connX: "connected(X n)" for n
+      unfolding X_def
+      apply (rule connected_imp_connected_closure)
+      apply (rule connected_continuous_image)
+      apply (simp add: continuous_on_subset [OF contf A01])
+      using connected_annulus [of _ "0::complex"] by (simp add: A_def)
+    have nestX: "X n \<subseteq> X m" if "m \<le> n" for m n
+    proof -
+      have "1 - 1 / (real m + 2) \<le> 1 - 1 / (real n + 2)"
+        using that by (auto simp: field_simps)
+      then show ?thesis
+        by (auto simp: X_def A_def intro!: closure_mono)
+    qed
+    have "closure S - S \<subseteq> (\<Inter>n. X n)"
+    proof
+      fix x
+      assume "x \<in> closure S - S"
+      then have "x \<in> closure S" "x \<notin> S" by auto
+      show "x \<in> (\<Inter>n. X n)"
+      proof
+        fix n
+        have "ball 0 1 = closure (D n) \<union> A n"
+          by (auto simp: D_def A_def le_less_trans)
+        with fim have Seq: "S = f ` (closure (D n)) \<union> f ` (A n)"
+          by (simp add: image_Un)
+        have "continuous_on (closure (D n)) f"
+          by (simp add: D_def cball_subset_ball_iff continuous_on_subset [OF contf])
+        moreover have "compact (closure (D n))"
+          by (simp add: D_def)
+        ultimately have clo_fim: "closed (f ` closure (D n))"
+          using compact_continuous_image compact_imp_closed by blast
+        have *: "(f ` cball 0 (1 - 1 / (real n + 2))) \<subseteq> S"
+          by (force simp: D_def Seq)
+        show "x \<in> X n"
+          using \<open>x \<in> closure S\<close> unfolding X_def Seq
+          using \<open>x \<notin> S\<close> * D_def clo_fim by auto
+      qed
+    qed
+    moreover have "(\<Inter>n. X n) \<subseteq> closure S - S"
+    proof -
+      have "(\<Inter>n. X n) \<subseteq> closure S"
+      proof -
+        have "(\<Inter>n. X n) \<subseteq> X 0"
+          by blast
+        also have "... \<subseteq> closure S"
+          apply (simp add: X_def fim [symmetric])
+          apply (rule closure_mono)
+          by (auto simp: A_def)
+        finally show "(\<Inter>n. X n) \<subseteq> closure S" .
+      qed
+      moreover have "(\<Inter>n. X n) \<inter> S \<subseteq> {}"
+      proof (clarify, clarsimp simp: X_def fim [symmetric])
+        fix x assume x [rule_format]: "\<forall>n. f x \<in> closure (f ` A n)" and "cmod x < 1"
+        then obtain n where n: "1 / (1 - norm x) < of_nat n"
+          using reals_Archimedean2 by blast
+        with \<open>cmod x < 1\<close> gr0I have XX: "1 / of_nat n < 1 - norm x" and "n > 0"
+          by (fastforce simp: field_split_simps algebra_simps)+
+        have "f x \<in> f ` (D n)"
+          using n \<open>cmod x < 1\<close> by (auto simp: field_split_simps algebra_simps D_def)
+        moreover have " f ` D n \<inter> closure (f ` A n) = {}"
+        proof -
+          have op_fDn: "open(f ` (D n))"
+          proof (rule invariance_of_domain)
+            show "continuous_on (D n) f"
+              by (rule continuous_on_subset [OF contf D01])
+            show "open (D n)"
+              by (simp add: D_def)
+            show "inj_on f (D n)"
+              unfolding inj_on_def using D01 by (metis gf mem_ball_0 subsetCE)
+          qed
+          have injf: "inj_on f (ball 0 1)"
+            by (metis mem_ball_0 inj_on_def gf)
+          have "D n \<union> A n \<subseteq> ball 0 1"
+            using D01 A01 by simp
+          moreover have "D n \<inter> A n = {}"
+            by (auto simp: D_def A_def)
+          ultimately have "f ` D n \<inter> f ` A n = {}"
+            by (metis A01 D01 image_is_empty inj_on_image_Int injf)
+          then show ?thesis
+            by (simp add: open_Int_closure_eq_empty [OF op_fDn])
+        qed
+        ultimately show False
+          using x [of n] by blast
+      qed
+      ultimately
+      show "(\<Inter>n. X n) \<subseteq> closure S - S"
+        using closure_subset disjoint_iff_not_equal by blast
+    qed
+    ultimately have "closure S - S = (\<Inter>n. X n)" by blast
+    then have frontierS: "frontier S = (\<Inter>n. X n)"
+      by (simp add: frontier_def openS interior_open)
+    show ?thesis
+    proof (cases "bounded S")
+      case True
+      have bouX: "bounded (X n)" for n
+        apply (simp add: X_def)
+        apply (rule bounded_closure)
+        by (metis A01 fim image_mono bounded_subset [OF True])
+      have compaX: "compact (X n)" for n
+        apply (simp add: compact_eq_bounded_closed bouX)
+        apply (auto simp: X_def)
+        done
+      have "connected (\<Inter>n. X n)"
+        by (metis nestX compaX connX connected_nest)
+      then show ?thesis
+        by (simp add: True \<open>frontier S = (\<Inter>n. X n)\<close>)
+    next
+      case False
+      have unboundedX: "\<not> bounded(X n)" for n
+      proof
+        assume bXn: "bounded(X n)"
+        have "continuous_on (cball 0 (1 - 1 / (2 + real n))) f"
+          by (simp add: cball_subset_ball_iff continuous_on_subset [OF contf])
+        then have "bounded (f ` cball 0 (1 - 1 / (2 + real n)))"
+          by (simp add: compact_imp_bounded [OF compact_continuous_image])
+        moreover have "bounded (f ` A n)"
+          by (auto simp: X_def closure_subset image_subset_iff bounded_subset [OF bXn])
+        ultimately have "bounded (f ` (cball 0 (1 - 1/(2 + real n)) \<union> A n))"
+          by (simp add: image_Un)
+        then have "bounded (f ` ball 0 1)"
+          apply (rule bounded_subset)
+          apply (auto simp: A_def algebra_simps)
+          done
+        then show False
+          using False by (simp add: fim [symmetric])
+      qed
+      have clo_INTX: "closed(\<Inter>(range X))"
+        by (metis cloX closed_INT)
+      then have lcX: "locally compact (\<Inter>(range X))"
+        by (metis closed_imp_locally_compact)
+      have False if C: "C \<in> components (frontier S)" and boC: "bounded C" for C
+      proof -
+        have "closed C"
+          by (metis C closed_components frontier_closed)
+        then have "compact C"
+          by (metis boC compact_eq_bounded_closed)
+        have Cco: "C \<in> components (\<Inter>(range X))"
+          by (metis frontierS C)
+        obtain K where "C \<subseteq> K" "compact K"
+                   and Ksub: "K \<subseteq> \<Inter>(range X)" and clo: "closed(\<Inter>(range X) - K)"
+        proof (cases "{k. C \<subseteq> k \<and> compact k \<and> openin (top_of_set (\<Inter>(range X))) k} = {}")
+          case True
+          then show ?thesis
+            using Sura_Bura [OF lcX Cco \<open>compact C\<close>] boC
+            by (simp add: True)
+        next
+          case False
+          then obtain L where "compact L" "C \<subseteq> L" and K: "openin (top_of_set (\<Inter>x. X x)) L"
+            by blast
+          show ?thesis
+          proof
+            show "L \<subseteq> \<Inter>(range X)"
+              by (metis K openin_imp_subset)
+            show "closed (\<Inter>(range X) - L)"
+              by (metis closedin_diff closedin_self closedin_closed_trans [OF _ clo_INTX] K)
+          qed (use \<open>compact L\<close> \<open>C \<subseteq> L\<close> in auto)
+        qed
+        obtain U V where "open U" and "compact (closure U)" and "open V" "K \<subseteq> U"
+                     and V: "\<Inter>(range X) - K \<subseteq> V" and "U \<inter> V = {}"
+          using separation_normal_compact [OF \<open>compact K\<close> clo] by blast
+        then have "U \<inter> (\<Inter> (range X) - K) = {}"
+          by blast
+        have "(closure U - U) \<inter> (\<Inter>n. X n \<inter> closure U) \<noteq> {}"
+        proof (rule compact_imp_fip)
+          show "compact (closure U - U)"
+            by (metis \<open>compact (closure U)\<close> \<open>open U\<close> compact_diff)
+          show "\<And>T. T \<in> range (\<lambda>n. X n \<inter> closure U) \<Longrightarrow> closed T"
+            by clarify (metis cloX closed_Int closed_closure)
+          show "(closure U - U) \<inter> \<Inter>\<F> \<noteq> {}"
+            if "finite \<F>" and \<F>: "\<F> \<subseteq> range (\<lambda>n. X n \<inter> closure U)" for \<F>
+          proof
+            assume empty: "(closure U - U) \<inter> \<Inter>\<F> = {}"
+            obtain J where "finite J" and J: "\<F> = (\<lambda>n. X n \<inter> closure U) ` J"
+              using finite_subset_image [OF \<open>finite \<F>\<close> \<F>] by auto
+            show False
+            proof (cases "J = {}")
+              case True
+              with J empty have "closed U"
+                by (simp add: closure_subset_eq)
+              have "C \<noteq> {}"
+                using C in_components_nonempty by blast
+              then have "U \<noteq> {}"
+                using \<open>K \<subseteq> U\<close> \<open>C \<subseteq> K\<close> by blast
+              moreover have "U \<noteq> UNIV"
+                using \<open>compact (closure U)\<close> by auto
+              ultimately show False
+                using \<open>open U\<close> \<open>closed U\<close> clopen by blast
+            next
+              case False
+              define j where "j \<equiv> Max J"
+              have "j \<in> J"
+                by (simp add: False \<open>finite J\<close> j_def)
+              have jmax: "\<And>m. m \<in> J \<Longrightarrow> m \<le> j"
+                by (simp add: j_def \<open>finite J\<close>)
+              have "\<Inter> ((\<lambda>n. X n \<inter> closure U) ` J) = X j \<inter> closure U"
+                using False jmax nestX \<open>j \<in> J\<close> by auto
+              then have "X j \<inter> closure U = X j \<inter> U"
+                apply safe
+                using DiffI J empty apply auto[1]
+                using closure_subset by blast
+              then have "openin (top_of_set (X j)) (X j \<inter> closure U)"
+                by (simp add: openin_open_Int \<open>open U\<close>)
+              moreover have "closedin (top_of_set (X j)) (X j \<inter> closure U)"
+                by (simp add: closedin_closed_Int)
+              moreover have "X j \<inter> closure U \<noteq> X j"
+                by (metis unboundedX \<open>compact (closure U)\<close> bounded_subset compact_eq_bounded_closed inf.order_iff)
+              moreover have "X j \<inter> closure U \<noteq> {}"
+              proof -
+                have "C \<noteq> {}"
+                  using C in_components_nonempty by blast
+                moreover have "C \<subseteq> X j \<inter> closure U"
+                  using \<open>C \<subseteq> K\<close> \<open>K \<subseteq> U\<close> Ksub closure_subset by blast
+                ultimately show ?thesis by blast
+              qed
+              ultimately show False
+                using connX [of j] by (force simp: connected_clopen)
+            qed
+          qed
+        qed
+        moreover have "(\<Inter>n. X n \<inter> closure U) = (\<Inter>n. X n) \<inter> closure U"
+          by blast
+        moreover have "x \<in> U" if "\<And>n. x \<in> X n" "x \<in> closure U" for x
+        proof -
+          have "x \<notin> V"
+            using \<open>U \<inter> V = {}\<close> \<open>open V\<close> closure_iff_nhds_not_empty that(2) by blast
+          then show ?thesis
+            by (metis (no_types) Diff_iff INT_I V \<open>K \<subseteq> U\<close> contra_subsetD that(1))
+        qed
+        ultimately show False
+          by (auto simp: open_Int_closure_eq_empty [OF \<open>open V\<close>, of U])
+      qed
+      then show ?thesis
+        by (auto simp: False)
+    qed
+  qed
+qed
+
+
+lemma unbounded_complement_components:
+  assumes C: "C \<in> components (- S)" and S: "connected S"
+    and prev: "if bounded S then connected(frontier S)
+               else \<forall>C \<in> components(frontier S). \<not> bounded C"
+  shows "\<not> bounded C"
+proof (cases "bounded S")
+  case True
+  with prev have "S \<noteq> UNIV" and confr: "connected(frontier S)"
+    by auto
+  obtain w where C_ccsw: "C = connected_component_set (- S) w" and "w \<notin> S"
+    using C by (auto simp: components_def)
+  show ?thesis
+  proof (cases "S = {}")
+    case True with C show ?thesis by auto
+  next
+    case False
+    show ?thesis
+    proof
+      assume "bounded C"
+      then have "outside C \<noteq> {}"
+        using outside_bounded_nonempty by metis
+      then obtain z where z: "\<not> bounded (connected_component_set (- C) z)" and "z \<notin> C"
+        by (auto simp: outside_def)
+      have clo_ccs: "closed (connected_component_set (- S) x)" for x
+        by (simp add: closed_Compl closed_connected_component openS)
+      have "connected_component_set (- S) w = connected_component_set (- S) z"
+      proof (rule joinable_connected_component_eq [OF confr])
+        show "frontier S \<subseteq> - S"
+          using openS by (auto simp: frontier_def interior_open)
+        have False if "connected_component_set (- S) w \<inter> frontier (- S) = {}"
+        proof -
+          have "C \<inter> frontier S = {}"
+            using that by (simp add: C_ccsw)
+          then show False
+            by (metis C_ccsw ComplI Compl_eq_Compl_iff Diff_subset False \<open>w \<notin> S\<close> clo_ccs closure_closed compl_bot_eq connected_component_eq_UNIV connected_component_eq_empty empty_subsetI frontier_complement frontier_def frontier_not_empty frontier_of_connected_component_subset le_inf_iff subset_antisym)
+        qed
+        then show "connected_component_set (- S) w \<inter> frontier S \<noteq> {}"
+          by auto
+        have *: "\<lbrakk>frontier C \<subseteq> C; frontier C \<subseteq> F; frontier C \<noteq> {}\<rbrakk> \<Longrightarrow> C \<inter> F \<noteq> {}" for C F::"complex set"
+          by blast
+        have "connected_component_set (- S) z \<inter> frontier (- S) \<noteq> {}"
+        proof (rule *)
+          show "frontier (connected_component_set (- S) z) \<subseteq> connected_component_set (- S) z"
+            by (auto simp: closed_Compl closed_connected_component frontier_def openS)
+          show "frontier (connected_component_set (- S) z) \<subseteq> frontier (- S)"
+            using frontier_of_connected_component_subset by fastforce
+          have "\<not> bounded (-S)"
+            by (simp add: True cobounded_imp_unbounded)
+          then have "connected_component_set (- S) z \<noteq> {}"
+            apply (simp only: connected_component_eq_empty)
+            using confr openS \<open>bounded C\<close> \<open>w \<notin> S\<close>
+            apply (simp add: frontier_def interior_open C_ccsw)
+            by (metis ComplI Compl_eq_Diff_UNIV connected_UNIV closed_closure closure_subset connected_component_eq_self
+                      connected_diff_open_from_closed subset_UNIV)
+          then show "frontier (connected_component_set (- S) z) \<noteq> {}"
+            apply (simp add: frontier_eq_empty connected_component_eq_UNIV)
+            apply (metis False compl_top_eq double_compl)
+            done
+        qed
+        then show "connected_component_set (- S) z \<inter> frontier S \<noteq> {}"
+          by auto
+      qed
+      then show False
+        by (metis C_ccsw Compl_iff \<open>w \<notin> S\<close> \<open>z \<notin> C\<close> connected_component_eq_empty connected_component_idemp)
+    qed
+  qed
+next
+  case False
+  obtain w where C_ccsw: "C = connected_component_set (- S) w" and "w \<notin> S"
+    using C by (auto simp: components_def)
+  have "frontier (connected_component_set (- S) w) \<subseteq> connected_component_set (- S) w"
+    by (simp add: closed_Compl closed_connected_component frontier_subset_eq openS)
+  moreover have "frontier (connected_component_set (- S) w) \<subseteq> frontier S"
+    using frontier_complement frontier_of_connected_component_subset by blast
+  moreover have "frontier (connected_component_set (- S) w) \<noteq> {}"
+    by (metis C C_ccsw False bounded_empty compl_top_eq connected_component_eq_UNIV double_compl frontier_not_empty in_components_nonempty)
+  ultimately obtain z where zin: "z \<in> frontier S" and z: "z \<in> connected_component_set (- S) w"
+    by blast
+  have *: "connected_component_set (frontier S) z \<in> components(frontier S)"
+    by (simp add: \<open>z \<in> frontier S\<close> componentsI)
+  with prev False have "\<not> bounded (connected_component_set (frontier S) z)"
+    by simp
+  moreover have "connected_component (- S) w = connected_component (- S) z"
+    using connected_component_eq [OF z] by force
+  ultimately show ?thesis
+    by (metis C_ccsw * zin bounded_subset closed_Compl closure_closed connected_component_maximal
+              connected_component_refl connected_connected_component frontier_closures in_components_subset le_inf_iff mem_Collect_eq openS)
+qed
+
+lemma empty_inside:
+  assumes "connected S" "\<And>C. C \<in> components (- S) \<Longrightarrow> \<not> bounded C"
+  shows "inside S = {}"
+  using assms by (auto simp: components_def inside_def)
+
+lemma empty_inside_imp_simply_connected:
+  "\<lbrakk>connected S; inside S = {}\<rbrakk> \<Longrightarrow> simply_connected S"
+  by (metis ComplI inside_Un_outside openS outside_mono simply_connected_eq_winding_number_zero subsetCE sup_bot.left_neutral winding_number_zero_in_outside)
+
+end
+
+proposition
+  fixes S :: "complex set"
+  assumes "open S"
+  shows simply_connected_eq_frontier_properties:
+         "simply_connected S \<longleftrightarrow>
+          connected S \<and>
+             (if bounded S then connected(frontier S)
+             else (\<forall>C \<in> components(frontier S). \<not>bounded C))" (is "?fp")
+    and simply_connected_eq_unbounded_complement_components:
+         "simply_connected S \<longleftrightarrow>
+          connected S \<and> (\<forall>C \<in> components(- S). \<not>bounded C)" (is "?ucc")
+    and simply_connected_eq_empty_inside:
+         "simply_connected S \<longleftrightarrow>
+          connected S \<and> inside S = {}" (is "?ei")
+proof -
+  interpret SC_Chain
+    using assms by (simp add: SC_Chain_def)
+  have "?fp \<and> ?ucc \<and> ?ei"
+proof -
+  have *: "\<lbrakk>\<alpha> \<Longrightarrow> \<beta>; \<beta> \<Longrightarrow> \<gamma>; \<gamma> \<Longrightarrow> \<delta>; \<delta> \<Longrightarrow> \<alpha>\<rbrakk>
+           \<Longrightarrow> (\<alpha> \<longleftrightarrow> \<beta>) \<and> (\<alpha> \<longleftrightarrow> \<gamma>) \<and> (\<alpha> \<longleftrightarrow> \<delta>)" for \<alpha> \<beta> \<gamma> \<delta>
+    by blast
+  show ?thesis
+    apply (rule *)
+    using frontier_properties simply_connected_imp_connected apply blast
+apply clarify
+    using unbounded_complement_components simply_connected_imp_connected apply blast
+    using empty_inside apply blast
+    using empty_inside_imp_simply_connected apply blast
+    done
+qed
+  then show ?fp ?ucc ?ei
+    by safe
+qed
+
+lemma simply_connected_iff_simple:
+  fixes S :: "complex set"
+  assumes "open S" "bounded S"
+  shows "simply_connected S \<longleftrightarrow> connected S \<and> connected(- S)"
+  apply (simp add: simply_connected_eq_unbounded_complement_components assms, safe)
+   apply (metis DIM_complex assms(2) cobounded_has_bounded_component double_compl order_refl)
+  by (meson assms inside_bounded_complement_connected_empty simply_connected_eq_empty_inside simply_connected_eq_unbounded_complement_components)
+
+subsection\<open>Further equivalences based on continuous logs and sqrts\<close>
+
+context SC_Chain
+begin
+
+lemma continuous_log:
+  fixes f :: "complex\<Rightarrow>complex"
+  assumes S: "simply_connected S"
+    and contf: "continuous_on S f" and nz: "\<And>z. z \<in> S \<Longrightarrow> f z \<noteq> 0"
+  shows "\<exists>g. continuous_on S g \<and> (\<forall>z \<in> S. f z = exp(g z))"
+proof -
+  consider "S = {}" | "S homeomorphic ball (0::complex) 1"
+    using simply_connected_eq_homeomorphic_to_disc [OF openS] S by metis
+  then show ?thesis
+  proof cases
+    case 1 then show ?thesis
+      by simp
+  next
+    case 2
+    then obtain h k :: "complex\<Rightarrow>complex"
+      where kh: "\<And>x. x \<in> S \<Longrightarrow> k(h x) = x" and him: "h ` S = ball 0 1"
+      and conth: "continuous_on S h"
+      and hk: "\<And>y. y \<in> ball 0 1 \<Longrightarrow> h(k y) = y" and kim: "k ` ball 0 1 = S"
+      and contk: "continuous_on (ball 0 1) k"
+      unfolding homeomorphism_def homeomorphic_def by metis
+    obtain g where contg: "continuous_on (ball 0 1) g"
+             and expg: "\<And>z. z \<in> ball 0 1 \<Longrightarrow> (f \<circ> k) z = exp (g z)"
+    proof (rule continuous_logarithm_on_ball)
+      show "continuous_on (ball 0 1) (f \<circ> k)"
+        apply (rule continuous_on_compose [OF contk])
+        using kim continuous_on_subset [OF contf]
+        by blast
+      show "\<And>z. z \<in> ball 0 1 \<Longrightarrow> (f \<circ> k) z \<noteq> 0"
+        using kim nz by auto
+    qed auto
+    then show ?thesis
+      by (metis comp_apply conth continuous_on_compose him imageI kh)
+  qed
+qed
+
+lemma continuous_sqrt:
+  fixes f :: "complex\<Rightarrow>complex"
+  assumes contf: "continuous_on S f" and nz: "\<And>z. z \<in> S \<Longrightarrow> f z \<noteq> 0"
+  and prev: "\<And>f::complex\<Rightarrow>complex.
+                \<lbrakk>continuous_on S f; \<And>z. z \<in> S \<Longrightarrow> f z \<noteq> 0\<rbrakk>
+                  \<Longrightarrow> \<exists>g. continuous_on S g \<and> (\<forall>z \<in> S. f z = exp(g z))"
+  shows "\<exists>g. continuous_on S g \<and> (\<forall>z \<in> S. f z = (g z)\<^sup>2)"
+proof -
+  obtain g where contg: "continuous_on S g" and geq: "\<And>z. z \<in> S \<Longrightarrow> f z = exp(g z)"
+    using contf nz prev by metis
+  show ?thesis
+proof (intro exI ballI conjI)
+  show "continuous_on S (\<lambda>z. exp(g z/2))"
+      by (intro continuous_intros) (auto simp: contg)
+    show "\<And>z. z \<in> S \<Longrightarrow> f z = (exp (g z/2))\<^sup>2"
+      by (metis (no_types, lifting) divide_inverse exp_double geq mult.left_commute mult.right_neutral right_inverse zero_neq_numeral)
+  qed
+qed
+
+lemma continuous_sqrt_imp_simply_connected:
+  assumes "connected S"
+    and prev: "\<And>f::complex\<Rightarrow>complex. \<lbrakk>continuous_on S f; \<forall>z \<in> S. f z \<noteq> 0\<rbrakk>
+                \<Longrightarrow> \<exists>g. continuous_on S g \<and> (\<forall>z \<in> S. f z = (g z)\<^sup>2)"
+  shows "simply_connected S"
+proof (clarsimp simp add: simply_connected_eq_holomorphic_sqrt [OF openS] \<open>connected S\<close>)
+  fix f
+  assume "f holomorphic_on S" and nz: "\<forall>z\<in>S. f z \<noteq> 0"
+  then obtain g where contg: "continuous_on S g" and geq: "\<And>z. z \<in> S \<Longrightarrow> f z = (g z)\<^sup>2"
+    by (metis holomorphic_on_imp_continuous_on prev)
+  show "\<exists>g. g holomorphic_on S \<and> (\<forall>z\<in>S. f z = (g z)\<^sup>2)"
+  proof (intro exI ballI conjI)
+    show "g holomorphic_on S"
+    proof (clarsimp simp add: holomorphic_on_open [OF openS])
+      fix z
+      assume "z \<in> S"
+      with nz geq have "g z \<noteq> 0"
+        by auto
+      obtain \<delta> where "0 < \<delta>" "\<And>w. \<lbrakk>w \<in> S; dist w z < \<delta>\<rbrakk> \<Longrightarrow> dist (g w) (g z) < cmod (g z)"
+        using contg [unfolded continuous_on_iff] by (metis \<open>g z \<noteq> 0\<close> \<open>z \<in> S\<close> zero_less_norm_iff)
+      then have \<delta>: "\<And>w. \<lbrakk>w \<in> S; w \<in> ball z \<delta>\<rbrakk> \<Longrightarrow> g w + g z \<noteq> 0"
+        apply (clarsimp simp: dist_norm)
+        by (metis \<open>g z \<noteq> 0\<close> add_diff_cancel_left' diff_0_right norm_eq_zero norm_increases_online norm_minus_commute norm_not_less_zero not_less_iff_gr_or_eq)
+      have *: "(\<lambda>x. (f x - f z) / (x - z) / (g x + g z)) \<midarrow>z\<rightarrow> deriv f z / (g z + g z)"
+        apply (intro tendsto_intros)
+        using SC_Chain.openS SC_Chain_axioms \<open>f holomorphic_on S\<close> \<open>z \<in> S\<close> has_field_derivativeD holomorphic_derivI apply fastforce
+        using \<open>z \<in> S\<close> contg continuous_on_eq_continuous_at isCont_def openS apply blast
+        by (simp add: \<open>g z \<noteq> 0\<close>)
+      then have "(g has_field_derivative deriv f z / (g z + g z)) (at z)"
+        unfolding has_field_derivative_iff
+      proof (rule Lim_transform_within_open)
+        show "open (ball z \<delta> \<inter> S)"
+          by (simp add: openS open_Int)
+        show "z \<in> ball z \<delta> \<inter> S"
+          using \<open>z \<in> S\<close> \<open>0 < \<delta>\<close> by simp
+        show "\<And>x. \<lbrakk>x \<in> ball z \<delta> \<inter> S; x \<noteq> z\<rbrakk>
+                  \<Longrightarrow> (f x - f z) / (x - z) / (g x + g z) = (g x - g z) / (x - z)"
+          using \<delta>
+          apply (simp add: geq \<open>z \<in> S\<close> divide_simps)
+          apply (auto simp: algebra_simps power2_eq_square)
+          done
+      qed
+      then show "\<exists>f'. (g has_field_derivative f') (at z)" ..
+    qed
+  qed (use geq in auto)
+qed
+
+end
+
+proposition
+  fixes S :: "complex set"
+  assumes "open S"
+  shows simply_connected_eq_continuous_log:
+         "simply_connected S \<longleftrightarrow>
+          connected S \<and>
+          (\<forall>f::complex\<Rightarrow>complex. continuous_on S f \<and> (\<forall>z \<in> S. f z \<noteq> 0)
+            \<longrightarrow> (\<exists>g. continuous_on S g \<and> (\<forall>z \<in> S. f z = exp (g z))))" (is "?log")
+    and simply_connected_eq_continuous_sqrt:
+         "simply_connected S \<longleftrightarrow>
+          connected S \<and>
+          (\<forall>f::complex\<Rightarrow>complex. continuous_on S f \<and> (\<forall>z \<in> S. f z \<noteq> 0)
+            \<longrightarrow> (\<exists>g. continuous_on S g \<and> (\<forall>z \<in> S. f z = (g z)\<^sup>2)))" (is "?sqrt")
+proof -
+  interpret SC_Chain
+    using assms by (simp add: SC_Chain_def)
+  have "?log \<and> ?sqrt"
+proof -
+  have *: "\<lbrakk>\<alpha> \<Longrightarrow> \<beta>; \<beta> \<Longrightarrow> \<gamma>; \<gamma> \<Longrightarrow> \<alpha>\<rbrakk>
+           \<Longrightarrow> (\<alpha> \<longleftrightarrow> \<beta>) \<and> (\<alpha> \<longleftrightarrow> \<gamma>)" for \<alpha> \<beta> \<gamma>
+    by blast
+  show ?thesis
+    apply (rule *)
+    apply (simp add: local.continuous_log winding_number_zero)
+    apply (simp add: continuous_sqrt)
+    apply (simp add: continuous_sqrt_imp_simply_connected)
+    done
+qed
+  then show ?log ?sqrt
+    by safe
+qed
+
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>More Borsukian results\<close>
+
+lemma Borsukian_componentwise_eq:
+  fixes S :: "'a::euclidean_space set"
+  assumes S: "locally connected S \<or> compact S"
+  shows "Borsukian S \<longleftrightarrow> (\<forall>C \<in> components S. Borsukian C)"
+proof -
+  have *: "ANR(-{0::complex})"
+    by (simp add: ANR_delete open_Compl open_imp_ANR)
+  show ?thesis
+    using cohomotopically_trivial_on_components [OF assms *] by (auto simp: Borsukian_alt)
+qed
+
+lemma Borsukian_componentwise:
+  fixes S :: "'a::euclidean_space set"
+  assumes "locally connected S \<or> compact S" "\<And>C. C \<in> components S \<Longrightarrow> Borsukian C"
+  shows "Borsukian S"
+  by (metis Borsukian_componentwise_eq assms)
+
+lemma simply_connected_eq_Borsukian:
+  fixes S :: "complex set"
+  shows "open S \<Longrightarrow> (simply_connected S \<longleftrightarrow> connected S \<and> Borsukian S)"
+  by (auto simp: simply_connected_eq_continuous_log Borsukian_continuous_logarithm)
+
+lemma Borsukian_eq_simply_connected:
+  fixes S :: "complex set"
+  shows "open S \<Longrightarrow> Borsukian S \<longleftrightarrow> (\<forall>C \<in> components S. simply_connected C)"
+apply (auto simp: Borsukian_componentwise_eq open_imp_locally_connected)
+  using in_components_connected open_components simply_connected_eq_Borsukian apply blast
+  using open_components simply_connected_eq_Borsukian by blast
+
+lemma Borsukian_separation_open_closed:
+  fixes S :: "complex set"
+  assumes S: "open S \<or> closed S" and "bounded S"
+  shows "Borsukian S \<longleftrightarrow> connected(- S)"
+  using S
+proof
+  assume "open S"
+  show ?thesis
+    unfolding Borsukian_eq_simply_connected [OF \<open>open S\<close>]
+    by (meson \<open>open S\<close> \<open>bounded S\<close> bounded_subset in_components_connected in_components_subset nonseparation_by_component_eq open_components simply_connected_iff_simple)
+next
+  assume "closed S"
+  with \<open>bounded S\<close> show ?thesis
+    by (simp add: Borsukian_separation_compact compact_eq_bounded_closed)
+qed
+
+
+subsection\<open>Finally, the Riemann Mapping Theorem\<close>
+
+theorem Riemann_mapping_theorem:
+    "open S \<and> simply_connected S \<longleftrightarrow>
+     S = {} \<or> S = UNIV \<or>
+     (\<exists>f g. f holomorphic_on S \<and> g holomorphic_on ball 0 1 \<and>
+           (\<forall>z \<in> S. f z \<in> ball 0 1 \<and> g(f z) = z) \<and>
+           (\<forall>z \<in> ball 0 1. g z \<in> S \<and> f(g z) = z))"
+    (is "_ = ?rhs")
+proof -
+  have "simply_connected S \<longleftrightarrow> ?rhs" if "open S"
+    by (simp add: simply_connected_eq_biholomorphic_to_disc that)
+  moreover have "open S" if "?rhs"
+  proof -
+    { fix f g
+      assume g: "g holomorphic_on ball 0 1" "\<forall>z\<in>ball 0 1. g z \<in> S \<and> f (g z) = z"
+        and "\<forall>z\<in>S. cmod (f z) < 1 \<and> g (f z) = z"
+      then have "S = g ` (ball 0 1)"
+        by (force simp:)
+      then have "open S"
+        by (metis open_ball g inj_on_def open_mapping_thm3)
+    }
+    with that show "open S" by auto
+  qed
+  ultimately show ?thesis by metis
+qed
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Complex_Analysis/Winding_Numbers.thy	Sat Nov 30 13:47:33 2019 +0100
@@ -0,0 +1,1153 @@
+section \<open>Winding Numbers\<close>
+
+text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2017)\<close>
+
+theory Winding_Numbers
+imports
+  Riemann_Mapping
+begin
+
+lemma simply_connected_inside_simple_path:
+  fixes p :: "real \<Rightarrow> complex"
+  shows "simple_path p \<Longrightarrow> simply_connected(inside(path_image p))"
+  using Jordan_inside_outside connected_simple_path_image inside_simple_curve_imp_closed simply_connected_eq_frontier_properties
+  by fastforce
+
+lemma simply_connected_Int:
+  fixes S :: "complex set"
+  assumes "open S" "open T" "simply_connected S" "simply_connected T" "connected (S \<inter> T)"
+  shows "simply_connected (S \<inter> T)"
+  using assms by (force simp: simply_connected_eq_winding_number_zero open_Int)
+
+subsection\<open>Winding number for a triangle\<close>
+
+lemma wn_triangle1:
+  assumes "0 \<in> interior(convex hull {a,b,c})"
+    shows "\<not> (Im(a/b) \<le> 0 \<and> 0 \<le> Im(b/c))"
+proof -
+  { assume 0: "Im(a/b) \<le> 0" "0 \<le> Im(b/c)"
+    have "0 \<notin> interior (convex hull {a,b,c})"
+    proof (cases "a=0 \<or> b=0 \<or> c=0")
+      case True then show ?thesis
+        by (auto simp: not_in_interior_convex_hull_3)
+    next
+      case False
+      then have "b \<noteq> 0" by blast
+      { fix x y::complex and u::real
+        assume eq_f': "Im x * Re b \<le> Im b * Re x" "Im y * Re b \<le> Im b * Re y" "0 \<le> u" "u \<le> 1"
+        then have "((1 - u) * Im x) * Re b \<le> Im b * ((1 - u) * Re x)"
+          by (simp add: mult_left_mono mult.assoc mult.left_commute [of "Im b"])
+        then have "((1 - u) * Im x + u * Im y) * Re b \<le> Im b * ((1 - u) * Re x + u * Re y)"
+          using eq_f' ordered_comm_semiring_class.comm_mult_left_mono
+          by (fastforce simp add: algebra_simps)
+      }
+      with False 0 have "convex hull {a,b,c} \<le> {z. Im z * Re b \<le> Im b * Re z}"
+        apply (simp add: Complex.Im_divide divide_simps complex_neq_0 [symmetric])
+        apply (simp add: algebra_simps)
+        apply (rule hull_minimal)
+        apply (auto simp: algebra_simps convex_alt)
+        done
+      moreover have "0 \<notin> interior({z. Im z * Re b \<le> Im b * Re z})"
+      proof
+        assume "0 \<in> interior {z. Im z * Re b \<le> Im b * Re z}"
+        then obtain e where "e>0" and e: "ball 0 e \<subseteq> {z. Im z * Re b \<le> Im b * Re z}"
+          by (meson mem_interior)
+        define z where "z \<equiv> - sgn (Im b) * (e/3) + sgn (Re b) * (e/3) * \<i>"
+        have "z \<in> ball 0 e"
+          using \<open>e>0\<close>
+          apply (simp add: z_def dist_norm)
+          apply (rule le_less_trans [OF norm_triangle_ineq4])
+          apply (simp add: norm_mult abs_sgn_eq)
+          done
+        then have "z \<in> {z. Im z * Re b \<le> Im b * Re z}"
+          using e by blast
+        then show False
+          using \<open>e>0\<close> \<open>b \<noteq> 0\<close>
+          apply (simp add: z_def dist_norm sgn_if less_eq_real_def mult_less_0_iff complex.expand split: if_split_asm)
+          apply (auto simp: algebra_simps)
+          apply (meson less_asym less_trans mult_pos_pos neg_less_0_iff_less)
+          by (metis less_asym mult_pos_pos neg_less_0_iff_less)
+      qed
+      ultimately show ?thesis
+        using interior_mono by blast
+    qed
+  } with assms show ?thesis by blast
+qed
+
+lemma wn_triangle2_0:
+  assumes "0 \<in> interior(convex hull {a,b,c})"
+  shows
+       "0 < Im((b - a) * cnj (b)) \<and>
+        0 < Im((c - b) * cnj (c)) \<and>
+        0 < Im((a - c) * cnj (a))
+        \<or>
+        Im((b - a) * cnj (b)) < 0 \<and>
+        0 < Im((b - c) * cnj (b)) \<and>
+        0 < Im((a - b) * cnj (a)) \<and>
+        0 < Im((c - a) * cnj (c))"
+proof -
+  have [simp]: "{b,c,a} = {a,b,c}" "{c,a,b} = {a,b,c}" by auto
+  show ?thesis
+    using wn_triangle1 [OF assms] wn_triangle1 [of b c a] wn_triangle1 [of c a b] assms
+    by (auto simp: algebra_simps Im_complex_div_gt_0 Im_complex_div_lt_0 not_le not_less)
+qed
+
+lemma wn_triangle2:
+  assumes "z \<in> interior(convex hull {a,b,c})"
+   shows "0 < Im((b - a) * cnj (b - z)) \<and>
+          0 < Im((c - b) * cnj (c - z)) \<and>
+          0 < Im((a - c) * cnj (a - z))
+          \<or>
+          Im((b - a) * cnj (b - z)) < 0 \<and>
+          0 < Im((b - c) * cnj (b - z)) \<and>
+          0 < Im((a - b) * cnj (a - z)) \<and>
+          0 < Im((c - a) * cnj (c - z))"
+proof -
+  have 0: "0 \<in> interior(convex hull {a-z, b-z, c-z})"
+    using assms convex_hull_translation [of "-z" "{a,b,c}"]
+                interior_translation [of "-z"]
+    by (simp cong: image_cong_simp)
+  show ?thesis using wn_triangle2_0 [OF 0]
+    by simp
+qed
+
+lemma wn_triangle3:
+  assumes z: "z \<in> interior(convex hull {a,b,c})"
+      and "0 < Im((b-a) * cnj (b-z))"
+          "0 < Im((c-b) * cnj (c-z))"
+          "0 < Im((a-c) * cnj (a-z))"
+    shows "winding_number (linepath a b +++ linepath b c +++ linepath c a) z = 1"
+proof -
+  have znot[simp]: "z \<notin> closed_segment a b" "z \<notin> closed_segment b c" "z \<notin> closed_segment c a"
+    using z interior_of_triangle [of a b c]
+    by (auto simp: closed_segment_def)
+  have gt0: "0 < Re (winding_number (linepath a b +++ linepath b c +++ linepath c a) z)"
+    using assms
+    by (simp add: winding_number_linepath_pos_lt path_image_join winding_number_join_pos_combined)
+  have lt2: "Re (winding_number (linepath a b +++ linepath b c +++ linepath c a) z) < 2"
+    using winding_number_lt_half_linepath [of _ a b]
+    using winding_number_lt_half_linepath [of _ b c]
+    using winding_number_lt_half_linepath [of _ c a] znot
+    apply (fastforce simp add: winding_number_join path_image_join)
+    done
+  show ?thesis
+    by (rule winding_number_eq_1) (simp_all add: path_image_join gt0 lt2)
+qed
+
+proposition winding_number_triangle:
+  assumes z: "z \<in> interior(convex hull {a,b,c})"
+    shows "winding_number(linepath a b +++ linepath b c +++ linepath c a) z =
+           (if 0 < Im((b - a) * cnj (b - z)) then 1 else -1)"
+proof -
+  have [simp]: "{a,c,b} = {a,b,c}"  by auto
+  have znot[simp]: "z \<notin> closed_segment a b" "z \<notin> closed_segment b c" "z \<notin> closed_segment c a"
+    using z interior_of_triangle [of a b c]
+    by (auto simp: closed_segment_def)
+  then have [simp]: "z \<notin> closed_segment b a" "z \<notin> closed_segment c b" "z \<notin> closed_segment a c"
+    using closed_segment_commute by blast+
+  have *: "winding_number (linepath a b +++ linepath b c +++ linepath c a) z =
+            winding_number (reversepath (linepath a c +++ linepath c b +++ linepath b a)) z"
+    by (simp add: reversepath_joinpaths winding_number_join not_in_path_image_join)
+  show ?thesis
+    using wn_triangle2 [OF z] apply (rule disjE)
+    apply (simp add: wn_triangle3 z)
+    apply (simp add: path_image_join winding_number_reversepath * wn_triangle3 z)
+    done
+qed
+
+subsection\<open>Winding numbers for simple closed paths\<close>
+
+lemma winding_number_from_innerpath:
+  assumes "simple_path c1" and c1: "pathstart c1 = a" "pathfinish c1 = b"
+      and "simple_path c2" and c2: "pathstart c2 = a" "pathfinish c2 = b"
+      and "simple_path c" and c: "pathstart c = a" "pathfinish c = b"
+      and c1c2: "path_image c1 \<inter> path_image c2 = {a,b}"
+      and c1c:  "path_image c1 \<inter> path_image c = {a,b}"
+      and c2c:  "path_image c2 \<inter> path_image c = {a,b}"
+      and ne_12: "path_image c \<inter> inside(path_image c1 \<union> path_image c2) \<noteq> {}"
+      and z: "z \<in> inside(path_image c1 \<union> path_image c)"
+      and wn_d: "winding_number (c1 +++ reversepath c) z = d"
+      and "a \<noteq> b" "d \<noteq> 0"
+  obtains "z \<in> inside(path_image c1 \<union> path_image c2)" "winding_number (c1 +++ reversepath c2) z = d"
+proof -
+  obtain 0: "inside(path_image c1 \<union> path_image c) \<inter> inside(path_image c2 \<union> path_image c) = {}"
+     and 1: "inside(path_image c1 \<union> path_image c) \<union> inside(path_image c2 \<union> path_image c) \<union>
+             (path_image c - {a,b}) = inside(path_image c1 \<union> path_image c2)"
+    by (rule split_inside_simple_closed_curve
+              [OF \<open>simple_path c1\<close> c1 \<open>simple_path c2\<close> c2 \<open>simple_path c\<close> c \<open>a \<noteq> b\<close> c1c2 c1c c2c ne_12])
+  have znot: "z \<notin> path_image c"  "z \<notin> path_image c1" "z \<notin> path_image c2"
+    using union_with_outside z 1 by auto
+  have wn_cc2: "winding_number (c +++ reversepath c2) z = 0"
+    apply (rule winding_number_zero_in_outside)
+    apply (simp_all add: \<open>simple_path c2\<close> c c2 \<open>simple_path c\<close> simple_path_imp_path path_image_join)
+    by (metis "0" ComplI UnE disjoint_iff_not_equal sup.commute union_with_inside z znot)
+  show ?thesis
+  proof
+    show "z \<in> inside (path_image c1 \<union> path_image c2)"
+      using "1" z by blast
+    have "winding_number c1 z - winding_number c z = d "
+      using assms znot
+      by (metis wn_d winding_number_join simple_path_imp_path winding_number_reversepath add.commute path_image_reversepath path_reversepath pathstart_reversepath uminus_add_conv_diff)
+    then show "winding_number (c1 +++ reversepath c2) z = d"
+      using wn_cc2 by (simp add: winding_number_join simple_path_imp_path assms znot winding_number_reversepath)
+  qed
+qed
+
+lemma simple_closed_path_wn1:
+  fixes a::complex and e::real
+  assumes "0 < e"
+    and sp_pl: "simple_path(p +++ linepath (a - e) (a + e))"
+    and psp:   "pathstart p = a + e"
+    and pfp:   "pathfinish p = a - e"
+    and disj:  "ball a e \<inter> path_image p = {}"
+obtains z where "z \<in> inside (path_image (p +++ linepath (a - e) (a + e)))"
+                "cmod (winding_number (p +++ linepath (a - e) (a + e)) z) = 1"
+proof -
+  have "arc p" and arc_lp: "arc (linepath (a - e) (a + e))"
+    and pap: "path_image p \<inter> path_image (linepath (a - e) (a + e)) \<subseteq> {pathstart p, a-e}"
+    using simple_path_join_loop_eq [of "linepath (a - e) (a + e)" p] assms by auto
+  have mid_eq_a: "midpoint (a - e) (a + e) = a"
+    by (simp add: midpoint_def)
+  then have "a \<in> path_image(p +++ linepath (a - e) (a + e))"
+    apply (simp add: assms path_image_join)
+    by (metis midpoint_in_closed_segment)
+  have "a \<in> frontier(inside (path_image(p +++ linepath (a - e) (a + e))))"
+    apply (simp add: assms Jordan_inside_outside)
+    apply (simp_all add: assms path_image_join)
+    by (metis mid_eq_a midpoint_in_closed_segment)
+  with \<open>0 < e\<close> obtain c where c: "c \<in> inside (path_image(p +++ linepath (a - e) (a + e)))"
+                  and dac: "dist a c < e"
+    by (auto simp: frontier_straddle)
+  then have "c \<notin> path_image(p +++ linepath (a - e) (a + e))"
+    using inside_no_overlap by blast
+  then have "c \<notin> path_image p"
+            "c \<notin> closed_segment (a - of_real e) (a + of_real e)"
+    by (simp_all add: assms path_image_join)
+  with \<open>0 < e\<close> dac have "c \<notin> affine hull {a - of_real e, a + of_real e}"
+    by (simp add: segment_as_ball not_le)
+  with \<open>0 < e\<close> have *: "\<not> collinear {a - e, c,a + e}"
+    using collinear_3_affine_hull [of "a-e" "a+e"] by (auto simp: insert_commute)
+  have 13: "1/3 + 1/3 + 1/3 = (1::real)" by simp
+  have "(1/3) *\<^sub>R (a - of_real e) + (1/3) *\<^sub>R c + (1/3) *\<^sub>R (a + of_real e) \<in> interior(convex hull {a - e, c, a + e})"
+    using interior_convex_hull_3_minimal [OF * DIM_complex]
+    by clarsimp (metis 13 zero_less_divide_1_iff zero_less_numeral)
+  then obtain z where z: "z \<in> interior(convex hull {a - e, c, a + e})" by force
+  have [simp]: "z \<notin> closed_segment (a - e) c"
+    by (metis DIM_complex Diff_iff IntD2 inf_sup_absorb interior_of_triangle z)
+  have [simp]: "z \<notin> closed_segment (a + e) (a - e)"
+    by (metis DIM_complex DiffD2 Un_iff interior_of_triangle z)
+  have [simp]: "z \<notin> closed_segment c (a + e)"
+    by (metis (no_types, lifting) DIM_complex DiffD2 Un_insert_right inf_sup_aci(5) insertCI interior_of_triangle mk_disjoint_insert z)
+  show thesis
+  proof
+    have "norm (winding_number (linepath (a - e) c +++ linepath c (a + e) +++ linepath (a + e) (a - e)) z) = 1"
+      using winding_number_triangle [OF z] by simp
+    have zin: "z \<in> inside (path_image (linepath (a + e) (a - e)) \<union> path_image p)"
+      and zeq: "winding_number (linepath (a + e) (a - e) +++ reversepath p) z =
+                winding_number (linepath (a - e) c +++ linepath c (a + e) +++ linepath (a + e) (a - e)) z"
+    proof (rule winding_number_from_innerpath
+        [of "linepath (a + e) (a - e)" "a+e" "a-e" p
+          "linepath (a + e) c +++ linepath c (a - e)" z
+          "winding_number (linepath (a - e)  c +++ linepath  c (a + e) +++ linepath (a + e) (a - e)) z"])
+      show sp_aec: "simple_path (linepath (a + e) c +++ linepath c (a - e))"
+      proof (rule arc_imp_simple_path [OF arc_join])
+        show "arc (linepath (a + e) c)"
+          by (metis \<open>c \<notin> path_image p\<close> arc_linepath pathstart_in_path_image psp)
+        show "arc (linepath c (a - e))"
+          by (metis \<open>c \<notin> path_image p\<close> arc_linepath pathfinish_in_path_image pfp)
+        show "path_image (linepath (a + e) c) \<inter> path_image (linepath c (a - e)) \<subseteq> {pathstart (linepath c (a - e))}"
+          by clarsimp (metis "*" IntI Int_closed_segment closed_segment_commute singleton_iff)
+      qed auto
+      show "simple_path p"
+        using \<open>arc p\<close> arc_simple_path by blast
+      show sp_ae2: "simple_path (linepath (a + e) (a - e))"
+        using \<open>arc p\<close> arc_distinct_ends pfp psp by fastforce
+      show pa: "pathfinish (linepath (a + e) (a - e)) = a - e"
+           "pathstart (linepath (a + e) c +++ linepath c (a - e)) = a + e"
+           "pathfinish (linepath (a + e) c +++ linepath c (a - e)) = a - e"
+           "pathstart p = a + e" "pathfinish p = a - e"
+           "pathstart (linepath (a + e) (a - e)) = a + e"
+        by (simp_all add: assms)
+      show 1: "path_image (linepath (a + e) (a - e)) \<inter> path_image p = {a + e, a - e}"
+      proof
+        show "path_image (linepath (a + e) (a - e)) \<inter> path_image p \<subseteq> {a + e, a - e}"
+          using pap closed_segment_commute psp segment_convex_hull by fastforce
+        show "{a + e, a - e} \<subseteq> path_image (linepath (a + e) (a - e)) \<inter> path_image p"
+          using pap pathfinish_in_path_image pathstart_in_path_image pfp psp by fastforce
+      qed
+      show 2: "path_image (linepath (a + e) (a - e)) \<inter> path_image (linepath (a + e) c +++ linepath c (a - e)) =
+               {a + e, a - e}"  (is "?lhs = ?rhs")
+      proof
+        have "\<not> collinear {c, a + e, a - e}"
+          using * by (simp add: insert_commute)
+        then have "convex hull {a + e, a - e} \<inter> convex hull {a + e, c} = {a + e}"
+                  "convex hull {a + e, a - e} \<inter> convex hull {c, a - e} = {a - e}"
+          by (metis (full_types) Int_closed_segment insert_commute segment_convex_hull)+
+        then show "?lhs \<subseteq> ?rhs"
+          by (metis Int_Un_distrib equalityD1 insert_is_Un path_image_join path_image_linepath path_join_eq path_linepath segment_convex_hull simple_path_def sp_aec)
+        show "?rhs \<subseteq> ?lhs"
+          using segment_convex_hull by (simp add: path_image_join)
+      qed
+      have "path_image p \<inter> path_image (linepath (a + e) c) \<subseteq> {a + e}"
+      proof (clarsimp simp: path_image_join)
+        fix x
+        assume "x \<in> path_image p" and x_ac: "x \<in> closed_segment (a + e) c"
+        then have "dist x a \<ge> e"
+          by (metis IntI all_not_in_conv disj dist_commute mem_ball not_less)
+        with x_ac dac \<open>e > 0\<close> show "x = a + e"
+          by (auto simp: norm_minus_commute dist_norm closed_segment_eq_open dest: open_segment_furthest_le [where y=a])
+      qed
+      moreover
+      have "path_image p \<inter> path_image (linepath c (a - e)) \<subseteq> {a - e}"
+      proof (clarsimp simp: path_image_join)
+        fix x
+        assume "x \<in> path_image p" and x_ac: "x \<in> closed_segment c (a - e)"
+        then have "dist x a \<ge> e"
+          by (metis IntI all_not_in_conv disj dist_commute mem_ball not_less)
+        with x_ac dac \<open>e > 0\<close> show "x = a - e"
+          by (auto simp: norm_minus_commute dist_norm closed_segment_eq_open dest: open_segment_furthest_le [where y=a])
+      qed
+      ultimately
+      have "path_image p \<inter> path_image (linepath (a + e) c +++ linepath c (a - e)) \<subseteq> {a + e, a - e}"
+        by (force simp: path_image_join)
+      then show 3: "path_image p \<inter> path_image (linepath (a + e) c +++ linepath c (a - e)) = {a + e, a - e}"
+        apply (rule equalityI)
+        apply (clarsimp simp: path_image_join)
+        apply (metis pathstart_in_path_image psp pathfinish_in_path_image pfp)
+        done
+      show 4: "path_image (linepath (a + e) c +++ linepath c (a - e)) \<inter>
+               inside (path_image (linepath (a + e) (a - e)) \<union> path_image p) \<noteq> {}"
+        apply (clarsimp simp: path_image_join segment_convex_hull disjoint_iff_not_equal)
+        by (metis (no_types, hide_lams) UnI1 Un_commute c closed_segment_commute ends_in_segment(1) path_image_join
+                  path_image_linepath pathstart_linepath pfp segment_convex_hull)
+      show zin_inside: "z \<in> inside (path_image (linepath (a + e) (a - e)) \<union>
+                                    path_image (linepath (a + e) c +++ linepath c (a - e)))"
+        apply (simp add: path_image_join)
+        by (metis z inside_of_triangle DIM_complex Un_commute closed_segment_commute)
+      show 5: "winding_number
+             (linepath (a + e) (a - e) +++ reversepath (linepath (a + e) c +++ linepath c (a - e))) z =
+            winding_number (linepath (a - e) c +++ linepath c (a + e) +++ linepath (a + e) (a - e)) z"
+        by (simp add: reversepath_joinpaths path_image_join winding_number_join)
+      show 6: "winding_number (linepath (a - e) c +++ linepath c (a + e) +++ linepath (a + e) (a - e)) z \<noteq> 0"
+        by (simp add: winding_number_triangle z)
+      show "winding_number (linepath (a + e) (a - e) +++ reversepath p) z =
+            winding_number (linepath (a - e) c +++ linepath c (a + e) +++ linepath (a + e) (a - e)) z"
+        by (metis 1 2 3 4 5 6 pa sp_aec sp_ae2 \<open>arc p\<close> \<open>simple_path p\<close> arc_distinct_ends winding_number_from_innerpath zin_inside)
+    qed (use assms \<open>e > 0\<close> in auto)
+    show "z \<in> inside (path_image (p +++ linepath (a - e) (a + e)))"
+      using zin by (simp add: assms path_image_join Un_commute closed_segment_commute)
+    then have "cmod (winding_number (p +++ linepath (a - e) (a + e)) z) =
+               cmod ((winding_number(reversepath (p +++ linepath (a - e) (a + e))) z))"
+      apply (subst winding_number_reversepath)
+      using simple_path_imp_path sp_pl apply blast
+       apply (metis IntI emptyE inside_no_overlap)
+      by (simp add: inside_def)
+    also have "... = cmod (winding_number(linepath (a + e) (a - e) +++ reversepath p) z)"
+      by (simp add: pfp reversepath_joinpaths)
+    also have "... = cmod (winding_number (linepath (a - e) c +++ linepath c (a + e) +++ linepath (a + e) (a - e)) z)"
+      by (simp add: zeq)
+    also have "... = 1"
+      using z by (simp add: interior_of_triangle winding_number_triangle)
+    finally show "cmod (winding_number (p +++ linepath (a - e) (a + e)) z) = 1" .
+  qed
+qed
+
+lemma simple_closed_path_wn2:
+  fixes a::complex and d e::real
+  assumes "0 < d" "0 < e"
+    and sp_pl: "simple_path(p +++ linepath (a - d) (a + e))"
+    and psp:   "pathstart p = a + e"
+    and pfp:   "pathfinish p = a - d"
+obtains z where "z \<in> inside (path_image (p +++ linepath (a - d) (a + e)))"
+                "cmod (winding_number (p +++ linepath (a - d) (a + e)) z) = 1"
+proof -
+  have [simp]: "a + of_real x \<in> closed_segment (a - \<alpha>) (a - \<beta>) \<longleftrightarrow> x \<in> closed_segment (-\<alpha>) (-\<beta>)" for x \<alpha> \<beta>::real
+    using closed_segment_translation_eq [of a]
+    by (metis (no_types, hide_lams) add_uminus_conv_diff of_real_minus of_real_closed_segment)
+  have [simp]: "a - of_real x \<in> closed_segment (a + \<alpha>) (a + \<beta>) \<longleftrightarrow> -x \<in> closed_segment \<alpha> \<beta>" for x \<alpha> \<beta>::real
+    by (metis closed_segment_translation_eq diff_conv_add_uminus of_real_closed_segment of_real_minus)
+  have "arc p" and arc_lp: "arc (linepath (a - d) (a + e))" and "path p"
+    and pap: "path_image p \<inter> closed_segment (a - d) (a + e) \<subseteq> {a+e, a-d}"
+    using simple_path_join_loop_eq [of "linepath (a - d) (a + e)" p] assms arc_imp_path  by auto
+  have "0 \<in> closed_segment (-d) e"
+    using \<open>0 < d\<close> \<open>0 < e\<close> closed_segment_eq_real_ivl by auto
+  then have "a \<in> path_image (linepath (a - d) (a + e))"
+    using of_real_closed_segment [THEN iffD2]
+    by (force dest: closed_segment_translation_eq [of a, THEN iffD2] simp del: of_real_closed_segment)
+  then have "a \<notin> path_image p"
+    using \<open>0 < d\<close> \<open>0 < e\<close> pap by auto
+  then obtain k where "0 < k" and k: "ball a k \<inter> (path_image p) = {}"
+    using \<open>0 < e\<close> \<open>path p\<close> not_on_path_ball by blast
+  define kde where "kde \<equiv> (min k (min d e)) / 2"
+  have "0 < kde" "kde < k" "kde < d" "kde < e"
+    using \<open>0 < k\<close> \<open>0 < d\<close> \<open>0 < e\<close> by (auto simp: kde_def)
+  let ?q = "linepath (a + kde) (a + e) +++ p +++ linepath (a - d) (a - kde)"
+  have "- kde \<in> closed_segment (-d) e"
+    using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>kde < e\<close> closed_segment_eq_real_ivl by auto
+  then have a_diff_kde: "a - kde \<in> closed_segment (a - d) (a + e)"
+    using of_real_closed_segment [THEN iffD2]
+    by (force dest: closed_segment_translation_eq [of a, THEN iffD2] simp del: of_real_closed_segment)
+  then have clsub2: "closed_segment (a - d) (a - kde) \<subseteq> closed_segment (a - d) (a + e)"
+    by (simp add: subset_closed_segment)
+  then have "path_image p \<inter> closed_segment (a - d) (a - kde) \<subseteq> {a + e, a - d}"
+    using pap by force
+  moreover
+  have "a + e \<notin> path_image p \<inter> closed_segment (a - d) (a - kde)"
+    using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>0 < e\<close> by (auto simp: closed_segment_eq_real_ivl)
+  ultimately have sub_a_diff_d: "path_image p \<inter> closed_segment (a - d) (a - kde) \<subseteq> {a - d}"
+    by blast
+  have "kde \<in> closed_segment (-d) e"
+    using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>kde < e\<close> closed_segment_eq_real_ivl by auto
+  then have a_diff_kde: "a + kde \<in> closed_segment (a - d) (a + e)"
+    using of_real_closed_segment [THEN iffD2]
+    by (force dest: closed_segment_translation_eq [of "a", THEN iffD2] simp del: of_real_closed_segment)
+  then have clsub1: "closed_segment (a + kde) (a + e) \<subseteq> closed_segment (a - d) (a + e)"
+    by (simp add: subset_closed_segment)
+  then have "closed_segment (a + kde) (a + e) \<inter> path_image p \<subseteq> {a + e, a - d}"
+    using pap by force
+  moreover
+  have "closed_segment (a + kde) (a + e) \<inter> closed_segment (a - d) (a - kde) = {}"
+  proof (clarsimp intro!: equals0I)
+    fix y
+    assume y1: "y \<in> closed_segment (a + kde) (a + e)"
+       and y2: "y \<in> closed_segment (a - d) (a - kde)"
+    obtain u where u: "y = a + of_real u" and "0 < u"
+      using y1 \<open>0 < kde\<close> \<open>kde < e\<close> \<open>0 < e\<close> apply (clarsimp simp: in_segment)
+      apply (rule_tac u = "(1 - u)*kde + u*e" in that)
+       apply (auto simp: scaleR_conv_of_real algebra_simps)
+      by (meson le_less_trans less_add_same_cancel2 less_eq_real_def mult_left_mono)
+    moreover
+    obtain v where v: "y = a + of_real v" and "v \<le> 0"
+      using y2 \<open>0 < kde\<close> \<open>0 < d\<close> \<open>0 < e\<close> apply (clarsimp simp: in_segment)
+      apply (rule_tac v = "- ((1 - u)*d + u*kde)" in that)
+       apply (force simp: scaleR_conv_of_real algebra_simps)
+      by (meson less_eq_real_def neg_le_0_iff_le segment_bound_lemma)
+    ultimately show False
+      by auto
+  qed
+  moreover have "a - d \<notin> closed_segment (a + kde) (a + e)"
+    using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>0 < e\<close> by (auto simp: closed_segment_eq_real_ivl)
+  ultimately have sub_a_plus_e:
+    "closed_segment (a + kde) (a + e) \<inter> (path_image p \<union> closed_segment (a - d) (a - kde))
+       \<subseteq> {a + e}"
+    by auto
+  have "kde \<in> closed_segment (-kde) e"
+    using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>kde < e\<close> closed_segment_eq_real_ivl by auto
+  then have a_add_kde: "a + kde \<in> closed_segment (a - kde) (a + e)"
+    using of_real_closed_segment [THEN iffD2]
+    by (force dest: closed_segment_translation_eq [of "a", THEN iffD2] simp del: of_real_closed_segment)
+  have "closed_segment (a - kde) (a + kde) \<inter> closed_segment (a + kde) (a + e) = {a + kde}"
+    by (metis a_add_kde Int_closed_segment)
+  moreover
+  have "path_image p \<inter> closed_segment (a - kde) (a + kde) = {}"
+  proof (rule equals0I, clarify)
+    fix y  assume "y \<in> path_image p" "y \<in> closed_segment (a - kde) (a + kde)"
+    with equals0D [OF k, of y] \<open>0 < kde\<close> \<open>kde < k\<close> show False
+      by (auto simp: dist_norm dest: dist_decreases_closed_segment [where c=a])
+  qed
+  moreover
+  have "- kde \<in> closed_segment (-d) kde"
+    using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>kde < e\<close> closed_segment_eq_real_ivl by auto
+  then have a_diff_kde': "a - kde \<in> closed_segment (a - d) (a + kde)"
+    using of_real_closed_segment [THEN iffD2]
+    by (force dest: closed_segment_translation_eq [of a, THEN iffD2] simp del: of_real_closed_segment)
+  then have "closed_segment (a - d) (a - kde) \<inter> closed_segment (a - kde) (a + kde) = {a - kde}"
+    by (metis Int_closed_segment)
+  ultimately
+  have pa_subset_pm_kde: "path_image ?q \<inter> closed_segment (a - kde) (a + kde) \<subseteq> {a - kde, a + kde}"
+    by (auto simp: path_image_join assms)
+  have ge_kde1: "\<exists>y. x = a + y \<and> y \<ge> kde" if "x \<in> closed_segment (a + kde) (a + e)" for x
+    using that \<open>kde < e\<close> mult_le_cancel_left
+    apply (auto simp: in_segment)
+    apply (rule_tac x="(1-u)*kde + u*e" in exI)
+    apply (fastforce simp: algebra_simps scaleR_conv_of_real)
+    done
+  have ge_kde2: "\<exists>y. x = a + y \<and> y \<le> -kde" if "x \<in> closed_segment (a - d) (a - kde)" for x
+    using that \<open>kde < d\<close> affine_ineq
+    apply (auto simp: in_segment)
+    apply (rule_tac x="- ((1-u)*d + u*kde)" in exI)
+    apply (fastforce simp: algebra_simps scaleR_conv_of_real)
+    done
+  have notin_paq: "x \<notin> path_image ?q" if "dist a x < kde" for x
+    using that using \<open>0 < kde\<close> \<open>kde < d\<close> \<open>kde < k\<close>
+    apply (auto simp: path_image_join assms dist_norm dest!: ge_kde1 ge_kde2)
+    by (meson k disjoint_iff_not_equal le_less_trans less_eq_real_def mem_ball that)
+  obtain z where zin: "z \<in> inside (path_image (?q +++ linepath (a - kde) (a + kde)))"
+           and z1: "cmod (winding_number (?q +++ linepath (a - kde) (a + kde)) z) = 1"
+  proof (rule simple_closed_path_wn1 [of kde ?q a])
+    show "simple_path (?q +++ linepath (a - kde) (a + kde))"
+    proof (intro simple_path_join_loop conjI)
+      show "arc ?q"
+      proof (rule arc_join)
+        show "arc (linepath (a + kde) (a + e))"
+          using \<open>kde < e\<close> \<open>arc p\<close> by (force simp: pfp)
+        show "arc (p +++ linepath (a - d) (a - kde))"
+          using \<open>kde < d\<close> \<open>kde < e\<close> \<open>arc p\<close> sub_a_diff_d by (force simp: pfp intro: arc_join)
+      qed (auto simp: psp pfp path_image_join sub_a_plus_e)
+      show "arc (linepath (a - kde) (a + kde))"
+        using \<open>0 < kde\<close> by auto
+    qed (use pa_subset_pm_kde in auto)
+  qed (use \<open>0 < kde\<close> notin_paq in auto)
+  have eq: "path_image (?q +++ linepath (a - kde) (a + kde)) = path_image (p +++ linepath (a - d) (a + e))"
+            (is "?lhs = ?rhs")
+  proof
+    show "?lhs \<subseteq> ?rhs"
+      using clsub1 clsub2 apply (auto simp: path_image_join assms)
+      by (meson subsetCE subset_closed_segment)
+    show "?rhs \<subseteq> ?lhs"
+      apply (simp add: path_image_join assms Un_ac)
+        by (metis Un_closed_segment Un_assoc a_diff_kde a_diff_kde' le_supI2 subset_refl)
+    qed
+  show thesis
+  proof
+    show zzin: "z \<in> inside (path_image (p +++ linepath (a - d) (a + e)))"
+      by (metis eq zin)
+    then have znotin: "z \<notin> path_image p"
+      by (metis ComplD Un_iff inside_Un_outside path_image_join pathfinish_linepath pathstart_reversepath pfp reversepath_linepath)
+    have znotin_de: "z \<notin> closed_segment (a - d) (a + kde)"
+      by (metis ComplD Un_iff Un_closed_segment a_diff_kde inside_Un_outside path_image_join path_image_linepath pathstart_linepath pfp zzin)
+    have "winding_number (linepath (a - d) (a + e)) z =
+          winding_number (linepath (a - d) (a + kde)) z + winding_number (linepath (a + kde) (a + e)) z"
+      apply (rule winding_number_split_linepath)
+      apply (simp add: a_diff_kde)
+      by (metis ComplD Un_iff inside_Un_outside path_image_join path_image_linepath pathstart_linepath pfp zzin)
+    also have "... = winding_number (linepath (a + kde) (a + e)) z +
+                     (winding_number (linepath (a - d) (a - kde)) z +
+                      winding_number (linepath (a - kde) (a + kde)) z)"
+      by (simp add: winding_number_split_linepath [of "a-kde", symmetric] znotin_de a_diff_kde')
+    finally have "winding_number (p +++ linepath (a - d) (a + e)) z =
+                    winding_number p z + winding_number (linepath (a + kde) (a + e)) z +
+                   (winding_number (linepath (a - d) (a - kde)) z +
+                    winding_number (linepath (a - kde) (a + kde)) z)"
+      by (metis (no_types, lifting) ComplD Un_iff \<open>arc p\<close> add.assoc arc_imp_path eq path_image_join path_join_path_ends path_linepath simple_path_imp_path sp_pl union_with_outside winding_number_join zin)
+    also have "... = winding_number ?q z + winding_number (linepath (a - kde) (a + kde)) z"
+      using \<open>path p\<close> znotin assms zzin clsub1
+      apply (subst winding_number_join, auto)
+      apply (metis (no_types, hide_lams) ComplD Un_iff contra_subsetD inside_Un_outside path_image_join path_image_linepath pathstart_linepath)
+      apply (metis Un_iff Un_closed_segment a_diff_kde' not_in_path_image_join path_image_linepath znotin_de)
+      by (metis Un_iff Un_closed_segment a_diff_kde' path_image_linepath path_linepath pathstart_linepath winding_number_join znotin_de)
+    also have "... = winding_number (?q +++ linepath (a - kde) (a + kde)) z"
+      using \<open>path p\<close> assms zin
+      apply (subst winding_number_join [symmetric], auto)
+      apply (metis ComplD Un_iff path_image_join pathfinish_join pathfinish_linepath pathstart_linepath union_with_outside)
+      by (metis Un_iff Un_closed_segment a_diff_kde' znotin_de)
+    finally have "winding_number (p +++ linepath (a - d) (a + e)) z =
+                  winding_number (?q +++ linepath (a - kde) (a + kde)) z" .
+    then show "cmod (winding_number (p +++ linepath (a - d) (a + e)) z) = 1"
+      by (simp add: z1)
+  qed
+qed
+
+lemma simple_closed_path_wn3:
+  fixes p :: "real \<Rightarrow> complex"
+  assumes "simple_path p" and loop: "pathfinish p = pathstart p"
+  obtains z where "z \<in> inside (path_image p)" "cmod (winding_number p z) = 1"
+proof -
+  have ins: "inside(path_image p) \<noteq> {}" "open(inside(path_image p))"
+            "connected(inside(path_image p))"
+   and out: "outside(path_image p) \<noteq> {}" "open(outside(path_image p))"
+            "connected(outside(path_image p))"
+   and bo:  "bounded(inside(path_image p))" "\<not> bounded(outside(path_image p))"
+   and ins_out: "inside(path_image p) \<inter> outside(path_image p) = {}"
+                "inside(path_image p) \<union> outside(path_image p) = - path_image p"
+   and fro: "frontier(inside(path_image p)) = path_image p"
+            "frontier(outside(path_image p)) = path_image p"
+    using Jordan_inside_outside [OF assms] by auto
+  obtain a where a: "a \<in> inside(path_image p)"
+    using \<open>inside (path_image p) \<noteq> {}\<close> by blast
+  obtain d::real where "0 < d" and d_fro: "a - d \<in> frontier (inside (path_image p))"
+                 and d_int: "\<And>\<epsilon>. \<lbrakk>0 \<le> \<epsilon>; \<epsilon> < d\<rbrakk> \<Longrightarrow> (a - \<epsilon>) \<in> inside (path_image p)"
+    apply (rule ray_to_frontier [of "inside (path_image p)" a "-1"])
+    using \<open>bounded (inside (path_image p))\<close> \<open>open (inside (path_image p))\<close> a interior_eq
+       apply (auto simp: of_real_def)
+    done
+  obtain e::real where "0 < e" and e_fro: "a + e \<in> frontier (inside (path_image p))"
+    and e_int: "\<And>\<epsilon>. \<lbrakk>0 \<le> \<epsilon>; \<epsilon> < e\<rbrakk> \<Longrightarrow> (a + \<epsilon>) \<in> inside (path_image p)"
+    apply (rule ray_to_frontier [of "inside (path_image p)" a 1])
+    using \<open>bounded (inside (path_image p))\<close> \<open>open (inside (path_image p))\<close> a interior_eq
+       apply (auto simp: of_real_def)
+    done
+  obtain t0 where "0 \<le> t0" "t0 \<le> 1" and pt: "p t0 = a - d"
+    using a d_fro fro by (auto simp: path_image_def)
+  obtain q where "simple_path q" and q_ends: "pathstart q = a - d" "pathfinish q = a - d"
+    and q_eq_p: "path_image q = path_image p"
+    and wn_q_eq_wn_p: "\<And>z. z \<in> inside(path_image p) \<Longrightarrow> winding_number q z = winding_number p z"
+  proof
+    show "simple_path (shiftpath t0 p)"
+      by (simp add: pathstart_shiftpath pathfinish_shiftpath
+          simple_path_shiftpath path_image_shiftpath \<open>0 \<le> t0\<close> \<open>t0 \<le> 1\<close> assms)
+    show "pathstart (shiftpath t0 p) = a - d"
+      using pt by (simp add: \<open>t0 \<le> 1\<close> pathstart_shiftpath)
+    show "pathfinish (shiftpath t0 p) = a - d"
+      by (simp add: \<open>0 \<le> t0\<close> loop pathfinish_shiftpath pt)
+    show "path_image (shiftpath t0 p) = path_image p"
+      by (simp add: \<open>0 \<le> t0\<close> \<open>t0 \<le> 1\<close> loop path_image_shiftpath)
+    show "winding_number (shiftpath t0 p) z = winding_number p z"
+      if "z \<in> inside (path_image p)" for z
+      by (metis ComplD Un_iff \<open>0 \<le> t0\<close> \<open>t0 \<le> 1\<close> \<open>simple_path p\<close> atLeastAtMost_iff inside_Un_outside
+          loop simple_path_imp_path that winding_number_shiftpath)
+  qed
+  have ad_not_ae: "a - d \<noteq> a + e"
+    by (metis \<open>0 < d\<close> \<open>0 < e\<close> add.left_inverse add_left_cancel add_uminus_conv_diff
+        le_add_same_cancel2 less_eq_real_def not_less of_real_add of_real_def of_real_eq_0_iff pt)
+  have ad_ae_q: "{a - d, a + e} \<subseteq> path_image q"
+    using \<open>path_image q = path_image p\<close> d_fro e_fro fro(1) by auto
+  have ada: "open_segment (a - d) a \<subseteq> inside (path_image p)"
+  proof (clarsimp simp: in_segment)
+    fix u::real assume "0 < u" "u < 1"
+    with d_int have "a - (1 - u) * d \<in> inside (path_image p)"
+      by (metis \<open>0 < d\<close> add.commute diff_add_cancel left_diff_distrib' less_add_same_cancel2 less_eq_real_def mult.left_neutral zero_less_mult_iff)
+    then show "(1 - u) *\<^sub>R (a - d) + u *\<^sub>R a \<in> inside (path_image p)"
+      by (simp add: diff_add_eq of_real_def real_vector.scale_right_diff_distrib)
+  qed
+  have aae: "open_segment a (a + e) \<subseteq> inside (path_image p)"
+  proof (clarsimp simp: in_segment)
+    fix u::real assume "0 < u" "u < 1"
+    with e_int have "a + u * e \<in> inside (path_image p)"
+      by (meson \<open>0 < e\<close> less_eq_real_def mult_less_cancel_right2 not_less zero_less_mult_iff)
+    then show "(1 - u) *\<^sub>R a + u *\<^sub>R (a + e) \<in> inside (path_image p)"
+      apply (simp add: algebra_simps)
+      by (simp add: diff_add_eq of_real_def real_vector.scale_right_diff_distrib)
+  qed
+  have "complex_of_real (d * d + (e * e + d * (e + e))) \<noteq> 0"
+    using ad_not_ae
+    by (metis \<open>0 < d\<close> \<open>0 < e\<close> add_strict_left_mono less_add_same_cancel1 not_sum_squares_lt_zero
+        of_real_eq_0_iff zero_less_double_add_iff_zero_less_single_add zero_less_mult_iff)
+  then have a_in_de: "a \<in> open_segment (a - d) (a + e)"
+    using ad_not_ae \<open>0 < d\<close> \<open>0 < e\<close>
+    apply (auto simp: in_segment algebra_simps scaleR_conv_of_real)
+    apply (rule_tac x="d / (d+e)" in exI)
+    apply (auto simp: field_simps)
+    done
+  then have "open_segment (a - d) (a + e) \<subseteq> inside (path_image p)"
+    using ada a aae Un_open_segment [of a "a-d" "a+e"] by blast
+  then have "path_image q \<inter> open_segment (a - d) (a + e) = {}"
+    using inside_no_overlap by (fastforce simp: q_eq_p)
+  with ad_ae_q have paq_Int_cs: "path_image q \<inter> closed_segment (a - d) (a + e) = {a - d, a + e}"
+    by (simp add: closed_segment_eq_open)
+  obtain t where "0 \<le> t" "t \<le> 1" and qt: "q t = a + e"
+    using a e_fro fro ad_ae_q by (auto simp: path_defs)
+  then have "t \<noteq> 0"
+    by (metis ad_not_ae pathstart_def q_ends(1))
+  then have "t \<noteq> 1"
+    by (metis ad_not_ae pathfinish_def q_ends(2) qt)
+  have q01: "q 0 = a - d" "q 1 = a - d"
+    using q_ends by (auto simp: pathstart_def pathfinish_def)
+  obtain z where zin: "z \<in> inside (path_image (subpath t 0 q +++ linepath (a - d) (a + e)))"
+             and z1: "cmod (winding_number (subpath t 0 q +++ linepath (a - d) (a + e)) z) = 1"
+  proof (rule simple_closed_path_wn2 [of d e "subpath t 0 q" a], simp_all add: q01)
+    show "simple_path (subpath t 0 q +++ linepath (a - d) (a + e))"
+    proof (rule simple_path_join_loop, simp_all add: qt q01)
+      have "inj_on q (closed_segment t 0)"
+        using \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close> \<open>t \<noteq> 0\<close> \<open>t \<noteq> 1\<close>
+        by (fastforce simp: simple_path_def inj_on_def closed_segment_eq_real_ivl)
+      then show "arc (subpath t 0 q)"
+        using \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close> \<open>t \<noteq> 0\<close>
+        by (simp add: arc_subpath_eq simple_path_imp_path)
+      show "arc (linepath (a - d) (a + e))"
+        by (simp add: ad_not_ae)
+      show "path_image (subpath t 0 q) \<inter> closed_segment (a - d) (a + e) \<subseteq> {a + e, a - d}"
+        using qt paq_Int_cs  \<open>simple_path q\<close> \<open>0 \<le> t\<close> \<open>t \<le> 1\<close>
+        by (force simp: dest: rev_subsetD [OF _ path_image_subpath_subset] intro: simple_path_imp_path)
+    qed
+  qed (auto simp: \<open>0 < d\<close> \<open>0 < e\<close> qt)
+  have pa01_Un: "path_image (subpath 0 t q) \<union> path_image (subpath 1 t q) = path_image q"
+    unfolding path_image_subpath
+    using \<open>0 \<le> t\<close> \<open>t \<le> 1\<close> by (force simp: path_image_def image_iff)
+  with paq_Int_cs have pa_01q:
+        "(path_image (subpath 0 t q) \<union> path_image (subpath 1 t q)) \<inter> closed_segment (a - d) (a + e) = {a - d, a + e}"
+    by metis
+  have z_notin_ed: "z \<notin> closed_segment (a + e) (a - d)"
+    using zin q01 by (simp add: path_image_join closed_segment_commute inside_def)
+  have z_notin_0t: "z \<notin> path_image (subpath 0 t q)"
+    by (metis (no_types, hide_lams) IntI Un_upper1 subsetD empty_iff inside_no_overlap path_image_join
+        path_image_subpath_commute pathfinish_subpath pathstart_def pathstart_linepath q_ends(1) qt subpath_trivial zin)
+  have [simp]: "- winding_number (subpath t 0 q) z = winding_number (subpath 0 t q) z"
+    by (metis \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close> atLeastAtMost_iff zero_le_one
+              path_image_subpath_commute path_subpath real_eq_0_iff_le_ge_0
+              reversepath_subpath simple_path_imp_path winding_number_reversepath z_notin_0t)
+  obtain z_in_q: "z \<in> inside(path_image q)"
+     and wn_q: "winding_number (subpath 0 t q +++ subpath t 1 q) z = - winding_number (subpath t 0 q +++ linepath (a - d) (a + e)) z"
+  proof (rule winding_number_from_innerpath
+          [of "subpath 0 t q" "a-d" "a+e" "subpath 1 t q" "linepath (a - d) (a + e)"
+            z "- winding_number (subpath t 0 q +++ linepath (a - d) (a + e)) z"],
+         simp_all add: q01 qt pa01_Un reversepath_subpath)
+    show "simple_path (subpath 0 t q)" "simple_path (subpath 1 t q)"
+      by (simp_all add: \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close> \<open>t \<noteq> 0\<close> \<open>t \<noteq> 1\<close> simple_path_subpath)
+    show "simple_path (linepath (a - d) (a + e))"
+      using ad_not_ae by blast
+    show "path_image (subpath 0 t q) \<inter> path_image (subpath 1 t q) = {a - d, a + e}"  (is "?lhs = ?rhs")
+    proof
+      show "?lhs \<subseteq> ?rhs"
+        using \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close> \<open>t \<noteq> 1\<close> q_ends qt q01
+        by (force simp: pathfinish_def qt simple_path_def path_image_subpath)
+      show "?rhs \<subseteq> ?lhs"
+        using \<open>0 \<le> t\<close> \<open>t \<le> 1\<close> q01 qt by (force simp: path_image_subpath)
+    qed
+    show "path_image (subpath 0 t q) \<inter> closed_segment (a - d) (a + e) = {a - d, a + e}" (is "?lhs = ?rhs")
+    proof
+      show "?lhs \<subseteq> ?rhs"  using paq_Int_cs pa01_Un by fastforce
+      show "?rhs \<subseteq> ?lhs"  using \<open>0 \<le> t\<close> \<open>t \<le> 1\<close> q01 qt by (force simp: path_image_subpath)
+    qed
+    show "path_image (subpath 1 t q) \<inter> closed_segment (a - d) (a + e) = {a - d, a + e}" (is "?lhs = ?rhs")
+    proof
+      show "?lhs \<subseteq> ?rhs"  by (auto simp: pa_01q [symmetric])
+      show "?rhs \<subseteq> ?lhs"  using \<open>0 \<le> t\<close> \<open>t \<le> 1\<close> q01 qt by (force simp: path_image_subpath)
+    qed
+    show "closed_segment (a - d) (a + e) \<inter> inside (path_image q) \<noteq> {}"
+      using a a_in_de open_closed_segment pa01_Un q_eq_p by fastforce
+    show "z \<in> inside (path_image (subpath 0 t q) \<union> closed_segment (a - d) (a + e))"
+      by (metis path_image_join path_image_linepath path_image_subpath_commute pathfinish_subpath pathstart_linepath q01(1) zin)
+    show "winding_number (subpath 0 t q +++ linepath (a + e) (a - d)) z =
+      - winding_number (subpath t 0 q +++ linepath (a - d) (a + e)) z"
+      using z_notin_ed z_notin_0t \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close>
+      by (simp add: simple_path_imp_path qt q01 path_image_subpath_commute closed_segment_commute winding_number_join winding_number_reversepath [symmetric])
+    show "- d \<noteq> e"
+      using ad_not_ae by auto
+    show "winding_number (subpath t 0 q +++ linepath (a - d) (a + e)) z \<noteq> 0"
+      using z1 by auto
+  qed
+  show ?thesis
+  proof
+    show "z \<in> inside (path_image p)"
+      using q_eq_p z_in_q by auto
+    then have [simp]: "z \<notin> path_image q"
+      by (metis disjoint_iff_not_equal inside_no_overlap q_eq_p)
+    have [simp]: "z \<notin> path_image (subpath 1 t q)"
+      using inside_def pa01_Un z_in_q by fastforce
+    have "winding_number(subpath 0 t q +++ subpath t 1 q) z = winding_number(subpath 0 1 q) z"
+      using z_notin_0t \<open>0 \<le> t\<close> \<open>simple_path q\<close> \<open>t \<le> 1\<close>
+      by (simp add: simple_path_imp_path qt path_image_subpath_commute winding_number_join winding_number_subpath_combine)
+    with wn_q have "winding_number (subpath t 0 q +++ linepath (a - d) (a + e)) z = - winding_number q z"
+      by auto
+    with z1 have "cmod (winding_number q z) = 1"
+      by simp
+    with z1 wn_q_eq_wn_p show "cmod (winding_number p z) = 1"
+      using z1 wn_q_eq_wn_p  by (simp add: \<open>z \<in> inside (path_image p)\<close>)
+    qed
+qed
+
+proposition simple_closed_path_winding_number_inside:
+  assumes "simple_path \<gamma>"
+  obtains "\<And>z. z \<in> inside(path_image \<gamma>) \<Longrightarrow> winding_number \<gamma> z = 1"
+        | "\<And>z. z \<in> inside(path_image \<gamma>) \<Longrightarrow> winding_number \<gamma> z = -1"
+proof (cases "pathfinish \<gamma> = pathstart \<gamma>")
+  case True
+  have "path \<gamma>"
+    by (simp add: assms simple_path_imp_path)
+  then have const: "winding_number \<gamma> constant_on inside(path_image \<gamma>)"
+  proof (rule winding_number_constant)
+    show "connected (inside(path_image \<gamma>))"
+      by (simp add: Jordan_inside_outside True assms)
+  qed (use inside_no_overlap True in auto)
+  obtain z where zin: "z \<in> inside (path_image \<gamma>)" and z1: "cmod (winding_number \<gamma> z) = 1"
+    using simple_closed_path_wn3 [of \<gamma>] True assms by blast
+  have "winding_number \<gamma> z \<in> \<int>"
+    using zin integer_winding_number [OF \<open>path \<gamma>\<close> True] inside_def by blast
+  with z1 consider "winding_number \<gamma> z = 1" | "winding_number \<gamma> z = -1"
+    apply (auto simp: Ints_def abs_if split: if_split_asm)
+    by (metis of_int_1 of_int_eq_iff of_int_minus)
+  with that const zin show ?thesis
+    unfolding constant_on_def by metis
+next
+  case False
+  then show ?thesis
+    using inside_simple_curve_imp_closed assms that(2) by blast
+qed
+
+lemma simple_closed_path_abs_winding_number_inside:
+  assumes "simple_path \<gamma>" "z \<in> inside(path_image \<gamma>)"
+    shows "\<bar>Re (winding_number \<gamma> z)\<bar> = 1"
+  by (metis assms norm_minus_cancel norm_one one_complex.simps(1) real_norm_def simple_closed_path_winding_number_inside uminus_complex.simps(1))
+
+lemma simple_closed_path_norm_winding_number_inside:
+  assumes "simple_path \<gamma>" "z \<in> inside(path_image \<gamma>)"
+  shows "norm (winding_number \<gamma> z) = 1"
+proof -
+  have "pathfinish \<gamma> = pathstart \<gamma>"
+    using assms inside_simple_curve_imp_closed by blast
+  with assms integer_winding_number have "winding_number \<gamma> z \<in> \<int>"
+    by (simp add: inside_def simple_path_def)
+  then show ?thesis
+    by (metis assms norm_minus_cancel norm_one simple_closed_path_winding_number_inside)
+qed
+
+lemma simple_closed_path_winding_number_cases:
+   "\<lbrakk>simple_path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> path_image \<gamma>\<rbrakk> \<Longrightarrow> winding_number \<gamma> z \<in> {-1,0,1}"
+apply (simp add: inside_Un_outside [of "path_image \<gamma>", symmetric, unfolded set_eq_iff Set.Compl_iff] del: inside_Un_outside)
+   apply (rule simple_closed_path_winding_number_inside)
+  using simple_path_def winding_number_zero_in_outside by blast+
+
+lemma simple_closed_path_winding_number_pos:
+   "\<lbrakk>simple_path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> path_image \<gamma>; 0 < Re(winding_number \<gamma> z)\<rbrakk>
+    \<Longrightarrow> winding_number \<gamma> z = 1"
+using simple_closed_path_winding_number_cases
+  by fastforce
+
+subsection \<open>Winding number for rectangular paths\<close>
+
+proposition winding_number_rectpath:
+  assumes "z \<in> box a1 a3"
+  shows   "winding_number (rectpath a1 a3) z = 1"
+proof -
+  from assms have less: "Re a1 < Re a3" "Im a1 < Im a3"
+    by (auto simp: in_box_complex_iff)
+  define a2 a4 where "a2 = Complex (Re a3) (Im a1)" and "a4 = Complex (Re a1) (Im a3)"
+  let ?l1 = "linepath a1 a2" and ?l2 = "linepath a2 a3"
+  and ?l3 = "linepath a3 a4" and ?l4 = "linepath a4 a1"
+  from assms and less have "z \<notin> path_image (rectpath a1 a3)"
+    by (auto simp: path_image_rectpath_cbox_minus_box)
+  also have "path_image (rectpath a1 a3) =
+               path_image ?l1 \<union> path_image ?l2 \<union> path_image ?l3 \<union> path_image ?l4"
+    by (simp add: rectpath_def Let_def path_image_join Un_assoc a2_def a4_def)
+  finally have "z \<notin> \<dots>" .
+  moreover have "\<forall>l\<in>{?l1,?l2,?l3,?l4}. Re (winding_number l z) > 0"
+    unfolding ball_simps HOL.simp_thms a2_def a4_def
+    by (intro conjI; (rule winding_number_linepath_pos_lt;
+          (insert assms, auto simp: a2_def a4_def in_box_complex_iff mult_neg_neg))+)
+  ultimately have "Re (winding_number (rectpath a1 a3) z) > 0"
+    by (simp add: winding_number_join path_image_join rectpath_def Let_def a2_def a4_def)
+  thus "winding_number (rectpath a1 a3) z = 1" using assms less
+    by (intro simple_closed_path_winding_number_pos simple_path_rectpath)
+       (auto simp: path_image_rectpath_cbox_minus_box)
+qed
+
+proposition winding_number_rectpath_outside:
+  assumes "Re a1 \<le> Re a3" "Im a1 \<le> Im a3"
+  assumes "z \<notin> cbox a1 a3"
+  shows   "winding_number (rectpath a1 a3) z = 0"
+  using assms by (intro winding_number_zero_outside[OF _ _ _ assms(3)]
+                     path_image_rectpath_subset_cbox) simp_all
+
+text\<open>A per-function version for continuous logs, a kind of monodromy\<close>
+proposition\<^marker>\<open>tag unimportant\<close> winding_number_compose_exp:
+  assumes "path p"
+  shows "winding_number (exp \<circ> p) 0 = (pathfinish p - pathstart p) / (2 * of_real pi * \<i>)"
+proof -
+  obtain e where "0 < e" and e: "\<And>t. t \<in> {0..1} \<Longrightarrow> e \<le> norm(exp(p t))"
+  proof
+    have "closed (path_image (exp \<circ> p))"
+      by (simp add: assms closed_path_image holomorphic_on_exp holomorphic_on_imp_continuous_on path_continuous_image)
+    then show "0 < setdist {0} (path_image (exp \<circ> p))"
+      by (metis exp_not_eq_zero imageE image_comp infdist_eq_setdist infdist_pos_not_in_closed path_defs(4) path_image_nonempty)
+  next
+    fix t::real
+    assume "t \<in> {0..1}"
+    have "setdist {0} (path_image (exp \<circ> p)) \<le> dist 0 (exp (p t))"
+      apply (rule setdist_le_dist)
+      using \<open>t \<in> {0..1}\<close> path_image_def by fastforce+
+    then show "setdist {0} (path_image (exp \<circ> p)) \<le> cmod (exp (p t))"
+      by simp
+  qed
+  have "bounded (path_image p)"
+    by (simp add: assms bounded_path_image)
+  then obtain B where "0 < B" and B: "path_image p \<subseteq> cball 0 B"
+    by (meson bounded_pos mem_cball_0 subsetI)
+  let ?B = "cball (0::complex) (B+1)"
+  have "uniformly_continuous_on ?B exp"
+    using holomorphic_on_exp holomorphic_on_imp_continuous_on
+    by (force intro: compact_uniformly_continuous)
+  then obtain d where "d > 0"
+        and d: "\<And>x x'. \<lbrakk>x\<in>?B; x'\<in>?B; dist x' x < d\<rbrakk> \<Longrightarrow> norm (exp x' - exp x) < e"
+    using \<open>e > 0\<close> by (auto simp: uniformly_continuous_on_def dist_norm)
+  then have "min 1 d > 0"
+    by force
+  then obtain g where pfg: "polynomial_function g"  and "g 0 = p 0" "g 1 = p 1"
+           and gless: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm(g t - p t) < min 1 d"
+    using path_approx_polynomial_function [OF \<open>path p\<close>] \<open>d > 0\<close> \<open>0 < e\<close>
+    unfolding pathfinish_def pathstart_def by meson
+  have "winding_number (exp \<circ> p) 0 = winding_number (exp \<circ> g) 0"
+  proof (rule winding_number_nearby_paths_eq [symmetric])
+    show "path (exp \<circ> p)" "path (exp \<circ> g)"
+      by (simp_all add: pfg assms holomorphic_on_exp holomorphic_on_imp_continuous_on path_continuous_image path_polynomial_function)
+  next
+    fix t :: "real"
+    assume t: "t \<in> {0..1}"
+    with gless have "norm(g t - p t) < 1"
+      using min_less_iff_conj by blast
+    moreover have ptB: "norm (p t) \<le> B"
+      using B t by (force simp: path_image_def)
+    ultimately have "cmod (g t) \<le> B + 1"
+      by (meson add_mono_thms_linordered_field(4) le_less_trans less_imp_le norm_triangle_sub)
+    with ptB gless t have "cmod ((exp \<circ> g) t - (exp \<circ> p) t) < e"
+      by (auto simp: dist_norm d)
+    with e t show "cmod ((exp \<circ> g) t - (exp \<circ> p) t) < cmod ((exp \<circ> p) t - 0)"
+      by fastforce
+  qed (use \<open>g 0 = p 0\<close> \<open>g 1 = p 1\<close> in \<open>auto simp: pathfinish_def pathstart_def\<close>)
+  also have "... = 1 / (of_real (2 * pi) * \<i>) * contour_integral (exp \<circ> g) (\<lambda>w. 1 / (w - 0))"
+  proof (rule winding_number_valid_path)
+    have "continuous_on (path_image g) (deriv exp)"
+      by (metis DERIV_exp DERIV_imp_deriv continuous_on_cong holomorphic_on_exp holomorphic_on_imp_continuous_on)
+    then show "valid_path (exp \<circ> g)"
+      by (simp add: field_differentiable_within_exp pfg valid_path_compose valid_path_polynomial_function)
+    show "0 \<notin> path_image (exp \<circ> g)"
+      by (auto simp: path_image_def)
+  qed
+  also have "... = 1 / (of_real (2 * pi) * \<i>) * integral {0..1} (\<lambda>x. vector_derivative g (at x))"
+  proof (simp add: contour_integral_integral, rule integral_cong)
+    fix t :: "real"
+    assume t: "t \<in> {0..1}"
+    show "vector_derivative (exp \<circ> g) (at t) / exp (g t) = vector_derivative g (at t)"
+    proof -
+      have "(exp \<circ> g has_vector_derivative vector_derivative (exp \<circ> g) (at t)) (at t)"
+        by (meson DERIV_exp differentiable_def field_vector_diff_chain_at has_vector_derivative_def
+            has_vector_derivative_polynomial_function pfg vector_derivative_works)
+      moreover have "(exp \<circ> g has_vector_derivative vector_derivative g (at t) * exp (g t)) (at t)"
+        apply (rule field_vector_diff_chain_at)
+        apply (metis has_vector_derivative_polynomial_function pfg vector_derivative_at)
+        using DERIV_exp has_field_derivative_def apply blast
+        done
+      ultimately show ?thesis
+        by (simp add: divide_simps, rule vector_derivative_unique_at)
+    qed
+  qed
+  also have "... = (pathfinish p - pathstart p) / (2 * of_real pi * \<i>)"
+  proof -
+    have "((\<lambda>x. vector_derivative g (at x)) has_integral g 1 - g 0) {0..1}"
+      apply (rule fundamental_theorem_of_calculus [OF zero_le_one])
+      by (metis has_vector_derivative_at_within has_vector_derivative_polynomial_function pfg vector_derivative_at)
+    then show ?thesis
+    apply (simp add: pathfinish_def pathstart_def)
+      using \<open>g 0 = p 0\<close> \<open>g 1 = p 1\<close> by auto
+  qed
+  finally show ?thesis .
+qed
+
+subsection\<^marker>\<open>tag unimportant\<close> \<open>The winding number defines a continuous logarithm for the path itself\<close>
+
+lemma winding_number_as_continuous_log:
+  assumes "path p" and \<zeta>: "\<zeta> \<notin> path_image p"
+  obtains q where "path q"
+                  "pathfinish q - pathstart q = 2 * of_real pi * \<i> * winding_number p \<zeta>"
+                  "\<And>t. t \<in> {0..1} \<Longrightarrow> p t = \<zeta> + exp(q t)"
+proof -
+  let ?q = "\<lambda>t. 2 * of_real pi * \<i> * winding_number(subpath 0 t p) \<zeta> + Ln(pathstart p - \<zeta>)"
+  show ?thesis
+  proof
+    have *: "continuous (at t within {0..1}) (\<lambda>x. winding_number (subpath 0 x p) \<zeta>)"
+      if t: "t \<in> {0..1}" for t
+    proof -
+      let ?B = "ball (p t) (norm(p t - \<zeta>))"
+      have "p t \<noteq> \<zeta>"
+        using path_image_def that \<zeta> by blast
+      then have "simply_connected ?B"
+        by (simp add: convex_imp_simply_connected)
+      then have "\<And>f::complex\<Rightarrow>complex. continuous_on ?B f \<and> (\<forall>\<zeta> \<in> ?B. f \<zeta> \<noteq> 0)
+                  \<longrightarrow> (\<exists>g. continuous_on ?B g \<and> (\<forall>\<zeta> \<in> ?B. f \<zeta> = exp (g \<zeta>)))"
+        by (simp add: simply_connected_eq_continuous_log)
+      moreover have "continuous_on ?B (\<lambda>w. w - \<zeta>)"
+        by (intro continuous_intros)
+      moreover have "(\<forall>z \<in> ?B. z - \<zeta> \<noteq> 0)"
+        by (auto simp: dist_norm)
+      ultimately obtain g where contg: "continuous_on ?B g"
+        and geq: "\<And>z. z \<in> ?B \<Longrightarrow> z - \<zeta> = exp (g z)" by blast
+      obtain d where "0 < d" and d:
+        "\<And>x. \<lbrakk>x \<in> {0..1}; dist x t < d\<rbrakk> \<Longrightarrow> dist (p x) (p t) < cmod (p t - \<zeta>)"
+        using \<open>path p\<close> t unfolding path_def continuous_on_iff
+        by (metis \<open>p t \<noteq> \<zeta>\<close> right_minus_eq zero_less_norm_iff)
+      have "((\<lambda>x. winding_number (\<lambda>w. subpath 0 x p w - \<zeta>) 0 -
+                  winding_number (\<lambda>w. subpath 0 t p w - \<zeta>) 0) \<longlongrightarrow> 0)
+            (at t within {0..1})"
+      proof (rule Lim_transform_within [OF _ \<open>d > 0\<close>])
+        have "continuous (at t within {0..1}) (g o p)"
+        proof (rule continuous_within_compose)
+          show "continuous (at t within {0..1}) p"
+            using \<open>path p\<close> continuous_on_eq_continuous_within path_def that by blast
+          show "continuous (at (p t) within p ` {0..1}) g"
+            by (metis (no_types, lifting) open_ball UNIV_I \<open>p t \<noteq> \<zeta>\<close> centre_in_ball contg continuous_on_eq_continuous_at continuous_within_topological right_minus_eq zero_less_norm_iff)
+        qed
+        with LIM_zero have "((\<lambda>u. (g (subpath t u p 1) - g (subpath t u p 0))) \<longlongrightarrow> 0) (at t within {0..1})"
+          by (auto simp: subpath_def continuous_within o_def)
+        then show "((\<lambda>u.  (g (subpath t u p 1) - g (subpath t u p 0)) / (2 * of_real pi * \<i>)) \<longlongrightarrow> 0)
+           (at t within {0..1})"
+          by (simp add: tendsto_divide_zero)
+        show "(g (subpath t u p 1) - g (subpath t u p 0)) / (2 * of_real pi * \<i>) =
+              winding_number (\<lambda>w. subpath 0 u p w - \<zeta>) 0 - winding_number (\<lambda>w. subpath 0 t p w - \<zeta>) 0"
+          if "u \<in> {0..1}" "0 < dist u t" "dist u t < d" for u
+        proof -
+          have "closed_segment t u \<subseteq> {0..1}"
+            using closed_segment_eq_real_ivl t that by auto
+          then have piB: "path_image(subpath t u p) \<subseteq> ?B"
+            apply (clarsimp simp add: path_image_subpath_gen)
+            by (metis subsetD le_less_trans \<open>dist u t < d\<close> d dist_commute dist_in_closed_segment)
+          have *: "path (g \<circ> subpath t u p)"
+            apply (rule path_continuous_image)
+            using \<open>path p\<close> t that apply auto[1]
+            using piB contg continuous_on_subset by blast
+          have "(g (subpath t u p 1) - g (subpath t u p 0)) / (2 * of_real pi * \<i>)
+              =  winding_number (exp \<circ> g \<circ> subpath t u p) 0"
+            using winding_number_compose_exp [OF *]
+            by (simp add: pathfinish_def pathstart_def o_assoc)
+          also have "... = winding_number (\<lambda>w. subpath t u p w - \<zeta>) 0"
+          proof (rule winding_number_cong)
+            have "exp(g y) = y - \<zeta>" if "y \<in> (subpath t u p) ` {0..1}" for y
+              by (metis that geq path_image_def piB subset_eq)
+            then show "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> (exp \<circ> g \<circ> subpath t u p) x = subpath t u p x - \<zeta>"
+              by auto
+          qed
+          also have "... = winding_number (\<lambda>w. subpath 0 u p w - \<zeta>) 0 -
+                           winding_number (\<lambda>w. subpath 0 t p w - \<zeta>) 0"
+            apply (simp add: winding_number_offset [symmetric])
+            using winding_number_subpath_combine [OF \<open>path p\<close> \<zeta>, of 0 t u] \<open>t \<in> {0..1}\<close> \<open>u \<in> {0..1}\<close>
+            by (simp add: add.commute eq_diff_eq)
+          finally show ?thesis .
+        qed
+      qed
+      then show ?thesis
+        by (subst winding_number_offset) (simp add: continuous_within LIM_zero_iff)
+    qed
+    show "path ?q"
+      unfolding path_def
+      by (intro continuous_intros) (simp add: continuous_on_eq_continuous_within *)
+
+    have "\<zeta> \<noteq> p 0"
+      by (metis \<zeta> pathstart_def pathstart_in_path_image)
+    then show "pathfinish ?q - pathstart ?q = 2 * of_real pi * \<i> * winding_number p \<zeta>"
+      by (simp add: pathfinish_def pathstart_def)
+    show "p t = \<zeta> + exp (?q t)" if "t \<in> {0..1}" for t
+    proof -
+      have "path (subpath 0 t p)"
+        using \<open>path p\<close> that by auto
+      moreover
+      have "\<zeta> \<notin> path_image (subpath 0 t p)"
+        using \<zeta> [unfolded path_image_def] that by (auto simp: path_image_subpath)
+      ultimately show ?thesis
+        using winding_number_exp_2pi [of "subpath 0 t p" \<zeta>] \<open>\<zeta> \<noteq> p 0\<close>
+        by (auto simp: exp_add algebra_simps pathfinish_def pathstart_def subpath_def)
+    qed
+  qed
+qed
+
+subsection \<open>Winding number equality is the same as path/loop homotopy in C - {0}\<close>
+
+lemma winding_number_homotopic_loops_null_eq:
+  assumes "path p" and \<zeta>: "\<zeta> \<notin> path_image p"
+  shows "winding_number p \<zeta> = 0 \<longleftrightarrow> (\<exists>a. homotopic_loops (-{\<zeta>}) p (\<lambda>t. a))"
+    (is "?lhs = ?rhs")
+proof
+  assume [simp]: ?lhs
+  obtain q where "path q"
+             and qeq:  "pathfinish q - pathstart q = 2 * of_real pi * \<i> * winding_number p \<zeta>"
+             and peq: "\<And>t. t \<in> {0..1} \<Longrightarrow> p t = \<zeta> + exp(q t)"
+    using winding_number_as_continuous_log [OF assms] by blast
+  have *: "homotopic_with_canon (\<lambda>r. pathfinish r = pathstart r)
+                       {0..1} (-{\<zeta>}) ((\<lambda>w. \<zeta> + exp w) \<circ> q) ((\<lambda>w. \<zeta> + exp w) \<circ> (\<lambda>t. 0))"
+  proof (rule homotopic_with_compose_continuous_left)
+    show "homotopic_with_canon (\<lambda>f. pathfinish ((\<lambda>w. \<zeta> + exp w) \<circ> f) = pathstart ((\<lambda>w. \<zeta> + exp w) \<circ> f))
+              {0..1} UNIV q (\<lambda>t. 0)"
+    proof (rule homotopic_with_mono, simp_all add: pathfinish_def pathstart_def)
+      have "homotopic_loops UNIV q (\<lambda>t. 0)"
+        by (rule homotopic_loops_linear) (use qeq \<open>path q\<close> in \<open>auto simp: path_defs\<close>)
+      then have "homotopic_with (\<lambda>r. r 1 = r 0) (top_of_set {0..1}) euclidean q (\<lambda>t. 0)"
+        by (simp add: homotopic_loops_def pathfinish_def pathstart_def)
+      then show "homotopic_with (\<lambda>h. exp (h 1) = exp (h 0)) (top_of_set {0..1}) euclidean q (\<lambda>t. 0)"
+        by (rule homotopic_with_mono) simp
+    qed
+    show "continuous_on UNIV (\<lambda>w. \<zeta> + exp w)"
+      by (rule continuous_intros)+
+    show "range (\<lambda>w. \<zeta> + exp w) \<subseteq> -{\<zeta>}"
+      by auto
+  qed
+  then have "homotopic_with_canon (\<lambda>r. pathfinish r = pathstart r) {0..1} (-{\<zeta>}) p (\<lambda>x. \<zeta> + 1)"
+    by (rule homotopic_with_eq) (auto simp: o_def peq pathfinish_def pathstart_def)
+  then have "homotopic_loops (-{\<zeta>}) p (\<lambda>t. \<zeta> + 1)"
+    by (simp add: homotopic_loops_def)
+  then show ?rhs ..
+next
+  assume ?rhs
+  then obtain a where "homotopic_loops (-{\<zeta>}) p (\<lambda>t. a)" ..
+  then have "winding_number p \<zeta> = winding_number (\<lambda>t. a) \<zeta>" "a \<noteq> \<zeta>"
+    using winding_number_homotopic_loops homotopic_loops_imp_subset by (force simp:)+
+  moreover have "winding_number (\<lambda>t. a) \<zeta> = 0"
+    by (metis winding_number_zero_const \<open>a \<noteq> \<zeta>\<close>)
+  ultimately show ?lhs by metis
+qed
+
+lemma winding_number_homotopic_paths_null_explicit_eq:
+  assumes "path p" and \<zeta>: "\<zeta> \<notin> path_image p"
+  shows "winding_number p \<zeta> = 0 \<longleftrightarrow> homotopic_paths (-{\<zeta>}) p (linepath (pathstart p) (pathstart p))"
+        (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  then show ?rhs
+    apply (auto simp: winding_number_homotopic_loops_null_eq [OF assms])
+    apply (rule homotopic_loops_imp_homotopic_paths_null)
+    apply (simp add: linepath_refl)
+    done
+next
+  assume ?rhs
+  then show ?lhs
+    by (metis \<zeta> pathstart_in_path_image winding_number_homotopic_paths winding_number_trivial)
+qed
+
+lemma winding_number_homotopic_paths_null_eq:
+  assumes "path p" and \<zeta>: "\<zeta> \<notin> path_image p"
+  shows "winding_number p \<zeta> = 0 \<longleftrightarrow> (\<exists>a. homotopic_paths (-{\<zeta>}) p (\<lambda>t. a))"
+    (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  then show ?rhs
+    by (auto simp: winding_number_homotopic_paths_null_explicit_eq [OF assms] linepath_refl)
+next
+  assume ?rhs
+  then show ?lhs
+    by (metis \<zeta> homotopic_paths_imp_pathfinish pathfinish_def pathfinish_in_path_image winding_number_homotopic_paths winding_number_zero_const)
+qed
+
+proposition winding_number_homotopic_paths_eq:
+  assumes "path p" and \<zeta>p: "\<zeta> \<notin> path_image p"
+      and "path q" and \<zeta>q: "\<zeta> \<notin> path_image q"
+      and qp: "pathstart q = pathstart p" "pathfinish q = pathfinish p"
+    shows "winding_number p \<zeta> = winding_number q \<zeta> \<longleftrightarrow> homotopic_paths (-{\<zeta>}) p q"
+    (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  then have "winding_number (p +++ reversepath q) \<zeta> = 0"
+    using assms by (simp add: winding_number_join winding_number_reversepath)
+  moreover
+  have "path (p +++ reversepath q)" "\<zeta> \<notin> path_image (p +++ reversepath q)"
+    using assms by (auto simp: not_in_path_image_join)
+  ultimately obtain a where "homotopic_paths (- {\<zeta>}) (p +++ reversepath q) (linepath a a)"
+    using winding_number_homotopic_paths_null_explicit_eq by blast
+  then show ?rhs
+    using homotopic_paths_imp_pathstart assms
+    by (fastforce simp add: dest: homotopic_paths_imp_homotopic_loops homotopic_paths_loop_parts)
+next
+  assume ?rhs
+  then show ?lhs
+    by (simp add: winding_number_homotopic_paths)
+qed
+
+lemma winding_number_homotopic_loops_eq:
+  assumes "path p" and \<zeta>p: "\<zeta> \<notin> path_image p"
+      and "path q" and \<zeta>q: "\<zeta> \<notin> path_image q"
+      and loops: "pathfinish p = pathstart p" "pathfinish q = pathstart q"
+    shows "winding_number p \<zeta> = winding_number q \<zeta> \<longleftrightarrow> homotopic_loops (-{\<zeta>}) p q"
+    (is "?lhs = ?rhs")
+proof
+  assume L: ?lhs
+  have "pathstart p \<noteq> \<zeta>" "pathstart q \<noteq> \<zeta>"
+    using \<zeta>p \<zeta>q by blast+
+  moreover have "path_connected (-{\<zeta>})"
+    by (simp add: path_connected_punctured_universe)
+  ultimately obtain r where "path r" and rim: "path_image r \<subseteq> -{\<zeta>}"
+                        and pas: "pathstart r = pathstart p" and paf: "pathfinish r = pathstart q"
+    by (auto simp: path_connected_def)
+  then have "pathstart r \<noteq> \<zeta>" by blast
+  have "homotopic_loops (- {\<zeta>}) p (r +++ q +++ reversepath r)"
+  proof (rule homotopic_paths_imp_homotopic_loops)
+    show "homotopic_paths (- {\<zeta>}) p (r +++ q +++ reversepath r)"
+      by (metis (mono_tags, hide_lams) \<open>path r\<close> L \<zeta>p \<zeta>q \<open>path p\<close> \<open>path q\<close> homotopic_loops_conjugate loops not_in_path_image_join paf pas path_image_reversepath path_imp_reversepath path_join_eq pathfinish_join pathfinish_reversepath  pathstart_join pathstart_reversepath rim subset_Compl_singleton winding_number_homotopic_loops winding_number_homotopic_paths_eq)
+  qed (use loops pas in auto)
+  moreover have "homotopic_loops (- {\<zeta>}) (r +++ q +++ reversepath r) q"
+    using rim \<zeta>q by (auto simp: homotopic_loops_conjugate paf \<open>path q\<close> \<open>path r\<close> loops)
+  ultimately show ?rhs
+    using homotopic_loops_trans by metis
+next
+  assume ?rhs
+  then show ?lhs
+    by (simp add: winding_number_homotopic_loops)
+qed
+
+end
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Complex_Analysis/document/root.bib	Sat Nov 30 13:47:33 2019 +0100
@@ -0,0 +1,3 @@
+
+
+@misc{dummy}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Complex_Analysis/document/root.tex	Sat Nov 30 13:47:33 2019 +0100
@@ -0,0 +1,43 @@
+\documentclass[11pt,a4paper]{article}
+\usepackage{graphicx}
+\usepackage{isabelle}
+\usepackage{isabellesym}
+\usepackage{latexsym}
+\usepackage{textcomp}
+\usepackage{amsmath}
+\usepackage{amssymb}
+\usepackage[only,bigsqcap]{stmaryrd}
+\usepackage{pdfsetup}
+
+\usepackage{tocloft}
+
+\urlstyle{rm}
+\isabellestyle{literalunderscore}
+\pagestyle{myheadings}
+
+\raggedbottom
+
+\begin{document}
+
+\title{Complex Analysis}
+\maketitle
+
+\tableofcontents
+
+\begin{center}
+  \includegraphics[height=\textheight]{session_graph}
+\end{center}
+
+\newpage
+
+\renewcommand{\setisabellecontext}[1]{\markright{\href{#1.html}{#1.thy}}}
+
+\parindent 0pt\parskip 0.5ex
+\input{session}
+
+\pagestyle{headings}
+\bibliographystyle{abbrv}
+\bibliography{root}
+\nocite{dummy}
+
+\end{document}
--- a/src/HOL/ROOT	Wed Nov 27 16:54:33 2019 +0000
+++ b/src/HOL/ROOT	Sat Nov 30 13:47:33 2019 +0100
@@ -71,6 +71,15 @@
     "root.tex"
     "root.bib"
 
+session "HOL-Complex_Analysis" (main timing) in Complex_Analysis = "HOL-Analysis" +
+  options [document_tags = "theorem%important,corollary%important,proposition%important,class%important,instantiation%important,subsubsection%unimportant,%unimportant",
+    document_variants = "document:manual=-proof,-ML,-unimportant"]
+  theories
+    Complex_Analysis
+  document_files
+    "root.tex"
+    "root.bib"
+
 session "HOL-Analysis-ex" in "Analysis/ex" = "HOL-Analysis" +
   theories
     Approximations