src/HOL/Library/Polynomial.thy
author haftmann
Tue Feb 19 19:44:10 2013 +0100 (2013-02-19)
changeset 51188 9b5bf1a9a710
parent 49962 a8cc904a6820
child 52380 3cc46b8cca5e
permissions -rw-r--r--
dropped spurious left-over from 0a2371e7ced3
wenzelm@41959
     1
(*  Title:      HOL/Library/Polynomial.thy
huffman@29451
     2
    Author:     Brian Huffman
wenzelm@41959
     3
    Author:     Clemens Ballarin
huffman@29451
     4
*)
huffman@29451
     5
huffman@29451
     6
header {* Univariate Polynomials *}
huffman@29451
     7
huffman@29451
     8
theory Polynomial
haftmann@30738
     9
imports Main
huffman@29451
    10
begin
huffman@29451
    11
huffman@29451
    12
subsection {* Definition of type @{text poly} *}
huffman@29451
    13
wenzelm@45694
    14
definition "Poly = {f::nat \<Rightarrow> 'a::zero. \<exists>n. \<forall>i>n. f i = 0}"
wenzelm@45694
    15
wenzelm@49834
    16
typedef 'a poly = "Poly :: (nat => 'a::zero) set"
huffman@29451
    17
  morphisms coeff Abs_poly
wenzelm@45694
    18
  unfolding Poly_def by auto
huffman@29451
    19
haftmann@47002
    20
(* FIXME should be named poly_eq_iff *)
huffman@29451
    21
lemma expand_poly_eq: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)"
wenzelm@45694
    22
  by (simp add: coeff_inject [symmetric] fun_eq_iff)
huffman@29451
    23
haftmann@47002
    24
(* FIXME should be named poly_eqI *)
huffman@29451
    25
lemma poly_ext: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
wenzelm@45694
    26
  by (simp add: expand_poly_eq)
huffman@29451
    27
huffman@29451
    28
huffman@29451
    29
subsection {* Degree of a polynomial *}
huffman@29451
    30
huffman@29451
    31
definition
huffman@29451
    32
  degree :: "'a::zero poly \<Rightarrow> nat" where
huffman@29451
    33
  "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
huffman@29451
    34
huffman@29451
    35
lemma coeff_eq_0: "degree p < n \<Longrightarrow> coeff p n = 0"
huffman@29451
    36
proof -
huffman@29451
    37
  have "coeff p \<in> Poly"
huffman@29451
    38
    by (rule coeff)
huffman@29451
    39
  hence "\<exists>n. \<forall>i>n. coeff p i = 0"
huffman@29451
    40
    unfolding Poly_def by simp
huffman@29451
    41
  hence "\<forall>i>degree p. coeff p i = 0"
huffman@29451
    42
    unfolding degree_def by (rule LeastI_ex)
huffman@29451
    43
  moreover assume "degree p < n"
huffman@29451
    44
  ultimately show ?thesis by simp
huffman@29451
    45
qed
huffman@29451
    46
huffman@29451
    47
lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
huffman@29451
    48
  by (erule contrapos_np, rule coeff_eq_0, simp)
huffman@29451
    49
huffman@29451
    50
lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
huffman@29451
    51
  unfolding degree_def by (erule Least_le)
huffman@29451
    52
huffman@29451
    53
lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0"
huffman@29451
    54
  unfolding degree_def by (drule not_less_Least, simp)
huffman@29451
    55
huffman@29451
    56
huffman@29451
    57
subsection {* The zero polynomial *}
huffman@29451
    58
huffman@29451
    59
instantiation poly :: (zero) zero
huffman@29451
    60
begin
huffman@29451
    61
huffman@29451
    62
definition
huffman@29451
    63
  zero_poly_def: "0 = Abs_poly (\<lambda>n. 0)"
huffman@29451
    64
huffman@29451
    65
instance ..
huffman@29451
    66
end
huffman@29451
    67
huffman@29451
    68
lemma coeff_0 [simp]: "coeff 0 n = 0"
huffman@29451
    69
  unfolding zero_poly_def
huffman@29451
    70
  by (simp add: Abs_poly_inverse Poly_def)
huffman@29451
    71
huffman@29451
    72
lemma degree_0 [simp]: "degree 0 = 0"
huffman@29451
    73
  by (rule order_antisym [OF degree_le le0]) simp
huffman@29451
    74
huffman@29451
    75
lemma leading_coeff_neq_0:
huffman@29451
    76
  assumes "p \<noteq> 0" shows "coeff p (degree p) \<noteq> 0"
huffman@29451
    77
proof (cases "degree p")
huffman@29451
    78
  case 0
huffman@29451
    79
  from `p \<noteq> 0` have "\<exists>n. coeff p n \<noteq> 0"
huffman@29451
    80
    by (simp add: expand_poly_eq)
huffman@29451
    81
  then obtain n where "coeff p n \<noteq> 0" ..
huffman@29451
    82
  hence "n \<le> degree p" by (rule le_degree)
huffman@29451
    83
  with `coeff p n \<noteq> 0` and `degree p = 0`
huffman@29451
    84
  show "coeff p (degree p) \<noteq> 0" by simp
huffman@29451
    85
next
huffman@29451
    86
  case (Suc n)
huffman@29451
    87
  from `degree p = Suc n` have "n < degree p" by simp
huffman@29451
    88
  hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp)
huffman@29451
    89
  then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast
huffman@29451
    90
  from `degree p = Suc n` and `n < i` have "degree p \<le> i" by simp
huffman@29451
    91
  also from `coeff p i \<noteq> 0` have "i \<le> degree p" by (rule le_degree)
huffman@29451
    92
  finally have "degree p = i" .
huffman@29451
    93
  with `coeff p i \<noteq> 0` show "coeff p (degree p) \<noteq> 0" by simp
huffman@29451
    94
qed
huffman@29451
    95
huffman@29451
    96
lemma leading_coeff_0_iff [simp]: "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
huffman@29451
    97
  by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
huffman@29451
    98
huffman@29451
    99
huffman@29451
   100
subsection {* List-style constructor for polynomials *}
huffman@29451
   101
huffman@29451
   102
definition
huffman@29451
   103
  pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
huffman@29451
   104
where
haftmann@37765
   105
  "pCons a p = Abs_poly (nat_case a (coeff p))"
huffman@29451
   106
huffman@29455
   107
syntax
huffman@29455
   108
  "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
huffman@29455
   109
huffman@29455
   110
translations
huffman@29455
   111
  "[:x, xs:]" == "CONST pCons x [:xs:]"
huffman@29455
   112
  "[:x:]" == "CONST pCons x 0"
huffman@30155
   113
  "[:x:]" <= "CONST pCons x (_constrain 0 t)"
huffman@29455
   114
huffman@29451
   115
lemma Poly_nat_case: "f \<in> Poly \<Longrightarrow> nat_case a f \<in> Poly"
huffman@29451
   116
  unfolding Poly_def by (auto split: nat.split)
huffman@29451
   117
huffman@29451
   118
lemma coeff_pCons:
huffman@29451
   119
  "coeff (pCons a p) = nat_case a (coeff p)"
huffman@29451
   120
  unfolding pCons_def
huffman@29451
   121
  by (simp add: Abs_poly_inverse Poly_nat_case coeff)
huffman@29451
   122
huffman@29451
   123
lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a"
huffman@29451
   124
  by (simp add: coeff_pCons)
huffman@29451
   125
huffman@29451
   126
lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n"
huffman@29451
   127
  by (simp add: coeff_pCons)
huffman@29451
   128
huffman@29451
   129
lemma degree_pCons_le: "degree (pCons a p) \<le> Suc (degree p)"
huffman@29451
   130
by (rule degree_le, simp add: coeff_eq_0 coeff_pCons split: nat.split)
huffman@29451
   131
huffman@29451
   132
lemma degree_pCons_eq:
huffman@29451
   133
  "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
huffman@29451
   134
apply (rule order_antisym [OF degree_pCons_le])
huffman@29451
   135
apply (rule le_degree, simp)
huffman@29451
   136
done
huffman@29451
   137
huffman@29451
   138
lemma degree_pCons_0: "degree (pCons a 0) = 0"
huffman@29451
   139
apply (rule order_antisym [OF _ le0])
huffman@29451
   140
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
huffman@29451
   141
done
huffman@29451
   142
huffman@29460
   143
lemma degree_pCons_eq_if [simp]:
huffman@29451
   144
  "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
huffman@29451
   145
apply (cases "p = 0", simp_all)
huffman@29451
   146
apply (rule order_antisym [OF _ le0])
huffman@29451
   147
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
huffman@29451
   148
apply (rule order_antisym [OF degree_pCons_le])
huffman@29451
   149
apply (rule le_degree, simp)
huffman@29451
   150
done
huffman@29451
   151
haftmann@46031
   152
lemma pCons_0_0 [simp, code_post]: "pCons 0 0 = 0"
huffman@29451
   153
by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   154
huffman@29451
   155
lemma pCons_eq_iff [simp]:
huffman@29451
   156
  "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
huffman@29451
   157
proof (safe)
huffman@29451
   158
  assume "pCons a p = pCons b q"
huffman@29451
   159
  then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
huffman@29451
   160
  then show "a = b" by simp
huffman@29451
   161
next
huffman@29451
   162
  assume "pCons a p = pCons b q"
huffman@29451
   163
  then have "\<forall>n. coeff (pCons a p) (Suc n) =
huffman@29451
   164
                 coeff (pCons b q) (Suc n)" by simp
huffman@29451
   165
  then show "p = q" by (simp add: expand_poly_eq)
huffman@29451
   166
qed
huffman@29451
   167
huffman@29451
   168
lemma pCons_eq_0_iff [simp]: "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
huffman@29451
   169
  using pCons_eq_iff [of a p 0 0] by simp
huffman@29451
   170
huffman@29451
   171
lemma Poly_Suc: "f \<in> Poly \<Longrightarrow> (\<lambda>n. f (Suc n)) \<in> Poly"
huffman@29451
   172
  unfolding Poly_def
huffman@29451
   173
  by (clarify, rule_tac x=n in exI, simp)
huffman@29451
   174
huffman@29451
   175
lemma pCons_cases [cases type: poly]:
huffman@29451
   176
  obtains (pCons) a q where "p = pCons a q"
huffman@29451
   177
proof
huffman@29451
   178
  show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
huffman@29451
   179
    by (rule poly_ext)
huffman@29451
   180
       (simp add: Abs_poly_inverse Poly_Suc coeff coeff_pCons
huffman@29451
   181
             split: nat.split)
huffman@29451
   182
qed
huffman@29451
   183
huffman@29451
   184
lemma pCons_induct [case_names 0 pCons, induct type: poly]:
huffman@29451
   185
  assumes zero: "P 0"
huffman@29451
   186
  assumes pCons: "\<And>a p. P p \<Longrightarrow> P (pCons a p)"
huffman@29451
   187
  shows "P p"
huffman@29451
   188
proof (induct p rule: measure_induct_rule [where f=degree])
huffman@29451
   189
  case (less p)
huffman@29451
   190
  obtain a q where "p = pCons a q" by (rule pCons_cases)
huffman@29451
   191
  have "P q"
huffman@29451
   192
  proof (cases "q = 0")
huffman@29451
   193
    case True
huffman@29451
   194
    then show "P q" by (simp add: zero)
huffman@29451
   195
  next
huffman@29451
   196
    case False
huffman@29451
   197
    then have "degree (pCons a q) = Suc (degree q)"
