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