src/HOL/Library/Formal_Power_Series.thy
 author huffman Sat Feb 14 16:51:18 2009 -0800 (2009-02-14) changeset 29914 c9ced4f54e82 parent 29913 89eadbe71e97 child 29915 2146e512cec9 permissions -rw-r--r--
generalize lemma eq_neg_iff_add_eq_0, and move to OrderedGroup
```     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_square_eq_iff: "(a:: 'a::idom fps)^ 2 = b^2  \<longleftrightarrow> (a = b \<or> a = -b)"
```
```   695 proof-
```
```   696   {assume "a = b \<or> a = -b" hence "a^2 = b^2" by auto}
```
```   697   moreover
```
```   698   {assume "a^2 = b^2 "
```
```   699     hence "a^2 - b^2 = 0" by simp
```
```   700     hence "(a-b) * (a+b) = 0" by (simp add: power2_eq_square ring_simps)
```
```   701     hence "a = b \<or> a = -b" by (simp add: eq_neg_iff_add_eq_0)}
```
```   702   ultimately show ?thesis by blast
```
```   703 qed
```
```   704
```
```   705 lemma fps_power_zeroth_eq_one: "a\$0 =1 \<Longrightarrow> a^n \$ 0 = (1::'a::semiring_1)"
```
```   706   by (induct n, auto simp add: fps_power_def expand_fps_eq fps_mult_nth)
```
```   707
```
```   708 lemma fps_power_first_eq: "(a:: 'a::comm_ring_1 fps)\$0 =1 \<Longrightarrow> a^n \$ 1 = of_nat n * a\$1"
```
```   709 proof(induct n)
```
```   710   case 0 thus ?case by (simp add: fps_power_def)
```
```   711 next
```
```   712   case (Suc n)
```
```   713   note h = Suc.hyps[OF `a\$0 = 1`]
```
```   714   show ?case unfolding power_Suc fps_mult_nth
```
```   715     using h `a\$0 = 1`  fps_power_zeroth_eq_one[OF `a\$0=1`] by (simp add: ring_simps)
```
```   716 qed
```
```   717
```
```   718 lemma startsby_one_power:"a \$ 0 = (1::'a::comm_ring_1) \<Longrightarrow> a^n \$ 0 = 1"
```
```   719   by (induct n, auto simp add: fps_power_def fps_mult_nth)
```
```   720
```
```   721 lemma startsby_zero_power:"a \$0 = (0::'a::comm_ring_1) \<Longrightarrow> n > 0 \<Longrightarrow> a^n \$0 = 0"
```
```   722   by (induct n, auto simp add: fps_power_def fps_mult_nth)
```
```   723
```
```   724 lemma startsby_power:"a \$0 = (v::'a::{comm_ring_1, recpower}) \<Longrightarrow> a^n \$0 = v^n"
```
```   725   by (induct n, auto simp add: fps_power_def fps_mult_nth power_Suc)
```
```   726
```
```   727 lemma startsby_zero_power_iff[simp]:
```
```   728   "a^n \$0 = (0::'a::{idom, recpower}) \<longleftrightarrow> (n \<noteq> 0 \<and> a\$0 = 0)"
```
```   729 apply (rule iffI)
```
```   730 apply (induct n, auto simp add: power_Suc fps_mult_nth)
```
```   731 by (rule startsby_zero_power, simp_all)
```
```   732
```
```   733 lemma startsby_zero_power_prefix:
```
```   734   assumes a0: "a \$0 = (0::'a::idom)"
```
```   735   shows "\<forall>n < k. a ^ k \$ n = 0"
```
```   736   using a0
```
```   737 proof(induct k rule: nat_less_induct)
```
```   738   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)"
```
```   739   let ?ths = "\<forall>m<k. a ^ k \$ m = 0"
```
```   740   {assume "k = 0" then have ?ths by simp}
```
```   741   moreover
```
```   742   {fix l assume k: "k = Suc l"
```
```   743     {fix m assume mk: "m < k"
```
```   744       {assume "m=0" hence "a^k \$ m = 0" using startsby_zero_power[of a k] k a0
```
```   745 	  by simp}
```
```   746       moreover
```
```   747       {assume m0: "m \<noteq> 0"
```
```   748 	have "a ^k \$ m = (a^l * a) \$m" by (simp add: k power_Suc mult_commute)
```
```   749 	also have "\<dots> = (\<Sum>i = 0..m. a ^ l \$ i * a \$ (m - i))" by (simp add: fps_mult_nth)
```
```   750 	also have "\<dots> = 0" apply (rule setsum_0')
```
```   751 	  apply auto
```
```   752 	  apply (case_tac "aa = m")
```
```   753 	  using a0
```
```   754 	  apply simp
```
```   755 	  apply (rule H[rule_format])
```
```   756 	  using a0 k mk by auto
```
```   757 	finally have "a^k \$ m = 0" .}
```
```   758     ultimately have "a^k \$ m = 0" by blast}
```
```   759     hence ?ths by blast}
```
```   760   ultimately show ?ths by (cases k, auto)
```
```   761 qed
```
```   762
```
```   763 lemma startsby_zero_setsum_depends:
```
```   764   assumes a0: "a \$0 = (0::'a::idom)" and kn: "n \<ge> k"
```
```   765   shows "setsum (\<lambda>i. (a ^ i)\$k) {0 .. n} = setsum (\<lambda>i. (a ^ i)\$k) {0 .. k}"
```
```   766   apply (rule setsum_mono_zero_right)
```
```   767   using kn apply auto
```
```   768   apply (rule startsby_zero_power_prefix[rule_format, OF a0])
```
```   769   by arith
```
```   770
```
```   771 lemma startsby_zero_power_nth_same: assumes a0: "a\$0 = (0::'a::{recpower, idom})"
```
```   772   shows "a^n \$ n = (a\$1) ^ n"
```
```   773 proof(induct n)
```
```   774   case 0 thus ?case by (simp add: power_0)
```
```   775 next
```
```   776   case (Suc n)
```
```   777   have "a ^ Suc n \$ (Suc n) = (a^n * a)\$(Suc n)" by (simp add: ring_simps power_Suc)
```
```   778   also have "\<dots> = setsum (\<lambda>i. a^n\$i * a \$ (Suc n - i)) {0.. Suc n}" by (simp add: fps_mult_nth)
```
```   779   also have "\<dots> = setsum (\<lambda>i. a^n\$i * a \$ (Suc n - i)) {n .. Suc n}"
```
```   780     apply (rule setsum_mono_zero_right)
```
```   781     apply simp
```
```   782     apply clarsimp
```
```   783     apply clarsimp
```
```   784     apply (rule startsby_zero_power_prefix[rule_format, OF a0])
```
```   785     apply arith
```
```   786     done
```
```   787   also have "\<dots> = a^n \$ n * a\$1" using a0 by simp
```
```   788   finally show ?case using Suc.hyps by (simp add: power_Suc)
```
```   789 qed
```
```   790
```
```   791 lemma fps_inverse_power:
```
```   792   fixes a :: "('a::{field, recpower}) fps"
```
```   793   shows "inverse (a^n) = inverse a ^ n"
```
```   794 proof-
```
```   795   {assume a0: "a\$0 = 0"
```
```   796     hence eq: "inverse a = 0" by (simp add: fps_inverse_def)
```
```   797     {assume "n = 0" hence ?thesis by simp}
```
```   798     moreover
```
```   799     {assume n: "n > 0"
```
```   800       from startsby_zero_power[OF a0 n] eq a0 n have ?thesis
```
```   801 	by (simp add: fps_inverse_def)}
```
```   802     ultimately have ?thesis by blast}
```
```   803   moreover
```
```   804   {assume a0: "a\$0 \<noteq> 0"
```
```   805     have ?thesis
```
```   806       apply (rule fps_inverse_unique)
```
```   807       apply (simp add: a0)
```
```   808       unfolding power_mult_distrib[symmetric]
```
```   809       apply (rule ssubst[where t = "a * inverse a" and s= 1])
```
```   810       apply simp_all
```
```   811       apply (subst mult_commute)
```
```   812       by (rule inverse_mult_eq_1[OF a0])}
```
```   813   ultimately show ?thesis by blast
```
```   814 qed
```
```   815
```
```   816 lemma fps_deriv_power: "fps_deriv (a ^ n) = fps_const (of_nat n :: 'a:: comm_ring_1) * fps_deriv a * a ^ (n - 1)"
```
```   817   apply (induct n, auto simp add: power_Suc ring_simps fps_const_add[symmetric] simp del: fps_const_add)
```
```   818   by (case_tac n, auto simp add: power_Suc ring_simps)
```
```   819
```
```   820 lemma fps_inverse_deriv:
```
```   821   fixes a:: "('a :: field) fps"
```
```   822   assumes a0: "a\$0 \<noteq> 0"
```
```   823   shows "fps_deriv (inverse a) = - fps_deriv a * inverse a ^ 2"
```
```   824 proof-
```
```   825   from inverse_mult_eq_1[OF a0]
```
```   826   have "fps_deriv (inverse a * a) = 0" by simp
```
```   827   hence "inverse a * fps_deriv a + fps_deriv (inverse a) * a = 0" by simp
```
```   828   hence "inverse a * (inverse a * fps_deriv a + fps_deriv (inverse a) * a) = 0"  by simp
```
```   829   with inverse_mult_eq_1[OF a0]
```
```   830   have "inverse a ^ 2 * fps_deriv a + fps_deriv (inverse a) = 0"
```
```   831     unfolding power2_eq_square
```
```   832     apply (simp add: ring_simps)
```
```   833     by (simp add: mult_assoc[symmetric])
```
```   834   hence "inverse a ^ 2 * fps_deriv a + fps_deriv (inverse a) - fps_deriv a * inverse a ^ 2 = 0 - fps_deriv a * inverse a ^ 2"
```
```   835     by simp
```
```   836   then show "fps_deriv (inverse a) = - fps_deriv a * inverse a ^ 2" by (simp add: ring_simps)
```
```   837 qed
```
```   838
```
```   839 lemma fps_inverse_mult:
```
```   840   fixes a::"('a :: field) fps"
```
```   841   shows "inverse (a * b) = inverse a * inverse b"
```
```   842 proof-
```
```   843   {assume a0: "a\$0 = 0" hence ab0: "(a*b)\$0 = 0" by (simp add: fps_mult_nth)
```
```   844     from a0 ab0 have th: "inverse a = 0" "inverse (a*b) = 0" by simp_all
```
```   845     have ?thesis unfolding th by simp}
```
```   846   moreover
```
```   847   {assume b0: "b\$0 = 0" hence ab0: "(a*b)\$0 = 0" by (simp add: fps_mult_nth)
```
```   848     from b0 ab0 have th: "inverse b = 0" "inverse (a*b) = 0" by simp_all
```
```   849     have ?thesis unfolding th by simp}
```
```   850   moreover
```
```   851   {assume a0: "a\$0 \<noteq> 0" and b0: "b\$0 \<noteq> 0"
```
```   852     from a0 b0 have ab0:"(a*b) \$ 0 \<noteq> 0" by (simp  add: fps_mult_nth)
```
```   853     from inverse_mult_eq_1[OF ab0]
```
```   854     have "inverse (a*b) * (a*b) * inverse a * inverse b = 1 * inverse a * inverse b" by simp
```
```   855     then have "inverse (a*b) * (inverse a * a) * (inverse b * b) = inverse a * inverse b"
```
```   856       by (simp add: ring_simps)
```
```   857     then have ?thesis using inverse_mult_eq_1[OF a0] inverse_mult_eq_1[OF b0] by simp}
```
```   858 ultimately show ?thesis by blast
```
```   859 qed
```
```   860
```
```   861 lemma fps_inverse_deriv':
```
```   862   fixes a:: "('a :: field) fps"
```
```   863   assumes a0: "a\$0 \<noteq> 0"
```
```   864   shows "fps_deriv (inverse a) = - fps_deriv a / a ^ 2"
```
```   865   using fps_inverse_deriv[OF a0]
```
```   866   unfolding power2_eq_square fps_divide_def
```
```   867     fps_inverse_mult by simp
```
```   868
```
```   869 lemma inverse_mult_eq_1': assumes f0: "f\$0 \<noteq> (0::'a::field)"
```
```   870   shows "f * inverse f= 1"
```
```   871   by (metis mult_commute inverse_mult_eq_1 f0)
```
```   872
```
```   873 lemma fps_divide_deriv:   fixes a:: "('a :: field) fps"
```
```   874   assumes a0: "b\$0 \<noteq> 0"
```
```   875   shows "fps_deriv (a / b) = (fps_deriv a * b - a * fps_deriv b) / b ^ 2"
```
```   876   using fps_inverse_deriv[OF a0]
```
```   877   by (simp add: fps_divide_def ring_simps power2_eq_square fps_inverse_mult inverse_mult_eq_1'[OF a0])
```
```   878
```
```   879 subsection{* The eXtractor series X*}
```
```   880
```
```   881 lemma minus_one_power_iff: "(- (1::'a :: {recpower, comm_ring_1})) ^ n = (if even n then 1 else - 1)"
```
```   882   by (induct n, auto)
```
```   883
```
```   884 definition "X = Abs_fps (\<lambda>n. if n = 1 then 1 else 0)"
```
```   885
```
```   886 lemma fps_inverse_gp': "inverse (Abs_fps(\<lambda>n. (1::'a::field)))
```
```   887   = 1 - X"
```
```   888   by (simp add: fps_inverse_gp fps_eq_iff X_def)
```
```   889
```
```   890 lemma X_mult_nth[simp]: "(X * (f :: ('a::semiring_1) fps)) \$n = (if n = 0 then 0 else f \$ (n - 1))"
```
```   891 proof-
```
```   892   {assume n: "n \<noteq> 0"
```
```   893     have fN: "finite {0 .. n}" by simp
```
```   894     have "(X * f) \$n = (\<Sum>i = 0..n. X \$ i * f \$ (n - i))" by (simp add: fps_mult_nth)
```
```   895     also have "\<dots> = f \$ (n - 1)"
```
```   896       using n by (simp add: X_def mult_delta_left setsum_delta [OF fN])
```
```   897   finally have ?thesis using n by simp }
```
```   898   moreover
```
```   899   {assume n: "n=0" hence ?thesis by (simp add: fps_mult_nth X_def)}
```
```   900   ultimately show ?thesis by blast
```
```   901 qed
```
```   902
```
```   903 lemma X_mult_right_nth[simp]: "((f :: ('a::comm_semiring_1) fps) * X) \$n = (if n = 0 then 0 else f \$ (n - 1))"
```
```   904   by (metis X_mult_nth mult_commute)
```
```   905
```
```   906 lemma X_power_iff: "X^k = Abs_fps (\<lambda>n. if n = k then (1::'a::comm_ring_1) else 0)"
```
```   907 proof(induct k)
```
```   908   case 0 thus ?case by (simp add: X_def fps_power_def fps_eq_iff)
```
```   909 next
```
```   910   case (Suc k)
```
```   911   {fix m
```
```   912     have "(X^Suc k) \$ m = (if m = 0 then (0::'a) else (X^k) \$ (m - 1))"
```
```   913       by (simp add: power_Suc del: One_nat_def)
```
```   914     then     have "(X^Suc k) \$ m = (if m = Suc k then (1::'a) else 0)"
```
```   915       using Suc.hyps by (auto cong del: if_weak_cong)}
```
```   916   then show ?case by (simp add: fps_eq_iff)
```
```   917 qed
```
```   918
```
```   919 lemma X_power_mult_nth: "(X^k * (f :: ('a::comm_ring_1) fps)) \$n = (if n < k then 0 else f \$ (n - k))"
```
```   920   apply (induct k arbitrary: n)
```
```   921   apply (simp)
```
```   922   unfolding power_Suc mult_assoc
```
```   923   by (case_tac n, auto)
```
```   924
```
```   925 lemma X_power_mult_right_nth: "((f :: ('a::comm_ring_1) fps) * X^k) \$n = (if n < k then 0 else f \$ (n - k))"
```
```   926   by (metis X_power_mult_nth mult_commute)
```
```   927 lemma fps_deriv_X[simp]: "fps_deriv X = 1"
```
```   928   by (simp add: fps_deriv_def X_def fps_eq_iff)
```
```   929
```
```   930 lemma fps_nth_deriv_X[simp]: "fps_nth_deriv n X = (if n = 0 then X else if n=1 then 1 else 0)"
```
```   931   by (cases "n", simp_all)
```
```   932
```
```   933 lemma X_nth[simp]: "X\$n = (if n = 1 then 1 else 0)" by (simp add: X_def)
```
```   934 lemma X_power_nth[simp]: "(X^k) \$n = (if n = k then 1 else (0::'a::comm_ring_1))"
```
```   935   by (simp add: X_power_iff)
```
```   936
```
```   937 lemma fps_inverse_X_plus1:
```
```   938   "inverse (1 + X) = Abs_fps (\<lambda>n. (- (1::'a::{recpower, field})) ^ n)" (is "_ = ?r")
```
```   939 proof-
```
```   940   have eq: "(1 + X) * ?r = 1"
```
```   941     unfolding minus_one_power_iff
```
```   942     apply (auto simp add: ring_simps fps_eq_iff)
```
```   943     by presburger+
```
```   944   show ?thesis by (auto simp add: eq intro: fps_inverse_unique)
```
```   945 qed
```
```   946
```
```   947
```
```   948 subsection{* Integration *}
```
```   949 definition "fps_integral a a0 = Abs_fps (\<lambda>n. if n = 0 then a0 else (a\$(n - 1) / of_nat n))"
```
```   950
```
```   951 lemma fps_deriv_fps_integral: "fps_deriv (fps_integral a (a0 :: 'a :: {field, ring_char_0})) = a"
```
```   952   by (simp add: fps_integral_def fps_deriv_def fps_eq_iff field_simps del: of_nat_Suc)
```
```   953
```
```   954 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")
```
```   955 proof-
```
```   956   have "fps_deriv ?l = fps_deriv ?r" by (simp add: fps_deriv_fps_integral)
```
```   957   moreover have "?l\$0 = ?r\$0" by (simp add: fps_integral_def)
```
```   958   ultimately show ?thesis
```
```   959     unfolding fps_deriv_eq_iff by auto
```
```   960 qed
```
```   961
```
```   962 subsection {* Composition of FPSs *}
```
```   963 definition fps_compose :: "('a::semiring_1) fps \<Rightarrow> 'a fps \<Rightarrow> 'a fps" (infixl "oo" 55) where
```
```   964   fps_compose_def: "a oo b = Abs_fps (\<lambda>n. setsum (\<lambda>i. a\$i * (b^i\$n)) {0..n})"
```
```   965
```
```   966 lemma fps_compose_nth: "(a oo b)\$n = setsum (\<lambda>i. a\$i * (b^i\$n)) {0..n}" by (simp add: fps_compose_def)
```
```   967
```
```   968 lemma fps_compose_X[simp]: "a oo X = (a :: ('a :: comm_ring_1) fps)"
```
```   969   by (simp add: fps_ext fps_compose_def mult_delta_right setsum_delta')
```
```   970
```
```   971 lemma fps_const_compose[simp]:
```
```   972   "fps_const (a::'a::{comm_ring_1}) oo b = fps_const (a)"
```
```   973   by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta)
```
```   974
```
```   975 lemma X_fps_compose_startby0[simp]: "a\$0 = 0 \<Longrightarrow> X oo a = (a :: ('a :: comm_ring_1) fps)"
```
```   976   by (simp add: fps_eq_iff fps_compose_def mult_delta_left setsum_delta
```
```   977                 power_Suc not_le)
```
```   978
```
```   979
```
```   980 subsection {* Rules from Herbert Wilf's Generatingfunctionology*}
```
```   981
```
```   982 subsubsection {* Rule 1 *}
```
```   983   (* {a_{n+k}}_0^infty Corresponds to (f - setsum (\<lambda>i. a_i * x^i))/x^h, for h>0*)
```
```   984
```
```   985 lemma fps_power_mult_eq_shift:
```
```   986   "X^Suc k * Abs_fps (\<lambda>n. a (n + Suc k)) = Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a:: field) * X^i) {0 .. k}" (is "?lhs = ?rhs")
```
```   987 proof-
```
```   988   {fix n:: nat
```
```   989     have "?lhs \$ n = (if n < Suc k then 0 else a n)"
```
```   990       unfolding X_power_mult_nth by auto
```
```   991     also have "\<dots> = ?rhs \$ n"
```
```   992     proof(induct k)
```
```   993       case 0 thus ?case by (simp add: fps_setsum_nth power_Suc)
```
```   994     next
```
```   995       case (Suc k)
```
```   996       note th = Suc.hyps[symmetric]
```
```   997       have "(Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a:: 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)
```
```   998       also  have "\<dots> = (if n < Suc k then 0 else a n) - (fps_const (a (Suc k)) * X^ Suc k)\$n"
```
```   999 	using th
```
```  1000 	unfolding fps_sub_nth by simp
```
```  1001       also have "\<dots> = (if n < Suc (Suc k) then 0 else a n)"
```
```  1002 	unfolding X_power_mult_right_nth
```
```  1003 	apply (auto simp add: not_less fps_const_def)
```
```  1004 	apply (rule cong[of a a, OF refl])
```
```  1005 	by arith
```
```  1006       finally show ?case by simp
```
```  1007     qed
```
```  1008     finally have "?lhs \$ n = ?rhs \$ n"  .}
```
```  1009   then show ?thesis by (simp add: fps_eq_iff)
```
```  1010 qed
```
```  1011
```
```  1012 subsubsection{* Rule 2*}
```
```  1013
```
```  1014   (* We can not reach the form of Wilf, but still near to it using rewrite rules*)
```
```  1015   (* If f reprents {a_n} and P is a polynomial, then
```
```  1016         P(xD) f represents {P(n) a_n}*)
```
```  1017
```
```  1018 definition "XD = op * X o fps_deriv"
```
```  1019
```
```  1020 lemma XD_add[simp]:"XD (a + b) = XD a + XD (b :: ('a::comm_ring_1) fps)"
```
```  1021   by (simp add: XD_def ring_simps)
```
```  1022
```
```  1023 lemma XD_mult_const[simp]:"XD (fps_const (c::'a::comm_ring_1) * a) = fps_const c * XD a"
```
```  1024   by (simp add: XD_def ring_simps)
```
```  1025
```
```  1026 lemma XD_linear[simp]: "XD (fps_const c * a + fps_const d * b) = fps_const c * XD a + fps_const d * XD (b :: ('a::comm_ring_1) fps)"
```
```  1027   by simp
```
```  1028
```
```  1029 lemma XDN_linear: "(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)"
```
```  1030   by (induct n, simp_all)
```
```  1031
```
```  1032 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)
```
```  1033
```
```  1034 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)"
```
```  1035 by (induct k arbitrary: a) (simp_all add: power_Suc XD_def fps_eq_iff ring_simps del: One_nat_def)
```
```  1036
```
```  1037 subsubsection{* Rule 3 is trivial and is given by @{text fps_times_def}*}
```
```  1038 subsubsection{* Rule 5 --- summation and "division" by (1 - X)*}
```
```  1039
```
```  1040 lemma fps_divide_X_minus1_setsum_lemma:
```
```  1041   "a = ((1::('a::comm_ring_1) fps) - X) * Abs_fps (\<lambda>n. setsum (\<lambda>i. a \$ i) {0..n})"
```
```  1042 proof-
```
```  1043   let ?X = "X::('a::comm_ring_1) fps"
```
```  1044   let ?sa = "Abs_fps (\<lambda>n. setsum (\<lambda>i. a \$ i) {0..n})"
```
```  1045   have th0: "\<And>i. (1 - (X::'a fps)) \$ i = (if i = 0 then 1 else if i = 1 then - 1 else 0)" by simp
```
```  1046   {fix n:: nat
```
```  1047     {assume "n=0" hence "a\$n = ((1 - ?X) * ?sa) \$ n"
```
```  1048 	by (simp add: fps_mult_nth)}
```
```  1049     moreover
```
```  1050     {assume n0: "n \<noteq> 0"
```
```  1051       then have u: "{0} \<union> ({1} \<union> {2..n}) = {0..n}" "{1}\<union>{2..n} = {1..n}"
```
```  1052 	"{0..n - 1}\<union>{n} = {0..n}"
```
```  1053 	apply (simp_all add: expand_set_eq) by presburger+
```
```  1054       have d: "{0} \<inter> ({1} \<union> {2..n}) = {}" "{1} \<inter> {2..n} = {}"
```
```  1055 	"{0..n - 1}\<inter>{n} ={}" using n0
```
```  1056 	by (simp_all add: expand_set_eq, presburger+)
```
```  1057       have f: "finite {0}" "finite {1}" "finite {2 .. n}"
```
```  1058 	"finite {0 .. n - 1}" "finite {n}" by simp_all
```
```  1059     have "((1 - ?X) * ?sa) \$ n = setsum (\<lambda>i. (1 - ?X)\$ i * ?sa \$ (n - i)) {0 .. n}"
```
```  1060       by (simp add: fps_mult_nth)
```
```  1061     also have "\<dots> = a\$n" unfolding th0
```
```  1062       unfolding setsum_Un_disjoint[OF f(1) finite_UnI[OF f(2,3)] d(1), unfolded u(1)]
```
```  1063       unfolding setsum_Un_disjoint[OF f(2) f(3) d(2)]
```
```  1064       apply (simp)
```
```  1065       unfolding setsum_Un_disjoint[OF f(4,5) d(3), unfolded u(3)]
```
```  1066       by simp
```
```  1067     finally have "a\$n = ((1 - ?X) * ?sa) \$ n" by simp}
```
```  1068   ultimately have "a\$n = ((1 - ?X) * ?sa) \$ n" by blast}
```
```  1069 then show ?thesis
```
```  1070   unfolding fps_eq_iff by blast
```
```  1071 qed
```
```  1072
```
```  1073 lemma fps_divide_X_minus1_setsum:
```
```  1074   "a /((1::('a::field) fps) - X)  = Abs_fps (\<lambda>n. setsum (\<lambda>i. a \$ i) {0..n})"
```
```  1075 proof-
```
```  1076   let ?X = "1 - (X::('a::field) fps)"
```
```  1077   have th0: "?X \$ 0 \<noteq> 0" by simp
```
```  1078   have "a /?X = ?X *  Abs_fps (\<lambda>n\<Colon>nat. setsum (op \$ a) {0..n}) * inverse ?X"
```
```  1079     using fps_divide_X_minus1_setsum_lemma[of a, symmetric] th0
```
```  1080     by (simp add: fps_divide_def mult_assoc)
```
```  1081   also have "\<dots> = (inverse ?X * ?X) * Abs_fps (\<lambda>n\<Colon>nat. setsum (op \$ a) {0..n}) "
```
```  1082     by (simp add: mult_ac)
```
```  1083   finally show ?thesis by (simp add: inverse_mult_eq_1[OF th0])
```
```  1084 qed
```
```  1085
```
```  1086 subsubsection{* Rule 4 in its more general form: generalizes Rule 3 for an arbitrary
```
```  1087   finite product of FPS, also the relvant instance of powers of a FPS*}
```
```  1088
```
```  1089 definition "natpermute n k = {l:: nat list. length l = k \<and> foldl op + 0 l = n}"
```
```  1090
```
```  1091 lemma natlist_trivial_1: "natpermute n 1 = {[n]}"
```
```  1092   apply (auto simp add: natpermute_def)
```
```  1093   apply (case_tac x, auto)
```
```  1094   done
```
```  1095
```
```  1096 lemma foldl_add_start0:
```
```  1097   "foldl op + x xs = x + foldl op + (0::nat) xs"
```
```  1098   apply (induct xs arbitrary: x)
```
```  1099   apply simp
```
```  1100   unfolding foldl.simps
```
```  1101   apply atomize
```
```  1102   apply (subgoal_tac "\<forall>x\<Colon>nat. foldl op + x xs = x + foldl op + (0\<Colon>nat) xs")
```
```  1103   apply (erule_tac x="x + a" in allE)
```
```  1104   apply (erule_tac x="a" in allE)
```
```  1105   apply simp
```
```  1106   apply assumption
```
```  1107   done
```
```  1108
```
```  1109 lemma foldl_add_append: "foldl op + (x::nat) (xs@ys) = foldl op + x xs + foldl op + 0 ys"
```
```  1110   apply (induct ys arbitrary: x xs)
```
```  1111   apply auto
```
```  1112   apply (subst (2) foldl_add_start0)
```
```  1113   apply simp
```
```  1114   apply (subst (2) foldl_add_start0)
```
```  1115   by simp
```
```  1116
```
```  1117 lemma foldl_add_setsum: "foldl op + (x::nat) xs = x + setsum (nth xs) {0..<length xs}"
```
```  1118 proof(induct xs arbitrary: x)
```
```  1119   case Nil thus ?case by simp
```
```  1120 next
```
```  1121   case (Cons a as x)
```
```  1122   have eq: "setsum (op ! (a#as)) {1..<length (a#as)} = setsum (op ! as) {0..<length as}"
```
```  1123     apply (rule setsum_reindex_cong [where f=Suc])
```
```  1124     by (simp_all add: inj_on_def)
```
```  1125   have f: "finite {0}" "finite {1 ..< length (a#as)}" by simp_all
```
```  1126   have d: "{0} \<inter> {1..<length (a#as)} = {}" by simp
```
```  1127   have seq: "{0} \<union> {1..<length(a#as)} = {0 ..<length (a#as)}" by auto
```
```  1128   have "foldl op + x (a#as) = x + foldl op + a as "
```
```  1129     apply (subst foldl_add_start0)    by simp
```
```  1130   also have "\<dots> = x + a + setsum (op ! as) {0..<length as}" unfolding Cons.hyps by simp
```
```  1131   also have "\<dots> = x + setsum (op ! (a#as)) {0..<length (a#as)}"
```
```  1132     unfolding eq[symmetric]
```
```  1133     unfolding setsum_Un_disjoint[OF f d, unfolded seq]
```
```  1134     by simp
```
```  1135   finally show ?case  .
```
```  1136 qed
```
```  1137
```
```  1138
```
```  1139 lemma append_natpermute_less_eq:
```
```  1140   assumes h: "xs@ys \<in> natpermute n k" shows "foldl op + 0 xs \<le> n" and "foldl op + 0 ys \<le> n"
```
```  1141 proof-
```
```  1142   {from h have "foldl op + 0 (xs@ ys) = n" by (simp add: natpermute_def)
```
```  1143     hence "foldl op + 0 xs + foldl op + 0 ys = n" unfolding foldl_add_append .}
```
```  1144   note th = this
```
```  1145   {from th show "foldl op + 0 xs \<le> n" by simp}
```
```  1146   {from th show "foldl op + 0 ys \<le> n" by simp}
```
```  1147 qed
```
```  1148
```
```  1149 lemma natpermute_split:
```
```  1150   assumes mn: "h \<le> k"
```
```  1151   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)")
```
```  1152 proof-
```
```  1153   {fix l assume l: "l \<in> ?R"
```
```  1154     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
```
```  1155     from xs have xs': "foldl op + 0 xs = m" by (simp add: natpermute_def)
```
```  1156     from ys have ys': "foldl op + 0 ys = n - m" by (simp add: natpermute_def)
```
```  1157     have "l \<in> ?L" using leq xs ys h
```
```  1158       apply simp
```
```  1159       apply (clarsimp simp add: natpermute_def simp del: foldl_append)
```
```  1160       apply (simp add: foldl_add_append[unfolded foldl_append])
```
```  1161       unfolding xs' ys'
```
```  1162       using mn xs ys
```
```  1163       unfolding natpermute_def by simp}
```
```  1164   moreover
```
```  1165   {fix l assume l: "l \<in> natpermute n k"
```
```  1166     let ?xs = "take h l"
```
```  1167     let ?ys = "drop h l"
```
```  1168     let ?m = "foldl op + 0 ?xs"
```
```  1169     from l have ls: "foldl op + 0 (?xs @ ?ys) = n" by (simp add: natpermute_def)
```
```  1170     have xs: "?xs \<in> natpermute ?m h" using l mn by (simp add: natpermute_def)
```
```  1171     have ys: "?ys \<in> natpermute (n - ?m) (k - h)" using l mn ls[unfolded foldl_add_append]
```
```  1172       by (simp add: natpermute_def)
```
```  1173     from ls have m: "?m \<in> {0..n}"  unfolding foldl_add_append by simp
```
```  1174     from xs ys ls have "l \<in> ?R"
```
```  1175       apply auto
```
```  1176       apply (rule bexI[where x = "?m"])
```
```  1177       apply (rule exI[where x = "?xs"])
```
```  1178       apply (rule exI[where x = "?ys"])
```
```  1179       using ls l unfolding foldl_add_append
```
```  1180       by (auto simp add: natpermute_def)}
```
```  1181   ultimately show ?thesis by blast
```
```  1182 qed
```
```  1183
```
```  1184 lemma natpermute_0: "natpermute n 0 = (if n = 0 then {[]} else {})"
```
```  1185   by (auto simp add: natpermute_def)
```
```  1186 lemma natpermute_0'[simp]: "natpermute 0 k = (if k = 0 then {[]} else {replicate k 0})"
```
```  1187   apply (auto simp add: set_replicate_conv_if natpermute_def)
```
```  1188   apply (rule nth_equalityI)
```
```  1189   by simp_all
```
```  1190
```
```  1191 lemma natpermute_finite: "finite (natpermute n k)"
```
```  1192 proof(induct k arbitrary: n)
```
```  1193   case 0 thus ?case
```
```  1194     apply (subst natpermute_split[of 0 0, simplified])
```
```  1195     by (simp add: natpermute_0)
```
```  1196 next
```
```  1197   case (Suc k)
```
```  1198   then show ?case unfolding natpermute_split[of k "Suc k", simplified]
```
```  1199     apply -
```
```  1200     apply (rule finite_UN_I)
```
```  1201     apply simp
```
```  1202     unfolding One_nat_def[symmetric] natlist_trivial_1
```
```  1203     apply simp
```
```  1204     unfolding image_Collect[symmetric]
```
```  1205     unfolding Collect_def mem_def
```
```  1206     apply (rule finite_imageI)
```
```  1207     apply blast
```
```  1208     done
```
```  1209 qed
```
```  1210
```
```  1211 lemma natpermute_contain_maximal:
```
```  1212   "{xs \<in> natpermute n (k+1). n \<in> set xs} = UNION {0 .. k} (\<lambda>i. {(replicate (k+1) 0) [i:=n]})"
```
```  1213   (is "?A = ?B")
```
```  1214 proof-
```
```  1215   {fix xs assume H: "xs \<in> natpermute n (k+1)" and n: "n \<in> set xs"
```
```  1216     from n obtain i where i: "i \<in> {0.. k}" "xs!i = n" using H
```
```  1217       unfolding in_set_conv_nth by (auto simp add: less_Suc_eq_le natpermute_def)
```
```  1218     have eqs: "({0..k} - {i}) \<union> {i} = {0..k}" using i by auto
```
```  1219     have f: "finite({0..k} - {i})" "finite {i}" by auto
```
```  1220     have d: "({0..k} - {i}) \<inter> {i} = {}" using i by auto
```
```  1221     from H have "n = setsum (nth xs) {0..k}" apply (simp add: natpermute_def)
```
```  1222       unfolding foldl_add_setsum by (auto simp add: atLeastLessThanSuc_atLeastAtMost)
```
```  1223     also have "\<dots> = n + setsum (nth xs) ({0..k} - {i})"
```
```  1224       unfolding setsum_Un_disjoint[OF f d, unfolded eqs] using i by simp
```
```  1225     finally have zxs: "\<forall> j\<in> {0..k} - {i}. xs!j = 0" by auto
```
```  1226     from H have xsl: "length xs = k+1" by (simp add: natpermute_def)
```
```  1227     from i have i': "i < length (replicate (k+1) 0)"   "i < k+1"
```
```  1228       unfolding length_replicate  by arith+
```
```  1229     have "xs = replicate (k+1) 0 [i := n]"
```
```  1230       apply (rule nth_equalityI)
```
```  1231       unfolding xsl length_list_update length_replicate
```
```  1232       apply simp
```
```  1233       apply clarify
```
```  1234       unfolding nth_list_update[OF i'(1)]
```
```  1235       using i zxs
```
```  1236       by (case_tac "ia=i", auto simp del: replicate.simps)
```
```  1237     then have "xs \<in> ?B" using i by blast}
```
```  1238   moreover
```
```  1239   {fix i assume i: "i \<in> {0..k}"
```
```  1240     let ?xs = "replicate (k+1) 0 [i:=n]"
```
```  1241     have nxs: "n \<in> set ?xs"
```
```  1242       apply (rule set_update_memI) using i by simp
```
```  1243     have xsl: "length ?xs = k+1" by (simp only: length_replicate length_list_update)
```
```  1244     have "foldl op + 0 ?xs = setsum (nth ?xs) {0..<k+1}"
```
```  1245       unfolding foldl_add_setsum add_0 length_replicate length_list_update ..
```
```  1246     also have "\<dots> = setsum (\<lambda>j. if j = i then n else 0) {0..< k+1}"
```
```  1247       apply (rule setsum_cong2) by (simp del: replicate.simps)
```
```  1248     also have "\<dots> = n" using i by (simp add: setsum_delta)
```
```  1249     finally
```
```  1250     have "?xs \<in> natpermute n (k+1)" using xsl unfolding natpermute_def Collect_def mem_def
```
```  1251       by blast
```
```  1252     then have "?xs \<in> ?A"  using nxs  by blast}
```
```  1253   ultimately show ?thesis by auto
```
```  1254 qed
```
```  1255
```
```  1256     (* The general form *)
```
```  1257 lemma fps_setprod_nth:
```
```  1258   fixes m :: nat and a :: "nat \<Rightarrow> ('a::comm_ring_1) fps"
```
```  1259   shows "(setprod a {0 .. m})\$n = setsum (\<lambda>v. setprod (\<lambda>j. (a j) \$ (v!j)) {0..m}) (natpermute n (m+1))"
```
```  1260   (is "?P m n")
```
```  1261 proof(induct m arbitrary: n rule: nat_less_induct)
```
```  1262   fix m n assume H: "\<forall>m' < m. \<forall>n. ?P m' n"
```
```  1263   {assume m0: "m = 0"
```
```  1264     hence "?P m n" apply simp
```
```  1265       unfolding natlist_trivial_1[where n = n, unfolded One_nat_def] by simp}
```
```  1266   moreover
```
```  1267   {fix k assume k: "m = Suc k"
```
```  1268     have km: "k < m" using k by arith
```
```  1269     have u0: "{0 .. k} \<union> {m} = {0..m}" using k apply (simp add: expand_set_eq) by presburger
```
```  1270     have f0: "finite {0 .. k}" "finite {m}" by auto
```
```  1271     have d0: "{0 .. k} \<inter> {m} = {}" using k by auto
```
```  1272     have "(setprod a {0 .. m}) \$ n = (setprod a {0 .. k} * a m) \$ n"
```
```  1273       unfolding setprod_Un_disjoint[OF f0 d0, unfolded u0] by simp
```
```  1274     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))"
```
```  1275       unfolding fps_mult_nth H[rule_format, OF km] ..
```
```  1276     also have "\<dots> = (\<Sum>v\<in>natpermute n (m + 1). \<Prod>j\<in>{0..m}. a j \$ v ! j)"
```
```  1277       apply (simp add: k)
```
```  1278       unfolding natpermute_split[of m "m + 1", simplified, of n, unfolded natlist_trivial_1[unfolded One_nat_def] k]
```
```  1279       apply (subst setsum_UN_disjoint)
```
```  1280       apply simp
```
```  1281       apply simp
```
```  1282       unfolding image_Collect[symmetric]
```
```  1283       apply clarsimp
```
```  1284       apply (rule finite_imageI)
```
```  1285       apply (rule natpermute_finite)
```
```  1286       apply (clarsimp simp add: expand_set_eq)
```
```  1287       apply auto
```
```  1288       apply (rule setsum_cong2)
```
```  1289       unfolding setsum_left_distrib
```
```  1290       apply (rule sym)
```
```  1291       apply (rule_tac f="\<lambda>xs. xs @[n - x]" in  setsum_reindex_cong)
```
```  1292       apply (simp add: inj_on_def)
```
```  1293       apply auto
```
```  1294       unfolding setprod_Un_disjoint[OF f0 d0, unfolded u0, unfolded k]
```
```  1295       apply (clarsimp simp add: natpermute_def nth_append)
```
```  1296       apply (rule_tac f="\<lambda>x. x * a (Suc k) \$ (n - foldl op + 0 aa)" in cong[OF refl])
```
```  1297       apply (rule setprod_cong)
```
```  1298       apply simp
```
```  1299       apply simp
```
```  1300       done
```
```  1301     finally have "?P m n" .}
```
```  1302   ultimately show "?P m n " by (cases m, auto)
```
```  1303 qed
```
```  1304
```
```  1305 text{* The special form for powers *}
```
```  1306 lemma fps_power_nth_Suc:
```
```  1307   fixes m :: nat and a :: "('a::comm_ring_1) fps"
```
```  1308   shows "(a ^ Suc m)\$n = setsum (\<lambda>v. setprod (\<lambda>j. a \$ (v!j)) {0..m}) (natpermute n (m+1))"
```
```  1309 proof-
```
```  1310   have f: "finite {0 ..m}" by simp
```
```  1311   have th0: "a^Suc m = setprod (\<lambda>i. a) {0..m}" unfolding setprod_constant[OF f, of a] by simp
```
```  1312   show ?thesis unfolding th0 fps_setprod_nth ..
```
```  1313 qed
```
```  1314 lemma fps_power_nth:
```
```  1315   fixes m :: nat and a :: "('a::comm_ring_1) fps"
```
```  1316   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))"
```
```  1317   by (cases m, simp_all add: fps_power_nth_Suc)
```
```  1318
```
```  1319 lemma fps_nth_power_0:
```
```  1320   fixes m :: nat and a :: "('a::{comm_ring_1, recpower}) fps"
```
```  1321   shows "(a ^m)\$0 = (a\$0) ^ m"
```
```  1322 proof-
```
```  1323   {assume "m=0" hence ?thesis by simp}
```
```  1324   moreover
```
```  1325   {fix n assume m: "m = Suc n"
```
```  1326     have c: "m = card {0..n}" using m by simp
```
```  1327    have "(a ^m)\$0 = setprod (\<lambda>i. a\$0) {0..n}"
```
```  1328      apply (simp add: m fps_power_nth del: replicate.simps)
```
```  1329      apply (rule setprod_cong)
```
```  1330      by (simp_all del: replicate.simps)
```
```  1331    also have "\<dots> = (a\$0) ^ m"
```
```  1332      unfolding c by (rule setprod_constant, simp)
```
```  1333    finally have ?thesis .}
```
```  1334  ultimately show ?thesis by (cases m, auto)
```
```  1335 qed
```
```  1336
```
```  1337 lemma fps_compose_inj_right:
```
```  1338   assumes a0: "a\$0 = (0::'a::{recpower,idom})"
```
```  1339   and a1: "a\$1 \<noteq> 0"
```
```  1340   shows "(b oo a = c oo a) \<longleftrightarrow> b = c" (is "?lhs \<longleftrightarrow>?rhs")
```
```  1341 proof-
```
```  1342   {assume ?rhs then have "?lhs" by simp}
```
```  1343   moreover
```
```  1344   {assume h: ?lhs
```
```  1345     {fix n have "b\$n = c\$n"
```
```  1346       proof(induct n rule: nat_less_induct)
```
```  1347 	fix n assume H: "\<forall>m<n. b\$m = c\$m"
```
```  1348 	{assume n0: "n=0"
```
```  1349 	  from h have "(b oo a)\$n = (c oo a)\$n" by simp
```
```  1350 	  hence "b\$n = c\$n" using n0 by (simp add: fps_compose_nth)}
```
```  1351 	moreover
```
```  1352 	{fix n1 assume n1: "n = Suc n1"
```
```  1353 	  have f: "finite {0 .. n1}" "finite {n}" by simp_all
```
```  1354 	  have eq: "{0 .. n1} \<union> {n} = {0 .. n}" using n1 by auto
```
```  1355 	  have d: "{0 .. n1} \<inter> {n} = {}" using n1 by auto
```
```  1356 	  have seq: "(\<Sum>i = 0..n1. b \$ i * a ^ i \$ n) = (\<Sum>i = 0..n1. c \$ i * a ^ i \$ n)"
```
```  1357 	    apply (rule setsum_cong2)
```
```  1358 	    using H n1 by auto
```
```  1359 	  have th0: "(b oo a) \$n = (\<Sum>i = 0..n1. c \$ i * a ^ i \$ n) + b\$n * (a\$1)^n"
```
```  1360 	    unfolding fps_compose_nth setsum_Un_disjoint[OF f d, unfolded eq] seq
```
```  1361 	    using startsby_zero_power_nth_same[OF a0]
```
```  1362 	    by simp
```
```  1363 	  have th1: "(c oo a) \$n = (\<Sum>i = 0..n1. c \$ i * a ^ i \$ n) + c\$n * (a\$1)^n"
```
```  1364 	    unfolding fps_compose_nth setsum_Un_disjoint[OF f d, unfolded eq]
```
```  1365 	    using startsby_zero_power_nth_same[OF a0]
```
```  1366 	    by simp
```
```  1367 	  from h[unfolded fps_eq_iff, rule_format, of n] th0 th1 a1
```
```  1368 	  have "b\$n = c\$n" by auto}
```
```  1369 	ultimately show "b\$n = c\$n" by (cases n, auto)
```
```  1370       qed}
```
```  1371     then have ?rhs by (simp add: fps_eq_iff)}
```
```  1372   ultimately show ?thesis by blast
```
```  1373 qed
```
```  1374
```
```  1375
```
```  1376 subsection {* Radicals *}
```
```  1377
```
```  1378 declare setprod_cong[fundef_cong]
```
```  1379 function radical :: "(nat \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> ('a::{field, recpower}) fps \<Rightarrow> nat \<Rightarrow> 'a" where
```
```  1380   "radical r 0 a 0 = 1"
```
```  1381 | "radical r 0 a (Suc n) = 0"
```
```  1382 | "radical r (Suc k) a 0 = r (Suc k) (a\$0)"
```
```  1383 | "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)"
```
```  1384 by pat_completeness auto
```
```  1385
```
```  1386 termination radical
```
```  1387 proof
```
```  1388   let ?R = "measure (\<lambda>(r, k, a, n). n)"
```
```  1389   {
```
```  1390     show "wf ?R" by auto}
```
```  1391   {fix r k a n xs i
```
```  1392     assume xs: "xs \<in> {xs \<in> natpermute (Suc n) (Suc k). Suc n \<notin> set xs}" and i: "i \<in> {0..k}"
```
```  1393     {assume c: "Suc n \<le> xs ! i"
```
```  1394       from xs i have "xs !i \<noteq> Suc n" by (auto simp add: in_set_conv_nth natpermute_def)
```
```  1395       with c have c': "Suc n < xs!i" by arith
```
```  1396       have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}" by simp_all
```
```  1397       have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}" by auto
```
```  1398       have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})" using i by auto
```
```  1399       from xs have "Suc n = foldl op + 0 xs" by (simp add: natpermute_def)
```
```  1400       also have "\<dots> = setsum (nth xs) {0..<Suc k}" unfolding foldl_add_setsum using xs
```
```  1401 	by (simp add: natpermute_def)
```
```  1402       also have "\<dots> = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
```
```  1403 	unfolding eqs  setsum_Un_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
```
```  1404 	unfolding setsum_Un_disjoint[OF fths(2) fths(3) d(2)]
```
```  1405 	by simp
```
```  1406       finally have False using c' by simp}
```
```  1407     then show "((r,Suc k,a,xs!i), r, Suc k, a, Suc n) \<in> ?R"
```
```  1408       apply auto by (metis not_less)}
```
```  1409   {fix r k a n
```
```  1410     show "((r,Suc k, a, 0),r, Suc k, a, Suc n) \<in> ?R" by simp}
```
```  1411 qed
```
```  1412
```
```  1413 definition "fps_radical r n a = Abs_fps (radical r n a)"
```
```  1414
```
```  1415 lemma fps_radical0[simp]: "fps_radical r 0 a = 1"
```
```  1416   apply (auto simp add: fps_eq_iff fps_radical_def)  by (case_tac n, auto)
```
```  1417
```
```  1418 lemma fps_radical_nth_0[simp]: "fps_radical r n a \$ 0 = (if n=0 then 1 else r n (a\$0))"
```
```  1419   by (cases n, simp_all add: fps_radical_def)
```
```  1420
```
```  1421 lemma fps_radical_power_nth[simp]:
```
```  1422   assumes r: "(r k (a\$0)) ^ k = a\$0"
```
```  1423   shows "fps_radical r k a ^ k \$ 0 = (if k = 0 then 1 else a\$0)"
```
```  1424 proof-
```
```  1425   {assume "k=0" hence ?thesis by simp }
```
```  1426   moreover
```
```  1427   {fix h assume h: "k = Suc h"
```
```  1428     have fh: "finite {0..h}" by simp
```
```  1429     have eq1: "fps_radical r k a ^ k \$ 0 = (\<Prod>j\<in>{0..h}. fps_radical r k a \$ (replicate k 0) ! j)"
```
```  1430       unfolding fps_power_nth h by simp
```
```  1431     also have "\<dots> = (\<Prod>j\<in>{0..h}. r k (a\$0))"
```
```  1432       apply (rule setprod_cong)
```
```  1433       apply simp
```
```  1434       using h
```
```  1435       apply (subgoal_tac "replicate k (0::nat) ! x = 0")
```
```  1436       by (auto intro: nth_replicate simp del: replicate.simps)
```
```  1437     also have "\<dots> = a\$0"
```
```  1438       unfolding setprod_constant[OF fh] using r by (simp add: h)
```
```  1439     finally have ?thesis using h by simp}
```
```  1440   ultimately show ?thesis by (cases k, auto)
```
```  1441 qed
```
```  1442
```
```  1443 lemma natpermute_max_card: assumes n0: "n\<noteq>0"
```
```  1444   shows "card {xs \<in> natpermute n (k+1). n \<in> set xs} = k+1"
```
```  1445   unfolding natpermute_contain_maximal
```
```  1446 proof-
```
```  1447   let ?A= "\<lambda>i. {replicate (k + 1) 0[i := n]}"
```
```  1448   let ?K = "{0 ..k}"
```
```  1449   have fK: "finite ?K" by simp
```
```  1450   have fAK: "\<forall>i\<in>?K. finite (?A i)" by auto
```
```  1451   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]} = {}"
```
```  1452   proof(clarify)
```
```  1453     fix i j assume i: "i \<in> ?K" and j: "j\<in> ?K" and ij: "i\<noteq>j"
```
```  1454     {assume eq: "replicate (k+1) 0 [i:=n] = replicate (k+1) 0 [j:= n]"
```
```  1455       have "(replicate (k+1) 0 [i:=n] ! i) = n" using i by (simp del: replicate.simps)
```
```  1456       moreover
```
```  1457       have "(replicate (k+1) 0 [j:=n] ! i) = 0" using i ij by (simp del: replicate.simps)
```
```  1458       ultimately have False using eq n0 by (simp del: replicate.simps)}
```
```  1459     then show "{replicate (k + 1) 0[i := n]} \<inter> {replicate (k + 1) 0[j := n]} = {}"
```
```  1460       by auto
```
```  1461   qed
```
```  1462   from card_UN_disjoint[OF fK fAK d]
```
```  1463   show "card (\<Union>i\<in>{0..k}. {replicate (k + 1) 0[i := n]}) = k+1" by simp
```
```  1464 qed
```
```  1465
```
```  1466 lemma power_radical:
```
```  1467   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
```
```  1468   assumes r0: "(r (Suc k) (a\$0)) ^ Suc k = a\$0" and a0: "a\$0 \<noteq> 0"
```
```  1469   shows "(fps_radical r (Suc k) a) ^ (Suc k) = a"
```
```  1470 proof-
```
```  1471   let ?r = "fps_radical r (Suc k) a"
```
```  1472   from a0 r0 have r00: "r (Suc k) (a\$0) \<noteq> 0" by auto
```
```  1473   {fix z have "?r ^ Suc k \$ z = a\$z"
```
```  1474     proof(induct z rule: nat_less_induct)
```
```  1475       fix n assume H: "\<forall>m<n. ?r ^ Suc k \$ m = a\$m"
```
```  1476       {assume "n = 0" hence "?r ^ Suc k \$ n = a \$n"
```
```  1477 	  using fps_radical_power_nth[of r "Suc k" a, OF r0] by simp}
```
```  1478       moreover
```
```  1479       {fix n1 assume n1: "n = Suc n1"
```
```  1480 	have fK: "finite {0..k}" by simp
```
```  1481 	have nz: "n \<noteq> 0" using n1 by arith
```
```  1482 	let ?Pnk = "natpermute n (k + 1)"
```
```  1483 	let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
```
```  1484 	let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
```
```  1485 	have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
```
```  1486 	have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
```
```  1487 	have f: "finite ?Pnkn" "finite ?Pnknn"
```
```  1488 	  using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
```
```  1489 	  by (metis natpermute_finite)+
```
```  1490 	let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r \$ v ! j"
```
```  1491 	have "setsum ?f ?Pnkn = setsum (\<lambda>v. ?r \$ n * r (Suc k) (a \$ 0) ^ k) ?Pnkn"
```
```  1492 	proof(rule setsum_cong2)
```
```  1493 	  fix v assume v: "v \<in> {xs \<in> natpermute n (k + 1). n \<in> set xs}"
```
```  1494 	  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"
```
```  1495 	  from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
```
```  1496 	    unfolding natpermute_contain_maximal by auto
```
```  1497 	  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))"
```
```  1498 	    apply (rule setprod_cong, simp)
```
```  1499 	    using i r0 by (simp del: replicate.simps)
```
```  1500 	  also have "\<dots> = (fps_radical r (Suc k) a \$ n) * r (Suc k) (a\$0) ^ k"
```
```  1501 	    unfolding setprod_gen_delta[OF fK] using i r0 by simp
```
```  1502 	  finally show ?ths .
```
```  1503 	qed
```
```  1504 	then have "setsum ?f ?Pnkn = of_nat (k+1) * ?r \$ n * r (Suc k) (a \$ 0) ^ k"
```
```  1505 	  by (simp add: natpermute_max_card[OF nz, simplified])
```
```  1506 	also have "\<dots> = a\$n - setsum ?f ?Pnknn"
```
```  1507 	  unfolding n1 using r00 a0 by (simp add: field_simps fps_radical_def del: of_nat_Suc )
```
```  1508 	finally have fn: "setsum ?f ?Pnkn = a\$n - setsum ?f ?Pnknn" .
```
```  1509 	have "(?r ^ Suc k)\$n = setsum ?f ?Pnkn + setsum ?f ?Pnknn"
```
```  1510 	  unfolding fps_power_nth_Suc setsum_Un_disjoint[OF f d, unfolded eq] ..
```
```  1511 	also have "\<dots> = a\$n" unfolding fn by simp
```
```  1512 	finally have "?r ^ Suc k \$ n = a \$n" .}
```
```  1513       ultimately  show "?r ^ Suc k \$ n = a \$n" by (cases n, auto)
```
```  1514   qed }
```
```  1515   then show ?thesis by (simp add: fps_eq_iff)
```
```  1516 qed
```
```  1517
```
```  1518 lemma eq_divide_imp': assumes c0: "(c::'a::field) ~= 0" and eq: "a * c = b"
```
```  1519   shows "a = b / c"
```
```  1520 proof-
```
```  1521   from eq have "a * c * inverse c = b * inverse c" by simp
```
```  1522   hence "a * (inverse c * c) = b/c" by (simp only: field_simps divide_inverse)
```
```  1523   then show "a = b/c" unfolding  field_inverse[OF c0] by simp
```
```  1524 qed
```
```  1525
```
```  1526 lemma radical_unique:
```
```  1527   assumes r0: "(r (Suc k) (b\$0)) ^ Suc k = b\$0"
```
```  1528   and a0: "r (Suc k) (b\$0 ::'a::{field, ring_char_0, recpower}) = a\$0" and b0: "b\$0 \<noteq> 0"
```
```  1529   shows "a^(Suc k) = b \<longleftrightarrow> a = fps_radical r (Suc k) b"
```
```  1530 proof-
```
```  1531   let ?r = "fps_radical r (Suc k) b"
```
```  1532   have r00: "r (Suc k) (b\$0) \<noteq> 0" using b0 r0 by auto
```
```  1533   {assume H: "a = ?r"
```
```  1534     from H have "a^Suc k = b" using power_radical[of r k, OF r0 b0] by simp}
```
```  1535   moreover
```
```  1536   {assume H: "a^Suc k = b"
```
```  1537     (* Generally a\$0 would need to be the k+1 st root of b\$0 *)
```
```  1538     have ceq: "card {0..k} = Suc k" by simp
```
```  1539     have fk: "finite {0..k}" by simp
```
```  1540     from a0 have a0r0: "a\$0 = ?r\$0" by simp
```
```  1541     {fix n have "a \$ n = ?r \$ n"
```
```  1542       proof(induct n rule: nat_less_induct)
```
```  1543 	fix n assume h: "\<forall>m<n. a\$m = ?r \$m"
```
```  1544 	{assume "n = 0" hence "a\$n = ?r \$n" using a0 by simp }
```
```  1545 	moreover
```
```  1546 	{fix n1 assume n1: "n = Suc n1"
```
```  1547 	  have fK: "finite {0..k}" by simp
```
```  1548 	have nz: "n \<noteq> 0" using n1 by arith
```
```  1549 	let ?Pnk = "natpermute n (Suc k)"
```
```  1550 	let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
```
```  1551 	let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
```
```  1552 	have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
```
```  1553 	have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
```
```  1554 	have f: "finite ?Pnkn" "finite ?Pnknn"
```
```  1555 	  using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
```
```  1556 	  by (metis natpermute_finite)+
```
```  1557 	let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r \$ v ! j"
```
```  1558 	let ?g = "\<lambda>v. \<Prod>j\<in>{0..k}. a \$ v ! j"
```
```  1559 	have "setsum ?g ?Pnkn = setsum (\<lambda>v. a \$ n * (?r\$0)^k) ?Pnkn"
```
```  1560 	proof(rule setsum_cong2)
```
```  1561 	  fix v assume v: "v \<in> {xs \<in> natpermute n (Suc k). n \<in> set xs}"
```
```  1562 	  let ?ths = "(\<Prod>j\<in>{0..k}. a \$ v ! j) = a \$ n * (?r\$0)^k"
```
```  1563 	  from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
```
```  1564 	    unfolding Suc_plus1 natpermute_contain_maximal by (auto simp del: replicate.simps)
```
```  1565 	  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))"
```
```  1566 	    apply (rule setprod_cong, simp)
```
```  1567 	    using i a0 by (simp del: replicate.simps)
```
```  1568 	  also have "\<dots> = a \$ n * (?r \$ 0)^k"
```
```  1569 	    unfolding  setprod_gen_delta[OF fK] using i by simp
```
```  1570 	  finally show ?ths .
```
```  1571 	qed
```
```  1572 	then have th0: "setsum ?g ?Pnkn = of_nat (k+1) * a \$ n * (?r \$ 0)^k"
```
```  1573 	  by (simp add: natpermute_max_card[OF nz, simplified])
```
```  1574 	have th1: "setsum ?g ?Pnknn = setsum ?f ?Pnknn"
```
```  1575 	proof (rule setsum_cong2, rule setprod_cong, simp)
```
```  1576 	  fix xs i assume xs: "xs \<in> ?Pnknn" and i: "i \<in> {0..k}"
```
```  1577 	  {assume c: "n \<le> xs ! i"
```
```  1578 	    from xs i have "xs !i \<noteq> n" by (auto simp add: in_set_conv_nth natpermute_def)
```
```  1579 	    with c have c': "n < xs!i" by arith
```
```  1580 	    have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}" by simp_all
```
```  1581 	    have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}" by auto
```
```  1582 	    have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})" using i by auto
```
```  1583 	    from xs have "n = foldl op + 0 xs" by (simp add: natpermute_def)
```
```  1584 	    also have "\<dots> = setsum (nth xs) {0..<Suc k}" unfolding foldl_add_setsum using xs
```
```  1585 	      by (simp add: natpermute_def)
```
```  1586 	    also have "\<dots> = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
```
```  1587 	      unfolding eqs  setsum_Un_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
```
```  1588 	      unfolding setsum_Un_disjoint[OF fths(2) fths(3) d(2)]
```
```  1589 	      by simp
```
```  1590 	    finally have False using c' by simp}
```
```  1591 	  then have thn: "xs!i < n" by arith
```
```  1592 	  from h[rule_format, OF thn]
```
```  1593 	  show "a\$(xs !i) = ?r\$(xs!i)" .
```
```  1594 	qed
```
```  1595 	have th00: "\<And>(x::'a). of_nat (Suc k) * (x * inverse (of_nat (Suc k))) = x"
```
```  1596 	  by (simp add: field_simps del: of_nat_Suc)
```
```  1597 	from H have "b\$n = a^Suc k \$ n" by (simp add: fps_eq_iff)
```
```  1598 	also have "a ^ Suc k\$n = setsum ?g ?Pnkn + setsum ?g ?Pnknn"
```
```  1599 	  unfolding fps_power_nth_Suc
```
```  1600 	  using setsum_Un_disjoint[OF f d, unfolded Suc_plus1[symmetric],
```
```  1601 	    unfolded eq, of ?g] by simp
```
```  1602 	also have "\<dots> = of_nat (k+1) * a \$ n * (?r \$ 0)^k + setsum ?f ?Pnknn" unfolding th0 th1 ..
```
```  1603 	finally have "of_nat (k+1) * a \$ n * (?r \$ 0)^k = b\$n - setsum ?f ?Pnknn" by simp
```
```  1604 	then have "a\$n = (b\$n - setsum ?f ?Pnknn) / (of_nat (k+1) * (?r \$ 0)^k)"
```
```  1605 	  apply -
```
```  1606 	  apply (rule eq_divide_imp')
```
```  1607 	  using r00
```
```  1608 	  apply (simp del: of_nat_Suc)
```
```  1609 	  by (simp add: mult_ac)
```
```  1610 	then have "a\$n = ?r \$n"
```
```  1611 	  apply (simp del: of_nat_Suc)
```
```  1612 	  unfolding fps_radical_def n1
```
```  1613 	  by (simp add: field_simps n1 th00 del: of_nat_Suc)}
```
```  1614 	ultimately show "a\$n = ?r \$ n" by (cases n, auto)
```
```  1615       qed}
```
```  1616     then have "a = ?r" by (simp add: fps_eq_iff)}
```
```  1617   ultimately show ?thesis by blast
```
```  1618 qed
```
```  1619
```
```  1620
```
```  1621 lemma radical_power:
```
```  1622   assumes r0: "r (Suc k) ((a\$0) ^ Suc k) = a\$0"
```
```  1623   and a0: "(a\$0 ::'a::{field, ring_char_0, recpower}) \<noteq> 0"
```
```  1624   shows "(fps_radical r (Suc k) (a ^ Suc k)) = a"
```
```  1625 proof-
```
```  1626   let ?ak = "a^ Suc k"
```
```  1627   have ak0: "?ak \$ 0 = (a\$0) ^ Suc k" by (simp add: fps_nth_power_0)
```
```  1628   from r0 have th0: "r (Suc k) (a ^ Suc k \$ 0) ^ Suc k = a ^ Suc k \$ 0" using ak0 by auto
```
```  1629   from r0 ak0 have th1: "r (Suc k) (a ^ Suc k \$ 0) = a \$ 0" by auto
```
```  1630   from ak0 a0 have ak00: "?ak \$ 0 \<noteq>0 " by auto
```
```  1631   from radical_unique[of r k ?ak a, OF th0 th1 ak00] show ?thesis by metis
```
```  1632 qed
```
```  1633
```
```  1634 lemma fps_deriv_radical:
```
```  1635   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
```
```  1636   assumes r0: "(r (Suc k) (a\$0)) ^ Suc k = a\$0" and a0: "a\$0 \<noteq> 0"
```
```  1637   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)"
```
```  1638 proof-
```
```  1639   let ?r= "fps_radical r (Suc k) a"
```
```  1640   let ?w = "(fps_const (of_nat (Suc k)) * ?r ^ k)"
```
```  1641   from a0 r0 have r0': "r (Suc k) (a\$0) \<noteq> 0" by auto
```
```  1642   from r0' have w0: "?w \$ 0 \<noteq> 0" by (simp del: of_nat_Suc)
```
```  1643   note th0 = inverse_mult_eq_1[OF w0]
```
```  1644   let ?iw = "inverse ?w"
```
```  1645   from power_radical[of r, OF r0 a0]
```
```  1646   have "fps_deriv (?r ^ Suc k) = fps_deriv a" by simp
```
```  1647   hence "fps_deriv ?r * ?w = fps_deriv a"
```
```  1648     by (simp add: fps_deriv_power mult_ac)
```
```  1649   hence "?iw * fps_deriv ?r * ?w = ?iw * fps_deriv a" by simp
```
```  1650   hence "fps_deriv ?r * (?iw * ?w) = fps_deriv a / ?w"
```
```  1651     by (simp add: fps_divide_def)
```
```  1652   then show ?thesis unfolding th0 by simp
```
```  1653 qed
```
```  1654
```
```  1655 lemma radical_mult_distrib:
```
```  1656   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
```
```  1657   assumes
```
```  1658   ra0: "r (k) (a \$ 0) ^ k = a \$ 0"
```
```  1659   and rb0: "r (k) (b \$ 0) ^ k = b \$ 0"
```
```  1660   and r0': "r (k) ((a * b) \$ 0) = r (k) (a \$ 0) * r (k) (b \$ 0)"
```
```  1661   and a0: "a\$0 \<noteq> 0"
```
```  1662   and b0: "b\$0 \<noteq> 0"
```
```  1663   shows "fps_radical r (k) (a*b) = fps_radical r (k) a * fps_radical r (k) (b)"
```
```  1664 proof-
```
```  1665   from r0' have r0: "(r (k) ((a*b)\$0)) ^ k = (a*b)\$0"
```
```  1666     by (simp add: fps_mult_nth ra0 rb0 power_mult_distrib)
```
```  1667   {assume "k=0" hence ?thesis by simp}
```
```  1668   moreover
```
```  1669   {fix h assume k: "k = Suc h"
```
```  1670   let ?ra = "fps_radical r (Suc h) a"
```
```  1671   let ?rb = "fps_radical r (Suc h) b"
```
```  1672   have th0: "r (Suc h) ((a * b) \$ 0) = (fps_radical r (Suc h) a * fps_radical r (Suc h) b) \$ 0"
```
```  1673     using r0' k by (simp add: fps_mult_nth)
```
```  1674   have ab0: "(a*b) \$ 0 \<noteq> 0" using a0 b0 by (simp add: fps_mult_nth)
```
```  1675   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]
```
```  1676     power_radical[of r, OF ra0[unfolded k] a0] power_radical[of r, OF rb0[unfolded k] b0] k
```
```  1677   have ?thesis by (auto simp add: power_mult_distrib)}
```
```  1678 ultimately show ?thesis by (cases k, auto)
```
```  1679 qed
```
```  1680
```
```  1681 lemma radical_inverse:
```
```  1682   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
```
```  1683   assumes
```
```  1684   ra0: "r (k) (a \$ 0) ^ k = a \$ 0"
```
```  1685   and ria0: "r (k) (inverse (a \$ 0)) = inverse (r (k) (a \$ 0))"
```
```  1686   and r1: "(r (k) 1) = 1"
```
```  1687   and a0: "a\$0 \<noteq> 0"
```
```  1688   shows "fps_radical r (k) (inverse a) = inverse (fps_radical r (k) a)"
```
```  1689 proof-
```
```  1690   {assume "k=0" then have ?thesis by simp}
```
```  1691   moreover
```
```  1692   {fix h assume k[simp]: "k = Suc h"
```
```  1693     let ?ra = "fps_radical r (Suc h) a"
```
```  1694     let ?ria = "fps_radical r (Suc h) (inverse a)"
```
```  1695     from ra0 a0 have th00: "r (Suc h) (a\$0) \<noteq> 0" by auto
```
```  1696     have ria0': "r (Suc h) (inverse a \$ 0) ^ Suc h = inverse a\$0"
```
```  1697     using ria0 ra0 a0
```
```  1698     by (simp add: fps_inverse_def  nonzero_power_inverse[OF th00, symmetric])
```
```  1699   from inverse_mult_eq_1[OF a0] have th0: "a * inverse a = 1"
```
```  1700     by (simp add: mult_commute)
```
```  1701   from radical_unique[where a=1 and b=1 and r=r and k=h, simplified, OF r1[unfolded k]]
```
```  1702   have th01: "fps_radical r (Suc h) 1 = 1" .
```
```  1703   have th1: "r (Suc h) ((a * inverse a) \$ 0) ^ Suc h = (a * inverse a) \$ 0"
```
```  1704     "r (Suc h) ((a * inverse a) \$ 0) =
```
```  1705 r (Suc h) (a \$ 0) * r (Suc h) (inverse a \$ 0)"
```
```  1706     using r1 unfolding th0  apply (simp_all add: ria0[symmetric])
```
```  1707     apply (simp add: fps_inverse_def a0)
```
```  1708     unfolding ria0[unfolded k]
```
```  1709     using th00 by simp
```
```  1710   from nonzero_imp_inverse_nonzero[OF a0] a0
```
```  1711   have th2: "inverse a \$ 0 \<noteq> 0" by (simp add: fps_inverse_def)
```
```  1712   from radical_mult_distrib[of r "Suc h" a "inverse a", OF ra0[unfolded k] ria0' th1(2) a0 th2]
```
```  1713   have th3: "?ra * ?ria = 1" unfolding th0 th01 by simp
```
```  1714   from th00 have ra0: "?ra \$ 0 \<noteq> 0" by simp
```
```  1715   from fps_inverse_unique[OF ra0 th3] have ?thesis by simp}
```
```  1716 ultimately show ?thesis by (cases k, auto)
```
```  1717 qed
```
```  1718
```
```  1719 lemma fps_divide_inverse: "(a::('a::field) fps) / b = a * inverse b"
```
```  1720   by (simp add: fps_divide_def)
```
```  1721
```
```  1722 lemma radical_divide:
```
```  1723   fixes a:: "'a ::{field, ring_char_0, recpower} fps"
```
```  1724   assumes
```
```  1725       ra0: "r k (a \$ 0) ^ k = a \$ 0"
```
```  1726   and rb0: "r k (b \$ 0) ^ k = b \$ 0"
```
```  1727   and r1: "r k 1 = 1"
```
```  1728   and rb0': "r k (inverse (b \$ 0)) = inverse (r k (b \$ 0))"
```
```  1729   and raib': "r k (a\$0 / (b\$0)) = r k (a\$0) / r k (b\$0)"
```
```  1730   and a0: "a\$0 \<noteq> 0"
```
```  1731   and b0: "b\$0 \<noteq> 0"
```
```  1732   shows "fps_radical r k (a/b) = fps_radical r k a / fps_radical r k b"
```
```  1733 proof-
```
```  1734   from raib'
```
```  1735   have raib: "r k (a\$0 / (b\$0)) = r k (a\$0) * r k (inverse (b\$0))"
```
```  1736     by (simp add: divide_inverse rb0'[symmetric])
```
```  1737
```
```  1738   {assume "k=0" hence ?thesis by (simp add: fps_divide_def)}
```
```  1739   moreover
```
```  1740   {assume k0: "k\<noteq> 0"
```
```  1741     from b0 k0 rb0 have rbn0: "r k (b \$0) \<noteq> 0"
```
```  1742       by (auto simp add: power_0_left)
```
```  1743
```
```  1744     from rb0 rb0' have rib0: "(r k (inverse (b \$ 0)))^k = inverse (b\$0)"
```
```  1745     by (simp add: nonzero_power_inverse[OF rbn0, symmetric])
```
```  1746   from rib0 have th0: "r k (inverse b \$ 0) ^ k = inverse b \$ 0"
```
```  1747     by (simp add:fps_inverse_def b0)
```
```  1748   from raib
```
```  1749   have th1: "r k ((a * inverse b) \$ 0) = r k (a \$ 0) * r k (inverse b \$ 0)"
```
```  1750     by (simp add: divide_inverse fps_inverse_def  b0 fps_mult_nth)
```
```  1751   from nonzero_imp_inverse_nonzero[OF b0] b0 have th2: "inverse b \$ 0 \<noteq> 0"
```
```  1752     by (simp add: fps_inverse_def)
```
```  1753   from radical_mult_distrib[of r k a "inverse b", OF ra0 th0 th1 a0 th2]
```
```  1754   have th: "fps_radical r k (a/b) = fps_radical r k a * fps_radical r k (inverse b)"
```
```  1755     by (simp add: fps_divide_def)
```
```  1756   with radical_inverse[of r k b, OF rb0 rb0' r1 b0]
```
```  1757   have ?thesis by (simp add: fps_divide_def)}
```
```  1758 ultimately show ?thesis by blast
```
```  1759 qed
```
```  1760
```
```  1761 subsection{* Derivative of composition *}
```
```  1762
```
```  1763 lemma fps_compose_deriv:
```
```  1764   fixes a:: "('a::idom) fps"
```
```  1765   assumes b0: "b\$0 = 0"
```
```  1766   shows "fps_deriv (a oo b) = ((fps_deriv a) oo b) * (fps_deriv b)"
```
```  1767 proof-
```
```  1768   {fix n
```
```  1769     have "(fps_deriv (a oo b))\$n = setsum (\<lambda>i. a \$ i * (fps_deriv (b^i))\$n) {0.. Suc n}"
```
```  1770       by (simp add: fps_compose_def ring_simps setsum_right_distrib del: of_nat_Suc)
```
```  1771     also have "\<dots> = setsum (\<lambda>i. a\$i * ((fps_const (of_nat i)) * (fps_deriv b * (b^(i - 1))))\$n) {0.. Suc n}"
```
```  1772       by (simp add: ring_simps fps_deriv_power del: fps_mult_left_const_nth of_nat_Suc)
```
```  1773   also have "\<dots> = setsum (\<lambda>i. of_nat i * a\$i * (((b^(i - 1)) * fps_deriv b))\$n) {0.. Suc n}"
```
```  1774     unfolding fps_mult_left_const_nth  by (simp add: ring_simps)
```
```  1775   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}"
```
```  1776     unfolding fps_mult_nth ..
```
```  1777   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}"
```
```  1778     apply (rule setsum_mono_zero_right)
```
```  1779     apply (auto simp add: mult_delta_left setsum_delta not_le)
```
```  1780     done
```
```  1781   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}"
```
```  1782     unfolding fps_deriv_nth
```
```  1783     apply (rule setsum_reindex_cong[where f="Suc"])
```
```  1784     by (auto simp add: mult_assoc)
```
```  1785   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}" .
```
```  1786
```
```  1787   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}"
```
```  1788     unfolding fps_mult_nth by (simp add: mult_ac)
```
```  1789   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}"
```
```  1790     unfolding fps_deriv_nth fps_compose_nth setsum_right_distrib mult_assoc
```
```  1791     apply (rule setsum_cong2)
```
```  1792     apply (rule setsum_mono_zero_left)
```
```  1793     apply (simp_all add: subset_eq)
```
```  1794     apply clarify
```
```  1795     apply (subgoal_tac "b^i\$x = 0")
```
```  1796     apply simp
```
```  1797     apply (rule startsby_zero_power_prefix[OF b0, rule_format])
```
```  1798     by simp
```
```  1799   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}"
```
```  1800     unfolding setsum_right_distrib
```
```  1801     apply (subst setsum_commute)
```
```  1802     by ((rule setsum_cong2)+) simp
```
```  1803   finally have "(fps_deriv (a oo b))\$n = (((fps_deriv a) oo b) * (fps_deriv b)) \$n"
```
```  1804     unfolding th0 by simp}
```
```  1805 then show ?thesis by (simp add: fps_eq_iff)
```
```  1806 qed
```
```  1807
```
```  1808 lemma fps_mult_X_plus_1_nth:
```
```  1809   "((1+X)*a) \$n = (if n = 0 then (a\$n :: 'a::comm_ring_1) else a\$n + a\$(n - 1))"
```
```  1810 proof-
```
```  1811   {assume "n = 0" hence ?thesis by (simp add: fps_mult_nth )}
```
```  1812   moreover
```
```  1813   {fix m assume m: "n = Suc m"
```
```  1814     have "((1+X)*a) \$n = setsum (\<lambda>i. (1+X)\$i * a\$(n-i)) {0..n}"
```
```  1815       by (simp add: fps_mult_nth)
```
```  1816     also have "\<dots> = setsum (\<lambda>i. (1+X)\$i * a\$(n-i)) {0.. 1}"
```
```  1817       unfolding m
```
```  1818       apply (rule setsum_mono_zero_right)
```
```  1819       by (auto simp add: )
```
```  1820     also have "\<dots> = (if n = 0 then (a\$n :: 'a::comm_ring_1) else a\$n + a\$(n - 1))"
```
```  1821       unfolding m
```
```  1822       by (simp add: )
```
```  1823     finally have ?thesis .}
```
```  1824   ultimately show ?thesis by (cases n, auto)
```
```  1825 qed
```
```  1826
```
```  1827 subsection{* Finite FPS (i.e. polynomials) and X *}
```
```  1828 lemma fps_poly_sum_X:
```
```  1829   assumes z: "\<forall>i > n. a\$i = (0::'a::comm_ring_1)"
```
```  1830   shows "a = setsum (\<lambda>i. fps_const (a\$i) * X^i) {0..n}" (is "a = ?r")
```
```  1831 proof-
```
```  1832   {fix i
```
```  1833     have "a\$i = ?r\$i"
```
```  1834       unfolding fps_setsum_nth fps_mult_left_const_nth X_power_nth
```
```  1835       by (simp add: mult_delta_right setsum_delta' z)
```
```  1836   }
```
```  1837   then show ?thesis unfolding fps_eq_iff by blast
```
```  1838 qed
```
```  1839
```
```  1840 subsection{* Compositional inverses *}
```
```  1841
```
```  1842
```
```  1843 fun compinv :: "'a fps \<Rightarrow> nat \<Rightarrow> 'a::{recpower,field}" where
```
```  1844   "compinv a 0 = X\$0"
```
```  1845 | "compinv a (Suc n) = (X\$ Suc n - setsum (\<lambda>i. (compinv a i) * (a^i)\$Suc n) {0 .. n}) / (a\$1) ^ Suc n"
```
```  1846
```
```  1847 definition "fps_inv a = Abs_fps (compinv a)"
```
```  1848
```
```  1849 lemma fps_inv: assumes a0: "a\$0 = 0" and a1: "a\$1 \<noteq> 0"
```
```  1850   shows "fps_inv a oo a = X"
```
```  1851 proof-
```
```  1852   let ?i = "fps_inv a oo a"
```
```  1853   {fix n
```
```  1854     have "?i \$n = X\$n"
```
```  1855     proof(induct n rule: nat_less_induct)
```
```  1856       fix n assume h: "\<forall>m<n. ?i\$m = X\$m"
```
```  1857       {assume "n=0" hence "?i \$n = X\$n" using a0
```
```  1858 	  by (simp add: fps_compose_nth fps_inv_def)}
```
```  1859       moreover
```
```  1860       {fix n1 assume n1: "n = Suc n1"
```
```  1861 	have "?i \$ n = setsum (\<lambda>i. (fps_inv a \$ i) * (a^i)\$n) {0 .. n1} + fps_inv a \$ Suc n1 * (a \$ 1)^ Suc n1"
```
```  1862 	  by (simp add: fps_compose_nth n1 startsby_zero_power_nth_same[OF a0])
```
```  1863 	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})"
```
```  1864 	  using a0 a1 n1 by (simp add: fps_inv_def)
```
```  1865 	also have "\<dots> = X\$n" using n1 by simp
```
```  1866 	finally have "?i \$ n = X\$n" .}
```
```  1867       ultimately show "?i \$ n = X\$n" by (cases n, auto)
```
```  1868     qed}
```
```  1869   then show ?thesis by (simp add: fps_eq_iff)
```
```  1870 qed
```
```  1871
```
```  1872
```
```  1873 fun gcompinv :: "'a fps \<Rightarrow> 'a fps \<Rightarrow> nat \<Rightarrow> 'a::{recpower,field}" where
```
```  1874   "gcompinv b a 0 = b\$0"
```
```  1875 | "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"
```
```  1876
```
```  1877 definition "fps_ginv b a = Abs_fps (gcompinv b a)"
```
```  1878
```
```  1879 lemma fps_ginv: assumes a0: "a\$0 = 0" and a1: "a\$1 \<noteq> 0"
```
```  1880   shows "fps_ginv b a oo a = b"
```
```  1881 proof-
```
```  1882   let ?i = "fps_ginv b a oo a"
```
```  1883   {fix n
```
```  1884     have "?i \$n = b\$n"
```
```  1885     proof(induct n rule: nat_less_induct)
```
```  1886       fix n assume h: "\<forall>m<n. ?i\$m = b\$m"
```
```  1887       {assume "n=0" hence "?i \$n = b\$n" using a0
```
```  1888 	  by (simp add: fps_compose_nth fps_ginv_def)}
```
```  1889       moreover
```
```  1890       {fix n1 assume n1: "n = Suc n1"
```
```  1891 	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"
```
```  1892 	  by (simp add: fps_compose_nth n1 startsby_zero_power_nth_same[OF a0])
```
```  1893 	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})"
```
```  1894 	  using a0 a1 n1 by (simp add: fps_ginv_def)
```
```  1895 	also have "\<dots> = b\$n" using n1 by simp
```
```  1896 	finally have "?i \$ n = b\$n" .}
```
```  1897       ultimately show "?i \$ n = b\$n" by (cases n, auto)
```
```  1898     qed}
```
```  1899   then show ?thesis by (simp add: fps_eq_iff)
```
```  1900 qed
```
```  1901
```
```  1902 lemma fps_inv_ginv: "fps_inv = fps_ginv X"
```
```  1903   apply (auto simp add: expand_fun_eq fps_eq_iff fps_inv_def fps_ginv_def)
```
```  1904   apply (induct_tac n rule: nat_less_induct, auto)
```
```  1905   apply (case_tac na)
```
```  1906   apply simp
```
```  1907   apply simp
```
```  1908   done
```
```  1909
```
```  1910 lemma fps_compose_1[simp]: "1 oo a = 1"
```
```  1911   by (simp add: fps_eq_iff fps_compose_nth fps_power_def mult_delta_left setsum_delta)
```
```  1912
```
```  1913 lemma fps_compose_0[simp]: "0 oo a = 0"
```
```  1914   by (simp add: fps_eq_iff fps_compose_nth)
```
```  1915
```
```  1916 lemma fps_pow_0: "fps_pow n 0 = (if n = 0 then 1 else 0)"
```
```  1917   by (induct n, simp_all)
```
```  1918
```
```  1919 lemma fps_compose_0_right[simp]: "a oo 0 = fps_const (a\$0)"
```
```  1920   by (auto simp add: fps_eq_iff fps_compose_nth fps_power_def fps_pow_0 setsum_0')
```
```  1921
```
```  1922 lemma fps_compose_add_distrib: "(a + b) oo c = (a oo c) + (b oo c)"
```
```  1923   by (simp add: fps_eq_iff fps_compose_nth  ring_simps setsum_addf)
```
```  1924
```
```  1925 lemma fps_compose_setsum_distrib: "(setsum f S) oo a = setsum (\<lambda>i. f i oo a) S"
```
```  1926 proof-
```
```  1927   {assume "\<not> finite S" hence ?thesis by simp}
```
```  1928   moreover
```
```  1929   {assume fS: "finite S"
```
```  1930     have ?thesis
```
```  1931     proof(rule finite_induct[OF fS])
```
```  1932       show "setsum f {} oo a = (\<Sum>i\<in>{}. f i oo a)" by simp
```
```  1933     next
```
```  1934       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"
```
```  1935       show "setsum f (insert x F) oo a  = setsum (\<lambda>i. f i oo a) (insert x F)"
```
```  1936 	using fF xF h by (simp add: fps_compose_add_distrib)
```
```  1937     qed}
```
```  1938   ultimately show ?thesis by blast
```
```  1939 qed
```
```  1940
```
```  1941 lemma convolution_eq:
```
```  1942   "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}"
```
```  1943   apply (rule setsum_reindex_cong[where f=fst])
```
```  1944   apply (clarsimp simp add: inj_on_def)
```
```  1945   apply (auto simp add: expand_set_eq image_iff)
```
```  1946   apply (rule_tac x= "x" in exI)
```
```  1947   apply clarsimp
```
```  1948   apply (rule_tac x="n - x" in exI)
```
```  1949   apply arith
```
```  1950   done
```
```  1951
```
```  1952 lemma product_composition_lemma:
```
```  1953   assumes c0: "c\$0 = (0::'a::idom)" and d0: "d\$0 = 0"
```
```  1954   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")
```
```  1955 proof-
```
```  1956   let ?S = "{(k\<Colon>nat, m\<Colon>nat). k + m \<le> n}"
```
```  1957   have s: "?S \<subseteq> {0..n} <*> {0..n}" by (auto simp add: subset_eq)
```
```  1958   have f: "finite {(k\<Colon>nat, m\<Colon>nat). k + m \<le> n}"
```
```  1959     apply (rule finite_subset[OF s])
```
```  1960     by auto
```
```  1961   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}"
```
```  1962     apply (simp add: fps_mult_nth setsum_right_distrib)
```
```  1963     apply (subst setsum_commute)
```
```  1964     apply (rule setsum_cong2)
```
```  1965     by (auto simp add: ring_simps)
```
```  1966   also have "\<dots> = ?l"
```
```  1967     apply (simp add: fps_mult_nth fps_compose_nth setsum_product)
```
```  1968     apply (rule setsum_cong2)
```
```  1969     apply (simp add: setsum_cartesian_product mult_assoc)
```
```  1970     apply (rule setsum_mono_zero_right[OF f])
```
```  1971     apply (simp add: subset_eq) apply presburger
```
```  1972     apply clarsimp
```
```  1973     apply (rule ccontr)
```
```  1974     apply (clarsimp simp add: not_le)
```
```  1975     apply (case_tac "x < aa")
```
```  1976     apply simp
```
```  1977     apply (frule_tac startsby_zero_power_prefix[rule_format, OF c0])
```
```  1978     apply blast
```
```  1979     apply simp
```
```  1980     apply (frule_tac startsby_zero_power_prefix[rule_format, OF d0])
```
```  1981     apply blast
```
```  1982     done
```
```  1983   finally show ?thesis by simp
```
```  1984 qed
```
```  1985
```
```  1986 lemma product_composition_lemma':
```
```  1987   assumes c0: "c\$0 = (0::'a::idom)" and d0: "d\$0 = 0"
```
```  1988   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")
```
```  1989   unfolding product_composition_lemma[OF c0 d0]
```
```  1990   unfolding setsum_cartesian_product
```
```  1991   apply (rule setsum_mono_zero_left)
```
```  1992   apply simp
```
```  1993   apply (clarsimp simp add: subset_eq)
```
```  1994   apply clarsimp
```
```  1995   apply (rule ccontr)
```
```  1996   apply (subgoal_tac "(c^aa * d^ba) \$ n = 0")
```
```  1997   apply simp
```
```  1998   unfolding fps_mult_nth
```
```  1999   apply (rule setsum_0')
```
```  2000   apply (clarsimp simp add: not_le)
```
```  2001   apply (case_tac "aaa < aa")
```
```  2002   apply (rule startsby_zero_power_prefix[OF c0, rule_format])
```
```  2003   apply simp
```
```  2004   apply (subgoal_tac "n - aaa < ba")
```
```  2005   apply (frule_tac k = "ba" in startsby_zero_power_prefix[OF d0, rule_format])
```
```  2006   apply simp
```
```  2007   apply arith
```
```  2008   done
```
```  2009
```
```  2010
```
```  2011 lemma setsum_pair_less_iff:
```
```  2012   "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")
```
```  2013 proof-
```
```  2014   let ?KM=  "{(k,m). k + m \<le> n}"
```
```  2015   let ?f = "%s. UNION {(0::nat)..s} (%i. {(i,s - i)})"
```
```  2016   have th0: "?KM = UNION {0..n} ?f"
```
```  2017     apply (simp add: expand_set_eq)
```
```  2018     apply arith (* FIXME: VERY slow! *)
```
```  2019     done
```
```  2020   show "?l = ?r "
```
```  2021     unfolding th0
```
```  2022     apply (subst setsum_UN_disjoint)
```
```  2023     apply auto
```
```  2024     apply (subst setsum_UN_disjoint)
```
```  2025     apply auto
```
```  2026     done
```
```  2027 qed
```
```  2028
```
```  2029 lemma fps_compose_mult_distrib_lemma:
```
```  2030   assumes c0: "c\$0 = (0::'a::idom)"
```
```  2031   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")
```
```  2032   unfolding product_composition_lemma[OF c0 c0] power_add[symmetric]
```
```  2033   unfolding setsum_pair_less_iff[where a = "%k. a\$k" and b="%m. b\$m" and c="%s. (c ^ s)\$n" and n = n] ..
```
```  2034
```
```  2035
```
```  2036 lemma fps_compose_mult_distrib:
```
```  2037   assumes c0: "c\$0 = (0::'a::idom)"
```
```  2038   shows "(a * b) oo c = (a oo c) * (b oo c)" (is "?l = ?r")
```
```  2039   apply (simp add: fps_eq_iff fps_compose_mult_distrib_lemma[OF c0])
```
```  2040   by (simp add: fps_compose_nth fps_mult_nth setsum_left_distrib)
```
```  2041 lemma fps_compose_setprod_distrib:
```
```  2042   assumes c0: "c\$0 = (0::'a::idom)"
```
```  2043   shows "(setprod a S) oo c = setprod (%k. a k oo c) S" (is "?l = ?r")
```
```  2044   apply (cases "finite S")
```
```  2045   apply simp_all
```
```  2046   apply (induct S rule: finite_induct)
```
```  2047   apply simp
```
```  2048   apply (simp add: fps_compose_mult_distrib[OF c0])
```
```  2049   done
```
```  2050
```
```  2051 lemma fps_compose_power:   assumes c0: "c\$0 = (0::'a::idom)"
```
```  2052   shows "(a oo c)^n = a^n oo c" (is "?l = ?r")
```
```  2053 proof-
```
```  2054   {assume "n=0" then have ?thesis by simp}
```
```  2055   moreover
```
```  2056   {fix m assume m: "n = Suc m"
```
```  2057     have th0: "a^n = setprod (%k. a) {0..m}" "(a oo c) ^ n = setprod (%k. a oo c) {0..m}"
```
```  2058       by (simp_all add: setprod_constant m)
```
```  2059     then have ?thesis
```
```  2060       by (simp add: fps_compose_setprod_distrib[OF c0])}
```
```  2061   ultimately show ?thesis by (cases n, auto)
```
```  2062 qed
```
```  2063
```
```  2064 lemma fps_const_mult_apply_left:
```
```  2065   "fps_const c * (a oo b) = (fps_const c * a) oo b"
```
```  2066   by (simp add: fps_eq_iff fps_compose_nth setsum_right_distrib mult_assoc)
```
```  2067
```
```  2068 lemma fps_const_mult_apply_right:
```
```  2069   "(a oo b) * fps_const (c::'a::comm_semiring_1) = (fps_const c * a) oo b"
```
```  2070   by (auto simp add: fps_const_mult_apply_left mult_commute)
```
```  2071
```
```  2072 lemma fps_compose_assoc:
```
```  2073   assumes c0: "c\$0 = (0::'a::idom)" and b0: "b\$0 = 0"
```
```  2074   shows "a oo (b oo c) = a oo b oo c" (is "?l = ?r")
```
```  2075 proof-
```
```  2076   {fix n
```
```  2077     have "?l\$n = (setsum (\<lambda>i. (fps_const (a\$i) * b^i) oo c) {0..n})\$n"
```
```  2078       by (simp add: fps_compose_nth fps_compose_power[OF c0] fps_const_mult_apply_left setsum_right_distrib mult_assoc fps_setsum_nth)
```
```  2079     also have "\<dots> = ((setsum (\<lambda>i. fps_const (a\$i) * b^i) {0..n}) oo c)\$n"
```
```  2080       by (simp add: fps_compose_setsum_distrib)
```
```  2081     also have "\<dots> = ?r\$n"
```
```  2082       apply (simp add: fps_compose_nth fps_setsum_nth setsum_left_distrib mult_assoc)
```
```  2083       apply (rule setsum_cong2)
```
```  2084       apply (rule setsum_mono_zero_right)
```
```  2085       apply (auto simp add: not_le)
```
```  2086       by (erule startsby_zero_power_prefix[OF b0, rule_format])
```
```  2087     finally have "?l\$n = ?r\$n" .}
```
```  2088   then show ?thesis by (simp add: fps_eq_iff)
```
```  2089 qed
```
```  2090
```
```  2091
```
```  2092 lemma fps_X_power_compose:
```
```  2093   assumes a0: "a\$0=0" shows "X^k oo a = (a::('a::idom fps))^k" (is "?l = ?r")
```
```  2094 proof-
```
```  2095   {assume "k=0" hence ?thesis by simp}
```
```  2096   moreover
```
```  2097   {fix h assume h: "k = Suc h"
```
```  2098     {fix n
```
```  2099       {assume kn: "k>n" hence "?l \$ n = ?r \$n" using a0 startsby_zero_power_prefix[OF a0] h
```
```  2100 	  by (simp add: fps_compose_nth)}
```
```  2101       moreover
```
```  2102       {assume kn: "k \<le> n"
```
```  2103 	hence "?l\$n = ?r\$n"
```
```  2104           by (simp add: fps_compose_nth mult_delta_left setsum_delta)}
```
```  2105       moreover have "k >n \<or> k\<le> n"  by arith
```
```  2106       ultimately have "?l\$n = ?r\$n"  by blast}
```
```  2107     then have ?thesis unfolding fps_eq_iff by blast}
```
```  2108   ultimately show ?thesis by (cases k, auto)
```
```  2109 qed
```
```  2110
```
```  2111 lemma fps_inv_right: assumes a0: "a\$0 = 0" and a1: "a\$1 \<noteq> 0"
```
```  2112   shows "a oo fps_inv a = X"
```
```  2113 proof-
```
```  2114   let ?ia = "fps_inv a"
```
```  2115   let ?iaa = "a oo fps_inv a"
```
```  2116   have th0: "?ia \$ 0 = 0" by (simp add: fps_inv_def)
```
```  2117   have th1: "?iaa \$ 0 = 0" using a0 a1
```
```  2118     by (simp add: fps_inv_def fps_compose_nth)
```
```  2119   have th2: "X\$0 = 0" by simp
```
```  2120   from fps_inv[OF a0 a1] have "a oo (fps_inv a oo a) = a oo X" by simp
```
```  2121   then have "(a oo fps_inv a) oo a = X oo a"
```
```  2122     by (simp add: fps_compose_assoc[OF a0 th0] X_fps_compose_startby0[OF a0])
```
```  2123   with fps_compose_inj_right[OF a0 a1]
```
```  2124   show ?thesis by simp
```
```  2125 qed
```
```  2126
```
```  2127 lemma fps_inv_deriv:
```
```  2128   assumes a0:"a\$0 = (0::'a::{recpower,field})" and a1: "a\$1 \<noteq> 0"
```
```  2129   shows "fps_deriv (fps_inv a) = inverse (fps_deriv a oo fps_inv a)"
```
```  2130 proof-
```
```  2131   let ?ia = "fps_inv a"
```
```  2132   let ?d = "fps_deriv a oo ?ia"
```
```  2133   let ?dia = "fps_deriv ?ia"
```
```  2134   have ia0: "?ia\$0 = 0" by (simp add: fps_inv_def)
```
```  2135   have th0: "?d\$0 \<noteq> 0" using a1 by (simp add: fps_compose_nth fps_deriv_nth)
```
```  2136   from fps_inv_right[OF a0 a1] have "?d * ?dia = 1"
```
```  2137     by (simp add: fps_compose_deriv[OF ia0, of a, symmetric] )
```
```  2138   hence "inverse ?d * ?d * ?dia = inverse ?d * 1" by simp
```
```  2139   with inverse_mult_eq_1[OF th0]
```
```  2140   show "?dia = inverse ?d" by simp
```
```  2141 qed
```
```  2142
```
```  2143 subsection{* Elementary series *}
```
```  2144
```
```  2145 subsubsection{* Exponential series *}
```
```  2146 definition "E x = Abs_fps (\<lambda>n. x^n / of_nat (fact n))"
```
```  2147
```
```  2148 lemma E_deriv[simp]: "fps_deriv (E a) = fps_const (a::'a::{field, recpower, ring_char_0}) * E a" (is "?l = ?r")
```
```  2149 proof-
```
```  2150   {fix n
```
```  2151     have "?l\$n = ?r \$ n"
```
```  2152   apply (auto simp add: E_def field_simps power_Suc[symmetric]simp del: fact_Suc of_nat_Suc)
```
```  2153   by (simp add: of_nat_mult ring_simps)}
```
```  2154 then show ?thesis by (simp add: fps_eq_iff)
```
```  2155 qed
```
```  2156
```
```  2157 lemma E_unique_ODE:
```
```  2158   "fps_deriv a = fps_const c * a \<longleftrightarrow> a = fps_const (a\$0) * E (c :: 'a::{field, ring_char_0, recpower})"
```
```  2159   (is "?lhs \<longleftrightarrow> ?rhs")
```
```  2160 proof-
```
```  2161   {assume d: ?lhs
```
```  2162   from d have th: "\<And>n. a \$ Suc n = c * a\$n / of_nat (Suc n)"
```
```  2163     by (simp add: fps_deriv_def fps_eq_iff field_simps del: of_nat_Suc)
```
```  2164   {fix n have "a\$n = a\$0 * c ^ n/ (of_nat (fact n))"
```
```  2165       apply (induct n)
```
```  2166       apply simp
```
```  2167       unfolding th
```
```  2168       using fact_gt_zero
```
```  2169       apply (simp add: field_simps del: of_nat_Suc fact.simps)
```
```  2170       apply (drule sym)
```
```  2171       by (simp add: ring_simps of_nat_mult power_Suc)}
```
```  2172   note th' = this
```
```  2173   have ?rhs
```
```  2174     by (auto simp add: fps_eq_iff fps_const_mult_left E_def intro : th')}
```
```  2175 moreover
```
```  2176 {assume h: ?rhs
```
```  2177   have ?lhs
```
```  2178     apply (subst h)
```
```  2179     apply simp
```
```  2180     apply (simp only: h[symmetric])
```
```  2181   by simp}
```
```  2182 ultimately show ?thesis by blast
```
```  2183 qed
```
```  2184
```
```  2185 lemma E_add_mult: "E (a + b) = E (a::'a::{ring_char_0, field, recpower}) * E b" (is "?l = ?r")
```
```  2186 proof-
```
```  2187   have "fps_deriv (?r) = fps_const (a+b) * ?r"
```
```  2188     by (simp add: fps_const_add[symmetric] ring_simps del: fps_const_add)
```
```  2189   then have "?r = ?l" apply (simp only: E_unique_ODE)
```
```  2190     by (simp add: fps_mult_nth E_def)
```
```  2191   then show ?thesis ..
```
```  2192 qed
```
```  2193
```
```  2194 lemma E_nth[simp]: "E a \$ n = a^n / of_nat (fact n)"
```
```  2195   by (simp add: E_def)
```
```  2196
```
```  2197 lemma E0[simp]: "E (0::'a::{field, recpower}) = 1"
```
```  2198   by (simp add: fps_eq_iff power_0_left)
```
```  2199
```
```  2200 lemma E_neg: "E (- a) = inverse (E (a::'a::{ring_char_0, field, recpower}))"
```
```  2201 proof-
```
```  2202   from E_add_mult[of a "- a"] have th0: "E a * E (- a) = 1"
```
```  2203     by (simp )
```
```  2204   have th1: "E a \$ 0 \<noteq> 0" by simp
```
```  2205   from fps_inverse_unique[OF th1 th0] show ?thesis by simp
```
```  2206 qed
```
```  2207
```
```  2208 lemma E_nth_deriv[simp]: "fps_nth_deriv n (E (a::'a::{field, recpower, ring_char_0})) = (fps_const a)^n * (E a)"
```
```  2209   by (induct n, auto simp add: power_Suc)
```
```  2210
```
```  2211 lemma fps_compose_uminus: "- (a::'a::ring_1 fps) oo c = - (a oo c)"
```
```  2212   by (simp add: fps_eq_iff fps_compose_nth ring_simps setsum_negf[symmetric])
```
```  2213
```
```  2214 lemma fps_compose_sub_distrib:
```
```  2215   shows "(a - b) oo (c::'a::ring_1 fps) = (a oo c) - (b oo c)"
```
```  2216   unfolding diff_minus fps_compose_uminus fps_compose_add_distrib ..
```
```  2217
```
```  2218 lemma X_fps_compose:"X oo a = Abs_fps (\<lambda>n. if n = 0 then (0::'a::comm_ring_1) else a\$n)"
```
```  2219   by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta power_Suc)
```
```  2220
```
```  2221 lemma X_compose_E[simp]: "X oo E (a::'a::{field, recpower}) = E a - 1"
```
```  2222   by (simp add: fps_eq_iff X_fps_compose)
```
```  2223
```
```  2224 lemma LE_compose:
```
```  2225   assumes a: "a\<noteq>0"
```
```  2226   shows "fps_inv (E a - 1) oo (E a - 1) = X"
```
```  2227   and "(E a - 1) oo fps_inv (E a - 1) = X"
```
```  2228 proof-
```
```  2229   let ?b = "E a - 1"
```
```  2230   have b0: "?b \$ 0 = 0" by simp
```
```  2231   have b1: "?b \$ 1 \<noteq> 0" by (simp add: a)
```
```  2232   from fps_inv[OF b0 b1] show "fps_inv (E a - 1) oo (E a - 1) = X" .
```
```  2233   from fps_inv_right[OF b0 b1] show "(E a - 1) oo fps_inv (E a - 1) = X" .
```
```  2234 qed
```
```  2235
```
```  2236
```
```  2237 lemma fps_const_inverse:
```
```  2238   "inverse (fps_const (a::'a::{field, division_by_zero})) = fps_const (inverse a)"
```
```  2239   apply (auto simp add: fps_eq_iff fps_inverse_def) by (case_tac "n", auto)
```
```  2240
```
```  2241
```
```  2242 lemma inverse_one_plus_X:
```
```  2243   "inverse (1 + X) = Abs_fps (\<lambda>n. (- 1 ::'a::{field, recpower})^n)"
```
```  2244   (is "inverse ?l = ?r")
```
```  2245 proof-
```
```  2246   have th: "?l * ?r = 1"
```
```  2247     apply (auto simp add: ring_simps fps_eq_iff X_mult_nth  minus_one_power_iff)
```
```  2248     apply presburger+
```
```  2249     done
```
```  2250   have th': "?l \$ 0 \<noteq> 0" by (simp add: )
```
```  2251   from fps_inverse_unique[OF th' th] show ?thesis .
```
```  2252 qed
```
```  2253
```
```  2254 lemma E_power_mult: "(E (c::'a::{field,recpower,ring_char_0}))^n = E (of_nat n * c)"
```
```  2255   by (induct n, auto simp add: ring_simps E_add_mult power_Suc)
```
```  2256
```
```  2257 subsubsection{* Logarithmic series *}
```
```  2258 definition "(L::'a::{field, ring_char_0,recpower} fps)
```
```  2259   = Abs_fps (\<lambda>n. (- 1) ^ Suc n / of_nat n)"
```
```  2260
```
```  2261 lemma fps_deriv_L: "fps_deriv L = inverse (1 + X)"
```
```  2262   unfolding inverse_one_plus_X
```
```  2263   by (simp add: L_def fps_eq_iff power_Suc del: of_nat_Suc)
```
```  2264
```
```  2265 lemma L_nth: "L \$ n = (- 1) ^ Suc n / of_nat n"
```
```  2266   by (simp add: L_def)
```
```  2267
```
```  2268 lemma L_E_inv:
```
```  2269   assumes a: "a\<noteq> (0::'a::{field,division_by_zero,ring_char_0,recpower})"
```
```  2270   shows "L = fps_const a * fps_inv (E a - 1)" (is "?l = ?r")
```
```  2271 proof-
```
```  2272   let ?b = "E a - 1"
```
```  2273   have b0: "?b \$ 0 = 0" by simp
```
```  2274   have b1: "?b \$ 1 \<noteq> 0" by (simp add: a)
```
```  2275   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)"
```
```  2276     by (simp add: ring_simps)
```
```  2277   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])
```
```  2278     by (simp add: ring_simps)
```
```  2279   finally have eq: "fps_deriv (E a - 1) oo fps_inv (E a - 1) = fps_const a * (X + 1)" .
```
```  2280   from fps_inv_deriv[OF b0 b1, unfolded eq]
```
```  2281   have "fps_deriv (fps_inv ?b) = fps_const (inverse a) / (X + 1)"
```
```  2282     by (simp add: fps_const_inverse eq fps_divide_def fps_inverse_mult)
```
```  2283   hence "fps_deriv (fps_const a * fps_inv ?b) = inverse (X + 1)"
```
```  2284     using a by (simp add: fps_divide_def field_simps)
```
```  2285   hence "fps_deriv ?l = fps_deriv ?r"
```
```  2286     by (simp add: fps_deriv_L add_commute)
```
```  2287   then show ?thesis unfolding fps_deriv_eq_iff
```
```  2288     by (simp add: L_nth fps_inv_def)
```
```  2289 qed
```
```  2290
```
```  2291 subsubsection{* Formal trigonometric functions  *}
```
```  2292
```
```  2293 definition "fps_sin (c::'a::{field, recpower, ring_char_0}) =
```
```  2294   Abs_fps (\<lambda>n. if even n then 0 else (- 1) ^((n - 1) div 2) * c^n /(of_nat (fact n)))"
```
```  2295
```
```  2296 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)"
```
```  2297
```
```  2298 lemma fps_sin_deriv:
```
```  2299   "fps_deriv (fps_sin c) = fps_const c * fps_cos c"
```
```  2300   (is "?lhs = ?rhs")
```
```  2301 proof-
```
```  2302   {fix n::nat
```
```  2303     {assume en: "even n"
```
```  2304       have "?lhs\$n = of_nat (n+1) * (fps_sin c \$ (n+1))" by simp
```
```  2305       also have "\<dots> = of_nat (n+1) * ((- 1)^(n div 2) * c^Suc n / of_nat (fact (Suc n)))"
```
```  2306 	using en by (simp add: fps_sin_def)
```
```  2307       also have "\<dots> = (- 1)^(n div 2) * c^Suc n * (of_nat (n+1) / (of_nat (Suc n) * of_nat (fact n)))"
```
```  2308 	unfolding fact_Suc of_nat_mult
```
```  2309 	by (simp add: field_simps del: of_nat_add of_nat_Suc)
```
```  2310       also have "\<dots> = (- 1)^(n div 2) *c^Suc n / of_nat (fact n)"
```
```  2311 	by (simp add: field_simps del: of_nat_add of_nat_Suc)
```
```  2312       finally have "?lhs \$n = ?rhs\$n" using en
```
```  2313 	by (simp add: fps_cos_def ring_simps power_Suc )}
```
```  2314     then have "?lhs \$ n = ?rhs \$ n"
```
```  2315       by (cases "even n", simp_all add: fps_deriv_def fps_sin_def fps_cos_def) }
```
```  2316   then show ?thesis by (auto simp add: fps_eq_iff)
```
```  2317 qed
```
```  2318
```
```  2319 lemma fps_cos_deriv:
```
```  2320   "fps_deriv (fps_cos c) = fps_const (- c)* (fps_sin c)"
```
```  2321   (is "?lhs = ?rhs")
```
```  2322 proof-
```
```  2323   have th0: "\<And>n. - ((- 1::'a) ^ n) = (- 1)^Suc n" by (simp add: power_Suc)
```
```  2324   have th1: "\<And>n. odd n\<Longrightarrow> Suc ((n - 1) div 2) = Suc n div 2" by presburger (* FIXME: VERY slow! *)
```
```  2325   {fix n::nat
```
```  2326     {assume en: "odd n"
```
```  2327       from en have n0: "n \<noteq>0 " by presburger
```
```  2328       have "?lhs\$n = of_nat (n+1) * (fps_cos c \$ (n+1))" by simp
```
```  2329       also have "\<dots> = of_nat (n+1) * ((- 1)^((n + 1) div 2) * c^Suc n / of_nat (fact (Suc n)))"
```
```  2330 	using en by (simp add: fps_cos_def)
```
```  2331       also have "\<dots> = (- 1)^((n + 1) div 2)*c^Suc n * (of_nat (n+1) / (of_nat (Suc n) * of_nat (fact n)))"
```
```  2332 	unfolding fact_Suc of_nat_mult
```
```  2333 	by (simp add: field_simps del: of_nat_add of_nat_Suc)
```
```  2334       also have "\<dots> = (- 1)^((n + 1) div 2) * c^Suc n / of_nat (fact n)"
```
```  2335 	by (simp add: field_simps del: of_nat_add of_nat_Suc)
```
```  2336       also have "\<dots> = (- ((- 1)^((n - 1) div 2))) * c^Suc n / of_nat (fact n)"
```
```  2337 	unfolding th0 unfolding th1[OF en] by simp
```
```  2338       finally have "?lhs \$n = ?rhs\$n" using en
```
```  2339 	by (simp add: fps_sin_def ring_simps power_Suc)}
```
```  2340     then have "?lhs \$ n = ?rhs \$ n"
```
```  2341       by (cases "even n", simp_all add: fps_deriv_def fps_sin_def
```
```  2342 	fps_cos_def) }
```
```  2343   then show ?thesis by (auto simp add: fps_eq_iff)
```
```  2344 qed
```
```  2345
```
```  2346 lemma fps_sin_cos_sum_of_squares:
```
```  2347   "fps_cos c ^ 2 + fps_sin c ^ 2 = 1" (is "?lhs = 1")
```
```  2348 proof-
```
```  2349   have "fps_deriv ?lhs = 0"
```
```  2350     apply (simp add:  fps_deriv_power fps_sin_deriv fps_cos_deriv power_Suc)
```
```  2351     by (simp add: fps_power_def ring_simps fps_const_neg[symmetric] del: fps_const_neg)
```
```  2352   then have "?lhs = fps_const (?lhs \$ 0)"
```
```  2353     unfolding fps_deriv_eq_0_iff .
```
```  2354   also have "\<dots> = 1"
```
```  2355     by (auto simp add: fps_eq_iff fps_power_def numeral_2_eq_2 fps_mult_nth fps_cos_def fps_sin_def)
```
```  2356   finally show ?thesis .
```
```  2357 qed
```
```  2358
```
```  2359 definition "fps_tan c = fps_sin c / fps_cos c"
```
```  2360
```
```  2361 lemma fps_tan_deriv: "fps_deriv(fps_tan c) = fps_const c/ (fps_cos c ^ 2)"
```
```  2362 proof-
```
```  2363   have th0: "fps_cos c \$ 0 \<noteq> 0" by (simp add: fps_cos_def)
```
```  2364   show ?thesis
```
```  2365     using fps_sin_cos_sum_of_squares[of c]
```
```  2366     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)
```
```  2367     unfolding right_distrib[symmetric]
```
```  2368     by simp
```
```  2369 qed
```
```  2370
```
```  2371 end
```