src/HOL/Decision_Procs/Polynomial_List.thy
author blanchet
Thu Sep 11 18:54:36 2014 +0200 (2014-09-11)
changeset 58306 117ba6cbe414
parent 57514 bdc2c6b40bf2
child 58889 5b7a9633cfa8
permissions -rw-r--r--
renamed 'rep_datatype' to 'old_rep_datatype' (HOL)
wenzelm@33268
     1
(*  Title:      HOL/Decision_Procs/Polynomial_List.thy
wenzelm@33268
     2
    Author:     Amine Chaieb
chaieb@33153
     3
*)
chaieb@33153
     4
haftmann@54219
     5
header {* Univariate Polynomials as lists *}
chaieb@33153
     6
chaieb@33153
     7
theory Polynomial_List
haftmann@54219
     8
imports Complex_Main
chaieb@33153
     9
begin
chaieb@33153
    10
haftmann@54219
    11
text{* Application of polynomial as a function. *}
chaieb@33153
    12
haftmann@54219
    13
primrec (in semiring_0) poly :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a"
wenzelm@52778
    14
where
chaieb@33153
    15
  poly_Nil:  "poly [] x = 0"
haftmann@54219
    16
| poly_Cons: "poly (h#t) x = h + x * poly t x"
chaieb@33153
    17
chaieb@33153
    18
chaieb@33153
    19
subsection{*Arithmetic Operations on Polynomials*}
chaieb@33153
    20
chaieb@33153
    21
text{*addition*}
haftmann@54219
    22
haftmann@54219
    23
primrec (in semiring_0) padd :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "+++" 65)
wenzelm@52778
    24
where
chaieb@33153
    25
  padd_Nil:  "[] +++ l2 = l2"
haftmann@54219
    26
| padd_Cons: "(h#t) +++ l2 = (if l2 = [] then h#t else (h + hd l2)#(t +++ tl l2))"
chaieb@33153
    27
chaieb@33153
    28
text{*Multiplication by a constant*}
haftmann@54219
    29
primrec (in semiring_0) cmult :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "%*" 70) where
haftmann@39246
    30
  cmult_Nil:  "c %* [] = []"
haftmann@54219
    31
| cmult_Cons: "c %* (h#t) = (c * h)#(c %* t)"
chaieb@33153
    32
chaieb@33153
    33
text{*Multiplication by a polynomial*}
haftmann@54219
    34
primrec (in semiring_0) pmult :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixl "***" 70)
wenzelm@52778
    35
where
haftmann@39246
    36
  pmult_Nil:  "[] *** l2 = []"
haftmann@54219
    37
