src/HOL/Library/Polynomial.thy
author haftmann
Sun Sep 07 09:49:05 2014 +0200 (2014-09-07)
changeset 58199 5fbe474b5da8
parent 57862 8f074e6e22fc
child 58513 0bf0cf1d3547
permissions -rw-r--r--
explicit theory with additional, less commonly used list operations
wenzelm@41959
     1
(*  Title:      HOL/Library/Polynomial.thy
huffman@29451
     2
    Author:     Brian Huffman
wenzelm@41959
     3
    Author:     Clemens Ballarin
haftmann@52380
     4
    Author:     Florian Haftmann
huffman@29451
     5
*)
huffman@29451
     6
haftmann@52380
     7
header {* Polynomials as type over a ring structure *}
huffman@29451
     8
huffman@29451
     9
theory Polynomial
haftmann@58199
    10
imports Main GCD "~~/src/HOL/Library/More_List"
huffman@29451
    11
begin
huffman@29451
    12
haftmann@52380
    13
subsection {* Auxiliary: operations for lists (later) representing coefficients *}
haftmann@52380
    14
haftmann@52380
    15
definition cCons :: "'a::zero \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixr "##" 65)
haftmann@52380
    16
where
haftmann@52380
    17
  "x ## xs = (if xs = [] \<and> x = 0 then [] else x # xs)"
haftmann@52380
    18
haftmann@52380
    19
lemma cCons_0_Nil_eq [simp]:
haftmann@52380
    20
  "0 ## [] = []"
haftmann@52380
    21
  by (simp add: cCons_def)
haftmann@52380
    22
haftmann@52380
    23
lemma cCons_Cons_eq [simp]:
haftmann@52380
    24
  "x ## y # ys = x # y # ys"
haftmann@52380
    25
  by (simp add: cCons_def)
haftmann@52380
    26
haftmann@52380
    27
lemma cCons_append_Cons_eq [simp]:
haftmann@52380
    28
  "x ## xs @ y # ys = x # xs @ y # ys"
haftmann@52380
    29
  by (simp add: cCons_def)
haftmann@52380
    30
haftmann@52380
    31
lemma cCons_not_0_eq [simp]:
haftmann@52380
    32
  "x \<noteq> 0 \<Longrightarrow> x ## xs = x # xs"
haftmann@52380
    33
  by (simp add: cCons_def)
haftmann@52380
    34
haftmann@52380
    35
lemma strip_while_not_0_Cons_eq [simp]:
haftmann@52380
    36
  "strip_while (\<lambda>x. x = 0) (x # xs) = x ## strip_while (\<lambda>x. x = 0) xs"
haftmann@52380
    37
proof (cases "x = 0")
haftmann@52380
    38
  case False then show ?thesis by simp
haftmann@52380
    39
next
haftmann@52380
    40
  case True show ?thesis
haftmann@52380
    41
  proof (induct xs rule: rev_induct)
haftmann@52380
    42
    case Nil with True show ?case by simp
haftmann@52380
    43
  next
haftmann@52380
    44
    case (snoc y ys) then show ?case
haftmann@52380
    45
      by (cases "y = 0") (simp_all add: append_Cons [symmetric] del: append_Cons)
haftmann@52380
    46
  qed
haftmann@52380
    47
qed
haftmann@52380
    48
haftmann@52380
    49
lemma tl_cCons [simp]:
haftmann@52380
    50
  "tl (x ## xs) = xs"
haftmann@52380
    51
  by (simp add: cCons_def)
haftmann@52380
    52
haftmann@52380
    53
haftmann@52380
    54
subsection {* Almost everywhere zero functions *}
haftmann@52380
    55
haftmann@52380
    56
definition almost_everywhere_zero :: "(nat \<Rightarrow> 'a::zero) \<Rightarrow> bool"
haftmann@52380
    57
where
haftmann@52380
    58
  "almost_everywhere_zero f \<longleftrightarrow> (\<exists>n. \<forall>i>n. f i = 0)"
haftmann@52380
    59
haftmann@52380
    60
lemma almost_everywhere_zeroI:
haftmann@52380
    61
  "(\<And>i. i > n \<Longrightarrow> f i = 0) \<Longrightarrow> almost_everywhere_zero f"
haftmann@52380
    62
  by (auto simp add: almost_everywhere_zero_def)
haftmann@52380
    63
haftmann@52380
    64
lemma almost_everywhere_zeroE:
haftmann@52380
    65
  assumes "almost_everywhere_zero f"
haftmann@52380
    66
  obtains n where "\<And>i. i > n \<Longrightarrow> f i = 0"
haftmann@52380
    67
proof -
haftmann@52380
    68
  from assms have "\<exists>n. \<forall>i>n. f i = 0" by (simp add: almost_everywhere_zero_def)
haftmann@52380
    69
  then obtain n where "\<And>i. i > n \<Longrightarrow> f i = 0" by blast
haftmann@52380
    70
  with that show thesis .
haftmann@52380
    71
qed
haftmann@52380
    72
blanchet@55415
    73
lemma almost_everywhere_zero_case_nat:
haftmann@52380
    74
  assumes "almost_everywhere_zero f"
blanchet@55415
    75
  shows "almost_everywhere_zero (case_nat a f)"
haftmann@52380
    76
  using assms
haftmann@52380
    77
  by (auto intro!: almost_everywhere_zeroI elim!: almost_everywhere_zeroE split: nat.split)
haftmann@52380
    78
    blast
haftmann@52380
    79
haftmann@52380
    80
lemma almost_everywhere_zero_Suc:
haftmann@52380
    81
  assumes "almost_everywhere_zero f"
haftmann@52380
    82
  shows "almost_everywhere_zero (\<lambda>n. f (Suc n))"
haftmann@52380
    83
proof -
haftmann@52380
    84
  from assms obtain n where "\<And>i. i > n \<Longrightarrow> f i = 0" by (erule almost_everywhere_zeroE)
haftmann@52380
    85
  then have "\<And>i. i > n \<Longrightarrow> f (Suc i) = 0" by auto
haftmann@52380
    86
  then show ?thesis by (rule almost_everywhere_zeroI)
haftmann@52380
    87
qed
haftmann@52380
    88
haftmann@52380
    89
huffman@29451
    90
subsection {* Definition of type @{text poly} *}
huffman@29451
    91
haftmann@52380
    92
typedef 'a poly = "{f :: nat \<Rightarrow> 'a::zero. almost_everywhere_zero f}"
huffman@29451
    93
  morphisms coeff Abs_poly
haftmann@52380
    94
  unfolding almost_everywhere_zero_def by auto
huffman@29451
    95
haftmann@52380
    96
setup_lifting (no_code) type_definition_poly
haftmann@52380
    97
haftmann@52380
    98
lemma poly_eq_iff: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)"
wenzelm@45694
    99
  by (simp add: coeff_inject [symmetric] fun_eq_iff)
huffman@29451
   100
haftmann@52380
   101
lemma poly_eqI: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
haftmann@52380
   102
  by (simp add: poly_eq_iff)
haftmann@52380
   103
haftmann@52380
   104
lemma coeff_almost_everywhere_zero:
haftmann@52380
   105
  "almost_everywhere_zero (coeff p)"
haftmann@52380
   106
  using coeff [of p] by simp
huffman@29451
   107
huffman@29451
   108
huffman@29451
   109
subsection {* Degree of a polynomial *}
huffman@29451
   110
haftmann@52380
   111
definition degree :: "'a::zero poly \<Rightarrow> nat"
haftmann@52380
   112
where
huffman@29451
   113
  "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
huffman@29451
   114
haftmann@52380
   115
lemma coeff_eq_0:
haftmann@52380
   116
  assumes "degree p < n"
haftmann@52380
   117
  shows "coeff p n = 0"
huffman@29451
   118
proof -
haftmann@52380
   119
  from coeff_almost_everywhere_zero
haftmann@52380
   120
  have "\<exists>n. \<forall>i>n. coeff p i = 0" by (blast intro: almost_everywhere_zeroE)
haftmann@52380
   121
  then have "\<forall>i>degree p. coeff p i = 0"
huffman@29451
   122
    unfolding degree_def by (rule LeastI_ex)
haftmann@52380
   123
  with assms show ?thesis by simp
huffman@29451
   124
qed
huffman@29451
   125
huffman@29451
   126
lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
huffman@29451
   127
  by (erule contrapos_np, rule coeff_eq_0, simp)
huffman@29451
   128
huffman@29451
   129
lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
huffman@29451
   130
  unfolding degree_def by (erule Least_le)
huffman@29451
   131
huffman@29451
   132
lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0"
huffman@29451
   133
  unfolding degree_def by (drule not_less_Least, simp)
huffman@29451
   134
huffman@29451
   135
huffman@29451
   136
subsection {* The zero polynomial *}
huffman@29451
   137
huffman@29451
   138
instantiation poly :: (zero) zero
huffman@29451
   139
begin
huffman@29451
   140
haftmann@52380
   141
lift_definition zero_poly :: "'a poly"
haftmann@52380
   142
  is "\<lambda>_. 0" by (rule almost_everywhere_zeroI) simp
huffman@29451
   143
huffman@29451
   144
instance ..
haftmann@52380
   145
huffman@29451
   146
end
huffman@29451
   147
haftmann@52380
   148
lemma coeff_0 [simp]:
haftmann@52380
   149
  "coeff 0 n = 0"
haftmann@52380
   150
  by transfer rule
huffman@29451
   151
haftmann@52380
   152
lemma degree_0 [simp]:
haftmann@52380
   153
  "degree 0 = 0"
huffman@29451
   154
  by (rule order_antisym [OF degree_le le0]) simp
huffman@29451
   155
huffman@29451
   156
lemma leading_coeff_neq_0:
haftmann@52380
   157
  assumes "p \<noteq> 0"
haftmann@52380
   158
  shows "coeff p (degree p) \<noteq> 0"
huffman@29451
   159
proof (cases "degree p")
huffman@29451
   160
  case 0
huffman@29451
   161
  from `p \<noteq> 0` have "\<exists>n. coeff p n \<noteq> 0"
haftmann@52380
   162
    by (simp add: poly_eq_iff)
huffman@29451
   163
  then obtain n where "coeff p n \<noteq> 0" ..
huffman@29451
   164
  hence "n \<le> degree p" by (rule le_degree)
huffman@29451
   165
  with `coeff p n \<noteq> 0` and `degree p = 0`
huffman@29451
   166
  show "coeff p (degree p) \<noteq> 0" by simp
huffman@29451
   167
next
huffman@29451
   168
  case (Suc n)
huffman@29451
   169
  from `degree p = Suc n` have "n < degree p" by simp
huffman@29451
   170
  hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp)
huffman@29451
   171
  then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast
huffman@29451
   172
  from `degree p = Suc n` and `n < i` have "degree p \<le> i" by simp
huffman@29451
   173
  also from `coeff p i \<noteq> 0` have "i \<le> degree p" by (rule le_degree)
huffman@29451
   174
  finally have "degree p = i" .
huffman@29451
   175
  with `coeff p i \<noteq> 0` show "coeff p (degree p) \<noteq> 0" by simp
huffman@29451
   176
qed
huffman@29451
   177
haftmann@52380
   178
lemma leading_coeff_0_iff [simp]:
haftmann@52380
   179
  "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
huffman@29451
   180
  by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
huffman@29451
   181
huffman@29451
   182
huffman@29451
   183
subsection {* List-style constructor for polynomials *}
huffman@29451
   184
haftmann@52380
   185
lift_definition pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
blanchet@55415
   186
  is "\<lambda>a p. case_nat a (coeff p)"
blanchet@55415
   187
  using coeff_almost_everywhere_zero by (rule almost_everywhere_zero_case_nat)
huffman@29451
   188
haftmann@52380
   189
lemmas coeff_pCons = pCons.rep_eq
huffman@29455
   190
haftmann@52380
   191
lemma coeff_pCons_0 [simp]:
haftmann@52380
   192
  "coeff (pCons a p) 0 = a"
haftmann@52380
   193
  by transfer simp
huffman@29455
   194
haftmann@52380
   195
lemma coeff_pCons_Suc [simp]:
haftmann@52380
   196
  "coeff (pCons a p) (Suc n) = coeff p n"
huffman@29451
   197
  by (simp add: coeff_pCons)
huffman@29451
   198
haftmann@52380
   199
lemma degree_pCons_le:
haftmann@52380
   200
  "degree (pCons a p) \<le> Suc (degree p)"
haftmann@52380
   201
  by (rule degree_le) (simp add: coeff_eq_0 coeff_pCons split: nat.split)
huffman@29451
   202
huffman@29451
   203
lemma degree_pCons_eq:
huffman@29451
   204
  "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
haftmann@52380
   205
  apply (rule order_antisym [OF degree_pCons_le])
haftmann@52380
   206
  apply (rule le_degree, simp)
haftmann@52380
   207
  done
huffman@29451
   208
haftmann@52380
   209
lemma degree_pCons_0:
haftmann@52380
   210
  "degree (pCons a 0) = 0"
haftmann@52380
   211
  apply (rule order_antisym [OF _ le0])
haftmann@52380
   212
  apply (rule degree_le, simp add: coeff_pCons split: nat.split)
haftmann@52380
   213
  done
huffman@29451
   214
huffman@29460
   215
lemma degree_pCons_eq_if [simp]:
huffman@29451
   216
  "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
haftmann@52380
   217
  apply (cases "p = 0", simp_all)
haftmann@52380
   218
  apply (rule order_antisym [OF _ le0])
haftmann@52380
   219
  apply (rule degree_le, simp add: coeff_pCons split: nat.split)
haftmann@52380
   220
  apply (rule order_antisym [OF degree_pCons_le])
haftmann@52380
   221
  apply (rule le_degree, simp)
haftmann@52380
   222
  done
huffman@29451
   223
haftmann@52380
   224
lemma pCons_0_0 [simp]:
haftmann@52380
   225
  "pCons 0 0 = 0"
haftmann@52380
   226
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
huffman@29451
   227
huffman@29451
   228
