Theory Ring

(*  Title:      HOL/Algebra/Ring.thy
    Author:     Clemens Ballarin, started 9 December 1996

With contributions by Martin Baillon.
*)

theory Ring
imports FiniteProduct
begin

section β€ΉThe Algebraic Hierarchy of Ringsβ€Ί

subsection β€ΉAbelian Groupsβ€Ί

record 'a ring = "'a monoid" +
  zero :: 'a ("𝟬ı")
  add :: "['a, 'a] β‡’ 'a" (infixl "βŠ•Δ±" 65)

abbreviation
  add_monoid :: "('a, 'm) ring_scheme β‡’ ('a, 'm) monoid_scheme"
  where "add_monoid R ≑ ⦇ carrier = carrier R, mult = add R, one = zero R, … = (undefined :: 'm) ⦈"

text β€ΉDerived operations.β€Ί

definition
  a_inv :: "[('a, 'm) ring_scheme, 'a ] β‡’ 'a" ("βŠ–Δ± _" [81] 80)
  where "a_inv R = m_inv (add_monoid R)"

definition
  a_minus :: "[('a, 'm) ring_scheme, 'a, 'a] => 'a" ("(_ βŠ–Δ± _)" [65,66] 65)
  where "x βŠ–β‡˜R⇙ y = x βŠ•β‡˜R⇙ (βŠ–β‡˜R⇙ y)"

definition
  add_pow :: "[_, ('b :: semiring_1), 'a] β‡’ 'a" ("[_] β‹…Δ± _" [81, 81] 80)
  where "add_pow R k a = pow (add_monoid R) a k"

locale abelian_monoid =
  fixes G (structure)
  assumes a_comm_monoid:
     "comm_monoid (add_monoid G)"

definition
  finsum :: "[('b, 'm) ring_scheme, 'a β‡’ 'b, 'a set] β‡’ 'b" where
  "finsum G = finprod (add_monoid G)"

syntax
  "_finsum" :: "index β‡’ idt β‡’ 'a set β‡’ 'b β‡’ 'b"
      ("(3⨁__∈_. _)" [1000, 0, 51, 10] 10)
translations
  "β¨β‡˜G⇙i∈A. b" β‡Œ "CONST finsum G (Ξ»i. b) A"
  ― β€ΉBeware of argument permutation!β€Ί


locale abelian_group = abelian_monoid +
  assumes a_comm_group:
     "comm_group (add_monoid G)"


subsection β€ΉBasic Propertiesβ€Ί

lemma abelian_monoidI:
  fixes R (structure)
  assumes "β‹€x y. ⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ x βŠ• y ∈ carrier R"
      and "𝟬 ∈ carrier R"
      and "β‹€x y z. ⟦ x ∈ carrier R; y ∈ carrier R; z ∈ carrier R ⟧ ⟹ (x βŠ• y) βŠ• z = x βŠ• (y βŠ• z)"
      and "β‹€x. x ∈ carrier R ⟹ 𝟬 βŠ• x = x"
      and "β‹€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: assms)

lemma abelian_monoidE:
  fixes R (structure)
  assumes "abelian_monoid R"
  shows "β‹€x y. ⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ x βŠ• y ∈ carrier R"
    and "𝟬 ∈ carrier R"
    and "β‹€x y z. ⟦ x ∈ carrier R; y ∈ carrier R; z ∈ carrier R ⟧ ⟹ (x βŠ• y) βŠ• z = x βŠ• (y βŠ• z)"
    and "β‹€x. x ∈ carrier R ⟹ 𝟬 βŠ• x = x"
    and "β‹€x y. ⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ x βŠ• y = y βŠ• x"
  using assms unfolding abelian_monoid_def comm_monoid_def comm_monoid_axioms_def monoid_def by auto

lemma abelian_groupI:
  fixes R (structure)
  assumes "β‹€x y. ⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ x βŠ• y ∈ carrier R"
      and "𝟬 ∈ carrier R"
      and "β‹€x y z. ⟦ x ∈ carrier R; y ∈ carrier R; z ∈ carrier R ⟧ ⟹ (x βŠ• y) βŠ• z = x βŠ• (y βŠ• z)"
      and "β‹€x y. ⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ x βŠ• y = y βŠ• x"
      and "β‹€x. x ∈ carrier R ⟹ 𝟬 βŠ• x = x"
      and "β‹€x. x ∈ carrier R ⟹ βˆƒy ∈ carrier R. y βŠ• x = 𝟬"
  shows "abelian_group R"
  by (auto intro!: abelian_group.intro abelian_monoidI
      abelian_group_axioms.intro comm_monoidI comm_groupI
    intro: assms)

lemma abelian_groupE:
  fixes R (structure)
  assumes "abelian_group R"
  shows "β‹€x y. ⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ x βŠ• y ∈ carrier R"
    and "𝟬 ∈ carrier R"
    and "β‹€x y z. ⟦ x ∈ carrier R; y ∈ carrier R; z ∈ carrier R ⟧ ⟹ (x βŠ• y) βŠ• z = x βŠ• (y βŠ• z)"
    and "β‹€x y. ⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ x βŠ• y = y βŠ• x"
    and "β‹€x. x ∈ carrier R ⟹ 𝟬 βŠ• x = x"
    and "β‹€x. x ∈ carrier R ⟹ βˆƒy ∈ carrier R. y βŠ• x = 𝟬"
  using abelian_group.a_comm_group assms comm_groupE by fastforce+

