src/HOL/Polynomial.thy
author huffman
Sun, 11 Jan 2009 12:05:50 -0800
changeset 29451 5f0cb3fa530d
child 29453 de4f26f59135
permissions -rw-r--r--
new theory of polynomials
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
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     9
imports Plain SetInterval
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
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   103
lemma Poly_nat_case: "f \<in> Poly \<Longrightarrow> nat_case a f \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   104
  unfolding Poly_def by (auto split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   105
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   106
lemma coeff_pCons:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   107
  "coeff (pCons a p) = nat_case a (coeff p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   108
  unfolding pCons_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   109
  by (simp add: Abs_poly_inverse Poly_nat_case coeff)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   110
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   111
lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   112
  by (simp add: coeff_pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   113
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   114
lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   115
  by (simp add: coeff_pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   116
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   117
lemma degree_pCons_le: "degree (pCons a p) \<le> Suc (degree p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   118
by (rule degree_le, simp add: coeff_eq_0 coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   119
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   120
lemma degree_pCons_eq:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   121
  "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   122
apply (rule order_antisym [OF degree_pCons_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   123
apply (rule le_degree, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   124
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   125
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   126
lemma degree_pCons_0: "degree (pCons a 0) = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   127
apply (rule order_antisym [OF _ le0])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   128
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   129
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   130
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   131
lemma degree_pCons_eq_if:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   132
  "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   133
apply (cases "p = 0", simp_all)
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
apply (rule order_antisym [OF degree_pCons_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   137
apply (rule le_degree, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   138
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   139
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   140
lemma pCons_0_0 [simp]: "pCons 0 0 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   141
by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   142
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   143
lemma pCons_eq_iff [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   144
  "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   145
proof (safe)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   146
  assume "pCons a p = pCons b q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   147
  then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   148
  then show "a = b" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   149
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   150
  assume "pCons a p = pCons b q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   151
  then have "\<forall>n. coeff (pCons a p) (Suc n) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   152
                 coeff (pCons b q) (Suc n)" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   153
  then show "p = q" by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   154
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   155
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   156
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
   157
  using pCons_eq_iff [of a p 0 0] by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   158
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   159
lemma Poly_Suc: "f \<in> Poly \<Longrightarrow> (\<lambda>n. f (Suc n)) \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   160
  unfolding Poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   161
  by (clarify, rule_tac x=n in exI, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   162
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   163
lemma pCons_cases [cases type: poly]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   164
  obtains (pCons) a q where "p = pCons a q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   165
proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   166
  show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   167
    by (rule poly_ext)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   168
       (simp add: Abs_poly_inverse Poly_Suc coeff coeff_pCons
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   169
             split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   170
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   171
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   172
lemma pCons_induct [case_names 0 pCons, induct type: poly]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   173
  assumes zero: "P 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   174
  assumes pCons: "\<And>a p. P p \<Longrightarrow> P (pCons a p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   175
  shows "P p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   176
proof (induct p rule: measure_induct_rule [where f=degree])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   177
  case (less p)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   178
  obtain a q where "p = pCons a q" by (rule pCons_cases)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   179
  have "P q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   180
  proof (cases "q = 0")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   181
    case True
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   182
    then show "P q" by (simp add: zero)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   183
  next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   184
    case False
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   185
    then have "degree (pCons a q) = Suc (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   186
      by (rule degree_pCons_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   187
    then have "degree q < degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   188
      using `p = pCons a q` by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   189
    then show "P q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   190
      by (rule less.hyps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   191
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   192
  then have "P (pCons a q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   193
    by (rule pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   194
  then show ?case
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
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   197
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   198
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   199
subsection {* Monomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   200
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   201
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   202
  monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   203
  "monom a m = Abs_poly (\<lambda>n. if m = n then a else 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   204
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   205
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
   206
  unfolding monom_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   207
  by (subst Abs_poly_inverse, auto simp add: Poly_def)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   208
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   209
lemma monom_0: "monom a 0 = pCons a 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   210
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   211
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   212
lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   213
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   214
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   215
lemma monom_eq_0 [simp]: "monom 0 n = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   216
  by (rule poly_ext) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   217
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   218
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   219
  by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   220
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   221
lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   222
  by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   223
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   224
lemma degree_monom_le: "degree (monom a n) \<le> n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   225
  by (rule degree_le, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   226
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   227
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   228
  apply (rule order_antisym [OF degree_monom_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   229
  apply (rule le_degree, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   230
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   231
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   232
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   233
subsection {* Addition and subtraction *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   234
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   235
instantiation poly :: (comm_monoid_add) comm_monoid_add
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   236
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   237
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   238
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   239
  plus_poly_def [code del]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   240
    "p + q = Abs_poly (\<lambda>n. coeff p n + coeff q n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   241
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   242
lemma Poly_add:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   243
  fixes f g :: "nat \<Rightarrow> 'a"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   244
  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
   245
  unfolding Poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   246
  apply (clarify, rename_tac m n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   247
  apply (rule_tac x="max m n" in exI, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   248
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   249
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   250
lemma coeff_add [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   251
  "coeff (p + q) n = coeff p n + coeff q n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   252
  unfolding plus_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   253
  by (simp add: Abs_poly_inverse coeff Poly_add)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   254
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   255
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   256
  fix p q r :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   257
  show "(p + q) + r = p + (q + r)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   258
    by (simp add: expand_poly_eq add_assoc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   259
  show "p + q = q + p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   260
    by (simp add: expand_poly_eq add_commute)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   261
  show "0 + p = p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   262
    by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   263
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   264
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   265
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   266
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   267
instantiation poly :: (ab_group_add) ab_group_add
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   268
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   269
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   270
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   271
  uminus_poly_def [code del]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   272
    "- p = Abs_poly (\<lambda>n. - coeff p n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   273
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   274
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   275
  minus_poly_def [code del]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   276
    "p - q = Abs_poly (\<lambda>n. coeff p n - coeff q n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   277
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   278
lemma Poly_minus:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   279
  fixes f :: "nat \<Rightarrow> 'a"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   280
  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. - f n) \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   281
  unfolding Poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   282
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   283
lemma Poly_diff:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   284
  fixes f g :: "nat \<Rightarrow> 'a"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   285
  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
   286
  unfolding diff_minus by (simp add: Poly_add Poly_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   287
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   288
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   289
  unfolding uminus_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   290
  by (simp add: Abs_poly_inverse coeff Poly_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   291
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   292
lemma coeff_diff [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   293
  "coeff (p - q) n = coeff p n - coeff q n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   294
  unfolding minus_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   295
  by (simp add: Abs_poly_inverse coeff Poly_diff)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   296
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   297
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   298
  fix p q :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   299
  show "- p + p = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   300
    by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   301
  show "p - q = p + - q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   302
    by (simp add: expand_poly_eq diff_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   303
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   304
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   305
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   306
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   307
lemma add_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   308
  "pCons a p + pCons b q = pCons (a + b) (p + q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   309
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   310
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   311
lemma minus_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   312
  "- pCons a p = pCons (- a) (- p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   313
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   314
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   315
lemma diff_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   316
  "pCons a p - pCons b q = pCons (a - b) (p - q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   317
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   318
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   319
lemma degree_add_le: "degree (p + q) \<le> max (degree p) (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   320
  by (rule degree_le, auto simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   321
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   322
lemma degree_add_eq_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   323
  "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   324
  apply (cases "q = 0", simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   325
  apply (rule order_antisym)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   326
  apply (rule ord_le_eq_trans [OF degree_add_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   327
  apply simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   328
  apply (rule le_degree)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   329
  apply (simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   330
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   331
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   332
lemma degree_add_eq_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   333
  "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   334
  using degree_add_eq_right [of q p]
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   335
  by (simp add: add_commute)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   336
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   337
lemma degree_minus [simp]: "degree (- p) = degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   338
  unfolding degree_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   339
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   340
lemma degree_diff_le: "degree (p - q) \<le> max (degree p) (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   341
  using degree_add_le [where p=p and q="-q"]
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   342
  by (simp add: diff_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   343
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   344
lemma add_monom: "monom a n + monom b n = monom (a + b) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   345
  by (rule poly_ext) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   346
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   347
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   348
  by (rule poly_ext) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   349
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   350
lemma minus_monom: "- monom a n = monom (-a) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   351
  by (rule poly_ext) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   352
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   353
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
   354
  by (cases "finite A", induct set: finite, simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   355
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   356
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
   357
  by (rule poly_ext) (simp add: coeff_setsum)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   358
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   359
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   360
subsection {* Multiplication by a constant *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   361
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   362
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   363
  smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   364
  "smult a p = Abs_poly (\<lambda>n. a * coeff p n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   365
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   366
lemma Poly_smult:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   367
  fixes f :: "nat \<Rightarrow> 'a::comm_semiring_0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   368
  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. a * f n) \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   369
  unfolding Poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   370
  by (clarify, rule_tac x=n in exI, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   371
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   372
lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   373
  unfolding smult_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   374
  by (simp add: Abs_poly_inverse Poly_smult coeff)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   375
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   376
lemma degree_smult_le: "degree (smult a p) \<le> degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   377
  by (rule degree_le, simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   378
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   379
lemma smult_smult: "smult a (smult b p) = smult (a * b) p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   380
  by (rule poly_ext, simp add: mult_assoc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   381
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   382
lemma smult_0_right [simp]: "smult a 0 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   383
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   384
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   385
lemma smult_0_left [simp]: "smult 0 p = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   386
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   387
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   388
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   389
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   390
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   391
lemma smult_add_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   392
  "smult a (p + q) = smult a p + smult a q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   393
  by (rule poly_ext, simp add: ring_simps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   394
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   395
lemma smult_add_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   396
  "smult (a + b) p = smult a p + smult b p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   397
  by (rule poly_ext, simp add: ring_simps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   398
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   399
lemma smult_minus_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   400
  "smult (a::'a::comm_ring) (- p) = - smult a p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   401
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   402
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   403
lemma smult_minus_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   404
  "smult (- a::'a::comm_ring) p = - smult a p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   405
  by (rule poly_ext, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   406
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   407
lemma smult_diff_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   408
  "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   409
  by (rule poly_ext, simp add: ring_simps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   410
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   411
lemma smult_diff_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   412
  "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   413
  by (rule poly_ext, simp add: ring_simps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   414
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   415
lemma smult_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   416
  "smult a (pCons b p) = pCons (a * b) (smult a p)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   417
  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   418
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   419
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   420
  by (induct n, simp add: monom_0, simp add: monom_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   421
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   422
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   423
subsection {* Multiplication of polynomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   424
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   425
lemma Poly_mult_lemma:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   426
  fixes f g :: "nat \<Rightarrow> 'a::comm_semiring_0" and m n :: nat
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   427
  assumes "\<forall>i>m. f i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   428
  assumes "\<forall>j>n. g j = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   429
  shows "\<forall>k>m+n. (\<Sum>i\<le>k. f i * g (k-i)) = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   430
proof (clarify)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   431
  fix k :: nat
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   432
  assume "m + n < k"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   433
  show "(\<Sum>i\<le>k. f i * g (k - i)) = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   434
  proof (rule setsum_0' [rule_format])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   435
    fix i :: nat
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   436
    assume "i \<in> {..k}" hence "i \<le> k" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   437
    with `m + n < k` have "m < i \<or> n < k - i" by arith
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   438
    thus "f i * g (k - i) = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   439
      using prems by auto
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   440
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   441
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   442
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   443
lemma Poly_mult:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   444
  fixes f g :: "nat \<Rightarrow> 'a::comm_semiring_0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   445
  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. \<Sum>i\<le>n. f i * g (n-i)) \<in> Poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   446
  unfolding Poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   447
  by (safe, rule exI, rule Poly_mult_lemma)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   448
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   449
lemma poly_mult_assoc_lemma:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   450
  fixes k :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   451
  shows "(\<Sum>j\<le>k. \<Sum>i\<le>j. f i (j - i) (n - j)) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   452
         (\<Sum>j\<le>k. \<Sum>i\<le>k - j. f j i (n - j - i))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   453
proof (induct k)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   454
  case 0 show ?case by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   455
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   456
  case (Suc k) thus ?case
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   457
    by (simp add: Suc_diff_le setsum_addf add_assoc
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   458
             cong: strong_setsum_cong)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   459
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   460
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   461
lemma poly_mult_commute_lemma:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   462
  fixes n :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   463
  shows "(\<Sum>i\<le>n. f i (n - i)) = (\<Sum>i\<le>n. f (n - i) i)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   464
proof (rule setsum_reindex_cong)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   465
  show "inj_on (\<lambda>i. n - i) {..n}"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   466
    by (rule inj_onI) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   467
  show "{..n} = (\<lambda>i. n - i) ` {..n}"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   468
    by (auto, rule_tac x="n - x" in image_eqI, simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   469
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   470
  fix i assume "i \<in> {..n}"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   471
  hence "n - (n - i) = i" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   472
  thus "f (n - i) i = f (n - i) (n - (n - i))" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   473
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   474
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   475
text {* TODO: move to appropriate theory *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   476
lemma setsum_atMost_Suc_shift:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   477
  fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   478
  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
   479
proof (induct n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   480
  case 0 show ?case by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   481
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   482
  case (Suc n) note IH = this
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   483
  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
   484
    by (rule setsum_atMost_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   485
  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
   486
    by (rule IH)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   487
  also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   488
             f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   489
    by (rule add_assoc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   490
  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
   491
    by (rule setsum_atMost_Suc [symmetric])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   492
  finally show ?case .
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   493
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   494
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   495
instantiation poly :: (comm_semiring_0) comm_semiring_0
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   496
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   497
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   498
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   499
  times_poly_def:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   500
    "p * q = Abs_poly (\<lambda>n. \<Sum>i\<le>n. coeff p i * coeff q (n-i))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   501
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   502
lemma coeff_mult:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   503
  "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   504
  unfolding times_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   505
  by (simp add: Abs_poly_inverse coeff Poly_mult)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   506
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   507
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   508
  fix p q r :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   509
  show 0: "0 * p = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   510
    by (simp add: expand_poly_eq coeff_mult)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   511
  show "p * 0 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   512
    by (simp add: expand_poly_eq coeff_mult)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   513
  show "(p + q) * r = p * r + q * r"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   514
    by (simp add: expand_poly_eq coeff_mult left_distrib setsum_addf)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   515
  show "(p * q) * r = p * (q * r)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   516
  proof (rule poly_ext)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   517
    fix n :: nat
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   518
    have "(\<Sum>j\<le>n. \<Sum>i\<le>j. coeff p i * coeff q (j - i) * coeff r (n - j)) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   519
          (\<Sum>j\<le>n. \<Sum>i\<le>n - j. coeff p j * coeff q i * coeff r (n - j - i))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   520
      by (rule poly_mult_assoc_lemma)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   521
    thus "coeff ((p * q) * r) n = coeff (p * (q * r)) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   522
      by (simp add: coeff_mult setsum_right_distrib
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   523
                    setsum_left_distrib mult_assoc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   524
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   525
  show "p * q = q * p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   526
  proof (rule poly_ext)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   527
    fix n :: nat
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   528
    have "(\<Sum>i\<le>n. coeff p i * coeff q (n - i)) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   529
          (\<Sum>i\<le>n. coeff p (n - i) * coeff q i)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   530
      by (rule poly_mult_commute_lemma)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   531
    thus "coeff (p * q) n = coeff (q * p) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   532
      by (simp add: coeff_mult mult_commute)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   533
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   534
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   535
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   536
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   537
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   538
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   539
apply (rule degree_le, simp add: coeff_mult)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   540
apply (rule Poly_mult_lemma)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   541
apply (simp_all add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   542
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   543
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   544
lemma mult_pCons_left [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   545
  "pCons a p * q = smult a q + pCons 0 (p * q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   546
apply (rule poly_ext)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   547
apply (case_tac n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   548
apply (simp add: coeff_mult)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   549
apply (simp add: coeff_mult setsum_atMost_Suc_shift
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   550
            del: setsum_atMost_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   551
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   552
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   553
lemma mult_pCons_right [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   554
  "p * pCons a q = smult a p + pCons 0 (p * q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   555
  using mult_pCons_left [of a q p] by (simp add: mult_commute)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   556
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   557
lemma mult_smult_left: "smult a p * q = smult a (p * q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   558
  by (induct p, simp, simp add: smult_add_right smult_smult)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   559
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   560
lemma mult_smult_right: "p * smult a q = smult a (p * q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   561
  using mult_smult_left [of a q p] by (simp add: mult_commute)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   562
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   563
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   564
  by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   565
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   566
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   567
subsection {* The unit polynomial and exponentiation *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   568
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   569
instantiation poly :: (comm_semiring_1) comm_semiring_1
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   570
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   571
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   572
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   573
  one_poly_def:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   574
    "1 = pCons 1 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   575
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   576
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   577
  fix p :: "'a poly" show "1 * p = p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   578
    unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   579
    by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   580
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   581
  show "0 \<noteq> (1::'a poly)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   582
    unfolding one_poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   583
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   584
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   585
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   586
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   587
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   588
  unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   589
  by (simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   590
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   591
lemma degree_1 [simp]: "degree 1 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   592
  unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   593
  by (rule degree_pCons_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   594
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   595
instantiation poly :: (comm_semiring_1) recpower
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   596
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   597
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   598
primrec power_poly where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   599
  power_poly_0: "(p::'a poly) ^ 0 = 1"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   600
| power_poly_Suc: "(p::'a poly) ^ (Suc n) = p * p ^ n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   601
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   602
instance
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   603
  by default simp_all
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   604
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   605
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   606
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   607
instance poly :: (comm_ring) comm_ring ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   608
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   609
instance poly :: (comm_ring_1) comm_ring_1 ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   610
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   611
instantiation poly :: (comm_ring_1) number_ring
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   612
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   613
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   614
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   615
  "number_of k = (of_int k :: 'a poly)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   616
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   617
instance
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   618
  by default (rule number_of_poly_def)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   619
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   620
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   621
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   622
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   623
subsection {* Polynomials form an integral domain *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   624
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   625
lemma coeff_mult_degree_sum:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   626
  "coeff (p * q) (degree p + degree q) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   627
   coeff p (degree p) * coeff q (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   628
 apply (simp add: coeff_mult)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   629
 apply (subst setsum_diff1' [where a="degree p"])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   630
   apply simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   631
  apply simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   632
 apply (subst setsum_0' [rule_format])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   633
  apply clarsimp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   634
  apply (subgoal_tac "degree p < a \<or> degree q < degree p + degree q - a")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   635
   apply (force simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   636
  apply arith
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   637
 apply simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   638
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   639
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   640
instance poly :: (idom) idom
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   641
proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   642
  fix p q :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   643
  assume "p \<noteq> 0" and "q \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   644
  have "coeff (p * q) (degree p + degree q) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   645
        coeff p (degree p) * coeff q (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   646
    by (rule coeff_mult_degree_sum)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   647
  also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   648
    using `p \<noteq> 0` and `q \<noteq> 0` by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   649
  finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   650
  thus "p * q \<noteq> 0" by (simp add: expand_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   651
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   652
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   653
lemma degree_mult_eq:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   654
  fixes p q :: "'a::idom poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   655
  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
   656
apply (rule order_antisym [OF degree_mult_le le_degree])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   657
apply (simp add: coeff_mult_degree_sum)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   658
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   659
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   660
lemma dvd_imp_degree_le:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   661
  fixes p q :: "'a::idom poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   662
  shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   663
  by (erule dvdE, simp add: degree_mult_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   664
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   665
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   666
subsection {* Long division of polynomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   667
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   668
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   669
  divmod_poly_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   670
where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   671
  "divmod_poly_rel x y q r \<longleftrightarrow>
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   672
    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
   673
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   674
lemma divmod_poly_rel_0:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   675
  "divmod_poly_rel 0 y 0 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   676
  unfolding divmod_poly_rel_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   677
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   678
lemma divmod_poly_rel_by_0:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   679
  "divmod_poly_rel x 0 0 x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   680
  unfolding divmod_poly_rel_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   681
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   682
lemma eq_zero_or_degree_less:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   683
  assumes "degree p \<le> n" and "coeff p n = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   684
  shows "p = 0 \<or> degree p < n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   685
proof (cases n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   686
  case 0
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   687
  with `degree p \<le> n` and `coeff p n = 0`
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   688
  have "coeff p (degree p) = 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   689
  then have "p = 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   690
  then show ?thesis ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   691
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   692
  case (Suc m)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   693
  have "\<forall>i>n. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   694
    using `degree p \<le> n` by (simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   695
  then have "\<forall>i\<ge>n. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   696
    using `coeff p n = 0` by (simp add: le_less)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   697
  then have "\<forall>i>m. coeff p i = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   698
    using `n = Suc m` by (simp add: less_eq_Suc_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   699
  then have "degree p \<le> m"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   700
    by (rule degree_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   701
  then have "degree p < n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   702
    using `n = Suc m` by (simp add: less_Suc_eq_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   703
  then show ?thesis ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   704
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   705
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   706
lemma divmod_poly_rel_pCons:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   707
  assumes rel: "divmod_poly_rel x y q r"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   708
  assumes y: "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   709
  assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   710
  shows "divmod_poly_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   711
    (is "divmod_poly_rel ?x y ?q ?r")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   712
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   713
  have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   714
    using assms unfolding divmod_poly_rel_def by simp_all
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   715
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   716
  have 1: "?x = ?q * y + ?r"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   717
    using b x by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   718
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   719
  have 2: "?r = 0 \<or> degree ?r < degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   720
  proof (rule eq_zero_or_degree_less)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   721
    have "degree ?r \<le> max (degree (pCons a r)) (degree (smult b y))"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   722
      by (rule degree_diff_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   723
    also have "\<dots> \<le> degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   724
    proof (rule min_max.le_supI)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   725
      show "degree (pCons a r) \<le> degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   726
        using r by (auto simp add: degree_pCons_eq_if)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   727
      show "degree (smult b y) \<le> degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   728
        by (rule degree_smult_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   729
    qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   730
    finally show "degree ?r \<le> degree y" .
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   731
  next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   732
    show "coeff ?r (degree y) = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   733
      using `y \<noteq> 0` unfolding b by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   734
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   735
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   736
  from 1 2 show ?thesis
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   737
    unfolding divmod_poly_rel_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   738
    using `y \<noteq> 0` by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   739
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   740
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   741
lemma divmod_poly_rel_exists: "\<exists>q r. divmod_poly_rel x y q r"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   742
apply (cases "y = 0")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   743
apply (fast intro!: divmod_poly_rel_by_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   744
apply (induct x)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   745
apply (fast intro!: divmod_poly_rel_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   746
apply (fast intro!: divmod_poly_rel_pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   747
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   748
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   749
lemma divmod_poly_rel_unique:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   750
  assumes 1: "divmod_poly_rel x y q1 r1"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   751
  assumes 2: "divmod_poly_rel x y q2 r2"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   752
  shows "q1 = q2 \<and> r1 = r2"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   753
proof (cases "y = 0")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   754
  assume "y = 0" with assms show ?thesis
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   755
    by (simp add: divmod_poly_rel_def)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   756
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   757
  assume [simp]: "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   758
  from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   759
    unfolding divmod_poly_rel_def by simp_all
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   760
  from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   761
    unfolding divmod_poly_rel_def by simp_all
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   762
  from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   763
    by (simp add: ring_simps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   764
  from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   765
    by (auto intro: le_less_trans [OF degree_diff_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   766
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   767
  show "q1 = q2 \<and> r1 = r2"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   768
  proof (rule ccontr)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   769
    assume "\<not> (q1 = q2 \<and> r1 = r2)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   770
    with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   771
    with r3 have "degree (r2 - r1) < degree y" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   772
    also have "degree y \<le> degree (q1 - q2) + degree y" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   773
    also have "\<dots> = degree ((q1 - q2) * y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   774
      using `q1 \<noteq> q2` by (simp add: degree_mult_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   775
    also have "\<dots> = degree (r2 - r1)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   776
      using q3 by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   777
    finally have "degree (r2 - r1) < degree (r2 - r1)" .
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   778
    then show "False" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   779
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   780
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   781
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   782
lemmas divmod_poly_rel_unique_div =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   783
  divmod_poly_rel_unique [THEN conjunct1, standard]
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   784
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   785
lemmas divmod_poly_rel_unique_mod =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   786
  divmod_poly_rel_unique [THEN conjunct2, standard]
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   787
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   788
instantiation poly :: (field) ring_div
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   789
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   790
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   791
definition div_poly where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   792
  [code del]: "x div y = (THE q. \<exists>r. divmod_poly_rel x y q r)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   793
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   794
definition mod_poly where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   795
  [code del]: "x mod y = (THE r. \<exists>q. divmod_poly_rel x y q r)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   796
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   797
lemma div_poly_eq:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   798
  "divmod_poly_rel x y q r \<Longrightarrow> x div y = q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   799
unfolding div_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   800
by (fast elim: divmod_poly_rel_unique_div)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   801
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   802
lemma mod_poly_eq:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   803
  "divmod_poly_rel x y q r \<Longrightarrow> x mod y = r"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   804
unfolding mod_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   805
by (fast elim: divmod_poly_rel_unique_mod)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   806
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   807
lemma divmod_poly_rel:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   808
  "divmod_poly_rel x y (x div y) (x mod y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   809
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   810
  from divmod_poly_rel_exists
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   811
    obtain q r where "divmod_poly_rel x y q r" by fast
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   812
  thus ?thesis
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   813
    by (simp add: div_poly_eq mod_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   814
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   815
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   816
instance proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   817
  fix x y :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   818
  show "x div y * y + x mod y = x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   819
    using divmod_poly_rel [of x y]
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   820
    by (simp add: divmod_poly_rel_def)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   821
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   822
  fix x :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   823
  have "divmod_poly_rel x 0 0 x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   824
    by (rule divmod_poly_rel_by_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   825
  thus "x div 0 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   826
    by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   827
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   828
  fix y :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   829
  have "divmod_poly_rel 0 y 0 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   830
    by (rule divmod_poly_rel_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   831
  thus "0 div y = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   832
    by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   833
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   834
  fix x y z :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   835
  assume "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   836
  hence "divmod_poly_rel (x + z * y) y (z + x div y) (x mod y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   837
    using divmod_poly_rel [of x y]
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   838
    by (simp add: divmod_poly_rel_def left_distrib)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   839
  thus "(x + z * y) div y = z + x div y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   840
    by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   841
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   842
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   843
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   844
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   845
lemma degree_mod_less:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   846
  "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   847
  using divmod_poly_rel [of x y]
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   848
  unfolding divmod_poly_rel_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   849
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   850
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   851
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   852
  assume "degree x < degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   853
  hence "divmod_poly_rel x y 0 x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   854
    by (simp add: divmod_poly_rel_def)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   855
  thus "x div y = 0" by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   856
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   857
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   858
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   859
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   860
  assume "degree x < degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   861
  hence "divmod_poly_rel x y 0 x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   862
    by (simp add: divmod_poly_rel_def)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   863
  thus "x mod y = x" by (rule mod_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   864
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   865
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   866
lemma mod_pCons:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   867
  fixes a and x
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   868
  assumes y: "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   869
  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
   870
  shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   871
unfolding b
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   872
apply (rule mod_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   873
apply (rule divmod_poly_rel_pCons [OF divmod_poly_rel y refl])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   874
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   875
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   876
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   877
subsection {* Evaluation of polynomials *}
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   878
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   879
definition
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   880
  poly :: "'a::{comm_semiring_1,recpower} poly \<Rightarrow> 'a \<Rightarrow> 'a" where
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   881
  "poly p = (\<lambda>x. \<Sum>n\<le>degree p. coeff p n * x ^ n)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   882
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   883
lemma poly_0 [simp]: "poly 0 x = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   884
  unfolding poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   885
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   886
lemma poly_pCons [simp]: "poly (pCons a p) x = a + x * poly p x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   887
  unfolding poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   888
  by (simp add: degree_pCons_eq_if setsum_atMost_Suc_shift power_Suc
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   889
                setsum_left_distrib setsum_right_distrib mult_ac
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   890
           del: setsum_atMost_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   891
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   892
lemma poly_1 [simp]: "poly 1 x = 1"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   893
  unfolding one_poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   894
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   895
lemma poly_monom: "poly (monom a n) x = a * x ^ n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   896
  by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   897
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   898
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   899
  apply (induct p arbitrary: q, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   900
  apply (case_tac q, simp, simp add: ring_simps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   901
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   902
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   903
lemma poly_minus [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   904
  fixes x :: "'a::{comm_ring_1,recpower}"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   905
  shows "poly (- p) x = - poly p x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   906
  by (induct p, simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   907
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   908
lemma poly_diff [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   909
  fixes x :: "'a::{comm_ring_1,recpower}"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   910
  shows "poly (p - q) x = poly p x - poly q x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   911
  by (simp add: diff_minus)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   912
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   913
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
   914
  by (cases "finite A", induct set: finite, simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   915
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   916
lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   917
  by (induct p, simp, simp add: ring_simps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   918
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   919
lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   920
  by (induct p, simp_all, simp add: ring_simps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   921
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   922
end