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