src/HOL/Library/Univ_Poly.thy
author wenzelm
Sun, 25 Aug 2013 23:20:25 +0200
changeset 54333 942a1b48bb31
parent 54328 14ab2f821e1d
child 54773 9b5735de1f1a
permissions -rw-r--r--
tuned proofs;
     1 (*  Title:      HOL/Library/Univ_Poly.thy
     2     Author:     Amine Chaieb
     3 *)
     4 
     5 header {* Univariate Polynomials *}
     6 
     7 theory Univ_Poly
     8 imports Main
     9 begin
    10 
    11 text{* Application of polynomial as a function. *}
    12 
    13 primrec (in semiring_0) poly :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a"
    14 where
    15   poly_Nil:  "poly [] x = 0"
    16 | poly_Cons: "poly (h#t) x = h + x * poly t x"
    17 
    18 
    19 subsection{*Arithmetic Operations on Polynomials*}
    20 
    21 text{*addition*}
    22 
    23 primrec (in semiring_0) padd :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "+++" 65)
    24 where
    25   padd_Nil:  "[] +++ l2 = l2"
    26 | padd_Cons: "(h#t) +++ l2 = (if l2 = [] then h#t else (h + hd l2)#(t +++ tl l2))"
    27 
    28 text{*Multiplication by a constant*}
    29 primrec (in semiring_0) cmult :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "%*" 70) where
    30   cmult_Nil:  "c %* [] = []"
    31 | cmult_Cons: "c %* (h#t) = (c * h)#(c %* t)"
    32 
    33 text{*Multiplication by a polynomial*}
    34 primrec (in semiring_0) pmult :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "***" 70)
    35 where
    36   pmult_Nil:  "[] *** l2 = []"
    37 | pmult_Cons: "(h#t) *** l2 = (if t = [] then h %* l2
    38                               else (h %* l2) +++ ((0) # (t *** l2)))"
    39 
    40 text{*Repeated multiplication by a polynomial*}
    41 primrec (in semiring_0) mulexp :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a  list \<Rightarrow> 'a list" where
    42   mulexp_zero:  "mulexp 0 p q = q"
    43 | mulexp_Suc:   "mulexp (Suc n) p q = p *** mulexp n p q"
    44 
    45 text{*Exponential*}
    46 primrec (in semiring_1) pexp :: "'a list \<Rightarrow> nat \<Rightarrow> 'a list"  (infixl "%^" 80) where
    47   pexp_0:   "p %^ 0 = [1]"
    48 | pexp_Suc: "p %^ (Suc n) = p *** (p %^ n)"
    49 
    50 text{*Quotient related value of dividing a polynomial by x + a*}
    51 (* Useful for divisor properties in inductive proofs *)
    52 primrec (in field) "pquot" :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list"
    53 where
    54   pquot_Nil:  "pquot [] a= []"
    55 | pquot_Cons: "pquot (h#t) a =
    56     (if t = [] then [h] else (inverse(a) * (h - hd( pquot t a)))#(pquot t a))"
    57 
    58 text{*normalization of polynomials (remove extra 0 coeff)*}
    59 primrec (in semiring_0) pnormalize :: "'a list \<Rightarrow> 'a list" where
    60   pnormalize_Nil:  "pnormalize [] = []"
    61 | pnormalize_Cons: "pnormalize (h#p) =
    62     (if pnormalize p = [] then (if h = 0 then [] else [h]) else h # pnormalize p)"
    63 
    64 definition (in semiring_0) "pnormal p = ((pnormalize p = p) \<and> p \<noteq> [])"
    65 definition (in semiring_0) "nonconstant p = (pnormal p \<and> (\<forall>x. p \<noteq> [x]))"
    66 text{*Other definitions*}
    67 
    68 definition (in ring_1) poly_minus :: "'a list \<Rightarrow> 'a list" ("-- _" [80] 80)
    69   where "-- p = (- 1) %* p"
    70 
    71 definition (in semiring_0) divides :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  (infixl "divides" 70)
    72   where "p1 divides p2 = (\<exists>q. poly p2 = poly(p1 *** q))"
    73 
    74     --{*order of a polynomial*}
    75 definition (in ring_1) order :: "'a \<Rightarrow> 'a list \<Rightarrow> nat" where
    76   "order a p = (SOME n. ([-a, 1] %^ n) divides p \<and> ~ (([-a, 1] %^ (Suc n)) divides p))"
    77 
    78      --{*degree of a polynomial*}
    79 definition (in semiring_0) degree :: "'a list \<Rightarrow> nat"
    80   where "degree p = length (pnormalize p) - 1"
    81 
    82      --{*squarefree polynomials --- NB with respect to real roots only.*}
    83 definition (in ring_1) rsquarefree :: "'a list \<Rightarrow> bool"
    84   where "rsquarefree p \<longleftrightarrow> poly p \<noteq> poly [] \<and> (\<forall>a. order a p = 0 \<or> order a p = 1)"
    85 
    86 context semiring_0
    87 begin
    88 
    89 lemma padd_Nil2[simp]: "p +++ [] = p"
    90   by (induct p) auto
    91 
    92 lemma padd_Cons_Cons: "(h1 # p1) +++ (h2 # p2) = (h1 + h2) # (p1 +++ p2)"
    93   by auto
    94 
    95 lemma pminus_Nil: "-- [] = []"
    96   by (simp add: poly_minus_def)
    97 
    98 lemma pmult_singleton: "[h1] *** p1 = h1 %* p1" by simp
    99 
   100 end
   101 
   102 lemma (in semiring_1) poly_ident_mult[simp]: "1 %* t = t" by (induct t) auto
   103 
   104 lemma (in semiring_0) poly_simple_add_Cons[simp]: "[a] +++ ((0)#t) = (a#t)"
   105   by simp
   106 
   107 text{*Handy general properties*}
   108 
   109 lemma (in comm_semiring_0) padd_commut: "b +++ a = a +++ b"
   110 proof (induct b arbitrary: a)
   111   case Nil
   112   thus ?case by auto
   113 next
   114   case (Cons b bs a)
   115   thus ?case by (cases a) (simp_all add: add_commute)
   116 qed
   117 
   118 lemma (in comm_semiring_0) padd_assoc: "\<forall>b c. (a +++ b) +++ c = a +++ (b +++ c)"
   119   apply (induct a)
   120   apply (simp, clarify)
   121   apply (case_tac b, simp_all add: add_ac)
   122   done
   123 
   124 lemma (in semiring_0) poly_cmult_distr: "a %* ( p +++ q) = (a %* p +++ a %* q)"
   125   apply (induct p arbitrary: q)
   126   apply simp
   127   apply (case_tac q, simp_all add: distrib_left)
   128   done
   129 
   130 lemma (in ring_1) pmult_by_x[simp]: "[0, 1] *** t = ((0)#t)"
   131   apply (induct t)
   132   apply simp
   133   apply (auto simp add: padd_commut)
   134   apply (case_tac t, auto)
   135   done
   136 
   137 text{*properties of evaluation of polynomials.*}
   138 
   139 lemma (in semiring_0) poly_add: "poly (p1 +++ p2) x = poly p1 x + poly p2 x"
   140 proof(induct p1 arbitrary: p2)
   141   case Nil
   142   thus ?case by simp
   143 next
   144   case (Cons a as p2)
   145   thus ?case
   146     by (cases p2) (simp_all  add: add_ac distrib_left)
   147 qed
   148 
   149 lemma (in comm_semiring_0) poly_cmult: "poly (c %* p) x = c * poly p x"
   150   apply (induct p)
   151   apply (case_tac [2] "x = zero")
   152   apply (auto simp add: distrib_left mult_ac)
   153   done
   154 
   155 lemma (in comm_semiring_0) poly_cmult_map: "poly (map (op * c) p) x = c*poly p x"
   156   by (induct p) (auto simp add: distrib_left mult_ac)
   157 
   158 lemma (in comm_ring_1) poly_minus: "poly (-- p) x = - (poly p x)"
   159   apply (simp add: poly_minus_def)
   160   apply (auto simp add: poly_cmult)
   161   done
   162 
   163 lemma (in comm_semiring_0) poly_mult: "poly (p1 *** p2) x = poly p1 x * poly p2 x"
   164 proof (induct p1 arbitrary: p2)
   165   case Nil
   166   thus ?case by simp
   167 next
   168   case (Cons a as p2)
   169   thus ?case by (cases as)
   170     (simp_all add: poly_cmult poly_add distrib_right distrib_left mult_ac)
   171 qed
   172 
   173 class idom_char_0 = idom + ring_char_0
   174 
   175 lemma (in comm_ring_1) poly_exp: "poly (p %^ n) x = (poly p x) ^ n"
   176   by (induct n) (auto simp add: poly_cmult poly_mult)
   177 
   178 text{*More Polynomial Evaluation Lemmas*}
   179 
   180 lemma (in semiring_0) poly_add_rzero[simp]: "poly (a +++ []) x = poly a x"
   181   by simp
   182 
   183 lemma (in comm_semiring_0) poly_mult_assoc: "poly ((a *** b) *** c) x = poly (a *** (b *** c)) x"
   184   by (simp add: poly_mult mult_assoc)
   185 
   186 lemma (in semiring_0) poly_mult_Nil2[simp]: "poly (p *** []) x = 0"
   187   by (induct p) auto
   188 
   189 lemma (in comm_semiring_1) poly_exp_add: "poly (p %^ (n + d)) x = poly( p %^ n *** p %^ d) x"
   190   by (induct n) (auto simp add: poly_mult mult_assoc)
   191 
   192 subsection{*Key Property: if @{term "f(a) = 0"} then @{term "(x - a)"} divides
   193  @{term "p(x)"} *}
   194 
   195 lemma (in comm_ring_1) lemma_poly_linear_rem: "\<forall>h. \<exists>q r. h#t = [r] +++ [-a, 1] *** q"
   196 proof(induct t)
   197   case Nil
   198   { fix h have "[h] = [h] +++ [- a, 1] *** []" by simp }
   199   thus ?case by blast
   200 next
   201   case (Cons  x xs)
   202   { fix h
   203     from Cons.hyps[rule_format, of x]
   204     obtain q r where qr: "x#xs = [r] +++ [- a, 1] *** q" by blast
   205     have "h#x#xs = [a*r + h] +++ [-a, 1] *** (r#q)"
   206       using qr by (cases q) (simp_all add: algebra_simps)
   207     hence "\<exists>q r. h#x#xs = [r] +++ [-a, 1] *** q" by blast}
   208   thus ?case by blast
   209 qed
   210 
   211 lemma (in comm_ring_1) poly_linear_rem: "\<exists>q r. h#t = [r] +++ [-a, 1] *** q"
   212   using lemma_poly_linear_rem [where t = t and a = a] by auto
   213 
   214 
   215 lemma (in comm_ring_1) poly_linear_divides: "(poly p a = 0) = ((p = []) | (\<exists>q. p = [-a, 1] *** q))"
   216 proof -
   217   { assume p: "p = []" hence ?thesis by simp }
   218   moreover
   219   {
   220     fix x xs assume p: "p = x#xs"
   221     {
   222       fix q assume "p = [-a, 1] *** q"
   223       hence "poly p a = 0" by (simp add: poly_add poly_cmult)
   224     }
   225     moreover
   226     { assume p0: "poly p a = 0"
   227       from poly_linear_rem[of x xs a] obtain q r
   228       where qr: "x#xs = [r] +++ [- a, 1] *** q" by blast
   229       have "r = 0" using p0 by (simp only: p qr poly_mult poly_add) simp
   230       hence "\<exists>q. p = [- a, 1] *** q"
   231         using p qr
   232         apply -
   233         apply (rule exI[where x=q])
   234         apply auto
   235         apply (cases q)
   236         apply auto
   237         done
   238     }
   239     ultimately have ?thesis using p by blast
   240   }
   241   ultimately show ?thesis by (cases p) auto
   242 qed
   243 
   244 lemma (in semiring_0) lemma_poly_length_mult[simp]: "\<forall>h k a. length (k %* p +++  (h # (a %* p))) = Suc (length p)"
   245   by (induct p) auto
   246 
   247 lemma (in semiring_0) lemma_poly_length_mult2[simp]: "\<forall>h k. length (k %* p +++  (h # p)) = Suc (length p)"
   248   by (induct p) auto
   249 
   250 lemma (in ring_1) poly_length_mult[simp]: "length([-a,1] *** q) = Suc (length q)"
   251   by auto
   252 
   253 subsection{*Polynomial length*}
   254 
   255 lemma (in semiring_0) poly_cmult_length[simp]: "length (a %* p) = length p"
   256   by (induct p) auto
   257 
   258 lemma (in semiring_0) poly_add_length: "length (p1 +++ p2) = max (length p1) (length p2)"
   259   by (induct p1 arbitrary: p2) (simp_all, arith)
   260 
   261 lemma (in semiring_0) poly_root_mult_length[simp]: "length([a,b] *** p) = Suc (length p)"
   262   by (simp add: poly_add_length)
   263 
   264 lemma (in idom) poly_mult_not_eq_poly_Nil[simp]:
   265   "poly (p *** q) x \<noteq> poly [] x \<longleftrightarrow> poly p x \<noteq> poly [] x \<and> poly q x \<noteq> poly [] x"
   266   by (auto simp add: poly_mult)
   267 
   268 lemma (in idom) poly_mult_eq_zero_disj: "poly (p *** q) x = 0 \<longleftrightarrow> poly p x = 0 \<or> poly q x = 0"
   269   by (auto simp add: poly_mult)
   270 
   271 text{*Normalisation Properties*}
   272 
   273 lemma (in semiring_0) poly_normalized_nil: "(pnormalize p = []) --> (poly p x = 0)"
   274   by (induct p) auto
   275 
   276 text{*A nontrivial polynomial of degree n has no more than n roots*}
   277 lemma (in idom) poly_roots_index_lemma:
   278    assumes p: "poly p x \<noteq> poly [] x" and n: "length p = n"
   279   shows "\<exists>i. \<forall>x. poly p x = 0 \<longrightarrow> (\<exists>m\<le>n. x = i m)"
   280   using p n
   281 proof (induct n arbitrary: p x)
   282   case 0
   283   thus ?case by simp
   284 next
   285   case (Suc n p x)
   286   {
   287     assume C: "\<And>i. \<exists>x. poly p x = 0 \<and> (\<forall>m\<le>Suc n. x \<noteq> i m)"
   288     from Suc.prems have p0: "poly p x \<noteq> 0" "p\<noteq> []" by auto
   289     from p0(1)[unfolded poly_linear_divides[of p x]]
   290     have "\<forall>q. p \<noteq> [- x, 1] *** q" by blast
   291     from C obtain a where a: "poly p a = 0" by blast
   292     from a[unfolded poly_linear_divides[of p a]] p0(2)
   293     obtain q where q: "p = [-a, 1] *** q" by blast
   294     have lg: "length q = n" using q Suc.prems(2) by simp
   295     from q p0 have qx: "poly q x \<noteq> poly [] x"
   296       by (auto simp add: poly_mult poly_add poly_cmult)
   297     from Suc.hyps[OF qx lg] obtain i where
   298       i: "\<forall>x. poly q x = 0 \<longrightarrow> (\<exists>m\<le>n. x = i m)" by blast
   299     let ?i = "\<lambda>m. if m = Suc n then a else i m"
   300     from C[of ?i] obtain y where y: "poly p y = 0" "\<forall>m\<le> Suc n. y \<noteq> ?i m"
   301       by blast
   302     from y have "y = a \<or> poly q y = 0"
   303       by (simp only: q poly_mult_eq_zero_disj poly_add) (simp add: algebra_simps)
   304     with i[rule_format, of y] y(1) y(2) have False
   305       apply auto
   306       apply (erule_tac x = "m" in allE)
   307       apply auto
   308       done
   309   }
   310   thus ?case by blast
   311 qed
   312 
   313 
   314 lemma (in idom) poly_roots_index_length:
   315   "poly p x \<noteq> poly [] x \<Longrightarrow> \<exists>i. \<forall>x. (poly p x = 0) \<longrightarrow> (\<exists>n. n \<le> length p \<and> x = i n)"
   316   by (blast intro: poly_roots_index_lemma)
   317 
   318 lemma (in idom) poly_roots_finite_lemma1:
   319   "poly p x \<noteq> poly [] x \<Longrightarrow> \<exists>N i. \<forall>x. (poly p x = 0) \<longrightarrow> (\<exists>n. (n::nat) < N \<and> x = i n)"
   320   apply (drule poly_roots_index_length, safe)
   321   apply (rule_tac x = "Suc (length p)" in exI)
   322   apply (rule_tac x = i in exI)
   323   apply (simp add: less_Suc_eq_le)
   324   done
   325 
   326 lemma (in idom) idom_finite_lemma:
   327   assumes P: "\<forall>x. P x --> (\<exists>n. n < length j \<and> x = j!n)"
   328   shows "finite {x. P x}"
   329 proof -
   330   let ?M = "{x. P x}"
   331   let ?N = "set j"
   332   have "?M \<subseteq> ?N" using P by auto
   333   thus ?thesis using finite_subset by auto
   334 qed
   335 
   336 lemma (in idom) poly_roots_finite_lemma2:
   337   "poly p x \<noteq> poly [] x \<Longrightarrow> \<exists>i. \<forall>x. poly p x = 0 \<longrightarrow> x \<in> set i"
   338   apply (drule poly_roots_index_length, safe)
   339   apply (rule_tac x="map (\<lambda>n. i n) [0 ..< Suc (length p)]" in exI)
   340   apply (auto simp add: image_iff)
   341   apply (erule_tac x="x" in allE, clarsimp)
   342   apply (case_tac "n = length p")
   343   apply (auto simp add: order_le_less)
   344   done
   345 
   346 lemma (in ring_char_0) UNIV_ring_char_0_infinte: "\<not> (finite (UNIV:: 'a set))"
   347 proof
   348   assume F: "finite (UNIV :: 'a set)"
   349   have "finite (UNIV :: nat set)"
   350   proof (rule finite_imageD)
   351     have "of_nat ` UNIV \<subseteq> UNIV" by simp
   352     then show "finite (of_nat ` UNIV :: 'a set)" using F by (rule finite_subset)
   353     show "inj (of_nat :: nat \<Rightarrow> 'a)" by (simp add: inj_on_def)
   354   qed
   355   with infinite_UNIV_nat show False ..
   356 qed
   357 
   358 lemma (in idom_char_0) poly_roots_finite: "poly p \<noteq> poly [] \<longleftrightarrow> finite {x. poly p x = 0}"
   359 proof
   360   assume H: "poly p \<noteq> poly []"
   361   show "finite {x. poly p x = (0::'a)}"
   362     using H
   363     apply -
   364     apply (erule contrapos_np, rule ext)
   365     apply (rule ccontr)
   366     apply (clarify dest!: poly_roots_finite_lemma2)
   367     using finite_subset
   368   proof -
   369     fix x i
   370     assume F: "\<not> finite {x. poly p x = (0\<Colon>'a)}"
   371       and P: "\<forall>x. poly p x = (0\<Colon>'a) \<longrightarrow> x \<in> set i"
   372     let ?M= "{x. poly p x = (0\<Colon>'a)}"
   373     from P have "?M \<subseteq> set i" by auto
   374     with finite_subset F show False by auto
   375   qed
   376 next
   377   assume F: "finite {x. poly p x = (0\<Colon>'a)}"
   378   show "poly p \<noteq> poly []" using F UNIV_ring_char_0_infinte by auto
   379 qed
   380 
   381 text{*Entirety and Cancellation for polynomials*}
   382 
   383 lemma (in idom_char_0) poly_entire_lemma2:
   384   assumes p0: "poly p \<noteq> poly []"
   385     and q0: "poly q \<noteq> poly []"
   386   shows "poly (p***q) \<noteq> poly []"
   387 proof -
   388   let ?S = "\<lambda>p. {x. poly p x = 0}"
   389   have "?S (p *** q) = ?S p \<union> ?S q" by (auto simp add: poly_mult)
   390   with p0 q0 show ?thesis  unfolding poly_roots_finite by auto
   391 qed
   392 
   393 lemma (in idom_char_0) poly_entire:
   394   "poly (p *** q) = poly [] \<longleftrightarrow> poly p = poly [] \<or> poly q = poly []"
   395   using poly_entire_lemma2[of p q]
   396   by (auto simp add: fun_eq_iff poly_mult)
   397 
   398 lemma (in idom_char_0) poly_entire_neg:
   399   "poly (p *** q) \<noteq> poly [] \<longleftrightarrow> poly p \<noteq> poly [] \<and> poly q \<noteq> poly []"
   400   by (simp add: poly_entire)
   401 
   402 lemma fun_eq: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
   403   by auto
   404 
   405 lemma (in comm_ring_1) poly_add_minus_zero_iff:
   406   "poly (p +++ -- q) = poly [] \<longleftrightarrow> poly p = poly q"
   407   by (auto simp add: algebra_simps poly_add poly_minus_def fun_eq poly_cmult)
   408 
   409 lemma (in comm_ring_1) poly_add_minus_mult_eq:
   410   "poly (p *** q +++ --(p *** r)) = poly (p *** (q +++ -- r))"
   411   by (auto simp add: poly_add poly_minus_def fun_eq poly_mult poly_cmult distrib_left)
   412 
   413 subclass (in idom_char_0) comm_ring_1 ..
   414 
   415 lemma (in idom_char_0) poly_mult_left_cancel:
   416   "poly (p *** q) = poly (p *** r) \<longleftrightarrow> poly p = poly [] \<or> poly q = poly r"
   417 proof -
   418   have "poly (p *** q) = poly (p *** r) \<longleftrightarrow> poly (p *** q +++ -- (p *** r)) = poly []"
   419     by (simp only: poly_add_minus_zero_iff)
   420   also have "\<dots> \<longleftrightarrow> poly p = poly [] \<or> poly q = poly r"
   421     by (auto intro: simp add: poly_add_minus_mult_eq poly_entire poly_add_minus_zero_iff)
   422   finally show ?thesis .
   423 qed
   424 
   425 lemma (in idom) poly_exp_eq_zero[simp]:
   426   "poly (p %^ n) = poly [] \<longleftrightarrow> poly p = poly [] \<and> n \<noteq> 0"
   427   apply (simp only: fun_eq add: HOL.all_simps [symmetric])
   428   apply (rule arg_cong [where f = All])
   429   apply (rule ext)
   430   apply (induct n)
   431   apply (auto simp add: poly_exp poly_mult)
   432   done
   433 
   434 lemma (in comm_ring_1) poly_prime_eq_zero[simp]: "poly [a,1] \<noteq> poly []"
   435   apply (simp add: fun_eq)
   436   apply (rule_tac x = "minus one a" in exI)
   437   apply (unfold diff_minus)
   438   apply (subst add_commute)
   439   apply (subst add_assoc)
   440   apply simp
   441   done
   442 
   443 lemma (in idom) poly_exp_prime_eq_zero: "poly ([a, 1] %^ n) \<noteq> poly []"
   444   by auto
   445 
   446 text{*A more constructive notion of polynomials being trivial*}
   447 
   448 lemma (in idom_char_0) poly_zero_lemma': "poly (h # t) = poly [] \<Longrightarrow> h = 0 \<and> poly t = poly []"
   449   apply (simp add: fun_eq)
   450   apply (case_tac "h = zero")
   451   apply (drule_tac [2] x = zero in spec, auto)
   452   apply (cases "poly t = poly []", simp)
   453 proof -
   454   fix x
   455   assume H: "\<forall>x. x = (0\<Colon>'a) \<or> poly t x = (0\<Colon>'a)"
   456     and pnz: "poly t \<noteq> poly []"
   457   let ?S = "{x. poly t x = 0}"
   458   from H have "\<forall>x. x \<noteq>0 \<longrightarrow> poly t x = 0" by blast
   459   hence th: "?S \<supseteq> UNIV - {0}" by auto
   460   from poly_roots_finite pnz have th': "finite ?S" by blast
   461   from finite_subset[OF th th'] UNIV_ring_char_0_infinte show "poly t x = (0\<Colon>'a)"
   462     by simp
   463 qed
   464 
   465 lemma (in idom_char_0) poly_zero: "(poly p = poly []) = list_all (%c. c = 0) p"
   466   apply (induct p)
   467   apply simp
   468   apply (rule iffI)
   469   apply (drule poly_zero_lemma', auto)
   470   done
   471 
   472 lemma (in idom_char_0) poly_0: "list_all (\<lambda>c. c = 0) p \<Longrightarrow> poly p x = 0"
   473   unfolding poly_zero[symmetric] by simp
   474 
   475 
   476 
   477 text{*Basics of divisibility.*}
   478 
   479 lemma (in idom) poly_primes:
   480   "[a, 1] divides (p *** q) \<longleftrightarrow> [a, 1] divides p \<or> [a, 1] divides q"
   481   apply (auto simp add: divides_def fun_eq poly_mult poly_add poly_cmult distrib_right [symmetric])
   482   apply (drule_tac x = "uminus a" in spec)
   483   apply (simp add: poly_linear_divides poly_add poly_cmult distrib_right [symmetric])
   484   apply (cases "p = []")
   485   apply (rule exI[where x="[]"])
   486   apply simp
   487   apply (cases "q = []")
   488   apply (erule allE[where x="[]"], simp)
   489 
   490   apply clarsimp
   491   apply (cases "\<exists>q\<Colon>'a list. p = a %* q +++ ((0\<Colon>'a) # q)")
   492   apply (clarsimp simp add: poly_add poly_cmult)
   493   apply (rule_tac x="qa" in exI)
   494   apply (simp add: distrib_right [symmetric])
   495   apply clarsimp
   496 
   497   apply (auto simp add: poly_linear_divides poly_add poly_cmult distrib_right [symmetric])
   498   apply (rule_tac x = "pmult qa q" in exI)
   499   apply (rule_tac [2] x = "pmult p qa" in exI)
   500   apply (auto simp add: poly_add poly_mult poly_cmult mult_ac)
   501   done
   502 
   503 lemma (in comm_semiring_1) poly_divides_refl[simp]: "p divides p"
   504   apply (simp add: divides_def)
   505   apply (rule_tac x = "[one]" in exI)
   506   apply (auto simp add: poly_mult fun_eq)
   507   done
   508 
   509 lemma (in comm_semiring_1) poly_divides_trans: "p divides q \<Longrightarrow> q divides r \<Longrightarrow> p divides r"
   510   apply (simp add: divides_def, safe)
   511   apply (rule_tac x = "pmult qa qaa" in exI)
   512   apply (auto simp add: poly_mult fun_eq mult_assoc)
   513   done
   514 
   515 lemma (in comm_semiring_1) poly_divides_exp: "m \<le> n \<Longrightarrow> (p %^ m) divides (p %^ n)"
   516   apply (auto simp add: le_iff_add)
   517   apply (induct_tac k)
   518   apply (rule_tac [2] poly_divides_trans)
   519   apply (auto simp add: divides_def)
   520   apply (rule_tac x = p in exI)
   521   apply (auto simp add: poly_mult fun_eq mult_ac)
   522   done
   523 
   524 lemma (in comm_semiring_1) poly_exp_divides:
   525   "(p %^ n) divides q \<Longrightarrow> m \<le> n \<Longrightarrow> (p %^ m) divides q"
   526   by (blast intro: poly_divides_exp poly_divides_trans)
   527 
   528 lemma (in comm_semiring_0) poly_divides_add:
   529   "p divides q \<Longrightarrow> p divides r \<Longrightarrow> p divides (q +++ r)"
   530   apply (simp add: divides_def, auto)
   531   apply (rule_tac x = "padd qa qaa" in exI)
   532   apply (auto simp add: poly_add fun_eq poly_mult distrib_left)
   533   done
   534 
   535 lemma (in comm_ring_1) poly_divides_diff:
   536   "p divides q \<Longrightarrow> p divides (q +++ r) \<Longrightarrow> p divides r"
   537   apply (simp add: divides_def, auto)
   538   apply (rule_tac x = "padd qaa (poly_minus qa)" in exI)
   539   apply (auto simp add: poly_add fun_eq poly_mult poly_minus algebra_simps)
   540   done
   541 
   542 lemma (in comm_ring_1) poly_divides_diff2:
   543   "p divides r \<Longrightarrow> p divides (q +++ r) \<Longrightarrow> p divides q"
   544   apply (erule poly_divides_diff)
   545   apply (auto simp add: poly_add fun_eq poly_mult divides_def add_ac)
   546   done
   547 
   548 lemma (in semiring_0) poly_divides_zero: "poly p = poly [] \<Longrightarrow> q divides p"
   549   apply (simp add: divides_def)
   550   apply (rule exI[where x="[]"])
   551   apply (auto simp add: fun_eq poly_mult)
   552   done
   553 
   554 lemma (in semiring_0) poly_divides_zero2 [simp]: "q divides []"
   555   apply (simp add: divides_def)
   556   apply (rule_tac x = "[]" in exI)
   557   apply (auto simp add: fun_eq)
   558   done
   559 
   560 text{*At last, we can consider the order of a root.*}
   561 
   562 lemma (in idom_char_0) poly_order_exists_lemma:
   563   assumes lp: "length p = d"
   564     and p: "poly p \<noteq> poly []"
   565   shows "\<exists>n q. p = mulexp n [-a, 1] q \<and> poly q a \<noteq> 0"
   566   using lp p
   567 proof (induct d arbitrary: p)
   568   case 0
   569   thus ?case by simp
   570 next
   571   case (Suc n p)
   572   show ?case
   573   proof (cases "poly p a = 0")
   574     case True
   575     from Suc.prems have h: "length p = Suc n" "poly p \<noteq> poly []" by auto
   576     hence pN: "p \<noteq> []" by auto
   577     from True[unfolded poly_linear_divides] pN obtain q where q: "p = [-a, 1] *** q"
   578       by blast
   579     from q h True have qh: "length q = n" "poly q \<noteq> poly []"
   580       apply -
   581       apply simp
   582       apply (simp only: fun_eq)
   583       apply (rule ccontr)
   584       apply (simp add: fun_eq poly_add poly_cmult)
   585       done
   586     from Suc.hyps[OF qh] obtain m r where mr: "q = mulexp m [-a,1] r" "poly r a \<noteq> 0"
   587       by blast
   588     from mr q have "p = mulexp (Suc m) [-a,1] r \<and> poly r a \<noteq> 0" by simp
   589     then show ?thesis by blast
   590   next
   591     case False
   592     then show ?thesis
   593       using Suc.prems
   594       apply simp
   595       apply (rule exI[where x="0::nat"])
   596       apply simp
   597       done
   598   qed
   599 qed
   600 
   601 
   602 lemma (in comm_semiring_1) poly_mulexp: "poly (mulexp n p q) x = (poly p x) ^ n * poly q x"
   603   by (induct n) (auto simp add: poly_mult mult_ac)
   604 
   605 lemma (in comm_semiring_1) divides_left_mult:
   606   assumes d:"(p***q) divides r" shows "p divides r \<and> q divides r"
   607 proof-
   608   from d obtain t where r:"poly r = poly (p***q *** t)"
   609     unfolding divides_def by blast
   610   hence "poly r = poly (p *** (q *** t))"
   611     "poly r = poly (q *** (p***t))" by(auto simp add: fun_eq poly_mult mult_ac)
   612   thus ?thesis unfolding divides_def by blast
   613 qed
   614 
   615 
   616 (* FIXME: Tidy up *)
   617 
   618 lemma (in semiring_1) zero_power_iff: "0 ^ n = (if n = 0 then 1 else 0)"
   619   by (induct n) simp_all
   620 
   621 lemma (in idom_char_0) poly_order_exists:
   622   assumes "length p = d" and "poly p \<noteq> poly []"
   623   shows "\<exists>n. ([-a, 1] %^ n) divides p \<and> ~(([-a, 1] %^ (Suc n)) divides p)"
   624   using assms
   625   apply -
   626   apply (drule poly_order_exists_lemma [where a=a], assumption, clarify)
   627   apply (rule_tac x = n in exI, safe)
   628   apply (unfold divides_def)
   629   apply (rule_tac x = q in exI)
   630   apply (induct_tac n, simp)
   631   apply (simp (no_asm_simp) add: poly_add poly_cmult poly_mult distrib_left mult_ac)
   632   apply safe
   633   apply (subgoal_tac "poly (mulexp n [uminus a, one] q) \<noteq>
   634     poly (pmult (pexp [uminus a, one] (Suc n)) qa)")
   635   apply simp
   636   apply (induct_tac n)
   637   apply (simp del: pmult_Cons pexp_Suc)
   638   apply (erule_tac Q = "poly q a = zero" in contrapos_np)
   639   apply (simp add: poly_add poly_cmult)
   640   apply (rule pexp_Suc [THEN ssubst])
   641   apply (rule ccontr)
   642   apply (simp add: poly_mult_left_cancel poly_mult_assoc del: pmult_Cons pexp_Suc)
   643   done
   644 
   645 lemma (in semiring_1) poly_one_divides[simp]: "[1] divides p"
   646   by (auto simp add: divides_def)
   647 
   648 lemma (in idom_char_0) poly_order:
   649   "poly p \<noteq> poly [] \<Longrightarrow> \<exists>!n. ([-a, 1] %^ n) divides p \<and> \<not> (([-a, 1] %^ Suc n) divides p)"
   650   apply (auto intro: poly_order_exists simp add: less_linear simp del: pmult_Cons pexp_Suc)
   651   apply (cut_tac x = y and y = n in less_linear)
   652   apply (drule_tac m = n in poly_exp_divides)
   653   apply (auto dest: Suc_le_eq [THEN iffD2, THEN [2] poly_exp_divides]
   654               simp del: pmult_Cons pexp_Suc)
   655   done
   656 
   657 text{*Order*}
   658 
   659 lemma some1_equalityD: "n = (SOME n. P n) \<Longrightarrow> \<exists>!n. P n \<Longrightarrow> P n"
   660   by (blast intro: someI2)
   661 
   662 lemma (in idom_char_0) order:
   663       "(([-a, 1] %^ n) divides p \<and>
   664         ~(([-a, 1] %^ (Suc n)) divides p)) =
   665         ((n = order a p) \<and> ~(poly p = poly []))"
   666   apply (unfold order_def)
   667   apply (rule iffI)
   668   apply (blast dest: poly_divides_zero intro!: some1_equality [symmetric] poly_order)
   669   apply (blast intro!: poly_order [THEN [2] some1_equalityD])
   670   done
   671 
   672 lemma (in idom_char_0) order2:
   673   "poly p \<noteq> poly [] \<Longrightarrow>
   674     ([-a, 1] %^ (order a p)) divides p \<and> \<not> (([-a, 1] %^ (Suc (order a p))) divides p)"
   675   by (simp add: order del: pexp_Suc)
   676 
   677 lemma (in idom_char_0) order_unique:
   678   "poly p \<noteq> poly [] \<Longrightarrow> ([-a, 1] %^ n) divides p \<Longrightarrow> ~(([-a, 1] %^ (Suc n)) divides p) \<Longrightarrow>
   679     n = order a p"
   680   using order [of a n p] by auto
   681 
   682 lemma (in idom_char_0) order_unique_lemma:
   683   "poly p \<noteq> poly [] \<and> ([-a, 1] %^ n) divides p \<and> ~(([-a, 1] %^ (Suc n)) divides p) \<Longrightarrow>
   684     n = order a p"
   685   by (blast intro: order_unique)
   686 
   687 lemma (in ring_1) order_poly: "poly p = poly q \<Longrightarrow> order a p = order a q"
   688   by (auto simp add: fun_eq divides_def poly_mult order_def)
   689 
   690 lemma (in semiring_1) pexp_one[simp]: "p %^ (Suc 0) = p"
   691   by (induct "p") auto
   692 
   693 lemma (in comm_ring_1) lemma_order_root:
   694   "0 < n \<and> [- a, 1] %^ n divides p \<and> ~ [- a, 1] %^ (Suc n) divides p \<Longrightarrow> poly p a = 0"
   695   by (induct n arbitrary: a p) (auto simp add: divides_def poly_mult simp del: pmult_Cons)
   696 
   697 lemma (in idom_char_0) order_root:
   698   "poly p a = 0 \<longleftrightarrow> poly p = poly [] \<or> order a p \<noteq> 0"
   699   apply (cases "poly p = poly []")
   700   apply auto
   701   apply (simp add: poly_linear_divides del: pmult_Cons, safe)
   702   apply (drule_tac [!] a = a in order2)
   703   apply (rule ccontr)
   704   apply (simp add: divides_def poly_mult fun_eq del: pmult_Cons, blast)
   705   using neq0_conv
   706   apply (blast intro: lemma_order_root)
   707   done
   708 
   709 lemma (in idom_char_0) order_divides:
   710   "([-a, 1] %^ n) divides p \<longleftrightarrow> poly p = poly [] \<or> n \<le> order a p"
   711   apply (cases "poly p = poly []")
   712   apply auto
   713   apply (simp add: divides_def fun_eq poly_mult)
   714   apply (rule_tac x = "[]" in exI)
   715   apply (auto dest!: order2 [where a=a] intro: poly_exp_divides simp del: pexp_Suc)
   716   done
   717 
   718 lemma (in idom_char_0) order_decomp:
   719   "poly p \<noteq> poly [] \<Longrightarrow> \<exists>q. poly p = poly (([-a, 1] %^ (order a p)) *** q) \<and> ~([-a, 1] divides q)"
   720   apply (unfold divides_def)
   721   apply (drule order2 [where a = a])
   722   apply (simp add: divides_def del: pexp_Suc pmult_Cons, safe)
   723   apply (rule_tac x = q in exI, safe)
   724   apply (drule_tac x = qa in spec)
   725   apply (auto simp add: poly_mult fun_eq poly_exp mult_ac simp del: pmult_Cons)
   726   done
   727 
   728 text{*Important composition properties of orders.*}
   729 lemma order_mult:
   730   "poly (p *** q) \<noteq> poly [] \<Longrightarrow>
   731     order a (p *** q) = order a p + order (a::'a::{idom_char_0}) q"
   732   apply (cut_tac a = a and p = "p *** q" and n = "order a p + order a q" in order)
   733   apply (auto simp add: poly_entire simp del: pmult_Cons)
   734   apply (drule_tac a = a in order2)+
   735   apply safe
   736   apply (simp add: divides_def fun_eq poly_exp_add poly_mult del: pmult_Cons, safe)
   737   apply (rule_tac x = "qa *** qaa" in exI)
   738   apply (simp add: poly_mult mult_ac del: pmult_Cons)
   739   apply (drule_tac a = a in order_decomp)+
   740   apply safe
   741   apply (subgoal_tac "[-a,1] divides (qa *** qaa) ")
   742   apply (simp add: poly_primes del: pmult_Cons)
   743   apply (auto simp add: divides_def simp del: pmult_Cons)
   744   apply (rule_tac x = qb in exI)
   745   apply (subgoal_tac "poly ([-a, 1] %^ (order a p) *** (qa *** qaa)) = poly ([-a, 1] %^ (order a p) *** ([-a, 1] *** qb))")
   746   apply (drule poly_mult_left_cancel [THEN iffD1], force)
   747   apply (subgoal_tac "poly ([-a, 1] %^ (order a q) *** ([-a, 1] %^ (order a p) *** (qa *** qaa))) = poly ([-a, 1] %^ (order a q) *** ([-a, 1] %^ (order a p) *** ([-a, 1] *** qb))) ")
   748   apply (drule poly_mult_left_cancel [THEN iffD1], force)
   749   apply (simp add: fun_eq poly_exp_add poly_mult mult_ac del: pmult_Cons)
   750   done
   751 
   752 lemma (in idom_char_0) order_mult:
   753   assumes "poly (p *** q) \<noteq> poly []"
   754   shows "order a (p *** q) = order a p + order a q"
   755   using assms
   756   apply (cut_tac a = a and p = "pmult p q" and n = "order a p + order a q" in order)
   757   apply (auto simp add: poly_entire simp del: pmult_Cons)
   758   apply (drule_tac a = a in order2)+
   759   apply safe
   760   apply (simp add: divides_def fun_eq poly_exp_add poly_mult del: pmult_Cons, safe)
   761   apply (rule_tac x = "pmult qa qaa" in exI)
   762   apply (simp add: poly_mult mult_ac del: pmult_Cons)
   763   apply (drule_tac a = a in order_decomp)+
   764   apply safe
   765   apply (subgoal_tac "[uminus a, one] divides pmult qa qaa")
   766   apply (simp add: poly_primes del: pmult_Cons)
   767   apply (auto simp add: divides_def simp del: pmult_Cons)
   768   apply (rule_tac x = qb in exI)
   769   apply (subgoal_tac "poly (pmult (pexp [uminus a, one] (order a p)) (pmult qa qaa)) =
   770     poly (pmult (pexp [uminus a, one] (?order a p)) (pmult [uminus a, one] qb))")
   771   apply (drule poly_mult_left_cancel [THEN iffD1], force)
   772   apply (subgoal_tac "poly (pmult (pexp [uminus a, one] (order a q))
   773       (pmult (pexp [uminus a, one] (order a p)) (pmult qa qaa))) =
   774     poly (pmult (pexp [uminus a, one] (order a q))
   775       (pmult (pexp [uminus a, one] (order a p)) (pmult [uminus a, one] qb)))")
   776   apply (drule poly_mult_left_cancel [THEN iffD1], force)
   777   apply (simp add: fun_eq poly_exp_add poly_mult mult_ac del: pmult_Cons)
   778   done
   779 
   780 lemma (in idom_char_0) order_root2: "poly p \<noteq> poly [] \<Longrightarrow> poly p a = 0 \<longleftrightarrow> order a p \<noteq> 0"
   781   by (rule order_root [THEN ssubst]) auto
   782 
   783 lemma (in semiring_1) pmult_one[simp]: "[1] *** p = p" by auto
   784 
   785 lemma (in semiring_0) poly_Nil_zero: "poly [] = poly [0]"
   786   by (simp add: fun_eq)
   787 
   788 lemma (in idom_char_0) rsquarefree_decomp:
   789   "rsquarefree p \<Longrightarrow> poly p a = 0 \<Longrightarrow>
   790     \<exists>q. poly p = poly ([-a, 1] *** q) \<and> poly q a \<noteq> 0"
   791   apply (simp add: rsquarefree_def, safe)
   792   apply (frule_tac a = a in order_decomp)
   793   apply (drule_tac x = a in spec)
   794   apply (drule_tac a = a in order_root2 [symmetric])
   795   apply (auto simp del: pmult_Cons)
   796   apply (rule_tac x = q in exI, safe)
   797   apply (simp add: poly_mult fun_eq)
   798   apply (drule_tac p1 = q in poly_linear_divides [THEN iffD1])
   799   apply (simp add: divides_def del: pmult_Cons, safe)
   800   apply (drule_tac x = "[]" in spec)
   801   apply (auto simp add: fun_eq)
   802   done
   803 
   804 
   805 text{*Normalization of a polynomial.*}
   806 
   807 lemma (in semiring_0) poly_normalize[simp]: "poly (pnormalize p) = poly p"
   808   by (induct p) (auto simp add: fun_eq)
   809 
   810 text{*The degree of a polynomial.*}
   811 
   812 lemma (in semiring_0) lemma_degree_zero: "list_all (%c. c = 0) p \<longleftrightarrow> pnormalize p = []"
   813   by (induct p) auto
   814 
   815 lemma (in idom_char_0) degree_zero:
   816   assumes "poly p = poly []"
   817   shows "degree p = 0"
   818   using assms
   819   by (cases "pnormalize p = []") (auto simp add: degree_def poly_zero lemma_degree_zero)
   820 
   821 lemma (in semiring_0) pnormalize_sing: "(pnormalize [x] = [x]) \<longleftrightarrow> x \<noteq> 0"
   822   by simp
   823 
   824 lemma (in semiring_0) pnormalize_pair: "y \<noteq> 0 \<longleftrightarrow> (pnormalize [x, y] = [x, y])"
   825   by simp
   826 
   827 lemma (in semiring_0) pnormal_cons: "pnormal p \<Longrightarrow> pnormal (c#p)"
   828   unfolding pnormal_def by simp
   829 
   830 lemma (in semiring_0) pnormal_tail: "p\<noteq>[] \<Longrightarrow> pnormal (c#p) \<Longrightarrow> pnormal p"
   831   unfolding pnormal_def by(auto split: split_if_asm)
   832 
   833 
   834 lemma (in semiring_0) pnormal_last_nonzero: "pnormal p \<Longrightarrow> last p \<noteq> 0"
   835   by (induct p) (simp_all add: pnormal_def split: split_if_asm)
   836 
   837 lemma (in semiring_0) pnormal_length: "pnormal p \<Longrightarrow> 0 < length p"
   838   unfolding pnormal_def length_greater_0_conv by blast
   839 
   840 lemma (in semiring_0) pnormal_last_length: "0 < length p \<Longrightarrow> last p \<noteq> 0 \<Longrightarrow> pnormal p"
   841   by (induct p) (auto simp: pnormal_def  split: split_if_asm)
   842 
   843 
   844 lemma (in semiring_0) pnormal_id: "pnormal p \<longleftrightarrow> 0 < length p \<and> last p \<noteq> 0"
   845   using pnormal_last_length pnormal_length pnormal_last_nonzero by blast
   846 
   847 lemma (in idom_char_0) poly_Cons_eq:
   848   "poly (c # cs) = poly (d # ds) \<longleftrightarrow> c = d \<and> poly cs = poly ds"
   849   (is "?lhs \<longleftrightarrow> ?rhs")
   850 proof
   851   assume eq: ?lhs
   852   hence "\<And>x. poly ((c#cs) +++ -- (d#ds)) x = 0"
   853     by (simp only: poly_minus poly_add algebra_simps) simp
   854   hence "poly ((c#cs) +++ -- (d#ds)) = poly []" by(simp add: fun_eq_iff)
   855   hence "c = d \<and> list_all (\<lambda>x. x=0) ((cs +++ -- ds))"
   856     unfolding poly_zero by (simp add: poly_minus_def algebra_simps)
   857   hence "c = d \<and> (\<forall>x. poly (cs +++ -- ds) x = 0)"
   858     unfolding poly_zero[symmetric] by simp
   859   then show ?rhs by (simp add: poly_minus poly_add algebra_simps fun_eq_iff)
   860 next
   861   assume ?rhs
   862   then show ?lhs by(simp add:fun_eq_iff)
   863 qed
   864 
   865 lemma (in idom_char_0) pnormalize_unique: "poly p = poly q \<Longrightarrow> pnormalize p = pnormalize q"
   866 proof (induct q arbitrary: p)
   867   case Nil
   868   thus ?case by (simp only: poly_zero lemma_degree_zero) simp
   869 next
   870   case (Cons c cs p)
   871   thus ?case
   872   proof (induct p)
   873     case Nil
   874     hence "poly [] = poly (c#cs)" by blast
   875     then have "poly (c#cs) = poly [] " by simp
   876     thus ?case by (simp only: poly_zero lemma_degree_zero) simp
   877   next
   878     case (Cons d ds)
   879     hence eq: "poly (d # ds) = poly (c # cs)" by blast
   880     hence eq': "\<And>x. poly (d # ds) x = poly (c # cs) x" by simp
   881     hence "poly (d # ds) 0 = poly (c # cs) 0" by blast
   882     hence dc: "d = c" by auto
   883     with eq have "poly ds = poly cs"
   884       unfolding  poly_Cons_eq by simp
   885     with Cons.prems have "pnormalize ds = pnormalize cs" by blast
   886     with dc show ?case by simp
   887   qed
   888 qed
   889 
   890 lemma (in idom_char_0) degree_unique:
   891   assumes pq: "poly p = poly q"
   892   shows "degree p = degree q"
   893   using pnormalize_unique[OF pq] unfolding degree_def by simp
   894 
   895 lemma (in semiring_0) pnormalize_length:
   896   "length (pnormalize p) \<le> length p" by (induct p) auto
   897 
   898 lemma (in semiring_0) last_linear_mul_lemma:
   899   "last ((a %* p) +++ (x#(b %* p))) = (if p = [] then x else b * last p)"
   900   apply (induct p arbitrary: a x b)
   901   apply auto
   902   apply (subgoal_tac "padd (cmult aa p) (times b a # cmult b p) \<noteq> []")
   903   apply simp
   904   apply (induct_tac p)
   905   apply auto
   906   done
   907 
   908 lemma (in semiring_1) last_linear_mul:
   909   assumes p: "p \<noteq> []"
   910   shows "last ([a,1] *** p) = last p"
   911 proof -
   912   from p obtain c cs where cs: "p = c#cs" by (cases p) auto
   913   from cs have eq: "[a,1] *** p = (a %* (c#cs)) +++ (0#(1 %* (c#cs)))"
   914     by (simp add: poly_cmult_distr)
   915   show ?thesis using cs
   916     unfolding eq last_linear_mul_lemma by simp
   917 qed
   918 
   919 lemma (in semiring_0) pnormalize_eq: "last p \<noteq> 0 \<Longrightarrow> pnormalize p = p"
   920   by (induct p) (auto split: split_if_asm)
   921 
   922 lemma (in semiring_0) last_pnormalize: "pnormalize p \<noteq> [] \<Longrightarrow> last (pnormalize p) \<noteq> 0"
   923   by (induct p) auto
   924 
   925 lemma (in semiring_0) pnormal_degree: "last p \<noteq> 0 \<Longrightarrow> degree p = length p - 1"
   926   using pnormalize_eq[of p] unfolding degree_def by simp
   927 
   928 lemma (in semiring_0) poly_Nil_ext: "poly [] = (\<lambda>x. 0)"
   929   by (rule ext) simp
   930 
   931 lemma (in idom_char_0) linear_mul_degree:
   932   assumes p: "poly p \<noteq> poly []"
   933   shows "degree ([a,1] *** p) = degree p + 1"
   934 proof -
   935   from p have pnz: "pnormalize p \<noteq> []"
   936     unfolding poly_zero lemma_degree_zero .
   937 
   938   from last_linear_mul[OF pnz, of a] last_pnormalize[OF pnz]
   939   have l0: "last ([a, 1] *** pnormalize p) \<noteq> 0" by simp
   940   from last_pnormalize[OF pnz] last_linear_mul[OF pnz, of a]
   941     pnormal_degree[OF l0] pnormal_degree[OF last_pnormalize[OF pnz]] pnz
   942 
   943   have th: "degree ([a,1] *** pnormalize p) = degree (pnormalize p) + 1"
   944     by simp
   945 
   946   have eqs: "poly ([a,1] *** pnormalize p) = poly ([a,1] *** p)"
   947     by (rule ext) (simp add: poly_mult poly_add poly_cmult)
   948   from degree_unique[OF eqs] th
   949   show ?thesis by (simp add: degree_unique[OF poly_normalize])
   950 qed
   951 
   952 lemma (in idom_char_0) linear_pow_mul_degree:
   953   "degree([a,1] %^n *** p) = (if poly p = poly [] then 0 else degree p + n)"
   954 proof (induct n arbitrary: a p)
   955   case (0 a p)
   956   show ?case
   957   proof (cases "poly p = poly []")
   958     case True
   959     then show ?thesis
   960       using degree_unique[OF True] by (simp add: degree_def)
   961   next
   962     case False
   963     then show ?thesis by (auto simp add: poly_Nil_ext)
   964   qed
   965 next
   966   case (Suc n a p)
   967   have eq: "poly ([a,1] %^(Suc n) *** p) = poly ([a,1] %^ n *** ([a,1] *** p))"
   968     apply (rule ext)
   969     apply (simp add: poly_mult poly_add poly_cmult)
   970     apply (simp add: mult_ac add_ac distrib_left)
   971     done
   972   note deq = degree_unique[OF eq]
   973   show ?case
   974   proof (cases "poly p = poly []")
   975     case True
   976     with eq have eq': "poly ([a,1] %^(Suc n) *** p) = poly []"
   977       apply -
   978       apply (rule ext)
   979       apply (simp add: poly_mult poly_cmult poly_add)
   980       done
   981     from degree_unique[OF eq'] True show ?thesis
   982       by (simp add: degree_def)
   983   next
   984     case False
   985     then have ap: "poly ([a,1] *** p) \<noteq> poly []"
   986       using poly_mult_not_eq_poly_Nil unfolding poly_entire by auto
   987     have eq: "poly ([a,1] %^(Suc n) *** p) = poly ([a,1]%^n *** ([a,1] *** p))"
   988       by (rule ext, simp add: poly_mult poly_add poly_exp poly_cmult algebra_simps)
   989     from ap have ap': "(poly ([a,1] *** p) = poly []) = False"
   990       by blast
   991     have th0: "degree ([a,1]%^n *** ([a,1] *** p)) = degree ([a,1] *** p) + n"
   992       apply (simp only: Suc.hyps[of a "pmult [a,one] p"] ap')
   993       apply simp
   994       done
   995     from degree_unique[OF eq] ap False th0 linear_mul_degree[OF False, of a]
   996     show ?thesis by (auto simp del: poly.simps)
   997   qed
   998 qed
   999 
  1000 lemma (in idom_char_0) order_degree:
  1001   assumes p0: "poly p \<noteq> poly []"
  1002   shows "order a p \<le> degree p"
  1003 proof -
  1004   from order2[OF p0, unfolded divides_def]
  1005   obtain q where q: "poly p = poly ([- a, 1]%^ (order a p) *** q)" by blast
  1006   {
  1007     assume "poly q = poly []"
  1008     with q p0 have False by (simp add: poly_mult poly_entire)
  1009   }
  1010   with degree_unique[OF q, unfolded linear_pow_mul_degree] show ?thesis
  1011     by auto
  1012 qed
  1013 
  1014 text{*Tidier versions of finiteness of roots.*}
  1015 
  1016 lemma (in idom_char_0) poly_roots_finite_set:
  1017   "poly p \<noteq> poly [] \<Longrightarrow> finite {x. poly p x = 0}"
  1018   unfolding poly_roots_finite .
  1019 
  1020 text{*bound for polynomial.*}
  1021 
  1022 lemma poly_mono: "abs(x) \<le> k \<Longrightarrow> abs(poly p (x::'a::{linordered_idom})) \<le> poly (map abs p) k"
  1023   apply (induct p)
  1024   apply auto
  1025   apply (rule_tac y = "abs a + abs (x * poly p x)" in order_trans)
  1026   apply (rule abs_triangle_ineq)
  1027   apply (auto intro!: mult_mono simp add: abs_mult)
  1028   done
  1029 
  1030 lemma (in semiring_0) poly_Sing: "poly [c] x = c" by simp
  1031 
  1032 end