src/HOL/Library/Polynomial.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 22 14:37:56 2016 +0000 (2016-02-22)
changeset 62379 340738057c8c
parent 62352 35a9e1cbb5b3
child 62422 4aa35fd6c152
permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
wenzelm@41959
     1
(*  Title:      HOL/Library/Polynomial.thy
huffman@29451
     2
    Author:     Brian Huffman
wenzelm@41959
     3
    Author:     Clemens Ballarin
haftmann@62352
     4
    Author:     Amine Chaieb
haftmann@52380
     5
    Author:     Florian Haftmann
huffman@29451
     6
*)
huffman@29451
     7
wenzelm@60500
     8
section \<open>Polynomials as type over a ring structure\<close>
huffman@29451
     9
huffman@29451
    10
theory Polynomial
haftmann@62352
    11
imports Main "~~/src/HOL/Deriv" "~~/src/HOL/Library/More_List"
haftmann@62352
    12
  "~~/src/HOL/Library/Infinite_Set"
huffman@29451
    13
begin
huffman@29451
    14
wenzelm@60500
    15
subsection \<open>Auxiliary: operations for lists (later) representing coefficients\<close>
haftmann@52380
    16
haftmann@52380
    17
definition cCons :: "'a::zero \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixr "##" 65)
haftmann@52380
    18
where
haftmann@52380
    19
  "x ## xs = (if xs = [] \<and> x = 0 then [] else x # xs)"
haftmann@52380
    20
haftmann@52380
    21
lemma cCons_0_Nil_eq [simp]:
haftmann@52380
    22
  "0 ## [] = []"
haftmann@52380
    23
  by (simp add: cCons_def)
haftmann@52380
    24
haftmann@52380
    25
lemma cCons_Cons_eq [simp]:
haftmann@52380
    26
  "x ## y # ys = x # y # ys"
haftmann@52380
    27
  by (simp add: cCons_def)
haftmann@52380
    28
haftmann@52380
    29
lemma cCons_append_Cons_eq [simp]:
haftmann@52380
    30
  "x ## xs @ y # ys = x # xs @ y # ys"
haftmann@52380
    31
  by (simp add: cCons_def)
haftmann@52380
    32
haftmann@52380
    33
lemma cCons_not_0_eq [simp]:
haftmann@52380
    34
  "x \<noteq> 0 \<Longrightarrow> x ## xs = x # xs"
haftmann@52380
    35
  by (simp add: cCons_def)
haftmann@52380
    36
haftmann@52380
    37
lemma strip_while_not_0_Cons_eq [simp]:
haftmann@52380
    38
  "strip_while (\<lambda>x. x = 0) (x # xs) = x ## strip_while (\<lambda>x. x = 0) xs"
haftmann@52380
    39
proof (cases "x = 0")
haftmann@52380
    40
  case False then show ?thesis by simp
haftmann@52380
    41
next
haftmann@52380
    42
  case True show ?thesis
haftmann@52380
    43
  proof (induct xs rule: rev_induct)
haftmann@52380
    44
    case Nil with True show ?case by simp
haftmann@52380
    45
  next
haftmann@52380
    46
    case (snoc y ys) then show ?case
haftmann@52380
    47
      by (cases "y = 0") (simp_all add: append_Cons [symmetric] del: append_Cons)
haftmann@52380
    48
  qed
haftmann@52380
    49
qed
haftmann@52380
    50
haftmann@52380
    51
lemma tl_cCons [simp]:
haftmann@52380
    52
  "tl (x ## xs) = xs"
haftmann@52380
    53
  by (simp add: cCons_def)
haftmann@52380
    54
wenzelm@61585
    55
subsection \<open>Definition of type \<open>poly\<close>\<close>
huffman@29451
    56
wenzelm@61260
    57
typedef (overloaded) 'a poly = "{f :: nat \<Rightarrow> 'a::zero. \<forall>\<^sub>\<infinity> n. f n = 0}"
hoelzl@59983
    58
  morphisms coeff Abs_poly by (auto intro!: ALL_MOST)
huffman@29451
    59
haftmann@59487
    60
setup_lifting type_definition_poly
haftmann@52380
    61
haftmann@52380
    62
lemma poly_eq_iff: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)"
wenzelm@45694
    63
  by (simp add: coeff_inject [symmetric] fun_eq_iff)
huffman@29451
    64
haftmann@52380
    65
lemma poly_eqI: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
haftmann@52380
    66
  by (simp add: poly_eq_iff)
haftmann@52380
    67
hoelzl@59983
    68
lemma MOST_coeff_eq_0: "\<forall>\<^sub>\<infinity> n. coeff p n = 0"
haftmann@52380
    69
  using coeff [of p] by simp
huffman@29451
    70
huffman@29451
    71
wenzelm@60500
    72
subsection \<open>Degree of a polynomial\<close>
huffman@29451
    73
haftmann@52380
    74
definition degree :: "'a::zero poly \<Rightarrow> nat"
haftmann@52380
    75
where
huffman@29451
    76
  "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
huffman@29451
    77
haftmann@52380
    78
lemma coeff_eq_0:
haftmann@52380
    79
  assumes "degree p < n"
haftmann@52380
    80
  shows "coeff p n = 0"
huffman@29451
    81
proof -
hoelzl@59983
    82
  have "\<exists>n. \<forall>i>n. coeff p i = 0"
hoelzl@59983
    83
    using MOST_coeff_eq_0 by (simp add: MOST_nat)
haftmann@52380
    84
  then have "\<forall>i>degree p. coeff p i = 0"
huffman@29451
    85
    unfolding degree_def by (rule LeastI_ex)
haftmann@52380
    86
  with assms show ?thesis by simp
huffman@29451
    87
qed
huffman@29451
    88
huffman@29451
    89
lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
huffman@29451
    90
  by (erule contrapos_np, rule coeff_eq_0, simp)
huffman@29451
    91
huffman@29451
    92
lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
huffman@29451
    93
  unfolding degree_def by (erule Least_le)
huffman@29451
    94
huffman@29451
    95
lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0"
huffman@29451
    96
  unfolding degree_def by (drule not_less_Least, simp)
huffman@29451
    97
huffman@29451
    98
wenzelm@60500
    99
subsection \<open>The zero polynomial\<close>
huffman@29451
   100
huffman@29451
   101
instantiation poly :: (zero) zero
huffman@29451
   102
begin
huffman@29451
   103
haftmann@52380
   104
lift_definition zero_poly :: "'a poly"
hoelzl@59983
   105
  is "\<lambda>_. 0" by (rule MOST_I) simp
huffman@29451
   106
huffman@29451
   107
instance ..
haftmann@52380
   108
huffman@29451
   109
end
huffman@29451
   110
haftmann@52380
   111
lemma coeff_0 [simp]:
haftmann@52380
   112
  "coeff 0 n = 0"
haftmann@52380
   113
  by transfer rule
huffman@29451
   114
haftmann@52380
   115
lemma degree_0 [simp]:
haftmann@52380
   116
  "degree 0 = 0"
huffman@29451
   117
  by (rule order_antisym [OF degree_le le0]) simp
huffman@29451
   118
huffman@29451
   119
lemma leading_coeff_neq_0:
haftmann@52380
   120
  assumes "p \<noteq> 0"
haftmann@52380
   121
  shows "coeff p (degree p) \<noteq> 0"
huffman@29451
   122
proof (cases "degree p")
huffman@29451
   123
  case 0
wenzelm@60500
   124
  from \<open>p \<noteq> 0\<close> have "\<exists>n. coeff p n \<noteq> 0"
haftmann@52380
   125
    by (simp add: poly_eq_iff)
huffman@29451
   126
  then obtain n where "coeff p n \<noteq> 0" ..
huffman@29451
   127
  hence "n \<le> degree p" by (rule le_degree)
wenzelm@60500
   128
  with \<open>coeff p n \<noteq> 0\<close> and \<open>degree p = 0\<close>
huffman@29451
   129
  show "coeff p (degree p) \<noteq> 0" by simp
huffman@29451
   130
next
huffman@29451
   131
  case (Suc n)
wenzelm@60500
   132
  from \<open>degree p = Suc n\<close> have "n < degree p" by simp
huffman@29451
   133
  hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp)
huffman@29451
   134
  then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast
wenzelm@60500
   135
  from \<open>degree p = Suc n\<close> and \<open>n < i\<close> have "degree p \<le> i" by simp
wenzelm@60500
   136
  also from \<open>coeff p i \<noteq> 0\<close> have "i \<le> degree p" by (rule le_degree)
huffman@29451
   137
  finally have "degree p = i" .
wenzelm@60500
   138
  with \<open>coeff p i \<noteq> 0\<close> show "coeff p (degree p) \<noteq> 0" by simp
huffman@29451
   139
qed
huffman@29451
   140
haftmann@52380
   141
lemma leading_coeff_0_iff [simp]:
haftmann@52380
   142
  "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
huffman@29451
   143
  by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
huffman@29451
   144
huffman@29451
   145
wenzelm@60500
   146
subsection \<open>List-style constructor for polynomials\<close>
huffman@29451
   147
haftmann@52380
   148
lift_definition pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
blanchet@55415
   149
  is "\<lambda>a p. case_nat a (coeff p)"
hoelzl@59983
   150
  by (rule MOST_SucD) (simp add: MOST_coeff_eq_0)
huffman@29451
   151
haftmann@52380
   152
lemmas coeff_pCons = pCons.rep_eq
huffman@29455
   153
haftmann@52380
   154
lemma coeff_pCons_0 [simp]:
haftmann@52380
   155
  "coeff (pCons a p) 0 = a"
haftmann@52380
   156
  by transfer simp
huffman@29455
   157
haftmann@52380
   158
lemma coeff_pCons_Suc [simp]:
haftmann@52380
   159
  "coeff (pCons a p) (Suc n) = coeff p n"
huffman@29451
   160
  by (simp add: coeff_pCons)
huffman@29451
   161
haftmann@52380
   162
lemma degree_pCons_le:
haftmann@52380
   163
  "degree (pCons a p) \<le> Suc (degree p)"
haftmann@52380
   164
  by (rule degree_le) (simp add: coeff_eq_0 coeff_pCons split: nat.split)
huffman@29451
   165
huffman@29451
   166
lemma degree_pCons_eq:
huffman@29451
   167
  "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
haftmann@52380
   168
  apply (rule order_antisym [OF degree_pCons_le])
haftmann@52380
   169
  apply (rule le_degree, simp)
haftmann@52380
   170
  done
huffman@29451
   171
haftmann@52380
   172
lemma degree_pCons_0:
haftmann@52380
   173
  "degree (pCons a 0) = 0"
haftmann@52380
   174
  apply (rule order_antisym [OF _ le0])
haftmann@52380
   175
  apply (rule degree_le, simp add: coeff_pCons split: nat.split)
haftmann@52380
   176
  done
huffman@29451
   177
huffman@29460
   178
lemma degree_pCons_eq_if [simp]:
huffman@29451
   179
  "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
haftmann@52380
   180
  apply (cases "p = 0", simp_all)
haftmann@52380
   181
  apply (rule order_antisym [OF _ le0])
haftmann@52380
   182
  apply (rule degree_le, simp add: coeff_pCons split: nat.split)
haftmann@52380
   183
  apply (rule order_antisym [OF degree_pCons_le])
haftmann@52380
   184
  apply (rule le_degree, simp)
haftmann@52380
   185
  done
huffman@29451
   186
haftmann@52380
   187
lemma pCons_0_0 [simp]:
haftmann@52380
   188
  "pCons 0 0 = 0"
haftmann@52380
   189
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
huffman@29451
   190
huffman@29451
   191
lemma pCons_eq_iff [simp]:
huffman@29451
   192
  "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
haftmann@52380
   193
proof safe
huffman@29451
   194
  assume "pCons a p = pCons b q"
huffman@29451
   195
  then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
huffman@29451
   196
  then show "a = b" by simp
huffman@29451
   197
next
huffman@29451
   198
  assume "pCons a p = pCons b q"
huffman@29451
   199
  then have "\<forall>n. coeff (pCons a p) (Suc n) =
huffman@29451
   200
                 coeff (pCons b q) (Suc n)" by simp
haftmann@52380
   201
  then show "p = q" by (simp add: poly_eq_iff)
huffman@29451
   202
qed
huffman@29451
   203
haftmann@52380
   204
lemma pCons_eq_0_iff [simp]:
haftmann@52380
   205
  "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
huffman@29451
   206
  using pCons_eq_iff [of a p 0 0] by simp
huffman@29451
   207
huffman@29451
   208
lemma pCons_cases [cases type: poly]:
huffman@29451
   209
  obtains (pCons) a q where "p = pCons a q"
huffman@29451
   210
proof
huffman@29451
   211
  show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
haftmann@52380
   212
    by transfer
hoelzl@59983
   213
       (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
   214
                 split: nat.split)
huffman@29451
   215
qed
huffman@29451
   216
huffman@29451
   217
lemma pCons_induct [case_names 0 pCons, induct type: poly]:
huffman@29451
   218
  assumes zero: "P 0"
haftmann@54856
   219
  assumes pCons: "\<And>a p. a \<noteq> 0 \<or> p \<noteq> 0 \<Longrightarrow> P p \<Longrightarrow> P (pCons a p)"
huffman@29451
   220
  shows "P p"
huffman@29451
   221
proof (induct p rule: measure_induct_rule [where f=degree])
huffman@29451
   222
  case (less p)
huffman@29451
   223
  obtain a q where "p = pCons a q" by (rule pCons_cases)
huffman@29451
   224
  have "P q"
huffman@29451
   225
  proof (cases "q = 0")
huffman@29451
   226
    case True
huffman@29451
   227
    then show "P q" by (simp add: zero)
huffman@29451
   228
  next
huffman@29451
   229
    case False
huffman@29451
   230
    then have "degree (pCons a q) = Suc (degree q)"
huffman@29451
   231
      by (rule degree_pCons_eq)
huffman@29451
   232
    then have "degree q < degree p"
wenzelm@60500
   233
      using \<open>p = pCons a q\<close> by simp
huffman@29451
   234
    then show "P q"
huffman@29451
   235
      by (rule less.hyps)
huffman@29451
   236
  qed
haftmann@54856
   237
  have "P (pCons a q)"
haftmann@54856
   238
  proof (cases "a \<noteq> 0 \<or> q \<noteq> 0")
haftmann@54856
   239
    case True
wenzelm@60500
   240
    with \<open>P q\<close> show ?thesis by (auto intro: pCons)
haftmann@54856
   241
  next
haftmann@54856
   242
    case False
haftmann@54856
   243
    with zero show ?thesis by simp
haftmann@54856
   244
  qed
huffman@29451
   245
  then show ?case
wenzelm@60500
   246
    using \<open>p = pCons a q\<close> by simp
huffman@29451
   247
qed
huffman@29451
   248
haftmann@60570
   249
lemma degree_eq_zeroE:
haftmann@60570
   250
  fixes p :: "'a::zero poly"
haftmann@60570
   251
  assumes "degree p = 0"
haftmann@60570
   252
  obtains a where "p = pCons a 0"
haftmann@60570
   253
proof -
haftmann@60570
   254
  obtain a q where p: "p = pCons a q" by (cases p)
haftmann@60570
   255
  with assms have "q = 0" by (cases "q = 0") simp_all
haftmann@60570
   256
  with p have "p = pCons a 0" by simp
haftmann@60570
   257
  with that show thesis .
haftmann@60570
   258
qed
haftmann@60570
   259
huffman@29451
   260
wenzelm@60500
   261
subsection \<open>List-style syntax for polynomials\<close>
haftmann@52380
   262
haftmann@52380
   263
syntax
haftmann@52380
   264
  "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
haftmann@52380
   265
haftmann@52380
   266
translations
haftmann@52380
   267
  "[:x, xs:]" == "CONST pCons x [:xs:]"
haftmann@52380
   268
  "[:x:]" == "CONST pCons x 0"
haftmann@52380
   269
  "[:x:]" <= "CONST pCons x (_constrain 0 t)"
haftmann@52380
   270
haftmann@52380
   271
wenzelm@60500
   272
subsection \<open>Representation of polynomials by lists of coefficients\<close>
haftmann@52380
   273
haftmann@52380
   274
primrec Poly :: "'a::zero list \<Rightarrow> 'a poly"
haftmann@52380
   275
where
haftmann@54855
   276
  [code_post]: "Poly [] = 0"
haftmann@54855
   277
| [code_post]: "Poly (a # as) = pCons a (Poly as)"
haftmann@52380
   278
haftmann@52380
   279
lemma Poly_replicate_0 [simp]:
haftmann@52380
   280
  "Poly (replicate n 0) = 0"
haftmann@52380
   281
  by (induct n) simp_all
haftmann@52380
   282
haftmann@52380
   283
lemma Poly_eq_0:
haftmann@52380
   284
  "Poly as = 0 \<longleftrightarrow> (\<exists>n. as = replicate n 0)"
haftmann@52380
   285
  by (induct as) (auto simp add: Cons_replicate_eq)
eberlm@62065
   286
  
eberlm@62065
   287
lemma degree_Poly: "degree (Poly xs) \<le> length xs"
eberlm@62065
   288
  by (induction xs) simp_all
eberlm@62065
   289
  
haftmann@52380
   290
definition coeffs :: "'a poly \<Rightarrow> 'a::zero list"
haftmann@52380
   291
where
haftmann@52380
   292
  "coeffs p = (if p = 0 then [] else map (\<lambda>i. coeff p i) [0 ..< Suc (degree p)])"
