src/HOL/Computational_Algebra/Formal_Power_Series.thy
author haftmann
Mon Jun 05 15:59:41 2017 +0200 (2017-06-05)
changeset 66010 2f7d39285a1a
parent 65578 e4997c181cce
child 66089 def95e0bc529
permissions -rw-r--r--
executable domain membership checks
     1 (*  Title:      HOL/Computational_Algebra/Formal_Power_Series.thy
     2     Author:     Amine Chaieb, University of Cambridge
     3 *)
     4 
     5 section \<open>A formalization of formal power series\<close>
     6 
     7 theory Formal_Power_Series
     8 imports
     9   Complex_Main
    10   Euclidean_Algorithm
    11 begin
    12 
    13 
    14 subsection \<open>The type of formal power series\<close>
    15 
    16 typedef 'a fps = "{f :: nat \<Rightarrow> 'a. True}"
    17   morphisms fps_nth Abs_fps
    18   by simp
    19 
    20 notation fps_nth (infixl "$" 75)
    21 
    22 lemma expand_fps_eq: "p = q \<longleftrightarrow> (\<forall>n. p $ n = q $ n)"
    23   by (simp add: fps_nth_inject [symmetric] fun_eq_iff)
    24 
    25 lemma fps_ext: "(\<And>n. p $ n = q $ n) \<Longrightarrow> p = q"
    26   by (simp add: expand_fps_eq)
    27 
    28 lemma fps_nth_Abs_fps [simp]: "Abs_fps f $ n = f n"
    29   by (simp add: Abs_fps_inverse)
    30 
    31 text \<open>Definition of the basic elements 0 and 1 and the basic operations of addition,
    32   negation and multiplication.\<close>
    33 
    34 instantiation fps :: (zero) zero
    35 begin
    36   definition fps_zero_def: "0 = Abs_fps (\<lambda>n. 0)"
    37   instance ..
    38 end
    39 
    40 lemma fps_zero_nth [simp]: "0 $ n = 0"
    41   unfolding fps_zero_def by simp
    42 
    43 instantiation fps :: ("{one, zero}") one
    44 begin
    45   definition fps_one_def: "1 = Abs_fps (\<lambda>n. if n = 0 then 1 else 0)"
    46   instance ..
    47 end
    48 
    49 lemma fps_one_nth [simp]: "1 $ n = (if n = 0 then 1 else 0)"
    50   unfolding fps_one_def by simp
    51 
    52 instantiation fps :: (plus) plus
    53 begin
    54   definition fps_plus_def: "op + = (\<lambda>f g. Abs_fps (\<lambda>n. f $ n + g $ n))"
    55   instance ..
    56 end
    57 
    58 lemma fps_add_nth [simp]: "(f + g) $ n = f $ n + g $ n"
    59   unfolding fps_plus_def by simp
    60 
    61 instantiation fps :: (minus) minus
    62 begin
    63   definition fps_minus_def: "op - = (\<lambda>f g. Abs_fps (\<lambda>n. f $ n - g $ n))"
    64   instance ..
    65 end
    66 
    67 lemma fps_sub_nth [simp]: "(f - g) $ n = f $ n - g $ n"
    68   unfolding fps_minus_def by simp
    69 
    70 instantiation fps :: (uminus) uminus
    71 begin
    72   definition fps_uminus_def: "uminus = (\<lambda>f. Abs_fps (\<lambda>n. - (f $ n)))"
    73   instance ..
    74 end
    75 
    76 lemma fps_neg_nth [simp]: "(- f) $ n = - (f $ n)"
    77   unfolding fps_uminus_def by simp
    78 
    79 instantiation fps :: ("{comm_monoid_add, times}") times
    80 begin
    81   definition fps_times_def: "op * = (\<lambda>f g. Abs_fps (\<lambda>n. \<Sum>i=0..n. f $ i * g $ (n - i)))"
    82   instance ..
    83 end
    84 
    85 lemma fps_mult_nth: "(f * g) $ n = (\<Sum>i=0..n. f$i * g$(n - i))"
    86   unfolding fps_times_def by simp
    87 
    88 lemma fps_mult_nth_0 [simp]: "(f * g) $ 0 = f $ 0 * g $ 0"
    89   unfolding fps_times_def by simp
    90 
    91 declare atLeastAtMost_iff [presburger]
    92 declare Bex_def [presburger]
    93 declare Ball_def [presburger]
    94 
    95 lemma mult_delta_left:
    96   fixes x y :: "'a::mult_zero"
    97   shows "(if b then x else 0) * y = (if b then x * y else 0)"
    98   by simp
    99 
   100 lemma mult_delta_right:
   101   fixes x y :: "'a::mult_zero"
   102   shows "x * (if b then y else 0) = (if b then x * y else 0)"
   103   by simp
   104 
   105 lemma cond_value_iff: "f (if b then x else y) = (if b then f x else f y)"
   106   by auto
   107 
   108 lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
   109   by auto
   110 
   111 
   112 subsection \<open>Formal power series form a commutative ring with unity, if the range of sequences
   113   they represent is a commutative ring with unity\<close>
   114 
   115 instance fps :: (semigroup_add) semigroup_add
   116 proof
   117   fix a b c :: "'a fps"
   118   show "a + b + c = a + (b + c)"
   119     by (simp add: fps_ext add.assoc)
   120 qed
   121 
   122 instance fps :: (ab_semigroup_add) ab_semigroup_add
   123 proof
   124   fix a b :: "'a fps"
   125   show "a + b = b + a"
   126     by (simp add: fps_ext add.commute)
   127 qed
   128 
   129 lemma fps_mult_assoc_lemma:
   130   fixes k :: nat
   131     and f :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
   132   shows "(\<Sum>j=0..k. \<Sum>i=0..j. f i (j - i) (n - j)) =
   133          (\<Sum>j=0..k. \<Sum>i=0..k - j. f j i (n - j - i))"
   134   by (induct k) (simp_all add: Suc_diff_le sum.distrib add.assoc)
   135 
   136 instance fps :: (semiring_0) semigroup_mult
   137 proof
   138   fix a b c :: "'a fps"
   139   show "(a * b) * c = a * (b * c)"
   140   proof (rule fps_ext)
   141     fix n :: nat
   142     have "(\<Sum>j=0..n. \<Sum>i=0..j. a$i * b$(j - i) * c$(n - j)) =
   143           (\<Sum>j=0..n. \<Sum>i=0..n - j. a$j * b$i * c$(n - j - i))"
   144       by (rule fps_mult_assoc_lemma)
   145     then show "((a * b) * c) $ n = (a * (b * c)) $ n"
   146       by (simp add: fps_mult_nth sum_distrib_left sum_distrib_right mult.assoc)
   147   qed
   148 qed
   149 
   150 lemma fps_mult_commute_lemma:
   151   fixes n :: nat
   152     and f :: "nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
   153   shows "(\<Sum>i=0..n. f i (n - i)) = (\<Sum>i=0..n. f (n - i) i)"
   154   by (rule sum.reindex_bij_witness[where i="op - n" and j="op - n"]) auto
   155 
   156 instance fps :: (comm_semiring_0) ab_semigroup_mult
   157 proof
   158   fix a b :: "'a fps"
   159   show "a * b = b * a"
   160   proof (rule fps_ext)
   161     fix n :: nat
   162     have "(\<Sum>i=0..n. a$i * b$(n - i)) = (\<Sum>i=0..n. a$(n - i) * b$i)"
   163       by (rule fps_mult_commute_lemma)
   164     then show "(a * b) $ n = (b * a) $ n"
   165       by (simp add: fps_mult_nth mult.commute)
   166   qed
   167 qed
   168 
   169 instance fps :: (monoid_add) monoid_add
   170 proof
   171   fix a :: "'a fps"
   172   show "0 + a = a" by (simp add: fps_ext)
   173   show "a + 0 = a" by (simp add: fps_ext)
   174 qed
   175 
   176 instance fps :: (comm_monoid_add) comm_monoid_add
   177 proof
   178   fix a :: "'a fps"
   179   show "0 + a = a" by (simp add: fps_ext)
   180 qed
   181 
   182 instance fps :: (semiring_1) monoid_mult
   183 proof
   184   fix a :: "'a fps"
   185   show "1 * a = a"
   186     by (simp add: fps_ext fps_mult_nth mult_delta_left sum.delta)
   187   show "a * 1 = a"
   188     by (simp add: fps_ext fps_mult_nth mult_delta_right sum.delta')
   189 qed
   190 
   191 instance fps :: (cancel_semigroup_add) cancel_semigroup_add
   192 proof
   193   fix a b c :: "'a fps"
   194   show "b = c" if "a + b = a + c"
   195     using that by (simp add: expand_fps_eq)
   196   show "b = c" if "b + a = c + a"
   197     using that by (simp add: expand_fps_eq)
   198 qed
   199 
   200 instance fps :: (cancel_ab_semigroup_add) cancel_ab_semigroup_add
   201 proof
   202   fix a b c :: "'a fps"
   203   show "a + b - a = b"
   204     by (simp add: expand_fps_eq)
   205   show "a - b - c = a - (b + c)"
   206     by (simp add: expand_fps_eq diff_diff_eq)
   207 qed
   208 
   209 instance fps :: (cancel_comm_monoid_add) cancel_comm_monoid_add ..
   210 
   211 instance fps :: (group_add) group_add
   212 proof
   213   fix a b :: "'a fps"
   214   show "- a + a = 0" by (simp add: fps_ext)
   215   show "a + - b = a - b" by (simp add: fps_ext)
   216 qed
   217 
   218 instance fps :: (ab_group_add) ab_group_add
   219 proof
   220   fix a b :: "'a fps"
   221   show "- a + a = 0" by (simp add: fps_ext)
   222   show "a - b = a + - b" by (simp add: fps_ext)
   223 qed
   224 
   225 instance fps :: (zero_neq_one) zero_neq_one
   226   by standard (simp add: expand_fps_eq)
   227 
   228 instance fps :: (semiring_0) semiring
   229 proof
   230   fix a b c :: "'a fps"
   231   show "(a + b) * c = a * c + b * c"
   232     by (simp add: expand_fps_eq fps_mult_nth distrib_right sum.distrib)
   233   show "a * (b + c) = a * b + a * c"
   234     by (simp add: expand_fps_eq fps_mult_nth distrib_left sum.distrib)
   235 qed
   236 
   237 instance fps :: (semiring_0) semiring_0
   238 proof
   239   fix a :: "'a fps"
   240   show "0 * a = 0"
   241     by (simp add: fps_ext fps_mult_nth)
   242   show "a * 0 = 0"
   243     by (simp add: fps_ext fps_mult_nth)
   244 qed
   245 
   246 instance fps :: (semiring_0_cancel) semiring_0_cancel ..
   247 
   248 instance fps :: (semiring_1) semiring_1 ..
   249 
   250 
   251 subsection \<open>Selection of the nth power of the implicit variable in the infinite sum\<close>
   252 
   253 lemma fps_square_nth: "(f^2) $ n = (\<Sum>k\<le>n. f $ k * f $ (n - k))"
   254   by (simp add: power2_eq_square fps_mult_nth atLeast0AtMost)
   255 
   256 lemma fps_nonzero_nth: "f \<noteq> 0 \<longleftrightarrow> (\<exists> n. f $n \<noteq> 0)"
   257   by (simp add: expand_fps_eq)
   258 
   259 lemma fps_nonzero_nth_minimal: "f \<noteq> 0 \<longleftrightarrow> (\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m < n. f $ m = 0))"
   260   (is "?lhs \<longleftrightarrow> ?rhs")
   261 proof
   262   let ?n = "LEAST n. f $ n \<noteq> 0"
   263   show ?rhs if ?lhs
   264   proof -
   265     from that have "\<exists>n. f $ n \<noteq> 0"
   266       by (simp add: fps_nonzero_nth)
   267     then have "f $ ?n \<noteq> 0"
   268       by (rule LeastI_ex)
   269     moreover have "\<forall>m<?n. f $ m = 0"
   270       by (auto dest: not_less_Least)
   271     ultimately have "f $ ?n \<noteq> 0 \<and> (\<forall>m<?n. f $ m = 0)" ..
   272     then show ?thesis ..
   273   qed
   274   show ?lhs if ?rhs
   275     using that by (auto simp add: expand_fps_eq)
   276 qed
   277 
   278 lemma fps_eq_iff: "f = g \<longleftrightarrow> (\<forall>n. f $ n = g $n)"
   279   by (rule expand_fps_eq)
   280 
   281 lemma fps_sum_nth: "sum f S $ n = sum (\<lambda>k. (f k) $ n) S"
   282 proof (cases "finite S")
   283   case True
   284   then show ?thesis by (induct set: finite) auto
   285 next
   286   case False
   287   then show ?thesis by simp
   288 qed
   289 
   290 
   291 subsection \<open>Injection of the basic ring elements and multiplication by scalars\<close>
   292 
   293 definition "fps_const c = Abs_fps (\<lambda>n. if n = 0 then c else 0)"
   294 
   295 lemma fps_nth_fps_const [simp]: "fps_const c $ n = (if n = 0 then c else 0)"
   296   unfolding fps_const_def by simp
   297 
   298 lemma fps_const_0_eq_0 [simp]: "fps_const 0 = 0"
   299   by (simp add: fps_ext)
   300 
   301 lemma fps_const_1_eq_1 [simp]: "fps_const 1 = 1"
   302   by (simp add: fps_ext)
   303 
   304 lemma fps_const_neg [simp]: "- (fps_const (c::'a::ring)) = fps_const (- c)"
   305   by (simp add: fps_ext)
   306 
   307 lemma fps_const_add [simp]: "fps_const (c::'a::monoid_add) + fps_const d = fps_const (c + d)"
   308   by (simp add: fps_ext)
   309 
   310 lemma fps_const_sub [simp]: "fps_const (c::'a::group_add) - fps_const d = fps_const (c - d)"
   311   by (simp add: fps_ext)
   312 
   313 lemma fps_const_mult[simp]: "fps_const (c::'a::ring) * fps_const d = fps_const (c * d)"
   314   by (simp add: fps_eq_iff fps_mult_nth sum.neutral)
   315 
   316 lemma fps_const_add_left: "fps_const (c::'a::monoid_add) + f =
   317     Abs_fps (\<lambda>n. if n = 0 then c + f$0 else f$n)"
   318   by (simp add: fps_ext)
   319 
   320 lemma fps_const_add_right: "f + fps_const (c::'a::monoid_add) =
   321     Abs_fps (\<lambda>n. if n = 0 then f$0 + c else f$n)"
   322   by (simp add: fps_ext)
   323 
   324 lemma fps_const_mult_left: "fps_const (c::'a::semiring_0) * f = Abs_fps (\<lambda>n. c * f$n)"
   325   unfolding fps_eq_iff fps_mult_nth
   326   by (simp add: fps_const_def mult_delta_left sum.delta)
   327 
   328 lemma fps_const_mult_right: "f * fps_const (c::'a::semiring_0) = Abs_fps (\<lambda>n. f$n * c)"
   329   unfolding fps_eq_iff fps_mult_nth
   330   by (simp add: fps_const_def mult_delta_right sum.delta')
   331 
   332 lemma fps_mult_left_const_nth [simp]: "(fps_const (c::'a::semiring_1) * f)$n = c* f$n"
   333   by (simp add: fps_mult_nth mult_delta_left sum.delta)
   334 
   335 lemma fps_mult_right_const_nth [simp]: "(f * fps_const (c::'a::semiring_1))$n = f$n * c"
   336   by (simp add: fps_mult_nth mult_delta_right sum.delta')
   337 
   338 
   339 subsection \<open>Formal power series form an integral domain\<close>
   340 
   341 instance fps :: (ring) ring ..
   342 
   343 instance fps :: (ring_1) ring_1
   344   by (intro_classes, auto simp add: distrib_right)
   345 
   346 instance fps :: (comm_ring_1) comm_ring_1
   347   by (intro_classes, auto simp add: distrib_right)
   348 
   349 instance fps :: (ring_no_zero_divisors) ring_no_zero_divisors
   350 proof
   351   fix a b :: "'a fps"
   352   assume "a \<noteq> 0" and "b \<noteq> 0"
   353   then obtain i j where i: "a $ i \<noteq> 0" "\<forall>k<i. a $ k = 0" and j: "b $ j \<noteq> 0" "\<forall>k<j. b $ k =0"
   354     unfolding fps_nonzero_nth_minimal
   355     by blast+
   356   have "(a * b) $ (i + j) = (\<Sum>k=0..i+j. a $ k * b $ (i + j - k))"
   357     by (rule fps_mult_nth)
   358   also have "\<dots> = (a $ i * b $ (i + j - i)) + (\<Sum>k\<in>{0..i+j} - {i}. a $ k * b $ (i + j - k))"
   359     by (rule sum.remove) simp_all
   360   also have "(\<Sum>k\<in>{0..i+j}-{i}. a $ k * b $ (i + j - k)) = 0"
   361   proof (rule sum.neutral [rule_format])
   362     fix k assume "k \<in> {0..i+j} - {i}"
   363     then have "k < i \<or> i+j-k < j"
   364       by auto
   365     then show "a $ k * b $ (i + j - k) = 0"
   366       using i j by auto
   367   qed
   368   also have "a $ i * b $ (i + j - i) + 0 = a $ i * b $ j"
   369     by simp
   370   also have "a $ i * b $ j \<noteq> 0"
   371     using i j by simp
   372   finally have "(a*b) $ (i+j) \<noteq> 0" .
   373   then show "a * b \<noteq> 0"
   374     unfolding fps_nonzero_nth by blast
   375 qed
   376 
   377 instance fps :: (ring_1_no_zero_divisors) ring_1_no_zero_divisors ..
   378 
   379 instance fps :: (idom) idom ..
   380 
   381 lemma numeral_fps_const: "numeral k = fps_const (numeral k)"
   382   by (induct k) (simp_all only: numeral.simps fps_const_1_eq_1
   383     fps_const_add [symmetric])
   384 
   385 lemma neg_numeral_fps_const:
   386   "(- numeral k :: 'a :: ring_1 fps) = fps_const (- numeral k)"
   387   by (simp add: numeral_fps_const)
   388 
   389 lemma fps_numeral_nth: "numeral n $ i = (if i = 0 then numeral n else 0)"
   390   by (simp add: numeral_fps_const)
   391 
   392 lemma fps_numeral_nth_0 [simp]: "numeral n $ 0 = numeral n"
   393   by (simp add: numeral_fps_const)
   394 
   395 lemma fps_of_nat: "fps_const (of_nat c) = of_nat c"
   396   by (induction c) (simp_all add: fps_const_add [symmetric] del: fps_const_add)
   397 
   398 lemma numeral_neq_fps_zero [simp]: "(numeral f :: 'a :: field_char_0 fps) \<noteq> 0"
   399 proof
   400   assume "numeral f = (0 :: 'a fps)"
   401   from arg_cong[of _ _ "\<lambda>F. F $ 0", OF this] show False by simp
   402 qed 
   403 
   404 
   405 subsection \<open>The eXtractor series X\<close>
   406 
   407 lemma minus_one_power_iff: "(- (1::'a::comm_ring_1)) ^ n = (if even n then 1 else - 1)"
   408   by (induct n) auto
   409 
   410 definition "X = Abs_fps (\<lambda>n. if n = 1 then 1 else 0)"
   411 
   412 lemma X_mult_nth [simp]:
   413   "(X * (f :: 'a::semiring_1 fps)) $n = (if n = 0 then 0 else f $ (n - 1))"
   414 proof (cases "n = 0")
   415   case False
   416   have "(X * f) $n = (\<Sum>i = 0..n. X $ i * f $ (n - i))"
   417     by (simp add: fps_mult_nth)
   418   also have "\<dots> = f $ (n - 1)"
   419     using False by (simp add: X_def mult_delta_left sum.delta)
   420   finally show ?thesis
   421     using False by simp
   422 next
   423   case True
   424   then show ?thesis
   425     by (simp add: fps_mult_nth X_def)
   426 qed
   427 
   428 lemma X_mult_right_nth[simp]:
   429   "((a::'a::semiring_1 fps) * X) $ n = (if n = 0 then 0 else a $ (n - 1))"
   430 proof -
   431   have "(a * X) $ n = (\<Sum>i = 0..n. a $ i * (if n - i = Suc 0 then 1 else 0))"
   432     by (simp add: fps_times_def X_def)
   433   also have "\<dots> = (\<Sum>i = 0..n. if i = n - 1 then if n = 0 then 0 else a $ i else 0)"
   434     by (intro sum.cong) auto
   435   also have "\<dots> = (if n = 0 then 0 else a $ (n - 1))" by (simp add: sum.delta)
   436   finally show ?thesis .
   437 qed
   438 
   439 lemma fps_mult_X_commute: "X * (a :: 'a :: semiring_1 fps) = a * X" 
   440   by (simp add: fps_eq_iff)
   441 
   442 lemma X_power_iff: "X^k = Abs_fps (\<lambda>n. if n = k then 1::'a::comm_ring_1 else 0)"
   443 proof (induct k)
   444   case 0
   445   then show ?case by (simp add: X_def fps_eq_iff)
   446 next
   447   case (Suc k)
   448   have "(X^Suc k) $ m = (if m = Suc k then 1::'a else 0)" for m
   449   proof -
   450     have "(X^Suc k) $ m = (if m = 0 then 0 else (X^k) $ (m - 1))"
   451       by (simp del: One_nat_def)
   452     then show ?thesis
   453       using Suc.hyps by (auto cong del: if_weak_cong)
   454   qed
   455   then show ?case
   456     by (simp add: fps_eq_iff)
   457 qed
   458 
   459 lemma X_nth[simp]: "X$n = (if n = 1 then 1 else 0)"
   460   by (simp add: X_def)
   461 
   462 lemma X_power_nth[simp]: "(X^k) $n = (if n = k then 1 else 0::'a::comm_ring_1)"
   463   by (simp add: X_power_iff)
   464 
   465 lemma X_power_mult_nth: "(X^k * (f :: 'a::comm_ring_1 fps)) $n = (if n < k then 0 else f $ (n - k))"
   466   apply (induct k arbitrary: n)
   467   apply simp
   468   unfolding power_Suc mult.assoc
   469   apply (case_tac n)
   470   apply auto
   471   done
   472 
   473 lemma X_power_mult_right_nth:
   474     "((f :: 'a::comm_ring_1 fps) * X^k) $n = (if n < k then 0 else f $ (n - k))"
   475   by (metis X_power_mult_nth mult.commute)
   476 
   477 
   478 lemma X_neq_fps_const [simp]: "(X :: 'a :: zero_neq_one fps) \<noteq> fps_const c"
   479 proof
   480   assume "(X::'a fps) = fps_const (c::'a)"
   481   hence "X$1 = (fps_const (c::'a))$1" by (simp only:)
   482   thus False by auto
   483 qed
   484 
   485 lemma X_neq_zero [simp]: "(X :: 'a :: zero_neq_one fps) \<noteq> 0"
   486   by (simp only: fps_const_0_eq_0[symmetric] X_neq_fps_const) simp
   487 
   488 lemma X_neq_one [simp]: "(X :: 'a :: zero_neq_one fps) \<noteq> 1"
   489   by (simp only: fps_const_1_eq_1[symmetric] X_neq_fps_const) simp
   490 
   491 lemma X_neq_numeral [simp]: "(X :: 'a :: {semiring_1,zero_neq_one} fps) \<noteq> numeral c"
   492   by (simp only: numeral_fps_const X_neq_fps_const) simp
   493 
   494 lemma X_pow_eq_X_pow_iff [simp]:
   495   "(X :: ('a :: {comm_ring_1}) fps) ^ m = X ^ n \<longleftrightarrow> m = n"
   496 proof
   497   assume "(X :: 'a fps) ^ m = X ^ n"
   498   hence "(X :: 'a fps) ^ m $ m = X ^ n $ m" by (simp only:)
   499   thus "m = n" by (simp split: if_split_asm)
   500 qed simp_all
   501 
   502 
   503 subsection \<open>Subdegrees\<close>
   504 
   505 definition subdegree :: "('a::zero) fps \<Rightarrow> nat" where
   506   "subdegree f = (if f = 0 then 0 else LEAST n. f$n \<noteq> 0)"
   507 
   508 lemma subdegreeI:
   509   assumes "f $ d \<noteq> 0" and "\<And>i. i < d \<Longrightarrow> f $ i = 0"
   510   shows   "subdegree f = d"
   511 proof-
   512   from assms(1) have "f \<noteq> 0" by auto
   513   moreover from assms(1) have "(LEAST i. f $ i \<noteq> 0) = d"
   514   proof (rule Least_equality)
   515     fix e assume "f $ e \<noteq> 0"
   516     with assms(2) have "\<not>(e < d)" by blast
   517     thus "e \<ge> d" by simp
   518   qed
   519   ultimately show ?thesis unfolding subdegree_def by simp
   520 qed
   521 
   522 lemma nth_subdegree_nonzero [simp,intro]: "f \<noteq> 0 \<Longrightarrow> f $ subdegree f \<noteq> 0"
   523 proof-
   524   assume "f \<noteq> 0"
   525   hence "subdegree f = (LEAST n. f $ n \<noteq> 0)" by (simp add: subdegree_def)
   526   also from \<open>f \<noteq> 0\<close> have "\<exists>n. f$n \<noteq> 0" using fps_nonzero_nth by blast
   527   from LeastI_ex[OF this] have "f $ (LEAST n. f $ n \<noteq> 0) \<noteq> 0" .
   528   finally show ?thesis .
   529 qed
   530 
   531 lemma nth_less_subdegree_zero [dest]: "n < subdegree f \<Longrightarrow> f $ n = 0"
   532 proof (cases "f = 0")
   533   assume "f \<noteq> 0" and less: "n < subdegree f"
   534   note less
   535   also from \<open>f \<noteq> 0\<close> have "subdegree f = (LEAST n. f $ n \<noteq> 0)" by (simp add: subdegree_def)
   536   finally show "f $ n = 0" using not_less_Least by blast
   537 qed simp_all
   538 
   539 lemma subdegree_geI:
   540   assumes "f \<noteq> 0" "\<And>i. i < n \<Longrightarrow> f$i = 0"
   541   shows   "subdegree f \<ge> n"
   542 proof (rule ccontr)
   543   assume "\<not>(subdegree f \<ge> n)"
   544   with assms(2) have "f $ subdegree f = 0" by simp
   545   moreover from assms(1) have "f $ subdegree f \<noteq> 0" by simp
   546   ultimately show False by contradiction
   547 qed
   548 
   549 lemma subdegree_greaterI:
   550   assumes "f \<noteq> 0" "\<And>i. i \<le> n \<Longrightarrow> f$i = 0"
   551   shows   "subdegree f > n"
   552 proof (rule ccontr)
   553   assume "\<not>(subdegree f > n)"
   554   with assms(2) have "f $ subdegree f = 0" by simp
   555   moreover from assms(1) have "f $ subdegree f \<noteq> 0" by simp
   556   ultimately show False by contradiction
   557 qed
   558 
   559 lemma subdegree_leI:
   560   "f $ n \<noteq> 0 \<Longrightarrow> subdegree f \<le> n"
   561   by (rule leI) auto
   562 
   563 
   564 lemma subdegree_0 [simp]: "subdegree 0 = 0"
   565   by (simp add: subdegree_def)
   566 
   567 lemma subdegree_1 [simp]: "subdegree (1 :: ('a :: zero_neq_one) fps) = 0"
   568   by (auto intro!: subdegreeI)
   569 
   570 lemma subdegree_X [simp]: "subdegree (X :: ('a :: zero_neq_one) fps) = 1"
   571   by (auto intro!: subdegreeI simp: X_def)
   572 
   573 lemma subdegree_fps_const [simp]: "subdegree (fps_const c) = 0"
   574   by (cases "c = 0") (auto intro!: subdegreeI)
   575 
   576 lemma subdegree_numeral [simp]: "subdegree (numeral n) = 0"
   577   by (simp add: numeral_fps_const)
   578 
   579 lemma subdegree_eq_0_iff: "subdegree f = 0 \<longleftrightarrow> f = 0 \<or> f $ 0 \<noteq> 0"
   580 proof (cases "f = 0")
   581   assume "f \<noteq> 0"
   582   thus ?thesis
   583     using nth_subdegree_nonzero[OF \<open>f \<noteq> 0\<close>] by (fastforce intro!: subdegreeI)
   584 qed simp_all
   585 
   586 lemma subdegree_eq_0 [simp]: "f $ 0 \<noteq> 0 \<Longrightarrow> subdegree f = 0"
   587   by (simp add: subdegree_eq_0_iff)
   588 
   589 lemma nth_subdegree_mult [simp]:
   590   fixes f g :: "('a :: {mult_zero,comm_monoid_add}) fps"
   591   shows "(f * g) $ (subdegree f + subdegree g) = f $ subdegree f * g $ subdegree g"
   592 proof-
   593   let ?n = "subdegree f + subdegree g"
   594   have "(f * g) $ ?n = (\<Sum>i=0..?n. f$i * g$(?n-i))"
   595     by (simp add: fps_mult_nth)
   596   also have "... = (\<Sum>i=0..?n. if i = subdegree f then f$i * g$(?n-i) else 0)"
   597   proof (intro sum.cong)
   598     fix x assume x: "x \<in> {0..?n}"
   599     hence "x = subdegree f \<or> x < subdegree f \<or> ?n - x < subdegree g" by auto
   600     thus "f $ x * g $ (?n - x) = (if x = subdegree f then f $ x * g $ (?n - x) else 0)"
   601       by (elim disjE conjE) auto
   602   qed auto
   603   also have "... = f $ subdegree f * g $ subdegree g" by (simp add: sum.delta)
   604   finally show ?thesis .
   605 qed
   606 
   607 lemma subdegree_mult [simp]:
   608   assumes "f \<noteq> 0" "g \<noteq> 0"
   609   shows "subdegree ((f :: ('a :: {ring_no_zero_divisors}) fps) * g) = subdegree f + subdegree g"
   610 proof (rule subdegreeI)
   611   let ?n = "subdegree f + subdegree g"
   612   have "(f * g) $ ?n = (\<Sum>i=0..?n. f$i * g$(?n-i))" by (simp add: fps_mult_nth)
   613   also have "... = (\<Sum>i=0..?n. if i = subdegree f then f$i * g$(?n-i) else 0)"
   614   proof (intro sum.cong)
   615     fix x assume x: "x \<in> {0..?n}"
   616     hence "x = subdegree f \<or> x < subdegree f \<or> ?n - x < subdegree g" by auto
   617     thus "f $ x * g $ (?n - x) = (if x = subdegree f then f $ x * g $ (?n - x) else 0)"
   618       by (elim disjE conjE) auto
   619   qed auto
   620   also have "... = f $ subdegree f * g $ subdegree g" by (simp add: sum.delta)
   621   also from assms have "... \<noteq> 0" by auto
   622   finally show "(f * g) $ (subdegree f + subdegree g) \<noteq> 0" .
   623 next
   624   fix m assume m: "m < subdegree f + subdegree g"
   625   have "(f * g) $ m = (\<Sum>i=0..m. f$i * g$(m-i))" by (simp add: fps_mult_nth)
   626   also have "... = (\<Sum>i=0..m. 0)"
   627   proof (rule sum.cong)
   628     fix i assume "i \<in> {0..m}"
   629     with m have "i < subdegree f \<or> m - i < subdegree g" by auto
   630     thus "f$i * g$(m-i) = 0" by (elim disjE) auto
   631   qed auto
   632   finally show "(f * g) $ m = 0" by simp
   633 qed
   634 
   635 lemma subdegree_power [simp]:
   636   "subdegree ((f :: ('a :: ring_1_no_zero_divisors) fps) ^ n) = n * subdegree f"
   637   by (cases "f = 0"; induction n) simp_all
   638 
   639 lemma subdegree_uminus [simp]:
   640   "subdegree (-(f::('a::group_add) fps)) = subdegree f"
   641   by (simp add: subdegree_def)
   642 
   643 lemma subdegree_minus_commute [simp]:
   644   "subdegree (f-(g::('a::group_add) fps)) = subdegree (g - f)"
   645 proof -
   646   have "f - g = -(g - f)" by simp
   647   also have "subdegree ... = subdegree (g - f)" by (simp only: subdegree_uminus)
   648   finally show ?thesis .
   649 qed
   650 
   651 lemma subdegree_add_ge:
   652   assumes "f \<noteq> -(g :: ('a :: {group_add}) fps)"
   653   shows   "subdegree (f + g) \<ge> min (subdegree f) (subdegree g)"
   654 proof (rule subdegree_geI)
   655   from assms show "f + g \<noteq> 0" by (subst (asm) eq_neg_iff_add_eq_0)
   656 next
   657   fix i assume "i < min (subdegree f) (subdegree g)"
   658   hence "f $ i = 0" and "g $ i = 0" by auto
   659   thus "(f + g) $ i = 0" by force
   660 qed
   661 
   662 lemma subdegree_add_eq1:
   663   assumes "f \<noteq> 0"
   664   assumes "subdegree f < subdegree (g :: ('a :: {group_add}) fps)"
   665   shows   "subdegree (f + g) = subdegree f"
   666 proof (rule antisym[OF subdegree_leI])
   667   from assms show "subdegree (f + g) \<ge> subdegree f"
   668     by (intro order.trans[OF min.boundedI subdegree_add_ge]) auto
   669   from assms have "f $ subdegree f \<noteq> 0" "g $ subdegree f = 0" by auto
   670   thus "(f + g) $ subdegree f \<noteq> 0" by simp
   671 qed
   672 
   673 lemma subdegree_add_eq2:
   674   assumes "g \<noteq> 0"
   675   assumes "subdegree g < subdegree (f :: ('a :: {ab_group_add}) fps)"
   676   shows   "subdegree (f + g) = subdegree g"
   677   using subdegree_add_eq1[OF assms] by (simp add: add.commute)
   678 
   679 lemma subdegree_diff_eq1:
   680   assumes "f \<noteq> 0"
   681   assumes "subdegree f < subdegree (g :: ('a :: {ab_group_add}) fps)"
   682   shows   "subdegree (f - g) = subdegree f"
   683   using subdegree_add_eq1[of f "-g"] assms by (simp add: add.commute)
   684 
   685 lemma subdegree_diff_eq2:
   686   assumes "g \<noteq> 0"
   687   assumes "subdegree g < subdegree (f :: ('a :: {ab_group_add}) fps)"
   688   shows   "subdegree (f - g) = subdegree g"
   689   using subdegree_add_eq2[of "-g" f] assms by (simp add: add.commute)
   690 
   691 lemma subdegree_diff_ge [simp]:
   692   assumes "f \<noteq> (g :: ('a :: {group_add}) fps)"
   693   shows   "subdegree (f - g) \<ge> min (subdegree f) (subdegree g)"
   694   using assms subdegree_add_ge[of f "-g"] by simp
   695 
   696 
   697 
   698 
   699 subsection \<open>Shifting and slicing\<close>
   700 
   701 definition fps_shift :: "nat \<Rightarrow> 'a fps \<Rightarrow> 'a fps" where
   702   "fps_shift n f = Abs_fps (\<lambda>i. f $ (i + n))"
   703 
   704 lemma fps_shift_nth [simp]: "fps_shift n f $ i = f $ (i + n)"
   705   by (simp add: fps_shift_def)
   706 
   707 lemma fps_shift_0 [simp]: "fps_shift 0 f = f"
   708   by (intro fps_ext) (simp add: fps_shift_def)
   709 
   710 lemma fps_shift_zero [simp]: "fps_shift n 0 = 0"
   711   by (intro fps_ext) (simp add: fps_shift_def)
   712 
   713 lemma fps_shift_one: "fps_shift n 1 = (if n = 0 then 1 else 0)"
   714   by (intro fps_ext) (simp add: fps_shift_def)
   715 
   716 lemma fps_shift_fps_const: "fps_shift n (fps_const c) = (if n = 0 then fps_const c else 0)"
   717   by (intro fps_ext) (simp add: fps_shift_def)
   718 
   719 lemma fps_shift_numeral: "fps_shift n (numeral c) = (if n = 0 then numeral c else 0)"
   720   by (simp add: numeral_fps_const fps_shift_fps_const)
   721 
   722 lemma fps_shift_X_power [simp]:
   723   "n \<le> m \<Longrightarrow> fps_shift n (X ^ m) = (X ^ (m - n) ::'a::comm_ring_1 fps)"
   724   by (intro fps_ext) (auto simp: fps_shift_def )
   725 
   726 lemma fps_shift_times_X_power:
   727   "n \<le> subdegree f \<Longrightarrow> fps_shift n f * X ^ n = (f :: 'a :: comm_ring_1 fps)"
   728   by (intro fps_ext) (auto simp: X_power_mult_right_nth nth_less_subdegree_zero)
   729 
   730 lemma fps_shift_times_X_power' [simp]:
   731   "fps_shift n (f * X^n) = (f :: 'a :: comm_ring_1 fps)"
   732   by (intro fps_ext) (auto simp: X_power_mult_right_nth nth_less_subdegree_zero)
   733 
   734 lemma fps_shift_times_X_power'':
   735   "m \<le> n \<Longrightarrow> fps_shift n (f * X^m) = fps_shift (n - m) (f :: 'a :: comm_ring_1 fps)"
   736   by (intro fps_ext) (auto simp: X_power_mult_right_nth nth_less_subdegree_zero)
   737 
   738 lemma fps_shift_subdegree [simp]:
   739   "n \<le> subdegree f \<Longrightarrow> subdegree (fps_shift n f) = subdegree (f :: 'a :: comm_ring_1 fps) - n"
   740   by (cases "f = 0") (force intro: nth_less_subdegree_zero subdegreeI)+
   741 
   742 lemma subdegree_decompose:
   743   "f = fps_shift (subdegree f) f * X ^ subdegree (f :: ('a :: comm_ring_1) fps)"
   744   by (rule fps_ext) (auto simp: X_power_mult_right_nth)
   745 
   746 lemma subdegree_decompose':
   747   "n \<le> subdegree (f :: ('a :: comm_ring_1) fps) \<Longrightarrow> f = fps_shift n f * X^n"
   748   by (rule fps_ext) (auto simp: X_power_mult_right_nth intro!: nth_less_subdegree_zero)
   749 
   750 lemma fps_shift_fps_shift:
   751   "fps_shift (m + n) f = fps_shift m (fps_shift n f)"
   752   by (rule fps_ext) (simp add: add_ac)
   753 
   754 lemma fps_shift_add:
   755   "fps_shift n (f + g) = fps_shift n f + fps_shift n g"
   756   by (simp add: fps_eq_iff)
   757 
   758 lemma fps_shift_mult:
   759   assumes "n \<le> subdegree (g :: 'b :: {comm_ring_1} fps)"
   760   shows   "fps_shift n (h*g) = h * fps_shift n g"
   761 proof -
   762   from assms have "g = fps_shift n g * X^n" by (rule subdegree_decompose')
   763   also have "h * ... = (h * fps_shift n g) * X^n" by simp
   764   also have "fps_shift n ... = h * fps_shift n g" by simp
   765   finally show ?thesis .
   766 qed
   767 
   768 lemma fps_shift_mult_right:
   769   assumes "n \<le> subdegree (g :: 'b :: {comm_ring_1} fps)"
   770   shows   "fps_shift n (g*h) = h * fps_shift n g"
   771   by (subst mult.commute, subst fps_shift_mult) (simp_all add: assms)
   772 
   773 lemma nth_subdegree_zero_iff [simp]: "f $ subdegree f = 0 \<longleftrightarrow> f = 0"
   774   by (cases "f = 0") auto
   775 
   776 lemma fps_shift_subdegree_zero_iff [simp]:
   777   "fps_shift (subdegree f) f = 0 \<longleftrightarrow> f = 0"
   778   by (subst (1) nth_subdegree_zero_iff[symmetric], cases "f = 0")
   779      (simp_all del: nth_subdegree_zero_iff)
   780 
   781 
   782 definition "fps_cutoff n f = Abs_fps (\<lambda>i. if i < n then f$i else 0)"
   783 
   784 lemma fps_cutoff_nth [simp]: "fps_cutoff n f $ i = (if i < n then f$i else 0)"
   785   unfolding fps_cutoff_def by simp
   786 
   787 lemma fps_cutoff_zero_iff: "fps_cutoff n f = 0 \<longleftrightarrow> (f = 0 \<or> n \<le> subdegree f)"
   788 proof
   789   assume A: "fps_cutoff n f = 0"
   790   thus "f = 0 \<or> n \<le> subdegree f"
   791   proof (cases "f = 0")
   792     assume "f \<noteq> 0"
   793     with A have "n \<le> subdegree f"
   794       by (intro subdegree_geI) (auto simp: fps_eq_iff split: if_split_asm)
   795     thus ?thesis ..
   796   qed simp
   797 qed (auto simp: fps_eq_iff intro: nth_less_subdegree_zero)
   798 
   799 lemma fps_cutoff_0 [simp]: "fps_cutoff 0 f = 0"
   800   by (simp add: fps_eq_iff)
   801 
   802 lemma fps_cutoff_zero [simp]: "fps_cutoff n 0 = 0"
   803   by (simp add: fps_eq_iff)
   804 
   805 lemma fps_cutoff_one: "fps_cutoff n 1 = (if n = 0 then 0 else 1)"
   806   by (simp add: fps_eq_iff)
   807 
   808 lemma fps_cutoff_fps_const: "fps_cutoff n (fps_const c) = (if n = 0 then 0 else fps_const c)"
   809   by (simp add: fps_eq_iff)
   810 
   811 lemma fps_cutoff_numeral: "fps_cutoff n (numeral c) = (if n = 0 then 0 else numeral c)"
   812   by (simp add: numeral_fps_const fps_cutoff_fps_const)
   813 
   814 lemma fps_shift_cutoff:
   815   "fps_shift n (f :: ('a :: comm_ring_1) fps) * X^n + fps_cutoff n f = f"
   816   by (simp add: fps_eq_iff X_power_mult_right_nth)
   817 
   818 
   819 subsection \<open>Formal Power series form a metric space\<close>
   820 
   821 definition (in dist) "ball x r = {y. dist y x < r}"
   822 
   823 instantiation fps :: (comm_ring_1) dist
   824 begin
   825 
   826 definition
   827   dist_fps_def: "dist (a :: 'a fps) b = (if a = b then 0 else inverse (2 ^ subdegree (a - b)))"
   828 
   829 lemma dist_fps_ge0: "dist (a :: 'a fps) b \<ge> 0"
   830   by (simp add: dist_fps_def)
   831 
   832 lemma dist_fps_sym: "dist (a :: 'a fps) b = dist b a"
   833   by (simp add: dist_fps_def)
   834 
   835 instance ..
   836 
   837 end
   838 
   839 instantiation fps :: (comm_ring_1) metric_space
   840 begin
   841 
   842 definition uniformity_fps_def [code del]:
   843   "(uniformity :: ('a fps \<times> 'a fps) filter) = (INF e:{0 <..}. principal {(x, y). dist x y < e})"
   844 
   845 definition open_fps_def' [code del]:
   846   "open (U :: 'a fps set) \<longleftrightarrow> (\<forall>x\<in>U. eventually (\<lambda>(x', y). x' = x \<longrightarrow> y \<in> U) uniformity)"
   847 
   848 instance
   849 proof
   850   show th: "dist a b = 0 \<longleftrightarrow> a = b" for a b :: "'a fps"
   851     by (simp add: dist_fps_def split: if_split_asm)
   852   then have th'[simp]: "dist a a = 0" for a :: "'a fps" by simp
   853 
   854   fix a b c :: "'a fps"
   855   consider "a = b" | "c = a \<or> c = b" | "a \<noteq> b" "a \<noteq> c" "b \<noteq> c" by blast
   856   then show "dist a b \<le> dist a c + dist b c"
   857   proof cases
   858     case 1
   859     then show ?thesis by (simp add: dist_fps_def)
   860   next
   861     case 2
   862     then show ?thesis
   863       by (cases "c = a") (simp_all add: th dist_fps_sym)
   864   next
   865     case neq: 3
   866     have False if "dist a b > dist a c + dist b c"
   867     proof -
   868       let ?n = "subdegree (a - b)"
   869       from neq have "dist a b > 0" "dist b c > 0" and "dist a c > 0" by (simp_all add: dist_fps_def)
   870       with that have "dist a b > dist a c" and "dist a b > dist b c" by simp_all
   871       with neq have "?n < subdegree (a - c)" and "?n < subdegree (b - c)"
   872         by (simp_all add: dist_fps_def field_simps)
   873       hence "(a - c) $ ?n = 0" and "(b - c) $ ?n = 0"
   874         by (simp_all only: nth_less_subdegree_zero)
   875       hence "(a - b) $ ?n = 0" by simp
   876       moreover from neq have "(a - b) $ ?n \<noteq> 0" by (intro nth_subdegree_nonzero) simp_all
   877       ultimately show False by contradiction
   878     qed
   879     thus ?thesis by (auto simp add: not_le[symmetric])
   880   qed
   881 qed (rule open_fps_def' uniformity_fps_def)+
   882 
   883 end
   884 
   885 declare uniformity_Abort[where 'a="'a :: comm_ring_1 fps", code]
   886 
   887 lemma open_fps_def: "open (S :: 'a::comm_ring_1 fps set) = (\<forall>a \<in> S. \<exists>r. r >0 \<and> ball a r \<subseteq> S)"
   888   unfolding open_dist ball_def subset_eq by simp
   889 
   890 text \<open>The infinite sums and justification of the notation in textbooks.\<close>
   891 
   892 lemma reals_power_lt_ex:
   893   fixes x y :: real
   894   assumes xp: "x > 0"
   895     and y1: "y > 1"
   896   shows "\<exists>k>0. (1/y)^k < x"
   897 proof -
   898   have yp: "y > 0"
   899     using y1 by simp
   900   from reals_Archimedean2[of "max 0 (- log y x) + 1"]
   901   obtain k :: nat where k: "real k > max 0 (- log y x) + 1"
   902     by blast
   903   from k have kp: "k > 0"
   904     by simp
   905   from k have "real k > - log y x"
   906     by simp
   907   then have "ln y * real k > - ln x"
   908     unfolding log_def
   909     using ln_gt_zero_iff[OF yp] y1
   910     by (simp add: minus_divide_left field_simps del: minus_divide_left[symmetric])
   911   then have "ln y * real k + ln x > 0"
   912     by simp
   913   then have "exp (real k * ln y + ln x) > exp 0"
   914     by (simp add: ac_simps)
   915   then have "y ^ k * x > 1"
   916     unfolding exp_zero exp_add exp_of_nat_mult exp_ln [OF xp] exp_ln [OF yp]
   917     by simp
   918   then have "x > (1 / y)^k" using yp
   919     by (simp add: field_simps)
   920   then show ?thesis
   921     using kp by blast
   922 qed
   923 
   924 lemma fps_sum_rep_nth: "(sum (\<lambda>i. fps_const(a$i)*X^i) {0..m})$n =
   925     (if n \<le> m then a$n else 0::'a::comm_ring_1)"
   926   apply (auto simp add: fps_sum_nth cond_value_iff cong del: if_weak_cong)
   927   apply (simp add: sum.delta')
   928   done
   929 
   930 lemma fps_notation: "(\<lambda>n. sum (\<lambda>i. fps_const(a$i) * X^i) {0..n}) \<longlonglongrightarrow> a"
   931   (is "?s \<longlonglongrightarrow> a")
   932 proof -
   933   have "\<exists>n0. \<forall>n \<ge> n0. dist (?s n) a < r" if "r > 0" for r
   934   proof -
   935     obtain n0 where n0: "(1/2)^n0 < r" "n0 > 0"
   936       using reals_power_lt_ex[OF \<open>r > 0\<close>, of 2] by auto
   937     show ?thesis
   938     proof -
   939       have "dist (?s n) a < r" if nn0: "n \<ge> n0" for n
   940       proof -
   941         from that have thnn0: "(1/2)^n \<le> (1/2 :: real)^n0"
   942           by (simp add: divide_simps)
   943         show ?thesis
   944         proof (cases "?s n = a")
   945           case True
   946           then show ?thesis
   947             unfolding dist_eq_0_iff[of "?s n" a, symmetric]
   948             using \<open>r > 0\<close> by (simp del: dist_eq_0_iff)
   949         next
   950           case False
   951           from False have dth: "dist (?s n) a = (1/2)^subdegree (?s n - a)"
   952             by (simp add: dist_fps_def field_simps)
   953           from False have kn: "subdegree (?s n - a) > n"
   954             by (intro subdegree_greaterI) (simp_all add: fps_sum_rep_nth)
   955           then have "dist (?s n) a < (1/2)^n"
   956             by (simp add: field_simps dist_fps_def)
   957           also have "\<dots> \<le> (1/2)^n0"
   958             using nn0 by (simp add: divide_simps)
   959           also have "\<dots> < r"
   960             using n0 by simp
   961           finally show ?thesis .
   962         qed
   963       qed
   964       then show ?thesis by blast
   965     qed
   966   qed
   967   then show ?thesis
   968     unfolding lim_sequentially by blast
   969 qed
   970 
   971 
   972 subsection \<open>Inverses of formal power series\<close>
   973 
   974 declare sum.cong[fundef_cong]
   975 
   976 instantiation fps :: ("{comm_monoid_add,inverse,times,uminus}") inverse
   977 begin
   978 
   979 fun natfun_inverse:: "'a fps \<Rightarrow> nat \<Rightarrow> 'a"
   980 where
   981   "natfun_inverse f 0 = inverse (f$0)"
   982 | "natfun_inverse f n = - inverse (f$0) * sum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}"
   983 
   984 definition fps_inverse_def: "inverse f = (if f $ 0 = 0 then 0 else Abs_fps (natfun_inverse f))"
   985 
   986 definition fps_divide_def:
   987   "f div g = (if g = 0 then 0 else
   988      let n = subdegree g; h = fps_shift n g
   989      in  fps_shift n (f * inverse h))"
   990 
   991 instance ..
   992 
   993 end
   994 
   995 lemma fps_inverse_zero [simp]:
   996   "inverse (0 :: 'a::{comm_monoid_add,inverse,times,uminus} fps) = 0"
   997   by (simp add: fps_ext fps_inverse_def)
   998 
   999 lemma fps_inverse_one [simp]: "inverse (1 :: 'a::{division_ring,zero_neq_one} fps) = 1"
  1000   apply (auto simp add: expand_fps_eq fps_inverse_def)
  1001   apply (case_tac n)
  1002   apply auto
  1003   done
  1004 
  1005 lemma inverse_mult_eq_1 [intro]:
  1006   assumes f0: "f$0 \<noteq> (0::'a::field)"
  1007   shows "inverse f * f = 1"
  1008 proof -
  1009   have c: "inverse f * f = f * inverse f"
  1010     by (simp add: mult.commute)
  1011   from f0 have ifn: "\<And>n. inverse f $ n = natfun_inverse f n"
  1012     by (simp add: fps_inverse_def)
  1013   from f0 have th0: "(inverse f * f) $ 0 = 1"
  1014     by (simp add: fps_mult_nth fps_inverse_def)
  1015   have "(inverse f * f)$n = 0" if np: "n > 0" for n
  1016   proof -
  1017     from np have eq: "{0..n} = {0} \<union> {1 .. n}"
  1018       by auto
  1019     have d: "{0} \<inter> {1 .. n} = {}"
  1020       by auto
  1021     from f0 np have th0: "- (inverse f $ n) =
  1022       (sum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}) / (f$0)"
  1023       by (cases n) (simp_all add: divide_inverse fps_inverse_def)
  1024     from th0[symmetric, unfolded nonzero_divide_eq_eq[OF f0]]
  1025     have th1: "sum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n} = - (f$0) * (inverse f)$n"
  1026       by (simp add: field_simps)
  1027     have "(f * inverse f) $ n = (\<Sum>i = 0..n. f $i * natfun_inverse f (n - i))"
  1028       unfolding fps_mult_nth ifn ..
  1029     also have "\<dots> = f$0 * natfun_inverse f n + (\<Sum>i = 1..n. f$i * natfun_inverse f (n-i))"
  1030       by (simp add: eq)
  1031     also have "\<dots> = 0"
  1032       unfolding th1 ifn by simp
  1033     finally show ?thesis unfolding c .
  1034   qed
  1035   with th0 show ?thesis
  1036     by (simp add: fps_eq_iff)
  1037 qed
  1038 
  1039 lemma fps_inverse_0_iff[simp]: "(inverse f) $ 0 = (0::'a::division_ring) \<longleftrightarrow> f $ 0 = 0"
  1040   by (simp add: fps_inverse_def nonzero_imp_inverse_nonzero)
  1041 
  1042 lemma fps_inverse_nth_0 [simp]: "inverse f $ 0 = inverse (f $ 0 :: 'a :: division_ring)"
  1043   by (simp add: fps_inverse_def)
  1044 
  1045 lemma fps_inverse_eq_0_iff[simp]: "inverse f = (0:: ('a::division_ring) fps) \<longleftrightarrow> f $ 0 = 0"
  1046 proof
  1047   assume A: "inverse f = 0"
  1048   have "0 = inverse f $ 0" by (subst A) simp
  1049   thus "f $ 0 = 0" by simp
  1050 qed (simp add: fps_inverse_def)
  1051 
  1052 lemma fps_inverse_idempotent[intro, simp]:
  1053   assumes f0: "f$0 \<noteq> (0::'a::field)"
  1054   shows "inverse (inverse f) = f"
  1055 proof -
  1056   from f0 have if0: "inverse f $ 0 \<noteq> 0" by simp
  1057   from inverse_mult_eq_1[OF f0] inverse_mult_eq_1[OF if0]
  1058   have "inverse f * f = inverse f * inverse (inverse f)"
  1059     by (simp add: ac_simps)
  1060   then show ?thesis
  1061     using f0 unfolding mult_cancel_left by simp
  1062 qed
  1063 
  1064 lemma fps_inverse_unique:
  1065   assumes fg: "(f :: 'a :: field fps) * g = 1"
  1066   shows   "inverse f = g"
  1067 proof -
  1068   have f0: "f $ 0 \<noteq> 0"
  1069   proof
  1070     assume "f $ 0 = 0"
  1071     hence "0 = (f * g) $ 0" by simp
  1072     also from fg have "(f * g) $ 0 = 1" by simp
  1073     finally show False by simp
  1074   qed
  1075   from inverse_mult_eq_1[OF this] fg
  1076   have th0: "inverse f * f = g * f"
  1077     by (simp add: ac_simps)
  1078   then show ?thesis
  1079     using f0
  1080     unfolding mult_cancel_right
  1081     by (auto simp add: expand_fps_eq)
  1082 qed
  1083 
  1084 lemma fps_inverse_eq_0: "f$0 = 0 \<Longrightarrow> inverse (f :: 'a :: division_ring fps) = 0"
  1085   by simp
  1086   
  1087 lemma sum_zero_lemma:
  1088   fixes n::nat
  1089   assumes "0 < n"
  1090   shows "(\<Sum>i = 0..n. if n = i then 1 else if n - i = 1 then - 1 else 0) = (0::'a::field)"
  1091 proof -
  1092   let ?f = "\<lambda>i. if n = i then 1 else if n - i = 1 then - 1 else 0"
  1093   let ?g = "\<lambda>i. if i = n then 1 else if i = n - 1 then - 1 else 0"
  1094   let ?h = "\<lambda>i. if i=n - 1 then - 1 else 0"
  1095   have th1: "sum ?f {0..n} = sum ?g {0..n}"
  1096     by (rule sum.cong) auto
  1097   have th2: "sum ?g {0..n - 1} = sum ?h {0..n - 1}"
  1098     apply (rule sum.cong)
  1099     using assms
  1100     apply auto
  1101     done
  1102   have eq: "{0 .. n} = {0.. n - 1} \<union> {n}"
  1103     by auto
  1104   from assms have d: "{0.. n - 1} \<inter> {n} = {}"
  1105     by auto
  1106   have f: "finite {0.. n - 1}" "finite {n}"
  1107     by auto
  1108   show ?thesis
  1109     unfolding th1
  1110     apply (simp add: sum.union_disjoint[OF f d, unfolded eq[symmetric]] del: One_nat_def)
  1111     unfolding th2
  1112     apply (simp add: sum.delta)
  1113     done
  1114 qed
  1115 
  1116 lemma fps_inverse_mult: "inverse (f * g :: 'a::field fps) = inverse f * inverse g"
  1117 proof (cases "f$0 = 0 \<or> g$0 = 0")
  1118   assume "\<not>(f$0 = 0 \<or> g$0 = 0)"
  1119   hence [simp]: "f$0 \<noteq> 0" "g$0 \<noteq> 0" by simp_all
  1120   show ?thesis
  1121   proof (rule fps_inverse_unique)
  1122     have "f * g * (inverse f * inverse g) = (inverse f * f) * (inverse g * g)" by simp
  1123     also have "... = 1" by (subst (1 2) inverse_mult_eq_1) simp_all
  1124     finally show "f * g * (inverse f * inverse g) = 1" .
  1125   qed
  1126 next
  1127   assume A: "f$0 = 0 \<or> g$0 = 0"
  1128   hence "inverse (f * g) = 0" by simp
  1129   also from A have "... = inverse f * inverse g" by auto
  1130   finally show "inverse (f * g) = inverse f * inverse g" .
  1131 qed
  1132 
  1133 
  1134 lemma fps_inverse_gp: "inverse (Abs_fps(\<lambda>n. (1::'a::field))) =
  1135     Abs_fps (\<lambda>n. if n= 0 then 1 else if n=1 then - 1 else 0)"
  1136   apply (rule fps_inverse_unique)
  1137   apply (simp_all add: fps_eq_iff fps_mult_nth sum_zero_lemma)
  1138   done
  1139 
  1140 lemma subdegree_inverse [simp]: "subdegree (inverse (f::'a::field fps)) = 0"
  1141 proof (cases "f$0 = 0")
  1142   assume nz: "f$0 \<noteq> 0"
  1143   hence "subdegree (inverse f) + subdegree f = subdegree (inverse f * f)"
  1144     by (subst subdegree_mult) auto
  1145   also from nz have "subdegree f = 0" by (simp add: subdegree_eq_0_iff)
  1146   also from nz have "inverse f * f = 1" by (rule inverse_mult_eq_1)
  1147   finally show "subdegree (inverse f) = 0" by simp
  1148 qed (simp_all add: fps_inverse_def)
  1149 
  1150 lemma fps_is_unit_iff [simp]: "(f :: 'a :: field fps) dvd 1 \<longleftrightarrow> f $ 0 \<noteq> 0"
  1151 proof
  1152   assume "f dvd 1"
  1153   then obtain g where "1 = f * g" by (elim dvdE)
  1154   from this[symmetric] have "(f*g) $ 0 = 1" by simp
  1155   thus "f $ 0 \<noteq> 0" by auto
  1156 next
  1157   assume A: "f $ 0 \<noteq> 0"
  1158   thus "f dvd 1" by (simp add: inverse_mult_eq_1[OF A, symmetric])
  1159 qed
  1160 
  1161 lemma subdegree_eq_0' [simp]: "(f :: 'a :: field fps) dvd 1 \<Longrightarrow> subdegree f = 0"
  1162   by simp
  1163 
  1164 lemma fps_unit_dvd [simp]: "(f $ 0 :: 'a :: field) \<noteq> 0 \<Longrightarrow> f dvd g"
  1165   by (rule dvd_trans, subst fps_is_unit_iff) simp_all
  1166 
  1167 instantiation fps :: (field) normalization_semidom
  1168 begin
  1169 
  1170 definition fps_unit_factor_def [simp]:
  1171   "unit_factor f = fps_shift (subdegree f) f"
  1172 
  1173 definition fps_normalize_def [simp]:
  1174   "normalize f = (if f = 0 then 0 else X ^ subdegree f)"
  1175 
  1176 instance proof
  1177   fix f :: "'a fps"
  1178   show "unit_factor f * normalize f = f"
  1179     by (simp add: fps_shift_times_X_power)
  1180 next
  1181   fix f g :: "'a fps"
  1182   show "unit_factor (f * g) = unit_factor f * unit_factor g"
  1183   proof (cases "f = 0 \<or> g = 0")
  1184     assume "\<not>(f = 0 \<or> g = 0)"
  1185     thus "unit_factor (f * g) = unit_factor f * unit_factor g"
  1186     unfolding fps_unit_factor_def
  1187       by (auto simp: fps_shift_fps_shift fps_shift_mult fps_shift_mult_right)
  1188   qed auto
  1189 next
  1190   fix f g :: "'a fps"
  1191   assume "g \<noteq> 0"
  1192   then have "f * (fps_shift (subdegree g) g * inverse (fps_shift (subdegree g) g)) = f"
  1193     by (metis add_cancel_right_left fps_shift_nth inverse_mult_eq_1 mult.commute mult_cancel_left2 nth_subdegree_nonzero)
  1194   then have "fps_shift (subdegree g) (g * (f * inverse (fps_shift (subdegree g) g))) = f"
  1195     by (simp add: fps_shift_mult_right mult.commute)
  1196   with \<open>g \<noteq> 0\<close> show "f * g / g = f"
  1197     by (simp add: fps_divide_def Let_def ac_simps)
  1198 qed (auto simp add: fps_divide_def Let_def)
  1199 
  1200 end
  1201 
  1202 instantiation fps :: (field) ring_div
  1203 begin
  1204 
  1205 definition fps_mod_def:
  1206   "f mod g = (if g = 0 then f else
  1207      let n = subdegree g; h = fps_shift n g
  1208      in  fps_cutoff n (f * inverse h) * h)"
  1209 
  1210 lemma fps_mod_eq_zero:
  1211   assumes "g \<noteq> 0" and "subdegree f \<ge> subdegree g"
  1212   shows   "f mod g = 0"
  1213   using assms by (cases "f = 0") (auto simp: fps_cutoff_zero_iff fps_mod_def Let_def)
  1214 
  1215 lemma fps_times_divide_eq:
  1216   assumes "g \<noteq> 0" and "subdegree f \<ge> subdegree (g :: 'a fps)"
  1217   shows   "f div g * g = f"
  1218 proof (cases "f = 0")
  1219   assume nz: "f \<noteq> 0"
  1220   define n where "n = subdegree g"
  1221   define h where "h = fps_shift n g"
  1222   from assms have [simp]: "h $ 0 \<noteq> 0" unfolding h_def by (simp add: n_def)
  1223 
  1224   from assms nz have "f div g * g = fps_shift n (f * inverse h) * g"
  1225     by (simp add: fps_divide_def Let_def h_def n_def)
  1226   also have "... = fps_shift n (f * inverse h) * X^n * h" unfolding h_def n_def
  1227     by (subst subdegree_decompose[of g]) simp
  1228   also have "fps_shift n (f * inverse h) * X^n = f * inverse h"
  1229     by (rule fps_shift_times_X_power) (simp_all add: nz assms n_def)
  1230   also have "... * h = f * (inverse h * h)" by simp
  1231   also have "inverse h * h = 1" by (rule inverse_mult_eq_1) simp
  1232   finally show ?thesis by simp
  1233 qed (simp_all add: fps_divide_def Let_def)
  1234 
  1235 lemma
  1236   assumes "g$0 \<noteq> 0"
  1237   shows   fps_divide_unit: "f div g = f * inverse g" and fps_mod_unit [simp]: "f mod g = 0"
  1238 proof -
  1239   from assms have [simp]: "subdegree g = 0" by (simp add: subdegree_eq_0_iff)
  1240   from assms show "f div g = f * inverse g"
  1241     by (auto simp: fps_divide_def Let_def subdegree_eq_0_iff)
  1242   from assms show "f mod g = 0" by (intro fps_mod_eq_zero) auto
  1243 qed
  1244 
  1245 context
  1246 begin
  1247 private lemma fps_divide_cancel_aux1:
  1248   assumes "h$0 \<noteq> (0 :: 'a :: field)"
  1249   shows   "(h * f) div (h * g) = f div g"
  1250 proof (cases "g = 0")
  1251   assume "g \<noteq> 0"
  1252   from assms have "h \<noteq> 0" by auto
  1253   note nz [simp] = \<open>g \<noteq> 0\<close> \<open>h \<noteq> 0\<close>
  1254   from assms have [simp]: "subdegree h = 0" by (simp add: subdegree_eq_0_iff)
  1255 
  1256   have "(h * f) div (h * g) =
  1257           fps_shift (subdegree g) (h * f * inverse (fps_shift (subdegree g) (h*g)))"
  1258     by (simp add: fps_divide_def Let_def)
  1259   also have "h * f * inverse (fps_shift (subdegree g) (h*g)) =
  1260                (inverse h * h) * f * inverse (fps_shift (subdegree g) g)"
  1261     by (subst fps_shift_mult) (simp_all add: algebra_simps fps_inverse_mult)
  1262   also from assms have "inverse h * h = 1" by (rule inverse_mult_eq_1)
  1263   finally show "(h * f) div (h * g) = f div g" by (simp_all add: fps_divide_def Let_def)
  1264 qed (simp_all add: fps_divide_def)
  1265 
  1266 private lemma fps_divide_cancel_aux2:
  1267   "(f * X^m) div (g * X^m) = f div (g :: 'a :: field fps)"
  1268 proof (cases "g = 0")
  1269   assume [simp]: "g \<noteq> 0"
  1270   have "(f * X^m) div (g * X^m) =
  1271           fps_shift (subdegree g + m) (f*inverse (fps_shift (subdegree g + m) (g*X^m))*X^m)"
  1272     by (simp add: fps_divide_def Let_def algebra_simps)
  1273   also have "... = f div g"
  1274     by (simp add: fps_shift_times_X_power'' fps_divide_def Let_def)
  1275   finally show ?thesis .
  1276 qed (simp_all add: fps_divide_def)
  1277 
  1278 instance proof
  1279   fix f g :: "'a fps"
  1280   define n where "n = subdegree g"
  1281   define h where "h = fps_shift n g"
  1282 
  1283   show "f div g * g + f mod g = f"
  1284   proof (cases "g = 0 \<or> f = 0")
  1285     assume "\<not>(g = 0 \<or> f = 0)"
  1286     hence nz [simp]: "f \<noteq> 0" "g \<noteq> 0" by simp_all
  1287     show ?thesis
  1288     proof (rule disjE[OF le_less_linear])
  1289       assume "subdegree f \<ge> subdegree g"
  1290       with nz show ?thesis by (simp add: fps_mod_eq_zero fps_times_divide_eq)
  1291     next
  1292       assume "subdegree f < subdegree g"
  1293       have g_decomp: "g = h * X^n" unfolding h_def n_def by (rule subdegree_decompose)
  1294       have "f div g * g + f mod g =
  1295               fps_shift n (f * inverse h) * g + fps_cutoff n (f * inverse h) * h"
  1296         by (simp add: fps_mod_def fps_divide_def Let_def n_def h_def)
  1297       also have "... = h * (fps_shift n (f * inverse h) * X^n + fps_cutoff n (f * inverse h))"
  1298         by (subst g_decomp) (simp add: algebra_simps)
  1299       also have "... = f * (inverse h * h)"
  1300         by (subst fps_shift_cutoff) simp
  1301       also have "inverse h * h = 1" by (rule inverse_mult_eq_1) (simp add: h_def n_def)
  1302       finally show ?thesis by simp
  1303     qed
  1304   qed (auto simp: fps_mod_def fps_divide_def Let_def)
  1305 next
  1306 
  1307   fix f g h :: "'a fps"
  1308   assume "h \<noteq> 0"
  1309   show "(h * f) div (h * g) = f div g"
  1310   proof -
  1311     define m where "m = subdegree h"
  1312     define h' where "h' = fps_shift m h"
  1313     have h_decomp: "h = h' * X ^ m" unfolding h'_def m_def by (rule subdegree_decompose)
  1314     from \<open>h \<noteq> 0\<close> have [simp]: "h'$0 \<noteq> 0" by (simp add: h'_def m_def)
  1315     have "(h * f) div (h * g) = (h' * f * X^m) div (h' * g * X^m)"
  1316       by (simp add: h_decomp algebra_simps)
  1317     also have "... = f div g" by (simp add: fps_divide_cancel_aux1 fps_divide_cancel_aux2)
  1318     finally show ?thesis .
  1319   qed
  1320 
  1321 next
  1322   fix f g h :: "'a fps"
  1323   assume [simp]: "h \<noteq> 0"
  1324   define n h' where dfs: "n = subdegree h" "h' = fps_shift n h"
  1325   have "(f + g * h) div h = fps_shift n (f * inverse h') + fps_shift n (g * (h * inverse h'))"
  1326     by (simp add: fps_divide_def Let_def dfs[symmetric] algebra_simps fps_shift_add)
  1327   also have "h * inverse h' = (inverse h' * h') * X^n"
  1328     by (subst subdegree_decompose) (simp_all add: dfs)
  1329   also have "... = X^n" by (subst inverse_mult_eq_1) (simp_all add: dfs)
  1330   also have "fps_shift n (g * X^n) = g" by simp
  1331   also have "fps_shift n (f * inverse h') = f div h"
  1332     by (simp add: fps_divide_def Let_def dfs)
  1333   finally show "(f + g * h) div h = g + f div h" by simp
  1334 qed
  1335 
  1336 end
  1337 end
  1338 
  1339 lemma subdegree_mod:
  1340   assumes "f \<noteq> 0" "subdegree f < subdegree g"
  1341   shows   "subdegree (f mod g) = subdegree f"
  1342 proof (cases "f div g * g = 0")
  1343   assume "f div g * g \<noteq> 0"
  1344   hence [simp]: "f div g \<noteq> 0" "g \<noteq> 0" by auto
  1345   from div_mult_mod_eq[of f g] have "f mod g = f - f div g * g" by (simp add: algebra_simps)
  1346   also from assms have "subdegree ... = subdegree f"
  1347     by (intro subdegree_diff_eq1) simp_all
  1348   finally show ?thesis .
  1349 next
  1350   assume zero: "f div g * g = 0"
  1351   from div_mult_mod_eq[of f g] have "f mod g = f - f div g * g" by (simp add: algebra_simps)
  1352   also note zero
  1353   finally show ?thesis by simp
  1354 qed
  1355 
  1356 lemma fps_divide_nth_0 [simp]: "g $ 0 \<noteq> 0 \<Longrightarrow> (f div g) $ 0 = f $ 0 / (g $ 0 :: _ :: field)"
  1357   by (simp add: fps_divide_unit divide_inverse)
  1358 
  1359 
  1360 lemma dvd_imp_subdegree_le:
  1361   "(f :: 'a :: idom fps) dvd g \<Longrightarrow> g \<noteq> 0 \<Longrightarrow> subdegree f \<le> subdegree g"
  1362   by (auto elim: dvdE)
  1363 
  1364 lemma fps_dvd_iff:
  1365   assumes "(f :: 'a :: field fps) \<noteq> 0" "g \<noteq> 0"
  1366   shows   "f dvd g \<longleftrightarrow> subdegree f \<le> subdegree g"
  1367 proof
  1368   assume "subdegree f \<le> subdegree g"
  1369   with assms have "g mod f = 0"
  1370     by (simp add: fps_mod_def Let_def fps_cutoff_zero_iff)
  1371   thus "f dvd g" by (simp add: dvd_eq_mod_eq_0)
  1372 qed (simp add: assms dvd_imp_subdegree_le)
  1373 
  1374 lemma fps_shift_altdef:
  1375   "fps_shift n f = (f :: 'a :: field fps) div X^n"
  1376   by (simp add: fps_divide_def)
  1377   
  1378 lemma fps_div_X_power_nth: "((f :: 'a :: field fps) div X^n) $ k = f $ (k + n)"
  1379   by (simp add: fps_shift_altdef [symmetric])
  1380 
  1381 lemma fps_div_X_nth: "((f :: 'a :: field fps) div X) $ k = f $ Suc k"
  1382   using fps_div_X_power_nth[of f 1] by simp
  1383 
  1384 lemma fps_const_inverse: "inverse (fps_const (a::'a::field)) = fps_const (inverse a)"
  1385   by (cases "a \<noteq> 0", rule fps_inverse_unique) (auto simp: fps_eq_iff)
  1386 
  1387 lemma fps_const_divide: "fps_const (x :: _ :: field) / fps_const y = fps_const (x / y)"
  1388   by (cases "y = 0") (simp_all add: fps_divide_unit fps_const_inverse divide_inverse)
  1389 
  1390 lemma inverse_fps_numeral:
  1391   "inverse (numeral n :: ('a :: field_char_0) fps) = fps_const (inverse (numeral n))"
  1392   by (intro fps_inverse_unique fps_ext) (simp_all add: fps_numeral_nth)
  1393 
  1394 lemma fps_numeral_divide_divide:
  1395   "x / numeral b / numeral c = (x / numeral (b * c) :: 'a :: field fps)"
  1396   by (cases "numeral b = (0::'a)"; cases "numeral c = (0::'a)")
  1397       (simp_all add: fps_divide_unit fps_inverse_mult [symmetric] numeral_fps_const numeral_mult 
  1398                 del: numeral_mult [symmetric])
  1399 
  1400 lemma fps_numeral_mult_divide:
  1401   "numeral b * x / numeral c = (numeral b / numeral c * x :: 'a :: field fps)"
  1402   by (cases "numeral c = (0::'a)") (simp_all add: fps_divide_unit numeral_fps_const)
  1403 
  1404 lemmas fps_numeral_simps = 
  1405   fps_numeral_divide_divide fps_numeral_mult_divide inverse_fps_numeral neg_numeral_fps_const
  1406 
  1407 
  1408 subsection \<open>Formal power series form a Euclidean ring\<close>
  1409 
  1410 instantiation fps :: (field) euclidean_ring_cancel
  1411 begin
  1412 
  1413 definition fps_euclidean_size_def:
  1414   "euclidean_size f = (if f = 0 then 0 else 2 ^ subdegree f)"
  1415 
  1416 instance proof
  1417   fix f g :: "'a fps" assume [simp]: "g \<noteq> 0"
  1418   show "euclidean_size f \<le> euclidean_size (f * g)"
  1419     by (cases "f = 0") (auto simp: fps_euclidean_size_def)
  1420   show "euclidean_size (f mod g) < euclidean_size g"
  1421     apply (cases "f = 0", simp add: fps_euclidean_size_def)
  1422     apply (rule disjE[OF le_less_linear[of "subdegree g" "subdegree f"]])
  1423     apply (simp_all add: fps_mod_eq_zero fps_euclidean_size_def subdegree_mod)
  1424     done
  1425 qed (simp_all add: fps_euclidean_size_def)
  1426 
  1427 end
  1428 
  1429 instantiation fps :: (field) euclidean_ring_gcd
  1430 begin
  1431 definition fps_gcd_def: "(gcd :: 'a fps \<Rightarrow> _) = Euclidean_Algorithm.gcd"
  1432 definition fps_lcm_def: "(lcm :: 'a fps \<Rightarrow> _) = Euclidean_Algorithm.lcm"
  1433 definition fps_Gcd_def: "(Gcd :: 'a fps set \<Rightarrow> _) = Euclidean_Algorithm.Gcd"
  1434 definition fps_Lcm_def: "(Lcm :: 'a fps set \<Rightarrow> _) = Euclidean_Algorithm.Lcm"
  1435 instance by standard (simp_all add: fps_gcd_def fps_lcm_def fps_Gcd_def fps_Lcm_def)
  1436 end
  1437 
  1438 lemma fps_gcd:
  1439   assumes [simp]: "f \<noteq> 0" "g \<noteq> 0"
  1440   shows   "gcd f g = X ^ min (subdegree f) (subdegree g)"
  1441 proof -
  1442   let ?m = "min (subdegree f) (subdegree g)"
  1443   show "gcd f g = X ^ ?m"
  1444   proof (rule sym, rule gcdI)
  1445     fix d assume "d dvd f" "d dvd g"
  1446     thus "d dvd X ^ ?m" by (cases "d = 0") (auto simp: fps_dvd_iff)
  1447   qed (simp_all add: fps_dvd_iff)
  1448 qed
  1449 
  1450 lemma fps_gcd_altdef: "gcd (f :: 'a :: field fps) g =
  1451   (if f = 0 \<and> g = 0 then 0 else
  1452    if f = 0 then X ^ subdegree g else
  1453    if g = 0 then X ^ subdegree f else
  1454      X ^ min (subdegree f) (subdegree g))"
  1455   by (simp add: fps_gcd)
  1456 
  1457 lemma fps_lcm:
  1458   assumes [simp]: "f \<noteq> 0" "g \<noteq> 0"
  1459   shows   "lcm f g = X ^ max (subdegree f) (subdegree g)"
  1460 proof -
  1461   let ?m = "max (subdegree f) (subdegree g)"
  1462   show "lcm f g = X ^ ?m"
  1463   proof (rule sym, rule lcmI)
  1464     fix d assume "f dvd d" "g dvd d"
  1465     thus "X ^ ?m dvd d" by (cases "d = 0") (auto simp: fps_dvd_iff)
  1466   qed (simp_all add: fps_dvd_iff)
  1467 qed
  1468 
  1469 lemma fps_lcm_altdef: "lcm (f :: 'a :: field fps) g =
  1470   (if f = 0 \<or> g = 0 then 0 else X ^ max (subdegree f) (subdegree g))"
  1471   by (simp add: fps_lcm)
  1472 
  1473 lemma fps_Gcd:
  1474   assumes "A - {0} \<noteq> {}"
  1475   shows   "Gcd A = X ^ (INF f:A-{0}. subdegree f)"
  1476 proof (rule sym, rule GcdI)
  1477   fix f assume "f \<in> A"
  1478   thus "X ^ (INF f:A - {0}. subdegree f) dvd f"
  1479     by (cases "f = 0") (auto simp: fps_dvd_iff intro!: cINF_lower)
  1480 next
  1481   fix d assume d: "\<And>f. f \<in> A \<Longrightarrow> d dvd f"
  1482   from assms obtain f where "f \<in> A - {0}" by auto
  1483   with d[of f] have [simp]: "d \<noteq> 0" by auto
  1484   from d assms have "subdegree d \<le> (INF f:A-{0}. subdegree f)"
  1485     by (intro cINF_greatest) (auto simp: fps_dvd_iff[symmetric])
  1486   with d assms show "d dvd X ^ (INF f:A-{0}. subdegree f)" by (simp add: fps_dvd_iff)
  1487 qed simp_all
  1488 
  1489 lemma fps_Gcd_altdef: "Gcd (A :: 'a :: field fps set) =
  1490   (if A \<subseteq> {0} then 0 else X ^ (INF f:A-{0}. subdegree f))"
  1491   using fps_Gcd by auto
  1492 
  1493 lemma fps_Lcm:
  1494   assumes "A \<noteq> {}" "0 \<notin> A" "bdd_above (subdegree`A)"
  1495   shows   "Lcm A = X ^ (SUP f:A. subdegree f)"
  1496 proof (rule sym, rule LcmI)
  1497   fix f assume "f \<in> A"
  1498   moreover from assms(3) have "bdd_above (subdegree ` A)" by auto
  1499   ultimately show "f dvd X ^ (SUP f:A. subdegree f)" using assms(2)
  1500     by (cases "f = 0") (auto simp: fps_dvd_iff intro!: cSUP_upper)
  1501 next
  1502   fix d assume d: "\<And>f. f \<in> A \<Longrightarrow> f dvd d"
  1503   from assms obtain f where f: "f \<in> A" "f \<noteq> 0" by auto
  1504   show "X ^ (SUP f:A. subdegree f) dvd d"
  1505   proof (cases "d = 0")
  1506     assume "d \<noteq> 0"
  1507     moreover from d have "\<And>f. f \<in> A \<Longrightarrow> f \<noteq> 0 \<Longrightarrow> f dvd d" by blast
  1508     ultimately have "subdegree d \<ge> (SUP f:A. subdegree f)" using assms
  1509       by (intro cSUP_least) (auto simp: fps_dvd_iff)
  1510     with \<open>d \<noteq> 0\<close> show ?thesis by (simp add: fps_dvd_iff)
  1511   qed simp_all
  1512 qed simp_all
  1513 
  1514 lemma fps_Lcm_altdef:
  1515   "Lcm (A :: 'a :: field fps set) =
  1516      (if 0 \<in> A \<or> \<not>bdd_above (subdegree`A) then 0 else
  1517       if A = {} then 1 else X ^ (SUP f:A. subdegree f))"
  1518 proof (cases "bdd_above (subdegree`A)")
  1519   assume unbounded: "\<not>bdd_above (subdegree`A)"
  1520   have "Lcm A = 0"
  1521   proof (rule ccontr)
  1522     assume "Lcm A \<noteq> 0"
  1523     from unbounded obtain f where f: "f \<in> A" "subdegree (Lcm A) < subdegree f"
  1524       unfolding bdd_above_def by (auto simp: not_le)
  1525     moreover from f and \<open>Lcm A \<noteq> 0\<close> have "subdegree f \<le> subdegree (Lcm A)"
  1526       by (intro dvd_imp_subdegree_le dvd_Lcm) simp_all
  1527     ultimately show False by simp
  1528   qed
  1529   with unbounded show ?thesis by simp
  1530 qed (simp_all add: fps_Lcm Lcm_eq_0_I)
  1531 
  1532 
  1533 
  1534 subsection \<open>Formal Derivatives, and the MacLaurin theorem around 0\<close>
  1535 
  1536 definition "fps_deriv f = Abs_fps (\<lambda>n. of_nat (n + 1) * f $ (n + 1))"
  1537 
  1538 lemma fps_deriv_nth[simp]: "fps_deriv f $ n = of_nat (n +1) * f $ (n + 1)"
  1539   by (simp add: fps_deriv_def)
  1540 
  1541 lemma fps_0th_higher_deriv: 
  1542   "(fps_deriv ^^ n) f $ 0 = (fact n * f $ n :: 'a :: {comm_ring_1, semiring_char_0})"
  1543   by (induction n arbitrary: f) (simp_all del: funpow.simps add: funpow_Suc_right algebra_simps)
  1544 
  1545 lemma fps_deriv_linear[simp]:
  1546   "fps_deriv (fps_const (a::'a::comm_semiring_1) * f + fps_const b * g) =
  1547     fps_const a * fps_deriv f + fps_const b * fps_deriv g"
  1548   unfolding fps_eq_iff fps_add_nth  fps_const_mult_left fps_deriv_nth by (simp add: field_simps)
  1549 
  1550 lemma fps_deriv_mult[simp]:
  1551   fixes f :: "'a::comm_ring_1 fps"
  1552   shows "fps_deriv (f * g) = f * fps_deriv g + fps_deriv f * g"
  1553 proof -
  1554   let ?D = "fps_deriv"
  1555   have "(f * ?D g + ?D f * g) $ n = ?D (f*g) $ n" for n
  1556   proof -
  1557     let ?Zn = "{0 ..n}"
  1558     let ?Zn1 = "{0 .. n + 1}"
  1559     let ?g = "\<lambda>i. of_nat (i+1) * g $ (i+1) * f $ (n - i) +
  1560         of_nat (i+1)* f $ (i+1) * g $ (n - i)"
  1561     let ?h = "\<lambda>i. of_nat i * g $ i * f $ ((n+1) - i) +
  1562         of_nat i* f $ i * g $ ((n + 1) - i)"
  1563     have s0: "sum (\<lambda>i. of_nat i * f $ i * g $ (n + 1 - i)) ?Zn1 =
  1564       sum (\<lambda>i. of_nat (n + 1 - i) * f $ (n + 1 - i) * g $ i) ?Zn1"
  1565        by (rule sum.reindex_bij_witness[where i="op - (n + 1)" and j="op - (n + 1)"]) auto
  1566     have s1: "sum (\<lambda>i. f $ i * g $ (n + 1 - i)) ?Zn1 =
  1567       sum (\<lambda>i. f $ (n + 1 - i) * g $ i) ?Zn1"
  1568        by (rule sum.reindex_bij_witness[where i="op - (n + 1)" and j="op - (n + 1)"]) auto
  1569     have "(f * ?D g + ?D f * g)$n = (?D g * f + ?D f * g)$n"
  1570       by (simp only: mult.commute)
  1571     also have "\<dots> = (\<Sum>i = 0..n. ?g i)"
  1572       by (simp add: fps_mult_nth sum.distrib[symmetric])
  1573     also have "\<dots> = sum ?h {0..n+1}"
  1574       by (rule sum.reindex_bij_witness_not_neutral
  1575             [where S'="{}" and T'="{0}" and j="Suc" and i="\<lambda>i. i - 1"]) auto
  1576     also have "\<dots> = (fps_deriv (f * g)) $ n"
  1577       apply (simp only: fps_deriv_nth fps_mult_nth sum.distrib)
  1578       unfolding s0 s1
  1579       unfolding sum.distrib[symmetric] sum_distrib_left
  1580       apply (rule sum.cong)
  1581       apply (auto simp add: of_nat_diff field_simps)
  1582       done
  1583     finally show ?thesis .
  1584   qed
  1585   then show ?thesis
  1586     unfolding fps_eq_iff by auto
  1587 qed
  1588 
  1589 lemma fps_deriv_X[simp]: "fps_deriv X = 1"
  1590   by (simp add: fps_deriv_def X_def fps_eq_iff)
  1591 
  1592 lemma fps_deriv_neg[simp]:
  1593   "fps_deriv (- (f:: 'a::comm_ring_1 fps)) = - (fps_deriv f)"
  1594   by (simp add: fps_eq_iff fps_deriv_def)
  1595 
  1596 lemma fps_deriv_add[simp]:
  1597   "fps_deriv ((f:: 'a::comm_ring_1 fps) + g) = fps_deriv f + fps_deriv g"
  1598   using fps_deriv_linear[of 1 f 1 g] by simp
  1599 
  1600 lemma fps_deriv_sub[simp]:
  1601   "fps_deriv ((f:: 'a::comm_ring_1 fps) - g) = fps_deriv f - fps_deriv g"
  1602   using fps_deriv_add [of f "- g"] by simp
  1603 
  1604 lemma fps_deriv_const[simp]: "fps_deriv (fps_const c) = 0"
  1605   by (simp add: fps_ext fps_deriv_def fps_const_def)
  1606 
  1607 lemma fps_deriv_of_nat [simp]: "fps_deriv (of_nat n) = 0"
  1608   by (simp add: fps_of_nat [symmetric])
  1609 
  1610 lemma fps_deriv_numeral [simp]: "fps_deriv (numeral n) = 0"
  1611   by (simp add: numeral_fps_const)    
  1612 
  1613 lemma fps_deriv_mult_const_left[simp]:
  1614   "fps_deriv (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_deriv f"
  1615   by simp
  1616 
  1617 lemma fps_deriv_0[simp]: "fps_deriv 0 = 0"
  1618   by (simp add: fps_deriv_def fps_eq_iff)
  1619 
  1620 lemma fps_deriv_1[simp]: "fps_deriv 1 = 0"
  1621   by (simp add: fps_deriv_def fps_eq_iff )
  1622 
  1623 lemma fps_deriv_mult_const_right[simp]:
  1624   "fps_deriv (f * fps_const (c::'a::comm_ring_1)) = fps_deriv f * fps_const c"
  1625   by simp
  1626 
  1627 lemma fps_deriv_sum:
  1628   "fps_deriv (sum f S) = sum (\<lambda>i. fps_deriv (f i :: 'a::comm_ring_1 fps)) S"
  1629 proof (cases "finite S")
  1630   case False
  1631   then show ?thesis by simp
  1632 next
  1633   case True
  1634   show ?thesis by (induct rule: finite_induct [OF True]) simp_all
  1635 qed
  1636 
  1637 lemma fps_deriv_eq_0_iff [simp]:
  1638   "fps_deriv f = 0 \<longleftrightarrow> f = fps_const (f$0 :: 'a::{idom,semiring_char_0})"
  1639   (is "?lhs \<longleftrightarrow> ?rhs")
  1640 proof
  1641   show ?lhs if ?rhs
  1642   proof -
  1643     from that have "fps_deriv f = fps_deriv (fps_const (f$0))"
  1644       by simp
  1645     then show ?thesis
  1646       by simp
  1647   qed
  1648   show ?rhs if ?lhs
  1649   proof -
  1650     from that have "\<forall>n. (fps_deriv f)$n = 0"
  1651       by simp
  1652     then have "\<forall>n. f$(n+1) = 0"
  1653       by (simp del: of_nat_Suc of_nat_add One_nat_def)
  1654     then show ?thesis
  1655       apply (clarsimp simp add: fps_eq_iff fps_const_def)
  1656       apply (erule_tac x="n - 1" in allE)
  1657       apply simp
  1658       done
  1659   qed
  1660 qed
  1661 
  1662 lemma fps_deriv_eq_iff:
  1663   fixes f :: "'a::{idom,semiring_char_0} fps"
  1664   shows "fps_deriv f = fps_deriv g \<longleftrightarrow> (f = fps_const(f$0 - g$0) + g)"
  1665 proof -
  1666   have "fps_deriv f = fps_deriv g \<longleftrightarrow> fps_deriv (f - g) = 0"
  1667     by simp
  1668   also have "\<dots> \<longleftrightarrow> f - g = fps_const ((f - g) $ 0)"
  1669     unfolding fps_deriv_eq_0_iff ..
  1670   finally show ?thesis
  1671     by (simp add: field_simps)
  1672 qed
  1673 
  1674 lemma fps_deriv_eq_iff_ex:
  1675   "(fps_deriv f = fps_deriv g) \<longleftrightarrow> (\<exists>c::'a::{idom,semiring_char_0}. f = fps_const c + g)"
  1676   by (auto simp: fps_deriv_eq_iff)
  1677 
  1678 
  1679 fun fps_nth_deriv :: "nat \<Rightarrow> 'a::semiring_1 fps \<Rightarrow> 'a fps"
  1680 where
  1681   "fps_nth_deriv 0 f = f"
  1682 | "fps_nth_deriv (Suc n) f = fps_nth_deriv n (fps_deriv f)"
  1683 
  1684 lemma fps_nth_deriv_commute: "fps_nth_deriv (Suc n) f = fps_deriv (fps_nth_deriv n f)"
  1685   by (induct n arbitrary: f) auto
  1686 
  1687 lemma fps_nth_deriv_linear[simp]:
  1688   "fps_nth_deriv n (fps_const (a::'a::comm_semiring_1) * f + fps_const b * g) =
  1689     fps_const a * fps_nth_deriv n f + fps_const b * fps_nth_deriv n g"
  1690   by (induct n arbitrary: f g) (auto simp add: fps_nth_deriv_commute)
  1691 
  1692 lemma fps_nth_deriv_neg[simp]:
  1693   "fps_nth_deriv n (- (f :: 'a::comm_ring_1 fps)) = - (fps_nth_deriv n f)"
  1694   by (induct n arbitrary: f) simp_all
  1695 
  1696 lemma fps_nth_deriv_add[simp]:
  1697   "fps_nth_deriv n ((f :: 'a::comm_ring_1 fps) + g) = fps_nth_deriv n f + fps_nth_deriv n g"
  1698   using fps_nth_deriv_linear[of n 1 f 1 g] by simp
  1699 
  1700 lemma fps_nth_deriv_sub[simp]:
  1701   "fps_nth_deriv n ((f :: 'a::comm_ring_1 fps) - g) = fps_nth_deriv n f - fps_nth_deriv n g"
  1702   using fps_nth_deriv_add [of n f "- g"] by simp
  1703 
  1704 lemma fps_nth_deriv_0[simp]: "fps_nth_deriv n 0 = 0"
  1705   by (induct n) simp_all
  1706 
  1707 lemma fps_nth_deriv_1[simp]: "fps_nth_deriv n 1 = (if n = 0 then 1 else 0)"
  1708   by (induct n) simp_all
  1709 
  1710 lemma fps_nth_deriv_const[simp]:
  1711   "fps_nth_deriv n (fps_const c) = (if n = 0 then fps_const c else 0)"
  1712   by (cases n) simp_all
  1713 
  1714 lemma fps_nth_deriv_mult_const_left[simp]:
  1715   "fps_nth_deriv n (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_nth_deriv n f"
  1716   using fps_nth_deriv_linear[of n "c" f 0 0 ] by simp
  1717 
  1718 lemma fps_nth_deriv_mult_const_right[simp]:
  1719   "fps_nth_deriv n (f * fps_const (c::'a::comm_ring_1)) = fps_nth_deriv n f * fps_const c"
  1720   using fps_nth_deriv_linear[of n "c" f 0 0] by (simp add: mult.commute)
  1721 
  1722 lemma fps_nth_deriv_sum:
  1723   "fps_nth_deriv n (sum f S) = sum (\<lambda>i. fps_nth_deriv n (f i :: 'a::comm_ring_1 fps)) S"
  1724 proof (cases "finite S")
  1725   case True
  1726   show ?thesis by (induct rule: finite_induct [OF True]) simp_all
  1727 next
  1728   case False
  1729   then show ?thesis by simp
  1730 qed
  1731 
  1732 lemma fps_deriv_maclauren_0:
  1733   "(fps_nth_deriv k (f :: 'a::comm_semiring_1 fps)) $ 0 = of_nat (fact k) * f $ k"
  1734   by (induct k arbitrary: f) (auto simp add: field_simps)
  1735 
  1736 
  1737 subsection \<open>Powers\<close>
  1738 
  1739 lemma fps_power_zeroth_eq_one: "a$0 =1 \<Longrightarrow> a^n $ 0 = (1::'a::semiring_1)"
  1740   by (induct n) (auto simp add: expand_fps_eq fps_mult_nth)
  1741 
  1742 lemma fps_power_first_eq: "(a :: 'a::comm_ring_1 fps) $ 0 =1 \<Longrightarrow> a^n $ 1 = of_nat n * a$1"
  1743 proof (induct n)
  1744   case 0
  1745   then show ?case by simp
  1746 next
  1747   case (Suc n)
  1748   show ?case unfolding power_Suc fps_mult_nth
  1749     using Suc.hyps[OF \<open>a$0 = 1\<close>] \<open>a$0 = 1\<close> fps_power_zeroth_eq_one[OF \<open>a$0=1\<close>]
  1750     by (simp add: field_simps)
  1751 qed
  1752 
  1753 lemma startsby_one_power:"a $ 0 = (1::'a::comm_ring_1) \<Longrightarrow> a^n $ 0 = 1"
  1754   by (induct n) (auto simp add: fps_mult_nth)
  1755 
  1756 lemma startsby_zero_power:"a $0 = (0::'a::comm_ring_1) \<Longrightarrow> n > 0 \<Longrightarrow> a^n $0 = 0"
  1757   by (induct n) (auto simp add: fps_mult_nth)
  1758 
  1759 lemma startsby_power:"a $0 = (v::'a::comm_ring_1) \<Longrightarrow> a^n $0 = v^n"
  1760   by (induct n) (auto simp add: fps_mult_nth)
  1761 
  1762 lemma startsby_zero_power_iff[simp]: "a^n $0 = (0::'a::idom) \<longleftrightarrow> n \<noteq> 0 \<and> a$0 = 0"
  1763   apply (rule iffI)
  1764   apply (induct n)
  1765   apply (auto simp add: fps_mult_nth)
  1766   apply (rule startsby_zero_power, simp_all)
  1767   done
  1768 
  1769 lemma startsby_zero_power_prefix:
  1770   assumes a0: "a $ 0 = (0::'a::idom)"
  1771   shows "\<forall>n < k. a ^ k $ n = 0"
  1772   using a0
  1773 proof (induct k rule: nat_less_induct)
  1774   fix k
  1775   assume H: "\<forall>m<k. a $0 =  0 \<longrightarrow> (\<forall>n<m. a ^ m $ n = 0)" and a0: "a $ 0 = 0"
  1776   show "\<forall>m<k. a ^ k $ m = 0"
  1777   proof (cases k)
  1778     case 0
  1779     then show ?thesis by simp
  1780   next
  1781     case (Suc l)
  1782     have "a^k $ m = 0" if mk: "m < k" for m
  1783     proof (cases "m = 0")
  1784       case True
  1785       then show ?thesis
  1786         using startsby_zero_power[of a k] Suc a0 by simp
  1787     next
  1788       case False
  1789       have "a ^k $ m = (a^l * a) $m"
  1790         by (simp add: Suc mult.commute)
  1791       also have "\<dots> = (\<Sum>i = 0..m. a ^ l $ i * a $ (m - i))"
  1792         by (simp add: fps_mult_nth)
  1793       also have "\<dots> = 0"
  1794         apply (rule sum.neutral)
  1795         apply auto
  1796         apply (case_tac "x = m")
  1797         using a0 apply simp
  1798         apply (rule H[rule_format])
  1799         using a0 Suc mk apply auto
  1800         done
  1801       finally show ?thesis .
  1802     qed
  1803     then show ?thesis by blast
  1804   qed
  1805 qed
  1806 
  1807 lemma startsby_zero_sum_depends:
  1808   assumes a0: "a $0 = (0::'a::idom)"
  1809     and kn: "n \<ge> k"
  1810   shows "sum (\<lambda>i. (a ^ i)$k) {0 .. n} = sum (\<lambda>i. (a ^ i)$k) {0 .. k}"
  1811   apply (rule sum.mono_neutral_right)
  1812   using kn
  1813   apply auto
  1814   apply (rule startsby_zero_power_prefix[rule_format, OF a0])
  1815   apply arith
  1816   done
  1817 
  1818 lemma startsby_zero_power_nth_same:
  1819   assumes a0: "a$0 = (0::'a::idom)"
  1820   shows "a^n $ n = (a$1) ^ n"
  1821 proof (induct n)
  1822   case 0
  1823   then show ?case by simp
  1824 next
  1825   case (Suc n)
  1826   have "a ^ Suc n $ (Suc n) = (a^n * a)$(Suc n)"
  1827     by (simp add: field_simps)
  1828   also have "\<dots> = sum (\<lambda>i. a^n$i * a $ (Suc n - i)) {0.. Suc n}"
  1829     by (simp add: fps_mult_nth)
  1830   also have "\<dots> = sum (\<lambda>i. a^n$i * a $ (Suc n - i)) {n .. Suc n}"
  1831     apply (rule sum.mono_neutral_right)
  1832     apply simp
  1833     apply clarsimp
  1834     apply clarsimp
  1835     apply (rule startsby_zero_power_prefix[rule_format, OF a0])
  1836     apply arith
  1837     done
  1838   also have "\<dots> = a^n $ n * a$1"
  1839     using a0 by simp
  1840   finally show ?case
  1841     using Suc.hyps by simp
  1842 qed
  1843 
  1844 lemma fps_inverse_power:
  1845   fixes a :: "'a::field fps"
  1846   shows "inverse (a^n) = inverse a ^ n"
  1847   by (induction n) (simp_all add: fps_inverse_mult)
  1848 
  1849 lemma fps_deriv_power:
  1850   "fps_deriv (a ^ n) = fps_const (of_nat n :: 'a::comm_ring_1) * fps_deriv a * a ^ (n - 1)"
  1851   apply (induct n)
  1852   apply (auto simp add: field_simps fps_const_add[symmetric] simp del: fps_const_add)
  1853   apply (case_tac n)
  1854   apply (auto simp add: field_simps)
  1855   done
  1856 
  1857 lemma fps_inverse_deriv:
  1858   fixes a :: "'a::field fps"
  1859   assumes a0: "a$0 \<noteq> 0"
  1860   shows "fps_deriv (inverse a) = - fps_deriv a * (inverse a)\<^sup>2"
  1861 proof -
  1862   from inverse_mult_eq_1[OF a0]
  1863   have "fps_deriv (inverse a * a) = 0" by simp
  1864   then have "inverse a * fps_deriv a + fps_deriv (inverse a) * a = 0"
  1865     by simp
  1866   then have "inverse a * (inverse a * fps_deriv a + fps_deriv (inverse a) * a) = 0"
  1867     by simp
  1868   with inverse_mult_eq_1[OF a0]
  1869   have "(inverse a)\<^sup>2 * fps_deriv a + fps_deriv (inverse a) = 0"
  1870     unfolding power2_eq_square
  1871     apply (simp add: field_simps)
  1872     apply (simp add: mult.assoc[symmetric])
  1873     done
  1874   then have "(inverse a)\<^sup>2 * fps_deriv a + fps_deriv (inverse a) - fps_deriv a * (inverse a)\<^sup>2 =
  1875       0 - fps_deriv a * (inverse a)\<^sup>2"
  1876     by simp
  1877   then show "fps_deriv (inverse a) = - fps_deriv a * (inverse a)\<^sup>2"
  1878     by (simp add: field_simps)
  1879 qed
  1880 
  1881 lemma fps_inverse_deriv':
  1882   fixes a :: "'a::field fps"
  1883   assumes a0: "a $ 0 \<noteq> 0"
  1884   shows "fps_deriv (inverse a) = - fps_deriv a / a\<^sup>2"
  1885   using fps_inverse_deriv[OF a0] a0
  1886   by (simp add: fps_divide_unit power2_eq_square fps_inverse_mult)
  1887 
  1888 lemma inverse_mult_eq_1':
  1889   assumes f0: "f$0 \<noteq> (0::'a::field)"
  1890   shows "f * inverse f = 1"
  1891   by (metis mult.commute inverse_mult_eq_1 f0)
  1892 
  1893 lemma fps_inverse_minus [simp]: "inverse (-f) = -inverse (f :: 'a :: field fps)"
  1894   by (cases "f$0 = 0") (auto intro: fps_inverse_unique simp: inverse_mult_eq_1' fps_inverse_eq_0)
  1895   
  1896 lemma divide_fps_const [simp]: "f / fps_const (c :: 'a :: field) = fps_const (inverse c) * f"
  1897   by (cases "c = 0") (simp_all add: fps_divide_unit fps_const_inverse)
  1898 
  1899 (* FIXME: The last part of this proof should go through by simp once we have a proper
  1900    theorem collection for simplifying division on rings *)
  1901 lemma fps_divide_deriv:
  1902   assumes "b dvd (a :: 'a :: field fps)"
  1903   shows   "fps_deriv (a / b) = (fps_deriv a * b - a * fps_deriv b) / b^2"
  1904 proof -
  1905   have eq_divide_imp: "c \<noteq> 0 \<Longrightarrow> a * c = b \<Longrightarrow> a = b div c" for a b c :: "'a :: field fps"
  1906     by (drule sym) (simp add: mult.assoc)
  1907   from assms have "a = a / b * b" by simp
  1908   also have "fps_deriv (a / b * b) = fps_deriv (a / b) * b + a / b * fps_deriv b" by simp
  1909   finally have "fps_deriv (a / b) * b^2 = fps_deriv a * b - a * fps_deriv b" using assms
  1910     by (simp add: power2_eq_square algebra_simps)
  1911   thus ?thesis by (cases "b = 0") (auto simp: eq_divide_imp)
  1912 qed
  1913 
  1914 lemma fps_inverse_gp': "inverse (Abs_fps (\<lambda>n. 1::'a::field)) = 1 - X"
  1915   by (simp add: fps_inverse_gp fps_eq_iff X_def)
  1916 
  1917 lemma fps_one_over_one_minus_X_squared:
  1918   "inverse ((1 - X)^2 :: 'a :: field fps) = Abs_fps (\<lambda>n. of_nat (n+1))"
  1919 proof -
  1920   have "inverse ((1 - X)^2 :: 'a fps) = fps_deriv (inverse (1 - X))"
  1921     by (subst fps_inverse_deriv) (simp_all add: fps_inverse_power)
  1922   also have "inverse (1 - X :: 'a fps) = Abs_fps (\<lambda>_. 1)"
  1923     by (subst fps_inverse_gp' [symmetric]) simp
  1924   also have "fps_deriv \<dots> = Abs_fps (\<lambda>n. of_nat (n + 1))"
  1925     by (simp add: fps_deriv_def)
  1926   finally show ?thesis .
  1927 qed
  1928 
  1929 lemma fps_nth_deriv_X[simp]: "fps_nth_deriv n X = (if n = 0 then X else if n=1 then 1 else 0)"
  1930   by (cases n) simp_all
  1931 
  1932 lemma fps_inverse_X_plus1: "inverse (1 + X) = Abs_fps (\<lambda>n. (- (1::'a::field)) ^ n)"
  1933   (is "_ = ?r")
  1934 proof -
  1935   have eq: "(1 + X) * ?r = 1"
  1936     unfolding minus_one_power_iff
  1937     by (auto simp add: field_simps fps_eq_iff)
  1938   show ?thesis
  1939     by (auto simp add: eq intro: fps_inverse_unique)
  1940 qed
  1941 
  1942 
  1943 subsection \<open>Integration\<close>
  1944 
  1945 definition fps_integral :: "'a::field_char_0 fps \<Rightarrow> 'a \<Rightarrow> 'a fps"
  1946   where "fps_integral a a0 = Abs_fps (\<lambda>n. if n = 0 then a0 else (a$(n - 1) / of_nat n))"
  1947 
  1948 lemma fps_deriv_fps_integral: "fps_deriv (fps_integral a a0) = a"
  1949   unfolding fps_integral_def fps_deriv_def
  1950   by (simp add: fps_eq_iff del: of_nat_Suc)
  1951 
  1952 lemma fps_integral_linear:
  1953   "fps_integral (fps_const a * f + fps_const b * g) (a*a0 + b*b0) =
  1954     fps_const a * fps_integral f a0 + fps_const b * fps_integral g b0"
  1955   (is "?l = ?r")
  1956 proof -
  1957   have "fps_deriv ?l = fps_deriv ?r"
  1958     by (simp add: fps_deriv_fps_integral)
  1959   moreover have "?l$0 = ?r$0"
  1960     by (simp add: fps_integral_def)
  1961   ultimately show ?thesis
  1962     unfolding fps_deriv_eq_iff by auto
  1963 qed
  1964 
  1965 
  1966 subsection \<open>Composition of FPSs\<close>
  1967 
  1968 definition fps_compose :: "'a::semiring_1 fps \<Rightarrow> 'a fps \<Rightarrow> 'a fps"  (infixl "oo" 55)
  1969   where "a oo b = Abs_fps (\<lambda>n. sum (\<lambda>i. a$i * (b^i$n)) {0..n})"
  1970 
  1971 lemma fps_compose_nth: "(a oo b)$n = sum (\<lambda>i. a$i * (b^i$n)) {0..n}"
  1972   by (simp add: fps_compose_def)
  1973 
  1974 lemma fps_compose_nth_0 [simp]: "(f oo g) $ 0 = f $ 0"
  1975   by (simp add: fps_compose_nth)
  1976 
  1977 lemma fps_compose_X[simp]: "a oo X = (a :: 'a::comm_ring_1 fps)"
  1978   by (simp add: fps_ext fps_compose_def mult_delta_right sum.delta')
  1979 
  1980 lemma fps_const_compose[simp]: "fps_const (a::'a::comm_ring_1) oo b = fps_const a"
  1981   by (simp add: fps_eq_iff fps_compose_nth mult_delta_left sum.delta)
  1982 
  1983 lemma numeral_compose[simp]: "(numeral k :: 'a::comm_ring_1 fps) oo b = numeral k"
  1984   unfolding numeral_fps_const by simp
  1985 
  1986 lemma neg_numeral_compose[simp]: "(- numeral k :: 'a::comm_ring_1 fps) oo b = - numeral k"
  1987   unfolding neg_numeral_fps_const by simp
  1988 
  1989 lemma X_fps_compose_startby0[simp]: "a$0 = 0 \<Longrightarrow> X oo a = (a :: 'a::comm_ring_1 fps)"
  1990   by (simp add: fps_eq_iff fps_compose_def mult_delta_left sum.delta not_le)
  1991 
  1992 
  1993 subsection \<open>Rules from Herbert Wilf's Generatingfunctionology\<close>
  1994 
  1995 subsubsection \<open>Rule 1\<close>
  1996   (* {a_{n+k}}_0^infty Corresponds to (f - sum (\<lambda>i. a_i * x^i))/x^h, for h>0*)
  1997 
  1998 lemma fps_power_mult_eq_shift:
  1999   "X^Suc k * Abs_fps (\<lambda>n. a (n + Suc k)) =
  2000     Abs_fps a - sum (\<lambda>i. fps_const (a i :: 'a::comm_ring_1) * X^i) {0 .. k}"
  2001   (is "?lhs = ?rhs")
  2002 proof -
  2003   have "?lhs $ n = ?rhs $ n" for n :: nat
  2004   proof -
  2005     have "?lhs $ n = (if n < Suc k then 0 else a n)"
  2006       unfolding X_power_mult_nth by auto
  2007     also have "\<dots> = ?rhs $ n"
  2008     proof (induct k)
  2009       case 0
  2010       then show ?case
  2011         by (simp add: fps_sum_nth)
  2012     next
  2013       case (Suc k)
  2014       have "(Abs_fps a - sum (\<lambda>i. fps_const (a i :: 'a) * X^i) {0 .. Suc k})$n =
  2015         (Abs_fps a - sum (\<lambda>i. fps_const (a i :: 'a) * X^i) {0 .. k} -
  2016           fps_const (a (Suc k)) * X^ Suc k) $ n"
  2017         by (simp add: field_simps)
  2018       also have "\<dots> = (if n < Suc k then 0 else a n) - (fps_const (a (Suc k)) * X^ Suc k)$n"
  2019         using Suc.hyps[symmetric] unfolding fps_sub_nth by simp
  2020       also have "\<dots> = (if n < Suc (Suc k) then 0 else a n)"
  2021         unfolding X_power_mult_right_nth
  2022         apply (auto simp add: not_less fps_const_def)
  2023         apply (rule cong[of a a, OF refl])
  2024         apply arith
  2025         done
  2026       finally show ?case
  2027         by simp
  2028     qed
  2029     finally show ?thesis .
  2030   qed
  2031   then show ?thesis
  2032     by (simp add: fps_eq_iff)
  2033 qed
  2034 
  2035 
  2036 subsubsection \<open>Rule 2\<close>
  2037 
  2038   (* We can not reach the form of Wilf, but still near to it using rewrite rules*)
  2039   (* If f reprents {a_n} and P is a polynomial, then
  2040         P(xD) f represents {P(n) a_n}*)
  2041 
  2042 definition "XD = op * X \<circ> fps_deriv"
  2043 
  2044 lemma XD_add[simp]:"XD (a + b) = XD a + XD (b :: 'a::comm_ring_1 fps)"
  2045   by (simp add: XD_def field_simps)
  2046 
  2047 lemma XD_mult_const[simp]:"XD (fps_const (c::'a::comm_ring_1) * a) = fps_const c * XD a"
  2048   by (simp add: XD_def field_simps)
  2049 
  2050 lemma XD_linear[simp]: "XD (fps_const c * a + fps_const d * b) =
  2051     fps_const c * XD a + fps_const d * XD (b :: 'a::comm_ring_1 fps)"
  2052   by simp
  2053 
  2054 lemma XDN_linear:
  2055   "(XD ^^ n) (fps_const c * a + fps_const d * b) =
  2056     fps_const c * (XD ^^ n) a + fps_const d * (XD ^^ n) (b :: 'a::comm_ring_1 fps)"
  2057   by (induct n) simp_all
  2058 
  2059 lemma fps_mult_X_deriv_shift: "X* fps_deriv a = Abs_fps (\<lambda>n. of_nat n* a$n)"
  2060   by (simp add: fps_eq_iff)
  2061 
  2062 lemma fps_mult_XD_shift:
  2063   "(XD ^^ k) (a :: 'a::comm_ring_1 fps) = Abs_fps (\<lambda>n. (of_nat n ^ k) * a$n)"
  2064   by (induct k arbitrary: a) (simp_all add: XD_def fps_eq_iff field_simps del: One_nat_def)
  2065 
  2066 
  2067 subsubsection \<open>Rule 3\<close>
  2068 
  2069 text \<open>Rule 3 is trivial and is given by \<open>fps_times_def\<close>.\<close>
  2070 
  2071 
  2072 subsubsection \<open>Rule 5 --- summation and "division" by (1 - X)\<close>
  2073 
  2074 lemma fps_divide_X_minus1_sum_lemma:
  2075   "a = ((1::'a::comm_ring_1 fps) - X) * Abs_fps (\<lambda>n. sum (\<lambda>i. a $ i) {0..n})"
  2076 proof -
  2077   let ?sa = "Abs_fps (\<lambda>n. sum (\<lambda>i. a $ i) {0..n})"
  2078   have th0: "\<And>i. (1 - (X::'a fps)) $ i = (if i = 0 then 1 else if i = 1 then - 1 else 0)"
  2079     by simp
  2080   have "a$n = ((1 - X) * ?sa) $ n" for n
  2081   proof (cases "n = 0")
  2082     case True
  2083     then show ?thesis
  2084       by (simp add: fps_mult_nth)
  2085   next
  2086     case False
  2087     then have u: "{0} \<union> ({1} \<union> {2..n}) = {0..n}" "{1} \<union> {2..n} = {1..n}"
  2088       "{0..n - 1} \<union> {n} = {0..n}"
  2089       by (auto simp: set_eq_iff)
  2090     have d: "{0} \<inter> ({1} \<union> {2..n}) = {}" "{1} \<inter> {2..n} = {}" "{0..n - 1} \<inter> {n} = {}"
  2091       using False by simp_all
  2092     have f: "finite {0}" "finite {1}" "finite {2 .. n}"
  2093       "finite {0 .. n - 1}" "finite {n}" by simp_all
  2094     have "((1 - X) * ?sa) $ n = sum (\<lambda>i. (1 - X)$ i * ?sa $ (n - i)) {0 .. n}"
  2095       by (simp add: fps_mult_nth)
  2096     also have "\<dots> = a$n"
  2097       unfolding th0
  2098       unfolding sum.union_disjoint[OF f(1) finite_UnI[OF f(2,3)] d(1), unfolded u(1)]
  2099       unfolding sum.union_disjoint[OF f(2) f(3) d(2)]
  2100       apply (simp)
  2101       unfolding sum.union_disjoint[OF f(4,5) d(3), unfolded u(3)]
  2102       apply simp
  2103       done
  2104     finally show ?thesis
  2105       by simp
  2106   qed
  2107   then show ?thesis
  2108     unfolding fps_eq_iff by blast
  2109 qed
  2110 
  2111 lemma fps_divide_X_minus1_sum:
  2112   "a /((1::'a::field fps) - X) = Abs_fps (\<lambda>n. sum (\<lambda>i. a $ i) {0..n})"
  2113 proof -
  2114   let ?X = "1 - (X::'a fps)"
  2115   have th0: "?X $ 0 \<noteq> 0"
  2116     by simp
  2117   have "a /?X = ?X *  Abs_fps (\<lambda>n::nat. sum (op $ a) {0..n}) * inverse ?X"
  2118     using fps_divide_X_minus1_sum_lemma[of a, symmetric] th0
  2119     by (simp add: fps_divide_def mult.assoc)
  2120   also have "\<dots> = (inverse ?X * ?X) * Abs_fps (\<lambda>n::nat. sum (op $ a) {0..n}) "
  2121     by (simp add: ac_simps)
  2122   finally show ?thesis
  2123     by (simp add: inverse_mult_eq_1[OF th0])
  2124 qed
  2125 
  2126 
  2127 subsubsection \<open>Rule 4 in its more general form: generalizes Rule 3 for an arbitrary
  2128   finite product of FPS, also the relvant instance of powers of a FPS\<close>
  2129 
  2130 definition "natpermute n k = {l :: nat list. length l = k \<and> sum_list l = n}"
  2131 
  2132 lemma natlist_trivial_1: "natpermute n 1 = {[n]}"
  2133   apply (auto simp add: natpermute_def)
  2134   apply (case_tac x)
  2135   apply auto
  2136   done
  2137 
  2138 lemma append_natpermute_less_eq:
  2139   assumes "xs @ ys \<in> natpermute n k"
  2140   shows "sum_list xs \<le> n"
  2141     and "sum_list ys \<le> n"
  2142 proof -
  2143   from assms have "sum_list (xs @ ys) = n"
  2144     by (simp add: natpermute_def)
  2145   then have "sum_list xs + sum_list ys = n"
  2146     by simp
  2147   then show "sum_list xs \<le> n" and "sum_list ys \<le> n"
  2148     by simp_all
  2149 qed
  2150 
  2151 lemma natpermute_split:
  2152   assumes "h \<le> k"
  2153   shows "natpermute n k =
  2154     (\<Union>m \<in>{0..n}. {l1 @ l2 |l1 l2. l1 \<in> natpermute m h \<and> l2 \<in> natpermute (n - m) (k - h)})"
  2155   (is "?L = ?R" is "_ = (\<Union>m \<in>{0..n}. ?S m)")
  2156 proof
  2157   show "?R \<subseteq> ?L"
  2158   proof
  2159     fix l
  2160     assume l: "l \<in> ?R"
  2161     from l obtain m xs ys where h: "m \<in> {0..n}"
  2162       and xs: "xs \<in> natpermute m h"
  2163       and ys: "ys \<in> natpermute (n - m) (k - h)"
  2164       and leq: "l = xs@ys" by blast
  2165     from xs have xs': "sum_list xs = m"
  2166       by (simp add: natpermute_def)
  2167     from ys have ys': "sum_list ys = n - m"
  2168       by (simp add: natpermute_def)
  2169     show "l \<in> ?L" using leq xs ys h
  2170       apply (clarsimp simp add: natpermute_def)
  2171       unfolding xs' ys'
  2172       using assms xs ys
  2173       unfolding natpermute_def
  2174       apply simp
  2175       done
  2176   qed
  2177   show "?L \<subseteq> ?R"
  2178   proof
  2179     fix l
  2180     assume l: "l \<in> natpermute n k"
  2181     let ?xs = "take h l"
  2182     let ?ys = "drop h l"
  2183     let ?m = "sum_list ?xs"
  2184     from l have ls: "sum_list (?xs @ ?ys) = n"
  2185       by (simp add: natpermute_def)
  2186     have xs: "?xs \<in> natpermute ?m h" using l assms
  2187       by (simp add: natpermute_def)
  2188     have l_take_drop: "sum_list l = sum_list (take h l @ drop h l)"
  2189       by simp
  2190     then have ys: "?ys \<in> natpermute (n - ?m) (k - h)"
  2191       using l assms ls by (auto simp add: natpermute_def simp del: append_take_drop_id)
  2192     from ls have m: "?m \<in> {0..n}"
  2193       by (simp add: l_take_drop del: append_take_drop_id)
  2194     from xs ys ls show "l \<in> ?R"
  2195       apply auto
  2196       apply (rule bexI [where x = "?m"])
  2197       apply (rule exI [where x = "?xs"])
  2198       apply (rule exI [where x = "?ys"])
  2199       using ls l
  2200       apply (auto simp add: natpermute_def l_take_drop simp del: append_take_drop_id)
  2201       apply simp
  2202       done
  2203   qed
  2204 qed
  2205 
  2206 lemma natpermute_0: "natpermute n 0 = (if n = 0 then {[]} else {})"
  2207   by (auto simp add: natpermute_def)
  2208 
  2209 lemma natpermute_0'[simp]: "natpermute 0 k = (if k = 0 then {[]} else {replicate k 0})"
  2210   apply (auto simp add: set_replicate_conv_if natpermute_def)
  2211   apply (rule nth_equalityI)
  2212   apply simp_all
  2213   done
  2214 
  2215 lemma natpermute_finite: "finite (natpermute n k)"
  2216 proof (induct k arbitrary: n)
  2217   case 0
  2218   then show ?case
  2219     apply (subst natpermute_split[of 0 0, simplified])
  2220     apply (simp add: natpermute_0)
  2221     done
  2222 next
  2223   case (Suc k)
  2224   then show ?case unfolding natpermute_split [of k "Suc k", simplified]
  2225     apply -
  2226     apply (rule finite_UN_I)
  2227     apply simp
  2228     unfolding One_nat_def[symmetric] natlist_trivial_1
  2229     apply simp
  2230     done
  2231 qed
  2232 
  2233 lemma natpermute_contain_maximal:
  2234   "{xs \<in> natpermute n (k + 1). n \<in> set xs} = (\<Union>i\<in>{0 .. k}. {(replicate (k + 1) 0) [i:=n]})"
  2235   (is "?A = ?B")
  2236 proof
  2237   show "?A \<subseteq> ?B"
  2238   proof
  2239     fix xs
  2240     assume "xs \<in> ?A"
  2241     then have H: "xs \<in> natpermute n (k + 1)" and n: "n \<in> set xs"
  2242       by blast+
  2243     then obtain i where i: "i \<in> {0.. k}" "xs!i = n"
  2244       unfolding in_set_conv_nth by (auto simp add: less_Suc_eq_le natpermute_def)
  2245     have eqs: "({0..k} - {i}) \<union> {i} = {0..k}"
  2246       using i by auto
  2247     have f: "finite({0..k} - {i})" "finite {i}"
  2248       by auto
  2249     have d: "({0..k} - {i}) \<inter> {i} = {}"
  2250       using i by auto
  2251     from H have "n = sum (nth xs) {0..k}"
  2252       apply (simp add: natpermute_def)
  2253       apply (auto simp add: atLeastLessThanSuc_atLeastAtMost sum_list_sum_nth)
  2254       done
  2255     also have "\<dots> = n + sum (nth xs) ({0..k} - {i})"
  2256       unfolding sum.union_disjoint[OF f d, unfolded eqs] using i by simp
  2257     finally have zxs: "\<forall> j\<in> {0..k} - {i}. xs!j = 0"
  2258       by auto
  2259     from H have xsl: "length xs = k+1"
  2260       by (simp add: natpermute_def)
  2261     from i have i': "i < length (replicate (k+1) 0)"   "i < k+1"
  2262       unfolding length_replicate by presburger+
  2263     have "xs = replicate (k+1) 0 [i := n]"
  2264       apply (rule nth_equalityI)
  2265       unfolding xsl length_list_update length_replicate
  2266       apply simp
  2267       apply clarify
  2268       unfolding nth_list_update[OF i'(1)]
  2269       using i zxs
  2270       apply (case_tac "ia = i")
  2271       apply (auto simp del: replicate.simps)
  2272       done
  2273     then show "xs \<in> ?B" using i by blast
  2274   qed
  2275   show "?B \<subseteq> ?A"
  2276   proof
  2277     fix xs
  2278     assume "xs \<in> ?B"
  2279     then obtain i where i: "i \<in> {0..k}" and xs: "xs = replicate (k + 1) 0 [i:=n]"
  2280       by auto
  2281     have nxs: "n \<in> set xs"
  2282       unfolding xs
  2283       apply (rule set_update_memI)
  2284       using i apply simp
  2285       done
  2286     have xsl: "length xs = k + 1"
  2287       by (simp only: xs length_replicate length_list_update)
  2288     have "sum_list xs = sum (nth xs) {0..<k+1}"
  2289       unfolding sum_list_sum_nth xsl ..
  2290     also have "\<dots> = sum (\<lambda>j. if j = i then n else 0) {0..< k+1}"
  2291       by (rule sum.cong) (simp_all add: xs del: replicate.simps)
  2292     also have "\<dots> = n" using i by (simp add: sum.delta)
  2293     finally have "xs \<in> natpermute n (k + 1)"
  2294       using xsl unfolding natpermute_def mem_Collect_eq by blast
  2295     then show "xs \<in> ?A"
  2296       using nxs by blast
  2297   qed
  2298 qed
  2299 
  2300 text \<open>The general form.\<close>
  2301 lemma fps_prod_nth:
  2302   fixes m :: nat
  2303     and a :: "nat \<Rightarrow> 'a::comm_ring_1 fps"
  2304   shows "(prod a {0 .. m}) $ n =
  2305     sum (\<lambda>v. prod (\<lambda>j. (a j) $ (v!j)) {0..m}) (natpermute n (m+1))"
  2306   (is "?P m n")
  2307 proof (induct m arbitrary: n rule: nat_less_induct)
  2308   fix m n assume H: "\<forall>m' < m. \<forall>n. ?P m' n"
  2309   show "?P m n"
  2310   proof (cases m)
  2311     case 0
  2312     then show ?thesis
  2313       apply simp
  2314       unfolding natlist_trivial_1[where n = n, unfolded One_nat_def]
  2315       apply simp
  2316       done
  2317   next
  2318     case (Suc k)
  2319     then have km: "k < m" by arith
  2320     have u0: "{0 .. k} \<union> {m} = {0..m}"
  2321       using Suc by (simp add: set_eq_iff) presburger
  2322     have f0: "finite {0 .. k}" "finite {m}" by auto
  2323     have d0: "{0 .. k} \<inter> {m} = {}" using Suc by auto
  2324     have "(prod a {0 .. m}) $ n = (prod a {0 .. k} * a m) $ n"
  2325       unfolding prod.union_disjoint[OF f0 d0, unfolded u0] by simp
  2326     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))"
  2327       unfolding fps_mult_nth H[rule_format, OF km] ..
  2328     also have "\<dots> = (\<Sum>v\<in>natpermute n (m + 1). \<Prod>j\<in>{0..m}. a j $ v ! j)"
  2329       apply (simp add: Suc)
  2330       unfolding natpermute_split[of m "m + 1", simplified, of n,
  2331         unfolded natlist_trivial_1[unfolded One_nat_def] Suc]
  2332       apply (subst sum.UNION_disjoint)
  2333       apply simp
  2334       apply simp
  2335       unfolding image_Collect[symmetric]
  2336       apply clarsimp
  2337       apply (rule finite_imageI)
  2338       apply (rule natpermute_finite)
  2339       apply (clarsimp simp add: set_eq_iff)
  2340       apply auto
  2341       apply (rule sum.cong)
  2342       apply (rule refl)
  2343       unfolding sum_distrib_right
  2344       apply (rule sym)
  2345       apply (rule_tac l = "\<lambda>xs. xs @ [n - x]" in sum.reindex_cong)
  2346       apply (simp add: inj_on_def)
  2347       apply auto
  2348       unfolding prod.union_disjoint[OF f0 d0, unfolded u0, unfolded Suc]
  2349       apply (clarsimp simp add: natpermute_def nth_append)
  2350       done
  2351     finally show ?thesis .
  2352   qed
  2353 qed
  2354 
  2355 text \<open>The special form for powers.\<close>
  2356 lemma fps_power_nth_Suc:
  2357   fixes m :: nat
  2358     and a :: "'a::comm_ring_1 fps"
  2359   shows "(a ^ Suc m)$n = sum (\<lambda>v. prod (\<lambda>j. a $ (v!j)) {0..m}) (natpermute n (m+1))"
  2360 proof -
  2361   have th0: "a^Suc m = prod (\<lambda>i. a) {0..m}"
  2362     by (simp add: prod_constant)
  2363   show ?thesis unfolding th0 fps_prod_nth ..
  2364 qed
  2365 
  2366 lemma fps_power_nth:
  2367   fixes m :: nat
  2368     and a :: "'a::comm_ring_1 fps"
  2369   shows "(a ^m)$n =
  2370     (if m=0 then 1$n else sum (\<lambda>v. prod (\<lambda>j. a $ (v!j)) {0..m - 1}) (natpermute n m))"
  2371   by (cases m) (simp_all add: fps_power_nth_Suc del: power_Suc)
  2372 
  2373 lemma fps_nth_power_0:
  2374   fixes m :: nat
  2375     and a :: "'a::comm_ring_1 fps"
  2376   shows "(a ^m)$0 = (a$0) ^ m"
  2377 proof (cases m)
  2378   case 0
  2379   then show ?thesis by simp
  2380 next
  2381   case (Suc n)
  2382   then have c: "m = card {0..n}" by simp
  2383   have "(a ^m)$0 = prod (\<lambda>i. a$0) {0..n}"
  2384     by (simp add: Suc fps_power_nth del: replicate.simps power_Suc)
  2385   also have "\<dots> = (a$0) ^ m"
  2386    unfolding c by (rule prod_constant)
  2387  finally show ?thesis .
  2388 qed
  2389 
  2390 lemma natpermute_max_card:
  2391   assumes n0: "n \<noteq> 0"
  2392   shows "card {xs \<in> natpermute n (k + 1). n \<in> set xs} = k + 1"
  2393   unfolding natpermute_contain_maximal
  2394 proof -
  2395   let ?A = "\<lambda>i. {replicate (k + 1) 0[i := n]}"
  2396   let ?K = "{0 ..k}"
  2397   have fK: "finite ?K"
  2398     by simp
  2399   have fAK: "\<forall>i\<in>?K. finite (?A i)"
  2400     by auto
  2401   have d: "\<forall>i\<in> ?K. \<forall>j\<in> ?K. i \<noteq> j \<longrightarrow>
  2402     {replicate (k + 1) 0[i := n]} \<inter> {replicate (k + 1) 0[j := n]} = {}"
  2403   proof clarify
  2404     fix i j
  2405     assume i: "i \<in> ?K" and j: "j \<in> ?K" and ij: "i \<noteq> j"
  2406     have False if eq: "replicate (k+1) 0 [i:=n] = replicate (k+1) 0 [j:= n]"
  2407     proof -
  2408       have "(replicate (k+1) 0 [i:=n] ! i) = n"
  2409         using i by (simp del: replicate.simps)
  2410       moreover
  2411       have "(replicate (k+1) 0 [j:=n] ! i) = 0"
  2412         using i ij by (simp del: replicate.simps)
  2413       ultimately show ?thesis
  2414         using eq n0 by (simp del: replicate.simps)
  2415     qed
  2416     then show "{replicate (k + 1) 0[i := n]} \<inter> {replicate (k + 1) 0[j := n]} = {}"
  2417       by auto
  2418   qed
  2419   from card_UN_disjoint[OF fK fAK d]
  2420   show "card (\<Union>i\<in>{0..k}. {replicate (k + 1) 0[i := n]}) = k + 1"
  2421     by simp
  2422 qed
  2423 
  2424 lemma fps_power_Suc_nth:
  2425   fixes f :: "'a :: comm_ring_1 fps"
  2426   assumes k: "k > 0"
  2427   shows "(f ^ Suc m) $ k = 
  2428            of_nat (Suc m) * (f $ k * (f $ 0) ^ m) +
  2429            (\<Sum>v\<in>{v\<in>natpermute k (m+1). k \<notin> set v}. \<Prod>j = 0..m. f $ v ! j)"
  2430 proof -
  2431   define A B 
  2432     where "A = {v\<in>natpermute k (m+1). k \<in> set v}" 
  2433       and  "B = {v\<in>natpermute k (m+1). k \<notin> set v}"
  2434   have [simp]: "finite A" "finite B" "A \<inter> B = {}" by (auto simp: A_def B_def natpermute_finite)
  2435 
  2436   from natpermute_max_card[of k m] k have card_A: "card A = m + 1" by (simp add: A_def)
  2437   {
  2438     fix v assume v: "v \<in> A"
  2439     from v have [simp]: "length v = Suc m" by (simp add: A_def natpermute_def)
  2440     from v have "\<exists>j. j \<le> m \<and> v ! j = k" 
  2441       by (auto simp: set_conv_nth A_def natpermute_def less_Suc_eq_le)
  2442     then guess j by (elim exE conjE) note j = this
  2443     
  2444     from v have "k = sum_list v" by (simp add: A_def natpermute_def)
  2445     also have "\<dots> = (\<Sum>i=0..m. v ! i)"
  2446       by (simp add: sum_list_sum_nth atLeastLessThanSuc_atLeastAtMost del: sum_op_ivl_Suc)
  2447     also from j have "{0..m} = insert j ({0..m}-{j})" by auto
  2448     also from j have "(\<Sum>i\<in>\<dots>. v ! i) = k + (\<Sum>i\<in>{0..m}-{j}. v ! i)"
  2449       by (subst sum.insert) simp_all
  2450     finally have "(\<Sum>i\<in>{0..m}-{j}. v ! i) = 0" by simp
  2451     hence zero: "v ! i = 0" if "i \<in> {0..m}-{j}" for i using that
  2452       by (subst (asm) sum_eq_0_iff) auto
  2453       
  2454     from j have "{0..m} = insert j ({0..m} - {j})" by auto
  2455     also from j have "(\<Prod>i\<in>\<dots>. f $ (v ! i)) = f $ k * (\<Prod>i\<in>{0..m} - {j}. f $ (v ! i))"
  2456       by (subst prod.insert) auto
  2457     also have "(\<Prod>i\<in>{0..m} - {j}. f $ (v ! i)) = (\<Prod>i\<in>{0..m} - {j}. f $ 0)"
  2458       by (intro prod.cong) (simp_all add: zero)
  2459     also from j have "\<dots> = (f $ 0) ^ m" by (subst prod_constant) simp_all
  2460     finally have "(\<Prod>j = 0..m. f $ (v ! j)) = f $ k * (f $ 0) ^ m" .
  2461   } note A = this
  2462   
  2463   have "(f ^ Suc m) $ k = (\<Sum>v\<in>natpermute k (m + 1). \<Prod>j = 0..m. f $ v ! j)"
  2464     by (rule fps_power_nth_Suc)
  2465   also have "natpermute k (m+1) = A \<union> B" unfolding A_def B_def by blast
  2466   also have "(\<Sum>v\<in>\<dots>. \<Prod>j = 0..m. f $ (v ! j)) = 
  2467                (\<Sum>v\<in>A. \<Prod>j = 0..m. f $ (v ! j)) + (\<Sum>v\<in>B. \<Prod>j = 0..m. f $ (v ! j))"
  2468     by (intro sum.union_disjoint) simp_all   
  2469   also have "(\<Sum>v\<in>A. \<Prod>j = 0..m. f $ (v ! j)) = of_nat (Suc m) * (f $ k * (f $ 0) ^ m)"
  2470     by (simp add: A card_A)
  2471   finally show ?thesis by (simp add: B_def)
  2472 qed 
  2473   
  2474 lemma fps_power_Suc_eqD:
  2475   fixes f g :: "'a :: {idom,semiring_char_0} fps"
  2476   assumes "f ^ Suc m = g ^ Suc m" "f $ 0 = g $ 0" "f $ 0 \<noteq> 0"
  2477   shows   "f = g"
  2478 proof (rule fps_ext)
  2479   fix k :: nat
  2480   show "f $ k = g $ k"
  2481   proof (induction k rule: less_induct)
  2482     case (less k)
  2483     show ?case
  2484     proof (cases "k = 0")
  2485       case False
  2486       let ?h = "\<lambda>f. (\<Sum>v | v \<in> natpermute k (m + 1) \<and> k \<notin> set v. \<Prod>j = 0..m. f $ v ! j)"
  2487       from False fps_power_Suc_nth[of k f m] fps_power_Suc_nth[of k g m]
  2488         have "f $ k * (of_nat (Suc m) * (f $ 0) ^ m) + ?h f =
  2489                 g $ k * (of_nat (Suc m) * (f $ 0) ^ m) + ?h g" using assms 
  2490         by (simp add: mult_ac del: power_Suc of_nat_Suc)
  2491       also have "v ! i < k" if "v \<in> {v\<in>natpermute k (m+1). k \<notin> set v}" "i \<le> m" for v i
  2492         using that elem_le_sum_list_nat[of i v] unfolding natpermute_def
  2493         by (auto simp: set_conv_nth dest!: spec[of _ i])
  2494       hence "?h f = ?h g"
  2495         by (intro sum.cong refl prod.cong less lessI) (auto simp: natpermute_def)
  2496       finally have "f $ k * (of_nat (Suc m) * (f $ 0) ^ m) = g $ k * (of_nat (Suc m) * (f $ 0) ^ m)"
  2497         by simp
  2498       with assms show "f $ k = g $ k" 
  2499         by (subst (asm) mult_right_cancel) (auto simp del: of_nat_Suc)
  2500     qed (simp_all add: assms)
  2501   qed
  2502 qed
  2503 
  2504 lemma fps_power_Suc_eqD':
  2505   fixes f g :: "'a :: {idom,semiring_char_0} fps"
  2506   assumes "f ^ Suc m = g ^ Suc m" "f $ subdegree f = g $ subdegree g"
  2507   shows   "f = g"
  2508 proof (cases "f = 0")
  2509   case False
  2510   have "Suc m * subdegree f = subdegree (f ^ Suc m)"
  2511     by (rule subdegree_power [symmetric])
  2512   also have "f ^ Suc m = g ^ Suc m" by fact
  2513   also have "subdegree \<dots> = Suc m * subdegree g" by (rule subdegree_power)
  2514   finally have [simp]: "subdegree f = subdegree g"
  2515     by (subst (asm) Suc_mult_cancel1)
  2516   have "fps_shift (subdegree f) f * X ^ subdegree f = f"
  2517     by (rule subdegree_decompose [symmetric])
  2518   also have "\<dots> ^ Suc m = g ^ Suc m" by fact
  2519   also have "g = fps_shift (subdegree g) g * X ^ subdegree g"
  2520     by (rule subdegree_decompose)
  2521   also have "subdegree f = subdegree g" by fact
  2522   finally have "fps_shift (subdegree g) f ^ Suc m = fps_shift (subdegree g) g ^ Suc m"
  2523     by (simp add: algebra_simps power_mult_distrib del: power_Suc)
  2524   hence "fps_shift (subdegree g) f = fps_shift (subdegree g) g"
  2525     by (rule fps_power_Suc_eqD) (insert assms False, auto)
  2526   with subdegree_decompose[of f] subdegree_decompose[of g] show ?thesis by simp
  2527 qed (insert assms, simp_all)
  2528 
  2529 lemma fps_power_eqD':
  2530   fixes f g :: "'a :: {idom,semiring_char_0} fps"
  2531   assumes "f ^ m = g ^ m" "f $ subdegree f = g $ subdegree g" "m > 0"
  2532   shows   "f = g"
  2533   using fps_power_Suc_eqD'[of f "m-1" g] assms by simp
  2534 
  2535 lemma fps_power_eqD:
  2536   fixes f g :: "'a :: {idom,semiring_char_0} fps"
  2537   assumes "f ^ m = g ^ m" "f $ 0 = g $ 0" "f $ 0 \<noteq> 0" "m > 0"
  2538   shows   "f = g"
  2539   by (rule fps_power_eqD'[of f m g]) (insert assms, simp_all)
  2540 
  2541 lemma fps_compose_inj_right:
  2542   assumes a0: "a$0 = (0::'a::idom)"
  2543     and a1: "a$1 \<noteq> 0"
  2544   shows "(b oo a = c oo a) \<longleftrightarrow> b = c"
  2545   (is "?lhs \<longleftrightarrow>?rhs")
  2546 proof
  2547   show ?lhs if ?rhs using that by simp
  2548   show ?rhs if ?lhs
  2549   proof -
  2550     have "b$n = c$n" for n
  2551     proof (induct n rule: nat_less_induct)
  2552       fix n
  2553       assume H: "\<forall>m<n. b$m = c$m"
  2554       show "b$n = c$n"
  2555       proof (cases n)
  2556         case 0
  2557         from \<open>?lhs\<close> have "(b oo a)$n = (c oo a)$n"
  2558           by simp
  2559         then show ?thesis
  2560           using 0 by (simp add: fps_compose_nth)
  2561       next
  2562         case (Suc n1)
  2563         have f: "finite {0 .. n1}" "finite {n}" by simp_all
  2564         have eq: "{0 .. n1} \<union> {n} = {0 .. n}" using Suc by auto
  2565         have d: "{0 .. n1} \<inter> {n} = {}" using Suc by auto
  2566         have seq: "(\<Sum>i = 0..n1. b $ i * a ^ i $ n) = (\<Sum>i = 0..n1. c $ i * a ^ i $ n)"
  2567           apply (rule sum.cong)
  2568           using H Suc
  2569           apply auto
  2570           done
  2571         have th0: "(b oo a) $n = (\<Sum>i = 0..n1. c $ i * a ^ i $ n) + b$n * (a$1)^n"
  2572           unfolding fps_compose_nth sum.union_disjoint[OF f d, unfolded eq] seq
  2573           using startsby_zero_power_nth_same[OF a0]
  2574           by simp
  2575         have th1: "(c oo a) $n = (\<Sum>i = 0..n1. c $ i * a ^ i $ n) + c$n * (a$1)^n"
  2576           unfolding fps_compose_nth sum.union_disjoint[OF f d, unfolded eq]
  2577           using startsby_zero_power_nth_same[OF a0]
  2578           by simp
  2579         from \<open>?lhs\<close>[unfolded fps_eq_iff, rule_format, of n] th0 th1 a1
  2580         show ?thesis by auto
  2581       qed
  2582     qed
  2583     then show ?rhs by (simp add: fps_eq_iff)
  2584   qed
  2585 qed
  2586 
  2587 
  2588 subsection \<open>Radicals\<close>
  2589 
  2590 declare prod.cong [fundef_cong]
  2591 
  2592 function radical :: "(nat \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a::field fps \<Rightarrow> nat \<Rightarrow> 'a"
  2593 where
  2594   "radical r 0 a 0 = 1"
  2595 | "radical r 0 a (Suc n) = 0"
  2596 | "radical r (Suc k) a 0 = r (Suc k) (a$0)"
  2597 | "radical r (Suc k) a (Suc n) =
  2598     (a$ Suc n - sum (\<lambda>xs. prod (\<lambda>j. radical r (Suc k) a (xs ! j)) {0..k})
  2599       {xs. xs \<in> natpermute (Suc n) (Suc k) \<and> Suc n \<notin> set xs}) /
  2600     (of_nat (Suc k) * (radical r (Suc k) a 0)^k)"
  2601   by pat_completeness auto
  2602 
  2603 termination radical
  2604 proof
  2605   let ?R = "measure (\<lambda>(r, k, a, n). n)"
  2606   {
  2607     show "wf ?R" by auto
  2608   next
  2609     fix r k a n xs i
  2610     assume xs: "xs \<in> {xs \<in> natpermute (Suc n) (Suc k). Suc n \<notin> set xs}" and i: "i \<in> {0..k}"
  2611     have False if c: "Suc n \<le> xs ! i"
  2612     proof -
  2613       from xs i have "xs !i \<noteq> Suc n"
  2614         by (auto simp add: in_set_conv_nth natpermute_def)
  2615       with c have c': "Suc n < xs!i" by arith
  2616       have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}"
  2617         by simp_all
  2618       have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}"
  2619         by auto
  2620       have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})"
  2621         using i by auto
  2622       from xs have "Suc n = sum_list xs"
  2623         by (simp add: natpermute_def)
  2624       also have "\<dots> = sum (nth xs) {0..<Suc k}" using xs
  2625         by (simp add: natpermute_def sum_list_sum_nth)
  2626       also have "\<dots> = xs!i + sum (nth xs) {0..<i} + sum (nth xs) {i+1..<Suc k}"
  2627         unfolding eqs  sum.union_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
  2628         unfolding sum.union_disjoint[OF fths(2) fths(3) d(2)]
  2629         by simp
  2630       finally show ?thesis using c' by simp
  2631     qed
  2632     then show "((r, Suc k, a, xs!i), r, Suc k, a, Suc n) \<in> ?R"
  2633       apply auto
  2634       apply (metis not_less)
  2635       done
  2636   next
  2637     fix r k a n
  2638     show "((r, Suc k, a, 0), r, Suc k, a, Suc n) \<in> ?R" by simp
  2639   }
  2640 qed
  2641 
  2642 definition "fps_radical r n a = Abs_fps (radical r n a)"
  2643 
  2644 lemma fps_radical0[simp]: "fps_radical r 0 a = 1"
  2645   apply (auto simp add: fps_eq_iff fps_radical_def)
  2646   apply (case_tac n)
  2647   apply auto
  2648   done
  2649 
  2650 lemma fps_radical_nth_0[simp]: "fps_radical r n a $ 0 = (if n = 0 then 1 else r n (a$0))"
  2651   by (cases n) (simp_all add: fps_radical_def)
  2652 
  2653 lemma fps_radical_power_nth[simp]:
  2654   assumes r: "(r k (a$0)) ^ k = a$0"
  2655   shows "fps_radical r k a ^ k $ 0 = (if k = 0 then 1 else a$0)"
  2656 proof (cases k)
  2657   case 0
  2658   then show ?thesis by simp
  2659 next
  2660   case (Suc h)
  2661   have eq1: "fps_radical r k a ^ k $ 0 = (\<Prod>j\<in>{0..h}. fps_radical r k a $ (replicate k 0) ! j)"
  2662     unfolding fps_power_nth Suc by simp
  2663   also have "\<dots> = (\<Prod>j\<in>{0..h}. r k (a$0))"
  2664     apply (rule prod.cong)
  2665     apply simp
  2666     using Suc
  2667     apply (subgoal_tac "replicate k 0 ! x = 0")
  2668     apply (auto intro: nth_replicate simp del: replicate.simps)
  2669     done
  2670   also have "\<dots> = a$0"
  2671     using r Suc by (simp add: prod_constant)
  2672   finally show ?thesis
  2673     using Suc by simp
  2674 qed
  2675 
  2676 lemma power_radical:
  2677   fixes a:: "'a::field_char_0 fps"
  2678   assumes a0: "a$0 \<noteq> 0"
  2679   shows "(r (Suc k) (a$0)) ^ Suc k = a$0 \<longleftrightarrow> (fps_radical r (Suc k) a) ^ (Suc k) = a"
  2680     (is "?lhs \<longleftrightarrow> ?rhs")
  2681 proof
  2682   let ?r = "fps_radical r (Suc k) a"
  2683   show ?rhs if r0: ?lhs
  2684   proof -
  2685     from a0 r0 have r00: "r (Suc k) (a$0) \<noteq> 0" by auto
  2686     have "?r ^ Suc k $ z = a$z" for z
  2687     proof (induct z rule: nat_less_induct)
  2688       fix n
  2689       assume H: "\<forall>m<n. ?r ^ Suc k $ m = a$m"
  2690       show "?r ^ Suc k $ n = a $n"
  2691       proof (cases n)
  2692         case 0
  2693         then show ?thesis
  2694           using fps_radical_power_nth[of r "Suc k" a, OF r0] by simp
  2695       next
  2696         case (Suc n1)
  2697         then have "n \<noteq> 0" by simp
  2698         let ?Pnk = "natpermute n (k + 1)"
  2699         let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
  2700         let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
  2701         have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
  2702         have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
  2703         have f: "finite ?Pnkn" "finite ?Pnknn"
  2704           using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
  2705           by (metis natpermute_finite)+
  2706         let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
  2707         have "sum ?f ?Pnkn = sum (\<lambda>v. ?r $ n * r (Suc k) (a $ 0) ^ k) ?Pnkn"
  2708         proof (rule sum.cong)
  2709           fix v assume v: "v \<in> {xs \<in> natpermute n (k + 1). n \<in> set xs}"
  2710           let ?ths = "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) =
  2711             fps_radical r (Suc k) a $ n * r (Suc k) (a $ 0) ^ k"
  2712           from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
  2713             unfolding natpermute_contain_maximal by auto
  2714           have "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) =
  2715               (\<Prod>j\<in>{0..k}. if j = i then fps_radical r (Suc k) a $ n else r (Suc k) (a$0))"
  2716             apply (rule prod.cong, simp)
  2717             using i r0
  2718             apply (simp del: replicate.simps)
  2719             done
  2720           also have "\<dots> = (fps_radical r (Suc k) a $ n) * r (Suc k) (a$0) ^ k"
  2721             using i r0 by (simp add: prod_gen_delta)
  2722           finally show ?ths .
  2723         qed rule
  2724         then have "sum ?f ?Pnkn = of_nat (k+1) * ?r $ n * r (Suc k) (a $ 0) ^ k"
  2725           by (simp add: natpermute_max_card[OF \<open>n \<noteq> 0\<close>, simplified])
  2726         also have "\<dots> = a$n - sum ?f ?Pnknn"
  2727           unfolding Suc using r00 a0 by (simp add: field_simps fps_radical_def del: of_nat_Suc)
  2728         finally have fn: "sum ?f ?Pnkn = a$n - sum ?f ?Pnknn" .
  2729         have "(?r ^ Suc k)$n = sum ?f ?Pnkn + sum ?f ?Pnknn"
  2730           unfolding fps_power_nth_Suc sum.union_disjoint[OF f d, unfolded eq] ..
  2731         also have "\<dots> = a$n" unfolding fn by simp
  2732         finally show ?thesis .
  2733       qed
  2734     qed
  2735     then show ?thesis using r0 by (simp add: fps_eq_iff)
  2736   qed
  2737   show ?lhs if ?rhs
  2738   proof -
  2739     from that have "((fps_radical r (Suc k) a) ^ (Suc k))$0 = a$0"
  2740       by simp
  2741     then show ?thesis
  2742       unfolding fps_power_nth_Suc
  2743       by (simp add: prod_constant del: replicate.simps)
  2744   qed
  2745 qed
  2746 
  2747 (*
  2748 lemma power_radical:
  2749   fixes a:: "'a::field_char_0 fps"
  2750   assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 \<noteq> 0"
  2751   shows "(fps_radical r (Suc k) a) ^ (Suc k) = a"
  2752 proof-
  2753   let ?r = "fps_radical r (Suc k) a"
  2754   from a0 r0 have r00: "r (Suc k) (a$0) \<noteq> 0" by auto
  2755   {fix z have "?r ^ Suc k $ z = a$z"
  2756     proof(induct z rule: nat_less_induct)
  2757       fix n assume H: "\<forall>m<n. ?r ^ Suc k $ m = a$m"
  2758       {assume "n = 0" then have "?r ^ Suc k $ n = a $n"
  2759           using fps_radical_power_nth[of r "Suc k" a, OF r0] by simp}
  2760       moreover
  2761       {fix n1 assume n1: "n = Suc n1"
  2762         have fK: "finite {0..k}" by simp
  2763         have nz: "n \<noteq> 0" using n1 by arith
  2764         let ?Pnk = "natpermute n (k + 1)"
  2765         let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
  2766         let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
  2767         have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
  2768         have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
  2769         have f: "finite ?Pnkn" "finite ?Pnknn"
  2770           using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
  2771           by (metis natpermute_finite)+
  2772         let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
  2773         have "sum ?f ?Pnkn = sum (\<lambda>v. ?r $ n * r (Suc k) (a $ 0) ^ k) ?Pnkn"
  2774         proof(rule sum.cong2)
  2775           fix v assume v: "v \<in> {xs \<in> natpermute n (k + 1). n \<in> set xs}"
  2776           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"
  2777           from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
  2778             unfolding natpermute_contain_maximal by auto
  2779           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))"
  2780             apply (rule prod.cong, simp)
  2781             using i r0 by (simp del: replicate.simps)
  2782           also have "\<dots> = (fps_radical r (Suc k) a $ n) * r (Suc k) (a$0) ^ k"
  2783             unfolding prod_gen_delta[OF fK] using i r0 by simp
  2784           finally show ?ths .
  2785         qed
  2786         then have "sum ?f ?Pnkn = of_nat (k+1) * ?r $ n * r (Suc k) (a $ 0) ^ k"
  2787           by (simp add: natpermute_max_card[OF nz, simplified])
  2788         also have "\<dots> = a$n - sum ?f ?Pnknn"
  2789           unfolding n1 using r00 a0 by (simp add: field_simps fps_radical_def del: of_nat_Suc )
  2790         finally have fn: "sum ?f ?Pnkn = a$n - sum ?f ?Pnknn" .
  2791         have "(?r ^ Suc k)$n = sum ?f ?Pnkn + sum ?f ?Pnknn"
  2792           unfolding fps_power_nth_Suc sum.union_disjoint[OF f d, unfolded eq] ..
  2793         also have "\<dots> = a$n" unfolding fn by simp
  2794         finally have "?r ^ Suc k $ n = a $n" .}
  2795       ultimately  show "?r ^ Suc k $ n = a $n" by (cases n, auto)
  2796   qed }
  2797   then show ?thesis by (simp add: fps_eq_iff)
  2798 qed
  2799 
  2800 *)
  2801 lemma eq_divide_imp':
  2802   fixes c :: "'a::field"
  2803   shows "c \<noteq> 0 \<Longrightarrow> a * c = b \<Longrightarrow> a = b / c"
  2804   by (simp add: field_simps)
  2805 
  2806 lemma radical_unique:
  2807   assumes r0: "(r (Suc k) (b$0)) ^ Suc k = b$0"
  2808     and a0: "r (Suc k) (b$0 ::'a::field_char_0) = a$0"
  2809     and b0: "b$0 \<noteq> 0"
  2810   shows "a^(Suc k) = b \<longleftrightarrow> a = fps_radical r (Suc k) b"
  2811     (is "?lhs \<longleftrightarrow> ?rhs" is "_ \<longleftrightarrow> a = ?r")
  2812 proof
  2813   show ?lhs if ?rhs
  2814     using that using power_radical[OF b0, of r k, unfolded r0] by simp
  2815   show ?rhs if ?lhs
  2816   proof -
  2817     have r00: "r (Suc k) (b$0) \<noteq> 0" using b0 r0 by auto
  2818     have ceq: "card {0..k} = Suc k" by simp
  2819     from a0 have a0r0: "a$0 = ?r$0" by simp
  2820     have "a $ n = ?r $ n" for n
  2821     proof (induct n rule: nat_less_induct)
  2822       fix n
  2823       assume h: "\<forall>m<n. a$m = ?r $m"
  2824       show "a$n = ?r $ n"
  2825       proof (cases n)
  2826         case 0
  2827         then show ?thesis using a0 by simp
  2828       next
  2829         case (Suc n1)
  2830         have fK: "finite {0..k}" by simp
  2831         have nz: "n \<noteq> 0" using Suc by simp
  2832         let ?Pnk = "natpermute n (Suc k)"
  2833         let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
  2834         let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
  2835         have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
  2836         have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
  2837         have f: "finite ?Pnkn" "finite ?Pnknn"
  2838           using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
  2839           by (metis natpermute_finite)+
  2840         let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
  2841         let ?g = "\<lambda>v. \<Prod>j\<in>{0..k}. a $ v ! j"
  2842         have "sum ?g ?Pnkn = sum (\<lambda>v. a $ n * (?r$0)^k) ?Pnkn"
  2843         proof (rule sum.cong)
  2844           fix v
  2845           assume v: "v \<in> {xs \<in> natpermute n (Suc k). n \<in> set xs}"
  2846           let ?ths = "(\<Prod>j\<in>{0..k}. a $ v ! j) = a $ n * (?r$0)^k"
  2847           from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
  2848             unfolding Suc_eq_plus1 natpermute_contain_maximal
  2849             by (auto simp del: replicate.simps)
  2850           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))"
  2851             apply (rule prod.cong, simp)
  2852             using i a0
  2853             apply (simp del: replicate.simps)
  2854             done
  2855           also have "\<dots> = a $ n * (?r $ 0)^k"
  2856             using i by (simp add: prod_gen_delta)
  2857           finally show ?ths .
  2858         qed rule
  2859         then have th0: "sum ?g ?Pnkn = of_nat (k+1) * a $ n * (?r $ 0)^k"
  2860           by (simp add: natpermute_max_card[OF nz, simplified])
  2861         have th1: "sum ?g ?Pnknn = sum ?f ?Pnknn"
  2862         proof (rule sum.cong, rule refl, rule prod.cong, simp)
  2863           fix xs i
  2864           assume xs: "xs \<in> ?Pnknn" and i: "i \<in> {0..k}"
  2865           have False if c: "n \<le> xs ! i"
  2866           proof -
  2867             from xs i have "xs ! i \<noteq> n"
  2868               by (auto simp add: in_set_conv_nth natpermute_def)
  2869             with c have c': "n < xs!i" by arith
  2870             have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}"
  2871               by simp_all
  2872             have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}"
  2873               by auto
  2874             have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})"
  2875               using i by auto
  2876             from xs have "n = sum_list xs"
  2877               by (simp add: natpermute_def)
  2878             also have "\<dots> = sum (nth xs) {0..<Suc k}"
  2879               using xs by (simp add: natpermute_def sum_list_sum_nth)
  2880             also have "\<dots> = xs!i + sum (nth xs) {0..<i} + sum (nth xs) {i+1..<Suc k}"
  2881               unfolding eqs  sum.union_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
  2882               unfolding sum.union_disjoint[OF fths(2) fths(3) d(2)]
  2883               by simp
  2884             finally show ?thesis using c' by simp
  2885           qed
  2886           then have thn: "xs!i < n" by presburger
  2887           from h[rule_format, OF thn] show "a$(xs !i) = ?r$(xs!i)" .
  2888         qed
  2889         have th00: "\<And>x::'a. of_nat (Suc k) * (x * inverse (of_nat (Suc k))) = x"
  2890           by (simp add: field_simps del: of_nat_Suc)
  2891         from \<open>?lhs\<close> have "b$n = a^Suc k $ n"
  2892           by (simp add: fps_eq_iff)
  2893         also have "a ^ Suc k$n = sum ?g ?Pnkn + sum ?g ?Pnknn"
  2894           unfolding fps_power_nth_Suc
  2895           using sum.union_disjoint[OF f d, unfolded Suc_eq_plus1[symmetric],
  2896             unfolded eq, of ?g] by simp
  2897         also have "\<dots> = of_nat (k+1) * a $ n * (?r $ 0)^k + sum ?f ?Pnknn"
  2898           unfolding th0 th1 ..
  2899         finally have "of_nat (k+1) * a $ n * (?r $ 0)^k = b$n - sum ?f ?Pnknn"
  2900           by simp
  2901         then have "a$n = (b$n - sum ?f ?Pnknn) / (of_nat (k+1) * (?r $ 0)^k)"
  2902           apply -
  2903           apply (rule eq_divide_imp')
  2904           using r00
  2905           apply (simp del: of_nat_Suc)
  2906           apply (simp add: ac_simps)
  2907           done
  2908         then show ?thesis
  2909           apply (simp del: of_nat_Suc)
  2910           unfolding fps_radical_def Suc
  2911           apply (simp add: field_simps Suc th00 del: of_nat_Suc)
  2912           done
  2913       qed
  2914     qed
  2915     then show ?rhs by (simp add: fps_eq_iff)
  2916   qed
  2917 qed
  2918 
  2919 
  2920 lemma radical_power:
  2921   assumes r0: "r (Suc k) ((a$0) ^ Suc k) = a$0"
  2922     and a0: "(a$0 :: 'a::field_char_0) \<noteq> 0"
  2923   shows "(fps_radical r (Suc k) (a ^ Suc k)) = a"
  2924 proof -
  2925   let ?ak = "a^ Suc k"
  2926   have ak0: "?ak $ 0 = (a$0) ^ Suc k"
  2927     by (simp add: fps_nth_power_0 del: power_Suc)
  2928   from r0 have th0: "r (Suc k) (a ^ Suc k $ 0) ^ Suc k = a ^ Suc k $ 0"
  2929     using ak0 by auto
  2930   from r0 ak0 have th1: "r (Suc k) (a ^ Suc k $ 0) = a $ 0"
  2931     by auto
  2932   from ak0 a0 have ak00: "?ak $ 0 \<noteq>0 "
  2933     by auto
  2934   from radical_unique[of r k ?ak a, OF th0 th1 ak00] show ?thesis
  2935     by metis
  2936 qed
  2937 
  2938 lemma fps_deriv_radical:
  2939   fixes a :: "'a::field_char_0 fps"
  2940   assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0"
  2941     and a0: "a$0 \<noteq> 0"
  2942   shows "fps_deriv (fps_radical r (Suc k) a) =
  2943     fps_deriv a / (fps_const (of_nat (Suc k)) * (fps_radical r (Suc k) a) ^ k)"
  2944 proof -
  2945   let ?r = "fps_radical r (Suc k) a"
  2946   let ?w = "(fps_const (of_nat (Suc k)) * ?r ^ k)"
  2947   from a0 r0 have r0': "r (Suc k) (a$0) \<noteq> 0"
  2948     by auto
  2949   from r0' have w0: "?w $ 0 \<noteq> 0"
  2950     by (simp del: of_nat_Suc)
  2951   note th0 = inverse_mult_eq_1[OF w0]
  2952   let ?iw = "inverse ?w"
  2953   from iffD1[OF power_radical[of a r], OF a0 r0]
  2954   have "fps_deriv (?r ^ Suc k) = fps_deriv a"
  2955     by simp
  2956   then have "fps_deriv ?r * ?w = fps_deriv a"
  2957     by (simp add: fps_deriv_power ac_simps del: power_Suc)
  2958   then have "?iw * fps_deriv ?r * ?w = ?iw * fps_deriv a"
  2959     by simp
  2960   with a0 r0 have "fps_deriv ?r * (?iw * ?w) = fps_deriv a / ?w"
  2961     by (subst fps_divide_unit) (auto simp del: of_nat_Suc)
  2962   then show ?thesis unfolding th0 by simp
  2963 qed
  2964 
  2965 lemma radical_mult_distrib:
  2966   fixes a :: "'a::field_char_0 fps"
  2967   assumes k: "k > 0"
  2968     and ra0: "r k (a $ 0) ^ k = a $ 0"
  2969     and rb0: "r k (b $ 0) ^ k = b $ 0"
  2970     and a0: "a $ 0 \<noteq> 0"
  2971     and b0: "b $ 0 \<noteq> 0"
  2972   shows "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0) \<longleftrightarrow>
  2973     fps_radical r k (a * b) = fps_radical r k a * fps_radical r k b"
  2974     (is "?lhs \<longleftrightarrow> ?rhs")
  2975 proof
  2976   show ?rhs if r0': ?lhs
  2977   proof -
  2978     from r0' have r0: "(r k ((a * b) $ 0)) ^ k = (a * b) $ 0"
  2979       by (simp add: fps_mult_nth ra0 rb0 power_mult_distrib)
  2980     show ?thesis
  2981     proof (cases k)
  2982       case 0
  2983       then show ?thesis using r0' by simp
  2984     next
  2985       case (Suc h)
  2986       let ?ra = "fps_radical r (Suc h) a"
  2987       let ?rb = "fps_radical r (Suc h) b"
  2988       have th0: "r (Suc h) ((a * b) $ 0) = (fps_radical r (Suc h) a * fps_radical r (Suc h) b) $ 0"
  2989         using r0' Suc by (simp add: fps_mult_nth)
  2990       have ab0: "(a*b) $ 0 \<noteq> 0"
  2991         using a0 b0 by (simp add: fps_mult_nth)
  2992       from radical_unique[of r h "a*b" "fps_radical r (Suc h) a * fps_radical r (Suc h) b", OF r0[unfolded Suc] th0 ab0, symmetric]
  2993         iffD1[OF power_radical[of _ r], OF a0 ra0[unfolded Suc]] iffD1[OF power_radical[of _ r], OF b0 rb0[unfolded Suc]] Suc r0'
  2994       show ?thesis
  2995         by (auto simp add: power_mult_distrib simp del: power_Suc)
  2996     qed
  2997   qed
  2998   show ?lhs if ?rhs
  2999   proof -
  3000     from that have "(fps_radical r k (a * b)) $ 0 = (fps_radical r k a * fps_radical r k b) $ 0"
  3001       by simp
  3002     then show ?thesis
  3003       using k by (simp add: fps_mult_nth)
  3004   qed
  3005 qed
  3006 
  3007 (*
  3008 lemma radical_mult_distrib:
  3009   fixes a:: "'a::field_char_0 fps"
  3010   assumes
  3011   ra0: "r k (a $ 0) ^ k = a $ 0"
  3012   and rb0: "r k (b $ 0) ^ k = b $ 0"
  3013   and r0': "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0)"
  3014   and a0: "a$0 \<noteq> 0"
  3015   and b0: "b$0 \<noteq> 0"
  3016   shows "fps_radical r (k) (a*b) = fps_radical r (k) a * fps_radical r (k) (b)"
  3017 proof-
  3018   from r0' have r0: "(r (k) ((a*b)$0)) ^ k = (a*b)$0"
  3019     by (simp add: fps_mult_nth ra0 rb0 power_mult_distrib)
  3020   {assume "k=0" then have ?thesis by simp}
  3021   moreover
  3022   {fix h assume k: "k = Suc h"
  3023   let ?ra = "fps_radical r (Suc h) a"
  3024   let ?rb = "fps_radical r (Suc h) b"
  3025   have th0: "r (Suc h) ((a * b) $ 0) = (fps_radical r (Suc h) a * fps_radical r (Suc h) b) $ 0"
  3026     using r0' k by (simp add: fps_mult_nth)
  3027   have ab0: "(a*b) $ 0 \<noteq> 0" using a0 b0 by (simp add: fps_mult_nth)
  3028   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]
  3029     power_radical[of r, OF ra0[unfolded k] a0] power_radical[of r, OF rb0[unfolded k] b0] k
  3030   have ?thesis by (auto simp add: power_mult_distrib simp del: power_Suc)}
  3031 ultimately show ?thesis by (cases k, auto)
  3032 qed
  3033 *)
  3034 
  3035 lemma fps_divide_1 [simp]: "(a :: 'a::field fps) / 1 = a"
  3036   by (fact div_by_1)
  3037 
  3038 lemma radical_divide:
  3039   fixes a :: "'a::field_char_0 fps"
  3040   assumes kp: "k > 0"
  3041     and ra0: "(r k (a $ 0)) ^ k = a $ 0"
  3042     and rb0: "(r k (b $ 0)) ^ k = b $ 0"
  3043     and a0: "a$0 \<noteq> 0"
  3044     and b0: "b$0 \<noteq> 0"
  3045   shows "r k ((a $ 0) / (b$0)) = r k (a$0) / r k (b $ 0) \<longleftrightarrow>
  3046     fps_radical r k (a/b) = fps_radical r k a / fps_radical r k b"
  3047   (is "?lhs = ?rhs")
  3048 proof
  3049   let ?r = "fps_radical r k"
  3050   from kp obtain h where k: "k = Suc h"
  3051     by (cases k) auto
  3052   have ra0': "r k (a$0) \<noteq> 0" using a0 ra0 k by auto
  3053   have rb0': "r k (b$0) \<noteq> 0" using b0 rb0 k by auto
  3054 
  3055   show ?lhs if ?rhs
  3056   proof -
  3057     from that have "?r (a/b) $ 0 = (?r a / ?r b)$0"
  3058       by simp
  3059     then show ?thesis
  3060       using k a0 b0 rb0' by (simp add: fps_divide_unit fps_mult_nth fps_inverse_def divide_inverse)
  3061   qed
  3062   show ?rhs if ?lhs
  3063   proof -
  3064     from a0 b0 have ab0[simp]: "(a/b)$0 = a$0 / b$0"
  3065       by (simp add: fps_divide_def fps_mult_nth divide_inverse fps_inverse_def)
  3066     have th0: "r k ((a/b)$0) ^ k = (a/b)$0"
  3067       by (simp add: \<open>?lhs\<close> power_divide ra0 rb0)
  3068     from a0 b0 ra0' rb0' kp \<open>?lhs\<close>
  3069     have th1: "r k ((a / b) $ 0) = (fps_radical r k a / fps_radical r k b) $ 0"
  3070       by (simp add: fps_divide_unit fps_mult_nth fps_inverse_def divide_inverse)
  3071     from a0 b0 ra0' rb0' kp have ab0': "(a / b) $ 0 \<noteq> 0"
  3072       by (simp add: fps_divide_unit fps_mult_nth fps_inverse_def nonzero_imp_inverse_nonzero)
  3073     note tha[simp] = iffD1[OF power_radical[where r=r and k=h], OF a0 ra0[unfolded k], unfolded k[symmetric]]
  3074     note thb[simp] = iffD1[OF power_radical[where r=r and k=h], OF b0 rb0[unfolded k], unfolded k[symmetric]]
  3075     from b0 rb0' have th2: "(?r a / ?r b)^k = a/b"
  3076       by (simp add: fps_divide_unit power_mult_distrib fps_inverse_power[symmetric])
  3077 
  3078     from iffD1[OF radical_unique[where r=r and a="?r a / ?r b" and b="a/b" and k=h], symmetric, unfolded k[symmetric], OF th0 th1 ab0' th2]
  3079     show ?thesis .
  3080   qed
  3081 qed
  3082 
  3083 lemma radical_inverse:
  3084   fixes a :: "'a::field_char_0 fps"
  3085   assumes k: "k > 0"
  3086     and ra0: "r k (a $ 0) ^ k = a $ 0"
  3087     and r1: "(r k 1)^k = 1"
  3088     and a0: "a$0 \<noteq> 0"
  3089   shows "r k (inverse (a $ 0)) = r k 1 / (r k (a $ 0)) \<longleftrightarrow>
  3090     fps_radical r k (inverse a) = fps_radical r k 1 / fps_radical r k a"
  3091   using radical_divide[where k=k and r=r and a=1 and b=a, OF k ] ra0 r1 a0
  3092   by (simp add: divide_inverse fps_divide_def)
  3093 
  3094 
  3095 subsection \<open>Derivative of composition\<close>
  3096 
  3097 lemma fps_compose_deriv:
  3098   fixes a :: "'a::idom fps"
  3099   assumes b0: "b$0 = 0"
  3100   shows "fps_deriv (a oo b) = ((fps_deriv a) oo b) * fps_deriv b"
  3101 proof -
  3102   have "(fps_deriv (a oo b))$n = (((fps_deriv a) oo b) * (fps_deriv b)) $n" for n
  3103   proof -
  3104     have "(fps_deriv (a oo b))$n = sum (\<lambda>i. a $ i * (fps_deriv (b^i))$n) {0.. Suc n}"
  3105       by (simp add: fps_compose_def field_simps sum_distrib_left del: of_nat_Suc)
  3106     also have "\<dots> = sum (\<lambda>i. a$i * ((fps_const (of_nat i)) * (fps_deriv b * (b^(i - 1))))$n) {0.. Suc n}"
  3107       by (simp add: field_simps fps_deriv_power del: fps_mult_left_const_nth of_nat_Suc)
  3108     also have "\<dots> = sum (\<lambda>i. of_nat i * a$i * (((b^(i - 1)) * fps_deriv b))$n) {0.. Suc n}"
  3109       unfolding fps_mult_left_const_nth  by (simp add: field_simps)
  3110     also have "\<dots> = sum (\<lambda>i. of_nat i * a$i * (sum (\<lambda>j. (b^ (i - 1))$j * (fps_deriv b)$(n - j)) {0..n})) {0.. Suc n}"
  3111       unfolding fps_mult_nth ..
  3112     also have "\<dots> = sum (\<lambda>i. of_nat i * a$i * (sum (\<lambda>j. (b^ (i - 1))$j * (fps_deriv b)$(n - j)) {0..n})) {1.. Suc n}"
  3113       apply (rule sum.mono_neutral_right)
  3114       apply (auto simp add: mult_delta_left sum.delta not_le)
  3115       done
  3116     also have "\<dots> = sum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (sum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}"
  3117       unfolding fps_deriv_nth
  3118       by (rule sum.reindex_cong [of Suc]) (auto simp add: mult.assoc)
  3119     finally have th0: "(fps_deriv (a oo b))$n =
  3120       sum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (sum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}" .
  3121 
  3122     have "(((fps_deriv a) oo b) * (fps_deriv b))$n = sum (\<lambda>i. (fps_deriv b)$ (n - i) * ((fps_deriv a) oo b)$i) {0..n}"
  3123       unfolding fps_mult_nth by (simp add: ac_simps)
  3124     also have "\<dots> = sum (\<lambda>i. sum (\<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}"
  3125       unfolding fps_deriv_nth fps_compose_nth sum_distrib_left mult.assoc
  3126       apply (rule sum.cong)
  3127       apply (rule refl)
  3128       apply (rule sum.mono_neutral_left)
  3129       apply (simp_all add: subset_eq)
  3130       apply clarify
  3131       apply (subgoal_tac "b^i$x = 0")
  3132       apply simp
  3133       apply (rule startsby_zero_power_prefix[OF b0, rule_format])
  3134       apply simp
  3135       done
  3136     also have "\<dots> = sum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (sum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}"
  3137       unfolding sum_distrib_left
  3138       apply (subst sum.commute)
  3139       apply (rule sum.cong, rule refl)+
  3140       apply simp
  3141       done
  3142     finally show ?thesis
  3143       unfolding th0 by simp
  3144   qed
  3145   then show ?thesis by (simp add: fps_eq_iff)
  3146 qed
  3147 
  3148 lemma fps_mult_X_plus_1_nth:
  3149   "((1+X)*a) $n = (if n = 0 then (a$n :: 'a::comm_ring_1) else a$n + a$(n - 1))"
  3150 proof (cases n)
  3151   case 0
  3152   then show ?thesis
  3153     by (simp add: fps_mult_nth)
  3154 next
  3155   case (Suc m)
  3156   have "((1 + X)*a) $ n = sum (\<lambda>i. (1 + X) $ i * a $ (n - i)) {0..n}"
  3157     by (simp add: fps_mult_nth)
  3158   also have "\<dots> = sum (\<lambda>i. (1+X)$i * a$(n-i)) {0.. 1}"
  3159     unfolding Suc by (rule sum.mono_neutral_right) auto
  3160   also have "\<dots> = (if n = 0 then (a$n :: 'a::comm_ring_1) else a$n + a$(n - 1))"
  3161     by (simp add: Suc)
  3162   finally show ?thesis .
  3163 qed
  3164 
  3165 
  3166 subsection \<open>Finite FPS (i.e. polynomials) and X\<close>
  3167 
  3168 lemma fps_poly_sum_X:
  3169   assumes "\<forall>i > n. a$i = (0::'a::comm_ring_1)"
  3170   shows "a = sum (\<lambda>i. fps_const (a$i) * X^i) {0..n}" (is "a = ?r")
  3171 proof -
  3172   have "a$i = ?r$i" for i
  3173     unfolding fps_sum_nth fps_mult_left_const_nth X_power_nth
  3174     by (simp add: mult_delta_right sum.delta' assms)
  3175   then show ?thesis
  3176     unfolding fps_eq_iff by blast
  3177 qed
  3178 
  3179 
  3180 subsection \<open>Compositional inverses\<close>
  3181 
  3182 fun compinv :: "'a fps \<Rightarrow> nat \<Rightarrow> 'a::field"
  3183 where
  3184   "compinv a 0 = X$0"
  3185 | "compinv a (Suc n) =
  3186     (X$ Suc n - sum (\<lambda>i. (compinv a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
  3187 
  3188 definition "fps_inv a = Abs_fps (compinv a)"
  3189 
  3190 lemma fps_inv:
  3191   assumes a0: "a$0 = 0"
  3192     and a1: "a$1 \<noteq> 0"
  3193   shows "fps_inv a oo a = X"
  3194 proof -
  3195   let ?i = "fps_inv a oo a"
  3196   have "?i $n = X$n" for n
  3197   proof (induct n rule: nat_less_induct)
  3198     fix n
  3199     assume h: "\<forall>m<n. ?i$m = X$m"
  3200     show "?i $ n = X$n"
  3201     proof (cases n)
  3202       case 0
  3203       then show ?thesis using a0
  3204         by (simp add: fps_compose_nth fps_inv_def)
  3205     next
  3206       case (Suc n1)
  3207       have "?i $ n = sum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1} + fps_inv a $ Suc n1 * (a $ 1)^ Suc n1"
  3208         by (simp only: fps_compose_nth) (simp add: Suc startsby_zero_power_nth_same [OF a0] del: power_Suc)
  3209       also have "\<dots> = sum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1} +
  3210         (X$ Suc n1 - sum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1})"
  3211         using a0 a1 Suc by (simp add: fps_inv_def)
  3212       also have "\<dots> = X$n" using Suc by simp
  3213       finally show ?thesis .
  3214     qed
  3215   qed
  3216   then show ?thesis
  3217     by (simp add: fps_eq_iff)
  3218 qed
  3219 
  3220 
  3221 fun gcompinv :: "'a fps \<Rightarrow> 'a fps \<Rightarrow> nat \<Rightarrow> 'a::field"
  3222 where
  3223   "gcompinv b a 0 = b$0"
  3224 | "gcompinv b a (Suc n) =
  3225     (b$ Suc n - sum (\<lambda>i. (gcompinv b a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
  3226 
  3227 definition "fps_ginv b a = Abs_fps (gcompinv b a)"
  3228 
  3229 lemma fps_ginv:
  3230   assumes a0: "a$0 = 0"
  3231     and a1: "a$1 \<noteq> 0"
  3232   shows "fps_ginv b a oo a = b"
  3233 proof -
  3234   let ?i = "fps_ginv b a oo a"
  3235   have "?i $n = b$n" for n
  3236   proof (induct n rule: nat_less_induct)
  3237     fix n
  3238     assume h: "\<forall>m<n. ?i$m = b$m"
  3239     show "?i $ n = b$n"
  3240     proof (cases n)
  3241       case 0
  3242       then show ?thesis using a0
  3243         by (simp add: fps_compose_nth fps_ginv_def)
  3244     next
  3245       case (Suc n1)
  3246       have "?i $ n = sum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1} + fps_ginv b a $ Suc n1 * (a $ 1)^ Suc n1"
  3247         by (simp only: fps_compose_nth) (simp add: Suc startsby_zero_power_nth_same [OF a0] del: power_Suc)
  3248       also have "\<dots> = sum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1} +
  3249         (b$ Suc n1 - sum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1})"
  3250         using a0 a1 Suc by (simp add: fps_ginv_def)
  3251       also have "\<dots> = b$n" using Suc by simp
  3252       finally show ?thesis .
  3253     qed
  3254   qed
  3255   then show ?thesis
  3256     by (simp add: fps_eq_iff)
  3257 qed
  3258 
  3259 lemma fps_inv_ginv: "fps_inv = fps_ginv X"
  3260   apply (auto simp add: fun_eq_iff fps_eq_iff fps_inv_def fps_ginv_def)
  3261   apply (induct_tac n rule: nat_less_induct)
  3262   apply auto
  3263   apply (case_tac na)
  3264   apply simp
  3265   apply simp
  3266   done
  3267 
  3268 lemma fps_compose_1[simp]: "1 oo a = 1"
  3269   by (simp add: fps_eq_iff fps_compose_nth mult_delta_left sum.delta)
  3270 
  3271 lemma fps_compose_0[simp]: "0 oo a = 0"
  3272   by (simp add: fps_eq_iff fps_compose_nth)
  3273 
  3274 lemma fps_compose_0_right[simp]: "a oo 0 = fps_const (a $ 0)"
  3275   by (auto simp add: fps_eq_iff fps_compose_nth power_0_left sum.neutral)
  3276 
  3277 lemma fps_compose_add_distrib: "(a + b) oo c = (a oo c) + (b oo c)"
  3278   by (simp add: fps_eq_iff fps_compose_nth field_simps sum.distrib)
  3279 
  3280 lemma fps_compose_sum_distrib: "(sum f S) oo a = sum (\<lambda>i. f i oo a) S"
  3281 proof (cases "finite S")
  3282   case True
  3283   show ?thesis
  3284   proof (rule finite_induct[OF True])
  3285     show "sum f {} oo a = (\<Sum>i\<in>{}. f i oo a)"
  3286       by simp
  3287   next
  3288     fix x F
  3289     assume fF: "finite F"
  3290       and xF: "x \<notin> F"
  3291       and h: "sum f F oo a = sum (\<lambda>i. f i oo a) F"
  3292     show "sum f (insert x F) oo a  = sum (\<lambda>i. f i oo a) (insert x F)"
  3293       using fF xF h by (simp add: fps_compose_add_distrib)
  3294   qed
  3295 next
  3296   case False
  3297   then show ?thesis by simp
  3298 qed
  3299 
  3300 lemma convolution_eq:
  3301   "sum (\<lambda>i. a (i :: nat) * b (n - i)) {0 .. n} =
  3302     sum (\<lambda>(i,j). a i * b j) {(i,j). i \<le> n \<and> j \<le> n \<and> i + j = n}"
  3303   by (rule sum.reindex_bij_witness[where i=fst and j="\<lambda>i. (i, n - i)"]) auto
  3304 
  3305 lemma product_composition_lemma:
  3306   assumes c0: "c$0 = (0::'a::idom)"
  3307     and d0: "d$0 = 0"
  3308   shows "((a oo c) * (b oo d))$n =
  3309     sum (\<lambda>(k,m). a$k * b$m * (c^k * d^m) $ n) {(k,m). k + m \<le> n}"  (is "?l = ?r")
  3310 proof -
  3311   let ?S = "{(k::nat, m::nat). k + m \<le> n}"
  3312   have s: "?S \<subseteq> {0..n} \<times> {0..n}" by (auto simp add: subset_eq)
  3313   have f: "finite {(k::nat, m::nat). k + m \<le> n}"
  3314     apply (rule finite_subset[OF s])
  3315     apply auto
  3316     done
  3317   have "?r =  sum (\<lambda>i. sum (\<lambda>(k,m). a$k * (c^k)$i * b$m * (d^m) $ (n - i)) {(k,m). k + m \<le> n}) {0..n}"
  3318     apply (simp add: fps_mult_nth sum_distrib_left)
  3319     apply (subst sum.commute)
  3320     apply (rule sum.cong)
  3321     apply (auto simp add: field_simps)
  3322     done
  3323   also have "\<dots> = ?l"
  3324     apply (simp add: fps_mult_nth fps_compose_nth sum_product)
  3325     apply (rule sum.cong)
  3326     apply (rule refl)
  3327     apply (simp add: sum.cartesian_product mult.assoc)
  3328     apply (rule sum.mono_neutral_right[OF f])
  3329     apply (simp add: subset_eq)
  3330     apply presburger
  3331     apply clarsimp
  3332     apply (rule ccontr)
  3333     apply (clarsimp simp add: not_le)
  3334     apply (case_tac "x < aa")
  3335     apply simp
  3336     apply (frule_tac startsby_zero_power_prefix[rule_format, OF c0])
  3337     apply blast
  3338     apply simp
  3339     apply (frule_tac startsby_zero_power_prefix[rule_format, OF d0])
  3340     apply blast
  3341     done
  3342   finally show ?thesis by simp
  3343 qed
  3344 
  3345 lemma product_composition_lemma':
  3346   assumes c0: "c$0 = (0::'a::idom)"
  3347     and d0: "d$0 = 0"
  3348   shows "((a oo c) * (b oo d))$n =
  3349     sum (\<lambda>k. sum (\<lambda>m. a$k * b$m * (c^k * d^m) $ n) {0..n}) {0..n}"  (is "?l = ?r")
  3350   unfolding product_composition_lemma[OF c0 d0]
  3351   unfolding sum.cartesian_product
  3352   apply (rule sum.mono_neutral_left)
  3353   apply simp
  3354   apply (clarsimp simp add: subset_eq)
  3355   apply clarsimp
  3356   apply (rule ccontr)
  3357   apply (subgoal_tac "(c^aa * d^ba) $ n = 0")
  3358   apply simp
  3359   unfolding fps_mult_nth
  3360   apply (rule sum.neutral)
  3361   apply (clarsimp simp add: not_le)
  3362   apply (case_tac "x < aa")
  3363   apply (rule startsby_zero_power_prefix[OF c0, rule_format])
  3364   apply simp
  3365   apply (subgoal_tac "n - x < ba")
  3366   apply (frule_tac k = "ba" in startsby_zero_power_prefix[OF d0, rule_format])
  3367   apply simp
  3368   apply arith
  3369   done
  3370 
  3371 
  3372 lemma sum_pair_less_iff:
  3373   "sum (\<lambda>((k::nat),m). a k * b m * c (k + m)) {(k,m). k + m \<le> n} =
  3374     sum (\<lambda>s. sum (\<lambda>i. a i * b (s - i) * c s) {0..s}) {0..n}"
  3375   (is "?l = ?r")
  3376 proof -
  3377   let ?KM = "{(k,m). k + m \<le> n}"
  3378   let ?f = "\<lambda>s. UNION {(0::nat)..s} (\<lambda>i. {(i,s - i)})"
  3379   have th0: "?KM = UNION {0..n} ?f"
  3380     by auto
  3381   show "?l = ?r "
  3382     unfolding th0
  3383     apply (subst sum.UNION_disjoint)
  3384     apply auto
  3385     apply (subst sum.UNION_disjoint)
  3386     apply auto
  3387     done
  3388 qed
  3389 
  3390 lemma fps_compose_mult_distrib_lemma:
  3391   assumes c0: "c$0 = (0::'a::idom)"
  3392   shows "((a oo c) * (b oo c))$n = sum (\<lambda>s. sum (\<lambda>i. a$i * b$(s - i) * (c^s) $ n) {0..s}) {0..n}"
  3393   unfolding product_composition_lemma[OF c0 c0] power_add[symmetric]
  3394   unfolding sum_pair_less_iff[where a = "\<lambda>k. a$k" and b="\<lambda>m. b$m" and c="\<lambda>s. (c ^ s)$n" and n = n] ..
  3395 
  3396 lemma fps_compose_mult_distrib:
  3397   assumes c0: "c $ 0 = (0::'a::idom)"
  3398   shows "(a * b) oo c = (a oo c) * (b oo c)"
  3399   apply (simp add: fps_eq_iff fps_compose_mult_distrib_lemma [OF c0])
  3400   apply (simp add: fps_compose_nth fps_mult_nth sum_distrib_right)
  3401   done
  3402 
  3403 lemma fps_compose_prod_distrib:
  3404   assumes c0: "c$0 = (0::'a::idom)"
  3405   shows "prod a S oo c = prod (\<lambda>k. a k oo c) S"
  3406   apply (cases "finite S")
  3407   apply simp_all
  3408   apply (induct S rule: finite_induct)
  3409   apply simp
  3410   apply (simp add: fps_compose_mult_distrib[OF c0])
  3411   done
  3412 
  3413 lemma fps_compose_divide:
  3414   assumes [simp]: "g dvd f" "h $ 0 = 0"
  3415   shows   "fps_compose f h = fps_compose (f / g :: 'a :: field fps) h * fps_compose g h"
  3416 proof -
  3417   have "f = (f / g) * g" by simp
  3418   also have "fps_compose \<dots> h = fps_compose (f / g) h * fps_compose g h"
  3419     by (subst fps_compose_mult_distrib) simp_all
  3420   finally show ?thesis .
  3421 qed
  3422 
  3423 lemma fps_compose_divide_distrib:
  3424   assumes "g dvd f" "h $ 0 = 0" "fps_compose g h \<noteq> 0"
  3425   shows   "fps_compose (f / g :: 'a :: field fps) h = fps_compose f h / fps_compose g h"
  3426   using fps_compose_divide[OF assms(1,2)] assms(3) by simp
  3427 
  3428 lemma fps_compose_power:
  3429   assumes c0: "c$0 = (0::'a::idom)"
  3430   shows "(a oo c)^n = a^n oo c"
  3431 proof (cases n)
  3432   case 0
  3433   then show ?thesis by simp
  3434 next
  3435   case (Suc m)
  3436   have th0: "a^n = prod (\<lambda>k. a) {0..m}" "(a oo c) ^ n = prod (\<lambda>k. a oo c) {0..m}"
  3437     by (simp_all add: prod_constant Suc)
  3438   then show ?thesis
  3439     by (simp add: fps_compose_prod_distrib[OF c0])
  3440 qed
  3441 
  3442 lemma fps_compose_uminus: "- (a::'a::ring_1 fps) oo c = - (a oo c)"
  3443   by (simp add: fps_eq_iff fps_compose_nth field_simps sum_negf[symmetric])
  3444     
  3445 lemma fps_compose_sub_distrib: "(a - b) oo (c::'a::ring_1 fps) = (a oo c) - (b oo c)"
  3446   using fps_compose_add_distrib [of a "- b" c] by (simp add: fps_compose_uminus)
  3447 
  3448 lemma X_fps_compose: "X oo a = Abs_fps (\<lambda>n. if n = 0 then (0::'a::comm_ring_1) else a$n)"
  3449   by (simp add: fps_eq_iff fps_compose_nth mult_delta_left sum.delta)
  3450 
  3451 lemma fps_inverse_compose:
  3452   assumes b0: "(b$0 :: 'a::field) = 0"
  3453     and a0: "a$0 \<noteq> 0"
  3454   shows "inverse a oo b = inverse (a oo b)"
  3455 proof -
  3456   let ?ia = "inverse a"
  3457   let ?ab = "a oo b"
  3458   let ?iab = "inverse ?ab"
  3459 
  3460   from a0 have ia0: "?ia $ 0 \<noteq> 0" by simp
  3461   from a0 have ab0: "?ab $ 0 \<noteq> 0" by (simp add: fps_compose_def)
  3462   have "(?ia oo b) *  (a oo b) = 1"
  3463     unfolding fps_compose_mult_distrib[OF b0, symmetric]
  3464     unfolding inverse_mult_eq_1[OF a0]
  3465     fps_compose_1 ..
  3466 
  3467   then have "(?ia oo b) *  (a oo b) * ?iab  = 1 * ?iab" by simp
  3468   then have "(?ia oo b) *  (?iab * (a oo b))  = ?iab" by simp
  3469   then show ?thesis unfolding inverse_mult_eq_1[OF ab0] by simp
  3470 qed
  3471 
  3472 lemma fps_divide_compose:
  3473   assumes c0: "(c$0 :: 'a::field) = 0"
  3474     and b0: "b$0 \<noteq> 0"
  3475   shows "(a/b) oo c = (a oo c) / (b oo c)"
  3476     using b0 c0 by (simp add: fps_divide_unit fps_inverse_compose fps_compose_mult_distrib)
  3477 
  3478 lemma gp:
  3479   assumes a0: "a$0 = (0::'a::field)"
  3480   shows "(Abs_fps (\<lambda>n. 1)) oo a = 1/(1 - a)"
  3481     (is "?one oo a = _")
  3482 proof -
  3483   have o0: "?one $ 0 \<noteq> 0" by simp
  3484   have th0: "(1 - X) $ 0 \<noteq> (0::'a)" by simp
  3485   from fps_inverse_gp[where ?'a = 'a]
  3486   have "inverse ?one = 1 - X" by (simp add: fps_eq_iff)
  3487   then have "inverse (inverse ?one) = inverse (1 - X)" by simp
  3488   then have th: "?one = 1/(1 - X)" unfolding fps_inverse_idempotent[OF o0]
  3489     by (simp add: fps_divide_def)
  3490   show ?thesis
  3491     unfolding th
  3492     unfolding fps_divide_compose[OF a0 th0]
  3493     fps_compose_1 fps_compose_sub_distrib X_fps_compose_startby0[OF a0] ..
  3494 qed
  3495 
  3496 lemma fps_const_power [simp]: "fps_const (c::'a::ring_1) ^ n = fps_const (c^n)"
  3497   by (induct n) auto
  3498 
  3499 lemma fps_compose_radical:
  3500   assumes b0: "b$0 = (0::'a::field_char_0)"
  3501     and ra0: "r (Suc k) (a$0) ^ Suc k = a$0"
  3502     and a0: "a$0 \<noteq> 0"
  3503   shows "fps_radical r (Suc k)  a oo b = fps_radical r (Suc k) (a oo b)"
  3504 proof -
  3505   let ?r = "fps_radical r (Suc k)"
  3506   let ?ab = "a oo b"
  3507   have ab0: "?ab $ 0 = a$0"
  3508     by (simp add: fps_compose_def)
  3509   from ab0 a0 ra0 have rab0: "?ab $ 0 \<noteq> 0" "r (Suc k) (?ab $ 0) ^ Suc k = ?ab $ 0"
  3510     by simp_all
  3511   have th00: "r (Suc k) ((a oo b) $ 0) = (fps_radical r (Suc k) a oo b) $ 0"
  3512     by (simp add: ab0 fps_compose_def)
  3513   have th0: "(?r a oo b) ^ (Suc k) = a  oo b"
  3514     unfolding fps_compose_power[OF b0]
  3515     unfolding iffD1[OF power_radical[of a r k], OF a0 ra0]  ..
  3516   from iffD1[OF radical_unique[where r=r and k=k and b= ?ab and a = "?r a oo b", OF rab0(2) th00 rab0(1)], OF th0]
  3517   show ?thesis  .
  3518 qed
  3519 
  3520 lemma fps_const_mult_apply_left: "fps_const c * (a oo b) = (fps_const c * a) oo b"
  3521   by (simp add: fps_eq_iff fps_compose_nth sum_distrib_left mult.assoc)
  3522 
  3523 lemma fps_const_mult_apply_right:
  3524   "(a oo b) * fps_const (c::'a::comm_semiring_1) = (fps_const c * a) oo b"
  3525   by (auto simp add: fps_const_mult_apply_left mult.commute)
  3526 
  3527 lemma fps_compose_assoc:
  3528   assumes c0: "c$0 = (0::'a::idom)"
  3529     and b0: "b$0 = 0"
  3530   shows "a oo (b oo c) = a oo b oo c" (is "?l = ?r")
  3531 proof -
  3532   have "?l$n = ?r$n" for n
  3533   proof -
  3534     have "?l$n = (sum (\<lambda>i. (fps_const (a$i) * b^i) oo c) {0..n})$n"
  3535       by (simp add: fps_compose_nth fps_compose_power[OF c0] fps_const_mult_apply_left
  3536         sum_distrib_left mult.assoc fps_sum_nth)
  3537     also have "\<dots> = ((sum (\<lambda>i. fps_const (a$i) * b^i) {0..n}) oo c)$n"
  3538       by (simp add: fps_compose_sum_distrib)
  3539     also have "\<dots> = ?r$n"
  3540       apply (simp add: fps_compose_nth fps_sum_nth sum_distrib_right mult.assoc)
  3541       apply (rule sum.cong)
  3542       apply (rule refl)
  3543       apply (rule sum.mono_neutral_right)
  3544       apply (auto simp add: not_le)
  3545       apply (erule startsby_zero_power_prefix[OF b0, rule_format])
  3546       done
  3547     finally show ?thesis .
  3548   qed
  3549   then show ?thesis
  3550     by (simp add: fps_eq_iff)
  3551 qed
  3552 
  3553 
  3554 lemma fps_X_power_compose:
  3555   assumes a0: "a$0=0"
  3556   shows "X^k oo a = (a::'a::idom fps)^k"
  3557   (is "?l = ?r")
  3558 proof (cases k)
  3559   case 0
  3560   then show ?thesis by simp
  3561 next
  3562   case (Suc h)
  3563   have "?l $ n = ?r $n" for n
  3564   proof -
  3565     consider "k > n" | "k \<le> n" by arith
  3566     then show ?thesis
  3567     proof cases
  3568       case 1
  3569       then show ?thesis
  3570         using a0 startsby_zero_power_prefix[OF a0] Suc
  3571         by (simp add: fps_compose_nth del: power_Suc)
  3572     next
  3573       case 2
  3574       then show ?thesis
  3575         by (simp add: fps_compose_nth mult_delta_left sum.delta)
  3576     qed
  3577   qed
  3578   then show ?thesis
  3579     unfolding fps_eq_iff by blast
  3580 qed
  3581 
  3582 lemma fps_inv_right:
  3583   assumes a0: "a$0 = 0"
  3584     and a1: "a$1 \<noteq> 0"
  3585   shows "a oo fps_inv a = X"
  3586 proof -
  3587   let ?ia = "fps_inv a"
  3588   let ?iaa = "a oo fps_inv a"
  3589   have th0: "?ia $ 0 = 0"
  3590     by (simp add: fps_inv_def)
  3591   have th1: "?iaa $ 0 = 0"
  3592     using a0 a1 by (simp add: fps_inv_def fps_compose_nth)
  3593   have th2: "X$0 = 0"
  3594     by simp
  3595   from fps_inv[OF a0 a1] have "a oo (fps_inv a oo a) = a oo X"
  3596     by simp
  3597   then have "(a oo fps_inv a) oo a = X oo a"
  3598     by (simp add: fps_compose_assoc[OF a0 th0] X_fps_compose_startby0[OF a0])
  3599   with fps_compose_inj_right[OF a0 a1] show ?thesis
  3600     by simp
  3601 qed
  3602 
  3603 lemma fps_inv_deriv:
  3604   assumes a0: "a$0 = (0::'a::field)"
  3605     and a1: "a$1 \<noteq> 0"
  3606   shows "fps_deriv (fps_inv a) = inverse (fps_deriv a oo fps_inv a)"
  3607 proof -
  3608   let ?ia = "fps_inv a"
  3609   let ?d = "fps_deriv a oo ?ia"
  3610   let ?dia = "fps_deriv ?ia"
  3611   have ia0: "?ia$0 = 0"
  3612     by (simp add: fps_inv_def)
  3613   have th0: "?d$0 \<noteq> 0"
  3614     using a1 by (simp add: fps_compose_nth)
  3615   from fps_inv_right[OF a0 a1] have "?d * ?dia = 1"
  3616     by (simp add: fps_compose_deriv[OF ia0, of a, symmetric] )
  3617   then have "inverse ?d * ?d * ?dia = inverse ?d * 1"
  3618     by simp
  3619   with inverse_mult_eq_1 [OF th0] show "?dia = inverse ?d"
  3620     by simp
  3621 qed
  3622 
  3623 lemma fps_inv_idempotent:
  3624   assumes a0: "a$0 = 0"
  3625     and a1: "a$1 \<noteq> 0"
  3626   shows "fps_inv (fps_inv a) = a"
  3627 proof -
  3628   let ?r = "fps_inv"
  3629   have ra0: "?r a $ 0 = 0"
  3630     by (simp add: fps_inv_def)
  3631   from a1 have ra1: "?r a $ 1 \<noteq> 0"
  3632     by (simp add: fps_inv_def field_simps)
  3633   have X0: "X$0 = 0"
  3634     by simp
  3635   from fps_inv[OF ra0 ra1] have "?r (?r a) oo ?r a = X" .
  3636   then have "?r (?r a) oo ?r a oo a = X oo a"
  3637     by simp
  3638   then have "?r (?r a) oo (?r a oo a) = a"
  3639     unfolding X_fps_compose_startby0[OF a0]
  3640     unfolding fps_compose_assoc[OF a0 ra0, symmetric] .
  3641   then show ?thesis
  3642     unfolding fps_inv[OF a0 a1] by simp
  3643 qed
  3644 
  3645 lemma fps_ginv_ginv:
  3646   assumes a0: "a$0 = 0"
  3647     and a1: "a$1 \<noteq> 0"
  3648     and c0: "c$0 = 0"
  3649     and  c1: "c$1 \<noteq> 0"
  3650   shows "fps_ginv b (fps_ginv c a) = b oo a oo fps_inv c"
  3651 proof -
  3652   let ?r = "fps_ginv"
  3653   from c0 have rca0: "?r c a $0 = 0"
  3654     by (simp add: fps_ginv_def)
  3655   from a1 c1 have rca1: "?r c a $ 1 \<noteq> 0"
  3656     by (simp add: fps_ginv_def field_simps)
  3657   from fps_ginv[OF rca0 rca1]
  3658   have "?r b (?r c a) oo ?r c a = b" .
  3659   then have "?r b (?r c a) oo ?r c a oo a = b oo a"
  3660     by simp
  3661   then have "?r b (?r c a) oo (?r c a oo a) = b oo a"
  3662     apply (subst fps_compose_assoc)
  3663     using a0 c0
  3664     apply (auto simp add: fps_ginv_def)
  3665     done
  3666   then have "?r b (?r c a) oo c = b oo a"
  3667     unfolding fps_ginv[OF a0 a1] .
  3668   then have "?r b (?r c a) oo c oo fps_inv c= b oo a oo fps_inv c"
  3669     by simp
  3670   then have "?r b (?r c a) oo (c oo fps_inv c) = b oo a oo fps_inv c"
  3671     apply (subst fps_compose_assoc)
  3672     using a0 c0
  3673     apply (auto simp add: fps_inv_def)
  3674     done
  3675   then show ?thesis
  3676     unfolding fps_inv_right[OF c0 c1] by simp
  3677 qed
  3678 
  3679 lemma fps_ginv_deriv:
  3680   assumes a0:"a$0 = (0::'a::field)"
  3681     and a1: "a$1 \<noteq> 0"
  3682   shows "fps_deriv (fps_ginv b a) = (fps_deriv b / fps_deriv a) oo fps_ginv X a"
  3683 proof -
  3684   let ?ia = "fps_ginv b a"
  3685   let ?iXa = "fps_ginv X a"
  3686   let ?d = "fps_deriv"
  3687   let ?dia = "?d ?ia"
  3688   have iXa0: "?iXa $ 0 = 0"
  3689     by (simp add: fps_ginv_def)
  3690   have da0: "?d a $ 0 \<noteq> 0"
  3691     using a1 by simp
  3692   from fps_ginv[OF a0 a1, of b] have "?d (?ia oo a) = fps_deriv b"
  3693     by simp
  3694   then have "(?d ?ia oo a) * ?d a = ?d b"
  3695     unfolding fps_compose_deriv[OF a0] .
  3696   then have "(?d ?ia oo a) * ?d a * inverse (?d a) = ?d b * inverse (?d a)"
  3697     by simp
  3698   with a1 have "(?d ?ia oo a) * (inverse (?d a) * ?d a) = ?d b / ?d a"
  3699     by (simp add: fps_divide_unit)
  3700   then have "(?d ?ia oo a) oo ?iXa =  (?d b / ?d a) oo ?iXa"
  3701     unfolding inverse_mult_eq_1[OF da0] by simp
  3702   then have "?d ?ia oo (a oo ?iXa) =  (?d b / ?d a) oo ?iXa"
  3703     unfolding fps_compose_assoc[OF iXa0 a0] .
  3704   then show ?thesis unfolding fps_inv_ginv[symmetric]
  3705     unfolding fps_inv_right[OF a0 a1] by simp
  3706 qed
  3707 
  3708 lemma fps_compose_linear:
  3709   "fps_compose (f :: 'a :: comm_ring_1 fps) (fps_const c * X) = Abs_fps (\<lambda>n. c^n * f $ n)"
  3710   by (simp add: fps_eq_iff fps_compose_def power_mult_distrib
  3711                 if_distrib sum.delta' cong: if_cong)
  3712               
  3713 lemma fps_compose_uminus': 
  3714   "fps_compose f (-X :: 'a :: comm_ring_1 fps) = Abs_fps (\<lambda>n. (-1)^n * f $ n)"
  3715   using fps_compose_linear[of f "-1"] 
  3716   by (simp only: fps_const_neg [symmetric] fps_const_1_eq_1) simp
  3717 
  3718 subsection \<open>Elementary series\<close>
  3719 
  3720 subsubsection \<open>Exponential series\<close>
  3721 
  3722 definition "fps_exp x = Abs_fps (\<lambda>n. x^n / of_nat (fact n))"
  3723 
  3724 lemma fps_exp_deriv[simp]: "fps_deriv (fps_exp a) = fps_const (a::'a::field_char_0) * fps_exp a" 
  3725   (is "?l = ?r")
  3726 proof -
  3727   have "?l$n = ?r $ n" for n
  3728     apply (auto simp add: fps_exp_def field_simps power_Suc[symmetric]
  3729       simp del: fact_Suc of_nat_Suc power_Suc)
  3730     apply (simp add: field_simps)
  3731     done
  3732   then show ?thesis
  3733     by (simp add: fps_eq_iff)
  3734 qed
  3735 
  3736 lemma fps_exp_unique_ODE:
  3737   "fps_deriv a = fps_const c * a \<longleftrightarrow> a = fps_const (a$0) * fps_exp (c::'a::field_char_0)"
  3738   (is "?lhs \<longleftrightarrow> ?rhs")
  3739 proof
  3740   show ?rhs if ?lhs
  3741   proof -
  3742     from that have th: "\<And>n. a $ Suc n = c * a$n / of_nat (Suc n)"
  3743       by (simp add: fps_deriv_def fps_eq_iff field_simps del: of_nat_Suc)
  3744     have th': "a$n = a$0 * c ^ n/ (fact n)" for n
  3745     proof (induct n)
  3746       case 0
  3747       then show ?case by simp
  3748     next
  3749       case Suc
  3750       then show ?case
  3751         unfolding th
  3752         using fact_gt_zero
  3753         apply (simp add: field_simps del: of_nat_Suc fact_Suc)
  3754         apply simp
  3755         done
  3756     qed
  3757     show ?thesis
  3758       by (auto simp add: fps_eq_iff fps_const_mult_left fps_exp_def intro: th')
  3759   qed
  3760   show ?lhs if ?rhs
  3761     using that by (metis fps_exp_deriv fps_deriv_mult_const_left mult.left_commute)
  3762 qed
  3763 
  3764 lemma fps_exp_add_mult: "fps_exp (a + b) = fps_exp (a::'a::field_char_0) * fps_exp b" (is "?l = ?r")
  3765 proof -
  3766   have "fps_deriv ?r = fps_const (a + b) * ?r"
  3767     by (simp add: fps_const_add[symmetric] field_simps del: fps_const_add)
  3768   then have "?r = ?l"
  3769     by (simp only: fps_exp_unique_ODE) (simp add: fps_mult_nth fps_exp_def)
  3770   then show ?thesis ..
  3771 qed
  3772 
  3773 lemma fps_exp_nth[simp]: "fps_exp a $ n = a^n / of_nat (fact n)"
  3774   by (simp add: fps_exp_def)
  3775 
  3776 lemma fps_exp_0[simp]: "fps_exp (0::'a::field) = 1"
  3777   by (simp add: fps_eq_iff power_0_left)
  3778 
  3779 lemma fps_exp_neg: "fps_exp (- a) = inverse (fps_exp (a::'a::field_char_0))"
  3780 proof -
  3781   from fps_exp_add_mult[of a "- a"] have th0: "fps_exp a * fps_exp (- a) = 1" by simp
  3782   from fps_inverse_unique[OF th0] show ?thesis by simp
  3783 qed
  3784 
  3785 lemma fps_exp_nth_deriv[simp]: 
  3786   "fps_nth_deriv n (fps_exp (a::'a::field_char_0)) = (fps_const a)^n * (fps_exp a)"
  3787   by (induct n) auto
  3788 
  3789 lemma X_compose_fps_exp[simp]: "X oo fps_exp (a::'a::field) = fps_exp a - 1"
  3790   by (simp add: fps_eq_iff X_fps_compose)
  3791 
  3792 lemma fps_inv_fps_exp_compose:
  3793   assumes a: "a \<noteq> 0"
  3794   shows "fps_inv (fps_exp a - 1) oo (fps_exp a - 1) = X"
  3795     and "(fps_exp a - 1) oo fps_inv (fps_exp a - 1) = X"
  3796 proof -
  3797   let ?b = "fps_exp a - 1"
  3798   have b0: "?b $ 0 = 0"
  3799     by simp
  3800   have b1: "?b $ 1 \<noteq> 0"
  3801     by (simp add: a)
  3802   from fps_inv[OF b0 b1] show "fps_inv (fps_exp a - 1) oo (fps_exp a - 1) = X" .
  3803   from fps_inv_right[OF b0 b1] show "(fps_exp a - 1) oo fps_inv (fps_exp a - 1) = X" .
  3804 qed
  3805 
  3806 lemma fps_exp_power_mult: "(fps_exp (c::'a::field_char_0))^n = fps_exp (of_nat n * c)"
  3807   by (induct n) (auto simp add: field_simps fps_exp_add_mult)
  3808 
  3809 lemma radical_fps_exp:
  3810   assumes r: "r (Suc k) 1 = 1"
  3811   shows "fps_radical r (Suc k) (fps_exp (c::'a::field_char_0)) = fps_exp (c / of_nat (Suc k))"
  3812 proof -
  3813   let ?ck = "(c / of_nat (Suc k))"
  3814   let ?r = "fps_radical r (Suc k)"
  3815   have eq0[simp]: "?ck * of_nat (Suc k) = c" "of_nat (Suc k) * ?ck = c"
  3816     by (simp_all del: of_nat_Suc)
  3817   have th0: "fps_exp ?ck ^ (Suc k) = fps_exp c" unfolding fps_exp_power_mult eq0 ..
  3818   have th: "r (Suc k) (fps_exp c $0) ^ Suc k = fps_exp c $ 0"
  3819     "r (Suc k) (fps_exp c $ 0) = fps_exp ?ck $ 0" "fps_exp c $ 0 \<noteq> 0" using r by simp_all
  3820   from th0 radical_unique[where r=r and k=k, OF th] show ?thesis
  3821     by auto
  3822 qed
  3823 
  3824 lemma fps_exp_compose_linear [simp]: 
  3825   "fps_exp (d::'a::field_char_0) oo (fps_const c * X) = fps_exp (c * d)"
  3826   by (simp add: fps_compose_linear fps_exp_def fps_eq_iff power_mult_distrib)
  3827   
  3828 lemma fps_fps_exp_compose_minus [simp]: 
  3829   "fps_compose (fps_exp c) (-X) = fps_exp (-c :: 'a :: field_char_0)"
  3830   using fps_exp_compose_linear[of c "-1 :: 'a"] 
  3831   unfolding fps_const_neg [symmetric] fps_const_1_eq_1 by simp
  3832 
  3833 lemma fps_exp_eq_iff [simp]: "fps_exp c = fps_exp d \<longleftrightarrow> c = (d :: 'a :: field_char_0)"
  3834 proof
  3835   assume "fps_exp c = fps_exp d"
  3836   from arg_cong[of _ _ "\<lambda>F. F $ 1", OF this] show "c = d" by simp
  3837 qed simp_all
  3838 
  3839 lemma fps_exp_eq_fps_const_iff [simp]: 
  3840   "fps_exp (c :: 'a :: field_char_0) = fps_const c' \<longleftrightarrow> c = 0 \<and> c' = 1"
  3841 proof
  3842   assume "c = 0 \<and> c' = 1"
  3843   thus "fps_exp c = fps_const c'" by (auto simp: fps_eq_iff)
  3844 next
  3845   assume "fps_exp c = fps_const c'"
  3846   from arg_cong[of _ _ "\<lambda>F. F $ 1", OF this] arg_cong[of _ _ "\<lambda>F. F $ 0", OF this] 
  3847     show "c = 0 \<and> c' = 1" by simp_all
  3848 qed
  3849 
  3850 lemma fps_exp_neq_0 [simp]: "\<not>fps_exp (c :: 'a :: field_char_0) = 0"
  3851   unfolding fps_const_0_eq_0 [symmetric] fps_exp_eq_fps_const_iff by simp  
  3852 
  3853 lemma fps_exp_eq_1_iff [simp]: "fps_exp (c :: 'a :: field_char_0) = 1 \<longleftrightarrow> c = 0"
  3854   unfolding fps_const_1_eq_1 [symmetric] fps_exp_eq_fps_const_iff by simp
  3855     
  3856 lemma fps_exp_neq_numeral_iff [simp]: 
  3857   "fps_exp (c :: 'a :: field_char_0) = numeral n \<longleftrightarrow> c = 0 \<and> n = Num.One"
  3858   unfolding numeral_fps_const fps_exp_eq_fps_const_iff by simp
  3859 
  3860 
  3861 subsubsection \<open>Logarithmic series\<close>
  3862 
  3863 lemma Abs_fps_if_0:
  3864   "Abs_fps (\<lambda>n. if n = 0 then (v::'a::ring_1) else f n) =
  3865     fps_const v + X * Abs_fps (\<lambda>n. f (Suc n))"
  3866   by (auto simp add: fps_eq_iff)
  3867 
  3868 definition fps_ln :: "'a::field_char_0 \<Rightarrow> 'a fps"
  3869   where "fps_ln c = fps_const (1/c) * Abs_fps (\<lambda>n. if n = 0 then 0 else (- 1) ^ (n - 1) / of_nat n)"
  3870 
  3871 lemma fps_ln_deriv: "fps_deriv (fps_ln c) = fps_const (1/c) * inverse (1 + X)"
  3872   unfolding fps_inverse_X_plus1
  3873   by (simp add: fps_ln_def fps_eq_iff del: of_nat_Suc)
  3874 
  3875 lemma fps_ln_nth: "fps_ln c $ n = (if n = 0 then 0 else 1/c * ((- 1) ^ (n - 1) / of_nat n))"
  3876   by (simp add: fps_ln_def field_simps)
  3877 
  3878 lemma fps_ln_0 [simp]: "fps_ln c $ 0 = 0" by (simp add: fps_ln_def)
  3879 
  3880 lemma fps_ln_fps_exp_inv:
  3881   fixes a :: "'a::field_char_0"
  3882   assumes a: "a \<noteq> 0"
  3883   shows "fps_ln a = fps_inv (fps_exp a - 1)"  (is "?l = ?r")
  3884 proof -
  3885   let ?b = "fps_exp a - 1"
  3886   have b0: "?b $ 0 = 0" by simp
  3887   have b1: "?b $ 1 \<noteq> 0" by (simp add: a)
  3888   have "fps_deriv (fps_exp a - 1) oo fps_inv (fps_exp a - 1) =
  3889     (fps_const a * (fps_exp a - 1) + fps_const a) oo fps_inv (fps_exp a - 1)"
  3890     by (simp add: field_simps)
  3891   also have "\<dots> = fps_const a * (X + 1)"
  3892     apply (simp add: fps_compose_add_distrib fps_const_mult_apply_left[symmetric] fps_inv_right[OF b0 b1])
  3893     apply (simp add: field_simps)
  3894     done
  3895   finally have eq: "fps_deriv (fps_exp a - 1) oo fps_inv (fps_exp a - 1) = fps_const a * (X + 1)" .
  3896   from fps_inv_deriv[OF b0 b1, unfolded eq]
  3897   have "fps_deriv (fps_inv ?b) = fps_const (inverse a) / (X + 1)"
  3898     using a by (simp add: fps_const_inverse eq fps_divide_def fps_inverse_mult)
  3899   then have "fps_deriv ?l = fps_deriv ?r"
  3900     by (simp add: fps_ln_deriv add.commute fps_divide_def divide_inverse)
  3901   then show ?thesis unfolding fps_deriv_eq_iff
  3902     by (simp add: fps_ln_nth fps_inv_def)
  3903 qed
  3904 
  3905 lemma fps_ln_mult_add:
  3906   assumes c0: "c\<noteq>0"
  3907     and d0: "d\<noteq>0"
  3908   shows "fps_ln c + fps_ln d = fps_const (c+d) * fps_ln (c*d)"
  3909   (is "?r = ?l")
  3910 proof-
  3911   from c0 d0 have eq: "1/c + 1/d = (c+d)/(c*d)" by (simp add: field_simps)
  3912   have "fps_deriv ?r = fps_const (1/c + 1/d) * inverse (1 + X)"
  3913     by (simp add: fps_ln_deriv fps_const_add[symmetric] algebra_simps del: fps_const_add)
  3914   also have "\<dots> = fps_deriv ?l"
  3915     apply (simp add: fps_ln_deriv)
  3916     apply (simp add: fps_eq_iff eq)
  3917     done
  3918   finally show ?thesis
  3919     unfolding fps_deriv_eq_iff by simp
  3920 qed
  3921 
  3922 lemma X_dvd_fps_ln [simp]: "X dvd fps_ln c"
  3923 proof -
  3924   have "fps_ln c = X * Abs_fps (\<lambda>n. (-1) ^ n / (of_nat (Suc n) * c))"
  3925     by (intro fps_ext) (auto simp: fps_ln_def of_nat_diff)
  3926   thus ?thesis by simp
  3927 qed
  3928 
  3929 
  3930 subsubsection \<open>Binomial series\<close>
  3931 
  3932 definition "fps_binomial a = Abs_fps (\<lambda>n. a gchoose n)"
  3933 
  3934 lemma fps_binomial_nth[simp]: "fps_binomial a $ n = a gchoose n"
  3935   by (simp add: fps_binomial_def)
  3936 
  3937 lemma fps_binomial_ODE_unique:
  3938   fixes c :: "'a::field_char_0"
  3939   shows "fps_deriv a = (fps_const c * a) / (1 + X) \<longleftrightarrow> a = fps_const (a$0) * fps_binomial c"
  3940   (is "?lhs \<longleftrightarrow> ?rhs")
  3941 proof
  3942   let ?da = "fps_deriv a"
  3943   let ?x1 = "(1 + X):: 'a fps"
  3944   let ?l = "?x1 * ?da"
  3945   let ?r = "fps_const c * a"
  3946 
  3947   have eq: "?l = ?r \<longleftrightarrow> ?lhs"
  3948   proof -
  3949     have x10: "?x1 $ 0 \<noteq> 0" by simp
  3950     have "?l = ?r \<longleftrightarrow> inverse ?x1 * ?l = inverse ?x1 * ?r" by simp
  3951     also have "\<dots> \<longleftrightarrow> ?da = (fps_const c * a) / ?x1"
  3952       apply (simp only: fps_divide_def  mult.assoc[symmetric] inverse_mult_eq_1[OF x10])
  3953       apply (simp add: field_simps)
  3954       done
  3955     finally show ?thesis .
  3956   qed
  3957 
  3958   show ?rhs if ?lhs
  3959   proof -
  3960     from eq that have h: "?l = ?r" ..
  3961     have th0: "a$ Suc n = ((c - of_nat n) / of_nat (Suc n)) * a $n" for n
  3962     proof -
  3963       from h have "?l $ n = ?r $ n" by simp
  3964       then show ?thesis
  3965         apply (simp add: field_simps del: of_nat_Suc)
  3966         apply (cases n)
  3967         apply (simp_all add: field_simps del: of_nat_Suc)
  3968         done
  3969     qed
  3970     have th1: "a $ n = (c gchoose n) * a $ 0" for n
  3971     proof (induct n)
  3972       case 0
  3973       then show ?case by simp
  3974     next
  3975       case (Suc m)
  3976       then show ?case
  3977         unfolding th0
  3978         apply (simp add: field_simps del: of_nat_Suc)
  3979         unfolding mult.assoc[symmetric] gbinomial_mult_1
  3980         apply (simp add: field_simps)
  3981         done
  3982     qed
  3983     show ?thesis
  3984       apply (simp add: fps_eq_iff)
  3985       apply (subst th1)
  3986       apply (simp add: field_simps)
  3987       done
  3988   qed
  3989 
  3990   show ?lhs if ?rhs
  3991   proof -
  3992     have th00: "x * (a $ 0 * y) = a $ 0 * (x * y)" for x y
  3993       by (simp add: mult.commute)
  3994     have "?l = ?r"
  3995       apply (subst \<open>?rhs\<close>)
  3996       apply (subst (2) \<open>?rhs\<close>)
  3997       apply (clarsimp simp add: fps_eq_iff field_simps)
  3998       unfolding mult.assoc[symmetric] th00 gbinomial_mult_1
  3999       apply (simp add: field_simps gbinomial_mult_1)
  4000       done
  4001     with eq show ?thesis ..
  4002   qed
  4003 qed
  4004 
  4005 lemma fps_binomial_ODE_unique':
  4006   "(fps_deriv a = fps_const c * a / (1 + X) \<and> a $ 0 = 1) \<longleftrightarrow> (a = fps_binomial c)"
  4007   by (subst fps_binomial_ODE_unique) auto
  4008 
  4009 lemma fps_binomial_deriv: "fps_deriv (fps_binomial c) = fps_const c * fps_binomial c / (1 + X)"
  4010 proof -
  4011   let ?a = "fps_binomial c"
  4012   have th0: "?a = fps_const (?a$0) * ?a" by (simp)
  4013   from iffD2[OF fps_binomial_ODE_unique, OF th0] show ?thesis .
  4014 qed
  4015 
  4016 lemma fps_binomial_add_mult: "fps_binomial (c+d) = fps_binomial c * fps_binomial d" (is "?l = ?r")
  4017 proof -
  4018   let ?P = "?r - ?l"
  4019   let ?b = "fps_binomial"
  4020   let ?db = "\<lambda>x. fps_deriv (?b x)"
  4021   have "fps_deriv ?P = ?db c * ?b d + ?b c * ?db d - ?db (c + d)"  by simp
  4022   also have "\<dots> = inverse (1 + X) *
  4023       (fps_const c * ?b c * ?b d + fps_const d * ?b c * ?b d - fps_const (c+d) * ?b (c + d))"
  4024     unfolding fps_binomial_deriv
  4025     by (simp add: fps_divide_def field_simps)
  4026   also have "\<dots> = (fps_const (c + d)/ (1 + X)) * ?P"
  4027     by (simp add: field_simps fps_divide_unit fps_const_add[symmetric] del: fps_const_add)
  4028   finally have th0: "fps_deriv ?P = fps_const (c+d) * ?P / (1 + X)"
  4029     by (simp add: fps_divide_def)
  4030   have "?P = fps_const (?P$0) * ?b (c + d)"
  4031     unfolding fps_binomial_ODE_unique[symmetric]
  4032     using th0 by simp
  4033   then have "?P = 0" by (simp add: fps_mult_nth)
  4034   then show ?thesis by simp
  4035 qed
  4036 
  4037 lemma fps_binomial_minus_one: "fps_binomial (- 1) = inverse (1 + X)"
  4038   (is "?l = inverse ?r")
  4039 proof-
  4040   have th: "?r$0 \<noteq> 0" by simp
  4041   have th': "fps_deriv (inverse ?r) = fps_const (- 1) * inverse ?r / (1 + X)"
  4042     by (simp add: fps_inverse_deriv[OF th] fps_divide_def
  4043       power2_eq_square mult.commute fps_const_neg[symmetric] del: fps_const_neg)
  4044   have eq: "inverse ?r $ 0 = 1"
  4045     by (simp add: fps_inverse_def)
  4046   from iffD1[OF fps_binomial_ODE_unique[of "inverse (1 + X)" "- 1"] th'] eq
  4047   show ?thesis by (simp add: fps_inverse_def)
  4048 qed
  4049 
  4050 lemma fps_binomial_of_nat: "fps_binomial (of_nat n) = (1 + X :: 'a :: field_char_0 fps) ^ n"
  4051 proof (cases "n = 0")
  4052   case [simp]: True
  4053   have "fps_deriv ((1 + X) ^ n :: 'a fps) = 0" by simp
  4054   also have "\<dots> = fps_const (of_nat n) * (1 + X) ^ n / (1 + X)" by (simp add: fps_binomial_def)
  4055   finally show ?thesis by (subst sym, subst fps_binomial_ODE_unique' [symmetric]) simp_all
  4056 next
  4057   case False
  4058   have "fps_deriv ((1 + X) ^ n :: 'a fps) = fps_const (of_nat n) * (1 + X) ^ (n - 1)"
  4059     by (simp add: fps_deriv_power)
  4060   also have "(1 + X :: 'a fps) $ 0 \<noteq> 0" by simp
  4061   hence "(1 + X :: 'a fps) \<noteq> 0" by (intro notI) (simp only: , simp)
  4062   with False have "(1 + X :: 'a fps) ^ (n - 1) = (1 + X) ^ n / (1 + X)"
  4063     by (cases n) (simp_all )
  4064   also have "fps_const (of_nat n :: 'a) * ((1 + X) ^ n / (1 + X)) =
  4065                fps_const (of_nat n) * (1 + X) ^ n / (1 + X)"
  4066     by (simp add: unit_div_mult_swap)
  4067   finally show ?thesis
  4068     by (subst sym, subst fps_binomial_ODE_unique' [symmetric]) (simp_all add: fps_power_nth)
  4069 qed
  4070 
  4071 lemma fps_binomial_0 [simp]: "fps_binomial 0 = 1"
  4072   using fps_binomial_of_nat[of 0] by simp
  4073   
  4074 lemma fps_binomial_power: "fps_binomial a ^ n = fps_binomial (of_nat n * a)"
  4075   by (induction n) (simp_all add: fps_binomial_add_mult ring_distribs)
  4076 
  4077 lemma fps_binomial_1: "fps_binomial 1 = 1 + X"
  4078   using fps_binomial_of_nat[of 1] by simp
  4079 
  4080 lemma fps_binomial_minus_of_nat:
  4081   "fps_binomial (- of_nat n) = inverse ((1 + X :: 'a :: field_char_0 fps) ^ n)"
  4082   by (rule sym, rule fps_inverse_unique)
  4083      (simp add: fps_binomial_of_nat [symmetric] fps_binomial_add_mult [symmetric])
  4084 
  4085 lemma one_minus_const_X_power:
  4086   "c \<noteq> 0 \<Longrightarrow> (1 - fps_const c * X) ^ n =
  4087      fps_compose (fps_binomial (of_nat n)) (-fps_const c * X)"
  4088   by (subst fps_binomial_of_nat)
  4089      (simp add: fps_compose_power [symmetric] fps_compose_add_distrib fps_const_neg [symmetric] 
  4090            del: fps_const_neg)
  4091 
  4092 lemma one_minus_X_const_neg_power:
  4093   "inverse ((1 - fps_const c * X) ^ n) = 
  4094        fps_compose (fps_binomial (-of_nat n)) (-fps_const c * X)"
  4095 proof (cases "c = 0")
  4096   case False
  4097   thus ?thesis
  4098   by (subst fps_binomial_minus_of_nat)
  4099      (simp add: fps_compose_power [symmetric] fps_inverse_compose fps_compose_add_distrib
  4100                 fps_const_neg [symmetric] del: fps_const_neg)
  4101 qed simp
  4102 
  4103 lemma X_plus_const_power:
  4104   "c \<noteq> 0 \<Longrightarrow> (X + fps_const c) ^ n =
  4105      fps_const (c^n) * fps_compose (fps_binomial (of_nat n)) (fps_const (inverse c) * X)"
  4106   by (subst fps_binomial_of_nat)
  4107      (simp add: fps_compose_power [symmetric] fps_binomial_of_nat fps_compose_add_distrib
  4108                 fps_const_power [symmetric] power_mult_distrib [symmetric] 
  4109                 algebra_simps inverse_mult_eq_1' del: fps_const_power)
  4110 
  4111 lemma X_plus_const_neg_power:
  4112   "c \<noteq> 0 \<Longrightarrow> inverse ((X + fps_const c) ^ n) =
  4113      fps_const (inverse c^n) * fps_compose (fps_binomial (-of_nat n)) (fps_const (inverse c) * X)"
  4114   by (subst fps_binomial_minus_of_nat)
  4115      (simp add: fps_compose_power [symmetric] fps_binomial_of_nat fps_compose_add_distrib
  4116                 fps_const_power [symmetric] power_mult_distrib [symmetric] fps_inverse_compose 
  4117                 algebra_simps fps_const_inverse [symmetric] fps_inverse_mult [symmetric]
  4118                 fps_inverse_power [symmetric] inverse_mult_eq_1'
  4119            del: fps_const_power)
  4120 
  4121 
  4122 lemma one_minus_const_X_neg_power':
  4123   "n > 0 \<Longrightarrow> inverse ((1 - fps_const (c :: 'a :: field_char_0) * X) ^ n) =
  4124        Abs_fps (\<lambda>k. of_nat ((n + k - 1) choose k) * c^k)"
  4125   apply (rule fps_ext)
  4126   apply (subst one_minus_X_const_neg_power, subst fps_const_neg, subst fps_compose_linear)
  4127   apply (simp add: power_mult_distrib [symmetric] mult.assoc [symmetric] 
  4128                    gbinomial_minus binomial_gbinomial of_nat_diff)
  4129   done
  4130 
  4131 text \<open>Vandermonde's Identity as a consequence.\<close>
  4132 lemma gbinomial_Vandermonde:
  4133   "sum (\<lambda>k. (a gchoose k) * (b gchoose (n - k))) {0..n} = (a + b) gchoose n"
  4134 proof -
  4135   let ?ba = "fps_binomial a"
  4136   let ?bb = "fps_binomial b"
  4137   let ?bab = "fps_binomial (a + b)"
  4138   from fps_binomial_add_mult[of a b] have "?bab $ n = (?ba * ?bb)$n" by simp
  4139   then show ?thesis by (simp add: fps_mult_nth)
  4140 qed
  4141 
  4142 lemma binomial_Vandermonde:
  4143   "sum (\<lambda>k. (a choose k) * (b choose (n - k))) {0..n} = (a + b) choose n"
  4144   using gbinomial_Vandermonde[of "(of_nat a)" "of_nat b" n]
  4145   by (simp only: binomial_gbinomial[symmetric] of_nat_mult[symmetric]
  4146                  of_nat_sum[symmetric] of_nat_add[symmetric] of_nat_eq_iff)
  4147 
  4148 lemma binomial_Vandermonde_same: "sum (\<lambda>k. (n choose k)\<^sup>2) {0..n} = (2 * n) choose n"
  4149   using binomial_Vandermonde[of n n n, symmetric]
  4150   unfolding mult_2
  4151   apply (simp add: power2_eq_square)
  4152   apply (rule sum.cong)
  4153   apply (auto intro:  binomial_symmetric)
  4154   done
  4155 
  4156 lemma Vandermonde_pochhammer_lemma:
  4157   fixes a :: "'a::field_char_0"
  4158   assumes b: "\<forall>j\<in>{0 ..<n}. b \<noteq> of_nat j"
  4159   shows "sum (\<lambda>k. (pochhammer (- a) k * pochhammer (- (of_nat n)) k) /
  4160       (of_nat (fact k) * pochhammer (b - of_nat n + 1) k)) {0..n} =
  4161     pochhammer (- (a + b)) n / pochhammer (- b) n"
  4162   (is "?l = ?r")
  4163 proof -
  4164   let ?m1 = "\<lambda>m. (- 1 :: 'a) ^ m"
  4165   let ?f = "\<lambda>m. of_nat (fact m)"
  4166   let ?p = "\<lambda>(x::'a). pochhammer (- x)"
  4167   from b have bn0: "?p b n \<noteq> 0"
  4168     unfolding pochhammer_eq_0_iff by simp
  4169   have th00:
  4170     "b gchoose (n - k) =
  4171         (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)"
  4172       (is ?gchoose)
  4173     "pochhammer (1 + b - of_nat n) k \<noteq> 0"
  4174       (is ?pochhammer)
  4175     if kn: "k \<in> {0..n}" for k
  4176   proof -
  4177     from kn have "k \<le> n" by simp
  4178     have nz: "pochhammer (1 + b - of_nat n) n \<noteq> 0"
  4179     proof
  4180       assume "pochhammer (1 + b - of_nat n) n = 0"
  4181       then have c: "pochhammer (b - of_nat n + 1) n = 0"
  4182         by (simp add: algebra_simps)
  4183       then obtain j where j: "j < n" "b - of_nat n + 1 = - of_nat j"
  4184         unfolding pochhammer_eq_0_iff by blast
  4185       from j have "b = of_nat n - of_nat j - of_nat 1"
  4186         by (simp add: algebra_simps)
  4187       then have "b = of_nat (n - j - 1)"
  4188         using j kn by (simp add: of_nat_diff)
  4189       with b show False using j by auto
  4190     qed
  4191 
  4192     from nz kn [simplified] have nz': "pochhammer (1 + b - of_nat n) k \<noteq> 0"
  4193       by (rule pochhammer_neq_0_mono)
  4194 
  4195     consider "k = 0 \<or> n = 0" | "k \<noteq> 0" "n \<noteq> 0"
  4196       by blast
  4197     then have "b gchoose (n - k) =
  4198       (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)"
  4199     proof cases
  4200       case 1
  4201       then show ?thesis
  4202         using kn by (cases "k = 0") (simp_all add: gbinomial_pochhammer)
  4203     next
  4204       case neq: 2
  4205       then obtain m where m: "n = Suc m"
  4206         by (cases n) auto
  4207       from neq(1) obtain h where h: "k = Suc h"
  4208         by (cases k) auto
  4209       show ?thesis
  4210       proof (cases "k = n")
  4211         case True
  4212         then show ?thesis
  4213           using pochhammer_minus'[where k=k and b=b]
  4214           apply (simp add: pochhammer_same)
  4215           using bn0
  4216           apply (simp add: field_simps power_add[symmetric])
  4217           done
  4218       next
  4219         case False
  4220         with kn have kn': "k < n"
  4221           by simp
  4222         have m1nk: "?m1 n = prod (\<lambda>i. - 1) {..m}" "?m1 k = prod (\<lambda>i. - 1) {0..h}"
  4223           by (simp_all add: prod_constant m h)
  4224         have bnz0: "pochhammer (b - of_nat n + 1) k \<noteq> 0"
  4225           using bn0 kn
  4226           unfolding pochhammer_eq_0_iff
  4227           apply auto
  4228           apply (erule_tac x= "n - ka - 1" in allE)
  4229           apply (auto simp add: algebra_simps of_nat_diff)
  4230           done
  4231         have eq1: "prod (\<lambda>k. (1::'a) + of_nat m - of_nat k) {..h} =
  4232           prod of_nat {Suc (m - h) .. Suc m}"
  4233           using kn' h m
  4234           by (intro prod.reindex_bij_witness[where i="\<lambda>k. Suc m - k" and j="\<lambda>k. Suc m - k"])
  4235              (auto simp: of_nat_diff)
  4236         have th1: "(?m1 k * ?p (of_nat n) k) / ?f n = 1 / of_nat(fact (n - k))"
  4237           apply (simp add: pochhammer_minus field_simps)
  4238           using \<open>k \<le> n\<close> apply (simp add: fact_split [of k n])
  4239           apply (simp add: pochhammer_prod)
  4240           using prod.atLeast_lessThan_shift_bounds [where ?'a = 'a, of "\<lambda>i. 1 + of_nat i" 0 "n - k" k]
  4241           apply (auto simp add: of_nat_diff field_simps)
  4242           done
  4243         have th20: "?m1 n * ?p b n = prod (\<lambda>i. b - of_nat i) {0..m}"
  4244           apply (simp add: pochhammer_minus field_simps m)
  4245           apply (auto simp add: pochhammer_prod_rev of_nat_diff prod.atLeast_Suc_atMost_Suc_shift)
  4246           done
  4247         have th21:"pochhammer (b - of_nat n + 1) k = prod (\<lambda>i. b - of_nat i) {n - k .. n - 1}"
  4248           using kn apply (simp add: pochhammer_prod_rev m h prod.atLeast_Suc_atMost_Suc_shift)
  4249           using prod.atLeast_atMost_shift_0 [of "m - h" m, where ?'a = 'a]
  4250           apply (auto simp add: of_nat_diff field_simps)
  4251           done
  4252         have "?m1 n * ?p b n =
  4253           prod (\<lambda>i. b - of_nat i) {0.. n - k - 1} * pochhammer (b - of_nat n + 1) k"
  4254           using kn' m h unfolding th20 th21 apply simp
  4255           apply (subst prod.union_disjoint [symmetric])
  4256           apply auto
  4257           apply (rule prod.cong)
  4258           apply auto
  4259           done
  4260         then have th2: "(?m1 n * ?p b n)/pochhammer (b - of_nat n + 1) k =
  4261           prod (\<lambda>i. b - of_nat i) {0.. n - k - 1}"
  4262           using nz' by (simp add: field_simps)
  4263         have "(?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k) =
  4264           ((?m1 k * ?p (of_nat n) k) / ?f n) * ((?m1 n * ?p b n)/pochhammer (b - of_nat n + 1) k)"
  4265           using bnz0
  4266           by (simp add: field_simps)
  4267         also have "\<dots> = b gchoose (n - k)"
  4268           unfolding th1 th2
  4269           using kn' m h
  4270           apply (simp add: field_simps gbinomial_mult_fact)
  4271           apply (rule prod.cong)
  4272           apply auto
  4273           done
  4274         finally show ?thesis by simp
  4275       qed
  4276     qed
  4277     then show ?gchoose and ?pochhammer
  4278       apply (cases "n = 0")
  4279       using nz'
  4280       apply auto
  4281       done
  4282   qed
  4283   have "?r = ((a + b) gchoose n) * (of_nat (fact n) / (?m1 n * pochhammer (- b) n))"
  4284     unfolding gbinomial_pochhammer
  4285     using bn0 by (auto simp add: field_simps)
  4286   also have "\<dots> = ?l"
  4287     unfolding gbinomial_Vandermonde[symmetric]
  4288     apply (simp add: th00)
  4289     unfolding gbinomial_pochhammer
  4290     using bn0
  4291     apply (simp add: sum_distrib_right sum_distrib_left field_simps)
  4292     done
  4293   finally show ?thesis by simp
  4294 qed
  4295 
  4296 lemma Vandermonde_pochhammer:
  4297   fixes a :: "'a::field_char_0"
  4298   assumes c: "\<forall>i \<in> {0..< n}. c \<noteq> - of_nat i"
  4299   shows "sum (\<lambda>k. (pochhammer a k * pochhammer (- (of_nat n)) k) /
  4300     (of_nat (fact k) * pochhammer c k)) {0..n} = pochhammer (c - a) n / pochhammer c n"
  4301 proof -
  4302   let ?a = "- a"
  4303   let ?b = "c + of_nat n - 1"
  4304   have h: "\<forall> j \<in>{0..< n}. ?b \<noteq> of_nat j"
  4305     using c
  4306     apply (auto simp add: algebra_simps of_nat_diff)
  4307     apply (erule_tac x = "n - j - 1" in ballE)
  4308     apply (auto simp add: of_nat_diff algebra_simps)
  4309     done
  4310   have th0: "pochhammer (- (?a + ?b)) n = (- 1)^n * pochhammer (c - a) n"
  4311     unfolding pochhammer_minus
  4312     by (simp add: algebra_simps)
  4313   have th1: "pochhammer (- ?b) n = (- 1)^n * pochhammer c n"
  4314     unfolding pochhammer_minus
  4315     by simp
  4316   have nz: "pochhammer c n \<noteq> 0" using c
  4317     by (simp add: pochhammer_eq_0_iff)
  4318   from Vandermonde_pochhammer_lemma[where a = "?a" and b="?b" and n=n, OF h, unfolded th0 th1]
  4319   show ?thesis
  4320     using nz by (simp add: field_simps sum_distrib_left)
  4321 qed
  4322 
  4323 
  4324 subsubsection \<open>Formal trigonometric functions\<close>
  4325 
  4326 definition "fps_sin (c::'a::field_char_0) =
  4327   Abs_fps (\<lambda>n. if even n then 0 else (- 1) ^((n - 1) div 2) * c^n /(of_nat (fact n)))"
  4328 
  4329 definition "fps_cos (c::'a::field_char_0) =
  4330   Abs_fps (\<lambda>n. if even n then (- 1) ^ (n div 2) * c^n / (of_nat (fact n)) else 0)"
  4331 
  4332 lemma fps_sin_deriv:
  4333   "fps_deriv (fps_sin c) = fps_const c * fps_cos c"
  4334   (is "?lhs = ?rhs")
  4335 proof (rule fps_ext)
  4336   fix n :: nat
  4337   show "?lhs $ n = ?rhs $ n"
  4338   proof (cases "even n")
  4339     case True
  4340     have "?lhs$n = of_nat (n+1) * (fps_sin c $ (n+1))" by simp
  4341     also have "\<dots> = of_nat (n+1) * ((- 1)^(n div 2) * c^Suc n / of_nat (fact (Suc n)))"
  4342       using True by (simp add: fps_sin_def)
  4343     also have "\<dots> = (- 1)^(n div 2) * c^Suc n * (of_nat (n+1) / (of_nat (Suc n) * of_nat (fact n)))"
  4344       unfolding fact_Suc of_nat_mult
  4345       by (simp add: field_simps del: of_nat_add of_nat_Suc)
  4346     also have "\<dots> = (- 1)^(n div 2) *c^Suc n / of_nat (fact n)"
  4347       by (simp add: field_simps del: of_nat_add of_nat_Suc)
  4348     finally show ?thesis
  4349       using True by (simp add: fps_cos_def field_simps)
  4350   next
  4351     case False
  4352     then show ?thesis
  4353       by (simp_all add: fps_deriv_def fps_sin_def fps_cos_def)
  4354   qed
  4355 qed
  4356 
  4357 lemma fps_cos_deriv: "fps_deriv (fps_cos c) = fps_const (- c)* (fps_sin c)"
  4358   (is "?lhs = ?rhs")
  4359 proof (rule fps_ext)
  4360   have th0: "- ((- 1::'a) ^ n) = (- 1)^Suc n" for n
  4361     by simp
  4362   show "?lhs $ n = ?rhs $ n" for n
  4363   proof (cases "even n")
  4364     case False
  4365     then have n0: "n \<noteq> 0" by presburger
  4366     from False have th1: "Suc ((n - 1) div 2) = Suc n div 2"
  4367       by (cases n) simp_all
  4368     have "?lhs$n = of_nat (n+1) * (fps_cos c $ (n+1))" by simp
  4369     also have "\<dots> = of_nat (n+1) * ((- 1)^((n + 1) div 2) * c^Suc n / of_nat (fact (Suc n)))"
  4370       using False by (simp add: fps_cos_def)
  4371     also have "\<dots> = (- 1)^((n + 1) div 2)*c^Suc n * (of_nat (n+1) / (of_nat (Suc n) * of_nat (fact n)))"
  4372       unfolding fact_Suc of_nat_mult
  4373       by (simp add: field_simps del: of_nat_add of_nat_Suc)
  4374     also have "\<dots> = (- 1)^((n + 1) div 2) * c^Suc n / of_nat (fact n)"
  4375       by (simp add: field_simps del: of_nat_add of_nat_Suc)
  4376     also have "\<dots> = (- ((- 1)^((n - 1) div 2))) * c^Suc n / of_nat (fact n)"
  4377       unfolding th0 unfolding th1 by simp
  4378     finally show ?thesis
  4379       using False by (simp add: fps_sin_def field_simps)
  4380   next
  4381     case True
  4382     then show ?thesis
  4383       by (simp_all add: fps_deriv_def fps_sin_def fps_cos_def)
  4384   qed
  4385 qed
  4386 
  4387 lemma fps_sin_cos_sum_of_squares: "(fps_cos c)\<^sup>2 + (fps_sin c)\<^sup>2 = 1"
  4388   (is "?lhs = _")
  4389 proof -
  4390   have "fps_deriv ?lhs = 0"
  4391     apply (simp add:  fps_deriv_power fps_sin_deriv fps_cos_deriv)
  4392     apply (simp add: field_simps fps_const_neg[symmetric] del: fps_const_neg)
  4393     done
  4394   then have "?lhs = fps_const (?lhs $ 0)"
  4395     unfolding fps_deriv_eq_0_iff .
  4396   also have "\<dots> = 1"
  4397     by (auto simp add: fps_eq_iff numeral_2_eq_2 fps_mult_nth fps_cos_def fps_sin_def)
  4398   finally show ?thesis .
  4399 qed
  4400 
  4401 lemma fps_sin_nth_0 [simp]: "fps_sin c $ 0 = 0"
  4402   unfolding fps_sin_def by simp
  4403 
  4404 lemma fps_sin_nth_1 [simp]: "fps_sin c $ 1 = c"
  4405   unfolding fps_sin_def by simp
  4406 
  4407 lemma fps_sin_nth_add_2:
  4408     "fps_sin c $ (n + 2) = - (c * c * fps_sin c $ n / (of_nat (n + 1) * of_nat (n + 2)))"
  4409   unfolding fps_sin_def
  4410   apply (cases n)
  4411   apply simp
  4412   apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq del: of_nat_Suc fact_Suc)
  4413   apply simp
  4414   done
  4415 
  4416 lemma fps_cos_nth_0 [simp]: "fps_cos c $ 0 = 1"
  4417   unfolding fps_cos_def by simp
  4418 
  4419 lemma fps_cos_nth_1 [simp]: "fps_cos c $ 1 = 0"
  4420   unfolding fps_cos_def by simp
  4421 
  4422 lemma fps_cos_nth_add_2:
  4423   "fps_cos c $ (n + 2) = - (c * c * fps_cos c $ n / (of_nat (n + 1) * of_nat (n + 2)))"
  4424   unfolding fps_cos_def
  4425   apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq del: of_nat_Suc fact_Suc)
  4426   apply simp
  4427   done
  4428 
  4429 lemma nat_induct2: "P 0 \<Longrightarrow> P 1 \<Longrightarrow> (\<And>n. P n \<Longrightarrow> P (n + 2)) \<Longrightarrow> P (n::nat)"
  4430   unfolding One_nat_def numeral_2_eq_2
  4431   apply (induct n rule: nat_less_induct)
  4432   apply (case_tac n)
  4433   apply simp
  4434   apply (rename_tac m)
  4435   apply (case_tac m)
  4436   apply simp
  4437   apply (rename_tac k)
  4438   apply (case_tac k)
  4439   apply simp_all
  4440   done
  4441 
  4442 lemma nat_add_1_add_1: "(n::nat) + 1 + 1 = n + 2"
  4443   by simp
  4444 
  4445 lemma eq_fps_sin:
  4446   assumes 0: "a $ 0 = 0"
  4447     and 1: "a $ 1 = c"
  4448     and 2: "fps_deriv (fps_deriv a) = - (fps_const c * fps_const c * a)"
  4449   shows "a = fps_sin c"
  4450   apply (rule fps_ext)
  4451   apply (induct_tac n rule: nat_induct2)
  4452   apply (simp add: 0)
  4453   apply (simp add: 1 del: One_nat_def)
  4454   apply (rename_tac m, cut_tac f="\<lambda>a. a $ m" in arg_cong [OF 2])
  4455   apply (simp add: nat_add_1_add_1 fps_sin_nth_add_2
  4456               del: One_nat_def of_nat_Suc of_nat_add add_2_eq_Suc')
  4457   apply (subst minus_divide_left)
  4458   apply (subst nonzero_eq_divide_eq)
  4459   apply (simp del: of_nat_add of_nat_Suc)
  4460   apply (simp only: ac_simps)
  4461   done
  4462 
  4463 lemma eq_fps_cos:
  4464   assumes 0: "a $ 0 = 1"
  4465     and 1: "a $ 1 = 0"
  4466     and 2: "fps_deriv (fps_deriv a) = - (fps_const c * fps_const c * a)"
  4467   shows "a = fps_cos c"
  4468   apply (rule fps_ext)
  4469   apply (induct_tac n rule: nat_induct2)
  4470   apply (simp add: 0)
  4471   apply (simp add: 1 del: One_nat_def)
  4472   apply (rename_tac m, cut_tac f="\<lambda>a. a $ m" in arg_cong [OF 2])
  4473   apply (simp add: nat_add_1_add_1 fps_cos_nth_add_2
  4474               del: One_nat_def of_nat_Suc of_nat_add add_2_eq_Suc')
  4475   apply (subst minus_divide_left)
  4476   apply (subst nonzero_eq_divide_eq)
  4477   apply (simp del: of_nat_add of_nat_Suc)
  4478   apply (simp only: ac_simps)
  4479   done
  4480 
  4481 lemma mult_nth_0 [simp]: "(a * b) $ 0 = a $ 0 * b $ 0"
  4482   by (simp add: fps_mult_nth)
  4483 
  4484 lemma mult_nth_1 [simp]: "(a * b) $ 1 = a $ 0 * b $ 1 + a $ 1 * b $ 0"
  4485   by (simp add: fps_mult_nth)
  4486 
  4487 lemma fps_sin_add: "fps_sin (a + b) = fps_sin a * fps_cos b + fps_cos a * fps_sin b"
  4488   apply (rule eq_fps_sin [symmetric], simp, simp del: One_nat_def)
  4489   apply (simp del: fps_const_neg fps_const_add fps_const_mult
  4490               add: fps_const_add [symmetric] fps_const_neg [symmetric]
  4491                    fps_sin_deriv fps_cos_deriv algebra_simps)
  4492   done
  4493 
  4494 lemma fps_cos_add: "fps_cos (a + b) = fps_cos a * fps_cos b - fps_sin a * fps_sin b"
  4495   apply (rule eq_fps_cos [symmetric], simp, simp del: One_nat_def)
  4496   apply (simp del: fps_const_neg fps_const_add fps_const_mult
  4497               add: fps_const_add [symmetric] fps_const_neg [symmetric]
  4498                    fps_sin_deriv fps_cos_deriv algebra_simps)
  4499   done
  4500 
  4501 lemma fps_sin_even: "fps_sin (- c) = - fps_sin c"
  4502   by (auto simp add: fps_eq_iff fps_sin_def)
  4503 
  4504 lemma fps_cos_odd: "fps_cos (- c) = fps_cos c"
  4505   by (auto simp add: fps_eq_iff fps_cos_def)
  4506 
  4507 definition "fps_tan c = fps_sin c / fps_cos c"
  4508 
  4509 lemma fps_tan_deriv: "fps_deriv (fps_tan c) = fps_const c / (fps_cos c)\<^sup>2"
  4510 proof -
  4511   have th0: "fps_cos c $ 0 \<noteq> 0" by (simp add: fps_cos_def)
  4512   from this have "fps_cos c \<noteq> 0" by (intro notI) simp
  4513   hence "fps_deriv (fps_tan c) =
  4514            fps_const c * (fps_cos c^2 + fps_sin c^2) / (fps_cos c^2)"
  4515     by (simp add: fps_tan_def fps_divide_deriv power2_eq_square algebra_simps
  4516                   fps_sin_deriv fps_cos_deriv fps_const_neg[symmetric] div_mult_swap
  4517              del: fps_const_neg)
  4518   also note fps_sin_cos_sum_of_squares
  4519   finally show ?thesis by simp
  4520 qed
  4521 
  4522 text \<open>Connection to @{const "fps_exp"} over the complex numbers --- Euler and de Moivre.\<close>
  4523 
  4524 lemma fps_exp_ii_sin_cos: "fps_exp (\<i> * c) = fps_cos c + fps_const \<i> * fps_sin c"
  4525   (is "?l = ?r")
  4526 proof -
  4527   have "?l $ n = ?r $ n" for n
  4528   proof (cases "even n")
  4529     case True
  4530     then obtain m where m: "n = 2 * m" ..
  4531     show ?thesis
  4532       by (simp add: m fps_sin_def fps_cos_def power_mult_distrib power_mult power_minus [of "c ^ 2"])
  4533   next
  4534     case False
  4535     then obtain m where m: "n = 2 * m + 1" ..
  4536     show ?thesis
  4537       by (simp add: m fps_sin_def fps_cos_def power_mult_distrib
  4538         power_mult power_minus [of "c ^ 2"])
  4539   qed
  4540   then show ?thesis
  4541     by (simp add: fps_eq_iff)
  4542 qed
  4543 
  4544 lemma fps_exp_minus_ii_sin_cos: "fps_exp (- (\<i> * c)) = fps_cos c - fps_const \<i> * fps_sin c"
  4545   unfolding minus_mult_right fps_exp_ii_sin_cos by (simp add: fps_sin_even fps_cos_odd)
  4546 
  4547 lemma fps_const_minus: "fps_const (c::'a::group_add) - fps_const d = fps_const (c - d)"
  4548   by (fact fps_const_sub)
  4549 
  4550 lemma fps_of_int: "fps_const (of_int c) = of_int c"
  4551   by (induction c) (simp_all add: fps_const_minus [symmetric] fps_of_nat fps_const_neg [symmetric] 
  4552                              del: fps_const_minus fps_const_neg)
  4553 
  4554 lemma fps_deriv_of_int [simp]: "fps_deriv (of_int n) = 0"
  4555   by (simp add: fps_of_int [symmetric])
  4556 
  4557 lemma fps_numeral_fps_const: "numeral i = fps_const (numeral i :: 'a::comm_ring_1)"
  4558   by (fact numeral_fps_const) (* FIXME: duplicate *)
  4559 
  4560 lemma fps_cos_fps_exp_ii: "fps_cos c = (fps_exp (\<i> * c) + fps_exp (- \<i> * c)) / fps_const 2"
  4561 proof -
  4562   have th: "fps_cos c + fps_cos c = fps_cos c * fps_const 2"
  4563     by (simp add: numeral_fps_const)
  4564   show ?thesis
  4565     unfolding fps_exp_ii_sin_cos minus_mult_commute
  4566     by (simp add: fps_sin_even fps_cos_odd numeral_fps_const fps_divide_unit fps_const_inverse th)
  4567 qed
  4568 
  4569 lemma fps_sin_fps_exp_ii: "fps_sin c = (fps_exp (\<i> * c) - fps_exp (- \<i> * c)) / fps_const (2*\<i>)"
  4570 proof -
  4571   have th: "fps_const \<i> * fps_sin c + fps_const \<i> * fps_sin c = fps_sin c * fps_const (2 * \<i>)"
  4572     by (simp add: fps_eq_iff numeral_fps_const)
  4573   show ?thesis
  4574     unfolding fps_exp_ii_sin_cos minus_mult_commute
  4575     by (simp add: fps_sin_even fps_cos_odd fps_divide_unit fps_const_inverse th)
  4576 qed
  4577 
  4578 lemma fps_tan_fps_exp_ii:
  4579   "fps_tan c = (fps_exp (\<i> * c) - fps_exp (- \<i> * c)) / 
  4580       (fps_const \<i> * (fps_exp (\<i> * c) + fps_exp (- \<i> * c)))"
  4581   unfolding fps_tan_def fps_sin_fps_exp_ii fps_cos_fps_exp_ii mult_minus_left fps_exp_neg
  4582   apply (simp add: fps_divide_unit fps_inverse_mult fps_const_mult[symmetric] fps_const_inverse del: fps_const_mult)
  4583   apply simp
  4584   done
  4585 
  4586 lemma fps_demoivre:
  4587   "(fps_cos a + fps_const \<i> * fps_sin a)^n =
  4588     fps_cos (of_nat n * a) + fps_const \<i> * fps_sin (of_nat n * a)"
  4589   unfolding fps_exp_ii_sin_cos[symmetric] fps_exp_power_mult
  4590   by (simp add: ac_simps)
  4591 
  4592 
  4593 subsection \<open>Hypergeometric series\<close>
  4594 
  4595 definition "fps_hypergeo as bs (c::'a::{field_char_0,field}) =
  4596   Abs_fps (\<lambda>n. (foldl (\<lambda>r a. r* pochhammer a n) 1 as * c^n) /
  4597     (foldl (\<lambda>r b. r * pochhammer b n) 1 bs * of_nat (fact n)))"
  4598 
  4599 lemma fps_hypergeo_nth[simp]: "fps_hypergeo as bs c $ n =
  4600   (foldl (\<lambda>r a. r* pochhammer a n) 1 as * c^n) /
  4601     (foldl (\<lambda>r b. r * pochhammer b n) 1 bs * of_nat (fact n))"
  4602   by (simp add: fps_hypergeo_def)
  4603 
  4604 lemma foldl_mult_start:
  4605   fixes v :: "'a::comm_ring_1"
  4606   shows "foldl (\<lambda>r x. r * f x) v as * x = foldl (\<lambda>r x. r * f x) (v * x) as "
  4607   by (induct as arbitrary: x v) (auto simp add: algebra_simps)
  4608 
  4609 lemma foldr_mult_foldl:
  4610   fixes v :: "'a::comm_ring_1"
  4611   shows "foldr (\<lambda>x r. r * f x) as v = foldl (\<lambda>r x. r * f x) v as"
  4612   by (induct as arbitrary: v) (auto simp add: foldl_mult_start)
  4613 
  4614 lemma fps_hypergeo_nth_alt:
  4615   "fps_hypergeo as bs c $ n = foldr (\<lambda>a r. r * pochhammer a n) as (c ^ n) /
  4616     foldr (\<lambda>b r. r * pochhammer b n) bs (of_nat (fact n))"
  4617   by (simp add: foldl_mult_start foldr_mult_foldl)
  4618 
  4619 lemma fps_hypergeo_fps_exp[simp]: "fps_hypergeo [] [] c = fps_exp c"
  4620   by (simp add: fps_eq_iff)
  4621 
  4622 lemma fps_hypergeo_1_0[simp]: "fps_hypergeo [1] [] c = 1/(1 - fps_const c * X)"
  4623 proof -
  4624   let ?a = "(Abs_fps (\<lambda>n. 1)) oo (fps_const c * X)"
  4625   have th0: "(fps_const c * X) $ 0 = 0" by simp
  4626   show ?thesis unfolding gp[OF th0, symmetric]
  4627     by (auto simp add: fps_eq_iff pochhammer_fact[symmetric]
  4628       fps_compose_nth power_mult_distrib cond_value_iff sum.delta' cong del: if_weak_cong)
  4629 qed
  4630 
  4631 lemma fps_hypergeo_B[simp]: "fps_hypergeo [-a] [] (- 1) = fps_binomial a"
  4632   by (simp add: fps_eq_iff gbinomial_pochhammer algebra_simps)
  4633 
  4634 lemma fps_hypergeo_0[simp]: "fps_hypergeo as bs c $ 0 = 1"
  4635   apply simp
  4636   apply (subgoal_tac "\<forall>as. foldl (\<lambda>(r::'a) (a::'a). r) 1 as = 1")
  4637   apply auto
  4638   apply (induct_tac as)
  4639   apply auto
  4640   done
  4641 
  4642 lemma foldl_prod_prod:
  4643   "foldl (\<lambda>(r::'b::comm_ring_1) (x::'a::comm_ring_1). r * f x) v as * foldl (\<lambda>r x. r * g x) w as =
  4644     foldl (\<lambda>r x. r * f x * g x) (v * w) as"
  4645   by (induct as arbitrary: v w) (auto simp add: algebra_simps)
  4646 
  4647 
  4648 lemma fps_hypergeo_rec:
  4649   "fps_hypergeo as bs c $ Suc n = ((foldl (\<lambda>r a. r* (a + of_nat n)) c as) /
  4650     (foldl (\<lambda>r b. r * (b + of_nat n)) (of_nat (Suc n)) bs )) * fps_hypergeo as bs c $ n"
  4651   apply (simp del: of_nat_Suc of_nat_add fact_Suc)
  4652   apply (simp add: foldl_mult_start del: fact_Suc of_nat_Suc)
  4653   unfolding foldl_prod_prod[unfolded foldl_mult_start] pochhammer_Suc
  4654   apply (simp add: algebra_simps)
  4655   done
  4656 
  4657 lemma XD_nth[simp]: "XD a $ n = (if n = 0 then 0 else of_nat n * a$n)"
  4658   by (simp add: XD_def)
  4659 
  4660 lemma XD_0th[simp]: "XD a $ 0 = 0"
  4661   by simp
  4662 lemma XD_Suc[simp]:" XD a $ Suc n = of_nat (Suc n) * a $ Suc n"
  4663   by simp
  4664 
  4665 definition "XDp c a = XD a + fps_const c * a"
  4666 
  4667 lemma XDp_nth[simp]: "XDp c a $ n = (c + of_nat n) * a$n"
  4668   by (simp add: XDp_def algebra_simps)
  4669 
  4670 lemma XDp_commute: "XDp b \<circ> XDp (c::'a::comm_ring_1) = XDp c \<circ> XDp b"
  4671   by (auto simp add: XDp_def fun_eq_iff fps_eq_iff algebra_simps)
  4672 
  4673 lemma XDp0 [simp]: "XDp 0 = XD"
  4674   by (simp add: fun_eq_iff fps_eq_iff)
  4675 
  4676 lemma XDp_fps_integral [simp]: "XDp 0 (fps_integral a c) = X * a"
  4677   by (simp add: fps_eq_iff fps_integral_def)
  4678 
  4679 lemma fps_hypergeo_minus_nat:
  4680   "fps_hypergeo [- of_nat n] [- of_nat (n + m)] (c::'a::{field_char_0,field}) $ k =
  4681     (if k \<le> n then
  4682       pochhammer (- of_nat n) k * c ^ k / (pochhammer (- of_nat (n + m)) k * of_nat (fact k))
  4683      else 0)"
  4684   "fps_hypergeo [- of_nat m] [- of_nat (m + n)] (c::'a::{field_char_0,field}) $ k =
  4685     (if k \<le> m then
  4686       pochhammer (- of_nat m) k * c ^ k / (pochhammer (- of_nat (m + n)) k * of_nat (fact k))
  4687      else 0)"
  4688   by (auto simp add: pochhammer_eq_0_iff)
  4689 
  4690 lemma sum_eq_if: "sum f {(n::nat) .. m} = (if m < n then 0 else f n + sum f {n+1 .. m})"
  4691   apply simp
  4692   apply (subst sum.insert[symmetric])
  4693   apply (auto simp add: not_less sum_head_Suc)
  4694   done
  4695 
  4696 lemma pochhammer_rec_if: "pochhammer a n = (if n = 0 then 1 else a * pochhammer (a + 1) (n - 1))"
  4697   by (cases n) (simp_all add: pochhammer_rec)
  4698 
  4699 lemma XDp_foldr_nth [simp]: "foldr (\<lambda>c r. XDp c \<circ> r) cs (\<lambda>c. XDp c a) c0 $ n =
  4700     foldr (\<lambda>c r. (c + of_nat n) * r) cs (c0 + of_nat n) * a$n"
  4701   by (induct cs arbitrary: c0) (auto simp add: algebra_simps)
  4702 
  4703 lemma genric_XDp_foldr_nth:
  4704   assumes f: "\<forall>n c a. f c a $ n = (of_nat n + k c) * a$n"
  4705   shows "foldr (\<lambda>c r. f c \<circ> r) cs (\<lambda>c. g c a) c0 $ n =
  4706     foldr (\<lambda>c r. (k c + of_nat n) * r) cs (g c0 a $ n)"
  4707   by (induct cs arbitrary: c0) (auto simp add: algebra_simps f)
  4708 
  4709 lemma dist_less_imp_nth_equal:
  4710   assumes "dist f g < inverse (2 ^ i)"
  4711     and"j \<le> i"
  4712   shows "f $ j = g $ j"
  4713 proof (rule ccontr)
  4714   assume "f $ j \<noteq> g $ j"
  4715   hence "f \<noteq> g" by auto
  4716   with assms have "i < subdegree (f - g)"
  4717     by (simp add: if_split_asm dist_fps_def)
  4718   also have "\<dots> \<le> j"
  4719     using \<open>f $ j \<noteq> g $ j\<close> by (intro subdegree_leI) simp_all
  4720   finally show False using \<open>j \<le> i\<close> by simp
  4721 qed
  4722 
  4723 lemma nth_equal_imp_dist_less:
  4724   assumes "\<And>j. j \<le> i \<Longrightarrow> f $ j = g $ j"
  4725   shows "dist f g < inverse (2 ^ i)"
  4726 proof (cases "f = g")
  4727   case True
  4728   then show ?thesis by simp
  4729 next
  4730   case False
  4731   with assms have "dist f g = inverse (2 ^ subdegree (f - g))"
  4732     by (simp add: if_split_asm dist_fps_def)
  4733   moreover
  4734   from assms and False have "i < subdegree (f - g)"
  4735     by (intro subdegree_greaterI) simp_all
  4736   ultimately show ?thesis by simp
  4737 qed
  4738 
  4739 lemma dist_less_eq_nth_equal: "dist f g < inverse (2 ^ i) \<longleftrightarrow> (\<forall>j \<le> i. f $ j = g $ j)"
  4740   using dist_less_imp_nth_equal nth_equal_imp_dist_less by blast
  4741 
  4742 instance fps :: (comm_ring_1) complete_space
  4743 proof
  4744   fix X :: "nat \<Rightarrow> 'a fps"
  4745   assume "Cauchy X"
  4746   obtain M where M: "\<forall>i. \<forall>m \<ge> M i. \<forall>j \<le> i. X (M i) $ j = X m $ j"
  4747   proof -
  4748     have "\<exists>M. \<forall>m \<ge> M. \<forall>j\<le>i. X M $ j = X m $ j" for i
  4749     proof -
  4750       have "0 < inverse ((2::real)^i)" by simp
  4751       from metric_CauchyD[OF \<open>Cauchy X\<close> this] dist_less_imp_nth_equal
  4752       show ?thesis by blast
  4753     qed
  4754     then show ?thesis using that by metis
  4755   qed
  4756 
  4757   show "convergent X"
  4758   proof (rule convergentI)
  4759     show "X \<longlonglongrightarrow> Abs_fps (\<lambda>i. X (M i) $ i)"
  4760       unfolding tendsto_iff
  4761     proof safe
  4762       fix e::real assume e: "0 < e"
  4763       have "(\<lambda>n. inverse (2 ^ n) :: real) \<longlonglongrightarrow> 0" by (rule LIMSEQ_inverse_realpow_zero) simp_all
  4764       from this and e have "eventually (\<lambda>i. inverse (2 ^ i) < e) sequentially"
  4765         by (rule order_tendstoD)
  4766       then obtain i where "inverse (2 ^ i) < e"
  4767         by (auto simp: eventually_sequentially)
  4768       have "eventually (\<lambda>x. M i \<le> x) sequentially"
  4769         by (auto simp: eventually_sequentially)
  4770       then show "eventually (\<lambda>x. dist (X x) (Abs_fps (\<lambda>i. X (M i) $ i)) < e) sequentially"
  4771       proof eventually_elim
  4772         fix x
  4773         assume x: "M i \<le> x"
  4774         have "X (M i) $ j = X (M j) $ j" if "j \<le> i" for j
  4775           using M that by (metis nat_le_linear)
  4776         with x have "dist (X x) (Abs_fps (\<lambda>j. X (M j) $ j)) < inverse (2 ^ i)"
  4777           using M by (force simp: dist_less_eq_nth_equal)
  4778         also note \<open>inverse (2 ^ i) < e\<close>
  4779         finally show "dist (X x) (Abs_fps (\<lambda>j. X (M j) $ j)) < e" .
  4780       qed
  4781     qed
  4782   qed
  4783 qed
  4784 
  4785 end