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