haftmann@52380
   293
haftmann@52380
   294
lemma coeffs_eq_Nil [simp]:
haftmann@52380
   295
  "coeffs p = [] \<longleftrightarrow> p = 0"
haftmann@52380
   296
  by (simp add: coeffs_def)
haftmann@52380
   297
haftmann@52380
   298
lemma not_0_coeffs_not_Nil:
haftmann@52380
   299
  "p \<noteq> 0 \<Longrightarrow> coeffs p \<noteq> []"
haftmann@52380
   300
  by simp
haftmann@52380
   301
haftmann@52380
   302
lemma coeffs_0_eq_Nil [simp]:
haftmann@52380
   303
  "coeffs 0 = []"
haftmann@52380
   304
  by simp
huffman@29454
   305
haftmann@52380
   306
lemma coeffs_pCons_eq_cCons [simp]:
haftmann@52380
   307
  "coeffs (pCons a p) = a ## coeffs p"
haftmann@52380
   308
proof -
haftmann@52380
   309
  { fix ms :: "nat list" and f :: "nat \<Rightarrow> 'a" and x :: "'a"
haftmann@52380
   310
    assume "\<forall>m\<in>set ms. m > 0"
blanchet@55415
   311
    then have "map (case_nat x f) ms = map f (map (\<lambda>n. n - 1) ms)"
haftmann@58199
   312
      by (induct ms) (auto split: nat.split)
haftmann@58199
   313
  }
haftmann@52380
   314
  note * = this
haftmann@52380
   315
  show ?thesis
haftmann@60570
   316
    by (simp add: coeffs_def * upt_conv_Cons coeff_pCons map_decr_upt del: upt_Suc)
haftmann@52380
   317
qed
haftmann@52380
   318
eberlm@62065
   319
lemma length_coeffs: "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = degree p + 1"
eberlm@62065
   320
  by (simp add: coeffs_def)
eberlm@62065
   321
  
eberlm@62065
   322
lemma coeffs_nth:
eberlm@62065
   323
  assumes "p \<noteq> 0" "n \<le> degree p"
eberlm@62065
   324
  shows   "coeffs p ! n = coeff p n"
eberlm@62065
   325
  using assms unfolding coeffs_def by (auto simp del: upt_Suc)
eberlm@62065
   326
haftmann@52380
   327
lemma not_0_cCons_eq [simp]:
haftmann@52380
   328
  "p \<noteq> 0 \<Longrightarrow> a ## coeffs p = a # coeffs p"
haftmann@52380
   329
  by (simp add: cCons_def)
haftmann@52380
   330
haftmann@52380
   331
lemma Poly_coeffs [simp, code abstype]:
haftmann@52380
   332
  "Poly (coeffs p) = p"
haftmann@54856
   333
  by (induct p) auto
haftmann@52380
   334
haftmann@52380
   335
lemma coeffs_Poly [simp]:
haftmann@52380
   336
  "coeffs (Poly as) = strip_while (HOL.eq 0) as"
haftmann@52380
   337
proof (induct as)
haftmann@52380
   338
  case Nil then show ?case by simp
haftmann@52380
   339
next
haftmann@52380
   340
  case (Cons a as)
haftmann@52380
   341
  have "(\<forall>n. as \<noteq> replicate n 0) \<longleftrightarrow> (\<exists>a\<in>set as. a \<noteq> 0)"
haftmann@52380
   342
    using replicate_length_same [of as 0] by (auto dest: sym [of _ as])
haftmann@52380
   343
  with Cons show ?case by auto
haftmann@52380
   344
qed
haftmann@52380
   345
haftmann@52380
   346
lemma last_coeffs_not_0:
haftmann@52380
   347
  "p \<noteq> 0 \<Longrightarrow> last (coeffs p) \<noteq> 0"
haftmann@52380
   348
  by (induct p) (auto simp add: cCons_def)
haftmann@52380
   349
haftmann@52380
   350
lemma strip_while_coeffs [simp]:
haftmann@52380
   351
  "strip_while (HOL.eq 0) (coeffs p) = coeffs p"
haftmann@52380
   352
  by (cases "p = 0") (auto dest: last_coeffs_not_0 intro: strip_while_not_last)
haftmann@52380
   353
haftmann@52380
   354
lemma coeffs_eq_iff:
haftmann@52380
   355
  "p = q \<longleftrightarrow> coeffs p = coeffs q" (is "?P \<longleftrightarrow> ?Q")
haftmann@52380
   356
proof
haftmann@52380
   357
  assume ?P then show ?Q by simp
haftmann@52380
   358
next
haftmann@52380
   359
  assume ?Q
haftmann@52380
   360
  then have "Poly (coeffs p) = Poly (coeffs q)" by simp
haftmann@52380
   361
  then show ?P by simp
haftmann@52380
   362
qed
haftmann@52380
   363
haftmann@52380
   364
lemma coeff_Poly_eq:
haftmann@52380
   365
  "coeff (Poly xs) n = nth_default 0 xs n"
haftmann@52380
   366
  apply (induct xs arbitrary: n) apply simp_all
blanchet@55642
   367
  by (metis nat.case not0_implies_Suc nth_default_Cons_0 nth_default_Cons_Suc pCons.rep_eq)
huffman@29454
   368
haftmann@52380
   369
lemma nth_default_coeffs_eq:
haftmann@52380
   370
  "nth_default 0 (coeffs p) = coeff p"
haftmann@52380
   371
  by (simp add: fun_eq_iff coeff_Poly_eq [symmetric])
haftmann@52380
   372
haftmann@52380
   373
lemma [code]:
haftmann@52380
   374
  "coeff p = nth_default 0 (coeffs p)"
haftmann@52380
   375
  by (simp add: nth_default_coeffs_eq)
haftmann@52380
   376
haftmann@52380
   377
lemma coeffs_eqI:
haftmann@52380
   378
  assumes coeff: "\<And>n. coeff p n = nth_default 0 xs n"
haftmann@52380
   379
  assumes zero: "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0"
haftmann@52380
   380
  shows "coeffs p = xs"
haftmann@52380
   381
proof -
haftmann@52380
   382
  from coeff have "p = Poly xs" by (simp add: poly_eq_iff coeff_Poly_eq)
haftmann@52380
   383
  with zero show ?thesis by simp (cases xs, simp_all)
haftmann@52380
   384
qed
haftmann@52380
   385
haftmann@52380
   386
lemma degree_eq_length_coeffs [code]:
haftmann@52380
   387
  "degree p = length (coeffs p) - 1"
haftmann@52380
   388
  by (simp add: coeffs_def)
haftmann@52380
   389
haftmann@52380
   390
lemma length_coeffs_degree:
haftmann@52380
   391
  "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = Suc (degree p)"
haftmann@52380
   392
  by (induct p) (auto simp add: cCons_def)
haftmann@52380
   393
haftmann@52380
   394
lemma [code abstract]:
haftmann@52380
   395
  "coeffs 0 = []"
haftmann@52380
   396
  by (fact coeffs_0_eq_Nil)
haftmann@52380
   397
haftmann@52380
   398
lemma [code abstract]:
haftmann@52380
   399
  "coeffs (pCons a p) = a ## coeffs p"
haftmann@52380
   400
  by (fact coeffs_pCons_eq_cCons)
haftmann@52380
   401
haftmann@52380
   402
instantiation poly :: ("{zero, equal}") equal
haftmann@52380
   403
begin
haftmann@52380
   404
haftmann@52380
   405
definition
haftmann@52380
   406
  [code]: "HOL.equal (p::'a poly) q \<longleftrightarrow> HOL.equal (coeffs p) (coeffs q)"
haftmann@52380
   407
wenzelm@60679
   408
instance
wenzelm@60679
   409
  by standard (simp add: equal equal_poly_def coeffs_eq_iff)
haftmann@52380
   410
haftmann@52380
   411
end
haftmann@52380
   412
wenzelm@60679
   413
lemma [code nbe]: "HOL.equal (p :: _ poly) p \<longleftrightarrow> True"
haftmann@52380
   414
  by (fact equal_refl)
huffman@29454
   415
haftmann@52380
   416
definition is_zero :: "'a::zero poly \<Rightarrow> bool"
haftmann@52380
   417
where
haftmann@52380
   418
  [code]: "is_zero p \<longleftrightarrow> List.null (coeffs p)"
haftmann@52380
   419
haftmann@52380
   420
lemma is_zero_null [code_abbrev]:
haftmann@52380
   421
  "is_zero p \<longleftrightarrow> p = 0"
haftmann@52380
   422
  by (simp add: is_zero_def null_def)
haftmann@52380
   423
haftmann@52380
   424
wenzelm@60500
   425
subsection \<open>Fold combinator for polynomials\<close>
haftmann@52380
   426
haftmann@52380
   427
definition fold_coeffs :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b"
haftmann@52380
   428
where
haftmann@52380
   429
  "fold_coeffs f p = foldr f (coeffs p)"
haftmann@52380
   430
haftmann@52380
   431
lemma fold_coeffs_0_eq [simp]:
haftmann@52380
   432
  "fold_coeffs f 0 = id"
haftmann@52380
   433
  by (simp add: fold_coeffs_def)
haftmann@52380
   434
haftmann@52380
   435
lemma fold_coeffs_pCons_eq [simp]:
haftmann@52380
   436
  "f 0 = id \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
haftmann@52380
   437
  by (simp add: fold_coeffs_def cCons_def fun_eq_iff)
huffman@29454
   438
haftmann@52380
   439
lemma fold_coeffs_pCons_0_0_eq [simp]:
haftmann@52380
   440
  "fold_coeffs f (pCons 0 0) = id"
haftmann@52380
   441
  by (simp add: fold_coeffs_def)
haftmann@52380
   442
haftmann@52380
   443
lemma fold_coeffs_pCons_coeff_not_0_eq [simp]:
haftmann@52380
   444
  "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
haftmann@52380
   445
  by (simp add: fold_coeffs_def)
haftmann@52380
   446
haftmann@52380
   447
lemma fold_coeffs_pCons_not_0_0_eq [simp]:
haftmann@52380
   448
  "p \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
haftmann@52380
   449
  by (simp add: fold_coeffs_def)
haftmann@52380
   450
haftmann@52380
   451
wenzelm@60500
   452
subsection \<open>Canonical morphism on polynomials -- evaluation\<close>
haftmann@52380
   453
haftmann@52380
   454
definition poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@52380
   455
where
wenzelm@61585
   456
  "poly p = fold_coeffs (\<lambda>a f x. a + x * f x) p (\<lambda>x. 0)" \<comment> \<open>The Horner Schema\<close>
haftmann@52380
   457
haftmann@52380
   458
lemma poly_0 [simp]:
haftmann@52380
   459
  "poly 0 x = 0"
haftmann@52380
   460
  by (simp add: poly_def)
eberlm@62128
   461
  
haftmann@52380
   462
lemma poly_pCons [simp]:
haftmann@52380
   463
  "poly (pCons a p) x = a + x * poly p x"
haftmann@52380
   464
  by (cases "p = 0 \<and> a = 0") (auto simp add: poly_def)
huffman@29454
   465
eberlm@62065
   466
lemma poly_altdef: 
eberlm@62065
   467
  "poly p (x :: 'a :: {comm_semiring_0, semiring_1}) = (\<Sum>i\<le>degree p. coeff p i * x ^ i)"
eberlm@62065
   468
proof (induction p rule: pCons_induct)
eberlm@62065
   469
  case (pCons a p)
eberlm@62065
   470
    show ?case
eberlm@62065
   471
    proof (cases "p = 0")
eberlm@62065
   472
      case False
eberlm@62065
   473
      let ?p' = "pCons a p"
eberlm@62065
   474
      note poly_pCons[of a p x]
eberlm@62065
   475
      also note pCons.IH
eberlm@62065
   476
      also have "a + x * (\<Sum>i\<le>degree p. coeff p i * x ^ i) =
eberlm@62065
   477
                 coeff ?p' 0 * x^0 + (\<Sum>i\<le>degree p. coeff ?p' (Suc i) * x^Suc i)"
eberlm@62065
   478
          by (simp add: field_simps setsum_right_distrib coeff_pCons)
eberlm@62065
   479
      also note setsum_atMost_Suc_shift[symmetric]
wenzelm@62072
   480
      also note degree_pCons_eq[OF \<open>p \<noteq> 0\<close>, of a, symmetric]
eberlm@62065
   481
      finally show ?thesis .
eberlm@62065
   482
   qed simp
eberlm@62065
   483
qed simp
eberlm@62065
   484
eberlm@62128
   485
lemma poly_0_coeff_0: "poly p 0 = coeff p 0"
eberlm@62128
   486
  by (cases p) (auto simp: poly_altdef)
eberlm@62128
   487
huffman@29454
   488
wenzelm@60500
   489
subsection \<open>Monomials\<close>
huffman@29451
   490
haftmann@52380
   491
lift_definition monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly"
haftmann@52380
   492
  is "\<lambda>a m n. if m = n then a else 0"
hoelzl@59983
   493
  by (simp add: MOST_iff_cofinite)
haftmann@52380
   494
haftmann@52380
   495
lemma coeff_monom [simp]:
haftmann@52380
   496
  "coeff (monom a m) n = (if m = n then a else 0)"
haftmann@52380
   497
  by transfer rule
huffman@29451
   498
haftmann@52380
   499
lemma monom_0:
haftmann@52380
   500
  "monom a 0 = pCons a 0"
haftmann@52380
   501
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
huffman@29451
   502
haftmann@52380
   503
lemma monom_Suc:
haftmann@52380
   504
  "monom a (Suc n) = pCons 0 (monom a n)"
haftmann@52380
   505
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
huffman@29451
   506
huffman@29451
   507
lemma monom_eq_0 [simp]: "monom 0 n = 0"
haftmann@52380
   508
  by (rule poly_eqI) simp
huffman@29451
   509
huffman@29451
   510
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
haftmann@52380
   511
  by (simp add: poly_eq_iff)
huffman@29451
   512
huffman@29451
   513
lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
haftmann@52380
   514
  by (simp add: poly_eq_iff)
huffman@29451
   515
huffman@29451
   516
lemma degree_monom_le: "degree (monom a n) \<le> n"
huffman@29451
   517
  by (rule degree_le, simp)
huffman@29451
   518
huffman@29451
   519
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
huffman@29451
   520
  apply (rule order_antisym [OF degree_monom_le])
huffman@29451
   521
  apply (rule le_degree, simp)
huffman@29451
   522
  done
huffman@29451
   523
haftmann@52380
   524
lemma coeffs_monom [code abstract]:
haftmann@52380
   525
  "coeffs (monom a n) = (if a = 0 then [] else replicate n 0 @ [a])"
haftmann@52380
   526
  by (induct n) (simp_all add: monom_0 monom_Suc)
haftmann@52380
   527
haftmann@52380
   528
lemma fold_coeffs_monom [simp]:
haftmann@52380
   529
  "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (monom a n) = f 0 ^^ n \<circ> f a"
haftmann@52380
   530
  by (simp add: fold_coeffs_def coeffs_monom fun_eq_iff)
haftmann@52380
   531
haftmann@52380
   532
lemma poly_monom:
haftmann@52380
   533
  fixes a x :: "'a::{comm_semiring_1}"
haftmann@52380
   534
  shows "poly (monom a n) x = a * x ^ n"
haftmann@52380
   535
  by (cases "a = 0", simp_all)
haftmann@52380
   536
    (induct n, simp_all add: mult.left_commute poly_def)
haftmann@52380
   537
eberlm@62065
   538
    
wenzelm@60500
   539
subsection \<open>Addition and subtraction\<close>
huffman@29451
   540
huffman@29451
   541
instantiation poly :: (comm_monoid_add) comm_monoid_add
huffman@29451
   542
begin
huffman@29451
   543
haftmann@52380
   544
lift_definition plus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
haftmann@52380
   545
  is "\<lambda>p q n. coeff p n + coeff q n"
hoelzl@60040
   546
proof -
wenzelm@60679
   547
  fix q p :: "'a poly"
wenzelm@60679
   548
  show "\<forall>\<^sub>\<infinity>n. coeff p n + coeff q n = 0"
hoelzl@60040
   549
    using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp
haftmann@52380
   550
qed
huffman@29451
   551
wenzelm@60679
   552
lemma coeff_add [simp]: "coeff (p + q) n = coeff p n + coeff q n"
haftmann@52380
   553
  by (simp add: plus_poly.rep_eq)
huffman@29451
   554
wenzelm@60679
   555
instance
wenzelm@60679
   556
proof
huffman@29451
   557
  fix p q r :: "'a poly"
huffman@29451
   558
  show "(p + q) + r = p + (q + r)"
haftmann@57512
   559
    by (simp add: poly_eq_iff add.assoc)
huffman@29451
   560
  show "p + q = q + p"
haftmann@57512
   561
    by (simp add: poly_eq_iff add.commute)
huffman@29451
   562
  show "0 + p = p"
haftmann@52380
   563
    by (simp add: poly_eq_iff)
huffman@29451
   564
qed
huffman@29451
   565
huffman@29451
   566
end
huffman@29451
   567
haftmann@59815
   568
instantiation poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
haftmann@59815
   569
begin
haftmann@59815
   570
haftmann@59815
   571
lift_definition minus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
haftmann@59815
   572
  is "\<lambda>p q n. coeff p n - coeff q n"
hoelzl@60040
   573
proof -
wenzelm@60679
   574
  fix q p :: "'a poly"
