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