src/HOL/Algebra/Ring.thy
author wenzelm
Sun Mar 21 17:12:31 2010 +0100 (2010-03-21)
changeset 35849 b5522b51cb1e
parent 35848 5443079512ea
child 41433 1b8ff770f02c
permissions -rw-r--r--
standard headers;
wenzelm@35849
     1
(*  Title:      The algebraic hierarchy of rings
wenzelm@35849
     2
    Author:     Clemens Ballarin, started 9 December 1996
wenzelm@35849
     3
    Copyright:  Clemens Ballarin
ballarin@20318
     4
*)
ballarin@20318
     5
haftmann@28823
     6
theory Ring
haftmann@28823
     7
imports FiniteProduct
wenzelm@35847
     8
uses ("ringsimp.ML")
wenzelm@35847
     9
begin
ballarin@20318
    10
ballarin@27717
    11
section {* The Algebraic Hierarchy of Rings *}
ballarin@27717
    12
ballarin@27717
    13
subsection {* Abelian Groups *}
ballarin@20318
    14
ballarin@20318
    15
record 'a ring = "'a monoid" +
ballarin@20318
    16
  zero :: 'a ("\<zero>\<index>")
ballarin@20318
    17
  add :: "['a, 'a] => 'a" (infixl "\<oplus>\<index>" 65)
ballarin@20318
    18
ballarin@20318
    19
text {* Derived operations. *}
ballarin@20318
    20
wenzelm@35847
    21
definition
ballarin@20318
    22
  a_inv :: "[('a, 'm) ring_scheme, 'a ] => 'a" ("\<ominus>\<index> _" [81] 80)
wenzelm@35848
    23
  where "a_inv R = m_inv (| carrier = carrier R, mult = add R, one = zero R |)"
ballarin@20318
    24
wenzelm@35847
    25
definition
ballarin@20318
    26
  a_minus :: "[('a, 'm) ring_scheme, 'a, 'a] => 'a" (infixl "\<ominus>\<index>" 65)
wenzelm@35848
    27
  where "[| x \<in> carrier R; y \<in> carrier R |] ==> x \<ominus>\<^bsub>R\<^esub> y = x \<oplus>\<^bsub>R\<^esub> (\<ominus>\<^bsub>R\<^esub> y)"
ballarin@20318
    28
ballarin@20318
    29
locale abelian_monoid =
ballarin@20318
    30
  fixes G (structure)
ballarin@20318
    31
  assumes a_comm_monoid:
ballarin@20318
    32
     "comm_monoid (| carrier = carrier G, mult = add G, one = zero G |)"
ballarin@20318
    33
ballarin@20318
    34
ballarin@20318
    35
text {*
ballarin@20318
    36
  The following definition is redundant but simple to use.
ballarin@20318
    37
*}
ballarin@20318
    38
ballarin@20318
    39
locale abelian_group = abelian_monoid +
ballarin@20318
    40
  assumes a_comm_group:
ballarin@20318
    41
     "comm_group (| carrier = carrier G, mult = add G, one = zero G |)"
ballarin@20318
    42
ballarin@20318
    43
ballarin@20318
    44
subsection {* Basic Properties *}
ballarin@20318
    45
ballarin@20318
    46
lemma abelian_monoidI:
ballarin@20318
    47
  fixes R (structure)
ballarin@20318
    48
  assumes a_closed:
ballarin@20318
    49
      "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==> x \<oplus> y \<in> carrier R"
ballarin@20318
    50
    and zero_closed: "\<zero> \<in> carrier R"
ballarin@20318
    51
    and a_assoc:
ballarin@20318
    52
      "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |] ==>