wenzelm@60679
   575
  show "\<forall>\<^sub>\<infinity>n. coeff p n - coeff q n = 0"
hoelzl@60040
   576
    using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp
haftmann@59815
   577
qed
haftmann@59815
   578
wenzelm@60679
   579
lemma coeff_diff [simp]: "coeff (p - q) n = coeff p n - coeff q n"
haftmann@59815
   580
  by (simp add: minus_poly.rep_eq)
haftmann@59815
   581
wenzelm@60679
   582
instance
wenzelm@60679
   583
proof
huffman@29540
   584
  fix p q r :: "'a poly"
haftmann@59815
   585
  show "p + q - p = q"
haftmann@52380
   586
    by (simp add: poly_eq_iff)
haftmann@59815
   587
  show "p - q - r = p - (q + r)"
haftmann@59815
   588
    by (simp add: poly_eq_iff diff_diff_eq)
huffman@29540
   589
qed
huffman@29540
   590
haftmann@59815
   591
end
haftmann@59815
   592
huffman@29451
   593
instantiation poly :: (ab_group_add) ab_group_add
huffman@29451
   594
begin
huffman@29451
   595
haftmann@52380
   596
lift_definition uminus_poly :: "'a poly \<Rightarrow> 'a poly"
haftmann@52380
   597
  is "\<lambda>p n. - coeff p n"
hoelzl@60040
   598
proof -
wenzelm@60679
   599
  fix p :: "'a poly"
wenzelm@60679
   600
  show "\<forall>\<^sub>\<infinity>n. - coeff p n = 0"
hoelzl@60040
   601
    using MOST_coeff_eq_0 by simp
haftmann@52380
   602
qed
huffman@29451
   603
huffman@29451
   604
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
haftmann@52380
   605
  by (simp add: uminus_poly.rep_eq)
huffman@29451
   606
wenzelm@60679
   607
instance
wenzelm@60679
   608
proof
huffman@29451
   609
  fix p q :: "'a poly"
huffman@29451
   610
  show "- p + p = 0"
haftmann@52380
   611
    by (simp add: poly_eq_iff)
huffman@29451
   612
  show "p - q = p + - q"
haftmann@54230
   613
    by (simp add: poly_eq_iff)
huffman@29451
   614
qed
huffman@29451
   615
huffman@29451
   616
end
huffman@29451
   617
huffman@29451
   618
lemma add_pCons [simp]:
huffman@29451
   619
  "pCons a p + pCons b q = pCons (a + b) (p + q)"
haftmann@52380
   620
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
huffman@29451
   621
huffman@29451
   622
lemma minus_pCons [simp]:
huffman@29451
   623
  "- pCons a p = pCons (- a) (- p)"
haftmann@52380
   624
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
huffman@29451
   625
huffman@29451
   626
lemma diff_pCons [simp]:
huffman@29451
   627
  "pCons a p - pCons b q = pCons (a - b) (p - q)"
haftmann@52380
   628
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
huffman@29451
   629
huffman@29539
   630
lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"
huffman@29451
   631
  by (rule degree_le, auto simp add: coeff_eq_0)
huffman@29451
   632
huffman@29539
   633
lemma degree_add_le:
huffman@29539
   634
  "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n"
huffman@29539
   635
  by (auto intro: order_trans degree_add_le_max)
huffman@29539
   636
huffman@29453
   637
lemma degree_add_less:
huffman@29453
   638
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"
huffman@29539
   639
  by (auto intro: le_less_trans degree_add_le_max)
huffman@29453
   640
huffman@29451
   641
lemma degree_add_eq_right:
huffman@29451
   642
  "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
huffman@29451
   643
  apply (cases "q = 0", simp)
huffman@29451
   644
  apply (rule order_antisym)
huffman@29539
   645
  apply (simp add: degree_add_le)
huffman@29451
   646
  apply (rule le_degree)
huffman@29451
   647
  apply (simp add: coeff_eq_0)
huffman@29451
   648
  done
huffman@29451
   649
huffman@29451
   650
lemma degree_add_eq_left:
huffman@29451
   651
  "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
huffman@29451
   652
  using degree_add_eq_right [of q p]
haftmann@57512
   653
  by (simp add: add.commute)
huffman@29451
   654
haftmann@59815
   655
lemma degree_minus [simp]:
haftmann@59815
   656
  "degree (- p) = degree p"
huffman@29451
   657
  unfolding degree_def by simp
huffman@29451
   658
haftmann@59815
   659
lemma degree_diff_le_max:
haftmann@59815
   660
  fixes p q :: "'a :: ab_group_add poly"
haftmann@59815
   661
  shows "degree (p - q) \<le> max (degree p) (degree q)"
huffman@29451
   662
  using degree_add_le [where p=p and q="-q"]
haftmann@54230
   663
  by simp
huffman@29451
   664
huffman@29539
   665
lemma degree_diff_le:
haftmann@59815
   666
  fixes p q :: "'a :: ab_group_add poly"
haftmann@59815
   667
  assumes "degree p \<le> n" and "degree q \<le> n"
haftmann@59815
   668
  shows "degree (p - q) \<le> n"
haftmann@59815
   669
  using assms degree_add_le [of p n "- q"] by simp
huffman@29539
   670
huffman@29453
   671
lemma degree_diff_less:
haftmann@59815
   672
  fixes p q :: "'a :: ab_group_add poly"
haftmann@59815
   673
  assumes "degree p < n" and "degree q < n"
haftmann@59815
   674
  shows "degree (p - q) < n"
haftmann@59815
   675
  using assms degree_add_less [of p n "- q"] by simp
huffman@29453
   676
huffman@29451
   677
lemma add_monom: "monom a n + monom b n = monom (a + b) n"
haftmann@52380
   678
  by (rule poly_eqI) simp
huffman@29451
   679
huffman@29451
   680
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
haftmann@52380
   681
  by (rule poly_eqI) simp
huffman@29451
   682
huffman@29451
   683
lemma minus_monom: "- monom a n = monom (-a) n"
haftmann@52380
   684
  by (rule poly_eqI) simp
huffman@29451
   685
huffman@29451
   686
lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"
huffman@29451
   687
  by (cases "finite A", induct set: finite, simp_all)
huffman@29451
   688
huffman@29451
   689
lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"
haftmann@52380
   690
  by (rule poly_eqI) (simp add: coeff_setsum)
haftmann@52380
   691
haftmann@52380
   692
fun plus_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list \<Rightarrow> 'a list"
haftmann@52380
   693
where
haftmann@52380
   694
  "plus_coeffs xs [] = xs"
haftmann@52380
   695
| "plus_coeffs [] ys = ys"
haftmann@52380
   696
| "plus_coeffs (x # xs) (y # ys) = (x + y) ## plus_coeffs xs ys"
haftmann@52380
   697
haftmann@52380
   698
lemma coeffs_plus_eq_plus_coeffs [code abstract]:
haftmann@52380
   699
  "coeffs (p + q) = plus_coeffs (coeffs p) (coeffs q)"
haftmann@52380
   700
proof -
haftmann@52380
   701
  { fix xs ys :: "'a list" and n
haftmann@52380
   702
    have "nth_default 0 (plus_coeffs xs ys) n = nth_default 0 xs n + nth_default 0 ys n"
haftmann@52380
   703
    proof (induct xs ys arbitrary: n rule: plus_coeffs.induct)
wenzelm@60679
   704
      case (3 x xs y ys n)
wenzelm@60679
   705
      then show ?case by (cases n) (auto simp add: cCons_def)
haftmann@52380
   706
    qed simp_all }
haftmann@52380
   707
  note * = this
haftmann@52380
   708
  { fix xs ys :: "'a list"
haftmann@52380
   709
    assume "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0" and "ys \<noteq> [] \<Longrightarrow> last ys \<noteq> 0"
haftmann@52380
   710
    moreover assume "plus_coeffs xs ys \<noteq> []"
haftmann@52380
   711
    ultimately have "last (plus_coeffs xs ys) \<noteq> 0"
haftmann@52380
   712
    proof (induct xs ys rule: plus_coeffs.induct)
haftmann@52380
   713
      case (3 x xs y ys) then show ?case by (auto simp add: cCons_def) metis
haftmann@52380
   714
    qed simp_all }
haftmann@52380
   715
  note ** = this
haftmann@52380
   716
  show ?thesis
haftmann@52380
   717
    apply (rule coeffs_eqI)
haftmann@52380
   718
    apply (simp add: * nth_default_coeffs_eq)
haftmann@52380
   719
    apply (rule **)
haftmann@52380
   720
    apply (auto dest: last_coeffs_not_0)
haftmann@52380
   721
    done
haftmann@52380
   722
qed
haftmann@52380
   723
haftmann@52380
   724
lemma coeffs_uminus [code abstract]:
haftmann@52380
   725
  "coeffs (- p) = map (\<lambda>a. - a) (coeffs p)"
haftmann@52380
   726
  by (rule coeffs_eqI)
haftmann@52380
   727
    (simp_all add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq)
haftmann@52380
   728
haftmann@52380
   729
lemma [code]:
haftmann@52380
   730
  fixes p q :: "'a::ab_group_add poly"
haftmann@52380
   731
  shows "p - q = p + - q"
haftmann@59557
   732
  by (fact diff_conv_add_uminus)
haftmann@52380
   733
haftmann@52380
   734
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
haftmann@52380
   735
  apply (induct p arbitrary: q, simp)
haftmann@52380
   736
  apply (case_tac q, simp, simp add: algebra_simps)
haftmann@52380
   737
  done
haftmann@52380
   738
haftmann@52380
   739
lemma poly_minus [simp]:
haftmann@52380
   740
  fixes x :: "'a::comm_ring"
haftmann@52380
   741
  shows "poly (- p) x = - poly p x"
haftmann@52380
   742
  by (induct p) simp_all
haftmann@52380
   743
haftmann@52380
   744
lemma poly_diff [simp]:
haftmann@52380
   745
  fixes x :: "'a::comm_ring"
haftmann@52380
   746
  shows "poly (p - q) x = poly p x - poly q x"
haftmann@54230
   747
  using poly_add [of p "- q" x] by simp
haftmann@52380
   748
haftmann@52380
   749
lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"
haftmann@52380
   750
  by (induct A rule: infinite_finite_induct) simp_all
huffman@29451
   751
eberlm@62128
   752
lemma degree_setsum_le: "finite S \<Longrightarrow> (\<And> p . p \<in> S \<Longrightarrow> degree (f p) \<le> n)
eberlm@62128
   753
  \<Longrightarrow> degree (setsum f S) \<le> n"
eberlm@62128
   754
proof (induct S rule: finite_induct)
eberlm@62128
   755
  case (insert p S)
eberlm@62128
   756
  hence "degree (setsum f S) \<le> n" "degree (f p) \<le> n" by auto
eberlm@62128
   757
  thus ?case unfolding setsum.insert[OF insert(1-2)] by (metis degree_add_le)
eberlm@62128
   758
qed simp
eberlm@62128
   759
eberlm@62128
   760
lemma poly_as_sum_of_monoms': 
eberlm@62128
   761
  assumes n: "degree p \<le> n" 
eberlm@62128
   762
  shows "(\<Sum>i\<le>n. monom (coeff p i) i) = p"
eberlm@62128
   763
proof -
eberlm@62128
   764
  have eq: "\<And>i. {..n} \<inter> {i} = (if i \<le> n then {i} else {})"
eberlm@62128
   765
    by auto
eberlm@62128
   766
  show ?thesis
eberlm@62128
   767
    using n by (simp add: poly_eq_iff coeff_setsum coeff_eq_0 setsum.If_cases eq 
eberlm@62128
   768
                  if_distrib[where f="\<lambda>x. x * a" for a])
eberlm@62128
   769
qed
eberlm@62128
   770
eberlm@62128
   771
lemma poly_as_sum_of_monoms: "(\<Sum>i\<le>degree p. monom (coeff p i) i) = p"
eberlm@62128
   772
  by (intro poly_as_sum_of_monoms' order_refl)
eberlm@62128
   773
eberlm@62065
   774
lemma Poly_snoc: "Poly (xs @ [x]) = Poly xs + monom x (length xs)"
eberlm@62065
   775
  by (induction xs) (simp_all add: monom_0 monom_Suc)
eberlm@62065
   776
huffman@29451
   777
wenzelm@60500
   778
subsection \<open>Multiplication by a constant, polynomial multiplication and the unit polynomial\<close>
huffman@29451
   779
haftmann@52380
   780
lift_definition smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
haftmann@52380
   781
  is "\<lambda>a p n. a * coeff p n"
hoelzl@60040
   782
proof -
hoelzl@60040
   783
  fix a :: 'a and p :: "'a poly" show "\<forall>\<^sub>\<infinity> i. a * coeff p i = 0"
hoelzl@60040
   784
    using MOST_coeff_eq_0[of p] by eventually_elim simp
haftmann@52380
   785
qed
huffman@29451
   786
haftmann@52380
   787
lemma coeff_smult [simp]:
haftmann@52380
   788
  "coeff (smult a p) n = a * coeff p n"
haftmann@52380
   789
  by (simp add: smult.rep_eq)
huffman@29451
   790
huffman@29451
   791
lemma degree_smult_le: "degree (smult a p) \<le> degree p"
huffman@29451
   792
  by (rule degree_le, simp add: coeff_eq_0)
huffman@29451
   793
huffman@29472
   794
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
haftmann@57512
   795
  by (rule poly_eqI, simp add: mult.assoc)
huffman@29451
   796
huffman@29451
   797
lemma smult_0_right [simp]: "smult a 0 = 0"
haftmann@52380
   798
  by (rule poly_eqI, simp)
huffman@29451
   799
huffman@29451
   800
lemma smult_0_left [simp]: "smult 0 p = 0"
haftmann@52380
   801
  by (rule poly_eqI, simp)
huffman@29451
   802
huffman@29451
   803
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
haftmann@52380
   804
  by (rule poly_eqI, simp)
huffman@29451
   805
huffman@29451
   806
lemma smult_add_right:
huffman@29451
   807
  "smult a (p + q) = smult a p + smult a q"
haftmann@52380
   808
  by (rule poly_eqI, simp add: algebra_simps)
huffman@29451
   809
huffman@29451
   810
lemma smult_add_left:
huffman@29451
   811
  "smult (a + b) p = smult a p + smult b p"
haftmann@52380
   812
  by (rule poly_eqI, simp add: algebra_simps)
huffman@29451
   813
huffman@29457
   814
lemma smult_minus_right [simp]:
huffman@29451
   815
  "smult (a::'a::comm_ring) (- p) = - smult a p"
haftmann@52380
   816
  by (rule poly_eqI, simp)
huffman@29451
   817
huffman@29457
   818
lemma smult_minus_left [simp]:
huffman@29451
   819
  "smult (- a::'a::comm_ring) p = - smult a p"
haftmann@52380
   820
  by (rule poly_eqI, simp)
huffman@29451
   821
huffman@29451
   822
lemma smult_diff_right:
huffman@29451
   823
  "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
haftmann@52380
   824
  by (rule poly_eqI, simp add: algebra_simps)
huffman@29451
   825
huffman@29451
   826
lemma smult_diff_left:
huffman@29451
   827
  "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
haftmann@52380
   828
  by (rule poly_eqI, simp add: algebra_simps)
huffman@29451
   829
huffman@29472
   830
lemmas smult_distribs =
huffman@29472
   831
  smult_add_left smult_add_right
huffman@29472
   832
  smult_diff_left smult_diff_right
huffman@29472
   833
huffman@29451
   834
lemma smult_pCons [simp]:
huffman@29451
   835
  "smult a (pCons b p) = pCons (a * b) (smult a p)"
haftmann@52380
   836
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
huffman@29451
   837
huffman@29451
   838
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
huffman@29451
   839
  by (induct n, simp add: monom_0, simp add: monom_Suc)
huffman@29451
   840
huffman@29659
   841
lemma degree_smult_eq [simp]:
huffman@29659
   842
  fixes a :: "'a::idom"
huffman@29659
   843
  shows "degree (smult a p) = (if a = 0 then 0 else degree p)"
huffman@29659
   844
  by (cases "a = 0", simp, simp add: degree_def)
huffman@29659
   845
huffman@29659
   846
lemma smult_eq_0_iff [simp]:
huffman@29659
   847
  fixes a :: "'a::idom"
huffman@29659
   848
  shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"
haftmann@52380
   849
  by (simp add: poly_eq_iff)
huffman@29451
   850
haftmann@52380
   851
lemma coeffs_smult [code abstract]:
haftmann@52380
   852
  fixes p :: "'a::idom poly"
haftmann@52380
   853
  shows "coeffs (smult a p) = (if a = 0 then [] else map (Groups.times a) (coeffs p))"
haftmann@52380
   854
  by (rule coeffs_eqI)
haftmann@52380
   855
    (auto simp add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq)
huffman@29451
   856
huffman@29451
   857
instantiation poly :: (comm_semiring_0) comm_semiring_0
huffman@29451
   858
begin
huffman@29451
   859
huffman@29451
   860
definition
haftmann@52380
   861
  "p * q = fold_coeffs (\<lambda>a p. smult a q + pCons 0 p) p 0"
huffman@29474
   862
huffman@29474
   863
lemma mult_poly_0_left: "(0::'a poly) * q = 0"
haftmann@52380
   864
  by (simp add: times_poly_def)
huffman@29474
   865
huffman@29474
   866
lemma mult_pCons_left [simp]:
huffman@29474
   867
  "pCons a p * q = smult a q + pCons 0 (p * q)"
haftmann@52380
   868
  by (cases "p = 0 \<and> a = 0") (auto simp add: times_poly_def)
huffman@29474
   869
huffman@29474
   870
lemma mult_poly_0_right: "p * (0::'a poly) = 0"
haftmann@52380
   871
  by (induct p) (simp add: mult_poly_0_left, simp)
huffman@29451
   872
huffman@29474
   873
lemma mult_pCons_right [simp]:
huffman@29474
   874
  "p * pCons a q = smult a p + pCons 0 (p * q)"
haftmann@52380
   875
  by (induct p) (simp add: mult_poly_0_left, simp add: algebra_simps)
huffman@29474
   876
huffman@29474
   877
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
huffman@29474
   878
haftmann@52380
   879
lemma mult_smult_left [simp]:
haftmann@52380
   880
  "smult a p * q = smult a (p * q)"
haftmann@52380
   881
  by (induct p) (simp add: mult_poly_0, simp add: smult_add_right)
huffman@29474
   882
haftmann@52380
   883
lemma mult_smult_right [simp]:
haftmann@52380
   884
  "p * smult a q = smult a (p * q)"
haftmann@52380
   885
  by (induct q) (simp add: mult_poly_0, simp add: smult_add_right)
huffman@29474
   886
huffman@29474
   887
lemma mult_poly_add_left:
huffman@29474
   888
  fixes p q r :: "'a poly"
huffman@29474
   889
  shows "(p + q) * r = p * r + q * r"
haftmann@52380
   890
  by (induct r) (simp add: mult_poly_0, simp add: smult_distribs algebra_simps)
huffman@29451
   891
wenzelm@60679
   892
instance
wenzelm@60679
   893
proof
huffman@29451
   894
  fix p q r :: "'a poly"
huffman@29451
   895
  show 0: "0 * p = 0"
huffman@29474
   896
    by (rule mult_poly_0_left)
huffman@29451
   897
  show "p * 0 = 0"
huffman@29474
   898
    by (rule mult_poly_0_right)
huffman@29451
   899
  show "(p + q) * r = p * r + q * r"
huffman@29474
   900
    by (rule mult_poly_add_left)
huffman@29451
   901
  show "(p * q) * r = p * (q * r)"
huffman@29474
   902
    by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)
