src/HOL/Library/Formal_Power_Series.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 22 14:37:56 2016 +0000 (2016-02-22)
changeset 62379 340738057c8c
parent 62343 24106dc44def
child 62390 842917225d56
permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
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)
hoelzl@62102
   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
hoelzl@62102
   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
hoelzl@62102
   477
hoelzl@62102
   478
hoelzl@62102
   479
subsection \<open>Subdegrees\<close>
hoelzl@62102
   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
hoelzl@62102
   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"
hoelzl@62102
   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
hoelzl@62102
   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)"
hoelzl@62102
   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
hoelzl@62102
   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)
hoelzl@62102
   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)
hoelzl@62102
   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)
hoelzl@62102
   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)"
hoelzl@62102
   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
hoelzl@62102
   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
hoelzl@62101
   818
definition uniformity_fps_def [code del]:
hoelzl@62101
   819
  "(uniformity :: ('a fps \<times> 'a fps) filter) = (INF e:{0 <..}. principal {(x, y). dist x y < e})"
hoelzl@62101
   820
hoelzl@62101
   821
definition open_fps_def' [code del]:
hoelzl@62101
   822
  "open (U :: 'a fps set) \<longleftrightarrow> (\<forall>x\<in>U. eventually (\<lambda>(x', y). x' = x \<longrightarrow> y \<in> U) uniformity)"
eberlm@61608
   823
chaieb@31968
   824
instance
chaieb@31968
   825
proof
wenzelm@60501
   826
  show th: "dist a b = 0 \<longleftrightarrow> a = b" for a b :: "'a fps"
eberlm@61608
   827
    by (simp add: dist_fps_def split: split_if_asm)
eberlm@61608
   828
  then have th'[simp]: "dist a a = 0" for a :: "'a fps" by simp
wenzelm@60501
   829
chaieb@31968
   830
  fix a b c :: "'a fps"
wenzelm@60501
   831
  consider "a = b" | "c = a \<or> c = b" | "a \<noteq> b" "a \<noteq> c" "b \<noteq> c" by blast
wenzelm@60501
   832
  then show "dist a b \<le> dist a c + dist b c"
wenzelm@60501
   833
  proof cases
wenzelm@60501
   834
    case 1
eberlm@61608
   835
    then show ?thesis by (simp add: dist_fps_def)
wenzelm@60501
   836
  next
wenzelm@60501
   837
    case 2
wenzelm@60501
   838
    then show ?thesis
wenzelm@52891
   839
      by (cases "c = a") (simp_all add: th dist_fps_sym)
wenzelm@60501
   840
  next
wenzelm@60567
   841
    case neq: 3
wenzelm@60558
   842
    have False if "dist a b > dist a c + dist b c"
wenzelm@60558
   843
    proof -
eberlm@61608
   844
      let ?n = "subdegree (a - b)"
eberlm@61608
   845
      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
   846
      with that have "dist a b > dist a c" and "dist a b > dist b c" by simp_all
hoelzl@62102
   847
      with neq have "?n < subdegree (a - c)" and "?n < subdegree (b - c)"
eberlm@61608
   848
        by (simp_all add: dist_fps_def field_simps)
hoelzl@62102
   849
      hence "(a - c) $ ?n = 0" and "(b - c) $ ?n = 0"
eberlm@61608
   850
        by (simp_all only: nth_less_subdegree_zero)
eberlm@61608
   851
      hence "(a - b) $ ?n = 0" by simp
eberlm@61608
   852
      moreover from neq have "(a - b) $ ?n \<noteq> 0" by (intro nth_subdegree_nonzero) simp_all
eberlm@61608
   853
      ultimately show False by contradiction
wenzelm@60558
   854
    qed
eberlm@61608
   855
    thus ?thesis by (auto simp add: not_le[symmetric])
wenzelm@60501
   856
  qed
hoelzl@62101
   857
qed (rule open_fps_def' uniformity_fps_def)+
wenzelm@52891
   858
chaieb@31968
   859
end
chaieb@31968
   860
hoelzl@62102
   861
declare uniformity_Abort[where 'a="'a :: comm_ring_1 fps", code]
hoelzl@62102
   862
hoelzl@62101
   863
lemma open_fps_def: "open (S :: 'a::comm_ring_1 fps set) = (\<forall>a \<in> S. \<exists>r. r >0 \<and> ball a r \<subseteq> S)"
hoelzl@62101
   864
  unfolding open_dist ball_def subset_eq by simp
eberlm@61608
   865
wenzelm@60558
   866
text \<open>The infinite sums and justification of the notation in textbooks.\<close>
chaieb@31968
   867
wenzelm@52891
   868
lemma reals_power_lt_ex:
wenzelm@54681
   869
  fixes x y :: real
wenzelm@54681
   870
  assumes xp: "x > 0"
wenzelm@54681
   871
    and y1: "y > 1"
chaieb@31968
   872
  shows "\<exists>k>0. (1/y)^k < x"
wenzelm@52891
   873
proof -
wenzelm@54681
   874
  have yp: "y > 0"
wenzelm@54681
   875
    using y1 by simp
chaieb@31968
   876
  from reals_Archimedean2[of "max 0 (- log y x) + 1"]
wenzelm@54681
   877
  obtain k :: nat where k: "real k > max 0 (- log y x) + 1"
wenzelm@54681
   878
    by blast
wenzelm@54681
   879
  from k have kp: "k > 0"
wenzelm@54681
   880
    by simp
wenzelm@54681
   881
  from k have "real k > - log y x"
wenzelm@54681
   882
    by simp
wenzelm@54681
   883
  then have "ln y * real k > - ln x"
wenzelm@54681
   884
    unfolding log_def
chaieb@31968
   885
    using ln_gt_zero_iff[OF yp] y1
wenzelm@54681
   886
    by (simp add: minus_divide_left field_simps del: minus_divide_left[symmetric])
wenzelm@54681
   887
  then have "ln y * real k + ln x > 0"
wenzelm@54681
   888
    by simp
chaieb@31968
   889
  then have "exp (real k * ln y + ln x) > exp 0"
haftmann@57514
   890
    by (simp add: ac_simps)
chaieb@31968
   891
  then have "y ^ k * x > 1"
wenzelm@52891
   892
    unfolding exp_zero exp_add exp_real_of_nat_mult exp_ln [OF xp] exp_ln [OF yp]
wenzelm@52891
   893
    by simp
wenzelm@52891
   894
  then have "x > (1 / y)^k" using yp
haftmann@60867
   895
    by (simp add: field_simps)
wenzelm@54681
   896
  then show ?thesis
wenzelm@54681
   897
    using kp by blast
chaieb@31968
   898
qed
wenzelm@52891
   899
wenzelm@54452
   900
lemma fps_sum_rep_nth: "(setsum (\<lambda>i. fps_const(a$i)*X^i) {0..m})$n =
wenzelm@54681
   901
    (if n \<le> m then a$n else 0::'a::comm_ring_1)"
wenzelm@52891
   902
  apply (auto simp add: fps_setsum_nth cond_value_iff cong del: if_weak_cong)
haftmann@57418
   903
  apply (simp add: setsum.delta')
wenzelm@48757
   904
  done
wenzelm@52891
   905
wenzelm@61969
   906
lemma fps_notation: "(\<lambda>n. setsum (\<lambda>i. fps_const(a$i) * X^i) {0..n}) \<longlonglongrightarrow> a"
wenzelm@61969
   907
  (is "?s \<longlonglongrightarrow> a")
wenzelm@52891
   908
proof -
wenzelm@60558
   909
  have "\<exists>n0. \<forall>n \<ge> n0. dist (?s n) a < r" if "r > 0" for r
wenzelm@60558
   910
  proof -
wenzelm@60501
   911
    obtain n0 where n0: "(1/2)^n0 < r" "n0 > 0"
wenzelm@60501
   912
      using reals_power_lt_ex[OF \<open>r > 0\<close>, of 2] by auto
wenzelm@60558
   913
    show ?thesis
wenzelm@60501
   914
    proof -
wenzelm@60558
   915
      have "dist (?s n) a < r" if nn0: "n \<ge> n0" for n
wenzelm@60558
   916
      proof -
wenzelm@60558
   917
        from that have thnn0: "(1/2)^n \<le> (1/2 :: real)^n0"
wenzelm@60501
   918
          by (simp add: divide_simps)
wenzelm@60558
   919
        show ?thesis
wenzelm@60501
   920
        proof (cases "?s n = a")
wenzelm@60501
   921
          case True
wenzelm@60501
   922
          then show ?thesis
wenzelm@60501
   923
            unfolding dist_eq_0_iff[of "?s n" a, symmetric]
wenzelm@60501
   924
            using \<open>r > 0\<close> by (simp del: dist_eq_0_iff)
wenzelm@60501
   925
        next
wenzelm@60501
   926
          case False
eberlm@61608
   927
          from False have dth: "dist (?s n) a = (1/2)^subdegree (?s n - a)"
eberlm@61608
   928
            by (simp add: dist_fps_def field_simps)
eberlm@61608
   929
          from False have kn: "subdegree (?s n - a) > n"
hoelzl@62102
   930
            by (intro subdegree_greaterI) (simp_all add: fps_sum_rep_nth)
hoelzl@62102
   931
          then have "dist (?s n) a < (1/2)^n"
eberlm@61608
   932
            by (simp add: field_simps dist_fps_def)
wenzelm@60501
   933
          also have "\<dots> \<le> (1/2)^n0"
wenzelm@60501
   934
            using nn0 by (simp add: divide_simps)
wenzelm@60501
   935
          also have "\<dots> < r"
wenzelm@60501
   936
            using n0 by simp
wenzelm@60501
   937
          finally show ?thesis .
wenzelm@60501
   938
        qed
wenzelm@60558
   939
      qed
wenzelm@60501
   940
      then show ?thesis by blast
wenzelm@60501
   941
    qed
wenzelm@60558
   942
  qed
wenzelm@54681
   943
  then show ?thesis
lp15@60017
   944
    unfolding lim_sequentially by blast
wenzelm@52891
   945
qed
chaieb@31968
   946
wenzelm@54681
   947
wenzelm@60501
   948
subsection \<open>Inverses of formal power series\<close>
chaieb@29687
   949
haftmann@57418
   950
declare setsum.cong[fundef_cong]
chaieb@29687
   951
wenzelm@60558
   952
instantiation fps :: ("{comm_monoid_add,inverse,times,uminus}") inverse
chaieb@29687
   953
begin
chaieb@29687
   954
wenzelm@52891
   955
fun natfun_inverse:: "'a fps \<Rightarrow> nat \<Rightarrow> 'a"
wenzelm@52891
   956
where
chaieb@29687
   957
  "natfun_inverse f 0 = inverse (f$0)"
huffman@30488
   958
| "natfun_inverse f n = - inverse (f$0) * setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}"
chaieb@29687
   959
wenzelm@60501
   960
definition fps_inverse_def: "inverse f = (if f $ 0 = 0 then 0 else Abs_fps (natfun_inverse f))"
wenzelm@60501
   961
eberlm@61608
   962
definition fps_divide_def:
hoelzl@62102
   963
  "f div g = (if g = 0 then 0 else
eberlm@61608
   964
     let n = subdegree g; h = fps_shift n g
eberlm@61608
   965
     in  fps_shift n (f * inverse h))"
haftmann@36311
   966
chaieb@29687
   967
instance ..
haftmann@36311
   968
chaieb@29687
   969
end
chaieb@29687
   970
wenzelm@52891
   971
lemma fps_inverse_zero [simp]:
wenzelm@54681
   972
  "inverse (0 :: 'a::{comm_monoid_add,inverse,times,uminus} fps) = 0"
huffman@29911
   973
  by (simp add: fps_ext fps_inverse_def)
chaieb@29687
   974
wenzelm@52891
   975
lemma fps_inverse_one [simp]: "inverse (1 :: 'a::{division_ring,zero_neq_one} fps) = 1"
huffman@29911
   976
  apply (auto simp add: expand_fps_eq fps_inverse_def)
wenzelm@52891
   977
  apply (case_tac n)
wenzelm@52891
   978
  apply auto
wenzelm@52891
   979
  done
wenzelm@52891
   980
wenzelm@52891
   981
lemma inverse_mult_eq_1 [intro]:
wenzelm@52891
   982
  assumes f0: "f$0 \<noteq> (0::'a::field)"
chaieb@29687
   983
  shows "inverse f * f = 1"
wenzelm@52891
   984
proof -
wenzelm@54681
   985
  have c: "inverse f * f = f * inverse f"
haftmann@57512
   986
    by (simp add: mult.commute)
huffman@30488
   987
  from f0 have ifn: "\<And>n. inverse f $ n = natfun_inverse f n"
chaieb@29687
   988
    by (simp add: fps_inverse_def)
chaieb@29687
   989
  from f0 have th0: "(inverse f * f) $ 0 = 1"
huffman@29911
   990
    by (simp add: fps_mult_nth fps_inverse_def)
wenzelm@60501
   991
  have "(inverse f * f)$n = 0" if np: "n > 0" for n
wenzelm@60501
   992
  proof -
wenzelm@54681
   993
    from np have eq: "{0..n} = {0} \<union> {1 .. n}"
wenzelm@54681
   994
      by auto
wenzelm@54681
   995
    have d: "{0} \<inter> {1 .. n} = {}"
wenzelm@54681
   996
      by auto
wenzelm@52891
   997
    from f0 np have th0: "- (inverse f $ n) =
chaieb@29687
   998
      (setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}) / (f$0)"
wenzelm@52891
   999
      by (cases n) (simp_all add: divide_inverse fps_inverse_def)
chaieb@29687
  1000
    from th0[symmetric, unfolded nonzero_divide_eq_eq[OF f0]]
wenzelm@52891
  1001
    have th1: "setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n} = - (f$0) * (inverse f)$n"
haftmann@36350
  1002
      by (simp add: field_simps)
huffman@30488
  1003
    have "(f * inverse f) $ n = (\<Sum>i = 0..n. f $i * natfun_inverse f (n - i))"
chaieb@29687
  1004
      unfolding fps_mult_nth ifn ..
wenzelm@52891
  1005
    also have "\<dots> = f$0 * natfun_inverse f n + (\<Sum>i = 1..n. f$i * natfun_inverse f (n-i))"
bulwahn@46757
  1006
      by (simp add: eq)
wenzelm@54681
  1007
    also have "\<dots> = 0"
wenzelm@54681
  1008
      unfolding th1 ifn by simp
wenzelm@60501
  1009
    finally show ?thesis unfolding c .
wenzelm@60501
  1010
  qed
wenzelm@54681
  1011
  with th0 show ?thesis
wenzelm@54681
  1012
    by (simp add: fps_eq_iff)
chaieb@29687
  1013
qed
chaieb@29687
  1014
wenzelm@60501
  1015
lemma fps_inverse_0_iff[simp]: "(inverse f) $ 0 = (0::'a::division_ring) \<longleftrightarrow> f $ 0 = 0"
huffman@29911
  1016
  by (simp add: fps_inverse_def nonzero_imp_inverse_nonzero)
hoelzl@62102
  1017
eberlm@61608
  1018
lemma fps_inverse_nth_0 [simp]: "inverse f $ 0 = inverse (f $ 0 :: 'a :: division_ring)"
eberlm@61608
  1019
  by (simp add: fps_inverse_def)
eberlm@61608
  1020
eberlm@61608
  1021
lemma fps_inverse_eq_0_iff[simp]: "inverse f = (0:: ('a::division_ring) fps) \<longleftrightarrow> f $ 0 = 0"
wenzelm@60501
  1022
proof
eberlm@61608
  1023
  assume A: "inverse f = 0"
eberlm@61608
  1024
  have "0 = inverse f $ 0" by (subst A) simp
eberlm@61608
  1025
  thus "f $ 0 = 0" by simp
eberlm@61608
  1026
qed (simp add: fps_inverse_def)
eberlm@61608
  1027
eberlm@61608
  1028
lemma fps_inverse_idempotent[intro, simp]:
wenzelm@48757
  1029
  assumes f0: "f$0 \<noteq> (0::'a::field)"
chaieb@29687
  1030
  shows "inverse (inverse f) = f"
wenzelm@52891
  1031
proof -
chaieb@29687
  1032
  from f0 have if0: "inverse f $ 0 \<noteq> 0" by simp
huffman@30488
  1033
  from inverse_mult_eq_1[OF f0] inverse_mult_eq_1[OF if0]
wenzelm@52891
  1034
  have "inverse f * f = inverse f * inverse (inverse f)"
haftmann@57514
  1035
    by (simp add: ac_simps)
wenzelm@54681
  1036
  then show ?thesis
wenzelm@54681
  1037
    using f0 unfolding mult_cancel_left by simp
chaieb@29687
  1038
qed
chaieb@29687
  1039
wenzelm@48757
  1040
lemma fps_inverse_unique:
eberlm@61608
  1041
  assumes fg: "(f :: 'a :: field fps) * g = 1"
eberlm@61608
  1042
  shows   "inverse f = g"
wenzelm@52891
  1043
proof -
eberlm@61608
  1044
  have f0: "f $ 0 \<noteq> 0"
eberlm@61608
  1045
  proof
eberlm@61608
  1046
    assume "f $ 0 = 0"
eberlm@61608
  1047
    hence "0 = (f * g) $ 0" by simp
eberlm@61608
  1048
    also from fg have "(f * g) $ 0 = 1" by simp
eberlm@61608
  1049
    finally show False by simp
eberlm@61608
  1050
  qed
eberlm@61608
  1051
  from inverse_mult_eq_1[OF this] fg
wenzelm@54681
  1052
  have th0: "inverse f * f = g * f"
haftmann@57514
  1053
    by (simp add: ac_simps)
wenzelm@54681
  1054
  then show ?thesis
wenzelm@54681
  1055
    using f0
wenzelm@54681
  1056
    unfolding mult_cancel_right
huffman@29911
  1057
    by (auto simp add: expand_fps_eq)
chaieb@29687
  1058
qed
chaieb@29687
  1059
lp15@60162
  1060
lemma setsum_zero_lemma:
lp15@60162
  1061
  fixes n::nat
lp15@60162
  1062
  assumes "0 < n"
lp15@60162
  1063
  shows "(\<Sum>i = 0..n. if n = i then 1 else if n - i = 1 then - 1 else 0) = (0::'a::field)"
wenzelm@54681
  1064
proof -
lp15@60162
  1065
  let ?f = "\<lambda>i. if n = i then 1 else if n - i = 1 then - 1 else 0"
lp15@60162
  1066
  let ?g = "\<lambda>i. if i = n then 1 else if i = n - 1 then - 1 else 0"
chaieb@29687
  1067
  let ?h = "\<lambda>i. if i=n - 1 then - 1 else 0"
huffman@30488
  1068
  have th1: "setsum ?f {0..n} = setsum ?g {0..n}"
haftmann@57418
  1069
    by (rule setsum.cong) auto
huffman@30488
  1070
  have th2: "setsum ?g {0..n - 1} = setsum ?h {0..n - 1}"
haftmann@57418
  1071
    apply (rule setsum.cong)
lp15@60162
  1072
    using assms
wenzelm@54681
  1073
    apply auto
wenzelm@54681
  1074
    done
wenzelm@54681
  1075
  have eq: "{0 .. n} = {0.. n - 1} \<union> {n}"
wenzelm@54681
  1076
    by auto
lp15@60162
  1077
  from assms have d: "{0.. n - 1} \<inter> {n} = {}"
wenzelm@54681
  1078
    by auto
wenzelm@54681
  1079
  have f: "finite {0.. n - 1}" "finite {n}"
wenzelm@54681
  1080
    by auto
lp15@60162
  1081
  show ?thesis
huffman@30488
  1082
    unfolding th1
haftmann@57418
  1083
    apply (simp add: setsum.union_disjoint[OF f d, unfolded eq[symmetric]] del: One_nat_def)
chaieb@29687
  1084
    unfolding th2
haftmann@57418
  1085
    apply (simp add: setsum.delta)
wenzelm@52891
  1086
    done
chaieb@29687
  1087
qed
chaieb@29687
  1088
eberlm@61608
  1089
lemma fps_inverse_mult: "inverse (f * g :: 'a::field fps) = inverse f * inverse g"
eberlm@61608
  1090
proof (cases "f$0 = 0 \<or> g$0 = 0")
eberlm@61608
  1091
  assume "\<not>(f$0 = 0 \<or> g$0 = 0)"
eberlm@61608
  1092
  hence [simp]: "f$0 \<noteq> 0" "g$0 \<noteq> 0" by simp_all
eberlm@61608
  1093
  show ?thesis
eberlm@61608
  1094
  proof (rule fps_inverse_unique)
eberlm@61608
  1095
    have "f * g * (inverse f * inverse g) = (inverse f * f) * (inverse g * g)" by simp
eberlm@61608
  1096
    also have "... = 1" by (subst (1 2) inverse_mult_eq_1) simp_all
eberlm@61608
  1097
    finally show "f * g * (inverse f * inverse g) = 1" .
eberlm@61608
  1098
  qed
eberlm@61608
  1099
next
eberlm@61608
  1100
  assume A: "f$0 = 0 \<or> g$0 = 0"
eberlm@61608
  1101
  hence "inverse (f * g) = 0" by simp
eberlm@61608
  1102
  also from A have "... = inverse f * inverse g" by auto
eberlm@61608
  1103
  finally show "inverse (f * g) = inverse f * inverse g" .
eberlm@61608
  1104
qed
hoelzl@62102
  1105
eberlm@61608
  1106
wenzelm@60501
  1107
lemma fps_inverse_gp: "inverse (Abs_fps(\<lambda>n. (1::'a::field))) =
wenzelm@60501
  1108
    Abs_fps (\<lambda>n. if n= 0 then 1 else if n=1 then - 1 else 0)"
lp15@60162
  1109
  apply (rule fps_inverse_unique)
lp15@60162
  1110
  apply (simp_all add: fps_eq_iff fps_mult_nth setsum_zero_lemma)
lp15@60162
  1111
  done
lp15@60162
  1112
eberlm@61608
  1113
lemma subdegree_inverse [simp]: "subdegree (inverse (f::'a::field fps)) = 0"
eberlm@61608
  1114
proof (cases "f$0 = 0")
eberlm@61608
  1115
  assume nz: "f$0 \<noteq> 0"
eberlm@61608
  1116
  hence "subdegree (inverse f) + subdegree f = subdegree (inverse f * f)"
eberlm@61608
  1117
    by (subst subdegree_mult) auto
eberlm@61608
  1118
  also from nz have "subdegree f = 0" by (simp add: subdegree_eq_0_iff)
eberlm@61608
  1119
  also from nz have "inverse f * f = 1" by (rule inverse_mult_eq_1)
eberlm@61608
  1120
  finally show "subdegree (inverse f) = 0" by simp
eberlm@61608
  1121
qed (simp_all add: fps_inverse_def)
eberlm@61608
  1122
eberlm@61608
  1123
lemma fps_is_unit_iff [simp]: "(f :: 'a :: field fps) dvd 1 \<longleftrightarrow> f $ 0 \<noteq> 0"
eberlm@61608
  1124
proof
eberlm@61608
  1125
  assume "f dvd 1"
eberlm@61608
  1126
  then obtain g where "1 = f * g" by (elim dvdE)
eberlm@61608
  1127
  from this[symmetric] have "(f*g) $ 0 = 1" by simp
eberlm@61608
  1128
  thus "f $ 0 \<noteq> 0" by auto
eberlm@61608
  1129
next
eberlm@61608
  1130
  assume A: "f $ 0 \<noteq> 0"
eberlm@61608
  1131
  thus "f dvd 1" by (simp add: inverse_mult_eq_1[OF A, symmetric])
eberlm@61608
  1132
qed
eberlm@61608
  1133
eberlm@61608
  1134
lemma subdegree_eq_0' [simp]: "(f :: 'a :: field fps) dvd 1 \<Longrightarrow> subdegree f = 0"
eberlm@61608
  1135
  by simp
eberlm@61608
  1136
eberlm@61608
  1137
lemma fps_unit_dvd [simp]: "(f $ 0 :: 'a :: field) \<noteq> 0 \<Longrightarrow> f dvd g"
eberlm@61608
  1138
  by (rule dvd_trans, subst fps_is_unit_iff) simp_all
eberlm@61608
  1139
eberlm@61608
  1140
eberlm@61608
  1141
eberlm@61608
  1142
instantiation fps :: (field) ring_div
eberlm@61608
  1143
begin
eberlm@61608
  1144
eberlm@61608
  1145
definition fps_mod_def:
eberlm@61608
  1146
  "f mod g = (if g = 0 then f else
hoelzl@62102
  1147
     let n = subdegree g; h = fps_shift n g
eberlm@61608
  1148
     in  fps_cutoff n (f * inverse h) * h)"
