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