ballarin@20318
    53
      (x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
ballarin@20318
    54
    and l_zero: "!!x. x \<in> carrier R ==> \<zero> \<oplus> x = x"
ballarin@20318
    55
    and a_comm:
ballarin@20318
    56
      "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==> x \<oplus> y = y \<oplus> x"
ballarin@20318
    57
  shows "abelian_monoid R"
ballarin@27714
    58
  by (auto intro!: abelian_monoid.intro comm_monoidI intro: assms)
ballarin@20318
    59
ballarin@20318
    60
lemma abelian_groupI:
ballarin@20318
    61
  fixes R (structure)
ballarin@20318
    62
  assumes a_closed:
ballarin@20318
    63
      "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==> x \<oplus> y \<in> carrier R"
ballarin@20318
    64
    and zero_closed: "zero R \<in> carrier R"
ballarin@20318
    65
    and a_assoc:
ballarin@20318
    66
      "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |] ==>
ballarin@20318
    67
      (x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
ballarin@20318
    68
    and a_comm:
ballarin@20318
    69
      "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==> x \<oplus> y = y \<oplus> x"
ballarin@20318
    70
    and l_zero: "!!x. x \<in> carrier R ==> \<zero> \<oplus> x = x"
ballarin@20318
    71
    and l_inv_ex: "!!x. x \<in> carrier R ==> EX y : carrier R. y \<oplus> x = \<zero>"
ballarin@20318
    72
  shows "abelian_group R"
ballarin@20318
    73
  by (auto intro!: abelian_group.intro abelian_monoidI
ballarin@20318
    74
      abelian_group_axioms.intro comm_monoidI comm_groupI
ballarin@27714
    75
    intro: assms)
ballarin@20318
    76
ballarin@20318
    77
lemma (in abelian_monoid) a_monoid:
ballarin@20318
    78
  "monoid (| carrier = carrier G, mult = add G, one = zero G |)"
ballarin@20318
    79
by (rule comm_monoid.axioms, rule a_comm_monoid) 
ballarin@20318
    80
ballarin@20318
    81
lemma (in abelian_group) a_group:
ballarin@20318
    82
  "group (| carrier = carrier G, mult = add G, one = zero G |)"
ballarin@20318
    83
  by (simp add: group_def a_monoid)
ballarin@20318
    84
    (simp add: comm_group.axioms group.axioms a_comm_group)
ballarin@20318
    85
ballarin@20318
    86
lemmas monoid_record_simps = partial_object.simps monoid.simps
ballarin@20318
    87
ballarin@20318
    88
lemma (in abelian_monoid) a_closed [intro, simp]:
ballarin@20318
    89
  "\<lbrakk> x \<in> carrier G; y \<in> carrier G \<rbrakk> \<Longrightarrow> x \<oplus> y \<in> carrier G"
ballarin@20318
    90
  by (rule monoid.m_closed [OF a_monoid, simplified monoid_record_simps]) 
ballarin@20318
    91
ballarin@20318
    92
lemma (in abelian_monoid) zero_closed [intro, simp]:
ballarin@20318
    93
  "\<zero> \<in> carrier G"
ballarin@20318
    94
  by (rule monoid.one_closed [OF a_monoid, simplified monoid_record_simps])
ballarin@20318
    95
ballarin@20318
    96
lemma (in abelian_group) a_inv_closed [intro, simp]:
ballarin@20318
    97
  "x \<in> carrier G ==> \<ominus> x \<in> carrier G"
ballarin@20318
    98
  by (simp add: a_inv_def
ballarin@20318
    99
    group.inv_closed [OF a_group, simplified monoid_record_simps])
ballarin@20318
   100
ballarin@20318
   101
lemma (in abelian_group) minus_closed [intro, simp]:
ballarin@20318
   102
  "[| x \<in> carrier G; y \<in> carrier G |] ==> x \<ominus> y \<in> carrier G"
ballarin@20318
   103
  by (simp add: a_minus_def)
ballarin@20318
   104
ballarin@20318
   105
lemma (in abelian_group) a_l_cancel [simp]:
ballarin@20318
   106
  "[| x \<in> carrier G; y \<in> carrier G; z \<in> carrier G |] ==>
ballarin@20318
   107
   (x \<oplus> y = x \<oplus> z) = (y = z)"
ballarin@20318
   108
  by (rule group.l_cancel [OF a_group, simplified monoid_record_simps])
ballarin@20318
   109
ballarin@20318
   110
lemma (in abelian_group) a_r_cancel [simp]:
ballarin@20318
   111
  "[| x \<in> carrier G; y \<in> carrier G; z \<in> carrier G |] ==>
ballarin@20318
   112
   (y \<oplus> x = z \<oplus> x) = (y = z)"
ballarin@20318
   113
  by (rule group.r_cancel [OF a_group, simplified monoid_record_simps])
ballarin@20318
   114
ballarin@20318
   115
lemma (in abelian_monoid) a_assoc:
ballarin@20318
   116
  "\<lbrakk>x \<in> carrier G; y \<in> carrier G; z \<in> carrier G\<rbrakk> \<Longrightarrow>
ballarin@20318
   117
  (x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
ballarin@20318
   118
  by (rule monoid.m_assoc [OF a_monoid, simplified monoid_record_simps])
ballarin@20318
   119
ballarin@20318
   120
lemma (in abelian_monoid) l_zero [simp]:
ballarin@20318
   121
  "x \<in> carrier G ==> \<zero> \<oplus> x = x"
ballarin@20318
   122
  by (rule monoid.l_one [OF a_monoid, simplified monoid_record_simps])
ballarin@20318
   123
ballarin@20318
   124
lemma (in abelian_group) l_neg:
ballarin@20318
   125
  "x \<in> carrier G ==> \<ominus> x \<oplus> x = \<zero>"
ballarin@20318
   126
  by (simp add: a_inv_def
ballarin@20318
   127
    group.l_inv [OF a_group, simplified monoid_record_simps])
ballarin@20318
   128
ballarin@20318
   129
lemma (in abelian_monoid) a_comm:
ballarin@20318
   130
  "\<lbrakk>x \<in> carrier G; y \<in> carrier G\<rbrakk> \<Longrightarrow> x \<oplus> y = y \<oplus> x"
ballarin@20318
   131
  by (rule comm_monoid.m_comm [OF a_comm_monoid,
ballarin@20318
   132
    simplified monoid_record_simps])
ballarin@20318
   133
ballarin@20318
   134
lemma (in abelian_monoid) a_lcomm:
ballarin@20318
   135
  "\<lbrakk>x \<in> carrier G; y \<in> carrier G; z \<in> carrier G\<rbrakk> \<Longrightarrow>
ballarin@20318
   136
   x \<oplus> (y \<oplus> z) = y \<oplus> (x \<oplus> z)"
ballarin@20318
   137
  by (rule comm_monoid.m_lcomm [OF a_comm_monoid,
ballarin@20318
   138
                                simplified monoid_record_simps])
ballarin@20318
   139
ballarin@20318
   140
lemma (in abelian_monoid) r_zero [simp]:
ballarin@20318
   141
  "x \<in> carrier G ==> x \<oplus> \<zero> = x"
ballarin@20318
   142
  using monoid.r_one [OF a_monoid]
ballarin@20318
   143
  by simp
ballarin@20318
   144
ballarin@20318
   145
lemma (in abelian_group) r_neg:
ballarin@20318
   146
  "x \<in> carrier G ==> x \<oplus> (\<ominus> x) = \<zero>"
ballarin@20318
   147
  using group.r_inv [OF a_group]
ballarin@20318
   148
  by (simp add: a_inv_def)
ballarin@20318
   149
ballarin@20318
   150
lemma (in abelian_group) minus_zero [simp]:
ballarin@20318
   151
  "\<ominus> \<zero> = \<zero>"
ballarin@20318
   152
  by (simp add: a_inv_def
ballarin@20318
   153
    group.inv_one [OF a_group, simplified monoid_record_simps])
ballarin@20318
   154
ballarin@20318
   155
lemma (in abelian_group) minus_minus [simp]:
ballarin@20318
   156
  "x \<in> carrier G ==> \<ominus> (\<ominus> x) = x"
ballarin@20318
   157
  using group.inv_inv [OF a_group, simplified monoid_record_simps]
ballarin@20318
   158
  by (simp add: a_inv_def)
ballarin@20318
   159
ballarin@20318
   160
lemma (in abelian_group) a_inv_inj:
ballarin@20318
   161
  "inj_on (a_inv G) (carrier G)"
ballarin@20318
   162
  using group.inv_inj [OF a_group, simplified monoid_record_simps]
ballarin@20318
   163
  by (simp add: a_inv_def)
ballarin@20318
   164
ballarin@20318
   165
lemma (in abelian_group) minus_add:
ballarin@20318
   166
  "[| x \<in> carrier G; y \<in> carrier G |] ==> \<ominus> (x \<oplus> y) = \<ominus> x \<oplus> \<ominus> y"
ballarin@20318
   167
  using comm_group.inv_mult [OF a_comm_group]
ballarin@20318
   168
  by (simp add: a_inv_def)
ballarin@20318
   169
ballarin@20318
   170
lemma (in abelian_group) minus_equality: 
ballarin@20318
   171
  "[| x \<in> carrier G; y \<in> carrier G; y \<oplus> x = \<zero> |] ==> \<ominus> x = y" 
ballarin@20318
   172
  using group.inv_equality [OF a_group] 
ballarin@20318
   173
  by (auto simp add: a_inv_def) 
ballarin@20318
   174
 
ballarin@20318
   175
lemma (in abelian_monoid) minus_unique: 
ballarin@20318
   176
  "[| x \<in> carrier G; y \<in> carrier G; y' \<in> carrier G;
ballarin@20318
   177
      y \<oplus> x = \<zero>; x \<oplus> y' = \<zero> |] ==> y = y'" 
ballarin@20318
   178
  using monoid.inv_unique [OF a_monoid] 
ballarin@20318
   179
  by (simp add: a_inv_def) 
ballarin@20318
   180
ballarin@20318
   181
lemmas (in abelian_monoid) a_ac = a_assoc a_comm a_lcomm
ballarin@20318
   182
ballarin@20318
   183
text {* Derive an @{text "abelian_group"} from a @{text "comm_group"} *}
ballarin@20318
   184
lemma comm_group_abelian_groupI:
ballarin@20318
   185
  fixes G (structure)
ballarin@20318
   186
  assumes cg: "comm_group \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
ballarin@20318
   187
  shows "abelian_group G"
ballarin@20318
   188
proof -
ballarin@29237
   189
  interpret comm_group "\<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
ballarin@20318
   190
    by (rule cg)
haftmann@28823
   191
  show "abelian_group G" ..
ballarin@20318
   192
qed
ballarin@20318
   193
ballarin@20318
   194
ballarin@20318
   195
subsection {* Sums over Finite Sets *}
ballarin@20318
   196
ballarin@20318
   197
text {*
ballarin@20318
   198
  This definition makes it easy to lift lemmas from @{term finprod}.
ballarin@20318
   199
*}
ballarin@20318
   200
wenzelm@35848
   201
definition
wenzelm@35848
   202
  finsum :: "[('b, 'm) ring_scheme, 'a => 'b, 'a set] => 'b" where
wenzelm@35848
   203
  "finsum G f A = finprod (| carrier = carrier G, mult = add G, one = zero G |) f A"
ballarin@20318
   204
ballarin@20318
   205
syntax
ballarin@20318
   206
  "_finsum" :: "index => idt => 'a set => 'b => 'b"
ballarin@20318
   207
      ("(3\<Oplus>__:_. _)" [1000, 0, 51, 10] 10)
ballarin@20318
   208
syntax (xsymbols)
ballarin@20318
   209
  "_finsum" :: "index => idt => 'a set => 'b => 'b"
ballarin@20318
   210
      ("(3\<Oplus>__\<in>_. _)" [1000, 0, 51, 10] 10)
ballarin@20318
   211
syntax (HTML output)
ballarin@20318
   212
  "_finsum" :: "index => idt => 'a set => 'b => 'b"
ballarin@20318
   213
      ("(3\<Oplus>__\<in>_. _)" [1000, 0, 51, 10] 10)
ballarin@20318
   214
translations
wenzelm@35054
   215
  "\<Oplus>\<index>i:A. b" == "CONST finsum \<struct>\<index> (%i. b) A"
ballarin@20318
   216
  -- {* Beware of argument permutation! *}
ballarin@20318
   217
ballarin@27933
   218
context abelian_monoid begin
ballarin@27933
   219
ballarin@20318
   220
(*
ballarin@27933
   221
  lemmas finsum_empty [simp] =
ballarin@20318
   222
    comm_monoid.finprod_empty [OF a_comm_monoid, simplified]
ballarin@20318
   223
  is dangeous, because attributes (like simplified) are applied upon opening
ballarin@20318
   224
  the locale, simplified refers to the simpset at that time!!!
ballarin@20318
   225
ballarin@27933
   226
  lemmas finsum_empty [simp] =
ballarin@20318
   227
    abelian_monoid.finprod_empty [OF a_abelian_monoid, folded finsum_def,
ballarin@20318
   228
      simplified monoid_record_simps]
ballarin@20318
   229
  makes the locale slow, because proofs are repeated for every
ballarin@20318
   230
  "lemma (in abelian_monoid)" command.
ballarin@20318
   231
  When lemma is used time in UnivPoly.thy from beginning to UP_cring goes down
ballarin@20318
   232
  from 110 secs to 60 secs.
ballarin@20318
   233
*)
ballarin@20318
   234
ballarin@27933
   235
lemma finsum_empty [simp]:
ballarin@20318
   236
  "finsum G f {} = \<zero>"
ballarin@20318
   237
  by (rule comm_monoid.finprod_empty [OF a_comm_monoid,
ballarin@20318
   238
    folded finsum_def, simplified monoid_record_simps])
ballarin@20318
   239
ballarin@27933
   240
lemma finsum_insert [simp]:
ballarin@20318
   241
  "[| finite F; a \<notin> F; f \<in> F -> carrier G; f a \<in> carrier G |]
ballarin@20318
   242
  ==> finsum G f (insert a F) = f a \<oplus> finsum G f F"
ballarin@20318
   243
  by (rule comm_monoid.finprod_insert [OF a_comm_monoid,
ballarin@20318
   244
    folded finsum_def, simplified monoid_record_simps])
ballarin@20318
   245
ballarin@27933
   246
lemma finsum_zero [simp]:
ballarin@20318
   247
  "finite A ==> (\<Oplus>i\<in>A. \<zero>) = \<zero>"
ballarin@20318
   248
  by (rule comm_monoid.finprod_one [OF a_comm_monoid, folded finsum_def,
ballarin@20318
   249
    simplified monoid_record_simps])
ballarin@20318
   250
ballarin@27933
   251
lemma finsum_closed [simp]:
ballarin@20318
   252
  fixes A
ballarin@20318
   253
  assumes fin: "finite A" and f: "f \<in> A -> carrier G" 
ballarin@20318
   254
  shows "finsum G f A \<in> carrier G"
wenzelm@23350
   255
  apply (rule comm_monoid.finprod_closed [OF a_comm_monoid,
ballarin@20318
   256
    folded finsum_def, simplified monoid_record_simps])
wenzelm@23350
   257
   apply (rule fin)
wenzelm@23350
   258
  apply (rule f)
wenzelm@23350
   259
  done
ballarin@20318
   260
ballarin@27933
   261
lemma finsum_Un_Int:
ballarin@20318
   262
  "[| finite A; finite B; g \<in> A -> carrier G; g \<in> B -> carrier G |] ==>
ballarin@20318
   263
     finsum G g (A Un B) \<oplus> finsum G g (A Int B) =
ballarin@20318
   264
     finsum G g A \<oplus> finsum G g B"
ballarin@20318
   265
  by (rule comm_monoid.finprod_Un_Int [OF a_comm_monoid,
ballarin@20318
   266
    folded finsum_def, simplified monoid_record_simps])
ballarin@20318
   267
ballarin@27933
   268
lemma finsum_Un_disjoint:
ballarin@20318
   269
  "[| finite A; finite B; A Int B = {};
ballarin@20318
   270
      g \<in> A -> carrier G; g \<in> B -> carrier G |]
ballarin@20318
   271
   ==> finsum G g (A Un B) = finsum G g A \<oplus> finsum G g B"
ballarin@20318
   272
  by (rule comm_monoid.finprod_Un_disjoint [OF a_comm_monoid,
ballarin@20318
   273
    folded finsum_def, simplified monoid_record_simps])
ballarin@20318
   274
ballarin@27933
   275
lemma finsum_addf:
ballarin@20318
   276
  "[| finite A; f \<in> A -> carrier G; g \<in> A -> carrier G |] ==>
ballarin@20318
   277
   finsum G (%x. f x \<oplus> g x) A = (finsum G f A \<oplus> finsum G g A)"
ballarin@20318
   278
  by (rule comm_monoid.finprod_multf [OF a_comm_monoid,
ballarin@20318
   279
    folded finsum_def, simplified monoid_record_simps])
ballarin@20318
   280
ballarin@27933
   281
lemma finsum_cong':
ballarin@20318
   282
  "[| A = B; g : B -> carrier G;
ballarin@20318
   283
      !!i. i : B ==> f i = g i |] ==> finsum G f A = finsum G g B"
ballarin@20318
   284
  by (rule comm_monoid.finprod_cong' [OF a_comm_monoid,
ballarin@20318
   285
    folded finsum_def, simplified monoid_record_simps]) auto
