src/HOL/Library/Formal_Power_Series.thy
author chaieb
Thu Jul 23 22:25:09 2009 +0200 (2009-07-23)
changeset 32162 c6a045559ce6
parent 32161 abda97d2deea
child 32413 b3241e8e9716
permissions -rw-r--r--
fixed proof --- fact_setprod removed for fact_altdef_nat
chaieb@29687
     1
(*  Title:      Formal_Power_Series.thy
chaieb@29687
     2
    Author:     Amine Chaieb, University of Cambridge
chaieb@29687
     3
*)
chaieb@29687
     4
chaieb@29687
     5
header{* A formalization of formal power series *}
chaieb@29687
     6
chaieb@29687
     7
theory Formal_Power_Series
chaieb@32157
     8
imports Complex_Main Binomial
chaieb@29687
     9
begin
chaieb@29687
    10
chaieb@31968
    11
huffman@29906
    12
subsection {* The type of formal power series*}
chaieb@29687
    13
huffman@29911
    14
typedef (open) 'a fps = "{f :: nat \<Rightarrow> 'a. True}"
huffman@29911
    15
  morphisms fps_nth Abs_fps
chaieb@29687
    16
  by simp
chaieb@29687
    17
huffman@29911
    18
notation fps_nth (infixl "$" 75)
huffman@29911
    19
huffman@29911
    20
lemma expand_fps_eq: "p = q \<longleftrightarrow> (\<forall>n. p $ n = q $ n)"
huffman@29911
    21
  by (simp add: fps_nth_inject [symmetric] expand_fun_eq)
huffman@29911
    22
huffman@29911
    23
lemma fps_ext: "(\<And>n. p $ n = q $ n) \<Longrightarrow> p = q"
huffman@29911
    24
  by (simp add: expand_fps_eq)
huffman@29911
    25
huffman@29911
    26
lemma fps_nth_Abs_fps [simp]: "Abs_fps f $ n = f n"
huffman@29911
    27
  by (simp add: Abs_fps_inverse)
huffman@29911
    28
chaieb@29687
    29
text{* Definition of the basic elements 0 and 1 and the basic operations of addition, negation and multiplication *}
chaieb@29687
    30
chaieb@29687
    31
instantiation fps :: (zero)  zero
chaieb@29687
    32
begin
chaieb@29687
    33
huffman@29911
    34
definition fps_zero_def:
huffman@29911
    35
  "0 = Abs_fps (\<lambda>n. 0)"
huffman@29911
    36
chaieb@29687
    37
instance ..
chaieb@29687
    38
end
chaieb@29687
    39
huffman@29911
    40
lemma fps_zero_nth [simp]: "0 $ n = 0"
huffman@29911
    41
  unfolding fps_zero_def by simp
huffman@29911
    42
chaieb@29687
    43
instantiation fps :: ("{one,zero}")  one
chaieb@29687
    44
begin
chaieb@29687
    45
huffman@29911
    46
definition fps_one_def:
huffman@29911
    47
  "1 = Abs_fps (\<lambda>n. if n = 0 then 1 else 0)"
huffman@29911
    48
chaieb@29687
    49
instance ..
chaieb@29687
    50
end
chaieb@29687
    51
huffman@30488
    52
lemma fps_one_nth [simp]: "1 $ n = (if n = 0 then 1 else 0)"
huffman@29911
    53
  unfolding fps_one_def by simp
huffman@29911
    54
chaieb@29687
    55
instantiation fps :: (plus)  plus
chaieb@29687
    56
begin
chaieb@29687
    57
huffman@29911
    58
definition fps_plus_def:
huffman@29911
    59
  "op + = (\<lambda>f g. Abs_fps (\<lambda>n. f $ n + g $ n))"
huffman@29911
    60
chaieb@29687
    61
instance ..
chaieb@29687
    62
end
chaieb@29687
    63
huffman@29911
    64
lemma fps_add_nth [simp]: "(f + g) $ n = f $ n + g $ n"
huffman@29911
    65
  unfolding fps_plus_def by simp
huffman@29911
    66
huffman@29911
    67
instantiation fps :: (minus) minus
chaieb@29687
    68
begin
chaieb@29687
    69
huffman@29911
    70
definition fps_minus_def:
huffman@29911
    71
  "op - = (\<lambda>f g. Abs_fps (\<lambda>n. f $ n - g $ n))"
huffman@29911
    72
chaieb@29687
    73
instance ..
chaieb@29687
    74
end
chaieb@29687
    75
huffman@29911
    76
lemma fps_sub_nth [simp]: "(f - g) $ n = f $ n - g $ n"
huffman@29911
    77
  unfolding fps_minus_def by simp
huffman@29911
    78
huffman@29911
    79
instantiation fps :: (uminus) uminus
chaieb@29687
    80
begin
chaieb@29687
    81
huffman@29911
    82
definition fps_uminus_def:
huffman@29911
    83
  "uminus = (\<lambda>f. Abs_fps (\<lambda>n. - (f $ n)))"
huffman@29911
    84
chaieb@29687
    85
instance ..
chaieb@29687
    86
end
chaieb@29687
    87
huffman@29911
    88
lemma fps_neg_nth [simp]: "(- f) $ n = - (f $ n)"
huffman@29911
    89
  unfolding fps_uminus_def by simp
huffman@29911
    90
chaieb@29687
    91
instantiation fps :: ("{comm_monoid_add, times}")  times
chaieb@29687
    92
begin
chaieb@29687
    93
huffman@29911
    94
definition fps_times_def:
huffman@29911
    95
  "op * = (\<lambda>f g. Abs_fps (\<lambda>n. \<Sum>i=0..n. f $ i * g $ (n - i)))"
huffman@29911
    96
chaieb@29687
    97
instance ..
chaieb@29687
    98
end
chaieb@29687
    99
huffman@29911
   100
lemma fps_mult_nth: "(f * g) $ n = (\<Sum>i=0..n. f$i * g$(n - i))"
huffman@29911
   101
  unfolding fps_times_def by simp
chaieb@29687
   102
huffman@29911
   103
declare atLeastAtMost_iff[presburger]
chaieb@29687
   104
declare Bex_def[presburger]
chaieb@29687
   105
declare Ball_def[presburger]
chaieb@29687
   106
huffman@29913
   107
lemma mult_delta_left:
huffman@29913
   108
  fixes x y :: "'a::mult_zero"
huffman@29913
   109
  shows "(if b then x else 0) * y = (if b then x * y else 0)"
huffman@29913
   110
  by simp
huffman@29913
   111
huffman@29913
   112
lemma mult_delta_right:
huffman@29913
   113
  fixes x y :: "'a::mult_zero"
huffman@29913
   114
  shows "x * (if b then y else 0) = (if b then x * y else 0)"
huffman@29913
   115
  by simp
huffman@29913
   116
chaieb@29687
   117
lemma cond_value_iff: "f (if b then x else y) = (if b then f x else f y)"
chaieb@29687
   118
  by auto
chaieb@29687
   119
lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
chaieb@29687
   120
  by auto
chaieb@29687
   121
huffman@30488
   122
subsection{* Formal power series form a commutative ring with unity, if the range of sequences
chaieb@29687
   123
  they represent is a commutative ring with unity*}
chaieb@29687
   124
huffman@29911
   125
instance fps :: (semigroup_add) semigroup_add
chaieb@29687
   126
proof
chaieb@29687
   127
  fix a b c :: "'a fps" show "a + b + c = a + (b + c)"
huffman@29911
   128
    by (simp add: fps_ext add_assoc)
huffman@29911
   129
qed
huffman@29911
   130
huffman@29911
   131
instance fps :: (ab_semigroup_add) ab_semigroup_add
huffman@29911
   132
proof
huffman@29911
   133
  fix a b :: "'a fps" show "a + b = b + a"
huffman@29911
   134
    by (simp add: fps_ext add_commute)
chaieb@29687
   135
qed
chaieb@29687
   136
huffman@29911
   137
lemma fps_mult_assoc_lemma:
huffman@29911
   138
  fixes k :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
huffman@29911
   139
  shows "(\<Sum>j=0..k. \<Sum>i=0..j. f i (j - i) (n - j)) =
huffman@29911
   140
         (\<Sum>j=0..k. \<Sum>i=0..k - j. f j i (n - j - i))"
huffman@29911
   141
proof (induct k)
huffman@29911
   142
  case 0 show ?case by simp
huffman@29911
   143
next
huffman@29911
   144
  case (Suc k) thus ?case
huffman@29911
   145
    by (simp add: Suc_diff_le setsum_addf add_assoc
huffman@29911
   146
             cong: strong_setsum_cong)
huffman@29911
   147
qed
chaieb@29687
   148
huffman@29911
   149
instance fps :: (semiring_0) semigroup_mult
chaieb@29687
   150
proof
chaieb@29687
   151
  fix a b c :: "'a fps"
huffman@29911
   152
  show "(a * b) * c = a * (b * c)"
huffman@29911
   153
  proof (rule fps_ext)
huffman@29911
   154
    fix n :: nat
huffman@29911
   155
    have "(\<Sum>j=0..n. \<Sum>i=0..j. a$i * b$(j - i) * c$(n - j)) =
huffman@29911
   156
          (\<Sum>j=0..n. \<Sum>i=0..n - j. a$j * b$i * c$(n - j - i))"
huffman@29911
   157
      by (rule fps_mult_assoc_lemma)
huffman@29911
   158
    thus "((a * b) * c) $ n = (a * (b * c)) $ n"
huffman@29911
   159
      by (simp add: fps_mult_nth setsum_right_distrib
huffman@29911
   160
                    setsum_left_distrib mult_assoc)
huffman@29911
   161
  qed
huffman@29911
   162
qed
huffman@29911
   163
huffman@29911
   164
lemma fps_mult_commute_lemma:
huffman@29911
   165
  fixes n :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
huffman@29911
   166
  shows "(\<Sum>i=0..n. f i (n - i)) = (\<Sum>i=0..n. f (n - i) i)"
huffman@29911
   167
proof (rule setsum_reindex_cong)
huffman@29911
   168
  show "inj_on (\<lambda>i. n - i) {0..n}"
huffman@29911
   169
    by (rule inj_onI) simp
huffman@29911
   170
  show "{0..n} = (\<lambda>i. n - i) ` {0..n}"
huffman@29911
   171
    by (auto, rule_tac x="n - x" in image_eqI, simp_all)
huffman@29911
   172
next
huffman@29911
   173
  fix i assume "i \<in> {0..n}"
huffman@29911
   174
  hence "n - (n - i) = i" by simp
huffman@29911
   175
  thus "f (n - i) i = f (n - i) (n - (n - i))" by simp
huffman@29911
   176
qed
huffman@29911
   177
huffman@29911
   178
instance fps :: (comm_semiring_0) ab_semigroup_mult
huffman@29911
   179
proof
huffman@29911
   180
  fix a b :: "'a fps"
huffman@29911
   181
  show "a * b = b * a"
huffman@29911
   182
  proof (rule fps_ext)
huffman@29911
   183
    fix n :: nat
huffman@29911
   184
    have "(\<Sum>i=0..n. a$i * b$(n - i)) = (\<Sum>i=0..n. a$(n - i) * b$i)"
huffman@29911
   185
      by (rule fps_mult_commute_lemma)
huffman@29911
   186
    thus "(a * b) $ n = (b * a) $ n"
huffman@29911
   187
      by (simp add: fps_mult_nth mult_commute)
chaieb@29687
   188
  qed
chaieb@29687
   189
qed
chaieb@29687
   190
huffman@29911
   191
instance fps :: (monoid_add) monoid_add
chaieb@29687
   192
proof
chaieb@29687
   193
  fix a :: "'a fps" show "0 + a = a "
huffman@29911
   194
    by (simp add: fps_ext)
chaieb@29687
   195
next
chaieb@29687
   196
  fix a :: "'a fps" show "a + 0 = a "
huffman@29911
   197
    by (simp add: fps_ext)
chaieb@29687
   198
qed
chaieb@29687
   199
huffman@29911
   200
instance fps :: (comm_monoid_add) comm_monoid_add
chaieb@29687
   201
proof
chaieb@29687
   202
  fix a :: "'a fps" show "0 + a = a "
huffman@29911
   203
    by (simp add: fps_ext)
chaieb@29687
   204
qed
chaieb@29687
   205
huffman@29911
   206
instance fps :: (semiring_1) monoid_mult
chaieb@29687
   207
proof
chaieb@29687
   208
  fix a :: "'a fps" show "1 * a = a"
huffman@29913
   209
    by (simp add: fps_ext fps_mult_nth mult_delta_left setsum_delta)
chaieb@29687
   210
next
huffman@29911
   211
  fix a :: "'a fps" show "a * 1 = a"
