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