# Theory CRing

Up to index of Isabelle/HOL/HOL-Algebra

theory CRing
imports FiniteProduct
uses (ringsimp.ML)
begin

    (*
      Title:     The algebraic hierarchy of rings
      Id:        $Id: CRing.thy,v 1.19 2005/07/01 12:01:13 berghofe Exp $
      Author:    Clemens Ballarin, started 9 December 1996
      Copyright: Clemens Ballarin
    *)

    header {* Abelian Groups *}

    theory CRing imports FiniteProduct
    uses ("ringsimp.ML") begin

    record 'a ring = "'a monoid" +
      zero :: 'a ("\<zero>\<index>")
      add :: "['a, 'a] => 'a" (infixl "⊕\<index>" 65)

    text {* Derived operations. *}

    constdefs (structure R)
      a_inv :: "[('a, 'm) ring_scheme, 'a ] => 'a" ("\<ominus>\<index> _" [81] 80)
      "a_inv R == m_inv (| carrier = carrier R, mult = add R, one = zero R |)"

      minus :: "[('a, 'm) ring_scheme, 'a, 'a] => 'a" (infixl "\<ominus>\<index>" 65)
      "[| x ∈ carrier R; y ∈ carrier R |] ==> x \<ominus> y == x ⊕ (\<ominus> y)"

    locale abelian_monoid = struct G +
      assumes a_comm_monoid:
         "comm_monoid (| carrier = carrier G, mult = add G, one = zero G |)"

    text {*
      The following definition is redundant but simple to use.
    *}

    locale abelian_group = abelian_monoid +
      assumes a_comm_group:
         "comm_group (| carrier = carrier G, mult = add G, one = zero G |)"

    subsection {* Basic Properties *}

    lemma abelian_monoidI:
      includes struct R
      assumes a_closed:
          "!!x y. [| x ∈ carrier R; y ∈ carrier R |] ==> x ⊕ y ∈ carrier R"
        and zero_closed: "\<zero> ∈ carrier R"
        and a_assoc:
          "!!x y z. [| x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |] ==>
          (x ⊕ y) ⊕ z = x ⊕ (y ⊕ z)"
        and l_zero: "!!x. x ∈ carrier R ==> \<zero> ⊕ x = x"
        and a_comm:
          "!!x y. [| x ∈ carrier R; y ∈ carrier R |] ==> x ⊕ y = y ⊕ x"
      shows "abelian_monoid R"
      by (auto intro!: abelian_monoid.intro comm_monoidI intro: prems)

    lemma abelian_groupI:
      includes struct R
      assumes a_closed:
          "!!x y. [| x ∈ carrier R; y ∈ carrier R |] ==> x ⊕ y ∈ carrier R"
        and zero_closed: "zero R ∈ carrier R"
        and a_assoc:
          "!!x y z. [| x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |] ==>
          (x ⊕ y) ⊕ z = x ⊕ (y ⊕ z)"
        and a_comm:
          "!!x y. [| x ∈ carrier R; y ∈ carrier R |] ==> x ⊕ y = y ⊕ x"
        and l_zero: "!!x. x ∈ carrier R ==> \<zero> ⊕ x = x"
        and l_inv_ex: "!!x. x ∈ carrier R ==> EX y : carrier R. y ⊕ x = \<zero>"
      shows "abelian_group R"
      by (auto intro!: abelian_group.intro abelian_monoidI
          abelian_group_axioms.intro comm_monoidI comm_groupI
        intro: prems)

    lemma (in abelian_monoid) a_monoid:
      "monoid (| carrier = carrier G, mult = add G, one = zero G |)"
    by (rule comm_monoid.axioms, rule a_comm_monoid)

    lemma (in abelian_group) a_group:
      "group (| carrier = carrier G, mult = add G, one = zero G |)"
    by (simp add: group_def a_monoid comm_group.axioms a_comm_group)

    lemmas monoid_record_simps = partial_object.simps monoid.simps

    lemma (in abelian_monoid) a_closed [intro, simp]:
      "[| x ∈ carrier G; y ∈ carrier G |] ==> x ⊕ y ∈ carrier G"
      by (rule monoid.m_closed [OF a_monoid, simplified monoid_record_simps])

    lemma (in abelian_monoid) zero_closed [intro, simp]:
      "\<zero> ∈ carrier G"
      by (rule monoid.one_closed [OF a_monoid, simplified monoid_record_simps])

    lemma (in abelian_group) a_inv_closed [intro, simp]:
      "x ∈ carrier G ==> \<ominus> x ∈ carrier G"
      by (simp add: a_inv_def
        group.inv_closed [OF a_group, simplified monoid_record_simps])

    lemma (in abelian_group) minus_closed [intro, simp]:
      "[| x ∈ carrier G; y ∈ carrier G |] ==> x \<ominus> y ∈ carrier G"
      by (simp add: minus_def)

    lemma (in abelian_group) a_l_cancel [simp]:
      "[| x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |] ==>
       (x ⊕ y = x ⊕ z) = (y = z)"
      by (rule group.l_cancel [OF a_group, simplified monoid_record_simps])

    lemma (in abelian_group) a_r_cancel [simp]:
      "[| x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |] ==>
       (y ⊕ x = z ⊕ x) = (y = z)"
      by (rule group.r_cancel [OF a_group, simplified monoid_record_simps])

    lemma (in abelian_monoid) a_assoc:
      "[|x ∈ carrier G; y ∈ carrier G; z ∈ carrier G|] ==>
      (x ⊕ y) ⊕ z = x ⊕ (y ⊕ z)"
      by (rule monoid.m_assoc [OF a_monoid, simplified monoid_record_simps])

    lemma (in abelian_monoid) l_zero [simp]:
      "x ∈ carrier G ==> \<zero> ⊕ x = x"
      by (rule monoid.l_one [OF a_monoid, simplified monoid_record_simps])

    lemma (in abelian_group) l_neg:
      "x ∈ carrier G ==> \<ominus> x ⊕ x = \<zero>"
      by (simp add: a_inv_def
        group.l_inv [OF a_group, simplified monoid_record_simps])

    lemma (in abelian_monoid) a_comm:
      "[|x ∈ carrier G; y ∈ carrier G|] ==> x ⊕ y = y ⊕ x"
      by (rule comm_monoid.m_comm [OF a_comm_monoid,
        simplified monoid_record_simps])

    lemma (in abelian_monoid) a_lcomm:
      "[|x ∈ carrier G; y ∈ carrier G; z ∈ carrier G|] ==>
       x ⊕ (y ⊕ z) = y ⊕ (x ⊕ z)"
      by (rule comm_monoid.m_lcomm [OF a_comm_monoid,
                                    simplified monoid_record_simps])

    lemma (in abelian_monoid) r_zero [simp]:
      "x ∈ carrier G ==> x ⊕ \<zero> = x"
      using monoid.r_one [OF a_monoid]
      by simp

    lemma (in abelian_group) r_neg:
      "x ∈ carrier G ==> x ⊕ (\<ominus> x) = \<zero>"
      using group.r_inv [OF a_group]
      by (simp add: a_inv_def)

    lemma (in abelian_group) minus_zero [simp]:
      "\<ominus> \<zero> = \<zero>"
      by (simp add: a_inv_def
        group.inv_one [OF a_group, simplified monoid_record_simps])

    lemma (in abelian_group) minus_minus [simp]:
      "x ∈ carrier G ==> \<ominus> (\<ominus> x) = x"
      using group.inv_inv [OF a_group, simplified monoid_record_simps]
      by (simp add: a_inv_def)

    lemma (in abelian_group) a_inv_inj:
      "inj_on (a_inv G) (carrier G)"
      using group.inv_inj [OF a_group, simplified monoid_record_simps]
      by (simp add: a_inv_def)

    lemma (in abelian_group) minus_add:
      "[| x ∈ carrier G; y ∈ carrier G |] ==> \<ominus> (x ⊕ y) = \<ominus> x ⊕ \<ominus> y"
      using comm_group.inv_mult [OF a_comm_group]
      by (simp add: a_inv_def)

    lemmas (in abelian_monoid) a_ac = a_assoc a_comm a_lcomm

    subsection {* Sums over Finite Sets *}

    text {*
      This definition makes it easy to lift lemmas from @{term finprod}.
    *}

    constdefs
      finsum :: "[('b, 'm) ring_scheme, 'a => 'b, 'a set] => 'b"
      "finsum G f A == finprod (| carrier = carrier G,
         mult = add G, one = zero G |) f A"

    syntax
      "_finsum" :: "index => idt => 'a set => 'b => 'b"
          ("(3\<Oplus>__:_. _)" [1000, 0, 51, 10] 10)
    syntax (xsymbols)
      "_finsum" :: "index => idt => 'a set => 'b => 'b"
          ("(3\<Oplus>__∈_. _)" [1000, 0, 51, 10] 10)
    syntax (HTML output)
      "_finsum" :: "index => idt => 'a set => 'b => 'b"
          ("(3\<Oplus>__∈_. _)" [1000, 0, 51, 10] 10)
    translations
      "\<Oplus>\<index>i:A. b" == "finsum \<struct>\<index> (%i. b) A"
      -- {* Beware of argument permutation! *}

    (*
      lemmas (in abelian_monoid) finsum_empty [simp] =
        comm_monoid.finprod_empty [OF a_comm_monoid, simplified]
      is dangeous, because attributes (like simplified) are applied upon opening
      the locale, simplified refers to the simpset at that time!!!

      lemmas (in abelian_monoid) finsum_empty [simp] =
        abelian_monoid.finprod_empty [OF a_abelian_monoid, folded finsum_def,
          simplified monoid_record_simps]
      makes the locale slow, because proofs are repeated for every
      "lemma (in abelian_monoid)" command.
      When lemma is used time in UnivPoly.thy from beginning to UP_cring goes down
      from 110 secs to 60 secs.
    *)

    lemma (in abelian_monoid) finsum_empty [simp]:
      "finsum G f {} = \<zero>"
      by (rule comm_monoid.finprod_empty [OF a_comm_monoid,
        folded finsum_def, simplified monoid_record_simps])

    lemma (in abelian_monoid) finsum_insert [simp]:
      "[| finite F; a ∉ F; f ∈ F -> carrier G; f a ∈ carrier G |]
      ==> finsum G f (insert a F) = f a ⊕ finsum G f F"
      by (rule comm_monoid.finprod_insert [OF a_comm_monoid,
        folded finsum_def, simplified monoid_record_simps])

    lemma (in abelian_monoid) finsum_zero [simp]:
      "finite A ==> (\<Oplus>i∈A. \<zero>) = \<zero>"
      by (rule comm_monoid.finprod_one [OF a_comm_monoid, folded finsum_def,
        simplified monoid_record_simps])

    lemma (in abelian_monoid) finsum_closed [simp]:
      fixes A
      assumes fin: "finite A" and f: "f ∈ A -> carrier G"
      shows "finsum G f A ∈ carrier G"
      by (rule comm_monoid.finprod_closed [OF a_comm_monoid,
        folded finsum_def, simplified monoid_record_simps])

    lemma (in abelian_monoid) finsum_Un_Int:
      "[| finite A; finite B; g ∈ A -> carrier G; g ∈ B -> carrier G |] ==>
         finsum G g (A Un B) ⊕ finsum G g (A Int B) =
         finsum G g A ⊕ finsum G g B"
      by (rule comm_monoid.finprod_Un_Int [OF a_comm_monoid,
        folded finsum_def, simplified monoid_record_simps])

    lemma (in abelian_monoid) finsum_Un_disjoint:
      "[| finite A; finite B; A Int B = {};
          g ∈ A -> carrier G; g ∈ B -> carrier G |]
       ==> finsum G g (A Un B) = finsum G g A ⊕ finsum G g B"
      by (rule comm_monoid.finprod_Un_disjoint [OF a_comm_monoid,
        folded finsum_def, simplified monoid_record_simps])

    lemma (in abelian_monoid) finsum_addf:
      "[| finite A; f ∈ A -> carrier G; g ∈ A -> carrier G |] ==>
       finsum G (%x. f x ⊕ g x) A = (finsum G f A ⊕ finsum G g A)"
      by (rule comm_monoid.finprod_multf [OF a_comm_monoid,
        folded finsum_def, simplified monoid_record_simps])

    lemma (in abelian_monoid) finsum_cong':
      "[| A = B; g : B -> carrier G;
          !!i. i : B ==> f i = g i |] ==> finsum G f A = finsum G g B"
      by (rule comm_monoid.finprod_cong' [OF a_comm_monoid,
        folded finsum_def, simplified monoid_record_simps]) auto

    lemma (in abelian_monoid) finsum_0 [simp]:
      "f : {0::nat} -> carrier G ==> finsum G f {..0} = f 0"
      by (rule comm_monoid.finprod_0 [OF a_comm_monoid, folded finsum_def,
        simplified monoid_record_simps])

    lemma (in abelian_monoid) finsum_Suc [simp]:
      "f : {..Suc n} -> carrier G ==>
       finsum G f {..Suc n} = (f (Suc n) ⊕ finsum G f {..n})"
      by (rule comm_monoid.finprod_Suc [OF a_comm_monoid, folded finsum_def,
        simplified monoid_record_simps])

    lemma (in abelian_monoid) finsum_Suc2:
      "f : {..Suc n} -> carrier G ==>
       finsum G f {..Suc n} = (finsum G (%i. f (Suc i)) {..n} ⊕ f 0)"
      by (rule comm_monoid.finprod_Suc2 [OF a_comm_monoid, folded finsum_def,
        simplified monoid_record_simps])

    lemma (in abelian_monoid) finsum_add [simp]:
      "[| f : {..n} -> carrier G; g : {..n} -> carrier G |] ==>
         finsum G (%i. f i ⊕ g i) {..n::nat} =
         finsum G f {..n} ⊕ finsum G g {..n}"
      by (rule comm_monoid.finprod_mult [OF a_comm_monoid, folded finsum_def,
        simplified monoid_record_simps])

    lemma (in abelian_monoid) finsum_cong:
      "[| A = B; f : B -> carrier G;
          !!i. i : B =simp=> f i = g i |] ==> finsum G f A = finsum G g B"
      by (rule comm_monoid.finprod_cong [OF a_comm_monoid, folded finsum_def,
        simplified monoid_record_simps]) (auto simp add: simp_implies_def)

    text {*Usually, if this rule causes a failed congruence proof error,
       the reason is that the premise @{text "g ∈ B -> carrier G"} cannot be shown.
       Adding @{thm [source] Pi_def} to the simpset is often useful. *}

    section {* The Algebraic Hierarchy of Rings *}

    subsection {* Basic Definitions *}

    locale ring = abelian_group R + monoid R +
      assumes l_distr: "[| x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
          ==> (x ⊕ y) ⊗ z = x ⊗ z ⊕ y ⊗ z"
        and r_distr: "[| x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
          ==> z ⊗ (x ⊕ y) = z ⊗ x ⊕ z ⊗ y"

    locale cring = ring + comm_monoid R

    locale "domain" = cring +
      assumes one_not_zero [simp]: "\<one> ~= \<zero>"
        and integral: "[| a ⊗ b = \<zero>; a ∈ carrier R; b ∈ carrier R |] ==>
                      a = \<zero> | b = \<zero>"

    locale field = "domain" +
      assumes field_Units: "Units R = carrier R - {\<zero>}"

    subsection {* Basic Facts of Rings *}

    lemma ringI:
      includes struct R
      assumes abelian_group: "abelian_group R"
        and monoid: "monoid R"
        and l_distr: "!!x y z. [| x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
          ==> (x ⊕ y) ⊗ z = x ⊗ z ⊕ y ⊗ z"
        and r_distr: "!!x y z. [| x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
          ==> z ⊗ (x ⊕ y) = z ⊗ x ⊕ z ⊗ y"
      shows "ring R"
      by (auto intro: ring.intro
        abelian_group.axioms ring_axioms.intro prems)

    lemma (in ring) is_abelian_group:
      "abelian_group R"
      by (auto intro!: abelian_groupI a_assoc a_comm l_neg)

    lemma (in ring) is_monoid:
      "monoid R"
      by (auto intro!: monoidI m_assoc)

    lemma cringI:
      includes struct R
      assumes abelian_group: "abelian_group R"
        and comm_monoid: "comm_monoid R"
        and l_distr: "!!x y z. [| x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
          ==> (x ⊕ y) ⊗ z = x ⊗ z ⊕ y ⊗ z"
      shows "cring R"
      proof (rule cring.intro)
        show "ring_axioms R"
        -- {* Right-distributivity follows from left-distributivity and
              commutativity. *}
        proof (rule ring_axioms.intro)
          fix x y z
          assume R: "x ∈ carrier R" "y ∈ carrier R" "z ∈ carrier R"
          note [simp]= comm_monoid.axioms [OF comm_monoid]
            abelian_group.axioms [OF abelian_group]
            abelian_monoid.a_closed

          from R have "z ⊗ (x ⊕ y) = (x ⊕ y) ⊗ z"
            by (simp add: comm_monoid.m_comm [OF comm_monoid.intro])
          also from R have "... = x ⊗ z ⊕ y ⊗ z" by (simp add: l_distr)
          also from R have "... = z ⊗ x ⊕ z ⊗ y"
            by (simp add: comm_monoid.m_comm [OF comm_monoid.intro])
          finally show "z ⊗ (x ⊕ y) = z ⊗ x ⊕ z ⊗ y" .
        qed
      qed (auto intro: cring.intro
          abelian_group.axioms comm_monoid.axioms ring_axioms.intro prems)

    lemma (in cring) is_comm_monoid:
      "comm_monoid R"
      by (auto intro!: comm_monoidI m_assoc m_comm)

    subsection {* Normaliser for Rings *}

    lemma (in abelian_group) r_neg2:
      "[| x ∈ carrier G; y ∈ carrier G |] ==> x ⊕ (\<ominus> x ⊕ y) = y"
    proof -
      assume G: "x ∈ carrier G" "y ∈ carrier G"
      then have "(x ⊕ \<ominus> x) ⊕ y = y"
        by (simp only: r_neg l_zero)
      with G show ?thesis
        by (simp add: a_ac)
    qed

    lemma (in abelian_group) r_neg1:
      "[| x ∈ carrier G; y ∈ carrier G |] ==> \<ominus> x ⊕ (x ⊕ y) = y"
    proof -
      assume G: "x ∈ carrier G" "y ∈ carrier G"
      then have "(\<ominus> x ⊕ x) ⊕ y = y"
        by (simp only: l_neg l_zero)
      with G show ?thesis by (simp add: a_ac)
    qed

    text {*
      The following proofs are from Jacobson, Basic Algebra I, pp.~88--89
    *}

    lemma (in ring) l_null [simp]:
      "x ∈ carrier R ==> \<zero> ⊗ x = \<zero>"
    proof -
      assume R: "x ∈ carrier R"
      then have "\<zero> ⊗ x ⊕ \<zero> ⊗ x = (\<zero> ⊕ \<zero>) ⊗ x"
        by (simp add: l_distr del: l_zero r_zero)
      also from R have "... = \<zero> ⊗ x ⊕ \<zero>" by simp
      finally have "\<zero> ⊗ x ⊕ \<zero> ⊗ x = \<zero> ⊗ x ⊕ \<zero>" .
      with R show ?thesis by (simp del: r_zero)
    qed

    lemma (in ring) r_null [simp]:
      "x ∈ carrier R ==> x ⊗ \<zero> = \<zero>"
    proof -
      assume R: "x ∈ carrier R"
      then have "x ⊗ \<zero> ⊕ x ⊗ \<zero> = x ⊗ (\<zero> ⊕ \<zero>)"
        by (simp add: r_distr del: l_zero r_zero)
      also from R have "... = x ⊗ \<zero> ⊕ \<zero>" by simp
      finally have "x ⊗ \<zero> ⊕ x ⊗ \<zero> = x ⊗ \<zero> ⊕ \<zero>" .
      with R show ?thesis by (simp del: r_zero)
    qed

    lemma (in ring) l_minus:
      "[| x ∈ carrier R; y ∈ carrier R |] ==> \<ominus> x ⊗ y = \<ominus> (x ⊗ y)"
    proof -
      assume R: "x ∈ carrier R" "y ∈ carrier R"
      then have "(\<ominus> x) ⊗ y ⊕ x ⊗ y = (\<ominus> x ⊕ x) ⊗ y" by (simp add: l_distr)
      also from R have "... = \<zero>" by (simp add: l_neg l_null)
      finally have "(\<ominus> x) ⊗ y ⊕ x ⊗ y = \<zero>" .
      with R have "(\<ominus> x) ⊗ y ⊕ x ⊗ y ⊕ \<ominus> (x ⊗ y) = \<zero> ⊕ \<ominus> (x ⊗ y)" by simp
      with R show ?thesis by (simp add: a_assoc r_neg )
    qed

    lemma (in ring) r_minus:
      "[| x ∈ carrier R; y ∈ carrier R |] ==> x ⊗ \<ominus> y = \<ominus> (x ⊗ y)"
    proof -
      assume R: "x ∈ carrier R" "y ∈ carrier R"
      then have "x ⊗ (\<ominus> y) ⊕ x ⊗ y = x ⊗ (\<ominus> y ⊕ y)" by (simp add: r_distr)
      also from R have "... = \<zero>" by (simp add: l_neg r_null)
      finally have "x ⊗ (\<ominus> y) ⊕ x ⊗ y = \<zero>" .
      with R have "x ⊗ (\<ominus> y) ⊕ x ⊗ y ⊕ \<ominus> (x ⊗ y) = \<zero> ⊕ \<ominus> (x ⊗ y)" by simp
      with R show ?thesis by (simp add: a_assoc r_neg )
    qed

    lemma (in ring) minus_eq:
      "[| x ∈ carrier R; y ∈ carrier R |] ==> x \<ominus> y = x ⊕ \<ominus> y"
      by (simp only: minus_def)

    lemmas (in ring) ring_simprules =
      a_closed zero_closed a_inv_closed minus_closed m_closed one_closed
      a_assoc l_zero l_neg a_comm m_assoc l_one l_distr minus_eq
      r_zero r_neg r_neg2 r_neg1 minus_add minus_minus minus_zero
      a_lcomm r_distr l_null r_null l_minus r_minus

    lemmas (in cring) cring_simprules =
      a_closed zero_closed a_inv_closed minus_closed m_closed one_closed
      a_assoc l_zero l_neg a_comm m_assoc l_one l_distr m_comm minus_eq
      r_zero r_neg r_neg2 r_neg1 minus_add minus_minus minus_zero
      a_lcomm m_lcomm r_distr l_null r_null l_minus r_minus

    use "ringsimp.ML"

    method_setup algebra =
      {* Method.ctxt_args cring_normalise *}
      {* computes distributive normal form in locale context cring *}

    lemma (in cring) nat_pow_zero:
      "(n::nat) ~= 0 ==> \<zero> (^) n = \<zero>"
      by (induct n) simp_all

    text {* Two examples for use of method algebra *}

    lemma
      includes ring R + cring S
      shows "[| a ∈ carrier R; b ∈ carrier R; c ∈ carrier S; d ∈ carrier S |] ==>
      a ⊕ \<ominus> (a ⊕ \<ominus> b) = b & c ⊗S d = d ⊗S c"
      by algebra

    lemma
      includes cring
      shows "[| a ∈ carrier R; b ∈ carrier R |] ==> a \<ominus> (a \<ominus> b) = b"
      by algebra

    subsection {* Sums over Finite Sets *}

    lemma (in cring) finsum_ldistr:
      "[| finite A; a ∈ carrier R; f ∈ A -> carrier R |] ==>
       finsum R f A ⊗ a = finsum R (%i. f i ⊗ a) A"
    proof (induct set: Finites)
      case empty then show ?case by simp
    next
      case (insert x F) then show ?case by (simp add: Pi_def l_distr)
    qed

    lemma (in cring) finsum_rdistr:
      "[| finite A; a ∈ carrier R; f ∈ A -> carrier R |] ==>
       a ⊗ finsum R f A = finsum R (%i. a ⊗ f i) A"
    proof (induct set: Finites)
      case empty then show ?case by simp
    next
      case (insert x F) then show ?case by (simp add: Pi_def r_distr)
    qed

    subsection {* Facts of Integral Domains *}

    lemma (in "domain") zero_not_one [simp]:
      "\<zero> ~= \<one>"
      by (rule not_sym) simp

    lemma (in "domain") integral_iff: (* not by default a simp rule! *)
      "[| a ∈ carrier R; b ∈ carrier R |] ==> (a ⊗ b = \<zero>) = (a = \<zero> | b = \<zero>)"
    proof
      assume "a ∈ carrier R" "b ∈ carrier R" "a ⊗ b = \<zero>"
      then show "a = \<zero> | b = \<zero>" by (simp add: integral)
    next
      assume "a ∈ carrier R" "b ∈ carrier R" "a = \<zero> | b = \<zero>"
      then show "a ⊗ b = \<zero>" by auto
    qed

    lemma (in "domain") m_lcancel:
      assumes prem: "a ~= \<zero>"
        and R: "a ∈ carrier R" "b ∈ carrier R" "c ∈ carrier R"
      shows "(a ⊗ b = a ⊗ c) = (b = c)"
    proof
      assume eq: "a ⊗ b = a ⊗ c"
      with R have "a ⊗ (b \<ominus> c) = \<zero>" by algebra
      with R have "a = \<zero> | (b \<ominus> c) = \<zero>" by (simp add: integral_iff)
      with prem and R have "b \<ominus> c = \<zero>" by auto
      with R have "b = b \<ominus> (b \<ominus> c)" by algebra
      also from R have "b \<ominus> (b \<ominus> c) = c" by algebra
      finally show "b = c" .
    next
      assume "b = c" then show "a ⊗ b = a ⊗ c" by simp
    qed

    lemma (in "domain") m_rcancel:
      assumes prem: "a ~= \<zero>"
        and R: "a ∈ carrier R" "b ∈ carrier R" "c ∈ carrier R"
      shows conc: "(b ⊗ a = c ⊗ a) = (b = c)"
    proof -
      from prem and R have "(a ⊗ b = a ⊗ c) = (b = c)" by (rule m_lcancel)
      with R show ?thesis by algebra
    qed

    subsection {* Morphisms *}

    constdefs (structure R S)
      ring_hom :: "[('a, 'm) ring_scheme, ('b, 'n) ring_scheme] => ('a => 'b) set"
      "ring_hom R S == {h. h ∈ carrier R -> carrier S &
          (ALL x y. x ∈ carrier R & y ∈ carrier R -->
            h (x ⊗ y) = h x ⊗S h y & h (x ⊕ y) = h x ⊕S h y) &
          h \<one> = \<one>S}"

    lemma ring_hom_memI:
      includes struct R + struct S
      assumes hom_closed: "!!x. x ∈ carrier R ==> h x ∈ carrier S"
        and hom_mult: "!!x y. [| x ∈ carrier R; y ∈ carrier R |] ==>
          h (x ⊗ y) = h x ⊗S h y"
        and hom_add: "!!x y. [| x ∈ carrier R; y ∈ carrier R |] ==>
          h (x ⊕ y) = h x ⊕S h y"
        and hom_one: "h \<one> = \<one>S"
      shows "h ∈ ring_hom R S"
      by (auto simp add: ring_hom_def prems Pi_def)

    lemma ring_hom_closed:
      "[| h ∈ ring_hom R S; x ∈ carrier R |] ==> h x ∈ carrier S"
      by (auto simp add: ring_hom_def funcset_mem)

    lemma ring_hom_mult:
      includes struct R + struct S
      shows
        "[| h ∈ ring_hom R S; x ∈ carrier R; y ∈ carrier R |] ==>
        h (x ⊗ y) = h x ⊗S h y"
        by (simp add: ring_hom_def)

    lemma ring_hom_add:
      includes struct R + struct S
      shows
        "[| h ∈ ring_hom R S; x ∈ carrier R; y ∈ carrier R |] ==>
        h (x ⊕ y) = h x ⊕S h y"
        by (simp add: ring_hom_def)

    lemma ring_hom_one:
      includes struct R + struct S
      shows "h ∈ ring_hom R S ==> h \<one> = \<one>S"
      by (simp add: ring_hom_def)

    locale ring_hom_cring = cring R + cring S + var h +
      assumes homh [simp, intro]: "h ∈ ring_hom R S"
      notes hom_closed [simp, intro] = ring_hom_closed [OF homh]
        and hom_mult [simp] = ring_hom_mult [OF homh]
        and hom_add [simp] = ring_hom_add [OF homh]
        and hom_one [simp] = ring_hom_one [OF homh]

    lemma (in ring_hom_cring) hom_zero [simp]:
      "h \<zero> = \<zero>S"
    proof -
      have "h \<zero> ⊕S h \<zero> = h \<zero> ⊕S \<zero>S"
        by (simp add: hom_add [symmetric] del: hom_add)
      then show ?thesis by (simp del: S.r_zero)
    qed

    lemma (in ring_hom_cring) hom_a_inv [simp]:
      "x ∈ carrier R ==> h (\<ominus> x) = \<ominus>S h x"
    proof -
      assume R: "x ∈ carrier R"
      then have "h x ⊕S h (\<ominus> x) = h x ⊕S (\<ominus>S h x)"
        by (simp add: hom_add [symmetric] R.r_neg S.r_neg del: hom_add)
      with R show ?thesis by simp
    qed

    lemma (in ring_hom_cring) hom_finsum [simp]:
      "[| finite A; f ∈ A -> carrier R |] ==>
      h (finsum R f A) = finsum S (h o f) A"
    proof (induct set: Finites)
      case empty then show ?case by simp
    next
      case insert then show ?case by (simp add: Pi_def)
    qed

    lemma (in ring_hom_cring) hom_finprod:
      "[| finite A; f ∈ A -> carrier R |] ==>
      h (finprod R f A) = finprod S (h o f) A"
    proof (induct set: Finites)
      case empty then show ?case by simp
    next
      case insert then show ?case by (simp add: Pi_def)
    qed

    declare ring_hom_cring.hom_finprod [simp]

    lemma id_ring_hom [simp]:
      "id ∈ ring_hom R R"
      by (auto intro!: ring_hom_memI)

    end

