src/HOL/Library/Polynomial.thy
author haftmann
Sun, 18 Mar 2012 08:57:45 +0100
changeset 47002 9435d419109a
parent 46031 ac6bae9fdc2f
child 47108 2a1953f0d20d
permissions -rw-r--r--
comments for uniformity
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 39302
diff changeset
     1
(*  Title:      HOL/Library/Polynomial.thy
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     2
    Author:     Brian Huffman
41959
b460124855b8 tuned headers;
wenzelm
parents: 39302
diff changeset
     3
    Author:     Clemens Ballarin
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     4
*)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     5
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     6
header {* Univariate Polynomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     7
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     8
theory Polynomial
30738
0842e906300c normalized imports
haftmann
parents: 30273
diff changeset
     9
imports Main
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    10
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    11
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    12
subsection {* Definition of type @{text poly} *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    13
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45605
diff changeset
    14
definition "Poly = {f::nat \<Rightarrow> 'a::zero. \<exists>n. \<forall>i>n. f i = 0}"
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45605
diff changeset
    15
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45605
diff changeset
    16
typedef (open) 'a poly = "Poly :: (nat => 'a::zero) set"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    17
  morphisms coeff Abs_poly
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45605
diff changeset
    18
  unfolding Poly_def by auto
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    19
47002
9435d419109a comments for uniformity
haftmann
parents: 46031
diff changeset
    20
(* FIXME should be named poly_eq_iff *)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    21
lemma expand_poly_eq: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)"
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45605
diff changeset
    22
  by (simp add: coeff_inject [symmetric] fun_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    23
47002
9435d419109a comments for uniformity
haftmann
parents: 46031
diff changeset
    24
(* FIXME should be named poly_eqI *)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    25
lemma poly_ext: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45605
diff changeset
    26
  by (simp add: expand_poly_eq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    27
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    28
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    29
subsection {* Degree of a polynomial *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    30
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    31
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    32
  degree :: "'a::zero poly \<Rightarrow> nat" where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    33
  "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    34
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    35
lemma coeff_eq_0: "degree p < n \<Longrightarrow> coeff p n = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    36
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    37
  have "coeff p \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    38
    by (rule coeff)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    39
  hence "\<exists>n. \<forall>i>n. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    40
    unfolding Poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    41
  hence "\<forall>i>degree p. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    42
    unfolding degree_def by (rule LeastI_ex)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    43
  moreover assume "degree p < n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    44
  ultimately show ?thesis by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    45
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    46
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    47
lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    48
  by (erule contrapos_np, rule coeff_eq_0, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    49
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    50
lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    51
  unfolding degree_def by (erule Least_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    52
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    53
lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    54
  unfolding degree_def by (drule not_less_Least, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    55
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    56
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    57
subsection {* The zero polynomial *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    58
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    59
instantiation poly :: (zero) zero
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    60
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    61
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    62
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    63
  zero_poly_def: "0 = Abs_poly (\<lambda>n. 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    64
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    65
instance ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    66
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    67
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    68
lemma coeff_0 [simp]: "coeff 0 n = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    69
  unfolding zero_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    70
  by (simp add: Abs_poly_inverse Poly_def)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    71
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    72
lemma degree_0 [simp]: "degree 0 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    73
  by (rule order_antisym [OF degree_le le0]) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    74
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    75
lemma leading_coeff_neq_0:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    76
  assumes "p \<noteq> 0" shows "coeff p (degree p) \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    77
proof (cases "degree p")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    78
  case 0
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    79
  from `p \<noteq> 0` have "\<exists>n. coeff p n \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    80
    by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    81
  then obtain n where "coeff p n \<noteq> 0" ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    82
  hence "n \<le> degree p" by (rule le_degree)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    83
  with `coeff p n \<noteq> 0` and `degree p = 0`
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    84
  show "coeff p (degree p) \<noteq> 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    85
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    86
  case (Suc n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    87
  from `degree p = Suc n` have "n < degree p" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    88
  hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    89
  then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    90
  from `degree p = Suc n` and `n < i` have "degree p \<le> i" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    91
  also from `coeff p i \<noteq> 0` have "i \<le> degree p" by (rule le_degree)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    92
  finally have "degree p = i" .
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    93
  with `coeff p i \<noteq> 0` show "coeff p (degree p) \<noteq> 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    94
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    95
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    96
lemma leading_coeff_0_iff [simp]: "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    97
  by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    98
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    99
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   100
subsection {* List-style constructor for polynomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   101
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   102
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   103
  pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   104
where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
   105
  "pCons a p = Abs_poly (nat_case a (coeff p))"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   106
29455
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   107
syntax
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   108
  "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   109
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   110
translations
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   111
  "[:x, xs:]" == "CONST pCons x [:xs:]"
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   112
  "[:x:]" == "CONST pCons x 0"
30155
f65dc19cd5f0 make list-style polynomial syntax work when show_sorts is on
huffman
parents: 30072
diff changeset
   113
  "[:x:]" <= "CONST pCons x (_constrain 0 t)"
29455
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   114
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   115
lemma Poly_nat_case: "f \<in> Poly \<Longrightarrow> nat_case a f \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   116
  unfolding Poly_def by (auto split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   117
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   118
lemma coeff_pCons:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   119
  "coeff (pCons a p) = nat_case a (coeff p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   120
  unfolding pCons_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   121
  by (simp add: Abs_poly_inverse Poly_nat_case coeff)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   122
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   123
lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   124
  by (simp add: coeff_pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   125
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   126
lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   127
  by (simp add: coeff_pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   128
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   129
lemma degree_pCons_le: "degree (pCons a p) \<le> Suc (degree p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   130
by (rule degree_le, simp add: coeff_eq_0 coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   131
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   132
lemma degree_pCons_eq:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   133
  "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   134
apply (rule order_antisym [OF degree_pCons_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   135
apply (rule le_degree, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   136
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   137
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   138
lemma degree_pCons_0: "degree (pCons a 0) = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   139
apply (rule order_antisym [OF _ le0])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   140
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   141
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   142
29460
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
   143
lemma degree_pCons_eq_if [simp]:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   144
  "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   145
apply (cases "p = 0", simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   146
apply (rule order_antisym [OF _ le0])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   147
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   148
apply (rule order_antisym [OF degree_pCons_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   149
apply (rule le_degree, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   150
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   151
46031
ac6bae9fdc2f tuned declaration
haftmann
parents: 45928
diff changeset
   152
lemma pCons_0_0 [simp, code_post]: "pCons 0 0 = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   153
by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   154
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   155
lemma pCons_eq_iff [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   156
  "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   157
proof (safe)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   158
  assume "pCons a p = pCons b q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   159
  then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   160
  then show "a = b" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   161
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   162
  assume "pCons a p = pCons b q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   163
  then have "\<forall>n. coeff (pCons a p) (Suc n) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   164
                 coeff (pCons b q) (Suc n)" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   165
  then show "p = q" by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   166
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   167
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   168
lemma pCons_eq_0_iff [simp]: "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   169
  using pCons_eq_iff [of a p 0 0] by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   170
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   171
lemma Poly_Suc: "f \<in> Poly \<Longrightarrow> (\<lambda>n. f (Suc n)) \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   172
  unfolding Poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   173
  by (clarify, rule_tac x=n in exI, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   174
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   175
lemma pCons_cases [cases type: poly]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   176
  obtains (pCons) a q where "p = pCons a q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   177
proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   178
  show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   179
    by (rule poly_ext)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   180
       (simp add: Abs_poly_inverse Poly_Suc coeff coeff_pCons
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   181
             split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   182
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   183
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   184
lemma pCons_induct [case_names 0 pCons, induct type: poly]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   185
  assumes zero: "P 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   186
  assumes pCons: "\<And>a p. P p \<Longrightarrow> P (pCons a p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   187
  shows "P p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   188
proof (induct p rule: measure_induct_rule [where f=degree])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   189
  case (less p)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   190
  obtain a q where "p = pCons a q" by (rule pCons_cases)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   191
  have "P q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   192
  proof (cases "q = 0")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   193
    case True
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   194
    then show "P q" by (simp add: zero)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   195
  next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   196
    case False
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   197
    then have "degree (pCons a q) = Suc (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   198
      by (rule degree_pCons_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   199
    then have "degree q < degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   200
      using `p = pCons a q` by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   201
    then show "P q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   202
      by (rule less.hyps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   203
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   204
  then have "P (pCons a q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   205
    by (rule pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   206
  then show ?case
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   207
    using `p = pCons a q` by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   208
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   209
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   210
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   211
subsection {* Recursion combinator for polynomials *}
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   212
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   213
function
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   214
  poly_rec :: "'b \<Rightarrow> ('a::zero \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b"
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   215
where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
   216
  poly_rec_pCons_eq_if [simp del]:
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   217
    "poly_rec z f (pCons a p) = f a p (if p = 0 then z else poly_rec z f p)"
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   218
by (case_tac x, rename_tac q, case_tac q, auto)
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   219
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   220
termination poly_rec
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   221
by (relation "measure (degree \<circ> snd \<circ> snd)", simp)
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   222
   (simp add: degree_pCons_eq)
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   223
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   224
lemma poly_rec_0:
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   225
  "f 0 0 z = z \<Longrightarrow> poly_rec z f 0 = z"
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   226
  using poly_rec_pCons_eq_if [of z f 0 0] by simp
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   227
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   228
lemma poly_rec_pCons:
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   229
  "f 0 0 z = z \<Longrightarrow> poly_rec z f (pCons a p) = f a p (poly_rec z f p)"
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   230
  by (simp add: poly_rec_pCons_eq_if poly_rec_0)
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   231
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   232
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   233
subsection {* Monomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   234
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   235
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   236
  monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   237
  "monom a m = Abs_poly (\<lambda>n. if m = n then a else 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   238
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   239
lemma coeff_monom [simp]: "coeff (monom a m) n = (if m=n then a else 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   240
  unfolding monom_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   241
  by (subst Abs_poly_inverse, auto simp add: Poly_def)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   242
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   243
lemma monom_0: "monom a 0 = pCons a 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   244
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   245
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   246
lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   247
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   248
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   249
lemma monom_eq_0 [simp]: "monom 0 n = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   250
  by (rule poly_ext) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   251
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   252
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   253
  by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   254
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   255
lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   256
  by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   257
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   258
lemma degree_monom_le: "degree (monom a n) \<le> n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   259
  by (rule degree_le, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   260
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   261
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   262
  apply (rule order_antisym [OF degree_monom_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   263
  apply (rule le_degree, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   264
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   265
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   266
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   267
subsection {* Addition and subtraction *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   268
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   269
instantiation poly :: (comm_monoid_add) comm_monoid_add
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   270
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   271
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   272
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
   273
  plus_poly_def:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   274
    "p + q = Abs_poly (\<lambda>n. coeff p n + coeff q n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   275
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   276
lemma Poly_add:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   277
  fixes f g :: "nat \<Rightarrow> 'a"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   278
  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n + g n) \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   279
  unfolding Poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   280
  apply (clarify, rename_tac m n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   281
  apply (rule_tac x="max m n" in exI, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   282
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   283
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   284
lemma coeff_add [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   285
  "coeff (p + q) n = coeff p n + coeff q n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   286
  unfolding plus_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   287
  by (simp add: Abs_poly_inverse coeff Poly_add)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   288
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   289
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   290
  fix p q r :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   291
  show "(p + q) + r = p + (q + r)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   292
    by (simp add: expand_poly_eq add_assoc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   293
  show "p + q = q + p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   294
    by (simp add: expand_poly_eq add_commute)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   295
  show "0 + p = p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   296
    by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   297
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   298
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   299
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   300
29904
856f16a3b436 add class cancel_comm_monoid_add
huffman
parents: 29878
diff changeset
   301
instance poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   302
proof
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   303
  fix p q r :: "'a poly"
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   304
  assume "p + q = p + r" thus "q = r"
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   305
    by (simp add: expand_poly_eq)
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   306
qed
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   307
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   308
instantiation poly :: (ab_group_add) ab_group_add
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   309
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   310
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   311
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
   312
  uminus_poly_def:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   313
    "- p = Abs_poly (\<lambda>n. - coeff p n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   314
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   315
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
   316
  minus_poly_def:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   317
    "p - q = Abs_poly (\<lambda>n. coeff p n - coeff q n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   318
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   319
lemma Poly_minus:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   320
  fixes f :: "nat \<Rightarrow> 'a"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   321
  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. - f n) \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   322
  unfolding Poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   323
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   324
lemma Poly_diff:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   325
  fixes f g :: "nat \<Rightarrow> 'a"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   326
  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n - g n) \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   327
  unfolding diff_minus by (simp add: Poly_add Poly_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   328
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   329
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   330
  unfolding uminus_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   331
  by (simp add: Abs_poly_inverse coeff Poly_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   332
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   333
lemma coeff_diff [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   334
  "coeff (p - q) n = coeff p n - coeff q n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   335
  unfolding minus_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   336
  by (simp add: Abs_poly_inverse coeff Poly_diff)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   337
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   338
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   339
  fix p q :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   340
  show "- p + p = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   341
    by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   342
  show "p - q = p + - q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   343
    by (simp add: expand_poly_eq diff_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   344
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   345
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   346
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   347
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   348
lemma add_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   349
  "pCons a p + pCons b q = pCons (a + b) (p + q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   350
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   351
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   352
lemma minus_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   353
  "- pCons a p = pCons (- a) (- p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   354
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   355
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   356
lemma diff_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   357
  "pCons a p - pCons b q = pCons (a - b) (p - q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   358
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   359
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   360
lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   361
  by (rule degree_le, auto simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   362
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   363
lemma degree_add_le:
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   364
  "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n"
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   365
  by (auto intro: order_trans degree_add_le_max)
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   366
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   367
lemma degree_add_less:
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   368
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   369
  by (auto intro: le_less_trans degree_add_le_max)
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   370
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   371
lemma degree_add_eq_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   372
  "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   373
  apply (cases "q = 0", simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   374
  apply (rule order_antisym)
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   375
  apply (simp add: degree_add_le)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   376
  apply (rule le_degree)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   377
  apply (simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   378
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   379
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   380
lemma degree_add_eq_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   381
  "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   382
  using degree_add_eq_right [of q p]
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   383
  by (simp add: add_commute)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   384
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   385
lemma degree_minus [simp]: "degree (- p) = degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   386
  unfolding degree_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   387
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   388
lemma degree_diff_le_max: "degree (p - q) \<le> max (degree p) (degree q)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   389
  using degree_add_le [where p=p and q="-q"]
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   390
  by (simp add: diff_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   391
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   392
lemma degree_diff_le:
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   393
  "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p - q) \<le> n"
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   394
  by (simp add: diff_minus degree_add_le)
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   395
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   396
lemma degree_diff_less:
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   397
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p - q) < n"
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   398
  by (simp add: diff_minus degree_add_less)
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   399
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   400
lemma add_monom: "monom a n + monom b n = monom (a + b) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   401
  by (rule poly_ext) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   402
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   403
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   404
  by (rule poly_ext) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   405
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   406
lemma minus_monom: "- monom a n = monom (-a) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   407
  by (rule poly_ext) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   408
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   409
lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   410
  by (cases "finite A", induct set: finite, simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   411
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   412
lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   413
  by (rule poly_ext) (simp add: coeff_setsum)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   414
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   415
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   416
subsection {* Multiplication by a constant *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   417
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   418
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   419
  smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   420
  "smult a p = Abs_poly (\<lambda>n. a * coeff p n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   421
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   422
lemma Poly_smult:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   423
  fixes f :: "nat \<Rightarrow> 'a::comm_semiring_0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   424
  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. a * f n) \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   425
  unfolding Poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   426
  by (clarify, rule_tac x=n in exI, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   427
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   428
lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   429
  unfolding smult_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   430
  by (simp add: Abs_poly_inverse Poly_smult coeff)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   431
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   432
lemma degree_smult_le: "degree (smult a p) \<le> degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   433
  by (rule degree_le, simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   434
29472
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   435
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   436
  by (rule poly_ext, simp add: mult_assoc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   437
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   438
lemma smult_0_right [simp]: "smult a 0 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   439
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   440
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   441
lemma smult_0_left [simp]: "smult 0 p = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   442
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   443
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   444
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   445
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   446
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   447
lemma smult_add_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   448
  "smult a (p + q) = smult a p + smult a q"
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
   449
  by (rule poly_ext, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   450
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   451
lemma smult_add_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   452
  "smult (a + b) p = smult a p + smult b p"
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
   453
  by (rule poly_ext, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   454
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
   455
lemma smult_minus_right [simp]:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   456
  "smult (a::'a::comm_ring) (- p) = - smult a p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   457
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   458
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
   459
lemma smult_minus_left [simp]:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   460
  "smult (- a::'a::comm_ring) p = - smult a p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   461
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   462
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   463
lemma smult_diff_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   464
  "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
   465
  by (rule poly_ext, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   466
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   467
lemma smult_diff_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   468
  "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
   469
  by (rule poly_ext, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   470
29472
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   471
lemmas smult_distribs =
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   472
  smult_add_left smult_add_right
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   473
  smult_diff_left smult_diff_right
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   474
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   475
lemma smult_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   476
  "smult a (pCons b p) = pCons (a * b) (smult a p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   477
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   478
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   479
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   480
  by (induct n, simp add: monom_0, simp add: monom_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   481
29659
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   482
lemma degree_smult_eq [simp]:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   483
  fixes a :: "'a::idom"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   484
  shows "degree (smult a p) = (if a = 0 then 0 else degree p)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   485
  by (cases "a = 0", simp, simp add: degree_def)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   486
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   487
lemma smult_eq_0_iff [simp]:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   488
  fixes a :: "'a::idom"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   489
  shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   490
  by (simp add: expand_poly_eq)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   491
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   492
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   493
subsection {* Multiplication of polynomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   494
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   495
text {* TODO: move to SetInterval.thy *}
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   496
lemma setsum_atMost_Suc_shift:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   497
  fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   498
  shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   499
proof (induct n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   500
  case 0 show ?case by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   501
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   502
  case (Suc n) note IH = this
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   503
  have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   504
    by (rule setsum_atMost_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   505
  also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   506
    by (rule IH)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   507
  also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   508
             f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   509
    by (rule add_assoc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   510
  also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   511
    by (rule setsum_atMost_Suc [symmetric])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   512
  finally show ?case .
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   513
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   514
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   515
instantiation poly :: (comm_semiring_0) comm_semiring_0
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   516
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   517
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   518
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
   519
  times_poly_def:
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   520
    "p * q = poly_rec 0 (\<lambda>a p pq. smult a q + pCons 0 pq) p"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   521
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   522
lemma mult_poly_0_left: "(0::'a poly) * q = 0"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   523
  unfolding times_poly_def by (simp add: poly_rec_0)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   524
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   525
lemma mult_pCons_left [simp]:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   526
  "pCons a p * q = smult a q + pCons 0 (p * q)"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   527
  unfolding times_poly_def by (simp add: poly_rec_pCons)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   528
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   529
lemma mult_poly_0_right: "p * (0::'a poly) = 0"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   530
  by (induct p, simp add: mult_poly_0_left, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   531
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   532
lemma mult_pCons_right [simp]:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   533
  "p * pCons a q = smult a p + pCons 0 (p * q)"
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
   534
  by (induct p, simp add: mult_poly_0_left, simp add: algebra_simps)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   535
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   536
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   537
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   538
lemma mult_smult_left [simp]: "smult a p * q = smult a (p * q)"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   539
  by (induct p, simp add: mult_poly_0, simp add: smult_add_right)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   540
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   541
lemma mult_smult_right [simp]: "p * smult a q = smult a (p * q)"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   542
  by (induct q, simp add: mult_poly_0, simp add: smult_add_right)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   543
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   544
lemma mult_poly_add_left:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   545
  fixes p q r :: "'a poly"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   546
  shows "(p + q) * r = p * r + q * r"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   547
  by (induct r, simp add: mult_poly_0,
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
   548
                simp add: smult_distribs algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   549
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   550
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   551
  fix p q r :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   552
  show 0: "0 * p = 0"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   553
    by (rule mult_poly_0_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   554
  show "p * 0 = 0"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   555
    by (rule mult_poly_0_right)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   556
  show "(p + q) * r = p * r + q * r"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   557
    by (rule mult_poly_add_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   558
  show "(p * q) * r = p * (q * r)"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   559
    by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   560
  show "p * q = q * p"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   561
    by (induct p, simp add: mult_poly_0, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   562
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   563
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   564
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   565
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   566
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   567
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   568
lemma coeff_mult:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   569
  "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   570
proof (induct p arbitrary: n)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   571
  case 0 show ?case by simp
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   572
next
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   573
  case (pCons a p n) thus ?case
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   574
    by (cases n, simp, simp add: setsum_atMost_Suc_shift
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   575
                            del: setsum_atMost_Suc)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   576
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   577
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   578
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   579
apply (rule degree_le)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   580
apply (induct p)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   581
apply simp
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   582
apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   583
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   584
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   585
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   586
  by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   587
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   588
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   589
subsection {* The unit polynomial and exponentiation *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   590
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   591
instantiation poly :: (comm_semiring_1) comm_semiring_1
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   592
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   593
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   594
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   595
  one_poly_def:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   596
    "1 = pCons 1 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   597
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   598
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   599
  fix p :: "'a poly" show "1 * p = p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   600
    unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   601
    by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   602
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   603
  show "0 \<noteq> (1::'a poly)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   604
    unfolding one_poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   605
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   606
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   607
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   608
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   609
instance poly :: (comm_semiring_1_cancel) comm_semiring_1_cancel ..
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   610
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   611
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   612
  unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   613
  by (simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   614
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   615
lemma degree_1 [simp]: "degree 1 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   616
  unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   617
  by (rule degree_pCons_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   618
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   619
text {* Lemmas about divisibility *}
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   620
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   621
lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   622
proof -
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   623
  assume "p dvd q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   624
  then obtain k where "q = p * k" ..
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   625
  then have "smult a q = p * smult a k" by simp
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   626
  then show "p dvd smult a q" ..
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   627
qed
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   628
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   629
lemma dvd_smult_cancel:
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   630
  fixes a :: "'a::field"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   631
  shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   632
  by (drule dvd_smult [where a="inverse a"]) simp
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   633
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   634
lemma dvd_smult_iff:
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   635
  fixes a :: "'a::field"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   636
  shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   637
  by (safe elim!: dvd_smult dvd_smult_cancel)
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   638
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   639
lemma smult_dvd_cancel:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   640
  "smult a p dvd q \<Longrightarrow> p dvd q"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   641
proof -
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   642
  assume "smult a p dvd q"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   643
  then obtain k where "q = smult a p * k" ..
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   644
  then have "q = p * smult a k" by simp
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   645
  then show "p dvd q" ..
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   646
qed
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   647
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   648
lemma smult_dvd:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   649
  fixes a :: "'a::field"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   650
  shows "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   651
  by (rule smult_dvd_cancel [where a="inverse a"]) simp
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   652
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   653
lemma smult_dvd_iff:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   654
  fixes a :: "'a::field"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   655
  shows "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   656
  by (auto elim: smult_dvd smult_dvd_cancel)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
   657
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   658
lemma degree_power_le: "degree (p ^ n) \<le> degree p * n"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   659
by (induct n, simp, auto intro: order_trans degree_mult_le)
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
   660
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   661
instance poly :: (comm_ring) comm_ring ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   662
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   663
instance poly :: (comm_ring_1) comm_ring_1 ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   664
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   665
instantiation poly :: (comm_ring_1) number_ring
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   666
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   667
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   668
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   669
  "number_of k = (of_int k :: 'a poly)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   670
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   671
instance
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   672
  by default (rule number_of_poly_def)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   673
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   674
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   675
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   676
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   677
subsection {* Polynomials form an integral domain *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   678
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   679
lemma coeff_mult_degree_sum:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   680
  "coeff (p * q) (degree p + degree q) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   681
   coeff p (degree p) * coeff q (degree q)"
29471
6a46a13ce1f9 simplify proof of coeff_mult_degree_sum
huffman
parents: 29462
diff changeset
   682
  by (induct p, simp, simp add: coeff_eq_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   683
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   684
instance poly :: (idom) idom
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   685
proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   686
  fix p q :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   687
  assume "p \<noteq> 0" and "q \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   688
  have "coeff (p * q) (degree p + degree q) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   689
        coeff p (degree p) * coeff q (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   690
    by (rule coeff_mult_degree_sum)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   691
  also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   692
    using `p \<noteq> 0` and `q \<noteq> 0` by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   693
  finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   694
  thus "p * q \<noteq> 0" by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   695
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   696
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   697
lemma degree_mult_eq:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   698
  fixes p q :: "'a::idom poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   699
  shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   700
apply (rule order_antisym [OF degree_mult_le le_degree])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   701
apply (simp add: coeff_mult_degree_sum)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   702
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   703
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   704
lemma dvd_imp_degree_le:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   705
  fixes p q :: "'a::idom poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   706
  shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   707
  by (erule dvdE, simp add: degree_mult_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   708
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   709
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   710
subsection {* Polynomials form an ordered integral domain *}
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   711
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   712
definition
35028
108662d50512 more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents: 34973
diff changeset
   713
  pos_poly :: "'a::linordered_idom poly \<Rightarrow> bool"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   714
where
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   715
  "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   716
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   717
lemma pos_poly_pCons:
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   718
  "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   719
  unfolding pos_poly_def by simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   720
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   721
lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   722
  unfolding pos_poly_def by simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   723
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   724
lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   725
  apply (induct p arbitrary: q, simp)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   726
  apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   727
  done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   728
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   729
lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   730
  unfolding pos_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   731
  apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   732
  apply (simp add: degree_mult_eq coeff_mult_degree_sum mult_pos_pos)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   733
  apply auto
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   734
  done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   735
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   736
lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   737
by (induct p) (auto simp add: pos_poly_pCons)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   738
35028
108662d50512 more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents: 34973
diff changeset
   739
instantiation poly :: (linordered_idom) linordered_idom
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   740
begin
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   741
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   742
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
   743
  "x < y \<longleftrightarrow> pos_poly (y - x)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   744
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   745
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
   746
  "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   747
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   748
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
   749
  "abs (x::'a poly) = (if x < 0 then - x else x)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   750
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   751
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
   752
  "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   753
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   754
instance proof
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   755
  fix x y :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   756
  show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   757
    unfolding less_eq_poly_def less_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   758
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   759
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   760
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   761
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   762
    done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   763
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   764
  fix x :: "'a poly" show "x \<le> x"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   765
    unfolding less_eq_poly_def by simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   766
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   767
  fix x y z :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   768
  assume "x \<le> y" and "y \<le> z" thus "x \<le> z"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   769
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   770
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   771
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   772
    apply (simp add: algebra_simps)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   773
    done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   774
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   775
  fix x y :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   776
  assume "x \<le> y" and "y \<le> x" thus "x = y"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   777
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   778
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   779
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   780
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   781
    done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   782
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   783
  fix x y z :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   784
  assume "x \<le> y" thus "z + x \<le> z + y"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   785
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   786
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   787
    apply (simp add: algebra_simps)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   788
    done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   789
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   790
  fix x y :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   791
  show "x \<le> y \<or> y \<le> x"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   792
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   793
    using pos_poly_total [of "x - y"]
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   794
    by auto
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   795
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   796
  fix x y z :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   797
  assume "x < y" and "0 < z"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   798
  thus "z * x < z * y"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   799
    unfolding less_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   800
    by (simp add: right_diff_distrib [symmetric] pos_poly_mult)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   801
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   802
  fix x :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   803
  show "\<bar>x\<bar> = (if x < 0 then - x else x)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   804
    by (rule abs_poly_def)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   805
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   806
  fix x :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   807
  show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   808
    by (rule sgn_poly_def)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   809
qed
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   810
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   811
end
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   812
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   813
text {* TODO: Simplification rules for comparisons *}
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   814
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   815
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   816
subsection {* Long division of polynomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   817
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   818
definition
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   819
  pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   820
where
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   821
  "pdivmod_rel x y q r \<longleftrightarrow>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   822
    x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   823
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   824
lemma pdivmod_rel_0:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   825
  "pdivmod_rel 0 y 0 0"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   826
  unfolding pdivmod_rel_def by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   827
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   828
lemma pdivmod_rel_by_0:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   829
  "pdivmod_rel x 0 0 x"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   830
  unfolding pdivmod_rel_def by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   831
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   832
lemma eq_zero_or_degree_less:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   833
  assumes "degree p \<le> n" and "coeff p n = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   834
  shows "p = 0 \<or> degree p < n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   835
proof (cases n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   836
  case 0
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   837
  with `degree p \<le> n` and `coeff p n = 0`
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   838
  have "coeff p (degree p) = 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   839
  then have "p = 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   840
  then show ?thesis ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   841
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   842
  case (Suc m)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   843
  have "\<forall>i>n. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   844
    using `degree p \<le> n` by (simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   845
  then have "\<forall>i\<ge>n. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   846
    using `coeff p n = 0` by (simp add: le_less)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   847
  then have "\<forall>i>m. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   848
    using `n = Suc m` by (simp add: less_eq_Suc_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   849
  then have "degree p \<le> m"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   850
    by (rule degree_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   851
  then have "degree p < n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   852
    using `n = Suc m` by (simp add: less_Suc_eq_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   853
  then show ?thesis ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   854
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   855
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   856
lemma pdivmod_rel_pCons:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   857
  assumes rel: "pdivmod_rel x y q r"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   858
  assumes y: "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   859
  assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   860
  shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   861
    (is "pdivmod_rel ?x y ?q ?r")
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   862
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   863
  have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   864
    using assms unfolding pdivmod_rel_def by simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   865
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   866
  have 1: "?x = ?q * y + ?r"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   867
    using b x by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   868
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   869
  have 2: "?r = 0 \<or> degree ?r < degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   870
  proof (rule eq_zero_or_degree_less)
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   871
    show "degree ?r \<le> degree y"
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   872
    proof (rule degree_diff_le)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   873
      show "degree (pCons a r) \<le> degree y"
29460
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
   874
        using r by auto
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   875
      show "degree (smult b y) \<le> degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   876
        by (rule degree_smult_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   877
    qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   878
  next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   879
    show "coeff ?r (degree y) = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   880
      using `y \<noteq> 0` unfolding b by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   881
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   882
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   883
  from 1 2 show ?thesis
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   884
    unfolding pdivmod_rel_def
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   885
    using `y \<noteq> 0` by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   886
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   887
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   888
lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   889
apply (cases "y = 0")
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   890
apply (fast intro!: pdivmod_rel_by_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   891
apply (induct x)
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   892
apply (fast intro!: pdivmod_rel_0)
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   893
apply (fast intro!: pdivmod_rel_pCons)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   894
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   895
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   896
lemma pdivmod_rel_unique:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   897
  assumes 1: "pdivmod_rel x y q1 r1"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   898
  assumes 2: "pdivmod_rel x y q2 r2"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   899
  shows "q1 = q2 \<and> r1 = r2"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   900
proof (cases "y = 0")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   901
  assume "y = 0" with assms show ?thesis
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   902
    by (simp add: pdivmod_rel_def)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   903
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   904
  assume [simp]: "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   905
  from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   906
    unfolding pdivmod_rel_def by simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   907
  from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   908
    unfolding pdivmod_rel_def by simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   909
  from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
   910
    by (simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   911
  from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   912
    by (auto intro: degree_diff_less)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   913
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   914
  show "q1 = q2 \<and> r1 = r2"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   915
  proof (rule ccontr)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   916
    assume "\<not> (q1 = q2 \<and> r1 = r2)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   917
    with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   918
    with r3 have "degree (r2 - r1) < degree y" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   919
    also have "degree y \<le> degree (q1 - q2) + degree y" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   920
    also have "\<dots> = degree ((q1 - q2) * y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   921
      using `q1 \<noteq> q2` by (simp add: degree_mult_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   922
    also have "\<dots> = degree (r2 - r1)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   923
      using q3 by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   924
    finally have "degree (r2 - r1) < degree (r2 - r1)" .
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   925
    then show "False" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   926
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   927
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   928
29660
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
   929
lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
   930
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
   931
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
   932
lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
   933
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
   934
45605
a89b4bc311a5 eliminated obsolete "standard";
wenzelm
parents: 44890
diff changeset
   935
lemmas pdivmod_rel_unique_div = pdivmod_rel_unique [THEN conjunct1]
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   936
45605
a89b4bc311a5 eliminated obsolete "standard";
wenzelm
parents: 44890
diff changeset
   937
lemmas pdivmod_rel_unique_mod = pdivmod_rel_unique [THEN conjunct2]
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   938
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   939
instantiation poly :: (field) ring_div
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   940
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   941
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   942
definition div_poly where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
   943
  "x div y = (THE q. \<exists>r. pdivmod_rel x y q r)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   944
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   945
definition mod_poly where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
   946
  "x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   947
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   948
lemma div_poly_eq:
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   949
  "pdivmod_rel x y q r \<Longrightarrow> x div y = q"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   950
unfolding div_poly_def
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   951
by (fast elim: pdivmod_rel_unique_div)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   952
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   953
lemma mod_poly_eq:
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   954
  "pdivmod_rel x y q r \<Longrightarrow> x mod y = r"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   955
unfolding mod_poly_def
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   956
by (fast elim: pdivmod_rel_unique_mod)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   957
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   958
lemma pdivmod_rel:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   959
  "pdivmod_rel x y (x div y) (x mod y)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   960
proof -
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   961
  from pdivmod_rel_exists
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   962
    obtain q r where "pdivmod_rel x y q r" by fast
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   963
  thus ?thesis
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   964
    by (simp add: div_poly_eq mod_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   965
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   966
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   967
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   968
  fix x y :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   969
  show "x div y * y + x mod y = x"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   970
    using pdivmod_rel [of x y]
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   971
    by (simp add: pdivmod_rel_def)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   972
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   973
  fix x :: "'a poly"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   974
  have "pdivmod_rel x 0 0 x"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   975
    by (rule pdivmod_rel_by_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   976
  thus "x div 0 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   977
    by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   978
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   979
  fix y :: "'a poly"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   980
  have "pdivmod_rel 0 y 0 0"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   981
    by (rule pdivmod_rel_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   982
  thus "0 div y = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   983
    by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   984
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   985
  fix x y z :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   986
  assume "y \<noteq> 0"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   987
  hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   988
    using pdivmod_rel [of x y]
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   989
    by (simp add: pdivmod_rel_def left_distrib)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   990
  thus "(x + z * y) div y = z + x div y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   991
    by (rule div_poly_eq)
30930
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
   992
next
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
   993
  fix x y z :: "'a poly"
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
   994
  assume "x \<noteq> 0"
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
   995
  show "(x * y) div (x * z) = y div z"
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
   996
  proof (cases "y \<noteq> 0 \<and> z \<noteq> 0")
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
   997
    have "\<And>x::'a poly. pdivmod_rel x 0 0 x"
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
   998
      by (rule pdivmod_rel_by_0)
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
   999
    then have [simp]: "\<And>x::'a poly. x div 0 = 0"
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1000
      by (rule div_poly_eq)
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1001
    have "\<And>x::'a poly. pdivmod_rel 0 x 0 0"
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1002
      by (rule pdivmod_rel_0)
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1003
    then have [simp]: "\<And>x::'a poly. 0 div x = 0"
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1004
      by (rule div_poly_eq)
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1005
    case False then show ?thesis by auto
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1006
  next
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1007
    case True then have "y \<noteq> 0" and "z \<noteq> 0" by auto
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1008
    with `x \<noteq> 0`
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1009
    have "\<And>q r. pdivmod_rel y z q r \<Longrightarrow> pdivmod_rel (x * y) (x * z) q (x * r)"
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1010
      by (auto simp add: pdivmod_rel_def algebra_simps)
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1011
        (rule classical, simp add: degree_mult_eq)
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1012
    moreover from pdivmod_rel have "pdivmod_rel y z (y div z) (y mod z)" .
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1013
    ultimately have "pdivmod_rel (x * y) (x * z) (y div z) (x * (y mod z))" .
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1014
    then show ?thesis by (simp add: div_poly_eq)
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1015
  qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1016
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1017
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1018
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1019
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1020
lemma degree_mod_less:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1021
  "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1022
  using pdivmod_rel [of x y]
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1023
  unfolding pdivmod_rel_def by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1024
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1025
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1026
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1027
  assume "degree x < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1028
  hence "pdivmod_rel x y 0 x"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1029
    by (simp add: pdivmod_rel_def)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1030
  thus "x div y = 0" by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1031
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1032
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1033
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1034
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1035
  assume "degree x < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1036
  hence "pdivmod_rel x y 0 x"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1037
    by (simp add: pdivmod_rel_def)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1038
  thus "x mod y = x" by (rule mod_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1039
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1040
29659
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1041
lemma pdivmod_rel_smult_left:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1042
  "pdivmod_rel x y q r
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1043
    \<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1044
  unfolding pdivmod_rel_def by (simp add: smult_add_right)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1045
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1046
lemma div_smult_left: "(smult a x) div y = smult a (x div y)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1047
  by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1048
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1049
lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1050
  by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1051
30072
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1052
lemma poly_div_minus_left [simp]:
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1053
  fixes x y :: "'a::field poly"
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1054
  shows "(- x) div y = - (x div y)"
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1055
  using div_smult_left [of "- 1::'a"] by simp
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1056
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1057
lemma poly_mod_minus_left [simp]:
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1058
  fixes x y :: "'a::field poly"
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1059
  shows "(- x) mod y = - (x mod y)"
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1060
  using mod_smult_left [of "- 1::'a"] by simp
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1061
29659
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1062
lemma pdivmod_rel_smult_right:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1063
  "\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk>
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1064
    \<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1065
  unfolding pdivmod_rel_def by simp
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1066
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1067
lemma div_smult_right:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1068
  "a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1069
  by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1070
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1071
lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1072
  by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1073
30072
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1074
lemma poly_div_minus_right [simp]:
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1075
  fixes x y :: "'a::field poly"
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1076
  shows "x div (- y) = - (x div y)"
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1077
  using div_smult_right [of "- 1::'a"]
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1078
  by (simp add: nonzero_inverse_minus_eq)
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1079
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1080
lemma poly_mod_minus_right [simp]:
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1081
  fixes x y :: "'a::field poly"
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1082
  shows "x mod (- y) = x mod y"
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1083
  using mod_smult_right [of "- 1::'a"] by simp
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1084
29660
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1085
lemma pdivmod_rel_mult:
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1086
  "\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk>
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1087
    \<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1088
apply (cases "z = 0", simp add: pdivmod_rel_def)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1089
apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1090
apply (cases "r = 0")
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1091
apply (cases "r' = 0")
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1092
apply (simp add: pdivmod_rel_def)
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35028
diff changeset
  1093
apply (simp add: pdivmod_rel_def field_simps degree_mult_eq)
29660
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1094
apply (cases "r' = 0")
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1095
apply (simp add: pdivmod_rel_def degree_mult_eq)
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35028
diff changeset
  1096
apply (simp add: pdivmod_rel_def field_simps)
29660
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1097
apply (simp add: degree_mult_eq degree_add_less)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1098
done
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1099
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1100
lemma poly_div_mult_right:
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1101
  fixes x y z :: "'a::field poly"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1102
  shows "x div (y * z) = (x div y) div z"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1103
  by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1104
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1105
lemma poly_mod_mult_right:
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1106
  fixes x y z :: "'a::field poly"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1107
  shows "x mod (y * z) = y * (x div y mod z) + x mod y"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1108
  by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1109
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1110
lemma mod_pCons:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1111
  fixes a and x
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1112
  assumes y: "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1113
  defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1114
  shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1115
unfolding b
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1116
apply (rule mod_poly_eq)
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1117
apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl])
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1118
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1119
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1120
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1121
subsection {* GCD of polynomials *}
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1122
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1123
function
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1124
  poly_gcd :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1125
  "poly_gcd x 0 = smult (inverse (coeff x (degree x))) x"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1126
| "y \<noteq> 0 \<Longrightarrow> poly_gcd x y = poly_gcd y (x mod y)"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1127
by auto
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1128
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1129
termination poly_gcd
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1130
by (relation "measure (\<lambda>(x, y). if y = 0 then 0 else Suc (degree y))")
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1131
   (auto dest: degree_mod_less)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1132
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
  1133
declare poly_gcd.simps [simp del]
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1134
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1135
lemma poly_gcd_dvd1 [iff]: "poly_gcd x y dvd x"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1136
  and poly_gcd_dvd2 [iff]: "poly_gcd x y dvd y"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1137
  apply (induct x y rule: poly_gcd.induct)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1138
  apply (simp_all add: poly_gcd.simps)
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 41959
diff changeset
  1139
  apply (fastforce simp add: smult_dvd_iff dest: inverse_zero_imp_zero)
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1140
  apply (blast dest: dvd_mod_imp_dvd)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1141
  done
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1142
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1143
lemma poly_gcd_greatest: "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd poly_gcd x y"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1144
  by (induct x y rule: poly_gcd.induct)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1145
     (simp_all add: poly_gcd.simps dvd_mod dvd_smult)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1146
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1147
lemma dvd_poly_gcd_iff [iff]:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1148
  "k dvd poly_gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1149
  by (blast intro!: poly_gcd_greatest intro: dvd_trans)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1150
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1151
lemma poly_gcd_monic:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1152
  "coeff (poly_gcd x y) (degree (poly_gcd x y)) =
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1153
    (if x = 0 \<and> y = 0 then 0 else 1)"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1154
  by (induct x y rule: poly_gcd.induct)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1155
     (simp_all add: poly_gcd.simps nonzero_imp_inverse_nonzero)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1156
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1157
lemma poly_gcd_zero_iff [simp]:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1158
  "poly_gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1159
  by (simp only: dvd_0_left_iff [symmetric] dvd_poly_gcd_iff)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1160
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1161
lemma poly_gcd_0_0 [simp]: "poly_gcd 0 0 = 0"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1162
  by simp
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1163
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1164
lemma poly_dvd_antisym:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1165
  fixes p q :: "'a::idom poly"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1166
  assumes coeff: "coeff p (degree p) = coeff q (degree q)"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1167
  assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1168
proof (cases "p = 0")
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1169
  case True with coeff show "p = q" by simp
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1170
next
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1171
  case False with coeff have "q \<noteq> 0" by auto
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1172
  have degree: "degree p = degree q"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1173
    using `p dvd q` `q dvd p` `p \<noteq> 0` `q \<noteq> 0`
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1174
    by (intro order_antisym dvd_imp_degree_le)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1175
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1176
  from `p dvd q` obtain a where a: "q = p * a" ..
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1177
  with `q \<noteq> 0` have "a \<noteq> 0" by auto
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1178
  with degree a `p \<noteq> 0` have "degree a = 0"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1179
    by (simp add: degree_mult_eq)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1180
  with coeff a show "p = q"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1181
    by (cases a, auto split: if_splits)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1182
qed
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1183
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1184
lemma poly_gcd_unique:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1185
  assumes dvd1: "d dvd x" and dvd2: "d dvd y"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1186
    and greatest: "\<And>k. k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd d"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1187
    and monic: "coeff d (degree d) = (if x = 0 \<and> y = 0 then 0 else 1)"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1188
  shows "poly_gcd x y = d"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1189
proof -
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1190
  have "coeff (poly_gcd x y) (degree (poly_gcd x y)) = coeff d (degree d)"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1191
    by (simp_all add: poly_gcd_monic monic)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1192
  moreover have "poly_gcd x y dvd d"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1193
    using poly_gcd_dvd1 poly_gcd_dvd2 by (rule greatest)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1194
  moreover have "d dvd poly_gcd x y"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1195
    using dvd1 dvd2 by (rule poly_gcd_greatest)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1196
  ultimately show ?thesis
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1197
    by (rule poly_dvd_antisym)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1198
qed
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1199
37770
cddb3106adb8 avoid explicit mandatory prefix markers when prefixes are mandatory implicitly
haftmann
parents: 37765
diff changeset
  1200
interpretation poly_gcd: abel_semigroup poly_gcd
34973
ae634fad947e dropped mk_left_commute; use interpretation of locale abel_semigroup instead
haftmann
parents: 34915
diff changeset
  1201
proof
ae634fad947e dropped mk_left_commute; use interpretation of locale abel_semigroup instead
haftmann
parents: 34915
diff changeset
  1202
  fix x y z :: "'a poly"
ae634fad947e dropped mk_left_commute; use interpretation of locale abel_semigroup instead
haftmann
parents: 34915
diff changeset
  1203
  show "poly_gcd (poly_gcd x y) z = poly_gcd x (poly_gcd y z)"
ae634fad947e dropped mk_left_commute; use interpretation of locale abel_semigroup instead
haftmann
parents: 34915
diff changeset
  1204
    by (rule poly_gcd_unique) (auto intro: dvd_trans simp add: poly_gcd_monic)
ae634fad947e dropped mk_left_commute; use interpretation of locale abel_semigroup instead
haftmann
parents: 34915
diff changeset
  1205
  show "poly_gcd x y = poly_gcd y x"
ae634fad947e dropped mk_left_commute; use interpretation of locale abel_semigroup instead
haftmann
parents: 34915
diff changeset
  1206
    by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
ae634fad947e dropped mk_left_commute; use interpretation of locale abel_semigroup instead
haftmann
parents: 34915
diff changeset
  1207
qed
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1208
34973
ae634fad947e dropped mk_left_commute; use interpretation of locale abel_semigroup instead
haftmann
parents: 34915
diff changeset
  1209
lemmas poly_gcd_assoc = poly_gcd.assoc
ae634fad947e dropped mk_left_commute; use interpretation of locale abel_semigroup instead
haftmann
parents: 34915
diff changeset
  1210
lemmas poly_gcd_commute = poly_gcd.commute
ae634fad947e dropped mk_left_commute; use interpretation of locale abel_semigroup instead
haftmann
parents: 34915
diff changeset
  1211
lemmas poly_gcd_left_commute = poly_gcd.left_commute
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1212
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1213
lemmas poly_gcd_ac = poly_gcd_assoc poly_gcd_commute poly_gcd_left_commute
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1214
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1215
lemma poly_gcd_1_left [simp]: "poly_gcd 1 y = 1"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1216
by (rule poly_gcd_unique) simp_all
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1217
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1218
lemma poly_gcd_1_right [simp]: "poly_gcd x 1 = 1"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1219
by (rule poly_gcd_unique) simp_all
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1220
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1221
lemma poly_gcd_minus_left [simp]: "poly_gcd (- x) y = poly_gcd x y"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1222
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1223
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1224
lemma poly_gcd_minus_right [simp]: "poly_gcd x (- y) = poly_gcd x y"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1225
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1226
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1227
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1228
subsection {* Evaluation of polynomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1229
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1230
definition
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1231
  poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a" where
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1232
  "poly = poly_rec (\<lambda>x. 0) (\<lambda>a p f x. a + x * f x)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1233
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1234
lemma poly_0 [simp]: "poly 0 x = 0"
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1235
  unfolding poly_def by (simp add: poly_rec_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1236
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1237
lemma poly_pCons [simp]: "poly (pCons a p) x = a + x * poly p x"
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1238
  unfolding poly_def by (simp add: poly_rec_pCons)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1239
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1240
lemma poly_1 [simp]: "poly 1 x = 1"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1241
  unfolding one_poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1242
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1243
lemma poly_monom:
31021
53642251a04f farewell to class recpower
haftmann
parents: 30960
diff changeset
  1244
  fixes a x :: "'a::{comm_semiring_1}"
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1245
  shows "poly (monom a n) x = a * x ^ n"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1246
  by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1247
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1248
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1249
  apply (induct p arbitrary: q, simp)
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
  1250
  apply (case_tac q, simp, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1251
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1252
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1253
lemma poly_minus [simp]:
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1254
  fixes x :: "'a::comm_ring"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1255
  shows "poly (- p) x = - poly p x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1256
  by (induct p, simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1257
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1258
lemma poly_diff [simp]:
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1259
  fixes x :: "'a::comm_ring"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1260
  shows "poly (p - q) x = poly p x - poly q x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1261
  by (simp add: diff_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1262
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1263
lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1264
  by (cases "finite A", induct set: finite, simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1265
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1266
lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x"
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
  1267
  by (induct p, simp, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1268
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1269
lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x"
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
  1270
  by (induct p, simp_all, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1271
29462
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1272
lemma poly_power [simp]:
31021
53642251a04f farewell to class recpower
haftmann
parents: 30960
diff changeset
  1273
  fixes p :: "'a::{comm_semiring_1} poly"
29462
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1274
  shows "poly (p ^ n) x = poly p x ^ n"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1275
  by (induct n, simp, simp add: power_Suc)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1276
29456
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1277
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1278
subsection {* Synthetic division *}
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1279
29980
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1280
text {*
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1281
  Synthetic division is simply division by the
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1282
  linear polynomial @{term "x - c"}.
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1283
*}
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1284
29456
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1285
definition
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1286
  synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
  1287
where
29456
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1288
  "synthetic_divmod p c =
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1289
    poly_rec (0, 0) (\<lambda>a p (q, r). (pCons r q, a + c * r)) p"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1290
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1291
definition
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1292
  synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1293
where
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1294
  "synthetic_div p c = fst (synthetic_divmod p c)"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1295
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1296
lemma synthetic_divmod_0 [simp]:
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1297
  "synthetic_divmod 0 c = (0, 0)"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1298
  unfolding synthetic_divmod_def
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1299
  by (simp add: poly_rec_0)
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1300
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1301
lemma synthetic_divmod_pCons [simp]:
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1302
  "synthetic_divmod (pCons a p) c =
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1303
    (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1304
  unfolding synthetic_divmod_def
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1305
  by (simp add: poly_rec_pCons)
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1306
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1307
lemma snd_synthetic_divmod: "snd (synthetic_divmod p c) = poly p c"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1308
  by (induct p, simp, simp add: split_def)
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1309
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1310
lemma synthetic_div_0 [simp]: "synthetic_div 0 c = 0"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1311
  unfolding synthetic_div_def by simp
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1312
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1313
lemma synthetic_div_pCons [simp]:
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1314
  "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1315
  unfolding synthetic_div_def
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1316
  by (simp add: split_def snd_synthetic_divmod)
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1317
29460
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1318
lemma synthetic_div_eq_0_iff:
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1319
  "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0"
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1320
  by (induct p, simp, case_tac p, simp)
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1321
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1322
lemma degree_synthetic_div:
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1323
  "degree (synthetic_div p c) = degree p - 1"
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1324
  by (induct p, simp, simp add: synthetic_div_eq_0_iff)
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1325
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1326
lemma synthetic_div_correct:
29456
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1327
  "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1328
  by (induct p) simp_all
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1329
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1330
lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0"
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1331
by (induct p arbitrary: a) simp_all
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1332
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1333
lemma synthetic_div_unique:
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1334
  "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c"
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1335
apply (induct p arbitrary: q r)
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1336
apply (simp, frule synthetic_div_unique_lemma, simp)
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1337
apply (case_tac q, force)
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1338
done
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1339
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1340
lemma synthetic_div_correct':
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1341
  fixes c :: "'a::comm_ring_1"
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1342
  shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1343
  using synthetic_div_correct [of p c]
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
  1344
  by (simp add: algebra_simps)
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1345
29460
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1346
lemma poly_eq_0_iff_dvd:
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1347
  fixes c :: "'a::idom"
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1348
  shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p"
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1349
proof
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1350
  assume "poly p c = 0"
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1351
  with synthetic_div_correct' [of c p]
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1352
  have "p = [:-c, 1:] * synthetic_div p c" by simp
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1353
  then show "[:-c, 1:] dvd p" ..
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1354
next
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1355
  assume "[:-c, 1:] dvd p"
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1356
  then obtain k where "p = [:-c, 1:] * k" by (rule dvdE)
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1357
  then show "poly p c = 0" by simp
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1358
qed
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1359
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1360
lemma dvd_iff_poly_eq_0:
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1361
  fixes c :: "'a::idom"
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1362
  shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0"
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1363
  by (simp add: poly_eq_0_iff_dvd)
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1364
29462
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1365
lemma poly_roots_finite:
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1366
  fixes p :: "'a::idom poly"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1367
  shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1368
proof (induct n \<equiv> "degree p" arbitrary: p)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1369
  case (0 p)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1370
  then obtain a where "a \<noteq> 0" and "p = [:a:]"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1371
    by (cases p, simp split: if_splits)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1372
  then show "finite {x. poly p x = 0}" by simp
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1373
next
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1374
  case (Suc n p)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1375
  show "finite {x. poly p x = 0}"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1376
  proof (cases "\<exists>x. poly p x = 0")
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1377
    case False
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1378
    then show "finite {x. poly p x = 0}" by simp
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1379
  next
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1380
    case True
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1381
    then obtain a where "poly p a = 0" ..
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1382
    then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1383
    then obtain k where k: "p = [:-a, 1:] * k" ..
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1384
    with `p \<noteq> 0` have "k \<noteq> 0" by auto
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1385
    with k have "degree p = Suc (degree k)"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1386
      by (simp add: degree_mult_eq del: mult_pCons_left)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1387
    with `Suc n = degree p` have "n = degree k" by simp
34915
7894c7dab132 Adapted to changes in induct method.
berghofe
parents: 31998
diff changeset
  1388
    then have "finite {x. poly k x = 0}" using `k \<noteq> 0` by (rule Suc.hyps)
29462
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1389
    then have "finite (insert a {x. poly k x = 0})" by simp
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1390
    then show "finite {x. poly p x = 0}"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1391
      by (simp add: k uminus_add_conv_diff Collect_disj_eq
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1392
               del: mult_pCons_left)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1393
  qed
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1394
qed
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1395
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1396
lemma poly_zero:
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1397
  fixes p :: "'a::{idom,ring_char_0} poly"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1398
  shows "poly p = poly 0 \<longleftrightarrow> p = 0"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1399
apply (cases "p = 0", simp_all)
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1400
apply (drule poly_roots_finite)
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1401
apply (auto simp add: infinite_UNIV_char_0)
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1402
done
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1403
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1404
lemma poly_eq_iff:
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1405
  fixes p q :: "'a::{idom,ring_char_0} poly"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1406
  shows "poly p = poly q \<longleftrightarrow> p = q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1407
  using poly_zero [of "p - q"]
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39198
diff changeset
  1408
  by (simp add: fun_eq_iff)
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1409
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1410
29980
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1411
subsection {* Composition of polynomials *}
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1412
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1413
definition
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1414
  pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1415
where
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1416
  "pcompose p q = poly_rec 0 (\<lambda>a _ c. [:a:] + q * c) p"
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1417
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1418
lemma pcompose_0 [simp]: "pcompose 0 q = 0"
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1419
  unfolding pcompose_def by (simp add: poly_rec_0)
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1420
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1421
lemma pcompose_pCons:
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1422
  "pcompose (pCons a p) q = [:a:] + q * pcompose p q"
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1423
  unfolding pcompose_def by (simp add: poly_rec_pCons)
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1424
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1425
lemma poly_pcompose: "poly (pcompose p q) x = poly p (poly q x)"
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1426
  by (induct p) (simp_all add: pcompose_pCons)
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1427
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1428
lemma degree_pcompose_le:
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1429
  "degree (pcompose p q) \<le> degree p * degree q"
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1430
apply (induct p, simp)
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1431
apply (simp add: pcompose_pCons, clarify)
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1432
apply (rule degree_add_le, simp)
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1433
apply (rule order_trans [OF degree_mult_le], simp)
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1434
done
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1435
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1436
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1437
subsection {* Order of polynomial roots *}
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1438
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1439
definition
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1440
  order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat"
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1441
where
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1442
  "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1443
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1444
lemma coeff_linear_power:
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1445
  fixes a :: "'a::comm_semiring_1"
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1446
  shows "coeff ([:a, 1:] ^ n) n = 1"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1447
apply (induct n, simp_all)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1448
apply (subst coeff_eq_0)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1449
apply (auto intro: le_less_trans degree_power_le)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1450
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1451
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1452
lemma degree_linear_power:
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1453
  fixes a :: "'a::comm_semiring_1"
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1454
  shows "degree ([:a, 1:] ^ n) = n"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1455
apply (rule order_antisym)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1456
apply (rule ord_le_eq_trans [OF degree_power_le], simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1457
apply (rule le_degree, simp add: coeff_linear_power)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1458
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1459
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1460
lemma order_1: "[:-a, 1:] ^ order a p dvd p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1461
apply (cases "p = 0", simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1462
apply (cases "order a p", simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1463
apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1464
apply (drule not_less_Least, simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1465
apply (fold order_def, simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1466
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1467
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1468
lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1469
unfolding order_def
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1470
apply (rule LeastI_ex)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1471
apply (rule_tac x="degree p" in exI)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1472
apply (rule notI)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1473
apply (drule (1) dvd_imp_degree_le)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1474
apply (simp only: degree_linear_power)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1475
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1476
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1477
lemma order:
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1478
  "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1479
by (rule conjI [OF order_1 order_2])
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1480
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1481
lemma order_degree:
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1482
  assumes p: "p \<noteq> 0"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1483
  shows "order a p \<le> degree p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1484
proof -
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1485
  have "order a p = degree ([:-a, 1:] ^ order a p)"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1486
    by (simp only: degree_linear_power)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1487
  also have "\<dots> \<le> degree p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1488
    using order_1 p by (rule dvd_imp_degree_le)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1489
  finally show ?thesis .
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1490
qed
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1491
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1492
lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1493
apply (cases "p = 0", simp_all)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1494
apply (rule iffI)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1495
apply (rule ccontr, simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1496
apply (frule order_2 [where a=a], simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1497
apply (simp add: poly_eq_0_iff_dvd)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1498
apply (simp add: poly_eq_0_iff_dvd)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1499
apply (simp only: order_def)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1500
apply (drule not_less_Least, simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1501
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1502
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1503
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1504
subsection {* Configuration of the code generator *}
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1505
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1506
code_datatype "0::'a::zero poly" pCons
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1507
45928
874845660119 adding quickcheck generators in some HOL-Library theories
bulwahn
parents: 45694
diff changeset
  1508
quickcheck_generator poly constructors: "0::'a::zero poly", pCons
874845660119 adding quickcheck generators in some HOL-Library theories
bulwahn
parents: 45694
diff changeset
  1509
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1510
instantiation poly :: ("{zero, equal}") equal
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1511
begin
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1512
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
  1513
definition
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1514
  "HOL.equal (p::'a poly) q \<longleftrightarrow> p = q"
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1515
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1516
instance proof
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1517
qed (rule equal_poly_def)
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1518
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1519
end
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1520
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1521
lemma eq_poly_code [code]:
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1522
  "HOL.equal (0::_ poly) (0::_ poly) \<longleftrightarrow> True"
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1523
  "HOL.equal (0::_ poly) (pCons b q) \<longleftrightarrow> HOL.equal 0 b \<and> HOL.equal 0 q"
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1524
  "HOL.equal (pCons a p) (0::_ poly) \<longleftrightarrow> HOL.equal a 0 \<and> HOL.equal p 0"
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1525
  "HOL.equal (pCons a p) (pCons b q) \<longleftrightarrow> HOL.equal a b \<and> HOL.equal p q"
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1526
  by (simp_all add: equal)
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1527
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1528
lemma [code nbe]:
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1529
  "HOL.equal (p :: _ poly) p \<longleftrightarrow> True"
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1530
  by (fact equal_refl)
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1531
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1532
lemmas coeff_code [code] =
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1533
  coeff_0 coeff_pCons_0 coeff_pCons_Suc
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1534
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1535
lemmas degree_code [code] =
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1536
  degree_0 degree_pCons_eq_if
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1537
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1538
lemmas monom_poly_code [code] =
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1539
  monom_0 monom_Suc
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1540
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1541
lemma add_poly_code [code]:
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1542
  "0 + q = (q :: _ poly)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1543
  "p + 0 = (p :: _ poly)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1544
  "pCons a p + pCons b q = pCons (a + b) (p + q)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1545
by simp_all
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1546
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1547
lemma minus_poly_code [code]:
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1548
  "- 0 = (0 :: _ poly)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1549
  "- pCons a p = pCons (- a) (- p)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1550
by simp_all
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1551
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1552
lemma diff_poly_code [code]:
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1553
  "0 - q = (- q :: _ poly)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1554
  "p - 0 = (p :: _ poly)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1555
  "pCons a p - pCons b q = pCons (a - b) (p - q)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1556
by simp_all
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1557
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1558
lemmas smult_poly_code [code] =
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1559
  smult_0_right smult_pCons
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1560
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1561
lemma mult_poly_code [code]:
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1562
  "0 * q = (0 :: _ poly)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1563
  "pCons a p * q = smult a q + pCons 0 (p * q)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1564
by simp_all
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1565
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1566
lemmas poly_code [code] =
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1567
  poly_0 poly_pCons
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1568
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1569
lemmas synthetic_divmod_code [code] =
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1570
  synthetic_divmod_0 synthetic_divmod_pCons
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1571
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1572
text {* code generator setup for div and mod *}
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1573
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1574
definition
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1575
  pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly"
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1576
where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
  1577
  "pdivmod x y = (x div y, x mod y)"
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1578
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1579
lemma div_poly_code [code]: "x div y = fst (pdivmod x y)"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1580
  unfolding pdivmod_def by simp
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1581
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1582
lemma mod_poly_code [code]: "x mod y = snd (pdivmod x y)"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1583
  unfolding pdivmod_def by simp
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1584
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1585
lemma pdivmod_0 [code]: "pdivmod 0 y = (0, 0)"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1586
  unfolding pdivmod_def by simp
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1587
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1588
lemma pdivmod_pCons [code]:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1589
  "pdivmod (pCons a x) y =
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1590
    (if y = 0 then (0, pCons a x) else
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1591
      (let (q, r) = pdivmod x y;
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1592
           b = coeff (pCons a r) (degree y) / coeff y (degree y)
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1593
        in (pCons b q, pCons a r - smult b y)))"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1594
apply (simp add: pdivmod_def Let_def, safe)
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1595
apply (rule div_poly_eq)
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1596
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1597
apply (rule mod_poly_eq)
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1598
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1599
done
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1600
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1601
lemma poly_gcd_code [code]:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1602
  "poly_gcd x y =
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1603
    (if y = 0 then smult (inverse (coeff x (degree x))) x
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1604
              else poly_gcd y (x mod y))"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1605
  by (simp add: poly_gcd.simps)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1606
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1607
end