lemma pCons_eq_iff [simp]:
huffman@29451
   229
  "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
haftmann@52380
   230
proof safe
huffman@29451
   231
  assume "pCons a p = pCons b q"
huffman@29451
   232
  then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
huffman@29451
   233
  then show "a = b" by simp
huffman@29451
   234
next
huffman@29451
   235
  assume "pCons a p = pCons b q"
huffman@29451
   236
  then have "\<forall>n. coeff (pCons a p) (Suc n) =
huffman@29451
   237
                 coeff (pCons b q) (Suc n)" by simp
haftmann@52380
   238
  then show "p = q" by (simp add: poly_eq_iff)
huffman@29451
   239
qed
huffman@29451
   240
haftmann@52380
   241
lemma pCons_eq_0_iff [simp]:
haftmann@52380
   242
  "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
huffman@29451
   243
  using pCons_eq_iff [of a p 0 0] by simp
huffman@29451
   244
huffman@29451
   245
lemma pCons_cases [cases type: poly]:
huffman@29451
   246
  obtains (pCons) a q where "p = pCons a q"
huffman@29451
   247
proof
huffman@29451
   248
  show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
haftmann@52380
   249
    by transfer
haftmann@52380
   250
      (simp add: Abs_poly_inverse almost_everywhere_zero_Suc fun_eq_iff split: nat.split)
huffman@29451
   251
qed
huffman@29451
   252
huffman@29451
   253
lemma pCons_induct [case_names 0 pCons, induct type: poly]:
huffman@29451
   254
  assumes zero: "P 0"
haftmann@54856
   255
  assumes pCons: "\<And>a p. a \<noteq> 0 \<or> p \<noteq> 0 \<Longrightarrow> P p \<Longrightarrow> P (pCons a p)"
huffman@29451
   256
  shows "P p"
huffman@29451
   257
proof (induct p rule: measure_induct_rule [where f=degree])
huffman@29451
   258
  case (less p)
huffman@29451
   259
  obtain a q where "p = pCons a q" by (rule pCons_cases)
huffman@29451
   260
  have "P q"
huffman@29451
   261
  proof (cases "q = 0")
huffman@29451
   262
    case True
huffman@29451
   263
    then show "P q" by (simp add: zero)
huffman@29451
   264
  next
huffman@29451
   265
    case False
huffman@29451
   266
    then have "degree (pCons a q) = Suc (degree q)"
huffman@29451
   267
      by (rule degree_pCons_eq)
huffman@29451
   268
    then have "degree q < degree p"
huffman@29451
   269
      using `p = pCons a q` by simp
huffman@29451
   270
    then show "P q"
huffman@29451
   271
      by (rule less.hyps)
huffman@29451
   272
  qed
haftmann@54856
   273
  have "P (pCons a q)"
haftmann@54856
   274
  proof (cases "a \<noteq> 0 \<or> q \<noteq> 0")
haftmann@54856
   275
    case True
haftmann@54856
   276
    with `P q` show ?thesis by (auto intro: pCons)
haftmann@54856
   277
  next
haftmann@54856
   278
    case False
haftmann@54856
   279
    with zero show ?thesis by simp
haftmann@54856
   280
  qed
huffman@29451
   281
  then show ?case
huffman@29451
   282
    using `p = pCons a q` by simp
huffman@29451
   283
qed
huffman@29451
   284
huffman@29451
   285
haftmann@52380
   286
subsection {* List-style syntax for polynomials *}
haftmann@52380
   287
haftmann@52380
   288
syntax
haftmann@52380
   289
  "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
haftmann@52380
   290
haftmann@52380
   291
translations
haftmann@52380
   292
  "[:x, xs:]" == "CONST pCons x [:xs:]"
haftmann@52380
   293
  "[:x:]" == "CONST pCons x 0"
haftmann@52380
   294
  "[:x:]" <= "CONST pCons x (_constrain 0 t)"
haftmann@52380
   295
haftmann@52380
   296
haftmann@52380
   297
subsection {* Representation of polynomials by lists of coefficients *}
haftmann@52380
   298
haftmann@52380
   299
primrec Poly :: "'a::zero list \<Rightarrow> 'a poly"
haftmann@52380
   300
where
haftmann@54855
   301
  [code_post]: "Poly [] = 0"
haftmann@54855
   302
| [code_post]: "Poly (a # as) = pCons a (Poly as)"
haftmann@52380
   303
haftmann@52380
   304
lemma Poly_replicate_0 [simp]:
haftmann@52380
   305
  "Poly (replicate n 0) = 0"
haftmann@52380
   306
  by (induct n) simp_all
haftmann@52380
   307
haftmann@52380
   308
lemma Poly_eq_0:
haftmann@52380
   309
  "Poly as = 0 \<longleftrightarrow> (\<exists>n. as = replicate n 0)"
haftmann@52380
   310
  by (induct as) (auto simp add: Cons_replicate_eq)
haftmann@52380
   311
haftmann@52380
   312
definition coeffs :: "'a poly \<Rightarrow> 'a::zero list"
haftmann@52380
   313
where
haftmann@52380
   314
  "coeffs p = (if p = 0 then [] else map (\<lambda>i. coeff p i) [0 ..< Suc (degree p)])"
haftmann@52380
   315
haftmann@52380
   316
lemma coeffs_eq_Nil [simp]:
haftmann@52380
   317
  "coeffs p = [] \<longleftrightarrow> p = 0"
haftmann@52380
   318
  by (simp add: coeffs_def)
haftmann@52380
   319
haftmann@52380
   320
lemma not_0_coeffs_not_Nil:
haftmann@52380
   321
  "p \<noteq> 0 \<Longrightarrow> coeffs p \<noteq> []"
haftmann@52380
   322
  by simp
haftmann@52380
   323
haftmann@52380
   324
lemma coeffs_0_eq_Nil [simp]:
haftmann@52380
   325
  "coeffs 0 = []"
haftmann@52380
   326
  by simp
huffman@29454
   327
haftmann@52380
   328
lemma coeffs_pCons_eq_cCons [simp]:
haftmann@52380
   329
  "coeffs (pCons a p) = a ## coeffs p"
haftmann@52380
   330
proof -
haftmann@52380
   331
  { fix ms :: "nat list" and f :: "nat \<Rightarrow> 'a" and x :: "'a"
haftmann@52380
   332
    assume "\<forall>m\<in>set ms. m > 0"
blanchet@55415
   333
    then have "map (case_nat x f) ms = map f (map (\<lambda>n. n - 1) ms)"
haftmann@58199
   334
      by (induct ms) (auto split: nat.split)
haftmann@58199
   335
  }
haftmann@52380
   336
  note * = this
haftmann@52380
   337
  show ?thesis
haftmann@52380
   338
    by (simp add: coeffs_def * upt_conv_Cons coeff_pCons map_decr_upt One_nat_def del: upt_Suc)
haftmann@52380
   339
qed
haftmann@52380
   340
haftmann@52380
   341
lemma not_0_cCons_eq [simp]:
haftmann@52380
   342
  "p \<noteq> 0 \<Longrightarrow> a ## coeffs p = a # coeffs p"
haftmann@52380
   343
  by (simp add: cCons_def)
haftmann@52380
   344
haftmann@52380
   345
lemma Poly_coeffs [simp, code abstype]:
haftmann@52380
   346
  "Poly (coeffs p) = p"
haftmann@54856
   347
  by (induct p) auto
haftmann@52380
   348
haftmann@52380
   349
lemma coeffs_Poly [simp]:
haftmann@52380
   350
  "coeffs (Poly as) = strip_while (HOL.eq 0) as"
haftmann@52380
   351
proof (induct as)
haftmann@52380
   352
  case Nil then show ?case by simp
haftmann@52380
   353
next
haftmann@52380
   354
  case (Cons a as)
haftmann@52380
   355
  have "(\<forall>n. as \<noteq> replicate n 0) \<longleftrightarrow> (\<exists>a\<in>set as. a \<noteq> 0)"
haftmann@52380
   356
    using replicate_length_same [of as 0] by (auto dest: sym [of _ as])
haftmann@52380
   357
  with Cons show ?case by auto
haftmann@52380
   358
qed
haftmann@52380
   359
haftmann@52380
   360
lemma last_coeffs_not_0:
haftmann@52380
   361
  "p \<noteq> 0 \<Longrightarrow> last (coeffs p) \<noteq> 0"
haftmann@52380
   362
  by (induct p) (auto simp add: cCons_def)
haftmann@52380
   363
haftmann@52380
   364
lemma strip_while_coeffs [simp]:
haftmann@52380
   365
  "strip_while (HOL.eq 0) (coeffs p) = coeffs p"
haftmann@52380
   366
  by (cases "p = 0") (auto dest: last_coeffs_not_0 intro: strip_while_not_last)
haftmann@52380
   367
haftmann@52380
   368
lemma coeffs_eq_iff:
haftmann@52380
   369
  "p = q \<longleftrightarrow> coeffs p = coeffs q" (is "?P \<longleftrightarrow> ?Q")
haftmann@52380
   370
proof
haftmann@52380
   371
  assume ?P then show ?Q by simp
haftmann@52380
   372
next
haftmann@52380
   373
  assume ?Q
haftmann@52380
   374
  then have "Poly (coeffs p) = Poly (coeffs q)" by simp
haftmann@52380
   375
  then show ?P by simp
haftmann@52380
   376
qed
haftmann@52380
   377
haftmann@52380
   378
lemma coeff_Poly_eq:
haftmann@52380
   379
  "coeff (Poly xs) n = nth_default 0 xs n"
haftmann@52380
   380
  apply (induct xs arbitrary: n) apply simp_all
blanchet@55642
   381
  by (metis nat.case not0_implies_Suc nth_default_Cons_0 nth_default_Cons_Suc pCons.rep_eq)
huffman@29454
   382
haftmann@52380
   383
lemma nth_default_coeffs_eq:
haftmann@52380
   384
  "nth_default 0 (coeffs p) = coeff p"
haftmann@52380
   385
  by (simp add: fun_eq_iff coeff_Poly_eq [symmetric])
haftmann@52380
   386
haftmann@52380
   387
lemma [code]:
haftmann@52380
   388
  "coeff p = nth_default 0 (coeffs p)"
haftmann@52380
   389
  by (simp add: nth_default_coeffs_eq)
haftmann@52380
   390
haftmann@52380
   391
lemma coeffs_eqI:
haftmann@52380
   392
  assumes coeff: "\<And>n. coeff p n = nth_default 0 xs n"
haftmann@52380
   393
  assumes zero: "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0"
haftmann@52380
   394
  shows "coeffs p = xs"
haftmann@52380
   395
proof -
haftmann@52380
   396
  from coeff have "p = Poly xs" by (simp add: poly_eq_iff coeff_Poly_eq)
haftmann@52380
   397
  with zero show ?thesis by simp (cases xs, simp_all)
haftmann@52380
   398
qed
haftmann@52380
   399
haftmann@52380
   400
lemma degree_eq_length_coeffs [code]:
haftmann@52380
   401
  "degree p = length (coeffs p) - 1"
haftmann@52380
   402
  by (simp add: coeffs_def)
haftmann@52380
   403
haftmann@52380
   404
lemma length_coeffs_degree:
haftmann@52380
   405
  "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = Suc (degree p)"
haftmann@52380
   406
  by (induct p) (auto simp add: cCons_def)
haftmann@52380
   407
haftmann@52380
   408
lemma [code abstract]:
haftmann@52380
   409
  "coeffs 0 = []"
haftmann@52380
   410
  by (fact coeffs_0_eq_Nil)
haftmann@52380
   411
haftmann@52380
   412
lemma [code abstract]:
haftmann@52380
   413
  "coeffs (pCons a p) = a ## coeffs p"
haftmann@52380
   414
  by (fact coeffs_pCons_eq_cCons)
haftmann@52380
   415
haftmann@52380
   416
instantiation poly :: ("{zero, equal}") equal
haftmann@52380
   417
begin
haftmann@52380
   418
haftmann@52380
   419
definition
haftmann@52380
   420
  [code]: "HOL.equal (p::'a poly) q \<longleftrightarrow> HOL.equal (coeffs p) (coeffs q)"
haftmann@52380
   421
haftmann@52380
   422
instance proof
haftmann@52380
   423
qed (simp add: equal equal_poly_def coeffs_eq_iff)
haftmann@52380
   424
haftmann@52380
   425
end
haftmann@52380
   426
haftmann@52380
   427
lemma [code nbe]:
haftmann@52380
   428
  "HOL.equal (p :: _ poly) p \<longleftrightarrow> True"
haftmann@52380
   429
  by (fact equal_refl)
huffman@29454
   430
haftmann@52380
   431
definition is_zero :: "'a::zero poly \<Rightarrow> bool"
haftmann@52380
   432
where
haftmann@52380
   433
  [code]: "is_zero p \<longleftrightarrow> List.null (coeffs p)"
haftmann@52380
   434
haftmann@52380
   435
lemma is_zero_null [code_abbrev]:
haftmann@52380
   436
  "is_zero p \<longleftrightarrow> p = 0"
haftmann@52380
   437
  by (simp add: is_zero_def null_def)
haftmann@52380
   438
haftmann@52380
   439
haftmann@52380
   440
subsection {* Fold combinator for polynomials *}
haftmann@52380
   441
haftmann@52380
   442
definition fold_coeffs :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b"
haftmann@52380
   443
where
haftmann@52380
   444
  "fold_coeffs f p = foldr f (coeffs p)"
haftmann@52380
   445
haftmann@52380
   446
lemma fold_coeffs_0_eq [simp]:
haftmann@52380
   447
  "fold_coeffs f 0 = id"
haftmann@52380
   448
  by (simp add: fold_coeffs_def)
haftmann@52380
   449
haftmann@52380
   450
lemma fold_coeffs_pCons_eq [simp]:
haftmann@52380
   451
  "f 0 = id \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
haftmann@52380
   452
  by (simp add: fold_coeffs_def cCons_def fun_eq_iff)
huffman@29454
   453
haftmann@52380
   454