huffman@29451
   198
      by (rule degree_pCons_eq)
huffman@29451
   199
    then have "degree q < degree p"
huffman@29451
   200
      using `p = pCons a q` by simp
huffman@29451
   201
    then show "P q"
huffman@29451
   202
      by (rule less.hyps)
huffman@29451
   203
  qed
huffman@29451
   204
  then have "P (pCons a q)"
huffman@29451
   205
    by (rule pCons)
huffman@29451
   206
  then show ?case
huffman@29451
   207
    using `p = pCons a q` by simp
huffman@29451
   208
qed
huffman@29451
   209
huffman@29451
   210
huffman@29454
   211
subsection {* Recursion combinator for polynomials *}
huffman@29454
   212
huffman@29454
   213
function
huffman@29454
   214
  poly_rec :: "'b \<Rightarrow> ('a::zero \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b"
huffman@29454
   215
where
haftmann@37765
   216
  poly_rec_pCons_eq_if [simp del]:
huffman@29454
   217
    "poly_rec z f (pCons a p) = f a p (if p = 0 then z else poly_rec z f p)"
huffman@29454
   218
by (case_tac x, rename_tac q, case_tac q, auto)
huffman@29454
   219
huffman@29454
   220
termination poly_rec
huffman@29454
   221
by (relation "measure (degree \<circ> snd \<circ> snd)", simp)
huffman@29454
   222
   (simp add: degree_pCons_eq)
huffman@29454
   223
huffman@29454
   224
lemma poly_rec_0:
huffman@29454
   225
  "f 0 0 z = z \<Longrightarrow> poly_rec z f 0 = z"
huffman@29454
   226
  using poly_rec_pCons_eq_if [of z f 0 0] by simp
huffman@29454
   227
huffman@29454
   228
lemma poly_rec_pCons:
huffman@29454
   229
  "f 0 0 z = z \<Longrightarrow> poly_rec z f (pCons a p) = f a p (poly_rec z f p)"
huffman@29454
   230
  by (simp add: poly_rec_pCons_eq_if poly_rec_0)
huffman@29454
   231
huffman@29454
   232
huffman@29451
   233
subsection {* Monomials *}
huffman@29451
   234
huffman@29451
   235
definition
huffman@29451
   236
  monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" where
huffman@29451
   237
  "monom a m = Abs_poly (\<lambda>n. if m = n then a else 0)"
huffman@29451
   238
huffman@29451
   239
lemma coeff_monom [simp]: "coeff (monom a m) n = (if m=n then a else 0)"
huffman@29451
   240
  unfolding monom_def
huffman@29451
   241
  by (subst Abs_poly_inverse, auto simp add: Poly_def)
huffman@29451
   242
huffman@29451
   243
lemma monom_0: "monom a 0 = pCons a 0"
huffman@29451
   244
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   245
huffman@29451
   246
lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)"
huffman@29451
   247
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   248
huffman@29451
   249
lemma monom_eq_0 [simp]: "monom 0 n = 0"
huffman@29451
   250
  by (rule poly_ext) simp
huffman@29451
   251
huffman@29451
   252
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
huffman@29451
   253
  by (simp add: expand_poly_eq)
huffman@29451
   254
huffman@29451
   255
lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
huffman@29451
   256
  by (simp add: expand_poly_eq)
huffman@29451
   257
huffman@29451
   258
lemma degree_monom_le: "degree (monom a n) \<le> n"
huffman@29451
   259
  by (rule degree_le, simp)
huffman@29451
   260
huffman@29451
   261
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
huffman@29451
   262
  apply (rule order_antisym [OF degree_monom_le])
huffman@29451
   263
  apply (rule le_degree, simp)
huffman@29451
   264
  done
huffman@29451
   265
huffman@29451
   266
huffman@29451
   267
subsection {* Addition and subtraction *}
huffman@29451
   268
huffman@29451
   269
instantiation poly :: (comm_monoid_add) comm_monoid_add
huffman@29451
   270
begin
huffman@29451
   271
huffman@29451
   272
definition
haftmann@37765
   273
  plus_poly_def:
huffman@29451
   274
    "p + q = Abs_poly (\<lambda>n. coeff p n + coeff q n)"
huffman@29451
   275
huffman@29451
   276
lemma Poly_add:
huffman@29451
   277
  fixes f g :: "nat \<Rightarrow> 'a"
huffman@29451
   278
  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n + g n) \<in> Poly"
huffman@29451
   279
  unfolding Poly_def
huffman@29451
   280
  apply (clarify, rename_tac m n)
huffman@29451
   281
  apply (rule_tac x="max m n" in exI, simp)
huffman@29451
   282
  done
huffman@29451
   283
huffman@29451
   284
lemma coeff_add [simp]:
huffman@29451
   285
  "coeff (p + q) n = coeff p n + coeff q n"
huffman@29451
   286
  unfolding plus_poly_def
huffman@29451
   287
  by (simp add: Abs_poly_inverse coeff Poly_add)
huffman@29451
   288
huffman@29451
   289
instance proof
huffman@29451
   290
  fix p q r :: "'a poly"
huffman@29451
   291
  show "(p + q) + r = p + (q + r)"
huffman@29451
   292
    by (simp add: expand_poly_eq add_assoc)
huffman@29451
   293
  show "p + q = q + p"
huffman@29451
   294
    by (simp add: expand_poly_eq add_commute)
huffman@29451
   295
  show "0 + p = p"
huffman@29451
   296
    by (simp add: expand_poly_eq)
huffman@29451
   297
qed
huffman@29451
   298
huffman@29451
   299
end
huffman@29451
   300
huffman@29904
   301
instance poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
huffman@29540
   302
proof
huffman@29540
   303
  fix p q r :: "'a poly"
huffman@29540
   304
  assume "p + q = p + r" thus "q = r"
huffman@29540
   305
    by (simp add: expand_poly_eq)
huffman@29540
   306
qed
huffman@29540
   307
huffman@29451
   308
instantiation poly :: (ab_group_add) ab_group_add
huffman@29451
   309
begin
huffman@29451
   310
huffman@29451
   311
definition
haftmann@37765
   312
  uminus_poly_def:
huffman@29451
   313
    "- p = Abs_poly (\<lambda>n. - coeff p n)"
huffman@29451
   314
huffman@29451
   315
definition
haftmann@37765
   316
  minus_poly_def:
huffman@29451
   317
    "p - q = Abs_poly (\<lambda>n. coeff p n - coeff q n)"
huffman@29451
   318
huffman@29451
   319
lemma Poly_minus:
huffman@29451
   320
  fixes f :: "nat \<Rightarrow> 'a"
huffman@29451
   321
  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. - f n) \<in> Poly"
huffman@29451
   322
  unfolding Poly_def by simp
huffman@29451
   323
huffman@29451
   324
lemma Poly_diff:
huffman@29451
   325
  fixes f g :: "nat \<Rightarrow> 'a"
huffman@29451
   326
  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n - g n) \<in> Poly"
huffman@29451
   327
  unfolding diff_minus by (simp add: Poly_add Poly_minus)
huffman@29451
   328
huffman@29451
   329
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
huffman@29451
   330
  unfolding uminus_poly_def
huffman@29451
   331
  by (simp add: Abs_poly_inverse coeff Poly_minus)
huffman@29451
   332
huffman@29451
   333
lemma coeff_diff [simp]:
huffman@29451
   334
  "coeff (p - q) n = coeff p n - coeff q n"
huffman@29451
   335
  unfolding minus_poly_def
huffman@29451
   336
  by (simp add: Abs_poly_inverse coeff Poly_diff)
huffman@29451
   337
huffman@29451
   338
instance proof
huffman@29451
   339
  fix p q :: "'a poly"
huffman@29451
   340
  show "- p + p = 0"
huffman@29451
   341
    by (simp add: expand_poly_eq)
huffman@29451
   342
  show "p - q = p + - q"
huffman@29451
   343
    by (simp add: expand_poly_eq diff_minus)
huffman@29451
   344
qed
huffman@29451
   345
huffman@29451
   346
end
huffman@29451
   347
huffman@29451
   348
lemma add_pCons [simp]:
huffman@29451
   349
  "pCons a p + pCons b q = pCons (a + b) (p + q)"
huffman@29451
   350
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   351
huffman@29451
   352
lemma minus_pCons [simp]:
huffman@29451
   353
  "- pCons a p = pCons (- a) (- p)"
huffman@29451
   354
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   355
huffman@29451
   356
lemma diff_pCons [simp]:
huffman@29451
   357
  "pCons a p - pCons b q = pCons (a - b) (p - q)"
huffman@29451
   358
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   359
huffman@29539
   360
lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"
huffman@29451
   361
  by (rule degree_le, auto simp add: coeff_eq_0)
huffman@29451
   362
huffman@29539
   363
lemma degree_add_le:
huffman@29539
   364
  "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n"
huffman@29539
   365
  by (auto intro: order_trans degree_add_le_max)
huffman@29539
   366
huffman@29453
   367
lemma degree_add_less:
huffman@29453
   368
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"
huffman@29539
   369
  by (auto intro: le_less_trans degree_add_le_max)
huffman@29453
   370
huffman@29451
   371
lemma degree_add_eq_right:
huffman@29451
   372
  "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
huffman@29451
   373
  apply (cases "q = 0", simp)
huffman@29451
   374
  apply (rule order_antisym)
huffman@29539
   375
  apply (simp add: degree_add_le)
huffman@29451
   376
  apply (rule le_degree)
huffman@29451
   377
  apply (simp add: coeff_eq_0)
huffman@29451
   378
  done
huffman@29451
   379
huffman@29451
   380
lemma degree_add_eq_left:
huffman@29451
   381
  "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
huffman@29451
   382
  using degree_add_eq_right [of q p]
huffman@29451
   383
  by (simp add: add_commute)
huffman@29451
   384
huffman@29451
   385
lemma degree_minus [simp]: "degree (- p) = degree p"
huffman@29451
   386
  unfolding degree_def by simp
huffman@29451
   387
huffman@29539
   388
lemma degree_diff_le_max: "degree (p - q) \<le> max (degree p) (degree q)"
huffman@29451
   389
  using degree_add_le [where p=p and q="-q"]
huffman@29451
   390
  by (simp add: diff_minus)
huffman@29451
   391
huffman@29539
   392
lemma degree_diff_le:
huffman@29539
   393
  "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p - q) \<le> n"
huffman@29539
   394
  by (simp add: diff_minus degree_add_le)
huffman@29539
   395
huffman@29453
   396
lemma degree_diff_less:
huffman@29453
   397
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p - q) < n"
huffman@29539
   398
  by (simp add: diff_minus degree_add_less)
huffman@29453
   399
huffman@29451
   400
lemma add_monom: "monom a n + monom b n = monom (a + b) n"
huffman@29451
   401
  by (rule poly_ext) simp
huffman@29451
   402
huffman@29451
   403
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
huffman@29451
   404
  by (rule poly_ext) simp
huffman@29451
   405
huffman@29451
   406
lemma minus_monom: "- monom a n = monom (-a) n"
huffman@29451
   407
  by (rule poly_ext) simp
huffman@29451
   408
huffman@29451
   409
lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"
huffman@29451
   410
  by (cases "finite A", induct set: finite, simp_all)
huffman@29451
   411
huffman@29451
   412
lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"
huffman@29451
   413
  by (rule poly_ext) (simp add: coeff_setsum)
huffman@29451
   414
