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