Theory Algebra4

(**       Algebra4
                            author Hidetsune Kobayashi
                                   Lingjun Chen (part of Chap 4. section 2,
                                   with revision by H. Kobayashi)
                             Group You Santo
                             Department of Mathematics
                             Nihon University
                             h_coba@math.cst.nihon-u.ac.jp
                             May 3, 2004.
                             April 6, 2007 (revised)

 chapter 3.  Group Theory. Focused on Jordan Hoelder theorem (continued)
     section 20.   abelian groups
     subsection 20-1. Homomorphism of abelian groups
     subsection 20-2  quotient abelian group
   section 21  direct product and direct sum of abelian groups,
               in general case

 chapter 4.  Ring theory
   section 1.  Definition of a ring and an ideal
   section 2.  Calculation of elements
   section 3.  ring homomorphisms
   section 4.  quotient rings
   section 5.  primary ideals, prime ideals
 **)

theory Algebra4
imports Algebra3
begin

section "Abelian groups"

record 'a aGroup = "'a carrier" +
  pop      :: "['a, 'a ]  'a"  (infixl "±ı" 62)
  mop      :: "'a   'a"        ("(-aı _)" [64]63 )
  zero     :: "'a"               ("𝟬ı")

locale aGroup =
  fixes A (structure)
 assumes
         pop_closed: "pop A  carrier A  carrier A  carrier A"
 and     aassoc : "a  carrier A; b  carrier A; c  carrier A 
         (a ± b) ± c = a ± (b ± c)"
 and     pop_commute:"a  carrier A; b  carrier A  a ± b = b ± a"
 and     mop_closed:"mop A  carrier A  carrier A"
 and     l_m :"a  carrier A   (-a a) ± a = 𝟬"
 and     ex_zero: "𝟬  carrier A"
 and     l_zero:"a  carrier A  𝟬 ± a = a"

definition
  b_ag :: "_  
   carrier:: 'a set, top:: ['a, 'a]  'a , iop:: 'a  'a, one:: 'a " where
  "b_ag A = carrier = carrier A, top = pop A, iop = mop A, one = zero A "

definition
  asubGroup :: "[_ , 'a set]  bool" where
  "asubGroup A H  (b_ag A) » H"

definition
  aqgrp :: "[_ , 'a set] 
          carrier::'a set set, pop::['a  set, 'a set]  'a set,
           mop::'a set  'a set, zero :: 'a set " where
  "aqgrp A H = carrier = set_rcs (b_ag A) H,
         pop = λX. λY. (c_top (b_ag A) H X Y),
         mop = λX. (c_iop (b_ag A) H X), zero = H "

definition
  ag_idmap :: "_  ('a  'a)"  ("(aI⇘_)") where
  "aI⇘A= (λxcarrier A. x)"

abbreviation
  ASubG :: "[('a, 'more) aGroup_scheme, 'a set] => bool"   (infixl "+>" 58) where
  "A +> H == asubGroup A H"

definition
  Ag_ind :: "[_ , 'a  'd]  'd aGroup" where
  "Ag_ind A f = carrier = f`(carrier A),
    pop = λx  f`(carrier A). λy  f`(carrier A).
               f(((invfun (carrier A) (f`(carrier A)) f) x) ±A((invfun (carrier A) (f`(carrier A)) f) y)),
    mop = λx(f`(carrier A)). f (-aA((invfun (carrier A) (f`(carrier A)) f) x)),
    zero = f (𝟬A)"

definition
  Agii :: "[_ , 'a  'd]  ('a  'd)" where
  "Agii A f = (λxcarrier A. f x)"   (** Ag_induced_isomorphism **)

lemma (in aGroup) ag_carrier_carrier:"carrier (b_ag A) = carrier A"
by (simp add:b_ag_def)

lemma (in aGroup) ag_pOp_closed:"x  carrier A; y  carrier A 
                                     pop A x y  carrier A"
apply (cut_tac pop_closed)
apply (frule funcset_mem[of ") " "carrier A" "carrier A  carrier A" "x"],
        assumption+)
apply (rule funcset_mem[of ") x" "carrier A" "carrier A" "y"], assumption+)
done

lemma (in aGroup) ag_mOp_closed:"x  carrier A  (-a x)   carrier A"
apply (cut_tac mop_closed)
apply (rule funcset_mem[of "mop A" "carrier A" "carrier A" "x"], assumption+)
done

lemma (in aGroup) asubg_subset:"A +> H  H  carrier A"
apply (simp add:asubGroup_def)
apply (simp add:sg_def, (erule conjE)+)
apply (simp add:ag_carrier_carrier)
done

lemma (in aGroup) ag_pOp_commute:"x  carrier A; y  carrier A  
           pop A x y = pop A y x"
by (simp add:pop_commute)

lemma (in aGroup) b_ag_group:"Group (b_ag A)"
apply (unfold Group_def)
 apply (simp add:b_ag_def)
apply (simp add:pop_closed mop_closed ex_zero)
apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (simp add:aassoc)
apply (rule conjI)
 apply (rule allI, rule impI)
 apply (simp add:l_m)

 apply (rule allI, rule impI)
 apply (simp add:l_zero)
done

lemma (in aGroup) agop_gop:"top (b_ag A) = pop A" (*agpop_gtop*)
 apply (simp add:b_ag_def)
done

lemma (in aGroup) agiop_giop:"iop (b_ag A) = mop A" (*agmop_giop*)
apply (simp add:b_ag_def)
done

lemma (in aGroup) agunit_gone:"one (b_ag A) = 𝟬"
apply (simp add:b_ag_def)
done

lemma (in aGroup) ag_pOp_add_r:"a  carrier A; b  carrier A; c  carrier A;
                 a = b   a ± c =  b ± c"
apply simp
done

lemma (in aGroup) ag_add_commute:"a  carrier A; b  carrier A 
                                                  a ± b = b ± a"
by (simp add:pop_commute)

lemma (in aGroup) ag_pOp_add_l:"a  carrier A; b  carrier A; c  carrier A;
                 a = b   c ± a =  c ± b"
apply simp
done

lemma (in aGroup) asubg_pOp_closed:"asubGroup A H; x  H; y  H
                                    pop A x y  H"
apply (simp add:asubGroup_def)
 apply (cut_tac b_ag_group)
 apply (frule Group.sg_mult_closed [of "b_ag A" "H" "x" "y"], assumption+)
apply (simp only:agop_gop)
done

lemma (in aGroup) asubg_mOp_closed:"asubGroup A H; x  H  -a x  H"
apply (simp add:asubGroup_def)
apply (cut_tac b_ag_group)
apply (frule Group.sg_i_closed[of "b_ag A" "H" "x"], assumption+)
apply (simp add:agiop_giop)
done

lemma (in aGroup) asubg_subset1:"asubGroup A H; x  H  x  carrier A"
apply (simp add:asubGroup_def)
apply (cut_tac b_ag_group)
apply (frule Group.sg_subset_elem[of "b_ag A" "H" "x"], assumption+)
apply (simp add:ag_carrier_carrier)
done

lemma (in aGroup) asubg_inc_zero:"asubGroup A H  𝟬  H"
apply (simp add:asubGroup_def)
apply (cut_tac b_ag_group)
apply (frule Group.sg_unit_closed[of "b_ag A" "H"], assumption)
apply (simp add:b_ag_def)
done

lemma (in aGroup) ag_inc_zero:"𝟬  carrier A"
by (simp add:ex_zero)

lemma (in aGroup) ag_l_zero:"x  carrier A  𝟬 ± x = x"
by (simp add:l_zero)

lemma (in aGroup) ag_r_zero:"x  carrier A  x ± 𝟬 = x"
apply (cut_tac ex_zero)
apply (subst pop_commute, assumption+)
apply (rule ag_l_zero, assumption)
done

lemma (in aGroup) ag_l_inv1:"x  carrier A  (-a x) ± x = 𝟬"
by (simp add:l_m)

lemma (in aGroup) ag_r_inv1:"x  carrier A  x ± (-a x) = 𝟬"
by (frule ag_mOp_closed[of "x"],
       subst ag_pOp_commute, assumption+,
       simp add:ag_l_inv1)

lemma (in aGroup) ag_pOp_assoc:"x  carrier A; y  carrier A; z  carrier A
                 (x ± y) ± z = x ± (y ± z)"
by (simp add:aassoc)

lemma (in aGroup) ag_inv_unique:"x  carrier A; y  carrier A; x ± y = 𝟬 
                                     y = -a x"
apply (frule ag_mOp_closed[of "x"],
       frule aassoc[of "-a x" "x" "y"], assumption+,
       simp add:l_m l_zero ag_r_zero)
done

lemma (in aGroup) ag_inv_inj:"x  carrier A; y  carrier A; x  y 
                                          (-a x)  (-a y)"
apply (rule contrapos_pp, simp+)
apply (frule ag_mOp_closed[of "y"],
       frule aassoc[of "y" "-a y" "x"], assumption+)
apply (simp only:ag_r_inv1,
       frule sym, thin_tac "-a x = -a y", simp add:l_m)
apply (simp add:l_zero ag_r_zero)
done

lemma (in aGroup) pOp_assocTr41:"a  carrier A; b  carrier A; c  carrier A;
 d  carrier A  a ± b ± c ± d = a ± b ± (c ± d)"
by (frule ag_pOp_closed[of "a" "b"], assumption+,
    rule aassoc[of "a ± b" "c" "d"], assumption+)

lemma (in aGroup) pOp_assocTr42:"a  carrier A; b  carrier A;
 c  carrier A; d  carrier A  a ± b ± c ± d = a ± (b ± c) ± d"
by (simp add:aassoc[THEN sym, of "a" "b" "c"])

lemma (in aGroup) pOp_assocTr43:"a  carrier A; b  carrier A;
 c  carrier A; d  carrier A  a ± b ± (c ± d) = a ± (b ± c) ± d"
by (subst  pOp_assocTr41[THEN sym], assumption+,
       rule pOp_assocTr42, assumption+)

lemma (in aGroup) pOp_assoc_cancel:"a  carrier A; b  carrier A;
 c  carrier A  a ± -a b ± (b ± -a c) = a ± -a c"
apply (subst pOp_assocTr43, assumption)
apply (simp add:ag_l_inv1 ag_mOp_closed)+
apply (simp add:ag_r_zero)
done

lemma (in aGroup) ag_p_inv:"x  carrier A; y  carrier A 
                                     (-a (x ± y)) = (-a x) ± (-a y)"
apply (frule ag_mOp_closed[of "x"], frule ag_mOp_closed[of "y"],
       frule ag_pOp_closed[of "x" "y"], assumption+)
apply (frule aassoc[of "x ± y" "-a x" "-a y"], assumption+,
       simp add:pOp_assocTr43, simp add:pop_commute[of "y" "-a x"],
       simp add:aassoc[THEN sym, of "x" "-a x" "y"],
       simp add:ag_r_inv1 l_zero)
apply (frule ag_pOp_closed[of "-a x" "-a y"], assumption+,
       simp add:pOp_assocTr41,
       rule ag_inv_unique[THEN sym, of "x ± y" "-a x ± -a y"], assumption+)
done

lemma (in aGroup) gEQAddcross: "l1  carrier A; l2  carrier A;
      r1  carrier A; r1  carrier A; l1 = r2; l2 = r1 
                          l1 ± l2 = r1 ± r2"
  apply (simp add:ag_pOp_commute)
  done

lemma (in aGroup) ag_eq_sol1:"a  carrier A; x carrier A; b carrier A;
                               a ± x = b  x = (-a a) ± b"
apply (frule ag_mOp_closed[of "a"])
apply (frule aassoc[of "-a a" "a" "x"], assumption+)
apply (simp add:l_m l_zero)
done

lemma (in aGroup) ag_eq_sol2:"a  carrier A; x carrier A; b carrier A;
                                x ± a = b  x = b ± (-a a)"
apply (frule ag_mOp_closed[of "a"],
       frule aassoc[of "x" "a" "-a a"], assumption+,
       simp add:ag_r_inv1 ag_r_zero)
done

lemma (in aGroup) ag_add4_rel:"a  carrier A; b  carrier A; c  carrier A;
 d  carrier A   a ± b ± (c ± d) =  a ± c ± (b ± d)"
apply (simp add:pOp_assocTr43[of "a" "b" "c" "d"],
       simp add:ag_pOp_commute[of "b" "c"],
       simp add:pOp_assocTr43[THEN sym, of "a" "c" "b" "d"])
done

lemma (in aGroup) ag_inv_inv:"x  carrier A  -a (-a x) = x"
by (frule ag_l_inv1[of "x"], frule ag_mOp_closed[of "x"],
       rule  ag_inv_unique[THEN sym, of "-a x" "x"], assumption+)

lemma (in aGroup) ag_inv_zero:"-a 𝟬 = 𝟬"
apply (cut_tac ex_zero)
apply (frule l_zero[of "𝟬"])
apply (rule ag_inv_unique[THEN sym], assumption+)
done

lemma (in aGroup) ag_diff_minus:"a  carrier A; b  carrier A; c  carrier A;
                   a ± (-a b) = c  b ± (-a a) = (-a c)"
apply (frule sym, thin_tac "a ± -a b = c", simp, thin_tac "c = a ± -a b")
apply (frule ag_mOp_closed[of "b"], frule ag_mOp_closed[of "a"],
       subst ag_p_inv, assumption+, subst ag_inv_inv, assumption)
apply (simp add:ag_pOp_commute)
done

lemma (in aGroup) pOp_cancel_l:"a  carrier A; b  carrier A; c  carrier A;                    c ± a =  c ± b   a = b"
apply (frule ag_mOp_closed[of "c"],
       frule aassoc[of "-a c" "c" "a"], assumption+,
       simp only:l_m l_zero)
apply (simp only:aassoc[THEN sym, of "-a c" "c" "b"],
        simp only:l_m l_zero)
done

lemma (in aGroup) pOp_cancel_r:"a  carrier A; b  carrier A; c  carrier A;               a ± c =  b ± c   a = b"
by (simp add:ag_pOp_commute pOp_cancel_l)

lemma (in aGroup) ag_eq_diffzero:"a  carrier A; b  carrier A 
                       (a = b) = (a ± (-a b) = 𝟬)"
apply (rule iffI)
 apply (simp add:ag_r_inv1)
 apply (frule ag_mOp_closed[of "b"])
 apply (simp add:ag_pOp_commute[of "a" "-a b"])
 apply (subst ag_inv_unique[of "-a b" "a"], assumption+,
        simp add:ag_inv_inv)
done

lemma (in aGroup) ag_eq_diffzero1:"a  carrier A; b  carrier A 
                       (a = b) = ((-a a) ± b = 𝟬)"
apply (frule ag_mOp_closed[of a],
       simp add:ag_pOp_commute)
apply (subst ag_eq_diffzero[THEN sym], assumption+)
apply (rule iffI, rule sym, assumption)
apply (rule sym, assumption)
done

lemma (in aGroup) ag_neq_diffnonzero:"a  carrier A; b  carrier A 
         (a  b) = (a ± (-a b)   𝟬)"
apply (rule iffI)
 apply (rule contrapos_pp, simp+)
 apply (simp add:ag_eq_diffzero[THEN sym])
apply (rule contrapos_pp, simp+)
 apply (simp add:ag_r_inv1)
done

lemma (in aGroup) ag_plus_zero:"x  carrier A; y  carrier A 
                     (x = -a y)  = (x ± y = 𝟬)"
apply (rule iffI)
 apply (simp add:ag_l_inv1)
apply (simp add:ag_pOp_commute[of "x" "y"])
apply (rule ag_inv_unique[of "y" "x"], assumption+)
done

lemma (in aGroup) asubg_nsubg:"A +> H   (b_ag A)  H"
apply (cut_tac b_ag_group)
apply (simp add:asubGroup_def)
apply (rule Group.cond_nsg[of "b_ag A" "H"], assumption+)
apply (rule ballI)+
apply(simp add:agop_gop agiop_giop)
 apply (frule Group.sg_subset[of "b_ag A" "H"], assumption)
 apply (simp add:ag_carrier_carrier)
apply (frule_tac c = h in subsetD[of "H" "carrier A"], assumption+)
 apply (subst ag_pOp_commute, assumption+)
 apply (frule_tac x = a in ag_mOp_closed)
 apply (subst aassoc, assumption+, simp add:ag_r_inv1 ag_r_zero)
done

lemma (in aGroup) subg_asubg:"b_ag G » H  G +> H"
apply (simp add:asubGroup_def)
done

lemma (in aGroup) asubg_test:"H  carrier A; H  {};
               aH. bH. (a ± (-a b)  H)  A +> H"
apply (simp add:asubGroup_def) apply (cut_tac b_ag_group)
apply (rule Group.sg_condition [of "b_ag A" "H"], assumption+)
 apply (simp add:ag_carrier_carrier) apply assumption
apply (rule allI)+ apply (rule impI)
apply (simp add:agop_gop agiop_giop)
done

lemma (in aGroup) asubg_zero:"A +> {𝟬}"
apply (rule asubg_test[of "{𝟬}"])
 apply (simp add:ag_inc_zero)
 apply simp
 apply (simp, cut_tac ag_inc_zero, simp add:ag_r_inv1)
done

lemma (in aGroup) asubg_whole:"A +> carrier A"
apply (rule asubg_test[of "carrier A"])
apply (simp,
       cut_tac ag_inc_zero, simp add:nonempty)
apply ((rule ballI)+,
       rule ag_pOp_closed, assumption,
       rule_tac x = b in ag_mOp_closed, assumption)
done

lemma (in aGroup) Ag_ind_carrier:"bij_to f (carrier A) (D::'d set) 
               carrier (Ag_ind A f) = f ` (carrier A)"
by (simp add:Ag_ind_def)

lemma (in aGroup) Ag_ind_aGroup:"f  carrier A  D;
      bij_to f (carrier A) (D::'d set)  aGroup (Ag_ind A f)"
apply (simp add:bij_to_def, frule conjunct1, frule conjunct2, fold bij_to_def)
apply (simp add:aGroup_def)
 apply (rule conjI)
 apply (rule Pi_I)+
 apply (simp add:Ag_ind_carrier surj_to_def)
 apply (frule_tac b = x in invfun_mem1[of "f" "carrier A" "D"], assumption+,
        frule_tac b = xa in invfun_mem1[of "f" "carrier A" "D"], assumption+)
apply (simp add:Ag_ind_def)
 apply (rule funcset_mem[of "f" "carrier A" "D"], assumption)
 apply (simp add:ag_pOp_closed)

 apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (simp add: Ag_ind_carrier surj_to_def)
 apply (frule_tac b = a in invfun_mem1[of "f" "carrier A" "D"], assumption+,
        frule_tac b = b in invfun_mem1[of "f" "carrier A" "D"], assumption+,
        frule_tac b = c in invfun_mem1[of "f" "carrier A" "D"], assumption+)
 apply (simp add:Ag_ind_def)
 apply (frule_tac x = "invfun (carrier A) D f a" and
                  y = "invfun (carrier A) D f b" in ag_pOp_closed, assumption+,
        frule_tac x = "invfun (carrier A) D f b" and
                  y = "invfun (carrier A) D f c" in ag_pOp_closed, assumption+)
 apply (simp add:Pi_def)
 apply (unfold bij_to_def, frule conjunct1, fold bij_to_def)
 apply (simp add:invfun_l)
 apply (subst injective_iff[of "f" "carrier A", THEN sym], assumption)
 apply (simp add:ag_pOp_closed)+
 apply (simp add:ag_pOp_assoc)

apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (simp add:Ag_ind_def)
 apply (subst injective_iff[of "f" "carrier A", THEN sym], assumption)
 apply (frule_tac b = a in invfun_mem1[of "f" "carrier A" "D"], assumption+,
        frule_tac b = b in invfun_mem1[of "f" "carrier A" "D"], assumption+)
       apply (simp add:surj_to_def) apply (simp add:surj_to_def)

 apply (simp add:surj_to_def)
 apply (frule_tac b = b in invfun_mem1[of "f" "carrier A" "D"], assumption+)
 apply (simp add:ag_pOp_closed)

 apply (simp add:surj_to_def)
 apply (frule_tac b = a in invfun_mem1[of "f" "carrier A" "D"], assumption+,
        frule_tac b = b in invfun_mem1[of "f" "carrier A" "D"], assumption+)
       apply (simp add:ag_pOp_closed)

 apply (simp add:surj_to_def)
 apply (frule_tac b = a in invfun_mem1[of "f" "carrier A" "D"], assumption+,
        frule_tac b = b in invfun_mem1[of "f" "carrier A" "D"], assumption+)
       apply (simp add:ag_pOp_commute)

apply (rule conjI)
 apply (rule Pi_I)
 apply (simp add:Ag_ind_def surj_to_def)
 apply (rule funcset_mem[of "f" "carrier A" "D"], assumption)
 apply (frule_tac b = x in invfun_mem1[of "f" "carrier A" "D"], assumption+)
 apply (simp add:ag_mOp_closed)

apply (rule conjI)
 apply (rule allI, rule impI)
 apply (simp add:Ag_ind_def surj_to_def)
 apply (frule_tac b = a in invfun_mem1[of "f" "carrier A" "D"], assumption+)

 apply (frule_tac x = "invfun (carrier A) D f a" in ag_mOp_closed)
 apply (simp add:Pi_def)
 apply (subst injective_iff[of "f" "carrier A", THEN sym], assumption)
 apply (unfold bij_to_def, frule conjunct1, fold bij_to_def)
 apply (simp add:invfun_l)
 apply (simp add:ag_pOp_closed)
 apply (simp add:ag_inc_zero)
 apply (unfold bij_to_def, frule conjunct1, fold bij_to_def)
 apply (simp add:invfun_l l_m)

apply (rule conjI)
 apply (simp add:Ag_ind_def surj_to_def)
 apply (rule funcset_mem[of "f" "carrier A" "D"], assumption)
 apply (simp add:ag_inc_zero)

apply (rule allI, rule impI)
  apply (simp add:Ag_ind_def surj_to_def)
  apply (cut_tac ag_inc_zero, simp add:funcset_mem del:Pi_I)
  apply (unfold bij_to_def, frule conjunct1, fold bij_to_def)
  apply (simp add:invfun_l)
 apply (frule_tac b = a in invfun_mem1[of "f" "carrier A" "D"], assumption+)
 apply (simp add:l_zero)
 apply (simp add:invfun_r)
done

subsection "Homomorphism of abelian groups"

definition
  aHom :: "[('a, 'm) aGroup_scheme, ('b, 'm1) aGroup_scheme]  ('a  'b) set" where
  "aHom A B = {f. f  carrier A  carrier B  f  extensional (carrier A) 
               (acarrier A. bcarrier A. f (a ±Ab) = (f a) ±B(f b))}"

definition
  compos :: "[('a, 'm) aGroup_scheme, 'b  'c, 'a  'b]  'a  'c" where
  "compos A g f = compose (carrier A) g f"

definition
  ker :: "[('a, 'm) aGroup_scheme, ('b, 'm1) aGroup_scheme]  ('a  'b)
         'a set" ("(3ker⇘_,_ _)" [82,82,83]82) where
  "ker⇘F,Gf = {a. a  carrier F  f a = (𝟬G)}"

definition
 injec :: "[('a, 'm) aGroup_scheme, ('b, 'm1) aGroup_scheme, 'a  'b]
             bool"             ("(3injec⇘_,_ _)" [82,82,83]82) where
  "injec⇘F,Gf  f  aHom F G  ker⇘F,Gf = {𝟬F}"

definition
  surjec :: "[('a, 'm) aGroup_scheme, ('b, 'm1) aGroup_scheme, 'a  'b]
             bool"             ("(3surjec⇘_,_ _)" [82,82,83]82) where
  "surjec⇘F,Gf  f  aHom F G  surj_to f (carrier F) (carrier G)"

definition
  bijec :: "[('a, 'm) aGroup_scheme, ('b, 'm1) aGroup_scheme, 'a  'b]
             bool"             ("(3bijec⇘_,_ _)" [82,82,83]82) where
  "bijec⇘F,Gf  injec⇘F,Gf  surjec⇘F,Gf"

definition
  ainvf :: "[('a, 'm) aGroup_scheme, ('b, 'm1) aGroup_scheme, 'a  'b]
             ('b  'a)"             ("(3ainvf⇘_,_ _)" [82,82,83]82) where
  "ainvf⇘F,Gf = invfun (carrier F) (carrier G) f"

lemma aHom_mem:"aGroup F; aGroup G; f  aHom F G; a  carrier F 
                       f a  carrier G"
apply (simp add:aHom_def) apply (erule conjE)+
apply (simp add:Pi_def)
done

lemma aHom_func:"f  aHom F G  f  carrier F  carrier G"
by (simp add:aHom_def)

lemma aHom_add:"aGroup F; aGroup G; f  aHom F G; a  carrier F;
 b  carrier F  f (a ±Fb) = (f a) ±G(f b)"
apply (simp add:aHom_def)
done

lemma aHom_0_0:"aGroup F; aGroup G; f  aHom F G  f (𝟬F) = 𝟬G⇙"
apply (frule aGroup.ag_inc_zero [of "F"])
apply (subst aGroup.ag_l_zero [THEN sym, of "F" "𝟬F⇙"], assumption+)
apply (simp add:aHom_add)
apply (frule aGroup.ag_l_zero [THEN sym, of "F" "𝟬F⇙"], assumption+)
apply (subgoal_tac "f (𝟬F) = f (𝟬F±F𝟬F)") prefer 2 apply simp
apply (thin_tac "𝟬F= 𝟬F±F𝟬F⇙")
apply (simp add:aHom_add) apply (frule sym)
apply (thin_tac "f 𝟬F= f 𝟬F±Gf 𝟬F⇙")
apply (frule aHom_mem[of "F" "G" "f" "𝟬F⇙"], assumption+)
apply (frule aGroup.ag_mOp_closed[of "G" "f 𝟬F⇙"], assumption+)
apply (frule aGroup.aassoc[of "G" "-aG(f 𝟬F)" "f 𝟬F⇙" "f 𝟬F⇙"], assumption+)
apply (simp add:aGroup.l_m aGroup.l_zero)
done

lemma ker_inc_zero:"aGroup F; aGroup G; f  aHom F G  𝟬F ker⇘F,Gf"
by (frule aHom_0_0[of "F" "G" "f"], assumption+,
       simp add:ker_def, simp add:aGroup.ag_inc_zero [of "F"])

lemma aHom_inv_inv:"aGroup F; aGroup G; f  aHom F G; a  carrier F 
                         f (-aFa) = -aG(f a)"
apply (frule aGroup.ag_l_inv1 [of "F" "a"], assumption+,
       frule sym, thin_tac "-aFa ±Fa = 𝟬F⇙",
       frule aHom_0_0[of "F" "G" "f"], assumption+,
       frule aGroup.ag_mOp_closed[of "F" "a"], assumption+)
 apply (simp add:aHom_add, thin_tac "𝟬F= -aFa ±Fa")
 apply (frule aHom_mem[of "F" "G" "f" "-aFa"], assumption+,
        frule aHom_mem[of "F" "G" "f" "a"], assumption+,
        simp only:aGroup.ag_pOp_commute[of "G" "f (-aFa)" "f a"])
 apply (rule aGroup.ag_inv_unique[of "G"], assumption+)
done

lemma aHom_compos:"aGroup L; aGroup M; aGroup N; f  aHom L M; g  aHom M N 
   compos L g f  aHom L N"
apply (simp add:aHom_def [of "L" "N"])
apply (rule conjI)
 apply (rule Pi_I)
 apply (simp add:compos_def compose_def)
 apply (rule aHom_mem [of "M" "N" "g"], assumption+)
 apply (simp add:aHom_mem [of "L" "M" "f"])
apply (rule conjI)
 apply (simp add:compos_def compose_def extensional_def)
apply (rule ballI)+
 apply (simp add:compos_def compose_def)
 apply (simp add:aGroup.ag_pOp_closed)
 apply (simp add:aHom_add)
 apply (rule aHom_add, assumption+)
 apply (simp add:aHom_mem)+
done

lemma aHom_compos_assoc:"aGroup K; aGroup L; aGroup M; aGroup N; f  aHom K L;
      g  aHom L M; h  aHom M N   
      compos K h (compos K g f) = compos K (compos L h g) f"
apply (simp add:compos_def compose_def)
apply (rule funcset_eq[of _ "carrier K"])
apply (simp add:restrict_def extensional_def)
apply (simp add:restrict_def extensional_def)
apply (rule ballI, simp)
apply (simp add:aHom_mem)
done

lemma injec_inj_on:"aGroup F; aGroup G; injec⇘F,Gf  inj_on f (carrier F)"
apply (simp add:inj_on_def)
 apply (rule ballI)+ apply (rule impI)
 apply (simp add:injec_def, erule conjE)
 apply (frule_tac a = x in aHom_mem[of "F" "G" "f"], assumption+,
        frule_tac a = x in aHom_mem[of "F" "G" "f"], assumption+)
 apply (frule_tac x = "f x" in aGroup.ag_r_inv1[of "G"], assumption+)
 apply (simp only:aHom_inv_inv[THEN sym, of "F" "G" "f"])
 apply (frule sym, thin_tac "f x = f y", simp)
 apply (frule_tac x = y in aGroup.ag_mOp_closed[of "F"], assumption+)
 apply (simp add:aHom_add[THEN sym], simp add:ker_def)
 apply (subgoal_tac "x ±F-aFy  {a  carrier F. f a = 𝟬G}",
        simp)
 apply (subst aGroup.ag_eq_diffzero[of "F"], assumption+)
apply (frule_tac x = x and y = "-aFy" in aGroup.ag_pOp_closed[of "F"],
           assumption+)
 apply simp apply blast
done

lemma surjec_surj_to:"surjec⇘R,Sf  surj_to f (carrier R) (carrier S)"
by (simp add:surjec_def)

lemma compos_bijec:"aGroup E; aGroup F; aGroup G; bijec⇘E,Ff; bijec⇘F,Gg 
                     bijec⇘E,G(compos E g f)"
apply (simp add:bijec_def, (erule conjE)+)
apply (rule conjI)
 apply (simp add:injec_def, (erule conjE)+)
 apply (simp add:aHom_compos[of "E" "F" "G" "f" "g"])
 apply (rule equalityI, rule subsetI, simp add:ker_def, erule conjE)
 apply (simp add:compos_def compose_def)
 apply (frule_tac a = x in aHom_mem[of "E" "F" "f"], assumption+)
 apply (subgoal_tac "(f x)  {a  carrier F. g a = 𝟬G}", simp)
 apply (subgoal_tac "x  {a  carrier E. f a = 𝟬F}", simp)
 apply blast apply blast
 apply (rule subsetI, simp)
 apply (simp add:ker_def compos_def compose_def)
 apply (simp add:aGroup.ag_inc_zero) apply (simp add:aHom_0_0)

apply (simp add:surjec_def, (erule conjE)+)
 apply (simp add:aHom_compos)
 apply (simp add:aHom_def, (erule conjE)+) apply (simp add:compos_def)
 apply (rule compose_surj[of "f" "carrier E" "carrier F" "g" "carrier G"],
            assumption+)
done

lemma ainvf_aHom:"aGroup F; aGroup G; bijec⇘F,Gf 
                      ainvf⇘F,Gf  aHom G F"
apply (subst aHom_def, simp)
 apply (simp add:ainvf_def)
 apply (simp add:bijec_def, erule conjE)
 apply (frule injec_inj_on[of "F" "G" "f"], assumption+)
 apply (simp add:surjec_def, (erule conjE)+)
 apply (simp add:aHom_def, (erule conjE)+)
 apply (frule inv_func[of "f" "carrier F" "carrier G"], assumption+, simp)
apply (rule conjI)
 apply (simp add:invfun_def)
apply (rule ballI)+
 apply (frule_tac x = a in funcset_mem[of "Ifn F G f" "carrier G" "carrier F"],
      assumption+,
      frule_tac x = b in funcset_mem[of "Ifn F G f" "carrier G" "carrier F"],
      assumption+,
      frule_tac x = a and y = b in aGroup.ag_pOp_closed[of "G"], assumption+,
      frule_tac x = "a ±Gb" in funcset_mem[of "Ifn F G f" "carrier G"
       "carrier F"], assumption+)
 apply (frule_tac a = "(Ifn F G f) a" and b = "(Ifn F G f) b" in
           aHom_add[of "F" "G" "f"], assumption+, simp add:injec_def,
           assumption+,
           thin_tac "acarrier F. bcarrier F. f (a ±Fb) = f a ±Gf b")
 apply (simp add:invfun_r[of "f" "carrier F" "carrier G"])
 apply (frule_tac x = a and y = b in aGroup.ag_pOp_closed[of "G"], assumption+) apply (frule_tac b = "a ±Gb" in invfun_r[of "f" "carrier F" "carrier G"],
           assumption+)
 apply (simp add:inj_on_def)
 apply (frule_tac x = "(Ifn F G f) a" and y = "(Ifn F G f) b" in
          aGroup.ag_pOp_closed, assumption+)
 apply (frule_tac x = "(Ifn F G f) (a ±Gb)" in bspec, assumption,
        thin_tac "xcarrier F. ycarrier F. f x = f y  x = y")
 apply (frule_tac x = "(Ifn F G f) a ±F(Ifn F G f) b" in bspec,
            assumption,
        thin_tac "ycarrier F.
              f ((Ifn F G f) (a ±Gb)) = f y  (Ifn F G f) (a ±Gb) = y")
 apply simp
done

lemma ainvf_bijec:"aGroup F; aGroup G; bijec⇘F,Gf  bijec⇘G,F(ainvf⇘F,Gf)"
apply (subst bijec_def)
apply (simp add:injec_def surjec_def)
apply (simp add:ainvf_aHom)
apply (rule conjI)
 apply (rule equalityI)
 apply (rule subsetI, simp add:ker_def, erule conjE)
 apply (simp add:ainvf_def)
 apply (simp add:bijec_def,(erule conjE)+, simp add:surjec_def,
         (erule conjE)+, simp add:aHom_def, (erule conjE)+)
 apply (frule injec_inj_on[of "F" "G" "f"], assumption+)
 apply (subst invfun_r[THEN sym, of "f" "carrier F" "carrier G"], assumption+)
 apply (simp add:injec_def, (erule conjE)+, simp add:aHom_0_0)

 apply (rule subsetI, simp add:ker_def)
 apply (simp add:aGroup.ex_zero)
 apply (frule ainvf_aHom[of "F" "G" "f"], assumption+)
 apply (simp add:aHom_0_0)

apply (frule ainvf_aHom[of "F" "G" "f"], assumption+,
        simp add:aHom_def, (erule conjE)+,
       rule surj_to_test[of "ainvf⇘F,Gf" "carrier G" "carrier F"],
        assumption+)
 apply (rule ballI,
        thin_tac "acarrier G. bcarrier G.
               (ainvf⇘F,Gf) (a ±Gb) = (ainvf⇘F,Gf) a ±F(ainvf⇘F,Gf) b")
 apply (simp add:bijec_def, erule conjE)
  apply (frule injec_inj_on[of "F" "G" "f"], assumption+)
  apply (simp add:surjec_def aHom_def, (erule conjE)+)
  apply (subst ainvf_def)
 apply (frule_tac a = b in invfun_l[of "f" "carrier F" "carrier G"],
                  assumption+,
        frule_tac x = b in funcset_mem[of "f" "carrier F" "carrier G"],
                  assumption+, blast)
done

lemma ainvf_l:"aGroup E; aGroup F; bijec⇘E,Ff; x  carrier E 
                      (ainvf⇘E,Ff) (f x) = x"
apply (simp add:bijec_def, erule conjE)
apply (frule injec_inj_on[of "E" "F" "f"], assumption+)
apply (simp add:surjec_def aHom_def, (erule conjE)+)
apply (frule invfun_l[of "f" "carrier E" "carrier F" "x"], assumption+)
apply (simp add:ainvf_def)
done

lemma (in aGroup) aI_aHom:"aI⇘A aHom A A"
by (simp add:aHom_def ag_idmap_def ag_idmap_def ag_pOp_closed)

lemma compos_aI_l:"aGroup A; aGroup B; f  aHom A B  compos A aI⇘Bf = f"
apply (simp add:compos_def)
apply (rule funcset_eq[of _ "carrier A"])
 apply (simp add:compose_def extensional_def)
 apply (simp add:aHom_def)
apply (rule ballI)
 apply (frule_tac a = x in aHom_mem[of "A" "B" "f"], assumption+)
 apply (simp add:compose_def ag_idmap_def)
done

lemma compos_aI_r:"aGroup A; aGroup B; f  aHom A B  compos A f aI⇘A= f"
apply (simp add:compos_def)
apply (rule funcset_eq[of _ "carrier A"])
 apply (simp add:compose_def extensional_def)
 apply (simp add:aHom_def)
apply (rule ballI)
 apply (simp add:compose_def ag_idmap_def)
done

lemma compos_aI_surj:"aGroup A; aGroup B; f  aHom A B; g  aHom B A;
                      compos A g f = aI⇘A  surjec⇘B,Ag"
apply (simp add:surjec_def)
apply (rule surj_to_test[of "g" "carrier B" "carrier A"])
 apply (simp add:aHom_def)
apply (rule ballI)
 apply (subgoal_tac "compos A g f b = aI⇘Ab",
        thin_tac "compos A g f = aI⇘A⇙")
 apply (simp add:compos_def compose_def ag_idmap_def)
 apply (frule_tac a = b in aHom_mem[of "A" "B" "f"], assumption+, blast)
 apply simp
done

lemma compos_aI_inj:"aGroup A; aGroup B; f  aHom A B; g  aHom B A;
                      compos A g f = aI⇘A  injec⇘A,Bf"
apply (simp add:injec_def)
apply (simp add:ker_def)
apply (rule equalityI)
 apply (rule subsetI, simp, erule conjE)
 apply (subgoal_tac "compos A g f x = aI⇘Ax",
        thin_tac "compos A g f = aI⇘A⇙")
 apply (simp add:compos_def compose_def)
 apply (simp add:aHom_0_0 ag_idmap_def) apply simp

 apply (rule subsetI, simp)
 apply (simp add:aGroup.ag_inc_zero aHom_0_0)
done

lemma (in aGroup) Ag_ind_aHom:"f  carrier A  D;
      bij_to f (carrier A) (D::'d set)  Agii A f  aHom A (Ag_ind A f)"
apply (simp add:aHom_def)
 apply (unfold bij_to_def, frule conjunct1, frule conjunct2, fold bij_to_def)
 apply (simp add:Ag_ind_carrier surj_to_def)
apply (rule conjI)
 apply (simp add:Agii_def Pi_def)
 apply (simp add:Agii_def)
 apply (simp add:Ag_ind_def Pi_def)
 apply (unfold bij_to_def, frule conjunct1, fold bij_to_def)
 apply (simp add:invfun_l)
 apply (simp add:ag_pOp_closed)
done

lemma (in aGroup) Agii_mem:"f  carrier A  D; x  carrier A;
      bij_to f (carrier A) (D::'d set)  Agii A f x  carrier (Ag_ind A f)"
apply (simp add:Agii_def Ag_ind_carrier)
done

lemma Ag_ind_bijec:"aGroup A; f  carrier A  D;
      bij_to f (carrier A) (D::'d set)  bijec⇘A, (Ag_ind A f)(Agii A f)"
apply (frule aGroup.Ag_ind_aHom[of "A" "f" "D"], assumption+)
apply (frule aGroup.Ag_ind_aGroup[of "A" "f" "D"], assumption+)
apply (simp add:bijec_def)
 apply (rule conjI)
 apply (simp add:injec_def)
 apply (rule equalityI)
 apply (rule subsetI)
 apply (simp add:ker_def, erule conjE)
apply (frule aHom_0_0[of "A" "Ag_ind A f" "Agii A f"], assumption+)
 apply (rotate_tac -2, frule sym, thin_tac "Agii A f x = 𝟬Ag_ind A f⇙", simp)
 apply (frule aGroup.ag_inc_zero[of "A"], simp add:Agii_def)
 apply (unfold bij_to_def, frule conjunct2, fold bij_to_def)
 apply (frule aGroup.ag_inc_zero[of "A"])
 apply (simp add:injective_iff[THEN sym, of "f" "carrier A" "𝟬A⇙"])
 apply (rule subsetI, simp)
 apply (subst ker_def, simp)
 apply (simp add:aGroup.ag_inc_zero, simp add:aHom_0_0)

apply (subst surjec_def)
apply (unfold bij_to_def, frule conjunct1, fold bij_to_def, simp)
 apply (simp add:aGroup.Ag_ind_carrier surj_to_def Agii_def)
done

definition
  aimg :: "[('b, 'm1) aGroup_scheme, _, 'b  'a]
             'a aGroup"  ("(3aimg⇘_,_ _)" [82,82,83]82) where
  "aimg⇘F,Af = A  carrier := f ` (carrier F), pop := pop A, mop := mop A,
                  zero := zero A"

lemma ker_subg:"aGroup F; aGroup G; f  aHom F G   F +> ker⇘F,Gf"
apply (rule aGroup.asubg_test, assumption+)
apply (rule subsetI)
 apply (simp add:ker_def)
apply (simp add:ker_def)
apply (frule aHom_0_0 [of "F" "G" "f"], assumption+)
apply (frule aGroup.ex_zero [of "F"]) apply blast
apply (rule ballI)+
apply (simp add:ker_def) apply (erule conjE)+
apply (frule_tac x = b in aGroup.ag_mOp_closed[of "F"], assumption+)
apply (rule conjI)
apply (rule aGroup.ag_pOp_closed, assumption+)
apply (simp add:aHom_add)
apply (simp add:aHom_inv_inv)
apply (simp add:aGroup.ag_inv_zero[of "G"])
apply (cut_tac aGroup.ex_zero[of "G"], simp add:aGroup.l_zero)
apply assumption
done

subsection "Quotient abelian group"

definition
  ar_coset :: "['a, _ , 'a set]  'a set" (** a_rcs **)
     ("(3_ ⊎⇘_ _)" [66,66,67]66) where
  "ar_coset a A H = H (b_ag A)a"

definition
  set_ar_cos :: "[_ , 'a set]  'a set set" where
  "set_ar_cos A I = {X. acarrier A. X = ar_coset a A I}"

definition
  aset_sum :: "[_ , 'a set, 'a set]  'a set" where
  "aset_sum A H K = s_top (b_ag A) H K"

abbreviation
  ASBOP1  (infix "ı" 60) where
  "H AK == aset_sum A H K"

lemma (in aGroup) ag_a_in_ar_cos:"A +> H; a  carrier A  a  a ⊎⇘AH"
apply (simp add:ar_coset_def)
apply (simp add:asubGroup_def)
apply (cut_tac b_ag_group)
apply (rule Group.a_in_rcs[of "b_ag A" "H" "a"], assumption+)
apply (simp add:ag_carrier_carrier[THEN sym])
done

lemma (in aGroup) r_cos_subset:"A +> H; X  set_rcs (b_ag A) H 
                   X  carrier A"
apply (simp add:asubGroup_def set_rcs_def)
apply (erule bexE)
apply (cut_tac  b_ag_group)
apply (frule_tac a = a in Group.rcs_subset[of "b_ag A" "H"], assumption+)
apply (simp add:ag_carrier_carrier)
done

lemma (in aGroup) asubg_costOp_commute:"A +> H; x  set_rcs (b_ag A) H;
       y  set_rcs (b_ag A) H 
             c_top (b_ag A) H x y = c_top (b_ag A) H y x"
apply (simp add:set_rcs_def, (erule bexE)+, simp)
apply (cut_tac b_ag_group)
apply (subst Group.c_top_welldef[THEN sym], assumption+,
       simp add:asubg_nsubg,
       (simp add:ag_carrier_carrier)+)
apply (subst Group.c_top_welldef[THEN sym], assumption+,
       simp add:asubg_nsubg,
       (simp add:ag_carrier_carrier)+)
apply (simp add:agop_gop)
 apply (simp add:ag_pOp_commute)
done

lemma (in aGroup) Subg_Qgroup:"A +> H  aGroup (aqgrp A H)"
apply (frule asubg_nsubg[of "H"])
apply (cut_tac b_ag_group)
apply (simp add:aGroup_def)
 apply (simp add:aqgrp_def)
 apply (simp add:Group.Qg_top [of "b_ag A" "H"])
 apply (simp add:Group.Qg_iop [of "b_ag A" "H"])
 apply (frule Group.nsg_sg[of "b_ag A" "H"], assumption+,
        simp add:Group.unit_rcs_in_set_rcs[of "b_ag A" "H"])
apply (simp add:Group.Qg_tassoc)
apply (simp add:asubg_costOp_commute)
apply (simp add:Group.Qg_i[of "b_ag A" "H"])
apply (simp add:Group.Qg_unit[of "b_ag A" "H"])
done

lemma (in aGroup) plus_subgs:"A +> H1; A +> H2  A +> H1  H2"
apply (simp add:aset_sum_def)
 apply (frule asubg_nsubg[of "H2"])
 apply (simp add:asubGroup_def[of _ "H1"])
apply (cut_tac "b_ag_group")
apply (frule Group.smult_sg_nsg[of "b_ag A" "H1" "H2"], assumption+)
apply (simp add:asubGroup_def)
done

lemma (in aGroup) set_sum:"H  carrier A; K  carrier A 
                    H  K = {x. hH. kK. x = h ± k}"
 apply (cut_tac b_ag_group)
apply (rule equalityI)
 apply (rule subsetI)
 apply (simp add:aset_sum_def)
 apply (simp add:agop_gop[THEN sym] s_top_def, (erule bexE)+,
        frule sym, thin_tac "xa b_ag Ay = x", simp, blast)
 apply (rule subsetI, simp add:aset_sum_def, (erule bexE)+)
 apply (frule_tac c = h in subsetD[of H "carrier A"], assumption+,
        frule_tac c = k in subsetD[of K "carrier A"], assumption+)
 apply (simp add:agop_gop[THEN sym], simp add:s_top_def, blast)
done

lemma (in aGroup) mem_set_sum:"H  carrier A; K  carrier A;
                  x  H  K   hH. kK. x = h ± k"
by (simp add:set_sum)

lemma (in aGroup) mem_sum_subgs:"A +> H; A +> K; h  H; k  K 
                    h ± k  H  K"
apply (frule asubg_subset[of H],
       frule asubg_subset[of K],
       simp add:set_sum, blast)
done

lemma (in aGroup) aqgrp_carrier:"A +> H 
                   set_rcs (b_ag A ) H = set_ar_cos A H"
apply (simp add:set_ar_cos_def)
apply (simp add:ag_carrier_carrier [THEN sym])
apply (simp add:ar_coset_def set_rcs_def)
done

lemma (in aGroup) unit_in_set_ar_cos:"A +> H  H  set_ar_cos A H"
apply (simp add:aqgrp_carrier[THEN sym])
apply (cut_tac b_ag_group) apply (simp add:asubGroup_def)
apply (simp add:Group.unit_rcs_in_set_rcs[of "b_ag A" "H"])
done

lemma (in aGroup) aqgrp_pOp_maps:"A +> H; a  carrier A; b  carrier A 
      pop (aqgrp A H) (a ⊎⇘AH) (b ⊎⇘AH) = (a ± b) ⊎⇘AH"
apply (simp add:aqgrp_def ar_coset_def)
apply (cut_tac b_ag_group)
apply (frule asubg_nsubg)
apply (simp add:ag_carrier_carrier [THEN sym])
apply (subst Group.c_top_welldef [THEN sym], assumption+)
apply (simp add:agop_gop)
done

lemma (in aGroup) aqgrp_mOp_maps:"A +> H; a  carrier A 
                   mop (aqgrp A H) (a ⊎⇘AH) = (-a a) ⊎⇘AH"
apply (simp add:aqgrp_def ar_coset_def)
apply (cut_tac b_ag_group)
apply (frule asubg_nsubg)
apply (simp add:ag_carrier_carrier [THEN sym])
apply (subst Group.c_iop_welldef, assumption+)
apply (simp add:agiop_giop)
done

lemma (in aGroup) aqgrp_zero:"A +> H  zero (aqgrp A H) = H"
apply (simp add:aqgrp_def)
done

lemma (in aGroup) arcos_fixed:"A +> H; a  carrier A; h  H  
                              a ⊎⇘AH = (h ± a) ⊎⇘AH"
 apply (cut_tac b_ag_group)
 apply (simp add:agop_gop[THEN sym])
 apply (simp add:ag_carrier_carrier[THEN sym])
 apply (simp add:ar_coset_def)
 apply (simp add:asubGroup_def)
 apply (simp add:Group.rcs_fixed1[of "b_ag A" "H"])
done

definition
  rind_hom :: "[('a, 'more) aGroup_scheme, ('b, 'more1) aGroup_scheme,
                ('a   'b)]  ('a set   'b )" where
  "rind_hom A B f = (λX(set_ar_cos A (ker⇘A,Bf)). f (SOME x. x  X))"

abbreviation
  RIND_HOM  ("(3_°⇘_,_)" [82,82,83]82)  where
  "f°⇘F,G== rind_hom F G f"
                                                          (* tOp → pOp *)

section "Direct product and direct sum of abelian groups, in general case"

definition
  Un_carrier :: "['i set, 'i  ('a, 'more) aGroup_scheme]  'a set" where
  "Un_carrier I A = {X. iI. X = carrier (A i)}"

definition
  carr_prodag :: "['i set, 'i  ('a, 'more) aGroup_scheme]  ('i   'a ) set" where
  "carr_prodag I A = {f. f  extensional I  f  I  (Un_carrier I A) 
                                               (iI. f i  carrier (A i))}"

definition
  prod_pOp :: "['i set,  'i  ('a, 'more) aGroup_scheme] 
                                 ('i  'a)  ('i  'a)   ('i  'a)" where
  "prod_pOp I A = (λfcarr_prodag I A. λgcarr_prodag I A.
                                        λxI. (f x) ±(A x)(g x))"

definition
  prod_mOp :: "['i set, 'i  ('a, 'more) aGroup_scheme] 
                                  ('i  'a)  ('i  'a)" where
  "prod_mOp I A = (λfcarr_prodag I A. λxI. (-a(A x)(f x)))"

definition
  prod_zero :: "['i set,  'i   ('a, 'more) aGroup_scheme]  ('i  'a)" where
  "prod_zero I A = (λxI. 𝟬(A x))"

definition
  prodag :: "['i set, 'i  ('a, 'more) aGroup_scheme]  ('i  'a) aGroup" where
  "prodag I A =  carrier = carr_prodag I A,
    pop = prod_pOp I A, mop = prod_mOp I A,
    zero = prod_zero I A"

definition
  PRoject :: "['i set, 'i  ('a, 'more) aGroup_scheme, 'i]
                    ('i  'a)  'a" ("(3π⇘_,_,_)" [82,82,83]82) where
  "PRoject I A x = (λf  carr_prodag I A. f x)"

abbreviation
  PRODag  ("(aΠ⇘_ _)" [72,73]72) where
  "aΠ⇘IA == prodag I A"

lemma prodag_comp_i:"a  carr_prodag I A; i  I  (a i)  carrier (A i)"
by (simp add:carr_prodag_def)

lemma prod_pOp_func:"kI. aGroup (A k) 
    prod_pOp I A  carr_prodag I A  carr_prodag I A  carr_prodag I A"
apply (rule Pi_I)+
apply(rename_tac a b)
 apply (subst carr_prodag_def) apply (simp add:CollectI)
apply (rule conjI)
 apply (simp add:prod_pOp_def restrict_def extensional_def)
apply (rule conjI)
 apply (rule Pi_I)
 apply (simp add:prod_pOp_def)
 apply (subst Un_carrier_def) apply (simp add:CollectI)
 apply (frule_tac x = x in bspec, assumption,
        thin_tac "kI. aGroup (A k)")
 apply (simp add:carr_prodag_def) apply (erule conjE)+
 apply (thin_tac "a  I  Un_carrier I A")
 apply (thin_tac "b  I  Un_carrier I A")
 apply (frule_tac x = x in bspec, assumption,
        thin_tac "iI. a i  carrier (A i)",
        frule_tac x = x in bspec, assumption,
        thin_tac "iI. b i  carrier (A i)")
 apply (frule_tac x = "a x" and y = "b x" in aGroup.ag_pOp_closed, assumption+)
 apply blast
apply (rule ballI)
 apply (simp add:prod_pOp_def)
 apply (rule_tac A = "A i" and x = "a i" and y = "b i" in aGroup.ag_pOp_closed)
 apply simp
 apply (simp add:carr_prodag_def)+
done

lemma prod_pOp_mem:"kI. aGroup (A k); X  carr_prodag I A;
 Y  carr_prodag I A  prod_pOp I A X Y  carr_prodag I A"
apply (frule prod_pOp_func)
apply (frule funcset_mem[of "prod_pOp I A"
                        "carr_prodag I A" "carr_prodag I A  carr_prodag I A"
                         "X"], assumption+)
apply (rule funcset_mem[of "prod_pOp I A X" "carr_prodag I A"
                           "carr_prodag I A" "Y"], assumption+)
done

lemma prod_pOp_mem_i:"kI. aGroup (A k); X  carr_prodag I A;
 Y  carr_prodag I A; i  I  prod_pOp I A X Y i = (X i) ±(A i)(Y i)"
apply (simp add:prod_pOp_def)
done

lemma prod_mOp_func:"kI. aGroup (A k) 
                  prod_mOp I A  carr_prodag I A  carr_prodag I A"
apply (rule Pi_I)
 apply (simp add:prod_mOp_def carr_prodag_def)
 apply (erule conjE)+
apply (rule conjI)
 apply (rule Pi_I) apply simp
 apply (rename_tac f j)
 apply (frule_tac f = f and x = j in funcset_mem [of _ "I" "Un_carrier I A"],
                             assumption+)
 apply (thin_tac "f  I  Un_carrier I A")
 apply (frule_tac x = j in bspec, assumption,
        thin_tac "kI. aGroup (A k)",
        frule_tac x = j in bspec, assumption,
        thin_tac "iI. f i  carrier (A i)")
 apply (thin_tac "f j  Un_carrier I A")
 apply (simp add:Un_carrier_def)
 apply (frule aGroup.ag_mOp_closed, assumption+)
 apply blast
apply (rule ballI)
 apply (rule_tac A = "A i" and x = "x i" in aGroup.ag_mOp_closed)
 apply simp+
done

lemma prod_mOp_mem:"jI. aGroup (A j); X  carr_prodag I A 
                         prod_mOp I A X  carr_prodag I A"
apply (frule prod_mOp_func)
apply (simp add:Pi_def)
done

lemma prod_mOp_mem_i:"jI. aGroup (A j); X  carr_prodag I A; i  I 
                         prod_mOp I A X i = -a(A i)(X i)"
apply (simp add:prod_mOp_def)
done

lemma prod_zero_func:"kI. aGroup (A k) 
                           prod_zero I A  carr_prodag I A"
apply (simp add:prod_zero_def prodag_def)
apply (simp add:carr_prodag_def)
apply (rule conjI)
 apply (rule Pi_I) apply simp
 apply (subgoal_tac "aGroup (A x)") prefer 2 apply simp
 apply (thin_tac "kI. aGroup (A k)")
 apply (simp add:Un_carrier_def)
 apply (frule aGroup.ex_zero)
 apply auto
apply (frule_tac x = i in bspec, assumption,
       thin_tac "kI. aGroup (A k)")
 apply (simp add:aGroup.ex_zero)
done

lemma prod_zero_i:"kI. aGroup (A k); i  I 
                           prod_zero I A i = 𝟬(A i)⇙ "
by (simp add:prod_zero_def)

lemma carr_prodag_mem_eq:"kI. aGroup (A k); X  carr_prodag I A;
Y  carr_prodag I A; lI. (X l) = (Y l)   X = Y"
apply (simp add:carr_prodag_def)
apply (erule conjE)+
apply (simp add:funcset_eq)
done

lemma prod_pOp_assoc:"kI. aGroup (A k); a  carr_prodag I A;
      b  carr_prodag I A; c  carr_prodag I A 
      prod_pOp I A (prod_pOp I A a b) c =
                               prod_pOp I A a (prod_pOp I A b c)"
 apply (frule_tac X = a and Y = b in prod_pOp_mem[of "I" "A"], assumption+,
        frule_tac X = b and Y = c in prod_pOp_mem[of "I" "A"], assumption+,
        frule_tac X = "prod_pOp I A a b" and Y = c in prod_pOp_mem[of "I"
            "A"], assumption+,
        frule_tac X = a and Y = "prod_pOp I A b c" in prod_pOp_mem[of "I"
            "A"], assumption+)
 apply (rule carr_prodag_mem_eq[of "I" "A"], assumption+,
       rule ballI)
 apply (simp add:prod_pOp_mem_i)
 apply (frule_tac x = l in bspec, assumption,
        thin_tac "kI. aGroup (A k)")
 apply (rule aGroup.ag_pOp_assoc, assumption)
 apply (simp add:prodag_comp_i)+
done

lemma prod_pOp_commute:"kI. aGroup (A k); a  carr_prodag I A;
                           b  carr_prodag I A 
                           prod_pOp I A a b = prod_pOp I A b a"
apply (frule_tac X = a and Y = b in prod_pOp_mem[of "I" "A"], assumption+,
         frule_tac X = b and Y = a in prod_pOp_mem[of "I" "A"], assumption+)
apply (rule carr_prodag_mem_eq[of "I" "A"], assumption+,
        rule ballI)
 apply (simp add:prod_pOp_mem_i)
 apply (frule_tac x = l in bspec, assumption,
        thin_tac "kI. aGroup (A k)",
        rule aGroup.ag_pOp_commute, assumption)
 apply (simp add:prodag_comp_i)+
done

lemma prodag_aGroup:"kI. aGroup (A k)  aGroup (prodag I A)"
apply (simp add:aGroup_def [of "(prodag I A)"])
apply (simp add:prodag_def)
 apply (simp add:prod_pOp_func)
 apply (simp add:prod_mOp_func)
 apply (simp add:prod_zero_func)
apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (simp add:prod_pOp_assoc)
apply (rule conjI)
  apply (rule allI, rule impI)+
  apply (simp add:prod_pOp_commute)
apply (rule conjI)
 apply (rule allI, rule impI)
 apply (frule_tac X = a in prod_mOp_mem [of "I" "A"], assumption+)
 apply (frule_tac X = "prod_mOp I A a" and Y = a in prod_pOp_mem[of "I" "A"],
        assumption+)
 apply (rule carr_prodag_mem_eq[of "I" "A"], assumption+)
 apply (simp add:prod_zero_func)
 apply (rule ballI)
 apply (simp add:prod_pOp_mem_i,
         simp add:prod_zero_i) apply (
         simp add:prod_mOp_mem_i)
  apply (frule_tac x = l in bspec, assumption,
         thin_tac "kI. aGroup (A k)",
         rule aGroup.l_m, assumption+, simp add:prodag_comp_i)
apply (rule allI, rule impI)
  apply (frule_tac prod_zero_func[of "I" "A"],
         frule_tac Y = a in prod_pOp_mem[of "I" "A" "prod_zero I A"],
          assumption+)
  apply (rule carr_prodag_mem_eq[of "I" "A"], assumption+)
  apply (rule ballI)
  apply (subst prod_pOp_mem_i[of "I" "A"], assumption+,
         subst prod_zero_i[of "I" "A"], assumption+)
  apply (frule_tac x = l in bspec, assumption,
         rule aGroup.l_zero, assumption+,
         simp add:prodag_comp_i)
done

lemma prodag_carrier:"kI. aGroup (A k) 
            carrier (prodag I A) = carr_prodag I A"
by (simp add:prodag_def)

lemma prodag_elemfun:"kI. aGroup (A k); f  carrier (prodag I A) 
         f  extensional I"
apply (simp add:prodag_carrier)
apply (simp add:carr_prodag_def)
done

lemma prodag_component:"f  carrier (prodag I A); i  I  
                              f i  carrier (A i)"
by (simp add:prodag_def carr_prodag_def)

lemma prodag_pOp:"kI. aGroup (A k) 
                  pop (prodag I A) = prod_pOp I A"
apply (simp add:prodag_def)
done

lemma prodag_iOp:"kI. aGroup (A k) 
                  mop (prodag I A) = prod_mOp I A"
apply (simp add:prodag_def)
done

lemma prodag_zero:"kI. aGroup (A k) 
                  zero (prodag I A) = prod_zero I A"
apply (simp add:prodag_def)
done

lemma prodag_sameTr0:"kI. aGroup (A k); kI. A k = B k
                                Un_carrier I A = Un_carrier I B"
apply (simp add:Un_carrier_def)
done

lemma prodag_sameTr1:"kI. aGroup (A k); kI. A k = B k
                                carr_prodag I A = carr_prodag I B"
apply (rule equalityI)
 apply (rule subsetI)
 apply (simp add:carr_prodag_def, (erule conjE)+)
 apply (rule Pi_I)
 apply (subst Un_carrier_def, simp, blast)

apply (rule subsetI)
 apply (simp add:carr_prodag_def, (erule conjE)+)
 apply (rule Pi_I)
 apply (subst Un_carrier_def, simp)
 apply blast
done

lemma prodag_sameTr2:"kI. aGroup (A k); kI. A k = B k
                                prod_pOp I A = prod_pOp I B"
apply (frule prodag_sameTr1 [of "I" "A" "B"], assumption+)
apply (simp add:prod_pOp_def)
apply (rule bivar_func_eq)
apply (rule ballI)+
apply (rule funcset_eq [of _ "I"])
 apply (simp add:restrict_def extensional_def)+
done

lemma prodag_sameTr3:"kI. aGroup (A k); kI. A k = B k
                                prod_mOp I A = prod_mOp I B"
apply (frule prodag_sameTr1 [of "I" "A" "B"], assumption+)
apply (simp add:prod_mOp_def)
apply (rule funcset_eq [of _ "carr_prodag I B"])
 apply (simp add:restrict_def extensional_def)
 apply (simp add:restrict_def extensional_def)
apply (rule ballI)
apply (rename_tac g) apply simp
apply (rule funcset_eq [of _ "I"])
 apply (simp add:restrict_def extensional_def)+
done

lemma prodag_sameTr4:"kI. aGroup (A k); kI. A k = B k
                                prod_zero I A = prod_zero I B"
apply (simp add:prod_zero_def)
apply (rule funcset_eq [of _ "I"])
 apply (simp add:restrict_def extensional_def)+
done

lemma prodag_same:"kI. aGroup (A k); kI. A k = B k
                                prodag I A = prodag I B"
apply (frule prodag_sameTr1, assumption+)
apply (frule prodag_sameTr2, assumption+)
apply (frule prodag_sameTr3, assumption+)
apply (frule prodag_sameTr4, assumption+)
apply (simp add:prodag_def)
done

lemma project_mem:"kI. aGroup (A k); j  I; x  carrier (prodag I A) 
                         (PRoject I A j) x   carrier (A j)"
apply (simp add:PRoject_def)
apply (simp add:prodag_def)
apply (simp add:carr_prodag_def)
done

lemma project_aHom:"kI. aGroup (A k); j  I 
                         PRoject I A j  aHom (prodag I A) (A j)"
apply (simp add:aHom_def)
apply (rule conjI)
 apply (simp add:project_mem)
apply (rule conjI)
 apply (simp add:PRoject_def restrict_def extensional_def)
 apply (rule allI, rule impI, simp add:prodag_def)
apply (rule ballI)+
 apply (simp add:prodag_def)
 apply (simp add:prod_pOp_def)
 apply (frule_tac X = a and Y = b in prod_pOp_mem[of I A], assumption+)
 apply (simp add:prod_pOp_def)
 apply (simp add:PRoject_def)
done

lemma project_aHom1:"kI. aGroup (A k) 
                      j  I. PRoject I A j  aHom (prodag I A) (A j)"
apply (rule ballI)
apply (rule project_aHom, assumption+)
done

definition
  A_to_prodag :: "[('a, 'm) aGroup_scheme, 'i set, 'i ('a  'b),
   'i   ('b, 'm1) aGroup_scheme]  ('a  ('i 'b))" where
 "A_to_prodag A I S B = (λacarrier A. λkI. S k a)"

 (* I is an index set, A is an abelian group, S: I → carrier A →
  carrier (prodag I B),   s i ∈ carrier A → B i  *)

lemma A_to_prodag_mem:"aGroup A; kI. aGroup (B k);  kI. (S k) 
 aHom A (B k); x  carrier A   A_to_prodag A I S B x  carr_prodag I B"
apply (simp add:carr_prodag_def)
apply (rule conjI)
apply (simp add:A_to_prodag_def extensional_def restrict_def)
apply (simp add:Pi_def restrict_def A_to_prodag_def)
apply (rule conjI)
apply (rule allI) apply (rule impI)
apply (simp add:Un_carrier_def)
 apply (rotate_tac 2,
        frule_tac x = xa in bspec, assumption,
        thin_tac "kI. S k  aHom A (B k)")
 apply (simp add:aHom_def) apply (erule conjE)+
 apply (frule_tac f = "S xa" and A = "carrier A" and B = "carrier (B xa)"
           and x = x in funcset_mem, assumption+)
 apply blast
apply (rule ballI)
 apply (rotate_tac 2,
        frule_tac x = i in bspec, assumption,
        thin_tac "kI. S k  aHom A (B k)")
 apply (simp add:aHom_def) apply (erule conjE)+
 apply (simp add:Pi_def)
done

lemma A_to_prodag_aHom:"aGroup A; kI. aGroup (B k); kI. (S k) 
 aHom A (B k)    A_to_prodag A I S B  aHom A (aΠ⇘IB)"
apply (simp add:aHom_def [of "A" "aΠ⇘IB"])
apply (rule conjI)
 apply (simp add:prodag_def A_to_prodag_mem)

apply (rule conjI)
apply (simp add:A_to_prodag_def restrict_def extensional_def)
apply (rule ballI)+
 apply (frule_tac x = a and y = b in aGroup.ag_pOp_closed, assumption+)
 apply (frule_tac x = "a ±Ab" in A_to_prodag_mem [of "A" "I" "B" "S"],
                                                       assumption+)
 apply (frule_tac x = a in A_to_prodag_mem [of "A" "I" "B" "S"],
                                                       assumption+)
 apply (frule_tac x = b in A_to_prodag_mem [of "A" "I" "B" "S"],
                                                       assumption+)
 apply (frule prodag_aGroup [of "I" "B"])
 apply (frule_tac x = a in A_to_prodag_mem[of "A" "I" "B" "S"], assumption+,
        frule_tac x = b in A_to_prodag_mem[of "A" "I" "B" "S"], assumption+,
        frule_tac x = "a ±Ab" in A_to_prodag_mem[of "A" "I" "B" "S"],
                                                 assumption+)
 apply (frule prodag_aGroup[of "I" "B"],
        frule_tac x = "A_to_prodag A I S B a" and
 y = "A_to_prodag A I S B b" in aGroup.ag_pOp_closed [of "aΠ⇘IB"])
 apply (simp add:prodag_carrier)
 apply (simp add:prodag_carrier)
 apply (rule carr_prodag_mem_eq, assumption+)
 apply (simp add:prodag_carrier)
 apply (rule ballI)
 apply (simp add:A_to_prodag_def prod_pOp_def)
 apply (rotate_tac 2,
        frule_tac x = l in bspec, assumption,
        thin_tac "kI. S k  aHom A (B k)")
 apply (simp add:prodag_def prod_pOp_def)
 apply (frule_tac x = l in bspec, assumption,
        thin_tac "kI. aGroup (B k)")
apply (simp add: aHom_add)
done

definition
  finiteHom :: "['i set, 'i  ('a, 'more) aGroup_scheme, 'i  'a]  bool" where
  "finiteHom I A f  f  carr_prodag I A  (H. H  I  finite H  (
    j  (I - H). (f j) = 𝟬(A j)))"

definition
  carr_dsumag :: "['i set, 'i  ('a, 'more) aGroup_scheme]  ('i   'a ) set" where
  "carr_dsumag I A = {f. finiteHom I A f}"

definition
  dsumag :: "['i set, 'i  ('a, 'more) aGroup_scheme]  ('i  'a) aGroup" where
  "dsumag I A =  carrier = carr_dsumag I A,
     pop = prod_pOp I A, mop = prod_mOp I A,
     zero = prod_zero I A"

definition
  dProj :: "['i set, 'i  ('a, 'more) aGroup_scheme, 'i]
                    ('i  'a)  'a" where
  "dProj I A x = (λfcarr_dsumag I A. f x)"

abbreviation
  DSUMag  ("(a⨁⇘_ _)" [72,73]72) where
  "a⨁⇘IA == dsumag I A"

lemma dsum_pOp_func:"kI. aGroup (A k) 
    prod_pOp I A  carr_dsumag I A  carr_dsumag I A  carr_dsumag I A"
apply (rule Pi_I)+
 apply (subst carr_dsumag_def) apply (simp add:CollectI)
apply (simp add:finiteHom_def)
 apply (rule conjI)
 apply (simp add:carr_dsumag_def) apply (simp add:finiteHom_def)
 apply (erule conjE)+ apply (simp add:prod_pOp_mem)
apply (simp add:carr_dsumag_def finiteHom_def) apply (erule conjE)+
 apply ((erule exE)+, (erule conjE)+)
 apply (frule_tac F = H and G = Ha in finite_UnI, assumption+)
 apply (subgoal_tac "jI - (H  Ha). prod_pOp I A x xa j = 𝟬A j⇙")
 apply (frule_tac A = H and B = Ha in Un_least[of _ "I"], assumption+)
  apply blast

 apply (rule ballI)
 apply (simp, (erule conjE)+)
 apply (frule_tac x = j in bspec, assumption,
         thin_tac "kI. aGroup (A k)",
        frule_tac x = j in bspec, simp,
         thin_tac "jI - H. x j = 𝟬A j⇙",
        frule_tac x = j in bspec, simp,
         thin_tac "jI - Ha. xa j = 𝟬A j⇙")
 apply (simp add:prod_pOp_def)
 apply (rule aGroup.ag_l_zero) apply simp
 apply (rule aGroup.ex_zero) apply assumption
done

lemma dsum_pOp_mem:"kI. aGroup (A k); X  carr_dsumag I A;
 Y  carr_dsumag I A  prod_pOp I A X Y  carr_dsumag I A"
apply (frule dsum_pOp_func[of "I" "A"])
apply (frule funcset_mem[of "prod_pOp I A" "carr_dsumag I A"
              "carr_dsumag I A  carr_dsumag I A" "X"], assumption+)
apply (rule funcset_mem[of "prod_pOp I A X" "carr_dsumag I A"
            "carr_dsumag I A" "Y"], assumption+)
done

lemma dsum_iOp_func:"kI. aGroup (A k) 
                  prod_mOp I A  carr_dsumag I A  carr_dsumag I A"
apply (rule Pi_I)
 apply (simp add:carr_dsumag_def) apply (simp add:finiteHom_def)
 apply (erule conjE)+ apply (simp add:prod_mOp_mem)
 apply (erule exE, (erule conjE)+)
 apply (simp add:prod_mOp_def)
 apply (subgoal_tac "jI - H. -aA j(x j) = 𝟬A j⇙")
 apply blast

apply (rule ballI)
 apply (frule_tac x = j in bspec, simp,
        thin_tac "kI. aGroup (A k)",
        frule_tac x = j in bspec, simp,
        thin_tac "jI - H. x j = 𝟬A j⇙", simp add:aGroup.ag_inv_zero)
done

lemma dsum_iOp_mem:"jI. aGroup (A j); X  carr_dsumag I A 
                         prod_mOp I A X  carr_dsumag I A"
apply (frule dsum_iOp_func)
apply (simp add:Pi_def)
done

lemma dsum_zero_func:"kI. aGroup (A k) 
                           prod_zero I A  carr_dsumag I A"
apply (simp add:carr_dsumag_def) apply (simp add:finiteHom_def)
apply (rule conjI) apply (simp add:prod_zero_func)
 apply (subgoal_tac "{}  I") prefer 2 apply simp
 apply (subgoal_tac "finite {}") prefer 2 apply simp
 apply (subgoal_tac "jI - {}. prod_zero I A j = 𝟬A j⇙")
 apply blast
 apply (rule ballI) apply simp
 apply (simp add:prod_zero_def)
done

lemma dsumag_sub_prodag:"kI. aGroup (A k) 
                              carr_dsumag I A  carr_prodag I A"
by (rule subsetI,
       simp add:carr_dsumag_def finiteHom_def)

lemma carrier_dsumag:"kI. aGroup (A k) 
         carrier (dsumag I A) = carr_dsumag I A"
apply (simp add:dsumag_def)
done

lemma dsumag_elemfun:"kI. aGroup (A k); f  carrier (dsumag I A) 
         f  extensional I"
apply (simp add:carrier_dsumag)
apply (simp add:carr_dsumag_def) apply (simp add:finiteHom_def)
apply (erule conjE) apply (simp add:carr_prodag_def)
done

lemma dsumag_aGroup:"kI. aGroup (A k)  aGroup (dsumag I A)"
apply (simp add:aGroup_def [of "dsumag I A"])
apply (simp add:dsumag_def)
apply (simp add:dsum_pOp_func)
apply (simp add:dsum_iOp_func)
apply (simp add:dsum_zero_func)
apply (frule dsumag_sub_prodag[of "I" "A"])

apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (frule_tac X = a and Y = b in dsum_pOp_mem, assumption+)
 apply (frule_tac X = b and Y = c in dsum_pOp_mem, assumption+)
 apply (frule_tac X = "prod_pOp I A a b" and Y = c in dsum_pOp_mem,
                    assumption+)
 apply (frule_tac Y = "prod_pOp I A b c" and X = a in dsum_pOp_mem,
                    assumption+)
 apply (rule carr_prodag_mem_eq [of "I" "A"], assumption+)
 apply (simp add:subsetD) apply (simp add:subsetD)
 apply (rule ballI)
 apply (subst prod_pOp_mem_i, assumption+, (simp add:subsetD)+)
 apply (subst prod_pOp_mem_i, assumption+)
  apply (simp add:subsetD)+
 apply (subst prod_pOp_mem_i, assumption+, (simp add:subsetD)+)
 apply (subst prod_pOp_mem_i, assumption+) apply (simp add:subsetD)+
 apply (thin_tac "prod_pOp I A a b  carr_dsumag I A",
        thin_tac "prod_pOp I A b c  carr_dsumag I A",
        thin_tac "prod_pOp I A (prod_pOp I A a b) c  carr_dsumag I A",
        thin_tac "prod_pOp I A a (prod_pOp I A b c)  carr_dsumag I A",
        thin_tac "carr_dsumag I A  carr_prodag I A")

 apply (frule_tac x = l in bspec, assumption,
        thin_tac "kI. aGroup (A k)",
        simp add:carr_dsumag_def finiteHom_def, (erule conjE)+,
        simp add:carr_prodag_def, (erule conjE)+)
 apply (frule_tac x = l in bspec, assumption,
        thin_tac "iI. a i  carrier (A i)",
        frule_tac x = l in bspec, assumption,
        thin_tac "iI. b i  carrier (A i)",
        frule_tac x = l in bspec, assumption,
        thin_tac "iI. c i  carrier (A i)")
 apply (simp add:aGroup.aassoc)

apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (rule carr_prodag_mem_eq [of "I" "A"], assumption+)
  apply (frule_tac X = a and Y = b in prod_pOp_mem[of "I" "A"],
         (simp add:subsetD)+)
  apply (frule_tac X = b and Y = a in prod_pOp_mem[of "I" "A"],
         (simp add:subsetD)+)
  apply (rule ballI,
         subst prod_pOp_mem_i, assumption+, (simp add:subsetD)+)
  apply (subst prod_pOp_mem_i, assumption+, (simp add:subsetD)+)
  apply (frule_tac x = l in bspec, assumption,
         thin_tac "kI. aGroup (A k)")
  apply (frule_tac c = a in subsetD[of "carr_dsumag I A" "carr_prodag I A"],
          assumption+, thin_tac "a  carr_dsumag I A",
         frule_tac c = b in subsetD[of "carr_dsumag I A" "carr_prodag I A"],
          assumption+, thin_tac "b  carr_dsumag I A",
          thin_tac "carr_dsumag I A  carr_prodag I A")
  apply (simp add:carr_prodag_def, (erule conjE)+,
         simp add:aGroup.ag_pOp_commute)

apply (rule conjI)
 apply (rule allI, rule impI)
 apply (frule_tac X = a in prod_mOp_mem[of "I" "A"],
        simp add:subsetD)
 apply (frule_tac X = "prod_mOp I A a" and Y = a in prod_pOp_mem[of "I" "A"],
        simp add:subsetD, simp add:subsetD)
 apply (rule carr_prodag_mem_eq [of "I" "A"], assumption+,
        simp add:prod_zero_func)
 apply (rule ballI)
 apply (subst prod_pOp_mem_i, assumption+,
        simp add:subsetD, assumption)
 apply (subst prod_mOp_mem_i, assumption+, simp add:subsetD, assumption)
 apply (simp add:prod_zero_i)
 apply (frule_tac x = l in bspec, assumption,
         thin_tac "kI. aGroup (A k)",
         thin_tac "prod_mOp I A a  carr_prodag I A",
         thin_tac "prod_pOp I A (prod_mOp I A a) a  carr_prodag I A",
        frule_tac c = a in subsetD[of "carr_dsumag I A" "carr_prodag I A"],
         assumption,
        thin_tac "carr_dsumag I A  carr_prodag I A",
        simp add:carr_prodag_def, (erule conjE)+)
  apply (frule_tac x = l in bspec, assumption,
         thin_tac "iI. a i  carrier (A i)")
  apply (rule aGroup.l_m, assumption+)

apply (rule allI, rule impI)
 apply (frule prod_zero_func[of "I" "A"])
 apply (frule_tac X = "prod_zero I A" and Y = a in prod_pOp_mem[of "I" "A"],
            assumption+, simp add:subsetD)
 apply (rule carr_prodag_mem_eq [of "I" "A"], assumption+,
        simp add:subsetD)
 apply (rule ballI)
 apply (subst prod_pOp_mem_i, assumption+)
        apply (simp add:subsetD, assumption)
 apply (simp add:prod_zero_i,
        frule_tac x = l in bspec, assumption,
        thin_tac "kI. aGroup (A k)",
        frule_tac c = a in subsetD[of "carr_dsumag I A" "carr_prodag I A"],
                  assumption+,
        thin_tac "carr_dsumag I A  carr_prodag I A",
        thin_tac "a  carr_dsumag I A",
        thin_tac "prod_pOp I A (prod_zero I A) a  carr_prodag I A")
 apply (simp add:carr_prodag_def, (erule conjE)+)
 apply (rule aGroup.l_zero, assumption)
 apply blast
done

lemma dsumag_pOp:"kI. aGroup (A k) 
                  pop (dsumag I A) = prod_pOp I A"
apply (simp add:dsumag_def)
done

lemma dsumag_mOp:"kI. aGroup (A k) 
                  mop (dsumag I A) = prod_mOp I A"
apply (simp add:dsumag_def)
done

lemma dsumag_zero:"kI. aGroup (A k) 
                  zero (dsumag I A) = prod_zero I A"
apply (simp add:dsumag_def)
done


subsection "Characterization of a direct product"

lemma direct_prod_mem_eq:"jI. aGroup (A j); f  carrier (aΠ⇘IA);
       g  carrier (aΠ⇘IA); jI. (PRoject I A j) f = (PRoject I A j) g 
       f = g"
apply (rule funcset_eq[of "f" "I" "g"])
 apply (thin_tac "jI. aGroup (A j)",
        thin_tac "g  carrier (aΠ⇘IA)",
        thin_tac "jI. (π⇘I,A,j) f = (π⇘I,A,j) g",
        simp add:prodag_def carr_prodag_def)
  apply (thin_tac "jI. aGroup (A j)",
        thin_tac "f  carrier (aΠ⇘IA)",
        thin_tac "jI. (π⇘I,A,j) f = (π⇘I,A,j) g",
        simp add:prodag_def carr_prodag_def)
 apply (simp add:PRoject_def prodag_def)
done

lemma map_family_fun:"jI. aGroup (A j); aGroup S;
      jI. ((g j)  aHom S (A j)); x  carrier S 
         (λy  carrier S. (λjI. (g j) y)) x  carrier (aΠ⇘IA)"
apply (simp add:prodag_def carr_prodag_def)
 apply (simp add:aHom_mem)
 apply (rule Pi_I, simp add:Un_carrier_def)
 apply (frule_tac x = xa in bspec, assumption,
        thin_tac "jI. aGroup (A j)",
        frule_tac x = xa in bspec, assumption,
        thin_tac "jI. g j  aHom S (A j)")
 apply (frule_tac G = "A xa" and f = "g xa" and a = x in aHom_mem[of "S"],
        assumption+, blast)
done

lemma map_family_aHom:"jI. aGroup (A j); aGroup S;
      jI. ((g j)  aHom S (A j)) 
         (λy  carrier S. (λjI. (g j) y))  aHom S (aΠ⇘IA)"
apply (subst aHom_def, simp)
 apply (simp add:aGroup.ag_pOp_closed)

apply (rule conjI)
 apply (rule Pi_I)
 apply (rule map_family_fun[of "I" "A" "S" "g"], assumption+)
apply (rule ballI)+
 apply (frule_tac x = a and y = b in aGroup.ag_pOp_closed[of "S"],
                   assumption+)
 apply (frule_tac x = "a ±Sb" in map_family_fun[of "I" "A" "S" "g"],
          assumption+, simp)
 apply (frule_tac x = a in map_family_fun[of "I" "A" "S" "g"],
          assumption+, simp,
         frule_tac x = b in map_family_fun[of "I" "A" "S" "g"],
          assumption+, simp)
 apply (frule prodag_aGroup[of "I" "A"])
 apply (frule_tac x = "(λjI. g j a)" and y = "(λjI. g j b)" in
        aGroup.ag_pOp_closed[of "aΠ⇘IA"], assumption+)
 apply (simp only:prodag_carrier)

apply (rule carr_prodag_mem_eq, assumption+)
 apply (rule ballI)
 apply (subst prodag_def, simp add:prod_pOp_def)
 apply (simp add:aHom_add)
done

lemma map_family_triangle:"jI. aGroup (A j); aGroup S;
         jI. ((g j)  aHom S (A j))  ∃!f. f  aHom S (aΠ⇘IA) 
                  (jI. compos S (PRoject I A j) f =  (g j))"
apply (rule ex_ex1I)
apply (frule map_family_aHom[of "I" "A" "S" "g"], assumption+)
apply (subgoal_tac "jI. compos S (π⇘I,A,j) (λycarrier S. λjI. g j y) = g j")
apply blast
apply (rule ballI)
apply (simp add:compos_def)
apply (rule funcset_eq[of _ "carrier S"])
 apply (simp add:compose_def) apply (simp add:aHom_def)
 apply (rule ballI)
 apply (frule prodag_aGroup[of "I" "A"])
 apply (frule prodag_carrier[of "I" "A"])
 apply (frule_tac f = "λycarrier S. λjI. g j y" and a = x in
        aHom_mem[of "S" "aΠ⇘IA"], assumption+)
 apply (simp add:compose_def, simp add:PRoject_def)
apply (rename_tac f f1)
 apply (erule conjE)+
 apply (rule funcset_eq[of _ "carrier S"])
 apply (simp add:aHom_def, simp add:aHom_def)
 apply (rule ballI)
 apply (frule prodag_aGroup[of "I" "A"])
 apply (frule_tac f = f and a = x in aHom_mem[of "S" "aΠ⇘IA"], assumption+,
        frule_tac f = f1 and a = x in aHom_mem[of "S" "aΠ⇘IA"], assumption+)
 apply (rule_tac f = "f x" and g = "f1 x" in direct_prod_mem_eq[of "I" "A"],
        assumption+)
 apply (rule ballI)
 apply (rotate_tac 4,
        frule_tac x = j in bspec, assumption,
        thin_tac "jI. compos S (π⇘I,A,j) f = g j",
         frule_tac x = j in bspec, assumption,
        thin_tac "jI. compos S (π⇘I,A,j) f1 = g j",
        simp add:compos_def compose_def)
 apply (subgoal_tac "(λxcarrier S. (π⇘I,A,j) (f x)) x = g j x",
        subgoal_tac "(λxcarrier S. (π⇘I,A,j) (f1 x)) x = g j x",
        thin_tac "(λxcarrier S. (π⇘I,A,j) (f x)) = g j",
        thin_tac "(λxcarrier S. (π⇘I,A,j) (f1 x)) = g j",
simp+)
done

lemma Ag_ind_triangle:"jI. aGroup (A j); j  I; f  carrier (aΠ⇘IA)  B;
      bij_to f (carrier (aΠ⇘IA)) (B::'d set); j  I 
compos (aΠ⇘IA) (compos (Ag_ind (aΠ⇘IA) f)(PRoject I A j) (ainvf⇘(aΠ⇘IA),
 (Ag_ind (aΠ⇘IA) f)(Agii (aΠ⇘IA) f))) (Agii (aΠ⇘IA) f) =
                                       PRoject I A j"
apply (frule prodag_aGroup[of "I" "A"])
apply (frule aGroup.Ag_ind_aGroup[of "aΠ⇘IA" "f" "B"], assumption+)
apply (simp add:compos_def)
apply (rule funcset_eq[of _ "carrier (aΠ⇘IA)"])
apply simp
apply (simp add:PRoject_def  prodag_carrier extensional_def)
apply (rule ballI)
apply (simp add:compose_def invfun_l)
apply (simp add:aGroup.Agii_mem)
apply (frule Ag_ind_bijec[of "aΠ⇘IA" "f" "B"], assumption+)
apply (frule_tac x = x in ainvf_l[of "aΠ⇘IA" "Ag_ind (aΠ⇘IA) f"
                                     "Agii (aΠ⇘IA) f"], assumption+)
apply simp
done

(** Note               f'
                 aΠI A → Ag_ind (aΠI A) f
                     \     |
                      \    |
        PRoject I A j  \   | (PRoject I A j) o (f'¯1)
                        \  |
                          A j             , where f' = Agii (aΠI A) f **)

definition
  ProjInd :: "['i set, 'i  ('a, 'm) aGroup_scheme, ('i  'a)  'd, 'i] 
                       ('d  'a)" where
  "ProjInd I A f j = compos (Ag_ind (aΠ⇘IA) f)(PRoject I A j) (ainvf⇘(aΠ⇘IA), (Ag_ind (aΠ⇘IA) f)(Agii (aΠ⇘IA) f))"

(** Note               f'
                 aΠI A → Ag_ind (aΠI A) f
                     \     |
                      \    |
        PRoject I A j  \   | PRojInd I A f j
                        \  |
                          A j              **)

lemma ProjInd_aHom:"j I. aGroup (A j); j  I; f  carrier (aΠ⇘IA)  B;
      bij_to f (carrier (aΠ⇘IA)) (B::'d set); j  I 
        (ProjInd I A f j)  aHom (Ag_ind (aΠ⇘IA) f) (A j)"
apply (frule prodag_aGroup[of "I" "A"])
apply (frule aGroup.Ag_ind_aGroup[of "aΠ⇘IA" "f" "B"], assumption+)
apply (frule_tac x = j in bspec, assumption)
apply (frule aGroup.Ag_ind_aHom[of "aΠ⇘IA" "f" "B"], assumption+)
apply (simp add:ProjInd_def)
apply (frule Ag_ind_bijec[of "aΠ⇘IA" "f" "B"], assumption+)
apply (frule ainvf_aHom[of "aΠ⇘IA" "Ag_ind (aΠ⇘IA) f" "Agii (aΠ⇘IA) f"],
             assumption+)
apply (frule project_aHom[of "I" "A" "j"], assumption)
apply (simp add:aHom_compos)
done

lemma ProjInd_aHom1:"j I. aGroup (A j); f  carrier (aΠ⇘IA)  B;
      bij_to f (carrier (aΠ⇘IA)) (B::'d set) 
        jI. (ProjInd I A f j)  aHom (Ag_ind (aΠ⇘IA) f) (A j)"
apply (rule ballI)
apply (simp add:ProjInd_aHom)
done

lemma ProjInd_mem_eq:"jI. aGroup (A j); f  carrier (aΠ⇘IA)  B;
      bij_to f (carrier (aΠ⇘IA)) B; aGroup S; x  carrier (Ag_ind (aΠ⇘IA) f);
      y  carrier (Ag_ind (aΠ⇘IA) f);
      jI. (ProjInd I A f j x = ProjInd I A f j y)  x = y"
apply (simp add:ProjInd_def)
apply (simp add:compos_def compose_def)
apply (frule prodag_aGroup[of "I" "A"])
apply (frule aGroup.Ag_ind_aGroup[of "aΠ⇘IA" "f" "B"], assumption+)
apply (frule aGroup.Ag_ind_aHom[of "aΠ⇘IA" "f" "B"], assumption+)
apply (frule Ag_ind_bijec[of "aΠ⇘IA" "f" "B"], assumption+)
apply (frule ainvf_aHom[of "aΠ⇘IA" "Ag_ind (aΠ⇘IA) f" "Agii (aΠ⇘IA) f"],
         assumption+)
apply (frule aHom_mem[of "Ag_ind (aΠ⇘IA) f" "aΠ⇘IA" "ainvf⇘(aΠ⇘IA),Ag_ind (aΠ⇘IA) fAgii (aΠ⇘IA) f" "x"], assumption+,
       frule aHom_mem[of "Ag_ind (aΠ⇘IA) f" "aΠ⇘IA" "ainvf⇘(aΠ⇘IA),Ag_ind (aΠ⇘IA) fAgii (aΠ⇘IA) f" "y"], assumption+)

apply (frule direct_prod_mem_eq[of "I" "A" "(ainvf⇘(aΠ⇘IA),Ag_ind (aΠ⇘IA) fAgii (aΠ⇘IA) f) x" "(ainvf⇘(aΠ⇘IA),Ag_ind (aΠ⇘IA) fAgii (aΠ⇘IA) f) y"], assumption+)
apply (thin_tac "ainvf⇘(aΠ⇘IA),Ag_ind (aΠ⇘IA) fAgii (aΠ⇘IA) f
      aHom (Ag_ind (aΠ⇘IA) f) (aΠ⇘IA)")
apply (frule ainvf_bijec[of "aΠ⇘IA" "Ag_ind (aΠ⇘IA) f" "Agii (aΠ⇘IA) f"],
                   assumption+)
apply (thin_tac "bijec⇘(aΠ⇘IA),Ag_ind (aΠ⇘IA) fAgii (aΠ⇘IA) f")
apply (unfold bijec_def, frule conjunct1, fold bijec_def)
apply (frule injec_inj_on[of "Ag_ind (aΠ⇘IA) f" "aΠ⇘IA" "ainvf⇘(aΠ⇘IA),Ag_ind (aΠ⇘IA) fAgii (aΠ⇘IA) f"], assumption+)
apply (simp add:injective_iff[THEN sym, of "ainvf⇘(aΠ⇘IA),Ag_ind (aΠ⇘IA) fAgii (aΠ⇘IA) f" "carrier (Ag_ind (aΠ⇘IA) f)" "x" "y"])
done

lemma ProjInd_mem_eq1:"jI. aGroup (A j); f  carrier (aΠ⇘IA)  B;
      bij_to f (carrier (aΠ⇘IA)) B; aGroup S;
      h  aHom (Ag_ind (aΠ⇘IA) f) (Ag_ind (aΠ⇘IA) f);
      jI. compos (Ag_ind (aΠ⇘IA) f) (ProjInd I A f j) h = ProjInd I A f j        h = ag_idmap (Ag_ind (aΠ⇘IA) f)"
apply (rule funcset_eq[of _ "carrier (Ag_ind (aΠ⇘IA) f)"])
 apply (simp add:aHom_def)
 apply (simp add:ag_idmap_def)
apply (rule ballI)
 apply (simp add:ag_idmap_def)
 apply (frule prodag_aGroup[of "I" "A"],
        frule aGroup.Ag_ind_aGroup[of "aΠ⇘IA" "f" "B"], assumption+)
 apply (frule_tac a = x in aHom_mem[of "Ag_ind (aΠ⇘IA) f" "Ag_ind (aΠ⇘IA) f"
        "h"], assumption+)
 apply (rule_tac x = "h x" and y = x in ProjInd_mem_eq[of "I" "A" "f" "B" "S"],
        assumption+)
 apply (rotate_tac 1,
        rule ballI,
        frule_tac x = j in bspec, assumption,
        thin_tac "jI. compos (Ag_ind (aΠ⇘IA) f) (ProjInd I A f j) h =
               ProjInd I A f j")
 apply (simp add:compos_def compose_def)
 apply (subgoal_tac "(λxcarrier (Ag_ind (aΠ⇘IA) f). ProjInd I A f j (h x)) x
                    = ProjInd I A f j x",
        thin_tac "(λxcarrier (Ag_ind (aΠ⇘IA) f). ProjInd I A f j (h x)) =
           ProjInd I A f j")
 apply simp+
done

lemma Ag_ind_triangle1:"jI. aGroup (A j); f  carrier (aΠ⇘IA)  B;
      bij_to f (carrier (aΠ⇘IA)) (B::'d set); j  I 
      compos (aΠ⇘IA) (ProjInd I A f j) (Agii (aΠ⇘IA) f) =  PRoject I A j"
apply (simp add:ProjInd_def)
apply (simp add:Ag_ind_triangle)
done

lemma map_family_triangle1:"jI. aGroup (A j); f  carrier (aΠ⇘IA)  B;
      bij_to f (carrier (aΠ⇘IA)) (B::'d set); aGroup S;
     jI. ((g j)  aHom S (A j))  ∃!h. h  aHom S (Ag_ind (aΠ⇘IA) f) 
                  (jI. compos S (ProjInd I A f j) h =  (g j))"
apply (frule prodag_aGroup[of "I" "A"])
apply (frule aGroup.Ag_ind_aGroup[of "aΠ⇘IA" "f" "B"], assumption+)
apply (frule Ag_ind_bijec[of "aΠ⇘IA" "f" "B"], assumption+)
apply (rule ex_ex1I)
apply (frule map_family_triangle[of "I" "A" "S" "g"], assumption+)
apply (frule ex1_implies_ex)
apply (erule exE)
apply (erule conjE)
apply (unfold bijec_def, frule conjunct2, fold bijec_def)
apply (unfold surjec_def, frule conjunct1, fold surjec_def)
apply (rename_tac fa,
       frule_tac f = fa in aHom_compos[of "S" "aΠ⇘IA" "Ag_ind (aΠ⇘IA) f" _
                 "Agii (aΠ⇘IA) f"], assumption+)
apply (subgoal_tac "jI. compos S (ProjInd I A f j)
                           (compos S (Agii (aΠ⇘IA) f) fa) = g j")
apply blast
apply (rule ballI)
apply (frule_tac N = "A j" and f = fa and g = "Agii (aΠ⇘IA) f" and
 h = "ProjInd I A f j" in aHom_compos_assoc[of "S" "aΠ⇘IA" "Ag_ind (aΠ⇘IA) f"],
 assumption+) apply simp apply assumption+
apply (simp add:ProjInd_aHom)
apply simp
apply (thin_tac "compos S (ProjInd I A f j) (compos S (Agii (aΠ⇘IA) f) fa) =
        compos S (compos (aΠ⇘IA) (ProjInd I A f j) (Agii (aΠ⇘IA) f)) fa")
apply (simp add:Ag_ind_triangle1)
apply (rename_tac h h1)
 apply (erule conjE)+
 apply (rule funcset_eq[of _ "carrier S"])
 apply (simp add:aHom_def, simp add:aHom_def)
 apply (rule ballI)
 apply (simp add:compos_def)

apply (frule_tac f = h and a = x in aHom_mem[of "S" "Ag_ind (aΠ⇘IA) f"],
          assumption+,
       frule_tac f = h1 and a = x in aHom_mem[of "S" "Ag_ind (aΠ⇘IA) f"],
          assumption+)
apply (rule_tac x = "h x" and y = "h1 x" in ProjInd_mem_eq[of "I" "A" "f"
       "B" "S"], assumption+)
apply (rule ballI)
apply (rotate_tac 5,
       frule_tac x = j in bspec, assumption,
       thin_tac "jI. compose (carrier S) (ProjInd I A f j) h = g j",
       frule_tac x = j in bspec, assumption,
       thin_tac "jI. compose (carrier S) (ProjInd I A f j) h1 = g j")
apply (simp add:compose_def,
       subgoal_tac "(λxcarrier S. ProjInd I A f j (h x)) x = g j x",
       thin_tac "(λxcarrier S. ProjInd I A f j (h x)) = g j",
       subgoal_tac "(λxcarrier S. ProjInd I A f j (h1 x)) x = g j x",
       thin_tac "(λxcarrier S. ProjInd I A f j (h1 x)) = g j", simp+)
done

lemma  map_family_triangle2:"I  {}; jI. aGroup (A j); aGroup S;
       jI. g j  aHom S (A j); ff  carrier (aΠ⇘IA)  B;
        bij_to ff (carrier (aΠ⇘IA)) B;
        h1  aHom (Ag_ind (aΠ⇘IA) ff) S;
        jI. compos (Ag_ind (aΠ⇘IA) ff) (g j) h1 = ProjInd I A ff j;
        h2  aHom S (Ag_ind (aΠ⇘IA) ff);
        jI. compos S (ProjInd I A ff j) h2 = g j
        jI. compos (Ag_ind (aΠ⇘IA) ff) (ProjInd I A ff j)
                 (compos (Ag_ind (aΠ⇘IA) ff) h2 h1) =
                ProjInd I A ff j"
apply (rule ballI)
apply (frule prodag_aGroup[of "I" "A"])
apply (frule_tac f = ff in aGroup.Ag_ind_aGroup[of "aΠ⇘IA" _ "B"], assumption+)

apply (frule_tac N = "A j" and h = "ProjInd I A ff j" in aHom_compos_assoc[of "Ag_ind (aΠ⇘IA) ff" "S" "Ag_ind (aΠ⇘IA) ff" _ "h1" "h2"], assumption+)
 apply simp apply assumption+ apply (simp add:ProjInd_aHom)
apply simp
done

lemma  map_family_triangle3:"jI. aGroup (A j); aGroup S; aGroup S1;
       jI. f j  aHom S (A j); jI. g j  aHom S1 (A j);
        h1  aHom S1 S; h2  aHom S S1;
        jI. compos S (g j) h2 = f j;
        jI. compos S1 (f j) h1 = g j
        jI. compos S (f j) (compos S h1 h2) = f j"
apply (rule ballI)
apply (frule_tac h = "f j" and N = "A j" in aHom_compos_assoc[of "S" "S1"
                              "S" _ "h2" "h1"], assumption+)
apply simp apply assumption+ apply simp
apply simp
done

lemma map_family_triangle4:"jI. aGroup (A j); aGroup S;
                jI. f j  aHom S (A j) 
               jI. compos S (f j) (ag_idmap S) = f j"
apply (rule ballI)
apply (frule_tac x = j in bspec, assumption,
       thin_tac "jI. aGroup (A j)",
       frule_tac x = j in bspec, assumption,
       thin_tac "jI. f j  aHom S (A j)")
apply (simp add:compos_aI_r)
done

lemma  prod_triangle:"I  {}; jI. aGroup (A j); aGroup S;
       jI. g j  aHom S (A j); ff  carrier (aΠ⇘IA)  B;
        bij_to ff (carrier (aΠ⇘IA)) B;
        h1  aHom (Ag_ind (aΠ⇘IA) ff) S;
        jI. compos (Ag_ind (aΠ⇘IA) ff) (g j) h1 = ProjInd I A ff j;
        h2  aHom S (Ag_ind (aΠ⇘IA) ff);
        jI. compos S (ProjInd I A ff j) h2 = g j
        (compos (Ag_ind (aΠ⇘IA) ff) h2 h1) = ag_idmap (Ag_ind (aΠ⇘IA) ff)"
apply (frule map_family_triangle2[of "I" "A" "S" "g" "ff" "B" "h1" "h2"], assumption+)
apply (frule prodag_aGroup[of "I" "A"],
       frule aGroup.Ag_ind_aGroup[of "aΠ⇘IA" "ff" "B"], assumption+)
apply (frule aHom_compos[of "Ag_ind (aΠ⇘IA) ff" "S" "Ag_ind (aΠ⇘IA) ff" "h1"
                            "h2"], assumption+)
apply (rule ProjInd_mem_eq1[of "I" "A" "ff" "B" "S"
                            "compos (Ag_ind (aΠ⇘IA) ff) h2 h1"], assumption+)
done

lemma characterization_prodag:"I  {}; j(I::'i set). aGroup ((A j)::
    ('a, 'm) aGroup_scheme); aGroup (S::'d aGroup);
    jI. ((g j)  aHom S (A j)); ff. ff  carrier (aΠ⇘IA)  (B::'d set) 
          bij_to ff (carrier (aΠ⇘IA)) B;
    (S':: 'd aGroup). aGroup S' 
        (g'. (jI. (g' j)  aHom S' (A j) 
         (∃! f. f  aHom S' S  (jI. compos S' (g j) f =  (g' j))))) 
     h. bijec⇘(prodag I A),Sh"
apply (frule prodag_aGroup[of "I" "A"])
apply (erule exE)
apply (frule_tac f = ff in aGroup.Ag_ind_aGroup[of "aΠ⇘IA" _ "B"], erule conjE,
       assumption, simp, erule conjE)
apply (frule aGroup.Ag_ind_aGroup[of "aΠ⇘IA" _ "B"], assumption+,
       frule_tac a = S in forall_spec, assumption+)
apply (rotate_tac -1,
       frule_tac x = g in spec,
       thin_tac "g'. jI. g' j  aHom S (A j) 
              (∃!f. f  aHom S S  (jI. compos S (g j) f = g' j))")
apply (frule_tac a = "Ag_ind (aΠ⇘IA) ff" in forall_spec, assumption+,
       thin_tac "S'. aGroup S'  (g'. jI. g' j  aHom S' (A j) 
                (∃!f. f  aHom S' S  (jI. compos S' (g j) f = g' j)))")
apply (frule_tac x = "ProjInd I A ff" in spec,
       thin_tac "g'. jI. g' j  aHom (Ag_ind (aΠ⇘IA) ff) (A j) 
                     (∃!f. f  aHom (Ag_ind (aΠ⇘IA) ff) S 
                          (jI. compos (Ag_ind (aΠ⇘IA) ff) (g j) f =
                                 g' j))")
apply (frule_tac f = ff in ProjInd_aHom1[of "I" "A" _ "B"], assumption+)
apply (simp add:nonempty_ex[of "I"],
       rotate_tac -2,
       frule ex1_implies_ex,
       thin_tac "∃!f. f  aHom (Ag_ind (aΠ⇘IA) ff) S 
         (jI. compos (Ag_ind (aΠ⇘IA) ff) (g j) f = ProjInd I A ff j)",
       rotate_tac -1, erule exE, erule conjE)
apply (rename_tac ff h1,
       frule_tac f = ff in map_family_triangle1[of "I" "A" _  "B" "S" "g"],
           assumption+,
       rotate_tac -1,
       frule ex1_implies_ex,
       thin_tac "∃!h. h  aHom S (Ag_ind (aΠ⇘IA) ff) 
             (jI. compos S (ProjInd I A ff j) h = g j)",
       rotate_tac -1,
       erule exE, erule conjE)
apply (rename_tac ff h1 h2)
apply (frule_tac ff = ff and ?h1.0 = h1 and ?h2.0 = h2 in prod_triangle[of "I"
        "A" "S" "g" _ "B"], assumption+,
       frule_tac ?S1.0 = "Ag_ind (aΠ⇘IA) ff" in map_family_triangle3[of "I"
                "A" "S" _ "g"],
        assumption+,
       frule_tac f = h2 and g = h1 and M =  "Ag_ind (aΠ⇘IA) ff" in
                aHom_compos[of "S" _ "S" ], assumption+)
apply (erule ex1E)
 apply (rotate_tac -1,
        frule_tac x = "compos S h1 h2" in spec,
        frule map_family_triangle4[of "I" "A" "S" "g"], assumption+,
        frule aGroup.aI_aHom[of "S"])
 apply (frule_tac x = "aI⇘S⇙" in spec,
   thin_tac "y. y  aHom S S  (jI. compos S (g j) y = g j)  y = f",
   simp,
   thin_tac "jI. compos S (ProjInd I A ff j) h2 = g j",
   thin_tac "jI. compos S (g j) f = g j",
   thin_tac "jI. compos (Ag_ind (aΠ⇘IA) ff) (g j) h1 = ProjInd I A ff j")
 apply (rotate_tac -1, frule sym, thin_tac "aI⇘S= f", simp,
        frule_tac A = "Ag_ind (aΠ⇘IA) ff" and f = h1 and g = h2 in
         compos_aI_inj[of _ "S"], assumption+,
        frule_tac B = "Ag_ind (aΠ⇘IA) ff" and f = h2 and g = h1 in
         compos_aI_surj[of "S"], assumption+)
 apply (frule_tac f = ff in Ag_ind_bijec[of "aΠ⇘IA" _ "B"], assumption+,
        frule_tac F = "Ag_ind (aΠ⇘IA) ff" and f = "Agii (aΠ⇘IA) ff" and g = h1
           in compos_bijec[of "aΠ⇘IA" _ "S"], assumption+)
apply (subst bijec_def, simp)
 apply (thin_tac "bijec⇘(aΠ⇘IA),Ag_ind (aΠ⇘IA) ffAgii (aΠ⇘IA) ff",
        thin_tac "injec⇘Ag_ind (aΠ⇘IA) ff,Sh1",
        thin_tac "surjec⇘Ag_ind (aΠ⇘IA) ff,Sh1")
apply (rule exI, simp)
done

(***  Note.
                                     f
                                  S' → S
                                    \   |
                                 g' j\  | g j
                                      \ |
                                        A j

       ***)



chapter "Ring theory"

section "Definition of a ring and an ideal"

record 'a Ring = "'a aGroup" +
  tp ::  "['a, 'a ]  'a" (infixl "rı" 70)
  un :: "'a"   ("1rı")

locale Ring =
 fixes R (structure)

 assumes
         pop_closed: "pop R  carrier R  carrier R  carrier R"
 and     pop_aassoc : "a  carrier R; b  carrier R; c  carrier R 
         (a ± b) ± c = a ± (b ± c)"
 and     pop_commute:"a  carrier R; b  carrier R  a ± b = b ± a"
 and     mop_closed:"mop R  carrier R  carrier R"
 and     l_m :"a  carrier R   (-a a) ± a = 𝟬"
 and     ex_zero: "𝟬  carrier R"
 and     l_zero:"a  carrier R  𝟬 ± a = a"
 and     tp_closed: "tp R  carrier R  carrier R  carrier R"
 and     tp_assoc : "a  carrier R; b  carrier R; c  carrier R 
                  (a r b) r c = a r (b r c)"
 and     tp_commute: "a  carrier R; b  carrier R  a r b = b  r a"
 and     un_closed: "(1r)  carrier R"
 and     rg_distrib: "a  carrier R; b  carrier R; c  carrier R 
                     a r (b ± c) = a r b  ±  a r c"
 and     rg_l_unit: "a  carrier R  (1r) r a = a"

definition
  zeroring :: "('a, 'more) Ring_scheme  bool" where
  "zeroring R  Ring R  carrier R = {𝟬R}"

primrec nscal ::  "('a, 'more) Ring_scheme  => 'a => nat  => 'a"
where
  nscal_0:  "nscal R x 0 = 𝟬R⇙"
| nscal_suc:  "nscal R x (Suc n) = (nscal R x n) ±Rx"

primrec npow ::  "('a, 'more) Ring_scheme  => 'a => nat  => 'a"
where
  npow_0: "npow R x 0 = 1rR⇙"
| npow_suc: "npow R x (Suc n) = (npow R x n) rRx"

primrec nprod  :: "('a, 'more) Ring_scheme => (nat => 'a) => nat => 'a"
where
  nprod_0: "nprod R f 0 = f 0"
| nprod_suc: "nprod R f (Suc n) = (nprod R f n) rR(f (Suc n))"

primrec nsum :: "('a, 'more) aGroup_scheme => (nat => 'a) => nat => 'a"
where
  nsum_0: "nsum R f 0 = f 0"
| nsum_suc: "nsum R f (Suc n) = (nsum R f n) ±R(f (Suc n))"

abbreviation
  NSCAL :: "[nat, ('a, 'more) Ring_scheme, 'a]  'a"
    ("(3 _ ×⇘_ _)" [75,75,76]75) where
  "n ×⇘Rx == nscal R x n"

abbreviation
  NPOW :: "['a, ('a, 'more) Ring_scheme, nat]   'a"
    ("(3_^⇗_ _)" [77,77,78]77) where
  "a^⇗R n== npow R a n"

abbreviation
  SUM :: "('a, 'more) aGroup_scheme => (nat => 'a) => nat => 'a"
    ("(3Σe _ _ _)" [85,85,86]85) where
  "Σe G f n == nsum G f n"

abbreviation
  NPROD :: "[('a, 'm) Ring_scheme, nat, nat  'a]  'a"
    ("(3eΠ⇘_,_ _)" [98,98,99]98) where
  "eΠ⇘R,nf == nprod R f n"

definition
  fSum :: "[_, (nat => 'a), nat, nat]  'a" where
  "fSum A f n m = (if n  m then nsum A (cmp f (slide n))(m - n)
                       else 𝟬A)"

abbreviation
  FSUM :: "[('a, 'more) aGroup_scheme, (nat  'a), nat, nat]  'a"
    ("(4Σf _ _ _ _)" [85,85,85,86]85) where
  "Σf G f n m == fSum G f n m"

lemma (in aGroup) nsum_zeroGTr:"(j  n. f j = 𝟬)  nsum A f n = 𝟬"
apply (induct_tac n)
 apply (rule impI, simp)

apply (rule impI)
apply (cut_tac n = n in Nsetn_sub_mem1, simp)
apply (cut_tac ex_zero)
apply (simp add:l_zero[of 𝟬])
done

lemma (in aGroup) nsum_zeroA:"j  n. f j = 𝟬    nsum A f n = 𝟬"
apply (simp add:nsum_zeroGTr)
done

definition
  sr :: "[_ , 'a set]  bool" where
  "sr R S == S  carrier R  1rR S  (xS. y  S. x  ±R(-aRy)  S 
               x rRy  S)"

definition
  Sr :: "[_ , 'a set]  _" where
  "Sr R S = R carrier := S, pop := λxS. λyS. x ±Ry, mop := λxS. (-aRx),
    zero := 𝟬R, tp := λxS. λyS. x rRy, un := 1rR"

(** sr is a subring without ring structure, Sr is a subring with Ring structure
     **)


lemma (in Ring) Ring: "Ring R" ..

lemma (in Ring) ring_is_ag:"aGroup R"
apply (rule aGroup.intro,
       rule pop_closed,
       rule pop_aassoc, assumption+,
       rule pop_commute, assumption+,
       rule mop_closed,
       rule l_m, assumption,
       rule ex_zero,
       rule l_zero, assumption)
done

lemma (in Ring) ring_zero:"𝟬  carrier R"
by (simp add: ex_zero)

lemma (in Ring) ring_one:"1r  carrier R"
by (simp add:un_closed)

lemma (in Ring) ring_tOp_closed:" x  carrier R; y  carrier R 
                     x r y  carrier R"
apply (cut_tac tp_closed)
 apply (frule funcset_mem[of "(⋅r)" "carrier R" "carrier R  carrier R"
            "x"], assumption+,
        thin_tac "(⋅r)  carrier R  carrier R  carrier R")
 apply (rule funcset_mem[of "(⋅r) x" "carrier R" "carrier R" "y"],
              assumption+)
done

lemma (in Ring) ring_tOp_commute:"x  carrier R; y  carrier R 
                x r y = y r x"
by (simp add:tp_commute)

lemma (in Ring) ring_distrib1:"x  carrier R; y  carrier R; z  carrier R 
                  x r (y ± z) = x r y ± x r z"
by (simp add:rg_distrib)

lemma (in Ring) ring_distrib2:"x  carrier R; y  carrier R; z  carrier R 
                 (y ± z) r x = y r x ±  z r x"
apply (subst tp_commute[of "y ± z" "x"])
 apply (cut_tac ring_is_ag, simp add:aGroup.ag_pOp_closed)
 apply assumption
apply (subst ring_distrib1, assumption+)
 apply (simp add:tp_commute)
done

lemma (in Ring) ring_distrib3:"a  carrier R; b  carrier R; x  carrier R;
      y  carrier R   (a ± b) r (x ± y) =
                                          a r x ± a r y ± b r x ± b r y"
apply (subst ring_distrib2)+
 apply (cut_tac ring_is_ag)
 apply (rule aGroup.ag_pOp_closed, assumption+)
 apply ((subst ring_distrib1)+, assumption+)
 apply (subst ring_distrib1, assumption+)
 apply (rule pop_aassoc [THEN sym, of "a r x ± a r y" "b r x" "b r y"])
 apply (cut_tac ring_is_ag, rule aGroup.ag_pOp_closed, assumption)
 apply (simp add:ring_tOp_closed)+
done

lemma (in Ring) rEQMulR:
  "x  carrier R; y  carrier R; z  carrier R; x = y 
         x r z = y r z"
by simp

lemma (in Ring) ring_tOp_assoc:"x  carrier R; y  carrier R; z  carrier R 
  (x r y) r z = x r (y r z)"
by (simp add:tp_assoc)

lemma (in Ring) ring_l_one:"x  carrier R  1r r x = x"
by (simp add:rg_l_unit)

lemma (in Ring) ring_r_one:"x  carrier R   x r 1r = x"
 apply (subst ring_tOp_commute, assumption+)
 apply (simp add:un_closed)
 apply (simp add:ring_l_one)
done

lemma (in Ring) ring_times_0_x:"x  carrier R  𝟬 r x = 𝟬"
apply (cut_tac ring_is_ag)
apply (cut_tac ring_zero)
apply (frule ring_distrib2 [of "x" "𝟬" "𝟬"], assumption+)
apply (simp add:aGroup.ag_l_zero [of "R" "𝟬"])
apply (frule ring_tOp_closed [of "𝟬" "x"], assumption+)
apply (frule sym, thin_tac "𝟬 r x = 𝟬 r x ± 𝟬 r x")
apply (frule aGroup.ag_eq_sol2 [of "R" "𝟬 r x" "𝟬 r x" "𝟬 r x"],
        assumption+)
apply (thin_tac "𝟬 r x ± 𝟬 r x = 𝟬 r x")
apply (simp add:aGroup.ag_r_inv1)
done

lemma (in Ring) ring_times_x_0:"x  carrier R   x r 𝟬 = 𝟬"
apply (cut_tac ring_zero)
apply (subst ring_tOp_commute, assumption+, simp add:ring_zero)
apply (simp add:ring_times_0_x)
done

lemma (in Ring) rMulZeroDiv:
     " x  carrier R; y  carrier R; x = 𝟬  y = 𝟬   x  r  y = 𝟬"
apply (erule disjE, simp)
apply (rule ring_times_0_x, assumption+)
apply (simp, rule ring_times_x_0, assumption+)
done

lemma (in Ring) ring_inv1:" a  carrier R; b  carrier R  
      -a (a r b) = (-a a) r b  -a (a r b) = a r (-a b)"
apply (cut_tac ring_is_ag)
apply (rule conjI)
apply (frule ring_distrib2 [THEN sym, of "b" "a" "-a a"], assumption+)
 apply (frule aGroup.ag_mOp_closed [of "R" "a"], assumption+)
 apply (simp add:aGroup.ag_r_inv1 [of "R" "a"])
 apply (simp add:ring_times_0_x)
 apply (frule aGroup.ag_mOp_closed [of "R" "a"], assumption+)
 apply (frule ring_tOp_closed [of "a" "b"], assumption+)
 apply (frule ring_tOp_closed [of "-a a" "b"], assumption+)
 apply (frule aGroup.ag_eq_sol1 [of "R" "a r b" "(-a a) r b" "𝟬"],
           assumption+)
 apply (rule ring_zero, assumption+)
 apply (thin_tac "a r b ± (-a a) r b = 𝟬")
 apply (frule sym) apply (thin_tac "(-a a) r b = -a (a r b) ± 𝟬")
 apply (frule aGroup.ag_mOp_closed [of "R" " a r b"], assumption+)
 apply (simp add:aGroup.ag_r_zero)
apply (frule ring_distrib1 [THEN sym, of "a" "b" "-a b"], assumption+)
 apply (simp add:aGroup.ag_mOp_closed)
  apply (simp add:aGroup.ag_r_inv1 [of "R" "b"])
  apply (simp add:ring_times_x_0)
 apply (frule aGroup.ag_mOp_closed [of "R" "b"], assumption+)
 apply (frule ring_tOp_closed [of "a" "b"], assumption+)
 apply (frule ring_tOp_closed [of "a" "-a b"], assumption+)
 apply (frule aGroup.ag_eq_sol1 [THEN sym, of "R" "a r b" "a r (-a b)" "𝟬"],
                                                      assumption+)
 apply (simp add:ring_zero) apply assumption
 apply (frule aGroup.ag_mOp_closed [of "R" " a r b"], assumption+)
  apply (simp add:aGroup.ag_r_zero)
done

lemma (in Ring) ring_inv1_1:"a  carrier R; b  carrier R  
      -a (a r b) = (-a a) r b"
apply (simp add:ring_inv1)
done

lemma (in Ring) ring_inv1_2:" a  carrier R; b  carrier R  
                                -a (a r b) = a r (-a b)"
apply (frule ring_inv1 [of "a" "b"], assumption+)
apply (frule conjunct2)
apply (thin_tac "-a a r b = (-a a) r b  -a (a r b) = a r (-a b)")
apply simp
done

lemma (in Ring) ring_times_minusl:"a  carrier R   -a a = (-a 1r) r a"
apply (cut_tac ring_one)
apply (frule ring_inv1_1[of "1r" "a"], assumption+)
apply (simp add:ring_l_one)
done

lemma (in Ring) ring_times_minusr:"a  carrier R   -a a = a r (-a 1r)"
apply (cut_tac ring_one)
apply (frule ring_inv1_2[of "a" "1r"], assumption+)
apply (simp add:ring_r_one)
done

lemma (in Ring) ring_inv1_3:"a  carrier R; b  carrier R 
                           a r b = (-a a) r (-a b)"
apply (cut_tac ring_is_ag)
apply (subst  aGroup.ag_inv_inv[THEN sym], assumption+)
apply (frule aGroup.ag_mOp_closed[of "R" "a"], assumption+)
apply (subst ring_inv1_1[THEN sym, of "-a a" "b"], assumption+)
apply (subst ring_inv1_2[of "-a a" "b"], assumption+, simp)
done

lemma (in Ring) ring_distrib4:"a  carrier R; b  carrier R;
                                x  carrier R; y  carrier R  
      a r b ± (-a x r y) = a r (b ± (-a y)) ± (a ± (-a x)) r y"
apply (cut_tac ring_is_ag)
apply (subst ring_distrib1, assumption+)
apply (rule aGroup.ag_mOp_closed, assumption+)
apply (subst ring_distrib2, assumption+)
apply (rule aGroup.ag_mOp_closed, assumption+)
apply (subst aGroup.pOp_assocTr43, assumption+)
apply (rule ring_tOp_closed, assumption+)+
 apply (rule aGroup.ag_mOp_closed, assumption+)
 apply (rule ring_tOp_closed, assumption+)
 apply (rule ring_tOp_closed)
 apply (simp add:aGroup.ag_mOp_closed)+
apply (subst ring_distrib1 [THEN sym, of "a" _], assumption+)
 apply (rule aGroup.ag_mOp_closed, assumption+)
apply (simp add:aGroup.ag_l_inv1)
apply (simp add:ring_times_x_0)
apply (subst aGroup.ag_r_zero, assumption+)
apply (simp add:ring_tOp_closed)
apply (simp add: ring_inv1_1)
done

lemma (in Ring) rMulLC:
     "x  carrier R; y  carrier R; z  carrier R
         x r (y r z) = y r (x r z)"
  apply (subst ring_tOp_assoc [THEN sym], assumption+)
  apply (subst ring_tOp_commute [of "x" "y"], assumption+)
  apply (subst ring_tOp_assoc, assumption+)
  apply simp
  done

lemma (in Ring) Zero_ring:"1r = 𝟬  zeroring R"
apply (simp add:zeroring_def)
apply (rule conjI)
 apply (rule Ring_axioms)
apply (rule equalityI)
 apply (rule subsetI)
 apply (frule_tac x = x in ring_r_one, simp add:ring_times_x_0)

 apply (simp add:ring_zero)
done

lemma (in Ring) Zero_ring1:"¬ (zeroring R)   1r  𝟬"
apply (rule contrapos_pp, simp+,
       cut_tac Zero_ring, simp+)
done

lemma (in Ring) Sr_one:"sr R S  1r  S"
apply (simp add:sr_def)
done

lemma (in Ring) Sr_zero:"sr R S  𝟬  S"
apply (cut_tac ring_is_ag, frule Sr_one[of "S"])
apply (simp add:sr_def) apply (erule conjE)+
apply (frule_tac x = "1r" in bspec, assumption,
       thin_tac "xS. yS. x ± -a y  S  x r y  S",
       frule_tac x = "1r" in bspec, assumption,
       thin_tac "yS. 1r ± -a y  S  1r r y  S",
       erule conjE)
apply (cut_tac ring_one,
       simp add:aGroup.ag_r_inv1[of "R" "1r"])
done

lemma (in Ring) Sr_mOp_closed:"sr R S; x  S  -a x  S"
apply (frule Sr_zero[of "S"])
apply (simp add:sr_def, (erule conjE)+)
apply (cut_tac ring_is_ag)
 apply (frule_tac x = "𝟬" in bspec, assumption,
        thin_tac "xS. yS. x ± -a y  S  x r y  S",
        frule_tac x = x in bspec, assumption,
        thin_tac "yS. 𝟬 ± -a y  S  𝟬 r y  S", erule conjE)
 apply (frule subsetD[of "S" "carrier R" "𝟬"], assumption+,
        frule subsetD[of "S" "carrier R" "x"], assumption+)
 apply (frule aGroup.ag_mOp_closed [of "R" "x"], assumption)
 apply (simp add:aGroup.ag_l_zero)
done

lemma (in Ring) Sr_pOp_closed:"sr R S; x  S; y  S  x ± y  S"
apply (frule Sr_mOp_closed[of "S" "y"], assumption+)
apply (unfold sr_def, (erule conjE)+)
 apply (frule_tac x = x in bspec, assumption,
        thin_tac "xS. yS. x ± -a y  S  x r y  S",
        frule_tac x = "-a y" in bspec, assumption,
        thin_tac "yS. x ± -a y  S  x r y  S", erule conjE)

 apply (cut_tac ring_is_ag )
 apply (frule subsetD[of "S" "carrier R" "y"], assumption+)
 apply (simp add:aGroup.ag_inv_inv)
done

lemma (in Ring) Sr_tOp_closed:"sr R S; x  S; y  S  x r y  S"
by (simp add:sr_def)

lemma (in Ring) Sr_ring:"sr R S  Ring (Sr R S)"
apply (simp add:Ring_def [of "Sr R S"],
       cut_tac ring_is_ag)
 apply (rule conjI)
 apply (simp add:Sr_def Sr_pOp_closed)

apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (simp add:Sr_def,
        frule_tac x = a and y = b in Sr_pOp_closed, assumption+,
        frule_tac x = b and y = c in Sr_pOp_closed, assumption+,
        simp add:Sr_def sr_def, (erule conjE)+)
 apply (frule_tac c = a in subsetD[of "S" "carrier R"], assumption+,
        frule_tac c = b in subsetD[of "S" "carrier R"], assumption+,
        frule_tac c = c in subsetD[of "S" "carrier R"], assumption+)
 apply (simp add:aGroup.ag_pOp_assoc)

apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (simp add:Sr_def sr_def, (erule conjE)+,
        frule_tac c = a in subsetD[of "S" "carrier R"], assumption+,
        frule_tac c = b in subsetD[of "S" "carrier R"], assumption+)
 apply (simp add:aGroup.ag_pOp_commute)

apply (rule conjI)
  apply ((subst Sr_def)+, simp)
  apply (simp add:Sr_mOp_closed)

apply (rule conjI)
  apply (rule allI)
  apply ((subst Sr_def)+, simp add:Sr_mOp_closed, rule impI)
  apply (unfold sr_def, frule conjunct1, fold sr_def,
         frule_tac c = a in subsetD[of "S" "carrier R"], assumption+,
         simp add:aGroup.ag_l_inv1)

apply (rule conjI)
  apply (simp add:Sr_def Sr_zero)

apply (rule conjI)
  apply (rule allI, simp add:Sr_def Sr_zero)
  apply (rule impI)
  apply (unfold sr_def, frule conjunct1, fold sr_def,
         frule_tac c = a in subsetD[of "S" "carrier R"], assumption+,
         simp add:aGroup.ag_l_zero)

apply (rule conjI)
  apply (simp add:Sr_def Sr_tOp_closed)

apply (rule conjI)
  apply (rule allI, rule impI)+
  apply (simp add:Sr_def Sr_tOp_closed)
  apply (unfold sr_def, frule conjunct1, fold sr_def,
         frule_tac c = a in subsetD[of "S" "carrier R"], assumption+,
         frule_tac c = b in subsetD[of "S" "carrier R"], assumption+,
         frule_tac c = c in subsetD[of "S" "carrier R"], assumption+)
  apply (simp add:ring_tOp_assoc)

apply (rule conjI)
  apply ((rule allI, rule impI)+, simp add:Sr_def)
  apply (unfold sr_def, frule conjunct1, fold sr_def,
         frule_tac c = a in subsetD[of "S" "carrier R"], assumption+,
         frule_tac c = b in subsetD[of "S" "carrier R"], assumption+,
         simp add:ring_tOp_commute)

apply (rule conjI)
  apply (simp add:Sr_def Sr_one)

apply (rule conjI)
  apply (simp add:Sr_def Sr_pOp_closed Sr_tOp_closed)
  apply (rule allI, rule impI)+
  apply (unfold sr_def, frule conjunct1, fold sr_def,
         frule_tac c = a in subsetD[of "S" "carrier R"], assumption+,
         frule_tac c = b in subsetD[of "S" "carrier R"], assumption+,
         frule_tac c = c in subsetD[of "S" "carrier R"], assumption+)
  apply (simp add:ring_distrib1)

apply (simp add:Sr_def Sr_one)
 apply (rule allI, rule impI)
   apply (unfold sr_def, frule conjunct1, fold sr_def,
         frule_tac c = a in subsetD[of "S" "carrier R"], assumption+)
 apply (simp add:ring_l_one)
done


section "Calculation of elements"
 (** The author of this part is L. Chen, revised by H. Murao and Y.
     Santo  **)

subsection "nscale"

lemma (in Ring) ring_tOp_rel:"xcarrier R; xacarrier R; ycarrier R;
ya  carrier R   (x r xa) r (y r ya) = (x r y) r (xa r ya)"
apply (frule ring_tOp_closed[of "y" "ya"], assumption+,
       simp add:ring_tOp_assoc[of "x" "xa"])
apply (simp add:ring_tOp_assoc[THEN sym, of "xa" "y" "ya"],
       simp add:ring_tOp_commute[of "xa" "y"],
       simp add:ring_tOp_assoc[of "y" "xa" "ya"])
apply (frule ring_tOp_closed[of "xa" "ya"], assumption+,
       simp add:ring_tOp_assoc[THEN sym, of "x" "y"])
done

lemma (in Ring) nsClose:
  " n.  x  carrier R    nscal R x n  carrier R"
  apply (induct_tac n)
  apply (simp add:ring_zero)
  apply (cut_tac ring_is_ag, simp add:aGroup.ag_pOp_closed)
done

lemma (in Ring) nsZero:
             "nscal R 𝟬 n = 𝟬"
  apply (cut_tac ring_is_ag)
  apply (induct_tac n)
  apply simp

  apply simp
   apply (cut_tac ring_zero, simp add:aGroup.ag_l_zero)
  done

lemma (in Ring) nsZeroI: " n.  x = 𝟬   nscal R x n = 𝟬"
  by (simp only:nsZero)

lemma (in Ring) nsEqElm:  " x  carrier R; y  carrier R; x = y 
         (nscal R x n) = (nscal R y n)"
  by simp

lemma (in Ring) nsDistr:  "x  carrier R
         (nscal R x n) ± (nscal R x m) = nscal R x (n + m)"
apply (cut_tac ring_is_ag)
  apply (induct_tac m)
  apply simp
  apply (frule nsClose[of "x" "n"])
  apply ( simp add:aGroup.ag_r_zero)

  apply simp
  apply (frule_tac x = x and n = n in nsClose,
         frule_tac x = x and n = na in nsClose)
  apply (subst aGroup.ag_pOp_assoc[THEN sym], assumption+, simp)
  done

lemma (in Ring) nsDistrL:  "x  carrier R; y  carrier R 
         (nscal R x n) ± (nscal R y n) = nscal R (x ± y) n"
  apply (cut_tac ring_is_ag)
  apply (induct_tac n)
  apply simp
  apply (cut_tac ring_zero,
         simp add:aGroup.ag_l_zero)

  apply simp
  apply (frule_tac x = x and n = n in nsClose,
         frule_tac x = y and n = n in nsClose)
  apply (subst aGroup.pOp_assocTr43[of R _ x _ y], assumption+)
  apply (frule_tac x = x and y = "n ×⇘Ry" in aGroup.ag_pOp_commute[of "R"],
         assumption+)
   apply simp
   apply (subst aGroup.pOp_assocTr43[THEN sym, of R _ _ x y], assumption+)
   apply simp
done

lemma (in Ring) nsMulDistrL:" x  carrier R; y  carrier R 
         x r (nscal R y n) = nscal R (x r y) n"
  apply (induct_tac n)
  apply simp
  apply (simp add:ring_times_x_0)

  apply simp apply (subst ring_distrib1, assumption+)
  apply (rule nsClose, assumption+)
  apply simp
done

lemma (in Ring) nsMulDistrR:" x  carrier R; y  carrier R
         (nscal R y n) r x = nscal R (y r x) n"
  apply (frule_tac x = y and n = n in nsClose,
         simp add:ring_tOp_commute[of "n ×⇘Ry" "x"],
         simp add:nsMulDistrL,
         simp add:ring_tOp_commute[of "y" "x"])
done

subsection "npow"

lemma (in Ring) npClose:"x  carrier R  npow R x n  carrier R"
  apply (induct_tac n)
  apply simp apply (simp add:ring_one)

  apply simp
  apply (rule ring_tOp_closed, assumption+)
  done

lemma (in Ring) npMulDistr:" n m. x  carrier R  
                 (npow R x n) r (npow R x m) = npow R x (n + m)"
  apply (induct_tac m)
  apply simp apply (rule ring_r_one, simp add:npClose)

  apply simp
  apply (frule_tac x = x and n = n in npClose,
         frule_tac x = x and n = na in npClose)
  apply (simp add:ring_tOp_assoc[THEN sym])
done

lemma (in Ring) npMulExp:"n m. x  carrier R
          npow R (npow R x n) m = npow R x (n * m)"
apply (induct_tac m)
apply simp
apply simp
apply (simp add:npMulDistr)
apply (simp add:add.commute)
done


lemma (in Ring) npGTPowZero_sub:
  "  n.  x  carrier R; npow R x m = 𝟬 
        (m  n)  (npow R x n = 𝟬 )"
  apply (rule impI)
  apply (subgoal_tac "npow R x n = (npow R x (n-m)) r (npow R x m)")
  apply simp
  apply (rule ring_times_x_0) apply (simp add:npClose)
  apply (thin_tac "x^⇗R m= 𝟬")
  apply (subst npMulDistr, assumption)
  apply simp
  done

lemma (in Ring) npGTPowZero:
  " n.  x  carrier R; npow R x m = 𝟬; m  n 
         npow R x n = 𝟬"
  apply (cut_tac x = x and m = m and n = n in npGTPowZero_sub, assumption+)
  apply simp
  done


lemma (in Ring) npOne: " npow R (1r) n = 1r"
  apply (induct_tac n) apply simp

  apply simp
    apply (rule ring_r_one, simp add:ring_one)
done

lemma (in Ring) npZero_sub: "0 < n  npow R 𝟬 n = 𝟬"
  apply (induct_tac "n")
  apply simp

  apply simp
    apply (cut_tac ring_zero,
           frule_tac n = n in npClose[of "𝟬"])
    apply (simp add:ring_times_x_0)
done

lemma (in Ring) npZero: "0 < n   npow R 𝟬 n = 𝟬"
  apply (simp add:npZero_sub)
done

lemma (in Ring) npMulElmL: " n.  x  carrier R; 0  n
         x r (npow R x n) = npow R x (Suc n)"
apply (simp only:npow_suc,
       frule_tac n = n and x = x in npClose,
       simp add:ring_tOp_commute)
done

lemma (in Ring) npMulEleL: " n. x  carrier R
         (npow R x n) r x =  npow R x (Suc n)"
by (simp add:npMulElmL[THEN sym])

lemma (in Ring) npMulElmR: " n. x  carrier R
         (npow R x n) r x =  npow R x (Suc n)"
  apply ( frule_tac n = n in npClose[of "x"])
   apply (simp only:ring_tOp_commute,
          subst npMulElmL, assumption, simp, simp)
  done

lemma (in Ring) np_1:"a  carrier R  npow R a (Suc 0) = a"  (* Y. Santo*)
apply simp
 apply (simp add:ring_l_one)
done

subsection  "nsum and fSum"

lemma (in aGroup) nsum_memTr: "(j  n. f j  carrier A) 
                                 nsum A f n  carrier A"
  apply (induct_tac "n")
  apply simp
  apply (rule impI)
  apply (cut_tac n = n in Nsetn_sub_mem1, simp)
  apply (frule_tac a = "Suc n" in forall_spec, simp,
         thin_tac "jSuc n. f j  carrier A")
   apply (rule ag_pOp_closed, assumption+)
   done

lemma (in aGroup) nsum_mem:"j  n. f j  carrier A 
                                 nsum A f n  carrier A"
apply (simp add:nsum_memTr)
done

lemma (in aGroup) nsum_eqTr:"(j  n. f j  carrier A 
                                      g j  carrier A 
                                      f j = g j)
                             nsum A f n = nsum A g n"
apply (induct_tac n)
 apply simp
apply (rule impI)
 apply (cut_tac n = n in Nsetn_sub_mem1, simp)
done

lemma (in aGroup) nsum_eq:"j  n. f j  carrier A; j  n. g j  carrier A;
                           j  n. f j = g j   nsum A f n = nsum A g n"
by (simp add:nsum_eqTr)

lemma (in aGroup) nsum_cmp_assoc:"j  n. f j  carrier A;
       g  {j. j  n}  {j. j  n}; h  {j. j  n}  {j. j  n} 
       nsum A (cmp (cmp f h) g) n = nsum A (cmp f (cmp h g)) n"
apply (rule nsum_eq)
apply (rule allI, rule impI, simp add:cmp_def)
apply (frule_tac x = j in funcset_mem[of g "{j. j  n}" "{j. j  n}"], simp,
       frule_tac x = "g j" in funcset_mem[of h "{j. j  n}" "{j. j  n}"],
       assumption, simp)
 apply (rule allI, rule impI, simp add:cmp_def,
       frule_tac x = j in funcset_mem[of g "{j. j  n}" "{j. j  n}"], simp,
       frule_tac x = "g j" in funcset_mem[of h "{j. j  n}" "{j. j  n}"],
       assumption, simp)
 apply (rule allI, simp add:cmp_def)
done

lemma (in aGroup) fSum_Suc:"j  nset n (n + Suc m). f j  carrier A 
              fSum A f n (n + Suc m) = fSum A f n (n + m) ± f (n + Suc m)"
by (simp add:fSum_def, simp add:cmp_def slide_def)

lemma (in aGroup) fSum_eqTr:"(j  nset n (n + m). f j  carrier A 
         g j  carrier A   f j = g j)  
                       fSum A f  n (n + m) = fSum A g n (n + m)"
apply (induct_tac m)
 apply (simp add:fSum_def,
        simp add:cmp_def slide_def,
        simp add:nset_def)

apply (rule impI)
 apply (subst fSum_Suc,
        rule ballI, simp, simp)
 apply (cut_tac n = n and m = na and f = g in fSum_Suc,
        rule ballI, simp, simp,
        thin_tac "Σf A g n (Suc (n + na)) =
                                   Σf A g n (n + na) ± g (Suc (n + na))")

 apply (cut_tac n = n and m = na in nsetnm_sub_mem, simp,
        thin_tac "j. j  nset n (n + na)  j  nset n (Suc (n + na))")
apply (frule_tac x = "Suc (n + na)" in bspec,
       simp add:nset_def, simp)
done

lemma (in aGroup) fSum_eq:" j  nset n (n + m). f j  carrier A;
      j  nset n (n + m). g j  carrier A; (j nset n (n + m). f j = g j)
       
         fSum A f n (n + m) = fSum A g n (n + m)"
by (simp add:fSum_eqTr)

lemma (in aGroup) fSum_eq1:"n  m; jnset n m. f j  carrier A;
       jnset n m. g j  carrier A;  jnset n m. f j = g j 
         fSum A f n m = fSum A g n m"
apply (cut_tac fSum_eq[of n "m - n" f g])
apply simp+
done

lemma (in aGroup) fSum_zeroTr:"(j  nset n (n + m). f j = 𝟬)  
                       fSum A f  n (n + m) = 𝟬"
apply (induct_tac m)
 apply (simp add:fSum_def cmp_def slide_def nset_def)
 apply (rule impI)
 apply (subst fSum_Suc)
 apply (rule ballI, simp add:ag_inc_zero)
apply (frule_tac x = "n + Suc na" in bspec, simp add:nset_def,
       simp)
 apply (simp add:nset_def)
 apply (cut_tac ag_inc_zero, simp add:ag_l_zero)
done

lemma (in aGroup) fSum_zero:"j  nset n (n + m). f j = 𝟬  
                       fSum A f  n (n + m) = 𝟬"
by (simp add:fSum_zeroTr)

lemma (in aGroup) fSum_zero1:"n < m; j  nset (Suc n) m. f j = 𝟬  
                       fSum A f  (Suc n) m = 𝟬"
apply (cut_tac fSum_zero[of "Suc n" "m - Suc n" f])
 apply simp+
done

lemma (in Ring) nsumMulEleL: " n.   i. f i  carrier R; x  carrier R 
         x r (nsum R f n) = nsum R (λ i. x r (f i)) n"
  apply (cut_tac ring_is_ag)
  apply (induct_tac "n")
  apply simp

  apply simp
  apply (subst ring_distrib1, assumption)
  apply (rule aGroup.nsum_mem, assumption)
 apply (rule allI, simp+)
done

lemma (in Ring) nsumMulElmL:
  " n.   i. f i  carrier R; x  carrier R 
         x r (nsum R f n) = nsum R (λ i. x r (f i)) n"
  apply (cut_tac ring_is_ag)
  apply (induct_tac "n")
  apply simp

  apply simp
  apply (subst ring_distrib1, assumption+)
    apply (simp add:aGroup.nsum_mem)+
  done

lemma (in aGroup) nsumTailTr:
         "(j(Suc n). f j  carrier A) 
          nsum A f (Suc n) = (nsum A (λ i. (f (Suc i))) n) ± (f 0)"
  apply (induct_tac "n")
  apply simp
  apply (rule impI,
         rule ag_pOp_commute)
  apply (cut_tac Nset_inc_0[of "Suc 0"],
         simp add:Pi_def,
         cut_tac n_in_Nsetn[of "Suc 0"],
         simp add:Pi_def)

  apply (rule impI)
   apply (cut_tac n = "Suc n" in Nsetn_sub_mem1, simp)
   apply (frule_tac a = 0 in forall_spec, simp,
          frule_tac a = "Suc (Suc n)" in forall_spec, simp)
    apply (cut_tac n = n in nsum_mem[of  _  "λi. f (Suc i)"],
          rule allI, rule impI,
          frule_tac a = "Suc j" in forall_spec, simp, simp,
          thin_tac "jSuc (Suc n). f j  carrier A")
    apply (subst ag_pOp_assoc, assumption+)
       apply (simp add:ag_pOp_commute[of  "f 0"])
    apply (subst ag_pOp_assoc[THEN sym], assumption+)
    apply simp
  done

lemma (in aGroup) nsumTail:
      "j  (Suc n). f j  carrier A 
            nsum A f (Suc n) = (nsum A (λ i. (f (Suc i))) n) ± (f 0)"
  by (cut_tac nsumTailTr[of n f], simp)

lemma (in aGroup) nsumElmTail:
  "i. f i  carrier A
         nsum A f (Suc n) = (nsum A (λ i. (f (Suc i))) n) ± (f 0)"
  apply (cut_tac n = n and f = f in nsumTail,
         rule allI, simp, simp)
done

lemma (in aGroup) nsum_addTr:
  "(j  n. f j  carrier A  g j  carrier A) 
   nsum A (λ i. (f i) ± (g i)) n = (nsum A f n) ± (nsum A g n)"
  apply (induct_tac "n")
  apply simp

  apply (simp, rule impI)
  apply (cut_tac n = n in Nsetn_sub_mem1, simp)
 apply (thin_tac "Σe A (λi. f i ± g i) n = Σe A f n ± Σe A g n")
  apply (rule aGroup.ag_add4_rel, rule aGroup_axioms)
  apply (rule aGroup.nsum_mem, rule aGroup_axioms, rule allI, simp)
  apply (rule aGroup.nsum_mem, rule aGroup_axioms, rule allI, simp)
  apply simp+
  done

lemma (in aGroup) nsum_add:
  " j  n. f j  carrier A; j  n. g j  carrier A  
   nsum A (λ i. (f i) ± (g i)) n = (nsum A f n) ± (nsum A g n)"
by (cut_tac nsum_addTr[of n f g], simp)

lemma (in aGroup) nsumElmAdd:
  "  i. f i  carrier A;  i. g i  carrier A
         nsum A (λ i. (f i) ± (g i)) n = (nsum A f n) ± (nsum A g n)"
 apply (cut_tac nsum_add[of n f g])
 apply simp
 apply (rule allI, simp)+
 done

lemma (in aGroup) nsum_add_nmTr:
  "(j  n. f j  carrier A)  (j  m. g j  carrier A) 
   nsum A (jointfun n f m g) (Suc (n + m)) = (nsum A f n) ± (nsum A g m)"
apply (induct_tac m)
 apply (simp add:jointfun_def sliden_def)
 apply (rule impI)
 apply (rule ag_pOp_add_r)
 apply (rule nsum_mem, rule allI, erule conjE, rule impI, simp)
 apply (erule conjE, simp add:nsum_mem, simp)
 apply (rule nsum_eq[of n], simp+)
apply (simp add:jointfun_def)
 apply (rule impI, simp)
 apply (erule conjE, simp add:sliden_def)
 apply (thin_tac "Σe A (λi. if i  n then f i else g (sliden (Suc n) i))
        (n + na) ± g na = Σe A f n ± Σe A g na")
 apply (subst ag_pOp_assoc)
 apply (simp add:nsum_mem)
 apply (simp add:nsum_mem, simp)
 apply simp
done

lemma (in aGroup) nsum_add_nm:
"j  n. f j  carrier A; j  m. g j  carrier A 
   nsum A (jointfun n f m g) (Suc (n + m)) = (nsum A f n) ± (nsum A g m)"
apply (cut_tac nsum_add_nmTr[of n f m g])
 apply simp
done

lemma (in Ring) npeSum2_sub_muly:
  " x  carrier R; y  carrier R  
        y r(nsum R (λi. nscal R ((npow R x (n-i)) r (npow R y i))
                                (n choose i)) n)
        = nsum R (λi. nscal R ((npow R x (n-i)) r (npow R y (i+1)))
                                (n choose i)) n"
  apply (cut_tac ring_is_ag)
  apply (subst nsumMulElmL)
    apply (rule allI)
      apply (simp only:nsClose add:ring_tOp_closed
             add:npClose)
    apply assumption
  apply (simp only:nsMulDistrL add:nsClose add:ring_tOp_closed
         add:npClose)
  apply (simp only: rMulLC [of "y"] add:npClose)

 apply (simp del:npow_suc add:ring_tOp_commute[of y])
 apply (rule aGroup.nsum_eq, assumption)
  apply (rule allI, rule impI, rule nsClose,
         rule ring_tOp_closed, simp add:npClose,
         rule ring_tOp_closed, assumption, simp add:npClose)
  apply (rule allI, rule impI, rule nsClose,
         rule ring_tOp_closed, simp add:npClose,
         rule npClose, assumption)
 apply (rule allI, rule impI)
  apply (frule_tac n = j in npClose[of y])
  apply (simp add:ring_tOp_commute[of y])
done

(********)(********)(********)(********)
lemma binomial_n0: "(Suc n choose 0) = (n choose 0)"
  by simp

lemma binomial_ngt_diff:
  "(n choose Suc n) = (Suc n choose Suc n) - (n choose n)"
  by (subst binomial_Suc_Suc, arith)


lemma binomial_ngt_0: "(n choose Suc n) = 0"
  apply (subst binomial_ngt_diff,
         (subst binomial_n_n)+)
  apply simp
  done

lemma diffLessSuc: "m  n  Suc (n-m) = Suc n - m"
  by arith

lemma (in Ring) npow_suc_i:
  " x  carrier R; i  n 
         npow R x (Suc n - i) =  x r (npow R x (n-i))"
  apply (subst diffLessSuc [THEN sym, of "i" "n"], assumption)
  apply (frule_tac n = "n - i" in npClose,
         simp add:ring_tOp_commute[of x])
  done
(**
lemma (in Ring) nsumEqFunc_sub:
  "⟦  ⋀ i. f i ∈ carrier R; ⋀ i. g i ∈ carrier R ⟧
        ⟹ ( ∀ i. i ≤ n ⟶ f i = g i) ⟶ (nsum0 R f n = nsum0 R g n)";
  apply (induct_tac "n")
  apply simp+
  done

lemma (in Ring) nsumEqFunc:
  "⟦ ⋀ i. f i ∈ carrier R; ⋀ i. g i ∈ carrier R;
     ⋀ i. i ≤ n ⟶ f i = g i ⟧ ⟹  nsum0 R f n = nsum0 R g n"
  apply (cut_tac nsumEqFunc_sub [of "f" "g" "n"])
  apply simp+
  done          nsumEqFunc ⟶ nsum_eq       **)
(********)(********)

lemma (in Ring) npeSum2_sub_mulx: " x  carrier R; y  carrier R  
  x r (nsum R (λ i. nscal R ((npow R x (n-i)) r (npow R y i))
                                                        (n choose i)) n)
   = (nsum R (λi. nscal R
                          ((npow R x (Suc n - Suc i)) r (npow R y (Suc i)))
                          (n choose Suc i)) n) ±
                (nscal R ((npow R x (Suc n - 0)) r (npow R y 0))
                        (Suc n choose 0))"
  apply (cut_tac ring_is_ag)
  apply (simp only: binomial_n0)
  apply (subst aGroup.nsumElmTail [THEN sym, of R "λ i. nscal R ((npow R x (Suc n - i)) r (npow R y i)) (n choose i)"], assumption+)
  apply (rule allI)
      apply (simp only:nsClose add:ring_tOp_closed add:npClose)

  apply (simp only:nsum_suc)
  apply (subst binomial_ngt_0)
  apply (simp only:nscal_0)
  apply (subst aGroup.ag_r_zero, assumption)
    apply (simp add:aGroup.nsum_mem nsClose ring_tOp_closed npClose)
  apply (subst nsumMulElmL [of  _ "x"])
    apply (rule allI, rule nsClose, rule ring_tOp_closed, simp add:npClose,
           simp add:npClose, assumption)

  apply (simp add: nsMulDistrL [of "x"] ring_tOp_closed npClose)
  apply (simp add:ring_tOp_assoc [THEN sym, of "x"] npClose)
  apply (rule aGroup.nsum_eq, assumption)
   apply (rule allI, rule impI,
          rule nsClose, (rule ring_tOp_closed)+, assumption,
          simp add:npClose, simp add:npClose)
   apply (rule allI, rule impI,
          rule nsClose, rule ring_tOp_closed,
          simp add:npClose, simp add:npClose)
  apply (rule allI, rule impI)
  apply (frule_tac n = "n - j" in npClose[of x],
        simp add:ring_tOp_commute[of x],
        subst npow_suc[THEN sym])
  apply (simp add:Suc_diff_le)
done

lemma (in Ring) npeSum2_sub_mulx2:
  " x  carrier R; y  carrier R  
        x r (nsum R (λ i. nscal R ((npow R x (n-i)) r (npow R y i))
                                (n choose i)) n)
        = (nsum R  (λi. nscal R
                          ((npow R x (n - i)) r ((npow R y i) r y ))
                          (n choose Suc i)) n) ±
                (𝟬 ± ((x r (npow R x n)) r (1r)))"
apply (subst  npeSum2_sub_mulx, assumption+, simp)
apply (frule npClose[of x n])
apply (subst ring_tOp_commute[of x], assumption+)
 apply (cut_tac ring_is_ag)
 apply (cut_tac aGroup.nsum_eq[of R n
        "λi.  (n choose Suc i) ×⇘R(x^⇗R (n - i)r y^⇗R (Suc i))"
        "λi.  (n choose Suc i) ×⇘R(x^⇗R (n - i)r (y^⇗R ir y))"])
 apply (simp del:npow_suc)+
  apply (rule allI, rule impI,
         rule nsClose, rule ring_tOp_closed, simp add:npClose,
         simp only:npClose)
  apply (rule allI, rule impI,
         rule nsClose, rule ring_tOp_closed, simp add:npClose,
         rule ring_tOp_closed, simp add:npClose, assumption)
  apply (rule allI, rule impI)
 apply (frule_tac n = j in npClose[of y])
 apply simp
done


lemma (in Ring) npeSum2:
  " n.  x  carrier R; y  carrier R 
         npow R (x ± y) n =
                nsum R (λ i. nscal R ((npow R x (n-i)) r (npow R y i))
                                       ( n choose i) ) n"
  apply (cut_tac ring_is_ag)
  apply (induct_tac "n")

  (*1*)
  apply simp
    apply (cut_tac ring_one, simp add:ring_r_one, simp add:aGroup.ag_l_zero)
  (*1:done*)

  apply (subst aGroup.nsumElmTail, assumption+)
    apply (rule allI)
    apply (simp add:nsClose ring_tOp_closed npClose)

(**
thm binomial_Suc_Suc
**)
  apply (simp only:binomial_Suc_Suc)
  apply (simp only: nsDistr [THEN sym] add:npClose ring_tOp_closed)
  apply (subst aGroup.nsumElmAdd, assumption+)
    apply (rule allI,
           simp add:nsClose ring_tOp_closed npClose)
    apply (rule allI,
           simp add:nsClose add:ring_tOp_closed npClose)
  apply (subst aGroup.ag_pOp_assoc, assumption)
    apply (rule aGroup.nsum_mem, assumption,
           rule allI, rule impI,  simp add:nsClose ring_tOp_closed npClose)
    apply (rule aGroup.nsum_mem, assumption,
           rule allI, rule impI,  simp add:nsClose ring_tOp_closed npClose)
    apply (simp add:nsClose ring_tOp_closed npClose)
    apply (rule aGroup.ag_pOp_closed, assumption)
    apply (simp add:aGroup.ag_inc_zero)
    apply (rule ring_tOp_closed)+
    apply (simp add:npClose, assumption, simp add:ring_one)

  apply (subst npMulElmL [THEN sym, of "x ± y"],
         simp add:aGroup.ag_pOp_closed, simp)
   apply simp
  apply (subst ring_distrib2 [of _ "x" "y"])
  apply (rule aGroup.nsum_mem,assumption,
         rule allI, rule impI, rule nsClose, rule ring_tOp_closed,
         simp add:npClose, simp add:npClose, assumption+)
  apply (rule aGroup.gEQAddcross [THEN sym], assumption+,
         rule aGroup.nsum_mem, assumption, rule allI, rule impI, rule nsClose,
         (rule ring_tOp_closed)+, simp add:npClose,
         rule ring_tOp_closed, simp add:npClose, assumption)
    apply (rule aGroup.ag_pOp_closed, assumption)
    apply (rule aGroup.nsum_mem, assumption,
           rule allI, rule impI, rule nsClose, rule ring_tOp_closed,
          simp add:npClose, rule ring_tOp_closed, simp add:npClose, assumption)
    apply (rule aGroup.ag_pOp_closed, assumption, simp add:ring_zero)
    apply ((rule ring_tOp_closed)+,
           simp add:npClose,assumption, simp add:ring_one)
    apply (rule ring_tOp_closed, assumption,
           rule aGroup.nsum_mem, assumption, rule allI, rule impI,
           rule nsClose, rule ring_tOp_closed,
           (simp add:npClose)+)
    apply (rule ring_tOp_closed, assumption+,
           rule aGroup.nsum_mem, assumption, rule allI, rule impI,
           rule nsClose,
           rule ring_tOp_closed,
           simp add:npClose, simp add:npClose)
    apply (subst npeSum2_sub_muly [of "x" "y"], assumption+, simp)

  (* final part *)
  apply (subst npeSum2_sub_mulx2 [of x y], assumption+)
  apply (frule_tac n = na in npClose[of x],
         simp add:ring_tOp_commute[of _ x])
  done

lemma (in aGroup) nsum_zeroTr:
  " n. ( i. i  n   f i = 𝟬)  (nsum A f n = 𝟬)"
  apply (induct_tac "n")
  apply simp

  apply (rule impI)
  apply (cut_tac n = na in Nsetn_sub_mem1, simp)
    apply (subst aGroup.ag_l_zero, rule aGroup_axioms)
    apply (simp add:ag_inc_zero)
  apply simp
  done

lemma (in Ring) npAdd:
  " x  carrier R; y  carrier R;
     npow R x m = 𝟬; npow R y n = 𝟬 
         npow R (x ± y) (m + n) = 𝟬"
  apply (subst npeSum2, assumption+)

  apply (rule aGroup.nsum_zeroTr [THEN mp])
  apply (simp add:ring_is_ag)
  apply (rule allI, rule impI)
  apply (rule nsZeroI)
  apply (rule rMulZeroDiv, simp add:npClose, simp add:npClose)

  apply (case_tac "i  n")

  apply (rule disjI1)
  apply (rule npGTPowZero [of "x" "m"], assumption+)
    apply arith

  apply (rule disjI2)
  apply (rule npGTPowZero [of "y" "n"], assumption+)
    apply (arith)
  done

lemma (in Ring) npInverse:
  "n. x  carrier R
         npow R (-a x) n = npow R x n
             npow R (-a x) n = -a (npow R x n)"
  apply (induct_tac n)
 (* n=0 *)
  apply simp

 apply (erule disjE)
 apply simp
 apply (subst ring_inv1_2,
        simp add:npClose, assumption, simp)
 apply (cut_tac ring_is_ag)

 apply simp
 apply (subst ring_inv1_2[THEN sym, of _ x])
 apply (rule aGroup.ag_mOp_closed, assumption+,
        simp add:npClose, assumption)
 apply (thin_tac "(-a x)^⇗R na= -a (x^⇗R na)",
        frule_tac n = na in npClose[of x],
        frule_tac x = "x^⇗R na⇖" in aGroup.ag_mOp_closed[of R], simp add:npClose)
 apply (simp add: ring_inv1_1[of _ x])
 apply (simp add:aGroup.ag_inv_inv[of R])
done

lemma (in Ring) npMul:
  " n.  x  carrier R; y  carrier R 
         npow R (x r y) n = (npow R x n) r (npow R y n)"
  apply (induct_tac "n")
 (* n=0 *)
  apply simp
  apply (rule ring_r_one [THEN sym]) apply (simp add:ring_one)
 (* n>0 *)
  apply (simp only:npow_suc)
  apply (rule ring_tOp_rel[THEN sym])
    apply (rule npClose, assumption+)+
  done

section "Ring homomorphisms"

definition
  rHom :: "[('a, 'm) Ring_scheme, ('b, 'm1) Ring_scheme]
                       ('a   'b) set" where
  "rHom A R = {f. f  aHom A R 
    (xcarrier A. ycarrier A. f ( x rAy) =  (f x) rR(f y))
     f (1rA) = (1rR)}"

definition
  rInvim :: "[('a, 'm) Ring_scheme, ('b, 'm1) Ring_scheme, 'a  'b, 'b set]
                'a set" where
  "rInvim A R f K = {a. a  carrier A  f a  K}"

definition
  rimg :: "[('a, 'm) Ring_scheme, ('b, 'm1) Ring_scheme, 'a  'b] 
            'b Ring" where
  "rimg A R f = carrier= f `(carrier A), pop = pop R, mop = mop R,
    zero = zero R, tp = tp R, un = un R "

definition
  ridmap :: "('a, 'm) Ring_scheme  ('a  'a)" where
  "ridmap R = (λxcarrier R. x)"

definition
  r_isom :: "[('a, 'm) Ring_scheme, ('b, 'm1) Ring_scheme]  bool"
                       (infixr "r" 100) where
  "r_isom R R'  (frHom R R'. bijec⇘R,R'f)"

definition
  Subring :: "[('a, 'm) Ring_scheme, ('a, 'm1) Ring_scheme]  bool" where
  "Subring R S == Ring S  (carrier S  carrier R)  (ridmap S)  rHom S R"

lemma ridmap_surjec:"Ring A  surjec⇘A,A(ridmap A)"
by(simp add:surjec_def aHom_def ridmap_def Ring.ring_is_ag aGroup.ag_pOp_closed surj_to_def)

lemma rHom_aHom:"f  rHom A R  f  aHom A R"
by (simp add:rHom_def)

lemma rimg_carrier:"f  rHom A R  carrier (rimg A R f) = f ` (carrier A)"
by (simp add:rimg_def)

lemma rHom_mem:" f  rHom A R; a  carrier A   f a  carrier R"
apply (simp add:rHom_def, frule conjunct1)
 apply (thin_tac "f  aHom A R 
     (xcarrier A. ycarrier A. f (x rAy) = f x rRf y)  f 1rA= 1rR⇙")
 apply (simp add:aHom_def, frule conjunct1)
 apply (thin_tac "f  carrier A  carrier R 
     f  extensional (carrier A) 
     (acarrier A. bcarrier A. f (a ±Ab) = f a ±Rf b)")
 apply (simp add:funcset_mem)
done

lemma rHom_func:"f  rHom A R  f  carrier A  carrier R"
by (simp add:rHom_def aHom_def)

lemma ringhom1:" Ring A; Ring R; x  carrier A; y  carrier A;
                    f  rHom A R   f (x ±Ay) = (f x) ±R(f y)"
apply (simp add:rHom_def) apply (erule conjE)
apply (frule Ring.ring_is_ag [of "A"])
apply (frule Ring.ring_is_ag [of "R"])
apply (rule aHom_add, assumption+)
done

lemma rHom_inv_inv:" Ring A; Ring R; x  carrier A; f  rHom A R 
  f (-aAx) = -aR(f x)"
apply (frule Ring.ring_is_ag [of "A"],
       frule Ring.ring_is_ag [of "R"])
apply (simp add:rHom_def, erule conjE)
apply (simp add:aHom_inv_inv)
done

lemma rHom_0_0:" Ring A; Ring R; f  rHom A R    f (𝟬A) = 𝟬R⇙"
apply (frule Ring.ring_is_ag [of "A"], frule Ring.ring_is_ag [of "R"])
apply (simp add:rHom_def, (erule conjE)+, simp add:aHom_0_0)
done

lemma rHom_tOp:" Ring A; Ring R; x  carrier A; y  carrier A;
 f  rHom A R   f (x rAy) = (f x) rR(f y)"
by (simp add:rHom_def)

lemma rHom_add:"f  rHom A R; x  carrier A; y  carrier A 
                   f (x ±Ay) = (f x) ±R(f y)"
by (simp add:rHom_def aHom_def)

lemma rHom_one:" Ring A; Ring R;f  rHom A R   f (1rA) = (1rR)"
by (simp add:rHom_def)

lemma rHom_npow:" Ring A; Ring R; x  carrier A; f  rHom A R  
                    f (x^⇗A n) = (f x)^⇗R n⇖"
apply (induct_tac n)
apply (simp add:rHom_one)
apply (simp,
      frule_tac n = n in Ring.npClose[of "A" "x"], assumption+,
      subst rHom_tOp[of "A" "R" _ "x" "f"], assumption+, simp)
done

lemma rHom_compos:"Ring A; Ring B; Ring C; f  rHom A B; g  rHom B C 
                   compos A g f  rHom A C"
apply (subst rHom_def, simp)
apply (frule Ring.ring_is_ag[of "A"], frule Ring.ring_is_ag[of "B"],
       frule Ring.ring_is_ag[of "C"],
       frule rHom_aHom[of "f" "A" "B"], frule rHom_aHom[of "g" "B" "C"],
       simp add:aHom_compos)
apply (rule conjI)
 apply ((rule ballI)+, simp add:compos_def compose_def,
        frule_tac x = x and y = y in Ring.ring_tOp_closed[of "A"], assumption+,
        simp)
apply (simp add:rHom_tOp)
 apply (frule_tac a = x in rHom_mem[of "f" "A" "B"], assumption+,
        frule_tac a = y in rHom_mem[of "f" "A" "B"], assumption+,
         simp add:rHom_tOp)
 apply (frule Ring.ring_one[of "A"], frule Ring.ring_one[of "B"],
        simp add:compos_def compose_def, simp add:rHom_one)
done

lemma rimg_ag:"Ring A; Ring R; f  rHom A R  aGroup (rimg A R f)"
apply (frule Ring.ring_is_ag [of "A"],
       frule Ring.ring_is_ag [of "R"])
apply (simp add:rHom_def, (erule conjE)+)
apply (subst aGroup_def)
apply (simp add:rimg_def)
apply (rule conjI)
 apply (rule Pi_I)+
 apply (simp add:image_def)
 apply (erule bexE)+
 apply simp
 apply (subst aHom_add [THEN sym, of "A" "R" "f"], assumption+)
 apply (blast dest: aGroup.ag_pOp_closed)
apply (rule conjI)
 apply ((rule allI, rule impI)+, simp add:image_def, (erule bexE)+, simp)
 apply (frule_tac x = x and y = xa in aGroup.ag_pOp_closed, assumption+,
        frule_tac x = xa and y = xb in aGroup.ag_pOp_closed, assumption+)
 apply (simp add:aHom_add[of "A" "R" "f", THEN sym] aGroup.ag_pOp_assoc)
apply (rule conjI)
 apply ((rule allI, rule impI)+, simp add:image_def, (erule bexE)+, simp)
 apply (simp add:aHom_add[of "A" "R" "f", THEN sym] aGroup.ag_pOp_commute)
apply (rule conjI)
 apply (rule Pi_I)
 apply (simp add:image_def, erule bexE, simp)
 apply (simp add:aHom_inv_inv[THEN sym],
        frule_tac x = xa in aGroup.ag_mOp_closed[of "A"], assumption+, blast)
apply (rule conjI)
  apply (rule allI, rule impI, simp add:image_def, (erule bexE)+, simp)
   apply (simp add:aHom_inv_inv[THEN sym],
        frule_tac x = x in aGroup.ag_mOp_closed[of "A"], assumption+,
        simp add:aHom_add[of "A" "R" "f", THEN sym])
 apply (simp add:aGroup.ag_l_inv1 aHom_0_0)
apply (rule conjI)
 apply (simp add:image_def)
 apply (frule aHom_0_0[THEN sym, of "A" "R" "f"], assumption+,
        frule Ring.ring_zero[of "A"], blast)

apply (rule allI, rule impI,
       simp add:image_def, erule bexE,
       frule_tac a = x in aHom_mem[of "A" "R" "f"], assumption+, simp)
 apply (simp add:aGroup.ag_l_zero)
done

lemma rimg_ring:"Ring A; Ring R; f  rHom A R   Ring (rimg A R f)"
apply (unfold Ring_def [of "rimg A R f"])
apply (frule rimg_ag[of "A" "R" "f"], assumption+)
 apply (rule conjI, simp add:aGroup_def[of "rimg A R f"])
apply(rule conjI)
 apply (rule conjI, rule allI, rule impI)
 apply (frule aGroup.ag_inc_zero[of "rimg A R f"],
        subst aGroup.ag_pOp_commute, assumption+,
        simp add:aGroup.ag_r_zero[of "rimg A R f"])

apply (rule conjI)
apply (rule Pi_I)+
apply (thin_tac "aGroup (rimg A R f)",
       simp add:rimg_def, simp add:image_def, (erule bexE)+,
       simp add:rHom_tOp[THEN sym])
 apply (blast dest:Ring.ring_tOp_closed)
 apply ((rule allI)+, (rule impI)+)
 apply (thin_tac "aGroup (rimg A R f)", simp add:rimg_def,
        simp add:image_def, (erule bexE)+, simp)
 apply (frule_tac x = x and y = xa in Ring.ring_tOp_closed, assumption+,
        frule_tac x = xa and y = xb in Ring.ring_tOp_closed, assumption+,
        simp add:rHom_tOp[THEN sym],
        simp add:Ring.ring_tOp_assoc)
apply (rule conjI, rule conjI, (rule allI)+, (rule impI)+)
 apply (thin_tac "aGroup (rimg A R f)", simp add:rimg_def,
        simp add:image_def, (erule bexE)+, simp,
        simp add:rHom_tOp[THEN sym],
        simp add:Ring.ring_tOp_commute)
  apply (thin_tac "aGroup (rimg A R f)", simp add:rimg_def,
         simp add:image_def)
  apply (subst rHom_one [THEN sym, of "A" "R" "f"], assumption+,
         frule Ring.ring_one[of "A"], blast)
apply (rule conjI, (rule allI)+, (rule impI)+)
apply (simp add:rimg_def, fold rimg_def,
       simp add:image_def, (erule bexE)+, simp)
 apply (frule rHom_aHom[of "f" "A" "R"],
        frule Ring.ring_is_ag [of "A"],
        frule Ring.ring_is_ag [of "R"],
        simp add:aHom_add[THEN sym],
        simp add:rHom_tOp[THEN sym])
 apply (frule_tac x = xa and y = xb in aGroup.ag_pOp_closed[of "A"],
          assumption+,
        frule_tac x = x and y = xa in Ring.ring_tOp_closed[of "A"],
          assumption+,
        frule_tac x = x and y = xb in Ring.ring_tOp_closed[of "A"],
          assumption+,
        simp add:aHom_add[THEN sym],
        simp add:rHom_tOp[THEN sym],
        simp add:Ring.ring_distrib1)
 apply (rule allI, rule impI,
        thin_tac "aGroup (rimg A R f)")
 apply (simp add:rimg_def,
        simp add:image_def, erule bexE, simp add:rHom_tOp[THEN sym],
        frule_tac a = x in rHom_mem[of "f" "A" "R"], assumption+,
         simp add:Ring.ring_l_one)
done

definition
  ideal :: "[_ , 'a set]  bool" where
  "ideal R I  (R +> I)  (rcarrier R. xI. (r rRx  I))"


lemma (in Ring) ideal_asubg:"ideal R I  R +> I"
by (simp add:ideal_def)

lemma (in Ring) ideal_pOp_closed:"ideal R I; x  I; y  I 
                                                    x ± y  I"
apply (unfold ideal_def, frule conjunct1, fold ideal_def)
apply (cut_tac ring_is_ag,
       simp add:aGroup.asubg_pOp_closed)
done

lemma (in Ring) ideal_nsum_closedTr:"ideal R I 
                                      (j  n. f j  I)   nsum R f n  I"
apply (induct_tac n)
 apply (rule impI)
 apply simp

 apply (rule impI)
 apply (cut_tac n = n in Nsetn_sub_mem1, simp)
 apply (rule ideal_pOp_closed, assumption+)
 apply simp
done

lemma (in Ring) ideal_nsum_closed:"ideal R I; j  n. f j  I 
                                             nsum R f n  I"
by (simp add:ideal_nsum_closedTr)

lemma (in Ring) ideal_subset1:"ideal R I  I  carrier R"
apply (unfold ideal_def, frule conjunct1, fold ideal_def)
  apply (simp add:asubGroup_def sg_def, (erule conjE)+)
  apply (cut_tac ring_is_ag,
         simp add:aGroup.ag_carrier_carrier)
done

lemma (in Ring) ideal_subset:"ideal R I; h  I  h  carrier R"
by (frule ideal_subset1[of "I"],
       simp add:subsetD)

lemma (in Ring) ideal_ring_multiple:"ideal R I; x  I; r  carrier R 
       r r x  I"
by (simp add:ideal_def)

lemma (in Ring) ideal_ring_multiple1:"ideal R I; x  I; r  carrier R  
       x r r  I"
apply (frule ideal_subset[of "I" "x"], assumption+)
apply (simp add:ring_tOp_commute ideal_ring_multiple)
done

lemma (in Ring) ideal_npow_closedTr:"ideal R I; x  I 
                                        0 < n  x^⇗R n I"
apply (induct_tac n,
       simp)
apply (rule impI)
 apply simp
 apply (case_tac "n = 0", simp)
 apply (frule ideal_subset[of "I" "x"], assumption+,
        simp add:ring_l_one)

 apply simp
apply (frule ideal_subset[of "I" "x"], assumption+,
       rule ideal_ring_multiple, assumption+,
       simp add:ideal_subset)
done

lemma (in Ring) ideal_npow_closed:"ideal R I; x  I; 0 < n  x^⇗R n I"
by (simp add:ideal_npow_closedTr)

lemma (in Ring) times_modTr:"a  carrier R; a'  carrier R; b  carrier R;
 b'  carrier R; ideal R I; a ± (-a b)  I; a' ± (-a b')  I 
                           a r a' ± (-a (b r b'))  I"
apply (cut_tac ring_is_ag)
apply (subgoal_tac "a r a' ± (-a (b r b')) = a r a' ± (-a (a r b'))
                       ± (a r b' ± (-a (b r b')))")
apply simp
 apply (simp add:ring_inv1_2[of "a" "b'"], simp add:ring_inv1_1[of "b" "b'"])
 apply (frule aGroup.ag_mOp_closed[of "R" "b'"], assumption+)
 apply (simp add:ring_distrib1[THEN sym, of "a" "a'" "-a b'"])
 apply (frule aGroup.ag_mOp_closed[of "R" "b"], assumption+)
 apply (frule ring_distrib2[THEN sym, of "b'" "a" "-a b" ], assumption+)
 apply simp

apply (thin_tac "a r a' ± (-a b) r b' = a r (a' ± -a b') ± (a ± -a b) r b'",
       thin_tac "a r b' ± (-a b) r b' = (a ± -a b) r b'")
 apply (frule ideal_ring_multiple[of "I" "a' ± (-a b')" "a"], assumption+,
        frule ideal_ring_multiple1[of "I" "a ± (-a b)" "b'"], assumption+)
 apply (simp add:ideal_pOp_closed)

apply (frule ring_tOp_closed[of "a" "a'"], assumption+,
       frule ring_tOp_closed[of "a" "b'"], assumption+,
       frule ring_tOp_closed[of "b" "b'"], assumption+,
       frule aGroup.ag_mOp_closed[of "R" "b r b'"], assumption+,
       frule aGroup.ag_mOp_closed[of "R" "a r b'"], assumption+)

 apply (subst aGroup.ag_pOp_assoc[of "R"], assumption+)
 apply (rule aGroup.ag_pOp_closed, assumption+)
 apply (simp add:aGroup.ag_pOp_assoc[THEN sym, of "R" "-a (a r b')" "a r b'"
                          "-a (b r b')"],
        simp add:aGroup.ag_l_inv1 aGroup.ag_l_zero)
done

lemma (in Ring) ideal_inv1_closed:" ideal R I; x  I   -a x  I"
apply (cut_tac ring_is_ag)
apply (unfold ideal_def, frule conjunct1, fold ideal_def)
apply (simp add:aGroup.asubg_mOp_closed[of "R" "I"])
done

lemma (in Ring) ideal_zero:"ideal R I   𝟬  I"

apply (cut_tac ring_is_ag)
apply (unfold ideal_def, frule conjunct1, fold ideal_def)
apply (simp add:aGroup.asubg_inc_zero)
done

lemma (in Ring) ideal_zero_forall:"I. ideal R I   𝟬  I"
by (simp add:ideal_zero)

lemma (in Ring) ideal_ele_sumTr1:" ideal R I; a  carrier R; b  carrier R;
          a ± b  I; a  I   b  I"
apply (frule ideal_inv1_closed[of "I" "a"], assumption+)
apply (frule ideal_pOp_closed[of "I" "-a a" "a ± b"], assumption+)
apply (frule ideal_subset[of "I" "-a a"], assumption+)
apply (cut_tac ring_is_ag,
       simp add:aGroup.ag_pOp_assoc[THEN sym],
       simp add:aGroup.ag_l_inv1,
       simp add:aGroup.ag_l_zero)
done

lemma (in Ring) ideal_ele_sumTr2:"ideal R I; a  carrier R; b  carrier R;
                a ± b  I; b  I  a  I"
apply (cut_tac ring_is_ag,
       simp add:aGroup.ag_pOp_commute[of "R" "a" "b"])
apply (simp add:ideal_ele_sumTr1[of "I" "b" "a"])
done

lemma (in Ring) ideal_condition:"I  carrier R; I  {};
       xI. yI. x ± (-a y)  I; rcarrier R. xI. r r x  I  
                                   ideal R I"
apply (simp add:ideal_def)
 apply (cut_tac ring_is_ag)
 apply (rule aGroup.asubg_test[of "R" "I"], assumption+)
done

lemma (in Ring) ideal_condition1:"I  carrier R; I  {};
  xI. yI. x ± y  I; rcarrier R. xI. r r x  I   ideal R I"
apply (rule ideal_condition[of "I"], assumption+)
apply (rule ballI)+
apply (cut_tac ring_is_ag,
       cut_tac ring_one,
       frule aGroup.ag_mOp_closed[of "R" "1r"], assumption+)
 apply (frule_tac x = "-a 1r " in bspec, assumption+,
        thin_tac "rcarrier R. xI. r r x  I",
        rotate_tac -1,
        frule_tac x = y in bspec, assumption,
        thin_tac "xI. (-a 1r) r x  I")
 apply (frule_tac c = y in subsetD[of "I" "carrier R"], assumption+,
        simp add:ring_times_minusl[THEN sym], simp add:ideal_pOp_closed)
done

lemma (in Ring) zero_ideal:"ideal R {𝟬}"
apply (cut_tac ring_is_ag)
apply (rule ideal_condition1)
 apply (simp add:ring_zero)
 apply simp
 apply simp
apply (cut_tac ring_zero, simp add:aGroup.ag_l_zero)
apply simp
 apply (rule ballI, simp add:ring_times_x_0)
done

lemma (in Ring) whole_ideal:"ideal R (carrier R)"
apply (rule ideal_condition1)
 apply simp
 apply (cut_tac ring_zero, blast)
 apply (cut_tac ring_is_ag,
        simp add:aGroup.ag_pOp_closed,
        simp add:ring_tOp_closed)
done

lemma (in Ring) ideal_inc_one:"ideal R I; 1r  I   I = carrier R"
apply (rule equalityI)
apply (simp add:ideal_subset1)
apply (rule subsetI,
       frule_tac r = x in ideal_ring_multiple[of "I" "1r"], assumption+,
       simp add:ring_r_one)
done

lemma (in Ring) ideal_inc_one1:"ideal R I 
                              (1r  I) = (I = carrier R)"
apply (rule iffI)
 apply (simp add:ideal_inc_one)
 apply (frule sym, thin_tac "I = carrier R",
        cut_tac ring_one, simp)
done

definition
  Unit :: "_  'a  bool" where
  "Unit R a  a  carrier R  (bcarrier R. a rRb = 1rR)"

lemma (in Ring) ideal_inc_unit:"ideal R I; a  I; Unit R a  1r  I"
by (simp add:Unit_def, erule conjE, erule bexE,
       frule_tac r = b in ideal_ring_multiple1[of "I" "a"], assumption+,
       simp)

lemma (in Ring) proper_ideal:"ideal R I; 1r  I  I  carrier R"
apply (rule contrapos_pp, simp+)
apply (simp add: ring_one)
done

lemma (in Ring) ideal_inc_unit1:"a  carrier R; Unit R a; ideal R I; a  I
                         I = carrier R"
apply (frule ideal_inc_unit[of "I" "a"], assumption+)
apply (rule ideal_inc_one[of "I"], assumption+)
done

lemma (in Ring) int_ideal:"ideal R I; ideal R J  ideal R (I  J)"
apply (rule ideal_condition1)
apply (frule ideal_subset1[of "I"], frule ideal_subset1[of "J"])
 apply blast
 apply (frule ideal_zero[of "I"], frule ideal_zero[of "J"], blast)

 apply ((rule ballI)+, simp, (erule conjE)+,
         simp add:ideal_pOp_closed)
 apply ((rule ballI)+, simp, (erule conjE)+)
 apply (simp add:ideal_ring_multiple)
done

definition
  ideal_prod::"[_, 'a set, 'a set]  'a set" (infix "rı" 90 ) where
  "ideal_prod R I J ==  {L. ideal R L 
                              {x.(iI. jJ. x = i rRj)}  L}"

lemma (in Ring) set_sum_mem:"a  I; b  J; I  carrier R; J  carrier R 
             a ± b  I  J"
apply (cut_tac ring_is_ag)
apply (simp add:aGroup.set_sum, blast)
done

lemma (in Ring) sum_ideals:"ideal R I1; ideal R I2  ideal R (I1  I2)"
apply (cut_tac ring_is_ag)
apply (frule ideal_subset1[of "I1"], frule ideal_subset1[of "I2"])
apply (rule ideal_condition1)
 apply (rule subsetI, simp add:aGroup.set_sum, (erule bexE)+)
 apply (frule_tac h = h in ideal_subset[of "I1"], assumption+,
        frule_tac h = k in ideal_subset[of "I2"], assumption+,
        cut_tac ring_is_ag,
        simp add:aGroup.ag_pOp_closed)
 apply (frule ideal_zero[of "I1"], frule ideal_zero[of "I2"],
        frule set_sum_mem[of "𝟬" "I1" "𝟬" "I2"], assumption+, blast)
apply (rule ballI)+
 apply (simp add:aGroup.set_sum, (erule bexE)+, simp)
 apply (rename_tac x y i ia j ja)
 apply (frule_tac h = i in ideal_subset[of "I1"], assumption+,
        frule_tac h = ia in ideal_subset[of "I1"], assumption+,
        frule_tac h = j in ideal_subset[of "I2"], assumption+,
        frule_tac h = ja in ideal_subset[of "I2"], assumption+)
 apply (subst aGroup.pOp_assocTr43, assumption+)
 apply (frule_tac x = j and y = ia in aGroup.ag_pOp_commute[of "R"],
          assumption+, simp)
 apply (subst aGroup.pOp_assocTr43[THEN sym], assumption+)
 apply (frule_tac x = i and y = ia in ideal_pOp_closed[of "I1"], assumption+,
        frule_tac x = j and y = ja in ideal_pOp_closed[of "I2"], assumption+,
        blast)
apply (rule ballI)+
 apply (simp add:aGroup.set_sum, (erule bexE)+, simp)
 apply (rename_tac r x i j)
 apply (frule_tac h = i in ideal_subset[of "I1"], assumption+,
        frule_tac h = j in ideal_subset[of "I2"], assumption+)
 apply (simp add:ring_distrib1)
 apply (frule_tac x = i and r = r in ideal_ring_multiple[of "I1"], assumption+,
        frule_tac x = j and r = r in ideal_ring_multiple[of "I2"], assumption+,
        blast)
done

lemma (in Ring) sum_ideals_la1:"ideal R I1; ideal R I2  I1  (I1  I2)"
apply (cut_tac ring_is_ag)
apply (rule subsetI)
apply (frule ideal_zero[of "I2"],
       frule_tac h = x in ideal_subset[of "I1"], assumption+,
       frule_tac x = x in aGroup.ag_r_zero[of "R"], assumption+)
apply (subst aGroup.set_sum, assumption,
       simp add:ideal_subset1, simp add:ideal_subset1, simp,
       frule sym, thin_tac "x ± 𝟬 = x", blast)
done

lemma (in Ring) sum_ideals_la2:"ideal R I1; ideal R I2   I2  (I1  I2)"
apply (cut_tac ring_is_ag)
apply (rule subsetI)
apply (frule ideal_zero[of "I1"],
       frule_tac h = x in ideal_subset[of "I2"], assumption+,
       frule_tac x = x in aGroup.ag_l_zero[of "R"], assumption+)
apply (subst aGroup.set_sum, assumption,
       simp add:ideal_subset1, simp add:ideal_subset1, simp,
       frule sym, thin_tac "𝟬 ± x = x", blast)
done

lemma (in Ring) sum_ideals_cont:"ideal R I;  A  I; B  I   A  B  I"
apply (cut_tac ring_is_ag)
apply (rule subsetI)
 apply (frule ideal_subset1[of I],
        frule subset_trans[of A I "carrier R"], assumption+,
        frule subset_trans[of B I "carrier R"], assumption+)
 apply (simp add:aGroup.set_sum[of R], (erule bexE)+, simp)
 apply (frule_tac c = h in subsetD[of "A" "I"], assumption+,
        frule_tac c = k in subsetD[of "B" "I"], assumption+)
 apply (simp add:ideal_pOp_closed)
done

lemma (in Ring) ideals_set_sum:"ideal R A; ideal R B; x  A  B 
             hA. kB. x = h ± k"
apply (frule ideal_subset1[of A],
       frule ideal_subset1[of B])
apply (cut_tac ring_is_ag,
       simp add:aGroup.set_sum)
done

definition
  Rxa :: "[_, 'a ]  'a set" (infixl "p" 200)  where
  "Rxa R a = {x. rcarrier R. x = (r rRa)}"

lemma (in Ring) a_in_principal:"a  carrier R  a  Rxa R a"
apply (cut_tac ring_one,
       frule ring_l_one[THEN sym, of "a"])
apply (simp add:Rxa_def, blast)
done

lemma (in Ring) principal_ideal:"a  carrier R  ideal R (Rxa R a)"
apply (rule ideal_condition1)
  apply (rule subsetI,
         simp add:Rxa_def, erule bexE, simp add:ring_tOp_closed)
apply (frule a_in_principal[of "a"], blast)
apply ((rule ballI)+,
        simp add:Rxa_def, (erule bexE)+, simp,
        subst ring_distrib2[THEN sym], assumption+,
        cut_tac ring_is_ag,
        frule_tac x = r and y = ra in aGroup.ag_pOp_closed, assumption+,
        blast)
apply ((rule ballI)+,
        simp add:Rxa_def, (erule bexE)+, simp,
        simp add:ring_tOp_assoc[THEN sym])
 apply (frule_tac x = r and y = ra in ring_tOp_closed, assumption, blast)
done

lemma (in Ring) rxa_in_Rxa:"a  carrier R; r  carrier R 
                                     r r a  Rxa R a"
by (simp add:Rxa_def, blast)

lemma (in Ring) Rxa_one:"Rxa R 1r = carrier R"
apply (rule equalityI)
 apply (rule subsetI, simp add:Rxa_def, erule bexE)
 apply (simp add:ring_r_one)

 apply (rule subsetI, simp add:Rxa_def)
 apply (frule_tac t = x in ring_r_one[THEN sym], blast)
done

lemma (in Ring) Rxa_zero:"Rxa R 𝟬 = {𝟬}"
apply (rule equalityI)
apply (rule subsetI)
 apply (simp add:Rxa_def, erule bexE, simp add:ring_times_x_0)
apply (rule subsetI)
 apply (simp add:Rxa_def)
 apply (cut_tac ring_zero,
        frule ring_times_x_0[THEN sym, of "𝟬"], blast)
done

lemma (in Ring) Rxa_nonzero:"a  carrier R; a  𝟬  Rxa R a  {𝟬}"
apply (rule contrapos_pp, simp+)
 apply (frule a_in_principal[of "a"])
 apply simp
done

lemma (in Ring) ideal_cont_Rxa:"ideal R I; a  I  Rxa R a  I"
apply (rule subsetI)
 apply (simp add:Rxa_def, erule bexE, simp)
 apply (simp add:ideal_ring_multiple)
done

lemma (in Ring) Rxa_mult_smaller:" a  carrier R; b  carrier R 
                    Rxa R (a r b)  Rxa R b"
apply (frule rxa_in_Rxa[of b a], assumption,
       frule principal_ideal[of b])
apply (rule ideal_cont_Rxa[of "R p b" "a r b"], assumption+)
done

lemma (in Ring) id_ideal_psub_sum:"ideal R I; a  carrier R; a  I 
                                             I  I  Rxa R a"
apply (cut_tac ring_is_ag)
apply (simp add:psubset_eq)
apply (frule principal_ideal)
apply (rule conjI)
apply (rule sum_ideals_la1, assumption+)
apply (rule contrapos_pp) apply simp+
apply (frule sum_ideals_la2[of "I" "Rxa R a"], assumption+)
apply (frule a_in_principal[of "a"],
       frule subsetD[of "Rxa R a" "I  Rxa R a" "a"], assumption+)
apply simp
done

lemma (in Ring) mul_two_principal_idealsTr:"a  carrier R; b  carrier R;
         x  Rxa R a; y  Rxa R b  rcarrier R. x r y = r r (a r b)"
apply (simp add:Rxa_def, (erule bexE)+)
apply simp
apply (frule_tac x = ra and y = b in ring_tOp_closed, assumption+)
apply (simp add:ring_tOp_assoc)
apply (simp add:ring_tOp_assoc[THEN sym, of a _ b])
apply (simp add:ring_tOp_commute[of a], simp add:ring_tOp_assoc)
apply (frule_tac x = a and y = b in ring_tOp_closed, assumption+,
       thin_tac "ra r b  carrier R",
       simp add:ring_tOp_assoc[THEN sym, of _ _ "a r b"],
       frule_tac x = r and y = ra in ring_tOp_closed, assumption+)
apply (simp add:ring_tOp_commute[of b a])
apply blast
done


primrec sum_pr_ideals::"[('a, 'm) Ring_scheme, nat  'a, nat]  'a set"
where
  sum_pr0: "sum_pr_ideals R f 0 = Rxa R (f 0)"
| sum_prn: "sum_pr_ideals R f (Suc n) =
                  (Rxa R (f (Suc n))) R(sum_pr_ideals R f n)"

lemma (in Ring) sum_of_prideals0:
      "f. (l  n. f l  carrier R)  ideal R (sum_pr_ideals R f n)"
apply (induct_tac n)
apply (rule allI) apply (rule impI)
 apply simp
 apply (rule Ring.principal_ideal, rule Ring_axioms, assumption)
(** case n **)
apply (rule allI, rule impI)
 apply (frule_tac x = f in spec,
        thin_tac "f. (l  n. f l  carrier R) 
               ideal R (sum_pr_ideals R f n)")
 apply (cut_tac n = n in Nsetn_sub_mem1, simp)
 apply (cut_tac a = "f (Suc n)" in  principal_ideal,
       simp)
 apply (rule_tac ?I1.0 = "Rxa R (f (Suc n))" and
        ?I2.0 = "sum_pr_ideals R f n" in Ring.sum_ideals, rule Ring_axioms, assumption+)
done

lemma (in Ring) sum_of_prideals:"l  n. f l  carrier R 
                      ideal R (sum_pr_ideals R f n)"
apply (simp add:sum_of_prideals0)
done

text ‹later, we show sum_pr_ideals› is the least ideal containing
        {f 0, f 1,…, f n}›

lemma (in Ring) sum_of_prideals1:"f. (l  n. f l  carrier R) 
                                    f ` {i. i  n}  (sum_pr_ideals R f n)"
apply (induct_tac n)
 apply (rule allI, rule impI)
apply (simp, simp add:a_in_principal)

apply (rule allI, rule impI)
 apply (frule_tac a = f in forall_spec,
        thin_tac "f. (l  n. f l  carrier R) 
               f ` {i. i  n}  sum_pr_ideals R f n")
 apply (rule allI, cut_tac n = n in Nset_un, simp)

 apply (subst Nset_un)
 apply (cut_tac A = "{i. i  (Suc n)}" and f = f and B = "carrier R" and
        ?A1.0 = "{i. i  n}" and ?A2.0 = "{Suc n}" in im_set_un1,
        simp, rule Nset_un)
 apply (thin_tac "f. (ln. f l  carrier R) 
               f ` {i. i  n}  sum_pr_ideals R f n",
        simp)
 apply (cut_tac n = n and f = f in sum_of_prideals,
        cut_tac n = n in Nsetn_sub_mem1, simp)
 apply (cut_tac a = "f (Suc n)" in principal_ideal, simp)
 apply (frule_tac ?I1.0 = "Rxa R (f (Suc n))" and ?I2.0 = "sum_pr_ideals R f n"
                 in sum_ideals_la1, assumption+,
        cut_tac a = "f (Suc n)" in a_in_principal, simp,
        frule_tac A = "R p f (Suc n)" and
         B = "R p f (Suc n)  sum_pr_ideals R f n" and c = "f (Suc n)" in
         subsetD, simp+)
  apply (frule_tac ?I1.0 = "Rxa R (f (Suc n))" and
         ?I2.0 = "sum_pr_ideals R f n" in sum_ideals_la2, assumption+)
  apply (rule_tac A = "f ` {j. j  n}" and B = "sum_pr_ideals R f n" and
         C = "Rxa R (f (Suc n))  sum_pr_ideals R f n" in subset_trans,
         assumption+)
done

lemma (in Ring) sum_of_prideals2:"l  n. f l  carrier R
                 f ` {i. i  n}  (sum_pr_ideals R f n)"
apply (simp add:sum_of_prideals1)
done

lemma (in Ring) sum_of_prideals3:"ideal R I 
      f. (l  n. f l  carrier R)  (f ` {i. i  n}  I) 
          (sum_pr_ideals R f n  I)"
apply (induct_tac n)
 apply (rule allI, rule impI, erule conjE)
 apply simp
 apply (rule ideal_cont_Rxa[of I], assumption+)

apply (rule allI, rule impI, erule conjE)
 apply (frule_tac a = f in forall_spec,
        thin_tac "f. (l  n. f l  carrier R)  f `{i. i  n}  I 
               sum_pr_ideals R f n  I")
 apply (simp add:Nset_un)
 apply (thin_tac "f. (l  n. f l  carrier R)  f ` {i. i  n}  I 
               sum_pr_ideals R f n  I")
 apply (frule_tac x = "Suc n" in spec,
        thin_tac "l  (Suc n). f l  carrier R", simp)
   apply (cut_tac a = "Suc n" and A = "{i. i  Suc n}" and
          f = f in mem_in_image2, simp)
   apply (frule_tac A = "f ` {i. i  Suc n}" and B = I and c = "f (Suc n)" in
          subsetD,  assumption+)
 apply (rule_tac A = "Rxa R  (f (Suc n))" and B = "sum_pr_ideals R f n" in
        sum_ideals_cont[of I], assumption)
 apply (rule ideal_cont_Rxa[of I], assumption+)
done

lemma (in Ring) sum_of_prideals4:"ideal R I; l  n. f l  carrier R;
       (f ` {i. i  n}  I)  sum_pr_ideals R f n  I"
apply (simp add:sum_of_prideals3)
done

lemma ker_ideal:"Ring A; Ring R; f  rHom A R  ideal A (ker⇘A,Rf)"
apply (frule Ring.ring_is_ag[of "A"], frule Ring.ring_is_ag[of "R"])
apply (rule Ring.ideal_condition1, assumption+)
apply (rule subsetI,
       simp add:ker_def)
apply (simp add:rHom_def, frule conjunct1)
apply (frule ker_inc_zero[of "A" "R" "f"], assumption+, blast)

apply (rule ballI)+
 apply (simp add:ker_def, (erule conjE)+)
 apply (simp add:aGroup.ag_pOp_closed)
 apply (simp add:rHom_def, frule conjunct1,
        simp add:aHom_add,
        frule Ring.ring_zero[of "R"],
        simp add:aGroup.ag_l_zero)
apply (rule ballI)+
 apply (simp add:ker_def, (erule conjE)+)
 apply (simp add:Ring.ring_tOp_closed)
 apply (simp add:rHom_tOp)
 apply (frule_tac a = r in rHom_mem[of "f" "A" "R"], assumption+,
        simp add:Ring.ring_times_x_0)
done

subsection "Ring of integers"

definition
  Zr :: "int Ring" where
  "Zr =  carrier = Zset, pop = λnZset. λmZset. (m + n),
    mop = λlZset. -l, zero = 0, tp = λmZset. λnZset. m * n, un = 1"

lemma ring_of_integers:"Ring Zr"
apply (simp add:Ring_def)
apply (rule conjI)
 apply (simp add:Zr_def Zset_def)
apply (rule conjI)
 apply (simp add:Zr_def Zset_def)
apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (simp add:Zr_def Zset_def)
apply (rule conjI)
 apply (simp add:Zr_def Zset_def)
apply (rule conjI,
       rule allI, rule impI, simp add:Zr_def Zset_def)
apply (rule conjI, simp add:Zr_def Zset_def)
apply (rule conjI,
       rule allI, rule impI, simp add:Zr_def Zset_def)
apply (rule conjI)
 apply (simp add:Zr_def Zset_def)
apply (rule conjI,
       (rule allI, rule impI)+, simp add:Zr_def Zset_def)
apply (rule conjI,
       (rule allI, rule impI)+, simp add:Zr_def Zset_def)
apply (rule conjI)
 apply (simp add:Zr_def Zset_def)
apply (rule conjI,
       (rule allI, rule impI)+, simp add:Zr_def Zset_def)
 apply (simp add: distrib_left)
apply (rule allI, rule impI)
  apply (simp add:Zr_def Zset_def)
done

lemma Zr_zero:"𝟬Zr= 0"
by (simp add:Zr_def)

lemma Zr_one:"1rZr= 1"
by (simp add:Zr_def)

lemma Zr_minus:"-aZrn = - n"
by (simp add:Zr_def Zset_def)

lemma Zr_add:"n ±Zrm = n + m"
by (simp add:Zr_def Zset_def)

lemma Zr_times:"n rZrm = n * m"
by (simp add:Zr_def Zset_def)

definition
  lev :: "int set  int" where
  "lev I = Zleast {n. n  I  0 < n}"

lemma Zr_gen_Zleast:"ideal Zr I; I  {0::int} 
                       Rxa Zr (lev I) = I"
 apply (cut_tac ring_of_integers)
 apply (simp add:lev_def)
 apply (subgoal_tac "{n. n  I  0 < n}  {}")
 apply (subgoal_tac "{n. n  I  0 < n}  Zset")
 apply (subgoal_tac "LB {n. n  I  0 < n} 0")
 apply (frule_tac A = "{n. n  I  0 < n}" and n = 0 in Zleast, assumption+)
 apply (erule conjE)+
 apply (fold lev_def)
defer
 apply (simp add:LB_def)
 apply (simp add:Zset_def)
 apply (frule Ring.ideal_zero[of "Zr" "I"], assumption+, simp add:Zr_zero)
 apply (frule singleton_sub[of "0" "I"])
 apply (frule sets_not_eq[of "I" "{0}"], assumption+, erule bexE, simp)
 apply (case_tac "0 < a", blast)
 apply (frule Ring.ring_one[of "Zr"])
 apply (frule Ring.ring_is_ag[of "Zr"],
         frule aGroup.ag_mOp_closed[of "Zr" "1rZr⇙"], assumption)
 apply (frule_tac x = a in Ring.ideal_ring_multiple[of "Zr" "I" _ "-aZr1rZr⇙"],
        assumption+)
 apply (simp add:Zr_one Zr_minus,
        thin_tac "ideal Zr I", thin_tac "Ring Zr", thin_tac "1  carrier Zr",
        thin_tac "-1  carrier Zr", thin_tac "aGroup Zr")
 apply (simp add:Zr_def Zset_def)
 apply (subgoal_tac "0 < - a", blast)
 apply arith
 apply (thin_tac "{n  I. 0 < n}  {}", thin_tac "{n  I. 0 < n}  Zset",
        thin_tac "LB {n  I. 0 < n} 0")

apply simp
 apply (erule conjE)
 apply (frule Ring.ideal_cont_Rxa[of "Zr" "I" "lev I"], assumption+)
 apply (rule equalityI, assumption,
        thin_tac "Rxa Zr (lev I)  I")
 apply (rule subsetI)
  apply (simp add:Rxa_def, simp add:Zr_times)
 apply (cut_tac t = x and b = "lev I" in mult_div_mod_eq [symmetric])
 apply (subgoal_tac "x = (x div lev I) * (lev I)",
        subgoal_tac "x div lev I  carrier Zr", blast)
 apply (simp add:Zr_def Zset_def)
apply (subgoal_tac "x mod lev I = 0", simp)
 apply (subst mult.commute, assumption)
 apply (subgoal_tac "x mod lev I  I")
   apply (thin_tac "x = lev I * (x div lev I) + x mod lev I")
 apply (frule_tac a = x in Divides.pos_mod_conj[of "lev I"])
 apply (rule contrapos_pp, simp+)
 apply (erule conjE)
 apply (frule_tac a = "x mod (lev I)" in forall_spec)
  apply simp apply arith
  apply (frule_tac r = "x div (lev I)" in
          Ring.ideal_ring_multiple1[of "Zr" "I" "lev I"], assumption+,
          simp add:Zr_def Zset_def)
  apply (frule sym, thin_tac "x = lev I * (x div lev I) + x mod lev I")
  apply (rule_tac a = "lev I * (x div lev I)" and b = "x mod lev I " in
         Ring.ideal_ele_sumTr1[of "Zr" "I"], assumption+)
 apply (simp add:Zr_def Zset_def)
 apply (simp add:Zr_def Zset_def)
 apply (subst Zr_add)
 apply simp
 apply (simp add:Zr_times)
done

lemma Zr_pir:"ideal Zr I  n. Rxa Zr n = I" (** principal ideal ring *)
apply (case_tac "I = {(0::int)}")
 apply (subgoal_tac "Rxa Zr 0 = I") apply blast
 apply (rule equalityI)
 apply (rule subsetI) apply (simp add:Rxa_def)
 apply (simp add:Zr_def Zset_def)
 apply (rule subsetI)
 apply (simp add:Rxa_def Zr_def Zset_def)
apply (frule Zr_gen_Zleast [of "I"], assumption+)
 apply blast
done

section "Quotient rings"

lemma (in Ring) mem_set_ar_cos:"ideal R I; a  carrier R 
                                         a ⊎⇘RI  set_ar_cos R I"
by (simp add:set_ar_cos_def, blast)

lemma (in Ring) I_in_set_ar_cos:"ideal R I  I  set_ar_cos R I"
apply (cut_tac ring_is_ag,
       frule ideal_asubg[of "I"],
       rule aGroup.unit_in_set_ar_cos, assumption+)
done

lemma (in Ring) ar_coset_same1:"ideal R I; a  carrier R; b  carrier R;
       b ± (-a a)  I   a ⊎⇘RI = b ⊎⇘RI"
apply (cut_tac ring_is_ag)
 apply (frule aGroup.b_ag_group[of "R"])
 apply (simp add:ideal_def asubGroup_def) apply (erule conjE)
 apply (frule aGroup.ag_carrier_carrier[THEN sym, of "R"])
 apply simp
 apply (frule Group.rcs_eq[of "b_ag R" "I" "a" "b"], assumption+)
 apply (frule aGroup.agop_gop [of "R"])
 apply (frule aGroup.agiop_giop[of "R"]) apply simp
 apply (simp add:ar_coset_def rcs_def)
done

lemma (in Ring) ar_coset_same2:"ideal R I; a  carrier R; b  carrier R;
                                  a ⊎⇘RI = b ⊎⇘RI   b ± (-a a)  I"
apply (cut_tac ring_is_ag)
apply (simp add:ar_coset_def)
 apply (frule aGroup.b_ag_group[of "R"])
 apply (simp add:ideal_def asubGroup_def, frule conjunct1, fold asubGroup_def,
        fold ideal_def, simp add:asubGroup_def)
 apply (subgoal_tac "a  carrier (b_ag R)",
         subgoal_tac "b  carrier (b_ag R)")
 apply (simp add:Group.rcs_eq[THEN sym, of "b_ag R" "I" "a" "b"])
 apply (frule aGroup.agop_gop [of "R"])
 apply (frule aGroup.agiop_giop[of "R"]) apply simp
 apply (simp add:b_ag_def)+
done

lemma (in Ring) ar_coset_same3:"ideal R I; a  carrier R; a ⊎⇘RI = I 
                               aI"
apply (cut_tac ring_is_ag)
apply (simp add:ar_coset_def)
apply (rule Group.rcs_fixed [of "b_ag R" "I" "a" ])
apply (rule aGroup.b_ag_group, assumption)
apply (simp add:ideal_def asubGroup_def)
apply (simp add:b_ag_def)
apply assumption
done

lemma (in Ring) ar_coset_same3_1:"ideal R I; a  carrier R; a  I 
                                                    a ⊎⇘RI  I"
apply (rule contrapos_pp, simp+)
apply (simp add:ar_coset_same3)
done

lemma (in Ring) ar_coset_same4:"ideal R I; a  I 
                                     a ⊎⇘RI = I"
apply (cut_tac ring_is_ag)
apply (frule ideal_subset[of "I" "a"], assumption+)
apply (simp add:ar_coset_def)
apply (rule Group.rcs_Unit2 [of "b_ag R" "I""a"])
apply (rule aGroup.b_ag_group, assumption)
apply (simp add:ideal_def asubGroup_def)
apply assumption
done

lemma (in Ring) ar_coset_same4_1:"ideal R I; a ⊎⇘RI  I  a  I"
apply (rule contrapos_pp, simp+)
apply (simp add:ar_coset_same4)
done

lemma (in Ring) belong_ar_coset1:"ideal R I; a  carrier R; x  carrier R;
                 x ± (-a a)  I   x  a ⊎⇘RI"
apply (frule ar_coset_same1 [of "I" "a" "x"], assumption+)
apply (subgoal_tac "x  x ⊎⇘RI")
 apply simp
 apply (cut_tac ring_is_ag)
 apply (subgoal_tac "carrier R = carrier (b_ag R)")
 apply (frule aGroup.agop_gop[THEN sym, of "R"])
 apply (frule aGroup.agiop_giop [THEN sym, of "R"])
 apply (simp add:ar_coset_def)
 apply (simp add:ideal_def asubGroup_def)

apply (rule Group.a_in_rcs [of "b_ag R" "I" "x"])
 apply (simp add: aGroup.b_ag_group)
 apply simp
 apply simp
 apply (simp add:b_ag_def)
done

lemma (in Ring) a_in_ar_coset:"ideal R I; a  carrier R  a  a ⊎⇘RI"
apply (rule belong_ar_coset1, assumption+)
apply (cut_tac ring_is_ag)
apply (simp add:aGroup.ag_r_inv1)
apply (simp add:ideal_zero)
done

lemma (in Ring) ar_coset_subsetD:"ideal R I; a  carrier R; x  a ⊎⇘RI  
                           x  carrier R"
 apply (subgoal_tac "carrier R = carrier (b_ag R)")
 apply (cut_tac ring_is_ag)
 apply (frule aGroup.agop_gop [THEN sym, of "R"])
 apply (frule aGroup.agiop_giop [THEN sym, of "R"])
 apply (simp add:ar_coset_def)
 apply (simp add:ideal_def asubGroup_def)
apply (rule Group.rcs_subset_elem[of "b_ag R" "I" "a" "x"])
 apply (simp add:aGroup.b_ag_group)
 apply simp
 apply assumption+
 apply (simp add:b_ag_def)
done

lemma (in Ring) ar_cos_mem:"ideal R I; a  carrier R 
                                 a ⊎⇘RI  set_rcs (b_ag R) I"
apply (cut_tac ring_is_ag)
 apply (simp add:set_rcs_def ar_coset_def)
 apply (frule aGroup.ag_carrier_carrier[THEN sym, of "R"]) apply simp
 apply blast
done

lemma (in Ring) mem_ar_coset1:"ideal R I; a  carrier R; x  a ⊎⇘RI 
                                 hI. h ± a = x"
 apply (cut_tac ring_is_ag)
 apply (frule aGroup.ag_carrier_carrier[THEN sym, of "R"])
 apply (frule aGroup.agop_gop [THEN sym, of "R"])
 apply (frule aGroup.agiop_giop [THEN sym, of "R"])
 apply (simp add:ar_coset_def)
 apply (simp add:ideal_def asubGroup_def)
apply (simp add:rcs_def)
done

lemma (in Ring) ar_coset_mem2:"ideal R I; a  carrier R; x  a ⊎⇘RI 
                           hI. x = a ± h"
apply (cut_tac ring_is_ag)
apply (frule mem_ar_coset1 [of "I" "a" "x"], assumption+)
apply (erule bexE,
       frule_tac h = h in ideal_subset[of "I"], assumption+)
apply (simp add:aGroup.ag_pOp_commute[of "R" _ "a"],
       frule sym, thin_tac "a ± h = x", blast)
done

lemma (in Ring) belong_ar_coset2:"ideal R I; a  carrier R; x  a ⊎⇘RI 
                                     x ± (-a a)  I"
apply (cut_tac ring_is_ag)
apply (frule mem_ar_coset1, assumption+, erule bexE)
 apply (frule sym, thin_tac "h ± a = x", simp)
 apply (frule_tac h = h in ideal_subset[of "I"], assumption)
 apply (frule aGroup.ag_mOp_closed[of "R" "a"], assumption)
 apply (subst aGroup.ag_pOp_assoc, assumption+,
        simp add:aGroup.ag_r_inv1,
        simp add:aGroup.ag_r_zero)
done

lemma (in Ring) ar_c_top: "ideal R I; a  carrier R; b  carrier R
        (c_top (b_ag R) I (a ⊎⇘RI) (b ⊎⇘RI)) = (a ± b) ⊎⇘RI"
apply (cut_tac ring_is_ag, frule ideal_asubg,
       frule aGroup.asubg_nsubg[of "R" "I"], assumption,
       frule aGroup.b_ag_group[of "R"])
apply (simp add:ar_coset_def)
apply (subst Group.c_top_welldef[THEN sym], assumption+)
apply (simp add:aGroup.ag_carrier_carrier)+
apply (simp add:aGroup.agop_gop)
done

text‹Following lemma is not necessary to define a quotient ring. But
it makes clear that the binary operation2 of the quotient ring is well
defined.›

lemma (in Ring) quotient_ring_tr1:"ideal R I; a1  carrier R; a2  carrier R;
                b1  carrier R; b2  carrier R;
                a1 ⊎⇘RI = a2 ⊎⇘RI; b1 ⊎⇘RI = b2 ⊎⇘RI 
                             (a1 r b1) ⊎⇘RI = (a2 r b2) ⊎⇘RI"
apply (rule ar_coset_same1, assumption+)
 apply (simp add: ring_tOp_closed)+
apply (frule ar_coset_same2 [of "I" "a1" "a2"], assumption+)
apply (frule ar_coset_same2 [of "I" "b1" "b2"], assumption+)
apply (frule ring_distrib4[of "a2" "b2" "a1" "b1"], assumption+)
 apply simp
 apply (rule ideal_pOp_closed[of "I"], assumption)
 apply (simp add:ideal_ring_multiple, simp add:ideal_ring_multiple1)
done

definition
  rcostOp :: "[_, 'a set]  (['a set, 'a set]  'a set)" where
  "rcostOp R I = (λX(set_rcs (b_ag R) I). λY(set_rcs (b_ag R) I).
                {z.  x  X.  y  Y. hI. (x rRy) ±Rh = z})"

lemma (in Ring) rcostOp:"ideal R I; a  carrier R; b  carrier R 
                    rcostOp R I (a ⊎⇘RI) (b ⊎⇘RI) = (a r b) ⊎⇘RI"
apply (cut_tac ring_is_ag)
 apply (frule ar_cos_mem[of "I" "a"], assumption+)
 apply (frule ar_cos_mem[of "I" "b"], assumption+)
apply (simp add:rcostOp_def)
apply (rule equalityI)
 apply (rule subsetI, simp) apply (erule bexE)+
 apply (rule belong_ar_coset1, assumption+)
 apply (simp add:ring_tOp_closed)
 apply (frule sym, thin_tac "xa r y ± h = x", simp)
 apply (rule aGroup.ag_pOp_closed, assumption)
 apply (frule_tac x = xa in ar_coset_mem2[of "I" "a"], assumption+,
        frule_tac x = y in ar_coset_mem2[of "I" "b"], assumption+,
        (erule bexE)+, simp)
 apply (rule ring_tOp_closed, rule aGroup.ag_pOp_closed, assumption+,
        simp add:ideal_subset)
 apply (rule aGroup.ag_pOp_closed, assumption+, simp add:ideal_subset,
        simp add:ideal_subset)
 apply (frule sym, thin_tac "xa r y ± h = x", simp)
 apply (frule_tac x = xa in belong_ar_coset2[of "I" "a"], assumption+,
        frule_tac x = y in belong_ar_coset2[of "I" "b"], assumption+)
 apply (frule_tac x = xa in ar_coset_subsetD[of "I" "a"], assumption+,
        frule_tac x = y in ar_coset_subsetD[of "I" "b"], assumption+)
 apply (subst aGroup.ag_pOp_commute, assumption,
        simp add:ring_tOp_closed, simp add:ideal_subset)
 apply (subst aGroup.ag_pOp_assoc, assumption,
        simp add:ideal_subset, simp add:ring_tOp_closed,
        rule aGroup.ag_mOp_closed, simp add:ring_tOp_closed,
        simp add:ring_tOp_closed)
 apply (rule ideal_pOp_closed, assumption+)
 apply (rule_tac a = xa and a' = y and b = a and b' = b in times_modTr,
        assumption+)

 apply (rule subsetI, simp)
 apply (frule_tac x = x in ar_coset_mem2[of "I" "a r b"],
        simp add:ring_tOp_closed, assumption)
 apply (erule bexE) apply simp
 apply (frule a_in_ar_coset[of "I" "a"], assumption+,
        frule a_in_ar_coset[of "I" "b"], assumption+)
 apply blast
done

definition
  qring ::  "[('a, 'm) Ring_scheme, 'a set]   carrier :: 'a set set,
    pop :: ['a  set, 'a set]  'a set, mop :: 'a set  'a set,
    zero :: 'a set, tp :: ['a  set, 'a set]  'a set, un :: 'a set " where
  "qring R I =  carrier = set_rcs (b_ag R) I,
    pop = c_top (b_ag R) I,
    mop = c_iop (b_ag R) I,
    zero = I,
    tp = rcostOp R I,
    un = 1rR⇙ ⊎⇘RI"

abbreviation
  QRING  (infixl "'/r" 200) where
  "R /r I == qring R I"

lemma (in Ring) carrier_qring:"ideal R I 
                               carrier (qring R I) = set_rcs (b_ag R) I"
by (simp add:qring_def)

lemma (in Ring) carrier_qring1:"ideal R I 
                                carrier (qring R I) = set_ar_cos R I"
apply (cut_tac ring_is_ag)
apply (simp add:carrier_qring set_rcs_def set_ar_cos_def)
apply (simp add:ar_coset_def aGroup.ag_carrier_carrier)
done

lemma (in Ring) qring_ring:"ideal R I  Ring (qring R I)"
apply (cut_tac ring_is_ag)
apply (frule ideal_asubg[of "I"],
        frule aGroup.asubg_nsubg[of "R" "I"], assumption,
        frule aGroup.b_ag_group[of "R"])
apply (subst Ring_def, simp)
apply (rule conjI)
 apply (rule Pi_I)+
 apply (simp add:carrier_qring, simp add:set_rcs_def, (erule bexE)+)
 apply (subst qring_def, simp)
 apply (subst Group.c_top_welldef[THEN sym, of "b_ag R" "I"], assumption+)
 apply (blast dest: Group.mult_closed[of "b_ag R"])
apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (simp add:qring_def)
 apply (simp add:Group.Qg_tassoc[of "b_ag R" "I"])
apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (simp add:qring_def)
 apply (simp add:set_rcs_def, (erule bexE)+, simp)
 apply (subst Group.c_top_welldef[THEN sym, of "b_ag R" "I"], assumption+)+
 apply (simp add:aGroup.agop_gop)
 apply (simp add:aGroup.ag_carrier_carrier)
 apply (simp add:aGroup.ag_pOp_commute)
apply (rule conjI)
 apply (simp add:qring_def Group.Qg_iop_closed)
apply (rule conjI)
 apply (rule allI, rule impI)
 apply (simp add:qring_def)
 apply (simp add:Group.Qg_i[of "b_ag R" "I"])
apply (rule conjI)
 apply (simp add:qring_def)
 apply (frule Group.nsg_sg[of "b_ag R" "I"], assumption)
 apply (simp add:Group.unit_rcs_in_set_rcs)
apply (rule conjI)
 apply (rule allI, rule impI)
 apply (simp add:qring_def)
 apply (simp add:Group.Qg_unit[of "b_ag R" "I"])
apply (rule conjI)
apply(rule Pi_I)+
 apply (simp add:qring_def aGroup.aqgrp_carrier)
 apply (simp add:set_ar_cos_def, (erule bexE)+, simp add:rcostOp,
        blast dest: ring_tOp_closed)
apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (simp add:qring_def aGroup.aqgrp_carrier)
 apply (simp add:set_ar_cos_def, (erule bexE)+, simp add:rcostOp)
 apply (frule_tac x = aa and y = ab in ring_tOp_closed, assumption+,
        frule_tac x = ab and y = ac in ring_tOp_closed, assumption+,
        simp add:rcostOp, simp add:ring_tOp_assoc)
apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (simp add:qring_def aGroup.aqgrp_carrier)
 apply (simp add:set_ar_cos_def, (erule bexE)+, simp add:rcostOp,
        simp add:ring_tOp_commute)
apply (rule conjI)
 apply (simp add:qring_def aGroup.aqgrp_carrier)
 apply (cut_tac ring_one, simp add:set_ar_cos_def, blast)
apply (rule conjI)
 apply (rule allI, rule impI)+
 apply (simp add:qring_def aGroup.aqgrp_carrier)
 apply (simp add:set_ar_cos_def, (erule bexE)+, simp)
 apply (simp add:ar_c_top rcostOp)
 apply (frule_tac x = ab and y = ac in aGroup.ag_pOp_closed,
                  assumption+,
        frule_tac x = aa and y = ab in ring_tOp_closed, assumption+ ,
        frule_tac x = aa and y = ac in ring_tOp_closed, assumption+)
 apply (simp add:ar_c_top rcostOp, simp add:ring_distrib1)
apply (rule allI, rule impI)
  apply (simp add:qring_def aGroup.aqgrp_carrier)
  apply (simp add:set_ar_cos_def, erule bexE, simp)
  apply (cut_tac ring_one)
  apply (simp add:rcostOp, simp add:ring_l_one)
done

lemma (in Ring) qring_carrier:"ideal R I 
              carrier (qring R I)  = {X. a carrier R. a ⊎⇘RI = X}"
apply (simp add:carrier_qring1 set_ar_cos_def)
apply (rule equalityI)
 apply (rule subsetI, simp, erule bexE, frule sym, thin_tac "x = a ⊎⇘RI",
        blast)
apply (rule subsetI, simp, erule bexE, frule sym, thin_tac "a ⊎⇘RI = x",
       blast)
done

lemma (in Ring) qring_mem:"ideal R I; a  carrier R 
                                 a ⊎⇘RI  carrier (qring R I)"
apply (simp add:qring_carrier)
apply blast
done

lemma (in Ring) qring_pOp:"ideal R I; a  carrier R; b  carrier R 
  pop (qring R I) (a ⊎⇘RI) (b ⊎⇘RI) = (a ± b) ⊎⇘RI"
by (simp add:qring_def, simp add:ar_c_top)

lemma (in Ring) qring_zero:"ideal R I  zero (qring R I) = I"
apply (simp add:qring_def)
done

lemma (in Ring) qring_zero_1:"a  carrier R; ideal R I; a ⊎⇘RI = I 
                                    a  I"
by (frule a_in_ar_coset [of "I" "a"], assumption+, simp)

lemma (in Ring) Qring_fix1:"a  carrier R; ideal R I; a  I  a ⊎⇘RI = I"
apply (cut_tac ring_is_ag, frule aGroup.b_ag_group)
apply (simp add:ar_coset_def)
apply (frule ideal_asubg[of "I"], simp add:asubGroup_def)
apply (simp add:Group.rcs_fixed2[of "b_ag R" "I"])
done

lemma (in Ring) ar_cos_same:"a  carrier R; ideal R I; x  a ⊎⇘RI 
                                x ⊎⇘RI = a ⊎⇘RI"
apply (cut_tac ring_is_ag)
apply (rule ar_coset_same1[of "I" "x" "a"], assumption+)
apply (rule ar_coset_subsetD[of "I"], assumption+)
apply (frule ar_coset_mem2[of "I" "a" "x"], assumption+,
       erule bexE)
apply (frule_tac h = h in ideal_subset[of "I"], assumption,
      simp add:aGroup.ag_p_inv)
apply (frule_tac x = a in aGroup.ag_mOp_closed[of "R"], assumption+,
       frule_tac x = h in aGroup.ag_mOp_closed[of "R"], assumption+)
apply (simp add:aGroup.ag_pOp_assoc[THEN sym],
       simp add:aGroup.ag_r_inv1 aGroup.ag_l_zero)
apply (simp add:ideal_inv1_closed)
done

lemma (in Ring) qring_tOp:"ideal R I; a  carrier R; b  carrier R 
                tp (qring R I) (a ⊎⇘RI) (b ⊎⇘RI) = (a r b) ⊎⇘RI"
by (simp add:qring_def, simp add:rcostOp)

lemma rind_hom_well_def:"Ring A; Ring R; f  rHom A R; a  carrier A  
                                   f a = (f°⇘A,R) (a ⊎⇘A(ker⇘A,Rf))"
apply (frule ker_ideal[of "A" "R" "f"], assumption+)
apply (frule Ring.mem_set_ar_cos[of "A" "ker⇘A,Rf" "a"], assumption+)
apply (simp add:rind_hom_def)
 apply (rule someI2_ex)
 apply (frule Ring.a_in_ar_coset [of "A" "ker⇘A,Rf" "a"], assumption+, blast)
 apply (frule_tac x = x in Ring.ar_coset_mem2[of "A" "ker⇘A,Rf" "a"],
           assumption+, erule bexE, simp,
        frule_tac h = h in Ring.ideal_subset[of "A" "ker⇘A,Rf"], assumption+)
 apply (frule_tac Ring.ring_is_ag[of "A"],
        frule_tac Ring.ring_is_ag[of "R"],
        simp add:rHom_def, frule conjunct1, simp add:aHom_add)
 apply (simp add:ker_def)
 apply (frule aHom_mem[of "A" "R" "f" "a"], assumption+,
        simp add:aGroup.ag_r_zero)
done

lemma (in Ring) set_r_ar_cos:"ideal R I 
                 set_rcs (b_ag R) I = set_ar_cos R I"
 apply (simp add:set_ar_cos_def set_rcs_def ar_coset_def)
 apply (cut_tac ring_is_ag)
 apply (simp add:aGroup.ag_carrier_carrier)
done

lemma set_r_ar_cos_ker:"Ring A; Ring R; f  rHom A R  
                     set_rcs (b_ag A) (ker⇘A,Rf) = set_ar_cos A (ker⇘A,Rf)"
apply (frule ker_ideal[of "A" "R" "f"], assumption+)
 apply (simp add:Ring.carrier_qring[THEN sym],
        simp add:Ring.carrier_qring1[THEN sym])
done

lemma ind_hom_rhom:"Ring A; Ring R; f  rHom A R 
                                    (f°⇘A,R)  rHom (qring A (ker⇘A,Rf)) R"
apply (simp add:rHom_def [of "qring A (ker⇘A,Rf)" "R"])
apply (rule conjI)
 apply (simp add:aHom_def)
 apply (rule conjI)
 apply (simp add:qring_def)
apply (simp add:rind_hom_def extensional_def)
apply (rule Pi_I)
 apply (frule Ring.ring_is_ag [of "A"], frule Ring.ring_is_ag [of "R"],
        frule aGroup.b_ag_group [of "R"])
 apply (simp add:aGroup.ag_carrier_carrier [THEN sym])
 apply (simp add:set_ar_cos_def)
 apply (rule conjI)
 apply (rule impI)
 apply (erule bexE, simp)
 apply (frule ker_ideal [of "A" "R" "f"], assumption+)
 apply (frule_tac a = a in Ring.a_in_ar_coset [of "A" "ker⇘A,Rf"],
        assumption+)
 apply (rule someI2_ex, blast)
 apply (frule_tac I = "ker⇘A,Rf" and a = a and x = xa in
                   Ring.ar_coset_subsetD[of "A"], assumption+)
 apply (simp add:aGroup.ag_carrier_carrier, simp add:rHom_mem)
 apply (simp add:set_r_ar_cos_ker, simp add:set_ar_cos_def, rule impI, blast)
apply (rule conjI)
 apply (simp add:qring_def)
 apply (simp add:set_r_ar_cos_ker)
 apply (simp add:rind_hom_def extensional_def)
apply (rule ballI)+
 apply (simp add:qring_def)
 apply (simp add:set_r_ar_cos_ker)
 apply (simp add:set_ar_cos_def)
 apply ((erule bexE)+, simp)
 apply (frule ker_ideal[of "A" "R" "f"], assumption+)
 apply (simp add:Ring.ar_c_top)
 apply (frule Ring.ring_is_ag[of "A"],
        frule Ring.ring_is_ag[of "R"],
        frule_tac x = aa and y = ab in aGroup.ag_pOp_closed[of "A"],
        assumption+)
 apply (simp add:rind_hom_well_def[THEN sym])
 apply (simp add:rHom_def, frule conjunct1, simp add:aHom_add)
apply (rule conjI)
 apply (rule ballI)+
 apply (frule ker_ideal[of "A" "R" "f"], assumption+,
        simp add:Ring.carrier_qring1, simp add:set_ar_cos_def,
        (erule bexE)+, simp add:qring_def Ring.rcostOp)
 apply (frule Ring.ring_is_ag[of "A"],
         frule_tac x = a and y = aa in Ring.ring_tOp_closed[of "A"],
         assumption+)
 apply (simp add:rind_hom_well_def[THEN sym], simp add:rHom_tOp)

apply (simp add:qring_def)
 apply (frule Ring.ring_one[of "A"],
        simp add:rind_hom_well_def[THEN sym],
        simp add:rHom_one)
done

lemma ind_hom_injec:"Ring A; Ring R; f  rHom A R 
                              injec⇘(qring A (ker⇘A,Rf)),R(f°⇘A,R)"
apply (simp add:injec_def)
apply (frule ind_hom_rhom [of "A" "R" "f"], assumption+)
apply (frule rHom_aHom[of "f°⇘A,R⇙" "A /r (ker⇘A,Rf)" "R"], simp)
 apply (simp add:ker_def[of _ _ "f°⇘A,R⇙"])
apply ((subst qring_def)+, simp)
 apply (simp add:set_r_ar_cos_ker)

apply (frule Ring.ring_is_ag[of "A"],
       frule Ring.ring_is_ag[of "R"],
       frule ker_ideal[of "A" "R" "f"], assumption+)
apply (rule equalityI)
 apply (rule subsetI)
 apply (simp, erule conjE)
 apply (simp add:set_ar_cos_def, erule bexE, simp)
 apply (simp add:rind_hom_well_def[THEN sym, of "A" "R" "f"],
        thin_tac "x = a ⊎⇘A⇙ ker⇘A,Rf")
 apply (rule_tac a = a in Ring.Qring_fix1[of "A" _ "ker⇘A,Rf"], assumption+)
 apply (simp add:ker_def)

 apply (rule subsetI, simp)
 apply (simp add:Ring.I_in_set_ar_cos[of "A" "ker⇘A,Rf"])
 apply (frule Ring.ideal_zero[of "A" "ker⇘A,Rf"], assumption+,
        frule Ring.ring_zero[of "A"])

 apply (frule Ring.ar_coset_same4[of "A" "ker⇘A,Rf" "𝟬A⇙"], assumption+)
 apply (frule rind_hom_well_def[THEN sym, of "A" "R" "f" "𝟬A⇙"], assumption+)
 apply simp

 apply (rule rHom_0_0, assumption+)
done

lemma rhom_to_rimg:"Ring A; Ring R; f  rHom A R 
                                   f  rHom A (rimg A R f)"
apply (frule Ring.ring_is_ag[of "A"], frule Ring.ring_is_ag[of "R"])
apply (subst rHom_def, simp)
apply (rule conjI)
 apply (subst aHom_def, simp)
 apply (rule conjI)
 apply (simp add:rimg_def)
 apply (rule conjI)
  apply (simp add:rHom_def aHom_def)
  apply ((rule ballI)+, simp add:rimg_def)
 apply (rule aHom_add, assumption+)
  apply (simp add:rHom_aHom, assumption+)

 apply (rule conjI)
 apply ((rule ballI)+, simp add:rimg_def, simp add:rHom_tOp)

 apply (simp add:rimg_def, simp add:rHom_one)
done

lemma ker_to_rimg:"Ring A; Ring R; f  rHom A R  
                         ker⇘A,Rf = ker⇘A,(rimg A R f)f"
apply (frule rhom_to_rimg [of "A" "R" "f"], assumption+)
apply (simp add:ker_def)
apply (simp add:rimg_def)
done

lemma indhom_eq:"Ring A; Ring R; f  rHom A R  f°⇘A,(rimg A R f)= f°⇘A,R⇙"
apply (frule rimg_ring[of "A" "R" "f"], assumption+)
apply (frule rhom_to_rimg[of "A" "R" "f"], assumption+,
       frule ind_hom_rhom[of "A" "rimg A R f"], assumption+,
       frule ind_hom_rhom[of "A" "R" "f"], assumption+) (** extensional **)
apply (rule funcset_eq[of "f°⇘A,rimg A R f⇙ " "carrier (A /r (ker⇘A,Rf))" "f°⇘A,R⇙"])
 apply (simp add:ker_to_rimg[THEN sym],
        simp add:rHom_def[of _ "rimg A R f"] aHom_def)
 apply (simp add:rHom_def[of _ "R"] aHom_def)

apply (simp add:ker_to_rimg[THEN sym])
 apply (rule ballI)
 apply (frule ker_ideal[of "A" "R" "f"], assumption+,
        simp add:Ring.carrier_qring1)
 apply (simp add:set_ar_cos_def, erule bexE, simp)
 apply (simp add:rind_hom_well_def[THEN sym])
 apply (frule rind_hom_well_def[THEN sym, of "A" "rimg A R f" "f"],
         assumption+, simp add:ker_to_rimg[THEN sym])
done

lemma indhom_bijec2_rimg:"Ring A; Ring R; f  rHom A R 
                    bijec⇘(qring A (ker⇘A,Rf)),(rimg A R f)(f°⇘A,R)"
apply (frule rimg_ring [of "A" "R" "f"], assumption+)
apply (frule rhom_to_rimg[of "A" "R" "f"], assumption+)
apply (frule ind_hom_rhom[of "A" "rimg A R f" "f"], assumption+)
 apply (frule ker_to_rimg[THEN sym, of "A" "R" "f"], assumption+)
 apply (frule indhom_eq[of "A" "R" "f"], assumption+)
apply simp
 apply (simp add:bijec_def)
 apply (rule conjI)
  apply (simp add:injec_def)
   apply (rule conjI)
   apply (simp add:rHom_def)
   apply (frule ind_hom_injec [of "A" "R" "f"], assumption+)
   apply (simp add:injec_def)
   apply (simp add:ker_def [of _ _ "f°⇘A,R⇙"])
   apply (simp add:rimg_def)

  apply (simp add:surjec_def)
   apply (rule conjI)
   apply (simp add:rHom_def)
   apply (rule surj_to_test)
   apply (simp add:rHom_def aHom_def)
   apply (rule ballI)
   apply (simp add:rimg_carrier)
   apply (simp add:image_def)
   apply (erule bexE, simp)
   apply (frule_tac a1 = x in rind_hom_well_def[THEN sym, of "A" "R" "f"],
                   assumption+)
   apply (frule ker_ideal[of "A" "R" "f"], assumption+,
        simp add:Ring.carrier_qring1,
        frule_tac a = x in Ring.mem_set_ar_cos[of "A" "ker⇘A,Rf"], assumption+)
 apply blast
done

lemma surjec_ind_bijec:"Ring A; Ring R; f  rHom A R; surjec⇘A,Rf 
     bijec⇘(qring A (ker⇘A,Rf)),R(f°⇘A,R)"
apply (frule ind_hom_rhom[of "A" "R" "f"], assumption+)
apply (simp add:surjec_def)
apply (simp add:bijec_def)
 apply (simp add:ind_hom_injec)

 apply (simp add:surjec_def)
   apply (simp add:rHom_aHom)
   apply (rule surj_to_test)
   apply (simp add:rHom_def aHom_def)
   apply (rule ballI)
   apply (simp add:surj_to_def, frule sym,
                        thin_tac "f ` carrier A = carrier R", simp,
                        thin_tac "carrier R = f ` carrier A")
   apply (simp add:image_def, erule bexE)
   apply (frule_tac a1 = x in rind_hom_well_def[THEN sym, of "A" "R" "f"],
                   assumption+)
   apply (frule ker_ideal[of "A" "R" "f"], assumption+,
        simp add:Ring.carrier_qring1,
        frule_tac a = x in Ring.mem_set_ar_cos[of "A" "ker⇘A,Rf"], assumption+)
 apply blast
done

lemma ridmap_ind_bijec:"Ring A 
     bijec⇘(qring A (ker⇘A,A(ridmap A))),A((ridmap A)°⇘A,A)"
apply (frule ridmap_surjec[of "A"])
apply (rule surjec_ind_bijec [of "A" "A" "ridmap A"], assumption+)
 apply (simp add:rHom_def, simp add:surjec_def)

 apply (rule conjI)
  apply (rule ballI)+
  apply (frule_tac x = x and y = y in Ring.ring_tOp_closed[of "A"],
          assumption+, simp add:ridmap_def)
  apply (simp add:ridmap_def Ring.ring_one)

 apply assumption
done

lemma ker_of_idmap:"Ring A  ker⇘A,A(ridmap A) = {𝟬A}"
apply (simp add:ker_def)
apply (simp add:ridmap_def)
apply (rule equalityI)
 apply (rule subsetI) apply (simp add:CollectI)
 apply (rule subsetI) apply (simp add:CollectI)

 apply (simp add:Ring.ring_zero)
done

lemma ring_natural_isom:"Ring A 
         bijec⇘(qring A {𝟬A}),A((ridmap A)°⇘A,A)"
apply (frule ridmap_ind_bijec)
apply (simp add: ker_of_idmap)
done           (** A /r {0A}⇩ ≅ A **)

definition
  pj :: "[('a, 'm) Ring_scheme, 'a set]  ('a => 'a set)" where
  "pj R I = (λx. Pj (b_ag R) I x)"

 (* pj is projection homomorphism *)

lemma pj_Hom:"Ring R; ideal R I  (pj R I)  rHom R (qring R I)"
apply (simp add:rHom_def)
apply (rule conjI)
apply (simp add:aHom_def)
 apply (rule conjI)
 apply (rule Pi_I)
 apply (simp add:qring_def)
 apply (frule Ring.ring_is_ag)
 apply (simp add:aGroup.ag_carrier_carrier [THEN sym])
 apply (simp add:pj_def Pj_def)
 apply (simp add:set_rcs_def) apply blast
apply (rule conjI)
 apply (simp add:pj_def Pj_def extensional_def)
 apply (frule Ring.ring_is_ag) apply (simp add:aGroup.ag_carrier_carrier)
apply (rule ballI)+
 apply (frule Ring.ring_is_ag)
 apply (frule_tac x = a and y = b in aGroup.ag_pOp_closed, assumption+)
 apply (simp add:aGroup.ag_carrier_carrier [THEN sym])
 apply (simp add:pj_def Pj_def)
 apply (simp add:qring_def) apply (frule aGroup.b_ag_group)
 apply (simp add:aGroup.agop_gop [THEN sym])
 apply (subst Group.c_top_welldef[of "b_ag R" "I"], assumption+)
 apply (frule Ring.ideal_asubg[of "R" "I"], assumption+)
 apply (simp add:aGroup.asubg_nsubg)
 apply assumption+
 apply simp

apply (rule conjI)
 apply (rule ballI)+
 apply (simp add: qring_def)
 apply (frule_tac x = x and y = y in Ring.ring_tOp_closed, assumption+)
 apply (frule Ring.ring_is_ag)
 apply (simp add:aGroup.ag_carrier_carrier [THEN sym])
 apply (simp add:pj_def Pj_def)
 apply (simp add:aGroup.ag_carrier_carrier)

 apply (frule_tac a1 = x and b1 = y in Ring.rcostOp [THEN sym, of "R" "I"],
                                                             assumption+)
 apply (simp add:ar_coset_def)
apply (simp add:qring_def)
 apply (frule Ring.ring_one)
 apply (frule Ring.ring_is_ag)
 apply (simp add:aGroup.ag_carrier_carrier [THEN sym])
 apply (simp add:pj_def Pj_def)
 apply (simp add:ar_coset_def)
done

lemma pj_mem:"Ring R; ideal R I; x  carrier R  pj R I x = x ⊎⇘RI"
apply (frule Ring.ring_is_ag)
apply (simp add:aGroup.ag_carrier_carrier [THEN sym])
apply (simp add:pj_def Pj_def)
apply (simp add:ar_coset_def)
done

lemma pj_zero:"Ring R; ideal R I; x  carrier R 
                         (pj R I x = 𝟬(R /r I)) = (x  I)"
apply (rule iffI)
apply (simp add:pj_mem Ring.qring_zero,
       simp add:Ring.qring_zero_1[of "R" "x" "I"])
apply (simp add:pj_mem Ring.qring_zero,
       rule Ring.Qring_fix1, assumption+)
done

lemma pj_surj_to:"Ring R; ideal R J; X  carrier (R /r J) 
                   r carrier R. pj R J r = X"
apply (simp add:qring_def set_rcs_def,
       fold ar_coset_def, simp add:b_ag_def, erule bexE,
       frule_tac x = a in pj_mem[of R J], assumption+, simp)
 apply blast
done

lemma invim_of_ideal:"Ring R; ideal R I; ideal (qring R I) J  
  ideal R (rInvim R (qring R I) (pj R I) J)"
apply (rule Ring.ideal_condition, assumption)
 apply (simp add:rInvim_def) 
apply (subgoal_tac "𝟬R rInvim R (qring R I) (pj R I) J")
apply (simp add:nonempty)
apply (simp add:rInvim_def)
apply (simp add: Ring.ring_zero)
 apply (frule Ring.ring_is_ag)
 apply (frule pj_Hom [of "R" "I"], assumption+)
 apply (frule Ring.qring_ring [of "R" "I"], assumption+)
 apply (frule rHom_0_0 [of "R" "R /r I" "pj R I"], assumption+)
 apply (simp add:Ring.ideal_zero)
apply (rule ballI)+
 apply (simp add:rInvim_def) apply (erule conjE)+
 apply (rule conjI)
 apply (frule Ring.ring_is_ag)
 apply (rule aGroup.ag_pOp_closed, assumption+)
 apply (rule aGroup.ag_mOp_closed, assumption+)
 apply (frule pj_Hom [of "R" "I"], assumption+)
 apply (frule Ring.ring_is_ag)
 apply (frule_tac x = y in aGroup.ag_mOp_closed [of "R"], assumption+)
 apply (simp add:rHom_def) apply (erule conjE)+
 apply (subst aHom_add [of "R" "R /r I" "pj R I"], assumption+)
 apply (simp add:Ring.qring_ring Ring.ring_is_ag)
 apply assumption+
apply (frule Ring.qring_ring [of "R" "I"], assumption+)
 apply (rule Ring.ideal_pOp_closed, assumption+)
 apply (subst aHom_inv_inv[of "R" "R /r I" "pj R I"], assumption+)
 apply (simp add:Ring.ring_is_ag) apply assumption+
 apply (frule_tac x = "pj R I y" in Ring.ideal_inv1_closed [of "R /r I" "J"],
                                              assumption+)
apply (rule ballI)+
 apply (simp add:rInvim_def) apply (erule conjE)
 apply (simp add:Ring.ring_tOp_closed)
 apply (frule pj_Hom [of "R" "I"], assumption+)
 apply (subst rHom_tOp [of "R" "R /r I" _ _ "pj R I"], assumption+)
 apply (frule Ring.qring_ring[of "R" "I"], assumption+)
 apply (rule Ring.ideal_ring_multiple [of "R /r I" "J"])
 apply (simp add:Ring.qring_ring) apply assumption+
 apply (simp add:rHom_mem)
done

lemma pj_invim_cont_I:"Ring R; ideal R I; ideal (qring R I) J 
                         I  (rInvim R (qring R I) (pj R I) J)"
apply (rule subsetI)
 apply (simp add:rInvim_def)
 apply (frule Ring.ideal_subset [of "R" "I"], assumption+)
 apply simp
 apply (frule  pj_mem [of "R" "I"  _], assumption+)
 apply (simp add:Ring.ar_coset_same4)
apply (frule  Ring.qring_ring[of "R" "I"], assumption+)
apply (frule Ring.ideal_zero [of "qring R I" "J"], assumption+)

apply (frule Ring.qring_zero[of "R" "I"], assumption)
 apply simp
done

lemma pj_invim_mono1:"Ring R; ideal R I; ideal (qring R I) J1;
      ideal (qring R I) J2; J1  J2  
      (rInvim R (qring R I) (pj R I) J1)  (rInvim R (qring R I) (pj R I) J2)"
apply (rule subsetI)
apply (simp add:rInvim_def)
apply (simp add:subsetD)
done

lemma pj_img_ideal:"Ring R; ideal R I; ideal R J; I  J 
                                  ideal (qring R I) ((pj R I)`J)"
apply (rule Ring.ideal_condition [of "qring R I" "(pj R I) `J"])
apply (simp add:Ring.qring_ring)
apply (rule subsetI, simp add:image_def)
 apply (erule bexE)
 apply (frule_tac h = xa in Ring.ideal_subset [of "R" "J"], assumption+)
 apply (frule pj_Hom [of "R" "I"], assumption+)
 apply (simp add:rHom_mem)
 apply (frule Ring.ideal_zero [of "R" "J"], assumption+)
 apply (simp add:image_def) apply blast
apply (rule ballI)+
 apply (simp add:image_def)
 apply (erule bexE)+
 apply (frule pj_Hom [of "R" "I"], assumption+)
 apply (rename_tac x y s t)
 apply (frule_tac h = s in Ring.ideal_subset [of "R" "J"], assumption+)
 apply (frule_tac h = t in Ring.ideal_subset [of "R" "J"], assumption+)
 apply (simp add:rHom_def)   apply (erule conjE)+
 apply (frule Ring.ring_is_ag)
 apply (frule Ring.qring_ring [of "R" "I"], assumption+)
 apply (frule Ring.ring_is_ag [of "R /r I"])
  apply (frule_tac x = t in aGroup.ag_mOp_closed [of "R"], assumption+)
 apply (frule_tac a1 = s and b1 = "-aRt" in aHom_add [of "R" "R /r I"
  "pj R I", THEN sym], assumption+) apply (simp add:aHom_inv_inv)
 apply (frule_tac x = t in Ring.ideal_inv1_closed [of "R" "J"], assumption+)
 apply (frule_tac x = s and y = "-aRt" in Ring.ideal_pOp_closed [of "R" "J"],
                                             assumption+)
 apply blast
apply (rule ballI)+
apply (simp add:qring_def)
 apply (simp add:Ring.set_r_ar_cos)
 apply (simp add:set_ar_cos_def, erule bexE)
 apply simp
 apply (simp add:image_def)
 apply (erule bexE)
 apply (frule_tac x = xa in pj_mem [of "R" "I"], assumption+)
 apply (simp add:Ring.ideal_subset) apply simp
 apply (subst Ring.rcostOp, assumption+)
    apply (simp add:Ring.ideal_subset)
 apply (frule_tac x = xa and r = a in Ring.ideal_ring_multiple [of "R" "J"],
                                                  assumption+)
 apply (frule_tac h = "a rRxa" in Ring.ideal_subset [of "R" "J"],
                                                                 assumption+)
 apply (frule_tac x1 = "a rRxa" in pj_mem [THEN sym, of "R" "I"],
                                                                 assumption+)
 apply simp
 apply blast
done

lemma npQring:"Ring R; ideal R I; a  carrier R 
      npow (qring R I) (a ⊎⇘RI) n = (npow R a n) ⊎⇘RI"
apply (induct_tac n)
apply (simp add:qring_def)

apply (simp add:qring_def)
apply (rule Ring.rcostOp, assumption+)
apply (rule Ring.npClose, assumption+)
done

section "Primary ideals, Prime ideals"

definition
  maximal_set :: "['a set set, 'a set]  bool" where
  "maximal_set S mx  mx  S  (sS. mx  s  s = mx)"

definition
  nilpotent :: "[_, 'a]  bool" where
  "nilpotent R a  ((n::nat). a^⇗R n= 𝟬R)"

definition
 zero_divisor :: "[_, 'a]  bool" where
  "zero_divisor R a  (x carrier R. x  𝟬R x rRa = 𝟬R)"

definition
  primary_ideal :: "[_, 'a set]  bool" where
  "primary_ideal R q  ideal R q  (1rR)  q 
    (x carrier R. y carrier R.
      x rRy  q   (n. (npow R x n)  q  y  q))"

definition
  prime_ideal :: "[_, 'a set]  bool" where
  "prime_ideal R p  ideal R p  (1rR)  p  (x carrier R. y carrier R.
    (x rRy  p  x  p  y  p))"

definition
  maximal_ideal :: "[_, 'a set]  bool" where
  "maximal_ideal R mx  ideal R mx  1rR mx 
        {J. (ideal R J  mx  J)} = {mx, carrier R}"

lemma (in Ring) maximal_ideal_ideal:"maximal_ideal R mx  ideal R mx"
by (simp add:maximal_ideal_def)

lemma (in Ring) maximal_ideal_proper:"maximal_ideal R mx  1r  mx"
by (simp add:maximal_ideal_def)

lemma (in Ring) prime_ideal_ideal:"prime_ideal R I  ideal R I"
by (simp add:prime_ideal_def)

lemma (in Ring) prime_ideal_proper:"prime_ideal R I  I  carrier R"
apply (simp add:prime_ideal_def, (erule conjE)+)
apply (simp add:proper_ideal)
done

lemma (in Ring) prime_ideal_proper1:"prime_ideal R p  1r  p"
by (simp add:prime_ideal_def)

lemma (in Ring) primary_ideal_ideal:"primary_ideal R q  ideal R q"
by (simp add:primary_ideal_def)

lemma (in Ring)  primary_ideal_proper1:"primary_ideal R q  1r  q"
by (simp add:primary_ideal_def)

lemma (in Ring) prime_elems_mult_not:"prime_ideal R P; x  carrier R;
                y  carrier R; x  P; y  P   x r y  P"
apply (simp add:prime_ideal_def, (erule conjE)+)
apply (rule contrapos_pp, simp+)
 apply (frule_tac x = x in bspec, assumption,
        thin_tac "xcarrier R. ycarrier R. x r y  P  x  P  y  P",
        frule_tac x = y in bspec, assumption,
        thin_tac "ycarrier R. x r y  P  x  P  y  P", simp)
done


lemma (in Ring) prime_is_primary:"prime_ideal R p  primary_ideal R p"
apply (unfold primary_ideal_def)
apply (rule conjI, simp add:prime_ideal_def)
apply (rule conjI, simp add:prime_ideal_def)
apply ((rule ballI)+, rule impI)
apply (simp add:prime_ideal_def, (erule conjE)+)
 apply (frule_tac x = x in bspec, assumption,
        thin_tac "xcarrier R. ycarrier R. x r y  p  x  p  y  p",
        frule_tac x = y in bspec, assumption,
        thin_tac "ycarrier R. x r y  p  x  p  y  p", simp)
 apply (erule disjE)
 apply (frule_tac t = x in np_1[THEN sym])
 apply (frule_tac a = x and A = p and b = "x^⇗R (Suc 0)⇖" in eq_elem_in,
                                               assumption)
 apply blast
apply simp
done

lemma (in Ring) maximal_prime_Tr0:"maximal_ideal R mx; x  carrier R; x  mx
                mx  (Rxa R x) = carrier R"
apply (frule principal_ideal [of "x"])
 apply (frule maximal_ideal_ideal[of "mx"])
 apply (frule sum_ideals [of "mx" "Rxa R x"], assumption)
 apply (frule sum_ideals_la1 [of "mx" "Rxa R x"], assumption)
 apply (simp add:maximal_ideal_def)
 apply (erule conjE)+
 apply (subgoal_tac "mx  (Rxa R x)  {J. ideal R J  mx  J}")
 apply simp
apply (frule sum_ideals_la2 [of "mx" "Rxa R x"], assumption+)
  apply (frule a_in_principal [of "x"])
  apply (frule subsetD [of "Rxa R x" "mx  (Rxa R x)" "x"], assumption+)
 apply (thin_tac "{J. ideal R J  mx  J} = {mx, carrier R}")
apply (erule disjE)
 apply simp apply simp

apply (thin_tac "{J. ideal R J  mx  J} = {mx, carrier R}")
 apply simp
done

lemma (in Ring) maximal_prime:"maximal_ideal R mx  prime_ideal R mx"
apply (cut_tac ring_is_ag)
apply (simp add:prime_ideal_def)
apply (simp add:maximal_ideal_ideal)
apply (simp add:maximal_ideal_proper)

apply ((rule ballI)+, rule impI)
apply (rule contrapos_pp, simp+, erule conjE)
apply (frule_tac x = x in maximal_prime_Tr0[of "mx"], assumption+,
       frule_tac x = y in maximal_prime_Tr0[of "mx"], assumption+,
       frule maximal_ideal_ideal[of mx],
       frule ideal_subset1[of mx],
       frule_tac a = x in principal_ideal,
       frule_tac a = y in principal_ideal,
       frule_tac I = "R p x" in ideal_subset1,
       frule_tac I = "R p y" in ideal_subset1)
apply (simp add:aGroup.set_sum)
 apply (cut_tac ring_one)
 apply (frule sym,
        thin_tac "{xa. hmx. kR p x. xa = h ± k} = carrier R",
        frule sym,
        thin_tac "{x. hmx. kR p y. x = h ± k} = carrier R")
 apply (frule_tac a = "1r" and B = "{xa. imx. j(Rxa R x). xa = i ± j}" in
                         eq_set_inc[of _ "carrier R"], assumption,
        frule_tac a = "1r" and B = "{xa. imx. j(Rxa R y). xa = i ± j}" in
                         eq_set_inc[of _ "carrier R"], assumption,
        thin_tac "carrier R = {xa. imx. j(Rxa R x). xa = i ± j}",
        thin_tac "carrier R = {x. imx. j(Rxa R y). x = i ± j}")
 apply (drule CollectD, (erule bexE)+,
        frule sym, thin_tac "1r = i ± j")
 apply (drule CollectD, (erule bexE)+, rotate_tac -1,
        frule sym, thin_tac "1r = ia ± ja")
 apply (frule_tac h = i in ideal_subset[of mx], assumption,
        frule_tac h = ia in ideal_subset[of mx], assumption,
        frule_tac h = j in ideal_subset, assumption+,
        frule_tac h = ja in ideal_subset, assumption+)
 apply (cut_tac ring_one)
 apply (frule_tac x = i and y = j in aGroup.ag_pOp_closed, assumption+)
 apply (frule_tac x = "i ± j" and y = ia and z = ja in ring_distrib1,
           assumption+)
 apply (frule_tac x = ia and y = i and z = j in ring_distrib2, assumption+,
        frule_tac x = ja and y = i and z = j in ring_distrib2, assumption+,
        simp)
 apply (thin_tac "1r r ia = i r ia ± j r ia",
        thin_tac "1r r ja = i r ja ± j r ja",
        simp add:ring_l_one[of "1r"])
 apply (frule_tac x = ia and r = i in ideal_ring_multiple[of mx], assumption+,
        frule_tac x = i and r = j in ideal_ring_multiple1[of mx], assumption+,
        frule_tac x = i and r = ja in ideal_ring_multiple1[of mx], assumption+,
        frule_tac r = j and x = ia in ideal_ring_multiple[of mx], assumption+)
 apply (subgoal_tac "j r ja  mx")
 apply (frule_tac x = "i r ia" and y = "j r ia" in ideal_pOp_closed[of mx],
                   assumption+) apply (
        frule_tac x = "i r ja" and y = "j r ja" in ideal_pOp_closed[of mx],
           assumption+)
 apply (frule_tac x = "i r ia ± j r ia" and y = "i r ja ± j r ja" in
          ideal_pOp_closed[of mx], assumption+,
        thin_tac "i ± j = i r ia ± j r ia ± (i r ja ± j r ja)",
        thin_tac "ia ± ja = i r ia ± j r ia ± (i r ja ± j r ja)")
 apply (frule sym, thin_tac "1r = i r ia ± j r ia ± (i r ja ± j r ja)",
       simp)
 apply (simp add:maximal_ideal_def)

apply (thin_tac "i ± j = i r ia ± j r ia ± (i r ja ± j r ja)",
       thin_tac "ia ± ja = i r ia ± j r ia ± (i r ja ± j r ja)",
       thin_tac "i r ia ± j r ia ± (i r ja ± j r ja)  carrier R",
       thin_tac "1r = i r ia ± j r ia ± (i r ja ± j r ja)",
       thin_tac "i r j  mx", thin_tac "i r ja  mx",
       thin_tac "R p y  carrier R", thin_tac "R p x  carrier R",
       thin_tac "ideal R (R p y)", thin_tac "ideal R (R p x)")
 apply (simp add:Rxa_def, (erule bexE)+, simp)
 apply (simp add:ring_tOp_assoc)
 apply (simp add:ring_tOp_assoc[THEN sym])
 apply (frule_tac x = x and y = ra in ring_tOp_commute, assumption+, simp)
 apply (simp add:ring_tOp_assoc,
        frule_tac x = x and y = y in ring_tOp_closed, assumption+)
 apply (frule_tac x1 = r and y1 = ra and z1 = "x r y" in
        ring_tOp_assoc[THEN sym], assumption+, simp)
 apply (frule_tac x = r and y = ra in ring_tOp_closed, assumption+,
        rule ideal_ring_multiple[of mx], assumption+)
done

lemma (in Ring) chains_un:"c  chains {I. ideal R I  I  carrier R}; c  {}
        ideal R (c)"
apply (rule ideal_condition1)
apply (rule Union_least[of "c" "carrier R"])
 apply (simp add:chains_def,
       erule conjE,
       frule_tac c = X in subsetD[of "c" "{I. ideal R I  I  carrier R}"],
       assumption+, simp add:psubset_imp_subset)
 apply (simp add:chains_def,
       erule conjE)
 apply (frule nonempty_ex[of "c"], erule exE)
 apply (frule_tac c = x in subsetD[of "c" "{I. ideal R I  I  carrier R}"],
        assumption+, simp, erule conjE)
 apply (frule_tac I = x in ideal_zero, blast)

apply (rule ballI)+
 apply simp
 apply (erule bexE)+
apply (simp add: chains_def chain_subset_def)
 apply (frule conjunct1) apply (frule conjunct2)
 apply (thin_tac "c  {I. ideal R I  I  carrier R}  (xc. yc. x  y  y  x)")
 apply (frule_tac x = X in bspec, assumption,
        thin_tac "xc. yc. x  y  y  x",
        frule_tac x = Xa in bspec, assumption,
        thin_tac "yc. X  y  y  X")
 apply (frule_tac c = Xa in subsetD[of "c" "{I. ideal R I  I  carrier R}"],
          assumption+,
        frule_tac c = X in subsetD[of "c" "{I. ideal R I  I  carrier R}"],
          assumption+, simp)
 apply (erule conjE)+
 apply (erule disjE,
        frule_tac c = x and A = X and B = Xa in subsetD, assumption+,
        frule_tac x = x and y = y and I = Xa in ideal_pOp_closed, assumption+,
        blast)
 apply (frule_tac c = y and A = Xa and B = X in subsetD, assumption+,
        frule_tac x = x and y = y and I = X in ideal_pOp_closed, assumption+,
        blast)

apply (rule ballI)+
 apply (simp, erule bexE)
 apply (simp add:chains_def, erule conjE)
 apply (frule_tac c = X in subsetD[of "c" "{I. ideal R I  I  carrier R}"],
        assumption+, simp, erule conjE)
 apply (frule_tac I = X and x = x and r = r in ideal_ring_multiple,
        assumption+, blast)
done

lemma (in Ring) zeroring_no_maximal:"zeroring R  ¬ (I. maximal_ideal R I)"
apply (rule contrapos_pp, simp+, erule exE,
       frule_tac mx = x in maximal_ideal_ideal)
apply (frule_tac I = x in ideal_zero)
apply (simp add:zeroring_def, erule conjE,
       cut_tac ring_one, simp, thin_tac "carrier R = {𝟬}",
        frule sym, thin_tac "1r = 𝟬", simp, thin_tac "𝟬 = 1r")
apply (simp add:maximal_ideal_def)
done

lemma (in Ring) id_maximal_Exist:"¬(zeroring R)  I. maximal_ideal R I"
 apply (cut_tac A="{ I. ideal R I  I  carrier R }" in Zorn_Lemma2)
 apply (rule ballI)

 apply (case_tac "C={}", simp)
   apply (cut_tac zero_ideal)
   apply (simp add:zeroring_def)
    apply (cut_tac Ring, simp,
           frule not_sym, thin_tac "carrier R  {𝟬}")
   apply (cut_tac ring_zero,
         frule singleton_sub[of "𝟬" "carrier R"],
         thin_tac "𝟬  carrier R")
   apply (subst psubset_eq)
   apply blast
 apply (subgoal_tac "C  {I. ideal R I  I  carrier R}")
 apply (subgoal_tac "xC. x  (C)", blast)
  apply (rule ballI, rule Union_upper, assumption)
  apply (simp add:chains_un)
  apply (cut_tac A = C in Union_least[of _ "carrier R"])
  apply (simp add:chains_def, erule conjE,
        frule_tac c = X and A = C in
          subsetD[of _ "{I. ideal R I  I  carrier R}"], assumption+,
          simp add:ideal_subset1, simp add:psubset_eq)
  apply (rule contrapos_pp, simp+,
         cut_tac ring_one, frule sym, thin_tac "C = carrier R")
  apply (frule_tac B = "C" in eq_set_inc[of "1r" "carrier R"], assumption,
         thin_tac "carrier R = C")
  apply (simp, erule bexE)
  apply (simp add:chains_def, erule conjE)
  apply (frule_tac c = X and A = C in
         subsetD[of _ "{I. ideal R I  I  carrier R  I  carrier R}"],
         assumption+, simp, (erule conjE)+)
  apply (frule_tac I = X in ideal_inc_one, assumption+, simp)

 apply (erule bexE, simp, erule conjE)
 apply (subgoal_tac "maximal_ideal R M", blast)
 apply (simp add:maximal_ideal_def)

apply (rule conjI, rule contrapos_pp, simp+,
       frule_tac  I = M in ideal_inc_one, assumption+, simp)

apply (rule equalityI)
 apply (rule subsetI, simp)
 apply (erule conjE)
 apply (frule_tac x = x in spec,
        thin_tac "x. ideal R x  x  carrier R  M  x  x = M", simp)
 apply (frule_tac I = x in ideal_subset1, simp add:psubset_eq)
 apply (case_tac "x = carrier R", simp)
 apply simp

 apply (rule subsetI, simp)
 apply (erule disjE)
 apply simp
 apply (simp add:whole_ideal)
done

definition
  ideal_Int :: "[_, 'a set set]  'a set" where
  "ideal_Int R S ==  S"

lemma (in Ring) ideal_Int_ideal:"S  {I. ideal R I}; S{} 
                                                 ideal R ( S)"
apply (rule ideal_condition1)
 apply (frule nonempty_ex[of "S"], erule exE)
 apply (frule_tac c = x in subsetD[of "S" "{I. ideal R I}"], assumption+)
 apply (simp, frule_tac I = x in ideal_subset1)
 apply (frule_tac B = x and A = S in Inter_lower)
 apply (rule_tac A = "S" and B = x and C = "carrier R" in subset_trans,
         assumption+)

 apply (cut_tac ideal_zero_forall, blast)
 apply (simp, rule ballI)

apply (rule ballI)+
 apply simp
 apply (frule_tac x = X in bspec, assumption,
        thin_tac "XS. x  X",
        frule_tac x = X in bspec, assumption,
        thin_tac "XS. y  X")
apply (frule_tac c = X in subsetD[of "S" "{I. ideal R I}"], assumption+,
       simp, rule_tac x = x and y = y in ideal_pOp_closed, assumption+)

apply (rule ballI)+
 apply (simp, rule ballI)
 apply (frule_tac x = X in bspec, assumption,
        thin_tac "XS. x  X",
        frule_tac c = X in subsetD[of "S" "{I. ideal R I}"], assumption+,
        simp add:ideal_ring_multiple)
done

lemma (in Ring) sum_prideals_Int:"l  n. f l  carrier R;
                S = {I. ideal R I  f ` {i. i  n}  I} 
                                  (sum_pr_ideals R f n) =  S"
apply (rule equalityI)
apply (subgoal_tac "XS. sum_pr_ideals R f n  X")
apply blast
 apply (rule ballI)
 apply (simp, erule conjE)
 apply (rule_tac I = X and n = n and f = f in sum_of_prideals4, assumption+)
apply (subgoal_tac "(sum_pr_ideals R f n)  S")
 apply blast
 apply (simp add:CollectI)
 apply (simp add: sum_of_prideals2)
 apply (simp add: sum_of_prideals)
done

text‹This proves that (sum_pr_ideals R f n)› is the smallest ideal containing
 f ` (Nset n)›

primrec ideal_n_prod::"[('a, 'm) Ring_scheme, nat,  nat  'a set]  'a set"
where
  ideal_n_prod0: "ideal_n_prod R 0 J = J 0"
| ideal_n_prodSn: "ideal_n_prod R (Suc n) J =
                          (ideal_n_prod R n J) rR(J (Suc n))"

abbreviation
  IDNPROD  ("(3iΠ⇘_,_ _)" [98,98,99]98) where
  "iΠ⇘R,nJ == ideal_n_prod R n J"

primrec
  ideal_pow :: "['a set, ('a, 'more) Ring_scheme, nat]  'a set"
               ("(3_/ ⇗♢_ _)" [120,120,121]120)
where
  ip0:  "I ⇗♢R 0= carrier R"
| ipSuc:  "I ⇗♢R (Suc n)= I rR(I ⇗♢R n)"

lemma (in Ring) prod_mem_prod_ideals:"ideal R I; ideal R J; i  I; j  J 
                            i r j  (I r J)"
apply (simp add:ideal_prod_def)
apply (rule allI, rule impI, erule conjE, rename_tac X)
 apply (rule_tac A = "{x. iI. jJ. x = Ring.tp R i j}" and B = X and c = "i r j" in  subsetD, assumption)
 apply simp apply blast
done

lemma (in Ring) ideal_prod_ideal:"ideal R I; ideal R J  
                                        ideal R (I r J)"
apply (rule ideal_condition1)
 apply (simp add:ideal_prod_def)
 apply (rule subsetI, simp)
 apply (cut_tac whole_ideal)
 apply (frule_tac x = "carrier R" in spec,
        thin_tac "xa. ideal R xa  {x. iI. jJ. x = i r j}  xa 
                                                                   x  xa")
 apply (subgoal_tac "{x. iI. jJ. x = i r j}  carrier R", simp)
     apply (thin_tac "ideal R (carrier R) 
            {x. iI. jJ. x = i r j}  carrier R  x  carrier R")
 apply (rule subsetI, simp, (erule bexE)+, simp)
 apply (frule_tac h = i in ideal_subset[of "I"], assumption+,
        frule_tac h = j in ideal_subset[of "J"], assumption+)
 apply (rule_tac x = i and y = j in ring_tOp_closed, assumption+)

 apply (frule ideal_zero[of "I"],
        frule ideal_zero[of "J"],
        subgoal_tac "𝟬  I rRJ", blast)
 apply (simp add:ideal_prod_def)
 apply (rule allI, rule impI, erule conjE)
 apply (rule ideal_zero, assumption)

 apply (rule ballI)+
 apply (simp add:ideal_prod_def)
 apply (rule allI, rule impI)
 apply (frule_tac x = xa in spec,
        thin_tac "xa. ideal R xa  {x. iI. jJ. x = i r j}  xa
                                             x  xa",
        frule_tac x = xa in spec,
        thin_tac "x. ideal R x  {x. iI. jJ. x = i r j}  x  y  x",
        erule conjE, simp,
        rule_tac x = x and y = y in ideal_pOp_closed, assumption+)
 apply (rule ballI)+
        apply (simp add:ideal_prod_def)
        apply (rule allI, rule impI, erule conjE)
        apply (frule_tac x = xa in spec,
               thin_tac "xa. ideal R xa  {x. iI. jJ. x = i r j}
                             xa  x  xa", simp)
 apply (simp add:ideal_ring_multiple)
done

lemma (in Ring) ideal_prod_commute:"ideal R I; ideal R J 
                                              I r J = J r I"
apply (simp add:ideal_prod_def)
apply (subgoal_tac "{K. ideal R K  {x. iI. jJ. x = i r j}
        K}  = {K. ideal R K  {x. iJ. jI. x = i r j}  K}")
apply simp
apply (rule equalityI)
apply (rule subsetI, rename_tac X, simp, erule conjE)
 apply (rule subsetI, simp)
 apply ((erule bexE)+)
 apply (subgoal_tac "x  {x. iI. jJ. x = i r j}",
        rule_tac c = x and A = "{x. iI. jJ. x = i r j}" and B = X in
        subsetD, assumption+,
        frule_tac h = i in ideal_subset[of "J"], assumption,
        frule_tac h = j in ideal_subset[of "I"], assumption,
        frule_tac x = i and y = j in ring_tOp_commute, assumption+, simp,
        blast)
 apply (rule subsetI, simp, erule conjE,
        rule subsetI, simp,
        (erule bexE)+,
        subgoal_tac "xa  {x. iJ. jI. x = i r j}",
        rule_tac c = xa and A = "{x. iJ. jI. x = i r j}" and B = x in
                 subsetD, assumption+,
        frule_tac h = i in ideal_subset[of "I"], assumption,
        frule_tac h = j in ideal_subset[of "J"], assumption,
        frule_tac x = i and y = j in ring_tOp_commute, assumption+, simp,
        blast)
done

lemma (in Ring) ideal_prod_subTr:"ideal R I; ideal R J; ideal R C;
                        iI. jJ. i r j  C  I r J  C"
apply (simp add:ideal_prod_def)
 apply (rule_tac B = C and
        A = "{L. ideal R L  {x. iI. jJ. x = i r j}  L}" in
        Inter_lower)
 apply simp
 apply (rule subsetI, simp, (erule bexE)+, simp)
done

lemma (in Ring) n_prod_idealTr:
     "(k  n. ideal R (J k))  ideal R (ideal_n_prod R n J)"
apply (induct_tac n)
apply (rule impI)
apply simp

apply (rule impI)
apply (simp only:ideal_n_prodSn)
 apply (cut_tac n = n in Nsetn_sub_mem1, simp)
 apply (rule ideal_prod_ideal, assumption)
 apply simp
done

lemma (in Ring) n_prod_ideal:"k  n. ideal R (J k)
                                 ideal R (ideal_n_prod R n J)"
apply (simp add:n_prod_idealTr)
done

lemma (in Ring) ideal_prod_la1:"ideal R I; ideal R J  (I r J)  I"
 apply (simp add:ideal_prod_def)
 apply (rule subsetI)
 apply (simp add:CollectI)
 apply (subgoal_tac "{x. iI. jJ. x =  i r j}  I")
 apply blast
apply (thin_tac "xa. ideal R xa  {x. iI. jJ. x =  i r j}  xa
                                                               x  xa")
 apply (rule subsetI, simp add:CollectI,
        (erule bexE)+, frule_tac h = j in ideal_subset[of "J"], assumption+)
 apply (simp add:ideal_ring_multiple1)
done

lemma (in Ring) ideal_prod_el1:"ideal R I; ideal R J; a  (I r J) 
                           a  I"
apply (frule ideal_prod_la1 [of "I" "J"], assumption+)
apply (rule subsetD, assumption+)
done

lemma (in Ring) ideal_prod_la2:"ideal R I; ideal R J   (I r J)  J"
 apply (subst ideal_prod_commute, assumption+,
        rule ideal_prod_la1[of "J" "I"], assumption+)
done

lemma (in Ring) ideal_prod_sub_Int:"ideal R I; ideal R J  
                     (I r J)  I  J"
by (simp add:ideal_prod_la1 ideal_prod_la2)

lemma (in Ring) ideal_prod_el2:"ideal R I; ideal R J; a  (I r J) 
                                 a  J"
by (frule ideal_prod_la2 [of "I" "J"], assumption+,
       rule subsetD, assumption+)

textR,n J› is the product of ideals›
lemma (in Ring) ele_n_prodTr0:"k  (Suc n). ideal R (J k);
             a  iΠ⇘R,(Suc n)J   a  (iΠ⇘R,nJ)  a  (J (Suc n))"
apply (simp add:Nset_Suc[of n])
 apply (cut_tac n_prod_ideal[of n J])
apply (rule conjI)
 apply (rule ideal_prod_el1 [of "iΠ⇘R,nJ" "J (Suc n)"], assumption, simp+)
 apply (rule ideal_prod_el2[of "iΠ⇘R,nJ" "J (Suc n)"], assumption+, simp+)
done

lemma (in Ring) ele_n_prodTr1:
      "(k  n. ideal R (J k))  a  ideal_n_prod R n J 
                                             (k  n. a  (J k))"
apply (induct_tac n)
(** n = 0 **)
 apply simp
(** n **)
 apply (rule impI)
 apply (rule allI, rule impI)
 apply (cut_tac n = n in Nsetn_sub_mem1, simp)
 apply (erule conjE)
 apply (frule_tac n = n in ele_n_prodTr0[of _ J a])
 apply simp

 apply (erule conjE,
        thin_tac "kSuc n. ideal R (J k)")
 apply simp
 apply (case_tac "k = Suc n", simp)
 apply (frule_tac m = k and n = "Suc n" in noteq_le_less, assumption+,
        thin_tac "k  Suc n")
 apply (frule_tac x = k and n = "Suc n" in less_le_diff, simp)
done

lemma (in Ring) ele_n_prod:"k  n. ideal R (J k);
                       a  ideal_n_prod R n J    k  n. a  (J k)"
by (simp add: ele_n_prodTr1 [of "n" "J" "a"])

lemma (in Ring) idealprod_whole_l:"ideal R I  (carrier R) rRI = I"
apply (rule equalityI)
 apply (rule subsetI)
 apply (simp add:ideal_prod_def)
 apply (subgoal_tac "{x. icarrier R. jI. x = i r j}  I")
 apply blast
 apply (thin_tac "xa. ideal R xa  {x. icarrier R. jI.
                       x = i r j}  xa  x  xa")
 apply (rule subsetI)
 apply simp
 apply ((erule bexE)+, simp)
 apply (thin_tac "xa = i r j", simp add:ideal_ring_multiple)
apply (rule subsetI)
 apply (simp add:ideal_prod_def)
 apply (rule allI, rule impI) apply (erule conjE)
 apply (rename_tac xa X)
 apply (cut_tac ring_one)
 apply (frule_tac h = xa in ideal_subset[of "I"], assumption,
        frule_tac x = xa in ring_l_one)
 apply (subgoal_tac "1r r xa  {x. icarrier R. jI. x = i r j}")
 apply (rule_tac c = xa and A = "{x. icarrier R. jI. x = i r j}" and
         B = X in subsetD, assumption+)
 apply simp
 apply simp
 apply (frule sym, thin_tac "1r r xa = xa", blast)
done

lemma (in Ring) idealprod_whole_r:"ideal R I  I r (carrier R) = I"
by (cut_tac whole_ideal,
       simp add:ideal_prod_commute[of "I" "carrier R"],
       simp add:idealprod_whole_l)

lemma (in Ring) idealpow_1_self:"ideal R I  I ⇗♢R (Suc 0)= I"
apply simp
apply (simp add:idealprod_whole_r)
done

lemma (in Ring) ideal_pow_ideal:"ideal R I  ideal R (I ⇗♢R n)"
apply (induct_tac n)
apply (simp add:whole_ideal)
apply simp
apply (simp add:ideal_prod_ideal)
done

lemma (in Ring) ideal_prod_prime:"ideal R I; ideal R J; prime_ideal R P;
                          I r J  P   I  P  J  P"
apply (rule contrapos_pp, simp+)
apply (erule conjE, simp add:subset_eq, (erule bexE)+)
apply (frule_tac i = x and j = xa in prod_mem_prod_ideals[of "I" "J"],
          assumption+)
 apply (frule_tac x = "x r xa" in bspec, assumption,
        thin_tac "xI rRJ. x  P")
 apply (simp add: prime_ideal_def, (erule conjE)+)
 apply (frule_tac h = x in ideal_subset, assumption,
        frule_tac x = x in bspec, assumption,
        thin_tac "xcarrier R. ycarrier R. x r y  P  x  P  y  P",
        frule_tac h = xa in ideal_subset, assumption,
        frule_tac x = xa in bspec, assumption,
        thin_tac "ycarrier R. x r y  P  x  P  y  P",
        simp)
done

lemma (in Ring) ideal_n_prod_primeTr:"prime_ideal R P 
       (k  n. ideal R (J k))  (ideal_n_prod R n J  P) 
                                               (i  n. (J i)  P)"
apply (induct_tac n)
apply simp

apply (rule impI)
 apply (rule impI, simp)
 apply (cut_tac I = "iΠ⇘R,nJ" and J = "J (Suc n)" in
                      ideal_prod_prime[of _ _ "P"],
        rule_tac n = n and J = J in n_prod_ideal,
         rule allI, simp+)
 apply (erule disjE, simp)
 apply (cut_tac n = n in Nsetn_sub_mem1,
        blast)
 apply blast
done

lemma (in Ring) ideal_n_prod_prime:"prime_ideal R P;
            k  n. ideal R (J k); ideal_n_prod R n J  P 
                                            i  n. (J i)  P"
apply (simp add:ideal_n_prod_primeTr)
done

definition
  ppa::"[_, nat  'a set, 'a set, nat]  (nat  'a)" where
  "ppa R P A i l = (SOME x. x  A  x  (P (skip i l))  x  P i)"
     (** Note (ppa R P A) is used to prove prime_ideal_cont1,
         some element x of A such that x ∈ P j for (i ≠ j) and x ∉ P i **)

lemma (in Ring) prod_primeTr:"prime_ideal R P; ideal R A; ¬ A  P;
                ideal R B; ¬ B  P   x. x  A  x  B  x  P"
apply (simp add:subset_eq)
 apply (erule bexE)+
apply (subgoal_tac "x r xa  A  x r xa  B  x r xa  P")
 apply blast
 apply (rule conjI)
 apply (rule ideal_ring_multiple1, assumption+)
  apply (simp add:ideal_subset)
 apply (rule conjI)
  apply (rule ideal_ring_multiple, assumption+)
  apply (simp add:ideal_subset)

 apply (rule contrapos_pp, simp+)
apply (simp add:prime_ideal_def, (erule conjE)+)
 apply (frule_tac h = x in ideal_subset[of "A"], assumption+,
        frule_tac h = xa in ideal_subset[of "B"], assumption+,
        frule_tac x = x in bspec, assumption,
        thin_tac "xcarrier R. ycarrier R. x r y  P  x  P  y  P",
        frule_tac x = xa in bspec, assumption,
        thin_tac "ycarrier R. x r y  P  x  P  y  P")
  apply simp
done

lemma (in Ring) prod_primeTr1:"k  (Suc n). prime_ideal R (P k);
       ideal R A; l  (Suc n). ¬ (A  P l);
       k  (Suc n). l  (Suc n). k = l  ¬ (P k)  (P l); i  (Suc n) 
       l  n. ppa R P A i l  A 
                  ppa R P A i l  (P (skip i l))  ppa R P A i l  (P i)"
apply (rule allI, rule impI)
apply (cut_tac i = i and l = l in skip_il_neq_i)
apply (rotate_tac 2)
      apply (frule_tac x = i in spec,
             thin_tac "l  (Suc n). ¬ A  P l", simp)

      apply (cut_tac l = l in skip_mem[of _ "n" "i"], simp,
             frule_tac x = "skip i l" in spec,
             thin_tac "k  (Suc n). l  (Suc n). k = l  ¬ P k  P l",
             simp)
     apply (rotate_tac -1,
            frule_tac x = i in spec,
            thin_tac "la  (Suc n). skip i l = la  ¬ P (skip i l)  P la",
            simp)
apply (cut_tac P = "P i" and A = A and B = "P (skip i l)" in prod_primeTr,
       simp, assumption+)
 apply (frule_tac x = "skip i l" in spec,
        thin_tac "kSuc n. prime_ideal R (P k)", simp,
        rule prime_ideal_ideal, assumption+)
 apply (simp add:ppa_def)
 apply (rule someI2_ex, assumption+)
done

lemma (in Ring) ppa_mem:"k  (Suc n). prime_ideal R (P k); ideal R A;
      l  (Suc n). ¬ (A  P l);
      k  (Suc n). l  (Suc n). k = l  ¬ (P k)  (P l);
      i   (Suc n); l  n  ppa R P A i l  carrier R"
apply (frule_tac prod_primeTr1[of n P A], assumption+)
 apply (rotate_tac -1, frule_tac x = l in spec,
        thin_tac "ln. ppa R P A i l  A 
           ppa R P A i l  P (skip i l)  ppa R P A i l  P i", simp)
 apply (simp add:ideal_subset)
done

lemma (in Ring) nsum_memrTr:"(i  n. f i  carrier R) 
                             (l  n. nsum R f l  carrier R)"
apply (cut_tac ring_is_ag)
apply (induct_tac n)
(** n = 0 **)
 apply (rule impI, rule allI, rule impI)
 apply simp
(** n **)
apply (rule impI)
 apply (rule allI, rule impI)

 apply (rule aGroup.nsum_mem, assumption)
 apply (rule allI, simp)
done

lemma (in Ring) nsum_memr:"i  n. f i  carrier R 
                          l  n. nsum R f l  carrier R"
by (simp add:nsum_memrTr)

lemma (in Ring) nsum_ideal_incTr:"ideal R A 
               (i  n. f i  A)   nsum R f n  A"
 apply (induct_tac n)
 apply (rule impI)
  apply simp
(** n **)
apply (rule impI)
apply simp
apply (rule ideal_pOp_closed, assumption+)
 apply simp
done

lemma (in Ring) nsum_ideal_inc:"ideal R A; i  n. f i  A 
                    nsum R f n  A"
by (simp add:nsum_ideal_incTr)

lemma (in Ring) nsum_ideal_excTr:"ideal R A 
      (i  n. f i  carrier R)  (j  n. (l  {i. i  n} -{j}. f l  A)
        (f j  A))  nsum R f n  A"
apply (induct_tac n)
(** n = 0 **)
 apply simp
(** n **)
 apply (rule impI)
 apply (erule conjE)+
apply (erule exE)
apply (case_tac "j = Suc n", simp) apply (
       thin_tac "(jn. f j  A)  Σe R f n  A")
 apply (erule conjE)
 apply (cut_tac n = n and f = f in nsum_ideal_inc[of A], assumption,
        rule allI, simp)
 apply (rule contrapos_pp, simp+)
 apply (frule_tac a = "Σe R f n" and b = "f (Suc n)" in
                   ideal_ele_sumTr1[of A],
        simp add:ideal_subset, simp, assumption+, simp)

apply (erule conjE,
       frule_tac m = j and n = "Suc n" in noteq_le_less, assumption,
       frule_tac x = j and n = "Suc n" in less_le_diff,
       thin_tac "j  Suc n", thin_tac "j < Suc n", simp,
       cut_tac n = n in Nsetn_sub_mem1, simp)
apply (erule conjE,
       frule_tac x = "Suc n" in bspec, simp)
apply (rule contrapos_pp, simp+)
 apply (frule_tac a = "Σe R f n" and b = "f (Suc n)" in
                   ideal_ele_sumTr2[of A])
 apply (cut_tac ring_is_ag,
        rule_tac n = n in aGroup.nsum_mem[of R _ f], assumption+,
        rule allI, simp, simp, assumption+, simp)
 apply (subgoal_tac "jn. (l{i. i  n} - {j}. f l  A)  f j  A",
        simp,
        thin_tac "(jn. (l{i. i  n} - {j}. f l  A)  f j  A)
                      Σe R f n  A")
 apply (subgoal_tac "l{i. i  n} - {j}. f l  A", blast,
        thin_tac "Σe R f n ± f (Suc n)  A",
        thin_tac "Σe R f n  A")
 apply (rule ballI)
 apply (frule_tac x = l in bspec, simp, assumption)
done

lemma (in Ring) nsum_ideal_exc:"ideal R A; i  n. f i  carrier R;
      j  n. (l{i. i  n} -{j}. f l  A)  (f j  A)   nsum R f n  A"
by (simp add:nsum_ideal_excTr)

lemma (in Ring) nprod_memTr:"(i  n. f i  carrier R) 
                             (l. l  n   nprod R f l  carrier R)"
apply (induct_tac n)
apply (rule impI, rule allI, rule impI, simp)

apply (rule impI, rule allI, rule impI)
apply (case_tac "l  n")
 apply (cut_tac n = n in Nset_Suc, blast)
 apply (cut_tac m = l and n = "Suc n" in Nat.le_antisym, assumption)
 apply (simp add: not_less)
 apply simp
 apply (rule ring_tOp_closed, simp)
 apply (cut_tac n = n in Nset_Suc, blast)
done

lemma (in Ring) nprod_mem:"i  n. f i  carrier R; l  n 
                              nprod R f l  carrier R"
by (simp add:nprod_memTr)

lemma (in Ring) ideal_nprod_incTr:"ideal R A 
                (i  n. f i  carrier R) 
                             (l  n. f l  A)  nprod R f n  A"
apply (induct_tac n)
(** n = 0 **)
apply simp
(** n **)
apply (rule impI)
 apply (erule conjE)+
apply simp
 apply (erule exE)
 apply (case_tac "l = Suc n", simp)
 apply (rule_tac x = "f (Suc n)" and r = "nprod R f n" in
                 ideal_ring_multiple[of "A"], assumption+)
 apply (rule_tac n = "Suc n" and f = f and l = n in nprod_mem,
                 assumption+, simp)
 apply (erule conjE)
 apply (frule_tac m = l and n = "Suc n" in noteq_le_less, assumption,
       frule_tac x = l and n = "Suc n" in less_le_diff,
       thin_tac "l  Suc n", thin_tac "l < Suc n", simp)
apply (rule_tac x = "nprod R f n" and r = "f (Suc n)" in
                      ideal_ring_multiple1[of "A"], assumption+)
 apply blast
 apply simp
done

lemma (in Ring) ideal_nprod_inc:"ideal R A; i  n. f i  carrier R;
                l  n. f l  A  nprod R f n  A"
by (simp add:ideal_nprod_incTr)

lemma (in Ring) nprod_excTr:"prime_ideal R P 
          (i  n. f i  carrier R)  (l  n. f l  P) 
                                                     nprod R f n  P"
apply (induct_tac n)
(** n = 0 **)
 apply simp  (* n = 0 done *)
(** n **)
apply (rule impI)
apply (erule conjE)+
 apply simp
  apply (rule_tac y = "f (Suc n)" and x = "nprod R f n" in
          prime_elems_mult_not[of "P"], assumption,
         rule_tac n = n in  nprod_mem, rule allI, simp+)
done

lemma (in Ring) prime_nprod_exc:"prime_ideal R P; i  n. f i  carrier R;
                l  n. f l  P  nprod R f n  P"
by (simp add:nprod_excTr)

definition
  nilrad :: "_  'a set" where
  "nilrad R = {x. x  carrier R  nilpotent R x}"

lemma (in Ring) id_nilrad_ideal:"ideal R (nilrad R)"
apply (cut_tac ring_is_ag)
apply (rule ideal_condition1[of "nilrad R"])
 apply (rule subsetI) apply (simp add:nilrad_def CollectI)
 apply (simp add:nilrad_def)
 apply (cut_tac ring_zero)
 apply (subgoal_tac "nilpotent R 𝟬")
 apply blast
 apply (simp add:nilpotent_def)
 apply (frule np_1[of "𝟬"], blast)

 apply (rule ballI)+
apply (simp add:nilrad_def nilpotent_def, (erule conjE)+)
 apply (erule exE)+
 apply (simp add:aGroup.ag_pOp_closed[of "R"])
 apply (frule_tac x = x and y = y and m = n and n = na in npAdd,
        assumption+, blast)

 apply (rule ballI)+
 apply (simp add:nilrad_def nilpotent_def, erule conjE, erule exE)
 apply (simp add:ring_tOp_closed,
        frule_tac x = r and y = x and n = n in npMul, assumption+,
           simp,
        frule_tac x = r and n = n in npClose)
        apply (simp add:ring_times_x_0, blast)
done

definition
  rad_ideal :: "[_, 'a set ]  'a set" where
  "rad_ideal R I = {a. a  carrier R  nilpotent (qring R I) ((pj R I) a)}"

lemma (in Ring) id_rad_invim:"ideal R I 
       rad_ideal R I = (rInvim R (qring R I) (pj R I ) (nilrad (qring R I)))"
apply (cut_tac ring_is_ag)
apply (rule equalityI)
 apply (rule subsetI)
 apply (simp add:rad_ideal_def)
 apply (erule conjE)+
 apply (simp add:rInvim_def)
 apply (simp add:nilrad_def)
 apply (subst pj_mem, rule Ring_axioms)
 apply assumption+
 apply (simp add:qring_def ar_coset_def set_rcs_def)
 apply (simp add:aGroup.ag_carrier_carrier)
 apply blast

apply (rule subsetI)
 apply (simp add:rInvim_def nilrad_def)
apply (simp add: rad_ideal_def)
done

lemma (in Ring) id_rad_ideal:"ideal R I  ideal R (rad_ideal R I)"
(* thm invim_of_ideal *)
apply (subst id_rad_invim [of "I"], assumption)
apply (rule invim_of_ideal, rule Ring_axioms, assumption)
apply (rule Ring.id_nilrad_ideal)
apply (simp add:qring_ring)
done

lemma (in Ring) id_rad_cont_I:"ideal R I  I  (rad_ideal R I)"
apply (simp add:rad_ideal_def)
apply (rule subsetI, simp,
       simp add:ideal_subset)
apply (simp add:nilpotent_def)
apply (subst pj_mem, rule Ring_axioms, assumption+,
       simp add:ideal_subset) (* thm npQring *)

 apply (frule_tac h = x in ideal_subset[of "I"], assumption,
        frule_tac a = x in npQring[OF Ring, of "I" _ "Suc 0"], assumption,
        simp only:np_1, simp only:Qring_fix1,
        subst qring_zero[of "I"], assumption)
 apply blast
done

lemma (in Ring) id_rad_set:"ideal R I 
       rad_ideal R I = {x. x  carrier R  (n. npow R x n  I)}"
apply (simp add:rad_ideal_def)
apply (rule equalityI)
 apply (rule subsetI)
 apply (simp add:nilpotent_def, erule conjE, erule exE)
 apply (simp add: pj_mem[OF Ring], simp add:npQring[OF Ring])
apply ( simp add:qring_zero)
 apply (frule_tac x = x and n = n in npClose)
 apply (frule_tac a = "x^⇗R n⇖" in ar_coset_same3[of "I"], assumption+,
        blast)
apply (rule subsetI, simp, erule conjE, erule exE)
 apply (simp add:nilpotent_def)
 apply (simp add: pj_mem[OF Ring], simp add:npQring[OF Ring],
                                            simp add:qring_zero)
 apply (frule_tac a = "x^⇗R n⇖" in ar_coset_same4[of "I"], assumption+)
 apply blast
done

lemma (in Ring) rad_primary_prime:"primary_ideal R q 
                                    prime_ideal R (rad_ideal R q)"
apply (simp add:prime_ideal_def)
apply (frule primary_ideal_ideal[of "q"])
apply (simp add:id_rad_ideal)
apply (rule conjI)
 apply (rule contrapos_pp, simp+)
 apply (simp add:id_rad_set, erule conjE, erule exE)
 apply (simp add:npOne)
 apply (simp add:primary_ideal_proper1[of "q"])

apply ((rule ballI)+, rule impI)
 apply (rule contrapos_pp, simp+, erule conjE)
 apply (simp add:id_rad_set, erule conjE, erule exE)
 apply (simp add:npMul)
 apply (simp add:primary_ideal_def, (erule conjE)+)
 apply (frule_tac x = x and n = n in npClose,
        frule_tac x = y and n = n in npClose)
 apply (frule_tac x = "x^⇗R n⇖" in bspec, assumption,
        thin_tac "xcarrier R. ycarrier R. x r y  q 
                                    (n. x^⇗R n q)  y  q",
        frule_tac x = "y^⇗R n⇖" in bspec, assumption,
        thin_tac "ycarrier R. x^⇗R nr y  q 
                             (na. x^⇗R n⇖^⇗R na q)  y  q", simp)
 apply (simp add:npMulExp)
done

lemma (in Ring) npow_notin_prime:"prime_ideal R P; x  carrier R; x  P
                                 n. npow R x n  P"
apply (rule allI)
apply (induct_tac n)
 apply simp
 apply (simp add:prime_ideal_proper1)

 apply simp
 apply (frule_tac x = x and n = na in npClose)
 apply (simp add:prime_elems_mult_not)
done

lemma (in Ring) npow_in_prime:"prime_ideal R P; x  carrier R;
                               n. npow R x n  P   x  P"
apply (rule contrapos_pp, simp+)
apply (frule npow_notin_prime, assumption+)
apply blast
done

definition
  mul_closed_set::"[_, 'a set ]  bool" where
  "mul_closed_set R S  S  carrier R  (sS. tS. s rRt  S)"

locale Idomain = Ring +
       assumes idom:
       "a  carrier R; b  carrier R; a r b = 𝟬  a = 𝟬  b = 𝟬"
  (* integral domain *)

locale Corps =
       fixes K (structure)
       assumes f_is_ring: "Ring K"
       and f_inv: "xcarrier K - {𝟬}. x'  carrier K. x' r x = 1r"
  (** integral domain **)

lemma (in Ring) mul_closed_set_sub:"mul_closed_set R S  S  carrier R"
by (simp add:mul_closed_set_def)

lemma (in Ring) mul_closed_set_tOp_closed:"mul_closed_set R S; s  S;
                            t  S  s r t  S"
by (simp add:mul_closed_set_def)

lemma (in Corps) f_inv_unique:" x  carrier K - {𝟬}; x'  carrier K;
      x''  carrier K; x' r  x = 1r; x'' r x = 1r   x' = x''"
apply (cut_tac  f_is_ring)
 apply (cut_tac x = x' and y = x and z = x'' in Ring.ring_tOp_assoc[of K],
        assumption+, simp, assumption, simp)
 apply (simp add:Ring.ring_l_one[of K],
        simp add:Ring.ring_tOp_commute[of K x x''] Ring.ring_r_one[of K])
done

definition
  invf :: "[_, 'a]  'a" where
  "invf K x = (THE y. y  carrier K  y rKx = 1rK)"

lemma (in Corps) invf_inv:"x  carrier K - {𝟬} 
                (invf K x)  carrier K  (invf K x) r x = 1r "
apply (simp add:invf_def)
apply (rule theI')
apply (rule ex_ex1I)
apply (cut_tac f_inv, blast)
apply (rule_tac x' = xa and x'' = y in f_inv_unique[of x])
       apply simp+
done



definition
  npowf :: "_   'a  int   'a" where
  "npowf K x n =
    (if 0  n then npow K x (nat n) else npow K (invf K x) (nat (- n)))"

abbreviation
  NPOWF ::  "['a, _, int]   'a"  ("(3__⇙⇗_)" [77,77,78]77) where
  "aK⇙⇗n== npowf K a n"

abbreviation
  IOP :: "['a, _]  'a" ("(_⇗‐ _)" [87,88]87) where
  "a⇗‐K== invf K a"

lemma (in Idomain) idom_is_ring: "Ring R" ..

lemma (in Idomain) idom_tOp_nonzeros:"x  carrier R;
       y  carrier R; x  𝟬;  y  𝟬  x r y  𝟬"
apply (rule contrapos_pp, simp+)
apply (cut_tac idom[of x y]) apply (erule disjE, simp+)
done

lemma (in Idomain) idom_potent_nonzero:
       "x  carrier R; x  𝟬   npow R x n  𝟬 "
apply (induct_tac n)
 apply simp  (* case 0 *)
 apply (rule contrapos_pp, simp+)
 apply (frule ring_l_one[of "x", THEN sym]) apply simp
 apply (simp add:ring_times_0_x)
 (* case (Suc n) *)

 apply (rule contrapos_pp, simp+)
 apply (frule_tac n = n in npClose[of x],
        cut_tac a = "x^⇗R n⇖" and b = x in idom, assumption+)
 apply (erule disjE, simp+)
done

lemma (in Idomain) idom_potent_unit:"a  carrier R; 0 < n
                  (Unit R a) = (Unit R (npow R a n))"
apply (rule iffI)
 apply (simp add:Unit_def, erule bexE)
 apply (simp add:npClose)
 apply (frule_tac x1 = a and y1 = b and n1 = n in npMul[THEN sym], assumption,
        simp add:npOne)
  apply (frule_tac x = b and n = n in npClose, blast)

apply (case_tac "n = Suc 0", simp only: np_1)
 apply (simp add:Unit_def, erule conjE, erule bexE)
 apply (cut_tac x = a and n = "n - Suc 0" in npow_suc[of R], simp del:npow_suc,
      thin_tac "a^⇗R n= a^⇗R (n - Suc 0)r a",
      frule_tac x = a and n = "n - Suc 0" in npClose,
      frule_tac x = "a^⇗R (n - Suc 0)⇖" and y = a in ring_tOp_commute, assumption+,
      simp add:ring_tOp_assoc,
      frule_tac x = "a^⇗R (n - Suc 0)⇖" and y = b in ring_tOp_closed, assumption+)
 apply blast
done

lemma (in Idomain) idom_mult_cancel_r:"a  carrier R;
       b  carrier R; c  carrier R; c  𝟬; a r c = b r c  a = b"
apply (cut_tac ring_is_ag)
 apply (frule ring_tOp_closed[of "a" "c"], assumption+,
        frule ring_tOp_closed[of "b" "c"], assumption+)
 apply (simp add:aGroup.ag_eq_diffzero[of "R" "a r c" "b r c"],
        simp add:ring_inv1_1,
        frule aGroup.ag_mOp_closed[of "R" "b"], assumption,
        simp add:ring_distrib2[THEN sym, of "c" "a" "-a b"])
 apply (frule aGroup.ag_pOp_closed[of "R" "a" "-a b"], assumption+)
 apply (subst aGroup.ag_eq_diffzero[of R a b], assumption+)
 apply (rule contrapos_pp, simp+)
 apply (frule idom_tOp_nonzeros[of "a ± -a b" c], assumption+, simp)
done

lemma (in Idomain) idom_mult_cancel_l:"a  carrier R;
      b  carrier R; c  carrier R; c  𝟬; c r a = c r b  a = b"
apply (simp add:ring_tOp_commute)
apply (simp add:idom_mult_cancel_r)
done

lemma (in Corps) invf_closed1:"x  carrier K - {𝟬} 
                               invf K x  (carrier K) - {𝟬}"
apply (frule  invf_inv[of x], erule conjE)
 apply (rule contrapos_pp, simp+)
 apply (cut_tac f_is_ring) apply (
        simp add:Ring.ring_times_0_x[of K])
 apply (frule sym, thin_tac "𝟬 = 1r", simp, erule conjE)
 apply (frule Ring.ring_l_one[of K x], assumption)
 apply (rotate_tac -1, frule sym, thin_tac "1r r x = x",
        simp add:Ring.ring_times_0_x)
done

lemma (in Corps) linvf:"x  carrier K - {𝟬}  (invf K x) r x = 1r"
by (simp add:invf_inv)

lemma (in Corps) field_is_ring:"Ring K"
by (simp add:f_is_ring)

lemma (in Corps) invf_one:"1r  𝟬   invf K (1r) = 1r"
apply (cut_tac field_is_ring)
 apply (frule_tac Ring.ring_one)
 apply (cut_tac invf_closed1 [of "1r"])
 apply (cut_tac linvf[of "1r"])
 apply (simp add:Ring.ring_r_one[of "K"])
 apply simp+
done

lemma (in Corps) field_tOp_assoc:"x  carrier K; y  carrier K; z  carrier K
                                 x r y r z =  x r (y r z)"
apply (cut_tac field_is_ring)
apply (simp add:Ring.ring_tOp_assoc)
done

lemma (in Corps) field_tOp_commute:"x  carrier K; y  carrier K
                                 x r y  =  y r x"
apply (cut_tac field_is_ring)
apply (simp add:Ring.ring_tOp_commute)
done

lemma (in Corps) field_inv_inv:"x  carrier K; x  𝟬  (x⇗‐K)⇗‐K= x"
apply (cut_tac invf_closed1[of "x"])
 apply (cut_tac invf_inv[of "x⇗‐K⇖"], erule conjE)
 apply (frule field_tOp_assoc[THEN sym, of "x⇗‐ K⇖⇗‐ K⇖" "x⇗‐ K⇖" "x"],
        simp, assumption, simp)
 apply (cut_tac field_is_ring,
        simp add:Ring.ring_l_one Ring.ring_r_one, erule conjE,
        cut_tac invf_inv[of x], erule conjE, simp add:Ring.ring_r_one)
 apply simp+
done

lemma (in Corps) field_is_idom:"Idomain K"
apply (rule Idomain.intro)
 apply (simp add:field_is_ring)
 apply (cut_tac field_is_ring)
 apply (rule Idomain_axioms.intro)
 apply (rule contrapos_pp, simp+, erule conjE)
 apply (cut_tac x = a in invf_closed1, simp, simp, erule conjE)
 apply (frule_tac x = "a⇗‐ K⇖" and y = a and z = b in field_tOp_assoc,
         assumption+)
 apply (simp add:linvf Ring.ring_times_x_0 Ring.ring_l_one)
done

lemma (in Corps) field_potent_nonzero:"x  carrier K; x  𝟬 
                                       x^⇗K n 𝟬"
apply (cut_tac field_is_idom)
apply (cut_tac field_is_ring,
       simp add:Idomain.idom_potent_nonzero)
done

lemma (in Corps) field_potent_nonzero1:"x  carrier K; x  𝟬  xK⇙⇗n 𝟬"
apply (simp add:npowf_def)
apply (case_tac "0  n")
apply (simp add:field_potent_nonzero)

apply simp
 apply (cut_tac invf_closed1[of "x"], simp+, (erule conjE)+)
 apply (simp add:field_potent_nonzero)
 apply simp
done

lemma (in Corps) field_nilp_zero:"x  carrier K; x^⇗K n= 𝟬  x = 𝟬"
by (rule contrapos_pp, simp+, simp add:field_potent_nonzero)

lemma (in Corps) npowf_mem:"a  carrier K; a  𝟬 
                                    npowf K a n  carrier K"
apply (simp add:npowf_def)
apply (cut_tac field_is_ring)
apply (case_tac "0  n", simp,
       simp add:Ring.npClose, simp)

apply (cut_tac invf_closed1[of "a"], simp, erule conjE,
       simp add:Ring.npClose, simp)
done

lemma (in Corps) field_npowf_exp_zero:"a  carrier K; a  𝟬 
                                    npowf K a 0 = 1r"
by (cut_tac field_is_ring, simp add:npowf_def)

lemma (in Corps) npow_exp_minusTr1:"x  carrier K; x  𝟬; 0  i  
       0  i - (int j)   xK⇙⇗(i - (int j))= x^⇗K (nat i)r (x⇗‐K)^⇗K j⇖"
apply (cut_tac field_is_ring,
       cut_tac invf_closed1[of "x"], simp,
       simp add:npowf_def, erule conjE)
apply (induct_tac "j", simp)
 apply (frule Ring.npClose[of "K" "x" "nat i"], assumption+,
        simp add:Ring.ring_r_one)
apply (rule impI, simp)
 apply (subst zdiff)
 apply (simp add:add.commute[of "1"])
 apply (cut_tac z = i and w = "int n + 1" in zdiff,
       simp only:minus_add_distrib,
       thin_tac "i - (int n + 1) = i + (- int n + - 1)")
 apply (cut_tac z = "i + - int n" in nat_diff_distrib[of "1"],
         simp, simp)
 apply (simp only:zdiff[of _ "1"], simp)

apply (cut_tac field_is_idom)
apply (frule_tac n = "nat i" in Ring.npClose[of "K" "x"], assumption+,
       frule_tac n = "nat i" in Ring.npClose[of "K" "x⇗‐ K⇖"], assumption+,
       frule_tac n = n in Ring.npClose[of "K" "x⇗‐ K⇖"], assumption+ )
apply (rule_tac a = "x^⇗K (nat (i + (- int n - 1)))⇖" and
       b = "x^⇗K (nat i)r (x⇗‐ K⇖^⇗K nr x⇗‐ K)" and c = x in
       Idomain.idom_mult_cancel_r[of "K"], assumption+)
 apply (simp add:Ring.npClose, rule Ring.ring_tOp_closed, assumption+,
        rule Ring.ring_tOp_closed, assumption+)
 apply (subgoal_tac "0 < nat (i - int n)")
 apply (subst Ring.npMulElmR, assumption+, simp,
        simp add:field_tOp_assoc[THEN sym, of "x^⇗K (nat i)⇖" _ "x⇗‐ K⇖"])
 apply (subst field_tOp_assoc[of _ _ x])
 apply (rule Ring.ring_tOp_closed[of K], assumption+)
 apply (simp add: linvf)
 apply (subst Ring.ring_r_one[of K], assumption)
 apply auto
 apply (metis Ring.npClose)
 apply (simp only: uminus_add_conv_diff [symmetric] add.assoc [symmetric])
 apply (simp add: algebra_simps nat_diff_distrib Suc_diff_Suc)
 apply (smt (verit) Ring.npMulElmR Suc_nat_eq_nat_zadd1 nat_diff_distrib' nat_int of_nat_0_le_iff)
done

lemma (in Corps) npow_exp_minusTr2:"x  carrier K; x  𝟬; 0  i; 0  j;
                 0  i - j    xK⇙⇗(i - j)= x^⇗K (nat i)r (x⇗‐K)^⇗K (nat j)⇖"
apply (frule npow_exp_minusTr1[of "x" "i" "nat j"], assumption+)
apply simp
done

lemma (in Corps) npowf_inv:"x  carrier K; x  𝟬; 0  j  xK⇙⇗j= (x⇗‐K)K⇙⇗(-j)⇖"
apply (simp add:npowf_def)
 apply (rule impI, simp add:zle)
 apply (simp add:field_inv_inv)
done

lemma (in Corps) npowf_inv1:"x  carrier K; x  𝟬; ¬ 0  j 
                                      xK⇙⇗j= (x⇗‐K)K⇙⇗(-j)⇖"
apply (simp add:npowf_def)
done

lemma (in Corps) npowf_inverse:"x  carrier K; x  𝟬  xK⇙⇗j= (x⇗‐K)K⇙⇗(-j)⇖"
apply (case_tac "0  j")
apply (simp add:npowf_inv, simp add:npowf_inv1)
done

lemma (in Corps) npowf_expTr1:"x  carrier K; x  𝟬; 0  i; 0  j;
                 0  i - j  xK⇙⇗(i - j)= xK⇙⇗ir xK⇙⇗(- j)⇖"
apply (simp add:npow_exp_minusTr2)
apply (simp add:npowf_def)
done

lemma (in Corps) npowf_expTr2:"x  carrier K; x  𝟬; 0  i + j 
                          xK⇙⇗(i + j)= xK⇙⇗ir xK⇙⇗j⇖"
apply (cut_tac field_is_ring)
 apply (case_tac "0  i")
  apply (case_tac "0  j")
  apply (simp add:npowf_def, simp add:nat_add_distrib,
         rule Ring.npMulDistr[THEN sym], assumption+)
 apply (subst zminus_minus[THEN sym, of "i" "j"],
        subst npow_exp_minusTr2[of "x" "i" "-j"], assumption+)
  apply (simp add:zle, simp add:zless_imp_zle, simp add:npowf_def)
 apply (simp add:add.commute[of "i" "j"],
        subst zminus_minus[THEN sym, of "j" "i"],
        subst npow_exp_minusTr2[of "x" "j" "-i"], assumption+)
  apply (simp add:zle, simp add:zless_imp_zle, simp)
  apply (frule npowf_mem[of "x" "i"], assumption+,
         frule npowf_mem[of "x" "j"], assumption+,
         simp add:field_tOp_commute[of "xK⇙⇗i⇖" "xK⇙⇗j⇖"])
  apply (simp add:npowf_def)
done

lemma (in Corps) npowf_exp_add:"x  carrier K; x  𝟬 
                          xK⇙⇗(i + j)= xK⇙⇗ir xK⇙⇗j⇖"
apply (case_tac "0  i + j")
apply (simp add:npowf_expTr2)
apply (simp add:npowf_inv1[of "x" "i + j"])
 apply (simp add:zle)
apply (subgoal_tac "0 < -i + -j") prefer 2 apply simp
 apply (thin_tac "i + j < 0")
 apply (frule zless_imp_zle[of "0" "-i + -j"])
 apply (thin_tac "0 < -i + -j")
apply (cut_tac invf_closed1[of "x"])
apply (simp, erule conjE,
       frule npowf_expTr2[of "x⇗‐K⇖" "-i" "-j"], assumption+)
 apply (simp add:zdiff[THEN sym])
apply (simp add:npowf_inverse, simp)
done

lemma (in Corps) npowf_exp_1_add:"x  carrier K; x  𝟬 
                                        xK⇙⇗(1 + j)= x r xK⇙⇗j⇖"
apply (simp add:npowf_exp_add[of "x" "1" "j"])
apply (cut_tac field_is_ring)
apply (simp add:npowf_def, simp add:Ring.ring_l_one)
done

lemma (in Corps) npowf_minus:"x  carrier K; x  𝟬  (xK⇙⇗j)⇗‐K= xK⇙⇗(- j)⇖"
apply (frule npowf_exp_add[of "x" "j" "-j"], assumption+)
 apply (simp add:field_npowf_exp_zero)
apply (cut_tac field_is_ring)
apply (frule npowf_mem[of "x" "j"], assumption+)
 apply (frule field_potent_nonzero1[of "x" "j"], assumption+)
apply (cut_tac invf_closed1[of "xK⇙⇗j⇖"], simp, erule conjE,
       frule Ring.ring_r_one[of "K" "(xK⇙⇗j)⇗‐K⇖"], assumption, simp,
      thin_tac "1r = xK⇙⇗jr xK⇙⇗- j⇖",
      frule npowf_mem[of "x" "-j"], assumption+)
apply (simp add:field_tOp_assoc[THEN sym], simp add:linvf,
       simp add:Ring.ring_l_one, simp)
done

lemma (in Ring) residue_fieldTr:"maximal_ideal R mx; x  carrier(qring R mx);
 x  𝟬(qring R mx) ycarrier (qring R mx). y r(qring R mx)x = 1r(qring R mx)⇙"
apply (frule maximal_ideal_ideal[of "mx"])
apply (simp add:qring_carrier)
 apply (simp add:qring_zero)
 apply (simp add:qring_def)
 apply (erule bexE)
 apply (frule sym, thin_tac "a ⊎⇘Rmx = x", simp)
 apply (frule_tac a = a in ar_coset_same4_1[of "mx"], assumption+)
 apply (frule_tac x = a in maximal_prime_Tr0[of "mx"], assumption+)
 apply (cut_tac ring_one)
 apply (rotate_tac -2, frule sym, thin_tac "mx  R p a = carrier R")
 apply (frule_tac B = "mx  R p a" in eq_set_inc[of "1r" "carrier R"],
                  assumption+,
        thin_tac "carrier R = mx  R p a")
 apply (frule ideal_subset1[of mx])
 apply (frule_tac a = a in principal_ideal,
        frule_tac I = "R p a" in ideal_subset1)
 apply (cut_tac ring_is_ag,
        simp add:aGroup.set_sum, (erule bexE)+)
 apply (thin_tac "ideal R (R p a)", thin_tac "R p a  carrier R",
        simp add:Rxa_def, (erule bexE)+, simp, thin_tac "k = r r a")
 apply (frule_tac a = r and b = a in rcostOp[of "mx"], assumption+)
 apply (frule_tac x = r and y = a in ring_tOp_closed, assumption+)
 apply (frule_tac a = "r r a" and x = h and b = "1r" in
        aGroup.ag_eq_sol2[of "R"], assumption+)
       apply (simp add:ideal_subset) apply (simp add:ring_one, simp)
       apply (frule_tac a = h and b = "1r ± -a (r r a)" and A = mx in
              eq_elem_in, assumption+)
 apply (frule_tac a = "r r a" and b = "1r" in ar_coset_same1[of "mx"],
        rule ring_tOp_closed, assumption+, rule ring_one, assumption)
  apply (frule_tac a1 = "r r a" and h1 = h in aGroup.arcos_fixed[THEN sym,
         of R mx],  unfold ideal_def, erule conjE, assumption+,
         thin_tac "R +> mx  (rcarrier R. xmx. r r x  mx)",
         thin_tac "x = a ⊎⇘Rmx",
         thin_tac "1r = h ± r r a",
         thin_tac "h = 1r ± -a (r r a)", thin_tac "1r ± -a (r r a)  mx")
  apply (rename_tac b h k r) apply simp
  apply blast
done

(*
constdefs (structure R)
 field_cd::"_ ⇒ bool"
 "field_cd R  == ∀x∈(carrier R - {𝟬}). ∃y∈carrier R.
                                                y ⋅r x = 1r" *)
(* field condition  *) (*
constdefs (structure R)
 rIf :: "_ ⇒ 'a  ⇒ 'a " *) (** rIf is ring_invf **) (*
 "rIf R == λx. (SOME y. y ∈ carrier R ∧ y ⋅r x = 1r)"
*) (*
constdefs (structure R)
  Rf::"_ ⇒ 'a field"
  "Rf R == ⦇carrier = carrier R, pop = pop R, mop = mop R, zero = zero R,
               tp = tp R, un = un R, invf = rIf R⦈" *)

(*
constdefs (structure R)
 Rf ::  "_ ⇒ ⦇ carrier :: 'a set,
  pOp :: ['a, 'a] ⇒ 'a, mOp ::'a ⇒ 'a, zero :: 'a, tOp :: ['a, 'a] ⇒ 'a,
  one ::'a, iOp ::'a ⇒ 'a⦈"

  "Rf R  == ⦇ carrier = carrier R, pOp = pOp R, mOp = mOp R, zero = zero R,
  tOp = tOp R, one = one R, iOp = ring_iOp R⦈" *)
(*
lemma (in Ring) rIf_mem:"⟦field_cd R; x ∈ carrier R - {𝟬}⟧ ⟹
                     rIf R x ∈ carrier R ∧ rIf R x ≠ 𝟬"
apply (simp add:rIf_def)
apply (rule someI2_ex)
apply (simp add:field_cd_def, blast)
apply (simp add:field_cd_def)
 apply (thin_tac "∀x∈carrier R - {𝟬}. ∃y∈carrier R. y ⋅r x = 1r")
 apply (erule conjE)+
 apply (rule contrapos_pp, simp+)
 apply (frule sym, thin_tac "𝟬 ⋅r x = 1r", simp add:ring_times_0_x)
  apply (frule ring_l_one[of "x"])
 apply (simp add:ring_times_0_x)
done

lemma (in Ring) rIf:"⟦field_cd R; x ∈ carrier R - {𝟬}⟧ ⟹
                                           (rIf R x) ⋅r x = 1r"
apply (simp add:rIf_def)
apply (rule someI2_ex)
apply (simp add:field_cd_def, blast)
apply simp
done

lemma (in Ring) field_cd_integral:"field_cd R ⟹ Idomain R"
apply (rule Idomain.intro)
 apply assumption
 apply (rule Idomain_axioms.intro)

apply (rule contrapos_pp, simp+, erule conjE)
apply (cut_tac x = a in rIf_mem, assumption, simp, erule conjE)
apply (frule_tac x = "rIf R a" and y = a and z = b in ring_tOp_assoc,
                 assumption+, simp add:rIf)
apply (simp add:ring_l_one ring_times_x_0)
done

lemma (in Ring) Rf_field:"field_cd R ⟹ field (Rf R)"
apply (rule field.intro)
 apply (simp add:Rf_def)
 apply (rule Ring.intro)
 apply (simp add:pop_closed)
 apply ( cut_tac ring_is_ag, simp add:aGroup.ag_pOp_assoc)
 apply (simp add:Rf_def,
         cut_tac ring_is_ag, simp add:aGroup.ag_pOp_commute)
 apply (simp add:mop_closed)
 apply (simp add:


apply (rule conjI)
 prefer 2
 apply (rule conjI)
 apply (rule univar_func_test, rule ballI)
 apply (simp, erule conjE, simp add:Rf_def)
 apply (rule rIf_mem, assumption+, simp)
apply (rule allI, rule impI)
 apply (simp add:Rf_def)
 apply (frule_tac x = x in rIf, simp, assumption)

 apply (subst Rf_def, simp add:Ring_def)
 apply (cut_tac ring_is_ag)
 apply (rule conjI, simp add:aGroup_def)
 apply (rule conjI, (rule allI, rule impI)+, simp add:aGroup.ag_pOp_assoc)
 apply (rule conjI, (rule allI, rule impI)+, simp add:aGroup.ag_pOp_commute)
 apply (rule conjI, rule univar_func_test, rule ballI,
                                              simp add:aGroup.ag_mOp_closed)
 apply (rule conjI, rule allI, rule impI, simp add:aGroup.ag_l_inv1)
 apply (simp add:aGroup.ag_inc_zero)
 apply (rule conjI, rule allI, rule impI, simp add:aGroup.ag_l_zero)

 apply (rule conjI, rule bivar_func_test, (rule ballI)+,
                                          simp add:ring_tOp_closed)
 apply (rule conjI, (rule allI, rule impI)+, simp add:ring_tOp_assoc)
 apply (rule conjI, (rule allI, rule impI)+, simp add:ring_tOp_commute)
 apply (simp add:ring_one)
 apply (rule conjI, (rule allI, rule impI)+, simp add:ring_distrib1)
 apply (rule allI, rule impI, simp add:ring_l_one)
done
 *)

lemma (in Ring) residue_field_cd:"maximal_ideal R mx 
                                           Corps (qring R mx)"
apply (rule Corps.intro)
apply (rule Ring.qring_ring, rule Ring_axioms)
apply (simp add:maximal_ideal_ideal)
apply (simp add:residue_fieldTr[of "mx"])
done

(*
lemma (in Ring) qRf_field:"maximal_ideal R mx ⟹ field (Rf (qring R mx))"
apply (frule maximal_ideal_ideal[of "mx"])
apply (frule qring_ring [of "mx"])
 apply (frule residue_field_cd[of "mx"])
 apply (rule Ring.Rf_field, assumption+)
done

lemma (in Ring) qRf_pj_rHom:"maximal_ideal R mx ⟹
                          (pj R mx) ∈ rHom R (Rf (qring R mx))"
apply (frule maximal_ideal_ideal[of "mx"])
apply (frule pj_Hom[OF Ring, of "mx"])
apply (simp add:rHom_def aHom_def Rf_def)
done *)

lemma (in Ring) maximal_set_idealTr:
       "maximal_set {I. ideal R I  S  I = {}} mx  ideal R mx"
by (simp add:maximal_set_def)

lemma (in Ring) maximal_setTr:"maximal_set {I. ideal R I  S  I = {}} mx;
                                         ideal R J; mx  J   S  J  {}"
by (rule contrapos_pp, simp+, simp add:psubset_eq, erule conjE,
       simp add:maximal_set_def, blast)

lemma (in Ring) mulDisj:"mul_closed_set R S; 1r  S; 𝟬  S;
    T = {I. ideal R I  S  I = {}}; maximal_set T mx   prime_ideal R mx"
apply (simp add:prime_ideal_def)
apply (rule conjI, simp add:maximal_set_def,
       rule conjI, simp add:maximal_set_def)
apply (rule contrapos_pp, simp+)
apply ((erule conjE)+, blast)

apply ((rule ballI)+, rule impI)
apply (rule contrapos_pp, simp+, (erule conjE)+)
apply (cut_tac a = x in id_ideal_psub_sum[of "mx"],
               simp add:maximal_set_def, assumption+,
       cut_tac a = y in id_ideal_psub_sum[of "mx"],
               simp add:maximal_set_def, assumption+)
apply (frule_tac J = "mx  R p x" in maximal_setTr[of "S" "mx"],
       rule sum_ideals, simp add:maximal_set_def,
       simp add:principal_ideal, assumption,
       thin_tac "mx  mx  R p x")
apply (frule_tac J = "mx  R p y" in maximal_setTr[of "S" "mx"],
       rule sum_ideals, simp add:maximal_set_def,
       simp add:principal_ideal, assumption,
       thin_tac "mx  mx  R p y")
apply (frule_tac A = "S  (mx  R p x)" in nonempty_ex,
       frule_tac A = "S  (mx  R p y)" in nonempty_ex,
       (erule exE)+, simp, (erule conjE)+)
apply (rename_tac x y s1 s2,
       thin_tac "S  (mx  R p x)  {}",
       thin_tac "S  (mx  R p y)  {}")
apply (frule maximal_set_idealTr,
       frule_tac a = x in principal_ideal,
       frule_tac a = y in principal_ideal,
       frule ideal_subset1[of mx],
       frule_tac I = "R p x" in ideal_subset1,
       frule_tac I = "R p y" in ideal_subset1)
apply (cut_tac ring_is_ag,
       simp add:aGroup.set_sum[of R mx],
       erule bexE, erule bexE, simp)
apply (frule_tac s = s1 and t = s2 in mul_closed_set_tOp_closed, simp,
       assumption, simp,
       frule_tac c = h in subsetD[of mx "carrier R"], assumption+,
       frule_tac c = k and A = "R p x" in subsetD[of _ "carrier R"],
       assumption+)
apply (
       cut_tac mul_closed_set_sub,
       frule_tac c = s2 in subsetD[of S "carrier R"], assumption+,
       simp add:ring_distrib2)
apply ((erule bexE)+, simp,
       frule_tac c = ha in subsetD[of mx "carrier R"], assumption+,
       frule_tac c = ka and A = "R p y" in subsetD[of _ "carrier R"],
       assumption+,
       simp add:ring_distrib1)
apply (frule_tac x = h and r = ha in ideal_ring_multiple1[of mx], assumption+)
apply (frule_tac x = h and r = ka in ideal_ring_multiple1[of mx], assumption+,
       frule_tac x = ha and r = k in ideal_ring_multiple[of mx], assumption+)
apply (frule_tac a = x and b = y and x = k and y = ka in
                  mul_two_principal_idealsTr, assumption+,
       erule bexE,
       frule_tac x = "x r y" and r = r in ideal_ring_multiple[of mx],
       assumption+,
       rotate_tac -2, frule sym, thin_tac "k r ka = r r (x r y)", simp)
 apply (frule_tac x = "h r ha ± h r ka" and y = "k r ha ± k r ka" in
        ideal_pOp_closed[of mx])
 apply (rule ideal_pOp_closed, assumption+)+
 apply (simp add:maximal_set_def)
 apply blast
 apply assumption
done

lemma (in Ring) ex_mulDisj_maximal:"mul_closed_set R S; 𝟬  S; 1r  S;
       T = {I. ideal R I  S  I = {}}   mx. maximal_set T mx"
apply (cut_tac A="{ I. ideal R I  S  I = {}}" in Zorn_Lemma2)
prefer 2
  apply (simp add:maximal_set_def)

apply (rule ballI)
apply (case_tac "C = {}")
 apply (cut_tac zero_ideal, blast)

apply (subgoal_tac "C  chains {I. ideal R I  I  carrier R}")
apply (frule chains_un, assumption)
 apply (subgoal_tac "S  ( C) = {}")
 apply (subgoal_tac "xC. x   C",  blast)
apply (rule ballI, rule subsetI, simp add:CollectI)
 apply blast

apply (rule contrapos_pp, simp+)
 apply (frule_tac A = S and B = " C" in nonempty_int)
 apply (erule exE)
 apply (simp, erule conjE, erule bexE)
 apply (simp add:chains_def, erule conjE)
 apply (frule_tac c = X and A = C and B = "{I. ideal R I  S  I = {}}" in
        subsetD, assumption+,
        thin_tac "C  {I. ideal R I  I  carrier R}",
        thin_tac "C  {I. ideal R I  S  I = {}}")
 apply (simp, blast)

apply (simp add:chains_def chain_subset_def, erule conjE)
 apply (rule subsetI)
 apply (frule_tac c = x and A = C and B = "{I. ideal R I  S  I = {}}" in
                  subsetD, assumption+,
        thin_tac "C  {I. ideal R I  S  I = {}}",
        thin_tac "T = {I. ideal R I  S  I = {}}")
 apply (simp, thin_tac "xC. yC. x  y  y  x", erule conjE)
 apply (simp add:psubset_eq ideal_subset1)
 apply (rule contrapos_pp, simp+)
 apply (rotate_tac -1, frule sym, thin_tac "x = carrier R",
        thin_tac "carrier R = x")
 apply (cut_tac ring_one, blast)
done

lemma (in Ring) ex_mulDisj_prime:"mul_closed_set R S; 𝟬  S; 1r  S 
                            mx. prime_ideal R mx  S  mx = {}"
apply (frule ex_mulDisj_maximal[of "S" "{I. ideal R I  S  I = {}}"],
               assumption+, simp, erule exE)
 apply (frule_tac mx = mx in mulDisj [of "S" "{I. ideal R I  S  I = {}}"],
                  assumption+, simp, assumption)
 apply (simp add:maximal_set_def, (erule conjE)+, blast)
done

lemma (in Ring) nilradTr1:"¬ zeroring R  nilrad R =  {p. prime_ideal R p}"
apply (rule equalityI)
 (* nilrad R ⊆ ⋂Collect (prime_ideal R) *)
apply (rule subsetI)
 apply (simp add:nilrad_def CollectI nilpotent_def)
 apply (erule conjE, erule exE)
 apply (rule allI, rule impI)
 apply (frule_tac prime_ideal_ideal)
 apply (frule sym, thin_tac "x^⇗R n= 𝟬", frule ideal_zero, simp)
 apply (case_tac "n = 0", simp)
 apply (frule Zero_ring1[THEN not_sym], simp)
 apply (rule_tac P = xa and x = x in npow_in_prime,assumption+, blast)

apply (rule subsetI)
 apply (rule contrapos_pp, simp+)
 apply (frule id_maximal_Exist, erule exE,
        frule maximal_prime)
 apply (frule_tac a = I in forall_spec, assumption,
        frule_tac I = I in prime_ideal_ideal,
        frule_tac h = x and I = I in ideal_subset, assumption)
apply (subgoal_tac "𝟬  {s. n. s = npow R x n} 
                                  1r  {s. n. s = npow R x n}")
apply (subgoal_tac "mul_closed_set R {s. n. s = npow R x n}")
apply (erule conjE)
apply (frule_tac S = "{s. n. s = npow R x n}" in ex_mulDisj_prime,
       assumption+, erule exE, erule conjE)
apply (subgoal_tac "x  {s. n. s = x^⇗R n}", blast)

apply simp
apply (cut_tac t = x in np_1[THEN sym], assumption, blast)

apply (thin_tac "𝟬  {s. n. s = x^⇗R n}  1r  {s. n. s = x^⇗R n}",
       thin_tac "xa. prime_ideal R xa  x  xa")
apply (subst mul_closed_set_def)
 apply (rule conjI)
 apply (rule subsetI, simp, erule exE)
 apply (simp add:npClose)
apply ((rule ballI)+, simp, (erule exE)+, simp)
 apply (simp add:npMulDistr, blast)

apply (rule conjI)
 apply simp
 apply (rule contrapos_pp, simp+, erule exE)
 apply (frule sym, thin_tac "𝟬 = x^⇗R n⇖")
 apply (simp add:nilrad_def nilpotent_def)

apply simp
 apply (cut_tac x1 = x in npow_0[THEN sym, of "R"], blast)
done

lemma (in Ring) nonilp_residue_nilrad:"¬ zeroring R; x  carrier R;
        nilpotent (qring R (nilrad R)) (x ⊎⇘R(nilrad R)) 
                   x ⊎⇘R(nilrad R) = 𝟬(qring R (nilrad R))⇙"
apply (simp add:nilpotent_def)
 apply (erule exE)
 apply (cut_tac id_nilrad_ideal)
 apply (simp add:qring_zero)
 apply (cut_tac "Ring")
 apply (simp add:npQring)
 apply (frule_tac x = x and n = n in npClose)
 apply (frule_tac I = "nilrad R" and a = "x^⇗R n⇖" in ar_coset_same3,
             assumption+)
 apply (rule_tac I = "nilrad R" and a = x in ar_coset_same4, assumption)
 apply (thin_tac "x^⇗R n⇖ ⊎⇘Rnilrad R = nilrad R",
        simp add:nilrad_def nilpotent_def, erule exE)
 apply (simp add:npMulExp, blast)
done

lemma (in Ring) ex_contid_maximal:" S = {1r}; 𝟬  S; ideal R I; I  S = {};
T = {J. ideal R J  S  J = {}  I  J}  mx. maximal_set T mx"
apply (cut_tac A="{J. ideal R J  S  J = {}  I  J}" in Zorn_Lemma2)
apply (rule ballI)
apply (case_tac "C = {}") (** case C = {} **)
 apply blast             (** case C = {} done **)
     (** existence of sup in C **)
apply (subgoal_tac "C{J. ideal R J  S  J = {}  I  J} 
                                         (xC. x   C)")
 apply blast
apply (rule conjI,
       simp add:CollectI)
apply (subgoal_tac "C  chains {I. ideal R I  I  carrier R}")
apply (rule conjI,
       simp add:chains_un)
apply (rule conjI)
apply (rule contrapos_pp, simp+, erule bexE)
 apply (thin_tac " C  chains {I. ideal R I  I  carrier R}")
 apply (simp add:chains_def, erule conjE)
 apply (frule_tac c = x and A = C and B = "{J. ideal R J  1r  J  I  J}"
         in subsetD, assumption+, simp,
        thin_tac "C  chains {I. ideal R I  I  carrier R}")
 apply (frule_tac A = C in nonempty_ex, erule exE, simp add:chains_def,
        erule conjE,
        frule_tac c = x and A = C and B = "{J. ideal R J  1r  J  I  J}" in
                  subsetD, assumption+, simp, (erule conjE)+)
 apply (rule_tac A = I and B = x and C = "C" in subset_trans, assumption,
        rule_tac B = x and A = C in Union_upper, assumption+)
 apply (simp add:chains_def, erule conjE)
 apply (rule subsetI, simp)
 apply (frule_tac c = x and A = C and B = "{J. ideal R J  1r  J  I  J}"
        in subsetD, assumption+, simp, (erule conjE)+)
 apply (subst psubset_eq, simp add:ideal_subset1)
 apply (rule contrapos_pp, simp+, simp add:ring_one)

 apply (rule ballI)
 apply (rule Union_upper, assumption)
 apply (erule bexE)
 apply (simp add:maximal_set_def)
 apply blast
done

lemma (in Ring) contid_maximal:"S = {1r}; 𝟬  S; ideal R I; I  S = {};
             T = {J. ideal R J  S  J = {}  I  J}; maximal_set T mx 
                                                maximal_ideal R mx"
apply (simp add:maximal_set_def maximal_ideal_def)
apply (erule conjE)+
apply (rule equalityI)
  (** {J. ideal R J ∧ mx ⊆ J} ⊆ {mx, carrier R} **)
  apply (rule subsetI, simp add:CollectI, erule conjE)
 apply (case_tac "x = mx", simp, simp)
 apply (subgoal_tac "1r  x")
 apply (rule_tac  I = x in ideal_inc_one, assumption+)
 apply (rule contrapos_pp, simp+)
apply (drule spec[of _ mx])
 apply (simp add:whole_ideal,
        rule subsetI, rule ideal_subset[of "mx"], assumption+)
done

lemma (in Ring) ideal_contained_maxid:"¬(zeroring R); ideal R I; 1r  I 
                    mx. maximal_ideal R mx  I  mx"
apply (cut_tac ex_contid_maximal[of "{1r}" "I"
                      "{J. ideal R J  {1r}  J = {}  I  J}"])
apply (erule exE,
       cut_tac mx = mx in contid_maximal[of "{1r}" "I"
                         "{J. ideal R J  {1r}  J = {}  I  J}"])
apply simp
 apply (frule Zero_ring1, simp,
        assumption, simp, simp, simp,
        simp add:maximal_set_def, (erule conjE)+, blast,
        simp, frule Zero_ring1, simp)
 apply (assumption, simp, simp)
done

lemma (in Ring) nonunit_principal_id:"a  carrier R; ¬ (Unit R a) 
                                             (R p a)  (carrier R)"
apply (rule contrapos_pp, simp+)
apply (frule sym, thin_tac "R p a = carrier R")
apply (cut_tac ring_one)
 apply (frule eq_set_inc[of "1r" "carrier R" "R p a"], assumption,
        thin_tac "carrier R = R p a", thin_tac "1r  carrier R")
apply (simp add:Rxa_def, erule bexE, simp add:ring_tOp_commute[of _ "a"],
       frule sym, thin_tac "1r = a r r")
apply (simp add:Unit_def)
done

lemma (in Ring) nonunit_contained_maxid:"¬(zeroring R); a  carrier R;
                ¬ Unit R a     mx. maximal_ideal R mx  a   mx"
apply (frule principal_ideal[of "a"],
       frule ideal_contained_maxid[of "R p a"], assumption)
 apply (rule contrapos_pp, simp+,
        frule ideal_inc_one[of "R p a"], assumption,
        simp add:nonunit_principal_id)
apply (erule exE, erule conjE)
 apply (frule a_in_principal[of "a"])
 apply (frule_tac B = mx in subsetD[of "R p a" _ "a"], assumption, blast)
done

definition
  local_ring :: "_  bool" where
  "local_ring R == Ring R  ¬ zeroring R  card {mx. maximal_ideal R mx} = 1"

lemma (in Ring) local_ring_diff:"¬ zeroring R; ideal R mx; mx  carrier R;
  a (carrier R - mx). Unit R a   local_ring R  maximal_ideal R mx"
apply (subgoal_tac "{mx} = {m. maximal_ideal R m}")
 apply (cut_tac singletonI[of "mx"], simp)
 apply (frule sym, thin_tac "{mx} = {m. maximal_ideal R m}")
 apply (simp add:local_ring_def, simp add:Ring)
apply (rule equalityI)
 apply (rule subsetI, simp)
 apply (simp add:maximal_ideal_def)
 apply (simp add:ideal_inc_one1[of "mx", THEN sym])
 apply (thin_tac "x = mx", simp)
 apply (rule equalityI)
  apply (rule subsetI, simp, erule conjE)
  apply (case_tac "x  mx")
  apply (frule_tac A = x and B = mx in sets_not_eq, assumption)
  apply (erule bexE)
  apply (frule_tac h = a and I = x in ideal_subset, assumption+)
  apply (frule_tac x = a in bspec, simp)
  apply (frule_tac I = x and a = a in ideal_inc_unit1, assumption+,
        simp)
  apply simp

  apply (rule subsetI, simp)
  apply (erule disjE)
  apply simp
  apply (simp add:whole_ideal ideal_subset1)

apply (rule subsetI)
 apply simp
 apply (subgoal_tac "x  mx",
        thin_tac "acarrier R - mx. Unit R a",
        simp add:maximal_ideal_def, (erule conjE)+)
 apply (subgoal_tac "mx  {J. ideal R J  x  J}", simp)
 apply (thin_tac "{J. ideal R J  x  J} = {x, carrier R}")
 apply simp

 apply (rule contrapos_pp, simp+)
 apply (simp add:subset_eq, erule bexE)
 apply (frule_tac mx = x in maximal_ideal_ideal,
        frule_tac x = xa in bspec,
        thin_tac "acarrier R - mx. Unit R a", simp,
        simp add:ideal_subset)
 apply (frule_tac I = x and a = xa in ideal_inc_unit, assumption+,
                  simp add:maximal_ideal_def)
done

lemma (in Ring) localring_unit:"¬ zeroring R; maximal_ideal R mx;
                x. x  mx  Unit R (x ± 1r)   local_ring R"
apply (frule maximal_ideal_ideal[of "mx"])
apply (frule local_ring_diff[of "mx"], assumption)
 apply (simp add:maximal_ideal_def, erule conjE)
 apply (simp add:ideal_inc_one1[THEN sym, of "mx"])
 apply (rule ballI, simp, erule conjE)

 apply (frule_tac x = a in maximal_prime_Tr0[of "mx"], assumption+)

 apply (frule sym, thin_tac "mx  R p a = carrier R",
        cut_tac ring_one,
        frule_tac a = "1r" and A = "carrier R" and B = "mx  R p a" in
                  eq_set_inc, assumption+,
        thin_tac "carrier R = mx  R p a")
 apply (frule_tac a = a in principal_ideal,
       frule ideal_subset1[of mx],
       frule_tac I = "R p a" in ideal_subset1)
 apply (cut_tac ring_is_ag,
        simp add:aGroup.set_sum, (erule bexE)+)
 apply (simp add:Rxa_def, erule bexE, simp)
 apply (frule sym, thin_tac "1r = h ± r r a",
        frule_tac x = r and y = a in ring_tOp_closed, assumption+,
        frule_tac h = h in ideal_subset[of "mx"], assumption+)
 apply (frule_tac I = mx and x = h in ideal_inv1_closed, assumption)
 apply (frule_tac a = "-a h" in forall_spec, assumption,
        thin_tac "x. x  mx  Unit R (x ± (h ± r r a))",
        thin_tac "h ± r r a = 1r")
 apply (frule_tac h = "-a h" in ideal_subset[of "mx"], assumption,
        frule_tac x1 = "-a h" and y1 = h and z1 = "r r a" in
        aGroup.ag_pOp_assoc[THEN sym], assumption+,
        simp add:aGroup.ag_l_inv1 aGroup.ag_l_zero,
        thin_tac "k = r r a", thin_tac "h ± r r a  carrier R",
        thin_tac "h  carrier R", thin_tac "-a h  mx",
        thin_tac "-a h ± (h ± r r a) = r r a")
 apply (simp add:ring_tOp_commute, simp add:Unit_def, erule bexE,
        simp add:ring_tOp_assoc,
        frule_tac x = r and y = b in ring_tOp_closed, assumption+, blast)
 apply simp
done

definition
  J_rad ::"_  'a set" where
  "J_rad R = (if (zeroring R) then (carrier R) else
                  {mx. maximal_ideal R mx})"
  (** if zeroring R then ⋂ {mx. maximal_ideal R mx} is UNIV, hence
      we restrict UNIV to carrier R **)

lemma (in Ring) zeroring_J_rad_empty:"zeroring R  J_rad R = carrier R"
by (simp add:J_rad_def)

lemma (in Ring) J_rad_mem:"x  J_rad R  x  carrier R"
apply (simp add:J_rad_def)
apply (case_tac "zeroring R", simp)
apply simp
apply (frule id_maximal_Exist, erule exE)
 apply (frule_tac a = I in forall_spec, assumption,
        thin_tac "xa. maximal_ideal R xa  x  xa")
 apply (frule maximal_ideal_ideal,
        simp add:ideal_subset)
done

lemma (in Ring) J_rad_unit:"¬ zeroring R; x  J_rad R 
            y. (y carrier R  Unit R (1r ± (-a x) r y))"
apply (cut_tac ring_is_ag,
       rule allI, rule impI,
       rule contrapos_pp, simp+)
apply (frule J_rad_mem[of "x"],
       frule_tac x = x and y = y in ring_tOp_closed, assumption,
       frule_tac x = "x r y" in aGroup.ag_mOp_closed, assumption+)
apply (cut_tac ring_one,
      frule_tac x = "1r" and y = "-a (x r y)" in aGroup.ag_pOp_closed,
      assumption+)
 apply (frule_tac a = "1r ± -a (x r y)" in nonunit_contained_maxid,
        assumption+, simp add:ring_inv1_1)
apply (erule exE, erule conjE)
 apply (simp add:J_rad_def,
        frule_tac a = mx in forall_spec, assumption,
        thin_tac "xa. maximal_ideal R xa  x  xa",
        frule_tac mx = mx in maximal_ideal_ideal,
        frule_tac I = mx and x = x and r = y in ideal_ring_multiple1,
        assumption+)
 apply (frule_tac I = mx and x = "x r y" in ideal_inv1_closed,
           assumption+)

 apply (frule_tac I = mx and a = "1r" and b = "-a (x r y)" in ideal_ele_sumTr2,
        assumption+)
 apply (simp add:maximal_ideal_def)
done

end