huffman@29913
   212
    by (simp add: fps_ext fps_mult_nth mult_delta_right setsum_delta')
chaieb@29687
   213
qed
chaieb@29687
   214
huffman@29911
   215
instance fps :: (cancel_semigroup_add) cancel_semigroup_add
huffman@29911
   216
proof
huffman@29911
   217
  fix a b c :: "'a fps"
huffman@29911
   218
  assume "a + b = a + c" then show "b = c"
huffman@29911
   219
    by (simp add: expand_fps_eq)
huffman@29911
   220
next
huffman@29911
   221
  fix a b c :: "'a fps"
huffman@29911
   222
  assume "b + a = c + a" then show "b = c"
huffman@29911
   223
    by (simp add: expand_fps_eq)
huffman@29911
   224
qed
chaieb@29687
   225
huffman@29911
   226
instance fps :: (cancel_ab_semigroup_add) cancel_ab_semigroup_add
huffman@29911
   227
proof
huffman@29911
   228
  fix a b c :: "'a fps"
huffman@29911
   229
  assume "a + b = a + c" then show "b = c"
huffman@29911
   230
    by (simp add: expand_fps_eq)
huffman@29911
   231
qed
chaieb@29687
   232
huffman@29911
   233
instance fps :: (cancel_comm_monoid_add) cancel_comm_monoid_add ..
huffman@29911
   234
huffman@29911
   235
instance fps :: (group_add) group_add
chaieb@29687
   236
proof
chaieb@29687
   237
  fix a :: "'a fps" show "- a + a = 0"
huffman@29911
   238
    by (simp add: fps_ext)
chaieb@29687
   239
next
chaieb@29687
   240
  fix a b :: "'a fps" show "a - b = a + - b"
huffman@29911
   241
    by (simp add: fps_ext diff_minus)
chaieb@29687
   242
qed
chaieb@29687
   243
huffman@29911
   244
instance fps :: (ab_group_add) ab_group_add
huffman@29911
   245
proof
huffman@29911
   246
  fix a :: "'a fps"
huffman@29911
   247
  show "- a + a = 0"
huffman@29911
   248
    by (simp add: fps_ext)
huffman@29911
   249
next
huffman@29911
   250
  fix a b :: "'a fps"
huffman@29911
   251
  show "a - b = a + - b"
huffman@29911
   252
    by (simp add: fps_ext)
huffman@29911
   253
qed
chaieb@29687
   254
huffman@29911
   255
instance fps :: (zero_neq_one) zero_neq_one
huffman@29911
   256
  by default (simp add: expand_fps_eq)
chaieb@29687
   257
huffman@29911
   258
instance fps :: (semiring_0) semiring
chaieb@29687
   259
proof
chaieb@29687
   260
  fix a b c :: "'a fps"
huffman@29911
   261
  show "(a + b) * c = a * c + b * c"
huffman@29911
   262
    by (simp add: expand_fps_eq fps_mult_nth left_distrib setsum_addf)
chaieb@29687
   263
next
chaieb@29687
   264
  fix a b c :: "'a fps"
huffman@29911
   265
  show "a * (b + c) = a * b + a * c"
huffman@29911
   266
    by (simp add: expand_fps_eq fps_mult_nth right_distrib setsum_addf)
chaieb@29687
   267
qed
chaieb@29687
   268
huffman@29911
   269
instance fps :: (semiring_0) semiring_0
chaieb@29687
   270
proof
huffman@29911
   271
  fix a:: "'a fps" show "0 * a = 0"
huffman@29911
   272
    by (simp add: fps_ext fps_mult_nth)
chaieb@29687
   273
next
huffman@29911
   274
  fix a:: "'a fps" show "a * 0 = 0"
huffman@29911
   275
    by (simp add: fps_ext fps_mult_nth)
chaieb@29687
   276
qed
huffman@29911
   277
huffman@29911
   278
instance fps :: (semiring_0_cancel) semiring_0_cancel ..
huffman@29911
   279
huffman@29906
   280
subsection {* Selection of the nth power of the implicit variable in the infinite sum*}
chaieb@29687
   281
chaieb@29687
   282
lemma fps_nonzero_nth: "f \<noteq> 0 \<longleftrightarrow> (\<exists> n. f $n \<noteq> 0)"
huffman@29911
   283
  by (simp add: expand_fps_eq)
chaieb@29687
   284
huffman@29911
   285
lemma fps_nonzero_nth_minimal:
huffman@29911
   286
  "f \<noteq> 0 \<longleftrightarrow> (\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0))"
huffman@29911
   287
proof
huffman@29911
   288
  let ?n = "LEAST n. f $ n \<noteq> 0"
huffman@29911
   289
  assume "f \<noteq> 0"
huffman@29911
   290
  then have "\<exists>n. f $ n \<noteq> 0"
huffman@29911
   291
    by (simp add: fps_nonzero_nth)
huffman@29911
   292
  then have "f $ ?n \<noteq> 0"
huffman@29911
   293
    by (rule LeastI_ex)
huffman@29911
   294
  moreover have "\<forall>m<?n. f $ m = 0"
huffman@29911
   295
    by (auto dest: not_less_Least)
huffman@29911
   296
  ultimately have "f $ ?n \<noteq> 0 \<and> (\<forall>m<?n. f $ m = 0)" ..
huffman@29911
   297
  then show "\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0)" ..
huffman@29911
   298
next
huffman@29911
   299
  assume "\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0)"
huffman@29911
   300
  then show "f \<noteq> 0" by (auto simp add: expand_fps_eq)
chaieb@29687
   301
qed
chaieb@29687
   302
chaieb@29687
   303
lemma fps_eq_iff: "f = g \<longleftrightarrow> (\<forall>n. f $ n = g $n)"
huffman@29911
   304
  by (rule expand_fps_eq)
chaieb@29687
   305
huffman@30488
   306
lemma fps_setsum_nth: "(setsum f S) $ n = setsum (\<lambda>k. (f k) $ n) S"
huffman@29911
   307
proof (cases "finite S")
huffman@29911
   308
  assume "\<not> finite S" then show ?thesis by simp
huffman@29911
   309
next
huffman@29911
   310
  assume "finite S"
huffman@29911
   311
  then show ?thesis by (induct set: finite) auto
chaieb@29687
   312
qed
chaieb@29687
   313
huffman@29906
   314
subsection{* Injection of the basic ring elements and multiplication by scalars *}
chaieb@29687
   315
huffman@29911
   316
definition
huffman@29911
   317
  "fps_const c = Abs_fps (\<lambda>n. if n = 0 then c else 0)"
huffman@29911
   318
huffman@29911
   319
lemma fps_nth_fps_const [simp]: "fps_const c $ n = (if n = 0 then c else 0)"
huffman@29911
   320
  unfolding fps_const_def by simp
huffman@29911
   321
huffman@29911
   322
lemma fps_const_0_eq_0 [simp]: "fps_const 0 = 0"
huffman@29911
   323
  by (simp add: fps_ext)
huffman@29911
   324
huffman@29911
   325
lemma fps_const_1_eq_1 [simp]: "fps_const 1 = 1"
huffman@29911
   326
  by (simp add: fps_ext)
huffman@29911
   327
huffman@29911
   328
lemma fps_const_neg [simp]: "- (fps_const (c::'a::ring)) = fps_const (- c)"
huffman@29911
   329
  by (simp add: fps_ext)
huffman@29911
   330
huffman@29911
   331
lemma fps_const_add [simp]: "fps_const (c::'a\<Colon>monoid_add) + fps_const d = fps_const (c + d)"
huffman@29911
   332
  by (simp add: fps_ext)
chaieb@31369
   333
lemma fps_const_sub [simp]: "fps_const (c::'a\<Colon>group_add) - fps_const d = fps_const (c - d)"
chaieb@31369
   334
  by (simp add: fps_ext)
chaieb@29687
   335
lemma fps_const_mult[simp]: "fps_const (c::'a\<Colon>ring) * fps_const d = fps_const (c * d)"
huffman@29911
   336
  by (simp add: fps_eq_iff fps_mult_nth setsum_0')
chaieb@29687
   337
chaieb@29687
   338
lemma fps_const_add_left: "fps_const (c::'a\<Colon>monoid_add) + f = Abs_fps (\<lambda>n. if n = 0 then c + f$0 else f$n)"
huffman@29911
   339
  by (simp add: fps_ext)
huffman@29911
   340
chaieb@29687
   341
lemma fps_const_add_right: "f + fps_const (c::'a\<Colon>monoid_add) = Abs_fps (\<lambda>n. if n = 0 then f$0 + c else f$n)"
huffman@29911
   342
  by (simp add: fps_ext)
chaieb@29687
   343
chaieb@29687
   344
lemma fps_const_mult_left: "fps_const (c::'a\<Colon>semiring_0) * f = Abs_fps (\<lambda>n. c * f$n)"
huffman@29911
   345
  unfolding fps_eq_iff fps_mult_nth
huffman@29913
   346
  by (simp add: fps_const_def mult_delta_left setsum_delta)
huffman@29911
   347
chaieb@29687
   348
lemma fps_const_mult_right: "f * fps_const (c::'a\<Colon>semiring_0) = Abs_fps (\<lambda>n. f$n * c)"
huffman@29911
   349
  unfolding fps_eq_iff fps_mult_nth
huffman@29913
   350
  by (simp add: fps_const_def mult_delta_right setsum_delta')
chaieb@29687
   351
huffman@29911
   352
lemma fps_mult_left_const_nth [simp]: "(fps_const (c::'a::semiring_1) * f)$n = c* f$n"
huffman@29913
   353
  by (simp add: fps_mult_nth mult_delta_left setsum_delta)
chaieb@29687
   354
huffman@29911
   355
lemma fps_mult_right_const_nth [simp]: "(f * fps_const (c::'a::semiring_1))$n = f$n * c"
huffman@29913
   356
  by (simp add: fps_mult_nth mult_delta_right setsum_delta')
chaieb@29687
   357
huffman@29906
   358
subsection {* Formal power series form an integral domain*}
chaieb@29687
   359
huffman@29911
   360
instance fps :: (ring) ring ..
chaieb@29687
   361
huffman@29911
   362
instance fps :: (ring_1) ring_1
huffman@29911
   363
  by (intro_classes, auto simp add: diff_minus left_distrib)
chaieb@29687
   364
huffman@29911
   365
instance fps :: (comm_ring_1) comm_ring_1
huffman@29911
   366
  by (intro_classes, auto simp add: diff_minus left_distrib)
chaieb@29687
   367
huffman@29911
   368
instance fps :: (ring_no_zero_divisors) ring_no_zero_divisors
chaieb@29687
   369
proof
chaieb@29687
   370
  fix a b :: "'a fps"
chaieb@29687
   371
  assume a0: "a \<noteq> 0" and b0: "b \<noteq> 0"
chaieb@29687
   372
  then obtain i j where i: "a$i\<noteq>0" "\<forall>k<i. a$k=0"
chaieb@29687
   373
    and j: "b$j \<noteq>0" "\<forall>k<j. b$k =0" unfolding fps_nonzero_nth_minimal
chaieb@29687
   374
    by blast+
huffman@29911
   375
  have "(a * b) $ (i+j) = (\<Sum>k=0..i+j. a$k * b$(i+j-k))"
chaieb@29687
   376
    by (rule fps_mult_nth)
huffman@29911
   377
  also have "\<dots> = (a$i * b$(i+j-i)) + (\<Sum>k\<in>{0..i+j}-{i}. a$k * b$(i+j-k))"
huffman@29911
   378
    by (rule setsum_diff1') simp_all
huffman@29911
   379
  also have "(\<Sum>k\<in>{0..i+j}-{i}. a$k * b$(i+j-k)) = 0"
huffman@29911
   380
    proof (rule setsum_0' [rule_format])
huffman@29911
   381
      fix k assume "k \<in> {0..i+j} - {i}"
huffman@29911
   382
      then have "k < i \<or> i+j-k < j" by auto
huffman@29911
   383
      then show "a$k * b$(i+j-k) = 0" using i j by auto
huffman@29911
   384
    qed
huffman@29911
   385
  also have "a$i * b$(i+j-i) + 0 = a$i * b$j" by simp
huffman@29911
   386
  also have "a$i * b$j \<noteq> 0" using i j by simp
huffman@29911
   387
  finally have "(a*b) $ (i+j) \<noteq> 0" .
chaieb@29687
   388
  then show "a*b \<noteq> 0" unfolding fps_nonzero_nth by blast
chaieb@29687
   389
qed
chaieb@29687
   390
huffman@29911
   391
instance fps :: (idom) idom ..
chaieb@29687
   392
chaieb@30746
   393
instantiation fps :: (comm_ring_1) number_ring
chaieb@30746
   394
begin
chaieb@30746
   395
definition number_of_fps_def: "(number_of k::'a fps) = of_int k"
chaieb@30746
   396
huffman@31273
   397
instance proof
huffman@31273
   398
qed (rule number_of_fps_def)
chaieb@30746
   399
end
chaieb@30746
   400
chaieb@31369
   401
lemma number_of_fps_const: "(number_of k::('a::comm_ring_1) fps) = fps_const (of_int k)"
chaieb@31369
   402
  
chaieb@31369
   403
proof(induct k rule: int_induct[where k=0])
chaieb@31369
   404
  case base thus ?case unfolding number_of_fps_def of_int_0 by simp
chaieb@31369
   405
next
chaieb@31369
   406
  case (step1 i) thus ?case unfolding number_of_fps_def 
chaieb@31369
   407
    by (simp add: fps_const_add[symmetric] del: fps_const_add)
chaieb@31369
   408
next
chaieb@31369
   409
  case (step2 i) thus ?case unfolding number_of_fps_def 
chaieb@31369
   410
    by (simp add: fps_const_sub[symmetric] del: fps_const_sub)
chaieb@31369
   411
qed
chaieb@31968
   412
subsection{* The eXtractor series X*}
chaieb@31968
   413
chaieb@31968
   414
lemma minus_one_power_iff: "(- (1::'a :: {comm_ring_1})) ^ n = (if even n then 1 else - 1)"
chaieb@31968
   415
  by (induct n, auto)
chaieb@31968
   416
chaieb@31968
   417
definition "X = Abs_fps (\<lambda>n. if n = 1 then 1 else 0)"
chaieb@31968
   418
lemma X_mult_nth[simp]: "(X * (f :: ('a::semiring_1) fps)) $n = (if n = 0 then 0 else f $ (n - 1))"
chaieb@31968
   419
proof-
chaieb@31968
   420
  {assume n: "n \<noteq> 0"
chaieb@31968
   421
    have fN: "finite {0 .. n}" by simp
chaieb@31968
   422
    have "(X * f) $n = (\<Sum>i = 0..n. X $ i * f $ (n - i))" by (simp add: fps_mult_nth)
chaieb@31968
   423
    also have "\<dots> = f $ (n - 1)"
chaieb@31968
   424
      using n by (simp add: X_def mult_delta_left setsum_delta [OF fN])
chaieb@31968
   425
  finally have ?thesis using n by simp }
chaieb@31968
   426
  moreover
chaieb@31968
   427
  {assume n: "n=0" hence ?thesis by (simp add: fps_mult_nth X_def)}
chaieb@31968
   428
  ultimately show ?thesis by blast
chaieb@31968
   429
qed
chaieb@31968
   430
chaieb@31968
   431
lemma X_mult_right_nth[simp]: "((f :: ('a::comm_semiring_1) fps) * X) $n = (if n = 0 then 0 else f $ (n - 1))"
chaieb@31968
   432
  by (metis X_mult_nth mult_commute)
chaieb@31968
   433
chaieb@31968
   434
lemma X_power_iff: "X^k = Abs_fps (\<lambda>n. if n = k then (1::'a::comm_ring_1) else 0)"
chaieb@31968
   435
proof(induct k)
chaieb@31968
   436
  case 0 thus ?case by (simp add: X_def fps_eq_iff)
chaieb@31968
   437
next
chaieb@31968
   438
  case (Suc k)
chaieb@31968
   439
  {fix m
chaieb@31968
   440
    have "(X^Suc k) $ m = (if m = 0 then (0::'a) else (X^k) $ (m - 1))"
chaieb@31968
   441
      by (simp add: power_Suc del: One_nat_def)
chaieb@31968
   442
    then     have "(X^Suc k) $ m = (if m = Suc k then (1::'a) else 0)"
chaieb@31968
   443
      using Suc.hyps by (auto cong del: if_weak_cong)}
chaieb@31968
   444
  then show ?case by (simp add: fps_eq_iff)
chaieb@31968
   445
qed
chaieb@31968
   446
chaieb@31968
   447
lemma X_power_mult_nth: "(X^k * (f :: ('a::comm_ring_1) fps)) $n = (if n < k then 0 else f $ (n - k))"
chaieb@31968
   448
  apply (induct k arbitrary: n)
chaieb@31968
   449
  apply (simp)
chaieb@31968
   450
  unfolding power_Suc mult_assoc
chaieb@31968
   451
  by (case_tac n, auto)
chaieb@31968
   452
chaieb@31968
   453
lemma X_power_mult_right_nth: "((f :: ('a::comm_ring_1) fps) * X^k) $n = (if n < k then 0 else f $ (n - k))"
chaieb@31968
   454
  by (metis X_power_mult_nth mult_commute)
chaieb@31968
   455
chaieb@31968
   456
chaieb@31968
   457
chaieb@31369
   458
  
chaieb@31968
   459
subsection{* Formal Power series form a metric space *}
chaieb@31968
   460
chaieb@31968
   461
definition (in dist) ball_def: "ball x r = {y. dist y x < r}"
chaieb@31968
   462
instantiation fps :: (comm_ring_1) dist
chaieb@31968
   463
begin
chaieb@31968
   464
chaieb@31968
   465
definition dist_fps_def: "dist (a::'a fps) b = (if (\<exists>n. a$n \<noteq> b$n) then inverse (2 ^ The (leastP (\<lambda>n. a$n \<noteq> b$n))) else 0)"
chaieb@31968
   466
chaieb@31968
   467
lemma dist_fps_ge0: "dist (a::'a fps) b \<ge> 0"
chaieb@31968
   468
  by (simp add: dist_fps_def)
chaieb@31968
   469
chaieb@31968
   470
lemma dist_fps_sym: "dist (a::'a fps) b = dist b a"
chaieb@31968
   471
  apply (auto simp add: dist_fps_def)
chaieb@31968
   472
  apply (rule cong[OF refl, where x="(\<lambda>n\<Colon>nat. a $ n \<noteq> b $ n)"])
chaieb@31968
   473
  apply (rule ext)
chaieb@31968
   474
  by auto
chaieb@31968
   475
instance ..
chaieb@31968
   476
end
chaieb@31968
   477
chaieb@31968
   478
lemma fps_nonzero_least_unique: assumes a0: "a \<noteq> 0"
chaieb@31968
   479
  shows "\<exists>! n. leastP (\<lambda>n. a$n \<noteq> 0) n"
chaieb@31968
   480
proof-
chaieb@31968
   481
  from fps_nonzero_nth_minimal[of a] a0
chaieb@31968
   482
  obtain n where n: "a$n \<noteq> 0" "\<forall>m < n. a$m = 0" by blast
chaieb@31968
   483
  from n have ln: "leastP (\<lambda>n. a$n \<noteq> 0) n" 
chaieb@31968
   484
    by (auto simp add: leastP_def setge_def not_le[symmetric])
chaieb@31968
   485
  moreover
chaieb@31968
   486
  {fix m assume "leastP (\<lambda>n. a$n \<noteq> 0) m"
chaieb@31968
   487
    then have "m = n" using ln
chaieb@31968
   488
      apply (auto simp add: leastP_def setge_def)
chaieb@31968
   489
      apply (erule allE[where x=n])
chaieb@31968
   490
      apply (erule allE[where x=m])
chaieb@31968
   491
      by simp}
chaieb@31968
   492
  ultimately show ?thesis by blast
chaieb@31968
   493
qed
chaieb@31968
   494
chaieb@31968
   495
lemma fps_eq_least_unique: assumes ab: "(a::('a::ab_group_add) fps) \<noteq> b"
chaieb@31968
   496
  shows "\<exists>! n. leastP (\<lambda>n. a$n \<noteq> b$n) n"
chaieb@31968
   497
using fps_nonzero_least_unique[of "a - b"] ab
chaieb@31968
   498
by auto
chaieb@31968
   499
chaieb@31968
   500
instantiation fps :: (comm_ring_1) metric_space
chaieb@31968
   501
begin
chaieb@31968
   502
chaieb@31968
   503
definition open_fps_def: "open (S :: 'a fps set) = (\<forall>a \<in> S. \<exists>r. r >0 \<and> ball a r \<subseteq> S)"
chaieb@31968
   504
chaieb@31968
   505
instance
chaieb@31968
   506
proof
chaieb@31968
   507
  fix S :: "'a fps set" 
chaieb@31968
   508
  show "open S = (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
chaieb@31968
   509
    by (auto simp add: open_fps_def ball_def subset_eq)
chaieb@31968
   510
next
chaieb@31968
   511
{  fix a b :: "'a fps"
chaieb@31968
   512
  {assume ab: "a = b"
chaieb@31968
   513
   then have "\<not> (\<exists>n. a$n \<noteq> b$n)" by simp
chaieb@31968
   514
   then have "dist a b = 0" by (simp add: dist_fps_def)}
chaieb@31968
   515
 moreover
chaieb@31968
   516
 {assume d: "dist a b = 0"
chaieb@31968
   517
   then have "\<forall>n. a$n = b$n" 
chaieb@31968
   518
     by - (rule ccontr, simp add: dist_fps_def)
chaieb@31968
   519
   then have "a = b" by (simp add: fps_eq_iff)}
chaieb@31968
   520
 ultimately show "dist a b =0 \<longleftrightarrow> a = b" by blast}
chaieb@31968
   521
note th = this
chaieb@31968
   522
from th have th'[simp]: "\<And>a::'a fps. dist a a = 0" by simp
chaieb@31968
   523
  fix a b c :: "'a fps"
chaieb@31968
   524
  {assume ab: "a = b" then have d0: "dist a b = 0"  unfolding th .
chaieb@31968
   525
    then have "dist a b \<le> dist a c + dist b c" 
chaieb@31968
   526
      using dist_fps_ge0[of a c] dist_fps_ge0[of b c] by simp}
chaieb@31968
   527
  moreover
chaieb@31968
   528
  {assume c: "c = a \<or> c = b" then have "dist a b \<le> dist a c + dist b c"
chaieb@31968
   529
      by (cases "c=a", simp_all add: th dist_fps_sym) }
chaieb@31968
   530
  moreover
chaieb@31968
   531
  {assume ab: "a \<noteq> b" and ac: "a \<noteq> c" and bc: "b \<noteq> c"
chaieb@31968
   532
    let ?P = "\<lambda>a b n. a$n \<noteq> b$n"
chaieb@31968
   533
    from fps_eq_least_unique[OF ab] fps_eq_least_unique[OF ac] 
chaieb@31968
   534
      fps_eq_least_unique[OF bc]
chaieb@31968
   535
    obtain nab nac nbc where nab: "leastP (?P a b) nab" 
chaieb@31968
   536
      and nac: "leastP (?P a c) nac" 
chaieb@31968
   537
      and nbc: "leastP (?P b c) nbc" by blast
chaieb@31968
   538
    from nab have nab': "\<And>m. m < nab \<Longrightarrow> a$m = b$m" "a$nab \<noteq> b$nab"
chaieb@31968
   539
      by (auto simp add: leastP_def setge_def)
chaieb@31968
   540
    from nac have nac': "\<And>m. m < nac \<Longrightarrow> a$m = c$m" "a$nac \<noteq> c$nac"
chaieb@31968
   541
      by (auto simp add: leastP_def setge_def)
chaieb@31968
   542
    from nbc have nbc': "\<And>m. m < nbc \<Longrightarrow> b$m = c$m" "b$nbc \<noteq> c$nbc"
chaieb@31968
   543
      by (auto simp add: leastP_def setge_def)
chaieb@31968
   544
chaieb@31968
   545
    have th0: "\<And>(a::'a fps) b. a \<noteq> b \<longleftrightarrow> (\<exists>n. a$n \<noteq> b$n)"
chaieb@31968
   546
      by (simp add: fps_eq_iff)
chaieb@31968
   547
    from ab ac bc nab nac nbc 
chaieb@31968
   548
    have dab: "dist a b = inverse (2 ^ nab)" 
chaieb@31968
   549
      and dac: "dist a c = inverse (2 ^ nac)" 
chaieb@31968
   550
      and dbc: "dist b c = inverse (2 ^ nbc)"
chaieb@31968
   551
      unfolding th0
chaieb@31968
   552
      apply (simp_all add: dist_fps_def)
chaieb@31968
   553
      apply (erule the1_equality[OF fps_eq_least_unique[OF ab]])
chaieb@31968
   554
      apply (erule the1_equality[OF fps_eq_least_unique[OF ac]])
chaieb@31968
   555
      by (erule the1_equality[OF fps_eq_least_unique[OF bc]])
chaieb@31968
   556
    from ab ac bc have nz: "dist a b \<noteq> 0" "dist a c \<noteq> 0" "dist b c \<noteq> 0"
chaieb@31968
   557
      unfolding th by simp_all
chaieb@31968
   558
    from nz have pos: "dist a b > 0" "dist a c > 0" "dist b c > 0"
chaieb@31968
   559
      using dist_fps_ge0[of a b] dist_fps_ge0[of a c] dist_fps_ge0[of b c] 
chaieb@31968
   560
      by auto
chaieb@31968
   561
    have th1: "\<And>n. (2::real)^n >0" by auto
chaieb@31968
   562
    {assume h: "dist a b > dist a c + dist b c"
chaieb@31968
   563
      then have gt: "dist a b > dist a c" "dist a b > dist b c"
chaieb@31968
   564
	using pos by auto
chaieb@31968
   565
      from gt have gtn: "nab < nbc" "nab < nac"
chaieb@31968
   566
	unfolding dab dbc dac by (auto simp add: th1)
chaieb@31968
   567
      from nac'(1)[OF gtn(2)] nbc'(1)[OF gtn(1)]
chaieb@31968
   568
      have "a$nab = b$nab" by simp
chaieb@31968
   569
      with nab'(2) have False  by simp}
chaieb@31968
   570
    then have "dist a b \<le> dist a c + dist b c"
chaieb@31968
   571
      by (auto simp add: not_le[symmetric]) }
chaieb@31968
   572
  ultimately show "dist a b \<le> dist a c + dist b c" by blast
chaieb@31968
   573
qed
chaieb@31968
   574
  
chaieb@31968
   575
end
chaieb@31968
   576
chaieb@31968
   577
text{* The infinite sums and justification of the notation in textbooks*}
chaieb@31968
   578
chaieb@31968
   579
lemma reals_power_lt_ex: assumes xp: "x > 0" and y1: "(y::real) > 1"
chaieb@31968
   580
  shows "\<exists>k>0. (1/y)^k < x"
chaieb@31968
   581
proof-
chaieb@31968
   582
  have yp: "y > 0" using y1 by simp
chaieb@31968
   583
  from reals_Archimedean2[of "max 0 (- log y x) + 1"]
chaieb@31968
   584
  obtain k::nat where k: "real k > max 0 (- log y x) + 1" by blast
chaieb@31968
   585
  from k have kp: "k > 0" by simp
chaieb@31968
   586
  from k have "real k > - log y x" by simp
chaieb@31968
   587
  then have "ln y * real k > - ln x" unfolding log_def
chaieb@31968
   588
    using ln_gt_zero_iff[OF yp] y1
chaieb@31968
   589
    by (simp add: minus_divide_left field_simps del:minus_divide_left[symmetric] )
chaieb@31968
   590
  then have "ln y * real k + ln x > 0" by simp
chaieb@31968
   591
  then have "exp (real k * ln y + ln x) > exp 0"
chaieb@31968
   592
    by (simp add: mult_ac)
chaieb@31968
   593
  then have "y ^ k * x > 1"
chaieb@31968
   594
    unfolding exp_zero exp_add exp_real_of_nat_mult
chaieb@31968
   595
    exp_ln[OF xp] exp_ln[OF yp] by simp
chaieb@31968
   596
  then have "x > (1/y)^k" using yp 
chaieb@31968
   597
    by (simp add: field_simps nonzero_power_divide )
chaieb@31968
   598
  then show ?thesis using kp by blast
chaieb@31968
   599
qed
chaieb@31968
   600
lemma X_nth[simp]: "X$n = (if n = 1 then 1 else 0)" by (simp add: X_def)
chaieb@31968
   601
lemma X_power_nth[simp]: "(X^k) $n = (if n = k then 1 else (0::'a::comm_ring_1))"
chaieb@31968
   602
  by (simp add: X_power_iff)
chaieb@31968
   603
 
chaieb@31968
   604
chaieb@31968
   605
lemma fps_sum_rep_nth: "(setsum (%i. fps_const(a$i)*X^i) {0..m})$n = (if n \<le> m then a$n else (0::'a::comm_ring_1))"
chaieb@31968
   606
  apply (auto simp add: fps_eq_iff fps_setsum_nth X_power_nth cond_application_beta cond_value_iff  cong del: if_weak_cong)
chaieb@31968
   607
  by (simp add: setsum_delta')
chaieb@31968
   608
  
chaieb@31968
   609
lemma fps_notation: 
chaieb@31968
   610
  "(%n. setsum (%i. fps_const(a$i) * X^i) {0..n}) ----> a" (is "?s ----> a")
chaieb@31968
   611
proof-
chaieb@31968
   612
    {fix r:: real
chaieb@31968
   613
      assume rp: "r > 0"
chaieb@31968
   614
      have th0: "(2::real) > 1" by simp
chaieb@31968
   615
      from reals_power_lt_ex[OF rp th0] 
chaieb@31968
   616
      obtain n0 where n0: "(1/2)^n0 < r" "n0 > 0" by blast
chaieb@31968
   617
      {fix n::nat
chaieb@31968
   618
	assume nn0: "n \<ge> n0"
chaieb@31968
   619
	then have thnn0: "(1/2)^n <= (1/2 :: real)^n0"
chaieb@31968
   620
	  by (auto intro: power_decreasing)
chaieb@31968
   621
	{assume "?s n = a" then have "dist (?s n) a < r" 
chaieb@31968
   622
	    unfolding dist_eq_0_iff[of "?s n" a, symmetric]
chaieb@31968
   623
	    using rp by (simp del: dist_eq_0_iff)}
chaieb@31968
   624
	moreover
chaieb@31968
   625
	{assume neq: "?s n \<noteq> a"
chaieb@31968
   626
	  from fps_eq_least_unique[OF neq] 
chaieb@31968
   627
	  obtain k where k: "leastP (\<lambda>i. ?s n $ i \<noteq> a$i) k" by blast
chaieb@31968
   628
	  have th0: "\<And>(a::'a fps) b. a \<noteq> b \<longleftrightarrow> (\<exists>n. a$n \<noteq> b$n)"
chaieb@31968
   629
	    by (simp add: fps_eq_iff)
chaieb@31968
   630
	  from neq have dth: "dist (?s n) a = (1/2)^k"
chaieb@31968
   631
	    unfolding th0 dist_fps_def
chaieb@31968
   632
	    unfolding the1_equality[OF fps_eq_least_unique[OF neq], OF k]
chaieb@31968
   633
	    by (auto simp add: inverse_eq_divide power_divide)
chaieb@31968
   634
chaieb@31968
   635
	  from k have kn: "k > n"
chaieb@31968
   636
	    apply (simp add: leastP_def setge_def fps_sum_rep_nth)
chaieb@31968
   637
	    by (cases "k \<le> n", auto)
chaieb@31968
   638
	  then have "dist (?s n) a < (1/2)^n" unfolding dth
chaieb@31968
   639
	    by (auto intro: power_strict_decreasing)
chaieb@31968
   640
	  also have "\<dots> <= (1/2)^n0" using nn0
chaieb@31968
   641
	    by (auto intro: power_decreasing)
chaieb@31968
   642
	  also have "\<dots> < r" using n0 by simp
chaieb@31968
   643
	  finally have "dist (?s n) a < r" .}
chaieb@31968
   644
	ultimately have "dist (?s n) a < r" by blast}
chaieb@31968
   645
      then have "\<exists>n0. \<forall> n \<ge> n0. dist (?s n) a < r " by blast}
chaieb@31968
   646
    then show ?thesis  unfolding  LIMSEQ_def by blast
chaieb@31968
   647
  qed
chaieb@31968
   648
huffman@29906
   649
subsection{* Inverses of formal power series *}
chaieb@29687
   650
chaieb@29687
   651
declare setsum_cong[fundef_cong]
chaieb@29687
   652
chaieb@29687
   653
chaieb@29687
   654
instantiation fps :: ("{comm_monoid_add,inverse, times, uminus}") inverse
chaieb@29687
   655
begin
chaieb@29687
   656
huffman@30488
   657
fun natfun_inverse:: "'a fps \<Rightarrow> nat \<Rightarrow> 'a" where
chaieb@29687
   658
  "natfun_inverse f 0 = inverse (f$0)"
huffman@30488
   659
| "natfun_inverse f n = - inverse (f$0) * setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}"
chaieb@29687
   660
huffman@30488
   661
definition fps_inverse_def:
chaieb@29687
   662
  "inverse f = (if f$0 = 0 then 0 else Abs_fps (natfun_inverse f))"
huffman@29911
   663
definition fps_divide_def: "divide = (\<lambda>(f::'a fps) g. f * inverse g)"
chaieb@29687
   664
instance ..
chaieb@29687
   665
end
chaieb@29687
   666
huffman@30488
   667
lemma fps_inverse_zero[simp]:
chaieb@29687
   668
  "inverse (0 :: 'a::{comm_monoid_add,inverse, times, uminus} fps) = 0"
huffman@29911
   669
  by (simp add: fps_ext fps_inverse_def)
chaieb@29687
   670
chaieb@29687
   671
lemma fps_inverse_one[simp]: "inverse (1 :: 'a::{division_ring,zero_neq_one} fps) = 1"
huffman@29911
   672
  apply (auto simp add: expand_fps_eq fps_inverse_def)
huffman@29911
   673
  by (case_tac n, auto)
chaieb@29687
   674
huffman@29911
   675
instance fps :: ("{comm_monoid_add,inverse, times, uminus}")  division_by_zero
huffman@29911
   676
  by default (rule fps_inverse_zero)
chaieb@29687
   677
chaieb@29687
   678
lemma inverse_mult_eq_1[intro]: assumes f0: "f$0 \<noteq> (0::'a::field)"
chaieb@29687
   679
  shows "inverse f * f = 1"
chaieb@29687
   680
proof-
chaieb@29687
   681
  have c: "inverse f * f = f * inverse f" by (simp add: mult_commute)
huffman@30488
   682
  from f0 have ifn: "\<And>n. inverse f $ n = natfun_inverse f n"
chaieb@29687
   683
    by (simp add: fps_inverse_def)
chaieb@29687
   684
  from f0 have th0: "(inverse f * f) $ 0 = 1"
huffman@29911
   685
    by (simp add: fps_mult_nth fps_inverse_def)
chaieb@29687
   686
  {fix n::nat assume np: "n >0 "
chaieb@29687
   687
    from np have eq: "{0..n} = {0} \<union> {1 .. n}" by auto
chaieb@29687
   688
    have d: "{0} \<inter> {1 .. n} = {}" by auto
chaieb@29687
   689
    have f: "finite {0::nat}" "finite {1..n}" by auto
huffman@30488
   690
    from f0 np have th0: "- (inverse f$n) =
chaieb@29687
   691
      (setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}) / (f$0)"
huffman@29911
   692
      by (cases n, simp, simp add: divide_inverse fps_inverse_def)
chaieb@29687
   693
    from th0[symmetric, unfolded nonzero_divide_eq_eq[OF f0]]
huffman@30488
   694
    have th1: "setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n} =
huffman@30488
   695
      - (f$0) * (inverse f)$n"
chaieb@29687
   696
      by (simp add: ring_simps)
huffman@30488
   697
    have "(f * inverse f) $ n = (\<Sum>i = 0..n. f $i * natfun_inverse f (n - i))"
chaieb@29687
   698
      unfolding fps_mult_nth ifn ..
huffman@30488
   699
    also have "\<dots> = f$0 * natfun_inverse f n
chaieb@29687
   700
      + (\<Sum>i = 1..n. f$i * natfun_inverse f (n-i))"
chaieb@29687
   701
      unfolding setsum_Un_disjoint[OF f d, unfolded eq[symmetric]]
chaieb@29687
   702
      by simp
chaieb@29687
   703
    also have "\<dots> = 0" unfolding th1 ifn by simp
chaieb@29687
   704
    finally have "(inverse f * f)$n = 0" unfolding c . }
chaieb@29687
   705
  with th0 show ?thesis by (simp add: fps_eq_iff)
chaieb@29687
   706
qed
chaieb@29687
   707
chaieb@29687
   708
lemma fps_inverse_0_iff[simp]: "(inverse f)$0 = (0::'a::division_ring) \<longleftrightarrow> f$0 = 0"
huffman@29911
   709
  by (simp add: fps_inverse_def nonzero_imp_inverse_nonzero)
chaieb@29687
   710
chaieb@29687
   711
lemma fps_inverse_eq_0_iff[simp]: "inverse f = (0:: ('a::field) fps) \<longleftrightarrow> f $0 = 0"
chaieb@29687
   712
proof-
chaieb@29687
   713
  {assume "f$0 = 0" hence "inverse f = 0" by (simp add: fps_inverse_def)}
chaieb@29687
   714
  moreover
chaieb@29687
   715
  {assume h: "inverse f = 0" and c: "f $0 \<noteq> 0"
chaieb@29687
   716
    from inverse_mult_eq_1[OF c] h have False by simp}
chaieb@29687
   717
  ultimately show ?thesis by blast
chaieb@29687
   718
qed
chaieb@29687
   719
chaieb@29687
   720
lemma fps_inverse_idempotent[intro]: assumes f0: "f$0 \<noteq> (0::'a::field)"
chaieb@29687
   721
  shows "inverse (inverse f) = f"
chaieb@29687
   722
proof-
chaieb@29687
   723
  from f0 have if0: "inverse f $ 0 \<noteq> 0" by simp
huffman@30488
   724
  from inverse_mult_eq_1[OF f0] inverse_mult_eq_1[OF if0]
chaieb@29687
   725
  have th0: "inverse f * f = inverse f * inverse (inverse f)"   by (simp add: mult_ac)
chaieb@29687
   726
  then show ?thesis using f0 unfolding mult_cancel_left by simp
chaieb@29687
   727
qed
chaieb@29687
   728
huffman@30488
   729
lemma fps_inverse_unique: assumes f0: "f$0 \<noteq> (0::'a::field)" and fg: "f*g = 1"
chaieb@29687
   730
  shows "inverse f = g"
chaieb@29687
   731
proof-
chaieb@29687
   732
  from inverse_mult_eq_1[OF f0] fg
chaieb@29687
   733
  have th0: "inverse f * f = g * f" by (simp add: mult_ac)
chaieb@29687
   734
  then show ?thesis using f0  unfolding mult_cancel_right
huffman@29911
   735
    by (auto simp add: expand_fps_eq)
chaieb@29687
   736
qed
chaieb@29687
   737
huffman@30488
   738
lemma fps_inverse_gp: "inverse (Abs_fps(\<lambda>n. (1::'a::field)))
chaieb@29687
   739
  = Abs_fps (\<lambda>n. if n= 0 then 1 else if n=1 then - 1 else 0)"
chaieb@29687
   740
  apply (rule fps_inverse_unique)
chaieb@29687
   741
  apply simp
huffman@29911
   742
  apply (simp add: fps_eq_iff fps_mult_nth)
chaieb@29687
   743
proof(clarsimp)
chaieb@29687
   744
  fix n::nat assume n: "n > 0"
chaieb@29687
   745
  let ?f = "\<lambda>i. if n = i then (1\<Colon>'a) else if n - i = 1 then - 1 else 0"
chaieb@29687
   746
  let ?g = "\<lambda>i. if i = n then 1 else if i=n - 1 then - 1 else 0"
chaieb@29687
   747
  let ?h = "\<lambda>i. if i=n - 1 then - 1 else 0"
huffman@30488
   748
  have th1: "setsum ?f {0..n} = setsum ?g {0..n}"
chaieb@29687
   749
    by (rule setsum_cong2) auto
huffman@30488
   750
  have th2: "setsum ?g {0..n - 1} = setsum ?h {0..n - 1}"
chaieb@29687
   751
    using n apply - by (rule setsum_cong2) auto
chaieb@29687
   752
  have eq: "{0 .. n} = {0.. n - 1} \<union> {n}" by auto
huffman@30488
   753
  from n have d: "{0.. n - 1} \<inter> {n} = {}" by auto
chaieb@29687
   754
  have f: "finite {0.. n - 1}" "finite {n}" by auto
chaieb@29687
   755
  show "setsum ?f {0..n} = 0"
huffman@30488
   756
    unfolding th1
chaieb@29687
   757
    apply (simp add: setsum_Un_disjoint[OF f d, unfolded eq[symmetric]] del: One_nat_def)
chaieb@29687
   758
    unfolding th2
chaieb@29687
   759
    by(simp add: setsum_delta)
chaieb@29687
   760
qed
chaieb@29687
   761
huffman@29912
   762
subsection{* Formal Derivatives, and the MacLaurin theorem around 0*}
chaieb@29687
   763
chaieb@29687
   764
definition "fps_deriv f = Abs_fps (\<lambda>n. of_nat (n + 1) * f $ (n + 1))"
chaieb@29687
   765
chaieb@29687
   766
lemma fps_deriv_nth[simp]: "fps_deriv f $ n = of_nat (n +1) * f $ (n+1)" by (simp add: fps_deriv_def)
chaieb@29687
   767
chaieb@29687
   768
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"
chaieb@29687
   769
  unfolding fps_eq_iff fps_add_nth  fps_const_mult_left fps_deriv_nth by (simp add: ring_simps)
chaieb@29687
   770
huffman@30488
   771
lemma fps_deriv_mult[simp]:
chaieb@29687
   772
  fixes f :: "('a :: comm_ring_1) fps"
chaieb@29687
   773
  shows "fps_deriv (f * g) = f * fps_deriv g + fps_deriv f * g"
chaieb@29687
   774
proof-
chaieb@29687
   775
  let ?D = "fps_deriv"
chaieb@29687
   776
  {fix n::nat
chaieb@29687
   777
    let ?Zn = "{0 ..n}"
chaieb@29687
   778
    let ?Zn1 = "{0 .. n + 1}"
chaieb@29687
   779
    let ?f = "\<lambda>i. i + 1"
chaieb@29687
   780
    have fi: "inj_on ?f {0..n}" by (simp add: inj_on_def)
chaieb@29687
   781
    have eq: "{1.. n+1} = ?f ` {0..n}" by auto
chaieb@29687
   782
    let ?g = "\<lambda>i. of_nat (i+1) * g $ (i+1) * f $ (n - i) +
chaieb@29687
   783
        of_nat (i+1)* f $ (i+1) * g $ (n - i)"
chaieb@29687
   784
    let ?h = "\<lambda>i. of_nat i * g $ i * f $ ((n+1) - i) +
chaieb@29687
   785
        of_nat i* f $ i * g $ ((n + 1) - i)"
chaieb@29687
   786
    {fix k assume k: "k \<in> {0..n}"
chaieb@29687
   787
      have "?h (k + 1) = ?g k" using k by auto}
chaieb@29687
   788
    note th0 = this
chaieb@29687
   789
    have eq': "{0..n +1}- {1 .. n+1} = {0}" by auto
chaieb@29687
   790
    have s0: "setsum (\<lambda>i. of_nat i * f $ i * g $ (n + 1 - i)) ?Zn1 = setsum (\<lambda>i. of_nat (n + 1 - i) * f $ (n + 1 - i) * g $ i) ?Zn1"
chaieb@29687
   791
      apply (rule setsum_reindex_cong[where f="\<lambda>i. n + 1 - i"])
chaieb@29687
   792
      apply (simp add: inj_on_def Ball_def)
chaieb@29687
   793
      apply presburger
chaieb@29687
   794
      apply (rule set_ext)
chaieb@29687
   795
      apply (presburger add: image_iff)
chaieb@29687
   796
      by simp
chaieb@29687
   797
    have s1: "setsum (\<lambda>i. f $ i * g $ (n + 1 - i)) ?Zn1 = setsum (\<lambda>i. f $ (n + 1 - i) * g $ i) ?Zn1"
chaieb@29687
   798
      apply (rule setsum_reindex_cong[where f="\<lambda>i. n + 1 - i"])
chaieb@29687
   799
      apply (simp add: inj_on_def Ball_def)
chaieb@29687
   800
      apply presburger
chaieb@29687
   801
      apply (rule set_ext)
chaieb@29687
   802
      apply (presburger add: image_iff)
chaieb@29687
   803
      by simp
chaieb@29687
   804
    have "(f * ?D g + ?D f * g)$n = (?D g * f + ?D f * g)$n" by (simp only: mult_commute)
chaieb@29687
   805
    also have "\<dots> = (\<Sum>i = 0..n. ?g i)"
chaieb@29687
   806
      by (simp add: fps_mult_nth setsum_addf[symmetric])
chaieb@29687
   807
    also have "\<dots> = setsum ?h {1..n+1}"
chaieb@29687
   808
      using th0 setsum_reindex_cong[OF fi eq, of "?g" "?h"] by auto
chaieb@29687
   809
    also have "\<dots> = setsum ?h {0..n+1}"
chaieb@29687
   810
      apply (rule setsum_mono_zero_left)
chaieb@29687
   811
      apply simp
chaieb@29687
   812
      apply (simp add: subset_eq)
chaieb@29687
   813
      unfolding eq'
chaieb@29687
   814
      by simp
chaieb@29687
   815
    also have "\<dots> = (fps_deriv (f * g)) $ n"
chaieb@29687
   816
      apply (simp only: fps_deriv_nth fps_mult_nth setsum_addf)
chaieb@29687
   817
      unfolding s0 s1
chaieb@29687
   818
      unfolding setsum_addf[symmetric] setsum_right_distrib
chaieb@29687
   819
      apply (rule setsum_cong2)
chaieb@29687
   820
      by (auto simp add: of_nat_diff ring_simps)
chaieb@29687
   821
    finally have "(f * ?D g + ?D f * g) $ n = ?D (f*g) $ n" .}
huffman@30488
   822
  then show ?thesis unfolding fps_eq_iff by auto
chaieb@29687
   823
qed
chaieb@29687
   824
chaieb@31968
   825
lemma fps_deriv_X[simp]: "fps_deriv X = 1"
chaieb@31968
   826
  by (simp add: fps_deriv_def X_def fps_eq_iff)
chaieb@31968
   827
chaieb@29687
   828
lemma fps_deriv_neg[simp]: "fps_deriv (- (f:: ('a:: comm_ring_1) fps)) = - (fps_deriv f)"
huffman@29911
   829
  by (simp add: fps_eq_iff fps_deriv_def)
chaieb@29687
   830
lemma fps_deriv_add[simp]: "fps_deriv ((f:: ('a::comm_ring_1) fps) + g) = fps_deriv f + fps_deriv g"
chaieb@29687
   831
  using fps_deriv_linear[of 1 f 1 g] by simp
chaieb@29687
   832
chaieb@29687
   833
lemma fps_deriv_sub[simp]: "fps_deriv ((f:: ('a::comm_ring_1) fps) - g) = fps_deriv f - fps_deriv g"
huffman@30488
   834
  unfolding diff_minus by simp
chaieb@29687
   835
chaieb@29687
   836
lemma fps_deriv_const[simp]: "fps_deriv (fps_const c) = 0"
huffman@29911
   837
  by (simp add: fps_ext fps_deriv_def fps_const_def)
chaieb@29687
   838
chaieb@29687
   839
lemma fps_deriv_mult_const_left[simp]: "fps_deriv (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_deriv f"
chaieb@29687
   840
  by simp
chaieb@29687
   841
chaieb@29687
   842
lemma fps_deriv_0[simp]: "fps_deriv 0 = 0"
chaieb@29687
   843
  by (simp add: fps_deriv_def fps_eq_iff)
chaieb@29687
   844
chaieb@29687
   845
lemma fps_deriv_1[simp]: "fps_deriv 1 = 0"
chaieb@29687
   846
  by (simp add: fps_deriv_def fps_eq_iff )
chaieb@29687
   847
chaieb@29687
   848
lemma fps_deriv_mult_const_right[simp]: "fps_deriv (f * fps_const (c::'a::comm_ring_1)) = fps_deriv f * fps_const c"
chaieb@29687
   849
  by simp
chaieb@29687
   850
chaieb@29687
   851
lemma fps_deriv_setsum: "fps_deriv (setsum f S) = setsum (\<lambda>i. fps_deriv (f i :: ('a::comm_ring_1) fps)) S"
chaieb@29687
   852
proof-
chaieb@29687
   853
  {assume "\<not> finite S" hence ?thesis by simp}
chaieb@29687
   854
  moreover
chaieb@29687
   855
  {assume fS: "finite S"
chaieb@29687
   856
    have ?thesis  by (induct rule: finite_induct[OF fS], simp_all)}
chaieb@29687
   857
  ultimately show ?thesis by blast
chaieb@29687
   858
qed
chaieb@29687
   859
chaieb@29687
   860
lemma fps_deriv_eq_0_iff[simp]: "fps_deriv f = 0 \<longleftrightarrow> (f = fps_const (f$0 :: 'a::{idom,semiring_char_0}))"
chaieb@29687
   861
proof-
chaieb@29687
   862
  {assume "f= fps_const (f$0)" hence "fps_deriv f = fps_deriv (fps_const (f$0))" by simp
chaieb@29687
   863
    hence "fps_deriv f = 0" by simp }
chaieb@29687
   864
  moreover
chaieb@29687
   865
  {assume z: "fps_deriv f = 0"
chaieb@29687
   866
    hence "\<forall>n. (fps_deriv f)$n = 0" by simp
chaieb@29687
   867
    hence "\<forall>n. f$(n+1) = 0" by (simp del: of_nat_Suc of_nat_add One_nat_def)
chaieb@29687
   868
    hence "f = fps_const (f$0)"
chaieb@29687
   869
      apply (clarsimp simp add: fps_eq_iff fps_const_def)
chaieb@29687
   870
      apply (erule_tac x="n - 1" in allE)
chaieb@29687
   871
      by simp}
chaieb@29687
   872
  ultimately show ?thesis by blast
chaieb@29687
   873
qed
chaieb@29687
   874
huffman@30488
   875
lemma fps_deriv_eq_iff:
chaieb@29687
   876
  fixes f:: "('a::{idom,semiring_char_0}) fps"
chaieb@29687
   877
  shows "fps_deriv f = fps_deriv g \<longleftrightarrow> (f = fps_const(f$0 - g$0) + g)"
chaieb@29687
   878
proof-
chaieb@29687
   879
  have "fps_deriv f = fps_deriv g \<longleftrightarrow> fps_deriv (f - g) = 0" by simp
chaieb@29687
   880
  also have "\<dots> \<longleftrightarrow> f - g = fps_const ((f-g)$0)" unfolding fps_deriv_eq_0_iff ..
chaieb@29687
   881
  finally show ?thesis by (simp add: ring_simps)
chaieb@29687
   882
qed
chaieb@29687
   883
chaieb@29687
   884
lemma fps_deriv_eq_iff_ex: "(fps_deriv f = fps_deriv g) \<longleftrightarrow> (\<exists>(c::'a::{idom,semiring_char_0}). f = fps_const c + g)"
chaieb@29687
   885
  apply auto unfolding fps_deriv_eq_iff by blast
huffman@30488
   886
chaieb@29687
   887
chaieb@29687
   888
fun fps_nth_deriv :: "nat \<Rightarrow> ('a::semiring_1) fps \<Rightarrow> 'a fps" where
chaieb@29687
   889
  "fps_nth_deriv 0 f = f"
chaieb@29687
   890
| "fps_nth_deriv (Suc n) f = fps_nth_deriv n (fps_deriv f)"
chaieb@29687
   891
chaieb@29687
   892
lemma fps_nth_deriv_commute: "fps_nth_deriv (Suc n) f = fps_deriv (fps_nth_deriv n f)"
chaieb@29687
   893
  by (induct n arbitrary: f, auto)
chaieb@29687
   894
chaieb@29687
   895
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"
chaieb@29687
   896
  by (induct n arbitrary: f g, auto simp add: fps_nth_deriv_commute)
chaieb@29687
   897
chaieb@29687
   898
lemma fps_nth_deriv_neg[simp]: "fps_nth_deriv n (- (f:: ('a:: comm_ring_1) fps)) = - (fps_nth_deriv n f)"
chaieb@29687
   899
  by (induct n arbitrary: f, simp_all)
chaieb@29687
   900
chaieb@29687
   901
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"
chaieb@29687
   902
  using fps_nth_deriv_linear[of n 1 f 1 g] by simp
chaieb@29687
   903
chaieb@29687
   904
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"
huffman@30488
   905
  unfolding diff_minus fps_nth_deriv_add by simp
chaieb@29687
   906
chaieb@29687
   907
lemma fps_nth_deriv_0[simp]: "fps_nth_deriv n 0 = 0"
chaieb@29687
   908
  by (induct n, simp_all )
chaieb@29687
   909
chaieb@29687
   910
lemma fps_nth_deriv_1[simp]: "fps_nth_deriv n 1 = (if n = 0 then 1 else 0)"
chaieb@29687
   911
  by (induct n, simp_all )
chaieb@29687
   912
chaieb@29687
   913
lemma fps_nth_deriv_const[simp]: "fps_nth_deriv n (fps_const c) = (if n = 0 then fps_const c else 0)"
chaieb@29687
   914
  by (cases n, simp_all)
chaieb@29687
   915
chaieb@29687
   916
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"
chaieb@29687
   917
  using fps_nth_deriv_linear[of n "c" f 0 0 ] by simp
chaieb@29687
   918
chaieb@29687
   919
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"
chaieb@29687
   920
  using fps_nth_deriv_linear[of n "c" f 0 0] by (simp add: mult_commute)
chaieb@29687
   921
chaieb@29687
   922
lemma fps_nth_deriv_setsum: "fps_nth_deriv n (setsum f S) = setsum (\<lambda>i. fps_nth_deriv n (f i :: ('a::comm_ring_1) fps)) S"
chaieb@29687
   923
proof-
chaieb@29687
   924
  {assume "\<not> finite S" hence ?thesis by simp}
chaieb@29687
   925
  moreover
chaieb@29687
   926
  {assume fS: "finite S"
chaieb@29687
   927
    have ?thesis  by (induct rule: finite_induct[OF fS], simp_all)}
chaieb@29687
   928
  ultimately show ?thesis by blast
chaieb@29687
   929
qed
chaieb@29687
   930
chaieb@29687
   931
lemma fps_deriv_maclauren_0: "(fps_nth_deriv k (f:: ('a::comm_semiring_1) fps)) $ 0 = of_nat (fact k) * f$(k)"
chaieb@29687
   932
  by (induct k arbitrary: f) (auto simp add: ring_simps of_nat_mult)
chaieb@29687
   933
huffman@29906
   934
subsection {* Powers*}
chaieb@29687
   935
chaieb@29687
   936
lemma fps_power_zeroth_eq_one: "a$0 =1 \<Longrightarrow> a^n $ 0 = (1::'a::semiring_1)"
haftmann@30960
   937
  by (induct n, auto simp add: expand_fps_eq fps_mult_nth)
chaieb@29687
   938
chaieb@29687
   939
lemma fps_power_first_eq: "(a:: 'a::comm_ring_1 fps)$0 =1 \<Longrightarrow> a^n $ 1 = of_nat n * a$1"
chaieb@29687
   940
proof(induct n)
haftmann@30960
   941
  case 0 thus ?case by simp
chaieb@29687
   942
next
chaieb@29687
   943
  case (Suc n)
chaieb@29687
   944
  note h = Suc.hyps[OF `a$0 = 1`]
huffman@30488
   945
  show ?case unfolding power_Suc fps_mult_nth
chaieb@29687
   946
    using h `a$0 = 1`  fps_power_zeroth_eq_one[OF `a$0=1`] by (simp add: ring_simps)
chaieb@29687
   947
qed
chaieb@29687
   948
chaieb@29687
   949
lemma startsby_one_power:"a $ 0 = (1::'a::comm_ring_1) \<Longrightarrow> a^n $ 0 = 1"
haftmann@30960
   950
  by (induct n, auto simp add: fps_mult_nth)
chaieb@29687
   951
chaieb@29687
   952
lemma startsby_zero_power:"a $0 = (0::'a::comm_ring_1) \<Longrightarrow> n > 0 \<Longrightarrow> a^n $0 = 0"
haftmann@30960
   953
  by (induct n, auto simp add: fps_mult_nth)
chaieb@29687
   954
haftmann@31021
   955
lemma startsby_power:"a $0 = (v::'a::{comm_ring_1}) \<Longrightarrow> a^n $0 = v^n"
haftmann@30960
   956
  by (induct n, auto simp add: fps_mult_nth power_Suc)
chaieb@29687
   957
chaieb@29687
   958
lemma startsby_zero_power_iff[simp]:
haftmann@31021
   959
  "a^n $0 = (0::'a::{idom}) \<longleftrightarrow> (n \<noteq> 0 \<and> a$0 = 0)"
chaieb@29687
   960
apply (rule iffI)
chaieb@29687
   961
apply (induct n, auto simp add: power_Suc fps_mult_nth)
chaieb@29687
   962
by (rule startsby_zero_power, simp_all)
chaieb@29687
   963
huffman@30488
   964
lemma startsby_zero_power_prefix:
chaieb@29687
   965
  assumes a0: "a $0 = (0::'a::idom)"
chaieb@29687
   966
  shows "\<forall>n < k. a ^ k $ n = 0"
huffman@30488
   967
  using a0
chaieb@29687
   968
proof(induct k rule: nat_less_induct)
chaieb@29687
   969
  fix k assume H: "\<forall>m<k. a $0 =  0 \<longrightarrow> (\<forall>n<m. a ^ m $ n = 0)" and a0: "a $0 = (0\<Colon>'a)"
chaieb@29687
   970
  let ?ths = "\<forall>m<k. a ^ k $ m = 0"
chaieb@29687
   971
  {assume "k = 0" then have ?ths by simp}
chaieb@29687
   972
  moreover
chaieb@29687
   973
  {fix l assume k: "k = Suc l"
chaieb@29687
   974
    {fix m assume mk: "m < k"
huffman@30488
   975
      {assume "m=0" hence "a^k $ m = 0" using startsby_zero_power[of a k] k a0
chaieb@29687
   976
	  by simp}
chaieb@29687
   977
      moreover
chaieb@29687
   978
      {assume m0: "m \<noteq> 0"
chaieb@29687
   979
	have "a ^k $ m = (a^l * a) $m" by (simp add: k power_Suc mult_commute)
chaieb@29687
   980
	also have "\<dots> = (\<Sum>i = 0..m. a ^ l $ i * a $ (m - i))" by (simp add: fps_mult_nth)
chaieb@29687
   981
	also have "\<dots> = 0" apply (rule setsum_0')
chaieb@29687
   982
	  apply auto
chaieb@29687
   983
	  apply (case_tac "aa = m")
chaieb@29687
   984
	  using a0
chaieb@29687
   985
	  apply simp
chaieb@29687
   986
	  apply (rule H[rule_format])
huffman@30488
   987
	  using a0 k mk by auto
chaieb@29687
   988
	finally have "a^k $ m = 0" .}
chaieb@29687
   989
    ultimately have "a^k $ m = 0" by blast}
chaieb@29687
   990
    hence ?ths by blast}
chaieb@29687
   991
  ultimately show ?ths by (cases k, auto)
chaieb@29687
   992
qed
chaieb@29687
   993
huffman@30488
   994
lemma startsby_zero_setsum_depends:
chaieb@29687
   995
  assumes a0: "a $0 = (0::'a::idom)" and kn: "n \<ge> k"
chaieb@29687
   996
  shows "setsum (\<lambda>i. (a ^ i)$k) {0 .. n} = setsum (\<lambda>i. (a ^ i)$k) {0 .. k}"
chaieb@29687
   997
  apply (rule setsum_mono_zero_right)
chaieb@29687
   998
  using kn apply auto
chaieb@29687
   999
  apply (rule startsby_zero_power_prefix[rule_format, OF a0])
chaieb@29687
  1000
  by arith
chaieb@29687
  1001
haftmann@31021
  1002
lemma startsby_zero_power_nth_same: assumes a0: "a$0 = (0::'a::{idom})"
chaieb@29687
  1003
  shows "a^n $ n = (a$1) ^ n"
chaieb@29687
  1004
proof(induct n)
chaieb@29687
  1005
  case 0 thus ?case by (simp add: power_0)
chaieb@29687
  1006
next
chaieb@29687
  1007
  case (Suc n)
chaieb@29687
  1008
  have "a ^ Suc n $ (Suc n) = (a^n * a)$(Suc n)" by (simp add: ring_simps power_Suc)
chaieb@29687
  1009
  also have "\<dots> = setsum (\<lambda>i. a^n$i * a $ (Suc n - i)) {0.. Suc n}" by (simp add: fps_mult_nth)
chaieb@29687
  1010
  also have "\<dots> = setsum (\<lambda>i. a^n$i * a $ (Suc n - i)) {n .. Suc n}"
chaieb@29687
  1011
    apply (rule setsum_mono_zero_right)
chaieb@29687
  1012
    apply simp
chaieb@29687
  1013
    apply clarsimp
chaieb@29687
  1014
    apply clarsimp
chaieb@29687
  1015
    apply (rule startsby_zero_power_prefix[rule_format, OF a0])
chaieb@29687
  1016
    apply arith
chaieb@29687
  1017
    done
chaieb@29687
  1018
  also have "\<dots> = a^n $ n * a$1" using a0 by simp
chaieb@29687
  1019
  finally show ?case using Suc.hyps by (simp add: power_Suc)
chaieb@29687
  1020
qed
chaieb@29687
  1021
chaieb@29687
  1022
lemma fps_inverse_power:
haftmann@31021
  1023
  fixes a :: "('a::{field}) fps"
chaieb@29687
  1024
  shows "inverse (a^n) = inverse a ^ n"
chaieb@29687
  1025
proof-
chaieb@29687
  1026
  {assume a0: "a$0 = 0"
chaieb@29687
  1027
    hence eq: "inverse a = 0" by (simp add: fps_inverse_def)
chaieb@29687
  1028
    {assume "n = 0" hence ?thesis by simp}
chaieb@29687
  1029
    moreover
chaieb@29687
  1030
    {assume n: "n > 0"
huffman@30488
  1031
      from startsby_zero_power[OF a0 n] eq a0 n have ?thesis
chaieb@29687
  1032
	by (simp add: fps_inverse_def)}
chaieb@29687
  1033
    ultimately have ?thesis by blast}
chaieb@29687
  1034
  moreover
chaieb@29687
  1035
  {assume a0: "a$0 \<noteq> 0"
chaieb@29687
  1036
    have ?thesis
chaieb@29687
  1037
      apply (rule fps_inverse_unique)
chaieb@29687
  1038
      apply (simp add: a0)
chaieb@29687
  1039
      unfolding power_mult_distrib[symmetric]
chaieb@29687
  1040
      apply (rule ssubst[where t = "a * inverse a" and s= 1])
chaieb@29687
  1041
      apply simp_all
chaieb@29687
  1042
      apply (subst mult_commute)
chaieb@29687
  1043
      by (rule inverse_mult_eq_1[OF a0])}
chaieb@29687
  1044
  ultimately show ?thesis by blast
chaieb@29687
  1045
qed
chaieb@29687
  1046
chaieb@29687
  1047
lemma fps_deriv_power: "fps_deriv (a ^ n) = fps_const (of_nat n :: 'a:: comm_ring_1) * fps_deriv a * a ^ (n - 1)"
chaieb@29687
  1048
  apply (induct n, auto simp add: power_Suc ring_simps fps_const_add[symmetric] simp del: fps_const_add)
chaieb@29687
  1049
  by (case_tac n, auto simp add: power_Suc ring_simps)
chaieb@29687
  1050
huffman@30488
  1051
lemma fps_inverse_deriv:
chaieb@29687
  1052
  fixes a:: "('a :: field) fps"
chaieb@29687
  1053
  assumes a0: "a$0 \<noteq> 0"
chaieb@29687
  1054
  shows "fps_deriv (inverse a) = - fps_deriv a * inverse a ^ 2"
chaieb@29687
  1055
proof-
chaieb@29687
  1056
  from inverse_mult_eq_1[OF a0]
chaieb@29687
  1057
  have "fps_deriv (inverse a * a) = 0" by simp
chaieb@29687
  1058
  hence "inverse a * fps_deriv a + fps_deriv (inverse a) * a = 0" by simp
chaieb@29687
  1059
  hence "inverse a * (inverse a * fps_deriv a + fps_deriv (inverse a) * a) = 0"  by simp
chaieb@29687
  1060
  with inverse_mult_eq_1[OF a0]
chaieb@29687
  1061
  have "inverse a ^ 2 * fps_deriv a + fps_deriv (inverse a) = 0"
chaieb@29687
  1062
    unfolding power2_eq_square
chaieb@29687
  1063
    apply (simp add: ring_simps)
chaieb@29687
  1064
    by (simp add: mult_assoc[symmetric])
chaieb@29687
  1065
  hence "inverse a ^ 2 * fps_deriv a + fps_deriv (inverse a) - fps_deriv a * inverse a ^ 2 = 0 - fps_deriv a * inverse a ^ 2"
chaieb@29687
  1066
    by simp
chaieb@29687
  1067
  then show "fps_deriv (inverse a) = - fps_deriv a * inverse a ^ 2" by (simp add: ring_simps)
chaieb@29687
  1068
qed
chaieb@29687
  1069
huffman@30488
  1070
lemma fps_inverse_mult:
chaieb@29687
  1071
  fixes a::"('a :: field) fps"
chaieb@29687
  1072
  shows "inverse (a * b) = inverse a * inverse b"
chaieb@29687
  1073
proof-
chaieb@29687
  1074
  {assume a0: "a$0 = 0" hence ab0: "(a*b)$0 = 0" by (simp add: fps_mult_nth)
chaieb@29687
  1075
    from a0 ab0 have th: "inverse a = 0" "inverse (a*b) = 0" by simp_all
chaieb@29687
  1076
    have ?thesis unfolding th by simp}
chaieb@29687
  1077
  moreover
chaieb@29687
  1078
  {assume b0: "b$0 = 0" hence ab0: "(a*b)$0 = 0" by (simp add: fps_mult_nth)
chaieb@29687
  1079
    from b0 ab0 have th: "inverse b = 0" "inverse (a*b) = 0" by simp_all
chaieb@29687
  1080
    have ?thesis unfolding th by simp}
chaieb@29687
  1081
  moreover
chaieb@29687
  1082
  {assume a0: "a$0 \<noteq> 0" and b0: "b$0 \<noteq> 0"
chaieb@29687
  1083
    from a0 b0 have ab0:"(a*b) $ 0 \<noteq> 0" by (simp  add: fps_mult_nth)
huffman@30488
  1084
    from inverse_mult_eq_1[OF ab0]
chaieb@29687
  1085
    have "inverse (a*b) * (a*b) * inverse a * inverse b = 1 * inverse a * inverse b" by simp
chaieb@29687
  1086
    then have "inverse (a*b) * (inverse a * a) * (inverse b * b) = inverse a * inverse b"
chaieb@29687
  1087
      by (simp add: ring_simps)
chaieb@29687
  1088
    then have ?thesis using inverse_mult_eq_1[OF a0] inverse_mult_eq_1[OF b0] by simp}
chaieb@29687
  1089
ultimately show ?thesis by blast
chaieb@29687
  1090
qed
chaieb@29687
  1091
huffman@30488
  1092
lemma fps_inverse_deriv':
chaieb@29687
  1093
  fixes a:: "('a :: field) fps"
chaieb@29687
  1094
  assumes a0: "a$0 \<noteq> 0"
chaieb@29687
  1095
  shows "fps_deriv (inverse a) = - fps_deriv a / a ^ 2"
chaieb@29687
  1096
  using fps_inverse_deriv[OF a0]
chaieb@29687
  1097
  unfolding power2_eq_square fps_divide_def
chaieb@29687
  1098
    fps_inverse_mult by simp
chaieb@29687
  1099
chaieb@29687
  1100
lemma inverse_mult_eq_1': assumes f0: "f$0 \<noteq> (0::'a::field)"
chaieb@29687
  1101
  shows "f * inverse f= 1"
chaieb@29687
  1102
  by (metis mult_commute inverse_mult_eq_1 f0)
chaieb@29687
  1103
chaieb@29687
  1104
lemma fps_divide_deriv:   fixes a:: "('a :: field) fps"
chaieb@29687
  1105
  assumes a0: "b$0 \<noteq> 0"
chaieb@29687
  1106
  shows "fps_deriv (a / b) = (fps_deriv a * b - a * fps_deriv b) / b ^ 2"
chaieb@29687
  1107
  using fps_inverse_deriv[OF a0]
chaieb@29687
  1108
  by (simp add: fps_divide_def ring_simps power2_eq_square fps_inverse_mult inverse_mult_eq_1'[OF a0])
huffman@30488
  1109
chaieb@29687
  1110
huffman@30488
  1111
lemma fps_inverse_gp': "inverse (Abs_fps(\<lambda>n. (1::'a::field)))
chaieb@29687
  1112
  = 1 - X"
huffman@29911
  1113
  by (simp add: fps_inverse_gp fps_eq_iff X_def)
chaieb@29687
  1114
chaieb@29687
  1115
lemma fps_nth_deriv_X[simp]: "fps_nth_deriv n X = (if n = 0 then X else if n=1 then 1 else 0)"
chaieb@29687
  1116
  by (cases "n", simp_all)
chaieb@29687
  1117
chaieb@29687
  1118
chaieb@29687
  1119
lemma fps_inverse_X_plus1:
haftmann@31021
  1120
  "inverse (1 + X) = Abs_fps (\<lambda>n. (- (1::'a::{field})) ^ n)" (is "_ = ?r")
chaieb@29687
  1121
proof-
chaieb@29687
  1122
  have eq: "(1 + X) * ?r = 1"
chaieb@29687
  1123
    unfolding minus_one_power_iff
nipkow@31148
  1124
    by (auto simp add: ring_simps fps_eq_iff)
chaieb@29687
  1125
  show ?thesis by (auto simp add: eq intro: fps_inverse_unique)
chaieb@29687
  1126
qed
chaieb@29687
  1127
huffman@30488
  1128
huffman@29906
  1129
subsection{* Integration *}
huffman@31273
  1130
huffman@31273
  1131
definition
huffman@31273
  1132
  fps_integral :: "'a::field_char_0 fps \<Rightarrow> 'a \<Rightarrow> 'a fps" where
huffman@31273
  1133
  "fps_integral a a0 = Abs_fps (\<lambda>n. if n = 0 then a0 else (a$(n - 1) / of_nat n))"
chaieb@29687
  1134
huffman@31273
  1135
lemma fps_deriv_fps_integral: "fps_deriv (fps_integral a a0) = a"
huffman@31273
  1136
  unfolding fps_integral_def fps_deriv_def
huffman@31273
  1137
  by (simp add: fps_eq_iff del: of_nat_Suc)
chaieb@29687
  1138
huffman@31273
  1139
lemma fps_integral_linear:
huffman@31273
  1140
  "fps_integral (fps_const a * f + fps_const b * g) (a*a0 + b*b0) =
huffman@31273
  1141
    fps_const a * fps_integral f a0 + fps_const b * fps_integral g b0"
huffman@31273
  1142
  (is "?l = ?r")
chaieb@29687
  1143
proof-
chaieb@29687
  1144
  have "fps_deriv ?l = fps_deriv ?r" by (simp add: fps_deriv_fps_integral)
chaieb@29687
  1145
  moreover have "?l$0 = ?r$0" by (simp add: fps_integral_def)
chaieb@29687
  1146
  ultimately show ?thesis
chaieb@29687
  1147
    unfolding fps_deriv_eq_iff by auto
chaieb@29687
  1148
qed
huffman@30488
  1149
huffman@29906
  1150
subsection {* Composition of FPSs *}
chaieb@29687
  1151
definition fps_compose :: "('a::semiring_1) fps \<Rightarrow> 'a fps \<Rightarrow> 'a fps" (infixl "oo" 55) where
chaieb@29687
  1152
  fps_compose_def: "a oo b = Abs_fps (\<lambda>n. setsum (\<lambda>i. a$i * (b^i$n)) {0..n})"
chaieb@29687
  1153
chaieb@29687
  1154
lemma fps_compose_nth: "(a oo b)$n = setsum (\<lambda>i. a$i * (b^i$n)) {0..n}" by (simp add: fps_compose_def)
chaieb@29687
  1155
chaieb@29687
  1156
lemma fps_compose_X[simp]: "a oo X = (a :: ('a :: comm_ring_1) fps)"
huffman@29913
  1157
  by (simp add: fps_ext fps_compose_def mult_delta_right setsum_delta')
huffman@30488
  1158
huffman@30488
  1159
lemma fps_const_compose[simp]:
chaieb@29687
  1160
  "fps_const (a::'a::{comm_ring_1}) oo b = fps_const (a)"
huffman@29913
  1161
  by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta)
chaieb@29687
  1162
chaieb@31369
  1163
lemma number_of_compose[simp]: "(number_of k::('a::{comm_ring_1}) fps) oo b = number_of k"
chaieb@31369
  1164
  unfolding number_of_fps_const by simp
chaieb@31369
  1165
chaieb@29687
  1166
lemma X_fps_compose_startby0[simp]: "a$0 = 0 \<Longrightarrow> X oo a = (a :: ('a :: comm_ring_1) fps)"
huffman@29913
  1167
  by (simp add: fps_eq_iff fps_compose_def mult_delta_left setsum_delta
huffman@29913
  1168
                power_Suc not_le)
chaieb@29687
  1169
chaieb@29687
  1170
huffman@29906
  1171
subsection {* Rules from Herbert Wilf's Generatingfunctionology*}
chaieb@29687
  1172
huffman@29906
  1173
subsubsection {* Rule 1 *}
chaieb@29687
  1174
  (* {a_{n+k}}_0^infty Corresponds to (f - setsum (\<lambda>i. a_i * x^i))/x^h, for h>0*)
chaieb@29687
  1175
huffman@30488
  1176
lemma fps_power_mult_eq_shift:
chaieb@30992
  1177
  "X^Suc k * Abs_fps (\<lambda>n. a (n + Suc k)) = Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a:: comm_ring_1) * X^i) {0 .. k}" (is "?lhs = ?rhs")
chaieb@29687
  1178
proof-
chaieb@29687
  1179
  {fix n:: nat
huffman@30488
  1180
    have "?lhs $ n = (if n < Suc k then 0 else a n)"
chaieb@29687
  1181
      unfolding X_power_mult_nth by auto
chaieb@29687
  1182
    also have "\<dots> = ?rhs $ n"
chaieb@29687
  1183
    proof(induct k)
chaieb@29687
  1184
      case 0 thus ?case by (simp add: fps_setsum_nth power_Suc)
chaieb@29687
  1185
    next
chaieb@29687
  1186
      case (Suc k)
chaieb@29687
  1187
      note th = Suc.hyps[symmetric]
chaieb@30992
  1188
      have "(Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a) * X^i) {0 .. Suc k})$n = (Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a) * X^i) {0 .. k} - fps_const (a (Suc k)) * X^ Suc k) $ n" by (simp add: ring_simps)
chaieb@29687
  1189
      also  have "\<dots> = (if n < Suc k then 0 else a n) - (fps_const (a (Suc k)) * X^ Suc k)$n"
huffman@30488
  1190
	using th
chaieb@29687
  1191
	unfolding fps_sub_nth by simp
chaieb@29687
  1192
      also have "\<dots> = (if n < Suc (Suc k) then 0 else a n)"
chaieb@29687
  1193
	unfolding X_power_mult_right_nth
chaieb@29687
  1194
	apply (auto simp add: not_less fps_const_def)
chaieb@29687
  1195
	apply (rule cong[of a a, OF refl])
chaieb@29687
  1196
	by arith
chaieb@29687
  1197
      finally show ?case by simp
chaieb@29687
  1198
    qed
chaieb@29687
  1199
    finally have "?lhs $ n = ?rhs $ n"  .}
chaieb@29687
  1200
  then show ?thesis by (simp add: fps_eq_iff)
chaieb@29687
  1201
qed
chaieb@29687
  1202
huffman@29906
  1203
subsubsection{* Rule 2*}
chaieb@29687
  1204
chaieb@29687
  1205
  (* We can not reach the form of Wilf, but still near to it using rewrite rules*)
huffman@30488
  1206
  (* If f reprents {a_n} and P is a polynomial, then
chaieb@29687
  1207
        P(xD) f represents {P(n) a_n}*)
chaieb@29687
  1208
chaieb@29687
  1209
definition "XD = op * X o fps_deriv"
chaieb@29687
  1210
chaieb@29687
  1211
lemma XD_add[simp]:"XD (a + b) = XD a + XD (b :: ('a::comm_ring_1) fps)"
chaieb@29687
  1212
  by (simp add: XD_def ring_simps)
chaieb@29687
  1213
chaieb@29687
  1214
lemma XD_mult_const[simp]:"XD (fps_const (c::'a::comm_ring_1) * a) = fps_const c * XD a"
chaieb@29687
  1215
  by (simp add: XD_def ring_simps)
chaieb@29687
  1216
chaieb@29687
  1217
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)"
chaieb@29687
  1218
  by simp
chaieb@29687
  1219
haftmann@30952
  1220
lemma XDN_linear:
haftmann@30971
  1221
  "(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)"
chaieb@29687
  1222
  by (induct n, simp_all)
chaieb@29687
  1223
chaieb@29687
  1224
lemma fps_mult_X_deriv_shift: "X* fps_deriv a = Abs_fps (\<lambda>n. of_nat n* a$n)" by (simp add: fps_eq_iff)
chaieb@29687
  1225
chaieb@30994
  1226
haftmann@30952
  1227
lemma fps_mult_XD_shift:
haftmann@31021
  1228
  "(XD ^^ k) (a:: ('a::{comm_ring_1}) fps) = Abs_fps (\<lambda>n. (of_nat n ^ k) * a$n)"
haftmann@30952
  1229
  by (induct k arbitrary: a) (simp_all add: power_Suc XD_def fps_eq_iff ring_simps del: One_nat_def)
chaieb@29687
  1230
huffman@29906
  1231
subsubsection{* Rule 3 is trivial and is given by @{text fps_times_def}*}
huffman@29906
  1232
subsubsection{* Rule 5 --- summation and "division" by (1 - X)*}
chaieb@29687
  1233
chaieb@29687
  1234
lemma fps_divide_X_minus1_setsum_lemma:
chaieb@29687
  1235
  "a = ((1::('a::comm_ring_1) fps) - X) * Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
chaieb@29687
  1236
proof-
chaieb@29687
  1237
  let ?X = "X::('a::comm_ring_1) fps"
chaieb@29687
  1238
  let ?sa = "Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
chaieb@29687
  1239
  have th0: "\<And>i. (1 - (X::'a fps)) $ i = (if i = 0 then 1 else if i = 1 then - 1 else 0)" by simp
chaieb@29687
  1240
  {fix n:: nat
huffman@30488
  1241
    {assume "n=0" hence "a$n = ((1 - ?X) * ?sa) $ n"
chaieb@29687
  1242
	by (simp add: fps_mult_nth)}
chaieb@29687
  1243
    moreover
chaieb@29687
  1244
    {assume n0: "n \<noteq> 0"
chaieb@29687
  1245
      then have u: "{0} \<union> ({1} \<union> {2..n}) = {0..n}" "{1}\<union>{2..n} = {1..n}"
chaieb@29687
  1246
	"{0..n - 1}\<union>{n} = {0..n}"
chaieb@29687
  1247
	apply (simp_all add: expand_set_eq) by presburger+
huffman@30488
  1248
      have d: "{0} \<inter> ({1} \<union> {2..n}) = {}" "{1} \<inter> {2..n} = {}"
chaieb@29687
  1249
	"{0..n - 1}\<inter>{n} ={}" using n0
chaieb@29687
  1250
	by (simp_all add: expand_set_eq, presburger+)
huffman@30488
  1251
      have f: "finite {0}" "finite {1}" "finite {2 .. n}"
huffman@30488
  1252
	"finite {0 .. n - 1}" "finite {n}" by simp_all
chaieb@29687
  1253
    have "((1 - ?X) * ?sa) $ n = setsum (\<lambda>i. (1 - ?X)$ i * ?sa $ (n - i)) {0 .. n}"
chaieb@29687
  1254
      by (simp add: fps_mult_nth)
chaieb@29687
  1255
    also have "\<dots> = a$n" unfolding th0
chaieb@29687
  1256
      unfolding setsum_Un_disjoint[OF f(1) finite_UnI[OF f(2,3)] d(1), unfolded u(1)]
chaieb@29687
  1257
      unfolding setsum_Un_disjoint[OF f(2) f(3) d(2)]
chaieb@29687
  1258
      apply (simp)
chaieb@29687
  1259
      unfolding setsum_Un_disjoint[OF f(4,5) d(3), unfolded u(3)]
chaieb@29687
  1260
      by simp
chaieb@29687
  1261
    finally have "a$n = ((1 - ?X) * ?sa) $ n" by simp}
chaieb@29687
  1262
  ultimately have "a$n = ((1 - ?X) * ?sa) $ n" by blast}
huffman@30488
  1263
then show ?thesis
chaieb@29687
  1264
  unfolding fps_eq_iff by blast
chaieb@29687
  1265
qed
chaieb@29687
  1266
chaieb@29687
  1267
lemma fps_divide_X_minus1_setsum:
chaieb@29687
  1268
  "a /((1::('a::field) fps) - X)  = Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
chaieb@29687
  1269
proof-
chaieb@29687
  1270
  let ?X = "1 - (X::('a::field) fps)"
chaieb@29687
  1271
  have th0: "?X $ 0 \<noteq> 0" by simp
chaieb@29687
  1272
  have "a /?X = ?X *  Abs_fps (\<lambda>n\<Colon>nat. setsum (op $ a) {0..n}) * inverse ?X"
chaieb@29687
  1273
    using fps_divide_X_minus1_setsum_lemma[of a, symmetric] th0
chaieb@29687
  1274
    by (simp add: fps_divide_def mult_assoc)
chaieb@29687
  1275
  also have "\<dots> = (inverse ?X * ?X) * Abs_fps (\<lambda>n\<Colon>nat. setsum (op $ a) {0..n}) "
chaieb@29687
  1276
    by (simp add: mult_ac)
chaieb@29687
  1277
  finally show ?thesis by (simp add: inverse_mult_eq_1[OF th0])
chaieb@29687
  1278
qed
chaieb@29687
  1279
huffman@30488
  1280
subsubsection{* Rule 4 in its more general form: generalizes Rule 3 for an arbitrary
chaieb@29687
  1281
  finite product of FPS, also the relvant instance of powers of a FPS*}
chaieb@29687
  1282
chaieb@29687
  1283
definition "natpermute n k = {l:: nat list. length l = k \<and> foldl op + 0 l = n}"
chaieb@29687
  1284
chaieb@29687
  1285
lemma natlist_trivial_1: "natpermute n 1 = {[n]}"
chaieb@29687
  1286
  apply (auto simp add: natpermute_def)
chaieb@29687
  1287
  apply (case_tac x, auto)
chaieb@29687
  1288
  done
chaieb@29687
  1289
huffman@30488
  1290
lemma foldl_add_start0:
chaieb@29687
  1291
  "foldl op + x xs = x + foldl op + (0::nat) xs"
chaieb@29687
  1292
  apply (induct xs arbitrary: x)
chaieb@29687
  1293
  apply simp
chaieb@29687
  1294
  unfolding foldl.simps
chaieb@29687
  1295
  apply atomize
chaieb@29687
  1296
  apply (subgoal_tac "\<forall>x\<Colon>nat. foldl op + x xs = x + foldl op + (0\<Colon>nat) xs")
chaieb@29687
  1297
  apply (erule_tac x="x + a" in allE)
chaieb@29687
  1298
  apply (erule_tac x="a" in allE)
chaieb@29687
  1299
  apply simp
chaieb@29687
  1300
  apply assumption
chaieb@29687
  1301
  done
chaieb@29687
  1302
chaieb@29687
  1303
lemma foldl_add_append: "foldl op + (x::nat) (xs@ys) = foldl op + x xs + foldl op + 0 ys"
chaieb@29687
  1304
  apply (induct ys arbitrary: x xs)
chaieb@29687
  1305
  apply auto
chaieb@29687
  1306
  apply (subst (2) foldl_add_start0)
chaieb@29687
  1307
  apply simp
chaieb@29687
  1308
  apply (subst (2) foldl_add_start0)
chaieb@29687
  1309
  by simp
chaieb@29687
  1310
chaieb@29687
  1311
lemma foldl_add_setsum: "foldl op + (x::nat) xs = x + setsum (nth xs) {0..<length xs}"
chaieb@29687
  1312
proof(induct xs arbitrary: x)
chaieb@29687
  1313
  case Nil thus ?case by simp
chaieb@29687
  1314
next
chaieb@29687
  1315
  case (Cons a as x)
chaieb@29687
  1316
  have eq: "setsum (op ! (a#as)) {1..<length (a#as)} = setsum (op ! as) {0..<length as}"
chaieb@29687
  1317
    apply (rule setsum_reindex_cong [where f=Suc])
chaieb@29687
  1318
    by (simp_all add: inj_on_def)
chaieb@29687
  1319
  have f: "finite {0}" "finite {1 ..< length (a#as)}" by simp_all
chaieb@29687
  1320
  have d: "{0} \<inter> {1..<length (a#as)} = {}" by simp
chaieb@29687
  1321
  have seq: "{0} \<union> {1..<length(a#as)} = {0 ..<length (a#as)}" by auto
chaieb@29687
  1322
  have "foldl op + x (a#as) = x + foldl op + a as "
chaieb@29687
  1323
    apply (subst foldl_add_start0)    by simp
chaieb@29687
  1324
  also have "\<dots> = x + a + setsum (op ! as) {0..<length as}" unfolding Cons.hyps by simp
chaieb@29687
  1325
  also have "\<dots> = x + setsum (op ! (a#as)) {0..<length (a#as)}"
huffman@30488
  1326
    unfolding eq[symmetric]
chaieb@29687
  1327
    unfolding setsum_Un_disjoint[OF f d, unfolded seq]
chaieb@29687
  1328
    by simp
chaieb@29687
  1329
  finally show ?case  .
chaieb@29687
  1330
qed
chaieb@29687
  1331
chaieb@29687
  1332
chaieb@29687
  1333
lemma append_natpermute_less_eq:
chaieb@29687
  1334
  assumes h: "xs@ys \<in> natpermute n k" shows "foldl op + 0 xs \<le> n" and "foldl op + 0 ys \<le> n"
chaieb@29687
  1335
proof-
chaieb@29687
  1336
  {from h have "foldl op + 0 (xs@ ys) = n" by (simp add: natpermute_def)
chaieb@29687
  1337
    hence "foldl op + 0 xs + foldl op + 0 ys = n" unfolding foldl_add_append .}
chaieb@29687
  1338
  note th = this
chaieb@29687
  1339
  {from th show "foldl op + 0 xs \<le> n" by simp}
chaieb@29687
  1340
  {from th show "foldl op + 0 ys \<le> n" by simp}
chaieb@29687
  1341
qed
chaieb@29687
  1342
chaieb@29687
  1343
lemma natpermute_split:
chaieb@29687
  1344
  assumes mn: "h \<le> k"
chaieb@29687
  1345
  shows "natpermute n k = (\<Union>m \<in>{0..n}. {l1 @ l2 |l1 l2. l1 \<in> natpermute m h \<and> l2 \<in> natpermute (n - m) (k - h)})" (is "?L = ?R" is "?L = (\<Union>m \<in>{0..n}. ?S m)")
chaieb@29687
  1346
proof-
huffman@30488
  1347
  {fix l assume l: "l \<in> ?R"
chaieb@29687
  1348
    from l obtain m xs ys where h: "m \<in> {0..n}" and xs: "xs \<in> natpermute m h" and ys: "ys \<in> natpermute (n - m) (k - h)"  and leq: "l = xs@ys" by blast
chaieb@29687
  1349
    from xs have xs': "foldl op + 0 xs = m" by (simp add: natpermute_def)
chaieb@29687
  1350
    from ys have ys': "foldl op + 0 ys = n - m" by (simp add: natpermute_def)
huffman@30488
  1351
    have "l \<in> ?L" using leq xs ys h
chaieb@29687
  1352
      apply simp
chaieb@29687
  1353
      apply (clarsimp simp add: natpermute_def simp del: foldl_append)
chaieb@29687
  1354
      apply (simp add: foldl_add_append[unfolded foldl_append])
chaieb@29687
  1355
      unfolding xs' ys'
huffman@30488
  1356
      using mn xs ys
chaieb@29687
  1357
      unfolding natpermute_def by simp}
chaieb@29687
  1358
  moreover
chaieb@29687
  1359
  {fix l assume l: "l \<in> natpermute n k"
chaieb@29687
  1360
    let ?xs = "take h l"
chaieb@29687
  1361
    let ?ys = "drop h l"
chaieb@29687
  1362
    let ?m = "foldl op + 0 ?xs"
chaieb@29687
  1363
    from l have ls: "foldl op + 0 (?xs @ ?ys) = n" by (simp add: natpermute_def)
huffman@30488
  1364
    have xs: "?xs \<in> natpermute ?m h" using l mn by (simp add: natpermute_def)
chaieb@29687
  1365
    have ys: "?ys \<in> natpermute (n - ?m) (k - h)" using l mn ls[unfolded foldl_add_append]
chaieb@29687
  1366
      by (simp add: natpermute_def)
chaieb@29687
  1367
    from ls have m: "?m \<in> {0..n}"  unfolding foldl_add_append by simp
huffman@30488
  1368
    from xs ys ls have "l \<in> ?R"
chaieb@29687
  1369
      apply auto
chaieb@29687
  1370
      apply (rule bexI[where x = "?m"])
chaieb@29687
  1371
      apply (rule exI[where x = "?xs"])
chaieb@29687
  1372
      apply (rule exI[where x = "?ys"])
huffman@30488
  1373
      using ls l unfolding foldl_add_append
chaieb@29687
  1374
      by (auto simp add: natpermute_def)}
chaieb@29687
  1375
  ultimately show ?thesis by blast
chaieb@29687
  1376
qed
chaieb@29687
  1377
chaieb@29687
  1378
lemma natpermute_0: "natpermute n 0 = (if n = 0 then {[]} else {})"
chaieb@29687
  1379
  by (auto simp add: natpermute_def)
chaieb@29687
  1380
lemma natpermute_0'[simp]: "natpermute 0 k = (if k = 0 then {[]} else {replicate k 0})"
chaieb@29687
  1381
  apply (auto simp add: set_replicate_conv_if natpermute_def)
chaieb@29687
  1382
  apply (rule nth_equalityI)
chaieb@29687
  1383
  by simp_all
chaieb@29687
  1384
chaieb@29687
  1385
lemma natpermute_finite: "finite (natpermute n k)"
chaieb@29687
  1386
proof(induct k arbitrary: n)
huffman@30488
  1387
  case 0 thus ?case
chaieb@29687
  1388
    apply (subst natpermute_split[of 0 0, simplified])
chaieb@29687
  1389
    by (simp add: natpermute_0)
chaieb@29687
  1390
next
chaieb@29687
  1391
  case (Suc k)
chaieb@29687
  1392
  then show ?case unfolding natpermute_split[of k "Suc k", simplified]
chaieb@29687
  1393
    apply -
chaieb@29687
  1394
    apply (rule finite_UN_I)
chaieb@29687
  1395
    apply simp
chaieb@29687
  1396
    unfolding One_nat_def[symmetric] natlist_trivial_1
chaieb@29687
  1397
    apply simp
chaieb@29687
  1398
    done
chaieb@29687
  1399
qed
chaieb@29687
  1400
chaieb@29687
  1401
lemma natpermute_contain_maximal:
chaieb@29687
  1402
  "{xs \<in> natpermute n (k+1). n \<in> set xs} = UNION {0 .. k} (\<lambda>i. {(replicate (k+1) 0) [i:=n]})"
chaieb@29687
  1403
  (is "?A = ?B")
chaieb@29687
  1404
proof-
chaieb@29687
  1405
  {fix xs assume H: "xs \<in> natpermute n (k+1)" and n: "n \<in> set xs"
chaieb@29687
  1406
    from n obtain i where i: "i \<in> {0.. k}" "xs!i = n" using H
huffman@30488
  1407
      unfolding in_set_conv_nth by (auto simp add: less_Suc_eq_le natpermute_def)
chaieb@29687
  1408
    have eqs: "({0..k} - {i}) \<union> {i} = {0..k}" using i by auto
chaieb@29687
  1409
    have f: "finite({0..k} - {i})" "finite {i}" by auto
chaieb@29687
  1410
    have d: "({0..k} - {i}) \<inter> {i} = {}" using i by auto
chaieb@29687
  1411
    from H have "n = setsum (nth xs) {0..k}" apply (simp add: natpermute_def)
chaieb@29687
  1412
      unfolding foldl_add_setsum by (auto simp add: atLeastLessThanSuc_atLeastAtMost)
chaieb@29687
  1413
    also have "\<dots> = n + setsum (nth xs) ({0..k} - {i})"
chaieb@29687
  1414
      unfolding setsum_Un_disjoint[OF f d, unfolded eqs] using i by simp
chaieb@29687
  1415
    finally have zxs: "\<forall> j\<in> {0..k} - {i}. xs!j = 0" by auto
chaieb@29687
  1416
    from H have xsl: "length xs = k+1" by (simp add: natpermute_def)
chaieb@29687
  1417
    from i have i': "i < length (replicate (k+1) 0)"   "i < k+1"
chaieb@29687
  1418
      unfolding length_replicate  by arith+
chaieb@29687
  1419
    have "xs = replicate (k+1) 0 [i := n]"
chaieb@29687
  1420
      apply (rule nth_equalityI)
chaieb@29687
  1421
      unfolding xsl length_list_update length_replicate
chaieb@29687
  1422
      apply simp
chaieb@29687
  1423
      apply clarify
chaieb@29687
  1424
      unfolding nth_list_update[OF i'(1)]
chaieb@29687
  1425
      using i zxs
chaieb@29687
  1426
      by (case_tac "ia=i", auto simp del: replicate.simps)
chaieb@29687
  1427
    then have "xs \<in> ?B" using i by blast}
chaieb@29687
  1428
  moreover
chaieb@29687
  1429
  {fix i assume i: "i \<in> {0..k}"
chaieb@29687
  1430
    let ?xs = "replicate (k+1) 0 [i:=n]"
chaieb@29687
  1431
    have nxs: "n \<in> set ?xs"
chaieb@29687
  1432
      apply (rule set_update_memI) using i by simp
chaieb@29687
  1433
    have xsl: "length ?xs = k+1" by (simp only: length_replicate length_list_update)
chaieb@29687
  1434
    have "foldl op + 0 ?xs = setsum (nth ?xs) {0..<k+1}"
chaieb@29687
  1435
      unfolding foldl_add_setsum add_0 length_replicate length_list_update ..
chaieb@29687
  1436
    also have "\<dots> = setsum (\<lambda>j. if j = i then n else 0) {0..< k+1}"
chaieb@29687
  1437
      apply (rule setsum_cong2) by (simp del: replicate.simps)
chaieb@29687
  1438
    also have "\<dots> = n" using i by (simp add: setsum_delta)
huffman@30488
  1439
    finally
chaieb@29687
  1440
    have "?xs \<in> natpermute n (k+1)" using xsl unfolding natpermute_def Collect_def mem_def
chaieb@29687
  1441
      by blast
chaieb@29687
  1442
    then have "?xs \<in> ?A"  using nxs  by blast}
chaieb@29687
  1443
  ultimately show ?thesis by auto
chaieb@29687
  1444
qed
chaieb@29687
  1445
huffman@30488
  1446
    (* The general form *)
chaieb@29687
  1447
lemma fps_setprod_nth:
chaieb@29687
  1448
  fixes m :: nat and a :: "nat \<Rightarrow> ('a::comm_ring_1) fps"
chaieb@29687
  1449
  shows "(setprod a {0 .. m})$n = setsum (\<lambda>v. setprod (\<lambda>j. (a j) $ (v!j)) {0..m}) (natpermute n (m+1))"
chaieb@29687
  1450
  (is "?P m n")
chaieb@29687
  1451
proof(induct m arbitrary: n rule: nat_less_induct)
chaieb@29687
  1452
  fix m n assume H: "\<forall>m' < m. \<forall>n. ?P m' n"
chaieb@29687
  1453
  {assume m0: "m = 0"
chaieb@29687
  1454
    hence "?P m n" apply simp
chaieb@29687
  1455
      unfolding natlist_trivial_1[where n = n, unfolded One_nat_def] by simp}
chaieb@29687
  1456
  moreover
chaieb@29687
  1457
  {fix k assume k: "m = Suc k"
chaieb@29687
  1458
    have km: "k < m" using k by arith
chaieb@29687
  1459
    have u0: "{0 .. k} \<union> {m} = {0..m}" using k apply (simp add: expand_set_eq) by presburger
chaieb@29687
  1460
    have f0: "finite {0 .. k}" "finite {m}" by auto
chaieb@29687
  1461
    have d0: "{0 .. k} \<inter> {m} = {}" using k by auto
chaieb@29687
  1462
    have "(setprod a {0 .. m}) $ n = (setprod a {0 .. k} * a m) $ n"
chaieb@29687
  1463
      unfolding setprod_Un_disjoint[OF f0 d0, unfolded u0] by simp
chaieb@29687
  1464
    also have "\<dots> = (\<Sum>i = 0..n. (\<Sum>v\<in>natpermute i (k + 1). \<Prod>j\<in>{0..k}. a j $ v ! j) * a m $ (n - i))"
chaieb@29687
  1465
      unfolding fps_mult_nth H[rule_format, OF km] ..
chaieb@29687
  1466
    also have "\<dots> = (\<Sum>v\<in>natpermute n (m + 1). \<Prod>j\<in>{0..m}. a j $ v ! j)"
chaieb@29687
  1467
      apply (simp add: k)
chaieb@29687
  1468
      unfolding natpermute_split[of m "m + 1", simplified, of n, unfolded natlist_trivial_1[unfolded One_nat_def] k]
chaieb@29687
  1469
      apply (subst setsum_UN_disjoint)
huffman@30488
  1470
      apply simp
chaieb@29687
  1471
      apply simp
chaieb@29687
  1472
      unfolding image_Collect[symmetric]
chaieb@29687
  1473
      apply clarsimp
chaieb@29687
  1474
      apply (rule finite_imageI)
chaieb@29687
  1475
      apply (rule natpermute_finite)
chaieb@29687
  1476
      apply (clarsimp simp add: expand_set_eq)
chaieb@29687
  1477
      apply auto
chaieb@29687
  1478
      apply (rule setsum_cong2)
chaieb@29687
  1479
      unfolding setsum_left_distrib
chaieb@29687
  1480
      apply (rule sym)
chaieb@29687
  1481
      apply (rule_tac f="\<lambda>xs. xs @[n - x]" in  setsum_reindex_cong)
chaieb@29687
  1482
      apply (simp add: inj_on_def)
chaieb@29687
  1483
      apply auto
chaieb@29687
  1484
      unfolding setprod_Un_disjoint[OF f0 d0, unfolded u0, unfolded k]
chaieb@29687
  1485
      apply (clarsimp simp add: natpermute_def nth_append)
chaieb@29687
  1486
      done
chaieb@29687
  1487
    finally have "?P m n" .}
chaieb@29687
  1488
  ultimately show "?P m n " by (cases m, auto)
chaieb@29687
  1489
qed
chaieb@29687
  1490
chaieb@29687
  1491
text{* The special form for powers *}
chaieb@29687
  1492
lemma fps_power_nth_Suc:
chaieb@29687
  1493
  fixes m :: nat and a :: "('a::comm_ring_1) fps"
chaieb@29687
  1494
  shows "(a ^ Suc m)$n = setsum (\<lambda>v. setprod (\<lambda>j. a $ (v!j)) {0..m}) (natpermute n (m+1))"
chaieb@29687
  1495
proof-
chaieb@29687
  1496
  have f: "finite {0 ..m}" by simp
chaieb@29687
  1497
  have th0: "a^Suc m = setprod (\<lambda>i. a) {0..m}" unfolding setprod_constant[OF f, of a] by simp
chaieb@29687
  1498
  show ?thesis unfolding th0 fps_setprod_nth ..
chaieb@29687
  1499
qed
chaieb@29687
  1500
lemma fps_power_nth:
chaieb@29687
  1501
  fixes m :: nat and a :: "('a::comm_ring_1) fps"
chaieb@29687
  1502
  shows "(a ^m)$n = (if m=0 then 1$n else setsum (\<lambda>v. setprod (\<lambda>j. a $ (v!j)) {0..m - 1}) (natpermute n m))"
huffman@30273
  1503
  by (cases m, simp_all add: fps_power_nth_Suc del: power_Suc)
chaieb@29687
  1504
huffman@30488
  1505
lemma fps_nth_power_0:
haftmann@31021
  1506
  fixes m :: nat and a :: "('a::{comm_ring_1}) fps"
chaieb@29687
  1507
  shows "(a ^m)$0 = (a$0) ^ m"
chaieb@29687
  1508
proof-
chaieb@29687
  1509
  {assume "m=0" hence ?thesis by simp}
chaieb@29687
  1510
  moreover
chaieb@29687
  1511
  {fix n assume m: "m = Suc n"
chaieb@29687
  1512
    have c: "m = card {0..n}" using m by simp
chaieb@29687
  1513
   have "(a ^m)$0 = setprod (\<lambda>i. a$0) {0..n}"
nipkow@30837
  1514
     by (simp add: m fps_power_nth del: replicate.simps power_Suc)
chaieb@29687
  1515
   also have "\<dots> = (a$0) ^ m"
chaieb@29687
  1516
     unfolding c by (rule setprod_constant, simp)
chaieb@29687
  1517
   finally have ?thesis .}
chaieb@29687
  1518
 ultimately show ?thesis by (cases m, auto)
chaieb@29687
  1519
qed
chaieb@29687
  1520
huffman@30488
  1521
lemma fps_compose_inj_right:
haftmann@31021
  1522
  assumes a0: "a$0 = (0::'a::{idom})"
chaieb@29687
  1523
  and a1: "a$1 \<noteq> 0"
chaieb@29687
  1524
  shows "(b oo a = c oo a) \<longleftrightarrow> b = c" (is "?lhs \<longleftrightarrow>?rhs")
chaieb@29687
  1525
proof-
chaieb@29687
  1526
  {assume ?rhs then have "?lhs" by simp}
chaieb@29687
  1527
  moreover
chaieb@29687
  1528
  {assume h: ?lhs
huffman@30488
  1529
    {fix n have "b$n = c$n"
chaieb@29687
  1530
      proof(induct n rule: nat_less_induct)
chaieb@29687
  1531
	fix n assume H: "\<forall>m<n. b$m = c$m"
chaieb@29687
  1532
	{assume n0: "n=0"
chaieb@29687
  1533
	  from h have "(b oo a)$n = (c oo a)$n" by simp
chaieb@29687
  1534
	  hence "b$n = c$n" using n0 by (simp add: fps_compose_nth)}
chaieb@29687
  1535
	moreover
chaieb@29687
  1536
	{fix n1 assume n1: "n = Suc n1"
chaieb@29687
  1537
	  have f: "finite {0 .. n1}" "finite {n}" by simp_all
chaieb@29687
  1538
	  have eq: "{0 .. n1} \<union> {n} = {0 .. n}" using n1 by auto
chaieb@29687
  1539
	  have d: "{0 .. n1} \<inter> {n} = {}" using n1 by auto
chaieb@29687
  1540
	  have seq: "(\<Sum>i = 0..n1. b $ i * a ^ i $ n) = (\<Sum>i = 0..n1. c $ i * a ^ i $ n)"
chaieb@29687
  1541
	    apply (rule setsum_cong2)
chaieb@29687
  1542
	    using H n1 by auto
chaieb@29687
  1543
	  have th0: "(b oo a) $n = (\<Sum>i = 0..n1. c $ i * a ^ i $ n) + b$n * (a$1)^n"
chaieb@29687
  1544
	    unfolding fps_compose_nth setsum_Un_disjoint[OF f d, unfolded eq] seq
chaieb@29687
  1545
	    using startsby_zero_power_nth_same[OF a0]
chaieb@29687
  1546
	    by simp
chaieb@29687
  1547
	  have th1: "(c oo a) $n = (\<Sum>i = 0..n1. c $ i * a ^ i $ n) + c$n * (a$1)^n"
chaieb@29687
  1548
	    unfolding fps_compose_nth setsum_Un_disjoint[OF f d, unfolded eq]
chaieb@29687
  1549
	    using startsby_zero_power_nth_same[OF a0]
chaieb@29687
  1550
	    by simp
chaieb@29687
  1551
	  from h[unfolded fps_eq_iff, rule_format, of n] th0 th1 a1
chaieb@29687
  1552
	  have "b$n = c$n" by auto}
chaieb@29687
  1553
	ultimately show "b$n = c$n" by (cases n, auto)
chaieb@29687
  1554
      qed}
chaieb@29687
  1555
    then have ?rhs by (simp add: fps_eq_iff)}
chaieb@29687
  1556
  ultimately show ?thesis by blast
chaieb@29687
  1557
qed
chaieb@29687
  1558
chaieb@29687
  1559
huffman@29906
  1560
subsection {* Radicals *}
chaieb@29687
  1561
chaieb@29687
  1562
declare setprod_cong[fundef_cong]
haftmann@31021
  1563
function radical :: "(nat \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> ('a::{field}) fps \<Rightarrow> nat \<Rightarrow> 'a" where
chaieb@29687
  1564
  "radical r 0 a 0 = 1"
chaieb@29687
  1565
| "radical r 0 a (Suc n) = 0"
chaieb@29687
  1566
| "radical r (Suc k) a 0 = r (Suc k) (a$0)"
chaieb@29687
  1567
| "radical r (Suc k) a (Suc n) = (a$ Suc n - setsum (\<lambda>xs. setprod (\<lambda>j. radical r (Suc k) a (xs ! j)) {0..k}) {xs. xs \<in> natpermute (Suc n) (Suc k) \<and> Suc n \<notin> set xs}) / (of_nat (Suc k) * (radical r (Suc k) a 0)^k)"
chaieb@29687
  1568
by pat_completeness auto
chaieb@29687
  1569
chaieb@29687
  1570
termination radical
chaieb@29687
  1571
proof
chaieb@29687
  1572
  let ?R = "measure (\<lambda>(r, k, a, n). n)"
chaieb@29687
  1573
  {
chaieb@29687
  1574
    show "wf ?R" by auto}
chaieb@29687
  1575
  {fix r k a n xs i
chaieb@29687
  1576
    assume xs: "xs \<in> {xs \<in> natpermute (Suc n) (Suc k). Suc n \<notin> set xs}" and i: "i \<in> {0..k}"
chaieb@29687
  1577
    {assume c: "Suc n \<le> xs ! i"
chaieb@29687
  1578
      from xs i have "xs !i \<noteq> Suc n" by (auto simp add: in_set_conv_nth natpermute_def)
chaieb@29687
  1579
      with c have c': "Suc n < xs!i" by arith
chaieb@29687
  1580
      have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}" by simp_all
chaieb@29687
  1581
      have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}" by auto
chaieb@29687
  1582
      have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})" using i by auto
chaieb@29687
  1583
      from xs have "Suc n = foldl op + 0 xs" by (simp add: natpermute_def)
chaieb@29687
  1584
      also have "\<dots> = setsum (nth xs) {0..<Suc k}" unfolding foldl_add_setsum using xs
chaieb@29687
  1585
	by (simp add: natpermute_def)
chaieb@29687
  1586
      also have "\<dots> = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
chaieb@29687
  1587
	unfolding eqs  setsum_Un_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
chaieb@29687
  1588
	unfolding setsum_Un_disjoint[OF fths(2) fths(3) d(2)]
chaieb@29687
  1589
	by simp
chaieb@29687
  1590
      finally have False using c' by simp}
huffman@30488
  1591
    then show "((r,Suc k,a,xs!i), r, Suc k, a, Suc n) \<in> ?R"
chaieb@29687
  1592
      apply auto by (metis not_less)}
huffman@30488
  1593
  {fix r k a n
chaieb@29687
  1594
    show "((r,Suc k, a, 0),r, Suc k, a, Suc n) \<in> ?R" by simp}
chaieb@29687
  1595
qed
chaieb@29687
  1596
chaieb@29687
  1597
definition "fps_radical r n a = Abs_fps (radical r n a)"
chaieb@29687
  1598
chaieb@29687
  1599
lemma fps_radical0[simp]: "fps_radical r 0 a = 1"
chaieb@29687
  1600
  apply (auto simp add: fps_eq_iff fps_radical_def)  by (case_tac n, auto)
chaieb@29687
  1601
chaieb@29687
  1602
lemma fps_radical_nth_0[simp]: "fps_radical r n a $ 0 = (if n=0 then 1 else r n (a$0))"
chaieb@29687
  1603
  by (cases n, simp_all add: fps_radical_def)
chaieb@29687
  1604
huffman@30488
  1605
lemma fps_radical_power_nth[simp]:
chaieb@29687
  1606
  assumes r: "(r k (a$0)) ^ k = a$0"
chaieb@29687
  1607
  shows "fps_radical r k a ^ k $ 0 = (if k = 0 then 1 else a$0)"
chaieb@29687
  1608
proof-
chaieb@29687
  1609
  {assume "k=0" hence ?thesis by simp }
chaieb@29687
  1610
  moreover
huffman@30488
  1611
  {fix h assume h: "k = Suc h"
chaieb@29687
  1612
    have fh: "finite {0..h}" by simp
chaieb@29687
  1613
    have eq1: "fps_radical r k a ^ k $ 0 = (\<Prod>j\<in>{0..h}. fps_radical r k a $ (replicate k 0) ! j)"
chaieb@29687
  1614
      unfolding fps_power_nth h by simp
chaieb@29687
  1615
    also have "\<dots> = (\<Prod>j\<in>{0..h}. r k (a$0))"
chaieb@29687
  1616
      apply (rule setprod_cong)
chaieb@29687
  1617
      apply simp
chaieb@29687
  1618
      using h
chaieb@29687
  1619
      apply (subgoal_tac "replicate k (0::nat) ! x = 0")
chaieb@29687
  1620
      by (auto intro: nth_replicate simp del: replicate.simps)
chaieb@29687
  1621
    also have "\<dots> = a$0"
chaieb@29687
  1622
      unfolding setprod_constant[OF fh] using r by (simp add: h)
chaieb@29687
  1623
    finally have ?thesis using h by simp}
chaieb@29687
  1624
  ultimately show ?thesis by (cases k, auto)
huffman@30488
  1625
qed
chaieb@29687
  1626
huffman@30488
  1627
lemma natpermute_max_card: assumes n0: "n\<noteq>0"
chaieb@29687
  1628
  shows "card {xs \<in> natpermute n (k+1). n \<in> set xs} = k+1"
chaieb@29687
  1629
  unfolding natpermute_contain_maximal
chaieb@29687
  1630
proof-
chaieb@29687
  1631
  let ?A= "\<lambda>i. {replicate (k + 1) 0[i := n]}"
chaieb@29687
  1632
  let ?K = "{0 ..k}"
chaieb@29687
  1633
  have fK: "finite ?K" by simp
chaieb@29687
  1634
  have fAK: "\<forall>i\<in>?K. finite (?A i)" by auto
chaieb@29687
  1635
  have d: "\<forall>i\<in> ?K. \<forall>j\<in> ?K. i \<noteq> j \<longrightarrow> {replicate (k + 1) 0[i := n]} \<inter> {replicate (k + 1) 0[j := n]} = {}"
chaieb@29687
  1636
  proof(clarify)
chaieb@29687
  1637
    fix i j assume i: "i \<in> ?K" and j: "j\<in> ?K" and ij: "i\<noteq>j"
chaieb@29687
  1638
    {assume eq: "replicate (k+1) 0 [i:=n] = replicate (k+1) 0 [j:= n]"
chaieb@29687
  1639
      have "(replicate (k+1) 0 [i:=n] ! i) = n" using i by (simp del: replicate.simps)
chaieb@29687
  1640
      moreover
chaieb@29687
  1641
      have "(replicate (k+1) 0 [j:=n] ! i) = 0" using i ij by (simp del: replicate.simps)
chaieb@29687
  1642
      ultimately have False using eq n0 by (simp del: replicate.simps)}
chaieb@29687
  1643
    then show "{replicate (k + 1) 0[i := n]} \<inter> {replicate (k + 1) 0[j := n]} = {}"
chaieb@29687
  1644
      by auto
chaieb@29687
  1645
  qed
huffman@30488
  1646
  from card_UN_disjoint[OF fK fAK d]
chaieb@29687
  1647
  show "card (\<Union>i\<in>{0..k}. {replicate (k + 1) 0[i := n]}) = k+1" by simp
chaieb@29687
  1648
qed
huffman@30488
  1649
huffman@30488
  1650
lemma power_radical:
huffman@31273
  1651
  fixes a:: "'a::field_char_0 fps"
chaieb@31073
  1652
  assumes a0: "a$0 \<noteq> 0"
chaieb@31073
  1653
  shows "(r (Suc k) (a$0)) ^ Suc k = a$0 \<longleftrightarrow> (fps_radical r (Suc k) a) ^ (Suc k) = a"
chaieb@31073
  1654
proof-
chaieb@31073
  1655
  let ?r = "fps_radical r (Suc k) a"
chaieb@31073
  1656
  {assume r0: "(r (Suc k) (a$0)) ^ Suc k = a$0"
chaieb@31073
  1657
    from a0 r0 have r00: "r (Suc k) (a$0) \<noteq> 0" by auto
chaieb@31073
  1658
    {fix z have "?r ^ Suc k $ z = a$z"
chaieb@31073
  1659
      proof(induct z rule: nat_less_induct)
chaieb@31073
  1660
	fix n assume H: "\<forall>m<n. ?r ^ Suc k $ m = a$m"
chaieb@31073
  1661
	{assume "n = 0" hence "?r ^ Suc k $ n = a $n"
chaieb@31073
  1662
	    using fps_radical_power_nth[of r "Suc k" a, OF r0] by simp}
chaieb@31073
  1663
	moreover
chaieb@31073
  1664
	{fix n1 assume n1: "n = Suc n1"
chaieb@31073
  1665
	  have fK: "finite {0..k}" by simp
chaieb@31073
  1666
	  have nz: "n \<noteq> 0" using n1 by arith
chaieb@31073
  1667
	  let ?Pnk = "natpermute n (k + 1)"
chaieb@31073
  1668
	  let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
chaieb@31073
  1669
	  let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
chaieb@31073
  1670
	  have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
chaieb@31073
  1671
	  have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
chaieb@31073
  1672
	  have f: "finite ?Pnkn" "finite ?Pnknn"
chaieb@31073
  1673
	    using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
chaieb@31073
  1674
	    by (metis natpermute_finite)+
chaieb@31073
  1675
	  let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
chaieb@31073
  1676
	  have "setsum ?f ?Pnkn = setsum (\<lambda>v. ?r $ n * r (Suc k) (a $ 0) ^ k) ?Pnkn"
chaieb@31073
  1677
	  proof(rule setsum_cong2)
chaieb@31073
  1678
	    fix v assume v: "v \<in> {xs \<in> natpermute n (k + 1). n \<in> set xs}"
chaieb@31073
  1679
	    let ?ths = "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) = fps_radical r (Suc k) a $ n * r (Suc k) (a $ 0) ^ k"
chaieb@31073
  1680
	  from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
chaieb@31073
  1681
	    unfolding natpermute_contain_maximal by auto
chaieb@31073
  1682
	  have "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) = (\<Prod>j\<in>{0..k}. if j = i then fps_radical r (Suc k) a $ n else r (Suc k) (a$0))"
chaieb@31073
  1683
	    apply (rule setprod_cong, simp)
chaieb@31073
  1684
	    using i r0 by (simp del: replicate.simps)
chaieb@31073
  1685
	  also have "\<dots> = (fps_radical r (Suc k) a $ n) * r (Suc k) (a$0) ^ k"
chaieb@31073
  1686
	    unfolding setprod_gen_delta[OF fK] using i r0 by simp
chaieb@31073
  1687
	  finally show ?ths .
chaieb@31073
  1688
	qed
chaieb@31073
  1689
	then have "setsum ?f ?Pnkn = of_nat (k+1) * ?r $ n * r (Suc k) (a $ 0) ^ k"
chaieb@31073
  1690
	  by (simp add: natpermute_max_card[OF nz, simplified])
chaieb@31073
  1691
	also have "\<dots> = a$n - setsum ?f ?Pnknn"
chaieb@31073
  1692
	  unfolding n1 using r00 a0 by (simp add: field_simps fps_radical_def del: of_nat_Suc )
chaieb@31073
  1693
	finally have fn: "setsum ?f ?Pnkn = a$n - setsum ?f ?Pnknn" .
chaieb@31073
  1694
	have "(?r ^ Suc k)$n = setsum ?f ?Pnkn + setsum ?f ?Pnknn"
chaieb@31073
  1695
	  unfolding fps_power_nth_Suc setsum_Un_disjoint[OF f d, unfolded eq] ..
chaieb@31073
  1696
	also have "\<dots> = a$n" unfolding fn by simp
chaieb@31073
  1697
	finally have "?r ^ Suc k $ n = a $n" .}
chaieb@31073
  1698
      ultimately  show "?r ^ Suc k $ n = a $n" by (cases n, auto)
chaieb@31073
  1699
    qed }
chaieb@31073
  1700
  then have ?thesis using r0 by (simp add: fps_eq_iff)}
chaieb@31073
  1701
moreover 
chaieb@31073
  1702
{ assume h: "(fps_radical r (Suc k) a) ^ (Suc k) = a"
chaieb@31073
  1703
  hence "((fps_radical r (Suc k) a) ^ (Suc k))$0 = a$0" by simp
chaieb@31073
  1704
  then have "(r (Suc k) (a$0)) ^ Suc k = a$0"
chaieb@31073
  1705
    unfolding fps_power_nth_Suc
chaieb@31073
  1706
    by (simp add: setprod_constant del: replicate.simps)}
chaieb@31073
  1707
ultimately show ?thesis by blast
chaieb@31073
  1708
qed
chaieb@31073
  1709
chaieb@31073
  1710
(*
chaieb@31073
  1711
lemma power_radical:
huffman@31273
  1712
  fixes a:: "'a::field_char_0 fps"
chaieb@29687
  1713
  assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 \<noteq> 0"
huffman@30488
  1714
  shows "(fps_radical r (Suc k) a) ^ (Suc k) = a"
chaieb@29687
  1715
proof-
chaieb@29687
  1716
  let ?r = "fps_radical r (Suc k) a"
chaieb@29687
  1717
  from a0 r0 have r00: "r (Suc k) (a$0) \<noteq> 0" by auto
chaieb@29687
  1718
  {fix z have "?r ^ Suc k $ z = a$z"
chaieb@29687
  1719
    proof(induct z rule: nat_less_induct)
chaieb@29687
  1720
      fix n assume H: "\<forall>m<n. ?r ^ Suc k $ m = a$m"
chaieb@29687
  1721
      {assume "n = 0" hence "?r ^ Suc k $ n = a $n"
chaieb@29687
  1722
	  using fps_radical_power_nth[of r "Suc k" a, OF r0] by simp}
chaieb@29687
  1723
      moreover
chaieb@29687
  1724
      {fix n1 assume n1: "n = Suc n1"
chaieb@29687
  1725
	have fK: "finite {0..k}" by simp
chaieb@29687
  1726
	have nz: "n \<noteq> 0" using n1 by arith
chaieb@29687
  1727
	let ?Pnk = "natpermute n (k + 1)"
chaieb@29687
  1728
	let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
chaieb@29687
  1729
	let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
chaieb@29687
  1730
	have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
chaieb@29687
  1731
	have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
huffman@30488
  1732
	have f: "finite ?Pnkn" "finite ?Pnknn"
chaieb@29687
  1733
	  using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
chaieb@29687
  1734
	  by (metis natpermute_finite)+
chaieb@29687
  1735
	let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
huffman@30488
  1736
	have "setsum ?f ?Pnkn = setsum (\<lambda>v. ?r $ n * r (Suc k) (a $ 0) ^ k) ?Pnkn"
chaieb@29687
  1737
	proof(rule setsum_cong2)
chaieb@29687
  1738
	  fix v assume v: "v \<in> {xs \<in> natpermute n (k + 1). n \<in> set xs}"
chaieb@29687
  1739
	  let ?ths = "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) = fps_radical r (Suc k) a $ n * r (Suc k) (a $ 0) ^ k"
chaieb@29687
  1740
	  from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
chaieb@29687
  1741
	    unfolding natpermute_contain_maximal by auto
chaieb@29687
  1742
	  have "(\<Prod>j\<in>{0..k}. fps_radical r (Suc k) a $ v ! j) = (\<Prod>j\<in>{0..k}. if j = i then fps_radical r (Suc k) a $ n else r (Suc k) (a$0))"
chaieb@29687
  1743
	    apply (rule setprod_cong, simp)
chaieb@29687
  1744
	    using i r0 by (simp del: replicate.simps)
chaieb@29687
  1745
	  also have "\<dots> = (fps_radical r (Suc k) a $ n) * r (Suc k) (a$0) ^ k"
chaieb@29687
  1746
	    unfolding setprod_gen_delta[OF fK] using i r0 by simp
chaieb@29687
  1747
	  finally show ?ths .
chaieb@29687
  1748
	qed
huffman@30488
  1749
	then have "setsum ?f ?Pnkn = of_nat (k+1) * ?r $ n * r (Suc k) (a $ 0) ^ k"
huffman@30488
  1750
	  by (simp add: natpermute_max_card[OF nz, simplified])
chaieb@29687
  1751
	also have "\<dots> = a$n - setsum ?f ?Pnknn"
chaieb@29687
  1752
	  unfolding n1 using r00 a0 by (simp add: field_simps fps_radical_def del: of_nat_Suc )
chaieb@29687
  1753
	finally have fn: "setsum ?f ?Pnkn = a$n - setsum ?f ?Pnknn" .
huffman@30488
  1754
	have "(?r ^ Suc k)$n = setsum ?f ?Pnkn + setsum ?f ?Pnknn"
chaieb@29687
  1755
	  unfolding fps_power_nth_Suc setsum_Un_disjoint[OF f d, unfolded eq] ..
chaieb@29687
  1756
	also have "\<dots> = a$n" unfolding fn by simp
chaieb@29687
  1757
	finally have "?r ^ Suc k $ n = a $n" .}
chaieb@29687
  1758
      ultimately  show "?r ^ Suc k $ n = a $n" by (cases n, auto)
chaieb@29687
  1759
  qed }
chaieb@29687
  1760
  then show ?thesis by (simp add: fps_eq_iff)
chaieb@29687
  1761
qed
chaieb@29687
  1762
chaieb@31073
  1763
*)
chaieb@29687
  1764
lemma eq_divide_imp': assumes c0: "(c::'a::field) ~= 0" and eq: "a * c = b"
huffman@30488
  1765
  shows "a = b / c"
chaieb@29687
  1766
proof-
chaieb@29687
  1767
  from eq have "a * c * inverse c = b * inverse c" by simp
chaieb@29687
  1768
  hence "a * (inverse c * c) = b/c" by (simp only: field_simps divide_inverse)
chaieb@29687
  1769
  then show "a = b/c" unfolding  field_inverse[OF c0] by simp
chaieb@29687
  1770
qed
chaieb@29687
  1771
huffman@30488
  1772
lemma radical_unique:
huffman@30488
  1773
  assumes r0: "(r (Suc k) (b$0)) ^ Suc k = b$0"
huffman@31273
  1774
  and a0: "r (Suc k) (b$0 ::'a::field_char_0) = a$0" and b0: "b$0 \<noteq> 0"
chaieb@29687
  1775
  shows "a^(Suc k) = b \<longleftrightarrow> a = fps_radical r (Suc k) b"
chaieb@29687
  1776
proof-
chaieb@29687
  1777
  let ?r = "fps_radical r (Suc k) b"
chaieb@29687
  1778
  have r00: "r (Suc k) (b$0) \<noteq> 0" using b0 r0 by auto
chaieb@29687
  1779
  {assume H: "a = ?r"
chaieb@31073
  1780
    from H have "a^Suc k = b" using power_radical[OF b0, of r k, unfolded r0] by simp}
chaieb@29687
  1781
  moreover
chaieb@29687
  1782
  {assume H: "a^Suc k = b"
chaieb@29687
  1783
    have ceq: "card {0..k} = Suc k" by simp
chaieb@29687
  1784
    have fk: "finite {0..k}" by simp
chaieb@29687
  1785
    from a0 have a0r0: "a$0 = ?r$0" by simp
chaieb@29687
  1786
    {fix n have "a $ n = ?r $ n"
chaieb@29687
  1787
      proof(induct n rule: nat_less_induct)
chaieb@29687
  1788
	fix n assume h: "\<forall>m<n. a$m = ?r $m"
chaieb@29687
  1789
	{assume "n = 0" hence "a$n = ?r $n" using a0 by simp }
chaieb@29687
  1790
	moreover
chaieb@29687
  1791
	{fix n1 assume n1: "n = Suc n1"
chaieb@29687
  1792
	  have fK: "finite {0..k}" by simp
chaieb@29687
  1793
	have nz: "n \<noteq> 0" using n1 by arith
chaieb@29687
  1794
	let ?Pnk = "natpermute n (Suc k)"
chaieb@29687
  1795
	let ?Pnkn = "{xs \<in> ?Pnk. n \<in> set xs}"
chaieb@29687
  1796
	let ?Pnknn = "{xs \<in> ?Pnk. n \<notin> set xs}"
chaieb@29687
  1797
	have eq: "?Pnkn \<union> ?Pnknn = ?Pnk" by blast
chaieb@29687
  1798
	have d: "?Pnkn \<inter> ?Pnknn = {}" by blast
huffman@30488
  1799
	have f: "finite ?Pnkn" "finite ?Pnknn"
chaieb@29687
  1800
	  using finite_Un[of ?Pnkn ?Pnknn, unfolded eq]
chaieb@29687
  1801
	  by (metis natpermute_finite)+
chaieb@29687
  1802
	let ?f = "\<lambda>v. \<Prod>j\<in>{0..k}. ?r $ v ! j"
chaieb@29687
  1803
	let ?g = "\<lambda>v. \<Prod>j\<in>{0..k}. a $ v ! j"
huffman@30488
  1804
	have "setsum ?g ?Pnkn = setsum (\<lambda>v. a $ n * (?r$0)^k) ?Pnkn"
chaieb@29687
  1805
	proof(rule setsum_cong2)
chaieb@29687
  1806
	  fix v assume v: "v \<in> {xs \<in> natpermute n (Suc k). n \<in> set xs}"
chaieb@29687
  1807
	  let ?ths = "(\<Prod>j\<in>{0..k}. a $ v ! j) = a $ n * (?r$0)^k"
chaieb@29687
  1808
	  from v obtain i where i: "i \<in> {0..k}" "v = replicate (k+1) 0 [i:= n]"
nipkow@31790
  1809
	    unfolding Suc_eq_plus1 natpermute_contain_maximal by (auto simp del: replicate.simps)
chaieb@29687
  1810
	  have "(\<Prod>j\<in>{0..k}. a $ v ! j) = (\<Prod>j\<in>{0..k}. if j = i then a $ n else r (Suc k) (b$0))"
chaieb@29687
  1811
	    apply (rule setprod_cong, simp)
chaieb@29687
  1812
	    using i a0 by (simp del: replicate.simps)
chaieb@29687
  1813
	  also have "\<dots> = a $ n * (?r $ 0)^k"
chaieb@29687
  1814
	    unfolding  setprod_gen_delta[OF fK] using i by simp
chaieb@29687
  1815
	  finally show ?ths .
chaieb@29687
  1816
	qed
huffman@30488
  1817
	then have th0: "setsum ?g ?Pnkn = of_nat (k+1) * a $ n * (?r $ 0)^k"
chaieb@29687
  1818
	  by (simp add: natpermute_max_card[OF nz, simplified])
chaieb@29687
  1819
	have th1: "setsum ?g ?Pnknn = setsum ?f ?Pnknn"
chaieb@29687
  1820
	proof (rule setsum_cong2, rule setprod_cong, simp)
chaieb@29687
  1821
	  fix xs i assume xs: "xs \<in> ?Pnknn" and i: "i \<in> {0..k}"
chaieb@29687
  1822
	  {assume c: "n \<le> xs ! i"
chaieb@29687
  1823
	    from xs i have "xs !i \<noteq> n" by (auto simp add: in_set_conv_nth natpermute_def)
chaieb@29687
  1824
	    with c have c': "n < xs!i" by arith
chaieb@29687
  1825
	    have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}" by simp_all
chaieb@29687
  1826
	    have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}" by auto
chaieb@29687
  1827
	    have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})" using i by auto
chaieb@29687
  1828
	    from xs have "n = foldl op + 0 xs" by (simp add: natpermute_def)
chaieb@29687
  1829
	    also have "\<dots> = setsum (nth xs) {0..<Suc k}" unfolding foldl_add_setsum using xs
chaieb@29687
  1830
	      by (simp add: natpermute_def)
chaieb@29687
  1831
	    also have "\<dots> = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
chaieb@29687
  1832
	      unfolding eqs  setsum_Un_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
chaieb@29687
  1833
	      unfolding setsum_Un_disjoint[OF fths(2) fths(3) d(2)]
chaieb@29687
  1834
	      by simp
chaieb@29687
  1835
	    finally have False using c' by simp}
chaieb@29687
  1836
	  then have thn: "xs!i < n" by arith
huffman@30488
  1837
	  from h[rule_format, OF thn]
chaieb@29687
  1838
	  show "a$(xs !i) = ?r$(xs!i)" .
chaieb@29687
  1839
	qed
chaieb@29687
  1840
	have th00: "\<And>(x::'a). of_nat (Suc k) * (x * inverse (of_nat (Suc k))) = x"
chaieb@29687
  1841
	  by (simp add: field_simps del: of_nat_Suc)
chaieb@29687
  1842
	from H have "b$n = a^Suc k $ n" by (simp add: fps_eq_iff)
chaieb@29687
  1843
	also have "a ^ Suc k$n = setsum ?g ?Pnkn + setsum ?g ?Pnknn"
huffman@30488
  1844
	  unfolding fps_power_nth_Suc
nipkow@31790
  1845
	  using setsum_Un_disjoint[OF f d, unfolded Suc_eq_plus1[symmetric],
chaieb@29687
  1846
	    unfolded eq, of ?g] by simp
chaieb@29687
  1847
	also have "\<dots> = of_nat (k+1) * a $ n * (?r $ 0)^k + setsum ?f ?Pnknn" unfolding th0 th1 ..
chaieb@29687
  1848
	finally have "of_nat (k+1) * a $ n * (?r $ 0)^k = b$n - setsum ?f ?Pnknn" by simp
chaieb@29687
  1849
	then have "a$n = (b$n - setsum ?f ?Pnknn) / (of_nat (k+1) * (?r $ 0)^k)"
huffman@30488
  1850
	  apply -
chaieb@29687
  1851
	  apply (rule eq_divide_imp')
chaieb@29687
  1852
	  using r00
chaieb@29687
  1853
	  apply (simp del: of_nat_Suc)
chaieb@29687
  1854
	  by (simp add: mult_ac)
chaieb@29687
  1855
	then have "a$n = ?r $n"
chaieb@29687
  1856
	  apply (simp del: of_nat_Suc)
chaieb@29687
  1857
	  unfolding fps_radical_def n1
huffman@29911
  1858
	  by (simp add: field_simps n1 th00 del: of_nat_Suc)}
chaieb@29687
  1859
	ultimately show "a$n = ?r $ n" by (cases n, auto)
chaieb@29687
  1860
      qed}
chaieb@29687
  1861
    then have "a = ?r" by (simp add: fps_eq_iff)}