huffman@29451
   415
huffman@29451
   416
subsection {* Multiplication by a constant *}
huffman@29451
   417
huffman@29451
   418
definition
huffman@29451
   419
  smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
huffman@29451
   420
  "smult a p = Abs_poly (\<lambda>n. a * coeff p n)"
huffman@29451
   421
huffman@29451
   422
lemma Poly_smult:
huffman@29451
   423
  fixes f :: "nat \<Rightarrow> 'a::comm_semiring_0"
huffman@29451
   424
  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. a * f n) \<in> Poly"
huffman@29451
   425
  unfolding Poly_def
huffman@29451
   426
  by (clarify, rule_tac x=n in exI, simp)
huffman@29451
   427
huffman@29451
   428
lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n"
huffman@29451
   429
  unfolding smult_def
huffman@29451
   430
  by (simp add: Abs_poly_inverse Poly_smult coeff)
huffman@29451
   431
huffman@29451
   432
lemma degree_smult_le: "degree (smult a p) \<le> degree p"
huffman@29451
   433
  by (rule degree_le, simp add: coeff_eq_0)
huffman@29451
   434
huffman@29472
   435
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
huffman@29451
   436
  by (rule poly_ext, simp add: mult_assoc)
huffman@29451
   437
huffman@29451
   438
lemma smult_0_right [simp]: "smult a 0 = 0"
huffman@29451
   439
  by (rule poly_ext, simp)
huffman@29451
   440
huffman@29451
   441
lemma smult_0_left [simp]: "smult 0 p = 0"
huffman@29451
   442
  by (rule poly_ext, simp)
huffman@29451
   443
huffman@29451
   444
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
huffman@29451
   445
  by (rule poly_ext, simp)
huffman@29451
   446
huffman@29451
   447
lemma smult_add_right:
huffman@29451
   448
  "smult a (p + q) = smult a p + smult a q"
nipkow@29667
   449
  by (rule poly_ext, simp add: algebra_simps)
huffman@29451
   450
huffman@29451
   451
lemma smult_add_left:
huffman@29451
   452
  "smult (a + b) p = smult a p + smult b p"
nipkow@29667
   453
  by (rule poly_ext, simp add: algebra_simps)
huffman@29451
   454
huffman@29457
   455
lemma smult_minus_right [simp]:
huffman@29451
   456
  "smult (a::'a::comm_ring) (- p) = - smult a p"
huffman@29451
   457
  by (rule poly_ext, simp)
huffman@29451
   458
huffman@29457
   459
lemma smult_minus_left [simp]:
huffman@29451
   460
  "smult (- a::'a::comm_ring) p = - smult a p"
huffman@29451
   461
  by (rule poly_ext, simp)
huffman@29451
   462
huffman@29451
   463
lemma smult_diff_right:
huffman@29451
   464
  "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
nipkow@29667
   465
  by (rule poly_ext, simp add: algebra_simps)
huffman@29451
   466
huffman@29451
   467
lemma smult_diff_left:
huffman@29451
   468
  "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
nipkow@29667
   469
  by (rule poly_ext, simp add: algebra_simps)
huffman@29451
   470
huffman@29472
   471
lemmas smult_distribs =
huffman@29472
   472
  smult_add_left smult_add_right
huffman@29472
   473
  smult_diff_left smult_diff_right
huffman@29472
   474
huffman@29451
   475
lemma smult_pCons [simp]:
huffman@29451
   476
  "smult a (pCons b p) = pCons (a * b) (smult a p)"
huffman@29451
   477
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
huffman@29451
   478
huffman@29451
   479
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
huffman@29451
   480
  by (induct n, simp add: monom_0, simp add: monom_Suc)
huffman@29451
   481
huffman@29659
   482
lemma degree_smult_eq [simp]:
huffman@29659
   483
  fixes a :: "'a::idom"
huffman@29659
   484
  shows "degree (smult a p) = (if a = 0 then 0 else degree p)"
huffman@29659
   485
  by (cases "a = 0", simp, simp add: degree_def)
huffman@29659
   486
huffman@29659
   487
lemma smult_eq_0_iff [simp]:
huffman@29659
   488
  fixes a :: "'a::idom"
huffman@29659
   489
  shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"
huffman@29659
   490
  by (simp add: expand_poly_eq)
huffman@29659
   491
huffman@29451
   492
huffman@29451
   493
subsection {* Multiplication of polynomials *}
huffman@29451
   494
wenzelm@47382
   495
(* TODO: move to Set_Interval.thy *)
huffman@29451
   496
lemma setsum_atMost_Suc_shift:
huffman@29451
   497
  fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"
huffman@29451
   498
  shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
huffman@29451
   499
proof (induct n)
huffman@29451
   500
  case 0 show ?case by simp
huffman@29451
   501
next
huffman@29451
   502
  case (Suc n) note IH = this
huffman@29451
   503
  have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))"
huffman@29451
   504
    by (rule setsum_atMost_Suc)
huffman@29451
   505
  also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
huffman@29451
   506
    by (rule IH)
huffman@29451
   507
  also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =
huffman@29451
   508
             f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"
huffman@29451
   509
    by (rule add_assoc)
huffman@29451
   510
  also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))"
huffman@29451
   511
    by (rule setsum_atMost_Suc [symmetric])
huffman@29451
   512
  finally show ?case .
huffman@29451
   513
qed
huffman@29451
   514
huffman@29451
   515
instantiation poly :: (comm_semiring_0) comm_semiring_0
huffman@29451
   516
begin
huffman@29451
   517
huffman@29451
   518
definition
haftmann@37765
   519
  times_poly_def:
huffman@29474
   520
    "p * q = poly_rec 0 (\<lambda>a p pq. smult a q + pCons 0 pq) p"
huffman@29474
   521
huffman@29474
   522
lemma mult_poly_0_left: "(0::'a poly) * q = 0"
huffman@29474
   523
  unfolding times_poly_def by (simp add: poly_rec_0)
huffman@29474
   524
huffman@29474
   525
lemma mult_pCons_left [simp]:
huffman@29474
   526
  "pCons a p * q = smult a q + pCons 0 (p * q)"
huffman@29474
   527
  unfolding times_poly_def by (simp add: poly_rec_pCons)
huffman@29474
   528
huffman@29474
   529
lemma mult_poly_0_right: "p * (0::'a poly) = 0"
huffman@29474
   530
  by (induct p, simp add: mult_poly_0_left, simp)
huffman@29451
   531
huffman@29474
   532
lemma mult_pCons_right [simp]:
huffman@29474
   533
  "p * pCons a q = smult a p + pCons 0 (p * q)"
nipkow@29667
   534
  by (induct p, simp add: mult_poly_0_left, simp add: algebra_simps)
huffman@29474
   535
huffman@29474
   536
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
huffman@29474
   537
huffman@29474
   538
lemma mult_smult_left [simp]: "smult a p * q = smult a (p * q)"
huffman@29474
   539
  by (induct p, simp add: mult_poly_0, simp add: smult_add_right)
huffman@29474
   540
huffman@29474
   541
lemma mult_smult_right [simp]: "p * smult a q = smult a (p * q)"
huffman@29474
   542
  by (induct q, simp add: mult_poly_0, simp add: smult_add_right)
huffman@29474
   543
huffman@29474
   544
lemma mult_poly_add_left:
huffman@29474
   545
  fixes p q r :: "'a poly"
huffman@29474
   546
  shows "(p + q) * r = p * r + q * r"
huffman@29474
   547
  by (induct r, simp add: mult_poly_0,
nipkow@29667
   548
                simp add: smult_distribs algebra_simps)
huffman@29451
   549
huffman@29451
   550
instance proof
huffman@29451
   551
  fix p q r :: "'a poly"
huffman@29451
   552
  show 0: "0 * p = 0"
huffman@29474
   553
    by (rule mult_poly_0_left)
huffman@29451
   554
  show "p * 0 = 0"
huffman@29474
   555
    by (rule mult_poly_0_right)
huffman@29451
   556
  show "(p + q) * r = p * r + q * r"
huffman@29474
   557
    by (rule mult_poly_add_left)
huffman@29451
   558
  show "(p * q) * r = p * (q * r)"
huffman@29474
   559
    by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)
huffman@29451
   560
  show "p * q = q * p"
huffman@29474
   561
    by (induct p, simp add: mult_poly_0, simp)
huffman@29451
   562
qed
huffman@29451
   563
huffman@29451
   564
end
huffman@29451
   565
huffman@29540
   566
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
huffman@29540
   567
huffman@29474
   568
lemma coeff_mult:
huffman@29474
   569
  "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
huffman@29474
   570
proof (induct p arbitrary: n)
huffman@29474
   571
  case 0 show ?case by simp
huffman@29474
   572
next
huffman@29474
   573
  case (pCons a p n) thus ?case
huffman@29474
   574
    by (cases n, simp, simp add: setsum_atMost_Suc_shift
huffman@29474
   575
                            del: setsum_atMost_Suc)
huffman@29474
   576
qed
huffman@29451
   577
huffman@29474
   578
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
huffman@29474
   579
apply (rule degree_le)
huffman@29474
   580
apply (induct p)
huffman@29474
   581
apply simp
huffman@29474
   582
apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
huffman@29451
   583
done
huffman@29451
   584
huffman@29451
   585
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
huffman@29451
   586
  by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)
huffman@29451
   587
huffman@29451
   588
huffman@29451
   589
subsection {* The unit polynomial and exponentiation *}
huffman@29451
   590
huffman@29451
   591
instantiation poly :: (comm_semiring_1) comm_semiring_1
huffman@29451
   592
begin
huffman@29451
   593
huffman@29451
   594
definition
huffman@29451
   595
  one_poly_def:
huffman@29451
   596
    "1 = pCons 1 0"
huffman@29451
   597
huffman@29451
   598
instance proof
huffman@29451
   599
  fix p :: "'a poly" show "1 * p = p"
huffman@29451
   600
    unfolding one_poly_def
huffman@29451
   601
    by simp
huffman@29451
   602
next
huffman@29451
   603
  show "0 \<noteq> (1::'a poly)"
huffman@29451
   604
    unfolding one_poly_def by simp
huffman@29451
   605
qed
huffman@29451
   606
huffman@29451
   607
end
huffman@29451
   608
huffman@29540
   609
instance poly :: (comm_semiring_1_cancel) comm_semiring_1_cancel ..
huffman@29540
   610
huffman@29451
   611
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
huffman@29451
   612
  unfolding one_poly_def
huffman@29451
   613
  by (simp add: coeff_pCons split: nat.split)
huffman@29451
   614
huffman@29451
   615
lemma degree_1 [simp]: "degree 1 = 0"
huffman@29451
   616
  unfolding one_poly_def
huffman@29451
   617
  by (rule degree_pCons_0)
huffman@29451
   618
huffman@29979
   619
text {* Lemmas about divisibility *}
huffman@29979
   620
huffman@29979
   621
lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q"
huffman@29979
   622
proof -
huffman@29979
   623
  assume "p dvd q"
huffman@29979
   624
  then obtain k where "q = p * k" ..
huffman@29979
   625
  then have "smult a q = p * smult a k" by simp
huffman@29979
   626
  then show "p dvd smult a q" ..
huffman@29979
   627
qed
huffman@29979
   628
huffman@29979
   629
lemma dvd_smult_cancel:
huffman@29979
   630
  fixes a :: "'a::field"
huffman@29979
   631
  shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q"
