# Theory UnivPoly

Up to index of Isabelle/HOL/HOL-Algebra

theory UnivPoly
imports Module
begin

    (*
      Title:     HOL/Algebra/UnivPoly.thy
      Id:        $Id: UnivPoly.thy,v 1.22 2005/08/17 15:02:16 ballarin Exp $
      Author:    Clemens Ballarin, started 9 December 1996
      Copyright: Clemens Ballarin
    *)

    header {* Univariate Polynomials *}

    theory UnivPoly imports Module begin

    text {*
      Polynomials are formalised as modules with additional operations for
      extracting coefficients from polynomials and for obtaining monomials
      from coefficients and exponents (record @{text "up_ring"}).  The
      carrier set is a set of bounded functions from Nat to the
      coefficient domain.  Bounded means that these functions return zero
      above a certain bound (the degree).  There is a chapter on the
      formalisation of polynomials in the PhD thesis \cite{Ballarin:1999},
      which was implemented with axiomatic type classes.  This was later
      ported to Locales.
    *}

    subsection {* The Constructor for Univariate Polynomials *}

    text {*
      Functions with finite support.
    *}

    locale bound =
      fixes z :: 'a
        and n :: nat
        and f :: "nat => 'a"
      assumes bound: "!!m. n < m ==> f m = z"

    declare bound.intro [intro!]
      and bound.bound [dest]

    lemma bound_below:
      assumes bound: "bound z m f" and nonzero: "f n ≠ z" shows "n ≤ m"
    proof (rule classical)
      assume "~ ?thesis"
      then have "m < n" by arith
      with bound have "f n = z" ..
      with nonzero show ?thesis by contradiction
    qed

    record ('a, 'p) up_ring = "('a, 'p) module" +
      monom :: "['a, nat] => 'p"
      coeff :: "['p, nat] => 'a"

    constdefs (structure R)
      up :: "('a, 'm) ring_scheme => (nat => 'a) set"
      "up R == {f. f ∈ UNIV -> carrier R & (EX n. bound \<zero> n f)}"
      UP :: "('a, 'm) ring_scheme => ('a, nat => 'a) up_ring"
      "UP R == (|
        carrier = up R,
        mult = (%p:up R. %q:up R. %n. \<Oplus>i ∈ {..n}. p i ⊗ q (n-i)),
        one = (%i. if i=0 then \<one> else \<zero>),
        zero = (%i. \<zero>),
        add = (%p:up R. %q:up R. %i. p i ⊕ q i),
        smult = (%a:carrier R. %p:up R. %i. a ⊗ p i),
        monom = (%a:carrier R. %n i. if i=n then a else \<zero>),
        coeff = (%p:up R. %n. p n) |)"

    text {*
      Properties of the set of polynomials @{term up}.
    *}

    lemma mem_upI [intro]:
      "[| !!n. f n ∈ carrier R; EX n. bound (zero R) n f |] ==> f ∈ up R"
      by (simp add: up_def Pi_def)

    lemma mem_upD [dest]:
      "f ∈ up R ==> f n ∈ carrier R"
      by (simp add: up_def Pi_def)

    lemma (in cring) bound_upD [dest]:
      "f ∈ up R ==> EX n. bound \<zero> n f"
      by (simp add: up_def)

    lemma (in cring) up_one_closed:
       "(%n. if n = 0 then \<one> else \<zero>) ∈ up R"
      using up_def by force

    lemma (in cring) up_smult_closed:
      "[| a ∈ carrier R; p ∈ up R |] ==> (%i. a ⊗ p i) ∈ up R"
      by force

    lemma (in cring) up_add_closed:
      "[| p ∈ up R; q ∈ up R |] ==> (%i. p i ⊕ q i) ∈ up R"
    proof
      fix n
      assume "p ∈ up R" and "q ∈ up R"
      then show "p n ⊕ q n ∈ carrier R"
        by auto
    next
      assume UP: "p ∈ up R" "q ∈ up R"
      show "EX n. bound \<zero> n (%i. p i ⊕ q i)"
      proof -
        from UP obtain n where boundn: "bound \<zero> n p" by fast
        from UP obtain m where boundm: "bound \<zero> m q" by fast
        have "bound \<zero> (max n m) (%i. p i ⊕ q i)"
        proof
          fix i
          assume "max n m < i"
          with boundn and boundm and UP show "p i ⊕ q i = \<zero>" by fastsimp
        qed
        then show ?thesis ..
      qed
    qed

    lemma (in cring) up_a_inv_closed:
      "p ∈ up R ==> (%i. \<ominus> (p i)) ∈ up R"
    proof
      assume R: "p ∈ up R"
      then obtain n where "bound \<zero> n p" by auto
      then have "bound \<zero> n (%i. \<ominus> p i)" by auto
      then show "EX n. bound \<zero> n (%i. \<ominus> p i)" by auto
    qed auto

    lemma (in cring) up_mult_closed:
      "[| p ∈ up R; q ∈ up R |] ==>
      (%n. \<Oplus>i ∈ {..n}. p i ⊗ q (n-i)) ∈ up R"
    proof
      fix n
      assume "p ∈ up R" "q ∈ up R"
      then show "(\<Oplus>i ∈ {..n}. p i ⊗ q (n-i)) ∈ carrier R"
        by (simp add: mem_upD  funcsetI)
    next
      assume UP: "p ∈ up R" "q ∈ up R"
      show "EX n. bound \<zero> n (%n. \<Oplus>i ∈ {..n}. p i ⊗ q (n-i))"
      proof -
        from UP obtain n where boundn: "bound \<zero> n p" by fast
        from UP obtain m where boundm: "bound \<zero> m q" by fast
        have "bound \<zero> (n + m) (%n. \<Oplus>i ∈ {..n}. p i ⊗ q (n - i))"
        proof
          fix k assume bound: "n + m < k"
          {
            fix i
            have "p i ⊗ q (k-i) = \<zero>"
            proof (cases "n < i")
              case True
              with boundn have "p i = \<zero>" by auto
              moreover from UP have "q (k-i) ∈ carrier R" by auto
              ultimately show ?thesis by simp
            next
              case False
              with bound have "m < k-i" by arith
              with boundm have "q (k-i) = \<zero>" by auto
              moreover from UP have "p i ∈ carrier R" by auto
              ultimately show ?thesis by simp
            qed
          }
          then show "(\<Oplus>i ∈ {..k}. p i ⊗ q (k-i)) = \<zero>"
            by (simp add: Pi_def)
        qed
        then show ?thesis by fast
      qed
    qed

    subsection {* Effect of operations on coefficients *}

    locale UP = struct R + struct P +
      defines P_def: "P == UP R"

    locale UP_cring = UP + cring R

    locale UP_domain = UP_cring + "domain" R

    text {*
      Temporarily declare @{thm [locale=UP] P_def} as simp rule.
    *}

    declare (in UP) P_def [simp]

    lemma (in UP_cring) coeff_monom [simp]:
      "a ∈ carrier R ==>
      coeff P (monom P a m) n = (if m=n then a else \<zero>)"
    proof -
      assume R: "a ∈ carrier R"
      then have "(%n. if n = m then a else \<zero>) ∈ up R"
        using up_def by force
      with R show ?thesis by (simp add: UP_def)
    qed

    lemma (in UP_cring) coeff_zero [simp]:
      "coeff P \<zero>P n = \<zero>"
      by (auto simp add: UP_def)

    lemma (in UP_cring) coeff_one [simp]:
      "coeff P \<one>P n = (if n=0 then \<one> else \<zero>)"
      using up_one_closed by (simp add: UP_def)

    lemma (in UP_cring) coeff_smult [simp]:
      "[| a ∈ carrier R; p ∈ carrier P |] ==>
      coeff P (a \<odot>P p) n = a ⊗ coeff P p n"
      by (simp add: UP_def up_smult_closed)

    lemma (in UP_cring) coeff_add [simp]:
      "[| p ∈ carrier P; q ∈ carrier P |] ==>
      coeff P (p ⊕P q) n = coeff P p n ⊕ coeff P q n"
      by (simp add: UP_def up_add_closed)

    lemma (in UP_cring) coeff_mult [simp]:
      "[| p ∈ carrier P; q ∈ carrier P |] ==>
      coeff P (p ⊗P q) n = (\<Oplus>i ∈ {..n}. coeff P p i ⊗ coeff P q (n-i))"
      by (simp add: UP_def up_mult_closed)

    lemma (in UP) up_eqI:
      assumes prem: "!!n. coeff P p n = coeff P q n"
        and R: "p ∈ carrier P" "q ∈ carrier P"
      shows "p = q"
    proof
      fix x
      from prem and R show "p x = q x" by (simp add: UP_def)
    qed

    subsection {* Polynomials form a commutative ring. *}

    text {* Operations are closed over @{term P}. *}

    lemma (in UP_cring) UP_mult_closed [simp]:
      "[| p ∈ carrier P; q ∈ carrier P |] ==> p ⊗P q ∈ carrier P"
      by (simp add: UP_def up_mult_closed)

    lemma (in UP_cring) UP_one_closed [simp]:
      "\<one>P ∈ carrier P"
      by (simp add: UP_def up_one_closed)

    lemma (in UP_cring) UP_zero_closed [intro, simp]:
      "\<zero>P ∈ carrier P"
      by (auto simp add: UP_def)

    lemma (in UP_cring) UP_a_closed [intro, simp]:
      "[| p ∈ carrier P; q ∈ carrier P |] ==> p ⊕P q ∈ carrier P"
      by (simp add: UP_def up_add_closed)

    lemma (in UP_cring) monom_closed [simp]:
      "a ∈ carrier R ==> monom P a n ∈ carrier P"
      by (auto simp add: UP_def up_def Pi_def)

    lemma (in UP_cring) UP_smult_closed [simp]:
      "[| a ∈ carrier R; p ∈ carrier P |] ==> a \<odot>P p ∈ carrier P"
      by (simp add: UP_def up_smult_closed)

    lemma (in UP) coeff_closed [simp]:
      "p ∈ carrier P ==> coeff P p n ∈ carrier R"
      by (auto simp add: UP_def)

    declare (in UP) P_def [simp del]

    text {* Algebraic ring properties *}

    lemma (in UP_cring) UP_a_assoc:
      assumes R: "p ∈ carrier P" "q ∈ carrier P" "r ∈ carrier P"
      shows "(p ⊕P q) ⊕P r = p ⊕P (q ⊕P r)"
      by (rule up_eqI, simp add: a_assoc R, simp_all add: R)

    lemma (in UP_cring) UP_l_zero [simp]:
      assumes R: "p ∈ carrier P"
      shows "\<zero>P ⊕P p = p"
      by (rule up_eqI, simp_all add: R)

    lemma (in UP_cring) UP_l_neg_ex:
      assumes R: "p ∈ carrier P"
      shows "EX q : carrier P. q ⊕P p = \<zero>P"
    proof -
      let ?q = "%i. \<ominus> (p i)"
      from R have closed: "?q ∈ carrier P"
        by (simp add: UP_def P_def up_a_inv_closed)
      from R have coeff: "!!n. coeff P ?q n = \<ominus> (coeff P p n)"
        by (simp add: UP_def P_def up_a_inv_closed)
      show ?thesis
      proof
        show "?q ⊕P p = \<zero>P"
          by (auto intro!: up_eqI simp add: R closed coeff R.l_neg)
      qed (rule closed)
    qed

    lemma (in UP_cring) UP_a_comm:
      assumes R: "p ∈ carrier P" "q ∈ carrier P"
      shows "p ⊕P q = q ⊕P p"
      by (rule up_eqI, simp add: a_comm R, simp_all add: R)

    lemma (in UP_cring) UP_m_assoc:
      assumes R: "p ∈ carrier P" "q ∈ carrier P" "r ∈ carrier P"
      shows "(p ⊗P q) ⊗P r = p ⊗P (q ⊗P r)"
    proof (rule up_eqI)
      fix n
      {
        fix k and a b c :: "nat=>'a"
        assume R: "a ∈ UNIV -> carrier R" "b ∈ UNIV -> carrier R"
          "c ∈ UNIV -> carrier R"
        then have "k <= n ==>
          (\<Oplus>j ∈ {..k}. (\<Oplus>i ∈ {..j}. a i ⊗ b (j-i)) ⊗ c (n-j)) =
          (\<Oplus>j ∈ {..k}. a j ⊗ (\<Oplus>i ∈ {..k-j}. b i ⊗ c (n-j-i)))"
          (concl is "?eq k")
        proof (induct k)
          case 0 then show ?case by (simp add: Pi_def m_assoc)
        next
          case (Suc k)
          then have "k <= n" by arith
          then have "?eq k" by (rule Suc)
          with R show ?case
            by (simp cong: finsum_cong
                 add: Suc_diff_le Pi_def l_distr r_distr m_assoc)
              (simp cong: finsum_cong add: Pi_def a_ac finsum_ldistr m_assoc)
        qed
      }
      with R show "coeff P ((p ⊗P q) ⊗P r) n = coeff P (p ⊗P (q ⊗P r)) n"
        by (simp add: Pi_def)
    qed (simp_all add: R)

    lemma (in UP_cring) UP_l_one [simp]:
      assumes R: "p ∈ carrier P"
      shows "\<one>P ⊗P p = p"
    proof (rule up_eqI)
      fix n
      show "coeff P (\<one>P ⊗P p) n = coeff P p n"
      proof (cases n)
        case 0 with R show ?thesis by simp
      next
        case Suc with R show ?thesis
          by (simp del: finsum_Suc add: finsum_Suc2 Pi_def)
      qed
    qed (simp_all add: R)

    lemma (in UP_cring) UP_l_distr:
      assumes R: "p ∈ carrier P" "q ∈ carrier P" "r ∈ carrier P"
      shows "(p ⊕P q) ⊗P r = (p ⊗P r) ⊕P (q ⊗P r)"
      by (rule up_eqI) (simp add: l_distr R Pi_def, simp_all add: R)

    lemma (in UP_cring) UP_m_comm:
      assumes R: "p ∈ carrier P" "q ∈ carrier P"
      shows "p ⊗P q = q ⊗P p"
    proof (rule up_eqI)
      fix n
      {
        fix k and a b :: "nat=>'a"
        assume R: "a ∈ UNIV -> carrier R" "b ∈ UNIV -> carrier R"
        then have "k <= n ==>
          (\<Oplus>i ∈ {..k}. a i ⊗ b (n-i)) =
          (\<Oplus>i ∈ {..k}. a (k-i) ⊗ b (i+n-k))"
          (concl is "?eq k")
        proof (induct k)
          case 0 then show ?case by (simp add: Pi_def)
        next
          case (Suc k) then show ?case
            by (subst (2) finsum_Suc2) (simp add: Pi_def a_comm)+
        qed
      }
      note l = this
      from R show "coeff P (p ⊗P q) n =  coeff P (q ⊗P p) n"
        apply (simp add: Pi_def)
        apply (subst l)
        apply (auto simp add: Pi_def)
        apply (simp add: m_comm)
        done
    qed (simp_all add: R)

    theorem (in UP_cring) UP_cring:
      "cring P"
      by (auto intro!: cringI abelian_groupI comm_monoidI UP_a_assoc UP_l_zero
        UP_l_neg_ex UP_a_comm UP_m_assoc UP_l_one UP_m_comm UP_l_distr)

    lemma (in UP_cring) UP_ring:
      (* preliminary,
         we want "UP_ring R P ==> ring P", not "UP_cring R P ==> ring P" *)
      "ring P"
      by (auto intro: ring.intro cring.axioms UP_cring)

    lemma (in UP_cring) UP_a_inv_closed [intro, simp]:
      "p ∈ carrier P ==> \<ominus>P p ∈ carrier P"
      by (rule abelian_group.a_inv_closed
        [OF ring.is_abelian_group [OF UP_ring]])

    lemma (in UP_cring) coeff_a_inv [simp]:
      assumes R: "p ∈ carrier P"
      shows "coeff P (\<ominus>P p) n = \<ominus> (coeff P p n)"
    proof -
      from R coeff_closed UP_a_inv_closed have
        "coeff P (\<ominus>P p) n = \<ominus> coeff P p n ⊕ (coeff P p n ⊕ coeff P (\<ominus>P p) n)"
        by algebra
      also from R have "... =  \<ominus> (coeff P p n)"
        by (simp del: coeff_add add: coeff_add [THEN sym]
          abelian_group.r_neg [OF ring.is_abelian_group [OF UP_ring]])
      finally show ?thesis .
    qed

    text {*
      Interpretation of lemmas from @{term cring}.  Saves lifting 43
      lemmas manually.
    *}

    interpretation UP_cring < cring P
      using UP_cring
      by - (erule cring.axioms)+

    subsection {* Polynomials form an Algebra *}

    lemma (in UP_cring) UP_smult_l_distr:
      "[| a ∈ carrier R; b ∈ carrier R; p ∈ carrier P |] ==>
      (a ⊕ b) \<odot>P p = a \<odot>P p ⊕P b \<odot>P p"
      by (rule up_eqI) (simp_all add: R.l_distr)

    lemma (in UP_cring) UP_smult_r_distr:
      "[| a ∈ carrier R; p ∈ carrier P; q ∈ carrier P |] ==>
      a \<odot>P (p ⊕P q) = a \<odot>P p ⊕P a \<odot>P q"
      by (rule up_eqI) (simp_all add: R.r_distr)

    lemma (in UP_cring) UP_smult_assoc1:
          "[| a ∈ carrier R; b ∈ carrier R; p ∈ carrier P |] ==>
          (a ⊗ b) \<odot>P p = a \<odot>P (b \<odot>P p)"
      by (rule up_eqI) (simp_all add: R.m_assoc)

    lemma (in UP_cring) UP_smult_one [simp]:
          "p ∈ carrier P ==> \<one> \<odot>P p = p"
      by (rule up_eqI) simp_all

    lemma (in UP_cring) UP_smult_assoc2:
      "[| a ∈ carrier R; p ∈ carrier P; q ∈ carrier P |] ==>
      (a \<odot>P p) ⊗P q = a \<odot>P (p ⊗P q)"
      by (rule up_eqI) (simp_all add: R.finsum_rdistr R.m_assoc Pi_def)

    text {*
      Interpretation of lemmas from @{term algebra}.
    *}

    lemma (in cring) cring:
      "cring R"
      by (fast intro: cring.intro prems)

    lemma (in UP_cring) UP_algebra:
      "algebra R P"
      by (auto intro: algebraI R.cring UP_cring UP_smult_l_distr UP_smult_r_distr
        UP_smult_assoc1 UP_smult_assoc2)

    interpretation UP_cring < algebra R P
      using UP_algebra
      by - (erule algebra.axioms)+

    subsection {* Further lemmas involving monomials *}

    lemma (in UP_cring) monom_zero [simp]:
      "monom P \<zero> n = \<zero>P"
      by (simp add: UP_def P_def)

    lemma (in UP_cring) monom_mult_is_smult:
      assumes R: "a ∈ carrier R" "p ∈ carrier P"
      shows "monom P a 0 ⊗P p = a \<odot>P p"
    proof (rule up_eqI)
      fix n
      have "coeff P (p ⊗P monom P a 0) n = coeff P (a \<odot>P p) n"
      proof (cases n)
        case 0 with R show ?thesis by (simp add: R.m_comm)
      next
        case Suc with R show ?thesis
          by (simp cong: R.finsum_cong add: R.r_null Pi_def)
            (simp add: R.m_comm)
      qed
      with R show "coeff P (monom P a 0 ⊗P p) n = coeff P (a \<odot>P p) n"
        by (simp add: UP_m_comm)
    qed (simp_all add: R)

    lemma (in UP_cring) monom_add [simp]:
      "[| a ∈ carrier R; b ∈ carrier R |] ==>
      monom P (a ⊕ b) n = monom P a n ⊕P monom P b n"
      by (rule up_eqI) simp_all

    lemma (in UP_cring) monom_one_Suc:
      "monom P \<one> (Suc n) = monom P \<one> n ⊗P monom P \<one> 1"
    proof (rule up_eqI)
      fix k
      show "coeff P (monom P \<one> (Suc n)) k = coeff P (monom P \<one> n ⊗P monom P \<one> 1) k"
      proof (cases "k = Suc n")
        case True show ?thesis
        proof -
          from True have less_add_diff:
            "!!i. [| n < i; i <= n + m |] ==> n + m - i < m" by arith
          from True have "coeff P (monom P \<one> (Suc n)) k = \<one>" by simp
          also from True
          have "... = (\<Oplus>i ∈ {..<n} ∪ {n}. coeff P (monom P \<one> n) i ⊗
            coeff P (monom P \<one> 1) (k - i))"
            by (simp cong: R.finsum_cong add: Pi_def)
          also have "... = (\<Oplus>i ∈  {..n}. coeff P (monom P \<one> n) i ⊗
            coeff P (monom P \<one> 1) (k - i))"
            by (simp only: ivl_disj_un_singleton)
          also from True
          have "... = (\<Oplus>i ∈ {..n} ∪ {n<..k}. coeff P (monom P \<one> n) i ⊗
            coeff P (monom P \<one> 1) (k - i))"
            by (simp cong: R.finsum_cong add: R.finsum_Un_disjoint ivl_disj_int_one
              order_less_imp_not_eq Pi_def)
          also from True have "... = coeff P (monom P \<one> n ⊗P monom P \<one> 1) k"
            by (simp add: ivl_disj_un_one)
          finally show ?thesis .
        qed
      next
        case False
        note neq = False
        let ?s =
          "λi. (if n = i then \<one> else \<zero>) ⊗ (if Suc 0 = k - i then \<one> else \<zero>)"
        from neq have "coeff P (monom P \<one> (Suc n)) k = \<zero>" by simp
        also have "... = (\<Oplus>i ∈ {..k}. ?s i)"
        proof -
          have f1: "(\<Oplus>i ∈ {..<n}. ?s i) = \<zero>"
            by (simp cong: R.finsum_cong add: Pi_def)
          from neq have f2: "(\<Oplus>i ∈ {n}. ?s i) = \<zero>"
            by (simp cong: R.finsum_cong add: Pi_def) arith
          have f3: "n < k ==> (\<Oplus>i ∈ {n<..k}. ?s i) = \<zero>"
            by (simp cong: R.finsum_cong add: order_less_imp_not_eq Pi_def)
          show ?thesis
          proof (cases "k < n")
            case True then show ?thesis by (simp cong: R.finsum_cong add: Pi_def)
          next
            case False then have n_le_k: "n <= k" by arith
            show ?thesis
            proof (cases "n = k")
              case True
              then have "\<zero> = (\<Oplus>i ∈ {..<n} ∪ {n}. ?s i)"
                by (simp cong: R.finsum_cong add: ivl_disj_int_singleton Pi_def)
              also from True have "... = (\<Oplus>i ∈ {..k}. ?s i)"
                by (simp only: ivl_disj_un_singleton)
              finally show ?thesis .
            next
              case False with n_le_k have n_less_k: "n < k" by arith
              with neq have "\<zero> = (\<Oplus>i ∈ {..<n} ∪ {n}. ?s i)"
                by (simp add: R.finsum_Un_disjoint f1 f2
                  ivl_disj_int_singleton Pi_def del: Un_insert_right)
              also have "... = (\<Oplus>i ∈ {..n}. ?s i)"
                by (simp only: ivl_disj_un_singleton)
              also from n_less_k neq have "... = (\<Oplus>i ∈ {..n} ∪ {n<..k}. ?s i)"
                by (simp add: R.finsum_Un_disjoint f3 ivl_disj_int_one Pi_def)
              also from n_less_k have "... = (\<Oplus>i ∈ {..k}. ?s i)"
                by (simp only: ivl_disj_un_one)
              finally show ?thesis .
            qed
          qed
        qed
        also have "... = coeff P (monom P \<one> n ⊗P monom P \<one> 1) k" by simp
        finally show ?thesis .
      qed
    qed (simp_all)

    lemma (in UP_cring) monom_mult_smult:
      "[| a ∈ carrier R; b ∈ carrier R |] ==> monom P (a ⊗ b) n = a \<odot>P monom P b n"
      by (rule up_eqI) simp_all

    lemma (in UP_cring) monom_one [simp]:
      "monom P \<one> 0 = \<one>P"
      by (rule up_eqI) simp_all

    lemma (in UP_cring) monom_one_mult:
      "monom P \<one> (n + m) = monom P \<one> n ⊗P monom P \<one> m"
    proof (induct n)
      case 0 show ?case by simp
    next
      case Suc then show ?case
        by (simp only: add_Suc monom_one_Suc) (simp add: P.m_ac)
    qed

    lemma (in UP_cring) monom_mult [simp]:
      assumes R: "a ∈ carrier R" "b ∈ carrier R"
      shows "monom P (a ⊗ b) (n + m) = monom P a n ⊗P monom P b m"
    proof -
      from R have "monom P (a ⊗ b) (n + m) = monom P (a ⊗ b ⊗ \<one>) (n + m)" by simp
      also from R have "... = a ⊗ b \<odot>P monom P \<one> (n + m)"
        by (simp add: monom_mult_smult del: R.r_one)
      also have "... = a ⊗ b \<odot>P (monom P \<one> n ⊗P monom P \<one> m)"
        by (simp only: monom_one_mult)
      also from R have "... = a \<odot>P (b \<odot>P (monom P \<one> n ⊗P monom P \<one> m))"
        by (simp add: UP_smult_assoc1)
      also from R have "... = a \<odot>P (b \<odot>P (monom P \<one> m ⊗P monom P \<one> n))"
        by (simp add: P.m_comm)
      also from R have "... = a \<odot>P ((b \<odot>P monom P \<one> m) ⊗P monom P \<one> n)"
        by (simp add: UP_smult_assoc2)
      also from R have "... = a \<odot>P (monom P \<one> n ⊗P (b \<odot>P monom P \<one> m))"
        by (simp add: P.m_comm)
      also from R have "... = (a \<odot>P monom P \<one> n) ⊗P (b \<odot>P monom P \<one> m)"
        by (simp add: UP_smult_assoc2)
      also from R have "... = monom P (a ⊗ \<one>) n ⊗P monom P (b ⊗ \<one>) m"
        by (simp add: monom_mult_smult del: R.r_one)
      also from R have "... = monom P a n ⊗P monom P b m" by simp
      finally show ?thesis .
    qed

    lemma (in UP_cring) monom_a_inv [simp]:
      "a ∈ carrier R ==> monom P (\<ominus> a) n = \<ominus>P monom P a n"
      by (rule up_eqI) simp_all

    lemma (in UP_cring) monom_inj:
      "inj_on (%a. monom P a n) (carrier R)"
    proof (rule inj_onI)
      fix x y
      assume R: "x ∈ carrier R" "y ∈ carrier R" and eq: "monom P x n = monom P y n"
      then have "coeff P (monom P x n) n = coeff P (monom P y n) n" by simp
      with R show "x = y" by simp
    qed

    subsection {* The degree function *}

    constdefs (structure R)
      deg :: "[('a, 'm) ring_scheme, nat => 'a] => nat"
      "deg R p == LEAST n. bound \<zero> n (coeff (UP R) p)"

    lemma (in UP_cring) deg_aboveI:
      "[| (!!m. n < m ==> coeff P p m = \<zero>); p ∈ carrier P |] ==> deg R p <= n"
      by (unfold deg_def P_def) (fast intro: Least_le)

    (*
    lemma coeff_bound_ex: "EX n. bound n (coeff p)"
    proof -
      have "(%n. coeff p n) : UP" by (simp add: coeff_def Rep_UP)
      then obtain n where "bound n (coeff p)" by (unfold UP_def) fast
      then show ?thesis ..
    qed

    lemma bound_coeff_obtain:
      assumes prem: "(!!n. bound n (coeff p) ==> P)" shows "P"
    proof -
      have "(%n. coeff p n) : UP" by (simp add: coeff_def Rep_UP)
      then obtain n where "bound n (coeff p)" by (unfold UP_def) fast
      with prem show P .
    qed
    *)

    lemma (in UP_cring) deg_aboveD:
      "[| deg R p < m; p ∈ carrier P |] ==> coeff P p m = \<zero>"
    proof -
      assume R: "p ∈ carrier P" and "deg R p < m"
      from R obtain n where "bound \<zero> n (coeff P p)"
        by (auto simp add: UP_def P_def)
      then have "bound \<zero> (deg R p) (coeff P p)"
        by (auto simp: deg_def P_def dest: LeastI)
      then show ?thesis ..
    qed

    lemma (in UP_cring) deg_belowI:
      assumes non_zero: "n ~= 0 ==> coeff P p n ~= \<zero>"
        and R: "p ∈ carrier P"
      shows "n <= deg R p"
    -- {* Logically, this is a slightly stronger version of
       @{thm [source] deg_aboveD} *}
    proof (cases "n=0")
      case True then show ?thesis by simp
    next
      case False then have "coeff P p n ~= \<zero>" by (rule non_zero)
      then have "~ deg R p < n" by (fast dest: deg_aboveD intro: R)
      then show ?thesis by arith
    qed

    lemma (in UP_cring) lcoeff_nonzero_deg:
      assumes deg: "deg R p ~= 0" and R: "p ∈ carrier P"
      shows "coeff P p (deg R p) ~= \<zero>"
    proof -
      from R obtain m where "deg R p <= m" and m_coeff: "coeff P p m ~= \<zero>"
      proof -
        have minus: "!!(n::nat) m. n ~= 0 ==> (n - Suc 0 < m) = (n <= m)"
          by arith
    (* TODO: why does simplification below not work with "1" *)
        from deg have "deg R p - 1 < (LEAST n. bound \<zero> n (coeff P p))"
          by (unfold deg_def P_def) arith
        then have "~ bound \<zero> (deg R p - 1) (coeff P p)" by (rule not_less_Least)
        then have "EX m. deg R p - 1 < m & coeff P p m ~= \<zero>"
          by (unfold bound_def) fast
        then have "EX m. deg R p <= m & coeff P p m ~= \<zero>" by (simp add: deg minus)
        then show ?thesis by auto
      qed
      with deg_belowI R have "deg R p = m" by fastsimp
      with m_coeff show ?thesis by simp
    qed

    lemma (in UP_cring) lcoeff_nonzero_nonzero:
      assumes deg: "deg R p = 0" and nonzero: "p ~= \<zero>P" and R: "p ∈ carrier P"
      shows "coeff P p 0 ~= \<zero>"
    proof -
      have "EX m. coeff P p m ~= \<zero>"
      proof (rule classical)
        assume "~ ?thesis"
        with R have "p = \<zero>P" by (auto intro: up_eqI)
        with nonzero show ?thesis by contradiction
      qed
      then obtain m where coeff: "coeff P p m ~= \<zero>" ..
      then have "m <= deg R p" by (rule deg_belowI)
      then have "m = 0" by (simp add: deg)
      with coeff show ?thesis by simp
    qed

    lemma (in UP_cring) lcoeff_nonzero:
      assumes neq: "p ~= \<zero>P" and R: "p ∈ carrier P"
      shows "coeff P p (deg R p) ~= \<zero>"
    proof (cases "deg R p = 0")
      case True with neq R show ?thesis by (simp add: lcoeff_nonzero_nonzero)
    next
      case False with neq R show ?thesis by (simp add: lcoeff_nonzero_deg)
    qed

    lemma (in UP_cring) deg_eqI:
      "[| !!m. n < m ==> coeff P p m = \<zero>;
          !!n. n ~= 0 ==> coeff P p n ~= \<zero>; p ∈ carrier P |] ==> deg R p = n"
    by (fast intro: le_anti_sym deg_aboveI deg_belowI)

    text {* Degree and polynomial operations *}

    lemma (in UP_cring) deg_add [simp]:
      assumes R: "p ∈ carrier P" "q ∈ carrier P"
      shows "deg R (p ⊕P q) <= max (deg R p) (deg R q)"
    proof (cases "deg R p <= deg R q")
      case True show ?thesis
        by (rule deg_aboveI) (simp_all add: True R deg_aboveD)
    next
      case False show ?thesis
        by (rule deg_aboveI) (simp_all add: False R deg_aboveD)
    qed

    lemma (in UP_cring) deg_monom_le:
      "a ∈ carrier R ==> deg R (monom P a n) <= n"
      by (intro deg_aboveI) simp_all

    lemma (in UP_cring) deg_monom [simp]:
      "[| a ~= \<zero>; a ∈ carrier R |] ==> deg R (monom P a n) = n"
      by (fastsimp intro: le_anti_sym deg_aboveI deg_belowI)

    lemma (in UP_cring) deg_const [simp]:
      assumes R: "a ∈ carrier R" shows "deg R (monom P a 0) = 0"
    proof (rule le_anti_sym)
      show "deg R (monom P a 0) <= 0" by (rule deg_aboveI) (simp_all add: R)
    next
      show "0 <= deg R (monom P a 0)" by (rule deg_belowI) (simp_all add: R)
    qed

    lemma (in UP_cring) deg_zero [simp]:
      "deg R \<zero>P = 0"
    proof (rule le_anti_sym)
      show "deg R \<zero>P <= 0" by (rule deg_aboveI) simp_all
    next
      show "0 <= deg R \<zero>P" by (rule deg_belowI) simp_all
    qed

    lemma (in UP_cring) deg_one [simp]:
      "deg R \<one>P = 0"
    proof (rule le_anti_sym)
      show "deg R \<one>P <= 0" by (rule deg_aboveI) simp_all
    next
      show "0 <= deg R \<one>P" by (rule deg_belowI) simp_all
    qed

    lemma (in UP_cring) deg_uminus [simp]:
      assumes R: "p ∈ carrier P" shows "deg R (\<ominus>P p) = deg R p"
    proof (rule le_anti_sym)
      show "deg R (\<ominus>P p) <= deg R p" by (simp add: deg_aboveI deg_aboveD R)
    next
      show "deg R p <= deg R (\<ominus>P p)"
        by (simp add: deg_belowI lcoeff_nonzero_deg
          inj_on_iff [OF R.a_inv_inj, of _ "\<zero>", simplified] R)
    qed

    lemma (in UP_domain) deg_smult_ring:
      "[| a ∈ carrier R; p ∈ carrier P |] ==>
      deg R (a \<odot>P p) <= (if a = \<zero> then 0 else deg R p)"
      by (cases "a = \<zero>") (simp add: deg_aboveI deg_aboveD)+

    lemma (in UP_domain) deg_smult [simp]:
      assumes R: "a ∈ carrier R" "p ∈ carrier P"
      shows "deg R (a \<odot>P p) = (if a = \<zero> then 0 else deg R p)"
    proof (rule le_anti_sym)
      show "deg R (a \<odot>P p) <= (if a = \<zero> then 0 else deg R p)"
        by (rule deg_smult_ring)
    next
      show "(if a = \<zero> then 0 else deg R p) <= deg R (a \<odot>P p)"
      proof (cases "a = \<zero>")
      qed (simp, simp add: deg_belowI lcoeff_nonzero_deg integral_iff R)
    qed

    lemma (in UP_cring) deg_mult_cring:
      assumes R: "p ∈ carrier P" "q ∈ carrier P"
      shows "deg R (p ⊗P q) <= deg R p + deg R q"
    proof (rule deg_aboveI)
      fix m
      assume boundm: "deg R p + deg R q < m"
      {
        fix k i
        assume boundk: "deg R p + deg R q < k"
        then have "coeff P p i ⊗ coeff P q (k - i) = \<zero>"
        proof (cases "deg R p < i")
          case True then show ?thesis by (simp add: deg_aboveD R)
        next
          case False with boundk have "deg R q < k - i" by arith
          then show ?thesis by (simp add: deg_aboveD R)
        qed
      }
      with boundm R show "coeff P (p ⊗P q) m = \<zero>" by simp
    qed (simp add: R)

    lemma (in UP_domain) deg_mult [simp]:
      "[| p ~= \<zero>P; q ~= \<zero>P; p ∈ carrier P; q ∈ carrier P |] ==>
      deg R (p ⊗P q) = deg R p + deg R q"
    proof (rule le_anti_sym)
      assume "p ∈ carrier P" " q ∈ carrier P"
      show "deg R (p ⊗P q) <= deg R p + deg R q" by (rule deg_mult_cring)
    next
      let ?s = "(%i. coeff P p i ⊗ coeff P q (deg R p + deg R q - i))"
      assume R: "p ∈ carrier P" "q ∈ carrier P" and nz: "p ~= \<zero>P" "q ~= \<zero>P"
      have less_add_diff: "!!(k::nat) n m. k < n ==> m < n + m - k" by arith
      show "deg R p + deg R q <= deg R (p ⊗P q)"
      proof (rule deg_belowI, simp add: R)
        have "(\<Oplus>i ∈ {.. deg R p + deg R q}. ?s i)
          = (\<Oplus>i ∈ {..< deg R p} ∪ {deg R p .. deg R p + deg R q}. ?s i)"
          by (simp only: ivl_disj_un_one)
        also have "... = (\<Oplus>i ∈ {deg R p .. deg R p + deg R q}. ?s i)"
          by (simp cong: R.finsum_cong add: R.finsum_Un_disjoint ivl_disj_int_one
            deg_aboveD less_add_diff R Pi_def)
        also have "...= (\<Oplus>i ∈ {deg R p} ∪ {deg R p <.. deg R p + deg R q}. ?s i)"
          by (simp only: ivl_disj_un_singleton)
        also have "... = coeff P p (deg R p) ⊗ coeff P q (deg R q)"
          by (simp cong: R.finsum_cong
            add: ivl_disj_int_singleton deg_aboveD R Pi_def)
        finally have "(\<Oplus>i ∈ {.. deg R p + deg R q}. ?s i)
          = coeff P p (deg R p) ⊗ coeff P q (deg R q)" .
        with nz show "(\<Oplus>i ∈ {.. deg R p + deg R q}. ?s i) ~= \<zero>"
          by (simp add: integral_iff lcoeff_nonzero R)
        qed (simp add: R)
      qed

    lemma (in UP_cring) coeff_finsum:
      assumes fin: "finite A"
      shows "p ∈ A -> carrier P ==>
        coeff P (finsum P p A) k = (\<Oplus>i ∈ A. coeff P (p i) k)"
      using fin by induct (auto simp: Pi_def)

    lemma (in UP_cring) up_repr:
      assumes R: "p ∈ carrier P"
      shows "(\<Oplus>P i ∈ {..deg R p}. monom P (coeff P p i) i) = p"
    proof (rule up_eqI)
      let ?s = "(%i. monom P (coeff P p i) i)"
      fix k
      from R have RR: "!!i. (if i = k then coeff P p i else \<zero>) ∈ carrier R"
        by simp
      show "coeff P (\<Oplus>P i ∈ {..deg R p}. ?s i) k = coeff P p k"
      proof (cases "k <= deg R p")
        case True
        hence "coeff P (\<Oplus>P i ∈ {..deg R p}. ?s i) k =
              coeff P (\<Oplus>P i ∈ {..k} ∪ {k<..deg R p}. ?s i) k"
          by (simp only: ivl_disj_un_one)
        also from True
        have "... = coeff P (\<Oplus>P i ∈ {..k}. ?s i) k"
          by (simp cong: R.finsum_cong add: R.finsum_Un_disjoint
            ivl_disj_int_one order_less_imp_not_eq2 coeff_finsum R RR Pi_def)
        also
        have "... = coeff P (\<Oplus>P i ∈ {..<k} ∪ {k}. ?s i) k"
          by (simp only: ivl_disj_un_singleton)
        also have "... = coeff P p k"
          by (simp cong: R.finsum_cong
            add: ivl_disj_int_singleton coeff_finsum deg_aboveD R RR Pi_def)
        finally show ?thesis .
      next
        case False
        hence "coeff P (\<Oplus>P i ∈ {..deg R p}. ?s i) k =
              coeff P (\<Oplus>P i ∈ {..<deg R p} ∪ {deg R p}. ?s i) k"
          by (simp only: ivl_disj_un_singleton)
        also from False have "... = coeff P p k"
          by (simp cong: R.finsum_cong
            add: ivl_disj_int_singleton coeff_finsum deg_aboveD R Pi_def)
        finally show ?thesis .
      qed
    qed (simp_all add: R Pi_def)

    lemma (in UP_cring) up_repr_le:
      "[| deg R p <= n; p ∈ carrier P |] ==>
      (\<Oplus>P i ∈ {..n}. monom P (coeff P p i) i) = p"
    proof -
      let ?s = "(%i. monom P (coeff P p i) i)"
      assume R: "p ∈ carrier P" and "deg R p <= n"
      then have "finsum P ?s {..n} = finsum P ?s ({..deg R p} ∪ {deg R p<..n})"
        by (simp only: ivl_disj_un_one)
      also have "... = finsum P ?s {..deg R p}"
        by (simp cong: P.finsum_cong add: P.finsum_Un_disjoint ivl_disj_int_one
          deg_aboveD R Pi_def)
      also have "... = p" by (rule up_repr)
      finally show ?thesis .
    qed

    subsection {* Polynomials over an integral domain form an integral domain *}

    lemma domainI:
      assumes cring: "cring R"
        and one_not_zero: "one R ~= zero R"
        and integral: "!!a b. [| mult R a b = zero R; a ∈ carrier R;
          b ∈ carrier R |] ==> a = zero R | b = zero R"
      shows "domain R"
      by (auto intro!: domain.intro domain_axioms.intro cring.axioms prems
        del: disjCI)

    lemma (in UP_domain) UP_one_not_zero:
      "\<one>P ~= \<zero>P"
    proof
      assume "\<one>P = \<zero>P"
      hence "coeff P \<one>P 0 = (coeff P \<zero>P 0)" by simp
      hence "\<one> = \<zero>" by simp
      with one_not_zero show "False" by contradiction
    qed

    lemma (in UP_domain) UP_integral:
      "[| p ⊗P q = \<zero>P; p ∈ carrier P; q ∈ carrier P |] ==> p = \<zero>P | q = \<zero>P"
    proof -
      fix p q
      assume pq: "p ⊗P q = \<zero>P" and R: "p ∈ carrier P" "q ∈ carrier P"
      show "p = \<zero>P | q = \<zero>P"
      proof (rule classical)
        assume c: "~ (p = \<zero>P | q = \<zero>P)"
        with R have "deg R p + deg R q = deg R (p ⊗P q)" by simp
        also from pq have "... = 0" by simp
        finally have "deg R p + deg R q = 0" .
        then have f1: "deg R p = 0 & deg R q = 0" by simp
        from f1 R have "p = (\<Oplus>P i ∈ {..0}. monom P (coeff P p i) i)"
          by (simp only: up_repr_le)
        also from R have "... = monom P (coeff P p 0) 0" by simp
        finally have p: "p = monom P (coeff P p 0) 0" .
        from f1 R have "q = (\<Oplus>P i ∈ {..0}. monom P (coeff P q i) i)"
          by (simp only: up_repr_le)
        also from R have "... = monom P (coeff P q 0) 0" by simp
        finally have q: "q = monom P (coeff P q 0) 0" .
        from R have "coeff P p 0 ⊗ coeff P q 0 = coeff P (p ⊗P q) 0" by simp
        also from pq have "... = \<zero>" by simp
        finally have "coeff P p 0 ⊗ coeff P q 0 = \<zero>" .
        with R have "coeff P p 0 = \<zero> | coeff P q 0 = \<zero>"
          by (simp add: R.integral_iff)
        with p q show "p = \<zero>P | q = \<zero>P" by fastsimp
      qed
    qed

    theorem (in UP_domain) UP_domain:
      "domain P"
      by (auto intro!: domainI UP_cring UP_one_not_zero UP_integral del: disjCI)

    text {*
      Interpretation of theorems from @{term domain}.
    *}

    interpretation UP_domain < "domain" P
      using UP_domain
      by (rule domain.axioms)

    subsection {* Evaluation Homomorphism and Universal Property*}

    (* alternative congruence rule (possibly more efficient)
    lemma (in abelian_monoid) finsum_cong2:
      "[| !!i. i ∈ A ==> f i ∈ carrier G = True; A = B;
      !!i. i ∈ B ==> f i = g i |] ==> finsum G f A = finsum G g B"
      sorry*)

    theorem (in cring) diagonal_sum:
      "[| f ∈ {..n + m::nat} -> carrier R; g ∈ {..n + m} -> carrier R |] ==>
      (\<Oplus>k ∈ {..n + m}. \<Oplus>i ∈ {..k}. f i ⊗ g (k - i)) =
      (\<Oplus>k ∈ {..n + m}. \<Oplus>i ∈ {..n + m - k}. f k ⊗ g i)"
    proof -
      assume Rf: "f ∈ {..n + m} -> carrier R" and Rg: "g ∈ {..n + m} -> carrier R"
      {
        fix j
        have "j <= n + m ==>
          (\<Oplus>k ∈ {..j}. \<Oplus>i ∈ {..k}. f i ⊗ g (k - i)) =
          (\<Oplus>k ∈ {..j}. \<Oplus>i ∈ {..j - k}. f k ⊗ g i)"
        proof (induct j)
          case 0 from Rf Rg show ?case by (simp add: Pi_def)
        next
          case (Suc j)
          have R6: "!!i k. [| k <= j; i <= Suc j - k |] ==> g i ∈ carrier R"
            using Suc by (auto intro!: funcset_mem [OF Rg]) arith
          have R8: "!!i k. [| k <= Suc j; i <= k |] ==> g (k - i) ∈ carrier R"
            using Suc by (auto intro!: funcset_mem [OF Rg]) arith
          have R9: "!!i k. [| k <= Suc j |] ==> f k ∈ carrier R"
            using Suc by (auto intro!: funcset_mem [OF Rf])
          have R10: "!!i k. [| k <= Suc j; i <= Suc j - k |] ==> g i ∈ carrier R"
            using Suc by (auto intro!: funcset_mem [OF Rg]) arith
          have R11: "g 0 ∈ carrier R"
            using Suc by (auto intro!: funcset_mem [OF Rg])
          from Suc show ?case
            by (simp cong: finsum_cong add: Suc_diff_le a_ac
              Pi_def R6 R8 R9 R10 R11)
        qed
      }
      then show ?thesis by fast
    qed

    lemma (in abelian_monoid) boundD_carrier:
      "[| bound \<zero> n f; n < m |] ==> f m ∈ carrier G"
      by auto

    theorem (in cring) cauchy_product:
      assumes bf: "bound \<zero> n f" and bg: "bound \<zero> m g"
        and Rf: "f ∈ {..n} -> carrier R" and Rg: "g ∈ {..m} -> carrier R"
      shows "(\<Oplus>k ∈ {..n + m}. \<Oplus>i ∈ {..k}. f i ⊗ g (k - i)) =
        (\<Oplus>i ∈ {..n}. f i) ⊗ (\<Oplus>i ∈ {..m}. g i)"      (* State reverse direction? *)
    proof -
      have f: "!!x. f x ∈ carrier R"
      proof -
        fix x
        show "f x ∈ carrier R"
          using Rf bf boundD_carrier by (cases "x <= n") (auto simp: Pi_def)
      qed
      have g: "!!x. g x ∈ carrier R"
      proof -
        fix x
        show "g x ∈ carrier R"
          using Rg bg boundD_carrier by (cases "x <= m") (auto simp: Pi_def)
      qed
      from f g have "(\<Oplus>k ∈ {..n + m}. \<Oplus>i ∈ {..k}. f i ⊗ g (k - i)) =
          (\<Oplus>k ∈ {..n + m}. \<Oplus>i ∈ {..n + m - k}. f k ⊗ g i)"
        by (simp add: diagonal_sum Pi_def)
      also have "... = (\<Oplus>k ∈ {..n} ∪ {n<..n + m}. \<Oplus>i ∈ {..n + m - k}. f k ⊗ g i)"
        by (simp only: ivl_disj_un_one)
      also from f g have "... = (\<Oplus>k ∈ {..n}. \<Oplus>i ∈ {..n + m - k}. f k ⊗ g i)"
        by (simp cong: finsum_cong
          add: bound.bound [OF bf] finsum_Un_disjoint ivl_disj_int_one Pi_def)
      also from f g
      have "... = (\<Oplus>k ∈ {..n}. \<Oplus>i ∈ {..m} ∪ {m<..n + m - k}. f k ⊗ g i)"
        by (simp cong: finsum_cong add: ivl_disj_un_one le_add_diff Pi_def)
      also from f g have "... = (\<Oplus>k ∈ {..n}. \<Oplus>i ∈ {..m}. f k ⊗ g i)"
        by (simp cong: finsum_cong
          add: bound.bound [OF bg] finsum_Un_disjoint ivl_disj_int_one Pi_def)
      also from f g have "... = (\<Oplus>i ∈ {..n}. f i) ⊗ (\<Oplus>i ∈ {..m}. g i)"
        by (simp add: finsum_ldistr diagonal_sum Pi_def,
          simp cong: finsum_cong add: finsum_rdistr Pi_def)
      finally show ?thesis .
    qed

    lemma (in UP_cring) const_ring_hom:
      "(%a. monom P a 0) ∈ ring_hom R P"
      by (auto intro!: ring_hom_memI intro: up_eqI simp: monom_mult_is_smult)

    constdefs (structure S)
      eval :: "[('a, 'm) ring_scheme, ('b, 'n) ring_scheme,
               'a => 'b, 'b, nat => 'a] => 'b"
      "eval R S phi s == λp ∈ carrier (UP R).
        \<Oplus>i ∈ {..deg R p}. phi (coeff (UP R) p i) ⊗ s (^) i"

    lemma (in UP) eval_on_carrier:
      includes struct S
      shows "p ∈ carrier P ==>
      eval R S phi s p = (\<Oplus>S i ∈ {..deg R p}. phi (coeff P p i) ⊗S s (^)S i)"
      by (unfold eval_def, fold P_def) simp

    lemma (in UP) eval_extensional:
      "eval R S phi p ∈ extensional (carrier P)"
      by (unfold eval_def, fold P_def) simp

    text {* The universal property of the polynomial ring *}

    locale UP_pre_univ_prop = ring_hom_cring R S h + UP_cring R P

    locale UP_univ_prop = UP_pre_univ_prop + var s + var Eval +
      assumes indet_img_carrier [simp, intro]: "s ∈ carrier S"
      defines Eval_def: "Eval == eval R S h s"

    theorem (in UP_pre_univ_prop) eval_ring_hom:
      assumes S: "s ∈ carrier S"
      shows "eval R S h s ∈ ring_hom P S"
    proof (rule ring_hom_memI)
      fix p
      assume R: "p ∈ carrier P"
      then show "eval R S h s p ∈ carrier S"
        by (simp only: eval_on_carrier) (simp add: S Pi_def)
    next
      fix p q
      assume R: "p ∈ carrier P" "q ∈ carrier P"
      then show "eval R S h s (p ⊗P q) = eval R S h s p ⊗S eval R S h s q"
      proof (simp only: eval_on_carrier UP_mult_closed)
        from R S have
          "(\<Oplus>S i ∈ {..deg R (p ⊗P q)}. h (coeff P (p ⊗P q) i) ⊗S s (^)S i) =
          (\<Oplus>S i ∈ {..deg R (p ⊗P q)} ∪ {deg R (p ⊗P q)<..deg R p + deg R q}.
            h (coeff P (p ⊗P q) i) ⊗S s (^)S i)"
          by (simp cong: S.finsum_cong
            add: deg_aboveD S.finsum_Un_disjoint ivl_disj_int_one Pi_def
            del: coeff_mult)
        also from R have "... =
          (\<Oplus>S i ∈ {..deg R p + deg R q}. h (coeff P (p ⊗P q) i) ⊗S s (^)S i)"
          by (simp only: ivl_disj_un_one deg_mult_cring)
        also from R S have "... =
          (\<Oplus>S i ∈ {..deg R p + deg R q}.
             \<Oplus>S k ∈ {..i}.
               h (coeff P p k) ⊗S h (coeff P q (i - k)) ⊗S
               (s (^)S k ⊗S s (^)S (i - k)))"
          by (simp cong: S.finsum_cong add: S.nat_pow_mult Pi_def
            S.m_ac S.finsum_rdistr)
        also from R S have "... =
          (\<Oplus>S i∈{..deg R p}. h (coeff P p i) ⊗S s (^)S i) ⊗S
          (\<Oplus>S i∈{..deg R q}. h (coeff P q i) ⊗S s (^)S i)"
          by (simp add: S.cauchy_product [THEN sym] bound.intro deg_aboveD S.m_ac
            Pi_def)
        finally show
          "(\<Oplus>S i ∈ {..deg R (p ⊗P q)}. h (coeff P (p ⊗P q) i) ⊗S s (^)S i) =
          (\<Oplus>S i ∈ {..deg R p}. h (coeff P p i) ⊗S s (^)S i) ⊗S
          (\<Oplus>S i ∈ {..deg R q}. h (coeff P q i) ⊗S s (^)S i)" .
      qed
    next
      fix p q
      assume R: "p ∈ carrier P" "q ∈ carrier P"
      then show "eval R S h s (p ⊕P q) = eval R S h s p ⊕S eval R S h s q"
      proof (simp only: eval_on_carrier P.a_closed)
        from S R have
          "(\<Oplus>S i∈{..deg R (p ⊕P q)}. h (coeff P (p ⊕P q) i) ⊗S s (^)S i) =
          (\<Oplus>S i∈{..deg R (p ⊕P q)} ∪ {deg R (p ⊕P q)<..max (deg R p) (deg R q)}.
            h (coeff P (p ⊕P q) i) ⊗S s (^)S i)"
          by (simp cong: S.finsum_cong
            add: deg_aboveD S.finsum_Un_disjoint ivl_disj_int_one Pi_def
            del: coeff_add)
        also from R have "... =
            (\<Oplus>S i ∈ {..max (deg R p) (deg R q)}.
              h (coeff P (p ⊕P q) i) ⊗S s (^)S i)"
          by (simp add: ivl_disj_un_one)
        also from R S have "... =
          (\<Oplus>Si∈{..max (deg R p) (deg R q)}. h (coeff P p i) ⊗S s (^)S i) ⊕S
          (\<Oplus>Si∈{..max (deg R p) (deg R q)}. h (coeff P q i) ⊗S s (^)S i)"
          by (simp cong: S.finsum_cong
            add: S.l_distr deg_aboveD ivl_disj_int_one Pi_def)
        also have "... =
            (\<Oplus>S i ∈ {..deg R p} ∪ {deg R p<..max (deg R p) (deg R q)}.
              h (coeff P p i) ⊗S s (^)S i) ⊕S
            (\<Oplus>S i ∈ {..deg R q} ∪ {deg R q<..max (deg R p) (deg R q)}.
              h (coeff P q i) ⊗S s (^)S i)"
          by (simp only: ivl_disj_un_one le_maxI1 le_maxI2)
        also from R S have "... =
          (\<Oplus>S i ∈ {..deg R p}. h (coeff P p i) ⊗S s (^)S i) ⊕S
          (\<Oplus>S i ∈ {..deg R q}. h (coeff P q i) ⊗S s (^)S i)"
          by (simp cong: S.finsum_cong
            add: deg_aboveD S.finsum_Un_disjoint ivl_disj_int_one Pi_def)
        finally show
          "(\<Oplus>Si ∈ {..deg R (p ⊕P q)}. h (coeff P (p ⊕P q) i) ⊗S s (^)S i) =
          (\<Oplus>Si ∈ {..deg R p}. h (coeff P p i) ⊗S s (^)S i) ⊕S
          (\<Oplus>Si ∈ {..deg R q}. h (coeff P q i) ⊗S s (^)S i)" .
      qed
    next
      show "eval R S h s \<one>P = \<one>S"
        by (simp only: eval_on_carrier UP_one_closed) simp
    qed

    text {* Interpretation of ring homomorphism lemmas. *}

    interpretation UP_univ_prop < ring_hom_cring P S Eval
      by (unfold Eval_def)
        (fast intro!: ring_hom_cring.intro UP_cring cring.axioms prems
          intro: ring_hom_cring_axioms.intro eval_ring_hom)

    text {* Further properties of the evaluation homomorphism. *}

    (* The following lemma could be proved in UP\_cring with the additional
       assumption that h is closed. *)

    lemma (in UP_pre_univ_prop) eval_const:
      "[| s ∈ carrier S; r ∈ carrier R |] ==> eval R S h s (monom P r 0) = h r"
      by (simp only: eval_on_carrier monom_closed) simp

    text {* The following proof is complicated by the fact that in arbitrary
      rings one might have @{term "one R = zero R"}. *}

    (* TODO: simplify by cases "one R = zero R" *)

    lemma (in UP_pre_univ_prop) eval_monom1:
      assumes S: "s ∈ carrier S"
      shows "eval R S h s (monom P \<one> 1) = s"
    proof (simp only: eval_on_carrier monom_closed R.one_closed)
       from S have
        "(\<Oplus>S i∈{..deg R (monom P \<one> 1)}. h (coeff P (monom P \<one> 1) i) ⊗S s (^)S i) =
        (\<Oplus>S i∈{..deg R (monom P \<one> 1)} ∪ {deg R (monom P \<one> 1)<..1}.
          h (coeff P (monom P \<one> 1) i) ⊗S s (^)S i)"
        by (simp cong: S.finsum_cong del: coeff_monom
          add: deg_aboveD S.finsum_Un_disjoint ivl_disj_int_one Pi_def)
      also have "... =
        (\<Oplus>S i ∈ {..1}. h (coeff P (monom P \<one> 1) i) ⊗S s (^)S i)"
        by (simp only: ivl_disj_un_one deg_monom_le R.one_closed)
      also have "... = s"
      proof (cases "s = \<zero>S")
        case True then show ?thesis by (simp add: Pi_def)
      next
        case False then show ?thesis by (simp add: S Pi_def)
      qed
      finally show "(\<Oplus>S i ∈ {..deg R (monom P \<one> 1)}.
        h (coeff P (monom P \<one> 1) i) ⊗S s (^)S i) = s" .
    qed

    lemma (in UP_cring) monom_pow:
      assumes R: "a ∈ carrier R"
      shows "(monom P a n) (^)P m = monom P (a (^) m) (n * m)"
    proof (induct m)
      case 0 from R show ?case by simp
    next
      case Suc with R show ?case
        by (simp del: monom_mult add: monom_mult [THEN sym] add_commute)
    qed

    lemma (in ring_hom_cring) hom_pow [simp]:
      "x ∈ carrier R ==> h (x (^) n) = h x (^)S (n::nat)"
      by (induct n) simp_all

    lemma (in UP_univ_prop) Eval_monom:
      "r ∈ carrier R ==> Eval (monom P r n) = h r ⊗S s (^)S n"
    proof -
      assume R: "r ∈ carrier R"
      from R have "Eval (monom P r n) = Eval (monom P r 0 ⊗P (monom P \<one> 1) (^)P n)"
        by (simp del: monom_mult add: monom_mult [THEN sym] monom_pow)
      also
      from R eval_monom1 [where s = s, folded Eval_def]
      have "... = h r ⊗S s (^)S n"
        by (simp add: eval_const [where s = s, folded Eval_def])
      finally show ?thesis .
    qed

    lemma (in UP_pre_univ_prop) eval_monom:
      assumes R: "r ∈ carrier R" and S: "s ∈ carrier S"
      shows "eval R S h s (monom P r n) = h r ⊗S s (^)S n"
    proof -
      from S interpret UP_univ_prop [R S h P s _]
        by (auto intro!: UP_univ_prop_axioms.intro)
      from R
      show ?thesis by (rule Eval_monom)
    qed

    lemma (in UP_univ_prop) Eval_smult:
      "[| r ∈ carrier R; p ∈ carrier P |] ==> Eval (r \<odot>P p) = h r ⊗S Eval p"
    proof -
      assume R: "r ∈ carrier R" and P: "p ∈ carrier P"
      then show ?thesis
        by (simp add: monom_mult_is_smult [THEN sym]
          eval_const [where s = s, folded Eval_def])
    qed

    lemma ring_hom_cringI:
      assumes "cring R"
        and "cring S"
        and "h ∈ ring_hom R S"
      shows "ring_hom_cring R S h"
      by (fast intro: ring_hom_cring.intro ring_hom_cring_axioms.intro
        cring.axioms prems)

    lemma (in UP_pre_univ_prop) UP_hom_unique:
      includes ring_hom_cring P S Phi
      assumes Phi: "Phi (monom P \<one> (Suc 0)) = s"
          "!!r. r ∈ carrier R ==> Phi (monom P r 0) = h r"
      includes ring_hom_cring P S Psi
      assumes Psi: "Psi (monom P \<one> (Suc 0)) = s"
          "!!r. r ∈ carrier R ==> Psi (monom P r 0) = h r"
        and P: "p ∈ carrier P" and S: "s ∈ carrier S"
      shows "Phi p = Psi p"
    proof -
      have "Phi p =
          Phi (\<Oplus>P i ∈ {..deg R p}. monom P (coeff P p i) 0 ⊗P monom P \<one> 1 (^)P i)"
        by (simp add: up_repr P monom_mult [THEN sym] monom_pow del: monom_mult)
      also
      have "... =
          Psi (\<Oplus>P i∈{..deg R p}. monom P (coeff P p i) 0 ⊗P monom P \<one> 1 (^)P i)"
        by (simp add: Phi Psi P Pi_def comp_def)
      also have "... = Psi p"
        by (simp add: up_repr P monom_mult [THEN sym] monom_pow del: monom_mult)
      finally show ?thesis .
    qed

    lemma (in UP_pre_univ_prop) ring_homD:
      assumes Phi: "Phi ∈ ring_hom P S"
      shows "ring_hom_cring P S Phi"
    proof (rule ring_hom_cring.intro)
      show "ring_hom_cring_axioms P S Phi"
      by (rule ring_hom_cring_axioms.intro) (rule Phi)
    qed (auto intro: P.cring cring.axioms)

    theorem (in UP_pre_univ_prop) UP_universal_property:
      assumes S: "s ∈ carrier S"
      shows "EX! Phi. Phi ∈ ring_hom P S ∩ extensional (carrier P) &
        Phi (monom P \<one> 1) = s &
        (ALL r : carrier R. Phi (monom P r 0) = h r)"
      using S eval_monom1
      apply (auto intro: eval_ring_hom eval_const eval_extensional)
      apply (rule extensionalityI)
      apply (auto intro: UP_hom_unique ring_homD)
      done

    subsection {* Sample application of evaluation homomorphism *}

    lemma UP_pre_univ_propI:
      assumes "cring R"
        and "cring S"
        and "h ∈ ring_hom R S"
      shows "UP_pre_univ_prop R S h "
      by (fast intro: UP_pre_univ_prop.intro ring_hom_cring_axioms.intro
        cring.axioms prems)

    constdefs
      INTEG :: "int ring"
      "INTEG == (| carrier = UNIV, mult = op *, one = 1, zero = 0, add = op + |)"

    lemma INTEG_cring:
      "cring INTEG"
      by (unfold INTEG_def) (auto intro!: cringI abelian_groupI comm_monoidI
        zadd_zminus_inverse2 zadd_zmult_distrib)

    lemma INTEG_id_eval:
      "UP_pre_univ_prop INTEG INTEG id"
      by (fast intro: UP_pre_univ_propI INTEG_cring id_ring_hom)

    text {*
      Interpretation now enables to import all theorems and lemmas
      valid in the context of homomorphisms between @{term INTEG} and @{term
      "UP INTEG"} globally.
    *}

    interpretation INTEG: UP_pre_univ_prop [INTEG INTEG id]
      using INTEG_id_eval
      by - (erule UP_pre_univ_prop.axioms)+

    lemma INTEG_closed [intro, simp]:
      "z ∈ carrier INTEG"
      by (unfold INTEG_def) simp

    lemma INTEG_mult [simp]:
      "mult INTEG z w = z * w"
      by (unfold INTEG_def) simp

    lemma INTEG_pow [simp]:
      "pow INTEG z n = z ^ n"
      by (induct n) (simp_all add: INTEG_def nat_pow_def)

    lemma "eval INTEG INTEG id 10 (monom (UP INTEG) 5 2) = 500"
      by (simp add: INTEG.eval_monom)

    end