huffman@29451
   903
  show "p * q = q * p"
huffman@29474
   904
    by (induct p, simp add: mult_poly_0, simp)
huffman@29451
   905
qed
huffman@29451
   906
huffman@29451
   907
end
huffman@29451
   908
huffman@29540
   909
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
huffman@29540
   910
huffman@29474
   911
lemma coeff_mult:
huffman@29474
   912
  "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
huffman@29474
   913
proof (induct p arbitrary: n)
huffman@29474
   914
  case 0 show ?case by simp
huffman@29474
   915
next
huffman@29474
   916
  case (pCons a p n) thus ?case
huffman@29474
   917
    by (cases n, simp, simp add: setsum_atMost_Suc_shift
huffman@29474
   918
                            del: setsum_atMost_Suc)
huffman@29474
   919
qed
huffman@29451
   920
huffman@29474
   921
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
huffman@29474
   922
apply (rule degree_le)
huffman@29474
   923
apply (induct p)
huffman@29474
   924
apply simp
huffman@29474
   925
apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
huffman@29451
   926
done
huffman@29451
   927
huffman@29451
   928
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
wenzelm@60679
   929
  by (induct m) (simp add: monom_0 smult_monom, simp add: monom_Suc)
huffman@29451
   930
huffman@29451
   931
instantiation poly :: (comm_semiring_1) comm_semiring_1
huffman@29451
   932
begin
huffman@29451
   933
wenzelm@60679
   934
definition one_poly_def: "1 = pCons 1 0"
huffman@29451
   935
wenzelm@60679
   936
instance
wenzelm@60679
   937
proof
wenzelm@60679
   938
  show "1 * p = p" for p :: "'a poly"
haftmann@52380
   939
    unfolding one_poly_def by simp
huffman@29451
   940
  show "0 \<noteq> (1::'a poly)"
huffman@29451
   941
    unfolding one_poly_def by simp
huffman@29451
   942
qed
huffman@29451
   943
huffman@29451
   944
end
huffman@29451
   945
haftmann@52380
   946
instance poly :: (comm_ring) comm_ring ..
haftmann@52380
   947
haftmann@52380
   948
instance poly :: (comm_ring_1) comm_ring_1 ..
haftmann@52380
   949
huffman@29451
   950
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
huffman@29451
   951
  unfolding one_poly_def
huffman@29451
   952
  by (simp add: coeff_pCons split: nat.split)
huffman@29451
   953
haftmann@60570
   954
lemma monom_eq_1 [simp]:
haftmann@60570
   955
  "monom 1 0 = 1"
haftmann@60570
   956
  by (simp add: monom_0 one_poly_def)
haftmann@60570
   957
  
huffman@29451
   958
lemma degree_1 [simp]: "degree 1 = 0"
huffman@29451
   959
  unfolding one_poly_def
huffman@29451
   960
  by (rule degree_pCons_0)
huffman@29451
   961
haftmann@52380
   962
lemma coeffs_1_eq [simp, code abstract]:
haftmann@52380
   963
  "coeffs 1 = [1]"
haftmann@52380
   964
  by (simp add: one_poly_def)
haftmann@52380
   965
haftmann@52380
   966
lemma degree_power_le:
haftmann@52380
   967
  "degree (p ^ n) \<le> degree p * n"
haftmann@52380
   968
  by (induct n) (auto intro: order_trans degree_mult_le)
haftmann@52380
   969
haftmann@52380
   970
lemma poly_smult [simp]:
haftmann@52380
   971
  "poly (smult a p) x = a * poly p x"
haftmann@52380
   972
  by (induct p, simp, simp add: algebra_simps)
haftmann@52380
   973
haftmann@52380
   974
lemma poly_mult [simp]:
haftmann@52380
   975
  "poly (p * q) x = poly p x * poly q x"
haftmann@52380
   976
  by (induct p, simp_all, simp add: algebra_simps)
haftmann@52380
   977
haftmann@52380
   978
lemma poly_1 [simp]:
haftmann@52380
   979
  "poly 1 x = 1"
haftmann@52380
   980
  by (simp add: one_poly_def)
haftmann@52380
   981
haftmann@52380
   982
lemma poly_power [simp]:
haftmann@52380
   983
  fixes p :: "'a::{comm_semiring_1} poly"
haftmann@52380
   984
  shows "poly (p ^ n) x = poly p x ^ n"
haftmann@52380
   985
  by (induct n) simp_all
haftmann@52380
   986
eberlm@62128
   987
lemma poly_setprod: "poly (\<Prod>k\<in>A. p k) x = (\<Prod>k\<in>A. poly (p k) x)"
eberlm@62128
   988
  by (induct A rule: infinite_finite_induct) simp_all
eberlm@62128
   989
eberlm@62128
   990
lemma degree_setprod_setsum_le: "finite S \<Longrightarrow> degree (setprod f S) \<le> setsum (degree o f) S"
eberlm@62128
   991
proof (induct S rule: finite_induct)
eberlm@62128
   992
  case (insert a S)
eberlm@62128
   993
  show ?case unfolding setprod.insert[OF insert(1-2)] setsum.insert[OF insert(1-2)]
eberlm@62128
   994
    by (rule le_trans[OF degree_mult_le], insert insert, auto)
eberlm@62128
   995
qed simp
eberlm@62128
   996
eberlm@62065
   997
subsection \<open>Conversions from natural numbers\<close>
eberlm@62065
   998
eberlm@62065
   999
lemma of_nat_poly: "of_nat n = [:of_nat n :: 'a :: comm_semiring_1:]"
eberlm@62065
  1000
proof (induction n)
eberlm@62065
  1001
  case (Suc n)
eberlm@62065
  1002
  hence "of_nat (Suc n) = 1 + (of_nat n :: 'a poly)" 
eberlm@62065
  1003
    by simp
eberlm@62065
  1004
  also have "(of_nat n :: 'a poly) = [: of_nat n :]" 
eberlm@62065
  1005
    by (subst Suc) (rule refl)
eberlm@62065
  1006
  also have "1 = [:1:]" by (simp add: one_poly_def)
eberlm@62065
  1007
  finally show ?case by (subst (asm) add_pCons) simp
eberlm@62065
  1008
qed simp
eberlm@62065
  1009
eberlm@62065
  1010
lemma degree_of_nat [simp]: "degree (of_nat n) = 0"
eberlm@62065
  1011
  by (simp add: of_nat_poly)
eberlm@62065
  1012
eberlm@62065
  1013
lemma degree_numeral [simp]: "degree (numeral n) = 0"
eberlm@62065
  1014
  by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp
eberlm@62065
  1015
eberlm@62065
  1016
lemma numeral_poly: "numeral n = [:numeral n:]"
eberlm@62065
  1017
  by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp
haftmann@52380
  1018
wenzelm@60500
  1019
subsection \<open>Lemmas about divisibility\<close>
huffman@29979
  1020
huffman@29979
  1021
lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q"
huffman@29979
  1022
proof -
huffman@29979
  1023
  assume "p dvd q"
huffman@29979
  1024
  then obtain k where "q = p * k" ..
huffman@29979
  1025
  then have "smult a q = p * smult a k" by simp
huffman@29979
  1026
  then show "p dvd smult a q" ..
huffman@29979
  1027
qed
huffman@29979
  1028
huffman@29979
  1029
lemma dvd_smult_cancel:
eberlm@62128
  1030
  fixes a :: "'a :: field"
huffman@29979
  1031
  shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q"
huffman@29979
  1032
  by (drule dvd_smult [where a="inverse a"]) simp
huffman@29979
  1033
huffman@29979
  1034
lemma dvd_smult_iff:
huffman@29979
  1035
  fixes a :: "'a::field"
huffman@29979
  1036
  shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q"
huffman@29979
  1037
  by (safe elim!: dvd_smult dvd_smult_cancel)
huffman@29979
  1038
huffman@31663
  1039
lemma smult_dvd_cancel:
huffman@31663
  1040
  "smult a p dvd q \<Longrightarrow> p dvd q"
huffman@31663
  1041
proof -
huffman@31663
  1042
  assume "smult a p dvd q"
huffman@31663
  1043
  then obtain k where "q = smult a p * k" ..
huffman@31663
  1044
  then have "q = p * smult a k" by simp
huffman@31663
  1045
  then show "p dvd q" ..
huffman@31663
  1046
qed
huffman@31663
  1047
huffman@31663
  1048
lemma smult_dvd:
huffman@31663
  1049
  fixes a :: "'a::field"
huffman@31663
  1050
  shows "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q"
huffman@31663
  1051
  by (rule smult_dvd_cancel [where a="inverse a"]) simp
huffman@31663
  1052
huffman@31663
  1053
lemma smult_dvd_iff:
huffman@31663
  1054
  fixes a :: "'a::field"
huffman@31663
  1055
  shows "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)"
huffman@31663
  1056
  by (auto elim: smult_dvd smult_dvd_cancel)
huffman@31663
  1057
huffman@29451
  1058
wenzelm@60500
  1059
subsection \<open>Polynomials form an integral domain\<close>
huffman@29451
  1060
huffman@29451
  1061
lemma coeff_mult_degree_sum:
huffman@29451
  1062
  "coeff (p * q) (degree p + degree q) =
huffman@29451
  1063
   coeff p (degree p) * coeff q (degree q)"
huffman@29471
  1064
  by (induct p, simp, simp add: coeff_eq_0)
huffman@29451
  1065
huffman@29451
  1066
instance poly :: (idom) idom
huffman@29451
  1067
proof
huffman@29451
  1068
  fix p q :: "'a poly"
huffman@29451
  1069
  assume "p \<noteq> 0" and "q \<noteq> 0"
huffman@29451
  1070
  have "coeff (p * q) (degree p + degree q) =
huffman@29451
  1071
        coeff p (degree p) * coeff q (degree q)"
huffman@29451
  1072
    by (rule coeff_mult_degree_sum)
huffman@29451
  1073
  also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
wenzelm@60500
  1074
    using \<open>p \<noteq> 0\<close> and \<open>q \<noteq> 0\<close> by simp
huffman@29451
  1075
  finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
haftmann@52380
  1076
  thus "p * q \<noteq> 0" by (simp add: poly_eq_iff)
huffman@29451
  1077
qed
huffman@29451
  1078
huffman@29451
  1079
lemma degree_mult_eq:
eberlm@62128
  1080
  fixes p q :: "'a::semidom poly"
huffman@29451
  1081
  shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q"
huffman@29451
  1082
apply (rule order_antisym [OF degree_mult_le le_degree])
huffman@29451
  1083
apply (simp add: coeff_mult_degree_sum)
huffman@29451
  1084
done
huffman@29451
  1085
haftmann@60570
  1086
lemma degree_mult_right_le:
eberlm@62128
  1087
  fixes p q :: "'a::semidom poly"
haftmann@60570
  1088
  assumes "q \<noteq> 0"
haftmann@60570
  1089
  shows "degree p \<le> degree (p * q)"
haftmann@60570
  1090
  using assms by (cases "p = 0") (simp_all add: degree_mult_eq)
haftmann@60570
  1091
haftmann@60570
  1092
lemma coeff_degree_mult:
eberlm@62128
  1093
  fixes p q :: "'a::semidom poly"
haftmann@60570
  1094
  shows "coeff (p * q) (degree (p * q)) =
haftmann@60570
  1095
    coeff q (degree q) * coeff p (degree p)"
eberlm@62128
  1096
  by (cases "p = 0 \<or> q = 0") (auto simp add: degree_mult_eq coeff_mult_degree_sum mult_ac)
haftmann@60570
  1097
huffman@29451
  1098
lemma dvd_imp_degree_le:
eberlm@62128
  1099
  fixes p q :: "'a::semidom poly"
huffman@29451
  1100
  shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
eberlm@62128
  1101
  by (erule dvdE, hypsubst, subst degree_mult_eq) auto
huffman@29451
  1102
eberlm@62128
  1103
lemma divides_degree:
eberlm@62128
  1104
  assumes pq: "p dvd (q :: 'a :: semidom poly)"
eberlm@62128
  1105
  shows "degree p \<le> degree q \<or> q = 0"
eberlm@62128
  1106
  by (metis dvd_imp_degree_le pq)
huffman@29451
  1107
wenzelm@60500
  1108
subsection \<open>Polynomials form an ordered integral domain\<close>
huffman@29878
  1109
haftmann@52380
  1110
definition pos_poly :: "'a::linordered_idom poly \<Rightarrow> bool"
huffman@29878
  1111
where
huffman@29878
  1112
  "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"
huffman@29878
  1113
huffman@29878
  1114
lemma pos_poly_pCons:
huffman@29878
  1115
  "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)"
huffman@29878
  1116
  unfolding pos_poly_def by simp
huffman@29878
  1117
huffman@29878
  1118
lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"
huffman@29878
  1119
  unfolding pos_poly_def by simp
huffman@29878
  1120
huffman@29878
  1121
lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)"
huffman@29878
  1122
  apply (induct p arbitrary: q, simp)
huffman@29878
  1123
  apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos)
huffman@29878
  1124
  done
huffman@29878
  1125
huffman@29878
  1126
lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)"
huffman@29878
  1127
  unfolding pos_poly_def
huffman@29878
  1128
  apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")
nipkow@56544
  1129
  apply (simp add: degree_mult_eq coeff_mult_degree_sum)
huffman@29878
  1130
  apply auto
huffman@29878
  1131
  done
huffman@29878
  1132
huffman@29878
  1133
lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)"
huffman@29878
  1134
by (induct p) (auto simp add: pos_poly_pCons)
huffman@29878
  1135
haftmann@52380
  1136
lemma last_coeffs_eq_coeff_degree:
haftmann@52380
  1137
  "p \<noteq> 0 \<Longrightarrow> last (coeffs p) = coeff p (degree p)"
haftmann@52380
  1138
  by (simp add: coeffs_def)
haftmann@52380
  1139
haftmann@52380
  1140
lemma pos_poly_coeffs [code]:
haftmann@52380
  1141
  "pos_poly p \<longleftrightarrow> (let as = coeffs p in as \<noteq> [] \<and> last as > 0)" (is "?P \<longleftrightarrow> ?Q")
haftmann@52380
  1142
proof
haftmann@52380
  1143
  assume ?Q then show ?P by (auto simp add: pos_poly_def last_coeffs_eq_coeff_degree)
haftmann@52380
  1144
next
haftmann@52380
  1145
  assume ?P then have *: "0 < coeff p (degree p)" by (simp add: pos_poly_def)
haftmann@52380
  1146
  then have "p \<noteq> 0" by auto
haftmann@52380
  1147
  with * show ?Q by (simp add: last_coeffs_eq_coeff_degree)
haftmann@52380
  1148
qed
haftmann@52380
  1149
haftmann@35028
  1150
instantiation poly :: (linordered_idom) linordered_idom
huffman@29878
  1151
begin
huffman@29878
  1152
huffman@29878
  1153
definition
haftmann@37765
  1154
  "x < y \<longleftrightarrow> pos_poly (y - x)"
huffman@29878
  1155
huffman@29878
  1156
definition
haftmann@37765
  1157
  "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"
huffman@29878
  1158
huffman@29878
  1159
definition
wenzelm@61945
  1160
  "\<bar>x::'a poly\<bar> = (if x < 0 then - x else x)"
