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