### The Constructor for Univariate Polynomials

lemma bound_below:

      [| bound z m f; f n ≠ z |] ==> n ≤ m

lemma mem_upI:

      [| !!n. f n ∈ carrier R; ∃n. bound \<zero>R n f |] ==> f ∈ up R

lemma mem_upD:

      f ∈ up R ==> f n ∈ carrier R

lemma bound_upD:

      [| cring R; f ∈ up R |] ==> ∃n. bound \<zero>R n f

lemma up_one_closed:

      cring R ==> (%n. if n = 0 then \<one>R else \<zero>R) ∈ up R

lemma up_smult_closed:

      [| cring R; a ∈ carrier R; p ∈ up R |] ==> (%i. a ⊗R p i) ∈ up R

lemma up_add_closed:

      [| cring R; p ∈ up R; q ∈ up R |] ==> (%i. p i ⊕R q i) ∈ up R

lemma up_a_inv_closed:

      [| cring R; p ∈ up R |] ==> (%i. \<ominus>R p i) ∈ up R

lemma up_mult_closed:

      [| cring R; p ∈ up R; q ∈ up R |]
      ==> (%n. \<Oplus>Ri∈{..n}. p i ⊗R q (n - i)) ∈ up R

### Effect of operations on coefficients

lemma coeff_monom:

      [| UP_cring R; a ∈ carrier R |]
      ==> coeff (UP R) (monom (UP R) a m) n = (if m = n then a else \<zero>R)