huffman@29979
   632
  by (drule dvd_smult [where a="inverse a"]) simp
huffman@29979
   633
huffman@29979
   634
lemma dvd_smult_iff:
huffman@29979
   635
  fixes a :: "'a::field"
huffman@29979
   636
  shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q"
huffman@29979
   637
  by (safe elim!: dvd_smult dvd_smult_cancel)
huffman@29979
   638
huffman@31663
   639
lemma smult_dvd_cancel:
huffman@31663
   640
  "smult a p dvd q \<Longrightarrow> p dvd q"
huffman@31663
   641
proof -
huffman@31663
   642
  assume "smult a p dvd q"
huffman@31663
   643
  then obtain k where "q = smult a p * k" ..
huffman@31663
   644
  then have "q = p * smult a k" by simp
huffman@31663
   645
  then show "p dvd q" ..
huffman@31663
   646
qed
huffman@31663
   647
huffman@31663
   648
lemma smult_dvd:
huffman@31663
   649
  fixes a :: "'a::field"
huffman@31663
   650
  shows "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q"
huffman@31663
   651
  by (rule smult_dvd_cancel [where a="inverse a"]) simp
huffman@31663
   652
huffman@31663
   653
lemma smult_dvd_iff:
huffman@31663
   654
  fixes a :: "'a::field"
huffman@31663
   655
  shows "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)"
huffman@31663
   656
  by (auto elim: smult_dvd smult_dvd_cancel)
huffman@31663
   657
huffman@29979
   658
lemma degree_power_le: "degree (p ^ n) \<le> degree p * n"
huffman@29979
   659
by (induct n, simp, auto intro: order_trans degree_mult_le)
huffman@29979
   660
huffman@29451
   661
instance poly :: (comm_ring) comm_ring ..
huffman@29451
   662
huffman@29451
   663
instance poly :: (comm_ring_1) comm_ring_1 ..
huffman@29451
   664
huffman@29451
   665
huffman@29451
   666
subsection {* Polynomials form an integral domain *}
huffman@29451
   667
huffman@29451
   668
lemma coeff_mult_degree_sum:
huffman@29451
   669
  "coeff (p * q) (degree p + degree q) =
huffman@29451
   670
   coeff p (degree p) * coeff q (degree q)"
huffman@29471
   671
  by (induct p, simp, simp add: coeff_eq_0)
huffman@29451
   672
huffman@29451
   673
instance poly :: (idom) idom
huffman@29451
   674
proof
huffman@29451
   675
  fix p q :: "'a poly"
huffman@29451
   676
  assume "p \<noteq> 0" and "q \<noteq> 0"
huffman@29451
   677
  have "coeff (p * q) (degree p + degree q) =
huffman@29451
   678
        coeff p (degree p) * coeff q (degree q)"
huffman@29451
   679
    by (rule coeff_mult_degree_sum)
huffman@29451
   680
  also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
huffman@29451
   681
    using `p \<noteq> 0` and `q \<noteq> 0` by simp
huffman@29451
   682
  finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
huffman@29451
   683
  thus "p * q \<noteq> 0" by (simp add: expand_poly_eq)
huffman@29451
   684
qed
huffman@29451
   685
huffman@29451
   686
lemma degree_mult_eq:
huffman@29451
   687
  fixes p q :: "'a::idom poly"
huffman@29451
   688
  shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q"
huffman@29451
   689
apply (rule order_antisym [OF degree_mult_le le_degree])
huffman@29451
   690
apply (simp add: coeff_mult_degree_sum)
huffman@29451
   691
done
huffman@29451
   692
huffman@29451
   693
lemma dvd_imp_degree_le:
huffman@29451
   694
  fixes p q :: "'a::idom poly"
huffman@29451
   695
  shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
huffman@29451
   696
  by (erule dvdE, simp add: degree_mult_eq)
huffman@29451
   697
huffman@29451
   698
huffman@29878
   699
subsection {* Polynomials form an ordered integral domain *}
huffman@29878
   700
huffman@29878
   701
definition
haftmann@35028
   702
  pos_poly :: "'a::linordered_idom poly \<Rightarrow> bool"
huffman@29878
   703
where
huffman@29878
   704
  "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"
huffman@29878
   705
huffman@29878
   706
lemma pos_poly_pCons:
huffman@29878
   707
  "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)"
huffman@29878
   708
  unfolding pos_poly_def by simp
huffman@29878
   709
huffman@29878
   710
lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"
huffman@29878
   711
  unfolding pos_poly_def by simp
huffman@29878
   712
huffman@29878
   713
lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)"
huffman@29878
   714
  apply (induct p arbitrary: q, simp)
huffman@29878
   715
  apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos)
huffman@29878
   716
  done
huffman@29878
   717
huffman@29878
   718
lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)"
huffman@29878
   719
  unfolding pos_poly_def
huffman@29878
   720
  apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")
huffman@29878
   721
  apply (simp add: degree_mult_eq coeff_mult_degree_sum mult_pos_pos)
huffman@29878
   722
  apply auto
huffman@29878
   723
  done
huffman@29878
   724
huffman@29878
   725
lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)"
huffman@29878
   726
by (induct p) (auto simp add: pos_poly_pCons)
huffman@29878
   727
haftmann@35028
   728
instantiation poly :: (linordered_idom) linordered_idom
huffman@29878
   729
begin
huffman@29878
   730
huffman@29878
   731
definition
haftmann@37765
   732
  "x < y \<longleftrightarrow> pos_poly (y - x)"
huffman@29878
   733
huffman@29878
   734
definition
haftmann@37765
   735
  "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"
huffman@29878
   736
huffman@29878
   737
definition
haftmann@37765
   738
  "abs (x::'a poly) = (if x < 0 then - x else x)"
huffman@29878
   739
huffman@29878
   740
definition
haftmann@37765
   741
  "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
huffman@29878
   742
huffman@29878
   743
instance proof
huffman@29878
   744
  fix x y :: "'a poly"
huffman@29878
   745
  show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
huffman@29878
   746
    unfolding less_eq_poly_def less_poly_def
huffman@29878
   747
    apply safe
huffman@29878
   748
    apply simp
huffman@29878
   749
    apply (drule (1) pos_poly_add)
huffman@29878
   750
    apply simp
huffman@29878
   751
    done
huffman@29878
   752
next
huffman@29878
   753
  fix x :: "'a poly" show "x \<le> x"
huffman@29878
   754
    unfolding less_eq_poly_def by simp
huffman@29878
   755
next
huffman@29878
   756
  fix x y z :: "'a poly"
huffman@29878
   757
  assume "x \<le> y" and "y \<le> z" thus "x \<le> z"
huffman@29878
   758
    unfolding less_eq_poly_def
huffman@29878
   759
    apply safe
huffman@29878
   760
    apply (drule (1) pos_poly_add)
huffman@29878
   761
    apply (simp add: algebra_simps)
huffman@29878
   762
    done
huffman@29878
   763
next
huffman@29878
   764
  fix x y :: "'a poly"
huffman@29878
   765
  assume "x \<le> y" and "y \<le> x" thus "x = y"
huffman@29878
   766
    unfolding less_eq_poly_def
huffman@29878
   767
    apply safe
huffman@29878
   768
    apply (drule (1) pos_poly_add)
huffman@29878
   769
    apply simp
huffman@29878
   770
    done
huffman@29878
   771
next
huffman@29878
   772
  fix x y z :: "'a poly"
huffman@29878
   773
  assume "x \<le> y" thus "z + x \<le> z + y"
huffman@29878
   774
    unfolding less_eq_poly_def
huffman@29878
   775
    apply safe
huffman@29878
   776
    apply (simp add: algebra_simps)
huffman@29878
   777
    done
huffman@29878
   778
next
huffman@29878
   779
  fix x y :: "'a poly"
huffman@29878
   780
  show "x \<le> y \<or> y \<le> x"
huffman@29878
   781
    unfolding less_eq_poly_def
huffman@29878
   782
    using pos_poly_total [of "x - y"]
huffman@29878
   783
    by auto
huffman@29878
   784
next
huffman@29878
   785
  fix x y z :: "'a poly"
huffman@29878
   786
  assume "x < y" and "0 < z"
huffman@29878
   787
  thus "z * x < z * y"
huffman@29878
   788
    unfolding less_poly_def
huffman@29878
   789
    by (simp add: right_diff_distrib [symmetric] pos_poly_mult)
huffman@29878
   790
next
huffman@29878
   791
  fix x :: "'a poly"
huffman@29878
   792
  show "\<bar>x\<bar> = (if x < 0 then - x else x)"
huffman@29878
   793
    by (rule abs_poly_def)
huffman@29878
   794
next
huffman@29878
   795
  fix x :: "'a poly"
huffman@29878
   796
  show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
huffman@29878
   797
    by (rule sgn_poly_def)
huffman@29878
   798
qed
huffman@29878
   799
huffman@29878
   800
end
huffman@29878
   801
huffman@29878
   802
text {* TODO: Simplification rules for comparisons *}
huffman@29878
   803
huffman@29878
   804
huffman@29451
   805
subsection {* Long division of polynomials *}
huffman@29451
   806
huffman@29451
   807
definition
huffman@29537
   808
  pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
huffman@29451
   809
where
huffman@29537
   810
  "pdivmod_rel x y q r \<longleftrightarrow>
huffman@29451
   811
    x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"
huffman@29451
   812
huffman@29537
   813
lemma pdivmod_rel_0:
huffman@29537
   814
  "pdivmod_rel 0 y 0 0"
huffman@29537
   815
  unfolding pdivmod_rel_def by simp
huffman@29451
   816
huffman@29537
   817
lemma pdivmod_rel_by_0:
huffman@29537
   818
  "pdivmod_rel x 0 0 x"
huffman@29537
   819
  unfolding pdivmod_rel_def by simp
huffman@29451
   820
huffman@29451
   821
lemma eq_zero_or_degree_less:
huffman@29451
   822
  assumes "degree p \<le> n" and "coeff p n = 0"
huffman@29451
   823
  shows "p = 0 \<or> degree p < n"
huffman@29451
   824
proof (cases n)
huffman@29451
   825
  case 0
huffman@29451
   826
  with `degree p \<le> n` and `coeff p n = 0`
huffman@29451
   827
  have "coeff p (degree p) = 0" by simp
huffman@29451
   828
  then have "p = 0" by simp
huffman@29451
   829
  then show ?thesis ..
huffman@29451
   830
next
huffman@29451
   831
  case (Suc m)
huffman@29451
   832
  have "\<forall>i>n. coeff p i = 0"
huffman@29451
   833
    using `degree p \<le> n` by (simp add: coeff_eq_0)
huffman@29451
   834
  then have "\<forall>i\<ge>n. coeff p i = 0"
huffman@29451
   835
    using `coeff p n = 0` by (simp add: le_less)
huffman@29451
   836
  then have "\<forall>i>m. coeff p i = 0"
huffman@29451
   837
    using `n = Suc m` by (simp add: less_eq_Suc_le)
huffman@29451
   838
  then have "degree p \<le> m"
huffman@29451
   839
    by (rule degree_le)
huffman@29451
   840
  then have "degree p < n"
huffman@29451
   841
    using `n = Suc m` by (simp add: less_Suc_eq_le)
huffman@29451
   842
  then show ?thesis ..
huffman@29451
   843
qed
huffman@29451
   844
huffman@29537
   845
lemma pdivmod_rel_pCons:
huffman@29537
   846
  assumes rel: "pdivmod_rel x y q r"