eberlm@61608
  1149
hoelzl@62102
  1150
lemma fps_mod_eq_zero:
eberlm@61608
  1151
  assumes "g \<noteq> 0" and "subdegree f \<ge> subdegree g"
eberlm@61608
  1152
  shows   "f mod g = 0"
eberlm@61608
  1153
  using assms by (cases "f = 0") (auto simp: fps_cutoff_zero_iff fps_mod_def Let_def)
eberlm@61608
  1154
hoelzl@62102
  1155
lemma fps_times_divide_eq:
eberlm@61608
  1156
  assumes "g \<noteq> 0" and "subdegree f \<ge> subdegree (g :: 'a fps)"
eberlm@61608
  1157
  shows   "f div g * g = f"
eberlm@61608
  1158
proof (cases "f = 0")
eberlm@61608
  1159
  assume nz: "f \<noteq> 0"
eberlm@61608
  1160
  def n \<equiv> "subdegree g"
eberlm@61608
  1161
  def h \<equiv> "fps_shift n g"
eberlm@61608
  1162
  from assms have [simp]: "h $ 0 \<noteq> 0" unfolding h_def by (simp add: n_def)
hoelzl@62102
  1163
eberlm@61608
  1164
  from assms nz have "f div g * g = fps_shift n (f * inverse h) * g"
eberlm@61608
  1165
    by (simp add: fps_divide_def Let_def h_def n_def)
eberlm@61608
  1166
  also have "... = fps_shift n (f * inverse h) * X^n * h" unfolding h_def n_def
eberlm@61608
  1167
    by (subst subdegree_decompose[of g]) simp
eberlm@61608
  1168
  also have "fps_shift n (f * inverse h) * X^n = f * inverse h"
eberlm@61608
  1169
    by (rule fps_shift_times_X_power) (simp_all add: nz assms n_def)
eberlm@61608
  1170
  also have "... * h = f * (inverse h * h)" by simp
eberlm@61608
  1171
  also have "inverse h * h = 1" by (rule inverse_mult_eq_1) simp
eberlm@61608
  1172
  finally show ?thesis by simp
eberlm@61608
  1173
qed (simp_all add: fps_divide_def Let_def)
eberlm@61608
  1174
hoelzl@62102
  1175
lemma
eberlm@61608
  1176
  assumes "g$0 \<noteq> 0"
eberlm@61608
  1177
  shows   fps_divide_unit: "f div g = f * inverse g" and fps_mod_unit [simp]: "f mod g = 0"
eberlm@61608
  1178
proof -
eberlm@61608
  1179
  from assms have [simp]: "subdegree g = 0" by (simp add: subdegree_eq_0_iff)
hoelzl@62102
  1180
  from assms show "f div g = f * inverse g"
eberlm@61608
  1181
    by (auto simp: fps_divide_def Let_def subdegree_eq_0_iff)
eberlm@61608
  1182
  from assms show "f mod g = 0" by (intro fps_mod_eq_zero) auto
eberlm@61608
  1183
qed
eberlm@61608
  1184
eberlm@61608
  1185
context
eberlm@61608
  1186
begin
eberlm@61608
  1187
private lemma fps_divide_cancel_aux1:
eberlm@61608
  1188
  assumes "h$0 \<noteq> (0 :: 'a :: field)"
eberlm@61608
  1189
  shows   "(h * f) div (h * g) = f div g"
eberlm@61608
  1190
proof (cases "g = 0")
eberlm@61608
  1191
  assume "g \<noteq> 0"
eberlm@61608
  1192
  from assms have "h \<noteq> 0" by auto
eberlm@61608
  1193
  note nz [simp] = \<open>g \<noteq> 0\<close> \<open>h \<noteq> 0\<close>
eberlm@61608
  1194
  from assms have [simp]: "subdegree h = 0" by (simp add: subdegree_eq_0_iff)
hoelzl@62102
  1195
hoelzl@62102
  1196
  have "(h * f) div (h * g) =
eberlm@61608
  1197
          fps_shift (subdegree g) (h * f * inverse (fps_shift (subdegree g) (h*g)))"
eberlm@61608
  1198
    by (simp add: fps_divide_def Let_def)
hoelzl@62102
  1199
  also have "h * f * inverse (fps_shift (subdegree g) (h*g)) =
eberlm@61608
  1200
               (inverse h * h) * f * inverse (fps_shift (subdegree g) g)"
eberlm@61608
  1201
    by (subst fps_shift_mult) (simp_all add: algebra_simps fps_inverse_mult)
eberlm@61608
  1202
  also from assms have "inverse h * h = 1" by (rule inverse_mult_eq_1)
eberlm@61608
  1203
  finally show "(h * f) div (h * g) = f div g" by (simp_all add: fps_divide_def Let_def)
eberlm@61608
  1204
qed (simp_all add: fps_divide_def)
eberlm@61608
  1205
eberlm@61608
  1206
private lemma fps_divide_cancel_aux2:
eberlm@61608
  1207
  "(f * X^m) div (g * X^m) = f div (g :: 'a :: field fps)"
eberlm@61608
  1208
proof (cases "g = 0")
eberlm@61608
  1209
  assume [simp]: "g \<noteq> 0"
hoelzl@62102
  1210
  have "(f * X^m) div (g * X^m) =
eberlm@61608
  1211
          fps_shift (subdegree g + m) (f*inverse (fps_shift (subdegree g + m) (g*X^m))*X^m)"
eberlm@61608
  1212
    by (simp add: fps_divide_def Let_def algebra_simps)
eberlm@61608
  1213
  also have "... = f div g"
eberlm@61608
  1214
    by (simp add: fps_shift_times_X_power'' fps_divide_def Let_def)
eberlm@61608
  1215
  finally show ?thesis .
eberlm@61608
  1216
qed (simp_all add: fps_divide_def)
eberlm@61608
  1217
eberlm@61608
  1218
instance proof
eberlm@61608
  1219
  fix f g :: "'a fps"
eberlm@61608
  1220
  def n \<equiv> "subdegree g"
eberlm@61608
  1221
  def h \<equiv> "fps_shift n g"
hoelzl@62102
  1222
eberlm@61608
  1223
  show "f div g * g + f mod g = f"
eberlm@61608
  1224
  proof (cases "g = 0 \<or> f = 0")
eberlm@61608
  1225
    assume "\<not>(g = 0 \<or> f = 0)"
eberlm@61608
  1226
    hence nz [simp]: "f \<noteq> 0" "g \<noteq> 0" by simp_all
eberlm@61608
  1227
    show ?thesis
eberlm@61608
  1228
    proof (rule disjE[OF le_less_linear])
eberlm@61608
  1229
      assume "subdegree f \<ge> subdegree g"
eberlm@61608
  1230
      with nz show ?thesis by (simp add: fps_mod_eq_zero fps_times_divide_eq)
eberlm@61608
  1231
    next
eberlm@61608
  1232
      assume "subdegree f < subdegree g"
eberlm@61608
  1233
      have g_decomp: "g = h * X^n" unfolding h_def n_def by (rule subdegree_decompose)
hoelzl@62102
  1234
      have "f div g * g + f mod g =
