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