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