huffman@29451
   847
  assumes y: "y \<noteq> 0"
huffman@29451
   848
  assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
huffman@29537
   849
  shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
huffman@29537
   850
    (is "pdivmod_rel ?x y ?q ?r")
huffman@29451
   851
proof -
huffman@29451
   852
  have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
huffman@29537
   853
    using assms unfolding pdivmod_rel_def by simp_all
huffman@29451
   854
huffman@29451
   855
  have 1: "?x = ?q * y + ?r"
huffman@29451
   856
    using b x by simp
huffman@29451
   857
huffman@29451
   858
  have 2: "?r = 0 \<or> degree ?r < degree y"
huffman@29451
   859
  proof (rule eq_zero_or_degree_less)
huffman@29539
   860
    show "degree ?r \<le> degree y"
huffman@29539
   861
    proof (rule degree_diff_le)
huffman@29451
   862
      show "degree (pCons a r) \<le> degree y"
huffman@29460
   863
        using r by auto
huffman@29451
   864
      show "degree (smult b y) \<le> degree y"
huffman@29451
   865
        by (rule degree_smult_le)
huffman@29451
   866
    qed
huffman@29451
   867
  next
huffman@29451
   868
    show "coeff ?r (degree y) = 0"
huffman@29451
   869
      using `y \<noteq> 0` unfolding b by simp
huffman@29451
   870
  qed
huffman@29451
   871
huffman@29451
   872
  from 1 2 show ?thesis
huffman@29537
   873
    unfolding pdivmod_rel_def
huffman@29451
   874
    using `y \<noteq> 0` by simp
huffman@29451
   875
qed
huffman@29451
   876
huffman@29537
   877
lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r"
huffman@29451
   878
apply (cases "y = 0")
huffman@29537
   879
apply (fast intro!: pdivmod_rel_by_0)
huffman@29451
   880
apply (induct x)
huffman@29537
   881
apply (fast intro!: pdivmod_rel_0)
huffman@29537
   882
apply (fast intro!: pdivmod_rel_pCons)
huffman@29451
   883
done
huffman@29451
   884
huffman@29537
   885
lemma pdivmod_rel_unique:
huffman@29537
   886
  assumes 1: "pdivmod_rel x y q1 r1"
huffman@29537
   887
  assumes 2: "pdivmod_rel x y q2 r2"
huffman@29451
   888
  shows "q1 = q2 \<and> r1 = r2"
huffman@29451
   889
proof (cases "y = 0")
huffman@29451
   890
  assume "y = 0" with assms show ?thesis
huffman@29537
   891
    by (simp add: pdivmod_rel_def)
huffman@29451
   892
next
huffman@29451
   893
  assume [simp]: "y \<noteq> 0"
huffman@29451
   894
  from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
huffman@29537
   895
    unfolding pdivmod_rel_def by simp_all
huffman@29451
   896
  from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
huffman@29537
   897
    unfolding pdivmod_rel_def by simp_all
huffman@29451
   898
  from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
nipkow@29667
   899
    by (simp add: algebra_simps)
huffman@29451
   900
  from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
huffman@29453
   901
    by (auto intro: degree_diff_less)
huffman@29451
   902
huffman@29451
   903
  show "q1 = q2 \<and> r1 = r2"
huffman@29451
   904
  proof (rule ccontr)
huffman@29451
   905
    assume "\<not> (q1 = q2 \<and> r1 = r2)"
huffman@29451
   906
    with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
huffman@29451
   907
    with r3 have "degree (r2 - r1) < degree y" by simp
huffman@29451
   908
    also have "degree y \<le> degree (q1 - q2) + degree y" by simp
huffman@29451
   909
    also have "\<dots> = degree ((q1 - q2) * y)"
huffman@29451
   910
      using `q1 \<noteq> q2` by (simp add: degree_mult_eq)
huffman@29451
   911
    also have "\<dots> = degree (r2 - r1)"
huffman@29451
   912
      using q3 by simp
huffman@29451
   913
    finally have "degree (r2 - r1) < degree (r2 - r1)" .
huffman@29451
   914
    then show "False" by simp
huffman@29451
   915
  qed
huffman@29451
   916
qed
huffman@29451
   917
huffman@29660
   918
lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0"
huffman@29660
   919
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0)
huffman@29660
   920
huffman@29660
   921
lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x"
huffman@29660
   922
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0)
huffman@29660
   923
wenzelm@45605
   924
lemmas pdivmod_rel_unique_div = pdivmod_rel_unique [THEN conjunct1]
huffman@29451
   925
wenzelm@45605
   926
lemmas pdivmod_rel_unique_mod = pdivmod_rel_unique [THEN conjunct2]
huffman@29451
   927
huffman@29451
   928
instantiation poly :: (field) ring_div
huffman@29451
   929
begin
huffman@29451
   930
huffman@29451
   931
definition div_poly where
haftmann@37765
   932
  "x div y = (THE q. \<exists>r. pdivmod_rel x y q r)"
huffman@29451
   933
huffman@29451
   934
definition mod_poly where
haftmann@37765
   935
  "x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)"
huffman@29451
   936
huffman@29451
   937
lemma div_poly_eq:
huffman@29537
   938
  "pdivmod_rel x y q r \<Longrightarrow> x div y = q"
huffman@29451
   939
unfolding div_poly_def
huffman@29537
   940
by (fast elim: pdivmod_rel_unique_div)
huffman@29451
   941
huffman@29451
   942
lemma mod_poly_eq:
huffman@29537
   943
  "pdivmod_rel x y q r \<Longrightarrow> x mod y = r"
huffman@29451
   944
unfolding mod_poly_def
huffman@29537
   945
by (fast elim: pdivmod_rel_unique_mod)
huffman@29451
   946
huffman@29537
   947
lemma pdivmod_rel:
huffman@29537
   948
  "pdivmod_rel x y (x div y) (x mod y)"
huffman@29451
   949
proof -
huffman@29537
   950
  from pdivmod_rel_exists
huffman@29537
   951
    obtain q r where "pdivmod_rel x y q r" by fast
huffman@29451
   952
  thus ?thesis
huffman@29451
   953
    by (simp add: div_poly_eq mod_poly_eq)
huffman@29451
   954
qed
huffman@29451
   955
huffman@29451
   956
instance proof
huffman@29451
   957
  fix x y :: "'a poly"
huffman@29451
   958
  show "x div y * y + x mod y = x"
huffman@29537
   959
    using pdivmod_rel [of x y]
huffman@29537
   960
    by (simp add: pdivmod_rel_def)
huffman@29451
   961
next
huffman@29451
   962
  fix x :: "'a poly"
huffman@29537
   963
  have "pdivmod_rel x 0 0 x"
huffman@29537
   964
    by (rule pdivmod_rel_by_0)
huffman@29451
   965
  thus "x div 0 = 0"
huffman@29451
   966
    by (rule div_poly_eq)
huffman@29451
   967
next
huffman@29451
   968
  fix y :: "'a poly"
huffman@29537
   969
  have "pdivmod_rel 0 y 0 0"
huffman@29537
   970
    by (rule pdivmod_rel_0)
huffman@29451
   971
  thus "0 div y = 0"
huffman@29451
   972
    by (rule div_poly_eq)
huffman@29451
   973
next
huffman@29451
   974
  fix x y z :: "'a poly"
huffman@29451
   975
  assume "y \<noteq> 0"
huffman@29537
   976
  hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)"
huffman@29537
   977
    using pdivmod_rel [of x y]
webertj@49962
   978
    by (simp add: pdivmod_rel_def distrib_right)
huffman@29451
   979
  thus "(x + z * y) div y = z + x div y"
huffman@29451
   980
    by (rule div_poly_eq)
haftmann@30930
   981
next
haftmann@30930
   982
  fix x y z :: "'a poly"
haftmann@30930
   983
  assume "x \<noteq> 0"
haftmann@30930
   984
  show "(x * y) div (x * z) = y div z"
haftmann@30930
   985
  proof (cases "y \<noteq> 0 \<and> z \<noteq> 0")
haftmann@30930
   986
    have "\<And>x::'a poly. pdivmod_rel x 0 0 x"
haftmann@30930
   987
      by (rule pdivmod_rel_by_0)
haftmann@30930
   988
    then have [simp]: "\<And>x::'a poly. x div 0 = 0"
haftmann@30930
   989
      by (rule div_poly_eq)
haftmann@30930
   990
    have "\<And>x::'a poly. pdivmod_rel 0 x 0 0"
haftmann@30930
   991
      by (rule pdivmod_rel_0)
haftmann@30930
   992
    then have [simp]: "\<And>x::'a poly. 0 div x = 0"
haftmann@30930
   993
      by (rule div_poly_eq)
haftmann@30930
   994
    case False then show ?thesis by auto
haftmann@30930
   995
  next
haftmann@30930
   996
    case True then have "y \<noteq> 0" and "z \<noteq> 0" by auto
haftmann@30930
   997
    with `x \<noteq> 0`
haftmann@30930
   998
    have "\<And>q r. pdivmod_rel y z q r \<Longrightarrow> pdivmod_rel (x * y) (x * z) q (x * r)"
haftmann@30930
   999
      by (auto simp add: pdivmod_rel_def algebra_simps)
haftmann@30930
  1000
        (rule classical, simp add: degree_mult_eq)
haftmann@30930
  1001
    moreover from pdivmod_rel have "pdivmod_rel y z (y div z) (y mod z)" .
haftmann@30930
  1002
    ultimately have "pdivmod_rel (x * y) (x * z) (y div z) (x * (y mod z))" .
haftmann@30930
  1003
    then show ?thesis by (simp add: div_poly_eq)
haftmann@30930
  1004
  qed
huffman@29451
  1005
qed
huffman@29451
  1006
huffman@29451
  1007
end
huffman@29451
  1008
huffman@29451
  1009
lemma degree_mod_less:
huffman@29451
  1010
  "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
huffman@29537
  1011
  using pdivmod_rel [of x y]
huffman@29537
  1012
  unfolding pdivmod_rel_def by simp
huffman@29451
  1013
huffman@29451
  1014
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
huffman@29451
  1015
proof -
huffman@29451
  1016
  assume "degree x < degree y"
huffman@29537
  1017
  hence "pdivmod_rel x y 0 x"
huffman@29537
  1018
    by (simp add: pdivmod_rel_def)
huffman@29451
  1019
  thus "x div y = 0" by (rule div_poly_eq)
huffman@29451
  1020
qed
huffman@29451
  1021
huffman@29451
  1022
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
huffman@29451
  1023
proof -
huffman@29451
  1024
  assume "degree x < degree y"
huffman@29537
  1025
  hence "pdivmod_rel x y 0 x"
huffman@29537
  1026
    by (simp add: pdivmod_rel_def)
huffman@29451
  1027
  thus "x mod y = x" by (rule mod_poly_eq)
huffman@29451
  1028
qed
huffman@29451
  1029
huffman@29659
  1030
lemma pdivmod_rel_smult_left:
huffman@29659
  1031
  "pdivmod_rel x y q r
huffman@29659
  1032
    \<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)"
huffman@29659
  1033
  unfolding pdivmod_rel_def by (simp add: smult_add_right)
huffman@29659
  1034
huffman@29659
  1035
lemma div_smult_left: "(smult a x) div y = smult a (x div y)"
huffman@29659
  1036
  by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
huffman@29659
  1037
huffman@29659
  1038
lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"
huffman@29659
  1039
  by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
