
wenzelm@33268: (*  Title:      HOL/Decision_Procs/Polynomial_List.thy
wenzelm@33268:     Author:     Amine Chaieb
chaieb@33153: *)
chaieb@33153: 
haftmann@54219: header {* Univariate Polynomials as lists *}
chaieb@33153: 
chaieb@33153: theory Polynomial_List
haftmann@54219: imports Complex_Main
chaieb@33153: begin
chaieb@33153: 
haftmann@54219: text{* Application of polynomial as a function. *}
chaieb@33153: 
haftmann@54219: primrec (in semiring_0) poly :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a"
wenzelm@52778: where
chaieb@33153:   poly_Nil:  "poly [] x = 0"
haftmann@54219: | poly_Cons: "poly (h#t) x = h + x * poly t x"
chaieb@33153: 
chaieb@33153: 
chaieb@33153: subsection{*Arithmetic Operations on Polynomials*}
chaieb@33153: 
chaieb@33153: text{*addition*}
haftmann@54219: 
haftmann@54219: primrec (in semiring_0) padd :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "+++" 65)
wenzelm@52778: where
chaieb@33153:   padd_Nil:  "[] +++ l2 = l2"
haftmann@54219: | padd_Cons: "(h#t) +++ l2 = (if l2 = [] then h#t else (h + hd l2)#(t +++ tl l2))"
chaieb@33153: 
chaieb@33153: text{*Multiplication by a constant*}
haftmann@54219: primrec (in semiring_0) cmult :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "%*" 70) where
haftmann@39246:   cmult_Nil:  "c %* [] = []"
haftmann@54219: | cmult_Cons: "c %* (h#t) = (c * h)#(c %* t)"
chaieb@33153: 
chaieb@33153: text{*Multiplication by a polynomial*}
haftmann@54219: primrec (in semiring_0) pmult :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "***" 70)
wenzelm@52778: where
haftmann@39246:   pmult_Nil:  "[] *** l2 = []"
haftmann@54219: | pmult_Cons: "(h#t) *** l2 = (if t = [] then h %* l2
haftmann@54219:                               else (h %* l2) +++ ((0) # (t *** l2)))"
chaieb@33153: 
chaieb@33153: text{*Repeated multiplication by a polynomial*}
haftmann@54219: primrec (in semiring_0) mulexp :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a  list \<Rightarrow> 'a list" where
haftmann@39246:   mulexp_zero:  "mulexp 0 p q = q"
haftmann@39246: | mulexp_Suc:   "mulexp (Suc n) p q = p *** mulexp n p q"
chaieb@33153: 
chaieb@33153: text{*Exponential*}
haftmann@54219: primrec (in semiring_1) pexp :: "'a list \<Rightarrow> nat \<Rightarrow> 'a list"  (infixl "%^" 80) where
haftmann@39246:   pexp_0:   "p %^ 0 = [1]"
haftmann@39246: | pexp_Suc: "p %^ (Suc n) = p *** (p %^ n)"
chaieb@33153: 
chaieb@33153: text{*Quotient related value of dividing a polynomial by x + a*}
chaieb@33153: (* Useful for divisor properties in inductive proofs *)
haftmann@54219: primrec (in field) "pquot" :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list"
wenzelm@52778: where
haftmann@54219:   pquot_Nil:  "pquot [] a= []"
haftmann@54219: | pquot_Cons: "pquot (h#t) a =
haftmann@54219:     (if t = [] then [h] else (inverse(a) * (h - hd( pquot t a)))#(pquot t a))"
chaieb@33153: 
chaieb@33153: text{*normalization of polynomials (remove extra 0 coeff)*}
haftmann@54219: primrec (in semiring_0) pnormalize :: "'a list \<Rightarrow> 'a list" where
haftmann@39246:   pnormalize_Nil:  "pnormalize [] = []"
haftmann@54219: | pnormalize_Cons: "pnormalize (h#p) =
haftmann@54219:     (if pnormalize p = [] then (if h = 0 then [] else [h]) else h # pnormalize p)"
chaieb@33153: 
haftmann@54219: definition (in semiring_0) "pnormal p = ((pnormalize p = p) \<and> p \<noteq> [])"
haftmann@54219: definition (in semiring_0) "nonconstant p = (pnormal p \<and> (\<forall>x. p \<noteq> [x]))"
chaieb@33153: text{*Other definitions*}
chaieb@33153: 
haftmann@54219: definition (in ring_1) poly_minus :: "'a list \<Rightarrow> 'a list" ("-- _" [80] 80)
wenzelm@52778:   where "-- p = (- 1) %* p"
chaieb@33153: 
haftmann@54219: definition (in semiring_0) divides :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  (infixl "divides" 70)
haftmann@54219:   where "p1 divides p2 = (\<exists>q. poly p2 = poly(p1 *** q))"
haftmann@54219: 
haftmann@54219: lemma (in semiring_0) dividesI:
haftmann@54219:   "poly p2 = poly (p1 *** q) \<Longrightarrow> p1 divides p2"
haftmann@54219:   by (auto simp add: divides_def)
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) dividesE:
haftmann@54219:   assumes "p1 divides p2"
haftmann@54219:   obtains q where "poly p2 = poly (p1 *** q)"
haftmann@54219:   using assms by (auto simp add: divides_def)
chaieb@33153: 
haftmann@54219:     --{*order of a polynomial*}
haftmann@54219: definition (in ring_1) order :: "'a \<Rightarrow> 'a list \<Rightarrow> nat" where
haftmann@54219:   "order a p = (SOME n. ([-a, 1] %^ n) divides p \<and> ~ (([-a, 1] %^ (Suc n)) divides p))"
haftmann@54219: 
haftmann@54219:      --{*degree of a polynomial*}
haftmann@54219: definition (in semiring_0) degree :: "'a list \<Rightarrow> nat"
wenzelm@52778:   where "degree p = length (pnormalize p) - 1"
chaieb@33153: 
haftmann@54219:      --{*squarefree polynomials --- NB with respect to real roots only.*}
haftmann@54219: definition (in ring_1) rsquarefree :: "'a list \<Rightarrow> bool"
haftmann@54219:   where "rsquarefree p \<longleftrightarrow> poly p \<noteq> poly [] \<and> (\<forall>a. order a p = 0 \<or> order a p = 1)"
chaieb@33153: 
haftmann@54219: context semiring_0
haftmann@54219: begin
haftmann@54219: 
haftmann@54219: lemma padd_Nil2[simp]: "p +++ [] = p"
wenzelm@52778:   by (induct p) auto
chaieb@33153: 
chaieb@33153: lemma padd_Cons_Cons: "(h1 # p1) +++ (h2 # p2) = (h1 + h2) # (p1 +++ p2)"
wenzelm@52778:   by auto
chaieb@33153: 
haftmann@54219: lemma pminus_Nil: "-- [] = []"
wenzelm@52778:   by (simp add: poly_minus_def)
chaieb@33153: 
haftmann@54219: lemma pmult_singleton: "[h1] *** p1 = h1 %* p1" by simp
haftmann@54219: 
haftmann@54219: end
chaieb@33153: 
haftmann@54219: lemma (in semiring_1) poly_ident_mult[simp]: "1 %* t = t" by (induct t) auto
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_simple_add_Cons[simp]: "[a] +++ ((0)#t) = (a#t)"
wenzelm@52778:   by simp
chaieb@33153: 
chaieb@33153: text{*Handy general properties*}
chaieb@33153: 
haftmann@54219: lemma (in comm_semiring_0) padd_commut: "b +++ a = a +++ b"
haftmann@54219: proof (induct b arbitrary: a)
haftmann@54219:   case Nil
haftmann@54219:   thus ?case by auto
haftmann@54219: next
haftmann@54219:   case (Cons b bs a)
haftmann@57512:   thus ?case by (cases a) (simp_all add: add.commute)
haftmann@54219: qed
haftmann@54219: 
haftmann@54219: lemma (in comm_semiring_0) padd_assoc: "\<forall>b c. (a +++ b) +++ c = a +++ (b +++ c)"
haftmann@54219:   apply (induct a)
haftmann@54219:   apply (simp, clarify)
haftmann@57514:   apply (case_tac b, simp_all add: ac_simps)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_cmult_distr: "a %* ( p +++ q) = (a %* p +++ a %* q)"
haftmann@54219:   apply (induct p arbitrary: q)
wenzelm@52881:   apply simp
haftmann@54219:   apply (case_tac q, simp_all add: distrib_left)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in ring_1) pmult_by_x[simp]: "[0, 1] *** t = ((0)#t)"
haftmann@54219:   apply (induct t)
wenzelm@52778:   apply simp
haftmann@54219:   apply (auto simp add: padd_commut)
haftmann@54219:   apply (case_tac t, auto)
wenzelm@52778:   done
chaieb@33153: 
chaieb@33153: text{*properties of evaluation of polynomials.*}
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_add: "poly (p1 +++ p2) x = poly p1 x + poly p2 x"
haftmann@54219: proof(induct p1 arbitrary: p2)
haftmann@54219:   case Nil
haftmann@54219:   thus ?case by simp
haftmann@54219: next
haftmann@54219:   case (Cons a as p2)
haftmann@54219:   thus ?case
haftmann@57514:     by (cases p2) (simp_all  add: ac_simps distrib_left)
haftmann@54219: qed
chaieb@33153: 
haftmann@54219: lemma (in comm_semiring_0) poly_cmult: "poly (c %* p) x = c * poly p x"
wenzelm@52881:   apply (induct p)
haftmann@54219:   apply (case_tac [2] "x = zero")
haftmann@57514:   apply (auto simp add: distrib_left ac_simps)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in comm_semiring_0) poly_cmult_map: "poly (map (op * c) p) x = c*poly p x"
haftmann@57514:   by (induct p) (auto simp add: distrib_left ac_simps)
chaieb@33153: 
haftmann@54219: lemma (in comm_ring_1) poly_minus: "poly (-- p) x = - (poly p x)"
haftmann@54219:   apply (simp add: poly_minus_def)
haftmann@54219:   apply (auto simp add: poly_cmult)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in comm_semiring_0) poly_mult: "poly (p1 *** p2) x = poly p1 x * poly p2 x"
haftmann@54219: proof (induct p1 arbitrary: p2)
haftmann@54219:   case Nil
haftmann@54219:   thus ?case by simp
haftmann@54219: next
haftmann@54219:   case (Cons a as p2)
haftmann@54219:   thus ?case by (cases as)
haftmann@57514:     (simp_all add: poly_cmult poly_add distrib_right distrib_left ac_simps)
haftmann@54219: qed
haftmann@54219: 
haftmann@54219: class idom_char_0 = idom + ring_char_0
haftmann@54219: 
haftmann@54219: subclass (in field_char_0) idom_char_0 ..
haftmann@54219: 
haftmann@54219: lemma (in comm_ring_1) poly_exp: "poly (p %^ n) x = (poly p x) ^ n"
wenzelm@52881:   by (induct n) (auto simp add: poly_cmult poly_mult)
chaieb@33153: 
chaieb@33153: text{*More Polynomial Evaluation Lemmas*}
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_add_rzero[simp]: "poly (a +++ []) x = poly a x"
wenzelm@52778:   by simp
chaieb@33153: 
haftmann@54219: lemma (in comm_semiring_0) poly_mult_assoc: "poly ((a *** b) *** c) x = poly (a *** (b *** c)) x"
haftmann@57512:   by (simp add: poly_mult mult.assoc)
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_mult_Nil2[simp]: "poly (p *** []) x = 0"
wenzelm@52881:   by (induct p) auto
chaieb@33153: 
haftmann@54219: lemma (in comm_semiring_1) poly_exp_add: "poly (p %^ (n + d)) x = poly( p %^ n *** p %^ d) x"
haftmann@57512:   by (induct n) (auto simp add: poly_mult mult.assoc)
chaieb@33153: 
chaieb@33153: subsection{*Key Property: if @{term "f(a) = 0"} then @{term "(x - a)"} divides
chaieb@33153:  @{term "p(x)"} *}
chaieb@33153: 
haftmann@54219: lemma (in comm_ring_1) lemma_poly_linear_rem: "\<forall>h. \<exists>q r. h#t = [r] +++ [-a, 1] *** q"
haftmann@54219: proof(induct t)
haftmann@54219:   case Nil
haftmann@54219:   { fix h have "[h] = [h] +++ [- a, 1] *** []" by simp }
haftmann@54219:   thus ?case by blast
haftmann@54219: next
haftmann@54219:   case (Cons  x xs)
haftmann@54219:   { fix h
haftmann@54219:     from Cons.hyps[rule_format, of x]
haftmann@54219:     obtain q r where qr: "x#xs = [r] +++ [- a, 1] *** q" by blast
haftmann@54219:     have "h#x#xs = [a*r + h] +++ [-a, 1] *** (r#q)"
haftmann@54219:       using qr by (cases q) (simp_all add: algebra_simps)
haftmann@54219:     hence "\<exists>q r. h#x#xs = [r] +++ [-a, 1] *** q" by blast}
haftmann@54219:   thus ?case by blast
haftmann@54219: qed
haftmann@54219: 
haftmann@54219: lemma (in comm_ring_1) poly_linear_rem: "\<exists>q r. h#t = [r] +++ [-a, 1] *** q"
haftmann@54219:   using lemma_poly_linear_rem [where t = t and a = a] by auto
haftmann@54219: 
chaieb@33153: 
haftmann@54219: lemma (in comm_ring_1) poly_linear_divides: "(poly p a = 0) = ((p = []) | (\<exists>q. p = [-a, 1] *** q))"
haftmann@54219: proof -
haftmann@54219:   { assume p: "p = []" hence ?thesis by simp }
haftmann@54219:   moreover
haftmann@54219:   {
haftmann@54219:     fix x xs assume p: "p = x#xs"
haftmann@54219:     {
haftmann@54219:       fix q assume "p = [-a, 1] *** q"
haftmann@54219:       hence "poly p a = 0" by (simp add: poly_add poly_cmult)
haftmann@54219:     }
haftmann@54219:     moreover
haftmann@54219:     { assume p0: "poly p a = 0"
haftmann@54219:       from poly_linear_rem[of x xs a] obtain q r
haftmann@54219:       where qr: "x#xs = [r] +++ [- a, 1] *** q" by blast
haftmann@54219:       have "r = 0" using p0 by (simp only: p qr poly_mult poly_add) simp
haftmann@54219:       hence "\<exists>q. p = [- a, 1] *** q"
haftmann@54219:         using p qr
haftmann@54219:         apply -
haftmann@54219:         apply (rule exI[where x=q])
haftmann@54219:         apply auto
haftmann@54219:         apply (cases q)
haftmann@54219:         apply auto
haftmann@54219:         done
haftmann@54219:     }
haftmann@54219:     ultimately have ?thesis using p by blast
haftmann@54219:   }
haftmann@54219:   ultimately show ?thesis by (cases p) auto
haftmann@54219: qed
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) lemma_poly_length_mult[simp]: "\<forall>h k a. length (k %* p +++  (h # (a %* p))) = Suc (length p)"
haftmann@54219:   by (induct p) auto
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) lemma_poly_length_mult2[simp]: "\<forall>h k. length (k %* p +++  (h # p)) = Suc (length p)"
haftmann@54219:   by (induct p) auto
chaieb@33153: 
haftmann@54219: lemma (in ring_1) poly_length_mult[simp]: "length([-a,1] *** q) = Suc (length q)"
wenzelm@52778:   by auto
chaieb@33153: 
chaieb@33153: subsection{*Polynomial length*}
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_cmult_length[simp]: "length (a %* p) = length p"
wenzelm@52778:   by (induct p) auto
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_add_length: "length (p1 +++ p2) = max (length p1) (length p2)"
haftmann@54219:   by (induct p1 arbitrary: p2) (simp_all, arith)
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_root_mult_length[simp]: "length([a,b] *** p) = Suc (length p)"
haftmann@54219:   by (simp add: poly_add_length)
chaieb@33153: 
haftmann@54219: lemma (in idom) poly_mult_not_eq_poly_Nil[simp]:
haftmann@54219:   "poly (p *** q) x \<noteq> poly [] x \<longleftrightarrow> poly p x \<noteq> poly [] x \<and> poly q x \<noteq> poly [] x"
wenzelm@52778:   by (auto simp add: poly_mult)
chaieb@33153: 
haftmann@54219: lemma (in idom) poly_mult_eq_zero_disj: "poly (p *** q) x = 0 \<longleftrightarrow> poly p x = 0 \<or> poly q x = 0"
wenzelm@52778:   by (auto simp add: poly_mult)
chaieb@33153: 
chaieb@33153: text{*Normalisation Properties*}
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_normalized_nil: "(pnormalize p = []) --> (poly p x = 0)"
wenzelm@52778:   by (induct p) auto
chaieb@33153: 
chaieb@33153: text{*A nontrivial polynomial of degree n has no more than n roots*}
haftmann@54219: lemma (in idom) poly_roots_index_lemma:
haftmann@54219:    assumes p: "poly p x \<noteq> poly [] x" and n: "length p = n"
haftmann@54219:   shows "\<exists>i. \<forall>x. poly p x = 0 \<longrightarrow> (\<exists>m\<le>n. x = i m)"
haftmann@54219:   using p n
haftmann@54219: proof (induct n arbitrary: p x)
haftmann@54219:   case 0
haftmann@54219:   thus ?case by simp
haftmann@54219: next
haftmann@54219:   case (Suc n p x)
haftmann@54219:   {
haftmann@54219:     assume C: "\<And>i. \<exists>x. poly p x = 0 \<and> (\<forall>m\<le>Suc n. x \<noteq> i m)"
haftmann@54219:     from Suc.prems have p0: "poly p x \<noteq> 0" "p\<noteq> []" by auto
haftmann@54219:     from p0(1)[unfolded poly_linear_divides[of p x]]
haftmann@54219:     have "\<forall>q. p \<noteq> [- x, 1] *** q" by blast
haftmann@54219:     from C obtain a where a: "poly p a = 0" by blast
haftmann@54219:     from a[unfolded poly_linear_divides[of p a]] p0(2)
haftmann@54219:     obtain q where q: "p = [-a, 1] *** q" by blast
haftmann@54219:     have lg: "length q = n" using q Suc.prems(2) by simp
haftmann@54219:     from q p0 have qx: "poly q x \<noteq> poly [] x"
haftmann@54219:       by (auto simp add: poly_mult poly_add poly_cmult)
haftmann@54219:     from Suc.hyps[OF qx lg] obtain i where
haftmann@54219:       i: "\<forall>x. poly q x = 0 \<longrightarrow> (\<exists>m\<le>n. x = i m)" by blast
haftmann@54219:     let ?i = "\<lambda>m. if m = Suc n then a else i m"
haftmann@54219:     from C[of ?i] obtain y where y: "poly p y = 0" "\<forall>m\<le> Suc n. y \<noteq> ?i m"
haftmann@54219:       by blast
haftmann@54219:     from y have "y = a \<or> poly q y = 0"
haftmann@54219:       by (simp only: q poly_mult_eq_zero_disj poly_add) (simp add: algebra_simps)
haftmann@54219:     with i[rule_format, of y] y(1) y(2) have False
haftmann@54219:       apply auto
haftmann@54219:       apply (erule_tac x = "m" in allE)
haftmann@54219:       apply auto
haftmann@54219:       done
haftmann@54219:   }
haftmann@54219:   thus ?case by blast
haftmann@54219: qed
chaieb@33153: 
chaieb@33153: 
haftmann@54219: lemma (in idom) poly_roots_index_length:
haftmann@54219:   "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)"
haftmann@54219:   by (blast intro: poly_roots_index_lemma)
chaieb@33153: 
haftmann@54219: lemma (in idom) poly_roots_finite_lemma1:
haftmann@54219:   "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)"
haftmann@54219:   apply (drule poly_roots_index_length, safe)
wenzelm@52778:   apply (rule_tac x = "Suc (length p)" in exI)
wenzelm@52778:   apply (rule_tac x = i in exI)
wenzelm@52778:   apply (simp add: less_Suc_eq_le)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in idom) idom_finite_lemma:
haftmann@54219:   assumes P: "\<forall>x. P x --> (\<exists>n. n < length j \<and> x = j!n)"
haftmann@54219:   shows "finite {x. P x}"
wenzelm@52778: proof -
chaieb@33153:   let ?M = "{x. P x}"
chaieb@33153:   let ?N = "set j"
haftmann@54219:   have "?M \<subseteq> ?N" using P by auto
haftmann@54219:   thus ?thesis using finite_subset by auto
chaieb@33153: qed
chaieb@33153: 
haftmann@54219: lemma (in idom) poly_roots_finite_lemma2:
haftmann@54219:   "poly p x \<noteq> poly [] x \<Longrightarrow> \<exists>i. \<forall>x. poly p x = 0 \<longrightarrow> x \<in> set i"
haftmann@54219:   apply (drule poly_roots_index_length, safe)
haftmann@54219:   apply (rule_tac x="map (\<lambda>n. i n) [0 ..< Suc (length p)]" in exI)
haftmann@54219:   apply (auto simp add: image_iff)
haftmann@54219:   apply (erule_tac x="x" in allE, clarsimp)
haftmann@54219:   apply (case_tac "n = length p")
haftmann@54219:   apply (auto simp add: order_le_less)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in ring_char_0) UNIV_ring_char_0_infinte: "\<not> (finite (UNIV:: 'a set))"
haftmann@54219: proof
haftmann@54219:   assume F: "finite (UNIV :: 'a set)"
haftmann@54219:   have "finite (UNIV :: nat set)"
haftmann@54219:   proof (rule finite_imageD)
haftmann@54219:     have "of_nat ` UNIV \<subseteq> UNIV" by simp
haftmann@54219:     then show "finite (of_nat ` UNIV :: 'a set)" using F by (rule finite_subset)
haftmann@54219:     show "inj (of_nat :: nat \<Rightarrow> 'a)" by (simp add: inj_on_def)
haftmann@54219:   qed
haftmann@54219:   with infinite_UNIV_nat show False ..
chaieb@33153: qed
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) poly_roots_finite: "poly p \<noteq> poly [] \<longleftrightarrow> finite {x. poly p x = 0}"
chaieb@33153: proof
haftmann@54219:   assume H: "poly p \<noteq> poly []"
haftmann@54219:   show "finite {x. poly p x = (0::'a)}"
haftmann@54219:     using H
chaieb@33153:     apply -
haftmann@54219:     apply (erule contrapos_np, rule ext)
chaieb@33153:     apply (rule ccontr)
haftmann@54219:     apply (clarify dest!: poly_roots_finite_lemma2)
chaieb@33153:     using finite_subset
wenzelm@52778:   proof -
chaieb@33153:     fix x i
wenzelm@52778:     assume F: "\<not> finite {x. poly p x = (0\<Colon>'a)}"
chaieb@33153:       and P: "\<forall>x. poly p x = (0\<Colon>'a) \<longrightarrow> x \<in> set i"
chaieb@33153:     let ?M= "{x. poly p x = (0\<Colon>'a)}"
chaieb@33153:     from P have "?M \<subseteq> set i" by auto
chaieb@33153:     with finite_subset F show False by auto
chaieb@33153:   qed
chaieb@33153: next
haftmann@54219:   assume F: "finite {x. poly p x = (0\<Colon>'a)}"
haftmann@54219:   show "poly p \<noteq> poly []" using F UNIV_ring_char_0_infinte by auto
chaieb@33153: qed
chaieb@33153: 
chaieb@33153: text{*Entirety and Cancellation for polynomials*}
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) poly_entire_lemma2:
haftmann@54219:   assumes p0: "poly p \<noteq> poly []"
haftmann@54219:     and q0: "poly q \<noteq> poly []"
haftmann@54219:   shows "poly (p***q) \<noteq> poly []"
haftmann@54219: proof -
haftmann@54219:   let ?S = "\<lambda>p. {x. poly p x = 0}"
haftmann@54219:   have "?S (p *** q) = ?S p \<union> ?S q" by (auto simp add: poly_mult)
haftmann@54219:   with p0 q0 show ?thesis  unfolding poly_roots_finite by auto
haftmann@54219: qed
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) poly_entire:
haftmann@54219:   "poly (p *** q) = poly [] \<longleftrightarrow> poly p = poly [] \<or> poly q = poly []"
haftmann@54219:   using poly_entire_lemma2[of p q]
haftmann@54219:   by (auto simp add: fun_eq_iff poly_mult)
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) poly_entire_neg:
haftmann@54219:   "poly (p *** q) \<noteq> poly [] \<longleftrightarrow> poly p \<noteq> poly [] \<and> poly q \<noteq> poly []"
wenzelm@52778:   by (simp add: poly_entire)
chaieb@33153: 
wenzelm@52778: lemma fun_eq: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
wenzelm@52778:   by auto
chaieb@33153: 
haftmann@54219: lemma (in comm_ring_1) poly_add_minus_zero_iff:
haftmann@54219:   "poly (p +++ -- q) = poly [] \<longleftrightarrow> poly p = poly q"
haftmann@54219:   by (auto simp add: algebra_simps poly_add poly_minus_def fun_eq poly_cmult)
chaieb@33153: 
haftmann@54219: lemma (in comm_ring_1) poly_add_minus_mult_eq:
haftmann@54219:   "poly (p *** q +++ --(p *** r)) = poly (p *** (q +++ -- r))"
haftmann@54230:   by (auto simp add: poly_add poly_minus_def fun_eq poly_mult poly_cmult algebra_simps)
chaieb@33153: 
haftmann@54219: subclass (in idom_char_0) comm_ring_1 ..
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) poly_mult_left_cancel:
haftmann@54219:   "poly (p *** q) = poly (p *** r) \<longleftrightarrow> poly p = poly [] \<or> poly q = poly r"
haftmann@54219: proof -
haftmann@54219:   have "poly (p *** q) = poly (p *** r) \<longleftrightarrow> poly (p *** q +++ -- (p *** r)) = poly []"
haftmann@54219:     by (simp only: poly_add_minus_zero_iff)
haftmann@54219:   also have "\<dots> \<longleftrightarrow> poly p = poly [] \<or> poly q = poly r"
haftmann@54219:     by (auto intro: simp add: poly_add_minus_mult_eq poly_entire poly_add_minus_zero_iff)
haftmann@54219:   finally show ?thesis .
haftmann@54219: qed
haftmann@54219: 
haftmann@54219: lemma (in idom) poly_exp_eq_zero[simp]:
haftmann@54219:   "poly (p %^ n) = poly [] \<longleftrightarrow> poly p = poly [] \<and> n \<noteq> 0"
wenzelm@52778:   apply (simp only: fun_eq add: HOL.all_simps [symmetric])
wenzelm@52778:   apply (rule arg_cong [where f = All])
wenzelm@52778:   apply (rule ext)
haftmann@54219:   apply (induct n)
haftmann@54219:   apply (auto simp add: poly_exp poly_mult)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in comm_ring_1) poly_prime_eq_zero[simp]: "poly [a,1] \<noteq> poly []"
wenzelm@52778:   apply (simp add: fun_eq)
haftmann@54219:   apply (rule_tac x = "minus one a" in exI)
haftmann@57512:   apply (simp add: add.commute [of a])
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in idom) poly_exp_prime_eq_zero: "poly ([a, 1] %^ n) \<noteq> poly []"
wenzelm@52778:   by auto
chaieb@33153: 
chaieb@33153: text{*A more constructive notion of polynomials being trivial*}
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) poly_zero_lemma': "poly (h # t) = poly [] \<Longrightarrow> h = 0 \<and> poly t = poly []"
wenzelm@52778:   apply (simp add: fun_eq)
haftmann@54219:   apply (case_tac "h = zero")
haftmann@54219:   apply (drule_tac [2] x = zero in spec, auto)
haftmann@54219:   apply (cases "poly t = poly []", simp)
wenzelm@52778: proof -
chaieb@33153:   fix x
haftmann@54219:   assume H: "\<forall>x. x = (0\<Colon>'a) \<or> poly t x = (0\<Colon>'a)"
haftmann@54219:     and pnz: "poly t \<noteq> poly []"
chaieb@33153:   let ?S = "{x. poly t x = 0}"
chaieb@33153:   from H have "\<forall>x. x \<noteq>0 \<longrightarrow> poly t x = 0" by blast
chaieb@33153:   hence th: "?S \<supseteq> UNIV - {0}" by auto
chaieb@33153:   from poly_roots_finite pnz have th': "finite ?S" by blast
haftmann@54219:   from finite_subset[OF th th'] UNIV_ring_char_0_infinte show "poly t x = (0\<Colon>'a)"
haftmann@54219:     by simp
wenzelm@52778: qed
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) poly_zero: "(poly p = poly []) = list_all (%c. c = 0) p"
wenzelm@52778:   apply (induct p)
wenzelm@52778:   apply simp
wenzelm@52778:   apply (rule iffI)
haftmann@54219:   apply (drule poly_zero_lemma', auto)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) poly_0: "list_all (\<lambda>c. c = 0) p \<Longrightarrow> poly p x = 0"
haftmann@54219:   unfolding poly_zero[symmetric] by simp
haftmann@54219: 
haftmann@54219: 
chaieb@33153: 
chaieb@33153: text{*Basics of divisibility.*}
chaieb@33153: 
haftmann@54219: lemma (in idom) poly_primes:
haftmann@54219:   "[a, 1] divides (p *** q) \<longleftrightarrow> [a, 1] divides p \<or> [a, 1] divides q"
wenzelm@52778:   apply (auto simp add: divides_def fun_eq poly_mult poly_add poly_cmult distrib_right [symmetric])
haftmann@54219:   apply (drule_tac x = "uminus a" in spec)
haftmann@54219:   apply (simp add: poly_linear_divides poly_add poly_cmult distrib_right [symmetric])
haftmann@54219:   apply (cases "p = []")
haftmann@54219:   apply (rule exI[where x="[]"])
haftmann@54219:   apply simp
haftmann@54219:   apply (cases "q = []")
haftmann@54219:   apply (erule allE[where x="[]"], simp)
haftmann@54219: 
haftmann@54219:   apply clarsimp
haftmann@54219:   apply (cases "\<exists>q\<Colon>'a list. p = a %* q +++ ((0\<Colon>'a) # q)")
haftmann@54219:   apply (clarsimp simp add: poly_add poly_cmult)
haftmann@54219:   apply (rule_tac x="qa" in exI)
haftmann@54219:   apply (simp add: distrib_right [symmetric])
haftmann@54219:   apply clarsimp
haftmann@54219: 
wenzelm@52778:   apply (auto simp add: poly_linear_divides poly_add poly_cmult distrib_right [symmetric])
haftmann@54219:   apply (rule_tac x = "pmult qa q" in exI)
haftmann@54219:   apply (rule_tac [2] x = "pmult p qa" in exI)
haftmann@57514:   apply (auto simp add: poly_add poly_mult poly_cmult ac_simps)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in comm_semiring_1) poly_divides_refl[simp]: "p divides p"
wenzelm@52778:   apply (simp add: divides_def)
haftmann@54219:   apply (rule_tac x = "[one]" in exI)
wenzelm@52778:   apply (auto simp add: poly_mult fun_eq)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in comm_semiring_1) poly_divides_trans: "p divides q \<Longrightarrow> q divides r \<Longrightarrow> p divides r"
haftmann@54219:   apply (simp add: divides_def, safe)
haftmann@54219:   apply (rule_tac x = "pmult qa qaa" in exI)
haftmann@57512:   apply (auto simp add: poly_mult fun_eq mult.assoc)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in comm_semiring_1) poly_divides_exp: "m \<le> n \<Longrightarrow> (p %^ m) divides (p %^ n)"
wenzelm@52778:   apply (auto simp add: le_iff_add)
wenzelm@52778:   apply (induct_tac k)
wenzelm@52778:   apply (rule_tac [2] poly_divides_trans)
wenzelm@52778:   apply (auto simp add: divides_def)
wenzelm@52778:   apply (rule_tac x = p in exI)
haftmann@57514:   apply (auto simp add: poly_mult fun_eq ac_simps)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in comm_semiring_1) poly_exp_divides:
haftmann@54219:   "(p %^ n) divides q \<Longrightarrow> m \<le> n \<Longrightarrow> (p %^ m) divides q"
wenzelm@52778:   by (blast intro: poly_divides_exp poly_divides_trans)
chaieb@33153: 
haftmann@54219: lemma (in comm_semiring_0) poly_divides_add:
haftmann@54219:   "p divides q \<Longrightarrow> p divides r \<Longrightarrow> p divides (q +++ r)"
haftmann@54219:   apply (simp add: divides_def, auto)
haftmann@54219:   apply (rule_tac x = "padd qa qaa" in exI)
wenzelm@52778:   apply (auto simp add: poly_add fun_eq poly_mult distrib_left)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in comm_ring_1) poly_divides_diff:
haftmann@54219:   "p divides q \<Longrightarrow> p divides (q +++ r) \<Longrightarrow> p divides r"
haftmann@54219:   apply (simp add: divides_def, auto)
haftmann@54219:   apply (rule_tac x = "padd qaa (poly_minus qa)" in exI)
wenzelm@52778:   apply (auto simp add: poly_add fun_eq poly_mult poly_minus algebra_simps)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in comm_ring_1) poly_divides_diff2:
haftmann@54219:   "p divides r \<Longrightarrow> p divides (q +++ r) \<Longrightarrow> p divides q"
wenzelm@52778:   apply (erule poly_divides_diff)
haftmann@57514:   apply (auto simp add: poly_add fun_eq poly_mult divides_def ac_simps)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_divides_zero: "poly p = poly [] \<Longrightarrow> q divides p"
wenzelm@52778:   apply (simp add: divides_def)
haftmann@54219:   apply (rule exI[where x="[]"])
wenzelm@52778:   apply (auto simp add: fun_eq poly_mult)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_divides_zero2 [simp]: "q divides []"
wenzelm@52778:   apply (simp add: divides_def)
wenzelm@52778:   apply (rule_tac x = "[]" in exI)
wenzelm@52778:   apply (auto simp add: fun_eq)
wenzelm@52778:   done
chaieb@33153: 
chaieb@33153: text{*At last, we can consider the order of a root.*}
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) poly_order_exists_lemma:
haftmann@54219:   assumes lp: "length p = d"
haftmann@54219:     and p: "poly p \<noteq> poly []"
haftmann@54219:   shows "\<exists>n q. p = mulexp n [-a, 1] q \<and> poly q a \<noteq> 0"
haftmann@54219:   using lp p
haftmann@54219: proof (induct d arbitrary: p)
haftmann@54219:   case 0
haftmann@54219:   thus ?case by simp
haftmann@54219: next
haftmann@54219:   case (Suc n p)
haftmann@54219:   show ?case
haftmann@54219:   proof (cases "poly p a = 0")
haftmann@54219:     case True
haftmann@54219:     from Suc.prems have h: "length p = Suc n" "poly p \<noteq> poly []" by auto
haftmann@54219:     hence pN: "p \<noteq> []" by auto
haftmann@54219:     from True[unfolded poly_linear_divides] pN obtain q where q: "p = [-a, 1] *** q"
haftmann@54219:       by blast
haftmann@54219:     from q h True have qh: "length q = n" "poly q \<noteq> poly []"
haftmann@54219:       apply -
haftmann@54219:       apply simp
haftmann@54219:       apply (simp only: fun_eq)
haftmann@54219:       apply (rule ccontr)
haftmann@54219:       apply (simp add: fun_eq poly_add poly_cmult)
haftmann@54219:       done
haftmann@54219:     from Suc.hyps[OF qh] obtain m r where mr: "q = mulexp m [-a,1] r" "poly r a \<noteq> 0"
haftmann@54219:       by blast
haftmann@54219:     from mr q have "p = mulexp (Suc m) [-a,1] r \<and> poly r a \<noteq> 0" by simp
haftmann@54219:     then show ?thesis by blast
haftmann@54219:   next
haftmann@54219:     case False
haftmann@54219:     then show ?thesis
haftmann@54219:       using Suc.prems
haftmann@54219:       apply simp
haftmann@54219:       apply (rule exI[where x="0::nat"])
haftmann@54219:       apply simp
haftmann@54219:       done
haftmann@54219:   qed
haftmann@54219: qed
haftmann@54219: 
haftmann@54219: 
haftmann@54219: lemma (in comm_semiring_1) poly_mulexp: "poly (mulexp n p q) x = (poly p x) ^ n * poly q x"
haftmann@57514:   by (induct n) (auto simp add: poly_mult ac_simps)
haftmann@54219: 
haftmann@54219: lemma (in comm_semiring_1) divides_left_mult:
haftmann@54219:   assumes d:"(p***q) divides r" shows "p divides r \<and> q divides r"
haftmann@54219: proof-
haftmann@54219:   from d obtain t where r:"poly r = poly (p***q *** t)"
haftmann@54219:     unfolding divides_def by blast
haftmann@54219:   hence "poly r = poly (p *** (q *** t))"
haftmann@57514:     "poly r = poly (q *** (p***t))" by(auto simp add: fun_eq poly_mult ac_simps)
haftmann@54219:   thus ?thesis unfolding divides_def by blast
haftmann@54219: qed
haftmann@54219: 
chaieb@33153: 
chaieb@33153: (* FIXME: Tidy up *)
haftmann@54219: 
haftmann@54219: lemma (in semiring_1) zero_power_iff: "0 ^ n = (if n = 0 then 1 else 0)"
haftmann@54219:   by (induct n) simp_all
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) poly_order_exists:
haftmann@54219:   assumes "length p = d" and "poly p \<noteq> poly []"
haftmann@54219:   shows "\<exists>n. [- a, 1] %^ n divides p \<and> \<not> [- a, 1] %^ Suc n divides p"
haftmann@54219: proof -
haftmann@54219:   from assms have "\<exists>n q. p = mulexp n [- a, 1] q \<and> poly q a \<noteq> 0"
haftmann@54219:     by (rule poly_order_exists_lemma)
haftmann@54219:   then obtain n q where p: "p = mulexp n [- a, 1] q" and "poly q a \<noteq> 0" by blast
haftmann@54219:   have "[- a, 1] %^ n divides mulexp n [- a, 1] q"
haftmann@54219:   proof (rule dividesI)
haftmann@54219:     show "poly (mulexp n [- a, 1] q) = poly ([- a, 1] %^ n *** q)"
haftmann@54230:       by (induct n) (simp_all add: poly_add poly_cmult poly_mult algebra_simps)
haftmann@54219:   qed
haftmann@54219:   moreover have "\<not> [- a, 1] %^ Suc n divides mulexp n [- a, 1] q"
haftmann@54219:   proof
haftmann@54219:     assume "[- a, 1] %^ Suc n divides mulexp n [- a, 1] q"
haftmann@54219:     then obtain m where "poly (mulexp n [- a, 1] q) = poly ([- a, 1] %^ Suc n *** m)"
haftmann@54219:       by (rule dividesE)
haftmann@54219:     moreover have "poly (mulexp n [- a, 1] q) \<noteq> poly ([- a, 1] %^ Suc n *** m)"
haftmann@54219:     proof (induct n)
haftmann@54219:       case 0 show ?case
haftmann@54219:       proof (rule ccontr)
haftmann@54219:         assume "\<not> poly (mulexp 0 [- a, 1] q) \<noteq> poly ([- a, 1] %^ Suc 0 *** m)"
haftmann@54219:         then have "poly q a = 0"
haftmann@54219:           by (simp add: poly_add poly_cmult)
haftmann@54219:         with `poly q a \<noteq> 0` show False by simp
haftmann@54219:       qed
haftmann@54219:     next
haftmann@54219:       case (Suc n) show ?case
haftmann@54219:         by (rule pexp_Suc [THEN ssubst], rule ccontr)
haftmann@54219:           (simp add: poly_mult_left_cancel poly_mult_assoc Suc del: pmult_Cons pexp_Suc)
haftmann@54219:     qed
haftmann@54219:     ultimately show False by simp
haftmann@54219:   qed
haftmann@54219:   ultimately show ?thesis by (auto simp add: p)
haftmann@54219: qed
chaieb@33153: 
haftmann@54219: lemma (in semiring_1) poly_one_divides[simp]: "[1] divides p"
haftmann@54219:   by (auto simp add: divides_def)
haftmann@54219: 
haftmann@54219: lemma (in idom_char_0) poly_order:
haftmann@54219:   "poly p \<noteq> poly [] \<Longrightarrow> \<exists>!n. ([-a, 1] %^ n) divides p \<and> \<not> (([-a, 1] %^ Suc n) divides p)"
wenzelm@52778:   apply (auto intro: poly_order_exists simp add: less_linear simp del: pmult_Cons pexp_Suc)
wenzelm@52778:   apply (cut_tac x = y and y = n in less_linear)
wenzelm@52778:   apply (drule_tac m = n in poly_exp_divides)
wenzelm@52778:   apply (auto dest: Suc_le_eq [THEN iffD2, THEN [2] poly_exp_divides]
haftmann@54219:               simp del: pmult_Cons pexp_Suc)
wenzelm@52778:   done
chaieb@33153: 
chaieb@33153: text{*Order*}
chaieb@33153: 
haftmann@54219: lemma some1_equalityD: "n = (SOME n. P n) \<Longrightarrow> \<exists>!n. P n \<Longrightarrow> P n"
wenzelm@52778:   by (blast intro: someI2)
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) order:
haftmann@54219:       "(([-a, 1] %^ n) divides p \<and>
haftmann@54219:         ~(([-a, 1] %^ (Suc n)) divides p)) =
haftmann@54219:         ((n = order a p) \<and> ~(poly p = poly []))"
wenzelm@52778:   apply (unfold order_def)
wenzelm@52778:   apply (rule iffI)
wenzelm@52778:   apply (blast dest: poly_divides_zero intro!: some1_equality [symmetric] poly_order)
wenzelm@52778:   apply (blast intro!: poly_order [THEN [2] some1_equalityD])
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) order2:
haftmann@54219:   "poly p \<noteq> poly [] \<Longrightarrow>
haftmann@54219:     ([-a, 1] %^ (order a p)) divides p \<and> \<not> (([-a, 1] %^ (Suc (order a p))) divides p)"
wenzelm@52778:   by (simp add: order del: pexp_Suc)
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) order_unique:
haftmann@54219:   "poly p \<noteq> poly [] \<Longrightarrow> ([-a, 1] %^ n) divides p \<Longrightarrow> ~(([-a, 1] %^ (Suc n)) divides p) \<Longrightarrow>
haftmann@54219:     n = order a p"
wenzelm@52778:   using order [of a n p] by auto
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) order_unique_lemma:
haftmann@54219:   "poly p \<noteq> poly [] \<and> ([-a, 1] %^ n) divides p \<and> ~(([-a, 1] %^ (Suc n)) divides p) \<Longrightarrow>
wenzelm@52881:     n = order a p"
wenzelm@52778:   by (blast intro: order_unique)
chaieb@33153: 
haftmann@54219: lemma (in ring_1) order_poly: "poly p = poly q \<Longrightarrow> order a p = order a q"
wenzelm@52778:   by (auto simp add: fun_eq divides_def poly_mult order_def)
chaieb@33153: 
haftmann@54219: lemma (in semiring_1) pexp_one[simp]: "p %^ (Suc 0) = p"
haftmann@54219:   by (induct "p") auto
haftmann@54219: 
haftmann@54219: lemma (in comm_ring_1) lemma_order_root:
haftmann@54219:   "0 < n \<and> [- a, 1] %^ n divides p \<and> ~ [- a, 1] %^ (Suc n) divides p \<Longrightarrow> poly p a = 0"
haftmann@54219:   by (induct n arbitrary: a p) (auto simp add: divides_def poly_mult simp del: pmult_Cons)
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) order_root:
haftmann@54219:   "poly p a = 0 \<longleftrightarrow> poly p = poly [] \<or> order a p \<noteq> 0"
haftmann@54219:   apply (cases "poly p = poly []")
haftmann@54219:   apply auto
haftmann@54219:   apply (simp add: poly_linear_divides del: pmult_Cons, safe)
haftmann@54219:   apply (drule_tac [!] a = a in order2)
haftmann@54219:   apply (rule ccontr)
haftmann@54219:   apply (simp add: divides_def poly_mult fun_eq del: pmult_Cons, blast)
haftmann@54219:   using neq0_conv
haftmann@54219:   apply (blast intro: lemma_order_root)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) order_divides:
haftmann@54219:   "([-a, 1] %^ n) divides p \<longleftrightarrow> poly p = poly [] \<or> n \<le> order a p"
wenzelm@52881:   apply (cases "poly p = poly []")
wenzelm@52881:   apply auto
wenzelm@52778:   apply (simp add: divides_def fun_eq poly_mult)
wenzelm@52778:   apply (rule_tac x = "[]" in exI)
haftmann@54219:   apply (auto dest!: order2 [where a=a] intro: poly_exp_divides simp del: pexp_Suc)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) order_decomp:
haftmann@54219:   "poly p \<noteq> poly [] \<Longrightarrow> \<exists>q. poly p = poly (([-a, 1] %^ (order a p)) *** q) \<and> ~([-a, 1] divides q)"
wenzelm@52778:   apply (unfold divides_def)
wenzelm@52778:   apply (drule order2 [where a = a])
haftmann@54219:   apply (simp add: divides_def del: pexp_Suc pmult_Cons, safe)
haftmann@54219:   apply (rule_tac x = q in exI, safe)
wenzelm@52778:   apply (drule_tac x = qa in spec)
haftmann@57514:   apply (auto simp add: poly_mult fun_eq poly_exp ac_simps simp del: pmult_Cons)
wenzelm@52778:   done
chaieb@33153: 
chaieb@33153: text{*Important composition properties of orders.*}
haftmann@54219: lemma order_mult:
haftmann@54219:   "poly (p *** q) \<noteq> poly [] \<Longrightarrow>
haftmann@54219:     order a (p *** q) = order a p + order (a::'a::{idom_char_0}) q"
haftmann@54219:   apply (cut_tac a = a and p = "p *** q" and n = "order a p + order a q" in order)
wenzelm@52778:   apply (auto simp add: poly_entire simp del: pmult_Cons)
wenzelm@52778:   apply (drule_tac a = a in order2)+
wenzelm@52778:   apply safe
haftmann@54219:   apply (simp add: divides_def fun_eq poly_exp_add poly_mult del: pmult_Cons, safe)
wenzelm@52778:   apply (rule_tac x = "qa *** qaa" in exI)
haftmann@57514:   apply (simp add: poly_mult ac_simps del: pmult_Cons)
wenzelm@52778:   apply (drule_tac a = a in order_decomp)+
wenzelm@52778:   apply safe
haftmann@54219:   apply (subgoal_tac "[-a,1] divides (qa *** qaa) ")
wenzelm@52778:   apply (simp add: poly_primes del: pmult_Cons)
wenzelm@52778:   apply (auto simp add: divides_def simp del: pmult_Cons)
wenzelm@52778:   apply (rule_tac x = qb in exI)
haftmann@54219:   apply (subgoal_tac "poly ([-a, 1] %^ (order a p) *** (qa *** qaa)) = poly ([-a, 1] %^ (order a p) *** ([-a, 1] *** qb))")
haftmann@54219:   apply (drule poly_mult_left_cancel [THEN iffD1], force)
haftmann@54219:   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))) ")
haftmann@54219:   apply (drule poly_mult_left_cancel [THEN iffD1], force)
haftmann@57514:   apply (simp add: fun_eq poly_exp_add poly_mult ac_simps del: pmult_Cons)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) order_mult:
haftmann@54219:   assumes "poly (p *** q) \<noteq> poly []"
haftmann@54219:   shows "order a (p *** q) = order a p + order a q"
haftmann@54219:   using assms
haftmann@54219:   apply (cut_tac a = a and p = "pmult p q" and n = "order a p + order a q" in order)
haftmann@54219:   apply (auto simp add: poly_entire simp del: pmult_Cons)
haftmann@54219:   apply (drule_tac a = a in order2)+
haftmann@54219:   apply safe
haftmann@54219:   apply (simp add: divides_def fun_eq poly_exp_add poly_mult del: pmult_Cons, safe)
haftmann@54219:   apply (rule_tac x = "pmult qa qaa" in exI)
haftmann@57514:   apply (simp add: poly_mult ac_simps del: pmult_Cons)
haftmann@54219:   apply (drule_tac a = a in order_decomp)+
haftmann@54219:   apply safe
haftmann@54219:   apply (subgoal_tac "[uminus a, one] divides pmult qa qaa")
haftmann@54219:   apply (simp add: poly_primes del: pmult_Cons)
haftmann@54219:   apply (auto simp add: divides_def simp del: pmult_Cons)
haftmann@54219:   apply (rule_tac x = qb in exI)
haftmann@54219:   apply (subgoal_tac "poly (pmult (pexp [uminus a, one] (order a p)) (pmult qa qaa)) =
haftmann@54219:     poly (pmult (pexp [uminus a, one] (?order a p)) (pmult [uminus a, one] qb))")
haftmann@54219:   apply (drule poly_mult_left_cancel [THEN iffD1], force)
haftmann@54219:   apply (subgoal_tac "poly (pmult (pexp [uminus a, one] (order a q))
haftmann@54219:       (pmult (pexp [uminus a, one] (order a p)) (pmult qa qaa))) =
haftmann@54219:     poly (pmult (pexp [uminus a, one] (order a q))
haftmann@54219:       (pmult (pexp [uminus a, one] (order a p)) (pmult [uminus a, one] qb)))")
haftmann@54219:   apply (drule poly_mult_left_cancel [THEN iffD1], force)
haftmann@57514:   apply (simp add: fun_eq poly_exp_add poly_mult ac_simps del: pmult_Cons)
haftmann@54219:   done
haftmann@54219: 
haftmann@54219: lemma (in idom_char_0) order_root2: "poly p \<noteq> poly [] \<Longrightarrow> poly p a = 0 \<longleftrightarrow> order a p \<noteq> 0"
wenzelm@52881:   by (rule order_root [THEN ssubst]) auto
chaieb@33153: 
haftmann@54219: lemma (in semiring_1) pmult_one[simp]: "[1] *** p = p" by auto
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_Nil_zero: "poly [] = poly [0]"
wenzelm@52778:   by (simp add: fun_eq)
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) rsquarefree_decomp:
haftmann@54219:   "rsquarefree p \<Longrightarrow> poly p a = 0 \<Longrightarrow>
wenzelm@52881:     \<exists>q. poly p = poly ([-a, 1] *** q) \<and> poly q a \<noteq> 0"
haftmann@54219:   apply (simp add: rsquarefree_def, safe)
wenzelm@52778:   apply (frule_tac a = a in order_decomp)
wenzelm@52778:   apply (drule_tac x = a in spec)
wenzelm@52778:   apply (drule_tac a = a in order_root2 [symmetric])
wenzelm@52778:   apply (auto simp del: pmult_Cons)
haftmann@54219:   apply (rule_tac x = q in exI, safe)
wenzelm@52778:   apply (simp add: poly_mult fun_eq)
wenzelm@52778:   apply (drule_tac p1 = q in poly_linear_divides [THEN iffD1])
haftmann@54219:   apply (simp add: divides_def del: pmult_Cons, safe)
wenzelm@52778:   apply (drule_tac x = "[]" in spec)
wenzelm@52778:   apply (auto simp add: fun_eq)
wenzelm@52778:   done
chaieb@33153: 
chaieb@33153: 
chaieb@33153: text{*Normalization of a polynomial.*}
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_normalize[simp]: "poly (pnormalize p) = poly p"
wenzelm@52778:   by (induct p) (auto simp add: fun_eq)
chaieb@33153: 
chaieb@33153: text{*The degree of a polynomial.*}
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) lemma_degree_zero: "list_all (%c. c = 0) p \<longleftrightarrow> pnormalize p = []"
wenzelm@52778:   by (induct p) auto
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) degree_zero:
haftmann@54219:   assumes "poly p = poly []"
haftmann@54219:   shows "degree p = 0"
haftmann@54219:   using assms
haftmann@54219:   by (cases "pnormalize p = []") (auto simp add: degree_def poly_zero lemma_degree_zero)
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) pnormalize_sing: "(pnormalize [x] = [x]) \<longleftrightarrow> x \<noteq> 0"
haftmann@54219:   by simp
haftmann@54219: 
haftmann@54219: lemma (in semiring_0) pnormalize_pair: "y \<noteq> 0 \<longleftrightarrow> (pnormalize [x, y] = [x, y])"
wenzelm@52881:   by simp
wenzelm@52778: 
haftmann@54219: lemma (in semiring_0) pnormal_cons: "pnormal p \<Longrightarrow> pnormal (c#p)"
chaieb@33153:   unfolding pnormal_def by simp
wenzelm@52778: 
haftmann@54219: lemma (in semiring_0) pnormal_tail: "p\<noteq>[] \<Longrightarrow> pnormal (c#p) \<Longrightarrow> pnormal p"
haftmann@54219:   unfolding pnormal_def by(auto split: split_if_asm)
haftmann@54219: 
haftmann@54219: 
haftmann@54219: lemma (in semiring_0) pnormal_last_nonzero: "pnormal p \<Longrightarrow> last p \<noteq> 0"
haftmann@54219:   by (induct p) (simp_all add: pnormal_def split: split_if_asm)
haftmann@54219: 
haftmann@54219: lemma (in semiring_0) pnormal_length: "pnormal p \<Longrightarrow> 0 < length p"
haftmann@54219:   unfolding pnormal_def length_greater_0_conv by blast
haftmann@54219: 
haftmann@54219: lemma (in semiring_0) pnormal_last_length: "0 < length p \<Longrightarrow> last p \<noteq> 0 \<Longrightarrow> pnormal p"
haftmann@54219:   by (induct p) (auto simp: pnormal_def  split: split_if_asm)
haftmann@54219: 
haftmann@54219: 
haftmann@54219: lemma (in semiring_0) pnormal_id: "pnormal p \<longleftrightarrow> 0 < length p \<and> last p \<noteq> 0"
haftmann@54219:   using pnormal_last_length pnormal_length pnormal_last_nonzero by blast
haftmann@54219: 
haftmann@54219: lemma (in idom_char_0) poly_Cons_eq:
haftmann@54219:   "poly (c # cs) = poly (d # ds) \<longleftrightarrow> c = d \<and> poly cs = poly ds"
haftmann@54219:   (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@54219: proof
haftmann@54219:   assume eq: ?lhs
haftmann@54219:   hence "\<And>x. poly ((c#cs) +++ -- (d#ds)) x = 0"
haftmann@54230:     by (simp only: poly_minus poly_add algebra_simps) (simp add: algebra_simps)
haftmann@54219:   hence "poly ((c#cs) +++ -- (d#ds)) = poly []" by(simp add: fun_eq_iff)
haftmann@54219:   hence "c = d \<and> list_all (\<lambda>x. x=0) ((cs +++ -- ds))"
haftmann@54219:     unfolding poly_zero by (simp add: poly_minus_def algebra_simps)
haftmann@54219:   hence "c = d \<and> (\<forall>x. poly (cs +++ -- ds) x = 0)"
haftmann@54219:     unfolding poly_zero[symmetric] by simp
haftmann@54219:   then show ?rhs by (simp add: poly_minus poly_add algebra_simps fun_eq_iff)
haftmann@54219: next
haftmann@54219:   assume ?rhs
haftmann@54219:   then show ?lhs by(simp add:fun_eq_iff)
haftmann@54219: qed
haftmann@54219: 
haftmann@54219: lemma (in idom_char_0) pnormalize_unique: "poly p = poly q \<Longrightarrow> pnormalize p = pnormalize q"
haftmann@54219: proof (induct q arbitrary: p)
haftmann@54219:   case Nil
haftmann@54219:   thus ?case by (simp only: poly_zero lemma_degree_zero) simp
haftmann@54219: next
haftmann@54219:   case (Cons c cs p)
haftmann@54219:   thus ?case
haftmann@54219:   proof (induct p)
haftmann@54219:     case Nil
haftmann@54219:     hence "poly [] = poly (c#cs)" by blast
haftmann@54219:     then have "poly (c#cs) = poly [] " by simp
haftmann@54219:     thus ?case by (simp only: poly_zero lemma_degree_zero) simp
haftmann@54219:   next
haftmann@54219:     case (Cons d ds)
haftmann@54219:     hence eq: "poly (d # ds) = poly (c # cs)" by blast
haftmann@54219:     hence eq': "\<And>x. poly (d # ds) x = poly (c # cs) x" by simp
haftmann@54219:     hence "poly (d # ds) 0 = poly (c # cs) 0" by blast
haftmann@54219:     hence dc: "d = c" by auto
haftmann@54219:     with eq have "poly ds = poly cs"
haftmann@54219:       unfolding  poly_Cons_eq by simp
haftmann@54219:     with Cons.prems have "pnormalize ds = pnormalize cs" by blast
haftmann@54219:     with dc show ?case by simp
haftmann@54219:   qed
haftmann@54219: qed
haftmann@54219: 
haftmann@54219: lemma (in idom_char_0) degree_unique:
haftmann@54219:   assumes pq: "poly p = poly q"
haftmann@54219:   shows "degree p = degree q"
haftmann@54219:   using pnormalize_unique[OF pq] unfolding degree_def by simp
haftmann@54219: 
haftmann@54219: lemma (in semiring_0) pnormalize_length:
haftmann@54219:   "length (pnormalize p) \<le> length p" by (induct p) auto
haftmann@54219: 
haftmann@54219: lemma (in semiring_0) last_linear_mul_lemma:
haftmann@54219:   "last ((a %* p) +++ (x#(b %* p))) = (if p = [] then x else b * last p)"
haftmann@54219:   apply (induct p arbitrary: a x b)
wenzelm@52881:   apply auto
blanchet@55417:   apply (rename_tac a p aa x b)
haftmann@54219:   apply (subgoal_tac "padd (cmult aa p) (times b a # cmult b p) \<noteq> []")
haftmann@54219:   apply simp
haftmann@54219:   apply (induct_tac p)
wenzelm@52881:   apply auto
wenzelm@52778:   done
wenzelm@52778: 
haftmann@54219: lemma (in semiring_1) last_linear_mul:
haftmann@54219:   assumes p: "p \<noteq> []"
haftmann@54219:   shows "last ([a,1] *** p) = last p"
haftmann@54219: proof -
haftmann@54219:   from p obtain c cs where cs: "p = c#cs" by (cases p) auto
haftmann@54219:   from cs have eq: "[a,1] *** p = (a %* (c#cs)) +++ (0#(1 %* (c#cs)))"
haftmann@54219:     by (simp add: poly_cmult_distr)
haftmann@54219:   show ?thesis using cs
haftmann@54219:     unfolding eq last_linear_mul_lemma by simp
haftmann@54219: qed
haftmann@54219: 
haftmann@54219: lemma (in semiring_0) pnormalize_eq: "last p \<noteq> 0 \<Longrightarrow> pnormalize p = p"
haftmann@54219:   by (induct p) (auto split: split_if_asm)
haftmann@54219: 
haftmann@54219: lemma (in semiring_0) last_pnormalize: "pnormalize p \<noteq> [] \<Longrightarrow> last (pnormalize p) \<noteq> 0"
haftmann@54219:   by (induct p) auto
haftmann@54219: 
haftmann@54219: lemma (in semiring_0) pnormal_degree: "last p \<noteq> 0 \<Longrightarrow> degree p = length p - 1"
haftmann@54219:   using pnormalize_eq[of p] unfolding degree_def by simp
wenzelm@52778: 
haftmann@54219: lemma (in semiring_0) poly_Nil_ext: "poly [] = (\<lambda>x. 0)"
haftmann@54219:   by (rule ext) simp
haftmann@54219: 
haftmann@54219: lemma (in idom_char_0) linear_mul_degree:
haftmann@54219:   assumes p: "poly p \<noteq> poly []"
haftmann@54219:   shows "degree ([a,1] *** p) = degree p + 1"
haftmann@54219: proof -
haftmann@54219:   from p have pnz: "pnormalize p \<noteq> []"
haftmann@54219:     unfolding poly_zero lemma_degree_zero .
haftmann@54219: 
haftmann@54219:   from last_linear_mul[OF pnz, of a] last_pnormalize[OF pnz]
haftmann@54219:   have l0: "last ([a, 1] *** pnormalize p) \<noteq> 0" by simp
haftmann@54219:   from last_pnormalize[OF pnz] last_linear_mul[OF pnz, of a]
haftmann@54219:     pnormal_degree[OF l0] pnormal_degree[OF last_pnormalize[OF pnz]] pnz
haftmann@54219: 
haftmann@54219:   have th: "degree ([a,1] *** pnormalize p) = degree (pnormalize p) + 1"
haftmann@54219:     by simp
haftmann@54219: 
haftmann@54219:   have eqs: "poly ([a,1] *** pnormalize p) = poly ([a,1] *** p)"
haftmann@54219:     by (rule ext) (simp add: poly_mult poly_add poly_cmult)
haftmann@54219:   from degree_unique[OF eqs] th
haftmann@54219:   show ?thesis by (simp add: degree_unique[OF poly_normalize])
haftmann@54219: qed
wenzelm@52778: 
haftmann@54219: lemma (in idom_char_0) linear_pow_mul_degree:
haftmann@54219:   "degree([a,1] %^n *** p) = (if poly p = poly [] then 0 else degree p + n)"
haftmann@54219: proof (induct n arbitrary: a p)
haftmann@54219:   case (0 a p)
haftmann@54219:   show ?case
haftmann@54219:   proof (cases "poly p = poly []")
haftmann@54219:     case True
haftmann@54219:     then show ?thesis
haftmann@54219:       using degree_unique[OF True] by (simp add: degree_def)
haftmann@54219:   next
haftmann@54219:     case False
haftmann@54219:     then show ?thesis by (auto simp add: poly_Nil_ext)
haftmann@54219:   qed
haftmann@54219: next
haftmann@54219:   case (Suc n a p)
haftmann@54219:   have eq: "poly ([a,1] %^(Suc n) *** p) = poly ([a,1] %^ n *** ([a,1] *** p))"
haftmann@54219:     apply (rule ext)
haftmann@54219:     apply (simp add: poly_mult poly_add poly_cmult)
haftmann@57514:     apply (simp add: ac_simps ac_simps distrib_left)
haftmann@54219:     done
haftmann@54219:   note deq = degree_unique[OF eq]
haftmann@54219:   show ?case
haftmann@54219:   proof (cases "poly p = poly []")
haftmann@54219:     case True
haftmann@54219:     with eq have eq': "poly ([a,1] %^(Suc n) *** p) = poly []"
haftmann@54219:       apply -
haftmann@54219:       apply (rule ext)
haftmann@54219:       apply (simp add: poly_mult poly_cmult poly_add)
haftmann@54219:       done
haftmann@54219:     from degree_unique[OF eq'] True show ?thesis
haftmann@54219:       by (simp add: degree_def)
haftmann@54219:   next
haftmann@54219:     case False
haftmann@54219:     then have ap: "poly ([a,1] *** p) \<noteq> poly []"
haftmann@54219:       using poly_mult_not_eq_poly_Nil unfolding poly_entire by auto
haftmann@54219:     have eq: "poly ([a,1] %^(Suc n) *** p) = poly ([a,1]%^n *** ([a,1] *** p))"
haftmann@54219:       by (rule ext, simp add: poly_mult poly_add poly_exp poly_cmult algebra_simps)
haftmann@54219:     from ap have ap': "(poly ([a,1] *** p) = poly []) = False"
haftmann@54219:       by blast
haftmann@54219:     have th0: "degree ([a,1]%^n *** ([a,1] *** p)) = degree ([a,1] *** p) + n"
haftmann@54219:       apply (simp only: Suc.hyps[of a "pmult [a,one] p"] ap')
haftmann@54219:       apply simp
haftmann@54219:       done
haftmann@54219:     from degree_unique[OF eq] ap False th0 linear_mul_degree[OF False, of a]
haftmann@54219:     show ?thesis by (auto simp del: poly.simps)
haftmann@54219:   qed
haftmann@54219: qed
wenzelm@52778: 
haftmann@54219: lemma (in idom_char_0) order_degree:
haftmann@54219:   assumes p0: "poly p \<noteq> poly []"
haftmann@54219:   shows "order a p \<le> degree p"
haftmann@54219: proof -
haftmann@54219:   from order2[OF p0, unfolded divides_def]
haftmann@54219:   obtain q where q: "poly p = poly ([- a, 1]%^ (order a p) *** q)" by blast
haftmann@54219:   {
haftmann@54219:     assume "poly q = poly []"
haftmann@54219:     with q p0 have False by (simp add: poly_mult poly_entire)
haftmann@54219:   }
haftmann@54219:   with degree_unique[OF q, unfolded linear_pow_mul_degree] show ?thesis
haftmann@54219:     by auto
haftmann@54219: qed
chaieb@33153: 
chaieb@33153: text{*Tidier versions of finiteness of roots.*}
chaieb@33153: 
haftmann@54219: lemma (in idom_char_0) poly_roots_finite_set:
haftmann@54219:   "poly p \<noteq> poly [] \<Longrightarrow> finite {x. poly p x = 0}"
wenzelm@52778:   unfolding poly_roots_finite .
chaieb@33153: 
chaieb@33153: text{*bound for polynomial.*}
chaieb@33153: 
haftmann@54219: lemma poly_mono: "abs(x) \<le> k \<Longrightarrow> abs(poly p (x::'a::{linordered_idom})) \<le> poly (map abs p) k"
wenzelm@52881:   apply (induct p)
wenzelm@52881:   apply auto
blanchet@55417:   apply (rename_tac a p)
wenzelm@52778:   apply (rule_tac y = "abs a + abs (x * poly p x)" in order_trans)
wenzelm@52778:   apply (rule abs_triangle_ineq)
wenzelm@52778:   apply (auto intro!: mult_mono simp add: abs_mult)
wenzelm@52778:   done
chaieb@33153: 
haftmann@54219: lemma (in semiring_0) poly_Sing: "poly [c] x = c" by simp
wenzelm@33268: 
chaieb@33153: end