| pmult_Cons: "(h#t) *** l2 = (if t = [] then h %* l2
haftmann@54219
    38
                              else (h %* l2) +++ ((0) # (t *** l2)))"
chaieb@33153
    39
chaieb@33153
    40
text{*Repeated multiplication by a polynomial*}
haftmann@54219
    41
primrec (in semiring_0) mulexp :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a  list \<Rightarrow> 'a list" where
haftmann@39246
    42
  mulexp_zero:  "mulexp 0 p q = q"
haftmann@39246
    43
| mulexp_Suc:   "mulexp (Suc n) p q = p *** mulexp n p q"
chaieb@33153
    44
chaieb@33153
    45
text{*Exponential*}
haftmann@54219
    46
primrec (in semiring_1) pexp :: "'a list \<Rightarrow> nat \<Rightarrow> 'a list"  (infixl "%^" 80) where
haftmann@39246
    47
  pexp_0:   "p %^ 0 = [1]"
haftmann@39246
    48
| pexp_Suc: "p %^ (Suc n) = p *** (p %^ n)"
chaieb@33153
    49
chaieb@33153
    50
text{*Quotient related value of dividing a polynomial by x + a*}
chaieb@33153
    51
(* Useful for divisor properties in inductive proofs *)
haftmann@54219
    52
primrec (in field) "pquot" :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list"
wenzelm@52778
    53
where
haftmann@54219
    54
  pquot_Nil:  "pquot [] a= []"
haftmann@54219
    55
| pquot_Cons: "pquot (h#t) a =
haftmann@54219
    56
    (if t = [] then [h] else (inverse(a) * (h - hd( pquot t a)))#(pquot t a))"
chaieb@33153
    57
chaieb@33153
    58
text{*normalization of polynomials (remove extra 0 coeff)*}
haftmann@54219
    59
primrec (in semiring_0) pnormalize :: "'a list \<Rightarrow> 'a list" where
haftmann@39246
    60
  pnormalize_Nil:  "pnormalize [] = []"
haftmann@54219
    61
| pnormalize_Cons: "pnormalize (h#p) =
haftmann@54219
    62
    (if pnormalize p = [] then (if h = 0 then [] else [h]) else h # pnormalize p)"
chaieb@33153
    63
haftmann@54219
    64
definition (in semiring_0) "pnormal p = ((pnormalize p = p) \<and> p \<noteq> [])"
haftmann@54219
    65
definition (in semiring_0) "nonconstant p = (pnormal p \<and> (\<forall>x. p \<noteq> [x]))"
chaieb@33153
    66
text{*Other definitions*}
chaieb@33153
    67
haftmann@54219
    68
definition (in ring_1) poly_minus :: "'a list \<Rightarrow> 'a list" ("-- _" [80] 80)
wenzelm@52778
    69
  where "-- p = (- 1) %* p"
chaieb@33153
    70
haftmann@54219
    71
definition (in semiring_0) divides :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  (infixl "divides" 70)
haftmann@54219
    72
  where "p1 divides p2 = (\<exists>q. poly p2 = poly(p1 *** q))"
haftmann@54219
    73
haftmann@54219
    74
lemma (in semiring_0) dividesI:
haftmann@54219
    75
  "poly p2 = poly (p1 *** q) \<Longrightarrow> p1 divides p2"
haftmann@54219
    76
  by (auto simp add: divides_def)
chaieb@33153
    77
haftmann@54219
    78
lemma (in semiring_0) dividesE:
haftmann@54219
    79
  assumes "p1 divides p2"
haftmann@54219
    80
  obtains q where "poly p2 = poly (p1 *** q)"
haftmann@54219
    81
  using assms by (auto simp add: divides_def)
chaieb@33153
    82
haftmann@54219
    83
    --{*order of a polynomial*}
haftmann@54219
    84
definition (in ring_1) order :: "'a \<Rightarrow> 'a list \<Rightarrow> nat" where
haftmann@54219
    85
  "order a p = (SOME n. ([-a, 1] %^ n) divides p \<and> ~ (([-a, 1] %^ (Suc n)) divides p))"
haftmann@54219
    86
haftmann@54219
    87
     --{*degree of a polynomial*}
haftmann@54219
    88
definition (in semiring_0) degree :: "'a list \<Rightarrow> nat"
wenzelm@52778
    89
  where "degree p = length (pnormalize p) - 1"
chaieb@33153
    90
haftmann@54219
    91
     --{*squarefree polynomials --- NB with respect to real roots only.*}
haftmann@54219
    92
definition (in ring_1) rsquarefree :: "'a list \<Rightarrow> bool"
haftmann@54219
    93
  where "rsquarefree p \<longleftrightarrow> poly p \<noteq> poly [] \<and> (\<forall>a. order a p = 0 \<or> order a p = 1)"
chaieb@33153
    94
haftmann@54219
    95
context semiring_0
haftmann@54219
    96
begin
haftmann@54219
    97
haftmann@54219
    98
lemma padd_Nil2[simp]: "p +++ [] = p"
wenzelm@52778
    99
  by (induct p) auto
chaieb@33153
   100
chaieb@33153
   101
lemma padd_Cons_Cons: "(h1 # p1) +++ (h2 # p2) = (h1 + h2) # (p1 +++ p2)"
wenzelm@52778
   102
  by auto
chaieb@33153
   103
haftmann@54219
   104
lemma pminus_Nil: "-- [] = []"
wenzelm@52778
   105
  by (simp add: poly_minus_def)
chaieb@33153
   106
haftmann@54219
   107
lemma pmult_singleton: "[h1] *** p1 = h1 %* p1" by simp
haftmann@54219
   108
haftmann@54219
   109
end
chaieb@33153
   110
haftmann@54219
   111
lemma (in semiring_1) poly_ident_mult[simp]: "1 %* t = t" by (induct t) auto
chaieb@33153
   112
haftmann@54219
   113
lemma (in semiring_0) poly_simple_add_Cons[simp]: "[a] +++ ((0)#t) = (a#t)"
wenzelm@52778
   114
  by simp
chaieb@33153
   115
chaieb@33153
   116
text{*Handy general properties*}
chaieb@33153
   117
haftmann@54219
   118
lemma (in comm_semiring_0) padd_commut: "b +++ a = a +++ b"
haftmann@54219
   119
proof (induct b arbitrary: a)
haftmann@54219
   120
  case Nil
haftmann@54219
   121
  thus ?case by auto
haftmann@54219
   122
next
haftmann@54219
   123
  case (Cons b bs a)
haftmann@57512
   124
  thus ?case by (cases a) (simp_all add: add.commute)
haftmann@54219
   125
qed
haftmann@54219
   126
haftmann@54219
   127
lemma (in comm_semiring_0) padd_assoc: "\<forall>b c. (a +++ b) +++ c = a +++ (b +++ c)"
haftmann@54219
   128
  apply (induct a)
haftmann@54219
   129
  apply (simp, clarify)
haftmann@57514
   130
  apply (case_tac b, simp_all add: ac_simps)
wenzelm@52778
   131
  done
chaieb@33153
   132
haftmann@54219
   133
lemma (in semiring_0) poly_cmult_distr: "a %* ( p +++ q) = (a %* p +++ a %* q)"
haftmann@54219
   134
  apply (induct p arbitrary: q)
wenzelm@52881
   135
  apply simp
haftmann@54219
   136
  apply (case_tac q, simp_all add: distrib_left)
wenzelm@52778
   137
  done
chaieb@33153
   138
haftmann@54219
   139
lemma (in ring_1) pmult_by_x[simp]: "[0, 1] *** t = ((0)#t)"
haftmann@54219
   140
  apply (induct t)
wenzelm@52778
   141
  apply simp
haftmann@54219
   142
  apply (auto simp add: padd_commut)
haftmann@54219
   143
  apply (case_tac t, auto)
wenzelm@52778
   144
  done
chaieb@33153
   145
chaieb@33153
   146
text{*properties of evaluation of polynomials.*}
chaieb@33153
   147
haftmann@54219
   148
lemma (in semiring_0) poly_add: "poly (p1 +++ p2) x = poly p1 x + poly p2 x"
haftmann@54219
   149
proof(induct p1 arbitrary: p2)
haftmann@54219
   150
  case Nil
haftmann@54219
   151
  thus ?case by simp
haftmann@54219
   152
next
haftmann@54219
   153
  case (Cons a as p2)
haftmann@54219
   154
  thus ?case
haftmann@57514
   155
    by (cases p2) (simp_all  add: ac_simps distrib_left)
haftmann@54219
   156
qed
chaieb@33153
   157
haftmann@54219
   158
lemma (in comm_semiring_0) poly_cmult: "poly (c %* p) x = c * poly p x"
wenzelm@52881
   159
  apply (induct p)
haftmann@54219
   160
  apply (case_tac [2] "x = zero")
haftmann@57514
   161
  apply (auto simp add: distrib_left ac_simps)
wenzelm@52778
   162
  done
chaieb@33153
   163
haftmann@54219
   164
lemma (in comm_semiring_0) poly_cmult_map: "poly (map (op * c) p) x = c*poly p x"
haftmann@57514
   165
  by (induct p) (auto simp add: distrib_left ac_simps)
chaieb@33153
   166
haftmann@54219
   167
lemma (in comm_ring_1) poly_minus: "poly (-- p) x = - (poly p x)"
haftmann@54219
   168
  apply (simp add: poly_minus_def)
haftmann@54219
   169
  apply (auto simp add: poly_cmult)
wenzelm@52778
   170
  done
chaieb@33153
   171
haftmann@54219
   172
lemma (in comm_semiring_0) poly_mult: "poly (p1 *** p2) x = poly p1 x * poly p2 x"
haftmann@54219
   173
proof (induct p1 arbitrary: p2)
haftmann@54219
   174
  case Nil
haftmann@54219
   175
  thus ?case by simp
haftmann@54219
   176
next
haftmann@54219
   177
  case (Cons a as p2)
haftmann@54219
   178
  thus ?case by (cases as)
haftmann@57514
   179
    (simp_all add: poly_cmult poly_add distrib_right distrib_left ac_simps)
haftmann@54219
   180
qed
haftmann@54219
   181
haftmann@54219
   182
class idom_char_0 = idom + ring_char_0
haftmann@54219
   183
haftmann@54219
   184
subclass (in field_char_0) idom_char_0 ..
haftmann@54219
   185
haftmann@54219
   186
lemma (in comm_ring_1) poly_exp: "poly (p %^ n) x = (poly p x) ^ n"
wenzelm@52881
   187
  by (induct n) (auto simp add: poly_cmult poly_mult)
chaieb@33153
   188
chaieb@33153
   189
text{*More Polynomial Evaluation Lemmas*}
chaieb@33153
   190
haftmann@54219
   191
lemma (in semiring_0) poly_add_rzero[simp]: "poly (a +++ []) x = poly a x"
wenzelm@52778
   192
  by simp
chaieb@33153
   193
haftmann@54219
   194
lemma (in comm_semiring_0) poly_mult_assoc: "poly ((a *** b) *** c) x = poly (a *** (b *** c)) x"
haftmann@57512
   195
  by (simp add: poly_mult mult.assoc)
chaieb@33153
   196
haftmann@54219
   197
lemma (in semiring_0) poly_mult_Nil2[simp]: "poly (p *** []) x = 0"
wenzelm@52881
   198
  by (induct p) auto
chaieb@33153
   199
haftmann@54219
   200
lemma (in comm_semiring_1) poly_exp_add: "poly (p %^ (n + d)) x = poly( p %^ n *** p %^ d) x"
haftmann@57512
   201
  by (induct n) (auto simp add: poly_mult mult.assoc)
chaieb@33153
   202
chaieb@33153
   203
subsection{*Key Property: if @{term "f(a) = 0"} then @{term "(x - a)"} divides
chaieb@33153
   204
 @{term "p(x)"} *}
chaieb@33153
   205
haftmann@54219
   206
lemma (in comm_ring_1) lemma_poly_linear_rem: "\<forall>h. \<exists>q r. h#t = [r] +++ [-a, 1] *** q"
haftmann@54219
   207
proof(induct t)
haftmann@54219
   208
  case Nil
haftmann@54219
   209
  { fix h have "[h] = [h] +++ [- a, 1] *** []" by simp }
haftmann@54219
   210
  thus ?case by blast
haftmann@54219
   211
next
haftmann@54219
   212
  case (Cons  x xs)
haftmann@54219
   213
  { fix h
haftmann@54219
   214
    from Cons.hyps[rule_format, of x]
haftmann@54219
   215
    obtain q r where qr: "x#xs = [r] +++ [- a, 1] *** q" by blast
haftmann@54219
   216
    have "h#x#xs = [a*r + h] +++ [-a, 1] *** (r#q)"
haftmann@54219
   217
      using qr by (cases q) (simp_all add: algebra_simps)
haftmann@54219
   218
    hence "\<exists>q r. h#x#xs = [r] +++ [-a, 1] *** q" by blast}
haftmann@54219
   219
  thus ?case by blast
haftmann@54219
   220
qed
haftmann@54219
   221
haftmann@54219
   222
lemma (in comm_ring_1) poly_linear_rem: "\<exists>q r. h#t = [r] +++ [-a, 1] *** q"
haftmann@54219
   223
  using lemma_poly_linear_rem [where t = t and a = a] by auto
haftmann@54219
   224
chaieb@33153
   225
haftmann@54219
   226
lemma (in comm_ring_1) poly_linear_divides: "(poly p a = 0) = ((p = []) | (\<exists>q. p = [-a, 1] *** q))"
haftmann@54219
   227
proof -
haftmann@54219
   228
  { assume p: "p = []" hence ?thesis by simp }
haftmann@54219
   229
  moreover
haftmann@54219
   230
  {
haftmann@54219
   231
    fix x xs assume p: "p = x#xs"
haftmann@54219
   232
    {
haftmann@54219
   233
      fix q assume "p = [-a, 1] *** q"
haftmann@54219
   234
      hence "poly p a = 0" by (simp add: poly_add poly_cmult)
haftmann@54219
   235
    }
haftmann@54219
   236
    moreover
haftmann@54219
   237
    { assume p0: "poly p a = 0"
haftmann@54219
   238
      from poly_linear_rem[of x xs a] obtain q r
haftmann@54219
   239
      where qr: "x#xs = [r] +++ [- a, 1] *** q" by blast
haftmann@54219
   240
      have "r = 0" using p0 by (simp only: p qr poly_mult poly_add) simp
haftmann@54219
   241
      hence "\<exists>q. p = [- a, 1] *** q"
haftmann@54219
   242
        using p qr
haftmann@54219
   243
        apply -
haftmann@54219
   244
        apply (rule exI[where x=q])
haftmann@54219
   245
        apply auto
haftmann@54219
   246
        apply (cases q)
haftmann@54219
   247
        apply auto
haftmann@54219
   248
        done
haftmann@54219
   249
    }
haftmann@54219
   250
    ultimately have ?thesis using p by blast
haftmann@54219
   251
  }
haftmann@54219
   252
  ultimately show ?thesis by (cases p) auto
haftmann@54219
   253
qed
chaieb@33153
   254
haftmann@54219
   255
lemma (in semiring_0) lemma_poly_length_mult[simp]: "\<forall>h k a. length (k %* p +++  (h # (a %* p))) = Suc (length p)"
haftmann@54219
   256
  by (induct p) auto
chaieb@33153
   257
haftmann@54219
   258
lemma (in semiring_0) lemma_poly_length_mult2[simp]: "\<forall>h k. length (k %* p +++  (h # p)) = Suc (length p)"
haftmann@54219
   259
  by (induct p) auto
chaieb@33153
   260
haftmann@54219
   261
lemma (in ring_1) poly_length_mult[simp]: "length([-a,1] *** q) = Suc (length q)"
wenzelm@52778
   262
  by auto
chaieb@33153
   263
chaieb@33153
   264
subsection{*Polynomial length*}
chaieb@33153
   265
haftmann@54219
   266
lemma (in semiring_0) poly_cmult_length[simp]: "length (a %* p) = length p"
wenzelm@52778
   267
  by (induct p) auto
chaieb@33153
   268
haftmann@54219
   269
lemma (in semiring_0) poly_add_length: "length (p1 +++ p2) = max (length p1) (length p2)"
haftmann@54219
   270
  by (induct p1 arbitrary: p2) (simp_all, arith)
chaieb@33153
   271
haftmann@54219
   272
lemma (in semiring_0) poly_root_mult_length[simp]: "length([a,b] *** p) = Suc (length p)"
haftmann@54219
   273
  by (simp add: poly_add_length)
chaieb@33153
   274
haftmann@54219
   275
lemma (in idom) poly_mult_not_eq_poly_Nil[simp]:
haftmann@54219
   276
  "poly (p *** q) x \<noteq> poly [] x \<longleftrightarrow> poly p x \<noteq> poly [] x \<and> poly q x \<noteq> poly [] x"
wenzelm@52778
   277
  by (auto simp add: poly_mult)
chaieb@33153
   278
haftmann@54219
   279
lemma (in idom) poly_mult_eq_zero_disj: "poly (p *** q) x = 0 \<longleftrightarrow> poly p x = 0 \<or> poly q x = 0"
wenzelm@52778
   280
  by (auto simp add: poly_mult)
chaieb@33153
   281
chaieb@33153
   282
text{*Normalisation Properties*}
chaieb@33153
   283
haftmann@54219
   284
lemma (in semiring_0) poly_normalized_nil: "(pnormalize p = []) --> (poly p x = 0)"
wenzelm@52778
   285
  by (induct p) auto
chaieb@33153
   286
chaieb@33153
   287
text{*A nontrivial polynomial of degree n has no more than n roots*}
haftmann@54219
   288
lemma (in idom) poly_roots_index_lemma:
haftmann@54219
   289
   assumes p: "poly p x \<noteq> poly [] x" and n: "length p = n"
haftmann@54219
   290
  shows "\<exists>i. \<forall>x. poly p x = 0 \<longrightarrow> (\<exists>m\<le>n. x = i m)"
haftmann@54219
   291
  using p n
haftmann@54219
   292
proof (induct n arbitrary: p x)
haftmann@54219
   293
  case 0
haftmann@54219
   294
  thus ?case by simp
haftmann@54219
   295
next
haftmann@54219
   296
  case (Suc n p x)
haftmann@54219
   297
  {
haftmann@54219
   298
    assume C: "\<And>i. \<exists>x. poly p x = 0 \<and> (\<forall>m\<le>Suc n. x \<noteq> i m)"
haftmann@54219
   299
    from Suc.prems have p0: "poly p x \<noteq> 0" "p\<noteq> []" by auto
haftmann@54219
   300
    from p0(1)[unfolded poly_linear_divides[of p x]]
haftmann@54219
   301
    have "\<forall>q. p \<noteq> [- x, 1] *** q" by blast
haftmann@54219
   302
    from C obtain a where a: "poly p a = 0" by blast
haftmann@54219
   303
    from a[unfolded poly_linear_divides[of p a]] p0(2)
haftmann@54219
   304
    obtain q where q: "p = [-a, 1] *** q" by blast
haftmann@54219
   305
    have lg: "length q = n" using q Suc.prems(2) by simp
haftmann@54219
   306
    from q p0 have qx: "poly q x \<noteq> poly [] x"
haftmann@54219
   307
      by (auto simp add: poly_mult poly_add poly_cmult)
haftmann@54219
   308
    from Suc.hyps[OF qx lg] obtain i where
haftmann@54219
   309
      i: "\<forall>x. poly q x = 0 \<longrightarrow> (\<exists>m\<le>n. x = i m)" by blast
haftmann@54219
   310
    let ?i = "\<lambda>m. if m = Suc n then a else i m"
haftmann@54219
   311
    from C[of ?i] obtain y where y: "poly p y = 0" "\<forall>m\<le> Suc n. y \<noteq> ?i m"
haftmann@54219
   312
      by blast
haftmann@54219
   313
    from y have "y = a \<or> poly q y = 0"
haftmann@54219
   314
      by (simp only: q poly_mult_eq_zero_disj poly_add) (simp add: algebra_simps)
haftmann@54219
   315
    with i[rule_format, of y] y(1) y(2) have False
haftmann@54219
   316
      apply auto
haftmann@54219
   317
      apply (erule_tac x = "m" in allE)
haftmann@54219
   318
      apply auto
haftmann@54219
   319
      done
haftmann@54219
   320
  }
haftmann@54219
   321
  thus ?case by blast
haftmann@54219
   322
qed
chaieb@33153
   323
chaieb@33153
   324
haftmann@54219
   325
lemma (in idom) poly_roots_index_length:
haftmann@54219
   326
  "poly p x \<noteq> poly [] x \<Longrightarrow> \<exists>i. \<forall>x. (poly p x = 0) \<longrightarrow> (\<exists>n. n \<le> length p \<and> x = i n)"
haftmann@54219
   327
  by (blast intro: poly_roots_index_lemma)
chaieb@33153
   328
haftmann@54219
   329
lemma (in idom) poly_roots_finite_lemma1:
haftmann@54219
   330
  "poly p x \<noteq> poly [] x \<Longrightarrow> \<exists>N i. \<forall>x. (poly p x = 0) \<longrightarrow> (\<exists>n. (n::nat) < N \<and> x = i n)"
haftmann@54219
   331
  apply (drule poly_roots_index_length, safe)
wenzelm@52778
   332
  apply (rule_tac x = "Suc (length p)" in exI)
wenzelm@52778
   333
  apply (rule_tac x = i in exI)
wenzelm@52778
   334
  apply (simp add: less_Suc_eq_le)
wenzelm@52778
   335
  done
chaieb@33153
   336
haftmann@54219
   337
lemma (in idom) idom_finite_lemma:
haftmann@54219
   338
  assumes P: "\<forall>x. P x --> (\<exists>n. n < length j \<and> x = j!n)"
haftmann@54219
   339
  shows "finite {x. P x}"
wenzelm@52778
   340
proof -
chaieb@33153
   341
  let ?M = "{x. P x}"
chaieb@33153
   342
  let ?N = "set j"
haftmann@54219
   343
  have "?M \<subseteq> ?N" using P by auto
haftmann@54219
   344
  thus ?thesis using finite_subset by auto
chaieb@33153
   345
qed
chaieb@33153
   346
haftmann@54219
   347
lemma (in idom) poly_roots_finite_lemma2:
haftmann@54219
   348
  "poly p x \<noteq> poly [] x \<Longrightarrow> \<exists>i. \<forall>x. poly p x = 0 \<longrightarrow> x \<in> set i"
haftmann@54219
   349
  apply (drule poly_roots_index_length, safe)
haftmann@54219
   350
  apply (rule_tac x="map (\<lambda>n. i n) [0 ..< Suc (length p)]" in exI)
haftmann@54219
   351
  apply (auto simp add: image_iff)
haftmann@54219
   352
  apply (erule_tac x="x" in allE, clarsimp)
haftmann@54219
   353
  apply (case_tac "n = length p")
haftmann@54219
   354
  apply (auto simp add: order_le_less)
wenzelm@52778
   355
  done
chaieb@33153
   356
haftmann@54219
   357
lemma (in ring_char_0) UNIV_ring_char_0_infinte: "\<not> (finite (UNIV:: 'a set))"
haftmann@54219
   358
proof
haftmann@54219
   359
  assume F: "finite (UNIV :: 'a set)"
haftmann@54219
   360
  have "finite (UNIV :: nat set)"
haftmann@54219
   361
  proof (rule finite_imageD)
haftmann@54219
   362
    have "of_nat ` UNIV \<subseteq> UNIV" by simp
haftmann@54219
   363
    then show "finite (of_nat ` UNIV :: 'a set)" using F by (rule finite_subset)
haftmann@54219
   364
    show "inj (of_nat :: nat \<Rightarrow> 'a)" by (simp add: inj_on_def)
haftmann@54219
   365
  qed
haftmann@54219
   366
  with infinite_UNIV_nat show False ..
chaieb@33153
   367
qed
chaieb@33153
   368
haftmann@54219
   369
lemma (in idom_char_0) poly_roots_finite: "poly p \<noteq> poly [] \<longleftrightarrow> finite {x. poly p x = 0}"
chaieb@33153
   370
proof
haftmann@54219
   371
  assume H: "poly p \<noteq> poly []"
haftmann@54219
   372
  show "finite {x. poly p x = (0::'a)}"
haftmann@54219
   373
    using H
chaieb@33153
   374
    apply -
haftmann@54219
   375
    apply (erule contrapos_np, rule ext)
chaieb@33153
   376
    apply (rule ccontr)
haftmann@54219
   377
    apply (clarify dest!: poly_roots_finite_lemma2)
chaieb@33153
   378
    using finite_subset
wenzelm@52778
   379
  proof -
chaieb@33153
   380
    fix x i
wenzelm@52778
   381
    assume F: "\<not> finite {x. poly p x = (0\<Colon>'a)}"
chaieb@33153
   382
      and P: "\<forall>x. poly p x = (0\<Colon>'a) \<longrightarrow> x \<in> set i"
chaieb@33153
   383
    let ?M= "{x. poly p x = (0\<Colon>'a)}"
chaieb@33153
   384
    from P have "?M \<subseteq> set i" by auto
chaieb@33153
   385
    with finite_subset F show False by auto
chaieb@33153
   386
  qed
chaieb@33153
   387
next
haftmann@54219
   388
  assume F: "finite {x. poly p x = (0\<Colon>'a)}"
haftmann@54219
   389
  show "poly p \<noteq> poly []" using F UNIV_ring_char_0_infinte by auto
chaieb@33153
   390
qed
chaieb@33153
   391
chaieb@33153
   392
text{*Entirety and Cancellation for polynomials*}
chaieb@33153
   393
haftmann@54219
   394
lemma (in idom_char_0) poly_entire_lemma2:
haftmann@54219
   395
  assumes p0: "poly p \<noteq> poly []"
haftmann@54219
   396
    and q0: "poly q \<noteq> poly []"
haftmann@54219
   397
  shows "poly (p***q) \<noteq> poly []"
haftmann@54219
   398
proof -
haftmann@54219
   399
  let ?S = "\<lambda>p. {x. poly p x = 0}"
haftmann@54219
   400
  have "?S (p *** q) = ?S p \<union> ?S q" by (auto simp add: poly_mult)
haftmann@54219
   401
  with p0 q0 show ?thesis  unfolding poly_roots_finite by auto
haftmann@54219
   402
qed
chaieb@33153
   403
haftmann@54219
   404
lemma (in idom_char_0) poly_entire:
haftmann@54219
   405
  "poly (p *** q) = poly [] \<longleftrightarrow> poly p = poly [] \<or> poly q = poly []"
haftmann@54219
   406
  using poly_entire_lemma2[of p q]
haftmann@54219
   407
  by (auto simp add: fun_eq_iff poly_mult)
chaieb@33153
   408
haftmann@54219
   409
lemma (in idom_char_0) poly_entire_neg:
haftmann@54219
   410
  "poly (p *** q) \<noteq> poly [] \<longleftrightarrow> poly p \<noteq> poly [] \<and> poly q \<noteq> poly []"
wenzelm@52778
   411
  by (simp add: poly_entire)
chaieb@33153
   412
wenzelm@52778
   413
lemma fun_eq: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
wenzelm@52778
   414
  by auto
chaieb@33153
   415
haftmann@54219
   416
lemma (in comm_ring_1) poly_add_minus_zero_iff:
haftmann@54219
   417
  "poly (p +++ -- q) = poly [] \<longleftrightarrow> poly p = poly q"
haftmann@54219
   418
  by (auto simp add: algebra_simps poly_add poly_minus_def fun_eq poly_cmult)
chaieb@33153
   419
haftmann@54219
   420
lemma (in comm_ring_1) poly_add_minus_mult_eq:
haftmann@54219
   421
  "poly (p *** q +++ --(p *** r)) = poly (p *** (q +++ -- r))"
haftmann@54230
   422
  by (auto simp add: poly_add poly_minus_def fun_eq poly_mult poly_cmult algebra_simps)
chaieb@33153
   423
haftmann@54219
   424
subclass (in idom_char_0) comm_ring_1 ..
chaieb@33153
   425
haftmann@54219
   426
lemma (in idom_char_0) poly_mult_left_cancel:
haftmann@54219
   427
  "poly (p *** q) = poly (p *** r) \<longleftrightarrow> poly p = poly [] \<or> poly q = poly r"
haftmann@54219
   428
proof -
haftmann@54219
   429
  have "poly (p *** q) = poly (p *** r) \<longleftrightarrow> poly (p *** q +++ -- (p *** r)) = poly []"
haftmann@54219
   430
    by (simp only: poly_add_minus_zero_iff)
haftmann@54219
   431
  also have "\<dots> \<longleftrightarrow> poly p = poly [] \<or> poly q = poly r"
haftmann@54219
   432
    by (auto intro: simp add: poly_add_minus_mult_eq poly_entire poly_add_minus_zero_iff)
haftmann@54219
   433
  finally show ?thesis .
haftmann@54219
   434
qed
haftmann@54219
   435
haftmann@54219
   436
lemma (in idom) poly_exp_eq_zero[simp]:
haftmann@54219
   437
  "poly (p %^ n) = poly [] \<longleftrightarrow> poly p = poly [] \<and> n \<noteq> 0"
wenzelm@52778
   438
  apply (simp only: fun_eq add: HOL.all_simps [symmetric])
wenzelm@52778
   439
  apply (rule arg_cong [where f = All])
wenzelm@52778
   440
  apply (rule ext)
haftmann@54219
   441
  apply (induct n)
haftmann@54219
   442
  apply (auto simp add: poly_exp poly_mult)
wenzelm@52778
   443
  done
chaieb@33153
   444
haftmann@54219
   445
lemma (in comm_ring_1) poly_prime_eq_zero[simp]: "poly [a,1] \<noteq> poly []"
wenzelm@52778
   446
  apply (simp add: fun_eq)
haftmann@54219
   447
  apply (rule_tac x = "minus one a" in exI)
haftmann@57512
   448
  apply (simp add: add.commute [of a])
wenzelm@52778
   449
  done
chaieb@33153
   450
haftmann@54219
   451
lemma (in idom) poly_exp_prime_eq_zero: "poly ([a, 1] %^ n) \<noteq> poly []"
wenzelm@52778
   452
  by auto
chaieb@33153
   453
chaieb@33153
   454
text{*A more constructive notion of polynomials being trivial*}
chaieb@33153
   455
haftmann@54219
   456
lemma (in idom_char_0) poly_zero_lemma': "poly (h # t) = poly [] \<Longrightarrow> h = 0 \<and> poly t = poly []"
wenzelm@52778
   457
  apply (simp add: fun_eq)
haftmann@54219
   458
  apply (case_tac "h = zero")
haftmann@54219
   459
  apply (drule_tac [2] x = zero in spec, auto)
haftmann@54219
   460
  apply (cases "poly t = poly []", simp)
wenzelm@52778
   461
proof -
chaieb@33153
   462
  fix x
haftmann@54219
   463
  assume H: "\<forall>x. x = (0\<Colon>'a) \<or> poly t x = (0\<Colon>'a)"
haftmann@54219
   464
    and pnz: "poly t \<noteq> poly []"
chaieb@33153
   465
  let ?S = "{x. poly t x = 0}"
chaieb@33153
   466
  from H have "\<forall>x. x \<noteq>0 \<longrightarrow> poly t x = 0" by blast
chaieb@33153
   467
  hence th: "?S \<supseteq> UNIV - {0}" by auto
chaieb@33153
   468
  from poly_roots_finite pnz have th': "finite ?S" by blast
haftmann@54219
   469
  from finite_subset[OF th th'] UNIV_ring_char_0_infinte show "poly t x = (0\<Colon>'a)"
haftmann@54219
   470
    by simp
wenzelm@52778
   471
qed
chaieb@33153
   472
haftmann@54219
   473
lemma (in idom_char_0) poly_zero: "(poly p = poly []) = list_all (%c. c = 0) p"
wenzelm@52778
   474
  apply (induct p)
wenzelm@52778
   475
  apply simp
wenzelm@52778
   476
  apply (rule iffI)
haftmann@54219
   477
  apply (drule poly_zero_lemma', auto)
wenzelm@52778
   478
  done
chaieb@33153
   479
haftmann@54219
   480
lemma (in idom_char_0) poly_0: "list_all (\<lambda>c. c = 0) p \<Longrightarrow> poly p x = 0"
haftmann@54219
   481
  unfolding poly_zero[symmetric] by simp
haftmann@54219
   482
haftmann@54219
   483
chaieb@33153
   484
chaieb@33153
   485
text{*Basics of divisibility.*}
chaieb@33153
   486
haftmann@54219
   487
lemma (in idom) poly_primes:
haftmann@54219
   488
  "[a, 1] divides (p *** q) \<longleftrightarrow> [a, 1] divides p \<or> [a, 1] divides q"
wenzelm@52778
   489
  apply (auto simp add: divides_def fun_eq poly_mult poly_add poly_cmult distrib_right [symmetric])
haftmann@54219
   490
  apply (drule_tac x = "uminus a" in spec)
haftmann@54219
   491
  apply (simp add: poly_linear_divides poly_add poly_cmult distrib_right [symmetric])
haftmann@54219
   492
  apply (cases "p = []")
haftmann@54219
   493
  apply (rule exI[where x="[]"])
haftmann@54219
   494
  apply simp
haftmann@54219
   495
  apply (cases "q = []")
haftmann@54219
   496
  apply (erule allE[where x="[]"], simp)
haftmann@54219
   497
haftmann@54219
   498
  apply clarsimp
haftmann@54219
   499
  apply (cases "\<exists>q\<Colon>'a list. p = a %* q +++ ((0\<Colon>'a) # q)")
haftmann@54219
   500
  apply (clarsimp simp add: poly_add poly_cmult)
haftmann@54219
   501
  apply (rule_tac x="qa" in exI)
haftmann@54219
   502
  apply (simp add: distrib_right [symmetric])
haftmann@54219
   503
  apply clarsimp
haftmann@54219
   504
wenzelm@52778
   505
  apply (auto simp add: poly_linear_divides poly_add poly_cmult distrib_right [symmetric])
haftmann@54219
   506
  apply (rule_tac x = "pmult qa q" in exI)
haftmann@54219
   507
  apply (rule_tac [2] x = "pmult p qa" in exI)
haftmann@57514
   508
  apply (auto simp add: poly_add poly_mult poly_cmult ac_simps)
wenzelm@52778
   509
  done
chaieb@33153
   510
haftmann@54219
   511
lemma (in comm_semiring_1) poly_divides_refl[simp]: "p divides p"
wenzelm@52778
   512
  apply (simp add: divides_def)
haftmann@54219
   513
  apply (rule_tac x = "[one]" in exI)
wenzelm@52778
   514
  apply (auto simp add: poly_mult fun_eq)
wenzelm@52778
   515
  done
chaieb@33153
   516
haftmann@54219
   517
lemma (in comm_semiring_1) poly_divides_trans: "p divides q \<Longrightarrow> q divides r \<Longrightarrow> p divides r"
haftmann@54219
   518
  apply (simp add: divides_def, safe)
haftmann@54219
   519
  apply (rule_tac x = "pmult qa qaa" in exI)
haftmann@57512
   520
  apply (auto simp add: poly_mult fun_eq mult.assoc)
wenzelm@52778
   521
  done
chaieb@33153
   522
haftmann@54219
   523
lemma (in comm_semiring_1) poly_divides_exp: "m \<le> n \<Longrightarrow> (p %^ m) divides (p %^ n)"
wenzelm@52778
   524
  apply (auto simp add: le_iff_add)
wenzelm@52778
   525
  apply (induct_tac k)
wenzelm@52778
   526
  apply (rule_tac [2] poly_divides_trans)
wenzelm@52778
   527
  apply (auto simp add: divides_def)
wenzelm@52778
   528
  apply (rule_tac x = p in exI)
haftmann@57514
   529
  apply (auto simp add: poly_mult fun_eq ac_simps)
wenzelm@52778
   530
  done
chaieb@33153
   531
haftmann@54219
   532
lemma (in comm_semiring_1) poly_exp_divides:
haftmann@54219
   533
  "(p %^ n) divides q \<Longrightarrow> m \<le> n \<Longrightarrow> (p %^ m) divides q"
wenzelm@52778
   534
  by (blast intro: poly_divides_exp poly_divides_trans)
chaieb@33153
   535
haftmann@54219
   536
lemma (in comm_semiring_0) poly_divides_add:
haftmann@54219
   537
  "p divides q \<Longrightarrow> p divides r \<Longrightarrow> p divides (q +++ r)"
haftmann@54219
   538
  apply (simp add: divides_def, auto)
haftmann@54219
   539
  apply (rule_tac x = "padd qa qaa" in exI)
wenzelm@52778
   540
  apply (auto simp add: poly_add fun_eq poly_mult distrib_left)
wenzelm@52778
   541
  done
chaieb@33153
   542
haftmann@54219
   543
lemma (in comm_ring_1) poly_divides_diff:
haftmann@54219
   544
  "p divides q \<Longrightarrow> p divides (q +++ r) \<Longrightarrow> p divides r"
haftmann@54219
   545
  apply (simp add: divides_def, auto)
haftmann@54219
   546
  apply (rule_tac x = "padd qaa (poly_minus qa)" in exI)
wenzelm@52778
   547
  apply (auto simp add: poly_add fun_eq poly_mult poly_minus algebra_simps)
wenzelm@52778
   548
  done
chaieb@33153
   549
haftmann@54219
   550
lemma (in comm_ring_1) poly_divides_diff2:
haftmann@54219
   551
  "p divides r \<Longrightarrow> p divides (q +++ r) \<Longrightarrow> p divides q"
wenzelm@52778
   552
  apply (erule poly_divides_diff)
haftmann@57514
   553
  apply (auto simp add: poly_add fun_eq poly_mult divides_def ac_simps)
wenzelm@52778
   554
  done
chaieb@33153
   555
haftmann@54219
   556
lemma (in semiring_0) poly_divides_zero: "poly p = poly [] \<Longrightarrow> q divides p"
wenzelm@52778
   557
  apply (simp add: divides_def)
haftmann@54219
   558
  apply (rule exI[where x="[]"])
wenzelm@52778
   559
  apply (auto simp add: fun_eq poly_mult)
wenzelm@52778
   560
  done
chaieb@33153
   561
haftmann@54219
   562
lemma (in semiring_0) poly_divides_zero2 [simp]: "q divides []"
wenzelm@52778
   563
  apply (simp add: divides_def)
wenzelm@52778
   564
  apply (rule_tac x = "[]" in exI)
wenzelm@52778
   565
  apply (auto simp add: fun_eq)
wenzelm@52778
   566
  done
chaieb@33153
   567
chaieb@33153
   568
text{*At last, we can consider the order of a root.*}
chaieb@33153
   569
haftmann@54219
   570
lemma (in idom_char_0) poly_order_exists_lemma:
haftmann@54219
   571
  assumes lp: "length p = d"
haftmann@54219
   572
    and p: "poly p \<noteq> poly []"
haftmann@54219
   573
  shows "\<exists>n q. p = mulexp n [-a, 1] q \<and> poly q a \<noteq> 0"
haftmann@54219
   574
  using lp p
haftmann@54219
   575
proof (induct d arbitrary: p)
haftmann@54219
   576
  case 0
haftmann@54219
   577
  thus ?case by simp
haftmann@54219
   578
next
haftmann@54219
   579
  case (Suc n p)
haftmann@54219
   580
  show ?case
haftmann@54219
   581
  proof (cases "poly p a = 0")
haftmann@54219
   582
    case True
haftmann@54219
   583
    from Suc.prems have h: "length p = Suc n" "poly p \<noteq> poly []" by auto
haftmann@54219
   584
    hence pN: "p \<noteq> []" by auto
haftmann@54219
   585
    from True[unfolded poly_linear_divides] pN obtain q where q: "p = [-a, 1] *** q"
haftmann@54219
   586
      by blast
haftmann@54219
   587
    from q h True have qh: "length q = n" "poly q \<noteq> poly []"
haftmann@54219
   588
      apply -
haftmann@54219
   589
      apply simp
haftmann@54219
   590
      apply (simp only: fun_eq)
haftmann@54219
   591
      apply (rule ccontr)
haftmann@54219
   592
      apply (simp add: fun_eq poly_add poly_cmult)
haftmann@54219
   593
      done
haftmann@54219
   594
    from Suc.hyps[OF qh] obtain m r where mr: "q = mulexp m [-a,1] r" "poly r a \<noteq> 0"
haftmann@54219
   595
      by blast
haftmann@54219
   596
    from mr q have "p = mulexp (Suc m) [-a,1] r \<and> poly r a \<noteq> 0" by simp
haftmann@54219
   597
    then show ?thesis by blast
haftmann@54219
   598
  next
haftmann@54219
   599
    case False
haftmann@54219
   600
    then show ?thesis
haftmann@54219
   601
      using Suc.prems
haftmann@54219
   602
      apply simp
haftmann@54219
   603
      apply (rule exI[where x="0::nat"])
haftmann@54219
   604
      apply simp
haftmann@54219
   605
      done
haftmann@54219
   606
  qed
haftmann@54219
   607
qed
haftmann@54219
   608
haftmann@54219
   609
haftmann@54219
   610
lemma (in comm_semiring_1) poly_mulexp: "poly (mulexp n p q) x = (poly p x) ^ n * poly q x"
haftmann@57514
   611
  by (induct n) (auto simp add: poly_mult ac_simps)
haftmann@54219
   612
haftmann@54219
   613
lemma (in comm_semiring_1) divides_left_mult:
haftmann@54219
   614
  assumes d:"(p***q) divides r" shows "p divides r \<and> q divides r"
haftmann@54219
   615
proof-
haftmann@54219
   616
  from d obtain t where r:"poly r = poly (p***q *** t)"
haftmann@54219
   617
    unfolding divides_def by blast
haftmann@54219
   618
  hence "poly r = poly (p *** (q *** t))"
haftmann@57514
   619
    "poly r = poly (q *** (p***t))" by(auto simp add: fun_eq poly_mult ac_simps)
haftmann@54219
   620
  thus ?thesis unfolding divides_def by blast
haftmann@54219
   621
qed
haftmann@54219
   622
chaieb@33153
   623
chaieb@33153
   624
(* FIXME: Tidy up *)
haftmann@54219
   625
haftmann@54219
   626
lemma (in semiring_1) zero_power_iff: "0 ^ n = (if n = 0 then 1 else 0)"
haftmann@54219
   627
  by (induct n) simp_all
chaieb@33153
   628
haftmann@54219
   629
lemma (in idom_char_0) poly_order_exists:
haftmann@54219
   630
  assumes "length p = d" and "poly p \<noteq> poly []"
haftmann@54219
   631
  shows "\<exists>n. [- a, 1] %^ n divides p \<and> \<not> [- a, 1] %^ Suc n divides p"
haftmann@54219
   632
proof -
haftmann@54219
   633
  from assms have "\<exists>n q. p = mulexp n [- a, 1] q \<and> poly q a \<noteq> 0"
haftmann@54219
   634
    by (rule poly_order_exists_lemma)
haftmann@54219
   635
  then obtain n q where p: "p = mulexp n [- a, 1] q" and "poly q a \<noteq> 0" by blast
haftmann@54219
   636
  have "[- a, 1] %^ n divides mulexp n [- a, 1] q"
haftmann@54219
   637
  proof (rule dividesI)
haftmann@54219
   638
    show "poly (mulexp n [- a, 1] q) = poly ([- a, 1] %^ n *** q)"
haftmann@54230
   639
      by (induct n) (simp_all add: poly_add poly_cmult poly_mult algebra_simps)
haftmann@54219
   640
  qed
haftmann@54219
   641
  moreover have "\<not> [- a, 1] %^ Suc n divides mulexp n [- a, 1] q"
haftmann@54219
   642
  proof
haftmann@54219
   643
    assume "[- a, 1] %^ Suc n divides mulexp n [- a, 1] q"
haftmann@54219
   644
    then obtain m where "poly (mulexp n [- a, 1] q) = poly ([- a, 1] %^ Suc n *** m)"
haftmann@54219
   645
      by (rule dividesE)
haftmann@54219
   646
    moreover have "poly (mulexp n [- a, 1] q) \<noteq> poly ([- a, 1] %^ Suc n *** m)"
haftmann@54219
   647
    proof (induct n)
haftmann@54219
   648
      case 0 show ?case
haftmann@54219
   649
      proof (rule ccontr)
haftmann@54219
   650
        assume "\<not> poly (mulexp 0 [- a, 1] q) \<noteq> poly ([- a, 1] %^ Suc 0 *** m)"
haftmann@54219
   651
        then have "poly q a = 0"
haftmann@54219
   652
          by (simp add: poly_add poly_cmult)
haftmann@54219
   653
        with `poly q a \<noteq> 0` show False by simp
haftmann@54219
   654
      qed
haftmann@54219
   655
    next
haftmann@54219
   656
      case (Suc n) show ?case
haftmann@54219
   657
        by (rule pexp_Suc [THEN ssubst], rule ccontr)
haftmann@54219
   658
          (simp add: poly_mult_left_cancel poly_mult_assoc Suc del: pmult_Cons pexp_Suc)
haftmann@54219
   659
    qed
haftmann@54219
   660
    ultimately show False by simp
haftmann@54219
   661
  qed
haftmann@54219
   662
  ultimately show ?thesis by (auto simp add: p)
haftmann@54219
   663
qed
chaieb@33153
   664
haftmann@54219
   665
lemma (in semiring_1) poly_one_divides[simp]: "[1] divides p"
haftmann@54219
   666
  by (auto simp add: divides_def)
haftmann@54219
   667
haftmann@54219
   668
lemma (in idom_char_0) poly_order:
haftmann@54219
   669
  "poly p \<noteq> poly [] \<Longrightarrow> \<exists>!n. ([-a, 1] %^ n) divides p \<and> \<not> (([-a, 1] %^ Suc n) divides p)"
wenzelm@52778
   670
  apply (auto intro: poly_order_exists simp add: less_linear simp del: pmult_Cons pexp_Suc)
wenzelm@52778
   671
  apply (cut_tac x = y and y = n in less_linear)
wenzelm@52778
   672
  apply (drule_tac m = n in poly_exp_divides)
wenzelm@52778
   673
  apply (auto dest: Suc_le_eq [THEN iffD2, THEN [2] poly_exp_divides]
haftmann@54219
   674
              simp del: pmult_Cons pexp_Suc)
wenzelm@52778
   675
  done
chaieb@33153
   676
chaieb@33153
   677
text{*Order*}
chaieb@33153
   678
haftmann@54219
   679
lemma some1_equalityD: "n = (SOME n. P n) \<Longrightarrow> \<exists>!n. P n \<Longrightarrow> P n"
wenzelm@52778
   680
  by (blast intro: someI2)
chaieb@33153
   681
haftmann@54219
   682
lemma (in idom_char_0) order:
haftmann@54219
   683
      "(([-a, 1] %^ n) divides p \<and>
haftmann@54219
   684
        ~(([-a, 1] %^ (Suc n)) divides p)) =
haftmann@54219
   685
        ((n = order a p) \<and> ~(poly p = poly []))"
wenzelm@52778
   686
  apply (unfold order_def)
wenzelm@52778
   687
  apply (rule iffI)
wenzelm@52778
   688
  apply (blast dest: poly_divides_zero intro!: some1_equality [symmetric] poly_order)
wenzelm@52778
   689
  apply (blast intro!: poly_order [THEN [2] some1_equalityD])
wenzelm@52778
   690
  done
chaieb@33153
   691
haftmann@54219
   692
lemma (in idom_char_0) order2:
haftmann@54219
   693
  "poly p \<noteq> poly [] \<Longrightarrow>
haftmann@54219
   694
    ([-a, 1] %^ (order a p)) divides p \<and> \<not> (([-a, 1] %^ (Suc (order a p))) divides p)"
wenzelm@52778
   695
  by (simp add: order del: pexp_Suc)
chaieb@33153
   696
haftmann@54219
   697
lemma (in idom_char_0) order_unique:
haftmann@54219
   698
  "poly p \<noteq> poly [] \<Longrightarrow> ([-a, 1] %^ n) divides p \<Longrightarrow> ~(([-a, 1] %^ (Suc n)) divides p) \<Longrightarrow>
haftmann@54219
   699
    n = order a p"
wenzelm@52778
   700
  using order [of a n p] by auto
chaieb@33153
   701
haftmann@54219
   702
lemma (in idom_char_0) order_unique_lemma:
haftmann@54219
   703
  "poly p \<noteq> poly [] \<and> ([-a, 1] %^ n) divides p \<and> ~(([-a, 1] %^ (Suc n)) divides p) \<Longrightarrow>
wenzelm@52881
   704
    n = order a p"
wenzelm@52778
   705
  by (blast intro: order_unique)
chaieb@33153
   706
haftmann@54219
   707
lemma (in ring_1) order_poly: "poly p = poly q \<Longrightarrow> order a p = order a q"
wenzelm@52778
   708
  by (auto simp add: fun_eq divides_def poly_mult order_def)
chaieb@33153
   709
haftmann@54219
   710
lemma (in semiring_1) pexp_one[simp]: "p %^ (Suc 0) = p"
haftmann@54219
   711
  by (induct "p") auto
haftmann@54219
   712
haftmann@54219
   713
lemma (in comm_ring_1) lemma_order_root:
haftmann@54219
   714
  "0 < n \<and> [- a, 1] %^ n divides p \<and> ~ [- a, 1] %^ (Suc n) divides p \<Longrightarrow> poly p a = 0"
haftmann@54219
   715
  by (induct n arbitrary: a p) (auto simp add: divides_def poly_mult simp del: pmult_Cons)
chaieb@33153
   716
haftmann@54219
   717
lemma (in idom_char_0) order_root:
haftmann@54219
   718
  "poly p a = 0 \<longleftrightarrow> poly p = poly [] \<or> order a p \<noteq> 0"
haftmann@54219
   719
  apply (cases "poly p = poly []")
haftmann@54219
   720
  apply auto
haftmann@54219
   721
  apply (simp add: poly_linear_divides del: pmult_Cons, safe)
haftmann@54219
   722
  apply (drule_tac [!] a = a in order2)
haftmann@54219
   723
  apply (rule ccontr)
haftmann@54219
   724
  apply (simp add: divides_def poly_mult fun_eq del: pmult_Cons, blast)
haftmann@54219
   725
  using neq0_conv
haftmann@54219
   726
  apply (blast intro: lemma_order_root)
wenzelm@52778
   727
  done
chaieb@33153
   728
haftmann@54219
   729
lemma (in idom_char_0) order_divides:
haftmann@54219
   730
  "([-a, 1] %^ n) divides p \<longleftrightarrow> poly p = poly [] \<or> n \<le> order a p"
wenzelm@52881
   731
  apply (cases "poly p = poly []")
wenzelm@52881
   732
  apply auto
wenzelm@52778
   733
  apply (simp add: divides_def fun_eq poly_mult)
wenzelm@52778
   734
  apply (rule_tac x = "[]" in exI)
haftmann@54219
   735
  apply (auto dest!: order2 [where a=a] intro: poly_exp_divides simp del: pexp_Suc)
wenzelm@52778
   736
  done
chaieb@33153
   737
haftmann@54219
   738
lemma (in idom_char_0) order_decomp:
haftmann@54219
   739
  "poly p \<noteq> poly [] \<Longrightarrow> \<exists>q. poly p = poly (([-a, 1] %^ (order a p)) *** q) \<and> ~([-a, 1] divides q)"
wenzelm@52778
   740
  apply (unfold divides_def)
wenzelm@52778
   741
  apply (drule order2 [where a = a])
haftmann@54219
   742
  apply (simp add: divides_def del: pexp_Suc pmult_Cons, safe)
haftmann@54219
   743
  apply (rule_tac x = q in exI, safe)
wenzelm@52778
   744
  apply (drule_tac x = qa in spec)
haftmann@57514
   745
  apply (auto simp add: poly_mult fun_eq poly_exp ac_simps simp del: pmult_Cons)
wenzelm@52778
   746
  done
chaieb@33153
   747
chaieb@33153
   748
text{*Important composition properties of orders.*}
haftmann@54219
   749
lemma order_mult:
haftmann@54219
   750
  "poly (p *** q) \<noteq> poly [] \<Longrightarrow>
haftmann@54219
   751
    order a (p *** q) = order a p + order (a::'a::{idom_char_0}) q"
haftmann@54219
   752
  apply (cut_tac a = a and p = "p *** q" and n = "order a p + order a q" in order)
wenzelm@52778
   753
  apply (auto simp add: poly_entire simp del: pmult_Cons)
wenzelm@52778
   754
  apply (drule_tac a = a in order2)+
wenzelm@52778
   755
  apply safe
haftmann@54219
   756
  apply (simp add: divides_def fun_eq poly_exp_add poly_mult del: pmult_Cons, safe)
wenzelm@52778
   757
  apply (rule_tac x = "qa *** qaa" in exI)
haftmann@57514
   758
  apply (simp add: poly_mult ac_simps del: pmult_Cons)
wenzelm@52778
   759
  apply (drule_tac a = a in order_decomp)+
wenzelm@52778
   760
  apply safe
haftmann@54219
   761
  apply (subgoal_tac "[-a,1] divides (qa *** qaa) ")
wenzelm@52778
   762
  apply (simp add: poly_primes del: pmult_Cons)
wenzelm@52778
   763
  apply (auto simp add: divides_def simp del: pmult_Cons)
wenzelm@52778
   764
  apply (rule_tac x = qb in exI)
haftmann@54219
   765
  apply (subgoal_tac "poly ([-a, 1] %^ (order a p) *** (qa *** qaa)) = poly ([-a, 1] %^ (order a p) *** ([-a, 1] *** qb))")
haftmann@54219
   766
  apply (drule poly_mult_left_cancel [THEN iffD1], force)
haftmann@54219
   767
  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))) ")
haftmann@54219
   768
  apply (drule poly_mult_left_cancel [THEN iffD1], force)
haftmann@57514
   769
  apply (simp add: fun_eq poly_exp_add poly_mult ac_simps del: pmult_Cons)
wenzelm@52778
   770
  done
chaieb@33153
   771
haftmann@54219
   772
lemma (in idom_char_0) order_mult:
haftmann@54219
   773
  assumes "poly (p *** q) \<noteq> poly []"
haftmann@54219
   774
  shows "order a (p *** q) = order a p + order a q"
haftmann@54219
   775
  using assms
haftmann@54219
   776
  apply (cut_tac a = a and p = "pmult p q" and n = "order a p + order a q" in order)
haftmann@54219
   777
  apply (auto simp add: poly_entire simp del: pmult_Cons)
haftmann@54219
   778
  apply (drule_tac a = a in order2)+
haftmann@54219
   779
  apply safe
haftmann@54219
   780
  apply (simp add: divides_def fun_eq poly_exp_add poly_mult del: pmult_Cons, safe)
haftmann@54219
   781
  apply (rule_tac x = "pmult qa qaa" in exI)
haftmann@57514
   782
  apply (simp add: poly_mult ac_simps del: pmult_Cons)
haftmann@54219
   783
  apply (drule_tac a = a in order_decomp)+
haftmann@54219
   784
  apply safe
haftmann@54219
   785
  apply (subgoal_tac "[uminus a, one] divides pmult qa qaa")
haftmann@54219
   786
  apply (simp add: poly_primes del: pmult_Cons)
haftmann@54219
   787
  apply (auto simp add: divides_def simp del: pmult_Cons)
haftmann@54219
   788
  apply (rule_tac x = qb in exI)
haftmann@54219
   789
  apply (subgoal_tac "poly (pmult (pexp [uminus a, one] (order a p)) (pmult qa qaa)) =
haftmann@54219
   790
    poly (pmult (pexp [uminus a, one] (?order a p)) (pmult [uminus a, one] qb))")
haftmann@54219
   791
  apply (drule poly_mult_left_cancel [THEN iffD1], force)
haftmann@54219
   792
  apply (subgoal_tac "poly (pmult (pexp [uminus a, one] (order a q))
haftmann@54219
   793
      (pmult (pexp [uminus a, one] (order a p)) (pmult qa qaa))) =
haftmann@54219
   794
    poly (pmult (pexp [uminus a, one] (order a q))
haftmann@54219
   795
      (pmult (pexp [uminus a, one] (order a p)) (pmult [uminus a, one] qb)))")
haftmann@54219
   796
  apply (drule poly_mult_left_cancel [THEN iffD1], force)
haftmann@57514
   797
  apply (simp add: fun_eq poly_exp_add poly_mult ac_simps del: pmult_Cons)
haftmann@54219
   798
  done
haftmann@54219
   799
haftmann@54219
   800
lemma (in idom_char_0) order_root2: "poly p \<noteq> poly [] \<Longrightarrow> poly p a = 0 \<longleftrightarrow> order a p \<noteq> 0"
wenzelm@52881
   801
  by (rule order_root [THEN ssubst]) auto
chaieb@33153
   802
haftmann@54219
   803
lemma (in semiring_1) pmult_one[simp]: "[1] *** p = p" by auto
chaieb@33153
   804
haftmann@54219
   805
lemma (in semiring_0) poly_Nil_zero: "poly [] = poly [0]"
wenzelm@52778
   806
  by (simp add: fun_eq)
chaieb@33153
   807
haftmann@54219
   808
lemma (in idom_char_0) rsquarefree_decomp:
haftmann@54219
   809
  "rsquarefree p \<Longrightarrow> poly p a = 0 \<Longrightarrow>
wenzelm@52881
   810
    \<exists>q. poly p = poly ([-a, 1] *** q) \<and> poly q a \<noteq> 0"
haftmann@54219
   811
  apply (simp add: rsquarefree_def, safe)
wenzelm@52778
   812
  apply (frule_tac a = a in order_decomp)
wenzelm@52778
   813
  apply (drule_tac x = a in spec)
wenzelm@52778
   814
  apply (drule_tac a = a in order_root2 [symmetric])
wenzelm@52778
   815
  apply (auto simp del: pmult_Cons)
haftmann@54219
   816
  apply (rule_tac x = q in exI, safe)
wenzelm@52778
   817
  apply (simp add: poly_mult fun_eq)
wenzelm@52778
   818
  apply (drule_tac p1 = q in poly_linear_divides [THEN iffD1])
haftmann@54219
   819
  apply (simp add: divides_def del: pmult_Cons, safe)
wenzelm@52778
   820
  apply (drule_tac x = "[]" in spec)
wenzelm@52778
   821
  apply (auto simp add: fun_eq)
wenzelm@52778
   822
  done
chaieb@33153
   823
chaieb@33153
   824
chaieb@33153
   825
text{*Normalization of a polynomial.*}
chaieb@33153
   826
haftmann@54219
   827
lemma (in semiring_0) poly_normalize[simp]: "poly (pnormalize p) = poly p"
wenzelm@52778
   828
  by (induct p) (auto simp add: fun_eq)
chaieb@33153
   829
chaieb@33153
   830
text{*The degree of a polynomial.*}
chaieb@33153
   831
haftmann@54219
   832
lemma (in semiring_0) lemma_degree_zero: "list_all (%c. c = 0) p \<longleftrightarrow> pnormalize p = []"
wenzelm@52778
   833
  by (induct p) auto
chaieb@33153
   834
haftmann@54219
   835
lemma (in idom_char_0) degree_zero:
haftmann@54219
   836
  assumes "poly p = poly []"
haftmann@54219
   837
  shows "degree p = 0"
haftmann@54219
   838
  using assms
haftmann@54219
   839
  by (cases "pnormalize p = []") (auto simp add: degree_def poly_zero lemma_degree_zero)
chaieb@33153
   840
haftmann@54219
   841
lemma (in semiring_0) pnormalize_sing: "(pnormalize [x] = [x]) \<longleftrightarrow> x \<noteq> 0"
haftmann@54219
   842
  by simp
haftmann@54219
   843
haftmann@54219
   844
lemma (in semiring_0) pnormalize_pair: "y \<noteq> 0 \<longleftrightarrow> (pnormalize [x, y] = [x, y])"
wenzelm@52881
   845
  by simp
wenzelm@52778
   846
haftmann@54219
   847
lemma (in semiring_0) pnormal_cons: "pnormal p \<Longrightarrow> pnormal (c#p)"
chaieb@33153
   848
  unfolding pnormal_def by simp
wenzelm@52778
   849
haftmann@54219
   850
lemma (in semiring_0) pnormal_tail: "p\<noteq>[] \<Longrightarrow> pnormal (c#p) \<Longrightarrow> pnormal p"
haftmann@54219
   851
  unfolding pnormal_def by(auto split: split_if_asm)
haftmann@54219
   852
haftmann@54219
   853
haftmann@54219
   854
lemma (in semiring_0) pnormal_last_nonzero: "pnormal p \<Longrightarrow> last p \<noteq> 0"
haftmann@54219
   855
  by (induct p) (simp_all add: pnormal_def split: split_if_asm)
haftmann@54219
   856
haftmann@54219
   857
lemma (in semiring_0) pnormal_length: "pnormal p \<Longrightarrow> 0 < length p"
haftmann@54219
   858
  unfolding pnormal_def length_greater_0_conv by blast
haftmann@54219
   859
haftmann@54219
   860
lemma (in semiring_0) pnormal_last_length: "0 < length p \<Longrightarrow> last p \<noteq> 0 \<Longrightarrow> pnormal p"
haftmann@54219
   861
  by (induct p) (auto simp: pnormal_def  split: split_if_asm)
haftmann@54219
   862
haftmann@54219
   863
haftmann@54219
   864
lemma (in semiring_0) pnormal_id: "pnormal p \<longleftrightarrow> 0 < length p \<and> last p \<noteq> 0"
haftmann@54219
   865
  using pnormal_last_length pnormal_length pnormal_last_nonzero by blast
haftmann@54219
   866
haftmann@54219
   867
lemma (in idom_char_0) poly_Cons_eq:
haftmann@54219
   868
  "poly (c # cs) = poly (d # ds) \<longleftrightarrow> c = d \<and> poly cs = poly ds"
haftmann@54219
   869
  (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@54219
   870
proof
haftmann@54219
   871
  assume eq: ?lhs
haftmann@54219
   872
  hence "\<And>x. poly ((c#cs) +++ -- (d#ds)) x = 0"
haftmann@54230
   873
    by (simp only: poly_minus poly_add algebra_simps) (simp add: algebra_simps)
haftmann@54219
   874
  hence "poly ((c#cs) +++ -- (d#ds)) = poly []" by(simp add: fun_eq_iff)
haftmann@54219
   875
  hence "c = d \<and> list_all (\<lambda>x. x=0) ((cs +++ -- ds))"
haftmann@54219
   876
    unfolding poly_zero by (simp add: poly_minus_def algebra_simps)
haftmann@54219
   877
  hence "c = d \<and> (\<forall>x. poly (cs +++ -- ds) x = 0)"
haftmann@54219
   878
    unfolding poly_zero[symmetric] by simp
haftmann@54219
   879
  then show ?rhs by (simp add: poly_minus poly_add algebra_simps fun_eq_iff)
haftmann@54219
   880
next
haftmann@54219
   881
  assume ?rhs
haftmann@54219
   882
  then show ?lhs by(simp add:fun_eq_iff)
haftmann@54219
   883
qed
haftmann@54219
   884
haftmann@54219
   885
lemma (in idom_char_0) pnormalize_unique: "poly p = poly q \<Longrightarrow> pnormalize p = pnormalize q"
haftmann@54219
   886
proof (induct q arbitrary: p)
haftmann@54219
   887
  case Nil
haftmann@54219
   888
  thus ?case by (simp only: poly_zero lemma_degree_zero) simp
haftmann@54219
   889
next
haftmann@54219
   890
  case (Cons c cs p)
haftmann@54219
   891
  thus ?case
haftmann@54219
   892
  proof (induct p)
haftmann@54219
   893
    case Nil
haftmann@54219
   894
    hence "poly [] = poly (c#cs)" by blast
haftmann@54219
   895
    then have "poly (c#cs) = poly [] " by simp
haftmann@54219
   896
    thus ?case by (simp only: poly_zero lemma_degree_zero) simp
haftmann@54219
   897
  next
haftmann@54219
   898
    case (Cons d ds)
haftmann@54219
   899
    hence eq: "poly (d # ds) = poly (c # cs)" by blast
haftmann@54219
   900
    hence eq': "\<And>x. poly (d # ds) x = poly (c # cs) x" by simp
haftmann@54219
   901
    hence "poly (d # ds) 0 = poly (c # cs) 0" by blast
haftmann@54219
   902
    hence dc: "d = c" by auto
haftmann@54219
   903
    with eq have "poly ds = poly cs"
haftmann@54219
   904
      unfolding  poly_Cons_eq by simp
haftmann@54219
   905
    with Cons.prems have "pnormalize ds = pnormalize cs" by blast
haftmann@54219
   906
    with dc show ?case by simp
haftmann@54219
   907
  qed
haftmann@54219
   908
qed
haftmann@54219
   909
haftmann@54219
   910
lemma (in idom_char_0) degree_unique:
haftmann@54219
   911
  assumes pq: "poly p = poly q"
haftmann@54219
   912
  shows "degree p = degree q"
haftmann@54219
   913
  using pnormalize_unique[OF pq] unfolding degree_def by simp
haftmann@54219
   914
haftmann@54219
   915
lemma (in semiring_0) pnormalize_length:
haftmann@54219
   916
  "length (pnormalize p) \<le> length p" by (induct p) auto
haftmann@54219
   917
haftmann@54219
   918
lemma (in semiring_0) last_linear_mul_lemma:
haftmann@54219
   919
  "last ((a %* p) +++ (x#(b %* p))) = (if p = [] then x else b * last p)"
haftmann@54219
   920
  apply (induct p arbitrary: a x b)
wenzelm@52881
   921
  apply auto
blanchet@55417
   922
  apply (rename_tac a p aa x b)
haftmann@54219
   923
  apply (subgoal_tac "padd (cmult aa p) (times b a # cmult b p) \<noteq> []")
haftmann@54219
   924
  apply simp
haftmann@54219
   925
  apply (induct_tac p)
wenzelm@52881
   926
  apply auto
wenzelm@52778
   927
  done
wenzelm@52778
   928
haftmann@54219
   929
lemma (in semiring_1) last_linear_mul:
haftmann@54219
   930
  assumes p: "p \<noteq> []"
haftmann@54219
   931
  shows "last ([a,1] *** p) = last p"
haftmann@54219
   932
proof -
haftmann@54219
   933
  from p obtain c cs where cs: "p = c#cs" by (cases p) auto
haftmann@54219
   934
  from cs have eq: "[a,1] *** p = (a %* (c#cs)) +++ (0#(1 %* (c#cs)))"
haftmann@54219
   935
    by (simp add: poly_cmult_distr)
haftmann@54219
   936
  show ?thesis using cs
haftmann@54219
   937
    unfolding eq last_linear_mul_lemma by simp
haftmann@54219
   938
qed
haftmann@54219
   939
haftmann@54219
   940
lemma (in semiring_0) pnormalize_eq: "last p \<noteq> 0 \<Longrightarrow> pnormalize p = p"
haftmann@54219
   941
  by (induct p) (auto split: split_if_asm)
haftmann@54219
   942
haftmann@54219
   943
lemma (in semiring_0) last_pnormalize: "pnormalize p \<noteq> [] \<Longrightarrow> last (pnormalize p) \<noteq> 0"
haftmann@54219
   944
  by (induct p) auto
haftmann@54219
   945
haftmann@54219
   946
lemma (in semiring_0) pnormal_degree: "last p \<noteq> 0 \<Longrightarrow> degree p = length p - 1"
haftmann@54219
   947
  using pnormalize_eq[of p] unfolding degree_def by simp
wenzelm@52778
   948
haftmann@54219
   949
lemma (in semiring_0) poly_Nil_ext: "poly [] = (\<lambda>x. 0)"
haftmann@54219
   950
  by (rule ext) simp
haftmann@54219
   951
haftmann@54219
   952
lemma (in idom_char_0) linear_mul_degree:
haftmann@54219
   953
  assumes p: "poly p \<noteq> poly []"
haftmann@54219
   954
  shows "degree ([a,1] *** p) = degree p + 1"
haftmann@54219
   955
proof -
haftmann@54219
   956
  from p have pnz: "pnormalize p \<noteq> []"
haftmann@54219
   957
    unfolding poly_zero lemma_degree_zero .
haftmann@54219
   958
haftmann@54219
   959
  from last_linear_mul[OF pnz, of a] last_pnormalize[OF pnz]
haftmann@54219
   960
  have l0: "last ([a, 1] *** pnormalize p) \<noteq> 0" by simp
haftmann@54219
   961
  from last_pnormalize[OF pnz] last_linear_mul[OF pnz, of a]
haftmann@54219
   962
    pnormal_degree[OF l0] pnormal_degree[OF last_pnormalize[OF pnz]] pnz
haftmann@54219
   963
haftmann@54219
   964
  have th: "degree ([a,1] *** pnormalize p) = degree (pnormalize p) + 1"
haftmann@54219
   965
    by simp
haftmann@54219
   966
haftmann@54219
   967
  have eqs: "poly ([a,1] *** pnormalize p) = poly ([a,1] *** p)"
haftmann@54219
   968
    by (rule ext) (simp add: poly_mult poly_add poly_cmult)
haftmann@54219
   969
  from degree_unique[OF eqs] th
haftmann@54219
   970
  show ?thesis by (simp add: degree_unique[OF poly_normalize])
haftmann@54219
   971
qed
wenzelm@52778
   972
haftmann@54219
   973
lemma (in idom_char_0) linear_pow_mul_degree:
haftmann@54219
   974
  "degree([a,1] %^n *** p) = (if poly p = poly [] then 0 else degree p + n)"
haftmann@54219
   975
proof (induct n arbitrary: a p)
haftmann@54219
   976
  case (0 a p)
haftmann@54219
   977
  show ?case
haftmann@54219
   978
  proof (cases "poly p = poly []")
haftmann@54219
   979
    case True
haftmann@54219
   980
    then show ?thesis
haftmann@54219
   981
      using degree_unique[OF True] by (simp add: degree_def)
haftmann@54219
   982
  next
haftmann@54219
   983
    case False
haftmann@54219
   984
    then show ?thesis by (auto simp add: poly_Nil_ext)
haftmann@54219
   985
  qed
haftmann@54219
   986
next
haftmann@54219
   987
  case (Suc n a p)
haftmann@54219
   988
  have eq: "poly ([a,1] %^(Suc n) *** p) = poly ([a,1] %^ n *** ([a,1] *** p))"
haftmann@54219
   989
    apply (rule ext)
haftmann@54219
   990
    apply (simp add: poly_mult poly_add poly_cmult)
haftmann@57514
   991
    apply (simp add: ac_simps ac_simps distrib_left)
haftmann@54219
   992
    done
haftmann@54219
   993
  note deq = degree_unique[OF eq]
haftmann@54219
   994
  show ?case
haftmann@54219
   995
  proof (cases "poly p = poly []")
haftmann@54219
   996
    case True
haftmann@54219
   997
    with eq have eq': "poly ([a,1] %^(Suc n) *** p) = poly []"
haftmann@54219
   998
      apply -
haftmann@54219
   999
      apply (rule ext)
haftmann@54219
  1000
      apply (simp add: poly_mult poly_cmult poly_add)
haftmann@54219
  1001
      done
haftmann@54219
  1002
    from degree_unique[OF eq'] True show ?thesis
haftmann@54219
  1003
      by (simp add: degree_def)
haftmann@54219
  1004
  next
haftmann@54219
  1005
    case False
haftmann@54219
  1006
    then have ap: "poly ([a,1] *** p) \<noteq> poly []"
haftmann@54219
  1007
      using poly_mult_not_eq_poly_Nil unfolding poly_entire by auto
haftmann@54219
  1008
    have eq: "poly ([a,1] %^(Suc n) *** p) = poly ([a,1]%^n *** ([a,1] *** p))"
haftmann@54219
  1009
      by (rule ext, simp add: poly_mult poly_add poly_exp poly_cmult algebra_simps)
haftmann@54219
  1010
    from ap have ap': "(poly ([a,1] *** p) = poly []) = False"
haftmann@54219
  1011
      by blast
haftmann@54219
  1012
    have th0: "degree ([a,1]%^n *** ([a,1] *** p)) = degree ([a,1] *** p) + n"
haftmann@54219
  1013
      apply (simp only: Suc.hyps[of a "pmult [a,one] p"] ap')
haftmann@54219
  1014
      apply simp
haftmann@54219
  1015
      done
haftmann@54219
  1016
    from degree_unique[OF eq] ap False th0 linear_mul_degree[OF False, of a]
haftmann@54219
  1017
    show ?thesis by (auto simp del: poly.simps)
haftmann@54219
  1018
  qed
haftmann@54219
  1019
qed
wenzelm@52778
  1020
haftmann@54219
  1021
lemma (in idom_char_0) order_degree:
haftmann@54219
  1022
  assumes p0: "poly p \<noteq> poly []"
haftmann@54219
  1023
  shows "order a p \<le> degree p"
haftmann@54219
  1024
proof -
haftmann@54219
  1025
  from order2[OF p0, unfolded divides_def]
haftmann@54219
  1026
  obtain q where q: "poly p = poly ([- a, 1]%^ (order a p) *** q)" by blast
haftmann@54219
  1027
  {
haftmann@54219
  1028
    assume "poly q = poly []"
haftmann@54219
  1029
    with q p0 have False by (simp add: poly_mult poly_entire)
haftmann@54219
  1030
  }
haftmann@54219
  1031
  with degree_unique[OF q, unfolded linear_pow_mul_degree] show ?thesis
haftmann@54219
  1032
    by auto
haftmann@54219
  1033
qed
chaieb@33153
  1034
chaieb@33153
  1035
text{*Tidier versions of finiteness of roots.*}
chaieb@33153
  1036
haftmann@54219
  1037
lemma (in idom_char_0) poly_roots_finite_set:
haftmann@54219
  1038
  "poly p \<noteq> poly [] \<Longrightarrow> finite {x. poly p x = 0}"
wenzelm@52778
  1039
  unfolding poly_roots_finite .
chaieb@33153
  1040
chaieb@33153
  1041
text{*bound for polynomial.*}
chaieb@33153
  1042
haftmann@54219
  1043
lemma poly_mono: "abs(x) \<le> k \<Longrightarrow> abs(poly p (x::'a::{linordered_idom})) \<le> poly (map abs p) k"
wenzelm@52881
  1044
  apply (induct p)
wenzelm@52881
  1045
  apply auto
blanchet@55417
  1046
  apply (rename_tac a p)
wenzelm@52778
  1047
  apply (rule_tac y = "abs a + abs (x * poly p x)" in order_trans)
wenzelm@52778
  1048
  apply (rule abs_triangle_ineq)
wenzelm@52778
  1049
  apply (auto intro!: mult_mono simp add: abs_mult)
wenzelm@52778
  1050
  done
chaieb@33153
  1051
haftmann@54219
  1052
lemma (in semiring_0) poly_Sing: "poly [c] x = c" by simp
wenzelm@33268
  1053
chaieb@33153
  1054
end