lemma coeff_zero:

      UP_cring R ==> coeff (UP R) \<zero>UP R n = \<zero>R

lemma coeff_one:

      UP_cring R ==> coeff (UP R) \<one>UP R n = (if n = 0 then \<one>R else \<zero>R)

lemma coeff_smult:

      [| UP_cring R; a ∈ carrier R; p ∈ carrier (UP R) |]
      ==> coeff (UP R) (a \<odot>UP R p) n = a ⊗R coeff (UP R) p n

lemma coeff_add:

      [| UP_cring R; p ∈ carrier (UP R); q ∈ carrier (UP R) |]
      ==> coeff (UP R) (p ⊕UP R q) n = coeff (UP R) p n ⊕R coeff (UP R) q n

lemma coeff_mult:

      [| UP_cring R; p ∈ carrier (UP R); q ∈ carrier (UP R) |]
      ==> coeff (UP R) (p ⊗UP R q) n =
          (\<Oplus>Ri∈{..n}. coeff (UP R) p i ⊗R coeff (UP R) q (n - i))

lemma up_eqI:

      [| !!n. coeff (UP R) p n = coeff (UP R) q n; p ∈ carrier (UP R);
         q ∈ carrier (UP R) |]
      ==> p = q

### Polynomials form a commutative ring.

