src/HOL/Library/Polynomial.thy
author haftmann
Wed, 17 Feb 2016 21:51:58 +0100
changeset 62351 fd049b54ad68
parent 62128 3201ddb00097
child 62352 35a9e1cbb5b3
permissions -rw-r--r--
gcd instances for poly
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 39302
diff changeset
     1
(*  Title:      HOL/Library/Polynomial.thy
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     2
    Author:     Brian Huffman
41959
b460124855b8 tuned headers;
wenzelm
parents: 39302
diff changeset
     3
    Author:     Clemens Ballarin
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
     4
    Author:     Florian Haftmann
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     5
*)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     6
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
     7
section \<open>Polynomials as type over a ring structure\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     8
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
     9
theory Polynomial
60685
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
    10
imports Main "~~/src/HOL/GCD" "~~/src/HOL/Library/More_List" "~~/src/HOL/Library/Infinite_Set"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    11
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    12
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
    13
subsection \<open>Auxiliary: operations for lists (later) representing coefficients\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    14
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    15
definition cCons :: "'a::zero \<Rightarrow> 'a list \<Rightarrow> 'a list"  (infixr "##" 65)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    16
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    17
  "x ## xs = (if xs = [] \<and> x = 0 then [] else x # xs)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    18
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    19
lemma cCons_0_Nil_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    20
  "0 ## [] = []"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    21
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    22
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    23
lemma cCons_Cons_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    24
  "x ## y # ys = x # y # ys"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    25
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    26
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    27
lemma cCons_append_Cons_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    28
  "x ## xs @ y # ys = x # xs @ y # ys"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    29
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    30
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    31
lemma cCons_not_0_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    32
  "x \<noteq> 0 \<Longrightarrow> x ## xs = x # xs"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    33
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    34
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    35
lemma strip_while_not_0_Cons_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    36
  "strip_while (\<lambda>x. x = 0) (x # xs) = x ## strip_while (\<lambda>x. x = 0) xs"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    37
proof (cases "x = 0")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    38
  case False then show ?thesis by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    39
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    40
  case True show ?thesis
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    41
  proof (induct xs rule: rev_induct)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    42
    case Nil with True show ?case by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    43
  next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    44
    case (snoc y ys) then show ?case
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    45
      by (cases "y = 0") (simp_all add: append_Cons [symmetric] del: append_Cons)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    46
  qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    47
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    48
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    49
lemma tl_cCons [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    50
  "tl (x ## xs) = xs"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    51
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    52
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61260
diff changeset
    53
subsection \<open>Definition of type \<open>poly\<close>\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    54
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 60686
diff changeset
    55
typedef (overloaded) 'a poly = "{f :: nat \<Rightarrow> 'a::zero. \<forall>\<^sub>\<infinity> n. f n = 0}"
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
    56
  morphisms coeff Abs_poly by (auto intro!: ALL_MOST)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    57
59487
adaa430fc0f7 default abstypes and default abstract equations make technical (no_code) annotation superfluous
haftmann
parents: 58881
diff changeset
    58