huffman@29878
  1161
huffman@29878
  1162
definition
haftmann@37765
  1163
  "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
huffman@29878
  1164
wenzelm@60679
  1165
instance
wenzelm@60679
  1166
proof
wenzelm@60679
  1167
  fix x y z :: "'a poly"
huffman@29878
  1168
  show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
huffman@29878
  1169
    unfolding less_eq_poly_def less_poly_def
huffman@29878
  1170
    apply safe
huffman@29878
  1171
    apply simp
huffman@29878
  1172
    apply (drule (1) pos_poly_add)
huffman@29878
  1173
    apply simp
huffman@29878
  1174
    done
wenzelm@60679
  1175
  show "x \<le> x"
huffman@29878
  1176
    unfolding less_eq_poly_def by simp
wenzelm@60679
  1177
  show "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
huffman@29878
  1178
    unfolding less_eq_poly_def
huffman@29878
  1179
    apply safe
huffman@29878
  1180
    apply (drule (1) pos_poly_add)
huffman@29878
  1181
    apply (simp add: algebra_simps)
huffman@29878
  1182
    done
wenzelm@60679
  1183
  show "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y"
huffman@29878
  1184
    unfolding less_eq_poly_def
huffman@29878
  1185
    apply safe
huffman@29878
  1186
    apply (drule (1) pos_poly_add)
huffman@29878
  1187
    apply simp
huffman@29878
  1188
    done
wenzelm@60679
  1189
  show "x \<le> y \<Longrightarrow> z + x \<le> z + y"
huffman@29878
  1190
    unfolding less_eq_poly_def
huffman@29878
  1191
    apply safe
huffman@29878
  1192
    apply (simp add: algebra_simps)
huffman@29878
  1193
    done
huffman@29878
  1194
  show "x \<le> y \<or> y \<le> x"
huffman@29878
  1195
    unfolding less_eq_poly_def
huffman@29878
  1196
    using pos_poly_total [of "x - y"]
huffman@29878
  1197
    by auto
wenzelm@60679
  1198
  show "x < y \<Longrightarrow> 0 < z \<Longrightarrow> z * x < z * y"
huffman@29878
  1199
    unfolding less_poly_def
huffman@29878
  1200
    by (simp add: right_diff_distrib [symmetric] pos_poly_mult)
huffman@29878
  1201
  show "\<bar>x\<bar> = (if x < 0 then - x else x)"
huffman@29878
  1202
    by (rule abs_poly_def)
huffman@29878
  1203
  show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
huffman@29878
  1204
    by (rule sgn_poly_def)
huffman@29878
  1205
qed
huffman@29878
  1206
huffman@29878
  1207
end
huffman@29878
  1208
wenzelm@60500
  1209
text \<open>TODO: Simplification rules for comparisons\<close>
huffman@29878
  1210
huffman@29878
  1211
wenzelm@60500
  1212
subsection \<open>Synthetic division and polynomial roots\<close>
haftmann@52380
  1213
wenzelm@60500
  1214
text \<open>
haftmann@52380
  1215
  Synthetic division is simply division by the linear polynomial @{term "x - c"}.
wenzelm@60500
  1216
\<close>
haftmann@52380
  1217
haftmann@52380
  1218
definition synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"
haftmann@52380
  1219
where
haftmann@52380
  1220
  "synthetic_divmod p c = fold_coeffs (\<lambda>a (q, r). (pCons r q, a + c * r)) p (0, 0)"
haftmann@52380
  1221
haftmann@52380
  1222
definition synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
haftmann@52380
  1223
where
haftmann@52380
  1224
  "synthetic_div p c = fst (synthetic_divmod p c)"
haftmann@52380
  1225
haftmann@52380
  1226
lemma synthetic_divmod_0 [simp]:
haftmann@52380
  1227
  "synthetic_divmod 0 c = (0, 0)"
haftmann@52380
  1228
  by (simp add: synthetic_divmod_def)
haftmann@52380
  1229
haftmann@52380
  1230
lemma synthetic_divmod_pCons [simp]:
haftmann@52380
  1231
  "synthetic_divmod (pCons a p) c = (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)"
haftmann@52380
  1232
  by (cases "p = 0 \<and> a = 0") (auto simp add: synthetic_divmod_def)
haftmann@52380
  1233
haftmann@52380
  1234
lemma synthetic_div_0 [simp]:
haftmann@52380
  1235
  "synthetic_div 0 c = 0"
haftmann@52380
  1236
  unfolding synthetic_div_def by simp
haftmann@52380
  1237
haftmann@52380
  1238
lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0"
haftmann@52380
  1239
by (induct p arbitrary: a) simp_all
haftmann@52380
  1240
haftmann@52380
  1241
lemma snd_synthetic_divmod:
haftmann@52380
  1242
  "snd (synthetic_divmod p c) = poly p c"
haftmann@52380
  1243
  by (induct p, simp, simp add: split_def)
haftmann@52380
  1244
haftmann@52380
  1245
lemma synthetic_div_pCons [simp]:
haftmann@52380
  1246
  "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)"
haftmann@52380
  1247
  unfolding synthetic_div_def
haftmann@52380
  1248
  by (simp add: split_def snd_synthetic_divmod)
haftmann@52380
  1249
haftmann@52380
  1250
lemma synthetic_div_eq_0_iff:
haftmann@52380
  1251
  "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0"
haftmann@52380
  1252
  by (induct p, simp, case_tac p, simp)
haftmann@52380
  1253
haftmann@52380
  1254
lemma degree_synthetic_div:
haftmann@52380
  1255
  "degree (synthetic_div p c) = degree p - 1"
haftmann@52380
  1256
  by (induct p, simp, simp add: synthetic_div_eq_0_iff)
haftmann@52380
  1257
haftmann@52380
  1258
lemma synthetic_div_correct:
haftmann@52380
  1259
  "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)"
haftmann@52380
  1260
  by (induct p) simp_all
haftmann@52380
  1261
haftmann@52380
  1262
lemma synthetic_div_unique:
haftmann@52380
  1263
  "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c"
haftmann@52380
  1264
apply (induct p arbitrary: q r)
haftmann@52380
  1265
apply (simp, frule synthetic_div_unique_lemma, simp)
haftmann@52380
  1266
apply (case_tac q, force)
haftmann@52380
  1267
done
haftmann@52380
  1268
haftmann@52380
  1269
lemma synthetic_div_correct':
haftmann@52380
  1270
  fixes c :: "'a::comm_ring_1"
haftmann@52380
  1271
  shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"
haftmann@52380
  1272
  using synthetic_div_correct [of p c]
haftmann@52380
  1273
  by (simp add: algebra_simps)
haftmann@52380
  1274
haftmann@52380
  1275
lemma poly_eq_0_iff_dvd:
haftmann@52380
  1276
  fixes c :: "'a::idom"
haftmann@52380
  1277
  shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p"
haftmann@52380
  1278
proof
haftmann@52380
  1279
  assume "poly p c = 0"
haftmann@52380
  1280
  with synthetic_div_correct' [of c p]
haftmann@52380
  1281
  have "p = [:-c, 1:] * synthetic_div p c" by simp
haftmann@52380
  1282
  then show "[:-c, 1:] dvd p" ..
haftmann@52380
  1283
next
haftmann@52380
  1284
  assume "[:-c, 1:] dvd p"
haftmann@52380
  1285
  then obtain k where "p = [:-c, 1:] * k" by (rule dvdE)
haftmann@52380
  1286
  then show "poly p c = 0" by simp
haftmann@52380
  1287
qed
haftmann@52380
  1288
haftmann@52380
  1289
lemma dvd_iff_poly_eq_0:
haftmann@52380
  1290
  fixes c :: "'a::idom"
haftmann@52380
  1291
  shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0"
haftmann@52380
  1292
  by (simp add: poly_eq_0_iff_dvd)
haftmann@52380
  1293
haftmann@52380
  1294
lemma poly_roots_finite:
haftmann@52380
  1295
  fixes p :: "'a::idom poly"
haftmann@52380
  1296
  shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
haftmann@52380
  1297
proof (induct n \<equiv> "degree p" arbitrary: p)
haftmann@52380
  1298
  case (0 p)
haftmann@52380
  1299
  then obtain a where "a \<noteq> 0" and "p = [:a:]"
haftmann@52380
  1300
    by (cases p, simp split: if_splits)
haftmann@52380
  1301
  then show "finite {x. poly p x = 0}" by simp
haftmann@52380
  1302
next
haftmann@52380
  1303
  case (Suc n p)
haftmann@52380
  1304
  show "finite {x. poly p x = 0}"
haftmann@52380
  1305
  proof (cases "\<exists>x. poly p x = 0")
haftmann@52380
  1306
    case False
haftmann@52380
  1307
    then show "finite {x. poly p x = 0}" by simp
haftmann@52380
  1308
  next
haftmann@52380
  1309
    case True
haftmann@52380
  1310
    then obtain a where "poly p a = 0" ..
haftmann@52380
  1311
    then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd)
haftmann@52380
  1312
    then obtain k where k: "p = [:-a, 1:] * k" ..
wenzelm@60500
  1313
    with \<open>p \<noteq> 0\<close> have "k \<noteq> 0" by auto
haftmann@52380
  1314
    with k have "degree p = Suc (degree k)"
haftmann@52380
  1315
      by (simp add: degree_mult_eq del: mult_pCons_left)
wenzelm@60500
  1316
    with \<open>Suc n = degree p\<close> have "n = degree k" by simp
wenzelm@60500
  1317
    then have "finite {x. poly k x = 0}" using \<open>k \<noteq> 0\<close> by (rule Suc.hyps)
haftmann@52380
  1318
    then have "finite (insert a {x. poly k x = 0})" by simp
haftmann@52380
  1319
    then show "finite {x. poly p x = 0}"
wenzelm@57862
  1320
      by (simp add: k Collect_disj_eq del: mult_pCons_left)
haftmann@52380
  1321
  qed
haftmann@52380
  1322
qed
haftmann@52380
  1323
haftmann@52380
  1324
lemma poly_eq_poly_eq_iff:
haftmann@52380
  1325
  fixes p q :: "'a::{idom,ring_char_0} poly"
haftmann@52380
  1326
  shows "poly p = poly q \<longleftrightarrow> p = q" (is "?P \<longleftrightarrow> ?Q")
haftmann@52380
  1327
proof
haftmann@52380
  1328
  assume ?Q then show ?P by simp
haftmann@52380
  1329
next
haftmann@52380
  1330
  { fix p :: "'a::{idom,ring_char_0} poly"
haftmann@52380
  1331
    have "poly p = poly 0 \<longleftrightarrow> p = 0"
haftmann@52380
  1332
      apply (cases "p = 0", simp_all)
haftmann@52380
  1333
      apply (drule poly_roots_finite)
haftmann@52380
  1334
      apply (auto simp add: infinite_UNIV_char_0)
haftmann@52380
  1335
      done
haftmann@52380
  1336
  } note this [of "p - q"]
haftmann@52380
  1337
  moreover assume ?P
haftmann@52380
  1338
  ultimately show ?Q by auto
haftmann@52380
  1339
qed
haftmann@52380
  1340
haftmann@52380
  1341
lemma poly_all_0_iff_0:
haftmann@52380
  1342
  fixes p :: "'a::{ring_char_0, idom} poly"
haftmann@52380
  1343
  shows "(\<forall>x. poly p x = 0) \<longleftrightarrow> p = 0"
haftmann@52380
  1344
  by (auto simp add: poly_eq_poly_eq_iff [symmetric])
haftmann@52380
  1345
haftmann@52380
  1346
wenzelm@60500
  1347
subsection \<open>Long division of polynomials\<close>
huffman@29451
  1348
haftmann@52380
  1349
definition pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
huffman@29451
  1350
where
huffman@29537
  1351
  "pdivmod_rel x y q r \<longleftrightarrow>
huffman@29451
  1352
    x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"
huffman@29451
  1353
huffman@29537
  1354
lemma pdivmod_rel_0:
huffman@29537
  1355
  "pdivmod_rel 0 y 0 0"
huffman@29537
  1356
  unfolding pdivmod_rel_def by simp
huffman@29451
  1357
huffman@29537
  1358
lemma pdivmod_rel_by_0:
huffman@29537
  1359
  "pdivmod_rel x 0 0 x"
huffman@29537
  1360
  unfolding pdivmod_rel_def by simp
huffman@29451
  1361
huffman@29451
  1362
lemma eq_zero_or_degree_less:
huffman@29451
  1363
  assumes "degree p \<le> n" and "coeff p n = 0"
huffman@29451
  1364
  shows "p = 0 \<or> degree p < n"
huffman@29451
  1365
proof (cases n)
huffman@29451
  1366
  case 0
wenzelm@60500
  1367
  with \<open>degree p \<le> n\<close> and \<open>coeff p n = 0\<close>
huffman@29451
  1368
  have "coeff p (degree p) = 0" by simp
huffman@29451
  1369
  then have "p = 0" by simp
huffman@29451
  1370
  then show ?thesis ..
huffman@29451
  1371
next
huffman@29451
  1372
  case (Suc m)
huffman@29451
  1373
  have "\<forall>i>n. coeff p i = 0"
wenzelm@60500
  1374
    using \<open>degree p \<le> n\<close> by (simp add: coeff_eq_0)
huffman@29451
  1375
  then have "\<forall>i\<ge>n. coeff p i = 0"
wenzelm@60500
  1376
    using \<open>coeff p n = 0\<close> by (simp add: le_less)
huffman@29451
  1377
  then have "\<forall>i>m. coeff p i = 0"
wenzelm@60500
  1378
    using \<open>n = Suc m\<close> by (simp add: less_eq_Suc_le)
huffman@29451
  1379
  then have "degree p \<le> m"
huffman@29451
  1380
    by (rule degree_le)
huffman@29451
  1381
  then have "degree p < n"
wenzelm@60500
  1382
    using \<open>n = Suc m\<close> by (simp add: less_Suc_eq_le)
huffman@29451
  1383
  then show ?thesis ..
huffman@29451
  1384
qed
huffman@29451
  1385
huffman@29537
  1386
lemma pdivmod_rel_pCons:
huffman@29537
  1387
  assumes rel: "pdivmod_rel x y q r"
huffman@29451
  1388
  assumes y: "y \<noteq> 0"
huffman@29451
  1389
  assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
huffman@29537
  1390
  shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
huffman@29537
  1391
    (is "pdivmod_rel ?x y ?q ?r")
huffman@29451
  1392
proof -
huffman@29451
  1393
  have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
huffman@29537
  1394
    using assms unfolding pdivmod_rel_def by simp_all
huffman@29451
  1395
huffman@29451
  1396
  have 1: "?x = ?q * y + ?r"
huffman@29451
  1397
    using b x by simp
huffman@29451
  1398
huffman@29451
  1399
  have 2: "?r = 0 \<or> degree ?r < degree y"
huffman@29451
  1400
  proof (rule eq_zero_or_degree_less)
huffman@29539
  1401
    show "degree ?r \<le> degree y"
huffman@29539
  1402
    proof (rule degree_diff_le)
huffman@29451
  1403
      show "degree (pCons a r) \<le> degree y"
huffman@29460
  1404
        using r by auto
huffman@29451
  1405
      show "degree (smult b y) \<le> degree y"
huffman@29451
  1406
        by (rule degree_smult_le)
huffman@29451
  1407
    qed
huffman@29451
  1408
  next
huffman@29451
  1409
    show "coeff ?r (degree y) = 0"
wenzelm@60500
  1410
      using \<open>y \<noteq> 0\<close> unfolding b by simp
huffman@29451
  1411
  qed
huffman@29451
  1412
huffman@29451
  1413
  from 1 2 show ?thesis
huffman@29537
  1414
    unfolding pdivmod_rel_def
wenzelm@60500
  1415
    using \<open>y \<noteq> 0\<close> by simp
huffman@29451
  1416
qed
huffman@29451
  1417
huffman@29537
  1418
lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r"
huffman@29451
  1419
apply (cases "y = 0")
huffman@29537
  1420
apply (fast intro!: pdivmod_rel_by_0)
huffman@29451
  1421
apply (induct x)
huffman@29537
  1422
apply (fast intro!: pdivmod_rel_0)
huffman@29537
  1423
apply (fast intro!: pdivmod_rel_pCons)
huffman@29451
  1424
done
huffman@29451
  1425
huffman@29537
  1426
lemma pdivmod_rel_unique:
huffman@29537
  1427
  assumes 1: "pdivmod_rel x y q1 r1"
huffman@29537
  1428
  assumes 2: "pdivmod_rel x y q2 r2"
huffman@29451
  1429
  shows "q1 = q2 \<and> r1 = r2"
huffman@29451
  1430
proof (cases "y = 0")
huffman@29451
  1431
  assume "y = 0" with assms show ?thesis
huffman@29537
  1432
    by (simp add: pdivmod_rel_def)
huffman@29451
  1433
next
huffman@29451
  1434
  assume [simp]: "y \<noteq> 0"
huffman@29451
  1435
  from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
huffman@29537
  1436
    unfolding pdivmod_rel_def by simp_all
huffman@29451
  1437
  from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
huffman@29537
  1438
    unfolding pdivmod_rel_def by simp_all
huffman@29451
  1439
  from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
nipkow@29667
  1440
    by (simp add: algebra_simps)
huffman@29451
  1441
  from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
huffman@29453
  1442
    by (auto intro: degree_diff_less)
