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