src/HOL/Library/Formal_Power_Series.thy
author huffman
Sat Feb 14 15:30:26 2009 -0800 (2009-02-14)
changeset 29913 89eadbe71e97
parent 29912 f4ac160d2e77
child 29914 c9ced4f54e82
permissions -rw-r--r--
add mult_delta lemmas; simplify some proofs
     1 (*  Title:      Formal_Power_Series.thy
     2     ID:         
     3     Author:     Amine Chaieb, University of Cambridge
     4 *)
     5 
     6 header{* A formalization of formal power series *}
     7 
     8 theory Formal_Power_Series
     9   imports Main Fact Parity
    10 begin
    11 
    12 subsection {* The type of formal power series*}
    13 
    14 typedef (open) 'a fps = "{f :: nat \<Rightarrow> 'a. True}"
    15   morphisms fps_nth Abs_fps
    16   by simp
    17 
    18 notation fps_nth (infixl "$" 75)
    19 
    20 lemma expand_fps_eq: "p = q \<longleftrightarrow> (\<forall>n. p $ n = q $ n)"
    21   by (simp add: fps_nth_inject [symmetric] expand_fun_eq)
    22 
    23 lemma fps_ext: "(\<And>n. p $ n = q $ n) \<Longrightarrow> p = q"
    24   by (simp add: expand_fps_eq)
    25 
    26 lemma fps_nth_Abs_fps [simp]: "Abs_fps f $ n = f n"
    27   by (simp add: Abs_fps_inverse)
    28 
    29 text{* Definition of the basic elements 0 and 1 and the basic operations of addition, negation and multiplication *}
    30 
    31 instantiation fps :: (zero)  zero
    32 begin
    33 
    34 definition fps_zero_def:
    35   "0 = Abs_fps (\<lambda>n. 0)"
    36 
    37 instance ..
    38 end
    39 
    40 lemma fps_zero_nth [simp]: "0 $ n = 0"
    41   unfolding fps_zero_def by simp
    42 
    43 instantiation fps :: ("{one,zero}")  one
    44 begin
    45 
    46 definition fps_one_def:
    47   "1 = Abs_fps (\<lambda>n. if n = 0 then 1 else 0)"
    48 
    49 instance ..
    50 end
    51 
    52 lemma fps_one_nth [simp]: "1 $ n = (if n = 0 then 1 else 0)" 
    53   unfolding fps_one_def by simp
    54 
    55 instantiation fps :: (plus)  plus
    56 begin
    57 
    58 definition fps_plus_def:
    59   "op + = (\<lambda>f g. Abs_fps (\<lambda>n. f $ n + g $ n))"
    60 
    61 instance ..
    62 end
    63 
    64 lemma fps_add_nth [simp]: "(f + g) $ n = f $ n + g $ n"
    65   unfolding fps_plus_def by simp
    66 
    67 instantiation fps :: (minus) minus
    68 begin
    69 
    70 definition fps_minus_def:
    71   "op - = (\<lambda>f g. Abs_fps (\<lambda>n. f $ n - g $ n))"
    72 
    73 instance ..
    74 end
    75 
    76 lemma fps_sub_nth [simp]: "(f - g) $ n = f $ n - g $ n"
    77   unfolding fps_minus_def by simp
    78 
    79 instantiation fps :: (uminus) uminus
    80 begin
    81 
    82 definition fps_uminus_def:
    83   "uminus = (\<lambda>f. Abs_fps (\<lambda>n. - (f $ n)))"
    84 
    85 instance ..
    86 end
    87 
    88 lemma fps_neg_nth [simp]: "(- f) $ n = - (f $ n)"
    89   unfolding fps_uminus_def by simp
    90 
    91 instantiation fps :: ("{comm_monoid_add, times}")  times
    92 begin
    93 
    94 definition fps_times_def:
    95   "op * = (\<lambda>f g. Abs_fps (\<lambda>n. \<Sum>i=0..n. f $ i * g $ (n - i)))"
    96 
    97 instance ..
    98 end
    99 
   100 lemma fps_mult_nth: "(f * g) $ n = (\<Sum>i=0..n. f$i * g$(n - i))"
   101   unfolding fps_times_def by simp
   102 
   103 declare atLeastAtMost_iff[presburger]
   104 declare Bex_def[presburger]
   105 declare Ball_def[presburger]
   106 
   107 lemma mult_delta_left:
   108   fixes x y :: "'a::mult_zero"
   109   shows "(if b then x else 0) * y = (if b then x * y else 0)"
   110   by simp
   111 
   112 lemma mult_delta_right:
   113   fixes x y :: "'a::mult_zero"
   114   shows "x * (if b then y else 0) = (if b then x * y else 0)"
   115   by simp
   116 
   117 lemma cond_value_iff: "f (if b then x else y) = (if b then f x else f y)"
   118   by auto
   119 lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
   120   by auto
   121 
   122 subsection{* Formal power series form a commutative ring with unity, if the range of sequences 
   123   they represent is a commutative ring with unity*}
   124 
   125 instance fps :: (semigroup_add) semigroup_add
   126 proof
   127   fix a b c :: "'a fps" show "a + b + c = a + (b + c)"
   128     by (simp add: fps_ext add_assoc)
   129 qed
   130 
   131 instance fps :: (ab_semigroup_add) ab_semigroup_add
   132 proof
   133   fix a b :: "'a fps" show "a + b = b + a"
   134     by (simp add: fps_ext add_commute)
   135 qed
   136 
   137 lemma fps_mult_assoc_lemma:
   138   fixes k :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
   139   shows "(\<Sum>j=0..k. \<Sum>i=0..j. f i (j - i) (n - j)) =
   140          (\<Sum>j=0..k. \<Sum>i=0..k - j. f j i (n - j - i))"
   141 proof (induct k)
   142   case 0 show ?case by simp
   143 next
   144   case (Suc k) thus ?case
   145     by (simp add: Suc_diff_le setsum_addf add_assoc
   146              cong: strong_setsum_cong)
   147 qed
   148 
   149 instance fps :: (semiring_0) semigroup_mult
   150 proof
   151   fix a b c :: "'a fps"
   152   show "(a * b) * c = a * (b * c)"
   153   proof (rule fps_ext)
   154     fix n :: nat
   155     have "(\<Sum>j=0..n. \<Sum>i=0..j. a$i * b$(j - i) * c$(n - j)) =
   156           (\<Sum>j=0..n. \<Sum>i=0..n - j. a$j * b$i * c$(n - j - i))"
   157       by (rule fps_mult_assoc_lemma)
   158     thus "((a * b) * c) $ n = (a * (b * c)) $ n"
   159       by (simp add: fps_mult_nth setsum_right_distrib
   160                     setsum_left_distrib mult_assoc)
   161   qed
   162 qed
   163 
   164 lemma fps_mult_commute_lemma:
   165   fixes n :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
   166   shows "(\<Sum>i=0..n. f i (n - i)) = (\<Sum>i=0..n. f (n - i) i)"
   167 proof (rule setsum_reindex_cong)
   168   show "inj_on (\<lambda>i. n - i) {0..n}"
   169     by (rule inj_onI) simp
   170   show "{0..n} = (\<lambda>i. n - i) ` {0..n}"
   171     by (auto, rule_tac x="n - x" in image_eqI, simp_all)
   172 next
   173   fix i assume "i \<in> {0..n}"
   174   hence "n - (n - i) = i" by simp
   175   thus "f (n - i) i = f (n - i) (n - (n - i))" by simp
   176 qed
   177 
   178 instance fps :: (comm_semiring_0) ab_semigroup_mult
   179 proof
   180   fix a b :: "'a fps"
   181   show "a * b = b * a"
   182   proof (rule fps_ext)
   183     fix n :: nat
   184     have "(\<Sum>i=0..n. a$i * b$(n - i)) = (\<Sum>i=0..n. a$(n - i) * b$i)"
   185       by (rule fps_mult_commute_lemma)
   186     thus "(a * b) $ n = (b * a) $ n"
   187       by (simp add: fps_mult_nth mult_commute)
   188   qed
   189 qed
   190 
   191 instance fps :: (monoid_add) monoid_add
   192 proof
   193   fix a :: "'a fps" show "0 + a = a "
   194     by (simp add: fps_ext)
   195 next
   196   fix a :: "'a fps" show "a + 0 = a "
   197     by (simp add: fps_ext)
   198 qed
   199 
   200 instance fps :: (comm_monoid_add) comm_monoid_add
   201 proof
   202   fix a :: "'a fps" show "0 + a = a "
   203     by (simp add: fps_ext)
   204 qed
   205 
   206 instance fps :: (semiring_1) monoid_mult
   207 proof
   208   fix a :: "'a fps" show "1 * a = a"
   209     by (simp add: fps_ext fps_mult_nth mult_delta_left setsum_delta)
   210 next
   211   fix a :: "'a fps" show "a * 1 = a"
   212     by (simp add: fps_ext fps_mult_nth mult_delta_right setsum_delta')
   213 qed
   214 
   215 instance fps :: (cancel_semigroup_add) cancel_semigroup_add
   216 proof
   217   fix a b c :: "'a fps"
   218   assume "a + b = a + c" then show "b = c"
   219     by (simp add: expand_fps_eq)
   220 next
   221   fix a b c :: "'a fps"
   222   assume "b + a = c + a" then show "b = c"
   223     by (simp add: expand_fps_eq)
   224 qed
   225 
   226 instance fps :: (cancel_ab_semigroup_add) cancel_ab_semigroup_add
   227 proof
   228   fix a b c :: "'a fps"
   229   assume "a + b = a + c" then show "b = c"
   230     by (simp add: expand_fps_eq)
   231 qed
   232 
   233 instance fps :: (cancel_comm_monoid_add) cancel_comm_monoid_add ..
   234 
   235 instance fps :: (group_add) group_add
   236 proof
   237   fix a :: "'a fps" show "- a + a = 0"
   238     by (simp add: fps_ext)
   239 next
   240   fix a b :: "'a fps" show "a - b = a + - b"
   241     by (simp add: fps_ext diff_minus)
   242 qed
   243 
   244 instance fps :: (ab_group_add) ab_group_add
   245 proof
   246   fix a :: "'a fps"
   247   show "- a + a = 0"
   248     by (simp add: fps_ext)
   249 next
   250   fix a b :: "'a fps"
   251   show "a - b = a + - b"
   252     by (simp add: fps_ext)
   253 qed
   254 
   255 instance fps :: (zero_neq_one) zero_neq_one
   256   by default (simp add: expand_fps_eq)
   257 
   258 instance fps :: (semiring_0) semiring
   259 proof
   260   fix a b c :: "'a fps"
   261   show "(a + b) * c = a * c + b * c"
   262     by (simp add: expand_fps_eq fps_mult_nth left_distrib setsum_addf)
   263 next
   264   fix a b c :: "'a fps"
   265   show "a * (b + c) = a * b + a * c"
   266     by (simp add: expand_fps_eq fps_mult_nth right_distrib setsum_addf)
   267 qed
   268 
   269 instance fps :: (semiring_0) semiring_0
   270 proof
   271   fix a:: "'a fps" show "0 * a = 0"
   272     by (simp add: fps_ext fps_mult_nth)
   273 next
   274   fix a:: "'a fps" show "a * 0 = 0"
   275     by (simp add: fps_ext fps_mult_nth)
   276 qed
   277 
   278 instance fps :: (semiring_0_cancel) semiring_0_cancel ..
   279 
   280 subsection {* Selection of the nth power of the implicit variable in the infinite sum*}
   281 
   282 lemma fps_nonzero_nth: "f \<noteq> 0 \<longleftrightarrow> (\<exists> n. f $n \<noteq> 0)"
   283   by (simp add: expand_fps_eq)
   284 
   285 lemma fps_nonzero_nth_minimal:
   286   "f \<noteq> 0 \<longleftrightarrow> (\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0))"
   287 proof
   288   let ?n = "LEAST n. f $ n \<noteq> 0"
   289   assume "f \<noteq> 0"
   290   then have "\<exists>n. f $ n \<noteq> 0"
   291     by (simp add: fps_nonzero_nth)
   292   then have "f $ ?n \<noteq> 0"
   293     by (rule LeastI_ex)
   294   moreover have "\<forall>m<?n. f $ m = 0"
   295     by (auto dest: not_less_Least)
   296   ultimately have "f $ ?n \<noteq> 0 \<and> (\<forall>m<?n. f $ m = 0)" ..
   297   then show "\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0)" ..
   298 next
   299   assume "\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0)"
   300   then show "f \<noteq> 0" by (auto simp add: expand_fps_eq)
   301 qed
   302 
   303 lemma fps_eq_iff: "f = g \<longleftrightarrow> (\<forall>n. f $ n = g $n)"
   304   by (rule expand_fps_eq)
   305 
   306 lemma fps_setsum_nth: "(setsum f S) $ n = setsum (\<lambda>k. (f k) $ n) S" 
   307 proof (cases "finite S")
   308   assume "\<not> finite S" then show ?thesis by simp
   309 next
   310   assume "finite S"
   311   then show ?thesis by (induct set: finite) auto
   312 qed
   313 
   314 subsection{* Injection of the basic ring elements and multiplication by scalars *}
   315 
   316 definition
   317   "fps_const c = Abs_fps (\<lambda>n. if n = 0 then c else 0)"
   318 
   319 lemma fps_nth_fps_const [simp]: "fps_const c $ n = (if n = 0 then c else 0)"
   320   unfolding fps_const_def by simp
   321 
   322 lemma fps_const_0_eq_0 [simp]: "fps_const 0 = 0"
   323   by (simp add: fps_ext)
   324 
   325 lemma fps_const_1_eq_1 [simp]: "fps_const 1 = 1"
   326   by (simp add: fps_ext)
   327 
   328 lemma fps_const_neg [simp]: "- (fps_const (c::'a::ring)) = fps_const (- c)"
   329   by (simp add: fps_ext)
   330 
   331 lemma fps_const_add [simp]: "fps_const (c::'a\<Colon>monoid_add) + fps_const d = fps_const (c + d)"
   332   by (simp add: fps_ext)
   333 
   334 lemma fps_const_mult[simp]: "fps_const (c::'a\<Colon>ring) * fps_const d = fps_const (c * d)"
   335   by (simp add: fps_eq_iff fps_mult_nth setsum_0')
   336 
   337 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)"
   338   by (simp add: fps_ext)
   339 
   340 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)"
   341   by (simp add: fps_ext)
   342 
   343 lemma fps_const_mult_left: "fps_const (c::'a\<Colon>semiring_0) * f = Abs_fps (\<lambda>n. c * f$n)"
   344   unfolding fps_eq_iff fps_mult_nth
   345   by (simp add: fps_const_def mult_delta_left setsum_delta)
   346 
   347 lemma fps_const_mult_right: "f * fps_const (c::'a\<Colon>semiring_0) = Abs_fps (\<lambda>n. f$n * c)"
   348   unfolding fps_eq_iff fps_mult_nth
   349   by (simp add: fps_const_def mult_delta_right setsum_delta')
   350 
   351 lemma fps_mult_left_const_nth [simp]: "(fps_const (c::'a::semiring_1) * f)$n = c* f$n"
   352   by (simp add: fps_mult_nth mult_delta_left setsum_delta)
   353 
   354 lemma fps_mult_right_const_nth [simp]: "(f * fps_const (c::'a::semiring_1))$n = f$n * c"
   355   by (simp add: fps_mult_nth mult_delta_right setsum_delta')
   356 
   357 subsection {* Formal power series form an integral domain*}
   358 
   359 instance fps :: (ring) ring ..
   360 
   361 instance fps :: (ring_1) ring_1
   362   by (intro_classes, auto simp add: diff_minus left_distrib)
   363 
   364 instance fps :: (comm_ring_1) comm_ring_1
   365   by (intro_classes, auto simp add: diff_minus left_distrib)
   366 
   367 instance fps :: (ring_no_zero_divisors) ring_no_zero_divisors
   368 proof
   369   fix a b :: "'a fps"
   370   assume a0: "a \<noteq> 0" and b0: "b \<noteq> 0"
   371   then obtain i j where i: "a$i\<noteq>0" "\<forall>k<i. a$k=0"
   372     and j: "b$j \<noteq>0" "\<forall>k<j. b$k =0" unfolding fps_nonzero_nth_minimal
   373     by blast+
   374   have "(a * b) $ (i+j) = (\<Sum>k=0..i+j. a$k * b$(i+j-k))"
   375     by (rule fps_mult_nth)
   376   also have "\<dots> = (a$i * b$(i+j-i)) + (\<Sum>k\<in>{0..i+j}-{i}. a$k * b$(i+j-k))"
   377     by (rule setsum_diff1') simp_all
   378   also have "(\<Sum>k\<in>{0..i+j}-{i}. a$k * b$(i+j-k)) = 0"
   379     proof (rule setsum_0' [rule_format])
   380       fix k assume "k \<in> {0..i+j} - {i}"
   381       then have "k < i \<or> i+j-k < j" by auto
   382       then show "a$k * b$(i+j-k) = 0" using i j by auto
   383     qed
   384   also have "a$i * b$(i+j-i) + 0 = a$i * b$j" by simp
   385   also have "a$i * b$j \<noteq> 0" using i j by simp
   386   finally have "(a*b) $ (i+j) \<noteq> 0" .
   387   then show "a*b \<noteq> 0" unfolding fps_nonzero_nth by blast
   388 qed
   389 
   390 instance fps :: (idom) idom ..
   391 
   392 subsection{* Inverses of formal power series *}
   393 
   394 declare setsum_cong[fundef_cong]
   395 
   396 
   397 instantiation fps :: ("{comm_monoid_add,inverse, times, uminus}") inverse
   398 begin
   399 
   400 fun natfun_inverse:: "'a fps \<Rightarrow> nat \<Rightarrow> 'a" where 
   401   "natfun_inverse f 0 = inverse (f$0)"
   402 | "natfun_inverse f n = - inverse (f$0) * setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}" 
   403 
   404 definition fps_inverse_def: 
   405   "inverse f = (if f$0 = 0 then 0 else Abs_fps (natfun_inverse f))"
   406 definition fps_divide_def: "divide = (\<lambda>(f::'a fps) g. f * inverse g)"
   407 instance ..
   408 end
   409 
   410 lemma fps_inverse_zero[simp]: 
   411   "inverse (0 :: 'a::{comm_monoid_add,inverse, times, uminus} fps) = 0"
   412   by (simp add: fps_ext fps_inverse_def)
   413 
   414 lemma fps_inverse_one[simp]: "inverse (1 :: 'a::{division_ring,zero_neq_one} fps) = 1"
   415   apply (auto simp add: expand_fps_eq fps_inverse_def)
   416   by (case_tac n, auto)
   417 
   418 instance fps :: ("{comm_monoid_add,inverse, times, uminus}")  division_by_zero
   419   by default (rule fps_inverse_zero)
   420 
   421 lemma inverse_mult_eq_1[intro]: assumes f0: "f$0 \<noteq> (0::'a::field)"
   422   shows "inverse f * f = 1"
   423 proof-
   424   have c: "inverse f * f = f * inverse f" by (simp add: mult_commute)
   425   from f0 have ifn: "\<And>n. inverse f $ n = natfun_inverse f n" 
   426     by (simp add: fps_inverse_def)
   427   from f0 have th0: "(inverse f * f) $ 0 = 1"
   428     by (simp add: fps_mult_nth fps_inverse_def)
   429   {fix n::nat assume np: "n >0 "
   430     from np have eq: "{0..n} = {0} \<union> {1 .. n}" by auto
   431     have d: "{0} \<inter> {1 .. n} = {}" by auto
   432     have f: "finite {0::nat}" "finite {1..n}" by auto
   433     from f0 np have th0: "- (inverse f$n) = 
   434       (setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}) / (f$0)"
   435       by (cases n, simp, simp add: divide_inverse fps_inverse_def)
   436     from th0[symmetric, unfolded nonzero_divide_eq_eq[OF f0]]
   437     have th1: "setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n} = 
   438       - (f$0) * (inverse f)$n" 
   439       by (simp add: ring_simps)
   440     have "(f * inverse f) $ n = (\<Sum>i = 0..n. f $i * natfun_inverse f (n - i))" 
   441       unfolding fps_mult_nth ifn ..
   442     also have "\<dots> = f$0 * natfun_inverse f n 
   443       + (\<Sum>i = 1..n. f$i * natfun_inverse f (n-i))"
   444       unfolding setsum_Un_disjoint[OF f d, unfolded eq[symmetric]]
   445       by simp
   446     also have "\<dots> = 0" unfolding th1 ifn by simp
   447     finally have "(inverse f * f)$n = 0" unfolding c . }
   448   with th0 show ?thesis by (simp add: fps_eq_iff)
   449 qed
   450 
   451 lemma fps_inverse_0_iff[simp]: "(inverse f)$0 = (0::'a::division_ring) \<longleftrightarrow> f$0 = 0"
   452   by (simp add: fps_inverse_def nonzero_imp_inverse_nonzero)
   453 
   454 lemma fps_inverse_eq_0_iff[simp]: "inverse f = (0:: ('a::field) fps) \<longleftrightarrow> f $0 = 0"
   455 proof-
   456   {assume "f$0 = 0" hence "inverse f = 0" by (simp add: fps_inverse_def)}
   457   moreover
   458   {assume h: "inverse f = 0" and c: "f $0 \<noteq> 0"
   459     from inverse_mult_eq_1[OF c] h have False by simp}
   460   ultimately show ?thesis by blast
   461 qed
   462 
   463 lemma fps_inverse_idempotent[intro]: assumes f0: "f$0 \<noteq> (0::'a::field)"
   464   shows "inverse (inverse f) = f"
   465 proof-
   466   from f0 have if0: "inverse f $ 0 \<noteq> 0" by simp
   467   from inverse_mult_eq_1[OF f0] inverse_mult_eq_1[OF if0] 
   468   have th0: "inverse f * f = inverse f * inverse (inverse f)"   by (simp add: mult_ac)
   469   then show ?thesis using f0 unfolding mult_cancel_left by simp
   470 qed
   471 
   472 lemma fps_inverse_unique: assumes f0: "f$0 \<noteq> (0::'a::field)" and fg: "f*g = 1" 
   473   shows "inverse f = g"
   474 proof-
   475   from inverse_mult_eq_1[OF f0] fg
   476   have th0: "inverse f * f = g * f" by (simp add: mult_ac)
   477   then show ?thesis using f0  unfolding mult_cancel_right
   478     by (auto simp add: expand_fps_eq)
   479 qed
   480 
   481 lemma fps_inverse_gp: "inverse (Abs_fps(\<lambda>n. (1::'a::field))) 
   482   = Abs_fps (\<lambda>n. if n= 0 then 1 else if n=1 then - 1 else 0)"
   483   apply (rule fps_inverse_unique)
   484   apply simp
   485   apply (simp add: fps_eq_iff fps_mult_nth)
   486 proof(clarsimp)
   487   fix n::nat assume n: "n > 0"
   488   let ?f = "\<lambda>i. if n = i then (1\<Colon>'a) else if n - i = 1 then - 1 else 0"
   489   let ?g = "\<lambda>i. if i = n then 1 else if i=n - 1 then - 1 else 0"
   490   let ?h = "\<lambda>i. if i=n - 1 then - 1 else 0"
   491   have th1: "setsum ?f {0..n} = setsum ?g {0..n}"  
   492     by (rule setsum_cong2) auto
   493   have th2: "setsum ?g {0..n - 1} = setsum ?h {0..n - 1}"  
   494     using n apply - by (rule setsum_cong2) auto
   495   have eq: "{0 .. n} = {0.. n - 1} \<union> {n}" by auto
   496   from n have d: "{0.. n - 1} \<inter> {n} = {}" by auto 
   497   have f: "finite {0.. n - 1}" "finite {n}" by auto
   498   show "setsum ?f {0..n} = 0"
   499     unfolding th1 
   500     apply (simp add: setsum_Un_disjoint[OF f d, unfolded eq[symmetric]] del: One_nat_def)
   501     unfolding th2
   502     by(simp add: setsum_delta)
   503 qed
   504 
   505 subsection{* Formal Derivatives, and the MacLaurin theorem around 0*}
   506 
   507 definition "fps_deriv f = Abs_fps (\<lambda>n. of_nat (n + 1) * f $ (n + 1))"
   508 
   509 lemma fps_deriv_nth[simp]: "fps_deriv f $ n = of_nat (n +1) * f $ (n+1)" by (simp add: fps_deriv_def)
   510 
   511 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"
   512   unfolding fps_eq_iff fps_add_nth  fps_const_mult_left fps_deriv_nth by (simp add: ring_simps)
   513 
   514 lemma fps_deriv_mult[simp]: 
   515   fixes f :: "('a :: comm_ring_1) fps"
   516   shows "fps_deriv (f * g) = f * fps_deriv g + fps_deriv f * g"
   517 proof-
   518   let ?D = "fps_deriv"
   519   {fix n::nat
   520     let ?Zn = "{0 ..n}"
   521     let ?Zn1 = "{0 .. n + 1}"
   522     let ?f = "\<lambda>i. i + 1"
   523     have fi: "inj_on ?f {0..n}" by (simp add: inj_on_def)
   524     have eq: "{1.. n+1} = ?f ` {0..n}" by auto
   525     let ?g = "\<lambda>i. of_nat (i+1) * g $ (i+1) * f $ (n - i) +
   526         of_nat (i+1)* f $ (i+1) * g $ (n - i)"
   527     let ?h = "\<lambda>i. of_nat i * g $ i * f $ ((n+1) - i) +
   528         of_nat i* f $ i * g $ ((n + 1) - i)"
   529     {fix k assume k: "k \<in> {0..n}"
   530       have "?h (k + 1) = ?g k" using k by auto}
   531     note th0 = this
   532     have eq': "{0..n +1}- {1 .. n+1} = {0}" by auto
   533     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"
   534       apply (rule setsum_reindex_cong[where f="\<lambda>i. n + 1 - i"])
   535       apply (simp add: inj_on_def Ball_def)
   536       apply presburger
   537       apply (rule set_ext)
   538       apply (presburger add: image_iff)
   539       by simp
   540     have s1: "setsum (\<lambda>i. f $ i * g $ (n + 1 - i)) ?Zn1 = setsum (\<lambda>i. f $ (n + 1 - i) * g $ i) ?Zn1"
   541       apply (rule setsum_reindex_cong[where f="\<lambda>i. n + 1 - i"])
   542       apply (simp add: inj_on_def Ball_def)
   543       apply presburger
   544       apply (rule set_ext)
   545       apply (presburger add: image_iff)
   546       by simp
   547     have "(f * ?D g + ?D f * g)$n = (?D g * f + ?D f * g)$n" by (simp only: mult_commute)
   548     also have "\<dots> = (\<Sum>i = 0..n. ?g i)"
   549       by (simp add: fps_mult_nth setsum_addf[symmetric])
   550     also have "\<dots> = setsum ?h {1..n+1}"
   551       using th0 setsum_reindex_cong[OF fi eq, of "?g" "?h"] by auto
   552     also have "\<dots> = setsum ?h {0..n+1}"
   553       apply (rule setsum_mono_zero_left)
   554       apply simp
   555       apply (simp add: subset_eq)
   556       unfolding eq'
   557       by simp
   558     also have "\<dots> = (fps_deriv (f * g)) $ n"
   559       apply (simp only: fps_deriv_nth fps_mult_nth setsum_addf)
   560       unfolding s0 s1
   561       unfolding setsum_addf[symmetric] setsum_right_distrib
   562       apply (rule setsum_cong2)
   563       by (auto simp add: of_nat_diff ring_simps)
   564     finally have "(f * ?D g + ?D f * g) $ n = ?D (f*g) $ n" .}
   565   then show ?thesis unfolding fps_eq_iff by auto 
   566 qed
   567 
   568 lemma fps_deriv_neg[simp]: "fps_deriv (- (f:: ('a:: comm_ring_1) fps)) = - (fps_deriv f)"
   569   by (simp add: fps_eq_iff fps_deriv_def)
   570 lemma fps_deriv_add[simp]: "fps_deriv ((f:: ('a::comm_ring_1) fps) + g) = fps_deriv f + fps_deriv g"
   571   using fps_deriv_linear[of 1 f 1 g] by simp
   572 
   573 lemma fps_deriv_sub[simp]: "fps_deriv ((f:: ('a::comm_ring_1) fps) - g) = fps_deriv f - fps_deriv g"
   574   unfolding diff_minus by simp 
   575 
   576 lemma fps_deriv_const[simp]: "fps_deriv (fps_const c) = 0"
   577   by (simp add: fps_ext fps_deriv_def fps_const_def)
   578 
   579 lemma fps_deriv_mult_const_left[simp]: "fps_deriv (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_deriv f"
   580   by simp
   581 
   582 lemma fps_deriv_0[simp]: "fps_deriv 0 = 0"
   583   by (simp add: fps_deriv_def fps_eq_iff)
   584 
   585 lemma fps_deriv_1[simp]: "fps_deriv 1 = 0"
   586   by (simp add: fps_deriv_def fps_eq_iff )
   587 
   588 lemma fps_deriv_mult_const_right[simp]: "fps_deriv (f * fps_const (c::'a::comm_ring_1)) = fps_deriv f * fps_const c"
   589   by simp
   590 
   591 lemma fps_deriv_setsum: "fps_deriv (setsum f S) = setsum (\<lambda>i. fps_deriv (f i :: ('a::comm_ring_1) fps)) S"
   592 proof-
   593   {assume "\<not> finite S" hence ?thesis by simp}
   594   moreover
   595   {assume fS: "finite S"
   596     have ?thesis  by (induct rule: finite_induct[OF fS], simp_all)}
   597   ultimately show ?thesis by blast
   598 qed
   599 
   600 lemma fps_deriv_eq_0_iff[simp]: "fps_deriv f = 0 \<longleftrightarrow> (f = fps_const (f$0 :: 'a::{idom,semiring_char_0}))"
   601 proof-
   602   {assume "f= fps_const (f$0)" hence "fps_deriv f = fps_deriv (fps_const (f$0))" by simp
   603     hence "fps_deriv f = 0" by simp }
   604   moreover
   605   {assume z: "fps_deriv f = 0"
   606     hence "\<forall>n. (fps_deriv f)$n = 0" by simp
   607     hence "\<forall>n. f$(n+1) = 0" by (simp del: of_nat_Suc of_nat_add One_nat_def)
   608     hence "f = fps_const (f$0)"
   609       apply (clarsimp simp add: fps_eq_iff fps_const_def)
   610       apply (erule_tac x="n - 1" in allE)
   611       by simp}
   612   ultimately show ?thesis by blast
   613 qed
   614 
   615 lemma fps_deriv_eq_iff: 
   616   fixes f:: "('a::{idom,semiring_char_0}) fps"
   617   shows "fps_deriv f = fps_deriv g \<longleftrightarrow> (f = fps_const(f$0 - g$0) + g)"
   618 proof-
   619   have "fps_deriv f = fps_deriv g \<longleftrightarrow> fps_deriv (f - g) = 0" by simp
   620   also have "\<dots> \<longleftrightarrow> f - g = fps_const ((f-g)$0)" unfolding fps_deriv_eq_0_iff ..
   621   finally show ?thesis by (simp add: ring_simps)
   622 qed
   623 
   624 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)"
   625   apply auto unfolding fps_deriv_eq_iff by blast
   626   
   627 
   628 fun fps_nth_deriv :: "nat \<Rightarrow> ('a::semiring_1) fps \<Rightarrow> 'a fps" where
   629   "fps_nth_deriv 0 f = f"
   630 | "fps_nth_deriv (Suc n) f = fps_nth_deriv n (fps_deriv f)"
   631 
   632 lemma fps_nth_deriv_commute: "fps_nth_deriv (Suc n) f = fps_deriv (fps_nth_deriv n f)"
   633   by (induct n arbitrary: f, auto)
   634 
   635 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"
   636   by (induct n arbitrary: f g, auto simp add: fps_nth_deriv_commute)
   637 
   638 lemma fps_nth_deriv_neg[simp]: "fps_nth_deriv n (- (f:: ('a:: comm_ring_1) fps)) = - (fps_nth_deriv n f)"
   639   by (induct n arbitrary: f, simp_all)
   640 
   641 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"
   642   using fps_nth_deriv_linear[of n 1 f 1 g] by simp
   643 
   644 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"
   645   unfolding diff_minus fps_nth_deriv_add by simp 
   646 
   647 lemma fps_nth_deriv_0[simp]: "fps_nth_deriv n 0 = 0"
   648   by (induct n, simp_all )
   649 
   650 lemma fps_nth_deriv_1[simp]: "fps_nth_deriv n 1 = (if n = 0 then 1 else 0)"
   651   by (induct n, simp_all )
   652 
   653 lemma fps_nth_deriv_const[simp]: "fps_nth_deriv n (fps_const c) = (if n = 0 then fps_const c else 0)"
   654   by (cases n, simp_all)
   655 
   656 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"
   657   using fps_nth_deriv_linear[of n "c" f 0 0 ] by simp
   658 
   659 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"
   660   using fps_nth_deriv_linear[of n "c" f 0 0] by (simp add: mult_commute)
   661 
   662 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"
   663 proof-
   664   {assume "\<not> finite S" hence ?thesis by simp}
   665   moreover
   666   {assume fS: "finite S"
   667     have ?thesis  by (induct rule: finite_induct[OF fS], simp_all)}
   668   ultimately show ?thesis by blast
   669 qed
   670 
   671 lemma fps_deriv_maclauren_0: "(fps_nth_deriv k (f:: ('a::comm_semiring_1) fps)) $ 0 = of_nat (fact k) * f$(k)"
   672   by (induct k arbitrary: f) (auto simp add: ring_simps of_nat_mult)
   673 
   674 subsection {* Powers*}
   675 
   676 instantiation fps :: (semiring_1) power
   677 begin
   678 
   679 fun fps_pow :: "nat \<Rightarrow> 'a fps \<Rightarrow> 'a fps" where
   680   "fps_pow 0 f = 1"
   681 | "fps_pow (Suc n) f = f * fps_pow n f"
   682 
   683 definition fps_power_def: "power (f::'a fps) n = fps_pow n f"
   684 instance ..
   685 end
   686 
   687 instantiation fps :: (comm_ring_1) recpower
   688 begin
   689 instance
   690   apply (intro_classes)
   691   by (simp_all add: fps_power_def)
   692 end
   693 
   694 lemma eq_neg_iff_add_eq_0: "(a::'a::ring) = -b \<longleftrightarrow> a + b = 0"
   695 proof-
   696   {assume "a = -b" hence "b + a = b + -b" by simp
   697     hence "a + b = 0" by (simp add: ring_simps)}
   698   moreover
   699   {assume "a + b = 0" hence "a + b - b = -b" by simp
   700     hence "a = -b" by simp}
   701   ultimately show ?thesis by blast
   702 qed
   703 
   704 lemma fps_square_eq_iff: "(a:: 'a::idom fps)^ 2 = b^2  \<longleftrightarrow> (a = b \<or> a = -b)"
   705 proof-
   706   {assume "a = b \<or> a = -b" hence "a^2 = b^2" by auto}
   707   moreover
   708   {assume "a^2 = b^2 "
   709     hence "a^2 - b^2 = 0" by simp
   710     hence "(a-b) * (a+b) = 0" by (simp add: power2_eq_square ring_simps)
   711     hence "a = b \<or> a = -b" by (simp add: eq_neg_iff_add_eq_0)}
   712   ultimately show ?thesis by blast
   713 qed
   714 
   715 lemma fps_power_zeroth_eq_one: "a$0 =1 \<Longrightarrow> a^n $ 0 = (1::'a::semiring_1)"
   716   by (induct n, auto simp add: fps_power_def expand_fps_eq fps_mult_nth)
   717 
   718 lemma fps_power_first_eq: "(a:: 'a::comm_ring_1 fps)$0 =1 \<Longrightarrow> a^n $ 1 = of_nat n * a$1"
   719 proof(induct n)
   720   case 0 thus ?case by (simp add: fps_power_def)
   721 next
   722   case (Suc n)
   723   note h = Suc.hyps[OF `a$0 = 1`]
   724   show ?case unfolding power_Suc fps_mult_nth 
   725     using h `a$0 = 1`  fps_power_zeroth_eq_one[OF `a$0=1`] by (simp add: ring_simps)
   726 qed
   727 
   728 lemma startsby_one_power:"a $ 0 = (1::'a::comm_ring_1) \<Longrightarrow> a^n $ 0 = 1"
   729   by (induct n, auto simp add: fps_power_def fps_mult_nth)
   730 
   731 lemma startsby_zero_power:"a $0 = (0::'a::comm_ring_1) \<Longrightarrow> n > 0 \<Longrightarrow> a^n $0 = 0"
   732   by (induct n, auto simp add: fps_power_def fps_mult_nth)
   733 
   734 lemma startsby_power:"a $0 = (v::'a::{comm_ring_1, recpower}) \<Longrightarrow> a^n $0 = v^n"
   735   by (induct n, auto simp add: fps_power_def fps_mult_nth power_Suc)
   736 
   737 lemma startsby_zero_power_iff[simp]:
   738   "a^n $0 = (0::'a::{idom, recpower}) \<longleftrightarrow> (n \<noteq> 0 \<and> a$0 = 0)"
   739 apply (rule iffI)
   740 apply (induct n, auto simp add: power_Suc fps_mult_nth)
   741 by (rule startsby_zero_power, simp_all)
   742 
   743 lemma startsby_zero_power_prefix: 
   744   assumes a0: "a $0 = (0::'a::idom)"
   745   shows "\<forall>n < k. a ^ k $ n = 0"
   746   using a0 
   747 proof(induct k rule: nat_less_induct)
   748   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)"
   749   let ?ths = "\<forall>m<k. a ^ k $ m = 0"
   750   {assume "k = 0" then have ?ths by simp}
   751   moreover
   752   {fix l assume k: "k = Suc l"
   753     {fix m assume mk: "m < k"
   754       {assume "m=0" hence "a^k $ m = 0" using startsby_zero_power[of a k] k a0 
   755 	  by simp}
   756       moreover
   757       {assume m0: "m \<noteq> 0"
   758 	have "a ^k $ m = (a^l * a) $m" by (simp add: k power_Suc mult_commute)
   759 	also have "\<dots> = (\<Sum>i = 0..m. a ^ l $ i * a $ (m - i))" by (simp add: fps_mult_nth)
   760 	also have "\<dots> = 0" apply (rule setsum_0')
   761 	  apply auto
   762 	  apply (case_tac "aa = m")
   763 	  using a0
   764 	  apply simp
   765 	  apply (rule H[rule_format])
   766 	  using a0 k mk by auto 
   767 	finally have "a^k $ m = 0" .}
   768     ultimately have "a^k $ m = 0" by blast}
   769     hence ?ths by blast}
   770   ultimately show ?ths by (cases k, auto)
   771 qed
   772 
   773 lemma startsby_zero_setsum_depends: 
   774   assumes a0: "a $0 = (0::'a::idom)" and kn: "n \<ge> k"
   775   shows "setsum (\<lambda>i. (a ^ i)$k) {0 .. n} = setsum (\<lambda>i. (a ^ i)$k) {0 .. k}"
   776   apply (rule setsum_mono_zero_right)
   777   using kn apply auto
   778   apply (rule startsby_zero_power_prefix[rule_format, OF a0])
   779   by arith
   780 
   781 lemma startsby_zero_power_nth_same: assumes a0: "a$0 = (0::'a::{recpower, idom})"
   782   shows "a^n $ n = (a$1) ^ n"
   783 proof(induct n)
   784   case 0 thus ?case by (simp add: power_0)
   785 next
   786   case (Suc n)
   787   have "a ^ Suc n $ (Suc n) = (a^n * a)$(Suc n)" by (simp add: ring_simps power_Suc)
   788   also have "\<dots> = setsum (\<lambda>i. a^n$i * a $ (Suc n - i)) {0.. Suc n}" by (simp add: fps_mult_nth)
   789   also have "\<dots> = setsum (\<lambda>i. a^n$i * a $ (Suc n - i)) {n .. Suc n}"
   790     apply (rule setsum_mono_zero_right)
   791     apply simp
   792     apply clarsimp
   793     apply clarsimp
   794     apply (rule startsby_zero_power_prefix[rule_format, OF a0])
   795     apply arith
   796     done
   797   also have "\<dots> = a^n $ n * a$1" using a0 by simp
   798   finally show ?case using Suc.hyps by (simp add: power_Suc)
   799 qed
   800 
   801 lemma fps_inverse_power:
   802   fixes a :: "('a::{field, recpower}) fps"
   803   shows "inverse (a^n) = inverse a ^ n"
   804 proof-
   805   {assume a0: "a$0 = 0"
   806     hence eq: "inverse a = 0" by (simp add: fps_inverse_def)
   807     {assume "n = 0" hence ?thesis by simp}
   808     moreover
   809     {assume n: "n > 0"
   810       from startsby_zero_power[OF a0 n] eq a0 n have ?thesis 
   811 	by (simp add: fps_inverse_def)}
   812     ultimately have ?thesis by blast}
   813   moreover
   814   {assume a0: "a$0 \<noteq> 0"
   815     have ?thesis
   816       apply (rule fps_inverse_unique)
   817       apply (simp add: a0)
   818       unfolding power_mult_distrib[symmetric]
   819       apply (rule ssubst[where t = "a * inverse a" and s= 1])
   820       apply simp_all
   821       apply (subst mult_commute)
   822       by (rule inverse_mult_eq_1[OF a0])}
   823   ultimately show ?thesis by blast
   824 qed
   825 
   826 lemma fps_deriv_power: "fps_deriv (a ^ n) = fps_const (of_nat n :: 'a:: comm_ring_1) * fps_deriv a * a ^ (n - 1)"
   827   apply (induct n, auto simp add: power_Suc ring_simps fps_const_add[symmetric] simp del: fps_const_add)
   828   by (case_tac n, auto simp add: power_Suc ring_simps)
   829 
   830 lemma fps_inverse_deriv: 
   831   fixes a:: "('a :: field) fps"
   832   assumes a0: "a$0 \<noteq> 0"
   833   shows "fps_deriv (inverse a) = - fps_deriv a * inverse a ^ 2"
   834 proof-
   835   from inverse_mult_eq_1[OF a0]
   836   have "fps_deriv (inverse a * a) = 0" by simp
   837   hence "inverse a * fps_deriv a + fps_deriv (inverse a) * a = 0" by simp
   838   hence "inverse a * (inverse a * fps_deriv a + fps_deriv (inverse a) * a) = 0"  by simp
   839   with inverse_mult_eq_1[OF a0]
   840   have "inverse a ^ 2 * fps_deriv a + fps_deriv (inverse a) = 0"
   841     unfolding power2_eq_square
   842     apply (simp add: ring_simps)
   843     by (simp add: mult_assoc[symmetric])
   844   hence "inverse a ^ 2 * fps_deriv a + fps_deriv (inverse a) - fps_deriv a * inverse a ^ 2 = 0 - fps_deriv a * inverse a ^ 2"
   845     by simp
   846   then show "fps_deriv (inverse a) = - fps_deriv a * inverse a ^ 2" by (simp add: ring_simps)
   847 qed
   848 
   849 lemma fps_inverse_mult: 
   850   fixes a::"('a :: field) fps"
   851   shows "inverse (a * b) = inverse a * inverse b"
   852 proof-
   853   {assume a0: "a$0 = 0" hence ab0: "(a*b)$0 = 0" by (simp add: fps_mult_nth)
   854     from a0 ab0 have th: "inverse a = 0" "inverse (a*b) = 0" by simp_all
   855     have ?thesis unfolding th by simp}
   856   moreover
   857   {assume b0: "b$0 = 0" hence ab0: "(a*b)$0 = 0" by (simp add: fps_mult_nth)
   858     from b0 ab0 have th: "inverse b = 0" "inverse (a*b) = 0" by simp_all
   859     have ?thesis unfolding th by simp}
   860   moreover
   861   {assume a0: "a$0 \<noteq> 0" and b0: "b$0 \<noteq> 0"
   862     from a0 b0 have ab0:"(a*b) $ 0 \<noteq> 0" by (simp  add: fps_mult_nth)
   863     from inverse_mult_eq_1[OF ab0] 
   864     have "inverse (a*b) * (a*b) * inverse a * inverse b = 1 * inverse a * inverse b" by simp
   865     then have "inverse (a*b) * (inverse a * a) * (inverse b * b) = inverse a * inverse b"
   866       by (simp add: ring_simps)
   867     then have ?thesis using inverse_mult_eq_1[OF a0] inverse_mult_eq_1[OF b0] by simp}
   868 ultimately show ?thesis by blast
   869 qed
   870 
   871 lemma fps_inverse_deriv': 
   872   fixes a:: "('a :: field) fps"
   873   assumes a0: "a$0 \<noteq> 0"
   874   shows "fps_deriv (inverse a) = - fps_deriv a / a ^ 2"
   875   using fps_inverse_deriv[OF a0]
   876   unfolding power2_eq_square fps_divide_def
   877     fps_inverse_mult by simp
   878 
   879 lemma inverse_mult_eq_1': assumes f0: "f$0 \<noteq> (0::'a::field)"
   880   shows "f * inverse f= 1"
   881   by (metis mult_commute inverse_mult_eq_1 f0)
   882 
   883 lemma fps_divide_deriv:   fixes a:: "('a :: field) fps"
   884   assumes a0: "b$0 \<noteq> 0"
   885   shows "fps_deriv (a / b) = (fps_deriv a * b - a * fps_deriv b) / b ^ 2"
   886   using fps_inverse_deriv[OF a0]
   887   by (simp add: fps_divide_def ring_simps power2_eq_square fps_inverse_mult inverse_mult_eq_1'[OF a0])
   888   
   889 subsection{* The eXtractor series X*}
   890 
   891 lemma minus_one_power_iff: "(- (1::'a :: {recpower, comm_ring_1})) ^ n = (if even n then 1 else - 1)"
   892   by (induct n, auto)
   893 
   894 definition "X = Abs_fps (\<lambda>n. if n = 1 then 1 else 0)"
   895 
   896 lemma fps_inverse_gp': "inverse (Abs_fps(\<lambda>n. (1::'a::field))) 
   897   = 1 - X"
   898   by (simp add: fps_inverse_gp fps_eq_iff X_def)
   899 
   900 lemma X_mult_nth[simp]: "(X * (f :: ('a::semiring_1) fps)) $n = (if n = 0 then 0 else f $ (n - 1))"
   901 proof-
   902   {assume n: "n \<noteq> 0"
   903     have fN: "finite {0 .. n}" by simp
   904     have "(X * f) $n = (\<Sum>i = 0..n. X $ i * f $ (n - i))" by (simp add: fps_mult_nth)
   905     also have "\<dots> = f $ (n - 1)"
   906       using n by (simp add: X_def mult_delta_left setsum_delta [OF fN])
   907   finally have ?thesis using n by simp }
   908   moreover
   909   {assume n: "n=0" hence ?thesis by (simp add: fps_mult_nth X_def)}
   910   ultimately show ?thesis by blast
   911 qed
   912 
   913 lemma X_mult_right_nth[simp]: "((f :: ('a::comm_semiring_1) fps) * X) $n = (if n = 0 then 0 else f $ (n - 1))"
   914   by (metis X_mult_nth mult_commute)
   915 
   916 lemma X_power_iff: "X^k = Abs_fps (\<lambda>n. if n = k then (1::'a::comm_ring_1) else 0)"
   917 proof(induct k)
   918   case 0 thus ?case by (simp add: X_def fps_power_def fps_eq_iff)
   919 next
   920   case (Suc k)
   921   {fix m 
   922     have "(X^Suc k) $ m = (if m = 0 then (0::'a) else (X^k) $ (m - 1))"
   923       by (simp add: power_Suc del: One_nat_def)
   924     then     have "(X^Suc k) $ m = (if m = Suc k then (1::'a) else 0)"
   925       using Suc.hyps by (auto cong del: if_weak_cong)}
   926   then show ?case by (simp add: fps_eq_iff)
   927 qed
   928 
   929 lemma X_power_mult_nth: "(X^k * (f :: ('a::comm_ring_1) fps)) $n = (if n < k then 0 else f $ (n - k))"
   930   apply (induct k arbitrary: n)
   931   apply (simp)
   932   unfolding power_Suc mult_assoc 
   933   by (case_tac n, auto)
   934 
   935 lemma X_power_mult_right_nth: "((f :: ('a::comm_ring_1) fps) * X^k) $n = (if n < k then 0 else f $ (n - k))"
   936   by (metis X_power_mult_nth mult_commute)
   937 lemma fps_deriv_X[simp]: "fps_deriv X = 1"
   938   by (simp add: fps_deriv_def X_def fps_eq_iff)
   939 
   940 lemma fps_nth_deriv_X[simp]: "fps_nth_deriv n X = (if n = 0 then X else if n=1 then 1 else 0)"
   941   by (cases "n", simp_all)
   942 
   943 lemma X_nth[simp]: "X$n = (if n = 1 then 1 else 0)" by (simp add: X_def)
   944 lemma X_power_nth[simp]: "(X^k) $n = (if n = k then 1 else (0::'a::comm_ring_1))"
   945   by (simp add: X_power_iff)
   946 
   947 lemma fps_inverse_X_plus1:
   948   "inverse (1 + X) = Abs_fps (\<lambda>n. (- (1::'a::{recpower, field})) ^ n)" (is "_ = ?r")
   949 proof-
   950   have eq: "(1 + X) * ?r = 1"
   951     unfolding minus_one_power_iff
   952     apply (auto simp add: ring_simps fps_eq_iff)
   953     by presburger+
   954   show ?thesis by (auto simp add: eq intro: fps_inverse_unique)
   955 qed
   956 
   957   
   958 subsection{* Integration *}
   959 definition "fps_integral a a0 = Abs_fps (\<lambda>n. if n = 0 then a0 else (a$(n - 1) / of_nat n))"
   960 
   961 lemma fps_deriv_fps_integral: "fps_deriv (fps_integral a (a0 :: 'a :: {field, ring_char_0})) = a"
   962   by (simp add: fps_integral_def fps_deriv_def fps_eq_iff field_simps del: of_nat_Suc)
   963 
   964 lemma fps_integral_linear: "fps_integral (fps_const (a::'a::{field, ring_char_0}) * f + fps_const b * g) (a*a0 + b*b0) = fps_const a * fps_integral f a0 + fps_const b * fps_integral g b0" (is "?l = ?r")
   965 proof-
   966   have "fps_deriv ?l = fps_deriv ?r" by (simp add: fps_deriv_fps_integral)
   967   moreover have "?l$0 = ?r$0" by (simp add: fps_integral_def)
   968   ultimately show ?thesis
   969     unfolding fps_deriv_eq_iff by auto
   970 qed
   971   
   972 subsection {* Composition of FPSs *}
   973 definition fps_compose :: "('a::semiring_1) fps \<Rightarrow> 'a fps \<Rightarrow> 'a fps" (infixl "oo" 55) where
   974   fps_compose_def: "a oo b = Abs_fps (\<lambda>n. setsum (\<lambda>i. a$i * (b^i$n)) {0..n})"
   975 
   976 lemma fps_compose_nth: "(a oo b)$n = setsum (\<lambda>i. a$i * (b^i$n)) {0..n}" by (simp add: fps_compose_def)
   977 
   978 lemma fps_compose_X[simp]: "a oo X = (a :: ('a :: comm_ring_1) fps)"
   979   by (simp add: fps_ext fps_compose_def mult_delta_right setsum_delta')
   980  
   981 lemma fps_const_compose[simp]: 
   982   "fps_const (a::'a::{comm_ring_1}) oo b = fps_const (a)"
   983   by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta)
   984 
   985 lemma X_fps_compose_startby0[simp]: "a$0 = 0 \<Longrightarrow> X oo a = (a :: ('a :: comm_ring_1) fps)"
   986   by (simp add: fps_eq_iff fps_compose_def mult_delta_left setsum_delta
   987                 power_Suc not_le)
   988 
   989 
   990 subsection {* Rules from Herbert Wilf's Generatingfunctionology*}
   991 
   992 subsubsection {* Rule 1 *}
   993   (* {a_{n+k}}_0^infty Corresponds to (f - setsum (\<lambda>i. a_i * x^i))/x^h, for h>0*)
   994 
   995 lemma fps_power_mult_eq_shift: 
   996   "X^Suc k * Abs_fps (\<lambda>n. a (n + Suc k)) = Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a:: field) * X^i) {0 .. k}" (is "?lhs = ?rhs")
   997 proof-
   998   {fix n:: nat
   999     have "?lhs $ n = (if n < Suc k then 0 else a n)" 
  1000       unfolding X_power_mult_nth by auto
  1001     also have "\<dots> = ?rhs $ n"
  1002     proof(induct k)
  1003       case 0 thus ?case by (simp add: fps_setsum_nth power_Suc)
  1004     next
  1005       case (Suc k)
  1006       note th = Suc.hyps[symmetric]
  1007       have "(Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a:: field) * X^i) {0 .. Suc k})$n = (Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a:: field) * X^i) {0 .. k} - fps_const (a (Suc k)) * X^ Suc k) $ n" by (simp add: ring_simps)
  1008       also  have "\<dots> = (if n < Suc k then 0 else a n) - (fps_const (a (Suc k)) * X^ Suc k)$n"
  1009 	using th 
  1010 	unfolding fps_sub_nth by simp
  1011       also have "\<dots> = (if n < Suc (Suc k) then 0 else a n)"
  1012 	unfolding X_power_mult_right_nth
  1013 	apply (auto simp add: not_less fps_const_def)
  1014 	apply (rule cong[of a a, OF refl])
  1015 	by arith
  1016       finally show ?case by simp
  1017     qed
  1018     finally have "?lhs $ n = ?rhs $ n"  .}
  1019   then show ?thesis by (simp add: fps_eq_iff)
  1020 qed
  1021 
  1022 subsubsection{* Rule 2*}
  1023 
  1024   (* We can not reach the form of Wilf, but still near to it using rewrite rules*)
  1025   (* If f reprents {a_n} and P is a polynomial, then 
  1026         P(xD) f represents {P(n) a_n}*)
  1027 
  1028 definition "XD = op * X o fps_deriv"
  1029 
  1030 lemma XD_add[simp]:"XD (a + b) = XD a + XD (b :: ('a::comm_ring_1) fps)"
  1031   by (simp add: XD_def ring_simps)
  1032 
  1033 lemma XD_mult_const[simp]:"XD (fps_const (c::'a::comm_ring_1) * a) = fps_const c * XD a"
  1034   by (simp add: XD_def ring_simps)
  1035 
  1036 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)"
  1037   by simp
  1038 
  1039 lemma XDN_linear: "(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)"
  1040   by (induct n, simp_all)
  1041 
  1042 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)
  1043 
  1044 lemma fps_mult_XD_shift: "(XD ^k) (a:: ('a::{comm_ring_1, recpower, ring_char_0}) fps) = Abs_fps (\<lambda>n. (of_nat n ^ k) * a$n)"
  1045 by (induct k arbitrary: a) (simp_all add: power_Suc XD_def fps_eq_iff ring_simps del: One_nat_def)
  1046 
  1047 subsubsection{* Rule 3 is trivial and is given by @{text fps_times_def}*}
  1048 subsubsection{* Rule 5 --- summation and "division" by (1 - X)*}
  1049 
  1050 lemma fps_divide_X_minus1_setsum_lemma:
  1051   "a = ((1::('a::comm_ring_1) fps) - X) * Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
  1052 proof-
  1053   let ?X = "X::('a::comm_ring_1) fps"
  1054   let ?sa = "Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
  1055   have th0: "\<And>i. (1 - (X::'a fps)) $ i = (if i = 0 then 1 else if i = 1 then - 1 else 0)" by simp
  1056   {fix n:: nat
  1057     {assume "n=0" hence "a$n = ((1 - ?X) * ?sa) $ n" 
  1058 	by (simp add: fps_mult_nth)}
  1059     moreover
  1060     {assume n0: "n \<noteq> 0"
  1061       then have u: "{0} \<union> ({1} \<union> {2..n}) = {0..n}" "{1}\<union>{2..n} = {1..n}"
  1062 	"{0..n - 1}\<union>{n} = {0..n}"
  1063 	apply (simp_all add: expand_set_eq) by presburger+
  1064       have d: "{0} \<inter> ({1} \<union> {2..n}) = {}" "{1} \<inter> {2..n} = {}" 
  1065 	"{0..n - 1}\<inter>{n} ={}" using n0
  1066 	by (simp_all add: expand_set_eq, presburger+)
  1067       have f: "finite {0}" "finite {1}" "finite {2 .. n}" 
  1068 	"finite {0 .. n - 1}" "finite {n}" by simp_all 
  1069     have "((1 - ?X) * ?sa) $ n = setsum (\<lambda>i. (1 - ?X)$ i * ?sa $ (n - i)) {0 .. n}"
  1070       by (simp add: fps_mult_nth)
  1071     also have "\<dots> = a$n" unfolding th0
  1072       unfolding setsum_Un_disjoint[OF f(1) finite_UnI[OF f(2,3)] d(1), unfolded u(1)]
  1073       unfolding setsum_Un_disjoint[OF f(2) f(3) d(2)]
  1074       apply (simp)
  1075       unfolding setsum_Un_disjoint[OF f(4,5) d(3), unfolded u(3)]
  1076       by simp
  1077     finally have "a$n = ((1 - ?X) * ?sa) $ n" by simp}
  1078   ultimately have "a$n = ((1 - ?X) * ?sa) $ n" by blast}
  1079 then show ?thesis 
  1080   unfolding fps_eq_iff by blast
  1081 qed
  1082 
  1083 lemma fps_divide_X_minus1_setsum:
  1084   "a /((1::('a::field) fps) - X)  = Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
  1085 proof-
  1086   let ?X = "1 - (X::('a::field) fps)"
  1087   have th0: "?X $ 0 \<noteq> 0" by simp
  1088   have "a /?X = ?X *  Abs_fps (\<lambda>n\<Colon>nat. setsum (op $ a) {0..n}) * inverse ?X"
  1089     using fps_divide_X_minus1_setsum_lemma[of a, symmetric] th0
  1090     by (simp add: fps_divide_def mult_assoc)
  1091   also have "\<dots> = (inverse ?X * ?X) * Abs_fps (\<lambda>n\<Colon>nat. setsum (op $ a) {0..n}) "
  1092     by (simp add: mult_ac)
  1093   finally show ?thesis by (simp add: inverse_mult_eq_1[OF th0])
  1094 qed
  1095 
  1096 subsubsection{* Rule 4 in its more general form: generalizes Rule 3 for an arbitrary 
  1097   finite product of FPS, also the relvant instance of powers of a FPS*}
  1098 
  1099 definition "natpermute n k = {l:: nat list. length l = k \<and> foldl op + 0 l = n}"
  1100 
  1101 lemma natlist_trivial_1: "natpermute n 1 = {[n]}"
  1102   apply (auto simp add: natpermute_def)
  1103   apply (case_tac x, auto)
  1104   done
  1105 
  1106 lemma foldl_add_start0: 
  1107   "foldl op + x xs = x + foldl op + (0::nat) xs"
  1108   apply (induct xs arbitrary: x)
  1109   apply simp
  1110   unfolding foldl.simps
  1111   apply atomize
  1112   apply (subgoal_tac "\<forall>x\<Colon>nat. foldl op + x xs = x + foldl op + (0\<Colon>nat) xs")
  1113   apply (erule_tac x="x + a" in allE)
  1114   apply (erule_tac x="a" in allE)
  1115   apply simp
  1116   apply assumption
  1117   done
  1118 
  1119 lemma foldl_add_append: "foldl op + (x::nat) (xs@ys) = foldl op + x xs + foldl op + 0 ys"
  1120   apply (induct ys arbitrary: x xs)
  1121   apply auto
  1122   apply (subst (2) foldl_add_start0)
  1123   apply simp
  1124   apply (subst (2) foldl_add_start0)
  1125   by simp
  1126 
  1127 lemma foldl_add_setsum: "foldl op + (x::nat) xs = x + setsum (nth xs) {0..<length xs}"
  1128 proof(induct xs arbitrary: x)
  1129   case Nil thus ?case by simp
  1130 next
  1131   case (Cons a as x)
  1132   have eq: "setsum (op ! (a#as)) {1..<length (a#as)} = setsum (op ! as) {0..<length as}"
  1133     apply (rule setsum_reindex_cong [where f=Suc])
  1134     by (simp_all add: inj_on_def)
  1135   have f: "finite {0}" "finite {1 ..< length (a#as)}" by simp_all
  1136   have d: "{0} \<inter> {1..<length (a#as)} = {}" by simp
  1137   have seq: "{0} \<union> {1..<length(a#as)} = {0 ..<length (a#as)}" by auto
  1138   have "foldl op + x (a#as) = x + foldl op + a as "
  1139     apply (subst foldl_add_start0)    by simp
  1140   also have "\<dots> = x + a + setsum (op ! as) {0..<length as}" unfolding Cons.hyps by simp
  1141   also have "\<dots> = x + setsum (op ! (a#as)) {0..<length (a#as)}"
  1142     unfolding eq[symmetric] 
  1143     unfolding setsum_Un_disjoint[OF f d, unfolded seq]
  1144     by simp
  1145   finally show ?case  .
  1146 qed
  1147 
  1148 
  1149 lemma append_natpermute_less_eq:
  1150   assumes h: "xs@ys \<in> natpermute n k" shows "foldl op + 0 xs \<le> n" and "foldl op + 0 ys \<le> n"
  1151 proof-
  1152   {from h have "foldl op + 0 (xs@ ys) = n" by (simp add: natpermute_def)
  1153     hence "foldl op + 0 xs + foldl op + 0 ys = n" unfolding foldl_add_append .}
  1154   note th = this
  1155   {from th show "foldl op + 0 xs \<le> n" by simp}
  1156   {from th show "foldl op + 0 ys \<le> n" by simp}
  1157 qed
  1158 
  1159 lemma natpermute_split:
  1160   assumes mn: "h \<le> k"
  1161   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)")
  1162 proof-
  1163   {fix l assume l: "l \<in> ?R" 
  1164     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
  1165     from xs have xs': "foldl op + 0 xs = m" by (simp add: natpermute_def)
  1166     from ys have ys': "foldl op + 0 ys = n - m" by (simp add: natpermute_def)
  1167     have "l \<in> ?L" using leq xs ys h 
  1168       apply simp
  1169       apply (clarsimp simp add: natpermute_def simp del: foldl_append)
  1170       apply (simp add: foldl_add_append[unfolded foldl_append])
  1171       unfolding xs' ys'
  1172       using mn xs ys 
  1173       unfolding natpermute_def by simp}
  1174   moreover
  1175   {fix l assume l: "l \<in> natpermute n k"
  1176     let ?xs = "take h l"
  1177     let ?ys = "drop h l"
  1178     let ?m = "foldl op + 0 ?xs"
  1179     from l have ls: "foldl op + 0 (?xs @ ?ys) = n" by (simp add: natpermute_def)
  1180     have xs: "?xs \<in> natpermute ?m h" using l mn by (simp add: natpermute_def)     
  1181     have ys: "?ys \<in> natpermute (n - ?m) (k - h)" using l mn ls[unfolded foldl_add_append]
  1182       by (simp add: natpermute_def)
  1183     from ls have m: "?m \<in> {0..n}"  unfolding foldl_add_append by simp
  1184     from xs ys ls have "l \<in> ?R" 
  1185       apply auto
  1186       apply (rule bexI[where x = "?m"])
  1187       apply (rule exI[where x = "?xs"])
  1188       apply (rule exI[where x = "?ys"])
  1189       using ls l unfolding foldl_add_append 
  1190       by (auto simp add: natpermute_def)}
  1191   ultimately show ?thesis by blast
  1192 qed
  1193 
  1194 lemma natpermute_0: "natpermute n 0 = (if n = 0 then {[]} else {})"
  1195   by (auto simp add: natpermute_def)
  1196 lemma natpermute_0'[simp]: "natpermute 0 k = (if k = 0 then {[]} else {replicate k 0})"
  1197   apply (auto simp add: set_replicate_conv_if natpermute_def)
  1198   apply (rule nth_equalityI)
  1199   by simp_all
  1200 
  1201 lemma natpermute_finite: "finite (natpermute n k)"
  1202 proof(induct k arbitrary: n)
  1203   case 0 thus ?case 
  1204     apply (subst natpermute_split[of 0 0, simplified])
  1205     by (simp add: natpermute_0)
  1206 next
  1207   case (Suc k)
  1208   then show ?case unfolding natpermute_split[of k "Suc k", simplified]
  1209     apply -
  1210     apply (rule finite_UN_I)
  1211     apply simp
  1212     unfolding One_nat_def[symmetric] natlist_trivial_1
  1213     apply simp
  1214     unfolding image_Collect[symmetric]
  1215     unfolding Collect_def mem_def
  1216     apply (rule finite_imageI)
  1217     apply blast
  1218     done
  1219 qed
  1220 
  1221 lemma natpermute_contain_maximal:
  1222   "{xs \<in> natpermute n (k+1). n \<in> set xs} = UNION {0 .. k} (\<lambda>i. {(replicate (k+1) 0) [i:=n]})"
  1223   (is "?A = ?B")
  1224 proof-
  1225   {fix xs assume H: "xs \<in> natpermute n (k+1)" and n: "n \<in> set xs"
  1226     from n obtain i where i: "i \<in> {0.. k}" "xs!i = n" using H
  1227       unfolding in_set_conv_nth by (auto simp add: less_Suc_eq_le natpermute_def) 
  1228     have eqs: "({0..k} - {i}) \<union> {i} = {0..k}" using i by auto
  1229     have f: "finite({0..k} - {i})" "finite {i}" by auto
  1230     have d: "({0..k} - {i}) \<inter> {i} = {}" using i by auto
  1231     from H have "n = setsum (nth xs) {0..k}" apply (simp add: natpermute_def)
  1232       unfolding foldl_add_setsum by (auto simp add: atLeastLessThanSuc_atLeastAtMost)
  1233     also have "\<dots> = n + setsum (nth xs) ({0..k} - {i})"
  1234       unfolding setsum_Un_disjoint[OF f d, unfolded eqs] using i by simp
  1235     finally have zxs: "\<forall> j\<in> {0..k} - {i}. xs!j = 0" by auto
  1236     from H have xsl: "length xs = k+1" by (simp add: natpermute_def)
  1237     from i have i': "i < length (replicate (k+1) 0)"   "i < k+1"
  1238       unfolding length_replicate  by arith+
  1239     have "xs = replicate (k+1) 0 [i := n]"
  1240       apply (rule nth_equalityI)
  1241       unfolding xsl length_list_update length_replicate
  1242       apply simp
  1243       apply clarify
  1244       unfolding nth_list_update[OF i'(1)]
  1245       using i zxs
  1246       by (case_tac "ia=i", auto simp del: replicate.simps)
  1247     then have "xs \<in> ?B" using i by blast}
  1248   moreover
  1249   {fix i assume i: "i \<in> {0..k}"
  1250     let ?xs = "replicate (k+1) 0 [i:=n]"
  1251     have nxs: "n \<in> set ?xs"
  1252       apply (rule set_update_memI) using i by simp
  1253     have xsl: "length ?xs = k+1" by (simp only: length_replicate length_list_update)
  1254     have "foldl op + 0 ?xs = setsum (nth ?xs) {0..<k+1}"
  1255       unfolding foldl_add_setsum add_0 length_replicate length_list_update ..
  1256     also have "\<dots> = setsum (\<lambda>j. if j = i then n else 0) {0..< k+1}"
  1257       apply (rule setsum_cong2) by (simp del: replicate.simps)
  1258     also have "\<dots> = n" using i by (simp add: setsum_delta)
  1259     finally 
  1260     have "?xs \<in> natpermute n (k+1)" using xsl unfolding natpermute_def Collect_def mem_def
  1261       by blast
  1262     then have "?xs \<in> ?A"  using nxs  by blast}
  1263   ultimately show ?thesis by auto
  1264 qed
  1265 
  1266     (* The general form *)	
  1267 lemma fps_setprod_nth:
  1268   fixes m :: nat and a :: "nat \<Rightarrow> ('a::comm_ring_1) fps"
  1269   shows "(setprod a {0 .. m})$n = setsum (\<lambda>v. setprod (\<lambda>j. (a j) $ (v!j)) {0..m}) (natpermute n (m+1))"
  1270   (is "?P m n")
  1271 proof(induct m arbitrary: n rule: nat_less_induct)
  1272   fix m n assume H: "\<forall>m' < m. \<forall>n. ?P m' n"
  1273   {assume m0: "m = 0"
  1274     hence "?P m n" apply simp
  1275       unfolding natlist_trivial_1[where n = n, unfolded One_nat_def] by simp}
  1276   moreover
  1277   {fix k assume k: "m = Suc k"
  1278     have km: "k < m" using k by arith
  1279     have u0: "{0 .. k} \<union> {m} = {0..m}" using k apply (simp add: expand_set_eq) by presburger
  1280     have f0: "finite {0 .. k}" "finite {m}" by auto
  1281     have d0: "{0 .. k} \<inter> {m} = {}" using k by auto
  1282     have "(setprod a {0 .. m}) $ n = (setprod a {0 .. k} * a m) $ n"
  1283       unfolding setprod_Un_disjoint[OF f0 d0, unfolded u0] by simp
  1284     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))"
  1285       unfolding fps_mult_nth H[rule_format, OF km] ..
  1286     also have "\<dots> = (\<Sum>v\<in>natpermute n (m + 1). \<Prod>j\<in>{0..m}. a j $ v ! j)"
  1287       apply (simp add: k)
  1288       unfolding natpermute_split[of m "m + 1", simplified, of n, unfolded natlist_trivial_1[unfolded One_nat_def] k]
  1289       apply (subst setsum_UN_disjoint)
  1290       apply simp 
  1291       apply simp
  1292       unfolding image_Collect[symmetric]
  1293       apply clarsimp
  1294       apply (rule finite_imageI)
  1295       apply (rule natpermute_finite)
  1296       apply (clarsimp simp add: expand_set_eq)
  1297       apply auto
  1298       apply (rule setsum_cong2)
  1299       unfolding setsum_left_distrib
  1300       apply (rule sym)
  1301       apply (rule_tac f="\<lambda>xs. xs @[n - x]" in  setsum_reindex_cong)
  1302       apply (simp add: inj_on_def)
  1303       apply auto
  1304       unfolding setprod_Un_disjoint[OF f0 d0, unfolded u0, unfolded k]
  1305       apply (clarsimp simp add: natpermute_def nth_append)
  1306       apply (rule_tac f="\<lambda>x. x * a (Suc k) $ (n - foldl op + 0 aa)" in cong[OF refl])
  1307       apply (rule setprod_cong)
  1308       apply simp
  1309       apply simp
  1310       done
  1311     finally have "?P m n" .}
  1312   ultimately show "?P m n " by (cases m, auto)
  1313 qed
  1314 
  1315 text{* The special form for powers *}
  1316 lemma fps_power_nth_Suc:
  1317   fixes m :: nat and a :: "('a::comm_ring_1) fps"
  1318   shows "(a ^ Suc m)$n = setsum (\<lambda>v. setprod (\<lambda>j. a $ (v!j)) {0..m}) (natpermute n (m+1))"
  1319 proof-
  1320   have f: "finite {0 ..m}" by simp
  1321   have th0: "a^Suc m = setprod (\<lambda>i. a) {0..m}" unfolding setprod_constant[OF f, of a] by simp
  1322   show ?thesis unfolding th0 fps_setprod_nth ..
  1323 qed
  1324 lemma fps_power_nth:
  1325   fixes m :: nat and a :: "('a::comm_ring_1) fps"
  1326   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))"
  1327   by (cases m, simp_all add: fps_power_nth_Suc)
  1328 
  1329 lemma fps_nth_power_0: 
  1330   fixes m :: nat and a :: "('a::{comm_ring_1, recpower}) fps"
  1331   shows "(a ^m)$0 = (a$0) ^ m"
  1332 proof-
  1333   {assume "m=0" hence ?thesis by simp}
  1334   moreover
  1335   {fix n assume m: "m = Suc n"
  1336     have c: "m = card {0..n}" using m by simp
  1337    have "(a ^m)$0 = setprod (\<lambda>i. a$0) {0..n}"
  1338      apply (simp add: m fps_power_nth del: replicate.simps)
  1339      apply (rule setprod_cong)
  1340      by (simp_all del: replicate.simps)
  1341    also have "\<dots> = (a$0) ^ m"
  1342      unfolding c by (rule setprod_constant, simp)
  1343    finally have ?thesis .}
  1344  ultimately show ?thesis by (cases m, auto)
  1345 qed
  1346 
  1347 lemma fps_compose_inj_right: 
  1348   assumes a0: "a$0 = (0::'a::{recpower,idom})"
  1349   and a1: "a$1 \<noteq> 0"
  1350   shows "(b oo a = c oo a) \<longleftrightarrow> b = c" (is "?lhs \<longleftrightarrow>?rhs")
  1351 proof-
  1352   {assume ?rhs then have "?lhs" by simp}
  1353   moreover
  1354   {assume h: ?lhs
  1355     {fix n have "b$n = c$n" 
  1356       proof(induct n rule: nat_less_induct)
  1357 	fix n assume H: "\<forall>m<n. b$m = c$m"
  1358 	{assume n0: "n=0"
  1359 	  from h have "(b oo a)$n = (c oo a)$n" by simp
  1360 	  hence "b$n = c$n" using n0 by (simp add: fps_compose_nth)}
  1361 	moreover
  1362 	{fix n1 assume n1: "n = Suc n1"
  1363 	  have f: "finite {0 .. n1}" "finite {n}" by simp_all
  1364 	  have eq: "{0 .. n1} \<union> {n} = {0 .. n}" using n1 by auto
  1365 	  have d: "{0 .. n1} \<inter> {n} = {}" using n1 by auto
  1366 	  have seq: "(\<Sum>i = 0..n1. b $ i * a ^ i $ n) = (\<Sum>i = 0..n1. c $ i * a ^ i $ n)"
  1367 	    apply (rule setsum_cong2)
  1368 	    using H n1 by auto
  1369 	  have th0: "(b oo a) $n = (\<Sum>i = 0..n1. c $ i * a ^ i $ n) + b$n * (a$1)^n"
  1370 	    unfolding fps_compose_nth setsum_Un_disjoint[OF f d, unfolded eq] seq
  1371 	    using startsby_zero_power_nth_same[OF a0]
  1372 	    by simp
  1373 	  have th1: "(c oo a) $n = (\<Sum>i = 0..n1. c $ i * a ^ i $ n) + c$n * (a$1)^n"
  1374 	    unfolding fps_compose_nth setsum_Un_disjoint[OF f d, unfolded eq]
  1375 	    using startsby_zero_power_nth_same[OF a0]
  1376 	    by simp
  1377 	  from h[unfolded fps_eq_iff, rule_format, of n] th0 th1 a1
  1378 	  have "b$n = c$n" by auto}
  1379 	ultimately show "b$n = c$n" by (cases n, auto)
  1380       qed}
  1381     then have ?rhs by (simp add: fps_eq_iff)}
  1382   ultimately show ?thesis by blast
  1383 qed
  1384 
  1385 
  1386 subsection {* Radicals *}
  1387 
  1388 declare setprod_cong[fundef_cong]
  1389 function radical :: "(nat \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> ('a::{field, recpower}) fps \<Rightarrow> nat \<Rightarrow> 'a" where
  1390   "radical r 0 a 0 = 1"
  1391 | "radical r 0 a (Suc n) = 0"
  1392 | "radical r (Suc k) a 0 = r (Suc k) (a$0)"
  1393 | "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)"
  1394 by pat_completeness auto
  1395 
  1396 termination radical
  1397 proof
  1398   let ?R = "measure (\<lambda>(r, k, a, n). n)"
  1399   {
  1400     show "wf ?R" by auto}
  1401   {fix r k a n xs i
  1402     assume xs: "xs \<in> {xs \<in> natpermute (Suc n) (Suc k). Suc n \<notin> set xs}" and i: "i \<in> {0..k}"
  1403     {assume c: "Suc n \<le> xs ! i"
  1404       from xs i have "xs !i \<noteq> Suc n" by (auto simp add: in_set_conv_nth natpermute_def)
  1405       with c have c': "Suc n < xs!i" by arith
  1406       have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}" by simp_all
  1407       have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}" by auto
  1408       have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})" using i by auto
  1409       from xs have "Suc n = foldl op + 0 xs" by (simp add: natpermute_def)
  1410       also have "\<dots> = setsum (nth xs) {0..<Suc k}" unfolding foldl_add_setsum using xs
  1411 	by (simp add: natpermute_def)
  1412       also have "\<dots> = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
  1413 	unfolding eqs  setsum_Un_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
  1414 	unfolding setsum_Un_disjoint[OF fths(2) fths(3) d(2)]
  1415 	by simp
  1416       finally have False using c' by simp}
  1417     then show "((r,Suc k,a,xs!i), r, Suc k, a, Suc n) \<in> ?R" 
  1418       apply auto by (metis not_less)}
  1419   {fix r k a n 
  1420     show "((r,Suc k, a, 0),r, Suc k, a, Suc n) \<in> ?R" by simp}
  1421 qed
  1422 
  1423 definition "fps_radical r n a = Abs_fps (radical r n a)"
  1424 
  1425 lemma fps_radical0[simp]: "fps_radical r 0 a = 1"
  1426   apply (auto simp add: fps_eq_iff fps_radical_def)  by (case_tac n, auto)
  1427 
  1428 lemma fps_radical_nth_0[simp]: "fps_radical r n a $ 0 = (if n=0 then 1 else r n (a$0))"
  1429   by (cases n, simp_all add: fps_radical_def)
  1430 
  1431 lemma fps_radical_power_nth[simp]: 
  1432   assumes r: "(r k (a$0)) ^ k = a$0"
  1433   shows "fps_radical r k a ^ k $ 0 = (if k = 0 then 1 else a$0)"
  1434 proof-
  1435   {assume "k=0" hence ?thesis by simp }
  1436   moreover
  1437   {fix h assume h: "k = Suc h" 
  1438     have fh: "finite {0..h}" by simp
  1439     have eq1: "fps_radical r k a ^ k $ 0 = (\<Prod>j\<in>{0..h}. fps_radical r k a $ (replicate k 0) ! j)"
  1440       unfolding fps_power_nth h by simp
  1441     also have "\<dots> = (\<Prod>j\<in>{0..h}. r k (a$0))"
  1442       apply (rule setprod_cong)
  1443       apply simp
  1444       using h
  1445       apply (subgoal_tac "replicate k (0::nat) ! x = 0")
  1446       by (auto intro: nth_replicate simp del: replicate.simps)
  1447     also have "\<dots> = a$0"
  1448       unfolding setprod_constant[OF fh] using r by (simp add: h)
  1449     finally have ?thesis using h by simp}
  1450   ultimately show ?thesis by (cases k, auto)
  1451 qed 
  1452 
  1453 lemma natpermute_max_card: assumes n0: "n\<noteq>0" 
  1454   shows "card {xs \<in> natpermute n (k+1). n \<in> set xs} = k+1"
  1455   unfolding natpermute_contain_maximal
  1456 proof-
  1457   let ?A= "\<lambda>i. {replicate (k + 1) 0[i := n]}"
  1458   let ?K = "{0 ..k}"
  1459   have fK: "finite ?K" by simp
  1460   have fAK: "\<forall>i\<in>?K. finite (?A i)" by auto
  1461   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]} = {}"
  1462   proof(clarify)
  1463     fix i j assume i: "i \<in> ?K" and j: "j\<in> ?K" and ij: "i\<noteq>j"
  1464     {assume eq: "replicate (k+1) 0 [i:=n] = replicate (k+1) 0 [j:= n]"
  1465       have "(replicate (k+1) 0 [i:=n] ! i) = n" using i by (simp del: replicate.simps)
  1466       moreover
  1467       have "(replicate (k+1) 0 [j:=n] ! i) = 0" using i ij by (simp del: replicate.simps)
  1468       ultimately have False using eq n0 by (simp del: replicate.simps)}
  1469     then show "{replicate (k + 1) 0[i := n]} \<inter> {replicate (k + 1) 0[j := n]} = {}"
  1470       by auto
  1471   qed
  1472   from card_UN_disjoint[OF fK fAK d] 
  1473   show "card (\<Union>i\<in>{0..k}. {replicate (k + 1) 0[i := n]}) = k+1" by simp
  1474 qed
  1475   
  1476 lemma power_radical: 
  1477   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
  1478   assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 \<noteq> 0"
  1479   shows "(fps_radical r (Suc k) a) ^ (Suc k) = a" 
  1480 proof-
  1481   let ?r = "fps_radical r (Suc k) a"
  1482   from a0 r0 have r00: "r (Suc k) (a$0) \<noteq> 0" by auto
  1483   {fix z have "?r ^ Suc k $ z = a$z"
  1484     proof(induct z rule: nat_less_induct)
  1485       fix n assume H: "\<forall>m<n. ?r ^ Suc k $ m = a$m"
  1486       {assume "n = 0" hence "?r ^ Suc k $ n = a $n"
  1487 	  using fps_radical_power_nth[of r "Suc k" a, OF r0] by simp}
  1488       moreover
  1489       {fix n1 assume n1: "n = Suc n1"
  1490 	have fK: "finite {0..k}" by simp
  1491 	have nz: "n \<noteq> 0" using n1 by arith
  1492 	let ?Pnk = "natpermute n (k + 1)"
  1493 	let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
  1494 	let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
  1495 	have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
  1496 	have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
  1497 	have f: "finite ?Pnkn" "finite ?Pnknn" 
  1498 	  using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
  1499 	  by (metis natpermute_finite)+
  1500 	let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
  1501 	have "setsum ?f ?Pnkn = setsum (\<lambda>v. ?r $ n * r (Suc k) (a $ 0) ^ k) ?Pnkn" 
  1502 	proof(rule setsum_cong2)
  1503 	  fix v assume v: "v \<in> {xs \<in> natpermute n (k + 1). n \<in> set xs}"
  1504 	  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"
  1505 	  from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
  1506 	    unfolding natpermute_contain_maximal by auto
  1507 	  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))"
  1508 	    apply (rule setprod_cong, simp)
  1509 	    using i r0 by (simp del: replicate.simps)
  1510 	  also have "\<dots> = (fps_radical r (Suc k) a $ n) * r (Suc k) (a$0) ^ k"
  1511 	    unfolding setprod_gen_delta[OF fK] using i r0 by simp
  1512 	  finally show ?ths .
  1513 	qed
  1514 	then have "setsum ?f ?Pnkn = of_nat (k+1) * ?r $ n * r (Suc k) (a $ 0) ^ k"  
  1515 	  by (simp add: natpermute_max_card[OF nz, simplified]) 
  1516 	also have "\<dots> = a$n - setsum ?f ?Pnknn"
  1517 	  unfolding n1 using r00 a0 by (simp add: field_simps fps_radical_def del: of_nat_Suc )
  1518 	finally have fn: "setsum ?f ?Pnkn = a$n - setsum ?f ?Pnknn" .
  1519 	have "(?r ^ Suc k)$n = setsum ?f ?Pnkn + setsum ?f ?Pnknn" 
  1520 	  unfolding fps_power_nth_Suc setsum_Un_disjoint[OF f d, unfolded eq] ..
  1521 	also have "\<dots> = a$n" unfolding fn by simp
  1522 	finally have "?r ^ Suc k $ n = a $n" .}
  1523       ultimately  show "?r ^ Suc k $ n = a $n" by (cases n, auto)
  1524   qed }
  1525   then show ?thesis by (simp add: fps_eq_iff)
  1526 qed
  1527 
  1528 lemma eq_divide_imp': assumes c0: "(c::'a::field) ~= 0" and eq: "a * c = b"
  1529   shows "a = b / c" 
  1530 proof-
  1531   from eq have "a * c * inverse c = b * inverse c" by simp
  1532   hence "a * (inverse c * c) = b/c" by (simp only: field_simps divide_inverse)
  1533   then show "a = b/c" unfolding  field_inverse[OF c0] by simp
  1534 qed
  1535 
  1536 lemma radical_unique:  
  1537   assumes r0: "(r (Suc k) (b$0)) ^ Suc k = b$0" 
  1538   and a0: "r (Suc k) (b$0 ::'a::{field, ring_char_0, recpower}) = a$0" and b0: "b$0 \<noteq> 0"
  1539   shows "a^(Suc k) = b \<longleftrightarrow> a = fps_radical r (Suc k) b"
  1540 proof-
  1541   let ?r = "fps_radical r (Suc k) b"
  1542   have r00: "r (Suc k) (b$0) \<noteq> 0" using b0 r0 by auto
  1543   {assume H: "a = ?r"
  1544     from H have "a^Suc k = b" using power_radical[of r k, OF r0 b0] by simp}
  1545   moreover
  1546   {assume H: "a^Suc k = b"
  1547     (* Generally a$0 would need to be the k+1 st root of b$0 *)
  1548     have ceq: "card {0..k} = Suc k" by simp
  1549     have fk: "finite {0..k}" by simp
  1550     from a0 have a0r0: "a$0 = ?r$0" by simp
  1551     {fix n have "a $ n = ?r $ n"
  1552       proof(induct n rule: nat_less_induct)
  1553 	fix n assume h: "\<forall>m<n. a$m = ?r $m"
  1554 	{assume "n = 0" hence "a$n = ?r $n" using a0 by simp }
  1555 	moreover
  1556 	{fix n1 assume n1: "n = Suc n1"
  1557 	  have fK: "finite {0..k}" by simp
  1558 	have nz: "n \<noteq> 0" using n1 by arith
  1559 	let ?Pnk = "natpermute n (Suc k)"
  1560 	let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
  1561 	let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
  1562 	have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
  1563 	have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
  1564 	have f: "finite ?Pnkn" "finite ?Pnknn" 
  1565 	  using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
  1566 	  by (metis natpermute_finite)+
  1567 	let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
  1568 	let ?g = "\<lambda>v. \<Prod>j\<in>{0..k}. a $ v ! j"
  1569 	have "setsum ?g ?Pnkn = setsum (\<lambda>v. a $ n * (?r$0)^k) ?Pnkn" 
  1570 	proof(rule setsum_cong2)
  1571 	  fix v assume v: "v \<in> {xs \<in> natpermute n (Suc k). n \<in> set xs}"
  1572 	  let ?ths = "(\<Prod>j\<in>{0..k}. a $ v ! j) = a $ n * (?r$0)^k"
  1573 	  from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
  1574 	    unfolding Suc_plus1 natpermute_contain_maximal by (auto simp del: replicate.simps)
  1575 	  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))"
  1576 	    apply (rule setprod_cong, simp)
  1577 	    using i a0 by (simp del: replicate.simps)
  1578 	  also have "\<dots> = a $ n * (?r $ 0)^k"
  1579 	    unfolding  setprod_gen_delta[OF fK] using i by simp
  1580 	  finally show ?ths .
  1581 	qed
  1582 	then have th0: "setsum ?g ?Pnkn = of_nat (k+1) * a $ n * (?r $ 0)^k"  
  1583 	  by (simp add: natpermute_max_card[OF nz, simplified])
  1584 	have th1: "setsum ?g ?Pnknn = setsum ?f ?Pnknn"
  1585 	proof (rule setsum_cong2, rule setprod_cong, simp)
  1586 	  fix xs i assume xs: "xs \<in> ?Pnknn" and i: "i \<in> {0..k}"
  1587 	  {assume c: "n \<le> xs ! i"
  1588 	    from xs i have "xs !i \<noteq> n" by (auto simp add: in_set_conv_nth natpermute_def)
  1589 	    with c have c': "n < xs!i" by arith
  1590 	    have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}" by simp_all
  1591 	    have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}" by auto
  1592 	    have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})" using i by auto
  1593 	    from xs have "n = foldl op + 0 xs" by (simp add: natpermute_def)
  1594 	    also have "\<dots> = setsum (nth xs) {0..<Suc k}" unfolding foldl_add_setsum using xs
  1595 	      by (simp add: natpermute_def)
  1596 	    also have "\<dots> = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
  1597 	      unfolding eqs  setsum_Un_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
  1598 	      unfolding setsum_Un_disjoint[OF fths(2) fths(3) d(2)]
  1599 	      by simp
  1600 	    finally have False using c' by simp}
  1601 	  then have thn: "xs!i < n" by arith
  1602 	  from h[rule_format, OF thn]  
  1603 	  show "a$(xs !i) = ?r$(xs!i)" .
  1604 	qed
  1605 	have th00: "\<And>(x::'a). of_nat (Suc k) * (x * inverse (of_nat (Suc k))) = x"
  1606 	  by (simp add: field_simps del: of_nat_Suc)
  1607 	from H have "b$n = a^Suc k $ n" by (simp add: fps_eq_iff)
  1608 	also have "a ^ Suc k$n = setsum ?g ?Pnkn + setsum ?g ?Pnknn"
  1609 	  unfolding fps_power_nth_Suc 
  1610 	  using setsum_Un_disjoint[OF f d, unfolded Suc_plus1[symmetric], 
  1611 	    unfolded eq, of ?g] by simp
  1612 	also have "\<dots> = of_nat (k+1) * a $ n * (?r $ 0)^k + setsum ?f ?Pnknn" unfolding th0 th1 ..
  1613 	finally have "of_nat (k+1) * a $ n * (?r $ 0)^k = b$n - setsum ?f ?Pnknn" by simp
  1614 	then have "a$n = (b$n - setsum ?f ?Pnknn) / (of_nat (k+1) * (?r $ 0)^k)"
  1615 	  apply - 
  1616 	  apply (rule eq_divide_imp')
  1617 	  using r00
  1618 	  apply (simp del: of_nat_Suc)
  1619 	  by (simp add: mult_ac)
  1620 	then have "a$n = ?r $n"
  1621 	  apply (simp del: of_nat_Suc)
  1622 	  unfolding fps_radical_def n1
  1623 	  by (simp add: field_simps n1 th00 del: of_nat_Suc)}
  1624 	ultimately show "a$n = ?r $ n" by (cases n, auto)
  1625       qed}
  1626     then have "a = ?r" by (simp add: fps_eq_iff)}
  1627   ultimately show ?thesis by blast
  1628 qed
  1629 
  1630 
  1631 lemma radical_power: 
  1632   assumes r0: "r (Suc k) ((a$0) ^ Suc k) = a$0" 
  1633   and a0: "(a$0 ::'a::{field, ring_char_0, recpower}) \<noteq> 0"
  1634   shows "(fps_radical r (Suc k) (a ^ Suc k)) = a"
  1635 proof-
  1636   let ?ak = "a^ Suc k"
  1637   have ak0: "?ak $ 0 = (a$0) ^ Suc k" by (simp add: fps_nth_power_0)
  1638   from r0 have th0: "r (Suc k) (a ^ Suc k $ 0) ^ Suc k = a ^ Suc k $ 0" using ak0 by auto
  1639   from r0 ak0 have th1: "r (Suc k) (a ^ Suc k $ 0) = a $ 0" by auto
  1640   from ak0 a0 have ak00: "?ak $ 0 \<noteq>0 " by auto
  1641   from radical_unique[of r k ?ak a, OF th0 th1 ak00] show ?thesis by metis
  1642 qed
  1643 
  1644 lemma fps_deriv_radical: 
  1645   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
  1646   assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 \<noteq> 0"
  1647   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)"
  1648 proof-
  1649   let ?r= "fps_radical r (Suc k) a"
  1650   let ?w = "(fps_const (of_nat (Suc k)) * ?r ^ k)"
  1651   from a0 r0 have r0': "r (Suc k) (a$0) \<noteq> 0" by auto
  1652   from r0' have w0: "?w $ 0 \<noteq> 0" by (simp del: of_nat_Suc)
  1653   note th0 = inverse_mult_eq_1[OF w0]
  1654   let ?iw = "inverse ?w"
  1655   from power_radical[of r, OF r0 a0]
  1656   have "fps_deriv (?r ^ Suc k) = fps_deriv a" by simp
  1657   hence "fps_deriv ?r * ?w = fps_deriv a"
  1658     by (simp add: fps_deriv_power mult_ac)
  1659   hence "?iw * fps_deriv ?r * ?w = ?iw * fps_deriv a" by simp
  1660   hence "fps_deriv ?r * (?iw * ?w) = fps_deriv a / ?w"
  1661     by (simp add: fps_divide_def)
  1662   then show ?thesis unfolding th0 by simp 
  1663 qed
  1664 
  1665 lemma radical_mult_distrib: 
  1666   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
  1667   assumes 
  1668   ra0: "r (k) (a $ 0) ^ k = a $ 0" 
  1669   and rb0: "r (k) (b $ 0) ^ k = b $ 0"
  1670   and r0': "r (k) ((a * b) $ 0) = r (k) (a $ 0) * r (k) (b $ 0)"
  1671   and a0: "a$0 \<noteq> 0"
  1672   and b0: "b$0 \<noteq> 0"
  1673   shows "fps_radical r (k) (a*b) = fps_radical r (k) a * fps_radical r (k) (b)"
  1674 proof-
  1675   from r0' have r0: "(r (k) ((a*b)$0)) ^ k = (a*b)$0"
  1676     by (simp add: fps_mult_nth ra0 rb0 power_mult_distrib)
  1677   {assume "k=0" hence ?thesis by simp}
  1678   moreover
  1679   {fix h assume k: "k = Suc h"
  1680   let ?ra = "fps_radical r (Suc h) a"
  1681   let ?rb = "fps_radical r (Suc h) b"
  1682   have th0: "r (Suc h) ((a * b) $ 0) = (fps_radical r (Suc h) a * fps_radical r (Suc h) b) $ 0" 
  1683     using r0' k by (simp add: fps_mult_nth)
  1684   have ab0: "(a*b) $ 0 \<noteq> 0" using a0 b0 by (simp add: fps_mult_nth)
  1685   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] 
  1686     power_radical[of r, OF ra0[unfolded k] a0] power_radical[of r, OF rb0[unfolded k] b0] k
  1687   have ?thesis by (auto simp add: power_mult_distrib)}
  1688 ultimately show ?thesis by (cases k, auto)
  1689 qed
  1690 
  1691 lemma radical_inverse:
  1692   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
  1693   assumes 
  1694   ra0: "r (k) (a $ 0) ^ k = a $ 0" 
  1695   and ria0: "r (k) (inverse (a $ 0)) = inverse (r (k) (a $ 0))"
  1696   and r1: "(r (k) 1) = 1" 
  1697   and a0: "a$0 \<noteq> 0"
  1698   shows "fps_radical r (k) (inverse a) = inverse (fps_radical r (k) a)"
  1699 proof-
  1700   {assume "k=0" then have ?thesis by simp}
  1701   moreover
  1702   {fix h assume k[simp]: "k = Suc h"
  1703     let ?ra = "fps_radical r (Suc h) a"
  1704     let ?ria = "fps_radical r (Suc h) (inverse a)"
  1705     from ra0 a0 have th00: "r (Suc h) (a$0) \<noteq> 0" by auto
  1706     have ria0': "r (Suc h) (inverse a $ 0) ^ Suc h = inverse a$0"
  1707     using ria0 ra0 a0
  1708     by (simp add: fps_inverse_def  nonzero_power_inverse[OF th00, symmetric])
  1709   from inverse_mult_eq_1[OF a0] have th0: "a * inverse a = 1" 
  1710     by (simp add: mult_commute)
  1711   from radical_unique[where a=1 and b=1 and r=r and k=h, simplified, OF r1[unfolded k]]
  1712   have th01: "fps_radical r (Suc h) 1 = 1" .
  1713   have th1: "r (Suc h) ((a * inverse a) $ 0) ^ Suc h = (a * inverse a) $ 0"
  1714     "r (Suc h) ((a * inverse a) $ 0) =
  1715 r (Suc h) (a $ 0) * r (Suc h) (inverse a $ 0)"
  1716     using r1 unfolding th0  apply (simp_all add: ria0[symmetric])
  1717     apply (simp add: fps_inverse_def a0)
  1718     unfolding ria0[unfolded k]
  1719     using th00 by simp
  1720   from nonzero_imp_inverse_nonzero[OF a0] a0
  1721   have th2: "inverse a $ 0 \<noteq> 0" by (simp add: fps_inverse_def)
  1722   from radical_mult_distrib[of r "Suc h" a "inverse a", OF ra0[unfolded k] ria0' th1(2) a0 th2]
  1723   have th3: "?ra * ?ria = 1" unfolding th0 th01 by simp
  1724   from th00 have ra0: "?ra $ 0 \<noteq> 0" by simp
  1725   from fps_inverse_unique[OF ra0 th3] have ?thesis by simp}
  1726 ultimately show ?thesis by (cases k, auto)
  1727 qed
  1728 
  1729 lemma fps_divide_inverse: "(a::('a::field) fps) / b = a * inverse b"
  1730   by (simp add: fps_divide_def)
  1731 
  1732 lemma radical_divide:
  1733   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
  1734   assumes 
  1735       ra0: "r k (a $ 0) ^ k = a $ 0" 
  1736   and rb0: "r k (b $ 0) ^ k = b $ 0"
  1737   and r1: "r k 1 = 1"
  1738   and rb0': "r k (inverse (b $ 0)) = inverse (r k (b $ 0))" 
  1739   and raib': "r k (a$0 / (b$0)) = r k (a$0) / r k (b$0)"
  1740   and a0: "a$0 \<noteq> 0" 
  1741   and b0: "b$0 \<noteq> 0"
  1742   shows "fps_radical r k (a/b) = fps_radical r k a / fps_radical r k b"
  1743 proof-
  1744   from raib'
  1745   have raib: "r k (a$0 / (b$0)) = r k (a$0) * r k (inverse (b$0))"
  1746     by (simp add: divide_inverse rb0'[symmetric])
  1747 
  1748   {assume "k=0" hence ?thesis by (simp add: fps_divide_def)}
  1749   moreover
  1750   {assume k0: "k\<noteq> 0"
  1751     from b0 k0 rb0 have rbn0: "r k (b $0) \<noteq> 0"
  1752       by (auto simp add: power_0_left)
  1753     
  1754     from rb0 rb0' have rib0: "(r k (inverse (b $ 0)))^k = inverse (b$0)"
  1755     by (simp add: nonzero_power_inverse[OF rbn0, symmetric])
  1756   from rib0 have th0: "r k (inverse b $ 0) ^ k = inverse b $ 0"
  1757     by (simp add:fps_inverse_def b0)
  1758   from raib 
  1759   have th1: "r k ((a * inverse b) $ 0) = r k (a $ 0) * r k (inverse b $ 0)"
  1760     by (simp add: divide_inverse fps_inverse_def  b0 fps_mult_nth)
  1761   from nonzero_imp_inverse_nonzero[OF b0] b0 have th2: "inverse b $ 0 \<noteq> 0"
  1762     by (simp add: fps_inverse_def)
  1763   from radical_mult_distrib[of r k a "inverse b", OF ra0 th0 th1 a0 th2]
  1764   have th: "fps_radical r k (a/b) = fps_radical r k a * fps_radical r k (inverse b)"
  1765     by (simp add: fps_divide_def)
  1766   with radical_inverse[of r k b, OF rb0 rb0' r1 b0]
  1767   have ?thesis by (simp add: fps_divide_def)}
  1768 ultimately show ?thesis by blast
  1769 qed
  1770 
  1771 subsection{* Derivative of composition *}
  1772 
  1773 lemma fps_compose_deriv: 
  1774   fixes a:: "('a::idom) fps"
  1775   assumes b0: "b$0 = 0"
  1776   shows "fps_deriv (a oo b) = ((fps_deriv a) oo b) * (fps_deriv b)"
  1777 proof-
  1778   {fix n
  1779     have "(fps_deriv (a oo b))$n = setsum (\<lambda>i. a $ i * (fps_deriv (b^i))$n) {0.. Suc n}"
  1780       by (simp add: fps_compose_def ring_simps setsum_right_distrib del: of_nat_Suc)
  1781     also have "\<dots> = setsum (\<lambda>i. a$i * ((fps_const (of_nat i)) * (fps_deriv b * (b^(i - 1))))$n) {0.. Suc n}"
  1782       by (simp add: ring_simps fps_deriv_power del: fps_mult_left_const_nth of_nat_Suc)
  1783   also have "\<dots> = setsum (\<lambda>i. of_nat i * a$i * (((b^(i - 1)) * fps_deriv b))$n) {0.. Suc n}"
  1784     unfolding fps_mult_left_const_nth  by (simp add: ring_simps)
  1785   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}"
  1786     unfolding fps_mult_nth ..
  1787   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}"
  1788     apply (rule setsum_mono_zero_right)
  1789     apply (auto simp add: mult_delta_left setsum_delta not_le)
  1790     done
  1791   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}"
  1792     unfolding fps_deriv_nth
  1793     apply (rule setsum_reindex_cong[where f="Suc"])
  1794     by (auto simp add: mult_assoc)
  1795   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}" .
  1796   
  1797   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}"
  1798     unfolding fps_mult_nth by (simp add: mult_ac)
  1799   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}"
  1800     unfolding fps_deriv_nth fps_compose_nth setsum_right_distrib mult_assoc
  1801     apply (rule setsum_cong2)
  1802     apply (rule setsum_mono_zero_left)
  1803     apply (simp_all add: subset_eq)
  1804     apply clarify
  1805     apply (subgoal_tac "b^i$x = 0")
  1806     apply simp
  1807     apply (rule startsby_zero_power_prefix[OF b0, rule_format])
  1808     by simp
  1809   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}"
  1810     unfolding setsum_right_distrib
  1811     apply (subst setsum_commute)
  1812     by ((rule setsum_cong2)+) simp
  1813   finally have "(fps_deriv (a oo b))$n = (((fps_deriv a) oo b) * (fps_deriv b)) $n"
  1814     unfolding th0 by simp}
  1815 then show ?thesis by (simp add: fps_eq_iff)
  1816 qed
  1817 
  1818 lemma fps_mult_X_plus_1_nth:
  1819   "((1+X)*a) $n = (if n = 0 then (a$n :: 'a::comm_ring_1) else a$n + a$(n - 1))"
  1820 proof-
  1821   {assume "n = 0" hence ?thesis by (simp add: fps_mult_nth )}
  1822   moreover
  1823   {fix m assume m: "n = Suc m"
  1824     have "((1+X)*a) $n = setsum (\<lambda>i. (1+X)$i * a$(n-i)) {0..n}"
  1825       by (simp add: fps_mult_nth)
  1826     also have "\<dots> = setsum (\<lambda>i. (1+X)$i * a$(n-i)) {0.. 1}"
  1827       unfolding m
  1828       apply (rule setsum_mono_zero_right)
  1829       by (auto simp add: )
  1830     also have "\<dots> = (if n = 0 then (a$n :: 'a::comm_ring_1) else a$n + a$(n - 1))"
  1831       unfolding m
  1832       by (simp add: )
  1833     finally have ?thesis .}
  1834   ultimately show ?thesis by (cases n, auto)
  1835 qed
  1836 
  1837 subsection{* Finite FPS (i.e. polynomials) and X *}
  1838 lemma fps_poly_sum_X:
  1839   assumes z: "\<forall>i > n. a$i = (0::'a::comm_ring_1)" 
  1840   shows "a = setsum (\<lambda>i. fps_const (a$i) * X^i) {0..n}" (is "a = ?r")
  1841 proof-
  1842   {fix i
  1843     have "a$i = ?r$i" 
  1844       unfolding fps_setsum_nth fps_mult_left_const_nth X_power_nth
  1845       by (simp add: mult_delta_right setsum_delta' z)
  1846   }
  1847   then show ?thesis unfolding fps_eq_iff by blast
  1848 qed
  1849 
  1850 subsection{* Compositional inverses *}
  1851 
  1852 
  1853 fun compinv :: "'a fps \<Rightarrow> nat \<Rightarrow> 'a::{recpower,field}" where
  1854   "compinv a 0 = X$0"
  1855 | "compinv a (Suc n) = (X$ Suc n - setsum (\<lambda>i. (compinv a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
  1856 
  1857 definition "fps_inv a = Abs_fps (compinv a)"
  1858 
  1859 lemma fps_inv: assumes a0: "a$0 = 0" and a1: "a$1 \<noteq> 0"
  1860   shows "fps_inv a oo a = X"
  1861 proof-
  1862   let ?i = "fps_inv a oo a"
  1863   {fix n
  1864     have "?i $n = X$n" 
  1865     proof(induct n rule: nat_less_induct)
  1866       fix n assume h: "\<forall>m<n. ?i$m = X$m"
  1867       {assume "n=0" hence "?i $n = X$n" using a0 
  1868 	  by (simp add: fps_compose_nth fps_inv_def)}
  1869       moreover
  1870       {fix n1 assume n1: "n = Suc n1"
  1871 	have "?i $ n = setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1} + fps_inv a $ Suc n1 * (a $ 1)^ Suc n1"
  1872 	  by (simp add: fps_compose_nth n1 startsby_zero_power_nth_same[OF a0])
  1873 	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})"
  1874 	  using a0 a1 n1 by (simp add: fps_inv_def)
  1875 	also have "\<dots> = X$n" using n1 by simp 
  1876 	finally have "?i $ n = X$n" .}
  1877       ultimately show "?i $ n = X$n" by (cases n, auto)
  1878     qed}
  1879   then show ?thesis by (simp add: fps_eq_iff)
  1880 qed
  1881 
  1882 
  1883 fun gcompinv :: "'a fps \<Rightarrow> 'a fps \<Rightarrow> nat \<Rightarrow> 'a::{recpower,field}" where
  1884   "gcompinv b a 0 = b$0"
  1885 | "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"
  1886 
  1887 definition "fps_ginv b a = Abs_fps (gcompinv b a)"
  1888 
  1889 lemma fps_ginv: assumes a0: "a$0 = 0" and a1: "a$1 \<noteq> 0"
  1890   shows "fps_ginv b a oo a = b"
  1891 proof-
  1892   let ?i = "fps_ginv b a oo a"
  1893   {fix n
  1894     have "?i $n = b$n" 
  1895     proof(induct n rule: nat_less_induct)
  1896       fix n assume h: "\<forall>m<n. ?i$m = b$m"
  1897       {assume "n=0" hence "?i $n = b$n" using a0 
  1898 	  by (simp add: fps_compose_nth fps_ginv_def)}
  1899       moreover
  1900       {fix n1 assume n1: "n = Suc n1"
  1901 	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"
  1902 	  by (simp add: fps_compose_nth n1 startsby_zero_power_nth_same[OF a0])
  1903 	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})"
  1904 	  using a0 a1 n1 by (simp add: fps_ginv_def)
  1905 	also have "\<dots> = b$n" using n1 by simp 
  1906 	finally have "?i $ n = b$n" .}
  1907       ultimately show "?i $ n = b$n" by (cases n, auto)
  1908     qed}
  1909   then show ?thesis by (simp add: fps_eq_iff)
  1910 qed
  1911 
  1912 lemma fps_inv_ginv: "fps_inv = fps_ginv X"
  1913   apply (auto simp add: expand_fun_eq fps_eq_iff fps_inv_def fps_ginv_def)
  1914   apply (induct_tac n rule: nat_less_induct, auto)
  1915   apply (case_tac na)
  1916   apply simp
  1917   apply simp
  1918   done
  1919 
  1920 lemma fps_compose_1[simp]: "1 oo a = 1"
  1921   by (simp add: fps_eq_iff fps_compose_nth fps_power_def mult_delta_left setsum_delta)
  1922 
  1923 lemma fps_compose_0[simp]: "0 oo a = 0"
  1924   by (simp add: fps_eq_iff fps_compose_nth)
  1925 
  1926 lemma fps_pow_0: "fps_pow n 0 = (if n = 0 then 1 else 0)"
  1927   by (induct n, simp_all)
  1928 
  1929 lemma fps_compose_0_right[simp]: "a oo 0 = fps_const (a$0)"
  1930   by (auto simp add: fps_eq_iff fps_compose_nth fps_power_def fps_pow_0 setsum_0')
  1931 
  1932 lemma fps_compose_add_distrib: "(a + b) oo c = (a oo c) + (b oo c)"
  1933   by (simp add: fps_eq_iff fps_compose_nth  ring_simps setsum_addf)
  1934 
  1935 lemma fps_compose_setsum_distrib: "(setsum f S) oo a = setsum (\<lambda>i. f i oo a) S"
  1936 proof-
  1937   {assume "\<not> finite S" hence ?thesis by simp}
  1938   moreover
  1939   {assume fS: "finite S"
  1940     have ?thesis
  1941     proof(rule finite_induct[OF fS])
  1942       show "setsum f {} oo a = (\<Sum>i\<in>{}. f i oo a)" by simp
  1943     next
  1944       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"
  1945       show "setsum f (insert x F) oo a  = setsum (\<lambda>i. f i oo a) (insert x F)"
  1946 	using fF xF h by (simp add: fps_compose_add_distrib)
  1947     qed}
  1948   ultimately show ?thesis by blast 
  1949 qed
  1950 
  1951 lemma convolution_eq: 
  1952   "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}"
  1953   apply (rule setsum_reindex_cong[where f=fst])
  1954   apply (clarsimp simp add: inj_on_def)
  1955   apply (auto simp add: expand_set_eq image_iff)
  1956   apply (rule_tac x= "x" in exI)
  1957   apply clarsimp
  1958   apply (rule_tac x="n - x" in exI)
  1959   apply arith
  1960   done
  1961 
  1962 lemma product_composition_lemma:
  1963   assumes c0: "c$0 = (0::'a::idom)" and d0: "d$0 = 0"
  1964   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")
  1965 proof-
  1966   let ?S = "{(k\<Colon>nat, m\<Colon>nat). k + m \<le> n}"
  1967   have s: "?S \<subseteq> {0..n} <*> {0..n}" by (auto simp add: subset_eq)  
  1968   have f: "finite {(k\<Colon>nat, m\<Colon>nat). k + m \<le> n}" 
  1969     apply (rule finite_subset[OF s])
  1970     by auto
  1971   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}"
  1972     apply (simp add: fps_mult_nth setsum_right_distrib)
  1973     apply (subst setsum_commute)
  1974     apply (rule setsum_cong2)
  1975     by (auto simp add: ring_simps)
  1976   also have "\<dots> = ?l" 
  1977     apply (simp add: fps_mult_nth fps_compose_nth setsum_product)
  1978     apply (rule setsum_cong2)
  1979     apply (simp add: setsum_cartesian_product mult_assoc)
  1980     apply (rule setsum_mono_zero_right[OF f])
  1981     apply (simp add: subset_eq) apply presburger
  1982     apply clarsimp
  1983     apply (rule ccontr)
  1984     apply (clarsimp simp add: not_le)
  1985     apply (case_tac "x < aa")
  1986     apply simp
  1987     apply (frule_tac startsby_zero_power_prefix[rule_format, OF c0])
  1988     apply blast
  1989     apply simp
  1990     apply (frule_tac startsby_zero_power_prefix[rule_format, OF d0])
  1991     apply blast
  1992     done
  1993   finally show ?thesis by simp
  1994 qed
  1995 
  1996 lemma product_composition_lemma':
  1997   assumes c0: "c$0 = (0::'a::idom)" and d0: "d$0 = 0"
  1998   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")
  1999   unfolding product_composition_lemma[OF c0 d0]
  2000   unfolding setsum_cartesian_product
  2001   apply (rule setsum_mono_zero_left)
  2002   apply simp
  2003   apply (clarsimp simp add: subset_eq)
  2004   apply clarsimp
  2005   apply (rule ccontr)
  2006   apply (subgoal_tac "(c^aa * d^ba) $ n = 0")
  2007   apply simp
  2008   unfolding fps_mult_nth
  2009   apply (rule setsum_0')
  2010   apply (clarsimp simp add: not_le)
  2011   apply (case_tac "aaa < aa")
  2012   apply (rule startsby_zero_power_prefix[OF c0, rule_format])
  2013   apply simp
  2014   apply (subgoal_tac "n - aaa < ba")
  2015   apply (frule_tac k = "ba" in startsby_zero_power_prefix[OF d0, rule_format])
  2016   apply simp
  2017   apply arith
  2018   done
  2019   
  2020 
  2021 lemma setsum_pair_less_iff: 
  2022   "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")
  2023 proof-
  2024   let ?KM=  "{(k,m). k + m \<le> n}"
  2025   let ?f = "%s. UNION {(0::nat)..s} (%i. {(i,s - i)})"
  2026   have th0: "?KM = UNION {0..n} ?f"
  2027     apply (simp add: expand_set_eq)
  2028     apply arith (* FIXME: VERY slow! *)
  2029     done
  2030   show "?l = ?r "
  2031     unfolding th0
  2032     apply (subst setsum_UN_disjoint)
  2033     apply auto
  2034     apply (subst setsum_UN_disjoint)
  2035     apply auto
  2036     done
  2037 qed
  2038 
  2039 lemma fps_compose_mult_distrib_lemma:
  2040   assumes c0: "c$0 = (0::'a::idom)"
  2041   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")
  2042   unfolding product_composition_lemma[OF c0 c0] power_add[symmetric]
  2043   unfolding setsum_pair_less_iff[where a = "%k. a$k" and b="%m. b$m" and c="%s. (c ^ s)$n" and n = n] ..
  2044 
  2045 
  2046 lemma fps_compose_mult_distrib: 
  2047   assumes c0: "c$0 = (0::'a::idom)"
  2048   shows "(a * b) oo c = (a oo c) * (b oo c)" (is "?l = ?r")
  2049   apply (simp add: fps_eq_iff fps_compose_mult_distrib_lemma[OF c0])
  2050   by (simp add: fps_compose_nth fps_mult_nth setsum_left_distrib)
  2051 lemma fps_compose_setprod_distrib: 
  2052   assumes c0: "c$0 = (0::'a::idom)"
  2053   shows "(setprod a S) oo c = setprod (%k. a k oo c) S" (is "?l = ?r")
  2054   apply (cases "finite S")
  2055   apply simp_all
  2056   apply (induct S rule: finite_induct)
  2057   apply simp
  2058   apply (simp add: fps_compose_mult_distrib[OF c0])
  2059   done
  2060 
  2061 lemma fps_compose_power:   assumes c0: "c$0 = (0::'a::idom)"
  2062   shows "(a oo c)^n = a^n oo c" (is "?l = ?r")
  2063 proof-
  2064   {assume "n=0" then have ?thesis by simp}
  2065   moreover
  2066   {fix m assume m: "n = Suc m"
  2067     have th0: "a^n = setprod (%k. a) {0..m}" "(a oo c) ^ n = setprod (%k. a oo c) {0..m}"
  2068       by (simp_all add: setprod_constant m)
  2069     then have ?thesis
  2070       by (simp add: fps_compose_setprod_distrib[OF c0])}
  2071   ultimately show ?thesis by (cases n, auto)
  2072 qed
  2073 
  2074 lemma fps_const_mult_apply_left:
  2075   "fps_const c * (a oo b) = (fps_const c * a) oo b"
  2076   by (simp add: fps_eq_iff fps_compose_nth setsum_right_distrib mult_assoc)
  2077 
  2078 lemma fps_const_mult_apply_right:
  2079   "(a oo b) * fps_const (c::'a::comm_semiring_1) = (fps_const c * a) oo b"
  2080   by (auto simp add: fps_const_mult_apply_left mult_commute)
  2081 
  2082 lemma fps_compose_assoc: 
  2083   assumes c0: "c$0 = (0::'a::idom)" and b0: "b$0 = 0"
  2084   shows "a oo (b oo c) = a oo b oo c" (is "?l = ?r")
  2085 proof-
  2086   {fix n
  2087     have "?l$n = (setsum (\<lambda>i. (fps_const (a$i) * b^i) oo c) {0..n})$n"
  2088       by (simp add: fps_compose_nth fps_compose_power[OF c0] fps_const_mult_apply_left setsum_right_distrib mult_assoc fps_setsum_nth)
  2089     also have "\<dots> = ((setsum (\<lambda>i. fps_const (a$i) * b^i) {0..n}) oo c)$n"
  2090       by (simp add: fps_compose_setsum_distrib)
  2091     also have "\<dots> = ?r$n"
  2092       apply (simp add: fps_compose_nth fps_setsum_nth setsum_left_distrib mult_assoc)
  2093       apply (rule setsum_cong2)
  2094       apply (rule setsum_mono_zero_right)
  2095       apply (auto simp add: not_le)
  2096       by (erule startsby_zero_power_prefix[OF b0, rule_format])
  2097     finally have "?l$n = ?r$n" .}
  2098   then show ?thesis by (simp add: fps_eq_iff)
  2099 qed
  2100 
  2101 
  2102 lemma fps_X_power_compose:
  2103   assumes a0: "a$0=0" shows "X^k oo a = (a::('a::idom fps))^k" (is "?l = ?r")
  2104 proof-
  2105   {assume "k=0" hence ?thesis by simp}
  2106   moreover
  2107   {fix h assume h: "k = Suc h"
  2108     {fix n
  2109       {assume kn: "k>n" hence "?l $ n = ?r $n" using a0 startsby_zero_power_prefix[OF a0] h 
  2110 	  by (simp add: fps_compose_nth)}
  2111       moreover
  2112       {assume kn: "k \<le> n"
  2113 	hence "?l$n = ?r$n"
  2114           by (simp add: fps_compose_nth mult_delta_left setsum_delta)}
  2115       moreover have "k >n \<or> k\<le> n"  by arith
  2116       ultimately have "?l$n = ?r$n"  by blast}
  2117     then have ?thesis unfolding fps_eq_iff by blast}
  2118   ultimately show ?thesis by (cases k, auto)
  2119 qed
  2120 
  2121 lemma fps_inv_right: assumes a0: "a$0 = 0" and a1: "a$1 \<noteq> 0"
  2122   shows "a oo fps_inv a = X"
  2123 proof-
  2124   let ?ia = "fps_inv a"
  2125   let ?iaa = "a oo fps_inv a"
  2126   have th0: "?ia $ 0 = 0" by (simp add: fps_inv_def)
  2127   have th1: "?iaa $ 0 = 0" using a0 a1 
  2128     by (simp add: fps_inv_def fps_compose_nth)
  2129   have th2: "X$0 = 0" by simp
  2130   from fps_inv[OF a0 a1] have "a oo (fps_inv a oo a) = a oo X" by simp
  2131   then have "(a oo fps_inv a) oo a = X oo a"
  2132     by (simp add: fps_compose_assoc[OF a0 th0] X_fps_compose_startby0[OF a0])
  2133   with fps_compose_inj_right[OF a0 a1]
  2134   show ?thesis by simp 
  2135 qed
  2136 
  2137 lemma fps_inv_deriv:
  2138   assumes a0:"a$0 = (0::'a::{recpower,field})" and a1: "a$1 \<noteq> 0"
  2139   shows "fps_deriv (fps_inv a) = inverse (fps_deriv a oo fps_inv a)"
  2140 proof-
  2141   let ?ia = "fps_inv a"
  2142   let ?d = "fps_deriv a oo ?ia"
  2143   let ?dia = "fps_deriv ?ia"
  2144   have ia0: "?ia$0 = 0" by (simp add: fps_inv_def)
  2145   have th0: "?d$0 \<noteq> 0" using a1 by (simp add: fps_compose_nth fps_deriv_nth)
  2146   from fps_inv_right[OF a0 a1] have "?d * ?dia = 1"
  2147     by (simp add: fps_compose_deriv[OF ia0, of a, symmetric] )
  2148   hence "inverse ?d * ?d * ?dia = inverse ?d * 1" by simp
  2149   with inverse_mult_eq_1[OF th0]
  2150   show "?dia = inverse ?d" by simp
  2151 qed
  2152 
  2153 subsection{* Elementary series *}
  2154 
  2155 subsubsection{* Exponential series *}
  2156 definition "E x = Abs_fps (\<lambda>n. x^n / of_nat (fact n))"   
  2157 
  2158 lemma E_deriv[simp]: "fps_deriv (E a) = fps_const (a::'a::{field, recpower, ring_char_0}) * E a" (is "?l = ?r")
  2159 proof-
  2160   {fix n
  2161     have "?l$n = ?r $ n"
  2162   apply (auto simp add: E_def field_simps power_Suc[symmetric]simp del: fact_Suc of_nat_Suc)
  2163   by (simp add: of_nat_mult ring_simps)}
  2164 then show ?thesis by (simp add: fps_eq_iff)
  2165 qed
  2166 
  2167 lemma E_unique_ODE: 
  2168   "fps_deriv a = fps_const c * a \<longleftrightarrow> a = fps_const (a$0) * E (c :: 'a::{field, ring_char_0, recpower})"
  2169   (is "?lhs \<longleftrightarrow> ?rhs")
  2170 proof-
  2171   {assume d: ?lhs
  2172   from d have th: "\<And>n. a $ Suc n = c * a$n / of_nat (Suc n)" 
  2173     by (simp add: fps_deriv_def fps_eq_iff field_simps del: of_nat_Suc)
  2174   {fix n have "a$n = a$0 * c ^ n/ (of_nat (fact n))"
  2175       apply (induct n)
  2176       apply simp
  2177       unfolding th 
  2178       using fact_gt_zero
  2179       apply (simp add: field_simps del: of_nat_Suc fact.simps)
  2180       apply (drule sym)
  2181       by (simp add: ring_simps of_nat_mult power_Suc)}
  2182   note th' = this
  2183   have ?rhs 
  2184     by (auto simp add: fps_eq_iff fps_const_mult_left E_def intro : th')}
  2185 moreover
  2186 {assume h: ?rhs
  2187   have ?lhs 
  2188     apply (subst h)
  2189     apply simp
  2190     apply (simp only: h[symmetric])
  2191   by simp}
  2192 ultimately show ?thesis by blast
  2193 qed
  2194 
  2195 lemma E_add_mult: "E (a + b) = E (a::'a::{ring_char_0, field, recpower}) * E b" (is "?l = ?r")
  2196 proof-
  2197   have "fps_deriv (?r) = fps_const (a+b) * ?r"
  2198     by (simp add: fps_const_add[symmetric] ring_simps del: fps_const_add)
  2199   then have "?r = ?l" apply (simp only: E_unique_ODE)
  2200     by (simp add: fps_mult_nth E_def)
  2201   then show ?thesis ..
  2202 qed
  2203 
  2204 lemma E_nth[simp]: "E a $ n = a^n / of_nat (fact n)"
  2205   by (simp add: E_def)
  2206 
  2207 lemma E0[simp]: "E (0::'a::{field, recpower}) = 1"
  2208   by (simp add: fps_eq_iff power_0_left)
  2209 
  2210 lemma E_neg: "E (- a) = inverse (E (a::'a::{ring_char_0, field, recpower}))"
  2211 proof-
  2212   from E_add_mult[of a "- a"] have th0: "E a * E (- a) = 1"
  2213     by (simp )
  2214   have th1: "E a $ 0 \<noteq> 0" by simp
  2215   from fps_inverse_unique[OF th1 th0] show ?thesis by simp
  2216 qed
  2217 
  2218 lemma E_nth_deriv[simp]: "fps_nth_deriv n (E (a::'a::{field, recpower, ring_char_0})) = (fps_const a)^n * (E a)"  
  2219   by (induct n, auto simp add: power_Suc)
  2220 
  2221 lemma fps_compose_uminus: "- (a::'a::ring_1 fps) oo c = - (a oo c)"
  2222   by (simp add: fps_eq_iff fps_compose_nth ring_simps setsum_negf[symmetric])
  2223 
  2224 lemma fps_compose_sub_distrib: 
  2225   shows "(a - b) oo (c::'a::ring_1 fps) = (a oo c) - (b oo c)"
  2226   unfolding diff_minus fps_compose_uminus fps_compose_add_distrib ..
  2227 
  2228 lemma X_fps_compose:"X oo a = Abs_fps (\<lambda>n. if n = 0 then (0::'a::comm_ring_1) else a$n)"
  2229   by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta power_Suc)
  2230 
  2231 lemma X_compose_E[simp]: "X oo E (a::'a::{field, recpower}) = E a - 1"
  2232   by (simp add: fps_eq_iff X_fps_compose)
  2233 
  2234 lemma LE_compose: 
  2235   assumes a: "a\<noteq>0" 
  2236   shows "fps_inv (E a - 1) oo (E a - 1) = X"
  2237   and "(E a - 1) oo fps_inv (E a - 1) = X"
  2238 proof-
  2239   let ?b = "E a - 1"
  2240   have b0: "?b $ 0 = 0" by simp
  2241   have b1: "?b $ 1 \<noteq> 0" by (simp add: a)
  2242   from fps_inv[OF b0 b1] show "fps_inv (E a - 1) oo (E a - 1) = X" .
  2243   from fps_inv_right[OF b0 b1] show "(E a - 1) oo fps_inv (E a - 1) = X" .
  2244 qed
  2245 
  2246 
  2247 lemma fps_const_inverse: 
  2248   "inverse (fps_const (a::'a::{field, division_by_zero})) = fps_const (inverse a)"
  2249   apply (auto simp add: fps_eq_iff fps_inverse_def) by (case_tac "n", auto)
  2250 
  2251 
  2252 lemma inverse_one_plus_X: 
  2253   "inverse (1 + X) = Abs_fps (\<lambda>n. (- 1 ::'a::{field, recpower})^n)"
  2254   (is "inverse ?l = ?r")
  2255 proof-
  2256   have th: "?l * ?r = 1"
  2257     apply (auto simp add: ring_simps fps_eq_iff X_mult_nth  minus_one_power_iff)
  2258     apply presburger+
  2259     done
  2260   have th': "?l $ 0 \<noteq> 0" by (simp add: )
  2261   from fps_inverse_unique[OF th' th] show ?thesis .
  2262 qed
  2263 
  2264 lemma E_power_mult: "(E (c::'a::{field,recpower,ring_char_0}))^n = E (of_nat n * c)"
  2265   by (induct n, auto simp add: ring_simps E_add_mult power_Suc)
  2266 
  2267 subsubsection{* Logarithmic series *}  
  2268 definition "(L::'a::{field, ring_char_0,recpower} fps) 
  2269   = Abs_fps (\<lambda>n. (- 1) ^ Suc n / of_nat n)"
  2270 
  2271 lemma fps_deriv_L: "fps_deriv L = inverse (1 + X)"
  2272   unfolding inverse_one_plus_X
  2273   by (simp add: L_def fps_eq_iff power_Suc del: of_nat_Suc)
  2274 
  2275 lemma L_nth: "L $ n = (- 1) ^ Suc n / of_nat n"
  2276   by (simp add: L_def)
  2277 
  2278 lemma L_E_inv:
  2279   assumes a: "a\<noteq> (0::'a::{field,division_by_zero,ring_char_0,recpower})" 
  2280   shows "L = fps_const a * fps_inv (E a - 1)" (is "?l = ?r")
  2281 proof-
  2282   let ?b = "E a - 1"
  2283   have b0: "?b $ 0 = 0" by simp
  2284   have b1: "?b $ 1 \<noteq> 0" by (simp add: a)
  2285   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)"
  2286     by (simp add: ring_simps)
  2287   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])
  2288     by (simp add: ring_simps)
  2289   finally have eq: "fps_deriv (E a - 1) oo fps_inv (E a - 1) = fps_const a * (X + 1)" .
  2290   from fps_inv_deriv[OF b0 b1, unfolded eq]
  2291   have "fps_deriv (fps_inv ?b) = fps_const (inverse a) / (X + 1)"
  2292     by (simp add: fps_const_inverse eq fps_divide_def fps_inverse_mult)
  2293   hence "fps_deriv (fps_const a * fps_inv ?b) = inverse (X + 1)"
  2294     using a by (simp add: fps_divide_def field_simps)
  2295   hence "fps_deriv ?l = fps_deriv ?r" 
  2296     by (simp add: fps_deriv_L add_commute)
  2297   then show ?thesis unfolding fps_deriv_eq_iff
  2298     by (simp add: L_nth fps_inv_def)
  2299 qed
  2300 
  2301 subsubsection{* Formal trigonometric functions  *}
  2302 
  2303 definition "fps_sin (c::'a::{field, recpower, ring_char_0}) = 
  2304   Abs_fps (\<lambda>n. if even n then 0 else (- 1) ^((n - 1) div 2) * c^n /(of_nat (fact n)))"
  2305 
  2306 definition "fps_cos (c::'a::{field, recpower, ring_char_0}) = Abs_fps (\<lambda>n. if even n then (- 1) ^ (n div 2) * c^n / (of_nat (fact n)) else 0)"
  2307 
  2308 lemma fps_sin_deriv: 
  2309   "fps_deriv (fps_sin c) = fps_const c * fps_cos c"
  2310   (is "?lhs = ?rhs")
  2311 proof-
  2312   {fix n::nat
  2313     {assume en: "even n"
  2314       have "?lhs$n = of_nat (n+1) * (fps_sin c $ (n+1))" by simp
  2315       also have "\<dots> = of_nat (n+1) * ((- 1)^(n div 2) * c^Suc n / of_nat (fact (Suc n)))" 
  2316 	using en by (simp add: fps_sin_def)
  2317       also have "\<dots> = (- 1)^(n div 2) * c^Suc n * (of_nat (n+1) / (of_nat (Suc n) * of_nat (fact n)))"
  2318 	unfolding fact_Suc of_nat_mult
  2319 	by (simp add: field_simps del: of_nat_add of_nat_Suc)
  2320       also have "\<dots> = (- 1)^(n div 2) *c^Suc n / of_nat (fact n)"
  2321 	by (simp add: field_simps del: of_nat_add of_nat_Suc)
  2322       finally have "?lhs $n = ?rhs$n" using en 
  2323 	by (simp add: fps_cos_def ring_simps power_Suc )}
  2324     then have "?lhs $ n = ?rhs $ n" 
  2325       by (cases "even n", simp_all add: fps_deriv_def fps_sin_def fps_cos_def) }
  2326   then show ?thesis by (auto simp add: fps_eq_iff)
  2327 qed
  2328 
  2329 lemma fps_cos_deriv: 
  2330   "fps_deriv (fps_cos c) = fps_const (- c)* (fps_sin c)"
  2331   (is "?lhs = ?rhs")
  2332 proof-
  2333   have th0: "\<And>n. - ((- 1::'a) ^ n) = (- 1)^Suc n" by (simp add: power_Suc)
  2334   have th1: "\<And>n. odd n\<Longrightarrow> Suc ((n - 1) div 2) = Suc n div 2" by presburger (* FIXME: VERY slow! *)
  2335   {fix n::nat
  2336     {assume en: "odd n"
  2337       from en have n0: "n \<noteq>0 " by presburger
  2338       have "?lhs$n = of_nat (n+1) * (fps_cos c $ (n+1))" by simp
  2339       also have "\<dots> = of_nat (n+1) * ((- 1)^((n + 1) div 2) * c^Suc n / of_nat (fact (Suc n)))" 
  2340 	using en by (simp add: fps_cos_def)
  2341       also have "\<dots> = (- 1)^((n + 1) div 2)*c^Suc n * (of_nat (n+1) / (of_nat (Suc n) * of_nat (fact n)))"
  2342 	unfolding fact_Suc of_nat_mult
  2343 	by (simp add: field_simps del: of_nat_add of_nat_Suc)
  2344       also have "\<dots> = (- 1)^((n + 1) div 2) * c^Suc n / of_nat (fact n)"
  2345 	by (simp add: field_simps del: of_nat_add of_nat_Suc)
  2346       also have "\<dots> = (- ((- 1)^((n - 1) div 2))) * c^Suc n / of_nat (fact n)"
  2347 	unfolding th0 unfolding th1[OF en] by simp
  2348       finally have "?lhs $n = ?rhs$n" using en 
  2349 	by (simp add: fps_sin_def ring_simps power_Suc)}
  2350     then have "?lhs $ n = ?rhs $ n" 
  2351       by (cases "even n", simp_all add: fps_deriv_def fps_sin_def 
  2352 	fps_cos_def) }
  2353   then show ?thesis by (auto simp add: fps_eq_iff)
  2354 qed
  2355 
  2356 lemma fps_sin_cos_sum_of_squares:
  2357   "fps_cos c ^ 2 + fps_sin c ^ 2 = 1" (is "?lhs = 1")
  2358 proof-
  2359   have "fps_deriv ?lhs = 0"
  2360     apply (simp add:  fps_deriv_power fps_sin_deriv fps_cos_deriv power_Suc)
  2361     by (simp add: fps_power_def ring_simps fps_const_neg[symmetric] del: fps_const_neg)
  2362   then have "?lhs = fps_const (?lhs $ 0)"
  2363     unfolding fps_deriv_eq_0_iff .
  2364   also have "\<dots> = 1"
  2365     by (auto simp add: fps_eq_iff fps_power_def numeral_2_eq_2 fps_mult_nth fps_cos_def fps_sin_def)
  2366   finally show ?thesis .
  2367 qed
  2368 
  2369 definition "fps_tan c = fps_sin c / fps_cos c"
  2370 
  2371 lemma fps_tan_deriv: "fps_deriv(fps_tan c) = fps_const c/ (fps_cos c ^ 2)"
  2372 proof-
  2373   have th0: "fps_cos c $ 0 \<noteq> 0" by (simp add: fps_cos_def)
  2374   show ?thesis 
  2375     using fps_sin_cos_sum_of_squares[of c]
  2376     apply (simp add: fps_tan_def fps_divide_deriv[OF th0] fps_sin_deriv fps_cos_deriv add: fps_const_neg[symmetric] ring_simps power2_eq_square del: fps_const_neg)
  2377     unfolding right_distrib[symmetric]
  2378     by simp
  2379 qed
  2380 
  2381 end