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