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