lemma UP_mult_closed:

      [| UP_cring R; p ∈ carrier (UP R); q ∈ carrier (UP R) |]
      ==> p ⊗UP R q ∈ carrier (UP R)

lemma UP_one_closed:

      UP_cring R ==> \<one>UP R ∈ carrier (UP R)

lemma UP_zero_closed:

      UP_cring R ==> \<zero>UP R ∈ carrier (UP R)

lemma UP_a_closed:

      [| UP_cring R; p ∈ carrier (UP R); q ∈ carrier (UP R) |]
      ==> p ⊕UP R q ∈ carrier (UP R)

lemma monom_closed:

      [| UP_cring R; a ∈ carrier R |] ==> monom (UP R) a n ∈ carrier (UP R)

lemma UP_smult_closed:

      [| UP_cring R; a ∈ carrier R; p ∈ carrier (UP R) |]
      ==> a \<odot>UP R p ∈ carrier (UP R)

lemma coeff_closed:

      p ∈ carrier (UP R) ==> coeff (UP R) p n ∈ carrier R

lemma UP_a_assoc:

      [| UP_cring R; p ∈ carrier (UP R); q ∈ carrier (UP R); r ∈ carrier (UP R) |]
      ==> p ⊕UP R q ⊕UP R r = p ⊕UP R (q ⊕UP R r)

