src/HOL/Polynomial.thy
author huffman
Mon Jan 12 08:15:07 2009 -0800 (2009-01-12)
changeset 29453 de4f26f59135
parent 29451 5f0cb3fa530d
child 29454 b0f586f38dd7
permissions -rw-r--r--
add lemmas degree_{add,diff}_less
huffman@29451
     1
(*  Title:      HOL/Polynomial.thy
huffman@29451
     2
    Author:     Brian Huffman
huffman@29451
     3
                Based on an earlier development by Clemens Ballarin
huffman@29451
     4
*)
huffman@29451
     5
huffman@29451
     6
header {* Univariate Polynomials *}
huffman@29451
     7
huffman@29451
     8
theory Polynomial
huffman@29451
     9
imports Plain SetInterval
huffman@29451
    10
begin
huffman@29451
    11
huffman@29451
    12
subsection {* Definition of type @{text poly} *}
huffman@29451
    13
huffman@29451
    14
typedef (Poly) 'a poly = "{f::nat \<Rightarrow> 'a::zero. \<exists>n. \<forall>i>n. f i = 0}"
huffman@29451
    15
  morphisms coeff Abs_poly
huffman@29451
    16
  by auto
huffman@29451
    17
huffman@29451
    18
lemma expand_poly_eq: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)"
huffman@29451
    19
by (simp add: coeff_inject [symmetric] expand_fun_eq)
huffman@29451
    20
huffman@29451
    21
lemma poly_ext: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
huffman@29451
    22
by (simp add: expand_poly_eq)
huffman@29451
    23
huffman@29451
    24
huffman@29451
    25
subsection {* Degree of a polynomial *}
huffman@29451
    26
huffman@29451
    27
definition
huffman@29451
    28
  degree :: "'a::zero poly \<Rightarrow> nat" where
huffman@29451
    29
  "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
huffman@29451
    30
huffman@29451
    31
lemma coeff_eq_0: "degree p < n \<Longrightarrow> coeff p n = 0"
huffman@29451
    32
proof -
huffman@29451
    33
  have "coeff p \<in> Poly"
huffman@29451
    34
    by (rule coeff)
huffman@29451
    35
  hence "\<exists>n. \<forall>i>n. coeff p i = 0"
huffman@29451
    36
    unfolding Poly_def by simp
huffman@29451
    37
  hence "\<forall>i>degree p. coeff p i = 0"
huffman@29451
    38
    unfolding degree_def by (rule LeastI_ex)
huffman@29451
    39
  moreover assume "degree p < n"
huffman@29451
    40
  ultimately show ?thesis by simp
huffman@29451
    41
qed
huffman@29451
    42
huffman@29451
    43
lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
huffman@29451
    44
  by (erule contrapos_np, rule coeff_eq_0, simp)
huffman@29451
    45
huffman@29451
    46
lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
huffman@29451
    47
  unfolding degree_def by (erule Least_le)
huffman@29451
    48
huffman@29451
    49
lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0"
huffman@29451
    50
  unfolding degree_def by (drule not_less_Least, simp)
huffman@29451
    51
huffman@29451
    52
huffman@29451
    53
subsection {* The zero polynomial *}
huffman@29451
    54
huffman@29451
    55
instantiation poly :: (zero) zero
huffman@29451
    56
begin
huffman@29451
    57
huffman@29451
    58
definition
huffman@29451
    59
  zero_poly_def: "0 = Abs_poly (\<lambda>n. 0)"
huffman@29451
    60
huffman@29451
    61
instance ..
huffman@29451
    62
end
huffman@29451
    63
huffman@29451
    64
lemma coeff_0 [simp]: "coeff 0 n = 0"
huffman@29451
    65
  unfolding zero_poly_def
huffman@29451
    66
  by (simp add: Abs_poly_inverse Poly_def)
huffman@29451
    67
huffman@29451
    68
lemma degree_0 [simp]: "degree 0 = 0"
huffman@29451
    69
  by (rule order_antisym [OF degree_le le0]) simp
huffman@29451
    70
huffman@29451
    71
lemma leading_coeff_neq_0:
huffman@29451
    72
  assumes "p \<noteq> 0" shows "coeff p (degree p) \<noteq> 0"
huffman@29451
    73
proof (cases "degree p")
huffman@29451
    74
  case 0
huffman@29451
    75
  from `p \<noteq> 0` have "\<exists>n. coeff p n \<noteq> 0"
huffman@29451
    76
    by (simp add: expand_poly_eq)
huffman@29451
    77
  then obtain n where "coeff p n \<noteq> 0" ..
huffman@29451
    78
  hence "n \<le> degree p" by (rule le_degree)
huffman@29451
    79
  with `coeff p n \<noteq> 0` and `degree p = 0`
huffman@29451
    80
  show "coeff p (degree p) \<noteq> 0" by simp
huffman@29451
    81
next
huffman@29451
    82
  case (Suc n)
huffman@29451
    83
  from `degree p = Suc n` have "n < degree p" by simp
huffman@29451
    84
  hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp)
huffman@29451
    85
  then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast
huffman@29451
    86
  from `degree p = Suc n` and `n < i` have "degree p \<le> i" by simp
huffman@29451
    87
  also from `coeff p i \<noteq> 0` have "i \<le> degree p" by (rule le_degree)
huffman@29451
    88
  finally have "degree p = i" .
huffman@29451
    89
  with `coeff p i \<noteq> 0` show "coeff p (degree p) \<noteq> 0" by simp
huffman@29451
    90
qed
huffman@29451
    91
huffman@29451
    92
lemma leading_coeff_0_iff [simp]: "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
huffman@29451
    93
  by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
huffman@29451
    94
huffman@29451
    95
huffman@29451
    96
subsection {* List-style constructor for polynomials *}
huffman@29451
    97
huffman@29451
    98
definition
huffman@29451
    99
  pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
huffman@29451
   100
where
huffman@29451
   101
  [code del]: "pCons a p = Abs_poly (nat_case a (coeff p))"
huffman@29451
   102
huffman@29451
   103
lemma Poly_nat_case: "f \<in> Poly \<Longrightarrow> nat_case a f \<in> Poly"
huffman@29451
   104
  unfolding Poly_def by (auto split: nat.split)
huffman@29451
   105
huffman@29451
   106
lemma coeff_pCons:
huffman@29451
   107
  "coeff (pCons a p) = nat_case a (coeff p)"
huffman@29451
   108
  unfolding pCons_def
huffman@29451
   109
  by (simp add: Abs_poly_inverse Poly_nat_case coeff)
huffman@29451
   110
huffman@29451
   111
lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a"
huffman@29451
   112
  by (simp add: coeff_pCons)