huffman@29659
  1040
huffman@30072
  1041
lemma poly_div_minus_left [simp]:
huffman@30072
  1042
  fixes x y :: "'a::field poly"
huffman@30072
  1043
  shows "(- x) div y = - (x div y)"
huffman@47108
  1044
  using div_smult_left [of "- 1::'a"] by (simp del: minus_one) (* FIXME *)
huffman@30072
  1045
huffman@30072
  1046
lemma poly_mod_minus_left [simp]:
huffman@30072
  1047
  fixes x y :: "'a::field poly"
huffman@30072
  1048
  shows "(- x) mod y = - (x mod y)"
huffman@47108
  1049
  using mod_smult_left [of "- 1::'a"] by (simp del: minus_one) (* FIXME *)
huffman@30072
  1050
huffman@29659
  1051
lemma pdivmod_rel_smult_right:
huffman@29659
  1052
  "\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk>
huffman@29659
  1053
    \<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r"
huffman@29659
  1054
  unfolding pdivmod_rel_def by simp
huffman@29659
  1055
huffman@29659
  1056
lemma div_smult_right:
huffman@29659
  1057
  "a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)"
huffman@29659
  1058
  by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
huffman@29659
  1059
huffman@29659
  1060
lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y"
huffman@29659
  1061
  by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
huffman@29659
  1062
huffman@30072
  1063
lemma poly_div_minus_right [simp]:
huffman@30072
  1064
  fixes x y :: "'a::field poly"
huffman@30072
  1065
  shows "x div (- y) = - (x div y)"
huffman@30072
  1066
  using div_smult_right [of "- 1::'a"]
huffman@47108
  1067
  by (simp add: nonzero_inverse_minus_eq del: minus_one) (* FIXME *)
huffman@30072
  1068
huffman@30072
  1069
lemma poly_mod_minus_right [simp]:
huffman@30072
  1070
  fixes x y :: "'a::field poly"
huffman@30072
  1071
  shows "x mod (- y) = x mod y"
huffman@47108
  1072
  using mod_smult_right [of "- 1::'a"] by (simp del: minus_one) (* FIXME *)
huffman@30072
  1073
huffman@29660
  1074
lemma pdivmod_rel_mult:
huffman@29660
  1075
  "\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk>
huffman@29660
  1076
    \<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)"
huffman@29660
  1077
apply (cases "z = 0", simp add: pdivmod_rel_def)
huffman@29660
  1078
apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff)
huffman@29660
  1079
apply (cases "r = 0")
huffman@29660
  1080
apply (cases "r' = 0")
huffman@29660
  1081
apply (simp add: pdivmod_rel_def)
haftmann@36350
  1082
apply (simp add: pdivmod_rel_def field_simps degree_mult_eq)
huffman@29660
  1083
apply (cases "r' = 0")
huffman@29660
  1084
apply (simp add: pdivmod_rel_def degree_mult_eq)
haftmann@36350
  1085
apply (simp add: pdivmod_rel_def field_simps)
huffman@29660
  1086
apply (simp add: degree_mult_eq degree_add_less)
huffman@29660
  1087
done
huffman@29660
  1088
huffman@29660
  1089
lemma poly_div_mult_right:
huffman@29660
  1090
  fixes x y z :: "'a::field poly"
huffman@29660
  1091
  shows "x div (y * z) = (x div y) div z"
huffman@29660
  1092
  by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
huffman@29660
  1093
huffman@29660
  1094
lemma poly_mod_mult_right:
huffman@29660
  1095
  fixes x y z :: "'a::field poly"
huffman@29660
  1096
  shows "x mod (y * z) = y * (x div y mod z) + x mod y"
huffman@29660
  1097
  by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
huffman@29660
  1098
huffman@29451
  1099
lemma mod_pCons:
huffman@29451
  1100
  fixes a and x
huffman@29451
  1101
  assumes y: "y \<noteq> 0"
huffman@29451
  1102
  defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"
huffman@29451
  1103
  shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
huffman@29451
  1104
unfolding b
huffman@29451
  1105
apply (rule mod_poly_eq)
huffman@29537
  1106
apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl])
huffman@29451
  1107
done
huffman@29451
  1108
huffman@29451
  1109
huffman@31663
  1110
subsection {* GCD of polynomials *}
huffman@31663
  1111
huffman@31663
  1112
function
huffman@31663
  1113
  poly_gcd :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
huffman@31663
  1114
  "poly_gcd x 0 = smult (inverse (coeff x (degree x))) x"
huffman@31663
  1115
| "y \<noteq> 0 \<Longrightarrow> poly_gcd x y = poly_gcd y (x mod y)"
huffman@31663
  1116
by auto
huffman@31663
  1117
huffman@31663
  1118
termination poly_gcd
huffman@31663
  1119
by (relation "measure (\<lambda>(x, y). if y = 0 then 0 else Suc (degree y))")
huffman@31663
  1120
   (auto dest: degree_mod_less)
huffman@31663
  1121
haftmann@37765
  1122
declare poly_gcd.simps [simp del]
huffman@31663
  1123
huffman@31663
  1124
lemma poly_gcd_dvd1 [iff]: "poly_gcd x y dvd x"
huffman@31663
  1125
  and poly_gcd_dvd2 [iff]: "poly_gcd x y dvd y"
huffman@31663
  1126
  apply (induct x y rule: poly_gcd.induct)
huffman@31663
  1127
  apply (simp_all add: poly_gcd.simps)
nipkow@44890
  1128
  apply (fastforce simp add: smult_dvd_iff dest: inverse_zero_imp_zero)
huffman@31663
  1129
  apply (blast dest: dvd_mod_imp_dvd)
huffman@31663
  1130
  done
huffman@31663
  1131
huffman@31663
  1132
lemma poly_gcd_greatest: "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd poly_gcd x y"
huffman@31663
  1133
  by (induct x y rule: poly_gcd.induct)
huffman@31663
  1134
     (simp_all add: poly_gcd.simps dvd_mod dvd_smult)
huffman@31663
  1135
huffman@31663
  1136
lemma dvd_poly_gcd_iff [iff]:
huffman@31663
  1137
  "k dvd poly_gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"
huffman@31663
  1138
  by (blast intro!: poly_gcd_greatest intro: dvd_trans)
huffman@31663
  1139
huffman@31663
  1140
lemma poly_gcd_monic:
huffman@31663
  1141
  "coeff (poly_gcd x y) (degree (poly_gcd x y)) =
huffman@31663
  1142
    (if x = 0 \<and> y = 0 then 0 else 1)"
huffman@31663
  1143
  by (induct x y rule: poly_gcd.induct)
huffman@31663
  1144
     (simp_all add: poly_gcd.simps nonzero_imp_inverse_nonzero)
huffman@31663
  1145
huffman@31663
  1146
lemma poly_gcd_zero_iff [simp]:
huffman@31663
  1147
  "poly_gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
huffman@31663
  1148
  by (simp only: dvd_0_left_iff [symmetric] dvd_poly_gcd_iff)
huffman@31663
  1149
huffman@31663
  1150
lemma poly_gcd_0_0 [simp]: "poly_gcd 0 0 = 0"
huffman@31663
  1151
  by simp
huffman@31663
  1152
huffman@31663
  1153
lemma poly_dvd_antisym:
huffman@31663
  1154
  fixes p q :: "'a::idom poly"
huffman@31663
  1155
  assumes coeff: "coeff p (degree p) = coeff q (degree q)"
huffman@31663
  1156
  assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q"
huffman@31663
  1157
proof (cases "p = 0")
huffman@31663
  1158
  case True with coeff show "p = q" by simp
huffman@31663
  1159
next
huffman@31663
  1160
  case False with coeff have "q \<noteq> 0" by auto
huffman@31663
  1161
  have degree: "degree p = degree q"
huffman@31663
  1162
    using `p dvd q` `q dvd p` `p \<noteq> 0` `q \<noteq> 0`
huffman@31663
  1163
    by (intro order_antisym dvd_imp_degree_le)
huffman@31663
  1164
huffman@31663
  1165
  from `p dvd q` obtain a where a: "q = p * a" ..
huffman@31663
  1166
  with `q \<noteq> 0` have "a \<noteq> 0" by auto
huffman@31663
  1167
  with degree a `p \<noteq> 0` have "degree a = 0"
huffman@31663
  1168
    by (simp add: degree_mult_eq)
huffman@31663
  1169
  with coeff a show "p = q"
huffman@31663
  1170
    by (cases a, auto split: if_splits)
huffman@31663
  1171
qed
huffman@31663
  1172
huffman@31663
  1173
lemma poly_gcd_unique:
huffman@31663
  1174
  assumes dvd1: "d dvd x" and dvd2: "d dvd y"
huffman@31663
  1175
    and greatest: "\<And>k. k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd d"
huffman@31663
  1176
    and monic: "coeff d (degree d) = (if x = 0 \<and> y = 0 then 0 else 1)"
huffman@31663
  1177
  shows "poly_gcd x y = d"
huffman@31663
  1178
proof -
huffman@31663
  1179
  have "coeff (poly_gcd x y) (degree (poly_gcd x y)) = coeff d (degree d)"
huffman@31663
  1180
    by (simp_all add: poly_gcd_monic monic)
huffman@31663
  1181
  moreover have "poly_gcd x y dvd d"
huffman@31663
  1182
    using poly_gcd_dvd1 poly_gcd_dvd2 by (rule greatest)
huffman@31663
  1183
  moreover have "d dvd poly_gcd x y"
huffman@31663
  1184
    using dvd1 dvd2 by (rule poly_gcd_greatest)
huffman@31663
  1185
  ultimately show ?thesis
huffman@31663
  1186
    by (rule poly_dvd_antisym)
huffman@31663
  1187
qed
huffman@31663
  1188
haftmann@37770
  1189
interpretation poly_gcd: abel_semigroup poly_gcd
haftmann@34973
  1190
proof
haftmann@34973
  1191
  fix x y z :: "'a poly"
haftmann@34973
  1192
  show "poly_gcd (poly_gcd x y) z = poly_gcd x (poly_gcd y z)"
haftmann@34973
  1193
    by (rule poly_gcd_unique) (auto intro: dvd_trans simp add: poly_gcd_monic)
haftmann@34973
  1194
  show "poly_gcd x y = poly_gcd y x"
haftmann@34973
  1195
    by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
haftmann@34973
  1196
qed
huffman@31663
  1197
haftmann@34973
  1198
lemmas poly_gcd_assoc = poly_gcd.assoc
haftmann@34973
  1199
lemmas poly_gcd_commute = poly_gcd.commute
haftmann@34973
  1200
lemmas poly_gcd_left_commute = poly_gcd.left_commute
huffman@31663
  1201
huffman@31663
  1202
lemmas poly_gcd_ac = poly_gcd_assoc poly_gcd_commute poly_gcd_left_commute
huffman@31663
  1203
huffman@31663
  1204
lemma poly_gcd_1_left [simp]: "poly_gcd 1 y = 1"
huffman@31663
  1205
by (rule poly_gcd_unique) simp_all
huffman@31663
  1206
huffman@31663
  1207
lemma poly_gcd_1_right [simp]: "poly_gcd x 1 = 1"
huffman@31663
  1208
by (rule poly_gcd_unique) simp_all
huffman@31663
  1209
huffman@31663
  1210
lemma poly_gcd_minus_left [simp]: "poly_gcd (- x) y = poly_gcd x y"
huffman@31663
  1211
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
huffman@31663
  1212
