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