huffman@29451
   113
huffman@29451
   114
lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n"
huffman@29451
   115
  by (simp add: coeff_pCons)
huffman@29451
   116
huffman@29451
   117
lemma degree_pCons_le: "degree (pCons a p) \<le> Suc (degree p)"
huffman@29451
   118
by (rule degree_le, simp add: coeff_eq_0 coeff_pCons split: nat.split)
huffman@29451
   119
huffman@29451
   120
lemma degree_pCons_eq:
huffman@29451
   121
  "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
huffman@29451
   122
apply (rule order_antisym [OF degree_pCons_le])
huffman@29451
   123
apply (rule le_degree, simp)
huffman@29451
   124
done
huffman@29451
   125
huffman@29451
   126
lemma degree_pCons_0: "degree (pCons a 0) = 0"
huffman@29451
   127
apply (rule order_antisym [OF _ le0])
huffman@29451
   128
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
huffman@29451
   129
done
huffman@29451
   130
huffman@29451
   131
lemma degree_pCons_eq_if:
huffman@29451
   132
  "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
huffman@29451
   133
apply (cases "p = 0", simp_all)
huffman@29451
   134
apply (rule order_antisym [OF _ le0])
huffman@29451
   135
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
huffman@29451
   136
apply (rule order_antisym [OF degree_pCons_le])
huffman@29451
   137
apply (rule le_degree, simp)
huffman@29451
   138
done
huffman@29451
   139
huffman@29451
   140
lemma pCons_0_0 [simp]: "pCons 0 0 = 0"
huffman@29451
   141
by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   142
huffman@29451
   143
lemma pCons_eq_iff [simp]:
huffman@29451
   144
  "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
huffman@29451
   145
proof (safe)
huffman@29451
   146
  assume "pCons a p = pCons b q"
huffman@29451
   147
  then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
huffman@29451
   148
  then show "a = b" by simp
huffman@29451
   149
next
huffman@29451
   150
  assume "pCons a p = pCons b q"
huffman@29451
   151
  then have "\<forall>n. coeff (pCons a p) (Suc n) =
huffman@29451
   152
                 coeff (pCons b q) (Suc n)" by simp
huffman@29451
   153
  then show "p = q" by (simp add: expand_poly_eq)
huffman@29451
   154
qed
huffman@29451
   155
huffman@29451
   156
lemma pCons_eq_0_iff [simp]: "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
huffman@29451
   157
  using pCons_eq_iff [of a p 0 0] by simp
huffman@29451
   158
huffman@29451
   159
lemma Poly_Suc: "f \<in> Poly \<Longrightarrow> (\<lambda>n. f (Suc n)) \<in> Poly"
huffman@29451
   160
  unfolding Poly_def
huffman@29451
   161
  by (clarify, rule_tac x=n in exI, simp)
huffman@29451
   162
huffman@29451
   163
lemma pCons_cases [cases type: poly]:
huffman@29451
   164
  obtains (pCons) a q where "p = pCons a q"
huffman@29451
   165
proof
huffman@29451
   166
  show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
huffman@29451
   167
    by (rule poly_ext)
huffman@29451
   168
       (simp add: Abs_poly_inverse Poly_Suc coeff coeff_pCons
huffman@29451
   169
             split: nat.split)
huffman@29451
   170
qed
huffman@29451
   171
huffman@29451
   172
lemma pCons_induct [case_names 0 pCons, induct type: poly]:
huffman@29451
   173
  assumes zero: "P 0"
huffman@29451
   174
  assumes pCons: "\<And>a p. P p \<Longrightarrow> P (pCons a p)"
huffman@29451
   175
  shows "P p"
huffman@29451
   176
proof (induct p rule: measure_induct_rule [where f=degree])
huffman@29451
   177
  case (less p)
huffman@29451
   178
  obtain a q where "p = pCons a q" by (rule pCons_cases)
huffman@29451
   179
  have "P q"
huffman@29451
   180
  proof (cases "q = 0")
huffman@29451
   181
    case True
huffman@29451
   182
    then show "P q" by (simp add: zero)
huffman@29451
   183
  next
huffman@29451
   184
    case False
huffman@29451
   185
    then have "degree (pCons a q) = Suc (degree q)"
huffman@29451
   186
      by (rule degree_pCons_eq)
huffman@29451
   187
    then have "degree q < degree p"
huffman@29451
   188
      using `p = pCons a q` by simp
huffman@29451
   189
    then show "P q"
huffman@29451
   190
      by (rule less.hyps)
huffman@29451
   191
  qed
huffman@29451
   192
  then have "P (pCons a q)"
huffman@29451
   193
    by (rule pCons)
huffman@29451
   194
  then show ?case
huffman@29451
   195
    using `p = pCons a q` by simp
huffman@29451
   196
qed
huffman@29451
   197
huffman@29451
   198
huffman@29451
   199
subsection {* Monomials *}
huffman@29451
   200
huffman@29451
   201
definition
huffman@29451
   202
  monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" where
huffman@29451
   203
  "monom a m = Abs_poly (\<lambda>n. if m = n then a else 0)"
huffman@29451
   204
huffman@29451
   205
lemma coeff_monom [simp]: "coeff (monom a m) n = (if m=n then a else 0)"
huffman@29451
   206
  unfolding monom_def
huffman@29451
   207
  by (subst Abs_poly_inverse, auto simp add: Poly_def)
huffman@29451
   208
huffman@29451
   209
lemma monom_0: "monom a 0 = pCons a 0"
huffman@29451
   210
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   211
huffman@29451
   212
lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)"
huffman@29451
   213
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   214
huffman@29451
   215
lemma monom_eq_0 [simp]: "monom 0 n = 0"
huffman@29451
   216
  by (rule poly_ext) simp
huffman@29451
   217
huffman@29451
   218
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
huffman@29451
   219
  by (simp add: expand_poly_eq)
huffman@29451
   220
huffman@29451
   221
lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
huffman@29451
   222
  by (simp add: expand_poly_eq)
huffman@29451
   223
huffman@29451
   224
lemma degree_monom_le: "degree (monom a n) \<le> n"
huffman@29451
   225
  by (rule degree_le, simp)
huffman@29451
   226
huffman@29451
   227
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
huffman@29451
   228
  apply (rule order_antisym [OF degree_monom_le])
huffman@29451
   229
  apply (rule le_degree, simp)
huffman@29451
   230
  done
huffman@29451
   231
huffman@29451
   232
huffman@29451
   233
subsection {* Addition and subtraction *}
huffman@29451
   234
huffman@29451
   235
instantiation poly :: (comm_monoid_add) comm_monoid_add
huffman@29451
   236
