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