lemma fold_coeffs_pCons_0_0_eq [simp]:
haftmann@52380
   455
  "fold_coeffs f (pCons 0 0) = id"
haftmann@52380
   456
  by (simp add: fold_coeffs_def)
haftmann@52380
   457
haftmann@52380
   458
lemma fold_coeffs_pCons_coeff_not_0_eq [simp]:
haftmann@52380
   459
  "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
haftmann@52380
   460
  by (simp add: fold_coeffs_def)
haftmann@52380
   461
haftmann@52380
   462
lemma fold_coeffs_pCons_not_0_0_eq [simp]:
haftmann@52380
   463
  "p \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
haftmann@52380
   464
  by (simp add: fold_coeffs_def)
haftmann@52380
   465
haftmann@52380
   466
haftmann@52380
   467
subsection {* Canonical morphism on polynomials -- evaluation *}
haftmann@52380
   468
haftmann@52380
   469
definition poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@52380
   470
where
haftmann@52380
   471
  "poly p = fold_coeffs (\<lambda>a f x. a + x * f x) p (\<lambda>x. 0)" -- {* The Horner Schema *}
haftmann@52380
   472
haftmann@52380
   473
lemma poly_0 [simp]:
haftmann@52380
   474
  "poly 0 x = 0"
haftmann@52380
   475
  by (simp add: poly_def)
haftmann@52380
   476
haftmann@52380
   477
lemma poly_pCons [simp]:
haftmann@52380
   478
  "poly (pCons a p) x = a + x * poly p x"
haftmann@52380
   479
  by (cases "p = 0 \<and> a = 0") (auto simp add: poly_def)
huffman@29454
   480
huffman@29454
   481
huffman@29451
   482
subsection {* Monomials *}
huffman@29451
   483
haftmann@52380
   484
lift_definition monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly"
haftmann@52380
   485
  is "\<lambda>a m n. if m = n then a else 0"
haftmann@52380
   486
  by (auto intro!: almost_everywhere_zeroI)
haftmann@52380
   487
haftmann@52380
   488
lemma coeff_monom [simp]:
haftmann@52380
   489
  "coeff (monom a m) n = (if m = n then a else 0)"
haftmann@52380
   490
  by transfer rule
huffman@29451
   491
haftmann@52380
   492
lemma monom_0:
haftmann@52380
   493
  "monom a 0 = pCons a 0"
haftmann@52380
   494
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
huffman@29451
   495
haftmann@52380
   496
lemma monom_Suc:
haftmann@52380
   497
  "monom a (Suc n) = pCons 0 (monom a n)"
haftmann@52380
   498
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
huffman@29451
   499
huffman@29451
   500
lemma monom_eq_0 [simp]: "monom 0 n = 0"
haftmann@52380
   501
  by (rule poly_eqI) simp
huffman@29451
   502
huffman@29451
   503
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
haftmann@52380
   504
  by (simp add: poly_eq_iff)
huffman@29451
   505
huffman@29451
   506
lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
haftmann@52380
   507
  by (simp add: poly_eq_iff)
huffman@29451
   508
huffman@29451
   509
lemma degree_monom_le: "degree (monom a n) \<le> n"
huffman@29451
   510
  by (rule degree_le, simp)
huffman@29451
   511
huffman@29451
   512
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
huffman@29451
   513
  apply (rule order_antisym [OF degree_monom_le])
huffman@29451
   514
  apply (rule le_degree, simp)
huffman@29451
   515
  done
huffman@29451
   516
haftmann@52380
   517
lemma coeffs_monom [code abstract]:
haftmann@52380
   518
  "coeffs (monom a n) = (if a = 0 then [] else replicate n 0 @ [a])"
haftmann@52380
   519
  by (induct n) (simp_all add: monom_0 monom_Suc)
haftmann@52380
   520
haftmann@52380
   521
lemma fold_coeffs_monom [simp]:
haftmann@52380
   522
  "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (monom a n) = f 0 ^^ n \<circ> f a"
haftmann@52380
   523
  by (simp add: fold_coeffs_def coeffs_monom fun_eq_iff)
haftmann@52380
   524
haftmann@52380
   525
lemma poly_monom:
haftmann@52380
   526
  fixes a x :: "'a::{comm_semiring_1}"
haftmann@52380
   527
  shows "poly (monom a n) x = a * x ^ n"
haftmann@52380
   528
  by (cases "a = 0", simp_all)
haftmann@52380
   529
    (induct n, simp_all add: mult.left_commute poly_def)
haftmann@52380
   530
huffman@29451
   531
huffman@29451
   532
subsection {* Addition and subtraction *}
huffman@29451
   533
huffman@29451
   534
instantiation poly :: (comm_monoid_add) comm_monoid_add
huffman@29451
   535
begin
huffman@29451
   536
haftmann@52380
   537
lift_definition plus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
haftmann@52380
   538
  is "\<lambda>p q n. coeff p n + coeff q n"
haftmann@52380
   539
proof (rule almost_everywhere_zeroI) 
haftmann@52380
   540
  fix q p :: "'a poly" and i
haftmann@52380
   541
  assume "max (degree q) (degree p) < i"
haftmann@52380
   542
  then show "coeff p i + coeff q i = 0"
haftmann@52380
   543
    by (simp add: coeff_eq_0)
haftmann@52380
   544
qed
huffman@29451
   545
huffman@29451
   546
lemma coeff_add [simp]:
huffman@29451
   547
  "coeff (p + q) n = coeff p n + coeff q n"
haftmann@52380
   548
  by (simp add: plus_poly.rep_eq)
huffman@29451
   549
huffman@29451
   550
instance proof
huffman@29451
   551
  fix p q r :: "'a poly"
huffman@29451
   552
  show "(p + q) + r = p + (q + r)"
haftmann@57512
   553
    by (simp add: poly_eq_iff add.assoc)
huffman@29451
   554
  show "p + q = q + p"
haftmann@57512
   555
    by (simp add: poly_eq_iff add.commute)
huffman@29451
   556
  show "0 + p = p"
haftmann@52380
   557
    by (simp add: poly_eq_iff)
huffman@29451
   558
qed
huffman@29451
   559
huffman@29451
   560
end
huffman@29451
   561
huffman@29904
   562
instance poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
huffman@29540
   563
proof
huffman@29540
   564
  fix p q r :: "'a poly"
huffman@29540
   565
  assume "p + q = p + r" thus "q = r"
haftmann@52380
   566
    by (simp add: poly_eq_iff)
huffman@29540
   567
qed
huffman@29540
   568
huffman@29451
   569
instantiation poly :: (ab_group_add) ab_group_add
huffman@29451
   570
begin
huffman@29451
   571
haftmann@52380
   572
lift_definition uminus_poly :: "'a poly \<Rightarrow> 'a poly"
haftmann@52380
   573
  is "\<lambda>p n. - coeff p n"
haftmann@52380
   574
proof (rule almost_everywhere_zeroI)
haftmann@52380
   575
  fix p :: "'a poly" and i
haftmann@52380
   576
  assume "degree p < i"
haftmann@52380
   577
  then show "- coeff p i = 0"
haftmann@52380
   578
    by (simp add: coeff_eq_0)
haftmann@52380
   579
qed
huffman@29451
   580
haftmann@52380
   581
lift_definition minus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
haftmann@52380
   582
  is "\<lambda>p q n. coeff p n - coeff q n"
haftmann@52380
   583
proof (rule almost_everywhere_zeroI) 
haftmann@52380
   584
  fix q p :: "'a poly" and i
haftmann@52380
   585
  assume "max (degree q) (degree p) < i"
haftmann@52380
   586
  then show "coeff p i - coeff q i = 0"
haftmann@52380
   587
    by (simp add: coeff_eq_0)
haftmann@52380
   588
qed
huffman@29451
   589
huffman@29451
   590
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
haftmann@52380
   591
  by (simp add: uminus_poly.rep_eq)
huffman@29451
   592
huffman@29451
   593
lemma coeff_diff [simp]:
huffman@29451
   594
  "coeff (p - q) n = coeff p n - coeff q n"
haftmann@52380
   595
  by (simp add: minus_poly.rep_eq)
huffman@29451
   596
huffman@29451
   597
instance proof
huffman@29451
   598
  fix p q :: "'a poly"
huffman@29451
   599
  show "- p + p = 0"
haftmann@52380
   600
    by (simp add: poly_eq_iff)
huffman@29451
   601
  show "p - q = p + - q"
haftmann@54230
   602
    by (simp add: poly_eq_iff)
huffman@29451
   603
qed
huffman@29451
   604
huffman@29451
   605
end
huffman@29451
   606
huffman@29451
   607
lemma add_pCons [simp]:
huffman@29451
   608
  "pCons a p + pCons b q = pCons (a + b) (p + q)"
haftmann@52380
   609
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
huffman@29451
   610
huffman@29451
   611
lemma minus_pCons [simp]:
huffman@29451
   612
  "- pCons a p = pCons (- a) (- p)"
haftmann@52380
   613
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
huffman@29451
   614
huffman@29451
   615
lemma diff_pCons [simp]:
huffman@29451
   616
  "pCons a p - pCons b q = pCons (a - b) (p - q)"
haftmann@52380
   617
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
huffman@29451
   618
huffman@29539
   619
lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"
huffman@29451
   620
  by (rule degree_le, auto simp add: coeff_eq_0)
huffman@29451
   621
huffman@29539
   622
lemma degree_add_le:
huffman@29539
   623
  "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n"
huffman@29539
   624
  by (auto intro: order_trans degree_add_le_max)
huffman@29539
   625
huffman@29453
   626
lemma degree_add_less:
huffman@29453
   627
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"
huffman@29539
   628
  by (auto intro: le_less_trans degree_add_le_max)
huffman@29453
   629
huffman@29451
   630
lemma degree_add_eq_right:
huffman@29451
   631
  "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
huffman@29451
   632
  apply (cases "q = 0", simp)
huffman@29451
   633
  apply (rule order_antisym)
huffman@29539
   634
  apply (simp add: degree_add_le)
huffman@29451
   635
  apply (rule le_degree)
huffman@29451
   636
  apply (simp add: coeff_eq_0)
huffman@29451
   637
  done
huffman@29451
   638
huffman@29451
   639
lemma degree_add_eq_left:
huffman@29451
   640
  "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
huffman@29451
   641
  using degree_add_eq_right [of q p]
haftmann@57512
   642
  by (simp add: add.commute)
huffman@29451
   643
huffman@29451
   644
lemma degree_minus [simp]: "degree (- p) = degree p"
huffman@29451
   645
  unfolding degree_def by simp
huffman@29451
   646
huffman@29539
   647
lemma degree_diff_le_max: "degree (p - q) \<le> max (degree p) (degree q)"
huffman@29451
   648
  using degree_add_le [where p=p and q="-q"]
haftmann@54230
   649
  by simp
huffman@29451
   650
huffman@29539
   651
lemma degree_diff_le:
huffman@29539
   652
  "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p - q) \<le> n"
haftmann@54230
   653
  using degree_add_le [of p n "- q"] by simp
huffman@29539
   654
huffman@29453
   655
lemma degree_diff_less:
huffman@29453
   656
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p - q) < n"
haftmann@54230
   657
  using degree_add_less [of p n "- q"] by simp
huffman@29453
   658
huffman@29451
   659
lemma add_monom: "monom a n + monom b n = monom (a + b) n"
haftmann@52380
   660
  by (rule poly_eqI) simp
huffman@29451
   661
huffman@29451
   662
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
haftmann@52380
   663
  by (rule poly_eqI) simp
huffman@29451
   664
huffman@29451
   665
lemma minus_monom: "- monom a n = monom (-a) n"
haftmann@52380
   666
  by (rule poly_eqI) simp
huffman@29451
   667
huffman@29451
   668
lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"
huffman@29451
   669
  by (cases "finite A", induct set: finite, simp_all)
huffman@29451
   670
huffman@29451
   671
lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"
haftmann@52380
   672
  by (rule poly_eqI) (simp add: coeff_setsum)
haftmann@52380
   673
haftmann@52380
   674
fun plus_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list \<Rightarrow> 'a list"
haftmann@52380
   675
where
haftmann@52380
   676
  "plus_coeffs xs [] = xs"
haftmann@52380
   677
| "plus_coeffs [] ys = ys"
haftmann@52380
   678
| "plus_coeffs (x # xs) (y # ys) = (x + y) ## plus_coeffs xs ys"
haftmann@52380
   679
haftmann@52380
   680
lemma coeffs_plus_eq_plus_coeffs [code abstract]:
haftmann@52380
   681
  "coeffs (p + q) = plus_coeffs (coeffs p) (coeffs q)"
haftmann@52380
   682
proof -
haftmann@52380
   683
  { fix xs ys :: "'a list" and n
haftmann@52380
   684
    have "nth_default 0 (plus_coeffs xs ys) n = nth_default 0 xs n + nth_default 0 ys n"
haftmann@52380
   685
    proof (induct xs ys arbitrary: n rule: plus_coeffs.induct)
haftmann@52380
   686
      case (3 x xs y ys n) then show ?case by (cases n) (auto simp add: cCons_def)
haftmann@52380
   687
    qed simp_all }
haftmann@52380
   688
  note * = this
haftmann@52380
   689
  { fix xs ys :: "'a list"
haftmann@52380
   690
    assume "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0" and "ys \<noteq> [] \<Longrightarrow> last ys \<noteq> 0"
haftmann@52380
   691
    moreover assume "plus_coeffs xs ys \<noteq> []"
haftmann@52380
   692
    ultimately have "last (plus_coeffs xs ys) \<noteq> 0"
haftmann@52380
   693
    proof (induct xs ys rule: plus_coeffs.induct)
haftmann@52380
   694
      case (3 x xs y ys) then show ?case by (auto simp add: cCons_def) metis
haftmann@52380
   695
    qed simp_all }
haftmann@52380
   696
  note ** = this
haftmann@52380
   697
  show ?thesis
haftmann@52380
   698
    apply (rule coeffs_eqI)
haftmann@52380
   699
    apply (simp add: * nth_default_coeffs_eq)
