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