setup_lifting type_definition_poly
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    59
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    60
lemma poly_eq_iff: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)"
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45605
diff changeset
    61
  by (simp add: coeff_inject [symmetric] fun_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    62
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    63
lemma poly_eqI: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    64
  by (simp add: poly_eq_iff)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    65
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
    66
lemma MOST_coeff_eq_0: "\<forall>\<^sub>\<infinity> n. coeff p n = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    67
  using coeff [of p] by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    68
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    69
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
    70
subsection \<open>Degree of a polynomial\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    71
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    72
definition degree :: "'a::zero poly \<Rightarrow> nat"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    73
where
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    74
  "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    75
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    76
lemma coeff_eq_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    77
  assumes "degree p < n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    78
  shows "coeff p n = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    79
proof -
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
    80
  have "\<exists>n. \<forall>i>n. coeff p i = 0"
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
    81
    using MOST_coeff_eq_0 by (simp add: MOST_nat)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    82
  then have "\<forall>i>degree p. coeff p i = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    83
    unfolding degree_def by (rule LeastI_ex)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
    84
  with assms show ?thesis by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    85
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    86
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    87
lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    88
  by (erule contrapos_np, rule coeff_eq_0, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    89
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    90
lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    91
  unfolding degree_def by (erule Least_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    92
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    93
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
    94
  unfolding degree_def by (drule not_less_Least, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    95
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    96
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
    97
subsection \<open>The zero polynomial\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    98
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
    99
instantiation poly :: (zero) zero
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   100
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   101
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   102
lift_definition zero_poly :: "'a poly"
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
   103
  is "\<lambda>_. 0" by (rule MOST_I) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   104
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   105
instance ..
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   106
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   107
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   108
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   109
lemma coeff_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   110
  "coeff 0 n = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   111
  by transfer rule
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   112
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   113
lemma degree_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   114
  "degree 0 = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   115
  by (rule order_antisym [OF degree_le le0]) simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   116
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   117
lemma leading_coeff_neq_0:
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   118
  assumes "p \<noteq> 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   119
  shows "coeff p (degree p) \<noteq> 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   120
proof (cases "degree p")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   121
  case 0
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   122
  from \<open>p \<noteq> 0\<close> have "\<exists>n. coeff p n \<noteq> 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   123
    by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   124
  then obtain n where "coeff p n \<noteq> 0" ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   125
  hence "n \<le> degree p" by (rule le_degree)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   126
  with \<open>coeff p n \<noteq> 0\<close> and \<open>degree p = 0\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   127
  show "coeff p (degree p) \<noteq> 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   128
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   129
  case (Suc n)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   130
  from \<open>degree p = Suc n\<close> have "n < degree p" by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   131
  hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   132
  then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   133
  from \<open>degree p = Suc n\<close> and \<open>n < i\<close> have "degree p \<le> i" by simp
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   134
  also from \<open>coeff p i \<noteq> 0\<close> have "i \<le> degree p" by (rule le_degree)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   135
  finally have "degree p = i" .
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   136
  with \<open>coeff p i \<noteq> 0\<close> show "coeff p (degree p) \<noteq> 0" by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   137
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   138
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   139
lemma leading_coeff_0_iff [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   140
  "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   141
  by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   142
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   143
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   144
subsection \<open>List-style constructor for polynomials\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   145
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   146
lift_definition pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
55415
05f5fdb8d093 renamed 'nat_{case,rec}' to '{case,rec}_nat'
blanchet
parents: 54856
diff changeset
   147
  is "\<lambda>a p. case_nat a (coeff p)"
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
   148
  by (rule MOST_SucD) (simp add: MOST_coeff_eq_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   149
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   150
lemmas coeff_pCons = pCons.rep_eq
29455
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   151
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   152
lemma coeff_pCons_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   153
  "coeff (pCons a p) 0 = a"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   154
  by transfer simp
29455
0139c9a01ca4 add list-style syntax for pCons
huffman
parents: 29454
diff changeset
   155
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   156
lemma coeff_pCons_Suc [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   157
  "coeff (pCons a p) (Suc n) = coeff p n"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   158
  by (simp add: coeff_pCons)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   159
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   160
lemma degree_pCons_le:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   161
  "degree (pCons a p) \<le> Suc (degree p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   162
  by (rule degree_le) (simp add: coeff_eq_0 coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   163
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   164
lemma degree_pCons_eq:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   165
  "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   166
  apply (rule order_antisym [OF degree_pCons_le])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   167
  apply (rule le_degree, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   168
  done
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   169
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   170
lemma degree_pCons_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   171
  "degree (pCons a 0) = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   172
  apply (rule order_antisym [OF _ le0])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   173
  apply (rule degree_le, simp add: coeff_pCons split: nat.split)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   174
  done
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   175
29460
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
   176
lemma degree_pCons_eq_if [simp]:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   177
  "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   178
  apply (cases "p = 0", simp_all)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   179
  apply (rule order_antisym [OF _ le0])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   180
  apply (rule degree_le, simp add: coeff_pCons split: nat.split)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   181
  apply (rule order_antisym [OF degree_pCons_le])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   182
  apply (rule le_degree, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   183
  done
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   184
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   185
lemma pCons_0_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   186
  "pCons 0 0 = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   187
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   188
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   189
lemma pCons_eq_iff [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   190
  "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   191
proof safe
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   192
  assume "pCons a p = pCons b q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   193
  then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   194
  then show "a = b" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   195
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   196
  assume "pCons a p = pCons b q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   197
  then have "\<forall>n. coeff (pCons a p) (Suc n) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   198
                 coeff (pCons b q) (Suc n)" by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   199
  then show "p = q" by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   200
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   201
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   202
lemma pCons_eq_0_iff [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   203
  "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   204
  using pCons_eq_iff [of a p 0 0] by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   205
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   206
lemma pCons_cases [cases type: poly]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   207
  obtains (pCons) a q where "p = pCons a q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   208
proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   209
  show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   210
    by transfer
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
   211
       (simp_all add: MOST_inj[where f=Suc and P="\<lambda>n. p n = 0" for p] fun_eq_iff Abs_poly_inverse
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
   212
                 split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   213
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   214
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   215
lemma pCons_induct [case_names 0 pCons, induct type: poly]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   216
  assumes zero: "P 0"
54856
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   217
  assumes pCons: "\<And>a p. a \<noteq> 0 \<or> p \<noteq> 0 \<Longrightarrow> P p \<Longrightarrow> P (pCons a p)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   218
  shows "P p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   219
proof (induct p rule: measure_induct_rule [where f=degree])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   220
  case (less p)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   221
  obtain a q where "p = pCons a q" by (rule pCons_cases)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   222
  have "P q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   223
  proof (cases "q = 0")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   224
    case True
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   225
    then show "P q" by (simp add: zero)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   226
  next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   227
    case False
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   228
    then have "degree (pCons a q) = Suc (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   229
      by (rule degree_pCons_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   230
    then have "degree q < degree p"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   231
      using \<open>p = pCons a q\<close> by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   232
    then show "P q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   233
      by (rule less.hyps)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   234
  qed
54856
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   235
  have "P (pCons a q)"
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   236
  proof (cases "a \<noteq> 0 \<or> q \<noteq> 0")
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   237
    case True
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   238
    with \<open>P q\<close> show ?thesis by (auto intro: pCons)
54856
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   239
  next
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   240
    case False
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   241
    with zero show ?thesis by simp
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   242
  qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   243
  then show ?case
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   244
    using \<open>p = pCons a q\<close> by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   245
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   246
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   247
lemma degree_eq_zeroE:
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   248
  fixes p :: "'a::zero poly"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   249
  assumes "degree p = 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   250
  obtains a where "p = pCons a 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   251
proof -
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   252
  obtain a q where p: "p = pCons a q" by (cases p)
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   253
  with assms have "q = 0" by (cases "q = 0") simp_all
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   254
  with p have "p = pCons a 0" by simp
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   255
  with that show thesis .
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   256
qed
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   257
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   258
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   259
subsection \<open>List-style syntax for polynomials\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   260
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   261
syntax
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   262
  "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   263
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   264
translations
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   265
  "[:x, xs:]" == "CONST pCons x [:xs:]"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   266
  "[:x:]" == "CONST pCons x 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   267
  "[:x:]" <= "CONST pCons x (_constrain 0 t)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   268
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   269
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   270
subsection \<open>Representation of polynomials by lists of coefficients\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   271
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   272
primrec Poly :: "'a::zero list \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   273
where
54855
d700d054d022 convenient printing of polynomial values
haftmann
parents: 54489
diff changeset
   274
  [code_post]: "Poly [] = 0"
d700d054d022 convenient printing of polynomial values
haftmann
parents: 54489
diff changeset
   275
| [code_post]: "Poly (a # as) = pCons a (Poly as)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   276
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   277
lemma Poly_replicate_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   278
  "Poly (replicate n 0) = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   279
  by (induct n) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   280
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   281
lemma Poly_eq_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   282
  "Poly as = 0 \<longleftrightarrow> (\<exists>n. as = replicate n 0)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   283
  by (induct as) (auto simp add: Cons_replicate_eq)
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   284
  
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   285
lemma degree_Poly: "degree (Poly xs) \<le> length xs"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   286
  by (induction xs) simp_all
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   287
  
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   288
definition coeffs :: "'a poly \<Rightarrow> 'a::zero list"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   289
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   290
  "coeffs p = (if p = 0 then [] else map (\<lambda>i. coeff p i) [0 ..< Suc (degree p)])"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   291
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   292
lemma coeffs_eq_Nil [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   293
  "coeffs p = [] \<longleftrightarrow> p = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   294
  by (simp add: coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   295
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   296
lemma not_0_coeffs_not_Nil:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   297
  "p \<noteq> 0 \<Longrightarrow> coeffs p \<noteq> []"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   298
  by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   299
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   300
lemma coeffs_0_eq_Nil [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   301
  "coeffs 0 = []"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   302
  by simp
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   303
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   304
lemma coeffs_pCons_eq_cCons [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   305
  "coeffs (pCons a p) = a ## coeffs p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   306
proof -
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   307
  { fix ms :: "nat list" and f :: "nat \<Rightarrow> 'a" and x :: "'a"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   308
    assume "\<forall>m\<in>set ms. m > 0"
55415
05f5fdb8d093 renamed 'nat_{case,rec}' to '{case,rec}_nat'
blanchet
parents: 54856
diff changeset
   309
    then have "map (case_nat x f) ms = map f (map (\<lambda>n. n - 1) ms)"
58199
5fbe474b5da8 explicit theory with additional, less commonly used list operations
haftmann
parents: 57862
diff changeset
   310
      by (induct ms) (auto split: nat.split)
5fbe474b5da8 explicit theory with additional, less commonly used list operations
haftmann
parents: 57862
diff changeset
   311
  }
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   312
  note * = this
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   313
  show ?thesis
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   314
    by (simp add: coeffs_def * upt_conv_Cons coeff_pCons map_decr_upt del: upt_Suc)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   315
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   316
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   317
lemma length_coeffs: "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = degree p + 1"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   318
  by (simp add: coeffs_def)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   319
  
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   320
lemma coeffs_nth:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   321
  assumes "p \<noteq> 0" "n \<le> degree p"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   322
  shows   "coeffs p ! n = coeff p n"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   323
  using assms unfolding coeffs_def by (auto simp del: upt_Suc)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   324
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   325
lemma not_0_cCons_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   326
  "p \<noteq> 0 \<Longrightarrow> a ## coeffs p = a # coeffs p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   327
  by (simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   328
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   329
lemma Poly_coeffs [simp, code abstype]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   330
  "Poly (coeffs p) = p"
54856
356b4c0a2061 more general induction rule;
haftmann
parents: 54855
diff changeset
   331
  by (induct p) auto
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   332
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   333
lemma coeffs_Poly [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   334
  "coeffs (Poly as) = strip_while (HOL.eq 0) as"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   335
proof (induct as)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   336
  case Nil then show ?case by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   337
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   338
  case (Cons a as)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   339
  have "(\<forall>n. as \<noteq> replicate n 0) \<longleftrightarrow> (\<exists>a\<in>set as. a \<noteq> 0)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   340
    using replicate_length_same [of as 0] by (auto dest: sym [of _ as])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   341
  with Cons show ?case by auto
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   342
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   343
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   344
lemma last_coeffs_not_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   345
  "p \<noteq> 0 \<Longrightarrow> last (coeffs p) \<noteq> 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   346
  by (induct p) (auto simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   347
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   348
lemma strip_while_coeffs [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   349
  "strip_while (HOL.eq 0) (coeffs p) = coeffs p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   350
  by (cases "p = 0") (auto dest: last_coeffs_not_0 intro: strip_while_not_last)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   351
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   352
lemma coeffs_eq_iff:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   353
  "p = q \<longleftrightarrow> coeffs p = coeffs q" (is "?P \<longleftrightarrow> ?Q")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   354
proof
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   355
  assume ?P then show ?Q by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   356
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   357
  assume ?Q
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   358
  then have "Poly (coeffs p) = Poly (coeffs q)" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   359
  then show ?P by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   360
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   361
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   362
lemma coeff_Poly_eq:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   363
  "coeff (Poly xs) n = nth_default 0 xs n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   364
  apply (induct xs arbitrary: n) apply simp_all
55642
63beb38e9258 adapted to renaming of datatype 'cases' and 'recs' to 'case' and 'rec'
blanchet
parents: 55417
diff changeset
   365
  by (metis nat.case not0_implies_Suc nth_default_Cons_0 nth_default_Cons_Suc pCons.rep_eq)
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   366
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   367
lemma nth_default_coeffs_eq:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   368
  "nth_default 0 (coeffs p) = coeff p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   369
  by (simp add: fun_eq_iff coeff_Poly_eq [symmetric])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   370
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   371
lemma [code]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   372
  "coeff p = nth_default 0 (coeffs p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   373
  by (simp add: nth_default_coeffs_eq)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   374
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   375
lemma coeffs_eqI:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   376
  assumes coeff: "\<And>n. coeff p n = nth_default 0 xs n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   377
  assumes zero: "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   378
  shows "coeffs p = xs"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   379
proof -
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   380
  from coeff have "p = Poly xs" by (simp add: poly_eq_iff coeff_Poly_eq)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   381
  with zero show ?thesis by simp (cases xs, simp_all)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   382
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   383
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   384
lemma degree_eq_length_coeffs [code]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   385
  "degree p = length (coeffs p) - 1"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   386
  by (simp add: coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   387
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   388
lemma length_coeffs_degree:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   389
  "p \<noteq> 0 \<Longrightarrow> length (coeffs p) = Suc (degree p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   390
  by (induct p) (auto simp add: cCons_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   391
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   392
lemma [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   393
  "coeffs 0 = []"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   394
  by (fact coeffs_0_eq_Nil)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   395
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   396
lemma [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   397
  "coeffs (pCons a p) = a ## coeffs p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   398
  by (fact coeffs_pCons_eq_cCons)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   399
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   400
instantiation poly :: ("{zero, equal}") equal
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   401
begin
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   402
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   403
definition
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   404
  [code]: "HOL.equal (p::'a poly) q \<longleftrightarrow> HOL.equal (coeffs p) (coeffs q)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   405
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   406
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   407
  by standard (simp add: equal equal_poly_def coeffs_eq_iff)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   408
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   409
end
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   410
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   411
lemma [code nbe]: "HOL.equal (p :: _ poly) p \<longleftrightarrow> True"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   412
  by (fact equal_refl)
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   413
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   414
definition is_zero :: "'a::zero poly \<Rightarrow> bool"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   415
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   416
  [code]: "is_zero p \<longleftrightarrow> List.null (coeffs p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   417
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   418
lemma is_zero_null [code_abbrev]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   419
  "is_zero p \<longleftrightarrow> p = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   420
  by (simp add: is_zero_def null_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   421
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   422
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   423
subsection \<open>Fold combinator for polynomials\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   424
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   425
definition fold_coeffs :: "('a::zero \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   426
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   427
  "fold_coeffs f p = foldr f (coeffs p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   428
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   429
lemma fold_coeffs_0_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   430
  "fold_coeffs f 0 = id"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   431
  by (simp add: fold_coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   432
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   433
lemma fold_coeffs_pCons_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   434
  "f 0 = id \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   435
  by (simp add: fold_coeffs_def cCons_def fun_eq_iff)
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   436
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   437
lemma fold_coeffs_pCons_0_0_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   438
  "fold_coeffs f (pCons 0 0) = id"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   439
  by (simp add: fold_coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   440
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   441
lemma fold_coeffs_pCons_coeff_not_0_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   442
  "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   443
  by (simp add: fold_coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   444
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   445
lemma fold_coeffs_pCons_not_0_0_eq [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   446
  "p \<noteq> 0 \<Longrightarrow> fold_coeffs f (pCons a p) = f a \<circ> fold_coeffs f p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   447
  by (simp add: fold_coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   448
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   449
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   450
subsection \<open>Canonical morphism on polynomials -- evaluation\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   451
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   452
definition poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   453
where
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61260
diff changeset
   454
  "poly p = fold_coeffs (\<lambda>a f x. a + x * f x) p (\<lambda>x. 0)" \<comment> \<open>The Horner Schema\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   455
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   456
lemma poly_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   457
  "poly 0 x = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   458
  by (simp add: poly_def)
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   459
  
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   460
lemma poly_pCons [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   461
  "poly (pCons a p) x = a + x * poly p x"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   462
  by (cases "p = 0 \<and> a = 0") (auto simp add: poly_def)
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   463
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   464
lemma poly_altdef: 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   465
  "poly p (x :: 'a :: {comm_semiring_0, semiring_1}) = (\<Sum>i\<le>degree p. coeff p i * x ^ i)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   466
proof (induction p rule: pCons_induct)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   467
  case (pCons a p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   468
    show ?case
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   469
    proof (cases "p = 0")
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   470
      case False
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   471
      let ?p' = "pCons a p"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   472
      note poly_pCons[of a p x]
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   473
      also note pCons.IH
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   474
      also have "a + x * (\<Sum>i\<le>degree p. coeff p i * x ^ i) =
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   475
                 coeff ?p' 0 * x^0 + (\<Sum>i\<le>degree p. coeff ?p' (Suc i) * x^Suc i)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   476
          by (simp add: field_simps setsum_right_distrib coeff_pCons)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   477
      also note setsum_atMost_Suc_shift[symmetric]
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
   478
      also note degree_pCons_eq[OF \<open>p \<noteq> 0\<close>, of a, symmetric]
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   479
      finally show ?thesis .
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   480
   qed simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   481
qed simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   482
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   483
lemma poly_0_coeff_0: "poly p 0 = coeff p 0"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   484
  by (cases p) (auto simp: poly_altdef)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   485
29454
b0f586f38dd7 add recursion combinator poly_rec; define poly function using poly_rec
huffman
parents: 29453
diff changeset
   486
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   487
subsection \<open>Monomials\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   488
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   489
lift_definition monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   490
  is "\<lambda>a m n. if m = n then a else 0"
59983
cd2efd7d06bd replace almost_everywhere_zero by Infinite_Set.MOST
hoelzl
parents: 59815
diff changeset
   491
  by (simp add: MOST_iff_cofinite)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   492
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   493
lemma coeff_monom [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   494
  "coeff (monom a m) n = (if m = n then a else 0)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   495
  by transfer rule
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   496
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   497
lemma monom_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   498
  "monom a 0 = pCons a 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   499
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   500
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   501
lemma monom_Suc:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   502
  "monom a (Suc n) = pCons 0 (monom a n)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   503
  by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   504
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   505
lemma monom_eq_0 [simp]: "monom 0 n = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   506
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   507
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   508
lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   509
  by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   510
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   511
lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   512
  by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   513
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   514
lemma degree_monom_le: "degree (monom a n) \<le> n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   515
  by (rule degree_le, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   516
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   517
lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   518
  apply (rule order_antisym [OF degree_monom_le])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   519
  apply (rule le_degree, simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   520
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   521
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   522
lemma coeffs_monom [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   523
  "coeffs (monom a n) = (if a = 0 then [] else replicate n 0 @ [a])"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   524
  by (induct n) (simp_all add: monom_0 monom_Suc)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   525
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   526
lemma fold_coeffs_monom [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   527
  "a \<noteq> 0 \<Longrightarrow> fold_coeffs f (monom a n) = f 0 ^^ n \<circ> f a"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   528
  by (simp add: fold_coeffs_def coeffs_monom fun_eq_iff)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   529
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   530
lemma poly_monom:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   531
  fixes a x :: "'a::{comm_semiring_1}"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   532
  shows "poly (monom a n) x = a * x ^ n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   533
  by (cases "a = 0", simp_all)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   534
    (induct n, simp_all add: mult.left_commute poly_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   535
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   536
    
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   537
subsection \<open>Addition and subtraction\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   538
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   539
instantiation poly :: (comm_monoid_add) comm_monoid_add
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   540
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   541
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   542
lift_definition plus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   543
  is "\<lambda>p q n. coeff p n + coeff q n"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   544
proof -
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   545
  fix q p :: "'a poly"
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   546
  show "\<forall>\<^sub>\<infinity>n. coeff p n + coeff q n = 0"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   547
    using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   548
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   549
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   550
lemma coeff_add [simp]: "coeff (p + q) n = coeff p n + coeff q n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   551
  by (simp add: plus_poly.rep_eq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   552
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   553
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   554
proof
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   555
  fix p q r :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   556
  show "(p + q) + r = p + (q + r)"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57482
diff changeset
   557
    by (simp add: poly_eq_iff add.assoc)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   558
  show "p + q = q + p"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57482
diff changeset
   559
    by (simp add: poly_eq_iff add.commute)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   560
  show "0 + p = p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   561
    by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   562
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   563
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   564
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   565
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   566
instantiation poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   567
begin
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   568
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   569
lift_definition minus_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   570
  is "\<lambda>p q n. coeff p n - coeff q n"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   571
proof -
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   572
  fix q p :: "'a poly"
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   573
  show "\<forall>\<^sub>\<infinity>n. coeff p n - coeff q n = 0"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   574
    using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   575
qed
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   576
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   577
lemma coeff_diff [simp]: "coeff (p - q) n = coeff p n - coeff q n"
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   578
  by (simp add: minus_poly.rep_eq)
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   579
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   580
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   581
proof
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   582
  fix p q r :: "'a poly"
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   583
  show "p + q - p = q"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   584
    by (simp add: poly_eq_iff)
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   585
  show "p - q - r = p - (q + r)"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   586
    by (simp add: poly_eq_iff diff_diff_eq)
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   587
qed
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   588
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   589
end
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   590
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   591
instantiation poly :: (ab_group_add) ab_group_add
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   592
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   593
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   594
lift_definition uminus_poly :: "'a poly \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   595
  is "\<lambda>p n. - coeff p n"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   596
proof -
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   597
  fix p :: "'a poly"
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   598
  show "\<forall>\<^sub>\<infinity>n. - coeff p n = 0"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   599
    using MOST_coeff_eq_0 by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   600
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   601
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   602
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   603
  by (simp add: uminus_poly.rep_eq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   604
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   605
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   606
proof
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   607
  fix p q :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   608
  show "- p + p = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   609
    by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   610
  show "p - q = p + - q"
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 52380
diff changeset
   611
    by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   612
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   613
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   614
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   615
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   616
lemma add_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   617
  "pCons a p + pCons b q = pCons (a + b) (p + q)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   618
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   619
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   620
lemma minus_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   621
  "- pCons a p = pCons (- a) (- p)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   622
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   623
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   624
lemma diff_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   625
  "pCons a p - pCons b q = pCons (a - b) (p - q)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   626
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   627
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   628
lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   629
  by (rule degree_le, auto simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   630
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   631
lemma degree_add_le:
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   632
  "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n"
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   633
  by (auto intro: order_trans degree_add_le_max)
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   634
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   635
lemma degree_add_less:
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   636
  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   637
  by (auto intro: le_less_trans degree_add_le_max)
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   638
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   639
lemma degree_add_eq_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   640
  "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   641
  apply (cases "q = 0", simp)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   642
  apply (rule order_antisym)
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   643
  apply (simp add: degree_add_le)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   644
  apply (rule le_degree)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   645
  apply (simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   646
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   647
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   648
lemma degree_add_eq_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   649
  "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   650
  using degree_add_eq_right [of q p]
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57482
diff changeset
   651
  by (simp add: add.commute)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   652
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   653
lemma degree_minus [simp]:
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   654
  "degree (- p) = degree p"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   655
  unfolding degree_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   656
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   657
lemma degree_diff_le_max:
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   658
  fixes p q :: "'a :: ab_group_add poly"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   659
  shows "degree (p - q) \<le> max (degree p) (degree q)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   660
  using degree_add_le [where p=p and q="-q"]
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 52380
diff changeset
   661
  by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   662
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   663
lemma degree_diff_le:
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   664
  fixes p q :: "'a :: ab_group_add poly"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   665
  assumes "degree p \<le> n" and "degree q \<le> n"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   666
  shows "degree (p - q) \<le> n"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   667
  using assms degree_add_le [of p n "- q"] by simp
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
   668
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   669
lemma degree_diff_less:
59815
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   670
  fixes p q :: "'a :: ab_group_add poly"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   671
  assumes "degree p < n" and "degree q < n"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   672
  shows "degree (p - q) < n"
cce82e360c2f explicit commutative additive inverse operation;
haftmann
parents: 59557
diff changeset
   673
  using assms degree_add_less [of p n "- q"] by simp
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
   674
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   675
lemma add_monom: "monom a n + monom b n = monom (a + b) n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   676
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   677
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   678
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   679
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   680
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   681
lemma minus_monom: "- monom a n = monom (-a) n"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   682
  by (rule poly_eqI) simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   683
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   684
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
   685
  by (cases "finite A", induct set: finite, simp_all)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   686
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   687
lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   688
  by (rule poly_eqI) (simp add: coeff_setsum)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   689
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   690
fun plus_coeffs :: "'a::comm_monoid_add list \<Rightarrow> 'a list \<Rightarrow> 'a list"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   691
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   692
  "plus_coeffs xs [] = xs"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   693
| "plus_coeffs [] ys = ys"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   694
| "plus_coeffs (x # xs) (y # ys) = (x + y) ## plus_coeffs xs ys"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   695
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   696
lemma coeffs_plus_eq_plus_coeffs [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   697
  "coeffs (p + q) = plus_coeffs (coeffs p) (coeffs q)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   698
proof -
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   699
  { fix xs ys :: "'a list" and n
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   700
    have "nth_default 0 (plus_coeffs xs ys) n = nth_default 0 xs n + nth_default 0 ys n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   701
    proof (induct xs ys arbitrary: n rule: plus_coeffs.induct)
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   702
      case (3 x xs y ys n)
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   703
      then show ?case by (cases n) (auto simp add: cCons_def)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   704
    qed simp_all }
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   705
  note * = this
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   706
  { fix xs ys :: "'a list"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   707
    assume "xs \<noteq> [] \<Longrightarrow> last xs \<noteq> 0" and "ys \<noteq> [] \<Longrightarrow> last ys \<noteq> 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   708
    moreover assume "plus_coeffs xs ys \<noteq> []"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   709
    ultimately have "last (plus_coeffs xs ys) \<noteq> 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   710
    proof (induct xs ys rule: plus_coeffs.induct)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   711
      case (3 x xs y ys) then show ?case by (auto simp add: cCons_def) metis
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   712
    qed simp_all }
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   713
  note ** = this
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   714
  show ?thesis
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   715
    apply (rule coeffs_eqI)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   716
    apply (simp add: * nth_default_coeffs_eq)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   717
    apply (rule **)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   718
    apply (auto dest: last_coeffs_not_0)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   719
    done
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   720
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   721
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   722
lemma coeffs_uminus [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   723
  "coeffs (- p) = map (\<lambda>a. - a) (coeffs p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   724
  by (rule coeffs_eqI)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   725
    (simp_all add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   726
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   727
lemma [code]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   728
  fixes p q :: "'a::ab_group_add poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   729
  shows "p - q = p + - q"
59557
ebd8ecacfba6 establish unique preferred fact names
haftmann
parents: 59487
diff changeset
   730
  by (fact diff_conv_add_uminus)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   731
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   732
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   733
  apply (induct p arbitrary: q, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   734
  apply (case_tac q, simp, simp add: algebra_simps)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   735
  done
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   736
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   737
lemma poly_minus [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   738
  fixes x :: "'a::comm_ring"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   739
  shows "poly (- p) x = - poly p x"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   740
  by (induct p) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   741
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   742
lemma poly_diff [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   743
  fixes x :: "'a::comm_ring"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   744
  shows "poly (p - q) x = poly p x - poly q x"
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 52380
diff changeset
   745
  using poly_add [of p "- q" x] by simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   746
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   747
lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   748
  by (induct A rule: infinite_finite_induct) simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   749
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   750
lemma degree_setsum_le: "finite S \<Longrightarrow> (\<And> p . p \<in> S \<Longrightarrow> degree (f p) \<le> n)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   751
  \<Longrightarrow> degree (setsum f S) \<le> n"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   752
proof (induct S rule: finite_induct)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   753
  case (insert p S)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   754
  hence "degree (setsum f S) \<le> n" "degree (f p) \<le> n" by auto
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   755
  thus ?case unfolding setsum.insert[OF insert(1-2)] by (metis degree_add_le)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   756
qed simp
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   757
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   758
lemma poly_as_sum_of_monoms': 
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   759
  assumes n: "degree p \<le> n" 
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   760
  shows "(\<Sum>i\<le>n. monom (coeff p i) i) = p"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   761
proof -
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   762
  have eq: "\<And>i. {..n} \<inter> {i} = (if i \<le> n then {i} else {})"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   763
    by auto
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   764
  show ?thesis
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   765
    using n by (simp add: poly_eq_iff coeff_setsum coeff_eq_0 setsum.If_cases eq 
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   766
                  if_distrib[where f="\<lambda>x. x * a" for a])
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   767
qed
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   768
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   769
lemma poly_as_sum_of_monoms: "(\<Sum>i\<le>degree p. monom (coeff p i) i) = p"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   770
  by (intro poly_as_sum_of_monoms' order_refl)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   771
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   772
lemma Poly_snoc: "Poly (xs @ [x]) = Poly xs + monom x (length xs)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   773
  by (induction xs) (simp_all add: monom_0 monom_Suc)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   774
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   775
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
   776
subsection \<open>Multiplication by a constant, polynomial multiplication and the unit polynomial\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   777
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   778
lift_definition smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   779
  is "\<lambda>a p n. a * coeff p n"
60040
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   780
proof -
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   781
  fix a :: 'a and p :: "'a poly" show "\<forall>\<^sub>\<infinity> i. a * coeff p i = 0"
1fa1023b13b9 move MOST and INFM in Infinite_Set to Filter; change them to abbreviations over the cofinite filter
hoelzl
parents: 59983
diff changeset
   782
    using MOST_coeff_eq_0[of p] by eventually_elim simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   783
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   784
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   785
lemma coeff_smult [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   786
  "coeff (smult a p) n = a * coeff p n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   787
  by (simp add: smult.rep_eq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   788
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   789
lemma degree_smult_le: "degree (smult a p) \<le> degree p"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   790
  by (rule degree_le, simp add: coeff_eq_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   791
29472
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   792
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57482
diff changeset
   793
  by (rule poly_eqI, simp add: mult.assoc)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   794
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   795
lemma smult_0_right [simp]: "smult a 0 = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   796
  by (rule poly_eqI, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   797
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   798
lemma smult_0_left [simp]: "smult 0 p = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   799
  by (rule poly_eqI, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   800
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   801
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   802
  by (rule poly_eqI, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   803
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   804
lemma smult_add_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   805
  "smult a (p + q) = smult a p + smult a q"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   806
  by (rule poly_eqI, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   807
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   808
lemma smult_add_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   809
  "smult (a + b) p = smult a p + smult b p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   810
  by (rule poly_eqI, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   811
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
   812
lemma smult_minus_right [simp]:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   813
  "smult (a::'a::comm_ring) (- p) = - smult a p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   814
  by (rule poly_eqI, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   815
29457
2eadbc24de8c correctness and uniqueness of synthetic division
huffman
parents: 29456
diff changeset
   816
lemma smult_minus_left [simp]:
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   817
  "smult (- a::'a::comm_ring) p = - smult a p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   818
  by (rule poly_eqI, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   819
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   820
lemma smult_diff_right:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   821
  "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   822
  by (rule poly_eqI, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   823
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   824
lemma smult_diff_left:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   825
  "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   826
  by (rule poly_eqI, simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   827
29472
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   828
lemmas smult_distribs =
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   829
  smult_add_left smult_add_right
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   830
  smult_diff_left smult_diff_right
a63a2e46cec9 declare smult rules [simp]
huffman
parents: 29471
diff changeset
   831
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   832
lemma smult_pCons [simp]:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   833
  "smult a (pCons b p) = pCons (a * b) (smult a p)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   834
  by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   835
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   836
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   837
  by (induct n, simp add: monom_0, simp add: monom_Suc)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   838
29659
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   839
lemma degree_smult_eq [simp]:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   840
  fixes a :: "'a::idom"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   841
  shows "degree (smult a p) = (if a = 0 then 0 else degree p)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   842
  by (cases "a = 0", simp, simp add: degree_def)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   843
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   844
lemma smult_eq_0_iff [simp]:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   845
  fixes a :: "'a::idom"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
   846
  shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   847
  by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   848
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   849
lemma coeffs_smult [code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   850
  fixes p :: "'a::idom poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   851
  shows "coeffs (smult a p) = (if a = 0 then [] else map (Groups.times a) (coeffs p))"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   852
  by (rule coeffs_eqI)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   853
    (auto simp add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   854
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   855
instantiation poly :: (comm_semiring_0) comm_semiring_0
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   856
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   857
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   858
definition
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   859
  "p * q = fold_coeffs (\<lambda>a p. smult a q + pCons 0 p) p 0"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   860
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   861
lemma mult_poly_0_left: "(0::'a poly) * q = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   862
  by (simp add: times_poly_def)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   863
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   864
lemma mult_pCons_left [simp]:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   865
  "pCons a p * q = smult a q + pCons 0 (p * q)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   866
  by (cases "p = 0 \<and> a = 0") (auto simp add: times_poly_def)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   867
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   868
lemma mult_poly_0_right: "p * (0::'a poly) = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   869
  by (induct p) (simp add: mult_poly_0_left, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   870
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   871
lemma mult_pCons_right [simp]:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   872
  "p * pCons a q = smult a p + pCons 0 (p * q)"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   873
  by (induct p) (simp add: mult_poly_0_left, simp add: algebra_simps)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   874
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   875
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   876
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   877
lemma mult_smult_left [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   878
  "smult a p * q = smult a (p * q)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   879
  by (induct p) (simp add: mult_poly_0, simp add: smult_add_right)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   880
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   881
lemma mult_smult_right [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   882
  "p * smult a q = smult a (p * q)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   883
  by (induct q) (simp add: mult_poly_0, simp add: smult_add_right)
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   884
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   885
lemma mult_poly_add_left:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   886
  fixes p q r :: "'a poly"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   887
  shows "(p + q) * r = p * r + q * r"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   888
  by (induct r) (simp add: mult_poly_0, simp add: smult_distribs algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   889
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   890
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   891
proof
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   892
  fix p q r :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   893
  show 0: "0 * p = 0"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   894
    by (rule mult_poly_0_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   895
  show "p * 0 = 0"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   896
    by (rule mult_poly_0_right)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   897
  show "(p + q) * r = p * r + q * r"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   898
    by (rule mult_poly_add_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   899
  show "(p * q) * r = p * (q * r)"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   900
    by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   901
  show "p * q = q * p"
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   902
    by (induct p, simp add: mult_poly_0, simp)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   903
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   904
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   905
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   906
29540
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   907
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
8858d197a9b6 more instance declarations for poly
huffman
parents: 29539
diff changeset
   908
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   909
lemma coeff_mult:
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   910
  "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   911
proof (induct p arbitrary: n)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   912
  case 0 show ?case by simp
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   913
next
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   914
  case (pCons a p n) thus ?case
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   915
    by (cases n, simp, simp add: setsum_atMost_Suc_shift
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   916
                            del: setsum_atMost_Suc)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   917
qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   918
29474
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   919
lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   920
apply (rule degree_le)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   921
apply (induct p)
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   922
apply simp
674a21226c5a define polynomial multiplication using poly_rec
huffman
parents: 29472
diff changeset
   923
apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   924
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   925
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   926
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   927
  by (induct m) (simp add: monom_0 smult_monom, simp add: monom_Suc)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   928
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   929
instantiation poly :: (comm_semiring_1) comm_semiring_1
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   930
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   931
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   932
definition one_poly_def: "1 = pCons 1 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   933
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   934
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   935
proof
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
   936
  show "1 * p = p" for p :: "'a poly"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   937
    unfolding one_poly_def by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   938
  show "0 \<noteq> (1::'a poly)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   939
    unfolding one_poly_def by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   940
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   941
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   942
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   943
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   944
instance poly :: (comm_ring) comm_ring ..
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   945
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   946
instance poly :: (comm_ring_1) comm_ring_1 ..
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   947
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   948
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   949
  unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   950
  by (simp add: coeff_pCons split: nat.split)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   951
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   952
lemma monom_eq_1 [simp]:
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   953
  "monom 1 0 = 1"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   954
  by (simp add: monom_0 one_poly_def)
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
   955
  
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   956
lemma degree_1 [simp]: "degree 1 = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   957
  unfolding one_poly_def
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   958
  by (rule degree_pCons_0)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
   959
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   960
lemma coeffs_1_eq [simp, code abstract]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   961
  "coeffs 1 = [1]"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   962
  by (simp add: one_poly_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   963
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   964
lemma degree_power_le:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   965
  "degree (p ^ n) \<le> degree p * n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   966
  by (induct n) (auto intro: order_trans degree_mult_le)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   967
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   968
lemma poly_smult [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   969
  "poly (smult a p) x = a * poly p x"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   970
  by (induct p, simp, simp add: algebra_simps)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   971
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   972
lemma poly_mult [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   973
  "poly (p * q) x = poly p x * poly q x"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   974
  by (induct p, simp_all, simp add: algebra_simps)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   975
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   976
lemma poly_1 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   977
  "poly 1 x = 1"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   978
  by (simp add: one_poly_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   979
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   980
lemma poly_power [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   981
  fixes p :: "'a::{comm_semiring_1} poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   982
  shows "poly (p ^ n) x = poly p x ^ n"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   983
  by (induct n) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
   984
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   985
lemma poly_setprod: "poly (\<Prod>k\<in>A. p k) x = (\<Prod>k\<in>A. poly (p k) x)"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   986
  by (induct A rule: infinite_finite_induct) simp_all
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   987
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   988
lemma degree_setprod_setsum_le: "finite S \<Longrightarrow> degree (setprod f S) \<le> setsum (degree o f) S"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   989
proof (induct S rule: finite_induct)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   990
  case (insert a S)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   991
  show ?case unfolding setprod.insert[OF insert(1-2)] setsum.insert[OF insert(1-2)]
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   992
    by (rule le_trans[OF degree_mult_le], insert insert, auto)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   993
qed simp
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
   994
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   995
subsection \<open>Conversions from natural numbers\<close>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   996
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   997
lemma of_nat_poly: "of_nat n = [:of_nat n :: 'a :: comm_semiring_1:]"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   998
proof (induction n)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
   999
  case (Suc n)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1000
  hence "of_nat (Suc n) = 1 + (of_nat n :: 'a poly)" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1001
    by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1002
  also have "(of_nat n :: 'a poly) = [: of_nat n :]" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1003
    by (subst Suc) (rule refl)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1004
  also have "1 = [:1:]" by (simp add: one_poly_def)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1005
  finally show ?case by (subst (asm) add_pCons) simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1006
qed simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1007
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1008
lemma degree_of_nat [simp]: "degree (of_nat n) = 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1009
  by (simp add: of_nat_poly)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1010
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1011
lemma degree_numeral [simp]: "degree (numeral n) = 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1012
  by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1013
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1014
lemma numeral_poly: "numeral n = [:numeral n:]"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1015
  by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1016
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1017
subsection \<open>Lemmas about divisibility\<close>
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1018
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1019
lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1020
proof -
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1021
  assume "p dvd q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1022
  then obtain k where "q = p * k" ..
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1023
  then have "smult a q = p * smult a k" by simp
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1024
  then show "p dvd smult a q" ..
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1025
qed
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1026
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1027
lemma dvd_smult_cancel:
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1028
  fixes a :: "'a :: field"
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1029
  shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1030
  by (drule dvd_smult [where a="inverse a"]) simp
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1031
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1032
lemma dvd_smult_iff:
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1033
  fixes a :: "'a::field"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1034
  shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q"
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1035
  by (safe elim!: dvd_smult dvd_smult_cancel)
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1036
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1037
lemma smult_dvd_cancel:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1038
  "smult a p dvd q \<Longrightarrow> p dvd q"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1039
proof -
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1040
  assume "smult a p dvd q"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1041
  then obtain k where "q = smult a p * k" ..
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1042
  then have "q = p * smult a k" by simp
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1043
  then show "p dvd q" ..
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1044
qed
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1045
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1046
lemma smult_dvd:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1047
  fixes a :: "'a::field"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1048
  shows "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1049
  by (rule smult_dvd_cancel [where a="inverse a"]) simp
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1050
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1051
lemma smult_dvd_iff:
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1052
  fixes a :: "'a::field"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1053
  shows "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)"
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1054
  by (auto elim: smult_dvd smult_dvd_cancel)
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1055
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1056
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1057
subsection \<open>Polynomials form an integral domain\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1058
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1059
lemma coeff_mult_degree_sum:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1060
  "coeff (p * q) (degree p + degree q) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1061
   coeff p (degree p) * coeff q (degree q)"
29471
6a46a13ce1f9 simplify proof of coeff_mult_degree_sum
huffman
parents: 29462
diff changeset
  1062
  by (induct p, simp, simp add: coeff_eq_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1063
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1064
instance poly :: (idom) idom
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1065
proof
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1066
  fix p q :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1067
  assume "p \<noteq> 0" and "q \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1068
  have "coeff (p * q) (degree p + degree q) =
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1069
        coeff p (degree p) * coeff q (degree q)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1070
    by (rule coeff_mult_degree_sum)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1071
  also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1072
    using \<open>p \<noteq> 0\<close> and \<open>q \<noteq> 0\<close> by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1073
  finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1074
  thus "p * q \<noteq> 0" by (simp add: poly_eq_iff)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1075
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1076
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1077
lemma degree_mult_eq:
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1078
  fixes p q :: "'a::semidom poly"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1079
  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
  1080
apply (rule order_antisym [OF degree_mult_le le_degree])
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1081
apply (simp add: coeff_mult_degree_sum)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1082
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1083
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1084
lemma degree_mult_right_le:
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1085
  fixes p q :: "'a::semidom poly"
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1086
  assumes "q \<noteq> 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1087
  shows "degree p \<le> degree (p * q)"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1088
  using assms by (cases "p = 0") (simp_all add: degree_mult_eq)
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1089
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1090
lemma coeff_degree_mult:
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1091
  fixes p q :: "'a::semidom poly"
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1092
  shows "coeff (p * q) (degree (p * q)) =
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1093
    coeff q (degree q) * coeff p (degree p)"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1094
  by (cases "p = 0 \<or> q = 0") (auto simp add: degree_mult_eq coeff_mult_degree_sum mult_ac)
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1095
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1096
lemma dvd_imp_degree_le:
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1097
  fixes p q :: "'a::semidom poly"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1098
  shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1099
  by (erule dvdE, hypsubst, subst degree_mult_eq) auto
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1100
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1101
lemma divides_degree:
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1102
  assumes pq: "p dvd (q :: 'a :: semidom poly)"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1103
  shows "degree p \<le> degree q \<or> q = 0"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  1104
  by (metis dvd_imp_degree_le pq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1105
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1106
subsection \<open>Polynomials form an ordered integral domain\<close>
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1107
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1108
definition pos_poly :: "'a::linordered_idom poly \<Rightarrow> bool"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1109
where
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1110
  "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1111
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1112
lemma pos_poly_pCons:
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1113
  "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1114
  unfolding pos_poly_def by simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1115
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1116
lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1117
  unfolding pos_poly_def by simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1118
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1119
lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1120
  apply (induct p arbitrary: q, simp)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1121
  apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1122
  done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1123
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1124
lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1125
  unfolding pos_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1126
  apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")
56544
b60d5d119489 made mult_pos_pos a simp rule
nipkow
parents: 56383
diff changeset
  1127
  apply (simp add: degree_mult_eq coeff_mult_degree_sum)
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1128
  apply auto
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1129
  done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1130
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1131
lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1132
by (induct p) (auto simp add: pos_poly_pCons)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1133
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1134
lemma last_coeffs_eq_coeff_degree:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1135
  "p \<noteq> 0 \<Longrightarrow> last (coeffs p) = coeff p (degree p)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1136
  by (simp add: coeffs_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1137
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1138
lemma pos_poly_coeffs [code]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1139
  "pos_poly p \<longleftrightarrow> (let as = coeffs p in as \<noteq> [] \<and> last as > 0)" (is "?P \<longleftrightarrow> ?Q")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1140
proof
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1141
  assume ?Q then show ?P by (auto simp add: pos_poly_def last_coeffs_eq_coeff_degree)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1142
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1143
  assume ?P then have *: "0 < coeff p (degree p)" by (simp add: pos_poly_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1144
  then have "p \<noteq> 0" by auto
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1145
  with * show ?Q by (simp add: last_coeffs_eq_coeff_degree)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1146
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1147
35028
108662d50512 more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents: 34973
diff changeset
  1148
instantiation poly :: (linordered_idom) linordered_idom
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1149
begin
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1150
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1151
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
  1152
  "x < y \<longleftrightarrow> pos_poly (y - x)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1153
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1154
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
  1155
  "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1156
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1157
definition
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61605
diff changeset
  1158
  "\<bar>x::'a poly\<bar> = (if x < 0 then - x else x)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1159
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1160
definition
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
  1161
  "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1162
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1163
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1164
proof
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1165
  fix x y z :: "'a poly"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1166
  show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1167
    unfolding less_eq_poly_def less_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1168
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1169
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1170
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1171
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1172
    done
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1173
  show "x \<le> x"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1174
    unfolding less_eq_poly_def by simp
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1175
  show "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1176
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1177
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1178
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1179
    apply (simp add: algebra_simps)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1180
    done
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1181
  show "x \<le> y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1182
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1183
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1184
    apply (drule (1) pos_poly_add)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1185
    apply simp
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1186
    done
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1187
  show "x \<le> y \<Longrightarrow> z + x \<le> z + y"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1188
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1189
    apply safe
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1190
    apply (simp add: algebra_simps)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1191
    done
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1192
  show "x \<le> y \<or> y \<le> x"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1193
    unfolding less_eq_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1194
    using pos_poly_total [of "x - y"]
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1195
    by auto
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1196
  show "x < y \<Longrightarrow> 0 < z \<Longrightarrow> z * x < z * y"
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1197
    unfolding less_poly_def
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1198
    by (simp add: right_diff_distrib [symmetric] pos_poly_mult)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1199
  show "\<bar>x\<bar> = (if x < 0 then - x else x)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1200
    by (rule abs_poly_def)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1201
  show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1202
    by (rule sgn_poly_def)
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1203
qed
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1204
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1205
end
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1206
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1207
text \<open>TODO: Simplification rules for comparisons\<close>
29878
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1208
06efd6e731c6 ordered_idom instance for polynomials
huffman
parents: 29668
diff changeset
  1209
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1210
subsection \<open>Synthetic division and polynomial roots\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1211
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1212
text \<open>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1213
  Synthetic division is simply division by the linear polynomial @{term "x - c"}.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1214
\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1215
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1216
definition synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1217
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1218
  "synthetic_divmod p c = fold_coeffs (\<lambda>a (q, r). (pCons r q, a + c * r)) p (0, 0)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1219
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1220
definition synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1221
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1222
  "synthetic_div p c = fst (synthetic_divmod p c)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1223
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1224
lemma synthetic_divmod_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1225
  "synthetic_divmod 0 c = (0, 0)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1226
  by (simp add: synthetic_divmod_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1227
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1228
lemma synthetic_divmod_pCons [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1229
  "synthetic_divmod (pCons a p) c = (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1230
  by (cases "p = 0 \<and> a = 0") (auto simp add: synthetic_divmod_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1231
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1232
lemma synthetic_div_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1233
  "synthetic_div 0 c = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1234
  unfolding synthetic_div_def by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1235
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1236
lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1237
by (induct p arbitrary: a) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1238
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1239
lemma snd_synthetic_divmod:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1240
  "snd (synthetic_divmod p c) = poly p c"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1241
  by (induct p, simp, simp add: split_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1242
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1243
lemma synthetic_div_pCons [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1244
  "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1245
  unfolding synthetic_div_def
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1246
  by (simp add: split_def snd_synthetic_divmod)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1247
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1248
lemma synthetic_div_eq_0_iff:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1249
  "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1250
  by (induct p, simp, case_tac p, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1251
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1252
lemma degree_synthetic_div:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1253
  "degree (synthetic_div p c) = degree p - 1"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1254
  by (induct p, simp, simp add: synthetic_div_eq_0_iff)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1255
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1256
lemma synthetic_div_correct:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1257
  "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1258
  by (induct p) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1259
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1260
lemma synthetic_div_unique:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1261
  "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1262
apply (induct p arbitrary: q r)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1263
apply (simp, frule synthetic_div_unique_lemma, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1264
apply (case_tac q, force)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1265
done
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1266
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1267
lemma synthetic_div_correct':
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1268
  fixes c :: "'a::comm_ring_1"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1269
  shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1270
  using synthetic_div_correct [of p c]
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1271
  by (simp add: algebra_simps)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1272
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1273
lemma poly_eq_0_iff_dvd:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1274
  fixes c :: "'a::idom"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1275
  shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1276
proof
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1277
  assume "poly p c = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1278
  with synthetic_div_correct' [of c p]
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1279
  have "p = [:-c, 1:] * synthetic_div p c" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1280
  then show "[:-c, 1:] dvd p" ..
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1281
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1282
  assume "[:-c, 1:] dvd p"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1283
  then obtain k where "p = [:-c, 1:] * k" by (rule dvdE)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1284
  then show "poly p c = 0" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1285
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1286
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1287
lemma dvd_iff_poly_eq_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1288
  fixes c :: "'a::idom"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1289
  shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1290
  by (simp add: poly_eq_0_iff_dvd)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1291
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1292
lemma poly_roots_finite:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1293
  fixes p :: "'a::idom poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1294
  shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1295
proof (induct n \<equiv> "degree p" arbitrary: p)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1296
  case (0 p)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1297
  then obtain a where "a \<noteq> 0" and "p = [:a:]"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1298
    by (cases p, simp split: if_splits)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1299
  then show "finite {x. poly p x = 0}" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1300
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1301
  case (Suc n p)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1302
  show "finite {x. poly p x = 0}"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1303
  proof (cases "\<exists>x. poly p x = 0")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1304
    case False
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1305
    then show "finite {x. poly p x = 0}" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1306
  next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1307
    case True
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1308
    then obtain a where "poly p a = 0" ..
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1309
    then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1310
    then obtain k where k: "p = [:-a, 1:] * k" ..
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1311
    with \<open>p \<noteq> 0\<close> have "k \<noteq> 0" by auto
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1312
    with k have "degree p = Suc (degree k)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1313
      by (simp add: degree_mult_eq del: mult_pCons_left)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1314
    with \<open>Suc n = degree p\<close> have "n = degree k" by simp
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1315
    then have "finite {x. poly k x = 0}" using \<open>k \<noteq> 0\<close> by (rule Suc.hyps)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1316
    then have "finite (insert a {x. poly k x = 0})" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1317
    then show "finite {x. poly p x = 0}"
57862
8f074e6e22fc tuned proofs;
wenzelm
parents: 57512
diff changeset
  1318
      by (simp add: k Collect_disj_eq del: mult_pCons_left)
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1319
  qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1320
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1321
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1322
lemma poly_eq_poly_eq_iff:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1323
  fixes p q :: "'a::{idom,ring_char_0} poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1324
  shows "poly p = poly q \<longleftrightarrow> p = q" (is "?P \<longleftrightarrow> ?Q")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1325
proof
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1326
  assume ?Q then show ?P by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1327
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1328
  { fix p :: "'a::{idom,ring_char_0} poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1329
    have "poly p = poly 0 \<longleftrightarrow> p = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1330
      apply (cases "p = 0", simp_all)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1331
      apply (drule poly_roots_finite)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1332
      apply (auto simp add: infinite_UNIV_char_0)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1333
      done
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1334
  } note this [of "p - q"]
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1335
  moreover assume ?P
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1336
  ultimately show ?Q by auto
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1337
qed
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1338
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1339
lemma poly_all_0_iff_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1340
  fixes p :: "'a::{ring_char_0, idom} poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1341
  shows "(\<forall>x. poly p x = 0) \<longleftrightarrow> p = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1342
  by (auto simp add: poly_eq_poly_eq_iff [symmetric])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1343
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1344
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1345
subsection \<open>Long division of polynomials\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1346
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1347
definition pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1348
where
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1349
  "pdivmod_rel x y q r \<longleftrightarrow>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1350
    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
  1351
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1352
lemma pdivmod_rel_0:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1353
  "pdivmod_rel 0 y 0 0"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1354
  unfolding pdivmod_rel_def by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1355
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1356
lemma pdivmod_rel_by_0:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1357
  "pdivmod_rel x 0 0 x"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1358
  unfolding pdivmod_rel_def by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1359
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1360
lemma eq_zero_or_degree_less:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1361
  assumes "degree p \<le> n" and "coeff p n = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1362
  shows "p = 0 \<or> degree p < n"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1363
proof (cases n)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1364
  case 0
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1365
  with \<open>degree p \<le> n\<close> and \<open>coeff p n = 0\<close>
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1366
  have "coeff p (degree p) = 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1367
  then have "p = 0" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1368
  then show ?thesis ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1369
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1370
  case (Suc m)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1371
  have "\<forall>i>n. coeff p i = 0"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1372
    using \<open>degree p \<le> n\<close> by (simp add: coeff_eq_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1373
  then have "\<forall>i\<ge>n. coeff p i = 0"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1374
    using \<open>coeff p n = 0\<close> by (simp add: le_less)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1375
  then have "\<forall>i>m. coeff p i = 0"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1376
    using \<open>n = Suc m\<close> by (simp add: less_eq_Suc_le)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1377
  then have "degree p \<le> m"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1378
    by (rule degree_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1379
  then have "degree p < n"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1380
    using \<open>n = Suc m\<close> by (simp add: less_Suc_eq_le)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1381
  then show ?thesis ..
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1382
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1383
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1384
lemma pdivmod_rel_pCons:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1385
  assumes rel: "pdivmod_rel x y q r"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1386
  assumes y: "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1387
  assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1388
  shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1389
    (is "pdivmod_rel ?x y ?q ?r")
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1390
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1391
  have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1392
    using assms unfolding pdivmod_rel_def by simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1393
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1394
  have 1: "?x = ?q * y + ?r"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1395
    using b x by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1396
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1397
  have 2: "?r = 0 \<or> degree ?r < degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1398
  proof (rule eq_zero_or_degree_less)
29539
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
  1399
    show "degree ?r \<le> degree y"
abfe2af6883e add lemmas about degree
huffman
parents: 29537
diff changeset
  1400
    proof (rule degree_diff_le)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1401
      show "degree (pCons a r) \<le> degree y"
29460
ad87e5d1488b new lemmas about synthetic_div; declare degree_pCons_eq_if [simp]
huffman
parents: 29457
diff changeset
  1402
        using r by auto
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1403
      show "degree (smult b y) \<le> degree y"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1404
        by (rule degree_smult_le)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1405
    qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1406
  next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1407
    show "coeff ?r (degree y) = 0"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1408
      using \<open>y \<noteq> 0\<close> unfolding b by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1409
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1410
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1411
  from 1 2 show ?thesis
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1412
    unfolding pdivmod_rel_def
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1413
    using \<open>y \<noteq> 0\<close> by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1414
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1415
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1416
lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1417
apply (cases "y = 0")
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1418
apply (fast intro!: pdivmod_rel_by_0)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1419
apply (induct x)
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1420
apply (fast intro!: pdivmod_rel_0)
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1421
apply (fast intro!: pdivmod_rel_pCons)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1422
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1423
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1424
lemma pdivmod_rel_unique:
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1425
  assumes 1: "pdivmod_rel x y q1 r1"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1426
  assumes 2: "pdivmod_rel x y q2 r2"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1427
  shows "q1 = q2 \<and> r1 = r2"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1428
proof (cases "y = 0")
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1429
  assume "y = 0" with assms show ?thesis
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1430
    by (simp add: pdivmod_rel_def)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1431
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1432
  assume [simp]: "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1433
  from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1434
    unfolding pdivmod_rel_def by simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1435
  from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1436
    unfolding pdivmod_rel_def by simp_all
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1437
  from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29540
diff changeset
  1438
    by (simp add: algebra_simps)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1439
  from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
29453
de4f26f59135 add lemmas degree_{add,diff}_less
huffman
parents: 29451
diff changeset
  1440
    by (auto intro: degree_diff_less)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1441
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1442
  show "q1 = q2 \<and> r1 = r2"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1443
  proof (rule ccontr)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1444
    assume "\<not> (q1 = q2 \<and> r1 = r2)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1445
    with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1446
    with r3 have "degree (r2 - r1) < degree y" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1447
    also have "degree y \<le> degree (q1 - q2) + degree y" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1448
    also have "\<dots> = degree ((q1 - q2) * y)"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1449
      using \<open>q1 \<noteq> q2\<close> by (simp add: degree_mult_eq)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1450
    also have "\<dots> = degree (r2 - r1)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1451
      using q3 by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1452
    finally have "degree (r2 - r1) < degree (r2 - r1)" .
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1453
    then show "False" by simp
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1454
  qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1455
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1456
29660
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1457
lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1458
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1459
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1460
lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1461
by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1462
45605
a89b4bc311a5 eliminated obsolete "standard";
wenzelm
parents: 44890
diff changeset
  1463
lemmas pdivmod_rel_unique_div = pdivmod_rel_unique [THEN conjunct1]
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1464
45605
a89b4bc311a5 eliminated obsolete "standard";
wenzelm
parents: 44890
diff changeset
  1465
lemmas pdivmod_rel_unique_mod = pdivmod_rel_unique [THEN conjunct2]
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1466
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1467
instantiation poly :: (field) ring_div
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1468
begin
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1469
60352
d46de31a50c4 separate class for division operator, with particular syntax added in more specific classes
haftmann
parents: 60040
diff changeset
  1470
definition divide_poly where
60429
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1471
  div_poly_def: "x div y = (THE q. \<exists>r. pdivmod_rel x y q r)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1472
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1473
definition mod_poly where
37765
26bdfb7b680b dropped superfluous [code del]s
haftmann
parents: 36350
diff changeset
  1474
  "x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1475
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1476
lemma div_poly_eq:
60429
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1477
  "pdivmod_rel x y q r \<Longrightarrow> x div y = q"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1478
unfolding div_poly_def
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1479
by (fast elim: pdivmod_rel_unique_div)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1480
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1481
lemma mod_poly_eq:
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1482
  "pdivmod_rel x y q r \<Longrightarrow> x mod y = r"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1483
unfolding mod_poly_def
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1484
by (fast elim: pdivmod_rel_unique_mod)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1485
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1486
lemma pdivmod_rel:
60429
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1487
  "pdivmod_rel x y (x div y) (x mod y)"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1488
proof -
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1489
  from pdivmod_rel_exists
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1490
    obtain q r where "pdivmod_rel x y q r" by fast
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1491
  thus ?thesis
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1492
    by (simp add: div_poly_eq mod_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1493
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1494
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1495
instance
ade12ef2773c tuned proofs;
wenzelm
parents: 60570
diff changeset
  1496
proof
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1497
  fix x y :: "'a poly"
60429
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1498
  show "x div y * y + x mod y = x"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1499
    using pdivmod_rel [of x y]
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1500
    by (simp add: pdivmod_rel_def)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1501
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1502
  fix x :: "'a poly"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1503
  have "pdivmod_rel x 0 0 x"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1504
    by (rule pdivmod_rel_by_0)
60429
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1505
  thus "x div 0 = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1506
    by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1507
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1508
  fix y :: "'a poly"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1509
  have "pdivmod_rel 0 y 0 0"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1510
    by (rule pdivmod_rel_0)
60429
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1511
  thus "0 div y = 0"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1512
    by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1513
next
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1514
  fix x y z :: "'a poly"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1515
  assume "y \<noteq> 0"
60429
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1516
  hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1517
    using pdivmod_rel [of x y]
49962
a8cc904a6820 Renamed {left,right}_distrib to distrib_{right,left}.
webertj
parents: 49834
diff changeset
  1518
    by (simp add: pdivmod_rel_def distrib_right)
60429
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1519
  thus "(x + z * y) div y = z + x div y"
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1520
    by (rule div_poly_eq)
30930
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1521
next
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1522
  fix x y z :: "'a poly"
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1523
  assume "x \<noteq> 0"
60429
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1524
  show "(x * y) div (x * z) = y div z"
30930
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1525
  proof (cases "y \<noteq> 0 \<and> z \<noteq> 0")
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1526
    have "\<And>x::'a poly. pdivmod_rel x 0 0 x"
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1527
      by (rule pdivmod_rel_by_0)
60429
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1528
    then have [simp]: "\<And>x::'a poly. x div 0 = 0"
30930
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1529
      by (rule div_poly_eq)
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1530
    have "\<And>x::'a poly. pdivmod_rel 0 x 0 0"
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1531
      by (rule pdivmod_rel_0)
60429
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1532
    then have [simp]: "\<And>x::'a poly. 0 div x = 0"
30930
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1533
      by (rule div_poly_eq)
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1534
    case False then show ?thesis by auto
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1535
  next
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1536
    case True then have "y \<noteq> 0" and "z \<noteq> 0" by auto
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1537
    with \<open>x \<noteq> 0\<close>
30930
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1538
    have "\<And>q r. pdivmod_rel y z q r \<Longrightarrow> pdivmod_rel (x * y) (x * z) q (x * r)"
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1539
      by (auto simp add: pdivmod_rel_def algebra_simps)
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1540
        (rule classical, simp add: degree_mult_eq)
60429
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1541
    moreover from pdivmod_rel have "pdivmod_rel y z (y div z) (y mod z)" .
d3d1e185cd63 uniform _ div _ as infix syntax for ring division
haftmann
parents: 60352
diff changeset
  1542
    ultimately have "pdivmod_rel (x * y) (x * z) (y div z) (x * (y mod z))" .
30930
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1543
    then show ?thesis by (simp add: div_poly_eq)
11010e5f18f0 tightended specification of class semiring_div
haftmann
parents: 30738
diff changeset
  1544
  qed
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1545
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1546
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1547
end
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1548
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1549
lemma is_unit_monom_0:
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1550
  fixes a :: "'a::field"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1551
  assumes "a \<noteq> 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1552
  shows "is_unit (monom a 0)"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1553
proof
62351
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1554
  from assms show "1 = monom a 0 * monom (inverse a) 0"
60570
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1555
    by (simp add: mult_monom)
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1556
qed
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1557
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1558
lemma is_unit_triv:
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1559
  fixes a :: "'a::field"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1560
  assumes "a \<noteq> 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1561
  shows "is_unit [:a:]"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1562
  using assms by (simp add: is_unit_monom_0 monom_0 [symmetric])
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1563
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1564
lemma is_unit_iff_degree:
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1565
  assumes "p \<noteq> 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1566
  shows "is_unit p \<longleftrightarrow> degree p = 0" (is "?P \<longleftrightarrow> ?Q")
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1567
proof
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1568
  assume ?Q
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1569
  then obtain a where "p = [:a:]" by (rule degree_eq_zeroE)
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1570
  with assms show ?P by (simp add: is_unit_triv)
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1571
next
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1572
  assume ?P
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1573
  then obtain q where "q \<noteq> 0" "p * q = 1" ..
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1574
  then have "degree (p * q) = degree 1"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1575
    by simp
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1576
  with \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close> have "degree p + degree q = 0"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1577
    by (simp add: degree_mult_eq)
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1578
  then show ?Q by simp
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1579
qed
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1580
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1581
lemma is_unit_pCons_iff:
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1582
  "is_unit (pCons a p) \<longleftrightarrow> p = 0 \<and> a \<noteq> 0" (is "?P \<longleftrightarrow> ?Q")
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1583
  by (cases "p = 0") (auto simp add: is_unit_triv is_unit_iff_degree)
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1584
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1585
lemma is_unit_monom_trival:
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1586
  fixes p :: "'a::field poly"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1587
  assumes "is_unit p"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1588
  shows "monom (coeff p (degree p)) 0 = p"
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1589
  using assms by (cases p) (simp_all add: monom_0 is_unit_pCons_iff)
7ed2cde6806d more theorems
haftmann
parents: 60562
diff changeset
  1590
60685
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1591
lemma is_unit_polyE:
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1592
  assumes "is_unit p"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1593
  obtains a where "p = monom a 0" and "a \<noteq> 0"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1594
proof -
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1595
  obtain a q where "p = pCons a q" by (cases p)
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1596
  with assms have "p = [:a:]" and "a \<noteq> 0"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1597
    by (simp_all add: is_unit_pCons_iff)
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1598
  with that show thesis by (simp add: monom_0)
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1599
qed
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1600
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1601
instantiation poly :: (field) normalization_semidom
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1602
begin
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1603
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1604
definition normalize_poly :: "'a poly \<Rightarrow> 'a poly"
62351
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1605
  where "normalize_poly p = smult (inverse (coeff p (degree p))) p"
60685
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1606
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1607
definition unit_factor_poly :: "'a poly \<Rightarrow> 'a poly"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1608
  where "unit_factor_poly p = monom (coeff p (degree p)) 0"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1609
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1610
instance
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1611
proof
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1612
  fix p :: "'a poly"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1613
  show "unit_factor p * normalize p = p"
62351
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1614
    by (cases "p = 0")
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1615
      (simp_all add: normalize_poly_def unit_factor_poly_def,
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1616
      simp only: mult_smult_left [symmetric] smult_monom, simp)
60685
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1617
next
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1618
  show "normalize 0 = (0::'a poly)"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1619
    by (simp add: normalize_poly_def)
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1620
next
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1621
  show "unit_factor 0 = (0::'a poly)"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1622
    by (simp add: unit_factor_poly_def)
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1623
next
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1624
  fix p :: "'a poly"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1625
  assume "is_unit p"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1626
  then obtain a where "p = monom a 0" and "a \<noteq> 0"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1627
    by (rule is_unit_polyE)
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1628
  then show "normalize p = 1"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1629
    by (auto simp add: normalize_poly_def smult_monom degree_monom_eq)
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1630
next
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1631
  fix p q :: "'a poly"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1632
  assume "q \<noteq> 0"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1633
  from \<open>q \<noteq> 0\<close> have "is_unit (monom (coeff q (degree q)) 0)"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1634
    by (auto intro: is_unit_monom_0)
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1635
  then show "is_unit (unit_factor q)"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1636
    by (simp add: unit_factor_poly_def)
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1637
next
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1638
  fix p q :: "'a poly"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1639
  have "monom (coeff (p * q) (degree (p * q))) 0 =
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1640
    monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1641
    by (simp add: monom_0 coeff_degree_mult)
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1642
  then show "unit_factor (p * q) =
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1643
    unit_factor p * unit_factor q"
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1644
    by (simp add: unit_factor_poly_def)
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1645
qed
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1646
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1647
end
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60679
diff changeset
  1648
62351
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1649
lemma unit_factor_monom [simp]:
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1650
  "unit_factor (monom a n) =
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1651
     (if a = 0 then 0 else monom a 0)"
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1652
  by (simp add: unit_factor_poly_def degree_monom_eq)
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1653
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1654
lemma unit_factor_pCons [simp]:
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1655
  "unit_factor (pCons a p) =
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1656
     (if p = 0 then monom a 0 else unit_factor p)"
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1657
  by (simp add: unit_factor_poly_def)
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1658
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1659
lemma normalize_monom [simp]:
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1660
  "normalize (monom a n) =
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1661
     (if a = 0 then 0 else monom 1 n)"
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1662
  by (simp add: normalize_poly_def degree_monom_eq smult_monom)
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  1663
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1664
lemma degree_mod_less:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1665
  "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1666
  using pdivmod_rel [of x y]
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1667
  unfolding pdivmod_rel_def by simp
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1668
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1669
lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1670
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1671
  assume "degree x < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1672
  hence "pdivmod_rel x y 0 x"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1673
    by (simp add: pdivmod_rel_def)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1674
  thus "x div y = 0" by (rule div_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1675
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1676
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1677
lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1678
proof -
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1679
  assume "degree x < degree y"
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1680
  hence "pdivmod_rel x y 0 x"
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1681
    by (simp add: pdivmod_rel_def)
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1682
  thus "x mod y = x" by (rule mod_poly_eq)
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1683
qed
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1684
29659
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1685
lemma pdivmod_rel_smult_left:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1686
  "pdivmod_rel x y q r
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1687
    \<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1688
  unfolding pdivmod_rel_def by (simp add: smult_add_right)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1689
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1690
lemma div_smult_left: "(smult a x) div y = smult a (x div y)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1691
  by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1692
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1693
lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1694
  by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1695
30072
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1696
lemma poly_div_minus_left [simp]:
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1697
  fixes x y :: "'a::field poly"
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1698
  shows "(- x) div y = - (x div y)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
  1699
  using div_smult_left [of "- 1::'a"] by simp
30072
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1700
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1701
lemma poly_mod_minus_left [simp]:
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1702
  fixes x y :: "'a::field poly"
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1703
  shows "(- x) mod y = - (x mod y)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
  1704
  using mod_smult_left [of "- 1::'a"] by simp
30072
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1705
57482
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1706
lemma pdivmod_rel_add_left:
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1707
  assumes "pdivmod_rel x y q r"
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1708
  assumes "pdivmod_rel x' y q' r'"
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1709
  shows "pdivmod_rel (x + x') y (q + q') (r + r')"
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1710
  using assms unfolding pdivmod_rel_def
59557
ebd8ecacfba6 establish unique preferred fact names
haftmann
parents: 59487
diff changeset
  1711
  by (auto simp add: algebra_simps degree_add_less)
57482
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1712
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1713
lemma poly_div_add_left:
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1714
  fixes x y z :: "'a::field poly"
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1715
  shows "(x + y) div z = x div z + y div z"
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1716
  using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel]
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1717
  by (rule div_poly_eq)
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1718
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1719
lemma poly_mod_add_left:
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1720
  fixes x y z :: "'a::field poly"
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1721
  shows "(x + y) mod z = x mod z + y mod z"
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1722
  using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel]
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1723
  by (rule mod_poly_eq)
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1724
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1725
lemma poly_div_diff_left:
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1726
  fixes x y z :: "'a::field poly"
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1727
  shows "(x - y) div z = x div z - y div z"
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1728
  by (simp only: diff_conv_add_uminus poly_div_add_left poly_div_minus_left)
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1729
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1730
lemma poly_mod_diff_left:
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1731
  fixes x y z :: "'a::field poly"
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1732
  shows "(x - y) mod z = x mod z - y mod z"
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1733
  by (simp only: diff_conv_add_uminus poly_mod_add_left poly_mod_minus_left)
60459c3853af add lemmas: polynomial div/mod distribute over addition
huffman
parents: 56544
diff changeset
  1734
29659
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1735
lemma pdivmod_rel_smult_right:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1736
  "\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk>
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1737
    \<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1738
  unfolding pdivmod_rel_def by simp
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1739
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1740
lemma div_smult_right:
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1741
  "a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1742
  by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1743
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1744
lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y"
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1745
  by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
f8d2c03ecfd8 add lemmas about smult
huffman
parents: 29540
diff changeset
  1746
30072
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1747
lemma poly_div_minus_right [simp]:
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1748
  fixes x y :: "'a::field poly"
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1749
  shows "x div (- y) = - (x div y)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
  1750
  using div_smult_right [of "- 1::'a"] by (simp add: nonzero_inverse_minus_eq)
30072
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1751
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1752
lemma poly_mod_minus_right [simp]:
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1753
  fixes x y :: "'a::field poly"
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1754
  shows "x mod (- y) = x mod y"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
  1755
  using mod_smult_right [of "- 1::'a"] by simp
30072
4eecd8b9b6cf add lemmas poly_{div,mod}_minus_{left,right}
huffman
parents: 29987
diff changeset
  1756
29660
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1757
lemma pdivmod_rel_mult:
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1758
  "\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk>
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1759
    \<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1760
apply (cases "z = 0", simp add: pdivmod_rel_def)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1761
apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1762
apply (cases "r = 0")
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1763
apply (cases "r' = 0")
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1764
apply (simp add: pdivmod_rel_def)
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35028
diff changeset
  1765
apply (simp add: pdivmod_rel_def field_simps degree_mult_eq)
29660
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1766
apply (cases "r' = 0")
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1767
apply (simp add: pdivmod_rel_def degree_mult_eq)
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35028
diff changeset
  1768
apply (simp add: pdivmod_rel_def field_simps)
29660
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1769
apply (simp add: degree_mult_eq degree_add_less)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1770
done
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1771
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1772
lemma poly_div_mult_right:
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1773
  fixes x y z :: "'a::field poly"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1774
  shows "x div (y * z) = (x div y) div z"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1775
  by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1776
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1777
lemma poly_mod_mult_right:
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1778
  fixes x y z :: "'a::field poly"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1779
  shows "x mod (y * z) = y * (x div y mod z) + x mod y"
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1780
  by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
d59918e668b7 add lemmas about div/mod with multiplication
huffman
parents: 29659
diff changeset
  1781
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1782
lemma mod_pCons:
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1783
  fixes a and x
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1784
  assumes y: "y \<noteq> 0"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1785
  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
  1786
  shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1787
unfolding b
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1788
apply (rule mod_poly_eq)
29537
50345a0f9df8 rename divmod_poly to pdivmod
huffman
parents: 29480
diff changeset
  1789
apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl])
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1790
done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1791
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1792
definition pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1793
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1794
  "pdivmod p q = (p div q, p mod q)"
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1795
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1796
lemma div_poly_code [code]: 
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1797
  "p div q = fst (pdivmod p q)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1798
  by (simp add: pdivmod_def)
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1799
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1800
lemma mod_poly_code [code]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1801
  "p mod q = snd (pdivmod p q)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1802
  by (simp add: pdivmod_def)
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1803
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1804
lemma pdivmod_0:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1805
  "pdivmod 0 q = (0, 0)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1806
  by (simp add: pdivmod_def)
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  1807
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1808
lemma pdivmod_pCons:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1809
  "pdivmod (pCons a p) q =
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1810
    (if q = 0 then (0, pCons a p) else
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1811
      (let (s, r) = pdivmod p q;
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1812
           b = coeff (pCons a r) (degree q) / coeff q (degree q)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1813
        in (pCons b s, pCons a r - smult b q)))"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1814
  apply (simp add: pdivmod_def Let_def, safe)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1815
  apply (rule div_poly_eq)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1816
  apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1817
  apply (rule mod_poly_eq)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1818
  apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1819
  done
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1820
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1821
lemma pdivmod_fold_coeffs [code]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1822
  "pdivmod p q = (if q = 0 then (0, p)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1823
    else fold_coeffs (\<lambda>a (s, r).
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1824
      let b = coeff (pCons a r) (degree q) / coeff q (degree q)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1825
      in (pCons b s, pCons a r - smult b q)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1826
   ) p (0, 0))"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1827
  apply (cases "q = 0")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1828
  apply (simp add: pdivmod_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1829
  apply (rule sym)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1830
  apply (induct p)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1831
  apply (simp_all add: pdivmod_0 pdivmod_pCons)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1832
  apply (case_tac "a = 0 \<and> p = 0")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1833
  apply (auto simp add: pdivmod_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1834
  done
29980
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1835
17ddfd0c3506 composition of polynomials
huffman
parents: 29979
diff changeset
  1836
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1837
subsection \<open>Order of polynomial roots\<close>
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1838
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1839
definition order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat"
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1840
where
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1841
  "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1842
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1843
lemma coeff_linear_power:
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1844
  fixes a :: "'a::comm_semiring_1"
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1845
  shows "coeff ([:a, 1:] ^ n) n = 1"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1846
apply (induct n, simp_all)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1847
apply (subst coeff_eq_0)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1848
apply (auto intro: le_less_trans degree_power_le)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1849
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1850
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1851
lemma degree_linear_power:
29979
666f5f72dbb5 add some lemmas, cleaned up
huffman
parents: 29977
diff changeset
  1852
  fixes a :: "'a::comm_semiring_1"
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1853
  shows "degree ([:a, 1:] ^ n) = n"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1854
apply (rule order_antisym)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1855
apply (rule ord_le_eq_trans [OF degree_power_le], simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1856
apply (rule le_degree, simp add: coeff_linear_power)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1857
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1858
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1859
lemma order_1: "[:-a, 1:] ^ order a p dvd p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1860
apply (cases "p = 0", simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1861
apply (cases "order a p", simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1862
apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1863
apply (drule not_less_Least, simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1864
apply (fold order_def, simp)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1865
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1866
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1867
lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1868
unfolding order_def
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1869
apply (rule LeastI_ex)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1870
apply (rule_tac x="degree p" in exI)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1871
apply (rule notI)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1872
apply (drule (1) dvd_imp_degree_le)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1873
apply (simp only: degree_linear_power)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1874
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1875
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1876
lemma order:
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1877
  "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1878
by (rule conjI [OF order_1 order_2])
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1879
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1880
lemma order_degree:
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1881
  assumes p: "p \<noteq> 0"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1882
  shows "order a p \<le> degree p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1883
proof -
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1884
  have "order a p = degree ([:-a, 1:] ^ order a p)"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1885
    by (simp only: degree_linear_power)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1886
  also have "\<dots> \<le> degree p"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1887
    using order_1 p by (rule dvd_imp_degree_le)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1888
  finally show ?thesis .
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1889
qed
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1890
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1891
lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1892
apply (cases "p = 0", simp_all)
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1893
apply (rule iffI)
56383
8e7052e9fda4 Cleaned up some messy proofs
paulson <lp15@cam.ac.uk>
parents: 55642
diff changeset
  1894
apply (metis order_2 not_gr0 poly_eq_0_iff_dvd power_0 power_Suc_0 power_one_right)
8e7052e9fda4 Cleaned up some messy proofs
paulson <lp15@cam.ac.uk>
parents: 55642
diff changeset
  1895
unfolding poly_eq_0_iff_dvd
8e7052e9fda4 Cleaned up some messy proofs
paulson <lp15@cam.ac.uk>
parents: 55642
diff changeset
  1896
apply (metis dvd_power dvd_trans order_1)
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1897
done
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1898
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1899
lemma order_0I: "poly p a \<noteq> 0 \<Longrightarrow> order a p = 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1900
  by (subst (asm) order_root) auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  1901
29977
d76b830366bc move polynomial order stuff from Fundamental_Theorem_Algebra to Polynomial
huffman
parents: 29904
diff changeset
  1902
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1903
subsection \<open>GCD of polynomials\<close>
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1904
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1905
instantiation poly :: (field) gcd
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1906
begin
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1907
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1908
function gcd_poly :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1909
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1910
  "gcd (x::'a poly) 0 = smult (inverse (coeff x (degree x))) x"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1911
| "y \<noteq> 0 \<Longrightarrow> gcd (x::'a poly) y = gcd y (x mod y)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1912
by auto
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1913
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1914
termination "gcd :: _ poly \<Rightarrow> _"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1915
by (relation "measure (\<lambda>(x, y). if y = 0 then 0 else Suc (degree y))")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1916
   (auto dest: degree_mod_less)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1917
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1918
declare gcd_poly.simps [simp del]
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1919
58513
0bf0cf1d3547 formal lcm definition for polynomials
haftmann
parents: 58199
diff changeset
  1920
definition lcm_poly :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
0bf0cf1d3547 formal lcm definition for polynomials
haftmann
parents: 58199
diff changeset
  1921
where
0bf0cf1d3547 formal lcm definition for polynomials
haftmann
parents: 58199
diff changeset
  1922
  "lcm_poly a b = a * b div smult (coeff a (degree a) * coeff b (degree b)) (gcd a b)"
0bf0cf1d3547 formal lcm definition for polynomials
haftmann
parents: 58199
diff changeset
  1923
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1924
instance ..
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1925
29451
5f0cb3fa530d new theory of polynomials
huffman
parents:
diff changeset
  1926
end
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1927
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1928
lemma
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1929
  fixes x y :: "_ poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1930
  shows poly_gcd_dvd1 [iff]: "gcd x y dvd x"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1931
    and poly_gcd_dvd2 [iff]: "gcd x y dvd y"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1932
  apply (induct x y rule: gcd_poly.induct)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1933
  apply (simp_all add: gcd_poly.simps)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1934
  apply (fastforce simp add: smult_dvd_iff dest: inverse_zero_imp_zero)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1935
  apply (blast dest: dvd_mod_imp_dvd)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1936
  done
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37770
diff changeset
  1937
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1938
lemma poly_gcd_greatest:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1939
  fixes k x y :: "_ poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1940
  shows "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd gcd x y"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1941
  by (induct x y rule: gcd_poly.induct)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1942
     (simp_all add: gcd_poly.simps dvd_mod dvd_smult)
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1943
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1944
lemma dvd_poly_gcd_iff [iff]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1945
  fixes k x y :: "_ poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1946
  shows "k dvd gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"
60686
ea5bc46c11e6 more algebraic properties for gcd/lcm
haftmann
parents: 60685
diff changeset
  1947
  by (auto intro!: poly_gcd_greatest intro: dvd_trans [of _ "gcd x y"])
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1948
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1949
lemma poly_gcd_monic:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1950
  fixes x y :: "_ poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1951
  shows "coeff (gcd x y) (degree (gcd x y)) =
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1952
    (if x = 0 \<and> y = 0 then 0 else 1)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1953
  by (induct x y rule: gcd_poly.induct)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1954
     (simp_all add: gcd_poly.simps nonzero_imp_inverse_nonzero)
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1955
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1956
lemma poly_gcd_zero_iff [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1957
  fixes x y :: "_ poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1958
  shows "gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1959
  by (simp only: dvd_0_left_iff [symmetric] dvd_poly_gcd_iff)
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1960
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1961
lemma poly_gcd_0_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1962
  "gcd (0::_ poly) 0 = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1963
  by simp
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1964
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1965
lemma poly_dvd_antisym:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1966
  fixes p q :: "'a::idom poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1967
  assumes coeff: "coeff p (degree p) = coeff q (degree q)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1968
  assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1969
proof (cases "p = 0")
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1970
  case True with coeff show "p = q" by simp
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1971
next
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1972
  case False with coeff have "q \<noteq> 0" by auto
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1973
  have degree: "degree p = degree q"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1974
    using \<open>p dvd q\<close> \<open>q dvd p\<close> \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close>
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1975
    by (intro order_antisym dvd_imp_degree_le)
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1976
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1977
  from \<open>p dvd q\<close> obtain a where a: "q = p * a" ..
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1978
  with \<open>q \<noteq> 0\<close> have "a \<noteq> 0" by auto
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  1979
  with degree a \<open>p \<noteq> 0\<close> have "degree a = 0"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1980
    by (simp add: degree_mult_eq)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1981
  with coeff a show "p = q"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1982
    by (cases a, auto split: if_splits)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1983
qed
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  1984
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1985
lemma poly_gcd_unique:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1986
  fixes d x y :: "_ poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1987
  assumes dvd1: "d dvd x" and dvd2: "d dvd y"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1988
    and greatest: "\<And>k. k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd d"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1989
    and monic: "coeff d (degree d) = (if x = 0 \<and> y = 0 then 0 else 1)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1990
  shows "gcd x y = d"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1991
proof -
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1992
  have "coeff (gcd x y) (degree (gcd x y)) = coeff d (degree d)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1993
    by (simp_all add: poly_gcd_monic monic)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1994
  moreover have "gcd x y dvd d"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1995
    using poly_gcd_dvd1 poly_gcd_dvd2 by (rule greatest)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1996
  moreover have "d dvd gcd x y"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1997
    using dvd1 dvd2 by (rule poly_gcd_greatest)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1998
  ultimately show ?thesis
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  1999
    by (rule poly_dvd_antisym)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2000
qed
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  2001
62351
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  2002
instance poly :: (field) semiring_gcd
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2003
proof
62351
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  2004
  fix p q :: "'a::field poly"
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  2005
  show "normalize (gcd p q) = gcd p q"
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  2006
    by (induct p q rule: gcd_poly.induct)
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  2007
      (simp_all add: gcd_poly.simps normalize_poly_def)
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  2008
  show "lcm p q = normalize (p * q) div gcd p q"
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  2009
    by (simp add: coeff_degree_mult div_smult_left div_smult_right lcm_poly_def normalize_poly_def)
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  2010
      (metis (no_types, lifting) div_smult_right inverse_mult_distrib inverse_zero mult.commute pdivmod_rel pdivmod_rel_def smult_eq_0_iff)
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  2011
qed simp_all
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2012
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2013
lemma poly_gcd_1_left [simp]: "gcd 1 y = (1 :: _ poly)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2014
by (rule poly_gcd_unique) simp_all
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  2015
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2016
lemma poly_gcd_1_right [simp]: "gcd x 1 = (1 :: _ poly)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2017
by (rule poly_gcd_unique) simp_all
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2018
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2019
lemma poly_gcd_minus_left [simp]: "gcd (- x) y = gcd x (y :: _ poly)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2020
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  2021
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2022
lemma poly_gcd_minus_right [simp]: "gcd x (- y) = gcd x (y :: _ poly)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2023
by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  2024
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2025
lemma poly_gcd_code [code]:
62351
fd049b54ad68 gcd instances for poly
haftmann
parents: 62128
diff changeset
  2026
  "gcd x y = (if y = 0 then normalize x else gcd y (x mod (y :: _ poly)))"
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2027
  by (simp add: gcd_poly.simps)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2028
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2029
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2030
subsection \<open>Additional induction rules on polynomials\<close>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2031
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2032
text \<open>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2033
  An induction rule for induction over the roots of a polynomial with a certain property. 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2034
  (e.g. all positive roots)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2035
\<close>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2036
lemma poly_root_induct [case_names 0 no_roots root]:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2037
  fixes p :: "'a :: idom poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2038
  assumes "Q 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2039
  assumes "\<And>p. (\<And>a. P a \<Longrightarrow> poly p a \<noteq> 0) \<Longrightarrow> Q p"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2040
  assumes "\<And>a p. P a \<Longrightarrow> Q p \<Longrightarrow> Q ([:a, -1:] * p)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2041
  shows   "Q p"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2042
proof (induction "degree p" arbitrary: p rule: less_induct)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2043
  case (less p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2044
  show ?case
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2045
  proof (cases "p = 0")
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2046
    assume nz: "p \<noteq> 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2047
    show ?case
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2048
    proof (cases "\<exists>a. P a \<and> poly p a = 0")
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2049
      case False
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2050
      thus ?thesis by (intro assms(2)) blast
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2051
    next
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2052
      case True
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2053
      then obtain a where a: "P a" "poly p a = 0" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2054
        by blast
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2055
      hence "-[:-a, 1:] dvd p" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2056
        by (subst minus_dvd_iff) (simp add: poly_eq_0_iff_dvd)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2057
      then obtain q where q: "p = [:a, -1:] * q" by (elim dvdE) simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2058
      with nz have q_nz: "q \<noteq> 0" by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2059
      have "degree p = Suc (degree q)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2060
        by (subst q, subst degree_mult_eq) (simp_all add: q_nz)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2061
      hence "Q q" by (intro less) simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2062
      from a(1) and this have "Q ([:a, -1:] * q)" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2063
        by (rule assms(3))
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2064
      with q show ?thesis by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2065
    qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2066
  qed (simp add: assms(1))
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2067
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2068
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2069
lemma dropWhile_replicate_append: 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2070
  "dropWhile (op= a) (replicate n a @ ys) = dropWhile (op= a) ys"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2071
  by (induction n) simp_all
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2072
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2073
lemma Poly_append_replicate_0: "Poly (xs @ replicate n 0) = Poly xs"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2074
  by (subst coeffs_eq_iff) (simp_all add: strip_while_def dropWhile_replicate_append)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2075
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2076
text \<open>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2077
  An induction rule for simultaneous induction over two polynomials, 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2078
  prepending one coefficient in each step.
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2079
\<close>
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2080
lemma poly_induct2 [case_names 0 pCons]:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2081
  assumes "P 0 0" "\<And>a p b q. P p q \<Longrightarrow> P (pCons a p) (pCons b q)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2082
  shows   "P p q"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2083
proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2084
  def n \<equiv> "max (length (coeffs p)) (length (coeffs q))"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2085
  def xs \<equiv> "coeffs p @ (replicate (n - length (coeffs p)) 0)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2086
  def ys \<equiv> "coeffs q @ (replicate (n - length (coeffs q)) 0)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2087
  have "length xs = length ys" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2088
    by (simp add: xs_def ys_def n_def)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2089
  hence "P (Poly xs) (Poly ys)" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2090
    by (induction rule: list_induct2) (simp_all add: assms)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2091
  also have "Poly xs = p" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2092
    by (simp add: xs_def Poly_append_replicate_0)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2093
  also have "Poly ys = q" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2094
    by (simp add: ys_def Poly_append_replicate_0)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2095
  finally show ?thesis .
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2096
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2097
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2098
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60429
diff changeset
  2099
subsection \<open>Composition of polynomials\<close>
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  2100
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2101
(* Several lemmas contributed by René Thiemann and Akihisa Yamada *)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2102
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2103
definition pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2104
where
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2105
  "pcompose p q = fold_coeffs (\<lambda>a c. [:a:] + q * c) p 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2106
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2107
notation pcompose (infixl "\<circ>\<^sub>p" 71)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2108
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2109
lemma pcompose_0 [simp]:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2110
  "pcompose 0 q = 0"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2111
  by (simp add: pcompose_def)
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2112
  
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2113
lemma pcompose_pCons:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2114
  "pcompose (pCons a p) q = [:a:] + q * pcompose p q"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2115
  by (cases "p = 0 \<and> a = 0") (auto simp add: pcompose_def)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2116
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2117
lemma pcompose_1:
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2118
  fixes p :: "'a :: comm_semiring_1 poly"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2119
  shows "pcompose 1 p = 1"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2120
  unfolding one_poly_def by (auto simp: pcompose_pCons)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2121
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2122
lemma poly_pcompose:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2123
  "poly (pcompose p q) x = poly p (poly q x)"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2124
  by (induct p) (simp_all add: pcompose_pCons)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2125
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2126
lemma degree_pcompose_le:
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2127
  "degree (pcompose p q) \<le> degree p * degree q"
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2128
apply (induct p, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2129
apply (simp add: pcompose_pCons, clarify)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2130
apply (rule degree_add_le, simp)
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2131
apply (rule order_trans [OF degree_mult_le], simp)
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  2132
done
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  2133
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2134
lemma pcompose_add:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2135
  fixes p q r :: "'a :: {comm_semiring_0, ab_semigroup_add} poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2136
  shows "pcompose (p + q) r = pcompose p r + pcompose q r"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2137
proof (induction p q rule: poly_induct2)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2138
  case (pCons a p b q)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2139
  have "pcompose (pCons a p + pCons b q) r = 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2140
          [:a + b:] + r * pcompose p r + r * pcompose q r"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2141
    by (simp_all add: pcompose_pCons pCons.IH algebra_simps)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2142
  also have "[:a + b:] = [:a:] + [:b:]" by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2143
  also have "\<dots> + r * pcompose p r + r * pcompose q r = 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2144
                 pcompose (pCons a p) r + pcompose (pCons b q) r"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2145
    by (simp only: pcompose_pCons add_ac)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2146
  finally show ?case .
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2147
qed simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2148
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2149
lemma pcompose_uminus:
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2150
  fixes p r :: "'a :: comm_ring poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2151
  shows "pcompose (-p) r = -pcompose p r"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2152
  by (induction p) (simp_all add: pcompose_pCons)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2153
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2154
lemma pcompose_diff:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2155
  fixes p q r :: "'a :: comm_ring poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2156
  shows "pcompose (p - q) r = pcompose p r - pcompose q r"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2157
  using pcompose_add[of p "-q"] by (simp add: pcompose_uminus)
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2158
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2159
lemma pcompose_smult:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2160
  fixes p r :: "'a :: comm_semiring_0 poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2161
  shows "pcompose (smult a p) r = smult a (pcompose p r)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2162
  by (induction p) 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2163
     (simp_all add: pcompose_pCons pcompose_add smult_add_right)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2164
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2165
lemma pcompose_mult:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2166
  fixes p q r :: "'a :: comm_semiring_0 poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2167
  shows "pcompose (p * q) r = pcompose p r * pcompose q r"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2168
  by (induction p arbitrary: q)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2169
     (simp_all add: pcompose_add pcompose_smult pcompose_pCons algebra_simps)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2170
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2171
lemma pcompose_assoc: 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2172
  "pcompose p (pcompose q r :: 'a :: comm_semiring_0 poly ) =
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2173
     pcompose (pcompose p q) r"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2174
  by (induction p arbitrary: q) 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2175
     (simp_all add: pcompose_pCons pcompose_add pcompose_mult)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2176
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2177
lemma pcompose_idR[simp]:
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2178
  fixes p :: "'a :: comm_semiring_1 poly"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2179
  shows "pcompose p [: 0, 1 :] = p"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2180
  by (induct p; simp add: pcompose_pCons)
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2181
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2182
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2183
(* The remainder of this section and the next were contributed by Wenda Li *)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2184
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2185
lemma degree_mult_eq_0:
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2186
  fixes p q:: "'a :: semidom poly"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2187
  shows "degree (p*q) = 0 \<longleftrightarrow> p=0 \<or> q=0 \<or> (p\<noteq>0 \<and> q\<noteq>0 \<and> degree p =0 \<and> degree q =0)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2188
by (auto simp add:degree_mult_eq)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2189
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2190
lemma pcompose_const[simp]:"pcompose [:a:] q = [:a:]" by (subst pcompose_pCons,simp) 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2191
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2192
lemma pcompose_0': "pcompose p 0 = [:coeff p 0:]"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2193
  by (induct p) (auto simp add:pcompose_pCons)
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2194
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2195
lemma degree_pcompose:
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2196
  fixes p q:: "'a::semidom poly"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2197
  shows "degree (pcompose p q) = degree p * degree q"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2198
proof (induct p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2199
  case 0
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2200
  thus ?case by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2201
next
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2202
  case (pCons a p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2203
  have "degree (q * pcompose p q) = 0 \<Longrightarrow> ?case" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2204
    proof (cases "p=0")
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2205
      case True
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2206
      thus ?thesis by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2207
    next
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2208
      case False assume "degree (q * pcompose p q) = 0"
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2209
      hence "degree q=0 \<or> pcompose p q=0" by (auto simp add: degree_mult_eq_0)
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2210
      moreover have "\<lbrakk>pcompose p q=0;degree q\<noteq>0\<rbrakk> \<Longrightarrow> False" using pCons.hyps(2) \<open>p\<noteq>0\<close> 
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2211
        proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2212
          assume "pcompose p q=0" "degree q\<noteq>0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2213
          hence "degree p=0" using pCons.hyps(2) by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2214
          then obtain a1 where "p=[:a1:]"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2215
            by (metis degree_pCons_eq_if old.nat.distinct(2) pCons_cases)
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2216
          thus False using \<open>pcompose p q=0\<close> \<open>p\<noteq>0\<close> by auto
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2217
        qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2218
      ultimately have "degree (pCons a p) * degree q=0" by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2219
      moreover have "degree (pcompose (pCons a p) q) = 0" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2220
        proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2221
          have" 0 = max (degree [:a:]) (degree (q*pcompose p q))"
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2222
            using \<open>degree (q * pcompose p q) = 0\<close> by simp
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2223
          also have "... \<ge> degree ([:a:] + q * pcompose p q)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2224
            by (rule degree_add_le_max)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2225
          finally show ?thesis by (auto simp add:pcompose_pCons)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2226
        qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2227
      ultimately show ?thesis by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2228
    qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2229
  moreover have "degree (q * pcompose p q)>0 \<Longrightarrow> ?case" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2230
    proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2231
      assume asm:"0 < degree (q * pcompose p q)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2232
      hence "p\<noteq>0" "q\<noteq>0" "pcompose p q\<noteq>0" by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2233
      have "degree (pcompose (pCons a p) q) = degree ( q * pcompose p q)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2234
        unfolding pcompose_pCons
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2235
        using degree_add_eq_right[of "[:a:]" ] asm by auto       
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2236
      thus ?thesis 
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2237
        using pCons.hyps(2) degree_mult_eq[OF \<open>q\<noteq>0\<close> \<open>pcompose p q\<noteq>0\<close>] by auto
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2238
    qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2239
  ultimately show ?case by blast
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2240
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2241
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2242
lemma pcompose_eq_0:
62128
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2243
  fixes p q:: "'a :: semidom poly"
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2244
  assumes "pcompose p q = 0" "degree q > 0" 
3201ddb00097 Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
eberlm
parents: 62072
diff changeset
  2245
  shows "p = 0"
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2246
proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2247
  have "degree p=0" using assms degree_pcompose[of p q] by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2248
  then obtain a where "p=[:a:]" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2249
    by (metis degree_pCons_eq_if gr0_conv_Suc neq0_conv pCons_cases)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2250
  hence "a=0" using assms(1) by auto
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2251
  thus ?thesis using \<open>p=[:a:]\<close> by simp
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2252
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2253
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2254
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2255
subsection \<open>Leading coefficient\<close>
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2256
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2257
definition lead_coeff:: "'a::zero poly \<Rightarrow> 'a" where
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2258
  "lead_coeff p= coeff p (degree p)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2259
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2260
lemma lead_coeff_pCons[simp]:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2261
    "p\<noteq>0 \<Longrightarrow>lead_coeff (pCons a p) = lead_coeff p"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2262
    "p=0 \<Longrightarrow> lead_coeff (pCons a p) = a"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2263
unfolding lead_coeff_def by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2264
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2265
lemma lead_coeff_0[simp]:"lead_coeff 0 =0" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2266
  unfolding lead_coeff_def by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2267
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2268
lemma lead_coeff_mult:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2269
   fixes p q::"'a ::idom poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2270
   shows "lead_coeff (p * q) = lead_coeff p * lead_coeff q"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2271
by (unfold lead_coeff_def,cases "p=0 \<or> q=0",auto simp add:coeff_mult_degree_sum degree_mult_eq)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2272
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2273
lemma lead_coeff_add_le:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2274
  assumes "degree p < degree q"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2275
  shows "lead_coeff (p+q) = lead_coeff q" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2276
using assms unfolding lead_coeff_def
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2277
by (metis coeff_add coeff_eq_0 monoid_add_class.add.left_neutral degree_add_eq_right)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2278
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2279
lemma lead_coeff_minus:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2280
  "lead_coeff (-p) = - lead_coeff p"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2281
by (metis coeff_minus degree_minus lead_coeff_def)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2282
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2283
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2284
lemma lead_coeff_comp:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2285
  fixes p q:: "'a::idom poly"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2286
  assumes "degree q > 0" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2287
  shows "lead_coeff (pcompose p q) = lead_coeff p * lead_coeff q ^ (degree p)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2288
proof (induct p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2289
  case 0
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2290
  thus ?case unfolding lead_coeff_def by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2291
next
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2292
  case (pCons a p)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2293
  have "degree ( q * pcompose p q) = 0 \<Longrightarrow> ?case"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2294
    proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2295
      assume "degree ( q * pcompose p q) = 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2296
      hence "pcompose p q = 0" by (metis assms degree_0 degree_mult_eq_0 neq0_conv)
62072
bf3d9f113474 isabelle update_cartouches -c -t;
wenzelm
parents: 62067
diff changeset
  2297
      hence "p=0" using pcompose_eq_0[OF _ \<open>degree q > 0\<close>] by simp
62065
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2298
      thus ?thesis by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2299
    qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2300
  moreover have "degree ( q * pcompose p q) > 0 \<Longrightarrow> ?case" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2301
    proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2302
      assume "degree ( q * pcompose p q) > 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2303
      hence "lead_coeff (pcompose (pCons a p) q) =lead_coeff ( q * pcompose p q)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2304
        by (auto simp add:pcompose_pCons lead_coeff_add_le)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2305
      also have "... = lead_coeff q * (lead_coeff p * lead_coeff q ^ degree p)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2306
        using pCons.hyps(2) lead_coeff_mult[of q "pcompose p q"] by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2307
      also have "... = lead_coeff p * lead_coeff q ^ (degree p + 1)"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2308
        by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2309
      finally show ?thesis by auto
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2310
    qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2311
  ultimately show ?case by blast
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2312
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2313
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2314
lemma lead_coeff_smult: 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2315
  "lead_coeff (smult c p :: 'a :: idom poly) = c * lead_coeff p"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2316
proof -
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2317
  have "smult c p = [:c:] * p" by simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2318
  also have "lead_coeff \<dots> = c * lead_coeff p" 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2319
    by (subst lead_coeff_mult) simp_all
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2320
  finally show ?thesis .
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2321
qed
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2322
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2323
lemma lead_coeff_1 [simp]: "lead_coeff 1 = 1"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2324
  by (simp add: lead_coeff_def)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2325
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2326
lemma lead_coeff_of_nat [simp]:
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2327
  "lead_coeff (of_nat n) = (of_nat n :: 'a :: {comm_semiring_1,semiring_char_0})"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2328
  by (induction n) (simp_all add: lead_coeff_def of_nat_poly)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2329
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2330
lemma lead_coeff_numeral [simp]: 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2331
  "lead_coeff (numeral n) = numeral n"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2332
  unfolding lead_coeff_def
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2333
  by (subst of_nat_numeral [symmetric], subst of_nat_poly) simp
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2334
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2335
lemma lead_coeff_power: 
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2336
  "lead_coeff (p ^ n :: 'a :: idom poly) = lead_coeff p ^ n"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2337
  by (induction n) (simp_all add: lead_coeff_mult)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2338
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2339
lemma lead_coeff_nonzero: "p \<noteq> 0 \<Longrightarrow> lead_coeff p \<noteq> 0"
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2340
  by (simp add: lead_coeff_def)
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2341
  
1546a042e87b Added some facts about polynomials
eberlm
parents: 61945
diff changeset
  2342
  
52380
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2343
3cc46b8cca5e lifting for primitive definitions;
haftmann
parents: 49962
diff changeset
  2344
no_notation cCons (infixr "##" 65)
31663
5eb82f064630 smult_dvd lemmas; polynomial gcd
huffman
parents: 31021
diff changeset
  2345
29478
4a2482e16934 code generation for polynomials
huffman
parents: 29475
diff changeset
  2346
end