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