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