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