chaieb@29687
  1862
  ultimately show ?thesis by blast
chaieb@29687
  1863
qed
chaieb@29687
  1864
chaieb@29687
  1865
huffman@30488
  1866
lemma radical_power:
huffman@30488
  1867
  assumes r0: "r (Suc k) ((a$0) ^ Suc k) = a$0"
huffman@31273
  1868
  and a0: "(a$0 ::'a::field_char_0) \<noteq> 0"
chaieb@29687
  1869
  shows "(fps_radical r (Suc k) (a ^ Suc k)) = a"
chaieb@29687
  1870
proof-
chaieb@29687
  1871
  let ?ak = "a^ Suc k"
huffman@30273
  1872
  have ak0: "?ak $ 0 = (a$0) ^ Suc k" by (simp add: fps_nth_power_0 del: power_Suc)
chaieb@29687
  1873
  from r0 have th0: "r (Suc k) (a ^ Suc k $ 0) ^ Suc k = a ^ Suc k $ 0" using ak0 by auto
chaieb@29687
  1874
  from r0 ak0 have th1: "r (Suc k) (a ^ Suc k $ 0) = a $ 0" by auto
chaieb@29687
  1875
  from ak0 a0 have ak00: "?ak $ 0 \<noteq>0 " by auto
chaieb@29687
  1876
  from radical_unique[of r k ?ak a, OF th0 th1 ak00] show ?thesis by metis
