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