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