Theory Formal_Power_Series

theory Formal_Power_Series
imports Complex_Main Euclidean_Algorithm
(*  Title:      HOL/Library/Formal_Power_Series.thy
    Author:     Amine Chaieb, University of Cambridge
*)

section ‹A formalization of formal power series›

theory Formal_Power_Series
imports Complex_Main "~~/src/HOL/Number_Theory/Euclidean_Algorithm"
begin


subsection ‹The type of formal power series›

typedef 'a fps = "{f :: nat ⇒ 'a. True}"
  morphisms fps_nth Abs_fps
  by simp

notation fps_nth (infixl "$" 75)

lemma expand_fps_eq: "p = q ⟷ (∀n. p $ n = q $ n)"
  by (simp add: fps_nth_inject [symmetric] fun_eq_iff)

lemma fps_ext: "(⋀n. p $ n = q $ n) ⟹ p = q"
  by (simp add: expand_fps_eq)

lemma fps_nth_Abs_fps [simp]: "Abs_fps f $ n = f n"
  by (simp add: Abs_fps_inverse)

text ‹Definition of the basic elements 0 and 1 and the basic operations of addition,
  negation and multiplication.›

instantiation fps :: (zero) zero
begin
  definition fps_zero_def: "0 = Abs_fps (λn. 0)"
  instance ..
end

lemma fps_zero_nth [simp]: "0 $ n = 0"
  unfolding fps_zero_def by simp

instantiation fps :: ("{one, zero}") one
begin
  definition fps_one_def: "1 = Abs_fps (λn. if n = 0 then 1 else 0)"
  instance ..
end

lemma fps_one_nth [simp]: "1 $ n = (if n = 0 then 1 else 0)"
  unfolding fps_one_def by simp

instantiation fps :: (plus) plus
begin
  definition fps_plus_def: "op + = (λf g. Abs_fps (λn. f $ n + g $ n))"
  instance ..
end

lemma fps_add_nth [simp]: "(f + g) $ n = f $ n + g $ n"
  unfolding fps_plus_def by simp

instantiation fps :: (minus) minus
begin
  definition fps_minus_def: "op - = (λf g. Abs_fps (λn. f $ n - g $ n))"
  instance ..
end

lemma fps_sub_nth [simp]: "(f - g) $ n = f $ n - g $ n"
  unfolding fps_minus_def by simp

instantiation fps :: (uminus) uminus
begin
  definition fps_uminus_def: "uminus = (λf. Abs_fps (λn. - (f $ n)))"
  instance ..
end

lemma fps_neg_nth [simp]: "(- f) $ n = - (f $ n)"
  unfolding fps_uminus_def by simp

instantiation fps :: ("{comm_monoid_add, times}") times
begin
  definition fps_times_def: "op * = (λf g. Abs_fps (λn. ∑i=0..n. f $ i * g $ (n - i)))"
  instance ..
end

lemma fps_mult_nth: "(f * g) $ n = (∑i=0..n. f$i * g$(n - i))"
  unfolding fps_times_def by simp

lemma fps_mult_nth_0 [simp]: "(f * g) $ 0 = f $ 0 * g $ 0"
  unfolding fps_times_def by simp

declare atLeastAtMost_iff [presburger]
declare Bex_def [presburger]
declare Ball_def [presburger]

lemma mult_delta_left:
  fixes x y :: "'a::mult_zero"
  shows "(if b then x else 0) * y = (if b then x * y else 0)"
  by simp

lemma mult_delta_right:
  fixes x y :: "'a::mult_zero"
  shows "x * (if b then y else 0) = (if b then x * y else 0)"
  by simp

lemma cond_value_iff: "f (if b then x else y) = (if b then f x else f y)"
  by auto

lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
  by auto


subsection ‹Formal power series form a commutative ring with unity, if the range of sequences
  they represent is a commutative ring with unity›

instance fps :: (semigroup_add) semigroup_add
proof
  fix a b c :: "'a fps"
  show "a + b + c = a + (b + c)"
    by (simp add: fps_ext add.assoc)
qed

instance fps :: (ab_semigroup_add) ab_semigroup_add
proof
  fix a b :: "'a fps"
  show "a + b = b + a"
    by (simp add: fps_ext add.commute)
qed

lemma fps_mult_assoc_lemma:
  fixes k :: nat
    and f :: "nat ⇒ nat ⇒ nat ⇒ 'a::comm_monoid_add"
  shows "(∑j=0..k. ∑i=0..j. f i (j - i) (n - j)) =
         (∑j=0..k. ∑i=0..k - j. f j i (n - j - i))"
  by (induct k) (simp_all add: Suc_diff_le setsum.distrib add.assoc)

instance fps :: (semiring_0) semigroup_mult
proof
  fix a b c :: "'a fps"
  show "(a * b) * c = a * (b * c)"
  proof (rule fps_ext)
    fix n :: nat
    have "(∑j=0..n. ∑i=0..j. a$i * b$(j - i) * c$(n - j)) =
          (∑j=0..n. ∑i=0..n - j. a$j * b$i * c$(n - j - i))"
      by (rule fps_mult_assoc_lemma)
    then show "((a * b) * c) $ n = (a * (b * c)) $ n"
      by (simp add: fps_mult_nth setsum_right_distrib setsum_left_distrib mult.assoc)
  qed
qed

lemma fps_mult_commute_lemma:
  fixes n :: nat
    and f :: "nat ⇒ nat ⇒ 'a::comm_monoid_add"
  shows "(∑i=0..n. f i (n - i)) = (∑i=0..n. f (n - i) i)"
  by (rule setsum.reindex_bij_witness[where i="op - n" and j="op - n"]) auto

instance fps :: (comm_semiring_0) ab_semigroup_mult
proof
  fix a b :: "'a fps"
  show "a * b = b * a"
  proof (rule fps_ext)
    fix n :: nat
    have "(∑i=0..n. a$i * b$(n - i)) = (∑i=0..n. a$(n - i) * b$i)"
      by (rule fps_mult_commute_lemma)
    then show "(a * b) $ n = (b * a) $ n"
      by (simp add: fps_mult_nth mult.commute)
  qed
qed

instance fps :: (monoid_add) monoid_add
proof
  fix a :: "'a fps"
  show "0 + a = a" by (simp add: fps_ext)
  show "a + 0 = a" by (simp add: fps_ext)
qed

instance fps :: (comm_monoid_add) comm_monoid_add
proof
  fix a :: "'a fps"
  show "0 + a = a" by (simp add: fps_ext)
qed