### Basic Properties

lemma abelian_monoidI:

      [| !!x y. [| x ∈ carrier R; y ∈ carrier R |] ==> x ⊕R y ∈ carrier R;
         \<zero>R ∈ carrier R;
         !!x y z.
            [| x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
            ==> x ⊕R y ⊕R z = x ⊕R (y ⊕R z);
         !!x. x ∈ carrier R ==> \<zero>R ⊕R x = x;
         !!x y. [| x ∈ carrier R; y ∈ carrier R |] ==> x ⊕R y = y ⊕R x |]
      ==> abelian_monoid R

lemma abelian_groupI:

      [| !!x y. [| x ∈ carrier R; y ∈ carrier R |] ==> x ⊕R y ∈ carrier R;
         \<zero>R ∈ carrier R;
         !!x y z.
            [| x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
            ==> x ⊕R y ⊕R z = x ⊕R (y ⊕R z);
         !!x y. [| x ∈ carrier R; y ∈ carrier R |] ==> x ⊕R y = y ⊕R x;
         !!x. x ∈ carrier R ==> \<zero>R ⊕R x = x;
         !!x. x ∈ carrier R ==> ∃y∈carrier R. y ⊕R x = \<zero>R |]
      ==> abelian_group R

lemma a_monoid:

      abelian_monoid G
      ==> monoid (| carrier = carrier G, mult = op ⊕G, one = \<zero>G |)

lemma a_group:

      abelian_group G
      ==> group (| carrier = carrier G, mult = op ⊕G, one = \<zero>G |)

lemmas monoid_record_simps:

      carrier (| carrier = carrier, ... = more |) = carrier

      partial_object.more (| carrier = carrier, ... = more |) = more

      (| carrier = carrier, ... = more |)(| carrier := carrier' |) =
      (| carrier = carrier', ... = more |)

      (| carrier = carrier, ... = more |)(| partial_object.more := more' |) =
      (| carrier = carrier, ... = more' |)

      op ⊗(| carrier = carrier, mult = mult, one = one, ... = more |) = mult

      \<one>(| carrier = carrier, mult = mult, one = one, ... = more |) = one

      monoid.more (| carrier = carrier, mult = mult, one = one, ... = more |) = more

      (| carrier = carrier, mult = mult, one = one, ... = more |)(| mult := mult' |) =
      (| carrier = carrier, mult = mult', one = one, ... = more |)

      (| carrier = carrier, mult = mult, one = one, ... = more |)(| one := one' |) =
      (| carrier = carrier, mult = mult, one = one', ... = more |)

      (| carrier = carrier, mult = mult, one = one, ... = more |)
      (| monoid.more := more' |) =
      (| carrier = carrier, mult = mult, one = one, ... = more' |)

lemmas monoid_record_simps:

      carrier (| carrier = carrier, ... = more |) = carrier

      partial_object.more (| carrier = carrier, ... = more |) = more

      (| carrier = carrier, ... = more |)(| carrier := carrier' |) =
      (| carrier = carrier', ... = more |)

      (| carrier = carrier, ... = more |)(| partial_object.more := more' |) =
      (| carrier = carrier, ... = more' |)

      op ⊗(| carrier = carrier, mult = mult, one = one, ... = more |) = mult

      \<one>(| carrier = carrier, mult = mult, one = one, ... = more |) = one

      monoid.more (| carrier = carrier, mult = mult, one = one, ... = more |) = more

      (| carrier = carrier, mult = mult, one = one, ... = more |)(| mult := mult' |) =
      (| carrier = carrier, mult = mult', one = one, ... = more |)

      (| carrier = carrier, mult = mult, one = one, ... = more |)(| one := one' |) =
      (| carrier = carrier, mult = mult, one = one', ... = more |)

      (| carrier = carrier, mult = mult, one = one, ... = more |)
      (| monoid.more := more' |) =
      (| carrier = carrier, mult = mult, one = one, ... = more' |)

lemma a_closed:

      [| abelian_monoid G; x ∈ carrier G; y ∈ carrier G |] ==> x ⊕G y ∈ carrier G

lemma zero_closed:

      abelian_monoid G ==> \<zero>G ∈ carrier G

lemma a_inv_closed:

      [| abelian_group G; x ∈ carrier G |] ==> \<ominus>G x ∈ carrier G

lemma minus_closed:

      [| abelian_group G; x ∈ carrier G; y ∈ carrier G |]
      ==> x \<ominus>G y ∈ carrier G

lemma a_l_cancel:

      [| abelian_group G; x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |]
      ==> (x ⊕G y = x ⊕G z) = (y = z)

lemma a_r_cancel:

      [| abelian_group G; x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |]
      ==> (y ⊕G x = z ⊕G x) = (y = z)

lemma a_assoc:

      [| abelian_monoid G; x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |]
      ==> x ⊕G y ⊕G z = x ⊕G (y ⊕G z)

lemma l_zero:

      [| abelian_monoid G; x ∈ carrier G |] ==> \<zero>G ⊕G x = x

lemma l_neg:

      [| abelian_group G; x ∈ carrier G |] ==> \<ominus>G x ⊕G x = \<zero>G

lemma a_comm:

      [| abelian_monoid G; x ∈ carrier G; y ∈ carrier G |] ==> x ⊕G y = y ⊕G x

lemma a_lcomm:

      [| abelian_monoid G; x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |]
      ==> x ⊕G (y ⊕G z) = y ⊕G (x ⊕G z)

lemma r_zero:

      [| abelian_monoid G; x ∈ carrier G |] ==> x ⊕G \<zero>G = x

lemma r_neg:

      [| abelian_group G; x ∈ carrier G |] ==> x ⊕G \<ominus>G x = \<zero>G

lemma minus_zero:

      abelian_group G ==> \<ominus>G \<zero>G = \<zero>G

lemma minus_minus:

      [| abelian_group G; x ∈ carrier G |] ==> \<ominus>G (\<ominus>G x) = x

lemma a_inv_inj:

      abelian_group G ==> inj_on (a_inv G) (carrier G)

lemma minus_add:

      [| abelian_group G; x ∈ carrier G; y ∈ carrier G |]
      ==> \<ominus>G (x ⊕G y) = \<ominus>G x ⊕G \<ominus>G y

lemmas a_ac:

      [| abelian_monoid G; x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |]
      ==> x ⊕G y ⊕G z = x ⊕G (y ⊕G z)

      [| abelian_monoid G; x ∈ carrier G; y ∈ carrier G |] ==> x ⊕G y = y ⊕G x

      [| abelian_monoid G; x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |]
      ==> x ⊕G (y ⊕G z) = y ⊕G (x ⊕G z)

### Sums over Finite Sets

lemma finsum_empty:

      abelian_monoid G ==> finsum G f {} = \<zero>G

lemma finsum_insert:

      [| abelian_monoid G; finite F; a ∉ F; f ∈ F -> carrier G; f a ∈ carrier G |]
      ==> finsum G f (insert a F) = f a ⊕G finsum G f F

lemma finsum_zero:

      [| abelian_monoid G; finite A |] ==> (\<Oplus>Gi∈A. \<zero>G) = \<zero>G

lemma finsum_closed:

      [| abelian_monoid G; finite A; f ∈ A -> carrier G |]
      ==> finsum G f A ∈ carrier G

lemma finsum_Un_Int:

      [| abelian_monoid G; finite A; finite B; g ∈ A -> carrier G;
         g ∈ B -> carrier G |]
      ==> finsum G g (A ∪ B) ⊕G finsum G g (A ∩ B) = finsum G g A ⊕G finsum G g B

lemma finsum_Un_disjoint:

      [| abelian_monoid G; finite A; finite B; A ∩ B = {}; g ∈ A -> carrier G;
         g ∈ B -> carrier G |]
      ==> finsum G g (A ∪ B) = finsum G g A ⊕G finsum G g B

lemma finsum_addf:

      [| abelian_monoid G; finite A; f ∈ A -> carrier G; g ∈ A -> carrier G |]
      ==> (\<Oplus>Gx∈A. f x ⊕G g x) = finsum G f A ⊕G finsum G g A

lemma finsum_cong':

      [| abelian_monoid G; A = B; g ∈ B -> carrier G; !!i. i ∈ B ==> f i = g i |]
      ==> finsum G f A = finsum G g B

lemma finsum_0:

      [| abelian_monoid G; f ∈ {0} -> carrier G |] ==> finsum G f {..0} = f 0

lemma finsum_Suc:

      [| abelian_monoid G; f ∈ {..Suc n} -> carrier G |]
      ==> finsum G f {..Suc n} = f (Suc n) ⊕G finsum G f {..n}

lemma finsum_Suc2:

      [| abelian_monoid G; f ∈ {..Suc n} -> carrier G |]
      ==> finsum G f {..Suc n} = (\<Oplus>Gi∈{..n}. f (Suc i)) ⊕G f 0

lemma finsum_add:

      [| abelian_monoid G; f ∈ {..n} -> carrier G; g ∈ {..n} -> carrier G |]
      ==> (\<Oplus>Gi∈{..n}. f i ⊕G g i) = finsum G f {..n} ⊕G finsum G g {..n}

lemma finsum_cong:

      [| abelian_monoid G; A = B; f ∈ B -> carrier G; !!i. i ∈ B =simp=> f i = g i |]
      ==> finsum G f A = finsum G g B

## The Algebraic Hierarchy of Rings

### Basic Definitions

### Basic Facts of Rings

lemma ringI:

      [| abelian_group R; monoid R;
         !!x y z.
            [| x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
            ==> (x ⊕R y) ⊗R z = x ⊗R z ⊕R y ⊗R z;
         !!x y z.
            [| x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
            ==> z ⊗R (x ⊕R y) = z ⊗R x ⊕R z ⊗R y |]
      ==> ring R

lemma is_abelian_group:

      ring R ==> abelian_group R

lemma is_monoid:

      ring R ==> monoid R

lemma cringI:

      [| abelian_group R; comm_monoid R;
         !!x y z.
            [| x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
            ==> (x ⊕R y) ⊗R z = x ⊗R z ⊕R y ⊗R z |]
      ==> cring R

lemma is_comm_monoid:

      cring R ==> comm_monoid R

### Normaliser for Rings

lemma r_neg2:

      [| abelian_group G; x ∈ carrier G; y ∈ carrier G |]
      ==> x ⊕G (\<ominus>G x ⊕G y) = y

lemma r_neg1:

      [| abelian_group G; x ∈ carrier G; y ∈ carrier G |]
      ==> \<ominus>G x ⊕G (x ⊕G y) = y

lemma l_null:

      [| ring R; x ∈ carrier R |] ==> \<zero>R ⊗R x = \<zero>R

lemma r_null:

      [| ring R; x ∈ carrier R |] ==> x ⊗R \<zero>R = \<zero>R

lemma l_minus:

      [| ring R; x ∈ carrier R; y ∈ carrier R |]
      ==> \<ominus>R x ⊗R y = \<ominus>R (x ⊗R y)

lemma r_minus:

      [| ring R; x ∈ carrier R; y ∈ carrier R |]
      ==> x ⊗R \<ominus>R y = \<ominus>R (x ⊗R y)

lemma minus_eq:

      [| ring R; x ∈ carrier R; y ∈ carrier R |]
      ==> x \<ominus>R y = x ⊕R \<ominus>R y

lemmas ring_simprules:

      [| ring R; x ∈ carrier R; y ∈ carrier R |] ==> x ⊕R y ∈ carrier R

      ring R ==> \<zero>R ∈ carrier R

      [| ring R; x ∈ carrier R |] ==> \<ominus>R x ∈ carrier R

      [| ring R; x ∈ carrier R; y ∈ carrier R |] ==> x \<ominus>R y ∈ carrier R

      [| ring R; x ∈ carrier R; y ∈ carrier R |] ==> x ⊗R y ∈ carrier R

      ring R ==> \<one>R ∈ carrier R

      [| ring R; x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
      ==> x ⊕R y ⊕R z = x ⊕R (y ⊕R z)

      [| ring R; x ∈ carrier R |] ==> \<zero>R ⊕R x = x

      [| ring R; x ∈ carrier R |] ==> \<ominus>R x ⊕R x = \<zero>R

      [| ring R; x ∈ carrier R; y ∈ carrier R |] ==> x ⊕R y = y ⊕R x

      [| ring R; x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
      ==> x ⊗R y ⊗R z = x ⊗R (y ⊗R z)

      [| ring R; x ∈ carrier R |] ==> \<one>R ⊗R x = x

      [| ring R; x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
      ==> (x ⊕R y) ⊗R z = x ⊗R z ⊕R y ⊗R z

      [| ring R; x ∈ carrier R; y ∈ carrier R |]
      ==> x \<ominus>R y = x ⊕R \<ominus>R y

      [| ring R; x ∈ carrier R |] ==> x ⊕R \<zero>R = x

      [| ring R; x ∈ carrier R |] ==> x ⊕R \<ominus>R x = \<zero>R

      [| ring R; x ∈ carrier R; y ∈ carrier R |] ==> x ⊕R (\<ominus>R x ⊕R y) = y

      [| ring R; x ∈ carrier R; y ∈ carrier R |] ==> \<ominus>R x ⊕R (x ⊕R y) = y

      [| ring R; x ∈ carrier R; y ∈ carrier R |]
      ==> \<ominus>R (x ⊕R y) = \<ominus>R x ⊕R \<ominus>R y

      [| ring R; x ∈ carrier R |] ==> \<ominus>R (\<ominus>R x) = x

      ring R ==> \<ominus>R \<zero>R = \<zero>R

      [| ring R; x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
      ==> x ⊕R (y ⊕R z) = y ⊕R (x ⊕R z)

      [| ring R; x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
      ==> z ⊗R (x ⊕R y) = z ⊗R x ⊕R z ⊗R y

      [| ring R; x ∈ carrier R |] ==> \<zero>R ⊗R x = \<zero>R

      [| ring R; x ∈ carrier R |] ==> x ⊗R \<zero>R = \<zero>R

      [| ring R; x ∈ carrier R; y ∈ carrier R |]
      ==> \<ominus>R x ⊗R y = \<ominus>R (x ⊗R y)

      [| ring R; x ∈ carrier R; y ∈ carrier R |]
      ==> x ⊗R \<ominus>R y = \<ominus>R (x ⊗R y)

lemmas cring_simprules:

      [| cring R; x ∈ carrier R; y ∈ carrier R |] ==> x ⊕R y ∈ carrier R

      cring R ==> \<zero>R ∈ carrier R

      [| cring R; x ∈ carrier R |] ==> \<ominus>R x ∈ carrier R

      [| cring R; x ∈ carrier R; y ∈ carrier R |] ==> x \<ominus>R y ∈ carrier R

      [| cring R; x ∈ carrier R; y ∈ carrier R |] ==> x ⊗R y ∈ carrier R

      cring R ==> \<one>R ∈ carrier R

      [| cring R; x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
      ==> x ⊕R y ⊕R z = x ⊕R (y ⊕R z)

      [| cring R; x ∈ carrier R |] ==> \<zero>R ⊕R x = x

      [| cring R; x ∈ carrier R |] ==> \<ominus>R x ⊕R x = \<zero>R

      [| cring R; x ∈ carrier R; y ∈ carrier R |] ==> x ⊕R y = y ⊕R x

      [| cring R; x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
      ==> x ⊗R y ⊗R z = x ⊗R (y ⊗R z)

      [| cring R; x ∈ carrier R |] ==> \<one>R ⊗R x = x

      [| cring R; x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
      ==> (x ⊕R y) ⊗R z = x ⊗R z ⊕R y ⊗R z

      [| cring R; x ∈ carrier R; y ∈ carrier R |] ==> x ⊗R y = y ⊗R x

      [| cring R; x ∈ carrier R; y ∈ carrier R |]
      ==> x \<ominus>R y = x ⊕R \<ominus>R y

      [| cring R; x ∈ carrier R |] ==> x ⊕R \<zero>R = x

      [| cring R; x ∈ carrier R |] ==> x ⊕R \<ominus>R x = \<zero>R

      [| cring R; x ∈ carrier R; y ∈ carrier R |] ==> x ⊕R (\<ominus>R x ⊕R y) = y

      [| cring R; x ∈ carrier R; y ∈ carrier R |] ==> \<ominus>R x ⊕R (x ⊕R y) = y

      [| cring R; x ∈ carrier R; y ∈ carrier R |]
      ==> \<ominus>R (x ⊕R y) = \<ominus>R x ⊕R \<ominus>R y

      [| cring R; x ∈ carrier R |] ==> \<ominus>R (\<ominus>R x) = x

      cring R ==> \<ominus>R \<zero>R = \<zero>R

      [| cring R; x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
      ==> x ⊕R (y ⊕R z) = y ⊕R (x ⊕R z)

      [| cring R; x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
      ==> x ⊗R (y ⊗R z) = y ⊗R (x ⊗R z)

      [| cring R; x ∈ carrier R; y ∈ carrier R; z ∈ carrier R |]
      ==> z ⊗R (x ⊕R y) = z ⊗R x ⊕R z ⊗R y

      [| cring R; x ∈ carrier R |] ==> \<zero>R ⊗R x = \<zero>R

      [| cring R; x ∈ carrier R |] ==> x ⊗R \<zero>R = \<zero>R

      [| cring R; x ∈ carrier R; y ∈ carrier R |]
      ==> \<ominus>R x ⊗R y = \<ominus>R (x ⊗R y)

      [| cring R; x ∈ carrier R; y ∈ carrier R |]
      ==> x ⊗R \<ominus>R y = \<ominus>R (x ⊗R y)

lemma nat_pow_zero:

      [| cring R; n ≠ 0 |] ==> \<zero>R (^)R n = \<zero>R

lemma

      [| ring R; cring S; a ∈ carrier R; b ∈ carrier R; c ∈ carrier S;
         d ∈ carrier S |]
      ==> a ⊕R \<ominus>R (a ⊕R \<ominus>R b) = b ∧ c ⊗S d = d ⊗S c

lemma

      [| cring R; a ∈ carrier R; b ∈ carrier R |]
      ==> a \<ominus>R (a \<ominus>R b) = b

### Sums over Finite Sets

lemma finsum_ldistr:

      [| cring R; finite A; a ∈ carrier R; f ∈ A -> carrier R |]
      ==> finsum R f A ⊗R a = (\<Oplus>Ri∈A. f i ⊗R a)

lemma finsum_rdistr:

      [| cring R; finite A; a ∈ carrier R; f ∈ A -> carrier R |]
      ==> a ⊗R finsum R f A = (\<Oplus>Ri∈A. a ⊗R f i)

### Facts of Integral Domains

lemma zero_not_one:

      domain R ==> \<zero>R ≠ \<one>R

lemma integral_iff:

      [| domain R; a ∈ carrier R; b ∈ carrier R |]
      ==> (a ⊗R b = \<zero>R) = (a = \<zero>R ∨ b = \<zero>R)

lemma m_lcancel:

      [| domain R; a ≠ \<zero>R; a ∈ carrier R; b ∈ carrier R; c ∈ carrier R |]
      ==> (a ⊗R b = a ⊗R c) = (b = c)

lemma conc:

      [| domain R; a ≠ \<zero>R; a ∈ carrier R; b ∈ carrier R; c ∈ carrier R |]
      ==> (b ⊗R a = c ⊗R a) = (b = c)

### Morphisms

lemma ring_hom_memI:

      [| !!x. x ∈ carrier R ==> h x ∈ carrier S;
         !!x y. [| x ∈ carrier R; y ∈ carrier R |] ==> h (x ⊗R y) = h x ⊗S h y;
         !!x y. [| x ∈ carrier R; y ∈ carrier R |] ==> h (x ⊕R y) = h x ⊕S h y;
         h \<one>R = \<one>S |]
      ==> h ∈ ring_hom R S

lemma ring_hom_closed:

      [| h ∈ ring_hom R S; x ∈ carrier R |] ==> h x ∈ carrier S

lemma ring_hom_mult:

      [| h ∈ ring_hom R S; x ∈ carrier R; y ∈ carrier R |] ==> h (x ⊗R y) = h x ⊗S h y

lemma ring_hom_add:

      [| h ∈ ring_hom R S; x ∈ carrier R; y ∈ carrier R |] ==> h (x ⊕R y) = h x ⊕S h y

lemma ring_hom_one:

      h ∈ ring_hom R S ==> h \<one>R = \<one>S

lemma hom_zero:

      ring_hom_cring R S h ==> h \<zero>R = \<zero>S

lemma hom_a_inv:

      [| ring_hom_cring R S h; x ∈ carrier R |] ==> h (\<ominus>R x) = \<ominus>S h x

lemma hom_finsum:

      [| ring_hom_cring R S h; finite A; f ∈ A -> carrier R |]
      ==> h (finsum R f A) = finsum S (h o f) A

lemma hom_finprod:

      [| ring_hom_cring R S h; finite A; f ∈ A -> carrier R |]
      ==> h (finprod R f A) = finprod S (h o f) A

lemma id_ring_hom:

      id ∈ ring_hom R R