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