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