lemma (in abelian_monoid) a_monoid:
  "monoid (add_monoid G)"
by (rule comm_monoid.axioms, rule a_comm_monoid)

lemma (in abelian_group) a_group:
  "group (add_monoid G)"
  by (simp add: group_def a_monoid)
    (simp add: comm_group.axioms group.axioms a_comm_group)

lemmas monoid_record_simps = partial_object.simps monoid.simps

text β€ΉTransfer facts from multiplicative structures via interpretation.β€Ί

sublocale abelian_monoid <
       add: monoid "(add_monoid G)"
  rewrites "carrier (add_monoid G) = carrier G"
       and "mult    (add_monoid G) = add G"
       and "one     (add_monoid G) = zero G"
       and "(Ξ»a k. pow (add_monoid G) a k) = (Ξ»a k. add_pow G k a)"
  by (rule a_monoid) (auto simp add: add_pow_def)

context abelian_monoid
begin

lemmas a_closed = add.m_closed
lemmas zero_closed = add.one_closed
lemmas a_assoc = add.m_assoc
lemmas l_zero = add.l_one
lemmas r_zero = add.r_one
lemmas minus_unique = add.inv_unique

end

sublocale abelian_monoid <
  add: comm_monoid "(add_monoid G)"
  rewrites "carrier (add_monoid G) = carrier G"
       and "mult    (add_monoid G) = add G"
       and "one     (add_monoid G) = zero G"
       and "finprod (add_monoid G) = finsum G"
       and "pow     (add_monoid G) = (Ξ»a k. add_pow G k a)"
  by (rule a_comm_monoid) (auto simp: finsum_def add_pow_def)

context abelian_monoid begin

lemmas a_comm = add.m_comm
lemmas a_lcomm = add.m_lcomm
lemmas a_ac = a_assoc a_comm a_lcomm

lemmas finsum_empty = add.finprod_empty
lemmas finsum_insert = add.finprod_insert
lemmas finsum_zero = add.finprod_one
lemmas finsum_closed = add.finprod_closed
lemmas finsum_Un_Int = add.finprod_Un_Int
lemmas finsum_Un_disjoint = add.finprod_Un_disjoint
lemmas finsum_addf = add.finprod_multf
lemmas finsum_cong' = add.finprod_cong'
lemmas finsum_0 = add.finprod_0
lemmas finsum_Suc = add.finprod_Suc
lemmas finsum_Suc2 = add.finprod_Suc2
lemmas finsum_infinite = add.finprod_infinite

lemmas finsum_cong = add.finprod_cong
text β€ΉUsually, if this rule causes a failed congruence proof error,
   the reason is that the premise β€Ήg ∈ B β†’ carrier Gβ€Ί cannot be shown.
   Adding @{thm [source] Pi_def} to the simpset is often useful.β€Ί

lemmas finsum_reindex = add.finprod_reindex

(* The following would be wrong.  Needed is the equivalent of [^] for addition,
  or indeed the canonical embedding from Nat into the monoid.

lemma finsum_const:
  assumes fin [simp]: "finite A"
      and a [simp]: "a : carrier G"
    shows "finsum G (%x. a) A = a [^] card A"
  using fin apply induct
  apply force
  apply (subst finsum_insert)
  apply auto
  apply (force simp add: Pi_def)
  apply (subst m_comm)
  apply auto
done
*)

lemmas finsum_singleton = add.finprod_singleton

end

sublocale abelian_group <
        add: group "(add_monoid G)"
  rewrites "carrier (add_monoid G) = carrier G"
       and "mult    (add_monoid G) = add G"
       and "one     (add_monoid G) = zero G"
       and "m_inv   (add_monoid G) = a_inv G"
       and "pow     (add_monoid G) = (Ξ»a k. add_pow G k a)"
  by (rule a_group) (auto simp: m_inv_def a_inv_def add_pow_def)

context abelian_group
begin

lemmas a_inv_closed = add.inv_closed

lemma minus_closed [intro, simp]:
  "[| x ∈ carrier G; y ∈ carrier G |] ==> x βŠ– y ∈ carrier G"
  by (simp add: a_minus_def)

lemmas l_neg = add.l_inv [simp del]
lemmas r_neg = add.r_inv [simp del]
lemmas minus_minus = add.inv_inv
lemmas a_inv_inj = add.inv_inj
lemmas minus_equality = add.inv_equality

end

sublocale abelian_group <
   add: comm_group "(add_monoid G)"
  rewrites "carrier (add_monoid G) = carrier G"
       and "mult    (add_monoid G) = add G"
       and "one     (add_monoid G) = zero G"
       and "m_inv   (add_monoid G) = a_inv G"
       and "finprod (add_monoid G) = finsum G"
       and "pow     (add_monoid G) = (Ξ»a k. add_pow G k a)"
  by (rule a_comm_group) (auto simp: m_inv_def a_inv_def finsum_def add_pow_def)