hoelzl@62102
  1235
              fps_shift n (f * inverse h) * g + fps_cutoff n (f * inverse h) * h"
eberlm@61608
  1236
        by (simp add: fps_mod_def fps_divide_def Let_def n_def h_def)
eberlm@61608
  1237
      also have "... = h * (fps_shift n (f * inverse h) * X^n + fps_cutoff n (f * inverse h))"
eberlm@61608
  1238
        by (subst g_decomp) (simp add: algebra_simps)
eberlm@61608
  1239
      also have "... = f * (inverse h * h)"
eberlm@61608
  1240
        by (subst fps_shift_cutoff) simp
eberlm@61608
  1241
      also have "inverse h * h = 1" by (rule inverse_mult_eq_1) (simp add: h_def n_def)
eberlm@61608
  1242
      finally show ?thesis by simp
eberlm@61608
  1243
    qed
eberlm@61608
  1244
  qed (auto simp: fps_mod_def fps_divide_def Let_def)
eberlm@61608
  1245
next
eberlm@61608
  1246
eberlm@61608
  1247
  fix f g h :: "'a fps"
eberlm@61608
  1248
  assume "h \<noteq> 0"
eberlm@61608
  1249
  show "(h * f) div (h * g) = f div g"
eberlm@61608
  1250
  proof -
eberlm@61608
  1251
    def m \<equiv> "subdegree h"
eberlm@61608
  1252
    def h' \<equiv> "fps_shift m h"
eberlm@61608
  1253
    have h_decomp: "h = h' * X ^ m" unfolding h'_def m_def by (rule subdegree_decompose)