instance fps :: (semiring_1) monoid_mult
proof
  fix a :: "'a fps"
  show "1 * a = a"
    by (simp add: fps_ext fps_mult_nth mult_delta_left setsum.delta)
  show "a * 1 = a"
    by (simp add: fps_ext fps_mult_nth mult_delta_right setsum.delta')
qed

instance fps :: (cancel_semigroup_add) cancel_semigroup_add
proof
  fix a b c :: "'a fps"
  show "b = c" if "a + b = a + c"
    using that by (simp add: expand_fps_eq)
  show "b = c" if "b + a = c + a"
    using that by (simp add: expand_fps_eq)
qed

instance fps :: (cancel_ab_semigroup_add) cancel_ab_semigroup_add
proof
  fix a b c :: "'a fps"
  show "a + b - a = b"
    by (simp add: expand_fps_eq)
  show "a - b - c = a - (b + c)"
    by (simp add: expand_fps_eq diff_diff_eq)
qed

instance fps :: (cancel_comm_monoid_add) cancel_comm_monoid_add ..

instance fps :: (group_add) group_add
proof
  fix a b :: "'a fps"
  show "- a + a = 0" by (simp add: fps_ext)
  show "a + - b = a - b" by (simp add: fps_ext)
qed

instance fps :: (ab_group_add) ab_group_add
proof
  fix a b :: "'a fps"
  show "- a + a = 0" by (simp add: fps_ext)
  show "a - b = a + - b" by (simp add: fps_ext)
qed

instance fps :: (zero_neq_one) zero_neq_one
  by standard (simp add: expand_fps_eq)

instance fps :: (semiring_0) semiring
proof
  fix a b c :: "'a fps"
  show "(a + b) * c = a * c + b * c"
    by (simp add: expand_fps_eq fps_mult_nth distrib_right setsum.distrib)
  show "a * (b + c) = a * b + a * c"
    by (simp add: expand_fps_eq fps_mult_nth distrib_left setsum.distrib)
qed

instance fps :: (semiring_0) semiring_0
proof
  fix a :: "'a fps"
  show "0 * a = 0"
    by (simp add: fps_ext fps_mult_nth)
  show "a * 0 = 0"
    by (simp add: fps_ext fps_mult_nth)
qed

instance fps :: (semiring_0_cancel) semiring_0_cancel ..

instance fps :: (semiring_1) semiring_1 ..


subsection ‹Selection of the nth power of the implicit variable in the infinite sum›

lemma fps_nonzero_nth: "f ≠ 0 ⟷ (∃ n. f $n ≠ 0)"
  by (simp add: expand_fps_eq)

lemma fps_nonzero_nth_minimal: "f ≠ 0 ⟷ (∃n. f $ n ≠ 0 ∧ (∀m < n. f $ m = 0))"
  (is "?lhs ⟷ ?rhs")
proof
  let ?n = "LEAST n. f $ n ≠ 0"
  show ?rhs if ?lhs
  proof -
    from that have "∃n. f $ n ≠ 0"
      by (simp add: fps_nonzero_nth)
    then have "f $ ?n ≠ 0"
      by (rule LeastI_ex)
    moreover have "∀m<?n. f $ m = 0"
      by (auto dest: not_less_Least)
    ultimately have "f $ ?n ≠ 0 ∧ (∀m<?n. f $ m = 0)" ..
    then show ?thesis ..
  qed
  show ?lhs if ?rhs
    using that by (auto simp add: expand_fps_eq)
qed

lemma fps_eq_iff: "f = g ⟷ (∀n. f $ n = g $n)"
  by (rule expand_fps_eq)

lemma fps_setsum_nth: "setsum f S $ n = setsum (λk. (f k) $ n) S"
proof (cases "finite S")
  case True
  then show ?thesis by (induct set: finite) auto
next
  case False
  then show ?thesis by simp
qed


subsection ‹Injection of the basic ring elements and multiplication by scalars›

definition "fps_const c = Abs_fps (λn. if n = 0 then c else 0)"

lemma fps_nth_fps_const [simp]: "fps_const c $ n = (if n = 0 then c else 0)"
  unfolding fps_const_def by simp

lemma fps_const_0_eq_0 [simp]: "fps_const 0 = 0"
  by (simp add: fps_ext)

lemma fps_const_1_eq_1 [simp]: "fps_const 1 = 1"
  by (simp add: fps_ext)

lemma fps_const_neg [simp]: "- (fps_const (c::'a::ring)) = fps_const (- c)"
  by (simp add: fps_ext)

lemma fps_const_add [simp]: "fps_const (c::'a::monoid_add) + fps_const d = fps_const (c + d)"
  by (simp add: fps_ext)

lemma fps_const_sub [simp]: "fps_const (c::'a::group_add) - fps_const d = fps_const (c - d)"
  by (simp add: fps_ext)

lemma fps_const_mult[simp]: "fps_const (c::'a::ring) * fps_const d = fps_const (c * d)"
  by (simp add: fps_eq_iff fps_mult_nth setsum.neutral)

lemma fps_const_add_left: "fps_const (c::'a::monoid_add) + f =
    Abs_fps (λn. if n = 0 then c + f$0 else f$n)"
  by (simp add: fps_ext)

lemma fps_const_add_right: "f + fps_const (c::'a::monoid_add) =
    Abs_fps (λn. if n = 0 then f$0 + c else f$n)"
  by (simp add: fps_ext)

lemma fps_const_mult_left: "fps_const (c::'a::semiring_0) * f = Abs_fps (λn. c * f$n)"
  unfolding fps_eq_iff fps_mult_nth
  by (simp add: fps_const_def mult_delta_left setsum.delta)

lemma fps_const_mult_right: "f * fps_const (c::'a::semiring_0) = Abs_fps (λn. f$n * c)"
  unfolding fps_eq_iff fps_mult_nth
  by (simp add: fps_const_def mult_delta_right setsum.delta')

lemma fps_mult_left_const_nth [simp]: "(fps_const (c::'a::semiring_1) * f)$n = c* f$n"
  by (simp add: fps_mult_nth mult_delta_left setsum.delta)

lemma fps_mult_right_const_nth [simp]: "(f * fps_const (c::'a::semiring_1))$n = f$n * c"
  by (simp add: fps_mult_nth mult_delta_right setsum.delta')


subsection ‹Formal power series form an integral domain›

instance fps :: (ring) ring ..

instance fps :: (ring_1) ring_1
  by (intro_classes, auto simp add: distrib_right)

instance fps :: (comm_ring_1) comm_ring_1
  by (intro_classes, auto simp add: distrib_right)

instance fps :: (ring_no_zero_divisors) ring_no_zero_divisors
proof
  fix a b :: "'a fps"
  assume "a ≠ 0" and "b ≠ 0"
  then obtain i j where i: "a $ i ≠ 0" "∀k<i. a $ k = 0" and j: "b $ j ≠ 0" "∀k<j. b $ k =0"
    unfolding fps_nonzero_nth_minimal
    by blast+
  have "(a * b) $ (i + j) = (∑k=0..i+j. a $ k * b $ (i + j - k))"
    by (rule fps_mult_nth)
  also have "… = (a $ i * b $ (i + j - i)) + (∑k∈{0..i+j} - {i}. a $ k * b $ (i + j - k))"
    by (rule setsum.remove) simp_all
  also have "(∑k∈{0..i+j}-{i}. a $ k * b $ (i + j - k)) = 0"
  proof (rule setsum.neutral [rule_format])
    fix k assume "k ∈ {0..i+j} - {i}"
    then have "k < i ∨ i+j-k < j"
      by auto
    then show "a $ k * b $ (i + j - k) = 0"
      using i j by auto
  qed
  also have "a $ i * b $ (i + j - i) + 0 = a $ i * b $ j"
    by simp
  also have "a $ i * b $ j ≠ 0"
    using i j by simp
  finally have "(a*b) $ (i+j) ≠ 0" .
  then show "a * b ≠ 0"
    unfolding fps_nonzero_nth by blast
qed

instance fps :: (ring_1_no_zero_divisors) ring_1_no_zero_divisors ..

instance fps :: (idom) idom ..

lemma numeral_fps_const: "numeral k = fps_const (numeral k)"
  by (induct k) (simp_all only: numeral.simps fps_const_1_eq_1
    fps_const_add [symmetric])

lemma neg_numeral_fps_const:
  "(- numeral k :: 'a :: ring_1 fps) = fps_const (- numeral k)"
  by (simp add: numeral_fps_const)

lemma fps_numeral_nth: "numeral n $ i = (if i = 0 then numeral n else 0)"
  by (simp add: numeral_fps_const)

lemma fps_numeral_nth_0 [simp]: "numeral n $ 0 = numeral n"
  by (simp add: numeral_fps_const)


subsection ‹The eXtractor series X›

lemma minus_one_power_iff: "(- (1::'a::comm_ring_1)) ^ n = (if even n then 1 else - 1)"
  by (induct n) auto

definition "X = Abs_fps (λn. if n = 1 then 1 else 0)"

lemma X_mult_nth [simp]:
  "(X * (f :: 'a::semiring_1 fps)) $n = (if n = 0 then 0 else f $ (n - 1))"
proof (cases "n = 0")
  case False
  have "(X * f) $n = (∑i = 0..n. X $ i * f $ (n - i))"
    by (simp add: fps_mult_nth)
  also have "… = f $ (n - 1)"
    using False by (simp add: X_def mult_delta_left setsum.delta)
  finally show ?thesis
    using False by simp
next
  case True
  then show ?thesis
    by (simp add: fps_mult_nth X_def)
qed

lemma X_mult_right_nth[simp]:
    "((f :: 'a::comm_semiring_1 fps) * X) $n = (if n = 0 then 0 else f $ (n - 1))"
  by (metis X_mult_nth mult.commute)

lemma X_power_iff: "X^k = Abs_fps (λn. if n = k then 1::'a::comm_ring_1 else 0)"
proof (induct k)
  case 0
  then show ?case by (simp add: X_def fps_eq_iff)
next
  case (Suc k)
  have "(X^Suc k) $ m = (if m = Suc k then 1::'a else 0)" for m
  proof -
    have "(X^Suc k) $ m = (if m = 0 then 0 else (X^k) $ (m - 1))"
      by (simp del: One_nat_def)
    then show ?thesis
      using Suc.hyps by (auto cong del: if_weak_cong)
  qed
  then show ?case
    by (simp add: fps_eq_iff)
qed

lemma X_nth[simp]: "X$n = (if n = 1 then 1 else 0)"
  by (simp add: X_def)

lemma X_power_nth[simp]: "(X^k) $n = (if n = k then 1 else 0::'a::comm_ring_1)"
  by (simp add: X_power_iff)

lemma X_power_mult_nth: "(X^k * (f :: 'a::comm_ring_1 fps)) $n = (if n < k then 0 else f $ (n - k))"
  apply (induct k arbitrary: n)
  apply simp
  unfolding power_Suc mult.assoc
  apply (case_tac n)
  apply auto
  done

lemma X_power_mult_right_nth:
    "((f :: 'a::comm_ring_1 fps) * X^k) $n = (if n < k then 0 else f $ (n - k))"
  by (metis X_power_mult_nth mult.commute)


lemma X_neq_fps_const [simp]: "(X :: 'a :: zero_neq_one fps) ≠ fps_const c"
proof
  assume "(X::'a fps) = fps_const (c::'a)"
  hence "X$1 = (fps_const (c::'a))$1" by (simp only:)
  thus False by auto
qed

lemma X_neq_zero [simp]: "(X :: 'a :: zero_neq_one fps) ≠ 0"
  by (simp only: fps_const_0_eq_0[symmetric] X_neq_fps_const) simp

lemma X_neq_one [simp]: "(X :: 'a :: zero_neq_one fps) ≠ 1"
  by (simp only: fps_const_1_eq_1[symmetric] X_neq_fps_const) simp

lemma X_neq_numeral [simp]: "(X :: 'a :: {semiring_1,zero_neq_one} fps) ≠ numeral c"
  by (simp only: numeral_fps_const X_neq_fps_const) simp

lemma X_pow_eq_X_pow_iff [simp]:
  "(X :: ('a :: {comm_ring_1}) fps) ^ m = X ^ n ⟷ m = n"
proof
  assume "(X :: 'a fps) ^ m = X ^ n"
  hence "(X :: 'a fps) ^ m $ m = X ^ n $ m" by (simp only:)
  thus "m = n" by (simp split: if_split_asm)
qed simp_all


subsection ‹Subdegrees›

definition subdegree :: "('a::zero) fps ⇒ nat" where
  "subdegree f = (if f = 0 then 0 else LEAST n. f$n ≠ 0)"

lemma subdegreeI:
  assumes "f $ d ≠ 0" and "⋀i. i < d ⟹ f $ i = 0"
  shows   "subdegree f = d"
proof-
  from assms(1) have "f ≠ 0" by auto
  moreover from assms(1) have "(LEAST i. f $ i ≠ 0) = d"
  proof (rule Least_equality)
    fix e assume "f $ e ≠ 0"
    with assms(2) have "¬(e < d)" by blast
    thus "e ≥ d" by simp
  qed
  ultimately show ?thesis unfolding subdegree_def by simp
qed

lemma nth_subdegree_nonzero [simp,intro]: "f ≠ 0 ⟹ f $ subdegree f ≠ 0"
proof-
  assume "f ≠ 0"
  hence "subdegree f = (LEAST n. f $ n ≠ 0)" by (simp add: subdegree_def)
  also from ‹f ≠ 0› have "∃n. f$n ≠ 0" using fps_nonzero_nth by blast
  from LeastI_ex[OF this] have "f $ (LEAST n. f $ n ≠ 0) ≠ 0" .
  finally show ?thesis .
qed

lemma nth_less_subdegree_zero [dest]: "n < subdegree f ⟹ f $ n = 0"
proof (cases "f = 0")
  assume "f ≠ 0" and less: "n < subdegree f"
  note less
  also from ‹f ≠ 0› have "subdegree f = (LEAST n. f $ n ≠ 0)" by (simp add: subdegree_def)
  finally show "f $ n = 0" using not_less_Least by blast
qed simp_all

lemma subdegree_geI:
  assumes "f ≠ 0" "⋀i. i < n ⟹ f$i = 0"
  shows   "subdegree f ≥ n"
proof (rule ccontr)
  assume "¬(subdegree f ≥ n)"
  with assms(2) have "f $ subdegree f = 0" by simp
  moreover from assms(1) have "f $ subdegree f ≠ 0" by simp
  ultimately show False by contradiction
qed

lemma subdegree_greaterI:
  assumes "f ≠ 0" "⋀i. i ≤ n ⟹ f$i = 0"
  shows   "subdegree f > n"
proof (rule ccontr)
  assume "¬(subdegree f > n)"
  with assms(2) have "f $ subdegree f = 0" by simp
  moreover from assms(1) have "f $ subdegree f ≠ 0" by simp
  ultimately show False by contradiction
qed

lemma subdegree_leI:
  "f $ n ≠ 0 ⟹ subdegree f ≤ n"
  by (rule leI) auto


lemma subdegree_0 [simp]: "subdegree 0 = 0"
  by (simp add: subdegree_def)

lemma subdegree_1 [simp]: "subdegree (1 :: ('a :: zero_neq_one) fps) = 0"
  by (auto intro!: subdegreeI)

lemma subdegree_X [simp]: "subdegree (X :: ('a :: zero_neq_one) fps) = 1"
  by (auto intro!: subdegreeI simp: X_def)

lemma subdegree_fps_const [simp]: "subdegree (fps_const c) = 0"
  by (cases "c = 0") (auto intro!: subdegreeI)

lemma subdegree_numeral [simp]: "subdegree (numeral n) = 0"
  by (simp add: numeral_fps_const)

lemma subdegree_eq_0_iff: "subdegree f = 0 ⟷ f = 0 ∨ f $ 0 ≠ 0"
proof (cases "f = 0")
  assume "f ≠ 0"
  thus ?thesis
    using nth_subdegree_nonzero[OF ‹f ≠ 0›] by (fastforce intro!: subdegreeI)
qed simp_all

lemma subdegree_eq_0 [simp]: "f $ 0 ≠ 0 ⟹ subdegree f = 0"
  by (simp add: subdegree_eq_0_iff)

lemma nth_subdegree_mult [simp]:
  fixes f g :: "('a :: {mult_zero,comm_monoid_add}) fps"
  shows "(f * g) $ (subdegree f + subdegree g) = f $ subdegree f * g $ subdegree g"
proof-
  let ?n = "subdegree f + subdegree g"
  have "(f * g) $ ?n = (∑i=0..?n. f$i * g$(?n-i))"
    by (simp add: fps_mult_nth)
  also have "... = (∑i=0..?n. if i = subdegree f then f$i * g$(?n-i) else 0)"
  proof (intro setsum.cong)
    fix x assume x: "x ∈ {0..?n}"
    hence "x = subdegree f ∨ x < subdegree f ∨ ?n - x < subdegree g" by auto
    thus "f $ x * g $ (?n - x) = (if x = subdegree f then f $ x * g $ (?n - x) else 0)"
      by (elim disjE conjE) auto
  qed auto
  also have "... = f $ subdegree f * g $ subdegree g" by (simp add: setsum.delta)
  finally show ?thesis .
qed

lemma subdegree_mult [simp]:
  assumes "f ≠ 0" "g ≠ 0"
  shows "subdegree ((f :: ('a :: {ring_no_zero_divisors}) fps) * g) = subdegree f + subdegree g"
proof (rule subdegreeI)
  let ?n = "subdegree f + subdegree g"
  have "(f * g) $ ?n = (∑i=0..?n. f$i * g$(?n-i))" by (simp add: fps_mult_nth)
  also have "... = (∑i=0..?n. if i = subdegree f then f$i * g$(?n-i) else 0)"
  proof (intro setsum.cong)
    fix x assume x: "x ∈ {0..?n}"
    hence "x = subdegree f ∨ x < subdegree f ∨ ?n - x < subdegree g" by auto
    thus "f $ x * g $ (?n - x) = (if x = subdegree f then f $ x * g $ (?n - x) else 0)"
      by (elim disjE conjE) auto
  qed auto
  also have "... = f $ subdegree f * g $ subdegree g" by (simp add: setsum.delta)
  also from assms have "... ≠ 0" by auto
  finally show "(f * g) $ (subdegree f + subdegree g) ≠ 0" .
next
  fix m assume m: "m < subdegree f + subdegree g"
  have "(f * g) $ m = (∑i=0..m. f$i * g$(m-i))" by (simp add: fps_mult_nth)
  also have "... = (∑i=0..m. 0)"
  proof (rule setsum.cong)
    fix i assume "i ∈ {0..m}"
    with m have "i < subdegree f ∨ m - i < subdegree g" by auto
    thus "f$i * g$(m-i) = 0" by (elim disjE) auto
  qed auto
  finally show "(f * g) $ m = 0" by simp
qed

lemma subdegree_power [simp]:
  "subdegree ((f :: ('a :: ring_1_no_zero_divisors) fps) ^ n) = n * subdegree f"
  by (cases "f = 0"; induction n) simp_all

lemma subdegree_uminus [simp]:
  "subdegree (-(f::('a::group_add) fps)) = subdegree f"
  by (simp add: subdegree_def)

lemma subdegree_minus_commute [simp]:
  "subdegree (f-(g::('a::group_add) fps)) = subdegree (g - f)"
proof -
  have "f - g = -(g - f)" by simp
  also have "subdegree ... = subdegree (g - f)" by (simp only: subdegree_uminus)
  finally show ?thesis .
qed

lemma subdegree_add_ge:
  assumes "f ≠ -(g :: ('a :: {group_add}) fps)"
  shows   "subdegree (f + g) ≥ min (subdegree f) (subdegree g)"
proof (rule subdegree_geI)
  from assms show "f + g ≠ 0" by (subst (asm) eq_neg_iff_add_eq_0)
next
  fix i assume "i < min (subdegree f) (subdegree g)"
  hence "f $ i = 0" and "g $ i = 0" by auto
  thus "(f + g) $ i = 0" by force
qed

lemma subdegree_add_eq1:
  assumes "f ≠ 0"
  assumes "subdegree f < subdegree (g :: ('a :: {group_add}) fps)"
  shows   "subdegree (f + g) = subdegree f"
proof (rule antisym[OF subdegree_leI])
  from assms show "subdegree (f + g) ≥ subdegree f"
    by (intro order.trans[OF min.boundedI subdegree_add_ge]) auto
  from assms have "f $ subdegree f ≠ 0" "g $ subdegree f = 0" by auto
  thus "(f + g) $ subdegree f ≠ 0" by simp
qed

lemma subdegree_add_eq2:
  assumes "g ≠ 0"
  assumes "subdegree g < subdegree (f :: ('a :: {ab_group_add}) fps)"
  shows   "subdegree (f + g) = subdegree g"
  using subdegree_add_eq1[OF assms] by (simp add: add.commute)

lemma subdegree_diff_eq1:
  assumes "f ≠ 0"
  assumes "subdegree f < subdegree (g :: ('a :: {ab_group_add}) fps)"
  shows   "subdegree (f - g) = subdegree f"
  using subdegree_add_eq1[of f "-g"] assms by (simp add: add.commute)

lemma subdegree_diff_eq2:
  assumes "g ≠ 0"
  assumes "subdegree g < subdegree (f :: ('a :: {ab_group_add}) fps)"
  shows   "subdegree (f - g) = subdegree g"
  using subdegree_add_eq2[of "-g" f] assms by (simp add: add.commute)

lemma subdegree_diff_ge [simp]:
  assumes "f ≠ (g :: ('a :: {group_add}) fps)"
  shows   "subdegree (f - g) ≥ min (subdegree f) (subdegree g)"
  using assms subdegree_add_ge[of f "-g"] by simp




subsection ‹Shifting and slicing›

definition fps_shift :: "nat ⇒ 'a fps ⇒ 'a fps" where
  "fps_shift n f = Abs_fps (λi. f $ (i + n))"

lemma fps_shift_nth [simp]: "fps_shift n f $ i = f $ (i + n)"
  by (simp add: fps_shift_def)

lemma fps_shift_0 [simp]: "fps_shift 0 f = f"
  by (intro fps_ext) (simp add: fps_shift_def)

lemma fps_shift_zero [simp]: "fps_shift n 0 = 0"
  by (intro fps_ext) (simp add: fps_shift_def)

lemma fps_shift_one: "fps_shift n 1 = (if n = 0 then 1 else 0)"
  by (intro fps_ext) (simp add: fps_shift_def)

lemma fps_shift_fps_const: "fps_shift n (fps_const c) = (if n = 0 then fps_const c else 0)"
  by (intro fps_ext) (simp add: fps_shift_def)

lemma fps_shift_numeral: "fps_shift n (numeral c) = (if n = 0 then numeral c else 0)"
  by (simp add: numeral_fps_const fps_shift_fps_const)

lemma fps_shift_X_power [simp]:
  "n ≤ m ⟹ fps_shift n (X ^ m) = (X ^ (m - n) ::'a::comm_ring_1 fps)"
  by (intro fps_ext) (auto simp: fps_shift_def )

lemma fps_shift_times_X_power:
  "n ≤ subdegree f ⟹ fps_shift n f * X ^ n = (f :: 'a :: comm_ring_1 fps)"
  by (intro fps_ext) (auto simp: X_power_mult_right_nth nth_less_subdegree_zero)

lemma fps_shift_times_X_power' [simp]:
  "fps_shift n (f * X^n) = (f :: 'a :: comm_ring_1 fps)"
  by (intro fps_ext) (auto simp: X_power_mult_right_nth nth_less_subdegree_zero)

lemma fps_shift_times_X_power'':
  "m ≤ n ⟹ fps_shift n (f * X^m) = fps_shift (n - m) (f :: 'a :: comm_ring_1 fps)"
  by (intro fps_ext) (auto simp: X_power_mult_right_nth nth_less_subdegree_zero)

lemma fps_shift_subdegree [simp]:
  "n ≤ subdegree f ⟹ subdegree (fps_shift n f) = subdegree (f :: 'a :: comm_ring_1 fps) - n"
  by (cases "f = 0") (force intro: nth_less_subdegree_zero subdegreeI)+

lemma subdegree_decompose:
  "f = fps_shift (subdegree f) f * X ^ subdegree (f :: ('a :: comm_ring_1) fps)"
  by (rule fps_ext) (auto simp: X_power_mult_right_nth)

lemma subdegree_decompose':
  "n ≤ subdegree (f :: ('a :: comm_ring_1) fps) ⟹ f = fps_shift n f * X^n"
  by (rule fps_ext) (auto simp: X_power_mult_right_nth intro!: nth_less_subdegree_zero)

lemma fps_shift_fps_shift:
  "fps_shift (m + n) f = fps_shift m (fps_shift n f)"
  by (rule fps_ext) (simp add: add_ac)

lemma fps_shift_add:
  "fps_shift n (f + g) = fps_shift n f + fps_shift n g"
  by (simp add: fps_eq_iff)

lemma fps_shift_mult:
  assumes "n ≤ subdegree (g :: 'b :: {comm_ring_1} fps)"
  shows   "fps_shift n (h*g) = h * fps_shift n g"
proof -
  from assms have "g = fps_shift n g * X^n" by (rule subdegree_decompose')
  also have "h * ... = (h * fps_shift n g) * X^n" by simp
  also have "fps_shift n ... = h * fps_shift n g" by simp
  finally show ?thesis .
qed

lemma fps_shift_mult_right:
  assumes "n ≤ subdegree (g :: 'b :: {comm_ring_1} fps)"
  shows   "fps_shift n (g*h) = h * fps_shift n g"
  by (subst mult.commute, subst fps_shift_mult) (simp_all add: assms)

lemma nth_subdegree_zero_iff [simp]: "f $ subdegree f = 0 ⟷ f = 0"
  by (cases "f = 0") auto

lemma fps_shift_subdegree_zero_iff [simp]:
  "fps_shift (subdegree f) f = 0 ⟷ f = 0"
  by (subst (1) nth_subdegree_zero_iff[symmetric], cases "f = 0")
     (simp_all del: nth_subdegree_zero_iff)


definition "fps_cutoff n f = Abs_fps (λi. if i < n then f$i else 0)"

lemma fps_cutoff_nth [simp]: "fps_cutoff n f $ i = (if i < n then f$i else 0)"
  unfolding fps_cutoff_def by simp

lemma fps_cutoff_zero_iff: "fps_cutoff n f = 0 ⟷ (f = 0 ∨ n ≤ subdegree f)"
proof
  assume A: "fps_cutoff n f = 0"
  thus "f = 0 ∨ n ≤ subdegree f"
  proof (cases "f = 0")
    assume "f ≠ 0"
    with A have "n ≤ subdegree f"
      by (intro subdegree_geI) (auto simp: fps_eq_iff split: if_split_asm)
    thus ?thesis ..
  qed simp
qed (auto simp: fps_eq_iff intro: nth_less_subdegree_zero)

lemma fps_cutoff_0 [simp]: "fps_cutoff 0 f = 0"
  by (simp add: fps_eq_iff)

lemma fps_cutoff_zero [simp]: "fps_cutoff n 0 = 0"
  by (simp add: fps_eq_iff)

lemma fps_cutoff_one: "fps_cutoff n 1 = (if n = 0 then 0 else 1)"
  by (simp add: fps_eq_iff)

lemma fps_cutoff_fps_const: "fps_cutoff n (fps_const c) = (if n = 0 then 0 else fps_const c)"
  by (simp add: fps_eq_iff)

lemma fps_cutoff_numeral: "fps_cutoff n (numeral c) = (if n = 0 then 0 else numeral c)"
  by (simp add: numeral_fps_const fps_cutoff_fps_const)

lemma fps_shift_cutoff:
  "fps_shift n (f :: ('a :: comm_ring_1) fps) * X^n + fps_cutoff n f = f"
  by (simp add: fps_eq_iff X_power_mult_right_nth)


subsection ‹Formal Power series form a metric space›

definition (in dist) "ball x r = {y. dist y x < r}"

instantiation fps :: (comm_ring_1) dist
begin

definition
  dist_fps_def: "dist (a :: 'a fps) b = (if a = b then 0 else inverse (2 ^ subdegree (a - b)))"

lemma dist_fps_ge0: "dist (a :: 'a fps) b ≥ 0"
  by (simp add: dist_fps_def)

lemma dist_fps_sym: "dist (a :: 'a fps) b = dist b a"
  by (simp add: dist_fps_def)

instance ..

end

instantiation fps :: (comm_ring_1) metric_space
begin

definition uniformity_fps_def [code del]:
  "(uniformity :: ('a fps × 'a fps) filter) = (INF e:{0 <..}. principal {(x, y). dist x y < e})"

definition open_fps_def' [code del]:
  "open (U :: 'a fps set) ⟷ (∀x∈U. eventually (λ(x', y). x' = x ⟶ y ∈ U) uniformity)"

instance
proof
  show th: "dist a b = 0 ⟷ a = b" for a b :: "'a fps"
    by (simp add: dist_fps_def split: if_split_asm)
  then have th'[simp]: "dist a a = 0" for a :: "'a fps" by simp

  fix a b c :: "'a fps"
  consider "a = b" | "c = a ∨ c = b" | "a ≠ b" "a ≠ c" "b ≠ c" by blast
  then show "dist a b ≤ dist a c + dist b c"
  proof cases
    case 1
    then show ?thesis by (simp add: dist_fps_def)
  next
    case 2
    then show ?thesis
      by (cases "c = a") (simp_all add: th dist_fps_sym)
  next
    case neq: 3
    have False if "dist a b > dist a c + dist b c"
    proof -
      let ?n = "subdegree (a - b)"
      from neq have "dist a b > 0" "dist b c > 0" and "dist a c > 0" by (simp_all add: dist_fps_def)
      with that have "dist a b > dist a c" and "dist a b > dist b c" by simp_all
      with neq have "?n < subdegree (a - c)" and "?n < subdegree (b - c)"
        by (simp_all add: dist_fps_def field_simps)
      hence "(a - c) $ ?n = 0" and "(b - c) $ ?n = 0"
        by (simp_all only: nth_less_subdegree_zero)
      hence "(a - b) $ ?n = 0" by simp
      moreover from neq have "(a - b) $ ?n ≠ 0" by (intro nth_subdegree_nonzero) simp_all
      ultimately show False by contradiction
    qed
    thus ?thesis by (auto simp add: not_le[symmetric])
  qed
qed (rule open_fps_def' uniformity_fps_def)+

end

declare uniformity_Abort[where 'a="'a :: comm_ring_1 fps", code]

lemma open_fps_def: "open (S :: 'a::comm_ring_1 fps set) = (∀a ∈ S. ∃r. r >0 ∧ ball a r ⊆ S)"
  unfolding open_dist ball_def subset_eq by simp

text ‹The infinite sums and justification of the notation in textbooks.›

lemma reals_power_lt_ex:
  fixes x y :: real
  assumes xp: "x > 0"
    and y1: "y > 1"
  shows "∃k>0. (1/y)^k < x"
proof -
  have yp: "y > 0"
    using y1 by simp
  from reals_Archimedean2[of "max 0 (- log y x) + 1"]
  obtain k :: nat where k: "real k > max 0 (- log y x) + 1"
    by blast
  from k have kp: "k > 0"
    by simp
  from k have "real k > - log y x"
    by simp
  then have "ln y * real k > - ln x"
    unfolding log_def
    using ln_gt_zero_iff[OF yp] y1
    by (simp add: minus_divide_left field_simps del: minus_divide_left[symmetric])
  then have "ln y * real k + ln x > 0"
    by simp
  then have "exp (real k * ln y + ln x) > exp 0"
    by (simp add: ac_simps)
  then have "y ^ k * x > 1"
    unfolding exp_zero exp_add exp_real_of_nat_mult exp_ln [OF xp] exp_ln [OF yp]
    by simp
  then have "x > (1 / y)^k" using yp
    by (simp add: field_simps)
  then show ?thesis
    using kp by blast
qed

lemma fps_sum_rep_nth: "(setsum (λi. fps_const(a$i)*X^i) {0..m})$n =
    (if n ≤ m then a$n else 0::'a::comm_ring_1)"
  apply (auto simp add: fps_setsum_nth cond_value_iff cong del: if_weak_cong)
  apply (simp add: setsum.delta')
  done

lemma fps_notation: "(λn. setsum (λi. fps_const(a$i) * X^i) {0..n}) ⇢ a"
  (is "?s ⇢ a")
proof -
  have "∃n0. ∀n ≥ n0. dist (?s n) a < r" if "r > 0" for r
  proof -
    obtain n0 where n0: "(1/2)^n0 < r" "n0 > 0"
      using reals_power_lt_ex[OF ‹r > 0›, of 2] by auto
    show ?thesis
    proof -
      have "dist (?s n) a < r" if nn0: "n ≥ n0" for n
      proof -
        from that have thnn0: "(1/2)^n ≤ (1/2 :: real)^n0"
          by (simp add: divide_simps)
        show ?thesis
        proof (cases "?s n = a")
          case True
          then show ?thesis
            unfolding dist_eq_0_iff[of "?s n" a, symmetric]
            using ‹r > 0› by (simp del: dist_eq_0_iff)
        next
          case False
          from False have dth: "dist (?s n) a = (1/2)^subdegree (?s n - a)"
            by (simp add: dist_fps_def field_simps)
          from False have kn: "subdegree (?s n - a) > n"
            by (intro subdegree_greaterI) (simp_all add: fps_sum_rep_nth)
          then have "dist (?s n) a < (1/2)^n"
            by (simp add: field_simps dist_fps_def)
          also have "… ≤ (1/2)^n0"
            using nn0 by (simp add: divide_simps)
          also have "… < r"
            using n0 by simp
          finally show ?thesis .
        qed
      qed
      then show ?thesis by blast
    qed
  qed
  then show ?thesis
    unfolding lim_sequentially by blast
qed


subsection ‹Inverses of formal power series›

declare setsum.cong[fundef_cong]

instantiation fps :: ("{comm_monoid_add,inverse,times,uminus}") inverse
begin

fun natfun_inverse:: "'a fps ⇒ nat ⇒ 'a"
where
  "natfun_inverse f 0 = inverse (f$0)"
| "natfun_inverse f n = - inverse (f$0) * setsum (λi. f$i * natfun_inverse f (n - i)) {1..n}"

definition fps_inverse_def: "inverse f = (if f $ 0 = 0 then 0 else Abs_fps (natfun_inverse f))"

definition fps_divide_def:
  "f div g = (if g = 0 then 0 else
     let n = subdegree g; h = fps_shift n g
     in  fps_shift n (f * inverse h))"

instance ..

end

lemma fps_inverse_zero [simp]:
  "inverse (0 :: 'a::{comm_monoid_add,inverse,times,uminus} fps) = 0"
  by (simp add: fps_ext fps_inverse_def)

lemma fps_inverse_one [simp]: "inverse (1 :: 'a::{division_ring,zero_neq_one} fps) = 1"
  apply (auto simp add: expand_fps_eq fps_inverse_def)
  apply (case_tac n)
  apply auto
  done

lemma inverse_mult_eq_1 [intro]:
  assumes f0: "f$0 ≠ (0::'a::field)"
  shows "inverse f * f = 1"
proof -
  have c: "inverse f * f = f * inverse f"
    by (simp add: mult.commute)
  from f0 have ifn: "⋀n. inverse f $ n = natfun_inverse f n"
    by (simp add: fps_inverse_def)
  from f0 have th0: "(inverse f * f) $ 0 = 1"
    by (simp add: fps_mult_nth fps_inverse_def)
  have "(inverse f * f)$n = 0" if np: "n > 0" for n
  proof -
    from np have eq: "{0..n} = {0} ∪ {1 .. n}"
      by auto
    have d: "{0} ∩ {1 .. n} = {}"
      by auto
    from f0 np have th0: "- (inverse f $ n) =
      (setsum (λi. f$i * natfun_inverse f (n - i)) {1..n}) / (f$0)"
      by (cases n) (simp_all add: divide_inverse fps_inverse_def)
    from th0[symmetric, unfolded nonzero_divide_eq_eq[OF f0]]
    have th1: "setsum (λi. f$i * natfun_inverse f (n - i)) {1..n} = - (f$0) * (inverse f)$n"
      by (simp add: field_simps)
    have "(f * inverse f) $ n = (∑i = 0..n. f $i * natfun_inverse f (n - i))"
      unfolding fps_mult_nth ifn ..
    also have "… = f$0 * natfun_inverse f n + (∑i = 1..n. f$i * natfun_inverse f (n-i))"
      by (simp add: eq)
    also have "… = 0"
      unfolding th1 ifn by simp
    finally show ?thesis unfolding c .
  qed
  with th0 show ?thesis
    by (simp add: fps_eq_iff)
qed

lemma fps_inverse_0_iff[simp]: "(inverse f) $ 0 = (0::'a::division_ring) ⟷ f $ 0 = 0"
  by (simp add: fps_inverse_def nonzero_imp_inverse_nonzero)

lemma fps_inverse_nth_0 [simp]: "inverse f $ 0 = inverse (f $ 0 :: 'a :: division_ring)"
  by (simp add: fps_inverse_def)

lemma fps_inverse_eq_0_iff[simp]: "inverse f = (0:: ('a::division_ring) fps) ⟷ f $ 0 = 0"
proof
  assume A: "inverse f = 0"
  have "0 = inverse f $ 0" by (subst A) simp
  thus "f $ 0 = 0" by simp
qed (simp add: fps_inverse_def)

lemma fps_inverse_idempotent[intro, simp]:
  assumes f0: "f$0 ≠ (0::'a::field)"
  shows "inverse (inverse f) = f"
proof -
  from f0 have if0: "inverse f $ 0 ≠ 0" by simp
  from inverse_mult_eq_1[OF f0] inverse_mult_eq_1[OF if0]
  have "inverse f * f = inverse f * inverse (inverse f)"
    by (simp add: ac_simps)
  then show ?thesis
    using f0 unfolding mult_cancel_left by simp
qed

lemma fps_inverse_unique:
  assumes fg: "(f :: 'a :: field fps) * g = 1"
  shows   "inverse f = g"
proof -
  have f0: "f $ 0 ≠ 0"
  proof
    assume "f $ 0 = 0"
    hence "0 = (f * g) $ 0" by simp
    also from fg have "(f * g) $ 0 = 1" by simp
    finally show False by simp
  qed
  from inverse_mult_eq_1[OF this] fg
  have th0: "inverse f * f = g * f"
    by (simp add: ac_simps)
  then show ?thesis
    using f0
    unfolding mult_cancel_right
    by (auto simp add: expand_fps_eq)
qed

lemma setsum_zero_lemma:
  fixes n::nat
  assumes "0 < n"
  shows "(∑i = 0..n. if n = i then 1 else if n - i = 1 then - 1 else 0) = (0::'a::field)"
proof -
  let ?f = "λi. if n = i then 1 else if n - i = 1 then - 1 else 0"
  let ?g = "λi. if i = n then 1 else if i = n - 1 then - 1 else 0"
  let ?h = "λi. if i=n - 1 then - 1 else 0"
  have th1: "setsum ?f {0..n} = setsum ?g {0..n}"
    by (rule setsum.cong) auto
  have th2: "setsum ?g {0..n - 1} = setsum ?h {0..n - 1}"
    apply (rule setsum.cong)
    using assms
    apply auto
    done
  have eq: "{0 .. n} = {0.. n - 1} ∪ {n}"
    by auto
  from assms have d: "{0.. n - 1} ∩ {n} = {}"
    by auto
  have f: "finite {0.. n - 1}" "finite {n}"
    by auto
  show ?thesis
    unfolding th1
    apply (simp add: setsum.union_disjoint[OF f d, unfolded eq[symmetric]] del: One_nat_def)
    unfolding th2
    apply (simp add: setsum.delta)
    done
qed

lemma fps_inverse_mult: "inverse (f * g :: 'a::field fps) = inverse f * inverse g"
proof (cases "f$0 = 0 ∨ g$0 = 0")
  assume "¬(f$0 = 0 ∨ g$0 = 0)"
  hence [simp]: "f$0 ≠ 0" "g$0 ≠ 0" by simp_all
  show ?thesis
  proof (rule fps_inverse_unique)
    have "f * g * (inverse f * inverse g) = (inverse f * f) * (inverse g * g)" by simp
    also have "... = 1" by (subst (1 2) inverse_mult_eq_1) simp_all
    finally show "f * g * (inverse f * inverse g) = 1" .
  qed
next
  assume A: "f$0 = 0 ∨ g$0 = 0"
  hence "inverse (f * g) = 0" by simp
  also from A have "... = inverse f * inverse g" by auto
  finally show "inverse (f * g) = inverse f * inverse g" .
qed


lemma fps_inverse_gp: "inverse (Abs_fps(λn. (1::'a::field))) =
    Abs_fps (λn. if n= 0 then 1 else if n=1 then - 1 else 0)"
  apply (rule fps_inverse_unique)
  apply (simp_all add: fps_eq_iff fps_mult_nth setsum_zero_lemma)
  done

lemma subdegree_inverse [simp]: "subdegree (inverse (f::'a::field fps)) = 0"
proof (cases "f$0 = 0")
  assume nz: "f$0 ≠ 0"
  hence "subdegree (inverse f) + subdegree f = subdegree (inverse f * f)"
    by (subst subdegree_mult) auto
  also from nz have "subdegree f = 0" by (simp add: subdegree_eq_0_iff)
  also from nz have "inverse f * f = 1" by (rule inverse_mult_eq_1)
  finally show "subdegree (inverse f) = 0" by simp
qed (simp_all add: fps_inverse_def)

lemma fps_is_unit_iff [simp]: "(f :: 'a :: field fps) dvd 1 ⟷ f $ 0 ≠ 0"
proof
  assume "f dvd 1"
  then obtain g where "1 = f * g" by (elim dvdE)
  from this[symmetric] have "(f*g) $ 0 = 1" by simp
  thus "f $ 0 ≠ 0" by auto
next
  assume A: "f $ 0 ≠ 0"
  thus "f dvd 1" by (simp add: inverse_mult_eq_1[OF A, symmetric])
qed

lemma subdegree_eq_0' [simp]: "(f :: 'a :: field fps) dvd 1 ⟹ subdegree f = 0"
  by simp

lemma fps_unit_dvd [simp]: "(f $ 0 :: 'a :: field) ≠ 0 ⟹ f dvd g"
  by (rule dvd_trans, subst fps_is_unit_iff) simp_all



instantiation fps :: (field) ring_div
begin

definition fps_mod_def:
  "f mod g = (if g = 0 then f else
     let n = subdegree g; h = fps_shift n g
     in  fps_cutoff n (f * inverse h) * h)"

lemma fps_mod_eq_zero:
  assumes "g ≠ 0" and "subdegree f ≥ subdegree g"
  shows   "f mod g = 0"
  using assms by (cases "f = 0") (auto simp: fps_cutoff_zero_iff fps_mod_def Let_def)

lemma fps_times_divide_eq:
  assumes "g ≠ 0" and "subdegree f ≥ subdegree (g :: 'a fps)"
  shows   "f div g * g = f"
proof (cases "f = 0")
  assume nz: "f ≠ 0"
  def n  "subdegree g"
  def h  "fps_shift n g"
  from assms have [simp]: "h $ 0 ≠ 0" unfolding h_def by (simp add: n_def)

  from assms nz have "f div g * g = fps_shift n (f * inverse h) * g"
    by (simp add: fps_divide_def Let_def h_def n_def)
  also have "... = fps_shift n (f * inverse h) * X^n * h" unfolding h_def n_def
    by (subst subdegree_decompose[of g]) simp
  also have "fps_shift n (f * inverse h) * X^n = f * inverse h"
    by (rule fps_shift_times_X_power) (simp_all add: nz assms n_def)
  also have "... * h = f * (inverse h * h)" by simp
  also have "inverse h * h = 1" by (rule inverse_mult_eq_1) simp
  finally show ?thesis by simp
qed (simp_all add: fps_divide_def Let_def)

lemma
  assumes "g$0 ≠ 0"
  shows   fps_divide_unit: "f div g = f * inverse g" and fps_mod_unit [simp]: "f mod g = 0"
proof -
  from assms have [simp]: "subdegree g = 0" by (simp add: subdegree_eq_0_iff)
  from assms show "f div g = f * inverse g"
    by (auto simp: fps_divide_def Let_def subdegree_eq_0_iff)
  from assms show "f mod g = 0" by (intro fps_mod_eq_zero) auto
qed

context
begin
private lemma fps_divide_cancel_aux1:
  assumes "h$0 ≠ (0 :: 'a :: field)"
  shows   "(h * f) div (h * g) = f div g"
proof (cases "g = 0")
  assume "g ≠ 0"
  from assms have "h ≠ 0" by auto
  note nz [simp] = ‹g ≠ 0› ‹h ≠ 0›
  from assms have [simp]: "subdegree h = 0" by (simp add: subdegree_eq_0_iff)

  have "(h * f) div (h * g) =
          fps_shift (subdegree g) (h * f * inverse (fps_shift (subdegree g) (h*g)))"
    by (simp add: fps_divide_def Let_def)
  also have "h * f * inverse (fps_shift (subdegree g) (h*g)) =
               (inverse h * h) * f * inverse (fps_shift (subdegree g) g)"
    by (subst fps_shift_mult) (simp_all add: algebra_simps fps_inverse_mult)
  also from assms have "inverse h * h = 1" by (rule inverse_mult_eq_1)
  finally show "(h * f) div (h * g) = f div g" by (simp_all add: fps_divide_def Let_def)
qed (simp_all add: fps_divide_def)

private lemma fps_divide_cancel_aux2:
  "(f * X^m) div (g * X^m) = f div (g :: 'a :: field fps)"
proof (cases "g = 0")
  assume [simp]: "g ≠ 0"
  have "(f * X^m) div (g * X^m) =
          fps_shift (subdegree g + m) (f*inverse (fps_shift (subdegree g + m) (g*X^m))*X^m)"
    by (simp add: fps_divide_def Let_def algebra_simps)
  also have "... = f div g"
    by (simp add: fps_shift_times_X_power'' fps_divide_def Let_def)
  finally show ?thesis .
qed (simp_all add: fps_divide_def)

instance proof
  fix f g :: "'a fps"
  def n  "subdegree g"
  def h  "fps_shift n g"

  show "f div g * g + f mod g = f"
  proof (cases "g = 0 ∨ f = 0")
    assume "¬(g = 0 ∨ f = 0)"
    hence nz [simp]: "f ≠ 0" "g ≠ 0" by simp_all
    show ?thesis
    proof (rule disjE[OF le_less_linear])
      assume "subdegree f ≥ subdegree g"
      with nz show ?thesis by (simp add: fps_mod_eq_zero fps_times_divide_eq)
    next
      assume "subdegree f < subdegree g"
      have g_decomp: "g = h * X^n" unfolding h_def n_def by (rule subdegree_decompose)
      have "f div g * g + f mod g =
              fps_shift n (f * inverse h) * g + fps_cutoff n (f * inverse h) * h"
        by (simp add: fps_mod_def fps_divide_def Let_def n_def h_def)
      also have "... = h * (fps_shift n (f * inverse h) * X^n + fps_cutoff n (f * inverse h))"
        by (subst g_decomp) (simp add: algebra_simps)
      also have "... = f * (inverse h * h)"
        by (subst fps_shift_cutoff) simp
      also have "inverse h * h = 1" by (rule inverse_mult_eq_1) (simp add: h_def n_def)
      finally show ?thesis by simp
    qed
  qed (auto simp: fps_mod_def fps_divide_def Let_def)
next

  fix f g h :: "'a fps"
  assume "h ≠ 0"
  show "(h * f) div (h * g) = f div g"
  proof -
    def m  "subdegree h"
    def h'  "fps_shift m h"
    have h_decomp: "h = h' * X ^ m" unfolding h'_def m_def by (rule subdegree_decompose)
    from ‹h ≠ 0› have [simp]: "h'$0 ≠ 0" by (simp add: h'_def m_def)
    have "(h * f) div (h * g) = (h' * f * X^m) div (h' * g * X^m)"
      by (simp add: h_decomp algebra_simps)
    also have "... = f div g" by (simp add: fps_divide_cancel_aux1 fps_divide_cancel_aux2)
    finally show ?thesis .
  qed

next
  fix f g h :: "'a fps"
  assume [simp]: "h ≠ 0"
  def n  "subdegree h"
  def h'  "fps_shift n h"
  note dfs = n_def h'_def
  have "(f + g * h) div h = fps_shift n (f * inverse h') + fps_shift n (g * (h * inverse h'))"
    by (simp add: fps_divide_def Let_def dfs[symmetric] algebra_simps fps_shift_add)
  also have "h * inverse h' = (inverse h' * h') * X^n"
    by (subst subdegree_decompose) (simp_all add: dfs)
  also have "... = X^n" by (subst inverse_mult_eq_1) (simp_all add: dfs)
  also have "fps_shift n (g * X^n) = g" by simp
  also have "fps_shift n (f * inverse h') = f div h"
    by (simp add: fps_divide_def Let_def dfs)
  finally show "(f + g * h) div h = g + f div h" by simp
qed (auto simp: fps_divide_def fps_mod_def Let_def)

end
end

lemma subdegree_mod:
  assumes "f ≠ 0" "subdegree f < subdegree g"
  shows   "subdegree (f mod g) = subdegree f"
proof (cases "f div g * g = 0")
  assume "f div g * g ≠ 0"
  hence [simp]: "f div g ≠ 0" "g ≠ 0" by auto
  from mod_div_equality[of f g] have "f mod g = f - f div g * g" by (simp add: algebra_simps)
  also from assms have "subdegree ... = subdegree f"
    by (intro subdegree_diff_eq1) simp_all
  finally show ?thesis .
next
  assume zero: "f div g * g = 0"
  from mod_div_equality[of f g] have "f mod g = f - f div g * g" by (simp add: algebra_simps)
  also note zero
  finally show ?thesis by simp
qed

lemma fps_divide_nth_0 [simp]: "g $ 0 ≠ 0 ⟹ (f div g) $ 0 = f $ 0 / (g $ 0 :: _ :: field)"
  by (simp add: fps_divide_unit divide_inverse)


lemma dvd_imp_subdegree_le:
  "(f :: 'a :: idom fps) dvd g ⟹ g ≠ 0 ⟹ subdegree f ≤ subdegree g"
  by (auto elim: dvdE)

lemma fps_dvd_iff:
  assumes "(f :: 'a :: field fps) ≠ 0" "g ≠ 0"
  shows   "f dvd g ⟷ subdegree f ≤ subdegree g"
proof
  assume "subdegree f ≤ subdegree g"
  with assms have "g mod f = 0"
    by (simp add: fps_mod_def Let_def fps_cutoff_zero_iff)
  thus "f dvd g" by (simp add: dvd_eq_mod_eq_0)
qed (simp add: assms dvd_imp_subdegree_le)

lemma fps_const_inverse: "inverse (fps_const (a::'a::field)) = fps_const (inverse a)"
  by (cases "a ≠ 0", rule fps_inverse_unique) (auto simp: fps_eq_iff)

lemma fps_const_divide: "fps_const (x :: _ :: field) / fps_const y = fps_const (x / y)"
  by (cases "y = 0") (simp_all add: fps_divide_unit fps_const_inverse divide_inverse)

lemma inverse_fps_numeral:
  "inverse (numeral n :: ('a :: field_char_0) fps) = fps_const (inverse (numeral n))"
  by (intro fps_inverse_unique fps_ext) (simp_all add: fps_numeral_nth)




instantiation fps :: (field) normalization_semidom
begin

definition fps_unit_factor_def [simp]:
  "unit_factor f = fps_shift (subdegree f) f"

definition fps_normalize_def [simp]:
  "normalize f = (if f = 0 then 0 else X ^ subdegree f)"

instance proof
  fix f :: "'a fps"
  show "unit_factor f * normalize f = f"
    by (simp add: fps_shift_times_X_power)
next
  fix f g :: "'a fps"
  show "unit_factor (f * g) = unit_factor f * unit_factor g"
  proof (cases "f = 0 ∨ g = 0")
    assume "¬(f = 0 ∨ g = 0)"
    thus "unit_factor (f * g) = unit_factor f * unit_factor g"
    unfolding fps_unit_factor_def
      by (auto simp: fps_shift_fps_shift fps_shift_mult fps_shift_mult_right)
  qed auto
qed auto

end

instance fps :: (field) algebraic_semidom ..


subsection ‹Formal power series form a Euclidean ring›

instantiation fps :: (field) euclidean_ring
begin

definition fps_euclidean_size_def:
  "euclidean_size f = (if f = 0 then 0 else 2 ^ subdegree f)"

instance proof
  fix f g :: "'a fps" assume [simp]: "g ≠ 0"
  show "euclidean_size f ≤ euclidean_size (f * g)"
    by (cases "f = 0") (auto simp: fps_euclidean_size_def)
  show "euclidean_size (f mod g) < euclidean_size g"
    apply (cases "f = 0", simp add: fps_euclidean_size_def)
    apply (rule disjE[OF le_less_linear[of "subdegree g" "subdegree f"]])
    apply (simp_all add: fps_mod_eq_zero fps_euclidean_size_def subdegree_mod)
    done
qed (simp_all add: fps_euclidean_size_def)

end

instantiation fps :: (field) euclidean_ring_gcd
begin
definition fps_gcd_def: "(gcd :: 'a fps ⇒ _) = gcd_eucl"
definition fps_lcm_def: "(lcm :: 'a fps ⇒ _) = lcm_eucl"
definition fps_Gcd_def: "(Gcd :: 'a fps set ⇒ _) = Gcd_eucl"
definition fps_Lcm_def: "(Lcm :: 'a fps set ⇒ _) = Lcm_eucl"
instance by standard (simp_all add: fps_gcd_def fps_lcm_def fps_Gcd_def fps_Lcm_def)
end

lemma fps_gcd:
  assumes [simp]: "f ≠ 0" "g ≠ 0"
  shows   "gcd f g = X ^ min (subdegree f) (subdegree g)"
proof -
  let ?m = "min (subdegree f) (subdegree g)"
  show "gcd f g = X ^ ?m"
  proof (rule sym, rule gcdI)
    fix d assume "d dvd f" "d dvd g"
    thus "d dvd X ^ ?m" by (cases "d = 0") (auto simp: fps_dvd_iff)
  qed (simp_all add: fps_dvd_iff)
qed

lemma fps_gcd_altdef: "gcd (f :: 'a :: field fps) g =
  (if f = 0 ∧ g = 0 then 0 else
   if f = 0 then X ^ subdegree g else
   if g = 0 then X ^ subdegree f else
     X ^ min (subdegree f) (subdegree g))"
  by (simp add: fps_gcd)

lemma fps_lcm:
  assumes [simp]: "f ≠ 0" "g ≠ 0"
  shows   "lcm f g = X ^ max (subdegree f) (subdegree g)"
proof -
  let ?m = "max (subdegree f) (subdegree g)"
  show "lcm f g = X ^ ?m"
  proof (rule sym, rule lcmI)
    fix d assume "f dvd d" "g dvd d"
    thus "X ^ ?m dvd d" by (cases "d = 0") (auto simp: fps_dvd_iff)
  qed (simp_all add: fps_dvd_iff)
qed

lemma fps_lcm_altdef: "lcm (f :: 'a :: field fps) g =
  (if f = 0 ∨ g = 0 then 0 else X ^ max (subdegree f) (subdegree g))"
  by (simp add: fps_lcm)

lemma fps_Gcd:
  assumes "A - {0} ≠ {}"
  shows   "Gcd A = X ^ (INF f:A-{0}. subdegree f)"
proof (rule sym, rule GcdI)
  fix f assume "f ∈ A"
  thus "X ^ (INF f:A - {0}. subdegree f) dvd f"
    by (cases "f = 0") (auto simp: fps_dvd_iff intro!: cINF_lower)
next
  fix d assume d: "⋀f. f ∈ A ⟹ d dvd f"
  from assms obtain f where "f ∈ A - {0}" by auto
  with d[of f] have [simp]: "d ≠ 0" by auto
  from d assms have "subdegree d ≤ (INF f:A-{0}. subdegree f)"
    by (intro cINF_greatest) (auto simp: fps_dvd_iff[symmetric])
  with d assms show "d dvd X ^ (INF f:A-{0}. subdegree f)" by (simp add: fps_dvd_iff)
qed simp_all

lemma fps_Gcd_altdef: "Gcd (A :: 'a :: field fps set) =
  (if A ⊆ {0} then 0 else X ^ (INF f:A-{0}. subdegree f))"
  using fps_Gcd by auto

lemma fps_Lcm:
  assumes "A ≠ {}" "0 ∉ A" "bdd_above (subdegree`A)"
  shows   "Lcm A = X ^ (SUP f:A. subdegree f)"
proof (rule sym, rule LcmI)
  fix f assume "f ∈ A"
  moreover from assms(3) have "bdd_above (subdegree ` A)" by auto
  ultimately show "f dvd X ^ (SUP f:A. subdegree f)" using assms(2)
    by (cases "f = 0") (auto simp: fps_dvd_iff intro!: cSUP_upper)
next
  fix d assume d: "⋀f. f ∈ A ⟹ f dvd d"
  from assms obtain f where f: "f ∈ A" "f ≠ 0" by auto
  show "X ^ (SUP f:A. subdegree f) dvd d"
  proof (cases "d = 0")
    assume "d ≠ 0"
    moreover from d have "⋀f. f ∈ A ⟹ f ≠ 0 ⟹ f dvd d" by blast
    ultimately have "subdegree d ≥ (SUP f:A. subdegree f)" using assms
      by (intro cSUP_least) (auto simp: fps_dvd_iff)
    with ‹d ≠ 0› show ?thesis by (simp add: fps_dvd_iff)
  qed simp_all
qed simp_all

lemma fps_Lcm_altdef:
  "Lcm (A :: 'a :: field fps set) =
     (if 0 ∈ A ∨ ¬bdd_above (subdegree`A) then 0 else
      if A = {} then 1 else X ^ (SUP f:A. subdegree f))"
proof (cases "bdd_above (subdegree`A)")
  assume unbounded: "¬bdd_above (subdegree`A)"
  have "Lcm A = 0"
  proof (rule ccontr)
    assume "Lcm A ≠ 0"
    from unbounded obtain f where f: "f ∈ A" "subdegree (Lcm A) < subdegree f"
      unfolding bdd_above_def by (auto simp: not_le)
    moreover from this and ‹Lcm A ≠ 0› have "subdegree f ≤ subdegree (Lcm A)"
      by (intro dvd_imp_subdegree_le dvd_Lcm) simp_all
    ultimately show False by simp
  qed
  with unbounded show ?thesis by simp
qed (simp_all add: fps_Lcm Lcm_eq_0_I)



subsection ‹Formal Derivatives, and the MacLaurin theorem around 0›

definition "fps_deriv f = Abs_fps (λn. of_nat (n + 1) * f $ (n + 1))"

lemma fps_deriv_nth[simp]: "fps_deriv f $ n = of_nat (n +1) * f $ (n + 1)"
  by (simp add: fps_deriv_def)

lemma fps_deriv_linear[simp]:
  "fps_deriv (fps_const (a::'a::comm_semiring_1) * f + fps_const b * g) =
    fps_const a * fps_deriv f + fps_const b * fps_deriv g"
  unfolding fps_eq_iff fps_add_nth  fps_const_mult_left fps_deriv_nth by (simp add: field_simps)

lemma fps_deriv_mult[simp]:
  fixes f :: "'a::comm_ring_1 fps"
  shows "fps_deriv (f * g) = f * fps_deriv g + fps_deriv f * g"
proof -
  let ?D = "fps_deriv"
  have "(f * ?D g + ?D f * g) $ n = ?D (f*g) $ n" for n
  proof -
    let ?Zn = "{0 ..n}"
    let ?Zn1 = "{0 .. n + 1}"
    let ?g = "λi. of_nat (i+1) * g $ (i+1) * f $ (n - i) +
        of_nat (i+1)* f $ (i+1) * g $ (n - i)"
    let ?h = "λi. of_nat i * g $ i * f $ ((n+1) - i) +
        of_nat i* f $ i * g $ ((n + 1) - i)"
    have s0: "setsum (λi. of_nat i * f $ i * g $ (n + 1 - i)) ?Zn1 =
      setsum (λi. of_nat (n + 1 - i) * f $ (n + 1 - i) * g $ i) ?Zn1"
       by (rule setsum.reindex_bij_witness[where i="op - (n + 1)" and j="op - (n + 1)"]) auto
    have s1: "setsum (λi. f $ i * g $ (n + 1 - i)) ?Zn1 =
      setsum (λi. f $ (n + 1 - i) * g $ i) ?Zn1"
       by (rule setsum.reindex_bij_witness[where i="op - (n + 1)" and j="op - (n + 1)"]) auto
    have "(f * ?D g + ?D f * g)$n = (?D g * f + ?D f * g)$n"
      by (simp only: mult.commute)
    also have "… = (∑i = 0..n. ?g i)"
      by (simp add: fps_mult_nth setsum.distrib[symmetric])
    also have "… = setsum ?h {0..n+1}"
      by (rule setsum.reindex_bij_witness_not_neutral
            [where S'="{}" and T'="{0}" and j="Suc" and i="λi. i - 1"]) auto
    also have "… = (fps_deriv (f * g)) $ n"
      apply (simp only: fps_deriv_nth fps_mult_nth setsum.distrib)
      unfolding s0 s1
      unfolding setsum.distrib[symmetric] setsum_right_distrib
      apply (rule setsum.cong)
      apply (auto simp add: of_nat_diff field_simps)
      done
    finally show ?thesis .
  qed
  then show ?thesis
    unfolding fps_eq_iff by auto
qed

lemma fps_deriv_X[simp]: "fps_deriv X = 1"
  by (simp add: fps_deriv_def X_def fps_eq_iff)

lemma fps_deriv_neg[simp]:
  "fps_deriv (- (f:: 'a::comm_ring_1 fps)) = - (fps_deriv f)"
  by (simp add: fps_eq_iff fps_deriv_def)

lemma fps_deriv_add[simp]:
  "fps_deriv ((f:: 'a::comm_ring_1 fps) + g) = fps_deriv f + fps_deriv g"
  using fps_deriv_linear[of 1 f 1 g] by simp

lemma fps_deriv_sub[simp]:
  "fps_deriv ((f:: 'a::comm_ring_1 fps) - g) = fps_deriv f - fps_deriv g"
  using fps_deriv_add [of f "- g"] by simp

lemma fps_deriv_const[simp]: "fps_deriv (fps_const c) = 0"
  by (simp add: fps_ext fps_deriv_def fps_const_def)

lemma fps_deriv_mult_const_left[simp]:
  "fps_deriv (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_deriv f"
  by simp

lemma fps_deriv_0[simp]: "fps_deriv 0 = 0"
  by (simp add: fps_deriv_def fps_eq_iff)

lemma fps_deriv_1[simp]: "fps_deriv 1 = 0"
  by (simp add: fps_deriv_def fps_eq_iff )

lemma fps_deriv_mult_const_right[simp]:
  "fps_deriv (f * fps_const (c::'a::comm_ring_1)) = fps_deriv f * fps_const c"
  by simp

lemma fps_deriv_setsum:
  "fps_deriv (setsum f S) = setsum (λi. fps_deriv (f i :: 'a::comm_ring_1 fps)) S"
proof (cases "finite S")
  case False
  then show ?thesis by simp
next
  case True
  show ?thesis by (induct rule: finite_induct [OF True]) simp_all
qed

lemma fps_deriv_eq_0_iff [simp]:
  "fps_deriv f = 0 ⟷ f = fps_const (f$0 :: 'a::{idom,semiring_char_0})"
  (is "?lhs ⟷ ?rhs")
proof
  show ?lhs if ?rhs
  proof -
    from that have "fps_deriv f = fps_deriv (fps_const (f$0))"
      by simp
    then show ?thesis
      by simp
  qed
  show ?rhs if ?lhs
  proof -
    from that have "∀n. (fps_deriv f)$n = 0"
      by simp
    then have "∀n. f$(n+1) = 0"
      by (simp del: of_nat_Suc of_nat_add One_nat_def)
    then show ?thesis
      apply (clarsimp simp add: fps_eq_iff fps_const_def)
      apply (erule_tac x="n - 1" in allE)
      apply simp
      done
  qed
qed

lemma fps_deriv_eq_iff:
  fixes f :: "'a::{idom,semiring_char_0} fps"
  shows "fps_deriv f = fps_deriv g ⟷ (f = fps_const(f$0 - g$0) + g)"
proof -
  have "fps_deriv f = fps_deriv g ⟷ fps_deriv (f - g) = 0"
    by simp
  also have "… ⟷ f - g = fps_const ((f - g) $ 0)"
    unfolding fps_deriv_eq_0_iff ..
  finally show ?thesis
    by (simp add: field_simps)
qed

lemma fps_deriv_eq_iff_ex:
  "(fps_deriv f = fps_deriv g) ⟷ (∃c::'a::{idom,semiring_char_0}. f = fps_const c + g)"
  by (auto simp: fps_deriv_eq_iff)


fun fps_nth_deriv :: "nat ⇒ 'a::semiring_1 fps ⇒ 'a fps"
where
  "fps_nth_deriv 0 f = f"
| "fps_nth_deriv (Suc n) f = fps_nth_deriv n (fps_deriv f)"

lemma fps_nth_deriv_commute: "fps_nth_deriv (Suc n) f = fps_deriv (fps_nth_deriv n f)"
  by (induct n arbitrary: f) auto

lemma fps_nth_deriv_linear[simp]:
  "fps_nth_deriv n (fps_const (a::'a::comm_semiring_1) * f + fps_const b * g) =
    fps_const a * fps_nth_deriv n f + fps_const b * fps_nth_deriv n g"
  by (induct n arbitrary: f g) (auto simp add: fps_nth_deriv_commute)

lemma fps_nth_deriv_neg[simp]:
  "fps_nth_deriv n (- (f :: 'a::comm_ring_1 fps)) = - (fps_nth_deriv n f)"
  by (induct n arbitrary: f) simp_all

lemma fps_nth_deriv_add[simp]:
  "fps_nth_deriv n ((f :: 'a::comm_ring_1 fps) + g) = fps_nth_deriv n f + fps_nth_deriv n g"
  using fps_nth_deriv_linear[of n 1 f 1 g] by simp

lemma fps_nth_deriv_sub[simp]:
  "fps_nth_deriv n ((f :: 'a::comm_ring_1 fps) - g) = fps_nth_deriv n f - fps_nth_deriv n g"
  using fps_nth_deriv_add [of n f "- g"] by simp

lemma fps_nth_deriv_0[simp]: "fps_nth_deriv n 0 = 0"
  by (induct n) simp_all

lemma fps_nth_deriv_1[simp]: "fps_nth_deriv n 1 = (if n = 0 then 1 else 0)"
  by (induct n) simp_all

lemma fps_nth_deriv_const[simp]:
  "fps_nth_deriv n (fps_const c) = (if n = 0 then fps_const c else 0)"
  by (cases n) simp_all

lemma fps_nth_deriv_mult_const_left[simp]:
  "fps_nth_deriv n (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_nth_deriv n f"
  using fps_nth_deriv_linear[of n "c" f 0 0 ] by simp

lemma fps_nth_deriv_mult_const_right[simp]:
  "fps_nth_deriv n (f * fps_const (c::'a::comm_ring_1)) = fps_nth_deriv n f * fps_const c"
  using fps_nth_deriv_linear[of n "c" f 0 0] by (simp add: mult.commute)

lemma fps_nth_deriv_setsum:
  "fps_nth_deriv n (setsum f S) = setsum (λi. fps_nth_deriv n (f i :: 'a::comm_ring_1 fps)) S"
proof (cases "finite S")
  case True
  show ?thesis by (induct rule: finite_induct [OF True]) simp_all
next
  case False
  then show ?thesis by simp
qed

lemma fps_deriv_maclauren_0:
  "(fps_nth_deriv k (f :: 'a::comm_semiring_1 fps)) $ 0 = of_nat (fact k) * f $ k"
  by (induct k arbitrary: f) (auto simp add: field_simps of_nat_mult)


subsection ‹Powers›

lemma fps_power_zeroth_eq_one: "a$0 =1 ⟹ a^n $ 0 = (1::'a::semiring_1)"
  by (induct n) (auto simp add: expand_fps_eq fps_mult_nth)

lemma fps_power_first_eq: "(a :: 'a::comm_ring_1 fps) $ 0 =1 ⟹ a^n $ 1 = of_nat n * a$1"
proof (induct n)
  case 0
  then show ?case by simp
next
  case (Suc n)
  show ?case unfolding power_Suc fps_mult_nth
    using Suc.hyps[OF ‹a$0 = 1›] ‹a$0 = 1› fps_power_zeroth_eq_one[OF ‹a$0=1›]
    by (simp add: field_simps)
qed

lemma startsby_one_power:"a $ 0 = (1::'a::comm_ring_1) ⟹ a^n $ 0 = 1"
  by (induct n) (auto simp add: fps_mult_nth)

lemma startsby_zero_power:"a $0 = (0::'a::comm_ring_1) ⟹ n > 0 ⟹ a^n $0 = 0"
  by (induct n) (auto simp add: fps_mult_nth)

lemma startsby_power:"a $0 = (v::'a::comm_ring_1) ⟹ a^n $0 = v^n"
  by (induct n) (auto simp add: fps_mult_nth)

lemma startsby_zero_power_iff[simp]: "a^n $0 = (0::'a::idom) ⟷ n ≠ 0 ∧ a$0 = 0"
  apply (rule iffI)
  apply (induct n)
  apply (auto simp add: fps_mult_nth)
  apply (rule startsby_zero_power, simp_all)
  done

lemma startsby_zero_power_prefix:
  assumes a0: "a $ 0 = (0::'a::idom)"
  shows "∀n < k. a ^ k $ n = 0"
  using a0
proof (induct k rule: nat_less_induct)
  fix k
  assume H: "∀m<k. a $0 =  0 ⟶ (∀n<m. a ^ m $ n = 0)" and a0: "a $ 0 = 0"
  show "∀m<k. a ^ k $ m = 0"
  proof (cases k)
    case 0
    then show ?thesis by simp
  next
    case (Suc l)
    have "a^k $ m = 0" if mk: "m < k" for m
    proof (cases "m = 0")
      case True
      then show ?thesis
        using startsby_zero_power[of a k] Suc a0 by simp
    next
      case False
      have "a ^k $ m = (a^l * a) $m"
        by (simp add: Suc mult.commute)
      also have "… = (∑i = 0..m. a ^ l $ i * a $ (m - i))"
        by (simp add: fps_mult_nth)
      also have "… = 0"
        apply (rule setsum.neutral)
        apply auto
        apply (case_tac "x = m")
        using a0 apply simp
        apply (rule H[rule_format])
        using a0 Suc mk apply auto
        done
      finally show ?thesis .
    qed
    then show ?thesis by blast
  qed
qed

lemma startsby_zero_setsum_depends:
  assumes a0: "a $0 = (0::'a::idom)"
    and kn: "n ≥ k"
  shows "setsum (λi. (a ^ i)$k) {0 .. n} = setsum (λi. (a ^ i)$k) {0 .. k}"
  apply (rule setsum.mono_neutral_right)
  using kn
  apply auto
  apply (rule startsby_zero_power_prefix[rule_format, OF a0])
  apply arith
  done

lemma startsby_zero_power_nth_same:
  assumes a0: "a$0 = (0::'a::idom)"
  shows "a^n $ n = (a$1) ^ n"
proof (induct n)
  case 0
  then show ?case by simp
next
  case (Suc n)
  have "a ^ Suc n $ (Suc n) = (a^n * a)$(Suc n)"
    by (simp add: field_simps)
  also have "… = setsum (λi. a^n$i * a $ (Suc n - i)) {0.. Suc n}"
    by (simp add: fps_mult_nth)
  also have "… = setsum (λi. a^n$i * a $ (Suc n - i)) {n .. Suc n}"
    apply (rule setsum.mono_neutral_right)
    apply simp
    apply clarsimp
    apply clarsimp
    apply (rule startsby_zero_power_prefix[rule_format, OF a0])
    apply arith
    done
  also have "… = a^n $ n * a$1"
    using a0 by simp
  finally show ?case
    using Suc.hyps by simp
qed

lemma fps_inverse_power:
  fixes a :: "'a::field fps"
  shows "inverse (a^n) = inverse a ^ n"
  by (induction n) (simp_all add: fps_inverse_mult)

lemma fps_deriv_power:
  "fps_deriv (a ^ n) = fps_const (of_nat n :: 'a::comm_ring_1) * fps_deriv a * a ^ (n - 1)"
  apply (induct n)
  apply (auto simp add: field_simps fps_const_add[symmetric] simp del: fps_const_add)
  apply (case_tac n)
  apply (auto simp add: field_simps)
  done

lemma fps_inverse_deriv:
  fixes a :: "'a::field fps"
  assumes a0: "a$0 ≠ 0"
  shows "fps_deriv (inverse a) = - fps_deriv a * (inverse a)2"
proof -
  from inverse_mult_eq_1[OF a0]
  have "fps_deriv (inverse a * a) = 0" by simp
  then have "inverse a * fps_deriv a + fps_deriv (inverse a) * a = 0"
    by simp
  then have "inverse a * (inverse a * fps_deriv a + fps_deriv (inverse a) * a) = 0"
    by simp
  with inverse_mult_eq_1[OF a0]
  have "(inverse a)2 * fps_deriv a + fps_deriv (inverse a) = 0"
    unfolding power2_eq_square
    apply (simp add: field_simps)
    apply (simp add: mult.assoc[symmetric])
    done
  then have "(inverse a)2 * fps_deriv a + fps_deriv (inverse a) - fps_deriv a * (inverse a)2 =
      0 - fps_deriv a * (inverse a)2"
    by simp
  then show "fps_deriv (inverse a) = - fps_deriv a * (inverse a)2"
    by (simp add: field_simps)
qed

lemma fps_inverse_deriv':
  fixes a :: "'a::field fps"
  assumes a0: "a $ 0 ≠ 0"
  shows "fps_deriv (inverse a) = - fps_deriv a / a2"
  using fps_inverse_deriv[OF a0] a0
  by (simp add: fps_divide_unit power2_eq_square fps_inverse_mult)

lemma inverse_mult_eq_1':
  assumes f0: "f$0 ≠ (0::'a::field)"
  shows "f * inverse f = 1"
  by (metis mult.commute inverse_mult_eq_1 f0)

(* FIXME: The last part of this proof should go through by simp once we have a proper
   theorem collection for simplifying division on rings *)
lemma fps_divide_deriv:
  assumes "b dvd (a :: 'a :: field fps)"
  shows   "fps_deriv (a / b) = (fps_deriv a * b - a * fps_deriv b) / b^2"
proof -
  have eq_divide_imp: "c ≠ 0 ⟹ a * c = b ⟹ a = b div c" for a b c :: "'a :: field fps"
    by (drule sym) (simp add: mult.assoc)
  from assms have "a = a / b * b" by simp
  also have "fps_deriv (a / b * b) = fps_deriv (a / b) * b + a / b * fps_deriv b" by simp
  finally have "fps_deriv (a / b) * b^2 = fps_deriv a * b - a * fps_deriv b" using assms
    by (simp add: power2_eq_square algebra_simps)
  thus ?thesis by (cases "b = 0") (auto simp: eq_divide_imp)
qed

lemma fps_inverse_gp': "inverse (Abs_fps (λn. 1::'a::field)) = 1 - X"
  by (simp add: fps_inverse_gp fps_eq_iff X_def)

lemma fps_nth_deriv_X[simp]: "fps_nth_deriv n X = (if n = 0 then X else if n=1 then 1 else 0)"
  by (cases n) simp_all

lemma fps_inverse_X_plus1: "inverse (1 + X) = Abs_fps (λn. (- (1::'a::field)) ^ n)"
  (is "_ = ?r")
proof -
  have eq: "(1 + X) * ?r = 1"
    unfolding minus_one_power_iff
    by (auto simp add: field_simps fps_eq_iff)
  show ?thesis
    by (auto simp add: eq intro: fps_inverse_unique)
qed


subsection ‹Integration›

definition fps_integral :: "'a::field_char_0 fps ⇒ 'a ⇒ 'a fps"
  where "fps_integral a a0 = Abs_fps (λn. if n = 0 then a0 else (a$(n - 1) / of_nat n))"

lemma fps_deriv_fps_integral: "fps_deriv (fps_integral a a0) = a"
  unfolding fps_integral_def fps_deriv_def
  by (simp add: fps_eq_iff del: of_nat_Suc)

lemma fps_integral_linear:
  "fps_integral (fps_const a * f + fps_const b * g) (a*a0 + b*b0) =
    fps_const a * fps_integral f a0 + fps_const b * fps_integral g b0"
  (is "?l = ?r")
proof -
  have "fps_deriv ?l = fps_deriv ?r"
    by (simp add: fps_deriv_fps_integral)
  moreover have "?l$0 = ?r$0"
    by (simp add: fps_integral_def)
  ultimately show ?thesis
    unfolding fps_deriv_eq_iff by auto
qed


subsection ‹Composition of FPSs›

definition fps_compose :: "'a::semiring_1 fps ⇒ 'a fps ⇒ 'a fps"  (infixl "oo" 55)
  where "a oo b = Abs_fps (λn. setsum (λi. a$i * (b^i$n)) {0..n})"

lemma fps_compose_nth: "(a oo b)$n = setsum (λi. a$i * (b^i$n)) {0..n}"
  by (simp add: fps_compose_def)

lemma fps_compose_nth_0 [simp]: "(f oo g) $ 0 = f $ 0"
  by (simp add: fps_compose_nth)

lemma fps_compose_X[simp]: "a oo X = (a :: 'a::comm_ring_1 fps)"
  by (simp add: fps_ext fps_compose_def mult_delta_right setsum.delta')

lemma fps_const_compose[simp]: "fps_const (a::'a::comm_ring_1) oo b = fps_const a"
  by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum.delta)

lemma numeral_compose[simp]: "(numeral k :: 'a::comm_ring_1 fps) oo b = numeral k"
  unfolding numeral_fps_const by simp

lemma neg_numeral_compose[simp]: "(- numeral k :: 'a::comm_ring_1 fps) oo b = - numeral k"
  unfolding neg_numeral_fps_const by simp

lemma X_fps_compose_startby0[simp]: "a$0 = 0 ⟹ X oo a = (a :: 'a::comm_ring_1 fps)"
  by (simp add: fps_eq_iff fps_compose_def mult_delta_left setsum.delta not_le)


subsection ‹Rules from Herbert Wilf's Generatingfunctionology›

subsubsection ‹Rule 1›
  (* {a_{n+k}}_0^infty Corresponds to (f - setsum (λi. a_i * x^i))/x^h, for h>0*)

lemma fps_power_mult_eq_shift:
  "X^Suc k * Abs_fps (λn. a (n + Suc k)) =
    Abs_fps a - setsum (λi. fps_const (a i :: 'a::comm_ring_1) * X^i) {0 .. k}"
  (is "?lhs = ?rhs")
proof -
  have "?lhs $ n = ?rhs $ n" for n :: nat
  proof -
    have "?lhs $ n = (if n < Suc k then 0 else a n)"
      unfolding X_power_mult_nth by auto
    also have "… = ?rhs $ n"
    proof (induct k)
      case 0
      then show ?case
        by (simp add: fps_setsum_nth)
    next
      case (Suc k)
      have "(Abs_fps a - setsum (λi. fps_const (a i :: 'a) * X^i) {0 .. Suc k})$n =
        (Abs_fps a - setsum (λi. fps_const (a i :: 'a) * X^i) {0 .. k} -
          fps_const (a (Suc k)) * X^ Suc k) $ n"
        by (simp add: field_simps)
      also have "… = (if n < Suc k then 0 else a n) - (fps_const (a (Suc k)) * X^ Suc k)$n"
        using Suc.hyps[symmetric] unfolding fps_sub_nth by simp
      also have "… = (if n < Suc (Suc k) then 0 else a n)"
        unfolding X_power_mult_right_nth
        apply (auto simp add: not_less fps_const_def)
        apply (rule cong[of a a, OF refl])
        apply arith
        done
      finally show ?case
        by simp
    qed
    finally show ?thesis .
  qed
  then show ?thesis
    by (simp add: fps_eq_iff)
qed


subsubsection ‹Rule 2›

  (* We can not reach the form of Wilf, but still near to it using rewrite rules*)
  (* If f reprents {a_n} and P is a polynomial, then
        P(xD) f represents {P(n) a_n}*)

definition "XD = op * X ∘ fps_deriv"

lemma XD_add[simp]:"XD (a + b) = XD a + XD (b :: 'a::comm_ring_1 fps)"
  by (simp add: XD_def field_simps)

lemma XD_mult_const[simp]:"XD (fps_const (c::'a::comm_ring_1) * a) = fps_const c * XD a"
  by (simp add: XD_def field_simps)

lemma XD_linear[simp]: "XD (fps_const c * a + fps_const d * b) =
    fps_const c * XD a + fps_const d * XD (b :: 'a::comm_ring_1 fps)"
  by simp

lemma XDN_linear:
  "(XD ^^ n) (fps_const c * a + fps_const d * b) =
    fps_const c * (XD ^^ n) a + fps_const d * (XD ^^ n) (b :: 'a::comm_ring_1 fps)"
  by (induct n) simp_all

lemma fps_mult_X_deriv_shift: "X* fps_deriv a = Abs_fps (λn. of_nat n* a$n)"
  by (simp add: fps_eq_iff)

lemma fps_mult_XD_shift:
  "(XD ^^ k) (a :: 'a::comm_ring_1 fps) = Abs_fps (λn. (of_nat n ^ k) * a$n)"
  by (induct k arbitrary: a) (simp_all add: XD_def fps_eq_iff field_simps del: One_nat_def)


subsubsection ‹Rule 3›

text ‹Rule 3 is trivial and is given by ‹fps_times_def›.›


subsubsection ‹Rule 5 --- summation and "division" by (1 - X)›

lemma fps_divide_X_minus1_setsum_lemma:
  "a = ((1::'a::comm_ring_1 fps) - X) * Abs_fps (λn. setsum (λi. a $ i) {0..n})"
proof -
  let ?sa = "Abs_fps (λn. setsum (λi. a $ i) {0..n})"
  have th0: "⋀i. (1 - (X::'a fps)) $ i = (if i = 0 then 1 else if i = 1 then - 1 else 0)"
    by simp
  have "a$n = ((1 - X) * ?sa) $ n" for n
  proof (cases "n = 0")
    case True
    then show ?thesis
      by (simp add: fps_mult_nth)
  next
    case False
    then have u: "{0} ∪ ({1} ∪ {2..n}) = {0..n}" "{1} ∪ {2..n} = {1..n}"
      "{0..n - 1} ∪ {n} = {0..n}"
      by (auto simp: set_eq_iff)
    have d: "{0} ∩ ({1} ∪ {2..n}) = {}" "{1} ∩ {2..n} = {}" "{0..n - 1} ∩ {n} = {}"
      using False by simp_all
    have f: "finite {0}" "finite {1}" "finite {2 .. n}"
      "finite {0 .. n - 1}" "finite {n}" by simp_all
    have "((1 - X) * ?sa) $ n = setsum (λi. (1 - X)$ i * ?sa $ (n - i)) {0 .. n}"
      by (simp add: fps_mult_nth)
    also have "… = a$n"
      unfolding th0
      unfolding setsum.union_disjoint[OF f(1) finite_UnI[OF f(2,3)] d(1), unfolded u(1)]
      unfolding setsum.union_disjoint[OF f(2) f(3) d(2)]
      apply (simp)
      unfolding setsum.union_disjoint[OF f(4,5) d(3), unfolded u(3)]
      apply simp
      done
    finally show ?thesis
      by simp
  qed
  then show ?thesis
    unfolding fps_eq_iff by blast
qed

lemma fps_divide_X_minus1_setsum:
  "a /((1::'a::field fps) - X) = Abs_fps (λn. setsum (λi. a $ i) {0..n})"
proof -
  let ?X = "1 - (X::'a fps)"
  have th0: "?X $ 0 ≠ 0"
    by simp
  have "a /?X = ?X *  Abs_fps (λn::nat. setsum (op $ a) {0..n}) * inverse ?X"
    using fps_divide_X_minus1_setsum_lemma[of a, symmetric] th0
    by (simp add: fps_divide_def mult.assoc)
  also have "… = (inverse ?X * ?X) * Abs_fps (λn::nat. setsum (op $ a) {0..n}) "
    by (simp add: ac_simps)
  finally show ?thesis
    by (simp add: inverse_mult_eq_1[OF th0])
qed


subsubsection ‹Rule 4 in its more general form: generalizes Rule 3 for an arbitrary
  finite product of FPS, also the relvant instance of powers of a FPS›

definition "natpermute n k = {l :: nat list. length l = k ∧ listsum l = n}"

lemma natlist_trivial_1: "natpermute n 1 = {[n]}"
  apply (auto simp add: natpermute_def)
  apply (case_tac x)
  apply auto
  done

lemma append_natpermute_less_eq:
  assumes "xs @ ys ∈ natpermute n k"
  shows "listsum xs ≤ n"
    and "listsum ys ≤ n"
proof -
  from assms have "listsum (xs @ ys) = n"
    by (simp add: natpermute_def)
  then have "listsum xs + listsum ys = n"
    by simp
  then show "listsum xs ≤ n" and "listsum ys ≤ n"
    by simp_all
qed

lemma natpermute_split:
  assumes "h ≤ k"
  shows "natpermute n k =
    (⋃m ∈{0..n}. {l1 @ l2 |l1 l2. l1 ∈ natpermute m h ∧ l2 ∈ natpermute (n - m) (k - h)})"
  (is "?L = ?R" is "_ = (⋃m ∈{0..n}. ?S m)")
proof
  show "?R ⊆ ?L"
  proof
    fix l
    assume l: "l ∈ ?R"
    from l obtain m xs ys where h: "m ∈ {0..n}"
      and xs: "xs ∈ natpermute m h"
      and ys: "ys ∈ natpermute (n - m) (k - h)"
      and leq: "l = xs@ys" by blast
    from xs have xs': "listsum xs = m"
      by (simp add: natpermute_def)
    from ys have ys': "listsum ys = n - m"
      by (simp add: natpermute_def)
    show "l ∈ ?L" using leq xs ys h
      apply (clarsimp simp add: natpermute_def)
      unfolding xs' ys'
      using assms xs ys
      unfolding natpermute_def
      apply simp
      done
  qed
  show "?L ⊆ ?R"
  proof
    fix l
    assume l: "l ∈ natpermute n k"
    let ?xs = "take h l"
    let ?ys = "drop h l"
    let ?m = "listsum ?xs"
    from l have ls: "listsum (?xs @ ?ys) = n"
      by (simp add: natpermute_def)
    have xs: "?xs ∈ natpermute ?m h" using l assms
      by (simp add: natpermute_def)
    have l_take_drop: "listsum l = listsum (take h l @ drop h l)"
      by simp
    then have ys: "?ys ∈ natpermute (n - ?m) (k - h)"
      using l assms ls by (auto simp add: natpermute_def simp del: append_take_drop_id)
    from ls have m: "?m ∈ {0..n}"
      by (simp add: l_take_drop del: append_take_drop_id)
    from xs ys ls show "l ∈ ?R"
      apply auto
      apply (rule bexI [where x = "?m"])
      apply (rule exI [where x = "?xs"])
      apply (rule exI [where x = "?ys"])
      using ls l
      apply (auto simp add: natpermute_def l_take_drop simp del: append_take_drop_id)
      apply simp
      done
  qed
qed

lemma natpermute_0: "natpermute n 0 = (if n = 0 then {[]} else {})"
  by (auto simp add: natpermute_def)

lemma natpermute_0'[simp]: "natpermute 0 k = (if k = 0 then {[]} else {replicate k 0})"
  apply (auto simp add: set_replicate_conv_if natpermute_def)
  apply (rule nth_equalityI)
  apply simp_all
  done

lemma natpermute_finite: "finite (natpermute n k)"
proof (induct k arbitrary: n)
  case 0
  then show ?case
    apply (subst natpermute_split[of 0 0, simplified])
    apply (simp add: natpermute_0)
    done
next
  case (Suc k)
  then show ?case unfolding natpermute_split [of k "Suc k", simplified]
    apply -
    apply (rule finite_UN_I)
    apply simp
    unfolding One_nat_def[symmetric] natlist_trivial_1
    apply simp
    done
qed

lemma natpermute_contain_maximal:
  "{xs ∈ natpermute n (k + 1). n ∈ set xs} = (⋃i∈{0 .. k}. {(replicate (k + 1) 0) [i:=n]})"
  (is "?A = ?B")
proof
  show "?A ⊆ ?B"
  proof
    fix xs
    assume "xs ∈ ?A"
    then have H: "xs ∈ natpermute n (k + 1)" and n: "n ∈ set xs"
      by blast+
    then obtain i where i: "i ∈ {0.. k}" "xs!i = n"
      unfolding in_set_conv_nth by (auto simp add: less_Suc_eq_le natpermute_def)
    have eqs: "({0..k} - {i}) ∪ {i} = {0..k}"
      using i by auto
    have f: "finite({0..k} - {i})" "finite {i}"
      by auto
    have d: "({0..k} - {i}) ∩ {i} = {}"
      using i by auto
    from H have "n = setsum (nth xs) {0..k}"
      apply (simp add: natpermute_def)
      apply (auto simp add: atLeastLessThanSuc_atLeastAtMost listsum_setsum_nth)
      done
    also have "… = n + setsum (nth xs) ({0..k} - {i})"
      unfolding setsum.union_disjoint[OF f d, unfolded eqs] using i by simp
    finally have zxs: "∀ j∈ {0..k} - {i}. xs!j = 0"
      by auto
    from H have xsl: "length xs = k+1"
      by (simp add: natpermute_def)
    from i have i': "i < length (replicate (k+1) 0)"   "i < k+1"
      unfolding length_replicate by presburger+
    have "xs = replicate (k+1) 0 [i := n]"
      apply (rule nth_equalityI)
      unfolding xsl length_list_update length_replicate
      apply simp
      apply clarify
      unfolding nth_list_update[OF i'(1)]
      using i zxs
      apply (case_tac "ia = i")
      apply (auto simp del: replicate.simps)
      done
    then show "xs ∈ ?B" using i by blast
  qed
  show "?B ⊆ ?A"
  proof
    fix xs
    assume "xs ∈ ?B"
    then obtain i where i: "i ∈ {0..k}" and xs: "xs = replicate (k + 1) 0 [i:=n]"
      by auto
    have nxs: "n ∈ set xs"
      unfolding xs
      apply (rule set_update_memI)
      using i apply simp
      done
    have xsl: "length xs = k + 1"
      by (simp only: xs length_replicate length_list_update)
    have "listsum xs = setsum (nth xs) {0..<k+1}"
      unfolding listsum_setsum_nth xsl ..
    also have "… = setsum (λj. if j = i then n else 0) {0..< k+1}"
      by (rule setsum.cong) (simp_all add: xs del: replicate.simps)
    also have "… = n" using i by (simp add: setsum.delta)
    finally have "xs ∈ natpermute n (k + 1)"
      using xsl unfolding natpermute_def mem_Collect_eq by blast
    then show "xs ∈ ?A"
      using nxs by blast
  qed
qed

text ‹The general form.›
lemma fps_setprod_nth:
  fixes m :: nat
    and a :: "nat ⇒ 'a::comm_ring_1 fps"
  shows "(setprod a {0 .. m}) $ n =
    setsum (λv. setprod (λj. (a j) $ (v!j)) {0..m}) (natpermute n (m+1))"
  (is "?P m n")
proof (induct m arbitrary: n rule: nat_less_induct)
  fix m n assume H: "∀m' < m. ∀n. ?P m' n"
  show "?P m n"
  proof (cases m)
    case 0
    then show ?thesis
      apply simp
      unfolding natlist_trivial_1[where n = n, unfolded One_nat_def]
      apply simp
      done
  next
    case (Suc k)
    then have km: "k < m" by arith
    have u0: "{0 .. k} ∪ {m} = {0..m}"
      using Suc by (simp add: set_eq_iff) presburger
    have f0: "finite {0 .. k}" "finite {m}" by auto
    have d0: "{0 .. k} ∩ {m} = {}" using Suc by auto
    have "(setprod a {0 .. m}) $ n = (setprod a {0 .. k} * a m) $ n"
      unfolding setprod.union_disjoint[OF f0 d0, unfolded u0] by simp
    also have "… = (∑i = 0..n. (∑v∈natpermute i (k + 1). ∏j∈{0..k}. a j $ v ! j) * a m $ (n - i))"
      unfolding fps_mult_nth H[rule_format, OF km] ..
    also have "… = (∑v∈natpermute n (m + 1). ∏j∈{0..m}. a j $ v ! j)"
      apply (simp add: Suc)
      unfolding natpermute_split[of m "m + 1", simplified, of n,
        unfolded natlist_trivial_1[unfolded One_nat_def] Suc]
      apply (subst setsum.UNION_disjoint)
      apply simp
      apply simp
      unfolding image_Collect[symmetric]
      apply clarsimp
      apply (rule finite_imageI)
      apply (rule natpermute_finite)
      apply (clarsimp simp add: set_eq_iff)
      apply auto
      apply (rule setsum.cong)
      apply (rule refl)
      unfolding setsum_left_distrib
      apply (rule sym)
      apply (rule_tac l = "λxs. xs @ [n - x]" in setsum.reindex_cong)
      apply (simp add: inj_on_def)
      apply auto
      unfolding setprod.union_disjoint[OF f0 d0, unfolded u0, unfolded Suc]
      apply (clarsimp simp add: natpermute_def nth_append)
      done
    finally show ?thesis .
  qed
qed

text ‹The special form for powers.›
lemma fps_power_nth_Suc:
  fixes m :: nat
    and a :: "'a::comm_ring_1 fps"
  shows "(a ^ Suc m)$n = setsum (λv. setprod (λj. a $ (v!j)) {0..m}) (natpermute n (m+1))"
proof -
  have th0: "a^Suc m = setprod (λi. a) {0..m}"
    by (simp add: setprod_constant)
  show ?thesis unfolding th0 fps_setprod_nth ..
qed

lemma fps_power_nth:
  fixes m :: nat
    and a :: "'a::comm_ring_1 fps"
  shows "(a ^m)$n =
    (if m=0 then 1$n else setsum (λv. setprod (λj. a $ (v!j)) {0..m - 1}) (natpermute n m))"
  by (cases m) (simp_all add: fps_power_nth_Suc del: power_Suc)

lemma fps_nth_power_0:
  fixes m :: nat
    and a :: "'a::comm_ring_1 fps"
  shows "(a ^m)$0 = (a$0) ^ m"
proof (cases m)
  case 0
  then show ?thesis by simp
next
  case (Suc n)
  then have c: "m = card {0..n}" by simp
  have "(a ^m)$0 = setprod (λi. a$0) {0..n}"
    by (simp add: Suc fps_power_nth del: replicate.simps power_Suc)
  also have "… = (a$0) ^ m"
   unfolding c by (rule setprod_constant)
 finally show ?thesis .
qed

lemma fps_compose_inj_right:
  assumes a0: "a$0 = (0::'a::idom)"
    and a1: "a$1 ≠ 0"
  shows "(b oo a = c oo a) ⟷ b = c"
  (is "?lhs ⟷?rhs")
proof
  show ?lhs if ?rhs using that by simp
  show ?rhs if ?lhs
  proof -
    have "b$n = c$n" for n
    proof (induct n rule: nat_less_induct)
      fix n
      assume H: "∀m<n. b$m = c$m"
      show "b$n = c$n"
      proof (cases n)
        case 0
        from ‹?lhs› have "(b oo a)$n = (c oo a)$n"
          by simp
        then show ?thesis
          using 0 by (simp add: fps_compose_nth)
      next
        case (Suc n1)
        have f: "finite {0 .. n1}" "finite {n}" by simp_all
        have eq: "{0 .. n1} ∪ {n} = {0 .. n}" using Suc by auto
        have d: "{0 .. n1} ∩ {n} = {}" using Suc by auto
        have seq: "(∑i = 0..n1. b $ i * a ^ i $ n) = (∑i = 0..n1. c $ i * a ^ i $ n)"
          apply (rule setsum.cong)
          using H Suc
          apply auto
          done
        have th0: "(b oo a) $n = (∑i = 0..n1. c $ i * a ^ i $ n) + b$n * (a$1)^n"
          unfolding fps_compose_nth setsum.union_disjoint[OF f d, unfolded eq] seq
          using startsby_zero_power_nth_same[OF a0]
          by simp
        have th1: "(c oo a) $n = (∑i = 0..n1. c $ i * a ^ i $ n) + c$n * (a$1)^n"
          unfolding fps_compose_nth setsum.union_disjoint[OF f d, unfolded eq]
          using startsby_zero_power_nth_same[OF a0]
          by simp
        from ‹?lhs›[unfolded fps_eq_iff, rule_format, of n] th0 th1 a1
        show ?thesis by auto
      qed
    qed
    then show ?rhs by (simp add: fps_eq_iff)
  qed
qed


subsection ‹Radicals›

declare setprod.cong [fundef_cong]

function radical :: "(nat ⇒ 'a ⇒ 'a) ⇒ nat ⇒ 'a::field fps ⇒ nat ⇒ 'a"
where
  "radical r 0 a 0 = 1"
| "radical r 0 a (Suc n) = 0"
| "radical r (Suc k) a 0 = r (Suc k) (a$0)"
| "radical r (Suc k) a (Suc n) =
    (a$ Suc n - setsum (λxs. setprod (λj. radical r (Suc k) a (xs ! j)) {0..k})
      {xs. xs ∈ natpermute (Suc n) (Suc k) ∧ Suc n ∉ set xs}) /
    (of_nat (Suc k) * (radical r (Suc k) a 0)^k)"
  by pat_completeness auto

termination radical
proof
  let ?R = "measure (λ(r, k, a, n). n)"
  {
    show "wf ?R" by auto
  next
    fix r k a n xs i
    assume xs: "xs ∈ {xs ∈ natpermute (Suc n) (Suc k). Suc n ∉ set xs}" and i: "i ∈ {0..k}"
    have False if c: "Suc n ≤ xs ! i"
    proof -
      from xs i have "xs !i ≠ Suc n"
        by (auto simp add: in_set_conv_nth natpermute_def)
      with c have c': "Suc n < xs!i" by arith
      have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}"
        by simp_all
      have d: "{0 ..< i} ∩ ({i} ∪ {i+1 ..< Suc k}) = {}" "{i} ∩ {i+1..< Suc k} = {}"
        by auto
      have eqs: "{0..<Suc k} = {0 ..< i} ∪ ({i} ∪ {i+1 ..< Suc k})"
        using i by auto
      from xs have "Suc n = listsum xs"
        by (simp add: natpermute_def)
      also have "… = setsum (nth xs) {0..<Suc k}" using xs
        by (simp add: natpermute_def listsum_setsum_nth)
      also have "… = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
        unfolding eqs  setsum.union_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
        unfolding setsum.union_disjoint[OF fths(2) fths(3) d(2)]
        by simp
      finally show ?thesis using c' by simp
    qed
    then show "((r, Suc k, a, xs!i), r, Suc k, a, Suc n) ∈ ?R"
      apply auto
      apply (metis not_less)
      done
  next
    fix r k a n
    show "((r, Suc k, a, 0), r, Suc k, a, Suc n) ∈ ?R" by simp
  }
qed

definition "fps_radical r n a = Abs_fps (radical r n a)"

lemma fps_radical0[simp]: "fps_radical r 0 a = 1"
  apply (auto simp add: fps_eq_iff fps_radical_def)
  apply (case_tac n)
  apply auto
  done

lemma fps_radical_nth_0[simp]: "fps_radical r n a $ 0 = (if n = 0 then 1 else r n (a$0))"
  by (cases n) (simp_all add: fps_radical_def)

lemma fps_radical_power_nth[simp]:
  assumes r: "(r k (a$0)) ^ k = a$0"
  shows "fps_radical r k a ^ k $ 0 = (if k = 0 then 1 else a$0)"
proof (cases k)
  case 0
  then show ?thesis by simp
next
  case (Suc h)
  have eq1: "fps_radical r k a ^ k $ 0 = (∏j∈{0..h}. fps_radical r k a $ (replicate k 0) ! j)"
    unfolding fps_power_nth Suc by simp
  also have "… = (∏j∈{0..h}. r k (a$0))"
    apply (rule setprod.cong)
    apply simp
    using Suc
    apply (subgoal_tac "replicate k 0 ! x = 0")
    apply (auto intro: nth_replicate simp del: replicate.simps)
    done
  also have "… = a$0"
    using r Suc by (simp add: setprod_constant)
  finally show ?thesis
    using Suc by simp
qed

lemma natpermute_max_card:
  assumes n0: "n ≠ 0"
  shows "card {xs ∈ natpermute n (k + 1). n ∈ set xs} = k + 1"
  unfolding natpermute_contain_maximal
proof -
  let ?A = "λi. {replicate (k + 1) 0[i := n]}"
  let ?K = "{0 ..k}"
  have fK: "finite ?K"
    by simp
  have fAK: "∀i∈?K. finite (?A i)"
    by auto
  have d: "∀i∈ ?K. ∀j∈ ?K. i ≠ j ⟶
    {replicate (k + 1) 0[i := n]} ∩ {replicate (k + 1) 0[j := n]} = {}"
  proof clarify
    fix i j
    assume i: "i ∈ ?K" and j: "j ∈ ?K" and ij: "i ≠ j"
    have False if eq: "replicate (k+1) 0 [i:=n] = replicate (k+1) 0 [j:= n]"
    proof -
      have "(replicate (k+1) 0 [i:=n] ! i) = n"
        using i by (simp del: replicate.simps)
      moreover
      have "(replicate (k+1) 0 [j:=n] ! i) = 0"
        using i ij by (simp del: replicate.simps)
      ultimately show ?thesis
        using eq n0 by (simp del: replicate.simps)
    qed
    then show "{replicate (k + 1) 0[i := n]} ∩ {replicate (k + 1) 0[j := n]} = {}"
      by auto
  qed
  from card_UN_disjoint[OF fK fAK d]
  show "card (⋃i∈{0..k}. {replicate (k + 1) 0[i := n]}) = k + 1"
    by simp
qed

lemma power_radical:
  fixes a:: "'a::field_char_0 fps"
  assumes a0: "a$0 ≠ 0"
  shows "(r (Suc k) (a$0)) ^ Suc k = a$0 ⟷ (fps_radical r (Suc k) a) ^ (Suc k) = a"
    (is "?lhs ⟷ ?rhs")
proof
  let ?r = "fps_radical r (Suc k) a"
  show ?rhs if r0: ?lhs
  proof -
    from a0 r0 have r00: "r (Suc k) (a$0) ≠ 0" by auto
    have "?r ^ Suc k $ z = a$z" for z
    proof (induct z rule: nat_less_induct)
      fix n
      assume H: "∀m<n. ?r ^ Suc k $ m = a$m"
      show "?r ^ Suc k $ n = a $n"
      proof (cases n)
        case 0
        then show ?thesis
          using fps_radical_power_nth[of r "Suc k" a, OF r0] by simp
      next
        case (Suc n1)
        then have "n ≠ 0" by simp
        let ?Pnk = "natpermute n (k + 1)"
        let ?Pnkn = "{xs ∈ ?Pnk. n ∈ set xs}"
        let ?Pnknn = "{xs ∈ ?Pnk. n ∉ set xs}"
        have eq: "?Pnkn ∪ ?Pnknn = ?Pnk" by blast
        have d: "?Pnkn ∩ ?Pnknn = {}" by blast
        have f: "finite ?Pnkn" "finite ?Pnknn"
          using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
          by (metis natpermute_finite)+
        let ?f = "λv. ∏j∈{0..k}. ?r $ v ! j"
        have "setsum ?f ?Pnkn = setsum (λv. ?r $ n * r (Suc k) (a $ 0) ^ k) ?Pnkn"
        proof (rule setsum.cong)
          fix v assume v: "v ∈ {xs ∈ natpermute n (k + 1). n ∈ set xs}"
          let ?ths = "(∏j∈{0..k}. fps_radical r (Suc k) a $ v ! j) =
            fps_radical r (Suc k) a $ n * r (Suc k) (a $ 0) ^ k"
          from v obtain i where i: "i ∈ {0..k}" "v = replicate (k+1) 0 [i:= n]"
            unfolding natpermute_contain_maximal by auto
          have "(∏j∈{0..k}. fps_radical r (Suc k) a $ v ! j) =
              (∏j∈{0..k}. if j = i then fps_radical r (Suc k) a $ n else r (Suc k) (a$0))"
            apply (rule setprod.cong, simp)
            using i r0
            apply (simp del: replicate.simps)
            done
          also have "… = (fps_radical r (Suc k) a $ n) * r (Suc k) (a$0) ^ k"
            using i r0 by (simp add: setprod_gen_delta)
          finally show ?ths .
        qed rule
        then have "setsum ?f ?Pnkn = of_nat (k+1) * ?r $ n * r (Suc k) (a $ 0) ^ k"
          by (simp add: natpermute_max_card[OF ‹n ≠ 0›, simplified])
        also have "… = a$n - setsum ?f ?Pnknn"
          unfolding Suc using r00 a0 by (simp add: field_simps fps_radical_def del: of_nat_Suc)
        finally have fn: "setsum ?f ?Pnkn = a$n - setsum ?f ?Pnknn" .
        have "(?r ^ Suc k)$n = setsum ?f ?Pnkn + setsum ?f ?Pnknn"
          unfolding fps_power_nth_Suc setsum.union_disjoint[OF f d, unfolded eq] ..
        also have "… = a$n" unfolding fn by simp
        finally show ?thesis .
      qed
    qed
    then show ?thesis using r0 by (simp add: fps_eq_iff)
  qed
  show ?lhs if ?rhs
  proof -
    from that have "((fps_radical r (Suc k) a) ^ (Suc k))$0 = a$0"
      by simp
    then show ?thesis
      unfolding fps_power_nth_Suc
      by (simp add: setprod_constant del: replicate.simps)
  qed
qed

(*
lemma power_radical:
  fixes a:: "'a::field_char_0 fps"
  assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 ≠ 0"
  shows "(fps_radical r (Suc k) a) ^ (Suc k) = a"
proof-
  let ?r = "fps_radical r (Suc k) a"
  from a0 r0 have r00: "r (Suc k) (a$0) ≠ 0" by auto
  {fix z have "?r ^ Suc k $ z = a$z"
    proof(induct z rule: nat_less_induct)
      fix n assume H: "∀m<n. ?r ^ Suc k $ m = a$m"
      {assume "n = 0" then have "?r ^ Suc k $ n = a $n"
          using fps_radical_power_nth[of r "Suc k" a, OF r0] by simp}
      moreover
      {fix n1 assume n1: "n = Suc n1"
        have fK: "finite {0..k}" by simp
        have nz: "n ≠ 0" using n1 by arith
        let ?Pnk = "natpermute n (k + 1)"
        let ?Pnkn = "{xs ∈ ?Pnk. n ∈ set xs}"
        let ?Pnknn = "{xs ∈ ?Pnk. n ∉ set xs}"
        have eq: "?Pnkn ∪ ?Pnknn = ?Pnk" by blast
        have d: "?Pnkn ∩ ?Pnknn = {}" by blast
        have f: "finite ?Pnkn" "finite ?Pnknn"
          using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
          by (metis natpermute_finite)+
        let ?f = "λv. ∏j∈{0..k}. ?r $ v ! j"
        have "setsum ?f ?Pnkn = setsum (λv. ?r $ n * r (Suc k) (a $ 0) ^ k) ?Pnkn"
        proof(rule setsum.cong2)
          fix v assume v: "v ∈ {xs ∈ natpermute n (k + 1). n ∈ set xs}"
          let ?ths = "(∏j∈{0..k}. fps_radical r (Suc k) a $ v ! j) = fps_radical r (Suc k) a $ n * r (Suc k) (a $ 0) ^ k"
          from v obtain i where i: "i ∈ {0..k}" "v = replicate (k+1) 0 [i:= n]"
            unfolding natpermute_contain_maximal by auto
          have "(∏j∈{0..k}. fps_radical r (Suc k) a $ v ! j) = (∏j∈{0..k}. if j = i then fps_radical r (Suc k) a $ n else r (Suc k) (a$0))"
            apply (rule setprod.cong, simp)
            using i r0 by (simp del: replicate.simps)
          also have "… = (fps_radical r (Suc k) a $ n) * r (Suc k) (a$0) ^ k"
            unfolding setprod_gen_delta[OF fK] using i r0 by simp
          finally show ?ths .
        qed
        then have "setsum ?f ?Pnkn = of_nat (k+1) * ?r $ n * r (Suc k) (a $ 0) ^ k"
          by (simp add: natpermute_max_card[OF nz, simplified])
        also have "… = a$n - setsum ?f ?Pnknn"
          unfolding n1 using r00 a0 by (simp add: field_simps fps_radical_def del: of_nat_Suc )
        finally have fn: "setsum ?f ?Pnkn = a$n - setsum ?f ?Pnknn" .
        have "(?r ^ Suc k)$n = setsum ?f ?Pnkn + setsum ?f ?Pnknn"
          unfolding fps_power_nth_Suc setsum.union_disjoint[OF f d, unfolded eq] ..
        also have "… = a$n" unfolding fn by simp
        finally have "?r ^ Suc k $ n = a $n" .}
      ultimately  show "?r ^ Suc k $ n = a $n" by (cases n, auto)
  qed }
  then show ?thesis by (simp add: fps_eq_iff)
qed

*)
lemma eq_divide_imp':
  fixes c :: "'a::field"
  shows "c ≠ 0 ⟹ a * c = b ⟹ a = b / c"
  by (simp add: field_simps)

lemma radical_unique:
  assumes r0: "(r (Suc k) (b$0)) ^ Suc k = b$0"
    and a0: "r (Suc k) (b$0 ::'a::field_char_0) = a$0"
    and b0: "b$0 ≠ 0"
  shows "a^(Suc k) = b ⟷ a = fps_radical r (Suc k) b"
    (is "?lhs ⟷ ?rhs" is "_ ⟷ a = ?r")
proof
  show ?lhs if ?rhs
    using that using power_radical[OF b0, of r k, unfolded r0] by simp
  show ?rhs if ?lhs
  proof -
    have r00: "r (Suc k) (b$0) ≠ 0" using b0 r0 by auto
    have ceq: "card {0..k} = Suc k" by simp
    from a0 have a0r0: "a$0 = ?r$0" by simp
    have "a $ n = ?r $ n" for n
    proof (induct n rule: nat_less_induct)
      fix n
      assume h: "∀m<n. a$m = ?r $m"
      show "a$n = ?r $ n"
      proof (cases n)
        case 0
        then show ?thesis using a0 by simp
      next
        case (Suc n1)
        have fK: "finite {0..k}" by simp
        have nz: "n ≠ 0" using Suc by simp
        let ?Pnk = "natpermute n (Suc k)"
        let ?Pnkn = "{xs ∈ ?Pnk. n ∈ set xs}"
        let ?Pnknn = "{xs ∈ ?Pnk. n ∉ set xs}"
        have eq: "?Pnkn ∪ ?Pnknn = ?Pnk" by blast
        have d: "?Pnkn ∩ ?Pnknn = {}" by blast
        have f: "finite ?Pnkn" "finite ?Pnknn"
          using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
          by (metis natpermute_finite)+
        let ?f = "λv. ∏j∈{0..k}. ?r $ v ! j"
        let ?g = "λv. ∏j∈{0..k}. a $ v ! j"
        have "setsum ?g ?Pnkn = setsum (λv. a $ n * (?r$0)^k) ?Pnkn"
        proof (rule setsum.cong)
          fix v
          assume v: "v ∈ {xs ∈ natpermute n (Suc k). n ∈ set xs}"
          let ?ths = "(∏j∈{0..k}. a $ v ! j) = a $ n * (?r$0)^k"
          from v obtain i where i: "i ∈ {0..k}" "v = replicate (k+1) 0 [i:= n]"
            unfolding Suc_eq_plus1 natpermute_contain_maximal
            by (auto simp del: replicate.simps)
          have "(∏j∈{0..k}. a $ v ! j) = (∏j∈{0..k}. if j = i then a $ n else r (Suc k) (b$0))"
            apply (rule setprod.cong, simp)
            using i a0
            apply (simp del: replicate.simps)
            done
          also have "… = a $ n * (?r $ 0)^k"
            using i by (simp add: setprod_gen_delta)
          finally show ?ths .
        qed rule
        then have th0: "setsum ?g ?Pnkn = of_nat (k+1) * a $ n * (?r $ 0)^k"
          by (simp add: natpermute_max_card[OF nz, simplified])
        have th1: "setsum ?g ?Pnknn = setsum ?f ?Pnknn"
        proof (rule setsum.cong, rule refl, rule setprod.cong, simp)
          fix xs i
          assume xs: "xs ∈ ?Pnknn" and i: "i ∈ {0..k}"
          have False if c: "n ≤ xs ! i"
          proof -
            from xs i have "xs ! i ≠ n"
              by (auto simp add: in_set_conv_nth natpermute_def)
            with c have c': "n < xs!i" by arith
            have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}"
              by simp_all
            have d: "{0 ..< i} ∩ ({i} ∪ {i+1 ..< Suc k}) = {}" "{i} ∩ {i+1..< Suc k} = {}"
              by auto
            have eqs: "{0..<Suc k} = {0 ..< i} ∪ ({i} ∪ {i+1 ..< Suc k})"
              using i by auto
            from xs have "n = listsum xs"
              by (simp add: natpermute_def)
            also have "… = setsum (nth xs) {0..<Suc k}"
              using xs by (simp add: natpermute_def listsum_setsum_nth)
            also have "… = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
              unfolding eqs  setsum.union_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
              unfolding setsum.union_disjoint[OF fths(2) fths(3) d(2)]
              by simp
            finally show ?thesis using c' by simp
          qed
          then have thn: "xs!i < n" by presburger
          from h[rule_format, OF thn] show "a$(xs !i) = ?r$(xs!i)" .
        qed
        have th00: "⋀x::'a. of_nat (Suc k) * (x * inverse (of_nat (Suc k))) = x"
          by (simp add: field_simps del: of_nat_Suc)
        from ‹?lhs› have "b$n = a^Suc k $ n"
          by (simp add: fps_eq_iff)
        also have "a ^ Suc k$n = setsum ?g ?Pnkn + setsum ?g ?Pnknn"
          unfolding fps_power_nth_Suc
          using setsum.union_disjoint[OF f d, unfolded Suc_eq_plus1[symmetric],
            unfolded eq, of ?g] by simp
        also have "… = of_nat (k+1) * a $ n * (?r $ 0)^k + setsum ?f ?Pnknn"
          unfolding th0 th1 ..
        finally have "of_nat (k+1) * a $ n * (?r $ 0)^k = b$n - setsum ?f ?Pnknn"
          by simp
        then have "a$n = (b$n - setsum ?f ?Pnknn) / (of_nat (k+1) * (?r $ 0)^k)"
          apply -
          apply (rule eq_divide_imp')
          using r00
          apply (simp del: of_nat_Suc)
          apply (simp add: ac_simps)
          done
        then show ?thesis
          apply (simp del: of_nat_Suc)
          unfolding fps_radical_def Suc
          apply (simp add: field_simps Suc th00 del: of_nat_Suc)
          done
      qed
    qed
    then show ?rhs by (simp add: fps_eq_iff)
  qed
qed


lemma radical_power:
  assumes r0: "r (Suc k) ((a$0) ^ Suc k) = a$0"
    and a0: "(a$0 :: 'a::field_char_0) ≠ 0"
  shows "(fps_radical r (Suc k) (a ^ Suc k)) = a"
proof -
  let ?ak = "a^ Suc k"
  have ak0: "?ak $ 0 = (a$0) ^ Suc k"
    by (simp add: fps_nth_power_0 del: power_Suc)
  from r0 have th0: "r (Suc k) (a ^ Suc k $ 0) ^ Suc k = a ^ Suc k $ 0"
    using ak0 by auto
  from r0 ak0 have th1: "r (Suc k) (a ^ Suc k $ 0) = a $ 0"
    by auto
  from ak0 a0 have ak00: "?ak $ 0 ≠0 "
    by auto
  from radical_unique[of r k ?ak a, OF th0 th1 ak00] show ?thesis
    by metis
qed

lemma fps_deriv_radical:
  fixes a :: "'a::field_char_0 fps"
  assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0"
    and a0: "a$0 ≠ 0"
  shows "fps_deriv (fps_radical r (Suc k) a) =
    fps_deriv a / (fps_const (of_nat (Suc k)) * (fps_radical r (Suc k) a) ^ k)"
proof -
  let ?r = "fps_radical r (Suc k) a"
  let ?w = "(fps_const (of_nat (Suc k)) * ?r ^ k)"
  from a0 r0 have r0': "r (Suc k) (a$0) ≠ 0"
    by auto
  from r0' have w0: "?w $ 0 ≠ 0"
    by (simp del: of_nat_Suc)
  note th0 = inverse_mult_eq_1[OF w0]
  let ?iw = "inverse ?w"
  from iffD1[OF power_radical[of a r], OF a0 r0]
  have "fps_deriv (?r ^ Suc k) = fps_deriv a"
    by simp
  then have "fps_deriv ?r * ?w = fps_deriv a"
    by (simp add: fps_deriv_power ac_simps del: power_Suc)
  then have "?iw * fps_deriv ?r * ?w = ?iw * fps_deriv a"
    by simp
  with a0 r0 have "fps_deriv ?r * (?iw * ?w) = fps_deriv a / ?w"
    by (subst fps_divide_unit) (auto simp del: of_nat_Suc)
  then show ?thesis unfolding th0 by simp
qed

lemma radical_mult_distrib:
  fixes a :: "'a::field_char_0 fps"
  assumes k: "k > 0"
    and ra0: "r k (a $ 0) ^ k = a $ 0"
    and rb0: "r k (b $ 0) ^ k = b $ 0"
    and a0: "a $ 0 ≠ 0"
    and b0: "b $ 0 ≠ 0"
  shows "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0) ⟷
    fps_radical r k (a * b) = fps_radical r k a * fps_radical r k b"
    (is "?lhs ⟷ ?rhs")
proof
  show ?rhs if r0': ?lhs
  proof -
    from r0' have r0: "(r k ((a * b) $ 0)) ^ k = (a * b) $ 0"
      by (simp add: fps_mult_nth ra0 rb0 power_mult_distrib)
    show ?thesis
    proof (cases k)
      case 0
      then show ?thesis using r0' by simp
    next
      case (Suc h)
      let ?ra = "fps_radical r (Suc h) a"
      let ?rb = "fps_radical r (Suc h) b"
      have th0: "r (Suc h) ((a * b) $ 0) = (fps_radical r (Suc h) a * fps_radical r (Suc h) b) $ 0"
        using r0' Suc by (simp add: fps_mult_nth)
      have ab0: "(a*b) $ 0 ≠ 0"
        using a0 b0 by (simp add: fps_mult_nth)
      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]
        iffD1[OF power_radical[of _ r], OF a0 ra0[unfolded Suc]] iffD1[OF power_radical[of _ r], OF b0 rb0[unfolded Suc]] Suc r0'
      show ?thesis
        by (auto simp add: power_mult_distrib simp del: power_Suc)
    qed
  qed
  show ?lhs if ?rhs
  proof -
    from that have "(fps_radical r k (a * b)) $ 0 = (fps_radical r k a * fps_radical r k b) $ 0"
      by simp
    then show ?thesis
      using k by (simp add: fps_mult_nth)
  qed
qed

(*
lemma radical_mult_distrib:
  fixes a:: "'a::field_char_0 fps"
  assumes
  ra0: "r k (a $ 0) ^ k = a $ 0"
  and rb0: "r k (b $ 0) ^ k = b $ 0"
  and r0': "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0)"
  and a0: "a$0 ≠ 0"
  and b0: "b$0 ≠ 0"
  shows "fps_radical r (k) (a*b) = fps_radical r (k) a * fps_radical r (k) (b)"
proof-
  from r0' have r0: "(r (k) ((a*b)$0)) ^ k = (a*b)$0"
    by (simp add: fps_mult_nth ra0 rb0 power_mult_distrib)
  {assume "k=0" then have ?thesis by simp}
  moreover
  {fix h assume k: "k = Suc h"
  let ?ra = "fps_radical r (Suc h) a"
  let ?rb = "fps_radical r (Suc h) b"
  have th0: "r (Suc h) ((a * b) $ 0) = (fps_radical r (Suc h) a * fps_radical r (Suc h) b) $ 0"
    using r0' k by (simp add: fps_mult_nth)
  have ab0: "(a*b) $ 0 ≠ 0" using a0 b0 by (simp add: fps_mult_nth)
  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]
    power_radical[of r, OF ra0[unfolded k] a0] power_radical[of r, OF rb0[unfolded k] b0] k
  have ?thesis by (auto simp add: power_mult_distrib simp del: power_Suc)}
ultimately show ?thesis by (cases k, auto)
qed
*)

lemma fps_divide_1 [simp]: "(a :: 'a::field fps) / 1 = a"
  by (fact divide_1)

lemma radical_divide:
  fixes a :: "'a::field_char_0 fps"
  assumes kp: "k > 0"
    and ra0: "(r k (a $ 0)) ^ k = a $ 0"
    and rb0: "(r k (b $ 0)) ^ k = b $ 0"
    and a0: "a$0 ≠ 0"
    and b0: "b$0 ≠ 0"
  shows "r k ((a $ 0) / (b$0)) = r k (a$0) / r k (b $ 0) ⟷
    fps_radical r k (a/b) = fps_radical r k a / fps_radical r k b"
  (is "?lhs = ?rhs")
proof
  let ?r = "fps_radical r k"
  from kp obtain h where k: "k = Suc h"
    by (cases k) auto
  have ra0': "r k (a$0) ≠ 0" using a0 ra0 k by auto
  have rb0': "r k (b$0) ≠ 0" using b0 rb0 k by auto

  show ?lhs if ?rhs
  proof -
    from that have "?r (a/b) $ 0 = (?r a / ?r b)$0"
      by simp
    then show ?thesis
      using k a0 b0 rb0' by (simp add: fps_divide_unit fps_mult_nth fps_inverse_def divide_inverse)
  qed
  show ?rhs if ?lhs
  proof -
    from a0 b0 have ab0[simp]: "(a/b)$0 = a$0 / b$0"
      by (simp add: fps_divide_def fps_mult_nth divide_inverse fps_inverse_def)
    have th0: "r k ((a/b)$0) ^ k = (a/b)$0"
      by (simp add: ‹?lhs› power_divide ra0 rb0)
    from a0 b0 ra0' rb0' kp ‹?lhs›
    have th1: "r k ((a / b) $ 0) = (fps_radical r k a / fps_radical r k b) $ 0"
      by (simp add: fps_divide_unit fps_mult_nth fps_inverse_def divide_inverse)
    from a0 b0 ra0' rb0' kp have ab0': "(a / b) $ 0 ≠ 0"
      by (simp add: fps_divide_unit fps_mult_nth fps_inverse_def nonzero_imp_inverse_nonzero)
    note tha[simp] = iffD1[OF power_radical[where r=r and k=h], OF a0 ra0[unfolded k], unfolded k[symmetric]]
    note thb[simp] = iffD1[OF power_radical[where r=r and k=h], OF b0 rb0[unfolded k], unfolded k[symmetric]]
    from b0 rb0' have th2: "(?r a / ?r b)^k = a/b"
      by (simp add: fps_divide_unit power_mult_distrib fps_inverse_power[symmetric])

    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]
    show ?thesis .
  qed
qed

lemma radical_inverse:
  fixes a :: "'a::field_char_0 fps"
  assumes k: "k > 0"
    and ra0: "r k (a $ 0) ^ k = a $ 0"
    and r1: "(r k 1)^k = 1"
    and a0: "a$0 ≠ 0"
  shows "r k (inverse (a $ 0)) = r k 1 / (r k (a $ 0)) ⟷
    fps_radical r k (inverse a) = fps_radical r k 1 / fps_radical r k a"
  using radical_divide[where k=k and r=r and a=1 and b=a, OF k ] ra0 r1 a0
  by (simp add: divide_inverse fps_divide_def)


subsection ‹Derivative of composition›

lemma fps_compose_deriv:
  fixes a :: "'a::idom fps"
  assumes b0: "b$0 = 0"
  shows "fps_deriv (a oo b) = ((fps_deriv a) oo b) * fps_deriv b"
proof -
  have "(fps_deriv (a oo b))$n = (((fps_deriv a) oo b) * (fps_deriv b)) $n" for n
  proof -
    have "(fps_deriv (a oo b))$n = setsum (λi. a $ i * (fps_deriv (b^i))$n) {0.. Suc n}"
      by (simp add: fps_compose_def field_simps setsum_right_distrib del: of_nat_Suc)
    also have "… = setsum (λi. a$i * ((fps_const (of_nat i)) * (fps_deriv b * (b^(i - 1))))$n) {0.. Suc n}"
      by (simp add: field_simps fps_deriv_power del: fps_mult_left_const_nth of_nat_Suc)
    also have "… = setsum (λi. of_nat i * a$i * (((b^(i - 1)) * fps_deriv b))$n) {0.. Suc n}"
      unfolding fps_mult_left_const_nth  by (simp add: field_simps)
    also have "… = setsum (λi. of_nat i * a$i * (setsum (λj. (b^ (i - 1))$j * (fps_deriv b)$(n - j)) {0..n})) {0.. Suc n}"
      unfolding fps_mult_nth ..
    also have "… = setsum (λi. of_nat i * a$i * (setsum (λj. (b^ (i - 1))$j * (fps_deriv b)$(n - j)) {0..n})) {1.. Suc n}"
      apply (rule setsum.mono_neutral_right)
      apply (auto simp add: mult_delta_left setsum.delta not_le)
      done
    also have "… = setsum (λi. of_nat (i + 1) * a$(i+1) * (setsum (λj. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}"
      unfolding fps_deriv_nth
      by (rule setsum.reindex_cong [of Suc]) (auto simp add: mult.assoc)
    finally have th0: "(fps_deriv (a oo b))$n =
      setsum (λi. of_nat (i + 1) * a$(i+1) * (setsum (λj. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}" .

    have "(((fps_deriv a) oo b) * (fps_deriv b))$n = setsum (λi. (fps_deriv b)$ (n - i) * ((fps_deriv a) oo b)$i) {0..n}"
      unfolding fps_mult_nth by (simp add: ac_simps)
    also have "… = setsum (λi. setsum (λj. of_nat (n - i +1) * b$(n - i + 1) * of_nat (j + 1) * a$(j+1) * (b^j)$i) {0..n}) {0..n}"
      unfolding fps_deriv_nth fps_compose_nth setsum_right_distrib mult.assoc
      apply (rule setsum.cong)
      apply (rule refl)
      apply (rule setsum.mono_neutral_left)
      apply (simp_all add: subset_eq)
      apply clarify
      apply (subgoal_tac "b^i$x = 0")
      apply simp
      apply (rule startsby_zero_power_prefix[OF b0, rule_format])
      apply simp
      done
    also have "… = setsum (λi. of_nat (i + 1) * a$(i+1) * (setsum (λj. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}"
      unfolding setsum_right_distrib
      apply (subst setsum.commute)
      apply (rule setsum.cong, rule refl)+
      apply simp
      done
    finally show ?thesis
      unfolding th0 by simp
  qed
  then show ?thesis by (simp add: fps_eq_iff)
qed

lemma fps_mult_X_plus_1_nth:
  "((1+X)*a) $n = (if n = 0 then (a$n :: 'a::comm_ring_1) else a$n + a$(n - 1))"
proof (cases n)
  case 0
  then show ?thesis
    by (simp add: fps_mult_nth)
next
  case (Suc m)
  have "((1 + X)*a) $ n = setsum (λi. (1 + X) $ i * a $ (n - i)) {0..n}"
    by (simp add: fps_mult_nth)
  also have "… = setsum (λi. (1+X)$i * a$(n-i)) {0.. 1}"
    unfolding Suc by (rule setsum.mono_neutral_right) auto
  also have "… = (if n = 0 then (a$n :: 'a::comm_ring_1) else a$n + a$(n - 1))"
    by (simp add: Suc)
  finally show ?thesis .
qed


subsection ‹Finite FPS (i.e. polynomials) and X›

lemma fps_poly_sum_X:
  assumes "∀i > n. a$i = (0::'a::comm_ring_1)"
  shows "a = setsum (λi. fps_const (a$i) * X^i) {0..n}" (is "a = ?r")
proof -
  have "a$i = ?r$i" for i
    unfolding fps_setsum_nth fps_mult_left_const_nth X_power_nth
    by (simp add: mult_delta_right setsum.delta' assms)
  then show ?thesis
    unfolding fps_eq_iff by blast
qed


subsection ‹Compositional inverses›

fun compinv :: "'a fps ⇒ nat ⇒ 'a::field"
where
  "compinv a 0 = X$0"
| "compinv a (Suc n) =
    (X$ Suc n - setsum (λi. (compinv a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"

definition "fps_inv a = Abs_fps (compinv a)"

lemma fps_inv:
  assumes a0: "a$0 = 0"
    and a1: "a$1 ≠ 0"
  shows "fps_inv a oo a = X"
proof -
  let ?i = "fps_inv a oo a"
  have "?i $n = X$n" for n
  proof (induct n rule: nat_less_induct)
    fix n
    assume h: "∀m<n. ?i$m = X$m"
    show "?i $ n = X$n"
    proof (cases n)
      case 0
      then show ?thesis using a0
        by (simp add: fps_compose_nth fps_inv_def)
    next
      case (Suc n1)
      have "?i $ n = setsum (λi. (fps_inv a $ i) * (a^i)$n) {0 .. n1} + fps_inv a $ Suc n1 * (a $ 1)^ Suc n1"
        by (simp only: fps_compose_nth) (simp add: Suc startsby_zero_power_nth_same [OF a0] del: power_Suc)
      also have "… = setsum (λi. (fps_inv a $ i) * (a^i)$n) {0 .. n1} +
        (X$ Suc n1 - setsum (λi. (fps_inv a $ i) * (a^i)$n) {0 .. n1})"
        using a0 a1 Suc by (simp add: fps_inv_def)
      also have "… = X$n" using Suc by simp
      finally show ?thesis .
    qed
  qed
  then show ?thesis
    by (simp add: fps_eq_iff)
qed


fun gcompinv :: "'a fps ⇒ 'a fps ⇒ nat ⇒ 'a::field"
where
  "gcompinv b a 0 = b$0"
| "gcompinv b a (Suc n) =
    (b$ Suc n - setsum (λi. (gcompinv b a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"

definition "fps_ginv b a = Abs_fps (gcompinv b a)"

lemma fps_ginv:
  assumes a0: "a$0 = 0"
    and a1: "a$1 ≠ 0"
  shows "fps_ginv b a oo a = b"
proof -
  let ?i = "fps_ginv b a oo a"
  have "?i $n = b$n" for n
  proof (induct n rule: nat_less_induct)
    fix n
    assume h: "∀m<n. ?i$m = b$m"
    show "?i $ n = b$n"
    proof (cases n)
      case 0
      then show ?thesis using a0
        by (simp add: fps_compose_nth fps_ginv_def)
    next
      case (Suc n1)
      have "?i $ n = setsum (λi. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1} + fps_ginv b a $ Suc n1 * (a $ 1)^ Suc n1"
        by (simp only: fps_compose_nth) (simp add: Suc startsby_zero_power_nth_same [OF a0] del: power_Suc)
      also have "… = setsum (λi. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1} +
        (b$ Suc n1 - setsum (λi. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1})"
        using a0 a1 Suc by (simp add: fps_ginv_def)
      also have "… = b$n" using Suc by simp
      finally show ?thesis .
    qed
  qed
  then show ?thesis
    by (simp add: fps_eq_iff)
qed

lemma fps_inv_ginv: "fps_inv = fps_ginv X"
  apply (auto simp add: fun_eq_iff fps_eq_iff fps_inv_def fps_ginv_def)
  apply (induct_tac n rule: nat_less_induct)
  apply auto
  apply (case_tac na)
  apply simp
  apply simp
  done

lemma fps_compose_1[simp]: "1 oo a = 1"
  by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum.delta)

lemma fps_compose_0[simp]: "0 oo a = 0"
  by (simp add: fps_eq_iff fps_compose_nth)

lemma fps_compose_0_right[simp]: "a oo 0 = fps_const (a $ 0)"
  by (auto simp add: fps_eq_iff fps_compose_nth power_0_left setsum.neutral)

lemma fps_compose_add_distrib: "(a + b) oo c = (a oo c) + (b oo c)"
  by (simp add: fps_eq_iff fps_compose_nth field_simps setsum.distrib)

lemma fps_compose_setsum_distrib: "(setsum f S) oo a = setsum (λi. f i oo a) S"
proof (cases "finite S")
  case True
  show ?thesis
  proof (rule finite_induct[OF True])
    show "setsum f {} oo a = (∑i∈{}. f i oo a)"
      by simp
  next
    fix x F
    assume fF: "finite F"
      and xF: "x ∉ F"
      and h: "setsum f F oo a = setsum (λi. f i oo a) F"
    show "setsum f (insert x F) oo a  = setsum (λi. f i oo a) (insert x F)"
      using fF xF h by (simp add: fps_compose_add_distrib)
  qed
next
  case False
  then show ?thesis by simp
qed

lemma convolution_eq:
  "setsum (λi. a (i :: nat) * b (n - i)) {0 .. n} =
    setsum (λ(i,j). a i * b j) {(i,j). i ≤ n ∧ j ≤ n ∧ i + j = n}"
  by (rule setsum.reindex_bij_witness[where i=fst and j="λi. (i, n - i)"]) auto

lemma product_composition_lemma:
  assumes c0: "c$0 = (0::'a::idom)"
    and d0: "d$0 = 0"
  shows "((a oo c) * (b oo d))$n =
    setsum (λ(k,m). a$k * b$m * (c^k * d^m) $ n) {(k,m). k + m ≤ n}"  (is "?l = ?r")
proof -
  let ?S = "{(k::nat, m::nat). k + m ≤ n}"
  have s: "?S ⊆ {0..n} × {0..n}" by (auto simp add: subset_eq)
  have f: "finite {(k::nat, m::nat). k + m ≤ n}"
    apply (rule finite_subset[OF s])
    apply auto
    done
  have "?r =  setsum (λi. setsum (λ(k,m). a$k * (c^k)$i * b$m * (d^m) $ (n - i)) {(k,m). k + m ≤ n}) {0..n}"
    apply (simp add: fps_mult_nth setsum_right_distrib)
    apply (subst setsum.commute)
    apply (rule setsum.cong)
    apply (auto simp add: field_simps)
    done
  also have "… = ?l"
    apply (simp add: fps_mult_nth fps_compose_nth setsum_product)
    apply (rule setsum.cong)
    apply (rule refl)
    apply (simp add: setsum.cartesian_product mult.assoc)
    apply (rule setsum.mono_neutral_right[OF f])
    apply (simp add: subset_eq)
    apply presburger
    apply clarsimp
    apply (rule ccontr)
    apply (clarsimp simp add: not_le)
    apply (case_tac "x < aa")
    apply simp
    apply (frule_tac startsby_zero_power_prefix[rule_format, OF c0])
    apply blast
    apply simp
    apply (frule_tac startsby_zero_power_prefix[rule_format, OF d0])
    apply blast
    done
  finally show ?thesis by simp
qed

lemma product_composition_lemma':
  assumes c0: "c$0 = (0::'a::idom)"
    and d0: "d$0 = 0"
  shows "((a oo c) * (b oo d))$n =
    setsum (λk. setsum (λm. a$k * b$m * (c^k * d^m) $ n) {0..n}) {0..n}"  (is "?l = ?r")
  unfolding product_composition_lemma[OF c0 d0]
  unfolding setsum.cartesian_product
  apply (rule setsum.mono_neutral_left)
  apply simp
  apply (clarsimp simp add: subset_eq)
  apply clarsimp
  apply (rule ccontr)
  apply (subgoal_tac "(c^aa * d^ba) $ n = 0")
  apply simp
  unfolding fps_mult_nth
  apply (rule setsum.neutral)
  apply (clarsimp simp add: not_le)
  apply (case_tac "x < aa")
  apply (rule startsby_zero_power_prefix[OF c0, rule_format])
  apply simp
  apply (subgoal_tac "n - x < ba")
  apply (frule_tac k = "ba" in startsby_zero_power_prefix[OF d0, rule_format])
  apply simp
  apply arith
  done


lemma setsum_pair_less_iff:
  "setsum (λ((k::nat),m). a k * b m * c (k + m)) {(k,m). k + m ≤ n} =
    setsum (λs. setsum (λi. a i * b (s - i) * c s) {0..s}) {0..n}"
  (is "?l = ?r")
proof -
  let ?KM = "{(k,m). k + m ≤ n}"
  let ?f = "λs. UNION {(0::nat)..s} (λi. {(i,s - i)})"
  have th0: "?KM = UNION {0..n} ?f"
    by auto
  show "?l = ?r "
    unfolding th0
    apply (subst setsum.UNION_disjoint)
    apply auto
    apply (subst setsum.UNION_disjoint)
    apply auto
    done
qed

lemma fps_compose_mult_distrib_lemma:
  assumes c0: "c$0 = (0::'a::idom)"
  shows "((a oo c) * (b oo c))$n = setsum (λs. setsum (λi. a$i * b$(s - i) * (c^s) $ n) {0..s}) {0..n}"
  unfolding product_composition_lemma[OF c0 c0] power_add[symmetric]
  unfolding setsum_pair_less_iff[where a = "λk. a$k" and b="λm. b$m" and c="λs. (c ^ s)$n" and n = n] ..

lemma fps_compose_mult_distrib:
  assumes c0: "c $ 0 = (0::'a::idom)"
  shows "(a * b) oo c = (a oo c) * (b oo c)"
  apply (simp add: fps_eq_iff fps_compose_mult_distrib_lemma [OF c0])
  apply (simp add: fps_compose_nth fps_mult_nth setsum_left_distrib)
  done

lemma fps_compose_setprod_distrib:
  assumes c0: "c$0 = (0::'a::idom)"
  shows "setprod a S oo c = setprod (λk. a k oo c) S"
  apply (cases "finite S")
  apply simp_all
  apply (induct S rule: finite_induct)
  apply simp
  apply (simp add: fps_compose_mult_distrib[OF c0])
  done

lemma fps_compose_power:
  assumes c0: "c$0 = (0::'a::idom)"
  shows "(a oo c)^n = a^n oo c"
proof (cases n)
  case 0
  then show ?thesis by simp
next
  case (Suc m)
  have th0: "a^n = setprod (λk. a) {0..m}" "(a oo c) ^ n = setprod (λk. a oo c) {0..m}"
    by (simp_all add: setprod_constant Suc)
  then show ?thesis
    by (simp add: fps_compose_setprod_distrib[OF c0])
qed

lemma fps_compose_uminus: "- (a::'a::ring_1 fps) oo c = - (a oo c)"
  by (simp add: fps_eq_iff fps_compose_nth field_simps setsum_negf[symmetric])

lemma fps_compose_sub_distrib: "(a - b) oo (c::'a::ring_1 fps) = (a oo c) - (b oo c)"
  using fps_compose_add_distrib [of a "- b" c] by (simp add: fps_compose_uminus)

lemma X_fps_compose: "X oo a = Abs_fps (λn. if n = 0 then (0::'a::comm_ring_1) else a$n)"
  by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum.delta)

lemma fps_inverse_compose:
  assumes b0: "(b$0 :: 'a::field) = 0"
    and a0: "a$0 ≠ 0"
  shows "inverse a oo b = inverse (a oo b)"
proof -
  let ?ia = "inverse a"
  let ?ab = "a oo b"
  let ?iab = "inverse ?ab"

  from a0 have ia0: "?ia $ 0 ≠ 0" by simp
  from a0 have ab0: "?ab $ 0 ≠ 0" by (simp add: fps_compose_def)
  have "(?ia oo b) *  (a oo b) = 1"
    unfolding fps_compose_mult_distrib[OF b0, symmetric]
    unfolding inverse_mult_eq_1[OF a0]
    fps_compose_1 ..

  then have "(?ia oo b) *  (a oo b) * ?iab  = 1 * ?iab" by simp
  then have "(?ia oo b) *  (?iab * (a oo b))  = ?iab" by simp
  then show ?thesis unfolding inverse_mult_eq_1[OF ab0] by simp
qed

lemma fps_divide_compose:
  assumes c0: "(c$0 :: 'a::field) = 0"
    and b0: "b$0 ≠ 0"
  shows "(a/b) oo c = (a oo c) / (b oo c)"
    using b0 c0 by (simp add: fps_divide_unit fps_inverse_compose fps_compose_mult_distrib)

lemma gp:
  assumes a0: "a$0 = (0::'a::field)"
  shows "(Abs_fps (λn. 1)) oo a = 1/(1 - a)"
    (is "?one oo a = _")
proof -
  have o0: "?one $ 0 ≠ 0" by simp
  have th0: "(1 - X) $ 0 ≠ (0::'a)" by simp
  from fps_inverse_gp[where ?'a = 'a]
  have "inverse ?one = 1 - X" by (simp add: fps_eq_iff)
  then have "inverse (inverse ?one) = inverse (1 - X)" by simp
  then have th: "?one = 1/(1 - X)" unfolding fps_inverse_idempotent[OF o0]
    by (simp add: fps_divide_def)
  show ?thesis
    unfolding th
    unfolding fps_divide_compose[OF a0 th0]
    fps_compose_1 fps_compose_sub_distrib X_fps_compose_startby0[OF a0] ..
qed

lemma fps_const_power [simp]: "fps_const (c::'a::ring_1) ^ n = fps_const (c^n)"
  by (induct n) auto

lemma fps_compose_radical:
  assumes b0: "b$0 = (0::'a::field_char_0)"
    and ra0: "r (Suc k) (a$0) ^ Suc k = a$0"
    and a0: "a$0 ≠ 0"
  shows "fps_radical r (Suc k)  a oo b = fps_radical r (Suc k) (a oo b)"
proof -
  let ?r = "fps_radical r (Suc k)"
  let ?ab = "a oo b"
  have ab0: "?ab $ 0 = a$0"
    by (simp add: fps_compose_def)
  from ab0 a0 ra0 have rab0: "?ab $ 0 ≠ 0" "r (Suc k) (?ab $ 0) ^ Suc k = ?ab $ 0"
    by simp_all
  have th00: "r (Suc k) ((a oo b) $ 0) = (fps_radical r (Suc k) a oo b) $ 0"
    by (simp add: ab0 fps_compose_def)
  have th0: "(?r a oo b) ^ (Suc k) = a  oo b"
    unfolding fps_compose_power[OF b0]
    unfolding iffD1[OF power_radical[of a r k], OF a0 ra0]  ..
  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]
  show ?thesis  .
qed

lemma fps_const_mult_apply_left: "fps_const c * (a oo b) = (fps_const c * a) oo b"
  by (simp add: fps_eq_iff fps_compose_nth setsum_right_distrib mult.assoc)

lemma fps_const_mult_apply_right:
  "(a oo b) * fps_const (c::'a::comm_semiring_1) = (fps_const c * a) oo b"
  by (auto simp add: fps_const_mult_apply_left mult.commute)

lemma fps_compose_assoc:
  assumes c0: "c$0 = (0::'a::idom)"
    and b0: "b$0 = 0"
  shows "a oo (b oo c) = a oo b oo c" (is "?l = ?r")
proof -
  have "?l$n = ?r$n" for n
  proof -
    have "?l$n = (setsum (λi. (fps_const (a$i) * b^i) oo c) {0..n})$n"
      by (simp add: fps_compose_nth fps_compose_power[OF c0] fps_const_mult_apply_left
        setsum_right_distrib mult.assoc fps_setsum_nth)
    also have "… = ((setsum (λi. fps_const (a$i) * b^i) {0..n}) oo c)$n"
      by (simp add: fps_compose_setsum_distrib)
    also have "… = ?r$n"
      apply (simp add: fps_compose_nth fps_setsum_nth setsum_left_distrib mult.assoc)
      apply (rule setsum.cong)
      apply (rule refl)
      apply (rule setsum.mono_neutral_right)
      apply (auto simp add: not_le)
      apply (erule startsby_zero_power_prefix[OF b0, rule_format])
      done
    finally show ?thesis .
  qed
  then show ?thesis
    by (simp add: fps_eq_iff)
qed


lemma fps_X_power_compose:
  assumes a0: "a$0=0"
  shows "X^k oo a = (a::'a::idom fps)^k"
  (is "?l = ?r")
proof (cases k)
  case 0
  then show ?thesis by simp
next
  case (Suc h)
  have "?l $ n = ?r $n" for n
  proof -
    consider "k > n" | "k ≤ n" by arith
    then show ?thesis
    proof cases
      case 1
      then show ?thesis
        using a0 startsby_zero_power_prefix[OF a0] Suc
        by (simp add: fps_compose_nth del: power_Suc)
    next
      case 2
      then show ?thesis
        by (simp add: fps_compose_nth mult_delta_left setsum.delta)
    qed
  qed
  then show ?thesis
    unfolding fps_eq_iff by blast
qed

lemma fps_inv_right:
  assumes a0: "a$0 = 0"
    and a1: "a$1 ≠ 0"
  shows "a oo fps_inv a = X"
proof -
  let ?ia = "fps_inv a"
  let ?iaa = "a oo fps_inv a"
  have th0: "?ia $ 0 = 0"
    by (simp add: fps_inv_def)
  have th1: "?iaa $ 0 = 0"
    using a0 a1 by (simp add: fps_inv_def fps_compose_nth)
  have th2: "X$0 = 0"
    by simp
  from fps_inv[OF a0 a1] have "a oo (fps_inv a oo a) = a oo X"
    by simp
  then have "(a oo fps_inv a) oo a = X oo a"
    by (simp add: fps_compose_assoc[OF a0 th0] X_fps_compose_startby0[OF a0])
  with fps_compose_inj_right[OF a0 a1] show ?thesis
    by simp
qed

lemma fps_inv_deriv:
  assumes a0: "a$0 = (0::'a::field)"
    and a1: "a$1 ≠ 0"
  shows "fps_deriv (fps_inv a) = inverse (fps_deriv a oo fps_inv a)"
proof -
  let ?ia = "fps_inv a"
  let ?d = "fps_deriv a oo ?ia"
  let ?dia = "fps_deriv ?ia"
  have ia0: "?ia$0 = 0"
    by (simp add: fps_inv_def)
  have th0: "?d$0 ≠ 0"
    using a1 by (simp add: fps_compose_nth)
  from fps_inv_right[OF a0 a1] have "?d * ?dia = 1"
    by (simp add: fps_compose_deriv[OF ia0, of a, symmetric] )
  then have "inverse ?d * ?d * ?dia = inverse ?d * 1"
    by simp
  with inverse_mult_eq_1 [OF th0] show "?dia = inverse ?d"
    by simp
qed

lemma fps_inv_idempotent:
  assumes a0: "a$0 = 0"
    and a1: "a$1 ≠ 0"
  shows "fps_inv (fps_inv a) = a"
proof -
  let ?r = "fps_inv"
  have ra0: "?r a $ 0 = 0"
    by (simp add: fps_inv_def)
  from a1 have ra1: "?r a $ 1 ≠ 0"
    by (simp add: fps_inv_def field_simps)
  have X0: "X$0 = 0"
    by simp
  from fps_inv[OF ra0 ra1] have "?r (?r a) oo ?r a = X" .
  then have "?r (?r a) oo ?r a oo a = X oo a"
    by simp
  then have "?r (?r a) oo (?r a oo a) = a"
    unfolding X_fps_compose_startby0[OF a0]
    unfolding fps_compose_assoc[OF a0 ra0, symmetric] .
  then show ?thesis
    unfolding fps_inv[OF a0 a1] by simp
qed

lemma fps_ginv_ginv:
  assumes a0: "a$0 = 0"
    and a1: "a$1 ≠ 0"
    and c0: "c$0 = 0"
    and  c1: "c$1 ≠ 0"
  shows "fps_ginv b (fps_ginv c a) = b oo a oo fps_inv c"
proof -
  let ?r = "fps_ginv"
  from c0 have rca0: "?r c a $0 = 0"
    by (simp add: fps_ginv_def)
  from a1 c1 have rca1: "?r c a $ 1 ≠ 0"
    by (simp add: fps_ginv_def field_simps)
  from fps_ginv[OF rca0 rca1]
  have "?r b (?r c a) oo ?r c a = b" .
  then have "?r b (?r c a) oo ?r c a oo a = b oo a"
    by simp
  then have "?r b (?r c a) oo (?r c a oo a) = b oo a"
    apply (subst fps_compose_assoc)
    using a0 c0
    apply (auto simp add: fps_ginv_def)
    done
  then have "?r b (?r c a) oo c = b oo a"
    unfolding fps_ginv[OF a0 a1] .
  then have "?r b (?r c a) oo c oo fps_inv c= b oo a oo fps_inv c"
    by simp
  then have "?r b (?r c a) oo (c oo fps_inv c) = b oo a oo fps_inv c"
    apply (subst fps_compose_assoc)
    using a0 c0
    apply (auto simp add: fps_inv_def)
    done
  then show ?thesis
    unfolding fps_inv_right[OF c0 c1] by simp
qed

lemma fps_ginv_deriv:
  assumes a0:"a$0 = (0::'a::field)"
    and a1: "a$1 ≠ 0"
  shows "fps_deriv (fps_ginv b a) = (fps_deriv b / fps_deriv a) oo fps_ginv X a"
proof -
  let ?ia = "fps_ginv b a"
  let ?iXa = "fps_ginv X a"
  let ?d = "fps_deriv"
  let ?dia = "?d ?ia"
  have iXa0: "?iXa $ 0 = 0"
    by (simp add: fps_ginv_def)
  have da0: "?d a $ 0 ≠ 0"
    using a1 by simp
  from fps_ginv[OF a0 a1, of b] have "?d (?ia oo a) = fps_deriv b"
    by simp
  then have "(?d ?ia oo a) * ?d a = ?d b"
    unfolding fps_compose_deriv[OF a0] .
  then have "(?d ?ia oo a) * ?d a * inverse (?d a) = ?d b * inverse (?d a)"
    by simp
  with a1 have "(?d ?ia oo a) * (inverse (?d a) * ?d a) = ?d b / ?d a"
    by (simp add: fps_divide_unit)
  then have "(?d ?ia oo a) oo ?iXa =  (?d b / ?d a) oo ?iXa"
    unfolding inverse_mult_eq_1[OF da0] by simp
  then have "?d ?ia oo (a oo ?iXa) =  (?d b / ?d a) oo ?iXa"
    unfolding fps_compose_assoc[OF iXa0 a0] .
  then show ?thesis unfolding fps_inv_ginv[symmetric]
    unfolding fps_inv_right[OF a0 a1] by simp
qed


subsection ‹Elementary series›

subsubsection ‹Exponential series›

definition "E x = Abs_fps (λn. x^n / of_nat (fact n))"

lemma E_deriv[simp]: "fps_deriv (E a) = fps_const (a::'a::field_char_0) * E a" (is "?l = ?r")
proof -
  have "?l$n = ?r $ n" for n
    apply (auto simp add: E_def field_simps power_Suc[symmetric]
      simp del: fact.simps of_nat_Suc power_Suc)
    apply (simp add: of_nat_mult field_simps)
    done
  then show ?thesis
    by (simp add: fps_eq_iff)
qed

lemma E_unique_ODE:
  "fps_deriv a = fps_const c * a ⟷ a = fps_const (a$0) * E (c::'a::field_char_0)"
  (is "?lhs ⟷ ?rhs")
proof
  show ?rhs if ?lhs
  proof -
    from that have th: "⋀n. a $ Suc n = c * a$n / of_nat (Suc n)"
      by (simp add: fps_deriv_def fps_eq_iff field_simps del: of_nat_Suc)
    have th': "a$n = a$0 * c ^ n/ (fact n)" for n
    proof (induct n)
      case 0
      then show ?case by simp
    next
      case Suc
      then show ?case
        unfolding th
        using fact_gt_zero
        apply (simp add: field_simps del: of_nat_Suc fact_Suc)
        apply simp
        done
    qed
    show ?thesis
      by (auto simp add: fps_eq_iff fps_const_mult_left E_def intro: th')
  qed
  show ?lhs if ?rhs
    using that by (metis E_deriv fps_deriv_mult_const_left mult.left_commute)
qed

lemma E_add_mult: "E (a + b) = E (a::'a::field_char_0) * E b" (is "?l = ?r")
proof -
  have "fps_deriv ?r = fps_const (a + b) * ?r"
    by (simp add: fps_const_add[symmetric] field_simps del: fps_const_add)
  then have "?r = ?l"
    by (simp only: E_unique_ODE) (simp add: fps_mult_nth E_def)
  then show ?thesis ..
qed

lemma E_nth[simp]: "E a $ n = a^n / of_nat (fact n)"
  by (simp add: E_def)

lemma E0[simp]: "E (0::'a::field) = 1"
  by (simp add: fps_eq_iff power_0_left)

lemma E_neg: "E (- a) = inverse (E (a::'a::field_char_0))"
proof -
  from E_add_mult[of a "- a"] have th0: "E a * E (- a) = 1" by simp
  from fps_inverse_unique[OF th0] show ?thesis by simp
qed

lemma E_nth_deriv[simp]: "fps_nth_deriv n (E (a::'a::field_char_0)) = (fps_const a)^n * (E a)"
  by (induct n) auto

lemma X_compose_E[simp]: "X oo E (a::'a::field) = E a - 1"
  by (simp add: fps_eq_iff X_fps_compose)

lemma LE_compose:
  assumes a: "a ≠ 0"
  shows "fps_inv (E a - 1) oo (E a - 1) = X"
    and "(E a - 1) oo fps_inv (E a - 1) = X"
proof -
  let ?b = "E a - 1"
  have b0: "?b $ 0 = 0"
    by simp
  have b1: "?b $ 1 ≠ 0"
    by (simp add: a)
  from fps_inv[OF b0 b1] show "fps_inv (E a - 1) oo (E a - 1) = X" .
  from fps_inv_right[OF b0 b1] show "(E a - 1) oo fps_inv (E a - 1) = X" .
qed

lemma E_power_mult: "(E (c::'a::field_char_0))^n = E (of_nat n * c)"
  by (induct n) (auto simp add: field_simps E_add_mult)

lemma radical_E:
  assumes r: "r (Suc k) 1 = 1"
  shows "fps_radical r (Suc k) (E (c::'a::field_char_0)) = E (c / of_nat (Suc k))"
proof -
  let ?ck = "(c / of_nat (Suc k))"
  let ?r = "fps_radical r (Suc k)"
  have eq0[simp]: "?ck * of_nat (Suc k) = c" "of_nat (Suc k) * ?ck = c"
    by (simp_all del: of_nat_Suc)
  have th0: "E ?ck ^ (Suc k) = E c" unfolding E_power_mult eq0 ..
  have th: "r (Suc k) (E c $0) ^ Suc k = E c $ 0"
    "r (Suc k) (E c $ 0) = E ?ck $ 0" "E c $ 0 ≠ 0" using r by simp_all
  from th0 radical_unique[where r=r and k=k, OF th] show ?thesis
    by auto
qed

lemma Ec_E1_eq: "E (1::'a::field_char_0) oo (fps_const c * X) = E c"
  apply (auto simp add: fps_eq_iff E_def fps_compose_def power_mult_distrib)
  apply (simp add: cond_value_iff cond_application_beta setsum.delta' cong del: if_weak_cong)
  done


subsubsection ‹Logarithmic series›

lemma Abs_fps_if_0:
  "Abs_fps (λn. if n = 0 then (v::'a::ring_1) else f n) =
    fps_const v + X * Abs_fps (λn. f (Suc n))"
  by (auto simp add: fps_eq_iff)

definition L :: "'a::field_char_0 ⇒ 'a fps"
  where "L c = fps_const (1/c) * Abs_fps (λn. if n = 0 then 0 else (- 1) ^ (n - 1) / of_nat n)"

lemma fps_deriv_L: "fps_deriv (L c) = fps_const (1/c) * inverse (1 + X)"
  unfolding fps_inverse_X_plus1
  by (simp add: L_def fps_eq_iff del: of_nat_Suc)

lemma L_nth: "L c $ n = (if n = 0 then 0 else 1/c * ((- 1) ^ (n - 1) / of_nat n))"
  by (simp add: L_def field_simps)

lemma L_0[simp]: "L c $ 0 = 0" by (simp add: L_def)

lemma L_E_inv:
  fixes a :: "'a::field_char_0"
  assumes a: "a ≠ 0"
  shows "L a = fps_inv (E a - 1)"  (is "?l = ?r")
proof -
  let ?b = "E a - 1"
  have b0: "?b $ 0 = 0" by simp
  have b1: "?b $ 1 ≠ 0" by (simp add: a)
  have "fps_deriv (E a - 1) oo fps_inv (E a - 1) =
    (fps_const a * (E a - 1) + fps_const a) oo fps_inv (E a - 1)"
    by (simp add: field_simps)
  also have "… = fps_const a * (X + 1)"
    apply (simp add: fps_compose_add_distrib fps_const_mult_apply_left[symmetric] fps_inv_right[OF b0 b1])
    apply (simp add: field_simps)
    done
  finally have eq: "fps_deriv (E a - 1) oo fps_inv (E a - 1) = fps_const a * (X + 1)" .
  from fps_inv_deriv[OF b0 b1, unfolded eq]
  have "fps_deriv (fps_inv ?b) = fps_const (inverse a) / (X + 1)"
    using a
    by (simp add: fps_const_inverse eq fps_divide_def fps_inverse_mult)
  then have "fps_deriv ?l = fps_deriv ?r"
    by (simp add: fps_deriv_L add.commute fps_divide_def divide_inverse)
  then show ?thesis unfolding fps_deriv_eq_iff
    by (simp add: L_nth fps_inv_def)
qed

lemma L_mult_add:
  assumes c0: "c≠0"
    and d0: "d≠0"
  shows "L c + L d = fps_const (c+d) * L (c*d)"
  (is "?r = ?l")
proof-
  from c0 d0 have eq: "1/c + 1/d = (c+d)/(c*d)" by (simp add: field_simps)
  have "fps_deriv ?r = fps_const (1/c + 1/d) * inverse (1 + X)"
    by (simp add: fps_deriv_L fps_const_add[symmetric] algebra_simps del: fps_const_add)
  also have "… = fps_deriv ?l"
    apply (simp add: fps_deriv_L)
    apply (simp add: fps_eq_iff eq)
    done
  finally show ?thesis
    unfolding fps_deriv_eq_iff by simp
qed


subsubsection ‹Binomial series›

definition "fps_binomial a = Abs_fps (λn. a gchoose n)"

lemma fps_binomial_nth[simp]: "fps_binomial a $ n = a gchoose n"
  by (simp add: fps_binomial_def)

lemma fps_binomial_ODE_unique:
  fixes c :: "'a::field_char_0"
  shows "fps_deriv a = (fps_const c * a) / (1 + X) ⟷ a = fps_const (a$0) * fps_binomial c"
  (is "?lhs ⟷ ?rhs")
proof
  let ?da = "fps_deriv a"
  let ?x1 = "(1 + X):: 'a fps"
  let ?l = "?x1 * ?da"
  let ?r = "fps_const c * a"

  have eq: "?l = ?r ⟷ ?lhs"
  proof -
    have x10: "?x1 $ 0 ≠ 0" by simp
    have "?l = ?r ⟷ inverse ?x1 * ?l = inverse ?x1 * ?r" by simp
    also have "… ⟷ ?da = (fps_const c * a) / ?x1"
      apply (simp only: fps_divide_def  mult.assoc[symmetric] inverse_mult_eq_1[OF x10])
      apply (simp add: field_simps)
      done
    finally show ?thesis .
  qed

  show ?rhs if ?lhs
  proof -
    from eq that have h: "?l = ?r" ..
    have th0: "a$ Suc n = ((c - of_nat n) / of_nat (Suc n)) * a $n" for n
    proof -
      from h have "?l $ n = ?r $ n" by simp
      then show ?thesis
        apply (simp add: field_simps del: of_nat_Suc)
        apply (cases n)
        apply (simp_all add: field_simps del: of_nat_Suc)
        done
    qed
    have th1: "a $ n = (c gchoose n) * a $ 0" for n
    proof (induct n)
      case 0
      then show ?case by simp
    next
      case (Suc m)
      then show ?case
        unfolding th0
        apply (simp add: field_simps del: of_nat_Suc)
        unfolding mult.assoc[symmetric] gbinomial_mult_1
        apply (simp add: field_simps)
        done
    qed
    show ?thesis
      apply (simp add: fps_eq_iff)
      apply (subst th1)
      apply (simp add: field_simps)
      done
  qed

  show ?lhs if ?rhs
  proof -
    have th00: "x * (a $ 0 * y) = a $ 0 * (x * y)" for x y
      by (simp add: mult.commute)
    have "?l = ?r"
      apply (subst ‹?rhs›)
      apply (subst (2) ‹?rhs›)
      apply (clarsimp simp add: fps_eq_iff field_simps)
      unfolding mult.assoc[symmetric] th00 gbinomial_mult_1
      apply (simp add: field_simps gbinomial_mult_1)
      done
    with eq show ?thesis ..
  qed
qed

lemma fps_binomial_deriv: "fps_deriv (fps_binomial c) = fps_const c * fps_binomial c / (1 + X)"
proof -
  let ?a = "fps_binomial c"
  have th0: "?a = fps_const (?a$0) * ?a" by (simp)
  from iffD2[OF fps_binomial_ODE_unique, OF th0] show ?thesis .
qed

lemma fps_binomial_add_mult: "fps_binomial (c+d) = fps_binomial c * fps_binomial d" (is "?l = ?r")
proof -
  let ?P = "?r - ?l"
  let ?b = "fps_binomial"
  let ?db = "λx. fps_deriv (?b x)"
  have "fps_deriv ?P = ?db c * ?b d + ?b c * ?db d - ?db (c + d)"  by simp
  also have "… = inverse (1 + X) *
      (fps_const c * ?b c * ?b d + fps_const d * ?b c * ?b d - fps_const (c+d) * ?b (c + d))"
    unfolding fps_binomial_deriv
    by (simp add: fps_divide_def field_simps)
  also have "… = (fps_const (c + d)/ (1 + X)) * ?P"
    by (simp add: field_simps fps_divide_unit fps_const_add[symmetric] del: fps_const_add)
  finally have th0: "fps_deriv ?P = fps_const (c+d) * ?P / (1 + X)"
    by (simp add: fps_divide_def)
  have "?P = fps_const (?P$0) * ?b (c + d)"
    unfolding fps_binomial_ODE_unique[symmetric]
    using th0 by simp
  then have "?P = 0" by (simp add: fps_mult_nth)
  then show ?thesis by simp
qed

lemma fps_binomial_minus_one: "fps_binomial (- 1) = inverse (1 + X)"
  (is "?l = inverse ?r")
proof-
  have th: "?r$0 ≠ 0" by simp
  have th': "fps_deriv (inverse ?r) = fps_const (- 1) * inverse ?r / (1 + X)"
    by (simp add: fps_inverse_deriv[OF th] fps_divide_def
      power2_eq_square mult.commute fps_const_neg[symmetric] del: fps_const_neg)
  have eq: "inverse ?r $ 0 = 1"
    by (simp add: fps_inverse_def)
  from iffD1[OF fps_binomial_ODE_unique[of "inverse (1 + X)" "- 1"] th'] eq
  show ?thesis by (simp add: fps_inverse_def)
qed

text ‹Vandermonde's Identity as a consequence.›
lemma gbinomial_Vandermonde:
  "setsum (λk. (a gchoose k) * (b gchoose (n - k))) {0..n} = (a + b) gchoose n"
proof -
  let ?ba = "fps_binomial a"
  let ?bb = "fps_binomial b"
  let ?bab = "fps_binomial (a + b)"
  from fps_binomial_add_mult[of a b] have "?bab $ n = (?ba * ?bb)$n" by simp
  then show ?thesis by (simp add: fps_mult_nth)
qed

lemma binomial_Vandermonde:
  "setsum (λk. (a choose k) * (b choose (n - k))) {0..n} = (a + b) choose n"
  using gbinomial_Vandermonde[of "(of_nat a)" "of_nat b" n]
  by (simp only: binomial_gbinomial[symmetric] of_nat_mult[symmetric]
                 of_nat_setsum[symmetric] of_nat_add[symmetric] of_nat_eq_iff)

lemma binomial_Vandermonde_same: "setsum (λk. (n choose k)2) {0..n} = (2 * n) choose n"
  using binomial_Vandermonde[of n n n, symmetric]
  unfolding mult_2
  apply (simp add: power2_eq_square)
  apply (rule setsum.cong)
  apply (auto intro:  binomial_symmetric)
  done

lemma Vandermonde_pochhammer_lemma:
  fixes a :: "'a::field_char_0"
  assumes b: "∀j∈{0 ..<n}. b ≠ of_nat j"
  shows "setsum (λk. (pochhammer (- a) k * pochhammer (- (of_nat n)) k) /
      (of_nat (fact k) * pochhammer (b - of_nat n + 1) k)) {0..n} =
    pochhammer (- (a + b)) n / pochhammer (- b) n"
  (is "?l = ?r")
proof -
  let ?m1 = "λm. (- 1 :: 'a) ^ m"
  let ?f = "λm. of_nat (fact m)"
  let ?p = "λ(x::'a). pochhammer (- x)"
  from b have bn0: "?p b n ≠ 0"
    unfolding pochhammer_eq_0_iff by simp
  have th00:
    "b gchoose (n - k) =
        (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)"
      (is ?gchoose)
    "pochhammer (1 + b - of_nat n) k ≠ 0"
      (is ?pochhammer)
    if kn: "k ∈ {0..n}" for k
  proof -
    have nz: "pochhammer (1 + b - of_nat n) n ≠ 0"
    proof
      assume "pochhammer (1 + b - of_nat n) n = 0"
      then have c: "pochhammer (b - of_nat n + 1) n = 0"
        by (simp add: algebra_simps)
      then obtain j where j: "j < n" "b - of_nat n + 1 = - of_nat j"
        unfolding pochhammer_eq_0_iff by blast
      from j have "b = of_nat n - of_nat j - of_nat 1"
        by (simp add: algebra_simps)
      then have "b = of_nat (n - j - 1)"
        using j kn by (simp add: of_nat_diff)
      with b show False using j by auto
    qed

    from nz kn [simplified] have nz': "pochhammer (1 + b - of_nat n) k ≠ 0"
      by (rule pochhammer_neq_0_mono)

    consider "k = 0 ∨ n = 0" | "k ≠ 0" "n ≠ 0"
      by blast
    then have "b gchoose (n - k) =
      (?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k)"
    proof cases
      case 1
      then show ?thesis
        using kn by (cases "k = 0") (simp_all add: gbinomial_pochhammer)
    next
      case neq: 2
      then obtain m where m: "n = Suc m"
        by (cases n) auto
      from neq(1) obtain h where h: "k = Suc h"
        by (cases k) auto
      show ?thesis
      proof (cases "k = n")
        case True
        then show ?thesis
          using pochhammer_minus'[where k=k and b=b]
          apply (simp add: pochhammer_same)
          using bn0
          apply (simp add: field_simps power_add[symmetric])
          done
      next
        case False
        with kn have kn': "k < n"
          by simp
        have m1nk: "?m1 n = setprod (λi. - 1) {0..m}" "?m1 k = setprod (λi. - 1) {0..h}"
          by (simp_all add: setprod_constant m h)
        have bnz0: "pochhammer (b - of_nat n + 1) k ≠ 0"
          using bn0 kn
          unfolding pochhammer_eq_0_iff
          apply auto
          apply (erule_tac x= "n - ka - 1" in allE)
          apply (auto simp add: algebra_simps of_nat_diff)
          done
        have eq1: "setprod (λk. (1::'a) + of_nat m - of_nat k) {0 .. h} =
          setprod of_nat {Suc (m - h) .. Suc m}"
          using kn' h m
          by (intro setprod.reindex_bij_witness[where i="λk. Suc m - k" and j="λk. Suc m - k"])
             (auto simp: of_nat_diff)

        have th1: "(?m1 k * ?p (of_nat n) k) / ?f n = 1 / of_nat(fact (n - k))"
          unfolding m1nk
          unfolding m h pochhammer_Suc_setprod
          apply (simp add: field_simps del: fact_Suc)
          unfolding fact_altdef id_def
          unfolding of_nat_setprod
          unfolding setprod.distrib[symmetric]
          apply auto
          unfolding eq1
          apply (subst setprod.union_disjoint[symmetric])
          apply (auto)
          apply (rule setprod.cong)
          apply auto
          done
        have th20: "?m1 n * ?p b n = setprod (λi. b - of_nat i) {0..m}"
          unfolding m1nk
          unfolding m h pochhammer_Suc_setprod
          unfolding setprod.distrib[symmetric]
          apply (rule setprod.cong)
          apply auto
          done
        have th21:"pochhammer (b - of_nat n + 1) k = setprod (λi. b - of_nat i) {n - k .. n - 1}"
          unfolding h m
          unfolding pochhammer_Suc_setprod
          using kn m h
          by (intro setprod.reindex_bij_witness[where i="λk. n - 1 - k" and j="λi. m-i"])
             (auto simp: of_nat_diff)

        have "?m1 n * ?p b n =
          pochhammer (b - of_nat n + 1) k * setprod (λi. b - of_nat i) {0.. n - k - 1}"
          unfolding th20 th21
          unfolding h m
          apply (subst setprod.union_disjoint[symmetric])
          using kn' h m
          apply auto
          apply (rule setprod.cong)
          apply auto
          done
        then have th2: "(?m1 n * ?p b n)/pochhammer (b - of_nat n + 1) k =
          setprod (λi. b - of_nat i) {0.. n - k - 1}"
          using nz' by (simp add: field_simps)
        have "(?m1 n * ?p b n * ?m1 k * ?p (of_nat n) k) / (?f n * pochhammer (b - of_nat n + 1) k) =
          ((?m1 k * ?p (of_nat n) k) / ?f n) * ((?m1 n * ?p b n)/pochhammer (b - of_nat n + 1) k)"
          using bnz0
          by (simp add: field_simps)
        also have "… = b gchoose (n - k)"
          unfolding th1 th2
          using kn' by (simp add: gbinomial_def)
        finally show ?thesis by simp
      qed
    qed
    then show ?gchoose and ?pochhammer
      apply (cases "n = 0")
      using nz'
      apply auto
      done
  qed
  have "?r = ((a + b) gchoose n) * (of_nat (fact n) / (?m1 n * pochhammer (- b) n))"
    unfolding gbinomial_pochhammer
    using bn0 by (auto simp add: field_simps)
  also have "… = ?l"
    unfolding gbinomial_Vandermonde[symmetric]
    apply (simp add: th00)
    unfolding gbinomial_pochhammer
    using bn0
    apply (simp add: setsum_left_distrib setsum_right_distrib field_simps)
    apply (rule setsum.cong)
    apply (rule refl)
    apply (drule th00(2))
    apply (simp add: field_simps power_add[symmetric])
    done
  finally show ?thesis by simp
qed

lemma Vandermonde_pochhammer:
  fixes a :: "'a::field_char_0"
  assumes c: "∀i ∈ {0..< n}. c ≠ - of_nat i"
  shows "setsum (λk. (pochhammer a k * pochhammer (- (of_nat n)) k) /
    (of_nat (fact k) * pochhammer c k)) {0..n} = pochhammer (c - a) n / pochhammer c n"
proof -
  let ?a = "- a"
  let ?b = "c + of_nat n - 1"
  have h: "∀ j ∈{0..< n}. ?b ≠ of_nat j"
    using c
    apply (auto simp add: algebra_simps of_nat_diff)
    apply (erule_tac x = "n - j - 1" in ballE)
    apply (auto simp add: of_nat_diff algebra_simps)
    done
  have th0: "pochhammer (- (?a + ?b)) n = (- 1)^n * pochhammer (c - a) n"
    unfolding pochhammer_minus
    by (simp add: algebra_simps)
  have th1: "pochhammer (- ?b) n = (- 1)^n * pochhammer c n"
    unfolding pochhammer_minus
    by simp
  have nz: "pochhammer c n ≠ 0" using c
    by (simp add: pochhammer_eq_0_iff)
  from Vandermonde_pochhammer_lemma[where a = "?a" and b="?b" and n=n, OF h, unfolded th0 th1]
  show ?thesis
    using nz by (simp add: field_simps setsum_right_distrib)
qed


subsubsection ‹Formal trigonometric functions›

definition "fps_sin (c::'a::field_char_0) =
  Abs_fps (λn. if even n then 0 else (- 1) ^((n - 1) div 2) * c^n /(of_nat (fact n)))"

definition "fps_cos (c::'a::field_char_0) =
  Abs_fps (λn. if even n then (- 1) ^ (n div 2) * c^n / (of_nat (fact n)) else 0)"

lemma fps_sin_deriv:
  "fps_deriv (fps_sin c) = fps_const c * fps_cos c"
  (is "?lhs = ?rhs")
proof (rule fps_ext)
  fix n :: nat
  show "?lhs $ n = ?rhs $ n"
  proof (cases "even n")
    case True
    have "?lhs$n = of_nat (n+1) * (fps_sin c $ (n+1))" by simp
    also have "… = of_nat (n+1) * ((- 1)^(n div 2) * c^Suc n / of_nat (fact (Suc n)))"
      using True by (simp add: fps_sin_def)
    also have "… = (- 1)^(n div 2) * c^Suc n * (of_nat (n+1) / (of_nat (Suc n) * of_nat (fact n)))"
      unfolding fact_Suc of_nat_mult
      by (simp add: field_simps del: of_nat_add of_nat_Suc)
    also have "… = (- 1)^(n div 2) *c^Suc n / of_nat (fact n)"
      by (simp add: field_simps del: of_nat_add of_nat_Suc)
    finally show ?thesis
      using True by (simp add: fps_cos_def field_simps)
  next
    case False
    then show ?thesis
      by (simp_all add: fps_deriv_def fps_sin_def fps_cos_def)
  qed
qed

lemma fps_cos_deriv: "fps_deriv (fps_cos c) = fps_const (- c)* (fps_sin c)"
  (is "?lhs = ?rhs")
proof (rule fps_ext)
  have th0: "- ((- 1::'a) ^ n) = (- 1)^Suc n" for n
    by simp
  show "?lhs $ n = ?rhs $ n" for n
  proof (cases "even n")
    case False
    then have n0: "n ≠ 0" by presburger
    from False have th1: "Suc ((n - 1) div 2) = Suc n div 2"
      by (cases n) simp_all
    have "?lhs$n = of_nat (n+1) * (fps_cos c $ (n+1))" by simp
    also have "… = of_nat (n+1) * ((- 1)^((n + 1) div 2) * c^Suc n / of_nat (fact (Suc n)))"
      using False by (simp add: fps_cos_def)
    also have "… = (- 1)^((n + 1) div 2)*c^Suc n * (of_nat (n+1) / (of_nat (Suc n) * of_nat (fact n)))"
      unfolding fact_Suc of_nat_mult
      by (simp add: field_simps del: of_nat_add of_nat_Suc)
    also have "… = (- 1)^((n + 1) div 2) * c^Suc n / of_nat (fact n)"
      by (simp add: field_simps del: of_nat_add of_nat_Suc)
    also have "… = (- ((- 1)^((n - 1) div 2))) * c^Suc n / of_nat (fact n)"
      unfolding th0 unfolding th1 by simp
    finally show ?thesis
      using False by (simp add: fps_sin_def field_simps)
  next
    case True
    then show ?thesis
      by (simp_all add: fps_deriv_def fps_sin_def fps_cos_def)
  qed
qed

lemma fps_sin_cos_sum_of_squares: "(fps_cos c)2 + (fps_sin c)2 = 1"
  (is "?lhs = _")
proof -
  have "fps_deriv ?lhs = 0"
    apply (simp add:  fps_deriv_power fps_sin_deriv fps_cos_deriv)
    apply (simp add: field_simps fps_const_neg[symmetric] del: fps_const_neg)
    done
  then have "?lhs = fps_const (?lhs $ 0)"
    unfolding fps_deriv_eq_0_iff .
  also have "… = 1"
    by (auto simp add: fps_eq_iff numeral_2_eq_2 fps_mult_nth fps_cos_def fps_sin_def)
  finally show ?thesis .
qed

lemma fps_sin_nth_0 [simp]: "fps_sin c $ 0 = 0"
  unfolding fps_sin_def by simp

lemma fps_sin_nth_1 [simp]: "fps_sin c $ 1 = c"
  unfolding fps_sin_def by simp

lemma fps_sin_nth_add_2:
    "fps_sin c $ (n + 2) = - (c * c * fps_sin c $ n / (of_nat (n + 1) * of_nat (n + 2)))"
  unfolding fps_sin_def
  apply (cases n)
  apply simp
  apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq del: of_nat_Suc fact_Suc)
  apply (simp add: of_nat_mult del: of_nat_Suc mult_Suc)
  done

lemma fps_cos_nth_0 [simp]: "fps_cos c $ 0 = 1"
  unfolding fps_cos_def by simp

lemma fps_cos_nth_1 [simp]: "fps_cos c $ 1 = 0"
  unfolding fps_cos_def by simp

lemma fps_cos_nth_add_2:
  "fps_cos c $ (n + 2) = - (c * c * fps_cos c $ n / (of_nat (n + 1) * of_nat (n + 2)))"
  unfolding fps_cos_def
  apply (simp add: nonzero_divide_eq_eq nonzero_eq_divide_eq del: of_nat_Suc fact_Suc)
  apply (simp add: of_nat_mult del: of_nat_Suc mult_Suc)
  done

lemma nat_induct2: "P 0 ⟹ P 1 ⟹ (⋀n. P n ⟹ P (n + 2)) ⟹ P (n::nat)"
  unfolding One_nat_def numeral_2_eq_2
  apply (induct n rule: nat_less_induct)
  apply (case_tac n)
  apply simp
  apply (rename_tac m)
  apply (case_tac m)
  apply simp
  apply (rename_tac k)
  apply (case_tac k)
  apply simp_all
  done

lemma nat_add_1_add_1: "(n::nat) + 1 + 1 = n + 2"
  by simp

lemma eq_fps_sin:
  assumes 0: "a $ 0 = 0"
    and 1: "a $ 1 = c"
    and 2: "fps_deriv (fps_deriv a) = - (fps_const c * fps_const c * a)"
  shows "a = fps_sin c"
  apply (rule fps_ext)
  apply (induct_tac n rule: nat_induct2)
  apply (simp add: 0)
  apply (simp add: 1 del: One_nat_def)
  apply (rename_tac m, cut_tac f="λa. a $ m" in arg_cong [OF 2])
  apply (simp add: nat_add_1_add_1 fps_sin_nth_add_2
              del: One_nat_def of_nat_Suc of_nat_add add_2_eq_Suc')
  apply (subst minus_divide_left)
  apply (subst nonzero_eq_divide_eq)
  apply (simp del: of_nat_add of_nat_Suc)
  apply (simp only: ac_simps)
  done

lemma eq_fps_cos:
  assumes 0: "a $ 0 = 1"
    and 1: "a $ 1 = 0"
    and 2: "fps_deriv (fps_deriv a) = - (fps_const c * fps_const c * a)"
  shows "a = fps_cos c"
  apply (rule fps_ext)
  apply (induct_tac n rule: nat_induct2)
  apply (simp add: 0)
  apply (simp add: 1 del: One_nat_def)
  apply (rename_tac m, cut_tac f="λa. a $ m" in arg_cong [OF 2])
  apply (simp add: nat_add_1_add_1 fps_cos_nth_add_2
              del: One_nat_def of_nat_Suc of_nat_add add_2_eq_Suc')
  apply (subst minus_divide_left)
  apply (subst nonzero_eq_divide_eq)
  apply (simp del: of_nat_add of_nat_Suc)
  apply (simp only: ac_simps)
  done

lemma mult_nth_0 [simp]: "(a * b) $ 0 = a $ 0 * b $ 0"
  by (simp add: fps_mult_nth)

lemma mult_nth_1 [simp]: "(a * b) $ 1 = a $ 0 * b $ 1 + a $ 1 * b $ 0"
  by (simp add: fps_mult_nth)

lemma fps_sin_add: "fps_sin (a + b) = fps_sin a * fps_cos b + fps_cos a * fps_sin b"
  apply (rule eq_fps_sin [symmetric], simp, simp del: One_nat_def)
  apply (simp del: fps_const_neg fps_const_add fps_const_mult
              add: fps_const_add [symmetric] fps_const_neg [symmetric]
                   fps_sin_deriv fps_cos_deriv algebra_simps)
  done

lemma fps_cos_add: "fps_cos (a + b) = fps_cos a * fps_cos b - fps_sin a * fps_sin b"
  apply (rule eq_fps_cos [symmetric], simp, simp del: One_nat_def)
  apply (simp del: fps_const_neg fps_const_add fps_const_mult
              add: fps_const_add [symmetric] fps_const_neg [symmetric]
                   fps_sin_deriv fps_cos_deriv algebra_simps)
  done

lemma fps_sin_even: "fps_sin (- c) = - fps_sin c"
  by (auto simp add: fps_eq_iff fps_sin_def)

lemma fps_cos_odd: "fps_cos (- c) = fps_cos c"
  by (auto simp add: fps_eq_iff fps_cos_def)

definition "fps_tan c = fps_sin c / fps_cos c"

lemma fps_tan_deriv: "fps_deriv (fps_tan c) = fps_const c / (fps_cos c)2"
proof -
  have th0: "fps_cos c $ 0 ≠ 0" by (simp add: fps_cos_def)
  from this have "fps_cos c ≠ 0" by (intro notI) simp
  hence "fps_deriv (fps_tan c) =
           fps_const c * (fps_cos c^2 + fps_sin c^2) / (fps_cos c^2)"
    by (simp add: fps_tan_def fps_divide_deriv power2_eq_square algebra_simps
                  fps_sin_deriv fps_cos_deriv fps_const_neg[symmetric] div_mult_swap
             del: fps_const_neg)
  also note fps_sin_cos_sum_of_squares
  finally show ?thesis by simp
qed

text ‹Connection to E c over the complex numbers --- Euler and de Moivre.›

lemma Eii_sin_cos: "E (ii * c) = fps_cos c + fps_const ii * fps_sin c"
  (is "?l = ?r")
proof -
  have "?l $ n = ?r $ n" for n
  proof (cases "even n")
    case True
    then obtain m where m: "n = 2 * m" ..
    show ?thesis
      by (simp add: m fps_sin_def fps_cos_def power_mult_distrib power_mult power_minus [of "c ^ 2"])
  next
    case False
    then obtain m where m: "n = 2 * m + 1" ..
    show ?thesis
      by (simp add: m fps_sin_def fps_cos_def power_mult_distrib
        power_mult power_minus [of "c ^ 2"])
  qed
  then show ?thesis
    by (simp add: fps_eq_iff)
qed

lemma E_minus_ii_sin_cos: "E (- (ii * c)) = fps_cos c - fps_const ii * fps_sin c"
  unfolding minus_mult_right Eii_sin_cos by (simp add: fps_sin_even fps_cos_odd)

lemma fps_const_minus: "fps_const (c::'a::group_add) - fps_const d = fps_const (c - d)"
  by (fact fps_const_sub)

lemma fps_numeral_fps_const: "numeral i = fps_const (numeral i :: 'a::comm_ring_1)"
  by (fact numeral_fps_const) (* FIXME: duplicate *)

lemma fps_cos_Eii: "fps_cos c = (E (ii * c) + E (- ii * c)) / fps_const 2"
proof -
  have th: "fps_cos c + fps_cos c = fps_cos c * fps_const 2"
    by (simp add: numeral_fps_const)
  show ?thesis
    unfolding Eii_sin_cos minus_mult_commute
    by (simp add: fps_sin_even fps_cos_odd numeral_fps_const fps_divide_unit fps_const_inverse th)
qed

lemma fps_sin_Eii: "fps_sin c = (E (ii * c) - E (- ii * c)) / fps_const (2*ii)"
proof -
  have th: "fps_const 𝗂 * fps_sin c + fps_const 𝗂 * fps_sin c = fps_sin c * fps_const (2 * ii)"
    by (simp add: fps_eq_iff numeral_fps_const)
  show ?thesis
    unfolding Eii_sin_cos minus_mult_commute
    by (simp add: fps_sin_even fps_cos_odd fps_divide_unit fps_const_inverse th)
qed

lemma fps_tan_Eii:
  "fps_tan c = (E (ii * c) - E (- ii * c)) / (fps_const ii * (E (ii * c) + E (- ii * c)))"
  unfolding fps_tan_def fps_sin_Eii fps_cos_Eii mult_minus_left E_neg
  apply (simp add: fps_divide_unit fps_inverse_mult fps_const_mult[symmetric] fps_const_inverse del: fps_const_mult)
  apply simp
  done

lemma fps_demoivre:
  "(fps_cos a + fps_const ii * fps_sin a)^n =
    fps_cos (of_nat n * a) + fps_const ii * fps_sin (of_nat n * a)"
  unfolding Eii_sin_cos[symmetric] E_power_mult
  by (simp add: ac_simps)


subsection ‹Hypergeometric series›

(* TODO: Rename this *)
definition "F as bs (c::'a::{field_char_0,field}) =
  Abs_fps (λn. (foldl (λr a. r* pochhammer a n) 1 as * c^n) /
    (foldl (λr b. r * pochhammer b n) 1 bs * of_nat (fact n)))"

lemma F_nth[simp]: "F as bs c $ n =
  (foldl (λr a. r* pochhammer a n) 1 as * c^n) /
    (foldl (λr b. r * pochhammer b n) 1 bs * of_nat (fact n))"
  by (simp add: F_def)

lemma foldl_mult_start:
  fixes v :: "'a::comm_ring_1"
  shows "foldl (λr x. r * f x) v as * x = foldl (λr x. r * f x) (v * x) as "
  by (induct as arbitrary: x v) (auto simp add: algebra_simps)

lemma foldr_mult_foldl:
  fixes v :: "'a::comm_ring_1"
  shows "foldr (λx r. r * f x) as v = foldl (λr x. r * f x) v as"
  by (induct as arbitrary: v) (auto simp add: foldl_mult_start)

lemma F_nth_alt:
  "F as bs c $ n = foldr (λa r. r * pochhammer a n) as (c ^ n) /
    foldr (λb r. r * pochhammer b n) bs (of_nat (fact n))"
  by (simp add: foldl_mult_start foldr_mult_foldl)

lemma F_E[simp]: "F [] [] c = E c"
  by (simp add: fps_eq_iff)

lemma F_1_0[simp]: "F [1] [] c = 1/(1 - fps_const c * X)"
proof -
  let ?a = "(Abs_fps (λn. 1)) oo (fps_const c * X)"
  have th0: "(fps_const c * X) $ 0 = 0" by simp
  show ?thesis unfolding gp[OF th0, symmetric]
    by (auto simp add: fps_eq_iff pochhammer_fact[symmetric]
      fps_compose_nth power_mult_distrib cond_value_iff setsum.delta' cong del: if_weak_cong)
qed

lemma F_B[simp]: "F [-a] [] (- 1) = fps_binomial a"
  by (simp add: fps_eq_iff gbinomial_pochhammer algebra_simps)

lemma F_0[simp]: "F as bs c $ 0 = 1"
  apply simp
  apply (subgoal_tac "∀as. foldl (λ(r::'a) (a::'a). r) 1 as = 1")
  apply auto
  apply (induct_tac as)
  apply auto
  done

lemma foldl_prod_prod:
  "foldl (λ(r::'b::comm_ring_1) (x::'a::comm_ring_1). r * f x) v as * foldl (λr x. r * g x) w as =
    foldl (λr x. r * f x * g x) (v * w) as"
  by (induct as arbitrary: v w) (auto simp add: algebra_simps)


lemma F_rec:
  "F as bs c $ Suc n = ((foldl (λr a. r* (a + of_nat n)) c as) /
    (foldl (λr b. r * (b + of_nat n)) (of_nat (Suc n)) bs )) * F as bs c $ n"
  apply (simp del: of_nat_Suc of_nat_add fact_Suc)
  apply (simp add: foldl_mult_start del: fact_Suc of_nat_Suc)
  unfolding foldl_prod_prod[unfolded foldl_mult_start] pochhammer_Suc
  apply (simp add: algebra_simps of_nat_mult)
  done

lemma XD_nth[simp]: "XD a $ n = (if n = 0 then 0 else of_nat n * a$n)"
  by (simp add: XD_def)

lemma XD_0th[simp]: "XD a $ 0 = 0"
  by simp
lemma XD_Suc[simp]:" XD a $ Suc n = of_nat (Suc n) * a $ Suc n"
  by simp

definition "XDp c a = XD a + fps_const c * a"

lemma XDp_nth[simp]: "XDp c a $ n = (c + of_nat n) * a$n"
  by (simp add: XDp_def algebra_simps)

lemma XDp_commute: "XDp b ∘ XDp (c::'a::comm_ring_1) = XDp c ∘ XDp b"
  by (auto simp add: XDp_def fun_eq_iff fps_eq_iff algebra_simps)

lemma XDp0 [simp]: "XDp 0 = XD"
  by (simp add: fun_eq_iff fps_eq_iff)

lemma XDp_fps_integral [simp]: "XDp 0 (fps_integral a c) = X * a"
  by (simp add: fps_eq_iff fps_integral_def)

lemma F_minus_nat:
  "F [- of_nat n] [- of_nat (n + m)] (c::'a::{field_char_0,field}) $ k =
    (if k ≤ n then
      pochhammer (- of_nat n) k * c ^ k / (pochhammer (- of_nat (n + m)) k * of_nat (fact k))
     else 0)"
  "F [- of_nat m] [- of_nat (m + n)] (c::'a::{field_char_0,field}) $ k =
    (if k ≤ m then
      pochhammer (- of_nat m) k * c ^ k / (pochhammer (- of_nat (m + n)) k * of_nat (fact k))
     else 0)"
  by (auto simp add: pochhammer_eq_0_iff)

lemma setsum_eq_if: "setsum f {(n::nat) .. m} = (if m < n then 0 else f n + setsum f {n+1 .. m})"
  apply simp
  apply (subst setsum.insert[symmetric])
  apply (auto simp add: not_less setsum_head_Suc)
  done

lemma pochhammer_rec_if: "pochhammer a n = (if n = 0 then 1 else a * pochhammer (a + 1) (n - 1))"
  by (cases n) (simp_all add: pochhammer_rec)

lemma XDp_foldr_nth [simp]: "foldr (λc r. XDp c ∘ r) cs (λc. XDp c a) c0 $ n =
    foldr (λc r. (c + of_nat n) * r) cs (c0 + of_nat n) * a$n"
  by (induct cs arbitrary: c0) (auto simp add: algebra_simps)

lemma genric_XDp_foldr_nth:
  assumes f: "∀n c a. f c a $ n = (of_nat n + k c) * a$n"
  shows "foldr (λc r. f c ∘ r) cs (λc. g c a) c0 $ n =
    foldr (λc r. (k c + of_nat n) * r) cs (g c0 a $ n)"
  by (induct cs arbitrary: c0) (auto simp add: algebra_simps f)

lemma dist_less_imp_nth_equal:
  assumes "dist f g < inverse (2 ^ i)"
    and"j ≤ i"
  shows "f $ j = g $ j"
proof (rule ccontr)
  assume "f $ j ≠ g $ j"
  hence "f ≠ g" by auto
  with assms have "i < subdegree (f - g)"
    by (simp add: if_split_asm dist_fps_def)
  also have "… ≤ j"
    using ‹f $ j ≠ g $ j› by (intro subdegree_leI) simp_all
  finally show False using ‹j ≤ i› by simp
qed

lemma nth_equal_imp_dist_less:
  assumes "⋀j. j ≤ i ⟹ f $ j = g $ j"
  shows "dist f g < inverse (2 ^ i)"
proof (cases "f = g")
  case True
  then show ?thesis by simp
next
  case False
  with assms have "dist f g = inverse (2 ^ subdegree (f - g))"
    by (simp add: if_split_asm dist_fps_def)
  moreover
  from assms and False have "i < subdegree (f - g)"
    by (intro subdegree_greaterI) simp_all
  ultimately show ?thesis by simp
qed

lemma dist_less_eq_nth_equal: "dist f g < inverse (2 ^ i) ⟷ (∀j ≤ i. f $ j = g $ j)"
  using dist_less_imp_nth_equal nth_equal_imp_dist_less by blast

instance fps :: (comm_ring_1) complete_space
proof
  fix X :: "nat ⇒ 'a fps"
  assume "Cauchy X"
  obtain M where M: "∀i. ∀m ≥ M i. ∀j ≤ i. X (M i) $ j = X m $ j"
  proof -
    have "∃M. ∀m ≥ M. ∀j≤i. X M $ j = X m $ j" for i
    proof -
      have "0 < inverse ((2::real)^i)" by simp
      from metric_CauchyD[OF ‹Cauchy X› this] dist_less_imp_nth_equal
      show ?thesis by blast
    qed
    then show ?thesis using that by metis
  qed

  show "convergent X"
  proof (rule convergentI)
    show "X ⇢ Abs_fps (λi. X (M i) $ i)"
      unfolding tendsto_iff
    proof safe
      fix e::real assume e: "0 < e"
      have "(λn. inverse (2 ^ n) :: real) ⇢ 0" by (rule LIMSEQ_inverse_realpow_zero) simp_all
      from this and e have "eventually (λi. inverse (2 ^ i) < e) sequentially"
        by (rule order_tendstoD)
      then obtain i where "inverse (2 ^ i) < e"
        by (auto simp: eventually_sequentially)
      have "eventually (λx. M i ≤ x) sequentially"
        by (auto simp: eventually_sequentially)
      then show "eventually (λx. dist (X x) (Abs_fps (λi. X (M i) $ i)) < e) sequentially"
      proof eventually_elim
        fix x
        assume x: "M i ≤ x"
        have "X (M i) $ j = X (M j) $ j" if "j ≤ i" for j
          using M that by (metis nat_le_linear)
        with x have "dist (X x) (Abs_fps (λj. X (M j) $ j)) < inverse (2 ^ i)"
          using M by (force simp: dist_less_eq_nth_equal)
        also note ‹inverse (2 ^ i) < e›
        finally show "dist (X x) (Abs_fps (λj. X (M j) $ j)) < e" .
      qed
    qed
  qed
qed

end