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