src/HOL/Algebra/Ring.thy
author haftmann
Mon Mar 01 13:40:23 2010 +0100 (2010-03-01)
changeset 35416 d8d7d1b785af
parent 35054 a5db9779b026
child 35847 19f1f7066917
permissions -rw-r--r--
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
     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 definition finsum :: "[('b, 'm) ring_scheme, 'a => 'b, 'a set] => 'b" where
   202   "finsum G f A == finprod (| carrier = carrier G,
   203      mult = add G, one = zero G |) f A"
   204 
   205 syntax
   206   "_finsum" :: "index => idt => 'a set => 'b => 'b"
   207       ("(3\<Oplus>__:_. _)" [1000, 0, 51, 10] 10)
   208 syntax (xsymbols)
   209   "_finsum" :: "index => idt => 'a set => 'b => 'b"
   210       ("(3\<Oplus>__\<in>_. _)" [1000, 0, 51, 10] 10)
   211 syntax (HTML output)
   212   "_finsum" :: "index => idt => 'a set => 'b => 'b"
   213       ("(3\<Oplus>__\<in>_. _)" [1000, 0, 51, 10] 10)
   214 translations
   215   "\<Oplus>\<index>i:A. b" == "CONST finsum \<struct>\<index> (%i. b) A"
   216   -- {* Beware of argument permutation! *}
   217 
   218 context abelian_monoid begin
   219 
   220 (*
   221   lemmas finsum_empty [simp] =
   222     comm_monoid.finprod_empty [OF a_comm_monoid, simplified]
   223   is dangeous, because attributes (like simplified) are applied upon opening
   224   the locale, simplified refers to the simpset at that time!!!
   225 
   226   lemmas finsum_empty [simp] =
   227     abelian_monoid.finprod_empty [OF a_abelian_monoid, folded finsum_def,
   228       simplified monoid_record_simps]
   229   makes the locale slow, because proofs are repeated for every
   230   "lemma (in abelian_monoid)" command.
   231   When lemma is used time in UnivPoly.thy from beginning to UP_cring goes down
   232   from 110 secs to 60 secs.
   233 *)
   234 
   235 lemma finsum_empty [simp]:
   236   "finsum G f {} = \<zero>"
   237   by (rule comm_monoid.finprod_empty [OF a_comm_monoid,
   238     folded finsum_def, simplified monoid_record_simps])
   239 
   240 lemma finsum_insert [simp]:
   241   "[| finite F; a \<notin> F; f \<in> F -> carrier G; f a \<in> carrier G |]
   242   ==> finsum G f (insert a F) = f a \<oplus> finsum G f F"
   243   by (rule comm_monoid.finprod_insert [OF a_comm_monoid,
   244     folded finsum_def, simplified monoid_record_simps])
   245 
   246 lemma finsum_zero [simp]:
   247   "finite A ==> (\<Oplus>i\<in>A. \<zero>) = \<zero>"
   248   by (rule comm_monoid.finprod_one [OF a_comm_monoid, folded finsum_def,
   249     simplified monoid_record_simps])
   250 
   251 lemma finsum_closed [simp]:
   252   fixes A
   253   assumes fin: "finite A" and f: "f \<in> A -> carrier G" 
   254   shows "finsum G f A \<in> carrier G"
   255   apply (rule comm_monoid.finprod_closed [OF a_comm_monoid,
   256     folded finsum_def, simplified monoid_record_simps])
   257    apply (rule fin)
   258   apply (rule f)
   259   done
   260 
   261 lemma finsum_Un_Int:
   262   "[| finite A; finite B; g \<in> A -> carrier G; g \<in> B -> carrier G |] ==>
   263      finsum G g (A Un B) \<oplus> finsum G g (A Int B) =
   264      finsum G g A \<oplus> finsum G g B"
   265   by (rule comm_monoid.finprod_Un_Int [OF a_comm_monoid,
   266     folded finsum_def, simplified monoid_record_simps])
   267 
   268 lemma finsum_Un_disjoint:
   269   "[| finite A; finite B; A Int B = {};
   270       g \<in> A -> carrier G; g \<in> B -> carrier G |]
   271    ==> finsum G g (A Un B) = finsum G g A \<oplus> finsum G g B"
   272   by (rule comm_monoid.finprod_Un_disjoint [OF a_comm_monoid,
   273     folded finsum_def, simplified monoid_record_simps])
   274 
   275 lemma finsum_addf:
   276   "[| finite A; f \<in> A -> carrier G; g \<in> A -> carrier G |] ==>
   277    finsum G (%x. f x \<oplus> g x) A = (finsum G f A \<oplus> finsum G g A)"
   278   by (rule comm_monoid.finprod_multf [OF a_comm_monoid,
   279     folded finsum_def, simplified monoid_record_simps])
   280 
   281 lemma finsum_cong':
   282   "[| A = B; g : B -> carrier G;
   283       !!i. i : B ==> f i = g i |] ==> finsum G f A = finsum G g B"
   284   by (rule comm_monoid.finprod_cong' [OF a_comm_monoid,
   285     folded finsum_def, simplified monoid_record_simps]) auto
   286 
   287 lemma finsum_0 [simp]:
   288   "f : {0::nat} -> carrier G ==> finsum G f {..0} = f 0"
   289   by (rule comm_monoid.finprod_0 [OF a_comm_monoid, folded finsum_def,
   290     simplified monoid_record_simps])
   291 
   292 lemma finsum_Suc [simp]:
   293   "f : {..Suc n} -> carrier G ==>
   294    finsum G f {..Suc n} = (f (Suc n) \<oplus> finsum G f {..n})"
   295   by (rule comm_monoid.finprod_Suc [OF a_comm_monoid, folded finsum_def,
   296     simplified monoid_record_simps])
   297 
   298 lemma finsum_Suc2:
   299   "f : {..Suc n} -> carrier G ==>
   300    finsum G f {..Suc n} = (finsum G (%i. f (Suc i)) {..n} \<oplus> f 0)"
   301   by (rule comm_monoid.finprod_Suc2 [OF a_comm_monoid, folded finsum_def,
   302     simplified monoid_record_simps])
   303 
   304 lemma finsum_add [simp]:
   305   "[| f : {..n} -> carrier G; g : {..n} -> carrier G |] ==>
   306      finsum G (%i. f i \<oplus> g i) {..n::nat} =
   307      finsum G f {..n} \<oplus> finsum G g {..n}"
   308   by (rule comm_monoid.finprod_mult [OF a_comm_monoid, folded finsum_def,
   309     simplified monoid_record_simps])
   310 
   311 lemma finsum_cong:
   312   "[| A = B; f : B -> carrier G;
   313       !!i. i : B =simp=> f i = g i |] ==> finsum G f A = finsum G g B"
   314   by (rule comm_monoid.finprod_cong [OF a_comm_monoid, folded finsum_def,
   315     simplified monoid_record_simps]) (auto simp add: simp_implies_def)
   316 
   317 text {*Usually, if this rule causes a failed congruence proof error,
   318    the reason is that the premise @{text "g \<in> B -> carrier G"} cannot be shown.
   319    Adding @{thm [source] Pi_def} to the simpset is often useful. *}
   320 
   321 lemma finsum_reindex:
   322   assumes fin: "finite A"
   323     shows "f : (h ` A) \<rightarrow> carrier G \<Longrightarrow> 
   324         inj_on h A ==> finsum G f (h ` A) = finsum G (%x. f (h x)) A"
   325   using fin apply induct
   326   apply (auto simp add: finsum_insert Pi_def)
   327 done
   328 
   329 (* The following is wrong.  Needed is the equivalent of (^) for addition,
   330   or indeed the canonical embedding from Nat into the monoid.
   331 
   332 lemma finsum_const:
   333   assumes fin [simp]: "finite A"
   334       and a [simp]: "a : carrier G"
   335     shows "finsum G (%x. a) A = a (^) card A"
   336   using fin apply induct
   337   apply force
   338   apply (subst finsum_insert)
   339   apply auto
   340   apply (force simp add: Pi_def)
   341   apply (subst m_comm)
   342   apply auto
   343 done
   344 *)
   345 
   346 (* By Jesus Aransay. *)
   347 
   348 lemma finsum_singleton:
   349   assumes i_in_A: "i \<in> A" and fin_A: "finite A" and f_Pi: "f \<in> A \<rightarrow> carrier G"
   350   shows "(\<Oplus>j\<in>A. if i = j then f j else \<zero>) = f i"
   351   using i_in_A finsum_insert [of "A - {i}" i "(\<lambda>j. if i = j then f j else \<zero>)"]
   352     fin_A f_Pi finsum_zero [of "A - {i}"]
   353     finsum_cong [of "A - {i}" "A - {i}" "(\<lambda>j. if i = j then f j else \<zero>)" "(\<lambda>i. \<zero>)"] 
   354   unfolding Pi_def simp_implies_def by (force simp add: insert_absorb)
   355 
   356 end
   357 
   358 
   359 subsection {* Rings: Basic Definitions *}
   360 
   361 locale ring = abelian_group R + monoid R for R (structure) +
   362   assumes l_distr: "[| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   363       ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   364     and r_distr: "[| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   365       ==> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
   366 
   367 locale cring = ring + comm_monoid R
   368 
   369 locale "domain" = cring +
   370   assumes one_not_zero [simp]: "\<one> ~= \<zero>"
   371     and integral: "[| a \<otimes> b = \<zero>; a \<in> carrier R; b \<in> carrier R |] ==>
   372                   a = \<zero> | b = \<zero>"
   373 
   374 locale field = "domain" +
   375   assumes field_Units: "Units R = carrier R - {\<zero>}"
   376 
   377 
   378 subsection {* Rings *}
   379 
   380 lemma ringI:
   381   fixes R (structure)
   382   assumes abelian_group: "abelian_group R"
   383     and monoid: "monoid R"
   384     and l_distr: "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   385       ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   386     and r_distr: "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   387       ==> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
   388   shows "ring R"
   389   by (auto intro: ring.intro
   390     abelian_group.axioms ring_axioms.intro assms)
   391 
   392 lemma (in ring) is_abelian_group:
   393   "abelian_group R"
   394   ..
   395 
   396 lemma (in ring) is_monoid:
   397   "monoid R"
   398   by (auto intro!: monoidI m_assoc)
   399 
   400 lemma (in ring) is_ring:
   401   "ring R"
   402   by (rule ring_axioms)
   403 
   404 lemmas ring_record_simps = monoid_record_simps ring.simps
   405 
   406 lemma cringI:
   407   fixes R (structure)
   408   assumes abelian_group: "abelian_group R"
   409     and comm_monoid: "comm_monoid R"
   410     and l_distr: "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   411       ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   412   shows "cring R"
   413 proof (intro cring.intro ring.intro)
   414   show "ring_axioms R"
   415     -- {* Right-distributivity follows from left-distributivity and
   416           commutativity. *}
   417   proof (rule ring_axioms.intro)
   418     fix x y z
   419     assume R: "x \<in> carrier R" "y \<in> carrier R" "z \<in> carrier R"
   420     note [simp] = comm_monoid.axioms [OF comm_monoid]
   421       abelian_group.axioms [OF abelian_group]
   422       abelian_monoid.a_closed
   423         
   424     from R have "z \<otimes> (x \<oplus> y) = (x \<oplus> y) \<otimes> z"
   425       by (simp add: comm_monoid.m_comm [OF comm_monoid.intro])
   426     also from R have "... = x \<otimes> z \<oplus> y \<otimes> z" by (simp add: l_distr)
   427     also from R have "... = z \<otimes> x \<oplus> z \<otimes> y"
   428       by (simp add: comm_monoid.m_comm [OF comm_monoid.intro])
   429     finally show "z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y" .
   430   qed (rule l_distr)
   431 qed (auto intro: cring.intro
   432   abelian_group.axioms comm_monoid.axioms ring_axioms.intro assms)
   433 
   434 (*
   435 lemma (in cring) is_comm_monoid:
   436   "comm_monoid R"
   437   by (auto intro!: comm_monoidI m_assoc m_comm)
   438 *)
   439 
   440 lemma (in cring) is_cring:
   441   "cring R" by (rule cring_axioms)
   442 
   443 
   444 subsubsection {* Normaliser for Rings *}
   445 
   446 lemma (in abelian_group) r_neg2:
   447   "[| x \<in> carrier G; y \<in> carrier G |] ==> x \<oplus> (\<ominus> x \<oplus> y) = y"
   448 proof -
   449   assume G: "x \<in> carrier G" "y \<in> carrier G"
   450   then have "(x \<oplus> \<ominus> x) \<oplus> y = y"
   451     by (simp only: r_neg l_zero)
   452   with G show ?thesis 
   453     by (simp add: a_ac)
   454 qed
   455 
   456 lemma (in abelian_group) r_neg1:
   457   "[| x \<in> carrier G; y \<in> carrier G |] ==> \<ominus> x \<oplus> (x \<oplus> y) = y"
   458 proof -
   459   assume G: "x \<in> carrier G" "y \<in> carrier G"
   460   then have "(\<ominus> x \<oplus> x) \<oplus> y = y" 
   461     by (simp only: l_neg l_zero)
   462   with G show ?thesis by (simp add: a_ac)
   463 qed
   464 
   465 text {* 
   466   The following proofs are from Jacobson, Basic Algebra I, pp.~88--89
   467 *}
   468 
   469 lemma (in ring) l_null [simp]:
   470   "x \<in> carrier R ==> \<zero> \<otimes> x = \<zero>"
   471 proof -
   472   assume R: "x \<in> carrier R"
   473   then have "\<zero> \<otimes> x \<oplus> \<zero> \<otimes> x = (\<zero> \<oplus> \<zero>) \<otimes> x"
   474     by (simp add: l_distr del: l_zero r_zero)
   475   also from R have "... = \<zero> \<otimes> x \<oplus> \<zero>" by simp
   476   finally have "\<zero> \<otimes> x \<oplus> \<zero> \<otimes> x = \<zero> \<otimes> x \<oplus> \<zero>" .
   477   with R show ?thesis by (simp del: r_zero)
   478 qed
   479 
   480 lemma (in ring) r_null [simp]:
   481   "x \<in> carrier R ==> x \<otimes> \<zero> = \<zero>"
   482 proof -
   483   assume R: "x \<in> carrier R"
   484   then have "x \<otimes> \<zero> \<oplus> x \<otimes> \<zero> = x \<otimes> (\<zero> \<oplus> \<zero>)"
   485     by (simp add: r_distr del: l_zero r_zero)
   486   also from R have "... = x \<otimes> \<zero> \<oplus> \<zero>" by simp
   487   finally have "x \<otimes> \<zero> \<oplus> x \<otimes> \<zero> = x \<otimes> \<zero> \<oplus> \<zero>" .
   488   with R show ?thesis by (simp del: r_zero)
   489 qed
   490 
   491 lemma (in ring) l_minus:
   492   "[| x \<in> carrier R; y \<in> carrier R |] ==> \<ominus> x \<otimes> y = \<ominus> (x \<otimes> y)"
   493 proof -
   494   assume R: "x \<in> carrier R" "y \<in> carrier R"
   495   then have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y = (\<ominus> x \<oplus> x) \<otimes> y" by (simp add: l_distr)
   496   also from R have "... = \<zero>" by (simp add: l_neg l_null)
   497   finally have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y = \<zero>" .
   498   with R have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y \<oplus> \<ominus> (x \<otimes> y) = \<zero> \<oplus> \<ominus> (x \<otimes> y)" by simp
   499   with R show ?thesis by (simp add: a_assoc r_neg)
   500 qed
   501 
   502 lemma (in ring) r_minus:
   503   "[| x \<in> carrier R; y \<in> carrier R |] ==> x \<otimes> \<ominus> y = \<ominus> (x \<otimes> y)"
   504 proof -
   505   assume R: "x \<in> carrier R" "y \<in> carrier R"
   506   then have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y = x \<otimes> (\<ominus> y \<oplus> y)" by (simp add: r_distr)
   507   also from R have "... = \<zero>" by (simp add: l_neg r_null)
   508   finally have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y = \<zero>" .
   509   with R have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y \<oplus> \<ominus> (x \<otimes> y) = \<zero> \<oplus> \<ominus> (x \<otimes> y)" by simp
   510   with R show ?thesis by (simp add: a_assoc r_neg )
   511 qed
   512 
   513 lemma (in abelian_group) minus_eq:
   514   "[| x \<in> carrier G; y \<in> carrier G |] ==> x \<ominus> y = x \<oplus> \<ominus> y"
   515   by (simp only: a_minus_def)
   516 
   517 text {* Setup algebra method:
   518   compute distributive normal form in locale contexts *}
   519 
   520 use "ringsimp.ML"
   521 
   522 setup Algebra.setup
   523 
   524 lemmas (in ring) ring_simprules
   525   [algebra ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   526   a_closed zero_closed a_inv_closed minus_closed m_closed one_closed
   527   a_assoc l_zero l_neg a_comm m_assoc l_one l_distr minus_eq
   528   r_zero r_neg r_neg2 r_neg1 minus_add minus_minus minus_zero
   529   a_lcomm r_distr l_null r_null l_minus r_minus
   530 
   531 lemmas (in cring)
   532   [algebra del: ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   533   _
   534 
   535 lemmas (in cring) cring_simprules
   536   [algebra add: cring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   537   a_closed zero_closed a_inv_closed minus_closed m_closed one_closed
   538   a_assoc l_zero l_neg a_comm m_assoc l_one l_distr m_comm minus_eq
   539   r_zero r_neg r_neg2 r_neg1 minus_add minus_minus minus_zero
   540   a_lcomm m_lcomm r_distr l_null r_null l_minus r_minus
   541 
   542 
   543 lemma (in cring) nat_pow_zero:
   544   "(n::nat) ~= 0 ==> \<zero> (^) n = \<zero>"
   545   by (induct n) simp_all
   546 
   547 lemma (in ring) one_zeroD:
   548   assumes onezero: "\<one> = \<zero>"
   549   shows "carrier R = {\<zero>}"
   550 proof (rule, rule)
   551   fix x
   552   assume xcarr: "x \<in> carrier R"
   553   from xcarr
   554       have "x = x \<otimes> \<one>" by simp
   555   from this and onezero
   556       have "x = x \<otimes> \<zero>" by simp
   557   from this and xcarr
   558       have "x = \<zero>" by simp
   559   thus "x \<in> {\<zero>}" by fast
   560 qed fast
   561 
   562 lemma (in ring) one_zeroI:
   563   assumes carrzero: "carrier R = {\<zero>}"
   564   shows "\<one> = \<zero>"
   565 proof -
   566   from one_closed and carrzero
   567       show "\<one> = \<zero>" by simp
   568 qed
   569 
   570 lemma (in ring) carrier_one_zero:
   571   shows "(carrier R = {\<zero>}) = (\<one> = \<zero>)"
   572   by (rule, erule one_zeroI, erule one_zeroD)
   573 
   574 lemma (in ring) carrier_one_not_zero:
   575   shows "(carrier R \<noteq> {\<zero>}) = (\<one> \<noteq> \<zero>)"
   576   by (simp add: carrier_one_zero)
   577 
   578 text {* Two examples for use of method algebra *}
   579 
   580 lemma
   581   fixes R (structure) and S (structure)
   582   assumes "ring R" "cring S"
   583   assumes RS: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier S" "d \<in> carrier S"
   584   shows "a \<oplus> \<ominus> (a \<oplus> \<ominus> b) = b & c \<otimes>\<^bsub>S\<^esub> d = d \<otimes>\<^bsub>S\<^esub> c"
   585 proof -
   586   interpret ring R by fact
   587   interpret cring S by fact
   588 ML_val {* Algebra.print_structures @{context} *}
   589   from RS show ?thesis by algebra
   590 qed
   591 
   592 lemma
   593   fixes R (structure)
   594   assumes "ring R"
   595   assumes R: "a \<in> carrier R" "b \<in> carrier R"
   596   shows "a \<ominus> (a \<ominus> b) = b"
   597 proof -
   598   interpret ring R by fact
   599   from R show ?thesis by algebra
   600 qed
   601 
   602 subsubsection {* Sums over Finite Sets *}
   603 
   604 lemma (in ring) finsum_ldistr:
   605   "[| finite A; a \<in> carrier R; f \<in> A -> carrier R |] ==>
   606    finsum R f A \<otimes> a = finsum R (%i. f i \<otimes> a) A"
   607 proof (induct set: finite)
   608   case empty then show ?case by simp
   609 next
   610   case (insert x F) then show ?case by (simp add: Pi_def l_distr)
   611 qed
   612 
   613 lemma (in ring) finsum_rdistr:
   614   "[| finite A; a \<in> carrier R; f \<in> A -> carrier R |] ==>
   615    a \<otimes> finsum R f A = finsum R (%i. a \<otimes> f i) A"
   616 proof (induct set: finite)
   617   case empty then show ?case by simp
   618 next
   619   case (insert x F) then show ?case by (simp add: Pi_def r_distr)
   620 qed
   621 
   622 
   623 subsection {* Integral Domains *}
   624 
   625 lemma (in "domain") zero_not_one [simp]:
   626   "\<zero> ~= \<one>"
   627   by (rule not_sym) simp
   628 
   629 lemma (in "domain") integral_iff: (* not by default a simp rule! *)
   630   "[| a \<in> carrier R; b \<in> carrier R |] ==> (a \<otimes> b = \<zero>) = (a = \<zero> | b = \<zero>)"
   631 proof
   632   assume "a \<in> carrier R" "b \<in> carrier R" "a \<otimes> b = \<zero>"
   633   then show "a = \<zero> | b = \<zero>" by (simp add: integral)
   634 next
   635   assume "a \<in> carrier R" "b \<in> carrier R" "a = \<zero> | b = \<zero>"
   636   then show "a \<otimes> b = \<zero>" by auto
   637 qed
   638 
   639 lemma (in "domain") m_lcancel:
   640   assumes prem: "a ~= \<zero>"
   641     and R: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier R"
   642   shows "(a \<otimes> b = a \<otimes> c) = (b = c)"
   643 proof
   644   assume eq: "a \<otimes> b = a \<otimes> c"
   645   with R have "a \<otimes> (b \<ominus> c) = \<zero>" by algebra
   646   with R have "a = \<zero> | (b \<ominus> c) = \<zero>" by (simp add: integral_iff)
   647   with prem and R have "b \<ominus> c = \<zero>" by auto 
   648   with R have "b = b \<ominus> (b \<ominus> c)" by algebra 
   649   also from R have "b \<ominus> (b \<ominus> c) = c" by algebra
   650   finally show "b = c" .
   651 next
   652   assume "b = c" then show "a \<otimes> b = a \<otimes> c" by simp
   653 qed
   654 
   655 lemma (in "domain") m_rcancel:
   656   assumes prem: "a ~= \<zero>"
   657     and R: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier R"
   658   shows conc: "(b \<otimes> a = c \<otimes> a) = (b = c)"
   659 proof -
   660   from prem and R have "(a \<otimes> b = a \<otimes> c) = (b = c)" by (rule m_lcancel)
   661   with R show ?thesis by algebra
   662 qed
   663 
   664 
   665 subsection {* Fields *}
   666 
   667 text {* Field would not need to be derived from domain, the properties
   668   for domain follow from the assumptions of field *}
   669 lemma (in cring) cring_fieldI:
   670   assumes field_Units: "Units R = carrier R - {\<zero>}"
   671   shows "field R"
   672 proof
   673   from field_Units
   674   have a: "\<zero> \<notin> Units R" by fast
   675   have "\<one> \<in> Units R" by fast
   676   from this and a
   677   show "\<one> \<noteq> \<zero>" by force
   678 next
   679   fix a b
   680   assume acarr: "a \<in> carrier R"
   681     and bcarr: "b \<in> carrier R"
   682     and ab: "a \<otimes> b = \<zero>"
   683   show "a = \<zero> \<or> b = \<zero>"
   684   proof (cases "a = \<zero>", simp)
   685     assume "a \<noteq> \<zero>"
   686     from this and field_Units and acarr
   687     have aUnit: "a \<in> Units R" by fast
   688     from bcarr
   689     have "b = \<one> \<otimes> b" by algebra
   690     also from aUnit acarr
   691     have "... = (inv a \<otimes> a) \<otimes> b" by (simp add: Units_l_inv)
   692     also from acarr bcarr aUnit[THEN Units_inv_closed]
   693     have "... = (inv a) \<otimes> (a \<otimes> b)" by algebra
   694     also from ab and acarr bcarr aUnit
   695     have "... = (inv a) \<otimes> \<zero>" by simp
   696     also from aUnit[THEN Units_inv_closed]
   697     have "... = \<zero>" by algebra
   698     finally
   699     have "b = \<zero>" .
   700     thus "a = \<zero> \<or> b = \<zero>" by simp
   701   qed
   702 qed (rule field_Units)
   703 
   704 text {* Another variant to show that something is a field *}
   705 lemma (in cring) cring_fieldI2:
   706   assumes notzero: "\<zero> \<noteq> \<one>"
   707   and invex: "\<And>a. \<lbrakk>a \<in> carrier R; a \<noteq> \<zero>\<rbrakk> \<Longrightarrow> \<exists>b\<in>carrier R. a \<otimes> b = \<one>"
   708   shows "field R"
   709   apply (rule cring_fieldI, simp add: Units_def)
   710   apply (rule, clarsimp)
   711   apply (simp add: notzero)
   712 proof (clarsimp)
   713   fix x
   714   assume xcarr: "x \<in> carrier R"
   715     and "x \<noteq> \<zero>"
   716   from this
   717   have "\<exists>y\<in>carrier R. x \<otimes> y = \<one>" by (rule invex)
   718   from this
   719   obtain y
   720     where ycarr: "y \<in> carrier R"
   721     and xy: "x \<otimes> y = \<one>"
   722     by fast
   723   from xy xcarr ycarr have "y \<otimes> x = \<one>" by (simp add: m_comm)
   724   from ycarr and this and xy
   725   show "\<exists>y\<in>carrier R. y \<otimes> x = \<one> \<and> x \<otimes> y = \<one>" by fast
   726 qed
   727 
   728 
   729 subsection {* Morphisms *}
   730 
   731 constdefs (structure R S)
   732   ring_hom :: "[('a, 'm) ring_scheme, ('b, 'n) ring_scheme] => ('a => 'b) set"
   733   "ring_hom R S == {h. h \<in> carrier R -> carrier S &
   734       (ALL x y. x \<in> carrier R & y \<in> carrier R -->
   735         h (x \<otimes> y) = h x \<otimes>\<^bsub>S\<^esub> h y & h (x \<oplus> y) = h x \<oplus>\<^bsub>S\<^esub> h y) &
   736       h \<one> = \<one>\<^bsub>S\<^esub>}"
   737 
   738 lemma ring_hom_memI:
   739   fixes R (structure) and S (structure)
   740   assumes hom_closed: "!!x. x \<in> carrier R ==> h x \<in> carrier S"
   741     and hom_mult: "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==>
   742       h (x \<otimes> y) = h x \<otimes>\<^bsub>S\<^esub> h y"
   743     and hom_add: "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==>
   744       h (x \<oplus> y) = h x \<oplus>\<^bsub>S\<^esub> h y"
   745     and hom_one: "h \<one> = \<one>\<^bsub>S\<^esub>"
   746   shows "h \<in> ring_hom R S"
   747   by (auto simp add: ring_hom_def assms Pi_def)
   748 
   749 lemma ring_hom_closed:
   750   "[| h \<in> ring_hom R S; x \<in> carrier R |] ==> h x \<in> carrier S"
   751   by (auto simp add: ring_hom_def funcset_mem)
   752 
   753 lemma ring_hom_mult:
   754   fixes R (structure) and S (structure)
   755   shows
   756     "[| h \<in> ring_hom R S; x \<in> carrier R; y \<in> carrier R |] ==>
   757     h (x \<otimes> y) = h x \<otimes>\<^bsub>S\<^esub> h y"
   758     by (simp add: ring_hom_def)
   759 
   760 lemma ring_hom_add:
   761   fixes R (structure) and S (structure)
   762   shows
   763     "[| h \<in> ring_hom R S; x \<in> carrier R; y \<in> carrier R |] ==>
   764     h (x \<oplus> y) = h x \<oplus>\<^bsub>S\<^esub> h y"
   765     by (simp add: ring_hom_def)
   766 
   767 lemma ring_hom_one:
   768   fixes R (structure) and S (structure)
   769   shows "h \<in> ring_hom R S ==> h \<one> = \<one>\<^bsub>S\<^esub>"
   770   by (simp add: ring_hom_def)
   771 
   772 locale ring_hom_cring = R: cring R + S: cring S
   773     for R (structure) and S (structure) +
   774   fixes h
   775   assumes homh [simp, intro]: "h \<in> ring_hom R S"
   776   notes hom_closed [simp, intro] = ring_hom_closed [OF homh]
   777     and hom_mult [simp] = ring_hom_mult [OF homh]
   778     and hom_add [simp] = ring_hom_add [OF homh]
   779     and hom_one [simp] = ring_hom_one [OF homh]
   780 
   781 lemma (in ring_hom_cring) hom_zero [simp]:
   782   "h \<zero> = \<zero>\<^bsub>S\<^esub>"
   783 proof -
   784   have "h \<zero> \<oplus>\<^bsub>S\<^esub> h \<zero> = h \<zero> \<oplus>\<^bsub>S\<^esub> \<zero>\<^bsub>S\<^esub>"
   785     by (simp add: hom_add [symmetric] del: hom_add)
   786   then show ?thesis by (simp del: S.r_zero)
   787 qed
   788 
   789 lemma (in ring_hom_cring) hom_a_inv [simp]:
   790   "x \<in> carrier R ==> h (\<ominus> x) = \<ominus>\<^bsub>S\<^esub> h x"
   791 proof -
   792   assume R: "x \<in> carrier R"
   793   then have "h x \<oplus>\<^bsub>S\<^esub> h (\<ominus> x) = h x \<oplus>\<^bsub>S\<^esub> (\<ominus>\<^bsub>S\<^esub> h x)"
   794     by (simp add: hom_add [symmetric] R.r_neg S.r_neg del: hom_add)
   795   with R show ?thesis by simp
   796 qed
   797 
   798 lemma (in ring_hom_cring) hom_finsum [simp]:
   799   "[| finite A; f \<in> A -> carrier R |] ==>
   800   h (finsum R f A) = finsum S (h o f) A"
   801 proof (induct set: finite)
   802   case empty then show ?case by simp
   803 next
   804   case insert then show ?case by (simp add: Pi_def)
   805 qed
   806 
   807 lemma (in ring_hom_cring) hom_finprod:
   808   "[| finite A; f \<in> A -> carrier R |] ==>
   809   h (finprod R f A) = finprod S (h o f) A"
   810 proof (induct set: finite)
   811   case empty then show ?case by simp
   812 next
   813   case insert then show ?case by (simp add: Pi_def)
   814 qed
   815 
   816 declare ring_hom_cring.hom_finprod [simp]
   817 
   818 lemma id_ring_hom [simp]:
   819   "id \<in> ring_hom R R"
   820   by (auto intro!: ring_hom_memI)
   821 
   822 end