chaieb@29687
  1877
qed
chaieb@29687
  1878
huffman@30488
  1879
lemma fps_deriv_radical:
huffman@31273
  1880
  fixes a:: "'a::field_char_0 fps"
chaieb@29687
  1881
  assumes r0: "(r (Suc k) (a$0)) ^ Suc k = a$0" and a0: "a$0 \<noteq> 0"
chaieb@29687
  1882
  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)"
chaieb@29687
  1883
proof-
chaieb@29687
  1884
  let ?r= "fps_radical r (Suc k) a"
chaieb@29687
  1885
  let ?w = "(fps_const (of_nat (Suc k)) * ?r ^ k)"
chaieb@29687
  1886
  from a0 r0 have r0': "r (Suc k) (a$0) \<noteq> 0" by auto
chaieb@29687
  1887
  from r0' have w0: "?w $ 0 \<noteq> 0" by (simp del: of_nat_Suc)
chaieb@29687
  1888
  note th0 = inverse_mult_eq_1[OF w0]
chaieb@29687
  1889
  let ?iw = "inverse ?w"
chaieb@31073
  1890
  from iffD1[OF power_radical[of a r], OF a0 r0]
chaieb@29687
  1891
  have "fps_deriv (?r ^ Suc k) = fps_deriv a" by simp