lemma UP_l_zero:

      [| UP_cring R; p ∈ carrier (UP R) |] ==> \<zero>UP R ⊕UP R p = p

lemma UP_l_neg_ex:

      [| UP_cring R; p ∈ carrier (UP R) |]
      ==> ∃q∈carrier (UP R). q ⊕UP R p = \<zero>UP R

lemma UP_a_comm:

      [| UP_cring R; p ∈ carrier (UP R); q ∈ carrier (UP R) |]
      ==> p ⊕UP R q = q ⊕UP R p

lemma UP_m_assoc:

      [| UP_cring R; p ∈ carrier (UP R); q ∈ carrier (UP R); r ∈ carrier (UP R) |]
      ==> p ⊗UP R q ⊗UP R r = p ⊗UP R (q ⊗UP R r)

lemma UP_l_one:

      [| UP_cring R; p ∈ carrier (UP R) |] ==> \<one>UP R ⊗UP R p = p

lemma UP_l_distr:

      [| UP_cring R; p ∈ carrier (UP R); q ∈ carrier (UP R); r ∈ carrier (UP R) |]
      ==> (p ⊕UP R q) ⊗UP R r = p ⊗UP R r ⊕UP R q ⊗UP R r

lemma UP_m_comm:

      [| UP_cring R; p ∈ carrier (UP R); q ∈ carrier (UP R) |]
      ==> p ⊗UP R q = q ⊗UP R p