begin
huffman@29451
   237
huffman@29451
   238
definition
huffman@29451
   239
  plus_poly_def [code del]:
huffman@29451
   240
    "p + q = Abs_poly (\<lambda>n. coeff p n + coeff q n)"
huffman@29451
   241
huffman@29451
   242
lemma Poly_add:
huffman@29451
   243
  fixes f g :: "nat \<Rightarrow> 'a"
huffman@29451
   244
  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n + g n) \<in> Poly"
huffman@29451
   245
  unfolding Poly_def
huffman@29451
   246
  apply (clarify, rename_tac m n)
huffman@29451
   247
  apply (rule_tac x="max m n" in exI, simp)
huffman@29451
   248
  done
huffman@29451
   249
huffman@29451
   250
lemma coeff_add [simp]:
huffman@29451
   251
  "coeff (p + q) n = coeff p n + coeff q n"
huffman@29451
   252
  unfolding plus_poly_def
huffman@29451
   253
  by (simp add: Abs_poly_inverse coeff Poly_add)
huffman@29451
   254
huffman@29451
   255
instance proof
huffman@29451
   256
  fix p q r :: "'a poly"
huffman@29451
   257
  show "(p + q) + r = p + (q + r)"
huffman@29451
   258
    by (simp add: expand_poly_eq add_assoc)
huffman@29451
   259
  show "p + q = q + p"
huffman@29451
   260
    by (simp add: expand_poly_eq add_commute)
huffman@29451
   261
  show "0 + p = p"
huffman@29451
   262
    by (simp add: expand_poly_eq)
huffman@29451
   263
qed
huffman@29451
   264
huffman@29451
   265
end
huffman@29451
   266
huffman@29451
   267
instantiation poly :: (ab_group_add) ab_group_add
huffman@29451
   268
begin
huffman@29451
   269
huffman@29451
   270
definition
huffman@29451
   271
  uminus_poly_def [code del]:
huffman@29451
   272
    "- p = Abs_poly (\<lambda>n. - coeff p n)"
huffman@29451
   273
huffman@29451
   274
definition
huffman@29451
   275
  minus_poly_def [code del]:
huffman@29451
   276
    "p - q = Abs_poly (\<lambda>n. coeff p n - coeff q n)"
huffman@29451
   277
huffman@29451
   278
lemma Poly_minus:
huffman@29451
   279
  fixes f :: "nat \<Rightarrow> 'a"
huffman@29451
   280
  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. - f n) \<in> Poly"
huffman@29451
   281
  unfolding Poly_def by simp
huffman@29451
   282
huffman@29451
   283
lemma Poly_diff:
huffman@29451
   284
  fixes f g :: "nat \<Rightarrow> 'a"
huffman@29451
   285
  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n - g n) \<in> Poly"
huffman@29451
   286
  unfolding diff_minus by (simp add: Poly_add Poly_minus)
huffman@29451
   287
huffman@29451
   288
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
huffman@29451
   289
  unfolding uminus_poly_def
huffman@29451
   290
  by (simp add: Abs_poly_inverse coeff Poly_minus)
huffman@29451
   291
huffman@29451
   292
lemma coeff_diff [simp]:
huffman@29451
   293
  "coeff (p - q) n = coeff p n - coeff q n"
huffman@29451
   294
  unfolding minus_poly_def
huffman@29451
   295
  by (simp add: Abs_poly_inverse coeff Poly_diff)
huffman@29451
   296
huffman@29451
   297
instance proof
huffman@29451
   298
  fix p q :: "'a poly"
huffman@29451
   299
  show "- p + p = 0"
huffman@29451
   300
    by (simp add: expand_poly_eq)
huffman@29451
   301
  show "p - q = p + - q"
huffman@29451
   302
    by (simp add: expand_poly_eq diff_minus)
huffman@29451
   303
qed
huffman@29451
   304
huffman@29451
   305
end
huffman@29451
   306
huffman@29451
   307
lemma add_pCons [simp]:
huffman@29451
   308
  "pCons a p + pCons b q = pCons (a + b) (p + q)"
huffman@29451
   309
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   310
huffman@29451
   311
lemma minus_pCons [simp]:
huffman@29451
   312
  "- pCons a p = pCons (- a) (- p)"
huffman@29451
   313
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   314
huffman@29451
   315
lemma diff_pCons [simp]:
huffman@29451
   316
  "pCons a p - pCons b q = pCons (a - b) (p - q)"
huffman@29451
   317
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   318
huffman@29451
   319
lemma degree_add_le: "degree (p + q) \<le> max (degree p) (degree q)"
huffman@29451
   320
  by (rule degree_le, auto simp add: coeff_eq_0)
huffman@29451
   321
huffman@29453
   322
lemma degree_add_less:
huffman@29453
   323
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"
huffman@29453
   324
  by (auto intro: le_less_trans degree_add_le)
huffman@29453
   325
huffman@29451
   326
lemma degree_add_eq_right:
huffman@29451
   327
  "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
huffman@29451
   328
  apply (cases "q = 0", simp)
huffman@29451
   329
  apply (rule order_antisym)
huffman@29451
   330
  apply (rule ord_le_eq_trans [OF degree_add_le])
huffman@29451
   331
  apply simp
huffman@29451
   332
  apply (rule le_degree)
huffman@29451
   333
  apply (simp add: coeff_eq_0)
huffman@29451
   334
  done
huffman@29451
   335
huffman@29451
   336
lemma degree_add_eq_left:
huffman@29451
   337
  "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
huffman@29451
   338
  using degree_add_eq_right [of q p]
huffman@29451
   339
  by (simp add: add_commute)
huffman@29451
   340
huffman@29451
   341
lemma degree_minus [simp]: "degree (- p) = degree p"
huffman@29451
   342
  unfolding degree_def by simp
huffman@29451
   343
huffman@29451
   344
lemma degree_diff_le: "degree (p - q) \<le> max (degree p) (degree q)"
huffman@29451
   345
  using degree_add_le [where p=p and q="-q"]
huffman@29451
   346
  by (simp add: diff_minus)
huffman@29451
   347
huffman@29453
   348
lemma degree_diff_less:
huffman@29453
   349
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p - q) < n"
huffman@29453
   350
  by (auto intro: le_less_trans degree_diff_le)
huffman@29453
   351
huffman@29451
   352
lemma add_monom: "monom a n + monom b n = monom (a + b) n"
huffman@29451
   353
  by (rule poly_ext) simp
huffman@29451
   354
huffman@29451
   355
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
huffman@29451
   356
  by (rule poly_ext) simp
huffman@29451
   357
huffman@29451
   358
lemma minus_monom: "- monom a n = monom (-a) n"
huffman@29451
   359
  by (rule poly_ext) simp