ballarin@20318
   286
ballarin@27933
   287
lemma finsum_0 [simp]:
ballarin@20318
   288
  "f : {0::nat} -> carrier G ==> finsum G f {..0} = f 0"
ballarin@20318
   289
  by (rule comm_monoid.finprod_0 [OF a_comm_monoid, folded finsum_def,
ballarin@20318
   290
    simplified monoid_record_simps])
ballarin@20318
   291
ballarin@27933
   292
lemma finsum_Suc [simp]:
ballarin@20318
   293
  "f : {..Suc n} -> carrier G ==>
ballarin@20318
   294
   finsum G f {..Suc n} = (f (Suc n) \<oplus> finsum G f {..n})"
ballarin@20318
   295
  by (rule comm_monoid.finprod_Suc [OF a_comm_monoid, folded finsum_def,
ballarin@20318
   296
    simplified monoid_record_simps])
ballarin@20318
   297
ballarin@27933
   298
lemma finsum_Suc2:
ballarin@20318
   299
  "f : {..Suc n} -> carrier G ==>
ballarin@20318
   300
   finsum G f {..Suc n} = (finsum G (%i. f (Suc i)) {..n} \<oplus> f 0)"
ballarin@20318
   301
  by (rule comm_monoid.finprod_Suc2 [OF a_comm_monoid, folded finsum_def,
ballarin@20318
   302
    simplified monoid_record_simps])
