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