huffman@29451
   360
huffman@29451
   361
lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"
huffman@29451
   362
  by (cases "finite A", induct set: finite, simp_all)
huffman@29451
   363
huffman@29451
   364
lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"
huffman@29451
   365
  by (rule poly_ext) (simp add: coeff_setsum)
huffman@29451
   366
huffman@29451
   367
huffman@29451
   368
subsection {* Multiplication by a constant *}
huffman@29451
   369
huffman@29451
   370
definition
huffman@29451
   371
  smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
huffman@29451
   372
  "smult a p = Abs_poly (\<lambda>n. a * coeff p n)"
huffman@29451
   373
huffman@29451
   374
lemma Poly_smult:
huffman@29451
   375
  fixes f :: "nat \<Rightarrow> 'a::comm_semiring_0"
huffman@29451
   376
  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. a * f n) \<in> Poly"
huffman@29451
   377
  unfolding Poly_def
huffman@29451
   378
  by (clarify, rule_tac x=n in exI, simp)
huffman@29451
   379
huffman@29451
   380
lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n"
huffman@29451
   381
  unfolding smult_def
huffman@29451
   382
  by (simp add: Abs_poly_inverse Poly_smult coeff)
huffman@29451
   383
huffman@29451
   384
lemma degree_smult_le: "degree (smult a p) \<le> degree p"
huffman@29451
   385
  by (rule degree_le, simp add: coeff_eq_0)
huffman@29451
   386
huffman@29451
   387
lemma smult_smult: "smult a (smult b p) = smult (a * b) p"
huffman@29451
   388
  by (rule poly_ext, simp add: mult_assoc)
huffman@29451
   389
huffman@29451
   390
lemma smult_0_right [simp]: "smult a 0 = 0"
huffman@29451
   391
  by (rule poly_ext, simp)
huffman@29451
   392
huffman@29451
   393
lemma smult_0_left [simp]: "smult 0 p = 0"
huffman@29451
   394
  by (rule poly_ext, simp)
huffman@29451
   395
huffman@29451
   396
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
huffman@29451
   397
  by (rule poly_ext, simp)
huffman@29451
   398
huffman@29451
   399
lemma smult_add_right:
huffman@29451
   400
  "smult a (p + q) = smult a p + smult a q"
huffman@29451
   401
  by (rule poly_ext, simp add: ring_simps)
huffman@29451
   402
huffman@29451
   403
lemma smult_add_left:
huffman@29451
   404
  "smult (a + b) p = smult a p + smult b p"
huffman@29451
   405
  by (rule poly_ext, simp add: ring_simps)
huffman@29451
   406
huffman@29451
   407
lemma smult_minus_right:
huffman@29451
   408
  "smult (a::'a::comm_ring) (- p) = - smult a p"
huffman@29451
   409
  by (rule poly_ext, simp)
huffman@29451
   410
huffman@29451
   411
lemma smult_minus_left:
huffman@29451
   412
  "smult (- a::'a::comm_ring) p = - smult a p"
huffman@29451
   413
  by (rule poly_ext, simp)
huffman@29451
   414
huffman@29451
   415
lemma smult_diff_right:
huffman@29451
   416
  "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
huffman@29451
   417
  by (rule poly_ext, simp add: ring_simps)
huffman@29451
   418
huffman@29451
   419
lemma smult_diff_left:
huffman@29451
   420
  "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
huffman@29451
   421
  by (rule poly_ext, simp add: ring_simps)
huffman@29451
   422
huffman@29451
   423
lemma smult_pCons [simp]:
huffman@29451
   424
  "smult a (pCons b p) = pCons (a * b) (smult a p)"
huffman@29451
   425
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   426
huffman@29451
   427
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
huffman@29451
   428
  by (induct n, simp add: monom_0, simp add: monom_Suc)
huffman@29451
   429
huffman@29451
   430
huffman@29451
   431
subsection {* Multiplication of polynomials *}
huffman@29451
   432
huffman@29451
   433
lemma Poly_mult_lemma:
huffman@29451
   434
  fixes f g :: "nat \<Rightarrow> 'a::comm_semiring_0" and m n :: nat
huffman@29451
   435
  assumes "\<forall>i>m. f i = 0"
huffman@29451
   436
  assumes "\<forall>j>n. g j = 0"
huffman@29451
   437
  shows "\<forall>k>m+n. (\<Sum>i\<le>k. f i * g (k-i)) = 0"
huffman@29451
   438
proof (clarify)
huffman@29451
   439
  fix k :: nat
huffman@29451
   440
  assume "m + n < k"
huffman@29451
   441
  show "(\<Sum>i\<le>k. f i * g (k - i)) = 0"
huffman@29451
   442
  proof (rule setsum_0' [rule_format])
huffman@29451
   443
    fix i :: nat
huffman@29451
   444
    assume "i \<in> {..k}" hence "i \<le> k" by simp
huffman@29451
   445
    with `m + n < k` have "m < i \<or> n < k - i" by arith
huffman@29451
   446
    thus "f i * g (k - i) = 0"
huffman@29451
   447
      using prems by auto
huffman@29451
   448
  qed
huffman@29451
   449
qed
huffman@29451
   450
huffman@29451
   451
lemma Poly_mult:
huffman@29451
   452
  fixes f g :: "nat \<Rightarrow> 'a::comm_semiring_0"
huffman@29451
   453
  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. \<Sum>i\<le>n. f i * g (n-i)) \<in> Poly"
huffman@29451
   454
  unfolding Poly_def
huffman@29451
   455
  by (safe, rule exI, rule Poly_mult_lemma)
huffman@29451
   456
huffman@29451
   457
lemma poly_mult_assoc_lemma:
huffman@29451
   458
  fixes k :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
huffman@29451
   459
  shows "(\<Sum>j\<le>k. \<Sum>i\<le>j. f i (j - i) (n - j)) =
huffman@29451
   460
         (\<Sum>j\<le>k. \<Sum>i\<le>k - j. f j i (n - j - i))"
huffman@29451
   461
proof (induct k)
huffman@29451
   462
  case 0 show ?case by simp
huffman@29451
   463
next
huffman@29451
   464
  case (Suc k) thus ?case
huffman@29451
   465
    by (simp add: Suc_diff_le setsum_addf add_assoc
huffman@29451
   466
             cong: strong_setsum_cong)
huffman@29451
   467
qed
huffman@29451
   468
huffman@29451
   469
lemma poly_mult_commute_lemma:
huffman@29451
   470
  fixes n :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
huffman@29451
   471
  shows "(\<Sum>i\<le>n. f i (n - i)) = (\<Sum>i\<le>n. f (n - i) i)"
