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