src/HOL/Algebra/UnivPoly.thy
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (20 months ago)
changeset 67003 49850a679c2c
parent 64913 3a9eb793fa10
child 67091 1393c2340eec
permissions -rw-r--r--
more robust sorted_entries;
     1 (*  Title:      HOL/Algebra/UnivPoly.thy
     2     Author:     Clemens Ballarin, started 9 December 1996
     3     Copyright:  Clemens Ballarin
     4 
     5 Contributions, in particular on long division, by Jesus Aransay.
     6 *)
     7 
     8 theory UnivPoly
     9 imports Module RingHom
    10 begin
    11 
    12 section \<open>Univariate Polynomials\<close>
    13 
    14 text \<open>
    15   Polynomials are formalised as modules with additional operations for
    16   extracting coefficients from polynomials and for obtaining monomials
    17   from coefficients and exponents (record \<open>up_ring\<close>).  The
    18   carrier set is a set of bounded functions from Nat to the
    19   coefficient domain.  Bounded means that these functions return zero
    20   above a certain bound (the degree).  There is a chapter on the
    21   formalisation of polynomials in the PhD thesis @{cite "Ballarin:1999"},
    22   which was implemented with axiomatic type classes.  This was later
    23   ported to Locales.
    24 \<close>
    25 
    26 
    27 subsection \<open>The Constructor for Univariate Polynomials\<close>
    28 
    29 text \<open>
    30   Functions with finite support.
    31 \<close>
    32 
    33 locale bound =
    34   fixes z :: 'a
    35     and n :: nat
    36     and f :: "nat => 'a"
    37   assumes bound: "!!m. n < m \<Longrightarrow> f m = z"
    38 
    39 declare bound.intro [intro!]
    40   and bound.bound [dest]
    41 
    42 lemma bound_below:
    43   assumes bound: "bound z m f" and nonzero: "f n \<noteq> z" shows "n \<le> m"
    44 proof (rule classical)
    45   assume "~ ?thesis"
    46   then have "m < n" by arith
    47   with bound have "f n = z" ..
    48   with nonzero show ?thesis by contradiction
    49 qed
    50 
    51 record ('a, 'p) up_ring = "('a, 'p) module" +
    52   monom :: "['a, nat] => 'p"
    53   coeff :: "['p, nat] => 'a"
    54 
    55 definition
    56   up :: "('a, 'm) ring_scheme => (nat => 'a) set"
    57   where "up R = {f. f \<in> UNIV \<rightarrow> carrier R & (EX n. bound \<zero>\<^bsub>R\<^esub> n f)}"
    58 
    59 definition UP :: "('a, 'm) ring_scheme => ('a, nat => 'a) up_ring"
    60   where "UP R = \<lparr>
    61    carrier = up R,
    62    mult = (\<lambda>p\<in>up R. \<lambda>q\<in>up R. \<lambda>n. \<Oplus>\<^bsub>R\<^esub>i \<in> {..n}. p i \<otimes>\<^bsub>R\<^esub> q (n-i)),
    63    one = (\<lambda>i. if i=0 then \<one>\<^bsub>R\<^esub> else \<zero>\<^bsub>R\<^esub>),
    64    zero = (\<lambda>i. \<zero>\<^bsub>R\<^esub>),
    65    add = (\<lambda>p\<in>up R. \<lambda>q\<in>up R. \<lambda>i. p i \<oplus>\<^bsub>R\<^esub> q i),
    66    smult = (\<lambda>a\<in>carrier R. \<lambda>p\<in>up R. \<lambda>i. a \<otimes>\<^bsub>R\<^esub> p i),
    67    monom = (\<lambda>a\<in>carrier R. \<lambda>n i. if i=n then a else \<zero>\<^bsub>R\<^esub>),
    68    coeff = (\<lambda>p\<in>up R. \<lambda>n. p n)\<rparr>"
    69 
    70 text \<open>
    71   Properties of the set of polynomials @{term up}.
    72 \<close>
    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: "(\<lambda>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 |] ==> (\<lambda>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 |] ==> (\<lambda>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 (\<lambda>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) (\<lambda>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 fastforce
   109     qed
   110     then show ?thesis ..
   111   qed
   112 qed
   113 
   114 lemma up_a_inv_closed:
   115   "p \<in> up R ==> (\<lambda>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 (\<lambda>i. \<ominus> p i)" by auto
   120   then show "EX n. bound \<zero> n (\<lambda>i. \<ominus> p i)" by auto
   121 qed auto
   122 
   123 lemma up_minus_closed:
   124   "[| p \<in> up R; q \<in> up R |] ==> (\<lambda>i. p i \<ominus> q i) \<in> up R"
   125   using mem_upD [of p R] mem_upD [of q R] up_add_closed up_a_inv_closed a_minus_def [of _ R]
   126   by auto
   127 
   128 lemma up_mult_closed:
   129   "[| p \<in> up R; q \<in> up R |] ==>
   130   (\<lambda>n. \<Oplus>i \<in> {..n}. p i \<otimes> q (n-i)) \<in> up R"
   131 proof
   132   fix n
   133   assume "p \<in> up R" "q \<in> up R"
   134   then show "(\<Oplus>i \<in> {..n}. p i \<otimes> q (n-i)) \<in> carrier R"
   135     by (simp add: mem_upD  funcsetI)
   136 next
   137   assume UP: "p \<in> up R" "q \<in> up R"
   138   show "EX n. bound \<zero> n (\<lambda>n. \<Oplus>i \<in> {..n}. p i \<otimes> q (n-i))"
   139   proof -
   140     from UP obtain n where boundn: "bound \<zero> n p" by fast
   141     from UP obtain m where boundm: "bound \<zero> m q" by fast
   142     have "bound \<zero> (n + m) (\<lambda>n. \<Oplus>i \<in> {..n}. p i \<otimes> q (n - i))"
   143     proof
   144       fix k assume bound: "n + m < k"
   145       {
   146         fix i
   147         have "p i \<otimes> q (k-i) = \<zero>"
   148         proof (cases "n < i")
   149           case True
   150           with boundn have "p i = \<zero>" by auto
   151           moreover from UP have "q (k-i) \<in> carrier R" by auto
   152           ultimately show ?thesis by simp
   153         next
   154           case False
   155           with bound have "m < k-i" by arith
   156           with boundm have "q (k-i) = \<zero>" by auto
   157           moreover from UP have "p i \<in> carrier R" by auto
   158           ultimately show ?thesis by simp
   159         qed
   160       }
   161       then show "(\<Oplus>i \<in> {..k}. p i \<otimes> q (k-i)) = \<zero>"
   162         by (simp add: Pi_def)
   163     qed
   164     then show ?thesis by fast
   165   qed
   166 qed
   167 
   168 end
   169 
   170 
   171 subsection \<open>Effect of Operations on Coefficients\<close>
   172 
   173 locale UP =
   174   fixes R (structure) and P (structure)
   175   defines P_def: "P == UP R"
   176 
   177 locale UP_ring = UP + R?: ring R
   178 
   179 locale UP_cring = UP + R?: cring R
   180 
   181 sublocale UP_cring < UP_ring
   182   by intro_locales [1] (rule P_def)
   183 
   184 locale UP_domain = UP + R?: "domain" R
   185 
   186 sublocale UP_domain < UP_cring
   187   by intro_locales [1] (rule P_def)
   188 
   189 context UP
   190 begin
   191 
   192 text \<open>Temporarily declare @{thm P_def} as simp rule.\<close>
   193 
   194 declare P_def [simp]
   195 
   196 lemma up_eqI:
   197   assumes prem: "!!n. coeff P p n = coeff P q n" and R: "p \<in> carrier P" "q \<in> carrier P"
   198   shows "p = q"
   199 proof
   200   fix x
   201   from prem and R show "p x = q x" by (simp add: UP_def)
   202 qed
   203 
   204 lemma coeff_closed [simp]:
   205   "p \<in> carrier P ==> coeff P p n \<in> carrier R" by (auto simp add: UP_def)
   206 
   207 end
   208 
   209 context UP_ring
   210 begin
   211 
   212 (* Theorems generalised from commutative rings to rings by Jesus Aransay. *)
   213 
   214 lemma coeff_monom [simp]:
   215   "a \<in> carrier R ==> coeff P (monom P a m) n = (if m=n then a else \<zero>)"
   216 proof -
   217   assume R: "a \<in> carrier R"
   218   then have "(\<lambda>n. if n = m then a else \<zero>) \<in> up R"
   219     using up_def by force
   220   with R show ?thesis by (simp add: UP_def)
   221 qed
   222 
   223 lemma coeff_zero [simp]: "coeff P \<zero>\<^bsub>P\<^esub> n = \<zero>" by (auto simp add: UP_def)
   224 
   225 lemma coeff_one [simp]: "coeff P \<one>\<^bsub>P\<^esub> n = (if n=0 then \<one> else \<zero>)"
   226   using up_one_closed by (simp add: UP_def)
   227 
   228 lemma coeff_smult [simp]:
   229   "[| a \<in> carrier R; p \<in> carrier P |] ==> coeff P (a \<odot>\<^bsub>P\<^esub> p) n = a \<otimes> coeff P p n"
   230   by (simp add: UP_def up_smult_closed)
   231 
   232 lemma coeff_add [simp]:
   233   "[| 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"
   234   by (simp add: UP_def up_add_closed)
   235 
   236 lemma coeff_mult [simp]:
   237   "[| 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))"
   238   by (simp add: UP_def up_mult_closed)
   239 
   240 end
   241 
   242 
   243 subsection \<open>Polynomials Form a Ring.\<close>
   244 
   245 context UP_ring
   246 begin
   247 
   248 text \<open>Operations are closed over @{term P}.\<close>
   249 
   250 lemma UP_mult_closed [simp]:
   251   "[| 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)
   252 
   253 lemma UP_one_closed [simp]:
   254   "\<one>\<^bsub>P\<^esub> \<in> carrier P" by (simp add: UP_def up_one_closed)
   255 
   256 lemma UP_zero_closed [intro, simp]:
   257   "\<zero>\<^bsub>P\<^esub> \<in> carrier P" by (auto simp add: UP_def)
   258 
   259 lemma UP_a_closed [intro, simp]:
   260   "[| 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)
   261 
   262 lemma monom_closed [simp]:
   263   "a \<in> carrier R ==> monom P a n \<in> carrier P" by (auto simp add: UP_def up_def Pi_def)
   264 
   265 lemma UP_smult_closed [simp]:
   266   "[| 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)
   267 
   268 end
   269 
   270 declare (in UP) P_def [simp del]
   271 
   272 text \<open>Algebraic ring properties\<close>
   273 
   274 context UP_ring
   275 begin
   276 
   277 lemma UP_a_assoc:
   278   assumes R: "p \<in> carrier P" "q \<in> carrier P" "r \<in> carrier P"
   279   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)
   280 
   281 lemma UP_l_zero [simp]:
   282   assumes R: "p \<in> carrier P"
   283   shows "\<zero>\<^bsub>P\<^esub> \<oplus>\<^bsub>P\<^esub> p = p" by (rule up_eqI, simp_all add: R)
   284 
   285 lemma UP_l_neg_ex:
   286   assumes R: "p \<in> carrier P"
   287   shows "EX q : carrier P. q \<oplus>\<^bsub>P\<^esub> p = \<zero>\<^bsub>P\<^esub>"
   288 proof -
   289   let ?q = "\<lambda>i. \<ominus> (p i)"
   290   from R have closed: "?q \<in> carrier P"
   291     by (simp add: UP_def P_def up_a_inv_closed)
   292   from R have coeff: "!!n. coeff P ?q n = \<ominus> (coeff P p n)"
   293     by (simp add: UP_def P_def up_a_inv_closed)
   294   show ?thesis
   295   proof
   296     show "?q \<oplus>\<^bsub>P\<^esub> p = \<zero>\<^bsub>P\<^esub>"
   297       by (auto intro!: up_eqI simp add: R closed coeff R.l_neg)
   298   qed (rule closed)
   299 qed
   300 
   301 lemma UP_a_comm:
   302   assumes R: "p \<in> carrier P" "q \<in> carrier P"
   303   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)
   304 
   305 lemma UP_m_assoc:
   306   assumes R: "p \<in> carrier P" "q \<in> carrier P" "r \<in> carrier P"
   307   shows "(p \<otimes>\<^bsub>P\<^esub> q) \<otimes>\<^bsub>P\<^esub> r = p \<otimes>\<^bsub>P\<^esub> (q \<otimes>\<^bsub>P\<^esub> r)"
   308 proof (rule up_eqI)
   309   fix n
   310   {
   311     fix k and a b c :: "nat=>'a"
   312     assume R: "a \<in> UNIV \<rightarrow> carrier R" "b \<in> UNIV \<rightarrow> carrier R"
   313       "c \<in> UNIV \<rightarrow> carrier R"
   314     then have "k <= n ==>
   315       (\<Oplus>j \<in> {..k}. (\<Oplus>i \<in> {..j}. a i \<otimes> b (j-i)) \<otimes> c (n-j)) =
   316       (\<Oplus>j \<in> {..k}. a j \<otimes> (\<Oplus>i \<in> {..k-j}. b i \<otimes> c (n-j-i)))"
   317       (is "_ \<Longrightarrow> ?eq k")
   318     proof (induct k)
   319       case 0 then show ?case by (simp add: Pi_def m_assoc)
   320     next
   321       case (Suc k)
   322       then have "k <= n" by arith
   323       from this R have "?eq k" by (rule Suc)
   324       with R show ?case
   325         by (simp cong: finsum_cong
   326              add: Suc_diff_le Pi_def l_distr r_distr m_assoc)
   327            (simp cong: finsum_cong add: Pi_def a_ac finsum_ldistr m_assoc)
   328     qed
   329   }
   330   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"
   331     by (simp add: Pi_def)
   332 qed (simp_all add: R)
   333 
   334 lemma UP_r_one [simp]:
   335   assumes R: "p \<in> carrier P" shows "p \<otimes>\<^bsub>P\<^esub> \<one>\<^bsub>P\<^esub> = p"
   336 proof (rule up_eqI)
   337   fix n
   338   show "coeff P (p \<otimes>\<^bsub>P\<^esub> \<one>\<^bsub>P\<^esub>) n = coeff P p n"
   339   proof (cases n)
   340     case 0
   341     {
   342       with R show ?thesis by simp
   343     }
   344   next
   345     case Suc
   346     {
   347       (*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 get it to work here*)
   348       fix nn assume Succ: "n = Suc nn"
   349       have "coeff P (p \<otimes>\<^bsub>P\<^esub> \<one>\<^bsub>P\<^esub>) (Suc nn) = coeff P p (Suc nn)"
   350       proof -
   351         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
   352         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>))"
   353           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
   354         also have "\<dots> = coeff P p (Suc nn) \<otimes> (if Suc nn \<le> Suc nn then \<one> else \<zero>)"
   355         proof -
   356           have "(\<Oplus>i\<in>{..nn}. coeff P p i \<otimes> (if Suc nn \<le> i then \<one> else \<zero>)) = (\<Oplus>i\<in>{..nn}. \<zero>)"
   357             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
   358             unfolding Pi_def by simp
   359           also have "\<dots> = \<zero>" by simp
   360           finally show ?thesis using r_zero R by simp
   361         qed
   362         also have "\<dots> = coeff P p (Suc nn)" using R by simp
   363         finally show ?thesis by simp
   364       qed
   365       then show ?thesis using Succ by simp
   366     }
   367   qed
   368 qed (simp_all add: R)
   369 
   370 lemma UP_l_one [simp]:
   371   assumes R: "p \<in> carrier P"
   372   shows "\<one>\<^bsub>P\<^esub> \<otimes>\<^bsub>P\<^esub> p = p"
   373 proof (rule up_eqI)
   374   fix n
   375   show "coeff P (\<one>\<^bsub>P\<^esub> \<otimes>\<^bsub>P\<^esub> p) n = coeff P p n"
   376   proof (cases n)
   377     case 0 with R show ?thesis by simp
   378   next
   379     case Suc with R show ?thesis
   380       by (simp del: finsum_Suc add: finsum_Suc2 Pi_def)
   381   qed
   382 qed (simp_all add: R)
   383 
   384 lemma UP_l_distr:
   385   assumes R: "p \<in> carrier P" "q \<in> carrier P" "r \<in> carrier P"
   386   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)"
   387   by (rule up_eqI) (simp add: l_distr R Pi_def, simp_all add: R)
   388 
   389 lemma UP_r_distr:
   390   assumes R: "p \<in> carrier P" "q \<in> carrier P" "r \<in> carrier P"
   391   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)"
   392   by (rule up_eqI) (simp add: r_distr R Pi_def, simp_all add: R)
   393 
   394 theorem UP_ring: "ring P"
   395   by (auto intro!: ringI abelian_groupI monoidI UP_a_assoc)
   396     (auto intro: UP_a_comm UP_l_neg_ex UP_m_assoc UP_l_distr UP_r_distr)
   397 
   398 end
   399 
   400 
   401 subsection \<open>Polynomials Form a Commutative Ring.\<close>
   402 
   403 context UP_cring
   404 begin
   405 
   406 lemma UP_m_comm:
   407   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"
   408 proof (rule up_eqI)
   409   fix n
   410   {
   411     fix k and a b :: "nat=>'a"
   412     assume R: "a \<in> UNIV \<rightarrow> carrier R" "b \<in> UNIV \<rightarrow> carrier R"
   413     then have "k <= n ==>
   414       (\<Oplus>i \<in> {..k}. a i \<otimes> b (n-i)) = (\<Oplus>i \<in> {..k}. a (k-i) \<otimes> b (i+n-k))"
   415       (is "_ \<Longrightarrow> ?eq k")
   416     proof (induct k)
   417       case 0 then show ?case by (simp add: Pi_def)
   418     next
   419       case (Suc k) then show ?case
   420         by (subst (2) finsum_Suc2) (simp add: Pi_def a_comm)+
   421     qed
   422   }
   423   note l = this
   424   from R1 R2 show "coeff P (p \<otimes>\<^bsub>P\<^esub> q) n =  coeff P (q \<otimes>\<^bsub>P\<^esub> p) n"
   425     unfolding coeff_mult [OF R1 R2, of n]
   426     unfolding coeff_mult [OF R2 R1, of n]
   427     using l [of "(\<lambda>i. coeff P p i)" "(\<lambda>i. coeff P q i)" "n"] by (simp add: Pi_def m_comm)
   428 qed (simp_all add: R1 R2)
   429 
   430 
   431 subsection \<open>Polynomials over a commutative ring for a commutative ring\<close>
   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 sublocale UP_ring < P?: ring P using UP_ring .
   461 sublocale UP_cring < P?: cring P using UP_cring .
   462 
   463 
   464 subsection \<open>Polynomials Form an Algebra\<close>
   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 \<open>
   500   Interpretation of lemmas from @{term algebra}.
   501 \<close>
   502 
   503 lemma (in cring) cring:
   504   "cring R" ..
   505 
   506 lemma (in UP_cring) UP_algebra:
   507   "algebra R P" by (auto intro!: algebraI R.cring UP_cring UP_smult_l_distr UP_smult_r_distr
   508     UP_smult_assoc1 UP_smult_assoc2)
   509 
   510 sublocale UP_cring < algebra R P using UP_algebra .
   511 
   512 
   513 subsection \<open>Further Lemmas Involving Monomials\<close>
   514 
   515 context UP_ring
   516 begin
   517 
   518 lemma monom_zero [simp]:
   519   "monom P \<zero> n = \<zero>\<^bsub>P\<^esub>" by (simp add: UP_def P_def)
   520 
   521 lemma monom_mult_is_smult:
   522   assumes R: "a \<in> carrier R" "p \<in> carrier P"
   523   shows "monom P a 0 \<otimes>\<^bsub>P\<^esub> p = a \<odot>\<^bsub>P\<^esub> p"
   524 proof (rule up_eqI)
   525   fix n
   526   show "coeff P (monom P a 0 \<otimes>\<^bsub>P\<^esub> p) n = coeff P (a \<odot>\<^bsub>P\<^esub> p) n"
   527   proof (cases n)
   528     case 0 with R show ?thesis by simp
   529   next
   530     case Suc with R show ?thesis
   531       using R.finsum_Suc2 by (simp del: R.finsum_Suc add: Pi_def)
   532   qed
   533 qed (simp_all add: R)
   534 
   535 lemma monom_one [simp]:
   536   "monom P \<one> 0 = \<one>\<^bsub>P\<^esub>"
   537   by (rule up_eqI) simp_all
   538 
   539 lemma monom_add [simp]:
   540   "[| a \<in> carrier R; b \<in> carrier R |] ==>
   541   monom P (a \<oplus> b) n = monom P a n \<oplus>\<^bsub>P\<^esub> monom P b n"
   542   by (rule up_eqI) simp_all
   543 
   544 lemma monom_one_Suc:
   545   "monom P \<one> (Suc n) = monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> 1"
   546 proof (rule up_eqI)
   547   fix k
   548   show "coeff P (monom P \<one> (Suc n)) k = coeff P (monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> 1) k"
   549   proof (cases "k = Suc n")
   550     case True show ?thesis
   551     proof -
   552       fix m
   553       from True have less_add_diff:
   554         "!!i. [| n < i; i <= n + m |] ==> n + m - i < m" by arith
   555       from True have "coeff P (monom P \<one> (Suc n)) k = \<one>" by simp
   556       also from True
   557       have "... = (\<Oplus>i \<in> {..<n} \<union> {n}. coeff P (monom P \<one> n) i \<otimes>
   558         coeff P (monom P \<one> 1) (k - i))"
   559         by (simp cong: R.finsum_cong add: Pi_def)
   560       also have "... = (\<Oplus>i \<in>  {..n}. coeff P (monom P \<one> n) i \<otimes>
   561         coeff P (monom P \<one> 1) (k - i))"
   562         by (simp only: ivl_disj_un_singleton)
   563       also from True
   564       have "... = (\<Oplus>i \<in> {..n} \<union> {n<..k}. coeff P (monom P \<one> n) i \<otimes>
   565         coeff P (monom P \<one> 1) (k - i))"
   566         by (simp cong: R.finsum_cong add: R.finsum_Un_disjoint ivl_disj_int_one
   567           order_less_imp_not_eq Pi_def)
   568       also from True have "... = coeff P (monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> 1) k"
   569         by (simp add: ivl_disj_un_one)
   570       finally show ?thesis .
   571     qed
   572   next
   573     case False
   574     note neq = False
   575     let ?s =
   576       "\<lambda>i. (if n = i then \<one> else \<zero>) \<otimes> (if Suc 0 = k - i then \<one> else \<zero>)"
   577     from neq have "coeff P (monom P \<one> (Suc n)) k = \<zero>" by simp
   578     also have "... = (\<Oplus>i \<in> {..k}. ?s i)"
   579     proof -
   580       have f1: "(\<Oplus>i \<in> {..<n}. ?s i) = \<zero>"
   581         by (simp cong: R.finsum_cong add: Pi_def)
   582       from neq have f2: "(\<Oplus>i \<in> {n}. ?s i) = \<zero>"
   583         by (simp cong: R.finsum_cong add: Pi_def) arith
   584       have f3: "n < k ==> (\<Oplus>i \<in> {n<..k}. ?s i) = \<zero>"
   585         by (simp cong: R.finsum_cong add: order_less_imp_not_eq Pi_def)
   586       show ?thesis
   587       proof (cases "k < n")
   588         case True then show ?thesis by (simp cong: R.finsum_cong add: Pi_def)
   589       next
   590         case False then have n_le_k: "n <= k" by arith
   591         show ?thesis
   592         proof (cases "n = k")
   593           case True
   594           then have "\<zero> = (\<Oplus>i \<in> {..<n} \<union> {n}. ?s i)"
   595             by (simp cong: R.finsum_cong add: Pi_def)
   596           also from True have "... = (\<Oplus>i \<in> {..k}. ?s i)"
   597             by (simp only: ivl_disj_un_singleton)
   598           finally show ?thesis .
   599         next
   600           case False with n_le_k have n_less_k: "n < k" by arith
   601           with neq have "\<zero> = (\<Oplus>i \<in> {..<n} \<union> {n}. ?s i)"
   602             by (simp add: R.finsum_Un_disjoint f1 f2 Pi_def del: Un_insert_right)
   603           also have "... = (\<Oplus>i \<in> {..n}. ?s i)"
   604             by (simp only: ivl_disj_un_singleton)
   605           also from n_less_k neq have "... = (\<Oplus>i \<in> {..n} \<union> {n<..k}. ?s i)"
   606             by (simp add: R.finsum_Un_disjoint f3 ivl_disj_int_one Pi_def)
   607           also from n_less_k have "... = (\<Oplus>i \<in> {..k}. ?s i)"
   608             by (simp only: ivl_disj_un_one)
   609           finally show ?thesis .
   610         qed
   611       qed
   612     qed
   613     also have "... = coeff P (monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> 1) k" by simp
   614     finally show ?thesis .
   615   qed
   616 qed (simp_all)
   617 
   618 lemma monom_one_Suc2:
   619   "monom P \<one> (Suc n) = monom P \<one> 1 \<otimes>\<^bsub>P\<^esub> monom P \<one> n"
   620 proof (induct n)
   621   case 0 show ?case by simp
   622 next
   623   case Suc
   624   {
   625     fix k:: nat
   626     assume hypo: "monom P \<one> (Suc k) = monom P \<one> 1 \<otimes>\<^bsub>P\<^esub> monom P \<one> k"
   627     then show "monom P \<one> (Suc (Suc k)) = monom P \<one> 1 \<otimes>\<^bsub>P\<^esub> monom P \<one> (Suc k)"
   628     proof -
   629       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"
   630         unfolding monom_one_Suc [of "Suc k"] unfolding hypo ..
   631       note cl = monom_closed [OF R.one_closed, of 1]
   632       note clk = monom_closed [OF R.one_closed, of k]
   633       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"
   634         unfolding monom_one_Suc [of k] unfolding sym [OF m_assoc  [OF cl clk cl]] ..
   635       from lhs rhs show ?thesis by simp
   636     qed
   637   }
   638 qed
   639 
   640 text\<open>The following corollary follows from lemmas @{thm "monom_one_Suc"}
   641   and @{thm "monom_one_Suc2"}, and is trivial in @{term UP_cring}\<close>
   642 
   643 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"
   644   unfolding monom_one_Suc [symmetric] monom_one_Suc2 [symmetric] ..
   645 
   646 lemma monom_mult_smult:
   647   "[| a \<in> carrier R; b \<in> carrier R |] ==> monom P (a \<otimes> b) n = a \<odot>\<^bsub>P\<^esub> monom P b n"
   648   by (rule up_eqI) simp_all
   649 
   650 lemma monom_one_mult:
   651   "monom P \<one> (n + m) = monom P \<one> n \<otimes>\<^bsub>P\<^esub> monom P \<one> m"
   652 proof (induct n)
   653   case 0 show ?case by simp
   654 next
   655   case Suc then show ?case
   656     unfolding add_Suc unfolding monom_one_Suc unfolding Suc.hyps
   657     using m_assoc monom_one_comm [of m] by simp
   658 qed
   659 
   660 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"
   661   unfolding monom_one_mult [symmetric] by (rule up_eqI) simp_all
   662 
   663 lemma monom_mult [simp]:
   664   assumes a_in_R: "a \<in> carrier R" and b_in_R: "b \<in> carrier R"
   665   shows "monom P (a \<otimes> b) (n + m) = monom P a n \<otimes>\<^bsub>P\<^esub> monom P b m"
   666 proof (rule up_eqI)
   667   fix k
   668   show "coeff P (monom P (a \<otimes> b) (n + m)) k = coeff P (monom P a n \<otimes>\<^bsub>P\<^esub> monom P b m) k"
   669   proof (cases "n + m = k")
   670     case True
   671     {
   672       show ?thesis
   673         unfolding True [symmetric]
   674           coeff_mult [OF monom_closed [OF a_in_R, of n] monom_closed [OF b_in_R, of m], of "n + m"]
   675           coeff_monom [OF a_in_R, of n] coeff_monom [OF b_in_R, of m]
   676         using R.finsum_cong [of "{.. n + m}" "{.. n + m}" "(\<lambda>i. (if n = i then a else \<zero>) \<otimes> (if m = n + m - i then b else \<zero>))"
   677           "(\<lambda>i. if n = i then a \<otimes> b else \<zero>)"]
   678           a_in_R b_in_R
   679         unfolding simp_implies_def
   680         using R.finsum_singleton [of n "{.. n + m}" "(\<lambda>i. a \<otimes> b)"]
   681         unfolding Pi_def by auto
   682     }
   683   next
   684     case False
   685     {
   686       show ?thesis
   687         unfolding coeff_monom [OF R.m_closed [OF a_in_R b_in_R], of "n + m" k] apply (simp add: False)
   688         unfolding coeff_mult [OF monom_closed [OF a_in_R, of n] monom_closed [OF b_in_R, of m], of k]
   689         unfolding coeff_monom [OF a_in_R, of n] unfolding coeff_monom [OF b_in_R, of m] using False
   690         using R.finsum_cong [of "{..k}" "{..k}" "(\<lambda>i. (if n = i then a else \<zero>) \<otimes> (if m = k - i then b else \<zero>))" "(\<lambda>i. \<zero>)"]
   691         unfolding Pi_def simp_implies_def using a_in_R b_in_R by force
   692     }
   693   qed
   694 qed (simp_all add: a_in_R b_in_R)
   695 
   696 lemma monom_a_inv [simp]:
   697   "a \<in> carrier R ==> monom P (\<ominus> a) n = \<ominus>\<^bsub>P\<^esub> monom P a n"
   698   by (rule up_eqI) simp_all
   699 
   700 lemma monom_inj:
   701   "inj_on (\<lambda>a. monom P a n) (carrier R)"
   702 proof (rule inj_onI)
   703   fix x y
   704   assume R: "x \<in> carrier R" "y \<in> carrier R" and eq: "monom P x n = monom P y n"
   705   then have "coeff P (monom P x n) n = coeff P (monom P y n) n" by simp
   706   with R show "x = y" by simp
   707 qed
   708 
   709 end
   710 
   711 
   712 subsection \<open>The Degree Function\<close>
   713 
   714 definition
   715   deg :: "[('a, 'm) ring_scheme, nat => 'a] => nat"
   716   where "deg R p = (LEAST n. bound \<zero>\<^bsub>R\<^esub> n (coeff (UP R) p))"
   717 
   718 context UP_ring
   719 begin
   720 
   721 lemma deg_aboveI:
   722   "[| (!!m. n < m ==> coeff P p m = \<zero>); p \<in> carrier P |] ==> deg R p <= n"
   723   by (unfold deg_def P_def) (fast intro: Least_le)
   724 
   725 (*
   726 lemma coeff_bound_ex: "EX n. bound n (coeff p)"
   727 proof -
   728   have "(\<lambda>n. coeff p n) : UP" by (simp add: coeff_def Rep_UP)
   729   then obtain n where "bound n (coeff p)" by (unfold UP_def) fast
   730   then show ?thesis ..
   731 qed
   732 
   733 lemma bound_coeff_obtain:
   734   assumes prem: "(!!n. bound n (coeff p) ==> P)" shows "P"
   735 proof -
   736   have "(\<lambda>n. coeff p n) : UP" by (simp add: coeff_def Rep_UP)
   737   then obtain n where "bound n (coeff p)" by (unfold UP_def) fast
   738   with prem show P .
   739 qed
   740 *)
   741 
   742 lemma deg_aboveD:
   743   assumes "deg R p < m" and "p \<in> carrier P"
   744   shows "coeff P p m = \<zero>"
   745 proof -
   746   from \<open>p \<in> carrier P\<close> obtain n where "bound \<zero> n (coeff P p)"
   747     by (auto simp add: UP_def P_def)
   748   then have "bound \<zero> (deg R p) (coeff P p)"
   749     by (auto simp: deg_def P_def dest: LeastI)
   750   from this and \<open>deg R p < m\<close> show ?thesis ..
   751 qed
   752 
   753 lemma deg_belowI:
   754   assumes non_zero: "n ~= 0 ==> coeff P p n ~= \<zero>"
   755     and R: "p \<in> carrier P"
   756   shows "n <= deg R p"
   757 \<comment> \<open>Logically, this is a slightly stronger version of
   758    @{thm [source] deg_aboveD}\<close>
   759 proof (cases "n=0")
   760   case True then show ?thesis by simp
   761 next
   762   case False then have "coeff P p n ~= \<zero>" by (rule non_zero)
   763   then have "~ deg R p < n" by (fast dest: deg_aboveD intro: R)
   764   then show ?thesis by arith
   765 qed
   766 
   767 lemma lcoeff_nonzero_deg:
   768   assumes deg: "deg R p ~= 0" and R: "p \<in> carrier P"
   769   shows "coeff P p (deg R p) ~= \<zero>"
   770 proof -
   771   from R obtain m where "deg R p <= m" and m_coeff: "coeff P p m ~= \<zero>"
   772   proof -
   773     have minus: "!!(n::nat) m. n ~= 0 ==> (n - Suc 0 < m) = (n <= m)"
   774       by arith
   775     from deg have "deg R p - 1 < (LEAST n. bound \<zero> n (coeff P p))"
   776       by (unfold deg_def P_def) simp
   777     then have "~ bound \<zero> (deg R p - 1) (coeff P p)" by (rule not_less_Least)
   778     then have "EX m. deg R p - 1 < m & coeff P p m ~= \<zero>"
   779       by (unfold bound_def) fast
   780     then have "EX m. deg R p <= m & coeff P p m ~= \<zero>" by (simp add: deg minus)
   781     then show ?thesis by (auto intro: that)
   782   qed
   783   with deg_belowI R have "deg R p = m" by fastforce
   784   with m_coeff show ?thesis by simp
   785 qed
   786 
   787 lemma lcoeff_nonzero_nonzero:
   788   assumes deg: "deg R p = 0" and nonzero: "p ~= \<zero>\<^bsub>P\<^esub>" and R: "p \<in> carrier P"
   789   shows "coeff P p 0 ~= \<zero>"
   790 proof -
   791   have "EX m. coeff P p m ~= \<zero>"
   792   proof (rule classical)
   793     assume "~ ?thesis"
   794     with R have "p = \<zero>\<^bsub>P\<^esub>" by (auto intro: up_eqI)
   795     with nonzero show ?thesis by contradiction
   796   qed
   797   then obtain m where coeff: "coeff P p m ~= \<zero>" ..
   798   from this and R have "m <= deg R p" by (rule deg_belowI)
   799   then have "m = 0" by (simp add: deg)
   800   with coeff show ?thesis by simp
   801 qed
   802 
   803 lemma lcoeff_nonzero:
   804   assumes neq: "p ~= \<zero>\<^bsub>P\<^esub>" and R: "p \<in> carrier P"
   805   shows "coeff P p (deg R p) ~= \<zero>"
   806 proof (cases "deg R p = 0")
   807   case True with neq R show ?thesis by (simp add: lcoeff_nonzero_nonzero)
   808 next
   809   case False with neq R show ?thesis by (simp add: lcoeff_nonzero_deg)
   810 qed
   811 
   812 lemma deg_eqI:
   813   "[| !!m. n < m ==> coeff P p m = \<zero>;
   814       !!n. n ~= 0 ==> coeff P p n ~= \<zero>; p \<in> carrier P |] ==> deg R p = n"
   815 by (fast intro: le_antisym deg_aboveI deg_belowI)
   816 
   817 text \<open>Degree and polynomial operations\<close>
   818 
   819 lemma deg_add [simp]:
   820   "p \<in> carrier P \<Longrightarrow> q \<in> carrier P \<Longrightarrow>
   821   deg R (p \<oplus>\<^bsub>P\<^esub> q) <= max (deg R p) (deg R q)"
   822 by(rule deg_aboveI)(simp_all add: deg_aboveD)
   823 
   824 lemma deg_monom_le:
   825   "a \<in> carrier R ==> deg R (monom P a n) <= n"
   826   by (intro deg_aboveI) simp_all
   827 
   828 lemma deg_monom [simp]:
   829   "[| a ~= \<zero>; a \<in> carrier R |] ==> deg R (monom P a n) = n"
   830   by (fastforce intro: le_antisym deg_aboveI deg_belowI)
   831 
   832 lemma deg_const [simp]:
   833   assumes R: "a \<in> carrier R" shows "deg R (monom P a 0) = 0"
   834 proof (rule le_antisym)
   835   show "deg R (monom P a 0) <= 0" by (rule deg_aboveI) (simp_all add: R)
   836 next
   837   show "0 <= deg R (monom P a 0)" by (rule deg_belowI) (simp_all add: R)
   838 qed
   839 
   840 lemma deg_zero [simp]:
   841   "deg R \<zero>\<^bsub>P\<^esub> = 0"
   842 proof (rule le_antisym)
   843   show "deg R \<zero>\<^bsub>P\<^esub> <= 0" by (rule deg_aboveI) simp_all
   844 next
   845   show "0 <= deg R \<zero>\<^bsub>P\<^esub>" by (rule deg_belowI) simp_all
   846 qed
   847 
   848 lemma deg_one [simp]:
   849   "deg R \<one>\<^bsub>P\<^esub> = 0"
   850 proof (rule le_antisym)
   851   show "deg R \<one>\<^bsub>P\<^esub> <= 0" by (rule deg_aboveI) simp_all
   852 next
   853   show "0 <= deg R \<one>\<^bsub>P\<^esub>" by (rule deg_belowI) simp_all
   854 qed
   855 
   856 lemma deg_uminus [simp]:
   857   assumes R: "p \<in> carrier P" shows "deg R (\<ominus>\<^bsub>P\<^esub> p) = deg R p"
   858 proof (rule le_antisym)
   859   show "deg R (\<ominus>\<^bsub>P\<^esub> p) <= deg R p" by (simp add: deg_aboveI deg_aboveD R)
   860 next
   861   show "deg R p <= deg R (\<ominus>\<^bsub>P\<^esub> p)"
   862     by (simp add: deg_belowI lcoeff_nonzero_deg
   863       inj_on_eq_iff [OF R.a_inv_inj, of _ "\<zero>", simplified] R)
   864 qed
   865 
   866 text\<open>The following lemma is later \emph{overwritten} by the most
   867   specific one for domains, \<open>deg_smult\<close>.\<close>
   868 
   869 lemma deg_smult_ring [simp]:
   870   "[| a \<in> carrier R; p \<in> carrier P |] ==>
   871   deg R (a \<odot>\<^bsub>P\<^esub> p) <= (if a = \<zero> then 0 else deg R p)"
   872   by (cases "a = \<zero>") (simp add: deg_aboveI deg_aboveD)+
   873 
   874 end
   875 
   876 context UP_domain
   877 begin
   878 
   879 lemma deg_smult [simp]:
   880   assumes R: "a \<in> carrier R" "p \<in> carrier P"
   881   shows "deg R (a \<odot>\<^bsub>P\<^esub> p) = (if a = \<zero> then 0 else deg R p)"
   882 proof (rule le_antisym)
   883   show "deg R (a \<odot>\<^bsub>P\<^esub> p) <= (if a = \<zero> then 0 else deg R p)"
   884     using R by (rule deg_smult_ring)
   885 next
   886   show "(if a = \<zero> then 0 else deg R p) <= deg R (a \<odot>\<^bsub>P\<^esub> p)"
   887   proof (cases "a = \<zero>")
   888   qed (simp, simp add: deg_belowI lcoeff_nonzero_deg integral_iff R)
   889 qed
   890 
   891 end
   892 
   893 context UP_ring
   894 begin
   895 
   896 lemma deg_mult_ring:
   897   assumes R: "p \<in> carrier P" "q \<in> carrier P"
   898   shows "deg R (p \<otimes>\<^bsub>P\<^esub> q) <= deg R p + deg R q"
   899 proof (rule deg_aboveI)
   900   fix m
   901   assume boundm: "deg R p + deg R q < m"
   902   {
   903     fix k i
   904     assume boundk: "deg R p + deg R q < k"
   905     then have "coeff P p i \<otimes> coeff P q (k - i) = \<zero>"
   906     proof (cases "deg R p < i")
   907       case True then show ?thesis by (simp add: deg_aboveD R)
   908     next
   909       case False with boundk have "deg R q < k - i" by arith
   910       then show ?thesis by (simp add: deg_aboveD R)
   911     qed
   912   }
   913   with boundm R show "coeff P (p \<otimes>\<^bsub>P\<^esub> q) m = \<zero>" by simp
   914 qed (simp add: R)
   915 
   916 end
   917 
   918 context UP_domain
   919 begin
   920 
   921 lemma deg_mult [simp]:
   922   "[| p ~= \<zero>\<^bsub>P\<^esub>; q ~= \<zero>\<^bsub>P\<^esub>; p \<in> carrier P; q \<in> carrier P |] ==>
   923   deg R (p \<otimes>\<^bsub>P\<^esub> q) = deg R p + deg R q"
   924 proof (rule le_antisym)
   925   assume "p \<in> carrier P" " q \<in> carrier P"
   926   then show "deg R (p \<otimes>\<^bsub>P\<^esub> q) <= deg R p + deg R q" by (rule deg_mult_ring)
   927 next
   928   let ?s = "(\<lambda>i. coeff P p i \<otimes> coeff P q (deg R p + deg R q - i))"
   929   assume R: "p \<in> carrier P" "q \<in> carrier P" and nz: "p ~= \<zero>\<^bsub>P\<^esub>" "q ~= \<zero>\<^bsub>P\<^esub>"
   930   have less_add_diff: "!!(k::nat) n m. k < n ==> m < n + m - k" by arith
   931   show "deg R p + deg R q <= deg R (p \<otimes>\<^bsub>P\<^esub> q)"
   932   proof (rule deg_belowI, simp add: R)
   933     have "(\<Oplus>i \<in> {.. deg R p + deg R q}. ?s i)
   934       = (\<Oplus>i \<in> {..< deg R p} \<union> {deg R p .. deg R p + deg R q}. ?s i)"
   935       by (simp only: ivl_disj_un_one)
   936     also have "... = (\<Oplus>i \<in> {deg R p .. deg R p + deg R q}. ?s i)"
   937       by (simp cong: R.finsum_cong add: R.finsum_Un_disjoint ivl_disj_int_one
   938         deg_aboveD less_add_diff R Pi_def)
   939     also have "...= (\<Oplus>i \<in> {deg R p} \<union> {deg R p <.. deg R p + deg R q}. ?s i)"
   940       by (simp only: ivl_disj_un_singleton)
   941     also have "... = coeff P p (deg R p) \<otimes> coeff P q (deg R q)"
   942       by (simp cong: R.finsum_cong add: deg_aboveD R Pi_def)
   943     finally have "(\<Oplus>i \<in> {.. deg R p + deg R q}. ?s i)
   944       = coeff P p (deg R p) \<otimes> coeff P q (deg R q)" .
   945     with nz show "(\<Oplus>i \<in> {.. deg R p + deg R q}. ?s i) ~= \<zero>"
   946       by (simp add: integral_iff lcoeff_nonzero R)
   947   qed (simp add: R)
   948 qed
   949 
   950 end
   951 
   952 text\<open>The following lemmas also can be lifted to @{term UP_ring}.\<close>
   953 
   954 context UP_ring
   955 begin
   956 
   957 lemma coeff_finsum:
   958   assumes fin: "finite A"
   959   shows "p \<in> A \<rightarrow> carrier P ==>
   960     coeff P (finsum P p A) k = (\<Oplus>i \<in> A. coeff P (p i) k)"
   961   using fin by induct (auto simp: Pi_def)
   962 
   963 lemma up_repr:
   964   assumes R: "p \<in> carrier P"
   965   shows "(\<Oplus>\<^bsub>P\<^esub> i \<in> {..deg R p}. monom P (coeff P p i) i) = p"
   966 proof (rule up_eqI)
   967   let ?s = "(\<lambda>i. monom P (coeff P p i) i)"
   968   fix k
   969   from R have RR: "!!i. (if i = k then coeff P p i else \<zero>) \<in> carrier R"
   970     by simp
   971   show "coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..deg R p}. ?s i) k = coeff P p k"
   972   proof (cases "k <= deg R p")
   973     case True
   974     hence "coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..deg R p}. ?s i) k =
   975           coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..k} \<union> {k<..deg R p}. ?s i) k"
   976       by (simp only: ivl_disj_un_one)
   977     also from True
   978     have "... = coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..k}. ?s i) k"
   979       by (simp cong: R.finsum_cong add: R.finsum_Un_disjoint
   980         ivl_disj_int_one order_less_imp_not_eq2 coeff_finsum R RR Pi_def)
   981     also
   982     have "... = coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..<k} \<union> {k}. ?s i) k"
   983       by (simp only: ivl_disj_un_singleton)
   984     also have "... = coeff P p k"
   985       by (simp cong: R.finsum_cong add: coeff_finsum deg_aboveD R RR Pi_def)
   986     finally show ?thesis .
   987   next
   988     case False
   989     hence "coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..deg R p}. ?s i) k =
   990           coeff P (\<Oplus>\<^bsub>P\<^esub> i \<in> {..<deg R p} \<union> {deg R p}. ?s i) k"
   991       by (simp only: ivl_disj_un_singleton)
   992     also from False have "... = coeff P p k"
   993       by (simp cong: R.finsum_cong add: coeff_finsum deg_aboveD R Pi_def)
   994     finally show ?thesis .
   995   qed
   996 qed (simp_all add: R Pi_def)
   997 
   998 lemma up_repr_le:
   999   "[| deg R p <= n; p \<in> carrier P |] ==>
  1000   (\<Oplus>\<^bsub>P\<^esub> i \<in> {..n}. monom P (coeff P p i) i) = p"
  1001 proof -
  1002   let ?s = "(\<lambda>i. monom P (coeff P p i) i)"
  1003   assume R: "p \<in> carrier P" and "deg R p <= n"
  1004   then have "finsum P ?s {..n} = finsum P ?s ({..deg R p} \<union> {deg R p<..n})"
  1005     by (simp only: ivl_disj_un_one)
  1006   also have "... = finsum P ?s {..deg R p}"
  1007     by (simp cong: P.finsum_cong add: P.finsum_Un_disjoint ivl_disj_int_one
  1008       deg_aboveD R Pi_def)
  1009   also have "... = p" using R by (rule up_repr)
  1010   finally show ?thesis .
  1011 qed
  1012 
  1013 end
  1014 
  1015 
  1016 subsection \<open>Polynomials over Integral Domains\<close>
  1017 
  1018 lemma domainI:
  1019   assumes cring: "cring R"
  1020     and one_not_zero: "one R ~= zero R"
  1021     and integral: "!!a b. [| mult R a b = zero R; a \<in> carrier R;
  1022       b \<in> carrier R |] ==> a = zero R | b = zero R"
  1023   shows "domain R"
  1024   by (auto intro!: domain.intro domain_axioms.intro cring.axioms assms
  1025     del: disjCI)
  1026 
  1027 context UP_domain
  1028 begin
  1029 
  1030 lemma UP_one_not_zero:
  1031   "\<one>\<^bsub>P\<^esub> ~= \<zero>\<^bsub>P\<^esub>"
  1032 proof
  1033   assume "\<one>\<^bsub>P\<^esub> = \<zero>\<^bsub>P\<^esub>"
  1034   hence "coeff P \<one>\<^bsub>P\<^esub> 0 = (coeff P \<zero>\<^bsub>P\<^esub> 0)" by simp
  1035   hence "\<one> = \<zero>" by simp
  1036   with R.one_not_zero show "False" by contradiction
  1037 qed
  1038 
  1039 lemma UP_integral:
  1040   "[| 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>"
  1041 proof -
  1042   fix p q
  1043   assume pq: "p \<otimes>\<^bsub>P\<^esub> q = \<zero>\<^bsub>P\<^esub>" and R: "p \<in> carrier P" "q \<in> carrier P"
  1044   show "p = \<zero>\<^bsub>P\<^esub> | q = \<zero>\<^bsub>P\<^esub>"
  1045   proof (rule classical)
  1046     assume c: "~ (p = \<zero>\<^bsub>P\<^esub> | q = \<zero>\<^bsub>P\<^esub>)"
  1047     with R have "deg R p + deg R q = deg R (p \<otimes>\<^bsub>P\<^esub> q)" by simp
  1048     also from pq have "... = 0" by simp
  1049     finally have "deg R p + deg R q = 0" .
  1050     then have f1: "deg R p = 0 & deg R q = 0" by simp
  1051     from f1 R have "p = (\<Oplus>\<^bsub>P\<^esub> i \<in> {..0}. monom P (coeff P p i) i)"
  1052       by (simp only: up_repr_le)
  1053     also from R have "... = monom P (coeff P p 0) 0" by simp
  1054     finally have p: "p = monom P (coeff P p 0) 0" .
  1055     from f1 R have "q = (\<Oplus>\<^bsub>P\<^esub> i \<in> {..0}. monom P (coeff P q i) i)"
  1056       by (simp only: up_repr_le)
  1057     also from R have "... = monom P (coeff P q 0) 0" by simp
  1058     finally have q: "q = monom P (coeff P q 0) 0" .
  1059     from R have "coeff P p 0 \<otimes> coeff P q 0 = coeff P (p \<otimes>\<^bsub>P\<^esub> q) 0" by simp
  1060     also from pq have "... = \<zero>" by simp
  1061     finally have "coeff P p 0 \<otimes> coeff P q 0 = \<zero>" .
  1062     with R have "coeff P p 0 = \<zero> | coeff P q 0 = \<zero>"
  1063       by (simp add: R.integral_iff)
  1064     with p q show "p = \<zero>\<^bsub>P\<^esub> | q = \<zero>\<^bsub>P\<^esub>" by fastforce
  1065   qed
  1066 qed
  1067 
  1068 theorem UP_domain:
  1069   "domain P"
  1070   by (auto intro!: domainI UP_cring UP_one_not_zero UP_integral del: disjCI)
  1071 
  1072 end
  1073 
  1074 text \<open>
  1075   Interpretation of theorems from @{term domain}.
  1076 \<close>
  1077 
  1078 sublocale UP_domain < "domain" P
  1079   by intro_locales (rule domain.axioms UP_domain)+
  1080 
  1081 
  1082 subsection \<open>The Evaluation Homomorphism and Universal Property\<close>
  1083 
  1084 (* alternative congruence rule (possibly more efficient)
  1085 lemma (in abelian_monoid) finsum_cong2:
  1086   "[| !!i. i \<in> A ==> f i \<in> carrier G = True; A = B;
  1087   !!i. i \<in> B ==> f i = g i |] ==> finsum G f A = finsum G g B"
  1088   sorry*)
  1089 
  1090 lemma (in abelian_monoid) boundD_carrier:
  1091   "[| bound \<zero> n f; n < m |] ==> f m \<in> carrier G"
  1092   by auto
  1093 
  1094 context ring
  1095 begin
  1096 
  1097 theorem diagonal_sum:
  1098   "[| f \<in> {..n + m::nat} \<rightarrow> carrier R; g \<in> {..n + m} \<rightarrow> carrier R |] ==>
  1099   (\<Oplus>k \<in> {..n + m}. \<Oplus>i \<in> {..k}. f i \<otimes> g (k - i)) =
  1100   (\<Oplus>k \<in> {..n + m}. \<Oplus>i \<in> {..n + m - k}. f k \<otimes> g i)"
  1101 proof -
  1102   assume Rf: "f \<in> {..n + m} \<rightarrow> carrier R" and Rg: "g \<in> {..n + m} \<rightarrow> carrier R"
  1103   {
  1104     fix j
  1105     have "j <= n + m ==>
  1106       (\<Oplus>k \<in> {..j}. \<Oplus>i \<in> {..k}. f i \<otimes> g (k - i)) =
  1107       (\<Oplus>k \<in> {..j}. \<Oplus>i \<in> {..j - k}. f k \<otimes> g i)"
  1108     proof (induct j)
  1109       case 0 from Rf Rg show ?case by (simp add: Pi_def)
  1110     next
  1111       case (Suc j)
  1112       have R6: "!!i k. [| k <= j; i <= Suc j - k |] ==> g i \<in> carrier R"
  1113         using Suc by (auto intro!: funcset_mem [OF Rg])
  1114       have R8: "!!i k. [| k <= Suc j; i <= k |] ==> g (k - i) \<in> carrier R"
  1115         using Suc by (auto intro!: funcset_mem [OF Rg])
  1116       have R9: "!!i k. [| k <= Suc j |] ==> f k \<in> carrier R"
  1117         using Suc by (auto intro!: funcset_mem [OF Rf])
  1118       have R10: "!!i k. [| k <= Suc j; i <= Suc j - k |] ==> g i \<in> carrier R"
  1119         using Suc by (auto intro!: funcset_mem [OF Rg])
  1120       have R11: "g 0 \<in> carrier R"
  1121         using Suc by (auto intro!: funcset_mem [OF Rg])
  1122       from Suc show ?case
  1123         by (simp cong: finsum_cong add: Suc_diff_le a_ac
  1124           Pi_def R6 R8 R9 R10 R11)
  1125     qed
  1126   }
  1127   then show ?thesis by fast
  1128 qed
  1129 
  1130 theorem cauchy_product:
  1131   assumes bf: "bound \<zero> n f" and bg: "bound \<zero> m g"
  1132     and Rf: "f \<in> {..n} \<rightarrow> carrier R" and Rg: "g \<in> {..m} \<rightarrow> carrier R"
  1133   shows "(\<Oplus>k \<in> {..n + m}. \<Oplus>i \<in> {..k}. f i \<otimes> g (k - i)) =
  1134     (\<Oplus>i \<in> {..n}. f i) \<otimes> (\<Oplus>i \<in> {..m}. g i)"      (* State reverse direction? *)
  1135 proof -
  1136   have f: "!!x. f x \<in> carrier R"
  1137   proof -
  1138     fix x
  1139     show "f x \<in> carrier R"
  1140       using Rf bf boundD_carrier by (cases "x <= n") (auto simp: Pi_def)
  1141   qed
  1142   have g: "!!x. g x \<in> carrier R"
  1143   proof -
  1144     fix x
  1145     show "g x \<in> carrier R"
  1146       using Rg bg boundD_carrier by (cases "x <= m") (auto simp: Pi_def)
  1147   qed
  1148   from f g have "(\<Oplus>k \<in> {..n + m}. \<Oplus>i \<in> {..k}. f i \<otimes> g (k - i)) =
  1149       (\<Oplus>k \<in> {..n + m}. \<Oplus>i \<in> {..n + m - k}. f k \<otimes> g i)"
  1150     by (simp add: diagonal_sum Pi_def)
  1151   also have "... = (\<Oplus>k \<in> {..n} \<union> {n<..n + m}. \<Oplus>i \<in> {..n + m - k}. f k \<otimes> g i)"
  1152     by (simp only: ivl_disj_un_one)
  1153   also from f g have "... = (\<Oplus>k \<in> {..n}. \<Oplus>i \<in> {..n + m - k}. f k \<otimes> g i)"
  1154     by (simp cong: finsum_cong
  1155       add: bound.bound [OF bf] finsum_Un_disjoint ivl_disj_int_one Pi_def)
  1156   also from f g
  1157   have "... = (\<Oplus>k \<in> {..n}. \<Oplus>i \<in> {..m} \<union> {m<..n + m - k}. f k \<otimes> g i)"
  1158     by (simp cong: finsum_cong add: ivl_disj_un_one le_add_diff Pi_def)
  1159   also from f g have "... = (\<Oplus>k \<in> {..n}. \<Oplus>i \<in> {..m}. f k \<otimes> g i)"
  1160     by (simp cong: finsum_cong
  1161       add: bound.bound [OF bg] finsum_Un_disjoint ivl_disj_int_one Pi_def)
  1162   also from f g have "... = (\<Oplus>i \<in> {..n}. f i) \<otimes> (\<Oplus>i \<in> {..m}. g i)"
  1163     by (simp add: finsum_ldistr diagonal_sum Pi_def,
  1164       simp cong: finsum_cong add: finsum_rdistr Pi_def)
  1165   finally show ?thesis .
  1166 qed
  1167 
  1168 end
  1169 
  1170 lemma (in UP_ring) const_ring_hom:
  1171   "(\<lambda>a. monom P a 0) \<in> ring_hom R P"
  1172   by (auto intro!: ring_hom_memI intro: up_eqI simp: monom_mult_is_smult)
  1173 
  1174 definition
  1175   eval :: "[('a, 'm) ring_scheme, ('b, 'n) ring_scheme,
  1176            'a => 'b, 'b, nat => 'a] => 'b"
  1177   where "eval R S phi s = (\<lambda>p \<in> carrier (UP R).
  1178     \<Oplus>\<^bsub>S\<^esub>i \<in> {..deg R p}. phi (coeff (UP R) p i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1179 
  1180 context UP
  1181 begin
  1182 
  1183 lemma eval_on_carrier:
  1184   fixes S (structure)
  1185   shows "p \<in> carrier P ==>
  1186   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)"
  1187   by (unfold eval_def, fold P_def) simp
  1188 
  1189 lemma eval_extensional:
  1190   "eval R S phi p \<in> extensional (carrier P)"
  1191   by (unfold eval_def, fold P_def) simp
  1192 
  1193 end
  1194 
  1195 text \<open>The universal property of the polynomial ring\<close>
  1196 
  1197 locale UP_pre_univ_prop = ring_hom_cring + UP_cring
  1198 
  1199 locale UP_univ_prop = UP_pre_univ_prop +
  1200   fixes s and Eval
  1201   assumes indet_img_carrier [simp, intro]: "s \<in> carrier S"
  1202   defines Eval_def: "Eval == eval R S h s"
  1203 
  1204 text\<open>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}.\<close>
  1205 text\<open>JE: I was considering using it in \<open>eval_ring_hom\<close>, but that property does not hold for non commutative rings, so
  1206   maybe it is not that necessary.\<close>
  1207 
  1208 lemma (in ring_hom_ring) hom_finsum [simp]:
  1209   "f \<in> A \<rightarrow> carrier R ==>
  1210   h (finsum R f A) = finsum S (h o f) A"
  1211   by (induct A rule: infinite_finite_induct, auto simp: Pi_def)
  1212 
  1213 context UP_pre_univ_prop
  1214 begin
  1215 
  1216 theorem eval_ring_hom:
  1217   assumes S: "s \<in> carrier S"
  1218   shows "eval R S h s \<in> ring_hom P S"
  1219 proof (rule ring_hom_memI)
  1220   fix p
  1221   assume R: "p \<in> carrier P"
  1222   then show "eval R S h s p \<in> carrier S"
  1223     by (simp only: eval_on_carrier) (simp add: S Pi_def)
  1224 next
  1225   fix p q
  1226   assume R: "p \<in> carrier P" "q \<in> carrier P"
  1227   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"
  1228   proof (simp only: eval_on_carrier P.a_closed)
  1229     from S R have
  1230       "(\<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) =
  1231       (\<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)}.
  1232         h (coeff P (p \<oplus>\<^bsub>P\<^esub> q) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1233       by (simp cong: S.finsum_cong
  1234         add: deg_aboveD S.finsum_Un_disjoint ivl_disj_int_one Pi_def del: coeff_add)
  1235     also from R have "... =
  1236         (\<Oplus>\<^bsub>S\<^esub> i \<in> {..max (deg R p) (deg R q)}.
  1237           h (coeff P (p \<oplus>\<^bsub>P\<^esub> q) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1238       by (simp add: ivl_disj_un_one)
  1239     also from R S have "... =
  1240       (\<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>
  1241       (\<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)"
  1242       by (simp cong: S.finsum_cong
  1243         add: S.l_distr deg_aboveD ivl_disj_int_one Pi_def)
  1244     also have "... =
  1245         (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R p} \<union> {deg R p<..max (deg R p) (deg R q)}.
  1246           h (coeff P p i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) \<oplus>\<^bsub>S\<^esub>
  1247         (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R q} \<union> {deg R q<..max (deg R p) (deg R q)}.
  1248           h (coeff P q i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1249       by (simp only: ivl_disj_un_one max.cobounded1 max.cobounded2)
  1250     also from R S have "... =
  1251       (\<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>
  1252       (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R q}. h (coeff P q i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1253       by (simp cong: S.finsum_cong
  1254         add: deg_aboveD S.finsum_Un_disjoint ivl_disj_int_one Pi_def)
  1255     finally show
  1256       "(\<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) =
  1257       (\<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>
  1258       (\<Oplus>\<^bsub>S\<^esub>i \<in> {..deg R q}. h (coeff P q i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)" .
  1259   qed
  1260 next
  1261   show "eval R S h s \<one>\<^bsub>P\<^esub> = \<one>\<^bsub>S\<^esub>"
  1262     by (simp only: eval_on_carrier UP_one_closed) simp
  1263 next
  1264   fix p q
  1265   assume R: "p \<in> carrier P" "q \<in> carrier P"
  1266   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"
  1267   proof (simp only: eval_on_carrier UP_mult_closed)
  1268     from R S have
  1269       "(\<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) =
  1270       (\<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}.
  1271         h (coeff P (p \<otimes>\<^bsub>P\<^esub> 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         del: coeff_mult)
  1275     also from R have "... =
  1276       (\<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)"
  1277       by (simp only: ivl_disj_un_one deg_mult_ring)
  1278     also from R S have "... =
  1279       (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R p + deg R q}.
  1280          \<Oplus>\<^bsub>S\<^esub> k \<in> {..i}.
  1281            h (coeff P p k) \<otimes>\<^bsub>S\<^esub> h (coeff P q (i - k)) \<otimes>\<^bsub>S\<^esub>
  1282            (s (^)\<^bsub>S\<^esub> k \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> (i - k)))"
  1283       by (simp cong: S.finsum_cong add: S.nat_pow_mult Pi_def
  1284         S.m_ac S.finsum_rdistr)
  1285     also from R S have "... =
  1286       (\<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>
  1287       (\<Oplus>\<^bsub>S\<^esub> i\<in>{..deg R q}. h (coeff P q i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1288       by (simp add: S.cauchy_product [THEN sym] bound.intro deg_aboveD S.m_ac
  1289         Pi_def)
  1290     finally show
  1291       "(\<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) =
  1292       (\<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>
  1293       (\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R q}. h (coeff P q i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)" .
  1294   qed
  1295 qed
  1296 
  1297 text \<open>
  1298   The following lemma could be proved in \<open>UP_cring\<close> with the additional
  1299   assumption that \<open>h\<close> is closed.\<close>
  1300 
  1301 lemma (in UP_pre_univ_prop) eval_const:
  1302   "[| s \<in> carrier S; r \<in> carrier R |] ==> eval R S h s (monom P r 0) = h r"
  1303   by (simp only: eval_on_carrier monom_closed) simp
  1304 
  1305 text \<open>Further properties of the evaluation homomorphism.\<close>
  1306 
  1307 text \<open>The following proof is complicated by the fact that in arbitrary
  1308   rings one might have @{term "one R = zero R"}.\<close>
  1309 
  1310 (* TODO: simplify by cases "one R = zero R" *)
  1311 
  1312 lemma (in UP_pre_univ_prop) eval_monom1:
  1313   assumes S: "s \<in> carrier S"
  1314   shows "eval R S h s (monom P \<one> 1) = s"
  1315 proof (simp only: eval_on_carrier monom_closed R.one_closed)
  1316    from S have
  1317     "(\<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) =
  1318     (\<Oplus>\<^bsub>S\<^esub> i\<in>{..deg R (monom P \<one> 1)} \<union> {deg R (monom P \<one> 1)<..1}.
  1319       h (coeff P (monom P \<one> 1) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1320     by (simp cong: S.finsum_cong del: coeff_monom
  1321       add: deg_aboveD S.finsum_Un_disjoint ivl_disj_int_one Pi_def)
  1322   also have "... =
  1323     (\<Oplus>\<^bsub>S\<^esub> i \<in> {..1}. h (coeff P (monom P \<one> 1) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i)"
  1324     by (simp only: ivl_disj_un_one deg_monom_le R.one_closed)
  1325   also have "... = s"
  1326   proof (cases "s = \<zero>\<^bsub>S\<^esub>")
  1327     case True then show ?thesis by (simp add: Pi_def)
  1328   next
  1329     case False then show ?thesis by (simp add: S Pi_def)
  1330   qed
  1331   finally show "(\<Oplus>\<^bsub>S\<^esub> i \<in> {..deg R (monom P \<one> 1)}.
  1332     h (coeff P (monom P \<one> 1) i) \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> i) = s" .
  1333 qed
  1334 
  1335 end
  1336 
  1337 text \<open>Interpretation of ring homomorphism lemmas.\<close>
  1338 
  1339 sublocale UP_univ_prop < ring_hom_cring P S Eval
  1340   unfolding Eval_def
  1341   by unfold_locales (fast intro: eval_ring_hom)
  1342 
  1343 lemma (in UP_cring) monom_pow:
  1344   assumes R: "a \<in> carrier R"
  1345   shows "(monom P a n) (^)\<^bsub>P\<^esub> m = monom P (a (^) m) (n * m)"
  1346 proof (induct m)
  1347   case 0 from R show ?case by simp
  1348 next
  1349   case Suc with R show ?case
  1350     by (simp del: monom_mult add: monom_mult [THEN sym] add.commute)
  1351 qed
  1352 
  1353 lemma (in ring_hom_cring) hom_pow [simp]:
  1354   "x \<in> carrier R ==> h (x (^) n) = h x (^)\<^bsub>S\<^esub> (n::nat)"
  1355   by (induct n) simp_all
  1356 
  1357 lemma (in UP_univ_prop) Eval_monom:
  1358   "r \<in> carrier R ==> Eval (monom P r n) = h r \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> n"
  1359 proof -
  1360   assume R: "r \<in> carrier R"
  1361   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)"
  1362     by (simp del: monom_mult add: monom_mult [THEN sym] monom_pow)
  1363   also
  1364   from R eval_monom1 [where s = s, folded Eval_def]
  1365   have "... = h r \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> n"
  1366     by (simp add: eval_const [where s = s, folded Eval_def])
  1367   finally show ?thesis .
  1368 qed
  1369 
  1370 lemma (in UP_pre_univ_prop) eval_monom:
  1371   assumes R: "r \<in> carrier R" and S: "s \<in> carrier S"
  1372   shows "eval R S h s (monom P r n) = h r \<otimes>\<^bsub>S\<^esub> s (^)\<^bsub>S\<^esub> n"
  1373 proof -
  1374   interpret UP_univ_prop R S h P s "eval R S h s"
  1375     using UP_pre_univ_prop_axioms P_def R S
  1376     by (auto intro: UP_univ_prop.intro UP_univ_prop_axioms.intro)
  1377   from R
  1378   show ?thesis by (rule Eval_monom)
  1379 qed
  1380 
  1381 lemma (in UP_univ_prop) Eval_smult:
  1382   "[| r \<in> carrier R; p \<in> carrier P |] ==> Eval (r \<odot>\<^bsub>P\<^esub> p) = h r \<otimes>\<^bsub>S\<^esub> Eval p"
  1383 proof -
  1384   assume R: "r \<in> carrier R" and P: "p \<in> carrier P"
  1385   then show ?thesis
  1386     by (simp add: monom_mult_is_smult [THEN sym]
  1387       eval_const [where s = s, folded Eval_def])
  1388 qed
  1389 
  1390 lemma ring_hom_cringI:
  1391   assumes "cring R"
  1392     and "cring S"
  1393     and "h \<in> ring_hom R S"
  1394   shows "ring_hom_cring R S h"
  1395   by (fast intro: ring_hom_cring.intro ring_hom_cring_axioms.intro
  1396     cring.axioms assms)
  1397 
  1398 context UP_pre_univ_prop
  1399 begin
  1400 
  1401 lemma UP_hom_unique:
  1402   assumes "ring_hom_cring P S Phi"
  1403   assumes Phi: "Phi (monom P \<one> (Suc 0)) = s"
  1404       "!!r. r \<in> carrier R ==> Phi (monom P r 0) = h r"
  1405   assumes "ring_hom_cring P S Psi"
  1406   assumes Psi: "Psi (monom P \<one> (Suc 0)) = s"
  1407       "!!r. r \<in> carrier R ==> Psi (monom P r 0) = h r"
  1408     and P: "p \<in> carrier P" and S: "s \<in> carrier S"
  1409   shows "Phi p = Psi p"
  1410 proof -
  1411   interpret ring_hom_cring P S Phi by fact
  1412   interpret ring_hom_cring P S Psi by fact
  1413   have "Phi p =
  1414       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)"
  1415     by (simp add: up_repr P monom_mult [THEN sym] monom_pow del: monom_mult)
  1416   also
  1417   have "... =
  1418       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)"
  1419     by (simp add: Phi Psi P Pi_def comp_def)
  1420   also have "... = Psi p"
  1421     by (simp add: up_repr P monom_mult [THEN sym] monom_pow del: monom_mult)
  1422   finally show ?thesis .
  1423 qed
  1424 
  1425 lemma ring_homD:
  1426   assumes Phi: "Phi \<in> ring_hom P S"
  1427   shows "ring_hom_cring P S Phi"
  1428   by unfold_locales (rule Phi)
  1429 
  1430 theorem UP_universal_property:
  1431   assumes S: "s \<in> carrier S"
  1432   shows "\<exists>!Phi. Phi \<in> ring_hom P S \<inter> extensional (carrier P) &
  1433     Phi (monom P \<one> 1) = s &
  1434     (ALL r : carrier R. Phi (monom P r 0) = h r)"
  1435   using S eval_monom1
  1436   apply (auto intro: eval_ring_hom eval_const eval_extensional)
  1437   apply (rule extensionalityI)
  1438   apply (auto intro: UP_hom_unique ring_homD)
  1439   done
  1440 
  1441 end
  1442 
  1443 text\<open>JE: The following lemma was added by me; it might be even lifted to a simpler locale\<close>
  1444 
  1445 context monoid
  1446 begin
  1447 
  1448 lemma nat_pow_eone[simp]: assumes x_in_G: "x \<in> carrier G" shows "x (^) (1::nat) = x"
  1449   using nat_pow_Suc [of x 0] unfolding nat_pow_0 [of x] unfolding l_one [OF x_in_G] by simp
  1450 
  1451 end
  1452 
  1453 context UP_ring
  1454 begin
  1455 
  1456 abbreviation lcoeff :: "(nat =>'a) => 'a" where "lcoeff p == coeff P p (deg R p)"
  1457 
  1458 lemma lcoeff_nonzero2: assumes p_in_R: "p \<in> carrier P" and p_not_zero: "p \<noteq> \<zero>\<^bsub>P\<^esub>" shows "lcoeff p \<noteq> \<zero>"
  1459   using lcoeff_nonzero [OF p_not_zero p_in_R] .
  1460 
  1461 
  1462 subsection\<open>The long division algorithm: some previous facts.\<close>
  1463 
  1464 lemma coeff_minus [simp]:
  1465   assumes p: "p \<in> carrier P" and q: "q \<in> carrier P" shows "coeff P (p \<ominus>\<^bsub>P\<^esub> q) n = coeff P p n \<ominus> coeff P q n"
  1466   unfolding a_minus_def [OF p q] unfolding coeff_add [OF p a_inv_closed [OF q]] unfolding coeff_a_inv [OF q]
  1467   using coeff_closed [OF p, of n] using coeff_closed [OF q, of n] by algebra
  1468 
  1469 lemma lcoeff_closed [simp]: assumes p: "p \<in> carrier P" shows "lcoeff p \<in> carrier R"
  1470   using coeff_closed [OF p, of "deg R p"] by simp
  1471 
  1472 lemma deg_smult_decr: assumes a_in_R: "a \<in> carrier R" and f_in_P: "f \<in> carrier P" shows "deg R (a \<odot>\<^bsub>P\<^esub> f) \<le> deg R f"
  1473   using deg_smult_ring [OF a_in_R f_in_P] by (cases "a = \<zero>", auto)
  1474 
  1475 lemma coeff_monom_mult: assumes R: "c \<in> carrier R" and P: "p \<in> carrier P"
  1476   shows "coeff P (monom P c n \<otimes>\<^bsub>P\<^esub> p) (m + n) = c \<otimes> (coeff P p m)"
  1477 proof -
  1478   have "coeff P (monom P c n \<otimes>\<^bsub>P\<^esub> p) (m + n) = (\<Oplus>i\<in>{..m + n}. (if n = i then c else \<zero>) \<otimes> coeff P p (m + n - i))"
  1479     unfolding coeff_mult [OF monom_closed [OF R, of n] P, of "m + n"] unfolding coeff_monom [OF R, of n] by simp
  1480   also have "(\<Oplus>i\<in>{..m + n}. (if n = i then c else \<zero>) \<otimes> coeff P p (m + n - i)) =
  1481     (\<Oplus>i\<in>{..m + n}. (if n = i then c \<otimes> coeff P p (m + n - i) else \<zero>))"
  1482     using  R.finsum_cong [of "{..m + n}" "{..m + n}" "(\<lambda>i::nat. (if n = i then c else \<zero>) \<otimes> coeff P p (m + n - i))"
  1483       "(\<lambda>i::nat. (if n = i then c \<otimes> coeff P p (m + n - i) else \<zero>))"]
  1484     using coeff_closed [OF P] unfolding Pi_def simp_implies_def using R by auto
  1485   also have "\<dots> = c \<otimes> coeff P p m" using R.finsum_singleton [of n "{..m + n}" "(\<lambda>i. c \<otimes> coeff P p (m + n - i))"]
  1486     unfolding Pi_def using coeff_closed [OF P] using P R by auto
  1487   finally show ?thesis by simp
  1488 qed
  1489 
  1490 lemma deg_lcoeff_cancel:
  1491   assumes p_in_P: "p \<in> carrier P" and q_in_P: "q \<in> carrier P" and r_in_P: "r \<in> carrier P"
  1492   and deg_r_nonzero: "deg R r \<noteq> 0"
  1493   and deg_R_p: "deg R p \<le> deg R r" and deg_R_q: "deg R q \<le> deg R r"
  1494   and coeff_R_p_eq_q: "coeff P p (deg R r) = \<ominus>\<^bsub>R\<^esub> (coeff P q (deg R r))"
  1495   shows "deg R (p \<oplus>\<^bsub>P\<^esub> q) < deg R r"
  1496 proof -
  1497   have deg_le: "deg R (p \<oplus>\<^bsub>P\<^esub> q) \<le> deg R r"
  1498   proof (rule deg_aboveI)
  1499     fix m
  1500     assume deg_r_le: "deg R r < m"
  1501     show "coeff P (p \<oplus>\<^bsub>P\<^esub> q) m = \<zero>"
  1502     proof -
  1503       have slp: "deg R p < m" and "deg R q < m" using deg_R_p deg_R_q using deg_r_le by auto
  1504       then have max_sl: "max (deg R p) (deg R q) < m" by simp
  1505       then have "deg R (p \<oplus>\<^bsub>P\<^esub> q) < m" using deg_add [OF p_in_P q_in_P] by arith
  1506       with deg_R_p deg_R_q show ?thesis using coeff_add [OF p_in_P q_in_P, of m]
  1507         using deg_aboveD [of "p \<oplus>\<^bsub>P\<^esub> q" m] using p_in_P q_in_P by simp
  1508     qed
  1509   qed (simp add: p_in_P q_in_P)
  1510   moreover have deg_ne: "deg R (p \<oplus>\<^bsub>P\<^esub> q) \<noteq> deg R r"
  1511   proof (rule ccontr)
  1512     assume nz: "\<not> deg R (p \<oplus>\<^bsub>P\<^esub> q) \<noteq> deg R r" then have deg_eq: "deg R (p \<oplus>\<^bsub>P\<^esub> q) = deg R r" by simp
  1513     from deg_r_nonzero have r_nonzero: "r \<noteq> \<zero>\<^bsub>P\<^esub>" by (cases "r = \<zero>\<^bsub>P\<^esub>", simp_all)
  1514     have "coeff P (p \<oplus>\<^bsub>P\<^esub> q) (deg R r) = \<zero>\<^bsub>R\<^esub>" using coeff_add [OF p_in_P q_in_P, of "deg R r"] using coeff_R_p_eq_q
  1515       using coeff_closed [OF p_in_P, of "deg R r"] coeff_closed [OF q_in_P, of "deg R r"] by algebra
  1516     with lcoeff_nonzero [OF r_nonzero r_in_P]  and deg_eq show False using lcoeff_nonzero [of "p \<oplus>\<^bsub>P\<^esub> q"] using p_in_P q_in_P
  1517       using deg_r_nonzero by (cases "p \<oplus>\<^bsub>P\<^esub> q \<noteq> \<zero>\<^bsub>P\<^esub>", auto)
  1518   qed
  1519   ultimately show ?thesis by simp
  1520 qed
  1521 
  1522 lemma monom_deg_mult:
  1523   assumes f_in_P: "f \<in> carrier P" and g_in_P: "g \<in> carrier P" and deg_le: "deg R g \<le> deg R f"
  1524   and a_in_R: "a \<in> carrier R"
  1525   shows "deg R (g \<otimes>\<^bsub>P\<^esub> monom P a (deg R f - deg R g)) \<le> deg R f"
  1526   using deg_mult_ring [OF g_in_P monom_closed [OF a_in_R, of "deg R f - deg R g"]]
  1527   apply (cases "a = \<zero>") using g_in_P apply simp
  1528   using deg_monom [OF _ a_in_R, of "deg R f - deg R g"] using deg_le by simp
  1529 
  1530 lemma deg_zero_impl_monom:
  1531   assumes f_in_P: "f \<in> carrier P" and deg_f: "deg R f = 0"
  1532   shows "f = monom P (coeff P f 0) 0"
  1533   apply (rule up_eqI) using coeff_monom [OF coeff_closed [OF f_in_P], of 0 0]
  1534   using f_in_P deg_f using deg_aboveD [of f _] by auto
  1535 
  1536 end
  1537 
  1538 
  1539 subsection \<open>The long division proof for commutative rings\<close>
  1540 
  1541 context UP_cring
  1542 begin
  1543 
  1544 lemma exI3: assumes exist: "Pred x y z"
  1545   shows "\<exists> x y z. Pred x y z"
  1546   using exist by blast
  1547 
  1548 text \<open>Jacobson's Theorem 2.14\<close>
  1549 
  1550 lemma long_div_theorem:
  1551   assumes g_in_P [simp]: "g \<in> carrier P" and f_in_P [simp]: "f \<in> carrier P"
  1552   and g_not_zero: "g \<noteq> \<zero>\<^bsub>P\<^esub>"
  1553   shows "\<exists> q r (k::nat). (q \<in> carrier P) \<and> (r \<in> carrier P) \<and> (lcoeff g)(^)\<^bsub>R\<^esub>k \<odot>\<^bsub>P\<^esub> f = g \<otimes>\<^bsub>P\<^esub> q \<oplus>\<^bsub>P\<^esub> r \<and> (r = \<zero>\<^bsub>P\<^esub> | deg R r < deg R g)"
  1554   using f_in_P
  1555 proof (induct "deg R f" arbitrary: "f" rule: nat_less_induct)
  1556   case (1 f)
  1557   note f_in_P [simp] = "1.prems"
  1558   let ?pred = "(\<lambda> q r (k::nat).
  1559     (q \<in> carrier P) \<and> (r \<in> carrier P)
  1560     \<and> (lcoeff g)(^)\<^bsub>R\<^esub>k \<odot>\<^bsub>P\<^esub> f = g \<otimes>\<^bsub>P\<^esub> q \<oplus>\<^bsub>P\<^esub> r \<and> (r = \<zero>\<^bsub>P\<^esub> | deg R r < deg R g))"
  1561   let ?lg = "lcoeff g" and ?lf = "lcoeff f"
  1562   show ?case
  1563   proof (cases "deg R f < deg R g")
  1564     case True
  1565     have "?pred \<zero>\<^bsub>P\<^esub> f 0" using True by force
  1566     then show ?thesis by blast
  1567   next
  1568     case False then have deg_g_le_deg_f: "deg R g \<le> deg R f" by simp
  1569     {
  1570       let ?k = "1::nat"
  1571       let ?f1 = "(g \<otimes>\<^bsub>P\<^esub> (monom P (?lf) (deg R f - deg R g))) \<oplus>\<^bsub>P\<^esub> \<ominus>\<^bsub>P\<^esub> (?lg \<odot>\<^bsub>P\<^esub> f)"
  1572       let ?q = "monom P (?lf) (deg R f - deg R g)"
  1573       have f1_in_carrier: "?f1 \<in> carrier P" and q_in_carrier: "?q \<in> carrier P" by simp_all
  1574       show ?thesis
  1575       proof (cases "deg R f = 0")
  1576         case True
  1577         {
  1578           have deg_g: "deg R g = 0" using True using deg_g_le_deg_f by simp
  1579           have "?pred f \<zero>\<^bsub>P\<^esub> 1"
  1580             using deg_zero_impl_monom [OF g_in_P deg_g]
  1581             using sym [OF monom_mult_is_smult [OF coeff_closed [OF g_in_P, of 0] f_in_P]]
  1582             using deg_g by simp
  1583           then show ?thesis by blast
  1584         }
  1585       next
  1586         case False note deg_f_nzero = False
  1587         {
  1588           have exist: "lcoeff g (^) ?k \<odot>\<^bsub>P\<^esub> f = g \<otimes>\<^bsub>P\<^esub> ?q \<oplus>\<^bsub>P\<^esub> \<ominus>\<^bsub>P\<^esub> ?f1"
  1589             by (simp add: minus_add r_neg sym [
  1590               OF a_assoc [of "g \<otimes>\<^bsub>P\<^esub> ?q" "\<ominus>\<^bsub>P\<^esub> (g \<otimes>\<^bsub>P\<^esub> ?q)" "lcoeff g \<odot>\<^bsub>P\<^esub> f"]])
  1591           have deg_remainder_l_f: "deg R (\<ominus>\<^bsub>P\<^esub> ?f1) < deg R f"
  1592           proof (unfold deg_uminus [OF f1_in_carrier])
  1593             show "deg R ?f1 < deg R f"
  1594             proof (rule deg_lcoeff_cancel)
  1595               show "deg R (\<ominus>\<^bsub>P\<^esub> (?lg \<odot>\<^bsub>P\<^esub> f)) \<le> deg R f"
  1596                 using deg_smult_ring [of ?lg f]
  1597                 using lcoeff_nonzero2 [OF g_in_P g_not_zero] by simp
  1598               show "deg R (g \<otimes>\<^bsub>P\<^esub> ?q) \<le> deg R f"
  1599                 by (simp add: monom_deg_mult [OF f_in_P g_in_P deg_g_le_deg_f, of ?lf])
  1600               show "coeff P (g \<otimes>\<^bsub>P\<^esub> ?q) (deg R f) = \<ominus> coeff P (\<ominus>\<^bsub>P\<^esub> (?lg \<odot>\<^bsub>P\<^esub> f)) (deg R f)"
  1601                 unfolding coeff_mult [OF g_in_P monom_closed
  1602                   [OF lcoeff_closed [OF f_in_P],
  1603                     of "deg R f - deg R g"], of "deg R f"]
  1604                 unfolding coeff_monom [OF lcoeff_closed
  1605                   [OF f_in_P], of "(deg R f - deg R g)"]
  1606                 using R.finsum_cong' [of "{..deg R f}" "{..deg R f}"
  1607                   "(\<lambda>i. coeff P g i \<otimes> (if deg R f - deg R g = deg R f - i then ?lf else \<zero>))"
  1608                   "(\<lambda>i. if deg R g = i then coeff P g i \<otimes> ?lf else \<zero>)"]
  1609                 using R.finsum_singleton [of "deg R g" "{.. deg R f}" "(\<lambda>i. coeff P g i \<otimes> ?lf)"]
  1610                 unfolding Pi_def using deg_g_le_deg_f by force
  1611             qed (simp_all add: deg_f_nzero)
  1612           qed
  1613           then obtain q' r' k'
  1614             where rem_desc: "?lg (^) (k'::nat) \<odot>\<^bsub>P\<^esub> (\<ominus>\<^bsub>P\<^esub> ?f1) = g \<otimes>\<^bsub>P\<^esub> q' \<oplus>\<^bsub>P\<^esub> r'"
  1615             and rem_deg: "(r' = \<zero>\<^bsub>P\<^esub> \<or> deg R r' < deg R g)"
  1616             and q'_in_carrier: "q' \<in> carrier P" and r'_in_carrier: "r' \<in> carrier P"
  1617             using "1.hyps" using f1_in_carrier by blast
  1618           show ?thesis
  1619           proof (rule exI3 [of _ "((?lg (^) k') \<odot>\<^bsub>P\<^esub> ?q \<oplus>\<^bsub>P\<^esub> q')" r' "Suc k'"], intro conjI)
  1620             show "(?lg (^) (Suc k')) \<odot>\<^bsub>P\<^esub> f = g \<otimes>\<^bsub>P\<^esub> ((?lg (^) k') \<odot>\<^bsub>P\<^esub> ?q \<oplus>\<^bsub>P\<^esub> q') \<oplus>\<^bsub>P\<^esub> r'"
  1621             proof -
  1622               have "(?lg (^) (Suc k')) \<odot>\<^bsub>P\<^esub> f = (?lg (^) k') \<odot>\<^bsub>P\<^esub> (g \<otimes>\<^bsub>P\<^esub> ?q \<oplus>\<^bsub>P\<^esub> \<ominus>\<^bsub>P\<^esub> ?f1)"
  1623                 using smult_assoc1 [OF _ _ f_in_P] using exist by simp
  1624               also have "\<dots> = (?lg (^) k') \<odot>\<^bsub>P\<^esub> (g \<otimes>\<^bsub>P\<^esub> ?q) \<oplus>\<^bsub>P\<^esub> ((?lg (^) k') \<odot>\<^bsub>P\<^esub> ( \<ominus>\<^bsub>P\<^esub> ?f1))"
  1625                 using UP_smult_r_distr by simp
  1626               also have "\<dots> = (?lg (^) k') \<odot>\<^bsub>P\<^esub> (g \<otimes>\<^bsub>P\<^esub> ?q) \<oplus>\<^bsub>P\<^esub> (g \<otimes>\<^bsub>P\<^esub> q' \<oplus>\<^bsub>P\<^esub> r')"
  1627                 unfolding rem_desc ..
  1628               also have "\<dots> = (?lg (^) k') \<odot>\<^bsub>P\<^esub> (g \<otimes>\<^bsub>P\<^esub> ?q) \<oplus>\<^bsub>P\<^esub> g \<otimes>\<^bsub>P\<^esub> q' \<oplus>\<^bsub>P\<^esub> r'"
  1629                 using sym [OF a_assoc [of "?lg (^) k' \<odot>\<^bsub>P\<^esub> (g \<otimes>\<^bsub>P\<^esub> ?q)" "g \<otimes>\<^bsub>P\<^esub> q'" "r'"]]
  1630                 using r'_in_carrier q'_in_carrier by simp
  1631               also have "\<dots> = (?lg (^) k') \<odot>\<^bsub>P\<^esub> (?q \<otimes>\<^bsub>P\<^esub> g) \<oplus>\<^bsub>P\<^esub> q' \<otimes>\<^bsub>P\<^esub> g \<oplus>\<^bsub>P\<^esub> r'"
  1632                 using q'_in_carrier by (auto simp add: m_comm)
  1633               also have "\<dots> = (((?lg (^) k') \<odot>\<^bsub>P\<^esub> ?q) \<otimes>\<^bsub>P\<^esub> g) \<oplus>\<^bsub>P\<^esub> q' \<otimes>\<^bsub>P\<^esub> g \<oplus>\<^bsub>P\<^esub> r'"
  1634                 using smult_assoc2 q'_in_carrier "1.prems" by auto
  1635               also have "\<dots> = ((?lg (^) k') \<odot>\<^bsub>P\<^esub> ?q \<oplus>\<^bsub>P\<^esub> q') \<otimes>\<^bsub>P\<^esub> g \<oplus>\<^bsub>P\<^esub> r'"
  1636                 using sym [OF l_distr] and q'_in_carrier by auto
  1637               finally show ?thesis using m_comm q'_in_carrier by auto
  1638             qed
  1639           qed (simp_all add: rem_deg q'_in_carrier r'_in_carrier)
  1640         }
  1641       qed
  1642     }
  1643   qed
  1644 qed
  1645 
  1646 end
  1647 
  1648 
  1649 text \<open>The remainder theorem as corollary of the long division theorem.\<close>
  1650 
  1651 context UP_cring
  1652 begin
  1653 
  1654 lemma deg_minus_monom:
  1655   assumes a: "a \<in> carrier R"
  1656   and R_not_trivial: "(carrier R \<noteq> {\<zero>})"
  1657   shows "deg R (monom P \<one>\<^bsub>R\<^esub> 1 \<ominus>\<^bsub>P\<^esub> monom P a 0) = 1"
  1658   (is "deg R ?g = 1")
  1659 proof -
  1660   have "deg R ?g \<le> 1"
  1661   proof (rule deg_aboveI)
  1662     fix m
  1663     assume "(1::nat) < m"
  1664     then show "coeff P ?g m = \<zero>"
  1665       using coeff_minus using a by auto algebra
  1666   qed (simp add: a)
  1667   moreover have "deg R ?g \<ge> 1"
  1668   proof (rule deg_belowI)
  1669     show "coeff P ?g 1 \<noteq> \<zero>"
  1670       using a using R.carrier_one_not_zero R_not_trivial by simp algebra
  1671   qed (simp add: a)
  1672   ultimately show ?thesis by simp
  1673 qed
  1674 
  1675 lemma lcoeff_monom:
  1676   assumes a: "a \<in> carrier R" and R_not_trivial: "(carrier R \<noteq> {\<zero>})"
  1677   shows "lcoeff (monom P \<one>\<^bsub>R\<^esub> 1 \<ominus>\<^bsub>P\<^esub> monom P a 0) = \<one>"
  1678   using deg_minus_monom [OF a R_not_trivial]
  1679   using coeff_minus a by auto algebra
  1680 
  1681 lemma deg_nzero_nzero:
  1682   assumes deg_p_nzero: "deg R p \<noteq> 0"
  1683   shows "p \<noteq> \<zero>\<^bsub>P\<^esub>"
  1684   using deg_zero deg_p_nzero by auto
  1685 
  1686 lemma deg_monom_minus:
  1687   assumes a: "a \<in> carrier R"
  1688   and R_not_trivial: "carrier R \<noteq> {\<zero>}"
  1689   shows "deg R (monom P \<one>\<^bsub>R\<^esub> 1 \<ominus>\<^bsub>P\<^esub> monom P a 0) = 1"
  1690   (is "deg R ?g = 1")
  1691 proof -
  1692   have "deg R ?g \<le> 1"
  1693   proof (rule deg_aboveI)
  1694     fix m::nat assume "1 < m" then show "coeff P ?g m = \<zero>"
  1695       using coeff_minus [OF monom_closed [OF R.one_closed, of 1] monom_closed [OF a, of 0], of m]
  1696       using coeff_monom [OF R.one_closed, of 1 m] using coeff_monom [OF a, of 0 m] by auto algebra
  1697   qed (simp add: a)
  1698   moreover have "1 \<le> deg R ?g"
  1699   proof (rule deg_belowI)
  1700     show "coeff P ?g 1 \<noteq> \<zero>"
  1701       using coeff_minus [OF monom_closed [OF R.one_closed, of 1] monom_closed [OF a, of 0], of 1]
  1702       using coeff_monom [OF R.one_closed, of 1 1] using coeff_monom [OF a, of 0 1]
  1703       using R_not_trivial using R.carrier_one_not_zero
  1704       by auto algebra
  1705   qed (simp add: a)
  1706   ultimately show ?thesis by simp
  1707 qed
  1708 
  1709 lemma eval_monom_expr:
  1710   assumes a: "a \<in> carrier R"
  1711   shows "eval R R id a (monom P \<one>\<^bsub>R\<^esub> 1 \<ominus>\<^bsub>P\<^esub> monom P a 0) = \<zero>"
  1712   (is "eval R R id a ?g = _")
  1713 proof -
  1714   interpret UP_pre_univ_prop R R id by unfold_locales simp
  1715   have eval_ring_hom: "eval R R id a \<in> ring_hom P R" using eval_ring_hom [OF a] by simp
  1716   interpret ring_hom_cring P R "eval R R id a" by unfold_locales (rule eval_ring_hom)
  1717   have mon1_closed: "monom P \<one>\<^bsub>R\<^esub> 1 \<in> carrier P"
  1718     and mon0_closed: "monom P a 0 \<in> carrier P"
  1719     and min_mon0_closed: "\<ominus>\<^bsub>P\<^esub> monom P a 0 \<in> carrier P"
  1720     using a R.a_inv_closed by auto
  1721   have "eval R R id a ?g = eval R R id a (monom P \<one> 1) \<ominus> eval R R id a (monom P a 0)"
  1722     unfolding P.minus_eq [OF mon1_closed mon0_closed]
  1723     unfolding hom_add [OF mon1_closed min_mon0_closed]
  1724     unfolding hom_a_inv [OF mon0_closed]
  1725     using R.minus_eq [symmetric] mon1_closed mon0_closed by auto
  1726   also have "\<dots> = a \<ominus> a"
  1727     using eval_monom [OF R.one_closed a, of 1] using eval_monom [OF a a, of 0] using a by simp
  1728   also have "\<dots> = \<zero>"
  1729     using a by algebra
  1730   finally show ?thesis by simp
  1731 qed
  1732 
  1733 lemma remainder_theorem_exist:
  1734   assumes f: "f \<in> carrier P" and a: "a \<in> carrier R"
  1735   and R_not_trivial: "carrier R \<noteq> {\<zero>}"
  1736   shows "\<exists> q r. (q \<in> carrier P) \<and> (r \<in> carrier P) \<and> f = (monom P \<one>\<^bsub>R\<^esub> 1 \<ominus>\<^bsub>P\<^esub> monom P a 0) \<otimes>\<^bsub>P\<^esub> q \<oplus>\<^bsub>P\<^esub> r \<and> (deg R r = 0)"
  1737   (is "\<exists> q r. (q \<in> carrier P) \<and> (r \<in> carrier P) \<and> f = ?g \<otimes>\<^bsub>P\<^esub> q \<oplus>\<^bsub>P\<^esub> r \<and> (deg R r = 0)")
  1738 proof -
  1739   let ?g = "monom P \<one>\<^bsub>R\<^esub> 1 \<ominus>\<^bsub>P\<^esub> monom P a 0"
  1740   from deg_minus_monom [OF a R_not_trivial]
  1741   have deg_g_nzero: "deg R ?g \<noteq> 0" by simp
  1742   have "\<exists>q r (k::nat). q \<in> carrier P \<and> r \<in> carrier P \<and>
  1743     lcoeff ?g (^) k \<odot>\<^bsub>P\<^esub> f = ?g \<otimes>\<^bsub>P\<^esub> q \<oplus>\<^bsub>P\<^esub> r \<and> (r = \<zero>\<^bsub>P\<^esub> \<or> deg R r < deg R ?g)"
  1744     using long_div_theorem [OF _ f deg_nzero_nzero [OF deg_g_nzero]] a
  1745     by auto
  1746   then show ?thesis
  1747     unfolding lcoeff_monom [OF a R_not_trivial]
  1748     unfolding deg_monom_minus [OF a R_not_trivial]
  1749     using smult_one [OF f] using deg_zero by force
  1750 qed
  1751 
  1752 lemma remainder_theorem_expression:
  1753   assumes f [simp]: "f \<in> carrier P" and a [simp]: "a \<in> carrier R"
  1754   and q [simp]: "q \<in> carrier P" and r [simp]: "r \<in> carrier P"
  1755   and R_not_trivial: "carrier R \<noteq> {\<zero>}"
  1756   and f_expr: "f = (monom P \<one>\<^bsub>R\<^esub> 1 \<ominus>\<^bsub>P\<^esub> monom P a 0) \<otimes>\<^bsub>P\<^esub> q \<oplus>\<^bsub>P\<^esub> r"
  1757   (is "f = ?g \<otimes>\<^bsub>P\<^esub> q \<oplus>\<^bsub>P\<^esub> r" is "f = ?gq \<oplus>\<^bsub>P\<^esub> r")
  1758     and deg_r_0: "deg R r = 0"
  1759     shows "r = monom P (eval R R id a f) 0"
  1760 proof -
  1761   interpret UP_pre_univ_prop R R id P by standard simp
  1762   have eval_ring_hom: "eval R R id a \<in> ring_hom P R"
  1763     using eval_ring_hom [OF a] by simp
  1764   have "eval R R id a f = eval R R id a ?gq \<oplus>\<^bsub>R\<^esub> eval R R id a r"
  1765     unfolding f_expr using ring_hom_add [OF eval_ring_hom] by auto
  1766   also have "\<dots> = ((eval R R id a ?g) \<otimes> (eval R R id a q)) \<oplus>\<^bsub>R\<^esub> eval R R id a r"
  1767     using ring_hom_mult [OF eval_ring_hom] by auto
  1768   also have "\<dots> = \<zero> \<oplus> eval R R id a r"
  1769     unfolding eval_monom_expr [OF a] using eval_ring_hom
  1770     unfolding ring_hom_def using q unfolding Pi_def by simp
  1771   also have "\<dots> = eval R R id a r"
  1772     using eval_ring_hom unfolding ring_hom_def using r unfolding Pi_def by simp
  1773   finally have eval_eq: "eval R R id a f = eval R R id a r" by simp
  1774   from deg_zero_impl_monom [OF r deg_r_0]
  1775   have "r = monom P (coeff P r 0) 0" by simp
  1776   with eval_const [OF a, of "coeff P r 0"] eval_eq
  1777   show ?thesis by auto
  1778 qed
  1779 
  1780 corollary remainder_theorem:
  1781   assumes f [simp]: "f \<in> carrier P" and a [simp]: "a \<in> carrier R"
  1782   and R_not_trivial: "carrier R \<noteq> {\<zero>}"
  1783   shows "\<exists> q r. (q \<in> carrier P) \<and> (r \<in> carrier P) \<and>
  1784      f = (monom P \<one>\<^bsub>R\<^esub> 1 \<ominus>\<^bsub>P\<^esub> monom P a 0) \<otimes>\<^bsub>P\<^esub> q \<oplus>\<^bsub>P\<^esub> monom P (eval R R id a f) 0"
  1785   (is "\<exists> q r. (q \<in> carrier P) \<and> (r \<in> carrier P) \<and> f = ?g \<otimes>\<^bsub>P\<^esub> q \<oplus>\<^bsub>P\<^esub> monom P (eval R R id a f) 0")
  1786 proof -
  1787   from remainder_theorem_exist [OF f a R_not_trivial]
  1788   obtain q r
  1789     where q_r: "q \<in> carrier P \<and> r \<in> carrier P \<and> f = ?g \<otimes>\<^bsub>P\<^esub> q \<oplus>\<^bsub>P\<^esub> r"
  1790     and deg_r: "deg R r = 0" by force
  1791   with remainder_theorem_expression [OF f a _ _ R_not_trivial, of q r]
  1792   show ?thesis by auto
  1793 qed
  1794 
  1795 end
  1796 
  1797 
  1798 subsection \<open>Sample Application of Evaluation Homomorphism\<close>
  1799 
  1800 lemma UP_pre_univ_propI:
  1801   assumes "cring R"
  1802     and "cring S"
  1803     and "h \<in> ring_hom R S"
  1804   shows "UP_pre_univ_prop R S h"
  1805   using assms
  1806   by (auto intro!: UP_pre_univ_prop.intro ring_hom_cring.intro
  1807     ring_hom_cring_axioms.intro UP_cring.intro)
  1808 
  1809 definition
  1810   INTEG :: "int ring"
  1811   where "INTEG = \<lparr>carrier = UNIV, mult = op *, one = 1, zero = 0, add = op +\<rparr>"
  1812 
  1813 lemma INTEG_cring: "cring INTEG"
  1814   by (unfold INTEG_def) (auto intro!: cringI abelian_groupI comm_monoidI
  1815     left_minus distrib_right)
  1816 
  1817 lemma INTEG_id_eval:
  1818   "UP_pre_univ_prop INTEG INTEG id"
  1819   by (fast intro: UP_pre_univ_propI INTEG_cring id_ring_hom)
  1820 
  1821 text \<open>
  1822   Interpretation now enables to import all theorems and lemmas
  1823   valid in the context of homomorphisms between @{term INTEG} and @{term
  1824   "UP INTEG"} globally.
  1825 \<close>
  1826 
  1827 interpretation INTEG: UP_pre_univ_prop INTEG INTEG id "UP INTEG"
  1828   using INTEG_id_eval by simp_all
  1829 
  1830 lemma INTEG_closed [intro, simp]:
  1831   "z \<in> carrier INTEG"
  1832   by (unfold INTEG_def) simp
  1833 
  1834 lemma INTEG_mult [simp]:
  1835   "mult INTEG z w = z * w"
  1836   by (unfold INTEG_def) simp
  1837 
  1838 lemma INTEG_pow [simp]:
  1839   "pow INTEG z n = z ^ n"
  1840   by (induct n) (simp_all add: INTEG_def nat_pow_def)
  1841 
  1842 lemma "eval INTEG INTEG id 10 (monom (UP INTEG) 5 2) = 500"
  1843   by (simp add: INTEG.eval_monom)
  1844 
  1845 end