haftmann@52380
   700
    apply (rule **)
haftmann@52380
   701
    apply (auto dest: last_coeffs_not_0)
haftmann@52380
   702
    done
haftmann@52380
   703
qed
haftmann@52380
   704
haftmann@52380
   705
lemma coeffs_uminus [code abstract]:
haftmann@52380
   706
  "coeffs (- p) = map (\<lambda>a. - a) (coeffs p)"
haftmann@52380
   707
  by (rule coeffs_eqI)
haftmann@52380
   708
    (simp_all add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq)
haftmann@52380
   709
haftmann@52380
   710
lemma [code]:
haftmann@52380
   711
  fixes p q :: "'a::ab_group_add poly"
haftmann@52380
   712
  shows "p - q = p + - q"
haftmann@54856
   713
  by (fact ab_add_uminus_conv_diff)
haftmann@52380
   714
haftmann@52380
   715
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
haftmann@52380
   716
  apply (induct p arbitrary: q, simp)
haftmann@52380
   717
  apply (case_tac q, simp, simp add: algebra_simps)
haftmann@52380
   718
  done
haftmann@52380
   719
haftmann@52380
   720
lemma poly_minus [simp]:
haftmann@52380
   721
  fixes x :: "'a::comm_ring"
haftmann@52380
   722
  shows "poly (- p) x = - poly p x"
haftmann@52380
   723
  by (induct p) simp_all
haftmann@52380
   724
haftmann@52380
   725
lemma poly_diff [simp]:
haftmann@52380
   726
  fixes x :: "'a::comm_ring"
haftmann@52380
   727
  shows "poly (p - q) x = poly p x - poly q x"
haftmann@54230
   728
  using poly_add [of p "- q" x] by simp
haftmann@52380
   729
haftmann@52380
   730
lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"
haftmann@52380
   731
  by (induct A rule: infinite_finite_induct) simp_all
huffman@29451
   732
huffman@29451
   733
haftmann@52380
   734
subsection {* Multiplication by a constant, polynomial multiplication and the unit polynomial *}
huffman@29451
   735
haftmann@52380
   736
lift_definition smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
haftmann@52380
   737
  is "\<lambda>a p n. a * coeff p n"
haftmann@52380
   738
proof (rule almost_everywhere_zeroI)
haftmann@52380
   739
  fix a :: 'a and p :: "'a poly" and i
haftmann@52380
   740
  assume "degree p < i"
haftmann@52380
   741
  then show "a * coeff p i = 0"
haftmann@52380
   742
    by (simp add: coeff_eq_0)
haftmann@52380
   743
qed
huffman@29451
   744
haftmann@52380
   745
lemma coeff_smult [simp]:
haftmann@52380
   746
  "coeff (smult a p) n = a * coeff p n"
haftmann@52380
   747
  by (simp add: smult.rep_eq)
huffman@29451
   748
huffman@29451
   749
lemma degree_smult_le: "degree (smult a p) \<le> degree p"
huffman@29451
   750
  by (rule degree_le, simp add: coeff_eq_0)
huffman@29451
   751
huffman@29472
   752
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
haftmann@57512
   753
  by (rule poly_eqI, simp add: mult.assoc)
huffman@29451
   754
huffman@29451
   755
lemma smult_0_right [simp]: "smult a 0 = 0"
haftmann@52380
   756
  by (rule poly_eqI, simp)
huffman@29451
   757
huffman@29451
   758
lemma smult_0_left [simp]: "smult 0 p = 0"
haftmann@52380
   759
  by (rule poly_eqI, simp)
huffman@29451
   760
huffman@29451
   761
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
haftmann@52380
   762
  by (rule poly_eqI, simp)
huffman@29451
   763
huffman@29451
   764
lemma smult_add_right:
huffman@29451
   765
  "smult a (p + q) = smult a p + smult a q"
haftmann@52380
   766
  by (rule poly_eqI, simp add: algebra_simps)
huffman@29451
   767
huffman@29451
   768
lemma smult_add_left:
huffman@29451
   769
  "smult (a + b) p = smult a p + smult b p"
haftmann@52380
   770
  by (rule poly_eqI, simp add: algebra_simps)
huffman@29451
   771
huffman@29457
   772
lemma smult_minus_right [simp]:
huffman@29451
   773
  "smult (a::'a::comm_ring) (- p) = - smult a p"
haftmann@52380
   774
  by (rule poly_eqI, simp)
huffman@29451
   775
huffman@29457
   776
lemma smult_minus_left [simp]:
huffman@29451
   777
  "smult (- a::'a::comm_ring) p = - smult a p"
haftmann@52380
   778
  by (rule poly_eqI, simp)
huffman@29451
   779
huffman@29451
   780
lemma smult_diff_right:
huffman@29451
   781
  "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
haftmann@52380
   782
  by (rule poly_eqI, simp add: algebra_simps)
huffman@29451
   783
huffman@29451
   784
lemma smult_diff_left:
huffman@29451
   785
  "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
haftmann@52380
   786
  by (rule poly_eqI, simp add: algebra_simps)
huffman@29451
   787
huffman@29472
   788
lemmas smult_distribs =
huffman@29472
   789
  smult_add_left smult_add_right
huffman@29472
   790
  smult_diff_left smult_diff_right
huffman@29472
   791
huffman@29451
   792
lemma smult_pCons [simp]:
huffman@29451
   793
  "smult a (pCons b p) = pCons (a * b) (smult a p)"
haftmann@52380
   794
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
huffman@29451
   795
huffman@29451
   796
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
huffman@29451
   797
  by (induct n, simp add: monom_0, simp add: monom_Suc)
huffman@29451
   798
huffman@29659
   799
lemma degree_smult_eq [simp]:
huffman@29659
   800
  fixes a :: "'a::idom"
huffman@29659
   801
  shows "degree (smult a p) = (if a = 0 then 0 else degree p)"
huffman@29659
   802
  by (cases "a = 0", simp, simp add: degree_def)
huffman@29659
   803
huffman@29659
   804
lemma smult_eq_0_iff [simp]:
huffman@29659
   805
  fixes a :: "'a::idom"
huffman@29659
   806
  shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"
haftmann@52380
   807
  by (simp add: poly_eq_iff)
huffman@29451
   808
haftmann@52380
   809
lemma coeffs_smult [code abstract]:
haftmann@52380
   810
  fixes p :: "'a::idom poly"
haftmann@52380
   811
  shows "coeffs (smult a p) = (if a = 0 then [] else map (Groups.times a) (coeffs p))"
haftmann@52380
   812
  by (rule coeffs_eqI)
haftmann@52380
   813
    (auto simp add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq)
huffman@29451
   814
huffman@29451
   815
instantiation poly :: (comm_semiring_0) comm_semiring_0
huffman@29451
   816
begin
huffman@29451
   817
huffman@29451
   818
definition
haftmann@52380
   819
  "p * q = fold_coeffs (\<lambda>a p. smult a q + pCons 0 p) p 0"
huffman@29474
   820
huffman@29474
   821
lemma mult_poly_0_left: "(0::'a poly) * q = 0"
haftmann@52380
   822
  by (simp add: times_poly_def)
huffman@29474
   823
huffman@29474
   824
lemma mult_pCons_left [simp]:
huffman@29474
   825
  "pCons a p * q = smult a q + pCons 0 (p * q)"
haftmann@52380
   826
  by (cases "p = 0 \<and> a = 0") (auto simp add: times_poly_def)
huffman@29474
   827
huffman@29474
   828
lemma mult_poly_0_right: "p * (0::'a poly) = 0"
haftmann@52380
   829
  by (induct p) (simp add: mult_poly_0_left, simp)
huffman@29451
   830
huffman@29474
   831
lemma mult_pCons_right [simp]:
huffman@29474
   832
  "p * pCons a q = smult a p + pCons 0 (p * q)"
haftmann@52380
   833
  by (induct p) (simp add: mult_poly_0_left, simp add: algebra_simps)
huffman@29474
   834
huffman@29474
   835
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
huffman@29474
   836
haftmann@52380
   837
lemma mult_smult_left [simp]:
haftmann@52380
   838
  "smult a p * q = smult a (p * q)"
haftmann@52380
   839
  by (induct p) (simp add: mult_poly_0, simp add: smult_add_right)
huffman@29474
   840
haftmann@52380
   841
lemma mult_smult_right [simp]:
haftmann@52380
   842
  "p * smult a q = smult a (p * q)"
haftmann@52380
   843
  by (induct q) (simp add: mult_poly_0, simp add: smult_add_right)
huffman@29474
   844
huffman@29474
   845
lemma mult_poly_add_left:
huffman@29474
   846
  fixes p q r :: "'a poly"
huffman@29474
   847
  shows "(p + q) * r = p * r + q * r"
haftmann@52380
   848
  by (induct r) (simp add: mult_poly_0, simp add: smult_distribs algebra_simps)
huffman@29451
   849
huffman@29451
   850
instance proof
huffman@29451
   851
  fix p q r :: "'a poly"
huffman@29451
   852
  show 0: "0 * p = 0"
huffman@29474
   853
    by (rule mult_poly_0_left)
huffman@29451
   854
  show "p * 0 = 0"
huffman@29474
   855
    by (rule mult_poly_0_right)
huffman@29451
   856
  show "(p + q) * r = p * r + q * r"
huffman@29474
   857
    by (rule mult_poly_add_left)
huffman@29451
   858
  show "(p * q) * r = p * (q * r)"
huffman@29474
   859
    by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)
huffman@29451
   860
  show "p * q = q * p"
huffman@29474
   861
    by (induct p, simp add: mult_poly_0, simp)
huffman@29451
   862
qed
huffman@29451
   863
huffman@29451
   864
end
huffman@29451
   865
huffman@29540
   866
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
huffman@29540
   867
huffman@29474
   868
lemma coeff_mult:
huffman@29474
   869
  "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
huffman@29474
   870
proof (induct p arbitrary: n)
huffman@29474
   871
  case 0 show ?case by simp
huffman@29474
   872
next
huffman@29474
   873
  case (pCons a p n) thus ?case
huffman@29474
   874
    by (cases n, simp, simp add: setsum_atMost_Suc_shift
huffman@29474
   875
                            del: setsum_atMost_Suc)
huffman@29474
   876
qed
huffman@29451
   877
huffman@29474
   878
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
huffman@29474
   879
apply (rule degree_le)
huffman@29474
   880
apply (induct p)
huffman@29474
   881
apply simp
huffman@29474
   882
apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
huffman@29451
   883
done
huffman@29451
   884
huffman@29451
   885
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
huffman@29451
   886
  by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)
huffman@29451
   887
huffman@29451
   888
instantiation poly :: (comm_semiring_1) comm_semiring_1
huffman@29451
   889
begin
huffman@29451
   890
haftmann@52380
   891
definition one_poly_def:
haftmann@52380
   892
  "1 = pCons 1 0"
huffman@29451
   893
huffman@29451
   894
instance proof
huffman@29451
   895
  fix p :: "'a poly" show "1 * p = p"
haftmann@52380
   896
    unfolding one_poly_def by simp
huffman@29451
   897
next
huffman@29451
   898
  show "0 \<noteq> (1::'a poly)"
huffman@29451
   899
    unfolding one_poly_def by simp
huffman@29451
   900
qed
huffman@29451
   901
huffman@29451
   902
end
huffman@29451
   903
huffman@29540
   904
instance poly :: (comm_semiring_1_cancel) comm_semiring_1_cancel ..
huffman@29540
   905
haftmann@52380
   906
instance poly :: (comm_ring) comm_ring ..
haftmann@52380
   907
haftmann@52380
   908
instance poly :: (comm_ring_1) comm_ring_1 ..
haftmann@52380
   909
huffman@29451
   910
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
huffman@29451
   911
  unfolding one_poly_def
huffman@29451
   912
  by (simp add: coeff_pCons split: nat.split)
huffman@29451
   913
huffman@29451
   914
lemma degree_1 [simp]: "degree 1 = 0"
huffman@29451
   915
  unfolding one_poly_def
huffman@29451
   916
  by (rule degree_pCons_0)
huffman@29451
   917
haftmann@52380
   918
lemma coeffs_1_eq [simp, code abstract]:
haftmann@52380
   919
  "coeffs 1 = [1]"
haftmann@52380
   920
  by (simp add: one_poly_def)
haftmann@52380
   921
haftmann@52380
   922
lemma degree_power_le:
haftmann@52380
   923
  "degree (p ^ n) \<le> degree p * n"
haftmann@52380
   924
  by (induct n) (auto intro: order_trans degree_mult_le)
haftmann@52380
   925
haftmann@52380
   926
lemma poly_smult [simp]:
haftmann@52380
   927
  "poly (smult a p) x = a * poly p x"
haftmann@52380
   928
  by (induct p, simp, simp add: algebra_simps)
haftmann@52380
   929
haftmann@52380
   930
lemma poly_mult [simp]:
haftmann@52380
   931
  "poly (p * q) x = poly p x * poly q x"
haftmann@52380
   932
  by (induct p, simp_all, simp add: algebra_simps)
haftmann@52380
   933
haftmann@52380
   934
lemma poly_1 [simp]:
haftmann@52380
   935
  "poly 1 x = 1"
haftmann@52380
   936
  by (simp add: one_poly_def)
haftmann@52380
   937
haftmann@52380
   938
lemma poly_power [simp]:
haftmann@52380
   939
  fixes p :: "'a::{comm_semiring_1} poly"
haftmann@52380
   940
  shows "poly (p ^ n) x = poly p x ^ n"
haftmann@52380
   941
  by (induct n) simp_all
haftmann@52380
   942
haftmann@52380
   943
haftmann@52380
   944
subsection {* Lemmas about divisibility *}
huffman@29979
   945
huffman@29979
   946
lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q"
huffman@29979
   947
proof -
huffman@29979
   948
  assume "p dvd q"
huffman@29979
   949
  then obtain k where "q = p * k" ..
huffman@29979
   950
  then have "smult a q = p * smult a k" by simp
huffman@29979
   951
  then show "p dvd smult a q" ..
