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