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