eberlm@61608
  1254
    from \<open>h \<noteq> 0\<close> have [simp]: "h'$0 \<noteq> 0" by (simp add: h'_def m_def)
eberlm@61608
  1255
    have "(h * f) div (h * g) = (h' * f * X^m) div (h' * g * X^m)"
eberlm@61608
  1256
      by (simp add: h_decomp algebra_simps)
eberlm@61608
  1257
    also have "... = f div g" by (simp add: fps_divide_cancel_aux1 fps_divide_cancel_aux2)
eberlm@61608
  1258
    finally show ?thesis .
eberlm@61608
  1259
  qed
eberlm@61608
  1260
eberlm@61608
  1261
next
eberlm@61608
  1262
  fix f g h :: "'a fps"
eberlm@61608
  1263
  assume [simp]: "h \<noteq> 0"
eberlm@61608
  1264
  def n \<equiv> "subdegree h"
eberlm@61608
  1265
  def h' \<equiv> "fps_shift n h"
eberlm@61608
  1266
  note dfs = n_def h'_def
eberlm@61608
  1267
  have "(f + g * h) div h = fps_shift n (f * inverse h') + fps_shift n (g * (h * inverse h'))"
eberlm@61608
  1268
    by (simp add: fps_divide_def Let_def dfs[symmetric] algebra_simps fps_shift_add)
eberlm@61608
  1269
  also have "h * inverse h' = (inverse h' * h') * X^n"
eberlm@61608
  1270
    by (subst subdegree_decompose) (simp_all add: dfs)
eberlm@61608
  1271
  also have "... = X^n" by (subst inverse_mult_eq_1) (simp_all add: dfs)
eberlm@61608
  1272
  also have "fps_shift n (g * X^n) = g" by simp
hoelzl@62102
  1273
  also have "fps_shift n (f * inverse h') = f div h"
eberlm@61608
  1274
    by (simp add: fps_divide_def Let_def dfs)
eberlm@61608
  1275
  finally show "(f + g * h) div h = g + f div h" by simp
eberlm@61608
  1276
qed (auto simp: fps_divide_def fps_mod_def Let_def)
eberlm@61608
  1277
eberlm@61608
  1278
end
eberlm@61608
  1279
end
eberlm@61608
  1280
eberlm@61608
  1281
lemma subdegree_mod:
eberlm@61608
  1282
  assumes "f \<noteq> 0" "subdegree f < subdegree g"
eberlm@61608
  1283
  shows   "subdegree (f mod g) = subdegree f"
eberlm@61608
  1284
proof (cases "f div g * g = 0")
eberlm@61608
  1285
  assume "f div g * g \<noteq> 0"
eberlm@61608
  1286
  hence [simp]: "f div g \<noteq> 0" "g \<noteq> 0" by auto
eberlm@61608
  1287
  from mod_div_equality[of f g] have "f mod g = f - f div g * g" by (simp add: algebra_simps)
eberlm@61608
  1288
  also from assms have "subdegree ... = subdegree f"
eberlm@61608
  1289
    by (intro subdegree_diff_eq1) simp_all
eberlm@61608
  1290
  finally show ?thesis .
eberlm@61608
  1291
next
eberlm@61608
  1292
  assume zero: "f div g * g = 0"
eberlm@61608
  1293
  from mod_div_equality[of f g] have "f mod g = f - f div g * g" by (simp add: algebra_simps)
eberlm@61608
  1294
  also note zero
eberlm@61608
  1295
  finally show ?thesis by simp
eberlm@61608
  1296
qed
eberlm@61608
  1297
eberlm@61608
  1298
lemma fps_divide_nth_0 [simp]: "g $ 0 \<noteq> 0 \<Longrightarrow> (f div g) $ 0 = f $ 0 / (g $ 0 :: _ :: field)"
eberlm@61608
  1299
  by (simp add: fps_divide_unit divide_inverse)
eberlm@61608
  1300
eberlm@61608
  1301
hoelzl@62102
  1302
lemma dvd_imp_subdegree_le:
eberlm@61608
  1303
  "(f :: 'a :: idom fps) dvd g \<Longrightarrow> g \<noteq> 0 \<Longrightarrow> subdegree f \<le> subdegree g"
eberlm@61608
  1304
  by (auto elim: dvdE)
eberlm@61608
  1305
hoelzl@62102
  1306
lemma fps_dvd_iff:
eberlm@61608
  1307
  assumes "(f :: 'a :: field fps) \<noteq> 0" "g \<noteq> 0"
eberlm@61608
  1308
  shows   "f dvd g \<longleftrightarrow> subdegree f \<le> subdegree g"
eberlm@61608
  1309
proof
eberlm@61608
  1310
  assume "subdegree f \<le> subdegree g"
hoelzl@62102
  1311
  with assms have "g mod f = 0"
eberlm@61608
  1312
    by (simp add: fps_mod_def Let_def fps_cutoff_zero_iff)
eberlm@61608
  1313
  thus "f dvd g" by (simp add: dvd_eq_mod_eq_0)
eberlm@61608
  1314
qed (simp add: assms dvd_imp_subdegree_le)
eberlm@61608
  1315
eberlm@61608
  1316
lemma fps_const_inverse: "inverse (fps_const (a::'a::field)) = fps_const (inverse a)"
eberlm@61608
  1317
  by (cases "a \<noteq> 0", rule fps_inverse_unique) (auto simp: fps_eq_iff)
eberlm@61608
  1318
eberlm@61608
  1319
lemma fps_const_divide: "fps_const (x :: _ :: field) / fps_const y = fps_const (x / y)"
eberlm@61608
  1320
  by (cases "y = 0") (simp_all add: fps_divide_unit fps_const_inverse divide_inverse)
eberlm@61608
  1321
hoelzl@62102
  1322
lemma inverse_fps_numeral:
eberlm@61608
  1323
  "inverse (numeral n :: ('a :: field_char_0) fps) = fps_const (inverse (numeral n))"
eberlm@61608
  1324
  by (intro fps_inverse_unique fps_ext) (simp_all add: fps_numeral_nth)
eberlm@61608
  1325
eberlm@61608
  1326
eberlm@61608
  1327
eberlm@61608
  1328
eberlm@61608
  1329
instantiation fps :: (field) normalization_semidom
eberlm@61608
  1330
begin
eberlm@61608
  1331
hoelzl@62102
  1332
definition fps_unit_factor_def [simp]:
eberlm@61608
  1333
  "unit_factor f = fps_shift (subdegree f) f"
eberlm@61608
  1334
eberlm@61608
  1335
definition fps_normalize_def [simp]:
eberlm@61608
  1336
  "normalize f = (if f = 0 then 0 else X ^ subdegree f)"
eberlm@61608
  1337
eberlm@61608
  1338
instance proof
eberlm@61608
  1339
  fix f :: "'a fps"
eberlm@61608
  1340
  show "unit_factor f * normalize f = f"
eberlm@61608
  1341
    by (simp add: fps_shift_times_X_power)
eberlm@61608
  1342
next
eberlm@61608
  1343
  fix f g :: "'a fps"
eberlm@61608
  1344
  show "unit_factor (f * g) = unit_factor f * unit_factor g"
eberlm@61608
  1345
  proof (cases "f = 0 \<or> g = 0")
eberlm@61608
  1346
    assume "\<not>(f = 0 \<or> g = 0)"
eberlm@61608
  1347
    thus "unit_factor (f * g) = unit_factor f * unit_factor g"
eberlm@61608
  1348
    unfolding fps_unit_factor_def
eberlm@61608
  1349
      by (auto simp: fps_shift_fps_shift fps_shift_mult fps_shift_mult_right)
eberlm@61608
  1350
  qed auto
eberlm@61608
  1351
qed auto
eberlm@61608
  1352
eberlm@61608
  1353
end
eberlm@61608
  1354
eberlm@61608
  1355
instance fps :: (field) algebraic_semidom ..
eberlm@61608
  1356
eberlm@61608
  1357
eberlm@61608
  1358
subsection \<open>Formal power series form a Euclidean ring\<close>
eberlm@61608
  1359
eberlm@61608
  1360
instantiation fps :: (field) euclidean_ring
eberlm@61608
  1361
begin
eberlm@61608
  1362
hoelzl@62102
  1363
definition fps_euclidean_size_def:
eberlm@61608
  1364
  "euclidean_size f = (if f = 0 then 0 else Suc (subdegree f))"
eberlm@61608
  1365
eberlm@61608
  1366
instance proof
eberlm@61608
  1367
  fix f g :: "'a fps" assume [simp]: "g \<noteq> 0"
eberlm@61608
  1368
  show "euclidean_size f \<le> euclidean_size (f * g)"
eberlm@61608
  1369
    by (cases "f = 0") (auto simp: fps_euclidean_size_def)
eberlm@61608
  1370
  show "euclidean_size (f mod g) < euclidean_size g"
eberlm@61608
  1371
    apply (cases "f = 0", simp add: fps_euclidean_size_def)
eberlm@61608
  1372
    apply (rule disjE[OF le_less_linear[of "subdegree g" "subdegree f"]])
eberlm@61608
  1373
    apply (simp_all add: fps_mod_eq_zero fps_euclidean_size_def subdegree_mod)
eberlm@61608
  1374
    done
eberlm@61608
  1375
qed
eberlm@61608
  1376
eberlm@61608
  1377
end
eberlm@61608
  1378
eberlm@61608
  1379
instantiation fps :: (field) euclidean_ring_gcd
eberlm@61608
  1380
begin
eberlm@61608
  1381
definition fps_gcd_def: "(gcd :: 'a fps \<Rightarrow> _) = gcd_eucl"
eberlm@61608
  1382
definition fps_lcm_def: "(lcm :: 'a fps \<Rightarrow> _) = lcm_eucl"
eberlm@61608
  1383
definition fps_Gcd_def: "(Gcd :: 'a fps set \<Rightarrow> _) = Gcd_eucl"
eberlm@61608
  1384
definition fps_Lcm_def: "(Lcm :: 'a fps set \<Rightarrow> _) = Lcm_eucl"
eberlm@61608
  1385
instance by intro_classes (simp_all add: fps_gcd_def fps_lcm_def fps_Gcd_def fps_Lcm_def)
eberlm@61608
  1386
end
eberlm@61608
  1387
eberlm@61608
  1388
lemma fps_gcd:
eberlm@61608
  1389
  assumes [simp]: "f \<noteq> 0" "g \<noteq> 0"
eberlm@61608
  1390
  shows   "gcd f g = X ^ min (subdegree f) (subdegree g)"
eberlm@61608
  1391
proof -
eberlm@61608
  1392
  let ?m = "min (subdegree f) (subdegree g)"
eberlm@61608
  1393
  show "gcd f g = X ^ ?m"
eberlm@61608
  1394
  proof (rule sym, rule gcdI)
eberlm@61608
  1395
    fix d assume "d dvd f" "d dvd g"
eberlm@61608
  1396
    thus "d dvd X ^ ?m" by (cases "d = 0") (auto simp: fps_dvd_iff)
eberlm@61608
  1397
  qed (simp_all add: fps_dvd_iff)
eberlm@61608
  1398
qed
eberlm@61608
  1399
hoelzl@62102
  1400
lemma fps_gcd_altdef: "gcd (f :: 'a :: field fps) g =
eberlm@61608
  1401
  (if f = 0 \<and> g = 0 then 0 else
hoelzl@62102
  1402
   if f = 0 then X ^ subdegree g else
hoelzl@62102
  1403
   if g = 0 then X ^ subdegree f else
eberlm@61608
  1404
     X ^ min (subdegree f) (subdegree g))"
eberlm@61608
  1405
  by (simp add: fps_gcd)
eberlm@61608
  1406
eberlm@61608
  1407
lemma fps_lcm:
eberlm@61608
  1408
  assumes [simp]: "f \<noteq> 0" "g \<noteq> 0"
eberlm@61608
  1409
  shows   "lcm f g = X ^ max (subdegree f) (subdegree g)"
eberlm@61608
  1410
proof -
eberlm@61608
  1411
  let ?m = "max (subdegree f) (subdegree g)"
eberlm@61608
  1412
  show "lcm f g = X ^ ?m"
eberlm@61608
  1413
  proof (rule sym, rule lcmI)
eberlm@61608
  1414
    fix d assume "f dvd d" "g dvd d"
eberlm@61608
  1415
    thus "X ^ ?m dvd d" by (cases "d = 0") (auto simp: fps_dvd_iff)
eberlm@61608
  1416
  qed (simp_all add: fps_dvd_iff)
eberlm@61608
  1417
qed
eberlm@61608
  1418
hoelzl@62102
  1419
lemma fps_lcm_altdef: "lcm (f :: 'a :: field fps) g =
eberlm@61608
  1420
  (if f = 0 \<or> g = 0 then 0 else X ^ max (subdegree f) (subdegree g))"
eberlm@61608
  1421
  by (simp add: fps_lcm)
eberlm@61608
  1422
eberlm@61608
  1423
lemma fps_Gcd:
eberlm@61608
  1424
  assumes "A - {0} \<noteq> {}"
eberlm@61608
  1425
  shows   "Gcd A = X ^ (INF f:A-{0}. subdegree f)"
eberlm@61608
  1426
proof (rule sym, rule GcdI)
eberlm@61608
  1427
  fix f assume "f \<in> A"
eberlm@61608
  1428
  thus "X ^ (INF f:A - {0}. subdegree f) dvd f"
eberlm@61608
  1429
    by (cases "f = 0") (auto simp: fps_dvd_iff intro!: cINF_lower)
eberlm@61608
  1430
next
eberlm@61608
  1431
  fix d assume d: "\<And>f. f \<in> A \<Longrightarrow> d dvd f"
eberlm@61608
  1432
  from assms obtain f where "f \<in> A - {0}" by auto
eberlm@61608
  1433
  with d[of f] have [simp]: "d \<noteq> 0" by auto
eberlm@61608
  1434
  from d assms have "subdegree d \<le> (INF f:A-{0}. subdegree f)"
eberlm@61608
  1435
    by (intro cINF_greatest) (auto simp: fps_dvd_iff[symmetric])
eberlm@61608
  1436
  with d assms show "d dvd X ^ (INF f:A-{0}. subdegree f)" by (simp add: fps_dvd_iff)
eberlm@61608
  1437
qed simp_all
eberlm@61608
  1438
hoelzl@62102
  1439
lemma fps_Gcd_altdef: "Gcd (A :: 'a :: field fps set) =
eberlm@61608
  1440
  (if A \<subseteq> {0} then 0 else X ^ (INF f:A-{0}. subdegree f))"
eberlm@61608
  1441
  using fps_Gcd by auto
eberlm@61608
  1442
eberlm@61608
  1443
lemma fps_Lcm:
eberlm@61608
  1444
  assumes "A \<noteq> {}" "0 \<notin> A" "bdd_above (subdegree`A)"
eberlm@61608
  1445
  shows   "Lcm A = X ^ (SUP f:A. subdegree f)"
eberlm@61608
  1446
proof (rule sym, rule LcmI)
eberlm@61608
  1447
  fix f assume "f \<in> A"
eberlm@61608
  1448
  moreover from assms(3) have "bdd_above (subdegree ` A)" by auto
eberlm@61608
  1449
  ultimately show "f dvd X ^ (SUP f:A. subdegree f)" using assms(2)
eberlm@61608
  1450
    by (cases "f = 0") (auto simp: fps_dvd_iff intro!: cSUP_upper)
eberlm@61608
  1451
next
eberlm@61608
  1452
  fix d assume d: "\<And>f. f \<in> A \<Longrightarrow> f dvd d"
eberlm@61608
  1453
  from assms obtain f where f: "f \<in> A" "f \<noteq> 0" by auto
eberlm@61608
  1454
  show "X ^ (SUP f:A. subdegree f) dvd d"
eberlm@61608
  1455
  proof (cases "d = 0")
eberlm@61608
  1456
    assume "d \<noteq> 0"
eberlm@61608
  1457
    moreover from d have "\<And>f. f \<in> A \<Longrightarrow> f \<noteq> 0 \<Longrightarrow> f dvd d" by blast
eberlm@61608
  1458
    ultimately have "subdegree d \<ge> (SUP f:A. subdegree f)" using assms
eberlm@61608
  1459
      by (intro cSUP_least) (auto simp: fps_dvd_iff)
eberlm@61608
  1460
    with \<open>d \<noteq> 0\<close> show ?thesis by (simp add: fps_dvd_iff)
eberlm@61608
  1461
  qed simp_all
eberlm@61608
  1462
qed simp_all
eberlm@61608
  1463
eberlm@61608
  1464
lemma fps_Lcm_altdef:
hoelzl@62102
  1465
  "Lcm (A :: 'a :: field fps set) =
eberlm@61608
  1466
     (if 0 \<in> A \<or> \<not>bdd_above (subdegree`A) then 0 else
eberlm@61608
  1467
      if A = {} then 1 else X ^ (SUP f:A. subdegree f))"
eberlm@61608
  1468
proof (cases "bdd_above (subdegree`A)")
eberlm@61608
  1469
  assume unbounded: "\<not>bdd_above (subdegree`A)"
eberlm@61608
  1470
  have "Lcm A = 0"
eberlm@61608
  1471
  proof (rule ccontr)
eberlm@61608
  1472
    assume "Lcm A \<noteq> 0"
eberlm@61608
  1473
    from unbounded obtain f where f: "f \<in> A" "subdegree (Lcm A) < subdegree f"
eberlm@61608
  1474
      unfolding bdd_above_def by (auto simp: not_le)
wenzelm@61799
  1475
    moreover from this and \<open>Lcm A \<noteq> 0\<close> have "subdegree f \<le> subdegree (Lcm A)"
eberlm@61608
  1476
      by (intro dvd_imp_subdegree_le) simp_all
eberlm@61608
  1477
    ultimately show False by simp
eberlm@61608
  1478
  qed
eberlm@61608
  1479
  with unbounded show ?thesis by simp
eberlm@61608
  1480
qed (simp_all add: fps_Lcm)
eberlm@61608
  1481
wenzelm@54681
  1482
wenzelm@60500
  1483
subsection \<open>Formal Derivatives, and the MacLaurin theorem around 0\<close>
chaieb@29687
  1484
chaieb@29687
  1485
definition "fps_deriv f = Abs_fps (\<lambda>n. of_nat (n + 1) * f $ (n + 1))"
chaieb@29687
  1486
wenzelm@54681
  1487
lemma fps_deriv_nth[simp]: "fps_deriv f $ n = of_nat (n +1) * f $ (n + 1)"
wenzelm@48757
  1488
  by (simp add: fps_deriv_def)
wenzelm@48757
  1489
wenzelm@48757
  1490
lemma fps_deriv_linear[simp]:
wenzelm@48757
  1491
  "fps_deriv (fps_const (a::'a::comm_semiring_1) * f + fps_const b * g) =
wenzelm@48757
  1492
    fps_const a * fps_deriv f + fps_const b * fps_deriv g"
haftmann@36350
  1493
  unfolding fps_eq_iff fps_add_nth  fps_const_mult_left fps_deriv_nth by (simp add: field_simps)
chaieb@29687
  1494
huffman@30488
  1495
lemma fps_deriv_mult[simp]:
wenzelm@54681
  1496
  fixes f :: "'a::comm_ring_1 fps"
chaieb@29687
  1497
  shows "fps_deriv (f * g) = f * fps_deriv g + fps_deriv f * g"
wenzelm@52891
  1498
proof -
chaieb@29687
  1499
  let ?D = "fps_deriv"
wenzelm@60558
  1500
  have "(f * ?D g + ?D f * g) $ n = ?D (f*g) $ n" for n
wenzelm@60558
  1501
  proof -
chaieb@29687
  1502
    let ?Zn = "{0 ..n}"
chaieb@29687
  1503
    let ?Zn1 = "{0 .. n + 1}"
chaieb@29687
  1504
    let ?g = "\<lambda>i. of_nat (i+1) * g $ (i+1) * f $ (n - i) +
chaieb@29687
  1505
        of_nat (i+1)* f $ (i+1) * g $ (n - i)"
chaieb@29687
  1506
    let ?h = "\<lambda>i. of_nat i * g $ i * f $ ((n+1) - i) +
chaieb@29687
  1507
        of_nat i* f $ i * g $ ((n + 1) - i)"
wenzelm@52891
  1508
    have s0: "setsum (\<lambda>i. of_nat i * f $ i * g $ (n + 1 - i)) ?Zn1 =
wenzelm@52891
  1509
      setsum (\<lambda>i. of_nat (n + 1 - i) * f $ (n + 1 - i) * g $ i) ?Zn1"
hoelzl@57129
  1510
       by (rule setsum.reindex_bij_witness[where i="op - (n + 1)" and j="op - (n + 1)"]) auto
wenzelm@52891
  1511
    have s1: "setsum (\<lambda>i. f $ i * g $ (n + 1 - i)) ?Zn1 =
wenzelm@52891
  1512
      setsum (\<lambda>i. f $ (n + 1 - i) * g $ i) ?Zn1"
hoelzl@57129
  1513
       by (rule setsum.reindex_bij_witness[where i="op - (n + 1)" and j="op - (n + 1)"]) auto
wenzelm@52891
  1514
    have "(f * ?D g + ?D f * g)$n = (?D g * f + ?D f * g)$n"
haftmann@57512
  1515
      by (simp only: mult.commute)
chaieb@29687
  1516
    also have "\<dots> = (\<Sum>i = 0..n. ?g i)"
haftmann@57418
  1517
      by (simp add: fps_mult_nth setsum.distrib[symmetric])
chaieb@29687
  1518
    also have "\<dots> = setsum ?h {0..n+1}"
hoelzl@57129
  1519
      by (rule setsum.reindex_bij_witness_not_neutral
hoelzl@57129
  1520
            [where S'="{}" and T'="{0}" and j="Suc" and i="\<lambda>i. i - 1"]) auto
chaieb@29687
  1521
    also have "\<dots> = (fps_deriv (f * g)) $ n"
haftmann@57418
  1522
      apply (simp only: fps_deriv_nth fps_mult_nth setsum.distrib)
chaieb@29687
  1523
      unfolding s0 s1
haftmann@57418
  1524
      unfolding setsum.distrib[symmetric] setsum_right_distrib
haftmann@57418
  1525
      apply (rule setsum.cong)
wenzelm@52891
  1526
      apply (auto simp add: of_nat_diff field_simps)
wenzelm@52891
  1527
      done
wenzelm@60558
  1528
    finally show ?thesis .
wenzelm@60558
  1529
  qed
wenzelm@60558
  1530
  then show ?thesis
wenzelm@60558
  1531
    unfolding fps_eq_iff by auto
chaieb@29687
  1532
qed
chaieb@29687
  1533
chaieb@31968
  1534
lemma fps_deriv_X[simp]: "fps_deriv X = 1"
chaieb@31968
  1535
  by (simp add: fps_deriv_def X_def fps_eq_iff)
chaieb@31968
  1536
wenzelm@54681
  1537
lemma fps_deriv_neg[simp]:
wenzelm@54681
  1538
  "fps_deriv (- (f:: 'a::comm_ring_1 fps)) = - (fps_deriv f)"
huffman@29911
  1539
  by (simp add: fps_eq_iff fps_deriv_def)
wenzelm@52891
  1540
wenzelm@54681
  1541
lemma fps_deriv_add[simp]:
wenzelm@54681
  1542
  "fps_deriv ((f:: 'a::comm_ring_1 fps) + g) = fps_deriv f + fps_deriv g"
chaieb@29687
  1543
  using fps_deriv_linear[of 1 f 1 g] by simp
chaieb@29687
  1544
wenzelm@54681
  1545
lemma fps_deriv_sub[simp]:
wenzelm@54681
  1546
  "fps_deriv ((f:: 'a::comm_ring_1 fps) - g) = fps_deriv f - fps_deriv g"
haftmann@54230
  1547
  using fps_deriv_add [of f "- g"] by simp
chaieb@29687
  1548
chaieb@29687
  1549
lemma fps_deriv_const[simp]: "fps_deriv (fps_const c) = 0"
huffman@29911
  1550
  by (simp add: fps_ext fps_deriv_def fps_const_def)
chaieb@29687
  1551
wenzelm@48757
  1552
lemma fps_deriv_mult_const_left[simp]:
wenzelm@54681
  1553
  "fps_deriv (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_deriv f"
chaieb@29687
  1554
  by simp
chaieb@29687
  1555
chaieb@29687
  1556
lemma fps_deriv_0[simp]: "fps_deriv 0 = 0"
chaieb@29687
  1557
  by (simp add: fps_deriv_def fps_eq_iff)
chaieb@29687
  1558
chaieb@29687
  1559
lemma fps_deriv_1[simp]: "fps_deriv 1 = 0"
chaieb@29687
  1560
  by (simp add: fps_deriv_def fps_eq_iff )
chaieb@29687
  1561
wenzelm@48757
  1562
lemma fps_deriv_mult_const_right[simp]:
wenzelm@54681
  1563
  "fps_deriv (f * fps_const (c::'a::comm_ring_1)) = fps_deriv f * fps_const c"
chaieb@29687
  1564
  by simp
chaieb@29687
  1565
wenzelm@48757
  1566
lemma fps_deriv_setsum:
wenzelm@54681
  1567
  "fps_deriv (setsum f S) = setsum (\<lambda>i. fps_deriv (f i :: 'a::comm_ring_1 fps)) S"
wenzelm@53195
  1568
proof (cases "finite S")
wenzelm@53195
  1569
  case False
wenzelm@53195
  1570
  then show ?thesis by simp
wenzelm@53195
  1571
next
wenzelm@53195
  1572
  case True
wenzelm@53195
  1573
  show ?thesis by (induct rule: finite_induct [OF True]) simp_all
chaieb@29687
  1574
qed
chaieb@29687
  1575
wenzelm@52902
  1576
lemma fps_deriv_eq_0_iff [simp]:
wenzelm@54681
  1577
  "fps_deriv f = 0 \<longleftrightarrow> f = fps_const (f$0 :: 'a::{idom,semiring_char_0})"
wenzelm@60501
  1578
  (is "?lhs \<longleftrightarrow> ?rhs")
wenzelm@60501
  1579
proof
wenzelm@60501
  1580
  show ?lhs if ?rhs
wenzelm@60501
  1581
  proof -
wenzelm@60501
  1582
    from that have "fps_deriv f = fps_deriv (fps_const (f$0))"
wenzelm@60501
  1583
      by simp
wenzelm@60501
  1584
    then show ?thesis
wenzelm@60501
  1585
      by simp
wenzelm@60501
  1586
  qed
wenzelm@60501
  1587
  show ?rhs if ?lhs
wenzelm@60501
  1588
  proof -
wenzelm@60501
  1589
    from that have "\<forall>n. (fps_deriv f)$n = 0"
wenzelm@60501
  1590
      by simp
wenzelm@60501
  1591
    then have "\<forall>n. f$(n+1) = 0"
wenzelm@60501
  1592
      by (simp del: of_nat_Suc of_nat_add One_nat_def)
wenzelm@60501
  1593
    then show ?thesis
chaieb@29687
  1594
      apply (clarsimp simp add: fps_eq_iff fps_const_def)
chaieb@29687
  1595
      apply (erule_tac x="n - 1" in allE)
wenzelm@52891
  1596
      apply simp
wenzelm@52891
  1597
      done
wenzelm@60501
  1598
  qed
chaieb@29687
  1599
qed
chaieb@29687
  1600
huffman@30488
  1601
lemma fps_deriv_eq_iff:
wenzelm@54681
  1602
  fixes f :: "'a::{idom,semiring_char_0} fps"
chaieb@29687
  1603
  shows "fps_deriv f = fps_deriv g \<longleftrightarrow> (f = fps_const(f$0 - g$0) + g)"
wenzelm@52891
  1604
proof -
wenzelm@52903
  1605
  have "fps_deriv f = fps_deriv g \<longleftrightarrow> fps_deriv (f - g) = 0"
wenzelm@52903
  1606
    by simp
wenzelm@54681
  1607
  also have "\<dots> \<longleftrightarrow> f - g = fps_const ((f - g) $ 0)"
wenzelm@52903
  1608
    unfolding fps_deriv_eq_0_iff ..
wenzelm@60501
  1609
  finally show ?thesis
wenzelm@60501
  1610
    by (simp add: field_simps)
chaieb@29687
  1611
qed
chaieb@29687
  1612
wenzelm@48757
  1613
lemma fps_deriv_eq_iff_ex:
wenzelm@54681
  1614
  "(fps_deriv f = fps_deriv g) \<longleftrightarrow> (\<exists>c::'a::{idom,semiring_char_0}. f = fps_const c + g)"
wenzelm@53195
  1615
  by (auto simp: fps_deriv_eq_iff)
wenzelm@48757
  1616
wenzelm@48757
  1617
wenzelm@54681
  1618
fun fps_nth_deriv :: "nat \<Rightarrow> 'a::semiring_1 fps \<Rightarrow> 'a fps"
wenzelm@48757
  1619
where
chaieb@29687
  1620
  "fps_nth_deriv 0 f = f"
chaieb@29687
  1621
| "fps_nth_deriv (Suc n) f = fps_nth_deriv n (fps_deriv f)"
chaieb@29687
  1622
chaieb@29687
  1623
lemma fps_nth_deriv_commute: "fps_nth_deriv (Suc n) f = fps_deriv (fps_nth_deriv n f)"
wenzelm@48757
  1624
  by (induct n arbitrary: f) auto
wenzelm@48757
  1625
wenzelm@48757
  1626
lemma fps_nth_deriv_linear[simp]:
wenzelm@48757
  1627
  "fps_nth_deriv n (fps_const (a::'a::comm_semiring_1) * f + fps_const b * g) =
wenzelm@48757
  1628
    fps_const a * fps_nth_deriv n f + fps_const b * fps_nth_deriv n g"
wenzelm@48757
  1629
  by (induct n arbitrary: f g) (auto simp add: fps_nth_deriv_commute)
wenzelm@48757
  1630
wenzelm@48757
  1631
lemma fps_nth_deriv_neg[simp]:
wenzelm@54681
  1632
  "fps_nth_deriv n (- (f :: 'a::comm_ring_1 fps)) = - (fps_nth_deriv n f)"
wenzelm@48757
  1633
  by (induct n arbitrary: f) simp_all
wenzelm@48757
  1634
wenzelm@48757
  1635
lemma fps_nth_deriv_add[simp]:
wenzelm@54681
  1636
  "fps_nth_deriv n ((f :: 'a::comm_ring_1 fps) + g) = fps_nth_deriv n f + fps_nth_deriv n g"
chaieb@29687
  1637
  using fps_nth_deriv_linear[of n 1 f 1 g] by simp
chaieb@29687
  1638
wenzelm@48757
  1639
lemma fps_nth_deriv_sub[simp]:
wenzelm@54681
  1640
  "fps_nth_deriv n ((f :: 'a::comm_ring_1 fps) - g) = fps_nth_deriv n f - fps_nth_deriv n g"
haftmann@54230
  1641
  using fps_nth_deriv_add [of n f "- g"] by simp
chaieb@29687
  1642
chaieb@29687
  1643
lemma fps_nth_deriv_0[simp]: "fps_nth_deriv n 0 = 0"
wenzelm@48757
  1644
  by (induct n) simp_all
chaieb@29687
  1645
chaieb@29687
  1646
lemma fps_nth_deriv_1[simp]: "fps_nth_deriv n 1 = (if n = 0 then 1 else 0)"
wenzelm@48757
  1647
  by (induct n) simp_all
wenzelm@48757
  1648
wenzelm@48757
  1649
lemma fps_nth_deriv_const[simp]:
wenzelm@48757
  1650
  "fps_nth_deriv n (fps_const c) = (if n = 0 then fps_const c else 0)"
wenzelm@48757
  1651
  by (cases n) simp_all
wenzelm@48757
  1652
wenzelm@48757
  1653
lemma fps_nth_deriv_mult_const_left[simp]:
wenzelm@48757
  1654
  "fps_nth_deriv n (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_nth_deriv n f"
chaieb@29687
  1655
  using fps_nth_deriv_linear[of n "c" f 0 0 ] by simp
chaieb@29687
  1656
wenzelm@48757
  1657
lemma fps_nth_deriv_mult_const_right[simp]:
wenzelm@48757
  1658
  "fps_nth_deriv n (f * fps_const (c::'a::comm_ring_1)) = fps_nth_deriv n f * fps_const c"
haftmann@57512
  1659
  using fps_nth_deriv_linear[of n "c" f 0 0] by (simp add: mult.commute)
chaieb@29687
  1660
wenzelm@48757
  1661
lemma fps_nth_deriv_setsum:
wenzelm@54681
  1662
  "fps_nth_deriv n (setsum f S) = setsum (\<lambda>i. fps_nth_deriv n (f i :: 'a::comm_ring_1 fps)) S"
wenzelm@52903
  1663
proof (cases "finite S")
wenzelm@52903
  1664
  case True
wenzelm@52903
  1665
  show ?thesis by (induct rule: finite_induct [OF True]) simp_all
wenzelm@52903
  1666
next
wenzelm@52903
  1667
  case False
wenzelm@52903
  1668
  then show ?thesis by simp
chaieb@29687
  1669
qed
chaieb@29687
  1670
wenzelm@48757
  1671
lemma fps_deriv_maclauren_0:
wenzelm@54681
  1672
  "(fps_nth_deriv k (f :: 'a::comm_semiring_1 fps)) $ 0 = of_nat (fact k) * f $ k"
haftmann@36350
  1673
  by (induct k arbitrary: f) (auto simp add: field_simps of_nat_mult)
chaieb@29687
  1674
wenzelm@54681
  1675
wenzelm@60500
  1676
subsection \<open>Powers\<close>
chaieb@29687
  1677
chaieb@29687
  1678
lemma fps_power_zeroth_eq_one: "a$0 =1 \<Longrightarrow> a^n $ 0 = (1::'a::semiring_1)"
wenzelm@48757
  1679
  by (induct n) (auto simp add: expand_fps_eq fps_mult_nth)
chaieb@29687
  1680
wenzelm@54681
  1681
lemma fps_power_first_eq: "(a :: 'a::comm_ring_1 fps) $ 0 =1 \<Longrightarrow> a^n $ 1 = of_nat n * a$1"
wenzelm@52891
  1682
proof (induct n)
wenzelm@52891
  1683
  case 0
wenzelm@52891
  1684
  then show ?case by simp
chaieb@29687
  1685
next
chaieb@29687
  1686
  case (Suc n)
huffman@30488
  1687
  show ?case unfolding power_Suc fps_mult_nth
wenzelm@60501
  1688
    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
  1689
    by (simp add: field_simps)
chaieb@29687
  1690
qed
chaieb@29687
  1691
chaieb@29687
  1692
lemma startsby_one_power:"a $ 0 = (1::'a::comm_ring_1) \<Longrightarrow> a^n $ 0 = 1"
wenzelm@48757
  1693
  by (induct n) (auto simp add: fps_mult_nth)
chaieb@29687
  1694
chaieb@29687
  1695
lemma startsby_zero_power:"a $0 = (0::'a::comm_ring_1) \<Longrightarrow> n > 0 \<Longrightarrow> a^n $0 = 0"
wenzelm@48757
  1696
  by (induct n) (auto simp add: fps_mult_nth)
chaieb@29687
  1697
wenzelm@54681
  1698
lemma startsby_power:"a $0 = (v::'a::comm_ring_1) \<Longrightarrow> a^n $0 = v^n"
wenzelm@52891
  1699
  by (induct n) (auto simp add: fps_mult_nth)
wenzelm@52891
  1700
wenzelm@54681
  1701
lemma startsby_zero_power_iff[simp]: "a^n $0 = (0::'a::idom) \<longleftrightarrow> n \<noteq> 0 \<and> a$0 = 0"
wenzelm@52891
  1702
  apply (rule iffI)
wenzelm@52891
  1703
  apply (induct n)
wenzelm@52891
  1704
  apply (auto simp add: fps_mult_nth)
wenzelm@52891
  1705
  apply (rule startsby_zero_power, simp_all)
wenzelm@52891
  1706
  done
chaieb@29687
  1707
huffman@30488
  1708
lemma startsby_zero_power_prefix:
wenzelm@60501
  1709
  assumes a0: "a $ 0 = (0::'a::idom)"
chaieb@29687
  1710
  shows "\<forall>n < k. a ^ k $ n = 0"
huffman@30488
  1711
  using a0
wenzelm@54681
  1712
proof (induct k rule: nat_less_induct)
wenzelm@52891
  1713
  fix k
wenzelm@54681
  1714
  assume H: "\<forall>m<k. a $0 =  0 \<longrightarrow> (\<forall>n<m. a ^ m $ n = 0)" and a0: "a $ 0 = 0"
wenzelm@60501
  1715
  show "\<forall>m<k. a ^ k $ m = 0"
wenzelm@60501
  1716
  proof (cases k)
wenzelm@60501
  1717
    case 0
wenzelm@60501
  1718
    then show ?thesis by simp
wenzelm@60501
  1719
  next
wenzelm@60501
  1720
    case (Suc l)
wenzelm@60501
  1721
    have "a^k $ m = 0" if mk: "m < k" for m
wenzelm@60501
  1722
    proof (cases "m = 0")
wenzelm@60501
  1723
      case True
wenzelm@60501
  1724
      then show ?thesis
wenzelm@60501
  1725
        using startsby_zero_power[of a k] Suc a0 by simp
wenzelm@60501
  1726
    next
wenzelm@60501
  1727
      case False
wenzelm@60501
  1728
      have "a ^k $ m = (a^l * a) $m"
wenzelm@60501
  1729
        by (simp add: Suc mult.commute)
wenzelm@60501
  1730
      also have "\<dots> = (\<Sum>i = 0..m. a ^ l $ i * a $ (m - i))"
wenzelm@60501
  1731
        by (simp add: fps_mult_nth)
wenzelm@60501
  1732
      also have "\<dots> = 0"
wenzelm@60501
  1733
        apply (rule setsum.neutral)
wenzelm@60501
  1734
        apply auto
wenzelm@60501
  1735
        apply (case_tac "x = m")
wenzelm@60501
  1736
        using a0 apply simp
wenzelm@60501
  1737
        apply (rule H[rule_format])
wenzelm@60501
  1738
        using a0 Suc mk apply auto
wenzelm@60501
  1739
        done
wenzelm@60501
  1740
      finally show ?thesis .
wenzelm@60501
  1741
    qed
wenzelm@60501
  1742
    then show ?thesis by blast
wenzelm@60501
  1743
  qed
chaieb@29687
  1744
qed
chaieb@29687
  1745
huffman@30488
  1746
lemma startsby_zero_setsum_depends:
wenzelm@54681
  1747
  assumes a0: "a $0 = (0::'a::idom)"
wenzelm@54681
  1748
    and kn: "n \<ge> k"
chaieb@29687
  1749
  shows "setsum (\<lambda>i. (a ^ i)$k) {0 .. n} = setsum (\<lambda>i. (a ^ i)$k) {0 .. k}"
haftmann@57418
  1750
  apply (rule setsum.mono_neutral_right)
wenzelm@54681
  1751
  using kn
wenzelm@54681
  1752
  apply auto
chaieb@29687
  1753
  apply (rule startsby_zero_power_prefix[rule_format, OF a0])
wenzelm@52891
  1754
  apply arith
wenzelm@52891
  1755
  done
wenzelm@52891
  1756
wenzelm@52891
  1757
lemma startsby_zero_power_nth_same:
wenzelm@54681
  1758
  assumes a0: "a$0 = (0::'a::idom)"
chaieb@29687
  1759
  shows "a^n $ n = (a$1) ^ n"
wenzelm@52891
  1760
proof (induct n)
wenzelm@52891
  1761
  case 0
wenzelm@52902
  1762
  then show ?case by simp
chaieb@29687
  1763
next
chaieb@29687
  1764
  case (Suc n)
wenzelm@54681
  1765
  have "a ^ Suc n $ (Suc n) = (a^n * a)$(Suc n)"
wenzelm@54681
  1766
    by (simp add: field_simps)
wenzelm@52891
  1767
  also have "\<dots> = setsum (\<lambda>i. a^n$i * a $ (Suc n - i)) {0.. Suc n}"
wenzelm@52891
  1768
    by (simp add: fps_mult_nth)
chaieb@29687
  1769
  also have "\<dots> = setsum (\<lambda>i. a^n$i * a $ (Suc n - i)) {n .. Suc n}"
haftmann@57418
  1770
    apply (rule setsum.mono_neutral_right)
chaieb@29687
  1771
    apply simp
chaieb@29687
  1772
    apply clarsimp
chaieb@29687
  1773
    apply clarsimp
chaieb@29687
  1774
    apply (rule startsby_zero_power_prefix[rule_format, OF a0])
chaieb@29687
  1775
    apply arith
chaieb@29687
  1776
    done
wenzelm@54681
  1777
  also have "\<dots> = a^n $ n * a$1"
wenzelm@54681
  1778
    using a0 by simp
wenzelm@54681
  1779
  finally show ?case
wenzelm@54681
  1780
    using Suc.hyps by simp
chaieb@29687
  1781
qed
chaieb@29687
  1782
chaieb@29687
  1783
lemma fps_inverse_power:
wenzelm@54681
  1784
  fixes a :: "'a::field fps"
chaieb@29687
  1785
  shows "inverse (a^n) = inverse a ^ n"
eberlm@61608
  1786
  by (induction n) (simp_all add: fps_inverse_mult)
chaieb@29687
  1787
wenzelm@48757
  1788
lemma fps_deriv_power:
wenzelm@54681
  1789
  "fps_deriv (a ^ n) = fps_const (of_nat n :: 'a::comm_ring_1) * fps_deriv a * a ^ (n - 1)"
wenzelm@48757
  1790
  apply (induct n)
wenzelm@52891
  1791
  apply (auto simp add: field_simps fps_const_add[symmetric] simp del: fps_const_add)
wenzelm@48757
  1792
  apply (case_tac n)
wenzelm@52891
  1793
  apply (auto simp add: field_simps)
wenzelm@48757
  1794
  done
chaieb@29687
  1795
huffman@30488
  1796
lemma fps_inverse_deriv:
wenzelm@54681
  1797
  fixes a :: "'a::field fps"
chaieb@29687
  1798
  assumes a0: "a$0 \<noteq> 0"
wenzelm@53077
  1799
  shows "fps_deriv (inverse a) = - fps_deriv a * (inverse a)\<^sup>2"
wenzelm@54681
  1800
proof -
chaieb@29687
  1801
  from inverse_mult_eq_1[OF a0]
chaieb@29687
  1802
  have "fps_deriv (inverse a * a) = 0" by simp
wenzelm@54452
  1803
  then have "inverse a * fps_deriv a + fps_deriv (inverse a) * a = 0"
wenzelm@54452
  1804
    by simp
wenzelm@54452
  1805
  then have "inverse a * (inverse a * fps_deriv a + fps_deriv (inverse a) * a) = 0"
wenzelm@54452
  1806
    by simp
chaieb@29687
  1807
  with inverse_mult_eq_1[OF a0]
wenzelm@53077
  1808
  have "(inverse a)\<^sup>2 * fps_deriv a + fps_deriv (inverse a) = 0"
chaieb@29687
  1809
    unfolding power2_eq_square
haftmann@36350
  1810
    apply (simp add: field_simps)
haftmann@57512
  1811
    apply (simp add: mult.assoc[symmetric])
wenzelm@52903
  1812
    done
wenzelm@53077
  1813
  then have "(inverse a)\<^sup>2 * fps_deriv a + fps_deriv (inverse a) - fps_deriv a * (inverse a)\<^sup>2 =
wenzelm@53077
  1814
      0 - fps_deriv a * (inverse a)\<^sup>2"
chaieb@29687
  1815
    by simp
wenzelm@53077
  1816
  then show "fps_deriv (inverse a) = - fps_deriv a * (inverse a)\<^sup>2"
wenzelm@52902
  1817
    by (simp add: field_simps)
chaieb@29687
  1818
qed
chaieb@29687
  1819
huffman@30488
  1820
lemma fps_inverse_deriv':
wenzelm@54681
  1821
  fixes a :: "'a::field fps"
wenzelm@60501
  1822
  assumes a0: "a $ 0 \<noteq> 0"
wenzelm@53077
  1823
  shows "fps_deriv (inverse a) = - fps_deriv a / a\<^sup>2"
eberlm@61608
  1824
  using fps_inverse_deriv[OF a0] a0
eberlm@61608
  1825
  by (simp add: fps_divide_unit power2_eq_square fps_inverse_mult)
chaieb@29687
  1826
wenzelm@52902
  1827
lemma inverse_mult_eq_1':
wenzelm@52902
  1828
  assumes f0: "f$0 \<noteq> (0::'a::field)"
wenzelm@60567
  1829
  shows "f * inverse f = 1"
haftmann@57512
  1830
  by (metis mult.commute inverse_mult_eq_1 f0)
chaieb@29687
  1831
eberlm@61804
  1832
(* FIXME: The last part of this proof should go through by simp once we have a proper
eberlm@61804
  1833
   theorem collection for simplifying division on rings *)
wenzelm@52902
  1834
lemma fps_divide_deriv:
eberlm@61804
  1835
  assumes "b dvd (a :: 'a :: field fps)"
eberlm@61804
  1836
  shows   "fps_deriv (a / b) = (fps_deriv a * b - a * fps_deriv b) / b^2"
eberlm@61804
  1837
proof -
eberlm@61804
  1838
  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
  1839
    by (drule sym) (simp add: mult.assoc)
eberlm@61804
  1840
  from assms have "a = a / b * b" by simp
eberlm@61804
  1841
  also have "fps_deriv (a / b * b) = fps_deriv (a / b) * b + a / b * fps_deriv b" by simp
eberlm@61804
  1842
  finally have "fps_deriv (a / b) * b^2 = fps_deriv a * b - a * fps_deriv b" using assms
eberlm@61804
  1843
    by (simp add: power2_eq_square algebra_simps)
eberlm@61804
  1844
  thus ?thesis by (cases "b = 0") (auto simp: eq_divide_imp)
eberlm@61804
  1845
qed
chaieb@29687
  1846
wenzelm@54681
  1847
lemma fps_inverse_gp': "inverse (Abs_fps (\<lambda>n. 1::'a::field)) = 1 - X"
huffman@29911
  1848
  by (simp add: fps_inverse_gp fps_eq_iff X_def)
