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