ballarin@20318
   303
ballarin@27933
   304
lemma finsum_add [simp]:
ballarin@20318
   305
  "[| f : {..n} -> carrier G; g : {..n} -> carrier G |] ==>
ballarin@20318
   306
     finsum G (%i. f i \<oplus> g i) {..n::nat} =
ballarin@20318
   307
     finsum G f {..n} \<oplus> finsum G g {..n}"
ballarin@20318
   308
  by (rule comm_monoid.finprod_mult [OF a_comm_monoid, folded finsum_def,
ballarin@20318
   309
    simplified monoid_record_simps])
ballarin@20318
   310
ballarin@27933
   311
lemma finsum_cong:
ballarin@20318
   312
  "[| A = B; f : B -> carrier G;
ballarin@20318
   313
      !!i. i : B =simp=> f i = g i |] ==> finsum G f A = finsum G g B"
ballarin@20318
   314
  by (rule comm_monoid.finprod_cong [OF a_comm_monoid, folded finsum_def,
ballarin@20318
   315
    simplified monoid_record_simps]) (auto simp add: simp_implies_def)
ballarin@20318
   316
ballarin@20318
   317
text {*Usually, if this rule causes a failed congruence proof error,
ballarin@20318
   318
   the reason is that the premise @{text "g \<in> B -> carrier G"} cannot be shown.
ballarin@20318
   319
   Adding @{thm [source] Pi_def} to the simpset is often useful. *}
ballarin@20318
   320
ballarin@27933
   321
lemma finsum_reindex:
ballarin@27699
   322
  assumes fin: "finite A"
ballarin@27699
   323
    shows "f : (h ` A) \<rightarrow> carrier G \<Longrightarrow> 