chaieb@29687
  1892
  hence "fps_deriv ?r * ?w = fps_deriv a"
huffman@30273
  1893
    by (simp add: fps_deriv_power mult_ac del: power_Suc)
chaieb@29687
  1894
  hence "?iw * fps_deriv ?r * ?w = ?iw * fps_deriv a" by simp
chaieb@29687
  1895
  hence "fps_deriv ?r * (?iw * ?w) = fps_deriv a / ?w"
chaieb@29687
  1896
    by (simp add: fps_divide_def)
huffman@30488
  1897
  then show ?thesis unfolding th0 by simp
chaieb@29687
  1898
qed
chaieb@29687
  1899
huffman@30488
  1900
lemma radical_mult_distrib:
huffman@31273
  1901
  fixes a:: "'a::field_char_0 fps"
huffman@30488
  1902
  assumes
chaieb@31073
  1903
  k: "k > 0"
chaieb@31073
  1904
  and ra0: "r k (a $ 0) ^ k = a $ 0"
chaieb@31073
  1905
  and rb0: "r k (b $ 0) ^ k = b $ 0"
chaieb@31073
  1906
  and a0: "a$0 \<noteq> 0"
chaieb@31073
  1907
  and b0: "b$0 \<noteq> 0"
chaieb@31073
  1908
  shows "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0) \<longleftrightarrow> fps_radical r (k) (a*b) = fps_radical r (k) a * fps_radical r (k) (b)"
