src/HOL/Polynomial.thy
author huffman
Fri, 13 Feb 2009 14:12:00 -0800
changeset 29904 856f16a3b436
parent 29878 06efd6e731c6
child 29977 d76b830366bc
permissions -rw-r--r--
add class cancel_comm_monoid_add
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     1
(*  Title:      HOL/Polynomial.thy
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     2
    Author:     Brian Huffman
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     3
                Based on an earlier development by Clemens Ballarin
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
29654
24e73987bfe2 Plain, Main form meeting points in import hierarchy
haftmann
parents: 29540
diff changeset
     9
imports Plain SetInterval 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
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    14
typedef (Poly) 'a poly = "{f::nat \<Rightarrow> 'a::zero. \<exists>n. \<forall>i>n. f i = 0}"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    15
  morphisms coeff Abs_poly
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    16
  by auto
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    17
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    18
lemma expand_poly_eq: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    19
by (simp add: coeff_inject [symmetric] expand_fun_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    20
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    21
lemma poly_ext: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    22
by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    23
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    24
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    25
subsection {* Degree of a polynomial *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    26
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    27
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    28
  degree :: "'a::zero poly \<Rightarrow> nat" where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    29
  "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    30
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    31
lemma coeff_eq_0: "degree p < n \<Longrightarrow> coeff p n = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    32
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    33
  have "coeff p \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    34
    by (rule coeff)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    35
  hence "\<exists>n. \<forall>i>n. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    36
    unfolding Poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    37
  hence "\<forall>i>degree p. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    38
    unfolding degree_def by (rule LeastI_ex)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    39
  moreover assume "degree p < n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    40
  ultimately show ?thesis by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    41
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    42
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    43
lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    44
  by (erule contrapos_np, rule coeff_eq_0, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    45
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    46
lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    47
  unfolding degree_def by (erule Least_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    48
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    49
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
    50
  unfolding degree_def by (drule not_less_Least, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    51
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    52
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    53
subsection {* The zero polynomial *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    54
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    55
instantiation poly :: (zero) zero
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    56
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    57
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    58
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    59
  zero_poly_def: "0 = Abs_poly (\<lambda>n. 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    60
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    61
instance ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    62
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    63
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    64
lemma coeff_0 [simp]: "coeff 0 n = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    65
  unfolding zero_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    66
  by (simp add: Abs_poly_inverse Poly_def)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    67
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    68
lemma degree_0 [simp]: "degree 0 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    69
  by (rule order_antisym [OF degree_le le0]) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    70
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    71
lemma leading_coeff_neq_0:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    72
  assumes "p \<noteq> 0" shows "coeff p (degree p) \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    73
proof (cases "degree p")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    74
  case 0
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    75
  from `p \<noteq> 0` have "\<exists>n. coeff p n \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    76
    by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    77
  then obtain n where "coeff p n \<noteq> 0" ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    78
  hence "n \<le> degree p" by (rule le_degree)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    79
  with `coeff p n \<noteq> 0` and `degree p = 0`
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    80
  show "coeff p (degree p) \<noteq> 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    81
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    82
  case (Suc n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    83
  from `degree p = Suc n` have "n < degree p" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    84
  hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    85
  then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    86
  from `degree p = Suc n` and `n < i` have "degree p \<le> i" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    87
  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
    88
  finally have "degree p = i" .
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    89
  with `coeff p i \<noteq> 0` show "coeff p (degree p) \<noteq> 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    90
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    91
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    92
lemma leading_coeff_0_iff [simp]: "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    93
  by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    94
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    95
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    96
subsection {* List-style constructor for polynomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    97
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    98
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    99
  pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   100
where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   101
  [code del]: "pCons a p = Abs_poly (nat_case a (coeff p))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   102
29455
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   103
syntax
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   104
  "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   105
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   106
translations
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   107
  "[:x, xs:]" == "CONST pCons x [:xs:]"
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   108
  "[:x:]" == "CONST pCons x 0"
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   109
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   110
lemma Poly_nat_case: "f \<in> Poly \<Longrightarrow> nat_case a f \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   111
  unfolding Poly_def by (auto split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   112
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   113
lemma coeff_pCons:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   114
  "coeff (pCons a p) = nat_case a (coeff p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   115
  unfolding pCons_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   116
  by (simp add: Abs_poly_inverse Poly_nat_case coeff)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   117
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   118
lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   119
  by (simp add: coeff_pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   120
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   121
lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   122
  by (simp add: coeff_pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   123
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   124
lemma degree_pCons_le: "degree (pCons a p) \<le> Suc (degree p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   125
by (rule degree_le, simp add: coeff_eq_0 coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   126
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   127
lemma degree_pCons_eq:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   128
  "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   129
apply (rule order_antisym [OF degree_pCons_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   130
apply (rule le_degree, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   131
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   132
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   133
lemma degree_pCons_0: "degree (pCons a 0) = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   134
apply (rule order_antisym [OF _ le0])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   135
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   136
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   137
29460
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
   138
lemma degree_pCons_eq_if [simp]:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   139
  "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   140
apply (cases "p = 0", simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   141
apply (rule order_antisym [OF _ le0])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   142
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   143
apply (rule order_antisym [OF degree_pCons_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   144
apply (rule le_degree, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   145
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   146
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   147
lemma pCons_0_0 [simp]: "pCons 0 0 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   148
by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   149
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   150
lemma pCons_eq_iff [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   151
  "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   152
proof (safe)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   153
  assume "pCons a p = pCons b q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   154
  then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   155
  then show "a = b" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   156
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   157
  assume "pCons a p = pCons b q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   158
  then have "\<forall>n. coeff (pCons a p) (Suc n) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   159
                 coeff (pCons b q) (Suc n)" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   160
  then show "p = q" by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   161
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   162
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   163
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
   164
  using pCons_eq_iff [of a p 0 0] by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   165
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   166
lemma Poly_Suc: "f \<in> Poly \<Longrightarrow> (\<lambda>n. f (Suc n)) \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   167
  unfolding Poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   168
  by (clarify, rule_tac x=n in exI, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   169
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   170
lemma pCons_cases [cases type: poly]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   171
  obtains (pCons) a q where "p = pCons a q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   172
proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   173
  show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   174
    by (rule poly_ext)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   175
       (simp add: Abs_poly_inverse Poly_Suc coeff coeff_pCons
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   176
             split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   177
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   178
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   179
lemma pCons_induct [case_names 0 pCons, induct type: poly]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   180
  assumes zero: "P 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   181
  assumes pCons: "\<And>a p. P p \<Longrightarrow> P (pCons a p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   182
  shows "P p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   183
proof (induct p rule: measure_induct_rule [where f=degree])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   184
  case (less p)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   185
  obtain a q where "p = pCons a q" by (rule pCons_cases)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   186
  have "P q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   187
  proof (cases "q = 0")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   188
    case True
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   189
    then show "P q" by (simp add: zero)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   190
  next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   191
    case False
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   192
    then have "degree (pCons a q) = Suc (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   193
      by (rule degree_pCons_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   194
    then have "degree q < degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   195
      using `p = pCons a q` by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   196
    then show "P q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   197
      by (rule less.hyps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   198
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   199
  then have "P (pCons a q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   200
    by (rule pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   201
  then show ?case
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   202
    using `p = pCons a q` by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   203
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   204
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   205
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   206
subsection {* Recursion combinator for polynomials *}
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   207
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   208
function
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   209
  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
   210
where
29475
c06d1b0a970f declare more definitions [code del]
huffman
parents: 29474
diff changeset
   211
  poly_rec_pCons_eq_if [simp del, code del]:
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   212
    "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
   213
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
   214
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   215
termination poly_rec
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   216
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
   217
   (simp add: degree_pCons_eq)
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   218
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   219
lemma poly_rec_0:
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   220
  "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
   221
  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
   222
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   223
lemma poly_rec_pCons:
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   224
  "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
   225
  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
   226
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   227
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   228
subsection {* Monomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   229
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   230
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   231
  monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   232
  "monom a m = Abs_poly (\<lambda>n. if m = n then a else 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   233
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   234
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
   235
  unfolding monom_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   236
  by (subst Abs_poly_inverse, auto simp add: Poly_def)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   237
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   238
lemma monom_0: "monom a 0 = pCons a 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   239
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   240
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   241
lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   242
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   243
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   244
lemma monom_eq_0 [simp]: "monom 0 n = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   245
  by (rule poly_ext) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   246
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   247
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   248
  by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   249
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   250
lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   251
  by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   252
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   253
lemma degree_monom_le: "degree (monom a n) \<le> n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   254
  by (rule degree_le, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   255
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   256
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   257
  apply (rule order_antisym [OF degree_monom_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   258
  apply (rule le_degree, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   259
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   260
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   261
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   262
subsection {* Addition and subtraction *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   263
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   264
instantiation poly :: (comm_monoid_add) comm_monoid_add
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   265
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   266
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   267
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   268
  plus_poly_def [code del]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   269
    "p + q = Abs_poly (\<lambda>n. coeff p n + coeff q n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   270
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   271
lemma Poly_add:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   272
  fixes f g :: "nat \<Rightarrow> 'a"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   273
  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
   274
  unfolding Poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   275
  apply (clarify, rename_tac m n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   276
  apply (rule_tac x="max m n" in exI, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   277
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   278
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   279
lemma coeff_add [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   280
  "coeff (p + q) n = coeff p n + coeff q n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   281
  unfolding plus_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   282
  by (simp add: Abs_poly_inverse coeff Poly_add)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   283
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   284
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   285
  fix p q r :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   286
  show "(p + q) + r = p + (q + r)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   287
    by (simp add: expand_poly_eq add_assoc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   288
  show "p + q = q + p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   289
    by (simp add: expand_poly_eq add_commute)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   290
  show "0 + p = p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   291
    by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   292
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   293
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   294
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   295
29904
856f16a3b436 add class cancel_comm_monoid_add
huffman
parents: 29878
diff changeset
   296
instance poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   297
proof
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   298
  fix p q r :: "'a poly"
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   299
  assume "p + q = p + r" thus "q = r"
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   300
    by (simp add: expand_poly_eq)
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   301
qed
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   302
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   303
instantiation poly :: (ab_group_add) ab_group_add
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   304
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   305
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   306
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   307
  uminus_poly_def [code del]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   308
    "- p = Abs_poly (\<lambda>n. - coeff p n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   309
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   310
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   311
  minus_poly_def [code del]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   312
    "p - q = Abs_poly (\<lambda>n. coeff p n - coeff q n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   313
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   314
lemma Poly_minus:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   315
  fixes f :: "nat \<Rightarrow> 'a"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   316
  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. - f n) \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   317
  unfolding Poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   318
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   319
lemma Poly_diff:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   320
  fixes f g :: "nat \<Rightarrow> 'a"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   321
  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
   322
  unfolding diff_minus by (simp add: Poly_add Poly_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   323
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   324
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   325
  unfolding uminus_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   326
  by (simp add: Abs_poly_inverse coeff Poly_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   327
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   328
lemma coeff_diff [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   329
  "coeff (p - q) n = coeff p n - coeff q n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   330
  unfolding minus_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   331
  by (simp add: Abs_poly_inverse coeff Poly_diff)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   332
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   333
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   334
  fix p q :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   335
  show "- p + p = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   336
    by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   337
  show "p - q = p + - q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   338
    by (simp add: expand_poly_eq diff_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   339
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   340
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   341
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   342
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   343
lemma add_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   344
  "pCons a p + pCons b q = pCons (a + b) (p + q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   345
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   346
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   347
lemma minus_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   348
  "- pCons a p = pCons (- a) (- p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   349
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   350
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   351
lemma diff_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   352
  "pCons a p - pCons b q = pCons (a - b) (p - q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   353
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   354
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   355
lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   356
  by (rule degree_le, auto simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   357
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   358
lemma degree_add_le:
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   359
  "\<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
   360
  by (auto intro: order_trans degree_add_le_max)
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   361
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   362
lemma degree_add_less:
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   363
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   364
  by (auto intro: le_less_trans degree_add_le_max)
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   365
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   366
lemma degree_add_eq_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   367
  "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   368
  apply (cases "q = 0", simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   369
  apply (rule order_antisym)
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   370
  apply (simp add: degree_add_le)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   371
  apply (rule le_degree)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   372
  apply (simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   373
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   374
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   375
lemma degree_add_eq_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   376
  "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   377
  using degree_add_eq_right [of q p]
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   378
  by (simp add: add_commute)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   379
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   380
lemma degree_minus [simp]: "degree (- p) = degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   381
  unfolding degree_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   382
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   383
lemma degree_diff_le_max: "degree (p - q) \<le> max (degree p) (degree q)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   384
  using degree_add_le [where p=p and q="-q"]
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   385
  by (simp add: diff_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   386
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   387
lemma degree_diff_le:
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   388
  "\<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
   389
  by (simp add: diff_minus degree_add_le)
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   390
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   391
lemma degree_diff_less:
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   392
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p - q) < n"
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   393
  by (simp add: diff_minus degree_add_less)
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   394
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   395
lemma add_monom: "monom a n + monom b n = monom (a + b) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   396
  by (rule poly_ext) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   397
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   398
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   399
  by (rule poly_ext) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   400
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   401
lemma minus_monom: "- monom a n = monom (-a) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   402
  by (rule poly_ext) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   403
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   404
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
   405
  by (cases "finite A", induct set: finite, simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   406
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   407
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
   408
  by (rule poly_ext) (simp add: coeff_setsum)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   409
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   410
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   411
subsection {* Multiplication by a constant *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   412
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   413
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   414
  smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   415
  "smult a p = Abs_poly (\<lambda>n. a * coeff p n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   416
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   417
lemma Poly_smult:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   418
  fixes f :: "nat \<Rightarrow> 'a::comm_semiring_0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   419
  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. a * f n) \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   420
  unfolding Poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   421
  by (clarify, rule_tac x=n in exI, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   422
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   423
lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   424
  unfolding smult_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   425
  by (simp add: Abs_poly_inverse Poly_smult coeff)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   426
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   427
lemma degree_smult_le: "degree (smult a p) \<le> degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   428
  by (rule degree_le, simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   429
29472
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   430
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   431
  by (rule poly_ext, simp add: mult_assoc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   432
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   433
lemma smult_0_right [simp]: "smult a 0 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   434
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   435
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   436
lemma smult_0_left [simp]: "smult 0 p = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   437
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   438
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   439
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   440
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   441
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   442
lemma smult_add_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   443
  "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
   444
  by (rule poly_ext, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   445
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   446
lemma smult_add_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   447
  "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
   448
  by (rule poly_ext, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   449
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
   450
lemma smult_minus_right [simp]:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   451
  "smult (a::'a::comm_ring) (- p) = - smult a p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   452
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   453
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
   454
lemma smult_minus_left [simp]:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   455
  "smult (- a::'a::comm_ring) p = - smult a p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   456
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   457
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   458
lemma smult_diff_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   459
  "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
   460
  by (rule poly_ext, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   461
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   462
lemma smult_diff_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   463
  "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
   464
  by (rule poly_ext, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   465
29472
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   466
lemmas smult_distribs =
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   467
  smult_add_left smult_add_right
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   468
  smult_diff_left smult_diff_right
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   469
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   470
lemma smult_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   471
  "smult a (pCons b p) = pCons (a * b) (smult a p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   472
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   473
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   474
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   475
  by (induct n, simp add: monom_0, simp add: monom_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   476
29659
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   477
lemma degree_smult_eq [simp]:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   478
  fixes a :: "'a::idom"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   479
  shows "degree (smult a p) = (if a = 0 then 0 else degree p)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   480
  by (cases "a = 0", simp, simp add: degree_def)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   481
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   482
lemma smult_eq_0_iff [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 "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   485
  by (simp add: expand_poly_eq)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   486
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   487
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   488
subsection {* Multiplication of polynomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   489
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   490
text {* TODO: move to SetInterval.thy *}
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   491
lemma setsum_atMost_Suc_shift:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   492
  fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   493
  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
   494
proof (induct n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   495
  case 0 show ?case by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   496
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   497
  case (Suc n) note IH = this
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   498
  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
   499
    by (rule setsum_atMost_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   500
  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
   501
    by (rule IH)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   502
  also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   503
             f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   504
    by (rule add_assoc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   505
  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
   506
    by (rule setsum_atMost_Suc [symmetric])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   507
  finally show ?case .
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   508
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   509
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   510
instantiation poly :: (comm_semiring_0) comm_semiring_0
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   511
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   512
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   513
definition
29475
c06d1b0a970f declare more definitions [code del]
huffman
parents: 29474
diff changeset
   514
  times_poly_def [code del]:
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   515
    "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
   516
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   517
lemma mult_poly_0_left: "(0::'a poly) * q = 0"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   518
  unfolding times_poly_def by (simp add: poly_rec_0)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   519
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   520
lemma mult_pCons_left [simp]:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   521
  "pCons a p * q = smult a q + pCons 0 (p * q)"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   522
  unfolding times_poly_def by (simp add: poly_rec_pCons)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   523
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   524
lemma mult_poly_0_right: "p * (0::'a poly) = 0"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   525
  by (induct p, simp add: mult_poly_0_left, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   526
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   527
lemma mult_pCons_right [simp]:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   528
  "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
   529
  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
   530
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   531
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   532
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   533
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
   534
  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
   535
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   536
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
   537
  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
   538
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   539
lemma mult_poly_add_left:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   540
  fixes p q r :: "'a poly"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   541
  shows "(p + q) * r = p * r + q * r"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   542
  by (induct r, simp add: mult_poly_0,
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
   543
                simp add: smult_distribs algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   544
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   545
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   546
  fix p q r :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   547
  show 0: "0 * p = 0"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   548
    by (rule mult_poly_0_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   549
  show "p * 0 = 0"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   550
    by (rule mult_poly_0_right)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   551
  show "(p + q) * r = p * r + q * r"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   552
    by (rule mult_poly_add_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   553
  show "(p * q) * r = p * (q * r)"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   554
    by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   555
  show "p * q = q * p"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   556
    by (induct p, simp add: mult_poly_0, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   557
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   558
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   559
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   560
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   561
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   562
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   563
lemma coeff_mult:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   564
  "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
   565
proof (induct p arbitrary: n)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   566
  case 0 show ?case by simp
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   567
next
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   568
  case (pCons a p n) thus ?case
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   569
    by (cases n, simp, simp add: setsum_atMost_Suc_shift
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   570
                            del: setsum_atMost_Suc)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   571
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   572
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   573
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   574
apply (rule degree_le)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   575
apply (induct p)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   576
apply simp
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   577
apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   578
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   579
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   580
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   581
  by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   582
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   583
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   584
subsection {* The unit polynomial and exponentiation *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   585
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   586
instantiation poly :: (comm_semiring_1) comm_semiring_1
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   587
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   588
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   589
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   590
  one_poly_def:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   591
    "1 = pCons 1 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   592
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   593
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   594
  fix p :: "'a poly" show "1 * p = p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   595
    unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   596
    by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   597
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   598
  show "0 \<noteq> (1::'a poly)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   599
    unfolding one_poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   600
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   601
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   602
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   603
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   604
instance poly :: (comm_semiring_1_cancel) comm_semiring_1_cancel ..
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   605
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   606
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   607
  unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   608
  by (simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   609
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   610
lemma degree_1 [simp]: "degree 1 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   611
  unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   612
  by (rule degree_pCons_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   613
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   614
instantiation poly :: (comm_semiring_1) recpower
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   615
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   616
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   617
primrec power_poly where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   618
  power_poly_0: "(p::'a poly) ^ 0 = 1"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   619
| power_poly_Suc: "(p::'a poly) ^ (Suc n) = p * p ^ n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   620
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   621
instance
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   622
  by default simp_all
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   623
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   624
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   625
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   626
instance poly :: (comm_ring) comm_ring ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   627
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   628
instance poly :: (comm_ring_1) comm_ring_1 ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   629
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   630
instantiation poly :: (comm_ring_1) number_ring
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   631
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   632
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   633
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   634
  "number_of k = (of_int k :: 'a poly)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   635
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   636
instance
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   637
  by default (rule number_of_poly_def)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   638
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   639
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   640
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   641
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   642
subsection {* Polynomials form an integral domain *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   643
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   644
lemma coeff_mult_degree_sum:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   645
  "coeff (p * q) (degree p + degree q) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   646
   coeff p (degree p) * coeff q (degree q)"
29471
6a46a13ce1f9 simplify proof of coeff_mult_degree_sum
huffman
parents: 29462
diff changeset
   647
  by (induct p, simp, simp add: coeff_eq_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   648
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   649
instance poly :: (idom) idom
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   650
proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   651
  fix p q :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   652
  assume "p \<noteq> 0" and "q \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   653
  have "coeff (p * q) (degree p + degree q) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   654
        coeff p (degree p) * coeff q (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   655
    by (rule coeff_mult_degree_sum)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   656
  also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   657
    using `p \<noteq> 0` and `q \<noteq> 0` by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   658
  finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   659
  thus "p * q \<noteq> 0" by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   660
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   661
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   662
lemma degree_mult_eq:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   663
  fixes p q :: "'a::idom poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   664
  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
   665
apply (rule order_antisym [OF degree_mult_le le_degree])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   666
apply (simp add: coeff_mult_degree_sum)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   667
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   668
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   669
lemma dvd_imp_degree_le:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   670
  fixes p q :: "'a::idom poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   671
  shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   672
  by (erule dvdE, simp add: degree_mult_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   673
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   674
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   675
subsection {* Polynomials form an ordered integral domain *}
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   676
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   677
definition
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   678
  pos_poly :: "'a::ordered_idom poly \<Rightarrow> bool"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   679
where
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   680
  "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   681
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   682
lemma pos_poly_pCons:
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   683
  "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
   684
  unfolding pos_poly_def by simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   685
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   686
lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   687
  unfolding pos_poly_def by simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   688
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   689
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
   690
  apply (induct p arbitrary: q, simp)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   691
  apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   692
  done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   693
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   694
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
   695
  unfolding pos_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   696
  apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   697
  apply (simp add: degree_mult_eq coeff_mult_degree_sum mult_pos_pos)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   698
  apply auto
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   699
  done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   700
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   701
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
   702
by (induct p) (auto simp add: pos_poly_pCons)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   703
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   704
instantiation poly :: (ordered_idom) ordered_idom
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   705
begin
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   706
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   707
definition
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   708
  [code del]:
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   709
    "x < y \<longleftrightarrow> pos_poly (y - x)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   710
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   711
definition
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   712
  [code del]:
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   713
    "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   714
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   715
definition
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   716
  [code del]:
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   717
    "abs (x::'a poly) = (if x < 0 then - x else x)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   718
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   719
definition
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   720
  [code del]:
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   721
    "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   722
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   723
instance proof
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   724
  fix x y :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   725
  show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   726
    unfolding less_eq_poly_def less_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   727
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   728
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   729
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   730
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   731
    done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   732
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   733
  fix x :: "'a poly" show "x \<le> x"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   734
    unfolding less_eq_poly_def by simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   735
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   736
  fix x y z :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   737
  assume "x \<le> y" and "y \<le> z" thus "x \<le> z"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   738
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   739
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   740
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   741
    apply (simp add: algebra_simps)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   742
    done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   743
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   744
  fix x y :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   745
  assume "x \<le> y" and "y \<le> x" thus "x = y"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   746
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   747
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   748
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   749
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   750
    done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   751
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   752
  fix x y z :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   753
  assume "x \<le> y" thus "z + x \<le> z + y"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   754
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   755
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   756
    apply (simp add: algebra_simps)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   757
    done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   758
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   759
  fix x y :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   760
  show "x \<le> y \<or> y \<le> x"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   761
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   762
    using pos_poly_total [of "x - y"]
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   763
    by auto
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   764
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   765
  fix x y z :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   766
  assume "x < y" and "0 < z"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   767
  thus "z * x < z * y"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   768
    unfolding less_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   769
    by (simp add: right_diff_distrib [symmetric] pos_poly_mult)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   770
next
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   771
  fix x :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   772
  show "\<bar>x\<bar> = (if x < 0 then - x else x)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   773
    by (rule abs_poly_def)
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 :: "'a poly"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   776
  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
   777
    by (rule sgn_poly_def)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   778
qed
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   779
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   780
end
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   781
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   782
text {* TODO: Simplification rules for comparisons *}
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   783
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
   784
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   785
subsection {* Long division of polynomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   786
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   787
definition
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   788
  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
   789
where
29475
c06d1b0a970f declare more definitions [code del]
huffman
parents: 29474
diff changeset
   790
  [code del]:
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   791
  "pdivmod_rel x y q r \<longleftrightarrow>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   792
    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
   793
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   794
lemma pdivmod_rel_0:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   795
  "pdivmod_rel 0 y 0 0"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   796
  unfolding pdivmod_rel_def by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   797
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   798
lemma pdivmod_rel_by_0:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   799
  "pdivmod_rel x 0 0 x"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   800
  unfolding pdivmod_rel_def by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   801
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   802
lemma eq_zero_or_degree_less:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   803
  assumes "degree p \<le> n" and "coeff p n = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   804
  shows "p = 0 \<or> degree p < n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   805
proof (cases n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   806
  case 0
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   807
  with `degree p \<le> n` and `coeff p n = 0`
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   808
  have "coeff p (degree p) = 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   809
  then have "p = 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   810
  then show ?thesis ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   811
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   812
  case (Suc m)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   813
  have "\<forall>i>n. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   814
    using `degree p \<le> n` by (simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   815
  then have "\<forall>i\<ge>n. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   816
    using `coeff p n = 0` by (simp add: le_less)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   817
  then have "\<forall>i>m. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   818
    using `n = Suc m` by (simp add: less_eq_Suc_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   819
  then have "degree p \<le> m"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   820
    by (rule degree_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   821
  then have "degree p < n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   822
    using `n = Suc m` by (simp add: less_Suc_eq_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   823
  then show ?thesis ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   824
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   825
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   826
lemma pdivmod_rel_pCons:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   827
  assumes rel: "pdivmod_rel x y q r"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   828
  assumes y: "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   829
  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
   830
  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
   831
    (is "pdivmod_rel ?x y ?q ?r")
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   832
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   833
  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
   834
    using assms unfolding pdivmod_rel_def by simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   835
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   836
  have 1: "?x = ?q * y + ?r"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   837
    using b x by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   838
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   839
  have 2: "?r = 0 \<or> degree ?r < degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   840
  proof (rule eq_zero_or_degree_less)
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   841
    show "degree ?r \<le> degree y"
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   842
    proof (rule degree_diff_le)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   843
      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
   844
        using r by auto
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   845
      show "degree (smult b y) \<le> degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   846
        by (rule degree_smult_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   847
    qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   848
  next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   849
    show "coeff ?r (degree y) = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   850
      using `y \<noteq> 0` unfolding b by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   851
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   852
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   853
  from 1 2 show ?thesis
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   854
    unfolding pdivmod_rel_def
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   855
    using `y \<noteq> 0` by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   856
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   857
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   858
lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   859
apply (cases "y = 0")
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   860
apply (fast intro!: pdivmod_rel_by_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   861
apply (induct x)
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   862
apply (fast intro!: pdivmod_rel_0)
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   863
apply (fast intro!: pdivmod_rel_pCons)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   864
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   865
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   866
lemma pdivmod_rel_unique:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   867
  assumes 1: "pdivmod_rel x y q1 r1"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   868
  assumes 2: "pdivmod_rel x y q2 r2"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   869
  shows "q1 = q2 \<and> r1 = r2"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   870
proof (cases "y = 0")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   871
  assume "y = 0" with assms show ?thesis
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   872
    by (simp add: pdivmod_rel_def)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   873
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   874
  assume [simp]: "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   875
  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
   876
    unfolding pdivmod_rel_def by simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   877
  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
   878
    unfolding pdivmod_rel_def by simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   879
  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
   880
    by (simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   881
  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
   882
    by (auto intro: degree_diff_less)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   883
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   884
  show "q1 = q2 \<and> r1 = r2"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   885
  proof (rule ccontr)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   886
    assume "\<not> (q1 = q2 \<and> r1 = r2)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   887
    with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   888
    with r3 have "degree (r2 - r1) < degree y" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   889
    also have "degree y \<le> degree (q1 - q2) + degree y" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   890
    also have "\<dots> = degree ((q1 - q2) * y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   891
      using `q1 \<noteq> q2` by (simp add: degree_mult_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   892
    also have "\<dots> = degree (r2 - r1)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   893
      using q3 by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   894
    finally have "degree (r2 - r1) < degree (r2 - r1)" .
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   895
    then show "False" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   896
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   897
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   898
29660
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
   899
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
   900
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
   901
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
   902
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
   903
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
   904
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   905
lemmas pdivmod_rel_unique_div =
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   906
  pdivmod_rel_unique [THEN conjunct1, standard]
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   907
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   908
lemmas pdivmod_rel_unique_mod =
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   909
  pdivmod_rel_unique [THEN conjunct2, standard]
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   910
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   911
instantiation poly :: (field) ring_div
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   912
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   913
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   914
definition div_poly where
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   915
  [code del]: "x div y = (THE q. \<exists>r. pdivmod_rel x y q r)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   916
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   917
definition mod_poly where
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   918
  [code del]: "x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   919
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   920
lemma div_poly_eq:
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   921
  "pdivmod_rel x y q r \<Longrightarrow> x div y = q"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   922
unfolding div_poly_def
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   923
by (fast elim: pdivmod_rel_unique_div)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   924
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   925
lemma mod_poly_eq:
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   926
  "pdivmod_rel x y q r \<Longrightarrow> x mod y = r"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   927
unfolding mod_poly_def
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   928
by (fast elim: pdivmod_rel_unique_mod)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   929
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   930
lemma pdivmod_rel:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   931
  "pdivmod_rel x y (x div y) (x mod y)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   932
proof -
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   933
  from pdivmod_rel_exists
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   934
    obtain q r where "pdivmod_rel x y q r" by fast
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   935
  thus ?thesis
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   936
    by (simp add: div_poly_eq mod_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   937
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   938
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   939
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   940
  fix x y :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   941
  show "x div y * y + x mod y = x"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   942
    using pdivmod_rel [of x y]
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   943
    by (simp add: pdivmod_rel_def)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   944
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   945
  fix x :: "'a poly"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   946
  have "pdivmod_rel x 0 0 x"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   947
    by (rule pdivmod_rel_by_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   948
  thus "x div 0 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   949
    by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   950
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   951
  fix y :: "'a poly"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   952
  have "pdivmod_rel 0 y 0 0"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   953
    by (rule pdivmod_rel_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   954
  thus "0 div y = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   955
    by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   956
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   957
  fix x y z :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   958
  assume "y \<noteq> 0"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   959
  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
   960
    using pdivmod_rel [of x y]
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   961
    by (simp add: pdivmod_rel_def left_distrib)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   962
  thus "(x + z * y) div y = z + x div y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   963
    by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   964
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   965
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   966
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   967
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   968
lemma degree_mod_less:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   969
  "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
   970
  using pdivmod_rel [of x y]
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   971
  unfolding pdivmod_rel_def by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   972
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   973
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   974
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   975
  assume "degree x < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   976
  hence "pdivmod_rel x y 0 x"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   977
    by (simp add: pdivmod_rel_def)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   978
  thus "x div y = 0" by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   979
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   980
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   981
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   982
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   983
  assume "degree x < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   984
  hence "pdivmod_rel x y 0 x"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
   985
    by (simp add: pdivmod_rel_def)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   986
  thus "x mod y = x" by (rule mod_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   987
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   988
29659
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   989
lemma pdivmod_rel_smult_left:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   990
  "pdivmod_rel x y q r
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   991
    \<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   992
  unfolding pdivmod_rel_def by (simp add: smult_add_right)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   993
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   994
lemma div_smult_left: "(smult a x) div y = smult a (x div y)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   995
  by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   996
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   997
lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   998
  by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   999
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1000
lemma pdivmod_rel_smult_right:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1001
  "\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk>
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1002
    \<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1003
  unfolding pdivmod_rel_def by simp
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1004
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1005
lemma div_smult_right:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1006
  "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
  1007
  by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1008
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1009
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
  1010
  by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1011
29660
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1012
lemma pdivmod_rel_mult:
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1013
  "\<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
  1014
    \<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1015
apply (cases "z = 0", simp add: pdivmod_rel_def)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1016
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
  1017
apply (cases "r = 0")
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1018
apply (cases "r' = 0")
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1019
apply (simp add: pdivmod_rel_def)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1020
apply (simp add: pdivmod_rel_def ring_simps degree_mult_eq)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1021
apply (cases "r' = 0")
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1022
apply (simp add: pdivmod_rel_def degree_mult_eq)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1023
apply (simp add: pdivmod_rel_def ring_simps)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1024
apply (simp add: degree_mult_eq degree_add_less)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1025
done
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1026
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1027
lemma poly_div_mult_right:
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1028
  fixes x y z :: "'a::field poly"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1029
  shows "x div (y * z) = (x div y) div z"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1030
  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
  1031
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1032
lemma poly_mod_mult_right:
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1033
  fixes x y z :: "'a::field poly"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1034
  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
  1035
  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
  1036
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1037
lemma mod_pCons:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1038
  fixes a and x
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1039
  assumes y: "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1040
  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
  1041
  shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1042
unfolding b
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1043
apply (rule mod_poly_eq)
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1044
apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl])
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1045
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1046
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1047
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1048
subsection {* Evaluation of polynomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1049
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1050
definition
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1051
  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
  1052
  "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
  1053
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1054
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
  1055
  unfolding poly_def by (simp add: poly_rec_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1056
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1057
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
  1058
  unfolding poly_def by (simp add: poly_rec_pCons)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1059
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1060
lemma poly_1 [simp]: "poly 1 x = 1"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1061
  unfolding one_poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1062
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1063
lemma poly_monom:
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1064
  fixes a x :: "'a::{comm_semiring_1,recpower}"
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1065
  shows "poly (monom a n) x = a * x ^ n"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1066
  by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1067
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1068
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1069
  apply (induct p arbitrary: q, simp)
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
  1070
  apply (case_tac q, simp, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1071
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1072
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1073
lemma poly_minus [simp]:
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1074
  fixes x :: "'a::comm_ring"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1075
  shows "poly (- p) x = - poly p x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1076
  by (induct p, simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1077
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1078
lemma poly_diff [simp]:
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
  1079
  fixes x :: "'a::comm_ring"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1080
  shows "poly (p - q) x = poly p x - poly q x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1081
  by (simp add: diff_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1082
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1083
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
  1084
  by (cases "finite A", induct set: finite, simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1085
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1086
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
  1087
  by (induct p, simp, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1088
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1089
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
  1090
  by (induct p, simp_all, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1091
29462
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1092
lemma poly_power [simp]:
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1093
  fixes p :: "'a::{comm_semiring_1,recpower} poly"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1094
  shows "poly (p ^ n) x = poly p x ^ n"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1095
  by (induct n, simp, simp add: power_Suc)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1096
29456
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1097
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1098
subsection {* Synthetic division *}
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1099
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1100
definition
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1101
  synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1102
where [code del]:
29456
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1103
  "synthetic_divmod p c =
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1104
    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
  1105
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1106
definition
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1107
  synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1108
where
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1109
  "synthetic_div p c = fst (synthetic_divmod p c)"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1110
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1111
lemma synthetic_divmod_0 [simp]:
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1112
  "synthetic_divmod 0 c = (0, 0)"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1113
  unfolding synthetic_divmod_def
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1114
  by (simp add: poly_rec_0)
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1115
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1116
lemma synthetic_divmod_pCons [simp]:
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1117
  "synthetic_divmod (pCons a p) c =
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1118
    (\<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
  1119
  unfolding synthetic_divmod_def
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1120
  by (simp add: poly_rec_pCons)
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1121
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1122
lemma snd_synthetic_divmod: "snd (synthetic_divmod p c) = poly p c"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1123
  by (induct p, simp, simp add: split_def)
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1124
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1125
lemma synthetic_div_0 [simp]: "synthetic_div 0 c = 0"
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1126
  unfolding synthetic_div_def by simp
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1127
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1128
lemma synthetic_div_pCons [simp]:
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1129
  "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
  1130
  unfolding synthetic_div_def
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1131
  by (simp add: split_def snd_synthetic_divmod)
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1132
29460
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1133
lemma synthetic_div_eq_0_iff:
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1134
  "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
  1135
  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
  1136
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1137
lemma degree_synthetic_div:
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1138
  "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
  1139
  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
  1140
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1141
lemma synthetic_div_correct:
29456
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1142
  "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
  1143
  by (induct p) simp_all
3f8b85444512 add synthetic division algorithm for polynomials
huffman
parents: 29455
diff changeset
  1144
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1145
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
  1146
by (induct p arbitrary: a) simp_all
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1147
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1148
lemma synthetic_div_unique:
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1149
  "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
  1150
apply (induct p arbitrary: q r)
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1151
apply (simp, frule synthetic_div_unique_lemma, simp)
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1152
apply (case_tac q, force)
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1153
done
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1154
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1155
lemma synthetic_div_correct':
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1156
  fixes c :: "'a::comm_ring_1"
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1157
  shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1158
  using synthetic_div_correct [of p c]
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
  1159
  by (simp add: algebra_simps)
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
  1160
29460
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1161
lemma poly_eq_0_iff_dvd:
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1162
  fixes c :: "'a::idom"
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1163
  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
  1164
proof
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1165
  assume "poly p c = 0"
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1166
  with synthetic_div_correct' [of c p]
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1167
  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
  1168
  then show "[:-c, 1:] dvd p" ..
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1169
next
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1170
  assume "[:-c, 1:] dvd p"
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1171
  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
  1172
  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
  1173
qed
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1174
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1175
lemma dvd_iff_poly_eq_0:
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1176
  fixes c :: "'a::idom"
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1177
  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
  1178
  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
  1179
29462
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1180
lemma poly_roots_finite:
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1181
  fixes p :: "'a::idom poly"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1182
  shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1183
proof (induct n \<equiv> "degree p" arbitrary: p)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1184
  case (0 p)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1185
  then obtain a where "a \<noteq> 0" and "p = [:a:]"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1186
    by (cases p, simp split: if_splits)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1187
  then show "finite {x. poly p x = 0}" by simp
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1188
next
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1189
  case (Suc n p)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1190
  show "finite {x. poly p x = 0}"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1191
  proof (cases "\<exists>x. poly p x = 0")
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1192
    case False
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1193
    then show "finite {x. poly p x = 0}" by simp
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1194
  next
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1195
    case True
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1196
    then obtain a where "poly p a = 0" ..
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1197
    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
  1198
    then obtain k where k: "p = [:-a, 1:] * k" ..
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1199
    with `p \<noteq> 0` have "k \<noteq> 0" by auto
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1200
    with k have "degree p = Suc (degree k)"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1201
      by (simp add: degree_mult_eq del: mult_pCons_left)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1202
    with `Suc n = degree p` have "n = degree k" by simp
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1203
    with `k \<noteq> 0` have "finite {x. poly k x = 0}" by (rule Suc.hyps)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1204
    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
  1205
    then show "finite {x. poly p x = 0}"
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1206
      by (simp add: k uminus_add_conv_diff Collect_disj_eq
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1207
               del: mult_pCons_left)
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1208
  qed
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1209
qed
dc97c6188a7a add lemmas poly_power, poly_roots_finite
huffman
parents: 29460
diff changeset
  1210
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1211
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1212
subsection {* Configuration of the code generator *}
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1213
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1214
code_datatype "0::'a::zero poly" pCons
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1215
29480
4e08ee896e81 declare pCons_0_0 [code post]
huffman
parents: 29478
diff changeset
  1216
declare pCons_0_0 [code post]
4e08ee896e81 declare pCons_0_0 [code post]
huffman
parents: 29478
diff changeset
  1217
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1218
instantiation poly :: ("{zero,eq}") eq
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1219
begin
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1220
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1221
definition [code del]:
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1222
  "eq_class.eq (p::'a poly) q \<longleftrightarrow> p = q"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1223
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1224
instance
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1225
  by default (rule eq_poly_def)
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1226
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1227
end
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1228
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1229
lemma eq_poly_code [code]:
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1230
  "eq_class.eq (0::_ poly) (0::_ poly) \<longleftrightarrow> True"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1231
  "eq_class.eq (0::_ poly) (pCons b q) \<longleftrightarrow> eq_class.eq 0 b \<and> eq_class.eq 0 q"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1232
  "eq_class.eq (pCons a p) (0::_ poly) \<longleftrightarrow> eq_class.eq a 0 \<and> eq_class.eq p 0"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1233
  "eq_class.eq (pCons a p) (pCons b q) \<longleftrightarrow> eq_class.eq a b \<and> eq_class.eq p q"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1234
unfolding eq by simp_all
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1235
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1236
lemmas coeff_code [code] =
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1237
  coeff_0 coeff_pCons_0 coeff_pCons_Suc
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1238
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1239
lemmas degree_code [code] =
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1240
  degree_0 degree_pCons_eq_if
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1241
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1242
lemmas monom_poly_code [code] =
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1243
  monom_0 monom_Suc
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1244
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1245
lemma add_poly_code [code]:
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1246
  "0 + q = (q :: _ poly)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1247
  "p + 0 = (p :: _ poly)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1248
  "pCons a p + pCons b q = pCons (a + b) (p + q)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1249
by simp_all
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1250
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1251
lemma minus_poly_code [code]:
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1252
  "- 0 = (0 :: _ poly)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1253
  "- pCons a p = pCons (- a) (- p)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1254
by simp_all
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1255
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1256
lemma diff_poly_code [code]:
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1257
  "0 - q = (- q :: _ poly)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1258
  "p - 0 = (p :: _ poly)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1259
  "pCons a p - pCons b q = pCons (a - b) (p - q)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1260
by simp_all
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1261
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1262
lemmas smult_poly_code [code] =
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1263
  smult_0_right smult_pCons
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1264
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1265
lemma mult_poly_code [code]:
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1266
  "0 * q = (0 :: _ poly)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1267
  "pCons a p * q = smult a q + pCons 0 (p * q)"
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1268
by simp_all
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1269
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1270
lemmas poly_code [code] =
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1271
  poly_0 poly_pCons
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1272
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1273
lemmas synthetic_divmod_code [code] =
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1274
  synthetic_divmod_0 synthetic_divmod_pCons
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1275
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1276
text {* code generator setup for div and mod *}
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1277
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1278
definition
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1279
  pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly"
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1280
where
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1281
  [code del]: "pdivmod x y = (x div y, x mod y)"
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1282
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1283
lemma div_poly_code [code]: "x div y = fst (pdivmod x y)"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1284
  unfolding pdivmod_def by simp
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1285
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1286
lemma mod_poly_code [code]: "x mod y = snd (pdivmod x y)"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1287
  unfolding pdivmod_def by simp
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1288
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1289
lemma pdivmod_0 [code]: "pdivmod 0 y = (0, 0)"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1290
  unfolding pdivmod_def by simp
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1291
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1292
lemma pdivmod_pCons [code]:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1293
  "pdivmod (pCons a x) y =
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1294
    (if y = 0 then (0, pCons a x) else
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1295
      (let (q, r) = pdivmod x y;
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1296
           b = coeff (pCons a r) (degree y) / coeff y (degree y)
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1297
        in (pCons b q, pCons a r - smult b y)))"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1298
apply (simp add: pdivmod_def Let_def, safe)
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1299
apply (rule div_poly_eq)
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1300
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1301
apply (rule mod_poly_eq)
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1302
apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1303
done
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1304
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1305
end