huffman@29979
   952
qed
huffman@29979
   953
huffman@29979
   954
lemma dvd_smult_cancel:
huffman@29979
   955
  fixes a :: "'a::field"
huffman@29979
   956
  shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q"
huffman@29979
   957
  by (drule dvd_smult [where a="inverse a"]) simp
huffman@29979
   958
huffman@29979
   959
lemma dvd_smult_iff:
huffman@29979
   960
  fixes a :: "'a::field"
huffman@29979
   961
  shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q"
huffman@29979
   962
  by (safe elim!: dvd_smult dvd_smult_cancel)
huffman@29979
   963
huffman@31663
   964
lemma smult_dvd_cancel:
huffman@31663
   965
  "smult a p dvd q \<Longrightarrow> p dvd q"
huffman@31663
   966
proof -
huffman@31663
   967
  assume "smult a p dvd q"
huffman@31663
   968
  then obtain k where "q = smult a p * k" ..
huffman@31663
   969
  then have "q = p * smult a k" by simp
huffman@31663
   970
  then show "p dvd q" ..
huffman@31663
   971
qed
huffman@31663
   972
huffman@31663
   973
lemma smult_dvd:
huffman@31663
   974
  fixes a :: "'a::field"
huffman@31663
   975
  shows "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q"
huffman@31663
   976
  by (rule smult_dvd_cancel [where a="inverse a"]) simp
huffman@31663
   977
huffman@31663
   978
lemma smult_dvd_iff:
huffman@31663
   979
  fixes a :: "'a::field"
huffman@31663
   980
  shows "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)"
huffman@31663
   981
  by (auto elim: smult_dvd smult_dvd_cancel)
huffman@31663
   982
huffman@29451
   983
huffman@29451
   984
subsection {* Polynomials form an integral domain *}
huffman@29451
   985
huffman@29451
   986
lemma coeff_mult_degree_sum:
huffman@29451
   987
  "coeff (p * q) (degree p + degree q) =
huffman@29451
   988
   coeff p (degree p) * coeff q (degree q)"
huffman@29471
   989
  by (induct p, simp, simp add: coeff_eq_0)
huffman@29451
   990
huffman@29451
   991
instance poly :: (idom) idom
huffman@29451
   992
proof
huffman@29451
   993
  fix p q :: "'a poly"
huffman@29451
   994
  assume "p \<noteq> 0" and "q \<noteq> 0"
huffman@29451
   995
  have "coeff (p * q) (degree p + degree q) =
huffman@29451
   996
        coeff p (degree p) * coeff q (degree q)"
huffman@29451
   997
    by (rule coeff_mult_degree_sum)
huffman@29451
   998
  also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
huffman@29451
   999
    using `p \<noteq> 0` and `q \<noteq> 0` by simp
huffman@29451
  1000
  finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
haftmann@52380
  1001
  thus "p * q \<noteq> 0" by (simp add: poly_eq_iff)
huffman@29451
  1002
qed
huffman@29451
  1003
huffman@29451
  1004
lemma degree_mult_eq:
huffman@29451
  1005
  fixes p q :: "'a::idom poly"
huffman@29451
  1006
  shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q"
huffman@29451
  1007
apply (rule order_antisym [OF degree_mult_le le_degree])
huffman@29451
  1008
apply (simp add: coeff_mult_degree_sum)
huffman@29451
  1009
done
huffman@29451
  1010
huffman@29451
  1011
lemma dvd_imp_degree_le:
huffman@29451
  1012
  fixes p q :: "'a::idom poly"
huffman@29451
  1013
  shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
huffman@29451
  1014
  by (erule dvdE, simp add: degree_mult_eq)
huffman@29451
  1015
huffman@29451
  1016
huffman@29878
  1017
subsection {* Polynomials form an ordered integral domain *}
huffman@29878
  1018
haftmann@52380
  1019
definition pos_poly :: "'a::linordered_idom poly \<Rightarrow> bool"
huffman@29878
  1020
where
huffman@29878
  1021
  "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"
huffman@29878
  1022
huffman@29878
  1023
lemma pos_poly_pCons:
huffman@29878
  1024
  "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)"
huffman@29878
  1025
  unfolding pos_poly_def by simp
huffman@29878
  1026
huffman@29878
  1027
lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"
huffman@29878
  1028
  unfolding pos_poly_def by simp
huffman@29878
  1029
huffman@29878
  1030
lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)"
huffman@29878
  1031
  apply (induct p arbitrary: q, simp)
huffman@29878
  1032
  apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos)
huffman@29878
  1033
  done
huffman@29878
  1034
huffman@29878
  1035
lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)"
huffman@29878
  1036
  unfolding pos_poly_def
huffman@29878
  1037
  apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")
nipkow@56544
  1038
  apply (simp add: degree_mult_eq coeff_mult_degree_sum)
huffman@29878
  1039
  apply auto
huffman@29878
  1040
  done
huffman@29878
  1041
huffman@29878
  1042
lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)"
huffman@29878
  1043
by (induct p) (auto simp add: pos_poly_pCons)
huffman@29878
  1044
haftmann@52380
  1045
lemma last_coeffs_eq_coeff_degree:
haftmann@52380
  1046
  "p \<noteq> 0 \<Longrightarrow> last (coeffs p) = coeff p (degree p)"
haftmann@52380
  1047
  by (simp add: coeffs_def)
haftmann@52380
  1048
haftmann@52380
  1049
lemma pos_poly_coeffs [code]:
haftmann@52380
  1050
  "pos_poly p \<longleftrightarrow> (let as = coeffs p in as \<noteq> [] \<and> last as > 0)" (is "?P \<longleftrightarrow> ?Q")
haftmann@52380
  1051
proof
haftmann@52380
  1052
  assume ?Q then show ?P by (auto simp add: pos_poly_def last_coeffs_eq_coeff_degree)
haftmann@52380
  1053
next
haftmann@52380
  1054
  assume ?P then have *: "0 < coeff p (degree p)" by (simp add: pos_poly_def)
haftmann@52380
  1055
  then have "p \<noteq> 0" by auto
haftmann@52380
  1056
  with * show ?Q by (simp add: last_coeffs_eq_coeff_degree)
haftmann@52380
  1057
qed
haftmann@52380
  1058
haftmann@35028
  1059
instantiation poly :: (linordered_idom) linordered_idom
huffman@29878
  1060
begin
huffman@29878
  1061
huffman@29878
  1062
definition
haftmann@37765
  1063
  "x < y \<longleftrightarrow> pos_poly (y - x)"
huffman@29878
  1064
huffman@29878
  1065
definition
haftmann@37765
  1066
  "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"
huffman@29878
  1067
huffman@29878
  1068
definition
haftmann@37765
  1069
  "abs (x::'a poly) = (if x < 0 then - x else x)"
huffman@29878
  1070
huffman@29878
  1071
definition
haftmann@37765
  1072
  "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
huffman@29878
  1073
huffman@29878
  1074
instance proof
huffman@29878
  1075
  fix x y :: "'a poly"
huffman@29878
  1076
  show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
huffman@29878
  1077
    unfolding less_eq_poly_def less_poly_def
huffman@29878
  1078
    apply safe
huffman@29878
  1079
    apply simp
huffman@29878
  1080
    apply (drule (1) pos_poly_add)
huffman@29878
  1081
    apply simp
huffman@29878
  1082
    done
huffman@29878
  1083
next
huffman@29878
  1084
  fix x :: "'a poly" show "x \<le> x"
huffman@29878
  1085
    unfolding less_eq_poly_def by simp
huffman@29878
  1086
next
huffman@29878
  1087
  fix x y z :: "'a poly"
huffman@29878
  1088
  assume "x \<le> y" and "y \<le> z" thus "x \<le> z"
huffman@29878
  1089
    unfolding less_eq_poly_def
huffman@29878
  1090
    apply safe
huffman@29878
  1091
    apply (drule (1) pos_poly_add)
huffman@29878
  1092
    apply (simp add: algebra_simps)
huffman@29878
  1093
    done
huffman@29878
  1094
next
huffman@29878
  1095
  fix x y :: "'a poly"
huffman@29878
  1096
  assume "x \<le> y" and "y \<le> x" thus "x = y"
huffman@29878
  1097
    unfolding less_eq_poly_def
huffman@29878
  1098
    apply safe
huffman@29878
  1099
    apply (drule (1) pos_poly_add)
huffman@29878
  1100
    apply simp
huffman@29878
  1101
    done
huffman@29878
  1102
next
huffman@29878
  1103
  fix x y z :: "'a poly"
huffman@29878
  1104
  assume "x \<le> y" thus "z + x \<le> z + y"
huffman@29878
  1105
    unfolding less_eq_poly_def
huffman@29878
  1106
    apply safe
huffman@29878
  1107
    apply (simp add: algebra_simps)
huffman@29878
  1108
    done
huffman@29878
  1109
next
huffman@29878
  1110
  fix x y :: "'a poly"
huffman@29878
  1111
  show "x \<le> y \<or> y \<le> x"
huffman@29878
  1112
    unfolding less_eq_poly_def
huffman@29878
  1113
    using pos_poly_total [of "x - y"]
huffman@29878
  1114
    by auto
huffman@29878
  1115
next
huffman@29878
  1116
  fix x y z :: "'a poly"
huffman@29878
  1117
  assume "x < y" and "0 < z"
huffman@29878
  1118
  thus "z * x < z * y"
huffman@29878
  1119
    unfolding less_poly_def
huffman@29878
  1120
    by (simp add: right_diff_distrib [symmetric] pos_poly_mult)
huffman@29878
  1121
next
huffman@29878
  1122
  fix x :: "'a poly"
huffman@29878
  1123
  show "\<bar>x\<bar> = (if x < 0 then - x else x)"
huffman@29878
  1124
    by (rule abs_poly_def)
huffman@29878
  1125
next
huffman@29878
  1126
  fix x :: "'a poly"
huffman@29878
  1127
  show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
huffman@29878
  1128
    by (rule sgn_poly_def)
huffman@29878
  1129
qed
huffman@29878
  1130
huffman@29878
  1131
end
huffman@29878
  1132
huffman@29878
  1133
text {* TODO: Simplification rules for comparisons *}
huffman@29878
  1134
huffman@29878
  1135
haftmann@52380
  1136
subsection {* Synthetic division and polynomial roots *}
haftmann@52380
  1137
haftmann@52380
  1138
text {*
haftmann@52380
  1139
  Synthetic division is simply division by the linear polynomial @{term "x - c"}.
haftmann@52380
  1140
*}
haftmann@52380
  1141
haftmann@52380
  1142
definition synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"
haftmann@52380
  1143
where
haftmann@52380
  1144
  "synthetic_divmod p c = fold_coeffs (\<lambda>a (q, r). (pCons r q, a + c * r)) p (0, 0)"
haftmann@52380
  1145
haftmann@52380
  1146
definition synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
haftmann@52380
  1147
where
haftmann@52380
  1148
  "synthetic_div p c = fst (synthetic_divmod p c)"
haftmann@52380
  1149
haftmann@52380
  1150
lemma synthetic_divmod_0 [simp]:
haftmann@52380
  1151
  "synthetic_divmod 0 c = (0, 0)"
haftmann@52380
  1152
  by (simp add: synthetic_divmod_def)
haftmann@52380
  1153
haftmann@52380
  1154
lemma synthetic_divmod_pCons [simp]:
haftmann@52380
  1155
  "synthetic_divmod (pCons a p) c = (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)"
haftmann@52380
  1156
  by (cases "p = 0 \<and> a = 0") (auto simp add: synthetic_divmod_def)
haftmann@52380
  1157
haftmann@52380
  1158
lemma synthetic_div_0 [simp]:
haftmann@52380
  1159
  "synthetic_div 0 c = 0"
haftmann@52380
  1160
  unfolding synthetic_div_def by simp
haftmann@52380
  1161
haftmann@52380
  1162
lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0"
haftmann@52380
  1163
by (induct p arbitrary: a) simp_all
haftmann@52380
  1164
haftmann@52380
  1165
lemma snd_synthetic_divmod:
haftmann@52380
  1166
  "snd (synthetic_divmod p c) = poly p c"
haftmann@52380
  1167
  by (induct p, simp, simp add: split_def)
haftmann@52380
  1168
haftmann@52380
  1169
lemma synthetic_div_pCons [simp]:
haftmann@52380
  1170
  "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)"
haftmann@52380
  1171
  unfolding synthetic_div_def
haftmann@52380
  1172
  by (simp add: split_def snd_synthetic_divmod)
haftmann@52380
  1173
haftmann@52380
  1174
lemma synthetic_div_eq_0_iff:
haftmann@52380
  1175
  "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0"
haftmann@52380
  1176
  by (induct p, simp, case_tac p, simp)
haftmann@52380
  1177
haftmann@52380
  1178
lemma degree_synthetic_div:
haftmann@52380
  1179
  "degree (synthetic_div p c) = degree p - 1"
haftmann@52380
  1180
  by (induct p, simp, simp add: synthetic_div_eq_0_iff)
haftmann@52380
  1181
haftmann@52380
  1182
lemma synthetic_div_correct:
haftmann@52380
  1183
  "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)"
haftmann@52380
  1184
  by (induct p) simp_all
haftmann@52380
  1185
haftmann@52380
  1186
lemma synthetic_div_unique:
haftmann@52380
  1187
  "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c"
haftmann@52380
  1188
apply (induct p arbitrary: q r)
haftmann@52380
  1189
apply (simp, frule synthetic_div_unique_lemma, simp)
haftmann@52380
  1190
apply (case_tac q, force)
haftmann@52380
  1191
done
haftmann@52380
  1192
haftmann@52380
  1193
lemma synthetic_div_correct':
haftmann@52380
  1194
  fixes c :: "'a::comm_ring_1"
haftmann@52380
  1195
  shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"
haftmann@52380
  1196
  using synthetic_div_correct [of p c]
haftmann@52380
  1197
  by (simp add: algebra_simps)
haftmann@52380
  1198
haftmann@52380
  1199