theorem UP_cring:

      UP_cring R ==> cring (UP R)

lemma UP_ring:

      UP_cring R ==> ring (UP R)

lemma UP_a_inv_closed:

      [| UP_cring R; p ∈ carrier (UP R) |] ==> \<ominus>UP R p ∈ carrier (UP R)

lemma coeff_a_inv:

      [| UP_cring R; p ∈ carrier (UP R) |]
      ==> coeff (UP R) (\<ominus>UP R p) n = \<ominus>R coeff (UP R) p n

### Polynomials form an Algebra

lemma UP_smult_l_distr:

      [| UP_cring R; a ∈ carrier R; b ∈ carrier R; p ∈ carrier (UP R) |]
      ==> (a ⊕R b) \<odot>UP R p = a \<odot>UP R p ⊕UP R b \<odot>UP R p

lemma UP_smult_r_distr:

      [| UP_cring R; a ∈ carrier R; p ∈ carrier (UP R); q ∈ carrier (UP R) |]
      ==> a \<odot>UP R (p ⊕UP R q) = a \<odot>UP R p ⊕UP R a \<odot>UP R q

lemma UP_smult_assoc1:

      [| UP_cring R; a ∈ carrier R; b ∈ carrier R; p ∈ carrier (UP R) |]
      ==> a ⊗R b \<odot>UP R p = a \<odot>UP R (b \<odot>UP R p)

