src/HOL/Library/Univ_Poly.thy
 changeset 26124 2514f0ade8bc child 26194 b9763c3272cb
1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Library/Univ_Poly.thy	Mon Feb 25 11:27:27 2008 +0100
1.3 @@ -0,0 +1,1062 @@
1.4 +(*  Title:       Univ_Poly.thy
1.5 +    ID:          \$Id\$
1.6 +    Author:      Amine Chaieb
1.7 +*)
1.8 +
1.10 +
1.11 +theory Univ_Poly
1.12 +imports Main
1.13 +begin
1.14 +
1.15 +text{* Application of polynomial as a function. *}
1.16 +
1.17 +fun (in semiring_0) poly :: "'a list => 'a  => 'a" where
1.18 +  poly_Nil:  "poly [] x = 0"
1.19 +| poly_Cons: "poly (h#t) x = h + x * poly t x"
1.20 +
1.21 +
1.22 +subsection{*Arithmetic Operations on Polynomials*}
1.23 +
1.25 +
1.26 +fun (in semiring_0) padd :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "+++" 65)
1.27 +where
1.28 +  padd_Nil:  "[] +++ l2 = l2"
1.29 +| padd_Cons: "(h#t) +++ l2 = (if l2 = [] then h#t
1.30 +                            else (h + hd l2)#(t +++ tl l2))"
1.31 +
1.32 +text{*Multiplication by a constant*}
1.33 +fun (in semiring_0) cmult :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "%*" 70) where
1.34 +   cmult_Nil:  "c %* [] = []"
1.35 +|  cmult_Cons: "c %* (h#t) = (c * h)#(c %* t)"
1.36 +
1.37 +text{*Multiplication by a polynomial*}
1.38 +fun (in semiring_0) pmult :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "***" 70)
1.39 +where
1.40 +   pmult_Nil:  "[] *** l2 = []"
1.41 +|  pmult_Cons: "(h#t) *** l2 = (if t = [] then h %* l2
1.42 +                              else (h %* l2) +++ ((0) # (t *** l2)))"
1.43 +
1.44 +text{*Repeated multiplication by a polynomial*}
1.45 +fun (in semiring_0) mulexp :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a  list \<Rightarrow> 'a list" where
1.46 +   mulexp_zero:  "mulexp 0 p q = q"
1.47 +|  mulexp_Suc:   "mulexp (Suc n) p q = p *** mulexp n p q"
1.48 +
1.49 +text{*Exponential*}
1.50 +fun (in semiring_1) pexp :: "'a list \<Rightarrow> nat \<Rightarrow> 'a list"  (infixl "%^" 80) where
1.51 +   pexp_0:   "p %^ 0 = [1]"
1.52 +|  pexp_Suc: "p %^ (Suc n) = p *** (p %^ n)"
1.53 +
1.54 +text{*Quotient related value of dividing a polynomial by x + a*}
1.55 +(* Useful for divisor properties in inductive proofs *)
1.56 +fun (in field) "pquot" :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list" where
1.57 +   pquot_Nil:  "pquot [] a= []"
1.58 +|  pquot_Cons: "pquot (h#t) a = (if t = [] then [h]
1.59 +                   else (inverse(a) * (h - hd( pquot t a)))#(pquot t a))"
1.60 +
1.61 +text{*normalization of polynomials (remove extra 0 coeff)*}
1.62 +fun (in semiring_0) pnormalize :: "'a list \<Rightarrow> 'a list" where
1.63 +  pnormalize_Nil:  "pnormalize [] = []"
1.64 +| pnormalize_Cons: "pnormalize (h#p) = (if ( (pnormalize p) = [])
1.65 +                                     then (if (h = 0) then [] else [h])
1.66 +                                     else (h#(pnormalize p)))"
1.67 +
1.68 +definition (in semiring_0) "pnormal p = ((pnormalize p = p) \<and> p \<noteq> [])"
1.69 +definition (in semiring_0) "nonconstant p = (pnormal p \<and> (\<forall>x. p \<noteq> [x]))"
1.70 +text{*Other definitions*}
1.71 +
1.72 +definition (in ring_1)
1.73 +  poly_minus :: "'a list => 'a list" ("-- _" [80] 80) where
1.74 +  "-- p = (- 1) %* p"
1.75 +
1.76 +definition (in semiring_0)
1.77 +  divides :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  (infixl "divides" 70) where
1.78 +  "p1 divides p2 = (\<exists>q. poly p2 = poly(p1 *** q))"
1.79 +
1.80 +    --{*order of a polynomial*}
1.81 +definition (in ring_1) order :: "'a => 'a list => nat" where
1.82 +  "order a p = (SOME n. ([-a, 1] %^ n) divides p &
1.83 +                      ~ (([-a, 1] %^ (Suc n)) divides p))"
1.84 +
1.85 +     --{*degree of a polynomial*}
1.86 +definition (in semiring_0) degree :: "'a list => nat" where
1.87 +  "degree p = length (pnormalize p) - 1"
1.88 +
1.89 +     --{*squarefree polynomials --- NB with respect to real roots only.*}
1.90 +definition (in ring_1)
1.91 +  rsquarefree :: "'a list => bool" where
1.92 +  "rsquarefree p = (poly p \<noteq> poly [] &
1.93 +                     (\<forall>a. (order a p = 0) | (order a p = 1)))"
1.94 +
1.95 +context semiring_0
1.96 +begin
1.97 +
1.98 +lemma padd_Nil2[simp]: "p +++ [] = p"
1.99 +by (induct p) auto
1.101 +lemma padd_Cons_Cons: "(h1 # p1) +++ (h2 # p2) = (h1 + h2) # (p1 +++ p2)"
1.102 +by auto
1.104 +lemma pminus_Nil[simp]: "-- [] = []"
1.107 +lemma pmult_singleton: "[h1] *** p1 = h1 %* p1" by simp
1.108 +end
1.110 +lemma (in semiring_1) poly_ident_mult[simp]: "1 %* t = t" by (induct "t", auto)
1.112 +lemma (in semiring_0) poly_simple_add_Cons[simp]: "[a] +++ ((0)#t) = (a#t)"
1.113 +by simp
1.115 +text{*Handy general properties*}
1.117 +lemma (in comm_semiring_0) padd_commut: "b +++ a = a +++ b"
1.118 +proof(induct b arbitrary: a)
1.119 +  case Nil thus ?case by auto
1.120 +next
1.121 +  case (Cons b bs a) thus ?case by (cases a, simp_all add: add_commute)
1.122 +qed
1.124 +lemma (in comm_semiring_0) padd_assoc: "\<forall>b c. (a +++ b) +++ c = a +++ (b +++ c)"
1.125 +apply (induct a arbitrary: b c)
1.126 +apply (simp, clarify)
1.128 +done
1.130 +lemma (in semiring_0) poly_cmult_distr: "a %* ( p +++ q) = (a %* p +++ a %* q)"
1.131 +apply (induct p arbitrary: q,simp)
1.132 +apply (case_tac q, simp_all add: right_distrib)
1.133 +done
1.135 +lemma (in ring_1) pmult_by_x[simp]: "[0, 1] *** t = ((0)#t)"
1.136 +apply (induct "t", simp)
1.138 +apply (case_tac t, auto)
1.139 +done
1.141 +text{*properties of evaluation of polynomials.*}
1.143 +lemma (in semiring_0) poly_add: "poly (p1 +++ p2) x = poly p1 x + poly p2 x"
1.144 +proof(induct p1 arbitrary: p2)
1.145 +  case Nil thus ?case by simp
1.146 +next
1.147 +  case (Cons a as p2) thus ?case
1.149 +qed
1.151 +lemma (in comm_semiring_0) poly_cmult: "poly (c %* p) x = c * poly p x"
1.152 +apply (induct "p")
1.153 +apply (case_tac [2] "x=zero")
1.154 +apply (auto simp add: right_distrib mult_ac)
1.155 +done
1.157 +lemma (in comm_semiring_0) poly_cmult_map: "poly (map (op * c) p) x = c*poly p x"
1.158 +  by (induct p, auto simp add: right_distrib mult_ac)
1.160 +lemma (in comm_ring_1) poly_minus: "poly (-- p) x = - (poly p x)"
1.162 +apply (auto simp add: poly_cmult minus_mult_left[symmetric])
1.163 +done
1.165 +lemma (in comm_semiring_0) poly_mult: "poly (p1 *** p2) x = poly p1 x * poly p2 x"
1.166 +proof(induct p1 arbitrary: p2)
1.167 +  case Nil thus ?case by simp
1.168 +next
1.169 +  case (Cons a as p2)
1.170 +  thus ?case by (cases as,
1.172 +qed
1.174 +class recpower_semiring = semiring + recpower
1.175 +class recpower_semiring_1 = semiring_1 + recpower
1.176 +class recpower_semiring_0 = semiring_0 + recpower
1.177 +class recpower_ring = ring + recpower
1.178 +class recpower_ring_1 = ring_1 + recpower
1.179 +subclass (in recpower_ring_1) recpower_ring by unfold_locales
1.180 +subclass (in recpower_ring_1) recpower_ring by unfold_locales
1.181 +class recpower_comm_semiring_1 = recpower + comm_semiring_1
1.182 +class recpower_comm_ring_1 = recpower + comm_ring_1
1.183 +subclass (in recpower_comm_ring_1) recpower_comm_semiring_1 by unfold_locales
1.184 +class recpower_idom = recpower + idom
1.185 +subclass (in recpower_idom) recpower_comm_ring_1 by unfold_locales
1.186 +class idom_char_0 = idom + ring_char_0
1.187 +class recpower_idom_char_0 = recpower + idom_char_0
1.188 +subclass (in recpower_idom_char_0) recpower_idom by unfold_locales
1.190 +lemma (in recpower_comm_ring_1) poly_exp: "poly (p %^ n) x = (poly p x) ^ n"
1.191 +apply (induct "n")
1.192 +apply (auto simp add: poly_cmult poly_mult power_Suc)
1.193 +done
1.195 +text{*More Polynomial Evaluation Lemmas*}
1.197 +lemma  (in semiring_0) poly_add_rzero[simp]: "poly (a +++ []) x = poly a x"
1.198 +by simp
1.200 +lemma (in comm_semiring_0) poly_mult_assoc: "poly ((a *** b) *** c) x = poly (a *** (b *** c)) x"
1.201 +  by (simp add: poly_mult mult_assoc)
1.203 +lemma (in semiring_0) poly_mult_Nil2[simp]: "poly (p *** []) x = 0"
1.204 +by (induct "p", auto)
1.206 +lemma (in comm_semiring_1) poly_exp_add: "poly (p %^ (n + d)) x = poly( p %^ n *** p %^ d) x"
1.207 +apply (induct "n")
1.208 +apply (auto simp add: poly_mult mult_assoc)
1.209 +done
1.211 +subsection{*Key Property: if @{term "f(a) = 0"} then @{term "(x - a)"} divides
1.212 + @{term "p(x)"} *}
1.214 +lemma (in comm_ring_1) lemma_poly_linear_rem: "\<forall>h. \<exists>q r. h#t = [r] +++ [-a, 1] *** q"
1.215 +proof(induct t)
1.216 +  case Nil
1.217 +  {fix h have "[h] = [h] +++ [- a, 1] *** []" by simp}
1.218 +  thus ?case by blast
1.219 +next
1.220 +  case (Cons  x xs)
1.221 +  {fix h
1.222 +    from Cons.hyps[rule_format, of x]
1.223 +    obtain q r where qr: "x#xs = [r] +++ [- a, 1] *** q" by blast
1.224 +    have "h#x#xs = [a*r + h] +++ [-a, 1] *** (r#q)"
1.225 +      using qr by(cases q, simp_all add: ring_simps diff_def[symmetric]
1.226 +	minus_mult_left[symmetric] right_minus)
1.227 +    hence "\<exists>q r. h#x#xs = [r] +++ [-a, 1] *** q" by blast}
1.228 +  thus ?case by blast
1.229 +qed
1.231 +lemma (in comm_ring_1) poly_linear_rem: "\<exists>q r. h#t = [r] +++ [-a, 1] *** q"
1.232 +by (cut_tac t = t and a = a in lemma_poly_linear_rem, auto)
1.235 +lemma (in comm_ring_1) poly_linear_divides: "(poly p a = 0) = ((p = []) | (\<exists>q. p = [-a, 1] *** q))"
1.236 +proof-
1.237 +  {assume p: "p = []" hence ?thesis by simp}
1.238 +  moreover
1.239 +  {fix x xs assume p: "p = x#xs"
1.240 +    {fix q assume "p = [-a, 1] *** q" hence "poly p a = 0"
1.242 +    moreover
1.243 +    {assume p0: "poly p a = 0"
1.244 +      from poly_linear_rem[of x xs a] obtain q r
1.245 +      where qr: "x#xs = [r] +++ [- a, 1] *** q" by blast
1.246 +      have "r = 0" using p0 by (simp only: p qr poly_mult poly_add) simp
1.247 +      hence "\<exists>q. p = [- a, 1] *** q" using p qr  apply - apply (rule exI[where x=q])apply auto apply (cases q) apply auto done}
1.248 +    ultimately have ?thesis using p by blast}
1.249 +  ultimately show ?thesis by (cases p, auto)
1.250 +qed
1.252 +lemma (in semiring_0) lemma_poly_length_mult[simp]: "\<forall>h k a. length (k %* p +++  (h # (a %* p))) = Suc (length p)"
1.253 +by (induct "p", auto)
1.255 +lemma (in semiring_0) lemma_poly_length_mult2[simp]: "\<forall>h k. length (k %* p +++  (h # p)) = Suc (length p)"
1.256 +by (induct "p", auto)
1.258 +lemma (in ring_1) poly_length_mult[simp]: "length([-a,1] *** q) = Suc (length q)"
1.259 +by auto
1.261 +subsection{*Polynomial length*}
1.263 +lemma (in semiring_0) poly_cmult_length[simp]: "length (a %* p) = length p"
1.264 +by (induct "p", auto)
1.266 +lemma (in semiring_0) poly_add_length: "length (p1 +++ p2) = max (length p1) (length p2)"
1.267 +apply (induct p1 arbitrary: p2, simp_all)
1.268 +apply arith
1.269 +done
1.271 +lemma (in semiring_0) poly_root_mult_length[simp]: "length([a,b] *** p) = Suc (length p)"
1.274 +lemma (in idom) poly_mult_not_eq_poly_Nil[simp]:
1.275 + "poly (p *** q) x \<noteq> poly [] x \<longleftrightarrow> poly p x \<noteq> poly [] x \<and> poly q x \<noteq> poly [] x"
1.276 +by (auto simp add: poly_mult)
1.278 +lemma (in idom) poly_mult_eq_zero_disj: "poly (p *** q) x = 0 \<longleftrightarrow> poly p x = 0 \<or> poly q x = 0"
1.279 +by (auto simp add: poly_mult)
1.281 +text{*Normalisation Properties*}
1.283 +lemma (in semiring_0) poly_normalized_nil: "(pnormalize p = []) --> (poly p x = 0)"
1.284 +by (induct "p", auto)
1.286 +text{*A nontrivial polynomial of degree n has no more than n roots*}
1.287 +lemma (in idom) poly_roots_index_lemma:
1.288 +   assumes p: "poly p x \<noteq> poly [] x" and n: "length p = n"
1.289 +  shows "\<exists>i. \<forall>x. poly p x = 0 \<longrightarrow> (\<exists>m\<le>n. x = i m)"
1.290 +  using p n
1.291 +proof(induct n arbitrary: p x)
1.292 +  case 0 thus ?case by simp
1.293 +next
1.294 +  case (Suc n p x)
1.295 +  {assume C: "\<And>i. \<exists>x. poly p x = 0 \<and> (\<forall>m\<le>Suc n. x \<noteq> i m)"
1.296 +    from Suc.prems have p0: "poly p x \<noteq> 0" "p\<noteq> []" by auto
1.297 +    from p0(1)[unfolded poly_linear_divides[of p x]]
1.298 +    have "\<forall>q. p \<noteq> [- x, 1] *** q" by blast
1.299 +    from C obtain a where a: "poly p a = 0" by blast
1.300 +    from a[unfolded poly_linear_divides[of p a]] p0(2)
1.301 +    obtain q where q: "p = [-a, 1] *** q" by blast
1.302 +    have lg: "length q = n" using q Suc.prems(2) by simp
1.303 +    from q p0 have qx: "poly q x \<noteq> poly [] x"
1.305 +    from Suc.hyps[OF qx lg] obtain i where
1.306 +      i: "\<forall>x. poly q x = 0 \<longrightarrow> (\<exists>m\<le>n. x = i m)" by blast
1.307 +    let ?i = "\<lambda>m. if m = Suc n then a else i m"
1.308 +    from C[of ?i] obtain y where y: "poly p y = 0" "\<forall>m\<le> Suc n. y \<noteq> ?i m"
1.309 +      by blast
1.310 +    from y have "y = a \<or> poly q y = 0"
1.312 +    with i[rule_format, of y] y(1) y(2) have False apply auto
1.313 +      apply (erule_tac x="m" in allE)
1.314 +      apply auto
1.315 +      done}
1.316 +  thus ?case by blast
1.317 +qed
1.320 +lemma (in idom) poly_roots_index_length: "poly p x \<noteq> poly [] x ==>
1.321 +      \<exists>i. \<forall>x. (poly p x = 0) --> (\<exists>n. n \<le> length p & x = i n)"
1.322 +by (blast intro: poly_roots_index_lemma)
1.324 +lemma (in idom) poly_roots_finite_lemma: "poly p x \<noteq> poly [] x ==>
1.325 +      \<exists>N i. \<forall>x. (poly p x = 0) --> (\<exists>n. (n::nat) < N & x = i n)"
1.326 +apply (drule poly_roots_index_length, safe)
1.327 +apply (rule_tac x = "Suc (length p)" in exI)
1.328 +apply (rule_tac x = i in exI)
1.330 +done
1.333 +lemma (in idom) idom_finite_lemma:
1.334 +  assumes P: "\<forall>x. P x --> (\<exists>n. n < length j & x = j!n)"
1.335 +  shows "finite {x. P x}"
1.336 +proof-
1.337 +  let ?M = "{x. P x}"
1.338 +  let ?N = "set j"
1.339 +  have "?M \<subseteq> ?N" using P by auto
1.340 +  thus ?thesis using finite_subset by auto
1.341 +qed
1.344 +lemma (in idom) poly_roots_finite_lemma: "poly p x \<noteq> poly [] x ==>
1.345 +      \<exists>i. \<forall>x. (poly p x = 0) --> x \<in> set i"
1.346 +apply (drule poly_roots_index_length, safe)
1.347 +apply (rule_tac x="map (\<lambda>n. i n) [0 ..< Suc (length p)]" in exI)
1.348 +apply (auto simp add: image_iff)
1.349 +apply (erule_tac x="x" in allE, clarsimp)
1.350 +by (case_tac "n=length p", auto simp add: order_le_less)
1.352 +lemma UNIV_nat_infinite: "\<not> finite (UNIV :: nat set)"
1.353 +  unfolding finite_conv_nat_seg_image
1.354 +proof(auto simp add: expand_set_eq image_iff)
1.355 +  fix n::nat and f:: "nat \<Rightarrow> nat"
1.356 +  let ?N = "{i. i < n}"
1.357 +  let ?fN = "f ` ?N"
1.358 +  let ?y = "Max ?fN + 1"
1.359 +  from nat_seg_image_imp_finite[of "?fN" "f" n]
1.360 +  have thfN: "finite ?fN" by simp
1.361 +  {assume "n =0" hence "\<exists>x. \<forall>xa<n. x \<noteq> f xa" by auto}
1.362 +  moreover
1.363 +  {assume nz: "n \<noteq> 0"
1.364 +    hence thne: "?fN \<noteq> {}" by (auto simp add: neq0_conv)
1.365 +    have "\<forall>x\<in> ?fN. Max ?fN \<ge> x" using nz Max_ge_iff[OF thfN thne] by auto
1.366 +    hence "\<forall>x\<in> ?fN. ?y > x" by auto
1.367 +    hence "?y \<notin> ?fN" by auto
1.368 +    hence "\<exists>x. \<forall>xa<n. x \<noteq> f xa" by auto }
1.369 +  ultimately show "\<exists>x. \<forall>xa<n. x \<noteq> f xa" by blast
1.370 +qed
1.372 +lemma (in ring_char_0) UNIV_ring_char_0_infinte:
1.373 +  "\<not> (finite (UNIV:: 'a set))"
1.374 +proof
1.375 +  assume F: "finite (UNIV :: 'a set)"
1.376 +  have th0: "of_nat ` UNIV \<subseteq> UNIV" by simp
1.377 +  from finite_subset[OF th0] have th: "finite (of_nat ` UNIV :: 'a set)" .
1.378 +  have th': "inj_on (of_nat::nat \<Rightarrow> 'a) (UNIV)"
1.379 +    unfolding inj_on_def by auto
1.380 +  from finite_imageD[OF th th'] UNIV_nat_infinite
1.381 +  show False by blast
1.382 +qed
1.384 +lemma (in idom_char_0) poly_roots_finite: "(poly p \<noteq> poly []) =
1.385 +  finite {x. poly p x = 0}"
1.386 +proof
1.387 +  assume H: "poly p \<noteq> poly []"
1.388 +  show "finite {x. poly p x = (0::'a)}"
1.389 +    using H
1.390 +    apply -
1.391 +    apply (erule contrapos_np, rule ext)
1.392 +    apply (rule ccontr)
1.393 +    apply (clarify dest!: poly_roots_finite_lemma)
1.394 +    using finite_subset
1.395 +  proof-
1.396 +    fix x i
1.397 +    assume F: "\<not> finite {x. poly p x = (0\<Colon>'a)}"
1.398 +      and P: "\<forall>x. poly p x = (0\<Colon>'a) \<longrightarrow> x \<in> set i"
1.399 +    let ?M= "{x. poly p x = (0\<Colon>'a)}"
1.400 +    from P have "?M \<subseteq> set i" by auto
1.401 +    with finite_subset F show False by auto
1.402 +  qed
1.403 +next
1.404 +  assume F: "finite {x. poly p x = (0\<Colon>'a)}"
1.405 +  show "poly p \<noteq> poly []" using F UNIV_ring_char_0_infinte by auto
1.406 +qed
1.408 +text{*Entirety and Cancellation for polynomials*}
1.410 +lemma (in idom_char_0) poly_entire_lemma: "\<lbrakk>poly p \<noteq> poly [] ; poly q \<noteq> poly [] \<rbrakk>
1.411 +      \<Longrightarrow>  poly (p *** q) \<noteq> poly []"
1.412 +by (auto simp add: poly_roots_finite poly_mult Collect_disj_eq)
1.414 +lemma (in idom_char_0) poly_entire: "poly (p *** q) = poly [] \<longleftrightarrow>(poly p = poly []) | (poly q = poly [])"
1.415 +apply (auto intro: ext dest: fun_cong simp add: poly_entire_lemma poly_mult)
1.416 +apply (blast intro: ccontr dest: poly_entire_lemma poly_mult [THEN subst])
1.417 +done
1.419 +lemma (in idom_char_0) poly_entire_neg: "(poly (p *** q) \<noteq> poly []) = ((poly p \<noteq> poly []) & (poly q \<noteq> poly []))"
1.422 +lemma fun_eq: " (f = g) = (\<forall>x. f x = g x)"
1.423 +by (auto intro!: ext)
1.425 +lemma (in comm_ring_1) poly_add_minus_zero_iff: "(poly (p +++ -- q) = poly []) = (poly p = poly q)"
1.428 +lemma (in comm_ring_1) poly_add_minus_mult_eq: "poly (p *** q +++ --(p *** r)) = poly (p *** (q +++ -- r))"
1.429 +by (auto simp add: poly_add poly_minus_def fun_eq poly_mult poly_cmult right_distrib minus_mult_left[symmetric] minus_mult_right[symmetric])
1.431 +subclass (in idom_char_0) comm_ring_1 by unfold_locales
1.432 +lemma (in idom_char_0) poly_mult_left_cancel: "(poly (p *** q) = poly (p *** r)) = (poly p = poly [] | poly q = poly r)"
1.433 +proof-
1.434 +  have "poly (p *** q) = poly (p *** r) \<longleftrightarrow> poly (p *** q +++ -- (p *** r)) = poly []" by (simp only: poly_add_minus_zero_iff)
1.435 +  also have "\<dots> \<longleftrightarrow> poly p = poly [] | poly q = poly r"
1.437 +  finally show ?thesis .
1.438 +qed
1.440 +lemma (in recpower_idom) poly_exp_eq_zero[simp]:
1.441 +     "(poly (p %^ n) = poly []) = (poly p = poly [] & n \<noteq> 0)"
1.442 +apply (simp only: fun_eq add: all_simps [symmetric])
1.443 +apply (rule arg_cong [where f = All])
1.444 +apply (rule ext)
1.445 +apply (induct_tac "n")
1.447 +using zero_neq_one apply blast
1.448 +apply (auto simp add: poly_exp poly_mult)
1.449 +done
1.451 +lemma (in semiring_1) one_neq_zero[simp]: "1 \<noteq> 0" using zero_neq_one by blast
1.452 +lemma (in comm_ring_1) poly_prime_eq_zero[simp]: "poly [a,1] \<noteq> poly []"
1.454 +apply (rule_tac x = "minus one a" in exI)
1.455 +apply (unfold diff_minus)
1.458 +apply simp
1.459 +done
1.461 +lemma (in recpower_idom) poly_exp_prime_eq_zero: "(poly ([a, 1] %^ n) \<noteq> poly [])"
1.462 +by auto
1.464 +text{*A more constructive notion of polynomials being trivial*}
1.466 +lemma (in idom_char_0) poly_zero_lemma': "poly (h # t) = poly [] ==> h = 0 & poly t = poly []"
1.468 +apply (case_tac "h = zero")
1.469 +apply (drule_tac [2] x = zero in spec, auto)
1.470 +apply (cases "poly t = poly []", simp)
1.471 +proof-
1.472 +  fix x
1.473 +  assume H: "\<forall>x. x = (0\<Colon>'a) \<or> poly t x = (0\<Colon>'a)"  and pnz: "poly t \<noteq> poly []"
1.474 +  let ?S = "{x. poly t x = 0}"
1.475 +  from H have "\<forall>x. x \<noteq>0 \<longrightarrow> poly t x = 0" by blast
1.476 +  hence th: "?S \<supseteq> UNIV - {0}" by auto
1.477 +  from poly_roots_finite pnz have th': "finite ?S" by blast
1.478 +  from finite_subset[OF th th'] UNIV_ring_char_0_infinte
1.479 +  show "poly t x = (0\<Colon>'a)" by simp
1.480 +  qed
1.482 +lemma (in idom_char_0) poly_zero: "(poly p = poly []) = list_all (%c. c = 0) p"
1.483 +apply (induct "p", simp)
1.484 +apply (rule iffI)
1.485 +apply (drule poly_zero_lemma', auto)
1.486 +done
1.488 +lemma (in idom_char_0) poly_0: "list_all (\<lambda>c. c = 0) p \<Longrightarrow> poly p x = 0"
1.489 +  unfolding poly_zero[symmetric] by simp
1.493 +text{*Basics of divisibility.*}
1.495 +lemma (in idom) poly_primes: "([a, 1] divides (p *** q)) = ([a, 1] divides p | [a, 1] divides q)"
1.496 +apply (auto simp add: divides_def fun_eq poly_mult poly_add poly_cmult left_distrib [symmetric])
1.497 +apply (drule_tac x = "uminus a" in spec)
1.499 +apply (cases "p = []")
1.500 +apply (rule exI[where x="[]"])
1.501 +apply simp
1.502 +apply (cases "q = []")
1.503 +apply (erule allE[where x="[]"], simp)
1.505 +apply clarsimp
1.506 +apply (cases "\<exists>q\<Colon>'a list. p = a %* q +++ ((0\<Colon>'a) # q)")
1.508 +apply (rule_tac x="qa" in exI)
1.509 +apply (simp add: left_distrib [symmetric])
1.510 +apply clarsimp
1.513 +apply (rule_tac x = "pmult qa q" in exI)
1.514 +apply (rule_tac [2] x = "pmult p qa" in exI)
1.516 +done
1.518 +lemma (in comm_semiring_1) poly_divides_refl[simp]: "p divides p"
1.520 +apply (rule_tac x = "[one]" in exI)
1.521 +apply (auto simp add: poly_mult fun_eq)
1.522 +done
1.524 +lemma (in comm_semiring_1) poly_divides_trans: "[| p divides q; q divides r |] ==> p divides r"
1.525 +apply (simp add: divides_def, safe)
1.526 +apply (rule_tac x = "pmult qa qaa" in exI)
1.527 +apply (auto simp add: poly_mult fun_eq mult_assoc)
1.528 +done
1.531 +lemma (in recpower_comm_semiring_1) poly_divides_exp: "m \<le> n ==> (p %^ m) divides (p %^ n)"
1.533 +apply (induct_tac k)
1.534 +apply (rule_tac [2] poly_divides_trans)
1.535 +apply (auto simp add: divides_def)
1.536 +apply (rule_tac x = p in exI)
1.537 +apply (auto simp add: poly_mult fun_eq mult_ac)
1.538 +done
1.540 +lemma (in recpower_comm_semiring_1) poly_exp_divides: "[| (p %^ n) divides q;  m\<le>n |] ==> (p %^ m) divides q"
1.541 +by (blast intro: poly_divides_exp poly_divides_trans)
1.544 +   "[| p divides q; p divides r |] ==> p divides (q +++ r)"
1.545 +apply (simp add: divides_def, auto)
1.546 +apply (rule_tac x = "padd qa qaa" in exI)
1.548 +done
1.550 +lemma (in comm_ring_1) poly_divides_diff:
1.551 +   "[| p divides q; p divides (q +++ r) |] ==> p divides r"
1.552 +apply (simp add: divides_def, auto)
1.553 +apply (rule_tac x = "padd qaa (poly_minus qa)" in exI)
1.555 +done
1.557 +lemma (in comm_ring_1) poly_divides_diff2: "[| p divides r; p divides (q +++ r) |] ==> p divides q"
1.558 +apply (erule poly_divides_diff)
1.560 +done
1.562 +lemma (in semiring_0) poly_divides_zero: "poly p = poly [] ==> q divides p"
1.564 +apply (rule exI[where x="[]"])
1.565 +apply (auto simp add: fun_eq poly_mult)
1.566 +done
1.568 +lemma (in semiring_0) poly_divides_zero2[simp]: "q divides []"
1.570 +apply (rule_tac x = "[]" in exI)
1.571 +apply (auto simp add: fun_eq)
1.572 +done
1.574 +text{*At last, we can consider the order of a root.*}
1.576 +lemma (in idom_char_0)  poly_order_exists_lemma:
1.577 +  assumes lp: "length p = d" and p: "poly p \<noteq> poly []"
1.578 +  shows "\<exists>n q. p = mulexp n [-a, 1] q \<and> poly q a \<noteq> 0"
1.579 +using lp p
1.580 +proof(induct d arbitrary: p)
1.581 +  case 0 thus ?case by simp
1.582 +next
1.583 +  case (Suc n p)
1.584 +  {assume p0: "poly p a = 0"
1.585 +    from Suc.prems have h: "length p = Suc n" "poly p \<noteq> poly []" by blast
1.586 +    hence pN: "p \<noteq> []" by - (rule ccontr, simp)
1.587 +    from p0[unfolded poly_linear_divides] pN  obtain q where
1.588 +      q: "p = [-a, 1] *** q" by blast
1.589 +    from q h p0 have qh: "length q = n" "poly q \<noteq> poly []"
1.590 +      apply -
1.591 +      apply simp
1.592 +      apply (simp only: fun_eq)
1.593 +      apply (rule ccontr)
1.595 +      done
1.596 +    from Suc.hyps[OF qh] obtain m r where
1.597 +      mr: "q = mulexp m [-a,1] r" "poly r a \<noteq> 0" by blast
1.598 +    from mr q have "p = mulexp (Suc m) [-a,1] r \<and> poly r a \<noteq> 0" by simp
1.599 +    hence ?case by blast}
1.600 +  moreover
1.601 +  {assume p0: "poly p a \<noteq> 0"
1.602 +    hence ?case using Suc.prems apply simp by (rule exI[where x="0::nat"], simp)}
1.603 +  ultimately show ?case by blast
1.604 +qed
1.607 +lemma (in recpower_comm_semiring_1) poly_mulexp: "poly (mulexp n p q) x = (poly p x) ^ n * poly q x"
1.608 +by(induct n, auto simp add: poly_mult power_Suc mult_ac)
1.610 +lemma (in comm_semiring_1) divides_left_mult:
1.611 +  assumes d:"(p***q) divides r" shows "p divides r \<and> q divides r"
1.612 +proof-
1.613 +  from d obtain t where r:"poly r = poly (p***q *** t)"
1.614 +    unfolding divides_def by blast
1.615 +  hence "poly r = poly (p *** (q *** t))"
1.616 +    "poly r = poly (q *** (p***t))" by(auto simp add: fun_eq poly_mult mult_ac)
1.617 +  thus ?thesis unfolding divides_def by blast
1.618 +qed
1.622 +(* FIXME: Tidy up *)
1.624 +lemma (in recpower_semiring_1)
1.625 +  zero_power_iff: "0 ^ n = (if n = 0 then 1 else 0)"
1.626 +  by (induct n, simp_all add: power_Suc)
1.628 +lemma (in recpower_idom_char_0) poly_order_exists:
1.629 +  assumes lp: "length p = d" and p0: "poly p \<noteq> poly []"
1.630 +  shows "\<exists>n. ([-a, 1] %^ n) divides p & ~(([-a, 1] %^ (Suc n)) divides p)"
1.631 +proof-
1.632 +let ?poly = poly
1.633 +let ?mulexp = mulexp
1.634 +let ?pexp = pexp
1.635 +from lp p0
1.636 +show ?thesis
1.637 +apply -
1.638 +apply (drule poly_order_exists_lemma [where a=a], assumption, clarify)
1.639 +apply (rule_tac x = n in exI, safe)
1.640 +apply (unfold divides_def)
1.641 +apply (rule_tac x = q in exI)
1.642 +apply (induct_tac "n", simp)
1.644 +apply safe
1.645 +apply (subgoal_tac "?poly (?mulexp n [uminus a, one] q) \<noteq> ?poly (pmult (?pexp [uminus a, one] (Suc n)) qa)")
1.646 +apply simp
1.647 +apply (induct_tac "n")
1.648 +apply (simp del: pmult_Cons pexp_Suc)
1.649 +apply (erule_tac Q = "?poly q a = zero" in contrapos_np)
1.651 +apply (rule pexp_Suc [THEN ssubst])
1.652 +apply (rule ccontr)
1.653 +apply (simp add: poly_mult_left_cancel poly_mult_assoc del: pmult_Cons pexp_Suc)
1.654 +done
1.655 +qed
1.658 +lemma (in semiring_1) poly_one_divides[simp]: "[1] divides p"
1.659 +by (simp add: divides_def, auto)
1.661 +lemma (in recpower_idom_char_0) poly_order: "poly p \<noteq> poly []
1.662 +      ==> EX! n. ([-a, 1] %^ n) divides p &
1.663 +                 ~(([-a, 1] %^ (Suc n)) divides p)"
1.664 +apply (auto intro: poly_order_exists simp add: less_linear simp del: pmult_Cons pexp_Suc)
1.665 +apply (cut_tac x = y and y = n in less_linear)
1.666 +apply (drule_tac m = n in poly_exp_divides)
1.667 +apply (auto dest: Suc_le_eq [THEN iffD2, THEN [2] poly_exp_divides]
1.668 +            simp del: pmult_Cons pexp_Suc)
1.669 +done
1.671 +text{*Order*}
1.673 +lemma some1_equalityD: "[| n = (@n. P n); EX! n. P n |] ==> P n"
1.674 +by (blast intro: someI2)
1.676 +lemma (in recpower_idom_char_0) order:
1.677 +      "(([-a, 1] %^ n) divides p &
1.678 +        ~(([-a, 1] %^ (Suc n)) divides p)) =
1.679 +        ((n = order a p) & ~(poly p = poly []))"
1.680 +apply (unfold order_def)
1.681 +apply (rule iffI)
1.682 +apply (blast dest: poly_divides_zero intro!: some1_equality [symmetric] poly_order)
1.683 +apply (blast intro!: poly_order [THEN [2] some1_equalityD])
1.684 +done
1.686 +lemma (in recpower_idom_char_0) order2: "[| poly p \<noteq> poly [] |]
1.687 +      ==> ([-a, 1] %^ (order a p)) divides p &
1.688 +              ~(([-a, 1] %^ (Suc(order a p))) divides p)"
1.689 +by (simp add: order del: pexp_Suc)
1.691 +lemma (in recpower_idom_char_0) order_unique: "[| poly p \<noteq> poly []; ([-a, 1] %^ n) divides p;
1.692 +         ~(([-a, 1] %^ (Suc n)) divides p)
1.693 +      |] ==> (n = order a p)"
1.694 +by (insert order [of a n p], auto)
1.696 +lemma (in recpower_idom_char_0) order_unique_lemma: "(poly p \<noteq> poly [] & ([-a, 1] %^ n) divides p &
1.697 +         ~(([-a, 1] %^ (Suc n)) divides p))
1.698 +      ==> (n = order a p)"
1.699 +by (blast intro: order_unique)
1.701 +lemma (in ring_1) order_poly: "poly p = poly q ==> order a p = order a q"
1.702 +by (auto simp add: fun_eq divides_def poly_mult order_def)
1.704 +lemma (in semiring_1) pexp_one[simp]: "p %^ (Suc 0) = p"
1.705 +apply (induct "p")
1.706 +apply (auto simp add: numeral_1_eq_1)
1.707 +done
1.709 +lemma (in comm_ring_1) lemma_order_root:
1.710 +     " 0 < n & [- a, 1] %^ n divides p & ~ [- a, 1] %^ (Suc n) divides p
1.711 +             \<Longrightarrow> poly p a = 0"
1.712 +apply (induct n arbitrary: a p, blast)
1.713 +apply (auto simp add: divides_def poly_mult simp del: pmult_Cons)
1.714 +done
1.716 +lemma (in recpower_idom_char_0) order_root: "(poly p a = 0) = ((poly p = poly []) | order a p \<noteq> 0)"
1.717 +proof-
1.718 +  let ?poly = poly
1.719 +  show ?thesis
1.720 +apply (case_tac "?poly p = ?poly []", auto)
1.721 +apply (simp add: poly_linear_divides del: pmult_Cons, safe)
1.722 +apply (drule_tac [!] a = a in order2)
1.723 +apply (rule ccontr)
1.724 +apply (simp add: divides_def poly_mult fun_eq del: pmult_Cons, blast)
1.725 +using neq0_conv
1.726 +apply (blast intro: lemma_order_root)
1.727 +done
1.728 +qed
1.730 +lemma (in recpower_idom_char_0) order_divides: "(([-a, 1] %^ n) divides p) = ((poly p = poly []) | n \<le> order a p)"
1.731 +proof-
1.732 +  let ?poly = poly
1.733 +  show ?thesis
1.734 +apply (case_tac "?poly p = ?poly []", auto)
1.735 +apply (simp add: divides_def fun_eq poly_mult)
1.736 +apply (rule_tac x = "[]" in exI)
1.737 +apply (auto dest!: order2 [where a=a]
1.738 +	    intro: poly_exp_divides simp del: pexp_Suc)
1.739 +done
1.740 +qed
1.742 +lemma (in recpower_idom_char_0) order_decomp:
1.743 +     "poly p \<noteq> poly []
1.744 +      ==> \<exists>q. (poly p = poly (([-a, 1] %^ (order a p)) *** q)) &
1.745 +                ~([-a, 1] divides q)"
1.746 +apply (unfold divides_def)
1.747 +apply (drule order2 [where a = a])
1.748 +apply (simp add: divides_def del: pexp_Suc pmult_Cons, safe)
1.749 +apply (rule_tac x = q in exI, safe)
1.750 +apply (drule_tac x = qa in spec)
1.751 +apply (auto simp add: poly_mult fun_eq poly_exp mult_ac simp del: pmult_Cons)
1.752 +done
1.754 +text{*Important composition properties of orders.*}
1.755 +lemma order_mult: "poly (p *** q) \<noteq> poly []
1.756 +      ==> order a (p *** q) = order a p + order (a::'a::{recpower_idom_char_0}) q"
1.757 +apply (cut_tac a = a and p = "p *** q" and n = "order a p + order a q" in order)
1.758 +apply (auto simp add: poly_entire simp del: pmult_Cons)
1.759 +apply (drule_tac a = a in order2)+
1.760 +apply safe
1.762 +apply (rule_tac x = "qa *** qaa" in exI)
1.763 +apply (simp add: poly_mult mult_ac del: pmult_Cons)
1.764 +apply (drule_tac a = a in order_decomp)+
1.765 +apply safe
1.766 +apply (subgoal_tac "[-a,1] divides (qa *** qaa) ")
1.767 +apply (simp add: poly_primes del: pmult_Cons)
1.768 +apply (auto simp add: divides_def simp del: pmult_Cons)
1.769 +apply (rule_tac x = qb in exI)
1.770 +apply (subgoal_tac "poly ([-a, 1] %^ (order a p) *** (qa *** qaa)) = poly ([-a, 1] %^ (order a p) *** ([-a, 1] *** qb))")
1.771 +apply (drule poly_mult_left_cancel [THEN iffD1], force)
1.772 +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))) ")
1.773 +apply (drule poly_mult_left_cancel [THEN iffD1], force)
1.775 +done
1.777 +lemma (in recpower_idom_char_0) order_mult:
1.778 +  assumes pq0: "poly (p *** q) \<noteq> poly []"
1.779 +  shows "order a (p *** q) = order a p + order a q"
1.780 +proof-
1.781 +  let ?order = order
1.782 +  let ?divides = "op divides"
1.783 +  let ?poly = poly
1.784 +from pq0
1.785 +show ?thesis
1.786 +apply (cut_tac a = a and p = "pmult p q" and n = "?order a p + ?order a q" in order)
1.787 +apply (auto simp add: poly_entire simp del: pmult_Cons)
1.788 +apply (drule_tac a = a in order2)+
1.789 +apply safe
1.791 +apply (rule_tac x = "pmult qa qaa" in exI)
1.792 +apply (simp add: poly_mult mult_ac del: pmult_Cons)
1.793 +apply (drule_tac a = a in order_decomp)+
1.794 +apply safe
1.795 +apply (subgoal_tac "?divides [uminus a,one ] (pmult qa qaa) ")
1.796 +apply (simp add: poly_primes del: pmult_Cons)
1.797 +apply (auto simp add: divides_def simp del: pmult_Cons)
1.798 +apply (rule_tac x = qb in exI)
1.799 +apply (subgoal_tac "?poly (pmult (pexp [uminus a, one] (?order a p)) (pmult qa qaa)) = ?poly (pmult (pexp [uminus a, one] (?order a p)) (pmult [uminus a, one] qb))")
1.800 +apply (drule poly_mult_left_cancel [THEN iffD1], force)
1.801 +apply (subgoal_tac "?poly (pmult (pexp [uminus a, one ] (order a q)) (pmult (pexp [uminus a, one] (order a p)) (pmult qa qaa))) = ?poly (pmult (pexp [uminus a, one] (order a q)) (pmult (pexp [uminus a, one] (order a p)) (pmult [uminus a, one] qb))) ")
1.802 +apply (drule poly_mult_left_cancel [THEN iffD1], force)
1.804 +done
1.805 +qed
1.807 +lemma (in recpower_idom_char_0) order_root2: "poly p \<noteq> poly [] ==> (poly p a = 0) = (order a p \<noteq> 0)"
1.808 +by (rule order_root [THEN ssubst], auto)
1.810 +lemma (in semiring_1) pmult_one[simp]: "[1] *** p = p" by auto
1.812 +lemma (in semiring_0) poly_Nil_zero: "poly [] = poly [0]"
1.815 +lemma (in recpower_idom_char_0) rsquarefree_decomp:
1.816 +     "[| rsquarefree p; poly p a = 0 |]
1.817 +      ==> \<exists>q. (poly p = poly ([-a, 1] *** q)) & poly q a \<noteq> 0"
1.818 +apply (simp add: rsquarefree_def, safe)
1.819 +apply (frule_tac a = a in order_decomp)
1.820 +apply (drule_tac x = a in spec)
1.821 +apply (drule_tac a = a in order_root2 [symmetric])
1.822 +apply (auto simp del: pmult_Cons)
1.823 +apply (rule_tac x = q in exI, safe)
1.824 +apply (simp add: poly_mult fun_eq)
1.825 +apply (drule_tac p1 = q in poly_linear_divides [THEN iffD1])
1.826 +apply (simp add: divides_def del: pmult_Cons, safe)
1.827 +apply (drule_tac x = "[]" in spec)
1.828 +apply (auto simp add: fun_eq)
1.829 +done
1.832 +text{*Normalization of a polynomial.*}
1.834 +lemma (in semiring_0) poly_normalize[simp]: "poly (pnormalize p) = poly p"
1.835 +apply (induct "p")
1.836 +apply (auto simp add: fun_eq)
1.837 +done
1.839 +text{*The degree of a polynomial.*}
1.841 +lemma (in semiring_0) lemma_degree_zero:
1.842 +     "list_all (%c. c = 0) p \<longleftrightarrow>  pnormalize p = []"
1.843 +by (induct "p", auto)
1.845 +lemma (in idom_char_0) degree_zero:
1.846 +  assumes pN: "poly p = poly []" shows"degree p = 0"
1.847 +proof-
1.848 +  let ?pn = pnormalize
1.849 +  from pN
1.850 +  show ?thesis
1.851 +    apply (simp add: degree_def)
1.852 +    apply (case_tac "?pn p = []")
1.853 +    apply (auto simp add: poly_zero lemma_degree_zero )
1.854 +    done
1.855 +qed
1.857 +lemma (in semiring_0) pnormalize_sing: "(pnormalize [x] = [x]) \<longleftrightarrow> x \<noteq> 0" by simp
1.858 +lemma (in semiring_0) pnormalize_pair: "y \<noteq> 0 \<longleftrightarrow> (pnormalize [x, y] = [x, y])" by simp
1.859 +lemma (in semiring_0) pnormal_cons: "pnormal p \<Longrightarrow> pnormal (c#p)"
1.860 +  unfolding pnormal_def by simp
1.861 +lemma (in semiring_0) pnormal_tail: "p\<noteq>[] \<Longrightarrow> pnormal (c#p) \<Longrightarrow> pnormal p"
1.862 +  unfolding pnormal_def
1.863 +  apply (cases "pnormalize p = []", auto)
1.864 +  by (cases "c = 0", auto)
1.867 +lemma (in semiring_0) pnormal_last_nonzero: "pnormal p ==> last p \<noteq> 0"
1.868 +proof(induct p)
1.869 +  case Nil thus ?case by (simp add: pnormal_def)
1.870 +next
1.871 +  case (Cons a as) thus ?case
1.872 +    apply (simp add: pnormal_def)
1.873 +    apply (cases "pnormalize as = []", simp_all)
1.874 +    apply (cases "as = []", simp_all)
1.875 +    apply (cases "a=0", simp_all)
1.876 +    apply (cases "a=0", simp_all)
1.877 +    done
1.878 +qed
1.880 +lemma (in semiring_0) pnormal_length: "pnormal p \<Longrightarrow> 0 < length p"
1.881 +  unfolding pnormal_def length_greater_0_conv by blast
1.883 +lemma (in semiring_0) pnormal_last_length: "\<lbrakk>0 < length p ; last p \<noteq> 0\<rbrakk> \<Longrightarrow> pnormal p"
1.884 +  apply (induct p, auto)
1.885 +  apply (case_tac "p = []", auto)
1.886 +  apply (simp add: pnormal_def)
1.887 +  by (rule pnormal_cons, auto)
1.889 +lemma (in semiring_0) pnormal_id: "pnormal p \<longleftrightarrow> (0 < length p \<and> last p \<noteq> 0)"
1.890 +  using pnormal_last_length pnormal_length pnormal_last_nonzero by blast
1.892 +lemma (in idom_char_0) poly_Cons_eq: "poly (c#cs) = poly (d#ds) \<longleftrightarrow> c=d \<and> poly cs = poly ds" (is "?lhs \<longleftrightarrow> ?rhs")
1.893 +proof
1.894 +  assume eq: ?lhs
1.895 +  hence "\<And>x. poly ((c#cs) +++ -- (d#ds)) x = 0"
1.896 +    by (simp only: poly_minus poly_add ring_simps) simp
1.897 +  hence "poly ((c#cs) +++ -- (d#ds)) = poly []" by - (rule ext, simp)
1.898 +  hence "c = d \<and> list_all (\<lambda>x. x=0) ((cs +++ -- ds))"
1.899 +    unfolding poly_zero by (simp add: poly_minus_def ring_simps minus_mult_left[symmetric])
1.900 +  hence "c = d \<and> (\<forall>x. poly (cs +++ -- ds) x = 0)"
1.901 +    unfolding poly_zero[symmetric] by simp
1.902 +  thus ?rhs  apply (simp add: poly_minus poly_add ring_simps) apply (rule ext, simp) done
1.903 +next
1.904 +  assume ?rhs then show ?lhs  by -  (rule ext,simp)
1.905 +qed
1.907 +lemma (in idom_char_0) pnormalize_unique: "poly p = poly q \<Longrightarrow> pnormalize p = pnormalize q"
1.908 +proof(induct q arbitrary: p)
1.909 +  case Nil thus ?case by (simp only: poly_zero lemma_degree_zero) simp
1.910 +next
1.911 +  case (Cons c cs p)
1.912 +  thus ?case
1.913 +  proof(induct p)
1.914 +    case Nil
1.915 +    hence "poly [] = poly (c#cs)" by blast
1.916 +    then have "poly (c#cs) = poly [] " by simp
1.917 +    thus ?case by (simp only: poly_zero lemma_degree_zero) simp
1.918 +  next
1.919 +    case (Cons d ds)
1.920 +    hence eq: "poly (d # ds) = poly (c # cs)" by blast
1.921 +    hence eq': "\<And>x. poly (d # ds) x = poly (c # cs) x" by simp
1.922 +    hence "poly (d # ds) 0 = poly (c # cs) 0" by blast
1.923 +    hence dc: "d = c" by auto
1.924 +    with eq have "poly ds = poly cs"
1.925 +      unfolding  poly_Cons_eq by simp
1.926 +    with Cons.prems have "pnormalize ds = pnormalize cs" by blast
1.927 +    with dc show ?case by simp
1.928 +  qed
1.929 +qed
1.931 +lemma (in idom_char_0) degree_unique: assumes pq: "poly p = poly q"
1.932 +  shows "degree p = degree q"
1.933 +using pnormalize_unique[OF pq] unfolding degree_def by simp
1.935 +lemma (in semiring_0) pnormalize_length: "length (pnormalize p) \<le> length p" by (induct p, auto)
1.937 +lemma (in semiring_0) last_linear_mul_lemma:
1.938 +  "last ((a %* p) +++ (x#(b %* p))) = (if p=[] then x else b*last p)"
1.940 +apply (induct p arbitrary: a x b, auto)
1.941 +apply (subgoal_tac "padd (cmult aa p) (times b a # cmult b p) \<noteq> []", simp)
1.942 +apply (induct_tac p, auto)
1.943 +done
1.945 +lemma (in semiring_1) last_linear_mul: assumes p:"p\<noteq>[]" shows "last ([a,1] *** p) = last p"
1.946 +proof-
1.947 +  from p obtain c cs where cs: "p = c#cs" by (cases p, auto)
1.948 +  from cs have eq:"[a,1] *** p = (a %* (c#cs)) +++ (0#(1 %* (c#cs)))"
1.949 +    by (simp add: poly_cmult_distr)
1.950 +  show ?thesis using cs
1.951 +    unfolding eq last_linear_mul_lemma by simp
1.952 +qed
1.954 +lemma (in semiring_0) pnormalize_eq: "last p \<noteq> 0 \<Longrightarrow> pnormalize p = p"
1.955 +  apply (induct p, auto)
1.956 +  apply (case_tac p, auto)+
1.957 +  done
1.959 +lemma (in semiring_0) last_pnormalize: "pnormalize p \<noteq> [] \<Longrightarrow> last (pnormalize p) \<noteq> 0"
1.960 +  by (induct p, auto)
1.962 +lemma (in semiring_0) pnormal_degree: "last p \<noteq> 0 \<Longrightarrow> degree p = length p - 1"
1.963 +  using pnormalize_eq[of p] unfolding degree_def by simp
1.965 +lemma (in semiring_0) poly_Nil: "poly [] = (\<lambda>x. 0)" by (rule ext, simp)
1.967 +lemma (in idom_char_0) linear_mul_degree: assumes p: "poly p \<noteq> poly []"
1.968 +  shows "degree ([a,1] *** p) = degree p + 1"
1.969 +proof-
1.970 +  from p have pnz: "pnormalize p \<noteq> []"
1.971 +    unfolding poly_zero lemma_degree_zero .
1.973 +  from last_linear_mul[OF pnz, of a] last_pnormalize[OF pnz]
1.974 +  have l0: "last ([a, 1] *** pnormalize p) \<noteq> 0" by simp
1.975 +  from last_pnormalize[OF pnz] last_linear_mul[OF pnz, of a]
1.976 +    pnormal_degree[OF l0] pnormal_degree[OF last_pnormalize[OF pnz]] pnz
1.979 +  have th: "degree ([a,1] *** pnormalize p) = degree (pnormalize p) + 1"
1.980 +    by (auto simp add: poly_length_mult)
1.982 +  have eqs: "poly ([a,1] *** pnormalize p) = poly ([a,1] *** p)"
1.984 +  from degree_unique[OF eqs] th
1.985 +  show ?thesis by (simp add: degree_unique[OF poly_normalize])
1.986 +qed
1.988 +lemma (in idom_char_0) poly_entire_lemma:
1.989 +  assumes p0: "poly p \<noteq> poly []" and q0: "poly q \<noteq> poly []"
1.990 +  shows "poly (p***q) \<noteq> poly []"
1.991 +proof-
1.992 +  let ?S = "\<lambda>p. {x. poly p x = 0}"
1.993 +  have "?S (p *** q) = ?S p \<union> ?S q" by (auto simp add: poly_mult)
1.994 +  with p0 q0 show ?thesis  unfolding poly_roots_finite by auto
1.995 +qed
1.997 +lemma (in idom_char_0) poly_entire:
1.998 +  "poly (p *** q) = poly [] \<longleftrightarrow> poly p = poly [] \<or> poly q = poly []"
1.999 +using poly_entire_lemma[of p q]
1.1000 +by auto (rule ext, simp add: poly_mult)+
1.1002 +lemma (in idom_char_0) linear_pow_mul_degree:
1.1003 +  "degree([a,1] %^n *** p) = (if poly p = poly [] then 0 else degree p + n)"
1.1004 +proof(induct n arbitrary: a p)
1.1005 +  case (0 a p)
1.1006 +  {assume p: "poly p = poly []"
1.1007 +    hence ?case using degree_unique[OF p] by (simp add: degree_def)}
1.1008 +  moreover
1.1009 +  {assume p: "poly p \<noteq> poly []" hence ?case by (auto simp add: poly_Nil) }
1.1010 +  ultimately show ?case by blast
1.1011 +next
1.1012 +  case (Suc n a p)
1.1013 +  have eq: "poly ([a,1] %^(Suc n) *** p) = poly ([a,1] %^ n *** ([a,1] *** p))"
1.1016 +  note deq = degree_unique[OF eq]
1.1017 +  {assume p: "poly p = poly []"
1.1018 +    with eq have eq': "poly ([a,1] %^(Suc n) *** p) = poly []"
1.1020 +    from degree_unique[OF eq'] p have ?case by (simp add: degree_def)}
1.1021 +  moreover
1.1022 +  {assume p: "poly p \<noteq> poly []"
1.1023 +    from p have ap: "poly ([a,1] *** p) \<noteq> poly []"
1.1024 +      using poly_mult_not_eq_poly_Nil unfolding poly_entire by auto
1.1025 +    have eq: "poly ([a,1] %^(Suc n) *** p) = poly ([a,1]%^n *** ([a,1] *** p))"
1.1027 +   from ap have ap': "(poly ([a,1] *** p) = poly []) = False" by blast
1.1028 +   have  th0: "degree ([a,1]%^n *** ([a,1] *** p)) = degree ([a,1] *** p) + n"
1.1029 +     apply (simp only: Suc.hyps[of a "pmult [a,one] p"] ap')
1.1030 +     by simp
1.1032 +   from degree_unique[OF eq] ap p th0 linear_mul_degree[OF p, of a]
1.1033 +   have ?case by (auto simp del: poly.simps)}
1.1034 +  ultimately show ?case by blast
1.1035 +qed
1.1037 +lemma (in recpower_idom_char_0) order_degree:
1.1038 +  assumes p0: "poly p \<noteq> poly []"
1.1039 +  shows "order a p \<le> degree p"
1.1040 +proof-
1.1041 +  from order2[OF p0, unfolded divides_def]
1.1042 +  obtain q where q: "poly p = poly ([- a, 1]%^ (order a p) *** q)" by blast
1.1043 +  {assume "poly q = poly []"
1.1044 +    with q p0 have False by (simp add: poly_mult poly_entire)}
1.1045 +  with degree_unique[OF q, unfolded linear_pow_mul_degree]
1.1046 +  show ?thesis by auto
1.1047 +qed
1.1049 +text{*Tidier versions of finiteness of roots.*}
1.1051 +lemma (in idom_char_0) poly_roots_finite_set: "poly p \<noteq> poly [] ==> finite {x. poly p x = 0}"
1.1052 +unfolding poly_roots_finite .
1.1054 +text{*bound for polynomial.*}
1.1056 +lemma poly_mono: "abs(x) \<le> k ==> abs(poly p (x::'a::{ordered_idom})) \<le> poly (map abs p) k"
1.1057 +apply (induct "p", auto)
1.1058 +apply (rule_tac y = "abs a + abs (x * poly p x)" in order_trans)
1.1059 +apply (rule abs_triangle_ineq)
1.1060 +apply (auto intro!: mult_mono simp add: abs_mult)
1.1061 +done
1.1063 +lemma (in semiring_0) poly_Sing: "poly [c] x = c" by simp
1.1065 +end