huffman@29451
  1443
huffman@29451
  1444
  show "q1 = q2 \<and> r1 = r2"
huffman@29451
  1445
  proof (rule ccontr)
huffman@29451
  1446
    assume "\<not> (q1 = q2 \<and> r1 = r2)"
huffman@29451
  1447
    with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
huffman@29451
  1448
    with r3 have "degree (r2 - r1) < degree y" by simp
huffman@29451
  1449
    also have "degree y \<le> degree (q1 - q2) + degree y" by simp
huffman@29451
  1450
    also have "\<dots> = degree ((q1 - q2) * y)"
wenzelm@60500
  1451
      using \<open>q1 \<noteq> q2\<close> by (simp add: degree_mult_eq)
huffman@29451
  1452
    also have "\<dots> = degree (r2 - r1)"
huffman@29451
  1453
      using q3 by simp
huffman@29451
  1454
    finally have "degree (r2 - r1) < degree (r2 - r1)" .
huffman@29451
  1455
    then show "False" by simp
huffman@29451
  1456
  qed
huffman@29451
  1457
qed
huffman@29451
  1458
huffman@29660
  1459
lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0"
huffman@29660
  1460
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0)
huffman@29660
  1461
huffman@29660
  1462
lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x"
huffman@29660
  1463
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0)
huffman@29660
  1464
wenzelm@45605
  1465
lemmas pdivmod_rel_unique_div = pdivmod_rel_unique [THEN conjunct1]
huffman@29451
  1466
wenzelm@45605
  1467
lemmas pdivmod_rel_unique_mod = pdivmod_rel_unique [THEN conjunct2]
huffman@29451
  1468
huffman@29451
  1469
instantiation poly :: (field) ring_div
huffman@29451
  1470
begin
huffman@29451
  1471
haftmann@60352
  1472
definition divide_poly where
haftmann@60429
  1473
  div_poly_def: "x div y = (THE q. \<exists>r. pdivmod_rel x y q r)"
huffman@29451
  1474
huffman@29451
  1475
definition mod_poly where
haftmann@37765
  1476
  "x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)"
huffman@29451
  1477
huffman@29451
  1478
lemma div_poly_eq:
haftmann@60429
  1479
  "pdivmod_rel x y q r \<Longrightarrow> x div y = q"
huffman@29451
  1480
unfolding div_poly_def
huffman@29537
  1481
by (fast elim: pdivmod_rel_unique_div)
huffman@29451
  1482
huffman@29451
  1483
lemma mod_poly_eq:
huffman@29537
  1484
  "pdivmod_rel x y q r \<Longrightarrow> x mod y = r"
huffman@29451
  1485
unfolding mod_poly_def
huffman@29537
  1486
by (fast elim: pdivmod_rel_unique_mod)
huffman@29451
  1487
huffman@29537
  1488
lemma pdivmod_rel:
haftmann@60429
  1489
  "pdivmod_rel x y (x div y) (x mod y)"
huffman@29451
  1490
proof -
huffman@29537
  1491
  from pdivmod_rel_exists
huffman@29537
  1492
    obtain q r where "pdivmod_rel x y q r" by fast
huffman@29451
  1493
  thus ?thesis
huffman@29451
  1494
    by (simp add: div_poly_eq mod_poly_eq)
huffman@29451
  1495
qed
huffman@29451
  1496
wenzelm@60679
  1497
instance
wenzelm@60679
  1498
proof
huffman@29451
  1499
  fix x y :: "'a poly"
haftmann@60429
  1500
  show "x div y * y + x mod y = x"
huffman@29537
  1501
    using pdivmod_rel [of x y]
huffman@29537
  1502
    by (simp add: pdivmod_rel_def)
huffman@29451
  1503
next
huffman@29451
  1504
  fix x :: "'a poly"
huffman@29537
  1505
  have "pdivmod_rel x 0 0 x"
huffman@29537
  1506
    by (rule pdivmod_rel_by_0)
haftmann@60429
  1507
  thus "x div 0 = 0"
huffman@29451
  1508
    by (rule div_poly_eq)
huffman@29451
  1509
next
huffman@29451
  1510
  fix y :: "'a poly"
huffman@29537
  1511
  have "pdivmod_rel 0 y 0 0"
huffman@29537
  1512
    by (rule pdivmod_rel_0)
haftmann@60429
  1513
  thus "0 div y = 0"
huffman@29451
  1514
    by (rule div_poly_eq)
huffman@29451
  1515
next
huffman@29451
  1516
  fix x y z :: "'a poly"
huffman@29451
  1517
  assume "y \<noteq> 0"
haftmann@60429
  1518
  hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)"
huffman@29537
  1519
    using pdivmod_rel [of x y]
webertj@49962
  1520
    by (simp add: pdivmod_rel_def distrib_right)
haftmann@60429
  1521
  thus "(x + z * y) div y = z + x div y"
huffman@29451
  1522
    by (rule div_poly_eq)
haftmann@30930
  1523
next
haftmann@30930
  1524
  fix x y z :: "'a poly"
haftmann@30930
  1525
  assume "x \<noteq> 0"
haftmann@60429
  1526
  show "(x * y) div (x * z) = y div z"
haftmann@30930
  1527
  proof (cases "y \<noteq> 0 \<and> z \<noteq> 0")
haftmann@30930
  1528
    have "\<And>x::'a poly. pdivmod_rel x 0 0 x"
haftmann@30930
  1529
      by (rule pdivmod_rel_by_0)
haftmann@60429
  1530
    then have [simp]: "\<And>x::'a poly. x div 0 = 0"
haftmann@30930
  1531
      by (rule div_poly_eq)
haftmann@30930
  1532
    have "\<And>x::'a poly. pdivmod_rel 0 x 0 0"
haftmann@30930
  1533
      by (rule pdivmod_rel_0)
haftmann@60429
  1534
    then have [simp]: "\<And>x::'a poly. 0 div x = 0"
haftmann@30930
  1535
      by (rule div_poly_eq)
haftmann@30930
  1536
    case False then show ?thesis by auto
haftmann@30930
  1537
  next
haftmann@30930
  1538
    case True then have "y \<noteq> 0" and "z \<noteq> 0" by auto
wenzelm@60500
  1539
    with \<open>x \<noteq> 0\<close>
haftmann@30930
  1540
    have "\<And>q r. pdivmod_rel y z q r \<Longrightarrow> pdivmod_rel (x * y) (x * z) q (x * r)"
haftmann@30930
  1541
      by (auto simp add: pdivmod_rel_def algebra_simps)
haftmann@30930
  1542
        (rule classical, simp add: degree_mult_eq)
haftmann@60429
  1543
    moreover from pdivmod_rel have "pdivmod_rel y z (y div z) (y mod z)" .
haftmann@60429
  1544
    ultimately have "pdivmod_rel (x * y) (x * z) (y div z) (x * (y mod z))" .
haftmann@30930
  1545
    then show ?thesis by (simp add: div_poly_eq)
haftmann@30930
  1546
  qed
huffman@29451
  1547
qed
huffman@29451
  1548
huffman@29451
  1549
end
huffman@29451
  1550
haftmann@60570
  1551
lemma is_unit_monom_0:
haftmann@60570
  1552
  fixes a :: "'a::field"
haftmann@60570
  1553
  assumes "a \<noteq> 0"
haftmann@60570
  1554
  shows "is_unit (monom a 0)"
haftmann@60570
  1555
proof
haftmann@62351
  1556
  from assms show "1 = monom a 0 * monom (inverse a) 0"
haftmann@60570
  1557
    by (simp add: mult_monom)
haftmann@60570
  1558
qed
haftmann@60570
  1559
haftmann@60570
  1560
lemma is_unit_triv:
haftmann@60570
  1561
  fixes a :: "'a::field"
haftmann@60570
  1562
  assumes "a \<noteq> 0"
haftmann@60570
  1563
  shows "is_unit [:a:]"
haftmann@60570
  1564
  using assms by (simp add: is_unit_monom_0 monom_0 [symmetric])
haftmann@60570
  1565
haftmann@60570
  1566
lemma is_unit_iff_degree:
haftmann@60570
  1567
  assumes "p \<noteq> 0"
haftmann@60570
  1568
  shows "is_unit p \<longleftrightarrow> degree p = 0" (is "?P \<longleftrightarrow> ?Q")
haftmann@60570
  1569
proof
haftmann@60570
  1570
  assume ?Q
haftmann@60570
  1571
  then obtain a where "p = [:a:]" by (rule degree_eq_zeroE)
haftmann@60570
  1572
  with assms show ?P by (simp add: is_unit_triv)
haftmann@60570
  1573
next
haftmann@60570
  1574
  assume ?P
haftmann@60570
  1575
  then obtain q where "q \<noteq> 0" "p * q = 1" ..
haftmann@60570
  1576
  then have "degree (p * q) = degree 1"
haftmann@60570
  1577
    by simp
haftmann@60570
  1578
  with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> have "degree p + degree q = 0"
haftmann@60570
  1579
    by (simp add: degree_mult_eq)
haftmann@60570
  1580
  then show ?Q by simp
haftmann@60570
  1581
qed
haftmann@60570
  1582
haftmann@60570
  1583
lemma is_unit_pCons_iff:
haftmann@60570
  1584
  "is_unit (pCons a p) \<longleftrightarrow> p = 0 \<and> a \<noteq> 0" (is "?P \<longleftrightarrow> ?Q")
haftmann@60570
  1585
  by (cases "p = 0") (auto simp add: is_unit_triv is_unit_iff_degree)
haftmann@60570
  1586
haftmann@60570
  1587
lemma is_unit_monom_trival:
haftmann@60570
  1588
  fixes p :: "'a::field poly"
haftmann@60570
  1589
  assumes "is_unit p"
haftmann@60570
  1590
  shows "monom (coeff p (degree p)) 0 = p"
haftmann@60570
  1591
  using assms by (cases p) (simp_all add: monom_0 is_unit_pCons_iff)
haftmann@60570
  1592
haftmann@60685
  1593
lemma is_unit_polyE:
haftmann@60685
  1594
  assumes "is_unit p"
haftmann@60685
  1595
  obtains a where "p = monom a 0" and "a \<noteq> 0"
haftmann@60685
  1596
proof -
haftmann@60685
  1597
  obtain a q where "p = pCons a q" by (cases p)
haftmann@60685
  1598
  with assms have "p = [:a:]" and "a \<noteq> 0"
haftmann@60685
  1599
    by (simp_all add: is_unit_pCons_iff)
haftmann@60685
  1600
  with that show thesis by (simp add: monom_0)
haftmann@60685
  1601
qed
haftmann@60685
  1602
haftmann@60685
  1603
instantiation poly :: (field) normalization_semidom
haftmann@60685
  1604
begin
haftmann@60685
  1605
haftmann@60685
  1606
definition normalize_poly :: "'a poly \<Rightarrow> 'a poly"
haftmann@62351
  1607
  where "normalize_poly p = smult (inverse (coeff p (degree p))) p"
haftmann@60685
  1608
haftmann@60685
  1609
definition unit_factor_poly :: "'a poly \<Rightarrow> 'a poly"
haftmann@60685
  1610
  where "unit_factor_poly p = monom (coeff p (degree p)) 0"
haftmann@60685
  1611
haftmann@60685
  1612
instance
haftmann@60685
  1613
proof
haftmann@60685
  1614
  fix p :: "'a poly"
haftmann@60685
  1615
  show "unit_factor p * normalize p = p"
haftmann@62351
  1616
    by (cases "p = 0")
haftmann@62351
  1617
      (simp_all add: normalize_poly_def unit_factor_poly_def,
haftmann@62351
  1618
      simp only: mult_smult_left [symmetric] smult_monom, simp)
haftmann@60685
  1619
next
haftmann@60685
  1620
  show "normalize 0 = (0::'a poly)"
haftmann@60685
  1621
    by (simp add: normalize_poly_def)
haftmann@60685
  1622
next
haftmann@60685
  1623
  show "unit_factor 0 = (0::'a poly)"
haftmann@60685
  1624
    by (simp add: unit_factor_poly_def)
haftmann@60685
  1625
next
haftmann@60685
  1626
  fix p :: "'a poly"
haftmann@60685
  1627
  assume "is_unit p"
haftmann@60685
  1628
  then obtain a where "p = monom a 0" and "a \<noteq> 0"
haftmann@60685
  1629
    by (rule is_unit_polyE)
haftmann@60685
  1630
  then show "normalize p = 1"
haftmann@60685
  1631
    by (auto simp add: normalize_poly_def smult_monom degree_monom_eq)
haftmann@60685
  1632
next
haftmann@60685
  1633
  fix p q :: "'a poly"
haftmann@60685
  1634
  assume "q \<noteq> 0"
haftmann@60685
  1635
  from \<open>q \<noteq> 0\<close> have "is_unit (monom (coeff q (degree q)) 0)"
haftmann@60685
  1636
    by (auto intro: is_unit_monom_0)
haftmann@60685
  1637
  then show "is_unit (unit_factor q)"
haftmann@60685
  1638
    by (simp add: unit_factor_poly_def)
haftmann@60685
  1639
next
haftmann@60685
  1640
  fix p q :: "'a poly"
haftmann@60685
  1641
  have "monom (coeff (p * q) (degree (p * q))) 0 =
haftmann@60685
  1642
    monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0"
haftmann@60685
  1643
    by (simp add: monom_0 coeff_degree_mult)
haftmann@60685
  1644
  then show "unit_factor (p * q) =
haftmann@60685
  1645
    unit_factor p * unit_factor q"
haftmann@60685
  1646
    by (simp add: unit_factor_poly_def)
haftmann@60685
  1647
qed
haftmann@60685
  1648
haftmann@60685
  1649
end
haftmann@60685
  1650
haftmann@62351
  1651
lemma unit_factor_monom [simp]:
haftmann@62351
  1652
  "unit_factor (monom a n) =
haftmann@62351
  1653
     (if a = 0 then 0 else monom a 0)"
haftmann@62351
  1654
  by (simp add: unit_factor_poly_def degree_monom_eq)
haftmann@62351
  1655
haftmann@62351
  1656
lemma unit_factor_pCons [simp]:
haftmann@62351
  1657
  "unit_factor (pCons a p) =
haftmann@62351
  1658
     (if p = 0 then monom a 0 else unit_factor p)"
haftmann@62351
  1659
  by (simp add: unit_factor_poly_def)
haftmann@62351
  1660
haftmann@62351
  1661
lemma normalize_monom [simp]:
haftmann@62351
  1662
  "normalize (monom a n) =
haftmann@62351
  1663
     (if a = 0 then 0 else monom 1 n)"
haftmann@62351
  1664
  by (simp add: normalize_poly_def degree_monom_eq smult_monom)
haftmann@62351
  1665
huffman@29451
  1666
lemma degree_mod_less:
huffman@29451
  1667
  "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
huffman@29537
  1668
  using pdivmod_rel [of x y]
huffman@29537
  1669
  unfolding pdivmod_rel_def by simp
huffman@29451
  1670
huffman@29451
  1671
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
huffman@29451
  1672
proof -
huffman@29451
  1673
  assume "degree x < degree y"
huffman@29537
  1674
  hence "pdivmod_rel x y 0 x"
huffman@29537
  1675
    by (simp add: pdivmod_rel_def)
huffman@29451
  1676
  thus "x div y = 0" by (rule div_poly_eq)
huffman@29451
  1677
qed
huffman@29451
  1678
huffman@29451
  1679
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
huffman@29451
  1680
proof -
huffman@29451
  1681
  assume "degree x < degree y"
huffman@29537
  1682
  hence "pdivmod_rel x y 0 x"
huffman@29537
  1683
    by (simp add: pdivmod_rel_def)
huffman@29451
  1684
  thus "x mod y = x" by (rule mod_poly_eq)
huffman@29451
  1685
qed
huffman@29451
  1686
huffman@29659
  1687
lemma pdivmod_rel_smult_left:
huffman@29659
  1688
  "pdivmod_rel x y q r
huffman@29659
  1689
    \<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)"
huffman@29659
  1690
  unfolding pdivmod_rel_def by (simp add: smult_add_right)
huffman@29659
  1691
huffman@29659
  1692
lemma div_smult_left: "(smult a x) div y = smult a (x div y)"
huffman@29659
  1693
  by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
huffman@29659
  1694
huffman@29659
  1695
lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"
huffman@29659
  1696
  by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
huffman@29659
  1697
huffman@30072
  1698
lemma poly_div_minus_left [simp]:
huffman@30072
  1699
  fixes x y :: "'a::field poly"
huffman@30072
  1700
  shows "(- x) div y = - (x div y)"
haftmann@54489
  1701
  using div_smult_left [of "- 1::'a"] by simp
huffman@30072
  1702
huffman@30072
  1703
lemma poly_mod_minus_left [simp]:
huffman@30072
  1704
  fixes x y :: "'a::field poly"
huffman@30072
  1705
  shows "(- x) mod y = - (x mod y)"
haftmann@54489
  1706
  using mod_smult_left [of "- 1::'a"] by simp
huffman@30072
  1707
huffman@57482
  1708
lemma pdivmod_rel_add_left:
huffman@57482
  1709
  assumes "pdivmod_rel x y q r"
huffman@57482
  1710
  assumes "pdivmod_rel x' y q' r'"
huffman@57482
  1711
  shows "pdivmod_rel (x + x') y (q + q') (r + r')"
huffman@57482
  1712
  using assms unfolding pdivmod_rel_def