chaieb@31073
  1909
proof-
chaieb@31073
  1910
  {assume  r0': "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0)"
chaieb@31073
  1911
  from r0' have r0: "(r (k) ((a*b)$0)) ^ k = (a*b)$0"
chaieb@31073
  1912
    by (simp add: fps_mult_nth ra0 rb0 power_mult_distrib)
chaieb@31073
  1913
  {assume "k=0" hence ?thesis using r0' by simp}
chaieb@31073
  1914
  moreover
chaieb@31073
  1915
  {fix h assume k: "k = Suc h"
chaieb@31073
  1916
  let ?ra = "fps_radical r (Suc h) a"
chaieb@31073
  1917
  let ?rb = "fps_radical r (Suc h) b"
chaieb@31073
  1918
  have th0: "r (Suc h) ((a * b) $ 0) = (fps_radical r (Suc h) a * fps_radical r (Suc h) b) $ 0"
chaieb@31073
  1919
    using r0' k by (simp add: fps_mult_nth)
chaieb@31073
  1920
  have ab0: "(a*b) $ 0 \<noteq> 0" using a0 b0 by (simp add: fps_mult_nth)
chaieb@31073
  1921
  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]
chaieb@31073
  1922
    iffD1[OF power_radical[of _ r], OF a0 ra0[unfolded k]] iffD1[OF power_radical[of _ r], OF b0 rb0[unfolded k]] k r0'
chaieb@31073
  1923
  have ?thesis by (auto simp add: power_mult_distrib simp del: power_Suc)}
chaieb@31073
  1924
ultimately have ?thesis by (cases k, auto)}
chaieb@31073
  1925