huffman@29451
   472
proof (rule setsum_reindex_cong)
huffman@29451
   473
  show "inj_on (\<lambda>i. n - i) {..n}"
huffman@29451
   474
    by (rule inj_onI) simp
huffman@29451
   475
  show "{..n} = (\<lambda>i. n - i) ` {..n}"
huffman@29451
   476
    by (auto, rule_tac x="n - x" in image_eqI, simp_all)
huffman@29451
   477
next
huffman@29451
   478
  fix i assume "i \<in> {..n}"
huffman@29451
   479
  hence "n - (n - i) = i" by simp
huffman@29451
   480
  thus "f (n - i) i = f (n - i) (n - (n - i))" by simp
huffman@29451
   481
qed
huffman@29451
   482
huffman@29451
   483
text {* TODO: move to appropriate theory *}
huffman@29451
   484
lemma setsum_atMost_Suc_shift:
huffman@29451
   485
  fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"
huffman@29451
   486
  shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
huffman@29451
   487
proof (induct n)
huffman@29451
   488
  case 0 show ?case by simp
huffman@29451
   489
next
huffman@29451
   490
  case (Suc n) note IH = this
huffman@29451
   491
  have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))"
huffman@29451
   492
    by (rule setsum_atMost_Suc)
huffman@29451
   493
  also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
huffman@29451
   494
    by (rule IH)
huffman@29451
   495
  also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =
huffman@29451
   496
             f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"
huffman@29451
   497
    by (rule add_assoc)
huffman@29451
   498
  also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))"
huffman@29451
   499
    by (rule setsum_atMost_Suc [symmetric])
huffman@29451
   500
  finally show ?case .
huffman@29451
   501
qed
huffman@29451
   502
huffman@29451
   503
instantiation poly :: (comm_semiring_0) comm_semiring_0
huffman@29451
   504
begin
huffman@29451
   505
huffman@29451
   506
definition
huffman@29451
   507
  times_poly_def:
huffman@29451
   508
    "p * q = Abs_poly (\<lambda>n. \<Sum>i\<le>n. coeff p i * coeff q (n-i))"
huffman@29451
   509
huffman@29451
   510
lemma coeff_mult:
huffman@29451
   511
  "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
huffman@29451
   512
  unfolding times_poly_def
huffman@29451
   513
  by (simp add: Abs_poly_inverse coeff Poly_mult)
huffman@29451
   514
huffman@29451
   515
instance proof
huffman@29451
   516
  fix p q r :: "'a poly"
huffman@29451
   517
  show 0: "0 * p = 0"
huffman@29451
   518
    by (simp add: expand_poly_eq coeff_mult)
huffman@29451
   519
  show "p * 0 = 0"
huffman@29451
   520
    by (simp add: expand_poly_eq coeff_mult)
huffman@29451
   521
  show "(p + q) * r = p * r + q * r"
huffman@29451
   522
    by (simp add: expand_poly_eq coeff_mult left_distrib setsum_addf)
huffman@29451
   523
  show "(p * q) * r = p * (q * r)"
huffman@29451
   524
  proof (rule poly_ext)
huffman@29451
   525
    fix n :: nat
huffman@29451
   526
    have "(\<Sum>j\<le>n. \<Sum>i\<le>j. coeff p i * coeff q (j - i) * coeff r (n - j)) =
huffman@29451
   527
          (\<Sum>j\<le>n. \<Sum>i\<le>n - j. coeff p j * coeff q i * coeff r (n - j - i))"
huffman@29451
   528
      by (rule poly_mult_assoc_lemma)
huffman@29451
   529
    thus "coeff ((p * q) * r) n = coeff (p * (q * r)) n"
huffman@29451
   530
      by (simp add: coeff_mult setsum_right_distrib
huffman@29451
   531
                    setsum_left_distrib mult_assoc)
huffman@29451
   532
  qed
huffman@29451
   533
  show "p * q = q * p"
huffman@29451
   534
  proof (rule poly_ext)
huffman@29451
   535
    fix n :: nat
huffman@29451
   536
    have "(\<Sum>i\<le>n. coeff p i * coeff q (n - i)) =
huffman@29451
   537
          (\<Sum>i\<le>n. coeff p (n - i) * coeff q i)"
huffman@29451
   538
      by (rule poly_mult_commute_lemma)
huffman@29451
   539
    thus "coeff (p * q) n = coeff (q * p) n"
huffman@29451
   540
      by (simp add: coeff_mult mult_commute)
huffman@29451
   541
  qed
huffman@29451
   542
qed
huffman@29451
   543
huffman@29451
   544
end
huffman@29451
   545
huffman@29451
   546
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
huffman@29451
   547
apply (rule degree_le, simp add: coeff_mult)
huffman@29451
   548
apply (rule Poly_mult_lemma)
huffman@29451
   549
apply (simp_all add: coeff_eq_0)
huffman@29451
   550
done
huffman@29451
   551
huffman@29451
   552
lemma mult_pCons_left [simp]:
huffman@29451
   553
  "pCons a p * q = smult a q + pCons 0 (p * q)"
huffman@29451
   554
apply (rule poly_ext)
huffman@29451
   555
apply (case_tac n)
huffman@29451
   556
apply (simp add: coeff_mult)
huffman@29451
   557
apply (simp add: coeff_mult setsum_atMost_Suc_shift
huffman@29451
   558
            del: setsum_atMost_Suc)
huffman@29451
   559
done
huffman@29451
   560
huffman@29451
   561
lemma mult_pCons_right [simp]:
huffman@29451
   562
  "p * pCons a q = smult a p + pCons 0 (p * q)"
huffman@29451
   563
  using mult_pCons_left [of a q p] by (simp add: mult_commute)
huffman@29451
   564
huffman@29451
   565
lemma mult_smult_left: "smult a p * q = smult a (p * q)"
huffman@29451
   566
  by (induct p, simp, simp add: smult_add_right smult_smult)
huffman@29451
   567
huffman@29451
   568
lemma mult_smult_right: "p * smult a q = smult a (p * q)"
huffman@29451
   569
  using mult_smult_left [of a q p] by (simp add: mult_commute)
huffman@29451
   570
huffman@29451
   571
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
huffman@29451
   572
  by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)
huffman@29451
   573
huffman@29451
   574
huffman@29451
   575
subsection {* The unit polynomial and exponentiation *}
huffman@29451
   576
huffman@29451
   577
instantiation poly :: (comm_semiring_1) comm_semiring_1
huffman@29451
   578
begin
huffman@29451
   579
huffman@29451
   580
definition
huffman@29451
   581
  one_poly_def:
huffman@29451
   582
    "1 = pCons 1 0"