lemmas (in abelian_group) minus_add = add.inv_mult

text β€ΉDerive an β€Ήabelian_groupβ€Ί from a β€Ήcomm_groupβ€Ίβ€Ί

lemma comm_group_abelian_groupI:
  fixes G (structure)
  assumes cg: "comm_group (add_monoid G)"
  shows "abelian_group G"
proof -
  interpret comm_group "(add_monoid G)"
    by (rule cg)
  show "abelian_group G" ..
qed


subsection β€ΉRings: Basic Definitionsβ€Ί

locale semiring = abelian_monoid (* for add *) R + monoid (* for mult *) R for R (structure) +
  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"
      and l_null[simp]: "x ∈ carrier R ⟹ 𝟬 βŠ— x = 𝟬"
      and r_null[simp]: "x ∈ carrier R ⟹ x βŠ— 𝟬 = 𝟬"

locale ring = abelian_group (* for add *) R + monoid (* for mult *) R for R (structure) +
  assumes "⟦ x ∈ carrier R; y ∈ carrier R; z ∈ carrier R ⟧ ⟹ (x βŠ• y) βŠ— z = x βŠ— z βŠ• y βŠ— z"
      and "⟦ x ∈ carrier R; y ∈ carrier R; z ∈ carrier R ⟧ ⟹ z βŠ— (x βŠ• y) = z βŠ— x βŠ• z βŠ— y"

locale cring = ring + comm_monoid (* for mult *) R

locale "domain" = cring +
  assumes one_not_zero [simp]: "𝟭 β‰  𝟬"
      and integral: "⟦ a βŠ— b = 𝟬; a ∈ carrier R; b ∈ carrier R ⟧ ⟹ a = 𝟬 ∨ b = 𝟬"

locale field = "domain" +
  assumes field_Units: "Units R = carrier R - {𝟬}"


subsection β€ΉRingsβ€Ί

lemma ringI:
  fixes R (structure)
  assumes "abelian_group R"
      and "monoid R"
      and "β‹€x y z. ⟦ x ∈ carrier R; y ∈ carrier R; z ∈ carrier R ⟧ ⟹ (x βŠ• y) βŠ— z = x βŠ— z βŠ• y βŠ— z"
      and "β‹€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 assms)

lemma ringE:
  fixes R (structure)
  assumes "ring R"
  shows "abelian_group R"
    and "monoid R"
    and "β‹€x y z. ⟦ x ∈ carrier R; y ∈ carrier R; z ∈ carrier R ⟧ ⟹ (x βŠ• y) βŠ— z = x βŠ— z βŠ• y βŠ— z"
    and "β‹€x y z. ⟦ x ∈ carrier R; y ∈ carrier R; z ∈ carrier R ⟧ ⟹ z βŠ— (x βŠ• y) = z βŠ— x βŠ• z βŠ— y"
  using assms unfolding ring_def ring_axioms_def by auto

context ring begin

lemma is_abelian_group: "abelian_group R" ..

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

end

thm monoid_record_simps
lemmas ring_record_simps = monoid_record_simps ring.simps

