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