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