lemma cringI:
  fixes R (structure)
  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 (intro cring.intro ring.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 (rule l_distr)
qed (auto intro: cring.intro
  abelian_group.axioms comm_monoid.axioms ring_axioms.intro assms)

lemma cringE:
  fixes R (structure)
  assumes "cring R"
  shows "comm_monoid R"
    and "β‹€x y z. ⟦ x ∈ carrier R; y ∈ carrier R; z ∈ carrier R ⟧ ⟹ (x βŠ• y) βŠ— z = x βŠ— z βŠ• y βŠ— z"
  using assms cring_def by auto (simp add: assms cring.axioms(1) ringE(3))

lemma (in cring) is_cring:
  "cring R" by (rule cring_axioms)

lemma (in ring) minus_zero [simp]: "βŠ– 𝟬 = 𝟬"
  by (simp add: a_inv_def)

subsubsection β€ΉNormaliser for Ringsβ€Ί

lemma (in abelian_group) r_neg1:
  "⟦ x ∈ carrier G; y ∈ carrier G ⟧ ⟹ (βŠ– x) βŠ• (x βŠ• y) = y"
proof -
  assume G: "x ∈ carrier G" "y ∈ carrier G"
  then have "(βŠ– x βŠ• x) βŠ• y = y"
    by (simp only: l_neg l_zero)
  with G show ?thesis by (simp add: a_ac)
qed

lemma (in abelian_group) r_neg2:
  "⟦ x ∈ carrier G; y ∈ carrier G ⟧ ⟹ x βŠ• ((βŠ– x) βŠ• y) = y"
proof -
  assume G: "x ∈ carrier G" "y ∈ carrier G"
  then have "(x βŠ• βŠ– x) βŠ• y = y"
    by (simp only: r_neg l_zero)
  with G show ?thesis
    by (simp add: a_ac)
qed

context ring begin

text β€Ή
  The following proofs are from Jacobson, Basic Algebra I, pp.~88--89.
β€Ί

sublocale semiring
proof -
  note [simp] = ring_axioms[unfolded ring_def ring_axioms_def]
  show "semiring R"
  proof (unfold_locales)
    fix x
    assume R: "x ∈ carrier R"
    then have "𝟬 βŠ— x βŠ• 𝟬 βŠ— x = (𝟬 βŠ• 𝟬) βŠ— x"
      by (simp del: l_zero r_zero)
    also from R have "... = 𝟬 βŠ— x βŠ• 𝟬" by simp
    finally have "𝟬 βŠ— x βŠ• 𝟬 βŠ— x = 𝟬 βŠ— x βŠ• 𝟬" .
    with R show "𝟬 βŠ— x = 𝟬" by (simp del: r_zero)
    from R have "x βŠ— 𝟬 βŠ• x βŠ— 𝟬 = x βŠ— (𝟬 βŠ• 𝟬)"
      by (simp del: l_zero r_zero)
    also from R have "... = x βŠ— 𝟬 βŠ• 𝟬" by simp
    finally have "x βŠ— 𝟬 βŠ• x βŠ— 𝟬 = x βŠ— 𝟬 βŠ• 𝟬" .
    with R show "x βŠ— 𝟬 = 𝟬" by (simp del: r_zero)
  qed auto
qed

lemma l_minus:
  "⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ (βŠ– x) βŠ— y = βŠ– (x βŠ— y)"
proof -
  assume R: "x ∈ carrier R" "y ∈ carrier R"
  then have "(βŠ– x) βŠ— y βŠ• x βŠ— y = (βŠ– x βŠ• x) βŠ— y" by (simp add: l_distr)
  also from R have "... = 𝟬" by (simp add: l_neg)
  finally have "(βŠ– x) βŠ— y βŠ• x βŠ— y = 𝟬" .
  with R have "(βŠ– x) βŠ— y βŠ• x βŠ— y βŠ• βŠ– (x βŠ— y) = 𝟬 βŠ• βŠ– (x βŠ— y)" by simp
  with R show ?thesis by (simp add: a_assoc r_neg)
qed

lemma r_minus:
  "⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ x βŠ— (βŠ– y) = βŠ– (x βŠ— y)"
proof -
  assume R: "x ∈ carrier R" "y ∈ carrier R"
  then have "x βŠ— (βŠ– y) βŠ• x βŠ— y = x βŠ— (βŠ– y βŠ• y)" by (simp add: r_distr)
  also from R have "... = 𝟬" by (simp add: l_neg)
  finally have "x βŠ— (βŠ– y) βŠ• x βŠ— y = 𝟬" .
  with R have "x βŠ— (βŠ– y) βŠ• x βŠ— y βŠ• βŠ– (x βŠ— y) = 𝟬 βŠ• βŠ– (x βŠ— y)" by simp
  with R show ?thesis by (simp add: a_assoc r_neg )
qed

end

lemma (in abelian_group) minus_eq: "x βŠ– y = x βŠ• (βŠ– y)"
  by (rule a_minus_def)

text β€ΉSetup algebra method:
  compute distributive normal form in locale contextsβ€Ί


ML_file β€Ήringsimp.MLβ€Ί

attribute_setup algebra = β€Ή
  Scan.lift ((Args.add >> K true || Args.del >> K false) --| Args.colon || Scan.succeed true)
    -- Scan.lift Args.name -- Scan.repeat Args.term
    >> (fn ((b, n), ts) => if b then Ringsimp.add_struct (n, ts) else Ringsimp.del_struct (n, ts))
β€Ί "theorems controlling algebra method"

method_setup algebra = β€Ή
  Scan.succeed (SIMPLE_METHOD' o Ringsimp.algebra_tac)
β€Ί "normalisation of algebraic structure"

lemmas (in semiring) semiring_simprules
  [algebra ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
  a_closed zero_closed  m_closed one_closed
  a_assoc l_zero  a_comm m_assoc l_one l_distr r_zero
  a_lcomm r_distr l_null r_null

lemmas (in ring) ring_simprules
  [algebra ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
  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)
  [algebra del: ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
  _

lemmas (in cring) cring_simprules
  [algebra add: cring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
  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

lemma (in semiring) nat_pow_zero:
  "(n::nat) β‰  0 ⟹ 𝟬 [^] n = 𝟬"
  by (induct n) simp_all

context semiring begin

lemma one_zeroD:
  assumes onezero: "𝟭 = 𝟬"
  shows "carrier R = {𝟬}"
proof (rule, rule)
  fix x
  assume xcarr: "x ∈ carrier R"
  from xcarr have "x = x βŠ— 𝟭" by simp
  with onezero have "x = x βŠ— 𝟬" by simp
  with xcarr have "x = 𝟬" by simp
  then show "x ∈ {𝟬}" by fast
qed fast

lemma one_zeroI:
  assumes carrzero: "carrier R = {𝟬}"
  shows "𝟭 = 𝟬"
proof -
  from one_closed and carrzero
      show "𝟭 = 𝟬" by simp
qed

lemma carrier_one_zero: "(carrier R = {𝟬}) = (𝟭 = 𝟬)"
  using one_zeroD by blast

lemma carrier_one_not_zero: "(carrier R β‰  {𝟬}) = (𝟭 β‰  𝟬)"
  by (simp add: carrier_one_zero)

end

text β€ΉTwo examples for use of method algebraβ€Ί

lemma
  fixes R (structure) and S (structure)
  assumes "ring R" "cring S"
  assumes RS: "a ∈ carrier R" "b ∈ carrier R" "c ∈ carrier S" "d ∈ carrier S"
  shows "a βŠ• (βŠ– (a βŠ• (βŠ– b))) = b ∧ c βŠ—β‡˜S⇙ d = d βŠ—β‡˜S⇙ c"
proof -
  interpret ring R by fact
  interpret cring S by fact
  from RS show ?thesis by algebra
qed

lemma
  fixes R (structure)
  assumes "ring R"
  assumes R: "a ∈ carrier R" "b ∈ carrier R"
  shows "a βŠ– (a βŠ– b) = b"
proof -
  interpret ring R by fact
  from R show ?thesis by algebra
qed


subsubsection β€ΉSums over Finite Setsβ€Ί

lemma (in semiring) finsum_ldistr:
  "⟦ finite A; a ∈ carrier R; f: A β†’ carrier R ⟧ ⟹
    (⨁ i ∈ A. (f i)) βŠ— a = (⨁ i ∈ A. ((f i) βŠ— a))"
proof (induct set: finite)
  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 semiring) finsum_rdistr:
  "⟦ finite A; a ∈ carrier R; f: A β†’ carrier R ⟧ ⟹
   a βŠ— (⨁ i ∈ A. (f i)) = (⨁ i ∈ A. (a βŠ— (f i)))"
proof (induct set: finite)
  case empty then show ?case by simp
next
  case (insert x F) then show ?case by (simp add: Pi_def r_distr)
qed


text β€ΉA quick detourβ€Ί

lemma add_pow_int_ge: "(k :: int) β‰₯ 0 ⟹ [ k ] β‹…β‡˜R⇙ a = [ nat k ] β‹…β‡˜R⇙ a" βœβ€Ήcontributor β€ΉPaulo EmΓ­lio de Vilhenaβ€Ίβ€Ί
  by (simp add: add_pow_def int_pow_def nat_pow_def)

lemma add_pow_int_lt: "(k :: int) < 0 ⟹ [ k ] β‹…β‡˜R⇙ a = βŠ–β‡˜R⇙ ([ nat (- k) ] β‹…β‡˜R⇙ a)" βœβ€Ήcontributor β€ΉPaulo EmΓ­lio de Vilhenaβ€Ίβ€Ί
  by (simp add: int_pow_def nat_pow_def a_inv_def add_pow_def)

corollary (in semiring) add_pow_ldistr: βœβ€Ήcontributor β€ΉPaulo EmΓ­lio de Vilhenaβ€Ίβ€Ί
  assumes "a ∈ carrier R" "b ∈ carrier R"
  shows "([(k :: nat)] β‹… a) βŠ— b = [k] β‹… (a βŠ— b)"
proof -
  have "([k] β‹… a) βŠ— b = (⨁ i ∈ {..< k}. a) βŠ— b"
    using add.finprod_const[OF assms(1), of "{..<k}"] by simp
  also have " ... = (⨁ i ∈ {..< k}. (a βŠ— b))"
    using finsum_ldistr[of "{..<k}" b "Ξ»x. a"] assms by simp
  also have " ... = [k] β‹… (a βŠ— b)"
    using add.finprod_const[of "a βŠ— b" "{..<k}"] assms by simp
  finally show ?thesis .
qed

corollary (in semiring) add_pow_rdistr: βœβ€Ήcontributor β€ΉPaulo EmΓ­lio de Vilhenaβ€Ίβ€Ί
  assumes "a ∈ carrier R" "b ∈ carrier R"
  shows "a βŠ— ([(k :: nat)] β‹… b) = [k] β‹… (a βŠ— b)"
proof -
  have "a βŠ— ([k] β‹… b) = a βŠ— (⨁ i ∈ {..< k}. b)"
    using add.finprod_const[OF assms(2), of "{..<k}"] by simp
  also have " ... = (⨁ i ∈ {..< k}. (a βŠ— b))"
    using finsum_rdistr[of "{..<k}" a "Ξ»x. b"] assms by simp
  also have " ... = [k] β‹… (a βŠ— b)"
    using add.finprod_const[of "a βŠ— b" "{..<k}"] assms by simp
  finally show ?thesis .
qed

(* For integers, we need the uniqueness of the additive inverse *)
lemma (in ring) add_pow_ldistr_int: βœβ€Ήcontributor β€ΉPaulo EmΓ­lio de Vilhenaβ€Ίβ€Ί
  assumes "a ∈ carrier R" "b ∈ carrier R"
  shows "([(k :: int)] β‹… a) βŠ— b = [k] β‹… (a βŠ— b)"
proof (cases "k β‰₯ 0")
  case True thus ?thesis
    using add_pow_int_ge[of k R] add_pow_ldistr[OF assms] by auto
next
  case False thus ?thesis
    using add_pow_int_lt[of k R a] add_pow_int_lt[of k R "a βŠ— b"]
          add_pow_ldistr[OF assms, of "nat (- k)"] assms l_minus by auto
qed

lemma (in ring) add_pow_rdistr_int: βœβ€Ήcontributor β€ΉPaulo EmΓ­lio de Vilhenaβ€Ίβ€Ί
  assumes "a ∈ carrier R" "b ∈ carrier R"
  shows "a βŠ— ([(k :: int)] β‹… b) = [k] β‹… (a βŠ— b)"
proof (cases "k β‰₯ 0")
  case True thus ?thesis
    using add_pow_int_ge[of k R] add_pow_rdistr[OF assms] by auto
next
  case False thus ?thesis
    using add_pow_int_lt[of k R b] add_pow_int_lt[of k R "a βŠ— b"]
          add_pow_rdistr[OF assms, of "nat (- k)"] assms r_minus by auto
qed


subsection β€ΉIntegral Domainsβ€Ί

context "domain" begin

lemma zero_not_one [simp]: "𝟬 β‰  𝟭"
  by (rule not_sym) simp

lemma integral_iff: (* not by default a simp rule! *)
  "⟦ a ∈ carrier R; b ∈ carrier R ⟧ ⟹ (a βŠ— b = 𝟬) = (a = 𝟬 ∨ b = 𝟬)"
proof
  assume "a ∈ carrier R" "b ∈ carrier R" "a βŠ— b = 𝟬"
  then show "a = 𝟬 ∨ b = 𝟬" by (simp add: integral)
next
  assume "a ∈ carrier R" "b ∈ carrier R" "a = 𝟬 ∨ b = 𝟬"
  then show "a βŠ— b = 𝟬" by auto
qed

lemma m_lcancel:
  assumes prem: "a β‰  𝟬"
    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 βŠ– c) = 𝟬" by algebra
  with R have "a = 𝟬 ∨ (b βŠ– c) = 𝟬" by (simp add: integral_iff)
  with prem and R have "b βŠ– c = 𝟬" by auto
  with R have "b = b βŠ– (b βŠ– c)" by algebra
  also from R have "b βŠ– (b βŠ– c) = c" by algebra
  finally show "b = c" .
next
  assume "b = c" then show "a βŠ— b = a βŠ— c" by simp
qed

lemma m_rcancel:
  assumes prem: "a β‰  𝟬"
    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

end


subsection β€ΉFieldsβ€Ί

text β€ΉField would not need to be derived from domain, the properties
  for domain follow from the assumptions of fieldβ€Ί

lemma (in field) is_ring: "ring R"
  using ring_axioms .

lemma fieldE :
  fixes R (structure)
  assumes "field R"
  shows "cring R"
    and one_not_zero : "𝟭 β‰  𝟬"
    and integral: "β‹€a b. ⟦ a βŠ— b = 𝟬; a ∈ carrier R; b ∈ carrier R ⟧ ⟹ a = 𝟬 ∨ b = 𝟬"
  and field_Units: "Units R = carrier R - {𝟬}"
  using assms unfolding field_def field_axioms_def domain_def domain_axioms_def by simp_all

lemma (in cring) cring_fieldI:
  assumes field_Units: "Units R = carrier R - {𝟬}"
  shows "field R"
proof
  from field_Units have "𝟬 βˆ‰ Units R" by fast
  moreover have "𝟭 ∈ Units R" by fast
  ultimately show "𝟭 β‰  𝟬" by force
next
  fix a b
  assume acarr: "a ∈ carrier R"
    and bcarr: "b ∈ carrier R"
    and ab: "a βŠ— b = 𝟬"
  show "a = 𝟬 ∨ b = 𝟬"
  proof (cases "a = 𝟬", simp)
    assume "a β‰  𝟬"
    with field_Units and acarr have aUnit: "a ∈ Units R" by fast
    from bcarr have "b = 𝟭 βŠ— b" by algebra
    also from aUnit acarr have "... = (inv a βŠ— a) βŠ— b" by simp
    also from acarr bcarr aUnit[THEN Units_inv_closed]
    have "... = (inv a) βŠ— (a βŠ— b)" by algebra
    also from ab and acarr bcarr aUnit have "... = (inv a) βŠ— 𝟬" by simp
    also from aUnit[THEN Units_inv_closed] have "... = 𝟬" by algebra
    finally have "b = 𝟬" .
    then show "a = 𝟬 ∨ b = 𝟬" by simp
  qed
qed (rule field_Units)

text β€ΉAnother variant to show that something is a fieldβ€Ί
lemma (in cring) cring_fieldI2:
  assumes notzero: "𝟬 β‰  𝟭"
    and invex: "β‹€a. ⟦a ∈ carrier R; a β‰  𝟬⟧ ⟹ βˆƒb∈carrier R. a βŠ— b = 𝟭"
  shows "field R"
proof -
  have *: "carrier R - {𝟬} βŠ† {y ∈ carrier R. βˆƒx∈carrier R. x βŠ— y = 𝟭 ∧ y βŠ— x = 𝟭}"
  proof (clarsimp)
    fix x
    assume xcarr: "x ∈ carrier R" and "x β‰  𝟬"
    obtain y where ycarr: "y ∈ carrier R" and xy: "x βŠ— y = 𝟭"
      using β€Ήx β‰  πŸ¬β€Ί invex xcarr by blast 
    with ycarr and xy show "βˆƒy∈carrier R. y βŠ— x = 𝟭 ∧ x βŠ— y = 𝟭"
      using m_comm xcarr by fastforce 
  qed
  show ?thesis
    apply (rule cring_fieldI, simp add: Units_def)
    using *
    using group_l_invI notzero set_diff_eq by auto
qed


subsection β€ΉMorphismsβ€Ί

definition
  ring_hom :: "[('a, 'm) ring_scheme, ('b, 'n) ring_scheme] => ('a => 'b) set"
  where "ring_hom R S =
    {h. h ∈ carrier R β†’ carrier S ∧
      (βˆ€x y. x ∈ carrier R ∧ y ∈ carrier R ⟢
        h (x βŠ—β‡˜R⇙ y) = h x βŠ—β‡˜S⇙ h y ∧ h (x βŠ•β‡˜R⇙ y) = h x βŠ•β‡˜S⇙ h y) ∧
      h πŸ­β‡˜R⇙ = πŸ­β‡˜S⇙}"

lemma ring_hom_memI:
  fixes R (structure) and S (structure)
  assumes "β‹€x. x ∈ carrier R ⟹ h x ∈ carrier S"
      and "β‹€x y. ⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ h (x βŠ— y) = h x βŠ—β‡˜S⇙ h y"
      and "β‹€x y. ⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ h (x βŠ• y) = h x βŠ•β‡˜S⇙ h y"
      and "h 𝟭 = πŸ­β‡˜S⇙"
  shows "h ∈ ring_hom R S"
  by (auto simp add: ring_hom_def assms Pi_def)

lemma ring_hom_memE:
  fixes R (structure) and S (structure)
  assumes "h ∈ ring_hom R S"
  shows "β‹€x. x ∈ carrier R ⟹ h x ∈ carrier S"
    and "β‹€x y. ⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ h (x βŠ— y) = h x βŠ—β‡˜S⇙ h y"
    and "β‹€x y. ⟦ x ∈ carrier R; y ∈ carrier R ⟧ ⟹ h (x βŠ• y) = h x βŠ•β‡˜S⇙ h y"
    and "h 𝟭 = πŸ­β‡˜S⇙"
  using assms unfolding ring_hom_def by auto

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:
  fixes R (structure) and S (structure)
  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:
  fixes R (structure) and S (structure)
  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:
  fixes R (structure) and S (structure)
  shows "h ∈ ring_hom R S ⟹ h 𝟭 = πŸ­β‡˜S⇙"
  by (simp add: ring_hom_def)

lemma ring_hom_zero:
  fixes R (structure) and S (structure)
  assumes "h ∈ ring_hom R S" "ring R" "ring S"
  shows "h 𝟬 = πŸ¬β‡˜S⇙"
proof -
  have "h 𝟬 = h 𝟬 βŠ•β‡˜S⇙ h 𝟬"
    using ring_hom_add[OF assms(1), of 𝟬 𝟬] assms(2)
    by (simp add: ring.ring_simprules(2) ring.ring_simprules(15))
  thus ?thesis
    by (metis abelian_group.l_neg assms ring.is_abelian_group ring.ring_simprules(18) ring.ring_simprules(2) ring_hom_closed)
qed

locale ring_hom_cring =
  R?: cring R + S?: cring S for R (structure) and S (structure) + fixes 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 𝟬 = πŸ¬β‡˜S⇙"
proof -
  have "h 𝟬 βŠ•β‡˜S⇙ h 𝟬 = h 𝟬 βŠ•β‡˜S⇙ πŸ¬β‡˜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 (βŠ– x) = βŠ–β‡˜S⇙ h x"
proof -
  assume R: "x ∈ carrier R"
  then have "h x βŠ•β‡˜S⇙ h (βŠ– x) = h x βŠ•β‡˜S⇙ (βŠ–β‡˜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]:
  assumes "f: A β†’ carrier R"
  shows "h (⨁ i ∈ A. f i) = (β¨β‡˜S⇙ i ∈ A. (h o f) i)"
  using assms by (induct A rule: infinite_finite_induct, auto simp: Pi_def)

lemma (in ring_hom_cring) hom_finprod:
  assumes "f: A β†’ carrier R"
  shows "h (⨂ i ∈ A. f i) = (β¨‚β‡˜S⇙ i ∈ A. (h o f) i)"
  using assms by (induct A rule: infinite_finite_induct, auto simp: Pi_def)

declare ring_hom_cring.hom_finprod [simp]

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

lemma ring_hom_trans: βœβ€Ήcontributor β€ΉPaulo EmΓ­lio de Vilhenaβ€Ίβ€Ί
  "⟦ f ∈ ring_hom R S; g ∈ ring_hom S T ⟧ ⟹ g ∘ f ∈ ring_hom R T"
  by (rule ring_hom_memI) (auto simp add: ring_hom_closed ring_hom_mult ring_hom_add ring_hom_one)

subsectionβ€ΉJeremy Avigad's β€ΉMore_Finite_Productβ€Ί materialβ€Ί

(* need better simplification rules for rings *)
(* the next one holds more generally for abelian groups *)

lemma (in cring) sum_zero_eq_neg: "x ∈ carrier R ⟹ y ∈ carrier R ⟹ x βŠ• y = 𝟬 ⟹ x = βŠ– y"
  by (metis minus_equality)

lemma (in domain) square_eq_one:
  fixes x
  assumes [simp]: "x ∈ carrier R"
    and "x βŠ— x = 𝟭"
  shows "x = 𝟭 ∨ x = βŠ–πŸ­"
proof -
  have "(x βŠ• 𝟭) βŠ— (x βŠ• βŠ– 𝟭) = x βŠ— x βŠ• βŠ– 𝟭"
    by (simp add: ring_simprules)
  also from β€Ήx βŠ— x = πŸ­β€Ί have "… = 𝟬"
    by (simp add: ring_simprules)
  finally have "(x βŠ• 𝟭) βŠ— (x βŠ• βŠ– 𝟭) = 𝟬" .
  then have "(x βŠ• 𝟭) = 𝟬 ∨ (x βŠ• βŠ– 𝟭) = 𝟬"
    by (intro integral) auto
  then show ?thesis
    by (metis add.inv_closed add.inv_solve_right assms(1) l_zero one_closed zero_closed)
qed

lemma (in domain) inv_eq_self: "x ∈ Units R ⟹ x = inv x ⟹ x = 𝟭 ∨ x = βŠ–πŸ­"
  by (metis Units_closed Units_l_inv square_eq_one)


text β€Ή
  The following translates theorems about groups to the facts about
  the units of a ring. (The list should be expanded as more things are
  needed.)
β€Ί

lemma (in ring) finite_ring_finite_units [intro]: "finite (carrier R) ⟹ finite (Units R)"
  by (rule finite_subset) auto

lemma (in monoid) units_of_pow:
  fixes n :: nat
  shows "x ∈ Units G ⟹ x [^]β‡˜units_of G⇙ n = x [^]β‡˜G⇙ n"
  apply (induct n)
  apply (auto simp add: units_group group.is_monoid
    monoid.nat_pow_0 monoid.nat_pow_Suc units_of_one units_of_mult)
  done

lemma (in cring) units_power_order_eq_one:
  "finite (Units R) ⟹ a ∈ Units R ⟹ a [^] card(Units R) = 𝟭"
  by (metis comm_group.power_order_eq_one units_comm_group units_of_carrier units_of_one units_of_pow)

subsectionβ€ΉJeremy Avigad's β€ΉMore_Ringβ€Ί materialβ€Ί

lemma (in cring) field_intro2: 
  assumes "πŸ¬β‡˜R⇙ β‰  πŸ­β‡˜R⇙" and un: "β‹€x. x ∈ carrier R - {πŸ¬β‡˜R⇙} ⟹ x ∈ Units R"
  shows "field R"
proof unfold_locales
  show "𝟭 β‰  𝟬" using assms by auto
  show "⟦a βŠ— b = 𝟬; a ∈ carrier R;
            b ∈ carrier R⟧
           ⟹ a = 𝟬 ∨ b = 𝟬" for a b
    by (metis un Units_l_cancel insert_Diff_single insert_iff r_null zero_closed)
qed (use assms in β€Ήauto simp: Units_defβ€Ί)

lemma (in monoid) inv_char:
  assumes "x ∈ carrier G" "y ∈ carrier G" "x βŠ— y = 𝟭" "y βŠ— x = 𝟭" 
  shows "inv x = y"
  using assms inv_unique' by auto

lemma (in comm_monoid) comm_inv_char: "x ∈ carrier G ⟹ y ∈ carrier G ⟹ x βŠ— y = 𝟭 ⟹ inv x = y"
  by (simp add: inv_char m_comm)

lemma (in ring) inv_neg_one [simp]: "inv (βŠ– 𝟭) = βŠ– 𝟭"
  by (simp add: inv_char local.ring_axioms ring.r_minus)

lemma (in monoid) inv_eq_imp_eq [dest!]: "inv x = inv y ⟹ x ∈ Units G ⟹ y ∈ Units G ⟹ x = y"
  by (metis Units_inv_inv)

lemma (in ring) Units_minus_one_closed [intro]: "βŠ– 𝟭 ∈ Units R"
  by (simp add: Units_def) (metis add.l_inv_ex local.minus_minus minus_equality one_closed r_minus r_one)

lemma (in ring) inv_eq_neg_one_eq: "x ∈ Units R ⟹ inv x = βŠ– 𝟭 ⟷ x = βŠ– 𝟭"
  by (metis Units_inv_inv inv_neg_one)

lemma (in monoid) inv_eq_one_eq: "x ∈ Units G ⟹ inv x = 𝟭 ⟷ x = 𝟭"
  by (metis Units_inv_inv inv_one)

end