huffman@29451
   583
huffman@29451
   584
instance proof
huffman@29451
   585
  fix p :: "'a poly" show "1 * p = p"
huffman@29451
   586
    unfolding one_poly_def
huffman@29451
   587
    by simp
huffman@29451
   588
next
huffman@29451
   589
  show "0 \<noteq> (1::'a poly)"
huffman@29451
   590
    unfolding one_poly_def by simp
huffman@29451
   591
qed
huffman@29451
   592
huffman@29451
   593
end
huffman@29451
   594
huffman@29451
   595
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
huffman@29451
   596
  unfolding one_poly_def
huffman@29451
   597
  by (simp add: coeff_pCons split: nat.split)
huffman@29451
   598
huffman@29451
   599
lemma degree_1 [simp]: "degree 1 = 0"
huffman@29451
   600
  unfolding one_poly_def
huffman@29451
   601
  by (rule degree_pCons_0)
huffman@29451
   602
huffman@29451
   603
instantiation poly :: (comm_semiring_1) recpower
huffman@29451
   604
begin
huffman@29451
   605
huffman@29451
   606
primrec power_poly where
huffman@29451
   607
  power_poly_0: "(p::'a poly) ^ 0 = 1"
huffman@29451
   608
| power_poly_Suc: "(p::'a poly) ^ (Suc n) = p * p ^ n"
huffman@29451
   609
huffman@29451
   610
instance
huffman@29451
   611
  by default simp_all
huffman@29451
   612
huffman@29451
   613
end
huffman@29451
   614
huffman@29451
   615
instance poly :: (comm_ring) comm_ring ..
huffman@29451
   616
huffman@29451
   617
instance poly :: (comm_ring_1) comm_ring_1 ..
huffman@29451
   618
huffman@29451
   619
instantiation poly :: (comm_ring_1) number_ring
huffman@29451
   620
begin
huffman@29451
   621
huffman@29451
   622
definition
huffman@29451
   623
  "number_of k = (of_int k :: 'a poly)"
huffman@29451
   624
huffman@29451
   625
instance
huffman@29451
   626
  by default (rule number_of_poly_def)
huffman@29451
   627
huffman@29451
   628
end
huffman@29451
   629
huffman@29451
   630
huffman@29451
   631
subsection {* Polynomials form an integral domain *}
huffman@29451
   632
huffman@29451
   633
lemma coeff_mult_degree_sum:
huffman@29451
   634
  "coeff (p * q) (degree p + degree q) =
huffman@29451
   635
   coeff p (degree p) * coeff q (degree q)"
huffman@29451
   636
 apply (simp add: coeff_mult)