ballarin@27699
   324
        inj_on h A ==> finsum G f (h ` A) = finsum G (%x. f (h x)) A"
ballarin@27699
   325
  using fin apply induct
ballarin@27699
   326
  apply (auto simp add: finsum_insert Pi_def)
ballarin@27699
   327
done
ballarin@27699
   328
ballarin@27699
   329
(* The following is wrong.  Needed is the equivalent of (^) for addition,
ballarin@27699
   330
  or indeed the canonical embedding from Nat into the monoid.
ballarin@27699
   331
ballarin@27933
   332
lemma finsum_const:
ballarin@27699
   333
  assumes fin [simp]: "finite A"
ballarin@27699
   334
      and a [simp]: "a : carrier G"
ballarin@27699
   335
    shows "finsum G (%x. a) A = a (^) card A"
ballarin@27699
   336
  using fin apply induct
ballarin@27699
   337
  apply force
ballarin@27699
   338
  apply (subst finsum_insert)
ballarin@27699
   339
  apply auto
ballarin@27699
   340
  apply (force simp add: Pi_def)
ballarin@27699
   341
  apply (subst m_comm)
ballarin@27699
   342
  apply auto
ballarin@27699
   343
done
ballarin@27699
   344
*)
ballarin@27699
   345
ballarin@27933
   346
(* By Jesus Aransay. *)
ballarin@27933
   347
ballarin@27933
   348
lemma finsum_singleton:
ballarin@27933
   349
  assumes i_in_A: "i \<in> A" and fin_A: "finite A" and f_Pi: "f \<in> A \<rightarrow> carrier G"
ballarin@27933
   350
  shows "(\<Oplus>j\<in>A. if i = j then f j else \<zero>) = f i"
ballarin@27933
   351
  using i_in_A finsum_insert [of "A - {i}" i "(\<lambda>j. if i = j then f j else \<zero>)"]
ballarin@27933
   352
    fin_A f_Pi finsum_zero [of "A - {i}"]
ballarin@27933
   353
    finsum_cong [of "A - {i}" "A - {i}" "(\<lambda>j. if i = j then f j else \<zero>)" "(\<lambda>i. \<zero>)"] 
ballarin@27933
   354
  unfolding Pi_def simp_implies_def by (force simp add: insert_absorb)
ballarin@27933
   355
ballarin@27933
   356
end
ballarin@27933
   357
ballarin@20318
   358
ballarin@27717
   359
subsection {* Rings: Basic Definitions *}
ballarin@20318
   360
ballarin@29237
   361
locale ring = abelian_group R + monoid R for R (structure) +
ballarin@20318
   362
  assumes l_distr: "[| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
ballarin@20318
   363
      ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
ballarin@20318
   364
    and r_distr: "[| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
ballarin@20318
   365
      ==> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
ballarin@20318
   366
ballarin@20318
   367
locale cring = ring + comm_monoid R
ballarin@20318
   368
ballarin@20318
   369
locale "domain" = cring +
ballarin@20318
   370
  assumes one_not_zero [simp]: "\<one> ~= \<zero>"
ballarin@20318
   371
    and integral: "[| a \<otimes> b = \<zero>; a \<in> carrier R; b \<in> carrier R |] ==>
ballarin@20318
   372
                  a = \<zero> | b = \<zero>"
ballarin@20318
   373
ballarin@20318
   374
locale field = "domain" +
ballarin@20318
   375
  assumes field_Units: "Units R = carrier R - {\<zero>}"
ballarin@20318
   376
ballarin@20318
   377
ballarin@20318
   378
subsection {* Rings *}
ballarin@20318
   379
ballarin@20318
   380
lemma ringI:
ballarin@20318
   381
  fixes R (structure)
ballarin@20318
   382
  assumes abelian_group: "abelian_group R"
ballarin@20318
   383
    and monoid: "monoid R"
ballarin@20318
   384
    and l_distr: "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
ballarin@20318
   385
      ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
ballarin@20318
   386
    and r_distr: "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
ballarin@20318
   387
      ==> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
ballarin@20318
   388
  shows "ring R"
ballarin@20318
   389
  by (auto intro: ring.intro
ballarin@27714
   390
    abelian_group.axioms ring_axioms.intro assms)
ballarin@20318
   391
ballarin@20318
   392
lemma (in ring) is_abelian_group:
ballarin@20318
   393
  "abelian_group R"
haftmann@28823
   394
  ..
ballarin@20318
   395
ballarin@20318
   396
lemma (in ring) is_monoid:
ballarin@20318
   397
  "monoid R"
ballarin@20318
   398
  by (auto intro!: monoidI m_assoc)
ballarin@20318
   399
ballarin@20318
   400
lemma (in ring) is_ring:
ballarin@20318
   401
  "ring R"
wenzelm@26202
   402
  by (rule ring_axioms)
ballarin@20318
   403
ballarin@20318
   404
lemmas ring_record_simps = monoid_record_simps ring.simps
ballarin@20318
   405
ballarin@20318
   406
lemma cringI:
ballarin@20318
   407
  fixes R (structure)
ballarin@20318
   408
  assumes abelian_group: "abelian_group R"
ballarin@20318
   409
    and comm_monoid: "comm_monoid R"
ballarin@20318
   410
    and l_distr: "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
ballarin@20318
   411
      ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
ballarin@20318
   412
  shows "cring R"
wenzelm@23350
   413
proof (intro cring.intro ring.intro)
wenzelm@23350
   414
  show "ring_axioms R"
ballarin@20318
   415
    -- {* Right-distributivity follows from left-distributivity and
ballarin@20318
   416
          commutativity. *}
wenzelm@23350
   417
  proof (rule ring_axioms.intro)
wenzelm@23350
   418
    fix x y z
wenzelm@23350
   419
    assume R: "x \<in> carrier R" "y \<in> carrier R" "z \<in> carrier R"
wenzelm@23350
   420
    note [simp] = comm_monoid.axioms [OF comm_monoid]
wenzelm@23350
   421
      abelian_group.axioms [OF abelian_group]
wenzelm@23350
   422
      abelian_monoid.a_closed
ballarin@20318
   423
        
wenzelm@23350
   424
    from R have "z \<otimes> (x \<oplus> y) = (x \<oplus> y) \<otimes> z"
wenzelm@23350
   425
      by (simp add: comm_monoid.m_comm [OF comm_monoid.intro])
wenzelm@23350
   426
    also from R have "... = x \<otimes> z \<oplus> y \<otimes> z" by (simp add: l_distr)
wenzelm@23350
   427
    also from R have "... = z \<otimes> x \<oplus> z \<otimes> y"
wenzelm@23350
   428
      by (simp add: comm_monoid.m_comm [OF comm_monoid.intro])
wenzelm@23350
   429
    finally show "z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y" .
wenzelm@23350
   430
  qed (rule l_distr)
wenzelm@23350
   431
qed (auto intro: cring.intro
ballarin@27714
   432
  abelian_group.axioms comm_monoid.axioms ring_axioms.intro assms)
ballarin@20318
   433
ballarin@27699
   434
(*
ballarin@20318
   435
lemma (in cring) is_comm_monoid:
ballarin@20318
   436
  "comm_monoid R"
ballarin@20318
   437
  by (auto intro!: comm_monoidI m_assoc m_comm)
ballarin@27699
   438
*)
ballarin@20318
   439
ballarin@20318
   440
lemma (in cring) is_cring:
wenzelm@26202
   441
  "cring R" by (rule cring_axioms)
wenzelm@23350
   442
ballarin@20318
   443
ballarin@20318
   444
subsubsection {* Normaliser for Rings *}
ballarin@20318
   445
ballarin@20318
   446
lemma (in abelian_group) r_neg2:
ballarin@20318
   447
  "[| x \<in> carrier G; y \<in> carrier G |] ==> x \<oplus> (\<ominus> x \<oplus> y) = y"
ballarin@20318
   448
proof -
ballarin@20318
   449
  assume G: "x \<in> carrier G" "y \<in> carrier G"
ballarin@20318
   450
  then have "(x \<oplus> \<ominus> x) \<oplus> y = y"
ballarin@20318
   451
    by (simp only: r_neg l_zero)
ballarin@20318
   452
  with G show ?thesis 
ballarin@20318
   453
    by (simp add: a_ac)
ballarin@20318
   454
qed
ballarin@20318
   455
ballarin@20318
   456
lemma (in abelian_group) r_neg1:
ballarin@20318
   457
  "[| x \<in> carrier G; y \<in> carrier G |] ==> \<ominus> x \<oplus> (x \<oplus> y) = y"
ballarin@20318
   458
proof -
ballarin@20318
   459
  assume G: "x \<in> carrier G" "y \<in> carrier G"
ballarin@20318
   460
  then have "(\<ominus> x \<oplus> x) \<oplus> y = y" 
ballarin@20318
   461
    by (simp only: l_neg l_zero)
ballarin@20318
   462
  with G show ?thesis by (simp add: a_ac)
ballarin@20318
   463
qed
ballarin@20318
   464
ballarin@20318
   465
text {* 
ballarin@20318
   466
  The following proofs are from Jacobson, Basic Algebra I, pp.~88--89
ballarin@20318
   467
*}
ballarin@20318
   468
ballarin@20318
   469
lemma (in ring) l_null [simp]:
ballarin@20318
   470
  "x \<in> carrier R ==> \<zero> \<otimes> x = \<zero>"
ballarin@20318
   471
proof -
ballarin@20318
   472
  assume R: "x \<in> carrier R"
ballarin@20318
   473
  then have "\<zero> \<otimes> x \<oplus> \<zero> \<otimes> x = (\<zero> \<oplus> \<zero>) \<otimes> x"
ballarin@20318
   474
    by (simp add: l_distr del: l_zero r_zero)
ballarin@20318
   475
  also from R have "... = \<zero> \<otimes> x \<oplus> \<zero>" by simp
ballarin@20318
   476
  finally have "\<zero> \<otimes> x \<oplus> \<zero> \<otimes> x = \<zero> \<otimes> x \<oplus> \<zero>" .
ballarin@20318
   477
  with R show ?thesis by (simp del: r_zero)
ballarin@20318
   478
qed
ballarin@20318
   479
ballarin@20318
   480
lemma (in ring) r_null [simp]:
ballarin@20318
   481
  "x \<in> carrier R ==> x \<otimes> \<zero> = \<zero>"
ballarin@20318
   482
proof -
ballarin@20318
   483
  assume R: "x \<in> carrier R"
ballarin@20318
   484
  then have "x \<otimes> \<zero> \<oplus> x \<otimes> \<zero> = x \<otimes> (\<zero> \<oplus> \<zero>)"
ballarin@20318
   485
    by (simp add: r_distr del: l_zero r_zero)
ballarin@20318
   486
  also from R have "... = x \<otimes> \<zero> \<oplus> \<zero>" by simp
ballarin@20318
   487
  finally have "x \<otimes> \<zero> \<oplus> x \<otimes> \<zero> = x \<otimes> \<zero> \<oplus> \<zero>" .
ballarin@20318
   488
  with R show ?thesis by (simp del: r_zero)
ballarin@20318
   489
qed
ballarin@20318
   490
ballarin@20318
   491
lemma (in ring) l_minus:
ballarin@20318
   492
  "[| x \<in> carrier R; y \<in> carrier R |] ==> \<ominus> x \<otimes> y = \<ominus> (x \<otimes> y)"
ballarin@20318
   493
proof -
ballarin@20318
   494
  assume R: "x \<in> carrier R" "y \<in> carrier R"
ballarin@20318
   495
  then have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y = (\<ominus> x \<oplus> x) \<otimes> y" by (simp add: l_distr)
ballarin@20318
   496
  also from R have "... = \<zero>" by (simp add: l_neg l_null)
ballarin@20318
   497
  finally have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y = \<zero>" .
ballarin@20318
   498
  with R have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y \<oplus> \<ominus> (x \<otimes> y) = \<zero> \<oplus> \<ominus> (x \<otimes> y)" by simp
ballarin@21896
   499
  with R show ?thesis by (simp add: a_assoc r_neg)
ballarin@20318
   500
qed
ballarin@20318
   501
ballarin@20318
   502
lemma (in ring) r_minus:
ballarin@20318
   503
  "[| x \<in> carrier R; y \<in> carrier R |] ==> x \<otimes> \<ominus> y = \<ominus> (x \<otimes> y)"
ballarin@20318
   504
proof -
ballarin@20318
   505
  assume R: "x \<in> carrier R" "y \<in> carrier R"
ballarin@20318
   506
  then have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y = x \<otimes> (\<ominus> y \<oplus> y)" by (simp add: r_distr)
ballarin@20318
   507
  also from R have "... = \<zero>" by (simp add: l_neg r_null)
ballarin@20318
   508
  finally have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y = \<zero>" .
ballarin@20318
   509
  with R have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y \<oplus> \<ominus> (x \<otimes> y) = \<zero> \<oplus> \<ominus> (x \<otimes> y)" by simp
ballarin@20318
   510
  with R show ?thesis by (simp add: a_assoc r_neg )
ballarin@20318
   511
qed
ballarin@20318
   512
ballarin@23957
   513
lemma (in abelian_group) minus_eq:
ballarin@23957
   514
  "[| x \<in> carrier G; y \<in> carrier G |] ==> x \<ominus> y = x \<oplus> \<ominus> y"
ballarin@20318
   515
  by (simp only: a_minus_def)
ballarin@20318
   516
ballarin@20318
   517
text {* Setup algebra method:
ballarin@20318
   518
  compute distributive normal form in locale contexts *}
ballarin@20318
   519
ballarin@20318
   520
use "ringsimp.ML"
ballarin@20318
   521
ballarin@20318
   522
setup Algebra.setup
ballarin@20318
   523
ballarin@20318
   524
lemmas (in ring) ring_simprules
ballarin@20318
   525
  [algebra ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
ballarin@20318
   526
  a_closed zero_closed a_inv_closed minus_closed m_closed one_closed
ballarin@20318
   527
  a_assoc l_zero l_neg a_comm m_assoc l_one l_distr minus_eq
ballarin@20318
   528
  r_zero r_neg r_neg2 r_neg1 minus_add minus_minus minus_zero
ballarin@20318
   529
  a_lcomm r_distr l_null r_null l_minus r_minus
ballarin@20318
   530
ballarin@20318
   531
lemmas (in cring)
ballarin@20318
   532
  [algebra del: ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
ballarin@20318
   533
  _
ballarin@20318
   534
ballarin@20318
   535
lemmas (in cring) cring_simprules
ballarin@20318
   536
  [algebra add: cring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
ballarin@20318
   537
  a_closed zero_closed a_inv_closed minus_closed m_closed one_closed
ballarin@20318
   538
  a_assoc l_zero l_neg a_comm m_assoc l_one l_distr m_comm minus_eq
ballarin@20318
   539
  r_zero r_neg r_neg2 r_neg1 minus_add minus_minus minus_zero
ballarin@20318
   540
  a_lcomm m_lcomm r_distr l_null r_null l_minus r_minus
ballarin@20318
   541
ballarin@20318
   542
ballarin@20318
   543
lemma (in cring) nat_pow_zero:
ballarin@20318
   544
  "(n::nat) ~= 0 ==> \<zero> (^) n = \<zero>"
ballarin@20318
   545
  by (induct n) simp_all
ballarin@20318
   546
ballarin@20318
   547
lemma (in ring) one_zeroD:
ballarin@20318
   548
  assumes onezero: "\<one> = \<zero>"
ballarin@20318
   549
  shows "carrier R = {\<zero>}"
ballarin@20318
   550
proof (rule, rule)
ballarin@20318
   551
  fix x
ballarin@20318
   552
  assume xcarr: "x \<in> carrier R"
ballarin@20318
   553
  from xcarr
ballarin@20318
   554
      have "x = x \<otimes> \<one>" by simp
ballarin@20318
   555
  from this and onezero
ballarin@20318
   556
      have "x = x \<otimes> \<zero>" by simp
ballarin@20318
   557
  from this and xcarr
ballarin@20318
   558
      have "x = \<zero>" by simp
ballarin@20318
   559
  thus "x \<in> {\<zero>}" by fast
ballarin@20318
   560
qed fast
ballarin@20318
   561
ballarin@20318
   562
lemma (in ring) one_zeroI:
ballarin@20318
   563
  assumes carrzero: "carrier R = {\<zero>}"
ballarin@20318
   564
  shows "\<one> = \<zero>"
ballarin@20318
   565
proof -
ballarin@20318
   566
  from one_closed and carrzero
ballarin@20318
   567
      show "\<one> = \<zero>" by simp
ballarin@20318
   568
qed
ballarin@20318
   569
ballarin@27717
   570
lemma (in ring) carrier_one_zero:
ballarin@20318
   571
  shows "(carrier R = {\<zero>}) = (\<one> = \<zero>)"
ballarin@20318
   572
  by (rule, erule one_zeroI, erule one_zeroD)
ballarin@20318
   573
ballarin@27717
   574
lemma (in ring) carrier_one_not_zero:
ballarin@20318
   575
  shows "(carrier R \<noteq> {\<zero>}) = (\<one> \<noteq> \<zero>)"
ballarin@27717
   576
  by (simp add: carrier_one_zero)
ballarin@20318
   577
ballarin@20318
   578
text {* Two examples for use of method algebra *}
ballarin@20318
   579
ballarin@20318
   580
lemma
ballarin@27611
   581
  fixes R (structure) and S (structure)
ballarin@27611
   582
  assumes "ring R" "cring S"
ballarin@27611
   583
  assumes RS: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier S" "d \<in> carrier S"
ballarin@27611
   584
  shows "a \<oplus> \<ominus> (a \<oplus> \<ominus> b) = b & c \<otimes>\<^bsub>S\<^esub> d = d \<otimes>\<^bsub>S\<^esub> c"
ballarin@27611
   585
proof -
ballarin@29237
   586
  interpret ring R by fact
ballarin@29237
   587
  interpret cring S by fact
ballarin@27611
   588
ML_val {* Algebra.print_structures @{context} *}
ballarin@27611
   589
  from RS show ?thesis by algebra
ballarin@27611
   590
qed
ballarin@20318
   591
ballarin@20318
   592
lemma
ballarin@27611
   593
  fixes R (structure)
ballarin@27611
   594
  assumes "ring R"
ballarin@27611
   595
  assumes R: "a \<in> carrier R" "b \<in> carrier R"
ballarin@27611
   596
  shows "a \<ominus> (a \<ominus> b) = b"
ballarin@27611
   597
proof -
ballarin@29237
   598
  interpret ring R by fact
ballarin@27611
   599
  from R show ?thesis by algebra
ballarin@27611
   600
qed
ballarin@20318
   601
wenzelm@35849
   602
ballarin@20318
   603
subsubsection {* Sums over Finite Sets *}
ballarin@20318
   604
ballarin@27717
   605
lemma (in ring) finsum_ldistr:
ballarin@20318
   606
  "[| finite A; a \<in> carrier R; f \<in> A -> carrier R |] ==>
ballarin@20318
   607
   finsum R f A \<otimes> a = finsum R (%i. f i \<otimes> a) A"
berghofe@22265
   608
proof (induct set: finite)
ballarin@20318
   609
  case empty then show ?case by simp
ballarin@20318
   610
next
ballarin@20318
   611
  case (insert x F) then show ?case by (simp add: Pi_def l_distr)
ballarin@20318
   612
qed
ballarin@20318
   613
ballarin@27717
   614
lemma (in ring) finsum_rdistr:
ballarin@20318
   615
  "[| finite A; a \<in> carrier R; f \<in> A -> carrier R |] ==>
ballarin@20318
   616
   a \<otimes> finsum R f A = finsum R (%i. a \<otimes> f i) A"
berghofe@22265
   617
proof (induct set: finite)
ballarin@20318
   618
  case empty then show ?case by simp
ballarin@20318
   619
next
ballarin@20318
   620
  case (insert x F) then show ?case by (simp add: Pi_def r_distr)
ballarin@20318
   621
qed
ballarin@20318
   622
ballarin@20318
   623
ballarin@20318
   624
subsection {* Integral Domains *}
ballarin@20318
   625
ballarin@20318
   626
lemma (in "domain") zero_not_one [simp]:
ballarin@20318
   627
  "\<zero> ~= \<one>"
ballarin@20318
   628
  by (rule not_sym) simp
ballarin@20318
   629
ballarin@20318
   630
lemma (in "domain") integral_iff: (* not by default a simp rule! *)
ballarin@20318
   631
  "[| a \<in> carrier R; b \<in> carrier R |] ==> (a \<otimes> b = \<zero>) = (a = \<zero> | b = \<zero>)"
ballarin@20318
   632
proof
ballarin@20318
   633
  assume "a \<in> carrier R" "b \<in> carrier R" "a \<otimes> b = \<zero>"
ballarin@20318
   634
  then show "a = \<zero> | b = \<zero>" by (simp add: integral)
ballarin@20318
   635
next
ballarin@20318
   636
  assume "a \<in> carrier R" "b \<in> carrier R" "a = \<zero> | b = \<zero>"
ballarin@20318
   637
  then show "a \<otimes> b = \<zero>" by auto
ballarin@20318
   638
qed
ballarin@20318
   639
ballarin@20318
   640
lemma (in "domain") m_lcancel:
ballarin@20318
   641
  assumes prem: "a ~= \<zero>"
ballarin@20318
   642
    and R: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier R"
ballarin@20318
   643
  shows "(a \<otimes> b = a \<otimes> c) = (b = c)"
ballarin@20318
   644
proof
ballarin@20318
   645
  assume eq: "a \<otimes> b = a \<otimes> c"
ballarin@20318
   646
  with R have "a \<otimes> (b \<ominus> c) = \<zero>" by algebra
ballarin@20318
   647
  with R have "a = \<zero> | (b \<ominus> c) = \<zero>" by (simp add: integral_iff)
ballarin@20318
   648
  with prem and R have "b \<ominus> c = \<zero>" by auto 
ballarin@20318
   649
  with R have "b = b \<ominus> (b \<ominus> c)" by algebra 
ballarin@20318
   650
  also from R have "b \<ominus> (b \<ominus> c) = c" by algebra
ballarin@20318
   651
  finally show "b = c" .
ballarin@20318
   652
next
ballarin@20318
   653
  assume "b = c" then show "a \<otimes> b = a \<otimes> c" by simp
ballarin@20318
   654
qed
ballarin@20318
   655
ballarin@20318
   656
lemma (in "domain") m_rcancel:
ballarin@20318
   657
  assumes prem: "a ~= \<zero>"
ballarin@20318
   658
    and R: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier R"
ballarin@20318
   659
  shows conc: "(b \<otimes> a = c \<otimes> a) = (b = c)"
ballarin@20318
   660
proof -
ballarin@20318
   661
  from prem and R have "(a \<otimes> b = a \<otimes> c) = (b = c)" by (rule m_lcancel)
ballarin@20318
   662
  with R show ?thesis by algebra
ballarin@20318
   663
qed
ballarin@20318
   664
ballarin@20318
   665
ballarin@20318
   666
subsection {* Fields *}
ballarin@20318
   667
ballarin@20318
   668
text {* Field would not need to be derived from domain, the properties
ballarin@20318
   669
  for domain follow from the assumptions of field *}
ballarin@20318
   670
lemma (in cring) cring_fieldI:
ballarin@20318
   671
  assumes field_Units: "Units R = carrier R - {\<zero>}"
ballarin@20318
   672
  shows "field R"
haftmann@28823
   673
proof
ballarin@20318
   674
  from field_Units
ballarin@20318
   675
  have a: "\<zero> \<notin> Units R" by fast
ballarin@20318
   676
  have "\<one> \<in> Units R" by fast
ballarin@20318
   677
  from this and a
ballarin@20318
   678
  show "\<one> \<noteq> \<zero>" by force
ballarin@20318
   679
next
ballarin@20318
   680
  fix a b
ballarin@20318
   681
  assume acarr: "a \<in> carrier R"
ballarin@20318
   682
    and bcarr: "b \<in> carrier R"
ballarin@20318
   683
    and ab: "a \<otimes> b = \<zero>"
ballarin@20318
   684
  show "a = \<zero> \<or> b = \<zero>"
ballarin@20318
   685
  proof (cases "a = \<zero>", simp)
ballarin@20318
   686
    assume "a \<noteq> \<zero>"
ballarin@20318
   687
    from this and field_Units and acarr
ballarin@20318
   688
    have aUnit: "a \<in> Units R" by fast
ballarin@20318
   689
    from bcarr
ballarin@20318
   690
    have "b = \<one> \<otimes> b" by algebra
ballarin@20318
   691
    also from aUnit acarr
ballarin@20318
   692
    have "... = (inv a \<otimes> a) \<otimes> b" by (simp add: Units_l_inv)
ballarin@20318
   693
    also from acarr bcarr aUnit[THEN Units_inv_closed]
ballarin@20318
   694
    have "... = (inv a) \<otimes> (a \<otimes> b)" by algebra
ballarin@20318
   695
    also from ab and acarr bcarr aUnit
ballarin@20318
   696
    have "... = (inv a) \<otimes> \<zero>" by simp
ballarin@20318
   697
    also from aUnit[THEN Units_inv_closed]
ballarin@20318
   698
    have "... = \<zero>" by algebra
ballarin@20318
   699
    finally
ballarin@20318
   700
    have "b = \<zero>" .
ballarin@20318
   701
    thus "a = \<zero> \<or> b = \<zero>" by simp
ballarin@20318
   702
  qed
wenzelm@23350
   703
qed (rule field_Units)
ballarin@20318
   704
ballarin@20318
   705
text {* Another variant to show that something is a field *}
ballarin@20318
   706
lemma (in cring) cring_fieldI2:
ballarin@20318
   707
  assumes notzero: "\<zero> \<noteq> \<one>"
ballarin@20318
   708
  and invex: "\<And>a. \<lbrakk>a \<in> carrier R; a \<noteq> \<zero>\<rbrakk> \<Longrightarrow> \<exists>b\<in>carrier R. a \<otimes> b = \<one>"
ballarin@20318
   709
  shows "field R"
ballarin@20318
   710
  apply (rule cring_fieldI, simp add: Units_def)
ballarin@20318
   711
  apply (rule, clarsimp)
ballarin@20318
   712
  apply (simp add: notzero)
ballarin@20318
   713
proof (clarsimp)
ballarin@20318
   714
  fix x
ballarin@20318
   715
  assume xcarr: "x \<in> carrier R"
ballarin@20318
   716
    and "x \<noteq> \<zero>"
ballarin@20318
   717
  from this
ballarin@20318
   718
  have "\<exists>y\<in>carrier R. x \<otimes> y = \<one>" by (rule invex)
ballarin@20318
   719
  from this
ballarin@20318
   720
  obtain y
ballarin@20318
   721
    where ycarr: "y \<in> carrier R"
ballarin@20318
   722
    and xy: "x \<otimes> y = \<one>"
ballarin@20318
   723
    by fast
ballarin@20318
   724
  from xy xcarr ycarr have "y \<otimes> x = \<one>" by (simp add: m_comm)
ballarin@20318
   725
  from ycarr and this and xy
ballarin@20318
   726
  show "\<exists>y\<in>carrier R. y \<otimes> x = \<one> \<and> x \<otimes> y = \<one>" by fast
ballarin@20318
   727
qed
ballarin@20318
   728
ballarin@20318
   729
ballarin@20318
   730
subsection {* Morphisms *}
ballarin@20318
   731
wenzelm@35847
   732
definition
ballarin@20318
   733
  ring_hom :: "[('a, 'm) ring_scheme, ('b, 'n) ring_scheme] => ('a => 'b) set"
wenzelm@35848
   734
  where "ring_hom R S =
wenzelm@35847
   735
    {h. h \<in> carrier R -> carrier S &
ballarin@20318
   736
      (ALL x y. x \<in> carrier R & y \<in> carrier R -->
wenzelm@35847
   737
        h (x \<otimes>\<^bsub>R\<^esub> y) = h x \<otimes>\<^bsub>S\<^esub> h y & h (x \<oplus>\<^bsub>R\<^esub> y) = h x \<oplus>\<^bsub>S\<^esub> h y) &
wenzelm@35847
   738
      h \<one>\<^bsub>R\<^esub> = \<one>\<^bsub>S\<^esub>}"
ballarin@20318
   739
ballarin@20318
   740
lemma ring_hom_memI:
ballarin@20318
   741
  fixes R (structure) and S (structure)
ballarin@20318
   742
  assumes hom_closed: "!!x. x \<in> carrier R ==> h x \<in> carrier S"
ballarin@20318
   743
    and hom_mult: "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==>
ballarin@20318
   744
      h (x \<otimes> y) = h x \<otimes>\<^bsub>S\<^esub> h y"
ballarin@20318
   745
    and hom_add: "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==>
ballarin@20318
   746
      h (x \<oplus> y) = h x \<oplus>\<^bsub>S\<^esub> h y"
ballarin@20318
   747
    and hom_one: "h \<one> = \<one>\<^bsub>S\<^esub>"
ballarin@20318
   748
  shows "h \<in> ring_hom R S"
ballarin@27714
   749
  by (auto simp add: ring_hom_def assms Pi_def)
ballarin@20318
   750
ballarin@20318
   751
lemma ring_hom_closed:
ballarin@20318
   752
  "[| h \<in> ring_hom R S; x \<in> carrier R |] ==> h x \<in> carrier S"
ballarin@20318
   753
  by (auto simp add: ring_hom_def funcset_mem)
ballarin@20318
   754
ballarin@20318
   755
lemma ring_hom_mult:
ballarin@20318
   756
  fixes R (structure) and S (structure)
ballarin@20318
   757
  shows
ballarin@20318
   758
    "[| h \<in> ring_hom R S; x \<in> carrier R; y \<in> carrier R |] ==>
ballarin@20318
   759
    h (x \<otimes> y) = h x \<otimes>\<^bsub>S\<^esub> h y"
ballarin@20318
   760
    by (simp add: ring_hom_def)
ballarin@20318
   761
ballarin@20318
   762
lemma ring_hom_add:
ballarin@20318
   763
  fixes R (structure) and S (structure)
ballarin@20318
   764
  shows
ballarin@20318
   765
    "[| h \<in> ring_hom R S; x \<in> carrier R; y \<in> carrier R |] ==>
ballarin@20318
   766
    h (x \<oplus> y) = h x \<oplus>\<^bsub>S\<^esub> h y"
ballarin@20318
   767
    by (simp add: ring_hom_def)
ballarin@20318
   768
ballarin@20318
   769
lemma ring_hom_one:
ballarin@20318
   770
  fixes R (structure) and S (structure)
ballarin@20318
   771
  shows "h \<in> ring_hom R S ==> h \<one> = \<one>\<^bsub>S\<^esub>"
ballarin@20318
   772
  by (simp add: ring_hom_def)
ballarin@20318
   773
ballarin@29237
   774
locale ring_hom_cring = R: cring R + S: cring S
ballarin@29237
   775
    for R (structure) and S (structure) +
ballarin@20318
   776
  fixes h
ballarin@20318
   777
  assumes homh [simp, intro]: "h \<in> ring_hom R S"
ballarin@20318
   778
  notes hom_closed [simp, intro] = ring_hom_closed [OF homh]
ballarin@20318
   779
    and hom_mult [simp] = ring_hom_mult [OF homh]
ballarin@20318
   780
    and hom_add [simp] = ring_hom_add [OF homh]
ballarin@20318
   781
    and hom_one [simp] = ring_hom_one [OF homh]
ballarin@20318
   782
ballarin@20318
   783
lemma (in ring_hom_cring) hom_zero [simp]:
ballarin@20318
   784
  "h \<zero> = \<zero>\<^bsub>S\<^esub>"
ballarin@20318
   785
proof -
ballarin@20318
   786
  have "h \<zero> \<oplus>\<^bsub>S\<^esub> h \<zero> = h \<zero> \<oplus>\<^bsub>S\<^esub> \<zero>\<^bsub>S\<^esub>"
ballarin@20318
   787
    by (simp add: hom_add [symmetric] del: hom_add)
ballarin@20318
   788
  then show ?thesis by (simp del: S.r_zero)
ballarin@20318
   789
qed
ballarin@20318
   790
ballarin@20318
   791
lemma (in ring_hom_cring) hom_a_inv [simp]:
ballarin@20318
   792
  "x \<in> carrier R ==> h (\<ominus> x) = \<ominus>\<^bsub>S\<^esub> h x"
ballarin@20318
   793
proof -
ballarin@20318
   794
  assume R: "x \<in> carrier R"
ballarin@20318
   795
  then have "h x \<oplus>\<^bsub>S\<^esub> h (\<ominus> x) = h x \<oplus>\<^bsub>S\<^esub> (\<ominus>\<^bsub>S\<^esub> h x)"
ballarin@20318
   796
    by (simp add: hom_add [symmetric] R.r_neg S.r_neg del: hom_add)
ballarin@20318
   797
  with R show ?thesis by simp
ballarin@20318
   798
qed
ballarin@20318
   799
ballarin@20318
   800
lemma (in ring_hom_cring) hom_finsum [simp]:
ballarin@20318
   801
  "[| finite A; f \<in> A -> carrier R |] ==>
ballarin@20318
   802
  h (finsum R f A) = finsum S (h o f) A"
berghofe@22265
   803
proof (induct set: finite)
ballarin@20318
   804
  case empty then show ?case by simp
ballarin@20318
   805
next
ballarin@20318
   806
  case insert then show ?case by (simp add: Pi_def)
ballarin@20318
   807
qed
ballarin@20318
   808
ballarin@20318
   809
lemma (in ring_hom_cring) hom_finprod:
ballarin@20318
   810
  "[| finite A; f \<in> A -> carrier R |] ==>
ballarin@20318
   811
  h (finprod R f A) = finprod S (h o f) A"
berghofe@22265
   812
proof (induct set: finite)
ballarin@20318
   813
  case empty then show ?case by simp
ballarin@20318
   814
next
ballarin@20318
   815
  case insert then show ?case by (simp add: Pi_def)
ballarin@20318
   816
qed
ballarin@20318
   817
ballarin@20318
   818
declare ring_hom_cring.hom_finprod [simp]
ballarin@20318
   819
ballarin@20318
   820
lemma id_ring_hom [simp]:
ballarin@20318
   821
  "id \<in> ring_hom R R"
ballarin@20318
   822
  by (auto intro!: ring_hom_memI)
ballarin@20318
   823
ballarin@20318
   824
end