huffman@31663
  1213
lemma poly_gcd_minus_right [simp]: "poly_gcd x (- y) = poly_gcd x y"
huffman@31663
  1214
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
huffman@31663
  1215
huffman@31663
  1216
huffman@29451
  1217
subsection {* Evaluation of polynomials *}
huffman@29451
  1218
huffman@29451
  1219
definition
huffman@29454
  1220
  poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a" where
huffman@29454
  1221
  "poly = poly_rec (\<lambda>x. 0) (\<lambda>a p f x. a + x * f x)"
huffman@29451
  1222
huffman@29451
  1223
lemma poly_0 [simp]: "poly 0 x = 0"
huffman@29454
  1224
  unfolding poly_def by (simp add: poly_rec_0)
huffman@29451
  1225
huffman@29451
  1226
lemma poly_pCons [simp]: "poly (pCons a p) x = a + x * poly p x"
huffman@29454
  1227
  unfolding poly_def by (simp add: poly_rec_pCons)
huffman@29451
  1228
huffman@29451
  1229
lemma poly_1 [simp]: "poly 1 x = 1"
huffman@29451
  1230
  unfolding one_poly_def by simp
huffman@29451
  1231
huffman@29454
  1232
lemma poly_monom:
haftmann@31021
  1233
  fixes a x :: "'a::{comm_semiring_1}"
huffman@29454
  1234
  shows "poly (monom a n) x = a * x ^ n"
huffman@29451
  1235
  by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac)
huffman@29451
  1236
huffman@29451
  1237
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
huffman@29451
  1238
  apply (induct p arbitrary: q, simp)
nipkow@29667
  1239
  apply (case_tac q, simp, simp add: algebra_simps)
huffman@29451
  1240
  done
huffman@29451
  1241
huffman@29451
  1242
lemma poly_minus [simp]:
huffman@29454
  1243
  fixes x :: "'a::comm_ring"
huffman@29451
  1244
  shows "poly (- p) x = - poly p x"
huffman@29451
  1245
  by (induct p, simp_all)
huffman@29451
  1246
huffman@29451
  1247
lemma poly_diff [simp]:
huffman@29454
  1248
  fixes x :: "'a::comm_ring"
huffman@29451
  1249
  shows "poly (p - q) x = poly p x - poly q x"
huffman@29451
  1250
  by (simp add: diff_minus)
huffman@29451
  1251
huffman@29451
  1252
lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"
huffman@29451
  1253
  by (cases "finite A", induct set: finite, simp_all)
huffman@29451
  1254
huffman@29451
  1255
lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x"
nipkow@29667
  1256
  by (induct p, simp, simp add: algebra_simps)
huffman@29451
  1257
huffman@29451
  1258
lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x"
nipkow@29667
  1259
  by (induct p, simp_all, simp add: algebra_simps)
huffman@29451
  1260
huffman@29462
  1261
lemma poly_power [simp]:
haftmann@31021
  1262
  fixes p :: "'a::{comm_semiring_1} poly"
huffman@29462
  1263
  shows "poly (p ^ n) x = poly p x ^ n"
huffman@29462
  1264
  by (induct n, simp, simp add: power_Suc)
huffman@29462
  1265
huffman@29456
  1266
huffman@29456
  1267
subsection {* Synthetic division *}
huffman@29456
  1268
huffman@29980
  1269
text {*
huffman@29980
  1270
  Synthetic division is simply division by the
huffman@29980
  1271
  linear polynomial @{term "x - c"}.
huffman@29980
  1272
*}
huffman@29980
  1273
huffman@29456
  1274
definition
huffman@29456
  1275
  synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"
haftmann@37765
  1276
where
huffman@29456
  1277
  "synthetic_divmod p c =
huffman@29456
  1278
    poly_rec (0, 0) (\<lambda>a p (q, r). (pCons r q, a + c * r)) p"
huffman@29456
  1279
huffman@29456
  1280
definition
huffman@29456
  1281
  synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
huffman@29456
  1282
where
huffman@29456
  1283
  "synthetic_div p c = fst (synthetic_divmod p c)"
huffman@29456
  1284
huffman@29456
  1285
lemma synthetic_divmod_0 [simp]:
huffman@29456
  1286
  "synthetic_divmod 0 c = (0, 0)"
huffman@29456
  1287
  unfolding synthetic_divmod_def
huffman@29456
  1288
  by (simp add: poly_rec_0)
huffman@29456
  1289
huffman@29456
  1290
lemma synthetic_divmod_pCons [simp]:
huffman@29456
  1291
  "synthetic_divmod (pCons a p) c =
huffman@29456
  1292
    (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)"
huffman@29456
  1293
  unfolding synthetic_divmod_def
huffman@29456
  1294
  by (simp add: poly_rec_pCons)
huffman@29456
  1295
huffman@29456
  1296
lemma snd_synthetic_divmod: "snd (synthetic_divmod p c) = poly p c"
huffman@29456
  1297
  by (induct p, simp, simp add: split_def)
huffman@29456
  1298
huffman@29456
  1299
lemma synthetic_div_0 [simp]: "synthetic_div 0 c = 0"
huffman@29456
  1300
  unfolding synthetic_div_def by simp
huffman@29456
  1301
huffman@29456
  1302
lemma synthetic_div_pCons [simp]:
huffman@29456
  1303
  "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)"
huffman@29456
  1304
  unfolding synthetic_div_def
huffman@29456
  1305
  by (simp add: split_def snd_synthetic_divmod)
huffman@29456
  1306
huffman@29460
  1307
lemma synthetic_div_eq_0_iff:
huffman@29460
  1308
  "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0"
huffman@29460
  1309
  by (induct p, simp, case_tac p, simp)
huffman@29460
  1310
huffman@29460
  1311
lemma degree_synthetic_div:
huffman@29460
  1312
  "degree (synthetic_div p c) = degree p - 1"
huffman@29460
  1313
  by (induct p, simp, simp add: synthetic_div_eq_0_iff)
huffman@29460
  1314
huffman@29457
  1315
lemma synthetic_div_correct:
huffman@29456
  1316
  "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)"
huffman@29456
  1317
  by (induct p) simp_all
huffman@29456
  1318
huffman@29457
  1319
lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0"
huffman@29457
  1320
by (induct p arbitrary: a) simp_all
huffman@29457
  1321
huffman@29457
  1322
lemma synthetic_div_unique:
huffman@29457
  1323
  "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c"
huffman@29457
  1324
apply (induct p arbitrary: q r)
huffman@29457
  1325
apply (simp, frule synthetic_div_unique_lemma, simp)
huffman@29457
  1326
apply (case_tac q, force)
huffman@29457
  1327
done
huffman@29457
  1328
huffman@29457
  1329
lemma synthetic_div_correct':
huffman@29457
  1330
  fixes c :: "'a::comm_ring_1"
huffman@29457
  1331
  shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"
huffman@29457
  1332
  using synthetic_div_correct [of p c]
nipkow@29667
  1333
  by (simp add: algebra_simps)
huffman@29457
  1334
huffman@29460
  1335
lemma poly_eq_0_iff_dvd:
huffman@29460
  1336
  fixes c :: "'a::idom"
huffman@29460
  1337
  shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p"
huffman@29460
  1338
proof
huffman@29460
  1339
  assume "poly p c = 0"
huffman@29460
  1340
  with synthetic_div_correct' [of c p]
huffman@29460
  1341
  have "p = [:-c, 1:] * synthetic_div p c" by simp
huffman@29460
  1342
  then show "[:-c, 1:] dvd p" ..
huffman@29460
  1343
next
huffman@29460
  1344
  assume "[:-c, 1:] dvd p"
huffman@29460
  1345
  then obtain k where "p = [:-c, 1:] * k" by (rule dvdE)
huffman@29460
  1346
  then show "poly p c = 0" by simp
huffman@29460
  1347
qed
huffman@29460
  1348
huffman@29460
  1349
lemma dvd_iff_poly_eq_0:
huffman@29460
  1350
  fixes c :: "'a::idom"
huffman@29460
  1351
  shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0"
huffman@29460
  1352
  by (simp add: poly_eq_0_iff_dvd)
huffman@29460
  1353
huffman@29462
  1354
lemma poly_roots_finite:
huffman@29462
  1355
  fixes p :: "'a::idom poly"
huffman@29462
  1356
  shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
huffman@29462
  1357
proof (induct n \<equiv> "degree p" arbitrary: p)
huffman@29462
  1358
  case (0 p)
huffman@29462
  1359
  then obtain a where "a \<noteq> 0" and "p = [:a:]"
huffman@29462
  1360
    by (cases p, simp split: if_splits)
huffman@29462
  1361
  then show "finite {x. poly p x = 0}" by simp
huffman@29462
  1362
next
huffman@29462
  1363
  case (Suc n p)
huffman@29462
  1364
  show "finite {x. poly p x = 0}"
huffman@29462
  1365
  proof (cases "\<exists>x. poly p x = 0")
huffman@29462
  1366
    case False
huffman@29462
  1367
    then show "finite {x. poly p x = 0}" by simp
huffman@29462
  1368
  next
huffman@29462
  1369
    case True
huffman@29462
  1370
    then obtain a where "poly p a = 0" ..
huffman@29462
  1371
    then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd)
huffman@29462
  1372
    then obtain k where k: "p = [:-a, 1:] * k" ..
huffman@29462
  1373
    with `p \<noteq> 0` have "k \<noteq> 0" by auto
huffman@29462
  1374
    with k have "degree p = Suc (degree k)"
huffman@29462
  1375
      by (simp add: degree_mult_eq del: mult_pCons_left)
huffman@29462
  1376
    with `Suc n = degree p` have "n = degree k" by simp
berghofe@34915
  1377
    then have "finite {x. poly k x = 0}" using `k \<noteq> 0` by (rule Suc.hyps)
huffman@29462
  1378
    then have "finite (insert a {x. poly k x = 0})" by simp
huffman@29462
  1379
    then show "finite {x. poly p x = 0}"
huffman@29462
  1380
      by (simp add: k uminus_add_conv_diff Collect_disj_eq
huffman@29462
  1381
               del: mult_pCons_left)
huffman@29462
  1382
  qed
huffman@29462
  1383
qed
huffman@29462
  1384
huffman@29979
  1385
lemma poly_zero:
huffman@29979
  1386
  fixes p :: "'a::{idom,ring_char_0} poly"
huffman@29979
  1387
  shows "poly p = poly 0 \<longleftrightarrow> p = 0"
huffman@29979
  1388
apply (cases "p = 0", simp_all)
huffman@29979
  1389
apply (drule poly_roots_finite)
huffman@29979
  1390
apply (auto simp add: infinite_UNIV_char_0)
huffman@29979
  1391
done
huffman@29979
  1392
huffman@29979
  1393
lemma poly_eq_iff:
huffman@29979
  1394
  fixes p q :: "'a::{idom,ring_char_0} poly"
huffman@29979
  1395
  shows "poly p = poly q \<longleftrightarrow> p = q"
huffman@29979
  1396
  using poly_zero [of "p - q"]
nipkow@39302
  1397
  by (simp add: fun_eq_iff)
huffman@29979
  1398
huffman@29478
  1399
huffman@29980
  1400
subsection {* Composition of polynomials *}
huffman@29980
  1401
huffman@29980
  1402
definition
huffman@29980
  1403
  pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
huffman@29980
  1404
