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