src/HOL/Decision_Procs/Polynomial_List.thy
changeset 33153 92080294beb8
child 33268 02de0317f66f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Decision_Procs/Polynomial_List.thy	Sun Oct 25 08:57:36 2009 +0100
     1.3 @@ -0,0 +1,783 @@
     1.4 +(*  Title:       HOL/Decision_Procs/Polynomial_List.thy
     1.5 +    Author:      Amine Chaieb
     1.6 +*)
     1.7 +
     1.8 +header{*Univariate Polynomials as Lists *}
     1.9 +
    1.10 +theory Polynomial_List
    1.11 +imports Main
    1.12 +begin
    1.13 +
    1.14 +text{* Application of polynomial as a real function. *}
    1.15 +
    1.16 +consts poly :: "'a list => 'a  => ('a::{comm_ring})"
    1.17 +primrec
    1.18 +  poly_Nil:  "poly [] x = 0"
    1.19 +  poly_Cons: "poly (h#t) x = h + x * poly t x"
    1.20 +
    1.21 +
    1.22 +subsection{*Arithmetic Operations on Polynomials*}
    1.23 +
    1.24 +text{*addition*}
    1.25 +consts padd :: "['a list, 'a list] => ('a::comm_ring_1) list"  (infixl "+++" 65)
    1.26 +primrec
    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 +consts cmult :: "['a :: comm_ring_1, 'a list] => 'a list"  (infixl "%*" 70)
    1.33 +primrec
    1.34 +   cmult_Nil:  "c %* [] = []"
    1.35 +   cmult_Cons: "c %* (h#t) = (c * h)#(c %* t)"
    1.36 +
    1.37 +text{*Multiplication by a polynomial*}
    1.38 +consts pmult :: "['a list, 'a list] => ('a::comm_ring_1) list"  (infixl "***" 70)
    1.39 +primrec
    1.40 +   pmult_Nil:  "[] *** l2 = []"
    1.41 +   pmult_Cons: "(h#t) *** l2 = (if t = [] then h %* l2
    1.42 +                              else (h %* l2) +++ ((0) # (t *** l2)))"
    1.43 +
    1.44 +text{*Repeated multiplication by a polynomial*}
    1.45 +consts mulexp :: "[nat, 'a list, 'a  list] => ('a ::comm_ring_1) list"
    1.46 +primrec
    1.47 +   mulexp_zero:  "mulexp 0 p q = q"
    1.48 +   mulexp_Suc:   "mulexp (Suc n) p q = p *** mulexp n p q"
    1.49 +
    1.50 +text{*Exponential*}
    1.51 +consts pexp :: "['a list, nat] => ('a::comm_ring_1) list"  (infixl "%^" 80)
    1.52 +primrec
    1.53 +   pexp_0:   "p %^ 0 = [1]"
    1.54 +   pexp_Suc: "p %^ (Suc n) = p *** (p %^ n)"
    1.55 +
    1.56 +text{*Quotient related value of dividing a polynomial by x + a*}
    1.57 +(* Useful for divisor properties in inductive proofs *)
    1.58 +consts "pquot" :: "['a list, 'a::field] => 'a list"
    1.59 +primrec
    1.60 +   pquot_Nil:  "pquot [] a= []"
    1.61 +   pquot_Cons: "pquot (h#t) a = (if t = [] then [h]
    1.62 +                   else (inverse(a) * (h - hd( pquot t a)))#(pquot t a))"
    1.63 +
    1.64 +
    1.65 +text{*normalization of polynomials (remove extra 0 coeff)*}
    1.66 +consts pnormalize :: "('a::comm_ring_1) list => 'a list"
    1.67 +primrec
    1.68 +   pnormalize_Nil:  "pnormalize [] = []"
    1.69 +   pnormalize_Cons: "pnormalize (h#p) = (if ( (pnormalize p) = [])
    1.70 +                                     then (if (h = 0) then [] else [h])
    1.71 +                                     else (h#(pnormalize p)))"
    1.72 +
    1.73 +definition "pnormal p = ((pnormalize p = p) \<and> p \<noteq> [])"
    1.74 +definition "nonconstant p = (pnormal p \<and> (\<forall>x. p \<noteq> [x]))"
    1.75 +text{*Other definitions*}
    1.76 +
    1.77 +definition
    1.78 +  poly_minus :: "'a list => ('a :: comm_ring_1) list"      ("-- _" [80] 80) where
    1.79 +  "-- p = (- 1) %* p"
    1.80 +
    1.81 +definition
    1.82 +  divides :: "[('a::comm_ring_1) list, 'a list] => bool"  (infixl "divides" 70) where
    1.83 +  "p1 divides p2 = (\<exists>q. poly p2 = poly(p1 *** q))"
    1.84 +
    1.85 +definition
    1.86 +  order :: "('a::comm_ring_1) => 'a list => nat" where
    1.87 +    --{*order of a polynomial*}
    1.88 +  "order a p = (SOME n. ([-a, 1] %^ n) divides p &
    1.89 +                      ~ (([-a, 1] %^ (Suc n)) divides p))"
    1.90 +
    1.91 +definition
    1.92 +  degree :: "('a::comm_ring_1) list => nat" where
    1.93 +     --{*degree of a polynomial*}
    1.94 +  "degree p = length (pnormalize p) - 1"
    1.95 +
    1.96 +definition
    1.97 +  rsquarefree :: "('a::comm_ring_1) list => bool" where
    1.98 +     --{*squarefree polynomials --- NB with respect to real roots only.*}
    1.99 +  "rsquarefree p = (poly p \<noteq> poly [] &
   1.100 +                     (\<forall>a. (order a p = 0) | (order a p = 1)))"
   1.101 +
   1.102 +lemma padd_Nil2: "p +++ [] = p"
   1.103 +by (induct p) auto
   1.104 +declare padd_Nil2 [simp]
   1.105 +
   1.106 +lemma padd_Cons_Cons: "(h1 # p1) +++ (h2 # p2) = (h1 + h2) # (p1 +++ p2)"
   1.107 +by auto
   1.108 +
   1.109 +lemma pminus_Nil: "-- [] = []"
   1.110 +by (simp add: poly_minus_def)
   1.111 +declare pminus_Nil [simp]
   1.112 +
   1.113 +lemma pmult_singleton: "[h1] *** p1 = h1 %* p1"
   1.114 +by simp
   1.115 +
   1.116 +lemma poly_ident_mult: "1 %* t = t"
   1.117 +by (induct "t", auto)
   1.118 +declare poly_ident_mult [simp]
   1.119 +
   1.120 +lemma poly_simple_add_Cons: "[a] +++ ((0)#t) = (a#t)"
   1.121 +by simp
   1.122 +declare poly_simple_add_Cons [simp]
   1.123 +
   1.124 +text{*Handy general properties*}
   1.125 +
   1.126 +lemma padd_commut: "b +++ a = a +++ b"
   1.127 +apply (subgoal_tac "\<forall>a. b +++ a = a +++ b")
   1.128 +apply (induct_tac [2] "b", auto)
   1.129 +apply (rule padd_Cons [THEN ssubst])
   1.130 +apply (case_tac "aa", auto)
   1.131 +done
   1.132 +
   1.133 +lemma padd_assoc [rule_format]: "\<forall>b c. (a +++ b) +++ c = a +++ (b +++ c)"
   1.134 +apply (induct "a", simp, clarify)
   1.135 +apply (case_tac b, simp_all)
   1.136 +done
   1.137 +
   1.138 +lemma poly_cmult_distr [rule_format]:
   1.139 +     "\<forall>q. a %* ( p +++ q) = (a %* p +++ a %* q)"
   1.140 +apply (induct "p", simp, clarify) 
   1.141 +apply (case_tac "q")
   1.142 +apply (simp_all add: right_distrib)
   1.143 +done
   1.144 +
   1.145 +lemma pmult_by_x[simp]: "[0, 1] *** t = ((0)#t)"
   1.146 +apply (induct "t", simp)
   1.147 +by (auto simp add: mult_zero_left poly_ident_mult padd_commut)
   1.148 +
   1.149 +
   1.150 +text{*properties of evaluation of polynomials.*}
   1.151 +
   1.152 +lemma poly_add: "poly (p1 +++ p2) x = poly p1 x + poly p2 x"
   1.153 +apply (subgoal_tac "\<forall>p2. poly (p1 +++ p2) x = poly (p1) x + poly (p2) x")
   1.154 +apply (induct_tac [2] "p1", auto)
   1.155 +apply (case_tac "p2")
   1.156 +apply (auto simp add: right_distrib)
   1.157 +done
   1.158 +
   1.159 +lemma poly_cmult: "poly (c %* p) x = c * poly p x"
   1.160 +apply (induct "p") 
   1.161 +apply (case_tac [2] "x=0")
   1.162 +apply (auto simp add: right_distrib mult_ac)
   1.163 +done
   1.164 +
   1.165 +lemma poly_minus: "poly (-- p) x = - (poly p x)"
   1.166 +apply (simp add: poly_minus_def)
   1.167 +apply (auto simp add: poly_cmult)
   1.168 +done
   1.169 +
   1.170 +lemma poly_mult: "poly (p1 *** p2) x = poly p1 x * poly p2 x"
   1.171 +apply (subgoal_tac "\<forall>p2. poly (p1 *** p2) x = poly p1 x * poly p2 x")
   1.172 +apply (simp (no_asm_simp))
   1.173 +apply (induct "p1")
   1.174 +apply (auto simp add: poly_cmult)
   1.175 +apply (case_tac p1)
   1.176 +apply (auto simp add: poly_cmult poly_add left_distrib right_distrib mult_ac)
   1.177 +done
   1.178 +
   1.179 +lemma poly_exp: "poly (p %^ n) (x::'a::comm_ring_1) = (poly p x) ^ n"
   1.180 +apply (induct "n")
   1.181 +apply (auto simp add: poly_cmult poly_mult power_Suc)
   1.182 +done
   1.183 +
   1.184 +text{*More Polynomial Evaluation Lemmas*}
   1.185 +
   1.186 +lemma poly_add_rzero: "poly (a +++ []) x = poly a x"
   1.187 +by simp
   1.188 +declare poly_add_rzero [simp]
   1.189 +
   1.190 +lemma poly_mult_assoc: "poly ((a *** b) *** c) x = poly (a *** (b *** c)) x"
   1.191 +  by (simp add: poly_mult mult_assoc)
   1.192 +
   1.193 +lemma poly_mult_Nil2: "poly (p *** []) x = 0"
   1.194 +by (induct "p", auto)
   1.195 +declare poly_mult_Nil2 [simp]
   1.196 +
   1.197 +lemma poly_exp_add: "poly (p %^ (n + d)) x = poly( p %^ n *** p %^ d) x"
   1.198 +apply (induct "n")
   1.199 +apply (auto simp add: poly_mult mult_assoc)
   1.200 +done
   1.201 +
   1.202 +subsection{*Key Property: if @{term "f(a) = 0"} then @{term "(x - a)"} divides
   1.203 + @{term "p(x)"} *}
   1.204 +
   1.205 +lemma lemma_poly_linear_rem: "\<forall>h. \<exists>q r. h#t = [r] +++ [-a, 1] *** q"
   1.206 +apply (induct "t", safe)
   1.207 +apply (rule_tac x = "[]" in exI)
   1.208 +apply (rule_tac x = h in exI, simp)
   1.209 +apply (drule_tac x = aa in spec, safe)
   1.210 +apply (rule_tac x = "r#q" in exI)
   1.211 +apply (rule_tac x = "a*r + h" in exI)
   1.212 +apply (case_tac "q", auto)
   1.213 +done
   1.214 +
   1.215 +lemma poly_linear_rem: "\<exists>q r. h#t = [r] +++ [-a, 1] *** q"
   1.216 +by (cut_tac t = t and a = a in lemma_poly_linear_rem, auto)
   1.217 +
   1.218 +
   1.219 +lemma poly_linear_divides: "(poly p a = 0) = ((p = []) | (\<exists>q. p = [-a, 1] *** q))"
   1.220 +apply (auto simp add: poly_add poly_cmult right_distrib)
   1.221 +apply (case_tac "p", simp) 
   1.222 +apply (cut_tac h = aa and t = list and a = a in poly_linear_rem, safe)
   1.223 +apply (case_tac "q", auto)
   1.224 +apply (drule_tac x = "[]" in spec, simp)
   1.225 +apply (auto simp add: poly_add poly_cmult add_assoc)
   1.226 +apply (drule_tac x = "aa#lista" in spec, auto)
   1.227 +done
   1.228 +
   1.229 +lemma lemma_poly_length_mult: "\<forall>h k a. length (k %* p +++  (h # (a %* p))) = Suc (length p)"
   1.230 +by (induct "p", auto)
   1.231 +declare lemma_poly_length_mult [simp]
   1.232 +
   1.233 +lemma lemma_poly_length_mult2: "\<forall>h k. length (k %* p +++  (h # p)) = Suc (length p)"
   1.234 +by (induct "p", auto)
   1.235 +declare lemma_poly_length_mult2 [simp]
   1.236 +
   1.237 +lemma poly_length_mult: "length([-a,1] *** q) = Suc (length q)"
   1.238 +by auto
   1.239 +declare poly_length_mult [simp]
   1.240 +
   1.241 +
   1.242 +subsection{*Polynomial length*}
   1.243 +
   1.244 +lemma poly_cmult_length: "length (a %* p) = length p"
   1.245 +by (induct "p", auto)
   1.246 +declare poly_cmult_length [simp]
   1.247 +
   1.248 +lemma poly_add_length [rule_format]:
   1.249 +     "\<forall>p2. length (p1 +++ p2) =
   1.250 +             (if (length p1 < length p2) then length p2 else length p1)"
   1.251 +apply (induct "p1", simp_all)
   1.252 +apply arith
   1.253 +done
   1.254 +
   1.255 +lemma poly_root_mult_length: "length([a,b] *** p) = Suc (length p)"
   1.256 +by (simp add: poly_cmult_length poly_add_length)
   1.257 +declare poly_root_mult_length [simp]
   1.258 +
   1.259 +lemma poly_mult_not_eq_poly_Nil: "(poly (p *** q) x \<noteq> poly [] x) =
   1.260 +      (poly p x \<noteq> poly [] x & poly q x \<noteq> poly [] (x::'a::idom))"
   1.261 +apply (auto simp add: poly_mult)
   1.262 +done
   1.263 +declare poly_mult_not_eq_poly_Nil [simp]
   1.264 +
   1.265 +lemma poly_mult_eq_zero_disj: "(poly (p *** q) (x::'a::idom) = 0) = (poly p x = 0 | poly q x = 0)"
   1.266 +by (auto simp add: poly_mult)
   1.267 +
   1.268 +text{*Normalisation Properties*}
   1.269 +
   1.270 +lemma poly_normalized_nil: "(pnormalize p = []) --> (poly p x = 0)"
   1.271 +by (induct "p", auto)
   1.272 +
   1.273 +text{*A nontrivial polynomial of degree n has no more than n roots*}
   1.274 +
   1.275 +lemma poly_roots_index_lemma0 [rule_format]:
   1.276 +   "\<forall>p x. poly p x \<noteq> poly [] x & length p = n
   1.277 +    --> (\<exists>i. \<forall>x. (poly p x = (0::'a::idom)) --> (\<exists>m. (m \<le> n & x = i m)))"
   1.278 +apply (induct "n", safe)
   1.279 +apply (rule ccontr)
   1.280 +apply (subgoal_tac "\<exists>a. poly p a = 0", safe)
   1.281 +apply (drule poly_linear_divides [THEN iffD1], safe)
   1.282 +apply (drule_tac x = q in spec)
   1.283 +apply (drule_tac x = x in spec)
   1.284 +apply (simp del: poly_Nil pmult_Cons)
   1.285 +apply (erule exE)
   1.286 +apply (drule_tac x = "%m. if m = Suc n then a else i m" in spec, safe)
   1.287 +apply (drule poly_mult_eq_zero_disj [THEN iffD1], safe)
   1.288 +apply (drule_tac x = "Suc (length q)" in spec)
   1.289 +apply (auto simp add: ring_simps)
   1.290 +apply (drule_tac x = xa in spec)
   1.291 +apply (clarsimp simp add: ring_simps)
   1.292 +apply (drule_tac x = m in spec)
   1.293 +apply (auto simp add:ring_simps)
   1.294 +done
   1.295 +lemmas poly_roots_index_lemma1 = conjI [THEN poly_roots_index_lemma0, standard]
   1.296 +
   1.297 +lemma poly_roots_index_length0: "poly p (x::'a::idom) \<noteq> poly [] x ==>
   1.298 +      \<exists>i. \<forall>x. (poly p x = 0) --> (\<exists>n. n \<le> length p & x = i n)"
   1.299 +by (blast intro: poly_roots_index_lemma1)
   1.300 +
   1.301 +lemma poly_roots_finite_lemma: "poly p (x::'a::idom) \<noteq> poly [] x ==>
   1.302 +      \<exists>N i. \<forall>x. (poly p x = 0) --> (\<exists>n. (n::nat) < N & x = i n)"
   1.303 +apply (drule poly_roots_index_length0, safe)
   1.304 +apply (rule_tac x = "Suc (length p)" in exI)
   1.305 +apply (rule_tac x = i in exI) 
   1.306 +apply (simp add: less_Suc_eq_le)
   1.307 +done
   1.308 +
   1.309 +
   1.310 +lemma real_finite_lemma:
   1.311 +  assumes P: "\<forall>x. P x --> (\<exists>n. n < length j & x = j!n)"
   1.312 +  shows "finite {(x::'a::idom). P x}"
   1.313 +proof-
   1.314 +  let ?M = "{x. P x}"
   1.315 +  let ?N = "set j"
   1.316 +  have "?M \<subseteq> ?N" using P by auto
   1.317 +  thus ?thesis using finite_subset by auto
   1.318 +qed
   1.319 +
   1.320 +lemma poly_roots_index_lemma [rule_format]:
   1.321 +   "\<forall>p x. poly p x \<noteq> poly [] x & length p = n
   1.322 +    --> (\<exists>i. \<forall>x. (poly p x = (0::'a::{idom})) --> x \<in> set i)"
   1.323 +apply (induct "n", safe)
   1.324 +apply (rule ccontr)
   1.325 +apply (subgoal_tac "\<exists>a. poly p a = 0", safe)
   1.326 +apply (drule poly_linear_divides [THEN iffD1], safe)
   1.327 +apply (drule_tac x = q in spec)
   1.328 +apply (drule_tac x = x in spec)
   1.329 +apply (auto simp del: poly_Nil pmult_Cons)
   1.330 +apply (drule_tac x = "a#i" in spec)
   1.331 +apply (auto simp only: poly_mult List.list.size)
   1.332 +apply (drule_tac x = xa in spec)
   1.333 +apply (clarsimp simp add: ring_simps)
   1.334 +done
   1.335 +
   1.336 +lemmas poly_roots_index_lemma2 = conjI [THEN poly_roots_index_lemma, standard]
   1.337 +
   1.338 +lemma poly_roots_index_length: "poly p (x::'a::idom) \<noteq> poly [] x ==>
   1.339 +      \<exists>i. \<forall>x. (poly p x = 0) --> x \<in> set i"
   1.340 +by (blast intro: poly_roots_index_lemma2)
   1.341 +
   1.342 +lemma poly_roots_finite_lemma': "poly p (x::'a::idom) \<noteq> poly [] x ==>
   1.343 +      \<exists>i. \<forall>x. (poly p x = 0) --> x \<in> set i"
   1.344 +by (drule poly_roots_index_length, safe)
   1.345 +
   1.346 +lemma UNIV_nat_infinite: "\<not> finite (UNIV :: nat set)"
   1.347 +  unfolding finite_conv_nat_seg_image
   1.348 +proof(auto simp add: expand_set_eq image_iff)
   1.349 +  fix n::nat and f:: "nat \<Rightarrow> nat"
   1.350 +  let ?N = "{i. i < n}"
   1.351 +  let ?fN = "f ` ?N"
   1.352 +  let ?y = "Max ?fN + 1"
   1.353 +  from nat_seg_image_imp_finite[of "?fN" "f" n] 
   1.354 +  have thfN: "finite ?fN" by simp
   1.355 +  {assume "n =0" hence "\<exists>x. \<forall>xa<n. x \<noteq> f xa" by auto}
   1.356 +  moreover
   1.357 +  {assume nz: "n \<noteq> 0"
   1.358 +    hence thne: "?fN \<noteq> {}" by (auto simp add: neq0_conv)
   1.359 +    have "\<forall>x\<in> ?fN. Max ?fN \<ge> x" using nz Max_ge_iff[OF thfN thne] by auto
   1.360 +    hence "\<forall>x\<in> ?fN. ?y > x" by (auto simp add: less_Suc_eq_le)
   1.361 +    hence "?y \<notin> ?fN" by auto
   1.362 +    hence "\<exists>x. \<forall>xa<n. x \<noteq> f xa" by auto }
   1.363 +  ultimately show "\<exists>x. \<forall>xa<n. x \<noteq> f xa" by blast
   1.364 +qed
   1.365 +
   1.366 +lemma UNIV_ring_char_0_infinte: "\<not> finite (UNIV:: ('a::ring_char_0) set)"
   1.367 +proof
   1.368 +  assume F: "finite (UNIV :: 'a set)"
   1.369 +  have th0: "of_nat ` UNIV \<subseteq> (UNIV:: 'a set)" by simp
   1.370 +  from finite_subset[OF th0 F] have th: "finite (of_nat ` UNIV :: 'a set)" .
   1.371 +  have th': "inj_on (of_nat::nat \<Rightarrow> 'a) (UNIV)"
   1.372 +    unfolding inj_on_def by auto
   1.373 +  from finite_imageD[OF th th'] UNIV_nat_infinite 
   1.374 +  show False by blast
   1.375 +qed
   1.376 +
   1.377 +lemma poly_roots_finite: "(poly p \<noteq> poly []) = 
   1.378 +  finite {x. poly p x = (0::'a::{idom, ring_char_0})}"
   1.379 +proof
   1.380 +  assume H: "poly p \<noteq> poly []"
   1.381 +  show "finite {x. poly p x = (0::'a)}"
   1.382 +    using H
   1.383 +    apply -
   1.384 +    apply (erule contrapos_np, rule ext)
   1.385 +    apply (rule ccontr)
   1.386 +    apply (clarify dest!: poly_roots_finite_lemma')
   1.387 +    using finite_subset
   1.388 +  proof-
   1.389 +    fix x i
   1.390 +    assume F: "\<not> finite {x. poly p x = (0\<Colon>'a)}" 
   1.391 +      and P: "\<forall>x. poly p x = (0\<Colon>'a) \<longrightarrow> x \<in> set i"
   1.392 +    let ?M= "{x. poly p x = (0\<Colon>'a)}"
   1.393 +    from P have "?M \<subseteq> set i" by auto
   1.394 +    with finite_subset F show False by auto
   1.395 +  qed
   1.396 +next
   1.397 +  assume F: "finite {x. poly p x = (0\<Colon>'a)}"
   1.398 +  show "poly p \<noteq> poly []" using F UNIV_ring_char_0_infinte by auto  
   1.399 +qed
   1.400 +
   1.401 +text{*Entirety and Cancellation for polynomials*}
   1.402 +
   1.403 +lemma poly_entire_lemma: "[| poly (p:: ('a::{idom,ring_char_0}) list) \<noteq> poly [] ; poly q \<noteq> poly [] |]
   1.404 +      ==>  poly (p *** q) \<noteq> poly []"
   1.405 +by (auto simp add: poly_roots_finite poly_mult Collect_disj_eq)
   1.406 +
   1.407 +lemma poly_entire: "(poly (p *** q) = poly ([]::('a::{idom,ring_char_0}) list)) = ((poly p = poly []) | (poly q = poly []))"
   1.408 +apply (auto intro: ext dest: fun_cong simp add: poly_entire_lemma poly_mult)
   1.409 +apply (blast intro: ccontr dest: poly_entire_lemma poly_mult [THEN subst])
   1.410 +done
   1.411 +
   1.412 +lemma poly_entire_neg: "(poly (p *** q) \<noteq> poly ([]::('a::{idom,ring_char_0}) list)) = ((poly p \<noteq> poly []) & (poly q \<noteq> poly []))"
   1.413 +by (simp add: poly_entire)
   1.414 +
   1.415 +lemma fun_eq: " (f = g) = (\<forall>x. f x = g x)"
   1.416 +by (auto intro!: ext)
   1.417 +
   1.418 +lemma poly_add_minus_zero_iff: "(poly (p +++ -- q) = poly []) = (poly p = poly q)"
   1.419 +by (auto simp add: ring_simps poly_add poly_minus_def fun_eq poly_cmult)
   1.420 +
   1.421 +lemma poly_add_minus_mult_eq: "poly (p *** q +++ --(p *** r)) = poly (p *** (q +++ -- r))"
   1.422 +by (auto simp add: poly_add poly_minus_def fun_eq poly_mult poly_cmult right_distrib)
   1.423 +
   1.424 +lemma poly_mult_left_cancel: "(poly (p *** q) = poly (p *** r)) = (poly p = poly ([]::('a::{idom, ring_char_0}) list) | poly q = poly r)"
   1.425 +apply (rule_tac p1 = "p *** q" in poly_add_minus_zero_iff [THEN subst])
   1.426 +apply (auto intro: ext simp add: poly_add_minus_mult_eq poly_entire poly_add_minus_zero_iff)
   1.427 +done
   1.428 +
   1.429 +lemma poly_exp_eq_zero:
   1.430 +     "(poly (p %^ n) = poly ([]::('a::idom) list)) = (poly p = poly [] & n \<noteq> 0)"
   1.431 +apply (simp only: fun_eq add: all_simps [symmetric]) 
   1.432 +apply (rule arg_cong [where f = All]) 
   1.433 +apply (rule ext)
   1.434 +apply (induct_tac "n")
   1.435 +apply (auto simp add: poly_mult)
   1.436 +done
   1.437 +declare poly_exp_eq_zero [simp]
   1.438 +
   1.439 +lemma poly_prime_eq_zero: "poly [a,(1::'a::comm_ring_1)] \<noteq> poly []"
   1.440 +apply (simp add: fun_eq)
   1.441 +apply (rule_tac x = "1 - a" in exI, simp)
   1.442 +done
   1.443 +declare poly_prime_eq_zero [simp]
   1.444 +
   1.445 +lemma poly_exp_prime_eq_zero: "(poly ([a, (1::'a::idom)] %^ n) \<noteq> poly [])"
   1.446 +by auto
   1.447 +declare poly_exp_prime_eq_zero [simp]
   1.448 +
   1.449 +text{*A more constructive notion of polynomials being trivial*}
   1.450 +
   1.451 +lemma poly_zero_lemma': "poly (h # t) = poly [] ==> h = (0::'a::{idom,ring_char_0}) & poly t = poly []"
   1.452 +apply(simp add: fun_eq)
   1.453 +apply (case_tac "h = 0")
   1.454 +apply (drule_tac [2] x = 0 in spec, auto) 
   1.455 +apply (case_tac "poly t = poly []", simp) 
   1.456 +proof-
   1.457 +  fix x
   1.458 +  assume H: "\<forall>x. x = (0\<Colon>'a) \<or> poly t x = (0\<Colon>'a)"  and pnz: "poly t \<noteq> poly []"
   1.459 +  let ?S = "{x. poly t x = 0}"
   1.460 +  from H have "\<forall>x. x \<noteq>0 \<longrightarrow> poly t x = 0" by blast
   1.461 +  hence th: "?S \<supseteq> UNIV - {0}" by auto
   1.462 +  from poly_roots_finite pnz have th': "finite ?S" by blast
   1.463 +  from finite_subset[OF th th'] UNIV_ring_char_0_infinte[where ?'a = 'a]
   1.464 +  show "poly t x = (0\<Colon>'a)" by simp
   1.465 +  qed
   1.466 +
   1.467 +lemma poly_zero: "(poly p = poly []) = list_all (%c. c = (0::'a::{idom,ring_char_0})) p"
   1.468 +apply (induct "p", simp)
   1.469 +apply (rule iffI)
   1.470 +apply (drule poly_zero_lemma', auto)
   1.471 +done
   1.472 +
   1.473 +
   1.474 +
   1.475 +text{*Basics of divisibility.*}
   1.476 +
   1.477 +lemma poly_primes: "([a, (1::'a::idom)] divides (p *** q)) = ([a, 1] divides p | [a, 1] divides q)"
   1.478 +apply (auto simp add: divides_def fun_eq poly_mult poly_add poly_cmult left_distrib [symmetric])
   1.479 +apply (drule_tac x = "-a" in spec)
   1.480 +apply (auto simp add: poly_linear_divides poly_add poly_cmult left_distrib [symmetric])
   1.481 +apply (rule_tac x = "qa *** q" in exI)
   1.482 +apply (rule_tac [2] x = "p *** qa" in exI)
   1.483 +apply (auto simp add: poly_add poly_mult poly_cmult mult_ac)
   1.484 +done
   1.485 +
   1.486 +lemma poly_divides_refl: "p divides p"
   1.487 +apply (simp add: divides_def)
   1.488 +apply (rule_tac x = "[1]" in exI)
   1.489 +apply (auto simp add: poly_mult fun_eq)
   1.490 +done
   1.491 +declare poly_divides_refl [simp]
   1.492 +
   1.493 +lemma poly_divides_trans: "[| p divides q; q divides r |] ==> p divides r"
   1.494 +apply (simp add: divides_def, safe)
   1.495 +apply (rule_tac x = "qa *** qaa" in exI)
   1.496 +apply (auto simp add: poly_mult fun_eq mult_assoc)
   1.497 +done
   1.498 +
   1.499 +lemma poly_divides_exp: "m \<le> n ==> (p %^ m) divides (p %^ n)"
   1.500 +apply (auto simp add: le_iff_add)
   1.501 +apply (induct_tac k)
   1.502 +apply (rule_tac [2] poly_divides_trans)
   1.503 +apply (auto simp add: divides_def)
   1.504 +apply (rule_tac x = p in exI)
   1.505 +apply (auto simp add: poly_mult fun_eq mult_ac)
   1.506 +done
   1.507 +
   1.508 +lemma poly_exp_divides: "[| (p %^ n) divides q;  m\<le>n |] ==> (p %^ m) divides q"
   1.509 +by (blast intro: poly_divides_exp poly_divides_trans)
   1.510 +
   1.511 +lemma poly_divides_add:
   1.512 +   "[| p divides q; p divides r |] ==> p divides (q +++ r)"
   1.513 +apply (simp add: divides_def, auto)
   1.514 +apply (rule_tac x = "qa +++ qaa" in exI)
   1.515 +apply (auto simp add: poly_add fun_eq poly_mult right_distrib)
   1.516 +done
   1.517 +
   1.518 +lemma poly_divides_diff:
   1.519 +   "[| p divides q; p divides (q +++ r) |] ==> p divides r"
   1.520 +apply (simp add: divides_def, auto)
   1.521 +apply (rule_tac x = "qaa +++ -- qa" in exI)
   1.522 +apply (auto simp add: poly_add fun_eq poly_mult poly_minus right_diff_distrib algebra_simps)
   1.523 +done
   1.524 +
   1.525 +lemma poly_divides_diff2: "[| p divides r; p divides (q +++ r) |] ==> p divides q"
   1.526 +apply (erule poly_divides_diff)
   1.527 +apply (auto simp add: poly_add fun_eq poly_mult divides_def add_ac)
   1.528 +done
   1.529 +
   1.530 +lemma poly_divides_zero: "poly p = poly [] ==> q divides p"
   1.531 +apply (simp add: divides_def)
   1.532 +apply (rule exI[where x="[]"])
   1.533 +apply (auto simp add: fun_eq poly_mult)
   1.534 +done
   1.535 +
   1.536 +lemma poly_divides_zero2: "q divides []"
   1.537 +apply (simp add: divides_def)
   1.538 +apply (rule_tac x = "[]" in exI)
   1.539 +apply (auto simp add: fun_eq)
   1.540 +done
   1.541 +declare poly_divides_zero2 [simp]
   1.542 +
   1.543 +text{*At last, we can consider the order of a root.*}
   1.544 +
   1.545 +
   1.546 +lemma poly_order_exists_lemma [rule_format]:
   1.547 +     "\<forall>p. length p = d --> poly p \<noteq> poly [] 
   1.548 +             --> (\<exists>n q. p = mulexp n [-a, (1::'a::{idom,ring_char_0})] q & poly q a \<noteq> 0)"
   1.549 +apply (induct "d")
   1.550 +apply (simp add: fun_eq, safe)
   1.551 +apply (case_tac "poly p a = 0")
   1.552 +apply (drule_tac poly_linear_divides [THEN iffD1], safe)
   1.553 +apply (drule_tac x = q in spec)
   1.554 +apply (drule_tac poly_entire_neg [THEN iffD1], safe, force) 
   1.555 +apply (rule_tac x = "Suc n" in exI)
   1.556 +apply (rule_tac x = qa in exI)
   1.557 +apply (simp del: pmult_Cons)
   1.558 +apply (rule_tac x = 0 in exI, force) 
   1.559 +done
   1.560 +
   1.561 +(* FIXME: Tidy up *)
   1.562 +lemma poly_order_exists:
   1.563 +     "[| length p = d; poly p \<noteq> poly [] |]
   1.564 +      ==> \<exists>n. ([-a, 1] %^ n) divides p &
   1.565 +                ~(([-a, (1::'a::{idom,ring_char_0})] %^ (Suc n)) divides p)"
   1.566 +apply (drule poly_order_exists_lemma [where a=a], assumption, clarify)  
   1.567 +apply (rule_tac x = n in exI, safe)
   1.568 +apply (unfold divides_def)
   1.569 +apply (rule_tac x = q in exI)
   1.570 +apply (induct_tac "n", simp)
   1.571 +apply (simp (no_asm_simp) add: poly_add poly_cmult poly_mult right_distrib mult_ac)
   1.572 +apply safe
   1.573 +apply (subgoal_tac "poly (mulexp n [- a, 1] q) \<noteq> poly ([- a, 1] %^ Suc n *** qa)") 
   1.574 +apply simp 
   1.575 +apply (induct_tac "n")
   1.576 +apply (simp del: pmult_Cons pexp_Suc)
   1.577 +apply (erule_tac Q = "poly q a = 0" in contrapos_np)
   1.578 +apply (simp add: poly_add poly_cmult)
   1.579 +apply (rule pexp_Suc [THEN ssubst])
   1.580 +apply (rule ccontr)
   1.581 +apply (simp add: poly_mult_left_cancel poly_mult_assoc del: pmult_Cons pexp_Suc)
   1.582 +done
   1.583 +
   1.584 +lemma poly_one_divides: "[1] divides p"
   1.585 +by (simp add: divides_def, auto)
   1.586 +declare poly_one_divides [simp]
   1.587 +
   1.588 +lemma poly_order: "poly p \<noteq> poly []
   1.589 +      ==> EX! n. ([-a, (1::'a::{idom,ring_char_0})] %^ n) divides p &
   1.590 +                 ~(([-a, 1] %^ (Suc n)) divides p)"
   1.591 +apply (auto intro: poly_order_exists simp add: less_linear simp del: pmult_Cons pexp_Suc)
   1.592 +apply (cut_tac x = y and y = n in less_linear)
   1.593 +apply (drule_tac m = n in poly_exp_divides)
   1.594 +apply (auto dest: Suc_le_eq [THEN iffD2, THEN [2] poly_exp_divides]
   1.595 +            simp del: pmult_Cons pexp_Suc)
   1.596 +done
   1.597 +
   1.598 +text{*Order*}
   1.599 +
   1.600 +lemma some1_equalityD: "[| n = (@n. P n); EX! n. P n |] ==> P n"
   1.601 +by (blast intro: someI2)
   1.602 +
   1.603 +lemma order:
   1.604 +      "(([-a, (1::'a::{idom,ring_char_0})] %^ n) divides p &
   1.605 +        ~(([-a, 1] %^ (Suc n)) divides p)) =
   1.606 +        ((n = order a p) & ~(poly p = poly []))"
   1.607 +apply (unfold order_def)
   1.608 +apply (rule iffI)
   1.609 +apply (blast dest: poly_divides_zero intro!: some1_equality [symmetric] poly_order)
   1.610 +apply (blast intro!: poly_order [THEN [2] some1_equalityD])
   1.611 +done
   1.612 +
   1.613 +lemma order2: "[| poly p \<noteq> poly [] |]
   1.614 +      ==> ([-a, (1::'a::{idom,ring_char_0})] %^ (order a p)) divides p &
   1.615 +              ~(([-a, 1] %^ (Suc(order a p))) divides p)"
   1.616 +by (simp add: order del: pexp_Suc)
   1.617 +
   1.618 +lemma order_unique: "[| poly p \<noteq> poly []; ([-a, 1] %^ n) divides p;
   1.619 +         ~(([-a, (1::'a::{idom,ring_char_0})] %^ (Suc n)) divides p)
   1.620 +      |] ==> (n = order a p)"
   1.621 +by (insert order [of a n p], auto) 
   1.622 +
   1.623 +lemma order_unique_lemma: "(poly p \<noteq> poly [] & ([-a, 1] %^ n) divides p &
   1.624 +         ~(([-a, (1::'a::{idom,ring_char_0})] %^ (Suc n)) divides p))
   1.625 +      ==> (n = order a p)"
   1.626 +by (blast intro: order_unique)
   1.627 +
   1.628 +lemma order_poly: "poly p = poly q ==> order a p = order a q"
   1.629 +by (auto simp add: fun_eq divides_def poly_mult order_def)
   1.630 +
   1.631 +lemma pexp_one: "p %^ (Suc 0) = p"
   1.632 +apply (induct "p")
   1.633 +apply (auto simp add: numeral_1_eq_1)
   1.634 +done
   1.635 +declare pexp_one [simp]
   1.636 +
   1.637 +lemma lemma_order_root [rule_format]:
   1.638 +     "\<forall>p a. 0 < n & [- a, 1] %^ n divides p & ~ [- a, 1] %^ (Suc n) divides p
   1.639 +             --> poly p a = 0"
   1.640 +apply (induct "n", blast)
   1.641 +apply (auto simp add: divides_def poly_mult simp del: pmult_Cons)
   1.642 +done
   1.643 +
   1.644 +lemma order_root: "(poly p a = (0::'a::{idom,ring_char_0})) = ((poly p = poly []) | order a p \<noteq> 0)"
   1.645 +apply (case_tac "poly p = poly []", auto)
   1.646 +apply (simp add: poly_linear_divides del: pmult_Cons, safe)
   1.647 +apply (drule_tac [!] a = a in order2)
   1.648 +apply (rule ccontr)
   1.649 +apply (simp add: divides_def poly_mult fun_eq del: pmult_Cons, blast)
   1.650 +using neq0_conv
   1.651 +apply (blast intro: lemma_order_root)
   1.652 +done
   1.653 +
   1.654 +lemma order_divides: "(([-a, 1::'a::{idom,ring_char_0}] %^ n) divides p) = ((poly p = poly []) | n \<le> order a p)"
   1.655 +apply (case_tac "poly p = poly []", auto)
   1.656 +apply (simp add: divides_def fun_eq poly_mult)
   1.657 +apply (rule_tac x = "[]" in exI)
   1.658 +apply (auto dest!: order2 [where a=a]
   1.659 +	    intro: poly_exp_divides simp del: pexp_Suc)
   1.660 +done
   1.661 +
   1.662 +lemma order_decomp:
   1.663 +     "poly p \<noteq> poly []
   1.664 +      ==> \<exists>q. (poly p = poly (([-a, 1] %^ (order a p)) *** q)) &
   1.665 +                ~([-a, 1::'a::{idom,ring_char_0}] divides q)"
   1.666 +apply (unfold divides_def)
   1.667 +apply (drule order2 [where a = a])
   1.668 +apply (simp add: divides_def del: pexp_Suc pmult_Cons, safe)
   1.669 +apply (rule_tac x = q in exI, safe)
   1.670 +apply (drule_tac x = qa in spec)
   1.671 +apply (auto simp add: poly_mult fun_eq poly_exp mult_ac simp del: pmult_Cons)
   1.672 +done
   1.673 +
   1.674 +text{*Important composition properties of orders.*}
   1.675 +
   1.676 +lemma order_mult: "poly (p *** q) \<noteq> poly []
   1.677 +      ==> order a (p *** q) = order a p + order (a::'a::{idom,ring_char_0}) q"
   1.678 +apply (cut_tac a = a and p = "p***q" and n = "order a p + order a q" in order)
   1.679 +apply (auto simp add: poly_entire simp del: pmult_Cons)
   1.680 +apply (drule_tac a = a in order2)+
   1.681 +apply safe
   1.682 +apply (simp add: divides_def fun_eq poly_exp_add poly_mult del: pmult_Cons, safe)
   1.683 +apply (rule_tac x = "qa *** qaa" in exI)
   1.684 +apply (simp add: poly_mult mult_ac del: pmult_Cons)
   1.685 +apply (drule_tac a = a in order_decomp)+
   1.686 +apply safe
   1.687 +apply (subgoal_tac "[-a,1] divides (qa *** qaa) ")
   1.688 +apply (simp add: poly_primes del: pmult_Cons)
   1.689 +apply (auto simp add: divides_def simp del: pmult_Cons)
   1.690 +apply (rule_tac x = qb in exI)
   1.691 +apply (subgoal_tac "poly ([-a, 1] %^ (order a p) *** (qa *** qaa)) = poly ([-a, 1] %^ (order a p) *** ([-a, 1] *** qb))")
   1.692 +apply (drule poly_mult_left_cancel [THEN iffD1], force)
   1.693 +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.694 +apply (drule poly_mult_left_cancel [THEN iffD1], force)
   1.695 +apply (simp add: fun_eq poly_exp_add poly_mult mult_ac del: pmult_Cons)
   1.696 +done
   1.697 +
   1.698 +
   1.699 +
   1.700 +lemma order_root2: "poly p \<noteq> poly [] ==> (poly p a = 0) = (order (a::'a::{idom,ring_char_0}) p \<noteq> 0)"
   1.701 +by (rule order_root [THEN ssubst], auto)
   1.702 +
   1.703 +
   1.704 +lemma pmult_one: "[1] *** p = p"
   1.705 +by auto
   1.706 +declare pmult_one [simp]
   1.707 +
   1.708 +lemma poly_Nil_zero: "poly [] = poly [0]"
   1.709 +by (simp add: fun_eq)
   1.710 +
   1.711 +lemma rsquarefree_decomp:
   1.712 +     "[| rsquarefree p; poly p a = (0::'a::{idom,ring_char_0}) |]
   1.713 +      ==> \<exists>q. (poly p = poly ([-a, 1] *** q)) & poly q a \<noteq> 0"
   1.714 +apply (simp add: rsquarefree_def, safe)
   1.715 +apply (frule_tac a = a in order_decomp)
   1.716 +apply (drule_tac x = a in spec)
   1.717 +apply (drule_tac a = a in order_root2 [symmetric])
   1.718 +apply (auto simp del: pmult_Cons)
   1.719 +apply (rule_tac x = q in exI, safe)
   1.720 +apply (simp add: poly_mult fun_eq)
   1.721 +apply (drule_tac p1 = q in poly_linear_divides [THEN iffD1])
   1.722 +apply (simp add: divides_def del: pmult_Cons, safe)
   1.723 +apply (drule_tac x = "[]" in spec)
   1.724 +apply (auto simp add: fun_eq)
   1.725 +done
   1.726 +
   1.727 +
   1.728 +text{*Normalization of a polynomial.*}
   1.729 +
   1.730 +lemma poly_normalize: "poly (pnormalize p) = poly p"
   1.731 +apply (induct "p")
   1.732 +apply (auto simp add: fun_eq)
   1.733 +done
   1.734 +declare poly_normalize [simp]
   1.735 +
   1.736 +
   1.737 +text{*The degree of a polynomial.*}
   1.738 +
   1.739 +lemma lemma_degree_zero:
   1.740 +     "list_all (%c. c = 0) p \<longleftrightarrow>  pnormalize p = []"
   1.741 +by (induct "p", auto)
   1.742 +
   1.743 +lemma degree_zero: "(poly p = poly ([]:: (('a::{idom,ring_char_0}) list))) \<Longrightarrow> (degree p = 0)"
   1.744 +apply (simp add: degree_def)
   1.745 +apply (case_tac "pnormalize p = []")
   1.746 +apply (auto simp add: poly_zero lemma_degree_zero )
   1.747 +done
   1.748 +
   1.749 +lemma pnormalize_sing: "(pnormalize [x] = [x]) \<longleftrightarrow> x \<noteq> 0" by simp
   1.750 +lemma pnormalize_pair: "y \<noteq> 0 \<longleftrightarrow> (pnormalize [x, y] = [x, y])" by simp
   1.751 +lemma pnormal_cons: "pnormal p \<Longrightarrow> pnormal (c#p)" 
   1.752 +  unfolding pnormal_def by simp
   1.753 +lemma pnormal_tail: "p\<noteq>[] \<Longrightarrow> pnormal (c#p) \<Longrightarrow> pnormal p"
   1.754 +  unfolding pnormal_def 
   1.755 +  apply (cases "pnormalize p = []", auto)
   1.756 +  by (cases "c = 0", auto)
   1.757 +lemma pnormal_last_nonzero: "pnormal p ==> last p \<noteq> 0"
   1.758 +  apply (induct p, auto simp add: pnormal_def)
   1.759 +  apply (case_tac "pnormalize p = []", auto)
   1.760 +  by (case_tac "a=0", auto)
   1.761 +lemma  pnormal_length: "pnormal p \<Longrightarrow> 0 < length p"
   1.762 +  unfolding pnormal_def length_greater_0_conv by blast
   1.763 +lemma pnormal_last_length: "\<lbrakk>0 < length p ; last p \<noteq> 0\<rbrakk> \<Longrightarrow> pnormal p"
   1.764 +  apply (induct p, auto)
   1.765 +  apply (case_tac "p = []", auto)
   1.766 +  apply (simp add: pnormal_def)
   1.767 +  by (rule pnormal_cons, auto)
   1.768 +lemma pnormal_id: "pnormal p \<longleftrightarrow> (0 < length p \<and> last p \<noteq> 0)"
   1.769 +  using pnormal_last_length pnormal_length pnormal_last_nonzero by blast
   1.770 +
   1.771 +text{*Tidier versions of finiteness of roots.*}
   1.772 +
   1.773 +lemma poly_roots_finite_set: "poly p \<noteq> poly [] ==> finite {x::'a::{idom,ring_char_0}. poly p x = 0}"
   1.774 +unfolding poly_roots_finite .
   1.775 +
   1.776 +text{*bound for polynomial.*}
   1.777 +
   1.778 +lemma poly_mono: "abs(x) \<le> k ==> abs(poly p (x::'a::{ordered_idom})) \<le> poly (map abs p) k"
   1.779 +apply (induct "p", auto)
   1.780 +apply (rule_tac y = "abs a + abs (x * poly p x)" in order_trans)
   1.781 +apply (rule abs_triangle_ineq)
   1.782 +apply (auto intro!: mult_mono simp add: abs_mult)
   1.783 +done
   1.784 +
   1.785 +lemma poly_Sing: "poly [c] x = c" by simp
   1.786 +end