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