src/HOL/Library/Formal_Power_Series.thy
author chaieb
Thu Jul 23 21:12:57 2009 +0200 (2009-07-23)
changeset 32160 63686057cbe8
parent 32157 adea7a729c7a
child 32161 abda97d2deea
permissions -rw-r--r--
Vandermonde vs Pochhammer; Hypergeometric series - very basic facts
     1 (*  Title:      Formal_Power_Series.thy
     2     Author:     Amine Chaieb, University of Cambridge
     3 *)
     4 
     5 header{* A formalization of formal power series *}
     6 
     7 theory Formal_Power_Series
     8 imports Complex_Main Binomial
     9 begin
    10 
    11 
    12 subsection {* The type of formal power series*}
    13 
    14 typedef (open) 'a fps = "{f :: nat \<Rightarrow> 'a. True}"
    15   morphisms fps_nth Abs_fps
    16   by simp
    17 
    18 notation fps_nth (infixl "$" 75)
    19 
    20 lemma expand_fps_eq: "p = q \<longleftrightarrow> (\<forall>n. p $ n = q $ n)"
    21   by (simp add: fps_nth_inject [symmetric] expand_fun_eq)
    22 
    23 lemma fps_ext: "(\<And>n. p $ n = q $ n) \<Longrightarrow> p = q"
    24   by (simp add: expand_fps_eq)
    25 
    26 lemma fps_nth_Abs_fps [simp]: "Abs_fps f $ n = f n"
    27   by (simp add: Abs_fps_inverse)
    28 
    29 text{* Definition of the basic elements 0 and 1 and the basic operations of addition, negation and multiplication *}
    30 
    31 instantiation fps :: (zero)  zero
    32 begin
    33 
    34 definition fps_zero_def:
    35   "0 = Abs_fps (\<lambda>n. 0)"
    36 
    37 instance ..
    38 end
    39 
    40 lemma fps_zero_nth [simp]: "0 $ n = 0"
    41   unfolding fps_zero_def by simp
    42 
    43 instantiation fps :: ("{one,zero}")  one
    44 begin
    45 
    46 definition fps_one_def:
    47   "1 = Abs_fps (\<lambda>n. if n = 0 then 1 else 0)"
    48 
    49 instance ..
    50 end
    51 
    52 lemma fps_one_nth [simp]: "1 $ n = (if n = 0 then 1 else 0)"
    53   unfolding fps_one_def by simp
    54 
    55 instantiation fps :: (plus)  plus
    56 begin
    57 
    58 definition fps_plus_def:
    59   "op + = (\<lambda>f g. Abs_fps (\<lambda>n. f $ n + g $ n))"
    60 
    61 instance ..
    62 end
    63 
    64 lemma fps_add_nth [simp]: "(f + g) $ n = f $ n + g $ n"
    65   unfolding fps_plus_def by simp
    66 
    67 instantiation fps :: (minus) minus
    68 begin
    69 
    70 definition fps_minus_def:
    71   "op - = (\<lambda>f g. Abs_fps (\<lambda>n. f $ n - g $ n))"
    72 
    73 instance ..
    74 end
    75 
    76 lemma fps_sub_nth [simp]: "(f - g) $ n = f $ n - g $ n"
    77   unfolding fps_minus_def by simp
    78 
    79 instantiation fps :: (uminus) uminus
    80 begin
    81 
    82 definition fps_uminus_def:
    83   "uminus = (\<lambda>f. Abs_fps (\<lambda>n. - (f $ n)))"
    84 
    85 instance ..
    86 end
    87 
    88 lemma fps_neg_nth [simp]: "(- f) $ n = - (f $ n)"
    89   unfolding fps_uminus_def by simp
    90 
    91 instantiation fps :: ("{comm_monoid_add, times}")  times
    92 begin
    93 
    94 definition fps_times_def:
    95   "op * = (\<lambda>f g. Abs_fps (\<lambda>n. \<Sum>i=0..n. f $ i * g $ (n - i)))"
    96 
    97 instance ..
    98 end
    99 
   100 lemma fps_mult_nth: "(f * g) $ n = (\<Sum>i=0..n. f$i * g$(n - i))"
   101   unfolding fps_times_def by simp
   102 
   103 declare atLeastAtMost_iff[presburger]
   104 declare Bex_def[presburger]
   105 declare Ball_def[presburger]
   106 
   107 lemma mult_delta_left:
   108   fixes x y :: "'a::mult_zero"
   109   shows "(if b then x else 0) * y = (if b then x * y else 0)"
   110   by simp
   111 
   112 lemma mult_delta_right:
   113   fixes x y :: "'a::mult_zero"
   114   shows "x * (if b then y else 0) = (if b then x * y else 0)"
   115   by simp
   116 
   117 lemma cond_value_iff: "f (if b then x else y) = (if b then f x else f y)"
   118   by auto
   119 lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
   120   by auto
   121 
   122 subsection{* Formal power series form a commutative ring with unity, if the range of sequences
   123   they represent is a commutative ring with unity*}
   124 
   125 instance fps :: (semigroup_add) semigroup_add
   126 proof
   127   fix a b c :: "'a fps" show "a + b + c = a + (b + c)"
   128     by (simp add: fps_ext add_assoc)
   129 qed
   130 
   131 instance fps :: (ab_semigroup_add) ab_semigroup_add
   132 proof
   133   fix a b :: "'a fps" show "a + b = b + a"
   134     by (simp add: fps_ext add_commute)
   135 qed
   136 
   137 lemma fps_mult_assoc_lemma:
   138   fixes k :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
   139   shows "(\<Sum>j=0..k. \<Sum>i=0..j. f i (j - i) (n - j)) =
   140          (\<Sum>j=0..k. \<Sum>i=0..k - j. f j i (n - j - i))"
   141 proof (induct k)
   142   case 0 show ?case by simp
   143 next
   144   case (Suc k) thus ?case
   145     by (simp add: Suc_diff_le setsum_addf add_assoc
   146              cong: strong_setsum_cong)
   147 qed
   148 
   149 instance fps :: (semiring_0) semigroup_mult
   150 proof
   151   fix a b c :: "'a fps"
   152   show "(a * b) * c = a * (b * c)"
   153   proof (rule fps_ext)
   154     fix n :: nat
   155     have "(\<Sum>j=0..n. \<Sum>i=0..j. a$i * b$(j - i) * c$(n - j)) =
   156           (\<Sum>j=0..n. \<Sum>i=0..n - j. a$j * b$i * c$(n - j - i))"
   157       by (rule fps_mult_assoc_lemma)
   158     thus "((a * b) * c) $ n = (a * (b * c)) $ n"
   159       by (simp add: fps_mult_nth setsum_right_distrib
   160                     setsum_left_distrib mult_assoc)
   161   qed
   162 qed
   163 
   164 lemma fps_mult_commute_lemma:
   165   fixes n :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
   166   shows "(\<Sum>i=0..n. f i (n - i)) = (\<Sum>i=0..n. f (n - i) i)"
   167 proof (rule setsum_reindex_cong)
   168   show "inj_on (\<lambda>i. n - i) {0..n}"
   169     by (rule inj_onI) simp
   170   show "{0..n} = (\<lambda>i. n - i) ` {0..n}"
   171     by (auto, rule_tac x="n - x" in image_eqI, simp_all)
   172 next
   173   fix i assume "i \<in> {0..n}"
   174   hence "n - (n - i) = i" by simp
   175   thus "f (n - i) i = f (n - i) (n - (n - i))" by simp
   176 qed
   177 
   178 instance fps :: (comm_semiring_0) ab_semigroup_mult
   179 proof
   180   fix a b :: "'a fps"
   181   show "a * b = b * a"
   182   proof (rule fps_ext)
   183     fix n :: nat
   184     have "(\<Sum>i=0..n. a$i * b$(n - i)) = (\<Sum>i=0..n. a$(n - i) * b$i)"
   185       by (rule fps_mult_commute_lemma)
   186     thus "(a * b) $ n = (b * a) $ n"
   187       by (simp add: fps_mult_nth mult_commute)
   188   qed
   189 qed
   190 
   191 instance fps :: (monoid_add) monoid_add
   192 proof
   193   fix a :: "'a fps" show "0 + a = a "
   194     by (simp add: fps_ext)
   195 next
   196   fix a :: "'a fps" show "a + 0 = a "
   197     by (simp add: fps_ext)
   198 qed
   199 
   200 instance fps :: (comm_monoid_add) comm_monoid_add
   201 proof
   202   fix a :: "'a fps" show "0 + a = a "
   203     by (simp add: fps_ext)
   204 qed
   205 
   206 instance fps :: (semiring_1) monoid_mult
   207 proof
   208   fix a :: "'a fps" show "1 * a = a"
   209     by (simp add: fps_ext fps_mult_nth mult_delta_left setsum_delta)
   210 next
   211   fix a :: "'a fps" show "a * 1 = a"
   212     by (simp add: fps_ext fps_mult_nth mult_delta_right setsum_delta')
   213 qed
   214 
   215 instance fps :: (cancel_semigroup_add) cancel_semigroup_add
   216 proof
   217   fix a b c :: "'a fps"
   218   assume "a + b = a + c" then show "b = c"
   219     by (simp add: expand_fps_eq)
   220 next
   221   fix a b c :: "'a fps"
   222   assume "b + a = c + a" then show "b = c"
   223     by (simp add: expand_fps_eq)
   224 qed
   225 
   226 instance fps :: (cancel_ab_semigroup_add) cancel_ab_semigroup_add
   227 proof
   228   fix a b c :: "'a fps"
   229   assume "a + b = a + c" then show "b = c"
   230     by (simp add: expand_fps_eq)
   231 qed
   232 
   233 instance fps :: (cancel_comm_monoid_add) cancel_comm_monoid_add ..
   234 
   235 instance fps :: (group_add) group_add
   236 proof
   237   fix a :: "'a fps" show "- a + a = 0"
   238     by (simp add: fps_ext)
   239 next
   240   fix a b :: "'a fps" show "a - b = a + - b"
   241     by (simp add: fps_ext diff_minus)
   242 qed
   243 
   244 instance fps :: (ab_group_add) ab_group_add
   245 proof
   246   fix a :: "'a fps"
   247   show "- a + a = 0"
   248     by (simp add: fps_ext)
   249 next
   250   fix a b :: "'a fps"
   251   show "a - b = a + - b"
   252     by (simp add: fps_ext)
   253 qed
   254 
   255 instance fps :: (zero_neq_one) zero_neq_one
   256   by default (simp add: expand_fps_eq)
   257 
   258 instance fps :: (semiring_0) semiring
   259 proof
   260   fix a b c :: "'a fps"
   261   show "(a + b) * c = a * c + b * c"
   262     by (simp add: expand_fps_eq fps_mult_nth left_distrib setsum_addf)
   263 next
   264   fix a b c :: "'a fps"
   265   show "a * (b + c) = a * b + a * c"
   266     by (simp add: expand_fps_eq fps_mult_nth right_distrib setsum_addf)
   267 qed
   268 
   269 instance fps :: (semiring_0) semiring_0
   270 proof
   271   fix a:: "'a fps" show "0 * a = 0"
   272     by (simp add: fps_ext fps_mult_nth)
   273 next
   274   fix a:: "'a fps" show "a * 0 = 0"
   275     by (simp add: fps_ext fps_mult_nth)
   276 qed
   277 
   278 instance fps :: (semiring_0_cancel) semiring_0_cancel ..
   279 
   280 subsection {* Selection of the nth power of the implicit variable in the infinite sum*}
   281 
   282 lemma fps_nonzero_nth: "f \<noteq> 0 \<longleftrightarrow> (\<exists> n. f $n \<noteq> 0)"
   283   by (simp add: expand_fps_eq)
   284 
   285 lemma fps_nonzero_nth_minimal:
   286   "f \<noteq> 0 \<longleftrightarrow> (\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0))"
   287 proof
   288   let ?n = "LEAST n. f $ n \<noteq> 0"
   289   assume "f \<noteq> 0"
   290   then have "\<exists>n. f $ n \<noteq> 0"
   291     by (simp add: fps_nonzero_nth)
   292   then have "f $ ?n \<noteq> 0"
   293     by (rule LeastI_ex)
   294   moreover have "\<forall>m<?n. f $ m = 0"
   295     by (auto dest: not_less_Least)
   296   ultimately have "f $ ?n \<noteq> 0 \<and> (\<forall>m<?n. f $ m = 0)" ..
   297   then show "\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0)" ..
   298 next
   299   assume "\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0)"
   300   then show "f \<noteq> 0" by (auto simp add: expand_fps_eq)
   301 qed
   302 
   303 lemma fps_eq_iff: "f = g \<longleftrightarrow> (\<forall>n. f $ n = g $n)"
   304   by (rule expand_fps_eq)
   305 
   306 lemma fps_setsum_nth: "(setsum f S) $ n = setsum (\<lambda>k. (f k) $ n) S"
   307 proof (cases "finite S")
   308   assume "\<not> finite S" then show ?thesis by simp
   309 next
   310   assume "finite S"
   311   then show ?thesis by (induct set: finite) auto
   312 qed
   313 
   314 subsection{* Injection of the basic ring elements and multiplication by scalars *}
   315 
   316 definition
   317   "fps_const c = Abs_fps (\<lambda>n. if n = 0 then c else 0)"
   318 
   319 lemma fps_nth_fps_const [simp]: "fps_const c $ n = (if n = 0 then c else 0)"
   320   unfolding fps_const_def by simp
   321 
   322 lemma fps_const_0_eq_0 [simp]: "fps_const 0 = 0"
   323   by (simp add: fps_ext)
   324 
   325 lemma fps_const_1_eq_1 [simp]: "fps_const 1 = 1"
   326   by (simp add: fps_ext)
   327 
   328 lemma fps_const_neg [simp]: "- (fps_const (c::'a::ring)) = fps_const (- c)"
   329   by (simp add: fps_ext)
   330 
   331 lemma fps_const_add [simp]: "fps_const (c::'a\<Colon>monoid_add) + fps_const d = fps_const (c + d)"
   332   by (simp add: fps_ext)
   333 lemma fps_const_sub [simp]: "fps_const (c::'a\<Colon>group_add) - fps_const d = fps_const (c - d)"
   334   by (simp add: fps_ext)
   335 lemma fps_const_mult[simp]: "fps_const (c::'a\<Colon>ring) * fps_const d = fps_const (c * d)"
   336   by (simp add: fps_eq_iff fps_mult_nth setsum_0')
   337 
   338 lemma fps_const_add_left: "fps_const (c::'a\<Colon>monoid_add) + f = Abs_fps (\<lambda>n. if n = 0 then c + f$0 else f$n)"
   339   by (simp add: fps_ext)
   340 
   341 lemma fps_const_add_right: "f + fps_const (c::'a\<Colon>monoid_add) = Abs_fps (\<lambda>n. if n = 0 then f$0 + c else f$n)"
   342   by (simp add: fps_ext)
   343 
   344 lemma fps_const_mult_left: "fps_const (c::'a\<Colon>semiring_0) * f = Abs_fps (\<lambda>n. c * f$n)"
   345   unfolding fps_eq_iff fps_mult_nth
   346   by (simp add: fps_const_def mult_delta_left setsum_delta)
   347 
   348 lemma fps_const_mult_right: "f * fps_const (c::'a\<Colon>semiring_0) = Abs_fps (\<lambda>n. f$n * c)"
   349   unfolding fps_eq_iff fps_mult_nth
   350   by (simp add: fps_const_def mult_delta_right setsum_delta')
   351 
   352 lemma fps_mult_left_const_nth [simp]: "(fps_const (c::'a::semiring_1) * f)$n = c* f$n"
   353   by (simp add: fps_mult_nth mult_delta_left setsum_delta)
   354 
   355 lemma fps_mult_right_const_nth [simp]: "(f * fps_const (c::'a::semiring_1))$n = f$n * c"
   356   by (simp add: fps_mult_nth mult_delta_right setsum_delta')
   357 
   358 subsection {* Formal power series form an integral domain*}
   359 
   360 instance fps :: (ring) ring ..
   361 
   362 instance fps :: (ring_1) ring_1
   363   by (intro_classes, auto simp add: diff_minus left_distrib)
   364 
   365 instance fps :: (comm_ring_1) comm_ring_1
   366   by (intro_classes, auto simp add: diff_minus left_distrib)
   367 
   368 instance fps :: (ring_no_zero_divisors) ring_no_zero_divisors
   369 proof
   370   fix a b :: "'a fps"
   371   assume a0: "a \<noteq> 0" and b0: "b \<noteq> 0"
   372   then obtain i j where i: "a$i\<noteq>0" "\<forall>k<i. a$k=0"
   373     and j: "b$j \<noteq>0" "\<forall>k<j. b$k =0" unfolding fps_nonzero_nth_minimal
   374     by blast+
   375   have "(a * b) $ (i+j) = (\<Sum>k=0..i+j. a$k * b$(i+j-k))"
   376     by (rule fps_mult_nth)
   377   also have "\<dots> = (a$i * b$(i+j-i)) + (\<Sum>k\<in>{0..i+j}-{i}. a$k * b$(i+j-k))"
   378     by (rule setsum_diff1') simp_all
   379   also have "(\<Sum>k\<in>{0..i+j}-{i}. a$k * b$(i+j-k)) = 0"
   380     proof (rule setsum_0' [rule_format])
   381       fix k assume "k \<in> {0..i+j} - {i}"
   382       then have "k < i \<or> i+j-k < j" by auto
   383       then show "a$k * b$(i+j-k) = 0" using i j by auto
   384     qed
   385   also have "a$i * b$(i+j-i) + 0 = a$i * b$j" by simp
   386   also have "a$i * b$j \<noteq> 0" using i j by simp
   387   finally have "(a*b) $ (i+j) \<noteq> 0" .
   388   then show "a*b \<noteq> 0" unfolding fps_nonzero_nth by blast
   389 qed
   390 
   391 instance fps :: (idom) idom ..
   392 
   393 instantiation fps :: (comm_ring_1) number_ring
   394 begin
   395 definition number_of_fps_def: "(number_of k::'a fps) = of_int k"
   396 
   397 instance proof
   398 qed (rule number_of_fps_def)
   399 end
   400 
   401 lemma number_of_fps_const: "(number_of k::('a::comm_ring_1) fps) = fps_const (of_int k)"
   402   
   403 proof(induct k rule: int_induct[where k=0])
   404   case base thus ?case unfolding number_of_fps_def of_int_0 by simp
   405 next
   406   case (step1 i) thus ?case unfolding number_of_fps_def 
   407     by (simp add: fps_const_add[symmetric] del: fps_const_add)
   408 next
   409   case (step2 i) thus ?case unfolding number_of_fps_def 
   410     by (simp add: fps_const_sub[symmetric] del: fps_const_sub)
   411 qed
   412 subsection{* The eXtractor series X*}
   413 
   414 lemma minus_one_power_iff: "(- (1::'a :: {comm_ring_1})) ^ n = (if even n then 1 else - 1)"
   415   by (induct n, auto)
   416 
   417 definition "X = Abs_fps (\<lambda>n. if n = 1 then 1 else 0)"
   418 lemma X_mult_nth[simp]: "(X * (f :: ('a::semiring_1) fps)) $n = (if n = 0 then 0 else f $ (n - 1))"
   419 proof-
   420   {assume n: "n \<noteq> 0"
   421     have fN: "finite {0 .. n}" by simp
   422     have "(X * f) $n = (\<Sum>i = 0..n. X $ i * f $ (n - i))" by (simp add: fps_mult_nth)
   423     also have "\<dots> = f $ (n - 1)"
   424       using n by (simp add: X_def mult_delta_left setsum_delta [OF fN])
   425   finally have ?thesis using n by simp }
   426   moreover
   427   {assume n: "n=0" hence ?thesis by (simp add: fps_mult_nth X_def)}
   428   ultimately show ?thesis by blast
   429 qed
   430 
   431 lemma X_mult_right_nth[simp]: "((f :: ('a::comm_semiring_1) fps) * X) $n = (if n = 0 then 0 else f $ (n - 1))"
   432   by (metis X_mult_nth mult_commute)
   433 
   434 lemma X_power_iff: "X^k = Abs_fps (\<lambda>n. if n = k then (1::'a::comm_ring_1) else 0)"
   435 proof(induct k)
   436   case 0 thus ?case by (simp add: X_def fps_eq_iff)
   437 next
   438   case (Suc k)
   439   {fix m
   440     have "(X^Suc k) $ m = (if m = 0 then (0::'a) else (X^k) $ (m - 1))"
   441       by (simp add: power_Suc del: One_nat_def)
   442     then     have "(X^Suc k) $ m = (if m = Suc k then (1::'a) else 0)"
   443       using Suc.hyps by (auto cong del: if_weak_cong)}
   444   then show ?case by (simp add: fps_eq_iff)
   445 qed
   446 
   447 lemma X_power_mult_nth: "(X^k * (f :: ('a::comm_ring_1) fps)) $n = (if n < k then 0 else f $ (n - k))"
   448   apply (induct k arbitrary: n)
   449   apply (simp)
   450   unfolding power_Suc mult_assoc
   451   by (case_tac n, auto)
   452 
   453 lemma X_power_mult_right_nth: "((f :: ('a::comm_ring_1) fps) * X^k) $n = (if n < k then 0 else f $ (n - k))"
   454   by (metis X_power_mult_nth mult_commute)
   455 
   456 
   457 
   458   
   459 subsection{* Formal Power series form a metric space *}
   460 
   461 definition (in dist) ball_def: "ball x r = {y. dist y x < r}"
   462 instantiation fps :: (comm_ring_1) dist
   463 begin
   464 
   465 definition dist_fps_def: "dist (a::'a fps) b = (if (\<exists>n. a$n \<noteq> b$n) then inverse (2 ^ The (leastP (\<lambda>n. a$n \<noteq> b$n))) else 0)"
   466 
   467 lemma dist_fps_ge0: "dist (a::'a fps) b \<ge> 0"
   468   by (simp add: dist_fps_def)
   469 
   470 lemma dist_fps_sym: "dist (a::'a fps) b = dist b a"
   471   apply (auto simp add: dist_fps_def)
   472   apply (rule cong[OF refl, where x="(\<lambda>n\<Colon>nat. a $ n \<noteq> b $ n)"])
   473   apply (rule ext)
   474   by auto
   475 instance ..
   476 end
   477 
   478 lemma fps_nonzero_least_unique: assumes a0: "a \<noteq> 0"
   479   shows "\<exists>! n. leastP (\<lambda>n. a$n \<noteq> 0) n"
   480 proof-
   481   from fps_nonzero_nth_minimal[of a] a0
   482   obtain n where n: "a$n \<noteq> 0" "\<forall>m < n. a$m = 0" by blast
   483   from n have ln: "leastP (\<lambda>n. a$n \<noteq> 0) n" 
   484     by (auto simp add: leastP_def setge_def not_le[symmetric])
   485   moreover
   486   {fix m assume "leastP (\<lambda>n. a$n \<noteq> 0) m"
   487     then have "m = n" using ln
   488       apply (auto simp add: leastP_def setge_def)
   489       apply (erule allE[where x=n])
   490       apply (erule allE[where x=m])
   491       by simp}
   492   ultimately show ?thesis by blast
   493 qed
   494 
   495 lemma fps_eq_least_unique: assumes ab: "(a::('a::ab_group_add) fps) \<noteq> b"
   496   shows "\<exists>! n. leastP (\<lambda>n. a$n \<noteq> b$n) n"
   497 using fps_nonzero_least_unique[of "a - b"] ab
   498 by auto
   499 
   500 instantiation fps :: (comm_ring_1) metric_space
   501 begin
   502 
   503 definition open_fps_def: "open (S :: 'a fps set) = (\<forall>a \<in> S. \<exists>r. r >0 \<and> ball a r \<subseteq> S)"
   504 
   505 instance
   506 proof
   507   fix S :: "'a fps set" 
   508   show "open S = (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   509     by (auto simp add: open_fps_def ball_def subset_eq)
   510 next
   511 {  fix a b :: "'a fps"
   512   {assume ab: "a = b"
   513    then have "\<not> (\<exists>n. a$n \<noteq> b$n)" by simp
   514    then have "dist a b = 0" by (simp add: dist_fps_def)}
   515  moreover
   516  {assume d: "dist a b = 0"
   517    then have "\<forall>n. a$n = b$n" 
   518      by - (rule ccontr, simp add: dist_fps_def)
   519    then have "a = b" by (simp add: fps_eq_iff)}
   520  ultimately show "dist a b =0 \<longleftrightarrow> a = b" by blast}
   521 note th = this
   522 from th have th'[simp]: "\<And>a::'a fps. dist a a = 0" by simp
   523   fix a b c :: "'a fps"
   524   {assume ab: "a = b" then have d0: "dist a b = 0"  unfolding th .
   525     then have "dist a b \<le> dist a c + dist b c" 
   526       using dist_fps_ge0[of a c] dist_fps_ge0[of b c] by simp}
   527   moreover
   528   {assume c: "c = a \<or> c = b" then have "dist a b \<le> dist a c + dist b c"
   529       by (cases "c=a", simp_all add: th dist_fps_sym) }
   530   moreover
   531   {assume ab: "a \<noteq> b" and ac: "a \<noteq> c" and bc: "b \<noteq> c"
   532     let ?P = "\<lambda>a b n. a$n \<noteq> b$n"
   533     from fps_eq_least_unique[OF ab] fps_eq_least_unique[OF ac] 
   534       fps_eq_least_unique[OF bc]
   535     obtain nab nac nbc where nab: "leastP (?P a b) nab" 
   536       and nac: "leastP (?P a c) nac" 
   537       and nbc: "leastP (?P b c) nbc" by blast
   538     from nab have nab': "\<And>m. m < nab \<Longrightarrow> a$m = b$m" "a$nab \<noteq> b$nab"
   539       by (auto simp add: leastP_def setge_def)
   540     from nac have nac': "\<And>m. m < nac \<Longrightarrow> a$m = c$m" "a$nac \<noteq> c$nac"
   541       by (auto simp add: leastP_def setge_def)
   542     from nbc have nbc': "\<And>m. m < nbc \<Longrightarrow> b$m = c$m" "b$nbc \<noteq> c$nbc"
   543       by (auto simp add: leastP_def setge_def)
   544 
   545     have th0: "\<And>(a::'a fps) b. a \<noteq> b \<longleftrightarrow> (\<exists>n. a$n \<noteq> b$n)"
   546       by (simp add: fps_eq_iff)
   547     from ab ac bc nab nac nbc 
   548     have dab: "dist a b = inverse (2 ^ nab)" 
   549       and dac: "dist a c = inverse (2 ^ nac)" 
   550       and dbc: "dist b c = inverse (2 ^ nbc)"
   551       unfolding th0
   552       apply (simp_all add: dist_fps_def)
   553       apply (erule the1_equality[OF fps_eq_least_unique[OF ab]])
   554       apply (erule the1_equality[OF fps_eq_least_unique[OF ac]])
   555       by (erule the1_equality[OF fps_eq_least_unique[OF bc]])
   556     from ab ac bc have nz: "dist a b \<noteq> 0" "dist a c \<noteq> 0" "dist b c \<noteq> 0"
   557       unfolding th by simp_all
   558     from nz have pos: "dist a b > 0" "dist a c > 0" "dist b c > 0"
   559       using dist_fps_ge0[of a b] dist_fps_ge0[of a c] dist_fps_ge0[of b c] 
   560       by auto
   561     have th1: "\<And>n. (2::real)^n >0" by auto
   562     {assume h: "dist a b > dist a c + dist b c"
   563       then have gt: "dist a b > dist a c" "dist a b > dist b c"
   564 	using pos by auto
   565       from gt have gtn: "nab < nbc" "nab < nac"
   566 	unfolding dab dbc dac by (auto simp add: th1)
   567       from nac'(1)[OF gtn(2)] nbc'(1)[OF gtn(1)]
   568       have "a$nab = b$nab" by simp
   569       with nab'(2) have False  by simp}
   570     then have "dist a b \<le> dist a c + dist b c"
   571       by (auto simp add: not_le[symmetric]) }
   572   ultimately show "dist a b \<le> dist a c + dist b c" by blast
   573 qed
   574   
   575 end
   576 
   577 text{* The infinite sums and justification of the notation in textbooks*}
   578 
   579 lemma reals_power_lt_ex: assumes xp: "x > 0" and y1: "(y::real) > 1"
   580   shows "\<exists>k>0. (1/y)^k < x"
   581 proof-
   582   have yp: "y > 0" using y1 by simp
   583   from reals_Archimedean2[of "max 0 (- log y x) + 1"]
   584   obtain k::nat where k: "real k > max 0 (- log y x) + 1" by blast
   585   from k have kp: "k > 0" by simp
   586   from k have "real k > - log y x" by simp
   587   then have "ln y * real k > - ln x" unfolding log_def
   588     using ln_gt_zero_iff[OF yp] y1
   589     by (simp add: minus_divide_left field_simps del:minus_divide_left[symmetric] )
   590   then have "ln y * real k + ln x > 0" by simp
   591   then have "exp (real k * ln y + ln x) > exp 0"
   592     by (simp add: mult_ac)
   593   then have "y ^ k * x > 1"
   594     unfolding exp_zero exp_add exp_real_of_nat_mult
   595     exp_ln[OF xp] exp_ln[OF yp] by simp
   596   then have "x > (1/y)^k" using yp 
   597     by (simp add: field_simps nonzero_power_divide )
   598   then show ?thesis using kp by blast
   599 qed
   600 lemma X_nth[simp]: "X$n = (if n = 1 then 1 else 0)" by (simp add: X_def)
   601 lemma X_power_nth[simp]: "(X^k) $n = (if n = k then 1 else (0::'a::comm_ring_1))"
   602   by (simp add: X_power_iff)
   603  
   604 
   605 lemma fps_sum_rep_nth: "(setsum (%i. fps_const(a$i)*X^i) {0..m})$n = (if n \<le> m then a$n else (0::'a::comm_ring_1))"
   606   apply (auto simp add: fps_eq_iff fps_setsum_nth X_power_nth cond_application_beta cond_value_iff  cong del: if_weak_cong)
   607   by (simp add: setsum_delta')
   608   
   609 lemma fps_notation: 
   610   "(%n. setsum (%i. fps_const(a$i) * X^i) {0..n}) ----> a" (is "?s ----> a")
   611 proof-
   612     {fix r:: real
   613       assume rp: "r > 0"
   614       have th0: "(2::real) > 1" by simp
   615       from reals_power_lt_ex[OF rp th0] 
   616       obtain n0 where n0: "(1/2)^n0 < r" "n0 > 0" by blast
   617       {fix n::nat
   618 	assume nn0: "n \<ge> n0"
   619 	then have thnn0: "(1/2)^n <= (1/2 :: real)^n0"
   620 	  by (auto intro: power_decreasing)
   621 	{assume "?s n = a" then have "dist (?s n) a < r" 
   622 	    unfolding dist_eq_0_iff[of "?s n" a, symmetric]
   623 	    using rp by (simp del: dist_eq_0_iff)}
   624 	moreover
   625 	{assume neq: "?s n \<noteq> a"
   626 	  from fps_eq_least_unique[OF neq] 
   627 	  obtain k where k: "leastP (\<lambda>i. ?s n $ i \<noteq> a$i) k" by blast
   628 	  have th0: "\<And>(a::'a fps) b. a \<noteq> b \<longleftrightarrow> (\<exists>n. a$n \<noteq> b$n)"
   629 	    by (simp add: fps_eq_iff)
   630 	  from neq have dth: "dist (?s n) a = (1/2)^k"
   631 	    unfolding th0 dist_fps_def
   632 	    unfolding the1_equality[OF fps_eq_least_unique[OF neq], OF k]
   633 	    by (auto simp add: inverse_eq_divide power_divide)
   634 
   635 	  from k have kn: "k > n"
   636 	    apply (simp add: leastP_def setge_def fps_sum_rep_nth)
   637 	    by (cases "k \<le> n", auto)
   638 	  then have "dist (?s n) a < (1/2)^n" unfolding dth
   639 	    by (auto intro: power_strict_decreasing)
   640 	  also have "\<dots> <= (1/2)^n0" using nn0
   641 	    by (auto intro: power_decreasing)
   642 	  also have "\<dots> < r" using n0 by simp
   643 	  finally have "dist (?s n) a < r" .}
   644 	ultimately have "dist (?s n) a < r" by blast}
   645       then have "\<exists>n0. \<forall> n \<ge> n0. dist (?s n) a < r " by blast}
   646     then show ?thesis  unfolding  LIMSEQ_def by blast
   647   qed
   648 
   649 subsection{* Inverses of formal power series *}
   650 
   651 declare setsum_cong[fundef_cong]
   652 
   653 
   654 instantiation fps :: ("{comm_monoid_add,inverse, times, uminus}") inverse
   655 begin
   656 
   657 fun natfun_inverse:: "'a fps \<Rightarrow> nat \<Rightarrow> 'a" where
   658   "natfun_inverse f 0 = inverse (f$0)"
   659 | "natfun_inverse f n = - inverse (f$0) * setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}"
   660 
   661 definition fps_inverse_def:
   662   "inverse f = (if f$0 = 0 then 0 else Abs_fps (natfun_inverse f))"
   663 definition fps_divide_def: "divide = (\<lambda>(f::'a fps) g. f * inverse g)"
   664 instance ..
   665 end
   666 
   667 lemma fps_inverse_zero[simp]:
   668   "inverse (0 :: 'a::{comm_monoid_add,inverse, times, uminus} fps) = 0"
   669   by (simp add: fps_ext fps_inverse_def)
   670 
   671 lemma fps_inverse_one[simp]: "inverse (1 :: 'a::{division_ring,zero_neq_one} fps) = 1"
   672   apply (auto simp add: expand_fps_eq fps_inverse_def)
   673   by (case_tac n, auto)
   674 
   675 instance fps :: ("{comm_monoid_add,inverse, times, uminus}")  division_by_zero
   676   by default (rule fps_inverse_zero)
   677 
   678 lemma inverse_mult_eq_1[intro]: assumes f0: "f$0 \<noteq> (0::'a::field)"
   679   shows "inverse f * f = 1"
   680 proof-
   681   have c: "inverse f * f = f * inverse f" by (simp add: mult_commute)
   682   from f0 have ifn: "\<And>n. inverse f $ n = natfun_inverse f n"
   683     by (simp add: fps_inverse_def)
   684   from f0 have th0: "(inverse f * f) $ 0 = 1"
   685     by (simp add: fps_mult_nth fps_inverse_def)
   686   {fix n::nat assume np: "n >0 "
   687     from np have eq: "{0..n} = {0} \<union> {1 .. n}" by auto
   688     have d: "{0} \<inter> {1 .. n} = {}" by auto
   689     have f: "finite {0::nat}" "finite {1..n}" by auto
   690     from f0 np have th0: "- (inverse f$n) =
   691       (setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}) / (f$0)"
   692       by (cases n, simp, simp add: divide_inverse fps_inverse_def)
   693     from th0[symmetric, unfolded nonzero_divide_eq_eq[OF f0]]
   694     have th1: "setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n} =
   695       - (f$0) * (inverse f)$n"
   696       by (simp add: ring_simps)
   697     have "(f * inverse f) $ n = (\<Sum>i = 0..n. f $i * natfun_inverse f (n - i))"
   698       unfolding fps_mult_nth ifn ..
   699     also have "\<dots> = f$0 * natfun_inverse f n
   700       + (\<Sum>i = 1..n. f$i * natfun_inverse f (n-i))"
   701       unfolding setsum_Un_disjoint[OF f d, unfolded eq[symmetric]]
   702       by simp
   703     also have "\<dots> = 0" unfolding th1 ifn by simp
   704     finally have "(inverse f * f)$n = 0" unfolding c . }
   705   with th0 show ?thesis by (simp add: fps_eq_iff)
   706 qed
   707 
   708 lemma fps_inverse_0_iff[simp]: "(inverse f)$0 = (0::'a::division_ring) \<longleftrightarrow> f$0 = 0"
   709   by (simp add: fps_inverse_def nonzero_imp_inverse_nonzero)
   710 
   711 lemma fps_inverse_eq_0_iff[simp]: "inverse f = (0:: ('a::field) fps) \<longleftrightarrow> f $0 = 0"
   712 proof-
   713   {assume "f$0 = 0" hence "inverse f = 0" by (simp add: fps_inverse_def)}
   714   moreover
   715   {assume h: "inverse f = 0" and c: "f $0 \<noteq> 0"
   716     from inverse_mult_eq_1[OF c] h have False by simp}
   717   ultimately show ?thesis by blast
   718 qed
   719 
   720 lemma fps_inverse_idempotent[intro]: assumes f0: "f$0 \<noteq> (0::'a::field)"
   721   shows "inverse (inverse f) = f"
   722 proof-
   723   from f0 have if0: "inverse f $ 0 \<noteq> 0" by simp
   724   from inverse_mult_eq_1[OF f0] inverse_mult_eq_1[OF if0]
   725   have th0: "inverse f * f = inverse f * inverse (inverse f)"   by (simp add: mult_ac)
   726   then show ?thesis using f0 unfolding mult_cancel_left by simp
   727 qed
   728 
   729 lemma fps_inverse_unique: assumes f0: "f$0 \<noteq> (0::'a::field)" and fg: "f*g = 1"
   730   shows "inverse f = g"
   731 proof-
   732   from inverse_mult_eq_1[OF f0] fg
   733   have th0: "inverse f * f = g * f" by (simp add: mult_ac)
   734   then show ?thesis using f0  unfolding mult_cancel_right
   735     by (auto simp add: expand_fps_eq)
   736 qed
   737 
   738 lemma fps_inverse_gp: "inverse (Abs_fps(\<lambda>n. (1::'a::field)))
   739   = Abs_fps (\<lambda>n. if n= 0 then 1 else if n=1 then - 1 else 0)"
   740   apply (rule fps_inverse_unique)
   741   apply simp
   742   apply (simp add: fps_eq_iff fps_mult_nth)
   743 proof(clarsimp)
   744   fix n::nat assume n: "n > 0"
   745   let ?f = "\<lambda>i. if n = i then (1\<Colon>'a) else if n - i = 1 then - 1 else 0"
   746   let ?g = "\<lambda>i. if i = n then 1 else if i=n - 1 then - 1 else 0"
   747   let ?h = "\<lambda>i. if i=n - 1 then - 1 else 0"
   748   have th1: "setsum ?f {0..n} = setsum ?g {0..n}"
   749     by (rule setsum_cong2) auto
   750   have th2: "setsum ?g {0..n - 1} = setsum ?h {0..n - 1}"
   751     using n apply - by (rule setsum_cong2) auto
   752   have eq: "{0 .. n} = {0.. n - 1} \<union> {n}" by auto
   753   from n have d: "{0.. n - 1} \<inter> {n} = {}" by auto
   754   have f: "finite {0.. n - 1}" "finite {n}" by auto
   755   show "setsum ?f {0..n} = 0"
   756     unfolding th1
   757     apply (simp add: setsum_Un_disjoint[OF f d, unfolded eq[symmetric]] del: One_nat_def)
   758     unfolding th2
   759     by(simp add: setsum_delta)
   760 qed
   761 
   762 subsection{* Formal Derivatives, and the MacLaurin theorem around 0*}
   763 
   764 definition "fps_deriv f = Abs_fps (\<lambda>n. of_nat (n + 1) * f $ (n + 1))"
   765 
   766 lemma fps_deriv_nth[simp]: "fps_deriv f $ n = of_nat (n +1) * f $ (n+1)" by (simp add: fps_deriv_def)
   767 
   768 lemma fps_deriv_linear[simp]: "fps_deriv (fps_const (a::'a::comm_semiring_1) * f + fps_const b * g) = fps_const a * fps_deriv f + fps_const b * fps_deriv g"
   769   unfolding fps_eq_iff fps_add_nth  fps_const_mult_left fps_deriv_nth by (simp add: ring_simps)
   770 
   771 lemma fps_deriv_mult[simp]:
   772   fixes f :: "('a :: comm_ring_1) fps"
   773   shows "fps_deriv (f * g) = f * fps_deriv g + fps_deriv f * g"
   774 proof-
   775   let ?D = "fps_deriv"
   776   {fix n::nat
   777     let ?Zn = "{0 ..n}"
   778     let ?Zn1 = "{0 .. n + 1}"
   779     let ?f = "\<lambda>i. i + 1"
   780     have fi: "inj_on ?f {0..n}" by (simp add: inj_on_def)
   781     have eq: "{1.. n+1} = ?f ` {0..n}" by auto
   782     let ?g = "\<lambda>i. of_nat (i+1) * g $ (i+1) * f $ (n - i) +
   783         of_nat (i+1)* f $ (i+1) * g $ (n - i)"
   784     let ?h = "\<lambda>i. of_nat i * g $ i * f $ ((n+1) - i) +
   785         of_nat i* f $ i * g $ ((n + 1) - i)"
   786     {fix k assume k: "k \<in> {0..n}"
   787       have "?h (k + 1) = ?g k" using k by auto}
   788     note th0 = this
   789     have eq': "{0..n +1}- {1 .. n+1} = {0}" by auto
   790     have s0: "setsum (\<lambda>i. of_nat i * f $ i * g $ (n + 1 - i)) ?Zn1 = setsum (\<lambda>i. of_nat (n + 1 - i) * f $ (n + 1 - i) * g $ i) ?Zn1"
   791       apply (rule setsum_reindex_cong[where f="\<lambda>i. n + 1 - i"])
   792       apply (simp add: inj_on_def Ball_def)
   793       apply presburger
   794       apply (rule set_ext)
   795       apply (presburger add: image_iff)
   796       by simp
   797     have s1: "setsum (\<lambda>i. f $ i * g $ (n + 1 - i)) ?Zn1 = setsum (\<lambda>i. f $ (n + 1 - i) * g $ i) ?Zn1"
   798       apply (rule setsum_reindex_cong[where f="\<lambda>i. n + 1 - i"])
   799       apply (simp add: inj_on_def Ball_def)
   800       apply presburger
   801       apply (rule set_ext)
   802       apply (presburger add: image_iff)
   803       by simp
   804     have "(f * ?D g + ?D f * g)$n = (?D g * f + ?D f * g)$n" by (simp only: mult_commute)
   805     also have "\<dots> = (\<Sum>i = 0..n. ?g i)"
   806       by (simp add: fps_mult_nth setsum_addf[symmetric])
   807     also have "\<dots> = setsum ?h {1..n+1}"
   808       using th0 setsum_reindex_cong[OF fi eq, of "?g" "?h"] by auto
   809     also have "\<dots> = setsum ?h {0..n+1}"
   810       apply (rule setsum_mono_zero_left)
   811       apply simp
   812       apply (simp add: subset_eq)
   813       unfolding eq'
   814       by simp
   815     also have "\<dots> = (fps_deriv (f * g)) $ n"
   816       apply (simp only: fps_deriv_nth fps_mult_nth setsum_addf)
   817       unfolding s0 s1
   818       unfolding setsum_addf[symmetric] setsum_right_distrib
   819       apply (rule setsum_cong2)
   820       by (auto simp add: of_nat_diff ring_simps)
   821     finally have "(f * ?D g + ?D f * g) $ n = ?D (f*g) $ n" .}
   822   then show ?thesis unfolding fps_eq_iff by auto
   823 qed
   824 
   825 lemma fps_deriv_X[simp]: "fps_deriv X = 1"
   826   by (simp add: fps_deriv_def X_def fps_eq_iff)
   827 
   828 lemma fps_deriv_neg[simp]: "fps_deriv (- (f:: ('a:: comm_ring_1) fps)) = - (fps_deriv f)"
   829   by (simp add: fps_eq_iff fps_deriv_def)
   830 lemma fps_deriv_add[simp]: "fps_deriv ((f:: ('a::comm_ring_1) fps) + g) = fps_deriv f + fps_deriv g"
   831   using fps_deriv_linear[of 1 f 1 g] by simp
   832 
   833 lemma fps_deriv_sub[simp]: "fps_deriv ((f:: ('a::comm_ring_1) fps) - g) = fps_deriv f - fps_deriv g"
   834   unfolding diff_minus by simp
   835 
   836 lemma fps_deriv_const[simp]: "fps_deriv (fps_const c) = 0"
   837   by (simp add: fps_ext fps_deriv_def fps_const_def)
   838 
   839 lemma fps_deriv_mult_const_left[simp]: "fps_deriv (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_deriv f"
   840   by simp
   841 
   842 lemma fps_deriv_0[simp]: "fps_deriv 0 = 0"
   843   by (simp add: fps_deriv_def fps_eq_iff)
   844 
   845 lemma fps_deriv_1[simp]: "fps_deriv 1 = 0"
   846   by (simp add: fps_deriv_def fps_eq_iff )
   847 
   848 lemma fps_deriv_mult_const_right[simp]: "fps_deriv (f * fps_const (c::'a::comm_ring_1)) = fps_deriv f * fps_const c"
   849   by simp
   850 
   851 lemma fps_deriv_setsum: "fps_deriv (setsum f S) = setsum (\<lambda>i. fps_deriv (f i :: ('a::comm_ring_1) fps)) S"
   852 proof-
   853   {assume "\<not> finite S" hence ?thesis by simp}
   854   moreover
   855   {assume fS: "finite S"
   856     have ?thesis  by (induct rule: finite_induct[OF fS], simp_all)}
   857   ultimately show ?thesis by blast
   858 qed
   859 
   860 lemma fps_deriv_eq_0_iff[simp]: "fps_deriv f = 0 \<longleftrightarrow> (f = fps_const (f$0 :: 'a::{idom,semiring_char_0}))"
   861 proof-
   862   {assume "f= fps_const (f$0)" hence "fps_deriv f = fps_deriv (fps_const (f$0))" by simp
   863     hence "fps_deriv f = 0" by simp }
   864   moreover
   865   {assume z: "fps_deriv f = 0"
   866     hence "\<forall>n. (fps_deriv f)$n = 0" by simp
   867     hence "\<forall>n. f$(n+1) = 0" by (simp del: of_nat_Suc of_nat_add One_nat_def)
   868     hence "f = fps_const (f$0)"
   869       apply (clarsimp simp add: fps_eq_iff fps_const_def)
   870       apply (erule_tac x="n - 1" in allE)
   871       by simp}
   872   ultimately show ?thesis by blast
   873 qed
   874 
   875 lemma fps_deriv_eq_iff:
   876   fixes f:: "('a::{idom,semiring_char_0}) fps"
   877   shows "fps_deriv f = fps_deriv g \<longleftrightarrow> (f = fps_const(f$0 - g$0) + g)"
   878 proof-
   879   have "fps_deriv f = fps_deriv g \<longleftrightarrow> fps_deriv (f - g) = 0" by simp
   880   also have "\<dots> \<longleftrightarrow> f - g = fps_const ((f-g)$0)" unfolding fps_deriv_eq_0_iff ..
   881   finally show ?thesis by (simp add: ring_simps)
   882 qed
   883 
   884 lemma fps_deriv_eq_iff_ex: "(fps_deriv f = fps_deriv g) \<longleftrightarrow> (\<exists>(c::'a::{idom,semiring_char_0}). f = fps_const c + g)"
   885   apply auto unfolding fps_deriv_eq_iff by blast
   886 
   887 
   888 fun fps_nth_deriv :: "nat \<Rightarrow> ('a::semiring_1) fps \<Rightarrow> 'a fps" where
   889   "fps_nth_deriv 0 f = f"
   890 | "fps_nth_deriv (Suc n) f = fps_nth_deriv n (fps_deriv f)"
   891 
   892 lemma fps_nth_deriv_commute: "fps_nth_deriv (Suc n) f = fps_deriv (fps_nth_deriv n f)"
   893   by (induct n arbitrary: f, auto)
   894 
   895 lemma fps_nth_deriv_linear[simp]: "fps_nth_deriv n (fps_const (a::'a::comm_semiring_1) * f + fps_const b * g) = fps_const a * fps_nth_deriv n f + fps_const b * fps_nth_deriv n g"
   896   by (induct n arbitrary: f g, auto simp add: fps_nth_deriv_commute)
   897 
   898 lemma fps_nth_deriv_neg[simp]: "fps_nth_deriv n (- (f:: ('a:: comm_ring_1) fps)) = - (fps_nth_deriv n f)"
   899   by (induct n arbitrary: f, simp_all)
   900 
   901 lemma fps_nth_deriv_add[simp]: "fps_nth_deriv n ((f:: ('a::comm_ring_1) fps) + g) = fps_nth_deriv n f + fps_nth_deriv n g"
   902   using fps_nth_deriv_linear[of n 1 f 1 g] by simp
   903 
   904 lemma fps_nth_deriv_sub[simp]: "fps_nth_deriv n ((f:: ('a::comm_ring_1) fps) - g) = fps_nth_deriv n f - fps_nth_deriv n g"
   905   unfolding diff_minus fps_nth_deriv_add by simp
   906 
   907 lemma fps_nth_deriv_0[simp]: "fps_nth_deriv n 0 = 0"
   908   by (induct n, simp_all )
   909 
   910 lemma fps_nth_deriv_1[simp]: "fps_nth_deriv n 1 = (if n = 0 then 1 else 0)"
   911   by (induct n, simp_all )
   912 
   913 lemma fps_nth_deriv_const[simp]: "fps_nth_deriv n (fps_const c) = (if n = 0 then fps_const c else 0)"
   914   by (cases n, simp_all)
   915 
   916 lemma fps_nth_deriv_mult_const_left[simp]: "fps_nth_deriv n (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_nth_deriv n f"
   917   using fps_nth_deriv_linear[of n "c" f 0 0 ] by simp
   918 
   919 lemma fps_nth_deriv_mult_const_right[simp]: "fps_nth_deriv n (f * fps_const (c::'a::comm_ring_1)) = fps_nth_deriv n f * fps_const c"
   920   using fps_nth_deriv_linear[of n "c" f 0 0] by (simp add: mult_commute)
   921 
   922 lemma fps_nth_deriv_setsum: "fps_nth_deriv n (setsum f S) = setsum (\<lambda>i. fps_nth_deriv n (f i :: ('a::comm_ring_1) fps)) S"
   923 proof-
   924   {assume "\<not> finite S" hence ?thesis by simp}
   925   moreover
   926   {assume fS: "finite S"
   927     have ?thesis  by (induct rule: finite_induct[OF fS], simp_all)}
   928   ultimately show ?thesis by blast
   929 qed
   930 
   931 lemma fps_deriv_maclauren_0: "(fps_nth_deriv k (f:: ('a::comm_semiring_1) fps)) $ 0 = of_nat (fact k) * f$(k)"
   932   by (induct k arbitrary: f) (auto simp add: ring_simps of_nat_mult)
   933 
   934 subsection {* Powers*}
   935 
   936 lemma fps_power_zeroth_eq_one: "a$0 =1 \<Longrightarrow> a^n $ 0 = (1::'a::semiring_1)"
   937   by (induct n, auto simp add: expand_fps_eq fps_mult_nth)
   938 
   939 lemma fps_power_first_eq: "(a:: 'a::comm_ring_1 fps)$0 =1 \<Longrightarrow> a^n $ 1 = of_nat n * a$1"
   940 proof(induct n)
   941   case 0 thus ?case by simp
   942 next
   943   case (Suc n)
   944   note h = Suc.hyps[OF `a$0 = 1`]
   945   show ?case unfolding power_Suc fps_mult_nth
   946     using h `a$0 = 1`  fps_power_zeroth_eq_one[OF `a$0=1`] by (simp add: ring_simps)
   947 qed
   948 
   949 lemma startsby_one_power:"a $ 0 = (1::'a::comm_ring_1) \<Longrightarrow> a^n $ 0 = 1"
   950   by (induct n, auto simp add: fps_mult_nth)
   951 
   952 lemma startsby_zero_power:"a $0 = (0::'a::comm_ring_1) \<Longrightarrow> n > 0 \<Longrightarrow> a^n $0 = 0"
   953   by (induct n, auto simp add: fps_mult_nth)
   954 
   955 lemma startsby_power:"a $0 = (v::'a::{comm_ring_1}) \<Longrightarrow> a^n $0 = v^n"
   956   by (induct n, auto simp add: fps_mult_nth power_Suc)
   957 
   958 lemma startsby_zero_power_iff[simp]:
   959   "a^n $0 = (0::'a::{idom}) \<longleftrightarrow> (n \<noteq> 0 \<and> a$0 = 0)"
   960 apply (rule iffI)
   961 apply (induct n, auto simp add: power_Suc fps_mult_nth)
   962 by (rule startsby_zero_power, simp_all)
   963 
   964 lemma startsby_zero_power_prefix:
   965   assumes a0: "a $0 = (0::'a::idom)"
   966   shows "\<forall>n < k. a ^ k $ n = 0"
   967   using a0
   968 proof(induct k rule: nat_less_induct)
   969   fix k assume H: "\<forall>m<k. a $0 =  0 \<longrightarrow> (\<forall>n<m. a ^ m $ n = 0)" and a0: "a $0 = (0\<Colon>'a)"
   970   let ?ths = "\<forall>m<k. a ^ k $ m = 0"
   971   {assume "k = 0" then have ?ths by simp}
   972   moreover
   973   {fix l assume k: "k = Suc l"
   974     {fix m assume mk: "m < k"
   975       {assume "m=0" hence "a^k $ m = 0" using startsby_zero_power[of a k] k a0
   976 	  by simp}
   977       moreover
   978       {assume m0: "m \<noteq> 0"
   979 	have "a ^k $ m = (a^l * a) $m" by (simp add: k power_Suc mult_commute)
   980 	also have "\<dots> = (\<Sum>i = 0..m. a ^ l $ i * a $ (m - i))" by (simp add: fps_mult_nth)
   981 	also have "\<dots> = 0" apply (rule setsum_0')
   982 	  apply auto
   983 	  apply (case_tac "aa = m")
   984 	  using a0
   985 	  apply simp
   986 	  apply (rule H[rule_format])
   987 	  using a0 k mk by auto
   988 	finally have "a^k $ m = 0" .}
   989     ultimately have "a^k $ m = 0" by blast}
   990     hence ?ths by blast}
   991   ultimately show ?ths by (cases k, auto)
   992 qed
   993 
   994 lemma startsby_zero_setsum_depends:
   995   assumes a0: "a $0 = (0::'a::idom)" and kn: "n \<ge> k"
   996   shows "setsum (\<lambda>i. (a ^ i)$k) {0 .. n} = setsum (\<lambda>i. (a ^ i)$k) {0 .. k}"
   997   apply (rule setsum_mono_zero_right)
   998   using kn apply auto
   999   apply (rule startsby_zero_power_prefix[rule_format, OF a0])
  1000   by arith
  1001 
  1002 lemma startsby_zero_power_nth_same: assumes a0: "a$0 = (0::'a::{idom})"
  1003   shows "a^n $ n = (a$1) ^ n"
  1004 proof(induct n)
  1005   case 0 thus ?case by (simp add: power_0)
  1006 next
  1007   case (Suc n)
  1008   have "a ^ Suc n $ (Suc n) = (a^n * a)$(Suc n)" by (simp add: ring_simps power_Suc)
  1009   also have "\<dots> = setsum (\<lambda>i. a^n$i * a $ (Suc n - i)) {0.. Suc n}" by (simp add: fps_mult_nth)
  1010   also have "\<dots> = setsum (\<lambda>i. a^n$i * a $ (Suc n - i)) {n .. Suc n}"
  1011     apply (rule setsum_mono_zero_right)
  1012     apply simp
  1013     apply clarsimp
  1014     apply clarsimp
  1015     apply (rule startsby_zero_power_prefix[rule_format, OF a0])
  1016     apply arith
  1017     done
  1018   also have "\<dots> = a^n $ n * a$1" using a0 by simp
  1019   finally show ?case using Suc.hyps by (simp add: power_Suc)
  1020 qed
  1021 
  1022 lemma fps_inverse_power:
  1023   fixes a :: "('a::{field}) fps"
  1024   shows "inverse (a^n) = inverse a ^ n"
  1025 proof-
  1026   {assume a0: "a$0 = 0"
  1027     hence eq: "inverse a = 0" by (simp add: fps_inverse_def)
  1028     {assume "n = 0" hence ?thesis by simp}
  1029     moreover
  1030     {assume n: "n > 0"
  1031       from startsby_zero_power[OF a0 n] eq a0 n have ?thesis
  1032 	by (simp add: fps_inverse_def)}
  1033     ultimately have ?thesis by blast}
  1034   moreover
  1035   {assume a0: "a$0 \<noteq> 0"
  1036     have ?thesis
  1037       apply (rule fps_inverse_unique)
  1038       apply (simp add: a0)
  1039       unfolding power_mult_distrib[symmetric]
  1040       apply (rule ssubst[where t = "a * inverse a" and s= 1])
  1041       apply simp_all
  1042       apply (subst mult_commute)
  1043       by (rule inverse_mult_eq_1[OF a0])}
  1044   ultimately show ?thesis by blast
  1045 qed
  1046 
  1047 lemma fps_deriv_power: "fps_deriv (a ^ n) = fps_const (of_nat n :: 'a:: comm_ring_1) * fps_deriv a * a ^ (n - 1)"
  1048   apply (induct n, auto simp add: power_Suc ring_simps fps_const_add[symmetric] simp del: fps_const_add)
  1049   by (case_tac n, auto simp add: power_Suc ring_simps)
  1050 
  1051 lemma fps_inverse_deriv:
  1052   fixes a:: "('a :: field) fps"
  1053   assumes a0: "a$0 \<noteq> 0"
  1054   shows "fps_deriv (inverse a) = - fps_deriv a * inverse a ^ 2"
  1055 proof-
  1056   from inverse_mult_eq_1[OF a0]
  1057   have "fps_deriv (inverse a * a) = 0" by simp
  1058   hence "inverse a * fps_deriv a + fps_deriv (inverse a) * a = 0" by simp
  1059   hence "inverse a * (inverse a * fps_deriv a + fps_deriv (inverse a) * a) = 0"  by simp
  1060   with inverse_mult_eq_1[OF a0]
  1061   have "inverse a ^ 2 * fps_deriv a + fps_deriv (inverse a) = 0"
  1062     unfolding power2_eq_square
  1063     apply (simp add: ring_simps)
  1064     by (simp add: mult_assoc[symmetric])
  1065   hence "inverse a ^ 2 * fps_deriv a + fps_deriv (inverse a) - fps_deriv a * inverse a ^ 2 = 0 - fps_deriv a * inverse a ^ 2"
  1066     by simp
  1067   then show "fps_deriv (inverse a) = - fps_deriv a * inverse a ^ 2" by (simp add: ring_simps)
  1068 qed
  1069 
  1070 lemma fps_inverse_mult:
  1071   fixes a::"('a :: field) fps"
  1072   shows "inverse (a * b) = inverse a * inverse b"
  1073 proof-
  1074   {assume a0: "a$0 = 0" hence ab0: "(a*b)$0 = 0" by (simp add: fps_mult_nth)
  1075     from a0 ab0 have th: "inverse a = 0" "inverse (a*b) = 0" by simp_all
  1076     have ?thesis unfolding th by simp}
  1077   moreover
  1078   {assume b0: "b$0 = 0" hence ab0: "(a*b)$0 = 0" by (simp add: fps_mult_nth)
  1079     from b0 ab0 have th: "inverse b = 0" "inverse (a*b) = 0" by simp_all
  1080     have ?thesis unfolding th by simp}
  1081   moreover
  1082   {assume a0: "a$0 \<noteq> 0" and b0: "b$0 \<noteq> 0"
  1083     from a0 b0 have ab0:"(a*b) $ 0 \<noteq> 0" by (simp  add: fps_mult_nth)
  1084     from inverse_mult_eq_1[OF ab0]
  1085     have "inverse (a*b) * (a*b) * inverse a * inverse b = 1 * inverse a * inverse b" by simp
  1086     then have "inverse (a*b) * (inverse a * a) * (inverse b * b) = inverse a * inverse b"
  1087       by (simp add: ring_simps)
  1088     then have ?thesis using inverse_mult_eq_1[OF a0] inverse_mult_eq_1[OF b0] by simp}
  1089 ultimately show ?thesis by blast
  1090 qed
  1091 
  1092 lemma fps_inverse_deriv':
  1093   fixes a:: "('a :: field) fps"
  1094   assumes a0: "a$0 \<noteq> 0"
  1095   shows "fps_deriv (inverse a) = - fps_deriv a / a ^ 2"
  1096   using fps_inverse_deriv[OF a0]
  1097   unfolding power2_eq_square fps_divide_def
  1098     fps_inverse_mult by simp
  1099 
  1100 lemma inverse_mult_eq_1': assumes f0: "f$0 \<noteq> (0::'a::field)"
  1101   shows "f * inverse f= 1"
  1102   by (metis mult_commute inverse_mult_eq_1 f0)
  1103 
  1104 lemma fps_divide_deriv:   fixes a:: "('a :: field) fps"
  1105   assumes a0: "b$0 \<noteq> 0"
  1106   shows "fps_deriv (a / b) = (fps_deriv a * b - a * fps_deriv b) / b ^ 2"
  1107   using fps_inverse_deriv[OF a0]
  1108   by (simp add: fps_divide_def ring_simps power2_eq_square fps_inverse_mult inverse_mult_eq_1'[OF a0])
  1109 
  1110 
  1111 lemma fps_inverse_gp': "inverse (Abs_fps(\<lambda>n. (1::'a::field)))
  1112   = 1 - X"
  1113   by (simp add: fps_inverse_gp fps_eq_iff X_def)
  1114 
  1115 lemma fps_nth_deriv_X[simp]: "fps_nth_deriv n X = (if n = 0 then X else if n=1 then 1 else 0)"
  1116   by (cases "n", simp_all)
  1117 
  1118 
  1119 lemma fps_inverse_X_plus1:
  1120   "inverse (1 + X) = Abs_fps (\<lambda>n. (- (1::'a::{field})) ^ n)" (is "_ = ?r")
  1121 proof-
  1122   have eq: "(1 + X) * ?r = 1"
  1123     unfolding minus_one_power_iff
  1124     by (auto simp add: ring_simps fps_eq_iff)
  1125   show ?thesis by (auto simp add: eq intro: fps_inverse_unique)
  1126 qed
  1127 
  1128 
  1129 subsection{* Integration *}
  1130 
  1131 definition
  1132   fps_integral :: "'a::field_char_0 fps \<Rightarrow> 'a \<Rightarrow> 'a fps" where
  1133   "fps_integral a a0 = Abs_fps (\<lambda>n. if n = 0 then a0 else (a$(n - 1) / of_nat n))"
  1134 
  1135 lemma fps_deriv_fps_integral: "fps_deriv (fps_integral a a0) = a"
  1136   unfolding fps_integral_def fps_deriv_def
  1137   by (simp add: fps_eq_iff del: of_nat_Suc)
  1138 
  1139 lemma fps_integral_linear:
  1140   "fps_integral (fps_const a * f + fps_const b * g) (a*a0 + b*b0) =
  1141     fps_const a * fps_integral f a0 + fps_const b * fps_integral g b0"
  1142   (is "?l = ?r")
  1143 proof-
  1144   have "fps_deriv ?l = fps_deriv ?r" by (simp add: fps_deriv_fps_integral)
  1145   moreover have "?l$0 = ?r$0" by (simp add: fps_integral_def)
  1146   ultimately show ?thesis
  1147     unfolding fps_deriv_eq_iff by auto
  1148 qed
  1149 
  1150 subsection {* Composition of FPSs *}
  1151 definition fps_compose :: "('a::semiring_1) fps \<Rightarrow> 'a fps \<Rightarrow> 'a fps" (infixl "oo" 55) where
  1152   fps_compose_def: "a oo b = Abs_fps (\<lambda>n. setsum (\<lambda>i. a$i * (b^i$n)) {0..n})"
  1153 
  1154 lemma fps_compose_nth: "(a oo b)$n = setsum (\<lambda>i. a$i * (b^i$n)) {0..n}" by (simp add: fps_compose_def)
  1155 
  1156 lemma fps_compose_X[simp]: "a oo X = (a :: ('a :: comm_ring_1) fps)"
  1157   by (simp add: fps_ext fps_compose_def mult_delta_right setsum_delta')
  1158 
  1159 lemma fps_const_compose[simp]:
  1160   "fps_const (a::'a::{comm_ring_1}) oo b = fps_const (a)"
  1161   by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta)
  1162 
  1163 lemma number_of_compose[simp]: "(number_of k::('a::{comm_ring_1}) fps) oo b = number_of k"
  1164   unfolding number_of_fps_const by simp
  1165 
  1166 lemma X_fps_compose_startby0[simp]: "a$0 = 0 \<Longrightarrow> X oo a = (a :: ('a :: comm_ring_1) fps)"
  1167   by (simp add: fps_eq_iff fps_compose_def mult_delta_left setsum_delta
  1168                 power_Suc not_le)
  1169 
  1170 
  1171 subsection {* Rules from Herbert Wilf's Generatingfunctionology*}
  1172 
  1173 subsubsection {* Rule 1 *}
  1174   (* {a_{n+k}}_0^infty Corresponds to (f - setsum (\<lambda>i. a_i * x^i))/x^h, for h>0*)
  1175 
  1176 lemma fps_power_mult_eq_shift:
  1177   "X^Suc k * Abs_fps (\<lambda>n. a (n + Suc k)) = Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a:: comm_ring_1) * X^i) {0 .. k}" (is "?lhs = ?rhs")
  1178 proof-
  1179   {fix n:: nat
  1180     have "?lhs $ n = (if n < Suc k then 0 else a n)"
  1181       unfolding X_power_mult_nth by auto
  1182     also have "\<dots> = ?rhs $ n"
  1183     proof(induct k)
  1184       case 0 thus ?case by (simp add: fps_setsum_nth power_Suc)
  1185     next
  1186       case (Suc k)
  1187       note th = Suc.hyps[symmetric]
  1188       have "(Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a) * X^i) {0 .. Suc k})$n = (Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a) * X^i) {0 .. k} - fps_const (a (Suc k)) * X^ Suc k) $ n" by (simp add: ring_simps)
  1189       also  have "\<dots> = (if n < Suc k then 0 else a n) - (fps_const (a (Suc k)) * X^ Suc k)$n"
  1190 	using th
  1191 	unfolding fps_sub_nth by simp
  1192       also have "\<dots> = (if n < Suc (Suc k) then 0 else a n)"
  1193 	unfolding X_power_mult_right_nth
  1194 	apply (auto simp add: not_less fps_const_def)
  1195 	apply (rule cong[of a a, OF refl])
  1196 	by arith
  1197       finally show ?case by simp
  1198     qed
  1199     finally have "?lhs $ n = ?rhs $ n"  .}
  1200   then show ?thesis by (simp add: fps_eq_iff)
  1201 qed
  1202 
  1203 subsubsection{* Rule 2*}
  1204 
  1205   (* We can not reach the form of Wilf, but still near to it using rewrite rules*)
  1206   (* If f reprents {a_n} and P is a polynomial, then
  1207         P(xD) f represents {P(n) a_n}*)
  1208 
  1209 definition "XD = op * X o fps_deriv"
  1210 
  1211 lemma XD_add[simp]:"XD (a + b) = XD a + XD (b :: ('a::comm_ring_1) fps)"
  1212   by (simp add: XD_def ring_simps)
  1213 
  1214 lemma XD_mult_const[simp]:"XD (fps_const (c::'a::comm_ring_1) * a) = fps_const c * XD a"
  1215   by (simp add: XD_def ring_simps)
  1216 
  1217 lemma XD_linear[simp]: "XD (fps_const c * a + fps_const d * b) = fps_const c * XD a + fps_const d * XD (b :: ('a::comm_ring_1) fps)"
  1218   by simp
  1219 
  1220 lemma XDN_linear:
  1221   "(XD ^^ n) (fps_const c * a + fps_const d * b) = fps_const c * (XD ^^ n) a + fps_const d * (XD ^^ n) (b :: ('a::comm_ring_1) fps)"
  1222   by (induct n, simp_all)
  1223 
  1224 lemma fps_mult_X_deriv_shift: "X* fps_deriv a = Abs_fps (\<lambda>n. of_nat n* a$n)" by (simp add: fps_eq_iff)
  1225 
  1226 
  1227 lemma fps_mult_XD_shift:
  1228   "(XD ^^ k) (a:: ('a::{comm_ring_1}) fps) = Abs_fps (\<lambda>n. (of_nat n ^ k) * a$n)"
  1229   by (induct k arbitrary: a) (simp_all add: power_Suc XD_def fps_eq_iff ring_simps del: One_nat_def)
  1230 
  1231 subsubsection{* Rule 3 is trivial and is given by @{text fps_times_def}*}
  1232 subsubsection{* Rule 5 --- summation and "division" by (1 - X)*}
  1233 
  1234 lemma fps_divide_X_minus1_setsum_lemma:
  1235   "a = ((1::('a::comm_ring_1) fps) - X) * Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
  1236 proof-
  1237   let ?X = "X::('a::comm_ring_1) fps"
  1238   let ?sa = "Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
  1239   have th0: "\<And>i. (1 - (X::'a fps)) $ i = (if i = 0 then 1 else if i = 1 then - 1 else 0)" by simp
  1240   {fix n:: nat
  1241     {assume "n=0" hence "a$n = ((1 - ?X) * ?sa) $ n"
  1242 	by (simp add: fps_mult_nth)}
  1243     moreover
  1244     {assume n0: "n \<noteq> 0"
  1245       then have u: "{0} \<union> ({1} \<union> {2..n}) = {0..n}" "{1}\<union>{2..n} = {1..n}"
  1246 	"{0..n - 1}\<union>{n} = {0..n}"
  1247 	apply (simp_all add: expand_set_eq) by presburger+
  1248       have d: "{0} \<inter> ({1} \<union> {2..n}) = {}" "{1} \<inter> {2..n} = {}"
  1249 	"{0..n - 1}\<inter>{n} ={}" using n0
  1250 	by (simp_all add: expand_set_eq, presburger+)
  1251       have f: "finite {0}" "finite {1}" "finite {2 .. n}"
  1252 	"finite {0 .. n - 1}" "finite {n}" by simp_all
  1253     have "((1 - ?X) * ?sa) $ n = setsum (\<lambda>i. (1 - ?X)$ i * ?sa $ (n - i)) {0 .. n}"
  1254       by (simp add: fps_mult_nth)
  1255     also have "\<dots> = a$n" unfolding th0
  1256       unfolding setsum_Un_disjoint[OF f(1) finite_UnI[OF f(2,3)] d(1), unfolded u(1)]
  1257       unfolding setsum_Un_disjoint[OF f(2) f(3) d(2)]
  1258       apply (simp)
  1259       unfolding setsum_Un_disjoint[OF f(4,5) d(3), unfolded u(3)]
  1260       by simp
  1261     finally have "a$n = ((1 - ?X) * ?sa) $ n" by simp}
  1262   ultimately have "a$n = ((1 - ?X) * ?sa) $ n" by blast}
  1263 then show ?thesis
  1264   unfolding fps_eq_iff by blast
  1265 qed
  1266 
  1267 lemma fps_divide_X_minus1_setsum:
  1268   "a /((1::('a::field) fps) - X)  = Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
  1269 proof-
  1270   let ?X = "1 - (X::('a::field) fps)"
  1271   have th0: "?X $ 0 \<noteq> 0" by simp
  1272   have "a /?X = ?X *  Abs_fps (\<lambda>n\<Colon>nat. setsum (op $ a) {0..n}) * inverse ?X"
  1273     using fps_divide_X_minus1_setsum_lemma[of a, symmetric] th0
  1274     by (simp add: fps_divide_def mult_assoc)
  1275   also have "\<dots> = (inverse ?X * ?X) * Abs_fps (\<lambda>n\<Colon>nat. setsum (op $ a) {0..n}) "
  1276     by (simp add: mult_ac)
  1277   finally show ?thesis by (simp add: inverse_mult_eq_1[OF th0])
  1278 qed
  1279 
  1280 subsubsection{* Rule 4 in its more general form: generalizes Rule 3 for an arbitrary
  1281   finite product of FPS, also the relvant instance of powers of a FPS*}
  1282 
  1283 definition "natpermute n k = {l:: nat list. length l = k \<and> foldl op + 0 l = n}"
  1284 
  1285 lemma natlist_trivial_1: "natpermute n 1 = {[n]}"
  1286   apply (auto simp add: natpermute_def)
  1287   apply (case_tac x, auto)
  1288   done
  1289 
  1290 lemma foldl_add_start0:
  1291   "foldl op + x xs = x + foldl op + (0::nat) xs"
  1292   apply (induct xs arbitrary: x)
  1293   apply simp
  1294   unfolding foldl.simps
  1295   apply atomize
  1296   apply (subgoal_tac "\<forall>x\<Colon>nat. foldl op + x xs = x + foldl op + (0\<Colon>nat) xs")
  1297   apply (erule_tac x="x + a" in allE)
  1298   apply (erule_tac x="a" in allE)
  1299   apply simp
  1300   apply assumption
  1301   done
  1302 
  1303 lemma foldl_add_append: "foldl op + (x::nat) (xs@ys) = foldl op + x xs + foldl op + 0 ys"
  1304   apply (induct ys arbitrary: x xs)
  1305   apply auto
  1306   apply (subst (2) foldl_add_start0)
  1307   apply simp
  1308   apply (subst (2) foldl_add_start0)
  1309   by simp
  1310 
  1311 lemma foldl_add_setsum: "foldl op + (x::nat) xs = x + setsum (nth xs) {0..<length xs}"
  1312 proof(induct xs arbitrary: x)
  1313   case Nil thus ?case by simp
  1314 next
  1315   case (Cons a as x)
  1316   have eq: "setsum (op ! (a#as)) {1..<length (a#as)} = setsum (op ! as) {0..<length as}"
  1317     apply (rule setsum_reindex_cong [where f=Suc])
  1318     by (simp_all add: inj_on_def)
  1319   have f: "finite {0}" "finite {1 ..< length (a#as)}" by simp_all
  1320   have d: "{0} \<inter> {1..<length (a#as)} = {}" by simp
  1321   have seq: "{0} \<union> {1..<length(a#as)} = {0 ..<length (a#as)}" by auto
  1322   have "foldl op + x (a#as) = x + foldl op + a as "
  1323     apply (subst foldl_add_start0)    by simp
  1324   also have "\<dots> = x + a + setsum (op ! as) {0..<length as}" unfolding Cons.hyps by simp
  1325   also have "\<dots> = x + setsum (op ! (a#as)) {0..<length (a#as)}"
  1326     unfolding eq[symmetric]
  1327     unfolding setsum_Un_disjoint[OF f d, unfolded seq]
  1328     by simp
  1329   finally show ?case  .
  1330 qed
  1331 
  1332 
  1333 lemma append_natpermute_less_eq:
  1334   assumes h: "xs@ys \<in> natpermute n k" shows "foldl op + 0 xs \<le> n" and "foldl op + 0 ys \<le> n"
  1335 proof-
  1336   {from h have "foldl op + 0 (xs@ ys) = n" by (simp add: natpermute_def)
  1337     hence "foldl op + 0 xs + foldl op + 0 ys = n" unfolding foldl_add_append .}
  1338   note th = this
  1339   {from th show "foldl op + 0 xs \<le> n" by simp}
  1340   {from th show "foldl op + 0 ys \<le> n" by simp}
  1341 qed
  1342 
  1343 lemma natpermute_split:
  1344   assumes mn: "h \<le> k"
  1345   shows "natpermute n k = (\<Union>m \<in>{0..n}. {l1 @ l2 |l1 l2. l1 \<in> natpermute m h \<and> l2 \<in> natpermute (n - m) (k - h)})" (is "?L = ?R" is "?L = (\<Union>m \<in>{0..n}. ?S m)")
  1346 proof-
  1347   {fix l assume l: "l \<in> ?R"
  1348     from l obtain m xs ys where h: "m \<in> {0..n}" and xs: "xs \<in> natpermute m h" and ys: "ys \<in> natpermute (n - m) (k - h)"  and leq: "l = xs@ys" by blast
  1349     from xs have xs': "foldl op + 0 xs = m" by (simp add: natpermute_def)
  1350     from ys have ys': "foldl op + 0 ys = n - m" by (simp add: natpermute_def)
  1351     have "l \<in> ?L" using leq xs ys h
  1352       apply simp
  1353       apply (clarsimp simp add: natpermute_def simp del: foldl_append)
  1354       apply (simp add: foldl_add_append[unfolded foldl_append])
  1355       unfolding xs' ys'
  1356       using mn xs ys
  1357       unfolding natpermute_def by simp}
  1358   moreover
  1359   {fix l assume l: "l \<in> natpermute n k"
  1360     let ?xs = "take h l"
  1361     let ?ys = "drop h l"
  1362     let ?m = "foldl op + 0 ?xs"
  1363     from l have ls: "foldl op + 0 (?xs @ ?ys) = n" by (simp add: natpermute_def)
  1364     have xs: "?xs \<in> natpermute ?m h" using l mn by (simp add: natpermute_def)
  1365     have ys: "?ys \<in> natpermute (n - ?m) (k - h)" using l mn ls[unfolded foldl_add_append]
  1366       by (simp add: natpermute_def)
  1367     from ls have m: "?m \<in> {0..n}"  unfolding foldl_add_append by simp
  1368     from xs ys ls have "l \<in> ?R"
  1369       apply auto
  1370       apply (rule bexI[where x = "?m"])
  1371       apply (rule exI[where x = "?xs"])
  1372       apply (rule exI[where x = "?ys"])
  1373       using ls l unfolding foldl_add_append
  1374       by (auto simp add: natpermute_def)}
  1375   ultimately show ?thesis by blast
  1376 qed
  1377 
  1378 lemma natpermute_0: "natpermute n 0 = (if n = 0 then {[]} else {})"
  1379   by (auto simp add: natpermute_def)
  1380 lemma natpermute_0'[simp]: "natpermute 0 k = (if k = 0 then {[]} else {replicate k 0})"
  1381   apply (auto simp add: set_replicate_conv_if natpermute_def)
  1382   apply (rule nth_equalityI)
  1383   by simp_all
  1384 
  1385 lemma natpermute_finite: "finite (natpermute n k)"
  1386 proof(induct k arbitrary: n)
  1387   case 0 thus ?case
  1388     apply (subst natpermute_split[of 0 0, simplified])
  1389     by (simp add: natpermute_0)
  1390 next
  1391   case (Suc k)
  1392   then show ?case unfolding natpermute_split[of k "Suc k", simplified]
  1393     apply -
  1394     apply (rule finite_UN_I)
  1395     apply simp
  1396     unfolding One_nat_def[symmetric] natlist_trivial_1
  1397     apply simp
  1398     done
  1399 qed
  1400 
  1401 lemma natpermute_contain_maximal:
  1402   "{xs \<in> natpermute n (k+1). n \<in> set xs} = UNION {0 .. k} (\<lambda>i. {(replicate (k+1) 0) [i:=n]})"
  1403   (is "?A = ?B")
  1404 proof-
  1405   {fix xs assume H: "xs \<in> natpermute n (k+1)" and n: "n \<in> set xs"
  1406     from n obtain i where i: "i \<in> {0.. k}" "xs!i = n" using H
  1407       unfolding in_set_conv_nth by (auto simp add: less_Suc_eq_le natpermute_def)
  1408     have eqs: "({0..k} - {i}) \<union> {i} = {0..k}" using i by auto
  1409     have f: "finite({0..k} - {i})" "finite {i}" by auto
  1410     have d: "({0..k} - {i}) \<inter> {i} = {}" using i by auto
  1411     from H have "n = setsum (nth xs) {0..k}" apply (simp add: natpermute_def)
  1412       unfolding foldl_add_setsum by (auto simp add: atLeastLessThanSuc_atLeastAtMost)
  1413     also have "\<dots> = n + setsum (nth xs) ({0..k} - {i})"
  1414       unfolding setsum_Un_disjoint[OF f d, unfolded eqs] using i by simp
  1415     finally have zxs: "\<forall> j\<in> {0..k} - {i}. xs!j = 0" by auto
  1416     from H have xsl: "length xs = k+1" by (simp add: natpermute_def)
  1417     from i have i': "i < length (replicate (k+1) 0)"   "i < k+1"
  1418       unfolding length_replicate  by arith+
  1419     have "xs = replicate (k+1) 0 [i := n]"
  1420       apply (rule nth_equalityI)
  1421       unfolding xsl length_list_update length_replicate
  1422       apply simp
  1423       apply clarify
  1424       unfolding nth_list_update[OF i'(1)]
  1425       using i zxs
  1426       by (case_tac "ia=i", auto simp del: replicate.simps)
  1427     then have "xs \<in> ?B" using i by blast}
  1428   moreover
  1429   {fix i assume i: "i \<in> {0..k}"
  1430     let ?xs = "replicate (k+1) 0 [i:=n]"
  1431     have nxs: "n \<in> set ?xs"
  1432       apply (rule set_update_memI) using i by simp
  1433     have xsl: "length ?xs = k+1" by (simp only: length_replicate length_list_update)
  1434     have "foldl op + 0 ?xs = setsum (nth ?xs) {0..<k+1}"
  1435       unfolding foldl_add_setsum add_0 length_replicate length_list_update ..
  1436     also have "\<dots> = setsum (\<lambda>j. if j = i then n else 0) {0..< k+1}"
  1437       apply (rule setsum_cong2) by (simp del: replicate.simps)
  1438     also have "\<dots> = n" using i by (simp add: setsum_delta)
  1439     finally
  1440     have "?xs \<in> natpermute n (k+1)" using xsl unfolding natpermute_def Collect_def mem_def
  1441       by blast
  1442     then have "?xs \<in> ?A"  using nxs  by blast}
  1443   ultimately show ?thesis by auto
  1444 qed
  1445 
  1446     (* The general form *)
  1447 lemma fps_setprod_nth:
  1448   fixes m :: nat and a :: "nat \<Rightarrow> ('a::comm_ring_1) fps"
  1449   shows "(setprod a {0 .. m})$n = setsum (\<lambda>v. setprod (\<lambda>j. (a j) $ (v!j)) {0..m}) (natpermute n (m+1))"
  1450   (is "?P m n")
  1451 proof(induct m arbitrary: n rule: nat_less_induct)
  1452   fix m n assume H: "\<forall>m' < m. \<forall>n. ?P m' n"
  1453   {assume m0: "m = 0"
  1454     hence "?P m n" apply simp
  1455       unfolding natlist_trivial_1[where n = n, unfolded One_nat_def] by simp}
  1456   moreover
  1457   {fix k assume k: "m = Suc k"
  1458     have km: "k < m" using k by arith
  1459     have u0: "{0 .. k} \<union> {m} = {0..m}" using k apply (simp add: expand_set_eq) by presburger
  1460     have f0: "finite {0 .. k}" "finite {m}" by auto
  1461     have d0: "{0 .. k} \<inter> {m} = {}" using k by auto
  1462     have "(setprod a {0 .. m}) $ n = (setprod a {0 .. k} * a m) $ n"
  1463       unfolding setprod_Un_disjoint[OF f0 d0, unfolded u0] by simp
  1464     also have "\<dots> = (\<Sum>i = 0..n. (\<Sum>v\<in>natpermute i (k + 1). \<Prod>j\<in>{0..k}. a j $ v ! j) * a m $ (n - i))"
  1465       unfolding fps_mult_nth H[rule_format, OF km] ..
  1466     also have "\<dots> = (\<Sum>v\<in>natpermute n (m + 1). \<Prod>j\<in>{0..m}. a j $ v ! j)"
  1467       apply (simp add: k)
  1468       unfolding natpermute_split[of m "m + 1", simplified, of n, unfolded natlist_trivial_1[unfolded One_nat_def] k]
  1469       apply (subst setsum_UN_disjoint)
  1470       apply simp
  1471       apply simp
  1472       unfolding image_Collect[symmetric]
  1473       apply clarsimp
  1474       apply (rule finite_imageI)
  1475       apply (rule natpermute_finite)
  1476       apply (clarsimp simp add: expand_set_eq)
  1477       apply auto
  1478       apply (rule setsum_cong2)
  1479       unfolding setsum_left_distrib
  1480       apply (rule sym)
  1481       apply (rule_tac f="\<lambda>xs. xs @[n - x]" in  setsum_reindex_cong)
  1482       apply (simp add: inj_on_def)
  1483       apply auto
  1484       unfolding setprod_Un_disjoint[OF f0 d0, unfolded u0, unfolded k]
  1485       apply (clarsimp simp add: natpermute_def nth_append)
  1486       done
  1487     finally have "?P m n" .}
  1488   ultimately show "?P m n " by (cases m, auto)
  1489 qed
  1490 
  1491 text{* The special form for powers *}
  1492 lemma fps_power_nth_Suc:
  1493   fixes m :: nat and a :: "('a::comm_ring_1) fps"
  1494   shows "(a ^ Suc m)$n = setsum (\<lambda>v. setprod (\<lambda>j. a $ (v!j)) {0..m}) (natpermute n (m+1))"
  1495 proof-
  1496   have f: "finite {0 ..m}" by simp
  1497   have th0: "a^Suc m = setprod (\<lambda>i. a) {0..m}" unfolding setprod_constant[OF f, of a] by simp
  1498   show ?thesis unfolding th0 fps_setprod_nth ..
  1499 qed
  1500 lemma fps_power_nth:
  1501   fixes m :: nat and a :: "('a::comm_ring_1) fps"
  1502   shows "(a ^m)$n = (if m=0 then 1$n else setsum (\<lambda>v. setprod (\<lambda>j. a $ (v!j)) {0..m - 1}) (natpermute n m))"
  1503   by (cases m, simp_all add: fps_power_nth_Suc del: power_Suc)
  1504 
  1505 lemma fps_nth_power_0:
  1506   fixes m :: nat and a :: "('a::{comm_ring_1}) fps"
  1507   shows "(a ^m)$0 = (a$0) ^ m"
  1508 proof-
  1509   {assume "m=0" hence ?thesis by simp}
  1510   moreover
  1511   {fix n assume m: "m = Suc n"
  1512     have c: "m = card {0..n}" using m by simp
  1513    have "(a ^m)$0 = setprod (\<lambda>i. a$0) {0..n}"
  1514      by (simp add: m fps_power_nth del: replicate.simps power_Suc)
  1515    also have "\<dots> = (a$0) ^ m"
  1516      unfolding c by (rule setprod_constant, simp)
  1517    finally have ?thesis .}
  1518  ultimately show ?thesis by (cases m, auto)
  1519 qed
  1520 
  1521 lemma fps_compose_inj_right:
  1522   assumes a0: "a$0 = (0::'a::{idom})"
  1523   and a1: "a$1 \<noteq> 0"
  1524   shows "(b oo a = c oo a) \<longleftrightarrow> b = c" (is "?lhs \<longleftrightarrow>?rhs")
  1525 proof-
  1526   {assume ?rhs then have "?lhs" by simp}
  1527   moreover
  1528   {assume h: ?lhs
  1529     {fix n have "b$n = c$n"
  1530       proof(induct n rule: nat_less_induct)
  1531 	fix n assume H: "\<forall>m<n. b$m = c$m"
  1532 	{assume n0: "n=0"
  1533 	  from h have "(b oo a)$n = (c oo a)$n" by simp
  1534 	  hence "b$n = c$n" using n0 by (simp add: fps_compose_nth)}
  1535 	moreover
  1536 	{fix n1 assume n1: "n = Suc n1"
  1537 	  have f: "finite {0 .. n1}" "finite {n}" by simp_all
  1538 	  have eq: "{0 .. n1} \<union> {n} = {0 .. n}" using n1 by auto
  1539 	  have d: "{0 .. n1} \<inter> {n} = {}" using n1 by auto
  1540 	  have seq: "(\<Sum>i = 0..n1. b $ i * a ^ i $ n) = (\<Sum>i = 0..n1. c $ i * a ^ i $ n)"
  1541 	    apply (rule setsum_cong2)
  1542 	    using H n1 by auto
  1543 	  have th0: "(b oo a) $n = (\<Sum>i = 0..n1. c $ i * a ^ i $ n) + b$n * (a$1)^n"
  1544 	    unfolding fps_compose_nth setsum_Un_disjoint[OF f d, unfolded eq] seq
  1545 	    using startsby_zero_power_nth_same[OF a0]
  1546 	    by simp
  1547 	  have th1: "(c oo a) $n = (\<Sum>i = 0..n1. c $ i * a ^ i $ n) + c$n * (a$1)^n"
  1548 	    unfolding fps_compose_nth setsum_Un_disjoint[OF f d, unfolded eq]
  1549 	    using startsby_zero_power_nth_same[OF a0]
  1550 	    by simp
  1551 	  from h[unfolded fps_eq_iff, rule_format, of n] th0 th1 a1
  1552 	  have "b$n = c$n" by auto}
  1553 	ultimately show "b$n = c$n" by (cases n, auto)
  1554       qed}
  1555     then have ?rhs by (simp add: fps_eq_iff)}
  1556   ultimately show ?thesis by blast
  1557 qed
  1558 
  1559 
  1560 subsection {* Radicals *}
  1561 
  1562 declare setprod_cong[fundef_cong]
  1563 function radical :: "(nat \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> ('a::{field}) fps \<Rightarrow> nat \<Rightarrow> 'a" where
  1564   "radical r 0 a 0 = 1"
  1565 | "radical r 0 a (Suc n) = 0"
  1566 | "radical r (Suc k) a 0 = r (Suc k) (a$0)"
  1567 | "radical r (Suc k) a (Suc n) = (a$ Suc n - setsum (\<lambda>xs. setprod (\<lambda>j. radical r (Suc k) a (xs ! j)) {0..k}) {xs. xs \<in> natpermute (Suc n) (Suc k) \<and> Suc n \<notin> set xs}) / (of_nat (Suc k) * (radical r (Suc k) a 0)^k)"
  1568 by pat_completeness auto
  1569 
  1570 termination radical
  1571 proof
  1572   let ?R = "measure (\<lambda>(r, k, a, n). n)"
  1573   {
  1574     show "wf ?R" by auto}
  1575   {fix r k a n xs i
  1576     assume xs: "xs \<in> {xs \<in> natpermute (Suc n) (Suc k). Suc n \<notin> set xs}" and i: "i \<in> {0..k}"
  1577     {assume c: "Suc n \<le> xs ! i"
  1578       from xs i have "xs !i \<noteq> Suc n" by (auto simp add: in_set_conv_nth natpermute_def)
  1579       with c have c': "Suc n < xs!i" by arith
  1580       have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}" by simp_all
  1581       have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}" by auto
  1582       have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})" using i by auto
  1583       from xs have "Suc n = foldl op + 0 xs" by (simp add: natpermute_def)
  1584       also have "\<dots> = setsum (nth xs) {0..<Suc k}" unfolding foldl_add_setsum using xs
  1585 	by (simp add: natpermute_def)
  1586       also have "\<dots> = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
  1587 	unfolding eqs  setsum_Un_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
  1588 	unfolding setsum_Un_disjoint[OF fths(2) fths(3) d(2)]
  1589 	by simp
  1590       finally have False using c' by simp}
  1591     then show "((r,Suc k,a,xs!i), r, Suc k, a, Suc n) \<in> ?R"
  1592       apply auto by (metis not_less)}
  1593   {fix r k a n
  1594     show "((r,Suc k, a, 0),r, Suc k, a, Suc n) \<in> ?R" by simp}
  1595 qed
  1596 
  1597 definition "fps_radical r n a = Abs_fps (radical r n a)"
  1598 
  1599 lemma fps_radical0[simp]: "fps_radical r 0 a = 1"
  1600   apply (auto simp add: fps_eq_iff fps_radical_def)  by (case_tac n, auto)
  1601 
  1602 lemma fps_radical_nth_0[simp]: "fps_radical r n a $ 0 = (if n=0 then 1 else r n (a$0))"
  1603   by (cases n, simp_all add: fps_radical_def)
  1604 
  1605 lemma fps_radical_power_nth[simp]:
  1606   assumes r: "(r k (a$0)) ^ k = a$0"
  1607   shows "fps_radical r k a ^ k $ 0 = (if k = 0 then 1 else a$0)"
  1608 proof-
  1609   {assume "k=0" hence ?thesis by simp }
  1610   moreover
  1611   {fix h assume h: "k = Suc h"
  1612     have fh: "finite {0..h}" by simp
  1613     have eq1: "fps_radical r k a ^ k $ 0 = (\<Prod>j\<in>{0..h}. fps_radical r k a $ (replicate k 0) ! j)"
  1614       unfolding fps_power_nth h by simp
  1615     also have "\<dots> = (\<Prod>j\<in>{0..h}. r k (a$0))"
  1616       apply (rule setprod_cong)
  1617       apply simp
  1618       using h
  1619       apply (subgoal_tac "replicate k (0::nat) ! x = 0")
  1620       by (auto intro: nth_replicate simp del: replicate.simps)
  1621     also have "\<dots> = a$0"
  1622       unfolding setprod_constant[OF fh] using r by (simp add: h)
  1623     finally have ?thesis using h by simp}
  1624   ultimately show ?thesis by (cases k, auto)
  1625 qed
  1626 
  1627 lemma natpermute_max_card: assumes n0: "n\<noteq>0"
  1628   shows "card {xs \<in> natpermute n (k+1). n \<in> set xs} = k+1"
  1629   unfolding natpermute_contain_maximal
  1630 proof-
  1631   let ?A= "\<lambda>i. {replicate (k + 1) 0[i := n]}"
  1632   let ?K = "{0 ..k}"
  1633   have fK: "finite ?K" by simp
  1634   have fAK: "\<forall>i\<in>?K. finite (?A i)" by auto
  1635   have d: "\<forall>i\<in> ?K. \<forall>j\<in> ?K. i \<noteq> j \<longrightarrow> {replicate (k + 1) 0[i := n]} \<inter> {replicate (k + 1) 0[j := n]} = {}"
  1636   proof(clarify)
  1637     fix i j assume i: "i \<in> ?K" and j: "j\<in> ?K" and ij: "i\<noteq>j"
  1638     {assume eq: "replicate (k+1) 0 [i:=n] = replicate (k+1) 0 [j:= n]"
  1639       have "(replicate (k+1) 0 [i:=n] ! i) = n" using i by (simp del: replicate.simps)
  1640       moreover
  1641       have "(replicate (k+1) 0 [j:=n] ! i) = 0" using i ij by (simp del: replicate.simps)
  1642       ultimately have False using eq n0 by (simp del: replicate.simps)}
  1643     then show "{replicate (k + 1) 0[i := n]} \<inter> {replicate (k + 1) 0[j := n]} = {}"
  1644       by auto
  1645   qed
  1646   from card_UN_disjoint[OF fK fAK d]
  1647   show "card (\<Union>i\<in>{0..k}. {replicate (k + 1) 0[i := n]}) = k+1" by simp
  1648 qed
  1649 
  1650 lemma power_radical:
  1651   fixes a:: "'a::field_char_0 fps"
  1652   assumes a0: "a$0 \<noteq> 0"
  1653   shows "(r (Suc k) (a$0)) ^ Suc k = a$0 \<longleftrightarrow> (fps_radical r (Suc k) a) ^ (Suc k) = a"
  1654 proof-
  1655   let ?r = "fps_radical r (Suc k) a"
  1656   {assume r0: "(r (Suc k) (a$0)) ^ Suc k = a$0"
  1657     from a0 r0 have r00: "r (Suc k) (a$0) \<noteq> 0" by auto
  1658     {fix z have "?r ^ Suc k $ z = a$z"
  1659       proof(induct z rule: nat_less_induct)
  1660 	fix n assume H: "\<forall>m<n. ?r ^ Suc k $ m = a$m"
  1661 	{assume "n = 0" hence "?r ^ Suc k $ n = a $n"
  1662 	    using fps_radical_power_nth[of r "Suc k" a, OF r0] by simp}
  1663 	moreover
  1664 	{fix n1 assume n1: "n = Suc n1"
  1665 	  have fK: "finite {0..k}" by simp
  1666 	  have nz: "n \<noteq> 0" using n1 by arith
  1667 	  let ?Pnk = "natpermute n (k + 1)"
  1668 	  let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
  1669 	  let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
  1670 	  have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
  1671 	  have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
  1672 	  have f: "finite ?Pnkn" "finite ?Pnknn"
  1673 	    using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
  1674 	    by (metis natpermute_finite)+
  1675 	  let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
  1676 	  have "setsum ?f ?Pnkn = setsum (\<lambda>v. ?r $ n * r (Suc k) (a $ 0) ^ k) ?Pnkn"
  1677 	  proof(rule setsum_cong2)
  1678 	    fix v assume v: "v \<in> {xs \<in> natpermute n (k + 1). n \<in> set xs}"
  1679 	    let ?ths = "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) = fps_radical r (Suc k) a $ n * r (Suc k) (a $ 0) ^ k"
  1680 	  from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
  1681 	    unfolding natpermute_contain_maximal by auto
  1682 	  have "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) = (\<Prod>j\<in>{0..k}. if j = i then fps_radical r (Suc k) a $ n else r (Suc k) (a$0))"
  1683 	    apply (rule setprod_cong, simp)
  1684 	    using i r0 by (simp del: replicate.simps)
  1685 	  also have "\<dots> = (fps_radical r (Suc k) a $ n) * r (Suc k) (a$0) ^ k"
  1686 	    unfolding setprod_gen_delta[OF fK] using i r0 by simp
  1687 	  finally show ?ths .
  1688 	qed
  1689 	then have "setsum ?f ?Pnkn = of_nat (k+1) * ?r $ n * r (Suc k) (a $ 0) ^ k"
  1690 	  by (simp add: natpermute_max_card[OF nz, simplified])
  1691 	also have "\<dots> = a$n - setsum ?f ?Pnknn"
  1692 	  unfolding n1 using r00 a0 by (simp add: field_simps fps_radical_def del: of_nat_Suc )
  1693 	finally have fn: "setsum ?f ?Pnkn = a$n - setsum ?f ?Pnknn" .
  1694 	have "(?r ^ Suc k)$n = setsum ?f ?Pnkn + setsum ?f ?Pnknn"
  1695 	  unfolding fps_power_nth_Suc setsum_Un_disjoint[OF f d, unfolded eq] ..
  1696 	also have "\<dots> = a$n" unfolding fn by simp
  1697 	finally have "?r ^ Suc k $ n = a $n" .}
  1698       ultimately  show "?r ^ Suc k $ n = a $n" by (cases n, auto)
  1699     qed }
  1700   then have ?thesis using r0 by (simp add: fps_eq_iff)}
  1701 moreover 
  1702 { assume h: "(fps_radical r (Suc k) a) ^ (Suc k) = a"
  1703   hence "((fps_radical r (Suc k) a) ^ (Suc k))$0 = a$0" by simp
  1704   then have "(r (Suc k) (a$0)) ^ Suc k = a$0"
  1705     unfolding fps_power_nth_Suc
  1706     by (simp add: setprod_constant del: replicate.simps)}
  1707 ultimately show ?thesis by blast
  1708 qed
  1709 
  1710 (*
  1711 lemma power_radical:
  1712   fixes a:: "'a::field_char_0 fps"
  1713   assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 \<noteq> 0"
  1714   shows "(fps_radical r (Suc k) a) ^ (Suc k) = a"
  1715 proof-
  1716   let ?r = "fps_radical r (Suc k) a"
  1717   from a0 r0 have r00: "r (Suc k) (a$0) \<noteq> 0" by auto
  1718   {fix z have "?r ^ Suc k $ z = a$z"
  1719     proof(induct z rule: nat_less_induct)
  1720       fix n assume H: "\<forall>m<n. ?r ^ Suc k $ m = a$m"
  1721       {assume "n = 0" hence "?r ^ Suc k $ n = a $n"
  1722 	  using fps_radical_power_nth[of r "Suc k" a, OF r0] by simp}
  1723       moreover
  1724       {fix n1 assume n1: "n = Suc n1"
  1725 	have fK: "finite {0..k}" by simp
  1726 	have nz: "n \<noteq> 0" using n1 by arith
  1727 	let ?Pnk = "natpermute n (k + 1)"
  1728 	let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
  1729 	let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
  1730 	have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
  1731 	have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
  1732 	have f: "finite ?Pnkn" "finite ?Pnknn"
  1733 	  using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
  1734 	  by (metis natpermute_finite)+
  1735 	let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
  1736 	have "setsum ?f ?Pnkn = setsum (\<lambda>v. ?r $ n * r (Suc k) (a $ 0) ^ k) ?Pnkn"
  1737 	proof(rule setsum_cong2)
  1738 	  fix v assume v: "v \<in> {xs \<in> natpermute n (k + 1). n \<in> set xs}"
  1739 	  let ?ths = "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) = fps_radical r (Suc k) a $ n * r (Suc k) (a $ 0) ^ k"
  1740 	  from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
  1741 	    unfolding natpermute_contain_maximal by auto
  1742 	  have "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) = (\<Prod>j\<in>{0..k}. if j = i then fps_radical r (Suc k) a $ n else r (Suc k) (a$0))"
  1743 	    apply (rule setprod_cong, simp)
  1744 	    using i r0 by (simp del: replicate.simps)
  1745 	  also have "\<dots> = (fps_radical r (Suc k) a $ n) * r (Suc k) (a$0) ^ k"
  1746 	    unfolding setprod_gen_delta[OF fK] using i r0 by simp
  1747 	  finally show ?ths .
  1748 	qed
  1749 	then have "setsum ?f ?Pnkn = of_nat (k+1) * ?r $ n * r (Suc k) (a $ 0) ^ k"
  1750 	  by (simp add: natpermute_max_card[OF nz, simplified])
  1751 	also have "\<dots> = a$n - setsum ?f ?Pnknn"
  1752 	  unfolding n1 using r00 a0 by (simp add: field_simps fps_radical_def del: of_nat_Suc )
  1753 	finally have fn: "setsum ?f ?Pnkn = a$n - setsum ?f ?Pnknn" .
  1754 	have "(?r ^ Suc k)$n = setsum ?f ?Pnkn + setsum ?f ?Pnknn"
  1755 	  unfolding fps_power_nth_Suc setsum_Un_disjoint[OF f d, unfolded eq] ..
  1756 	also have "\<dots> = a$n" unfolding fn by simp
  1757 	finally have "?r ^ Suc k $ n = a $n" .}
  1758       ultimately  show "?r ^ Suc k $ n = a $n" by (cases n, auto)
  1759   qed }
  1760   then show ?thesis by (simp add: fps_eq_iff)
  1761 qed
  1762 
  1763 *)
  1764 lemma eq_divide_imp': assumes c0: "(c::'a::field) ~= 0" and eq: "a * c = b"
  1765   shows "a = b / c"
  1766 proof-
  1767   from eq have "a * c * inverse c = b * inverse c" by simp
  1768   hence "a * (inverse c * c) = b/c" by (simp only: field_simps divide_inverse)
  1769   then show "a = b/c" unfolding  field_inverse[OF c0] by simp
  1770 qed
  1771 
  1772 lemma radical_unique:
  1773   assumes r0: "(r (Suc k) (b$0)) ^ Suc k = b$0"
  1774   and a0: "r (Suc k) (b$0 ::'a::field_char_0) = a$0" and b0: "b$0 \<noteq> 0"
  1775   shows "a^(Suc k) = b \<longleftrightarrow> a = fps_radical r (Suc k) b"
  1776 proof-
  1777   let ?r = "fps_radical r (Suc k) b"
  1778   have r00: "r (Suc k) (b$0) \<noteq> 0" using b0 r0 by auto
  1779   {assume H: "a = ?r"
  1780     from H have "a^Suc k = b" using power_radical[OF b0, of r k, unfolded r0] by simp}
  1781   moreover
  1782   {assume H: "a^Suc k = b"
  1783     have ceq: "card {0..k} = Suc k" by simp
  1784     have fk: "finite {0..k}" by simp
  1785     from a0 have a0r0: "a$0 = ?r$0" by simp
  1786     {fix n have "a $ n = ?r $ n"
  1787       proof(induct n rule: nat_less_induct)
  1788 	fix n assume h: "\<forall>m<n. a$m = ?r $m"
  1789 	{assume "n = 0" hence "a$n = ?r $n" using a0 by simp }
  1790 	moreover
  1791 	{fix n1 assume n1: "n = Suc n1"
  1792 	  have fK: "finite {0..k}" by simp
  1793 	have nz: "n \<noteq> 0" using n1 by arith
  1794 	let ?Pnk = "natpermute n (Suc k)"
  1795 	let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
  1796 	let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
  1797 	have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
  1798 	have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
  1799 	have f: "finite ?Pnkn" "finite ?Pnknn"
  1800 	  using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
  1801 	  by (metis natpermute_finite)+
  1802 	let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
  1803 	let ?g = "\<lambda>v. \<Prod>j\<in>{0..k}. a $ v ! j"
  1804 	have "setsum ?g ?Pnkn = setsum (\<lambda>v. a $ n * (?r$0)^k) ?Pnkn"
  1805 	proof(rule setsum_cong2)
  1806 	  fix v assume v: "v \<in> {xs \<in> natpermute n (Suc k). n \<in> set xs}"
  1807 	  let ?ths = "(\<Prod>j\<in>{0..k}. a $ v ! j) = a $ n * (?r$0)^k"
  1808 	  from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
  1809 	    unfolding Suc_eq_plus1 natpermute_contain_maximal by (auto simp del: replicate.simps)
  1810 	  have "(\<Prod>j\<in>{0..k}. a $ v ! j) = (\<Prod>j\<in>{0..k}. if j = i then a $ n else r (Suc k) (b$0))"
  1811 	    apply (rule setprod_cong, simp)
  1812 	    using i a0 by (simp del: replicate.simps)
  1813 	  also have "\<dots> = a $ n * (?r $ 0)^k"
  1814 	    unfolding  setprod_gen_delta[OF fK] using i by simp
  1815 	  finally show ?ths .
  1816 	qed
  1817 	then have th0: "setsum ?g ?Pnkn = of_nat (k+1) * a $ n * (?r $ 0)^k"
  1818 	  by (simp add: natpermute_max_card[OF nz, simplified])
  1819 	have th1: "setsum ?g ?Pnknn = setsum ?f ?Pnknn"
  1820 	proof (rule setsum_cong2, rule setprod_cong, simp)
  1821 	  fix xs i assume xs: "xs \<in> ?Pnknn" and i: "i \<in> {0..k}"
  1822 	  {assume c: "n \<le> xs ! i"
  1823 	    from xs i have "xs !i \<noteq> n" by (auto simp add: in_set_conv_nth natpermute_def)
  1824 	    with c have c': "n < xs!i" by arith
  1825 	    have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}" by simp_all
  1826 	    have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}" by auto
  1827 	    have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})" using i by auto
  1828 	    from xs have "n = foldl op + 0 xs" by (simp add: natpermute_def)
  1829 	    also have "\<dots> = setsum (nth xs) {0..<Suc k}" unfolding foldl_add_setsum using xs
  1830 	      by (simp add: natpermute_def)
  1831 	    also have "\<dots> = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
  1832 	      unfolding eqs  setsum_Un_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
  1833 	      unfolding setsum_Un_disjoint[OF fths(2) fths(3) d(2)]
  1834 	      by simp
  1835 	    finally have False using c' by simp}
  1836 	  then have thn: "xs!i < n" by arith
  1837 	  from h[rule_format, OF thn]
  1838 	  show "a$(xs !i) = ?r$(xs!i)" .
  1839 	qed
  1840 	have th00: "\<And>(x::'a). of_nat (Suc k) * (x * inverse (of_nat (Suc k))) = x"
  1841 	  by (simp add: field_simps del: of_nat_Suc)
  1842 	from H have "b$n = a^Suc k $ n" by (simp add: fps_eq_iff)
  1843 	also have "a ^ Suc k$n = setsum ?g ?Pnkn + setsum ?g ?Pnknn"
  1844 	  unfolding fps_power_nth_Suc
  1845 	  using setsum_Un_disjoint[OF f d, unfolded Suc_eq_plus1[symmetric],
  1846 	    unfolded eq, of ?g] by simp
  1847 	also have "\<dots> = of_nat (k+1) * a $ n * (?r $ 0)^k + setsum ?f ?Pnknn" unfolding th0 th1 ..
  1848 	finally have "of_nat (k+1) * a $ n * (?r $ 0)^k = b$n - setsum ?f ?Pnknn" by simp
  1849 	then have "a$n = (b$n - setsum ?f ?Pnknn) / (of_nat (k+1) * (?r $ 0)^k)"
  1850 	  apply -
  1851 	  apply (rule eq_divide_imp')
  1852 	  using r00
  1853 	  apply (simp del: of_nat_Suc)
  1854 	  by (simp add: mult_ac)
  1855 	then have "a$n = ?r $n"
  1856 	  apply (simp del: of_nat_Suc)
  1857 	  unfolding fps_radical_def n1
  1858 	  by (simp add: field_simps n1 th00 del: of_nat_Suc)}
  1859 	ultimately show "a$n = ?r $ n" by (cases n, auto)
  1860       qed}
  1861     then have "a = ?r" by (simp add: fps_eq_iff)}
  1862   ultimately show ?thesis by blast
  1863 qed
  1864 
  1865 
  1866 lemma radical_power:
  1867   assumes r0: "r (Suc k) ((a$0) ^ Suc k) = a$0"
  1868   and a0: "(a$0 ::'a::field_char_0) \<noteq> 0"
  1869   shows "(fps_radical r (Suc k) (a ^ Suc k)) = a"
  1870 proof-
  1871   let ?ak = "a^ Suc k"
  1872   have ak0: "?ak $ 0 = (a$0) ^ Suc k" by (simp add: fps_nth_power_0 del: power_Suc)
  1873   from r0 have th0: "r (Suc k) (a ^ Suc k $ 0) ^ Suc k = a ^ Suc k $ 0" using ak0 by auto
  1874   from r0 ak0 have th1: "r (Suc k) (a ^ Suc k $ 0) = a $ 0" by auto
  1875   from ak0 a0 have ak00: "?ak $ 0 \<noteq>0 " by auto
  1876   from radical_unique[of r k ?ak a, OF th0 th1 ak00] show ?thesis by metis
  1877 qed
  1878 
  1879 lemma fps_deriv_radical:
  1880   fixes a:: "'a::field_char_0 fps"
  1881   assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 \<noteq> 0"
  1882   shows "fps_deriv (fps_radical r (Suc k) a) = fps_deriv a / (fps_const (of_nat (Suc k)) * (fps_radical r (Suc k) a) ^ k)"
  1883 proof-
  1884   let ?r= "fps_radical r (Suc k) a"
  1885   let ?w = "(fps_const (of_nat (Suc k)) * ?r ^ k)"
  1886   from a0 r0 have r0': "r (Suc k) (a$0) \<noteq> 0" by auto
  1887   from r0' have w0: "?w $ 0 \<noteq> 0" by (simp del: of_nat_Suc)
  1888   note th0 = inverse_mult_eq_1[OF w0]
  1889   let ?iw = "inverse ?w"
  1890   from iffD1[OF power_radical[of a r], OF a0 r0]
  1891   have "fps_deriv (?r ^ Suc k) = fps_deriv a" by simp
  1892   hence "fps_deriv ?r * ?w = fps_deriv a"
  1893     by (simp add: fps_deriv_power mult_ac del: power_Suc)
  1894   hence "?iw * fps_deriv ?r * ?w = ?iw * fps_deriv a" by simp
  1895   hence "fps_deriv ?r * (?iw * ?w) = fps_deriv a / ?w"
  1896     by (simp add: fps_divide_def)
  1897   then show ?thesis unfolding th0 by simp
  1898 qed
  1899 
  1900 lemma radical_mult_distrib:
  1901   fixes a:: "'a::field_char_0 fps"
  1902   assumes
  1903   k: "k > 0"
  1904   and ra0: "r k (a $ 0) ^ k = a $ 0"
  1905   and rb0: "r k (b $ 0) ^ k = b $ 0"
  1906   and a0: "a$0 \<noteq> 0"
  1907   and b0: "b$0 \<noteq> 0"
  1908   shows "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0) \<longleftrightarrow> fps_radical r (k) (a*b) = fps_radical r (k) a * fps_radical r (k) (b)"
  1909 proof-
  1910   {assume  r0': "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0)"
  1911   from r0' have r0: "(r (k) ((a*b)$0)) ^ k = (a*b)$0"
  1912     by (simp add: fps_mult_nth ra0 rb0 power_mult_distrib)
  1913   {assume "k=0" hence ?thesis using r0' by simp}
  1914   moreover
  1915   {fix h assume k: "k = Suc h"
  1916   let ?ra = "fps_radical r (Suc h) a"
  1917   let ?rb = "fps_radical r (Suc h) b"
  1918   have th0: "r (Suc h) ((a * b) $ 0) = (fps_radical r (Suc h) a * fps_radical r (Suc h) b) $ 0"
  1919     using r0' k by (simp add: fps_mult_nth)
  1920   have ab0: "(a*b) $ 0 \<noteq> 0" using a0 b0 by (simp add: fps_mult_nth)
  1921   from radical_unique[of r h "a*b" "fps_radical r (Suc h) a * fps_radical r (Suc h) b", OF r0[unfolded k] th0 ab0, symmetric]
  1922     iffD1[OF power_radical[of _ r], OF a0 ra0[unfolded k]] iffD1[OF power_radical[of _ r], OF b0 rb0[unfolded k]] k r0'
  1923   have ?thesis by (auto simp add: power_mult_distrib simp del: power_Suc)}
  1924 ultimately have ?thesis by (cases k, auto)}
  1925 moreover
  1926 {assume h: "fps_radical r k (a*b) = fps_radical r k a * fps_radical r k b"
  1927   hence "(fps_radical r k (a*b))$0 = (fps_radical r k a * fps_radical r k b)$0" by simp
  1928   then have "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0)"
  1929     using k by (simp add: fps_mult_nth)}
  1930 ultimately show ?thesis by blast
  1931 qed
  1932 
  1933 (*
  1934 lemma radical_mult_distrib:
  1935   fixes a:: "'a::field_char_0 fps"
  1936   assumes
  1937   ra0: "r k (a $ 0) ^ k = a $ 0"
  1938   and rb0: "r k (b $ 0) ^ k = b $ 0"
  1939   and r0': "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0)"
  1940   and a0: "a$0 \<noteq> 0"
  1941   and b0: "b$0 \<noteq> 0"
  1942   shows "fps_radical r (k) (a*b) = fps_radical r (k) a * fps_radical r (k) (b)"
  1943 proof-
  1944   from r0' have r0: "(r (k) ((a*b)$0)) ^ k = (a*b)$0"
  1945     by (simp add: fps_mult_nth ra0 rb0 power_mult_distrib)
  1946   {assume "k=0" hence ?thesis by simp}
  1947   moreover
  1948   {fix h assume k: "k = Suc h"
  1949   let ?ra = "fps_radical r (Suc h) a"
  1950   let ?rb = "fps_radical r (Suc h) b"
  1951   have th0: "r (Suc h) ((a * b) $ 0) = (fps_radical r (Suc h) a * fps_radical r (Suc h) b) $ 0"
  1952     using r0' k by (simp add: fps_mult_nth)
  1953   have ab0: "(a*b) $ 0 \<noteq> 0" using a0 b0 by (simp add: fps_mult_nth)
  1954   from radical_unique[of r h "a*b" "fps_radical r (Suc h) a * fps_radical r (Suc h) b", OF r0[unfolded k] th0 ab0, symmetric]
  1955     power_radical[of r, OF ra0[unfolded k] a0] power_radical[of r, OF rb0[unfolded k] b0] k
  1956   have ?thesis by (auto simp add: power_mult_distrib simp del: power_Suc)}
  1957 ultimately show ?thesis by (cases k, auto)
  1958 qed
  1959 *)
  1960 
  1961 lemma fps_divide_1[simp]: "(a:: ('a::field) fps) / 1 = a"
  1962   by (simp add: fps_divide_def)
  1963 
  1964 lemma radical_divide:
  1965   fixes a :: "'a::field_char_0 fps"
  1966   assumes
  1967   kp: "k>0"
  1968   and ra0: "(r k (a $ 0)) ^ k = a $ 0"
  1969   and rb0: "(r k (b $ 0)) ^ k = b $ 0"
  1970   and a0: "a$0 \<noteq> 0"
  1971   and b0: "b$0 \<noteq> 0"
  1972   shows "r k ((a $ 0) / (b$0)) = r k (a$0) / r k (b $ 0) \<longleftrightarrow> fps_radical r k (a/b) = fps_radical r k a / fps_radical r k b" (is "?lhs = ?rhs")
  1973 proof-
  1974   let ?r = "fps_radical r k"
  1975   from kp obtain h where k: "k = Suc h" by (cases k, auto)
  1976   have ra0': "r k (a$0) \<noteq> 0" using a0 ra0 k by auto
  1977   have rb0': "r k (b$0) \<noteq> 0" using b0 rb0 k by auto
  1978 
  1979   {assume ?rhs
  1980     then have "?r (a/b) $ 0 = (?r a / ?r b)$0" by simp
  1981     then have ?lhs using k a0 b0 rb0' 
  1982       by (simp add: fps_divide_def fps_mult_nth fps_inverse_def divide_inverse) }
  1983   moreover
  1984   {assume h: ?lhs
  1985     from a0 b0 have ab0[simp]: "(a/b)$0 = a$0 / b$0" 
  1986       by (simp add: fps_divide_def fps_mult_nth divide_inverse fps_inverse_def)
  1987     have th0: "r k ((a/b)$0) ^ k = (a/b)$0"
  1988       by (simp add: h nonzero_power_divide[OF rb0'] ra0 rb0 del: k)
  1989     from a0 b0 ra0' rb0' kp h 
  1990     have th1: "r k ((a / b) $ 0) = (fps_radical r k a / fps_radical r k b) $ 0"
  1991       by (simp add: fps_divide_def fps_mult_nth fps_inverse_def divide_inverse del: k)
  1992     from a0 b0 ra0' rb0' kp have ab0': "(a / b) $ 0 \<noteq> 0"
  1993       by (simp add: fps_divide_def fps_mult_nth fps_inverse_def nonzero_imp_inverse_nonzero)
  1994     note tha[simp] = iffD1[OF power_radical[where r=r and k=h], OF a0 ra0[unfolded k], unfolded k[symmetric]]
  1995     note thb[simp] = iffD1[OF power_radical[where r=r and k=h], OF b0 rb0[unfolded k], unfolded k[symmetric]]
  1996     have th2: "(?r a / ?r b)^k = a/b"
  1997       by (simp add: fps_divide_def power_mult_distrib fps_inverse_power[symmetric])
  1998     from iffD1[OF radical_unique[where r=r and a="?r a / ?r b" and b="a/b" and k=h], symmetric, unfolded k[symmetric], OF th0 th1 ab0' th2] have ?rhs .}
  1999   ultimately show ?thesis by blast
  2000 qed
  2001 
  2002 lemma radical_inverse:
  2003   fixes a :: "'a::field_char_0 fps"
  2004   assumes
  2005   k: "k>0"
  2006   and ra0: "r k (a $ 0) ^ k = a $ 0"
  2007   and r1: "(r k 1)^k = 1"
  2008   and a0: "a$0 \<noteq> 0"
  2009   shows "r k (inverse (a $ 0)) = r k 1 / (r k (a $ 0)) \<longleftrightarrow> fps_radical r k (inverse a) = fps_radical r k 1 / fps_radical r k a"
  2010   using radical_divide[where k=k and r=r and a=1 and b=a, OF k ] ra0 r1 a0
  2011   by (simp add: divide_inverse fps_divide_def)
  2012 
  2013 subsection{* Derivative of composition *}
  2014 
  2015 lemma fps_compose_deriv:
  2016   fixes a:: "('a::idom) fps"
  2017   assumes b0: "b$0 = 0"
  2018   shows "fps_deriv (a oo b) = ((fps_deriv a) oo b) * (fps_deriv b)"
  2019 proof-
  2020   {fix n
  2021     have "(fps_deriv (a oo b))$n = setsum (\<lambda>i. a $ i * (fps_deriv (b^i))$n) {0.. Suc n}"
  2022       by (simp add: fps_compose_def ring_simps setsum_right_distrib del: of_nat_Suc)
  2023     also have "\<dots> = setsum (\<lambda>i. a$i * ((fps_const (of_nat i)) * (fps_deriv b * (b^(i - 1))))$n) {0.. Suc n}"
  2024       by (simp add: ring_simps fps_deriv_power del: fps_mult_left_const_nth of_nat_Suc)
  2025   also have "\<dots> = setsum (\<lambda>i. of_nat i * a$i * (((b^(i - 1)) * fps_deriv b))$n) {0.. Suc n}"
  2026     unfolding fps_mult_left_const_nth  by (simp add: ring_simps)
  2027   also have "\<dots> = setsum (\<lambda>i. of_nat i * a$i * (setsum (\<lambda>j. (b^ (i - 1))$j * (fps_deriv b)$(n - j)) {0..n})) {0.. Suc n}"
  2028     unfolding fps_mult_nth ..
  2029   also have "\<dots> = setsum (\<lambda>i. of_nat i * a$i * (setsum (\<lambda>j. (b^ (i - 1))$j * (fps_deriv b)$(n - j)) {0..n})) {1.. Suc n}"
  2030     apply (rule setsum_mono_zero_right)
  2031     apply (auto simp add: mult_delta_left setsum_delta not_le)
  2032     done
  2033   also have "\<dots> = setsum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (setsum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}"
  2034     unfolding fps_deriv_nth
  2035     apply (rule setsum_reindex_cong[where f="Suc"])
  2036     by (auto simp add: mult_assoc)
  2037   finally have th0: "(fps_deriv (a oo b))$n = setsum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (setsum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}" .
  2038 
  2039   have "(((fps_deriv a) oo b) * (fps_deriv b))$n = setsum (\<lambda>i. (fps_deriv b)$ (n - i) * ((fps_deriv a) oo b)$i) {0..n}"
  2040     unfolding fps_mult_nth by (simp add: mult_ac)
  2041   also have "\<dots> = setsum (\<lambda>i. setsum (\<lambda>j. of_nat (n - i +1) * b$(n - i + 1) * of_nat (j + 1) * a$(j+1) * (b^j)$i) {0..n}) {0..n}"
  2042     unfolding fps_deriv_nth fps_compose_nth setsum_right_distrib mult_assoc
  2043     apply (rule setsum_cong2)
  2044     apply (rule setsum_mono_zero_left)
  2045     apply (simp_all add: subset_eq)
  2046     apply clarify
  2047     apply (subgoal_tac "b^i$x = 0")
  2048     apply simp
  2049     apply (rule startsby_zero_power_prefix[OF b0, rule_format])
  2050     by simp
  2051   also have "\<dots> = setsum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (setsum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}"
  2052     unfolding setsum_right_distrib
  2053     apply (subst setsum_commute)
  2054     by ((rule setsum_cong2)+) simp
  2055   finally have "(fps_deriv (a oo b))$n = (((fps_deriv a) oo b) * (fps_deriv b)) $n"
  2056     unfolding th0 by simp}
  2057 then show ?thesis by (simp add: fps_eq_iff)
  2058 qed
  2059 
  2060 lemma fps_mult_X_plus_1_nth:
  2061   "((1+X)*a) $n = (if n = 0 then (a$n :: 'a::comm_ring_1) else a$n + a$(n - 1))"
  2062 proof-
  2063   {assume "n = 0" hence ?thesis by (simp add: fps_mult_nth )}
  2064   moreover
  2065   {fix m assume m: "n = Suc m"
  2066     have "((1+X)*a) $n = setsum (\<lambda>i. (1+X)$i * a$(n-i)) {0..n}"
  2067       by (simp add: fps_mult_nth)
  2068     also have "\<dots> = setsum (\<lambda>i. (1+X)$i * a$(n-i)) {0.. 1}"
  2069       unfolding m
  2070       apply (rule setsum_mono_zero_right)
  2071       by (auto simp add: )
  2072     also have "\<dots> = (if n = 0 then (a$n :: 'a::comm_ring_1) else a$n + a$(n - 1))"
  2073       unfolding m
  2074       by (simp add: )
  2075     finally have ?thesis .}
  2076   ultimately show ?thesis by (cases n, auto)
  2077 qed
  2078 
  2079 subsection{* Finite FPS (i.e. polynomials) and X *}
  2080 lemma fps_poly_sum_X:
  2081   assumes z: "\<forall>i > n. a$i = (0::'a::comm_ring_1)"
  2082   shows "a = setsum (\<lambda>i. fps_const (a$i) * X^i) {0..n}" (is "a = ?r")
  2083 proof-
  2084   {fix i
  2085     have "a$i = ?r$i"
  2086       unfolding fps_setsum_nth fps_mult_left_const_nth X_power_nth
  2087       by (simp add: mult_delta_right setsum_delta' z)
  2088   }
  2089   then show ?thesis unfolding fps_eq_iff by blast
  2090 qed
  2091 
  2092 subsection{* Compositional inverses *}
  2093 
  2094 
  2095 fun compinv :: "'a fps \<Rightarrow> nat \<Rightarrow> 'a::{field}" where
  2096   "compinv a 0 = X$0"
  2097 | "compinv a (Suc n) = (X$ Suc n - setsum (\<lambda>i. (compinv a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
  2098 
  2099 definition "fps_inv a = Abs_fps (compinv a)"
  2100 
  2101 lemma fps_inv: assumes a0: "a$0 = 0" and a1: "a$1 \<noteq> 0"
  2102   shows "fps_inv a oo a = X"
  2103 proof-
  2104   let ?i = "fps_inv a oo a"
  2105   {fix n
  2106     have "?i $n = X$n"
  2107     proof(induct n rule: nat_less_induct)
  2108       fix n assume h: "\<forall>m<n. ?i$m = X$m"
  2109       {assume "n=0" hence "?i $n = X$n" using a0
  2110 	  by (simp add: fps_compose_nth fps_inv_def)}
  2111       moreover
  2112       {fix n1 assume n1: "n = Suc n1"
  2113 	have "?i $ n = setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1} + fps_inv a $ Suc n1 * (a $ 1)^ Suc n1"
  2114 	  by (simp add: fps_compose_nth n1 startsby_zero_power_nth_same[OF a0]
  2115                    del: power_Suc)
  2116 	also have "\<dots> = setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1} + (X$ Suc n1 - setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1})"
  2117 	  using a0 a1 n1 by (simp add: fps_inv_def)
  2118 	also have "\<dots> = X$n" using n1 by simp
  2119 	finally have "?i $ n = X$n" .}
  2120       ultimately show "?i $ n = X$n" by (cases n, auto)
  2121     qed}
  2122   then show ?thesis by (simp add: fps_eq_iff)
  2123 qed
  2124 
  2125 
  2126 fun gcompinv :: "'a fps \<Rightarrow> 'a fps \<Rightarrow> nat \<Rightarrow> 'a::{field}" where
  2127   "gcompinv b a 0 = b$0"
  2128 | "gcompinv b a (Suc n) = (b$ Suc n - setsum (\<lambda>i. (gcompinv b a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
  2129 
  2130 definition "fps_ginv b a = Abs_fps (gcompinv b a)"
  2131 
  2132 lemma fps_ginv: assumes a0: "a$0 = 0" and a1: "a$1 \<noteq> 0"
  2133   shows "fps_ginv b a oo a = b"
  2134 proof-
  2135   let ?i = "fps_ginv b a oo a"
  2136   {fix n
  2137     have "?i $n = b$n"
  2138     proof(induct n rule: nat_less_induct)
  2139       fix n assume h: "\<forall>m<n. ?i$m = b$m"
  2140       {assume "n=0" hence "?i $n = b$n" using a0
  2141 	  by (simp add: fps_compose_nth fps_ginv_def)}
  2142       moreover
  2143       {fix n1 assume n1: "n = Suc n1"
  2144 	have "?i $ n = setsum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1} + fps_ginv b a $ Suc n1 * (a $ 1)^ Suc n1"
  2145 	  by (simp add: fps_compose_nth n1 startsby_zero_power_nth_same[OF a0]
  2146                    del: power_Suc)
  2147 	also have "\<dots> = setsum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1} + (b$ Suc n1 - setsum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1})"
  2148 	  using a0 a1 n1 by (simp add: fps_ginv_def)
  2149 	also have "\<dots> = b$n" using n1 by simp
  2150 	finally have "?i $ n = b$n" .}
  2151       ultimately show "?i $ n = b$n" by (cases n, auto)
  2152     qed}
  2153   then show ?thesis by (simp add: fps_eq_iff)
  2154 qed
  2155 
  2156 lemma fps_inv_ginv: "fps_inv = fps_ginv X"
  2157   apply (auto simp add: expand_fun_eq fps_eq_iff fps_inv_def fps_ginv_def)
  2158   apply (induct_tac n rule: nat_less_induct, auto)
  2159   apply (case_tac na)
  2160   apply simp
  2161   apply simp
  2162   done
  2163 
  2164 lemma fps_compose_1[simp]: "1 oo a = 1"
  2165   by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta)
  2166 
  2167 lemma fps_compose_0[simp]: "0 oo a = 0"
  2168   by (simp add: fps_eq_iff fps_compose_nth)
  2169 
  2170 lemma fps_compose_0_right[simp]: "a oo 0 = fps_const (a$0)"
  2171   by (auto simp add: fps_eq_iff fps_compose_nth power_0_left setsum_0')
  2172 
  2173 lemma fps_compose_add_distrib: "(a + b) oo c = (a oo c) + (b oo c)"
  2174   by (simp add: fps_eq_iff fps_compose_nth ring_simps setsum_addf)
  2175 
  2176 lemma fps_compose_setsum_distrib: "(setsum f S) oo a = setsum (\<lambda>i. f i oo a) S"
  2177 proof-
  2178   {assume "\<not> finite S" hence ?thesis by simp}
  2179   moreover
  2180   {assume fS: "finite S"
  2181     have ?thesis
  2182     proof(rule finite_induct[OF fS])
  2183       show "setsum f {} oo a = (\<Sum>i\<in>{}. f i oo a)" by simp
  2184     next
  2185       fix x F assume fF: "finite F" and xF: "x \<notin> F" and h: "setsum f F oo a = setsum (\<lambda>i. f i oo a) F"
  2186       show "setsum f (insert x F) oo a  = setsum (\<lambda>i. f i oo a) (insert x F)"
  2187 	using fF xF h by (simp add: fps_compose_add_distrib)
  2188     qed}
  2189   ultimately show ?thesis by blast
  2190 qed
  2191 
  2192 lemma convolution_eq:
  2193   "setsum (%i. a (i :: nat) * b (n - i)) {0 .. n} = setsum (%(i,j). a i * b j) {(i,j). i <= n \<and> j \<le> n \<and> i + j = n}"
  2194   apply (rule setsum_reindex_cong[where f=fst])
  2195   apply (clarsimp simp add: inj_on_def)
  2196   apply (auto simp add: expand_set_eq image_iff)
  2197   apply (rule_tac x= "x" in exI)
  2198   apply clarsimp
  2199   apply (rule_tac x="n - x" in exI)
  2200   apply arith
  2201   done
  2202 
  2203 lemma product_composition_lemma:
  2204   assumes c0: "c$0 = (0::'a::idom)" and d0: "d$0 = 0"
  2205   shows "((a oo c) * (b oo d))$n = setsum (%(k,m). a$k * b$m * (c^k * d^m) $ n) {(k,m). k + m \<le> n}" (is "?l = ?r")
  2206 proof-
  2207   let ?S = "{(k\<Colon>nat, m\<Colon>nat). k + m \<le> n}"
  2208   have s: "?S \<subseteq> {0..n} <*> {0..n}" by (auto simp add: subset_eq)
  2209   have f: "finite {(k\<Colon>nat, m\<Colon>nat). k + m \<le> n}"
  2210     apply (rule finite_subset[OF s])
  2211     by auto
  2212   have "?r =  setsum (%i. setsum (%(k,m). a$k * (c^k)$i * b$m * (d^m) $ (n - i)) {(k,m). k + m \<le> n}) {0..n}"
  2213     apply (simp add: fps_mult_nth setsum_right_distrib)
  2214     apply (subst setsum_commute)
  2215     apply (rule setsum_cong2)
  2216     by (auto simp add: ring_simps)
  2217   also have "\<dots> = ?l"
  2218     apply (simp add: fps_mult_nth fps_compose_nth setsum_product)
  2219     apply (rule setsum_cong2)
  2220     apply (simp add: setsum_cartesian_product mult_assoc)
  2221     apply (rule setsum_mono_zero_right[OF f])
  2222     apply (simp add: subset_eq) apply presburger
  2223     apply clarsimp
  2224     apply (rule ccontr)
  2225     apply (clarsimp simp add: not_le)
  2226     apply (case_tac "x < aa")
  2227     apply simp
  2228     apply (frule_tac startsby_zero_power_prefix[rule_format, OF c0])
  2229     apply blast
  2230     apply simp
  2231     apply (frule_tac startsby_zero_power_prefix[rule_format, OF d0])
  2232     apply blast
  2233     done
  2234   finally show ?thesis by simp
  2235 qed
  2236 
  2237 lemma product_composition_lemma':
  2238   assumes c0: "c$0 = (0::'a::idom)" and d0: "d$0 = 0"
  2239   shows "((a oo c) * (b oo d))$n = setsum (%k. setsum (%m. a$k * b$m * (c^k * d^m) $ n) {0..n}) {0..n}" (is "?l = ?r")
  2240   unfolding product_composition_lemma[OF c0 d0]
  2241   unfolding setsum_cartesian_product
  2242   apply (rule setsum_mono_zero_left)
  2243   apply simp
  2244   apply (clarsimp simp add: subset_eq)
  2245   apply clarsimp
  2246   apply (rule ccontr)
  2247   apply (subgoal_tac "(c^aa * d^ba) $ n = 0")
  2248   apply simp
  2249   unfolding fps_mult_nth
  2250   apply (rule setsum_0')
  2251   apply (clarsimp simp add: not_le)
  2252   apply (case_tac "aaa < aa")
  2253   apply (rule startsby_zero_power_prefix[OF c0, rule_format])
  2254   apply simp
  2255   apply (subgoal_tac "n - aaa < ba")
  2256   apply (frule_tac k = "ba" in startsby_zero_power_prefix[OF d0, rule_format])
  2257   apply simp
  2258   apply arith
  2259   done
  2260 
  2261 
  2262 lemma setsum_pair_less_iff:
  2263   "setsum (%((k::nat),m). a k * b m * c (k + m)) {(k,m). k + m \<le> n} = setsum (%s. setsum (%i. a i * b (s - i) * c s) {0..s}) {0..n}" (is "?l = ?r")
  2264 proof-
  2265   let ?KM=  "{(k,m). k + m \<le> n}"
  2266   let ?f = "%s. UNION {(0::nat)..s} (%i. {(i,s - i)})"
  2267   have th0: "?KM = UNION {0..n} ?f"
  2268     apply (simp add: expand_set_eq)
  2269     apply arith (* FIXME: VERY slow! *)
  2270     done
  2271   show "?l = ?r "
  2272     unfolding th0
  2273     apply (subst setsum_UN_disjoint)
  2274     apply auto
  2275     apply (subst setsum_UN_disjoint)
  2276     apply auto
  2277     done
  2278 qed
  2279 
  2280 lemma fps_compose_mult_distrib_lemma:
  2281   assumes c0: "c$0 = (0::'a::idom)"
  2282   shows "((a oo c) * (b oo c))$n = setsum (%s. setsum (%i. a$i * b$(s - i) * (c^s) $ n) {0..s}) {0..n}" (is "?l = ?r")
  2283   unfolding product_composition_lemma[OF c0 c0] power_add[symmetric]
  2284   unfolding setsum_pair_less_iff[where a = "%k. a$k" and b="%m. b$m" and c="%s. (c ^ s)$n" and n = n] ..
  2285 
  2286 
  2287 lemma fps_compose_mult_distrib:
  2288   assumes c0: "c$0 = (0::'a::idom)"
  2289   shows "(a * b) oo c = (a oo c) * (b oo c)" (is "?l = ?r")
  2290   apply (simp add: fps_eq_iff fps_compose_mult_distrib_lemma[OF c0])
  2291   by (simp add: fps_compose_nth fps_mult_nth setsum_left_distrib)
  2292 lemma fps_compose_setprod_distrib:
  2293   assumes c0: "c$0 = (0::'a::idom)"
  2294   shows "(setprod a S) oo c = setprod (%k. a k oo c) S" (is "?l = ?r")
  2295   apply (cases "finite S")
  2296   apply simp_all
  2297   apply (induct S rule: finite_induct)
  2298   apply simp
  2299   apply (simp add: fps_compose_mult_distrib[OF c0])
  2300   done
  2301 
  2302 lemma fps_compose_power:   assumes c0: "c$0 = (0::'a::idom)"
  2303   shows "(a oo c)^n = a^n oo c" (is "?l = ?r")
  2304 proof-
  2305   {assume "n=0" then have ?thesis by simp}
  2306   moreover
  2307   {fix m assume m: "n = Suc m"
  2308     have th0: "a^n = setprod (%k. a) {0..m}" "(a oo c) ^ n = setprod (%k. a oo c) {0..m}"
  2309       by (simp_all add: setprod_constant m)
  2310     then have ?thesis
  2311       by (simp add: fps_compose_setprod_distrib[OF c0])}
  2312   ultimately show ?thesis by (cases n, auto)
  2313 qed
  2314 
  2315 lemma fps_compose_uminus: "- (a::'a::ring_1 fps) oo c = - (a oo c)"
  2316   by (simp add: fps_eq_iff fps_compose_nth ring_simps setsum_negf[symmetric])
  2317 
  2318 lemma fps_compose_sub_distrib:
  2319   shows "(a - b) oo (c::'a::ring_1 fps) = (a oo c) - (b oo c)"
  2320   unfolding diff_minus fps_compose_uminus fps_compose_add_distrib ..
  2321 
  2322 lemma X_fps_compose:"X oo a = Abs_fps (\<lambda>n. if n = 0 then (0::'a::comm_ring_1) else a$n)"
  2323   by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta power_Suc)
  2324 
  2325 lemma fps_inverse_compose:
  2326   assumes b0: "(b$0 :: 'a::field) = 0" and a0: "a$0 \<noteq> 0"
  2327   shows "inverse a oo b = inverse (a oo b)"
  2328 proof-
  2329   let ?ia = "inverse a"
  2330   let ?ab = "a oo b"
  2331   let ?iab = "inverse ?ab"
  2332 
  2333 from a0 have ia0: "?ia $ 0 \<noteq> 0" by (simp )
  2334 from a0 have ab0: "?ab $ 0 \<noteq> 0" by (simp add: fps_compose_def)
  2335 have "(?ia oo b) *  (a oo b) = 1"
  2336 unfolding fps_compose_mult_distrib[OF b0, symmetric]
  2337 unfolding inverse_mult_eq_1[OF a0]
  2338 fps_compose_1 ..
  2339 
  2340 then have "(?ia oo b) *  (a oo b) * ?iab  = 1 * ?iab" by simp
  2341 then have "(?ia oo b) *  (?iab * (a oo b))  = ?iab" by simp
  2342 then show ?thesis 
  2343   unfolding inverse_mult_eq_1[OF ab0] by simp
  2344 qed
  2345 
  2346 lemma fps_divide_compose:
  2347   assumes c0: "(c$0 :: 'a::field) = 0" and b0: "b$0 \<noteq> 0"
  2348   shows "(a/b) oo c = (a oo c) / (b oo c)"
  2349     unfolding fps_divide_def fps_compose_mult_distrib[OF c0]
  2350     fps_inverse_compose[OF c0 b0] ..
  2351 
  2352 lemma gp: assumes a0: "a$0 = (0::'a::field)"
  2353   shows "(Abs_fps (\<lambda>n. 1)) oo a = 1/(1 - a)" (is "?one oo a = _")
  2354 proof-
  2355   have o0: "?one $ 0 \<noteq> 0" by simp
  2356   have th0: "(1 - X) $ 0 \<noteq> (0::'a)" by simp  
  2357   from fps_inverse_gp[where ?'a = 'a]
  2358   have "inverse ?one = 1 - X" by (simp add: fps_eq_iff)
  2359   hence "inverse (inverse ?one) = inverse (1 - X)" by simp
  2360   hence th: "?one = 1/(1 - X)" unfolding fps_inverse_idempotent[OF o0] 
  2361     by (simp add: fps_divide_def)
  2362   show ?thesis unfolding th
  2363     unfolding fps_divide_compose[OF a0 th0]
  2364     fps_compose_1 fps_compose_sub_distrib X_fps_compose_startby0[OF a0] ..
  2365 qed
  2366 
  2367 lemma fps_const_power[simp]: "fps_const (c::'a::ring_1) ^ n = fps_const (c^n)"
  2368 by (induct n, auto)
  2369 
  2370 lemma fps_compose_radical:
  2371   assumes b0: "b$0 = (0::'a::field_char_0)"
  2372   and ra0: "r (Suc k) (a$0) ^ Suc k = a$0"
  2373   and a0: "a$0 \<noteq> 0"
  2374   shows "fps_radical r (Suc k)  a oo b = fps_radical r (Suc k) (a oo b)"
  2375 proof-
  2376   let ?r = "fps_radical r (Suc k)"
  2377   let ?ab = "a oo b"
  2378   have ab0: "?ab $ 0 = a$0" by (simp add: fps_compose_def)
  2379   from ab0 a0 ra0 have rab0: "?ab $ 0 \<noteq> 0" "r (Suc k) (?ab $ 0) ^ Suc k = ?ab $ 0" by simp_all
  2380   have th00: "r (Suc k) ((a oo b) $ 0) = (fps_radical r (Suc k) a oo b) $ 0"
  2381     by (simp add: ab0 fps_compose_def)
  2382   have th0: "(?r a oo b) ^ (Suc k) = a  oo b"
  2383     unfolding fps_compose_power[OF b0]
  2384     unfolding iffD1[OF power_radical[of a r k], OF a0 ra0]  .. 
  2385   from iffD1[OF radical_unique[where r=r and k=k and b= ?ab and a = "?r a oo b", OF rab0(2) th00 rab0(1)], OF th0] show ?thesis  . 
  2386 qed
  2387 
  2388 lemma fps_const_mult_apply_left:
  2389   "fps_const c * (a oo b) = (fps_const c * a) oo b"
  2390   by (simp add: fps_eq_iff fps_compose_nth setsum_right_distrib mult_assoc)
  2391 
  2392 lemma fps_const_mult_apply_right:
  2393   "(a oo b) * fps_const (c::'a::comm_semiring_1) = (fps_const c * a) oo b"
  2394   by (auto simp add: fps_const_mult_apply_left mult_commute)
  2395 
  2396 lemma fps_compose_assoc:
  2397   assumes c0: "c$0 = (0::'a::idom)" and b0: "b$0 = 0"
  2398   shows "a oo (b oo c) = a oo b oo c" (is "?l = ?r")
  2399 proof-
  2400   {fix n
  2401     have "?l$n = (setsum (\<lambda>i. (fps_const (a$i) * b^i) oo c) {0..n})$n"
  2402       by (simp add: fps_compose_nth fps_compose_power[OF c0] fps_const_mult_apply_left setsum_right_distrib mult_assoc fps_setsum_nth)
  2403     also have "\<dots> = ((setsum (\<lambda>i. fps_const (a$i) * b^i) {0..n}) oo c)$n"
  2404       by (simp add: fps_compose_setsum_distrib)
  2405     also have "\<dots> = ?r$n"
  2406       apply (simp add: fps_compose_nth fps_setsum_nth setsum_left_distrib mult_assoc)
  2407       apply (rule setsum_cong2)
  2408       apply (rule setsum_mono_zero_right)
  2409       apply (auto simp add: not_le)
  2410       by (erule startsby_zero_power_prefix[OF b0, rule_format])
  2411     finally have "?l$n = ?r$n" .}
  2412   then show ?thesis by (simp add: fps_eq_iff)
  2413 qed
  2414 
  2415 
  2416 lemma fps_X_power_compose:
  2417   assumes a0: "a$0=0" shows "X^k oo a = (a::('a::idom fps))^k" (is "?l = ?r")
  2418 proof-
  2419   {assume "k=0" hence ?thesis by simp}
  2420   moreover
  2421   {fix h assume h: "k = Suc h"
  2422     {fix n
  2423       {assume kn: "k>n" hence "?l $ n = ?r $n" using a0 startsby_zero_power_prefix[OF a0] h
  2424 	  by (simp add: fps_compose_nth del: power_Suc)}
  2425       moreover
  2426       {assume kn: "k \<le> n"
  2427 	hence "?l$n = ?r$n"
  2428           by (simp add: fps_compose_nth mult_delta_left setsum_delta)}
  2429       moreover have "k >n \<or> k\<le> n"  by arith
  2430       ultimately have "?l$n = ?r$n"  by blast}
  2431     then have ?thesis unfolding fps_eq_iff by blast}
  2432   ultimately show ?thesis by (cases k, auto)
  2433 qed
  2434 
  2435 lemma fps_inv_right: assumes a0: "a$0 = 0" and a1: "a$1 \<noteq> 0"
  2436   shows "a oo fps_inv a = X"
  2437 proof-
  2438   let ?ia = "fps_inv a"
  2439   let ?iaa = "a oo fps_inv a"
  2440   have th0: "?ia $ 0 = 0" by (simp add: fps_inv_def)
  2441   have th1: "?iaa $ 0 = 0" using a0 a1
  2442     by (simp add: fps_inv_def fps_compose_nth)
  2443   have th2: "X$0 = 0" by simp
  2444   from fps_inv[OF a0 a1] have "a oo (fps_inv a oo a) = a oo X" by simp
  2445   then have "(a oo fps_inv a) oo a = X oo a"
  2446     by (simp add: fps_compose_assoc[OF a0 th0] X_fps_compose_startby0[OF a0])
  2447   with fps_compose_inj_right[OF a0 a1]
  2448   show ?thesis by simp
  2449 qed
  2450 
  2451 lemma fps_inv_deriv:
  2452   assumes a0:"a$0 = (0::'a::{field})" and a1: "a$1 \<noteq> 0"
  2453   shows "fps_deriv (fps_inv a) = inverse (fps_deriv a oo fps_inv a)"
  2454 proof-
  2455   let ?ia = "fps_inv a"
  2456   let ?d = "fps_deriv a oo ?ia"
  2457   let ?dia = "fps_deriv ?ia"
  2458   have ia0: "?ia$0 = 0" by (simp add: fps_inv_def)
  2459   have th0: "?d$0 \<noteq> 0" using a1 by (simp add: fps_compose_nth fps_deriv_nth)
  2460   from fps_inv_right[OF a0 a1] have "?d * ?dia = 1"
  2461     by (simp add: fps_compose_deriv[OF ia0, of a, symmetric] )
  2462   hence "inverse ?d * ?d * ?dia = inverse ?d * 1" by simp
  2463   with inverse_mult_eq_1[OF th0]
  2464   show "?dia = inverse ?d" by simp
  2465 qed
  2466 
  2467 lemma fps_inv_idempotent: 
  2468   assumes a0: "a$0 = 0" and a1: "a$1 \<noteq> 0"
  2469   shows "fps_inv (fps_inv a) = a"
  2470 proof-
  2471   let ?r = "fps_inv"
  2472   have ra0: "?r a $ 0 = 0" by (simp add: fps_inv_def)
  2473   from a1 have ra1: "?r a $ 1 \<noteq> 0" by (simp add: fps_inv_def field_simps)
  2474   have X0: "X$0 = 0" by simp
  2475   from fps_inv[OF ra0 ra1] have "?r (?r a) oo ?r a = X" .
  2476   then have "?r (?r a) oo ?r a oo a = X oo a" by simp
  2477   then have "?r (?r a) oo (?r a oo a) = a" 
  2478     unfolding X_fps_compose_startby0[OF a0]
  2479     unfolding fps_compose_assoc[OF a0 ra0, symmetric] .
  2480   then show ?thesis unfolding fps_inv[OF a0 a1] by simp
  2481 qed
  2482 
  2483 lemma fps_ginv_ginv:
  2484   assumes a0: "a$0 = 0" and a1: "a$1 \<noteq> 0"
  2485   and c0: "c$0 = 0" and  c1: "c$1 \<noteq> 0"
  2486   shows "fps_ginv b (fps_ginv c a) = b oo a oo fps_inv c"
  2487 proof-
  2488   let ?r = "fps_ginv"
  2489   from c0 have rca0: "?r c a $0 = 0" by (simp add: fps_ginv_def)
  2490   from a1 c1 have rca1: "?r c a $ 1 \<noteq> 0" by (simp add: fps_ginv_def field_simps)
  2491   from fps_ginv[OF rca0 rca1] 
  2492   have "?r b (?r c a) oo ?r c a = b" .
  2493   then have "?r b (?r c a) oo ?r c a oo a = b oo a" by simp
  2494   then have "?r b (?r c a) oo (?r c a oo a) = b oo a"
  2495     apply (subst fps_compose_assoc)
  2496     using a0 c0 by (auto simp add: fps_ginv_def)
  2497   then have "?r b (?r c a) oo c = b oo a"
  2498     unfolding fps_ginv[OF a0 a1] .
  2499   then have "?r b (?r c a) oo c oo fps_inv c= b oo a oo fps_inv c" by simp
  2500   then have "?r b (?r c a) oo (c oo fps_inv c) = b oo a oo fps_inv c"
  2501     apply (subst fps_compose_assoc)
  2502     using a0 c0 by (auto simp add: fps_inv_def)
  2503   then show ?thesis unfolding fps_inv_right[OF c0 c1] by simp
  2504 qed
  2505 
  2506 subsection{* Elementary series *}
  2507 
  2508 subsubsection{* Exponential series *}
  2509 definition "E x = Abs_fps (\<lambda>n. x^n / of_nat (fact n))"
  2510 
  2511 lemma E_deriv[simp]: "fps_deriv (E a) = fps_const (a::'a::field_char_0) * E a" (is "?l = ?r")
  2512 proof-
  2513   {fix n
  2514     have "?l$n = ?r $ n"
  2515   apply (auto simp add: E_def field_simps power_Suc[symmetric]simp del: fact_Suc of_nat_Suc power_Suc)
  2516   by (simp add: of_nat_mult ring_simps)}
  2517 then show ?thesis by (simp add: fps_eq_iff)
  2518 qed
  2519 
  2520 lemma E_unique_ODE:
  2521   "fps_deriv a = fps_const c * a \<longleftrightarrow> a = fps_const (a$0) * E (c :: 'a::field_char_0)"
  2522   (is "?lhs \<longleftrightarrow> ?rhs")
  2523 proof-
  2524   {assume d: ?lhs
  2525   from d have th: "\<And>n. a $ Suc n = c * a$n / of_nat (Suc n)"
  2526     by (simp add: fps_deriv_def fps_eq_iff field_simps del: of_nat_Suc)
  2527   {fix n have "a$n = a$0 * c ^ n/ (of_nat (fact n))"
  2528       apply (induct n)
  2529       apply simp
  2530       unfolding th
  2531       using fact_gt_zero
  2532       apply (simp add: field_simps del: of_nat_Suc fact.simps)
  2533       apply (drule sym)
  2534       by (simp add: ring_simps of_nat_mult power_Suc)}
  2535   note th' = this
  2536   have ?rhs
  2537     by (auto simp add: fps_eq_iff fps_const_mult_left E_def intro : th')}
  2538 moreover
  2539 {assume h: ?rhs
  2540   have ?lhs
  2541     apply (subst h)
  2542     apply simp
  2543     apply (simp only: h[symmetric])
  2544   by simp}
  2545 ultimately show ?thesis by blast
  2546 qed
  2547 
  2548 lemma E_add_mult: "E (a + b) = E (a::'a::field_char_0) * E b" (is "?l = ?r")
  2549 proof-
  2550   have "fps_deriv (?r) = fps_const (a+b) * ?r"
  2551     by (simp add: fps_const_add[symmetric] ring_simps del: fps_const_add)
  2552   then have "?r = ?l" apply (simp only: E_unique_ODE)
  2553     by (simp add: fps_mult_nth E_def)
  2554   then show ?thesis ..
  2555 qed
  2556 
  2557 lemma E_nth[simp]: "E a $ n = a^n / of_nat (fact n)"
  2558   by (simp add: E_def)
  2559 
  2560 lemma E0[simp]: "E (0::'a::{field}) = 1"
  2561   by (simp add: fps_eq_iff power_0_left)
  2562 
  2563 lemma E_neg: "E (- a) = inverse (E (a::'a::field_char_0))"
  2564 proof-
  2565   from E_add_mult[of a "- a"] have th0: "E a * E (- a) = 1"
  2566     by (simp )
  2567   have th1: "E a $ 0 \<noteq> 0" by simp
  2568   from fps_inverse_unique[OF th1 th0] show ?thesis by simp
  2569 qed
  2570 
  2571 lemma E_nth_deriv[simp]: "fps_nth_deriv n (E (a::'a::field_char_0)) = (fps_const a)^n * (E a)"
  2572   by (induct n, auto simp add: power_Suc)
  2573 
  2574 lemma X_compose_E[simp]: "X oo E (a::'a::{field}) = E a - 1"
  2575   by (simp add: fps_eq_iff X_fps_compose)
  2576 
  2577 lemma LE_compose:
  2578   assumes a: "a\<noteq>0"
  2579   shows "fps_inv (E a - 1) oo (E a - 1) = X"
  2580   and "(E a - 1) oo fps_inv (E a - 1) = X"
  2581 proof-
  2582   let ?b = "E a - 1"
  2583   have b0: "?b $ 0 = 0" by simp
  2584   have b1: "?b $ 1 \<noteq> 0" by (simp add: a)
  2585   from fps_inv[OF b0 b1] show "fps_inv (E a - 1) oo (E a - 1) = X" .
  2586   from fps_inv_right[OF b0 b1] show "(E a - 1) oo fps_inv (E a - 1) = X" .
  2587 qed
  2588 
  2589 
  2590 lemma fps_const_inverse:
  2591   "a \<noteq> 0 \<Longrightarrow> inverse (fps_const (a::'a::field)) = fps_const (inverse a)"
  2592   apply (auto simp add: fps_eq_iff fps_inverse_def) by (case_tac "n", auto)
  2593 
  2594 lemma inverse_one_plus_X:
  2595   "inverse (1 + X) = Abs_fps (\<lambda>n. (- 1 ::'a::{field})^n)"
  2596   (is "inverse ?l = ?r")
  2597 proof-
  2598   have th: "?l * ?r = 1"
  2599     by (auto simp add: ring_simps fps_eq_iff minus_one_power_iff)
  2600   have th': "?l $ 0 \<noteq> 0" by (simp add: )
  2601   from fps_inverse_unique[OF th' th] show ?thesis .
  2602 qed
  2603 
  2604 lemma E_power_mult: "(E (c::'a::field_char_0))^n = E (of_nat n * c)"
  2605   by (induct n, auto simp add: ring_simps E_add_mult power_Suc)
  2606 
  2607 lemma radical_E:
  2608   assumes r: "r (Suc k) 1 = 1" 
  2609   shows "fps_radical r (Suc k) (E (c::'a::{field_char_0})) = E (c / of_nat (Suc k))"
  2610 proof-
  2611   let ?ck = "(c / of_nat (Suc k))"
  2612   let ?r = "fps_radical r (Suc k)"
  2613   have eq0[simp]: "?ck * of_nat (Suc k) = c" "of_nat (Suc k) * ?ck = c"
  2614     by (simp_all del: of_nat_Suc)
  2615   have th0: "E ?ck ^ (Suc k) = E c" unfolding E_power_mult eq0 ..
  2616   have th: "r (Suc k) (E c $0) ^ Suc k = E c $ 0"
  2617     "r (Suc k) (E c $ 0) = E ?ck $ 0" "E c $ 0 \<noteq> 0" using r by simp_all
  2618   from th0 radical_unique[where r=r and k=k, OF th]
  2619   show ?thesis by auto 
  2620 qed
  2621 
  2622 lemma Ec_E1_eq: 
  2623   "E (1::'a::{field_char_0}) oo (fps_const c * X) = E c"
  2624   apply (auto simp add: fps_eq_iff E_def fps_compose_def power_mult_distrib)
  2625   by (simp add: cond_value_iff cond_application_beta setsum_delta' cong del: if_weak_cong)
  2626 
  2627 text{* The generalized binomial theorem as a  consequence of @{thm E_add_mult} *}
  2628 
  2629 lemma gbinomial_theorem: 
  2630   "((a::'a::{field_char_0, division_by_zero})+b) ^ n = (\<Sum>k=0..n. of_nat (n choose k) * a^k * b^(n-k))"
  2631 proof-
  2632   from E_add_mult[of a b] 
  2633   have "(E (a + b)) $ n = (E a * E b)$n" by simp
  2634   then have "(a + b) ^ n = (\<Sum>i\<Colon>nat = 0\<Colon>nat..n. a ^ i * b ^ (n - i)  * (of_nat (fact n) / of_nat (fact i * fact (n - i))))"
  2635     by (simp add: field_simps fps_mult_nth of_nat_mult[symmetric] setsum_right_distrib)
  2636   then show ?thesis 
  2637     apply simp
  2638     apply (rule setsum_cong2)
  2639     apply simp
  2640     apply (frule binomial_fact[where ?'a = 'a, symmetric])
  2641     by (simp add: field_simps of_nat_mult)
  2642 qed
  2643 
  2644 text{* And the nat-form -- also available from Binomial.thy *}
  2645 lemma binomial_theorem: "(a+b) ^ n = (\<Sum>k=0..n. (n choose k) * a^k * b^(n-k))"
  2646   using gbinomial_theorem[of "of_nat a" "of_nat b" n]
  2647   unfolding of_nat_add[symmetric] of_nat_power[symmetric] of_nat_mult[symmetric] of_nat_setsum[symmetric]
  2648   by simp
  2649 
  2650 subsubsection{* Logarithmic series *}
  2651 
  2652 lemma Abs_fps_if_0: 
  2653   "Abs_fps(%n. if n=0 then (v::'a::ring_1) else f n) = fps_const v + X * Abs_fps (%n. f (Suc n))"
  2654   by (auto simp add: fps_eq_iff)
  2655 
  2656 definition L:: "'a::{field_char_0} \<Rightarrow> 'a fps" where 
  2657   "L c \<equiv> fps_const (1/c) * Abs_fps (\<lambda>n. if n = 0 then 0 else (- 1) ^ (n - 1) / of_nat n)"
  2658 
  2659 lemma fps_deriv_L: "fps_deriv (L c) = fps_const (1/c) * inverse (1 + X)"
  2660   unfolding inverse_one_plus_X
  2661   by (simp add: L_def fps_eq_iff del: of_nat_Suc)
  2662 
  2663 lemma L_nth: "L c $ n = (if n=0 then 0 else 1/c * ((- 1) ^ (n - 1) / of_nat n))"
  2664   by (simp add: L_def field_simps)
  2665 
  2666 lemma L_0[simp]: "L c $ 0 = 0" by (simp add: L_def)
  2667 lemma L_E_inv:
  2668   assumes a: "a\<noteq> (0::'a::{field_char_0})"
  2669   shows "L a = fps_inv (E a - 1)" (is "?l = ?r")
  2670 proof-
  2671   let ?b = "E a - 1"
  2672   have b0: "?b $ 0 = 0" by simp
  2673   have b1: "?b $ 1 \<noteq> 0" by (simp add: a)
  2674   have "fps_deriv (E a - 1) oo fps_inv (E a - 1) = (fps_const a * (E a - 1) + fps_const a) oo fps_inv (E a - 1)"
  2675     by (simp add: ring_simps)
  2676   also have "\<dots> = fps_const a * (X + 1)" apply (simp add: fps_compose_add_distrib fps_const_mult_apply_left[symmetric] fps_inv_right[OF b0 b1])
  2677     by (simp add: ring_simps)
  2678   finally have eq: "fps_deriv (E a - 1) oo fps_inv (E a - 1) = fps_const a * (X + 1)" .
  2679   from fps_inv_deriv[OF b0 b1, unfolded eq]
  2680   have "fps_deriv (fps_inv ?b) = fps_const (inverse a) / (X + 1)"
  2681     using a 
  2682     by (simp add: fps_const_inverse eq fps_divide_def fps_inverse_mult)
  2683   hence "fps_deriv ?l = fps_deriv ?r"
  2684     by (simp add: fps_deriv_L add_commute fps_divide_def divide_inverse)
  2685   then show ?thesis unfolding fps_deriv_eq_iff
  2686     by (simp add: L_nth fps_inv_def)
  2687 qed
  2688 
  2689 lemma L_mult_add: 
  2690   assumes c0: "c\<noteq>0" and d0: "d\<noteq>0"
  2691   shows "L c + L d = fps_const (c+d) * L (c*d)"
  2692   (is "?r = ?l")
  2693 proof-
  2694   from c0 d0 have eq: "1/c + 1/d = (c+d)/(c*d)" by (simp add: field_simps)
  2695   have "fps_deriv ?r = fps_const (1/c + 1/d) * inverse (1 + X)"
  2696     by (simp add: fps_deriv_L fps_const_add[symmetric] algebra_simps del: fps_const_add)
  2697   also have "\<dots> = fps_deriv ?l"
  2698     apply (simp add: fps_deriv_L)
  2699     by (simp add: fps_eq_iff eq)
  2700   finally show ?thesis
  2701     unfolding fps_deriv_eq_iff by simp
  2702 qed
  2703 
  2704 subsubsection{* Binomial series *}
  2705 
  2706 definition "fps_binomial a = Abs_fps (\<lambda>n. a gchoose n)"
  2707 
  2708 lemma fps_binomial_nth[simp]: "fps_binomial a $ n = a gchoose n"
  2709   by (simp add: fps_binomial_def)
  2710 
  2711 lemma fps_binomial_ODE_unique:
  2712   fixes c :: "'a::field_char_0"
  2713   shows "fps_deriv a = (fps_const c * a) / (1 + X) \<longleftrightarrow> a = fps_const (a$0) * fps_binomial c"
  2714   (is "?lhs \<longleftrightarrow> ?rhs")
  2715 proof-
  2716   let ?da = "fps_deriv a"
  2717   let ?x1 = "(1 + X):: 'a fps"
  2718   let ?l = "?x1 * ?da"
  2719   let ?r = "fps_const c * a"
  2720   have x10: "?x1 $ 0 \<noteq> 0" by simp
  2721   have "?l = ?r \<longleftrightarrow> inverse ?x1 * ?l = inverse ?x1 * ?r" by simp
  2722   also have "\<dots> \<longleftrightarrow> ?da = (fps_const c * a) / ?x1"
  2723     apply (simp only: fps_divide_def  mult_assoc[symmetric] inverse_mult_eq_1[OF x10])
  2724     by (simp add: ring_simps)
  2725   finally have eq: "?l = ?r \<longleftrightarrow> ?lhs" by simp
  2726   moreover
  2727   {assume h: "?l = ?r" 
  2728     {fix n
  2729       from h have lrn: "?l $ n = ?r$n" by simp
  2730       
  2731       from lrn 
  2732       have "a$ Suc n = ((c - of_nat n) / of_nat (Suc n)) * a $n" 
  2733 	apply (simp add: ring_simps del: of_nat_Suc)
  2734 	by (cases n, simp_all add: field_simps del: of_nat_Suc)
  2735     }
  2736     note th0 = this
  2737     {fix n have "a$n = (c gchoose n) * a$0"
  2738       proof(induct n)
  2739 	case 0 thus ?case by simp
  2740       next
  2741 	case (Suc m)
  2742 	thus ?case unfolding th0
  2743 	  apply (simp add: field_simps del: of_nat_Suc)
  2744 	  unfolding mult_assoc[symmetric] gbinomial_mult_1
  2745 	  by (simp add: ring_simps)
  2746       qed}
  2747     note th1 = this
  2748     have ?rhs
  2749       apply (simp add: fps_eq_iff)
  2750       apply (subst th1)
  2751       by (simp add: ring_simps)}
  2752   moreover
  2753   {assume h: ?rhs
  2754   have th00:"\<And>x y. x * (a$0 * y) = a$0 * (x*y)" by (simp add: mult_commute)
  2755     have "?l = ?r" 
  2756       apply (subst h)
  2757       apply (subst (2) h)
  2758       apply (clarsimp simp add: fps_eq_iff ring_simps)
  2759       unfolding mult_assoc[symmetric] th00 gbinomial_mult_1
  2760       by (simp add: ring_simps gbinomial_mult_1)}
  2761   ultimately show ?thesis by blast
  2762 qed
  2763 
  2764 lemma fps_binomial_deriv: "fps_deriv (fps_binomial c) = fps_const c * fps_binomial c / (1 + X)"
  2765 proof-
  2766   let ?a = "fps_binomial c"
  2767   have th0: "?a = fps_const (?a$0) * ?a" by (simp)
  2768   from iffD2[OF fps_binomial_ODE_unique, OF th0] show ?thesis .
  2769 qed
  2770 
  2771 lemma fps_binomial_add_mult: "fps_binomial (c+d) = fps_binomial c * fps_binomial d" (is "?l = ?r")
  2772 proof-
  2773   let ?P = "?r - ?l"
  2774   let ?b = "fps_binomial"
  2775   let ?db = "\<lambda>x. fps_deriv (?b x)"
  2776   have "fps_deriv ?P = ?db c * ?b d + ?b c * ?db d - ?db (c + d)"  by simp
  2777   also have "\<dots> = inverse (1 + X) * (fps_const c * ?b c * ?b d + fps_const d * ?b c * ?b d - fps_const (c+d) * ?b (c + d))"
  2778     unfolding fps_binomial_deriv
  2779     by (simp add: fps_divide_def ring_simps)
  2780   also have "\<dots> = (fps_const (c + d)/ (1 + X)) * ?P"
  2781     by (simp add: ring_simps fps_divide_def fps_const_add[symmetric] del: fps_const_add)
  2782   finally have th0: "fps_deriv ?P = fps_const (c+d) * ?P / (1 + X)"
  2783     by (simp add: fps_divide_def)
  2784   have "?P = fps_const (?P$0) * ?b (c + d)"
  2785     unfolding fps_binomial_ODE_unique[symmetric]
  2786     using th0 by simp
  2787   hence "?P = 0" by (simp add: fps_mult_nth)
  2788   then show ?thesis by simp
  2789 qed
  2790 
  2791 lemma fps_minomial_minus_one: "fps_binomial (- 1) = inverse (1 + X)"
  2792   (is "?l = inverse ?r")
  2793 proof-
  2794   have th: "?r$0 \<noteq> 0" by simp
  2795   have th': "fps_deriv (inverse ?r) = fps_const (- 1) * inverse ?r / (1 + X)"
  2796     by (simp add: fps_inverse_deriv[OF th] fps_divide_def power2_eq_square mult_commute fps_const_neg[symmetric] del: fps_const_neg)
  2797   have eq: "inverse ?r $ 0 = 1"
  2798     by (simp add: fps_inverse_def)
  2799   from iffD1[OF fps_binomial_ODE_unique[of "inverse (1 + X)" "- 1"] th'] eq
  2800   show ?thesis by (simp add: fps_inverse_def)
  2801 qed
  2802 
  2803 text{* Vandermonde's Identity as a consequence *}
  2804 lemma gbinomial_Vandermonde: "setsum (\<lambda>k. (a gchoose k) * (b gchoose (n - k))) {0..n} = (a + b) gchoose n"
  2805 proof-
  2806   let ?ba = "fps_binomial a"
  2807   let ?bb = "fps_binomial b"
  2808   let ?bab = "fps_binomial (a + b)"
  2809   from fps_binomial_add_mult[of a b] have "?bab $ n = (?ba * ?bb)$n" by simp
  2810   then show ?thesis by (simp add: fps_mult_nth)
  2811 qed
  2812 
  2813 lemma binomial_Vandermonde: "setsum (\<lambda>k. (a choose k) * (b choose (n - k))) {0..n} = (a + b) choose n"
  2814   using gbinomial_Vandermonde[of "(of_nat a)" "of_nat b" n]
  2815   
  2816   apply (simp only: binomial_gbinomial[symmetric] of_nat_mult[symmetric] of_nat_setsum[symmetric] of_nat_add[symmetric])
  2817   by simp
  2818   
  2819 lemma binomial_Vandermonde_same: "setsum (\<lambda>k. (n choose k)^2) {0..n} = (2*n) choose n"
  2820   using binomial_Vandermonde[of n n n,symmetric]
  2821   unfolding nat_mult_2 apply (simp add: power2_eq_square)
  2822   apply (rule setsum_cong2)
  2823   by (auto intro:  binomial_symmetric)
  2824 
  2825 lemma Vandermonde_pochhammer_lemma:
  2826   fixes a :: "'a::field_char_0"
  2827   assumes b: "\<forall> j\<in>{0 ..<n}. b \<noteq> of_nat j"
  2828   shows "setsum (%k. (pochhammer (- a) k * pochhammer (- (of_nat n)) k) / (of_nat (fact k) * pochhammer (b - of_nat n + 1) k)) {0..n} = pochhammer (- (a+ b)) n / pochhammer (- b) n" (is "?l = ?r")
  2829 proof-
  2830   let ?m1 = "%m. (- 1 :: 'a) ^ m"
  2831   let ?f = "%m. of_nat (fact m)"
  2832   let ?p = "%(x::'a). pochhammer (- x)"
  2833   from b have bn0: "?p b n \<noteq> 0" unfolding pochhammer_eq_0_iff by simp
  2834   {fix k assume kn: "k \<in> {0..n}"
  2835     {assume c:"pochhammer (b - of_nat n + 1) n = 0"
  2836       then obtain j where j: "j < n" "b - of_nat n + 1 = - of_nat j"
  2837 	unfolding pochhammer_eq_0_iff by blast
  2838       from j have "b = of_nat n - of_nat j - of_nat 1" 
  2839 	by (simp add: algebra_simps)
  2840       then have "b = of_nat (n - j - 1)" 
  2841 	using j kn by (simp add: of_nat_diff)
  2842       with b have False using j by auto}
  2843     then have nz: "pochhammer (1 + b - of_nat n) n \<noteq> 0" 
  2844       by (auto simp add: algebra_simps)
  2845     
  2846     from nz kn have nz': "pochhammer (1 + b - of_nat n) k \<noteq> 0" 
  2847       by (simp add: pochhammer_neq_0_mono)
  2848     {assume k0: "k = 0 \<or> n =0" 
  2849       then have "b gchoose (n - k) = (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)" 
  2850 	using kn
  2851 	by (cases "k=0", simp_all add: gbinomial_pochhammer)}
  2852     moreover
  2853     {assume n0: "n \<noteq> 0" and k0: "k \<noteq> 0" 
  2854       then obtain m where m: "n = Suc m" by (cases n, auto)
  2855       from k0 obtain h where h: "k = Suc h" by (cases k, auto)
  2856       {assume kn: "k = n"
  2857 	then have "b gchoose (n - k) = (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)"
  2858 	  using kn pochhammer_minus'[where k=k and n=n and b=b]
  2859 	  apply (simp add:  pochhammer_same)
  2860 	  using bn0
  2861 	  by (simp add: field_simps power_add[symmetric])}
  2862       moreover
  2863       {assume nk: "k \<noteq> n"
  2864 	have m1nk: "?m1 n = setprod (%i. - 1) {0..m}" 
  2865 	  "?m1 k = setprod (%i. - 1) {0..h}"
  2866 	  by (simp_all add: setprod_constant m h)
  2867 	from kn nk have kn': "k < n" by simp
  2868 	have bnz0: "pochhammer (b - of_nat n + 1) k \<noteq> 0"
  2869 	  using bn0 kn 
  2870 	  unfolding pochhammer_eq_0_iff
  2871 	  apply auto
  2872 	  apply (erule_tac x= "n - ka - 1" in allE)
  2873 	  by (auto simp add: algebra_simps of_nat_diff)
  2874 	have eq1: "setprod (%k. (1::'a) + of_nat m - of_nat k) {0 .. h} = setprod of_nat {Suc (m - h) .. Suc m}"	
  2875 	  apply (rule strong_setprod_reindex_cong[where f="%k. Suc m - k "])
  2876 	  using kn' h m
  2877 	  apply (auto simp add: inj_on_def image_def)
  2878 	  apply (rule_tac x="Suc m - x" in bexI)
  2879 	  apply (simp_all add: of_nat_diff)
  2880 	  done
  2881 	
  2882 	have th1: "(?m1 k * ?p (of_nat n) k) / ?f n = 1 / of_nat(fact (n - k))"
  2883 	  unfolding m1nk 
  2884 	  
  2885 	  unfolding m h pochhammer_Suc_setprod
  2886 	  apply (simp add: field_simps del: fact_Suc id_def)
  2887 	  unfolding fact_setprod id_def
  2888 	  unfolding of_nat_setprod
  2889 	  unfolding setprod_timesf[symmetric]
  2890 	  apply auto
  2891 	  unfolding eq1
  2892 	  apply (subst setprod_Un_disjoint[symmetric])
  2893 	  apply (auto)
  2894 	  apply (rule setprod_cong)
  2895 	  apply auto
  2896 	  done
  2897 	have th20: "?m1 n * ?p b n = setprod (%i. b - of_nat i) {0..m}"
  2898 	  unfolding m1nk 
  2899 	  unfolding m h pochhammer_Suc_setprod
  2900 	  unfolding setprod_timesf[symmetric]
  2901 	  apply (rule setprod_cong)
  2902 	  apply auto
  2903 	  done
  2904 	have th21:"pochhammer (b - of_nat n + 1) k = setprod (%i. b - of_nat i) {n - k .. n - 1}"
  2905 	  unfolding h m 
  2906 	  unfolding pochhammer_Suc_setprod
  2907 	  apply (rule strong_setprod_reindex_cong[where f="%k. n - 1 - k"])
  2908 	  using kn
  2909 	  apply (auto simp add: inj_on_def m h image_def)
  2910 	  apply (rule_tac x= "m - x" in bexI)
  2911 	  by (auto simp add: of_nat_diff)
  2912 	
  2913 	have "?m1 n * ?p b n = pochhammer (b - of_nat n + 1) k * setprod (%i. b - of_nat i) {0.. n - k - 1}"
  2914 	  unfolding th20 th21
  2915 	  unfolding h m
  2916 	  apply (subst setprod_Un_disjoint[symmetric])
  2917 	  using kn' h m
  2918 	  apply auto
  2919 	  apply (rule setprod_cong)
  2920 	  apply auto
  2921 	  done
  2922 	then have th2: "(?m1 n * ?p b n)/pochhammer (b - of_nat n + 1) k = setprod (%i. b - of_nat i) {0.. n - k - 1}" 
  2923 	  using nz' by (simp add: field_simps)
  2924 	have "(?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k) = ((?m1 k * ?p (of_nat n) k) / ?f n) * ((?m1 n * ?p b n)/pochhammer (b - of_nat n + 1) k)"
  2925 	  using bnz0
  2926 	  by (simp add: field_simps)
  2927 	also have "\<dots> = b gchoose (n - k)" 
  2928 	  unfolding th1 th2
  2929 	  using kn' by (simp add: gbinomial_def)
  2930 	finally have "b gchoose (n - k) = (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)" by simp}
  2931       ultimately have "b gchoose (n - k) = (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)"
  2932 	by (cases "k =n", auto)}
  2933     ultimately have "b gchoose (n - k) = (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)" "pochhammer (1 + b - of_nat n) k \<noteq> 0 "
  2934       using nz' 
  2935       apply (cases "n=0", auto)
  2936       by (cases "k", auto)}
  2937   note th00 = this
  2938   have "?r = ((a + b) gchoose n) * (of_nat (fact n)/ (?m1 n * pochhammer (- b) n))"
  2939     unfolding gbinomial_pochhammer 
  2940     using bn0 by (auto simp add: field_simps)
  2941   also have "\<dots> = ?l"
  2942     unfolding gbinomial_Vandermonde[symmetric]
  2943     apply (simp add: th00)
  2944     unfolding gbinomial_pochhammer
  2945     using bn0 apply (simp add: setsum_left_distrib setsum_right_distrib field_simps)
  2946     apply (rule setsum_cong2)
  2947     apply (drule th00(2))
  2948     by (simp add: field_simps power_add[symmetric])
  2949   finally show ?thesis by simp
  2950 qed 
  2951 
  2952     
  2953 lemma Vandermonde_pochhammer:
  2954    fixes a :: "'a::field_char_0"
  2955   assumes c: "ALL i : {0..< n}. c \<noteq> - of_nat i"
  2956   shows "setsum (%k. (pochhammer a k * pochhammer (- (of_nat n)) k) / (of_nat (fact k) * pochhammer c k)) {0..n} = pochhammer (c - a) n / pochhammer c n"
  2957 proof-
  2958   let ?a = "- a"
  2959   let ?b = "c + of_nat n - 1"
  2960   have h: "\<forall> j \<in>{0..< n}. ?b \<noteq> of_nat j" using c
  2961     apply (auto simp add: algebra_simps of_nat_diff)
  2962     apply (erule_tac x= "n - j - 1" in ballE)
  2963     by (auto simp add: of_nat_diff algebra_simps)
  2964   have th0: "pochhammer (- (?a + ?b)) n = (- 1)^n * pochhammer (c - a) n"
  2965     unfolding pochhammer_minus[OF le_refl]
  2966     by (simp add: algebra_simps)
  2967   have th1: "pochhammer (- ?b) n = (- 1)^n * pochhammer c n"
  2968     unfolding pochhammer_minus[OF le_refl]
  2969     by simp
  2970   have nz: "pochhammer c n \<noteq> 0" using c
  2971     by (simp add: pochhammer_eq_0_iff)
  2972   from Vandermonde_pochhammer_lemma[where a = "?a" and b="?b" and n=n, OF h, unfolded th0 th1]
  2973   show ?thesis using nz by (simp add: field_simps setsum_right_distrib)
  2974 qed
  2975 
  2976 subsubsection{* Formal trigonometric functions  *}
  2977 
  2978 definition "fps_sin (c::'a::field_char_0) =
  2979   Abs_fps (\<lambda>n. if even n then 0 else (- 1) ^((n - 1) div 2) * c^n /(of_nat (fact n)))"
  2980 
  2981 definition "fps_cos (c::'a::field_char_0) =
  2982   Abs_fps (\<lambda>n. if even n then (- 1) ^ (n div 2) * c^n / (of_nat (fact n)) else 0)"
  2983 
  2984 lemma fps_sin_deriv:
  2985   "fps_deriv (fps_sin c) = fps_const c * fps_cos c"
  2986   (is "?lhs = ?rhs")
  2987 proof (rule fps_ext)
  2988   fix n::nat
  2989     {assume en: "even n"
  2990       have "?lhs$n = of_nat (n+1) * (fps_sin c $ (n+1))" by simp
  2991       also have "\<dots> = of_nat (n+1) * ((- 1)^(n div 2) * c^Suc n / of_nat (fact (Suc n)))"
  2992 	using en by (simp add: fps_sin_def)
  2993       also have "\<dots> = (- 1)^(n div 2) * c^Suc n * (of_nat (n+1) / (of_nat (Suc n) * of_nat (fact n)))"
  2994 	unfolding fact_Suc of_nat_mult
  2995 	by (simp add: field_simps del: of_nat_add of_nat_Suc)
  2996       also have "\<dots> = (- 1)^(n div 2) *c^Suc n / of_nat (fact n)"
  2997 	by (simp add: field_simps del: of_nat_add of_nat_Suc)
  2998       finally have "?lhs $n = ?rhs$n" using en
  2999 	by (simp add: fps_cos_def ring_simps power_Suc )}
  3000     then show "?lhs $ n = ?rhs $ n"
  3001       by (cases "even n", simp_all add: fps_deriv_def fps_sin_def fps_cos_def)
  3002 qed
  3003 
  3004 lemma fps_cos_deriv:
  3005   "fps_deriv (fps_cos c) = fps_const (- c)* (fps_sin c)"
  3006   (is "?lhs = ?rhs")
  3007 proof (rule fps_ext)
  3008   have th0: "\<And>n. - ((- 1::'a) ^ n) = (- 1)^Suc n" by (simp add: power_Suc)
  3009   have th1: "\<And>n. odd n \<Longrightarrow> Suc ((n - 1) div 2) = Suc n div 2"
  3010     by (case_tac n, simp_all)
  3011   fix n::nat
  3012     {assume en: "odd n"
  3013       from en have n0: "n \<noteq>0 " by presburger
  3014       have "?lhs$n = of_nat (n+1) * (fps_cos c $ (n+1))" by simp
  3015       also have "\<dots> = of_nat (n+1) * ((- 1)^((n + 1) div 2) * c^Suc n / of_nat (fact (Suc n)))"
  3016 	using en by (simp add: fps_cos_def)
  3017       also have "\<dots> = (- 1)^((n + 1) div 2)*c^Suc n * (of_nat (n+1) / (of_nat (Suc n) * of_nat (fact n)))"
  3018 	unfolding fact_Suc of_nat_mult
  3019 	by (simp add: field_simps del: of_nat_add of_nat_Suc)
  3020       also have "\<dots> = (- 1)^((n + 1) div 2) * c^Suc n / of_nat (fact n)"
  3021 	by (simp add: field_simps del: of_nat_add of_nat_Suc)
  3022       also have "\<dots> = (- ((- 1)^((n - 1) div 2))) * c^Suc n / of_nat (fact n)"
  3023 	unfolding th0 unfolding th1[OF en] by simp
  3024       finally have "?lhs $n = ?rhs$n" using en
  3025 	by (simp add: fps_sin_def ring_simps power_Suc)}
  3026     then show "?lhs $ n = ?rhs $ n"
  3027       by (cases "even n", simp_all add: fps_deriv_def fps_sin_def
  3028 	fps_cos_def)
  3029 qed
  3030 
  3031 lemma fps_sin_cos_sum_of_squares:
  3032   "fps_cos c ^ 2 + fps_sin c ^ 2 = 1" (is "?lhs = 1")
  3033 proof-
  3034   have "fps_deriv ?lhs = 0"
  3035     apply (simp add:  fps_deriv_power fps_sin_deriv fps_cos_deriv power_Suc)
  3036     by (simp add: ring_simps fps_const_neg[symmetric] del: fps_const_neg)
  3037   then have "?lhs = fps_const (?lhs $ 0)"
  3038     unfolding fps_deriv_eq_0_iff .
  3039   also have "\<dots> = 1"
  3040     by (auto simp add: fps_eq_iff numeral_2_eq_2 fps_mult_nth fps_cos_def fps_sin_def)
  3041   finally show ?thesis .
  3042 qed
  3043 
  3044 lemma fact_1 [simp]: "fact 1 = 1"
  3045 unfolding One_nat_def fact_Suc by simp
  3046 
  3047 lemma divide_eq_iff: "a \<noteq> (0::'a::field) \<Longrightarrow> x / a = y \<longleftrightarrow> x = y * a"
  3048 by auto
  3049 
  3050 lemma eq_divide_iff: "a \<noteq> (0::'a::field) \<Longrightarrow> x = y / a \<longleftrightarrow> x * a = y"
  3051 by auto
  3052 
  3053 lemma fps_sin_nth_0 [simp]: "fps_sin c $ 0 = 0"
  3054 unfolding fps_sin_def by simp
  3055 
  3056 lemma fps_sin_nth_1 [simp]: "fps_sin c $ 1 = c"
  3057 unfolding fps_sin_def by simp
  3058 
  3059 lemma fps_sin_nth_add_2:
  3060   "fps_sin c $ (n + 2) = - (c * c * fps_sin c $ n / (of_nat(n+1) * of_nat(n+2)))"
  3061 unfolding fps_sin_def
  3062 apply (cases n, simp)
  3063 apply (simp add: divide_eq_iff eq_divide_iff del: of_nat_Suc fact_Suc)
  3064 apply (simp add: of_nat_mult del: of_nat_Suc mult_Suc)
  3065 done
  3066 
  3067 lemma fps_cos_nth_0 [simp]: "fps_cos c $ 0 = 1"
  3068 unfolding fps_cos_def by simp
  3069 
  3070 lemma fps_cos_nth_1 [simp]: "fps_cos c $ 1 = 0"
  3071 unfolding fps_cos_def by simp
  3072 
  3073 lemma fps_cos_nth_add_2:
  3074   "fps_cos c $ (n + 2) = - (c * c * fps_cos c $ n / (of_nat(n+1) * of_nat(n+2)))"
  3075 unfolding fps_cos_def
  3076 apply (simp add: divide_eq_iff eq_divide_iff del: of_nat_Suc fact_Suc)
  3077 apply (simp add: of_nat_mult del: of_nat_Suc mult_Suc)
  3078 done
  3079 
  3080 lemma nat_induct2:
  3081   "\<lbrakk>P 0; P 1; \<And>n. P n \<Longrightarrow> P (n + 2)\<rbrakk> \<Longrightarrow> P (n::nat)"
  3082 unfolding One_nat_def numeral_2_eq_2
  3083 apply (induct n rule: nat_less_induct)
  3084 apply (case_tac n, simp)
  3085 apply (rename_tac m, case_tac m, simp)
  3086 apply (rename_tac k, case_tac k, simp_all)
  3087 done
  3088 
  3089 lemma nat_add_1_add_1: "(n::nat) + 1 + 1 = n + 2"
  3090 by simp
  3091 
  3092 lemma eq_fps_sin:
  3093   assumes 0: "a $ 0 = 0" and 1: "a $ 1 = c"
  3094   and 2: "fps_deriv (fps_deriv a) = - (fps_const c * fps_const c * a)"
  3095   shows "a = fps_sin c"
  3096 apply (rule fps_ext)
  3097 apply (induct_tac n rule: nat_induct2)
  3098 apply (simp add: fps_sin_nth_0 0)
  3099 apply (simp add: fps_sin_nth_1 1 del: One_nat_def)
  3100 apply (rename_tac m, cut_tac f="\<lambda>a. a $ m" in arg_cong [OF 2])
  3101 apply (simp add: nat_add_1_add_1 fps_sin_nth_add_2
  3102             del: One_nat_def of_nat_Suc of_nat_add add_2_eq_Suc')
  3103 apply (subst minus_divide_left)
  3104 apply (subst eq_divide_iff)
  3105 apply (simp del: of_nat_add of_nat_Suc)
  3106 apply (simp only: mult_ac)
  3107 done
  3108 
  3109 lemma eq_fps_cos:
  3110   assumes 0: "a $ 0 = 1" and 1: "a $ 1 = 0"
  3111   and 2: "fps_deriv (fps_deriv a) = - (fps_const c * fps_const c * a)"
  3112   shows "a = fps_cos c"
  3113 apply (rule fps_ext)
  3114 apply (induct_tac n rule: nat_induct2)
  3115 apply (simp add: fps_cos_nth_0 0)
  3116 apply (simp add: fps_cos_nth_1 1 del: One_nat_def)
  3117 apply (rename_tac m, cut_tac f="\<lambda>a. a $ m" in arg_cong [OF 2])
  3118 apply (simp add: nat_add_1_add_1 fps_cos_nth_add_2
  3119             del: One_nat_def of_nat_Suc of_nat_add add_2_eq_Suc')
  3120 apply (subst minus_divide_left)
  3121 apply (subst eq_divide_iff)
  3122 apply (simp del: of_nat_add of_nat_Suc)
  3123 apply (simp only: mult_ac)
  3124 done
  3125 
  3126 lemma mult_nth_0 [simp]: "(a * b) $ 0 = a $ 0 * b $ 0"
  3127 by (simp add: fps_mult_nth)
  3128 
  3129 lemma mult_nth_1 [simp]: "(a * b) $ 1 = a $ 0 * b $ 1 + a $ 1 * b $ 0"
  3130 by (simp add: fps_mult_nth)
  3131 
  3132 lemma fps_sin_add:
  3133   "fps_sin (a + b) = fps_sin a * fps_cos b + fps_cos a * fps_sin b"
  3134 apply (rule eq_fps_sin [symmetric], simp, simp del: One_nat_def)
  3135 apply (simp del: fps_const_neg fps_const_add fps_const_mult
  3136             add: fps_const_add [symmetric] fps_const_neg [symmetric]
  3137                  fps_sin_deriv fps_cos_deriv algebra_simps)
  3138 done
  3139 
  3140 lemma fps_cos_add:
  3141   "fps_cos (a + b) = fps_cos a * fps_cos b - fps_sin a * fps_sin b"
  3142 apply (rule eq_fps_cos [symmetric], simp, simp del: One_nat_def)
  3143 apply (simp del: fps_const_neg fps_const_add fps_const_mult
  3144             add: fps_const_add [symmetric] fps_const_neg [symmetric]
  3145                  fps_sin_deriv fps_cos_deriv algebra_simps)
  3146 done
  3147 
  3148 lemma fps_sin_even: "fps_sin (- c) = - fps_sin c"
  3149   by (auto simp add: fps_eq_iff fps_sin_def)
  3150 
  3151 lemma fps_cos_odd: "fps_cos (- c) = fps_cos c"
  3152   by (auto simp add: fps_eq_iff fps_cos_def)
  3153 
  3154 definition "fps_tan c = fps_sin c / fps_cos c"
  3155 
  3156 lemma fps_tan_deriv: "fps_deriv(fps_tan c) = fps_const c/ (fps_cos c ^ 2)"
  3157 proof-
  3158   have th0: "fps_cos c $ 0 \<noteq> 0" by (simp add: fps_cos_def)
  3159   show ?thesis
  3160     using fps_sin_cos_sum_of_squares[of c]
  3161     apply (simp add: fps_tan_def fps_divide_deriv[OF th0] fps_sin_deriv fps_cos_deriv add: fps_const_neg[symmetric] ring_simps power2_eq_square del: fps_const_neg)
  3162     unfolding right_distrib[symmetric]
  3163     by simp
  3164 qed
  3165 
  3166 text {* Connection to E c over the complex numbers --- Euler and De Moivre*}
  3167 lemma Eii_sin_cos:
  3168   "E (ii * c) = fps_cos c + fps_const ii * fps_sin c "
  3169   (is "?l = ?r")
  3170 proof-
  3171   {fix n::nat
  3172     {assume en: "even n"
  3173       from en obtain m where m: "n = 2*m" 
  3174 	unfolding even_mult_two_ex by blast
  3175       
  3176       have "?l $n = ?r$n" 
  3177 	by (simp add: m fps_sin_def fps_cos_def power_mult_distrib
  3178 	  power_mult power_minus)}
  3179     moreover
  3180     {assume on: "odd n"
  3181       from on obtain m where m: "n = 2*m + 1" 
  3182 	unfolding odd_nat_equiv_def2 by (auto simp add: nat_mult_2)  
  3183       have "?l $n = ?r$n" 
  3184 	by (simp add: m fps_sin_def fps_cos_def power_mult_distrib
  3185 	  power_mult power_minus)}
  3186     ultimately have "?l $n = ?r$n"  by blast}
  3187   then show ?thesis by (simp add: fps_eq_iff)
  3188 qed
  3189 
  3190 lemma E_minus_ii_sin_cos: "E (- (ii * c)) = fps_cos c - fps_const ii * fps_sin c "
  3191   unfolding minus_mult_right Eii_sin_cos by (simp add: fps_sin_even fps_cos_odd)
  3192 
  3193 lemma fps_const_minus: "fps_const (c::'a::group_add) - fps_const d = fps_const (c - d)"
  3194   by (simp add: fps_eq_iff fps_const_def)
  3195 
  3196 lemma fps_number_of_fps_const: "number_of i = fps_const (number_of i :: 'a:: {comm_ring_1, number_ring})"
  3197   apply (subst (2) number_of_eq)
  3198 apply(rule int_induct[of _ 0])
  3199 apply (simp_all add: number_of_fps_def)
  3200 by (simp_all add: fps_const_add[symmetric] fps_const_minus[symmetric])
  3201 
  3202 lemma fps_cos_Eii:
  3203   "fps_cos c = (E (ii * c) + E (- ii * c)) / fps_const 2"
  3204 proof-
  3205   have th: "fps_cos c + fps_cos c = fps_cos c * fps_const 2" 
  3206     by (simp add: fps_eq_iff fps_number_of_fps_const complex_number_of_def[symmetric])
  3207   show ?thesis
  3208   unfolding Eii_sin_cos minus_mult_commute
  3209   by (simp add: fps_sin_even fps_cos_odd fps_number_of_fps_const
  3210     fps_divide_def fps_const_inverse th complex_number_of_def[symmetric])
  3211 qed
  3212 
  3213 lemma fps_sin_Eii:
  3214   "fps_sin c = (E (ii * c) - E (- ii * c)) / fps_const (2*ii)"
  3215 proof-
  3216   have th: "fps_const \<i> * fps_sin c + fps_const \<i> * fps_sin c = fps_sin c * fps_const (2 * ii)" 
  3217     by (simp add: fps_eq_iff fps_number_of_fps_const complex_number_of_def[symmetric])
  3218   show ?thesis
  3219   unfolding Eii_sin_cos minus_mult_commute
  3220   by (simp add: fps_sin_even fps_cos_odd fps_divide_def fps_const_inverse th)
  3221 qed
  3222 
  3223 lemma fps_tan_Eii:
  3224   "fps_tan c = (E (ii * c) - E (- ii * c)) / (fps_const ii * (E (ii * c) + E (- ii * c)))"
  3225   unfolding fps_tan_def fps_sin_Eii fps_cos_Eii mult_minus_left E_neg
  3226   apply (simp add: fps_divide_def fps_inverse_mult fps_const_mult[symmetric] fps_const_inverse del: fps_const_mult)
  3227   by simp
  3228 
  3229 lemma fps_demoivre: "(fps_cos a + fps_const ii * fps_sin a)^n = fps_cos (of_nat n * a) + fps_const ii * fps_sin (of_nat n * a)"
  3230   unfolding Eii_sin_cos[symmetric] E_power_mult
  3231   by (simp add: mult_ac)
  3232 
  3233 subsection {* Hypergeometric series *}
  3234 
  3235 
  3236 definition "F as bs (c::'a::{field_char_0, division_by_zero}) = Abs_fps (%n. (foldl (%r a. r* pochhammer a n) 1 as * c^n)/ (foldl (%r b. r * pochhammer b n) 1 bs * of_nat (fact n)))"
  3237 
  3238 lemma F_nth[simp]: "F as bs c $ n =  (foldl (%r a. r* pochhammer a n) 1 as * c^n)/ (foldl (%r b. r * pochhammer b n) 1 bs * of_nat (fact n))"
  3239   by (simp add: F_def)
  3240 
  3241 lemma foldl_mult_start:
  3242   "foldl (%r x. r * f x) (v::'a::comm_ring_1) as * x = foldl (%r x. r * f x) (v * x) as "
  3243   by (induct as arbitrary: x v, auto simp add: algebra_simps)
  3244 
  3245 lemma foldr_mult_foldl: "foldr (%x r. r * f x) as v = foldl (%r x. r * f x) (v :: 'a::comm_ring_1) as"
  3246   by (induct as arbitrary: v, auto simp add: foldl_mult_start)
  3247 
  3248 lemma F_nth_alt: "F as bs c $ n = foldr (\<lambda>a r. r * pochhammer a n) as (c ^ n) /
  3249     foldr (\<lambda>b r. r * pochhammer b n) bs (of_nat (fact n))"
  3250   by (simp add: foldl_mult_start foldr_mult_foldl)
  3251 
  3252 lemma F_E[simp]: "F [] [] c = E c" 
  3253   by (simp add: fps_eq_iff)
  3254 
  3255 lemma F_1_0[simp]: "F [1] [] c = 1/(1 - fps_const c * X)"
  3256 proof-
  3257   let ?a = "(Abs_fps (\<lambda>n. 1)) oo (fps_const c * X)"
  3258   thm gp
  3259   have th0: "(fps_const c * X) $ 0 = 0" by simp
  3260   show ?thesis unfolding gp[OF th0, symmetric]
  3261     by (auto simp add: fps_eq_iff pochhammer_fact[symmetric] fps_compose_nth power_mult_distrib cond_value_iff setsum_delta' cong del: if_weak_cong)
  3262 qed
  3263 
  3264 lemma F_B[simp]: "F [-a] [] (- 1) = fps_binomial a"
  3265   by (simp add: fps_eq_iff gbinomial_pochhammer algebra_simps)
  3266 
  3267 lemma F_0[simp]: "F as bs c $0 = 1"
  3268   apply simp
  3269   apply (subgoal_tac "ALL as. foldl (%(r::'a) (a::'a). r) 1 as = 1")
  3270   apply auto
  3271   apply (induct_tac as, auto)
  3272   done
  3273 
  3274 lemma foldl_prod_prod: "foldl (%(r::'b::comm_ring_1) (x::'a::comm_ring_1). r * f x) v as * foldl (%r x. r * g x) w as = foldl (%r x. r * f x * g x) (v*w) as"
  3275   by (induct as arbitrary: v w, auto simp add: algebra_simps)
  3276 
  3277 
  3278 lemma F_rec: "F as bs c $ Suc n = ((foldl (%r a. r* (a + of_nat n)) c as)/ (foldl (%r b. r * (b + of_nat n)) (of_nat (Suc n)) bs )) * F as bs c $ n"
  3279   apply (simp del: of_nat_Suc of_nat_add fact_Suc)
  3280   apply (simp add: foldl_mult_start del: fact_Suc of_nat_Suc)
  3281   unfolding foldl_prod_prod[unfolded foldl_mult_start] pochhammer_Suc
  3282   by (simp add: algebra_simps of_nat_mult)
  3283 
  3284 lemma XD_nth[simp]: "XD a $ n = (if n=0 then 0 else of_nat n * a$n)"
  3285   by (simp add: XD_def)
  3286 
  3287 lemma XD_0th[simp]: "XD a $ 0 = 0" by simp
  3288 lemma XD_Suc[simp]:" XD a $ Suc n = of_nat (Suc n) * a $ Suc n" by simp
  3289 
  3290 definition "XDp c a = XD a + fps_const c * a"
  3291 
  3292 lemma XDp_nth[simp]: "XDp c a $ n = (c + of_nat n) * a$n"
  3293   by (simp add: XDp_def algebra_simps)
  3294 
  3295 lemma XDp_commute:
  3296   shows "XDp b o XDp (c::'a::comm_ring_1) = XDp c o XDp b"
  3297   by (auto simp add: XDp_def expand_fun_eq fps_eq_iff algebra_simps)
  3298 
  3299 lemma XDp0[simp]: "XDp 0 = XD"
  3300   by (simp add: expand_fun_eq fps_eq_iff)
  3301 
  3302 lemma XDp_fps_integral[simp]:"XDp 0 (fps_integral a c) = X * a"
  3303   by (simp add: fps_eq_iff fps_integral_def)
  3304 
  3305 lemma F_minus_nat: 
  3306   "F [- of_nat n] [- of_nat (n + m)] (c::'a::{field_char_0, division_by_zero}) $ k = (if k <= n then pochhammer (- of_nat n) k * c ^ k /
  3307     (pochhammer (- of_nat (n + m)) k * of_nat (fact k)) else 0)"
  3308   "F [- of_nat m] [- of_nat (m + n)] (c::'a::{field_char_0, division_by_zero}) $ k = (if k <= m then pochhammer (- of_nat m) k * c ^ k /
  3309     (pochhammer (- of_nat (m + n)) k * of_nat (fact k)) else 0)"
  3310   by (auto simp add: pochhammer_eq_0_iff)
  3311 
  3312 lemma setsum_eq_if: "setsum f {(n::nat) .. m} = (if m < n then 0 else f n + setsum f {n+1 .. m})"
  3313   apply simp
  3314   apply (subst setsum_insert[symmetric])
  3315   by (auto simp add: not_less setsum_head_Suc)
  3316 
  3317 lemma pochhammer_rec_if: 
  3318   "pochhammer a n = (if n = 0 then 1 else a * pochhammer (a + 1) (n - 1))"
  3319   by (cases n, simp_all add: pochhammer_rec)
  3320 
  3321 lemma XDp_foldr_nth[simp]: "foldr (%c r. XDp c o r) cs (%c. XDp c a) c0 $ n = 
  3322   foldr (%c r. (c + of_nat n) * r) cs (c0 + of_nat n) * a$n"
  3323   by (induct cs arbitrary: c0, auto simp add: algebra_simps)
  3324 
  3325 lemma genric_XDp_foldr_nth:
  3326   assumes 
  3327   f: "ALL n c a. f c a $ n = (of_nat n + k c) * a$n"
  3328 
  3329   shows "foldr (%c r. f c o r) cs (%c. g c a) c0 $ n = 
  3330   foldr (%c r. (k c + of_nat n) * r) cs (g c0 a $ n)"
  3331   by (induct cs arbitrary: c0, auto simp add: algebra_simps f )
  3332 
  3333 end