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