moreover
chaieb@31073
  1926
{assume h: "fps_radical r k (a*b) = fps_radical r k a * fps_radical r k b"
chaieb@31073
  1927
  hence "(fps_radical r k (a*b))$0 = (fps_radical r k a * fps_radical r k b)$0" by simp
chaieb@31073
  1928
  then have "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0)"
chaieb@31073
  1929
    using k by (simp add: fps_mult_nth)}
chaieb@31073
  1930
ultimately show ?thesis by blast
chaieb@31073
  1931
qed
chaieb@31073
  1932
chaieb@31073
  1933
(*
chaieb@31073
  1934
lemma radical_mult_distrib:
huffman@31273
  1935
  fixes a:: "'a::field_char_0 fps"
chaieb@31073
  1936
  assumes
chaieb@31073
  1937
  ra0: "r k (a $ 0) ^ k = a $ 0"
chaieb@31073
  1938
  and rb0: "r k (b $ 0) ^ k = b $ 0"
chaieb@31073
  1939
  and r0': "r k ((a * b) $ 0) = r k (a $ 0) * r k (b $ 0)"
chaieb@29687
  1940
  and a0: "a$0 \<noteq> 0"
chaieb@29687
  1941
  and b0: "b$0 \<noteq> 0"
chaieb@29687
  1942
  shows "fps_radical r (k) (a*b) = fps_radical r (k) a * fps_radical r (k) (b)"
chaieb@29687
  1943
proof-
chaieb@29687
  1944
  from r0' have r0: "(r (k) ((a*b)$0)) ^ k = (a*b)$0"
chaieb@29687
  1945
    by (simp add: fps_mult_nth ra0 rb0 power_mult_distrib)
chaieb@29687
  1946
  {assume "k=0" hence ?thesis by simp}
chaieb@29687
  1947
  moreover
chaieb@29687
  1948
  {fix h assume k: "k = Suc h"
chaieb@29687
  1949
  let ?ra = "fps_radical r (Suc h) a"
chaieb@29687
  1950
  let ?rb = "fps_radical r (Suc h) b"
huffman@30488
  1951
  have th0: "r (Suc h) ((a * b) $ 0) = (fps_radical r (Suc h) a * fps_radical r (Suc h) b) $ 0"
chaieb@29687
  1952
    using r0' k by (simp add: fps_mult_nth)
chaieb@29687
  1953
  have ab0: "(a*b) $ 0 \<noteq> 0" using a0 b0 by (simp add: fps_mult_nth)
huffman@30488
  1954
  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]
chaieb@29687
  1955
    power_radical[of r, OF ra0[unfolded k] a0] power_radical[of r, OF rb0[unfolded k] b0] k
huffman@30273
  1956
  have ?thesis by (auto simp add: power_mult_distrib simp del: power_Suc)}
chaieb@29687
  1957
ultimately show ?thesis by (cases k, auto)
chaieb@29687
  1958
qed
chaieb@31073
  1959
*)
chaieb@29687
  1960
chaieb@31073
  1961
lemma fps_divide_1[simp]: "(a:: ('a::field) fps) / 1 = a"
chaieb@29687
  1962
  by (simp add: fps_divide_def)
chaieb@29687
  1963
chaieb@29687
  1964
lemma radical_divide:
huffman@31273
  1965
  fixes a :: "'a::field_char_0 fps"
huffman@30488
  1966
  assumes
chaieb@31073
  1967
  kp: "k>0"
chaieb@31073
  1968
  and ra0: "(r k (a $ 0)) ^ k = a $ 0"
chaieb@31073
  1969
  and rb0: "(r k (b $ 0)) ^ k = b $ 0"
huffman@30488
  1970
  and a0: "a$0 \<noteq> 0"
chaieb@29687
  1971
  and b0: "b$0 \<noteq> 0"
chaieb@31073
  1972
  shows "r k ((a $ 0) / (b$0)) = r k (a$0) / r k (b $ 0) \<longleftrightarrow> fps_radical r k (a/b) = fps_radical r k a / fps_radical r k b" (is "?lhs = ?rhs")
chaieb@29687
  1973
proof-
chaieb@31073
  1974
  let ?r = "fps_radical r k"
chaieb@31073
  1975
  from kp obtain h where k: "k = Suc h" by (cases k, auto)
chaieb@31073
  1976
  have ra0': "r k (a$0) \<noteq> 0" using a0 ra0 k by auto
chaieb@31073
  1977
  have rb0': "r k (b$0) \<noteq> 0" using b0 rb0 k by auto
huffman@30488
  1978
chaieb@31073
  1979
  {assume ?rhs
chaieb@31073
  1980
    then have "?r (a/b) $ 0 = (?r a / ?r b)$0" by simp
chaieb@31073
  1981
    then have ?lhs using k a0 b0 rb0' 
chaieb@31073
  1982
      by (simp add: fps_divide_def fps_mult_nth fps_inverse_def divide_inverse) }
chaieb@31073
  1983
  moreover