chaieb@29687
  1849
chaieb@29687
  1850
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
  1851
  by (cases n) simp_all
chaieb@29687
  1852
wenzelm@60501
  1853
lemma fps_inverse_X_plus1: "inverse (1 + X) = Abs_fps (\<lambda>n. (- (1::'a::field)) ^ n)"
wenzelm@60501
  1854
  (is "_ = ?r")
wenzelm@54681
  1855
proof -
chaieb@29687
  1856
  have eq: "(1 + X) * ?r = 1"
chaieb@29687
  1857
    unfolding minus_one_power_iff
haftmann@36350
  1858
    by (auto simp add: field_simps fps_eq_iff)
wenzelm@54681
  1859
  show ?thesis
wenzelm@54681
  1860
    by (auto simp add: eq intro: fps_inverse_unique)
chaieb@29687
  1861
qed
chaieb@29687
  1862
huffman@30488
  1863
wenzelm@60501
  1864
subsection \<open>Integration\<close>
huffman@31273
  1865
wenzelm@52903
  1866
definition fps_integral :: "'a::field_char_0 fps \<Rightarrow> 'a \<Rightarrow> 'a fps"
wenzelm@52903
  1867
  where "fps_integral a a0 = Abs_fps (\<lambda>n. if n = 0 then a0 else (a$(n - 1) / of_nat n))"
