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