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