huffman@29451
   637
 apply (subst setsum_diff1' [where a="degree p"])
huffman@29451
   638
   apply simp
huffman@29451
   639
  apply simp
huffman@29451
   640
 apply (subst setsum_0' [rule_format])
huffman@29451
   641
  apply clarsimp
huffman@29451
   642
  apply (subgoal_tac "degree p < a \<or> degree q < degree p + degree q - a")
huffman@29451
   643
   apply (force simp add: coeff_eq_0)
huffman@29451
   644
  apply arith
huffman@29451
   645
 apply simp
huffman@29451
   646
done
huffman@29451
   647
huffman@29451
   648
instance poly :: (idom) idom
huffman@29451
   649
proof
huffman@29451
   650
  fix p q :: "'a poly"
huffman@29451
   651
  assume "p \<noteq> 0" and "q \<noteq> 0"
huffman@29451
   652
  have "coeff (p * q) (degree p + degree q) =
huffman@29451
   653
        coeff p (degree p) * coeff q (degree q)"
huffman@29451
   654
    by (rule coeff_mult_degree_sum)
huffman@29451
   655
  also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
huffman@29451
   656
    using `p \<noteq> 0` and `q \<noteq> 0` by simp
huffman@29451
   657
  finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
huffman@29451
   658
  thus "p * q \<noteq> 0" by (simp add: expand_poly_eq)
huffman@29451
   659
qed
huffman@29451
   660
huffman@29451
   661
lemma degree_mult_eq:
huffman@29451
   662
  fixes p q :: "'a::idom poly"
huffman@29451
   663
  shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q"
huffman@29451
   664
apply (rule order_antisym [OF degree_mult_le le_degree])
huffman@29451
   665
apply (simp add: coeff_mult_degree_sum)
huffman@29451
   666
done
huffman@29451
   667
huffman@29451
   668
lemma dvd_imp_degree_le:
huffman@29451
   669
  fixes p q :: "'a::idom poly"
huffman@29451
   670
  shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
huffman@29451
   671
  by (erule dvdE, simp add: degree_mult_eq)
huffman@29451
   672
huffman@29451
   673
huffman@29451
   674
subsection {* Long division of polynomials *}
huffman@29451
   675
huffman@29451
   676
definition
huffman@29451
   677
  divmod_poly_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
huffman@29451
   678
where
huffman@29451
   679
  "divmod_poly_rel x y q r \<longleftrightarrow>
huffman@29451
   680
    x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"
huffman@29451
   681
huffman@29451
   682
lemma divmod_poly_rel_0:
huffman@29451
   683
  "divmod_poly_rel 0 y 0 0"
huffman@29451
   684
  unfolding divmod_poly_rel_def by simp
huffman@29451
   685
huffman@29451
   686
lemma divmod_poly_rel_by_0:
huffman@29451
   687
  "divmod_poly_rel x 0 0 x"
huffman@29451
   688
  unfolding divmod_poly_rel_def by simp
huffman@29451
   689
huffman@29451
   690
lemma eq_zero_or_degree_less:
huffman@29451
   691
  assumes "degree p \<le> n" and "coeff p n = 0"
huffman@29451
   692
  shows "p = 0 \<or> degree p < n"
huffman@29451
   693
proof (cases n)
huffman@29451
   694
  case 0
huffman@29451
   695
  with `degree p \<le> n` and `coeff p n = 0`
huffman@29451
   696
  have "coeff p (degree p) = 0" by simp
huffman@29451
   697
  then have "p = 0" by simp
huffman@29451
   698
  then show ?thesis ..
huffman@29451
   699
next
huffman@29451
   700
  case (Suc m)
huffman@29451
   701
  have "\<forall>i>n. coeff p i = 0"
huffman@29451
   702
    using `degree p \<le> n` by (simp add: coeff_eq_0)
huffman@29451
   703
  then have "\<forall>i\<ge>n. coeff p i = 0"
huffman@29451
   704
    using `coeff p n = 0` by (simp add: le_less)
huffman@29451
   705
  then have "\<forall>i>m. coeff p i = 0"
huffman@29451
   706
    using `n = Suc m` by (simp add: less_eq_Suc_le)
huffman@29451
   707
  then have "degree p \<le> m"
huffman@29451
   708
    by (rule degree_le)
huffman@29451
   709
  then have "degree p < n"
huffman@29451
   710
    using `n = Suc m` by (simp add: less_Suc_eq_le)
huffman@29451
   711
  then show ?thesis ..
huffman@29451
   712
qed
huffman@29451
   713
huffman@29451
   714
lemma divmod_poly_rel_pCons:
huffman@29451
   715
  assumes rel: "divmod_poly_rel x y q r"
huffman@29451
   716
  assumes y: "y \<noteq> 0"
huffman@29451
   717
  assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
huffman@29451
   718
  shows "divmod_poly_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
huffman@29451
   719
    (is "divmod_poly_rel ?x y ?q ?r")
huffman@29451
   720
proof -
huffman@29451
   721
  have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
huffman@29451
   722
    using assms unfolding divmod_poly_rel_def by simp_all
huffman@29451
   723
huffman@29451
   724
  have 1: "?x = ?q * y + ?r"
huffman@29451
   725
    using b x by simp
huffman@29451
   726
huffman@29451
   727
  have 2: "?r = 0 \<or> degree ?r < degree y"
huffman@29451
   728
  proof (rule eq_zero_or_degree_less)
huffman@29451
   729
    have "degree ?r \<le> max (degree (pCons a r)) (degree (smult b y))"
huffman@29451
   730
      by (rule degree_diff_le)
huffman@29451
   731
    also have "\<dots> \<le> degree y"
huffman@29451
   732
    proof (rule min_max.le_supI)
huffman@29451
   733
      show "degree (pCons a r) \<le> degree y"
huffman@29451
   734
        using r by (auto simp add: degree_pCons_eq_if)
huffman@29451
   735
      show "degree (smult b y) \<le> degree y"
huffman@29451
   736
        by (rule degree_smult_le)
huffman@29451
   737
    qed
huffman@29451
   738
    finally show "degree ?r \<le> degree y" .
huffman@29451
   739
  next
huffman@29451
   740
    show "coeff ?r (degree y) = 0"
huffman@29451
   741
      using `y \<noteq> 0` unfolding b by simp
huffman@29451
   742
  qed
huffman@29451
   743
huffman@29451
   744
  from 1 2 show ?thesis
huffman@29451
   745
    unfolding divmod_poly_rel_def
huffman@29451
   746
    using `y \<noteq> 0` by simp
huffman@29451
   747
qed
huffman@29451
   748
huffman@29451
   749
lemma divmod_poly_rel_exists: "\<exists>q r. divmod_poly_rel x y q r"
huffman@29451
   750
apply (cases "y = 0")
huffman@29451
   751
apply (fast intro!: divmod_poly_rel_by_0)
huffman@29451
   752
apply (induct x)
huffman@29451
   753
apply (fast intro!: divmod_poly_rel_0)
huffman@29451
   754
apply (fast intro!: divmod_poly_rel_pCons)
huffman@29451
   755
done
huffman@29451
   756
huffman@29451
   757
lemma divmod_poly_rel_unique:
huffman@29451
   758
  assumes 1: "divmod_poly_rel x y q1 r1"
huffman@29451
   759
  assumes 2: "divmod_poly_rel x y q2 r2"
huffman@29451
   760
  shows "q1 = q2 \<and> r1 = r2"
huffman@29451
   761
proof (cases "y = 0")
huffman@29451
   762
  assume "y = 0" with assms show ?thesis
huffman@29451
   763
    by (simp add: divmod_poly_rel_def)
huffman@29451
   764
next
huffman@29451
   765
  assume [simp]: "y \<noteq> 0"
huffman@29451
   766
  from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
huffman@29451
   767
    unfolding divmod_poly_rel_def by simp_all
huffman@29451
   768
  from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
huffman@29451
   769
    unfolding divmod_poly_rel_def by simp_all
huffman@29451
   770
  from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
huffman@29451
   771
    by (simp add: ring_simps)
huffman@29451
   772
  from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
huffman@29453
   773
    by (auto intro: degree_diff_less)
huffman@29451
   774
huffman@29451
   775
  show "q1 = q2 \<and> r1 = r2"
huffman@29451
   776
  proof (rule ccontr)
huffman@29451
   777
    assume "\<not> (q1 = q2 \<and> r1 = r2)"
huffman@29451
   778
    with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
huffman@29451
   779
    with r3 have "degree (r2 - r1) < degree y" by simp
huffman@29451
   780
    also have "degree y \<le> degree (q1 - q2) + degree y" by simp
huffman@29451
   781
    also have "\<dots> = degree ((q1 - q2) * y)"
huffman@29451
   782
      using `q1 \<noteq> q2` by (simp add: degree_mult_eq)
huffman@29451
   783
    also have "\<dots> = degree (r2 - r1)"
huffman@29451
   784
      using q3 by simp
huffman@29451
   785
    finally have "degree (r2 - r1) < degree (r2 - r1)" .
huffman@29451
   786
    then show "False" by simp
huffman@29451
   787
  qed
huffman@29451
   788
qed
huffman@29451
   789
huffman@29451
   790
lemmas divmod_poly_rel_unique_div =
huffman@29451
   791
  divmod_poly_rel_unique [THEN conjunct1, standard]
huffman@29451
   792
huffman@29451
   793
lemmas divmod_poly_rel_unique_mod =
huffman@29451
   794
  divmod_poly_rel_unique [THEN conjunct2, standard]
huffman@29451
   795
huffman@29451
   796
instantiation poly :: (field) ring_div
huffman@29451
   797
begin
huffman@29451
   798
huffman@29451
   799
definition div_poly where
huffman@29451
   800
  [code del]: "x div y = (THE q. \<exists>r. divmod_poly_rel x y q r)"
huffman@29451
   801
huffman@29451
   802
definition mod_poly where
huffman@29451
   803
  [code del]: "x mod y = (THE r. \<exists>q. divmod_poly_rel x y q r)"
huffman@29451
   804
huffman@29451
   805
lemma div_poly_eq:
huffman@29451
   806
  "divmod_poly_rel x y q r \<Longrightarrow> x div y = q"
huffman@29451
   807
unfolding div_poly_def
huffman@29451
   808
by (fast elim: divmod_poly_rel_unique_div)
huffman@29451
   809
huffman@29451
   810
lemma mod_poly_eq:
huffman@29451
   811
  "divmod_poly_rel x y q r \<Longrightarrow> x mod y = r"
huffman@29451
   812
unfolding mod_poly_def
huffman@29451
   813
by (fast elim: divmod_poly_rel_unique_mod)
huffman@29451
   814
huffman@29451
   815
lemma divmod_poly_rel:
huffman@29451
   816
  "divmod_poly_rel x y (x div y) (x mod y)"
huffman@29451
   817
proof -
huffman@29451
   818
  from divmod_poly_rel_exists
huffman@29451
   819
    obtain q r where "divmod_poly_rel x y q r" by fast
huffman@29451
   820
  thus ?thesis
huffman@29451
   821
    by (simp add: div_poly_eq mod_poly_eq)
huffman@29451
   822
qed
huffman@29451
   823
huffman@29451
   824
instance proof
huffman@29451
   825
  fix x y :: "'a poly"
huffman@29451
   826
  show "x div y * y + x mod y = x"
huffman@29451
   827
    using divmod_poly_rel [of x y]
huffman@29451
   828
    by (simp add: divmod_poly_rel_def)
huffman@29451
   829
next
huffman@29451
   830
  fix x :: "'a poly"
huffman@29451
   831
  have "divmod_poly_rel x 0 0 x"
huffman@29451
   832
    by (rule divmod_poly_rel_by_0)
huffman@29451
   833
  thus "x div 0 = 0"
huffman@29451
   834
    by (rule div_poly_eq)
huffman@29451
   835
next
huffman@29451
   836
  fix y :: "'a poly"
huffman@29451
   837
  have "divmod_poly_rel 0 y 0 0"
huffman@29451
   838
    by (rule divmod_poly_rel_0)
huffman@29451
   839
  thus "0 div y = 0"
huffman@29451
   840
    by (rule div_poly_eq)
huffman@29451
   841
next
huffman@29451
   842
  fix x y z :: "'a poly"
huffman@29451
   843
  assume "y \<noteq> 0"
huffman@29451
   844
  hence "divmod_poly_rel (x + z * y) y (z + x div y) (x mod y)"
huffman@29451
   845
    using divmod_poly_rel [of x y]
huffman@29451
   846
    by (simp add: divmod_poly_rel_def left_distrib)
huffman@29451
   847
  thus "(x + z * y) div y = z + x div y"
huffman@29451
   848
    by (rule div_poly_eq)
huffman@29451
   849
qed
huffman@29451
   850
huffman@29451
   851
end
huffman@29451
   852
huffman@29451
   853
lemma degree_mod_less:
huffman@29451
   854
  "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
huffman@29451
   855
  using divmod_poly_rel [of x y]
huffman@29451
   856
  unfolding divmod_poly_rel_def by simp
huffman@29451
   857
huffman@29451
   858
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
huffman@29451
   859
proof -
huffman@29451
   860
  assume "degree x < degree y"
huffman@29451
   861
  hence "divmod_poly_rel x y 0 x"
huffman@29451
   862
    by (simp add: divmod_poly_rel_def)
huffman@29451
   863
  thus "x div y = 0" by (rule div_poly_eq)
huffman@29451
   864
qed
huffman@29451
   865
huffman@29451
   866
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
huffman@29451
   867
proof -
huffman@29451
   868
  assume "degree x < degree y"
huffman@29451
   869
  hence "divmod_poly_rel x y 0 x"
huffman@29451
   870
    by (simp add: divmod_poly_rel_def)
huffman@29451
   871
  thus "x mod y = x" by (rule mod_poly_eq)
huffman@29451
   872
qed
huffman@29451
   873
huffman@29451
   874
lemma mod_pCons:
huffman@29451
   875
  fixes a and x
huffman@29451
   876
  assumes y: "y \<noteq> 0"
huffman@29451
   877
  defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"
huffman@29451
   878
  shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
huffman@29451
   879
unfolding b
huffman@29451
   880
apply (rule mod_poly_eq)
huffman@29451
   881
apply (rule divmod_poly_rel_pCons [OF divmod_poly_rel y refl])
huffman@29451
   882
done
huffman@29451
   883
huffman@29451
   884
huffman@29451
   885
subsection {* Evaluation of polynomials *}
huffman@29451
   886
huffman@29451
   887
definition
huffman@29451
   888
  poly :: "'a::{comm_semiring_1,recpower} poly \<Rightarrow> 'a \<Rightarrow> 'a" where
huffman@29451
   889
  "poly p = (\<lambda>x. \<Sum>n\<le>degree p. coeff p n * x ^ n)"
huffman@29451
   890
huffman@29451
   891
lemma poly_0 [simp]: "poly 0 x = 0"
huffman@29451
   892
  unfolding poly_def by simp
huffman@29451
   893
huffman@29451
   894
lemma poly_pCons [simp]: "poly (pCons a p) x = a + x * poly p x"
huffman@29451
   895
  unfolding poly_def
huffman@29451
   896
  by (simp add: degree_pCons_eq_if setsum_atMost_Suc_shift power_Suc
huffman@29451
   897
                setsum_left_distrib setsum_right_distrib mult_ac
huffman@29451
   898
           del: setsum_atMost_Suc)
huffman@29451
   899
huffman@29451
   900
lemma poly_1 [simp]: "poly 1 x = 1"
huffman@29451
   901
  unfolding one_poly_def by simp
huffman@29451
   902
huffman@29451
   903
lemma poly_monom: "poly (monom a n) x = a * x ^ n"
huffman@29451
   904
  by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac)
huffman@29451
   905
huffman@29451
   906
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
huffman@29451
   907
  apply (induct p arbitrary: q, simp)
huffman@29451
   908
  apply (case_tac q, simp, simp add: ring_simps)
huffman@29451
   909
  done
huffman@29451
   910
huffman@29451
   911
lemma poly_minus [simp]:
huffman@29451
   912
  fixes x :: "'a::{comm_ring_1,recpower}"
huffman@29451
   913
  shows "poly (- p) x = - poly p x"
huffman@29451
   914
  by (induct p, simp_all)
huffman@29451
   915
huffman@29451
   916
lemma poly_diff [simp]:
huffman@29451
   917
  fixes x :: "'a::{comm_ring_1,recpower}"
huffman@29451
   918
  shows "poly (p - q) x = poly p x - poly q x"
huffman@29451
   919
  by (simp add: diff_minus)
huffman@29451
   920
huffman@29451
   921
lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"
huffman@29451
   922
  by (cases "finite A", induct set: finite, simp_all)
huffman@29451
   923
huffman@29451
   924
lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x"
huffman@29451
   925
  by (induct p, simp, simp add: ring_simps)
huffman@29451
   926
huffman@29451
   927
lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x"
huffman@29451
   928
  by (induct p, simp_all, simp add: ring_simps)
huffman@29451
   929
huffman@29451
   930
end