lemma poly_eq_0_iff_dvd:
haftmann@52380
  1200
  fixes c :: "'a::idom"
haftmann@52380
  1201
  shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p"
haftmann@52380
  1202
proof
haftmann@52380
  1203
  assume "poly p c = 0"
haftmann@52380
  1204
  with synthetic_div_correct' [of c p]
haftmann@52380
  1205
  have "p = [:-c, 1:] * synthetic_div p c" by simp
haftmann@52380
  1206
  then show "[:-c, 1:] dvd p" ..
haftmann@52380
  1207
next
haftmann@52380
  1208
  assume "[:-c, 1:] dvd p"
haftmann@52380
  1209
  then obtain k where "p = [:-c, 1:] * k" by (rule dvdE)
haftmann@52380
  1210
  then show "poly p c = 0" by simp
haftmann@52380
  1211
qed
haftmann@52380
  1212
haftmann@52380
  1213
lemma dvd_iff_poly_eq_0:
haftmann@52380
  1214
  fixes c :: "'a::idom"
haftmann@52380
  1215
  shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0"
haftmann@52380
  1216
  by (simp add: poly_eq_0_iff_dvd)
haftmann@52380
  1217
haftmann@52380
  1218
lemma poly_roots_finite:
haftmann@52380
  1219
  fixes p :: "'a::idom poly"
haftmann@52380
  1220
  shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
haftmann@52380
  1221
proof (induct n \<equiv> "degree p" arbitrary: p)
haftmann@52380
  1222
  case (0 p)
haftmann@52380
  1223
  then obtain a where "a \<noteq> 0" and "p = [:a:]"
haftmann@52380
  1224
    by (cases p, simp split: if_splits)
haftmann@52380
  1225
  then show "finite {x. poly p x = 0}" by simp
haftmann@52380
  1226
next
haftmann@52380
  1227
  case (Suc n p)
haftmann@52380
  1228
  show "finite {x. poly p x = 0}"
haftmann@52380
  1229
  proof (cases "\<exists>x. poly p x = 0")
haftmann@52380
  1230
    case False
haftmann@52380
  1231
    then show "finite {x. poly p x = 0}" by simp
haftmann@52380
  1232
  next
haftmann@52380
  1233
    case True
haftmann@52380
  1234
    then obtain a where "poly p a = 0" ..
haftmann@52380
  1235
    then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd)
haftmann@52380
  1236
    then obtain k where k: "p = [:-a, 1:] * k" ..
haftmann@52380
  1237
    with `p \<noteq> 0` have "k \<noteq> 0" by auto
haftmann@52380
  1238
    with k have "degree p = Suc (degree k)"
haftmann@52380
  1239
      by (simp add: degree_mult_eq del: mult_pCons_left)
haftmann@52380
  1240
    with `Suc n = degree p` have "n = degree k" by simp
haftmann@52380
  1241
    then have "finite {x. poly k x = 0}" using `k \<noteq> 0` by (rule Suc.hyps)
haftmann@52380
  1242
    then have "finite (insert a {x. poly k x = 0})" by simp
haftmann@52380
  1243
    then show "finite {x. poly p x = 0}"
wenzelm@57862
  1244
      by (simp add: k Collect_disj_eq del: mult_pCons_left)
haftmann@52380
  1245
  qed
haftmann@52380
  1246
qed
haftmann@52380
  1247
haftmann@52380
  1248
lemma poly_eq_poly_eq_iff:
haftmann@52380
  1249
  fixes p q :: "'a::{idom,ring_char_0} poly"
haftmann@52380
  1250
  shows "poly p = poly q \<longleftrightarrow> p = q" (is "?P \<longleftrightarrow> ?Q")
haftmann@52380
  1251
proof
haftmann@52380
  1252
  assume ?Q then show ?P by simp
haftmann@52380
  1253
next
haftmann@52380
  1254
  { fix p :: "'a::{idom,ring_char_0} poly"
haftmann@52380
  1255
    have "poly p = poly 0 \<longleftrightarrow> p = 0"
haftmann@52380
  1256
      apply (cases "p = 0", simp_all)
haftmann@52380
  1257
      apply (drule poly_roots_finite)
haftmann@52380
  1258
      apply (auto simp add: infinite_UNIV_char_0)
haftmann@52380
  1259
      done
haftmann@52380
  1260
  } note this [of "p - q"]
haftmann@52380
  1261
  moreover assume ?P
haftmann@52380
  1262
  ultimately show ?Q by auto
haftmann@52380
  1263
qed
haftmann@52380
  1264
haftmann@52380
  1265
lemma poly_all_0_iff_0:
haftmann@52380
  1266
  fixes p :: "'a::{ring_char_0, idom} poly"
haftmann@52380
  1267
  shows "(\<forall>x. poly p x = 0) \<longleftrightarrow> p = 0"
haftmann@52380
  1268
  by (auto simp add: poly_eq_poly_eq_iff [symmetric])
haftmann@52380
  1269
haftmann@52380
  1270
huffman@29451
  1271
subsection {* Long division of polynomials *}
huffman@29451
  1272
haftmann@52380
  1273
definition pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
huffman@29451
  1274
where
huffman@29537
  1275
  "pdivmod_rel x y q r \<longleftrightarrow>
huffman@29451
  1276
    x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"
huffman@29451
  1277
huffman@29537
  1278
lemma pdivmod_rel_0:
huffman@29537
  1279
  "pdivmod_rel 0 y 0 0"
huffman@29537
  1280
  unfolding pdivmod_rel_def by simp
huffman@29451
  1281
huffman@29537
  1282
lemma pdivmod_rel_by_0:
huffman@29537
  1283
  "pdivmod_rel x 0 0 x"
huffman@29537
  1284
  unfolding pdivmod_rel_def by simp
huffman@29451
  1285
huffman@29451
  1286
lemma eq_zero_or_degree_less:
huffman@29451
  1287
  assumes "degree p \<le> n" and "coeff p n = 0"
huffman@29451
  1288
  shows "p = 0 \<or> degree p < n"
huffman@29451
  1289
proof (cases n)
huffman@29451
  1290
  case 0
huffman@29451
  1291
  with `degree p \<le> n` and `coeff p n = 0`
huffman@29451
  1292
  have "coeff p (degree p) = 0" by simp
huffman@29451
  1293
  then have "p = 0" by simp
huffman@29451
  1294
  then show ?thesis ..
huffman@29451
  1295
next
huffman@29451
  1296
  case (Suc m)
huffman@29451
  1297
  have "\<forall>i>n. coeff p i = 0"
huffman@29451
  1298
    using `degree p \<le> n` by (simp add: coeff_eq_0)
huffman@29451
  1299
  then have "\<forall>i\<ge>n. coeff p i = 0"
huffman@29451
  1300
    using `coeff p n = 0` by (simp add: le_less)
huffman@29451
  1301
  then have "\<forall>i>m. coeff p i = 0"
huffman@29451
  1302
    using `n = Suc m` by (simp add: less_eq_Suc_le)
huffman@29451
  1303
  then have "degree p \<le> m"
huffman@29451
  1304
    by (rule degree_le)
huffman@29451
  1305
  then have "degree p < n"
huffman@29451
  1306
    using `n = Suc m` by (simp add: less_Suc_eq_le)
huffman@29451
  1307
  then show ?thesis ..
huffman@29451
  1308
qed
huffman@29451
  1309
huffman@29537
  1310
lemma pdivmod_rel_pCons:
huffman@29537
  1311
  assumes rel: "pdivmod_rel x y q r"
huffman@29451
  1312
  assumes y: "y \<noteq> 0"
huffman@29451
  1313
  assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
huffman@29537
  1314
  shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
huffman@29537
  1315
    (is "pdivmod_rel ?x y ?q ?r")
huffman@29451
  1316
proof -
huffman@29451
  1317
  have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
huffman@29537
  1318
    using assms unfolding pdivmod_rel_def by simp_all
huffman@29451
  1319
huffman@29451
  1320
  have 1: "?x = ?q * y + ?r"
huffman@29451
  1321
    using b x by simp
huffman@29451
  1322
huffman@29451
  1323
  have 2: "?r = 0 \<or> degree ?r < degree y"
huffman@29451
  1324
  proof (rule eq_zero_or_degree_less)
huffman@29539
  1325
    show "degree ?r \<le> degree y"
huffman@29539
  1326
    proof (rule degree_diff_le)
huffman@29451
  1327
      show "degree (pCons a r) \<le> degree y"
huffman@29460
  1328
        using r by auto
huffman@29451
  1329
      show "degree (smult b y) \<le> degree y"
huffman@29451
  1330
        by (rule degree_smult_le)
huffman@29451
  1331
    qed
huffman@29451
  1332
  next
huffman@29451
  1333
    show "coeff ?r (degree y) = 0"
huffman@29451
  1334
      using `y \<noteq> 0` unfolding b by simp
huffman@29451
  1335
  qed
huffman@29451
  1336
huffman@29451
  1337
  from 1 2 show ?thesis
huffman@29537
  1338
    unfolding pdivmod_rel_def
huffman@29451
  1339
    using `y \<noteq> 0` by simp
huffman@29451
  1340
qed
huffman@29451
  1341
huffman@29537
  1342
lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r"
huffman@29451
  1343
apply (cases "y = 0")
huffman@29537
  1344
apply (fast intro!: pdivmod_rel_by_0)
huffman@29451
  1345
apply (induct x)
huffman@29537
  1346
apply (fast intro!: pdivmod_rel_0)
huffman@29537
  1347
apply (fast intro!: pdivmod_rel_pCons)
huffman@29451
  1348
done
huffman@29451
  1349
huffman@29537
  1350
lemma pdivmod_rel_unique:
huffman@29537
  1351
  assumes 1: "pdivmod_rel x y q1 r1"
huffman@29537
  1352
  assumes 2: "pdivmod_rel x y q2 r2"
huffman@29451
  1353
  shows "q1 = q2 \<and> r1 = r2"
huffman@29451
  1354
proof (cases "y = 0")
huffman@29451
  1355
  assume "y = 0" with assms show ?thesis
huffman@29537
  1356
    by (simp add: pdivmod_rel_def)
huffman@29451
  1357
next
huffman@29451
  1358
  assume [simp]: "y \<noteq> 0"
huffman@29451
  1359
  from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
huffman@29537
  1360
    unfolding pdivmod_rel_def by simp_all
huffman@29451
  1361
  from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
huffman@29537
  1362
    unfolding pdivmod_rel_def by simp_all
huffman@29451
  1363
  from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
nipkow@29667
  1364
    by (simp add: algebra_simps)
huffman@29451
  1365
  from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
huffman@29453
  1366
    by (auto intro: degree_diff_less)
huffman@29451
  1367
huffman@29451
  1368
  show "q1 = q2 \<and> r1 = r2"
huffman@29451
  1369
  proof (rule ccontr)
huffman@29451
  1370
    assume "\<not> (q1 = q2 \<and> r1 = r2)"
huffman@29451
  1371
    with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
huffman@29451
  1372
    with r3 have "degree (r2 - r1) < degree y" by simp
huffman@29451
  1373
    also have "degree y \<le> degree (q1 - q2) + degree y" by simp
huffman@29451
  1374
    also have "\<dots> = degree ((q1 - q2) * y)"
huffman@29451
  1375
      using `q1 \<noteq> q2` by (simp add: degree_mult_eq)
huffman@29451
  1376
    also have "\<dots> = degree (r2 - r1)"
huffman@29451
  1377
      using q3 by simp
huffman@29451
  1378
    finally have "degree (r2 - r1) < degree (r2 - r1)" .
huffman@29451
  1379
    then show "False" by simp
huffman@29451
  1380
  qed
huffman@29451
  1381
qed
huffman@29451
  1382
huffman@29660
  1383
lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0"
huffman@29660
  1384
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0)
huffman@29660
  1385
huffman@29660
  1386
lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x"
huffman@29660
  1387
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0)
huffman@29660
  1388
wenzelm@45605
  1389
lemmas pdivmod_rel_unique_div = pdivmod_rel_unique [THEN conjunct1]
huffman@29451
  1390
wenzelm@45605
  1391
lemmas pdivmod_rel_unique_mod = pdivmod_rel_unique [THEN conjunct2]
huffman@29451
  1392
huffman@29451
  1393
instantiation poly :: (field) ring_div
huffman@29451
  1394
begin
huffman@29451
  1395
huffman@29451
  1396
definition div_poly where
haftmann@37765
  1397
  "x div y = (THE q. \<exists>r. pdivmod_rel x y q r)"
huffman@29451
  1398
huffman@29451
  1399
definition mod_poly where
haftmann@37765
  1400
  "x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)"
huffman@29451
  1401
huffman@29451
  1402
lemma div_poly_eq:
huffman@29537
  1403
  "pdivmod_rel x y q r \<Longrightarrow> x div y = q"
huffman@29451
  1404
unfolding div_poly_def
huffman@29537
  1405
by (fast elim: pdivmod_rel_unique_div)
huffman@29451
  1406
huffman@29451
  1407
lemma mod_poly_eq:
huffman@29537
  1408
  "pdivmod_rel x y q r \<Longrightarrow> x mod y = r"
huffman@29451
  1409
unfolding mod_poly_def
huffman@29537
  1410
by (fast elim: pdivmod_rel_unique_mod)
huffman@29451
  1411
huffman@29537
  1412
lemma pdivmod_rel:
huffman@29537
  1413
  "pdivmod_rel x y (x div y) (x mod y)"
huffman@29451
  1414
proof -
huffman@29537
  1415
  from pdivmod_rel_exists
huffman@29537
  1416
    obtain q r where "pdivmod_rel x y q r" by fast
huffman@29451
  1417
  thus ?thesis
huffman@29451
  1418
    by (simp add: div_poly_eq mod_poly_eq)
huffman@29451
  1419
qed
huffman@29451
  1420
huffman@29451
  1421
instance proof
huffman@29451
  1422
  fix x y :: "'a poly"
huffman@29451
  1423
  show "x div y * y + x mod y = x"
