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