chaieb@29687
  1868
huffman@31273
  1869
lemma fps_deriv_fps_integral: "fps_deriv (fps_integral a a0) = a"
huffman@31273
  1870
  unfolding fps_integral_def fps_deriv_def
huffman@31273
  1871
  by (simp add: fps_eq_iff del: of_nat_Suc)
chaieb@29687
  1872
huffman@31273
  1873
lemma fps_integral_linear:
huffman@31273
  1874
  "fps_integral (fps_const a * f + fps_const b * g) (a*a0 + b*b0) =
huffman@31273
  1875
    fps_const a * fps_integral f a0 + fps_const b * fps_integral g b0"
huffman@31273
  1876
  (is "?l = ?r")
wenzelm@53195
  1877
proof -
wenzelm@54681
  1878
  have "fps_deriv ?l = fps_deriv ?r"
wenzelm@54681
  1879
    by (simp add: fps_deriv_fps_integral)
wenzelm@54681
  1880
  moreover have "?l$0 = ?r$0"
wenzelm@54681
  1881
    by (simp add: fps_integral_def)
chaieb@29687
  1882
  ultimately show ?thesis
chaieb@29687
  1883
    unfolding fps_deriv_eq_iff by auto
chaieb@29687
  1884
qed
huffman@30488
  1885
wenzelm@53195
  1886
wenzelm@60500
  1887
subsection \<open>Composition of FPSs\<close>
wenzelm@53195
  1888
wenzelm@60501
  1889
definition fps_compose :: "'a::semiring_1 fps \<Rightarrow> 'a fps \<Rightarrow> 'a fps"  (infixl "oo" 55)
wenzelm@54681
  1890
  where "a oo b = Abs_fps (\<lambda>n. setsum (\<lambda>i. a$i * (b^i$n)) {0..n})"
chaieb@29687
  1891
wenzelm@48757
  1892
lemma fps_compose_nth: "(a oo b)$n = setsum (\<lambda>i. a$i * (b^i$n)) {0..n}"
wenzelm@48757
  1893
  by (simp add: fps_compose_def)
chaieb@29687
  1894
eberlm@61608
  1895
lemma fps_compose_nth_0 [simp]: "(f oo g) $ 0 = f $ 0"
eberlm@61608
  1896
  by (simp add: fps_compose_nth)
eberlm@61608
  1897
wenzelm@54681
  1898
lemma fps_compose_X[simp]: "a oo X = (a :: 'a::comm_ring_1 fps)"
haftmann@57418
  1899
  by (simp add: fps_ext fps_compose_def mult_delta_right setsum.delta')
huffman@30488
  1900
wenzelm@60501
  1901
lemma fps_const_compose[simp]: "fps_const (a::'a::comm_ring_1) oo b = fps_const a"
haftmann@57418
  1902
  by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum.delta)
chaieb@29687
  1903
wenzelm@54681
  1904
lemma numeral_compose[simp]: "(numeral k :: 'a::comm_ring_1 fps) oo b = numeral k"
huffman@47108
  1905
  unfolding numeral_fps_const by simp
huffman@47108
  1906
wenzelm@54681
  1907
lemma neg_numeral_compose[simp]: "(- numeral k :: 'a::comm_ring_1 fps) oo b = - numeral k"
huffman@47108
  1908
  unfolding neg_numeral_fps_const by simp
chaieb@31369
  1909
wenzelm@54681
  1910
lemma X_fps_compose_startby0[simp]: "a$0 = 0 \<Longrightarrow> X oo a = (a :: 'a::comm_ring_1 fps)"
haftmann@57418
  1911
  by (simp add: fps_eq_iff fps_compose_def mult_delta_left setsum.delta not_le)
chaieb@29687
  1912
chaieb@29687
  1913
wenzelm@60500
  1914
subsection \<open>Rules from Herbert Wilf's Generatingfunctionology\<close>
wenzelm@60500
  1915
wenzelm@60500
  1916
subsubsection \<open>Rule 1\<close>
chaieb@29687
  1917
  (* {a_{n+k}}_0^infty Corresponds to (f - setsum (\<lambda>i. a_i * x^i))/x^h, for h>0*)
chaieb@29687
  1918
huffman@30488
  1919
lemma fps_power_mult_eq_shift:
wenzelm@52902
  1920
  "X^Suc k * Abs_fps (\<lambda>n. a (n + Suc k)) =
wenzelm@54681
  1921
    Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a::comm_ring_1) * X^i) {0 .. k}"
wenzelm@52902
  1922
  (is "?lhs = ?rhs")
wenzelm@52902
  1923
proof -
wenzelm@60501
  1924
  have "?lhs $ n = ?rhs $ n" for n :: nat
wenzelm@60501
  1925
  proof -
huffman@30488
  1926
    have "?lhs $ n = (if n < Suc k then 0 else a n)"
chaieb@29687
  1927
      unfolding X_power_mult_nth by auto
chaieb@29687
  1928
    also have "\<dots> = ?rhs $ n"
wenzelm@52902
  1929
    proof (induct k)
wenzelm@52902
  1930
      case 0
wenzelm@60501
  1931
      then show ?case
wenzelm@60501
  1932
        by (simp add: fps_setsum_nth)
chaieb@29687
  1933
    next
chaieb@29687
  1934
      case (Suc k)
wenzelm@52902
  1935
      have "(Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a) * X^i) {0 .. Suc k})$n =
