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