haftmann@59557
  1713
  by (auto simp add: algebra_simps degree_add_less)
huffman@57482
  1714
huffman@57482
  1715
lemma poly_div_add_left:
huffman@57482
  1716
  fixes x y z :: "'a::field poly"
huffman@57482
  1717
  shows "(x + y) div z = x div z + y div z"
huffman@57482
  1718
  using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel]
huffman@57482
  1719
  by (rule div_poly_eq)
huffman@57482
  1720
huffman@57482
  1721
lemma poly_mod_add_left:
huffman@57482
  1722
  fixes x y z :: "'a::field poly"
huffman@57482
  1723
  shows "(x + y) mod z = x mod z + y mod z"
huffman@57482
  1724
  using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel]
huffman@57482
  1725
  by (rule mod_poly_eq)
huffman@57482
  1726
huffman@57482
  1727
lemma poly_div_diff_left:
huffman@57482
  1728
  fixes x y z :: "'a::field poly"
huffman@57482
  1729
  shows "(x - y) div z = x div z - y div z"
huffman@57482
  1730
  by (simp only: diff_conv_add_uminus poly_div_add_left poly_div_minus_left)
huffman@57482
  1731
huffman@57482
  1732
lemma poly_mod_diff_left:
huffman@57482
  1733
  fixes x y z :: "'a::field poly"
huffman@57482
  1734
  shows "(x - y) mod z = x mod z - y mod z"
huffman@57482
  1735
  by (simp only: diff_conv_add_uminus poly_mod_add_left poly_mod_minus_left)
huffman@57482
  1736
huffman@29659
  1737
lemma pdivmod_rel_smult_right:
huffman@29659
  1738
  "\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk>
huffman@29659
  1739
    \<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r"
huffman@29659
  1740
  unfolding pdivmod_rel_def by simp
huffman@29659
  1741
huffman@29659
  1742
lemma div_smult_right:
huffman@29659
  1743
  "a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)"
huffman@29659
  1744
  by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
huffman@29659
  1745
huffman@29659
  1746
lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y"
huffman@29659
  1747
  by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
huffman@29659
  1748
huffman@30072
  1749
lemma poly_div_minus_right [simp]:
huffman@30072
  1750
  fixes x y :: "'a::field poly"
huffman@30072
  1751
  shows "x div (- y) = - (x div y)"
haftmann@54489
  1752
  using div_smult_right [of "- 1::'a"] by (simp add: nonzero_inverse_minus_eq)
huffman@30072
  1753
huffman@30072
  1754
lemma poly_mod_minus_right [simp]:
huffman@30072
  1755
  fixes x y :: "'a::field poly"
huffman@30072
  1756
  shows "x mod (- y) = x mod y"
haftmann@54489
  1757
  using mod_smult_right [of "- 1::'a"] by simp
huffman@30072
  1758
huffman@29660
  1759
lemma pdivmod_rel_mult:
huffman@29660
  1760
  "\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk>
huffman@29660
  1761
    \<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)"
huffman@29660
  1762
apply (cases "z = 0", simp add: pdivmod_rel_def)
huffman@29660
  1763
apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff)
huffman@29660
  1764
apply (cases "r = 0")
huffman@29660
  1765
apply (cases "r' = 0")
huffman@29660
  1766
apply (simp add: pdivmod_rel_def)
haftmann@36350
  1767
apply (simp add: pdivmod_rel_def field_simps degree_mult_eq)
huffman@29660
  1768
apply (cases "r' = 0")
huffman@29660
  1769
apply (simp add: pdivmod_rel_def degree_mult_eq)
haftmann@36350
  1770
apply (simp add: pdivmod_rel_def field_simps)
huffman@29660
  1771
apply (simp add: degree_mult_eq degree_add_less)
huffman@29660
  1772
done
huffman@29660
  1773
huffman@29660
  1774
lemma poly_div_mult_right:
huffman@29660
  1775
  fixes x y z :: "'a::field poly"
huffman@29660
  1776
  shows "x div (y * z) = (x div y) div z"
huffman@29660
  1777
  by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
huffman@29660
  1778
huffman@29660
  1779
lemma poly_mod_mult_right:
huffman@29660
  1780
  fixes x y z :: "'a::field poly"
huffman@29660
  1781
  shows "x mod (y * z) = y * (x div y mod z) + x mod y"
huffman@29660
  1782
  by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
huffman@29660
  1783
huffman@29451
  1784
lemma mod_pCons:
huffman@29451
  1785
  fixes a and x
huffman@29451
  1786
  assumes y: "y \<noteq> 0"
huffman@29451
  1787
  defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"
huffman@29451
  1788
  shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
huffman@29451
  1789
unfolding b
huffman@29451
  1790
apply (rule mod_poly_eq)
huffman@29537
  1791
apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl])
huffman@29451
  1792
done
huffman@29451
  1793
haftmann@52380
  1794
definition pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly"
haftmann@52380
  1795
where
haftmann@52380
  1796
  "pdivmod p q = (p div q, p mod q)"
huffman@31663
  1797
haftmann@52380
  1798
lemma div_poly_code [code]: 
haftmann@52380
  1799
  "p div q = fst (pdivmod p q)"
haftmann@52380
  1800
  by (simp add: pdivmod_def)
huffman@31663
  1801
haftmann@52380
  1802
lemma mod_poly_code [code]:
haftmann@52380
  1803
  "p mod q = snd (pdivmod p q)"
haftmann@52380
  1804
  by (simp add: pdivmod_def)
huffman@31663
  1805
haftmann@52380
  1806
lemma pdivmod_0:
haftmann@52380
  1807
  "pdivmod 0 q = (0, 0)"
haftmann@52380
  1808
  by (simp add: pdivmod_def)
huffman@31663
  1809
haftmann@52380
  1810
lemma pdivmod_pCons:
haftmann@52380
  1811
  "pdivmod (pCons a p) q =
haftmann@52380
  1812
    (if q = 0 then (0, pCons a p) else
haftmann@52380
  1813
      (let (s, r) = pdivmod p q;
haftmann@52380
  1814
           b = coeff (pCons a r) (degree q) / coeff q (degree q)
haftmann@52380
  1815
        in (pCons b s, pCons a r - smult b q)))"
haftmann@52380
  1816
  apply (simp add: pdivmod_def Let_def, safe)
haftmann@52380
  1817
  apply (rule div_poly_eq)
haftmann@52380
  1818
  apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
haftmann@52380
  1819
  apply (rule mod_poly_eq)
haftmann@52380
  1820
  apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
huffman@29451
  1821
  done
huffman@29451
  1822
haftmann@52380
  1823
lemma pdivmod_fold_coeffs [code]:
haftmann@52380
  1824
  "pdivmod p q = (if q = 0 then (0, p)
haftmann@52380
  1825
    else fold_coeffs (\<lambda>a (s, r).
haftmann@52380
  1826
      let b = coeff (pCons a r) (degree q) / coeff q (degree q)
haftmann@52380
  1827
      in (pCons b s, pCons a r - smult b q)
haftmann@52380
  1828
   ) p (0, 0))"
haftmann@52380
  1829
  apply (cases "q = 0")
haftmann@52380
  1830
  apply (simp add: pdivmod_def)
haftmann@52380
  1831
  apply (rule sym)
haftmann@52380
  1832
  apply (induct p)
haftmann@52380
  1833
  apply (simp_all add: pdivmod_0 pdivmod_pCons)
haftmann@52380
  1834
  apply (case_tac "a = 0 \<and> p = 0")
haftmann@52380
  1835
  apply (auto simp add: pdivmod_def)
haftmann@52380
  1836
  done
huffman@29980
  1837
huffman@29980
  1838
wenzelm@60500
  1839
subsection \<open>Order of polynomial roots\<close>
huffman@29977
  1840
haftmann@52380
  1841
definition order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat"
huffman@29977
  1842
where
huffman@29977
  1843
  "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
huffman@29977
  1844
huffman@29977
  1845
lemma coeff_linear_power:
huffman@29979
  1846
  fixes a :: "'a::comm_semiring_1"
huffman@29977
  1847
  shows "coeff ([:a, 1:] ^ n) n = 1"
huffman@29977
  1848
apply (induct n, simp_all)
huffman@29977
  1849
apply (subst coeff_eq_0)
huffman@29977
  1850
apply (auto intro: le_less_trans degree_power_le)
huffman@29977
  1851
done
huffman@29977
  1852
huffman@29977
  1853
lemma degree_linear_power:
huffman@29979
  1854
  fixes a :: "'a::comm_semiring_1"
huffman@29977
  1855
  shows "degree ([:a, 1:] ^ n) = n"
huffman@29977
  1856
apply (rule order_antisym)
huffman@29977
  1857
apply (rule ord_le_eq_trans [OF degree_power_le], simp)
huffman@29977
  1858
apply (rule le_degree, simp add: coeff_linear_power)
huffman@29977
  1859
done
huffman@29977
  1860
huffman@29977
  1861
lemma order_1: "[:-a, 1:] ^ order a p dvd p"
huffman@29977
  1862
apply (cases "p = 0", simp)
huffman@29977
  1863
apply (cases "order a p", simp)
huffman@29977
  1864
apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
huffman@29977
  1865
apply (drule not_less_Least, simp)
huffman@29977
  1866
apply (fold order_def, simp)
huffman@29977
  1867
done
huffman@29977
  1868
huffman@29977
  1869
lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
huffman@29977
  1870
unfolding order_def
huffman@29977
  1871
apply (rule LeastI_ex)
huffman@29977
  1872
apply (rule_tac x="degree p" in exI)
huffman@29977
  1873
apply (rule notI)
huffman@29977
  1874
apply (drule (1) dvd_imp_degree_le)
huffman@29977
  1875
apply (simp only: degree_linear_power)
huffman@29977
  1876
done
huffman@29977
  1877
huffman@29977
  1878
lemma order:
huffman@29977
  1879
  "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
huffman@29977
  1880
by (rule conjI [OF order_1 order_2])
huffman@29977
  1881
huffman@29977
  1882
lemma order_degree:
huffman@29977
  1883
  assumes p: "p \<noteq> 0"
huffman@29977
  1884
  shows "order a p \<le> degree p"
huffman@29977
  1885
proof -
huffman@29977
  1886
  have "order a p = degree ([:-a, 1:] ^ order a p)"
huffman@29977
  1887
    by (simp only: degree_linear_power)
huffman@29977
  1888
  also have "\<dots> \<le> degree p"
huffman@29977
  1889
    using order_1 p by (rule dvd_imp_degree_le)
huffman@29977
  1890
  finally show ?thesis .
huffman@29977
  1891
qed
huffman@29977
  1892
huffman@29977
  1893
lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
huffman@29977
  1894
apply (cases "p = 0", simp_all)
huffman@29977
  1895
apply (rule iffI)
lp15@56383
  1896
apply (metis order_2 not_gr0 poly_eq_0_iff_dvd power_0 power_Suc_0 power_one_right)
lp15@56383
  1897
unfolding poly_eq_0_iff_dvd
lp15@56383
  1898
apply (metis dvd_power dvd_trans order_1)
huffman@29977
  1899
done
huffman@29977
  1900
eberlm@62065
  1901
lemma order_0I: "poly p a \<noteq> 0 \<Longrightarrow> order a p = 0"
eberlm@62065
  1902
  by (subst (asm) order_root) auto
eberlm@62065
  1903
huffman@29977
  1904
eberlm@62065
  1905
subsection \<open>Additional induction rules on polynomials\<close>
eberlm@62065
  1906
eberlm@62065
  1907
text \<open>
eberlm@62065
  1908
  An induction rule for induction over the roots of a polynomial with a certain property. 
eberlm@62065
  1909
  (e.g. all positive roots)
eberlm@62065
  1910
\<close>
eberlm@62065
  1911
lemma poly_root_induct [case_names 0 no_roots root]:
eberlm@62065
  1912
  fixes p :: "'a :: idom poly"
eberlm@62065
  1913
  assumes "Q 0"
eberlm@62065
  1914
  assumes "\<And>p. (\<And>a. P a \<Longrightarrow> poly p a \<noteq> 0) \<Longrightarrow> Q p"
eberlm@62065
  1915
  assumes "\<And>a p. P a \<Longrightarrow> Q p \<Longrightarrow> Q ([:a, -1:] * p)"
eberlm@62065
  1916
  shows   "Q p"
eberlm@62065
  1917
proof (induction "degree p" arbitrary: p rule: less_induct)
eberlm@62065
  1918
  case (less p)
eberlm@62065
  1919
  show ?case
eberlm@62065
  1920
  proof (cases "p = 0")
eberlm@62065
  1921
    assume nz: "p \<noteq> 0"
eberlm@62065
  1922
    show ?case
eberlm@62065
  1923
    proof (cases "\<exists>a. P a \<and> poly p a = 0")
eberlm@62065
  1924
      case False
eberlm@62065
  1925
      thus ?thesis by (intro assms(2)) blast
eberlm@62065
  1926
    next
eberlm@62065
  1927
      case True
eberlm@62065
  1928
      then obtain a where a: "P a" "poly p a = 0" 
eberlm@62065
  1929
        by blast
eberlm@62065
  1930
      hence "-[:-a, 1:] dvd p" 
eberlm@62065
  1931
        by (subst minus_dvd_iff) (simp add: poly_eq_0_iff_dvd)
eberlm@62065
  1932
      then obtain q where q: "p = [:a, -1:] * q" by (elim dvdE) simp
eberlm@62065
  1933
      with nz have q_nz: "q \<noteq> 0" by auto
eberlm@62065
  1934
      have "degree p = Suc (degree q)"
eberlm@62065
  1935
        by (subst q, subst degree_mult_eq) (simp_all add: q_nz)
eberlm@62065
  1936
      hence "Q q" by (intro less) simp
eberlm@62065
  1937
      from a(1) and this have "Q ([:a, -1:] * q)" 
eberlm@62065
  1938
        by (rule assms(3))
eberlm@62065
  1939
      with q show ?thesis by simp
eberlm@62065
  1940
    qed
eberlm@62065
  1941
  qed (simp add: assms(1))
eberlm@62065
  1942
qed
eberlm@62065
  1943
eberlm@62065
  1944
lemma dropWhile_replicate_append: 
eberlm@62065
  1945
  "dropWhile (op= a) (replicate n a @ ys) = dropWhile (op= a) ys"
eberlm@62065
  1946
  by (induction n) simp_all
eberlm@62065
  1947
eberlm@62065
  1948
lemma Poly_append_replicate_0: "Poly (xs @ replicate n 0) = Poly xs"
eberlm@62065
  1949
  by (subst coeffs_eq_iff) (simp_all add: strip_while_def dropWhile_replicate_append)
eberlm@62065
  1950
eberlm@62065
  1951
text \<open>
eberlm@62065
  1952
  An induction rule for simultaneous induction over two polynomials, 
eberlm@62065
  1953
  prepending one coefficient in each step.
eberlm@62065
  1954
\<close>
eberlm@62065
  1955
lemma poly_induct2 [case_names 0 pCons]:
eberlm@62065
  1956
  assumes "P 0 0" "\<And>a p b q. P p q \<Longrightarrow> P (pCons a p) (pCons b q)"
eberlm@62065
  1957
  shows   "P p q"
eberlm@62065
  1958
proof -
eberlm@62065
  1959
  def n \<equiv> "max (length (coeffs p)) (length (coeffs q))"
eberlm@62065
  1960
  def xs \<equiv> "coeffs p @ (replicate (n - length (coeffs p)) 0)"
eberlm@62065
  1961
  def ys \<equiv> "coeffs q @ (replicate (n - length (coeffs q)) 0)"
eberlm@62065
  1962
  have "length xs = length ys" 
eberlm@62065
  1963
    by (simp add: xs_def ys_def n_def)
eberlm@62065
  1964
  hence "P (Poly xs) (Poly ys)" 
eberlm@62065
  1965
    by (induction rule: list_induct2) (simp_all add: assms)
eberlm@62065
  1966
  also have "Poly xs = p" 
eberlm@62065
  1967
    by (simp add: xs_def Poly_append_replicate_0)
eberlm@62065
  1968
  also have "Poly ys = q" 
eberlm@62065
  1969
    by (simp add: ys_def Poly_append_replicate_0)
eberlm@62065
  1970
  finally show ?thesis .
eberlm@62065
  1971
qed
eberlm@62065
  1972
eberlm@62065
  1973
wenzelm@60500
  1974
subsection \<open>Composition of polynomials\<close>
huffman@29478
  1975
eberlm@62128
  1976
(* Several lemmas contributed by René Thiemann and Akihisa Yamada *)
eberlm@62128
  1977
haftmann@52380
  1978
definition pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
haftmann@52380
  1979
where
haftmann@52380
  1980
  "pcompose p q = fold_coeffs (\<lambda>a c. [:a:] + q * c) p 0"
haftmann@52380
  1981
eberlm@62128
  1982
notation pcompose (infixl "\<circ>\<^sub>p" 71)
eberlm@62128
  1983
haftmann@52380
  1984
lemma pcompose_0 [simp]:
haftmann@52380
  1985
  "pcompose 0 q = 0"
haftmann@52380
  1986
  by (simp add: pcompose_def)
eberlm@62128
  1987
  
haftmann@52380
  1988
lemma pcompose_pCons:
haftmann@52380
  1989
  "pcompose (pCons a p) q = [:a:] + q * pcompose p q"
haftmann@52380
  1990
  by (cases "p = 0 \<and> a = 0") (auto simp add: pcompose_def)