chaieb@31073
  1984
  {assume h: ?lhs
chaieb@31073
  1985
    from a0 b0 have ab0[simp]: "(a/b)$0 = a$0 / b$0" 
chaieb@31073
  1986
      by (simp add: fps_divide_def fps_mult_nth divide_inverse fps_inverse_def)
chaieb@31073
  1987
    have th0: "r k ((a/b)$0) ^ k = (a/b)$0"
chaieb@31073
  1988
      by (simp add: h nonzero_power_divide[OF rb0'] ra0 rb0 del: k)
chaieb@31073
  1989
    from a0 b0 ra0' rb0' kp h 
chaieb@31073
  1990
    have th1: "r k ((a / b) $ 0) = (fps_radical r k a / fps_radical r k b) $ 0"
chaieb@31073
  1991
      by (simp add: fps_divide_def fps_mult_nth fps_inverse_def divide_inverse del: k)
chaieb@31073
  1992
    from a0 b0 ra0' rb0' kp have ab0': "(a / b) $ 0 \<noteq> 0"
chaieb@31073
  1993
      by (simp add: fps_divide_def fps_mult_nth fps_inverse_def nonzero_imp_inverse_nonzero)
chaieb@31073
  1994
    note tha[simp] = iffD1[OF power_radical[where r=r and k=h], OF a0 ra0[unfolded k], unfolded k[symmetric]]
chaieb@31073
  1995
    note thb[simp] = iffD1[OF power_radical[where r=r and k=h], OF b0 rb0[unfolded k], unfolded k[symmetric]]
chaieb@31073
  1996
    have th2: "(?r a / ?r b)^k = a/b"
chaieb@31073
  1997
      by (simp add: fps_divide_def power_mult_distrib fps_inverse_power[symmetric])
chaieb@31073
  1998
    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] have ?rhs .}
chaieb@31073
  1999
  ultimately show ?thesis by blast
chaieb@29687
  2000
qed
chaieb@29687
  2001
chaieb@31073
  2002
lemma radical_inverse:
huffman@31273
  2003
  fixes a :: "'a::field_char_0 fps"
chaieb@31073
  2004
  assumes
chaieb@31073
  2005
  k: "k>0"
chaieb@31073
  2006
  and ra0: "r k (a $ 0) ^ k = a $ 0"
chaieb@31073
  2007
  and r1: "(r k 1)^k = 1"
chaieb@31073
  2008
  and a0: "a$0 \<noteq> 0"
chaieb@31073
  2009
  shows "r k (inverse (a $ 0)) = r k 1 / (r k (a $ 0)) \<longleftrightarrow> fps_radical r k (inverse a) = fps_radical r k 1 / fps_radical r k a"
chaieb@31073
  2010
  using radical_divide[where k=k and r=r and a=1 and b=a, OF k ] ra0 r1 a0
chaieb@31073
  2011
  by (simp add: divide_inverse fps_divide_def)
chaieb@31073
  2012
huffman@29906
  2013
subsection{* Derivative of composition *}
chaieb@29687
  2014
huffman@30488
  2015
lemma fps_compose_deriv:
chaieb@29687
  2016
  fixes a:: "('a::idom) fps"
chaieb@29687
  2017
  assumes b0: "b$0 = 0"
chaieb@29687
  2018
  shows "fps_deriv (a oo b) = ((fps_deriv a) oo b) * (fps_deriv b)"
chaieb@29687
  2019
proof-
chaieb@29687
  2020
  {fix n
chaieb@29687
  2021
    have "(fps_deriv (a oo b))$n = setsum (\<lambda>i. a $ i * (fps_deriv (b^i))$n) {0.. Suc n}"
chaieb@29687
  2022
      by (simp add: fps_compose_def ring_simps setsum_right_distrib del: of_nat_Suc)
chaieb@29687
  2023
    also have "\<dots> = setsum (\<lambda>i. a$i * ((fps_const (of_nat i)) * (fps_deriv b * (b^(i - 1))))$n) {0.. Suc n}"
chaieb@29687
  2024
      by (simp add: ring_simps fps_deriv_power del: fps_mult_left_const_nth of_nat_Suc)
chaieb@29687
  2025
  also have "\<dots> = setsum (\<lambda>i. of_nat i * a$i * (((b^(i - 1)) * fps_deriv b))$n) {0.. Suc n}"
chaieb@29687
  2026
    unfolding fps_mult_left_const_nth  by (simp add: ring_simps)
chaieb@29687
  2027
  also have "\<dots> = setsum (\<lambda>i. of_nat i * a$i * (setsum (\<lambda>j. (b^ (i - 1))$j * (fps_deriv b)$(n - j)) {0..n})) {0.. Suc n}"
chaieb@29687
  2028
    unfolding fps_mult_nth ..
chaieb@29687
  2029
  also have "\<dots> = setsum (\<lambda>i. of_nat i * a$i * (setsum (\<lambda>j. (b^ (i - 1))$j * (fps_deriv b)$(n - j)) {0..n})) {1.. Suc n}"
chaieb@29687
  2030
    apply (rule setsum_mono_zero_right)
huffman@29913
  2031
    apply (auto simp add: mult_delta_left setsum_delta not_le)
huffman@29913
  2032
    done
chaieb@29687
  2033
  also have "\<dots> = setsum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (setsum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}"
chaieb@29687
  2034
    unfolding fps_deriv_nth
chaieb@29687
  2035
    apply (rule setsum_reindex_cong[where f="Suc"])
chaieb@29687
  2036
    by (auto simp add: mult_assoc)
chaieb@29687
  2037
  finally have th0: "(fps_deriv (a oo b))$n = setsum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (setsum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}" .
huffman@30488
  2038
chaieb@29687
  2039
  have "(((fps_deriv a) oo b) * (fps_deriv b))$n = setsum (\<lambda>i. (fps_deriv b)$ (n - i) * ((fps_deriv a) oo b)$i) {0..n}"
chaieb@29687
  2040
    unfolding fps_mult_nth by (simp add: mult_ac)
chaieb@29687
  2041
  also have "\<dots> = setsum (\<lambda>i. setsum (\<lambda>j. of_nat (n - i +1) * b$(n - i + 1) * of_nat (j + 1) * a$(j+1) * (b^j)$i) {0..n}) {0..n}"
chaieb@29687
  2042
    unfolding fps_deriv_nth fps_compose_nth setsum_right_distrib mult_assoc
chaieb@29687
  2043
    apply (rule setsum_cong2)
chaieb@29687
  2044
    apply (rule setsum_mono_zero_left)
chaieb@29687
  2045
    apply (simp_all add: subset_eq)
chaieb@29687
  2046
    apply clarify
chaieb@29687
  2047
    apply (subgoal_tac "b^i$x = 0")
chaieb@29687
  2048
    apply simp
chaieb@29687
  2049
    apply (rule startsby_zero_power_prefix[OF b0, rule_format])
chaieb@29687
  2050
    by simp
chaieb@29687
  2051
  also have "\<dots> = setsum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (setsum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}"
chaieb@29687
  2052
    unfolding setsum_right_distrib
chaieb@29687
  2053
    apply (subst setsum_commute)
chaieb@29687
  2054
    by ((rule setsum_cong2)+) simp
chaieb@29687
  2055
  finally have "(fps_deriv (a oo b))$n = (((fps_deriv a) oo b) * (fps_deriv b)) $n"
chaieb@29687
  2056
    unfolding th0 by simp}
chaieb@29687
  2057
then show ?thesis by (simp add: fps_eq_iff)
chaieb@29687
  2058
qed
chaieb@29687
  2059
chaieb@29687
  2060
lemma fps_mult_X_plus_1_nth:
chaieb@29687
  2061
  "((1+X)*a) $n = (if n = 0 then (a$n :: 'a::comm_ring_1) else a$n + a$(n - 1))"
chaieb@29687
  2062
proof-
chaieb@29687
  2063
  {assume "n = 0" hence ?thesis by (simp add: fps_mult_nth )}
chaieb@29687
  2064
  moreover
chaieb@29687
  2065
  {fix m assume m: "n = Suc m"
chaieb@29687
  2066
    have "((1+X)*a) $n = setsum (\<lambda>i. (1+X)$i * a$(n-i)) {0..n}"
chaieb@29687
  2067
      by (simp add: fps_mult_nth)
chaieb@29687
  2068
    also have "\<dots> = setsum (\<lambda>i. (1+X)$i * a$(n-i)) {0.. 1}"
chaieb@29687
  2069
      unfolding m
chaieb@29687
  2070
      apply (rule setsum_mono_zero_right)
chaieb@29687
  2071
      by (auto simp add: )
chaieb@29687
  2072
    also have "\<dots> = (if n = 0 then (a$n :: 'a::comm_ring_1) else a$n + a$(n - 1))"
chaieb@29687
  2073
      unfolding m
chaieb@29687
  2074
      by (simp add: )
chaieb@29687
  2075
    finally have ?thesis .}
chaieb@29687
  2076
  ultimately show ?thesis by (cases n, auto)
chaieb@29687
  2077
qed
chaieb@29687
  2078
huffman@29906
  2079
subsection{* Finite FPS (i.e. polynomials) and X *}
chaieb@29687
  2080
lemma fps_poly_sum_X:
huffman@30488
  2081
  assumes z: "\<forall>i > n. a$i = (0::'a::comm_ring_1)"
chaieb@29687
  2082
  shows "a = setsum (\<lambda>i. fps_const (a$i) * X^i) {0..n}" (is "a = ?r")
chaieb@29687
  2083
proof-
chaieb@29687
  2084
  {fix i
huffman@30488
  2085
    have "a$i = ?r$i"
chaieb@29687
  2086
      unfolding fps_setsum_nth fps_mult_left_const_nth X_power_nth
huffman@29913
  2087
      by (simp add: mult_delta_right setsum_delta' z)
huffman@29913
  2088
  }
chaieb@29687
  2089
  then show ?thesis unfolding fps_eq_iff by blast
chaieb@29687
  2090
qed
chaieb@29687
  2091
huffman@29906
  2092
subsection{* Compositional inverses *}
chaieb@29687
  2093
chaieb@29687
  2094
haftmann@31021
  2095
fun compinv :: "'a fps \<Rightarrow> nat \<Rightarrow> 'a::{field}" where
chaieb@29687
  2096
  "compinv a 0 = X$0"
chaieb@29687
  2097
| "compinv a (Suc n) = (X$ Suc n - setsum (\<lambda>i. (compinv a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
chaieb@29687
  2098
chaieb@29687
  2099
definition "fps_inv a = Abs_fps (compinv a)"
chaieb@29687
  2100
chaieb@29687
  2101
lemma fps_inv: assumes a0: "a$0 = 0" and a1: "a$1 \<noteq> 0"
chaieb@29687
  2102
  shows "fps_inv a oo a = X"
chaieb@29687
  2103
proof-
chaieb@29687
  2104
  let ?i = "fps_inv a oo a"
chaieb@29687
  2105
  {fix n
huffman@30488
  2106
    have "?i $n = X$n"
chaieb@29687
  2107
    proof(induct n rule: nat_less_induct)
chaieb@29687
  2108
      fix n assume h: "\<forall>m<n. ?i$m = X$m"
huffman@30488
  2109
      {assume "n=0" hence "?i $n = X$n" using a0
chaieb@29687
  2110
	  by (simp add: fps_compose_nth fps_inv_def)}
chaieb@29687
  2111
      moreover
chaieb@29687
  2112
      {fix n1 assume n1: "n = Suc n1"
chaieb@29687
  2113
	have "?i $ n = setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1} + fps_inv a $ Suc n1 * (a $ 1)^ Suc n1"
huffman@30273
  2114
	  by (simp add: fps_compose_nth n1 startsby_zero_power_nth_same[OF a0]
huffman@30273
  2115
                   del: power_Suc)
chaieb@29687
  2116
	also have "\<dots> = setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1} + (X$ Suc n1 - setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1})"
huffman@29911
  2117
	  using a0 a1 n1 by (simp add: fps_inv_def)
huffman@30488
  2118
	also have "\<dots> = X$n" using n1 by simp
chaieb@29687
  2119
	finally have "?i $ n = X$n" .}
chaieb@29687
  2120
      ultimately show "?i $ n = X$n" by (cases n, auto)
chaieb@29687
  2121
    qed}
chaieb@29687
  2122
  then show ?thesis by (simp add: fps_eq_iff)
chaieb@29687
  2123
qed
chaieb@29687
  2124
chaieb@29687
  2125
haftmann@31021
  2126
fun gcompinv :: "'a fps \<Rightarrow> 'a fps \<Rightarrow> nat \<Rightarrow> 'a::{field}" where
chaieb@29687
  2127
  "gcompinv b a 0 = b$0"
chaieb@29687
  2128
| "gcompinv b a (Suc n) = (b$ Suc n - setsum (\<lambda>i. (gcompinv b a i) * (a^i)$Suc n) {0 .. n}) / (a$1) ^ Suc n"
chaieb@29687
  2129
chaieb@29687
  2130
definition "fps_ginv b a = Abs_fps (gcompinv b a)"
chaieb@29687
  2131
chaieb@29687
  2132
lemma fps_ginv: assumes a0: "a$0 = 0" and a1: "a$1 \<noteq> 0"
chaieb@29687
  2133
  shows "fps_ginv b a oo a = b"
chaieb@29687
  2134
proof-
chaieb@29687
  2135
  let ?i = "fps_ginv b a oo a"
chaieb@29687
  2136
  {fix n
huffman@30488
  2137
    have "?i $n = b$n"
chaieb@29687
  2138
    proof(induct n rule: nat_less_induct)
chaieb@29687
  2139
      fix n assume h: "\<forall>m<n. ?i$m = b$m"
huffman@30488
  2140
      {assume "n=0" hence "?i $n = b$n" using a0
chaieb@29687
  2141
	  by (simp add: fps_compose_nth fps_ginv_def)}
chaieb@29687
  2142
      moreover
chaieb@29687
  2143
      {fix n1 assume n1: "n = Suc n1"
chaieb@29687
  2144
	have "?i $ n = setsum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1} + fps_ginv b a $ Suc n1 * (a $ 1)^ Suc n1"
huffman@30273
  2145
	  by (simp add: fps_compose_nth n1 startsby_zero_power_nth_same[OF a0]
huffman@30273
  2146
                   del: power_Suc)
chaieb@29687
  2147
	also have "\<dots> = setsum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1} + (b$ Suc n1 - setsum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1})"
huffman@29911
  2148
	  using a0 a1 n1 by (simp add: fps_ginv_def)
huffman@30488
  2149
	also have "\<dots> = b$n" using n1 by simp
chaieb@29687
  2150
	finally have "?i $ n = b$n" .}
chaieb@29687
  2151
      ultimately show "?i $ n = b$n" by (cases n, auto)
chaieb@29687
  2152
    qed}
chaieb@29687
  2153
  then show ?thesis by (simp add: fps_eq_iff)
chaieb@29687
  2154
qed
chaieb@29687
  2155
chaieb@29687
  2156
lemma fps_inv_ginv: "fps_inv = fps_ginv X"
chaieb@29687
  2157
  apply (auto simp add: expand_fun_eq fps_eq_iff fps_inv_def fps_ginv_def)
chaieb@29687
  2158
  apply (induct_tac n rule: nat_less_induct, auto)
chaieb@29687
  2159
  apply (case_tac na)
chaieb@29687
  2160
  apply simp
chaieb@29687
  2161
  apply simp
chaieb@29687
  2162
  done
chaieb@29687
  2163
chaieb@29687
  2164
lemma fps_compose_1[simp]: "1 oo a = 1"
haftmann@30960
  2165
  by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta)
chaieb@29687
  2166
chaieb@29687
  2167
lemma fps_compose_0[simp]: "0 oo a = 0"
huffman@29913
  2168
  by (simp add: fps_eq_iff fps_compose_nth)
chaieb@29687
  2169
chaieb@29687
  2170
lemma fps_compose_0_right[simp]: "a oo 0 = fps_const (a$0)"
haftmann@30960
  2171
  by (auto simp add: fps_eq_iff fps_compose_nth power_0_left setsum_0')
chaieb@29687
  2172
chaieb@29687
  2173
lemma fps_compose_add_distrib: "(a + b) oo c = (a oo c) + (b oo c)"
haftmann@30960
  2174
  by (simp add: fps_eq_iff fps_compose_nth ring_simps setsum_addf)
chaieb@29687
  2175
chaieb@29687
  2176
lemma fps_compose_setsum_distrib: "(setsum f S) oo a = setsum (\<lambda>i. f i oo a) S"
chaieb@29687
  2177
proof-
chaieb@29687
  2178
  {assume "\<not> finite S" hence ?thesis by simp}
chaieb@29687
  2179
  moreover
chaieb@29687
  2180
  {assume fS: "finite S"
chaieb@29687
  2181
    have ?thesis
chaieb@29687
  2182
    proof(rule finite_induct[OF fS])
chaieb@29687
  2183
      show "setsum f {} oo a = (\<Sum>i\<in>{}. f i oo a)" by simp
chaieb@29687
  2184
    next
chaieb@29687
  2185
      fix x F assume fF: "finite F" and xF: "x \<notin> F" and h: "setsum f F oo a = setsum (\<lambda>i. f i oo a) F"
chaieb@29687
  2186
      show "setsum f (insert x F) oo a  = setsum (\<lambda>i. f i oo a) (insert x F)"
chaieb@29687
  2187
	using fF xF h by (simp add: fps_compose_add_distrib)
chaieb@29687
  2188
    qed}
huffman@30488
  2189
  ultimately show ?thesis by blast
chaieb@29687
  2190
qed
chaieb@29687
  2191
huffman@30488
  2192
lemma convolution_eq:
chaieb@29687
  2193
  "setsum (%i. a (i :: nat) * b (n - i)) {0 .. n} = setsum (%(i,j). a i * b j) {(i,j). i <= n \<and> j \<le> n \<and> i + j = n}"
chaieb@29687
  2194
  apply (rule setsum_reindex_cong[where f=fst])
chaieb@29687
  2195
  apply (clarsimp simp add: inj_on_def)
chaieb@29687
  2196
  apply (auto simp add: expand_set_eq image_iff)
chaieb@29687
  2197
  apply (rule_tac x= "x" in exI)
chaieb@29687
  2198
  apply clarsimp
chaieb@29687
  2199
  apply (rule_tac x="n - x" in exI)
chaieb@29687
  2200
  apply arith
chaieb@29687
  2201
  done
chaieb@29687
  2202
chaieb@29687
  2203
lemma product_composition_lemma:
chaieb@29687
  2204
  assumes c0: "c$0 = (0::'a::idom)" and d0: "d$0 = 0"
chaieb@29687
  2205
  shows "((a oo c) * (b oo d))$n = setsum (%(k,m). a$k * b$m * (c^k * d^m) $ n) {(k,m). k + m \<le> n}" (is "?l = ?r")
chaieb@29687
  2206
proof-
chaieb@29687
  2207
  let ?S = "{(k\<Colon>nat, m\<Colon>nat). k + m \<le> n}"
huffman@30488
  2208
  have s: "?S \<subseteq> {0..n} <*> {0..n}" by (auto simp add: subset_eq)
huffman@30488
  2209
  have f: "finite {(k\<Colon>nat, m\<Colon>nat). k + m \<le> n}"
chaieb@29687
  2210
    apply (rule finite_subset[OF s])
chaieb@29687
  2211
    by auto
chaieb@29687
  2212
  have "?r =  setsum (%i. setsum (%(k,m). a$k * (c^k)$i * b$m * (d^m) $ (n - i)) {(k,m). k + m \<le> n}) {0..n}"
chaieb@29687
  2213
    apply (simp add: fps_mult_nth setsum_right_distrib)
chaieb@29687
  2214
    apply (subst setsum_commute)
chaieb@29687
  2215
    apply (rule setsum_cong2)
chaieb@29687
  2216
    by (auto simp add: ring_simps)
huffman@30488
  2217
  also have "\<dots> = ?l"
chaieb@29687
  2218
    apply (simp add: fps_mult_nth fps_compose_nth setsum_product)
chaieb@29687
  2219
    apply (rule setsum_cong2)
chaieb@29687
  2220
    apply (simp add: setsum_cartesian_product mult_assoc)
chaieb@29687
  2221
    apply (rule setsum_mono_zero_right[OF f])
chaieb@29687
  2222
    apply (simp add: subset_eq) apply presburger
chaieb@29687
  2223
    apply clarsimp
chaieb@29687
  2224
    apply (rule ccontr)
chaieb@29687
  2225
    apply (clarsimp simp add: not_le)
chaieb@29687
  2226
    apply (case_tac "x < aa")
chaieb@29687
  2227
    apply simp
chaieb@29687
  2228
    apply (frule_tac startsby_zero_power_prefix[rule_format, OF c0])
chaieb@29687
  2229
    apply blast
chaieb@29687
  2230
    apply simp
chaieb@29687
  2231
    apply (frule_tac startsby_zero_power_prefix[rule_format, OF d0])
chaieb@29687
  2232
    apply blast
chaieb@29687
  2233
    done
chaieb@29687
  2234
  finally show ?thesis by simp
chaieb@29687
  2235
qed
chaieb@29687
  2236
chaieb@29687
  2237
lemma product_composition_lemma':
chaieb@29687
  2238
  assumes c0: "c$0 = (0::'a::idom)" and d0: "d$0 = 0"
chaieb@29687
  2239
  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")
chaieb@29687
  2240
  unfolding product_composition_lemma[OF c0 d0]
chaieb@29687
  2241
  unfolding setsum_cartesian_product
chaieb@29687
  2242
  apply (rule setsum_mono_zero_left)
chaieb@29687
  2243
  apply simp
chaieb@29687
  2244
  apply (clarsimp simp add: subset_eq)
chaieb@29687
  2245
  apply clarsimp
chaieb@29687
  2246
  apply (rule ccontr)
chaieb@29687
  2247
  apply (subgoal_tac "(c^aa * d^ba) $ n = 0")
chaieb@29687
  2248
  apply simp
chaieb@29687
  2249
  unfolding fps_mult_nth
chaieb@29687
  2250
  apply (rule setsum_0')
chaieb@29687
  2251
  apply (clarsimp simp add: not_le)
chaieb@29687
  2252
  apply (case_tac "aaa < aa")
chaieb@29687
  2253
  apply (rule startsby_zero_power_prefix[OF c0, rule_format])
chaieb@29687
  2254
  apply simp
chaieb@29687
  2255
  apply (subgoal_tac "n - aaa < ba")
chaieb@29687
  2256
  apply (frule_tac k = "ba" in startsby_zero_power_prefix[OF d0, rule_format])
chaieb@29687
  2257
  apply simp
chaieb@29687
  2258
  apply arith
chaieb@29687
  2259
  done
huffman@30488
  2260
chaieb@29687
  2261
huffman@30488
  2262
lemma setsum_pair_less_iff:
chaieb@29687
  2263
  "setsum (%((k::nat),m). a k * b m * c (k + m)) {(k,m). k + m \<le> n} = setsum (%s. setsum (%i. a i * b (s - i) * c s) {0..s}) {0..n}" (is "?l = ?r")
chaieb@29687
  2264
proof-
chaieb@29687
  2265
  let ?KM=  "{(k,m). k + m \<le> n}"
chaieb@29687
  2266
  let ?f = "%s. UNION {(0::nat)..s} (%i. {(i,s - i)})"
chaieb@29687
  2267
  have th0: "?KM = UNION {0..n} ?f"
chaieb@29687
  2268
    apply (simp add: expand_set_eq)
huffman@29911
  2269
    apply arith (* FIXME: VERY slow! *)
chaieb@29687
  2270
    done
chaieb@29687
  2271
  show "?l = ?r "
chaieb@29687
  2272
    unfolding th0
chaieb@29687
  2273
    apply (subst setsum_UN_disjoint)
chaieb@29687
  2274
    apply auto
chaieb@29687
  2275
    apply (subst setsum_UN_disjoint)
chaieb@29687
  2276
    apply auto
chaieb@29687
  2277
    done
chaieb@29687
  2278
qed
chaieb@29687
  2279
chaieb@29687
  2280
lemma fps_compose_mult_distrib_lemma:
chaieb@29687
  2281
  assumes c0: "c$0 = (0::'a::idom)"
chaieb@29687
  2282
  shows "((a oo c) * (b oo c))$n = setsum (%s. setsum (%i. a$i * b$(s - i) * (c^s) $ n) {0..s}) {0..n}" (is "?l = ?r")
chaieb@29687
  2283
  unfolding product_composition_lemma[OF c0 c0] power_add[symmetric]
chaieb@29687
  2284
  unfolding setsum_pair_less_iff[where a = "%k. a$k" and b="%m. b$m" and c="%s. (c ^ s)$n" and n = n] ..
chaieb@29687
  2285
chaieb@29687
  2286
huffman@30488
  2287
lemma fps_compose_mult_distrib:
chaieb@29687
  2288
  assumes c0: "c$0 = (0::'a::idom)"
chaieb@29687
  2289
  shows "(a * b) oo c = (a oo c) * (b oo c)" (is "?l = ?r")
chaieb@29687
  2290
  apply (simp add: fps_eq_iff fps_compose_mult_distrib_lemma[OF c0])
chaieb@29687
  2291
  by (simp add: fps_compose_nth fps_mult_nth setsum_left_distrib)
huffman@30488
  2292
lemma fps_compose_setprod_distrib:
chaieb@29687
  2293
  assumes c0: "c$0 = (0::'a::idom)"
chaieb@29687
  2294
  shows "(setprod a S) oo c = setprod (%k. a k oo c) S" (is "?l = ?r")
chaieb@29687
  2295
  apply (cases "finite S")
chaieb@29687
  2296
  apply simp_all
chaieb@29687
  2297
  apply (induct S rule: finite_induct)
chaieb@29687
  2298
  apply simp
chaieb@29687
  2299
  apply (simp add: fps_compose_mult_distrib[OF c0])
chaieb@29687
  2300
  done
chaieb@29687
  2301
chaieb@29687
  2302
lemma fps_compose_power:   assumes c0: "c$0 = (0::'a::idom)"
chaieb@29687
  2303
  shows "(a oo c)^n = a^n oo c" (is "?l = ?r")
chaieb@29687
  2304
proof-
chaieb@29687
  2305
  {assume "n=0" then have ?thesis by simp}
chaieb@29687
  2306
  moreover
chaieb@29687
  2307
  {fix m assume m: "n = Suc m"
chaieb@29687
  2308
    have th0: "a^n = setprod (%k. a) {0..m}" "(a oo c) ^ n = setprod (%k. a oo c) {0..m}"
chaieb@29687
  2309
      by (simp_all add: setprod_constant m)
chaieb@29687
  2310
    then have ?thesis
chaieb@29687
  2311
      by (simp add: fps_compose_setprod_distrib[OF c0])}
chaieb@29687
  2312
  ultimately show ?thesis by (cases n, auto)
chaieb@29687
  2313
qed
chaieb@29687
  2314
chaieb@31199
  2315
lemma fps_compose_uminus: "- (a::'a::ring_1 fps) oo c = - (a oo c)"
chaieb@31199
  2316
  by (simp add: fps_eq_iff fps_compose_nth ring_simps setsum_negf[symmetric])
chaieb@31199
  2317
chaieb@31199
  2318
lemma fps_compose_sub_distrib:
chaieb@31199
  2319
  shows "(a - b) oo (c::'a::ring_1 fps) = (a oo c) - (b oo c)"
chaieb@31199
  2320
  unfolding diff_minus fps_compose_uminus fps_compose_add_distrib ..
chaieb@31199
  2321
chaieb@31199
  2322
lemma X_fps_compose:"X oo a = Abs_fps (\<lambda>n. if n = 0 then (0::'a::comm_ring_1) else a$n)"
chaieb@31199
  2323
  by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta power_Suc)
chaieb@31199
  2324
chaieb@31199
  2325
lemma fps_inverse_compose:
chaieb@31199
  2326
  assumes b0: "(b$0 :: 'a::field) = 0" and a0: "a$0 \<noteq> 0"
chaieb@31199
  2327
  shows "inverse a oo b = inverse (a oo b)"
chaieb@31199
  2328
proof-
chaieb@31199
  2329
  let ?ia = "inverse a"
chaieb@31199
  2330
  let ?ab = "a oo b"
chaieb@31199
  2331
  let ?iab = "inverse ?ab"
chaieb@31199
  2332
chaieb@31199
  2333
from a0 have ia0: "?ia $ 0 \<noteq> 0" by (simp )
chaieb@31199
  2334
from a0 have ab0: "?ab $ 0 \<noteq> 0" by (simp add: fps_compose_def)
chaieb@31199
  2335
have "(?ia oo b) *  (a oo b) = 1"
chaieb@31199
  2336
unfolding fps_compose_mult_distrib[OF b0, symmetric]
chaieb@31199
  2337
unfolding inverse_mult_eq_1[OF a0]
chaieb@31199
  2338
fps_compose_1 ..
chaieb@31199
  2339
chaieb@31199
  2340
then have "(?ia oo b) *  (a oo b) * ?iab  = 1 * ?iab" by simp
chaieb@31199
  2341
then have "(?ia oo b) *  (?iab * (a oo b))  = ?iab" by simp
chaieb@31199
  2342
then show ?thesis 
chaieb@31199
  2343
  unfolding inverse_mult_eq_1[OF ab0] by simp
chaieb@31199
  2344
qed
chaieb@31199
  2345
chaieb@31199
  2346
lemma fps_divide_compose:
chaieb@31199
  2347
  assumes c0: "(c$0 :: 'a::field) = 0" and b0: "b$0 \<noteq> 0"
chaieb@31199
  2348
  shows "(a/b) oo c = (a oo c) / (b oo c)"
chaieb@31199
  2349
    unfolding fps_divide_def fps_compose_mult_distrib[OF c0]
chaieb@31199
  2350
    fps_inverse_compose[OF c0 b0] ..
chaieb@31199
  2351
chaieb@31199
  2352
lemma gp: assumes a0: "a$0 = (0::'a::field)"
chaieb@31199
  2353
  shows "(Abs_fps (\<lambda>n. 1)) oo a = 1/(1 - a)" (is "?one oo a = _")
chaieb@31199
  2354
proof-
chaieb@31199
  2355
  have o0: "?one $ 0 \<noteq> 0" by simp
chaieb@31199
  2356
  have th0: "(1 - X) $ 0 \<noteq> (0::'a)" by simp  
chaieb@31199
  2357
  from fps_inverse_gp[where ?'a = 'a]
chaieb@31199
  2358
  have "inverse ?one = 1 - X" by (simp add: fps_eq_iff)
chaieb@31199
  2359
  hence "inverse (inverse ?one) = inverse (1 - X)" by simp
chaieb@31199
  2360
  hence th: "?one = 1/(1 - X)" unfolding fps_inverse_idempotent[OF o0] 
chaieb@31199
  2361
    by (simp add: fps_divide_def)
chaieb@31199
  2362
  show ?thesis unfolding th
chaieb@31199
  2363
    unfolding fps_divide_compose[OF a0 th0]
chaieb@31199
  2364
    fps_compose_1 fps_compose_sub_distrib X_fps_compose_startby0[OF a0] ..
chaieb@31199
  2365
qed
chaieb@31199
  2366
chaieb@31199
  2367
lemma fps_const_power[simp]: "fps_const (c::'a::ring_1) ^ n = fps_const (c^n)"
chaieb@31199
  2368
by (induct n, auto)
chaieb@31199
  2369
chaieb@31199
  2370
lemma fps_compose_radical:
huffman@31273
  2371
  assumes b0: "b$0 = (0::'a::field_char_0)"
chaieb@31199
  2372
  and ra0: "r (Suc k) (a$0) ^ Suc k = a$0"
chaieb@31199
  2373
  and a0: "a$0 \<noteq> 0"
chaieb@31199
  2374
  shows "fps_radical r (Suc k)  a oo b = fps_radical r (Suc k) (a oo b)"
chaieb@31199
  2375
proof-
chaieb@31199
  2376
  let ?r = "fps_radical r (Suc k)"
chaieb@31199
  2377
  let ?ab = "a oo b"
chaieb@31199
  2378
  have ab0: "?ab $ 0 = a$0" by (simp add: fps_compose_def)
chaieb@31199
  2379
  from ab0 a0 ra0 have rab0: "?ab $ 0 \<noteq> 0" "r (Suc k) (?ab $ 0) ^ Suc k = ?ab $ 0" by simp_all
chaieb@31199
  2380
  have th00: "r (Suc k) ((a oo b) $ 0) = (fps_radical r (Suc k) a oo b) $ 0"
chaieb@31199
  2381
    by (simp add: ab0 fps_compose_def)
chaieb@31199
  2382
  have th0: "(?r a oo b) ^ (Suc k) = a  oo b"
chaieb@31199
  2383
    unfolding fps_compose_power[OF b0]
chaieb@31199
  2384
    unfolding iffD1[OF power_radical[of a r k], OF a0 ra0]  .. 
chaieb@31199
  2385
  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  . 
chaieb@31199
  2386
qed
chaieb@31199
  2387
chaieb@29687
  2388
lemma fps_const_mult_apply_left:
chaieb@29687
  2389
  "fps_const c * (a oo b) = (fps_const c * a) oo b"
chaieb@29687
  2390
  by (simp add: fps_eq_iff fps_compose_nth setsum_right_distrib mult_assoc)
chaieb@29687
  2391
chaieb@29687
  2392
lemma fps_const_mult_apply_right:
chaieb@29687
  2393
  "(a oo b) * fps_const (c::'a::comm_semiring_1) = (fps_const c * a) oo b"
chaieb@29687
  2394
  by (auto simp add: fps_const_mult_apply_left mult_commute)
chaieb@29687
  2395
huffman@30488
  2396
lemma fps_compose_assoc:
chaieb@29687
  2397
  assumes c0: "c$0 = (0::'a::idom)" and b0: "b$0 = 0"
chaieb@29687
  2398
  shows "a oo (b oo c) = a oo b oo c" (is "?l = ?r")
chaieb@29687
  2399
proof-
chaieb@29687
  2400
  {fix n
chaieb@29687
  2401
    have "?l$n = (setsum (\<lambda>i. (fps_const (a$i) * b^i) oo c) {0..n})$n"
chaieb@29687
  2402
      by (simp add: fps_compose_nth fps_compose_power[OF c0] fps_const_mult_apply_left setsum_right_distrib mult_assoc fps_setsum_nth)
chaieb@29687
  2403
    also have "\<dots> = ((setsum (\<lambda>i. fps_const (a$i) * b^i) {0..n}) oo c)$n"
chaieb@29687
  2404
      by (simp add: fps_compose_setsum_distrib)
chaieb@29687
  2405
    also have "\<dots> = ?r$n"
chaieb@29687
  2406
      apply (simp add: fps_compose_nth fps_setsum_nth setsum_left_distrib mult_assoc)
chaieb@29687
  2407
      apply (rule setsum_cong2)
chaieb@29687
  2408
      apply (rule setsum_mono_zero_right)
chaieb@29687
  2409
      apply (auto simp add: not_le)
chaieb@29687
  2410
      by (erule startsby_zero_power_prefix[OF b0, rule_format])
chaieb@29687
  2411
    finally have "?l$n = ?r$n" .}
chaieb@29687
  2412
  then show ?thesis by (simp add: fps_eq_iff)
chaieb@29687
  2413
qed
chaieb@29687
  2414
chaieb@29687
  2415
chaieb@29687
  2416
lemma fps_X_power_compose:
chaieb@29687
  2417
  assumes a0: "a$0=0" shows "X^k oo a = (a::('a::idom fps))^k" (is "?l = ?r")
chaieb@29687
  2418
proof-