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