src/HOL/Algebra/Ring.thy
author wenzelm
Sat Oct 10 16:26:23 2015 +0200 (2015-10-10)
changeset 61382 efac889fccbc
parent 60773 d09c66a0ea10
child 61384 9f5145281888
permissions -rw-r--r--
isabelle update_cartouches;
     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" (infixl "\<ominus>\<index>" 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>\<index>i\<in>A. b" \<rightleftharpoons> "CONST finsum \<struct>\<index> (%i. b) A"
    42   -- \<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: "!!x. x \<in> carrier R ==> EX y : 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   where "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   where "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 @{text "g \<in> B -> carrier G"} 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   where "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 a_l_cancel = add.l_cancel
   188 lemmas a_r_cancel = add.r_cancel
   189 lemmas l_neg = add.l_inv [simp del]
   190 lemmas r_neg = add.r_inv [simp del]
   191 lemmas minus_zero = add.inv_one
   192 lemmas minus_minus = add.inv_inv
   193 lemmas a_inv_inj = add.inv_inj
   194 lemmas minus_equality = add.inv_equality
   195 
   196 end
   197 
   198 sublocale abelian_group <
   199   add!: comm_group "\<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
   200   where "carrier \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = carrier G"
   201     and "mult \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = add G"
   202     and "one \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = zero G"
   203     and "m_inv \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = a_inv G"
   204     and "finprod \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr> = finsum G"
   205   by (rule a_comm_group) (auto simp: m_inv_def a_inv_def finsum_def)
   206 
   207 lemmas (in abelian_group) minus_add = add.inv_mult
   208  
   209 text \<open>Derive an @{text "abelian_group"} from a @{text "comm_group"}\<close>
   210 
   211 lemma comm_group_abelian_groupI:
   212   fixes G (structure)
   213   assumes cg: "comm_group \<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
   214   shows "abelian_group G"
   215 proof -
   216   interpret comm_group "\<lparr>carrier = carrier G, mult = add G, one = zero G\<rparr>"
   217     by (rule cg)
   218   show "abelian_group G" ..
   219 qed
   220 
   221 
   222 subsection \<open>Rings: Basic Definitions\<close>
   223 
   224 locale semiring = abelian_monoid R + monoid R for R (structure) +
   225   assumes l_distr: "[| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   226       ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   227     and r_distr: "[| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   228       ==> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
   229     and l_null[simp]: "x \<in> carrier R ==> \<zero> \<otimes> x = \<zero>"
   230     and r_null[simp]: "x \<in> carrier R ==> x \<otimes> \<zero> = \<zero>"
   231 
   232 locale ring = abelian_group R + monoid R for R (structure) +
   233   assumes "[| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   234       ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   235     and "[| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   236       ==> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
   237 
   238 locale cring = ring + comm_monoid R
   239 
   240 locale "domain" = cring +
   241   assumes one_not_zero [simp]: "\<one> ~= \<zero>"
   242     and integral: "[| a \<otimes> b = \<zero>; a \<in> carrier R; b \<in> carrier R |] ==>
   243                   a = \<zero> | b = \<zero>"
   244 
   245 locale field = "domain" +
   246   assumes field_Units: "Units R = carrier R - {\<zero>}"
   247 
   248 
   249 subsection \<open>Rings\<close>
   250 
   251 lemma ringI:
   252   fixes R (structure)
   253   assumes abelian_group: "abelian_group R"
   254     and monoid: "monoid R"
   255     and l_distr: "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   256       ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   257     and r_distr: "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   258       ==> z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y"
   259   shows "ring R"
   260   by (auto intro: ring.intro
   261     abelian_group.axioms ring_axioms.intro assms)
   262 
   263 context ring begin
   264 
   265 lemma is_abelian_group: "abelian_group R" ..
   266 
   267 lemma is_monoid: "monoid R"
   268   by (auto intro!: monoidI m_assoc)
   269 
   270 lemma is_ring: "ring R"
   271   by (rule ring_axioms)
   272 
   273 end
   274 
   275 lemmas ring_record_simps = monoid_record_simps ring.simps
   276 
   277 lemma cringI:
   278   fixes R (structure)
   279   assumes abelian_group: "abelian_group R"
   280     and comm_monoid: "comm_monoid R"
   281     and l_distr: "!!x y z. [| x \<in> carrier R; y \<in> carrier R; z \<in> carrier R |]
   282       ==> (x \<oplus> y) \<otimes> z = x \<otimes> z \<oplus> y \<otimes> z"
   283   shows "cring R"
   284 proof (intro cring.intro ring.intro)
   285   show "ring_axioms R"
   286     -- \<open>Right-distributivity follows from left-distributivity and
   287           commutativity.\<close>
   288   proof (rule ring_axioms.intro)
   289     fix x y z
   290     assume R: "x \<in> carrier R" "y \<in> carrier R" "z \<in> carrier R"
   291     note [simp] = comm_monoid.axioms [OF comm_monoid]
   292       abelian_group.axioms [OF abelian_group]
   293       abelian_monoid.a_closed
   294         
   295     from R have "z \<otimes> (x \<oplus> y) = (x \<oplus> y) \<otimes> z"
   296       by (simp add: comm_monoid.m_comm [OF comm_monoid.intro])
   297     also from R have "... = x \<otimes> z \<oplus> y \<otimes> z" by (simp add: l_distr)
   298     also from R have "... = z \<otimes> x \<oplus> z \<otimes> y"
   299       by (simp add: comm_monoid.m_comm [OF comm_monoid.intro])
   300     finally show "z \<otimes> (x \<oplus> y) = z \<otimes> x \<oplus> z \<otimes> y" .
   301   qed (rule l_distr)
   302 qed (auto intro: cring.intro
   303   abelian_group.axioms comm_monoid.axioms ring_axioms.intro assms)
   304 
   305 (*
   306 lemma (in cring) is_comm_monoid:
   307   "comm_monoid R"
   308   by (auto intro!: comm_monoidI m_assoc m_comm)
   309 *)
   310 
   311 lemma (in cring) is_cring:
   312   "cring R" by (rule cring_axioms)
   313 
   314 
   315 subsubsection \<open>Normaliser for Rings\<close>
   316 
   317 lemma (in abelian_group) r_neg2:
   318   "[| x \<in> carrier G; y \<in> carrier G |] ==> x \<oplus> (\<ominus> x \<oplus> y) = y"
   319 proof -
   320   assume G: "x \<in> carrier G" "y \<in> carrier G"
   321   then have "(x \<oplus> \<ominus> x) \<oplus> y = y"
   322     by (simp only: r_neg l_zero)
   323   with G show ?thesis
   324     by (simp add: a_ac)
   325 qed
   326 
   327 lemma (in abelian_group) r_neg1:
   328   "[| x \<in> carrier G; y \<in> carrier G |] ==> \<ominus> x \<oplus> (x \<oplus> y) = y"
   329 proof -
   330   assume G: "x \<in> carrier G" "y \<in> carrier G"
   331   then have "(\<ominus> x \<oplus> x) \<oplus> y = y" 
   332     by (simp only: l_neg l_zero)
   333   with G show ?thesis by (simp add: a_ac)
   334 qed
   335 
   336 context ring begin
   337 
   338 text \<open>
   339   The following proofs are from Jacobson, Basic Algebra I, pp.~88--89.
   340 \<close>
   341 
   342 sublocale semiring
   343 proof -
   344   note [simp] = ring_axioms[unfolded ring_def ring_axioms_def]
   345   show "semiring R"
   346   proof (unfold_locales)
   347     fix x
   348     assume R: "x \<in> carrier R"
   349     then have "\<zero> \<otimes> x \<oplus> \<zero> \<otimes> x = (\<zero> \<oplus> \<zero>) \<otimes> x"
   350       by (simp del: l_zero r_zero)
   351     also from R have "... = \<zero> \<otimes> x \<oplus> \<zero>" by simp
   352     finally have "\<zero> \<otimes> x \<oplus> \<zero> \<otimes> x = \<zero> \<otimes> x \<oplus> \<zero>" .
   353     with R show "\<zero> \<otimes> x = \<zero>" by (simp del: r_zero)
   354     from R have "x \<otimes> \<zero> \<oplus> x \<otimes> \<zero> = x \<otimes> (\<zero> \<oplus> \<zero>)"
   355       by (simp del: l_zero r_zero)
   356     also from R have "... = x \<otimes> \<zero> \<oplus> \<zero>" by simp
   357     finally have "x \<otimes> \<zero> \<oplus> x \<otimes> \<zero> = x \<otimes> \<zero> \<oplus> \<zero>" .
   358     with R show "x \<otimes> \<zero> = \<zero>" by (simp del: r_zero)
   359   qed auto
   360 qed
   361 
   362 lemma l_minus:
   363   "[| x \<in> carrier R; y \<in> carrier R |] ==> \<ominus> x \<otimes> y = \<ominus> (x \<otimes> y)"
   364 proof -
   365   assume R: "x \<in> carrier R" "y \<in> carrier R"
   366   then have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y = (\<ominus> x \<oplus> x) \<otimes> y" by (simp add: l_distr)
   367   also from R have "... = \<zero>" by (simp add: l_neg)
   368   finally have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y = \<zero>" .
   369   with R have "(\<ominus> x) \<otimes> y \<oplus> x \<otimes> y \<oplus> \<ominus> (x \<otimes> y) = \<zero> \<oplus> \<ominus> (x \<otimes> y)" by simp
   370   with R show ?thesis by (simp add: a_assoc r_neg)
   371 qed
   372 
   373 lemma r_minus:
   374   "[| x \<in> carrier R; y \<in> carrier R |] ==> x \<otimes> \<ominus> y = \<ominus> (x \<otimes> y)"
   375 proof -
   376   assume R: "x \<in> carrier R" "y \<in> carrier R"
   377   then have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y = x \<otimes> (\<ominus> y \<oplus> y)" by (simp add: r_distr)
   378   also from R have "... = \<zero>" by (simp add: l_neg)
   379   finally have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y = \<zero>" .
   380   with R have "x \<otimes> (\<ominus> y) \<oplus> x \<otimes> y \<oplus> \<ominus> (x \<otimes> y) = \<zero> \<oplus> \<ominus> (x \<otimes> y)" by simp
   381   with R show ?thesis by (simp add: a_assoc r_neg )
   382 qed
   383 
   384 end
   385 
   386 lemma (in abelian_group) minus_eq:
   387   "[| x \<in> carrier G; y \<in> carrier G |] ==> x \<ominus> y = x \<oplus> \<ominus> y"
   388   by (simp only: a_minus_def)
   389 
   390 text \<open>Setup algebra method:
   391   compute distributive normal form in locale contexts\<close>
   392 
   393 ML_file "ringsimp.ML"
   394 
   395 attribute_setup algebra = \<open>
   396   Scan.lift ((Args.add >> K true || Args.del >> K false) --| Args.colon || Scan.succeed true)
   397     -- Scan.lift Args.name -- Scan.repeat Args.term
   398     >> (fn ((b, n), ts) => if b then Ringsimp.add_struct (n, ts) else Ringsimp.del_struct (n, ts))
   399 \<close> "theorems controlling algebra method"
   400 
   401 method_setup algebra = \<open>
   402   Scan.succeed (SIMPLE_METHOD' o Ringsimp.algebra_tac)
   403 \<close> "normalisation of algebraic structure"
   404 
   405 lemmas (in semiring) semiring_simprules
   406   [algebra ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   407   a_closed zero_closed  m_closed one_closed
   408   a_assoc l_zero  a_comm m_assoc l_one l_distr r_zero
   409   a_lcomm r_distr l_null r_null 
   410 
   411 lemmas (in ring) ring_simprules
   412   [algebra ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   413   a_closed zero_closed a_inv_closed minus_closed m_closed one_closed
   414   a_assoc l_zero l_neg a_comm m_assoc l_one l_distr minus_eq
   415   r_zero r_neg r_neg2 r_neg1 minus_add minus_minus minus_zero
   416   a_lcomm r_distr l_null r_null l_minus r_minus
   417 
   418 lemmas (in cring)
   419   [algebra del: ring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   420   _
   421 
   422 lemmas (in cring) cring_simprules
   423   [algebra add: cring "zero R" "add R" "a_inv R" "a_minus R" "one R" "mult R"] =
   424   a_closed zero_closed a_inv_closed minus_closed m_closed one_closed
   425   a_assoc l_zero l_neg a_comm m_assoc l_one l_distr m_comm minus_eq
   426   r_zero r_neg r_neg2 r_neg1 minus_add minus_minus minus_zero
   427   a_lcomm m_lcomm r_distr l_null r_null l_minus r_minus
   428 
   429 lemma (in semiring) nat_pow_zero:
   430   "(n::nat) ~= 0 ==> \<zero> (^) n = \<zero>"
   431   by (induct n) simp_all
   432 
   433 context semiring begin
   434 
   435 lemma one_zeroD:
   436   assumes onezero: "\<one> = \<zero>"
   437   shows "carrier R = {\<zero>}"
   438 proof (rule, rule)
   439   fix x
   440   assume xcarr: "x \<in> carrier R"
   441   from xcarr have "x = x \<otimes> \<one>" by simp
   442   with onezero have "x = x \<otimes> \<zero>" by simp
   443   with xcarr have "x = \<zero>" by simp
   444   then show "x \<in> {\<zero>}" by fast
   445 qed fast
   446 
   447 lemma one_zeroI:
   448   assumes carrzero: "carrier R = {\<zero>}"
   449   shows "\<one> = \<zero>"
   450 proof -
   451   from one_closed and carrzero
   452       show "\<one> = \<zero>" by simp
   453 qed
   454 
   455 lemma carrier_one_zero: "(carrier R = {\<zero>}) = (\<one> = \<zero>)"
   456   apply rule
   457    apply (erule one_zeroI)
   458   apply (erule one_zeroD)
   459   done
   460 
   461 lemma carrier_one_not_zero: "(carrier R \<noteq> {\<zero>}) = (\<one> \<noteq> \<zero>)"
   462   by (simp add: carrier_one_zero)
   463 
   464 end
   465 
   466 text \<open>Two examples for use of method algebra\<close>
   467 
   468 lemma
   469   fixes R (structure) and S (structure)
   470   assumes "ring R" "cring S"
   471   assumes RS: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier S" "d \<in> carrier S"
   472   shows "a \<oplus> \<ominus> (a \<oplus> \<ominus> b) = b & c \<otimes>\<^bsub>S\<^esub> d = d \<otimes>\<^bsub>S\<^esub> c"
   473 proof -
   474   interpret ring R by fact
   475   interpret cring S by fact
   476   from RS show ?thesis by algebra
   477 qed
   478 
   479 lemma
   480   fixes R (structure)
   481   assumes "ring R"
   482   assumes R: "a \<in> carrier R" "b \<in> carrier R"
   483   shows "a \<ominus> (a \<ominus> b) = b"
   484 proof -
   485   interpret ring R by fact
   486   from R show ?thesis by algebra
   487 qed
   488 
   489 
   490 subsubsection \<open>Sums over Finite Sets\<close>
   491 
   492 lemma (in semiring) finsum_ldistr:
   493   "[| finite A; a \<in> carrier R; f \<in> A -> carrier R |] ==>
   494    finsum R f A \<otimes> a = finsum R (%i. f i \<otimes> a) A"
   495 proof (induct set: finite)
   496   case empty then show ?case by simp
   497 next
   498   case (insert x F) then show ?case by (simp add: Pi_def l_distr)
   499 qed
   500 
   501 lemma (in semiring) finsum_rdistr:
   502   "[| finite A; a \<in> carrier R; f \<in> A -> carrier R |] ==>
   503    a \<otimes> finsum R f A = finsum R (%i. a \<otimes> f i) A"
   504 proof (induct set: finite)
   505   case empty then show ?case by simp
   506 next
   507   case (insert x F) then show ?case by (simp add: Pi_def r_distr)
   508 qed
   509 
   510 
   511 subsection \<open>Integral Domains\<close>
   512 
   513 context "domain" begin
   514 
   515 lemma zero_not_one [simp]:
   516   "\<zero> ~= \<one>"
   517   by (rule not_sym) simp
   518 
   519 lemma integral_iff: (* not by default a simp rule! *)
   520   "[| a \<in> carrier R; b \<in> carrier R |] ==> (a \<otimes> b = \<zero>) = (a = \<zero> | b = \<zero>)"
   521 proof
   522   assume "a \<in> carrier R" "b \<in> carrier R" "a \<otimes> b = \<zero>"
   523   then show "a = \<zero> | b = \<zero>" by (simp add: integral)
   524 next
   525   assume "a \<in> carrier R" "b \<in> carrier R" "a = \<zero> | b = \<zero>"
   526   then show "a \<otimes> b = \<zero>" by auto
   527 qed
   528 
   529 lemma m_lcancel:
   530   assumes prem: "a ~= \<zero>"
   531     and R: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier R"
   532   shows "(a \<otimes> b = a \<otimes> c) = (b = c)"
   533 proof
   534   assume eq: "a \<otimes> b = a \<otimes> c"
   535   with R have "a \<otimes> (b \<ominus> c) = \<zero>" by algebra
   536   with R have "a = \<zero> | (b \<ominus> c) = \<zero>" by (simp add: integral_iff)
   537   with prem and R have "b \<ominus> c = \<zero>" by auto 
   538   with R have "b = b \<ominus> (b \<ominus> c)" by algebra 
   539   also from R have "b \<ominus> (b \<ominus> c) = c" by algebra
   540   finally show "b = c" .
   541 next
   542   assume "b = c" then show "a \<otimes> b = a \<otimes> c" by simp
   543 qed
   544 
   545 lemma m_rcancel:
   546   assumes prem: "a ~= \<zero>"
   547     and R: "a \<in> carrier R" "b \<in> carrier R" "c \<in> carrier R"
   548   shows conc: "(b \<otimes> a = c \<otimes> a) = (b = c)"
   549 proof -
   550   from prem and R have "(a \<otimes> b = a \<otimes> c) = (b = c)" by (rule m_lcancel)
   551   with R show ?thesis by algebra
   552 qed
   553 
   554 end
   555 
   556 
   557 subsection \<open>Fields\<close>
   558 
   559 text \<open>Field would not need to be derived from domain, the properties
   560   for domain follow from the assumptions of field\<close>
   561 lemma (in cring) cring_fieldI:
   562   assumes field_Units: "Units R = carrier R - {\<zero>}"
   563   shows "field R"
   564 proof
   565   from field_Units have "\<zero> \<notin> Units R" by fast
   566   moreover have "\<one> \<in> Units R" by fast
   567   ultimately show "\<one> \<noteq> \<zero>" by force
   568 next
   569   fix a b
   570   assume acarr: "a \<in> carrier R"
   571     and bcarr: "b \<in> carrier R"
   572     and ab: "a \<otimes> b = \<zero>"
   573   show "a = \<zero> \<or> b = \<zero>"
   574   proof (cases "a = \<zero>", simp)
   575     assume "a \<noteq> \<zero>"
   576     with field_Units and acarr have aUnit: "a \<in> Units R" by fast
   577     from bcarr have "b = \<one> \<otimes> b" by algebra
   578     also from aUnit acarr have "... = (inv a \<otimes> a) \<otimes> b" by simp
   579     also from acarr bcarr aUnit[THEN Units_inv_closed]
   580     have "... = (inv a) \<otimes> (a \<otimes> b)" by algebra
   581     also from ab and acarr bcarr aUnit have "... = (inv a) \<otimes> \<zero>" by simp
   582     also from aUnit[THEN Units_inv_closed] have "... = \<zero>" by algebra
   583     finally have "b = \<zero>" .
   584     then show "a = \<zero> \<or> b = \<zero>" by simp
   585   qed
   586 qed (rule field_Units)
   587 
   588 text \<open>Another variant to show that something is a field\<close>
   589 lemma (in cring) cring_fieldI2:
   590   assumes notzero: "\<zero> \<noteq> \<one>"
   591   and invex: "\<And>a. \<lbrakk>a \<in> carrier R; a \<noteq> \<zero>\<rbrakk> \<Longrightarrow> \<exists>b\<in>carrier R. a \<otimes> b = \<one>"
   592   shows "field R"
   593   apply (rule cring_fieldI, simp add: Units_def)
   594   apply (rule, clarsimp)
   595   apply (simp add: notzero)
   596 proof (clarsimp)
   597   fix x
   598   assume xcarr: "x \<in> carrier R"
   599     and "x \<noteq> \<zero>"
   600   then have "\<exists>y\<in>carrier R. x \<otimes> y = \<one>" by (rule invex)
   601   then obtain y where ycarr: "y \<in> carrier R" and xy: "x \<otimes> y = \<one>" by fast
   602   from xy xcarr ycarr have "y \<otimes> x = \<one>" by (simp add: m_comm)
   603   with ycarr and xy show "\<exists>y\<in>carrier R. y \<otimes> x = \<one> \<and> x \<otimes> y = \<one>" by fast
   604 qed
   605 
   606 
   607 subsection \<open>Morphisms\<close>
   608 
   609 definition
   610   ring_hom :: "[('a, 'm) ring_scheme, ('b, 'n) ring_scheme] => ('a => 'b) set"
   611   where "ring_hom R S =
   612     {h. h \<in> carrier R -> carrier S &
   613       (ALL x y. x \<in> carrier R & y \<in> carrier R -->
   614         h (x \<otimes>\<^bsub>R\<^esub> y) = h x \<otimes>\<^bsub>S\<^esub> h y & h (x \<oplus>\<^bsub>R\<^esub> y) = h x \<oplus>\<^bsub>S\<^esub> h y) &
   615       h \<one>\<^bsub>R\<^esub> = \<one>\<^bsub>S\<^esub>}"
   616 
   617 lemma ring_hom_memI:
   618   fixes R (structure) and S (structure)
   619   assumes hom_closed: "!!x. x \<in> carrier R ==> h x \<in> carrier S"
   620     and hom_mult: "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==>
   621       h (x \<otimes> y) = h x \<otimes>\<^bsub>S\<^esub> h y"
   622     and hom_add: "!!x y. [| x \<in> carrier R; y \<in> carrier R |] ==>
   623       h (x \<oplus> y) = h x \<oplus>\<^bsub>S\<^esub> h y"
   624     and hom_one: "h \<one> = \<one>\<^bsub>S\<^esub>"
   625   shows "h \<in> ring_hom R S"
   626   by (auto simp add: ring_hom_def assms Pi_def)
   627 
   628 lemma ring_hom_closed:
   629   "[| h \<in> ring_hom R S; x \<in> carrier R |] ==> h x \<in> carrier S"
   630   by (auto simp add: ring_hom_def funcset_mem)
   631 
   632 lemma ring_hom_mult:
   633   fixes R (structure) and S (structure)
   634   shows
   635     "[| h \<in> ring_hom R S; x \<in> carrier R; y \<in> carrier R |] ==>
   636     h (x \<otimes> y) = h x \<otimes>\<^bsub>S\<^esub> h y"
   637     by (simp add: ring_hom_def)
   638 
   639 lemma ring_hom_add:
   640   fixes R (structure) and S (structure)
   641   shows
   642     "[| h \<in> ring_hom R S; x \<in> carrier R; y \<in> carrier R |] ==>
   643     h (x \<oplus> y) = h x \<oplus>\<^bsub>S\<^esub> h y"
   644     by (simp add: ring_hom_def)
   645 
   646 lemma ring_hom_one:
   647   fixes R (structure) and S (structure)
   648   shows "h \<in> ring_hom R S ==> h \<one> = \<one>\<^bsub>S\<^esub>"
   649   by (simp add: ring_hom_def)
   650 
   651 locale ring_hom_cring = R: cring R + S: cring S
   652     for R (structure) and S (structure) +
   653   fixes h
   654   assumes homh [simp, intro]: "h \<in> ring_hom R S"
   655   notes hom_closed [simp, intro] = ring_hom_closed [OF homh]
   656     and hom_mult [simp] = ring_hom_mult [OF homh]
   657     and hom_add [simp] = ring_hom_add [OF homh]
   658     and hom_one [simp] = ring_hom_one [OF homh]
   659 
   660 lemma (in ring_hom_cring) hom_zero [simp]:
   661   "h \<zero> = \<zero>\<^bsub>S\<^esub>"
   662 proof -
   663   have "h \<zero> \<oplus>\<^bsub>S\<^esub> h \<zero> = h \<zero> \<oplus>\<^bsub>S\<^esub> \<zero>\<^bsub>S\<^esub>"
   664     by (simp add: hom_add [symmetric] del: hom_add)
   665   then show ?thesis by (simp del: S.r_zero)
   666 qed
   667 
   668 lemma (in ring_hom_cring) hom_a_inv [simp]:
   669   "x \<in> carrier R ==> h (\<ominus> x) = \<ominus>\<^bsub>S\<^esub> h x"
   670 proof -
   671   assume R: "x \<in> carrier R"
   672   then have "h x \<oplus>\<^bsub>S\<^esub> h (\<ominus> x) = h x \<oplus>\<^bsub>S\<^esub> (\<ominus>\<^bsub>S\<^esub> h x)"
   673     by (simp add: hom_add [symmetric] R.r_neg S.r_neg del: hom_add)
   674   with R show ?thesis by simp
   675 qed
   676 
   677 lemma (in ring_hom_cring) hom_finsum [simp]:
   678   "f \<in> A -> carrier R ==>
   679   h (finsum R f A) = finsum S (h o f) A"
   680   by (induct A rule: infinite_finite_induct, auto simp: Pi_def)
   681 
   682 lemma (in ring_hom_cring) hom_finprod:
   683   "f \<in> A -> carrier R ==>
   684   h (finprod R f A) = finprod S (h o f) A"
   685   by (induct A rule: infinite_finite_induct, auto simp: Pi_def)
   686 
   687 declare ring_hom_cring.hom_finprod [simp]
   688 
   689 lemma id_ring_hom [simp]:
   690   "id \<in> ring_hom R R"
   691   by (auto intro!: ring_hom_memI)
   692 
   693 end