where
huffman@29980
  1405
  "pcompose p q = poly_rec 0 (\<lambda>a _ c. [:a:] + q * c) p"
huffman@29980
  1406
huffman@29980
  1407
lemma pcompose_0 [simp]: "pcompose 0 q = 0"
huffman@29980
  1408
  unfolding pcompose_def by (simp add: poly_rec_0)
huffman@29980
  1409
huffman@29980
  1410
lemma pcompose_pCons:
huffman@29980
  1411
  "pcompose (pCons a p) q = [:a:] + q * pcompose p q"
huffman@29980
  1412
  unfolding pcompose_def by (simp add: poly_rec_pCons)
huffman@29980
  1413
huffman@29980
  1414
lemma poly_pcompose: "poly (pcompose p q) x = poly p (poly q x)"
huffman@29980
  1415
  by (induct p) (simp_all add: pcompose_pCons)
huffman@29980
  1416
huffman@29980
  1417
lemma degree_pcompose_le:
huffman@29980
  1418
  "degree (pcompose p q) \<le> degree p * degree q"
huffman@29980
  1419
apply (induct p, simp)
huffman@29980
  1420
apply (simp add: pcompose_pCons, clarify)
huffman@29980
  1421
apply (rule degree_add_le, simp)
huffman@29980
  1422
apply (rule order_trans [OF degree_mult_le], simp)
huffman@29980
  1423
done
huffman@29980
  1424
huffman@29980
  1425
huffman@29977
  1426
subsection {* Order of polynomial roots *}
huffman@29977
  1427
huffman@29977
  1428
definition
huffman@29979
  1429
  order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat"
huffman@29977
  1430
where
huffman@29977
  1431
  "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
huffman@29977
  1432
huffman@29977
  1433
lemma coeff_linear_power:
huffman@29979
  1434
  fixes a :: "'a::comm_semiring_1"
huffman@29977
  1435
  shows "coeff ([:a, 1:] ^ n) n = 1"
huffman@29977
  1436
apply (induct n, simp_all)
huffman@29977
  1437
apply (subst coeff_eq_0)
huffman@29977
  1438
apply (auto intro: le_less_trans degree_power_le)
huffman@29977
  1439
done
huffman@29977
  1440
huffman@29977
  1441
lemma degree_linear_power:
huffman@29979
  1442
  fixes a :: "'a::comm_semiring_1"
huffman@29977
  1443
  shows "degree ([:a, 1:] ^ n) = n"
huffman@29977
  1444
apply (rule order_antisym)
huffman@29977
  1445
apply (rule ord_le_eq_trans [OF degree_power_le], simp)
huffman@29977
  1446
apply (rule le_degree, simp add: coeff_linear_power)
huffman@29977
  1447
done
huffman@29977
  1448
huffman@29977
  1449
lemma order_1: "[:-a, 1:] ^ order a p dvd p"
huffman@29977
  1450
apply (cases "p = 0", simp)
huffman@29977
  1451
apply (cases "order a p", simp)
huffman@29977
  1452
apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
huffman@29977
  1453
apply (drule not_less_Least, simp)
huffman@29977
  1454
apply (fold order_def, simp)
huffman@29977
  1455
done
huffman@29977
  1456
huffman@29977
  1457
lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
huffman@29977
  1458
unfolding order_def
huffman@29977
  1459
apply (rule LeastI_ex)
huffman@29977
  1460
apply (rule_tac x="degree p" in exI)
huffman@29977
  1461
apply (rule notI)
huffman@29977
  1462
apply (drule (1) dvd_imp_degree_le)
huffman@29977
  1463
apply (simp only: degree_linear_power)
huffman@29977
  1464
done
huffman@29977
  1465
huffman@29977
  1466
lemma order:
huffman@29977
  1467
  "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
huffman@29977
  1468
by (rule conjI [OF order_1 order_2])
huffman@29977
  1469
huffman@29977
  1470
lemma order_degree:
huffman@29977
  1471
  assumes p: "p \<noteq> 0"
huffman@29977
  1472
  shows "order a p \<le> degree p"
huffman@29977
  1473
proof -
huffman@29977
  1474
  have "order a p = degree ([:-a, 1:] ^ order a p)"
huffman@29977
  1475
    by (simp only: degree_linear_power)
huffman@29977
  1476
  also have "\<dots> \<le> degree p"
huffman@29977
  1477
    using order_1 p by (rule dvd_imp_degree_le)
huffman@29977
  1478
  finally show ?thesis .
huffman@29977
  1479
qed
huffman@29977
  1480
huffman@29977
  1481
lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
huffman@29977
  1482
apply (cases "p = 0", simp_all)
huffman@29977
  1483
apply (rule iffI)
huffman@29977
  1484
apply (rule ccontr, simp)
huffman@29977
  1485
apply (frule order_2 [where a=a], simp)
huffman@29977
  1486
apply (simp add: poly_eq_0_iff_dvd)
huffman@29977
  1487
apply (simp add: poly_eq_0_iff_dvd)
huffman@29977
  1488
apply (simp only: order_def)
huffman@29977
  1489
apply (drule not_less_Least, simp)
huffman@29977
  1490
done
huffman@29977
  1491
huffman@29977
  1492
huffman@29478
  1493
subsection {* Configuration of the code generator *}
huffman@29478
  1494
huffman@29478
  1495
code_datatype "0::'a::zero poly" pCons
huffman@29478
  1496
bulwahn@45928
  1497
quickcheck_generator poly constructors: "0::'a::zero poly", pCons
bulwahn@45928
  1498
haftmann@38857
  1499
instantiation poly :: ("{zero, equal}") equal
huffman@29478
  1500
begin
huffman@29478
  1501
haftmann@37765
  1502
definition
haftmann@38857
  1503
  "HOL.equal (p::'a poly) q \<longleftrightarrow> p = q"
huffman@29478
  1504
haftmann@38857
  1505
instance proof
haftmann@38857
  1506
qed (rule equal_poly_def)
huffman@29478
  1507
huffman@29451
  1508
end
huffman@29478
  1509
huffman@29478
  1510
lemma eq_poly_code [code]:
haftmann@38857
  1511
  "HOL.equal (0::_ poly) (0::_ poly) \<longleftrightarrow> True"
haftmann@38857
  1512
  "HOL.equal (0::_ poly) (pCons b q) \<longleftrightarrow> HOL.equal 0 b \<and> HOL.equal 0 q"
haftmann@38857
  1513
  "HOL.equal (pCons a p) (0::_ poly) \<longleftrightarrow> HOL.equal a 0 \<and> HOL.equal p 0"
haftmann@38857
  1514
  "HOL.equal (pCons a p) (pCons b q) \<longleftrightarrow> HOL.equal a b \<and> HOL.equal p q"
haftmann@38857
  1515
  by (simp_all add: equal)
haftmann@38857
  1516
haftmann@38857
  1517
lemma [code nbe]:
haftmann@38857
  1518
  "HOL.equal (p :: _ poly) p \<longleftrightarrow> True"
haftmann@38857
  1519
  by (fact equal_refl)
huffman@29478
  1520
huffman@29478
  1521
lemmas coeff_code [code] =
huffman@29478
  1522
  coeff_0 coeff_pCons_0 coeff_pCons_Suc
huffman@29478
  1523
huffman@29478
  1524
lemmas degree_code [code] =
huffman@29478
  1525
  degree_0 degree_pCons_eq_if
huffman@29478
  1526
huffman@29478
  1527
lemmas monom_poly_code [code] =
huffman@29478
  1528
  monom_0 monom_Suc
huffman@29478
  1529
huffman@29478
  1530
lemma add_poly_code [code]:
huffman@29478
  1531
  "0 + q = (q :: _ poly)"
huffman@29478
  1532
  "p + 0 = (p :: _ poly)"
huffman@29478
  1533
  "pCons a p + pCons b q = pCons (a + b) (p + q)"
huffman@29478
  1534
by simp_all
huffman@29478
  1535
huffman@29478
  1536
lemma minus_poly_code [code]:
huffman@29478
  1537
  "- 0 = (0 :: _ poly)"
huffman@29478
  1538
  "- pCons a p = pCons (- a) (- p)"
huffman@29478
  1539
by simp_all
huffman@29478
  1540
huffman@29478
  1541
lemma diff_poly_code [code]:
huffman@29478
  1542
  "0 - q = (- q :: _ poly)"
huffman@29478
  1543
  "p - 0 = (p :: _ poly)"
huffman@29478
  1544
  "pCons a p - pCons b q = pCons (a - b) (p - q)"
huffman@29478
  1545
by simp_all
huffman@29478
  1546
huffman@29478
  1547
lemmas smult_poly_code [code] =
huffman@29478
  1548
  smult_0_right smult_pCons
huffman@29478
  1549
huffman@29478
  1550
lemma mult_poly_code [code]:
huffman@29478
  1551
  "0 * q = (0 :: _ poly)"
huffman@29478
  1552
  "pCons a p * q = smult a q + pCons 0 (p * q)"
huffman@29478
  1553
by simp_all
huffman@29478
  1554
huffman@29478
  1555
lemmas poly_code [code] =
huffman@29478
  1556
  poly_0 poly_pCons
huffman@29478
  1557
huffman@29478
  1558
lemmas synthetic_divmod_code [code] =
huffman@29478
  1559
  synthetic_divmod_0 synthetic_divmod_pCons
huffman@29478
  1560
huffman@29478
  1561
text {* code generator setup for div and mod *}
huffman@29478
  1562
huffman@29478
  1563
definition
huffman@29537
  1564
  pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly"
huffman@29478
  1565
where
haftmann@37765
  1566
  "pdivmod x y = (x div y, x mod y)"
huffman@29478
  1567
huffman@29537
  1568
lemma div_poly_code [code]: "x div y = fst (pdivmod x y)"
huffman@29537
  1569
  unfolding pdivmod_def by simp
huffman@29478
  1570
huffman@29537
  1571
lemma mod_poly_code [code]: "x mod y = snd (pdivmod x y)"
huffman@29537
  1572
  unfolding pdivmod_def by simp
huffman@29478
  1573
huffman@29537
  1574
lemma pdivmod_0 [code]: "pdivmod 0 y = (0, 0)"
huffman@29537
  1575
  unfolding pdivmod_def by simp
huffman@29478
  1576
huffman@29537
  1577
lemma pdivmod_pCons [code]:
huffman@29537
  1578
  "pdivmod (pCons a x) y =
huffman@29478
  1579
    (if y = 0 then (0, pCons a x) else
huffman@29537
  1580
      (let (q, r) = pdivmod x y;
huffman@29478
  1581
           b = coeff (pCons a r) (degree y) / coeff y (degree y)
huffman@29478
  1582
        in (pCons b q, pCons a r - smult b y)))"
huffman@29537
  1583
apply (simp add: pdivmod_def Let_def, safe)
huffman@29478
  1584
apply (rule div_poly_eq)
huffman@29537
  1585
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
huffman@29478
  1586
apply (rule mod_poly_eq)
huffman@29537
  1587
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
huffman@29478
  1588
done
huffman@29478
  1589
huffman@31663
  1590
lemma poly_gcd_code [code]:
huffman@31663
  1591
  "poly_gcd x y =
huffman@31663
  1592
    (if y = 0 then smult (inverse (coeff x (degree x))) x
huffman@31663
  1593
              else poly_gcd y (x mod y))"
huffman@31663
  1594
  by (simp add: poly_gcd.simps)
huffman@31663
  1595
huffman@29478
  1596
end