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