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