src/HOL/Decision_Procs/Polynomial_List.thy
author haftmann
Mon Apr 26 15:37:50 2010 +0200 (2010-04-26)
changeset 36409 d323e7773aa8
parent 36350 bc7982c54e37
child 37598 893dcabf0c04
permissions -rw-r--r--
use new classes (linordered_)field_inverse_zero
     1 (*  Title:      HOL/Decision_Procs/Polynomial_List.thy
     2     Author:     Amine Chaieb
     3 *)
     4 
     5 header {* Univariate Polynomials as Lists *}
     6 
     7 theory Polynomial_List
     8 imports Main
     9 begin
    10 
    11 text{* Application of polynomial as a real function. *}
    12 
    13 consts poly :: "'a list => 'a  => ('a::{comm_ring})"
    14 primrec
    15   poly_Nil:  "poly [] x = 0"
    16   poly_Cons: "poly (h#t) x = h + x * poly t x"
    17 
    18 
    19 subsection{*Arithmetic Operations on Polynomials*}
    20 
    21 text{*addition*}
    22 consts padd :: "['a list, 'a list] => ('a::comm_ring_1) list"  (infixl "+++" 65)
    23 primrec
    24   padd_Nil:  "[] +++ l2 = l2"
    25   padd_Cons: "(h#t) +++ l2 = (if l2 = [] then h#t
    26                             else (h + hd l2)#(t +++ tl l2))"
    27 
    28 text{*Multiplication by a constant*}
    29 consts cmult :: "['a :: comm_ring_1, 'a list] => 'a list"  (infixl "%*" 70)
    30 primrec
    31    cmult_Nil:  "c %* [] = []"
    32    cmult_Cons: "c %* (h#t) = (c * h)#(c %* t)"
    33 
    34 text{*Multiplication by a polynomial*}
    35 consts pmult :: "['a list, 'a list] => ('a::comm_ring_1) list"  (infixl "***" 70)
    36 primrec
    37    pmult_Nil:  "[] *** l2 = []"
    38    pmult_Cons: "(h#t) *** l2 = (if t = [] then h %* l2
    39                               else (h %* l2) +++ ((0) # (t *** l2)))"
    40 
    41 text{*Repeated multiplication by a polynomial*}
    42 consts mulexp :: "[nat, 'a list, 'a  list] => ('a ::comm_ring_1) list"
    43 primrec
    44    mulexp_zero:  "mulexp 0 p q = q"
    45    mulexp_Suc:   "mulexp (Suc n) p q = p *** mulexp n p q"
    46 
    47 text{*Exponential*}
    48 consts pexp :: "['a list, nat] => ('a::comm_ring_1) list"  (infixl "%^" 80)
    49 primrec
    50    pexp_0:   "p %^ 0 = [1]"
    51    pexp_Suc: "p %^ (Suc n) = p *** (p %^ n)"
    52 
    53 text{*Quotient related value of dividing a polynomial by x + a*}
    54 (* Useful for divisor properties in inductive proofs *)
    55 consts "pquot" :: "['a list, 'a::field] => 'a list"
    56 primrec
    57    pquot_Nil:  "pquot [] a= []"
    58    pquot_Cons: "pquot (h#t) a = (if t = [] then [h]
    59                    else (inverse(a) * (h - hd( pquot t a)))#(pquot t a))"
    60 
    61 
    62 text{*normalization of polynomials (remove extra 0 coeff)*}
    63 consts pnormalize :: "('a::comm_ring_1) list => 'a list"
    64 primrec
    65    pnormalize_Nil:  "pnormalize [] = []"
    66    pnormalize_Cons: "pnormalize (h#p) = (if ( (pnormalize p) = [])
    67                                      then (if (h = 0) then [] else [h])
    68                                      else (h#(pnormalize p)))"
    69 
    70 definition "pnormal p = ((pnormalize p = p) \<and> p \<noteq> [])"
    71 definition "nonconstant p = (pnormal p \<and> (\<forall>x. p \<noteq> [x]))"
    72 text{*Other definitions*}
    73 
    74 definition
    75   poly_minus :: "'a list => ('a :: comm_ring_1) list"      ("-- _" [80] 80) where
    76   "-- p = (- 1) %* p"
    77 
    78 definition
    79   divides :: "[('a::comm_ring_1) list, 'a list] => bool"  (infixl "divides" 70) where
    80   "p1 divides p2 = (\<exists>q. poly p2 = poly(p1 *** q))"
    81 
    82 definition
    83   order :: "('a::comm_ring_1) => 'a list => nat" where
    84     --{*order of a polynomial*}
    85   "order a p = (SOME n. ([-a, 1] %^ n) divides p &
    86                       ~ (([-a, 1] %^ (Suc n)) divides p))"
    87 
    88 definition
    89   degree :: "('a::comm_ring_1) list => nat" where
    90      --{*degree of a polynomial*}
    91   "degree p = length (pnormalize p) - 1"
    92 
    93 definition
    94   rsquarefree :: "('a::comm_ring_1) list => bool" where
    95      --{*squarefree polynomials --- NB with respect to real roots only.*}
    96   "rsquarefree p = (poly p \<noteq> poly [] &
    97                      (\<forall>a. (order a p = 0) | (order a p = 1)))"
    98 
    99 lemma padd_Nil2: "p +++ [] = p"
   100 by (induct p) auto
   101 declare padd_Nil2 [simp]
   102 
   103 lemma padd_Cons_Cons: "(h1 # p1) +++ (h2 # p2) = (h1 + h2) # (p1 +++ p2)"
   104 by auto
   105 
   106 lemma pminus_Nil: "-- [] = []"
   107 by (simp add: poly_minus_def)
   108 declare pminus_Nil [simp]
   109 
   110 lemma pmult_singleton: "[h1] *** p1 = h1 %* p1"
   111 by simp
   112 
   113 lemma poly_ident_mult: "1 %* t = t"
   114 by (induct "t", auto)
   115 declare poly_ident_mult [simp]
   116 
   117 lemma poly_simple_add_Cons: "[a] +++ ((0)#t) = (a#t)"
   118 by simp
   119 declare poly_simple_add_Cons [simp]
   120 
   121 text{*Handy general properties*}
   122 
   123 lemma padd_commut: "b +++ a = a +++ b"
   124 apply (subgoal_tac "\<forall>a. b +++ a = a +++ b")
   125 apply (induct_tac [2] "b", auto)
   126 apply (rule padd_Cons [THEN ssubst])
   127 apply (case_tac "aa", auto)
   128 done
   129 
   130 lemma padd_assoc [rule_format]: "\<forall>b c. (a +++ b) +++ c = a +++ (b +++ c)"
   131 apply (induct "a", simp, clarify)
   132 apply (case_tac b, simp_all)
   133 done
   134 
   135 lemma poly_cmult_distr [rule_format]:
   136      "\<forall>q. a %* ( p +++ q) = (a %* p +++ a %* q)"
   137 apply (induct "p", simp, clarify) 
   138 apply (case_tac "q")
   139 apply (simp_all add: right_distrib)
   140 done
   141 
   142 lemma pmult_by_x[simp]: "[0, 1] *** t = ((0)#t)"
   143 apply (induct "t", simp)
   144 by (auto simp add: mult_zero_left poly_ident_mult padd_commut)
   145 
   146 
   147 text{*properties of evaluation of polynomials.*}
   148 
   149 lemma poly_add: "poly (p1 +++ p2) x = poly p1 x + poly p2 x"
   150 apply (subgoal_tac "\<forall>p2. poly (p1 +++ p2) x = poly (p1) x + poly (p2) x")
   151 apply (induct_tac [2] "p1", auto)
   152 apply (case_tac "p2")
   153 apply (auto simp add: right_distrib)
   154 done
   155 
   156 lemma poly_cmult: "poly (c %* p) x = c * poly p x"
   157 apply (induct "p") 
   158 apply (case_tac [2] "x=0")
   159 apply (auto simp add: right_distrib mult_ac)
   160 done
   161 
   162 lemma poly_minus: "poly (-- p) x = - (poly p x)"
   163 apply (simp add: poly_minus_def)
   164 apply (auto simp add: poly_cmult)
   165 done
   166 
   167 lemma poly_mult: "poly (p1 *** p2) x = poly p1 x * poly p2 x"
   168 apply (subgoal_tac "\<forall>p2. poly (p1 *** p2) x = poly p1 x * poly p2 x")
   169 apply (simp (no_asm_simp))
   170 apply (induct "p1")
   171 apply (auto simp add: poly_cmult)
   172 apply (case_tac p1)
   173 apply (auto simp add: poly_cmult poly_add left_distrib right_distrib mult_ac)
   174 done
   175 
   176 lemma poly_exp: "poly (p %^ n) (x::'a::comm_ring_1) = (poly p x) ^ n"
   177 apply (induct "n")
   178 apply (auto simp add: poly_cmult poly_mult power_Suc)
   179 done
   180 
   181 text{*More Polynomial Evaluation Lemmas*}
   182 
   183 lemma poly_add_rzero: "poly (a +++ []) x = poly a x"
   184 by simp
   185 declare poly_add_rzero [simp]
   186 
   187 lemma poly_mult_assoc: "poly ((a *** b) *** c) x = poly (a *** (b *** c)) x"
   188   by (simp add: poly_mult mult_assoc)
   189 
   190 lemma poly_mult_Nil2: "poly (p *** []) x = 0"
   191 by (induct "p", auto)
   192 declare poly_mult_Nil2 [simp]
   193 
   194 lemma poly_exp_add: "poly (p %^ (n + d)) x = poly( p %^ n *** p %^ d) x"
   195 apply (induct "n")
   196 apply (auto simp add: poly_mult mult_assoc)
   197 done
   198 
   199 subsection{*Key Property: if @{term "f(a) = 0"} then @{term "(x - a)"} divides
   200  @{term "p(x)"} *}
   201 
   202 lemma lemma_poly_linear_rem: "\<forall>h. \<exists>q r. h#t = [r] +++ [-a, 1] *** q"
   203 apply (induct "t", safe)
   204 apply (rule_tac x = "[]" in exI)
   205 apply (rule_tac x = h in exI, simp)
   206 apply (drule_tac x = aa in spec, safe)
   207 apply (rule_tac x = "r#q" in exI)
   208 apply (rule_tac x = "a*r + h" in exI)
   209 apply (case_tac "q", auto)
   210 done
   211 
   212 lemma poly_linear_rem: "\<exists>q r. h#t = [r] +++ [-a, 1] *** q"
   213 by (cut_tac t = t and a = a in lemma_poly_linear_rem, auto)
   214 
   215 
   216 lemma poly_linear_divides: "(poly p a = 0) = ((p = []) | (\<exists>q. p = [-a, 1] *** q))"
   217 apply (auto simp add: poly_add poly_cmult right_distrib)
   218 apply (case_tac "p", simp) 
   219 apply (cut_tac h = aa and t = list and a = a in poly_linear_rem, safe)
   220 apply (case_tac "q", auto)
   221 apply (drule_tac x = "[]" in spec, simp)
   222 apply (auto simp add: poly_add poly_cmult add_assoc)
   223 apply (drule_tac x = "aa#lista" in spec, auto)
   224 done
   225 
   226 lemma lemma_poly_length_mult: "\<forall>h k a. length (k %* p +++  (h # (a %* p))) = Suc (length p)"
   227 by (induct "p", auto)
   228 declare lemma_poly_length_mult [simp]
   229 
   230 lemma lemma_poly_length_mult2: "\<forall>h k. length (k %* p +++  (h # p)) = Suc (length p)"
   231 by (induct "p", auto)
   232 declare lemma_poly_length_mult2 [simp]
   233 
   234 lemma poly_length_mult: "length([-a,1] *** q) = Suc (length q)"
   235 by auto
   236 declare poly_length_mult [simp]
   237 
   238 
   239 subsection{*Polynomial length*}
   240 
   241 lemma poly_cmult_length: "length (a %* p) = length p"
   242 by (induct "p", auto)
   243 declare poly_cmult_length [simp]
   244 
   245 lemma poly_add_length [rule_format]:
   246      "\<forall>p2. length (p1 +++ p2) =
   247              (if (length p1 < length p2) then length p2 else length p1)"
   248 apply (induct "p1", simp_all)
   249 apply arith
   250 done
   251 
   252 lemma poly_root_mult_length: "length([a,b] *** p) = Suc (length p)"
   253 by (simp add: poly_cmult_length poly_add_length)
   254 declare poly_root_mult_length [simp]
   255 
   256 lemma poly_mult_not_eq_poly_Nil: "(poly (p *** q) x \<noteq> poly [] x) =
   257       (poly p x \<noteq> poly [] x & poly q x \<noteq> poly [] (x::'a::idom))"
   258 apply (auto simp add: poly_mult)
   259 done
   260 declare poly_mult_not_eq_poly_Nil [simp]
   261 
   262 lemma poly_mult_eq_zero_disj: "(poly (p *** q) (x::'a::idom) = 0) = (poly p x = 0 | poly q x = 0)"
   263 by (auto simp add: poly_mult)
   264 
   265 text{*Normalisation Properties*}
   266 
   267 lemma poly_normalized_nil: "(pnormalize p = []) --> (poly p x = 0)"
   268 by (induct "p", auto)
   269 
   270 text{*A nontrivial polynomial of degree n has no more than n roots*}
   271 
   272 lemma poly_roots_index_lemma0 [rule_format]:
   273    "\<forall>p x. poly p x \<noteq> poly [] x & length p = n
   274     --> (\<exists>i. \<forall>x. (poly p x = (0::'a::idom)) --> (\<exists>m. (m \<le> n & x = i m)))"
   275 apply (induct "n", safe)
   276 apply (rule ccontr)
   277 apply (subgoal_tac "\<exists>a. poly p a = 0", safe)
   278 apply (drule poly_linear_divides [THEN iffD1], safe)
   279 apply (drule_tac x = q in spec)
   280 apply (drule_tac x = x in spec)
   281 apply (simp del: poly_Nil pmult_Cons)
   282 apply (erule exE)
   283 apply (drule_tac x = "%m. if m = Suc n then a else i m" in spec, safe)
   284 apply (drule poly_mult_eq_zero_disj [THEN iffD1], safe)
   285 apply (drule_tac x = "Suc (length q)" in spec)
   286 apply (auto simp add: field_simps)
   287 apply (drule_tac x = xa in spec)
   288 apply (clarsimp simp add: field_simps)
   289 apply (drule_tac x = m in spec)
   290 apply (auto simp add:field_simps)
   291 done
   292 lemmas poly_roots_index_lemma1 = conjI [THEN poly_roots_index_lemma0, standard]
   293 
   294 lemma poly_roots_index_length0: "poly p (x::'a::idom) \<noteq> poly [] x ==>
   295       \<exists>i. \<forall>x. (poly p x = 0) --> (\<exists>n. n \<le> length p & x = i n)"
   296 by (blast intro: poly_roots_index_lemma1)
   297 
   298 lemma poly_roots_finite_lemma: "poly p (x::'a::idom) \<noteq> poly [] x ==>
   299       \<exists>N i. \<forall>x. (poly p x = 0) --> (\<exists>n. (n::nat) < N & x = i n)"
   300 apply (drule poly_roots_index_length0, safe)
   301 apply (rule_tac x = "Suc (length p)" in exI)
   302 apply (rule_tac x = i in exI) 
   303 apply (simp add: less_Suc_eq_le)
   304 done
   305 
   306 
   307 lemma real_finite_lemma:
   308   assumes P: "\<forall>x. P x --> (\<exists>n. n < length j & x = j!n)"
   309   shows "finite {(x::'a::idom). P x}"
   310 proof-
   311   let ?M = "{x. P x}"
   312   let ?N = "set j"
   313   have "?M \<subseteq> ?N" using P by auto
   314   thus ?thesis using finite_subset by auto
   315 qed
   316 
   317 lemma poly_roots_index_lemma [rule_format]:
   318    "\<forall>p x. poly p x \<noteq> poly [] x & length p = n
   319     --> (\<exists>i. \<forall>x. (poly p x = (0::'a::{idom})) --> x \<in> set i)"
   320 apply (induct "n", safe)
   321 apply (rule ccontr)
   322 apply (subgoal_tac "\<exists>a. poly p a = 0", safe)
   323 apply (drule poly_linear_divides [THEN iffD1], safe)
   324 apply (drule_tac x = q in spec)
   325 apply (drule_tac x = x in spec)
   326 apply (auto simp del: poly_Nil pmult_Cons)
   327 apply (drule_tac x = "a#i" in spec)
   328 apply (auto simp only: poly_mult List.list.size)
   329 apply (drule_tac x = xa in spec)
   330 apply (clarsimp simp add: field_simps)
   331 done
   332 
   333 lemmas poly_roots_index_lemma2 = conjI [THEN poly_roots_index_lemma, standard]
   334 
   335 lemma poly_roots_index_length: "poly p (x::'a::idom) \<noteq> poly [] x ==>
   336       \<exists>i. \<forall>x. (poly p x = 0) --> x \<in> set i"
   337 by (blast intro: poly_roots_index_lemma2)
   338 
   339 lemma poly_roots_finite_lemma': "poly p (x::'a::idom) \<noteq> poly [] x ==>
   340       \<exists>i. \<forall>x. (poly p x = 0) --> x \<in> set i"
   341 by (drule poly_roots_index_length, safe)
   342 
   343 lemma UNIV_nat_infinite: "\<not> finite (UNIV :: nat set)"
   344   unfolding finite_conv_nat_seg_image
   345 proof(auto simp add: expand_set_eq image_iff)
   346   fix n::nat and f:: "nat \<Rightarrow> nat"
   347   let ?N = "{i. i < n}"
   348   let ?fN = "f ` ?N"
   349   let ?y = "Max ?fN + 1"
   350   from nat_seg_image_imp_finite[of "?fN" "f" n] 
   351   have thfN: "finite ?fN" by simp
   352   {assume "n =0" hence "\<exists>x. \<forall>xa<n. x \<noteq> f xa" by auto}
   353   moreover
   354   {assume nz: "n \<noteq> 0"
   355     hence thne: "?fN \<noteq> {}" by (auto simp add: neq0_conv)
   356     have "\<forall>x\<in> ?fN. Max ?fN \<ge> x" using nz Max_ge_iff[OF thfN thne] by auto
   357     hence "\<forall>x\<in> ?fN. ?y > x" by (auto simp add: less_Suc_eq_le)
   358     hence "?y \<notin> ?fN" by auto
   359     hence "\<exists>x. \<forall>xa<n. x \<noteq> f xa" by auto }
   360   ultimately show "\<exists>x. \<forall>xa<n. x \<noteq> f xa" by blast
   361 qed
   362 
   363 lemma UNIV_ring_char_0_infinte: "\<not> finite (UNIV:: ('a::ring_char_0) set)"
   364 proof
   365   assume F: "finite (UNIV :: 'a set)"
   366   have th0: "of_nat ` UNIV \<subseteq> (UNIV:: 'a set)" by simp
   367   from finite_subset[OF th0 F] have th: "finite (of_nat ` UNIV :: 'a set)" .
   368   have th': "inj_on (of_nat::nat \<Rightarrow> 'a) (UNIV)"
   369     unfolding inj_on_def by auto
   370   from finite_imageD[OF th th'] UNIV_nat_infinite 
   371   show False by blast
   372 qed
   373 
   374 lemma poly_roots_finite: "(poly p \<noteq> poly []) = 
   375   finite {x. poly p x = (0::'a::{idom, ring_char_0})}"
   376 proof
   377   assume H: "poly p \<noteq> poly []"
   378   show "finite {x. poly p x = (0::'a)}"
   379     using H
   380     apply -
   381     apply (erule contrapos_np, rule ext)
   382     apply (rule ccontr)
   383     apply (clarify dest!: poly_roots_finite_lemma')
   384     using finite_subset
   385   proof-
   386     fix x i
   387     assume F: "\<not> finite {x. poly p x = (0\<Colon>'a)}" 
   388       and P: "\<forall>x. poly p x = (0\<Colon>'a) \<longrightarrow> x \<in> set i"
   389     let ?M= "{x. poly p x = (0\<Colon>'a)}"
   390     from P have "?M \<subseteq> set i" by auto
   391     with finite_subset F show False by auto
   392   qed
   393 next
   394   assume F: "finite {x. poly p x = (0\<Colon>'a)}"
   395   show "poly p \<noteq> poly []" using F UNIV_ring_char_0_infinte by auto  
   396 qed
   397 
   398 text{*Entirety and Cancellation for polynomials*}
   399 
   400 lemma poly_entire_lemma: "[| poly (p:: ('a::{idom,ring_char_0}) list) \<noteq> poly [] ; poly q \<noteq> poly [] |]
   401       ==>  poly (p *** q) \<noteq> poly []"
   402 by (auto simp add: poly_roots_finite poly_mult Collect_disj_eq)
   403 
   404 lemma poly_entire: "(poly (p *** q) = poly ([]::('a::{idom,ring_char_0}) list)) = ((poly p = poly []) | (poly q = poly []))"
   405 apply (auto intro: ext dest: fun_cong simp add: poly_entire_lemma poly_mult)
   406 apply (blast intro: ccontr dest: poly_entire_lemma poly_mult [THEN subst])
   407 done
   408 
   409 lemma poly_entire_neg: "(poly (p *** q) \<noteq> poly ([]::('a::{idom,ring_char_0}) list)) = ((poly p \<noteq> poly []) & (poly q \<noteq> poly []))"
   410 by (simp add: poly_entire)
   411 
   412 lemma fun_eq: " (f = g) = (\<forall>x. f x = g x)"
   413 by (auto intro!: ext)
   414 
   415 lemma poly_add_minus_zero_iff: "(poly (p +++ -- q) = poly []) = (poly p = poly q)"
   416 by (auto simp add: field_simps poly_add poly_minus_def fun_eq poly_cmult)
   417 
   418 lemma poly_add_minus_mult_eq: "poly (p *** q +++ --(p *** r)) = poly (p *** (q +++ -- r))"
   419 by (auto simp add: poly_add poly_minus_def fun_eq poly_mult poly_cmult right_distrib)
   420 
   421 lemma poly_mult_left_cancel: "(poly (p *** q) = poly (p *** r)) = (poly p = poly ([]::('a::{idom, ring_char_0}) list) | poly q = poly r)"
   422 apply (rule_tac p1 = "p *** q" in poly_add_minus_zero_iff [THEN subst])
   423 apply (auto intro: ext simp add: poly_add_minus_mult_eq poly_entire poly_add_minus_zero_iff)
   424 done
   425 
   426 lemma poly_exp_eq_zero:
   427      "(poly (p %^ n) = poly ([]::('a::idom) list)) = (poly p = poly [] & n \<noteq> 0)"
   428 apply (simp only: fun_eq add: all_simps [symmetric]) 
   429 apply (rule arg_cong [where f = All]) 
   430 apply (rule ext)
   431 apply (induct_tac "n")
   432 apply (auto simp add: poly_mult)
   433 done
   434 declare poly_exp_eq_zero [simp]
   435 
   436 lemma poly_prime_eq_zero: "poly [a,(1::'a::comm_ring_1)] \<noteq> poly []"
   437 apply (simp add: fun_eq)
   438 apply (rule_tac x = "1 - a" in exI, simp)
   439 done
   440 declare poly_prime_eq_zero [simp]
   441 
   442 lemma poly_exp_prime_eq_zero: "(poly ([a, (1::'a::idom)] %^ n) \<noteq> poly [])"
   443 by auto
   444 declare poly_exp_prime_eq_zero [simp]
   445 
   446 text{*A more constructive notion of polynomials being trivial*}
   447 
   448 lemma poly_zero_lemma': "poly (h # t) = poly [] ==> h = (0::'a::{idom,ring_char_0}) & poly t = poly []"
   449 apply(simp add: fun_eq)
   450 apply (case_tac "h = 0")
   451 apply (drule_tac [2] x = 0 in spec, auto) 
   452 apply (case_tac "poly t = poly []", simp) 
   453 proof-
   454   fix x
   455   assume H: "\<forall>x. x = (0\<Colon>'a) \<or> poly t x = (0\<Colon>'a)"  and pnz: "poly t \<noteq> poly []"
   456   let ?S = "{x. poly t x = 0}"
   457   from H have "\<forall>x. x \<noteq>0 \<longrightarrow> poly t x = 0" by blast
   458   hence th: "?S \<supseteq> UNIV - {0}" by auto
   459   from poly_roots_finite pnz have th': "finite ?S" by blast
   460   from finite_subset[OF th th'] UNIV_ring_char_0_infinte[where ?'a = 'a]
   461   show "poly t x = (0\<Colon>'a)" by simp
   462   qed
   463 
   464 lemma poly_zero: "(poly p = poly []) = list_all (%c. c = (0::'a::{idom,ring_char_0})) p"
   465 apply (induct "p", simp)
   466 apply (rule iffI)
   467 apply (drule poly_zero_lemma', auto)
   468 done
   469 
   470 
   471 
   472 text{*Basics of divisibility.*}
   473 
   474 lemma poly_primes: "([a, (1::'a::idom)] divides (p *** q)) = ([a, 1] divides p | [a, 1] divides q)"
   475 apply (auto simp add: divides_def fun_eq poly_mult poly_add poly_cmult left_distrib [symmetric])
   476 apply (drule_tac x = "-a" in spec)
   477 apply (auto simp add: poly_linear_divides poly_add poly_cmult left_distrib [symmetric])
   478 apply (rule_tac x = "qa *** q" in exI)
   479 apply (rule_tac [2] x = "p *** qa" in exI)
   480 apply (auto simp add: poly_add poly_mult poly_cmult mult_ac)
   481 done
   482 
   483 lemma poly_divides_refl: "p divides p"
   484 apply (simp add: divides_def)
   485 apply (rule_tac x = "[1]" in exI)
   486 apply (auto simp add: poly_mult fun_eq)
   487 done
   488 declare poly_divides_refl [simp]
   489 
   490 lemma poly_divides_trans: "[| p divides q; q divides r |] ==> p divides r"
   491 apply (simp add: divides_def, safe)
   492 apply (rule_tac x = "qa *** qaa" in exI)
   493 apply (auto simp add: poly_mult fun_eq mult_assoc)
   494 done
   495 
   496 lemma poly_divides_exp: "m \<le> n ==> (p %^ m) divides (p %^ n)"
   497 apply (auto simp add: le_iff_add)
   498 apply (induct_tac k)
   499 apply (rule_tac [2] poly_divides_trans)
   500 apply (auto simp add: divides_def)
   501 apply (rule_tac x = p in exI)
   502 apply (auto simp add: poly_mult fun_eq mult_ac)
   503 done
   504 
   505 lemma poly_exp_divides: "[| (p %^ n) divides q;  m\<le>n |] ==> (p %^ m) divides q"
   506 by (blast intro: poly_divides_exp poly_divides_trans)
   507 
   508 lemma poly_divides_add:
   509    "[| p divides q; p divides r |] ==> p divides (q +++ r)"
   510 apply (simp add: divides_def, auto)
   511 apply (rule_tac x = "qa +++ qaa" in exI)
   512 apply (auto simp add: poly_add fun_eq poly_mult right_distrib)
   513 done
   514 
   515 lemma poly_divides_diff:
   516    "[| p divides q; p divides (q +++ r) |] ==> p divides r"
   517 apply (simp add: divides_def, auto)
   518 apply (rule_tac x = "qaa +++ -- qa" in exI)
   519 apply (auto simp add: poly_add fun_eq poly_mult poly_minus right_diff_distrib algebra_simps)
   520 done
   521 
   522 lemma poly_divides_diff2: "[| p divides r; p divides (q +++ r) |] ==> p divides q"
   523 apply (erule poly_divides_diff)
   524 apply (auto simp add: poly_add fun_eq poly_mult divides_def add_ac)
   525 done
   526 
   527 lemma poly_divides_zero: "poly p = poly [] ==> q divides p"
   528 apply (simp add: divides_def)
   529 apply (rule exI[where x="[]"])
   530 apply (auto simp add: fun_eq poly_mult)
   531 done
   532 
   533 lemma poly_divides_zero2: "q divides []"
   534 apply (simp add: divides_def)
   535 apply (rule_tac x = "[]" in exI)
   536 apply (auto simp add: fun_eq)
   537 done
   538 declare poly_divides_zero2 [simp]
   539 
   540 text{*At last, we can consider the order of a root.*}
   541 
   542 
   543 lemma poly_order_exists_lemma [rule_format]:
   544      "\<forall>p. length p = d --> poly p \<noteq> poly [] 
   545              --> (\<exists>n q. p = mulexp n [-a, (1::'a::{idom,ring_char_0})] q & poly q a \<noteq> 0)"
   546 apply (induct "d")
   547 apply (simp add: fun_eq, safe)
   548 apply (case_tac "poly p a = 0")
   549 apply (drule_tac poly_linear_divides [THEN iffD1], safe)
   550 apply (drule_tac x = q in spec)
   551 apply (drule_tac poly_entire_neg [THEN iffD1], safe, force) 
   552 apply (rule_tac x = "Suc n" in exI)
   553 apply (rule_tac x = qa in exI)
   554 apply (simp del: pmult_Cons)
   555 apply (rule_tac x = 0 in exI, force) 
   556 done
   557 
   558 (* FIXME: Tidy up *)
   559 lemma poly_order_exists:
   560      "[| length p = d; poly p \<noteq> poly [] |]
   561       ==> \<exists>n. ([-a, 1] %^ n) divides p &
   562                 ~(([-a, (1::'a::{idom,ring_char_0})] %^ (Suc n)) divides p)"
   563 apply (drule poly_order_exists_lemma [where a=a], assumption, clarify)  
   564 apply (rule_tac x = n in exI, safe)
   565 apply (unfold divides_def)
   566 apply (rule_tac x = q in exI)
   567 apply (induct_tac "n", simp)
   568 apply (simp (no_asm_simp) add: poly_add poly_cmult poly_mult right_distrib mult_ac)
   569 apply safe
   570 apply (subgoal_tac "poly (mulexp n [- a, 1] q) \<noteq> poly ([- a, 1] %^ Suc n *** qa)") 
   571 apply simp 
   572 apply (induct_tac "n")
   573 apply (simp del: pmult_Cons pexp_Suc)
   574 apply (erule_tac Q = "poly q a = 0" in contrapos_np)
   575 apply (simp add: poly_add poly_cmult)
   576 apply (rule pexp_Suc [THEN ssubst])
   577 apply (rule ccontr)
   578 apply (simp add: poly_mult_left_cancel poly_mult_assoc del: pmult_Cons pexp_Suc)
   579 done
   580 
   581 lemma poly_one_divides: "[1] divides p"
   582 by (simp add: divides_def, auto)
   583 declare poly_one_divides [simp]
   584 
   585 lemma poly_order: "poly p \<noteq> poly []
   586       ==> EX! n. ([-a, (1::'a::{idom,ring_char_0})] %^ n) divides p &
   587                  ~(([-a, 1] %^ (Suc n)) divides p)"
   588 apply (auto intro: poly_order_exists simp add: less_linear simp del: pmult_Cons pexp_Suc)
   589 apply (cut_tac x = y and y = n in less_linear)
   590 apply (drule_tac m = n in poly_exp_divides)
   591 apply (auto dest: Suc_le_eq [THEN iffD2, THEN [2] poly_exp_divides]
   592             simp del: pmult_Cons pexp_Suc)
   593 done
   594 
   595 text{*Order*}
   596 
   597 lemma some1_equalityD: "[| n = (@n. P n); EX! n. P n |] ==> P n"
   598 by (blast intro: someI2)
   599 
   600 lemma order:
   601       "(([-a, (1::'a::{idom,ring_char_0})] %^ n) divides p &
   602         ~(([-a, 1] %^ (Suc n)) divides p)) =
   603         ((n = order a p) & ~(poly p = poly []))"
   604 apply (unfold order_def)
   605 apply (rule iffI)
   606 apply (blast dest: poly_divides_zero intro!: some1_equality [symmetric] poly_order)
   607 apply (blast intro!: poly_order [THEN [2] some1_equalityD])
   608 done
   609 
   610 lemma order2: "[| poly p \<noteq> poly [] |]
   611       ==> ([-a, (1::'a::{idom,ring_char_0})] %^ (order a p)) divides p &
   612               ~(([-a, 1] %^ (Suc(order a p))) divides p)"
   613 by (simp add: order del: pexp_Suc)
   614 
   615 lemma order_unique: "[| poly p \<noteq> poly []; ([-a, 1] %^ n) divides p;
   616          ~(([-a, (1::'a::{idom,ring_char_0})] %^ (Suc n)) divides p)
   617       |] ==> (n = order a p)"
   618 by (insert order [of a n p], auto) 
   619 
   620 lemma order_unique_lemma: "(poly p \<noteq> poly [] & ([-a, 1] %^ n) divides p &
   621          ~(([-a, (1::'a::{idom,ring_char_0})] %^ (Suc n)) divides p))
   622       ==> (n = order a p)"
   623 by (blast intro: order_unique)
   624 
   625 lemma order_poly: "poly p = poly q ==> order a p = order a q"
   626 by (auto simp add: fun_eq divides_def poly_mult order_def)
   627 
   628 lemma pexp_one: "p %^ (Suc 0) = p"
   629 apply (induct "p")
   630 apply (auto simp add: numeral_1_eq_1)
   631 done
   632 declare pexp_one [simp]
   633 
   634 lemma lemma_order_root [rule_format]:
   635      "\<forall>p a. 0 < n & [- a, 1] %^ n divides p & ~ [- a, 1] %^ (Suc n) divides p
   636              --> poly p a = 0"
   637 apply (induct "n", blast)
   638 apply (auto simp add: divides_def poly_mult simp del: pmult_Cons)
   639 done
   640 
   641 lemma order_root: "(poly p a = (0::'a::{idom,ring_char_0})) = ((poly p = poly []) | order a p \<noteq> 0)"
   642 apply (case_tac "poly p = poly []", auto)
   643 apply (simp add: poly_linear_divides del: pmult_Cons, safe)
   644 apply (drule_tac [!] a = a in order2)
   645 apply (rule ccontr)
   646 apply (simp add: divides_def poly_mult fun_eq del: pmult_Cons, blast)
   647 using neq0_conv
   648 apply (blast intro: lemma_order_root)
   649 done
   650 
   651 lemma order_divides: "(([-a, 1::'a::{idom,ring_char_0}] %^ n) divides p) = ((poly p = poly []) | n \<le> order a p)"
   652 apply (case_tac "poly p = poly []", auto)
   653 apply (simp add: divides_def fun_eq poly_mult)
   654 apply (rule_tac x = "[]" in exI)
   655 apply (auto dest!: order2 [where a=a]
   656             intro: poly_exp_divides simp del: pexp_Suc)
   657 done
   658 
   659 lemma order_decomp:
   660      "poly p \<noteq> poly []
   661       ==> \<exists>q. (poly p = poly (([-a, 1] %^ (order a p)) *** q)) &
   662                 ~([-a, 1::'a::{idom,ring_char_0}] divides q)"
   663 apply (unfold divides_def)
   664 apply (drule order2 [where a = a])
   665 apply (simp add: divides_def del: pexp_Suc pmult_Cons, safe)
   666 apply (rule_tac x = q in exI, safe)
   667 apply (drule_tac x = qa in spec)
   668 apply (auto simp add: poly_mult fun_eq poly_exp mult_ac simp del: pmult_Cons)
   669 done
   670 
   671 text{*Important composition properties of orders.*}
   672 
   673 lemma order_mult: "poly (p *** q) \<noteq> poly []
   674       ==> order a (p *** q) = order a p + order (a::'a::{idom,ring_char_0}) q"
   675 apply (cut_tac a = a and p = "p***q" and n = "order a p + order a q" in order)
   676 apply (auto simp add: poly_entire simp del: pmult_Cons)
   677 apply (drule_tac a = a in order2)+
   678 apply safe
   679 apply (simp add: divides_def fun_eq poly_exp_add poly_mult del: pmult_Cons, safe)
   680 apply (rule_tac x = "qa *** qaa" in exI)
   681 apply (simp add: poly_mult mult_ac del: pmult_Cons)
   682 apply (drule_tac a = a in order_decomp)+
   683 apply safe
   684 apply (subgoal_tac "[-a,1] divides (qa *** qaa) ")
   685 apply (simp add: poly_primes del: pmult_Cons)
   686 apply (auto simp add: divides_def simp del: pmult_Cons)
   687 apply (rule_tac x = qb in exI)
   688 apply (subgoal_tac "poly ([-a, 1] %^ (order a p) *** (qa *** qaa)) = poly ([-a, 1] %^ (order a p) *** ([-a, 1] *** qb))")
   689 apply (drule poly_mult_left_cancel [THEN iffD1], force)
   690 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))) ")
   691 apply (drule poly_mult_left_cancel [THEN iffD1], force)
   692 apply (simp add: fun_eq poly_exp_add poly_mult mult_ac del: pmult_Cons)
   693 done
   694 
   695 
   696 
   697 lemma order_root2: "poly p \<noteq> poly [] ==> (poly p a = 0) = (order (a::'a::{idom,ring_char_0}) p \<noteq> 0)"
   698 by (rule order_root [THEN ssubst], auto)
   699 
   700 
   701 lemma pmult_one: "[1] *** p = p"
   702 by auto
   703 declare pmult_one [simp]
   704 
   705 lemma poly_Nil_zero: "poly [] = poly [0]"
   706 by (simp add: fun_eq)
   707 
   708 lemma rsquarefree_decomp:
   709      "[| rsquarefree p; poly p a = (0::'a::{idom,ring_char_0}) |]
   710       ==> \<exists>q. (poly p = poly ([-a, 1] *** q)) & poly q a \<noteq> 0"
   711 apply (simp add: rsquarefree_def, safe)
   712 apply (frule_tac a = a in order_decomp)
   713 apply (drule_tac x = a in spec)
   714 apply (drule_tac a = a in order_root2 [symmetric])
   715 apply (auto simp del: pmult_Cons)
   716 apply (rule_tac x = q in exI, safe)
   717 apply (simp add: poly_mult fun_eq)
   718 apply (drule_tac p1 = q in poly_linear_divides [THEN iffD1])
   719 apply (simp add: divides_def del: pmult_Cons, safe)
   720 apply (drule_tac x = "[]" in spec)
   721 apply (auto simp add: fun_eq)
   722 done
   723 
   724 
   725 text{*Normalization of a polynomial.*}
   726 
   727 lemma poly_normalize: "poly (pnormalize p) = poly p"
   728 apply (induct "p")
   729 apply (auto simp add: fun_eq)
   730 done
   731 declare poly_normalize [simp]
   732 
   733 
   734 text{*The degree of a polynomial.*}
   735 
   736 lemma lemma_degree_zero:
   737      "list_all (%c. c = 0) p \<longleftrightarrow>  pnormalize p = []"
   738 by (induct "p", auto)
   739 
   740 lemma degree_zero: "(poly p = poly ([]:: (('a::{idom,ring_char_0}) list))) \<Longrightarrow> (degree p = 0)"
   741 apply (simp add: degree_def)
   742 apply (case_tac "pnormalize p = []")
   743 apply (auto simp add: poly_zero lemma_degree_zero )
   744 done
   745 
   746 lemma pnormalize_sing: "(pnormalize [x] = [x]) \<longleftrightarrow> x \<noteq> 0" by simp
   747 lemma pnormalize_pair: "y \<noteq> 0 \<longleftrightarrow> (pnormalize [x, y] = [x, y])" by simp
   748 lemma pnormal_cons: "pnormal p \<Longrightarrow> pnormal (c#p)" 
   749   unfolding pnormal_def by simp
   750 lemma pnormal_tail: "p\<noteq>[] \<Longrightarrow> pnormal (c#p) \<Longrightarrow> pnormal p"
   751   unfolding pnormal_def 
   752   apply (cases "pnormalize p = []", auto)
   753   by (cases "c = 0", auto)
   754 lemma pnormal_last_nonzero: "pnormal p ==> last p \<noteq> 0"
   755   apply (induct p, auto simp add: pnormal_def)
   756   apply (case_tac "pnormalize p = []", auto)
   757   by (case_tac "a=0", auto)
   758 lemma  pnormal_length: "pnormal p \<Longrightarrow> 0 < length p"
   759   unfolding pnormal_def length_greater_0_conv by blast
   760 lemma pnormal_last_length: "\<lbrakk>0 < length p ; last p \<noteq> 0\<rbrakk> \<Longrightarrow> pnormal p"
   761   apply (induct p, auto)
   762   apply (case_tac "p = []", auto)
   763   apply (simp add: pnormal_def)
   764   by (rule pnormal_cons, auto)
   765 lemma pnormal_id: "pnormal p \<longleftrightarrow> (0 < length p \<and> last p \<noteq> 0)"
   766   using pnormal_last_length pnormal_length pnormal_last_nonzero by blast
   767 
   768 text{*Tidier versions of finiteness of roots.*}
   769 
   770 lemma poly_roots_finite_set: "poly p \<noteq> poly [] ==> finite {x::'a::{idom,ring_char_0}. poly p x = 0}"
   771 unfolding poly_roots_finite .
   772 
   773 text{*bound for polynomial.*}
   774 
   775 lemma poly_mono: "abs(x) \<le> k ==> abs(poly p (x::'a::{linordered_idom})) \<le> poly (map abs p) k"
   776 apply (induct "p", auto)
   777 apply (rule_tac y = "abs a + abs (x * poly p x)" in order_trans)
   778 apply (rule abs_triangle_ineq)
   779 apply (auto intro!: mult_mono simp add: abs_mult)
   780 done
   781 
   782 lemma poly_Sing: "poly [c] x = c" by simp
   783 
   784 end