(* Title: HOL/Library/Polynomial.thy
Author: Brian Huffman
Author: Clemens Ballarin
Author: Florian Haftmann
*)
header {* Polynomials as type over a ring structure *}
theory Polynomial
imports Main GCD
begin
subsection {* Auxiliary: operations for lists (later) representing coefficients *}
definition strip_while :: "('a \ bool) \ 'a list \ 'a list"
where
"strip_while P = rev \ dropWhile P \ rev"
lemma strip_while_Nil [simp]:
"strip_while P [] = []"
by (simp add: strip_while_def)
lemma strip_while_append [simp]:
"\ P x \ strip_while P (xs @ [x]) = xs @ [x]"
by (simp add: strip_while_def)
lemma strip_while_append_rec [simp]:
"P x \ strip_while P (xs @ [x]) = strip_while P xs"
by (simp add: strip_while_def)
lemma strip_while_Cons [simp]:
"\ P x \ strip_while P (x # xs) = x # strip_while P xs"
by (induct xs rule: rev_induct) (simp_all add: strip_while_def)
lemma strip_while_eq_Nil [simp]:
"strip_while P xs = [] \ (\x\set xs. P x)"
by (simp add: strip_while_def)
lemma strip_while_eq_Cons_rec:
"strip_while P (x # xs) = x # strip_while P xs \ \ (P x \ (\x\set xs. P x))"
by (induct xs rule: rev_induct) (simp_all add: strip_while_def)
lemma strip_while_not_last [simp]:
"\ P (last xs) \ strip_while P xs = xs"
by (cases xs rule: rev_cases) simp_all
lemma split_strip_while_append:
fixes xs :: "'a list"
obtains ys zs :: "'a list"
where "strip_while P xs = ys" and "\x\set zs. P x" and "xs = ys @ zs"
proof (rule that)
show "strip_while P xs = strip_while P xs" ..
show "\x\set (rev (takeWhile P (rev xs))). P x" by (simp add: takeWhile_eq_all_conv [symmetric])
have "rev xs = rev (strip_while P xs @ rev (takeWhile P (rev xs)))"
by (simp add: strip_while_def)
then show "xs = strip_while P xs @ rev (takeWhile P (rev xs))"
by (simp only: rev_is_rev_conv)
qed
definition nth_default :: "'a \ 'a list \ nat \ 'a"
where
"nth_default x xs n = (if n < length xs then xs ! n else x)"
lemma nth_default_Nil [simp]:
"nth_default y [] n = y"
by (simp add: nth_default_def)
lemma nth_default_Cons_0 [simp]:
"nth_default y (x # xs) 0 = x"
by (simp add: nth_default_def)
lemma nth_default_Cons_Suc [simp]:
"nth_default y (x # xs) (Suc n) = nth_default y xs n"
by (simp add: nth_default_def)
lemma nth_default_map_eq:
"f y = x \ nth_default x (map f xs) n = f (nth_default y xs n)"
by (simp add: nth_default_def)
lemma nth_default_strip_while_eq [simp]:
"nth_default x (strip_while (HOL.eq x) xs) n = nth_default x xs n"
proof -
from split_strip_while_append obtain ys zs
where "strip_while (HOL.eq x) xs = ys" and "\z\set zs. x = z" and "xs = ys @ zs" by blast
then show ?thesis by (simp add: nth_default_def not_less nth_append)
qed
definition cCons :: "'a::zero \ 'a list \ 'a list" (infixr "##" 65)
where
"x ## xs = (if xs = [] \ x = 0 then [] else x # xs)"
lemma cCons_0_Nil_eq [simp]:
"0 ## [] = []"
by (simp add: cCons_def)
lemma cCons_Cons_eq [simp]:
"x ## y # ys = x # y # ys"
by (simp add: cCons_def)
lemma cCons_append_Cons_eq [simp]:
"x ## xs @ y # ys = x # xs @ y # ys"
by (simp add: cCons_def)
lemma cCons_not_0_eq [simp]:
"x \ 0 \ x ## xs = x # xs"
by (simp add: cCons_def)
lemma strip_while_not_0_Cons_eq [simp]:
"strip_while (\x. x = 0) (x # xs) = x ## strip_while (\x. x = 0) xs"
proof (cases "x = 0")
case False then show ?thesis by simp
next
case True show ?thesis
proof (induct xs rule: rev_induct)
case Nil with True show ?case by simp
next
case (snoc y ys) then show ?case
by (cases "y = 0") (simp_all add: append_Cons [symmetric] del: append_Cons)
qed
qed
lemma tl_cCons [simp]:
"tl (x ## xs) = xs"
by (simp add: cCons_def)
subsection {* Almost everywhere zero functions *}
definition almost_everywhere_zero :: "(nat \ 'a::zero) \ bool"
where
"almost_everywhere_zero f \ (\n. \i>n. f i = 0)"
lemma almost_everywhere_zeroI:
"(\i. i > n \ f i = 0) \ almost_everywhere_zero f"
by (auto simp add: almost_everywhere_zero_def)
lemma almost_everywhere_zeroE:
assumes "almost_everywhere_zero f"
obtains n where "\i. i > n \ f i = 0"
proof -
from assms have "\n. \i>n. f i = 0" by (simp add: almost_everywhere_zero_def)
then obtain n where "\i. i > n \ f i = 0" by blast
with that show thesis .
qed
lemma almost_everywhere_zero_case_nat:
assumes "almost_everywhere_zero f"
shows "almost_everywhere_zero (case_nat a f)"
using assms
by (auto intro!: almost_everywhere_zeroI elim!: almost_everywhere_zeroE split: nat.split)
blast
lemma almost_everywhere_zero_Suc:
assumes "almost_everywhere_zero f"
shows "almost_everywhere_zero (\n. f (Suc n))"
proof -
from assms obtain n where "\i. i > n \ f i = 0" by (erule almost_everywhere_zeroE)
then have "\i. i > n \ f (Suc i) = 0" by auto
then show ?thesis by (rule almost_everywhere_zeroI)
qed
subsection {* Definition of type @{text poly} *}
typedef 'a poly = "{f :: nat \ 'a::zero. almost_everywhere_zero f}"
morphisms coeff Abs_poly
unfolding almost_everywhere_zero_def by auto
setup_lifting (no_code) type_definition_poly
lemma poly_eq_iff: "p = q \ (\n. coeff p n = coeff q n)"
by (simp add: coeff_inject [symmetric] fun_eq_iff)
lemma poly_eqI: "(\n. coeff p n = coeff q n) \ p = q"
by (simp add: poly_eq_iff)
lemma coeff_almost_everywhere_zero:
"almost_everywhere_zero (coeff p)"
using coeff [of p] by simp
subsection {* Degree of a polynomial *}
definition degree :: "'a::zero poly \ nat"
where
"degree p = (LEAST n. \i>n. coeff p i = 0)"
lemma coeff_eq_0:
assumes "degree p < n"
shows "coeff p n = 0"
proof -
from coeff_almost_everywhere_zero
have "\n. \i>n. coeff p i = 0" by (blast intro: almost_everywhere_zeroE)
then have "\i>degree p. coeff p i = 0"
unfolding degree_def by (rule LeastI_ex)
with assms show ?thesis by simp
qed
lemma le_degree: "coeff p n \ 0 \ n \ degree p"
by (erule contrapos_np, rule coeff_eq_0, simp)
lemma degree_le: "\i>n. coeff p i = 0 \ degree p \ n"
unfolding degree_def by (erule Least_le)
lemma less_degree_imp: "n < degree p \ \i>n. coeff p i \ 0"
unfolding degree_def by (drule not_less_Least, simp)
subsection {* The zero polynomial *}
instantiation poly :: (zero) zero
begin
lift_definition zero_poly :: "'a poly"
is "\_. 0" by (rule almost_everywhere_zeroI) simp
instance ..
end
lemma coeff_0 [simp]:
"coeff 0 n = 0"
by transfer rule
lemma degree_0 [simp]:
"degree 0 = 0"
by (rule order_antisym [OF degree_le le0]) simp
lemma leading_coeff_neq_0:
assumes "p \ 0"
shows "coeff p (degree p) \ 0"
proof (cases "degree p")
case 0
from `p \ 0` have "\n. coeff p n \ 0"
by (simp add: poly_eq_iff)
then obtain n where "coeff p n \ 0" ..
hence "n \ degree p" by (rule le_degree)
with `coeff p n \ 0` and `degree p = 0`
show "coeff p (degree p) \ 0" by simp
next
case (Suc n)
from `degree p = Suc n` have "n < degree p" by simp
hence "\i>n. coeff p i \ 0" by (rule less_degree_imp)
then obtain i where "n < i" and "coeff p i \ 0" by fast
from `degree p = Suc n` and `n < i` have "degree p \ i" by simp
also from `coeff p i \ 0` have "i \ degree p" by (rule le_degree)
finally have "degree p = i" .
with `coeff p i \ 0` show "coeff p (degree p) \ 0" by simp
qed
lemma leading_coeff_0_iff [simp]:
"coeff p (degree p) = 0 \ p = 0"
by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
subsection {* List-style constructor for polynomials *}
lift_definition pCons :: "'a::zero \ 'a poly \ 'a poly"
is "\a p. case_nat a (coeff p)"
using coeff_almost_everywhere_zero by (rule almost_everywhere_zero_case_nat)
lemmas coeff_pCons = pCons.rep_eq
lemma coeff_pCons_0 [simp]:
"coeff (pCons a p) 0 = a"
by transfer simp
lemma coeff_pCons_Suc [simp]:
"coeff (pCons a p) (Suc n) = coeff p n"
by (simp add: coeff_pCons)
lemma degree_pCons_le:
"degree (pCons a p) \ Suc (degree p)"
by (rule degree_le) (simp add: coeff_eq_0 coeff_pCons split: nat.split)
lemma degree_pCons_eq:
"p \ 0 \ degree (pCons a p) = Suc (degree p)"
apply (rule order_antisym [OF degree_pCons_le])
apply (rule le_degree, simp)
done
lemma degree_pCons_0:
"degree (pCons a 0) = 0"
apply (rule order_antisym [OF _ le0])
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
done
lemma degree_pCons_eq_if [simp]:
"degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
apply (cases "p = 0", simp_all)
apply (rule order_antisym [OF _ le0])
apply (rule degree_le, simp add: coeff_pCons split: nat.split)
apply (rule order_antisym [OF degree_pCons_le])
apply (rule le_degree, simp)
done
lemma pCons_0_0 [simp]:
"pCons 0 0 = 0"
by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
lemma pCons_eq_iff [simp]:
"pCons a p = pCons b q \ a = b \ p = q"
proof safe
assume "pCons a p = pCons b q"
then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
then show "a = b" by simp
next
assume "pCons a p = pCons b q"
then have "\n. coeff (pCons a p) (Suc n) =
coeff (pCons b q) (Suc n)" by simp
then show "p = q" by (simp add: poly_eq_iff)
qed
lemma pCons_eq_0_iff [simp]:
"pCons a p = 0 \ a = 0 \ p = 0"
using pCons_eq_iff [of a p 0 0] by simp
lemma pCons_cases [cases type: poly]:
obtains (pCons) a q where "p = pCons a q"
proof
show "p = pCons (coeff p 0) (Abs_poly (\n. coeff p (Suc n)))"
by transfer
(simp add: Abs_poly_inverse almost_everywhere_zero_Suc fun_eq_iff split: nat.split)
qed
lemma pCons_induct [case_names 0 pCons, induct type: poly]:
assumes zero: "P 0"
assumes pCons: "\a p. a \ 0 \ p \ 0 \ P p \ P (pCons a p)"
shows "P p"
proof (induct p rule: measure_induct_rule [where f=degree])
case (less p)
obtain a q where "p = pCons a q" by (rule pCons_cases)
have "P q"
proof (cases "q = 0")
case True
then show "P q" by (simp add: zero)
next
case False
then have "degree (pCons a q) = Suc (degree q)"
by (rule degree_pCons_eq)
then have "degree q < degree p"
using `p = pCons a q` by simp
then show "P q"
by (rule less.hyps)
qed
have "P (pCons a q)"
proof (cases "a \ 0 \ q \ 0")
case True
with `P q` show ?thesis by (auto intro: pCons)
next
case False
with zero show ?thesis by simp
qed
then show ?case
using `p = pCons a q` by simp
qed
subsection {* List-style syntax for polynomials *}
syntax
"_poly" :: "args \ 'a poly" ("[:(_):]")
translations
"[:x, xs:]" == "CONST pCons x [:xs:]"
"[:x:]" == "CONST pCons x 0"
"[:x:]" <= "CONST pCons x (_constrain 0 t)"
subsection {* Representation of polynomials by lists of coefficients *}
primrec Poly :: "'a::zero list \ 'a poly"
where
[code_post]: "Poly [] = 0"
| [code_post]: "Poly (a # as) = pCons a (Poly as)"
lemma Poly_replicate_0 [simp]:
"Poly (replicate n 0) = 0"
by (induct n) simp_all
lemma Poly_eq_0:
"Poly as = 0 \ (\n. as = replicate n 0)"
by (induct as) (auto simp add: Cons_replicate_eq)
definition coeffs :: "'a poly \ 'a::zero list"
where
"coeffs p = (if p = 0 then [] else map (\i. coeff p i) [0 ..< Suc (degree p)])"
lemma coeffs_eq_Nil [simp]:
"coeffs p = [] \ p = 0"
by (simp add: coeffs_def)
lemma not_0_coeffs_not_Nil:
"p \ 0 \ coeffs p \ []"
by simp
lemma coeffs_0_eq_Nil [simp]:
"coeffs 0 = []"
by simp
lemma coeffs_pCons_eq_cCons [simp]:
"coeffs (pCons a p) = a ## coeffs p"
proof -
{ fix ms :: "nat list" and f :: "nat \ 'a" and x :: "'a"
assume "\m\set ms. m > 0"
then have "map (case_nat x f) ms = map f (map (\n. n - 1) ms)"
by (induct ms) (auto, metis Suc_pred' nat.cases(2)) }
note * = this
show ?thesis
by (simp add: coeffs_def * upt_conv_Cons coeff_pCons map_decr_upt One_nat_def del: upt_Suc)
qed
lemma not_0_cCons_eq [simp]:
"p \ 0 \ a ## coeffs p = a # coeffs p"
by (simp add: cCons_def)
lemma Poly_coeffs [simp, code abstype]:
"Poly (coeffs p) = p"
by (induct p) auto
lemma coeffs_Poly [simp]:
"coeffs (Poly as) = strip_while (HOL.eq 0) as"
proof (induct as)
case Nil then show ?case by simp
next
case (Cons a as)
have "(\n. as \ replicate n 0) \ (\a\set as. a \ 0)"
using replicate_length_same [of as 0] by (auto dest: sym [of _ as])
with Cons show ?case by auto
qed
lemma last_coeffs_not_0:
"p \ 0 \ last (coeffs p) \ 0"
by (induct p) (auto simp add: cCons_def)
lemma strip_while_coeffs [simp]:
"strip_while (HOL.eq 0) (coeffs p) = coeffs p"
by (cases "p = 0") (auto dest: last_coeffs_not_0 intro: strip_while_not_last)
lemma coeffs_eq_iff:
"p = q \ coeffs p = coeffs q" (is "?P \ ?Q")
proof
assume ?P then show ?Q by simp
next
assume ?Q
then have "Poly (coeffs p) = Poly (coeffs q)" by simp
then show ?P by simp
qed
lemma coeff_Poly_eq:
"coeff (Poly xs) n = nth_default 0 xs n"
apply (induct xs arbitrary: n) apply simp_all
by (metis nat.cases not0_implies_Suc nth_default_Cons_0 nth_default_Cons_Suc pCons.rep_eq)
lemma nth_default_coeffs_eq:
"nth_default 0 (coeffs p) = coeff p"
by (simp add: fun_eq_iff coeff_Poly_eq [symmetric])
lemma [code]:
"coeff p = nth_default 0 (coeffs p)"
by (simp add: nth_default_coeffs_eq)
lemma coeffs_eqI:
assumes coeff: "\n. coeff p n = nth_default 0 xs n"
assumes zero: "xs \ [] \ last xs \ 0"
shows "coeffs p = xs"
proof -
from coeff have "p = Poly xs" by (simp add: poly_eq_iff coeff_Poly_eq)
with zero show ?thesis by simp (cases xs, simp_all)
qed
lemma degree_eq_length_coeffs [code]:
"degree p = length (coeffs p) - 1"
by (simp add: coeffs_def)
lemma length_coeffs_degree:
"p \ 0 \ length (coeffs p) = Suc (degree p)"
by (induct p) (auto simp add: cCons_def)
lemma [code abstract]:
"coeffs 0 = []"
by (fact coeffs_0_eq_Nil)
lemma [code abstract]:
"coeffs (pCons a p) = a ## coeffs p"
by (fact coeffs_pCons_eq_cCons)
instantiation poly :: ("{zero, equal}") equal
begin
definition
[code]: "HOL.equal (p::'a poly) q \ HOL.equal (coeffs p) (coeffs q)"
instance proof
qed (simp add: equal equal_poly_def coeffs_eq_iff)
end
lemma [code nbe]:
"HOL.equal (p :: _ poly) p \ True"
by (fact equal_refl)
definition is_zero :: "'a::zero poly \ bool"
where
[code]: "is_zero p \ List.null (coeffs p)"
lemma is_zero_null [code_abbrev]:
"is_zero p \ p = 0"
by (simp add: is_zero_def null_def)
subsection {* Fold combinator for polynomials *}
definition fold_coeffs :: "('a::zero \ 'b \ 'b) \ 'a poly \ 'b \ 'b"
where
"fold_coeffs f p = foldr f (coeffs p)"
lemma fold_coeffs_0_eq [simp]:
"fold_coeffs f 0 = id"
by (simp add: fold_coeffs_def)
lemma fold_coeffs_pCons_eq [simp]:
"f 0 = id \ fold_coeffs f (pCons a p) = f a \ fold_coeffs f p"
by (simp add: fold_coeffs_def cCons_def fun_eq_iff)
lemma fold_coeffs_pCons_0_0_eq [simp]:
"fold_coeffs f (pCons 0 0) = id"
by (simp add: fold_coeffs_def)
lemma fold_coeffs_pCons_coeff_not_0_eq [simp]:
"a \ 0 \ fold_coeffs f (pCons a p) = f a \ fold_coeffs f p"
by (simp add: fold_coeffs_def)
lemma fold_coeffs_pCons_not_0_0_eq [simp]:
"p \ 0 \ fold_coeffs f (pCons a p) = f a \ fold_coeffs f p"
by (simp add: fold_coeffs_def)
subsection {* Canonical morphism on polynomials -- evaluation *}
definition poly :: "'a::comm_semiring_0 poly \ 'a \ 'a"
where
"poly p = fold_coeffs (\a f x. a + x * f x) p (\x. 0)" -- {* The Horner Schema *}
lemma poly_0 [simp]:
"poly 0 x = 0"
by (simp add: poly_def)
lemma poly_pCons [simp]:
"poly (pCons a p) x = a + x * poly p x"
by (cases "p = 0 \ a = 0") (auto simp add: poly_def)
subsection {* Monomials *}
lift_definition monom :: "'a \ nat \ 'a::zero poly"
is "\a m n. if m = n then a else 0"
by (auto intro!: almost_everywhere_zeroI)
lemma coeff_monom [simp]:
"coeff (monom a m) n = (if m = n then a else 0)"
by transfer rule
lemma monom_0:
"monom a 0 = pCons a 0"
by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
lemma monom_Suc:
"monom a (Suc n) = pCons 0 (monom a n)"
by (rule poly_eqI) (simp add: coeff_pCons split: nat.split)
lemma monom_eq_0 [simp]: "monom 0 n = 0"
by (rule poly_eqI) simp
lemma monom_eq_0_iff [simp]: "monom a n = 0 \ a = 0"
by (simp add: poly_eq_iff)
lemma monom_eq_iff [simp]: "monom a n = monom b n \ a = b"
by (simp add: poly_eq_iff)
lemma degree_monom_le: "degree (monom a n) \ n"
by (rule degree_le, simp)
lemma degree_monom_eq: "a \ 0 \ degree (monom a n) = n"
apply (rule order_antisym [OF degree_monom_le])
apply (rule le_degree, simp)
done
lemma coeffs_monom [code abstract]:
"coeffs (monom a n) = (if a = 0 then [] else replicate n 0 @ [a])"
by (induct n) (simp_all add: monom_0 monom_Suc)
lemma fold_coeffs_monom [simp]:
"a \ 0 \ fold_coeffs f (monom a n) = f 0 ^^ n \ f a"
by (simp add: fold_coeffs_def coeffs_monom fun_eq_iff)
lemma poly_monom:
fixes a x :: "'a::{comm_semiring_1}"
shows "poly (monom a n) x = a * x ^ n"
by (cases "a = 0", simp_all)
(induct n, simp_all add: mult.left_commute poly_def)
subsection {* Addition and subtraction *}
instantiation poly :: (comm_monoid_add) comm_monoid_add
begin
lift_definition plus_poly :: "'a poly \ 'a poly \ 'a poly"
is "\p q n. coeff p n + coeff q n"
proof (rule almost_everywhere_zeroI)
fix q p :: "'a poly" and i
assume "max (degree q) (degree p) < i"
then show "coeff p i + coeff q i = 0"
by (simp add: coeff_eq_0)
qed
lemma coeff_add [simp]:
"coeff (p + q) n = coeff p n + coeff q n"
by (simp add: plus_poly.rep_eq)
instance proof
fix p q r :: "'a poly"
show "(p + q) + r = p + (q + r)"
by (simp add: poly_eq_iff add_assoc)
show "p + q = q + p"
by (simp add: poly_eq_iff add_commute)
show "0 + p = p"
by (simp add: poly_eq_iff)
qed
end
instance poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
proof
fix p q r :: "'a poly"
assume "p + q = p + r" thus "q = r"
by (simp add: poly_eq_iff)
qed
instantiation poly :: (ab_group_add) ab_group_add
begin
lift_definition uminus_poly :: "'a poly \ 'a poly"
is "\p n. - coeff p n"
proof (rule almost_everywhere_zeroI)
fix p :: "'a poly" and i
assume "degree p < i"
then show "- coeff p i = 0"
by (simp add: coeff_eq_0)
qed
lift_definition minus_poly :: "'a poly \ 'a poly \ 'a poly"
is "\p q n. coeff p n - coeff q n"
proof (rule almost_everywhere_zeroI)
fix q p :: "'a poly" and i
assume "max (degree q) (degree p) < i"
then show "coeff p i - coeff q i = 0"
by (simp add: coeff_eq_0)
qed
lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
by (simp add: uminus_poly.rep_eq)
lemma coeff_diff [simp]:
"coeff (p - q) n = coeff p n - coeff q n"
by (simp add: minus_poly.rep_eq)
instance proof
fix p q :: "'a poly"
show "- p + p = 0"
by (simp add: poly_eq_iff)
show "p - q = p + - q"
by (simp add: poly_eq_iff)
qed
end
lemma add_pCons [simp]:
"pCons a p + pCons b q = pCons (a + b) (p + q)"
by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
lemma minus_pCons [simp]:
"- pCons a p = pCons (- a) (- p)"
by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
lemma diff_pCons [simp]:
"pCons a p - pCons b q = pCons (a - b) (p - q)"
by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
lemma degree_add_le_max: "degree (p + q) \ max (degree p) (degree q)"
by (rule degree_le, auto simp add: coeff_eq_0)
lemma degree_add_le:
"\degree p \ n; degree q \ n\ \ degree (p + q) \ n"
by (auto intro: order_trans degree_add_le_max)
lemma degree_add_less:
"\degree p < n; degree q < n\ \ degree (p + q) < n"
by (auto intro: le_less_trans degree_add_le_max)
lemma degree_add_eq_right:
"degree p < degree q \ degree (p + q) = degree q"
apply (cases "q = 0", simp)
apply (rule order_antisym)
apply (simp add: degree_add_le)
apply (rule le_degree)
apply (simp add: coeff_eq_0)
done
lemma degree_add_eq_left:
"degree q < degree p \ degree (p + q) = degree p"
using degree_add_eq_right [of q p]
by (simp add: add_commute)
lemma degree_minus [simp]: "degree (- p) = degree p"
unfolding degree_def by simp
lemma degree_diff_le_max: "degree (p - q) \ max (degree p) (degree q)"
using degree_add_le [where p=p and q="-q"]
by simp
lemma degree_diff_le:
"\degree p \ n; degree q \ n\ \ degree (p - q) \ n"
using degree_add_le [of p n "- q"] by simp
lemma degree_diff_less:
"\degree p < n; degree q < n\ \ degree (p - q) < n"
using degree_add_less [of p n "- q"] by simp
lemma add_monom: "monom a n + monom b n = monom (a + b) n"
by (rule poly_eqI) simp
lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
by (rule poly_eqI) simp
lemma minus_monom: "- monom a n = monom (-a) n"
by (rule poly_eqI) simp
lemma coeff_setsum: "coeff (\x\A. p x) i = (\x\A. coeff (p x) i)"
by (cases "finite A", induct set: finite, simp_all)
lemma monom_setsum: "monom (\x\A. a x) n = (\x\A. monom (a x) n)"
by (rule poly_eqI) (simp add: coeff_setsum)
fun plus_coeffs :: "'a::comm_monoid_add list \ 'a list \ 'a list"
where
"plus_coeffs xs [] = xs"
| "plus_coeffs [] ys = ys"
| "plus_coeffs (x # xs) (y # ys) = (x + y) ## plus_coeffs xs ys"
lemma coeffs_plus_eq_plus_coeffs [code abstract]:
"coeffs (p + q) = plus_coeffs (coeffs p) (coeffs q)"
proof -
{ fix xs ys :: "'a list" and n
have "nth_default 0 (plus_coeffs xs ys) n = nth_default 0 xs n + nth_default 0 ys n"
proof (induct xs ys arbitrary: n rule: plus_coeffs.induct)
case (3 x xs y ys n) then show ?case by (cases n) (auto simp add: cCons_def)
qed simp_all }
note * = this
{ fix xs ys :: "'a list"
assume "xs \ [] \ last xs \ 0" and "ys \ [] \ last ys \ 0"
moreover assume "plus_coeffs xs ys \ []"
ultimately have "last (plus_coeffs xs ys) \ 0"
proof (induct xs ys rule: plus_coeffs.induct)
case (3 x xs y ys) then show ?case by (auto simp add: cCons_def) metis
qed simp_all }
note ** = this
show ?thesis
apply (rule coeffs_eqI)
apply (simp add: * nth_default_coeffs_eq)
apply (rule **)
apply (auto dest: last_coeffs_not_0)
done
qed
lemma coeffs_uminus [code abstract]:
"coeffs (- p) = map (\a. - a) (coeffs p)"
by (rule coeffs_eqI)
(simp_all add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq)
lemma [code]:
fixes p q :: "'a::ab_group_add poly"
shows "p - q = p + - q"
by (fact ab_add_uminus_conv_diff)
lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
apply (induct p arbitrary: q, simp)
apply (case_tac q, simp, simp add: algebra_simps)
done
lemma poly_minus [simp]:
fixes x :: "'a::comm_ring"
shows "poly (- p) x = - poly p x"
by (induct p) simp_all
lemma poly_diff [simp]:
fixes x :: "'a::comm_ring"
shows "poly (p - q) x = poly p x - poly q x"
using poly_add [of p "- q" x] by simp
lemma poly_setsum: "poly (\k\A. p k) x = (\k\A. poly (p k) x)"
by (induct A rule: infinite_finite_induct) simp_all
subsection {* Multiplication by a constant, polynomial multiplication and the unit polynomial *}
lift_definition smult :: "'a::comm_semiring_0 \ 'a poly \ 'a poly"
is "\a p n. a * coeff p n"
proof (rule almost_everywhere_zeroI)
fix a :: 'a and p :: "'a poly" and i
assume "degree p < i"
then show "a * coeff p i = 0"
by (simp add: coeff_eq_0)
qed
lemma coeff_smult [simp]:
"coeff (smult a p) n = a * coeff p n"
by (simp add: smult.rep_eq)
lemma degree_smult_le: "degree (smult a p) \ degree p"
by (rule degree_le, simp add: coeff_eq_0)
lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
by (rule poly_eqI, simp add: mult_assoc)
lemma smult_0_right [simp]: "smult a 0 = 0"
by (rule poly_eqI, simp)
lemma smult_0_left [simp]: "smult 0 p = 0"
by (rule poly_eqI, simp)
lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
by (rule poly_eqI, simp)
lemma smult_add_right:
"smult a (p + q) = smult a p + smult a q"
by (rule poly_eqI, simp add: algebra_simps)
lemma smult_add_left:
"smult (a + b) p = smult a p + smult b p"
by (rule poly_eqI, simp add: algebra_simps)
lemma smult_minus_right [simp]:
"smult (a::'a::comm_ring) (- p) = - smult a p"
by (rule poly_eqI, simp)
lemma smult_minus_left [simp]:
"smult (- a::'a::comm_ring) p = - smult a p"
by (rule poly_eqI, simp)
lemma smult_diff_right:
"smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
by (rule poly_eqI, simp add: algebra_simps)
lemma smult_diff_left:
"smult (a - b::'a::comm_ring) p = smult a p - smult b p"
by (rule poly_eqI, simp add: algebra_simps)
lemmas smult_distribs =
smult_add_left smult_add_right
smult_diff_left smult_diff_right
lemma smult_pCons [simp]:
"smult a (pCons b p) = pCons (a * b) (smult a p)"
by (rule poly_eqI, simp add: coeff_pCons split: nat.split)
lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
by (induct n, simp add: monom_0, simp add: monom_Suc)
lemma degree_smult_eq [simp]:
fixes a :: "'a::idom"
shows "degree (smult a p) = (if a = 0 then 0 else degree p)"
by (cases "a = 0", simp, simp add: degree_def)
lemma smult_eq_0_iff [simp]:
fixes a :: "'a::idom"
shows "smult a p = 0 \ a = 0 \ p = 0"
by (simp add: poly_eq_iff)
lemma coeffs_smult [code abstract]:
fixes p :: "'a::idom poly"
shows "coeffs (smult a p) = (if a = 0 then [] else map (Groups.times a) (coeffs p))"
by (rule coeffs_eqI)
(auto simp add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq)
instantiation poly :: (comm_semiring_0) comm_semiring_0
begin
definition
"p * q = fold_coeffs (\a p. smult a q + pCons 0 p) p 0"
lemma mult_poly_0_left: "(0::'a poly) * q = 0"
by (simp add: times_poly_def)
lemma mult_pCons_left [simp]:
"pCons a p * q = smult a q + pCons 0 (p * q)"
by (cases "p = 0 \ a = 0") (auto simp add: times_poly_def)
lemma mult_poly_0_right: "p * (0::'a poly) = 0"
by (induct p) (simp add: mult_poly_0_left, simp)
lemma mult_pCons_right [simp]:
"p * pCons a q = smult a p + pCons 0 (p * q)"
by (induct p) (simp add: mult_poly_0_left, simp add: algebra_simps)
lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
lemma mult_smult_left [simp]:
"smult a p * q = smult a (p * q)"
by (induct p) (simp add: mult_poly_0, simp add: smult_add_right)
lemma mult_smult_right [simp]:
"p * smult a q = smult a (p * q)"
by (induct q) (simp add: mult_poly_0, simp add: smult_add_right)
lemma mult_poly_add_left:
fixes p q r :: "'a poly"
shows "(p + q) * r = p * r + q * r"
by (induct r) (simp add: mult_poly_0, simp add: smult_distribs algebra_simps)
instance proof
fix p q r :: "'a poly"
show 0: "0 * p = 0"
by (rule mult_poly_0_left)
show "p * 0 = 0"
by (rule mult_poly_0_right)
show "(p + q) * r = p * r + q * r"
by (rule mult_poly_add_left)
show "(p * q) * r = p * (q * r)"
by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)
show "p * q = q * p"
by (induct p, simp add: mult_poly_0, simp)
qed
end
instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
lemma coeff_mult:
"coeff (p * q) n = (\i\n. coeff p i * coeff q (n-i))"
proof (induct p arbitrary: n)
case 0 show ?case by simp
next
case (pCons a p n) thus ?case
by (cases n, simp, simp add: setsum_atMost_Suc_shift
del: setsum_atMost_Suc)
qed
lemma degree_mult_le: "degree (p * q) \ degree p + degree q"
apply (rule degree_le)
apply (induct p)
apply simp
apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
done
lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)
instantiation poly :: (comm_semiring_1) comm_semiring_1
begin
definition one_poly_def:
"1 = pCons 1 0"
instance proof
fix p :: "'a poly" show "1 * p = p"
unfolding one_poly_def by simp
next
show "0 \ (1::'a poly)"
unfolding one_poly_def by simp
qed
end
instance poly :: (comm_semiring_1_cancel) comm_semiring_1_cancel ..
instance poly :: (comm_ring) comm_ring ..
instance poly :: (comm_ring_1) comm_ring_1 ..
lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
unfolding one_poly_def
by (simp add: coeff_pCons split: nat.split)
lemma degree_1 [simp]: "degree 1 = 0"
unfolding one_poly_def
by (rule degree_pCons_0)
lemma coeffs_1_eq [simp, code abstract]:
"coeffs 1 = [1]"
by (simp add: one_poly_def)
lemma degree_power_le:
"degree (p ^ n) \ degree p * n"
by (induct n) (auto intro: order_trans degree_mult_le)
lemma poly_smult [simp]:
"poly (smult a p) x = a * poly p x"
by (induct p, simp, simp add: algebra_simps)
lemma poly_mult [simp]:
"poly (p * q) x = poly p x * poly q x"
by (induct p, simp_all, simp add: algebra_simps)
lemma poly_1 [simp]:
"poly 1 x = 1"
by (simp add: one_poly_def)
lemma poly_power [simp]:
fixes p :: "'a::{comm_semiring_1} poly"
shows "poly (p ^ n) x = poly p x ^ n"
by (induct n) simp_all
subsection {* Lemmas about divisibility *}
lemma dvd_smult: "p dvd q \ p dvd smult a q"
proof -
assume "p dvd q"
then obtain k where "q = p * k" ..
then have "smult a q = p * smult a k" by simp
then show "p dvd smult a q" ..
qed
lemma dvd_smult_cancel:
fixes a :: "'a::field"
shows "p dvd smult a q \ a \ 0 \ p dvd q"
by (drule dvd_smult [where a="inverse a"]) simp
lemma dvd_smult_iff:
fixes a :: "'a::field"
shows "a \ 0 \ p dvd smult a q \ p dvd q"
by (safe elim!: dvd_smult dvd_smult_cancel)
lemma smult_dvd_cancel:
"smult a p dvd q \