src/HOL/Algebra/UnivPoly.thy
author ballarin
Fri Aug 01 18:10:52 2008 +0200 (2008-08-01)
changeset 27717 21bbd410ba04
parent 27714 27b4d7c01f8b
child 27933 4b867f6a65d3
permissions -rw-r--r--
Generalised polynomial lemmas from cring to ring.
     1 (*
     2   Title:     HOL/Algebra/UnivPoly.thy
     3   Id:        $Id$
     4   Author:    Clemens Ballarin, started 9 December 1996
     5   Copyright: Clemens Ballarin
     6 
     7 Contributions by Jesus Aransay.
     8 *)
     9 
    10 theory UnivPoly imports Module RingHom begin
    11 
    12 
    13 section {* Univariate Polynomials *}
    14 
    15 text {*
    16   Polynomials are formalised as modules with additional operations for
    17   extracting coefficients from polynomials and for obtaining monomials
    18   from coefficients and exponents (record @{text "up_ring"}).  The
    19   carrier set is a set of bounded functions from Nat to the
    20   coefficient domain.  Bounded means that these functions return zero
    21   above a certain bound (the degree).  There is a chapter on the
    22   formalisation of polynomials in the PhD thesis \cite{Ballarin:1999},
    23   which was implemented with axiomatic type classes.  This was later
    24   ported to Locales.
    25 *}
    26 
    27 
    28 subsection {* The Constructor for Univariate Polynomials *}
    29 
    30 text {*
    31   Functions with finite support.
    32 *}
    33 
    34 locale bound =
    35   fixes z :: 'a
    36     and n :: nat
    37     and f :: "nat => 'a"
    38   assumes bound: "!!m. n < m \<Longrightarrow> f m = z"
    39 
    40 declare bound.intro [intro!]
    41   and bound.bound [dest]
    42 
    43 lemma bound_below:
    44   assumes bound: "bound z m f" and nonzero: "f n \<noteq> z" shows "n \<le> m"
    45 proof (rule classical)
    46   assume "~ ?thesis"
    47   then have "m < n" by arith
    48   with bound have "f n = z" ..
    49   with nonzero show ?thesis by contradiction
    50 qed
    51 
    52 record ('a, 'p) up_ring = "('a, 'p) module" +
    53   monom :: "['a, nat] => 'p"
    54   coeff :: "['p, nat] => 'a"
    55 
    56 constdefs (structure R)
    57   up :: "('a, 'm) ring_scheme => (nat => 'a) set"
    58   "up R == {f. f \<in> UNIV -> carrier R & (EX n. bound \<zero> n f)}"
    59   UP :: "('a, 'm) ring_scheme => ('a, nat => 'a) up_ring"
    60   "UP R == (|
    61     carrier = up R,
    62     mult = (%p:up R. %q:up R. %n. \<Oplus>i \<in> {..n}. p i \<otimes> q (n-i)),
    63     one = (%i. if i=0 then \<one> else \<zero>),
    64     zero = (%i. \<zero>),
    65     add = (%p:up R. %q:up R. %i. p i \<oplus> q i),
    66     smult = (%a:carrier R. %p:up R. %i. a \<otimes> p i),
    67     monom = (%a:carrier R. %n i. if i=n then a else \<zero>),
    68     coeff = (%p:up R. %n. p n) |)"
    69 
    70 text {*
    71   Properties of the set of polynomials @{term up}.
    72 *}
    73 
    74 lemma mem_upI [intro]:
    75   "[| !!n. f n \<in> carrier R; EX n. bound (zero R) n f |] ==> f \<in> up R"
    76   by (simp add: up_def Pi_def)
    77 
    78 lemma mem_upD [dest]:
    79   "f \<in> up R ==> f n \<in> carrier R"
    80   by (simp add: up_def Pi_def)
    81 
    82 context ring
    83 begin
    84 
    85 lemma bound_upD [dest]: "f \<in> up R ==> EX n. bound \<zero> n f" by (simp add: up_def)
    86 
    87 lemma up_one_closed: "(%n. if n = 0 then \<one> else \<zero>) \<in> up R" using up_def by force
    88 
    89 lemma up_smult_closed: "[| a \<in> carrier R; p \<in> up R |] ==> (%i. a \<otimes> p i) \<in> up R" by force
    90 
    91 lemma up_add_closed:
    92   "[| p \<in> up R; q \<in> up R |] ==> (%i. p i \<oplus> q i) \<in> up R"
    93 proof
    94   fix n
    95   assume "p \<in> up R" and "q \<in> up R"
    96   then show "p n \<oplus> q n \<in> carrier R"
    97     by auto
    98 next
    99   assume UP: "p \<in> up R" "q \<in> up R"
   100   show "EX n. bound \<zero> n (%i. p i \<oplus> q i)"
   101   proof -
   102     from UP obtain n where boundn: "bound \<zero> n p" by fast
   103     from UP obtain m where boundm: "bound \<zero> m q" by fast
   104     have "bound \<zero> (max n m) (%i. p i \<oplus> q i)"
   105     proof
   106       fix i
   107       assume "max n m < i"
   108       with boundn and boundm and UP show "p i \<oplus> q i = \<zero>" by fastsimp
   109     qed
   110     then show ?thesis ..
   111   qed
   112 qed
   113 
   114 lemma up_a_inv_closed:
   115   "p \<in> up R ==> (%i. \<ominus> (p i)) \<in> up R"
   116 proof
   117   assume R: "p \<in> up R"
   118   then obtain n where "bound \<zero> n p" by auto
   119   then have "bound \<zero> n (%i. \<ominus> p i)" by auto
   120   then show "EX n. bound \<zero> n (%i. \<ominus> p i)" by auto
   121 qed auto
   122 
   123 lemma up_mult_closed:
   124   "[| p \<in> up R; q \<in> up R |] ==>
   125   (%n. \<Oplus>i \<in> {..n}. p i \<otimes> q (n-i)) \<in> up R"
   126 proof
   127   fix n
   128   assume "p \<in> up R" "q \<in> up R"
   129   then show "(\<Oplus>i \<in> {..n}. p i \<otimes> q (n-i)) \<in> carrier R"
   130     by (simp add: mem_upD  funcsetI)
   131 next
   132   assume UP: "p \<in> up R" "q \<in> up R"
   133   show "EX n. bound \<zero> n (%n. \<Oplus>i \<in> {..n}. p i \<otimes> q (n-i))"
   134   proof -
   135     from UP obtain n where boundn: "bound \<zero> n p" by fast
   136     from UP obtain m where boundm: "bound \<zero> m q" by fast
   137     have "bound \<zero> (n + m) (%n. \<Oplus>i \<in> {..n}. p i \<otimes> q (n - i))"
   138     proof
   139       fix k assume bound: "n + m < k"
   140       {
   141         fix i
   142         have "p i \<otimes> q (k-i) = \<zero>"
   143         proof (cases "n < i")
   144           case True
   145           with boundn have "p i = \<zero>" by auto
   146           moreover from UP have "q (k-i) \<in> carrier R" by auto
   147           ultimately show ?thesis by simp
   148         next
   149           case False
   150           with bound have "m < k-i" by arith
   151           with boundm have "q (k-i) = \<zero>" by auto
   152           moreover from UP have "p i \<in> carrier R" by auto
   153           ultimately show ?thesis by simp
   154         qed
   155       }
   156       then show "(\<Oplus>i \<in> {..k}. p i \<otimes> q (k-i)) = \<zero>"
   157         by (simp add: Pi_def)
   158     qed
   159     then show ?thesis by fast
   160   qed
   161 qed
   162 
   163 end
   164 
   165 
   166 subsection {* Effect of Operations on Coefficients *}
   167 
   168 locale UP =
   169   fixes R (structure) and P (structure)
   170   defines P_def: "P == UP R"
   171 
   172 locale UP_ring = UP + ring R
   173 
   174 locale UP_cring = UP + cring R
   175 
   176 interpretation UP_cring < UP_ring
   177   by (rule P_def) intro_locales
   178 
   179 locale UP_domain = UP + "domain" R
   180 
   181 interpretation UP_domain < UP_cring
   182   by (rule P_def) intro_locales
   183 
   184 context UP
   185 begin
   186 
   187 text {*Temporarily declare @{thm [locale=UP] P_def} as simp rule.*}
   188 
   189 declare P_def [simp]
   190 
   191 lemma up_eqI:
   192   assumes prem: "!!n. coeff P p n = coeff P q n" and R: "p \<in> carrier P" "q \<in> carrier P"
   193   shows "p = q"
   194 proof
   195   fix x
   196   from prem and R show "p x = q x" by (simp add: UP_def)
   197 qed
   198 
   199 lemma coeff_closed [simp]:
   200   "p \<in> carrier P ==> coeff P p n \<in> carrier R" by (auto simp add: UP_def)
   201 
   202 end
   203 
   204 context UP_ring 
   205 begin
   206 
   207 (* Theorems generalised to rings by Jesus Aransay. *)
   208 
   209 lemma coeff_monom [simp]:
   210   "a \<in> carrier R ==> coeff P (monom P a m) n = (if m=n then a else \<zero>)"
   211 proof -
   212   assume R: "a \<in> carrier R"
   213   then have "(%n. if n = m then a else \<zero>) \<in> up R"
   214     using up_def by force
   215   with R show ?thesis by (simp add: UP_def)
   216 qed
   217 
   218 lemma coeff_zero [simp]: "coeff P \<zero>\<^bsub>P\<^esub> n = \<zero>" by (auto simp add: UP_def)
   219 
   220 lemma coeff_one [simp]: "coeff P \<one>\<^bsub>P\<^esub> n = (if n=0 then \<one> else \<zero>)"
   221   using up_one_closed by (simp add: UP_def)
   222 
   223 lemma coeff_smult [simp]:
   224   "[| a \<in> carrier R; p \<in> carrier P |] ==> coeff P (a \<odot>\<^bsub>P\<^esub> p) n = a \<otimes> coeff P p n"
   225   by (simp add: UP_def up_smult_closed)
   226 
   227 lemma coeff_add [simp]:
   228   "[| p \<in> carrier P; q \<in> carrier P |] ==> coeff P (p \<oplus>\<^bsub>P\<^esub> q) n = coeff P p n \<oplus> coeff P q n"
   229   by (simp add: UP_def up_add_closed)
   230 
   231 lemma coeff_mult [simp]:
   232   "[| p \<in> carrier P; q \<in> carrier P |] ==> coeff P (p \<otimes>\<^bsub>P\<^esub> q) n = (\<Oplus>i \<in> {..n}. coeff P p i \<otimes> coeff P q (n-i))"
   233   by (simp add: UP_def up_mult_closed)
   234 
   235 end
   236 
   237 
   238 subsection {* Polynomials Form a Ring. *}
   239 
   240 context UP_ring
   241 begin
   242 
   243 text {* Operations are closed over @{term P}. *}
   244 
   245 lemma UP_mult_closed [simp]:
   246   "[| p \<in> carrier P; q \<in> carrier P |] ==> p \<otimes>\<^bsub>P\<^esub> q \<in> carrier P" by (simp add: UP_def up_mult_closed)
   247 
   248 lemma UP_one_closed [simp]:
   249   "\<one>\<^bsub>P\<^esub> \<in> carrier P" by (simp add: UP_def up_one_closed)
   250 
   251 lemma UP_zero_closed [intro, simp]:
   252   "\<zero>\<^bsub>P\<^esub> \<in> carrier P" by (auto simp add: UP_def)
   253 
   254 lemma UP_a_closed [intro, simp]:
   255   "[| p \<in> carrier P; q \<in> carrier P |] ==> p \<oplus>\<^bsub>P\<^esub> q \<in> carrier P" by (simp add: UP_def up_add_closed)
   256 
   257 lemma monom_closed [simp]:
   258   "a \<in> carrier R ==> monom P a n \<in> carrier P" by (auto simp add: UP_def up_def Pi_def)
   259 
   260 lemma UP_smult_closed [simp]:
   261   "[| a \<in> carrier R; p \<in> carrier P |] ==> a \<odot>\<^bsub>P\<^esub> p \<in> carrier P" by (simp add: UP_def up_smult_closed)
   262 
   263 end
   264 
   265 declare (in UP) P_def [simp del]
   266 
   267 text {* Algebraic ring properties *}
   268 
   269 context UP_ring
   270 begin
   271 
   272 lemma UP_a_assoc:
   273   assumes R: "p \<in> carrier P" "q \<in> carrier P" "r \<in> carrier P"
   274   shows "(p \<oplus>\<^bsub>P\<^esub> q) \<oplus>\<^bsub>P\<^esub> r = p \<oplus>\<^bsub>P\<^esub> (q \<oplus>\<^bsub>P\<^esub> r)" by (rule up_eqI, simp add: a_assoc R, simp_all add: R)
   275 
   276 lemma UP_l_zero [simp]:
   277   assumes R: "p \<in> carrier P"
   278   shows "\<zero>\<^bsub>P\<^esub> \<oplus>\<^bsub>P\<^esub> p = p" by (rule up_eqI, simp_all add: R)
   279 
   280 lemma UP_l_neg_ex:
   281   assumes R: "p \<in> carrier P"
   282   shows "EX q : carrier P. q \<oplus>\<^bsub>P\<^esub> p = \<zero>\<^bsub>P\<^esub>"
   283 proof -
   284   let ?q = "%i. \<ominus> (p i)"
   285   from R have closed: "?q \<in> carrier P"
   286     by (simp add: UP_def P_def up_a_inv_closed)
   287   from R have coeff: "!!n. coeff P ?q n = \<ominus> (coeff P p n)"
   288     by (simp add: UP_def P_def up_a_inv_closed)
   289   show ?thesis
   290   proof
   291     show "?q \<oplus>\<^bsub>P\<^esub> p = \<zero>\<^bsub>P\<^esub>"
   292       by (auto intro!: up_eqI simp add: R closed coeff R.l_neg)
   293   qed (rule closed)
   294 qed
   295 
   296 lemma UP_a_comm:
   297   assumes R: "p \<in> carrier P" "q \<in> carrier P"
   298   shows "p \<oplus>\<^bsub>P\<^esub> q = q \<oplus>\<^bsub>P\<^esub> p" by (rule up_eqI, simp add: a_comm R, simp_all add: R)
   299 
   300 end
   301 
   302 
   303 context UP_ring
   304 begin
   305 
   306 lemma UP_m_assoc:
   307   assumes R: "p \<in> carrier P" "q \<in> carrier P" "r \<in> carrier P"
   308   shows "(p \<otimes>\<^bsub>P\<^esub> q) \<otimes>\<^bsub>P\<^esub> r = p \<otimes>\<^bsub>P\<^esub> (q \<otimes>\<^bsub>P\<^esub> r)"
   309 proof (rule up_eqI)
   310   fix n
   311   {
   312     fix k and a b c :: "nat=>'a"
   313     assume R: "a \<in> UNIV -> carrier R" "b \<in> UNIV -> carrier R"
   314       "c \<in> UNIV -> carrier R"
   315     then have "k <= n ==>
   316       (\<Oplus>j \<in> {..k}. (\<Oplus>i \<in> {..j}. a i \<otimes> b (j-i)) \<otimes> c (n-j)) =
   317       (\<Oplus>j \<in> {..k}. a j \<otimes> (\<Oplus>i \<in> {..k-j}. b i \<otimes> c (n-j-i)))"
   318       (is "_ \<Longrightarrow> ?eq k")
   319     proof (induct k)
   320       case 0 then show ?case by (simp add: Pi_def m_assoc)
   321     next
   322       case (Suc k)
   323       then have "k <= n" by arith
   324       from this R have "?eq k" by (rule Suc)
   325       with R show ?case
   326         by (simp cong: finsum_cong
   327              add: Suc_diff_le Pi_def l_distr r_distr m_assoc)
   328            (simp cong: finsum_cong add: Pi_def a_ac finsum_ldistr m_assoc)
   329     qed
   330   }
   331   with R show "coeff P ((p \<otimes>\<^bsub>P\<^esub> q) \<otimes>\<^bsub>P\<^esub> r) n = coeff P (p \<otimes>\<^bsub>P\<^esub> (q \<otimes>\<^bsub>P\<^esub> r)) n"
   332     by (simp add: Pi_def)
   333 qed (simp_all add: R)
   334 
   335 lemma UP_r_one [simp]:
   336   assumes R: "p \<in> carrier P" shows "p \<otimes>\<^bsub>P\<^esub> \<one>\<^bsub>P\<^esub> = p"
   337 proof (rule up_eqI)
   338   fix n
   339   show "coeff P (p \<otimes>\<^bsub>P\<^esub> \<one>\<^bsub>P\<^esub>) n = coeff P p n"
   340   proof (cases n)
   341     case 0 
   342     {
   343       with R show ?thesis by simp
   344     }
   345   next
   346     case Suc
   347     {
   348       (*JE: in the locale UP_cring the proof was solved only with "by (simp del: finsum_Suc add: finsum_Suc2 Pi_def)", but I did not
   349       get it to work here*)
   350       fix nn assume Succ: "n = Suc nn"
   351       have "coeff P (p \<otimes>\<^bsub>P\<^esub> \<one>\<^bsub>P\<^esub>) (Suc nn) = coeff P p (Suc nn)"
   352       proof -
   353 	have "coeff P (p \<otimes>\<^bsub>P\<^esub> \<one>\<^bsub>P\<^esub>) (Suc nn) = (\<Oplus>i\<in>{..Suc nn}. coeff P p i \<otimes> (if Suc nn \<le> i then \<one> else \<zero>))" using R by simp
   354 	also have "\<dots> = coeff P p (Suc nn) \<otimes> (if Suc nn \<le> Suc nn then \<one> else \<zero>) \<oplus> (\<Oplus>i\<in>{..nn}. coeff P p i \<otimes> (if Suc nn \<le> i then \<one> else \<zero>))"
   355 	  using finsum_Suc [of "(\<lambda>i::nat. coeff P p i \<otimes> (if Suc nn \<le> i then \<one> else \<zero>))" "nn"] unfolding Pi_def using R by simp
   356 	also have "\<dots> = coeff P p (Suc nn) \<otimes> (if Suc nn \<le> Suc nn then \<one> else \<zero>)"
   357 	proof -
   358 	  have "(\<Oplus>i\<in>{..nn}. coeff P p i \<otimes> (if Suc nn \<le> i then \<one> else \<zero>)) = (\<Oplus>i\<in>{..nn}. \<zero>)"
   359 	    using finsum_cong [of "{..nn}" "{..nn}" "(\<lambda>i::nat. coeff P p i \<otimes> (if Suc nn \<le> i then \<one> else \<zero>))" "(\<lambda>i::nat. \<zero>)"] using R 
   360 	    unfolding Pi_def by simp
   361 	  also have "\<dots> = \<zero>" by simp
   362 	  finally show ?thesis using r_zero R by simp
   363 	qed
   364 	also have "\<dots> = coeff P p (Suc nn)" using R by simp
   365 	finally show ?thesis by simp
   366       qed
   367       then show ?thesis using Succ by simp
   368     }
   369   qed
   370 qed (simp_all add: R)
   371   
   372 lemma UP_l_one [simp]:
   373   assumes R: "p \<in> carrier P"
   374   shows "\<one>\<^bsub>P\<^esub> \<otimes>\<^bsub>P\<^esub> p = p"
   375 proof (rule up_eqI)
   376   fix n
   377   show "coeff P (\<one>\<^bsub>P\<^esub> \<otimes>\<^bsub>P\<^esub> p) n = coeff P p n"
   378   proof (cases n)
   379     case 0 with R show ?thesis by simp
   380   next
   381     case Suc with R show ?thesis
   382       by (simp del: finsum_Suc add: finsum_Suc2 Pi_def)
   383   qed
   384 qed (simp_all add: R)
   385 
   386 lemma UP_l_distr:
   387   assumes R: "p \<in> carrier P" "q \<in> carrier P" "r \<in> carrier P"
   388   shows "(p \<oplus>\<^bsub>P\<^esub> q) \<otimes>\<^bsub>P\<^esub> r = (p \<otimes>\<^bsub>P\<^esub> r) \<oplus>\<^bsub>P\<^esub> (q \<otimes>\<^bsub>P\<^esub> r)"
   389   by (rule up_eqI) (simp add: l_distr R Pi_def, simp_all add: R)
   390 
   391 lemma UP_r_distr:
   392   assumes R: "p \<in> carrier P" "q \<in> carrier P" "r \<in> carrier P"
   393   shows "r \<otimes>\<^bsub>P\<^esub> (p \<oplus>\<^bsub>P\<^esub> q) = (r \<otimes>\<^bsub>P\<^esub> p) \<oplus>\<^bsub>P\<^esub> (r \<otimes>\<^bsub>P\<^esub> q)"
   394   by (rule up_eqI) (simp add: r_distr R Pi_def, simp_all add: R)
   395 
   396 theorem UP_ring: "ring P"
   397   by (auto intro!: ringI abelian_groupI monoidI UP_a_assoc)
   398 (auto intro: UP_a_comm UP_l_neg_ex UP_m_assoc UP_l_distr UP_r_distr)
   399 
   400 end
   401 
   402 subsection {* Polynomials form a commutative Ring. *}
   403 
   404 context UP_cring
   405 begin
   406 
   407 lemma UP_m_comm:
   408   assumes R1: "p \<in> carrier P" and R2: "q \<in> carrier P" shows "p \<otimes>\<^bsub>P\<^esub> q = q \<otimes>\<^bsub>P\<^esub> p"
   409 proof (rule up_eqI)
   410   fix n
   411   {
   412     fix k and a b :: "nat=>'a"
   413     assume R: "a \<in> UNIV -> carrier R" "b \<in> UNIV -> carrier R"
   414     then have "k <= n ==>
   415       (\<Oplus>i \<in> {..k}. a i \<otimes> b (n-i)) = (\<Oplus>i \<in> {..k}. a (k-i) \<otimes> b (i+n-k))"
   416       (is "_ \<Longrightarrow> ?eq k")
   417     proof (induct k)
   418       case 0 then show ?case by (simp add: Pi_def)
   419     next
   420       case (Suc k) then show ?case
   421         by (subst (2) finsum_Suc2) (simp add: Pi_def a_comm)+
   422     qed
   423   }
   424   note l = this
   425   from R1 R2 show "coeff P (p \<otimes>\<^bsub>P\<^esub> q) n =  coeff P (q \<otimes>\<^bsub>P\<^esub> p) n"
   426     unfolding coeff_mult [OF R1 R2, of n] 
   427     unfolding coeff_mult [OF R2 R1, of n] 
   428     using l [of "(\<lambda>i. coeff P p i)" "(\<lambda>i. coeff P q i)" "n"] by (simp add: Pi_def m_comm)
   429 qed (simp_all add: R1 R2)
   430 
   431 subsection{*Polynomials over a commutative ring for a commutative ring*}
   432 
   433 theorem UP_cring:
   434   "cring P" using UP_ring unfolding cring_def by (auto intro!: comm_monoidI UP_m_assoc UP_m_comm)
   435 
   436 end
   437 
   438 context UP_ring
   439 begin
   440 
   441 lemma UP_a_inv_closed [intro, simp]:
   442   "p \<in> carrier P ==> \<ominus>\<^bsub>P\<^esub> p \<in> carrier P"
   443   by (rule abelian_group.a_inv_closed [OF ring.is_abelian_group [OF UP_ring]])
   444 
   445 lemma coeff_a_inv [simp]:
   446   assumes R: "p \<in> carrier P"
   447   shows "coeff P (\<ominus>\<^bsub>P\<^esub> p) n = \<ominus> (coeff P p n)"
   448 proof -
   449   from R coeff_closed UP_a_inv_closed have
   450     "coeff P (\<ominus>\<^bsub>P\<^esub> p) n = \<ominus> coeff P p n \<oplus> (coeff P p n \<oplus> coeff P (\<ominus>\<^bsub>P\<^esub> p) n)"
   451     by algebra
   452   also from R have "... =  \<ominus> (coeff P p n)"
   453     by (simp del: coeff_add add: coeff_add [THEN sym]
   454       abelian_group.r_neg [OF ring.is_abelian_group [OF UP_ring]])
   455   finally show ?thesis .
   456 qed
   457 
   458 end
   459 
   460 interpretation UP_ring < ring P using UP_ring .
   461 interpretation UP_cring < cring P using UP_cring .
   462 
   463 
   464 subsection {* Polynomials Form an Algebra *}
   465 
   466 context UP_ring
   467 begin
   468 
   469 lemma UP_smult_l_distr:
   470   "[| a \<in> carrier R; b \<in> carrier R; p \<in> carrier P |] ==>
   471   (a \<oplus> b) \<odot>\<^bsub>P\<^esub> p = a \<odot>\<^bsub>P\<^esub> p \<oplus>\<^bsub>P\<^esub> b \<odot>\<^bsub>P\<^esub> p"
   472   by (rule up_eqI) (simp_all add: R.l_distr)
   473 
   474 lemma UP_smult_r_distr:
   475   "[| a \<in> carrier R; p \<in> carrier P; q \<in> carrier P |] ==>
   476   a \<odot>\<^bsub>P\<^esub> (p \<oplus>\<^bsub>P\<^esub> q) = a \<odot>\<^bsub>P\<^esub> p \<oplus>\<^bsub>P\<^esub> a \<odot>\<^bsub>P\<^esub> q"
   477   by (rule up_eqI) (simp_all add: R.r_distr)
   478 
   479 lemma UP_smult_assoc1:
   480       "[| a \<in> carrier R; b \<in> carrier R; p \<in> carrier P |] ==>
   481       (a \<otimes> b) \<odot>\<^bsub>P\<^esub> p = a \<odot>\<^bsub>P\<^esub> (b \<odot>\<^bsub>P\<^esub> p)"
   482   by (rule up_eqI) (simp_all add: R.m_assoc)
   483 
   484 lemma UP_smult_zero [simp]:
   485       "p \<in> carrier P ==> \<zero> \<odot>\<^bsub>P\<^esub> p = \<zero>\<^bsub>P\<^esub>"
   486   by (rule up_eqI) simp_all
   487 
   488 lemma UP_smult_one [simp]:
   489       "p \<in> carrier P ==> \<one> \<odot>\<^bsub>P\<^esub> p = p"
   490   by (rule up_eqI) simp_all
   491 
   492 lemma UP_smult_assoc2:
   493   "[| a \<in> carrier R; p \<in> carrier P; q \<in> carrier P |] ==>
   494   (a \<odot>\<^bsub>P\<^esub> p) \<otimes>\<^bsub>P\<^esub> q = a \<odot>\<^bsub>P\<^esub> (p \<otimes>\<^bsub>P\<^esub> q)"
   495   by (rule up_eqI) (simp_all add: R.finsum_rdistr R.m_assoc Pi_def)
   496 
   497 end
   498 
   499 text {*
   500   Interpretation of lemmas from @{term algebra}.
   501 *}
   502 
   503 lemma (in cring) cring:
   504   "cring R"
   505   by unfold_locales
   506 
   507 lemma (in UP_cring) UP_algebra:
   508   "algebra R P" by (auto intro!: algebraI R.cring UP_cring UP_smult_l_distr UP_smult_r_distr
   509     UP_smult_assoc1 UP_smult_assoc2)
   510 
   511 interpretation UP_cring < algebra R P using UP_algebra .
   512 
   513 
   514 subsection {* Further Lemmas Involving Monomials *}
   515 
   516 context UP_ring
   517 begin
   518 
   519 lemma monom_zero [simp]:
   520   "monom P \<zero> n = \<zero>\<^bsub>P\<^esub>" by (simp add: UP_def P_def)
   521 
   522 lemma monom_mult_is_smult:
   523   assumes R: "a \<in> carrier R" "p \<in> carrier P"
   524   shows "monom P a 0 \<otimes>\<^bsub>P\<^esub> p = a \<odot>\<^bsub>P\<^esub> p"
   525 proof (rule up_eqI)
   526   fix n
   527   show "coeff P (monom P a 0 \<otimes>\<^bsub>P\<^esub> p) n = coeff P (a \<odot>\<^bsub>P\<^esub> p) n"
   528   proof (cases n)
   529     case 0 with R show ?thesis by simp
   530   next
   531     case Suc with R show ?thesis
   532       using R.finsum_Suc2 by (simp del: R.finsum_Suc add: R.r_null Pi_def)
   533   qed
   534 qed (simp_all add: R)
   535 
   536 lemma monom_one [simp]:
   537   "monom P \<one> 0 = \<one>\<^bsub>P\<^esub>"
   538   by (rule up_eqI) simp_all
   539 
   540 lemma monom_add [simp]:
   541   "[| a \<in> carrier R; b \<in> carrier R |] ==>
   542   monom P (a \<oplus> b) n = monom P a n \<oplus>\<^bsub>P\<^esub> monom P b n"
   543   by (rule up_eqI) simp_all
   544 
   545 lemma monom_one_Suc:
   546   "monom P \<one> (Suc n) = monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> 1"
   547 proof (rule up_eqI)
   548   fix k
   549   show "coeff P (monom P \<one> (Suc n)) k = coeff P (monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> 1) k"
   550   proof (cases "k = Suc n")
   551     case True show ?thesis
   552     proof -
   553       fix m
   554       from True have less_add_diff:
   555         "!!i. [| n < i; i <= n + m |] ==> n + m - i < m" by arith
   556       from True have "coeff P (monom P \<one> (Suc n)) k = \<one>" by simp
   557       also from True
   558       have "... = (\<Oplus>i \<in> {..<n} \<union> {n}. coeff P (monom P \<one> n) i \<otimes>
   559         coeff P (monom P \<one> 1) (k - i))"
   560         by (simp cong: R.finsum_cong add: Pi_def)
   561       also have "... = (\<Oplus>i \<in>  {..n}. coeff P (monom P \<one> n) i \<otimes>
   562         coeff P (monom P \<one> 1) (k - i))"
   563         by (simp only: ivl_disj_un_singleton)
   564       also from True
   565       have "... = (\<Oplus>i \<in> {..n} \<union> {n<..k}. coeff P (monom P \<one> n) i \<otimes>
   566         coeff P (monom P \<one> 1) (k - i))"
   567         by (simp cong: R.finsum_cong add: R.finsum_Un_disjoint ivl_disj_int_one
   568           order_less_imp_not_eq Pi_def)
   569       also from True have "... = coeff P (monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> 1) k"
   570         by (simp add: ivl_disj_un_one)
   571       finally show ?thesis .
   572     qed
   573   next
   574     case False
   575     note neq = False
   576     let ?s =
   577       "\<lambda>i. (if n = i then \<one> else \<zero>) \<otimes> (if Suc 0 = k - i then \<one> else \<zero>)"
   578     from neq have "coeff P (monom P \<one> (Suc n)) k = \<zero>" by simp
   579     also have "... = (\<Oplus>i \<in> {..k}. ?s i)"
   580     proof -
   581       have f1: "(\<Oplus>i \<in> {..<n}. ?s i) = \<zero>"
   582         by (simp cong: R.finsum_cong add: Pi_def)
   583       from neq have f2: "(\<Oplus>i \<in> {n}. ?s i) = \<zero>"
   584         by (simp cong: R.finsum_cong add: Pi_def) arith
   585       have f3: "n < k ==> (\<Oplus>i \<in> {n<..k}. ?s i) = \<zero>"
   586         by (simp cong: R.finsum_cong add: order_less_imp_not_eq Pi_def)
   587       show ?thesis
   588       proof (cases "k < n")
   589         case True then show ?thesis by (simp cong: R.finsum_cong add: Pi_def)
   590       next
   591         case False then have n_le_k: "n <= k" by arith
   592         show ?thesis
   593         proof (cases "n = k")
   594           case True
   595           then have "\<zero> = (\<Oplus>i \<in> {..<n} \<union> {n}. ?s i)"
   596             by (simp cong: R.finsum_cong add: ivl_disj_int_singleton Pi_def)
   597           also from True have "... = (\<Oplus>i \<in> {..k}. ?s i)"
   598             by (simp only: ivl_disj_un_singleton)
   599           finally show ?thesis .
   600         next
   601           case False with n_le_k have n_less_k: "n < k" by arith
   602           with neq have "\<zero> = (\<Oplus>i \<in> {..<n} \<union> {n}. ?s i)"
   603             by (simp add: R.finsum_Un_disjoint f1 f2
   604               ivl_disj_int_singleton Pi_def del: Un_insert_right)
   605           also have "... = (\<Oplus>i \<in> {..n}. ?s i)"
   606             by (simp only: ivl_disj_un_singleton)
   607           also from n_less_k neq have "... = (\<Oplus>i \<in> {..n} \<union> {n<..k}. ?s i)"
   608             by (simp add: R.finsum_Un_disjoint f3 ivl_disj_int_one Pi_def)
   609           also from n_less_k have "... = (\<Oplus>i \<in> {..k}. ?s i)"
   610             by (simp only: ivl_disj_un_one)
   611           finally show ?thesis .
   612         qed
   613       qed
   614     qed
   615     also have "... = coeff P (monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> 1) k" by simp
   616     finally show ?thesis .
   617   qed
   618 qed (simp_all)
   619 
   620 lemma monom_one_Suc2:
   621   "monom P \<one> (Suc n) = monom P \<one> 1 \<otimes>\<^bsub>P\<^esub> monom P \<one> n"
   622 proof (induct n)
   623   case 0 show ?case by simp
   624 next
   625   case Suc
   626   {
   627     fix k:: nat
   628     assume hypo: "monom P \<one> (Suc k) = monom P \<one> 1 \<otimes>\<^bsub>P\<^esub> monom P \<one> k"
   629     then show "monom P \<one> (Suc (Suc k)) = monom P \<one> 1 \<otimes>\<^bsub>P\<^esub> monom P \<one> (Suc k)"
   630     proof -
   631       have lhs: "monom P \<one> (Suc (Suc k)) = monom P \<one> 1 \<otimes>\<^bsub>P\<^esub> monom P \<one> k \<otimes>\<^bsub>P\<^esub> monom P \<one> 1"
   632 	unfolding monom_one_Suc [of "Suc k"] unfolding hypo ..
   633       note cl = monom_closed [OF R.one_closed, of 1]
   634       note clk = monom_closed [OF R.one_closed, of k]
   635       have rhs: "monom P \<one> 1 \<otimes>\<^bsub>P\<^esub> monom P \<one> (Suc k) = monom P \<one> 1 \<otimes>\<^bsub>P\<^esub> monom P \<one> k \<otimes>\<^bsub>P\<^esub> monom P \<one> 1"
   636 	unfolding monom_one_Suc [of k] unfolding sym [OF m_assoc  [OF cl clk cl]] ..
   637       from lhs rhs show ?thesis by simp
   638     qed
   639   }
   640 qed
   641 
   642 text{*The following corollary follows from lemmas @{thm [locale=UP_ring] "monom_one_Suc"} 
   643   and @{thm [locale=UP_ring] "monom_one_Suc2"}, and is trivial in @{term UP_cring}*}
   644 
   645 corollary monom_one_comm: shows "monom P \<one> k \<otimes>\<^bsub>P\<^esub> monom P \<one> 1 = monom P \<one> 1 \<otimes>\<^bsub>P\<^esub> monom P \<one> k"
   646   unfolding monom_one_Suc [symmetric] monom_one_Suc2 [symmetric] ..
   647 
   648 lemma monom_mult_smult:
   649   "[| a \<in> carrier R; b \<in> carrier R |] ==> monom P (a \<otimes> b) n = a \<odot>\<^bsub>P\<^esub> monom P b n"
   650   by (rule up_eqI) simp_all
   651 
   652 lemma monom_one_mult:
   653   "monom P \<one> (n + m) = monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> m"
   654 proof (induct n)
   655   case 0 show ?case by simp
   656 next
   657   case Suc then show ?case
   658     unfolding add_Suc unfolding monom_one_Suc unfolding Suc.hyps
   659     using m_assoc monom_one_comm [of m] by simp
   660 qed
   661 
   662 lemma monom_one_mult_comm: "monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> m = monom P \<one> m \<otimes>\<^bsub>P\<^esub> monom P \<one> n"
   663   unfolding monom_one_mult [symmetric] by (rule up_eqI) simp_all
   664 
   665 end
   666 
   667 context UP_cring
   668 begin
   669 
   670 (* Could got to UP_ring?  *)
   671 
   672 lemma monom_mult [simp]:
   673   assumes R: "a \<in> carrier R" "b \<in> carrier R"
   674   shows "monom P (a \<otimes> b) (n + m) = monom P a n \<otimes>\<^bsub>P\<^esub> monom P b m"
   675 proof -
   676   from R have "monom P (a \<otimes> b) (n + m) = monom P (a \<otimes> b \<otimes> \<one>) (n + m)" by simp
   677   also from R have "... = a \<otimes> b \<odot>\<^bsub>P\<^esub> monom P \<one> (n + m)"
   678     by (simp add: monom_mult_smult del: R.r_one)
   679   also have "... = a \<otimes> b \<odot>\<^bsub>P\<^esub> (monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> m)"
   680     by (simp only: monom_one_mult)
   681   also from R have "... = a \<odot>\<^bsub>P\<^esub> (b \<odot>\<^bsub>P\<^esub> (monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> m))"
   682     by (simp add: UP_smult_assoc1)
   683   also from R have "... = a \<odot>\<^bsub>P\<^esub> (b \<odot>\<^bsub>P\<^esub> (monom P \<one> m \<otimes>\<^bsub>P\<^esub> monom P \<one> n))"
   684     unfolding monom_one_mult_comm by simp
   685   also from R have "... = a \<odot>\<^bsub>P\<^esub> ((b \<odot>\<^bsub>P\<^esub> monom P \<one> m) \<otimes>\<^bsub>P\<^esub> monom P \<one> n)"
   686     by (simp add: UP_smult_assoc2)
   687   also from R have "... = a \<odot>\<^bsub>P\<^esub> (monom P \<one> n \<otimes>\<^bsub>P\<^esub> (b \<odot>\<^bsub>P\<^esub> monom P \<one> m))"
   688     using monom_one_mult_comm [of n m] by (simp add: P.m_comm)
   689   also from R have "... = (a \<odot>\<^bsub>P\<^esub> monom P \<one> n) \<otimes>\<^bsub>P\<^esub> (b \<odot>\<^bsub>P\<^esub> monom P \<one> m)"
   690     by (simp add: UP_smult_assoc2)
   691   also from R have "... = monom P (a \<otimes> \<one>) n \<otimes>\<^bsub>P\<^esub> monom P (b \<otimes> \<one>) m"
   692     by (simp add: monom_mult_smult del: R.r_one)
   693   also from R have "... = monom P a n \<otimes>\<^bsub>P\<^esub> monom P b m" by simp
   694   finally show ?thesis .
   695 qed
   696 
   697 end
   698 
   699 context UP_ring
   700 begin
   701 
   702 lemma monom_a_inv [simp]:
   703   "a \<in> carrier R ==> monom P (\<ominus> a) n = \<ominus>\<^bsub>P\<^esub> monom P a n"
   704   by (rule up_eqI) simp_all
   705 
   706 lemma monom_inj:
   707   "inj_on (%a. monom P a n) (carrier R)"
   708 proof (rule inj_onI)
   709   fix x y
   710   assume R: "x \<in> carrier R" "y \<in> carrier R" and eq: "monom P x n = monom P y n"
   711   then have "coeff P (monom P x n) n = coeff P (monom P y n) n" by simp
   712   with R show "x = y" by simp
   713 qed
   714 
   715 end
   716 
   717 
   718 subsection {* The Degree Function *}
   719 
   720 constdefs (structure R)
   721   deg :: "[('a, 'm) ring_scheme, nat => 'a] => nat"
   722   "deg R p == LEAST n. bound \<zero> n (coeff (UP R) p)"
   723 
   724 context UP_ring
   725 begin
   726 
   727 lemma deg_aboveI:
   728   "[| (!!m. n < m ==> coeff P p m = \<zero>); p \<in> carrier P |] ==> deg R p <= n"
   729   by (unfold deg_def P_def) (fast intro: Least_le)
   730 
   731 (*
   732 lemma coeff_bound_ex: "EX n. bound n (coeff p)"
   733 proof -
   734   have "(%n. coeff p n) : UP" by (simp add: coeff_def Rep_UP)
   735   then obtain n where "bound n (coeff p)" by (unfold UP_def) fast
   736   then show ?thesis ..
   737 qed
   738 
   739 lemma bound_coeff_obtain:
   740   assumes prem: "(!!n. bound n (coeff p) ==> P)" shows "P"
   741 proof -
   742   have "(%n. coeff p n) : UP" by (simp add: coeff_def Rep_UP)
   743   then obtain n where "bound n (coeff p)" by (unfold UP_def) fast
   744   with prem show P .
   745 qed
   746 *)
   747 
   748 lemma deg_aboveD:
   749   assumes "deg R p < m" and "p \<in> carrier P"
   750   shows "coeff P p m = \<zero>"
   751 proof -
   752   from `p \<in> carrier P` obtain n where "bound \<zero> n (coeff P p)"
   753     by (auto simp add: UP_def P_def)
   754   then have "bound \<zero> (deg R p) (coeff P p)"
   755     by (auto simp: deg_def P_def dest: LeastI)
   756   from this and `deg R p < m` show ?thesis ..
   757 qed
   758 
   759 lemma deg_belowI:
   760   assumes non_zero: "n ~= 0 ==> coeff P p n ~= \<zero>"
   761     and R: "p \<in> carrier P"
   762   shows "n <= deg R p"
   763 -- {* Logically, this is a slightly stronger version of
   764    @{thm [source] deg_aboveD} *}
   765 proof (cases "n=0")
   766   case True then show ?thesis by simp
   767 next
   768   case False then have "coeff P p n ~= \<zero>" by (rule non_zero)
   769   then have "~ deg R p < n" by (fast dest: deg_aboveD intro: R)
   770   then show ?thesis by arith
   771 qed
   772 
   773 lemma lcoeff_nonzero_deg:
   774   assumes deg: "deg R p ~= 0" and R: "p \<in> carrier P"
   775   shows "coeff P p (deg R p) ~= \<zero>"
   776 proof -
   777   from R obtain m where "deg R p <= m" and m_coeff: "coeff P p m ~= \<zero>"
   778   proof -
   779     have minus: "!!(n::nat) m. n ~= 0 ==> (n - Suc 0 < m) = (n <= m)"
   780       by arith
   781     from deg have "deg R p - 1 < (LEAST n. bound \<zero> n (coeff P p))"
   782       by (unfold deg_def P_def) simp
   783     then have "~ bound \<zero> (deg R p - 1) (coeff P p)" by (rule not_less_Least)
   784     then have "EX m. deg R p - 1 < m & coeff P p m ~= \<zero>"
   785       by (unfold bound_def) fast
   786     then have "EX m. deg R p <= m & coeff P p m ~= \<zero>" by (simp add: deg minus)
   787     then show ?thesis by (auto intro: that)
   788   qed
   789   with deg_belowI R have "deg R p = m" by fastsimp
   790   with m_coeff show ?thesis by simp
   791 qed
   792 
   793 lemma lcoeff_nonzero_nonzero:
   794   assumes deg: "deg R p = 0" and nonzero: "p ~= \<zero>\<^bsub>P\<^esub>" and R: "p \<in> carrier P"
   795   shows "coeff P p 0 ~= \<zero>"
   796 proof -
   797   have "EX m. coeff P p m ~= \<zero>"
   798   proof (rule classical)
   799     assume "~ ?thesis"
   800     with R have "p = \<zero>\<^bsub>P\<^esub>" by (auto intro: up_eqI)
   801     with nonzero show ?thesis by contradiction
   802   qed
   803   then obtain m where coeff: "coeff P p m ~= \<zero>" ..
   804   from this and R have "m <= deg R p" by (rule deg_belowI)
   805   then have "m = 0" by (simp add: deg)
   806   with coeff show ?thesis by simp
   807 qed
   808 
   809 lemma lcoeff_nonzero:
   810   assumes neq: "p ~= \<zero>\<^bsub>P\<^esub>" and R: "p \<in> carrier P"
   811   shows "coeff P p (deg R p) ~= \<zero>"
   812 proof (cases "deg R p = 0")
   813   case True with neq R show ?thesis by (simp add: lcoeff_nonzero_nonzero)
   814 next
   815   case False with neq R show ?thesis by (simp add: lcoeff_nonzero_deg)
   816 qed
   817 
   818 lemma deg_eqI:
   819   "[| !!m. n < m ==> coeff P p m = \<zero>;
   820       !!n. n ~= 0 ==> coeff P p n ~= \<zero>; p \<in> carrier P |] ==> deg R p = n"
   821 by (fast intro: le_anti_sym deg_aboveI deg_belowI)
   822 
   823 text {* Degree and polynomial operations *}
   824 
   825 lemma deg_add [simp]:
   826   assumes R: "p \<in> carrier P" "q \<in> carrier P"
   827   shows "deg R (p \<oplus>\<^bsub>P\<^esub> q) <= max (deg R p) (deg R q)"
   828 proof (cases "deg R p <= deg R q")
   829   case True show ?thesis
   830     by (rule deg_aboveI) (simp_all add: True R deg_aboveD)
   831 next
   832   case False show ?thesis
   833     by (rule deg_aboveI) (simp_all add: False R deg_aboveD)
   834 qed
   835 
   836 lemma deg_monom_le:
   837   "a \<in> carrier R ==> deg R (monom P a n) <= n"
   838   by (intro deg_aboveI) simp_all
   839 
   840 lemma deg_monom [simp]:
   841   "[| a ~= \<zero>; a \<in> carrier R |] ==> deg R (monom P a n) = n"
   842   by (fastsimp intro: le_anti_sym deg_aboveI deg_belowI)
   843 
   844 lemma deg_const [simp]:
   845   assumes R: "a \<in> carrier R" shows "deg R (monom P a 0) = 0"
   846 proof (rule le_anti_sym)
   847   show "deg R (monom P a 0) <= 0" by (rule deg_aboveI) (simp_all add: R)
   848 next
   849   show "0 <= deg R (monom P a 0)" by (rule deg_belowI) (simp_all add: R)
   850 qed
   851 
   852 lemma deg_zero [simp]:
   853   "deg R \<zero>\<^bsub>P\<^esub> = 0"
   854 proof (rule le_anti_sym)
   855   show "deg R \<zero>\<^bsub>P\<^esub> <= 0" by (rule deg_aboveI) simp_all
   856 next
   857   show "0 <= deg R \<zero>\<^bsub>P\<^esub>" by (rule deg_belowI) simp_all
   858 qed
   859 
   860 lemma deg_one [simp]:
   861   "deg R \<one>\<^bsub>P\<^esub> = 0"
   862 proof (rule le_anti_sym)
   863   show "deg R \<one>\<^bsub>P\<^esub> <= 0" by (rule deg_aboveI) simp_all
   864 next
   865   show "0 <= deg R \<one>\<^bsub>P\<^esub>" by (rule deg_belowI) simp_all
   866 qed
   867 
   868 lemma deg_uminus [simp]:
   869   assumes R: "p \<in> carrier P" shows "deg R (\<ominus>\<^bsub>P\<^esub> p) = deg R p"
   870 proof (rule le_anti_sym)
   871   show "deg R (\<ominus>\<^bsub>P\<^esub> p) <= deg R p" by (simp add: deg_aboveI deg_aboveD R)
   872 next
   873   show "deg R p <= deg R (\<ominus>\<^bsub>P\<^esub> p)"
   874     by (simp add: deg_belowI lcoeff_nonzero_deg
   875       inj_on_iff [OF R.a_inv_inj, of _ "\<zero>", simplified] R)
   876 qed
   877 
   878 text{*The following lemma is later \emph{overwritten} by the most
   879   specific one for domains, @{text deg_smult}.*}
   880 
   881 lemma deg_smult_ring [simp]:
   882   "[| a \<in> carrier R; p \<in> carrier P |] ==>
   883   deg R (a \<odot>\<^bsub>P\<^esub> p) <= (if a = \<zero> then 0 else deg R p)"
   884   by (cases "a = \<zero>") (simp add: deg_aboveI deg_aboveD)+
   885 
   886 end
   887 
   888 context UP_domain
   889 begin
   890 
   891 lemma deg_smult [simp]:
   892   assumes R: "a \<in> carrier R" "p \<in> carrier P"
   893   shows "deg R (a \<odot>\<^bsub>P\<^esub> p) = (if a = \<zero> then 0 else deg R p)"
   894 proof (rule le_anti_sym)
   895   show "deg R (a \<odot>\<^bsub>P\<^esub> p) <= (if a = \<zero> then 0 else deg R p)"
   896     using R by (rule deg_smult_ring)
   897 next
   898   show "(if a = \<zero> then 0 else deg R p) <= deg R (a \<odot>\<^bsub>P\<^esub> p)"
   899   proof (cases "a = \<zero>")
   900   qed (simp, simp add: deg_belowI lcoeff_nonzero_deg integral_iff R)
   901 qed
   902 
   903 end
   904 
   905 context UP_ring
   906 begin
   907 
   908 lemma deg_mult_ring:
   909   assumes R: "p \<in> carrier P" "q \<in> carrier P"
   910   shows "deg R (p \<otimes>\<^bsub>P\<^esub> q) <= deg R p + deg R q"
   911 proof (rule deg_aboveI)
   912   fix m
   913   assume boundm: "deg R p + deg R q < m"
   914   {
   915     fix k i
   916     assume boundk: "deg R p + deg R q < k"
   917     then have "coeff P p i \<otimes> coeff P q (k - i) = \<zero>"
   918     proof (cases "deg R p < i")
   919       case True then show ?thesis by (simp add: deg_aboveD R)
   920     next
   921       case False with boundk have "deg R q < k - i" by arith
   922       then show ?thesis by (simp add: deg_aboveD R)
   923     qed
   924   }
   925   with boundm R show "coeff P (p \<otimes>\<^bsub>P\<^esub> q) m = \<zero>" by simp
   926 qed (simp add: R)
   927 
   928 end
   929 
   930 context UP_domain
   931 begin
   932 
   933 lemma deg_mult [simp]:
   934   "[| p ~= \<zero>\<^bsub>P\<^esub>; q ~= \<zero>\<^bsub>P\<^esub>; p \<in> carrier P; q \<in> carrier P |] ==>
   935   deg R (p \<otimes>\<^bsub>P\<^esub> q) = deg R p + deg R q"
   936 proof (rule le_anti_sym)
   937   assume "p \<in> carrier P" " q \<in> carrier P"
   938   then show "deg R (p \<otimes>\<^bsub>P\<^esub> q) <= deg R p + deg R q" by (rule deg_mult_ring)
   939 next
   940   let ?s = "(%i. coeff P p i \<otimes> coeff P q (deg R p + deg R q - i))"
   941   assume R: "p \<in> carrier P" "q \<in> carrier P" and nz: "p ~= \<zero>\<^bsub>P\<^esub>" "q ~= \<zero>\<^bsub>P\<^esub>"
   942   have less_add_diff: "!!(k::nat) n m. k < n ==> m < n + m - k" by arith
   943   show "deg R p + deg R q <= deg R (p \<otimes>\<^bsub>P\<^esub> q)"
   944   proof (rule deg_belowI, simp add: R)
   945     have "(\<Oplus>i \<in> {.. deg R p + deg R q}. ?s i)
   946       = (\<Oplus>i \<in> {..< deg R p} \<union> {deg R p .. deg R p + deg R q}. ?s i)"
   947       by (simp only: ivl_disj_un_one)
   948     also have "... = (\<Oplus>i \<in> {deg R p .. deg R p + deg R q}. ?s i)"
   949       by (simp cong: R.finsum_cong add: R.finsum_Un_disjoint ivl_disj_int_one
   950         deg_aboveD less_add_diff R Pi_def)
   951     also have "...= (\<Oplus>i \<in> {deg R p} \<union> {deg R p <.. deg R p + deg R q}. ?s i)"
   952       by (simp only: ivl_disj_un_singleton)
   953     also have "... = coeff P p (deg R p) \<otimes> coeff P q (deg R q)"
   954       by (simp cong: R.finsum_cong
   955 	add: ivl_disj_int_singleton deg_aboveD R Pi_def)
   956     finally have "(\<Oplus>i \<in> {.. deg R p + deg R q}. ?s i)
   957       = coeff P p (deg R p) \<otimes> coeff P q (deg R q)" .
   958     with nz show "(\<Oplus>i \<in> {.. deg R p + deg R q}. ?s i) ~= \<zero>"
   959       by (simp add: integral_iff lcoeff_nonzero R)
   960   qed (simp add: R)
   961 qed
   962 
   963 end
   964 
   965 text{*The following lemmas also can be lifted to @{term UP_ring}.*}
   966 
   967 context UP_ring
   968 begin
   969 
   970 lemma coeff_finsum:
   971   assumes fin: "finite A"
   972   shows "p \<in> A -> carrier P ==>
   973     coeff P (finsum P p A) k = (\<Oplus>i \<in> A. coeff P (p i) k)"
   974   using fin by induct (auto simp: Pi_def)
   975 
   976 lemma up_repr:
   977   assumes R: "p \<in> carrier P"
   978   shows "(\<Oplus>\<^bsub>P\<^esub> i \<in> {..deg R p}. monom P (coeff P p i) i) = p"
   979 proof (rule up_eqI)
   980   let ?s = "(%i. monom P (coeff P p i) i)"
   981   fix k
   982   from R have RR: "!!i. (if i = k then coeff P p i else \<zero>) \<in> carrier R"
   983     by simp
   984   show "coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..deg R p}. ?s i) k = coeff P p k"
   985   proof (cases "k <= deg R p")
   986     case True
   987     hence "coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..deg R p}. ?s i) k =
   988           coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..k} \<union> {k<..deg R p}. ?s i) k"
   989       by (simp only: ivl_disj_un_one)
   990     also from True
   991     have "... = coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..k}. ?s i) k"
   992       by (simp cong: R.finsum_cong add: R.finsum_Un_disjoint
   993         ivl_disj_int_one order_less_imp_not_eq2 coeff_finsum R RR Pi_def)
   994     also
   995     have "... = coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..<k} \<union> {k}. ?s i) k"
   996       by (simp only: ivl_disj_un_singleton)
   997     also have "... = coeff P p k"
   998       by (simp cong: R.finsum_cong
   999 	add: ivl_disj_int_singleton coeff_finsum deg_aboveD R RR Pi_def)
  1000     finally show ?thesis .
  1001   next
  1002     case False
  1003     hence "coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..deg R p}. ?s i) k =
  1004           coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..<deg R p} \<union> {deg R p}. ?s i) k"
  1005       by (simp only: ivl_disj_un_singleton)
  1006     also from False have "... = coeff P p k"
  1007       by (simp cong: R.finsum_cong
  1008 	add: ivl_disj_int_singleton coeff_finsum deg_aboveD R Pi_def)
  1009     finally show ?thesis .
  1010   qed
  1011 qed (simp_all add: R Pi_def)
  1012 
  1013 lemma up_repr_le:
  1014   "[| deg R p <= n; p \<in> carrier P |] ==>
  1015   (\<Oplus>\<^bsub>P\<^esub> i \<in> {..n}. monom P (coeff P p i) i) = p"
  1016 proof -
  1017   let ?s = "(%i. monom P (coeff P p i) i)"
  1018   assume R: "p \<in> carrier P" and "deg R p <= n"
  1019   then have "finsum P ?s {..n} = finsum P ?s ({..deg R p} \<union> {deg R p<..n})"
  1020     by (simp only: ivl_disj_un_one)
  1021   also have "... = finsum P ?s {..deg R p}"
  1022     by (simp cong: P.finsum_cong add: P.finsum_Un_disjoint ivl_disj_int_one
  1023       deg_aboveD R Pi_def)
  1024   also have "... = p" using R by (rule up_repr)
  1025   finally show ?thesis .
  1026 qed
  1027 
  1028 end
  1029 
  1030 
  1031 subsection {* Polynomials over Integral Domains *}
  1032 
  1033 lemma domainI:
  1034   assumes cring: "cring R"
  1035     and one_not_zero: "one R ~= zero R"
  1036     and integral: "!!a b. [| mult R a b = zero R; a \<in> carrier R;
  1037       b \<in> carrier R |] ==> a = zero R | b = zero R"
  1038   shows "domain R"
  1039   by (auto intro!: domain.intro domain_axioms.intro cring.axioms assms
  1040     del: disjCI)
  1041 
  1042 context UP_domain
  1043 begin
  1044 
  1045 lemma UP_one_not_zero:
  1046   "\<one>\<^bsub>P\<^esub> ~= \<zero>\<^bsub>P\<^esub>"
  1047 proof
  1048   assume "\<one>\<^bsub>P\<^esub> = \<zero>\<^bsub>P\<^esub>"
  1049   hence "coeff P \<one>\<^bsub>P\<^esub> 0 = (coeff P \<zero>\<^bsub>P\<^esub> 0)" by simp
  1050   hence "\<one> = \<zero>" by simp
  1051   with R.one_not_zero show "False" by contradiction
  1052 qed
  1053 
  1054 lemma UP_integral:
  1055   "[| p \<otimes>\<^bsub>P\<^esub> q = \<zero>\<^bsub>P\<^esub>; p \<in> carrier P; q \<in> carrier P |] ==> p = \<zero>\<^bsub>P\<^esub> | q = \<zero>\<^bsub>P\<^esub>"
  1056 proof -
  1057   fix p q
  1058   assume pq: "p \<otimes>\<^bsub>P\<^esub> q = \<zero>\<^bsub>P\<^esub>" and R: "p \<in> carrier P" "q \<in> carrier P"
  1059   show "p = \<zero>\<^bsub>P\<^esub> | q = \<zero>\<^bsub>P\<^esub>"
  1060   proof (rule classical)
  1061     assume c: "~ (p = \<zero>\<^bsub>P\<^esub> | q = \<zero>\<^bsub>P\<^esub>)"
  1062     with R have "deg R p + deg R q = deg R (p \<otimes>\<^bsub>P\<^esub> q)" by simp
  1063     also from pq have "... = 0" by simp
  1064     finally have "deg R p + deg R q = 0" .
  1065     then have f1: "deg R p = 0 & deg R q = 0" by simp
  1066     from f1 R have "p = (\<Oplus>\<^bsub>P\<^esub> i \<in> {..0}. monom P (coeff P p i) i)"
  1067       by (simp only: up_repr_le)
  1068     also from R have "... = monom P (coeff P p 0) 0" by simp
  1069     finally have p: "p = monom P (coeff P p 0) 0" .
  1070     from f1 R have "q = (\<Oplus>\<^bsub>P\<^esub> i \<in> {..0}. monom P (coeff P q i) i)"
  1071       by (simp only: up_repr_le)
  1072     also from R have "... = monom P (coeff P q 0) 0" by simp
  1073     finally have q: "q = monom P (coeff P q 0) 0" .
  1074     from R have "coeff P p 0 \<otimes> coeff P q 0 = coeff P (p \<otimes>\<^bsub>P\<^esub> q) 0" by simp
  1075     also from pq have "... = \<zero>" by simp
  1076     finally have "coeff P p 0 \<otimes> coeff P q 0 = \<zero>" .
  1077     with R have "coeff P p 0 = \<zero> | coeff P q 0 = \<zero>"
  1078       by (simp add: R.integral_iff)
  1079     with p q show "p = \<zero>\<^bsub>P\<^esub> | q = \<zero>\<^bsub>P\<^esub>" by fastsimp
  1080   qed
  1081 qed
  1082 
  1083 theorem UP_domain:
  1084   "domain P"
  1085   by (auto intro!: domainI UP_cring UP_one_not_zero UP_integral del: disjCI)
  1086 
  1087 end
  1088 
  1089 text {*
  1090   Interpretation of theorems from @{term domain}.
  1091 *}
  1092 
  1093 interpretation UP_domain < "domain" P
  1094   by intro_locales (rule domain.axioms UP_domain)+
  1095 
  1096 
  1097 subsection {* The Evaluation Homomorphism and Universal Property*}
  1098 
  1099 (* alternative congruence rule (possibly more efficient)
  1100 lemma (in abelian_monoid) finsum_cong2:
  1101   "[| !!i. i \<in> A ==> f i \<in> carrier G = True; A = B;
  1102   !!i. i \<in> B ==> f i = g i |] ==> finsum G f A = finsum G g B"
  1103   sorry*)
  1104 
  1105 lemma (in abelian_monoid) boundD_carrier:
  1106   "[| bound \<zero> n f; n < m |] ==> f m \<in> carrier G"
  1107   by auto
  1108 
  1109 context ring
  1110 begin
  1111 
  1112 theorem diagonal_sum:
  1113   "[| f \<in> {..n + m::nat} -> carrier R; g \<in> {..n + m} -> carrier R |] ==>
  1114   (\<Oplus>k \<in> {..n + m}. \<Oplus>i \<in> {..k}. f i \<otimes> g (k - i)) =
  1115   (\<Oplus>k \<in> {..n + m}. \<Oplus>i \<in> {..n + m - k}. f k \<otimes> g i)"
  1116 proof -
  1117   assume Rf: "f \<in> {..n + m} -> carrier R" and Rg: "g \<in> {..n + m} -> carrier R"
  1118   {
  1119     fix j
  1120     have "j <= n + m ==>
  1121       (\<Oplus>k \<in> {..j}. \<Oplus>i \<in> {..k}. f i \<otimes> g (k - i)) =
  1122       (\<Oplus>k \<in> {..j}. \<Oplus>i \<in> {..j - k}. f k \<otimes> g i)"
  1123     proof (induct j)
  1124       case 0 from Rf Rg show ?case by (simp add: Pi_def)
  1125     next
  1126       case (Suc j)
  1127       have R6: "!!i k. [| k <= j; i <= Suc j - k |] ==> g i \<in> carrier R"
  1128         using Suc by (auto intro!: funcset_mem [OF Rg])
  1129       have R8: "!!i k. [| k <= Suc j; i <= k |] ==> g (k - i) \<in> carrier R"
  1130         using Suc by (auto intro!: funcset_mem [OF Rg])
  1131       have R9: "!!i k. [| k <= Suc j |] ==> f k \<in> carrier R"
  1132         using Suc by (auto intro!: funcset_mem [OF Rf])
  1133       have R10: "!!i k. [| k <= Suc j; i <= Suc j - k |] ==> g i \<in> carrier R"
  1134         using Suc by (auto intro!: funcset_mem [OF Rg])
  1135       have R11: "g 0 \<in> carrier R"
  1136         using Suc by (auto intro!: funcset_mem [OF Rg])
  1137       from Suc show ?case
  1138         by (simp cong: finsum_cong add: Suc_diff_le a_ac
  1139           Pi_def R6 R8 R9 R10 R11)
  1140     qed
  1141   }
  1142   then show ?thesis by fast
  1143 qed
  1144 
  1145 theorem cauchy_product:
  1146   assumes bf: "bound \<zero> n f" and bg: "bound \<zero> m g"
  1147     and Rf: "f \<in> {..n} -> carrier R" and Rg: "g \<in> {..m} -> carrier R"
  1148   shows "(\<Oplus>k \<in> {..n + m}. \<Oplus>i \<in> {..k}. f i \<otimes> g (k - i)) =
  1149     (\<Oplus>i \<in> {..n}. f i) \<otimes> (\<Oplus>i \<in> {..m}. g i)"      (* State reverse direction? *)
  1150 proof -
  1151   have f: "!!x. f x \<in> carrier R"
  1152   proof -
  1153     fix x
  1154     show "f x \<in> carrier R"
  1155       using Rf bf boundD_carrier by (cases "x <= n") (auto simp: Pi_def)
  1156   qed
  1157   have g: "!!x. g x \<in> carrier R"
  1158   proof -
  1159     fix x
  1160     show "g x \<in> carrier R"
  1161       using Rg bg boundD_carrier by (cases "x <= m") (auto simp: Pi_def)
  1162   qed
  1163   from f g have "(\<Oplus>k \<in> {..n + m}. \<Oplus>i \<in> {..k}. f i \<otimes> g (k - i)) =
  1164       (\<Oplus>k \<in> {..n + m}. \<Oplus>i \<in> {..n + m - k}. f k \<otimes> g i)"
  1165     by (simp add: diagonal_sum Pi_def)
  1166   also have "... = (\<Oplus>k \<in> {..n} \<union> {n<..n + m}. \<Oplus>i \<in> {..n + m - k}. f k \<otimes> g i)"
  1167     by (simp only: ivl_disj_un_one)
  1168   also from f g have "... = (\<Oplus>k \<in> {..n}. \<Oplus>i \<in> {..n + m - k}. f k \<otimes> g i)"
  1169     by (simp cong: finsum_cong
  1170       add: bound.bound [OF bf] finsum_Un_disjoint ivl_disj_int_one Pi_def)
  1171   also from f g
  1172   have "... = (\<Oplus>k \<in> {..n}. \<Oplus>i \<in> {..m} \<union> {m<..n + m - k}. f k \<otimes> g i)"
  1173     by (simp cong: finsum_cong add: ivl_disj_un_one le_add_diff Pi_def)
  1174   also from f g have "... = (\<Oplus>k \<in> {..n}. \<Oplus>i \<in> {..m}. f k \<otimes> g i)"
  1175     by (simp cong: finsum_cong
  1176       add: bound.bound [OF bg] finsum_Un_disjoint ivl_disj_int_one Pi_def)
  1177   also from f g have "... = (\<Oplus>i \<in> {..n}. f i) \<otimes> (\<Oplus>i \<in> {..m}. g i)"
  1178     by (simp add: finsum_ldistr diagonal_sum Pi_def,
  1179       simp cong: finsum_cong add: finsum_rdistr Pi_def)
  1180   finally show ?thesis .
  1181 qed
  1182 
  1183 end
  1184 
  1185 lemma (in UP_ring) const_ring_hom:
  1186   "(%a. monom P a 0) \<in> ring_hom R P"
  1187   by (auto intro!: ring_hom_memI intro: up_eqI simp: monom_mult_is_smult)
  1188 
  1189 constdefs (structure S)
  1190   eval :: "[('a, 'm) ring_scheme, ('b, 'n) ring_scheme,
  1191            'a => 'b, 'b, nat => 'a] => 'b"
  1192   "eval R S phi s == \<lambda>p \<in> carrier (UP R).
  1193     \<Oplus>i \<in> {..deg R p}. phi (coeff (UP R) p i) \<otimes> s (^) i"
  1194 
  1195 context UP
  1196 begin
  1197 
  1198 lemma eval_on_carrier:
  1199   fixes S (structure)
  1200   shows "p \<in> carrier P ==>
  1201   eval R S phi s p = (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R p}. phi (coeff P p i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1202   by (unfold eval_def, fold P_def) simp
  1203 
  1204 lemma eval_extensional:
  1205   "eval R S phi p \<in> extensional (carrier P)"
  1206   by (unfold eval_def, fold P_def) simp
  1207 
  1208 end
  1209 
  1210 text {* The universal property of the polynomial ring *}
  1211 
  1212 locale UP_pre_univ_prop = ring_hom_cring R S h + UP_cring R P
  1213 
  1214 locale UP_univ_prop = UP_pre_univ_prop +
  1215   fixes s and Eval
  1216   assumes indet_img_carrier [simp, intro]: "s \<in> carrier S"
  1217   defines Eval_def: "Eval == eval R S h s"
  1218 
  1219 text{*JE: I have moved the following lemma from Ring.thy and lifted then to the locale @{term ring_hom_ring} from @{term ring_hom_cring}.*}
  1220 text{*JE: I was considering using it in @{text eval_ring_hom}, but that property does not hold for non commutative rings, so 
  1221   maybe it is not that necessary.*}
  1222 
  1223 lemma (in ring_hom_ring) hom_finsum [simp]:
  1224   "[| finite A; f \<in> A -> carrier R |] ==>
  1225   h (finsum R f A) = finsum S (h o f) A"
  1226 proof (induct set: finite)
  1227   case empty then show ?case by simp
  1228 next
  1229   case insert then show ?case by (simp add: Pi_def)
  1230 qed
  1231 
  1232 context UP_pre_univ_prop
  1233 begin
  1234 
  1235 theorem eval_ring_hom:
  1236   assumes S: "s \<in> carrier S"
  1237   shows "eval R S h s \<in> ring_hom P S"
  1238 proof (rule ring_hom_memI)
  1239   fix p
  1240   assume R: "p \<in> carrier P"
  1241   then show "eval R S h s p \<in> carrier S"
  1242     by (simp only: eval_on_carrier) (simp add: S Pi_def)
  1243 next
  1244   fix p q
  1245   assume R: "p \<in> carrier P" "q \<in> carrier P"
  1246   then show "eval R S h s (p \<oplus>\<^bsub>P\<^esub> q) = eval R S h s p \<oplus>\<^bsub>S\<^esub> eval R S h s q"
  1247   proof (simp only: eval_on_carrier P.a_closed)
  1248     from S R have
  1249       "(\<Oplus>\<^bsub>S \<^esub>i\<in>{..deg R (p \<oplus>\<^bsub>P\<^esub> q)}. h (coeff P (p \<oplus>\<^bsub>P\<^esub> q) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) =
  1250       (\<Oplus>\<^bsub>S \<^esub>i\<in>{..deg R (p \<oplus>\<^bsub>P\<^esub> q)} \<union> {deg R (p \<oplus>\<^bsub>P\<^esub> q)<..max (deg R p) (deg R q)}.
  1251         h (coeff P (p \<oplus>\<^bsub>P\<^esub> q) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1252       by (simp cong: S.finsum_cong
  1253         add: deg_aboveD S.finsum_Un_disjoint ivl_disj_int_one Pi_def del: coeff_add)
  1254     also from R have "... =
  1255         (\<Oplus>\<^bsub>S\<^esub> i \<in> {..max (deg R p) (deg R q)}.
  1256           h (coeff P (p \<oplus>\<^bsub>P\<^esub> q) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1257       by (simp add: ivl_disj_un_one)
  1258     also from R S have "... =
  1259       (\<Oplus>\<^bsub>S\<^esub>i\<in>{..max (deg R p) (deg R q)}. h (coeff P p i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) \<oplus>\<^bsub>S\<^esub>
  1260       (\<Oplus>\<^bsub>S\<^esub>i\<in>{..max (deg R p) (deg R q)}. h (coeff P q i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1261       by (simp cong: S.finsum_cong
  1262         add: S.l_distr deg_aboveD ivl_disj_int_one Pi_def)
  1263     also have "... =
  1264         (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R p} \<union> {deg R p<..max (deg R p) (deg R q)}.
  1265           h (coeff P p i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) \<oplus>\<^bsub>S\<^esub>
  1266         (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R q} \<union> {deg R q<..max (deg R p) (deg R q)}.
  1267           h (coeff P q i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1268       by (simp only: ivl_disj_un_one le_maxI1 le_maxI2)
  1269     also from R S have "... =
  1270       (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R p}. h (coeff P p i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) \<oplus>\<^bsub>S\<^esub>
  1271       (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R q}. h (coeff P q i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1272       by (simp cong: S.finsum_cong
  1273         add: deg_aboveD S.finsum_Un_disjoint ivl_disj_int_one Pi_def)
  1274     finally show
  1275       "(\<Oplus>\<^bsub>S\<^esub>i \<in> {..deg R (p \<oplus>\<^bsub>P\<^esub> q)}. h (coeff P (p \<oplus>\<^bsub>P\<^esub> q) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) =
  1276       (\<Oplus>\<^bsub>S\<^esub>i \<in> {..deg R p}. h (coeff P p i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) \<oplus>\<^bsub>S\<^esub>
  1277       (\<Oplus>\<^bsub>S\<^esub>i \<in> {..deg R q}. h (coeff P q i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)" .
  1278   qed
  1279 next
  1280   show "eval R S h s \<one>\<^bsub>P\<^esub> = \<one>\<^bsub>S\<^esub>"
  1281     by (simp only: eval_on_carrier UP_one_closed) simp
  1282 next
  1283   fix p q
  1284   assume R: "p \<in> carrier P" "q \<in> carrier P"
  1285   then show "eval R S h s (p \<otimes>\<^bsub>P\<^esub> q) = eval R S h s p \<otimes>\<^bsub>S\<^esub> eval R S h s q"
  1286   proof (simp only: eval_on_carrier UP_mult_closed)
  1287     from R S have
  1288       "(\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R (p \<otimes>\<^bsub>P\<^esub> q)}. h (coeff P (p \<otimes>\<^bsub>P\<^esub> q) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) =
  1289       (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R (p \<otimes>\<^bsub>P\<^esub> q)} \<union> {deg R (p \<otimes>\<^bsub>P\<^esub> q)<..deg R p + deg R q}.
  1290         h (coeff P (p \<otimes>\<^bsub>P\<^esub> q) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1291       by (simp cong: S.finsum_cong
  1292         add: deg_aboveD S.finsum_Un_disjoint ivl_disj_int_one Pi_def
  1293         del: coeff_mult)
  1294     also from R have "... =
  1295       (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R p + deg R q}. h (coeff P (p \<otimes>\<^bsub>P\<^esub> q) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1296       by (simp only: ivl_disj_un_one deg_mult_ring)
  1297     also from R S have "... =
  1298       (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R p + deg R q}.
  1299          \<Oplus>\<^bsub>S\<^esub> k \<in> {..i}.
  1300            h (coeff P p k) \<otimes>\<^bsub>S\<^esub> h (coeff P q (i - k)) \<otimes>\<^bsub>S\<^esub>
  1301            (s (^)\<^bsub>S\<^esub> k \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> (i - k)))"
  1302       by (simp cong: S.finsum_cong add: S.nat_pow_mult Pi_def
  1303         S.m_ac S.finsum_rdistr)
  1304     also from R S have "... =
  1305       (\<Oplus>\<^bsub>S\<^esub> i\<in>{..deg R p}. h (coeff P p i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) \<otimes>\<^bsub>S\<^esub>
  1306       (\<Oplus>\<^bsub>S\<^esub> i\<in>{..deg R q}. h (coeff P q i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1307       by (simp add: S.cauchy_product [THEN sym] bound.intro deg_aboveD S.m_ac
  1308         Pi_def)
  1309     finally show
  1310       "(\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R (p \<otimes>\<^bsub>P\<^esub> q)}. h (coeff P (p \<otimes>\<^bsub>P\<^esub> q) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) =
  1311       (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R p}. h (coeff P p i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) \<otimes>\<^bsub>S\<^esub>
  1312       (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R q}. h (coeff P q i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)" .
  1313   qed
  1314 qed
  1315 
  1316 text {*
  1317   The following lemma could be proved in @{text UP_cring} with the additional
  1318   assumption that @{text h} is closed. *}
  1319 
  1320 lemma (in UP_pre_univ_prop) eval_const:
  1321   "[| s \<in> carrier S; r \<in> carrier R |] ==> eval R S h s (monom P r 0) = h r"
  1322   by (simp only: eval_on_carrier monom_closed) simp
  1323 
  1324 text {* Further properties of the evaluation homomorphism. *}
  1325 
  1326 text {* The following proof is complicated by the fact that in arbitrary
  1327   rings one might have @{term "one R = zero R"}. *}
  1328 
  1329 (* TODO: simplify by cases "one R = zero R" *)
  1330 
  1331 lemma (in UP_pre_univ_prop) eval_monom1:
  1332   assumes S: "s \<in> carrier S"
  1333   shows "eval R S h s (monom P \<one> 1) = s"
  1334 proof (simp only: eval_on_carrier monom_closed R.one_closed)
  1335    from S have
  1336     "(\<Oplus>\<^bsub>S\<^esub> i\<in>{..deg R (monom P \<one> 1)}. h (coeff P (monom P \<one> 1) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) =
  1337     (\<Oplus>\<^bsub>S\<^esub> i\<in>{..deg R (monom P \<one> 1)} \<union> {deg R (monom P \<one> 1)<..1}.
  1338       h (coeff P (monom P \<one> 1) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1339     by (simp cong: S.finsum_cong del: coeff_monom
  1340       add: deg_aboveD S.finsum_Un_disjoint ivl_disj_int_one Pi_def)
  1341   also have "... =
  1342     (\<Oplus>\<^bsub>S\<^esub> i \<in> {..1}. h (coeff P (monom P \<one> 1) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1343     by (simp only: ivl_disj_un_one deg_monom_le R.one_closed)
  1344   also have "... = s"
  1345   proof (cases "s = \<zero>\<^bsub>S\<^esub>")
  1346     case True then show ?thesis by (simp add: Pi_def)
  1347   next
  1348     case False then show ?thesis by (simp add: S Pi_def)
  1349   qed
  1350   finally show "(\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R (monom P \<one> 1)}.
  1351     h (coeff P (monom P \<one> 1) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) = s" .
  1352 qed
  1353 
  1354 end
  1355 
  1356 text {* Interpretation of ring homomorphism lemmas. *}
  1357 
  1358 interpretation UP_univ_prop < ring_hom_cring P S Eval
  1359   apply (unfold Eval_def)
  1360   apply intro_locales
  1361   apply (rule ring_hom_cring.axioms)
  1362   apply (rule ring_hom_cring.intro)
  1363   apply unfold_locales
  1364   apply (rule eval_ring_hom)
  1365   apply rule
  1366   done
  1367 
  1368 lemma (in UP_cring) monom_pow:
  1369   assumes R: "a \<in> carrier R"
  1370   shows "(monom P a n) (^)\<^bsub>P\<^esub> m = monom P (a (^) m) (n * m)"
  1371 proof (induct m)
  1372   case 0 from R show ?case by simp
  1373 next
  1374   case Suc with R show ?case
  1375     by (simp del: monom_mult add: monom_mult [THEN sym] add_commute)
  1376 qed
  1377 
  1378 lemma (in ring_hom_cring) hom_pow [simp]:
  1379   "x \<in> carrier R ==> h (x (^) n) = h x (^)\<^bsub>S\<^esub> (n::nat)"
  1380   by (induct n) simp_all
  1381 
  1382 lemma (in UP_univ_prop) Eval_monom:
  1383   "r \<in> carrier R ==> Eval (monom P r n) = h r \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> n"
  1384 proof -
  1385   assume R: "r \<in> carrier R"
  1386   from R have "Eval (monom P r n) = Eval (monom P r 0 \<otimes>\<^bsub>P\<^esub> (monom P \<one> 1) (^)\<^bsub>P\<^esub> n)"
  1387     by (simp del: monom_mult add: monom_mult [THEN sym] monom_pow)
  1388   also
  1389   from R eval_monom1 [where s = s, folded Eval_def]
  1390   have "... = h r \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> n"
  1391     by (simp add: eval_const [where s = s, folded Eval_def])
  1392   finally show ?thesis .
  1393 qed
  1394 
  1395 lemma (in UP_pre_univ_prop) eval_monom:
  1396   assumes R: "r \<in> carrier R" and S: "s \<in> carrier S"
  1397   shows "eval R S h s (monom P r n) = h r \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> n"
  1398 proof -
  1399   interpret UP_univ_prop [R S h P s _]
  1400     using UP_pre_univ_prop_axioms P_def R S
  1401     by (auto intro: UP_univ_prop.intro UP_univ_prop_axioms.intro)
  1402   from R
  1403   show ?thesis by (rule Eval_monom)
  1404 qed
  1405 
  1406 lemma (in UP_univ_prop) Eval_smult:
  1407   "[| r \<in> carrier R; p \<in> carrier P |] ==> Eval (r \<odot>\<^bsub>P\<^esub> p) = h r \<otimes>\<^bsub>S\<^esub> Eval p"
  1408 proof -
  1409   assume R: "r \<in> carrier R" and P: "p \<in> carrier P"
  1410   then show ?thesis
  1411     by (simp add: monom_mult_is_smult [THEN sym]
  1412       eval_const [where s = s, folded Eval_def])
  1413 qed
  1414 
  1415 lemma ring_hom_cringI:
  1416   assumes "cring R"
  1417     and "cring S"
  1418     and "h \<in> ring_hom R S"
  1419   shows "ring_hom_cring R S h"
  1420   by (fast intro: ring_hom_cring.intro ring_hom_cring_axioms.intro
  1421     cring.axioms assms)
  1422 
  1423 context UP_pre_univ_prop
  1424 begin
  1425 
  1426 lemma UP_hom_unique:
  1427   assumes "ring_hom_cring P S Phi"
  1428   assumes Phi: "Phi (monom P \<one> (Suc 0)) = s"
  1429       "!!r. r \<in> carrier R ==> Phi (monom P r 0) = h r"
  1430   assumes "ring_hom_cring P S Psi"
  1431   assumes Psi: "Psi (monom P \<one> (Suc 0)) = s"
  1432       "!!r. r \<in> carrier R ==> Psi (monom P r 0) = h r"
  1433     and P: "p \<in> carrier P" and S: "s \<in> carrier S"
  1434   shows "Phi p = Psi p"
  1435 proof -
  1436   interpret ring_hom_cring [P S Phi] by fact
  1437   interpret ring_hom_cring [P S Psi] by fact
  1438   have "Phi p =
  1439       Phi (\<Oplus>\<^bsub>P \<^esub>i \<in> {..deg R p}. monom P (coeff P p i) 0 \<otimes>\<^bsub>P\<^esub> monom P \<one> 1 (^)\<^bsub>P\<^esub> i)"
  1440     by (simp add: up_repr P monom_mult [THEN sym] monom_pow del: monom_mult)
  1441   also
  1442   have "... =
  1443       Psi (\<Oplus>\<^bsub>P \<^esub>i\<in>{..deg R p}. monom P (coeff P p i) 0 \<otimes>\<^bsub>P\<^esub> monom P \<one> 1 (^)\<^bsub>P\<^esub> i)"
  1444     by (simp add: Phi Psi P Pi_def comp_def)
  1445   also have "... = Psi p"
  1446     by (simp add: up_repr P monom_mult [THEN sym] monom_pow del: monom_mult)
  1447   finally show ?thesis .
  1448 qed
  1449 
  1450 lemma ring_homD:
  1451   assumes Phi: "Phi \<in> ring_hom P S"
  1452   shows "ring_hom_cring P S Phi"
  1453 proof (rule ring_hom_cring.intro)
  1454   show "ring_hom_cring_axioms P S Phi"
  1455   by (rule ring_hom_cring_axioms.intro) (rule Phi)
  1456 qed unfold_locales
  1457 
  1458 theorem UP_universal_property:
  1459   assumes S: "s \<in> carrier S"
  1460   shows "EX! Phi. Phi \<in> ring_hom P S \<inter> extensional (carrier P) &
  1461     Phi (monom P \<one> 1) = s &
  1462     (ALL r : carrier R. Phi (monom P r 0) = h r)"
  1463   using S eval_monom1
  1464   apply (auto intro: eval_ring_hom eval_const eval_extensional)
  1465   apply (rule extensionalityI)
  1466   apply (auto intro: UP_hom_unique ring_homD)
  1467   done
  1468 
  1469 end
  1470 
  1471 
  1472 subsection {* Sample Application of Evaluation Homomorphism *}
  1473 
  1474 lemma UP_pre_univ_propI:
  1475   assumes "cring R"
  1476     and "cring S"
  1477     and "h \<in> ring_hom R S"
  1478   shows "UP_pre_univ_prop R S h"
  1479   using assms
  1480   by (auto intro!: UP_pre_univ_prop.intro ring_hom_cring.intro
  1481     ring_hom_cring_axioms.intro UP_cring.intro)
  1482 
  1483 definition  INTEG :: "int ring"
  1484   where INTEG_def: "INTEG == (| carrier = UNIV, mult = op *, one = 1, zero = 0, add = op + |)"
  1485 
  1486 lemma INTEG_cring:
  1487   "cring INTEG"
  1488   by (unfold INTEG_def) (auto intro!: cringI abelian_groupI comm_monoidI
  1489     zadd_zminus_inverse2 zadd_zmult_distrib)
  1490 
  1491 lemma INTEG_id_eval:
  1492   "UP_pre_univ_prop INTEG INTEG id"
  1493   by (fast intro: UP_pre_univ_propI INTEG_cring id_ring_hom)
  1494 
  1495 text {*
  1496   Interpretation now enables to import all theorems and lemmas
  1497   valid in the context of homomorphisms between @{term INTEG} and @{term
  1498   "UP INTEG"} globally.
  1499 *}
  1500 
  1501 interpretation INTEG: UP_pre_univ_prop [INTEG INTEG id] using INTEG_id_eval by simp_all
  1502 
  1503 lemma INTEG_closed [intro, simp]:
  1504   "z \<in> carrier INTEG"
  1505   by (unfold INTEG_def) simp
  1506 
  1507 lemma INTEG_mult [simp]:
  1508   "mult INTEG z w = z * w"
  1509   by (unfold INTEG_def) simp
  1510 
  1511 lemma INTEG_pow [simp]:
  1512   "pow INTEG z n = z ^ n"
  1513   by (induct n) (simp_all add: INTEG_def nat_pow_def)
  1514 
  1515 lemma "eval INTEG INTEG id 10 (monom (UP INTEG) 5 2) = 500"
  1516   by (simp add: INTEG.eval_monom)
  1517 
  1518 end