src/HOL/Algebra/Ring.thy
author paulson <lp15@cam.ac.uk>
Wed Jun 06 14:25:53 2018 +0100 (12 months ago)
changeset 68399 0b71d08528f0
parent 67613 ce654b0e6d69
child 68443 43055b016688
permissions -rw-r--r--
resolution of name clashes in Algebra
     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] => 'a" (infixl "\<oplus>\<index>" 65)
    17 
    18 text \<open>Derived operations.\<close>
    19 
    20 definition
    21   a_inv :: "[('a, 'm) ring_scheme, 'a ] => 'a" ("\<ominus>\<index> _" [81] 80)
    22   where "a_inv R = m_inv \<lparr>carrier = carrier R, mult = add R, one = zero R\<rparr>"
    23 
    24 definition
    25   a_minus :: "[('a, 'm) ring_scheme, 'a, 'a] => 'a" ("(_ \<ominus>\<index> _)" [65,66] 65)
    26   where "[| x \<in> carrier R; y \<in> carrier R |] ==> x \<ominus>\<^bsub>R\<^esub> y = x \<oplus>\<^bsub>R\<^esub> (\<ominus>\<^bsub>R\<^esub> y)"
    27 
    28 locale abelian_monoid =
    29   fixes G (structure)
    30   assumes a_comm_monoid:
    31      "comm_monoid \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
    32 
    33 definition
    34   finsum :: "[('b, 'm) ring_scheme, 'a => 'b, 'a set] => 'b" where
    35   "finsum G = finprod \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
    36 
    37 syntax
    38   "_finsum" :: "index => idt => 'a set => 'b => 'b"
    39       ("(3\<Oplus>__\<in>_. _)" [1000, 0, 51, 10] 10)
    40 translations
    41   "\<Oplus>\<^bsub>G\<^esub>i\<in>A. b" \<rightleftharpoons> "CONST finsum G (%i. b) A"
    42   \<comment> \<open>Beware of argument permutation!\<close>
    43 
    44 
    45 locale abelian_group = abelian_monoid +
    46   assumes a_comm_group:
    47      "comm_group \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
    48 
    49 
    50 subsection \<open>Basic Properties\<close>
    51 
    52 lemma abelian_monoidI:
    53   fixes R (structure)
    54   assumes a_closed:
    55       "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==> x \<oplus> y \<in> carrier R"
    56     and zero_closed: "\<zero> \<in> carrier R"
    57     and a_assoc:
    58       "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |] ==>
    59       (x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
    60     and l_zero: "!!x. x \<in> carrier R ==> \<zero> \<oplus> x = x"
    61     and a_comm:
    62       "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==> x \<oplus> y = y \<oplus> x"
    63   shows "abelian_monoid R"
    64   by (auto intro!: abelian_monoid.intro comm_monoidI intro: assms)
    65 
    66 lemma abelian_groupI:
    67   fixes R (structure)
    68   assumes a_closed:
    69       "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==> x \<oplus> y \<in> carrier R"
    70     and zero_closed: "zero R \<in> carrier R"
    71     and a_assoc:
    72       "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |] ==>
    73       (x \<oplus> y) \<oplus> z = x \<oplus> (y \<oplus> z)"
    74     and a_comm:
    75       "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==> x \<oplus> y = y \<oplus> x"
    76     and l_zero: "!!x. x \<in> carrier R ==> \<zero> \<oplus> x = x"
    77     and l_inv_ex: "\<And>x. x \<in> carrier R \<Longrightarrow> \<exists>y \<in> carrier R. y \<oplus> x = \<zero>"
    78   shows "abelian_group R"
    79   by (auto intro!: abelian_group.intro abelian_monoidI
    80       abelian_group_axioms.intro comm_monoidI comm_groupI
    81     intro: assms)
    82 
    83 lemma (in abelian_monoid) a_monoid:
    84   "monoid \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
    85 by (rule comm_monoid.axioms, rule a_comm_monoid) 
    86 
    87 lemma (in abelian_group) a_group:
    88   "group \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
    89   by (simp add: group_def a_monoid)
    90     (simp add: comm_group.axioms group.axioms a_comm_group)
    91 
    92 lemmas monoid_record_simps = partial_object.simps monoid.simps
    93 
    94 text \<open>Transfer facts from multiplicative structures via interpretation.\<close>
    95 
    96 sublocale abelian_monoid <
    97   add: monoid "\<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
    98   rewrites "carrier \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = carrier G"
    99     and "mult \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = add G"
   100     and "one \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = zero G"
   101   by (rule a_monoid) auto
   102 
   103 context abelian_monoid begin
   104 
   105 lemmas a_closed = add.m_closed 
   106 lemmas zero_closed = add.one_closed
   107 lemmas a_assoc = add.m_assoc
   108 lemmas l_zero = add.l_one
   109 lemmas r_zero = add.r_one
   110 lemmas minus_unique = add.inv_unique
   111 
   112 end
   113 
   114 sublocale abelian_monoid <
   115   add: comm_monoid "\<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
   116   rewrites "carrier \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = carrier G"
   117     and "mult \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = add G"
   118     and "one \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = zero G"
   119     and "finprod \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = finsum G"
   120   by (rule a_comm_monoid) (auto simp: finsum_def)
   121 
   122 context abelian_monoid begin
   123 
   124 lemmas a_comm = add.m_comm
   125 lemmas a_lcomm = add.m_lcomm
   126 lemmas a_ac = a_assoc a_comm a_lcomm
   127 
   128 lemmas finsum_empty = add.finprod_empty
   129 lemmas finsum_insert = add.finprod_insert
   130 lemmas finsum_zero = add.finprod_one
   131 lemmas finsum_closed = add.finprod_closed
   132 lemmas finsum_Un_Int = add.finprod_Un_Int
   133 lemmas finsum_Un_disjoint = add.finprod_Un_disjoint
   134 lemmas finsum_addf = add.finprod_multf
   135 lemmas finsum_cong' = add.finprod_cong'
   136 lemmas finsum_0 = add.finprod_0
   137 lemmas finsum_Suc = add.finprod_Suc
   138 lemmas finsum_Suc2 = add.finprod_Suc2
   139 lemmas finsum_add = add.finprod_mult
   140 lemmas finsum_infinite = add.finprod_infinite
   141 
   142 lemmas finsum_cong = add.finprod_cong
   143 text \<open>Usually, if this rule causes a failed congruence proof error,
   144    the reason is that the premise \<open>g \<in> B \<rightarrow> carrier G\<close> cannot be shown.
   145    Adding @{thm [source] Pi_def} to the simpset is often useful.\<close>
   146 
   147 lemmas finsum_reindex = add.finprod_reindex
   148 
   149 (* The following would be wrong.  Needed is the equivalent of [^] for addition,
   150   or indeed the canonical embedding from Nat into the monoid.
   151 
   152 lemma finsum_const:
   153   assumes fin [simp]: "finite A"
   154       and a [simp]: "a : carrier G"
   155     shows "finsum G (%x. a) A = a [^] card A"
   156   using fin apply induct
   157   apply force
   158   apply (subst finsum_insert)
   159   apply auto
   160   apply (force simp add: Pi_def)
   161   apply (subst m_comm)
   162   apply auto
   163 done
   164 *)
   165 
   166 lemmas finsum_singleton = add.finprod_singleton
   167 
   168 end
   169 
   170 sublocale abelian_group <
   171   add: group "\<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
   172   rewrites "carrier \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = carrier G"
   173     and "mult \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = add G"
   174     and "one \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = zero G"
   175     and "m_inv \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = a_inv G"
   176   by (rule a_group) (auto simp: m_inv_def a_inv_def)
   177 
   178 context abelian_group
   179 begin
   180 
   181 lemmas a_inv_closed = add.inv_closed
   182 
   183 lemma minus_closed [intro, simp]:
   184   "[| x \<in> carrier G; y \<in> carrier G |] ==> x \<ominus> y \<in> carrier G"
   185   by (simp add: a_minus_def)
   186 
   187 lemmas l_neg = add.l_inv [simp del]
   188 lemmas r_neg = add.r_inv [simp del]
   189 lemmas minus_zero = add.inv_one
   190 lemmas minus_minus = add.inv_inv
   191 lemmas a_inv_inj = add.inv_inj
   192 lemmas minus_equality = add.inv_equality
   193 
   194 end
   195 
   196 sublocale abelian_group <
   197   add: comm_group "\<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
   198   rewrites "carrier \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = carrier G"
   199     and "mult \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = add G"
   200     and "one \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = zero G"
   201     and "m_inv \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = a_inv G"
   202     and "finprod \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = finsum G"
   203   by (rule a_comm_group) (auto simp: m_inv_def a_inv_def finsum_def)
   204 
   205 lemmas (in abelian_group) minus_add = add.inv_mult
   206  
   207 text \<open>Derive an \<open>abelian_group\<close> from a \<open>comm_group\<close>\<close>
   208 
   209 lemma comm_group_abelian_groupI:
   210   fixes G (structure)
   211   assumes cg: "comm_group \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
   212   shows "abelian_group G"
   213 proof -
   214   interpret comm_group "\<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
   215     by (rule cg)
   216   show "abelian_group G" ..
   217 qed
   218 
   219 
   220 subsection \<open>Rings: Basic Definitions\<close>
   221 
   222 locale semiring = abelian_monoid R + monoid R for R (structure) +
   223   assumes l_distr: "[| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   224       ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   225     and r_distr: "[| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   226       ==> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
   227     and l_null[simp]: "x \<in> carrier R ==> \<zero> \<otimes> x = \<zero>"
   228     and r_null[simp]: "x \<in> carrier R ==> x \<otimes> \<zero> = \<zero>"
   229 
   230 locale ring = abelian_group R + monoid R for R (structure) +
   231   assumes "[| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   232       ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   233     and "[| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   234       ==> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
   235 
   236 locale cring = ring + comm_monoid R
   237 
   238 locale "domain" = cring +
   239   assumes one_not_zero [simp]: "\<one> \<noteq> \<zero>"
   240     and integral: "[| a \<otimes> b = \<zero>; a \<in> carrier R; b \<in> carrier R |] ==>
   241                   a = \<zero> \<or> b = \<zero>"
   242 
   243 locale field = "domain" +
   244   assumes field_Units: "Units R = carrier R - {\<zero>}"
   245 
   246 
   247 subsection \<open>Rings\<close>
   248 
   249 lemma ringI:
   250   fixes R (structure)
   251   assumes abelian_group: "abelian_group R"
   252     and monoid: "monoid R"
   253     and l_distr: "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   254       ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   255     and r_distr: "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   256       ==> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
   257   shows "ring R"
   258   by (auto intro: ring.intro
   259     abelian_group.axioms ring_axioms.intro assms)
   260 
   261 context ring begin
   262 
   263 lemma is_abelian_group: "abelian_group R" ..
   264 
   265 lemma is_monoid: "monoid R"
   266   by (auto intro!: monoidI m_assoc)
   267 
   268 lemma is_ring: "ring R"
   269   by (rule ring_axioms)
   270 
   271 end
   272 
   273 lemmas ring_record_simps = monoid_record_simps ring.simps
   274 
   275 lemma cringI:
   276   fixes R (structure)
   277   assumes abelian_group: "abelian_group R"
   278     and comm_monoid: "comm_monoid R"
   279     and l_distr: "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   280       ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   281   shows "cring R"
   282 proof (intro cring.intro ring.intro)
   283   show "ring_axioms R"
   284     \<comment> \<open>Right-distributivity follows from left-distributivity and
   285           commutativity.\<close>
   286   proof (rule ring_axioms.intro)
   287     fix x y z
   288     assume R: "x \<in> carrier R" "y \<in> carrier R" "z \<in> carrier R"
   289     note [simp] = comm_monoid.axioms [OF comm_monoid]
   290       abelian_group.axioms [OF abelian_group]
   291       abelian_monoid.a_closed
   292         
   293     from R have "z \<otimes> (x \<oplus> y) = (x \<oplus> y) \<otimes> z"
   294       by (simp add: comm_monoid.m_comm [OF comm_monoid.intro])
   295     also from R have "... = x \<otimes> z \<oplus> y \<otimes> z" by (simp add: l_distr)
   296     also from R have "... = z \<otimes> x \<oplus> z \<otimes> y"
   297       by (simp add: comm_monoid.m_comm [OF comm_monoid.intro])
   298     finally show "z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y" .
   299   qed (rule l_distr)
   300 qed (auto intro: cring.intro
   301   abelian_group.axioms comm_monoid.axioms ring_axioms.intro assms)
   302 
   303 (*
   304 lemma (in cring) is_comm_monoid:
   305   "comm_monoid R"
   306   by (auto intro!: comm_monoidI m_assoc m_comm)
   307 *)
   308 
   309 lemma (in cring) is_cring:
   310   "cring R" by (rule cring_axioms)
   311 
   312 
   313 subsubsection \<open>Normaliser for Rings\<close>
   314 
   315 lemma (in abelian_group) r_neg2:
   316   "[| x \<in> carrier G; y \<in> carrier G |] ==> x \<oplus> (\<ominus> x \<oplus> y) = y"
   317 proof -
   318   assume G: "x \<in> carrier G" "y \<in> carrier G"
   319   then have "(x \<oplus> \<ominus> x) \<oplus> y = y"
   320     by (simp only: r_neg l_zero)
   321   with G show ?thesis
   322     by (simp add: a_ac)
   323 qed
   324 
   325 lemma (in abelian_group) r_neg1:
   326   "[| x \<in> carrier G; y \<in> carrier G |] ==> \<ominus> x \<oplus> (x \<oplus> y) = y"
   327 proof -
   328   assume G: "x \<in> carrier G" "y \<in> carrier G"
   329   then have "(\<ominus> x \<oplus> x) \<oplus> y = y" 
   330     by (simp only: l_neg l_zero)
   331   with G show ?thesis by (simp add: a_ac)
   332 qed
   333 
   334 context ring begin
   335 
   336 text \<open>
   337   The following proofs are from Jacobson, Basic Algebra I, pp.~88--89.
   338 \<close>
   339 
   340 sublocale semiring
   341 proof -
   342   note [simp] = ring_axioms[unfolded ring_def ring_axioms_def]
   343   show "semiring R"
   344   proof (unfold_locales)
   345     fix x
   346     assume R: "x \<in> carrier R"
   347     then have "\<zero> \<otimes> x \<oplus> \<zero> \<otimes> x = (\<zero> \<oplus> \<zero>) \<otimes> x"
   348       by (simp del: l_zero r_zero)
   349     also from R have "... = \<zero> \<otimes> x \<oplus> \<zero>" by simp
   350     finally have "\<zero> \<otimes> x \<oplus> \<zero> \<otimes> x = \<zero> \<otimes> x \<oplus> \<zero>" .
   351     with R show "\<zero> \<otimes> x = \<zero>" by (simp del: r_zero)
   352     from R have "x \<otimes> \<zero> \<oplus> x \<otimes> \<zero> = x \<otimes> (\<zero> \<oplus> \<zero>)"
   353       by (simp del: l_zero r_zero)
   354     also from R have "... = x \<otimes> \<zero> \<oplus> \<zero>" by simp
   355     finally have "x \<otimes> \<zero> \<oplus> x \<otimes> \<zero> = x \<otimes> \<zero> \<oplus> \<zero>" .
   356     with R show "x \<otimes> \<zero> = \<zero>" by (simp del: r_zero)
   357   qed auto
   358 qed
   359 
   360 lemma l_minus:
   361   "[| x \<in> carrier R; y \<in> carrier R |] ==> \<ominus> x \<otimes> y = \<ominus> (x \<otimes> y)"
   362 proof -
   363   assume R: "x \<in> carrier R" "y \<in> carrier R"
   364   then have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y = (\<ominus> x \<oplus> x) \<otimes> y" by (simp add: l_distr)
   365   also from R have "... = \<zero>" by (simp add: l_neg)
   366   finally have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y = \<zero>" .
   367   with R have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y \<oplus> \<ominus> (x \<otimes> y) = \<zero> \<oplus> \<ominus> (x \<otimes> y)" by simp
   368   with R show ?thesis by (simp add: a_assoc r_neg)
   369 qed
   370 
   371 lemma r_minus:
   372   "[| x \<in> carrier R; y \<in> carrier R |] ==> x \<otimes> \<ominus> y = \<ominus> (x \<otimes> y)"
   373 proof -
   374   assume R: "x \<in> carrier R" "y \<in> carrier R"
   375   then have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y = x \<otimes> (\<ominus> y \<oplus> y)" by (simp add: r_distr)
   376   also from R have "... = \<zero>" by (simp add: l_neg)
   377   finally have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y = \<zero>" .
   378   with R have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y \<oplus> \<ominus> (x \<otimes> y) = \<zero> \<oplus> \<ominus> (x \<otimes> y)" by simp
   379   with R show ?thesis by (simp add: a_assoc r_neg )
   380 qed
   381 
   382 end
   383 
   384 lemma (in abelian_group) minus_eq:
   385   "[| x \<in> carrier G; y \<in> carrier G |] ==> x \<ominus> y = x \<oplus> \<ominus> y"
   386   by (simp only: a_minus_def)
   387 
   388 text \<open>Setup algebra method:
   389   compute distributive normal form in locale contexts\<close>
   390 
   391 ML_file "ringsimp.ML"
   392 
   393 attribute_setup algebra = \<open>
   394   Scan.lift ((Args.add >> K true || Args.del >> K false) --| Args.colon || Scan.succeed true)
   395     -- Scan.lift Args.name -- Scan.repeat Args.term
   396     >> (fn ((b, n), ts) => if b then Ringsimp.add_struct (n, ts) else Ringsimp.del_struct (n, ts))
   397 \<close> "theorems controlling algebra method"
   398 
   399 method_setup algebra = \<open>
   400   Scan.succeed (SIMPLE_METHOD' o Ringsimp.algebra_tac)
   401 \<close> "normalisation of algebraic structure"
   402 
   403 lemmas (in semiring) semiring_simprules
   404   [algebra ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   405   a_closed zero_closed  m_closed one_closed
   406   a_assoc l_zero  a_comm m_assoc l_one l_distr r_zero
   407   a_lcomm r_distr l_null r_null 
   408 
   409 lemmas (in ring) ring_simprules
   410   [algebra ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   411   a_closed zero_closed a_inv_closed minus_closed m_closed one_closed
   412   a_assoc l_zero l_neg a_comm m_assoc l_one l_distr minus_eq
   413   r_zero r_neg r_neg2 r_neg1 minus_add minus_minus minus_zero
   414   a_lcomm r_distr l_null r_null l_minus r_minus
   415 
   416 lemmas (in cring)
   417   [algebra del: ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   418   _
   419 
   420 lemmas (in cring) cring_simprules
   421   [algebra add: cring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   422   a_closed zero_closed a_inv_closed minus_closed m_closed one_closed
   423   a_assoc l_zero l_neg a_comm m_assoc l_one l_distr m_comm minus_eq
   424   r_zero r_neg r_neg2 r_neg1 minus_add minus_minus minus_zero
   425   a_lcomm m_lcomm r_distr l_null r_null l_minus r_minus
   426 
   427 lemma (in semiring) nat_pow_zero:
   428   "(n::nat) \<noteq> 0 \<Longrightarrow> \<zero> [^] n = \<zero>"
   429   by (induct n) simp_all
   430 
   431 context semiring begin
   432 
   433 lemma one_zeroD:
   434   assumes onezero: "\<one> = \<zero>"
   435   shows "carrier R = {\<zero>}"
   436 proof (rule, rule)
   437   fix x
   438   assume xcarr: "x \<in> carrier R"
   439   from xcarr have "x = x \<otimes> \<one>" by simp
   440   with onezero have "x = x \<otimes> \<zero>" by simp
   441   with xcarr have "x = \<zero>" by simp
   442   then show "x \<in> {\<zero>}" by fast
   443 qed fast
   444 
   445 lemma one_zeroI:
   446   assumes carrzero: "carrier R = {\<zero>}"
   447   shows "\<one> = \<zero>"
   448 proof -
   449   from one_closed and carrzero
   450       show "\<one> = \<zero>" by simp
   451 qed
   452 
   453 lemma carrier_one_zero: "(carrier R = {\<zero>}) = (\<one> = \<zero>)"
   454   apply rule
   455    apply (erule one_zeroI)
   456   apply (erule one_zeroD)
   457   done
   458 
   459 lemma carrier_one_not_zero: "(carrier R \<noteq> {\<zero>}) = (\<one> \<noteq> \<zero>)"
   460   by (simp add: carrier_one_zero)
   461 
   462 end
   463 
   464 text \<open>Two examples for use of method algebra\<close>
   465 
   466 lemma
   467   fixes R (structure) and S (structure)
   468   assumes "ring R" "cring S"
   469   assumes RS: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier S" "d \<in> carrier S"
   470   shows "a \<oplus> \<ominus> (a \<oplus> \<ominus> b) = b \<and> c \<otimes>\<^bsub>S\<^esub> d = d \<otimes>\<^bsub>S\<^esub> c"
   471 proof -
   472   interpret ring R by fact
   473   interpret cring S by fact
   474   from RS show ?thesis by algebra
   475 qed
   476 
   477 lemma
   478   fixes R (structure)
   479   assumes "ring R"
   480   assumes R: "a \<in> carrier R" "b \<in> carrier R"
   481   shows "a \<ominus> (a \<ominus> b) = b"
   482 proof -
   483   interpret ring R by fact
   484   from R show ?thesis by algebra
   485 qed
   486 
   487 
   488 subsubsection \<open>Sums over Finite Sets\<close>
   489 
   490 lemma (in semiring) finsum_ldistr:
   491   "[| finite A; a \<in> carrier R; f \<in> A \<rightarrow> carrier R |] ==>
   492    finsum R f A \<otimes> a = finsum R (%i. f i \<otimes> a) A"
   493 proof (induct set: finite)
   494   case empty then show ?case by simp
   495 next
   496   case (insert x F) then show ?case by (simp add: Pi_def l_distr)
   497 qed
   498 
   499 lemma (in semiring) finsum_rdistr:
   500   "[| finite A; a \<in> carrier R; f \<in> A \<rightarrow> carrier R |] ==>
   501    a \<otimes> finsum R f A = finsum R (%i. a \<otimes> f i) A"
   502 proof (induct set: finite)
   503   case empty then show ?case by simp
   504 next
   505   case (insert x F) then show ?case by (simp add: Pi_def r_distr)
   506 qed
   507 
   508 
   509 subsection \<open>Integral Domains\<close>
   510 
   511 context "domain" begin
   512 
   513 lemma zero_not_one [simp]:
   514   "\<zero> \<noteq> \<one>"
   515   by (rule not_sym) simp
   516 
   517 lemma integral_iff: (* not by default a simp rule! *)
   518   "[| a \<in> carrier R; b \<in> carrier R |] ==> (a \<otimes> b = \<zero>) = (a = \<zero> \<or> b = \<zero>)"
   519 proof
   520   assume "a \<in> carrier R" "b \<in> carrier R" "a \<otimes> b = \<zero>"
   521   then show "a = \<zero> \<or> b = \<zero>" by (simp add: integral)
   522 next
   523   assume "a \<in> carrier R" "b \<in> carrier R" "a = \<zero> \<or> b = \<zero>"
   524   then show "a \<otimes> b = \<zero>" by auto
   525 qed
   526 
   527 lemma m_lcancel:
   528   assumes prem: "a \<noteq> \<zero>"
   529     and R: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier R"
   530   shows "(a \<otimes> b = a \<otimes> c) = (b = c)"
   531 proof
   532   assume eq: "a \<otimes> b = a \<otimes> c"
   533   with R have "a \<otimes> (b \<ominus> c) = \<zero>" by algebra
   534   with R have "a = \<zero> \<or> (b \<ominus> c) = \<zero>" by (simp add: integral_iff)
   535   with prem and R have "b \<ominus> c = \<zero>" by auto 
   536   with R have "b = b \<ominus> (b \<ominus> c)" by algebra 
   537   also from R have "b \<ominus> (b \<ominus> c) = c" by algebra
   538   finally show "b = c" .
   539 next
   540   assume "b = c" then show "a \<otimes> b = a \<otimes> c" by simp
   541 qed
   542 
   543 lemma m_rcancel:
   544   assumes prem: "a \<noteq> \<zero>"
   545     and R: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier R"
   546   shows conc: "(b \<otimes> a = c \<otimes> a) = (b = c)"
   547 proof -
   548   from prem and R have "(a \<otimes> b = a \<otimes> c) = (b = c)" by (rule m_lcancel)
   549   with R show ?thesis by algebra
   550 qed
   551 
   552 end
   553 
   554 
   555 subsection \<open>Fields\<close>
   556 
   557 text \<open>Field would not need to be derived from domain, the properties
   558   for domain follow from the assumptions of field\<close>
   559 lemma (in cring) cring_fieldI:
   560   assumes field_Units: "Units R = carrier R - {\<zero>}"
   561   shows "field R"
   562 proof
   563   from field_Units have "\<zero> \<notin> Units R" by fast
   564   moreover have "\<one> \<in> Units R" by fast
   565   ultimately show "\<one> \<noteq> \<zero>" by force
   566 next
   567   fix a b
   568   assume acarr: "a \<in> carrier R"
   569     and bcarr: "b \<in> carrier R"
   570     and ab: "a \<otimes> b = \<zero>"
   571   show "a = \<zero> \<or> b = \<zero>"
   572   proof (cases "a = \<zero>", simp)
   573     assume "a \<noteq> \<zero>"
   574     with field_Units and acarr have aUnit: "a \<in> Units R" by fast
   575     from bcarr have "b = \<one> \<otimes> b" by algebra
   576     also from aUnit acarr have "... = (inv a \<otimes> a) \<otimes> b" by simp
   577     also from acarr bcarr aUnit[THEN Units_inv_closed]
   578     have "... = (inv a) \<otimes> (a \<otimes> b)" by algebra
   579     also from ab and acarr bcarr aUnit have "... = (inv a) \<otimes> \<zero>" by simp
   580     also from aUnit[THEN Units_inv_closed] have "... = \<zero>" by algebra
   581     finally have "b = \<zero>" .
   582     then show "a = \<zero> \<or> b = \<zero>" by simp
   583   qed
   584 qed (rule field_Units)
   585 
   586 text \<open>Another variant to show that something is a field\<close>
   587 lemma (in cring) cring_fieldI2:
   588   assumes notzero: "\<zero> \<noteq> \<one>"
   589   and invex: "\<And>a. \<lbrakk>a \<in> carrier R; a \<noteq> \<zero>\<rbrakk> \<Longrightarrow> \<exists>b\<in>carrier R. a \<otimes> b = \<one>"
   590   shows "field R"
   591   apply (rule cring_fieldI, simp add: Units_def)
   592   apply (rule, clarsimp)
   593   apply (simp add: notzero)
   594 proof (clarsimp)
   595   fix x
   596   assume xcarr: "x \<in> carrier R"
   597     and "x \<noteq> \<zero>"
   598   then have "\<exists>y\<in>carrier R. x \<otimes> y = \<one>" by (rule invex)
   599   then obtain y where ycarr: "y \<in> carrier R" and xy: "x \<otimes> y = \<one>" by fast
   600   from xy xcarr ycarr have "y \<otimes> x = \<one>" by (simp add: m_comm)
   601   with ycarr and xy show "\<exists>y\<in>carrier R. y \<otimes> x = \<one> \<and> x \<otimes> y = \<one>" by fast
   602 qed
   603 
   604 
   605 subsection \<open>Morphisms\<close>
   606 
   607 definition
   608   ring_hom :: "[('a, 'm) ring_scheme, ('b, 'n) ring_scheme] => ('a => 'b) set"
   609   where "ring_hom R S =
   610     {h. h \<in> carrier R \<rightarrow> carrier S \<and>
   611       (\<forall>x y. x \<in> carrier R \<and> y \<in> carrier R \<longrightarrow>
   612         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>
   613       h \<one>\<^bsub>R\<^esub> = \<one>\<^bsub>S\<^esub>}"
   614 
   615 lemma ring_hom_memI:
   616   fixes R (structure) and S (structure)
   617   assumes hom_closed: "!!x. x \<in> carrier R ==> h x \<in> carrier S"
   618     and hom_mult: "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==>
   619       h (x \<otimes> y) = h x \<otimes>\<^bsub>S\<^esub> h y"
   620     and hom_add: "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==>
   621       h (x \<oplus> y) = h x \<oplus>\<^bsub>S\<^esub> h y"
   622     and hom_one: "h \<one> = \<one>\<^bsub>S\<^esub>"
   623   shows "h \<in> ring_hom R S"
   624   by (auto simp add: ring_hom_def assms Pi_def)
   625 
   626 lemma ring_hom_closed:
   627   "[| h \<in> ring_hom R S; x \<in> carrier R |] ==> h x \<in> carrier S"
   628   by (auto simp add: ring_hom_def funcset_mem)
   629 
   630 lemma ring_hom_mult:
   631   fixes R (structure) and S (structure)
   632   shows
   633     "[| h \<in> ring_hom R S; x \<in> carrier R; y \<in> carrier R |] ==>
   634     h (x \<otimes> y) = h x \<otimes>\<^bsub>S\<^esub> h y"
   635     by (simp add: ring_hom_def)
   636 
   637 lemma ring_hom_add:
   638   fixes R (structure) and S (structure)
   639   shows
   640     "[| h \<in> ring_hom R S; x \<in> carrier R; y \<in> carrier R |] ==>
   641     h (x \<oplus> y) = h x \<oplus>\<^bsub>S\<^esub> h y"
   642     by (simp add: ring_hom_def)
   643 
   644 lemma ring_hom_one:
   645   fixes R (structure) and S (structure)
   646   shows "h \<in> ring_hom R S ==> h \<one> = \<one>\<^bsub>S\<^esub>"
   647   by (simp add: ring_hom_def)
   648 
   649 locale ring_hom_cring = R?: cring R + S?: cring S
   650     for R (structure) and S (structure) +
   651   fixes h
   652   assumes homh [simp, intro]: "h \<in> ring_hom R S"
   653   notes hom_closed [simp, intro] = ring_hom_closed [OF homh]
   654     and hom_mult [simp] = ring_hom_mult [OF homh]
   655     and hom_add [simp] = ring_hom_add [OF homh]
   656     and hom_one [simp] = ring_hom_one [OF homh]
   657 
   658 lemma (in ring_hom_cring) hom_zero [simp]:
   659   "h \<zero> = \<zero>\<^bsub>S\<^esub>"
   660 proof -
   661   have "h \<zero> \<oplus>\<^bsub>S\<^esub> h \<zero> = h \<zero> \<oplus>\<^bsub>S\<^esub> \<zero>\<^bsub>S\<^esub>"
   662     by (simp add: hom_add [symmetric] del: hom_add)
   663   then show ?thesis by (simp del: S.r_zero)
   664 qed
   665 
   666 lemma (in ring_hom_cring) hom_a_inv [simp]:
   667   "x \<in> carrier R ==> h (\<ominus> x) = \<ominus>\<^bsub>S\<^esub> h x"
   668 proof -
   669   assume R: "x \<in> carrier R"
   670   then have "h x \<oplus>\<^bsub>S\<^esub> h (\<ominus> x) = h x \<oplus>\<^bsub>S\<^esub> (\<ominus>\<^bsub>S\<^esub> h x)"
   671     by (simp add: hom_add [symmetric] R.r_neg S.r_neg del: hom_add)
   672   with R show ?thesis by simp
   673 qed
   674 
   675 lemma (in ring_hom_cring) hom_finsum [simp]:
   676   "f \<in> A \<rightarrow> carrier R \<Longrightarrow>
   677   h (finsum R f A) = finsum S (h \<circ> f) A"
   678   by (induct A rule: infinite_finite_induct, auto simp: Pi_def)
   679 
   680 lemma (in ring_hom_cring) hom_finprod:
   681   "f \<in> A \<rightarrow> carrier R \<Longrightarrow>
   682   h (finprod R f A) = finprod S (h \<circ> f) A"
   683   by (induct A rule: infinite_finite_induct, auto simp: Pi_def)
   684 
   685 declare ring_hom_cring.hom_finprod [simp]
   686 
   687 lemma id_ring_hom [simp]:
   688   "id \<in> ring_hom R R"
   689   by (auto intro!: ring_hom_memI)
   690 
   691 end