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