src/HOL/Algebra/Ring.thy
author paulson <lp15@cam.ac.uk>
Thu Jun 14 14:23:38 2018 +0100 (10 months ago)
changeset 68445 c183a6a69f2d
parent 68443 43055b016688
child 68517 6b5f15387353
permissions -rw-r--r--
reorganisation of Algebra: new material from Baillon and Vilhena, removal of duplicate names, elimination of "More_" theories
     1 (*  Title:      HOL/Algebra/Ring.thy
     2     Author:     Clemens Ballarin, started 9 December 1996
     3     Copyright:  Clemens Ballarin
     4 *)
     5 
     6 theory Ring
     7 imports FiniteProduct
     8 begin
     9 
    10 section \<open>The Algebraic Hierarchy of Rings\<close>
    11 
    12 subsection \<open>Abelian Groups\<close>
    13 
    14 record 'a ring = "'a monoid" +
    15   zero :: 'a ("\<zero>\<index>")
    16   add :: "['a, 'a] \<Rightarrow> 'a" (infixl "\<oplus>\<index>" 65)
    17 
    18 abbreviation
    19   add_monoid :: "('a, 'm) ring_scheme \<Rightarrow> ('a, 'm) monoid_scheme"
    20   where "add_monoid R \<equiv> \<lparr> carrier = carrier R, mult = add R, one = zero R, \<dots> = (undefined :: 'm) \<rparr>"
    21 
    22 text \<open>Derived operations.\<close>
    23 
    24 definition
    25   a_inv :: "[('a, 'm) ring_scheme, 'a ] \<Rightarrow> 'a" ("\<ominus>\<index> _" [81] 80)
    26   where "a_inv R = m_inv (add_monoid R)"
    27 
    28 definition
    29   a_minus :: "[('a, 'm) ring_scheme, 'a, 'a] => 'a" ("(_ \<ominus>\<index> _)" [65,66] 65)
    30   where "x \<ominus>\<^bsub>R\<^esub> y = x \<oplus>\<^bsub>R\<^esub> (\<ominus>\<^bsub>R\<^esub> y)"
    31 
    32 definition
    33   add_pow :: "[_, ('b :: semiring_1), 'a] \<Rightarrow> 'a" ("[_] \<cdot>\<index> _" [81, 81] 80)
    34   where "add_pow R k a = pow (add_monoid R) a k"
    35 
    36 locale abelian_monoid =
    37   fixes G (structure)
    38   assumes a_comm_monoid:
    39      "comm_monoid (add_monoid G)"
    40 
    41 definition
    42   finsum :: "[('b, 'm) ring_scheme, 'a \<Rightarrow> 'b, 'a set] \<Rightarrow> 'b" where
    43   "finsum G = finprod (add_monoid G)"
    44 
    45 syntax
    46   "_finsum" :: "index \<Rightarrow> idt \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"
    47       ("(3\<Oplus>__\<in>_. _)" [1000, 0, 51, 10] 10)
    48 translations
    49   "\<Oplus>\<^bsub>G\<^esub>i\<in>A. b" \<rightleftharpoons> "CONST finsum G (\<lambda>i. b) A"
    50   \<comment> \<open>Beware of argument permutation!\<close>
    51 
    52 
    53 locale abelian_group = abelian_monoid +
    54   assumes a_comm_group:
    55      "comm_group (add_monoid G)"
    56 
    57 
    58 subsection \<open>Basic Properties\<close>
    59 
    60 lemma abelian_monoidI:
    61   fixes R (structure)
    62   assumes "\<And>x y. \<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> x \<oplus> y \<in> carrier R"
    63       and "\<zero> \<in> carrier R"
    64       and "\<And>x y z. \<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> (x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
    65       and "\<And>x. x \<in> carrier R \<Longrightarrow> \<zero> \<oplus> x = x"
    66       and "\<And>x y. \<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> x \<oplus> y = y \<oplus> x"
    67   shows "abelian_monoid R"
    68   by (auto intro!: abelian_monoid.intro comm_monoidI intro: assms)
    69 
    70 lemma abelian_monoidE:
    71   fixes R (structure)
    72   assumes "abelian_monoid R"
    73   shows "\<And>x y. \<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> x \<oplus> y \<in> carrier R"
    74     and "\<zero> \<in> carrier R"
    75     and "\<And>x y z. \<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> (x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
    76     and "\<And>x. x \<in> carrier R \<Longrightarrow> \<zero> \<oplus> x = x"
    77     and "\<And>x y. \<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> x \<oplus> y = y \<oplus> x"
    78   using assms unfolding abelian_monoid_def comm_monoid_def comm_monoid_axioms_def monoid_def by auto
    79 
    80 lemma abelian_groupI:
    81   fixes R (structure)
    82   assumes "\<And>x y. \<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> x \<oplus> y \<in> carrier R"
    83       and "\<zero> \<in> carrier R"
    84       and "\<And>x y z. \<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> (x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
    85       and "\<And>x y. \<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> x \<oplus> y = y \<oplus> x"
    86       and "\<And>x. x \<in> carrier R \<Longrightarrow> \<zero> \<oplus> x = x"
    87       and "\<And>x. x \<in> carrier R \<Longrightarrow> \<exists>y \<in> carrier R. y \<oplus> x = \<zero>"
    88   shows "abelian_group R"
    89   by (auto intro!: abelian_group.intro abelian_monoidI
    90       abelian_group_axioms.intro comm_monoidI comm_groupI
    91     intro: assms)
    92 
    93 lemma abelian_groupE:
    94   fixes R (structure)
    95   assumes "abelian_group R"
    96   shows "\<And>x y. \<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> x \<oplus> y \<in> carrier R"
    97     and "\<zero> \<in> carrier R"
    98     and "\<And>x y z. \<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> (x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
    99     and "\<And>x y. \<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> x \<oplus> y = y \<oplus> x"
   100     and "\<And>x. x \<in> carrier R \<Longrightarrow> \<zero> \<oplus> x = x"
   101     and "\<And>x. x \<in> carrier R \<Longrightarrow> \<exists>y \<in> carrier R. y \<oplus> x = \<zero>"
   102   using abelian_group.a_comm_group assms comm_groupE by fastforce+
   103 
   104 lemma (in abelian_monoid) a_monoid:
   105   "monoid (add_monoid G)"
   106 by (rule comm_monoid.axioms, rule a_comm_monoid) 
   107 
   108 lemma (in abelian_group) a_group:
   109   "group (add_monoid G)"
   110   by (simp add: group_def a_monoid)
   111     (simp add: comm_group.axioms group.axioms a_comm_group)
   112 
   113 lemmas monoid_record_simps = partial_object.simps monoid.simps
   114 
   115 text \<open>Transfer facts from multiplicative structures via interpretation.\<close>
   116 
   117 sublocale abelian_monoid <
   118        add: monoid "(add_monoid G)"
   119   rewrites "carrier (add_monoid G) = carrier G"
   120        and "mult    (add_monoid G) = add G"
   121        and "one     (add_monoid G) = zero G"
   122        and "(\<lambda>a k. pow (add_monoid G) a k) = (\<lambda>a k. add_pow G k a)"
   123   by (rule a_monoid) (auto simp add: add_pow_def)
   124 
   125 context abelian_monoid
   126 begin
   127 
   128 lemmas a_closed = add.m_closed 
   129 lemmas zero_closed = add.one_closed
   130 lemmas a_assoc = add.m_assoc
   131 lemmas l_zero = add.l_one
   132 lemmas r_zero = add.r_one
   133 lemmas minus_unique = add.inv_unique
   134 
   135 end
   136 
   137 sublocale abelian_monoid <
   138   add: comm_monoid "(add_monoid G)"
   139   rewrites "carrier (add_monoid G) = carrier G"
   140        and "mult    (add_monoid G) = add G"
   141        and "one     (add_monoid G) = zero G"
   142        and "finprod (add_monoid G) = finsum G"
   143        and "pow     (add_monoid G) = (\<lambda>a k. add_pow G k a)"
   144   by (rule a_comm_monoid) (auto simp: finsum_def add_pow_def)
   145 
   146 context abelian_monoid begin
   147 
   148 lemmas a_comm = add.m_comm
   149 lemmas a_lcomm = add.m_lcomm
   150 lemmas a_ac = a_assoc a_comm a_lcomm
   151 
   152 lemmas finsum_empty = add.finprod_empty
   153 lemmas finsum_insert = add.finprod_insert
   154 lemmas finsum_zero = add.finprod_one
   155 lemmas finsum_closed = add.finprod_closed
   156 lemmas finsum_Un_Int = add.finprod_Un_Int
   157 lemmas finsum_Un_disjoint = add.finprod_Un_disjoint
   158 lemmas finsum_addf = add.finprod_multf
   159 lemmas finsum_cong' = add.finprod_cong'
   160 lemmas finsum_0 = add.finprod_0
   161 lemmas finsum_Suc = add.finprod_Suc
   162 lemmas finsum_Suc2 = add.finprod_Suc2
   163 lemmas finsum_add = add.finprod_mult
   164 lemmas finsum_infinite = add.finprod_infinite
   165 
   166 lemmas finsum_cong = add.finprod_cong
   167 text \<open>Usually, if this rule causes a failed congruence proof error,
   168    the reason is that the premise \<open>g \<in> B \<rightarrow> carrier G\<close> cannot be shown.
   169    Adding @{thm [source] Pi_def} to the simpset is often useful.\<close>
   170 
   171 lemmas finsum_reindex = add.finprod_reindex
   172 
   173 (* The following would be wrong.  Needed is the equivalent of [^] for addition,
   174   or indeed the canonical embedding from Nat into the monoid.
   175 
   176 lemma finsum_const:
   177   assumes fin [simp]: "finite A"
   178       and a [simp]: "a : carrier G"
   179     shows "finsum G (%x. a) A = a [^] card A"
   180   using fin apply induct
   181   apply force
   182   apply (subst finsum_insert)
   183   apply auto
   184   apply (force simp add: Pi_def)
   185   apply (subst m_comm)
   186   apply auto
   187 done
   188 *)
   189 
   190 lemmas finsum_singleton = add.finprod_singleton
   191 
   192 end
   193 
   194 sublocale abelian_group <
   195         add: group "(add_monoid G)"
   196   rewrites "carrier (add_monoid G) = carrier G"
   197        and "mult    (add_monoid G) = add G"
   198        and "one     (add_monoid G) = zero G"
   199        and "m_inv   (add_monoid G) = a_inv G"
   200        and "pow     (add_monoid G) = (\<lambda>a k. add_pow G k a)"
   201   by (rule a_group) (auto simp: m_inv_def a_inv_def add_pow_def)
   202 
   203 context abelian_group
   204 begin
   205 
   206 lemmas a_inv_closed = add.inv_closed
   207 
   208 lemma minus_closed [intro, simp]:
   209   "[| x \<in> carrier G; y \<in> carrier G |] ==> x \<ominus> y \<in> carrier G"
   210   by (simp add: a_minus_def)
   211 
   212 lemmas l_neg = add.l_inv [simp del]
   213 lemmas r_neg = add.r_inv [simp del]
   214 lemmas minus_minus = add.inv_inv
   215 lemmas a_inv_inj = add.inv_inj
   216 lemmas minus_equality = add.inv_equality
   217 
   218 end
   219 
   220 sublocale abelian_group <
   221    add: comm_group "(add_monoid G)"
   222   rewrites "carrier (add_monoid G) = carrier G"
   223        and "mult    (add_monoid G) = add G"
   224        and "one     (add_monoid G) = zero G"
   225        and "m_inv   (add_monoid G) = a_inv G"
   226        and "finprod (add_monoid G) = finsum G"
   227        and "pow     (add_monoid G) = (\<lambda>a k. add_pow G k a)"
   228   by (rule a_comm_group) (auto simp: m_inv_def a_inv_def finsum_def add_pow_def)
   229 
   230 lemmas (in abelian_group) minus_add = add.inv_mult
   231  
   232 text \<open>Derive an \<open>abelian_group\<close> from a \<open>comm_group\<close>\<close>
   233 
   234 lemma comm_group_abelian_groupI:
   235   fixes G (structure)
   236   assumes cg: "comm_group (add_monoid G)"
   237   shows "abelian_group G"
   238 proof -
   239   interpret comm_group "(add_monoid G)"
   240     by (rule cg)
   241   show "abelian_group G" ..
   242 qed
   243 
   244 
   245 subsection \<open>Rings: Basic Definitions\<close>
   246 
   247 locale semiring = abelian_monoid (* for add *) R + monoid (* for mult *) R for R (structure) +
   248   assumes l_distr: "\<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   249       and r_distr: "\<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
   250       and l_null[simp]: "x \<in> carrier R \<Longrightarrow> \<zero> \<otimes> x = \<zero>"
   251       and r_null[simp]: "x \<in> carrier R \<Longrightarrow> x \<otimes> \<zero> = \<zero>"
   252 
   253 locale ring = abelian_group (* for add *) R + monoid (* for mult *) R for R (structure) +
   254   assumes "\<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   255       and "\<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
   256 
   257 locale cring = ring + comm_monoid (* for mult *) R
   258 
   259 locale "domain" = cring +
   260   assumes one_not_zero [simp]: "\<one> \<noteq> \<zero>"
   261       and integral: "\<lbrakk> a \<otimes> b = \<zero>; a \<in> carrier R; b \<in> carrier R \<rbrakk> \<Longrightarrow> a = \<zero> \<or> b = \<zero>"
   262 
   263 locale field = "domain" +
   264   assumes field_Units: "Units R = carrier R - {\<zero>}"
   265 
   266 
   267 subsection \<open>Rings\<close>
   268 
   269 lemma ringI:
   270   fixes R (structure)
   271   assumes "abelian_group R"
   272       and "monoid R"
   273       and "\<And>x y z. \<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   274       and "\<And>x y z. \<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
   275   shows "ring R"
   276   by (auto intro: ring.intro
   277     abelian_group.axioms ring_axioms.intro assms)
   278 
   279 lemma ringE:
   280   fixes R (structure)
   281   assumes "ring R"
   282   shows "abelian_group R"
   283     and "monoid R"
   284     and "\<And>x y z. \<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   285     and "\<And>x y z. \<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
   286   using assms unfolding ring_def ring_axioms_def by auto
   287 
   288 context ring begin
   289 
   290 lemma is_abelian_group: "abelian_group R" ..
   291 
   292 lemma is_monoid: "monoid R"
   293   by (auto intro!: monoidI m_assoc)
   294 
   295 lemma is_ring: "ring R"
   296   by (rule ring_axioms)
   297 
   298 end
   299 thm monoid_record_simps
   300 lemmas ring_record_simps = monoid_record_simps ring.simps
   301 
   302 lemma cringI:
   303   fixes R (structure)
   304   assumes abelian_group: "abelian_group R"
   305     and comm_monoid: "comm_monoid R"
   306     and l_distr: "\<And>x y z. \<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow>
   307                             (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   308   shows "cring R"
   309 proof (intro cring.intro ring.intro)
   310   show "ring_axioms R"
   311     \<comment> \<open>Right-distributivity follows from left-distributivity and
   312           commutativity.\<close>
   313   proof (rule ring_axioms.intro)
   314     fix x y z
   315     assume R: "x \<in> carrier R" "y \<in> carrier R" "z \<in> carrier R"
   316     note [simp] = comm_monoid.axioms [OF comm_monoid]
   317       abelian_group.axioms [OF abelian_group]
   318       abelian_monoid.a_closed
   319         
   320     from R have "z \<otimes> (x \<oplus> y) = (x \<oplus> y) \<otimes> z"
   321       by (simp add: comm_monoid.m_comm [OF comm_monoid.intro])
   322     also from R have "... = x \<otimes> z \<oplus> y \<otimes> z" by (simp add: l_distr)
   323     also from R have "... = z \<otimes> x \<oplus> z \<otimes> y"
   324       by (simp add: comm_monoid.m_comm [OF comm_monoid.intro])
   325     finally show "z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y" .
   326   qed (rule l_distr)
   327 qed (auto intro: cring.intro
   328   abelian_group.axioms comm_monoid.axioms ring_axioms.intro assms)
   329 
   330 lemma cringE:
   331   fixes R (structure)
   332   assumes "cring R"
   333   shows "comm_monoid R"
   334     and "\<And>x y z. \<lbrakk> x \<in> carrier R; y \<in> carrier R; z \<in> carrier R \<rbrakk> \<Longrightarrow> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   335   using assms cring_def apply auto by (simp add: assms cring.axioms(1) ringE(3))
   336 
   337 (*
   338 lemma (in cring) is_comm_monoid:
   339   "comm_monoid R"
   340   by (auto intro!: comm_monoidI m_assoc m_comm)
   341 *)
   342 lemma (in cring) is_cring:
   343   "cring R" by (rule cring_axioms)
   344 
   345 lemma (in ring) minus_zero [simp]: "\<ominus> \<zero> = \<zero>"
   346   by (simp add: a_inv_def)
   347 
   348 subsubsection \<open>Normaliser for Rings\<close>
   349 
   350 lemma (in abelian_group) r_neg1:
   351   "\<lbrakk> x \<in> carrier G; y \<in> carrier G \<rbrakk> \<Longrightarrow> (\<ominus> x) \<oplus> (x \<oplus> y) = y"
   352 proof -
   353   assume G: "x \<in> carrier G" "y \<in> carrier G"
   354   then have "(\<ominus> x \<oplus> x) \<oplus> y = y" 
   355     by (simp only: l_neg l_zero)
   356   with G show ?thesis by (simp add: a_ac)
   357 qed
   358 
   359 lemma (in abelian_group) r_neg2:
   360   "\<lbrakk> x \<in> carrier G; y \<in> carrier G \<rbrakk> \<Longrightarrow> x \<oplus> ((\<ominus> x) \<oplus> y) = y"
   361 proof -
   362   assume G: "x \<in> carrier G" "y \<in> carrier G"
   363   then have "(x \<oplus> \<ominus> x) \<oplus> y = y"
   364     by (simp only: r_neg l_zero)
   365   with G show ?thesis
   366     by (simp add: a_ac)
   367 qed
   368 
   369 context ring begin
   370 
   371 text \<open>
   372   The following proofs are from Jacobson, Basic Algebra I, pp.~88--89.
   373 \<close>
   374 
   375 sublocale semiring
   376 proof -
   377   note [simp] = ring_axioms[unfolded ring_def ring_axioms_def]
   378   show "semiring R"
   379   proof (unfold_locales)
   380     fix x
   381     assume R: "x \<in> carrier R"
   382     then have "\<zero> \<otimes> x \<oplus> \<zero> \<otimes> x = (\<zero> \<oplus> \<zero>) \<otimes> x"
   383       by (simp del: l_zero r_zero)
   384     also from R have "... = \<zero> \<otimes> x \<oplus> \<zero>" by simp
   385     finally have "\<zero> \<otimes> x \<oplus> \<zero> \<otimes> x = \<zero> \<otimes> x \<oplus> \<zero>" .
   386     with R show "\<zero> \<otimes> x = \<zero>" by (simp del: r_zero)
   387     from R have "x \<otimes> \<zero> \<oplus> x \<otimes> \<zero> = x \<otimes> (\<zero> \<oplus> \<zero>)"
   388       by (simp del: l_zero r_zero)
   389     also from R have "... = x \<otimes> \<zero> \<oplus> \<zero>" by simp
   390     finally have "x \<otimes> \<zero> \<oplus> x \<otimes> \<zero> = x \<otimes> \<zero> \<oplus> \<zero>" .
   391     with R show "x \<otimes> \<zero> = \<zero>" by (simp del: r_zero)
   392   qed auto
   393 qed
   394 
   395 lemma l_minus:
   396   "\<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> (\<ominus> x) \<otimes> y = \<ominus> (x \<otimes> y)"
   397 proof -
   398   assume R: "x \<in> carrier R" "y \<in> carrier R"
   399   then have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y = (\<ominus> x \<oplus> x) \<otimes> y" by (simp add: l_distr)
   400   also from R have "... = \<zero>" by (simp add: l_neg)
   401   finally have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y = \<zero>" .
   402   with R have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y \<oplus> \<ominus> (x \<otimes> y) = \<zero> \<oplus> \<ominus> (x \<otimes> y)" by simp
   403   with R show ?thesis by (simp add: a_assoc r_neg)
   404 qed
   405 
   406 lemma r_minus:
   407   "\<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> x \<otimes> (\<ominus> y) = \<ominus> (x \<otimes> y)"
   408 proof -
   409   assume R: "x \<in> carrier R" "y \<in> carrier R"
   410   then have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y = x \<otimes> (\<ominus> y \<oplus> y)" by (simp add: r_distr)
   411   also from R have "... = \<zero>" by (simp add: l_neg)
   412   finally have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y = \<zero>" .
   413   with R have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y \<oplus> \<ominus> (x \<otimes> y) = \<zero> \<oplus> \<ominus> (x \<otimes> y)" by simp
   414   with R show ?thesis by (simp add: a_assoc r_neg )
   415 qed
   416 
   417 end
   418 
   419 lemma (in abelian_group) minus_eq: "x \<ominus> y = x \<oplus> (\<ominus> y)"
   420   by (rule a_minus_def)
   421 
   422 text \<open>Setup algebra method:
   423   compute distributive normal form in locale contexts\<close>
   424 
   425 
   426 ML_file "ringsimp.ML"
   427 
   428 attribute_setup algebra = \<open>
   429   Scan.lift ((Args.add >> K true || Args.del >> K false) --| Args.colon || Scan.succeed true)
   430     -- Scan.lift Args.name -- Scan.repeat Args.term
   431     >> (fn ((b, n), ts) => if b then Ringsimp.add_struct (n, ts) else Ringsimp.del_struct (n, ts))
   432 \<close> "theorems controlling algebra method"
   433 
   434 method_setup algebra = \<open>
   435   Scan.succeed (SIMPLE_METHOD' o Ringsimp.algebra_tac)
   436 \<close> "normalisation of algebraic structure"
   437 
   438 lemmas (in semiring) semiring_simprules
   439   [algebra ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   440   a_closed zero_closed  m_closed one_closed
   441   a_assoc l_zero  a_comm m_assoc l_one l_distr r_zero
   442   a_lcomm r_distr l_null r_null 
   443 
   444 lemmas (in ring) ring_simprules
   445   [algebra ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   446   a_closed zero_closed a_inv_closed minus_closed m_closed one_closed
   447   a_assoc l_zero l_neg a_comm m_assoc l_one l_distr minus_eq
   448   r_zero r_neg r_neg2 r_neg1 minus_add minus_minus minus_zero
   449   a_lcomm r_distr l_null r_null l_minus r_minus
   450 
   451 lemmas (in cring)
   452   [algebra del: ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   453   _
   454 
   455 lemmas (in cring) cring_simprules
   456   [algebra add: cring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   457   a_closed zero_closed a_inv_closed minus_closed m_closed one_closed
   458   a_assoc l_zero l_neg a_comm m_assoc l_one l_distr m_comm minus_eq
   459   r_zero r_neg r_neg2 r_neg1 minus_add minus_minus minus_zero
   460   a_lcomm m_lcomm r_distr l_null r_null l_minus r_minus
   461 
   462 lemma (in semiring) nat_pow_zero:
   463   "(n::nat) \<noteq> 0 \<Longrightarrow> \<zero> [^] n = \<zero>"
   464   by (induct n) simp_all
   465 
   466 context semiring begin
   467 
   468 lemma one_zeroD:
   469   assumes onezero: "\<one> = \<zero>"
   470   shows "carrier R = {\<zero>}"
   471 proof (rule, rule)
   472   fix x
   473   assume xcarr: "x \<in> carrier R"
   474   from xcarr have "x = x \<otimes> \<one>" by simp
   475   with onezero have "x = x \<otimes> \<zero>" by simp
   476   with xcarr have "x = \<zero>" by simp
   477   then show "x \<in> {\<zero>}" by fast
   478 qed fast
   479 
   480 lemma one_zeroI:
   481   assumes carrzero: "carrier R = {\<zero>}"
   482   shows "\<one> = \<zero>"
   483 proof -
   484   from one_closed and carrzero
   485       show "\<one> = \<zero>" by simp
   486 qed
   487 
   488 lemma carrier_one_zero: "(carrier R = {\<zero>}) = (\<one> = \<zero>)"
   489   apply rule
   490    apply (erule one_zeroI)
   491   apply (erule one_zeroD)
   492   done
   493 
   494 lemma carrier_one_not_zero: "(carrier R \<noteq> {\<zero>}) = (\<one> \<noteq> \<zero>)"
   495   by (simp add: carrier_one_zero)
   496 
   497 end
   498 
   499 text \<open>Two examples for use of method algebra\<close>
   500 
   501 lemma
   502   fixes R (structure) and S (structure)
   503   assumes "ring R" "cring S"
   504   assumes RS: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier S" "d \<in> carrier S"
   505   shows "a \<oplus> (\<ominus> (a \<oplus> (\<ominus> b))) = b \<and> c \<otimes>\<^bsub>S\<^esub> d = d \<otimes>\<^bsub>S\<^esub> c"
   506 proof -
   507   interpret ring R by fact
   508   interpret cring S by fact
   509   from RS show ?thesis by algebra
   510 qed
   511 
   512 lemma
   513   fixes R (structure)
   514   assumes "ring R"
   515   assumes R: "a \<in> carrier R" "b \<in> carrier R"
   516   shows "a \<ominus> (a \<ominus> b) = b"
   517 proof -
   518   interpret ring R by fact
   519   from R show ?thesis by algebra
   520 qed
   521 
   522 
   523 subsubsection \<open>Sums over Finite Sets\<close>
   524 
   525 lemma (in semiring) finsum_ldistr:
   526   "\<lbrakk> finite A; a \<in> carrier R; f: A \<rightarrow> carrier R \<rbrakk> \<Longrightarrow>
   527     (\<Oplus> i \<in> A. (f i)) \<otimes> a = (\<Oplus> i \<in> A. ((f i) \<otimes> a))"
   528 proof (induct set: finite)
   529   case empty then show ?case by simp
   530 next
   531   case (insert x F) then show ?case by (simp add: Pi_def l_distr)
   532 qed
   533 
   534 lemma (in semiring) finsum_rdistr:
   535   "\<lbrakk> finite A; a \<in> carrier R; f: A \<rightarrow> carrier R \<rbrakk> \<Longrightarrow>
   536    a \<otimes> (\<Oplus> i \<in> A. (f i)) = (\<Oplus> i \<in> A. (a \<otimes> (f i)))"
   537 proof (induct set: finite)
   538   case empty then show ?case by simp
   539 next
   540   case (insert x F) then show ?case by (simp add: Pi_def r_distr)
   541 qed
   542 
   543 (* ************************************************************************** *)
   544 (* Contributed by Paulo E. de Vilhena.                                        *)
   545 
   546 text \<open>A quick detour\<close>
   547 
   548 lemma add_pow_int_ge: "(k :: int) \<ge> 0 \<Longrightarrow> [ k ] \<cdot>\<^bsub>R\<^esub> a = [ nat k ] \<cdot>\<^bsub>R\<^esub> a"
   549   by (simp add: add_pow_def int_pow_def nat_pow_def)
   550 
   551 lemma add_pow_int_lt: "(k :: int) < 0 \<Longrightarrow> [ k ] \<cdot>\<^bsub>R\<^esub> a = \<ominus>\<^bsub>R\<^esub> ([ nat (- k) ] \<cdot>\<^bsub>R\<^esub> a)"
   552   by (simp add: int_pow_def nat_pow_def a_inv_def add_pow_def) 
   553 
   554 corollary (in semiring) add_pow_ldistr:
   555   assumes "a \<in> carrier R" "b \<in> carrier R"
   556   shows "([(k :: nat)] \<cdot> a) \<otimes> b = [k] \<cdot> (a \<otimes> b)"
   557 proof -
   558   have "([k] \<cdot> a) \<otimes> b = (\<Oplus> i \<in> {..< k}. a) \<otimes> b"
   559     using add.finprod_const[OF assms(1), of "{..<k}"] by simp
   560   also have " ... = (\<Oplus> i \<in> {..< k}. (a \<otimes> b))"
   561     using finsum_ldistr[of "{..<k}" b "\<lambda>x. a"] assms by simp
   562   also have " ... = [k] \<cdot> (a \<otimes> b)"
   563     using add.finprod_const[of "a \<otimes> b" "{..<k}"] assms by simp
   564   finally show ?thesis .
   565 qed
   566 
   567 corollary (in semiring) add_pow_rdistr:
   568   assumes "a \<in> carrier R" "b \<in> carrier R"
   569   shows "a \<otimes> ([(k :: nat)] \<cdot> b) = [k] \<cdot> (a \<otimes> b)"
   570 proof -
   571   have "a \<otimes> ([k] \<cdot> b) = a \<otimes> (\<Oplus> i \<in> {..< k}. b)"
   572     using add.finprod_const[OF assms(2), of "{..<k}"] by simp
   573   also have " ... = (\<Oplus> i \<in> {..< k}. (a \<otimes> b))"
   574     using finsum_rdistr[of "{..<k}" a "\<lambda>x. b"] assms by simp
   575   also have " ... = [k] \<cdot> (a \<otimes> b)"
   576     using add.finprod_const[of "a \<otimes> b" "{..<k}"] assms by simp
   577   finally show ?thesis .
   578 qed 
   579 
   580 (* For integers, we need the uniqueness of the additive inverse *)
   581 lemma (in ring) add_pow_ldistr_int:
   582   assumes "a \<in> carrier R" "b \<in> carrier R"
   583   shows "([(k :: int)] \<cdot> a) \<otimes> b = [k] \<cdot> (a \<otimes> b)"
   584 proof (cases "k \<ge> 0")
   585   case True thus ?thesis
   586     using add_pow_int_ge[of k R] add_pow_ldistr[OF assms] by auto
   587 next
   588   case False thus ?thesis
   589     using add_pow_int_lt[of k R a] add_pow_int_lt[of k R "a \<otimes> b"]
   590           add_pow_ldistr[OF assms, of "nat (- k)"] assms l_minus by auto 
   591 qed
   592 
   593 lemma (in ring) add_pow_rdistr_int:
   594   assumes "a \<in> carrier R" "b \<in> carrier R"
   595   shows "a \<otimes> ([(k :: int)] \<cdot> b) = [k] \<cdot> (a \<otimes> b)"
   596 proof (cases "k \<ge> 0")
   597   case True thus ?thesis
   598     using add_pow_int_ge[of k R] add_pow_rdistr[OF assms] by auto
   599 next
   600   case False thus ?thesis
   601     using add_pow_int_lt[of k R b] add_pow_int_lt[of k R "a \<otimes> b"]
   602           add_pow_rdistr[OF assms, of "nat (- k)"] assms r_minus by auto 
   603 qed
   604 
   605 
   606 subsection \<open>Integral Domains\<close>
   607 
   608 context "domain" begin
   609 
   610 lemma zero_not_one [simp]: "\<zero> \<noteq> \<one>"
   611   by (rule not_sym) simp
   612 
   613 lemma integral_iff: (* not by default a simp rule! *)
   614   "\<lbrakk> a \<in> carrier R; b \<in> carrier R \<rbrakk> \<Longrightarrow> (a \<otimes> b = \<zero>) = (a = \<zero> \<or> b = \<zero>)"
   615 proof
   616   assume "a \<in> carrier R" "b \<in> carrier R" "a \<otimes> b = \<zero>"
   617   then show "a = \<zero> \<or> b = \<zero>" by (simp add: integral)
   618 next
   619   assume "a \<in> carrier R" "b \<in> carrier R" "a = \<zero> \<or> b = \<zero>"
   620   then show "a \<otimes> b = \<zero>" by auto
   621 qed
   622 
   623 lemma m_lcancel:
   624   assumes prem: "a \<noteq> \<zero>"
   625     and R: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier R"
   626   shows "(a \<otimes> b = a \<otimes> c) = (b = c)"
   627 proof
   628   assume eq: "a \<otimes> b = a \<otimes> c"
   629   with R have "a \<otimes> (b \<ominus> c) = \<zero>" by algebra
   630   with R have "a = \<zero> \<or> (b \<ominus> c) = \<zero>" by (simp add: integral_iff)
   631   with prem and R have "b \<ominus> c = \<zero>" by auto 
   632   with R have "b = b \<ominus> (b \<ominus> c)" by algebra
   633   also from R have "b \<ominus> (b \<ominus> c) = c" by algebra
   634   finally show "b = c" .
   635 next
   636   assume "b = c" then show "a \<otimes> b = a \<otimes> c" by simp
   637 qed
   638 
   639 lemma m_rcancel:
   640   assumes prem: "a \<noteq> \<zero>"
   641     and R: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier R"
   642   shows conc: "(b \<otimes> a = c \<otimes> a) = (b = c)"
   643 proof -
   644   from prem and R have "(a \<otimes> b = a \<otimes> c) = (b = c)" by (rule m_lcancel)
   645   with R show ?thesis by algebra
   646 qed
   647 
   648 end
   649 
   650 
   651 subsection \<open>Fields\<close>
   652 
   653 text \<open>Field would not need to be derived from domain, the properties
   654   for domain follow from the assumptions of field\<close>
   655 
   656 lemma (in cring) cring_fieldI:
   657   assumes field_Units: "Units R = carrier R - {\<zero>}"
   658   shows "field R"
   659 proof
   660   from field_Units have "\<zero> \<notin> Units R" by fast
   661   moreover have "\<one> \<in> Units R" by fast
   662   ultimately show "\<one> \<noteq> \<zero>" by force
   663 next
   664   fix a b
   665   assume acarr: "a \<in> carrier R"
   666     and bcarr: "b \<in> carrier R"
   667     and ab: "a \<otimes> b = \<zero>"
   668   show "a = \<zero> \<or> b = \<zero>"
   669   proof (cases "a = \<zero>", simp)
   670     assume "a \<noteq> \<zero>"
   671     with field_Units and acarr have aUnit: "a \<in> Units R" by fast
   672     from bcarr have "b = \<one> \<otimes> b" by algebra
   673     also from aUnit acarr have "... = (inv a \<otimes> a) \<otimes> b" by simp
   674     also from acarr bcarr aUnit[THEN Units_inv_closed]
   675     have "... = (inv a) \<otimes> (a \<otimes> b)" by algebra
   676     also from ab and acarr bcarr aUnit have "... = (inv a) \<otimes> \<zero>" by simp
   677     also from aUnit[THEN Units_inv_closed] have "... = \<zero>" by algebra
   678     finally have "b = \<zero>" .
   679     then show "a = \<zero> \<or> b = \<zero>" by simp
   680   qed
   681 qed (rule field_Units)
   682 
   683 text \<open>Another variant to show that something is a field\<close>
   684 lemma (in cring) cring_fieldI2:
   685   assumes notzero: "\<zero> \<noteq> \<one>"
   686   and invex: "\<And>a. \<lbrakk>a \<in> carrier R; a \<noteq> \<zero>\<rbrakk> \<Longrightarrow> \<exists>b\<in>carrier R. a \<otimes> b = \<one>"
   687   shows "field R"
   688   apply (rule cring_fieldI, simp add: Units_def)
   689   apply (rule, clarsimp)
   690   apply (simp add: notzero)
   691 proof (clarsimp)
   692   fix x
   693   assume xcarr: "x \<in> carrier R"
   694     and "x \<noteq> \<zero>"
   695   then have "\<exists>y\<in>carrier R. x \<otimes> y = \<one>" by (rule invex)
   696   then obtain y where ycarr: "y \<in> carrier R" and xy: "x \<otimes> y = \<one>" by fast
   697   from xy xcarr ycarr have "y \<otimes> x = \<one>" by (simp add: m_comm)
   698   with ycarr and xy show "\<exists>y\<in>carrier R. y \<otimes> x = \<one> \<and> x \<otimes> y = \<one>" by fast
   699 qed
   700 
   701 
   702 subsection \<open>Morphisms\<close>
   703 
   704 definition
   705   ring_hom :: "[('a, 'm) ring_scheme, ('b, 'n) ring_scheme] => ('a => 'b) set"
   706   where "ring_hom R S =
   707     {h. h \<in> carrier R \<rightarrow> carrier S \<and>
   708       (\<forall>x y. x \<in> carrier R \<and> y \<in> carrier R \<longrightarrow>
   709         h (x \<otimes>\<^bsub>R\<^esub> y) = h x \<otimes>\<^bsub>S\<^esub> h y \<and> h (x \<oplus>\<^bsub>R\<^esub> y) = h x \<oplus>\<^bsub>S\<^esub> h y) \<and>
   710       h \<one>\<^bsub>R\<^esub> = \<one>\<^bsub>S\<^esub>}"
   711 
   712 lemma ring_hom_memI:
   713   fixes R (structure) and S (structure)
   714   assumes "\<And>x. x \<in> carrier R \<Longrightarrow> h x \<in> carrier S"
   715       and "\<And>x y. \<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> h (x \<otimes> y) = h x \<otimes>\<^bsub>S\<^esub> h y"
   716       and "\<And>x y. \<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> h (x \<oplus> y) = h x \<oplus>\<^bsub>S\<^esub> h y"
   717       and "h \<one> = \<one>\<^bsub>S\<^esub>"
   718   shows "h \<in> ring_hom R S"
   719   by (auto simp add: ring_hom_def assms Pi_def)
   720 
   721 lemma ring_hom_memE:
   722   fixes R (structure) and S (structure)
   723   assumes "h \<in> ring_hom R S"
   724   shows "\<And>x. x \<in> carrier R \<Longrightarrow> h x \<in> carrier S"
   725     and "\<And>x y. \<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> h (x \<otimes> y) = h x \<otimes>\<^bsub>S\<^esub> h y"
   726     and "\<And>x y. \<lbrakk> x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> h (x \<oplus> y) = h x \<oplus>\<^bsub>S\<^esub> h y"
   727     and "h \<one> = \<one>\<^bsub>S\<^esub>"
   728   using assms unfolding ring_hom_def by auto
   729 
   730 lemma ring_hom_closed:
   731   "\<lbrakk> h \<in> ring_hom R S; x \<in> carrier R \<rbrakk> \<Longrightarrow> h x \<in> carrier S"
   732   by (auto simp add: ring_hom_def funcset_mem)
   733 
   734 lemma ring_hom_mult:
   735   fixes R (structure) and S (structure)
   736   shows "\<lbrakk> h \<in> ring_hom R S; x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> h (x \<otimes> y) = h x \<otimes>\<^bsub>S\<^esub> h y"
   737     by (simp add: ring_hom_def)
   738 
   739 lemma ring_hom_add:
   740   fixes R (structure) and S (structure)
   741   shows "\<lbrakk> h \<in> ring_hom R S; x \<in> carrier R; y \<in> carrier R \<rbrakk> \<Longrightarrow> h (x \<oplus> y) = h x \<oplus>\<^bsub>S\<^esub> h y"
   742     by (simp add: ring_hom_def)
   743 
   744 lemma ring_hom_one:
   745   fixes R (structure) and S (structure)
   746   shows "h \<in> ring_hom R S \<Longrightarrow> h \<one> = \<one>\<^bsub>S\<^esub>"
   747   by (simp add: ring_hom_def)
   748 
   749 lemma ring_hom_zero:
   750   fixes R (structure) and S (structure)
   751   assumes "h \<in> ring_hom R S" "ring R" "ring S"
   752   shows "h \<zero> = \<zero>\<^bsub>S\<^esub>"
   753 proof -
   754   have "h \<zero> = h \<zero> \<oplus>\<^bsub>S\<^esub> h \<zero>"
   755     using ring_hom_add[OF assms(1), of \<zero> \<zero>] assms(2)
   756     by (simp add: ring.ring_simprules(2) ring.ring_simprules(15))
   757   thus ?thesis
   758     by (metis abelian_group.l_neg assms ring.is_abelian_group ring.ring_simprules(18) ring.ring_simprules(2) ring_hom_closed)
   759 qed
   760 
   761 locale ring_hom_cring =
   762   R?: cring R + S?: cring S for R (structure) and S (structure) + fixes h
   763   assumes homh [simp, intro]: "h \<in> ring_hom R S"
   764   notes hom_closed [simp, intro] = ring_hom_closed [OF homh]
   765     and hom_mult [simp] = ring_hom_mult [OF homh]
   766     and hom_add [simp] = ring_hom_add [OF homh]
   767     and hom_one [simp] = ring_hom_one [OF homh]
   768 
   769 lemma (in ring_hom_cring) hom_zero [simp]: "h \<zero> = \<zero>\<^bsub>S\<^esub>"
   770 proof -
   771   have "h \<zero> \<oplus>\<^bsub>S\<^esub> h \<zero> = h \<zero> \<oplus>\<^bsub>S\<^esub> \<zero>\<^bsub>S\<^esub>"
   772     by (simp add: hom_add [symmetric] del: hom_add)
   773   then show ?thesis by (simp del: S.r_zero)
   774 qed
   775 
   776 lemma (in ring_hom_cring) hom_a_inv [simp]:
   777   "x \<in> carrier R \<Longrightarrow> h (\<ominus> x) = \<ominus>\<^bsub>S\<^esub> h x"
   778 proof -
   779   assume R: "x \<in> carrier R"
   780   then have "h x \<oplus>\<^bsub>S\<^esub> h (\<ominus> x) = h x \<oplus>\<^bsub>S\<^esub> (\<ominus>\<^bsub>S\<^esub> h x)"
   781     by (simp add: hom_add [symmetric] R.r_neg S.r_neg del: hom_add)
   782   with R show ?thesis by simp
   783 qed
   784 
   785 lemma (in ring_hom_cring) hom_finsum [simp]:
   786   assumes "f: A \<rightarrow> carrier R"
   787   shows "h (\<Oplus> i \<in> A. f i) = (\<Oplus>\<^bsub>S\<^esub> i \<in> A. (h o f) i)"
   788   using assms by (induct A rule: infinite_finite_induct, auto simp: Pi_def)
   789 
   790 lemma (in ring_hom_cring) hom_finprod:
   791   assumes "f: A \<rightarrow> carrier R"
   792   shows "h (\<Otimes> i \<in> A. f i) = (\<Otimes>\<^bsub>S\<^esub> i \<in> A. (h o f) i)"
   793   using assms by (induct A rule: infinite_finite_induct, auto simp: Pi_def)
   794 
   795 declare ring_hom_cring.hom_finprod [simp]
   796 
   797 lemma id_ring_hom [simp]: "id \<in> ring_hom R R"
   798   by (auto intro!: ring_hom_memI)
   799 
   800 (* Next lemma contributed by Paulo Emílio de Vilhena. *)
   801 
   802 lemma ring_hom_trans:
   803   "\<lbrakk> f \<in> ring_hom R S; g \<in> ring_hom S T \<rbrakk> \<Longrightarrow> g \<circ> f \<in> ring_hom R T"
   804   by (rule ring_hom_memI) (auto simp add: ring_hom_closed ring_hom_mult ring_hom_add ring_hom_one)
   805 
   806 subsection\<open>Jeremy Avigad's @{text"More_Finite_Product"} material\<close>
   807 
   808 (* need better simplification rules for rings *)
   809 (* the next one holds more generally for abelian groups *)
   810 
   811 lemma (in cring) sum_zero_eq_neg: "x \<in> carrier R \<Longrightarrow> y \<in> carrier R \<Longrightarrow> x \<oplus> y = \<zero> \<Longrightarrow> x = \<ominus> y"
   812   by (metis minus_equality)
   813 
   814 lemma (in domain) square_eq_one:
   815   fixes x
   816   assumes [simp]: "x \<in> carrier R"
   817     and "x \<otimes> x = \<one>"
   818   shows "x = \<one> \<or> x = \<ominus>\<one>"
   819 proof -
   820   have "(x \<oplus> \<one>) \<otimes> (x \<oplus> \<ominus> \<one>) = x \<otimes> x \<oplus> \<ominus> \<one>"
   821     by (simp add: ring_simprules)
   822   also from \<open>x \<otimes> x = \<one>\<close> have "\<dots> = \<zero>"
   823     by (simp add: ring_simprules)
   824   finally have "(x \<oplus> \<one>) \<otimes> (x \<oplus> \<ominus> \<one>) = \<zero>" .
   825   then have "(x \<oplus> \<one>) = \<zero> \<or> (x \<oplus> \<ominus> \<one>) = \<zero>"
   826     by (intro integral) auto
   827   then show ?thesis
   828     by (metis add.inv_closed add.inv_solve_right assms(1) l_zero one_closed zero_closed)
   829 qed
   830 
   831 lemma (in domain) inv_eq_self: "x \<in> Units R \<Longrightarrow> x = inv x \<Longrightarrow> x = \<one> \<or> x = \<ominus>\<one>"
   832   by (metis Units_closed Units_l_inv square_eq_one)
   833 
   834 
   835 text \<open>
   836   The following translates theorems about groups to the facts about
   837   the units of a ring. (The list should be expanded as more things are
   838   needed.)
   839 \<close>
   840 
   841 lemma (in ring) finite_ring_finite_units [intro]: "finite (carrier R) \<Longrightarrow> finite (Units R)"
   842   by (rule finite_subset) auto
   843 
   844 lemma (in monoid) units_of_pow:
   845   fixes n :: nat
   846   shows "x \<in> Units G \<Longrightarrow> x [^]\<^bsub>units_of G\<^esub> n = x [^]\<^bsub>G\<^esub> n"
   847   apply (induct n)
   848   apply (auto simp add: units_group group.is_monoid
   849     monoid.nat_pow_0 monoid.nat_pow_Suc units_of_one units_of_mult)
   850   done
   851 
   852 lemma (in cring) units_power_order_eq_one:
   853   "finite (Units R) \<Longrightarrow> a \<in> Units R \<Longrightarrow> a [^] card(Units R) = \<one>"
   854   by (metis comm_group.power_order_eq_one units_comm_group units_of_carrier units_of_one units_of_pow)
   855 
   856 subsection\<open>Jeremy Avigad's @{text"More_Ring"} material\<close>
   857 
   858 lemma (in cring) field_intro2: "\<zero>\<^bsub>R\<^esub> \<noteq> \<one>\<^bsub>R\<^esub> \<Longrightarrow> \<forall>x \<in> carrier R - {\<zero>\<^bsub>R\<^esub>}. x \<in> Units R \<Longrightarrow> field R"
   859   apply (unfold_locales)
   860     apply (use cring_axioms in auto)
   861    apply (rule trans)
   862     apply (subgoal_tac "a = (a \<otimes> b) \<otimes> inv b")
   863      apply assumption
   864     apply (subst m_assoc)
   865        apply auto
   866   apply (unfold Units_def)
   867   apply auto
   868   done
   869 
   870 lemma (in monoid) inv_char:
   871   "x \<in> carrier G \<Longrightarrow> y \<in> carrier G \<Longrightarrow> x \<otimes> y = \<one> \<Longrightarrow> y \<otimes> x = \<one> \<Longrightarrow> inv x = y"
   872   apply (subgoal_tac "x \<in> Units G")
   873    apply (subgoal_tac "y = inv x \<otimes> \<one>")
   874     apply simp
   875    apply (erule subst)
   876    apply (subst m_assoc [symmetric])
   877       apply auto
   878   apply (unfold Units_def)
   879   apply auto
   880   done
   881 
   882 lemma (in comm_monoid) comm_inv_char: "x \<in> carrier G \<Longrightarrow> y \<in> carrier G \<Longrightarrow> x \<otimes> y = \<one> \<Longrightarrow> inv x = y"
   883   by (simp add: inv_char m_comm)
   884 
   885 lemma (in ring) inv_neg_one [simp]: "inv (\<ominus> \<one>) = \<ominus> \<one>"
   886   apply (rule inv_char)
   887      apply (auto simp add: l_minus r_minus)
   888   done
   889 
   890 lemma (in monoid) inv_eq_imp_eq: "x \<in> Units G \<Longrightarrow> y \<in> Units G \<Longrightarrow> inv x = inv y \<Longrightarrow> x = y"
   891   apply (subgoal_tac "inv (inv x) = inv (inv y)")
   892    apply (subst (asm) Units_inv_inv)+
   893     apply auto
   894   done
   895 
   896 lemma (in ring) Units_minus_one_closed [intro]: "\<ominus> \<one> \<in> Units R"
   897   apply (unfold Units_def)
   898   apply auto
   899   apply (rule_tac x = "\<ominus> \<one>" in bexI)
   900    apply auto
   901   apply (simp add: l_minus r_minus)
   902   done
   903 
   904 lemma (in ring) inv_eq_neg_one_eq: "x \<in> Units R \<Longrightarrow> inv x = \<ominus> \<one> \<longleftrightarrow> x = \<ominus> \<one>"
   905   apply auto
   906   apply (subst Units_inv_inv [symmetric])
   907    apply auto
   908   done
   909 
   910 lemma (in monoid) inv_eq_one_eq: "x \<in> Units G \<Longrightarrow> inv x = \<one> \<longleftrightarrow> x = \<one>"
   911   by (metis Units_inv_inv inv_one)
   912 
   913 end