haftmann@52380
  1991
eberlm@62128
  1992
lemma pcompose_1:
eberlm@62128
  1993
  fixes p :: "'a :: comm_semiring_1 poly"
eberlm@62128
  1994
  shows "pcompose 1 p = 1"
eberlm@62128
  1995
  unfolding one_poly_def by (auto simp: pcompose_pCons)
eberlm@62128
  1996
haftmann@52380
  1997
lemma poly_pcompose:
haftmann@52380
  1998
  "poly (pcompose p q) x = poly p (poly q x)"
haftmann@52380
  1999
  by (induct p) (simp_all add: pcompose_pCons)
haftmann@52380
  2000
haftmann@52380
  2001
lemma degree_pcompose_le:
haftmann@52380
  2002
  "degree (pcompose p q) \<le> degree p * degree q"
haftmann@52380
  2003
apply (induct p, simp)
haftmann@52380
  2004
apply (simp add: pcompose_pCons, clarify)
haftmann@52380
  2005
apply (rule degree_add_le, simp)
haftmann@52380
  2006
apply (rule order_trans [OF degree_mult_le], simp)
huffman@29478
  2007
done
huffman@29478
  2008
eberlm@62065
  2009
lemma pcompose_add:
eberlm@62065
  2010
  fixes p q r :: "'a :: {comm_semiring_0, ab_semigroup_add} poly"
eberlm@62065
  2011
  shows "pcompose (p + q) r = pcompose p r + pcompose q r"
eberlm@62065
  2012
proof (induction p q rule: poly_induct2)
eberlm@62065
  2013
  case (pCons a p b q)
eberlm@62065
  2014
  have "pcompose (pCons a p + pCons b q) r = 
eberlm@62065
  2015
          [:a + b:] + r * pcompose p r + r * pcompose q r"
eberlm@62065
  2016
    by (simp_all add: pcompose_pCons pCons.IH algebra_simps)
eberlm@62065
  2017
  also have "[:a + b:] = [:a:] + [:b:]" by simp
eberlm@62065
  2018
  also have "\<dots> + r * pcompose p r + r * pcompose q r = 
eberlm@62065
  2019
                 pcompose (pCons a p) r + pcompose (pCons b q) r"
eberlm@62065
  2020
    by (simp only: pcompose_pCons add_ac)
eberlm@62065
  2021
  finally show ?case .
eberlm@62065
  2022
qed simp
eberlm@62065
  2023
eberlm@62128
  2024
lemma pcompose_uminus:
eberlm@62065
  2025
  fixes p r :: "'a :: comm_ring poly"
eberlm@62065
  2026
  shows "pcompose (-p) r = -pcompose p r"
eberlm@62065
  2027
  by (induction p) (simp_all add: pcompose_pCons)
eberlm@62065
  2028
eberlm@62065
  2029
lemma pcompose_diff:
eberlm@62065
  2030
  fixes p q r :: "'a :: comm_ring poly"
eberlm@62065
  2031
  shows "pcompose (p - q) r = pcompose p r - pcompose q r"
eberlm@62128
  2032
  using pcompose_add[of p "-q"] by (simp add: pcompose_uminus)
eberlm@62065
  2033
eberlm@62065
  2034
lemma pcompose_smult:
eberlm@62065
  2035
  fixes p r :: "'a :: comm_semiring_0 poly"
eberlm@62065
  2036
  shows "pcompose (smult a p) r = smult a (pcompose p r)"
eberlm@62065
  2037
  by (induction p) 
eberlm@62065
  2038
     (simp_all add: pcompose_pCons pcompose_add smult_add_right)
eberlm@62065
  2039
eberlm@62065
  2040
lemma pcompose_mult:
eberlm@62065
  2041
  fixes p q r :: "'a :: comm_semiring_0 poly"
eberlm@62065
  2042
  shows "pcompose (p * q) r = pcompose p r * pcompose q r"
eberlm@62065
  2043
  by (induction p arbitrary: q)
eberlm@62065
  2044
     (simp_all add: pcompose_add pcompose_smult pcompose_pCons algebra_simps)
eberlm@62065
  2045
eberlm@62065
  2046
lemma pcompose_assoc: 
eberlm@62065
  2047
  "pcompose p (pcompose q r :: 'a :: comm_semiring_0 poly ) =
eberlm@62065
  2048
     pcompose (pcompose p q) r"
eberlm@62065
  2049
  by (induction p arbitrary: q) 
eberlm@62065
  2050
     (simp_all add: pcompose_pCons pcompose_add pcompose_mult)
eberlm@62065
  2051
eberlm@62128
  2052
lemma pcompose_idR[simp]:
eberlm@62128
  2053
  fixes p :: "'a :: comm_semiring_1 poly"
eberlm@62128
  2054
  shows "pcompose p [: 0, 1 :] = p"
eberlm@62128
  2055
  by (induct p; simp add: pcompose_pCons)
eberlm@62128
  2056
eberlm@62065
  2057
eberlm@62065
  2058
(* The remainder of this section and the next were contributed by Wenda Li *)
eberlm@62065
  2059
eberlm@62065
  2060
lemma degree_mult_eq_0:
eberlm@62128
  2061
  fixes p q:: "'a :: semidom poly"
eberlm@62065
  2062
  shows "degree (p*q) = 0 \<longleftrightarrow> p=0 \<or> q=0 \<or> (p\<noteq>0 \<and> q\<noteq>0 \<and> degree p =0 \<and> degree q =0)"
eberlm@62065
  2063
by (auto simp add:degree_mult_eq)
eberlm@62065
  2064
eberlm@62065
  2065
lemma pcompose_const[simp]:"pcompose [:a:] q = [:a:]" by (subst pcompose_pCons,simp) 
eberlm@62065
  2066
eberlm@62128
  2067
lemma pcompose_0': "pcompose p 0 = [:coeff p 0:]"
eberlm@62128
  2068
  by (induct p) (auto simp add:pcompose_pCons)
eberlm@62065
  2069
eberlm@62065
  2070
lemma degree_pcompose:
eberlm@62128
  2071
  fixes p q:: "'a::semidom poly"
eberlm@62128
  2072
  shows "degree (pcompose p q) = degree p * degree q"
eberlm@62065
  2073
proof (induct p)
eberlm@62065
  2074
  case 0
eberlm@62065
  2075
  thus ?case by auto
eberlm@62065
  2076
next
eberlm@62065
  2077
  case (pCons a p)
eberlm@62065
  2078
  have "degree (q * pcompose p q) = 0 \<Longrightarrow> ?case" 
eberlm@62065
  2079
    proof (cases "p=0")
eberlm@62065
  2080
      case True
eberlm@62065
  2081
      thus ?thesis by auto
eberlm@62065
  2082
    next
eberlm@62065
  2083
      case False assume "degree (q * pcompose p q) = 0"
eberlm@62128
  2084
      hence "degree q=0 \<or> pcompose p q=0" by (auto simp add: degree_mult_eq_0)
wenzelm@62072
  2085
      moreover have "\<lbrakk>pcompose p q=0;degree q\<noteq>0\<rbrakk> \<Longrightarrow> False" using pCons.hyps(2) \<open>p\<noteq>0\<close> 
eberlm@62065
  2086
        proof -
eberlm@62065
  2087
          assume "pcompose p q=0" "degree q\<noteq>0"
eberlm@62065
  2088
          hence "degree p=0" using pCons.hyps(2) by auto
eberlm@62065
  2089
          then obtain a1 where "p=[:a1:]"
eberlm@62065
  2090
            by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases)
wenzelm@62072
  2091
          thus False using \<open>pcompose p q=0\<close> \<open>p\<noteq>0\<close> by auto
eberlm@62065
  2092
        qed
eberlm@62065
  2093
      ultimately have "degree (pCons a p) * degree q=0" by auto
eberlm@62065
  2094
      moreover have "degree (pcompose (pCons a p) q) = 0" 
eberlm@62065
  2095
        proof -
eberlm@62065
  2096
          have" 0 = max (degree [:a:]) (degree (q*pcompose p q))"
wenzelm@62072
  2097
            using \<open>degree (q * pcompose p q) = 0\<close> by simp
eberlm@62065
  2098
          also have "... \<ge> degree ([:a:] + q * pcompose p q)"
eberlm@62065
  2099
            by (rule degree_add_le_max)
eberlm@62065
  2100
          finally show ?thesis by (auto simp add:pcompose_pCons)
eberlm@62065
  2101
        qed
eberlm@62065
  2102
      ultimately show ?thesis by simp
eberlm@62065
  2103
    qed
eberlm@62065
  2104
  moreover have "degree (q * pcompose p q)>0 \<Longrightarrow> ?case" 
eberlm@62065
  2105
    proof -
eberlm@62065
  2106
      assume asm:"0 < degree (q * pcompose p q)"
eberlm@62065
  2107
      hence "p\<noteq>0" "q\<noteq>0" "pcompose p q\<noteq>0" by auto
eberlm@62065
  2108
      have "degree (pcompose (pCons a p) q) = degree ( q * pcompose p q)"
eberlm@62065
  2109
        unfolding pcompose_pCons
eberlm@62065
  2110
        using degree_add_eq_right[of "[:a:]" ] asm by auto       
eberlm@62065
  2111
      thus ?thesis 
wenzelm@62072
  2112
        using pCons.hyps(2) degree_mult_eq[OF \<open>q\<noteq>0\<close> \<open>pcompose p q\<noteq>0\<close>] by auto
eberlm@62065
  2113
    qed
eberlm@62065
  2114
  ultimately show ?case by blast
eberlm@62065
  2115
qed
eberlm@62065
  2116
eberlm@62065
  2117
lemma pcompose_eq_0:
eberlm@62128
  2118
  fixes p q:: "'a :: semidom poly"
eberlm@62128
  2119
  assumes "pcompose p q = 0" "degree q > 0" 
eberlm@62128
  2120
  shows "p = 0"
eberlm@62065
  2121
proof -
eberlm@62065
  2122
  have "degree p=0" using assms degree_pcompose[of p q] by auto
eberlm@62065
  2123
  then obtain a where "p=[:a:]" 
eberlm@62065
  2124
    by (metis degree_pCons_eq_if gr0_conv_Suc neq0_conv pCons_cases)
eberlm@62065
  2125
  hence "a=0" using assms(1) by auto
wenzelm@62072
  2126
  thus ?thesis using \<open>p=[:a:]\<close> by simp
eberlm@62065
  2127
qed
eberlm@62065
  2128
eberlm@62065
  2129
wenzelm@62072
  2130
subsection \<open>Leading coefficient\<close>
eberlm@62065
  2131
eberlm@62065
  2132
definition lead_coeff:: "'a::zero poly \<Rightarrow> 'a" where
eberlm@62065
  2133
  "lead_coeff p= coeff p (degree p)"
eberlm@62065
  2134
eberlm@62065
  2135
lemma lead_coeff_pCons[simp]:
eberlm@62065
  2136
    "p\<noteq>0 \<Longrightarrow>lead_coeff (pCons a p) = lead_coeff p"
eberlm@62065
  2137
    "p=0 \<Longrightarrow> lead_coeff (pCons a p) = a"
eberlm@62065
  2138
unfolding lead_coeff_def by auto
eberlm@62065
  2139
eberlm@62065
  2140
lemma lead_coeff_0[simp]:"lead_coeff 0 =0" 
eberlm@62065
  2141
  unfolding lead_coeff_def by auto
eberlm@62065
  2142
eberlm@62065
  2143
lemma lead_coeff_mult:
eberlm@62065
  2144
   fixes p q::"'a ::idom poly"
eberlm@62065
  2145
   shows "lead_coeff (p * q) = lead_coeff p * lead_coeff q"
eberlm@62065
  2146
by (unfold lead_coeff_def,cases "p=0 \<or> q=0",auto simp add:coeff_mult_degree_sum degree_mult_eq)
eberlm@62065
  2147
eberlm@62065
  2148
lemma lead_coeff_add_le:
eberlm@62065
  2149
  assumes "degree p < degree q"
eberlm@62065
  2150
  shows "lead_coeff (p+q) = lead_coeff q" 
eberlm@62065
  2151
using assms unfolding lead_coeff_def
eberlm@62065
  2152
by (metis coeff_add coeff_eq_0 monoid_add_class.add.left_neutral degree_add_eq_right)
eberlm@62065
  2153
eberlm@62065
  2154
lemma lead_coeff_minus:
eberlm@62065
  2155
  "lead_coeff (-p) = - lead_coeff p"
eberlm@62065
  2156
by (metis coeff_minus degree_minus lead_coeff_def)
eberlm@62065
  2157
eberlm@62065
  2158
eberlm@62065
  2159
lemma lead_coeff_comp:
eberlm@62065
  2160
  fixes p q:: "'a::idom poly"
eberlm@62065
  2161
  assumes "degree q > 0" 
eberlm@62065
  2162
  shows "lead_coeff (pcompose p q) = lead_coeff p * lead_coeff q ^ (degree p)"
eberlm@62065
  2163
proof (induct p)
eberlm@62065
  2164
  case 0
eberlm@62065
  2165
  thus ?case unfolding lead_coeff_def by auto
eberlm@62065
  2166
next
eberlm@62065
  2167
  case (pCons a p)
eberlm@62065
  2168
  have "degree ( q * pcompose p q) = 0 \<Longrightarrow> ?case"
eberlm@62065
  2169
    proof -
eberlm@62065
  2170
      assume "degree ( q * pcompose p q) = 0"
eberlm@62065
  2171
      hence "pcompose p q = 0" by (metis assms degree_0 degree_mult_eq_0 neq0_conv)
wenzelm@62072
  2172
      hence "p=0" using pcompose_eq_0[OF _ \<open>degree q > 0\<close>] by simp
eberlm@62065
  2173
      thus ?thesis by auto
eberlm@62065
  2174
    qed
eberlm@62065
  2175
  moreover have "degree ( q * pcompose p q) > 0 \<Longrightarrow> ?case" 
eberlm@62065
  2176
    proof -
eberlm@62065
  2177
      assume "degree ( q * pcompose p q) > 0"
eberlm@62065
  2178
      hence "lead_coeff (pcompose (pCons a p) q) =lead_coeff ( q * pcompose p q)"
eberlm@62065
  2179
        by (auto simp add:pcompose_pCons lead_coeff_add_le)
eberlm@62065
  2180
      also have "... = lead_coeff q * (lead_coeff p * lead_coeff q ^ degree p)"
eberlm@62065
  2181
        using pCons.hyps(2) lead_coeff_mult[of q "pcompose p q"] by simp
eberlm@62065
  2182
      also have "... = lead_coeff p * lead_coeff q ^ (degree p + 1)"
eberlm@62065
  2183
        by auto
eberlm@62065
  2184
      finally show ?thesis by auto
eberlm@62065
  2185
    qed
eberlm@62065
  2186
  ultimately show ?case by blast
eberlm@62065
  2187
qed
eberlm@62065
  2188
eberlm@62065
  2189
lemma lead_coeff_smult: 
eberlm@62065
  2190
  "lead_coeff (smult c p :: 'a :: idom poly) = c * lead_coeff p"
eberlm@62065
  2191
proof -
eberlm@62065
  2192
  have "smult c p = [:c:] * p" by simp
eberlm@62065
  2193
  also have "lead_coeff \<dots> = c * lead_coeff p" 
eberlm@62065
  2194
    by (subst lead_coeff_mult) simp_all
eberlm@62065
  2195
  finally show ?thesis .
eberlm@62065
  2196
qed
eberlm@62065
  2197
eberlm@62065
  2198
lemma lead_coeff_1 [simp]: "lead_coeff 1 = 1"
eberlm@62065
  2199
  by (simp add: lead_coeff_def)
eberlm@62065
  2200
eberlm@62065
  2201
lemma lead_coeff_of_nat [simp]:
eberlm@62065
  2202
  "lead_coeff (of_nat n) = (of_nat n :: 'a :: {comm_semiring_1,semiring_char_0})"
eberlm@62065
  2203
  by (induction n) (simp_all add: lead_coeff_def of_nat_poly)
eberlm@62065
  2204
eberlm@62065
  2205
lemma lead_coeff_numeral [simp]: 
eberlm@62065
  2206
  "lead_coeff (numeral n) = numeral n"
eberlm@62065
  2207
  unfolding lead_coeff_def
eberlm@62065
  2208
  by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp
eberlm@62065
  2209
eberlm@62065
  2210
lemma lead_coeff_power: 
eberlm@62065
  2211
  "lead_coeff (p ^ n :: 'a :: idom poly) = lead_coeff p ^ n"
eberlm@62065
  2212
  by (induction n) (simp_all add: lead_coeff_mult)
eberlm@62065
  2213
eberlm@62065
  2214
lemma lead_coeff_nonzero: "p \<noteq> 0 \<Longrightarrow> lead_coeff p \<noteq> 0"
eberlm@62065
  2215
  by (simp add: lead_coeff_def)
haftmann@62352
  2216
haftmann@62352
  2217
haftmann@62352
  2218
subsection \<open>Derivatives of univariate polynomials\<close>
haftmann@62352
  2219
haftmann@62352
  2220
function pderiv :: "('a :: semidom) poly \<Rightarrow> 'a poly"
haftmann@62352
  2221
where
haftmann@62352
  2222
  [simp del]: "pderiv (pCons a p) = (if p = 0 then 0 else p + pCons 0 (pderiv p))"
haftmann@62352
  2223
  by (auto intro: pCons_cases)
haftmann@62352
  2224