src/HOL/Analysis/Cauchy_Integral_Theorem.thy
author paulson <lp15@cam.ac.uk>
Tue Apr 25 16:39:54 2017 +0100 (2017-04-25)
changeset 65578 e4997c181cce
parent 65037 2cf841ff23be
child 65587 16a8991ab398
permissions -rw-r--r--
New material from PNT proof, as well as more default [simp] declarations. Also removed duplicate theorems about geometric series
     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 by auto
  1731   have c': "c = k *\<^sub>R (b - a) + a"
  1732     by (metis diff_add_cancel c)
  1733   have bc: "(b - c) = (1 - k) *\<^sub>R (b - a)"
  1734     by (simp add: algebra_simps c')
  1735   { assume *: "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R c) * (c - a)) has_integral i) {0..1}"
  1736     have **: "\<And>x. ((k - x) / k) *\<^sub>R a + (x / k) *\<^sub>R c = (1 - x) *\<^sub>R a + x *\<^sub>R b"
  1737       using False
  1738       apply (simp add: c' algebra_simps)
  1739       apply (simp add: real_vector.scale_left_distrib [symmetric] divide_simps)
  1740       done
  1741     have "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R b) * (b - a)) has_integral i) {0..k}"
  1742       using * k
  1743       apply -
  1744       apply (drule has_integral_affinity [of _ _ 0 "1::real" "inverse k" "0", simplified])
  1745       apply (simp_all add: divide_simps mult.commute [of _ "k"] image_affinity_atLeastAtMost ** c)
  1746       apply (drule has_integral_cmul [where c = "inverse k"])
  1747       apply (simp add: has_integral_cmul)
  1748       done
  1749   } note fi = this
  1750   { assume *: "((\<lambda>x. f ((1 - x) *\<^sub>R c + x *\<^sub>R b) * (b - c)) has_integral j) {0..1}"
  1751     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)"
  1752       using k
  1753       apply (simp add: c' field_simps)
  1754       apply (simp add: scaleR_conv_of_real divide_simps)
  1755       apply (simp add: field_simps)
  1756       done
  1757     have "((\<lambda>x. f ((1 - x) *\<^sub>R a + x *\<^sub>R b) * (b - a)) has_integral j) {k..1}"
  1758       using * k
  1759       apply -
  1760       apply (drule has_integral_affinity [of _ _ 0 "1::real" "inverse(1 - k)" "-(k/(1 - k))", simplified])
  1761       apply (simp_all add: divide_simps mult.commute [of _ "1-k"] image_affinity_atLeastAtMost ** bc)
  1762       apply (drule has_integral_cmul [where k = "(1 - k) *\<^sub>R j" and c = "inverse (1 - k)"])
  1763       apply (simp add: has_integral_cmul)
  1764       done
  1765   } note fj = this
  1766   show ?thesis
  1767     using f k
  1768     apply (simp add: has_contour_integral_linepath)
  1769     apply (simp add: linepath_def)
  1770     apply (rule has_integral_combine [OF _ _ fi fj], simp_all)
  1771     done
  1772 qed
  1773 
  1774 lemma continuous_on_closed_segment_transform:
  1775   assumes f: "continuous_on (closed_segment a b) f"
  1776       and k: "0 \<le> k" "k \<le> 1"
  1777       and c: "c - a = k *\<^sub>R (b - a)"
  1778     shows "continuous_on (closed_segment a c) f"
  1779 proof -
  1780   have c': "c = (1 - k) *\<^sub>R a + k *\<^sub>R b"
  1781     using c by (simp add: algebra_simps)
  1782   show "continuous_on (closed_segment a c) f"
  1783     apply (rule continuous_on_subset [OF f])
  1784     apply (simp add: segment_convex_hull)
  1785     apply (rule convex_hull_subset)
  1786     using assms
  1787     apply (auto simp: hull_inc c' convexD_alt)
  1788     done
  1789 qed
  1790 
  1791 lemma contour_integral_split:
  1792   assumes f: "continuous_on (closed_segment a b) f"
  1793       and k: "0 \<le> k" "k \<le> 1"
  1794       and c: "c - a = k *\<^sub>R (b - a)"
  1795     shows "contour_integral(linepath a b) f = contour_integral(linepath a c) f + contour_integral(linepath c b) f"
  1796 proof -
  1797   have c': "c = (1 - k) *\<^sub>R a + k *\<^sub>R b"
  1798     using c by (simp add: algebra_simps)
  1799   have *: "continuous_on (closed_segment a c) f" "continuous_on (closed_segment c b) f"
  1800     apply (rule_tac [!] continuous_on_subset [OF f])
  1801     apply (simp_all add: segment_convex_hull)
  1802     apply (rule_tac [!] convex_hull_subset)
  1803     using assms
  1804     apply (auto simp: hull_inc c' convexD_alt)
  1805     done
  1806   show ?thesis
  1807     apply (rule contour_integral_unique)
  1808     apply (rule has_contour_integral_split [OF has_contour_integral_integral has_contour_integral_integral k c])
  1809     apply (rule contour_integrable_continuous_linepath *)+
  1810     done
  1811 qed
  1812 
  1813 lemma contour_integral_split_linepath:
  1814   assumes f: "continuous_on (closed_segment a b) f"
  1815       and c: "c \<in> closed_segment a b"
  1816     shows "contour_integral(linepath a b) f = contour_integral(linepath a c) f + contour_integral(linepath c b) f"
  1817   using c
  1818   by (auto simp: closed_segment_def algebra_simps intro!: contour_integral_split [OF f])
  1819 
  1820 (* The special case of midpoints used in the main quadrisection.*)
  1821 
  1822 lemma has_contour_integral_midpoint:
  1823   assumes "(f has_contour_integral i) (linepath a (midpoint a b))"
  1824           "(f has_contour_integral j) (linepath (midpoint a b) b)"
  1825     shows "(f has_contour_integral (i + j)) (linepath a b)"
  1826   apply (rule has_contour_integral_split [where c = "midpoint a b" and k = "1/2"])
  1827   using assms
  1828   apply (auto simp: midpoint_def algebra_simps scaleR_conv_of_real)
  1829   done
  1830 
  1831 lemma contour_integral_midpoint:
  1832    "continuous_on (closed_segment a b) f
  1833     \<Longrightarrow> contour_integral (linepath a b) f =
  1834         contour_integral (linepath a (midpoint a b)) f + contour_integral (linepath (midpoint a b) b) f"
  1835   apply (rule contour_integral_split [where c = "midpoint a b" and k = "1/2"])
  1836   apply (auto simp: midpoint_def algebra_simps scaleR_conv_of_real)
  1837   done
  1838 
  1839 
  1840 text\<open>A couple of special case lemmas that are useful below\<close>
  1841 
  1842 lemma triangle_linear_has_chain_integral:
  1843     "((\<lambda>x. m*x + d) has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  1844   apply (rule Cauchy_theorem_primitive [of UNIV "\<lambda>x. m/2 * x^2 + d*x"])
  1845   apply (auto intro!: derivative_eq_intros)
  1846   done
  1847 
  1848 lemma has_chain_integral_chain_integral3:
  1849      "(f has_contour_integral i) (linepath a b +++ linepath b c +++ linepath c d)
  1850       \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c d) f = i"
  1851   apply (subst contour_integral_unique [symmetric], assumption)
  1852   apply (drule has_contour_integral_integrable)
  1853   apply (simp add: valid_path_join)
  1854   done
  1855 
  1856 lemma has_chain_integral_chain_integral4:
  1857      "(f has_contour_integral i) (linepath a b +++ linepath b c +++ linepath c d +++ linepath d e)
  1858       \<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"
  1859   apply (subst contour_integral_unique [symmetric], assumption)
  1860   apply (drule has_contour_integral_integrable)
  1861   apply (simp add: valid_path_join)
  1862   done
  1863 
  1864 subsection\<open>Reversing the order in a double path integral\<close>
  1865 
  1866 text\<open>The condition is stronger than needed but it's often true in typical situations\<close>
  1867 
  1868 lemma fst_im_cbox [simp]: "cbox c d \<noteq> {} \<Longrightarrow> (fst ` cbox (a,c) (b,d)) = cbox a b"
  1869   by (auto simp: cbox_Pair_eq)
  1870 
  1871 lemma snd_im_cbox [simp]: "cbox a b \<noteq> {} \<Longrightarrow> (snd ` cbox (a,c) (b,d)) = cbox c d"
  1872   by (auto simp: cbox_Pair_eq)
  1873 
  1874 lemma contour_integral_swap:
  1875   assumes fcon:  "continuous_on (path_image g \<times> path_image h) (\<lambda>(y1,y2). f y1 y2)"
  1876       and vp:    "valid_path g" "valid_path h"
  1877       and gvcon: "continuous_on {0..1} (\<lambda>t. vector_derivative g (at t))"
  1878       and hvcon: "continuous_on {0..1} (\<lambda>t. vector_derivative h (at t))"
  1879   shows "contour_integral g (\<lambda>w. contour_integral h (f w)) =
  1880          contour_integral h (\<lambda>z. contour_integral g (\<lambda>w. f w z))"
  1881 proof -
  1882   have gcon: "continuous_on {0..1} g" and hcon: "continuous_on {0..1} h"
  1883     using assms by (auto simp: valid_path_def piecewise_C1_differentiable_on_def)
  1884   have fgh1: "\<And>x. (\<lambda>t. f (g x) (h t)) = (\<lambda>(y1,y2). f y1 y2) o (\<lambda>t. (g x, h t))"
  1885     by (rule ext) simp
  1886   have fgh2: "\<And>x. (\<lambda>t. f (g t) (h x)) = (\<lambda>(y1,y2). f y1 y2) o (\<lambda>t. (g t, h x))"
  1887     by (rule ext) simp
  1888   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)"
  1889     by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
  1890   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)"
  1891     by (rule continuous_on_subset [OF fcon]) (auto simp: path_image_def)
  1892   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}"
  1893     apply (rule integrable_continuous_real)
  1894     apply (rule continuous_on_mult [OF _ gvcon])
  1895     apply (subst fgh2)
  1896     apply (rule fcon_im2 gcon continuous_intros | simp)+
  1897     done
  1898   have "(\<lambda>z. vector_derivative g (at (fst z))) = (\<lambda>x. vector_derivative g (at x)) o fst"
  1899     by auto
  1900   then have gvcon': "continuous_on (cbox (0, 0) (1, 1::real)) (\<lambda>x. vector_derivative g (at (fst x)))"
  1901     apply (rule ssubst)
  1902     apply (rule continuous_intros | simp add: gvcon)+
  1903     done
  1904   have "(\<lambda>z. vector_derivative h (at (snd z))) = (\<lambda>x. vector_derivative h (at x)) o snd"
  1905     by auto
  1906   then have hvcon': "continuous_on (cbox (0, 0) (1::real, 1)) (\<lambda>x. vector_derivative h (at (snd x)))"
  1907     apply (rule ssubst)
  1908     apply (rule continuous_intros | simp add: hvcon)+
  1909     done
  1910   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))"
  1911     by auto
  1912   then have fgh: "continuous_on (cbox (0, 0) (1, 1)) (\<lambda>x. f (g (fst x)) (h (snd x)))"
  1913     apply (rule ssubst)
  1914     apply (rule gcon hcon continuous_intros | simp)+
  1915     apply (auto simp: path_image_def intro: continuous_on_subset [OF fcon])
  1916     done
  1917   have "integral {0..1} (\<lambda>x. contour_integral h (f (g x)) * vector_derivative g (at x)) =
  1918         integral {0..1} (\<lambda>x. contour_integral h (\<lambda>y. f (g x) y * vector_derivative g (at x)))"
  1919     apply (rule integral_cong [OF contour_integral_rmul [symmetric]])
  1920     apply (clarsimp simp: contour_integrable_on)
  1921     apply (rule integrable_continuous_real)
  1922     apply (rule continuous_on_mult [OF _ hvcon])
  1923     apply (subst fgh1)
  1924     apply (rule fcon_im1 hcon continuous_intros | simp)+
  1925     done
  1926   also have "... = integral {0..1}
  1927                      (\<lambda>y. contour_integral g (\<lambda>x. f x (h y) * vector_derivative h (at y)))"
  1928     apply (simp only: contour_integral_integral)
  1929     apply (subst integral_swap_continuous [where 'a = real and 'b = real, of 0 0 1 1, simplified])
  1930      apply (rule fgh gvcon' hvcon' continuous_intros | simp add: split_def)+
  1931     unfolding integral_mult_left [symmetric]
  1932     apply (simp only: mult_ac)
  1933     done
  1934   also have "... = contour_integral h (\<lambda>z. contour_integral g (\<lambda>w. f w z))"
  1935     apply (simp add: contour_integral_integral)
  1936     apply (rule integral_cong)
  1937     unfolding integral_mult_left [symmetric]
  1938     apply (simp add: algebra_simps)
  1939     done
  1940   finally show ?thesis
  1941     by (simp add: contour_integral_integral)
  1942 qed
  1943 
  1944 
  1945 subsection\<open>The key quadrisection step\<close>
  1946 
  1947 lemma norm_sum_half:
  1948   assumes "norm(a + b) >= e"
  1949     shows "norm a >= e/2 \<or> norm b >= e/2"
  1950 proof -
  1951   have "e \<le> norm (- a - b)"
  1952     by (simp add: add.commute assms norm_minus_commute)
  1953   thus ?thesis
  1954     using norm_triangle_ineq4 order_trans by fastforce
  1955 qed
  1956 
  1957 lemma norm_sum_lemma:
  1958   assumes "e \<le> norm (a + b + c + d)"
  1959     shows "e / 4 \<le> norm a \<or> e / 4 \<le> norm b \<or> e / 4 \<le> norm c \<or> e / 4 \<le> norm d"
  1960 proof -
  1961   have "e \<le> norm ((a + b) + (c + d))" using assms
  1962     by (simp add: algebra_simps)
  1963   then show ?thesis
  1964     by (auto dest!: norm_sum_half)
  1965 qed
  1966 
  1967 lemma Cauchy_theorem_quadrisection:
  1968   assumes f: "continuous_on (convex hull {a,b,c}) f"
  1969       and dist: "dist a b \<le> K" "dist b c \<le> K" "dist c a \<le> K"
  1970       and e: "e * K^2 \<le>
  1971               norm (contour_integral(linepath a b) f + contour_integral(linepath b c) f + contour_integral(linepath c a) f)"
  1972   shows "\<exists>a' b' c'.
  1973            a' \<in> convex hull {a,b,c} \<and> b' \<in> convex hull {a,b,c} \<and> c' \<in> convex hull {a,b,c} \<and>
  1974            dist a' b' \<le> K/2  \<and>  dist b' c' \<le> K/2  \<and>  dist c' a' \<le> K/2  \<and>
  1975            e * (K/2)^2 \<le> norm(contour_integral(linepath a' b') f + contour_integral(linepath b' c') f + contour_integral(linepath c' a') f)"
  1976 proof -
  1977   note divide_le_eq_numeral1 [simp del]
  1978   define a' where "a' = midpoint b c"
  1979   define b' where "b' = midpoint c a"
  1980   define c' where "c' = midpoint a b"
  1981   have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
  1982     using f continuous_on_subset segments_subset_convex_hull by metis+
  1983   have fcont': "continuous_on (closed_segment c' b') f"
  1984                "continuous_on (closed_segment a' c') f"
  1985                "continuous_on (closed_segment b' a') f"
  1986     unfolding a'_def b'_def c'_def
  1987     apply (rule continuous_on_subset [OF f],
  1988            metis midpoints_in_convex_hull convex_hull_subset hull_subset insert_subset segment_convex_hull)+
  1989     done
  1990   let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  1991   have *: "?pathint a b + ?pathint b c + ?pathint c a =
  1992           (?pathint a c' + ?pathint c' b' + ?pathint b' a) +
  1993           (?pathint a' c' + ?pathint c' b + ?pathint b a') +
  1994           (?pathint a' c + ?pathint c b' + ?pathint b' a') +
  1995           (?pathint a' b' + ?pathint b' c' + ?pathint c' a')"
  1996     apply (simp add: fcont' contour_integral_reverse_linepath)
  1997     apply (simp add: a'_def b'_def c'_def contour_integral_midpoint fabc)
  1998     done
  1999   have [simp]: "\<And>x y. cmod (x * 2 - y * 2) = cmod (x - y) * 2"
  2000     by (metis left_diff_distrib mult.commute norm_mult_numeral1)
  2001   have [simp]: "\<And>x y. cmod (x - y) = cmod (y - x)"
  2002     by (simp add: norm_minus_commute)
  2003   consider "e * K\<^sup>2 / 4 \<le> cmod (?pathint a c' + ?pathint c' b' + ?pathint b' a)" |
  2004            "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' b' + ?pathint b' c' + ?pathint c' a')"
  2007     using assms
  2008     apply (simp only: *)
  2009     apply (blast intro: that dest!: norm_sum_lemma)
  2010     done
  2011   then show ?thesis
  2012   proof cases
  2013     case 1 then show ?thesis
  2014       apply (rule_tac x=a in exI)
  2015       apply (rule exI [where x=c'])
  2016       apply (rule exI [where x=b'])
  2017       using assms
  2018       apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
  2019       apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
  2020       done
  2021   next
  2022     case 2 then show ?thesis
  2023       apply (rule_tac x=a' in exI)
  2024       apply (rule exI [where x=c'])
  2025       apply (rule exI [where x=b])
  2026       using assms
  2027       apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
  2028       apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
  2029       done
  2030   next
  2031     case 3 then show ?thesis
  2032       apply (rule_tac x=a' in exI)
  2033       apply (rule exI [where x=c])
  2034       apply (rule exI [where x=b'])
  2035       using assms
  2036       apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
  2037       apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
  2038       done
  2039   next
  2040     case 4 then show ?thesis
  2041       apply (rule_tac x=a' in exI)
  2042       apply (rule exI [where x=b'])
  2043       apply (rule exI [where x=c'])
  2044       using assms
  2045       apply (auto simp: a'_def c'_def b'_def midpoints_in_convex_hull hull_subset [THEN subsetD])
  2046       apply (auto simp: midpoint_def dist_norm scaleR_conv_of_real divide_simps)
  2047       done
  2048   qed
  2049 qed
  2050 
  2051 subsection\<open>Cauchy's theorem for triangles\<close>
  2052 
  2053 lemma triangle_points_closer:
  2054   fixes a::complex
  2055   shows "\<lbrakk>x \<in> convex hull {a,b,c};  y \<in> convex hull {a,b,c}\<rbrakk>
  2056          \<Longrightarrow> norm(x - y) \<le> norm(a - b) \<or>
  2057              norm(x - y) \<le> norm(b - c) \<or>
  2058              norm(x - y) \<le> norm(c - a)"
  2059   using simplex_extremal_le [of "{a,b,c}"]
  2060   by (auto simp: norm_minus_commute)
  2061 
  2062 lemma holomorphic_point_small_triangle:
  2063   assumes x: "x \<in> s"
  2064       and f: "continuous_on s f"
  2065       and cd: "f field_differentiable (at x within s)"
  2066       and e: "0 < e"
  2067     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>
  2068               x \<in> convex hull {a,b,c} \<and> convex hull {a,b,c} \<subseteq> s
  2069               \<longrightarrow> norm(contour_integral(linepath a b) f + contour_integral(linepath b c) f +
  2070                        contour_integral(linepath c a) f)
  2071                   \<le> e*(dist a b + dist b c + dist c a)^2"
  2072            (is "\<exists>k>0. \<forall>a b c. _ \<longrightarrow> ?normle a b c")
  2073 proof -
  2074   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>
  2075                      \<Longrightarrow> a \<le> e*(x + y + z)^2"
  2076     by (simp add: algebra_simps power2_eq_square)
  2077   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"
  2078              for x::real and a b c
  2079     by linarith
  2080   have fabc: "f contour_integrable_on linepath a b" "f contour_integrable_on linepath b c" "f contour_integrable_on linepath c a"
  2081               if "convex hull {a, b, c} \<subseteq> s" for a b c
  2082     using segments_subset_convex_hull that
  2083     by (metis continuous_on_subset f contour_integrable_continuous_linepath)+
  2084   note path_bound = has_contour_integral_bound_linepath [simplified norm_minus_commute, OF has_contour_integral_integral]
  2085   { fix f' a b c d
  2086     assume d: "0 < d"
  2087        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)"
  2088        and le: "cmod (a - b) \<le> d" "cmod (b - c) \<le> d" "cmod (c - a) \<le> d"
  2089        and xc: "x \<in> convex hull {a, b, c}"
  2090        and s: "convex hull {a, b, c} \<subseteq> s"
  2091     have pa: "contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f =
  2092               contour_integral (linepath a b) (\<lambda>y. f y - f x - f'*(y - x)) +
  2093               contour_integral (linepath b c) (\<lambda>y. f y - f x - f'*(y - x)) +
  2094               contour_integral (linepath c a) (\<lambda>y. f y - f x - f'*(y - x))"
  2095       apply (simp add: contour_integral_diff contour_integral_lmul contour_integrable_lmul contour_integrable_diff fabc [OF s])
  2096       apply (simp add: field_simps)
  2097       done
  2098     { fix y
  2099       assume yc: "y \<in> convex hull {a,b,c}"
  2100       have "cmod (f y - f x - f' * (y - x)) \<le> e*norm(y - x)"
  2101         apply (rule f')
  2102         apply (metis triangle_points_closer [OF xc yc] le norm_minus_commute order_trans)
  2103         using s yc by blast
  2104       also have "... \<le> e * (cmod (a - b) + cmod (b - c) + cmod (c - a))"
  2105         by (simp add: yc e xc disj_le [OF triangle_points_closer])
  2106       finally have "cmod (f y - f x - f' * (y - x)) \<le> e * (cmod (a - b) + cmod (b - c) + cmod (c - a))" .
  2107     } note cm_le = this
  2108     have "?normle a b c"
  2109       apply (simp add: dist_norm pa)
  2110       apply (rule le_of_3)
  2111       using f' xc s e
  2112       apply simp_all
  2113       apply (intro norm_triangle_le add_mono path_bound)
  2114       apply (simp_all add: contour_integral_diff contour_integral_lmul contour_integrable_lmul contour_integrable_diff fabc)
  2115       apply (blast intro: cm_le elim: dest: segments_subset_convex_hull [THEN subsetD])+
  2116       done
  2117   } note * = this
  2118   show ?thesis
  2119     using cd e
  2120     apply (simp add: field_differentiable_def has_field_derivative_def has_derivative_within_alt approachable_lt_le2 Ball_def)
  2121     apply (clarify dest!: spec mp)
  2122     using *
  2123     apply (simp add: dist_norm, blast)
  2124     done
  2125 qed
  2126 
  2127 
  2128 (* Hence the most basic theorem for a triangle.*)
  2129 locale Chain =
  2130   fixes x0 At Follows
  2131   assumes At0: "At x0 0"
  2132       and AtSuc: "\<And>x n. At x n \<Longrightarrow> \<exists>x'. At x' (Suc n) \<and> Follows x' x"
  2133 begin
  2134   primrec f where
  2135     "f 0 = x0"
  2136   | "f (Suc n) = (SOME x. At x (Suc n) \<and> Follows x (f n))"
  2137 
  2138   lemma At: "At (f n) n"
  2139   proof (induct n)
  2140     case 0 show ?case
  2141       by (simp add: At0)
  2142   next
  2143     case (Suc n) show ?case
  2144       by (metis (no_types, lifting) AtSuc [OF Suc] f.simps(2) someI_ex)
  2145   qed
  2146 
  2147   lemma Follows: "Follows (f(Suc n)) (f n)"
  2148     by (metis (no_types, lifting) AtSuc [OF At [of n]] f.simps(2) someI_ex)
  2149 
  2150   declare f.simps(2) [simp del]
  2151 end
  2152 
  2153 lemma Chain3:
  2154   assumes At0: "At x0 y0 z0 0"
  2155       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"
  2156   obtains f g h where
  2157     "f 0 = x0" "g 0 = y0" "h 0 = z0"
  2158                       "\<And>n. At (f n) (g n) (h n) n"
  2159                        "\<And>n. Follows (f(Suc n)) (g(Suc n)) (h(Suc n)) (f n) (g n) (h n)"
  2160 proof -
  2161   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"
  2162     apply unfold_locales
  2163     using At0 AtSuc by auto
  2164   show ?thesis
  2165   apply (rule that [of "\<lambda>n. fst (three.f n)"  "\<lambda>n. fst (snd (three.f n))" "\<lambda>n. snd (snd (three.f n))"])
  2166   apply simp_all
  2167   using three.At three.Follows
  2168   apply (simp_all add: split_beta')
  2169   done
  2170 qed
  2171 
  2172 lemma Cauchy_theorem_triangle:
  2173   assumes "f holomorphic_on (convex hull {a,b,c})"
  2174     shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2175 proof -
  2176   have contf: "continuous_on (convex hull {a,b,c}) f"
  2177     by (metis assms holomorphic_on_imp_continuous_on)
  2178   let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2179   { fix y::complex
  2180     assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
  2181        and ynz: "y \<noteq> 0"
  2182     define K where "K = 1 + max (dist a b) (max (dist b c) (dist c a))"
  2183     define e where "e = norm y / K^2"
  2184     have K1: "K \<ge> 1"  by (simp add: K_def max.coboundedI1)
  2185     then have K: "K > 0" by linarith
  2186     have [iff]: "dist a b \<le> K" "dist b c \<le> K" "dist c a \<le> K"
  2187       by (simp_all add: K_def)
  2188     have e: "e > 0"
  2189       unfolding e_def using ynz K1 by simp
  2190     define At where "At x y z n \<longleftrightarrow>
  2191         convex hull {x,y,z} \<subseteq> convex hull {a,b,c} \<and>
  2192         dist x y \<le> K/2^n \<and> dist y z \<le> K/2^n \<and> dist z x \<le> K/2^n \<and>
  2193         norm(?pathint x y + ?pathint y z + ?pathint z x) \<ge> e*(K/2^n)^2"
  2194       for x y z n
  2195     have At0: "At a b c 0"
  2196       using fy
  2197       by (simp add: At_def e_def has_chain_integral_chain_integral3)
  2198     { fix x y z n
  2199       assume At: "At x y z n"
  2200       then have contf': "continuous_on (convex hull {x,y,z}) f"
  2201         using contf At_def continuous_on_subset by metis
  2202       have "\<exists>x' y' z'. At x' y' z' (Suc n) \<and> convex hull {x',y',z'} \<subseteq> convex hull {x,y,z}"
  2203         using At
  2204         apply (simp add: At_def)
  2205         using  Cauchy_theorem_quadrisection [OF contf', of "K/2^n" e]
  2206         apply clarsimp
  2207         apply (rule_tac x="a'" in exI)
  2208         apply (rule_tac x="b'" in exI)
  2209         apply (rule_tac x="c'" in exI)
  2210         apply (simp add: algebra_simps)
  2211         apply (meson convex_hull_subset empty_subsetI insert_subset subsetCE)
  2212         done
  2213     } note AtSuc = this
  2214     obtain fa fb fc
  2215       where f0 [simp]: "fa 0 = a" "fb 0 = b" "fc 0 = c"
  2216         and cosb: "\<And>n. convex hull {fa n, fb n, fc n} \<subseteq> convex hull {a,b,c}"
  2217         and dist: "\<And>n. dist (fa n) (fb n) \<le> K/2^n"
  2218                   "\<And>n. dist (fb n) (fc n) \<le> K/2^n"
  2219                   "\<And>n. dist (fc n) (fa n) \<le> K/2^n"
  2220         and no: "\<And>n. norm(?pathint (fa n) (fb n) +
  2221                            ?pathint (fb n) (fc n) +
  2222                            ?pathint (fc n) (fa n)) \<ge> e * (K/2^n)^2"
  2223         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}"
  2224       apply (rule Chain3 [of At, OF At0 AtSuc])
  2225       apply (auto simp: At_def)
  2226       done
  2227     have "\<exists>x. \<forall>n. x \<in> convex hull {fa n, fb n, fc n}"
  2228       apply (rule bounded_closed_nest)
  2229       apply (simp_all add: compact_imp_closed finite_imp_compact_convex_hull finite_imp_bounded_convex_hull)
  2230       apply (rule allI)
  2231       apply (rule transitive_stepwise_le)
  2232       apply (auto simp: conv_le)
  2233       done
  2234     then obtain x where x: "\<And>n. x \<in> convex hull {fa n, fb n, fc n}" by auto
  2235     then have xin: "x \<in> convex hull {a,b,c}"
  2236       using assms f0 by blast
  2237     then have fx: "f field_differentiable at x within (convex hull {a,b,c})"
  2238       using assms holomorphic_on_def by blast
  2239     { fix k n
  2240       assume k: "0 < k"
  2241          and le:
  2242             "\<And>x' y' z'.
  2243                \<lbrakk>dist x' y' \<le> k; dist y' z' \<le> k; dist z' x' \<le> k;
  2244                 x \<in> convex hull {x',y',z'};
  2245                 convex hull {x',y',z'} \<subseteq> convex hull {a,b,c}\<rbrakk>
  2246                \<Longrightarrow>
  2247                cmod (?pathint x' y' + ?pathint y' z' + ?pathint z' x') * 10
  2248                      \<le> e * (dist x' y' + dist y' z' + dist z' x')\<^sup>2"
  2249          and Kk: "K / k < 2 ^ n"
  2250       have "K / 2 ^ n < k" using Kk k
  2251         by (auto simp: field_simps)
  2252       then have DD: "dist (fa n) (fb n) \<le> k" "dist (fb n) (fc n) \<le> k" "dist (fc n) (fa n) \<le> k"
  2253         using dist [of n]  k
  2254         by linarith+
  2255       have dle: "(dist (fa n) (fb n) + dist (fb n) (fc n) + dist (fc n) (fa n))\<^sup>2
  2256                \<le> (3 * K / 2 ^ n)\<^sup>2"
  2257         using dist [of n] e K
  2258         by (simp add: abs_le_square_iff [symmetric])
  2259       have less10: "\<And>x y::real. 0 < x \<Longrightarrow> y \<le> 9*x \<Longrightarrow> y < x*10"
  2260         by linarith
  2261       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"
  2262         using ynz dle e mult_le_cancel_left_pos by blast
  2263       also have "... <
  2264           cmod (?pathint (fa n) (fb n) + ?pathint (fb n) (fc n) + ?pathint (fc n) (fa n)) * 10"
  2265         using no [of n] e K
  2266         apply (simp add: e_def field_simps)
  2267         apply (simp only: zero_less_norm_iff [symmetric])
  2268         done
  2269       finally have False
  2270         using le [OF DD x cosb] by auto
  2271     } then
  2272     have ?thesis
  2273       using holomorphic_point_small_triangle [OF xin contf fx, of "e/10"] e
  2274       apply clarsimp
  2275       apply (rule_tac y1="K/k" in exE [OF real_arch_pow[of 2]])
  2276       apply force+
  2277       done
  2278   }
  2279   moreover have "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
  2280     by simp (meson contf continuous_on_subset contour_integrable_continuous_linepath segments_subset_convex_hull(1)
  2281                    segments_subset_convex_hull(3) segments_subset_convex_hull(5))
  2282   ultimately show ?thesis
  2283     using has_contour_integral_integral by fastforce
  2284 qed
  2285 
  2286 
  2287 subsection\<open>Version needing function holomorphic in interior only\<close>
  2288 
  2289 lemma Cauchy_theorem_flat_lemma:
  2290   assumes f: "continuous_on (convex hull {a,b,c}) f"
  2291       and c: "c - a = k *\<^sub>R (b - a)"
  2292       and k: "0 \<le> k"
  2293     shows "contour_integral (linepath a b) f + contour_integral (linepath b c) f +
  2294           contour_integral (linepath c a) f = 0"
  2295 proof -
  2296   have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
  2297     using f continuous_on_subset segments_subset_convex_hull by metis+
  2298   show ?thesis
  2299   proof (cases "k \<le> 1")
  2300     case True show ?thesis
  2301       by (simp add: contour_integral_split [OF fabc(1) k True c] contour_integral_reverse_linepath fabc)
  2302   next
  2303     case False then show ?thesis
  2304       using fabc c
  2305       apply (subst contour_integral_split [of a c f "1/k" b, symmetric])
  2306       apply (metis closed_segment_commute fabc(3))
  2307       apply (auto simp: k contour_integral_reverse_linepath)
  2308       done
  2309   qed
  2310 qed
  2311 
  2312 lemma Cauchy_theorem_flat:
  2313   assumes f: "continuous_on (convex hull {a,b,c}) f"
  2314       and c: "c - a = k *\<^sub>R (b - a)"
  2315     shows "contour_integral (linepath a b) f +
  2316            contour_integral (linepath b c) f +
  2317            contour_integral (linepath c a) f = 0"
  2318 proof (cases "0 \<le> k")
  2319   case True with assms show ?thesis
  2320     by (blast intro: Cauchy_theorem_flat_lemma)
  2321 next
  2322   case False
  2323   have "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
  2324     using f continuous_on_subset segments_subset_convex_hull by metis+
  2325   moreover have "contour_integral (linepath b a) f + contour_integral (linepath a c) f +
  2326         contour_integral (linepath c b) f = 0"
  2327     apply (rule Cauchy_theorem_flat_lemma [of b a c f "1-k"])
  2328     using False c
  2329     apply (auto simp: f insert_commute scaleR_conv_of_real algebra_simps)
  2330     done
  2331   ultimately show ?thesis
  2332     apply (auto simp: contour_integral_reverse_linepath)
  2333     using add_eq_0_iff by force
  2334 qed
  2335 
  2336 
  2337 lemma Cauchy_theorem_triangle_interior:
  2338   assumes contf: "continuous_on (convex hull {a,b,c}) f"
  2339       and holf:  "f holomorphic_on interior (convex hull {a,b,c})"
  2340      shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2341 proof -
  2342   have fabc: "continuous_on (closed_segment a b) f" "continuous_on (closed_segment b c) f" "continuous_on (closed_segment c a) f"
  2343     using contf continuous_on_subset segments_subset_convex_hull by metis+
  2344   have "bounded (f ` (convex hull {a,b,c}))"
  2345     by (simp add: compact_continuous_image compact_convex_hull compact_imp_bounded contf)
  2346   then obtain B where "0 < B" and Bnf: "\<And>x. x \<in> convex hull {a,b,c} \<Longrightarrow> norm (f x) \<le> B"
  2347      by (auto simp: dest!: bounded_pos [THEN iffD1])
  2348   have "bounded (convex hull {a,b,c})"
  2349     by (simp add: bounded_convex_hull)
  2350   then obtain C where C: "0 < C" and Cno: "\<And>y. y \<in> convex hull {a,b,c} \<Longrightarrow> norm y < C"
  2351     using bounded_pos_less by blast
  2352   then have diff_2C: "norm(x - y) \<le> 2*C"
  2353            if x: "x \<in> convex hull {a, b, c}" and y: "y \<in> convex hull {a, b, c}" for x y
  2354   proof -
  2355     have "cmod x \<le> C"
  2356       using x by (meson Cno not_le not_less_iff_gr_or_eq)
  2357     hence "cmod (x - y) \<le> C + C"
  2358       using y by (meson Cno add_mono_thms_linordered_field(4) less_eq_real_def norm_triangle_ineq4 order_trans)
  2359     thus "cmod (x - y) \<le> 2 * C"
  2360       by (metis mult_2)
  2361   qed
  2362   have contf': "continuous_on (convex hull {b,a,c}) f"
  2363     using contf by (simp add: insert_commute)
  2364   { fix y::complex
  2365     assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
  2366        and ynz: "y \<noteq> 0"
  2367     have pi_eq_y: "contour_integral (linepath a b) f + contour_integral (linepath b c) f + contour_integral (linepath c a) f = y"
  2368       by (rule has_chain_integral_chain_integral3 [OF fy])
  2369     have ?thesis
  2370     proof (cases "c=a \<or> a=b \<or> b=c")
  2371       case True then show ?thesis
  2372         using Cauchy_theorem_flat [OF contf, of 0]
  2373         using has_chain_integral_chain_integral3 [OF fy] ynz
  2374         by (force simp: fabc contour_integral_reverse_linepath)
  2375     next
  2376       case False
  2377       then have car3: "card {a, b, c} = Suc (DIM(complex))"
  2378         by auto
  2379       { assume "interior(convex hull {a,b,c}) = {}"
  2380         then have "collinear{a,b,c}"
  2381           using interior_convex_hull_eq_empty [OF car3]
  2382           by (simp add: collinear_3_eq_affine_dependent)
  2383         then have "False"
  2384           using False
  2385           apply (clarsimp simp add: collinear_3 collinear_lemma)
  2386           apply (drule Cauchy_theorem_flat [OF contf'])
  2387           using pi_eq_y ynz
  2388           apply (simp add: fabc add_eq_0_iff contour_integral_reverse_linepath)
  2389           done
  2390       }
  2391       then obtain d where d: "d \<in> interior (convex hull {a, b, c})"
  2392         by blast
  2393       { fix d1
  2394         assume d1_pos: "0 < d1"
  2395            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>
  2396                            \<Longrightarrow> cmod (f x' - f x) < cmod y / (24 * C)"
  2397         define e where "e = min 1 (min (d1/(4*C)) ((norm y / 24 / C) / B))"
  2398         define shrink where "shrink x = x - e *\<^sub>R (x - d)" for x
  2399         let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2400         have e: "0 < e" "e \<le> 1" "e \<le> d1 / (4 * C)" "e \<le> cmod y / 24 / C / B"
  2401           using d1_pos \<open>C>0\<close> \<open>B>0\<close> ynz by (simp_all add: e_def)
  2402         then have eCB: "24 * e * C * B \<le> cmod y"
  2403           using \<open>C>0\<close> \<open>B>0\<close>  by (simp add: field_simps)
  2404         have e_le_d1: "e * (4 * C) \<le> d1"
  2405           using e \<open>C>0\<close> by (simp add: field_simps)
  2406         have "shrink a \<in> interior(convex hull {a,b,c})"
  2407              "shrink b \<in> interior(convex hull {a,b,c})"
  2408              "shrink c \<in> interior(convex hull {a,b,c})"
  2409           using d e by (auto simp: hull_inc mem_interior_convex_shrink shrink_def)
  2410         then have fhp0: "(f has_contour_integral 0)
  2411                 (linepath (shrink a) (shrink b) +++ linepath (shrink b) (shrink c) +++ linepath (shrink c) (shrink a))"
  2412           by (simp add: Cauchy_theorem_triangle holomorphic_on_subset [OF holf] hull_minimal convex_interior)
  2413         then have f_0_shrink: "?pathint (shrink a) (shrink b) + ?pathint (shrink b) (shrink c) + ?pathint (shrink c) (shrink a) = 0"
  2414           by (simp add: has_chain_integral_chain_integral3)
  2415         have fpi_abc: "f contour_integrable_on linepath (shrink a) (shrink b)"
  2416                       "f contour_integrable_on linepath (shrink b) (shrink c)"
  2417                       "f contour_integrable_on linepath (shrink c) (shrink a)"
  2418           using fhp0  by (auto simp: valid_path_join dest: has_contour_integral_integrable)
  2419         have cmod_shr: "\<And>x y. cmod (shrink y - shrink x - (y - x)) = e * cmod (x - y)"
  2420           using e by (simp add: shrink_def real_vector.scale_right_diff_distrib [symmetric])
  2421         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)"
  2422           by (simp add: algebra_simps)
  2423         have "cmod y / (24 * C) \<le> cmod y / cmod (b - a) / 12"
  2424           using False \<open>C>0\<close> diff_2C [of b a] ynz
  2425           by (auto simp: divide_simps hull_inc)
  2426         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
  2427           apply (cases "x=0", simp add: \<open>0<C\<close>)
  2428           using Cno [of u] mult_left_le_one_le [of "cmod u" x] le_less_trans norm_ge_zero by blast
  2429         { fix u v
  2430           assume uv: "u \<in> convex hull {a, b, c}" "v \<in> convex hull {a, b, c}" "u\<noteq>v"
  2431              and fpi_uv: "f contour_integrable_on linepath (shrink u) (shrink v)"
  2432           have shr_uv: "shrink u \<in> interior(convex hull {a,b,c})"
  2433                        "shrink v \<in> interior(convex hull {a,b,c})"
  2434             using d e uv
  2435             by (auto simp: hull_inc mem_interior_convex_shrink shrink_def)
  2436           have cmod_fuv: "\<And>x. 0\<le>x \<Longrightarrow> x\<le>1 \<Longrightarrow> cmod (f (linepath (shrink u) (shrink v) x)) \<le> B"
  2437             using shr_uv by (blast intro: Bnf linepath_in_convex_hull interior_subset [THEN subsetD])
  2438           have By_uv: "B * (12 * (e * cmod (u - v))) \<le> cmod y"
  2439             apply (rule order_trans [OF _ eCB])
  2440             using e \<open>B>0\<close> diff_2C [of u v] uv
  2441             by (auto simp: field_simps)
  2442           { fix x::real   assume x: "0\<le>x" "x\<le>1"
  2443             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)"
  2444               apply (rule add_strict_mono; rule norm_triangle_half_l [of _ 0])
  2445               using uv x d interior_subset
  2446               apply (auto simp: hull_inc intro!: less_C)
  2447               done
  2448             have ll: "linepath (shrink u) (shrink v) x - linepath u v x = -e * ((1 - x) *\<^sub>R (u - d) + x *\<^sub>R (v - d))"
  2449               by (simp add: linepath_def shrink_def algebra_simps scaleR_conv_of_real)
  2450             have cmod_less_dt: "cmod (linepath (shrink u) (shrink v) x - linepath u v x) < d1"
  2451               using \<open>e>0\<close>
  2452               apply (simp add: ll norm_mult scaleR_diff_right)
  2453               apply (rule less_le_trans [OF _ e_le_d1])
  2454               using cmod_less_4C
  2455               apply (force intro: norm_triangle_lt)
  2456               done
  2457             have "cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) < cmod y / (24 * C)"
  2458               using x uv shr_uv cmod_less_dt
  2459               by (auto simp: hull_inc intro: d1 interior_subset [THEN subsetD] linepath_in_convex_hull)
  2460             also have "... \<le> cmod y / cmod (v - u) / 12"
  2461               using False uv \<open>C>0\<close> diff_2C [of v u] ynz
  2462               by (auto simp: divide_simps hull_inc)
  2463             finally have "cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x)) \<le> cmod y / cmod (v - u) / 12"
  2464               by simp
  2465             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"
  2466               using uv False by (auto simp: field_simps)
  2467             have "cmod (f (linepath (shrink u) (shrink v) x)) * cmod (shrink v - shrink u - (v - u)) +
  2468                   cmod (v - u) * cmod (f (linepath (shrink u) (shrink v) x) - f (linepath u v x))
  2469                   \<le> cmod y / 6"
  2470               apply (rule order_trans [of _ "B*((norm y / 24 / C / B)*2*C) + (2*C)*(norm y /24 / C)"])
  2471               apply (rule add_mono [OF mult_mono])
  2472               using By_uv e \<open>0 < B\<close> \<open>0 < C\<close> x ynz
  2473               apply (simp_all add: cmod_fuv cmod_shr cmod_12_le hull_inc)
  2474               apply (simp add: field_simps)
  2475               done
  2476           } note cmod_diff_le = this
  2477           have f_uv: "continuous_on (closed_segment u v) f"
  2478             by (blast intro: uv continuous_on_subset [OF contf closed_segment_subset_convex_hull])
  2479           have **: "\<And>f' x' f x::complex. f'*x' - f*x = f'*(x' - x) + x*(f' - f)"
  2480             by (simp add: algebra_simps)
  2481           have "norm (?pathint (shrink u) (shrink v) - ?pathint u v) \<le> norm y / 6"
  2482             apply (rule order_trans)
  2483             apply (rule has_integral_bound
  2484                     [of "B*(norm y /24/C/B)*2*C + (2*C)*(norm y/24/C)"
  2485                         "\<lambda>x. f(linepath (shrink u) (shrink v) x) * (shrink v - shrink u) - f(linepath u v x)*(v - u)"
  2486                         _ 0 1 ])
  2487             using ynz \<open>0 < B\<close> \<open>0 < C\<close>
  2488             apply (simp_all del: le_divide_eq_numeral1)
  2489             apply (simp add: has_integral_sub has_contour_integral_linepath [symmetric] has_contour_integral_integral
  2490                              fpi_uv f_uv contour_integrable_continuous_linepath, clarify)
  2491             apply (simp only: **)
  2492             apply (simp add: norm_triangle_le norm_mult cmod_diff_le del: le_divide_eq_numeral1)
  2493             done
  2494           } note * = this
  2495           have "norm (?pathint (shrink a) (shrink b) - ?pathint a b) \<le> norm y / 6"
  2496             using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
  2497           moreover
  2498           have "norm (?pathint (shrink b) (shrink c) - ?pathint b c) \<le> norm y / 6"
  2499             using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
  2500           moreover
  2501           have "norm (?pathint (shrink c) (shrink a) - ?pathint c a) \<le> norm y / 6"
  2502             using False fpi_abc by (rule_tac *) (auto simp: hull_inc)
  2503           ultimately
  2504           have "norm((?pathint (shrink a) (shrink b) - ?pathint a b) +
  2505                      (?pathint (shrink b) (shrink c) - ?pathint b c) + (?pathint (shrink c) (shrink a) - ?pathint c a))
  2506                 \<le> norm y / 6 + norm y / 6 + norm y / 6"
  2507             by (metis norm_triangle_le add_mono)
  2508           also have "... = norm y / 2"
  2509             by simp
  2510           finally have "norm((?pathint (shrink a) (shrink b) + ?pathint (shrink b) (shrink c) + ?pathint (shrink c) (shrink a)) -
  2511                           (?pathint a b + ?pathint b c + ?pathint c a))
  2512                 \<le> norm y / 2"
  2513             by (simp add: algebra_simps)
  2514           then
  2515           have "norm(?pathint a b + ?pathint b c + ?pathint c a) \<le> norm y / 2"
  2516             by (simp add: f_0_shrink) (metis (mono_tags) add.commute minus_add_distrib norm_minus_cancel uminus_add_conv_diff)
  2517           then have "False"
  2518             using pi_eq_y ynz by auto
  2519         }
  2520         moreover have "uniformly_continuous_on (convex hull {a,b,c}) f"
  2521           by (simp add: contf compact_convex_hull compact_uniformly_continuous)
  2522         ultimately have "False"
  2523           unfolding uniformly_continuous_on_def
  2524           by (force simp: ynz \<open>0 < C\<close> dist_norm)
  2525         then show ?thesis ..
  2526       qed
  2527   }
  2528   moreover have "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
  2529     using fabc contour_integrable_continuous_linepath by auto
  2530   ultimately show ?thesis
  2531     using has_contour_integral_integral by fastforce
  2532 qed
  2533 
  2534 
  2535 subsection\<open>Version allowing finite number of exceptional points\<close>
  2536 
  2537 lemma Cauchy_theorem_triangle_cofinite:
  2538   assumes "continuous_on (convex hull {a,b,c}) f"
  2539       and "finite s"
  2540       and "(\<And>x. x \<in> interior(convex hull {a,b,c}) - s \<Longrightarrow> f field_differentiable (at x))"
  2541      shows "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2542 using assms
  2543 proof (induction "card s" arbitrary: a b c s rule: less_induct)
  2544   case (less s a b c)
  2545   show ?case
  2546   proof (cases "s={}")
  2547     case True with less show ?thesis
  2548       by (fastforce simp: holomorphic_on_def field_differentiable_at_within
  2549                     Cauchy_theorem_triangle_interior)
  2550   next
  2551     case False
  2552     then obtain d s' where d: "s = insert d s'" "d \<notin> s'"
  2553       by (meson Set.set_insert all_not_in_conv)
  2554     then show ?thesis
  2555     proof (cases "d \<in> convex hull {a,b,c}")
  2556       case False
  2557       show "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2558         apply (rule less.hyps [of "s'"])
  2559         using False d \<open>finite s\<close> interior_subset
  2560         apply (auto intro!: less.prems)
  2561         done
  2562     next
  2563       case True
  2564       have *: "convex hull {a, b, d} \<subseteq> convex hull {a, b, c}"
  2565         by (meson True hull_subset insert_subset convex_hull_subset)
  2566       have abd: "(f has_contour_integral 0) (linepath a b +++ linepath b d +++ linepath d a)"
  2567         apply (rule less.hyps [of "s'"])
  2568         using True d  \<open>finite s\<close> not_in_interior_convex_hull_3
  2569         apply (auto intro!: less.prems continuous_on_subset [OF  _ *])
  2570         apply (metis * insert_absorb insert_subset interior_mono)
  2571         done
  2572       have *: "convex hull {b, c, d} \<subseteq> convex hull {a, b, c}"
  2573         by (meson True hull_subset insert_subset convex_hull_subset)
  2574       have bcd: "(f has_contour_integral 0) (linepath b c +++ linepath c d +++ linepath d b)"
  2575         apply (rule less.hyps [of "s'"])
  2576         using True d  \<open>finite s\<close> not_in_interior_convex_hull_3
  2577         apply (auto intro!: less.prems continuous_on_subset [OF _ *])
  2578         apply (metis * insert_absorb insert_subset interior_mono)
  2579         done
  2580       have *: "convex hull {c, a, d} \<subseteq> convex hull {a, b, c}"
  2581         by (meson True hull_subset insert_subset convex_hull_subset)
  2582       have cad: "(f has_contour_integral 0) (linepath c a +++ linepath a d +++ linepath d c)"
  2583         apply (rule less.hyps [of "s'"])
  2584         using True d  \<open>finite s\<close> not_in_interior_convex_hull_3
  2585         apply (auto intro!: less.prems continuous_on_subset [OF _ *])
  2586         apply (metis * insert_absorb insert_subset interior_mono)
  2587         done
  2588       have "f contour_integrable_on linepath a b"
  2589         using less.prems
  2590         by (metis continuous_on_subset insert_commute contour_integrable_continuous_linepath segments_subset_convex_hull(3))
  2591       moreover have "f contour_integrable_on linepath b c"
  2592         using less.prems
  2593         by (metis continuous_on_subset contour_integrable_continuous_linepath segments_subset_convex_hull(3))
  2594       moreover have "f contour_integrable_on linepath c a"
  2595         using less.prems
  2596         by (metis continuous_on_subset insert_commute contour_integrable_continuous_linepath segments_subset_convex_hull(3))
  2597       ultimately have fpi: "f contour_integrable_on (linepath a b +++ linepath b c +++ linepath c a)"
  2598         by auto
  2599       { fix y::complex
  2600         assume fy: "(f has_contour_integral y) (linepath a b +++ linepath b c +++ linepath c a)"
  2601            and ynz: "y \<noteq> 0"
  2602         have cont_ad: "continuous_on (closed_segment a d) f"
  2603           by (meson "*" continuous_on_subset less.prems(1) segments_subset_convex_hull(3))
  2604         have cont_bd: "continuous_on (closed_segment b d) f"
  2605           by (meson True closed_segment_subset_convex_hull continuous_on_subset hull_subset insert_subset less.prems(1))
  2606         have cont_cd: "continuous_on (closed_segment c d) f"
  2607           by (meson "*" continuous_on_subset less.prems(1) segments_subset_convex_hull(2))
  2608         have "contour_integral  (linepath a b) f = - (contour_integral (linepath b d) f + (contour_integral (linepath d a) f))"
  2609                 "contour_integral  (linepath b c) f = - (contour_integral (linepath c d) f + (contour_integral (linepath d b) f))"
  2610                 "contour_integral  (linepath c a) f = - (contour_integral (linepath a d) f + contour_integral (linepath d c) f)"
  2611             using has_chain_integral_chain_integral3 [OF abd]
  2612                   has_chain_integral_chain_integral3 [OF bcd]
  2613                   has_chain_integral_chain_integral3 [OF cad]
  2614             by (simp_all add: algebra_simps add_eq_0_iff)
  2615         then have ?thesis
  2616           using cont_ad cont_bd cont_cd fy has_chain_integral_chain_integral3 contour_integral_reverse_linepath by fastforce
  2617       }
  2618       then show ?thesis
  2619         using fpi contour_integrable_on_def by blast
  2620     qed
  2621   qed
  2622 qed
  2623 
  2624 
  2625 subsection\<open>Cauchy's theorem for an open starlike set\<close>
  2626 
  2627 lemma starlike_convex_subset:
  2628   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"
  2629     shows "convex hull {a,b,c} \<subseteq> s"
  2630       using s
  2631       apply (clarsimp simp add: convex_hull_insert [of "{b,c}" a] segment_convex_hull)
  2632       apply (meson subs convexD convex_closed_segment ends_in_segment(1) ends_in_segment(2) subsetCE)
  2633       done
  2634 
  2635 lemma triangle_contour_integrals_starlike_primitive:
  2636   assumes contf: "continuous_on s f"
  2637       and s: "a \<in> s" "open s"
  2638       and x: "x \<in> s"
  2639       and subs: "\<And>y. y \<in> s \<Longrightarrow> closed_segment a y \<subseteq> s"
  2640       and zer: "\<And>b c. closed_segment b c \<subseteq> s
  2641                    \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f +
  2642                        contour_integral (linepath c a) f = 0"
  2643     shows "((\<lambda>x. contour_integral(linepath a x) f) has_field_derivative f x) (at x)"
  2644 proof -
  2645   let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2646   { fix e y
  2647     assume e: "0 < e" and bxe: "ball x e \<subseteq> s" and close: "cmod (y - x) < e"
  2648     have y: "y \<in> s"
  2649       using bxe close  by (force simp: dist_norm norm_minus_commute)
  2650     have cont_ayf: "continuous_on (closed_segment a y) f"
  2651       using contf continuous_on_subset subs y by blast
  2652     have xys: "closed_segment x y \<subseteq> s"
  2653       apply (rule order_trans [OF _ bxe])
  2654       using close
  2655       by (auto simp: dist_norm ball_def norm_minus_commute dest: segment_bound)
  2656     have "?pathint a y - ?pathint a x = ?pathint x y"
  2657       using zer [OF xys]  contour_integral_reverse_linepath [OF cont_ayf]  add_eq_0_iff by force
  2658   } note [simp] = this
  2659   { fix e::real
  2660     assume e: "0 < e"
  2661     have cont_atx: "continuous (at x) f"
  2662       using x s contf continuous_on_eq_continuous_at by blast
  2663     then obtain d1 where d1: "d1>0" and d1_less: "\<And>y. cmod (y - x) < d1 \<Longrightarrow> cmod (f y - f x) < e/2"
  2664       unfolding continuous_at Lim_at dist_norm  using e
  2665       by (drule_tac x="e/2" in spec) force
  2666     obtain d2 where d2: "d2>0" "ball x d2 \<subseteq> s" using  \<open>open s\<close> x
  2667       by (auto simp: open_contains_ball)
  2668     have dpos: "min d1 d2 > 0" using d1 d2 by simp
  2669     { fix y
  2670       assume yx: "y \<noteq> x" and close: "cmod (y - x) < min d1 d2"
  2671       have y: "y \<in> s"
  2672         using d2 close  by (force simp: dist_norm norm_minus_commute)
  2673       have fxy: "f contour_integrable_on linepath x y"
  2674         apply (rule contour_integrable_continuous_linepath)
  2675         apply (rule continuous_on_subset [OF contf])
  2676         using close d2
  2677         apply (auto simp: dist_norm norm_minus_commute dest!: segment_bound(1))
  2678         done
  2679       then obtain i where i: "(f has_contour_integral i) (linepath x y)"
  2680         by (auto simp: contour_integrable_on_def)
  2681       then have "((\<lambda>w. f w - f x) has_contour_integral (i - f x * (y - x))) (linepath x y)"
  2682         by (rule has_contour_integral_diff [OF _ has_contour_integral_const_linepath])
  2683       then have "cmod (i - f x * (y - x)) \<le> e / 2 * cmod (y - x)"
  2684         apply (rule has_contour_integral_bound_linepath [where B = "e/2"])
  2685         using e apply simp
  2686         apply (rule d1_less [THEN less_imp_le])
  2687         using close segment_bound
  2688         apply force
  2689         done
  2690       also have "... < e * cmod (y - x)"
  2691         by (simp add: e yx)
  2692       finally have "cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
  2693         using i yx  by (simp add: contour_integral_unique divide_less_eq)
  2694     }
  2695     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"
  2696       using dpos by blast
  2697   }
  2698   then have *: "(\<lambda>y. (?pathint x y - f x * (y - x)) /\<^sub>R cmod (y - x)) \<midarrow>x\<rightarrow> 0"
  2699     by (simp add: Lim_at dist_norm inverse_eq_divide)
  2700   show ?thesis
  2701     apply (simp add: has_field_derivative_def has_derivative_at bounded_linear_mult_right)
  2702     apply (rule Lim_transform [OF * Lim_eventually])
  2703     apply (simp add: inverse_eq_divide [symmetric] eventually_at)
  2704     using \<open>open s\<close> x
  2705     apply (force simp: dist_norm open_contains_ball)
  2706     done
  2707 qed
  2708 
  2709 (** Existence of a primitive.*)
  2710 
  2711 lemma holomorphic_starlike_primitive:
  2712   fixes f :: "complex \<Rightarrow> complex"
  2713   assumes contf: "continuous_on s f"
  2714       and s: "starlike s" and os: "open s"
  2715       and k: "finite k"
  2716       and fcd: "\<And>x. x \<in> s - k \<Longrightarrow> f field_differentiable at x"
  2717     shows "\<exists>g. \<forall>x \<in> s. (g has_field_derivative f x) (at x)"
  2718 proof -
  2719   obtain a where a: "a\<in>s" and a_cs: "\<And>x. x\<in>s \<Longrightarrow> closed_segment a x \<subseteq> s"
  2720     using s by (auto simp: starlike_def)
  2721   { fix x b c
  2722     assume "x \<in> s" "closed_segment b c \<subseteq> s"
  2723     then have abcs: "convex hull {a, b, c} \<subseteq> s"
  2724       by (simp add: a a_cs starlike_convex_subset)
  2725     then have *: "continuous_on (convex hull {a, b, c}) f"
  2726       by (simp add: continuous_on_subset [OF contf])
  2727     have "(f has_contour_integral 0) (linepath a b +++ linepath b c +++ linepath c a)"
  2728       apply (rule Cauchy_theorem_triangle_cofinite [OF _ k])
  2729       using abcs apply (simp add: continuous_on_subset [OF contf])
  2730       using * abcs interior_subset apply (auto intro: fcd)
  2731       done
  2732   } note 0 = this
  2733   show ?thesis
  2734     apply (intro exI ballI)
  2735     apply (rule triangle_contour_integrals_starlike_primitive [OF contf a os], assumption)
  2736     apply (metis a_cs)
  2737     apply (metis has_chain_integral_chain_integral3 0)
  2738     done
  2739 qed
  2740 
  2741 lemma Cauchy_theorem_starlike:
  2742  "\<lbrakk>open s; starlike s; finite k; continuous_on s f;
  2743    \<And>x. x \<in> s - k \<Longrightarrow> f field_differentiable at x;
  2744    valid_path g; path_image g \<subseteq> s; pathfinish g = pathstart g\<rbrakk>
  2745    \<Longrightarrow> (f has_contour_integral 0)  g"
  2746   by (metis holomorphic_starlike_primitive Cauchy_theorem_primitive at_within_open)
  2747 
  2748 lemma Cauchy_theorem_starlike_simple:
  2749   "\<lbrakk>open s; starlike s; f holomorphic_on s; valid_path g; path_image g \<subseteq> s; pathfinish g = pathstart g\<rbrakk>
  2750    \<Longrightarrow> (f has_contour_integral 0) g"
  2751 apply (rule Cauchy_theorem_starlike [OF _ _ finite.emptyI])
  2752 apply (simp_all add: holomorphic_on_imp_continuous_on)
  2753 apply (metis at_within_open holomorphic_on_def)
  2754 done
  2755 
  2756 
  2757 subsection\<open>Cauchy's theorem for a convex set\<close>
  2758 
  2759 text\<open>For a convex set we can avoid assuming openness and boundary analyticity\<close>
  2760 
  2761 lemma triangle_contour_integrals_convex_primitive:
  2762   assumes contf: "continuous_on s f"
  2763       and s: "a \<in> s" "convex s"
  2764       and x: "x \<in> s"
  2765       and zer: "\<And>b c. \<lbrakk>b \<in> s; c \<in> s\<rbrakk>
  2766                    \<Longrightarrow> contour_integral (linepath a b) f + contour_integral (linepath b c) f +
  2767                        contour_integral (linepath c a) f = 0"
  2768     shows "((\<lambda>x. contour_integral(linepath a x) f) has_field_derivative f x) (at x within s)"
  2769 proof -
  2770   let ?pathint = "\<lambda>x y. contour_integral(linepath x y) f"
  2771   { fix y
  2772     assume y: "y \<in> s"
  2773     have cont_ayf: "continuous_on (closed_segment a y) f"
  2774       using s y  by (meson contf continuous_on_subset convex_contains_segment)
  2775     have xys: "closed_segment x y \<subseteq> s"  (*?*)
  2776       using convex_contains_segment s x y by auto
  2777     have "?pathint a y - ?pathint a x = ?pathint x y"
  2778       using zer [OF x y]  contour_integral_reverse_linepath [OF cont_ayf]  add_eq_0_iff by force
  2779   } note [simp] = this
  2780   { fix e::real
  2781     assume e: "0 < e"
  2782     have cont_atx: "continuous (at x within s) f"
  2783       using x s contf  by (simp add: continuous_on_eq_continuous_within)
  2784     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"
  2785       unfolding continuous_within Lim_within dist_norm using e
  2786       by (drule_tac x="e/2" in spec) force
  2787     { fix y
  2788       assume yx: "y \<noteq> x" and close: "cmod (y - x) < d1" and y: "y \<in> s"
  2789       have fxy: "f contour_integrable_on linepath x y"
  2790         using convex_contains_segment s x y
  2791         by (blast intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
  2792       then obtain i where i: "(f has_contour_integral i) (linepath x y)"
  2793         by (auto simp: contour_integrable_on_def)
  2794       then have "((\<lambda>w. f w - f x) has_contour_integral (i - f x * (y - x))) (linepath x y)"
  2795         by (rule has_contour_integral_diff [OF _ has_contour_integral_const_linepath])
  2796       then have "cmod (i - f x * (y - x)) \<le> e / 2 * cmod (y - x)"
  2797         apply (rule has_contour_integral_bound_linepath [where B = "e/2"])
  2798         using e apply simp
  2799         apply (rule d1_less [THEN less_imp_le])
  2800         using convex_contains_segment s(2) x y apply blast
  2801         using close segment_bound(1) apply fastforce
  2802         done
  2803       also have "... < e * cmod (y - x)"
  2804         by (simp add: e yx)
  2805       finally have "cmod (?pathint x y - f x * (y-x)) / cmod (y-x) < e"
  2806         using i yx  by (simp add: contour_integral_unique divide_less_eq)
  2807     }
  2808     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"
  2809       using d1 by blast
  2810   }
  2811   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)"
  2812     by (simp add: Lim_within dist_norm inverse_eq_divide)
  2813   show ?thesis
  2814     apply (simp add: has_field_derivative_def has_derivative_within bounded_linear_mult_right)
  2815     apply (rule Lim_transform [OF * Lim_eventually])
  2816     using linordered_field_no_ub
  2817     apply (force simp: inverse_eq_divide [symmetric] eventually_at)
  2818     done
  2819 qed
  2820 
  2821 lemma contour_integral_convex_primitive:
  2822   "\<lbrakk>convex s; continuous_on s f;
  2823     \<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>
  2824          \<Longrightarrow> \<exists>g. \<forall>x \<in> s. (g has_field_derivative f x) (at x within s)"
  2825   apply (cases "s={}")
  2826   apply (simp_all add: ex_in_conv [symmetric])
  2827   apply (blast intro: triangle_contour_integrals_convex_primitive has_chain_integral_chain_integral3)
  2828   done
  2829 
  2830 lemma holomorphic_convex_primitive:
  2831   fixes f :: "complex \<Rightarrow> complex"
  2832   shows
  2833   "\<lbrakk>convex s; finite k; continuous_on s f;
  2834     \<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x\<rbrakk>
  2835    \<Longrightarrow> \<exists>g. \<forall>x \<in> s. (g has_field_derivative f x) (at x within s)"
  2836 apply (rule contour_integral_convex_primitive [OF _ _ Cauchy_theorem_triangle_cofinite])
  2837 prefer 3
  2838 apply (erule continuous_on_subset)
  2839 apply (simp add: subset_hull continuous_on_subset, assumption+)
  2840 by (metis Diff_iff convex_contains_segment insert_absorb insert_subset interior_mono segment_convex_hull subset_hull)
  2841 
  2842 lemma Cauchy_theorem_convex:
  2843     "\<lbrakk>continuous_on s f; convex s; finite k;
  2844       \<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x;
  2845      valid_path g; path_image g \<subseteq> s;
  2846      pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
  2847   by (metis holomorphic_convex_primitive Cauchy_theorem_primitive)
  2848 
  2849 lemma Cauchy_theorem_convex_simple:
  2850     "\<lbrakk>f holomorphic_on s; convex s;
  2851      valid_path g; path_image g \<subseteq> s;
  2852      pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
  2853   apply (rule Cauchy_theorem_convex)
  2854   apply (simp_all add: holomorphic_on_imp_continuous_on)
  2855   apply (rule finite.emptyI)
  2856   using at_within_interior holomorphic_on_def interior_subset by fastforce
  2857 
  2858 
  2859 text\<open>In particular for a disc\<close>
  2860 lemma Cauchy_theorem_disc:
  2861     "\<lbrakk>finite k; continuous_on (cball a e) f;
  2862       \<And>x. x \<in> ball a e - k \<Longrightarrow> f field_differentiable at x;
  2863      valid_path g; path_image g \<subseteq> cball a e;
  2864      pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
  2865   apply (rule Cauchy_theorem_convex)
  2866   apply (auto simp: convex_cball interior_cball)
  2867   done
  2868 
  2869 lemma Cauchy_theorem_disc_simple:
  2870     "\<lbrakk>f holomorphic_on (ball a e); valid_path g; path_image g \<subseteq> ball a e;
  2871      pathfinish g = pathstart g\<rbrakk> \<Longrightarrow> (f has_contour_integral 0) g"
  2872 by (simp add: Cauchy_theorem_convex_simple)
  2873 
  2874 
  2875 subsection\<open>Generalize integrability to local primitives\<close>
  2876 
  2877 lemma contour_integral_local_primitive_lemma:
  2878   fixes f :: "complex\<Rightarrow>complex"
  2879   shows
  2880     "\<lbrakk>g piecewise_differentiable_on {a..b};
  2881       \<And>x. x \<in> s \<Longrightarrow> (f has_field_derivative f' x) (at x within s);
  2882       \<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s\<rbrakk>
  2883      \<Longrightarrow> (\<lambda>x. f' (g x) * vector_derivative g (at x within {a..b}))
  2884             integrable_on {a..b}"
  2885   apply (cases "cbox a b = {}", force)
  2886   apply (simp add: integrable_on_def)
  2887   apply (rule exI)
  2888   apply (rule contour_integral_primitive_lemma, assumption+)
  2889   using atLeastAtMost_iff by blast
  2890 
  2891 lemma contour_integral_local_primitive_any:
  2892   fixes f :: "complex \<Rightarrow> complex"
  2893   assumes gpd: "g piecewise_differentiable_on {a..b}"
  2894       and dh: "\<And>x. x \<in> s
  2895                \<Longrightarrow> \<exists>d h. 0 < d \<and>
  2896                          (\<forall>y. norm(y - x) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
  2897       and gs: "\<And>x. x \<in> {a..b} \<Longrightarrow> g x \<in> s"
  2898   shows "(\<lambda>x. f(g x) * vector_derivative g (at x)) integrable_on {a..b}"
  2899 proof -
  2900   { fix x
  2901     assume x: "a \<le> x" "x \<le> b"
  2902     obtain d h where d: "0 < d"
  2903                and h: "(\<And>y. norm(y - g x) < d \<Longrightarrow> (h has_field_derivative f y) (at y within s))"
  2904       using x gs dh by (metis atLeastAtMost_iff)
  2905     have "continuous_on {a..b} g" using gpd piecewise_differentiable_on_def by blast
  2906     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"
  2907       using x d
  2908       apply (auto simp: dist_norm continuous_on_iff)
  2909       apply (drule_tac x=x in bspec)
  2910       using x apply simp
  2911       apply (drule_tac x=d in spec, auto)
  2912       done
  2913     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>
  2914                           (\<lambda>x. f (g x) * vector_derivative g (at x)) integrable_on {u..v}"
  2915       apply (rule_tac x=e in exI)
  2916       using e
  2917       apply (simp add: integrable_on_localized_vector_derivative [symmetric], clarify)
  2918       apply (rule_tac f = h and s = "g ` {u..v}" in contour_integral_local_primitive_lemma)
  2919         apply (meson atLeastatMost_subset_iff gpd piecewise_differentiable_on_subset)
  2920        apply (force simp: ball_def dist_norm intro: lessd gs DERIV_subset [OF h], force)
  2921       done
  2922   } then
  2923   show ?thesis
  2924     by (force simp: intro!: integrable_on_little_subintervals [of a b, simplified])
  2925 qed
  2926 
  2927 lemma contour_integral_local_primitive:
  2928   fixes f :: "complex \<Rightarrow> complex"
  2929   assumes g: "valid_path g" "path_image g \<subseteq> s"
  2930       and dh: "\<And>x. x \<in> s
  2931                \<Longrightarrow> \<exists>d h. 0 < d \<and>
  2932                          (\<forall>y. norm(y - x) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
  2933   shows "f contour_integrable_on g"
  2934   using g
  2935   apply (simp add: valid_path_def path_image_def contour_integrable_on_def has_contour_integral_def
  2936             has_integral_localized_vector_derivative integrable_on_def [symmetric])
  2937   using contour_integral_local_primitive_any [OF _ dh]
  2938   by (meson image_subset_iff piecewise_C1_imp_differentiable)
  2939 
  2940 
  2941 text\<open>In particular if a function is holomorphic\<close>
  2942 
  2943 lemma contour_integrable_holomorphic:
  2944   assumes contf: "continuous_on s f"
  2945       and os: "open s"
  2946       and k: "finite k"
  2947       and g: "valid_path g" "path_image g \<subseteq> s"
  2948       and fcd: "\<And>x. x \<in> s - k \<Longrightarrow> f field_differentiable at x"
  2949     shows "f contour_integrable_on g"
  2950 proof -
  2951   { fix z
  2952     assume z: "z \<in> s"
  2953     obtain d where d: "d>0" "ball z d \<subseteq> s" using  \<open>open s\<close> z
  2954       by (auto simp: open_contains_ball)
  2955     then have contfb: "continuous_on (ball z d) f"
  2956       using contf continuous_on_subset by blast
  2957     obtain h where "\<forall>y\<in>ball z d. (h has_field_derivative f y) (at y within ball z d)"
  2958       using holomorphic_convex_primitive [OF convex_ball k contfb fcd] d
  2959             interior_subset by force
  2960     then have "\<forall>y\<in>ball z d. (h has_field_derivative f y) (at y within s)"
  2961       by (metis Topology_Euclidean_Space.open_ball at_within_open d(2) os subsetCE)
  2962     then have "\<exists>h. (\<forall>y. cmod (y - z) < d \<longrightarrow> (h has_field_derivative f y) (at y within s))"
  2963       by (force simp: dist_norm norm_minus_commute)
  2964     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))"
  2965       using d by blast
  2966   }
  2967   then show ?thesis
  2968     by (rule contour_integral_local_primitive [OF g])
  2969 qed
  2970 
  2971 lemma contour_integrable_holomorphic_simple:
  2972   assumes fh: "f holomorphic_on s"
  2973       and os: "open s"
  2974       and g: "valid_path g" "path_image g \<subseteq> s"
  2975     shows "f contour_integrable_on g"
  2976   apply (rule contour_integrable_holomorphic [OF _ os Finite_Set.finite.emptyI g])
  2977   apply (simp add: fh holomorphic_on_imp_continuous_on)
  2978   using fh  by (simp add: field_differentiable_def holomorphic_on_open os)
  2979 
  2980 lemma continuous_on_inversediff:
  2981   fixes z:: "'a::real_normed_field" shows "z \<notin> s \<Longrightarrow> continuous_on s (\<lambda>w. 1 / (w - z))"
  2982   by (rule continuous_intros | force)+
  2983 
  2984 corollary contour_integrable_inversediff:
  2985     "\<lbrakk>valid_path g; z \<notin> path_image g\<rbrakk> \<Longrightarrow> (\<lambda>w. 1 / (w-z)) contour_integrable_on g"
  2986 apply (rule contour_integrable_holomorphic_simple [of _ "UNIV-{z}"])
  2987 apply (auto simp: holomorphic_on_open open_delete intro!: derivative_eq_intros)
  2988 done
  2989 
  2990 text\<open>Key fact that path integral is the same for a "nearby" path. This is the
  2991  main lemma for the homotopy form of Cauchy's theorem and is also useful
  2992  if we want "without loss of generality" to assume some nice properties of a
  2993  path (e.g. smoothness). It can also be used to define the integrals of
  2994  analytic functions over arbitrary continuous paths. This is just done for
  2995  winding numbers now.
  2996 \<close>
  2997 
  2998 text\<open>A technical definition to avoid duplication of similar proofs,
  2999      for paths joined at the ends versus looping paths\<close>
  3000 definition linked_paths :: "bool \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> (real \<Rightarrow> 'a::topological_space) \<Rightarrow> bool"
  3001   where "linked_paths atends g h ==
  3002         (if atends then pathstart h = pathstart g \<and> pathfinish h = pathfinish g
  3003                    else pathfinish g = pathstart g \<and> pathfinish h = pathstart h)"
  3004 
  3005 text\<open>This formulation covers two cases: @{term g} and @{term h} share their
  3006       start and end points; @{term g} and @{term h} both loop upon themselves.\<close>
  3007 lemma contour_integral_nearby:
  3008   assumes os: "open s" and p: "path p" "path_image p \<subseteq> s"
  3009     shows
  3010        "\<exists>d. 0 < d \<and>
  3011             (\<forall>g h. valid_path g \<and> valid_path h \<and>
  3012                   (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
  3013                   linked_paths atends g h
  3014                   \<longrightarrow> path_image g \<subseteq> s \<and> path_image h \<subseteq> s \<and>
  3015                       (\<forall>f. f holomorphic_on s \<longrightarrow> contour_integral h f = contour_integral g f))"
  3016 proof -
  3017   have "\<forall>z. \<exists>e. z \<in> path_image p \<longrightarrow> 0 < e \<and> ball z e \<subseteq> s"
  3018     using open_contains_ball os p(2) by blast
  3019   then obtain ee where ee: "\<And>z. z \<in> path_image p \<Longrightarrow> 0 < ee z \<and> ball z (ee z) \<subseteq> s"
  3020     by metis
  3021   define cover where "cover = (\<lambda>z. ball z (ee z/3)) ` (path_image p)"
  3022   have "compact (path_image p)"
  3023     by (metis p(1) compact_path_image)
  3024   moreover have "path_image p \<subseteq> (\<Union>c\<in>path_image p. ball c (ee c / 3))"
  3025     using ee by auto
  3026   ultimately have "\<exists>D \<subseteq> cover. finite D \<and> path_image p \<subseteq> \<Union>D"
  3027     by (simp add: compact_eq_heine_borel cover_def)
  3028   then obtain D where D: "D \<subseteq> cover" "finite D" "path_image p \<subseteq> \<Union>D"
  3029     by blast
  3030   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"
  3031     apply (simp add: cover_def path_image_def image_comp)
  3032     apply (blast dest!: finite_subset_image [OF \<open>finite D\<close>])
  3033     done
  3034   then have kne: "k \<noteq> {}"
  3035     using D by auto
  3036   have pi: "\<And>i. i \<in> k \<Longrightarrow> p i \<in> path_image p"
  3037     using k  by (auto simp: path_image_def)
  3038   then have eepi: "\<And>i. i \<in> k \<Longrightarrow> 0 < ee((p i))"
  3039     by (metis ee)
  3040   define e where "e = Min((ee o p) ` k)"
  3041   have fin_eep: "finite ((ee o p) ` k)"
  3042     using k  by blast
  3043   have enz: "0 < e"
  3044     using ee k  by (simp add: kne e_def Min_gr_iff [OF fin_eep] eepi)
  3045   have "uniformly_continuous_on {0..1} p"
  3046     using p  by (simp add: path_def compact_uniformly_continuous)
  3047   then obtain d::real where d: "d>0"
  3048           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"
  3049     unfolding uniformly_continuous_on_def dist_norm real_norm_def
  3050     by (metis divide_pos_pos enz zero_less_numeral)
  3051   then obtain N::nat where N: "N>0" "inverse N < d"
  3052     using real_arch_inverse [of d]   by auto
  3053   { fix g h
  3054     assume g: "valid_path g" and gp: "\<forall>t\<in>{0..1}. cmod (g t - p t) < e / 3"
  3055        and h: "valid_path h" and hp: "\<forall>t\<in>{0..1}. cmod (h t - p t) < e / 3"
  3056        and joins: "linked_paths atends g h"
  3057     { fix t::real
  3058       assume t: "0 \<le> t" "t \<le> 1"
  3059       then obtain u where u: "u \<in> k" and ptu: "p t \<in> ball(p u) (ee(p u) / 3)"
  3060         using \<open>path_image p \<subseteq> \<Union>D\<close> D_eq by (force simp: path_image_def)
  3061       then have ele: "e \<le> ee (p u)" using fin_eep
  3062         by (simp add: e_def)
  3063       have "cmod (g t - p t) < e / 3" "cmod (h t - p t) < e / 3"
  3064         using gp hp t by auto
  3065       with ele have "cmod (g t - p t) < ee (p u) / 3"
  3066                     "cmod (h t - p t) < ee (p u) / 3"
  3067         by linarith+
  3068       then have "g t \<in> ball(p u) (ee(p u))"  "h t \<in> ball(p u) (ee(p u))"
  3069         using norm_diff_triangle_ineq [of "g t" "p t" "p t" "p u"]
  3070               norm_diff_triangle_ineq [of "h t" "p t" "p t" "p u"] ptu eepi u
  3071         by (force simp: dist_norm ball_def norm_minus_commute)+
  3072       then have "g t \<in> s" "h t \<in> s" using ee u k
  3073         by (auto simp: path_image_def ball_def)
  3074     }
  3075     then have ghs: "path_image g \<subseteq> s" "path_image h \<subseteq> s"
  3076       by (auto simp: path_image_def)
  3077     moreover
  3078     { fix f
  3079       assume fhols: "f holomorphic_on s"
  3080       then have fpa: "f contour_integrable_on g"  "f contour_integrable_on h"
  3081         using g ghs h holomorphic_on_imp_continuous_on os contour_integrable_holomorphic_simple
  3082         by blast+
  3083       have contf: "continuous_on s f"
  3084         by (simp add: fhols holomorphic_on_imp_continuous_on)
  3085       { fix z
  3086         assume z: "z \<in> path_image p"
  3087         have "f holomorphic_on ball z (ee z)"
  3088           using fhols ee z holomorphic_on_subset by blast
  3089         then have "\<exists>ff. (\<forall>w \<in> ball z (ee z). (ff has_field_derivative f w) (at w))"
  3090           using holomorphic_convex_primitive [of "ball z (ee z)" "{}" f, simplified]
  3091           by (metis open_ball at_within_open holomorphic_on_def holomorphic_on_imp_continuous_on mem_ball)
  3092       }
  3093       then obtain ff where ff:
  3094             "\<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)"
  3095         by metis
  3096       { fix n
  3097         assume n: "n \<le> N"
  3098         then have "contour_integral(subpath 0 (n/N) h) f - contour_integral(subpath 0 (n/N) g) f =
  3099                    contour_integral(linepath (g(n/N)) (h(n/N))) f - contour_integral(linepath (g 0) (h 0)) f"
  3100         proof (induct n)
  3101           case 0 show ?case by simp
  3102         next
  3103           case (Suc n)
  3104           obtain t where t: "t \<in> k" and "p (n/N) \<in> ball(p t) (ee(p t) / 3)"
  3105             using \<open>path_image p \<subseteq> \<Union>D\<close> [THEN subsetD, where c="p (n/N)"] D_eq N Suc.prems
  3106             by (force simp: path_image_def)
  3107           then have ptu: "cmod (p t - p (n/N)) < ee (p t) / 3"
  3108             by (simp add: dist_norm)
  3109           have e3le: "e/3 \<le> ee (p t) / 3"  using fin_eep t
  3110             by (simp add: e_def)
  3111           { fix x
  3112             assume x: "n/N \<le> x" "x \<le> (1 + n)/N"
  3113             then have nN01: "0 \<le> n/N" "(1 + n)/N \<le> 1"
  3114               using Suc.prems by auto
  3115             then have x01: "0 \<le> x" "x \<le> 1"
  3116               using x by linarith+
  3117             have "cmod (p t - p x)  < ee (p t) / 3 + e/3"
  3118               apply (rule norm_diff_triangle_less [OF ptu de])
  3119               using x N x01 Suc.prems
  3120               apply (auto simp: field_simps)
  3121               done
  3122             then have ptx: "cmod (p t - p x) < 2*ee (p t)/3"
  3123               using e3le eepi [OF t] by simp
  3124             have "cmod (p t - g x) < 2*ee (p t)/3 + e/3 "
  3125               apply (rule norm_diff_triangle_less [OF ptx])
  3126               using gp x01 by (simp add: norm_minus_commute)
  3127             also have "... \<le> ee (p t)"
  3128               using e3le eepi [OF t] by simp
  3129             finally have gg: "cmod (p t - g x) < ee (p t)" .
  3130             have "cmod (p t - h x) < 2*ee (p t)/3 + e/3 "
  3131               apply (rule norm_diff_triangle_less [OF ptx])
  3132               using hp x01 by (simp add: norm_minus_commute)
  3133             also have "... \<le> ee (p t)"
  3134               using e3le eepi [OF t] by simp
  3135             finally have "cmod (p t - g x) < ee (p t)"
  3136                          "cmod (p t - h x) < ee (p t)"
  3137               using gg by auto
  3138           } note ptgh_ee = this
  3139           have pi_hgn: "path_image (linepath (h (n/N)) (g (n/N))) \<subseteq> ball (p t) (ee (p t))"
  3140             using ptgh_ee [of "n/N"] Suc.prems
  3141             by (auto simp: field_simps dist_norm dest: segment_furthest_le [where y="p t"])
  3142           then have gh_ns: "closed_segment (g (n/N)) (h (n/N)) \<subseteq> s"
  3143             using \<open>N>0\<close> Suc.prems
  3144             apply (simp add: path_image_join field_simps closed_segment_commute)
  3145             apply (erule order_trans)
  3146             apply (simp add: ee pi t)
  3147             done
  3148           have pi_ghn': "path_image (linepath (g ((1 + n) / N)) (h ((1 + n) / N)))
  3149                   \<subseteq> ball (p t) (ee (p t))"
  3150             using ptgh_ee [of "(1+n)/N"] Suc.prems
  3151             by (auto simp: field_simps dist_norm dest: segment_furthest_le [where y="p t"])
  3152           then have gh_n's: "closed_segment (g ((1 + n) / N)) (h ((1 + n) / N)) \<subseteq> s"
  3153             using \<open>N>0\<close> Suc.prems ee pi t
  3154             by (auto simp: Path_Connected.path_image_join field_simps)
  3155           have pi_subset_ball:
  3156                 "path_image (subpath (n/N) ((1+n) / N) g +++ linepath (g ((1+n) / N)) (h ((1+n) / N)) +++
  3157                              subpath ((1+n) / N) (n/N) h +++ linepath (h (n/N)) (g (n/N)))
  3158                  \<subseteq> ball (p t) (ee (p t))"
  3159             apply (intro subset_path_image_join pi_hgn pi_ghn')
  3160             using \<open>N>0\<close> Suc.prems
  3161             apply (auto simp: path_image_subpath dist_norm field_simps closed_segment_eq_real_ivl ptgh_ee)
  3162             done
  3163           have pi0: "(f has_contour_integral 0)
  3164                        (subpath (n/ N) ((Suc n)/N) g +++ linepath(g ((Suc n) / N)) (h((Suc n) / N)) +++
  3165                         subpath ((Suc n) / N) (n/N) h +++ linepath(h (n/N)) (g (n/N)))"
  3166             apply (rule Cauchy_theorem_primitive [of "ball(p t) (ee(p t))" "ff (p t)" "f"])
  3167             apply (metis ff open_ball at_within_open pi t)
  3168             apply (intro valid_path_join)
  3169             using Suc.prems pi_subset_ball apply (simp_all add: valid_path_subpath g h)
  3170             done
  3171           have fpa1: "f contour_integrable_on subpath (real n / real N) (real (Suc n) / real N) g"
  3172             using Suc.prems by (simp add: contour_integrable_subpath g fpa)
  3173           have fpa2: "f contour_integrable_on linepath (g (real (Suc n) / real N)) (h (real (Suc n) / real N))"
  3174             using gh_n's
  3175             by (auto intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
  3176           have fpa3: "f contour_integrable_on linepath (h (real n / real N)) (g (real n / real N))"
  3177             using gh_ns
  3178             by (auto simp: closed_segment_commute intro!: contour_integrable_continuous_linepath continuous_on_subset [OF contf])
  3179           have eq0: "contour_integral (subpath (n/N) ((Suc n) / real N) g) f +
  3180                      contour_integral (linepath (g ((Suc n) / N)) (h ((Suc n) / N))) f +
  3181                      contour_integral (subpath ((Suc n) / N) (n/N) h) f +
  3182                      contour_integral (linepath (h (n/N)) (g (n/N))) f = 0"
  3183             using contour_integral_unique [OF pi0] Suc.prems
  3184             by (simp add: g h fpa valid_path_subpath contour_integrable_subpath
  3185                           fpa1 fpa2 fpa3 algebra_simps del: of_nat_Suc)
  3186           have *: "\<And>hn he hn' gn gd gn' hgn ghn gh0 ghn'.
  3187                     \<lbrakk>hn - gn = ghn - gh0;
  3188                      gd + ghn' + he + hgn = (0::complex);
  3189                      hn - he = hn'; gn + gd = gn'; hgn = -ghn\<rbrakk> \<Longrightarrow> hn' - gn' = ghn' - gh0"
  3190             by (auto simp: algebra_simps)
  3191           have "contour_integral (subpath 0 (n/N) h) f - contour_integral (subpath ((Suc n) / N) (n/N) h) f =
  3192                 contour_integral (subpath 0 (n/N) h) f + contour_integral (subpath (n/N) ((Suc n) / N) h) f"
  3193             unfolding reversepath_subpath [symmetric, of "((Suc n) / N)"]
  3194             using Suc.prems by (simp add: h fpa contour_integral_reversepath valid_path_subpath contour_integrable_subpath)
  3195           also have "... = contour_integral (subpath 0 ((Suc n) / N) h) f"
  3196             using Suc.prems by (simp add: contour_integral_subpath_combine h fpa)
  3197           finally have pi0_eq:
  3198                "contour_integral (subpath 0 (n/N) h) f - contour_integral (subpath ((Suc n) / N) (n/N) h) f =
  3199                 contour_integral (subpath 0 ((Suc n) / N) h) f" .
  3200           show ?case
  3201             apply (rule * [OF Suc.hyps eq0 pi0_eq])
  3202             using Suc.prems
  3203             apply (simp_all add: g h fpa contour_integral_subpath_combine
  3204                      contour_integral_reversepath [symmetric] contour_integrable_continuous_linepath
  3205                      continuous_on_subset [OF contf gh_ns])
  3206             done
  3207       qed
  3208       } note ind = this
  3209       have "contour_integral h f = contour_integral g f"
  3210         using ind [OF order_refl] N joins
  3211         by (simp add: linked_paths_def pathstart_def pathfinish_def split: if_split_asm)
  3212     }
  3213     ultimately
  3214     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)"
  3215       by metis
  3216   } note * = this
  3217   show ?thesis
  3218     apply (rule_tac x="e/3" in exI)
  3219     apply (rule conjI)
  3220     using enz apply simp
  3221     apply (clarsimp simp only: ball_conj_distrib)
  3222     apply (rule *; assumption)
  3223     done
  3224 qed
  3225 
  3226 
  3227 lemma
  3228   assumes "open s" "path p" "path_image p \<subseteq> s"
  3229     shows contour_integral_nearby_ends:
  3230       "\<exists>d. 0 < d \<and>
  3231               (\<forall>g h. valid_path g \<and> valid_path h \<and>
  3232                     (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
  3233                     pathstart h = pathstart g \<and> pathfinish h = pathfinish g
  3234                     \<longrightarrow> path_image g \<subseteq> s \<and>
  3235                         path_image h \<subseteq> s \<and>
  3236                         (\<forall>f. f holomorphic_on s
  3237                             \<longrightarrow> contour_integral h f = contour_integral g f))"
  3238     and contour_integral_nearby_loops:
  3239       "\<exists>d. 0 < d \<and>
  3240               (\<forall>g h. valid_path g \<and> valid_path h \<and>
  3241                     (\<forall>t \<in> {0..1}. norm(g t - p t) < d \<and> norm(h t - p t) < d) \<and>
  3242                     pathfinish g = pathstart g \<and> pathfinish h = pathstart h
  3243                     \<longrightarrow> path_image g \<subseteq> s \<and>
  3244                         path_image h \<subseteq> s \<and>
  3245                         (\<forall>f. f holomorphic_on s
  3246                             \<longrightarrow> contour_integral h f = contour_integral g f))"
  3247   using contour_integral_nearby [OF assms, where atends=True]
  3248   using contour_integral_nearby [OF assms, where atends=False]
  3249   unfolding linked_paths_def by simp_all
  3250 
  3251 lemma C1_differentiable_polynomial_function:
  3252   fixes p :: "real \<Rightarrow> 'a::euclidean_space"
  3253   shows "polynomial_function p \<Longrightarrow> p C1_differentiable_on s"
  3254   by (metis continuous_on_polymonial_function C1_differentiable_on_def  has_vector_derivative_polynomial_function)
  3255 
  3256 lemma valid_path_polynomial_function:
  3257   fixes p :: "real \<Rightarrow> 'a::euclidean_space"
  3258   shows "polynomial_function p \<Longrightarrow> valid_path p"
  3259 by (force simp: valid_path_def piecewise_C1_differentiable_on_def continuous_on_polymonial_function C1_differentiable_polynomial_function)
  3260 
  3261 lemma valid_path_subpath_trivial [simp]:
  3262     fixes g :: "real \<Rightarrow> 'a::euclidean_space"
  3263     shows "z \<noteq> g x \<Longrightarrow> valid_path (subpath x x g)"
  3264   by (simp add: subpath_def valid_path_polynomial_function)
  3265 
  3266 lemma contour_integral_bound_exists:
  3267 assumes s: "open s"
  3268     and g: "valid_path g"
  3269     and pag: "path_image g \<subseteq> s"
  3270   shows "\<exists>L. 0 < L \<and>
  3271        (\<forall>f B. f holomorphic_on s \<and> (\<forall>z \<in> s. norm(f z) \<le> B)
  3272          \<longrightarrow> norm(contour_integral g f) \<le> L*B)"
  3273 proof -
  3274 have "path g" using g
  3275   by (simp add: valid_path_imp_path)
  3276 then obtain d::real and p
  3277   where d: "0 < d"
  3278     and p: "polynomial_function p" "path_image p \<subseteq> s"
  3279     and pi: "\<And>f. f holomorphic_on s \<Longrightarrow> contour_integral g f = contour_integral p f"
  3280   using contour_integral_nearby_ends [OF s \<open>path g\<close> pag]
  3281   apply clarify
  3282   apply (drule_tac x=g in spec)
  3283   apply (simp only: assms)
  3284   apply (force simp: valid_path_polynomial_function dest: path_approx_polynomial_function)
  3285   done
  3286 then obtain p' where p': "polynomial_function p'"
  3287          "\<And>x. (p has_vector_derivative (p' x)) (at x)"
  3288   by (blast intro: has_vector_derivative_polynomial_function that elim: )
  3289 then have "bounded(p' ` {0..1})"
  3290   using continuous_on_polymonial_function
  3291   by (force simp: intro!: compact_imp_bounded compact_continuous_image)
  3292 then obtain L where L: "L>0" and nop': "\<And>x. x \<in> {0..1} \<Longrightarrow> norm (p' x) \<le> L"
  3293   by (force simp: bounded_pos)
  3294 { fix f B
  3295   assume f: "f holomorphic_on s"
  3296      and B: "\<And>z. z\<in>s \<Longrightarrow> cmod (f z) \<le> B"
  3297   then have "f contour_integrable_on p \<and> valid_path p"
  3298     using p s
  3299     by (blast intro: valid_path_polynomial_function contour_integrable_holomorphic_simple holomorphic_on_imp_continuous_on)
  3300   moreover have "\<And>x. x \<in> {0..1} \<Longrightarrow> cmod (vector_derivative p (at x)) * cmod (f (p x)) \<le> L * B"
  3301     apply (rule mult_mono)
  3302     apply (subst Derivative.vector_derivative_at; force intro: p' nop')
  3303     using L B p
  3304     apply (auto simp: path_image_def image_subset_iff)
  3305     done
  3306   ultimately have "cmod (contour_integral g f) \<le> L * B"
  3307     apply (simp add: pi [OF f])
  3308     apply (simp add: contour_integral_integral)
  3309     apply (rule order_trans [OF integral_norm_bound_integral])
  3310     apply (auto simp: mult.commute integral_norm_bound_integral contour_integrable_on [symmetric] norm_mult)
  3311     done
  3312 } then
  3313 show ?thesis
  3314   by (force simp: L contour_integral_integral)
  3315 qed
  3316 
  3317 text\<open>We can treat even non-rectifiable paths as having a "length" for bounds on analytic functions in open sets.\<close>
  3318 
  3319 subsection\<open>Winding Numbers\<close>
  3320 
  3321 definition winding_number:: "[real \<Rightarrow> complex, complex] \<Rightarrow> complex" where
  3322   "winding_number \<gamma> z \<equiv>
  3323     @n. \<forall>e > 0. \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
  3324                     pathstart p = pathstart \<gamma> \<and>
  3325                     pathfinish p = pathfinish \<gamma> \<and>
  3326                     (\<forall>t \<in> {0..1}. norm(\<gamma> t - p t) < e) \<and>
  3327                     contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3328 
  3329 lemma winding_number:
  3330   assumes "path \<gamma>" "z \<notin> path_image \<gamma>" "0 < e"
  3331     shows "\<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
  3332                pathstart p = pathstart \<gamma> \<and>
  3333                pathfinish p = pathfinish \<gamma> \<and>
  3334                (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
  3335                contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * winding_number \<gamma> z"
  3336 proof -
  3337   have "path_image \<gamma> \<subseteq> UNIV - {z}"
  3338     using assms by blast
  3339   then obtain d
  3340     where d: "d>0"
  3341       and pi_eq: "\<And>h1 h2. valid_path h1 \<and> valid_path h2 \<and>
  3342                     (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < d \<and> cmod (h2 t - \<gamma> t) < d) \<and>
  3343                     pathstart h2 = pathstart h1 \<and> pathfinish h2 = pathfinish h1 \<longrightarrow>
  3344                       path_image h1 \<subseteq> UNIV - {z} \<and> path_image h2 \<subseteq> UNIV - {z} \<and>
  3345                       (\<forall>f. f holomorphic_on UNIV - {z} \<longrightarrow> contour_integral h2 f = contour_integral h1 f)"
  3346     using contour_integral_nearby_ends [of "UNIV - {z}" \<gamma>] assms by (auto simp: open_delete)
  3347   then obtain h where h: "polynomial_function h \<and> pathstart h = pathstart \<gamma> \<and> pathfinish h = pathfinish \<gamma> \<and>
  3348                           (\<forall>t \<in> {0..1}. norm(h t - \<gamma> t) < d/2)"
  3349     using path_approx_polynomial_function [OF \<open>path \<gamma>\<close>, of "d/2"] d by auto
  3350   define nn where "nn = 1/(2* pi*\<i>) * contour_integral h (\<lambda>w. 1/(w - z))"
  3351   have "\<exists>n. \<forall>e > 0. \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
  3352                         pathstart p = pathstart \<gamma> \<and>  pathfinish p = pathfinish \<gamma> \<and>
  3353                         (\<forall>t \<in> {0..1}. norm(\<gamma> t - p t) < e) \<and>
  3354                         contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3355                     (is "\<exists>n. \<forall>e > 0. ?PP e n")
  3356     proof (rule_tac x=nn in exI, clarify)
  3357       fix e::real
  3358       assume e: "e>0"
  3359       obtain p where p: "polynomial_function p \<and>
  3360             pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and> (\<forall>t\<in>{0..1}. cmod (p t - \<gamma> t) < min e (d / 2))"
  3361         using path_approx_polynomial_function [OF \<open>path \<gamma>\<close>, of "min e (d/2)"] d \<open>0<e\<close> by auto
  3362       have "(\<lambda>w. 1 / (w - z)) holomorphic_on UNIV - {z}"
  3363         by (auto simp: intro!: holomorphic_intros)
  3364       then show "?PP e nn"
  3365         apply (rule_tac x=p in exI)
  3366         using pi_eq [of h p] h p d
  3367         apply (auto simp: valid_path_polynomial_function norm_minus_commute nn_def)
  3368         done
  3369     qed
  3370   then show ?thesis
  3371     unfolding winding_number_def
  3372     apply (rule someI2_ex)
  3373     apply (blast intro: \<open>0<e\<close>)
  3374     done
  3375 qed
  3376 
  3377 lemma winding_number_unique:
  3378   assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
  3379       and pi:
  3380         "\<And>e. e>0 \<Longrightarrow> \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
  3381                           pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and>
  3382                           (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
  3383                           contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3384    shows "winding_number \<gamma> z = n"
  3385 proof -
  3386   have "path_image \<gamma> \<subseteq> UNIV - {z}"
  3387     using assms by blast
  3388   then obtain e
  3389     where e: "e>0"
  3390       and pi_eq: "\<And>h1 h2 f. \<lbrakk>valid_path h1; valid_path h2;
  3391                     (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < e \<and> cmod (h2 t - \<gamma> t) < e);
  3392                     pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1; f holomorphic_on UNIV - {z}\<rbrakk> \<Longrightarrow>
  3393                     contour_integral h2 f = contour_integral h1 f"
  3394     using contour_integral_nearby_ends [of "UNIV - {z}" \<gamma>] assms  by (auto simp: open_delete)
  3395   obtain p where p:
  3396      "valid_path p \<and> z \<notin> path_image p \<and>
  3397       pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and>
  3398       (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
  3399       contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3400     using pi [OF e] by blast
  3401   obtain q where q:
  3402      "valid_path q \<and> z \<notin> path_image q \<and>
  3403       pathstart q = pathstart \<gamma> \<and> pathfinish q = pathfinish \<gamma> \<and>
  3404       (\<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"
  3405     using winding_number [OF \<gamma> e] by blast
  3406   have "2 * complex_of_real pi * \<i> * n = contour_integral p (\<lambda>w. 1 / (w - z))"
  3407     using p by auto
  3408   also have "... = contour_integral q (\<lambda>w. 1 / (w - z))"
  3409     apply (rule pi_eq)
  3410     using p q
  3411     by (auto simp: valid_path_polynomial_function norm_minus_commute intro!: holomorphic_intros)
  3412   also have "... = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z"
  3413     using q by auto
  3414   finally have "2 * complex_of_real pi * \<i> * n = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z" .
  3415   then show ?thesis
  3416     by simp
  3417 qed
  3418 
  3419 lemma winding_number_unique_loop:
  3420   assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
  3421       and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  3422       and pi:
  3423         "\<And>e. e>0 \<Longrightarrow> \<exists>p. valid_path p \<and> z \<notin> path_image p \<and>
  3424                            pathfinish p = pathstart p \<and>
  3425                            (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
  3426                            contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3427    shows "winding_number \<gamma> z = n"
  3428 proof -
  3429   have "path_image \<gamma> \<subseteq> UNIV - {z}"
  3430     using assms by blast
  3431   then obtain e
  3432     where e: "e>0"
  3433       and pi_eq: "\<And>h1 h2 f. \<lbrakk>valid_path h1; valid_path h2;
  3434                     (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < e \<and> cmod (h2 t - \<gamma> t) < e);
  3435                     pathfinish h1 = pathstart h1; pathfinish h2 = pathstart h2; f holomorphic_on UNIV - {z}\<rbrakk> \<Longrightarrow>
  3436                     contour_integral h2 f = contour_integral h1 f"
  3437     using contour_integral_nearby_loops [of "UNIV - {z}" \<gamma>] assms  by (auto simp: open_delete)
  3438   obtain p where p:
  3439      "valid_path p \<and> z \<notin> path_image p \<and>
  3440       pathfinish p = pathstart p \<and>
  3441       (\<forall>t \<in> {0..1}. norm (\<gamma> t - p t) < e) \<and>
  3442       contour_integral p (\<lambda>w. 1/(w - z)) = 2 * pi * \<i> * n"
  3443     using pi [OF e] by blast
  3444   obtain q where q:
  3445      "valid_path q \<and> z \<notin> path_image q \<and>
  3446       pathstart q = pathstart \<gamma> \<and> pathfinish q = pathfinish \<gamma> \<and>
  3447       (\<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"
  3448     using winding_number [OF \<gamma> e] by blast
  3449   have "2 * complex_of_real pi * \<i> * n = contour_integral p (\<lambda>w. 1 / (w - z))"
  3450     using p by auto
  3451   also have "... = contour_integral q (\<lambda>w. 1 / (w - z))"
  3452     apply (rule pi_eq)
  3453     using p q loop
  3454     by (auto simp: valid_path_polynomial_function norm_minus_commute intro!: holomorphic_intros)
  3455   also have "... = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z"
  3456     using q by auto
  3457   finally have "2 * complex_of_real pi * \<i> * n = 2 * complex_of_real pi * \<i> * winding_number \<gamma> z" .
  3458   then show ?thesis
  3459     by simp
  3460 qed
  3461 
  3462 lemma winding_number_valid_path:
  3463   assumes "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3464     shows "winding_number \<gamma> z = 1/(2*pi*\<i>) * contour_integral \<gamma> (\<lambda>w. 1/(w - z))"
  3465   using assms by (auto simp: valid_path_imp_path intro!: winding_number_unique)
  3466 
  3467 lemma has_contour_integral_winding_number:
  3468   assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3469     shows "((\<lambda>w. 1/(w - z)) has_contour_integral (2*pi*\<i>*winding_number \<gamma> z)) \<gamma>"
  3470 by (simp add: winding_number_valid_path has_contour_integral_integral contour_integrable_inversediff assms)
  3471 
  3472 lemma winding_number_trivial [simp]: "z \<noteq> a \<Longrightarrow> winding_number(linepath a a) z = 0"
  3473   by (simp add: winding_number_valid_path)
  3474 
  3475 lemma winding_number_subpath_trivial [simp]: "z \<noteq> g x \<Longrightarrow> winding_number (subpath x x g) z = 0"
  3476   by (simp add: path_image_subpath winding_number_valid_path)
  3477 
  3478 lemma winding_number_join:
  3479   assumes g1: "path g1" "z \<notin> path_image g1"
  3480       and g2: "path g2" "z \<notin> path_image g2"
  3481       and "pathfinish g1 = pathstart g2"
  3482     shows "winding_number(g1 +++ g2) z = winding_number g1 z + winding_number g2 z"
  3483   apply (rule winding_number_unique)
  3484   using assms apply (simp_all add: not_in_path_image_join)
  3485   apply (frule winding_number [OF g2])
  3486   apply (frule winding_number [OF g1], clarify)
  3487   apply (rename_tac p2 p1)
  3488   apply (rule_tac x="p1+++p2" in exI)
  3489   apply (simp add: not_in_path_image_join contour_integrable_inversediff algebra_simps)
  3490   apply (auto simp: joinpaths_def)
  3491   done
  3492 
  3493 lemma winding_number_reversepath:
  3494   assumes "path \<gamma>" "z \<notin> path_image \<gamma>"
  3495     shows "winding_number(reversepath \<gamma>) z = - (winding_number \<gamma> z)"
  3496   apply (rule winding_number_unique)
  3497   using assms
  3498   apply simp_all
  3499   apply (frule winding_number [OF assms], clarify)
  3500   apply (rule_tac x="reversepath p" in exI)
  3501   apply (simp add: contour_integral_reversepath contour_integrable_inversediff valid_path_imp_reverse)
  3502   apply (auto simp: reversepath_def)
  3503   done
  3504 
  3505 lemma winding_number_shiftpath:
  3506   assumes \<gamma>: "path \<gamma>" "z \<notin> path_image \<gamma>"
  3507       and "pathfinish \<gamma> = pathstart \<gamma>" "a \<in> {0..1}"
  3508     shows "winding_number(shiftpath a \<gamma>) z = winding_number \<gamma> z"
  3509   apply (rule winding_number_unique_loop)
  3510   using assms
  3511   apply (simp_all add: path_shiftpath path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath)
  3512   apply (frule winding_number [OF \<gamma>], clarify)
  3513   apply (rule_tac x="shiftpath a p" in exI)
  3514   apply (simp add: contour_integral_shiftpath path_image_shiftpath pathfinish_shiftpath pathstart_shiftpath valid_path_shiftpath)
  3515   apply (auto simp: shiftpath_def)
  3516   done
  3517 
  3518 lemma winding_number_split_linepath:
  3519   assumes "c \<in> closed_segment a b" "z \<notin> closed_segment a b"
  3520     shows "winding_number(linepath a b) z = winding_number(linepath a c) z + winding_number(linepath c b) z"
  3521 proof -
  3522   have "z \<notin> closed_segment a c" "z \<notin> closed_segment c b"
  3523     using assms  apply (meson convex_contains_segment convex_segment ends_in_segment(1) subsetCE)
  3524     using assms  by (meson convex_contains_segment convex_segment ends_in_segment(2) subsetCE)
  3525   then show ?thesis
  3526     using assms
  3527     by (simp add: winding_number_valid_path contour_integral_split_linepath [symmetric] continuous_on_inversediff field_simps)
  3528 qed
  3529 
  3530 lemma winding_number_cong:
  3531    "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> p t = q t) \<Longrightarrow> winding_number p z = winding_number q z"
  3532   by (simp add: winding_number_def pathstart_def pathfinish_def)
  3533 
  3534 lemma winding_number_offset: "winding_number p z = winding_number (\<lambda>w. p w - z) 0"
  3535   apply (simp add: winding_number_def contour_integral_integral path_image_def valid_path_def pathstart_def pathfinish_def)
  3536   apply (intro ext arg_cong [where f = Eps] arg_cong [where f = All] imp_cong refl, safe)
  3537   apply (rename_tac g)
  3538   apply (rule_tac x="\<lambda>t. g t - z" in exI)
  3539   apply (force simp: vector_derivative_def has_vector_derivative_diff_const piecewise_C1_differentiable_diff C1_differentiable_imp_piecewise)
  3540   apply (rename_tac g)
  3541   apply (rule_tac x="\<lambda>t. g t + z" in exI)
  3542   apply (simp add: piecewise_C1_differentiable_add vector_derivative_def has_vector_derivative_add_const C1_differentiable_imp_piecewise)
  3543   apply (force simp: algebra_simps)
  3544   done
  3545 
  3546 (* A combined theorem deducing several things piecewise.*)
  3547 lemma winding_number_join_pos_combined:
  3548      "\<lbrakk>valid_path \<gamma>1; z \<notin> path_image \<gamma>1; 0 < Re(winding_number \<gamma>1 z);
  3549        valid_path \<gamma>2; z \<notin> path_image \<gamma>2; 0 < Re(winding_number \<gamma>2 z); pathfinish \<gamma>1 = pathstart \<gamma>2\<rbrakk>
  3550       \<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)"
  3551   by (simp add: valid_path_join path_image_join winding_number_join valid_path_imp_path)
  3552 
  3553 
  3554 (* Useful sufficient conditions for the winding number to be positive etc.*)
  3555 
  3556 lemma Re_winding_number:
  3557     "\<lbrakk>valid_path \<gamma>; z \<notin> path_image \<gamma>\<rbrakk>
  3558      \<Longrightarrow> Re(winding_number \<gamma> z) = Im(contour_integral \<gamma> (\<lambda>w. 1/(w - z))) / (2*pi)"
  3559 by (simp add: winding_number_valid_path field_simps Re_divide power2_eq_square)
  3560 
  3561 lemma winding_number_pos_le:
  3562   assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3563       and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> 0 \<le> Im (vector_derivative \<gamma> (at x) * cnj(\<gamma> x - z))"
  3564     shows "0 \<le> Re(winding_number \<gamma> z)"
  3565 proof -
  3566   have *: "0 \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))" if x: "0 < x" "x < 1" for x
  3567     using ge by (simp add: Complex.Im_divide algebra_simps x)
  3568   show ?thesis
  3569     apply (simp add: Re_winding_number [OF \<gamma>] field_simps)
  3570     apply (rule has_integral_component_nonneg
  3571              [of \<i> "\<lambda>x. if x \<in> {0<..<1}
  3572                          then 1/(\<gamma> x - z) * vector_derivative \<gamma> (at x) else 0", simplified])
  3573       prefer 3 apply (force simp: *)
  3574      apply (simp add: Basis_complex_def)
  3575     apply (rule has_integral_spike_interior [of 0 1 _ "\<lambda>x. 1/(\<gamma> x - z) * vector_derivative \<gamma> (at x)"])
  3576     apply simp
  3577     apply (simp only: box_real)
  3578     apply (subst has_contour_integral [symmetric])
  3579     using \<gamma>
  3580     apply (simp add: contour_integrable_inversediff has_contour_integral_integral)
  3581     done
  3582 qed
  3583 
  3584 lemma winding_number_pos_lt_lemma:
  3585   assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3586       and e: "0 < e"
  3587       and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> e \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
  3588     shows "0 < Re(winding_number \<gamma> z)"
  3589 proof -
  3590   have "e \<le> Im (contour_integral \<gamma> (\<lambda>w. 1 / (w - z)))"
  3591     apply (rule has_integral_component_le
  3592              [of \<i> "\<lambda>x. \<i>*e" "\<i>*e" "{0..1}"
  3593                     "\<lambda>x. if x \<in> {0<..<1} then 1/(\<gamma> x - z) * vector_derivative \<gamma> (at x) else \<i>*e"
  3594                     "contour_integral \<gamma> (\<lambda>w. 1/(w - z))", simplified])
  3595     using e
  3596     apply (simp_all add: Basis_complex_def)
  3597     using has_integral_const_real [of _ 0 1] apply force
  3598     apply (rule has_integral_spike_interior [of 0 1 _ "\<lambda>x. 1/(\<gamma> x - z) * vector_derivative \<gamma> (at x)", simplified box_real])
  3599     apply simp
  3600     apply (subst has_contour_integral [symmetric])
  3601     using \<gamma>
  3602     apply (simp_all add: contour_integrable_inversediff has_contour_integral_integral ge)
  3603     done
  3604   with e show ?thesis
  3605     by (simp add: Re_winding_number [OF \<gamma>] field_simps)
  3606 qed
  3607 
  3608 lemma winding_number_pos_lt:
  3609   assumes \<gamma>: "valid_path \<gamma>" "z \<notin> path_image \<gamma>"
  3610       and e: "0 < e"
  3611       and ge: "\<And>x. \<lbrakk>0 < x; x < 1\<rbrakk> \<Longrightarrow> e \<le> Im (vector_derivative \<gamma> (at x) * cnj(\<gamma> x - z))"
  3612     shows "0 < Re (winding_number \<gamma> z)"
  3613 proof -
  3614   have bm: "bounded ((\<lambda>w. w - z) ` (path_image \<gamma>))"
  3615     using bounded_translation [of _ "-z"] \<gamma> by (simp add: bounded_valid_path_image)
  3616   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"
  3617     using bounded_pos [THEN iffD1, OF bm] by blast
  3618   { fix x::real  assume x: "0 < x" "x < 1"
  3619     then have B2: "cmod (\<gamma> x - z)^2 \<le> B^2" using Bno [of "\<gamma> x - z"]
  3620       by (simp add: path_image_def power2_eq_square mult_mono')
  3621     with x have "\<gamma> x \<noteq> z" using \<gamma>
  3622       using path_image_def by fastforce
  3623     then have "e / B\<^sup>2 \<le> Im (vector_derivative \<gamma> (at x) * cnj (\<gamma> x - z)) / (cmod (\<gamma> x - z))\<^sup>2"
  3624       using B ge [OF x] B2 e
  3625       apply (rule_tac y="e / (cmod (\<gamma> x - z))\<^sup>2" in order_trans)
  3626       apply (auto simp: divide_left_mono divide_right_mono)
  3627       done
  3628     then have "e / B\<^sup>2 \<le> Im (vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
  3629       by (simp add: Im_divide_Reals complex_div_cnj [of _ "\<gamma> x - z" for x] del: complex_cnj_diff times_complex.sel)
  3630   } note * = this
  3631   show ?thesis
  3632     using e B by (simp add: * winding_number_pos_lt_lemma [OF \<gamma>, of "e/B^2"])
  3633 qed
  3634 
  3635 subsection\<open>The winding number is an integer\<close>
  3636 
  3637 text\<open>Proof from the book Complex Analysis by Lars V. Ahlfors, Chapter 4, section 2.1,
  3638      Also on page 134 of Serge Lang's book with the name title, etc.\<close>
  3639 
  3640 lemma exp_fg:
  3641   fixes z::complex
  3642   assumes g: "(g has_vector_derivative g') (at x within s)"
  3643       and f: "(f has_vector_derivative (g' / (g x - z))) (at x within s)"
  3644       and z: "g x \<noteq> z"
  3645     shows "((\<lambda>x. exp(-f x) * (g x - z)) has_vector_derivative 0) (at x within s)"
  3646 proof -
  3647   have *: "(exp o (\<lambda>x. (- f x)) has_vector_derivative - (g' / (g x - z)) * exp (- f x)) (at x within s)"
  3648     using assms unfolding has_vector_derivative_def scaleR_conv_of_real
  3649     by (auto intro!: derivative_eq_intros)
  3650   show ?thesis
  3651     apply (rule has_vector_derivative_eq_rhs)
  3652     apply (rule bounded_bilinear.has_vector_derivative [OF bounded_bilinear_mult])
  3653     using z
  3654     apply (auto simp: intro!: derivative_eq_intros * [unfolded o_def] g)
  3655     done
  3656 qed
  3657 
  3658 lemma winding_number_exp_integral:
  3659   fixes z::complex
  3660   assumes \<gamma>: "\<gamma> piecewise_C1_differentiable_on {a..b}"
  3661       and ab: "a \<le> b"
  3662       and z: "z \<notin> \<gamma> ` {a..b}"
  3663     shows "(\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)) integrable_on {a..b}"
  3664           (is "?thesis1")
  3665           "exp (- (integral {a..b} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))) * (\<gamma> b - z) = \<gamma> a - z"
  3666           (is "?thesis2")
  3667 proof -
  3668   let ?D\<gamma> = "\<lambda>x. vector_derivative \<gamma> (at x)"
  3669   have [simp]: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> \<gamma> x \<noteq> z"
  3670     using z by force
  3671   have cong: "continuous_on {a..b} \<gamma>"
  3672     using \<gamma> by (simp add: piecewise_C1_differentiable_on_def)
  3673   obtain k where fink: "finite k" and g_C1_diff: "\<gamma> C1_differentiable_on ({a..b} - k)"
  3674     using \<gamma> by (force simp: piecewise_C1_differentiable_on_def)
  3675   have o: "open ({a<..<b} - k)"
  3676     using \<open>finite k\<close> by (simp add: finite_imp_closed open_Diff)
  3677   moreover have "{a<..<b} - k \<subseteq> {a..b} - k"
  3678     by force
  3679   ultimately have g_diff_at: "\<And>x. \<lbrakk>x \<notin> k; x \<in> {a<..<b}\<rbrakk> \<Longrightarrow> \<gamma> differentiable at x"
  3680     by (metis Diff_iff differentiable_on_subset C1_diff_imp_diff [OF g_C1_diff] differentiable_on_def at_within_open)
  3681   { fix w
  3682     assume "w \<noteq> z"
  3683     have "continuous_on (ball w (cmod (w - z))) (\<lambda>w. 1 / (w - z))"
  3684       by (auto simp: dist_norm intro!: continuous_intros)
  3685     moreover have "\<And>x. cmod (w - x) < cmod (w - z) \<Longrightarrow> \<exists>f'. ((\<lambda>w. 1 / (w - z)) has_field_derivative f') (at x)"
  3686       by (auto simp: intro!: derivative_eq_intros)
  3687     ultimately have "\<exists>h. \<forall>y. norm(y - w) < norm(w - z) \<longrightarrow> (h has_field_derivative 1/(y - z)) (at y)"
  3688       using holomorphic_convex_primitive [of "ball w (norm(w - z))" "{}" "\<lambda>w. 1/(w - z)"]
  3689       by (simp add: field_differentiable_def Ball_def dist_norm at_within_open_NO_MATCH norm_minus_commute)
  3690   }
  3691   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)"
  3692     by meson
  3693   have exy: "\<exists>y. ((\<lambda>x. inverse (\<gamma> x - z) * ?D\<gamma> x) has_integral y) {a..b}"
  3694     unfolding integrable_on_def [symmetric]
  3695     apply (rule contour_integral_local_primitive_any [OF piecewise_C1_imp_differentiable [OF \<gamma>], of "-{z}"])
  3696     apply (rename_tac w)
  3697     apply (rule_tac x="norm(w - z)" in exI)
  3698     apply (simp_all add: inverse_eq_divide)
  3699     apply (metis has_field_derivative_at_within h)
  3700     done
  3701   have vg_int: "(\<lambda>x. ?D\<gamma> x / (\<gamma> x - z)) integrable_on {a..b}"
  3702     unfolding box_real [symmetric] divide_inverse_commute
  3703     by (auto intro!: exy integrable_subinterval simp add: integrable_on_def ab)
  3704   with ab show ?thesis1
  3705     by (simp add: divide_inverse_commute integral_def integrable_on_def)
  3706   { fix t
  3707     assume t: "t \<in> {a..b}"
  3708     have cball: "continuous_on (ball (\<gamma> t) (dist (\<gamma> t) z)) (\<lambda>x. inverse (x - z))"
  3709         using z by (auto intro!: continuous_intros simp: dist_norm)
  3710     have icd: "\<And>x. cmod (\<gamma> t - x) < cmod (\<gamma> t - z) \<Longrightarrow> (\<lambda>w. inverse (w - z)) field_differentiable at x"
  3711       unfolding field_differentiable_def by (force simp: intro!: derivative_eq_intros)
  3712     obtain h where h: "\<And>x. cmod (\<gamma> t - x) < cmod (\<gamma> t - z) \<Longrightarrow>
  3713                        (h has_field_derivative inverse (x - z)) (at x within {y. cmod (\<gamma> t - y) < cmod (\<gamma> t - z)})"
  3714       using holomorphic_convex_primitive [where f = "\<lambda>w. inverse(w - z)", OF convex_ball finite.emptyI cball icd]
  3715       by simp (auto simp: ball_def dist_norm that)
  3716     { fix x D
  3717       assume x: "x \<notin> k" "a < x" "x < b"
  3718       then have "x \<in> interior ({a..b} - k)"
  3719         using open_subset_interior [OF o] by fastforce
  3720       then have con: "isCont (\<lambda>x. ?D\<gamma> x) x"
  3721         using g_C1_diff x by (auto simp: C1_differentiable_on_eq intro: continuous_on_interior)
  3722       then have con_vd: "continuous (at x within {a..b}) (\<lambda>x. ?D\<gamma> x)"
  3723         by (rule continuous_at_imp_continuous_within)
  3724       have gdx: "\<gamma> differentiable at x"
  3725         using x by (simp add: g_diff_at)
  3726       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))
  3727           (at x within {a..b})"
  3728         using x gdx t
  3729         apply (clarsimp simp add: differentiable_iff_scaleR)
  3730         apply (rule exp_fg [unfolded has_vector_derivative_def, simplified], blast intro: has_derivative_at_within)
  3731         apply (simp_all add: has_vector_derivative_def [symmetric])
  3732         apply (rule has_vector_derivative_eq_rhs [OF integral_has_vector_derivative_continuous_at])
  3733         apply (rule con_vd continuous_intros cong vg_int | simp add: continuous_at_imp_continuous_within has_vector_derivative_continuous vector_derivative_at)+
  3734         done
  3735       } note * = this
  3736     have "exp (- (integral {a..t} (\<lambda>x. ?D\<gamma> x / (\<gamma> x - z)))) * (\<gamma> t - z) =\<gamma> a - z"
  3737       apply (rule has_derivative_zero_unique_strong_interval [of "{a,b} \<union> k" a b])
  3738       using t
  3739       apply (auto intro!: * continuous_intros fink cong indefinite_integral_continuous [OF vg_int]  simp add: ab)+
  3740       done
  3741    }
  3742   with ab show ?thesis2
  3743     by (simp add: divide_inverse_commute integral_def)
  3744 qed
  3745 
  3746 corollary winding_number_exp_2pi:
  3747     "\<lbrakk>path p; z \<notin> path_image p\<rbrakk>
  3748      \<Longrightarrow> pathfinish p - z = exp (2 * pi * \<i> * winding_number p z) * (pathstart p - z)"
  3749 using winding_number [of p z 1] unfolding valid_path_def path_image_def pathstart_def pathfinish_def
  3750   by (force dest: winding_number_exp_integral(2) [of _ 0 1 z] simp: field_simps contour_integral_integral exp_minus)
  3751 
  3752 
  3753 subsection\<open>The version with complex integers and equality\<close>
  3754 
  3755 lemma integer_winding_number_eq:
  3756   assumes \<gamma>: "path \<gamma>" and z: "z \<notin> path_image \<gamma>"
  3757   shows "winding_number \<gamma> z \<in> \<int> \<longleftrightarrow> pathfinish \<gamma> = pathstart \<gamma>"
  3758 proof -
  3759   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)"
  3760       by (simp add: field_simps) algebra
  3761   obtain p where p: "valid_path p" "z \<notin> path_image p"
  3762                     "pathstart p = pathstart \<gamma>" "pathfinish p = pathfinish \<gamma>"
  3763                     "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
  3764     using winding_number [OF assms, of 1] by auto
  3765   have [simp]: "(winding_number \<gamma> z \<in> \<int>) = (exp (contour_integral p (\<lambda>w. 1 / (w - z))) = 1)"
  3766       using p by (simp add: exp_eq_1 complex_is_Int_iff)
  3767   have "winding_number p z \<in> \<int> \<longleftrightarrow> pathfinish p = pathstart p"
  3768     using p z
  3769     apply (simp add: winding_number_valid_path valid_path_def path_image_def pathstart_def pathfinish_def)
  3770     using winding_number_exp_integral(2) [of p 0 1 z]
  3771     apply (simp add: field_simps contour_integral_integral exp_minus)
  3772     apply (rule *)
  3773     apply (auto simp: path_image_def field_simps)
  3774     done
  3775   then show ?thesis using p
  3776     by (auto simp: winding_number_valid_path)
  3777 qed
  3778 
  3779 theorem integer_winding_number:
  3780   "\<lbrakk>path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> path_image \<gamma>\<rbrakk> \<Longrightarrow> winding_number \<gamma> z \<in> \<int>"
  3781 by (metis integer_winding_number_eq)
  3782 
  3783 
  3784 text\<open>If the winding number's magnitude is at least one, then the path must contain points in every direction.*)
  3785    We can thus bound the winding number of a path that doesn't intersect a given ray. \<close>
  3786 
  3787 lemma winding_number_pos_meets:
  3788   fixes z::complex
  3789   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and 1: "Re (winding_number \<gamma> z) \<ge> 1"
  3790       and w: "w \<noteq> z"
  3791   shows "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image \<gamma>"
  3792 proof -
  3793   have [simp]: "\<And>x. 0 \<le> x \<Longrightarrow> x \<le> 1 \<Longrightarrow> \<gamma> x \<noteq> z"
  3794     using z by (auto simp: path_image_def)
  3795   have [simp]: "z \<notin> \<gamma> ` {0..1}"
  3796     using path_image_def z by auto
  3797   have gpd: "\<gamma> piecewise_C1_differentiable_on {0..1}"
  3798     using \<gamma> valid_path_def by blast
  3799   define r where "r = (w - z) / (\<gamma> 0 - z)"
  3800   have [simp]: "r \<noteq> 0"
  3801     using w z by (auto simp: r_def)
  3802   have "Arg r \<le> 2*pi"
  3803     by (simp add: Arg less_eq_real_def)
  3804   also have "... \<le> Im (integral {0..1} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))"
  3805     using 1
  3806     apply (simp add: winding_number_valid_path [OF \<gamma> z] contour_integral_integral)
  3807     apply (simp add: Complex.Re_divide field_simps power2_eq_square)
  3808     done
  3809   finally have "Arg r \<le> Im (integral {0..1} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z)))" .
  3810   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"
  3811     apply (simp add:)
  3812     apply (rule IVT')
  3813     apply (simp_all add: Arg_ge_0)
  3814     apply (intro continuous_intros indefinite_integral_continuous winding_number_exp_integral [OF gpd]; simp)
  3815     done
  3816   then obtain t where t:     "t \<in> {0..1}"
  3817                   and eqArg: "Im (integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x)/(\<gamma> x - z))) = Arg r"
  3818     by blast
  3819   define i where "i = integral {0..t} (\<lambda>x. vector_derivative \<gamma> (at x) / (\<gamma> x - z))"
  3820   have iArg: "Arg r = Im i"
  3821     using eqArg by (simp add: i_def)
  3822   have gpdt: "\<gamma> piecewise_C1_differentiable_on {0..t}"
  3823     by (metis atLeastAtMost_iff atLeastatMost_subset_iff order_refl piecewise_C1_differentiable_on_subset gpd t)
  3824   have "exp (- i) * (\<gamma> t - z) = \<gamma> 0 - z"
  3825     unfolding i_def
  3826     apply (rule winding_number_exp_integral [OF gpdt])
  3827     using t z unfolding path_image_def
  3828     apply force+
  3829     done
  3830   then have *: "\<gamma> t - z = exp i * (\<gamma> 0 - z)"
  3831     by (simp add: exp_minus field_simps)
  3832   then have "(w - z) = r * (\<gamma> 0 - z)"
  3833     by (simp add: r_def)
  3834   then have "z + complex_of_real (exp (Re i)) * (w - z) / complex_of_real (cmod r) = \<gamma> t"
  3835     apply (simp add:)
  3836     apply (subst Complex_Transcendental.Arg_eq [of r])
  3837     apply (simp add: iArg)
  3838     using *
  3839     apply (simp add: exp_eq_polar field_simps)
  3840     done
  3841   with t show ?thesis
  3842     by (rule_tac x="exp(Re i) / norm r" in exI) (auto simp: path_image_def)
  3843 qed
  3844 
  3845 lemma winding_number_big_meets:
  3846   fixes z::complex
  3847   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "\<bar>Re (winding_number \<gamma> z)\<bar> \<ge> 1"
  3848       and w: "w \<noteq> z"
  3849   shows "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image \<gamma>"
  3850 proof -
  3851   { assume "Re (winding_number \<gamma> z) \<le> - 1"
  3852     then have "Re (winding_number (reversepath \<gamma>) z) \<ge> 1"
  3853       by (simp add: \<gamma> valid_path_imp_path winding_number_reversepath z)
  3854     moreover have "valid_path (reversepath \<gamma>)"
  3855       using \<gamma> valid_path_imp_reverse by auto
  3856     moreover have "z \<notin> path_image (reversepath \<gamma>)"
  3857       by (simp add: z)
  3858     ultimately have "\<exists>a::real. 0 < a \<and> z + a*(w - z) \<in> path_image (reversepath \<gamma>)"
  3859       using winding_number_pos_meets w by blast
  3860     then have ?thesis
  3861       by simp
  3862   }
  3863   then show ?thesis
  3864     using assms
  3865     by (simp add: abs_if winding_number_pos_meets split: if_split_asm)
  3866 qed
  3867 
  3868 lemma winding_number_less_1:
  3869   fixes z::complex
  3870   shows
  3871   "\<lbrakk>valid_path \<gamma>; z \<notin> path_image \<gamma>; w \<noteq> z;
  3872     \<And>a::real. 0 < a \<Longrightarrow> z + a*(w - z) \<notin> path_image \<gamma>\<rbrakk>
  3873    \<Longrightarrow> Re(winding_number \<gamma> z) < 1"
  3874    by (auto simp: not_less dest: winding_number_big_meets)
  3875 
  3876 text\<open>One way of proving that WN=1 for a loop.\<close>
  3877 lemma winding_number_eq_1:
  3878   fixes z::complex
  3879   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  3880       and 0: "0 < Re(winding_number \<gamma> z)" and 2: "Re(winding_number \<gamma> z) < 2"
  3881   shows "winding_number \<gamma> z = 1"
  3882 proof -
  3883   have "winding_number \<gamma> z \<in> Ints"
  3884     by (simp add: \<gamma> integer_winding_number loop valid_path_imp_path z)
  3885   then show ?thesis
  3886     using 0 2 by (auto simp: Ints_def)
  3887 qed
  3888 
  3889 
  3890 subsection\<open>Continuity of winding number and invariance on connected sets.\<close>
  3891 
  3892 lemma continuous_at_winding_number:
  3893   fixes z::complex
  3894   assumes \<gamma>: "path \<gamma>" and z: "z \<notin> path_image \<gamma>"
  3895   shows "continuous (at z) (winding_number \<gamma>)"
  3896 proof -
  3897   obtain e where "e>0" and cbg: "cball z e \<subseteq> - path_image \<gamma>"
  3898     using open_contains_cball [of "- path_image \<gamma>"]  z
  3899     by (force simp: closed_def [symmetric] closed_path_image [OF \<gamma>])
  3900   then have ppag: "path_image \<gamma> \<subseteq> - cball z (e/2)"
  3901     by (force simp: cball_def dist_norm)
  3902   have oc: "open (- cball z (e / 2))"
  3903     by (simp add: closed_def [symmetric])
  3904   obtain d where "d>0" and pi_eq:
  3905     "\<And>h1 h2. \<lbrakk>valid_path h1; valid_path h2;
  3906               (\<forall>t\<in>{0..1}. cmod (h1 t - \<gamma> t) < d \<and> cmod (h2 t - \<gamma> t) < d);
  3907               pathstart h2 = pathstart h1; pathfinish h2 = pathfinish h1\<rbrakk>
  3908              \<Longrightarrow>
  3909                path_image h1 \<subseteq> - cball z (e / 2) \<and>
  3910                path_image h2 \<subseteq> - cball z (e / 2) \<and>
  3911                (\<forall>f. f holomorphic_on - cball z (e / 2) \<longrightarrow> contour_integral h2 f = contour_integral h1 f)"
  3912     using contour_integral_nearby_ends [OF oc \<gamma> ppag] by metis
  3913   obtain p where p: "valid_path p" "z \<notin> path_image p"
  3914                     "pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma>"
  3915               and pg: "\<And>t. t\<in>{0..1} \<Longrightarrow> cmod (\<gamma> t - p t) < min d e / 2"
  3916               and pi: "contour_integral p (\<lambda>x. 1 / (x - z)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> z"
  3917     using winding_number [OF \<gamma> z, of "min d e / 2"] \<open>d>0\<close> \<open>e>0\<close> by auto
  3918   { fix w
  3919     assume d2: "cmod (w - z) < d/2" and e2: "cmod (w - z) < e/2"
  3920     then have wnotp: "w \<notin> path_image p"
  3921       using cbg \<open>d>0\<close> \<open>e>0\<close>
  3922       apply (simp add: path_image_def cball_def dist_norm, clarify)
  3923       apply (frule pg)
  3924       apply (drule_tac c="\<gamma> x" in subsetD)
  3925       apply (auto simp: less_eq_real_def norm_minus_commute norm_triangle_half_l)
  3926       done
  3927     have wnotg: "w \<notin> path_image \<gamma>"
  3928       using cbg e2 \<open>e>0\<close> by (force simp: dist_norm norm_minus_commute)
  3929     { fix k::real
  3930       assume k: "k>0"
  3931       then obtain q where q: "valid_path q" "w \<notin> path_image q"
  3932                              "pathstart q = pathstart \<gamma> \<and> pathfinish q = pathfinish \<gamma>"
  3933                     and qg: "\<And>t. t \<in> {0..1} \<Longrightarrow> cmod (\<gamma> t - q t) < min k (min d e) / 2"
  3934                     and qi: "contour_integral q (\<lambda>u. 1 / (u - w)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> w"
  3935         using winding_number [OF \<gamma> wnotg, of "min k (min d e) / 2"] \<open>d>0\<close> \<open>e>0\<close> k
  3936         by (force simp: min_divide_distrib_right)
  3937       have "contour_integral p (\<lambda>u. 1 / (u - w)) = contour_integral q (\<lambda>u. 1 / (u - w))"
  3938         apply (rule pi_eq [OF \<open>valid_path q\<close> \<open>valid_path p\<close>, THEN conjunct2, THEN conjunct2, rule_format])
  3939         apply (frule pg)
  3940         apply (frule qg)
  3941         using p q \<open>d>0\<close> e2
  3942         apply (auto simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
  3943         done
  3944       then have "contour_integral p (\<lambda>x. 1 / (x - w)) = complex_of_real (2 * pi) * \<i> * winding_number \<gamma> w"
  3945         by (simp add: pi qi)
  3946     } note pip = this
  3947     have "path p"
  3948       using p by (simp add: valid_path_imp_path)
  3949     then have "winding_number p w = winding_number \<gamma> w"
  3950       apply (rule winding_number_unique [OF _ wnotp])
  3951       apply (rule_tac x=p in exI)
  3952       apply (simp add: p wnotp min_divide_distrib_right pip)
  3953       done
  3954   } note wnwn = this
  3955   obtain pe where "pe>0" and cbp: "cball z (3 / 4 * pe) \<subseteq> - path_image p"
  3956     using p open_contains_cball [of "- path_image p"]
  3957     by (force simp: closed_def [symmetric] closed_path_image [OF valid_path_imp_path])
  3958   obtain L
  3959     where "L>0"
  3960       and L: "\<And>f B. \<lbrakk>f holomorphic_on - cball z (3 / 4 * pe);
  3961                       \<forall>z \<in> - cball z (3 / 4 * pe). cmod (f z) \<le> B\<rbrakk> \<Longrightarrow>
  3962                       cmod (contour_integral p f) \<le> L * B"
  3963     using contour_integral_bound_exists [of "- cball z (3/4*pe)" p] cbp \<open>valid_path p\<close> by force
  3964   { fix e::real and w::complex
  3965     assume e: "0 < e" and w: "cmod (w - z) < pe/4" "cmod (w - z) < e * pe\<^sup>2 / (8 * L)"
  3966     then have [simp]: "w \<notin> path_image p"
  3967       using cbp p(2) \<open>0 < pe\<close>
  3968       by (force simp: dist_norm norm_minus_commute path_image_def cball_def)
  3969     have [simp]: "contour_integral p (\<lambda>x. 1/(x - w)) - contour_integral p (\<lambda>x. 1/(x - z)) =
  3970                   contour_integral p (\<lambda>x. 1/(x - w) - 1/(x - z))"
  3971       by (simp add: p contour_integrable_inversediff contour_integral_diff)
  3972     { fix x
  3973       assume pe: "3/4 * pe < cmod (z - x)"
  3974       have "cmod (w - x) < pe/4 + cmod (z - x)"
  3975         by (meson add_less_cancel_right norm_diff_triangle_le order_refl order_trans_rules(21) w(1))
  3976       then have wx: "cmod (w - x) < 4/3 * cmod (z - x)" using pe by simp
  3977       have "cmod (z - x) \<le> cmod (z - w) + cmod (w - x)"
  3978         using norm_diff_triangle_le by blast
  3979       also have "... < pe/4 + cmod (w - x)"
  3980         using w by (simp add: norm_minus_commute)
  3981       finally have "pe/2 < cmod (w - x)"
  3982         using pe by auto
  3983       then have "(pe/2)^2 < cmod (w - x) ^ 2"
  3984         apply (rule power_strict_mono)
  3985         using \<open>pe>0\<close> by auto
  3986       then have pe2: "pe^2 < 4 * cmod (w - x) ^ 2"
  3987         by (simp add: power_divide)
  3988       have "8 * L * cmod (w - z) < e * pe\<^sup>2"
  3989         using w \<open>L>0\<close> by (simp add: field_simps)
  3990       also have "... < e * 4 * cmod (w - x) * cmod (w - x)"
  3991         using pe2 \<open>e>0\<close> by (simp add: power2_eq_square)
  3992       also have "... < e * 4 * cmod (w - x) * (4/3 * cmod (z - x))"
  3993         using wx
  3994         apply (rule mult_strict_left_mono)
  3995         using pe2 e not_less_iff_gr_or_eq by fastforce
  3996       finally have "L * cmod (w - z) < 2/3 * e * cmod (w - x) * cmod (z - x)"
  3997         by simp
  3998       also have "... \<le> e * cmod (w - x) * cmod (z - x)"
  3999          using e by simp
  4000       finally have Lwz: "L * cmod (w - z) < e * cmod (w - x) * cmod (z - x)" .
  4001       have "L * cmod (1 / (x - w) - 1 / (x - z)) \<le> e"
  4002         apply (cases "x=z \<or> x=w")
  4003         using pe \<open>pe>0\<close> w \<open>L>0\<close>
  4004         apply (force simp: norm_minus_commute)
  4005         using wx w(2) \<open>L>0\<close> pe pe2 Lwz
  4006         apply (auto simp: divide_simps mult_less_0_iff norm_minus_commute norm_divide norm_mult power2_eq_square)
  4007         done
  4008     } note L_cmod_le = this
  4009     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)"
  4010       apply (rule L)
  4011       using \<open>pe>0\<close> w
  4012       apply (force simp: dist_norm norm_minus_commute intro!: holomorphic_intros)
  4013       using \<open>pe>0\<close> w \<open>L>0\<close>
  4014       apply (auto simp: cball_def dist_norm field_simps L_cmod_le  simp del: less_divide_eq_numeral1 le_divide_eq_numeral1)
  4015       done
  4016     have "cmod (contour_integral p (\<lambda>x. 1 / (x - w)) - contour_integral p (\<lambda>x. 1 / (x - z))) < 2*e"
  4017       apply (simp add:)
  4018       apply (rule le_less_trans [OF *])
  4019       using \<open>L>0\<close> e
  4020       apply (force simp: field_simps)
  4021       done
  4022     then have "cmod (winding_number p w - winding_number p z) < e"
  4023       using pi_ge_two e
  4024       by (force simp: winding_number_valid_path p field_simps norm_divide norm_mult intro: less_le_trans)
  4025   } note cmod_wn_diff = this
  4026   then have "isCont (winding_number p) z"
  4027     apply (simp add: continuous_at_eps_delta, clarify)
  4028     apply (rule_tac x="min (pe/4) (e/2*pe^2/L/4)" in exI)
  4029     using \<open>pe>0\<close> \<open>L>0\<close>
  4030     apply (simp add: dist_norm cmod_wn_diff)
  4031     done
  4032   then show ?thesis
  4033     apply (rule continuous_transform_within [where d = "min d e / 2"])
  4034     apply (auto simp: \<open>d>0\<close> \<open>e>0\<close> dist_norm wnwn)
  4035     done
  4036 qed
  4037 
  4038 corollary continuous_on_winding_number:
  4039     "path \<gamma> \<Longrightarrow> continuous_on (- path_image \<gamma>) (\<lambda>w. winding_number \<gamma> w)"
  4040   by (simp add: continuous_at_imp_continuous_on continuous_at_winding_number)
  4041 
  4042 
  4043 subsection\<open>The winding number is constant on a connected region\<close>
  4044 
  4045 lemma winding_number_constant:
  4046   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and cs: "connected s" and sg: "s \<inter> path_image \<gamma> = {}"
  4047   obtains k where "\<And>z. z \<in> s \<Longrightarrow> winding_number \<gamma> z = k"
  4048 proof -
  4049   have *: "1 \<le> cmod (winding_number \<gamma> y - winding_number \<gamma> z)"
  4050       if ne: "winding_number \<gamma> y \<noteq> winding_number \<gamma> z" and "y \<in> s" "z \<in> s" for y z
  4051   proof -
  4052     have "winding_number \<gamma> y \<in> \<int>"  "winding_number \<gamma> z \<in>  \<int>"
  4053       using that integer_winding_number [OF \<gamma> loop] sg \<open>y \<in> s\<close> by auto
  4054     with ne show ?thesis
  4055       by (auto simp: Ints_def of_int_diff [symmetric] simp del: of_int_diff)
  4056   qed
  4057   have cont: "continuous_on s (\<lambda>w. winding_number \<gamma> w)"
  4058     using continuous_on_winding_number [OF \<gamma>] sg
  4059     by (meson continuous_on_subset disjoint_eq_subset_Compl)
  4060   show ?thesis
  4061     apply (rule continuous_discrete_range_constant [OF cs cont])
  4062     using "*" zero_less_one apply blast
  4063     by (simp add: that)
  4064 qed
  4065 
  4066 lemma winding_number_eq:
  4067      "\<lbrakk>path \<gamma>; pathfinish \<gamma> = pathstart \<gamma>; w \<in> s; z \<in> s; connected s; s \<inter> path_image \<gamma> = {}\<rbrakk>
  4068       \<Longrightarrow> winding_number \<gamma> w = winding_number \<gamma> z"
  4069   using winding_number_constant by blast
  4070 
  4071 lemma open_winding_number_levelsets:
  4072   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4073     shows "open {z. z \<notin> path_image \<gamma> \<and> winding_number \<gamma> z = k}"
  4074 proof -
  4075   have op: "open (- path_image \<gamma>)"
  4076     by (simp add: closed_path_image \<gamma> open_Compl)
  4077   { fix z assume z: "z \<notin> path_image \<gamma>" and k: "k = winding_number \<gamma> z"
  4078     obtain e where e: "e>0" "ball z e \<subseteq> - path_image \<gamma>"
  4079       using open_contains_ball [of "- path_image \<gamma>"] op z
  4080       by blast
  4081     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"
  4082       apply (rule_tac x=e in exI)
  4083       using e apply (simp add: dist_norm ball_def norm_minus_commute)
  4084       apply (auto simp: dist_norm norm_minus_commute intro!: winding_number_eq [OF assms, where s = "ball z e"])
  4085       done
  4086   } then
  4087   show ?thesis
  4088     by (auto simp: open_dist)
  4089 qed
  4090 
  4091 subsection\<open>Winding number is zero "outside" a curve, in various senses\<close>
  4092 
  4093 lemma winding_number_zero_in_outside:
  4094   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>" and z: "z \<in> outside (path_image \<gamma>)"
  4095     shows "winding_number \<gamma> z = 0"
  4096 proof -
  4097   obtain B::real where "0 < B" and B: "path_image \<gamma> \<subseteq> ball 0 B"
  4098     using bounded_subset_ballD [OF bounded_path_image [OF \<gamma>]] by auto
  4099   obtain w::complex where w: "w \<notin> ball 0 (B + 1)"
  4100     by (metis abs_of_nonneg le_less less_irrefl mem_ball_0 norm_of_real)
  4101   have "- ball 0 (B + 1) \<subseteq> outside (path_image \<gamma>)"
  4102     apply (rule outside_subset_convex)
  4103     using B subset_ball by auto
  4104   then have wout: "w \<in> outside (path_image \<gamma>)"
  4105     using w by blast
  4106   moreover obtain k where "\<And>z. z \<in> outside (path_image \<gamma>) \<Longrightarrow> winding_number \<gamma> z = k"
  4107     using winding_number_constant [OF \<gamma> loop, of "outside(path_image \<gamma>)"] connected_outside
  4108     by (metis DIM_complex bounded_path_image dual_order.refl \<gamma> outside_no_overlap)
  4109   ultimately have "winding_number \<gamma> z = winding_number \<gamma> w"
  4110     using z by blast
  4111   also have "... = 0"
  4112   proof -
  4113     have wnot: "w \<notin> path_image \<gamma>"  using wout by (simp add: outside_def)
  4114     { fix e::real assume "0<e"
  4115       obtain p where p: "polynomial_function p" "pathstart p = pathstart \<gamma>" "pathfinish p = pathfinish \<gamma>"
  4116                  and pg1: "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> cmod (p t - \<gamma> t) < 1)"
  4117                  and pge: "(\<And>t. \<lbrakk>0 \<le> t; t \<le> 1\<rbrakk> \<Longrightarrow> cmod (p t - \<gamma> t) < e)"
  4118         using path_approx_polynomial_function [OF \<gamma>, of "min 1 e"] \<open>e>0\<close> by force
  4119       have pip: "path_image p \<subseteq> ball 0 (B + 1)"
  4120         using B
  4121         apply (clarsimp simp add: path_image_def dist_norm ball_def)
  4122         apply (frule (1) pg1)
  4123         apply (fastforce dest: norm_add_less)
  4124         done
  4125       then have "w \<notin> path_image p"  using w by blast
  4126       then have "\<exists>p. valid_path p \<and> w \<notin> path_image p \<and>
  4127                      pathstart p = pathstart \<gamma> \<and> pathfinish p = pathfinish \<gamma> \<and>
  4128                      (\<forall>t\<in>{0..1}. cmod (\<gamma> t - p t) < e) \<and> contour_integral p (\<lambda>wa. 1 / (wa - w)) = 0"
  4129         apply (rule_tac x=p in exI)
  4130         apply (simp add: p valid_path_polynomial_function)
  4131         apply (intro conjI)
  4132         using pge apply (simp add: norm_minus_commute)
  4133         apply (rule contour_integral_unique [OF Cauchy_theorem_convex_simple [OF _ convex_ball [of 0 "B+1"]]])
  4134         apply (rule holomorphic_intros | simp add: dist_norm)+
  4135         using mem_ball_0 w apply blast
  4136         using p apply (simp_all add: valid_path_polynomial_function loop pip)
  4137         done
  4138     }
  4139     then show ?thesis
  4140       by (auto intro: winding_number_unique [OF \<gamma>] simp add: wnot)
  4141   qed
  4142   finally show ?thesis .
  4143 qed
  4144 
  4145 lemma winding_number_zero_outside:
  4146     "\<lbrakk>path \<gamma>; convex s; pathfinish \<gamma> = pathstart \<gamma>; z \<notin> s; path_image \<gamma> \<subseteq> s\<rbrakk> \<Longrightarrow> winding_number \<gamma> z = 0"
  4147   by (meson convex_in_outside outside_mono subsetCE winding_number_zero_in_outside)
  4148 
  4149 lemma winding_number_zero_at_infinity:
  4150   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4151     shows "\<exists>B. \<forall>z. B \<le> norm z \<longrightarrow> winding_number \<gamma> z = 0"
  4152 proof -
  4153   obtain B::real where "0 < B" and B: "path_image \<gamma> \<subseteq> ball 0 B"
  4154     using bounded_subset_ballD [OF bounded_path_image [OF \<gamma>]] by auto
  4155   then show ?thesis
  4156     apply (rule_tac x="B+1" in exI, clarify)
  4157     apply (rule winding_number_zero_outside [OF \<gamma> convex_cball [of 0 B] loop])
  4158     apply (meson less_add_one mem_cball_0 not_le order_trans)
  4159     using ball_subset_cball by blast
  4160 qed
  4161 
  4162 lemma winding_number_zero_point:
  4163     "\<lbrakk>path \<gamma>; convex s; pathfinish \<gamma> = pathstart \<gamma>; open s; path_image \<gamma> \<subseteq> s\<rbrakk>
  4164      \<Longrightarrow> \<exists>z. z \<in> s \<and> winding_number \<gamma> z = 0"
  4165   using outside_compact_in_open [of "path_image \<gamma>" s] path_image_nonempty winding_number_zero_in_outside
  4166   by (fastforce simp add: compact_path_image)
  4167 
  4168 
  4169 text\<open>If a path winds round a set, it winds rounds its inside.\<close>
  4170 lemma winding_number_around_inside:
  4171   assumes \<gamma>: "path \<gamma>" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4172       and cls: "closed s" and cos: "connected s" and s_disj: "s \<inter> path_image \<gamma> = {}"
  4173       and z: "z \<in> s" and wn_nz: "winding_number \<gamma> z \<noteq> 0" and w: "w \<in> s \<union> inside s"
  4174     shows "winding_number \<gamma> w = winding_number \<gamma> z"
  4175 proof -
  4176   have ssb: "s \<subseteq> inside(path_image \<gamma>)"
  4177   proof
  4178     fix x :: complex
  4179     assume "x \<in> s"
  4180     hence "x \<notin> path_image \<gamma>"
  4181       by (meson disjoint_iff_not_equal s_disj)
  4182     thus "x \<in> inside (path_image \<gamma>)"
  4183       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)
  4184 qed
  4185   show ?thesis
  4186     apply (rule winding_number_eq [OF \<gamma> loop w])
  4187     using z apply blast
  4188     apply (simp add: cls connected_with_inside cos)
  4189     apply (simp add: Int_Un_distrib2 s_disj, safe)
  4190     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)
  4191  qed
  4192 
  4193 
  4194 text\<open>Bounding a WN by 1/2 for a path and point in opposite halfspaces.\<close>
  4195 lemma winding_number_subpath_continuous:
  4196   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>"
  4197     shows "continuous_on {0..1} (\<lambda>x. winding_number(subpath 0 x \<gamma>) z)"
  4198 proof -
  4199   have *: "integral {0..x} (\<lambda>t. vector_derivative \<gamma> (at t) / (\<gamma> t - z)) / (2 * of_real pi * \<i>) =
  4200          winding_number (subpath 0 x \<gamma>) z"
  4201          if x: "0 \<le> x" "x \<le> 1" for x
  4202   proof -
  4203     have "integral {0..x} (\<lambda>t. vector_derivative \<gamma> (at t) / (\<gamma> t - z)) / (2 * of_real pi * \<i>) =
  4204           1 / (2*pi*\<i>) * contour_integral (subpath 0 x \<gamma>) (\<lambda>w. 1/(w - z))"
  4205       using assms x
  4206       apply (simp add: contour_integral_subcontour_integral [OF contour_integrable_inversediff])
  4207       done
  4208     also have "... = winding_number (subpath 0 x \<gamma>) z"
  4209       apply (subst winding_number_valid_path)
  4210       using assms x
  4211       apply (simp_all add: path_image_subpath valid_path_subpath)
  4212       by (force simp: path_image_def)
  4213     finally show ?thesis .
  4214   qed
  4215   show ?thesis
  4216     apply (rule continuous_on_eq
  4217                  [where f = "\<lambda>x. 1 / (2*pi*\<i>) *
  4218                                  integral {0..x} (\<lambda>t. 1/(\<gamma> t - z) * vector_derivative \<gamma> (at t))"])
  4219     apply (rule continuous_intros)+
  4220     apply (rule indefinite_integral_continuous)
  4221     apply (rule contour_integrable_inversediff [OF assms, unfolded contour_integrable_on])
  4222       using assms
  4223     apply (simp add: *)
  4224     done
  4225 qed
  4226 
  4227 lemma winding_number_ivt_pos:
  4228     assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "0 \<le> w" "w \<le> Re(winding_number \<gamma> z)"
  4229       shows "\<exists>t \<in> {0..1}. Re(winding_number(subpath 0 t \<gamma>) z) = w"
  4230   apply (rule ivt_increasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right])
  4231   apply (simp add:)
  4232   apply (rule winding_number_subpath_continuous [OF \<gamma> z])
  4233   using assms
  4234   apply (auto simp: path_image_def image_def)
  4235   done
  4236 
  4237 lemma winding_number_ivt_neg:
  4238     assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>" and "Re(winding_number \<gamma> z) \<le> w" "w \<le> 0"
  4239       shows "\<exists>t \<in> {0..1}. Re(winding_number(subpath 0 t \<gamma>) z) = w"
  4240   apply (rule ivt_decreasing_component_on_1 [of 0 1, where ?k = "1::complex", simplified complex_inner_1_right])
  4241   apply (simp add:)
  4242   apply (rule winding_number_subpath_continuous [OF \<gamma> z])
  4243   using assms
  4244   apply (auto simp: path_image_def image_def)
  4245   done
  4246 
  4247 lemma winding_number_ivt_abs:
  4248     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>"
  4249       shows "\<exists>t \<in> {0..1}. \<bar>Re (winding_number (subpath 0 t \<gamma>) z)\<bar> = w"
  4250   using assms winding_number_ivt_pos [of \<gamma> z w] winding_number_ivt_neg [of \<gamma> z "-w"]
  4251   by force
  4252 
  4253 lemma winding_number_lt_half_lemma:
  4254   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}"
  4255     shows "Re(winding_number \<gamma> z) < 1/2"
  4256 proof -
  4257   { assume "Re(winding_number \<gamma> z) \<ge> 1/2"
  4258     then obtain t::real where t: "0 \<le> t" "t \<le> 1" and sub12: "Re (winding_number (subpath 0 t \<gamma>) z) = 1/2"
  4259       using winding_number_ivt_pos [OF \<gamma> z, of "1/2"] by auto
  4260     have gt: "\<gamma> t - z = - (of_real (exp (- (2 * pi * Im (winding_number (subpath 0 t \<gamma>) z)))) * (\<gamma> 0 - z))"
  4261       using winding_number_exp_2pi [of "subpath 0 t \<gamma>" z]
  4262       apply (simp add: t \<gamma> valid_path_imp_path)
  4263       using closed_segment_eq_real_ivl path_image_def t z by (fastforce simp: path_image_subpath Euler sub12)
  4264     have "b < a \<bullet> \<gamma> 0"
  4265     proof -
  4266       have "\<gamma> 0 \<in> {c. b < a \<bullet> c}"
  4267         by (metis (no_types) pag atLeastAtMost_iff image_subset_iff order_refl path_image_def zero_le_one)
  4268       thus ?thesis
  4269         by blast
  4270     qed
  4271     moreover have "b < a \<bullet> \<gamma> t"
  4272     proof -
  4273       have "\<gamma> t \<in> {c. b < a \<bullet> c}"
  4274         by (metis (no_types) pag atLeastAtMost_iff image_subset_iff path_image_def t)
  4275       thus ?thesis
  4276         by blast
  4277     qed
  4278     ultimately have "0 < a \<bullet> (\<gamma> 0 - z)" "0 < a \<bullet> (\<gamma> t - z)" using az
  4279       by (simp add: inner_diff_right)+
  4280     then have False
  4281       by (simp add: gt inner_mult_right mult_less_0_iff)
  4282   }
  4283   then show ?thesis by force
  4284 qed
  4285 
  4286 lemma winding_number_lt_half:
  4287   assumes "valid_path \<gamma>" "a \<bullet> z \<le> b" "path_image \<gamma> \<subseteq> {w. a \<bullet> w > b}"
  4288     shows "\<bar>Re (winding_number \<gamma> z)\<bar> < 1/2"
  4289 proof -
  4290   have "z \<notin> path_image \<gamma>" using assms by auto
  4291   with assms show ?thesis
  4292     apply (simp add: winding_number_lt_half_lemma abs_if del: less_divide_eq_numeral1)
  4293     apply (metis complex_inner_1_right winding_number_lt_half_lemma [OF valid_path_imp_reverse, of \<gamma> z a b]
  4294                  winding_number_reversepath valid_path_imp_path inner_minus_left path_image_reversepath)
  4295     done
  4296 qed
  4297 
  4298 lemma winding_number_le_half:
  4299   assumes \<gamma>: "valid_path \<gamma>" and z: "z \<notin> path_image \<gamma>"
  4300       and anz: "a \<noteq> 0" and azb: "a \<bullet> z \<le> b" and pag: "path_image \<gamma> \<subseteq> {w. a \<bullet> w \<ge> b}"
  4301     shows "\<bar>Re (winding_number \<gamma> z)\<bar> \<le> 1/2"
  4302 proof -
  4303   { assume wnz_12: "\<bar>Re (winding_number \<gamma> z)\<bar> > 1/2"
  4304     have "isCont (winding_number \<gamma>) z"
  4305       by (metis continuous_at_winding_number valid_path_imp_path \<gamma> z)
  4306     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"
  4307       using continuous_at_eps_delta wnz_12 diff_gt_0_iff_gt by blast
  4308     define z' where "z' = z - (d / (2 * cmod a)) *\<^sub>R a"
  4309     have *: "a \<bullet> z' \<le> b - d / 3 * cmod a"
  4310       unfolding z'_def inner_mult_right' divide_inverse
  4311       apply (simp add: divide_simps algebra_simps dot_square_norm power2_eq_square anz)
  4312       apply (metis \<open>0 < d\<close> add_increasing azb less_eq_real_def mult_nonneg_nonneg mult_right_mono norm_ge_zero norm_numeral)
  4313       done
  4314     have "cmod (winding_number \<gamma> z' - winding_number \<gamma> z) < \<bar>Re (winding_number \<gamma> z)\<bar> - 1/2"
  4315       using d [of z'] anz \<open>d>0\<close> by (simp add: dist_norm z'_def)
  4316     then have "1/2 < \<bar>Re (winding_number \<gamma> z)\<bar> - cmod (winding_number \<gamma> z' - winding_number \<gamma> z)"
  4317       by simp
  4318     then have "1/2 < \<bar>Re (winding_number \<gamma> z)\<bar> - \<bar>Re (winding_number \<gamma> z') - Re (winding_number \<gamma> z)\<bar>"
  4319       using abs_Re_le_cmod [of "winding_number \<gamma> z' - winding_number \<gamma> z"] by simp
  4320     then have wnz_12': "\<bar>Re (winding_number \<gamma> z')\<bar> > 1/2"
  4321       by linarith
  4322     moreover have "\<bar>Re (winding_number \<gamma> z')\<bar> < 1/2"
  4323       apply (rule winding_number_lt_half [OF \<gamma> *])
  4324       using azb \<open>d>0\<close> pag
  4325       apply (auto simp: add_strict_increasing anz divide_simps algebra_simps dest!: subsetD)
  4326       done
  4327     ultimately have False
  4328       by simp
  4329   }
  4330   then show ?thesis by force
  4331 qed
  4332 
  4333 lemma winding_number_lt_half_linepath: "z \<notin> closed_segment a b \<Longrightarrow> \<bar>Re (winding_number (linepath a b) z)\<bar> < 1/2"
  4334   using separating_hyperplane_closed_point [of "closed_segment a b" z]
  4335   apply auto
  4336   apply (simp add: closed_segment_def)
  4337   apply (drule less_imp_le)
  4338   apply (frule winding_number_lt_half [OF valid_path_linepath [of a b]])
  4339   apply (auto simp: segment)
  4340   done
  4341 
  4342 
  4343 text\<open> Positivity of WN for a linepath.\<close>
  4344 lemma winding_number_linepath_pos_lt:
  4345     assumes "0 < Im ((b - a) * cnj (b - z))"
  4346       shows "0 < Re(winding_number(linepath a b) z)"
  4347 proof -
  4348   have z: "z \<notin> path_image (linepath a b)"
  4349     using assms
  4350     by (simp add: closed_segment_def) (force simp: algebra_simps)
  4351   show ?thesis
  4352     apply (rule winding_number_pos_lt [OF valid_path_linepath z assms])
  4353     apply (simp add: linepath_def algebra_simps)
  4354     done
  4355 qed
  4356 
  4357 
  4358 subsection\<open>Cauchy's integral formula, again for a convex enclosing set.\<close>
  4359 
  4360 lemma Cauchy_integral_formula_weak:
  4361     assumes s: "convex s" and "finite k" and conf: "continuous_on s f"
  4362         and fcd: "(\<And>x. x \<in> interior s - k \<Longrightarrow> f field_differentiable at x)"
  4363         and z: "z \<in> interior s - k" and vpg: "valid_path \<gamma>"
  4364         and pasz: "path_image \<gamma> \<subseteq> s - {z}" and loop: "pathfinish \<gamma> = pathstart \<gamma>"
  4365       shows "((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
  4366 proof -
  4367   obtain f' where f': "(f has_field_derivative f') (at z)"
  4368     using fcd [OF z] by (auto simp: field_differentiable_def)
  4369   have pas: "path_image \<gamma> \<subseteq> s" and znotin: "z \<notin> path_image \<gamma>" using pasz by blast+
  4370   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
  4371   proof (cases "x = z")
  4372     case True then show ?thesis
  4373       apply (simp add: continuous_within)
  4374       apply (rule Lim_transform_away_within [of _ "z+1" _ "\<lambda>w::complex. (f w - f z)/(w - z)"])
  4375       using has_field_derivative_at_within DERIV_within_iff f'
  4376       apply (fastforce simp add:)+
  4377       done
  4378   next
  4379     case False
  4380     then have dxz: "dist x z > 0" by auto
  4381     have cf: "continuous (at x within s) f"
  4382       using conf continuous_on_eq_continuous_within that by blast
  4383     have "continuous (at x within s) (\<lambda>w. (f w - f z) / (w - z))"
  4384       by (rule cf continuous_intros | simp add: False)+
  4385     then show ?thesis
  4386       apply (rule continuous_transform_within [OF _ dxz that, of "\<lambda>w::complex. (f w - f z)/(w - z)"])
  4387       apply (force simp: dist_commute)
  4388       done
  4389   qed
  4390   have fink': "finite (insert z k)" using \<open>finite k\<close> by blast
  4391   have *: "((\<lambda>w. if w = z then f' else (f w - f z) / (w - z)) has_contour_integral 0) \<gamma>"
  4392     apply (rule Cauchy_theorem_convex [OF _ s fink' _ vpg pas loop])
  4393     using c apply (force simp: continuous_on_eq_continuous_within)
  4394     apply (rename_tac w)
  4395     apply (rule_tac d="dist w z" and f = "\<lambda>w. (f w - f z)/(w - z)" in field_differentiable_transform_within)
  4396     apply (simp_all add: dist_pos_lt dist_commute)
  4397     apply (metis less_irrefl)
  4398     apply (rule derivative_intros fcd | simp)+
  4399     done
  4400   show ?thesis
  4401     apply (rule has_contour_integral_eq)
  4402     using znotin has_contour_integral_add [OF has_contour_integral_lmul [OF has_contour_integral_winding_number [OF vpg znotin], of "f z"] *]
  4403     apply (auto simp: mult_ac divide_simps)
  4404     done
  4405 qed
  4406 
  4407 theorem Cauchy_integral_formula_convex_simple:
  4408     "\<lbrakk>convex s; f holomorphic_on s; z \<in> interior s; valid_path \<gamma>; path_image \<gamma> \<subseteq> s - {z};
  4409       pathfinish \<gamma> = pathstart \<gamma>\<rbrakk>
  4410      \<Longrightarrow> ((\<lambda>w. f w / (w - z)) has_contour_integral (2*pi * \<i> * winding_number \<gamma> z * f z)) \<gamma>"
  4411   apply (rule Cauchy_integral_formula_weak [where k = "{}"])
  4412   using holomorphic_on_imp_continuous_on
  4413   by auto (metis at_within_interior holomorphic_on_def interiorE subsetCE)
  4414 
  4415 
  4416 subsection\<open>Homotopy forms of Cauchy's theorem\<close>
  4417 
  4418 proposition Cauchy_theorem_homotopic:
  4419     assumes hom: "if atends then homotopic_paths s g h else homotopic_loops s g h"
  4420         and "open s" and f: "f holomorphic_on s"
  4421         and vpg: "valid_path g" and vph: "valid_path h"
  4422     shows "contour_integral g f = contour_integral h f"
  4423 proof -
  4424   have pathsf: "linked_paths atends g h"
  4425     using hom  by (auto simp: linked_paths_def homotopic_paths_imp_pathstart homotopic_paths_imp_pathfinish homotopic_loops_imp_loop)
  4426   obtain k :: "real \<times> real \<Rightarrow> complex"
  4427     where contk: "continuous_on ({0..1} \<times> {0..1}) k"
  4428       and ks: "k ` ({0..1} \<times> {0..1}) \<subseteq> s"
  4429       and k [simp]: "\<forall>x. k (0, x) = g x" "\<forall>x. k (1, x) = h x"
  4430       and ksf: "\<forall>t\<in>{0..1}. linked_paths atends g (\<lambda>x. k (t, x))"
  4431       using hom pathsf by (auto simp: linked_paths_def homotopic_paths_def homotopic_loops_def homotopic_with_def split: if_split_asm)
  4432   have ucontk: "uniformly_continuous_on ({0..1} \<times> {0..1}) k"
  4433     by (blast intro: compact_Times compact_uniformly_continuous [OF contk])
  4434   { fix t::real assume t: "t \<in> {0..1}"
  4435     have pak: "path (k o (\<lambda>u. (t, u)))"
  4436       unfolding path_def
  4437       apply (rule continuous_intros continuous_on_subset [OF contk])+
  4438       using t by force
  4439     have pik: "path_image (k \<circ> Pair t) \<subseteq> s"
  4440       using ks t by (auto simp: path_image_def)
  4441     obtain e where "e>0" and e:
  4442          "\<And>g h. \<lbrakk>valid_path g; valid_path h;
  4443                   \<forall>u\<in>{0..1}. cmod (g u - (k \<circ> Pair t) u) < e \<and> cmod (h u - (k \<circ> Pair t) u) < e;
  4444                   linked_paths atends g h\<rbrakk>
  4445                  \<Longrightarrow> contour_integral h f = contour_integral g f"
  4446       using contour_integral_nearby [OF \<open>open s\<close> pak pik, of atends] f by metis
  4447     obtain d where "d>0" and d:
  4448         "\<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"
  4449       by (rule uniformly_continuous_onE [OF ucontk, of "e/4"]) (auto simp: dist_norm \<open>e>0\<close>)
  4450     { fix t1 t2
  4451       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"
  4452       have no2: "\<And>g1 k1 kt. \<lbrakk>norm(g1 - k1) < e/4; norm(k1 - kt) < e/4\<rbrakk> \<Longrightarrow> norm(g1 - kt) < e"
  4453         using \<open>e > 0\<close>
  4454         apply (rule_tac y = k1 in norm_triangle_half_l)
  4455         apply (auto simp: norm_minus_commute intro: order_less_trans)
  4456         done
  4457       have "\<exists>d>0. \<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
  4458                           (\<forall>u\<in>{0..1}. cmod (g1 u - k (t1, u)) < d \<and> cmod (g2 u - k (t2, u)) < d) \<and>
  4459                           linked_paths atends g1 g2 \<longrightarrow>
  4460                           contour_integral g2 f = contour_integral g1 f"
  4461         apply (rule_tac x="e/4" in exI)
  4462         using t t1 t2 ltd \<open>e > 0\<close>
  4463         apply (auto intro!: e simp: d no2 simp del: less_divide_eq_numeral1)
  4464         done
  4465     }
  4466     then have "\<exists>e. 0 < e \<and>
  4467               (\<forall>t1 t2. t1 \<in> {0..1} \<and> t2 \<in> {0..1} \<and> \<bar>t1 - t\<bar> < e \<and> \<bar>t2 - t\<bar> < e
  4468                 \<longrightarrow> (\<exists>d. 0 < d \<and>
  4469                      (\<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
  4470                        (\<forall>u \<in> {0..1}.
  4471                           norm(g1 u - k((t1,u))) < d \<and> norm(g2 u - k((t2,u))) < d) \<and>
  4472                           linked_paths atends g1 g2
  4473                           \<longrightarrow> contour_integral g2 f = contour_integral g1 f)))"
  4474       by (rule_tac x=d in exI) (simp add: \<open>d > 0\<close>)
  4475   }
  4476   then obtain ee where ee:
  4477        "\<And>t. t \<in> {0..1} \<Longrightarrow> ee t > 0 \<and>
  4478           (\<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
  4479             \<longrightarrow> (\<exists>d. 0 < d \<and>
  4480                  (\<forall>g1 g2. valid_path g1 \<and> valid_path g2 \<and>
  4481                    (\<forall>u \<in> {0..1}.
  4482                       norm(g1 u - k((t1,u))) < d \<and> norm(g2 u - k((t2,u))) < d) \<and>
  4483                       linked_paths atends g1 g2
  4484                       \<longrightarrow> contour_integral g2 f = contour_integral g1 f)))"
  4485     by metis
  4486   note ee_rule = ee [THEN conjunct2, rule_format]
  4487   define C where "C = (\<lambda>t. ball t (ee t / 3)) ` {0..1}"
  4488   obtain C' where C': "C' \<subseteq> C" "finite C'" and C'01: "{0..1} \<subseteq> \<Union>C'"
  4489   proof (rule compactE [OF compact_interval])
  4490     show "{0..1} \<subseteq> \<Union>C"
  4491       using ee [THEN conjunct1] by (auto simp: C_def dist_norm)
  4492   qed (use C_def in auto)
  4493   define kk where "kk = {t \<in> {0..1}. ball t (ee t / 3) \<in> C'}"
  4494   have kk01: "kk \<subseteq> {0..1}" by (auto simp: kk_def)
  4495   define e where "e = Min (ee ` kk)"
  4496   have C'_eq: "C' = (\<lambda>t. ball t (ee t / 3)) ` kk"
  4497     using C' by (auto simp: kk_def C_def)
  4498   have ee_pos[simp]: "\<And>t. t \<in> {0..1} \<Longrightarrow> ee t > 0"
  4499     by (simp add: kk_def ee)
  4500   moreover have "finite kk"
  4501     using \<open>finite C'\<close> kk01 by (force simp: C'_eq inj_on_def ball_eq_ball_iff dest: ee_pos finite_imageD)
  4502   moreover have "kk \<noteq> {}" using \<open>{0..1} \<subseteq> \<Union>C'\<close> C'_eq by force
  4503   ultimately have "e > 0"
  4504     using finite_less_Inf_iff [of "ee ` kk" 0] kk01 by (force simp: e_def)
  4505   then obtain N::nat where "N > 0" and N: "1/N < e/3"
  4506     by (meson divide_pos_pos nat_approx_posE zero_less_Suc zero_less_numeral)
  4507   have e_le_ee: "\<And>i. i \<in> kk \<Longrightarrow> e \<le> ee i"
  4508     using \<open>finite kk\<close> by (simp add: e_def Min_le_iff [of "ee ` kk"])
  4509   have plus: "\<exists>t \<in> kk. x \<in> ball t (ee t / 3)" if "x \<in> {0..1}" for x
  4510     using C' subsetD [OF C'01 that]  unfolding C'_eq by blast
  4511   have [OF order_refl]:
  4512       "\<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
  4513                         \<longrightarrow> contour_integral j f = contour_integral g f)"
  4514        if "n \<le> N" for n
  4515   using that
  4516   proof (induct n)
  4517     case 0 show ?case using ee_rule [of 0 0 0]
  4518       apply clarsimp
  4519       apply (rule_tac x=d in exI, safe)
  4520       by (metis diff_self vpg norm_zero)
  4521   next
  4522     case (Suc n)
  4523     then have N01: "n/N \<in> {0..1}" "(Suc n)/N \<in> {0..1}"  by auto
  4524     then obtain t where t: "t \<in> kk" "n/N \<in> ball t (ee t / 3)"
  4525       using plus [of "n/N"] by blast
  4526     then have nN_less: "\<bar>n/N - t\<bar> < ee t"
  4527       by (simp add: dist_norm del: less_divide_eq_numeral1)
  4528     have n'N_less: "\<bar>real (Suc n) / real N - t\<bar> < ee t"
  4529       using t N \<open>N > 0\<close> e_le_ee [of t]
  4530       by (simp add: dist_norm add_divide_distrib abs_diff_less_iff del: less_divide_eq_numeral1) (simp add: field_simps)
  4531     have t01: "t \<in> {0..1}" using \<open>kk \<subseteq> {0..1}\<close> \<open>t \<in> kk\<close> by blast
  4532     obtain d1 where "d1 > 0" and d1:
  4533         "\<And>g1 g2. \<lbrakk>valid_path g1; valid_path g2;
  4534                    \<forall>u\<in>{0..1}. cmod (g1 u - k (n/N, u)) < d1 \<and> cmod (g2 u - k ((Suc n) / N, u)) < d1;
  4535                    linked_paths atends g1 g2\<rbrakk>
  4536                    \<Longrightarrow> contour_integral g2 f = contour_integral g1 f"
  4537       using ee [THEN conjunct2, rule_format, OF t01 N01 nN_less n'N_less] by fastforce
  4538     have "n \<le> N" using Suc.prems by auto
  4539     with Suc.hyps
  4540     obtain d2 where "d2 > 0"
  4541       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>
  4542                      \<Longrightarrow> contour_integral j f = contour_integral g f"
  4543         by auto
  4544     have "continuous_on {0..1} (k o (\<lambda>u. (n/N, u)))"
  4545       apply (rule continuous_intros continuous_on_subset [OF contk])+
  4546       using N01 by auto
  4547     then have pkn: "path (\<lambda>u. k (n/N, u))"
  4548       by (simp add: path_def)
  4549     have min12: "min d1 d2 > 0" by (simp add: \<open>0 < d1\<close> \<open>0 < d2\<close>)
  4550     obtain p where "polynomial_function p"
  4551         and psf: "pathstart p = pathstart (\<lambda>u. k (n/N, u))"
  4552                  "pathfinish p = pathfinish (\<lambda>u. k (n/N, u))"
  4553         and pk_le:  "\<And>t. t\<in>{0..1} \<Longrightarrow> cmod (p t - k (n/N, t)) < min d1 d2"
  4554       using path_approx_polynomial_function [OF pkn min12] by blast
  4555     then have vpp: "valid_path p" using valid_path_polynomial_function by blast
  4556     have lpa: "linked_paths atends g p"
  4557       by (metis (mono_tags, lifting) N01(1) ksf linked_paths_def pathfinish_def pathstart_def psf)
  4558     show ?case
  4559       apply (rule_tac x="min d1 d2" in exI)
  4560       apply (simp add: \<open>0 < d1\<close> \<open>0 < d2\<close>, clarify)
  4561       apply (rule_tac s="contour_integral p f" in trans)
  4562       using pk_le N01(1) ksf pathfinish_def pathstart_def
  4563       apply (force intro!: vpp d1 simp add: linked_paths_def psf ksf)
  4564       using pk_le N01 apply (force intro!: vpp d2 lpa simp add: linked_paths_def psf ksf)
  4565       done
  4566   qed
  4567   then obtain d where "0 < d"
  4568                        "\<And>j. valid_path j \<and> (\<forall>u \<in> {0..1}. norm(j u - k (1,u)) < d) \<and>
  4569                             linked_paths atends g j
  4570                             \<Longrightarrow> contour_integral j f = contour_integral g f"
  4571     using \<open>N>0\<close> by auto
  4572   then have "linked_paths atends g h \<Longrightarrow> contour_integral h f = contour_integral g f"
  4573     using \<open>N>0\<close> vph by fastforce
  4574   then show ?thesis
  4575     by (simp add: pathsf)
  4576 qed
  4577 
  4578 proposition Cauchy_theorem_homotopic_paths:
  4579     assumes hom: "homotopic_paths s g h"
  4580         and "open s" and f: "f holomorphic_on s"
  4581         and vpg: "valid_path g" and vph: "valid_path h"
  4582     shows "contour_integral g f = contour_integral h f"
  4583   using Cauchy_theorem_homotopic [of True s g h] assms by simp
  4584 
  4585 proposition Cauchy_theorem_homotopic_loops:
  4586     assumes hom: "homotopic_loops s g h"
  4587         and "open s" and f: "f holomorphic_on s"
  4588         and vpg: "valid_path g" and vph: "valid_path h"
  4589     shows "contour_integral g f = contour_integral h f"
  4590   using Cauchy_theorem_homotopic [of False s g h] assms by simp
  4591 
  4592 lemma has_contour_integral_newpath:
  4593     "\<lbrakk>(f has_contour_integral y) h; f contour_integrable_on g; contour_integral g f = contour_integral h f\<rbrakk>
  4594      \<Longrightarrow> (f has_contour_integral y) g"
  4595   using has_contour_integral_integral contour_integral_unique by auto
  4596 
  4597 lemma Cauchy_theorem_null_homotopic:
  4598      "\<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"
  4599   apply (rule has_contour_integral_newpath [where h = "linepath a a"], simp)
  4600   using contour_integrable_holomorphic_simple
  4601     apply (blast dest: holomorphic_on_imp_continuous_on homotopic_loops_imp_subset)
  4602   by (simp add: Cauchy_theorem_homotopic_loops)
  4603 
  4604 
  4605 
  4606 subsection\<open>More winding number properties\<close>
  4607 
  4608 text\<open>including the fact that it's +-1 inside a simple closed curve.\<close>
  4609 
  4610 lemma winding_number_homotopic_paths:
  4611     assumes "homotopic_paths (-{z}) g h"
  4612       shows "winding_number g z = winding_number h z"
  4613 proof -
  4614   have "path g" "path h" using homotopic_paths_imp_path [OF assms] by auto
  4615   moreover have pag: "z \<notin> path_image g" and pah: "z \<notin> path_image h"
  4616     using homotopic_paths_imp_subset [OF assms] by auto
  4617   ultimately obtain d e where "d > 0" "e > 0"
  4618       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>
  4619             \<Longrightarrow> homotopic_paths (-{z}) g p"
  4620       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>
  4621             \<Longrightarrow> homotopic_paths (-{z}) h q"
  4622     using homotopic_nearby_paths [of g "-{z}"] homotopic_nearby_paths [of h "-{z}"] by force
  4623   obtain p where p:
  4624        "valid_path p" "z \<notin> path_image p"
  4625        "pathstart p = pathstart g" "pathfinish p = pathfinish g"
  4626        and gp_less:"\<forall>t\<in>{0..1}. cmod (g t - p t) < d"
  4627        and pap: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number g z"
  4628     using winding_number [OF \<open>path g\<close> pag \<open>0 < d\<close>] by blast
  4629   obtain q where q:
  4630        "valid_path q" "z \<notin> path_image q"
  4631        "pathstart q = pathstart h" "pathfinish q = pathfinish h"
  4632        and hq_less: "\<forall>t\<in>{0..1}. cmod (h t - q t) < e"
  4633        and paq:  "contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number h z"
  4634     using winding_number [OF \<open>path h\<close> pah \<open>0 < e\<close>] by blast
  4635   have gp: "homotopic_paths (- {z}) g p"
  4636     by (simp add: d p valid_path_imp_path norm_minus_commute gp_less)
  4637   have hq: "homotopic_paths (- {z}) h q"
  4638     by (simp add: e q valid_path_imp_path norm_minus_commute hq_less)
  4639   have "contour_integral p (\<lambda>w. 1/(w - z)) = contour_integral q (\<lambda>w. 1/(w - z))"
  4640     apply (rule Cauchy_theorem_homotopic_paths [of "-{z}"])
  4641     apply (blast intro: homotopic_paths_trans homotopic_paths_sym gp hq assms)
  4642     apply (auto intro!: holomorphic_intros simp: p q)
  4643     done
  4644   then show ?thesis
  4645     by (simp add: pap paq)
  4646 qed
  4647 
  4648 lemma winding_number_homotopic_loops:
  4649     assumes "homotopic_loops (-{z}) g h"
  4650       shows "winding_number g z = winding_number h z"
  4651 proof -
  4652   have "path g" "path h" using homotopic_loops_imp_path [OF assms] by auto
  4653   moreover have pag: "z \<notin> path_image g" and pah: "z \<notin> path_image h"
  4654     using homotopic_loops_imp_subset [OF assms] by auto
  4655   moreover have gloop: "pathfinish g = pathstart g" and hloop: "pathfinish h = pathstart h"
  4656     using homotopic_loops_imp_loop [OF assms] by auto
  4657   ultimately obtain d e where "d > 0" "e > 0"
  4658       and d: "\<And>p. \<lbrakk>path p; pathfinish p = pathstart p; \<forall>t\<in>{0..1}. norm (p t - g t) < d\<rbrakk>
  4659             \<Longrightarrow> homotopic_loops (-{z}) g p"
  4660       and e: "\<And>q. \<lbrakk>path q; pathfinish q = pathstart q; \<forall>t\<in>{0..1}. norm (q t - h t) < e\<rbrakk>
  4661             \<Longrightarrow> homotopic_loops (-{z}) h q"
  4662     using homotopic_nearby_loops [of g "-{z}"] homotopic_nearby_loops [of h "-{z}"] by force
  4663   obtain p where p:
  4664        "valid_path p" "z \<notin> path_image p"
  4665        "pathstart p = pathstart g" "pathfinish p = pathfinish g"
  4666        and gp_less:"\<forall>t\<in>{0..1}. cmod (g t - p t) < d"
  4667        and pap: "contour_integral p (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number g z"
  4668     using winding_number [OF \<open>path g\<close> pag \<open>0 < d\<close>] by blast
  4669   obtain q where q:
  4670        "valid_path q" "z \<notin> path_image q"
  4671        "pathstart q = pathstart h" "pathfinish q = pathfinish h"
  4672        and hq_less: "\<forall>t\<in>{0..1}. cmod (h t - q t) < e"
  4673        and paq:  "contour_integral q (\<lambda>w. 1 / (w - z)) = complex_of_real (2 * pi) * \<i> * winding_number h z"
  4674     using winding_number [OF \<open>path h\<close> pah \<open>0 < e\<close>] by blast
  4675   have gp: "homotopic_loops (- {z}) g p"
  4676     by (simp add: gloop d gp_less norm_minus_commute p valid_path_imp_path)
  4677   have hq: "homotopic_loops (- {z}) h q"
  4678     by (simp add: e hloop hq_less norm_minus_commute q valid_path_imp_path)
  4679   have "contour_integral p (\<lambda>w. 1/(w - z)) = contour_integral q (\<lambda>w. 1/(w - z))"
  4680     apply (rule Cauchy_theorem_homotopic_loops [of "-{z}"])
  4681     apply (blast intro: homotopic_loops_trans homotopic_loops_sym gp hq assms)
  4682     apply (auto intro!: holomorphic_intros simp: p q)
  4683     done
  4684   then show ?thesis
  4685     by (simp add: pap paq)
  4686 qed
  4687 
  4688 lemma winding_number_paths_linear_eq:
  4689   "\<lbrakk>path g; path h; pathstart h = pathstart g; pathfinish h = pathfinish g;
  4690     \<And>t. t \<in> {0..1} \<Longrightarrow> z \<notin> closed_segment (g t) (h t)\<rbrakk>
  4691         \<Longrightarrow> winding_number h z = winding_number g z"
  4692   by (blast intro: sym homotopic_paths_linear winding_number_homotopic_paths elim: )
  4693 
  4694 lemma winding_number_loops_linear_eq:
  4695   "\<lbrakk>path g; path h; pathfinish g = pathstart g; pathfinish h = pathstart h;
  4696     \<And>t. t \<in> {0..1} \<Longrightarrow> z \<notin> closed_segment (g t) (h t)\<rbrakk>
  4697         \<Longrightarrow> winding_number h z = winding_number g z"
  4698   by (blast intro: sym homotopic_loops_linear winding_number_homotopic_loops elim: )
  4699 
  4700 lemma winding_number_nearby_paths_eq:
  4701      "\<lbrakk>path g; path h;
  4702       pathstart h = pathstart g; pathfinish h = pathfinish g;
  4703       \<And>t. t \<in> {0..1} \<Longrightarrow> norm(h t - g t) < norm(g t - z)\<rbrakk>
  4704       \<Longrightarrow> winding_number h z = winding_number g z"
  4705   by (metis segment_bound(2) norm_minus_commute not_le winding_number_paths_linear_eq)
  4706 
  4707 lemma winding_number_nearby_loops_eq:
  4708      "\<lbrakk>path g; path h;
  4709       pathfinish g = pathstart g;
  4710         pathfinish h = pathstart h;
  4711       \<And>t. t \<in> {0..1} \<Longrightarrow> norm(h t - g t) < norm(g t - z)\<rbrakk>
  4712       \<Longrightarrow> winding_number h z = winding_number g z"
  4713   by (metis segment_bound(2) norm_minus_commute not_le winding_number_loops_linear_eq)
  4714 
  4715 
  4716 proposition winding_number_subpath_combine:
  4717     "\<lbrakk>path g; z \<notin> path_image g;
  4718       u \<in> {0..1}; v \<in> {0..1}; w \<in> {0..1}\<rbrakk>
  4719       \<Longrightarrow> winding_number (subpath u v g) z + winding_number (subpath v w g) z =
  4720           winding_number (subpath u w g) z"
  4721 apply (rule trans [OF winding_number_join [THEN sym]
  4722                       winding_number_homotopic_paths [OF homotopic_join_subpaths]])
  4723 apply (auto dest: path_image_subpath_subset)
  4724 done
  4725 
  4726 
  4727 subsection\<open>Partial circle path\<close>
  4728 
  4729 definition part_circlepath :: "[complex, real, real, real, real] \<Rightarrow> complex"
  4730   where "part_circlepath z r s t \<equiv> \<lambda>x. z + of_real r * exp (\<i> * of_real (linepath s t x))"
  4731 
  4732 lemma pathstart_part_circlepath [simp]:
  4733      "pathstart(part_circlepath z r s t) = z + r*exp(\<i> * s)"
  4734 by (metis part_circlepath_def pathstart_def pathstart_linepath)
  4735 
  4736 lemma pathfinish_part_circlepath [simp]:
  4737      "pathfinish(part_circlepath z r s t) = z + r*exp(\<i>*t)"
  4738 by (metis part_circlepath_def pathfinish_def pathfinish_linepath)
  4739 
  4740 proposition has_vector_derivative_part_circlepath [derivative_intros]:
  4741     "((part_circlepath z r s t) has_vector_derivative
  4742       (\<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)))
  4743      (at x within X)"
  4744   apply (simp add: part_circlepath_def linepath_def scaleR_conv_of_real)
  4745   apply (rule has_vector_derivative_real_complex)
  4746   apply (rule derivative_eq_intros | simp)+
  4747   done
  4748 
  4749 corollary vector_derivative_part_circlepath:
  4750     "vector_derivative (part_circlepath z r s t) (at x) =
  4751        \<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)"
  4752   using has_vector_derivative_part_circlepath vector_derivative_at by blast
  4753 
  4754 corollary vector_derivative_part_circlepath01:
  4755     "\<lbrakk>0 \<le> x; x \<le> 1\<rbrakk>
  4756      \<Longrightarrow> vector_derivative (part_circlepath z r s t) (at x within {0..1}) =
  4757           \<i> * r * (of_real t - of_real s) * exp(\<i> * linepath s t x)"
  4758   using has_vector_derivative_part_circlepath
  4759   by (auto simp: vector_derivative_at_within_ivl)
  4760 
  4761 lemma valid_path_part_circlepath [simp]: "valid_path (part_circlepath z r s t)"
  4762   apply (simp add: valid_path_def)
  4763   apply (rule C1_differentiable_imp_piecewise)
  4764   apply (auto simp: C1_differentiable_on_eq vector_derivative_works vector_derivative_part_circlepath has_vector_derivative_part_circlepath
  4765               intro!: continuous_intros)
  4766   done
  4767 
  4768 lemma path_part_circlepath [simp]: "path (part_circlepath z r s t)"
  4769   by (simp add: valid_path_imp_path)
  4770 
  4771 proposition path_image_part_circlepath:
  4772   assumes "s \<le> t"
  4773     shows "path_image (part_circlepath z r s t) = {z + r * exp(\<i> * of_real x) | x. s \<le> x \<and> x \<le> t}"
  4774 proof -
  4775   { fix z::real
  4776     assume "0 \<le> z" "z \<le> 1"
  4777     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"
  4778       apply (rule_tac x="(1 - z) * s + z * t" in exI)
  4779       apply (simp add: linepath_def scaleR_conv_of_real algebra_simps)
  4780       apply (rule conjI)
  4781       using mult_right_mono apply blast
  4782       using affine_ineq  by (metis "mult.commute")
  4783   }
  4784   moreover
  4785   { fix z
  4786     assume "s \<le> z" "z \<le> t"
  4787     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}"
  4788       apply (rule_tac x="(z - s)/(t - s)" in image_eqI)
  4789       apply (simp add: linepath_def scaleR_conv_of_real divide_simps exp_eq)
  4790       apply (auto simp: algebra_simps divide_simps)
  4791       done
  4792   }
  4793   ultimately show ?thesis
  4794     by (fastforce simp add: path_image_def part_circlepath_def)
  4795 qed
  4796 
  4797 corollary path_image_part_circlepath_subset:
  4798     "\<lbrakk>s \<le> t; 0 \<le> r\<rbrakk> \<Longrightarrow> path_image(part_circlepath z r s t) \<subseteq> sphere z r"
  4799 by (auto simp: path_image_part_circlepath sphere_def dist_norm algebra_simps norm_mult)
  4800 
  4801 proposition in_path_image_part_circlepath:
  4802   assumes "w \<in> path_image(part_circlepath z r s t)" "s \<le> t" "0 \<le> r"
  4803     shows "norm(w - z) = r"
  4804 proof -
  4805   have "w \<in> {c. dist z c = r}"
  4806     by (metis (no_types) path_image_part_circlepath_subset sphere_def subset_eq assms)
  4807   thus ?thesis
  4808     by (simp add: dist_norm norm_minus_commute)
  4809 qed
  4810 
  4811 proposition finite_bounded_log: "finite {z::complex. norm z \<le> b \<and> exp z = w}"
  4812 proof (cases "w = 0")
  4813   case True then show ?thesis by auto
  4814 next
  4815   case False
  4816   have *: "finite {x. cmod (complex_of_real (2 * real_of_int x * pi) * \<i>) \<le> b + cmod (Ln w)}"
  4817     apply (simp add: norm_mult finite_int_iff_bounded_le)
  4818     apply (rule_tac x="\<lfloor>(b + cmod (Ln w)) / (2*pi)\<rfloor>" in exI)
  4819     apply (auto simp: divide_simps le_floor_iff)
  4820     done
  4821   have [simp]: "\<And>P f. {z. P z \<and> (\<exists>n. z = f n)} = f ` {n. P (f n)}"
  4822     by blast
  4823   show ?thesis
  4824     apply (subst exp_Ln [OF False, symmetric])
  4825     apply (simp add: exp_eq)
  4826     using norm_add_leD apply (fastforce intro: finite_subset [OF _ *])
  4827     done
  4828 qed
  4829 
  4830 lemma finite_bounded_log2:
  4831   fixes a::complex
  4832     assumes "a \<noteq> 0"
  4833     shows "finite {z. norm z \<le> b \<and> exp(a*z) = w}"
  4834 proof -
  4835   have *: "finite ((\<lambda>z. z / a) ` {z. cmod z \<le> b * cmod a \<and> exp z = w})"
  4836     by (rule finite_imageI [OF finite_bounded_log])
  4837   show ?thesis
  4838     by (rule finite_subset [OF _ *]) (force simp: assms norm_mult)
  4839 qed
  4840 
  4841 proposition has_contour_integral_bound_part_circlepath_strong:
  4842   assumes fi: "(f has_contour_integral i) (part_circlepath z r s t)"
  4843       and "finite k" and le: "0 \<le> B" "0 < r" "s \<le> t"
  4844       and B: "\<And>x. x \<in> path_image(part_circlepath z r s t) - k \<Longrightarrow> norm(f x) \<le> B"
  4845     shows "cmod i \<le> B * r * (t - s)"
  4846 proof -
  4847   consider "s = t" | "s < t" using \<open>s \<le> t\<close> by linarith
  4848   then show ?thesis
  4849   proof cases
  4850     case 1 with fi [unfolded has_contour_integral]
  4851     have "i = 0"  by (simp add: vector_derivative_part_circlepath)
  4852     with assms show ?thesis by simp
  4853   next
  4854     case 2
  4855     have [simp]: "\<bar>r\<bar> = r" using \<open>r > 0\<close> by linarith
  4856     have [simp]: "cmod (complex_of_real t - complex_of_real s) = t-s"
  4857       by (metis "2" abs_of_pos diff_gt_0_iff_gt norm_of_real of_real_diff)
  4858     have "finite (part_circlepath z r s t -` {y} \<inter> {0..1})" if "y \<in> k" for y
  4859     proof -
  4860       define w where "w = (y - z)/of_real r / exp(\<i> * of_real s)"
  4861       have fin: "finite (of_real -` {z. cmod z \<le> 1 \<and> exp (\<i> * complex_of_real (t - s) * z) = w})"
  4862         apply (rule finite_vimageI [OF finite_bounded_log2])
  4863         using \<open>s < t\<close> apply (auto simp: inj_of_real)
  4864         done
  4865       show ?thesis
  4866         apply (simp add: part_circlepath_def linepath_def vimage_def)
  4867         apply (rule finite_subset [OF _ fin])
  4868         using le
  4869         apply (auto simp: w_def algebra_simps scaleR_conv_of_real exp_add exp_diff)
  4870         done
  4871     qed
  4872     then have fin01: "finite ((part_circlepath z r s t) -` k \<inter> {0..1})"
  4873       by (rule finite_finite_vimage_IntI [OF \<open>finite k\<close>])
  4874     have **: "((\<lambda>x. if (part_circlepath z r s t x) \<in> k then 0
  4875                     else f(part_circlepath z r s t x) *
  4876                        vector_derivative (part_circlepath z r s t) (at x)) has_integral i)  {0..1}"
  4877       apply (rule has_integral_spike
  4878               [where f = "\<lambda>x. f(part_circlepath z r s t x) * vector_derivative (part_circlepath z r s t) (at x)"])
  4879       apply (rule negligible_finite [OF fin01])
  4880       using fi has_contour_integral
  4881       apply auto
  4882       done
  4883     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"
  4884       by (auto intro!: B [unfolded path_image_def image_def, simplified])
  4885     show ?thesis
  4886       apply (rule has_integral_bound [where 'a=real, simplified, OF _ **, simplified])
  4887       using assms apply force
  4888       apply (simp add: norm_mult vector_derivative_part_circlepath)
  4889       using le * "2" \<open>r > 0\<close> by auto
  4890   qed
  4891 qed
  4892 
  4893 corollary has_contour_integral_bound_part_circlepath:
  4894       "\<lbrakk>(f has_contour_integral i) (part_circlepath z r s t);
  4895         0 \<le> B; 0 < r; s \<le> t;
  4896         \<And>x. x \<in> path_image(part_circlepath z r s t) \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
  4897        \<Longrightarrow> norm i \<le> B*r*(t - s)"
  4898   by (auto intro: has_contour_integral_bound_part_circlepath_strong)
  4899 
  4900 proposition contour_integrable_continuous_part_circlepath:
  4901      "continuous_on (path_image (part_circlepath z r s t)) f
  4902       \<Longrightarrow> f contour_integrable_on (part_circlepath z r s t)"
  4903   apply (simp add: contour_integrable_on has_contour_integral_def vector_derivative_part_circlepath path_image_def)
  4904   apply (rule integrable_continuous_real)
  4905   apply (fast intro: path_part_circlepath [unfolded path_def] continuous_intros continuous_on_compose2 [where g=f, OF _ _ order_refl])
  4906   done
  4907 
  4908 proposition winding_number_part_circlepath_pos_less:
  4909   assumes "s < t" and no: "norm(w - z) < r"
  4910     shows "0 < Re (winding_number(part_circlepath z r s t) w)"
  4911 proof -
  4912   have "0 < r" by (meson no norm_not_less_zero not_le order.strict_trans2)
  4913   note valid_path_part_circlepath
  4914   moreover have " w \<notin> path_image (part_circlepath z r s t)"
  4915     using assms by (auto simp: path_image_def image_def part_circlepath_def norm_mult linepath_def)
  4916   moreover have "0 < r * (t - s) * (r - cmod (w - z))"
  4917     using assms by (metis \<open>0 < r\<close> diff_gt_0_iff_gt mult_pos_pos)
  4918   ultimately show ?thesis
  4919     apply (rule winding_number_pos_lt [where e = "r*(t - s)*(r - norm(w - z))"])
  4920     apply (simp add: vector_derivative_part_circlepath right_diff_distrib [symmetric] mult_ac)
  4921     apply (rule mult_left_mono)+
  4922     using Re_Im_le_cmod [of "w-z" "linepath s t x" for x]
  4923     apply (simp add: exp_Euler cos_of_real sin_of_real part_circlepath_def algebra_simps cos_squared_eq [unfolded power2_eq_square])
  4924     using assms \<open>0 < r\<close> by auto
  4925 qed
  4926 
  4927 proposition simple_path_part_circlepath:
  4928     "simple_path(part_circlepath z r s t) \<longleftrightarrow> (r \<noteq> 0 \<and> s \<noteq> t \<and> \<bar>s - t\<bar> \<le> 2*pi)"
  4929 proof (cases "r = 0 \<or> s = t")
  4930   case True
  4931   then show ?thesis
  4932     apply (rule disjE)
  4933     apply (force simp: part_circlepath_def simple_path_def intro: bexI [where x = "1/4"] bexI [where x = "1/3"])+
  4934     done
  4935 next
  4936   case False then have "r \<noteq> 0" "s \<noteq> t" by auto
  4937   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"
  4938     by (simp add: algebra_simps)
  4939   have abs01: "\<And>x y::real. 0 \<le> x \<and> x \<le> 1 \<and> 0 \<le> y \<and> y \<le> 1
  4940                       \<Longrightarrow> (x = y \<or> x = 0 \<and> y = 1 \<or> x = 1 \<and> y = 0 \<longleftrightarrow> \<bar>x - y\<bar> \<in> {0,1})"
  4941     by auto
  4942   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)"
  4943     by force
  4944   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>
  4945                   (\<exists>n. \<bar>x - y\<bar> * (t - s) = 2 * (of_int n * pi))"
  4946     by (force simp: algebra_simps abs_if dest: arg_cong [where f=Re] arg_cong [where f=complex_of_real]
  4947                     intro: exI [where x = "-n" for n])
  4948   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"
  4949     apply (rule ccontr)
  4950     apply (drule_tac x="2*pi / \<bar>t - s\<bar>" in spec)
  4951     using False
  4952     apply (simp add: abs_minus_commute divide_simps)
  4953     apply (frule_tac x=1 in spec)
  4954     apply (drule_tac x="-1" in spec)
  4955     apply (simp add:)
  4956     done
  4957   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
  4958   proof -
  4959     have "t-s = 2 * (real_of_int n * pi)/x"
  4960       using that by (simp add: field_simps)
  4961     then show ?thesis by (metis abs_minus_commute)
  4962   qed
  4963   show ?thesis using False
  4964     apply (simp add: simple_path_def path_part_circlepath)
  4965     apply (simp add: part_circlepath_def linepath_def exp_eq  * ** abs01  del: Set.insert_iff)
  4966     apply (subst abs_away)
  4967     apply (auto simp: 1)
  4968     apply (rule ccontr)
  4969     apply (auto simp: 2 divide_simps abs_mult dest: of_int_leD)
  4970     done
  4971 qed
  4972 
  4973 proposition arc_part_circlepath:
  4974   assumes "r \<noteq> 0" "s \<noteq> t" "\<bar>s - t\<bar> < 2*pi"
  4975     shows "arc (part_circlepath z r s t)"
  4976 proof -
  4977   have *: "x = y" if eq: "\<i> * (linepath s t x) = \<i> * (linepath s t y) + 2 * of_int n * complex_of_real pi * \<i>"
  4978                   and x: "x \<in> {0..1}" and y: "y \<in> {0..1}" for x y n
  4979     proof -
  4980       have "(linepath s t x) = (linepath s t y) + 2 * of_int n * complex_of_real pi"
  4981         by (metis add_divide_eq_iff complex_i_not_zero mult.commute nonzero_mult_div_cancel_left eq)
  4982       then have "s*y + t*x = s*x + (t*y + of_int n * (pi * 2))"
  4983         by (force simp: algebra_simps linepath_def dest: arg_cong [where f=Re])
  4984       then have st: "x \<noteq> y \<Longrightarrow> (s-t) = (of_int n * (pi * 2) / (y-x))"
  4985         by (force simp: field_simps)
  4986       show ?thesis
  4987         apply (rule ccontr)
  4988         using assms x y
  4989         apply (simp add: st abs_mult field_simps)
  4990         using st
  4991         apply (auto simp: dest: of_int_lessD)
  4992         done
  4993     qed
  4994   show ?thesis
  4995     using assms
  4996     apply (simp add: arc_def)
  4997     apply (simp add: part_circlepath_def inj_on_def exp_eq)
  4998     apply (blast intro: *)
  4999     done
  5000 qed
  5001 
  5002 
  5003 subsection\<open>Special case of one complete circle\<close>
  5004 
  5005 definition circlepath :: "[complex, real, real] \<Rightarrow> complex"
  5006   where "circlepath z r \<equiv> part_circlepath z r 0 (2*pi)"
  5007 
  5008 lemma circlepath: "circlepath z r = (\<lambda>x. z + r * exp(2 * of_real pi * \<i> * of_real x))"
  5009   by (simp add: circlepath_def part_circlepath_def linepath_def algebra_simps)
  5010 
  5011 lemma pathstart_circlepath [simp]: "pathstart (circlepath z r) = z + r"
  5012   by (simp add: circlepath_def)
  5013 
  5014 lemma pathfinish_circlepath [simp]: "pathfinish (circlepath z r) = z + r"
  5015   by (simp add: circlepath_def) (metis exp_two_pi_i mult.commute)
  5016 
  5017 lemma circlepath_minus: "circlepath z (-r) x = circlepath z r (x + 1/2)"
  5018 proof -
  5019   have "z + of_real r * exp (2 * pi * \<i> * (x + 1 / 2)) =
  5020         z + of_real r * exp (2 * pi * \<i> * x + pi * \<i>)"
  5021     by (simp add: divide_simps) (simp add: algebra_simps)
  5022   also have "... = z - r * exp (2 * pi * \<i> * x)"
  5023     by (simp add: exp_add)
  5024   finally show ?thesis
  5025     by (simp add: circlepath path_image_def sphere_def dist_norm)
  5026 qed
  5027 
  5028 lemma circlepath_add1: "circlepath z r (x+1) = circlepath z r x"
  5029   using circlepath_minus [of z r "x+1/2"] circlepath_minus [of z "-r" x]
  5030   by (simp add: add.commute)
  5031 
  5032 lemma circlepath_add_half: "circlepath z r (x + 1/2) = circlepath z r (x - 1/2)"
  5033   using circlepath_add1 [of z r "x-1/2"]
  5034   by (simp add: add.commute)
  5035 
  5036 lemma path_image_circlepath_minus_subset:
  5037      "path_image (circlepath z (-r)) \<subseteq> path_image (circlepath z r)"
  5038   apply (simp add: path_image_def image_def circlepath_minus, clarify)
  5039   apply (case_tac "xa \<le> 1/2", force)
  5040   apply (force simp add: circlepath_add_half)+
  5041   done
  5042 
  5043 lemma path_image_circlepath_minus: "path_image (circlepath z (-r)) = path_image (circlepath z r)"
  5044   using path_image_circlepath_minus_subset by fastforce
  5045 
  5046 proposition has_vector_derivative_circlepath [derivative_intros]:
  5047  "((circlepath z r) has_vector_derivative (2 * pi * \<i> * r * exp (2 * of_real pi * \<i> * of_real x)))
  5048    (at x within X)"
  5049   apply (simp add: circlepath_def scaleR_conv_of_real)
  5050   apply (rule derivative_eq_intros)
  5051   apply (simp add: algebra_simps)
  5052   done
  5053 
  5054 corollary vector_derivative_circlepath:
  5055    "vector_derivative (circlepath z r) (at x) =
  5056     2 * pi * \<i> * r * exp(2 * of_real pi * \<i> * x)"
  5057 using has_vector_derivative_circlepath vector_derivative_at by blast
  5058 
  5059 corollary vector_derivative_circlepath01:
  5060     "\<lbrakk>0 \<le> x; x \<le> 1\<rbrakk>
  5061      \<Longrightarrow> vector_derivative (circlepath z r) (at x within {0..1}) =
  5062           2 * pi * \<i> * r * exp(2 * of_real pi * \<i> * x)"
  5063   using has_vector_derivative_circlepath
  5064   by (auto simp: vector_derivative_at_within_ivl)
  5065 
  5066 lemma valid_path_circlepath [simp]: "valid_path (circlepath z r)"
  5067   by (simp add: circlepath_def)
  5068 
  5069 lemma path_circlepath [simp]: "path (circlepath z r)"
  5070   by (simp add: valid_path_imp_path)
  5071 
  5072 lemma path_image_circlepath_nonneg:
  5073   assumes "0 \<le> r" shows "path_image (circlepath z r) = sphere z r"
  5074 proof -
  5075   have *: "x \<in> (\<lambda>u. z + (cmod (x - z)) * exp (\<i> * (of_real u * (of_real pi * 2)))) ` {0..1}" for x
  5076   proof (cases "x = z")
  5077     case True then show ?thesis by force
  5078   next
  5079     case False
  5080     define w where "w = x - z"
  5081     then have "w \<noteq> 0" by (simp add: False)
  5082     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)"
  5083       using cis_conv_exp complex_eq_iff by auto
  5084     show ?thesis
  5085       apply (rule sincos_total_2pi [of "Re(w/of_real(norm w))" "Im(w/of_real(norm w))"])
  5086       apply (simp add: divide_simps \<open>w \<noteq> 0\<close> cmod_power2 [symmetric])
  5087       apply (rule_tac x="t / (2*pi)" in image_eqI)
  5088       apply (simp add: divide_simps \<open>w \<noteq> 0\<close>)
  5089       using False **
  5090       apply (auto simp: w_def)
  5091       done
  5092   qed
  5093   show ?thesis
  5094     unfolding circlepath path_image_def sphere_def dist_norm
  5095     by (force simp: assms algebra_simps norm_mult norm_minus_commute intro: *)
  5096 qed
  5097 
  5098 proposition path_image_circlepath [simp]:
  5099     "path_image (circlepath z r) = sphere z \<bar>r\<bar>"
  5100   using path_image_circlepath_minus
  5101   by (force simp add: path_image_circlepath_nonneg abs_if)
  5102 
  5103 lemma has_contour_integral_bound_circlepath_strong:
  5104       "\<lbrakk>(f has_contour_integral i) (circlepath z r);
  5105         finite k; 0 \<le> B; 0 < r;
  5106         \<And>x. \<lbrakk>norm(x - z) = r; x \<notin> k\<rbrakk> \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
  5107         \<Longrightarrow> norm i \<le> B*(2*pi*r)"
  5108   unfolding circlepath_def
  5109   by (auto simp: algebra_simps in_path_image_part_circlepath dest!: has_contour_integral_bound_part_circlepath_strong)
  5110 
  5111 corollary has_contour_integral_bound_circlepath:
  5112       "\<lbrakk>(f has_contour_integral i) (circlepath z r);
  5113         0 \<le> B; 0 < r; \<And>x. norm(x - z) = r \<Longrightarrow> norm(f x) \<le> B\<rbrakk>
  5114         \<Longrightarrow> norm i \<le> B*(2*pi*r)"
  5115   by (auto intro: has_contour_integral_bound_circlepath_strong)
  5116 
  5117 proposition contour_integrable_continuous_circlepath:
  5118     "continuous_on (path_image (circlepath z r)) f
  5119      \<Longrightarrow> f contour_integrable_on (circlepath z r)"
  5120   by (simp add: circlepath_def contour_integrable_continuous_part_circlepath)
  5121 
  5122 lemma simple_path_circlepath: "simple_path(circlepath z r) \<longleftrightarrow> (r \<noteq> 0)"
  5123   by (simp add: circlepath_def simple_path_part_circlepath)
  5124 
  5125 lemma notin_path_image_circlepath [simp]: "cmod (w - z) < r \<Longrightarrow> w \<notin> path_image (circlepath z r)"
  5126   by (simp add: sphere_def dist_norm norm_minus_commute)
  5127 
  5128 proposition contour_integral_circlepath:
  5129      "0 < r \<Longrightarrow> contour_integral (circlepath z r) (\<lambda>w. 1 / (w - z)) = 2 * complex_of_real pi * \<i>"
  5130   apply (rule contour_integral_unique)
  5131   apply (simp add: has_contour_integral_def)
  5132   apply (subst has_integral_cong)
  5133   apply (simp add: vector_derivative_circlepath01)
  5134   using has_integral_const_real [of _ 0 1]
  5135   apply (force simp: circlepath)
  5136   done
  5137 
  5138 lemma winding_number_circlepath_centre: "0 < r \<Longrightarrow> winding_number (circlepath z r) z = 1"
  5139   apply (rule winding_number_unique_loop)
  5140   apply (simp_all add: sphere_def valid_path_imp_path)
  5141   apply (rule_tac x="circlepath z r" in exI)
  5142   apply (simp add: sphere_def contour_integral_circlepath)
  5143   done
  5144 
  5145 proposition winding_number_circlepath:
  5146   assumes "norm(w - z) < r" shows "winding_number(circlepath z r) w = 1"
  5147 proof (cases "w = z")
  5148   case True then show ?thesis
  5149     using assms winding_number_circlepath_centre by auto
  5150 next
  5151   case False
  5152   have [simp]: "r > 0"
  5153     using assms le_less_trans norm_ge_zero by blast
  5154   define r' where "r' = norm(w - z)"
  5155   have "r' < r"
  5156     by (simp add: assms r'_def)
  5157   have disjo: "cball z r' \<inter> sphere z r = {}"
  5158     using \<open>r' < r\<close> by (force simp: cball_def sphere_def)
  5159   have "winding_number(circlepath z r) w = winding_number(circlepath z r) z"
  5160     apply (rule winding_number_around_inside [where s = "cball z r'"])
  5161     apply (simp_all add: disjo order.strict_implies_order winding_number_circlepath_centre)
  5162     apply (simp_all add: False r'_def dist_norm norm_minus_commute)
  5163     done
  5164   also have "... = 1"
  5165     by (simp add: winding_number_circlepath_centre)
  5166   finally show ?thesis .
  5167 qed
  5168 
  5169 
  5170 text\<open> Hence the Cauchy formula for points inside a circle.\<close>
  5171 
  5172 theorem Cauchy_integral_circlepath:
  5173   assumes "continuous_on (cball z r) f" "f holomorphic_on (ball z r)" "norm(w - z) < r"
  5174   shows "((\<lambda>u. f u/(u - w)) has_contour_integral (2 * of_real pi * \<i> * f w))
  5175          (circlepath z r)"
  5176 proof -
  5177   have "r > 0"
  5178     using assms le_less_trans norm_ge_zero by blast
  5179   have "((\<lambda>u. f u / (u - w)) has_contour_integral (2 * pi) * \<i> * winding_number (circlepath z r) w * f w)
  5180         (circlepath z r)"
  5181     apply (rule Cauchy_integral_formula_weak [where s = "cball z r" and k = "{}"])
  5182     using assms  \<open>r > 0\<close>
  5183     apply (simp_all add: dist_norm norm_minus_commute)
  5184     apply (metis at_within_interior dist_norm holomorphic_on_def interior_ball mem_ball norm_minus_commute)
  5185     apply (simp add: cball_def sphere_def dist_norm, clarify)
  5186     apply (simp add:)
  5187     by (metis dist_commute dist_norm less_irrefl)
  5188   then show ?thesis
  5189     by (simp add: winding_number_circlepath assms)
  5190 qed
  5191 
  5192 corollary Cauchy_integral_circlepath_simple:
  5193   assumes "f holomorphic_on cball z r" "norm(w - z) < r"
  5194   shows "((\<lambda>u. f u/(u - w)) has_contour_integral (2 * of_real pi * \<i> * f w))
  5195          (circlepath z r)"
  5196 using assms by (force simp: holomorphic_on_imp_continuous_on holomorphic_on_subset Cauchy_integral_circlepath)
  5197 
  5198 
  5199 lemma no_bounded_connected_component_imp_winding_number_zero:
  5200   assumes g: "path g" "path_image g \<subseteq> s" "pathfinish g = pathstart g" "z \<notin> s"
  5201       and nb: "\<And>z. bounded (connected_component_set (- s) z) \<longrightarrow> z \<in> s"
  5202   shows "winding_number g z = 0"
  5203 apply (rule winding_number_zero_in_outside)
  5204 apply (simp_all add: assms)
  5205 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)
  5206 
  5207 lemma no_bounded_path_component_imp_winding_number_zero:
  5208   assumes g: "path g" "path_image g \<subseteq> s" "pathfinish g = pathstart g" "z \<notin> s"
  5209       and nb: "\<And>z. bounded (path_component_set (- s) z) \<longrightarrow> z \<in> s"
  5210   shows "winding_number g z = 0"
  5211 apply (rule no_bounded_connected_component_imp_winding_number_zero [OF g])
  5212 by (simp add: bounded_subset nb path_component_subset_connected_component)
  5213 
  5214 
  5215 subsection\<open> Uniform convergence of path integral\<close>
  5216 
  5217 text\<open>Uniform convergence when the derivative of the path is bounded, and in particular for the special case of a circle.\<close>
  5218 
  5219 proposition contour_integral_uniform_limit:
  5220   assumes ev_fint: "eventually (\<lambda>n::'a. (f n) contour_integrable_on \<gamma>) F"
  5221       and ul_f: "uniform_limit (path_image \<gamma>) f l F"
  5222       and noleB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
  5223       and \<gamma>: "valid_path \<gamma>"
  5224       and [simp]: "~ (trivial_limit F)"
  5225   shows "l contour_integrable_on \<gamma>" "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
  5226 proof -
  5227   have "0 \<le> B" by (meson noleB [of 0] atLeastAtMost_iff norm_ge_zero order_refl order_trans zero_le_one)
  5228   { fix e::real
  5229     assume "0 < e"
  5230     then have "0 < e / (\<bar>B\<bar> + 1)" by simp
  5231     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)"
  5232       using ul_f [unfolded uniform_limit_iff dist_norm] by auto
  5233     with ev_fint
  5234     obtain a where fga: "\<And>x. x \<in> {0..1} \<Longrightarrow> cmod (f a (\<gamma> x) - l (\<gamma> x)) < e / (\<bar>B\<bar> + 1)"
  5235                and inta: "(\<lambda>t. f a (\<gamma> t) * vector_derivative \<gamma> (at t)) integrable_on {0..1}"
  5236       using eventually_happens [OF eventually_conj]
  5237       by (fastforce simp: contour_integrable_on path_image_def)
  5238     have Ble: "B * e / (\<bar>B\<bar> + 1) \<le> e"
  5239       using \<open>0 \<le> B\<close>  \<open>0 < e\<close> by (simp add: divide_simps)
  5240     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}"
  5241       apply (rule_tac x="\<lambda>x. f (a::'a) (\<gamma> x) * vector_derivative \<gamma> (at x)" in exI)
  5242       apply (intro inta conjI ballI)
  5243       apply (rule order_trans [OF _ Ble])
  5244       apply (frule noleB)
  5245       apply (frule fga)
  5246       using \<open>0 \<le> B\<close>  \<open>0 < e\<close>
  5247       apply (simp add: norm_mult left_diff_distrib [symmetric] norm_minus_commute divide_simps)
  5248       apply (drule (1) mult_mono [OF less_imp_le])
  5249       apply (simp_all add: mult_ac)
  5250       done
  5251   }
  5252   then show lintg: "l contour_integrable_on \<gamma>"
  5253     apply (simp add: contour_integrable_on)
  5254     apply (blast intro: integrable_uniform_limit_real)
  5255     done
  5256   { fix e::real
  5257     define B' where "B' = B + 1"
  5258     have B': "B' > 0" "B' > B" using  \<open>0 \<le> B\<close> by (auto simp: B'_def)
  5259     assume "0 < e"
  5260     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'"
  5261       using ul_f [unfolded uniform_limit_iff dist_norm, rule_format, of "e / B' / 2"] B' 
  5262         by (simp add: field_simps)
  5263     have ie: "integral {0..1::real} (\<lambda>x. e / 2) < e" using \<open>0 < e\<close> by simp
  5264     have *: "cmod (f x (\<gamma> t) * vector_derivative \<gamma> (at t) - l (\<gamma> t) * vector_derivative \<gamma> (at t)) \<le> e / 2"
  5265              if t: "t\<in>{0..1}" and leB': "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) < e / B'" for x t
  5266     proof -
  5267       have "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) * cmod (vector_derivative \<gamma> (at t)) \<le> e * (B/ B')"
  5268         using mult_mono [OF less_imp_le [OF leB'] noleB] B' \<open>0 < e\<close> t by auto
  5269       also have "... < e"
  5270         by (simp add: B' \<open>0 < e\<close> mult_imp_div_pos_less)
  5271       finally have "2 * cmod (f x (\<gamma> t) - l (\<gamma> t)) * cmod (vector_derivative \<gamma> (at t)) < e" .
  5272       then show ?thesis
  5273         by (simp add: left_diff_distrib [symmetric] norm_mult)
  5274     qed
  5275     have "\<forall>\<^sub>F x in F. dist (contour_integral \<gamma> (f x)) (contour_integral \<gamma> l) < e"
  5276       apply (rule eventually_mono [OF eventually_conj [OF ev_no' ev_fint]])
  5277       apply (simp add: dist_norm contour_integrable_on path_image_def contour_integral_integral)
  5278       apply (simp add: lintg integral_diff [symmetric] contour_integrable_on [symmetric], clarify)
  5279       apply (rule le_less_trans [OF integral_norm_bound_integral ie])
  5280       apply (simp add: lintg integrable_diff contour_integrable_on [symmetric])
  5281       apply (blast intro: *)+
  5282       done
  5283   }
  5284   then show "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
  5285     by (rule tendstoI)
  5286 qed
  5287 
  5288 corollary contour_integral_uniform_limit_circlepath:
  5289   assumes "\<forall>\<^sub>F n::'a in F. (f n) contour_integrable_on (circlepath z r)"
  5290       and "uniform_limit (sphere z r) f l F"
  5291       and "~ (trivial_limit F)" "0 < r"
  5292     shows "l contour_integrable_on (circlepath z r)"
  5293           "((\<lambda>n. contour_integral (circlepath z r) (f n)) \<longlongrightarrow> contour_integral (circlepath z r) l) F"
  5294   using assms by (auto simp: vector_derivative_circlepath norm_mult intro!: contour_integral_uniform_limit)
  5295 
  5296 
  5297 subsection\<open> General stepping result for derivative formulas.\<close>
  5298 
  5299 proposition Cauchy_next_derivative:
  5300   assumes "continuous_on (path_image \<gamma>) f'"
  5301       and leB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
  5302       and int: "\<And>w. w \<in> s - path_image \<gamma> \<Longrightarrow> ((\<lambda>u. f' u / (u - w)^k) has_contour_integral f w) \<gamma>"
  5303       and k: "k \<noteq> 0"
  5304       and "open s"
  5305       and \<gamma>: "valid_path \<gamma>"
  5306       and w: "w \<in> s - path_image \<gamma>"
  5307     shows "(\<lambda>u. f' u / (u - w)^(Suc k)) contour_integrable_on \<gamma>"
  5308       and "(f has_field_derivative (k * contour_integral \<gamma> (\<lambda>u. f' u/(u - w)^(Suc k))))
  5309            (at w)"  (is "?thes2")
  5310 proof -
  5311   have "open (s - path_image \<gamma>)" using \<open>open s\<close> closed_valid_path_image \<gamma> by blast
  5312   then obtain d where "d>0" and d: "ball w d \<subseteq> s - path_image \<gamma>" using w
  5313     using open_contains_ball by blast
  5314   have [simp]: "\<And>n. cmod (1 + of_nat n) = 1 + of_nat n"
  5315     by (metis norm_of_nat of_nat_Suc)
  5316   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)
  5317                          contour_integrable_on \<gamma>"
  5318     apply (simp add: eventually_at)
  5319     apply (rule_tac x=d in exI)
  5320     apply (simp add: \<open>d > 0\<close> dist_norm field_simps, clarify)
  5321     apply (rule contour_integrable_div [OF contour_integrable_diff])
  5322     using int w d
  5323     apply (force simp:  dist_norm norm_minus_commute intro!: has_contour_integral_integrable)+
  5324     done
  5325   have bim_g: "bounded (image f' (path_image \<gamma>))"
  5326     by (simp add: compact_imp_bounded compact_continuous_image compact_valid_path_image assms)
  5327   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"
  5328     by (force simp: bounded_pos path_image_def)
  5329   have twom: "\<forall>\<^sub>F n in at w.
  5330                \<forall>x\<in>path_image \<gamma>.
  5331                 cmod ((inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / k - inverse (x - w) ^ Suc k) < e"
  5332          if "0 < e" for e
  5333   proof -
  5334     have *: "cmod ((inverse (x - u) ^ k - inverse (x - w) ^ k) / ((u - w) * k) - inverse (x - w) ^ Suc k)   < e"
  5335             if x: "x \<in> path_image \<gamma>" and "u \<noteq> w" and uwd: "cmod (u - w) < d/2"
  5336                 and uw_less: "cmod (u - w) < e * (d / 2) ^ (k+2) / (1 + real k)"
  5337             for u x
  5338     proof -
  5339       define ff where [abs_def]:
  5340         "ff n w =
  5341           (if n = 0 then inverse(x - w)^k
  5342            else if n = 1 then k / (x - w)^(Suc k)
  5343            else (k * of_real(Suc k)) / (x - w)^(k + 2))" for n :: nat and w
  5344       have km1: "\<And>z::complex. z \<noteq> 0 \<Longrightarrow> z ^ (k - Suc 0) = z ^ k / z"
  5345         by (simp add: field_simps) (metis Suc_pred \<open>k \<noteq> 0\<close> neq0_conv power_Suc)
  5346       have ff1: "(ff i has_field_derivative ff (Suc i) z) (at z within ball w (d / 2))"
  5347               if "z \<in> ball w (d / 2)" "i \<le> 1" for i z
  5348       proof -
  5349         have "z \<notin> path_image \<gamma>"
  5350           using \<open>x \<in> path_image \<gamma>\<close> d that ball_divide_subset_numeral by blast
  5351         then have xz[simp]: "x \<noteq> z" using \<open>x \<in> path_image \<gamma>\<close> by blast
  5352         then have neq: "x * x + z * z \<noteq> x * (z * 2)"
  5353           by (blast intro: dest!: sum_sqs_eq)
  5354         with xz have "\<And>v. v \<noteq> 0 \<Longrightarrow> (x * x + z * z) * v \<noteq> (x * (z * 2) * v)" by auto
  5355         then have neqq: "\<And>v. v \<noteq> 0 \<Longrightarrow> x * (x * v) + z * (z * v) \<noteq> x * (z * (2 * v))"
  5356           by (simp add: algebra_simps)
  5357         show ?thesis using \<open>i \<le> 1\<close>
  5358           apply (simp add: ff_def dist_norm Nat.le_Suc_eq km1, safe)
  5359           apply (rule derivative_eq_intros | simp add: km1 | simp add: field_simps neq neqq)+
  5360           done
  5361       qed
  5362       { fix a::real and b::real assume ab: "a > 0" "b > 0"
  5363         then have "k * (1 + real k) * (1 / a) \<le> k * (1 + real k) * (4 / b) \<longleftrightarrow> b \<le> 4 * a"
  5364           apply (subst mult_le_cancel_left_pos)
  5365           using \<open>k \<noteq> 0\<close>
  5366           apply (auto simp: divide_simps)
  5367           done
  5368         with ab have "real k * (1 + real k) / a \<le> (real k * 4 + real k * real k * 4) / b \<longleftrightarrow> b \<le> 4 * a"
  5369           by (simp add: field_simps)
  5370       } note canc = this
  5371       have ff2: "cmod (ff (Suc 1) v) \<le> real (k * (k + 1)) / (d / 2) ^ (k + 2)"
  5372                 if "v \<in> ball w (d / 2)" for v
  5373       proof -
  5374         have "d/2 \<le> cmod (x - v)" using d x that
  5375           apply (simp add: dist_norm path_image_def ball_def not_less [symmetric] del: divide_const_simps, clarify)
  5376           apply (drule subsetD)
  5377            prefer 2 apply blast
  5378           apply (metis norm_minus_commute norm_triangle_half_r CollectI)
  5379           done
  5380         then have "d \<le> cmod (x - v) * 2"
  5381           by (simp add: divide_simps)
  5382         then have dpow_le: "d ^ (k+2) \<le> (cmod (x - v) * 2) ^ (k+2)"
  5383           using \<open>0 < d\<close> order_less_imp_le power_mono by blast
  5384         have "x \<noteq> v" using that
  5385           using \<open>x \<in> path_image \<gamma>\<close> ball_divide_subset_numeral d by fastforce
  5386         then show ?thesis
  5387         using \<open>d > 0\<close>
  5388         apply (simp add: ff_def norm_mult norm_divide norm_power dist_norm canc)
  5389         using dpow_le
  5390         apply (simp add: algebra_simps divide_simps mult_less_0_iff)
  5391         done
  5392       qed
  5393       have ub: "u \<in> ball w (d / 2)"
  5394         using uwd by (simp add: dist_commute dist_norm)
  5395       have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
  5396                   \<le> (real k * 4 + real k * real k * 4) * (cmod (u - w) * cmod (u - w)) / (d * (d * (d / 2) ^ k))"
  5397         using complex_taylor [OF _ ff1 ff2 _ ub, of w, simplified]
  5398         by (simp add: ff_def \<open>0 < d\<close>)
  5399       then have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
  5400                   \<le> (cmod (u - w) * real k) * (1 + real k) * cmod (u - w) / (d / 2) ^ (k+2)"
  5401         by (simp add: field_simps)
  5402       then have "cmod (inverse (x - u) ^ k - (inverse (x - w) ^ k + of_nat k * (u - w) / ((x - w) * (x - w) ^ k)))
  5403                  / (cmod (u - w) * real k)
  5404                   \<le> (1 + real k) * cmod (u - w) / (d / 2) ^ (k+2)"
  5405         using \<open>k \<noteq> 0\<close> \<open>u \<noteq> w\<close> by (simp add: mult_ac zero_less_mult_iff pos_divide_le_eq)
  5406       also have "... < e"
  5407         using uw_less \<open>0 < d\<close> by (simp add: mult_ac divide_simps)
  5408       finally have e: "cmod (inverse (x-u)^k - (inverse (x-w)^k + of_nat k * (u-w) / ((x-w) * (x-w)^k)))
  5409                         / cmod ((u - w) * real k)   <   e"
  5410         by (simp add: norm_mult)
  5411       have "x \<noteq> u"
  5412         using uwd \<open>0 < d\<close> x d by (force simp: dist_norm ball_def norm_minus_commute)
  5413       show ?thesis
  5414         apply (rule le_less_trans [OF _ e])
  5415         using \<open>k \<noteq> 0\<close> \<open>x \<noteq> u\<close>  \<open>u \<noteq> w\<close>
  5416         apply (simp add: field_simps norm_divide [symmetric])
  5417         done
  5418     qed
  5419     show ?thesis
  5420       unfolding eventually_at
  5421       apply (rule_tac x = "min (d/2) ((e*(d/2)^(k + 2))/(Suc k))" in exI)
  5422       apply (force simp: \<open>d > 0\<close> dist_norm that simp del: power_Suc intro: *)
  5423       done
  5424   qed
  5425   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)"
  5426     unfolding uniform_limit_iff dist_norm
  5427   proof clarify
  5428     fix e::real
  5429     assume "0 < e"
  5430     have *: "cmod (f' (\<gamma> x) * (inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
  5431                         f' (\<gamma> x) / ((\<gamma> x - w) * (\<gamma> x - w) ^ k)) < e"
  5432               if ec: "cmod ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
  5433                       inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k) < e / C"
  5434                  and x: "0 \<le> x" "x \<le> 1"
  5435               for u x
  5436     proof (cases "(f' (\<gamma> x)) = 0")
  5437       case True then show ?thesis by (simp add: \<open>0 < e\<close>)
  5438     next
  5439       case False
  5440       have "cmod (f' (\<gamma> x) * (inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
  5441                         f' (\<gamma> x) / ((\<gamma> x - w) * (\<gamma> x - w) ^ k)) =
  5442             cmod (f' (\<gamma> x) * ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
  5443                              inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k))"
  5444         by (simp add: field_simps)
  5445       also have "... = cmod (f' (\<gamma> x)) *
  5446                        cmod ((inverse (\<gamma> x - u) ^ k - inverse (\<gamma> x - w) ^ k) / ((u - w) * k) -
  5447                              inverse (\<gamma> x - w) * inverse (\<gamma> x - w) ^ k)"
  5448         by (simp add: norm_mult)
  5449       also have "... < cmod (f' (\<gamma> x)) * (e/C)"
  5450         apply (rule mult_strict_left_mono [OF ec])
  5451         using False by simp
  5452       also have "... \<le> e" using C
  5453         by (metis False \<open>0 < e\<close> frac_le less_eq_real_def mult.commute pos_le_divide_eq x zero_less_norm_iff)
  5454       finally show ?thesis .
  5455     qed
  5456     show "\<forall>\<^sub>F n in at w.
  5457               \<forall>x\<in>path_image \<gamma>.
  5458                cmod (f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / of_nat k - f' x / (x - w) ^ Suc k) < e"
  5459       using twom [OF divide_pos_pos [OF \<open>0 < e\<close> \<open>C > 0\<close>]]   unfolding path_image_def
  5460       by (force intro: * elim: eventually_mono)
  5461   qed
  5462   show "(\<lambda>u. f' u / (u - w) ^ (Suc k)) contour_integrable_on \<gamma>"
  5463     by (rule contour_integral_uniform_limit [OF 1 2 leB \<gamma>]) auto
  5464   have *: "(\<lambda>n. contour_integral \<gamma> (\<lambda>x. f' x * (inverse (x - n) ^ k - inverse (x - w) ^ k) / (n - w) / k))
  5465            \<midarrow>w\<rightarrow> contour_integral \<gamma> (\<lambda>u. f' u / (u - w) ^ (Suc k))"
  5466     by (rule contour_integral_uniform_limit [OF 1 2 leB \<gamma>]) auto
  5467   have **: "contour_integral \<gamma> (\<lambda>x. f' x * (inverse (x - u) ^ k - inverse (x - w) ^ k) / ((u - w) * k)) =
  5468               (f u - f w) / (u - w) / k"
  5469            if "dist u w < d" for u
  5470     apply (rule contour_integral_unique)
  5471     apply (simp add: diff_divide_distrib algebra_simps)
  5472     apply (rule has_contour_integral_diff; rule has_contour_integral_div; simp add: field_simps; rule int)
  5473     apply (metis contra_subsetD d dist_commute mem_ball that)
  5474     apply (rule w)
  5475     done
  5476   show ?thes2
  5477     apply (simp add: DERIV_within_iff del: power_Suc)
  5478     apply (rule Lim_transform_within [OF tendsto_mult_left [OF *] \<open>0 < d\<close> ])
  5479     apply (simp add: \<open>k \<noteq> 0\<close> **)
  5480     done
  5481 qed
  5482 
  5483 corollary Cauchy_next_derivative_circlepath:
  5484   assumes contf: "continuous_on (path_image (circlepath z r)) f"
  5485       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)"
  5486       and k: "k \<noteq> 0"
  5487       and w: "w \<in> ball z r"
  5488     shows "(\<lambda>u. f u / (u - w)^(Suc k)) contour_integrable_on (circlepath z r)"
  5489            (is "?thes1")
  5490       and "(g has_field_derivative (k * contour_integral (circlepath z r) (\<lambda>u. f u/(u - w)^(Suc k)))) (at w)"
  5491            (is "?thes2")
  5492 proof -
  5493   have "r > 0" using w
  5494     using ball_eq_empty by fastforce
  5495   have wim: "w \<in> ball z r - path_image (circlepath z r)"
  5496     using w by (auto simp: dist_norm)
  5497   show ?thes1 ?thes2
  5498     by (rule Cauchy_next_derivative [OF contf _ int k open_ball valid_path_circlepath wim, where B = "2 * pi * \<bar>r\<bar>"];
  5499         auto simp: vector_derivative_circlepath norm_mult)+
  5500 qed
  5501 
  5502 
  5503 text\<open> In particular, the first derivative formula.\<close>
  5504 
  5505 proposition Cauchy_derivative_integral_circlepath:
  5506   assumes contf: "continuous_on (cball z r) f"
  5507       and holf: "f holomorphic_on ball z r"
  5508       and w: "w \<in> ball z r"
  5509     shows "(\<lambda>u. f u/(u - w)^2) contour_integrable_on (circlepath z r)"
  5510            (is "?thes1")
  5511       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)"
  5512            (is "?thes2")
  5513 proof -
  5514   have [simp]: "r \<ge> 0" using w
  5515     using ball_eq_empty by fastforce
  5516   have f: "continuous_on (path_image (circlepath z r)) f"
  5517     by (rule continuous_on_subset [OF contf]) (force simp add: cball_def sphere_def)
  5518   have int: "\<And>w. dist z w < r \<Longrightarrow>
  5519                  ((\<lambda>u. f u / (u - w)) has_contour_integral (\<lambda>x. 2 * of_real pi * \<i> * f x) w) (circlepath z r)"
  5520     by (rule Cauchy_integral_circlepath [OF contf holf]) (simp add: dist_norm norm_minus_commute)
  5521   show ?thes1
  5522     apply (simp add: power2_eq_square)
  5523     apply (rule Cauchy_next_derivative_circlepath [OF f _ _ w, where k=1, simplified])
  5524     apply (blast intro: int)
  5525     done
  5526   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)"
  5527     apply (simp add: power2_eq_square)
  5528     apply (rule Cauchy_next_derivative_circlepath [OF f _ _ w, where k=1 and g = "\<lambda>x. 2 * of_real pi * \<i> * f x", simplified])
  5529     apply (blast intro: int)
  5530     done
  5531   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)"
  5532     by (rule DERIV_cdivide [where f = "\<lambda>x. 2 * of_real pi * \<i> * f x" and c = "2 * of_real pi * \<i>", simplified])
  5533   show ?thes2
  5534     by simp (rule fder)
  5535 qed
  5536 
  5537 subsection\<open> Existence of all higher derivatives.\<close>
  5538 
  5539 proposition derivative_is_holomorphic:
  5540   assumes "open s"
  5541       and fder: "\<And>z. z \<in> s \<Longrightarrow> (f has_field_derivative f' z) (at z)"
  5542     shows "f' holomorphic_on s"
  5543 proof -
  5544   have *: "\<exists>h. (f' has_field_derivative h) (at z)" if "z \<in> s" for z
  5545   proof -
  5546     obtain r where "r > 0" and r: "cball z r \<subseteq> s"
  5547       using open_contains_cball \<open>z \<in> s\<close> \<open>open s\<close> by blast
  5548     then have holf_cball: "f holomorphic_on cball z r"
  5549       apply (simp add: holomorphic_on_def)
  5550       using field_differentiable_at_within field_differentiable_def fder by blast
  5551     then have "continuous_on (path_image (circlepath z r)) f"
  5552       using \<open>r > 0\<close> by (force elim: holomorphic_on_subset [THEN holomorphic_on_imp_continuous_on])
  5553     then have contfpi: "continuous_on (path_image (circlepath z r)) (\<lambda>x. 1/(2 * of_real pi*\<i>) * f x)"
  5554       by (auto intro: continuous_intros)+
  5555     have contf_cball: "continuous_on (cball z r) f" using holf_cball
  5556       by (simp add: holomorphic_on_imp_continuous_on holomorphic_on_subset)
  5557     have holf_ball: "f holomorphic_on ball z r" using holf_cball
  5558       using ball_subset_cball holomorphic_on_subset by blast
  5559     { fix w  assume w: "w \<in> ball z r"
  5560       have intf: "(\<lambda>u. f u / (u - w)\<^sup>2) contour_integrable_on circlepath z r"
  5561         by (blast intro: w Cauchy_derivative_integral_circlepath [OF contf_cball holf_ball])
  5562       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))
  5563                   (at w)"
  5564         by (blast intro: w Cauchy_derivative_integral_circlepath [OF contf_cball holf_ball])
  5565       have f'_eq: "f' w = contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>)"
  5566         using fder' ball_subset_cball r w by (force intro: DERIV_unique [OF fder])
  5567       have "((\<lambda>u. f u / (u - w)\<^sup>2 / (2 * of_real pi * \<i>)) has_contour_integral
  5568                 contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>))
  5569                 (circlepath z r)"
  5570         by (rule has_contour_integral_div [OF has_contour_integral_integral [OF intf]])
  5571       then have "((\<lambda>u. f u / (2 * of_real pi * \<i> * (u - w)\<^sup>2)) has_contour_integral
  5572                 contour_integral (circlepath z r) (\<lambda>u. f u / (u - w)\<^sup>2) / (2 * of_real pi * \<i>))
  5573                 (circlepath z r)"
  5574         by (simp add: algebra_simps)
  5575       then have "((\<lambda>u. f u / (2 * of_real pi * \<i> * (u - w)\<^sup>2)) has_contour_integral f' w) (circlepath z r)"
  5576         by (simp add: f'_eq)
  5577     } note * = this
  5578     show ?thesis
  5579       apply (rule exI)
  5580       apply (rule Cauchy_next_derivative_circlepath [OF contfpi, of 2 f', simplified])
  5581       apply (simp_all add: \<open>0 < r\<close> * dist_norm)
  5582       done
  5583   qed
  5584   show ?thesis
  5585     by (simp add: holomorphic_on_open [OF \<open>open s\<close>] *)
  5586 qed
  5587 
  5588 lemma holomorphic_deriv [holomorphic_intros]:
  5589     "\<lbrakk>f holomorphic_on s; open s\<rbrakk> \<Longrightarrow> (deriv f) holomorphic_on s"
  5590 by (metis DERIV_deriv_iff_field_differentiable at_within_open derivative_is_holomorphic holomorphic_on_def)
  5591 
  5592 lemma analytic_deriv: "f analytic_on s \<Longrightarrow> (deriv f) analytic_on s"
  5593   using analytic_on_holomorphic holomorphic_deriv by auto
  5594 
  5595 lemma holomorphic_higher_deriv [holomorphic_intros]: "\<lbrakk>f holomorphic_on s; open s\<rbrakk> \<Longrightarrow> (deriv ^^ n) f holomorphic_on s"
  5596   by (induction n) (auto simp: holomorphic_deriv)
  5597 
  5598 lemma analytic_higher_deriv: "f analytic_on s \<Longrightarrow> (deriv ^^ n) f analytic_on s"
  5599   unfolding analytic_on_def using holomorphic_higher_deriv by blast
  5600 
  5601 lemma has_field_derivative_higher_deriv:
  5602      "\<lbrakk>f holomorphic_on s; open s; x \<in> s\<rbrakk>
  5603       \<Longrightarrow> ((deriv ^^ n) f has_field_derivative (deriv ^^ (Suc n)) f x) (at x)"
  5604 by (metis (no_types, hide_lams) DERIV_deriv_iff_field_differentiable at_within_open comp_apply
  5605          funpow.simps(2) holomorphic_higher_deriv holomorphic_on_def)
  5606 
  5607 lemma valid_path_compose_holomorphic:
  5608   assumes "valid_path g" and holo:"f holomorphic_on s" and "open s" "path_image g \<subseteq> s"
  5609   shows "valid_path (f o g)"
  5610 proof (rule valid_path_compose[OF \<open>valid_path g\<close>])
  5611   fix x assume "x \<in> path_image g"
  5612   then show "f field_differentiable at x"
  5613     using analytic_on_imp_differentiable_at analytic_on_open assms holo by blast
  5614 next
  5615   have "deriv f holomorphic_on s"
  5616     using holomorphic_deriv holo \<open>open s\<close> by auto
  5617   then show "continuous_on (path_image g) (deriv f)"
  5618     using assms(4) holomorphic_on_imp_continuous_on holomorphic_on_subset by auto
  5619 qed
  5620 
  5621 
  5622 subsection\<open> Morera's theorem.\<close>
  5623 
  5624 lemma Morera_local_triangle_ball:
  5625   assumes "\<And>z. z \<in> s
  5626           \<Longrightarrow> \<exists>e a. 0 < e \<and> z \<in> ball a e \<and> continuous_on (ball a e) f \<and>
  5627                     (\<forall>b c. closed_segment b c \<subseteq> ball a e
  5628                            \<longrightarrow> contour_integral (linepath a b) f +
  5629                                contour_integral (linepath b c) f +
  5630                                contour_integral (linepath c a) f = 0)"
  5631   shows "f analytic_on s"
  5632 proof -
  5633   { fix z  assume "z \<in> s"
  5634     with assms obtain e a where
  5635             "0 < e" and z: "z \<in> ball a e" and contf: "continuous_on (ball a e) f"
  5636         and 0: "\<And>b c. closed_segment b c \<subseteq> ball a e
  5637                       \<Longrightarrow> contour_integral (linepath a b) f +
  5638                           contour_integral (linepath b c) f +
  5639                           contour_integral (linepath c a) f = 0"
  5640       by fastforce
  5641     have az: "dist a z < e" using mem_ball z by blast
  5642     have sb_ball: "ball z (e - dist a z) \<subseteq> ball a e"
  5643       by (simp add: dist_commute ball_subset_ball_iff)
  5644     have "\<exists>e>0. f holomorphic_on ball z e"
  5645       apply (rule_tac x="e - dist a z" in exI)
  5646       apply (simp add: az)
  5647       apply (rule holomorphic_on_subset [OF _ sb_ball])