src/HOL/Analysis/Cauchy_Integral_Theorem.thy
author paulson <lp15@cam.ac.uk>
Tue Feb 21 17:12:10 2017 +0000 (2017-02-21)
changeset 65037 2cf841ff23be
parent 65036 ab7e11730ad8
child 65578 e4997c181cce
permissions -rw-r--r--
some new material, also recasting some theorems using “obtains”
     1 section \<open>Complex path integrals and Cauchy's integral theorem\<close>
     2 
     3 text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2015)\<close>
     4 
     5 theory Cauchy_Integral_Theorem
     6 imports Complex_Transcendental Weierstrass_Theorems Ordered_Euclidean_Space
     7 begin
     8 
     9 subsection\<open>Homeomorphisms of arc images\<close>
    10 
    11 lemma homeomorphism_arc:
    12   fixes g :: "real \<Rightarrow> 'a::t2_space"
    13   assumes "arc g"
    14   obtains h where "homeomorphism {0..1} (path_image g) g h"
    15 using assms by (force simp add: arc_def homeomorphism_compact path_def path_image_def)
    16 
    17 lemma homeomorphic_arc_image_interval:
    18   fixes g :: "real \<Rightarrow> 'a::t2_space" and a::real
    19   assumes "arc g" "a < b"
    20   shows "(path_image g) homeomorphic {a..b}"
    21 proof -
    22   have "(path_image g) homeomorphic {0..1::real}"
    23     by (meson assms(1) homeomorphic_def homeomorphic_sym homeomorphism_arc)
    24   also have "... homeomorphic {a..b}"
    25     using assms by (force intro: homeomorphic_closed_intervals_real)
    26   finally show ?thesis .
    27 qed
    28 
    29 lemma homeomorphic_arc_images:
    30   fixes g :: "real \<Rightarrow> 'a::t2_space" and h :: "real \<Rightarrow> 'b::t2_space"
    31   assumes "arc g" "arc h"
    32   shows "(path_image g) homeomorphic (path_image h)"
    33 proof -
    34   have "(path_image g) homeomorphic {0..1::real}"
    35     by (meson assms homeomorphic_def homeomorphic_sym homeomorphism_arc)
    36   also have "... homeomorphic (path_image h)"
    37     by (meson assms homeomorphic_def homeomorphism_arc)
    38   finally show ?thesis .
    39 qed
    40 
    41 lemma path_connected_arc_complement:
    42   fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
    43   assumes "arc \<gamma>" "2 \<le> DIM('a)"
    44   shows "path_connected(- path_image \<gamma>)"
    45 proof -
    46   have "path_image \<gamma> homeomorphic {0..1::real}"
    47     by (simp add: assms homeomorphic_arc_image_interval)
    48   then
    49   show ?thesis
    50     apply (rule path_connected_complement_homeomorphic_convex_compact)
    51       apply (auto simp: assms)
    52     done
    53 qed
    54 
    55 lemma connected_arc_complement:
    56   fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
    57   assumes "arc \<gamma>" "2 \<le> DIM('a)"
    58   shows "connected(- path_image \<gamma>)"
    59   by (simp add: assms path_connected_arc_complement path_connected_imp_connected)
    60 
    61 lemma inside_arc_empty:
    62   fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
    63   assumes "arc \<gamma>"
    64     shows "inside(path_image \<gamma>) = {}"
    65 proof (cases "DIM('a) = 1")
    66   case True
    67   then show ?thesis
    68     using assms connected_arc_image connected_convex_1_gen inside_convex by blast
    69 next
    70   case False
    71   show ?thesis
    72   proof (rule inside_bounded_complement_connected_empty)
    73     show "connected (- path_image \<gamma>)"
    74       apply (rule connected_arc_complement [OF assms])
    75       using False
    76       by (metis DIM_ge_Suc0 One_nat_def Suc_1 not_less_eq_eq order_class.order.antisym)
    77     show "bounded (path_image \<gamma>)"
    78       by (simp add: assms bounded_arc_image)
    79   qed
    80 qed
    81 
    82 lemma inside_simple_curve_imp_closed:
    83   fixes \<gamma> :: "real \<Rightarrow> 'a::euclidean_space"
    84     shows "\<lbrakk>simple_path \<gamma>; x \<in> inside(path_image \<gamma>)\<rbrakk> \<Longrightarrow> pathfinish \<gamma> = pathstart \<gamma>"
    85   using arc_simple_path  inside_arc_empty by blast
    86 
    87     
    88 subsection \<open>Piecewise differentiable functions\<close>
    89 
    90 definition piecewise_differentiable_on
    91            (infixr "piecewise'_differentiable'_on" 50)
    92   where "f piecewise_differentiable_on i  \<equiv>
    93            continuous_on i f \<and>
    94            (\<exists>s. finite s \<and> (\<forall>x \<in> i - s. f differentiable (at x within i)))"
    95 
    96 lemma piecewise_differentiable_on_imp_continuous_on:
    97     "f piecewise_differentiable_on s \<Longrightarrow> continuous_on s f"
    98 by (simp add: piecewise_differentiable_on_def)
    99 
   100 lemma piecewise_differentiable_on_subset:
   101     "f piecewise_differentiable_on s \<Longrightarrow> t \<le> s \<Longrightarrow> f piecewise_differentiable_on t"
   102   using continuous_on_subset
   103   unfolding piecewise_differentiable_on_def
   104   apply safe
   105   apply (blast intro: elim: continuous_on_subset)
   106   by (meson Diff_iff differentiable_within_subset subsetCE)
   107 
   108 lemma differentiable_on_imp_piecewise_differentiable:
   109   fixes a:: "'a::{linorder_topology,real_normed_vector}"
   110   shows "f differentiable_on {a..b} \<Longrightarrow> f piecewise_differentiable_on {a..b}"
   111   apply (simp add: piecewise_differentiable_on_def differentiable_imp_continuous_on)
   112   apply (rule_tac x="{a,b}" in exI, simp add: differentiable_on_def)
   113   done
   114 
   115 lemma differentiable_imp_piecewise_differentiable:
   116     "(\<And>x. x \<in> s \<Longrightarrow> f differentiable (at x within s))
   117          \<Longrightarrow> f piecewise_differentiable_on s"
   118 by (auto simp: piecewise_differentiable_on_def differentiable_imp_continuous_on differentiable_on_def
   119          intro: differentiable_within_subset)
   120 
   121 lemma piecewise_differentiable_const [iff]: "(\<lambda>x. z) piecewise_differentiable_on s"
   122   by (simp add: differentiable_imp_piecewise_differentiable)
   123 
   124 lemma piecewise_differentiable_compose:
   125     "\<lbrakk>f piecewise_differentiable_on s; g piecewise_differentiable_on (f ` s);
   126       \<And>x. finite (s \<inter> f-`{x})\<rbrakk>
   127       \<Longrightarrow> (g o f) piecewise_differentiable_on s"
   128   apply (simp add: piecewise_differentiable_on_def, safe)
   129   apply (blast intro: continuous_on_compose2)
   130   apply (rename_tac A B)
   131   apply (rule_tac x="A \<union> (\<Union>x\<in>B. s \<inter> f-`{x})" in exI)
   132   apply (blast intro!: differentiable_chain_within)
   133   done
   134 
   135 lemma piecewise_differentiable_affine:
   136   fixes m::real
   137   assumes "f piecewise_differentiable_on ((\<lambda>x. m *\<^sub>R x + c) ` s)"
   138   shows "(f o (\<lambda>x. m *\<^sub>R x + c)) piecewise_differentiable_on s"
   139 proof (cases "m = 0")
   140   case True
   141   then show ?thesis
   142     unfolding o_def
   143     by (force intro: differentiable_imp_piecewise_differentiable differentiable_const)
   144 next
   145   case False
   146   show ?thesis
   147     apply (rule piecewise_differentiable_compose [OF differentiable_imp_piecewise_differentiable])
   148     apply (rule assms derivative_intros | simp add: False vimage_def real_vector_affinity_eq)+
   149     done
   150 qed
   151 
   152 lemma piecewise_differentiable_cases:
   153   fixes c::real
   154   assumes "f piecewise_differentiable_on {a..c}"
   155           "g piecewise_differentiable_on {c..b}"
   156            "a \<le> c" "c \<le> b" "f c = g c"
   157   shows "(\<lambda>x. if x \<le> c then f x else g x) piecewise_differentiable_on {a..b}"
   158 proof -
   159   obtain s t where st: "finite s" "finite t"
   160                        "\<forall>x\<in>{a..c} - s. f differentiable at x within {a..c}"
   161                        "\<forall>x\<in>{c..b} - t. g differentiable at x within {c..b}"
   162     using assms
   163     by (auto simp: piecewise_differentiable_on_def)
   164   have finabc: "finite ({a,b,c} \<union> (s \<union> t))"
   165     by (metis \<open>finite s\<close> \<open>finite t\<close> finite_Un finite_insert finite.emptyI)
   166   have "continuous_on {a..c} f" "continuous_on {c..b} g"
   167     using assms piecewise_differentiable_on_def by auto
   168   then have "continuous_on {a..b} (\<lambda>x. if x \<le> c then f x else g x)"
   169     using continuous_on_cases [OF closed_real_atLeastAtMost [of a c],
   170                                OF closed_real_atLeastAtMost [of c b],
   171                                of f g "\<lambda>x. x\<le>c"]  assms
   172     by (force simp: ivl_disj_un_two_touch)
   173   moreover
   174   { fix x
   175     assume x: "x \<in> {a..b} - ({a,b,c} \<union> (s \<union> t))"
   176     have "(\<lambda>x. if x \<le> c then f x else g x) differentiable at x within {a..b}" (is "?diff_fg")
   177     proof (cases x c rule: le_cases)
   178       case le show ?diff_fg
   179       proof (rule differentiable_transform_within [where d = "dist x c" and f = f])
   180         have "f differentiable at x within ({a<..<c} - s)"
   181           apply (rule differentiable_at_withinI)
   182           using x le st
   183           by (metis (no_types, lifting) DiffD1 DiffD2 DiffI UnCI atLeastAtMost_diff_ends atLeastAtMost_iff at_within_interior insert_iff interior_atLeastAtMost le st(3) x)
   184         moreover have "open ({a<..<c} - s)"
   185           by (blast intro: open_greaterThanLessThan \<open>finite s\<close> finite_imp_closed)
   186         ultimately show "f differentiable at x within {a..b}"
   187           using x le by (auto simp add: at_within_open_NO_MATCH differentiable_at_withinI)
   188       qed (use x le st dist_real_def in auto)
   189     next
   190       case ge show ?diff_fg
   191       proof (rule differentiable_transform_within [where d = "dist x c" and f = g])
   192         have "g differentiable at x within ({c<..<b} - t)"
   193           apply (rule differentiable_at_withinI)
   194           using x ge st
   195           by (metis DiffD1 DiffD2 DiffI UnCI atLeastAtMost_diff_ends atLeastAtMost_iff at_within_interior insert_iff interior_atLeastAtMost)
   196         moreover have "open ({c<..<b} - t)"
   197           by (blast intro: open_greaterThanLessThan \<open>finite t\<close> finite_imp_closed)
   198         ultimately show "g differentiable at x within {a..b}"
   199           using x ge by (auto simp add: at_within_open_NO_MATCH differentiable_at_withinI)
   200       qed (use x ge st dist_real_def in auto)
   201     qed
   202   }
   203   then have "\<exists>s. finite s \<and>
   204                  (\<forall>x\<in>{a..b} - s. (\<lambda>x. if x \<le> c then f x else g x) differentiable at x within {a..b})"
   205     by (meson finabc)
   206   ultimately show ?thesis
   207     by (simp add: piecewise_differentiable_on_def)
   208 qed
   209 
   210 lemma piecewise_differentiable_neg:
   211     "f piecewise_differentiable_on s \<Longrightarrow> (\<lambda>x. -(f x)) piecewise_differentiable_on s"
   212   by (auto simp: piecewise_differentiable_on_def continuous_on_minus)
   213 
   214 lemma piecewise_differentiable_add:
   215   assumes "f piecewise_differentiable_on i"
   216           "g piecewise_differentiable_on i"
   217     shows "(\<lambda>x. f x + g x) piecewise_differentiable_on i"
   218 proof -
   219   obtain s t where st: "finite s" "finite t"
   220                        "\<forall>x\<in>i - s. f differentiable at x within i"
   221                        "\<forall>x\<in>i - t. g differentiable at x within i"
   222     using assms by (auto simp: piecewise_differentiable_on_def)
   223   then have "finite (s \<union> t) \<and> (\<forall>x\<in>i - (s \<union> t). (\<lambda>x. f x + g x) differentiable at x within i)"
   224     by auto
   225   moreover have "continuous_on i f" "continuous_on i g"
   226     using assms piecewise_differentiable_on_def by auto
   227   ultimately show ?thesis
   228     by (auto simp: piecewise_differentiable_on_def continuous_on_add)
   229 qed
   230 
   231 lemma piecewise_differentiable_diff:
   232     "\<lbrakk>f piecewise_differentiable_on s;  g piecewise_differentiable_on s\<rbrakk>
   233      \<Longrightarrow> (\<lambda>x. f x - g x) piecewise_differentiable_on s"
   234   unfolding diff_conv_add_uminus
   235   by (metis piecewise_differentiable_add piecewise_differentiable_neg)
   236 
   237 lemma continuous_on_joinpaths_D1:
   238     "continuous_on {0..1} (g1 +++ g2) \<Longrightarrow> continuous_on {0..1} g1"
   239   apply (rule continuous_on_eq [of _ "(g1 +++ g2) o (op*(inverse 2))"])
   240   apply (rule continuous_intros | simp)+
   241   apply (auto elim!: continuous_on_subset simp: joinpaths_def)
   242   done
   243 
   244 lemma continuous_on_joinpaths_D2:
   245     "\<lbrakk>continuous_on {0..1} (g1 +++ g2); pathfinish g1 = pathstart g2\<rbrakk> \<Longrightarrow> continuous_on {0..1} g2"
   246   apply (rule continuous_on_eq [of _ "(g1 +++ g2) o (\<lambda>x. inverse 2*x + 1/2)"])
   247   apply (rule continuous_intros | simp)+
   248   apply (auto elim!: continuous_on_subset simp add: joinpaths_def pathfinish_def pathstart_def Ball_def)
   249   done
   250 
   251 lemma piecewise_differentiable_D1:
   252     "(g1 +++ g2) piecewise_differentiable_on {0..1} \<Longrightarrow> g1 piecewise_differentiable_on {0..1}"
   253   apply (clarsimp simp add: piecewise_differentiable_on_def dest!: continuous_on_joinpaths_D1)
   254   apply (rule_tac x="insert 1 ((op*2)`s)" in exI)
   255   apply simp
   256   apply (intro ballI)
   257   apply (rule_tac d="dist (x/2) (1/2)" and f = "(g1 +++ g2) o (op*(inverse 2))"
   258        in differentiable_transform_within)
   259   apply (auto simp: dist_real_def joinpaths_def)
   260   apply (rule differentiable_chain_within derivative_intros | simp)+
   261   apply (rule differentiable_subset)
   262   apply (force simp:)+
   263   done
   264 
   265 lemma piecewise_differentiable_D2:
   266     "\<lbrakk>(g1 +++ g2) piecewise_differentiable_on {0..1}; pathfinish g1 = pathstart g2\<rbrakk>
   267     \<Longrightarrow> g2 piecewise_differentiable_on {0..1}"
   268   apply (clarsimp simp add: piecewise_differentiable_on_def dest!: continuous_on_joinpaths_D2)
   269   apply (rule_tac x="insert 0 ((\<lambda>x. 2*x-1)`s)" in exI)
   270   apply simp
   271   apply (intro ballI)
   272   apply (rule_tac d="dist ((x+1)/2) (1/2)" and f = "(g1 +++ g2) o (\<lambda>x. (x+1)/2)"
   273           in differentiable_transform_within)
   274   apply (auto simp: dist_real_def joinpaths_def abs_if field_simps split: if_split_asm)
   275   apply (rule differentiable_chain_within derivative_intros | simp)+
   276   apply (rule differentiable_subset)
   277   apply (force simp: divide_simps)+
   278   done
   279 
   280 
   281 subsubsection\<open>The concept of continuously differentiable\<close>
   282 
   283 text \<open>
   284 John Harrison writes as follows:
   285 
   286 ``The usual assumption in complex analysis texts is that a path \<open>\<gamma>\<close> should be piecewise
   287 continuously differentiable, which ensures that the path integral exists at least for any continuous
   288 f, since all piecewise continuous functions are integrable. However, our notion of validity is
   289 weaker, just piecewise differentiability... [namely] continuity plus differentiability except on a
   290 finite set ... [Our] underlying theory of integration is the Kurzweil-Henstock theory. In contrast to
   291 the Riemann or Lebesgue theory (but in common with a simple notion based on antiderivatives), this
   292 can integrate all derivatives.''
   293 
   294 "Formalizing basic complex analysis." From Insight to Proof: Festschrift in Honour of Andrzej Trybulec.
   295 Studies in Logic, Grammar and Rhetoric 10.23 (2007): 151-165.
   296 
   297 And indeed he does not assume that his derivatives are continuous, but the penalty is unreasonably
   298 difficult proofs concerning winding numbers. We need a self-contained and straightforward theorem
   299 asserting that all derivatives can be integrated before we can adopt Harrison's choice.\<close>
   300 
   301 definition C1_differentiable_on :: "(real \<Rightarrow> 'a::real_normed_vector) \<Rightarrow> real set \<Rightarrow> bool"
   302            (infix "C1'_differentiable'_on" 50)
   303   where
   304   "f C1_differentiable_on s \<longleftrightarrow>
   305    (\<exists>D. (\<forall>x \<in> s. (f has_vector_derivative (D x)) (at x)) \<and> continuous_on s D)"
   306 
   307 lemma C1_differentiable_on_eq:
   308     "f C1_differentiable_on s \<longleftrightarrow>
   309      (\<forall>x \<in> s. f differentiable at x) \<and> continuous_on s (\<lambda>x. vector_derivative f (at x))"
   310   unfolding C1_differentiable_on_def
   311   apply safe
   312   using differentiable_def has_vector_derivative_def apply blast
   313   apply (erule continuous_on_eq)
   314   using vector_derivative_at apply fastforce
   315   using vector_derivative_works apply fastforce
   316   done
   317 
   318 lemma C1_differentiable_on_subset:
   319   "f C1_differentiable_on t \<Longrightarrow> s \<subseteq> t \<Longrightarrow> f C1_differentiable_on s"
   320   unfolding C1_differentiable_on_def  continuous_on_eq_continuous_within
   321   by (blast intro:  continuous_within_subset)
   322 
   323 lemma C1_differentiable_compose:
   324     "\<lbrakk>f C1_differentiable_on s; g C1_differentiable_on (f ` s);
   325       \<And>x. finite (s \<inter> f-`{x})\<rbrakk>
   326       \<Longrightarrow> (g o f) C1_differentiable_on s"
   327   apply (simp add: C1_differentiable_on_eq, safe)
   328    using differentiable_chain_at apply blast
   329   apply (rule continuous_on_eq [of _ "\<lambda>x. vector_derivative f (at x) *\<^sub>R vector_derivative g (at (f x))"])
   330    apply (rule Limits.continuous_on_scaleR, assumption)
   331    apply (metis (mono_tags, lifting) continuous_on_eq continuous_at_imp_continuous_on continuous_on_compose differentiable_imp_continuous_within o_def)
   332   by (simp add: vector_derivative_chain_at)
   333 
   334 lemma C1_diff_imp_diff: "f C1_differentiable_on s \<Longrightarrow> f differentiable_on s"
   335   by (simp add: C1_differentiable_on_eq differentiable_at_imp_differentiable_on)
   336 
   337 lemma C1_differentiable_on_ident [simp, derivative_intros]: "(\<lambda>x. x) C1_differentiable_on s"
   338   by (auto simp: C1_differentiable_on_eq continuous_on_const)
   339 
   340 lemma C1_differentiable_on_const [simp, derivative_intros]: "(\<lambda>z. a) C1_differentiable_on s"
   341   by (auto simp: C1_differentiable_on_eq continuous_on_const)
   342 
   343 lemma C1_differentiable_on_add [simp, derivative_intros]:
   344   "f C1_differentiable_on s \<Longrightarrow> g C1_differentiable_on s \<Longrightarrow> (\<lambda>x. f x + g x) C1_differentiable_on s"
   345   unfolding C1_differentiable_on_eq  by (auto intro: continuous_intros)
   346 
   347 lemma C1_differentiable_on_minus [simp, derivative_intros]:
   348   "f C1_differentiable_on s \<Longrightarrow> (\<lambda>x. - f x) C1_differentiable_on s"
   349   unfolding C1_differentiable_on_eq  by (auto intro: continuous_intros)
   350 
   351 lemma C1_differentiable_on_diff [simp, derivative_intros]:
   352   "f C1_differentiable_on s \<Longrightarrow> g C1_differentiable_on s \<Longrightarrow> (\<lambda>x. f x - g x) C1_differentiable_on s"
   353   unfolding C1_differentiable_on_eq  by (auto intro: continuous_intros)
   354 
   355 lemma C1_differentiable_on_mult [simp, derivative_intros]:
   356   fixes f g :: "real \<Rightarrow> 'a :: real_normed_algebra"
   357   shows "f C1_differentiable_on s \<Longrightarrow> g C1_differentiable_on s \<Longrightarrow> (\<lambda>x. f x * g x) C1_differentiable_on s"
   358   unfolding C1_differentiable_on_eq
   359   by (auto simp: continuous_on_add continuous_on_mult continuous_at_imp_continuous_on differentiable_imp_continuous_within)
   360 
   361 lemma C1_differentiable_on_scaleR [simp, derivative_intros]:
   362   "f C1_differentiable_on s \<Longrightarrow> g C1_differentiable_on s \<Longrightarrow> (\<lambda>x. f x *\<^sub>R g x) C1_differentiable_on s"
   363   unfolding C1_differentiable_on_eq
   364   by (rule continuous_intros | simp add: continuous_at_imp_continuous_on differentiable_imp_continuous_within)+
   365 
   366 
   367 definition piecewise_C1_differentiable_on
   368            (infixr "piecewise'_C1'_differentiable'_on" 50)
   369   where "f piecewise_C1_differentiable_on i  \<equiv>
   370            continuous_on i f \<and>
   371            (\<exists>s. finite s \<and> (f C1_differentiable_on (i - s)))"
   372 
   373 lemma C1_differentiable_imp_piecewise:
   374     "f C1_differentiable_on s \<Longrightarrow> f piecewise_C1_differentiable_on s"
   375   by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq continuous_at_imp_continuous_on differentiable_imp_continuous_within)
   376 
   377 lemma piecewise_C1_imp_differentiable:
   378     "f piecewise_C1_differentiable_on i \<Longrightarrow> f piecewise_differentiable_on i"
   379   by (auto simp: piecewise_C1_differentiable_on_def piecewise_differentiable_on_def
   380            C1_differentiable_on_def differentiable_def has_vector_derivative_def
   381            intro: has_derivative_at_within)
   382 
   383 lemma piecewise_C1_differentiable_compose:
   384     "\<lbrakk>f piecewise_C1_differentiable_on s; g piecewise_C1_differentiable_on (f ` s);
   385       \<And>x. finite (s \<inter> f-`{x})\<rbrakk>
   386       \<Longrightarrow> (g o f) piecewise_C1_differentiable_on s"
   387   apply (simp add: piecewise_C1_differentiable_on_def, safe)
   388   apply (blast intro: continuous_on_compose2)
   389   apply (rename_tac A B)
   390   apply (rule_tac x="A \<union> (\<Union>x\<in>B. s \<inter> f-`{x})" in exI)
   391   apply (rule conjI, blast)
   392   apply (rule C1_differentiable_compose)
   393   apply (blast intro: C1_differentiable_on_subset)
   394   apply (blast intro: C1_differentiable_on_subset)
   395   by (simp add: Diff_Int_distrib2)
   396 
   397 lemma piecewise_C1_differentiable_on_subset:
   398     "f piecewise_C1_differentiable_on s \<Longrightarrow> t \<le> s \<Longrightarrow> f piecewise_C1_differentiable_on t"
   399   by (auto simp: piecewise_C1_differentiable_on_def elim!: continuous_on_subset C1_differentiable_on_subset)
   400 
   401 lemma C1_differentiable_imp_continuous_on:
   402   "f C1_differentiable_on s \<Longrightarrow> continuous_on s f"
   403   unfolding C1_differentiable_on_eq continuous_on_eq_continuous_within
   404   using differentiable_at_withinI differentiable_imp_continuous_within by blast
   405 
   406 lemma C1_differentiable_on_empty [iff]: "f C1_differentiable_on {}"
   407   unfolding C1_differentiable_on_def
   408   by auto
   409 
   410 lemma piecewise_C1_differentiable_affine:
   411   fixes m::real
   412   assumes "f piecewise_C1_differentiable_on ((\<lambda>x. m * x + c) ` s)"
   413   shows "(f o (\<lambda>x. m *\<^sub>R x + c)) piecewise_C1_differentiable_on s"
   414 proof (cases "m = 0")
   415   case True
   416   then show ?thesis
   417     unfolding o_def by (auto simp: piecewise_C1_differentiable_on_def continuous_on_const)
   418 next
   419   case False
   420   show ?thesis
   421     apply (rule piecewise_C1_differentiable_compose [OF C1_differentiable_imp_piecewise])
   422     apply (rule assms derivative_intros | simp add: False vimage_def)+
   423     using real_vector_affinity_eq [OF False, where c=c, unfolded scaleR_conv_of_real]
   424     apply simp
   425     done
   426 qed
   427 
   428 lemma piecewise_C1_differentiable_cases:
   429   fixes c::real
   430   assumes "f piecewise_C1_differentiable_on {a..c}"
   431           "g piecewise_C1_differentiable_on {c..b}"
   432            "a \<le> c" "c \<le> b" "f c = g c"
   433   shows "(\<lambda>x. if x \<le> c then f x else g x) piecewise_C1_differentiable_on {a..b}"
   434 proof -
   435   obtain s t where st: "f C1_differentiable_on ({a..c} - s)"
   436                        "g C1_differentiable_on ({c..b} - t)"
   437                        "finite s" "finite t"
   438     using assms
   439     by (force simp: piecewise_C1_differentiable_on_def)
   440   then have f_diff: "f differentiable_on {a..<c} - s"
   441         and g_diff: "g differentiable_on {c<..b} - t"
   442     by (simp_all add: C1_differentiable_on_eq differentiable_at_withinI differentiable_on_def)
   443   have "continuous_on {a..c} f" "continuous_on {c..b} g"
   444     using assms piecewise_C1_differentiable_on_def by auto
   445   then have cab: "continuous_on {a..b} (\<lambda>x. if x \<le> c then f x else g x)"
   446     using continuous_on_cases [OF closed_real_atLeastAtMost [of a c],
   447                                OF closed_real_atLeastAtMost [of c b],
   448                                of f g "\<lambda>x. x\<le>c"]  assms
   449     by (force simp: ivl_disj_un_two_touch)
   450   { fix x
   451     assume x: "x \<in> {a..b} - insert c (s \<union> t)"
   452     have "(\<lambda>x. if x \<le> c then f x else g x) differentiable at x" (is "?diff_fg")
   453     proof (cases x c rule: le_cases)
   454       case le show ?diff_fg
   455         apply (rule differentiable_transform_within [where f=f and d = "dist x c"])
   456         using x dist_real_def le st by (auto simp: C1_differentiable_on_eq)
   457     next
   458       case ge show ?diff_fg
   459         apply (rule differentiable_transform_within [where f=g and d = "dist x c"])
   460         using dist_nz x dist_real_def ge st x by (auto simp: C1_differentiable_on_eq)
   461     qed
   462   }
   463   then have "(\<forall>x \<in> {a..b} - insert c (s \<union> t). (\<lambda>x. if x \<le> c then f x else g x) differentiable at x)"
   464     by auto
   465   moreover
   466   { assume fcon: "continuous_on ({a<..<c} - s) (\<lambda>x. vector_derivative f (at x))"
   467        and gcon: "continuous_on ({c<..<b} - t) (\<lambda>x. vector_derivative g (at x))"
   468     have "open ({a<..<c} - s)"  "open ({c<..<b} - t)"
   469       using st by (simp_all add: open_Diff finite_imp_closed)
   470     moreover have "continuous_on ({a<..<c} - s) (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
   471       apply (rule continuous_on_eq [OF fcon])
   472       apply (simp add:)
   473       apply (rule vector_derivative_at [symmetric])
   474       apply (rule_tac f=f and d="dist x c" in has_vector_derivative_transform_within)
   475       apply (simp_all add: dist_norm vector_derivative_works [symmetric])
   476       apply (metis (full_types) C1_differentiable_on_eq Diff_iff Groups.add_ac(2) add_mono_thms_linordered_field(5) atLeastAtMost_iff linorder_not_le order_less_irrefl st(1))
   477       apply auto
   478       done
   479     moreover have "continuous_on ({c<..<b} - t) (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
   480       apply (rule continuous_on_eq [OF gcon])
   481       apply (simp add:)
   482       apply (rule vector_derivative_at [symmetric])
   483       apply (rule_tac f=g and d="dist x c" in has_vector_derivative_transform_within)
   484       apply (simp_all add: dist_norm vector_derivative_works [symmetric])
   485       apply (metis (full_types) C1_differentiable_on_eq Diff_iff Groups.add_ac(2) add_mono_thms_linordered_field(5) atLeastAtMost_iff less_irrefl not_le st(2))
   486       apply auto
   487       done
   488     ultimately have "continuous_on ({a<..<b} - insert c (s \<union> t))
   489         (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
   490       apply (rule continuous_on_subset [OF continuous_on_open_Un], auto)
   491       done
   492   } note * = this
   493   have "continuous_on ({a<..<b} - insert c (s \<union> t)) (\<lambda>x. vector_derivative (\<lambda>x. if x \<le> c then f x else g x) (at x))"
   494     using st
   495     by (auto simp: C1_differentiable_on_eq elim!: continuous_on_subset intro: *)
   496   ultimately have "\<exists>s. finite s \<and> ((\<lambda>x. if x \<le> c then f x else g x) C1_differentiable_on {a..b} - s)"
   497     apply (rule_tac x="{a,b,c} \<union> s \<union> t" in exI)
   498     using st  by (auto simp: C1_differentiable_on_eq elim!: continuous_on_subset)
   499   with cab show ?thesis
   500     by (simp add: piecewise_C1_differentiable_on_def)
   501 qed
   502 
   503 lemma piecewise_C1_differentiable_neg:
   504     "f piecewise_C1_differentiable_on s \<Longrightarrow> (\<lambda>x. -(f x)) piecewise_C1_differentiable_on s"
   505   unfolding piecewise_C1_differentiable_on_def
   506   by (auto intro!: continuous_on_minus C1_differentiable_on_minus)
   507 
   508 lemma piecewise_C1_differentiable_add:
   509   assumes "f piecewise_C1_differentiable_on i"
   510           "g piecewise_C1_differentiable_on i"
   511     shows "(\<lambda>x. f x + g x) piecewise_C1_differentiable_on i"
   512 proof -
   513   obtain s t where st: "finite s" "finite t"
   514                        "f C1_differentiable_on (i-s)"
   515                        "g C1_differentiable_on (i-t)"
   516     using assms by (auto simp: piecewise_C1_differentiable_on_def)
   517   then have "finite (s \<union> t) \<and> (\<lambda>x. f x + g x) C1_differentiable_on i - (s \<union> t)"
   518     by (auto intro: C1_differentiable_on_add elim!: C1_differentiable_on_subset)
   519   moreover have "continuous_on i f" "continuous_on i g"
   520     using assms piecewise_C1_differentiable_on_def by auto
   521   ultimately show ?thesis
   522     by (auto simp: piecewise_C1_differentiable_on_def continuous_on_add)
   523 qed
   524 
   525 lemma piecewise_C1_differentiable_diff:
   526     "\<lbrakk>f piecewise_C1_differentiable_on s;  g piecewise_C1_differentiable_on s\<rbrakk>
   527      \<Longrightarrow> (\<lambda>x. f x - g x) piecewise_C1_differentiable_on s"
   528   unfolding diff_conv_add_uminus
   529   by (metis piecewise_C1_differentiable_add piecewise_C1_differentiable_neg)
   530 
   531 lemma piecewise_C1_differentiable_D1:
   532   fixes g1 :: "real \<Rightarrow> 'a::real_normed_field"
   533   assumes "(g1 +++ g2) piecewise_C1_differentiable_on {0..1}"
   534     shows "g1 piecewise_C1_differentiable_on {0..1}"
   535 proof -
   536   obtain s where "finite s"
   537              and co12: "continuous_on ({0..1} - s) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
   538              and g12D: "\<forall>x\<in>{0..1} - s. g1 +++ g2 differentiable at x"
   539     using assms  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
   540   then have g1D: "g1 differentiable at x" if "x \<in> {0..1} - insert 1 (op * 2 ` s)" for x
   541     apply (rule_tac d="dist (x/2) (1/2)" and f = "(g1 +++ g2) o (op*(inverse 2))" in differentiable_transform_within)
   542     using that
   543     apply (simp_all add: dist_real_def joinpaths_def)
   544     apply (rule differentiable_chain_at derivative_intros | force)+
   545     done
   546   have [simp]: "vector_derivative (g1 \<circ> op * 2) (at (x/2)) = 2 *\<^sub>R vector_derivative g1 (at x)"
   547                if "x \<in> {0..1} - insert 1 (op * 2 ` s)" for x
   548     apply (subst vector_derivative_chain_at)
   549     using that
   550     apply (rule derivative_eq_intros g1D | simp)+
   551     done
   552   have "continuous_on ({0..1/2} - insert (1/2) s) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
   553     using co12 by (rule continuous_on_subset) force
   554   then have coDhalf: "continuous_on ({0..1/2} - insert (1/2) s) (\<lambda>x. vector_derivative (g1 o op*2) (at x))"
   555     apply (rule continuous_on_eq [OF _ vector_derivative_at])
   556     apply (rule_tac f="g1 o op*2" and d="dist x (1/2)" in has_vector_derivative_transform_within)
   557     apply (simp_all add: dist_norm joinpaths_def vector_derivative_works [symmetric])
   558     apply (force intro: g1D differentiable_chain_at)
   559     apply auto
   560     done
   561   have "continuous_on ({0..1} - insert 1 (op * 2 ` s))
   562                       ((\<lambda>x. 1/2 * vector_derivative (g1 o op*2) (at x)) o op*(1/2))"
   563     apply (rule continuous_intros)+
   564     using coDhalf
   565     apply (simp add: scaleR_conv_of_real image_set_diff image_image)
   566     done
   567   then have con_g1: "continuous_on ({0..1} - insert 1 (op * 2 ` s)) (\<lambda>x. vector_derivative g1 (at x))"
   568     by (rule continuous_on_eq) (simp add: scaleR_conv_of_real)
   569   have "continuous_on {0..1} g1"
   570     using continuous_on_joinpaths_D1 assms piecewise_C1_differentiable_on_def by blast
   571   with \<open>finite s\<close> show ?thesis
   572     apply (clarsimp simp add: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
   573     apply (rule_tac x="insert 1 ((op*2)`s)" in exI)
   574     apply (simp add: g1D con_g1)
   575   done
   576 qed
   577 
   578 lemma piecewise_C1_differentiable_D2:
   579   fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
   580   assumes "(g1 +++ g2) piecewise_C1_differentiable_on {0..1}" "pathfinish g1 = pathstart g2"
   581     shows "g2 piecewise_C1_differentiable_on {0..1}"
   582 proof -
   583   obtain s where "finite s"
   584              and co12: "continuous_on ({0..1} - s) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
   585              and g12D: "\<forall>x\<in>{0..1} - s. g1 +++ g2 differentiable at x"
   586     using assms  by (auto simp: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
   587   then have g2D: "g2 differentiable at x" if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1) ` s)" for x
   588     apply (rule_tac d="dist ((x+1)/2) (1/2)" and f = "(g1 +++ g2) o (\<lambda>x. (x+1)/2)" in differentiable_transform_within)
   589     using that
   590     apply (simp_all add: dist_real_def joinpaths_def)
   591     apply (auto simp: dist_real_def joinpaths_def field_simps)
   592     apply (rule differentiable_chain_at derivative_intros | force)+
   593     apply (drule_tac x= "(x + 1) / 2" in bspec, force simp: divide_simps)
   594     apply assumption
   595     done
   596   have [simp]: "vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at ((x+1)/2)) = 2 *\<^sub>R vector_derivative g2 (at x)"
   597                if "x \<in> {0..1} - insert 0 ((\<lambda>x. 2*x-1) ` s)" for x
   598     using that  by (auto simp: vector_derivative_chain_at divide_simps g2D)
   599   have "continuous_on ({1/2..1} - insert (1/2) s) (\<lambda>x. vector_derivative (g1 +++ g2) (at x))"
   600     using co12 by (rule continuous_on_subset) force
   601   then have coDhalf: "continuous_on ({1/2..1} - insert (1/2) s) (\<lambda>x. vector_derivative (g2 o (\<lambda>x. 2*x-1)) (at x))"
   602     apply (rule continuous_on_eq [OF _ vector_derivative_at])
   603     apply (rule_tac f="g2 o (\<lambda>x. 2*x-1)" and d="dist (3/4) ((x+1)/2)" in has_vector_derivative_transform_within)
   604     apply (auto simp: dist_real_def field_simps joinpaths_def vector_derivative_works [symmetric]
   605                 intro!: g2D differentiable_chain_at)
   606     done
   607   have [simp]: "((\<lambda>x. (x + 1) / 2) ` ({0..1} - insert 0 ((\<lambda>x. 2 * x - 1) ` s))) = ({1/2..1} - insert (1/2) s)"
   608     apply (simp add: image_set_diff inj_on_def image_image)
   609     apply (auto simp: image_affinity_atLeastAtMost_div add_divide_distrib)
   610     done
   611   have "continuous_on ({0..1} - insert 0 ((\<lambda>x. 2*x-1) ` s))
   612                       ((\<lambda>x. 1/2 * vector_derivative (g2 \<circ> (\<lambda>x. 2*x-1)) (at x)) o (\<lambda>x. (x+1)/2))"
   613     by (rule continuous_intros | simp add:  coDhalf)+
   614   then have con_g2: "continuous_on ({0..1} - insert 0 ((\<lambda>x. 2*x-1) ` s)) (\<lambda>x. vector_derivative g2 (at x))"
   615     by (rule continuous_on_eq) (simp add: scaleR_conv_of_real)
   616   have "continuous_on {0..1} g2"
   617     using continuous_on_joinpaths_D2 assms piecewise_C1_differentiable_on_def by blast
   618   with \<open>finite s\<close> show ?thesis
   619     apply (clarsimp simp add: piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
   620     apply (rule_tac x="insert 0 ((\<lambda>x. 2 * x - 1) ` s)" in exI)
   621     apply (simp add: g2D con_g2)
   622   done
   623 qed
   624 
   625 subsection \<open>Valid paths, and their start and finish\<close>
   626 
   627 definition valid_path :: "(real \<Rightarrow> 'a :: real_normed_vector) \<Rightarrow> bool"
   628   where "valid_path f \<equiv> f piecewise_C1_differentiable_on {0..1::real}"
   629 
   630 definition closed_path :: "(real \<Rightarrow> 'a :: real_normed_vector) \<Rightarrow> bool"
   631   where "closed_path g \<equiv> g 0 = g 1"
   632 
   633 subsubsection\<open>In particular, all results for paths apply\<close>
   634 
   635 lemma valid_path_imp_path: "valid_path g \<Longrightarrow> path g"
   636 by (simp add: path_def piecewise_C1_differentiable_on_def valid_path_def)
   637 
   638 lemma connected_valid_path_image: "valid_path g \<Longrightarrow> connected(path_image g)"
   639   by (metis connected_path_image valid_path_imp_path)
   640 
   641 lemma compact_valid_path_image: "valid_path g \<Longrightarrow> compact(path_image g)"
   642   by (metis compact_path_image valid_path_imp_path)
   643 
   644 lemma bounded_valid_path_image: "valid_path g \<Longrightarrow> bounded(path_image g)"
   645   by (metis bounded_path_image valid_path_imp_path)
   646 
   647 lemma closed_valid_path_image: "valid_path g \<Longrightarrow> closed(path_image g)"
   648   by (metis closed_path_image valid_path_imp_path)
   649 
   650 proposition valid_path_compose:
   651   assumes "valid_path g"
   652       and der: "\<And>x. x \<in> path_image g \<Longrightarrow> f field_differentiable (at x)"
   653       and con: "continuous_on (path_image g) (deriv f)"
   654     shows "valid_path (f o g)"
   655 proof -
   656   obtain s where "finite s" and g_diff: "g C1_differentiable_on {0..1} - s"
   657     using \<open>valid_path g\<close> unfolding valid_path_def piecewise_C1_differentiable_on_def by auto
   658   have "f \<circ> g differentiable at t" when "t\<in>{0..1} - s" for t
   659     proof (rule differentiable_chain_at)
   660       show "g differentiable at t" using \<open>valid_path g\<close>
   661         by (meson C1_differentiable_on_eq \<open>g C1_differentiable_on {0..1} - s\<close> that)
   662     next
   663       have "g t\<in>path_image g" using that DiffD1 image_eqI path_image_def by metis
   664       then show "f differentiable at (g t)" 
   665         using der[THEN field_differentiable_imp_differentiable] by auto
   666     qed
   667   moreover have "continuous_on ({0..1} - s) (\<lambda>x. vector_derivative (f \<circ> g) (at x))"
   668     proof (rule continuous_on_eq [where f = "\<lambda>x. vector_derivative g (at x) * deriv f (g x)"],
   669         rule continuous_intros)
   670       show "continuous_on ({0..1} - s) (\<lambda>x. vector_derivative g (at x))"
   671         using g_diff C1_differentiable_on_eq by auto
   672     next
   673       have "continuous_on {0..1} (\<lambda>x. deriv f (g x))"
   674         using continuous_on_compose[OF _ con[unfolded path_image_def],unfolded comp_def]
   675           \<open>valid_path g\<close> piecewise_C1_differentiable_on_def valid_path_def
   676         by blast
   677       then show "continuous_on ({0..1} - s) (\<lambda>x. deriv f (g x))"
   678         using continuous_on_subset by blast
   679     next
   680       show "vector_derivative g (at t) * deriv f (g t) = vector_derivative (f \<circ> g) (at t)"
   681           when "t \<in> {0..1} - s" for t
   682         proof (rule vector_derivative_chain_at_general[symmetric])
   683           show "g differentiable at t" by (meson C1_differentiable_on_eq g_diff that)
   684         next
   685           have "g t\<in>path_image g" using that DiffD1 image_eqI path_image_def by metis
   686           then show "f field_differentiable at (g t)" using der by auto
   687         qed
   688     qed
   689   ultimately have "f o g C1_differentiable_on {0..1} - s"
   690     using C1_differentiable_on_eq by blast
   691   moreover have "path (f \<circ> g)" 
   692     apply (rule path_continuous_image[OF valid_path_imp_path[OF \<open>valid_path g\<close>]])
   693     using der
   694     by (simp add: continuous_at_imp_continuous_on field_differentiable_imp_continuous_at)
   695   ultimately show ?thesis unfolding valid_path_def piecewise_C1_differentiable_on_def path_def
   696     using \<open>finite s\<close> by auto
   697 qed
   698 
   699 
   700 subsection\<open>Contour Integrals along a path\<close>
   701 
   702 text\<open>This definition is for complex numbers only, and does not generalise to line integrals in a vector field\<close>
   703 
   704 text\<open>piecewise differentiable function on [0,1]\<close>
   705 
   706 definition has_contour_integral :: "(complex \<Rightarrow> complex) \<Rightarrow> complex \<Rightarrow> (real \<Rightarrow> complex) \<Rightarrow> bool"
   707            (infixr "has'_contour'_integral" 50)
   708   where "(f has_contour_integral i) g \<equiv>
   709            ((\<lambda>x. f(g x) * vector_derivative g (at x within {0..1}))
   710             has_integral i) {0..1}"
   711 
   712 definition contour_integrable_on
   713            (infixr "contour'_integrable'_on" 50)
   714   where "f contour_integrable_on g \<equiv> \<exists>i. (f has_contour_integral i) g"
   715 
   716 definition contour_integral
   717   where "contour_integral g f \<equiv> @i. (f has_contour_integral i) g \<or> ~ f contour_integrable_on g \<and> i=0"
   718 
   719 lemma not_integrable_contour_integral: "~ f contour_integrable_on g \<Longrightarrow> contour_integral g f = 0"
   720   unfolding contour_integrable_on_def contour_integral_def by blast
   721 
   722 lemma contour_integral_unique: "(f has_contour_integral i) g \<Longrightarrow> contour_integral g f = i"
   723   apply (simp add: contour_integral_def has_contour_integral_def contour_integrable_on_def)
   724   using has_integral_unique by blast
   725 
   726 corollary has_contour_integral_eqpath:
   727      "\<lbrakk>(f has_contour_integral y) p; f contour_integrable_on \<gamma>;
   728        contour_integral p f = contour_integral \<gamma> f\<rbrakk>
   729       \<Longrightarrow> (f has_contour_integral y) \<gamma>"
   730 using contour_integrable_on_def contour_integral_unique by auto
   731 
   732 lemma has_contour_integral_integral:
   733     "f contour_integrable_on i \<Longrightarrow> (f has_contour_integral (contour_integral i f)) i"
   734   by (metis contour_integral_unique contour_integrable_on_def)
   735 
   736 lemma has_contour_integral_unique:
   737     "(f has_contour_integral i) g \<Longrightarrow> (f has_contour_integral j) g \<Longrightarrow> i = j"
   738   using has_integral_unique
   739   by (auto simp: has_contour_integral_def)
   740 
   741 lemma has_contour_integral_integrable: "(f has_contour_integral i) g \<Longrightarrow> f contour_integrable_on g"
   742   using contour_integrable_on_def by blast
   743 
   744 (* Show that we can forget about the localized derivative.*)
   745 
   746 lemma vector_derivative_within_interior:
   747      "\<lbrakk>x \<in> interior s; NO_MATCH UNIV s\<rbrakk>
   748       \<Longrightarrow> vector_derivative f (at x within s) = vector_derivative f (at x)"
   749   apply (simp add: vector_derivative_def has_vector_derivative_def has_derivative_def netlimit_within_interior)
   750   apply (subst lim_within_interior, auto)
   751   done
   752 
   753 lemma has_integral_localized_vector_derivative:
   754     "((\<lambda>x. f (g x) * vector_derivative g (at x within {a..b})) has_integral i) {a..b} \<longleftrightarrow>
   755      ((\<lambda>x. f (g x) * vector_derivative g (at x)) has_integral i) {a..b}"
   756 proof -
   757   have "{a..b} - {a,b} = interior {a..b}"
   758     by (simp add: atLeastAtMost_diff_ends)
   759   show ?thesis
   760     apply (rule has_integral_spike_eq [of "{a,b}"])
   761     apply (auto simp: vector_derivative_within_interior)
   762     done
   763 qed
   764 
   765 lemma integrable_on_localized_vector_derivative:
   766     "(\<lambda>x. f (g x) * vector_derivative g (at x within {a..b})) integrable_on {a..b} \<longleftrightarrow>
   767      (\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on {a..b}"
   768   by (simp add: integrable_on_def has_integral_localized_vector_derivative)
   769 
   770 lemma has_contour_integral:
   771      "(f has_contour_integral i) g \<longleftrightarrow>
   772       ((\<lambda>x. f (g x) * vector_derivative g (at x)) has_integral i) {0..1}"
   773   by (simp add: has_integral_localized_vector_derivative has_contour_integral_def)
   774 
   775 lemma contour_integrable_on:
   776      "f contour_integrable_on g \<longleftrightarrow>
   777       (\<lambda>t. f(g t) * vector_derivative g (at t)) integrable_on {0..1}"
   778   by (simp add: has_contour_integral integrable_on_def contour_integrable_on_def)
   779 
   780 subsection\<open>Reversing a path\<close>
   781 
   782 lemma valid_path_imp_reverse:
   783   assumes "valid_path g"
   784     shows "valid_path(reversepath g)"
   785 proof -
   786   obtain s where "finite s" "g C1_differentiable_on ({0..1} - s)"
   787     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
   788   then have "finite (op - 1 ` s)" "(reversepath g C1_differentiable_on ({0..1} - op - 1 ` s))"
   789     apply (auto simp: reversepath_def)
   790     apply (rule C1_differentiable_compose [of "\<lambda>x::real. 1-x" _ g, unfolded o_def])
   791     apply (auto simp: C1_differentiable_on_eq)
   792     apply (rule continuous_intros, force)
   793     apply (force elim!: continuous_on_subset)
   794     apply (simp add: finite_vimageI inj_on_def)
   795     done
   796   then show ?thesis using assms
   797     by (auto simp: valid_path_def piecewise_C1_differentiable_on_def path_def [symmetric])
   798 qed
   799 
   800 lemma valid_path_reversepath [simp]: "valid_path(reversepath g) \<longleftrightarrow> valid_path g"
   801   using valid_path_imp_reverse by force
   802 
   803 lemma has_contour_integral_reversepath:
   804   assumes "valid_path g" "(f has_contour_integral i) g"
   805     shows "(f has_contour_integral (-i)) (reversepath g)"
   806 proof -
   807   { fix s x
   808     assume xs: "g C1_differentiable_on ({0..1} - s)" "x \<notin> op - 1 ` s" "0 \<le> x" "x \<le> 1"
   809       have "vector_derivative (\<lambda>x. g (1 - x)) (at x within {0..1}) =
   810             - vector_derivative g (at (1 - x) within {0..1})"
   811       proof -
   812         obtain f' where f': "(g has_vector_derivative f') (at (1 - x))"
   813           using xs
   814           by (force simp: has_vector_derivative_def C1_differentiable_on_def)
   815         have "(g o (\<lambda>x. 1 - x) has_vector_derivative -1 *\<^sub>R f') (at x)"
   816           apply (rule vector_diff_chain_within)
   817           apply (intro vector_diff_chain_within derivative_eq_intros | simp)+
   818           apply (rule has_vector_derivative_at_within [OF f'])
   819           done
   820         then have mf': "((\<lambda>x. g (1 - x)) has_vector_derivative -f') (at x)"
   821           by (simp add: o_def)
   822         show ?thesis
   823           using xs
   824           by (auto simp: vector_derivative_at_within_ivl [OF mf'] vector_derivative_at_within_ivl [OF f'])
   825       qed
   826   } note * = this
   827   have 01: "{0..1::real} = cbox 0 1"
   828     by simp
   829   show ?thesis using assms
   830     apply (auto simp: has_contour_integral_def)
   831     apply (drule has_integral_affinity01 [where m= "-1" and c=1])
   832     apply (auto simp: reversepath_def valid_path_def piecewise_C1_differentiable_on_def)
   833     apply (drule has_integral_neg)
   834     apply (rule_tac s = "(\<lambda>x. 1 - x) ` s" in has_integral_spike_finite)
   835     apply (auto simp: *)
   836     done
   837 qed
   838 
   839 lemma contour_integrable_reversepath:
   840     "valid_path g \<Longrightarrow> f contour_integrable_on g \<Longrightarrow> f contour_integrable_on (reversepath g)"
   841   using has_contour_integral_reversepath contour_integrable_on_def by blast
   842 
   843 lemma contour_integrable_reversepath_eq:
   844     "valid_path g \<Longrightarrow> (f contour_integrable_on (reversepath g) \<longleftrightarrow> f contour_integrable_on g)"
   845   using contour_integrable_reversepath valid_path_reversepath by fastforce
   846 
   847 lemma contour_integral_reversepath:
   848   assumes "valid_path g"
   849     shows "contour_integral (reversepath g) f = - (contour_integral g f)"
   850 proof (cases "f contour_integrable_on g")
   851   case True then show ?thesis
   852     by (simp add: assms contour_integral_unique has_contour_integral_integral has_contour_integral_reversepath)
   853 next
   854   case False then have "~ f contour_integrable_on (reversepath g)"
   855     by (simp add: assms contour_integrable_reversepath_eq)
   856   with False show ?thesis by (simp add: not_integrable_contour_integral)
   857 qed
   858 
   859 
   860 subsection\<open>Joining two paths together\<close>
   861 
   862 lemma valid_path_join:
   863   assumes "valid_path g1" "valid_path g2" "pathfinish g1 = pathstart g2"
   864     shows "valid_path(g1 +++ g2)"
   865 proof -
   866   have "g1 1 = g2 0"
   867     using assms by (auto simp: pathfinish_def pathstart_def)
   868   moreover have "(g1 o (\<lambda>x. 2*x)) piecewise_C1_differentiable_on {0..1/2}"
   869     apply (rule piecewise_C1_differentiable_compose)
   870     using assms
   871     apply (auto simp: valid_path_def piecewise_C1_differentiable_on_def continuous_on_joinpaths)
   872     apply (rule continuous_intros | simp)+
   873     apply (force intro: finite_vimageI [where h = "op*2"] inj_onI)
   874     done
   875   moreover have "(g2 o (\<lambda>x. 2*x-1)) piecewise_C1_differentiable_on {1/2..1}"
   876     apply (rule piecewise_C1_differentiable_compose)
   877     using assms unfolding valid_path_def piecewise_C1_differentiable_on_def
   878     by (auto intro!: continuous_intros finite_vimageI [where h = "(\<lambda>x. 2*x - 1)"] inj_onI
   879              simp: image_affinity_atLeastAtMost_diff continuous_on_joinpaths)
   880   ultimately show ?thesis
   881     apply (simp only: valid_path_def continuous_on_joinpaths joinpaths_def)
   882     apply (rule piecewise_C1_differentiable_cases)
   883     apply (auto simp: o_def)
   884     done
   885 qed
   886 
   887 lemma valid_path_join_D1:
   888   fixes g1 :: "real \<Rightarrow> 'a::real_normed_field"
   889   shows "valid_path (g1 +++ g2) \<Longrightarrow> valid_path g1"
   890   unfolding valid_path_def
   891   by (rule piecewise_C1_differentiable_D1)
   892 
   893 lemma valid_path_join_D2:
   894   fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
   895   shows "\<lbrakk>valid_path (g1 +++ g2); pathfinish g1 = pathstart g2\<rbrakk> \<Longrightarrow> valid_path g2"
   896   unfolding valid_path_def
   897   by (rule piecewise_C1_differentiable_D2)
   898 
   899 lemma valid_path_join_eq [simp]:
   900   fixes g2 :: "real \<Rightarrow> 'a::real_normed_field"
   901   shows "pathfinish g1 = pathstart g2 \<Longrightarrow> (valid_path(g1 +++ g2) \<longleftrightarrow> valid_path g1 \<and> valid_path g2)"
   902   using valid_path_join_D1 valid_path_join_D2 valid_path_join by blast
   903 
   904 lemma has_contour_integral_join:
   905   assumes "(f has_contour_integral i1) g1" "(f has_contour_integral i2) g2"
   906           "valid_path g1" "valid_path g2"
   907     shows "(f has_contour_integral (i1 + i2)) (g1 +++ g2)"
   908 proof -
   909   obtain s1 s2
   910     where s1: "finite s1" "\<forall>x\<in>{0..1} - s1. g1 differentiable at x"
   911       and s2: "finite s2" "\<forall>x\<in>{0..1} - s2. g2 differentiable at x"
   912     using assms
   913     by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
   914   have 1: "((\<lambda>x. f (g1 x) * vector_derivative g1 (at x)) has_integral i1) {0..1}"
   915    and 2: "((\<lambda>x. f (g2 x) * vector_derivative g2 (at x)) has_integral i2) {0..1}"
   916     using assms
   917     by (auto simp: has_contour_integral)
   918   have i1: "((\<lambda>x. (2*f (g1 (2*x))) * vector_derivative g1 (at (2*x))) has_integral i1) {0..1/2}"
   919    and i2: "((\<lambda>x. (2*f (g2 (2*x - 1))) * vector_derivative g2 (at (2*x - 1))) has_integral i2) {1/2..1}"
   920     using has_integral_affinity01 [OF 1, where m= 2 and c=0, THEN has_integral_cmul [where c=2]]
   921           has_integral_affinity01 [OF 2, where m= 2 and c="-1", THEN has_integral_cmul [where c=2]]
   922     by (simp_all only: image_affinity_atLeastAtMost_div_diff, simp_all add: scaleR_conv_of_real mult_ac)
   923   have g1: "\<lbrakk>0 \<le> z; z*2 < 1; z*2 \<notin> s1\<rbrakk> \<Longrightarrow>
   924             vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at z) =
   925             2 *\<^sub>R vector_derivative g1 (at (z*2))" for z
   926     apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g1(2*x))" and d = "\<bar>z - 1/2\<bar>"]])
   927     apply (simp_all add: dist_real_def abs_if split: if_split_asm)
   928     apply (rule vector_diff_chain_at [of "\<lambda>x. 2*x" 2 _ g1, simplified o_def])
   929     apply (simp add: has_vector_derivative_def has_derivative_def bounded_linear_mult_left)
   930     using s1
   931     apply (auto simp: algebra_simps vector_derivative_works)
   932     done
   933   have g2: "\<lbrakk>1 < z*2; z \<le> 1; z*2 - 1 \<notin> s2\<rbrakk> \<Longrightarrow>
   934             vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at z) =
   935             2 *\<^sub>R vector_derivative g2 (at (z*2 - 1))" for z
   936     apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g2 (2*x - 1))" and d = "\<bar>z - 1/2\<bar>"]])
   937     apply (simp_all add: dist_real_def abs_if split: if_split_asm)
   938     apply (rule vector_diff_chain_at [of "\<lambda>x. 2*x - 1" 2 _ g2, simplified o_def])
   939     apply (simp add: has_vector_derivative_def has_derivative_def bounded_linear_mult_left)
   940     using s2
   941     apply (auto simp: algebra_simps vector_derivative_works)
   942     done
   943   have "((\<lambda>x. f ((g1 +++ g2) x) * vector_derivative (g1 +++ g2) (at x)) has_integral i1) {0..1/2}"
   944     apply (rule has_integral_spike_finite [OF _ _ i1, of "insert (1/2) (op*2 -` s1)"])
   945     using s1
   946     apply (force intro: finite_vimageI [where h = "op*2"] inj_onI)
   947     apply (clarsimp simp add: joinpaths_def scaleR_conv_of_real mult_ac g1)
   948     done
   949   moreover have "((\<lambda>x. f ((g1 +++ g2) x) * vector_derivative (g1 +++ g2) (at x)) has_integral i2) {1/2..1}"
   950     apply (rule has_integral_spike_finite [OF _ _ i2, of "insert (1/2) ((\<lambda>x. 2*x-1) -` s2)"])
   951     using s2
   952     apply (force intro: finite_vimageI [where h = "\<lambda>x. 2*x-1"] inj_onI)
   953     apply (clarsimp simp add: joinpaths_def scaleR_conv_of_real mult_ac g2)
   954     done
   955   ultimately
   956   show ?thesis
   957     apply (simp add: has_contour_integral)
   958     apply (rule has_integral_combine [where c = "1/2"], auto)
   959     done
   960 qed
   961 
   962 lemma contour_integrable_joinI:
   963   assumes "f contour_integrable_on g1" "f contour_integrable_on g2"
   964           "valid_path g1" "valid_path g2"
   965     shows "f contour_integrable_on (g1 +++ g2)"
   966   using assms
   967   by (meson has_contour_integral_join contour_integrable_on_def)
   968 
   969 lemma contour_integrable_joinD1:
   970   assumes "f contour_integrable_on (g1 +++ g2)" "valid_path g1"
   971     shows "f contour_integrable_on g1"
   972 proof -
   973   obtain s1
   974     where s1: "finite s1" "\<forall>x\<in>{0..1} - s1. g1 differentiable at x"
   975     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
   976   have "(\<lambda>x. f ((g1 +++ g2) (x/2)) * vector_derivative (g1 +++ g2) (at (x/2))) integrable_on {0..1}"
   977     using assms
   978     apply (auto simp: contour_integrable_on)
   979     apply (drule integrable_on_subcbox [where a=0 and b="1/2"])
   980     apply (auto intro: integrable_affinity [of _ 0 "1/2::real" "1/2" 0, simplified])
   981     done
   982   then have *: "(\<lambda>x. (f ((g1 +++ g2) (x/2))/2) * vector_derivative (g1 +++ g2) (at (x/2))) integrable_on {0..1}"
   983     by (auto dest: integrable_cmul [where c="1/2"] simp: scaleR_conv_of_real)
   984   have g1: "\<lbrakk>0 < z; z < 1; z \<notin> s1\<rbrakk> \<Longrightarrow>
   985             vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at (z/2)) =
   986             2 *\<^sub>R vector_derivative g1 (at z)"  for z
   987     apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g1(2*x))" and d = "\<bar>(z-1)/2\<bar>"]])
   988     apply (simp_all add: field_simps dist_real_def abs_if split: if_split_asm)
   989     apply (rule vector_diff_chain_at [of "\<lambda>x. x*2" 2 _ g1, simplified o_def])
   990     using s1
   991     apply (auto simp: vector_derivative_works has_vector_derivative_def has_derivative_def bounded_linear_mult_left)
   992     done
   993   show ?thesis
   994     using s1
   995     apply (auto simp: contour_integrable_on)
   996     apply (rule integrable_spike_finite [of "{0,1} \<union> s1", OF _ _ *])
   997     apply (auto simp: joinpaths_def scaleR_conv_of_real g1)
   998     done
   999 qed
  1000 
  1001 lemma contour_integrable_joinD2:
  1002   assumes "f contour_integrable_on (g1 +++ g2)" "valid_path g2"
  1003     shows "f contour_integrable_on g2"
  1004 proof -
  1005   obtain s2
  1006     where s2: "finite s2" "\<forall>x\<in>{0..1} - s2. g2 differentiable at x"
  1007     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  1008   have "(\<lambda>x. f ((g1 +++ g2) (x/2 + 1/2)) * vector_derivative (g1 +++ g2) (at (x/2 + 1/2))) integrable_on {0..1}"
  1009     using assms
  1010     apply (auto simp: contour_integrable_on)
  1011     apply (drule integrable_on_subcbox [where a="1/2" and b=1], auto)
  1012     apply (drule integrable_affinity [of _ "1/2::real" 1 "1/2" "1/2", simplified])
  1013     apply (simp add: image_affinity_atLeastAtMost_diff)
  1014     done
  1015   then have *: "(\<lambda>x. (f ((g1 +++ g2) (x/2 + 1/2))/2) * vector_derivative (g1 +++ g2) (at (x/2 + 1/2)))
  1016                 integrable_on {0..1}"
  1017     by (auto dest: integrable_cmul [where c="1/2"] simp: scaleR_conv_of_real)
  1018   have g2: "\<lbrakk>0 < z; z < 1; z \<notin> s2\<rbrakk> \<Longrightarrow>
  1019             vector_derivative (\<lambda>x. if x*2 \<le> 1 then g1 (2*x) else g2 (2*x - 1)) (at (z/2+1/2)) =
  1020             2 *\<^sub>R vector_derivative g2 (at z)" for z
  1021     apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g2(2*x-1))" and d = "\<bar>z/2\<bar>"]])
  1022     apply (simp_all add: field_simps dist_real_def abs_if split: if_split_asm)
  1023     apply (rule vector_diff_chain_at [of "\<lambda>x. x*2-1" 2 _ g2, simplified o_def])
  1024     using s2
  1025     apply (auto simp: has_vector_derivative_def has_derivative_def bounded_linear_mult_left
  1026                       vector_derivative_works add_divide_distrib)
  1027     done
  1028   show ?thesis
  1029     using s2
  1030     apply (auto simp: contour_integrable_on)
  1031     apply (rule integrable_spike_finite [of "{0,1} \<union> s2", OF _ _ *])
  1032     apply (auto simp: joinpaths_def scaleR_conv_of_real g2)
  1033     done
  1034 qed
  1035 
  1036 lemma contour_integrable_join [simp]:
  1037   shows
  1038     "\<lbrakk>valid_path g1; valid_path g2\<rbrakk>
  1039      \<Longrightarrow> f contour_integrable_on (g1 +++ g2) \<longleftrightarrow> f contour_integrable_on g1 \<and> f contour_integrable_on g2"
  1040 using contour_integrable_joinD1 contour_integrable_joinD2 contour_integrable_joinI by blast
  1041 
  1042 lemma contour_integral_join [simp]:
  1043   shows
  1044     "\<lbrakk>f contour_integrable_on g1; f contour_integrable_on g2; valid_path g1; valid_path g2\<rbrakk>
  1045         \<Longrightarrow> contour_integral (g1 +++ g2) f = contour_integral g1 f + contour_integral g2 f"
  1046   by (simp add: has_contour_integral_integral has_contour_integral_join contour_integral_unique)
  1047 
  1048 
  1049 subsection\<open>Shifting the starting point of a (closed) path\<close>
  1050 
  1051 lemma shiftpath_alt_def: "shiftpath a f = (\<lambda>x. if x \<le> 1-a then f (a + x) else f (a + x - 1))"
  1052   by (auto simp: shiftpath_def)
  1053 
  1054 lemma valid_path_shiftpath [intro]:
  1055   assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
  1056     shows "valid_path(shiftpath a g)"
  1057   using assms
  1058   apply (auto simp: valid_path_def shiftpath_alt_def)
  1059   apply (rule piecewise_C1_differentiable_cases)
  1060   apply (auto simp: algebra_simps)
  1061   apply (rule piecewise_C1_differentiable_affine [of g 1 a, simplified o_def scaleR_one])
  1062   apply (auto simp: pathfinish_def pathstart_def elim: piecewise_C1_differentiable_on_subset)
  1063   apply (rule piecewise_C1_differentiable_affine [of g 1 "a-1", simplified o_def scaleR_one algebra_simps])
  1064   apply (auto simp: pathfinish_def pathstart_def elim: piecewise_C1_differentiable_on_subset)
  1065   done
  1066 
  1067 lemma has_contour_integral_shiftpath:
  1068   assumes f: "(f has_contour_integral i) g" "valid_path g"
  1069       and a: "a \<in> {0..1}"
  1070     shows "(f has_contour_integral i) (shiftpath a g)"
  1071 proof -
  1072   obtain s
  1073     where s: "finite s" and g: "\<forall>x\<in>{0..1} - s. g differentiable at x"
  1074     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  1075   have *: "((\<lambda>x. f (g x) * vector_derivative g (at x)) has_integral i) {0..1}"
  1076     using assms by (auto simp: has_contour_integral)
  1077   then have i: "i = integral {a..1} (\<lambda>x. f (g x) * vector_derivative g (at x)) +
  1078                     integral {0..a} (\<lambda>x. f (g x) * vector_derivative g (at x))"
  1079     apply (rule has_integral_unique)
  1080     apply (subst add.commute)
  1081     apply (subst integral_combine)
  1082     using assms * integral_unique by auto
  1083   { fix x
  1084     have "0 \<le> x \<Longrightarrow> x + a < 1 \<Longrightarrow> x \<notin> (\<lambda>x. x - a) ` s \<Longrightarrow>
  1085          vector_derivative (shiftpath a g) (at x) = vector_derivative g (at (x + a))"
  1086       unfolding shiftpath_def
  1087       apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g(a+x))" and d = "dist(1-a) x"]])
  1088         apply (auto simp: field_simps dist_real_def abs_if split: if_split_asm)
  1089       apply (rule vector_diff_chain_at [of "\<lambda>x. x+a" 1 _ g, simplified o_def scaleR_one])
  1090        apply (intro derivative_eq_intros | simp)+
  1091       using g
  1092        apply (drule_tac x="x+a" in bspec)
  1093       using a apply (auto simp: has_vector_derivative_def vector_derivative_works image_def add.commute)
  1094       done
  1095   } note vd1 = this
  1096   { fix x
  1097     have "1 < x + a \<Longrightarrow> x \<le> 1 \<Longrightarrow> x \<notin> (\<lambda>x. x - a + 1) ` s \<Longrightarrow>
  1098           vector_derivative (shiftpath a g) (at x) = vector_derivative g (at (x + a - 1))"
  1099       unfolding shiftpath_def
  1100       apply (rule vector_derivative_at [OF has_vector_derivative_transform_within [where f = "(\<lambda>x. g(a+x-1))" and d = "dist (1-a) x"]])
  1101         apply (auto simp: field_simps dist_real_def abs_if split: if_split_asm)
  1102       apply (rule vector_diff_chain_at [of "\<lambda>x. x+a-1" 1 _ g, simplified o_def scaleR_one])
  1103        apply (intro derivative_eq_intros | simp)+
  1104       using g
  1105       apply (drule_tac x="x+a-1" in bspec)
  1106       using a apply (auto simp: has_vector_derivative_def vector_derivative_works image_def add.commute)
  1107       done
  1108   } note vd2 = this
  1109   have va1: "(\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on ({a..1})"
  1110     using * a   by (fastforce intro: integrable_subinterval_real)
  1111   have v0a: "(\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on ({0..a})"
  1112     apply (rule integrable_subinterval_real)
  1113     using * a by auto
  1114   have "((\<lambda>x. f (shiftpath a g x) * vector_derivative (shiftpath a g) (at x))
  1115         has_integral  integral {a..1} (\<lambda>x. f (g x) * vector_derivative g (at x)))  {0..1 - a}"
  1116     apply (rule has_integral_spike_finite
  1117              [where s = "{1-a} \<union> (\<lambda>x. x-a) ` s" and f = "\<lambda>x. f(g(a+x)) * vector_derivative g (at(a+x))"])
  1118       using s apply blast
  1119      using a apply (auto simp: algebra_simps vd1)
  1120      apply (force simp: shiftpath_def add.commute)
  1121     using has_integral_affinity [where m=1 and c=a, simplified, OF integrable_integral [OF va1]]
  1122     apply (simp add: image_affinity_atLeastAtMost_diff [where m=1 and c=a, simplified] add.commute)
  1123     done
  1124   moreover
  1125   have "((\<lambda>x. f (shiftpath a g x) * vector_derivative (shiftpath a g) (at x))
  1126         has_integral  integral {0..a} (\<lambda>x. f (g x) * vector_derivative g (at x)))  {1 - a..1}"
  1127     apply (rule has_integral_spike_finite
  1128              [where s = "{1-a} \<union> (\<lambda>x. x-a+1) ` s" and f = "\<lambda>x. f(g(a+x-1)) * vector_derivative g (at(a+x-1))"])
  1129       using s apply blast
  1130      using a apply (auto simp: algebra_simps vd2)
  1131      apply (force simp: shiftpath_def add.commute)
  1132     using has_integral_affinity [where m=1 and c="a-1", simplified, OF integrable_integral [OF v0a]]
  1133     apply (simp add: image_affinity_atLeastAtMost [where m=1 and c="1-a", simplified])
  1134     apply (simp add: algebra_simps)
  1135     done
  1136   ultimately show ?thesis
  1137     using a
  1138     by (auto simp: i has_contour_integral intro: has_integral_combine [where c = "1-a"])
  1139 qed
  1140 
  1141 lemma has_contour_integral_shiftpath_D:
  1142   assumes "(f has_contour_integral i) (shiftpath a g)"
  1143           "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
  1144     shows "(f has_contour_integral i) g"
  1145 proof -
  1146   obtain s
  1147     where s: "finite s" and g: "\<forall>x\<in>{0..1} - s. g differentiable at x"
  1148     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq)
  1149   { fix x
  1150     assume x: "0 < x" "x < 1" "x \<notin> s"
  1151     then have gx: "g differentiable at x"
  1152       using g by auto
  1153     have "vector_derivative g (at x within {0..1}) =
  1154           vector_derivative (shiftpath (1 - a) (shiftpath a g)) (at x within {0..1})"
  1155       apply (rule vector_derivative_at_within_ivl
  1156                   [OF has_vector_derivative_transform_within_open
  1157                       [where f = "(shiftpath (1 - a) (shiftpath a g))" and s = "{0<..<1}-s"]])
  1158       using s g assms x
  1159       apply (auto simp: finite_imp_closed open_Diff shiftpath_shiftpath
  1160                         vector_derivative_within_interior vector_derivative_works [symmetric])
  1161       apply (rule differentiable_transform_within [OF gx, of "min x (1-x)"])
  1162       apply (auto simp: dist_real_def shiftpath_shiftpath abs_if split: if_split_asm)
  1163       done
  1164   } note vd = this
  1165   have fi: "(f has_contour_integral i) (shiftpath (1 - a) (shiftpath a g))"
  1166     using assms  by (auto intro!: has_contour_integral_shiftpath)
  1167   show ?thesis
  1168     apply (simp add: has_contour_integral_def)
  1169     apply (rule has_integral_spike_finite [of "{0,1} \<union> s", OF _ _  fi [unfolded has_contour_integral_def]])
  1170     using s assms vd
  1171     apply (auto simp: Path_Connected.shiftpath_shiftpath)
  1172     done
  1173 qed
  1174 
  1175 lemma has_contour_integral_shiftpath_eq:
  1176   assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
  1177     shows "(f has_contour_integral i) (shiftpath a g) \<longleftrightarrow> (f has_contour_integral i) g"
  1178   using assms has_contour_integral_shiftpath has_contour_integral_shiftpath_D by blast
  1179 
  1180 lemma contour_integrable_on_shiftpath_eq:
  1181   assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
  1182     shows "f contour_integrable_on (shiftpath a g) \<longleftrightarrow> f contour_integrable_on g"
  1183 using assms contour_integrable_on_def has_contour_integral_shiftpath_eq by auto
  1184 
  1185 lemma contour_integral_shiftpath:
  1186   assumes "valid_path g" "pathfinish g = pathstart g" "a \<in> {0..1}"
  1187     shows "contour_integral (shiftpath a g) f = contour_integral g f"
  1188    using assms
  1189    by (simp add: contour_integral_def contour_integrable_on_def has_contour_integral_shiftpath_eq)
  1190 
  1191 
  1192 subsection\<open>More about straight-line paths\<close>
  1193 
  1194 lemma has_vector_derivative_linepath_within:
  1195     "(linepath a b has_vector_derivative (b - a)) (at x within s)"
  1196 apply (simp add: linepath_def has_vector_derivative_def algebra_simps)
  1197 apply (rule derivative_eq_intros | simp)+
  1198 done
  1199 
  1200 lemma vector_derivative_linepath_within:
  1201     "x \<in> {0..1} \<Longrightarrow> vector_derivative (linepath a b) (at x within {0..1}) = b - a"
  1202   apply (rule vector_derivative_within_closed_interval [of 0 "1::real", simplified])
  1203   apply (auto simp: has_vector_derivative_linepath_within)
  1204   done
  1205 
  1206 lemma vector_derivative_linepath_at [simp]: "vector_derivative (linepath a b) (at x) = b - a"
  1207   by (simp add: has_vector_derivative_linepath_within vector_derivative_at)
  1208 
  1209 lemma valid_path_linepath [iff]: "valid_path (linepath a b)"
  1210   apply (simp add: valid_path_def piecewise_C1_differentiable_on_def C1_differentiable_on_eq continuous_on_linepath)
  1211   apply (rule_tac x="{}" in exI)
  1212   apply (simp add: differentiable_on_def differentiable_def)
  1213   using has_vector_derivative_def has_vector_derivative_linepath_within
  1214   apply (fastforce simp add: continuous_on_eq_continuous_within)
  1215   done
  1216 
  1217 lemma has_contour_integral_linepath:
  1218   shows "(f has_contour_integral i) (linepath a b) \<longleftrightarrow>
  1219          ((\<lambda>x. f(linepath a b x) * (b - a)) has_integral i) {0..1}"
  1220   by (simp add: has_contour_integral vector_derivative_linepath_at)
  1221 
  1222 lemma linepath_in_path:
  1223   shows "x \<in> {0..1} \<Longrightarrow> linepath a b x \<in> closed_segment a b"
  1224   by (auto simp: segment linepath_def)
  1225 
  1226 lemma linepath_image_01: "linepath a b ` {0..1} = closed_segment a b"
  1227   by (auto simp: segment linepath_def)
  1228 
  1229 lemma linepath_in_convex_hull:
  1230     fixes x::real
  1231     assumes a: "a \<in> convex hull s"
  1232         and b: "b \<in> convex hull s"
  1233         and x: "0\<le>x" "x\<le>1"
  1234        shows "linepath a b x \<in> convex hull s"
  1235   apply (rule closed_segment_subset_convex_hull [OF a b, THEN subsetD])
  1236   using x
  1237   apply (auto simp: linepath_image_01 [symmetric])
  1238   done
  1239 
  1240 lemma Re_linepath: "Re(linepath (of_real a) (of_real b) x) = (1 - x)*a + x*b"
  1241   by (simp add: linepath_def)
  1242 
  1243 lemma Im_linepath: "Im(linepath (of_real a) (of_real b) x) = 0"
  1244   by (simp add: linepath_def)
  1245 
  1246 lemma has_contour_integral_trivial [iff]: "(f has_contour_integral 0) (linepath a a)"
  1247   by (simp add: has_contour_integral_linepath)
  1248 
  1249 lemma contour_integral_trivial [simp]: "contour_integral (linepath a a) f = 0"
  1250   using has_contour_integral_trivial contour_integral_unique by blast
  1251 
  1252 
  1253 subsection\<open>Relation to subpath construction\<close>
  1254 
  1255 lemma valid_path_subpath:
  1256   fixes g :: "real \<Rightarrow> 'a :: real_normed_vector"
  1257   assumes "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}"
  1258     shows "valid_path(subpath u v g)"
  1259 proof (cases "v=u")
  1260   case True
  1261   then show ?thesis
  1262     unfolding valid_path_def subpath_def
  1263     by (force intro: C1_differentiable_on_const C1_differentiable_imp_piecewise)
  1264 next
  1265   case False
  1266   have "(g o (\<lambda>x. ((v-u) * x + u))) piecewise_C1_differentiable_on {0..1}"
  1267     apply (rule piecewise_C1_differentiable_compose)
  1268     apply (simp add: C1_differentiable_imp_piecewise)
  1269      apply (simp add: image_affinity_atLeastAtMost)
  1270     using assms False
  1271     apply (auto simp: algebra_simps valid_path_def piecewise_C1_differentiable_on_subset)
  1272     apply (subst Int_commute)
  1273     apply (auto simp: inj_on_def algebra_simps crossproduct_eq finite_vimage_IntI)
  1274     done
  1275   then show ?thesis
  1276     by (auto simp: o_def valid_path_def subpath_def)
  1277 qed
  1278 
  1279 lemma has_contour_integral_subpath_refl [iff]: "(f has_contour_integral 0) (subpath u u g)"
  1280   by (simp add: has_contour_integral subpath_def)
  1281 
  1282 lemma contour_integrable_subpath_refl [iff]: "f contour_integrable_on (subpath u u g)"
  1283   using has_contour_integral_subpath_refl contour_integrable_on_def by blast
  1284 
  1285 lemma contour_integral_subpath_refl [simp]: "contour_integral (subpath u u g) f = 0"
  1286   by (simp add: has_contour_integral_subpath_refl contour_integral_unique)
  1287 
  1288 lemma has_contour_integral_subpath:
  1289   assumes f: "f contour_integrable_on g" and g: "valid_path g"
  1290       and uv: "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
  1291     shows "(f has_contour_integral  integral {u..v} (\<lambda>x. f(g x) * vector_derivative g (at x)))
  1292            (subpath u v g)"
  1293 proof (cases "v=u")
  1294   case True
  1295   then show ?thesis
  1296     using f   by (simp add: contour_integrable_on_def subpath_def has_contour_integral)
  1297 next
  1298   case False
  1299   obtain s where s: "\<And>x. x \<in> {0..1} - s \<Longrightarrow> g differentiable at x" and fs: "finite s"
  1300     using g unfolding piecewise_C1_differentiable_on_def C1_differentiable_on_eq valid_path_def by blast
  1301   have *: "((\<lambda>x. f (g ((v - u) * x + u)) * vector_derivative g (at ((v - u) * x + u)))
  1302             has_integral (1 / (v - u)) * integral {u..v} (\<lambda>t. f (g t) * vector_derivative g (at t)))
  1303            {0..1}"
  1304     using f uv
  1305     apply (simp add: contour_integrable_on subpath_def has_contour_integral)
  1306     apply (drule integrable_on_subcbox [where a=u and b=v, simplified])
  1307     apply (simp_all add: has_integral_integral)
  1308     apply (drule has_integral_affinity [where m="v-u" and c=u, simplified])
  1309     apply (simp_all add: False image_affinity_atLeastAtMost_div_diff scaleR_conv_of_real)
  1310     apply (simp add: divide_simps False)
  1311     done
  1312   { fix x
  1313     have "x \<in> {0..1} \<Longrightarrow>
  1314            x \<notin> (\<lambda>t. (v-u) *\<^sub>R t + u) -` s \<Longrightarrow>
  1315            vector_derivative (\<lambda>x. g ((v-u) * x + u)) (at x) = (v-u) *\<^sub>R vector_derivative g (at ((v-u) * x + u))"
  1316       apply (rule vector_derivative_at [OF vector_diff_chain_at [simplified o_def]])
  1317       apply (intro derivative_eq_intros | simp)+
  1318       apply (cut_tac s [of "(v - u) * x + u"])
  1319       using uv mult_left_le [of x "v-u"]
  1320       apply (auto simp:  vector_derivative_works)
  1321       done
  1322   } note vd = this
  1323   show ?thesis
  1324     apply (cut_tac has_integral_cmul [OF *, where c = "v-u"])
  1325     using fs assms
  1326     apply (simp add: False subpath_def has_contour_integral)
  1327     apply (rule_tac s = "(\<lambda>t. ((v-u) *\<^sub>R t + u)) -` s" in has_integral_spike_finite)
  1328     apply (auto simp: inj_on_def False finite_vimageI vd scaleR_conv_of_real)
  1329     done
  1330 qed
  1331 
  1332 lemma contour_integrable_subpath:
  1333   assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}"
  1334     shows "f contour_integrable_on (subpath u v g)"
  1335   apply (cases u v rule: linorder_class.le_cases)
  1336    apply (metis contour_integrable_on_def has_contour_integral_subpath [OF assms])
  1337   apply (subst reversepath_subpath [symmetric])
  1338   apply (rule contour_integrable_reversepath)
  1339    using assms apply (blast intro: valid_path_subpath)
  1340   apply (simp add: contour_integrable_on_def)
  1341   using assms apply (blast intro: has_contour_integral_subpath)
  1342   done
  1343 
  1344 lemma has_integral_integrable_integral: "(f has_integral i) s \<longleftrightarrow> f integrable_on s \<and> integral s f = i"
  1345   by blast
  1346 
  1347 lemma has_integral_contour_integral_subpath:
  1348   assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
  1349     shows "(((\<lambda>x. f(g x) * vector_derivative g (at x)))
  1350             has_integral  contour_integral (subpath u v g) f) {u..v}"
  1351   using assms
  1352   apply (auto simp: has_integral_integrable_integral)
  1353   apply (rule integrable_on_subcbox [where a=u and b=v and s = "{0..1}", simplified])
  1354   apply (auto simp: contour_integral_unique [OF has_contour_integral_subpath] contour_integrable_on)
  1355   done
  1356 
  1357 lemma contour_integral_subcontour_integral:
  1358   assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "u \<le> v"
  1359     shows "contour_integral (subpath u v g) f =
  1360            integral {u..v} (\<lambda>x. f(g x) * vector_derivative g (at x))"
  1361   using assms has_contour_integral_subpath contour_integral_unique by blast
  1362 
  1363 lemma contour_integral_subpath_combine_less:
  1364   assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "w \<in> {0..1}"
  1365           "u<v" "v<w"
  1366     shows "contour_integral (subpath u v g) f + contour_integral (subpath v w g) f =
  1367            contour_integral (subpath u w g) f"
  1368   using assms apply (auto simp: contour_integral_subcontour_integral)
  1369   apply (rule integral_combine, auto)
  1370   apply (rule integrable_on_subcbox [where a=u and b=w and s = "{0..1}", simplified])
  1371   apply (auto simp: contour_integrable_on)
  1372   done
  1373 
  1374 lemma contour_integral_subpath_combine:
  1375   assumes "f contour_integrable_on g" "valid_path g" "u \<in> {0..1}" "v \<in> {0..1}" "w \<in> {0..1}"
  1376     shows "contour_integral (subpath u v g) f + contour_integral (subpath v w g) f =
  1377            contour_integral (subpath u w g) f"
  1378 proof (cases "u\<noteq>v \<and> v\<noteq>w \<and> u\<noteq>w")
  1379   case True
  1380     have *: "subpath v u g = reversepath(subpath u v g) \<and>
  1381              subpath w u g = reversepath(subpath u w g) \<and>
  1382              subpath w v g = reversepath(subpath v w g)"
  1383       by (auto simp: reversepath_subpath)
  1384     have "u < v \<and> v < w \<or>
  1385           u < w \<and> w < v \<or>
  1386           v < u \<and> u < w \<or>
  1387           v < w \<and> w < u \<or>
  1388           w < u \<and> u < v \<or>
  1389           w < v \<and> v < u"
  1390       using True assms by linarith
  1391     with assms show ?thesis
  1392       using contour_integral_subpath_combine_less [of f g u v w]
  1393             contour_integral_subpath_combine_less [of f g u w v]
  1394             contour_integral_subpath_combine_less [of f g v u w]
  1395             contour_integral_subpath_combine_less [of f g v w u]
  1396             contour_integral_subpath_combine_less [of f g w u v]
  1397             contour_integral_subpath_combine_less [of f g w v u]
  1398       apply simp
  1399       apply (elim disjE)
  1400       apply (auto simp: * contour_integral_reversepath contour_integrable_subpath
  1401                    valid_path_reversepath valid_path_subpath algebra_simps)
  1402       done
  1403 next
  1404   case False
  1405   then show ?thesis
  1406     apply (auto simp: contour_integral_subpath_refl)
  1407     using assms
  1408     by (metis eq_neg_iff_add_eq_0 contour_integrable_subpath contour_integral_reversepath reversepath_subpath valid_path_subpath)
  1409 qed
  1410 
  1411 lemma contour_integral_integral:
  1412      "contour_integral g f = integral {0..1} (\<lambda>x. f (g x) * vector_derivative g (at x))"
  1413   by (simp add: contour_integral_def integral_def has_contour_integral contour_integrable_on)
  1414 
  1415 
  1416 text\<open>Cauchy's theorem where there's a primitive\<close>
  1417 
  1418 lemma contour_integral_primitive_lemma:
  1419   fixes f :: "complex \<Rightarrow> complex" and g :: "real \<Rightarrow> complex"
  1420   assumes "a \<le> b"
  1421       and "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
  1422       and "g piecewise_differentiable_on {a..b}"  "\<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s"
  1423     shows "((\<lambda>x. f'(g x) * vector_derivative g (at x within {a..b}))
  1424              has_integral (f(g b) - f(g a))) {a..b}"
  1425 proof -
  1426   obtain k where k: "finite k" "\<forall>x\<in>{a..b} - k. g differentiable (at x within {a..b})" and cg: "continuous_on {a..b} g"
  1427     using assms by (auto simp: piecewise_differentiable_on_def)
  1428   have cfg: "continuous_on {a..b} (\<lambda>x. f (g x))"
  1429     apply (rule continuous_on_compose [OF cg, unfolded o_def])
  1430     using assms
  1431     apply (metis field_differentiable_def field_differentiable_imp_continuous_at continuous_on_eq_continuous_within continuous_on_subset image_subset_iff)
  1432     done
  1433   { fix x::real
  1434     assume a: "a < x" and b: "x < b" and xk: "x \<notin> k"
  1435     then have "g differentiable at x within {a..b}"
  1436       using k by (simp add: differentiable_at_withinI)
  1437     then have "(g has_vector_derivative vector_derivative g (at x within {a..b})) (at x within {a..b})"
  1438       by (simp add: vector_derivative_works has_field_derivative_def scaleR_conv_of_real)
  1439     then have gdiff: "(g has_derivative (\<lambda>u. u * vector_derivative g (at x within {a..b}))) (at x within {a..b})"
  1440       by (simp add: has_vector_derivative_def scaleR_conv_of_real)
  1441     have "(f has_field_derivative (f' (g x))) (at (g x) within g ` {a..b})"
  1442       using assms by (metis a atLeastAtMost_iff b DERIV_subset image_subset_iff less_eq_real_def)
  1443     then have fdiff: "(f has_derivative op * (f' (g x))) (at (g x) within g ` {a..b})"
  1444       by (simp add: has_field_derivative_def)
  1445     have "((\<lambda>x. f (g x)) has_vector_derivative f' (g x) * vector_derivative g (at x within {a..b})) (at x within {a..b})"
  1446       using diff_chain_within [OF gdiff fdiff]
  1447       by (simp add: has_vector_derivative_def scaleR_conv_of_real o_def mult_ac)
  1448   } note * = this
  1449   show ?thesis
  1450     apply (rule fundamental_theorem_of_calculus_interior_strong)
  1451     using k assms cfg *
  1452     apply (auto simp: at_within_closed_interval)
  1453     done
  1454 qed
  1455 
  1456 lemma contour_integral_primitive:
  1457   assumes "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
  1458       and "valid_path g" "path_image g \<subseteq> s"
  1459     shows "(f' has_contour_integral (f(pathfinish g) - f(pathstart g))) g"
  1460   using assms
  1461   apply (simp add: valid_path_def path_image_def pathfinish_def pathstart_def has_contour_integral_def)
  1462   apply (auto intro!: piecewise_C1_imp_differentiable contour_integral_primitive_lemma [of 0 1 s])
  1463   done
  1464 
  1465 corollary Cauchy_theorem_primitive:
  1466   assumes "\<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s)"
  1467       and "valid_path g"  "path_image g \<subseteq> s" "pathfinish g = pathstart g"
  1468     shows "(f' has_contour_integral 0) g"
  1469   using assms
  1470   by (metis diff_self contour_integral_primitive)
  1471 
  1472 
  1473 text\<open>Existence of path integral for continuous function\<close>
  1474 lemma contour_integrable_continuous_linepath:
  1475   assumes "continuous_on (closed_segment a b) f"
  1476   shows "f contour_integrable_on (linepath a b)"
  1477 proof -
  1478   have "continuous_on {0..1} ((\<lambda>x. f x * (b - a)) o linepath a b)"
  1479     apply (rule continuous_on_compose [OF continuous_on_linepath], simp add: linepath_image_01)
  1480     apply (rule continuous_intros | simp add: assms)+
  1481     done
  1482   then show ?thesis
  1483     apply (simp add: contour_integrable_on_def has_contour_integral_def integrable_on_def [symmetric])
  1484     apply (rule integrable_continuous [of 0 "1::real", simplified])
  1485     apply (rule continuous_on_eq [where f = "\<lambda>x. f(linepath a b x)*(b - a)"])
  1486     apply (auto simp: vector_derivative_linepath_within)
  1487     done
  1488 qed
  1489 
  1490 lemma has_field_der_id: "((\<lambda>x. x\<^sup>2 / 2) has_field_derivative x) (at x)"
  1491   by (rule has_derivative_imp_has_field_derivative)
  1492      (rule derivative_intros | simp)+
  1493 
  1494 lemma contour_integral_id [simp]: "contour_integral (linepath a b) (\<lambda>y. y) = (b^2 - a^2)/2"
  1495   apply (rule contour_integral_unique)
  1496   using contour_integral_primitive [of UNIV "\<lambda>x. x^2/2" "\<lambda>x. x" "linepath a b"]
  1497   apply (auto simp: field_simps has_field_der_id)
  1498   done
  1499 
  1500 lemma contour_integrable_on_const [iff]: "(\<lambda>x. c) contour_integrable_on (linepath a b)"
  1501   by (simp add: continuous_on_const contour_integrable_continuous_linepath)
  1502 
  1503 lemma contour_integrable_on_id [iff]: "(\<lambda>x. x) contour_integrable_on (linepath a b)"
  1504   by (simp add: continuous_on_id contour_integrable_continuous_linepath)
  1505 
  1506 
  1507 subsection\<open>Arithmetical combining theorems\<close>
  1508 
  1509 lemma has_contour_integral_neg:
  1510     "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. -(f x)) has_contour_integral (-i)) g"
  1511   by (simp add: has_integral_neg has_contour_integral_def)
  1512 
  1513 lemma has_contour_integral_add:
  1514     "\<lbrakk>(f1 has_contour_integral i1) g; (f2 has_contour_integral i2) g\<rbrakk>
  1515      \<Longrightarrow> ((\<lambda>x. f1 x + f2 x) has_contour_integral (i1 + i2)) g"
  1516   by (simp add: has_integral_add has_contour_integral_def algebra_simps)
  1517 
  1518 lemma has_contour_integral_diff:
  1519   "\<lbrakk>(f1 has_contour_integral i1) g; (f2 has_contour_integral i2) g\<rbrakk>
  1520          \<Longrightarrow> ((\<lambda>x. f1 x - f2 x) has_contour_integral (i1 - i2)) g"
  1521   by (simp add: has_integral_sub has_contour_integral_def algebra_simps)
  1522 
  1523 lemma has_contour_integral_lmul:
  1524   "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. c * (f x)) has_contour_integral (c*i)) g"
  1525 apply (simp add: has_contour_integral_def)
  1526 apply (drule has_integral_mult_right)
  1527 apply (simp add: algebra_simps)
  1528 done
  1529 
  1530 lemma has_contour_integral_rmul:
  1531   "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. (f x) * c) has_contour_integral (i*c)) g"
  1532 apply (drule has_contour_integral_lmul)
  1533 apply (simp add: mult.commute)
  1534 done
  1535 
  1536 lemma has_contour_integral_div:
  1537   "(f has_contour_integral i) g \<Longrightarrow> ((\<lambda>x. f x/c) has_contour_integral (i/c)) g"
  1538   by (simp add: field_class.field_divide_inverse) (metis has_contour_integral_rmul)
  1539 
  1540 lemma has_contour_integral_eq:
  1541     "\<lbrakk>(f has_contour_integral y) p; \<And>x. x \<in> path_image p \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> (g has_contour_integral y) p"
  1542 apply (simp add: path_image_def has_contour_integral_def)
  1543 by (metis (no_types, lifting) image_eqI has_integral_eq)
  1544 
  1545 lemma has_contour_integral_bound_linepath:
  1546   assumes "(f has_contour_integral i) (linepath a b)"
  1547           "0 \<le> B" "\<And>x. x \<in> closed_segment a b \<Longrightarrow> norm(f x) \<le> B"
  1548     shows "norm i \<le> B * norm(b - a)"
  1549 proof -
  1550   { fix x::real
  1551     assume x: "0 \<le> x" "x \<le> 1"
  1552   have "norm (f (linepath a b x)) *
  1553         norm (vector_derivative (linepath a b) (at x within {0..1})) \<le> B * norm (b - a)"
  1554     by (auto intro: mult_mono simp: assms linepath_in_path of_real_linepath vector_derivative_linepath_within x)
  1555   } note * = this
  1556   have "norm i \<le> (B * norm (b - a)) * content (cbox 0 (1::real))"
  1557     apply (rule has_integral_bound
  1558        [of _ "\<lambda>x. f (linepath a b x) * vector_derivative (linepath a b) (at x within {0..1})"])
  1559     using assms * unfolding has_contour_integral_def
  1560     apply (auto simp: norm_mult)
  1561     done
  1562   then show ?thesis
  1563     by (auto simp: content_real)
  1564 qed
  1565 
  1566 (*UNUSED
  1567 lemma has_contour_integral_bound_linepath_strong:
  1568   fixes a :: real and f :: "complex \<Rightarrow> real"
  1569   assumes "(f has_contour_integral i) (linepath a b)"
  1570           "finite k"
  1571           "0 \<le> B" "\<And>x::real. x \<in> closed_segment a b - k \<Longrightarrow> norm(f x) \<le> B"
  1572     shows "norm i \<le> B*norm(b - a)"
  1573 *)
  1574 
  1575 lemma has_contour_integral_const_linepath: "((\<lambda>x. c) has_contour_integral c*(b - a))(linepath a b)"
  1576   unfolding has_contour_integral_linepath
  1577   by (metis content_real diff_0_right has_integral_const_real lambda_one of_real_1 scaleR_conv_of_real zero_le_one)
  1578 
  1579 lemma has_contour_integral_0: "((\<lambda>x. 0) has_contour_integral 0) g"
  1580   by (simp add: has_contour_integral_def)
  1581 
  1582 lemma has_contour_integral_is_0:
  1583     "(\<And>z. z \<in> path_image g \<Longrightarrow> f z = 0) \<Longrightarrow> (f has_contour_integral 0) g"
  1584   by (rule has_contour_integral_eq [OF has_contour_integral_0]) auto
  1585 
  1586 lemma has_contour_integral_sum:
  1587     "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a has_contour_integral i a) p\<rbrakk>
  1588      \<Longrightarrow> ((\<lambda>x. sum (\<lambda>a. f a x) s) has_contour_integral sum i s) p"
  1589   by (induction s rule: finite_induct) (auto simp: has_contour_integral_0 has_contour_integral_add)
  1590 
  1591 
  1592 subsection \<open>Operations on path integrals\<close>
  1593 
  1594 lemma contour_integral_const_linepath [simp]: "contour_integral (linepath a b) (\<lambda>x. c) = c*(b - a)"
  1595   by (rule contour_integral_unique [OF has_contour_integral_const_linepath])
  1596 
  1597 lemma contour_integral_neg:
  1598     "f contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>x. -(f x)) = -(contour_integral g f)"
  1599   by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_neg)
  1600 
  1601 lemma contour_integral_add:
  1602     "f1 contour_integrable_on g \<Longrightarrow> f2 contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>x. f1 x + f2 x) =
  1603                 contour_integral g f1 + contour_integral g f2"
  1604   by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_add)
  1605 
  1606 lemma contour_integral_diff:
  1607     "f1 contour_integrable_on g \<Longrightarrow> f2 contour_integrable_on g \<Longrightarrow> contour_integral g (\<lambda>x. f1 x - f2 x) =
  1608                 contour_integral g f1 - contour_integral g f2"
  1609   by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_diff)
  1610 
  1611 lemma contour_integral_lmul:
  1612   shows "f contour_integrable_on g
  1613            \<Longrightarrow> contour_integral g (\<lambda>x. c * f x) = c*contour_integral g f"
  1614   by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_lmul)
  1615 
  1616 lemma contour_integral_rmul:
  1617   shows "f contour_integrable_on g
  1618         \<Longrightarrow> contour_integral g (\<lambda>x. f x * c) = contour_integral g f * c"
  1619   by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_rmul)
  1620 
  1621 lemma contour_integral_div:
  1622   shows "f contour_integrable_on g
  1623         \<Longrightarrow> contour_integral g (\<lambda>x. f x / c) = contour_integral g f / c"
  1624   by (simp add: contour_integral_unique has_contour_integral_integral has_contour_integral_div)
  1625 
  1626 lemma contour_integral_eq:
  1627     "(\<And>x. x \<in> path_image p \<Longrightarrow> f x = g x) \<Longrightarrow> contour_integral p f = contour_integral p g"
  1628   apply (simp add: contour_integral_def)
  1629   using has_contour_integral_eq
  1630   by (metis contour_integral_unique has_contour_integral_integrable has_contour_integral_integral)
  1631 
  1632 lemma contour_integral_eq_0:
  1633     "(\<And>z. z \<in> path_image g \<Longrightarrow> f z = 0) \<Longrightarrow> contour_integral g f = 0"
  1634   by (simp add: has_contour_integral_is_0 contour_integral_unique)
  1635 
  1636 lemma contour_integral_bound_linepath:
  1637   shows
  1638     "\<lbrakk>f contour_integrable_on (linepath a b);
  1639       0 \<le> B; \<And>x. x \<in> closed_segment a b \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
  1640      \<Longrightarrow> norm(contour_integral (linepath a b) f) \<le> B*norm(b - a)"
  1641   apply (rule has_contour_integral_bound_linepath [of f])
  1642   apply (auto simp: has_contour_integral_integral)
  1643   done
  1644 
  1645 lemma contour_integral_0 [simp]: "contour_integral g (\<lambda>x. 0) = 0"
  1646   by (simp add: contour_integral_unique has_contour_integral_0)
  1647 
  1648 lemma contour_integral_sum:
  1649     "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a) contour_integrable_on p\<rbrakk>
  1650      \<Longrightarrow> contour_integral p (\<lambda>x. sum (\<lambda>a. f a x) s) = sum (\<lambda>a. contour_integral p (f a)) s"
  1651   by (auto simp: contour_integral_unique has_contour_integral_sum has_contour_integral_integral)
  1652 
  1653 lemma contour_integrable_eq:
  1654     "\<lbrakk>f contour_integrable_on p; \<And>x. x \<in> path_image p \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> g contour_integrable_on p"
  1655   unfolding contour_integrable_on_def
  1656   by (metis has_contour_integral_eq)
  1657 
  1658 
  1659 subsection \<open>Arithmetic theorems for path integrability\<close>
  1660 
  1661 lemma contour_integrable_neg:
  1662     "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. -(f x)) contour_integrable_on g"
  1663   using has_contour_integral_neg contour_integrable_on_def by blast
  1664 
  1665 lemma contour_integrable_add:
  1666     "\<lbrakk>f1 contour_integrable_on g; f2 contour_integrable_on g\<rbrakk> \<Longrightarrow> (\<lambda>x. f1 x + f2 x) contour_integrable_on g"
  1667   using has_contour_integral_add contour_integrable_on_def
  1668   by fastforce
  1669 
  1670 lemma contour_integrable_diff:
  1671     "\<lbrakk>f1 contour_integrable_on g; f2 contour_integrable_on g\<rbrakk> \<Longrightarrow> (\<lambda>x. f1 x - f2 x) contour_integrable_on g"
  1672   using has_contour_integral_diff contour_integrable_on_def
  1673   by fastforce
  1674 
  1675 lemma contour_integrable_lmul:
  1676     "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. c * f x) contour_integrable_on g"
  1677   using has_contour_integral_lmul contour_integrable_on_def
  1678   by fastforce
  1679 
  1680 lemma contour_integrable_rmul:
  1681     "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. f x * c) contour_integrable_on g"
  1682   using has_contour_integral_rmul contour_integrable_on_def
  1683   by fastforce
  1684 
  1685 lemma contour_integrable_div:
  1686     "f contour_integrable_on g \<Longrightarrow> (\<lambda>x. f x / c) contour_integrable_on g"
  1687   using has_contour_integral_div contour_integrable_on_def
  1688   by fastforce
  1689 
  1690 lemma contour_integrable_sum:
  1691     "\<lbrakk>finite s; \<And>a. a \<in> s \<Longrightarrow> (f a) contour_integrable_on p\<rbrakk>
  1692      \<Longrightarrow> (\<lambda>x. sum (\<lambda>a. f a x) s) contour_integrable_on p"
  1693    unfolding contour_integrable_on_def
  1694    by (metis has_contour_integral_sum)
  1695 
  1696 
  1697 subsection\<open>Reversing a path integral\<close>
  1698 
  1699 lemma has_contour_integral_reverse_linepath:
  1700     "(f has_contour_integral i) (linepath a b)
  1701      \<Longrightarrow> (f has_contour_integral (-i)) (linepath b a)"
  1702   using has_contour_integral_reversepath valid_path_linepath by fastforce
  1703 
  1704 lemma contour_integral_reverse_linepath:
  1705     "continuous_on (closed_segment a b) f
  1706      \<Longrightarrow> contour_integral (linepath a b) f = - (contour_integral(linepath b a) f)"
  1707 apply (rule contour_integral_unique)
  1708 apply (rule has_contour_integral_reverse_linepath)
  1709 by (simp add: closed_segment_commute contour_integrable_continuous_linepath has_contour_integral_integral)
  1710 
  1711 
  1712 (* Splitting a path integral in a flat way.*)
  1713 
  1714 lemma has_contour_integral_split:
  1715   assumes f: "(f has_contour_integral i) (linepath a c)" "(f has_contour_integral j) (linepath c b)"
  1716       and k: "0 \<le> k" "k \<le> 1"
  1717       and c: "c - a = k *\<^sub>R (b - a)"
  1718     shows "(f has_contour_integral (i + j)) (linepath a b)"
  1719 proof (cases "k = 0 \<or> k = 1")
  1720   case True
  1721   then show ?thesis
  1722     using assms
  1723     apply auto
  1724     apply (metis add.left_neutral has_contour_integral_trivial has_contour_integral_unique)
  1725     apply (metis add.right_neutral has_contour_integral_trivial has_contour_integral_unique)
  1726     done
  1727 next
  1728   case False
  1729   then have k: "0 < k" "k < 1" "complex_of_real k \<noteq> 1"
  1730     using assms apply auto
  1731     using of_real_eq_iff by fastforce
  1732   have c': "c = k *\<^sub>R (b - a) + a"
  1733     by (metis diff_add_cancel c)
  1734   have bc: "(b - c) = (1 - k) *\<^sub>R (b - a)"
  1735     by (simp add: algebra_simps c')
  1736   { assume *: "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R c) * (c - a)) has_integral i) {0..1}"
  1737     have **: "\<And>x. ((k - x) / k) *\<^sub>R a + (x / k) *\<^sub>R c = (1 - x) *\<^sub>R a + x *\<^sub>R b"
  1738       using False
  1739       apply (simp add: c' algebra_simps)
  1740       apply (simp add: real_vector.scale_left_distrib [symmetric] divide_simps)
  1741       done
  1742     have "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R b) * (b - a)) has_integral i) {0..k}"
  1743       using * k
  1744       apply -
  1745       apply (drule has_integral_affinity [of _ _ 0 "1::real" "inverse k" "0", simplified])
  1746       apply (simp_all add: divide_simps mult.commute [of _ "k"] image_affinity_atLeastAtMost ** c)
  1747       apply (drule has_integral_cmul [where c = "inverse k"])
  1748       apply (simp add: has_integral_cmul)
  1749       done
  1750   } note fi = this
  1751   { assume *: "((\<lambda>x. f ((1 - x) *\<^sub>R c + x *\<^sub>R b) * (b - c)) has_integral j) {0..1}"
  1752     have **: "\<And>x. (((1 - x) / (1 - k)) *\<^sub>R c + ((x - k) / (1 - k)) *\<^sub>R b) = ((1 - x) *\<^sub>R a + x *\<^sub>R b)"
  1753       using k
  1754       apply (simp add: c' field_simps)
  1755       apply (simp add: scaleR_conv_of_real divide_simps)
  1756       apply (simp add: field_simps)
  1757       done
  1758     have "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R b) * (b - a)) has_integral j) {k..1}"
  1759       using * k
  1760       apply -
  1761       apply (drule has_integral_affinity [of _ _ 0 "1::real" "inverse(1 - k)" "-(k/(1 - k))", simplified])
  1762       apply (simp_all add: divide_simps mult.commute [of _ "1-k"] image_affinity_atLeastAtMost ** bc)
  1763       apply (drule has_integral_cmul [where k = "(1 - k) *\<^sub>R j" and c = "inverse (1 - k)"])
  1764       apply (simp add: has_integral_cmul)
  1765       done
  1766   } note fj = this
  1767   show ?thesis
  1768     using f k
  1769     apply (simp add: has_contour_integral_linepath)
  1770     apply (simp add: linepath_def)
  1771     apply (rule has_integral_combine [OF _ _ fi fj], simp_all)
  1772     done
  1773 qed
  1774 
  1775 lemma continuous_on_closed_segment_transform:
  1776   assumes f: "continuous_on (closed_segment a b) f"
  1777       and k: "0 \<le> k" "k \<le> 1"
  1778       and c: "c - a = k *\<^sub>R (b - a)"
  1779     shows "continuous_on (closed_segment a c) f"
  1780 proof -
  1781   have c': "c = (1 - k) *\<^sub>R a + k *\<^sub>R b"
  1782     using c by (simp add: algebra_simps)
  1783   show "continuous_on (closed_segment a c) f"
  1784     apply (rule continuous_on_subset [OF f])
  1785     apply (simp add: segment_convex_hull)
  1786     apply (rule convex_hull_subset)
  1787     using assms
  1788     apply (auto simp: hull_inc c' convexD_alt)
  1789     done
  1790 qed
  1791 
  1792 lemma contour_integral_split:
  1793   assumes f: "continuous_on (closed_segment a b) f"
  1794       and k: "0 \<le> k" "k \<le> 1"
  1795       and c: "c - a = k *\<^sub>R (b - a)"
  1796     shows "contour_integral(linepath a b) f = contour_integral(linepath a c) f + contour_integral(linepath c b) f"
  1797 proof -
  1798   have c': "c = (1 - k) *\<^sub>R a + k *\<^sub>R b"
  1799     using c by (simp add: algebra_simps)
  1800   have *: "continuous_on (closed_segment a c) f" "continuous_on (closed_segment c b) f"
  1801     apply (rule_tac [!] continuous_on_subset [OF f])
  1802     apply (simp_all add: segment_convex_hull)
  1803     apply (rule_tac [!] convex_hull_subset)
  1804     using assms
  1805     apply (auto simp: hull_inc c' convexD_alt)
  1806     done
  1807   show ?thesis
  1808     apply (rule contour_integral_unique)
  1809     apply (rule has_contour_integral_split [OF has_contour_integral_integral has_contour_integral_integral k c])
  1810     apply (rule contour_integrable_continuous_linepath *)+
  1811     done
  1812 qed
  1813 
  1814 lemma contour_integral_split_linepath:
  1815   assumes f: "continuous_on (closed_segment a b) f"
  1816       and c: "c \<in> closed_segment a b"
  1817     shows "contour_integral(linepath a b) f = contour_integral(linepath a c) f + contour_integral(linepath c b) f"
  1818   using c
  1819   by (auto simp: closed_segment_def algebra_simps intro!: contour_integral_split [OF f])
  1820 
  1821 (* The special case of midpoints used in the main quadrisection.*)
  1822 
  1823 lemma has_contour_integral_midpoint:
  1824   assumes "(f has_contour_integral i) (linepath a (midpoint a b))"
  1825           "(f has_contour_integral j) (linepath (midpoint a b) b)"
  1826     shows "(f has_contour_integral (i + j)) (linepath a b)"
  1827   apply (rule has_contour_integral_split [where c = "midpoint a b" and k = "1/2"])
  1828   using assms
  1829   apply (auto simp: midpoint_def algebra_simps scaleR_conv_of_real)
  1830   done
  1831 
  1832 lemma contour_integral_midpoint:
  1833    "continuous_on (closed_segment a b) f
  1834     \<Longrightarrow> contour_integral (linepath a b) f =
  1835         contour_integral (linepath a (midpoint a b)) f + contour_integral (linepath (midpoint a b) b) f"
  1836   apply (rule contour_integral_split [where c = "midpoint a b" and k = "1/2"])
  1837   apply (auto simp: midpoint_def algebra_simps scaleR_conv_of_real)
  1838   done
  1839 
  1840 
  1841 text\<open>A couple of special case lemmas that are useful below\<close>
  1842 
  1843 lemma triangle_linear_has_chain_integral:
  1844     "((\<lambda>x. m*x + d) has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  1845   apply (rule Cauchy_theorem_primitive [of UNIV "\<lambda>x. m/2 * x^2 + d*x"])
  1846   apply (auto intro!: derivative_eq_intros)
  1847   done
  1848 
  1849 lemma has_chain_integral_chain_integral3:
  1850      "(f has_contour_integral i) (linepath a b +++ linepath b c +++ linepath c d)
  1851       \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c d) f = i"
  1852   apply (subst contour_integral_unique [symmetric], assumption)
  1853   apply (drule has_contour_integral_integrable)
  1854   apply (simp add: valid_path_join)
  1855   done
  1856 
  1857 lemma has_chain_integral_chain_integral4:
  1858      "(f has_contour_integral i) (linepath a b +++ linepath b c +++ linepath c d +++ linepath d e)
  1859       \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c d) f + contour_integral (linepath d e) f = i"
  1860   apply (subst contour_integral_unique [symmetric], assumption)
  1861   apply (drule has_contour_integral_integrable)
  1862   apply (simp add: valid_path_join)
  1863   done
  1864 
  1865 subsection\<open>Reversing the order in a double path integral\<close>
  1866 
  1867 text\<open>The condition is stronger than needed but it's often true in typical situations\<close>
  1868 
  1869 lemma fst_im_cbox [simp]: "cbox c d \<noteq> {} \<Longrightarrow> (fst ` cbox (a,c) (b,d)) = cbox a b"
  1870   by (auto simp: cbox_Pair_eq)
  1871 
  1872 lemma snd_im_cbox [simp]: "cbox a b \<noteq> {} \<Longrightarrow> (snd ` cbox (a,c) (b,d)) = cbox c d"
  1873   by (auto simp: cbox_Pair_eq)
  1874 
  1875 lemma contour_integral_swap:
  1876   assumes fcon:  "continuous_on (path_image g \<times> path_image h) (\<lambda>(y1,y2). f y1 y2)"
  1877       and vp:    "valid_path g" "valid_path h"
  1878       and gvcon: "continuous_on {0..1} (\<lambda>t. vector_derivative g (at t))"
  1879       and hvcon: "continuous_on {0..1} (\<lambda>t. vector_derivative h (at t))"
  1880   shows "contour_integral g (\<lambda>w. contour_integral h (f w)) =
  1881          contour_integral h (\<lambda>z. contour_integral g (\<lambda>w. f w z))"
  1882 proof -
  1883   have gcon: "continuous_on {0..1} g" and hcon: "continuous_on {0..1} h"
  1884     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
  1885   have fgh1: "\<And>x. (\<lambda>t. f (g x) (h t)) = (\<lambda>(y1,y2). f y1 y2) o (\<lambda>t. (g x, h t))"
  1886     by (rule ext) simp
  1887   have fgh2: "\<And>x. (\<lambda>t. f (g t) (h x)) = (\<lambda>(y1,y2). f y1 y2) o (\<lambda>t. (g t, h x))"
  1888     by (rule ext) simp
  1889   have fcon_im1: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> continuous_on ((\<lambda>t. (g x, h t)) ` {0..1}) (\<lambda>(x, y). f x y)"
  1890     by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
  1891   have fcon_im2: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> continuous_on ((\<lambda>t. (g t, h x)) ` {0..1}) (\<lambda>(x, y). f x y)"
  1892     by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
  1893   have vdg: "\<And>y. y \<in> {0..1} \<Longrightarrow> (\<lambda>x. f (g x) (h y) * vector_derivative g (at x)) integrable_on {0..1}"
  1894     apply (rule integrable_continuous_real)
  1895     apply (rule continuous_on_mult [OF _ gvcon])
  1896     apply (subst fgh2)
  1897     apply (rule fcon_im2 gcon continuous_intros | simp)+
  1898     done
  1899   have "(\<lambda>z. vector_derivative g (at (fst z))) = (\<lambda>x. vector_derivative g (at x)) o fst"
  1900     by auto
  1901   then have gvcon': "continuous_on (cbox (0, 0) (1, 1::real)) (\<lambda>x. vector_derivative g (at (fst x)))"
  1902     apply (rule ssubst)
  1903     apply (rule continuous_intros | simp add: gvcon)+
  1904     done
  1905   have "(\<lambda>z. vector_derivative h (at (snd z))) = (\<lambda>x. vector_derivative h (at x)) o snd"
  1906     by auto
  1907   then have hvcon': "continuous_on (cbox (0, 0) (1::real, 1)) (\<lambda>x. vector_derivative h (at (snd x)))"
  1908     apply (rule ssubst)
  1909     apply (rule continuous_intros | simp add: hvcon)+
  1910     done
  1911   have "(\<lambda>x. f (g (fst x)) (h (snd x))) = (\<lambda>(y1,y2). f y1 y2) o (\<lambda>w. ((g o fst) w, (h o snd) w))"
  1912     by auto
  1913   then have fgh: "continuous_on (cbox (0, 0) (1, 1)) (\<lambda>x. f (g (fst x)) (h (snd x)))"
  1914     apply (rule ssubst)
  1915     apply (rule gcon hcon continuous_intros | simp)+
  1916     apply (auto simp: path_image_def intro: continuous_on_subset [OF fcon])
  1917     done
  1918   have "integral {0..1} (\<lambda>x. contour_integral h (f (g x)) * vector_derivative g (at x)) =
  1919         integral {0..1} (\<lambda>x. contour_integral h (\<lambda>y. f (g x) y * vector_derivative g (at x)))"
  1920     apply (rule integral_cong [OF contour_integral_rmul [symmetric]])
  1921     apply (clarsimp simp: contour_integrable_on)
  1922     apply (rule integrable_continuous_real)
  1923     apply (rule continuous_on_mult [OF _ hvcon])
  1924     apply (subst fgh1)
  1925     apply (rule fcon_im1 hcon continuous_intros | simp)+
  1926     done
  1927   also have "... = integral {0..1}
  1928                      (\<lambda>y. contour_integral g (\<lambda>x. f x (h y) * vector_derivative h (at y)))"
  1929     apply (simp only: contour_integral_integral)
  1930     apply (subst integral_swap_continuous [where 'a = real and 'b = real, of 0 0 1 1, simplified])
  1931      apply (rule fgh gvcon' hvcon' continuous_intros | simp add: split_def)+
  1932     unfolding integral_mult_left [symmetric]
  1933     apply (simp only: mult_ac)
  1934     done
  1935   also have "... = contour_integral h (\<lambda>z. contour_integral g (\<lambda>w. f w z))"
  1936     apply (simp add: contour_integral_integral)
  1937     apply (rule integral_cong)
  1938     unfolding integral_mult_left [symmetric]
  1939     apply (simp add: algebra_simps)
  1940     done
  1941   finally show ?thesis
  1942     by (simp add: contour_integral_integral)
  1943 qed
  1944 
  1945 
  1946 subsection\<open>The key quadrisection step\<close>
  1947 
  1948 lemma norm_sum_half:
  1949   assumes "norm(a + b) >= e"
  1950     shows "norm a >= e/2 \<or> norm b >= e/2"
  1951 proof -
  1952   have "e \<le> norm (- a - b)"
  1953     by (simp add: add.commute assms norm_minus_commute)
  1954   thus ?thesis
  1955     using norm_triangle_ineq4 order_trans by fastforce
  1956 qed
  1957 
  1958 lemma norm_sum_lemma:
  1959   assumes "e \<le> norm (a + b + c + d)"
  1960     shows "e / 4 \<le> norm a \<or> e / 4 \<le> norm b \<or> e / 4 \<le> norm c \<or> e / 4 \<le> norm d"
  1961 proof -
  1962   have "e \<le> norm ((a + b) + (c + d))" using assms
  1963     by (simp add: algebra_simps)
  1964   then show ?thesis
  1965     by (auto dest!: norm_sum_half)
  1966 qed
  1967 
  1968 lemma Cauchy_theorem_quadrisection:
  1969   assumes f: "continuous_on (convex hull {a,b,c}) f"
  1970       and dist: "dist a b \<le> K" "dist b c \<le> K" "dist c a \<le> K"
  1971       and e: "e * K^2 \<le>
  1972               norm (contour_integral(linepath a b) f + contour_integral(linepath b c) f + contour_integral(linepath c a) f)"
  1973   shows "\<exists>a' b' c'.
  1974            a' \<in> convex hull {a,b,c} \<and> b' \<in> convex hull {a,b,c} \<and> c' \<in> convex hull {a,b,c} \<and>
  1975            dist a' b' \<le> K/2  \<and>  dist b' c' \<le> K/2  \<and>  dist c' a' \<le> K/2  \<and>
  1976            e * (K/2)^2 \<le> norm(contour_integral(linepath a' b') f + contour_integral(linepath b' c') f + contour_integral(linepath c' a') f)"
  1977 proof -
  1978   note divide_le_eq_numeral1 [simp del]
  1979   define a' where "a' = midpoint b c"
  1980   define b' where "b' = midpoint c a"
  1981   define c' where "c' = midpoint a b"
  1982   have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
  1983     using f continuous_on_subset segments_subset_convex_hull by metis+
  1984   have fcont': "continuous_on (closed_segment c' b') f"
  1985                "continuous_on (closed_segment a' c') f"
  1986                "continuous_on (closed_segment b' a') f"
  1987     unfolding a'_def b'_def c'_def
  1988     apply (rule continuous_on_subset [OF f],
  1989            metis midpoints_in_convex_hull convex_hull_subset hull_subset insert_subset segment_convex_hull)+
  1990     done
  1991   let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  1992   have *: "?pathint a b + ?pathint b c + ?pathint c a =
  1993           (?pathint a c' + ?pathint c' b' + ?pathint b' a) +
  1994           (?pathint a' c' + ?pathint c' b + ?pathint b a') +
  1995           (?pathint a' c + ?pathint c b' + ?pathint b' a') +
  1996           (?pathint a' b' + ?pathint b' c' + ?pathint c' a')"
  1997     apply (simp add: fcont' contour_integral_reverse_linepath)
  1998     apply (simp add: a'_def b'_def c'_def contour_integral_midpoint fabc)
  1999     done
  2000   have [simp]: "\<And>x y. cmod (x * 2 - y * 2) = cmod (x - y) * 2"
  2001     by (metis left_diff_distrib mult.commute norm_mult_numeral1)
  2002   have [simp]: "\<And>x y. cmod (x - y) = cmod (y - x)"
  2003     by (simp add: norm_minus_commute)
  2004   consider "e * K\<^sup>2 / 4 \<le> cmod (?pathint a c' + ?pathint c' b' + ?pathint b' a)" |
  2005            "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' c' + ?pathint c' b + ?pathint b a')" |
  2006            "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' c + ?pathint c b' + ?pathint b' a')" |
  2007            "e * K\<^sup>2 / 4 \<le> cmod (?pathint a' b' + ?pathint b' c' + ?pathint c' a')"
  2008     using assms
  2009     apply (simp only: *)
  2010     apply (blast intro: that dest!: norm_sum_lemma)
  2011     done
  2012   then show ?thesis
  2013   proof cases
  2014     case 1 then show ?thesis
  2015       apply (rule_tac x=a in exI)
  2016       apply (rule exI [where x=c'])
  2017       apply (rule exI [where x=b'])
  2018       using assms
  2019       apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
  2020       apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
  2021       done
  2022   next
  2023     case 2 then show ?thesis
  2024       apply (rule_tac x=a' in exI)
  2025       apply (rule exI [where x=c'])
  2026       apply (rule exI [where x=b])
  2027       using assms
  2028       apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
  2029       apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
  2030       done
  2031   next
  2032     case 3 then show ?thesis
  2033       apply (rule_tac x=a' in exI)
  2034       apply (rule exI [where x=c])
  2035       apply (rule exI [where x=b'])
  2036       using assms
  2037       apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
  2038       apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
  2039       done
  2040   next
  2041     case 4 then show ?thesis
  2042       apply (rule_tac x=a' in exI)
  2043       apply (rule exI [where x=b'])
  2044       apply (rule exI [where x=c'])
  2045       using assms
  2046       apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
  2047       apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
  2048       done
  2049   qed
  2050 qed
  2051 
  2052 subsection\<open>Cauchy's theorem for triangles\<close>
  2053 
  2054 lemma triangle_points_closer:
  2055   fixes a::complex
  2056   shows "\<lbrakk>x \<in> convex hull {a,b,c};  y \<in> convex hull {a,b,c}\<rbrakk>
  2057          \<Longrightarrow> norm(x - y) \<le> norm(a - b) \<or>
  2058              norm(x - y) \<le> norm(b - c) \<or>
  2059              norm(x - y) \<le> norm(c - a)"
  2060   using simplex_extremal_le [of "{a,b,c}"]
  2061   by (auto simp: norm_minus_commute)
  2062 
  2063 lemma holomorphic_point_small_triangle:
  2064   assumes x: "x \<in> s"
  2065       and f: "continuous_on s f"
  2066       and cd: "f field_differentiable (at x within s)"
  2067       and e: "0 < e"
  2068     shows "\<exists>k>0. \<forall>a b c. dist a b \<le> k \<and> dist b c \<le> k \<and> dist c a \<le> k \<and>
  2069               x \<in> convex hull {a,b,c} \<and> convex hull {a,b,c} \<subseteq> s
  2070               \<longrightarrow> norm(contour_integral(linepath a b) f + contour_integral(linepath b c) f +
  2071                        contour_integral(linepath c a) f)
  2072                   \<le> e*(dist a b + dist b c + dist c a)^2"
  2073            (is "\<exists>k>0. \<forall>a b c. _ \<longrightarrow> ?normle a b c")
  2074 proof -
  2075   have le_of_3: "\<And>a x y z. \<lbrakk>0 \<le> x*y; 0 \<le> x*z; 0 \<le> y*z; a \<le> (e*(x + y + z))*x + (e*(x + y + z))*y + (e*(x + y + z))*z\<rbrakk>
  2076                      \<Longrightarrow> a \<le> e*(x + y + z)^2"
  2077     by (simp add: algebra_simps power2_eq_square)
  2078   have disj_le: "\<lbrakk>x \<le> a \<or> x \<le> b \<or> x \<le> c; 0 \<le> a; 0 \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> x \<le> a + b + c"
  2079              for x::real and a b c
  2080     by linarith
  2081   have fabc: "f contour_integrable_on linepath a b" "f contour_integrable_on linepath b c" "f contour_integrable_on linepath c a"
  2082               if "convex hull {a, b, c} \<subseteq> s" for a b c
  2083     using segments_subset_convex_hull that
  2084     by (metis continuous_on_subset f contour_integrable_continuous_linepath)+
  2085   note path_bound = has_contour_integral_bound_linepath [simplified norm_minus_commute, OF has_contour_integral_integral]
  2086   { fix f' a b c d
  2087     assume d: "0 < d"
  2088        and f': "\<And>y. \<lbrakk>cmod (y - x) \<le> d; y \<in> s\<rbrakk> \<Longrightarrow> cmod (f y - f x - f' * (y - x)) \<le> e * cmod (y - x)"
  2089        and le: "cmod (a - b) \<le> d" "cmod (b - c) \<le> d" "cmod (c - a) \<le> d"
  2090        and xc: "x \<in> convex hull {a, b, c}"
  2091        and s: "convex hull {a, b, c} \<subseteq> s"
  2092     have pa: "contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f =
  2093               contour_integral (linepath a b) (\<lambda>y. f y - f x - f'*(y - x)) +
  2094               contour_integral (linepath b c) (\<lambda>y. f y - f x - f'*(y - x)) +
  2095               contour_integral (linepath c a) (\<lambda>y. f y - f x - f'*(y - x))"
  2096       apply (simp add: contour_integral_diff contour_integral_lmul contour_integrable_lmul contour_integrable_diff fabc [OF s])
  2097       apply (simp add: field_simps)
  2098       done
  2099     { fix y
  2100       assume yc: "y \<in> convex hull {a,b,c}"
  2101       have "cmod (f y - f x - f' * (y - x)) \<le> e*norm(y - x)"
  2102         apply (rule f')
  2103         apply (metis triangle_points_closer [OF xc yc] le norm_minus_commute order_trans)
  2104         using s yc by blast
  2105       also have "... \<le> e * (cmod (a - b) + cmod (b - c) + cmod (c - a))"
  2106         by (simp add: yc e xc disj_le [OF triangle_points_closer])
  2107       finally have "cmod (f y - f x - f' * (y - x)) \<le> e * (cmod (a - b) + cmod (b - c) + cmod (c - a))" .
  2108     } note cm_le = this
  2109     have "?normle a b c"
  2110       apply (simp add: dist_norm pa)
  2111       apply (rule le_of_3)
  2112       using f' xc s e
  2113       apply simp_all
  2114       apply (intro norm_triangle_le add_mono path_bound)
  2115       apply (simp_all add: contour_integral_diff contour_integral_lmul contour_integrable_lmul contour_integrable_diff fabc)
  2116       apply (blast intro: cm_le elim: dest: segments_subset_convex_hull [THEN subsetD])+
  2117       done
  2118   } note * = this
  2119   show ?thesis
  2120     using cd e
  2121     apply (simp add: field_differentiable_def has_field_derivative_def has_derivative_within_alt approachable_lt_le2 Ball_def)
  2122     apply (clarify dest!: spec mp)
  2123     using *
  2124     apply (simp add: dist_norm, blast)
  2125     done
  2126 qed
  2127 
  2128 
  2129 (* Hence the most basic theorem for a triangle.*)
  2130 locale Chain =
  2131   fixes x0 At Follows
  2132   assumes At0: "At x0 0"
  2133       and AtSuc: "\<And>x n. At x n \<Longrightarrow> \<exists>x'. At x' (Suc n) \<and> Follows x' x"
  2134 begin
  2135   primrec f where
  2136     "f 0 = x0"
  2137   | "f (Suc n) = (SOME x. At x (Suc n) \<and> Follows x (f n))"
  2138 
  2139   lemma At: "At (f n) n"
  2140   proof (induct n)
  2141     case 0 show ?case
  2142       by (simp add: At0)
  2143   next
  2144     case (Suc n) show ?case
  2145       by (metis (no_types, lifting) AtSuc [OF Suc] f.simps(2) someI_ex)
  2146   qed
  2147 
  2148   lemma Follows: "Follows (f(Suc n)) (f n)"
  2149     by (metis (no_types, lifting) AtSuc [OF At [of n]] f.simps(2) someI_ex)
  2150 
  2151   declare f.simps(2) [simp del]
  2152 end
  2153 
  2154 lemma Chain3:
  2155   assumes At0: "At x0 y0 z0 0"
  2156       and AtSuc: "\<And>x y z n. At x y z n \<Longrightarrow> \<exists>x' y' z'. At x' y' z' (Suc n) \<and> Follows x' y' z' x y z"
  2157   obtains f g h where
  2158     "f 0 = x0" "g 0 = y0" "h 0 = z0"
  2159                       "\<And>n. At (f n) (g n) (h n) n"
  2160                        "\<And>n. Follows (f(Suc n)) (g(Suc n)) (h(Suc n)) (f n) (g n) (h n)"
  2161 proof -
  2162   interpret three: Chain "(x0,y0,z0)" "\<lambda>(x,y,z). At x y z" "\<lambda>(x',y',z'). \<lambda>(x,y,z). Follows x' y' z' x y z"
  2163     apply unfold_locales
  2164     using At0 AtSuc by auto
  2165   show ?thesis
  2166   apply (rule that [of "\<lambda>n. fst (three.f n)"  "\<lambda>n. fst (snd (three.f n))" "\<lambda>n. snd (snd (three.f n))"])
  2167   apply simp_all
  2168   using three.At three.Follows
  2169   apply (simp_all add: split_beta')
  2170   done
  2171 qed
  2172 
  2173 lemma Cauchy_theorem_triangle:
  2174   assumes "f holomorphic_on (convex hull {a,b,c})"
  2175     shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2176 proof -
  2177   have contf: "continuous_on (convex hull {a,b,c}) f"
  2178     by (metis assms holomorphic_on_imp_continuous_on)
  2179   let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2180   { fix y::complex
  2181     assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
  2182        and ynz: "y \<noteq> 0"
  2183     define K where "K = 1 + max (dist a b) (max (dist b c) (dist c a))"
  2184     define e where "e = norm y / K^2"
  2185     have K1: "K \<ge> 1"  by (simp add: K_def max.coboundedI1)
  2186     then have K: "K > 0" by linarith
  2187     have [iff]: "dist a b \<le> K" "dist b c \<le> K" "dist c a \<le> K"
  2188       by (simp_all add: K_def)
  2189     have e: "e > 0"
  2190       unfolding e_def using ynz K1 by simp
  2191     define At where "At x y z n \<longleftrightarrow>
  2192         convex hull {x,y,z} \<subseteq> convex hull {a,b,c} \<and>
  2193         dist x y \<le> K/2^n \<and> dist y z \<le> K/2^n \<and> dist z x \<le> K/2^n \<and>
  2194         norm(?pathint x y + ?pathint y z + ?pathint z x) \<ge> e*(K/2^n)^2"
  2195       for x y z n
  2196     have At0: "At a b c 0"
  2197       using fy
  2198       by (simp add: At_def e_def has_chain_integral_chain_integral3)
  2199     { fix x y z n
  2200       assume At: "At x y z n"
  2201       then have contf': "continuous_on (convex hull {x,y,z}) f"
  2202         using contf At_def continuous_on_subset by metis
  2203       have "\<exists>x' y' z'. At x' y' z' (Suc n) \<and> convex hull {x',y',z'} \<subseteq> convex hull {x,y,z}"
  2204         using At
  2205         apply (simp add: At_def)
  2206         using  Cauchy_theorem_quadrisection [OF contf', of "K/2^n" e]
  2207         apply clarsimp
  2208         apply (rule_tac x="a'" in exI)
  2209         apply (rule_tac x="b'" in exI)
  2210         apply (rule_tac x="c'" in exI)
  2211         apply (simp add: algebra_simps)
  2212         apply (meson convex_hull_subset empty_subsetI insert_subset subsetCE)
  2213         done
  2214     } note AtSuc = this
  2215     obtain fa fb fc
  2216       where f0 [simp]: "fa 0 = a" "fb 0 = b" "fc 0 = c"
  2217         and cosb: "\<And>n. convex hull {fa n, fb n, fc n} \<subseteq> convex hull {a,b,c}"
  2218         and dist: "\<And>n. dist (fa n) (fb n) \<le> K/2^n"
  2219                   "\<And>n. dist (fb n) (fc n) \<le> K/2^n"
  2220                   "\<And>n. dist (fc n) (fa n) \<le> K/2^n"
  2221         and no: "\<And>n. norm(?pathint (fa n) (fb n) +
  2222                            ?pathint (fb n) (fc n) +
  2223                            ?pathint (fc n) (fa n)) \<ge> e * (K/2^n)^2"
  2224         and conv_le: "\<And>n. convex hull {fa(Suc n), fb(Suc n), fc(Suc n)} \<subseteq> convex hull {fa n, fb n, fc n}"
  2225       apply (rule Chain3 [of At, OF At0 AtSuc])
  2226       apply (auto simp: At_def)
  2227       done
  2228     have "\<exists>x. \<forall>n. x \<in> convex hull {fa n, fb n, fc n}"
  2229       apply (rule bounded_closed_nest)
  2230       apply (simp_all add: compact_imp_closed finite_imp_compact_convex_hull finite_imp_bounded_convex_hull)
  2231       apply (rule allI)
  2232       apply (rule transitive_stepwise_le)
  2233       apply (auto simp: conv_le)
  2234       done
  2235     then obtain x where x: "\<And>n. x \<in> convex hull {fa n, fb n, fc n}" by auto
  2236     then have xin: "x \<in> convex hull {a,b,c}"
  2237       using assms f0 by blast
  2238     then have fx: "f field_differentiable at x within (convex hull {a,b,c})"
  2239       using assms holomorphic_on_def by blast
  2240     { fix k n
  2241       assume k: "0 < k"
  2242          and le:
  2243             "\<And>x' y' z'.
  2244                \<lbrakk>dist x' y' \<le> k; dist y' z' \<le> k; dist z' x' \<le> k;
  2245                 x \<in> convex hull {x',y',z'};
  2246                 convex hull {x',y',z'} \<subseteq> convex hull {a,b,c}\<rbrakk>
  2247                \<Longrightarrow>
  2248                cmod (?pathint x' y' + ?pathint y' z' + ?pathint z' x') * 10
  2249                      \<le> e * (dist x' y' + dist y' z' + dist z' x')\<^sup>2"
  2250          and Kk: "K / k < 2 ^ n"
  2251       have "K / 2 ^ n < k" using Kk k
  2252         by (auto simp: field_simps)
  2253       then have DD: "dist (fa n) (fb n) \<le> k" "dist (fb n) (fc n) \<le> k" "dist (fc n) (fa n) \<le> k"
  2254         using dist [of n]  k
  2255         by linarith+
  2256       have dle: "(dist (fa n) (fb n) + dist (fb n) (fc n) + dist (fc n) (fa n))\<^sup>2
  2257                \<le> (3 * K / 2 ^ n)\<^sup>2"
  2258         using dist [of n] e K
  2259         by (simp add: abs_le_square_iff [symmetric])
  2260       have less10: "\<And>x y::real. 0 < x \<Longrightarrow> y \<le> 9*x \<Longrightarrow> y < x*10"
  2261         by linarith
  2262       have "e * (dist (fa n) (fb n) + dist (fb n) (fc n) + dist (fc n) (fa n))\<^sup>2 \<le> e * (3 * K / 2 ^ n)\<^sup>2"
  2263         using ynz dle e mult_le_cancel_left_pos by blast
  2264       also have "... <
  2265           cmod (?pathint (fa n) (fb n) + ?pathint (fb n) (fc n) + ?pathint (fc n) (fa n)) * 10"
  2266         using no [of n] e K
  2267         apply (simp add: e_def field_simps)
  2268         apply (simp only: zero_less_norm_iff [symmetric])
  2269         done
  2270       finally have False
  2271         using le [OF DD x cosb] by auto
  2272     } then
  2273     have ?thesis
  2274       using holomorphic_point_small_triangle [OF xin contf fx, of "e/10"] e
  2275       apply clarsimp
  2276       apply (rule_tac y1="K/k" in exE [OF real_arch_pow[of 2]])
  2277       apply force+
  2278       done
  2279   }
  2280   moreover have "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
  2281     by simp (meson contf continuous_on_subset contour_integrable_continuous_linepath segments_subset_convex_hull(1)
  2282                    segments_subset_convex_hull(3) segments_subset_convex_hull(5))
  2283   ultimately show ?thesis
  2284     using has_contour_integral_integral by fastforce
  2285 qed
  2286 
  2287 
  2288 subsection\<open>Version needing function holomorphic in interior only\<close>
  2289 
  2290 lemma Cauchy_theorem_flat_lemma:
  2291   assumes f: "continuous_on (convex hull {a,b,c}) f"
  2292       and c: "c - a = k *\<^sub>R (b - a)"
  2293       and k: "0 \<le> k"
  2294     shows "contour_integral (linepath a b) f + contour_integral (linepath b c) f +
  2295           contour_integral (linepath c a) f = 0"
  2296 proof -
  2297   have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
  2298     using f continuous_on_subset segments_subset_convex_hull by metis+
  2299   show ?thesis
  2300   proof (cases "k \<le> 1")
  2301     case True show ?thesis
  2302       by (simp add: contour_integral_split [OF fabc(1) k True c] contour_integral_reverse_linepath fabc)
  2303   next
  2304     case False then show ?thesis
  2305       using fabc c
  2306       apply (subst contour_integral_split [of a c f "1/k" b, symmetric])
  2307       apply (metis closed_segment_commute fabc(3))
  2308       apply (auto simp: k contour_integral_reverse_linepath)
  2309       done
  2310   qed
  2311 qed
  2312 
  2313 lemma Cauchy_theorem_flat:
  2314   assumes f: "continuous_on (convex hull {a,b,c}) f"
  2315       and c: "c - a = k *\<^sub>R (b - a)"
  2316     shows "contour_integral (linepath a b) f +
  2317            contour_integral (linepath b c) f +
  2318            contour_integral (linepath c a) f = 0"
  2319 proof (cases "0 \<le> k")
  2320   case True with assms show ?thesis
  2321     by (blast intro: Cauchy_theorem_flat_lemma)
  2322 next
  2323   case False
  2324   have "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
  2325     using f continuous_on_subset segments_subset_convex_hull by metis+
  2326   moreover have "contour_integral (linepath b a) f + contour_integral (linepath a c) f +
  2327         contour_integral (linepath c b) f = 0"
  2328     apply (rule Cauchy_theorem_flat_lemma [of b a c f "1-k"])
  2329     using False c
  2330     apply (auto simp: f insert_commute scaleR_conv_of_real algebra_simps)
  2331     done
  2332   ultimately show ?thesis
  2333     apply (auto simp: contour_integral_reverse_linepath)
  2334     using add_eq_0_iff by force
  2335 qed
  2336 
  2337 
  2338 lemma Cauchy_theorem_triangle_interior:
  2339   assumes contf: "continuous_on (convex hull {a,b,c}) f"
  2340       and holf:  "f holomorphic_on interior (convex hull {a,b,c})"
  2341      shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2342 proof -
  2343   have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
  2344     using contf continuous_on_subset segments_subset_convex_hull by metis+
  2345   have "bounded (f ` (convex hull {a,b,c}))"
  2346     by (simp add: compact_continuous_image compact_convex_hull compact_imp_bounded contf)
  2347   then obtain B where "0 < B" and Bnf: "\<And>x. x \<in> convex hull {a,b,c} \<Longrightarrow> norm (f x) \<le> B"
  2348      by (auto simp: dest!: bounded_pos [THEN iffD1])
  2349   have "bounded (convex hull {a,b,c})"
  2350     by (simp add: bounded_convex_hull)
  2351   then obtain C where C: "0 < C" and Cno: "\<And>y. y \<in> convex hull {a,b,c} \<Longrightarrow> norm y < C"
  2352     using bounded_pos_less by blast
  2353   then have diff_2C: "norm(x - y) \<le> 2*C"
  2354            if x: "x \<in> convex hull {a, b, c}" and y: "y \<in> convex hull {a, b, c}" for x y
  2355   proof -
  2356     have "cmod x \<le> C"
  2357       using x by (meson Cno not_le not_less_iff_gr_or_eq)
  2358     hence "cmod (x - y) \<le> C + C"
  2359       using y by (meson Cno add_mono_thms_linordered_field(4) less_eq_real_def norm_triangle_ineq4 order_trans)
  2360     thus "cmod (x - y) \<le> 2 * C"
  2361       by (metis mult_2)
  2362   qed
  2363   have contf': "continuous_on (convex hull {b,a,c}) f"
  2364     using contf by (simp add: insert_commute)
  2365   { fix y::complex
  2366     assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
  2367        and ynz: "y \<noteq> 0"
  2368     have pi_eq_y: "contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f = y"
  2369       by (rule has_chain_integral_chain_integral3 [OF fy])
  2370     have ?thesis
  2371     proof (cases "c=a \<or> a=b \<or> b=c")
  2372       case True then show ?thesis
  2373         using Cauchy_theorem_flat [OF contf, of 0]
  2374         using has_chain_integral_chain_integral3 [OF fy] ynz
  2375         by (force simp: fabc contour_integral_reverse_linepath)
  2376     next
  2377       case False
  2378       then have car3: "card {a, b, c} = Suc (DIM(complex))"
  2379         by auto
  2380       { assume "interior(convex hull {a,b,c}) = {}"
  2381         then have "collinear{a,b,c}"
  2382           using interior_convex_hull_eq_empty [OF car3]
  2383           by (simp add: collinear_3_eq_affine_dependent)
  2384         then have "False"
  2385           using False
  2386           apply (clarsimp simp add: collinear_3 collinear_lemma)
  2387           apply (drule Cauchy_theorem_flat [OF contf'])
  2388           using pi_eq_y ynz
  2389           apply (simp add: fabc add_eq_0_iff contour_integral_reverse_linepath)
  2390           done
  2391       }
  2392       then obtain d where d: "d \<in> interior (convex hull {a, b, c})"
  2393         by blast
  2394       { fix d1
  2395         assume d1_pos: "0 < d1"
  2396            and d1: "\<And>x x'. \<lbrakk>x\<in>convex hull {a, b, c}; x'\<in>convex hull {a, b, c}; cmod (x' - x) < d1\<rbrakk>
  2397                            \<Longrightarrow> cmod (f x' - f x) < cmod y / (24 * C)"
  2398         define e where "e = min 1 (min (d1/(4*C)) ((norm y / 24 / C) / B))"
  2399         define shrink where "shrink x = x - e *\<^sub>R (x - d)" for x
  2400         let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2401         have e: "0 < e" "e \<le> 1" "e \<le> d1 / (4 * C)" "e \<le> cmod y / 24 / C / B"
  2402           using d1_pos \<open>C>0\<close> \<open>B>0\<close> ynz by (simp_all add: e_def)
  2403         then have eCB: "24 * e * C * B \<le> cmod y"
  2404           using \<open>C>0\<close> \<open>B>0\<close>  by (simp add: field_simps)
  2405         have e_le_d1: "e * (4 * C) \<le> d1"
  2406           using e \<open>C>0\<close> by (simp add: field_simps)
  2407         have "shrink a \<in> interior(convex hull {a,b,c})"
  2408              "shrink b \<in> interior(convex hull {a,b,c})"
  2409              "shrink c \<in> interior(convex hull {a,b,c})"
  2410           using d e by (auto simp: hull_inc mem_interior_convex_shrink shrink_def)
  2411         then have fhp0: "(f has_contour_integral 0)
  2412                 (linepath (shrink a) (shrink b) +++ linepath (shrink b) (shrink c) +++ linepath (shrink c) (shrink a))"
  2413           by (simp add: Cauchy_theorem_triangle holomorphic_on_subset [OF holf] hull_minimal convex_interior)
  2414         then have f_0_shrink: "?pathint (shrink a) (shrink b) + ?pathint (shrink b) (shrink c) + ?pathint (shrink c) (shrink a) = 0"
  2415           by (simp add: has_chain_integral_chain_integral3)
  2416         have fpi_abc: "f contour_integrable_on linepath (shrink a) (shrink b)"
  2417                       "f contour_integrable_on linepath (shrink b) (shrink c)"
  2418                       "f contour_integrable_on linepath (shrink c) (shrink a)"
  2419           using fhp0  by (auto simp: valid_path_join dest: has_contour_integral_integrable)
  2420         have cmod_shr: "\<And>x y. cmod (shrink y - shrink x - (y - x)) = e * cmod (x - y)"
  2421           using e by (simp add: shrink_def real_vector.scale_right_diff_distrib [symmetric])
  2422         have sh_eq: "\<And>a b d::complex. (b - e *\<^sub>R (b - d)) - (a - e *\<^sub>R (a - d)) - (b - a) = e *\<^sub>R (a - b)"
  2423           by (simp add: algebra_simps)
  2424         have "cmod y / (24 * C) \<le> cmod y / cmod (b - a) / 12"
  2425           using False \<open>C>0\<close> diff_2C [of b a] ynz
  2426           by (auto simp: divide_simps hull_inc)
  2427         have less_C: "\<lbrakk>u \<in> convex hull {a, b, c}; 0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> x * cmod u < C" for x u
  2428           apply (cases "x=0", simp add: \<open>0<C\<close>)
  2429           using Cno [of u] mult_left_le_one_le [of "cmod u" x] le_less_trans norm_ge_zero by blast
  2430         { fix u v
  2431           assume uv: "u \<in> convex hull {a, b, c}" "v \<in> convex hull {a, b, c}" "u\<noteq>v"
  2432              and fpi_uv: "f contour_integrable_on linepath (shrink u) (shrink v)"
  2433           have shr_uv: "shrink u \<in> interior(convex hull {a,b,c})"
  2434                        "shrink v \<in> interior(convex hull {a,b,c})"
  2435             using d e uv
  2436             by (auto simp: hull_inc mem_interior_convex_shrink shrink_def)
  2437           have cmod_fuv: "\<And>x. 0\<le>x \<Longrightarrow> x\<le>1 \<Longrightarrow> cmod (f (linepath (shrink u) (shrink v) x)) \<le> B"
  2438             using shr_uv by (blast intro: Bnf linepath_in_convex_hull interior_subset [THEN subsetD])
  2439           have By_uv: "B * (12 * (e * cmod (u - v))) \<le> cmod y"
  2440             apply (rule order_trans [OF _ eCB])
  2441             using e \<open>B>0\<close> diff_2C [of u v] uv
  2442             by (auto simp: field_simps)
  2443           { fix x::real   assume x: "0\<le>x" "x\<le>1"
  2444             have cmod_less_4C: "cmod ((1 - x) *\<^sub>R u - (1 - x) *\<^sub>R d) + cmod (x *\<^sub>R v - x *\<^sub>R d) < (C+C) + (C+C)"
  2445               apply (rule add_strict_mono; rule norm_triangle_half_l [of _ 0])
  2446               using uv x d interior_subset
  2447               apply (auto simp: hull_inc intro!: less_C)
  2448               done
  2449             have ll: "linepath (shrink u) (shrink v) x - linepath u v x = -e * ((1 - x) *\<^sub>R (u - d) + x *\<^sub>R (v - d))"
  2450               by (simp add: linepath_def shrink_def algebra_simps scaleR_conv_of_real)
  2451             have cmod_less_dt: "cmod (linepath (shrink u) (shrink v) x - linepath u v x) < d1"
  2452               using \<open>e>0\<close>
  2453               apply (simp add: ll norm_mult scaleR_diff_right)
  2454               apply (rule less_le_trans [OF _ e_le_d1])
  2455               using cmod_less_4C
  2456               apply (force intro: norm_triangle_lt)
  2457               done
  2458             have "cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) < cmod y / (24 * C)"
  2459               using x uv shr_uv cmod_less_dt
  2460               by (auto simp: hull_inc intro: d1 interior_subset [THEN subsetD] linepath_in_convex_hull)
  2461             also have "... \<le> cmod y / cmod (v - u) / 12"
  2462               using False uv \<open>C>0\<close> diff_2C [of v u] ynz
  2463               by (auto simp: divide_simps hull_inc)
  2464             finally have "cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) \<le> cmod y / cmod (v - u) / 12"
  2465               by simp
  2466             then have cmod_12_le: "cmod (v - u) * cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) * 12 \<le> cmod y"
  2467               using uv False by (auto simp: field_simps)
  2468             have "cmod (f (linepath (shrink u) (shrink v) x)) * cmod (shrink v - shrink u - (v - u)) +
  2469                   cmod (v - u) * cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x))
  2470                   \<le> cmod y / 6"
  2471               apply (rule order_trans [of _ "B*((norm y / 24 / C / B)*2*C) + (2*C)*(norm y /24 / C)"])
  2472               apply (rule add_mono [OF mult_mono])
  2473               using By_uv e \<open>0 < B\<close> \<open>0 < C\<close> x ynz
  2474               apply (simp_all add: cmod_fuv cmod_shr cmod_12_le hull_inc)
  2475               apply (simp add: field_simps)
  2476               done
  2477           } note cmod_diff_le = this
  2478           have f_uv: "continuous_on (closed_segment u v) f"
  2479             by (blast intro: uv continuous_on_subset [OF contf closed_segment_subset_convex_hull])
  2480           have **: "\<And>f' x' f x::complex. f'*x' - f*x = f'*(x' - x) + x*(f' - f)"
  2481             by (simp add: algebra_simps)
  2482           have "norm (?pathint (shrink u) (shrink v) - ?pathint u v) \<le> norm y / 6"
  2483             apply (rule order_trans)
  2484             apply (rule has_integral_bound
  2485                     [of "B*(norm y /24/C/B)*2*C + (2*C)*(norm y/24/C)"
  2486                         "\<lambda>x. f(linepath (shrink u) (shrink v) x) * (shrink v - shrink u) - f(linepath u v x)*(v - u)"
  2487                         _ 0 1 ])
  2488             using ynz \<open>0 < B\<close> \<open>0 < C\<close>
  2489             apply (simp_all del: le_divide_eq_numeral1)
  2490             apply (simp add: has_integral_sub has_contour_integral_linepath [symmetric] has_contour_integral_integral
  2491                              fpi_uv f_uv contour_integrable_continuous_linepath, clarify)
  2492             apply (simp only: **)
  2493             apply (simp add: norm_triangle_le norm_mult cmod_diff_le del: le_divide_eq_numeral1)
  2494             done
  2495           } note * = this
  2496           have "norm (?pathint (shrink a) (shrink b) - ?pathint a b) \<le> norm y / 6"
  2497             using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
  2498           moreover
  2499           have "norm (?pathint (shrink b) (shrink c) - ?pathint b c) \<le> norm y / 6"
  2500             using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
  2501           moreover
  2502           have "norm (?pathint (shrink c) (shrink a) - ?pathint c a) \<le> norm y / 6"
  2503             using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
  2504           ultimately
  2505           have "norm((?pathint (shrink a) (shrink b) - ?pathint a b) +
  2506                      (?pathint (shrink b) (shrink c) - ?pathint b c) + (?pathint (shrink c) (shrink a) - ?pathint c a))
  2507                 \<le> norm y / 6 + norm y / 6 + norm y / 6"
  2508             by (metis norm_triangle_le add_mono)
  2509           also have "... = norm y / 2"
  2510             by simp
  2511           finally have "norm((?pathint (shrink a) (shrink b) + ?pathint (shrink b) (shrink c) + ?pathint (shrink c) (shrink a)) -
  2512                           (?pathint a b + ?pathint b c + ?pathint c a))
  2513                 \<le> norm y / 2"
  2514             by (simp add: algebra_simps)
  2515           then
  2516           have "norm(?pathint a b + ?pathint b c + ?pathint c a) \<le> norm y / 2"
  2517             by (simp add: f_0_shrink) (metis (mono_tags) add.commute minus_add_distrib norm_minus_cancel uminus_add_conv_diff)
  2518           then have "False"
  2519             using pi_eq_y ynz by auto
  2520         }
  2521         moreover have "uniformly_continuous_on (convex hull {a,b,c}) f"
  2522           by (simp add: contf compact_convex_hull compact_uniformly_continuous)
  2523         ultimately have "False"
  2524           unfolding uniformly_continuous_on_def
  2525           by (force simp: ynz \<open>0 < C\<close> dist_norm)
  2526         then show ?thesis ..
  2527       qed
  2528   }
  2529   moreover have "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
  2530     using fabc contour_integrable_continuous_linepath by auto
  2531   ultimately show ?thesis
  2532     using has_contour_integral_integral by fastforce
  2533 qed
  2534 
  2535 
  2536 subsection\<open>Version allowing finite number of exceptional points\<close>
  2537 
  2538 lemma Cauchy_theorem_triangle_cofinite:
  2539   assumes "continuous_on (convex hull {a,b,c}) f"
  2540       and "finite s"
  2541       and "(\<And>x. x \<in> interior(convex hull {a,b,c}) - s \<Longrightarrow> f field_differentiable (at x))"
  2542      shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2543 using assms
  2544 proof (induction "card s" arbitrary: a b c s rule: less_induct)
  2545   case (less s a b c)
  2546   show ?case
  2547   proof (cases "s={}")
  2548     case True with less show ?thesis
  2549       by (fastforce simp: holomorphic_on_def field_differentiable_at_within
  2550                     Cauchy_theorem_triangle_interior)
  2551   next
  2552     case False
  2553     then obtain d s' where d: "s = insert d s'" "d \<notin> s'"
  2554       by (meson Set.set_insert all_not_in_conv)
  2555     then show ?thesis
  2556     proof (cases "d \<in> convex hull {a,b,c}")
  2557       case False
  2558       show "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2559         apply (rule less.hyps [of "s'"])
  2560         using False d \<open>finite s\<close> interior_subset
  2561         apply (auto intro!: less.prems)
  2562         done
  2563     next
  2564       case True
  2565       have *: "convex hull {a, b, d} \<subseteq> convex hull {a, b, c}"
  2566         by (meson True hull_subset insert_subset convex_hull_subset)
  2567       have abd: "(f has_contour_integral 0) (linepath a b +++ linepath b d +++ linepath d a)"
  2568         apply (rule less.hyps [of "s'"])
  2569         using True d  \<open>finite s\<close> not_in_interior_convex_hull_3
  2570         apply (auto intro!: less.prems continuous_on_subset [OF  _ *])
  2571         apply (metis * insert_absorb insert_subset interior_mono)
  2572         done
  2573       have *: "convex hull {b, c, d} \<subseteq> convex hull {a, b, c}"
  2574         by (meson True hull_subset insert_subset convex_hull_subset)
  2575       have bcd: "(f has_contour_integral 0) (linepath b c +++ linepath c d +++ linepath d b)"
  2576         apply (rule less.hyps [of "s'"])
  2577         using True d  \<open>finite s\<close> not_in_interior_convex_hull_3
  2578         apply (auto intro!: less.prems continuous_on_subset [OF _ *])
  2579         apply (metis * insert_absorb insert_subset interior_mono)
  2580         done
  2581       have *: "convex hull {c, a, d} \<subseteq> convex hull {a, b, c}"
  2582         by (meson True hull_subset insert_subset convex_hull_subset)
  2583       have cad: "(f has_contour_integral 0) (linepath c a +++ linepath a d +++ linepath d c)"
  2584         apply (rule less.hyps [of "s'"])
  2585         using True d  \<open>finite s\<close> not_in_interior_convex_hull_3
  2586         apply (auto intro!: less.prems continuous_on_subset [OF _ *])
  2587         apply (metis * insert_absorb insert_subset interior_mono)
  2588         done
  2589       have "f contour_integrable_on linepath a b"
  2590         using less.prems
  2591         by (metis continuous_on_subset insert_commute contour_integrable_continuous_linepath segments_subset_convex_hull(3))
  2592       moreover have "f contour_integrable_on linepath b c"
  2593         using less.prems
  2594         by (metis continuous_on_subset contour_integrable_continuous_linepath segments_subset_convex_hull(3))
  2595       moreover have "f contour_integrable_on linepath c a"
  2596         using less.prems
  2597         by (metis continuous_on_subset insert_commute contour_integrable_continuous_linepath segments_subset_convex_hull(3))
  2598       ultimately have fpi: "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
  2599         by auto
  2600       { fix y::complex
  2601         assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
  2602            and ynz: "y \<noteq> 0"
  2603         have cont_ad: "continuous_on (closed_segment a d) f"
  2604           by (meson "*" continuous_on_subset less.prems(1) segments_subset_convex_hull(3))
  2605         have cont_bd: "continuous_on (closed_segment b d) f"
  2606           by (meson True closed_segment_subset_convex_hull continuous_on_subset hull_subset insert_subset less.prems(1))
  2607         have cont_cd: "continuous_on (closed_segment c d) f"
  2608           by (meson "*" continuous_on_subset less.prems(1) segments_subset_convex_hull(2))
  2609         have "contour_integral  (linepath a b) f = - (contour_integral (linepath b d) f + (contour_integral (linepath d a) f))"
  2610                 "contour_integral  (linepath b c) f = - (contour_integral (linepath c d) f + (contour_integral (linepath d b) f))"
  2611                 "contour_integral  (linepath c a) f = - (contour_integral (linepath a d) f + contour_integral (linepath d c) f)"
  2612             using has_chain_integral_chain_integral3 [OF abd]
  2613                   has_chain_integral_chain_integral3 [OF bcd]
  2614                   has_chain_integral_chain_integral3 [OF cad]
  2615             by (simp_all add: algebra_simps add_eq_0_iff)
  2616         then have ?thesis
  2617           using cont_ad cont_bd cont_cd fy has_chain_integral_chain_integral3 contour_integral_reverse_linepath by fastforce
  2618       }
  2619       then show ?thesis
  2620         using fpi contour_integrable_on_def by blast
  2621     qed
  2622   qed
  2623 qed
  2624 
  2625 
  2626 subsection\<open>Cauchy's theorem for an open starlike set\<close>
  2627 
  2628 lemma starlike_convex_subset:
  2629   assumes s: "a \<in> s" "closed_segment b c \<subseteq> s" and subs: "\<And>x. x \<in> s \<Longrightarrow> closed_segment a x \<subseteq> s"
  2630     shows "convex hull {a,b,c} \<subseteq> s"
  2631       using s
  2632       apply (clarsimp simp add: convex_hull_insert [of "{b,c}" a] segment_convex_hull)
  2633       apply (meson subs convexD convex_closed_segment ends_in_segment(1) ends_in_segment(2) subsetCE)
  2634       done
  2635 
  2636 lemma triangle_contour_integrals_starlike_primitive:
  2637   assumes contf: "continuous_on s f"
  2638       and s: "a \<in> s" "open s"
  2639       and x: "x \<in> s"
  2640       and subs: "\<And>y. y \<in> s \<Longrightarrow> closed_segment a y \<subseteq> s"
  2641       and zer: "\<And>b c. closed_segment b c \<subseteq> s
  2642                    \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f +
  2643                        contour_integral (linepath c a) f = 0"
  2644     shows "((\<lambda>x. contour_integral(linepath a x) f) has_field_derivative f x) (at x)"
  2645 proof -
  2646   let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2647   { fix e y
  2648     assume e: "0 < e" and bxe: "ball x e \<subseteq> s" and close: "cmod (y - x) < e"
  2649     have y: "y \<in> s"
  2650       using bxe close  by (force simp: dist_norm norm_minus_commute)
  2651     have cont_ayf: "continuous_on (closed_segment a y) f"
  2652       using contf continuous_on_subset subs y by blast
  2653     have xys: "closed_segment x y \<subseteq> s"
  2654       apply (rule order_trans [OF _ bxe])
  2655       using close
  2656       by (auto simp: dist_norm ball_def norm_minus_commute dest: segment_bound)
  2657     have "?pathint a y - ?pathint a x = ?pathint x y"
  2658       using zer [OF xys]  contour_integral_reverse_linepath [OF cont_ayf]  add_eq_0_iff by force
  2659   } note [simp] = this
  2660   { fix e::real
  2661     assume e: "0 < e"
  2662     have cont_atx: "continuous (at x) f"
  2663       using x s contf continuous_on_eq_continuous_at by blast
  2664     then obtain d1 where d1: "d1>0" and d1_less: "\<And>y. cmod (y - x) < d1 \<Longrightarrow> cmod (f y - f x) < e/2"
  2665       unfolding continuous_at Lim_at dist_norm  using e
  2666       by (drule_tac x="e/2" in spec) force
  2667     obtain d2 where d2: "d2>0" "ball x d2 \<subseteq> s" using  \<open>open s\<close> x
  2668       by (auto simp: open_contains_ball)
  2669     have dpos: "min d1 d2 > 0" using d1 d2 by simp
  2670     { fix y
  2671       assume yx: "y \<noteq> x" and close: "cmod (y - x) < min d1 d2"
  2672       have y: "y \<in> s"
  2673         using d2 close  by (force simp: dist_norm norm_minus_commute)
  2674       have fxy: "f contour_integrable_on linepath x y"
  2675         apply (rule contour_integrable_continuous_linepath)
  2676         apply (rule continuous_on_subset [OF contf])
  2677         using close d2
  2678         apply (auto simp: dist_norm norm_minus_commute dest!: segment_bound(1))
  2679         done
  2680       then obtain i where i: "(f has_contour_integral i) (linepath x y)"
  2681         by (auto simp: contour_integrable_on_def)
  2682       then have "((\<lambda>w. f w - f x) has_contour_integral (i - f x * (y - x))) (linepath x y)"
  2683         by (rule has_contour_integral_diff [OF _ has_contour_integral_const_linepath])
  2684       then have "cmod (i - f x * (y - x)) \<le> e / 2 * cmod (y - x)"
  2685         apply (rule has_contour_integral_bound_linepath [where B = "e/2"])
  2686         using e apply simp
  2687         apply (rule d1_less [THEN less_imp_le])
  2688         using close segment_bound
  2689         apply force
  2690         done
  2691       also have "... < e * cmod (y - x)"
  2692         by (simp add: e yx)
  2693       finally have "cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
  2694         using i yx  by (simp add: contour_integral_unique divide_less_eq)
  2695     }
  2696     then have "\<exists>d>0. \<forall>y. y \<noteq> x \<and> cmod (y-x) < d \<longrightarrow> cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
  2697       using dpos by blast
  2698   }
  2699   then have *: "(\<lambda>y. (?pathint x y - f x * (y - x)) /\<^sub>R cmod (y - x)) \<midarrow>x\<rightarrow> 0"
  2700     by (simp add: Lim_at dist_norm inverse_eq_divide)
  2701   show ?thesis
  2702     apply (simp add: has_field_derivative_def has_derivative_at bounded_linear_mult_right)
  2703     apply (rule Lim_transform [OF * Lim_eventually])
  2704     apply (simp add: inverse_eq_divide [symmetric] eventually_at)
  2705     using \<open>open s\<close> x
  2706     apply (force simp: dist_norm open_contains_ball)
  2707     done
  2708 qed
  2709 
  2710 (** Existence of a primitive.*)
  2711 
  2712 lemma holomorphic_starlike_primitive:
  2713   fixes f :: "complex \<Rightarrow> complex"
  2714   assumes contf: "continuous_on s f"
  2715       and s: "starlike s" and os: "open s"
  2716       and k: "finite k"
  2717       and fcd: "\<And>x. x \<in> s - k \<Longrightarrow> f field_differentiable at x"
  2718     shows "\<exists>g. \<forall>x \<in> s. (g has_field_derivative f x) (at x)"
  2719 proof -
  2720   obtain a where a: "a\<in>s" and a_cs: "\<And>x. x\<in>s \<Longrightarrow> closed_segment a x \<subseteq> s"
  2721     using s by (auto simp: starlike_def)
  2722   { fix x b c
  2723     assume "x \<in> s" "closed_segment b c \<subseteq> s"
  2724     then have abcs: "convex hull {a, b, c} \<subseteq> s"
  2725       by (simp add: a a_cs starlike_convex_subset)
  2726     then have *: "continuous_on (convex hull {a, b, c}) f"
  2727       by (simp add: continuous_on_subset [OF contf])
  2728     have "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2729       apply (rule Cauchy_theorem_triangle_cofinite [OF _ k])
  2730       using abcs apply (simp add: continuous_on_subset [OF contf])
  2731       using * abcs interior_subset apply (auto intro: fcd)
  2732       done
  2733   } note 0 = this
  2734   show ?thesis
  2735     apply (intro exI ballI)
  2736     apply (rule triangle_contour_integrals_starlike_primitive [OF contf a os], assumption)
  2737     apply (metis a_cs)
  2738     apply (metis has_chain_integral_chain_integral3 0)
  2739     done
  2740 qed
  2741 
  2742 lemma Cauchy_theorem_starlike:
  2743  "\<lbrakk>open s; starlike s; finite k; continuous_on s f;
  2744    \<And>x. x \<in> s - k \<Longrightarrow> f field_differentiable at x;
  2745    valid_path g; path_image g \<subseteq> s; pathfinish g = pathstart g\<rbrakk>
  2746    \<Longrightarrow> (f has_contour_integral 0)  g"
  2747   by (metis holomorphic_starlike_primitive Cauchy_theorem_primitive at_within_open)
  2748 
  2749 lemma Cauchy_theorem_starlike_simple:
  2750   "\<lbrakk>open s; starlike s; f holomorphic_on s; valid_path g; path_image g \<subseteq> s; pathfinish g = pathstart g\<rbrakk>
  2751    \<Longrightarrow> (f has_contour_integral 0) g"
  2752 apply (rule Cauchy_theorem_starlike [OF _ _ finite.emptyI])
  2753 apply (simp_all add: holomorphic_on_imp_continuous_on)
  2754 apply (metis at_within_open holomorphic_on_def)
  2755 done
  2756 
  2757 
  2758 subsection\<open>Cauchy's theorem for a convex set\<close>
  2759 
  2760 text\<open>For a convex set we can avoid assuming openness and boundary analyticity\<close>
  2761 
  2762 lemma triangle_contour_integrals_convex_primitive:
  2763   assumes contf: "continuous_on s f"
  2764       and s: "a \<in> s" "convex s"
  2765       and x: "x \<in> s"
  2766       and zer: "\<And>b c. \<lbrakk>b \<in> s; c \<in> s\<rbrakk>
  2767                    \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f +
  2768                        contour_integral (linepath c a) f = 0"
  2769     shows "((\<lambda>x. contour_integral(linepath a x) f) has_field_derivative f x) (at x within s)"
  2770 proof -
  2771   let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2772   { fix y
  2773     assume y: "y \<in> s"
  2774     have cont_ayf: "continuous_on (closed_segment a y) f"
  2775       using s y  by (meson contf continuous_on_subset convex_contains_segment)
  2776     have xys: "closed_segment x y \<subseteq> s"  (*?*)
  2777       using convex_contains_segment s x y by auto
  2778     have "?pathint a y - ?pathint a x = ?pathint x y"
  2779       using zer [OF x y]  contour_integral_reverse_linepath [OF cont_ayf]  add_eq_0_iff by force
  2780   } note [simp] = this
  2781   { fix e::real
  2782     assume e: "0 < e"
  2783     have cont_atx: "continuous (at x within s) f"
  2784       using x s contf  by (simp add: continuous_on_eq_continuous_within)
  2785     then obtain d1 where d1: "d1>0" and d1_less: "\<And>y. \<lbrakk>y \<in> s; cmod (y - x) < d1\<rbrakk> \<Longrightarrow> cmod (f y - f x) < e/2"
  2786       unfolding continuous_within Lim_within dist_norm using e
  2787       by (drule_tac x="e/2" in spec) force
  2788     { fix y
  2789       assume yx: "y \<noteq> x" and close: "cmod (y - x) < d1" and y: "y \<in> s"
  2790       have fxy: "f contour_integrable_on linepath x y"
  2791         using convex_contains_segment s x y
  2792         by (blast intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
  2793       then obtain i where i: "(f has_contour_integral i) (linepath x y)"
  2794         by (auto simp: contour_integrable_on_def)
  2795       then have "((\<lambda>w. f w - f x) has_contour_integral (i - f x * (y - x))) (linepath x y)"
  2796         by (rule has_contour_integral_diff [OF _ has_contour_integral_const_linepath])
  2797       then have "cmod (i - f x * (y - x)) \<le> e / 2 * cmod (y - x)"
  2798         apply (rule has_contour_integral_bound_linepath [where B = "e/2"])
  2799         using e apply simp
  2800         apply (rule d1_less [THEN less_imp_le])
  2801         using convex_contains_segment s(2) x y apply blast
  2802         using close segment_bound(1) apply fastforce
  2803         done
  2804       also have "... < e * cmod (y - x)"
  2805         by (simp add: e yx)
  2806       finally have "cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
  2807         using i yx  by (simp add: contour_integral_unique divide_less_eq)
  2808     }
  2809     then have "\<exists>d>0. \<forall>y\<in>s. y \<noteq> x \<and> cmod (y-x) < d \<longrightarrow> cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
  2810       using d1 by blast
  2811   }
  2812   then have *: "((\<lambda>y. (contour_integral (linepath x y) f - f x * (y - x)) /\<^sub>R cmod (y - x)) \<longlongrightarrow> 0) (at x within s)"
  2813     by (simp add: Lim_within dist_norm inverse_eq_divide)
  2814   show ?thesis
  2815     apply (simp add: has_field_derivative_def has_derivative_within bounded_linear_mult_right)
  2816     apply (rule Lim_transform [OF * Lim_eventually])
  2817     using linordered_field_no_ub
  2818     apply (force simp: inverse_eq_divide [symmetric] eventually_at)
  2819     done
  2820 qed
  2821 
  2822 lemma contour_integral_convex_primitive:
  2823   "\<lbrakk>convex s; continuous_on s f;
  2824     \<And>a b c. \<lbrakk>a \<in> s; b \<in> s; c \<in> s\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)\<rbrakk>
  2825          \<Longrightarrow> \<exists>g. \<forall>x \<in> s. (g has_field_derivative f x) (at x within s)"
  2826   apply (cases "s={}")
  2827   apply (simp_all add: ex_in_conv [symmetric])
  2828   apply (blast intro: triangle_contour_integrals_convex_primitive has_chain_integral_chain_integral3)
  2829   done
  2830 
  2831 lemma holomorphic_convex_primitive:
  2832   fixes f :: "complex \<Rightarrow> complex"
  2833   shows
  2834   "\<lbrakk>convex s; finite k; continuous_on s f;
  2835     \<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x\<rbrakk>
  2836    \<Longrightarrow> \<exists>g. \<forall>x \<in> s. (g has_field_derivative f x) (at x within s)"
  2837 apply (rule contour_integral_convex_primitive [OF _ _ Cauchy_theorem_triangle_cofinite])
  2838 prefer 3
  2839 apply (erule continuous_on_subset)
  2840 apply (simp add: subset_hull continuous_on_subset, assumption+)
  2841 by (metis Diff_iff convex_contains_segment insert_absorb insert_subset interior_mono segment_convex_hull subset_hull)
  2842 
  2843 lemma Cauchy_theorem_convex:
  2844     "\<lbrakk>continuous_on s f; convex s; finite k;
  2845       \<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x;
  2846      valid_path g; path_image g \<subseteq> s;
  2847      pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
  2848   by (metis holomorphic_convex_primitive Cauchy_theorem_primitive)
  2849 
  2850 lemma Cauchy_theorem_convex_simple:
  2851     "\<lbrakk>f holomorphic_on s; convex s;
  2852      valid_path g; path_image g \<subseteq> s;
  2853      pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
  2854   apply (rule Cauchy_theorem_convex)
  2855   apply (simp_all add: holomorphic_on_imp_continuous_on)
  2856   apply (rule finite.emptyI)
  2857   using at_within_interior holomorphic_on_def interior_subset by fastforce
  2858 
  2859 
  2860 text\<open>In particular for a disc\<close>
  2861 lemma Cauchy_theorem_disc:
  2862     "\<lbrakk>finite k; continuous_on (cball a e) f;
  2863       \<And>x. x \<in> ball a e - k \<Longrightarrow> f field_differentiable at x;
  2864      valid_path g; path_image g \<subseteq> cball a e;
  2865      pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
  2866   apply (rule Cauchy_theorem_convex)
  2867   apply (auto simp: convex_cball interior_cball)
  2868   done
  2869 
  2870 lemma Cauchy_theorem_disc_simple:
  2871     "\<lbrakk>f holomorphic_on (ball a e); valid_path g; path_image g \<subseteq> ball a e;
  2872      pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
  2873 by (simp add: Cauchy_theorem_convex_simple)
  2874 
  2875 
  2876 subsection\<open>Generalize integrability to local primitives\<close>
  2877 
  2878 lemma contour_integral_local_primitive_lemma:
  2879   fixes f :: "complex\<Rightarrow>complex"
  2880   shows
  2881     "\<lbrakk>g piecewise_differentiable_on {a..b};
  2882       \<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s);
  2883       \<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s\<rbrakk>
  2884      \<Longrightarrow> (\<lambda>x. f' (g x) * vector_derivative g (at x within {a..b}))
  2885             integrable_on {a..b}"
  2886   apply (cases "cbox a b = {}", force)
  2887   apply (simp add: integrable_on_def)
  2888   apply (rule exI)
  2889   apply (rule contour_integral_primitive_lemma, assumption+)
  2890   using atLeastAtMost_iff by blast
  2891 
  2892 lemma contour_integral_local_primitive_any:
  2893   fixes f :: "complex \<Rightarrow> complex"
  2894   assumes gpd: "g piecewise_differentiable_on {a..b}"
  2895       and dh: "\<And>x. x \<in> s
  2896                \<Longrightarrow> \<exists>d h. 0 < d \<and>
  2897                          (\<forall>y. norm(y - x) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
  2898       and gs: "\<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s"
  2899   shows "(\<lambda>x. f(g x) * vector_derivative g (at x)) integrable_on {a..b}"
  2900 proof -
  2901   { fix x
  2902     assume x: "a \<le> x" "x \<le> b"
  2903     obtain d h where d: "0 < d"
  2904                and h: "(\<And>y. norm(y - g x) < d \<Longrightarrow> (h has_field_derivative f y) (at y within s))"
  2905       using x gs dh by (metis atLeastAtMost_iff)
  2906     have "continuous_on {a..b} g" using gpd piecewise_differentiable_on_def by blast
  2907     then obtain e where e: "e>0" and lessd: "\<And>x'. x' \<in> {a..b} \<Longrightarrow> \<bar>x' - x\<bar> < e \<Longrightarrow> cmod (g x' - g x) < d"
  2908       using x d
  2909       apply (auto simp: dist_norm continuous_on_iff)
  2910       apply (drule_tac x=x in bspec)
  2911       using x apply simp
  2912       apply (drule_tac x=d in spec, auto)
  2913       done
  2914     have "\<exists>d>0. \<forall>u v. u \<le> x \<and> x \<le> v \<and> {u..v} \<subseteq> ball x d \<and> (u \<le> v \<longrightarrow> a \<le> u \<and> v \<le> b) \<longrightarrow>
  2915                           (\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on {u..v}"
  2916       apply (rule_tac x=e in exI)
  2917       using e
  2918       apply (simp add: integrable_on_localized_vector_derivative [symmetric], clarify)
  2919       apply (rule_tac f = h and s = "g ` {u..v}" in contour_integral_local_primitive_lemma)
  2920         apply (meson atLeastatMost_subset_iff gpd piecewise_differentiable_on_subset)
  2921        apply (force simp: ball_def dist_norm intro: lessd gs DERIV_subset [OF h], force)
  2922       done
  2923   } then
  2924   show ?thesis
  2925     by (force simp: intro!: integrable_on_little_subintervals [of a b, simplified])
  2926 qed
  2927 
  2928 lemma contour_integral_local_primitive:
  2929   fixes f :: "complex \<Rightarrow> complex"
  2930   assumes g: "valid_path g" "path_image g \<subseteq> s"
  2931       and dh: "\<And>x. x \<in> s
  2932                \<Longrightarrow> \<exists>d h. 0 < d \<and>
  2933                          (\<forall>y. norm(y - x) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
  2934   shows "f contour_integrable_on g"
  2935   using g
  2936   apply (simp add: valid_path_def path_image_def contour_integrable_on_def has_contour_integral_def
  2937             has_integral_localized_vector_derivative integrable_on_def [symmetric])
  2938   using contour_integral_local_primitive_any [OF _ dh]
  2939   by (meson image_subset_iff piecewise_C1_imp_differentiable)
  2940 
  2941 
  2942 text\<open>In particular if a function is holomorphic\<close>
  2943 
  2944 lemma contour_integrable_holomorphic:
  2945   assumes contf: "continuous_on s f"
  2946       and os: "open s"
  2947       and k: "finite k"
  2948       and g: "valid_path g" "path_image g \<subseteq> s"
  2949       and fcd: "\<And>x. x \<in> s - k \<Longrightarrow> f field_differentiable at x"
  2950     shows "f contour_integrable_on g"
  2951 proof -
  2952   { fix z
  2953     assume z: "z \<in> s"
  2954     obtain d where d: "d>0" "ball z d \<subseteq> s" using  \<open>open s\<close> z
  2955       by (auto simp: open_contains_ball)
  2956     then have contfb: "continuous_on (ball z d) f"
  2957       using contf continuous_on_subset by blast
  2958     obtain h where "\<forall>y\<in>ball z d. (h has_field_derivative f y) (at y within ball z d)"
  2959       using holomorphic_convex_primitive [OF convex_ball k contfb fcd] d
  2960             interior_subset by force
  2961     then have "\<forall>y\<in>ball z d. (h has_field_derivative f y) (at y within s)"
  2962       by (metis Topology_Euclidean_Space.open_ball at_within_open d(2) os subsetCE)
  2963     then have "\<exists>h. (\<forall>y. cmod (y - z) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
  2964       by (force simp: dist_norm norm_minus_commute)
  2965     then have "\<exists>d h. 0 < d \<and> (\<forall>y. cmod (y - z) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
  2966       using d by blast
  2967   }
  2968   then show ?thesis
  2969     by (rule contour_integral_local_primitive [OF g])
  2970 qed
  2971 
  2972 lemma contour_integrable_holomorphic_simple:
  2973   assumes fh: "f holomorphic_on s"
  2974       and os: "open s"
  2975       and g: "valid_path g" "path_image g \<subseteq> s"
  2976     shows "f contour_integrable_on g"
  2977   apply (rule contour_integrable_holomorphic [OF _ os Finite_Set.finite.emptyI g])
  2978   apply (simp add: fh holomorphic_on_imp_continuous_on)
  2979   using fh  by (simp add: field_differentiable_def holomorphic_on_open os)
  2980 
  2981 lemma continuous_on_inversediff:
  2982   fixes z:: "'a::real_normed_field" shows "z \<notin> s \<Longrightarrow> continuous_on s (\<lambda>w. 1 / (w - z))"
  2983   by (rule continuous_intros | force)+
  2984 
  2985 corollary contour_integrable_inversediff:
  2986     "\<lbrakk>valid_path g; z \<notin> path_image g\<rbrakk> \<Longrightarrow> (\<lambda>w. 1 / (w-z)) contour_integrable_on g"
  2987 apply (rule contour_integrable_holomorphic_simple [of _ "UNIV-{z}"])
  2988 apply (auto simp: holomorphic_on_open open_delete intro!: derivative_eq_intros)
  2989 done
  2990 
  2991 text\<open>Key fact that path integral is the same for a "nearby" path. This is the
  2992  main lemma for the homotopy form of Cauchy's theorem and is also useful
  2993  if we want "without loss of generality" to assume some nice properties of a
  2994  path (e.g. smoothness). It can also be used to define the integrals of
  2995  analytic functions over arbitrary continuous paths. This is just done for
  2996  winding numbers now.
  2997 \<close>
  2998 
  2999 text\<open>A technical definition to avoid duplication of similar proofs,
  3000      for paths joined at the ends versus looping paths\<close>
  3001 definition linked_paths :: "bool \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> (real \<Rightarrow> 'a::topological_space) \<Rightarrow> bool"
  3002   where "linked_paths atends g h ==
  3003         (if atends then pathstart h = pathstart g \<and> pathfinish h = pathfinish g
  3004                    else pathfinish g = pathstart g \<and> pathfinish h = pathstart h)"
  3005 
  3006 text\<open>This formulation covers two cases: @{term g} and @{term h} share their
  3007       start and end points; @{term g} and @{term h} both loop upon themselves.\<close>
  3008 lemma contour_integral_nearby:
  3009   assumes os: "open s" and p: "path p" "path_image p \<subseteq> s"
  3010     shows
  3011        "\<exists>d. 0 < d \<and>
  3012             (\<forall>g h. valid_path g \<and> valid_path h \<and>
  3013                   (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
  3014                   linked_paths atends g h
  3015                   \<longrightarrow> path_image g \<subseteq> s \<and> path_image h \<subseteq> s \<and>
  3016                       (\<forall>f. f holomorphic_on s \<longrightarrow> contour_integral h f = contour_integral g f))"
  3017 proof -
  3018   have "\<forall>z. \<exists>e. z \<in> path_image p \<longrightarrow> 0 < e \<and> ball z e \<subseteq> s"
  3019     using open_contains_ball os p(2) by blast
  3020   then obtain ee where ee: "\<And>z. z \<in> path_image p \<Longrightarrow> 0 < ee z \<and> ball z (ee z) \<subseteq> s"
  3021     by metis
  3022   define cover where "cover = (\<lambda>z. ball z (ee z/3)) ` (path_image p)"
  3023   have "compact (path_image p)"
  3024     by (metis p(1) compact_path_image)
  3025   moreover have "path_image p \<subseteq> (\<Union>c\<in>path_image p. ball c (ee c / 3))"
  3026     using ee by auto
  3027   ultimately have "\<exists>D \<subseteq> cover. finite D \<and> path_image p \<subseteq> \<Union>D"
  3028     by (simp add: compact_eq_heine_borel cover_def)
  3029   then obtain D where D: "D \<subseteq> cover" "finite D" "path_image p \<subseteq> \<Union>D"
  3030     by blast
  3031   then obtain k where k: "k \<subseteq> {0..1}" "finite k" and D_eq: "D = ((\<lambda>z. ball z (ee z / 3)) \<circ> p) ` k"
  3032     apply (simp add: cover_def path_image_def image_comp)
  3033     apply (blast dest!: finite_subset_image [OF \<open>finite D\<close>])
  3034     done
  3035   then have kne: "k \<noteq> {}"
  3036     using D by auto
  3037   have pi: "\<And>i. i \<in> k \<Longrightarrow> p i \<in> path_image p"
  3038     using k  by (auto simp: path_image_def)
  3039   then have eepi: "\<And>i. i \<in> k \<Longrightarrow> 0 < ee((p i))"
  3040     by (metis ee)
  3041   define e where "e = Min((ee o p) ` k)"
  3042   have fin_eep: "finite ((ee o p) ` k)"
  3043     using k  by blast
  3044   have enz: "0 < e"
  3045     using ee k  by (simp add: kne e_def Min_gr_iff [OF fin_eep] eepi)
  3046   have "uniformly_continuous_on {0..1} p"
  3047     using p  by (simp add: path_def compact_uniformly_continuous)
  3048   then obtain d::real where d: "d>0"
  3049           and de: "\<And>x x'. \<bar>x' - x\<bar> < d \<Longrightarrow> x\<in>{0..1} \<Longrightarrow> x'\<in>{0..1} \<Longrightarrow> cmod (p x' - p x) < e/3"
  3050     unfolding uniformly_continuous_on_def dist_norm real_norm_def
  3051     by (metis divide_pos_pos enz zero_less_numeral)
  3052   then obtain N::nat where N: "N>0" "inverse N < d"
  3053     using real_arch_inverse [of d]   by auto
  3054   { fix g h
  3055     assume g: "valid_path g" and gp: "\<forall>t\<in>{0..1}. cmod (g t - p t) < e / 3"
  3056        and h: "valid_path h" and hp: "\<forall>t\<in>{0..1}. cmod (h t - p t) < e / 3"
  3057        and joins: "linked_paths atends g h"
  3058     { fix t::real
  3059       assume t: "0 \<le> t" "t \<le> 1"
  3060       then obtain u where u: "u \<in> k" and ptu: "p t \<in> ball(p u) (ee(p u) / 3)"
  3061         using \<open>path_image p \<subseteq> \<Union>D\<close> D_eq by (force simp: path_image_def)
  3062       then have ele: "e \<le> ee (p u)" using fin_eep
  3063         by (simp add: e_def)
  3064       have "cmod (g t - p t) < e / 3" "cmod (h t - p t) < e / 3"
  3065         using gp hp t by auto
  3066       with ele have "cmod (g t - p t) < ee (p u) / 3"
  3067                     "cmod (h t - p t) < ee (p u) / 3"
  3068         by linarith+
  3069       then have "g t \<in> ball(p u) (ee(p u))"  "h t \<in> ball(p u) (ee(p u))"
  3070         using norm_diff_triangle_ineq [of "g t" "p t" "p t" "p u"]
  3071               norm_diff_triangle_ineq [of "h t" "p t" "p t" "p u"] ptu eepi u
  3072         by (force simp: dist_norm ball_def norm_minus_commute)+
  3073       then have "g t \<in> s" "h t \<in> s" using ee u k
  3074         by (auto simp: path_image_def ball_def)
  3075     }
  3076     then have ghs: "path_image g \<subseteq> s" "path_image h \<subseteq> s"
  3077       by (auto simp: path_image_def)
  3078     moreover
  3079     { fix f
  3080       assume fhols: "f holomorphic_on s"
  3081       then have fpa: "f contour_integrable_on g"  "f contour_integrable_on h"
  3082         using g ghs h holomorphic_on_imp_continuous_on os contour_integrable_holomorphic_simple
  3083         by blast+
  3084       have contf: "continuous_on s f"
  3085         by (simp add: fhols holomorphic_on_imp_continuous_on)
  3086       { fix z
  3087         assume z: "z \<in> path_image p"
  3088         have "f holomorphic_on ball z (ee z)"
  3089           using fhols ee z holomorphic_on_subset by blast
  3090         then have "\<exists>ff. (\<forall>w \<in> ball z (ee z). (ff has_field_derivative f w) (at w))"
  3091           using holomorphic_convex_primitive [of "ball z (ee z)" "{}" f, simplified]
  3092           by (metis open_ball at_within_open holomorphic_on_def holomorphic_on_imp_continuous_on mem_ball)
  3093       }
  3094       then obtain ff where ff:
  3095             "\<And>z w. \<lbrakk>z \<in> path_image p; w \<in> ball z (ee z)\<rbrakk> \<Longrightarrow> (ff z has_field_derivative f w) (at w)"
  3096         by metis
  3097       { fix n
  3098         assume n: "n \<le> N"
  3099         then have "contour_integral(subpath 0 (n/N) h) f - contour_integral(subpath 0 (n/N) g) f =
  3100                    contour_integral(linepath (g(n/N)) (h(n/N))) f - contour_integral(linepath (g 0) (h 0)) f"
  3101         proof (induct n)
  3102           case 0 show ?case by simp
  3103         next
  3104           case (Suc n)
  3105           obtain t where t: "t \<in> k" and "p (n/N) \<in> ball(p t) (ee(p t) / 3)"
  3106             using \<open>path_image p \<subseteq> \<Union>D\<close> [THEN subsetD, where c="p (n/N)"] D_eq N Suc.prems
  3107             by (force simp: path_image_def)
  3108           then have ptu: "cmod (p t - p (n/N)) < ee (p t) / 3"
  3109             by (simp add: dist_norm)
  3110           have e3le: "e/3 \<le> ee (p t) / 3"  using fin_eep t
  3111             by (simp add: e_def)
  3112           { fix x
  3113             assume x: "n/N \<le> x" "x \<le> (1 + n)/N"
  3114             then have nN01: "0 \<le> n/N" "(1 + n)/N \<le> 1"
  3115               using Suc.prems by auto
  3116             then have x01: "0 \<le> x" "x \<le> 1"
  3117               using x by linarith+
  3118             have "cmod (p t - p x)  < ee (p t) / 3 + e/3"
  3119               apply (rule norm_diff_triangle_less [OF ptu de])
  3120               using x N x01 Suc.prems
  3121               apply (auto simp: field_simps)
  3122               done
  3123             then have ptx: "cmod (p t - p x) < 2*ee (p t)/3"
  3124               using e3le eepi [OF t] by simp
  3125             have "cmod (p t - g x) < 2*ee (p t)/3 + e/3 "
  3126               apply (rule norm_diff_triangle_less [OF ptx])
  3127               using gp x01 by (simp add: norm_minus_commute)
  3128             also have "... \<le> ee (p t)"
  3129               using e3le eepi [OF t] by simp
  3130             finally have gg: "cmod (p t - g x) < ee (p t)" .
  3131             have "cmod (p t - h x) < 2*ee (p t)/3 + e/3 "
  3132               apply (rule norm_diff_triangle_less [OF ptx])
  3133               using hp x01 by (simp add: norm_minus_commute)
  3134             also have "... \<le> ee (p t)"
  3135               using e3le eepi [OF t] by simp
  3136             finally have "cmod (p t - g x) < ee (p t)"
  3137                          "cmod (p t - h x) < ee (p t)"
  3138               using gg by auto
  3139           } note ptgh_ee = this
  3140           have pi_hgn: "path_image (linepath (h (n/N)) (g (n/N))) \<subseteq> ball (p t) (ee (p t))"
  3141             using ptgh_ee [of "n/N"] Suc.prems
  3142             by (auto simp: field_simps dist_norm dest: segment_furthest_le [where y="p t"])
  3143           then have gh_ns: "closed_segment (g (n/N)) (h (n/N)) \<subseteq> s"
  3144             using \<open>N>0\<close> Suc.prems
  3145             apply (simp add: path_image_join field_simps closed_segment_commute)
  3146             apply (erule order_trans)
  3147             apply (simp add: ee pi t)
  3148             done
  3149           have pi_ghn': "path_image (linepath (g ((1 + n) / N)) (h ((1 + n) / N)))
  3150                   \<subseteq> ball (p t) (ee (p t))"
  3151             using ptgh_ee [of "(1+n)/N"] Suc.prems
  3152             by (auto simp: field_simps dist_norm dest: segment_furthest_le [where y="p t"])
  3153           then have gh_n's: "closed_segment (g ((1 + n) / N)) (h ((1 + n) / N)) \<subseteq> s"
  3154             using \<open>N>0\<close> Suc.prems ee pi t
  3155             by (auto simp: Path_Connected.path_image_join field_simps)
  3156           have pi_subset_ball:
  3157                 "path_image (subpath (n/N) ((1+n) / N) g +++ linepath (g ((1+n) / N)) (h ((1+n) / N)) +++
  3158                              subpath ((1+n) / N) (n/N) h +++ linepath (h (n/N)) (g (n/N)))
  3159                  \<subseteq> ball (p t) (ee (p t))"
  3160             apply (intro subset_path_image_join pi_hgn pi_ghn')
  3161             using \<open>N>0\<close> Suc.prems
  3162             apply (auto simp: path_image_subpath dist_norm field_simps closed_segment_eq_real_ivl ptgh_ee)
  3163             done
  3164           have pi0: "(f has_contour_integral 0)
  3165                        (subpath (n/ N) ((Suc n)/N) g +++ linepath(g ((Suc n) / N)) (h((Suc n) / N)) +++
  3166                         subpath ((Suc n) / N) (n/N) h +++ linepath(h (n/N)) (g (n/N)))"
  3167             apply (rule Cauchy_theorem_primitive [of "ball(p t) (ee(p t))" "ff (p t)" "f"])
  3168             apply (metis ff open_ball at_within_open pi t)
  3169             apply (intro valid_path_join)
  3170             using Suc.prems pi_subset_ball apply (simp_all add: valid_path_subpath g h)
  3171             done
  3172           have fpa1: "f contour_integrable_on subpath (real n / real N) (real (Suc n) / real N) g"
  3173             using Suc.prems by (simp add: contour_integrable_subpath g fpa)
  3174           have fpa2: "f contour_integrable_on linepath (g (real (Suc n) / real N)) (h (real (Suc n) / real N))"
  3175             using gh_n's
  3176             by (auto intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
  3177           have fpa3: "f contour_integrable_on linepath (h (real n / real N)) (g (real n / real N))"
  3178             using gh_ns
  3179             by (auto simp: closed_segment_commute intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
  3180           have eq0: "contour_integral (subpath (n/N) ((Suc n) / real N) g) f +
  3181                      contour_integral (linepath (g ((Suc n) / N)) (h ((Suc n) / N))) f +
  3182                      contour_integral (subpath ((Suc n) / N) (n/N) h) f +
  3183                      contour_integral (linepath (h (n/N)) (g (n/N))) f = 0"
  3184             using contour_integral_unique [OF pi0] Suc.prems
  3185             by (simp add: g h fpa valid_path_subpath contour_integrable_subpath
  3186                           fpa1 fpa2 fpa3 algebra_simps del: of_nat_Suc)
  3187           have *: "\<And>hn he hn' gn gd gn' hgn ghn gh0 ghn'.
  3188                     \<lbrakk>hn - gn = ghn - gh0;
  3189                      gd + ghn' + he + hgn = (0::complex);
  3190                      hn - he = hn'; gn + gd = gn'; hgn = -ghn\<rbrakk> \<Longrightarrow> hn' - gn' = ghn' - gh0"
  3191             by (auto simp: algebra_simps)
  3192           have "contour_integral (subpath 0 (n/N) h) f - contour_integral (subpath ((Suc n) / N) (n/N) h) f =
  3193                 contour_integral (subpath 0 (n/N) h) f + contour_integral (subpath (n/N) ((Suc n) / N) h) f"
  3194             unfolding reversepath_subpath [symmetric, of "((Suc n) / N)"]
  3195             using Suc.prems by (simp add: h fpa contour_integral_reversepath valid_path_subpath contour_integrable_subpath)
  3196           also have "... = contour_integral (subpath 0 ((Suc n) / N) h) f"
  3197             using Suc.prems by (simp add: contour_integral_subpath_combine h fpa)
  3198           finally have pi0_eq:
  3199                "contour_integral (subpath 0 (n/N) h) f - contour_integral (subpath ((Suc n) / N) (n/N) h) f =
  3200                 contour_integral (subpath 0 ((Suc n) / N) h) f" .
  3201           show ?case
  3202             apply (rule * [OF Suc.hyps eq0 pi0_eq])
  3203             using Suc.prems
  3204             apply (simp_all add: g h fpa contour_integral_subpath_combine
  3205                      contour_integral_reversepath [symmetric] contour_integrable_continuous_linepath
  3206                      continuous_on_subset [OF contf gh_ns])
  3207             done
  3208       qed
  3209       } note ind = this
  3210       have "contour_integral h f = contour_integral g f"
  3211         using ind [OF order_refl] N joins
  3212         by (simp add: linked_paths_def pathstart_def pathfinish_def split: if_split_asm)
  3213     }
  3214     ultimately
  3215     have "path_image g \<subseteq> s \<and> path_image h \<subseteq> s \<and> (\<forall>f. f holomorphic_on s \<longrightarrow> contour_integral h f = contour_integral g f)"
  3216       by metis
  3217   } note * = this
  3218   show ?thesis
  3219     apply (rule_tac x="e/3" in exI)
  3220     apply (rule conjI)
  3221     using enz apply simp
  3222     apply (clarsimp simp only: ball_conj_distrib)
  3223     apply (rule *; assumption)
  3224     done
  3225 qed
  3226 
  3227 
  3228 lemma
  3229   assumes "open s" "path p" "path_image p \<subseteq> s"
  3230     shows contour_integral_nearby_ends:
  3231       "\<exists>d. 0 < d \<and>
  3232               (\<forall>g h. valid_path g \<and> valid_path h \<and>
  3233                     (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
  3234                     pathstart h = pathstart g \<and> pathfinish h = pathfinish g
  3235                     \<longrightarrow> path_image g \<subseteq> s \<and>
  3236                         path_image h \<subseteq> s \<and>
  3237                         (\<forall>f. f holomorphic_on s
  3238                             \<longrightarrow> contour_integral h f = contour_integral g f))"
  3239     and contour_integral_nearby_loops:
  3240       "\<exists>d. 0 < d \<and>
  3241               (\<forall>g h. valid_path g \<and> valid_path h \<and>
  3242                     (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
  3243                     pathfinish g = pathstart g \<and> pathfinish h = pathstart h
  3244                     \<longrightarrow> path_image g \<subseteq> s \<and>
  3245                         path_image h \<subseteq> s \<and>
  3246                         (\<forall>f. f holomorphic_on s
  3247                             \<longrightarrow> contour_integral h f = contour_integral g f))"
  3248   using contour_integral_nearby [OF assms, where atends=True]
  3249   using contour_integral_nearby [OF assms, where atends=False]
  3250   unfolding linked_paths_def by simp_all
  3251 
  3252 lemma C1_differentiable_polynomial_function:
  3253   fixes p :: "real \<Rightarrow> 'a::euclidean_space"
  3254   shows "polynomial_function p \<Longrightarrow> p C1_differentiable_on s"
  3255   by (metis continuous_on_polymonial_function C1_differentiable_on_def  has_vector_derivative_polynomial_function)
  3256 
  3257 lemma valid_path_polynomial_function:
  3258   fixes p :: "real \<Rightarrow> 'a::euclidean_space"
  3259   shows "polynomial_function p \<Longrightarrow> valid_path p"
  3260 by (force simp: valid_path_def piecewise_C1_differentiable_on_def continuous_on_polymonial_function C1_differentiable_polynomial_function)
  3261 
  3262 lemma valid_path_subpath_trivial [simp]:
  3263     fixes g :: "real \<Rightarrow> 'a::euclidean_space"
  3264     shows "z \<noteq> g x \<Longrightarrow> valid_path (subpath x x g)"
  3265   by (simp add: subpath_def valid_path_polynomial_function)
  3266 
  3267 lemma contour_integral_bound_exists:
  3268 assumes s: "open s"
  3269     and g: "valid_path g"
  3270     and pag: "path_image g \<subseteq> s"
  3271   shows "\<exists>L. 0 < L \<and>
  3272        (\<forall>f B. f holomorphic_on s \<and> (\<forall>z \<in> s. norm(f z) \<le> B)
  3273          \<longrightarrow> norm(contour_integral g f) \<le> L*B)"
  3274 proof -
  3275 have "path g" using g
  3276   by (simp add: valid_path_imp_path)
  3277 then obtain d::real and p
  3278   where d: "0 < d"
  3279     and p: "polynomial_function p" "path_image p \<subseteq> s"
  3280     and pi: "\<And>f. f holomorphic_on s \<Longrightarrow> contour_integral g f = contour_integral p f"
  3281   using contour_integral_nearby_ends [OF s \<open>path g\<close> pag]
  3282   apply clarify
  3283   apply (drule_tac x=g in spec)
  3284   apply (simp only: assms)
  3285   apply (force simp: valid_path_polynomial_function dest: path_approx_polynomial_function)
  3286   done
  3287 then obtain p' where p': "polynomial_function p'"
  3288          "\<And>x. (p has_vector_derivative (p' x)) (at x)"
  3289   by (blast intro: has_vector_derivative_polynomial_function that elim: )
  3290 then have "bounded(p' ` {0..1})"
  3291   using continuous_on_polymonial_function
  3292   by (force simp: intro!: compact_imp_bounded compact_continuous_image)
  3293 then obtain L where L: "L>0" and nop': "\<And>x. x \<in> {0..1} \<Longrightarrow> norm (p' x) \<le> L"
  3294   by (force simp: bounded_pos)
  3295 { fix f B
  3296   assume f: "f holomorphic_on s"
  3297      and B: "\<And>z. z\<in>s \<Longrightarrow> cmod (f z) \<le> B"
  3298   then have "f contour_integrable_on p \<and> valid_path p"
  3299     using p s
  3300     by (blast intro: valid_path_polynomial_function contour_integrable_holomorphic_simple holomorphic_on_imp_continuous_on)
  3301   moreover have "\<And>x. x \<in> {0..1} \<Longrightarrow> cmod (vector_derivative p (at x)) * cmod (f (p x)) \<le> L * B"
  3302     apply (rule mult_mono)
  3303     apply (subst Derivative.vector_derivative_at; force intro: p' nop')
  3304     using L B p
  3305     apply (auto simp: path_image_def image_subset_iff)
  3306     done
  3307   ultimately have "cmod (contour_integral g f) \<le> L * B"
  3308     apply (simp add: pi [OF f])
  3309     apply (simp add: contour_integral_integral)
  3310     apply (rule order_trans [OF integral_norm_bound_integral])
  3311     apply (auto simp: mult.commute integral_norm_bound_integral contour_integrable_on [symmetric] norm_mult)
  3312     done
  3313 } then
  3314 show ?thesis
  3315   by (force simp: L contour_integral_integral)
  3316 qed
  3317 
  3318 text\<open>We can treat even non-rectifiable paths as having a "length" for bounds on analytic functions in open sets.\<close>
  3319 
  3320 subsection\<open>Winding Numbers\<close>
  3321 
  3322 definition winding_number:: "[real \<Rightarrow> complex, complex] \<Rightarrow> complex" where
  3323   "winding_number \<gamma> z \<equiv>
  3324     @n. \<forall>e > 0. \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
  3325                     pathstart p = pathstart \<gamma> \<and>
  3326                     pathfinish p = pathfinish \<gamma> \<and>
  3327                     (\<forall>t \<in> {0..1}. norm(\<gamma> t - p t) < e) \<and>
  3328                     contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3329 
  3330 lemma winding_number:
  3331   assumes "path \<gamma>" "z \<notin> path_image \<gamma>" "0 < e"
  3332     shows "\<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
  3333                pathstart p = pathstart \<gamma> \<and>
  3334                pathfinish p = pathfinish \<gamma> \<and>
  3335                (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
  3336                contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * winding_number \<gamma> z"
  3337 proof -
  3338   have "path_image \<gamma> \<subseteq> UNIV - {z}"
  3339     using assms by blast
  3340   then obtain d
  3341     where d: "d>0"
  3342       and pi_eq: "\<And>h1 h2. valid_path h1 \<and> valid_path h2 \<and>
  3343                     (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < d \<and> cmod (h2 t - \<gamma> t) < d) \<and>
  3344                     pathstart h2 = pathstart h1 \<and> pathfinish h2 = pathfinish h1 \<longrightarrow>
  3345                       path_image h1 \<subseteq> UNIV - {z} \<and> path_image h2 \<subseteq> UNIV - {z} \<and>
  3346                       (\<forall>f. f holomorphic_on UNIV - {z} \<longrightarrow> contour_integral h2 f = contour_integral h1 f)"
  3347     using contour_integral_nearby_ends [of "UNIV - {z}" \<gamma>] assms by (auto simp: open_delete)
  3348   then obtain h where h: "polynomial_function h \<and> pathstart h = pathstart \<gamma> \<and> pathfinish h = pathfinish \<gamma> \<and>
  3349                           (\<forall>t \<in> {0..1}. norm(h t - \<gamma> t) < d/2)"
  3350     using path_approx_polynomial_function [OF \<open>path \<gamma>\<close>, of "d/2"] d by auto
  3351   define nn where "nn = 1/(2* pi*\<i>) * contour_integral h (\<lambda>w. 1/(w - z))"
  3352   have "\<exists>n. \<forall>e > 0. \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
  3353                         pathstart p = pathstart \<gamma> \<and>  pathfinish p = pathfinish \<gamma> \<and>
  3354                         (\<forall>t \<in> {0..1}. norm(\<gamma> t - p t) < e) \<and>
  3355                         contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3356                     (is "\<exists>n. \<forall>e > 0. ?PP e n")
  3357     proof (rule_tac x=nn in exI, clarify)
  3358       fix e::real
  3359       assume e: "e>0"
  3360       obtain p where p: "polynomial_function p \<and>
  3361             pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and> (\<forall>t\<in>{0..1}. cmod (p t - \<gamma> t) < min e (d / 2))"
  3362         using path_approx_polynomial_function [OF \<open>path \<gamma>\<close>, of "min e (d/2)"] d \<open>0<e\<close> by auto
  3363       have "(\<lambda>w. 1 / (w - z)) holomorphic_on UNIV - {z}"
  3364         by (auto simp: intro!: holomorphic_intros)
  3365       then show "?PP e nn"
  3366         apply (rule_tac x=p in exI)
  3367         using pi_eq [of h p] h p d
  3368         apply (auto simp: valid_path_polynomial_function norm_minus_commute nn_def)
  3369         done
  3370     qed
  3371   then show ?thesis
  3372     unfolding winding_number_def
  3373     apply (rule someI2_ex)
  3374     apply (blast intro: \<open>0<e\<close>)
  3375     done
  3376 qed
  3377 
  3378 lemma winding_number_unique:
  3379   assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
  3380       and pi:
  3381         "\<And>e. e>0 \<Longrightarrow> \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
  3382                           pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and>
  3383                           (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
  3384                           contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3385    shows "winding_number \<gamma> z = n"
  3386 proof -
  3387   have "path_image \<gamma> \<subseteq> UNIV - {z}"
  3388     using assms by blast
  3389   then obtain e
  3390     where e: "e>0"
  3391       and pi_eq: "\<And>h1 h2 f. \<lbrakk>valid_path h1; valid_path h2;
  3392                     (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < e \<and> cmod (h2 t - \<gamma> t) < e);
  3393                     pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1; f holomorphic_on UNIV - {z}\<rbrakk> \<Longrightarrow>
  3394                     contour_integral h2 f = contour_integral h1 f"
  3395     using contour_integral_nearby_ends [of "UNIV - {z}" \<gamma>] assms  by (auto simp: open_delete)
  3396   obtain p where p:
  3397      "valid_path p \<and> z \<notin> path_image p \<and>
  3398       pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and>
  3399       (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
  3400       contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3401     using pi [OF e] by blast
  3402   obtain q where q:
  3403      "valid_path q \<and> z \<notin> path_image q \<and>
  3404       pathstart q = pathstart \<gamma> \<and> pathfinish q = pathfinish \<gamma> \<and>
  3405       (\<forall>t\<in>{0..1}. cmod (\<gamma> t - q t) < e) \<and> contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
  3406     using winding_number [OF \<gamma> e] by blast
  3407   have "2 * complex_of_real pi * \<i> * n = contour_integral p (\<lambda>w. 1 / (w - z))"
  3408     using p by auto
  3409   also have "... = contour_integral q (\<lambda>w. 1 / (w - z))"
  3410     apply (rule pi_eq)
  3411     using p q
  3412     by (auto simp: valid_path_polynomial_function norm_minus_commute intro!: holomorphic_intros)
  3413   also have "... = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z"
  3414     using q by auto
  3415   finally have "2 * complex_of_real pi * \<i> * n = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z" .
  3416   then show ?thesis
  3417     by simp
  3418 qed
  3419 
  3420 lemma winding_number_unique_loop:
  3421   assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
  3422       and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  3423       and pi:
  3424         "\<And>e. e>0 \<Longrightarrow> \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
  3425                            pathfinish p = pathstart p \<and>
  3426                            (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
  3427                            contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3428    shows "winding_number \<gamma> z = n"
  3429 proof -
  3430   have "path_image \<gamma> \<subseteq> UNIV - {z}"
  3431     using assms by blast
  3432   then obtain e
  3433     where e: "e>0"
  3434       and pi_eq: "\<And>h1 h2 f. \<lbrakk>valid_path h1; valid_path h2;
  3435                     (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < e \<and> cmod (h2 t - \<gamma> t) < e);
  3436                     pathfinish h1 = pathstart h1; pathfinish h2 = pathstart h2; f holomorphic_on UNIV - {z}\<rbrakk> \<Longrightarrow>
  3437                     contour_integral h2 f = contour_integral h1 f"
  3438     using contour_integral_nearby_loops [of "UNIV - {z}" \<gamma>] assms  by (auto simp: open_delete)
  3439   obtain p where p:
  3440      "valid_path p \<and> z \<notin> path_image p \<and>
  3441       pathfinish p = pathstart p \<and>
  3442       (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
  3443       contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3444     using pi [OF e] by blast
  3445   obtain q where q:
  3446      "valid_path q \<and> z \<notin> path_image q \<and>
  3447       pathstart q = pathstart \<gamma> \<and> pathfinish q = pathfinish \<gamma> \<and>
  3448       (\<forall>t\<in>{0..1}. cmod (\<gamma> t - q t) < e) \<and> contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
  3449     using winding_number [OF \<gamma> e] by blast
  3450   have "2 * complex_of_real pi * \<i> * n = contour_integral p (\<lambda>w. 1 / (w - z))"
  3451     using p by auto
  3452   also have "... = contour_integral q (\<lambda>w. 1 / (w - z))"
  3453     apply (rule pi_eq)
  3454     using p q loop
  3455     by (auto simp: valid_path_polynomial_function norm_minus_commute intro!: holomorphic_intros)
  3456   also have "... = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z"
  3457     using q by auto
  3458   finally have "2 * complex_of_real pi * \<i> * n = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z" .
  3459   then show ?thesis
  3460     by simp
  3461 qed
  3462 
  3463 lemma winding_number_valid_path:
  3464   assumes "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3465     shows "winding_number \<gamma> z = 1/(2*pi*\<i>) * contour_integral \<gamma> (\<lambda>w. 1/(w - z))"
  3466   using assms by (auto simp: valid_path_imp_path intro!: winding_number_unique)
  3467 
  3468 lemma has_contour_integral_winding_number:
  3469   assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3470     shows "((\<lambda>w. 1/(w - z)) has_contour_integral (2*pi*\<i>*winding_number \<gamma> z)) \<gamma>"
  3471 by (simp add: winding_number_valid_path has_contour_integral_integral contour_integrable_inversediff assms)
  3472 
  3473 lemma winding_number_trivial [simp]: "z \<noteq> a \<Longrightarrow> winding_number(linepath a a) z = 0"
  3474   by (simp add: winding_number_valid_path)
  3475 
  3476 lemma winding_number_subpath_trivial [simp]: "z \<noteq> g x \<Longrightarrow> winding_number (subpath x x g) z = 0"
  3477   by (simp add: path_image_subpath winding_number_valid_path)
  3478 
  3479 lemma winding_number_join:
  3480   assumes g1: "path g1" "z \<notin> path_image g1"
  3481       and g2: "path g2" "z \<notin> path_image g2"
  3482       and "pathfinish g1 = pathstart g2"
  3483     shows "winding_number(g1 +++ g2) z = winding_number g1 z + winding_number g2 z"
  3484   apply (rule winding_number_unique)
  3485   using assms apply (simp_all add: not_in_path_image_join)
  3486   apply (frule winding_number [OF g2])
  3487   apply (frule winding_number [OF g1], clarify)
  3488   apply (rename_tac p2 p1)
  3489   apply (rule_tac x="p1+++p2" in exI)
  3490   apply (simp add: not_in_path_image_join contour_integrable_inversediff algebra_simps)
  3491   apply (auto simp: joinpaths_def)
  3492   done
  3493 
  3494 lemma winding_number_reversepath:
  3495   assumes "path \<gamma>" "z \<notin> path_image \<gamma>"
  3496     shows "winding_number(reversepath \<gamma>) z = - (winding_number \<gamma> z)"
  3497   apply (rule winding_number_unique)
  3498   using assms
  3499   apply simp_all
  3500   apply (frule winding_number [OF assms], clarify)
  3501   apply (rule_tac x="reversepath p" in exI)
  3502   apply (simp add: contour_integral_reversepath contour_integrable_inversediff valid_path_imp_reverse)
  3503   apply (auto simp: reversepath_def)
  3504   done
  3505 
  3506 lemma winding_number_shiftpath:
  3507   assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
  3508       and "pathfinish \<gamma> = pathstart \<gamma>" "a \<in> {0..1}"
  3509     shows "winding_number(shiftpath a \<gamma>) z = winding_number \<gamma> z"
  3510   apply (rule winding_number_unique_loop)
  3511   using assms
  3512   apply (simp_all add: path_shiftpath path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath)
  3513   apply (frule winding_number [OF \<gamma>], clarify)
  3514   apply (rule_tac x="shiftpath a p" in exI)
  3515   apply (simp add: contour_integral_shiftpath path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath valid_path_shiftpath)
  3516   apply (auto simp: shiftpath_def)
  3517   done
  3518 
  3519 lemma winding_number_split_linepath:
  3520   assumes "c \<in> closed_segment a b" "z \<notin> closed_segment a b"
  3521     shows "winding_number(linepath a b) z = winding_number(linepath a c) z + winding_number(linepath c b) z"
  3522 proof -
  3523   have "z \<notin> closed_segment a c" "z \<notin> closed_segment c b"
  3524     using assms  apply (meson convex_contains_segment convex_segment ends_in_segment(1) subsetCE)
  3525     using assms  by (meson convex_contains_segment convex_segment ends_in_segment(2) subsetCE)
  3526   then show ?thesis
  3527     using assms
  3528     by (simp add: winding_number_valid_path contour_integral_split_linepath [symmetric] continuous_on_inversediff field_simps)
  3529 qed
  3530 
  3531 lemma winding_number_cong:
  3532    "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> p t = q t) \<Longrightarrow> winding_number p z = winding_number q z"
  3533   by (simp add: winding_number_def pathstart_def pathfinish_def)
  3534 
  3535 lemma winding_number_offset: "winding_number p z = winding_number (\<lambda>w. p w - z) 0"
  3536   apply (simp add: winding_number_def contour_integral_integral path_image_def valid_path_def pathstart_def pathfinish_def)
  3537   apply (intro ext arg_cong [where f = Eps] arg_cong [where f = All] imp_cong refl, safe)
  3538   apply (rename_tac g)
  3539   apply (rule_tac x="\<lambda>t. g t - z" in exI)
  3540   apply (force simp: vector_derivative_def has_vector_derivative_diff_const piecewise_C1_differentiable_diff C1_differentiable_imp_piecewise)
  3541   apply (rename_tac g)
  3542   apply (rule_tac x="\<lambda>t. g t + z" in exI)
  3543   apply (simp add: piecewise_C1_differentiable_add vector_derivative_def has_vector_derivative_add_const C1_differentiable_imp_piecewise)
  3544   apply (force simp: algebra_simps)
  3545   done
  3546 
  3547 (* A combined theorem deducing several things piecewise.*)
  3548 lemma winding_number_join_pos_combined:
  3549      "\<lbrakk>valid_path \<gamma>1; z \<notin> path_image \<gamma>1; 0 < Re(winding_number \<gamma>1 z);
  3550        valid_path \<gamma>2; z \<notin> path_image \<gamma>2; 0 < Re(winding_number \<gamma>2 z); pathfinish \<gamma>1 = pathstart \<gamma>2\<rbrakk>
  3551       \<Longrightarrow> valid_path(\<gamma>1 +++ \<gamma>2) \<and> z \<notin> path_image(\<gamma>1 +++ \<gamma>2) \<and> 0 < Re(winding_number(\<gamma>1 +++ \<gamma>2) z)"
  3552   by (simp add: valid_path_join path_image_join winding_number_join valid_path_imp_path)
  3553 
  3554 
  3555 (* Useful sufficient conditions for the winding number to be positive etc.*)
  3556 
  3557 lemma Re_winding_number:
  3558     "\<lbrakk>valid_path \<gamma>; z \<notin> path_image \<gamma>\<rbrakk>
  3559      \<Longrightarrow> Re(winding_number \<gamma> z) = Im(contour_integral \<gamma> (\<lambda>w. 1/(w - z))) / (2*pi)"
  3560 by (simp add: winding_number_valid_path field_simps Re_divide power2_eq_square)
  3561 
  3562 lemma winding_number_pos_le:
  3563   assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3564       and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> 0 \<le> Im (vector_derivative \<gamma> (at x) * cnj(\<gamma> x - z))"
  3565     shows "0 \<le> Re(winding_number \<gamma> z)"
  3566 proof -
  3567   have *: "0 \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))" if x: "0 < x" "x < 1" for x
  3568     using ge by (simp add: Complex.Im_divide algebra_simps x)
  3569   show ?thesis
  3570     apply (simp add: Re_winding_number [OF \<gamma>] field_simps)
  3571     apply (rule has_integral_component_nonneg
  3572              [of \<i> "\<lambda>x. if x \<in> {0<..<1}
  3573                          then 1/(\<gamma> x - z) * vector_derivative \<gamma> (at x) else 0", simplified])
  3574       prefer 3 apply (force simp: *)
  3575      apply (simp add: Basis_complex_def)
  3576     apply (rule has_integral_spike_interior [of 0 1 _ "\<lambda>x. 1/(\<gamma> x - z) * vector_derivative \<gamma> (at x)"])
  3577     apply simp
  3578     apply (simp only: box_real)
  3579     apply (subst has_contour_integral [symmetric])
  3580     using \<gamma>
  3581     apply (simp add: contour_integrable_inversediff has_contour_integral_integral)
  3582     done
  3583 qed
  3584 
  3585 lemma winding_number_pos_lt_lemma:
  3586   assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3587       and e: "0 < e"
  3588       and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> e \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
  3589     shows "0 < Re(winding_number \<gamma> z)"
  3590 proof -
  3591   have "e \<le> Im (contour_integral \<gamma> (\<lambda>w. 1 / (w - z)))"
  3592     apply (rule has_integral_component_le
  3593              [of \<i> "\<lambda>x. \<i>*e" "\<i>*e" "{0..1}"
  3594                     "\<lambda>x. if x \<in> {0<..<1} then 1/(\<gamma> x - z) * vector_derivative \<gamma> (at x) else \<i>*e"
  3595                     "contour_integral \<gamma> (\<lambda>w. 1/(w - z))", simplified])
  3596     using e
  3597     apply (simp_all add: Basis_complex_def)
  3598     using has_integral_const_real [of _ 0 1] apply force
  3599     apply (rule has_integral_spike_interior [of 0 1 _ "\<lambda>x. 1/(\<gamma> x - z) * vector_derivative \<gamma> (at x)", simplified box_real])
  3600     apply simp
  3601     apply (subst has_contour_integral [symmetric])
  3602     using \<gamma>
  3603     apply (simp_all add: contour_integrable_inversediff has_contour_integral_integral ge)
  3604     done
  3605   with e show ?thesis
  3606     by (simp add: Re_winding_number [OF \<gamma>] field_simps)
  3607 qed
  3608 
  3609 lemma winding_number_pos_lt:
  3610   assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3611       and e: "0 < e"
  3612       and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> e \<le> Im (vector_derivative \<gamma> (at x) * cnj(\<gamma> x - z))"
  3613     shows "0 < Re (winding_number \<gamma> z)"
  3614 proof -
  3615   have bm: "bounded ((\<lambda>w. w - z) ` (path_image \<gamma>))"
  3616     using bounded_translation [of _ "-z"] \<gamma> by (simp add: bounded_valid_path_image)
  3617   then obtain B where B: "B > 0" and Bno: "\<And>x. x \<in> (\<lambda>w. w - z) ` (path_image \<gamma>) \<Longrightarrow> norm x \<le> B"
  3618     using bounded_pos [THEN iffD1, OF bm] by blast
  3619   { fix x::real  assume x: "0 < x" "x < 1"
  3620     then have B2: "cmod (\<gamma> x - z)^2 \<le> B^2" using Bno [of "\<gamma> x - z"]
  3621       by (simp add: path_image_def power2_eq_square mult_mono')
  3622     with x have "\<gamma> x \<noteq> z" using \<gamma>
  3623       using path_image_def by fastforce
  3624     then have "e / B\<^sup>2 \<le> Im (vector_derivative \<gamma> (at x) * cnj (\<gamma> x - z)) / (cmod (\<gamma> x - z))\<^sup>2"
  3625       using B ge [OF x] B2 e
  3626       apply (rule_tac y="e / (cmod (\<gamma> x - z))\<^sup>2" in order_trans)
  3627       apply (auto simp: divide_left_mono divide_right_mono)
  3628       done
  3629     then have "e / B\<^sup>2 \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
  3630       by (simp add: Im_divide_Reals complex_div_cnj [of _ "\<gamma> x - z" for x] del: complex_cnj_diff times_complex.sel)
  3631   } note * = this
  3632   show ?thesis
  3633     using e B by (simp add: * winding_number_pos_lt_lemma [OF \<gamma>, of "e/B^2"])
  3634 qed
  3635 
  3636 subsection\<open>The winding number is an integer\<close>
  3637 
  3638 text\<open>Proof from the book Complex Analysis by Lars V. Ahlfors, Chapter 4, section 2.1,
  3639      Also on page 134 of Serge Lang's book with the name title, etc.\<close>
  3640 
  3641 lemma exp_fg:
  3642   fixes z::complex
  3643   assumes g: "(g has_vector_derivative g') (at x within s)"
  3644       and f: "(f has_vector_derivative (g' / (g x - z))) (at x within s)"
  3645       and z: "g x \<noteq> z"
  3646     shows "((\<lambda>x. exp(-f x) * (g x - z)) has_vector_derivative 0) (at x within s)"
  3647 proof -
  3648   have *: "(exp o (\<lambda>x. (- f x)) has_vector_derivative - (g' / (g x - z)) * exp (- f x)) (at x within s)"
  3649     using assms unfolding has_vector_derivative_def scaleR_conv_of_real
  3650     by (auto intro!: derivative_eq_intros)
  3651   show ?thesis
  3652     apply (rule has_vector_derivative_eq_rhs)
  3653     apply (rule bounded_bilinear.has_vector_derivative [OF bounded_bilinear_mult])
  3654     using z
  3655     apply (auto simp: intro!: derivative_eq_intros * [unfolded o_def] g)
  3656     done
  3657 qed
  3658 
  3659 lemma winding_number_exp_integral:
  3660   fixes z::complex
  3661   assumes \<gamma>: "\<gamma> piecewise_C1_differentiable_on {a..b}"
  3662       and ab: "a \<le> b"
  3663       and z: "z \<notin> \<gamma> ` {a..b}"
  3664     shows "(\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)) integrable_on {a..b}"
  3665           (is "?thesis1")
  3666           "exp (- (integral {a..b} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))) * (\<gamma> b - z) = \<gamma> a - z"
  3667           (is "?thesis2")
  3668 proof -
  3669   let ?D\<gamma> = "\<lambda>x. vector_derivative \<gamma> (at x)"
  3670   have [simp]: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> \<gamma> x \<noteq> z"
  3671     using z by force
  3672   have cong: "continuous_on {a..b} \<gamma>"
  3673     using \<gamma> by (simp add: piecewise_C1_differentiable_on_def)
  3674   obtain k where fink: "finite k" and g_C1_diff: "\<gamma> C1_differentiable_on ({a..b} - k)"
  3675     using \<gamma> by (force simp: piecewise_C1_differentiable_on_def)
  3676   have o: "open ({a<..<b} - k)"
  3677     using \<open>finite k\<close> by (simp add: finite_imp_closed open_Diff)
  3678   moreover have "{a<..<b} - k \<subseteq> {a..b} - k"
  3679     by force
  3680   ultimately have g_diff_at: "\<And>x. \<lbrakk>x \<notin> k; x \<in> {a<..<b}\<rbrakk> \<Longrightarrow> \<gamma> differentiable at x"
  3681     by (metis Diff_iff differentiable_on_subset C1_diff_imp_diff [OF g_C1_diff] differentiable_on_def at_within_open)
  3682   { fix w
  3683     assume "w \<noteq> z"
  3684     have "continuous_on (ball w (cmod (w - z))) (\<lambda>w. 1 / (w - z))"
  3685       by (auto simp: dist_norm intro!: continuous_intros)
  3686     moreover have "\<And>x. cmod (w - x) < cmod (w - z) \<Longrightarrow> \<exists>f'. ((\<lambda>w. 1 / (w - z)) has_field_derivative f') (at x)"
  3687       by (auto simp: intro!: derivative_eq_intros)
  3688     ultimately have "\<exists>h. \<forall>y. norm(y - w) < norm(w - z) \<longrightarrow> (h has_field_derivative 1/(y - z)) (at y)"
  3689       using holomorphic_convex_primitive [of "ball w (norm(w - z))" "{}" "\<lambda>w. 1/(w - z)"]
  3690       by (simp add: field_differentiable_def Ball_def dist_norm at_within_open_NO_MATCH norm_minus_commute)
  3691   }
  3692   then obtain h where h: "\<And>w y. w \<noteq> z \<Longrightarrow> norm(y - w) < norm(w - z) \<Longrightarrow> (h w has_field_derivative 1/(y - z)) (at y)"
  3693     by meson
  3694   have exy: "\<exists>y. ((\<lambda>x. inverse (\<gamma> x - z) * ?D\<gamma> x) has_integral y) {a..b}"
  3695     unfolding integrable_on_def [symmetric]
  3696     apply (rule contour_integral_local_primitive_any [OF piecewise_C1_imp_differentiable [OF \<gamma>], of "-{z}"])
  3697     apply (rename_tac w)
  3698     apply (rule_tac x="norm(w - z)" in exI)
  3699     apply (simp_all add: inverse_eq_divide)
  3700     apply (metis has_field_derivative_at_within h)
  3701     done
  3702   have vg_int: "(\<lambda>x. ?D\<gamma> x / (\<gamma> x - z)) integrable_on {a..b}"
  3703     unfolding box_real [symmetric] divide_inverse_commute
  3704     by (auto intro!: exy integrable_subinterval simp add: integrable_on_def ab)
  3705   with ab show ?thesis1
  3706     by (simp add: divide_inverse_commute integral_def integrable_on_def)
  3707   { fix t
  3708     assume t: "t \<in> {a..b}"
  3709     have cball: "continuous_on (ball (\<gamma> t) (dist (\<gamma> t) z)) (\<lambda>x. inverse (x - z))"
  3710         using z by (auto intro!: continuous_intros simp: dist_norm)
  3711     have icd: "\<And>x. cmod (\<gamma> t - x) < cmod (\<gamma> t - z) \<Longrightarrow> (\<lambda>w. inverse (w - z)) field_differentiable at x"
  3712       unfolding field_differentiable_def by (force simp: intro!: derivative_eq_intros)
  3713     obtain h where h: "\<And>x. cmod (\<gamma> t - x) < cmod (\<gamma> t - z) \<Longrightarrow>
  3714                        (h has_field_derivative inverse (x - z)) (at x within {y. cmod (\<gamma> t - y) < cmod (\<gamma> t - z)})"
  3715       using holomorphic_convex_primitive [where f = "\<lambda>w. inverse(w - z)", OF convex_ball finite.emptyI cball icd]
  3716       by simp (auto simp: ball_def dist_norm that)
  3717     { fix x D
  3718       assume x: "x \<notin> k" "a < x" "x < b"
  3719       then have "x \<in> interior ({a..b} - k)"
  3720         using open_subset_interior [OF o] by fastforce
  3721       then have con: "isCont (\<lambda>x. ?D\<gamma> x) x"
  3722         using g_C1_diff x by (auto simp: C1_differentiable_on_eq intro: continuous_on_interior)
  3723       then have con_vd: "continuous (at x within {a..b}) (\<lambda>x. ?D\<gamma> x)"
  3724         by (rule continuous_at_imp_continuous_within)
  3725       have gdx: "\<gamma> differentiable at x"
  3726         using x by (simp add: g_diff_at)
  3727       have "((\<lambda>c. exp (- integral {a..c} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z))) * (\<gamma> c - z)) has_derivative (\<lambda>h. 0))
  3728           (at x within {a..b})"
  3729         using x gdx t
  3730         apply (clarsimp simp add: differentiable_iff_scaleR)
  3731         apply (rule exp_fg [unfolded has_vector_derivative_def, simplified], blast intro: has_derivative_at_within)
  3732         apply (simp_all add: has_vector_derivative_def [symmetric])
  3733         apply (rule has_vector_derivative_eq_rhs [OF integral_has_vector_derivative_continuous_at])
  3734         apply (rule con_vd continuous_intros cong vg_int | simp add: continuous_at_imp_continuous_within has_vector_derivative_continuous vector_derivative_at)+
  3735         done
  3736       } note * = this
  3737     have "exp (- (integral {a..t} (\<lambda>x. ?D\<gamma> x / (\<gamma> x - z)))) * (\<gamma> t - z) =\<gamma> a - z"
  3738       apply (rule has_derivative_zero_unique_strong_interval [of "{a,b} \<union> k" a b])
  3739       using t
  3740       apply (auto intro!: * continuous_intros fink cong indefinite_integral_continuous [OF vg_int]  simp add: ab)+
  3741       done
  3742    }
  3743   with ab show ?thesis2
  3744     by (simp add: divide_inverse_commute integral_def)
  3745 qed
  3746 
  3747 corollary winding_number_exp_2pi:
  3748     "\<lbrakk>path p; z \<notin> path_image p\<rbrakk>
  3749      \<Longrightarrow> pathfinish p - z = exp (2 * pi * \<i> * winding_number p z) * (pathstart p - z)"
  3750 using winding_number [of p z 1] unfolding valid_path_def path_image_def pathstart_def pathfinish_def
  3751   by (force dest: winding_number_exp_integral(2) [of _ 0 1 z] simp: field_simps contour_integral_integral exp_minus)
  3752 
  3753 
  3754 subsection\<open>The version with complex integers and equality\<close>
  3755 
  3756 lemma integer_winding_number_eq:
  3757   assumes \<gamma>: "path \<gamma>" and z: "z \<notin> path_image \<gamma>"
  3758   shows "winding_number \<gamma> z \<in> \<int> \<longleftrightarrow> pathfinish \<gamma> = pathstart \<gamma>"
  3759 proof -
  3760   have *: "\<And>i::complex. \<And>g0 g1. \<lbrakk>i \<noteq> 0; g0 \<noteq> z; (g1 - z) / i = g0 - z\<rbrakk> \<Longrightarrow> (i = 1 \<longleftrightarrow> g1 = g0)"
  3761       by (simp add: field_simps) algebra
  3762   obtain p where p: "valid_path p" "z \<notin> path_image p"
  3763                     "pathstart p = pathstart \<gamma>" "pathfinish p = pathfinish \<gamma>"
  3764                     "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
  3765     using winding_number [OF assms, of 1] by auto
  3766   have [simp]: "(winding_number \<gamma> z \<in> \<int>) = (exp (contour_integral p (\<lambda>w. 1 / (w - z))) = 1)"
  3767       using p by (simp add: exp_eq_1 complex_is_Int_iff)
  3768   have "winding_number p z \<in> \<int> \<longleftrightarrow> pathfinish p = pathstart p"
  3769     using p z
  3770     apply (simp add: winding_number_valid_path valid_path_def path_image_def pathstart_def pathfinish_def)
  3771     using winding_number_exp_integral(2) [of p 0 1 z]
  3772     apply (simp add: field_simps contour_integral_integral exp_minus)
  3773     apply (rule *)
  3774     apply (auto simp: path_image_def field_simps)
  3775     done
  3776   then show ?thesis using p
  3777     by (auto simp: winding_number_valid_path)
  3778 qed
  3779 
  3780 theorem integer_winding_number:
  3781   "\<lbrakk>path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> path_image \<gamma>\<rbrakk> \<Longrightarrow> winding_number \<gamma> z \<in> \<int>"
  3782 by (metis integer_winding_number_eq)
  3783 
  3784 
  3785 text\<open>If the winding number's magnitude is at least one, then the path must contain points in every direction.*)
  3786    We can thus bound the winding number of a path that doesn't intersect a given ray. \<close>
  3787 
  3788 lemma winding_number_pos_meets:
  3789   fixes z::complex
  3790   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and 1: "Re (winding_number \<gamma> z) \<ge> 1"
  3791       and w: "w \<noteq> z"
  3792   shows "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image \<gamma>"
  3793 proof -
  3794   have [simp]: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> \<gamma> x \<noteq> z"
  3795     using z by (auto simp: path_image_def)
  3796   have [simp]: "z \<notin> \<gamma> ` {0..1}"
  3797     using path_image_def z by auto
  3798   have gpd: "\<gamma> piecewise_C1_differentiable_on {0..1}"
  3799     using \<gamma> valid_path_def by blast
  3800   define r where "r = (w - z) / (\<gamma> 0 - z)"
  3801   have [simp]: "r \<noteq> 0"
  3802     using w z by (auto simp: r_def)
  3803   have "Arg r \<le> 2*pi"
  3804     by (simp add: Arg less_eq_real_def)
  3805   also have "... \<le> Im (integral {0..1} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))"
  3806     using 1
  3807     apply (simp add: winding_number_valid_path [OF \<gamma> z] contour_integral_integral)
  3808     apply (simp add: Complex.Re_divide field_simps power2_eq_square)
  3809     done
  3810   finally have "Arg r \<le> Im (integral {0..1} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))" .
  3811   then have "\<exists>t. t \<in> {0..1} \<and> Im(integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x)/(\<gamma> x - z))) = Arg r"
  3812     apply (simp add:)
  3813     apply (rule IVT')
  3814     apply (simp_all add: Arg_ge_0)
  3815     apply (intro continuous_intros indefinite_integral_continuous winding_number_exp_integral [OF gpd]; simp)
  3816     done
  3817   then obtain t where t:     "t \<in> {0..1}"
  3818                   and eqArg: "Im (integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x)/(\<gamma> x - z))) = Arg r"
  3819     by blast
  3820   define i where "i = integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
  3821   have iArg: "Arg r = Im i"
  3822     using eqArg by (simp add: i_def)
  3823   have gpdt: "\<gamma> piecewise_C1_differentiable_on {0..t}"
  3824     by (metis atLeastAtMost_iff atLeastatMost_subset_iff order_refl piecewise_C1_differentiable_on_subset gpd t)
  3825   have "exp (- i) * (\<gamma> t - z) = \<gamma> 0 - z"
  3826     unfolding i_def
  3827     apply (rule winding_number_exp_integral [OF gpdt])
  3828     using t z unfolding path_image_def
  3829     apply force+
  3830     done
  3831   then have *: "\<gamma> t - z = exp i * (\<gamma> 0 - z)"
  3832     by (simp add: exp_minus field_simps)
  3833   then have "(w - z) = r * (\<gamma> 0 - z)"
  3834     by (simp add: r_def)
  3835   then have "z + complex_of_real (exp (Re i)) * (w - z) / complex_of_real (cmod r) = \<gamma> t"
  3836     apply (simp add:)
  3837     apply (subst Complex_Transcendental.Arg_eq [of r])
  3838     apply (simp add: iArg)
  3839     using *
  3840     apply (simp add: exp_eq_polar field_simps)
  3841     done
  3842   with t show ?thesis
  3843     by (rule_tac x="exp(Re i) / norm r" in exI) (auto simp: path_image_def)
  3844 qed
  3845 
  3846 lemma winding_number_big_meets:
  3847   fixes z::complex
  3848   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "\<bar>Re (winding_number \<gamma> z)\<bar> \<ge> 1"
  3849       and w: "w \<noteq> z"
  3850   shows "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image \<gamma>"
  3851 proof -
  3852   { assume "Re (winding_number \<gamma> z) \<le> - 1"
  3853     then have "Re (winding_number (reversepath \<gamma>) z) \<ge> 1"
  3854       by (simp add: \<gamma> valid_path_imp_path winding_number_reversepath z)
  3855     moreover have "valid_path (reversepath \<gamma>)"
  3856       using \<gamma> valid_path_imp_reverse by auto
  3857     moreover have "z \<notin> path_image (reversepath \<gamma>)"
  3858       by (simp add: z)
  3859     ultimately have "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image (reversepath \<gamma>)"
  3860       using winding_number_pos_meets w by blast
  3861     then have ?thesis
  3862       by simp
  3863   }
  3864   then show ?thesis
  3865     using assms
  3866     by (simp add: abs_if winding_number_pos_meets split: if_split_asm)
  3867 qed
  3868 
  3869 lemma winding_number_less_1:
  3870   fixes z::complex
  3871   shows
  3872   "\<lbrakk>valid_path \<gamma>; z \<notin> path_image \<gamma>; w \<noteq> z;
  3873     \<And>a::real. 0 < a \<Longrightarrow> z + a*(w - z) \<notin> path_image \<gamma>\<rbrakk>
  3874    \<Longrightarrow> \<bar>Re(winding_number \<gamma> z)\<bar> < 1"
  3875    by (auto simp: not_less dest: winding_number_big_meets)
  3876 
  3877 text\<open>One way of proving that WN=1 for a loop.\<close>
  3878 lemma winding_number_eq_1:
  3879   fixes z::complex
  3880   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  3881       and 0: "0 < Re(winding_number \<gamma> z)" and 2: "Re(winding_number \<gamma> z) < 2"
  3882   shows "winding_number \<gamma> z = 1"
  3883 proof -
  3884   have "winding_number \<gamma> z \<in> Ints"
  3885     by (simp add: \<gamma> integer_winding_number loop valid_path_imp_path z)
  3886   then show ?thesis
  3887     using 0 2 by (auto simp: Ints_def)
  3888 qed
  3889 
  3890 
  3891 subsection\<open>Continuity of winding number and invariance on connected sets.\<close>
  3892 
  3893 lemma continuous_at_winding_number:
  3894   fixes z::complex
  3895   assumes \<gamma>: "path \<gamma>" and z: "z \<notin> path_image \<gamma>"
  3896   shows "continuous (at z) (winding_number \<gamma>)"
  3897 proof -
  3898   obtain e where "e>0" and cbg: "cball z e \<subseteq> - path_image \<gamma>"
  3899     using open_contains_cball [of "- path_image \<gamma>"]  z
  3900     by (force simp: closed_def [symmetric] closed_path_image [OF \<gamma>])
  3901   then have ppag: "path_image \<gamma> \<subseteq> - cball z (e/2)"
  3902     by (force simp: cball_def dist_norm)
  3903   have oc: "open (- cball z (e / 2))"
  3904     by (simp add: closed_def [symmetric])
  3905   obtain d where "d>0" and pi_eq:
  3906     "\<And>h1 h2. \<lbrakk>valid_path h1; valid_path h2;
  3907               (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < d \<and> cmod (h2 t - \<gamma> t) < d);
  3908               pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1\<rbrakk>
  3909              \<Longrightarrow>
  3910                path_image h1 \<subseteq> - cball z (e / 2) \<and>
  3911                path_image h2 \<subseteq> - cball z (e / 2) \<and>
  3912                (\<forall>f. f holomorphic_on - cball z (e / 2) \<longrightarrow> contour_integral h2 f = contour_integral h1 f)"
  3913     using contour_integral_nearby_ends [OF oc \<gamma> ppag] by metis
  3914   obtain p where p: "valid_path p" "z \<notin> path_image p"
  3915                     "pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma>"
  3916               and pg: "\<And>t. t\<in>{0..1} \<Longrightarrow> cmod (\<gamma> t - p t) < min d e / 2"
  3917               and pi: "contour_integral p (\<lambda>x. 1 / (x - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
  3918     using winding_number [OF \<gamma> z, of "min d e / 2"] \<open>d>0\<close> \<open>e>0\<close> by auto
  3919   { fix w
  3920     assume d2: "cmod (w - z) < d/2" and e2: "cmod (w - z) < e/2"
  3921     then have wnotp: "w \<notin> path_image p"
  3922       using cbg \<open>d>0\<close> \<open>e>0\<close>
  3923       apply (simp add: path_image_def cball_def dist_norm, clarify)
  3924       apply (frule pg)
  3925       apply (drule_tac c="\<gamma> x" in subsetD)
  3926       apply (auto simp: less_eq_real_def norm_minus_commute norm_triangle_half_l)
  3927       done
  3928     have wnotg: "w \<notin> path_image \<gamma>"
  3929       using cbg e2 \<open>e>0\<close> by (force simp: dist_norm norm_minus_commute)
  3930     { fix k::real
  3931       assume k: "k>0"
  3932       then obtain q where q: "valid_path q" "w \<notin> path_image q"
  3933                              "pathstart q = pathstart \<gamma> \<and> pathfinish q = pathfinish \<gamma>"
  3934                     and qg: "\<And>t. t \<in> {0..1} \<Longrightarrow> cmod (\<gamma> t - q t) < min k (min d e) / 2"
  3935                     and qi: "contour_integral q (\<lambda>u. 1 / (u - w)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> w"
  3936         using winding_number [OF \<gamma> wnotg, of "min k (min d e) / 2"] \<open>d>0\<close> \<open>e>0\<close> k
  3937         by (force simp: min_divide_distrib_right)
  3938       have "contour_integral p (\<lambda>u. 1 / (u - w)) = contour_integral q (\<lambda>u. 1 / (u - w))"
  3939         apply (rule pi_eq [OF \<open>valid_path q\<close> \<open>valid_path p\<close>, THEN conjunct2, THEN conjunct2, rule_format])
  3940         apply (frule pg)
  3941         apply (frule qg)
  3942         using p q \<open>d>0\<close> e2
  3943         apply (auto simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
  3944         done
  3945       then have "contour_integral p (\<lambda>x. 1 / (x - w)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> w"
  3946         by (simp add: pi qi)
  3947     } note pip = this
  3948     have "path p"
  3949       using p by (simp add: valid_path_imp_path)
  3950     then have "winding_number p w = winding_number \<gamma> w"
  3951       apply (rule winding_number_unique [OF _ wnotp])
  3952       apply (rule_tac x=p in exI)
  3953       apply (simp add: p wnotp min_divide_distrib_right pip)
  3954       done
  3955   } note wnwn = this
  3956   obtain pe where "pe>0" and cbp: "cball z (3 / 4 * pe) \<subseteq> - path_image p"
  3957     using p open_contains_cball [of "- path_image p"]
  3958     by (force simp: closed_def [symmetric] closed_path_image [OF valid_path_imp_path])
  3959   obtain L
  3960     where "L>0"
  3961       and L: "\<And>f B. \<lbrakk>f holomorphic_on - cball z (3 / 4 * pe);
  3962                       \<forall>z \<in> - cball z (3 / 4 * pe). cmod (f z) \<le> B\<rbrakk> \<Longrightarrow>
  3963                       cmod (contour_integral p f) \<le> L * B"
  3964     using contour_integral_bound_exists [of "- cball z (3/4*pe)" p] cbp \<open>valid_path p\<close> by force
  3965   { fix e::real and w::complex
  3966     assume e: "0 < e" and w: "cmod (w - z) < pe/4" "cmod (w - z) < e * pe\<^sup>2 / (8 * L)"
  3967     then have [simp]: "w \<notin> path_image p"
  3968       using cbp p(2) \<open>0 < pe\<close>
  3969       by (force simp: dist_norm norm_minus_commute path_image_def cball_def)
  3970     have [simp]: "contour_integral p (\<lambda>x. 1/(x - w)) - contour_integral p (\<lambda>x. 1/(x - z)) =
  3971                   contour_integral p (\<lambda>x. 1/(x - w) - 1/(x - z))"
  3972       by (simp add: p contour_integrable_inversediff contour_integral_diff)
  3973     { fix x
  3974       assume pe: "3/4 * pe < cmod (z - x)"
  3975       have "cmod (w - x) < pe/4 + cmod (z - x)"
  3976         by (meson add_less_cancel_right norm_diff_triangle_le order_refl order_trans_rules(21) w(1))
  3977       then have wx: "cmod (w - x) < 4/3 * cmod (z - x)" using pe by simp
  3978       have "cmod (z - x) \<le> cmod (z - w) + cmod (w - x)"
  3979         using norm_diff_triangle_le by blast
  3980       also have "... < pe/4 + cmod (w - x)"
  3981         using w by (simp add: norm_minus_commute)
  3982       finally have "pe/2 < cmod (w - x)"
  3983         using pe by auto
  3984       then have "(pe/2)^2 < cmod (w - x) ^ 2"
  3985         apply (rule power_strict_mono)
  3986         using \<open>pe>0\<close> by auto
  3987       then have pe2: "pe^2 < 4 * cmod (w - x) ^ 2"
  3988         by (simp add: power_divide)
  3989       have "8 * L * cmod (w - z) < e * pe\<^sup>2"
  3990         using w \<open>L>0\<close> by (simp add: field_simps)
  3991       also have "... < e * 4 * cmod (w - x) * cmod (w - x)"
  3992         using pe2 \<open>e>0\<close> by (simp add: power2_eq_square)
  3993       also have "... < e * 4 * cmod (w - x) * (4/3 * cmod (z - x))"
  3994         using wx
  3995         apply (rule mult_strict_left_mono)
  3996         using pe2 e not_less_iff_gr_or_eq by fastforce
  3997       finally have "L * cmod (w - z) < 2/3 * e * cmod (w - x) * cmod (z - x)"
  3998         by simp
  3999       also have "... \<le> e * cmod (w - x) * cmod (z - x)"
  4000          using e by simp
  4001       finally have Lwz: "L * cmod (w - z) < e * cmod (w - x) * cmod (z - x)" .
  4002       have "L * cmod (1 / (x - w) - 1 / (x - z)) \<le> e"
  4003         apply (cases "x=z \<or> x=w")
  4004         using pe \<open>pe>0\<close> w \<open>L>0\<close>
  4005         apply (force simp: norm_minus_commute)
  4006         using wx w(2) \<open>L>0\<close> pe pe2 Lwz
  4007         apply (auto simp: divide_simps mult_less_0_iff norm_minus_commute norm_divide norm_mult power2_eq_square)
  4008         done
  4009     } note L_cmod_le = this
  4010     have *: "cmod (contour_integral p (\<lambda>x. 1 / (x - w) - 1 / (x - z))) \<le> L * (e * pe\<^sup>2 / L / 4 * (inverse (pe / 2))\<^sup>2)"
  4011       apply (rule L)
  4012       using \<open>pe>0\<close> w
  4013       apply (force simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
  4014       using \<open>pe>0\<close> w \<open>L>0\<close>
  4015       apply (auto simp: cball_def dist_norm field_simps L_cmod_le  simp del: less_divide_eq_numeral1 le_divide_eq_numeral1)
  4016       done
  4017     have "cmod (contour_integral p (\<lambda>x. 1 / (x - w)) - contour_integral p (\<lambda>x. 1 / (x - z))) < 2*e"
  4018       apply (simp add:)
  4019       apply (rule le_less_trans [OF *])
  4020       using \<open>L>0\<close> e
  4021       apply (force simp: field_simps)
  4022       done
  4023     then have "cmod (winding_number p w - winding_number p z) < e"
  4024       using pi_ge_two e
  4025       by (force simp: winding_number_valid_path p field_simps norm_divide norm_mult intro: less_le_trans)
  4026   } note cmod_wn_diff = this
  4027   then have "isCont (winding_number p) z"
  4028     apply (simp add: continuous_at_eps_delta, clarify)
  4029     apply (rule_tac x="min (pe/4) (e/2*pe^2/L/4)" in exI)
  4030     using \<open>pe>0\<close> \<open>L>0\<close>
  4031     apply (simp add: dist_norm cmod_wn_diff)
  4032     done
  4033   then show ?thesis
  4034     apply (rule continuous_transform_within [where d = "min d e / 2"])
  4035     apply (auto simp: \<open>d>0\<close> \<open>e>0\<close> dist_norm wnwn)
  4036     done
  4037 qed
  4038 
  4039 corollary continuous_on_winding_number:
  4040     "path \<gamma> \<Longrightarrow> continuous_on (- path_image \<gamma>) (\<lambda>w. winding_number \<gamma> w)"
  4041   by (simp add: continuous_at_imp_continuous_on continuous_at_winding_number)
  4042 
  4043 
  4044 subsection\<open>The winding number is constant on a connected region\<close>
  4045 
  4046 lemma winding_number_constant:
  4047   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and cs: "connected s" and sg: "s \<inter> path_image \<gamma> = {}"
  4048   obtains k where "\<And>z. z \<in> s \<Longrightarrow> winding_number \<gamma> z = k"
  4049 proof -
  4050   have *: "1 \<le> cmod (winding_number \<gamma> y - winding_number \<gamma> z)"
  4051       if ne: "winding_number \<gamma> y \<noteq> winding_number \<gamma> z" and "y \<in> s" "z \<in> s" for y z
  4052   proof -
  4053     have "winding_number \<gamma> y \<in> \<int>"  "winding_number \<gamma> z \<in>  \<int>"
  4054       using that integer_winding_number [OF \<gamma> loop] sg \<open>y \<in> s\<close> by auto
  4055     with ne show ?thesis
  4056       by (auto simp: Ints_def of_int_diff [symmetric] simp del: of_int_diff)
  4057   qed
  4058   have cont: "continuous_on s (\<lambda>w. winding_number \<gamma> w)"
  4059     using continuous_on_winding_number [OF \<gamma>] sg
  4060     by (meson continuous_on_subset disjoint_eq_subset_Compl)
  4061   show ?thesis
  4062     apply (rule continuous_discrete_range_constant [OF cs cont])
  4063     using "*" zero_less_one apply blast
  4064     by (simp add: that)
  4065 qed
  4066 
  4067 lemma winding_number_eq:
  4068      "\<lbrakk>path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; w \<in> s; z \<in> s; connected s; s \<inter> path_image \<gamma> = {}\<rbrakk>
  4069       \<Longrightarrow> winding_number \<gamma> w = winding_number \<gamma> z"
  4070   using winding_number_constant by blast
  4071 
  4072 lemma open_winding_number_levelsets:
  4073   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4074     shows "open {z. z \<notin> path_image \<gamma> \<and> winding_number \<gamma> z = k}"
  4075 proof -
  4076   have op: "open (- path_image \<gamma>)"
  4077     by (simp add: closed_path_image \<gamma> open_Compl)
  4078   { fix z assume z: "z \<notin> path_image \<gamma>" and k: "k = winding_number \<gamma> z"
  4079     obtain e where e: "e>0" "ball z e \<subseteq> - path_image \<gamma>"
  4080       using open_contains_ball [of "- path_image \<gamma>"] op z
  4081       by blast
  4082     have "\<exists>e>0. \<forall>y. dist y z < e \<longrightarrow> y \<notin> path_image \<gamma> \<and> winding_number \<gamma> y = winding_number \<gamma> z"
  4083       apply (rule_tac x=e in exI)
  4084       using e apply (simp add: dist_norm ball_def norm_minus_commute)
  4085       apply (auto simp: dist_norm norm_minus_commute intro!: winding_number_eq [OF assms, where s = "ball z e"])
  4086       done
  4087   } then
  4088   show ?thesis
  4089     by (auto simp: open_dist)
  4090 qed
  4091 
  4092 subsection\<open>Winding number is zero "outside" a curve, in various senses\<close>
  4093 
  4094 lemma winding_number_zero_in_outside:
  4095   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and z: "z \<in> outside (path_image \<gamma>)"
  4096     shows "winding_number \<gamma> z = 0"
  4097 proof -
  4098   obtain B::real where "0 < B" and B: "path_image \<gamma> \<subseteq> ball 0 B"
  4099     using bounded_subset_ballD [OF bounded_path_image [OF \<gamma>]] by auto
  4100   obtain w::complex where w: "w \<notin> ball 0 (B + 1)"
  4101     by (metis abs_of_nonneg le_less less_irrefl mem_ball_0 norm_of_real)
  4102   have "- ball 0 (B + 1) \<subseteq> outside (path_image \<gamma>)"
  4103     apply (rule outside_subset_convex)
  4104     using B subset_ball by auto
  4105   then have wout: "w \<in> outside (path_image \<gamma>)"
  4106     using w by blast
  4107   moreover obtain k where "\<And>z. z \<in> outside (path_image \<gamma>) \<Longrightarrow> winding_number \<gamma> z = k"
  4108     using winding_number_constant [OF \<gamma> loop, of "outside(path_image \<gamma>)"] connected_outside
  4109     by (metis DIM_complex bounded_path_image dual_order.refl \<gamma> outside_no_overlap)
  4110   ultimately have "winding_number \<gamma> z = winding_number \<gamma> w"
  4111     using z by blast
  4112   also have "... = 0"
  4113   proof -
  4114     have wnot: "w \<notin> path_image \<gamma>"  using wout by (simp add: outside_def)
  4115     { fix e::real assume "0<e"
  4116       obtain p where p: "polynomial_function p" "pathstart p = pathstart \<gamma>" "pathfinish p = pathfinish \<gamma>"
  4117                  and pg1: "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> cmod (p t - \<gamma> t) < 1)"
  4118                  and pge: "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> cmod (p t - \<gamma> t) < e)"
  4119         using path_approx_polynomial_function [OF \<gamma>, of "min 1 e"] \<open>e>0\<close> by force
  4120       have pip: "path_image p \<subseteq> ball 0 (B + 1)"
  4121         using B
  4122         apply (clarsimp simp add: path_image_def dist_norm ball_def)
  4123         apply (frule (1) pg1)
  4124         apply (fastforce dest: norm_add_less)
  4125         done
  4126       then have "w \<notin> path_image p"  using w by blast
  4127       then have "\<exists>p. valid_path p \<and> w \<notin> path_image p \<and>
  4128                      pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and>
  4129                      (\<forall>t\<in>{0..1}. cmod (\<gamma> t - p t) < e) \<and> contour_integral p (\<lambda>wa. 1 / (wa - w)) = 0"
  4130         apply (rule_tac x=p in exI)
  4131         apply (simp add: p valid_path_polynomial_function)
  4132         apply (intro conjI)
  4133         using pge apply (simp add: norm_minus_commute)
  4134         apply (rule contour_integral_unique [OF Cauchy_theorem_convex_simple [OF _ convex_ball [of 0 "B+1"]]])
  4135         apply (rule holomorphic_intros | simp add: dist_norm)+
  4136         using mem_ball_0 w apply blast
  4137         using p apply (simp_all add: valid_path_polynomial_function loop pip)
  4138         done
  4139     }
  4140     then show ?thesis
  4141       by (auto intro: winding_number_unique [OF \<gamma>] simp add: wnot)
  4142   qed
  4143   finally show ?thesis .
  4144 qed
  4145 
  4146 lemma winding_number_zero_outside:
  4147     "\<lbrakk>path \<gamma>; convex s; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> s; path_image \<gamma> \<subseteq> s\<rbrakk> \<Longrightarrow> winding_number \<gamma> z = 0"
  4148   by (meson convex_in_outside outside_mono subsetCE winding_number_zero_in_outside)
  4149 
  4150 lemma winding_number_zero_at_infinity:
  4151   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4152     shows "\<exists>B. \<forall>z. B \<le> norm z \<longrightarrow> winding_number \<gamma> z = 0"
  4153 proof -
  4154   obtain B::real where "0 < B" and B: "path_image \<gamma> \<subseteq> ball 0 B"
  4155     using bounded_subset_ballD [OF bounded_path_image [OF \<gamma>]] by auto
  4156   then show ?thesis
  4157     apply (rule_tac x="B+1" in exI, clarify)
  4158     apply (rule winding_number_zero_outside [OF \<gamma> convex_cball [of 0 B] loop])
  4159     apply (meson less_add_one mem_cball_0 not_le order_trans)
  4160     using ball_subset_cball by blast
  4161 qed
  4162 
  4163 lemma winding_number_zero_point:
  4164     "\<lbrakk>path \<gamma>; convex s; pathfinish \<gamma> = pathstart \<gamma>; open s; path_image \<gamma> \<subseteq> s\<rbrakk>
  4165      \<Longrightarrow> \<exists>z. z \<in> s \<and> winding_number \<gamma> z = 0"
  4166   using outside_compact_in_open [of "path_image \<gamma>" s] path_image_nonempty winding_number_zero_in_outside
  4167   by (fastforce simp add: compact_path_image)
  4168 
  4169 
  4170 text\<open>If a path winds round a set, it winds rounds its inside.\<close>
  4171 lemma winding_number_around_inside:
  4172   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4173       and cls: "closed s" and cos: "connected s" and s_disj: "s \<inter> path_image \<gamma> = {}"
  4174       and z: "z \<in> s" and wn_nz: "winding_number \<gamma> z \<noteq> 0" and w: "w \<in> s \<union> inside s"
  4175     shows "winding_number \<gamma> w = winding_number \<gamma> z"
  4176 proof -
  4177   have ssb: "s \<subseteq> inside(path_image \<gamma>)"
  4178   proof
  4179     fix x :: complex
  4180     assume "x \<in> s"
  4181     hence "x \<notin> path_image \<gamma>"
  4182       by (meson disjoint_iff_not_equal s_disj)
  4183     thus "x \<in> inside (path_image \<gamma>)"
  4184       using \<open>x \<in> s\<close> by (metis (no_types) ComplI UnE cos \<gamma> loop s_disj union_with_outside winding_number_eq winding_number_zero_in_outside wn_nz z)
  4185 qed
  4186   show ?thesis
  4187     apply (rule winding_number_eq [OF \<gamma> loop w])
  4188     using z apply blast
  4189     apply (simp add: cls connected_with_inside cos)
  4190     apply (simp add: Int_Un_distrib2 s_disj, safe)
  4191     by (meson ssb inside_inside_compact_connected [OF cls, of "path_image \<gamma>"] compact_path_image connected_path_image contra_subsetD disjoint_iff_not_equal \<gamma> inside_no_overlap)
  4192  qed
  4193 
  4194 
  4195 text\<open>Bounding a WN by 1/2 for a path and point in opposite halfspaces.\<close>
  4196 lemma winding_number_subpath_continuous:
  4197   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>"
  4198     shows "continuous_on {0..1} (\<lambda>x. winding_number(subpath 0 x \<gamma>) z)"
  4199 proof -
  4200   have *: "integral {0..x} (\<lambda>t. vector_derivative \<gamma> (at t) / (\<gamma> t - z)) / (2 * of_real pi * \<i>) =
  4201          winding_number (subpath 0 x \<gamma>) z"
  4202          if x: "0 \<le> x" "x \<le> 1" for x
  4203   proof -
  4204     have "integral {0..x} (\<lambda>t. vector_derivative \<gamma> (at t) / (\<gamma> t - z)) / (2 * of_real pi * \<i>) =
  4205           1 / (2*pi*\<i>) * contour_integral (subpath 0 x \<gamma>) (\<lambda>w. 1/(w - z))"
  4206       using assms x
  4207       apply (simp add: contour_integral_subcontour_integral [OF contour_integrable_inversediff])
  4208       done
  4209     also have "... = winding_number (subpath 0 x \<gamma>) z"
  4210       apply (subst winding_number_valid_path)
  4211       using assms x
  4212       apply (simp_all add: path_image_subpath valid_path_subpath)
  4213       by (force simp: path_image_def)
  4214     finally show ?thesis .
  4215   qed
  4216   show ?thesis
  4217     apply (rule continuous_on_eq
  4218                  [where f = "\<lambda>x. 1 / (2*pi*\<i>) *
  4219                                  integral {0..x} (\<lambda>t. 1/(\<gamma> t - z) * vector_derivative \<gamma> (at t))"])
  4220     apply (rule continuous_intros)+
  4221     apply (rule indefinite_integral_continuous)
  4222     apply (rule contour_integrable_inversediff [OF assms, unfolded contour_integrable_on])
  4223       using assms
  4224     apply (simp add: *)
  4225     done
  4226 qed
  4227 
  4228 lemma winding_number_ivt_pos:
  4229     assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "0 \<le> w" "w \<le> Re(winding_number \<gamma> z)"
  4230       shows "\<exists>t \<in> {0..1}. Re(winding_number(subpath 0 t \<gamma>) z) = w"
  4231   apply (rule ivt_increasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right])
  4232   apply (simp add:)
  4233   apply (rule winding_number_subpath_continuous [OF \<gamma> z])
  4234   using assms
  4235   apply (auto simp: path_image_def image_def)
  4236   done
  4237 
  4238 lemma winding_number_ivt_neg:
  4239     assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "Re(winding_number \<gamma> z) \<le> w" "w \<le> 0"
  4240       shows "\<exists>t \<in> {0..1}. Re(winding_number(subpath 0 t \<gamma>) z) = w"
  4241   apply (rule ivt_decreasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right])
  4242   apply (simp add:)
  4243   apply (rule winding_number_subpath_continuous [OF \<gamma> z])
  4244   using assms
  4245   apply (auto simp: path_image_def image_def)
  4246   done
  4247 
  4248 lemma winding_number_ivt_abs:
  4249     assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "0 \<le> w" "w \<le> \<bar>Re(winding_number \<gamma> z)\<bar>"
  4250       shows "\<exists>t \<in> {0..1}. \<bar>Re (winding_number (subpath 0 t \<gamma>) z)\<bar> = w"
  4251   using assms winding_number_ivt_pos [of \<gamma> z w] winding_number_ivt_neg [of \<gamma> z "-w"]
  4252   by force
  4253 
  4254 lemma winding_number_lt_half_lemma:
  4255   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and az: "a \<bullet> z \<le> b" and pag: "path_image \<gamma> \<subseteq> {w. a \<bullet> w > b}"
  4256     shows "Re(winding_number \<gamma> z) < 1/2"
  4257 proof -
  4258   { assume "Re(winding_number \<gamma> z) \<ge> 1/2"
  4259     then obtain t::real where t: "0 \<le> t" "t \<le> 1" and sub12: "Re (winding_number (subpath 0 t \<gamma>) z) = 1/2"
  4260       using winding_number_ivt_pos [OF \<gamma> z, of "1/2"] by auto
  4261     have gt: "\<gamma> t - z = - (of_real (exp (- (2 * pi * Im (winding_number (subpath 0 t \<gamma>) z)))) * (\<gamma> 0 - z))"
  4262       using winding_number_exp_2pi [of "subpath 0 t \<gamma>" z]
  4263       apply (simp add: t \<gamma> valid_path_imp_path)
  4264       using closed_segment_eq_real_ivl path_image_def t z by (fastforce simp: path_image_subpath Euler sub12)
  4265     have "b < a \<bullet> \<gamma> 0"
  4266     proof -
  4267       have "\<gamma> 0 \<in> {c. b < a \<bullet> c}"
  4268         by (metis (no_types) pag atLeastAtMost_iff image_subset_iff order_refl path_image_def zero_le_one)
  4269       thus ?thesis
  4270         by blast
  4271     qed
  4272     moreover have "b < a \<bullet> \<gamma> t"
  4273     proof -
  4274       have "\<gamma> t \<in> {c. b < a \<bullet> c}"
  4275         by (metis (no_types) pag atLeastAtMost_iff image_subset_iff path_image_def t)
  4276       thus ?thesis
  4277         by blast
  4278     qed
  4279     ultimately have "0 < a \<bullet> (\<gamma> 0 - z)" "0 < a \<bullet> (\<gamma> t - z)" using az
  4280       by (simp add: inner_diff_right)+
  4281     then have False
  4282       by (simp add: gt inner_mult_right mult_less_0_iff)
  4283   }
  4284   then show ?thesis by force
  4285 qed
  4286 
  4287 lemma winding_number_lt_half:
  4288   assumes "valid_path \<gamma>" "a \<bullet> z \<le> b" "path_image \<gamma> \<subseteq> {w. a \<bullet> w > b}"
  4289     shows "\<bar>Re (winding_number \<gamma> z)\<bar> < 1/2"
  4290 proof -
  4291   have "z \<notin> path_image \<gamma>" using assms by auto
  4292   with assms show ?thesis
  4293     apply (simp add: winding_number_lt_half_lemma abs_if del: less_divide_eq_numeral1)
  4294     apply (metis complex_inner_1_right winding_number_lt_half_lemma [OF valid_path_imp_reverse, of \<gamma> z a b]
  4295                  winding_number_reversepath valid_path_imp_path inner_minus_left path_image_reversepath)
  4296     done
  4297 qed
  4298 
  4299 lemma winding_number_le_half:
  4300   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>"
  4301       and anz: "a \<noteq> 0" and azb: "a \<bullet> z \<le> b" and pag: "path_image \<gamma> \<subseteq> {w. a \<bullet> w \<ge> b}"
  4302     shows "\<bar>Re (winding_number \<gamma> z)\<bar> \<le> 1/2"
  4303 proof -
  4304   { assume wnz_12: "\<bar>Re (winding_number \<gamma> z)\<bar> > 1/2"
  4305     have "isCont (winding_number \<gamma>) z"
  4306       by (metis continuous_at_winding_number valid_path_imp_path \<gamma> z)
  4307     then obtain d where "d>0" and d: "\<And>x'. dist x' z < d \<Longrightarrow> dist (winding_number \<gamma> x') (winding_number \<gamma> z) < \<bar>Re(winding_number \<gamma> z)\<bar> - 1/2"
  4308       using continuous_at_eps_delta wnz_12 diff_gt_0_iff_gt by blast
  4309     define z' where "z' = z - (d / (2 * cmod a)) *\<^sub>R a"
  4310     have *: "a \<bullet> z' \<le> b - d / 3 * cmod a"
  4311       unfolding z'_def inner_mult_right' divide_inverse
  4312       apply (simp add: divide_simps algebra_simps dot_square_norm power2_eq_square anz)
  4313       apply (metis \<open>0 < d\<close> add_increasing azb less_eq_real_def mult_nonneg_nonneg mult_right_mono norm_ge_zero norm_numeral)
  4314       done
  4315     have "cmod (winding_number \<gamma> z' - winding_number \<gamma> z) < \<bar>Re (winding_number \<gamma> z)\<bar> - 1/2"
  4316       using d [of z'] anz \<open>d>0\<close> by (simp add: dist_norm z'_def)
  4317     then have "1/2 < \<bar>Re (winding_number \<gamma> z)\<bar> - cmod (winding_number \<gamma> z' - winding_number \<gamma> z)"
  4318       by simp
  4319     then have "1/2 < \<bar>Re (winding_number \<gamma> z)\<bar> - \<bar>Re (winding_number \<gamma> z') - Re (winding_number \<gamma> z)\<bar>"
  4320       using abs_Re_le_cmod [of "winding_number \<gamma> z' - winding_number \<gamma> z"] by simp
  4321     then have wnz_12': "\<bar>Re (winding_number \<gamma> z')\<bar> > 1/2"
  4322       by linarith
  4323     moreover have "\<bar>Re (winding_number \<gamma> z')\<bar> < 1/2"
  4324       apply (rule winding_number_lt_half [OF \<gamma> *])
  4325       using azb \<open>d>0\<close> pag
  4326       apply (auto simp: add_strict_increasing anz divide_simps algebra_simps dest!: subsetD)
  4327       done
  4328     ultimately have False
  4329       by simp
  4330   }
  4331   then show ?thesis by force
  4332 qed
  4333 
  4334 lemma winding_number_lt_half_linepath: "z \<notin> closed_segment a b \<Longrightarrow> \<bar>Re (winding_number (linepath a b) z)\<bar> < 1/2"
  4335   using separating_hyperplane_closed_point [of "closed_segment a b" z]
  4336   apply auto
  4337   apply (simp add: closed_segment_def)
  4338   apply (drule less_imp_le)
  4339   apply (frule winding_number_lt_half [OF valid_path_linepath [of a b]])
  4340   apply (auto simp: segment)
  4341   done
  4342 
  4343 
  4344 text\<open> Positivity of WN for a linepath.\<close>
  4345 lemma winding_number_linepath_pos_lt:
  4346     assumes "0 < Im ((b - a) * cnj (b - z))"
  4347       shows "0 < Re(winding_number(linepath a b) z)"
  4348 proof -
  4349   have z: "z \<notin> path_image (linepath a b)"
  4350     using assms
  4351     by (simp add: closed_segment_def) (force simp: algebra_simps)
  4352   show ?thesis
  4353     apply (rule winding_number_pos_lt [OF valid_path_linepath z assms])
  4354     apply (simp add: linepath_def algebra_simps)
  4355     done
  4356 qed
  4357 
  4358 
  4359 subsection\<open>Cauchy's integral formula, again for a convex enclosing set.\<close>
  4360 
  4361 lemma Cauchy_integral_formula_weak:
  4362     assumes s: "convex s" and "finite k" and conf: "continuous_on s f"
  4363         and fcd: "(\<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x)"
  4364         and z: "z \<in> interior s - k" and vpg: "valid_path \<gamma>"
  4365         and pasz: "path_image \<gamma> \<subseteq> s - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4366       shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
  4367 proof -
  4368   obtain f' where f': "(f has_field_derivative f') (at z)"
  4369     using fcd [OF z] by (auto simp: field_differentiable_def)
  4370   have pas: "path_image \<gamma> \<subseteq> s" and znotin: "z \<notin> path_image \<gamma>" using pasz by blast+
  4371   have c: "continuous (at x within s) (\<lambda>w. if w = z then f' else (f w - f z) / (w - z))" if "x \<in> s" for x
  4372   proof (cases "x = z")
  4373     case True then show ?thesis
  4374       apply (simp add: continuous_within)
  4375       apply (rule Lim_transform_away_within [of _ "z+1" _ "\<lambda>w::complex. (f w - f z)/(w - z)"])
  4376       using has_field_derivative_at_within DERIV_within_iff f'
  4377       apply (fastforce simp add:)+
  4378       done
  4379   next
  4380     case False
  4381     then have dxz: "dist x z > 0" by auto
  4382     have cf: "continuous (at x within s) f"
  4383       using conf continuous_on_eq_continuous_within that by blast
  4384     have "continuous (at x within s) (\<lambda>w. (f w - f z) / (w - z))"
  4385       by (rule cf continuous_intros | simp add: False)+
  4386     then show ?thesis
  4387       apply (rule continuous_transform_within [OF _ dxz that, of "\<lambda>w::complex. (f w - f z)/(w - z)"])
  4388       apply (force simp: dist_commute)
  4389       done
  4390   qed
  4391   have fink': "finite (insert z k)" using \<open>finite k\<close> by blast
  4392   have *: "((\<lambda>w. if w = z then f' else (f w - f z) / (w - z)) has_contour_integral 0) \<gamma>"
  4393     apply (rule Cauchy_theorem_convex [OF _ s fink' _ vpg pas loop])
  4394     using c apply (force simp: continuous_on_eq_continuous_within)
  4395     apply (rename_tac w)
  4396     apply (rule_tac d="dist w z" and f = "\<lambda>w. (f w - f z)/(w - z)" in field_differentiable_transform_within)
  4397     apply (simp_all add: dist_pos_lt dist_commute)
  4398     apply (metis less_irrefl)
  4399     apply (rule derivative_intros fcd | simp)+
  4400     done
  4401   show ?thesis
  4402     apply (rule has_contour_integral_eq)
  4403     using znotin has_contour_integral_add [OF has_contour_integral_lmul [OF has_contour_integral_winding_number [OF vpg znotin], of "f z"] *]
  4404     apply (auto simp: mult_ac divide_simps)
  4405     done
  4406 qed
  4407 
  4408 theorem Cauchy_integral_formula_convex_simple:
  4409     "\<lbrakk>convex s; f holomorphic_on s; z \<in> interior s; valid_path \<gamma>; path_image \<gamma> \<subseteq> s - {z};
  4410       pathfinish \<gamma> = pathstart \<gamma>\<rbrakk>
  4411      \<Longrightarrow> ((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
  4412   apply (rule Cauchy_integral_formula_weak [where k = "{}"])
  4413   using holomorphic_on_imp_continuous_on
  4414   by auto (metis at_within_interior holomorphic_on_def interiorE subsetCE)
  4415 
  4416 
  4417 subsection\<open>Homotopy forms of Cauchy's theorem\<close>
  4418 
  4419 proposition Cauchy_theorem_homotopic:
  4420     assumes hom: "if atends then homotopic_paths s g h else homotopic_loops s g h"
  4421         and "open s" and f: "f holomorphic_on s"
  4422         and vpg: "valid_path g" and vph: "valid_path h"
  4423     shows "contour_integral g f = contour_integral h f"
  4424 proof -
  4425   have pathsf: "linked_paths atends g h"
  4426     using hom  by (auto simp: linked_paths_def homotopic_paths_imp_pathstart homotopic_paths_imp_pathfinish homotopic_loops_imp_loop)
  4427   obtain k :: "real \<times> real \<Rightarrow> complex"
  4428     where contk: "continuous_on ({0..1} \<times> {0..1}) k"
  4429       and ks: "k ` ({0..1} \<times> {0..1}) \<subseteq> s"
  4430       and k [simp]: "\<forall>x. k (0, x) = g x" "\<forall>x. k (1, x) = h x"
  4431       and ksf: "\<forall>t\<in>{0..1}. linked_paths atends g (\<lambda>x. k (t, x))"
  4432       using hom pathsf by (auto simp: linked_paths_def homotopic_paths_def homotopic_loops_def homotopic_with_def split: if_split_asm)
  4433   have ucontk: "uniformly_continuous_on ({0..1} \<times> {0..1}) k"
  4434     by (blast intro: compact_Times compact_uniformly_continuous [OF contk])
  4435   { fix t::real assume t: "t \<in> {0..1}"
  4436     have pak: "path (k o (\<lambda>u. (t, u)))"
  4437       unfolding path_def
  4438       apply (rule continuous_intros continuous_on_subset [OF contk])+
  4439       using t by force
  4440     have pik: "path_image (k \<circ> Pair t) \<subseteq> s"
  4441       using ks t by (auto simp: path_image_def)
  4442     obtain e where "e>0" and e:
  4443          "\<And>g h. \<lbrakk>valid_path g; valid_path h;
  4444                   \<forall>u\<in>{0..1}. cmod (g u - (k \<circ> Pair t) u) < e \<and> cmod (h u - (k \<circ> Pair t) u) < e;
  4445                   linked_paths atends g h\<rbrakk>
  4446                  \<Longrightarrow> contour_integral h f = contour_integral g f"
  4447       using contour_integral_nearby [OF \<open>open s\<close> pak pik, of atends] f by metis
  4448     obtain d where "d>0" and d:
  4449         "\<And>x x'. \<lbrakk>x \<in> {0..1} \<times> {0..1}; x' \<in> {0..1} \<times> {0..1}; norm (x'-x) < d\<rbrakk> \<Longrightarrow> norm (k x' - k x) < e/4"
  4450       by (rule uniformly_continuous_onE [OF ucontk, of "e/4"]) (auto simp: dist_norm \<open>e>0\<close>)
  4451     { fix t1 t2
  4452       assume t1: "0 \<le> t1" "t1 \<le> 1" and t2: "0 \<le> t2" "t2 \<le> 1" and ltd: "\<bar>t1 - t\<bar> < d" "\<bar>t2 - t\<bar> < d"
  4453       have no2: "\<And>g1 k1 kt. \<lbrakk>norm(g1 - k1) < e/4; norm(k1 - kt) < e/4\<rbrakk> \<Longrightarrow> norm(g1 - kt) < e"
  4454         using \<open>e > 0\<close>
  4455         apply (rule_tac y = k1 in norm_triangle_half_l)
  4456         apply (auto simp: norm_minus_commute intro: order_less_trans)
  4457         done
  4458       have "\<exists>d>0. \<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
  4459                           (\<forall>u\<in>{0..1}. cmod (g1 u - k (t1, u)) < d \<and> cmod (g2 u - k (t2, u)) < d) \<and>
  4460                           linked_paths atends g1 g2 \<longrightarrow>
  4461                           contour_integral g2 f = contour_integral g1 f"
  4462         apply (rule_tac x="e/4" in exI)
  4463         using t t1 t2 ltd \<open>e > 0\<close>
  4464         apply (auto intro!: e simp: d no2 simp del: less_divide_eq_numeral1)
  4465         done
  4466     }
  4467     then have "\<exists>e. 0 < e \<and>
  4468               (\<forall>t1 t2. t1 \<in> {0..1} \<and> t2 \<in> {0..1} \<and> \<bar>t1 - t\<bar> < e \<and> \<bar>t2 - t\<bar> < e
  4469                 \<longrightarrow> (\<exists>d. 0 < d \<and>
  4470                      (\<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
  4471                        (\<forall>u \<in> {0..1}.
  4472                           norm(g1 u - k((t1,u))) < d \<and> norm(g2 u - k((t2,u))) < d) \<and>
  4473                           linked_paths atends g1 g2
  4474                           \<longrightarrow> contour_integral g2 f = contour_integral g1 f)))"
  4475       by (rule_tac x=d in exI) (simp add: \<open>d > 0\<close>)
  4476   }
  4477   then obtain ee where ee:
  4478        "\<And>t. t \<in> {0..1} \<Longrightarrow> ee t > 0 \<and>
  4479           (\<forall>t1 t2. t1 \<in> {0..1} \<longrightarrow> t2 \<in> {0..1} \<longrightarrow> \<bar>t1 - t\<bar> < ee t \<longrightarrow> \<bar>t2 - t\<bar> < ee t
  4480             \<longrightarrow> (\<exists>d. 0 < d \<and>
  4481                  (\<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
  4482                    (\<forall>u \<in> {0..1}.
  4483                       norm(g1 u - k((t1,u))) < d \<and> norm(g2 u - k((t2,u))) < d) \<and>
  4484                       linked_paths atends g1 g2
  4485                       \<longrightarrow> contour_integral g2 f = contour_integral g1 f)))"
  4486     by metis
  4487   note ee_rule = ee [THEN conjunct2, rule_format]
  4488   define C where "C = (\<lambda>t. ball t (ee t / 3)) ` {0..1}"
  4489   obtain C' where C': "C' \<subseteq> C" "finite C'" and C'01: "{0..1} \<subseteq> \<Union>C'"
  4490   proof (rule compactE [OF compact_interval])
  4491     show "{0..1} \<subseteq> \<Union>C"
  4492       using ee [THEN conjunct1] by (auto simp: C_def dist_norm)
  4493   qed (use C_def in auto)
  4494   define kk where "kk = {t \<in> {0..1}. ball t (ee t / 3) \<in> C'}"
  4495   have kk01: "kk \<subseteq> {0..1}" by (auto simp: kk_def)
  4496   define e where "e = Min (ee ` kk)"
  4497   have C'_eq: "C' = (\<lambda>t. ball t (ee t / 3)) ` kk"
  4498     using C' by (auto simp: kk_def C_def)
  4499   have ee_pos[simp]: "\<And>t. t \<in> {0..1} \<Longrightarrow> ee t > 0"
  4500     by (simp add: kk_def ee)
  4501   moreover have "finite kk"
  4502     using \<open>finite C'\<close> kk01 by (force simp: C'_eq inj_on_def ball_eq_ball_iff dest: ee_pos finite_imageD)
  4503   moreover have "kk \<noteq> {}" using \<open>{0..1} \<subseteq> \<Union>C'\<close> C'_eq by force
  4504   ultimately have "e > 0"
  4505     using finite_less_Inf_iff [of "ee ` kk" 0] kk01 by (force simp: e_def)
  4506   then obtain N::nat where "N > 0" and N: "1/N < e/3"
  4507     by (meson divide_pos_pos nat_approx_posE zero_less_Suc zero_less_numeral)
  4508   have e_le_ee: "\<And>i. i \<in> kk \<Longrightarrow> e \<le> ee i"
  4509     using \<open>finite kk\<close> by (simp add: e_def Min_le_iff [of "ee ` kk"])
  4510   have plus: "\<exists>t \<in> kk. x \<in> ball t (ee t / 3)" if "x \<in> {0..1}" for x
  4511     using C' subsetD [OF C'01 that]  unfolding C'_eq by blast
  4512   have [OF order_refl]:
  4513       "\<exists>d. 0 < d \<and> (\<forall>j. valid_path j \<and> (\<forall>u \<in> {0..1}. norm(j u - k (n/N, u)) < d) \<and> linked_paths atends g j
  4514                         \<longrightarrow> contour_integral j f = contour_integral g f)"
  4515        if "n \<le> N" for n
  4516   using that
  4517   proof (induct n)
  4518     case 0 show ?case using ee_rule [of 0 0 0]
  4519       apply clarsimp
  4520       apply (rule_tac x=d in exI, safe)
  4521       by (metis diff_self vpg norm_zero)
  4522   next
  4523     case (Suc n)
  4524     then have N01: "n/N \<in> {0..1}" "(Suc n)/N \<in> {0..1}"  by auto
  4525     then obtain t where t: "t \<in> kk" "n/N \<in> ball t (ee t / 3)"
  4526       using plus [of "n/N"] by blast
  4527     then have nN_less: "\<bar>n/N - t\<bar> < ee t"
  4528       by (simp add: dist_norm del: less_divide_eq_numeral1)
  4529     have n'N_less: "\<bar>real (Suc n) / real N - t\<bar> < ee t"
  4530       using t N \<open>N > 0\<close> e_le_ee [of t]
  4531       by (simp add: dist_norm add_divide_distrib abs_diff_less_iff del: less_divide_eq_numeral1) (simp add: field_simps)
  4532     have t01: "t \<in> {0..1}" using \<open>kk \<subseteq> {0..1}\<close> \<open>t \<in> kk\<close> by blast
  4533     obtain d1 where "d1 > 0" and d1:
  4534         "\<And>g1 g2. \<lbrakk>valid_path g1; valid_path g2;
  4535                    \<forall>u\<in>{0..1}. cmod (g1 u - k (n/N, u)) < d1 \<and> cmod (g2 u - k ((Suc n) / N, u)) < d1;
  4536                    linked_paths atends g1 g2\<rbrakk>
  4537                    \<Longrightarrow> contour_integral g2 f = contour_integral g1 f"
  4538       using ee [THEN conjunct2, rule_format, OF t01 N01 nN_less n'N_less] by fastforce
  4539     have "n \<le> N" using Suc.prems by auto
  4540     with Suc.hyps
  4541     obtain d2 where "d2 > 0"
  4542       and d2: "\<And>j. \<lbrakk>valid_path j; \<forall>u\<in>{0..1}. cmod (j u - k (n/N, u)) < d2; linked_paths atends g j\<rbrakk>
  4543                      \<Longrightarrow> contour_integral j f = contour_integral g f"
  4544         by auto
  4545     have "continuous_on {0..1} (k o (\<lambda>u. (n/N, u)))"
  4546       apply (rule continuous_intros continuous_on_subset [OF contk])+
  4547       using N01 by auto
  4548     then have pkn: "path (\<lambda>u. k (n/N, u))"
  4549       by (simp add: path_def)
  4550     have min12: "min d1 d2 > 0" by (simp add: \<open>0 < d1\<close> \<open>0 < d2\<close>)
  4551     obtain p where "polynomial_function p"
  4552         and psf: "pathstart p = pathstart (\<lambda>u. k (n/N, u))"
  4553                  "pathfinish p = pathfinish (\<lambda>u. k (n/N, u))"
  4554         and pk_le:  "\<And>t. t\<in>{0..1} \<Longrightarrow> cmod (p t - k (n/N, t)) < min d1 d2"
  4555       using path_approx_polynomial_function [OF pkn min12] by blast
  4556     then have vpp: "valid_path p" using valid_path_polynomial_function by blast
  4557     have lpa: "linked_paths atends g p"
  4558       by (metis (mono_tags, lifting) N01(1) ksf linked_paths_def pathfinish_def pathstart_def psf)
  4559     show ?case
  4560       apply (rule_tac x="min d1 d2" in exI)
  4561       apply (simp add: \<open>0 < d1\<close> \<open>0 < d2\<close>, clarify)
  4562       apply (rule_tac s="contour_integral p f" in trans)
  4563       using pk_le N01(1) ksf pathfinish_def pathstart_def
  4564       apply (force intro!: vpp d1 simp add: linked_paths_def psf ksf)
  4565       using pk_le N01 apply (force intro!: vpp d2 lpa simp add: linked_paths_def psf ksf)
  4566       done
  4567   qed
  4568   then obtain d where "0 < d"
  4569                        "\<And>j. valid_path j \<and> (\<forall>u \<in> {0..1}. norm(j u - k (1,u)) < d) \<and>
  4570                             linked_paths atends g j
  4571                             \<Longrightarrow> contour_integral j f = contour_integral g f"
  4572     using \<open>N>0\<close> by auto
  4573   then have "linked_paths atends g h \<Longrightarrow> contour_integral h f = contour_integral g f"
  4574     using \<open>N>0\<close> vph by fastforce
  4575   then show ?thesis
  4576     by (simp add: pathsf)
  4577 qed
  4578 
  4579 proposition Cauchy_theorem_homotopic_paths:
  4580     assumes hom: "homotopic_paths s g h"
  4581         and "open s" and f: "f holomorphic_on s"
  4582         and vpg: "valid_path g" and vph: "valid_path h"
  4583     shows "contour_integral g f = contour_integral h f"
  4584   using Cauchy_theorem_homotopic [of True s g h] assms by simp
  4585 
  4586 proposition Cauchy_theorem_homotopic_loops:
  4587     assumes hom: "homotopic_loops s g h"
  4588         and "open s" and f: "f holomorphic_on s"
  4589         and vpg: "valid_path g" and vph: "valid_path h"
  4590     shows "contour_integral g f = contour_integral h f"
  4591   using Cauchy_theorem_homotopic [of False s g h] assms by simp
  4592 
  4593 lemma has_contour_integral_newpath:
  4594     "\<lbrakk>(f has_contour_integral y) h; f contour_integrable_on g; contour_integral g f = contour_integral h f\<rbrakk>
  4595      \<Longrightarrow> (f has_contour_integral y) g"
  4596   using has_contour_integral_integral contour_integral_unique by auto
  4597 
  4598 lemma Cauchy_theorem_null_homotopic:
  4599      "\<lbrakk>f holomorphic_on s; open s; valid_path g; homotopic_loops s g (linepath a a)\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
  4600   apply (rule has_contour_integral_newpath [where h = "linepath a a"], simp)
  4601   using contour_integrable_holomorphic_simple
  4602     apply (blast dest: holomorphic_on_imp_continuous_on homotopic_loops_imp_subset)
  4603   by (simp add: Cauchy_theorem_homotopic_loops)
  4604 
  4605 
  4606 
  4607 subsection\<open>More winding number properties\<close>
  4608 
  4609 text\<open>including the fact that it's +-1 inside a simple closed curve.\<close>
  4610 
  4611 lemma winding_number_homotopic_paths:
  4612     assumes "homotopic_paths (-{z}) g h"
  4613       shows "winding_number g z = winding_number h z"
  4614 proof -
  4615   have "path g" "path h" using homotopic_paths_imp_path [OF assms] by auto
  4616   moreover have pag: "z \<notin> path_image g" and pah: "z \<notin> path_image h"
  4617     using homotopic_paths_imp_subset [OF assms] by auto
  4618   ultimately obtain d e where "d > 0" "e > 0"
  4619       and d: "\<And>p. \<lbrakk>path p; pathstart p = pathstart g; pathfinish p = pathfinish g; \<forall>t\<in>{0..1}. norm (p t - g t) < d\<rbrakk>
  4620             \<Longrightarrow> homotopic_paths (-{z}) g p"
  4621       and e: "\<And>q. \<lbrakk>path q; pathstart q = pathstart h; pathfinish q = pathfinish h; \<forall>t\<in>{0..1}. norm (q t - h t) < e\<rbrakk>
  4622             \<Longrightarrow> homotopic_paths (-{z}) h q"
  4623     using homotopic_nearby_paths [of g "-{z}"] homotopic_nearby_paths [of h "-{z}"] by force
  4624   obtain p where p:
  4625        "valid_path p" "z \<notin> path_image p"
  4626        "pathstart p = pathstart g" "pathfinish p = pathfinish g"
  4627        and gp_less:"\<forall>t\<in>{0..1}. cmod (g t - p t) < d"
  4628        and pap: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number g z"
  4629     using winding_number [OF \<open>path g\<close> pag \<open>0 < d\<close>] by blast
  4630   obtain q where q:
  4631        "valid_path q" "z \<notin> path_image q"
  4632        "pathstart q = pathstart h" "pathfinish q = pathfinish h"
  4633        and hq_less: "\<forall>t\<in>{0..1}. cmod (h t - q t) < e"
  4634        and paq:  "contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number h z"
  4635     using winding_number [OF \<open>path h\<close> pah \<open>0 < e\<close>] by blast
  4636   have gp: "homotopic_paths (- {z}) g p"
  4637     by (simp add: d p valid_path_imp_path norm_minus_commute gp_less)
  4638   have hq: "homotopic_paths (- {z}) h q"
  4639     by (simp add: e q valid_path_imp_path norm_minus_commute hq_less)
  4640   have "contour_integral p (\<lambda>w. 1/(w - z)) = contour_integral q (\<lambda>w. 1/(w - z))"
  4641     apply (rule Cauchy_theorem_homotopic_paths [of "-{z}"])
  4642     apply (blast intro: homotopic_paths_trans homotopic_paths_sym gp hq assms)
  4643     apply (auto intro!: holomorphic_intros simp: p q)
  4644     done
  4645   then show ?thesis
  4646     by (simp add: pap paq)
  4647 qed
  4648 
  4649 lemma winding_number_homotopic_loops:
  4650     assumes "homotopic_loops (-{z}) g h"
  4651       shows "winding_number g z = winding_number h z"
  4652 proof -
  4653   have "path g" "path h" using homotopic_loops_imp_path [OF assms] by auto
  4654   moreover have pag: "z \<notin> path_image g" and pah: "z \<notin> path_image h"
  4655     using homotopic_loops_imp_subset [OF assms] by auto
  4656   moreover have gloop: "pathfinish g = pathstart g" and hloop: "pathfinish h = pathstart h"
  4657     using homotopic_loops_imp_loop [OF assms] by auto
  4658   ultimately obtain d e where "d > 0" "e > 0"
  4659       and d: "\<And>p. \<lbrakk>path p; pathfinish p = pathstart p; \<forall>t\<in>{0..1}. norm (p t - g t) < d\<rbrakk>
  4660             \<Longrightarrow> homotopic_loops (-{z}) g p"
  4661       and e: "\<And>q. \<lbrakk>path q; pathfinish q = pathstart q; \<forall>t\<in>{0..1}. norm (q t - h t) < e\<rbrakk>
  4662             \<Longrightarrow> homotopic_loops (-{z}) h q"
  4663     using homotopic_nearby_loops [of g "-{z}"] homotopic_nearby_loops [of h "-{z}"] by force
  4664   obtain p where p:
  4665        "valid_path p" "z \<notin> path_image p"
  4666        "pathstart p = pathstart g" "pathfinish p = pathfinish g"
  4667        and gp_less:"\<forall>t\<in>{0..1}. cmod (g t - p t) < d"
  4668        and pap: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number g z"
  4669     using winding_number [OF \<open>path g\<close> pag \<open>0 < d\<close>] by blast
  4670   obtain q where q:
  4671        "valid_path q" "z \<notin> path_image q"
  4672        "pathstart q = pathstart h" "pathfinish q = pathfinish h"
  4673        and hq_less: "\<forall>t\<in>{0..1}. cmod (h t - q t) < e"
  4674        and paq:  "contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number h z"
  4675     using winding_number [OF \<open>path h\<close> pah \<open>0 < e\<close>] by blast
  4676   have gp: "homotopic_loops (- {z}) g p"
  4677     by (simp add: gloop d gp_less norm_minus_commute p valid_path_imp_path)
  4678   have hq: "homotopic_loops (- {z}) h q"
  4679     by (simp add: e hloop hq_less norm_minus_commute q valid_path_imp_path)
  4680   have "contour_integral p (\<lambda>w. 1/(w - z)) = contour_integral q (\<lambda>w. 1/(w - z))"
  4681     apply (rule Cauchy_theorem_homotopic_loops [of "-{z}"])
  4682     apply (blast intro: homotopic_loops_trans homotopic_loops_sym gp hq assms)
  4683     apply (auto intro!: holomorphic_intros simp: p q)
  4684     done
  4685   then show ?thesis
  4686     by (simp add: pap paq)
  4687 qed
  4688 
  4689 lemma winding_number_paths_linear_eq:
  4690   "\<lbrakk>path g; path h; pathstart h = pathstart g; pathfinish h = pathfinish g;
  4691     \<And>t. t \<in> {0..1} \<Longrightarrow> z \<notin> closed_segment (g t) (h t)\<rbrakk>
  4692         \<Longrightarrow> winding_number h z = winding_number g z"
  4693   by (blast intro: sym homotopic_paths_linear winding_number_homotopic_paths elim: )
  4694 
  4695 lemma winding_number_loops_linear_eq:
  4696   "\<lbrakk>path g; path h; pathfinish g = pathstart g; pathfinish h = pathstart h;
  4697     \<And>t. t \<in> {0..1} \<Longrightarrow> z \<notin> closed_segment (g t) (h t)\<rbrakk>
  4698         \<Longrightarrow> winding_number h z = winding_number g z"
  4699   by (blast intro: sym homotopic_loops_linear winding_number_homotopic_loops elim: )
  4700 
  4701 lemma winding_number_nearby_paths_eq:
  4702      "\<lbrakk>path g; path h;
  4703       pathstart h = pathstart g; pathfinish h = pathfinish g;
  4704       \<And>t. t \<in> {0..1} \<Longrightarrow> norm(h t - g t) < norm(g t - z)\<rbrakk>
  4705       \<Longrightarrow> winding_number h z = winding_number g z"
  4706   by (metis segment_bound(2) norm_minus_commute not_le winding_number_paths_linear_eq)
  4707 
  4708 lemma winding_number_nearby_loops_eq:
  4709      "\<lbrakk>path g; path h;
  4710       pathfinish g = pathstart g;
  4711         pathfinish h = pathstart h;
  4712       \<And>t. t \<in> {0..1} \<Longrightarrow> norm(h t - g t) < norm(g t - z)\<rbrakk>
  4713       \<Longrightarrow> winding_number h z = winding_number g z"
  4714   by (metis segment_bound(2) norm_minus_commute not_le winding_number_loops_linear_eq)
  4715 
  4716 
  4717 proposition winding_number_subpath_combine:
  4718     "\<lbrakk>path g; z \<notin> path_image g;
  4719       u \<in> {0..1}; v \<in> {0..1}; w \<in> {0..1}\<rbrakk>
  4720       \<Longrightarrow> winding_number (subpath u v g) z + winding_number (subpath v w g) z =
  4721           winding_number (subpath u w g) z"
  4722 apply (rule trans [OF winding_number_join [THEN sym]
  4723                       winding_number_homotopic_paths [OF homotopic_join_subpaths]])
  4724 apply (auto dest: path_image_subpath_subset)
  4725 done
  4726 
  4727 
  4728 subsection\<open>Partial circle path\<close>
  4729 
  4730 definition part_circlepath :: "[complex, real, real, real, real] \<Rightarrow> complex"
  4731   where "part_circlepath z r s t \<equiv> \<lambda>x. z + of_real r * exp (\<i> * of_real (linepath s t x))"
  4732 
  4733 lemma pathstart_part_circlepath [simp]:
  4734      "pathstart(part_circlepath z r s t) = z + r*exp(\<i> * s)"
  4735 by (metis part_circlepath_def pathstart_def pathstart_linepath)
  4736 
  4737 lemma pathfinish_part_circlepath [simp]:
  4738      "pathfinish(part_circlepath z r s t) = z + r*exp(\<i>*t)"
  4739 by (metis part_circlepath_def pathfinish_def pathfinish_linepath)
  4740 
  4741 proposition has_vector_derivative_part_circlepath [derivative_intros]:
  4742     "((part_circlepath z r s t) has_vector_derivative
  4743       (\<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)))
  4744      (at x within X)"
  4745   apply (simp add: part_circlepath_def linepath_def scaleR_conv_of_real)
  4746   apply (rule has_vector_derivative_real_complex)
  4747   apply (rule derivative_eq_intros | simp)+
  4748   done
  4749 
  4750 corollary vector_derivative_part_circlepath:
  4751     "vector_derivative (part_circlepath z r s t) (at x) =
  4752        \<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)"
  4753   using has_vector_derivative_part_circlepath vector_derivative_at by blast
  4754 
  4755 corollary vector_derivative_part_circlepath01:
  4756     "\<lbrakk>0 \<le> x; x \<le> 1\<rbrakk>
  4757      \<Longrightarrow> vector_derivative (part_circlepath z r s t) (at x within {0..1}) =
  4758           \<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)"
  4759   using has_vector_derivative_part_circlepath
  4760   by (auto simp: vector_derivative_at_within_ivl)
  4761 
  4762 lemma valid_path_part_circlepath [simp]: "valid_path (part_circlepath z r s t)"
  4763   apply (simp add: valid_path_def)
  4764   apply (rule C1_differentiable_imp_piecewise)
  4765   apply (auto simp: C1_differentiable_on_eq vector_derivative_works vector_derivative_part_circlepath has_vector_derivative_part_circlepath
  4766               intro!: continuous_intros)
  4767   done
  4768 
  4769 lemma path_part_circlepath [simp]: "path (part_circlepath z r s t)"
  4770   by (simp add: valid_path_imp_path)
  4771 
  4772 proposition path_image_part_circlepath:
  4773   assumes "s \<le> t"
  4774     shows "path_image (part_circlepath z r s t) = {z + r * exp(\<i> * of_real x) | x. s \<le> x \<and> x \<le> t}"
  4775 proof -
  4776   { fix z::real
  4777     assume "0 \<le> z" "z \<le> 1"
  4778     with \<open>s \<le> t\<close> have "\<exists>x. (exp (\<i> * linepath s t z) = exp (\<i> * of_real x)) \<and> s \<le> x \<and> x \<le> t"
  4779       apply (rule_tac x="(1 - z) * s + z * t" in exI)
  4780       apply (simp add: linepath_def scaleR_conv_of_real algebra_simps)
  4781       apply (rule conjI)
  4782       using mult_right_mono apply blast
  4783       using affine_ineq  by (metis "mult.commute")
  4784   }
  4785   moreover
  4786   { fix z
  4787     assume "s \<le> z" "z \<le> t"
  4788     then have "z + of_real r * exp (\<i> * of_real z) \<in> (\<lambda>x. z + of_real r * exp (\<i> * linepath s t x)) ` {0..1}"
  4789       apply (rule_tac x="(z - s)/(t - s)" in image_eqI)
  4790       apply (simp add: linepath_def scaleR_conv_of_real divide_simps exp_eq)
  4791       apply (auto simp: algebra_simps divide_simps)
  4792       done
  4793   }
  4794   ultimately show ?thesis
  4795     by (fastforce simp add: path_image_def part_circlepath_def)
  4796 qed
  4797 
  4798 corollary path_image_part_circlepath_subset:
  4799     "\<lbrakk>s \<le> t; 0 \<le> r\<rbrakk> \<Longrightarrow> path_image(part_circlepath z r s t) \<subseteq> sphere z r"
  4800 by (auto simp: path_image_part_circlepath sphere_def dist_norm algebra_simps norm_mult)
  4801 
  4802 proposition in_path_image_part_circlepath:
  4803   assumes "w \<in> path_image(part_circlepath z r s t)" "s \<le> t" "0 \<le> r"
  4804     shows "norm(w - z) = r"
  4805 proof -
  4806   have "w \<in> {c. dist z c = r}"
  4807     by (metis (no_types) path_image_part_circlepath_subset sphere_def subset_eq assms)
  4808   thus ?thesis
  4809     by (simp add: dist_norm norm_minus_commute)
  4810 qed
  4811 
  4812 proposition finite_bounded_log: "finite {z::complex. norm z \<le> b \<and> exp z = w}"
  4813 proof (cases "w = 0")
  4814   case True then show ?thesis by auto
  4815 next
  4816   case False
  4817   have *: "finite {x. cmod (complex_of_real (2 * real_of_int x * pi) * \<i>) \<le> b + cmod (Ln w)}"
  4818     apply (simp add: norm_mult finite_int_iff_bounded_le)
  4819     apply (rule_tac x="\<lfloor>(b + cmod (Ln w)) / (2*pi)\<rfloor>" in exI)
  4820     apply (auto simp: divide_simps le_floor_iff)
  4821     done
  4822   have [simp]: "\<And>P f. {z. P z \<and> (\<exists>n. z = f n)} = f ` {n. P (f n)}"
  4823     by blast
  4824   show ?thesis
  4825     apply (subst exp_Ln [OF False, symmetric])
  4826     apply (simp add: exp_eq)
  4827     using norm_add_leD apply (fastforce intro: finite_subset [OF _ *])
  4828     done
  4829 qed
  4830 
  4831 lemma finite_bounded_log2:
  4832   fixes a::complex
  4833     assumes "a \<noteq> 0"
  4834     shows "finite {z. norm z \<le> b \<and> exp(a*z) = w}"
  4835 proof -
  4836   have *: "finite ((\<lambda>z. z / a) ` {z. cmod z \<le> b * cmod a \<and> exp z = w})"
  4837     by (rule finite_imageI [OF finite_bounded_log])
  4838   show ?thesis
  4839     by (rule finite_subset [OF _ *]) (force simp: assms norm_mult)
  4840 qed
  4841 
  4842 proposition has_contour_integral_bound_part_circlepath_strong:
  4843   assumes fi: "(f has_contour_integral i) (part_circlepath z r s t)"
  4844       and "finite k" and le: "0 \<le> B" "0 < r" "s \<le> t"
  4845       and B: "\<And>x. x \<in> path_image(part_circlepath z r s t) - k \<Longrightarrow> norm(f x) \<le> B"
  4846     shows "cmod i \<le> B * r * (t - s)"
  4847 proof -
  4848   consider "s = t" | "s < t" using \<open>s \<le> t\<close> by linarith
  4849   then show ?thesis
  4850   proof cases
  4851     case 1 with fi [unfolded has_contour_integral]
  4852     have "i = 0"  by (simp add: vector_derivative_part_circlepath)
  4853     with assms show ?thesis by simp
  4854   next
  4855     case 2
  4856     have [simp]: "\<bar>r\<bar> = r" using \<open>r > 0\<close> by linarith
  4857     have [simp]: "cmod (complex_of_real t - complex_of_real s) = t-s"
  4858       by (metis "2" abs_of_pos diff_gt_0_iff_gt norm_of_real of_real_diff)
  4859     have "finite (part_circlepath z r s t -` {y} \<inter> {0..1})" if "y \<in> k" for y
  4860     proof -
  4861       define w where "w = (y - z)/of_real r / exp(\<i> * of_real s)"
  4862       have fin: "finite (of_real -` {z. cmod z \<le> 1 \<and> exp (\<i> * complex_of_real (t - s) * z) = w})"
  4863         apply (rule finite_vimageI [OF finite_bounded_log2])
  4864         using \<open>s < t\<close> apply (auto simp: inj_of_real)
  4865         done
  4866       show ?thesis
  4867         apply (simp add: part_circlepath_def linepath_def vimage_def)
  4868         apply (rule finite_subset [OF _ fin])
  4869         using le
  4870         apply (auto simp: w_def algebra_simps scaleR_conv_of_real exp_add exp_diff)
  4871         done
  4872     qed
  4873     then have fin01: "finite ((part_circlepath z r s t) -` k \<inter> {0..1})"
  4874       by (rule finite_finite_vimage_IntI [OF \<open>finite k\<close>])
  4875     have **: "((\<lambda>x. if (part_circlepath z r s t x) \<in> k then 0
  4876                     else f(part_circlepath z r s t x) *
  4877                        vector_derivative (part_circlepath z r s t) (at x)) has_integral i)  {0..1}"
  4878       apply (rule has_integral_spike
  4879               [where f = "\<lambda>x. f(part_circlepath z r s t x) * vector_derivative (part_circlepath z r s t) (at x)"])
  4880       apply (rule negligible_finite [OF fin01])
  4881       using fi has_contour_integral
  4882       apply auto
  4883       done
  4884     have *: "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1; part_circlepath z r s t x \<notin> k\<rbrakk> \<Longrightarrow> cmod (f (part_circlepath z r s t x)) \<le> B"
  4885       by (auto intro!: B [unfolded path_image_def image_def, simplified])
  4886     show ?thesis
  4887       apply (rule has_integral_bound [where 'a=real, simplified, OF _ **, simplified])
  4888       using assms apply force
  4889       apply (simp add: norm_mult vector_derivative_part_circlepath)
  4890       using le * "2" \<open>r > 0\<close> by auto
  4891   qed
  4892 qed
  4893 
  4894 corollary has_contour_integral_bound_part_circlepath:
  4895       "\<lbrakk>(f has_contour_integral i) (part_circlepath z r s t);
  4896         0 \<le> B; 0 < r; s \<le> t;
  4897         \<And>x. x \<in> path_image(part_circlepath z r s t) \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
  4898        \<Longrightarrow> norm i \<le> B*r*(t - s)"
  4899   by (auto intro: has_contour_integral_bound_part_circlepath_strong)
  4900 
  4901 proposition contour_integrable_continuous_part_circlepath:
  4902      "continuous_on (path_image (part_circlepath z r s t)) f
  4903       \<Longrightarrow> f contour_integrable_on (part_circlepath z r s t)"
  4904   apply (simp add: contour_integrable_on has_contour_integral_def vector_derivative_part_circlepath path_image_def)
  4905   apply (rule integrable_continuous_real)
  4906   apply (fast intro: path_part_circlepath [unfolded path_def] continuous_intros continuous_on_compose2 [where g=f, OF _ _ order_refl])
  4907   done
  4908 
  4909 proposition winding_number_part_circlepath_pos_less:
  4910   assumes "s < t" and no: "norm(w - z) < r"
  4911     shows "0 < Re (winding_number(part_circlepath z r s t) w)"
  4912 proof -
  4913   have "0 < r" by (meson no norm_not_less_zero not_le order.strict_trans2)
  4914   note valid_path_part_circlepath
  4915   moreover have " w \<notin> path_image (part_circlepath z r s t)"
  4916     using assms by (auto simp: path_image_def image_def part_circlepath_def norm_mult linepath_def)
  4917   moreover have "0 < r * (t - s) * (r - cmod (w - z))"
  4918     using assms by (metis \<open>0 < r\<close> diff_gt_0_iff_gt mult_pos_pos)
  4919   ultimately show ?thesis
  4920     apply (rule winding_number_pos_lt [where e = "r*(t - s)*(r - norm(w - z))"])
  4921     apply (simp add: vector_derivative_part_circlepath right_diff_distrib [symmetric] mult_ac)
  4922     apply (rule mult_left_mono)+
  4923     using Re_Im_le_cmod [of "w-z" "linepath s t x" for x]
  4924     apply (simp add: exp_Euler cos_of_real sin_of_real part_circlepath_def algebra_simps cos_squared_eq [unfolded power2_eq_square])
  4925     using assms \<open>0 < r\<close> by auto
  4926 qed
  4927 
  4928 proposition simple_path_part_circlepath:
  4929     "simple_path(part_circlepath z r s t) \<longleftrightarrow> (r \<noteq> 0 \<and> s \<noteq> t \<and> \<bar>s - t\<bar> \<le> 2*pi)"
  4930 proof (cases "r = 0 \<or> s = t")
  4931   case True
  4932   then show ?thesis
  4933     apply (rule disjE)
  4934     apply (force simp: part_circlepath_def simple_path_def intro: bexI [where x = "1/4"] bexI [where x = "1/3"])+
  4935     done
  4936 next
  4937   case False then have "r \<noteq> 0" "s \<noteq> t" by auto
  4938   have *: "\<And>x y z s t. \<i>*((1 - x) * s + x * t) = \<i>*(((1 - y) * s + y * t)) + z  \<longleftrightarrow> \<i>*(x - y) * (t - s) = z"
  4939     by (simp add: algebra_simps)
  4940   have abs01: "\<And>x y::real. 0 \<le> x \<and> x \<le> 1 \<and> 0 \<le> y \<and> y \<le> 1
  4941                       \<Longrightarrow> (x = y \<or> x = 0 \<and> y = 1 \<or> x = 1 \<and> y = 0 \<longleftrightarrow> \<bar>x - y\<bar> \<in> {0,1})"
  4942     by auto
  4943   have abs_away: "\<And>P. (\<forall>x\<in>{0..1}. \<forall>y\<in>{0..1}. P \<bar>x - y\<bar>) \<longleftrightarrow> (\<forall>x::real. 0 \<le> x \<and> x \<le> 1 \<longrightarrow> P x)"
  4944     by force
  4945   have **: "\<And>x y. (\<exists>n. (complex_of_real x - of_real y) * (of_real t - of_real s) = 2 * (of_int n * of_real pi)) \<longleftrightarrow>
  4946                   (\<exists>n. \<bar>x - y\<bar> * (t - s) = 2 * (of_int n * pi))"
  4947     by (force simp: algebra_simps abs_if dest: arg_cong [where f=Re] arg_cong [where f=complex_of_real]
  4948                     intro: exI [where x = "-n" for n])
  4949   have 1: "\<forall>x. 0 \<le> x \<and> x \<le> 1 \<longrightarrow> (\<exists>n. x * (t - s) = 2 * (real_of_int n * pi)) \<longrightarrow> x = 0 \<or> x = 1 \<Longrightarrow> \<bar>s - t\<bar> \<le> 2 * pi"
  4950     apply (rule ccontr)
  4951     apply (drule_tac x="2*pi / \<bar>t - s\<bar>" in spec)
  4952     using False
  4953     apply (simp add: abs_minus_commute divide_simps)
  4954     apply (frule_tac x=1 in spec)
  4955     apply (drule_tac x="-1" in spec)
  4956     apply (simp add:)
  4957     done
  4958   have 2: "\<bar>s - t\<bar> = \<bar>2 * (real_of_int n * pi) / x\<bar>" if "x \<noteq> 0" "x * (t - s) = 2 * (real_of_int n * pi)" for x n
  4959   proof -
  4960     have "t-s = 2 * (real_of_int n * pi)/x"
  4961       using that by (simp add: field_simps)
  4962     then show ?thesis by (metis abs_minus_commute)
  4963   qed
  4964   show ?thesis using False
  4965     apply (simp add: simple_path_def path_part_circlepath)
  4966     apply (simp add: part_circlepath_def linepath_def exp_eq  * ** abs01  del: Set.insert_iff)
  4967     apply (subst abs_away)
  4968     apply (auto simp: 1)
  4969     apply (rule ccontr)
  4970     apply (auto simp: 2 divide_simps abs_mult dest: of_int_leD)
  4971     done
  4972 qed
  4973 
  4974 proposition arc_part_circlepath:
  4975   assumes "r \<noteq> 0" "s \<noteq> t" "\<bar>s - t\<bar> < 2*pi"
  4976     shows "arc (part_circlepath z r s t)"
  4977 proof -
  4978   have *: "x = y" if eq: "\<i> * (linepath s t x) = \<i> * (linepath s t y) + 2 * of_int n * complex_of_real pi * \<i>"
  4979                   and x: "x \<in> {0..1}" and y: "y \<in> {0..1}" for x y n
  4980     proof -
  4981       have "(linepath s t x) = (linepath s t y) + 2 * of_int n * complex_of_real pi"
  4982         by (metis add_divide_eq_iff complex_i_not_zero mult.commute nonzero_mult_div_cancel_left eq)
  4983       then have "s*y + t*x = s*x + (t*y + of_int n * (pi * 2))"
  4984         by (force simp: algebra_simps linepath_def dest: arg_cong [where f=Re])
  4985       then have st: "x \<noteq> y \<Longrightarrow> (s-t) = (of_int n * (pi * 2) / (y-x))"
  4986         by (force simp: field_simps)
  4987       show ?thesis
  4988         apply (rule ccontr)
  4989         using assms x y
  4990         apply (simp add: st abs_mult field_simps)
  4991         using st
  4992         apply (auto simp: dest: of_int_lessD)
  4993         done
  4994     qed
  4995   show ?thesis
  4996     using assms
  4997     apply (simp add: arc_def)
  4998     apply (simp add: part_circlepath_def inj_on_def exp_eq)
  4999     apply (blast intro: *)
  5000     done
  5001 qed
  5002 
  5003 
  5004 subsection\<open>Special case of one complete circle\<close>
  5005 
  5006 definition circlepath :: "[complex, real, real] \<Rightarrow> complex"
  5007   where "circlepath z r \<equiv> part_circlepath z r 0 (2*pi)"
  5008 
  5009 lemma circlepath: "circlepath z r = (\<lambda>x. z + r * exp(2 * of_real pi * \<i> * of_real x))"
  5010   by (simp add: circlepath_def part_circlepath_def linepath_def algebra_simps)
  5011 
  5012 lemma pathstart_circlepath [simp]: "pathstart (circlepath z r) = z + r"
  5013   by (simp add: circlepath_def)
  5014 
  5015 lemma pathfinish_circlepath [simp]: "pathfinish (circlepath z r) = z + r"
  5016   by (simp add: circlepath_def) (metis exp_two_pi_i mult.commute)
  5017 
  5018 lemma circlepath_minus: "circlepath z (-r) x = circlepath z r (x + 1/2)"
  5019 proof -
  5020   have "z + of_real r * exp (2 * pi * \<i> * (x + 1 / 2)) =
  5021         z + of_real r * exp (2 * pi * \<i> * x + pi * \<i>)"
  5022     by (simp add: divide_simps) (simp add: algebra_simps)
  5023   also have "... = z - r * exp (2 * pi * \<i> * x)"
  5024     by (simp add: exp_add)
  5025   finally show ?thesis
  5026     by (simp add: circlepath path_image_def sphere_def dist_norm)
  5027 qed
  5028 
  5029 lemma circlepath_add1: "circlepath z r (x+1) = circlepath z r x"
  5030   using circlepath_minus [of z r "x+1/2"] circlepath_minus [of z "-r" x]
  5031   by (simp add: add.commute)
  5032 
  5033 lemma circlepath_add_half: "circlepath z r (x + 1/2) = circlepath z r (x - 1/2)"
  5034   using circlepath_add1 [of z r "x-1/2"]
  5035   by (simp add: add.commute)
  5036 
  5037 lemma path_image_circlepath_minus_subset:
  5038      "path_image (circlepath z (-r)) \<subseteq> path_image (circlepath z r)"
  5039   apply (simp add: path_image_def image_def circlepath_minus, clarify)
  5040   apply (case_tac "xa \<le> 1/2", force)
  5041   apply (force simp add: circlepath_add_half)+
  5042   done
  5043 
  5044 lemma path_image_circlepath_minus: "path_image (circlepath z (-r)) = path_image (circlepath z r)"
  5045   using path_image_circlepath_minus_subset by fastforce
  5046 
  5047 proposition has_vector_derivative_circlepath [derivative_intros]:
  5048  "((circlepath z r) has_vector_derivative (2 * pi * \<i> * r * exp (2 * of_real pi * \<i> * of_real x)))
  5049    (at x within X)"
  5050   apply (simp add: circlepath_def scaleR_conv_of_real)
  5051   apply (rule derivative_eq_intros)
  5052   apply (simp add: algebra_simps)
  5053   done
  5054 
  5055 corollary vector_derivative_circlepath:
  5056    "vector_derivative (circlepath z r) (at x) =
  5057     2 * pi * \<i> * r * exp(2 * of_real pi * \<i> * x)"
  5058 using has_vector_derivative_circlepath vector_derivative_at by blast
  5059 
  5060 corollary vector_derivative_circlepath01:
  5061     "\<lbrakk>0 \<le> x; x \<le> 1\<rbrakk>
  5062      \<Longrightarrow> vector_derivative (circlepath z r) (at x within {0..1}) =
  5063           2 * pi * \<i> * r * exp(2 * of_real pi * \<i> * x)"
  5064   using has_vector_derivative_circlepath
  5065   by (auto simp: vector_derivative_at_within_ivl)
  5066 
  5067 lemma valid_path_circlepath [simp]: "valid_path (circlepath z r)"
  5068   by (simp add: circlepath_def)
  5069 
  5070 lemma path_circlepath [simp]: "path (circlepath z r)"
  5071   by (simp add: valid_path_imp_path)
  5072 
  5073 lemma path_image_circlepath_nonneg:
  5074   assumes "0 \<le> r" shows "path_image (circlepath z r) = sphere z r"
  5075 proof -
  5076   have *: "x \<in> (\<lambda>u. z + (cmod (x - z)) * exp (\<i> * (of_real u * (of_real pi * 2)))) ` {0..1}" for x
  5077   proof (cases "x = z")
  5078     case True then show ?thesis by force
  5079   next
  5080     case False
  5081     define w where "w = x - z"
  5082     then have "w \<noteq> 0" by (simp add: False)
  5083     have **: "\<And>t. \<lbrakk>Re w = cos t * cmod w; Im w = sin t * cmod w\<rbrakk> \<Longrightarrow> w = of_real (cmod w) * exp (\<i> * t)"
  5084       using cis_conv_exp complex_eq_iff by auto
  5085     show ?thesis
  5086       apply (rule sincos_total_2pi [of "Re(w/of_real(norm w))" "Im(w/of_real(norm w))"])
  5087       apply (simp add: divide_simps \<open>w \<noteq> 0\<close> cmod_power2 [symmetric])
  5088       apply (rule_tac x="t / (2*pi)" in image_eqI)
  5089       apply (simp add: divide_simps \<open>w \<noteq> 0\<close>)
  5090       using False **
  5091       apply (auto simp: w_def)
  5092       done
  5093   qed
  5094   show ?thesis
  5095     unfolding circlepath path_image_def sphere_def dist_norm
  5096     by (force simp: assms algebra_simps norm_mult norm_minus_commute intro: *)
  5097 qed
  5098 
  5099 proposition path_image_circlepath [simp]:
  5100     "path_image (circlepath z r) = sphere z \<bar>r\<bar>"
  5101   using path_image_circlepath_minus
  5102   by (force simp add: path_image_circlepath_nonneg abs_if)
  5103 
  5104 lemma has_contour_integral_bound_circlepath_strong:
  5105       "\<lbrakk>(f has_contour_integral i) (circlepath z r);
  5106         finite k; 0 \<le> B; 0 < r;
  5107         \<And>x. \<lbrakk>norm(x - z) = r; x \<notin> k\<rbrakk> \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
  5108         \<Longrightarrow> norm i \<le> B*(2*pi*r)"
  5109   unfolding circlepath_def
  5110   by (auto simp: algebra_simps in_path_image_part_circlepath dest!: has_contour_integral_bound_part_circlepath_strong)
  5111 
  5112 corollary has_contour_integral_bound_circlepath:
  5113       "\<lbrakk>(f has_contour_integral i) (circlepath z r);
  5114         0 \<le> B; 0 < r; \<And>x. norm(x - z) = r \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
  5115         \<Longrightarrow> norm i \<le> B*(2*pi*r)"
  5116   by (auto intro: has_contour_integral_bound_circlepath_strong)
  5117 
  5118 proposition contour_integrable_continuous_circlepath:
  5119     "continuous_on (path_image (circlepath z r)) f
  5120      \<Longrightarrow> f contour_integrable_on (circlepath z r)"
  5121   by (simp add: circlepath_def contour_integrable_continuous_part_circlepath)
  5122 
  5123 lemma simple_path_circlepath: "simple_path(circlepath z r) \<longleftrightarrow> (r \<noteq> 0)"
  5124   by (simp add: circlepath_def simple_path_part_circlepath)
  5125 
  5126 lemma notin_path_image_circlepath [simp]: "cmod (w - z) < r \<Longrightarrow> w \<notin> path_image (circlepath z r)"
  5127   by (simp add: sphere_def dist_norm norm_minus_commute)
  5128 
  5129 proposition contour_integral_circlepath:
  5130      "0 < r \<Longrightarrow> contour_integral (circlepath z r) (\<lambda>w. 1 / (w - z)) = 2 * complex_of_real pi * \<i>"
  5131   apply (rule contour_integral_unique)
  5132   apply (simp add: has_contour_integral_def)
  5133   apply (subst has_integral_cong)
  5134   apply (simp add: vector_derivative_circlepath01)
  5135   using has_integral_const_real [of _ 0 1]
  5136   apply (force simp: circlepath)
  5137   done
  5138 
  5139 lemma winding_number_circlepath_centre: "0 < r \<Longrightarrow> winding_number (circlepath z r) z = 1"
  5140   apply (rule winding_number_unique_loop)
  5141   apply (simp_all add: sphere_def valid_path_imp_path)
  5142   apply (rule_tac x="circlepath z r" in exI)
  5143   apply (simp add: sphere_def contour_integral_circlepath)
  5144   done
  5145 
  5146 proposition winding_number_circlepath:
  5147   assumes "norm(w - z) < r" shows "winding_number(circlepath z r) w = 1"
  5148 proof (cases "w = z")
  5149   case True then show ?thesis
  5150     using assms winding_number_circlepath_centre by auto
  5151 next
  5152   case False
  5153   have [simp]: "r > 0"
  5154     using assms le_less_trans norm_ge_zero by blast
  5155   define r' where "r' = norm(w - z)"
  5156   have "r' < r"
  5157     by (simp add: assms r'_def)
  5158   have disjo: "cball z r' \<inter> sphere z r = {}"
  5159     using \<open>r' < r\<close> by (force simp: cball_def sphere_def)
  5160   have "winding_number(circlepath z r) w = winding_number(circlepath z r) z"
  5161     apply (rule winding_number_around_inside [where s = "cball z r'"])
  5162     apply (simp_all add: disjo order.strict_implies_order winding_number_circlepath_centre)
  5163     apply (simp_all add: False r'_def dist_norm norm_minus_commute)
  5164     done
  5165   also have "... = 1"
  5166     by (simp add: winding_number_circlepath_centre)
  5167   finally show ?thesis .
  5168 qed
  5169 
  5170 
  5171 text\<open> Hence the Cauchy formula for points inside a circle.\<close>
  5172 
  5173 theorem Cauchy_integral_circlepath:
  5174   assumes "continuous_on (cball z r) f" "f holomorphic_on (ball z r)" "norm(w - z) < r"
  5175   shows "((\<lambda>u. f u/(u - w)) has_contour_integral (2 * of_real pi * \<i> * f w))
  5176          (circlepath z r)"
  5177 proof -
  5178   have "r > 0"
  5179     using assms le_less_trans norm_ge_zero by blast
  5180   have "((\<lambda>u. f u / (u - w)) has_contour_integral (2 * pi) * \<i> * winding_number (circlepath z r) w * f w)
  5181         (circlepath z r)"
  5182     apply (rule Cauchy_integral_formula_weak [where s = "cball z r" and k = "{}"])
  5183     using assms  \<open>r > 0\<close>
  5184     apply (simp_all add: dist_norm norm_minus_commute)
  5185     apply (metis at_within_interior dist_norm holomorphic_on_def interior_ball mem_ball norm_minus_commute)
  5186     apply (simp add: cball_def sphere_def dist_norm, clarify)
  5187     apply (simp add:)
  5188     by (metis dist_commute dist_norm less_irrefl)
  5189   then show ?thesis
  5190     by (simp add: winding_number_circlepath assms)
  5191 qed
  5192 
  5193 corollary Cauchy_integral_circlepath_simple:
  5194   assumes "f holomorphic_on cball z r" "norm(w - z) < r"
  5195   shows "((\<lambda>u. f u/(u - w)) has_contour_integral (2 * of_real pi * \<i> * f w))
  5196          (circlepath z r)"
  5197 using assms by (force simp: holomorphic_on_imp_continuous_on holomorphic_on_subset Cauchy_integral_circlepath)
  5198 
  5199 
  5200 lemma no_bounded_connected_component_imp_winding_number_zero:
  5201   assumes g: "path g" "path_image g \<subseteq> s" "pathfinish g = pathstart g" "z \<notin> s"
  5202       and nb: "\<And>z. bounded (connected_component_set (- s) z) \<longrightarrow> z \<in> s"
  5203   shows "winding_number g z = 0"
  5204 apply (rule winding_number_zero_in_outside)
  5205 apply (simp_all add: assms)
  5206 by (metis nb [of z] \<open>path_image g \<subseteq> s\<close> \<open>z \<notin> s\<close> contra_subsetD mem_Collect_eq outside outside_mono)
  5207 
  5208 lemma no_bounded_path_component_imp_winding_number_zero:
  5209   assumes g: "path g" "path_image g \<subseteq> s" "pathfinish g = pathstart g" "z \<notin> s"
  5210       and nb: "\<And>z. bounded (path_component_set (- s) z) \<longrightarrow> z \<in> s"
  5211   shows "winding_number g z = 0"
  5212 apply (rule no_bounded_connected_component_imp_winding_number_zero [OF g])
  5213 by (simp add: bounded_subset nb path_component_subset_connected_component)
  5214 
  5215 
  5216 subsection\<open> Uniform convergence of path integral\<close>
  5217 
  5218 text\<open>Uniform convergence when the derivative of the path is bounded, and in particular for the special case of a circle.\<close>
  5219 
  5220 proposition contour_integral_uniform_limit:
  5221   assumes ev_fint: "eventually (\<lambda>n::'a. (f n) contour_integrable_on \<gamma>) F"
  5222       and ul_f: "uniform_limit (path_image \<gamma>) f l F"
  5223       and noleB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
  5224       and \<gamma>: "valid_path \<gamma>"
  5225       and [simp]: "~ (trivial_limit F)"
  5226   shows "l contour_integrable_on \<gamma>" "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
  5227 proof -
  5228   have "0 \<le> B" by (meson noleB [of 0] atLeastAtMost_iff norm_ge_zero order_refl order_trans zero_le_one)
  5229   { fix e::real
  5230     assume "0 < e"
  5231     then have "0 < e / (\<bar>B\<bar> + 1)" by simp
  5232     then have "\<forall>\<^sub>F n in F. \<forall>x\<in>path_image \<gamma>. cmod (f n x - l x) < e / (\<bar>B\<bar> + 1)"
  5233       using ul_f [unfolded uniform_limit_iff dist_norm] by auto
  5234     with ev_fint
  5235     obtain a where fga: "\<And>x. x \<in> {0..1} \<Longrightarrow> cmod (f a (\<gamma> x) - l (\<gamma> x)) < e / (\<bar>B\<bar> + 1)"
  5236                and inta: "(\<lambda>t. f a (\<gamma> t) * vector_derivative \<gamma> (at t)) integrable_on {0..1}"
  5237       using eventually_happens [OF eventually_conj]
  5238       by (fastforce simp: contour_integrable_on path_image_def)
  5239     have Ble: "B * e / (\<bar>B\<bar> + 1) \<le> e"
  5240       using \<open>0 \<le> B\<close>  \<open>0 < e\<close> by (simp add: divide_simps)
  5241     have "\<exists>h. (\<forall>x\<in>{0..1}. cmod (l (\<gamma> x) * vector_derivative \<gamma> (at x) - h x) \<le> e) \<and> h integrable_on {0..1}"
  5242       apply (rule_tac x="\<lambda>x. f (a::'a) (\<gamma> x) * vector_derivative \<gamma> (at x)" in exI)
  5243       apply (intro inta conjI ballI)
  5244       apply (rule order_trans [OF _ Ble])
  5245       apply (frule noleB)
  5246       apply (frule fga)
  5247       using \<open>0 \<le> B\<close>  \<open>0 < e\<close>
  5248       apply (simp add: norm_mult left_diff_distrib [symmetric] norm_minus_commute divide_simps)
  5249       apply (drule (1) mult_mono [OF less_imp_le])
  5250       apply (simp_all add: mult_ac)
  5251       done
  5252   }
  5253   then show lintg: "l contour_integrable_on \<gamma>"
  5254     apply (simp add: contour_integrable_on)
  5255     apply (blast intro: integrable_uniform_limit_real)
  5256     done
  5257   { fix e::real
  5258     define B' where "B' = B + 1"
  5259     have B': "B' > 0" "B' > B" using  \<open>0 \<le> B\<close> by (auto simp: B'_def)
  5260     assume "0 < e"
  5261     then have ev_no': "\<forall>\<^sub>F n in F. \<forall>x\<in>path_image \<gamma>. 2 * cmod (f n x - l x) < e / B'"
  5262       using ul_f [unfolded uniform_limit_iff dist_norm, rule_format, of "e / B' / 2"] B' 
  5263         by (simp add: field_simps)
  5264     have ie: "integral {0..1::real} (\<lambda>x. e / 2) < e" using \<open>0 < e\<close> by simp
  5265     have *: "cmod (f x (\<gamma> t) * vector_derivative \<gamma> (at t) - l (\<gamma> t) * vector_derivative \<gamma> (at t)) \<le> e / 2"
  5266              if t: "t\<in>{0..1}" and leB': "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) < e / B'" for x t
  5267     proof -
  5268       have "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) * cmod (vector_derivative \<gamma> (at t)) \<le> e * (B/ B')"
  5269         using mult_mono [OF less_imp_le [OF leB'] noleB] B' \<open>0 < e\<close> t by auto
  5270       also have "... < e"
  5271         by (simp add: B' \<open>0 < e\<close> mult_imp_div_pos_less)
  5272       finally have "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) * cmod (vector_derivative \<gamma> (at t)) < e" .
  5273       then show ?thesis
  5274         by (simp add: left_diff_distrib [symmetric] norm_mult)
  5275     qed
  5276     have "\<forall>\<^sub>F x in F. dist (contour_integral \<gamma> (f x)) (contour_integral \<gamma> l) < e"
  5277       apply (rule eventually_mono [OF eventually_conj [OF ev_no' ev_fint]])
  5278       apply (simp add: dist_norm contour_integrable_on path_image_def contour_integral_integral)
  5279       apply (simp add: lintg integral_diff [symmetric] contour_integrable_on [symmetric], clarify)
  5280       apply (rule le_less_trans [OF integral_norm_bound_integral ie])
  5281       apply (simp add: lintg integrable_diff contour_integrable_on [symmetric])
  5282       apply (blast intro: *)+
  5283       done
  5284   }
  5285   then show "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
  5286     by (rule tendstoI)
  5287 qed
  5288 
  5289 corollary contour_integral_uniform_limit_circlepath:
  5290   assumes "\<forall>\<^sub>F n::'a in F. (f n) contour_integrable_on (circlepath z r)"
  5291       and "uniform_limit (sphere z r) f l F"
  5292       and "~ (trivial_limit F)" "0 < r"
  5293     shows "l contour_integrable_on (circlepath z r)"
  5294           "((\<lambda>n. contour_integral (circlepath z r) (f n)) \<longlongrightarrow> contour_integral (circlepath z r) l) F"
  5295   using assms by (auto simp: vector_derivative_circlepath norm_mult intro!: contour_integral_uniform_limit)
  5296 
  5297 
  5298 subsection\<open> General stepping result for derivative formulas.\<close>
  5299 
  5300 proposition Cauchy_next_derivative:
  5301   assumes "continuous_on (path_image \<gamma>) f'"
  5302       and leB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
  5303       and int: "\<And>w. w \<in> s - path_image \<gamma> \<Longrightarrow> ((\<lambda>u. f' u / (u - w)^k) has_contour_integral f w) \<gamma>"
  5304       and k: "k \<noteq> 0"
  5305       and "open s"
  5306       and \<gamma>: "valid_path \<gamma>"
  5307       and w: "w \<in> s - path_image \<gamma>"
  5308     shows "(\<lambda>u. f' u / (u - w)^(Suc k)) contour_integrable_on \<gamma>"
  5309       and "(f has_field_derivative (k * contour_integral \<gamma> (\<lambda>u. f' u/(u - w)^(Suc k))))
  5310            (at w)"  (is "?thes2")
  5311 proof -
  5312   have "open (s - path_image \<gamma>)" using \<open>open s\<close> closed_valid_path_image \<gamma> by blast
  5313   then obtain d where "d>0" and d: "ball w d \<subseteq> s - path_image \<gamma>" using w
  5314     using open_contains_ball by blast
  5315   have [simp]: "\<And>n. cmod (1 + of_nat n) = 1 + of_nat n"
  5316     by (metis norm_of_nat of_nat_Suc)
  5317   have 1: "\<forall>\<^sub>F n in at w. (\<lambda>x. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k)
  5318                          contour_integrable_on \<gamma>"
  5319     apply (simp add: eventually_at)
  5320     apply (rule_tac x=d in exI)
  5321     apply (simp add: \<open>d > 0\<close> dist_norm field_simps, clarify)
  5322     apply (rule contour_integrable_div [OF contour_integrable_diff])
  5323     using int w d
  5324     apply (force simp:  dist_norm norm_minus_commute intro!: has_contour_integral_integrable)+
  5325     done
  5326   have bim_g: "bounded (image f' (path_image \<gamma>))"
  5327     by (simp add: compact_imp_bounded compact_continuous_image compact_valid_path_image assms)
  5328   then obtain C where "C > 0" and C: "\<And>x. \<lbrakk>0 \<le> x; x \<le> 1\<rbrakk> \<Longrightarrow> cmod (f' (\<gamma> x)) \<le> C"
  5329     by (force simp: bounded_pos path_image_def)
  5330   have twom: "\<forall>\<^sub>F n in at w.
  5331                \<forall>x\<in>path_image \<gamma>.
  5332                 cmod ((inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / k - inverse (x - w) ^ Suc k) < e"
  5333          if "0 < e" for e
  5334   proof -
  5335     have *: "cmod ((inverse (x - u) ^ k - inverse (x - w) ^ k) / ((u - w) * k) - inverse (x - w) ^ Suc k)   < e"
  5336             if x: "x \<in> path_image \<gamma>" and "u \<noteq> w" and uwd: "cmod (u - w) < d/2"
  5337                 and uw_less: "cmod (u - w) < e * (d / 2) ^ (k+2) / (1 + real k)"
  5338             for u x
  5339     proof -
  5340       define ff where [abs_def]:
  5341         "ff n w =
  5342           (if n = 0 then inverse(x - w)^k
  5343            else if n = 1 then k / (x - w)^(Suc k)
  5344            else (k * of_real(Suc k)) / (x - w)^(k + 2))" for n :: nat and w
  5345       have km1: "\<And>z::complex. z \<noteq> 0 \<Longrightarrow> z ^ (k - Suc 0) = z ^ k / z"
  5346         by (simp add: field_simps) (metis Suc_pred \<open>k \<noteq> 0\<close> neq0_conv power_Suc)
  5347       have ff1: "(ff i has_field_derivative ff (Suc i) z) (at z within ball w (d / 2))"
  5348               if "z \<in> ball w (d / 2)" "i \<le> 1" for i z
  5349       proof -
  5350         have "z \<notin> path_image \<gamma>"
  5351           using \<open>x \<in> path_image \<gamma>\<close> d that ball_divide_subset_numeral by blast
  5352         then have xz[simp]: "x \<noteq> z" using \<open>x \<in> path_image \<gamma>\<close> by blast
  5353         then have neq: "x * x + z * z \<noteq> x * (z * 2)"
  5354           by (blast intro: dest!: sum_sqs_eq)
  5355         with xz have "\<And>v. v \<noteq> 0 \<Longrightarrow> (x * x + z * z) * v \<noteq> (x * (z * 2) * v)" by auto
  5356         then have neqq: "\<And>v. v \<noteq> 0 \<Longrightarrow> x * (x * v) + z * (z * v) \<noteq> x * (z * (2 * v))"
  5357           by (simp add: algebra_simps)
  5358         show ?thesis using \<open>i \<le> 1\<close>
  5359           apply (simp add: ff_def dist_norm Nat.le_Suc_eq km1, safe)
  5360           apply (rule derivative_eq_intros | simp add: km1 | simp add: field_simps neq neqq)+
  5361           done
  5362       qed
  5363       { fix a::real and b::real assume ab: "a > 0" "b > 0"
  5364         then have "k * (1 + real k) * (1 / a) \<le> k * (1 + real k) * (4 / b) \<longleftrightarrow> b \<le> 4 * a"
  5365           apply (subst mult_le_cancel_left_pos)
  5366           using \<open>k \<noteq> 0\<close>
  5367           apply (auto simp: divide_simps)
  5368           done
  5369         with ab have "real k * (1 + real k) / a \<le> (real k * 4 + real k * real k * 4) / b \<longleftrightarrow> b \<le> 4 * a"
  5370           by (simp add: field_simps)
  5371       } note canc = this
  5372       have ff2: "cmod (ff (Suc 1) v) \<le> real (k * (k + 1)) / (d / 2) ^ (k + 2)"
  5373                 if "v \<in> ball w (d / 2)" for v
  5374       proof -
  5375         have "d/2 \<le> cmod (x - v)" using d x that
  5376           apply (simp add: dist_norm path_image_def ball_def not_less [symmetric] del: divide_const_simps, clarify)
  5377           apply (drule subsetD)
  5378            prefer 2 apply blast
  5379           apply (metis norm_minus_commute norm_triangle_half_r CollectI)
  5380           done
  5381         then have "d \<le> cmod (x - v) * 2"
  5382           by (simp add: divide_simps)
  5383         then have dpow_le: "d ^ (k+2) \<le> (cmod (x - v) * 2) ^ (k+2)"
  5384           using \<open>0 < d\<close> order_less_imp_le power_mono by blast
  5385         have "x \<noteq> v" using that
  5386           using \<open>x \<in> path_image \<gamma>\<close> ball_divide_subset_numeral d by fastforce
  5387         then show ?thesis
  5388         using \<open>d > 0\<close>
  5389         apply (simp add: ff_def norm_mult norm_divide norm_power dist_norm canc)
  5390         using dpow_le
  5391         apply (simp add: algebra_simps divide_simps mult_less_0_iff)
  5392         done
  5393       qed
  5394       have ub: "u \<in> ball w (d / 2)"
  5395         using uwd by (simp add: dist_commute dist_norm)
  5396       have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
  5397                   \<le> (real k * 4 + real k * real k * 4) * (cmod (u - w) * cmod (u - w)) / (d * (d * (d / 2) ^ k))"
  5398         using complex_taylor [OF _ ff1 ff2 _ ub, of w, simplified]
  5399         by (simp add: ff_def \<open>0 < d\<close>)
  5400       then have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
  5401                   \<le> (cmod (u - w) * real k) * (1 + real k) * cmod (u - w) / (d / 2) ^ (k+2)"
  5402         by (simp add: field_simps)
  5403       then have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
  5404                  / (cmod (u - w) * real k)
  5405                   \<le> (1 + real k) * cmod (u - w) / (d / 2) ^ (k+2)"
  5406         using \<open>k \<noteq> 0\<close> \<open>u \<noteq> w\<close> by (simp add: mult_ac zero_less_mult_iff pos_divide_le_eq)
  5407       also have "... < e"
  5408         using uw_less \<open>0 < d\<close> by (simp add: mult_ac divide_simps)
  5409       finally have e: "cmod (inverse (x-u)^k - (inverse (x-w)^k + of_nat k * (u-w) / ((x-w) * (x-w)^k)))
  5410                         / cmod ((u - w) * real k)   <   e"
  5411         by (simp add: norm_mult)
  5412       have "x \<noteq> u"
  5413         using uwd \<open>0 < d\<close> x d by (force simp: dist_norm ball_def norm_minus_commute)
  5414       show ?thesis
  5415         apply (rule le_less_trans [OF _ e])
  5416         using \<open>k \<noteq> 0\<close> \<open>x \<noteq> u\<close>  \<open>u \<noteq> w\<close>
  5417         apply (simp add: field_simps norm_divide [symmetric])
  5418         done
  5419     qed
  5420     show ?thesis
  5421       unfolding eventually_at
  5422       apply (rule_tac x = "min (d/2) ((e*(d/2)^(k + 2))/(Suc k))" in exI)
  5423       apply (force simp: \<open>d > 0\<close> dist_norm that simp del: power_Suc intro: *)
  5424       done
  5425   qed
  5426   have 2: "uniform_limit (path_image \<gamma>) (\<lambda>n x. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k) (\<lambda>x. f' x / (x - w) ^ Suc k) (at w)"
  5427     unfolding uniform_limit_iff dist_norm
  5428   proof clarify
  5429     fix e::real
  5430     assume "0 < e"
  5431     have *: "cmod (f' (\<gamma> x) * (inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
  5432                         f' (\<gamma> x) / ((\<gamma> x - w) * (\<gamma> x - w) ^ k)) < e"
  5433               if ec: "cmod ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
  5434                       inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k) < e / C"
  5435                  and x: "0 \<le> x" "x \<le> 1"
  5436               for u x
  5437     proof (cases "(f' (\<gamma> x)) = 0")
  5438       case True then show ?thesis by (simp add: \<open>0 < e\<close>)
  5439     next
  5440       case False
  5441       have "cmod (f' (\<gamma> x) * (inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
  5442                         f' (\<gamma> x) / ((\<gamma> x - w) * (\<gamma> x - w) ^ k)) =
  5443             cmod (f' (\<gamma> x) * ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
  5444                              inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k))"
  5445         by (simp add: field_simps)
  5446       also have "... = cmod (f' (\<gamma> x)) *
  5447                        cmod ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
  5448                              inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k)"
  5449         by (simp add: norm_mult)
  5450       also have "... < cmod (f' (\<gamma> x)) * (e/C)"
  5451         apply (rule mult_strict_left_mono [OF ec])
  5452         using False by simp
  5453       also have "... \<le> e" using C
  5454         by (metis False \<open>0 < e\<close> frac_le less_eq_real_def mult.commute pos_le_divide_eq x zero_less_norm_iff)
  5455       finally show ?thesis .
  5456     qed
  5457     show "\<forall>\<^sub>F n in at w.
  5458               \<forall>x\<in>path_image \<gamma>.
  5459                cmod (f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k - f' x / (x - w) ^ Suc k) < e"
  5460       using twom [OF divide_pos_pos [OF \<open>0 < e\<close> \<open>C > 0\<close>]]   unfolding path_image_def
  5461       by (force intro: * elim: eventually_mono)
  5462   qed
  5463   show "(\<lambda>u. f' u / (u - w) ^ (Suc k)) contour_integrable_on \<gamma>"
  5464     by (rule contour_integral_uniform_limit [OF 1 2 leB \<gamma>]) auto
  5465   have *: "(\<lambda>n. contour_integral \<gamma> (\<lambda>x. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / k))
  5466            \<midarrow>w\<rightarrow> contour_integral \<gamma> (\<lambda>u. f' u / (u - w) ^ (Suc k))"
  5467     by (rule contour_integral_uniform_limit [OF 1 2 leB \<gamma>]) auto
  5468   have **: "contour_integral \<gamma> (\<lambda>x. f' x * (inverse (x - u) ^ k - inverse (x - w) ^ k) / ((u - w) * k)) =
  5469               (f u - f w) / (u - w) / k"
  5470            if "dist u w < d" for u
  5471     apply (rule contour_integral_unique)
  5472     apply (simp add: diff_divide_distrib algebra_simps)
  5473     apply (rule has_contour_integral_diff; rule has_contour_integral_div; simp add: field_simps; rule int)
  5474     apply (metis contra_subsetD d dist_commute mem_ball that)
  5475     apply (rule w)
  5476     done
  5477   show ?thes2
  5478     apply (simp add: DERIV_within_iff del: power_Suc)
  5479     apply (rule Lim_transform_within [OF tendsto_mult_left [OF *] \<open>0 < d\<close> ])
  5480     apply (simp add: \<open>k \<noteq> 0\<close> **)
  5481     done
  5482 qed
  5483 
  5484 corollary Cauchy_next_derivative_circlepath:
  5485   assumes contf: "continuous_on (path_image (circlepath z r)) f"
  5486       and int: "\<And>w. w \<in> ball z r \<Longrightarrow> ((\<lambda>u. f u / (u - w)^k) has_contour_integral g w) (circlepath z r)"
  5487       and k: "k \<noteq> 0"
  5488       and w: "w \<in> ball z r"
  5489     shows "(\<lambda>u. f u / (u - w)^(Suc k)) contour_integrable_on (circlepath z r)"
  5490            (is "?thes1")
  5491       and "(g has_field_derivative (k * contour_integral (circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k)))) (at w)"
  5492            (is "?thes2")
  5493 proof -
  5494   have "r > 0" using w
  5495     using ball_eq_empty by fastforce
  5496   have wim: "w \<in> ball z r - path_image (circlepath z r)"
  5497     using w by (auto simp: dist_norm)
  5498   show ?thes1 ?thes2
  5499     by (rule Cauchy_next_derivative [OF contf _ int k open_ball valid_path_circlepath wim, where B = "2 * pi * \<bar>r\<bar>"];
  5500         auto simp: vector_derivative_circlepath norm_mult)+
  5501 qed
  5502 
  5503 
  5504 text\<open> In particular, the first derivative formula.\<close>
  5505 
  5506 proposition Cauchy_derivative_integral_circlepath:
  5507   assumes contf: "continuous_on (cball z r) f"
  5508       and holf: "f holomorphic_on ball z r"
  5509       and w: "w \<in> ball z r"
  5510     shows "(\<lambda>u. f u/(u - w)^2) contour_integrable_on (circlepath z r)"
  5511            (is "?thes1")
  5512       and "(f has_field_derivative (1 / (2 * of_real pi * \<i>) * contour_integral(circlepath z r) (\<lambda>u. f u / (u - w)^2))) (at w)"
  5513            (is "?thes2")
  5514 proof -
  5515   have [simp]: "r \<ge> 0" using w
  5516     using ball_eq_empty by fastforce
  5517   have f: "continuous_on (path_image (circlepath z r)) f"
  5518     by (rule continuous_on_subset [OF contf]) (force simp add: cball_def sphere_def)
  5519   have int: "\<And>w. dist z w < r \<Longrightarrow>
  5520                  ((\<lambda>u. f u / (u - w)) has_contour_integral (\<lambda>x. 2 * of_real pi * \<i> * f x) w) (circlepath z r)"
  5521     by (rule Cauchy_integral_circlepath [OF contf holf]) (simp add: dist_norm norm_minus_commute)
  5522   show ?thes1
  5523     apply (simp add: power2_eq_square)
  5524     apply (rule Cauchy_next_derivative_circlepath [OF f _ _ w, where k=1, simplified])
  5525     apply (blast intro: int)
  5526     done
  5527   have "((\<lambda>x. 2 * of_real pi * \<i> * f x) has_field_derivative contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)^2)) (at w)"
  5528     apply (simp add: power2_eq_square)
  5529     apply (rule Cauchy_next_derivative_circlepath [OF f _ _ w, where k=1 and g = "\<lambda>x. 2 * of_real pi * \<i> * f x", simplified])
  5530     apply (blast intro: int)
  5531     done
  5532   then have fder: "(f has_field_derivative contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)^2) / (2 * of_real pi * \<i>)) (at w)"
  5533     by (rule DERIV_cdivide [where f = "\<lambda>x. 2 * of_real pi * \<i> * f x" and c = "2 * of_real pi * \<i>", simplified])
  5534   show ?thes2
  5535     by simp (rule fder)
  5536 qed
  5537 
  5538 subsection\<open> Existence of all higher derivatives.\<close>
  5539 
  5540 proposition derivative_is_holomorphic:
  5541   assumes "open s"
  5542       and fder: "\<And>z. z \<in> s \<Longrightarrow> (f has_field_derivative f' z) (at z)"
  5543     shows "f' holomorphic_on s"
  5544 proof -
  5545   have *: "\<exists>h. (f' has_field_derivative h) (at z)" if "z \<in> s" for z
  5546   proof -
  5547     obtain r where "r > 0" and r: "cball z r \<subseteq> s"
  5548       using open_contains_cball \<open>z \<in> s\<close> \<open>open s\<close> by blast
  5549     then have holf_cball: "f holomorphic_on cball z r"
  5550       apply (simp add: holomorphic_on_def)
  5551       using field_differentiable_at_within field_differentiable_def fder by blast
  5552     then have "continuous_on (path_image (circlepath z r)) f"
  5553       using \<open>r > 0\<close> by (force elim: holomorphic_on_subset [THEN holomorphic_on_imp_continuous_on])
  5554     then have contfpi: "continuous_on (path_image (circlepath z r)) (\<lambda>x. 1/(2 * of_real pi*\<i>) * f x)"
  5555       by (auto intro: continuous_intros)+
  5556     have contf_cball: "continuous_on (cball z r) f" using holf_cball
  5557       by (simp add: holomorphic_on_imp_continuous_on holomorphic_on_subset)
  5558     have holf_ball: "f holomorphic_on ball z r" using holf_cball
  5559       using ball_subset_cball holomorphic_on_subset by blast
  5560     { fix w  assume w: "w \<in> ball z r"
  5561       have intf: "(\<lambda>u. f u / (u - w)\<^sup>2) contour_integrable_on circlepath z r"
  5562         by (blast intro: w Cauchy_derivative_integral_circlepath [OF contf_cball holf_ball])
  5563       have fder': "(f has_field_derivative 1 / (2 * of_real pi * \<i>) * contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2))
  5564                   (at w)"
  5565         by (blast intro: w Cauchy_derivative_integral_circlepath [OF contf_cball holf_ball])
  5566       have f'_eq: "f' w = contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>)"
  5567         using fder' ball_subset_cball r w by (force intro: DERIV_unique [OF fder])
  5568       have "((\<lambda>u. f u / (u - w)\<^sup>2 / (2 * of_real pi * \<i>)) has_contour_integral
  5569                 contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>))
  5570                 (circlepath z r)"
  5571         by (rule has_contour_integral_div [OF has_contour_integral_integral [OF intf]])
  5572       then have "((\<lambda>u. f u / (2 * of_real pi * \<i> * (u - w)\<^sup>2)) has_contour_integral
  5573                 contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>))
  5574                 (circlepath z r)"
  5575         by (simp add: algebra_simps)
  5576       then have "((\<lambda>u. f u / (2 * of_real pi * \<i> * (u - w)\<^sup>2)) has_contour_integral f' w) (circlepath z r)"
  5577         by (simp add: f'_eq)
  5578     } note * = this
  5579     show ?thesis
  5580       apply (rule exI)
  5581       apply (rule Cauchy_next_derivative_circlepath [OF contfpi, of 2 f', simplified])
  5582       apply (simp_all add: \<open>0 < r\<close> * dist_norm)
  5583       done
  5584   qed
  5585   show ?thesis
  5586     by (simp add: holomorphic_on_open [OF \<open>open s\<close>] *)
  5587 qed
  5588 
  5589 lemma holomorphic_deriv [holomorphic_intros]:
  5590     "\<lbrakk>f holomorphic_on s; open s\<rbrakk> \<Longrightarrow> (deriv f) holomorphic_on s"
  5591 by (metis DERIV_deriv_iff_field_differentiable at_within_open derivative_is_holomorphic holomorphic_on_def)
  5592 
  5593 lemma analytic_deriv: "f analytic_on s \<Longrightarrow> (deriv f) analytic_on s"
  5594   using analytic_on_holomorphic holomorphic_deriv by auto
  5595 
  5596 lemma holomorphic_higher_deriv [holomorphic_intros]: "\<lbrakk>f holomorphic_on s; open s\<rbrakk> \<Longrightarrow> (deriv ^^ n) f holomorphic_on s"
  5597   by (induction n) (auto simp: holomorphic_deriv)
  5598 
  5599 lemma analytic_higher_deriv: "f analytic_on s \<Longrightarrow> (deriv ^^ n) f analytic_on s"
  5600   unfolding analytic_on_def using holomorphic_higher_deriv by blast
  5601 
  5602 lemma has_field_derivative_higher_deriv:
  5603      "\<lbrakk>f holomorphic_on s; open s; x \<in> s\<rbrakk>
  5604       \<Longrightarrow> ((deriv ^^ n) f has_field_derivative (deriv ^^ (Suc n)) f x) (at x)"
  5605 by (metis (no_types, hide_lams) DERIV_deriv_iff_field_differentiable at_within_open comp_apply
  5606          funpow.simps(2) holomorphic_higher_deriv holomorphic_on_def)
  5607 
  5608 lemma valid_path_compose_holomorphic:
  5609   assumes "valid_path g" and holo:"f holomorphic_on s" and "open s" "path_image g \<subseteq> s"
  5610   shows "valid_path (f o g)"
  5611 proof (rule valid_path_compose[OF \<open>valid_path g\<close>])
  5612   fix x assume "x \<in> path_image g"
  5613   then show "f field_differentiable at x"
  5614     using analytic_on_imp_differentiable_at analytic_on_open assms holo by blast
  5615 next
  5616   have "deriv f holomorphic_on s"
  5617     using holomorphic_deriv holo \<open>open s\<close> by auto
  5618   then show "continuous_on (path_image g) (deriv f)"
  5619     using assms(4) holomorphic_on_imp_continuous_on holomorphic_on_subset by auto
  5620 qed
  5621 
  5622 
  5623 subsection\<open> Morera's theorem.\<close>
  5624 
  5625 lemma Morera_local_triangle_ball:
  5626   assumes "\<And>z. z \<in> s
  5627           \<Longrightarrow> \<exists>e a. 0 < e \<and> z \<in> ball a e \<and> continuous_on (ball a e) f \<and>
  5628                     (\<forall>b c. closed_segment b c \<subseteq> ball a e
  5629                            \<longrightarrow> contour_integral (linepath a b) f +
  5630                                contour_integral (linepath b c) f +
  5631                                contour_integral (linepath c a) f = 0)"
  5632   shows "f analytic_on s"
  5633 proof -
  5634   { fix z  assume "z \<in> s"
  5635     with assms obtain e a where
  5636             "0 < e" and z: "z \<in> ball a e" and contf: "continuous_on (ball a e) f"
  5637         and 0: "\<And>b c. closed_segment b c \<subseteq> ball a e
  5638                       \<Longrightarrow> contour_integral (linepath a b) f +
  5639                           contour_integral (linepath b c) f +
  5640                           contour_integral (linepath c a) f = 0"
  5641       by fastforce
  5642     have az: "dist a z < e" using mem_ball z by blast
  5643     have sb_ball: "ball z (e - dist a z) \<subseteq> ball a e"
  5644       by (simp add: dist_commute ball_subset_ball_iff)
  5645     have "\<exists>e>0. f holomorphic_on ball z e"
  5646       apply (rule_tac x="e - dist a z" in exI)
  5647       apply (simp add: az)