lemma UP_smult_one:

      [| UP_cring R; p ∈ carrier (UP R) |] ==> \<one>R \<odot>UP R p = p

lemma UP_smult_assoc2:

      [| UP_cring R; a ∈ carrier R; p ∈ carrier (UP R); q ∈ carrier (UP R) |]
      ==> a \<odot>UP R p ⊗UP R q = a \<odot>UP R (p ⊗UP R q)

lemma cring:

      cring R ==> cring R

lemma UP_algebra:

      UP_cring R ==> algebra R (UP R)

### Further lemmas involving monomials

lemma monom_zero:

      UP_cring R ==> monom (UP R) \<zero>R n = \<zero>UP R

lemma monom_mult_is_smult:

      [| UP_cring R; a ∈ carrier R; p ∈ carrier (UP R) |]
      ==> monom (UP R) a 0 ⊗UP R p = a \<odot>UP R p

lemma monom_add:

      [| UP_cring R; a ∈ carrier R; b ∈ carrier R |]
      ==> monom (UP R) (a ⊕R b) n = monom (UP R) a n ⊕UP R monom (UP R) b n

lemma monom_one_Suc:

      UP_cring R
      ==> monom (UP R) \<one>R (Suc n) =
          monom (UP R) \<one>R n ⊗UP R monom (UP R) \<one>R 1

lemma monom_mult_smult:

      [| UP_cring R; a ∈ carrier R; b ∈ carrier R |]
      ==> monom (UP R) (a ⊗R b) n = a \<odot>UP R monom (UP R) b n