huffman@29537
  1424
    using pdivmod_rel [of x y]
huffman@29537
  1425
    by (simp add: pdivmod_rel_def)
huffman@29451
  1426
next
huffman@29451
  1427
  fix x :: "'a poly"
huffman@29537
  1428
  have "pdivmod_rel x 0 0 x"
huffman@29537
  1429
    by (rule pdivmod_rel_by_0)
huffman@29451
  1430
  thus "x div 0 = 0"
huffman@29451
  1431
    by (rule div_poly_eq)
huffman@29451
  1432
next
huffman@29451
  1433
  fix y :: "'a poly"
huffman@29537
  1434
  have "pdivmod_rel 0 y 0 0"
huffman@29537
  1435
    by (rule pdivmod_rel_0)
huffman@29451
  1436
  thus "0 div y = 0"
huffman@29451
  1437
    by (rule div_poly_eq)
huffman@29451
  1438
next
huffman@29451
  1439
  fix x y z :: "'a poly"
huffman@29451
  1440
  assume "y \<noteq> 0"
huffman@29537
  1441
  hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)"
huffman@29537
  1442
    using pdivmod_rel [of x y]
webertj@49962
  1443
    by (simp add: pdivmod_rel_def distrib_right)
huffman@29451
  1444
  thus "(x + z * y) div y = z + x div y"
huffman@29451
  1445
    by (rule div_poly_eq)
haftmann@30930
  1446
next
haftmann@30930
  1447
  fix x y z :: "'a poly"
haftmann@30930
  1448
  assume "x \<noteq> 0"
haftmann@30930
  1449
  show "(x * y) div (x * z) = y div z"
haftmann@30930
  1450
  proof (cases "y \<noteq> 0 \<and> z \<noteq> 0")
haftmann@30930
  1451
    have "\<And>x::'a poly. pdivmod_rel x 0 0 x"
haftmann@30930
  1452
      by (rule pdivmod_rel_by_0)
haftmann@30930
  1453
    then have [simp]: "\<And>x::'a poly. x div 0 = 0"
haftmann@30930
  1454
      by (rule div_poly_eq)
haftmann@30930
  1455
    have "\<And>x::'a poly. pdivmod_rel 0 x 0 0"
haftmann@30930
  1456
      by (rule pdivmod_rel_0)
haftmann@30930
  1457
    then have [simp]: "\<And>x::'a poly. 0 div x = 0"
haftmann@30930
  1458
      by (rule div_poly_eq)
haftmann@30930
  1459
    case False then show ?thesis by auto
haftmann@30930
  1460
  next
haftmann@30930
  1461
    case True then have "y \<noteq> 0" and "z \<noteq> 0" by auto
haftmann@30930
  1462
    with `x \<noteq> 0`
haftmann@30930
  1463
    have "\<And>q r. pdivmod_rel y z q r \<Longrightarrow> pdivmod_rel (x * y) (x * z) q (x * r)"
haftmann@30930
  1464
      by (auto simp add: pdivmod_rel_def algebra_simps)
haftmann@30930
  1465
        (rule classical, simp add: degree_mult_eq)
haftmann@30930
  1466
    moreover from pdivmod_rel have "pdivmod_rel y z (y div z) (y mod z)" .
haftmann@30930
  1467
    ultimately have "pdivmod_rel (x * y) (x * z) (y div z) (x * (y mod z))" .
haftmann@30930
  1468
    then show ?thesis by (simp add: div_poly_eq)
haftmann@30930
  1469
  qed
huffman@29451
  1470
qed
huffman@29451
  1471
huffman@29451
  1472
end
huffman@29451
  1473
huffman@29451
  1474
lemma degree_mod_less:
huffman@29451
  1475
  "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
huffman@29537
  1476
  using pdivmod_rel [of x y]
huffman@29537
  1477
  unfolding pdivmod_rel_def by simp
huffman@29451
  1478
huffman@29451
  1479
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
huffman@29451
  1480
proof -
huffman@29451
  1481
  assume "degree x < degree y"
huffman@29537
  1482
  hence "pdivmod_rel x y 0 x"
huffman@29537
  1483
    by (simp add: pdivmod_rel_def)
huffman@29451
  1484
  thus "x div y = 0" by (rule div_poly_eq)
huffman@29451
  1485
qed
huffman@29451
  1486
huffman@29451
  1487
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
huffman@29451
  1488
proof -
huffman@29451
  1489
  assume "degree x < degree y"
huffman@29537
  1490
  hence "pdivmod_rel x y 0 x"
huffman@29537
  1491
    by (simp add: pdivmod_rel_def)
huffman@29451
  1492
  thus "x mod y = x" by (rule mod_poly_eq)
huffman@29451
  1493
qed
huffman@29451
  1494
huffman@29659
  1495
lemma pdivmod_rel_smult_left:
huffman@29659
  1496
  "pdivmod_rel x y q r
huffman@29659
  1497
    \<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)"
huffman@29659
  1498
  unfolding pdivmod_rel_def by (simp add: smult_add_right)
huffman@29659
  1499
huffman@29659
  1500
lemma div_smult_left: "(smult a x) div y = smult a (x div y)"
huffman@29659
  1501
  by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
huffman@29659
  1502
huffman@29659
  1503
lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"
huffman@29659
  1504
  by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
huffman@29659
  1505
huffman@30072
  1506
lemma poly_div_minus_left [simp]:
huffman@30072
  1507
  fixes x y :: "'a::field poly"
huffman@30072
  1508
  shows "(- x) div y = - (x div y)"
haftmann@54489
  1509
  using div_smult_left [of "- 1::'a"] by simp
huffman@30072
  1510
huffman@30072
  1511
lemma poly_mod_minus_left [simp]:
huffman@30072
  1512
  fixes x y :: "'a::field poly"
huffman@30072
  1513
  shows "(- x) mod y = - (x mod y)"
haftmann@54489
  1514
  using mod_smult_left [of "- 1::'a"] by simp
huffman@30072
  1515
huffman@57482
  1516
lemma pdivmod_rel_add_left:
huffman@57482
  1517
  assumes "pdivmod_rel x y q r"
huffman@57482
  1518
  assumes "pdivmod_rel x' y q' r'"
huffman@57482
  1519
  shows "pdivmod_rel (x + x') y (q + q') (r + r')"
huffman@57482
  1520
  using assms unfolding pdivmod_rel_def
huffman@57482
  1521
  by (auto simp add: distrib degree_add_less)
huffman@57482
  1522
huffman@57482
  1523
lemma poly_div_add_left:
huffman@57482
  1524
  fixes x y z :: "'a::field poly"
huffman@57482
  1525
  shows "(x + y) div z = x div z + y div z"
huffman@57482
  1526
  using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel]
huffman@57482
  1527
  by (rule div_poly_eq)
huffman@57482
  1528
huffman@57482
  1529
lemma poly_mod_add_left:
huffman@57482
  1530
  fixes x y z :: "'a::field poly"
huffman@57482
  1531
  shows "(x + y) mod z = x mod z + y mod z"
huffman@57482
  1532
  using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel]
huffman@57482
  1533
  by (rule mod_poly_eq)
huffman@57482
  1534
huffman@57482
  1535
lemma poly_div_diff_left:
huffman@57482
  1536
  fixes x y z :: "'a::field poly"
huffman@57482
  1537
  shows "(x - y) div z = x div z - y div z"
huffman@57482
  1538
  by (simp only: diff_conv_add_uminus poly_div_add_left poly_div_minus_left)
huffman@57482
  1539
huffman@57482
  1540
lemma poly_mod_diff_left:
huffman@57482
  1541
  fixes x y z :: "'a::field poly"
huffman@57482
  1542
  shows "(x - y) mod z = x mod z - y mod z"
huffman@57482
  1543
  by (simp only: diff_conv_add_uminus poly_mod_add_left poly_mod_minus_left)
huffman@57482
  1544
huffman@29659
  1545
lemma pdivmod_rel_smult_right:
huffman@29659
  1546
  "\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk>
huffman@29659
  1547
    \<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r"
huffman@29659
  1548
  unfolding pdivmod_rel_def by simp
huffman@29659
  1549
huffman@29659
  1550
lemma div_smult_right:
huffman@29659
  1551
  "a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)"
huffman@29659
  1552
  by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
huffman@29659
  1553
huffman@29659
  1554
lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y"
huffman@29659
  1555
  by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
huffman@29659
  1556
huffman@30072
  1557
lemma poly_div_minus_right [simp]:
huffman@30072
  1558
  fixes x y :: "'a::field poly"
huffman@30072
  1559
  shows "x div (- y) = - (x div y)"
haftmann@54489
  1560
  using div_smult_right [of "- 1::'a"] by (simp add: nonzero_inverse_minus_eq)
huffman@30072
  1561
huffman@30072
  1562
lemma poly_mod_minus_right [simp]:
huffman@30072
  1563
  fixes x y :: "'a::field poly"
huffman@30072
  1564
  shows "x mod (- y) = x mod y"
haftmann@54489
  1565
  using mod_smult_right [of "- 1::'a"] by simp
huffman@30072
  1566
huffman@29660
  1567
lemma pdivmod_rel_mult:
huffman@29660
  1568
  "\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk>
huffman@29660
  1569
    \<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)"
huffman@29660
  1570
apply (cases "z = 0", simp add: pdivmod_rel_def)
huffman@29660
  1571
apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff)
huffman@29660
  1572
apply (cases "r = 0")
huffman@29660
  1573
apply (cases "r' = 0")
huffman@29660
  1574
apply (simp add: pdivmod_rel_def)
haftmann@36350
  1575
apply (simp add: pdivmod_rel_def field_simps degree_mult_eq)
huffman@29660
  1576
apply (cases "r' = 0")
huffman@29660
  1577
apply (simp add: pdivmod_rel_def degree_mult_eq)
haftmann@36350
  1578
apply (simp add: pdivmod_rel_def field_simps)
huffman@29660
  1579
apply (simp add: degree_mult_eq degree_add_less)
huffman@29660
  1580
done
huffman@29660
  1581
huffman@29660
  1582
lemma poly_div_mult_right:
huffman@29660
  1583
  fixes x y z :: "'a::field poly"
huffman@29660
  1584
  shows "x div (y * z) = (x div y) div z"
huffman@29660
  1585
  by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
huffman@29660
  1586
huffman@29660
  1587
lemma poly_mod_mult_right:
huffman@29660
  1588
  fixes x y z :: "'a::field poly"
huffman@29660
  1589
  shows "x mod (y * z) = y * (x div y mod z) + x mod y"
huffman@29660
  1590
  by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
huffman@29660
  1591
huffman@29451
  1592
lemma mod_pCons:
huffman@29451
  1593
  fixes a and x
huffman@29451
  1594
  assumes y: "y \<noteq> 0"
huffman@29451
  1595
  defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"
huffman@29451
  1596
  shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
huffman@29451
  1597
unfolding b
huffman@29451
  1598
apply (rule mod_poly_eq)
huffman@29537
  1599
apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl])
huffman@29451
  1600
done
huffman@29451
  1601
haftmann@52380
  1602
definition pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly"
haftmann@52380
  1603
where
haftmann@52380
  1604
  "pdivmod p q = (p div q, p mod q)"
huffman@31663
  1605
haftmann@52380
  1606
lemma div_poly_code [code]: 
haftmann@52380
  1607
  "p div q = fst (pdivmod p q)"
haftmann@52380
  1608
  by (simp add: pdivmod_def)
huffman@31663
  1609
haftmann@52380
  1610
lemma mod_poly_code [code]:
haftmann@52380
  1611
  "p mod q = snd (pdivmod p q)"
haftmann@52380
  1612
  by (simp add: pdivmod_def)
huffman@31663
  1613
haftmann@52380
  1614
lemma pdivmod_0:
haftmann@52380
  1615
  "pdivmod 0 q = (0, 0)"
haftmann@52380
  1616
  by (simp add: pdivmod_def)
huffman@31663
  1617
haftmann@52380
  1618
lemma pdivmod_pCons:
haftmann@52380
  1619
  "pdivmod (pCons a p) q =
haftmann@52380
  1620
    (if q = 0 then (0, pCons a p) else
haftmann@52380
  1621
      (let (s, r) = pdivmod p q;
haftmann@52380
  1622
           b = coeff (pCons a r) (degree q) / coeff q (degree q)
haftmann@52380
  1623
        in (pCons b s, pCons a r - smult b q)))"
haftmann@52380
  1624
  apply (simp add: pdivmod_def Let_def, safe)
haftmann@52380
  1625
  apply (rule div_poly_eq)
haftmann@52380
  1626
  apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
haftmann@52380
  1627
  apply (rule mod_poly_eq)
haftmann@52380
  1628
  apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
huffman@29451
  1629
  done
huffman@29451
  1630
haftmann@52380
  1631
lemma pdivmod_fold_coeffs [code]:
haftmann@52380
  1632
  "pdivmod p q = (if q = 0 then (0, p)
haftmann@52380
  1633
    else fold_coeffs (\<lambda>a (s, r).
haftmann@52380
  1634
      let b = coeff (pCons a r) (degree q) / coeff q (degree q)
haftmann@52380
  1635
      in (pCons b s, pCons a r - smult b q)
haftmann@52380
  1636
   ) p (0, 0))"
haftmann@52380
  1637
  apply (cases "q = 0")
haftmann@52380
  1638
  apply (simp add: pdivmod_def)
haftmann@52380
  1639
  apply (rule sym)
haftmann@52380
  1640
  apply (induct p)
haftmann@52380
  1641
  apply (simp_all add: pdivmod_0 pdivmod_pCons)
haftmann@52380
  1642
  apply (case_tac "a = 0 \<and> p = 0")
haftmann@52380
  1643
  apply (auto simp add: pdivmod_def)
haftmann@52380
  1644
  done
huffman@29980
  1645
huffman@29980
  1646
huffman@29977
  1647
subsection {* Order of polynomial roots *}
huffman@29977
  1648
haftmann@52380
  1649
definition order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat"
huffman@29977
  1650
