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