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