where
huffman@29977
  1651
  "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
huffman@29977
  1652
huffman@29977
  1653
lemma coeff_linear_power:
huffman@29979
  1654
  fixes a :: "'a::comm_semiring_1"
huffman@29977
  1655
  shows "coeff ([:a, 1:] ^ n) n = 1"
huffman@29977
  1656
apply (induct n, simp_all)
huffman@29977
  1657
apply (subst coeff_eq_0)
huffman@29977
  1658
apply (auto intro: le_less_trans degree_power_le)
huffman@29977
  1659
done
huffman@29977
  1660
huffman@29977
  1661
lemma degree_linear_power:
huffman@29979
  1662
  fixes a :: "'a::comm_semiring_1"
huffman@29977
  1663
  shows "degree ([:a, 1:] ^ n) = n"
huffman@29977
  1664
apply (rule order_antisym)
huffman@29977
  1665
apply (rule ord_le_eq_trans [OF degree_power_le], simp)
huffman@29977
  1666
apply (rule le_degree, simp add: coeff_linear_power)
huffman@29977
  1667
done
huffman@29977
  1668
huffman@29977
  1669
lemma order_1: "[:-a, 1:] ^ order a p dvd p"
huffman@29977
  1670
apply (cases "p = 0", simp)
huffman@29977
  1671
apply (cases "order a p", simp)
huffman@29977
  1672
apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
huffman@29977
  1673
apply (drule not_less_Least, simp)
huffman@29977
  1674
apply (fold order_def, simp)
huffman@29977
  1675
done
huffman@29977
  1676
huffman@29977
  1677
lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
huffman@29977
  1678
unfolding order_def
huffman@29977
  1679
apply (rule LeastI_ex)
huffman@29977
  1680
apply (rule_tac x="degree p" in exI)
huffman@29977
  1681
apply (rule notI)
huffman@29977
  1682
apply (drule (1) dvd_imp_degree_le)
huffman@29977
  1683
apply (simp only: degree_linear_power)
huffman@29977
  1684
done
huffman@29977
  1685
huffman@29977
  1686
lemma order:
huffman@29977
  1687
  "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
huffman@29977
  1688
by (rule conjI [OF order_1 order_2])
huffman@29977
  1689
huffman@29977
  1690
lemma order_degree:
huffman@29977
  1691
  assumes p: "p \<noteq> 0"
huffman@29977
  1692
  shows "order a p \<le> degree p"
huffman@29977
  1693
proof -
huffman@29977
  1694
  have "order a p = degree ([:-a, 1:] ^ order a p)"
huffman@29977
  1695
    by (simp only: degree_linear_power)
huffman@29977
  1696
  also have "\<dots> \<le> degree p"
huffman@29977
  1697
    using order_1 p by (rule dvd_imp_degree_le)
huffman@29977
  1698
  finally show ?thesis .
huffman@29977
  1699
qed
huffman@29977
  1700
huffman@29977
  1701
lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
huffman@29977
  1702
apply (cases "p = 0", simp_all)
huffman@29977
  1703
apply (rule iffI)
lp15@56383
  1704
apply (metis order_2 not_gr0 poly_eq_0_iff_dvd power_0 power_Suc_0 power_one_right)
lp15@56383
  1705
unfolding poly_eq_0_iff_dvd
lp15@56383
  1706
apply (metis dvd_power dvd_trans order_1)
huffman@29977
  1707
done
huffman@29977
  1708
huffman@29977
  1709
haftmann@52380
  1710
subsection {* GCD of polynomials *}
huffman@29478
  1711
haftmann@52380
  1712
instantiation poly :: (field) gcd
huffman@29478
  1713
begin
huffman@29478
  1714
haftmann@52380
  1715
function gcd_poly :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
haftmann@52380
  1716
where
haftmann@52380
  1717
  "gcd (x::'a poly) 0 = smult (inverse (coeff x (degree x))) x"
haftmann@52380
  1718
| "y \<noteq> 0 \<Longrightarrow> gcd (x::'a poly) y = gcd y (x mod y)"
haftmann@52380
  1719
by auto
huffman@29478
  1720
haftmann@52380
  1721
termination "gcd :: _ poly \<Rightarrow> _"
haftmann@52380
  1722
by (relation "measure (\<lambda>(x, y). if y = 0 then 0 else Suc (degree y))")
haftmann@52380
  1723
   (auto dest: degree_mod_less)
haftmann@52380
  1724
haftmann@52380
  1725
declare gcd_poly.simps [simp del]
haftmann@52380
  1726
haftmann@52380
  1727
instance ..
huffman@29478
  1728
huffman@29451
  1729
end
huffman@29478
  1730
haftmann@52380
  1731
lemma
haftmann@52380
  1732
  fixes x y :: "_ poly"
haftmann@52380
  1733
  shows poly_gcd_dvd1 [iff]: "gcd x y dvd x"
haftmann@52380
  1734
    and poly_gcd_dvd2 [iff]: "gcd x y dvd y"
haftmann@52380
  1735
  apply (induct x y rule: gcd_poly.induct)
haftmann@52380
  1736
  apply (simp_all add: gcd_poly.simps)
haftmann@52380
  1737
  apply (fastforce simp add: smult_dvd_iff dest: inverse_zero_imp_zero)
haftmann@52380
  1738
  apply (blast dest: dvd_mod_imp_dvd)
haftmann@52380
  1739
  done
haftmann@38857
  1740
haftmann@52380
  1741
lemma poly_gcd_greatest:
haftmann@52380
  1742
  fixes k x y :: "_ poly"
haftmann@52380
  1743
  shows "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd gcd x y"
haftmann@52380
  1744
  by (induct x y rule: gcd_poly.induct)
haftmann@52380
  1745
     (simp_all add: gcd_poly.simps dvd_mod dvd_smult)
huffman@29478
  1746
haftmann@52380
  1747
lemma dvd_poly_gcd_iff [iff]:
haftmann@52380
  1748
  fixes k x y :: "_ poly"
haftmann@52380
  1749
  shows "k dvd gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"
haftmann@52380
  1750
  by (blast intro!: poly_gcd_greatest intro: dvd_trans)
huffman@29478
  1751
haftmann@52380
  1752
lemma poly_gcd_monic:
haftmann@52380
  1753
  fixes x y :: "_ poly"
haftmann@52380
  1754
  shows "coeff (gcd x y) (degree (gcd x y)) =
haftmann@52380
  1755
    (if x = 0 \<and> y = 0 then 0 else 1)"
haftmann@52380
  1756
  by (induct x y rule: gcd_poly.induct)
haftmann@52380
  1757
     (simp_all add: gcd_poly.simps nonzero_imp_inverse_nonzero)
huffman@29478
  1758
haftmann@52380
  1759
lemma poly_gcd_zero_iff [simp]:
haftmann@52380
  1760
  fixes x y :: "_ poly"
haftmann@52380
  1761
  shows "gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
haftmann@52380
  1762
  by (simp only: dvd_0_left_iff [symmetric] dvd_poly_gcd_iff)
huffman@29478
  1763
haftmann@52380
  1764
lemma poly_gcd_0_0 [simp]:
haftmann@52380
  1765
  "gcd (0::_ poly) 0 = 0"
haftmann@52380
  1766
  by simp
huffman@29478
  1767
haftmann@52380
  1768
lemma poly_dvd_antisym:
haftmann@52380
  1769
  fixes p q :: "'a::idom poly"
haftmann@52380
  1770
  assumes coeff: "coeff p (degree p) = coeff q (degree q)"
haftmann@52380
  1771
  assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q"
haftmann@52380
  1772
proof (cases "p = 0")
haftmann@52380
  1773
  case True with coeff show "p = q" by simp
haftmann@52380
  1774
next
haftmann@52380
  1775
  case False with coeff have "q \<noteq> 0" by auto
haftmann@52380
  1776
  have degree: "degree p = degree q"
haftmann@52380
  1777
    using `p dvd q` `q dvd p` `p \<noteq> 0` `q \<noteq> 0`
haftmann@52380
  1778
    by (intro order_antisym dvd_imp_degree_le)
huffman@29478
  1779
haftmann@52380
  1780
  from `p dvd q` obtain a where a: "q = p * a" ..
haftmann@52380
  1781
  with `q \<noteq> 0` have "a \<noteq> 0" by auto
haftmann@52380
  1782
  with degree a `p \<noteq> 0` have "degree a = 0"
haftmann@52380
  1783
    by (simp add: degree_mult_eq)
haftmann@52380
  1784
  with coeff a show "p = q"
haftmann@52380
  1785
    by (cases a, auto split: if_splits)
haftmann@52380
  1786
qed
huffman@29478
  1787
haftmann@52380
  1788
lemma poly_gcd_unique:
haftmann@52380
  1789
  fixes d x y :: "_ poly"
haftmann@52380
  1790
  assumes dvd1: "d dvd x" and dvd2: "d dvd y"
haftmann@52380
  1791
    and greatest: "\<And>k. k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd d"
haftmann@52380
  1792
    and monic: "coeff d (degree d) = (if x = 0 \<and> y = 0 then 0 else 1)"
haftmann@52380
  1793
  shows "gcd x y = d"
haftmann@52380
  1794
proof -
haftmann@52380
  1795
  have "coeff (gcd x y) (degree (gcd x y)) = coeff d (degree d)"
haftmann@52380
  1796
    by (simp_all add: poly_gcd_monic monic)
haftmann@52380
  1797
  moreover have "gcd x y dvd d"
haftmann@52380
  1798
    using poly_gcd_dvd1 poly_gcd_dvd2 by (rule greatest)
haftmann@52380
  1799
  moreover have "d dvd gcd x y"
haftmann@52380
  1800
    using dvd1 dvd2 by (rule poly_gcd_greatest)
haftmann@52380
  1801
  ultimately show ?thesis
haftmann@52380
  1802
    by (rule poly_dvd_antisym)
haftmann@52380
  1803
qed
huffman@29478
  1804
haftmann@52380
  1805
interpretation gcd_poly!: abel_semigroup "gcd :: _ poly \<Rightarrow> _"
haftmann@52380
  1806
proof
haftmann@52380
  1807
  fix x y z :: "'a poly"
haftmann@52380
  1808
  show "gcd (gcd x y) z = gcd x (gcd y z)"
haftmann@52380
  1809
    by (rule poly_gcd_unique) (auto intro: dvd_trans simp add: poly_gcd_monic)
haftmann@52380
  1810
  show "gcd x y = gcd y x"
haftmann@52380
  1811
    by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
haftmann@52380
  1812
qed
huffman@29478
  1813
haftmann@52380
  1814
lemmas poly_gcd_assoc = gcd_poly.assoc
haftmann@52380
  1815
lemmas poly_gcd_commute = gcd_poly.commute
haftmann@52380
  1816
lemmas poly_gcd_left_commute = gcd_poly.left_commute
huffman@29478
  1817
haftmann@52380
  1818
lemmas poly_gcd_ac = poly_gcd_assoc poly_gcd_commute poly_gcd_left_commute
haftmann@52380
  1819
haftmann@52380
  1820
lemma poly_gcd_1_left [simp]: "gcd 1 y = (1 :: _ poly)"
haftmann@52380
  1821
by (rule poly_gcd_unique) simp_all
huffman@29478
  1822
haftmann@52380
  1823
lemma poly_gcd_1_right [simp]: "gcd x 1 = (1 :: _ poly)"
haftmann@52380
  1824
by (rule poly_gcd_unique) simp_all
haftmann@52380
  1825
haftmann@52380
  1826
lemma poly_gcd_minus_left [simp]: "gcd (- x) y = gcd x (y :: _ poly)"
haftmann@52380
  1827
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
huffman@29478
  1828
haftmann@52380
  1829
lemma poly_gcd_minus_right [simp]: "gcd x (- y) = gcd x (y :: _ poly)"
haftmann@52380
  1830
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
huffman@29478
  1831
haftmann@52380
  1832
lemma poly_gcd_code [code]:
haftmann@52380
  1833
  "gcd x y = (if y = 0 then smult (inverse (coeff x (degree x))) x else gcd y (x mod (y :: _ poly)))"
haftmann@52380
  1834
  by (simp add: gcd_poly.simps)
haftmann@52380
  1835
haftmann@52380
  1836
haftmann@52380
  1837
subsection {* Composition of polynomials *}
huffman@29478
  1838
haftmann@52380
  1839
definition pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
haftmann@52380
  1840
where
haftmann@52380
  1841
  "pcompose p q = fold_coeffs (\<lambda>a c. [:a:] + q * c) p 0"
haftmann@52380
  1842
haftmann@52380
  1843
lemma pcompose_0 [simp]:
haftmann@52380
  1844
  "pcompose 0 q = 0"
haftmann@52380
  1845
  by (simp add: pcompose_def)
haftmann@52380
  1846
haftmann@52380
  1847
lemma pcompose_pCons:
haftmann@52380
  1848
  "pcompose (pCons a p) q = [:a:] + q * pcompose p q"
haftmann@52380
  1849
  by (cases "p = 0 \<and> a = 0") (auto simp add: pcompose_def)
haftmann@52380
  1850
haftmann@52380
  1851
lemma poly_pcompose:
haftmann@52380
  1852
  "poly (pcompose p q) x = poly p (poly q x)"
haftmann@52380
  1853
  by (induct p) (simp_all add: pcompose_pCons)
haftmann@52380
  1854
haftmann@52380
  1855
lemma degree_pcompose_le:
haftmann@52380
  1856
  "degree (pcompose p q) \<le> degree p * degree q"
haftmann@52380
  1857
apply (induct p, simp)
haftmann@52380
  1858
apply (simp add: pcompose_pCons, clarify)
haftmann@52380
  1859
apply (rule degree_add_le, simp)
haftmann@52380
  1860
apply (rule order_trans [OF degree_mult_le], simp)
huffman@29478
  1861
done
huffman@29478
  1862
haftmann@52380
  1863
haftmann@52380
  1864
no_notation cCons (infixr "##" 65)
huffman@31663
  1865
huffman@29478
  1866
end
haftmann@52380
  1867