lemma monom_one:

      UP_cring R ==> monom (UP R) \<one>R 0 = \<one>UP R

lemma monom_one_mult:

      UP_cring R
      ==> monom (UP R) \<one>R (n + m) =
          monom (UP R) \<one>R n ⊗UP R monom (UP R) \<one>R m

lemma monom_mult:

      [| UP_cring R; a ∈ carrier R; b ∈ carrier R |]
      ==> monom (UP R) (a ⊗R b) (n + m) = monom (UP R) a n ⊗UP R monom (UP R) b m

lemma monom_a_inv:

      [| UP_cring R; a ∈ carrier R |]
      ==> monom (UP R) (\<ominus>R a) n = \<ominus>UP R monom (UP R) a n

lemma monom_inj:

      UP_cring R ==> inj_on (%a. monom (UP R) a n) (carrier R)

### The degree function

lemma deg_aboveI:

      [| UP_cring R; !!m. n < m ==> coeff (UP R) p m = \<zero>R; p ∈ carrier (UP R) |]
      ==> deg R p ≤ n

lemma deg_aboveD:

      [| UP_cring R; deg R p < m; p ∈ carrier (UP R) |]
      ==> coeff (UP R) p m = \<zero>R

lemma deg_belowI:

      [| UP_cring R; n ≠ 0 ==> coeff (UP R) p n ≠ \<zero>R; p ∈ carrier (UP R) |]
      ==> n ≤ deg R p

lemma lcoeff_nonzero_deg:

      [| UP_cring R; deg R p ≠ 0; p ∈ carrier (UP R) |]
      ==> coeff (UP R) p (deg R p) ≠ \<zero>R

lemma lcoeff_nonzero_nonzero:

      [| UP_cring R; deg R p = 0; p ≠ \<zero>UP R; p ∈ carrier (UP R) |]
      ==> coeff (UP R) p 0 ≠ \<zero>R

lemma lcoeff_nonzero:

      [| UP_cring R; p ≠ \<zero>UP R; p ∈ carrier (UP R) |]
      ==> coeff (UP R) p (deg R p) ≠ \<zero>R

lemma deg_eqI:

      [| UP_cring R; !!m. n < m ==> coeff (UP R) p m = \<zero>R;
         !!n. n ≠ 0 ==> coeff (UP R) p n ≠ \<zero>R; p ∈ carrier (UP R) |]
      ==> deg R p = n

lemma deg_add:

      [| UP_cring R; p ∈ carrier (UP R); q ∈ carrier (UP R) |]
      ==> deg R (p ⊕UP R q) ≤ max (deg R p) (deg R q)

lemma deg_monom_le:

      [| UP_cring R; a ∈ carrier R |] ==> deg R (monom (UP R) a n) ≤ n

lemma deg_monom:

      [| UP_cring R; a ≠ \<zero>R; a ∈ carrier R |] ==> deg R (monom (UP R) a n) = n

lemma deg_const:

      [| UP_cring R; a ∈ carrier R |] ==> deg R (monom (UP R) a 0) = 0

lemma deg_zero:

      UP_cring R ==> deg R \<zero>UP R = 0

lemma deg_one:

      UP_cring R ==> deg R \<one>UP R = 0

lemma deg_uminus:

      [| UP_cring R; p ∈ carrier (UP R) |] ==> deg R (\<ominus>UP R p) = deg R p

lemma deg_smult_ring:

      [| UP_domain R; a ∈ carrier R; p ∈ carrier (UP R) |]
      ==> deg R (a \<odot>UP R p) ≤ (if a = \<zero>R then 0 else deg R p)

lemma deg_smult:

      [| UP_domain R; a ∈ carrier R; p ∈ carrier (UP R) |]
      ==> deg R (a \<odot>UP R p) = (if a = \<zero>R then 0 else deg R p)

lemma deg_mult_cring:

      [| UP_cring R; p ∈ carrier (UP R); q ∈ carrier (UP R) |]
      ==> deg R (p ⊗UP R q) ≤ deg R p + deg R q

lemma deg_mult:

      [| UP_domain R; p ≠ \<zero>UP R; q ≠ \<zero>UP R; p ∈ carrier (UP R);
         q ∈ carrier (UP R) |]
      ==> deg R (p ⊗UP R q) = deg R p + deg R q

lemma coeff_finsum:

      [| UP_cring R; finite A; p ∈ A -> carrier (UP R) |]
      ==> coeff (UP R) (finsum (UP R) p A) k = (\<Oplus>Ri∈A. coeff (UP R) (p i) k)

lemma up_repr:

      [| UP_cring R; p ∈ carrier (UP R) |]
      ==> (\<Oplus>UP Ri∈{..deg R p}. monom (UP R) (coeff (UP R) p i) i) = p

lemma up_repr_le:

      [| UP_cring R; deg R p ≤ n; p ∈ carrier (UP R) |]
      ==> (\<Oplus>UP Ri∈{..n}. monom (UP R) (coeff (UP R) p i) i) = p

### Polynomials over an integral domain form an integral domain

lemma domainI:

      [| cring R; \<one>R ≠ \<zero>R;
         !!a b. [| a ⊗R b = \<zero>R; a ∈ carrier R; b ∈ carrier R |]
                ==> a = \<zero>R ∨ b = \<zero>R |]
      ==> domain R

lemma UP_one_not_zero:

      UP_domain R ==> \<one>UP R ≠ \<zero>UP R

lemma UP_integral:

      [| UP_domain R; p ⊗UP R q = \<zero>UP R; p ∈ carrier (UP R);
         q ∈ carrier (UP R) |]
      ==> p = \<zero>UP R ∨ q = \<zero>UP R

theorem UP_domain:

      UP_domain R ==> domain (UP R)

### Evaluation Homomorphism and Universal Property

theorem diagonal_sum:

      [| cring R; f ∈ {..n + m} -> carrier R; g ∈ {..n + m} -> carrier R |]
      ==> (\<Oplus>Rk∈{..n + m}. \<Oplus>Ri∈{..k}. f i ⊗R g (k - i)) =
          (\<Oplus>Rk∈{..n + m}. \<Oplus>Ri∈{..n + m - k}. f k ⊗R g i)

lemma boundD_carrier:

      [| abelian_monoid G; bound \<zero>G n f; n < m |] ==> f m ∈ carrier G

theorem cauchy_product:

      [| cring R; bound \<zero>R n f; bound \<zero>R m g; f ∈ {..n} -> carrier R;
         g ∈ {..m} -> carrier R |]
      ==> (\<Oplus>Rk∈{..n + m}. \<Oplus>Ri∈{..k}. f i ⊗R g (k - i)) =
          finsum R f {..n} ⊗R finsum R g {..m}

lemma const_ring_hom:

      UP_cring R ==> (%a. monom (UP R) a 0) ∈ ring_hom R (UP R)

lemma eval_on_carrier:

      p ∈ carrier (UP R)
      ==> eval R S phi s p =
          (\<Oplus>Si∈{..deg R p}. phi (coeff (UP R) p i) ⊗S s (^)S i)

lemma eval_extensional:

      eval R S phi p ∈ extensional (carrier (UP R))

theorem eval_ring_hom:

      [| UP_pre_univ_prop R S h; s ∈ carrier S |] ==> eval R S h s ∈ ring_hom (UP R) S

lemma eval_const:

      [| UP_pre_univ_prop R S h; s ∈ carrier S; r ∈ carrier R |]
      ==> eval R S h s (monom (UP R) r 0) = h r

lemma eval_monom1:

      [| UP_pre_univ_prop R S h; s ∈ carrier S |]
      ==> eval R S h s (monom (UP R) \<one>R 1) = s

lemma monom_pow:

      [| UP_cring R; a ∈ carrier R |]
      ==> monom (UP R) a n (^)UP R m = monom (UP R) (a (^)R m) (n * m)

lemma hom_pow:

      [| ring_hom_cring R S h; x ∈ carrier R |] ==> h (x (^)R n) = h x (^)S n

lemma Eval_monom:

      [| UP_univ_prop R S h s; r ∈ carrier R |]
      ==> eval R S h s (monom (UP R) r n) = h r ⊗S s (^)S n

lemma eval_monom:

      [| UP_pre_univ_prop R S h; r ∈ carrier R; s ∈ carrier S |]
      ==> eval R S h s (monom (UP R) r n) = h r ⊗S s (^)S n

lemma Eval_smult:

      [| UP_univ_prop R S h s; r ∈ carrier R; p ∈ carrier (UP R) |]
      ==> eval R S h s (r \<odot>UP R p) = h r ⊗S eval R S h s p

lemma ring_hom_cringI:

      [| cring R; cring S; h ∈ ring_hom R S |] ==> ring_hom_cring R S h

lemma UP_hom_unique:

      [| UP_pre_univ_prop R S h; ring_hom_cring (UP R) S Phi;
         ring_hom_cring (UP R) S Psi; Phi (monom (UP R) \<one>R (Suc 0)) = s;
         !!r. r ∈ carrier R ==> Phi (monom (UP R) r 0) = h r;
         Psi (monom (UP R) \<one>R (Suc 0)) = s;
         !!r. r ∈ carrier R ==> Psi (monom (UP R) r 0) = h r; p ∈ carrier (UP R);
         s ∈ carrier S |]
      ==> Phi p = Psi p

lemma ring_homD:

      [| UP_pre_univ_prop R S h; Phi ∈ ring_hom (UP R) S |]
      ==> ring_hom_cring (UP R) S Phi

theorem UP_universal_property:

      [| UP_pre_univ_prop R S h; s ∈ carrier S |]
      ==> ∃!Phi. Phi ∈ ring_hom (UP R) S ∩ extensional (carrier (UP R)) ∧
                 Phi (monom (UP R) \<one>R 1) = s ∧
                 (∀r∈carrier R. Phi (monom (UP R) r 0) = h r)

### Sample application of evaluation homomorphism

lemma UP_pre_univ_propI:

      [| cring R; cring S; h ∈ ring_hom R S |] ==> UP_pre_univ_prop R S h

lemma INTEG_cring:

      cring INTEG

lemma INTEG_id_eval:

      UP_pre_univ_prop INTEG INTEG id

lemma INTEG_closed:

      z ∈ carrier INTEG

lemma INTEG_mult:

      z ⊗INTEG w = z * w

lemma INTEG_pow:

      z (^)INTEG n = z ^ n

lemma

      eval INTEG INTEG id 10 (monom (UP INTEG) 5 2) = 500