wenzelm@53196
  1936
        (Abs_fps a - setsum (\<lambda>i. fps_const (a i :: 'a) * X^i) {0 .. k} -
wenzelm@53196
  1937
          fps_const (a (Suc k)) * X^ Suc k) $ n"
wenzelm@52902
  1938
        by (simp add: field_simps)
wenzelm@52902
  1939
      also have "\<dots> = (if n < Suc k then 0 else a n) - (fps_const (a (Suc k)) * X^ Suc k)$n"
wenzelm@60501
  1940
        using Suc.hyps[symmetric] unfolding fps_sub_nth by simp
chaieb@29687
  1941
      also have "\<dots> = (if n < Suc (Suc k) then 0 else a n)"
wenzelm@32960
  1942
        unfolding X_power_mult_right_nth
wenzelm@32960
  1943
        apply (auto simp add: not_less fps_const_def)
wenzelm@32960
  1944
        apply (rule cong[of a a, OF refl])
wenzelm@52902
  1945
        apply arith
wenzelm@52902
  1946
        done
wenzelm@60501
  1947
      finally show ?case
wenzelm@60501
  1948
        by simp
chaieb@29687
  1949
    qed
wenzelm@60501
  1950
    finally show ?thesis .
wenzelm@60501
  1951
  qed
wenzelm@60501
  1952
  then show ?thesis
wenzelm@60501
  1953
    by (simp add: fps_eq_iff)
chaieb@29687
  1954
qed
chaieb@29687
  1955
wenzelm@53195
  1956
wenzelm@60500
  1957
subsubsection \<open>Rule 2\<close>
chaieb@29687
  1958
chaieb@29687
  1959
  (* We can not reach the form of Wilf, but still near to it using rewrite rules*)
huffman@30488
  1960
  (* If f reprents {a_n} and P is a polynomial, then
chaieb@29687
  1961
        P(xD) f represents {P(n) a_n}*)
chaieb@29687
  1962
wenzelm@54681
  1963
definition "XD = op * X \<circ> fps_deriv"
wenzelm@54681
  1964
wenzelm@54681
  1965
lemma XD_add[simp]:"XD (a + b) = XD a + XD (b :: 'a::comm_ring_1 fps)"
haftmann@36350
  1966
  by (simp add: XD_def field_simps)
chaieb@29687
  1967
chaieb@29687
  1968
lemma XD_mult_const[simp]:"XD (fps_const (c::'a::comm_ring_1) * a) = fps_const c * XD a"
haftmann@36350
  1969
  by (simp add: XD_def field_simps)
chaieb@29687
  1970
wenzelm@52902
  1971
lemma XD_linear[simp]: "XD (fps_const c * a + fps_const d * b) =
wenzelm@54681
  1972
    fps_const c * XD a + fps_const d * XD (b :: 'a::comm_ring_1 fps)"
chaieb@29687
  1973
  by simp
chaieb@29687
  1974
haftmann@30952
  1975
lemma XDN_linear:
wenzelm@52902
  1976
  "(XD ^^ n) (fps_const c * a + fps_const d * b) =
wenzelm@54681
  1977
    fps_const c * (XD ^^ n) a + fps_const d * (XD ^^ n) (b :: 'a::comm_ring_1 fps)"
wenzelm@48757
  1978
  by (induct n) simp_all
chaieb@29687
  1979
wenzelm@52902
  1980
lemma fps_mult_X_deriv_shift: "X* fps_deriv a = Abs_fps (\<lambda>n. of_nat n* a$n)"
wenzelm@52902
  1981
  by (simp add: fps_eq_iff)
chaieb@29687
  1982
haftmann@30952
  1983
lemma fps_mult_XD_shift:
wenzelm@54681
  1984
  "(XD ^^ k) (a :: 'a::comm_ring_1 fps) = Abs_fps (\<lambda>n. (of_nat n ^ k) * a$n)"
wenzelm@52902
  1985
  by (induct k arbitrary: a) (simp_all add: XD_def fps_eq_iff field_simps del: One_nat_def)
chaieb@29687
  1986
wenzelm@53195
  1987
wenzelm@60501
  1988
subsubsection \<open>Rule 3\<close>
wenzelm@60501
  1989
wenzelm@61585
  1990
text \<open>Rule 3 is trivial and is given by \<open>fps_times_def\<close>.\<close>
wenzelm@60501
  1991
wenzelm@60500
  1992
wenzelm@60500
  1993
subsubsection \<open>Rule 5 --- summation and "division" by (1 - X)\<close>
chaieb@29687
  1994
chaieb@29687
  1995
lemma fps_divide_X_minus1_setsum_lemma:
wenzelm@54681
  1996
  "a = ((1::'a::comm_ring_1 fps) - X) * Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
wenzelm@53195
  1997
proof -
chaieb@29687
  1998
  let ?sa = "Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
wenzelm@52902
  1999
  have th0: "\<And>i. (1 - (X::'a fps)) $ i = (if i = 0 then 1 else if i = 1 then - 1 else 0)"
wenzelm@52902
  2000
    by simp
wenzelm@60501
  2001
  have "a$n = ((1 - X) * ?sa) $ n" for n
wenzelm@60501
  2002
  proof (cases "n = 0")
wenzelm@60501
  2003
    case True
wenzelm@60501
  2004
    then show ?thesis
wenzelm@60501
  2005
      by (simp add: fps_mult_nth)
wenzelm@60501
  2006
  next
wenzelm@60501
  2007
    case False
wenzelm@60501
  2008
    then have u: "{0} \<union> ({1} \<union> {2..n}) = {0..n}" "{1} \<union> {2..n} = {1..n}"
wenzelm@60501
  2009
      "{0..n - 1} \<union> {n} = {0..n}"
wenzelm@60501
  2010
      by (auto simp: set_eq_iff)
wenzelm@60501
  2011
    have d: "{0} \<inter> ({1} \<union> {2..n}) = {}" "{1} \<inter> {2..n} = {}" "{0..n - 1} \<inter> {n} = {}"
wenzelm@60501
  2012
      using False by simp_all
wenzelm@60501
  2013
    have f: "finite {0}" "finite {1}" "finite {2 .. n}"
wenzelm@60501
  2014
      "finite {0 .. n - 1}" "finite {n}" by simp_all
wenzelm@60501
  2015
    have "((1 - X) * ?sa) $ n = setsum (\<lambda>i. (1 - X)$ i * ?sa $ (n - i)) {0 .. n}"
wenzelm@60501
  2016
      by (simp add: fps_mult_nth)
wenzelm@60501
  2017
    also have "\<dots> = a$n"
wenzelm@60501
  2018
      unfolding th0
wenzelm@60501
  2019
      unfolding setsum.union_disjoint[OF f(1) finite_UnI[OF f(2,3)] d(1), unfolded u(1)]
wenzelm@60501
  2020
      unfolding setsum.union_disjoint[OF f(2) f(3) d(2)]
wenzelm@60501
  2021
      apply (simp)
wenzelm@60501
  2022
      unfolding setsum.union_disjoint[OF f(4,5) d(3), unfolded u(3)]
wenzelm@60501
  2023
      apply simp
wenzelm@60501
  2024
      done
wenzelm@60501
  2025
    finally show ?thesis
wenzelm@60501
  2026
      by simp
wenzelm@60501
  2027
  qed
wenzelm@54681
  2028
  then show ?thesis
wenzelm@54681
  2029
    unfolding fps_eq_iff by blast
chaieb@29687
  2030
qed
chaieb@29687
  2031
chaieb@29687
  2032
lemma fps_divide_X_minus1_setsum:
wenzelm@54681
  2033
  "a /((1::'a::field fps) - X) = Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
wenzelm@52902
  2034
proof -
wenzelm@54681
  2035
  let ?X = "1 - (X::'a fps)"
wenzelm@54681
  2036
  have th0: "?X $ 0 \<noteq> 0"
wenzelm@54681
  2037
    by simp
wenzelm@54681
  2038
  have "a /?X = ?X *  Abs_fps (\<lambda>n::nat. setsum (op $ a) {0..n}) * inverse ?X"
chaieb@29687
  2039
    using fps_divide_X_minus1_setsum_lemma[of a, symmetric] th0
haftmann@57512
  2040
    by (simp add: fps_divide_def mult.assoc)
wenzelm@54681
  2041
  also have "\<dots> = (inverse ?X * ?X) * Abs_fps (\<lambda>n::nat. setsum (op $ a) {0..n}) "
haftmann@57514
  2042
    by (simp add: ac_simps)
wenzelm@54681
  2043
  finally show ?thesis
wenzelm@54681
  2044
    by (simp add: inverse_mult_eq_1[OF th0])
chaieb@29687
  2045
qed
chaieb@29687
  2046
wenzelm@53195
  2047
wenzelm@60501
  2048
subsubsection \<open>Rule 4 in its more general form: generalizes Rule 3 for an arbitrary
wenzelm@60500
  2049
  finite product of FPS, also the relvant instance of powers of a FPS\<close>
chaieb@29687
  2050
haftmann@46131
  2051
definition "natpermute n k = {l :: nat list. length l = k \<and> listsum l = n}"
chaieb@29687
  2052
chaieb@29687
  2053
lemma natlist_trivial_1: "natpermute n 1 = {[n]}"
chaieb@29687
  2054
  apply (auto simp add: natpermute_def)
wenzelm@52902
  2055
  apply (case_tac x)
wenzelm@52902
  2056
  apply auto
chaieb@29687
  2057
  done
chaieb@29687
  2058
chaieb@29687
  2059
lemma append_natpermute_less_eq:
wenzelm@54452
  2060
  assumes "xs @ ys \<in> natpermute n k"
wenzelm@54681
  2061
  shows "listsum xs \<le> n"
wenzelm@54681
  2062
    and "listsum ys \<le> n"
wenzelm@52902
  2063
proof -
wenzelm@54452
  2064
  from assms have "listsum (xs @ ys) = n"
wenzelm@54452
  2065
    by (simp add: natpermute_def)
wenzelm@54452
  2066
  then have "listsum xs + listsum ys = n"
wenzelm@54452
  2067
    by simp
wenzelm@54452
  2068
  then show "listsum xs \<le> n" and "listsum ys \<le> n"
wenzelm@54452
  2069
    by simp_all
chaieb@29687
  2070
qed
chaieb@29687
  2071
chaieb@29687
  2072
lemma natpermute_split:
wenzelm@54452
  2073
  assumes "h \<le> k"
wenzelm@52902
  2074
  shows "natpermute n k =
wenzelm@52902
  2075
    (\<Union>m \<in>{0..n}. {l1 @ l2 |l1 l2. l1 \<in> natpermute m h \<and> l2 \<in> natpermute (n - m) (k - h)})"
wenzelm@60558
  2076
  (is "?L = ?R" is "_ = (\<Union>m \<in>{0..n}. ?S m)")
wenzelm@60558
  2077
proof
wenzelm@60558
  2078
  show "?R \<subseteq> ?L"
wenzelm@60558
  2079
  proof
wenzelm@52902
  2080
    fix l
wenzelm@52902
  2081
    assume l: "l \<in> ?R"
wenzelm@52902
  2082
    from l obtain m xs ys where h: "m \<in> {0..n}"
wenzelm@52902
  2083
      and xs: "xs \<in> natpermute m h"
wenzelm@52902
  2084
      and ys: "ys \<in> natpermute (n - m) (k - h)"
wenzelm@52902
  2085
      and leq: "l = xs@ys" by blast
wenzelm@52902
  2086
    from xs have xs': "listsum xs = m"
wenzelm@52902
  2087
      by (simp add: natpermute_def)
wenzelm@52902
  2088
    from ys have ys': "listsum ys = n - m"
wenzelm@52902
  2089
      by (simp add: natpermute_def)
wenzelm@60558
  2090
    show "l \<in> ?L" using leq xs ys h
haftmann@46131
  2091
      apply (clarsimp simp add: natpermute_def)
chaieb@29687
  2092
      unfolding xs' ys'
wenzelm@54452
  2093
      using assms xs ys
wenzelm@48757
  2094
      unfolding natpermute_def
wenzelm@48757
  2095
      apply simp
wenzelm@48757
  2096
      done
wenzelm@60558
  2097
  qed
wenzelm@60558
  2098
  show "?L \<subseteq> ?R"
wenzelm@60558
  2099
  proof
wenzelm@52902
  2100
    fix l
wenzelm@52902
  2101
    assume l: "l \<in> natpermute n k"
chaieb@29687
  2102
    let ?xs = "take h l"
chaieb@29687
  2103
    let ?ys = "drop h l"
haftmann@46131
  2104
    let ?m = "listsum ?xs"
wenzelm@52902
  2105
    from l have ls: "listsum (?xs @ ?ys) = n"
wenzelm@52902
  2106
      by (simp add: natpermute_def)
wenzelm@54452
  2107
    have xs: "?xs \<in> natpermute ?m h" using l assms
wenzelm@52902
  2108
      by (simp add: natpermute_def)
wenzelm@52902
  2109
    have l_take_drop: "listsum l = listsum (take h l @ drop h l)"
wenzelm@52902
  2110
      by simp
wenzelm@52902
  2111
    then have ys: "?ys \<in> natpermute (n - ?m) (k - h)"
wenzelm@54452
  2112
      using l assms ls by (auto simp add: natpermute_def simp del: append_take_drop_id)
wenzelm@52902
  2113
    from ls have m: "?m \<in> {0..n}"
wenzelm@52902
  2114
      by (simp add: l_take_drop del: append_take_drop_id)
wenzelm@60558
  2115
    from xs ys ls show "l \<in> ?R"
chaieb@29687
  2116
      apply auto
wenzelm@52902
  2117
      apply (rule bexI [where x = "?m"])
wenzelm@52902
  2118
      apply (rule exI [where x = "?xs"])
wenzelm@52902
  2119
      apply (rule exI [where x = "?ys"])
wenzelm@52891
  2120
      using ls l
haftmann@46131
  2121
      apply (auto simp add: natpermute_def l_take_drop simp del: append_take_drop_id)
wenzelm@48757
  2122
      apply simp
wenzelm@48757
  2123
      done
wenzelm@60558
  2124
  qed
chaieb@29687
  2125
qed
chaieb@29687
  2126
chaieb@29687
  2127
lemma natpermute_0: "natpermute n 0 = (if n = 0 then {[]} else {})"
chaieb@29687
  2128
  by (auto simp add: natpermute_def)
wenzelm@52902
  2129
chaieb@29687
  2130
lemma natpermute_0'[simp]: "natpermute 0 k = (if k = 0 then {[]} else {replicate k 0})"
chaieb@29687
  2131
  apply (auto simp add: set_replicate_conv_if natpermute_def)
chaieb@29687
  2132
  apply (rule nth_equalityI)
wenzelm@48757
  2133
  apply simp_all
wenzelm@48757
  2134
  done
chaieb@29687
  2135
chaieb@29687
  2136
lemma natpermute_finite: "finite (natpermute n k)"
wenzelm@52902
  2137
proof (induct k arbitrary: n)
wenzelm@52902
  2138
  case 0
wenzelm@52902
  2139
  then show ?case
chaieb@29687
  2140
    apply (subst natpermute_split[of 0 0, simplified])
wenzelm@52902
  2141
    apply (simp add: natpermute_0)
wenzelm@52902
  2142
    done
chaieb@29687
  2143
next
chaieb@29687
  2144
  case (Suc k)
wenzelm@52902
  2145
  then show ?case unfolding natpermute_split [of k "Suc k", simplified]
chaieb@29687
  2146
    apply -
chaieb@29687
  2147
    apply (rule finite_UN_I)
chaieb@29687
  2148
    apply simp
chaieb@29687
  2149
    unfolding One_nat_def[symmetric] natlist_trivial_1
chaieb@29687
  2150
    apply simp
chaieb@29687
  2151
    done
chaieb@29687
  2152
qed
chaieb@29687
  2153
chaieb@29687
  2154
lemma natpermute_contain_maximal:
wenzelm@60558
  2155
  "{xs \<in> natpermute n (k + 1). n \<in> set xs} = (\<Union>i\<in>{0 .. k}. {(replicate (k + 1) 0) [i:=n]})"
chaieb@29687
  2156
  (is "?A = ?B")
wenzelm@60558
  2157
proof
wenzelm@60558
  2158
  show "?A \<subseteq> ?B"
wenzelm@60558
  2159
  proof
wenzelm@52902
  2160
    fix xs
wenzelm@60558
  2161
    assume "xs \<in> ?A"
wenzelm@60558
  2162
    then have H: "xs \<in> natpermute n (k + 1)" and n: "n \<in> set xs"
wenzelm@60558
  2163
      by blast+
wenzelm@60558
  2164
    then obtain i where i: "i \<in> {0.. k}" "xs!i = n"
huffman@30488
  2165
      unfolding in_set_conv_nth by (auto simp add: less_Suc_eq_le natpermute_def)
wenzelm@52902
  2166
    have eqs: "({0..k} - {i}) \<union> {i} = {0..k}"
wenzelm@52902
  2167
      using i by auto
wenzelm@52902
  2168
    have f: "finite({0..k} - {i})" "finite {i}"
wenzelm@52902
  2169
      by auto
wenzelm@52902
  2170
    have d: "({0..k} - {i}) \<inter> {i} = {}"
wenzelm@52902
  2171
      using i by auto
wenzelm@52902
  2172
    from H have "n = setsum (nth xs) {0..k}"
wenzelm@52902
  2173
      apply (simp add: natpermute_def)
wenzelm@52902
  2174
      apply (auto simp add: atLeastLessThanSuc_atLeastAtMost listsum_setsum_nth)
wenzelm@52902
  2175
      done
chaieb@29687
  2176
    also have "\<dots> = n + setsum (nth xs) ({0..k} - {i})"
haftmann@57418
  2177
      unfolding setsum.union_disjoint[OF f d, unfolded eqs] using i by simp
wenzelm@52902
  2178
    finally have zxs: "\<forall> j\<in> {0..k} - {i}. xs!j = 0"
wenzelm@52902
  2179
      by auto
wenzelm@52902
  2180
    from H have xsl: "length xs = k+1"
wenzelm@52902
  2181
      by (simp add: natpermute_def)
chaieb@29687
  2182
    from i have i': "i < length (replicate (k+1) 0)"   "i < k+1"
wenzelm@52902
  2183
      unfolding length_replicate by presburger+
chaieb@29687
  2184
    have "xs = replicate (k+1) 0 [i := n]"
chaieb@29687
  2185
      apply (rule nth_equalityI)
chaieb@29687
  2186
      unfolding xsl length_list_update length_replicate
chaieb@29687
  2187
      apply simp
chaieb@29687
  2188
      apply clarify
chaieb@29687
  2189
      unfolding nth_list_update[OF i'(1)]
chaieb@29687
  2190
      using i zxs
wenzelm@52902
  2191
      apply (case_tac "ia = i")
wenzelm@52902
  2192
      apply (auto simp del: replicate.simps)
wenzelm@52902
  2193
      done
wenzelm@60558
  2194
    then show "xs \<in> ?B" using i by blast
wenzelm@60558
  2195
  qed
wenzelm@60558
  2196
  show "?B \<subseteq> ?A"
wenzelm@60558
  2197
  proof
wenzelm@60558
  2198
    fix xs
wenzelm@60558
  2199
    assume "xs \<in> ?B"
wenzelm@60558
  2200
    then obtain i where i: "i \<in> {0..k}" and xs: "xs = replicate (k + 1) 0 [i:=n]"
wenzelm@60558
  2201
      by auto
wenzelm@60558
  2202
    have nxs: "n \<in> set xs"
wenzelm@60558
  2203
      unfolding xs
wenzelm@52902
  2204
      apply (rule set_update_memI)
wenzelm@52902
  2205
      using i apply simp
wenzelm@52902
  2206
      done
wenzelm@60558
  2207
    have xsl: "length xs = k + 1"
wenzelm@60558
  2208
      by (simp only: xs length_replicate length_list_update)
wenzelm@60558
  2209
    have "listsum xs = setsum (nth xs) {0..<k+1}"
haftmann@46131
  2210
      unfolding listsum_setsum_nth xsl ..
chaieb@29687
  2211
    also have "\<dots> = setsum (\<lambda>j. if j = i then n else 0) {0..< k+1}"
wenzelm@60558
  2212
      by (rule setsum.cong) (simp_all add: xs del: replicate.simps)
haftmann@57418
  2213
    also have "\<dots> = n" using i by (simp add: setsum.delta)
wenzelm@60558
  2214
    finally have "xs \<in> natpermute n (k + 1)"
wenzelm@52902
  2215
      using xsl unfolding natpermute_def mem_Collect_eq by blast
wenzelm@60558
  2216
    then show "xs \<in> ?A"
wenzelm@60558
  2217
      using nxs by blast
wenzelm@60558
  2218
  qed
chaieb@29687
  2219
qed
chaieb@29687
  2220
wenzelm@60558
  2221
text \<open>The general form.\<close>
chaieb@29687
  2222
lemma fps_setprod_nth:
wenzelm@52902
  2223
  fixes m :: nat
wenzelm@54681
  2224
    and a :: "nat \<Rightarrow> 'a::comm_ring_1 fps"
wenzelm@54681
  2225
  shows "(setprod a {0 .. m}) $ n =
wenzelm@53196
  2226
    setsum (\<lambda>v. setprod (\<lambda>j. (a j) $ (v!j)) {0..m}) (natpermute n (m+1))"
chaieb@29687
  2227
  (is "?P m n")
wenzelm@52902
  2228
proof (induct m arbitrary: n rule: nat_less_induct)
chaieb@29687
  2229
  fix m n assume H: "\<forall>m' < m. \<forall>n. ?P m' n"
wenzelm@53196
  2230
  show "?P m n"
wenzelm@53196
  2231
  proof (cases m)
wenzelm@53196
  2232
    case 0
wenzelm@53196
  2233
    then show ?thesis
wenzelm@53196
  2234
      apply simp
wenzelm@53196
  2235
      unfolding natlist_trivial_1[where n = n, unfolded One_nat_def]
wenzelm@53196
  2236
      apply simp
wenzelm@53196
  2237
      done
wenzelm@53196
  2238
  next
wenzelm@53196
  2239
    case (Suc k)
wenzelm@53196
  2240
    then have km: "k < m" by arith
wenzelm@52902
  2241
    have u0: "{0 .. k} \<union> {m} = {0..m}"
wenzelm@54452
  2242
      using Suc by (simp add: set_eq_iff) presburger
chaieb@29687
  2243
    have f0: "finite {0 .. k}" "finite {m}" by auto
wenzelm@53196
  2244
    have d0: "{0 .. k} \<inter> {m} = {}" using Suc by auto
chaieb@29687
  2245
    have "(setprod a {0 .. m}) $ n = (setprod a {0 .. k} * a m) $ n"
haftmann@57418
  2246
      unfolding setprod.union_disjoint[OF f0 d0, unfolded u0] by simp
chaieb@29687
  2247
    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
  2248
      unfolding fps_mult_nth H[rule_format, OF km] ..
chaieb@29687
  2249
    also have "\<dots> = (\<Sum>v\<in>natpermute n (m + 1). \<Prod>j\<in>{0..m}. a j $ v ! j)"
wenzelm@53196
  2250
      apply (simp add: Suc)
wenzelm@48757
  2251
      unfolding natpermute_split[of m "m + 1", simplified, of n,
wenzelm@53196
  2252
        unfolded natlist_trivial_1[unfolded One_nat_def] Suc]
haftmann@57418
  2253
      apply (subst setsum.UNION_disjoint)
huffman@30488
  2254
      apply simp
chaieb@29687
  2255
      apply simp
chaieb@29687
  2256
      unfolding image_Collect[symmetric]
chaieb@29687
  2257
      apply clarsimp
chaieb@29687
  2258
      apply (rule finite_imageI)
chaieb@29687
  2259
      apply (rule natpermute_finite)
nipkow@39302
  2260
      apply (clarsimp simp add: set_eq_iff)
chaieb@29687
  2261
      apply auto
haftmann@57418
  2262
      apply (rule setsum.cong)
haftmann@57418
  2263
      apply (rule refl)
chaieb@29687
  2264
      unfolding setsum_left_distrib
chaieb@29687
  2265
      apply (rule sym)
haftmann@57418
  2266
      apply (rule_tac l = "\<lambda>xs. xs @ [n - x]" in setsum.reindex_cong)
chaieb@29687
  2267
      apply (simp add: inj_on_def)
chaieb@29687
  2268
      apply auto
haftmann@57418
  2269
      unfolding setprod.union_disjoint[OF f0 d0, unfolded u0, unfolded Suc]
chaieb@29687
  2270
      apply (clarsimp simp add: natpermute_def nth_append)
chaieb@29687
  2271
      done
wenzelm@53196
  2272
    finally show ?thesis .
wenzelm@53196
  2273
  qed
chaieb@29687
  2274
qed
chaieb@29687
  2275
wenzelm@60558
  2276
text \<open>The special form for powers.\<close>
chaieb@29687
  2277
lemma fps_power_nth_Suc:
wenzelm@52903
  2278
  fixes m :: nat
wenzelm@54681
  2279
    and a :: "'a::comm_ring_1 fps"
chaieb@29687
  2280
  shows "(a ^ Suc m)$n = setsum (\<lambda>v. setprod (\<lambda>j. a $ (v!j)) {0..m}) (natpermute n (m+1))"
wenzelm@52902
  2281
proof -
wenzelm@54452
  2282
  have th0: "a^Suc m = setprod (\<lambda>i. a) {0..m}"
wenzelm@54452
  2283
    by (simp add: setprod_constant)
chaieb@29687
  2284
  show ?thesis unfolding th0 fps_setprod_nth ..
chaieb@29687
  2285
qed
wenzelm@52902
  2286
chaieb@29687
  2287
lemma fps_power_nth:
wenzelm@54452
  2288
  fixes m :: nat
wenzelm@54681
  2289
    and a :: "'a::comm_ring_1 fps"
wenzelm@53196
  2290
  shows "(a ^m)$n =
wenzelm@53196
  2291
    (if m=0 then 1$n else setsum (\<lambda>v. setprod (\<lambda>j. a $ (v!j)) {0..m - 1}) (natpermute n m))"
wenzelm@52902
  2292
  by (cases m) (simp_all add: fps_power_nth_Suc del: power_Suc)
chaieb@29687
  2293
huffman@30488
  2294
lemma fps_nth_power_0:
wenzelm@54452
  2295
  fixes m :: nat
wenzelm@54681
  2296
    and a :: "'a::comm_ring_1 fps"
chaieb@29687
  2297
  shows "(a ^m)$0 = (a$0) ^ m"
wenzelm@53195
  2298
proof (cases m)
wenzelm@53195
  2299
  case 0
wenzelm@53195
  2300
  then show ?thesis by simp
wenzelm@53195
  2301
next
wenzelm@53195
  2302
  case (Suc n)
wenzelm@53195
  2303
  then have c: "m = card {0..n}" by simp
wenzelm@53195
  2304
  have "(a ^m)$0 = setprod (\<lambda>i. a$0) {0..n}"
wenzelm@53195
  2305
    by (simp add: Suc fps_power_nth del: replicate.simps power_Suc)
wenzelm@53195
  2306
  also have "\<dots> = (a$0) ^ m"
wenzelm@53195
  2307
   unfolding c by (rule setprod_constant) simp
wenzelm@53195
  2308
 finally show ?thesis .
chaieb@29687
  2309
qed
chaieb@29687
  2310
huffman@30488
  2311
lemma fps_compose_inj_right:
wenzelm@54681
  2312
  assumes a0: "a$0 = (0::'a::idom)"
wenzelm@52902
  2313
    and a1: "a$1 \<noteq> 0"
wenzelm@54681
  2314
  shows "(b oo a = c oo a) \<longleftrightarrow> b = c"
wenzelm@54681
  2315
  (is "?lhs \<longleftrightarrow>?rhs")
wenzelm@53196
  2316
proof
wenzelm@60501
  2317
  show ?lhs if ?rhs using that by simp
wenzelm@60501
  2318
  show ?rhs if ?lhs
wenzelm@60501
  2319
  proof -
wenzelm@60501
  2320
    have "b$n = c$n" for n
wenzelm@53196
  2321
    proof (induct n rule: nat_less_induct)
wenzelm@53196
  2322
      fix n
wenzelm@53196
  2323
      assume H: "\<forall>m<n. b$m = c$m"
wenzelm@60501
  2324
      show "b$n = c$n"
wenzelm@60501
  2325
      proof (cases n)
wenzelm@60501
  2326
        case 0
wenzelm@60501
  2327
        from \<open>?lhs\<close> have "(b oo a)$n = (c oo a)$n"
wenzelm@60501
  2328
          by simp
wenzelm@60501
  2329
        then show ?thesis
wenzelm@60501
  2330
          using 0 by (simp add: fps_compose_nth)
wenzelm@60501
  2331
      next
wenzelm@60501
  2332
        case (Suc n1)
wenzelm@53196
  2333
        have f: "finite {0 .. n1}" "finite {n}" by simp_all
wenzelm@60501
  2334
        have eq: "{0 .. n1} \<union> {n} = {0 .. n}" using Suc by auto
wenzelm@60501
  2335
        have d: "{0 .. n1} \<inter> {n} = {}" using Suc by auto
wenzelm@53196
  2336
        have seq: "(\<Sum>i = 0..n1. b $ i * a ^ i $ n) = (\<Sum>i = 0..n1. c $ i * a ^ i $ n)"
haftmann@57418
  2337
          apply (rule setsum.cong)
wenzelm@60501
  2338
          using H Suc
wenzelm@53196
  2339
          apply auto
wenzelm@53196
  2340
          done
wenzelm@53196
  2341
        have th0: "(b oo a) $n = (\<Sum>i = 0..n1. c $ i * a ^ i $ n) + b$n * (a$1)^n"
haftmann@57418
  2342
          unfolding fps_compose_nth setsum.union_disjoint[OF f d, unfolded eq] seq
wenzelm@53196
  2343
          using startsby_zero_power_nth_same[OF a0]
wenzelm@53196
  2344
          by simp
wenzelm@53196
  2345
        have th1: "(c oo a) $n = (\<Sum>i = 0..n1. c $ i * a ^ i $ n) + c$n * (a$1)^n"
haftmann@57418
  2346
          unfolding fps_compose_nth setsum.union_disjoint[OF f d, unfolded eq]
wenzelm@53196
  2347
          using startsby_zero_power_nth_same[OF a0]
wenzelm@53196
  2348
          by simp
wenzelm@60501
  2349
        from \<open>?lhs\<close>[unfolded fps_eq_iff, rule_format, of n] th0 th1 a1
wenzelm@60501
  2350
        show ?thesis by auto
wenzelm@60501
  2351
      qed
wenzelm@60501
  2352
    qed
wenzelm@60501
  2353
    then show ?rhs by (simp add: fps_eq_iff)
wenzelm@60501
  2354
  qed
chaieb@29687
  2355
qed
chaieb@29687
  2356
chaieb@29687
  2357
wenzelm@60500
  2358
subsection \<open>Radicals\<close>
chaieb@29687
  2359
haftmann@57418
  2360
declare setprod.cong [fundef_cong]
wenzelm@52903
  2361
wenzelm@54681
  2362
function radical :: "(nat \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a::field fps \<Rightarrow> nat \<Rightarrow> 'a"
wenzelm@52902
  2363
where
chaieb@29687
  2364
  "radical r 0 a 0 = 1"
chaieb@29687
  2365
| "radical r 0 a (Suc n) = 0"
chaieb@29687
  2366
| "radical r (Suc k) a 0 = r (Suc k) (a$0)"
wenzelm@48757
  2367
| "radical r (Suc k) a (Suc n) =
wenzelm@48757
  2368
    (a$ Suc n - setsum (\<lambda>xs. setprod (\<lambda>j. radical r (Suc k) a (xs ! j)) {0..k})
wenzelm@48757
  2369
      {xs. xs \<in> natpermute (Suc n) (Suc k) \<and> Suc n \<notin> set xs}) /
wenzelm@48757
  2370
    (of_nat (Suc k) * (radical r (Suc k) a 0)^k)"
wenzelm@52902
  2371
  by pat_completeness auto
chaieb@29687
  2372
chaieb@29687
  2373
termination radical
chaieb@29687
  2374
proof
chaieb@29687
  2375
  let ?R = "measure (\<lambda>(r, k, a, n). n)"
chaieb@29687
  2376
  {
wenzelm@52902
  2377
    show "wf ?R" by auto
wenzelm@52902
  2378
  next
wenzelm@52902
  2379
    fix r k a n xs i
chaieb@29687
  2380
    assume xs: "xs \<in> {xs \<in> natpermute (Suc n) (Suc k). Suc n \<notin> set xs}" and i: "i \<in> {0..k}"
wenzelm@60558
  2381
    have False if c: "Suc n \<le> xs ! i"
wenzelm@60558
  2382
    proof -
wenzelm@52902
  2383
      from xs i have "xs !i \<noteq> Suc n"
wenzelm@52902
  2384
        by (auto simp add: in_set_conv_nth natpermute_def)
chaieb@29687
  2385
      with c have c': "Suc n < xs!i" by arith
wenzelm@52902
  2386
      have fths: "finite {0 ..< i}" "finite {i}" "finite {i+1..<Suc k}"
wenzelm@52902
  2387
        by simp_all
wenzelm@52902
  2388
      have d: "{0 ..< i} \<inter> ({i} \<union> {i+1 ..< Suc k}) = {}" "{i} \<inter> {i+1..< Suc k} = {}"
wenzelm@52902
  2389
        by auto
wenzelm@52902
  2390
      have eqs: "{0..<Suc k} = {0 ..< i} \<union> ({i} \<union> {i+1 ..< Suc k})"
wenzelm@52902
  2391
        using i by auto
wenzelm@52902
  2392
      from xs have "Suc n = listsum xs"
wenzelm@52902
  2393
        by (simp add: natpermute_def)
haftmann@46131
  2394
      also have "\<dots> = setsum (nth xs) {0..<Suc k}" using xs
haftmann@46131
  2395
        by (simp add: natpermute_def listsum_setsum_nth)
chaieb@29687
  2396
      also have "\<dots> = xs!i + setsum (nth xs) {0..<i} + setsum (nth xs) {i+1..<Suc k}"
haftmann@57418
  2397
        unfolding eqs  setsum.union_disjoint[OF fths(1) finite_UnI[OF fths(2,3)] d(1)]
haftmann@57418
  2398
        unfolding setsum.union_disjoint[OF fths(2) fths(3) d(2)]
wenzelm@32960
  2399
        by simp
wenzelm@60558
  2400
      finally show ?thesis using c' by simp
wenzelm@60558
  2401
    qed
wenzelm@52902
  2402
    then show "((r, Suc k, a, xs!i), r, Suc k, a, Suc n) \<in> ?R"
wenzelm@52902
  2403
      apply auto
wenzelm@52902
  2404
      apply (metis not_less)
wenzelm@52902
  2405
      done
wenzelm@52902
  2406
  next
wenzelm@52902
  2407
    fix r k a n
wenzelm@52902
  2408
    show "((r, Suc k, a, 0), r, Suc k, a, Suc n) \<in> ?R" by simp
wenzelm@52902
  2409
  }
chaieb@29687
  2410
qed
chaieb@29687
  2411
chaieb@29687
  2412
definition "fps_radical r n a = Abs_fps (radical r n a)"
chaieb@29687
  2413
chaieb@29687
  2414
lemma fps_radical0[simp]: "fps_radical r 0 a = 1"
wenzelm@52902
  2415
  apply (auto simp add: fps_eq_iff fps_radical_def)
wenzelm@52902
  2416
  apply (case_tac n)
wenzelm@52902
  2417
  apply auto
wenzelm@52902
  2418
  done
chaieb@29687
  2419
wenzelm@60501
  2420
lemma fps_radical_nth_0[simp]: "fps_radical r n a $ 0 = (if n = 0 then 1 else r n (a$0))"
wenzelm@52902
  2421
  by (cases n) (simp_all add: fps_radical_def)
chaieb@29687
  2422
huffman@30488
  2423
lemma fps_radical_power_nth[simp]:
chaieb@29687
  2424
  assumes r: "(r k (a$0)) ^ k = a$0"
chaieb@29687
  2425
  shows "fps_radical r k a ^ k $ 0 = (if k = 0 then 1 else a$0)"
wenzelm@53196
  2426
proof (cases k)
wenzelm@53196
  2427
  case 0
wenzelm@53196
  2428
  then show ?thesis by simp
wenzelm@53196
  2429
next
wenzelm@53196
  2430
  case (Suc h)
wenzelm@53196
  2431
  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
  2432
    unfolding fps_power_nth Suc by simp
wenzelm@53196
  2433
  also have "\<dots> = (\<Prod>j\<in>{0..h}. r k (a$0))"
haftmann@57418
  2434
    apply (rule setprod.cong)
wenzelm@53196
  2435
    apply simp
wenzelm@53196
  2436
    using Suc
wenzelm@54681
  2437
    apply (subgoal_tac "replicate k 0 ! x = 0")
wenzelm@53196
  2438
    apply (auto intro: nth_replicate simp del: replicate.simps)
wenzelm@53196
  2439
    done
wenzelm@60501
  2440
  also have "\<dots> = a$0"
wenzelm@60501
  2441
    using r Suc by (simp add: setprod_constant)
wenzelm@60501
  2442
  finally show ?thesis
wenzelm@60501
  2443
    using Suc by simp
huffman@30488
  2444
qed
chaieb@29687
  2445
wenzelm@52902
  2446
lemma natpermute_max_card:
wenzelm@54452
  2447
  assumes n0: "n \<noteq> 0"
wenzelm@60558
  2448
  shows "card {xs \<in> natpermute n (k + 1). n \<in> set xs} = k + 1"
chaieb@29687
  2449
  unfolding natpermute_contain_maximal
wenzelm@52902
  2450
proof -
wenzelm@60501
  2451
  let ?A = "\<lambda>i. {replicate (k + 1) 0[i := n]}"
chaieb@29687
  2452
  let ?K = "{0 ..k}"
wenzelm@60501
  2453
  have fK: "finite ?K"
wenzelm@60501
  2454
    by simp
wenzelm@60501
  2455
  have fAK: "\<forall>i\<in>?K. finite (?A i)"
wenzelm@60501
  2456
    by auto
wenzelm@48757
  2457
  have d: "\<forall>i\<in> ?K. \<forall>j\<in> ?K. i \<noteq> j \<longrightarrow>
wenzelm@48757
  2458
    {replicate (k + 1) 0[i := n]} \<inter> {replicate (k + 1) 0[j := n]} = {}"
wenzelm@52902
  2459
  proof clarify
wenzelm@52902
  2460
    fix i j
wenzelm@60501
  2461
    assume i: "i \<in> ?K" and j: "j \<in> ?K" and ij: "i \<noteq> j"
wenzelm@60558
  2462
    have False if eq: "replicate (k+1) 0 [i:=n] = replicate (k+1) 0 [j:= n]"
wenzelm@60558
  2463
    proof -
wenzelm@52902
  2464
      have "(replicate (k+1) 0 [i:=n] ! i) = n"
wenzelm@52902
  2465
        using i by (simp del: replicate.simps)
chaieb@29687
  2466
      moreover
wenzelm@52902
  2467
      have "(replicate (k+1) 0 [j:=n] ! i) = 0"
wenzelm@52902
  2468
        using i ij by (simp del: replicate.simps)
wenzelm@60558
  2469
      ultimately show ?thesis
wenzelm@52902
  2470
        using eq n0 by (simp del: replicate.simps)
wenzelm@60558
  2471
    qed
chaieb@29687
  2472
    then show "{replicate (k + 1) 0[i := n]} \<inter> {replicate (k + 1) 0[j := n]} = {}"
chaieb@29687
  2473
      by auto
chaieb@29687
  2474
  qed
wenzelm@54452
  2475
  from card_UN_disjoint[OF fK fAK d]
wenzelm@54452
  2476
  show "card (\<Union>i\<in>{0..k}. {replicate (k + 1) 0[i := n]}) = k + 1"
wenzelm@52902
  2477
    by simp
chaieb@29687
  2478
qed
huffman@30488
  2479
huffman@30488
  2480
lemma power_radical:
huffman@31273
  2481
  fixes a:: "'a::field_char_0 fps"
chaieb@31073
  2482
  assumes a0: "a$0 \<noteq> 0"