src/HOL/Complex_Analysis/Conformal_Mappings.thy
changeset 71201 6617fb368a06
parent 71189 954ee5acaae0
child 72228 aa7cb84983e9
--- a/src/HOL/Complex_Analysis/Conformal_Mappings.thy	Mon Dec 02 22:40:16 2019 -0500
+++ b/src/HOL/Complex_Analysis/Conformal_Mappings.thy	Mon Dec 02 17:51:54 2019 +0100
@@ -5,222 +5,10 @@
 text\<open>Also Cauchy's residue theorem by Wenda Li (2016)\<close>
 
 theory Conformal_Mappings
-imports Cauchy_Integral_Theorem
+imports Cauchy_Integral_Formula
 
 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:
@@ -2173,2944 +1961,4 @@
   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