Theory Algebra9

(**        Algebra9  
                            author Hidetsune Kobayashi
                            Group You Santo
                            Department of Mathematics
                            Nihon University
                            hikoba@math.cst.nihon-u.ac.jp
                            May 3, 2004.
                            April 6, 2007 (revised)

   chapter 5. Modules
    section 8. exact sequence 
    section 9. Tensor products 

   chapter 6. Construction of a special aelian group
    section 1. free generated abelian group, direct sum and direct product 2 
    section 2. Abelian Group generated by one element
    section 3. Free Generated Modules
    section 4. a fgmodule and a free module
    section 5. direct sum, again
 
   **)

theory Algebra9 imports Algebra8 begin

section "Exact sequence"

definition
  Zm :: "[('r, 'm) Ring_scheme, 'a]  ('a, 'r) Module" where
  "Zm R e =  carrier = {e}, pop = λx{e}. λy{e}. e, mop = 
    λx{e}. e, zero = e, sprod = λrcarrier R. λx{e}. e"

lemma (in Ring) Zm_Module:"R module (Zm R e)"
apply (simp add:Module_def aGroup_def Zm_def Module_axioms_def)
 apply (simp add:ring_one, cut_tac Ring, simp)
 apply (rule conjI)
 apply (rule allI, rule impI, rule allI, rule impI, rule impI)
 apply (cut_tac ring_is_ag, 
        frule_tac x = a and y = b in aGroup.ag_pOp_closed, assumption+, simp)
 apply (rule allI, rule impI, rule allI, rule impI, rule impI,
        frule_tac x = a and y = b in ring_tOp_closed, assumption+, simp)
done

lemma (in Ring) Zm_carrier:"carrier (Zm R e) = {e}"
apply (simp add:Zm_def)
done

lemma (in Ring) Zm_to_M_0:"R module M; f  mHom R (Zm R e) M  
                     f e = 𝟬M⇙"
apply (cut_tac Zm_Module [of e])  
 apply (frule Module.mHom_add [of "Zm R e" R M f e e], assumption+,
        (simp add:Zm_carrier)+,
        frule_tac R = R and M = "Zm R e" in Module.module_is_ag,
        frule_tac x = e and y = e in aGroup.ag_pOp_closed[of "Zm R e"],
        (simp add:Zm_carrier)+)
 apply (frule_tac R = R and M = "Zm R e" and N = M and f = f and m = e in
        Module.mHom_mem, assumption+, simp add:Zm_carrier,
        frule sym, thin_tac "f e =  f e  ±M(f e)",
        frule_tac R = R and M = M in Module.module_is_ag,
        frule aGroup.ag_eq_sol2 [of M "f e" "f e" "f e"], assumption+)
apply (simp add:aGroup.ag_r_inv1)
done

lemma (in Ring) Z_to_M:"R module M; f  mHom R (Zm R e) M; 
                              g  mHom R (Zm R e) M    f = g"
apply (rule_tac R = R and M = "Zm R e" and N = M in Module.mHom_eq)
 apply (simp add:Zm_Module)
 apply assumption+
 apply (rule ballI)
 apply (simp add:Zm_carrier)
 apply (simp add:Zm_to_M_0 [of _ _ e])
done

lemma (in Ring) mzeromap_mHom:"R module M; R module N  
                                     mzeromap M N  mHom R M N"  
apply (simp add:mHom_def aHom_def)
apply (rule conjI)
 apply (simp add:mzeromap_def, simp add:Module_def aGroup_def)
apply (rule conjI)
 apply (simp add:mzeromap_def extensional_def)
apply (rule conjI)
 apply ((rule ballI)+, 
        frule_tac R = R and M = M in Module.module_is_ag,
        frule_tac x = a and y = b in aGroup.ag_pOp_closed [of "M"], 
        assumption+,
        simp add:mzeromap_def,
        frule_tac R = R and M = N in Module.module_is_ag,
        rule aGroup.ag_l_zero[THEN sym, of "N"], assumption+,
        simp add:aGroup.ag_inc_zero)
apply (rule ballI)+
 apply (frule_tac a = a and m = m in Module.sc_mem [of M R], assumption+,
        simp add:mzeromap_def,
        rule Module.sc_a_0 [THEN sym], assumption+)
done

lemma (in Ring) HOM_carrier:"carrier (HOM⇘RM N) = mHom R M N"
apply (simp add:HOM_def)
done

lemma (in Ring) mHom_Z_M:"R module M  
              mHom R (Zm R e) M = {mzeromap (Zm R e) M}"
apply (rule equalityI)
 apply (rule subsetI)
 apply simp 
 apply (cut_tac Zm_Module [of e])
 apply (frule mzeromap_mHom [of "Zm R e" M], assumption+)
 apply (simp add:Z_to_M)
apply (rule subsetI) apply simp
 apply (cut_tac Zm_Module[of e],
        simp add:mzeromap_mHom)
done

lemma (in Module) Modules_single_carrier_isom:"R module N; carrier M = {𝟬};
      carrier N = {𝟬N}  M ≅⇘RN"
apply (subgoal_tac "bijec⇘M, N(λx{𝟬}. 𝟬N) 
                          (λx{𝟬}. 𝟬N)  mHom R M N")
apply (simp add:misomorphic_def, blast,
       subgoal_tac "(λx{𝟬}. 𝟬N)  mHom R M N", simp)
apply (simp add:bijec_def injec_def surjec_def mHom_def,
       simp add:ker_def surj_to_def)

apply (simp add:mHom_def aHom_def)
 apply (cut_tac ag_inc_zero, simp add:ag_l_zero)
 apply (frule_tac R = R and M = N in Module.module_is_ag,
        frule aGroup.ag_inc_zero[of N],
        simp add:aGroup.ag_l_zero[of N])
 apply (simp add:sc_a_0 Module.sc_a_0)
done

lemma (in Ring) Zm_isom:"(Zm R (e::'a)) ≅⇘R(Zm R (u::'b))"
apply (cut_tac Zm_Module[of e], cut_tac Zm_Module[of u])
apply (rule_tac R = R and M = "Zm R e" and N = "Zm R u" in 
                Module.Modules_single_carrier_isom, assumption+)
apply (simp add:Zm_def)+
done

lemma (in Ring) HOM_Z_M_0:"R module M  HOM⇘R(Zm R e) M ≅⇘R(Zm R e)"
 apply (cut_tac Zm_Module[of e],
        frule_tac M = "Zm R e" and N = M in Module.HOM_is_module,
        assumption+)
 apply (cut_tac M = "Zm R e" and N = M in HOM_carrier)
 apply (simp add: mHom_Z_M)
 apply (simp add:Module.zero_HOM)
 apply (rule_tac R = R and M = "HOM⇘R(Zm R e) M" and N = "Zm R e" in 
         Module.Modules_single_carrier_isom, assumption+)
 apply (simp add:Zm_def)
done
 
lemma (in Ring) M_to_Z:"R module M; f  mHom R M (Zm R e); 
                               g  mHom R M (Zm R e)   f = g"
apply (rule Module.mHom_eq [of M _ "Zm R e"], assumption+)
 apply (simp add:Zm_Module, assumption+)
 apply (rule ballI) 
 apply (frule_tac m = m in Module.mHom_mem [of M _ "Zm R e" f],
        simp add:Zm_Module, assumption+)
 apply (frule_tac m = m in Module.mHom_mem [of M _ "Zm R e" g],
        simp add:Zm_Module, assumption+)
 apply (simp add:Zm_carrier)
done

lemma (in Ring) mHom_to_zero:"R module M   mHom R M (Zm R e) = 
                                              {mzeromap M (Zm R e)}"
apply (frule mzeromap_mHom [of M "Zm R e"])
 apply (simp add:Zm_Module)
apply (rule equalityI)
 apply (rule subsetI)
 apply (frule_tac f = "mzeromap M (Zm R e)" and g = x in M_to_Z [of M],
                                      assumption+)
 apply simp
 apply (rule subsetI)
 apply simp
done

lemma (in Ring) carrier_HOM_M_Z:"R module M  
                 carrier (HOM⇘RM (Zm R e)) = {mzeromap M (Zm R e)}"
apply (subst HOM_carrier)
apply (simp add:mHom_to_zero)
done

lemma (in Ring) HOM_M_Z_0:"R module M  HOM⇘RM (Zm R e) ≅⇘R(Zm R e)"
apply (cut_tac Zm_Module[of e],
        frule_tac M = M and N = "Zm R e" in Module.HOM_is_module,
        assumption+)
 apply (frule_tac M = M and e = e in carrier_HOM_M_Z)
 apply (simp add:Module.zero_HOM)
 apply (rule Module.Modules_single_carrier_isom, assumption+)
 apply (simp add:Zm_def)
done

lemma (in Ring) M_to_Z_0:"R module M; f  mHom R M (Zm R e) 
                              ker⇘M,(Zm R e)f = carrier M"
apply (simp add:ker_def)
apply (simp add:Zm_def) apply (fold Zm_def)
apply (rule equalityI)
 apply (rule subsetI)
 apply (simp add:CollectI)
 apply (rule subsetI, simp)
 apply (cut_tac Zm_Module[of e])
 apply (frule_tac R = R and M = M and N = "Zm R e" and f = f and m = x in 
         Module.mHom_mem, assumption+)
 apply (simp add:Zm_carrier)
done

definition
  exact3 :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 'a  'b,
    ('b, 'r, 'm1) Module_scheme, 'b  'c, ('c, 'r, 'm1) Module_scheme]  bool" where
  "exact3 R L0 h0 L1 h1 L2 == h0 ` (carrier L0) = ker⇘(L1),(L2)h1"

definition
  exact4 :: "[('r, 'm) Ring_scheme, ('a0, 'r, 'm1) Module_scheme, 'a0  'a1, 
    ('a1, 'r, 'm1) Module_scheme, 'a1  'a2, ('a2, 'r, 'm1) Module_scheme, 
    'a2  'a3, ('a3, 'r, 'm1) Module_scheme]  bool" where
  "exact4 R L0 h0 L1 h1 L2 h2 L3  h0 ` (carrier L0) = ker⇘(L1),(L2)h1  
                                     h1 ` (carrier L1) = ker⇘(L2),(L3)h2 "

definition
  exact5 :: "[('r, 'm) Ring_scheme, ('a0, 'r, 'm1) Module_scheme, 'a0  'a1,  
    ('a1, 'r, 'm1) Module_scheme, 'a1  'a2, ('a2, 'r, 'm1) Module_scheme, 
    'a2  'a3, ('a3, 'r, 'm1) Module_scheme, 'a3  'a4, 
    ('a4, 'r, 'm1) Module_scheme]  bool" where
  "exact5 R L0 h0 L1 h1 L2 h2 L3 h3 L4 == h0 ` (carrier L0) = ker⇘(L1),(L2)h1 
    h1 ` (carrier L1) = ker⇘(L2),(L3)h2  h2 `(carrier L2) = ker⇘(L3),(L4)h3 "

definition
  exact8 :: "[('r, 'm) Ring_scheme, ('a0, 'r, 'm1) Module_scheme, 'a0  'a1, 
    ('a1, 'r, 'm1) Module_scheme, 'a1  'a2, ('a2, 'r, 'm1) Module_scheme, 
    'a2  'a3, ('a3, 'r, 'm1) Module_scheme, 'a3  'a4,  
    ('a4, 'r, 'm1) Module_scheme, 'a4  'a5, ('a5, 'r, 'm1) Module_scheme,
    'a5  'a6, ('a6, 'r, 'm1) Module_scheme, 'a6  'a7, 
    ('a7, 'r, 'm1) Module_scheme]  bool"  where
  "exact8 R L0 h0 L1 h1 L2 h2 L3 h3 L4 h4 L5 h5 L6 h6 L7 
    h0 ` (carrier L0) = ker⇘(L1),(L2)h1  h1 ` (carrier L1) = ker⇘(L2),(L3)h2 
    h2 ` (carrier L2) = ker⇘(L3),(L4)h3  h3 ` (carrier L3) = ker⇘(L4),(L5)h4  
    h4 ` (carrier L4) = ker⇘(L5),(L6)h5  h5 ` (carrier L5) = ker⇘(L6),(L7)h6"

lemma (in Ring) exact3_comp_0:"R module L; R module M; R module N; 
       f  mHom R L M; g  mHom R M N; exact3 R L f M g N  
      compos L g f = mzeromap L N"
apply (frule Module.mHom_compos [of M R L N f g], assumption+,
       frule mzeromap_mHom [of L N], assumption,
       rule Module.mHom_eq [of L R N], assumption+)
apply (rule ballI)
 apply (subst compos_def)+ 
 apply (simp add:exact3_def)
 apply (cut_tac mHom_func[of f L M])
 apply (frule_tac a = m in mem_in_image [of f "carrier L" "carrier M"], 
          assumption+)
 apply simp
apply (simp add:ker_def mzeromap_def compose_def, assumption)
done

lemma (in Ring) exact_im_sub_kern:"R module L; R module M; R module N; 
             f  mHom R L M; g  mHom R M N; exact3 R L f M g N  
           f ` (carrier L)  ker⇘M,Ng"
apply (simp add:exact3_def)
done

lemma (in Ring) mzero_im_sub_ker:"R module L; R module M; R module N; 
       f  mHom R L M; g  mHom R M N; compos L g f = mzeromap L N  
      f ` (carrier L)  ker⇘M,Ng"
apply (rule subsetI)
 apply (simp add:image_def)
 apply auto
 apply (simp add:ker_def)
 apply (simp add:Module.mHom_mem)
 apply (simp add:compos_def compose_def)
 apply (subgoal_tac "(λxcarrier L. g (f x)) xa = mzeromap L N xa")
 prefer 2 apply simp
 apply (thin_tac "(λxcarrier L. g (f x)) = mzeromap L N")
 apply simp
 apply (simp add:mzeromap_def)
done

lemma (in Ring) left_exact_injec:"R module M; R module N; 
      z  mHom R (Zm R e) M; f  mHom R M N; exact3 R (Zm R e) z M f N 
      injec⇘M,Nf"
apply (simp add:injec_def)
apply (rule conjI)
apply (simp add:mHom_def) 
apply (simp add:exact3_def)
apply (simp add:Zm_def, fold Zm_def)
apply (simp add: Zm_to_M_0 [of M z e])
done

lemma (in Ring) injec_left_exact:"R module M; R module N; 
       z  mHom R (Zm R e) M; f  mHom R M N; injec⇘M,Nf  
       exact3 R (Zm R e) z M f N"
apply (simp add:exact3_def)
apply (simp add:Zm_def, fold Zm_def)
 apply (simp add:Zm_to_M_0 [of  "M" "z" "e"])
 apply (simp add:injec_def)
done

  (*  injec_mHom_image
                 N
                 | \ x      x `(N) ⊆ f `(M1)     
                 |  \            
                M1 → M2
                   f                     *)
lemma (in Ring) injec_mHom_image:"R module N; R module M1; R module M2; 
       x  mHom R N M2; f  mHom R M1 M2; x ` (carrier N)  f ` (carrier M1);
       injec⇘M1,M2f
   (λn (carrier N). (SOME m. (m  carrier M1  x n = f m)))  mHom R N M1 
   compos N f (λn  (carrier N). (SOME m. m  carrier M1  x n = f m)) = x"
apply (subgoal_tac "(λncarrier N. SOME m. m  carrier M1  x n = f m)  
       mHom R N M1", simp)
apply (rule Module.mHom_eq, assumption+)
 apply (simp add:Module.mHom_compos, assumption)
 apply (rule ballI)
 apply (simp add:compos_def compose_def)
 apply (thin_tac "(λncarrier N. SOME m. m  carrier M1  x n = f m)  
                   mHom R N M1")
 apply (frule_tac m = m in Module.mHom_mem [of N R M2 x], assumption+)
 apply (cut_tac mHom_func[of x N M2],
        frule_tac a = m in mem_in_image[of x "carrier N" "carrier M2"], 
        assumption+,
        frule_tac c = "x m" in subsetD [of "x ` carrier N" "f ` carrier M1"], 
        assumption+)
 apply (simp add:image_def)
 apply (rule someI2_ex, blast)
 apply (thin_tac "xacarrier M1. x m = f xa", erule conjE)
 apply (rotate_tac -1, rule sym, assumption+) 

apply (simp add:mHom_def[of R N M1] aHom_def)
 apply (rule conjI)
 apply (rule Pi_I)
 apply (cut_tac mHom_func[of x N M2],
        frule_tac a = xa in mem_in_image[of x "carrier N" "carrier M2"], 
        assumption+,
        frule_tac c = "x xa" in subsetD [of "x ` carrier N" "f ` carrier M1"], 
        assumption+)
 apply (simp add:image_def)
 apply (rule someI2_ex, blast, simp, assumption)
 
apply (frule_tac R = R and M = N in Module.module_is_ag,
       simp add:aGroup.ag_pOp_closed, 
       simp add:Module.sc_mem)
apply (rule conjI)
 apply (rule ballI)+
 apply (frule_tac x = a and y = b in aGroup.ag_pOp_closed [of "N"], 
                                                            assumption+)
 apply (cut_tac mHom_func[of x N M2],
        frule_tac a = "a ±Nb" in mem_in_image[of x "carrier N" "carrier M2"],
        assumption+,
        frule_tac c = "x (a ±Nb)" in subsetD[of "x ` carrier N" 
                                      "f ` carrier M1"], assumption+)
 apply (frule_tac a = a in mem_in_image[of x "carrier N" "carrier M2"],
        assumption+,
        frule_tac c = "x a" in subsetD[of "x ` carrier N" 
                                      "f ` carrier M1"], assumption+,
        frule_tac a = b in mem_in_image[of x "carrier N" "carrier M2"],
        assumption+,
        frule_tac c = "x b" in subsetD[of "x ` carrier N" 
                                      "f ` carrier M1"], assumption+)  
 apply (simp add:image_def)
 apply (rule someI2_ex, blast)
 apply (rule someI2_ex, blast)
 apply (rule someI2_ex, blast)
 apply (thin_tac "xacarrier N. x ( a ±Nb) = x xa",
        thin_tac "xacarrier M1. x ( a ±Nb) = f xa",
        thin_tac "xacarrier N. x a = x xa",
        thin_tac "xacarrier M1. x a = f xa",
        thin_tac "xacarrier N. x b = x xa",
        thin_tac "xacarrier M1. x b = f xa")
 apply ((erule conjE)+, fold image_def)
 apply (frule_tac R = R and M = N and N = M2 and f = x and m = a and n = b in
        Module.mHom_add, assumption+, simp)
 apply (simp add:Module.mHom_add[THEN sym, of _ _ _ f])
 apply (frule_tac R = R and M = M1 and N = M2 and f = f in Module.minjec_inj,
        assumption+)
 apply (frule_tac R = R and M = M1 in Module.module_is_ag,
        frule_tac x = xaa and y = xa in aGroup.ag_pOp_closed[of M1], 
        assumption+)
 apply (simp add:inj_on_def, assumption) 

apply (rule ballI)+
 apply (frule_tac a = a and m = m in Module.sc_mem [of N R], assumption+)
 apply (cut_tac mHom_func[of x N M2],
        frule_tac a = "a sNm" in mem_in_image[of x "carrier N" "carrier M2"],
        assumption+,
        frule_tac c = "x (a sNm)" in subsetD[of "x ` carrier N" 
                                      "f ` carrier M1"], assumption+)
 apply (frule_tac a = m in mem_in_image[of x "carrier N" "carrier M2"],
        assumption+,
        frule_tac c = "x m" in subsetD[of "x ` carrier N" 
                                      "f ` carrier M1"], assumption+)
 apply (thin_tac "x (a sNm)  x ` carrier N",
        thin_tac "x m  x ` carrier N")
 apply (simp add:image_def)
 apply (rule someI2_ex, blast)
 apply (rule someI2_ex, blast)
 apply (thin_tac "xacarrier M1. x m = f xa",
        thin_tac "xacarrier M1. x (a sNm) = f xa")
 apply (erule conjE)+
 apply (simp add:Module.mHom_lin) 
 apply (simp add:Module.mHom_lin[THEN sym])
 apply (rule sym)
 apply (frule_tac R = R and M = M1 and N = M2 and f = f in Module.minjec_inj,
        assumption+,
        frule_tac R = R and M = M1 and a = a and m = xa in Module.sc_mem,
        assumption+)
 apply (simp add:inj_on_def, assumption)
done

lemma (in Ring) right_exact_surjec:"R module M; R module N; f  mHom R M N;
 p  mHom R N (Zm R e); exact3 R M f N p (Zm R e)  surjec⇘M,Nf" 
apply (simp add:surjec_def)
 apply (rule conjI)
 apply (simp add:mHom_def)
 apply (simp add:surj_to_def)
apply (simp add:exact3_def) 
 apply (simp add:M_to_Z_0)
done

lemma (in Ring) surjec_right_exact:"R module M; R module N; f  mHom R M N;
 p  mHom R N (Zm R e); surjec⇘M,Nf  exact3 R M f N p (Zm R e)"
apply (simp add:exact3_def)
apply (simp add:ker_def)
 apply (frule_tac f = p and M = N and N = "Zm R e" in mHom_func,
        simp add:Zm_carrier)
 apply (simp add:surjec_def surj_to_def,
        thin_tac "f  aHom M N  f ` carrier M = carrier N")
 apply (subst Zm_def, simp)
 apply (rule equalityI, rule subsetI, simp, 
        frule funcset_mem[of p "carrier N" "{e}"], assumption, simp)
 apply (rule subsetI, simp)
done

lemma (in Ring) exact4_exact3:"R module M; R module N; z  mHom R (Zm R e) M;
       f  mHom R M N; z1  mHom R N (Zm R e); 
       exact4 R (Zm R e) z M f N z1 (Zm R e)  
      exact3 R (Zm R e) z M f N  exact3 R M f N z1 (Zm R e)"
apply (simp add:exact4_def exact3_def)
done
 
lemma (in Ring) exact4_bijec:"R module M; R module N; z  mHom R (Zm R e) M; 
       f  mHom R M N; z1  mHom R N (Zm R e); 
       exact4 R (Zm R e) z M f N z1 (Zm R e)  bijec⇘M,Nf"
apply (frule exact4_exact3 [of M N z e f z1], assumption+)
 apply (erule conjE)
 apply (simp add:bijec_def)
 apply (simp add:left_exact_injec)
 apply (simp add:right_exact_surjec)
done

lemma (in Ring) exact_im_sub_ker:"R module L; R module M; R module N; 
  f  mHom R L M; g  mHom R M N; z1  mHom R N (Zm R e); R module Z; 
  exact4 R L f M g N z1 (Zm R e); x  mHom R M Z; compos L x f = mzeromap L Z
   (λz(carrier N). x (SOME y. y  carrier M  g y = z))  mHom R N Z"
apply (subst mHom_def, simp)
apply (rule conjI)
 apply (simp add:aHom_def)
 apply (rule conjI)
 apply (rule Pi_I)
 apply simp
apply (subgoal_tac "exact3 R M g N z1 (Zm R e)")
prefer 2 apply (simp add:exact4_def exact3_def)
apply (frule right_exact_surjec [of M N g z1], assumption+)
 apply (simp add:surjec_def, frule conjunct2)
 apply (simp add:surj_to_def, erule conjE)
 apply (rotate_tac -1, frule sym, thin_tac "g ` carrier M = carrier N",
        simp, thin_tac "carrier N = g ` carrier M")
 apply (simp add:image_def)
 apply (rule someI2_ex, blast)
 apply (erule conjE, simp add:Module.mHom_mem)
 
apply (frule_tac R = R and M = N in Module.module_is_ag,
       simp add:aGroup.ag_pOp_closed) 
 apply (rule ballI)+
 apply (frule_tac x = a and y = b in aGroup.ag_pOp_closed [of "N"], 
                                                         assumption+)
apply (subgoal_tac "exact3 R M g N z1 (Zm R e)") 
prefer 2 apply (simp add:exact4_def exact3_def,
        frule right_exact_surjec[of M N g z1], assumption+)
 apply (simp add:surjec_def surj_to_def, erule conjE)
 apply (rotate_tac -1, frule sym, thin_tac "g ` carrier M = carrier N",
        simp)
 apply (simp add:image_def)
 apply (rule someI2_ex, blast) 
 apply (rule someI2_ex, blast)
 apply (rule someI2_ex) 
 apply (thin_tac "xcarrier M. a = g x", thin_tac "xcarrier M. b = g x",
        thin_tac "xa  carrier M  g xa = b", 
        thin_tac "xaa  carrier M  g xaa = a")
 apply (erule bexE, rotate_tac -1, frule sym, thin_tac "a ±Nb = g xb")
 apply blast
 apply (thin_tac "xcarrier M. a = g x", thin_tac "xcarrier M. b = g x",
        thin_tac "xcarrier M. a ±Nb = g x")
 apply (erule conjE)+
 apply (rotate_tac -5)
 apply (frule sym, thin_tac "g xa = b", frule sym, thin_tac "g xaa = a",
        frule sym, thin_tac "g xb = a ±Nb", simp)
 apply (simp add:Module.mHom_add[THEN sym])
  apply (frule mzero_im_sub_ker [of L M Z f x], assumption+)
 apply (simp add:exact4_def, fold image_def,
        thin_tac "f ` carrier L = ker⇘M,Ng  g ` carrier M = ker⇘N,Zm R ez1")
 apply (frule_tac R = R and M = M in Module.module_is_ag,
       frule_tac x = xaa and y = xa in aGroup.ag_pOp_closed[of M], assumption+,
       frule_tac R = R and M = M and N = N and f = g and a = "xaa ±Mxa"
        and b = xb in Module.mHom_ker_eq, assumption+)
 apply (frule_tac c = "xaa ±Mxa ±M-aMxb" in subsetD[of "ker⇘M,Ng" 
                   "ker⇘M,Zx"], assumption+)
 apply (frule_tac a = "xaa ±Mxa" and b = xb in 
                  Module.mHom_eq_ker[of M R Z x], assumption+)
 apply (rule sym, assumption)

 apply (simp add:Module.sc_mem)
 apply (rule ballI)+
 apply (frule right_exact_surjec[of M N g z1], assumption+,
        simp add:exact4_def exact3_def)
 apply (frule_tac a = a and m = m in Module.sc_mem[of N], assumption+)
 apply (simp add:surjec_def surj_to_def, erule conjE,
        rotate_tac -1, frule sym, thin_tac "g ` carrier M = carrier N",
        simp, thin_tac "carrier N = g ` carrier M")
 apply (simp add:image_def)
 apply (rule someI2_ex, blast,
        thin_tac "xcarrier M. m = g x") 
 apply (rule someI2_ex)  
 apply (erule bexE, rotate_tac -1, frule sym, thin_tac "a sNm = g xaa")
 apply blast
 apply (thin_tac "xcarrier M. a sNm = g x")
 apply (erule conjE)+
 apply (rotate_tac -3, frule sym, thin_tac "g xa = m", simp)
 apply (simp add:Module.mHom_lin[THEN sym])
 apply (frule_tac a = a and m = xa in Module.sc_mem, assumption+)
 apply (frule_tac R = R and M = M and N = N and f = g and a = xaa and 
        b = "a sMxa" in Module.mHom_ker_eq, assumption+)
 
 apply (frule mzero_im_sub_ker [of L M Z f x], assumption+,
        simp add:exact4_def,
        thin_tac "f ` carrier L = ker⇘M,Ng  g ` carrier M = ker⇘N,Zm R ez1")
 apply (frule_tac c = "xaa ±M-aM(a sMxa)" in 
                  subsetD[of "ker⇘M,Ng " "ker⇘M,Zx"], assumption+)
 apply (rule_tac a = xaa and b = "a sMxa" in 
                  Module.mHom_eq_ker[of M R Z x], assumption+)
done
    
    (*     f    g    z1
         L → M → N → 0      exact4 L M N (Zm R e) f g z1, x ∈ mHom R M Z
               x\  | ∃x'       im f ⊆ ker x, then exists x'
                   Z        *)

lemma (in Ring) exact_im_sub_ker1:"R module L; R module M; R module N; 
      f  mHom R L M; g  mHom R M N; z1  mHom R N (Zm R e); R module Z; 
      exact4 R L f M g N z1 (Zm R e); x  mHom R M Z; 
      compos L x f = mzeromap L Z   
    compos M (λz(carrier N). x (SOME y. y  carrier M  g y = z)) g = x"
apply (frule exact_im_sub_ker [of L M N f g z1 e Z x], assumption+)
apply (frule_tac g = "(λzcarrier N. x (SOME y. y  carrier M  g y = z))" in
        Module.mHom_compos [of N R M Z g], assumption+)
 apply (rule Module.mHom_eq [of M R Z _ x], assumption+)
 apply (rule ballI)
 apply (subst compos_def, subst compose_def, simp) 
 apply (simp add:Module.mHom_mem)
 apply (thin_tac "compos M (λzcarrier N. x 
                 (SOME y. y  carrier M  g y = z)) g  mHom R M Z")
 apply (thin_tac "(λzcarrier N. x (SOME y. y  carrier M  g y = z))  
                                                               mHom R N Z")
 apply (frule right_exact_surjec [of M N g z1 e], assumption+)
 apply (simp add:exact4_def exact3_def)
 apply (frule mHom_func[of g M N],
        frule_tac a = m in mem_in_image[of g "carrier M" "carrier N"],
        assumption+)
 apply (simp add:image_def)
 apply (rule someI2_ex) apply blast
 apply (thin_tac "xcarrier M. g m = g x") apply (erule conjE)
 apply (frule mzero_im_sub_ker [of L M Z f x], assumption+)
 apply (simp add:surjec_def surj_to_def exact4_def)
 apply (frule_tac a = xa and b = m in Module.mHom_ker_eq[of M R N g],
         assumption+)
 apply (frule_tac c = "xa ±M-aMm" in subsetD[of "ker⇘M,Ng" "ker⇘M,Zx"],
                  assumption+)
 apply (rule_tac a = xa and b = m in Module.mHom_eq_ker[of M R Z x],
           assumption+)
done
 
definition
  module_iota :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme] 
                'a  'a"  ((mι⇘_ _) [92, 93]92) where
  "mι⇘RM = (λxcarrier M. x)"

lemma (in Ring) short_exact_sequence:"R module M; submodule R M N; 
 z  mHom R (Zm R e) (mdl M N); z1  mHom R (M /m N) (Zm R e)  
 exact5 R (Zm R e) z (mdl M N)(mι⇘R(mdl M N)) M (mpj M N) (M /m N) z1 (Zm R e)"
apply (simp add:exact5_def)
apply (rule conjI)
 apply (simp add:Zm_def, fold Zm_def)
 apply (frule Module.mdl_is_module [of M R N], assumption+)
 apply (simp add:ker_def, simp add:module_iota_def)
 apply (simp add:Zm_to_M_0 [of "mdl M N" "z"])
 apply (simp add:mdl_def, fold mdl_def)
 apply (rule equalityI)
 apply simp
 apply (simp add:Module.submodule_inc_0)
 apply (rule subsetI)
 apply (simp add:CollectI)
apply (rule conjI)
 apply (simp add:module_iota_def)
 apply (simp add:mdl_def, fold mdl_def) 
  apply (simp add:Module.mker_of_mpj[THEN sym])
 apply (frule Module.qmodule_module [of M R N], assumption)
apply (subst M_to_Z_0 [of "M /m N" z1 e], assumption+)
 apply (frule Module.mpj_surjec [of M R N], assumption+)
 apply (simp add:surjec_def surj_to_def)
done

lemma (in Ring) rexact4_lexact4_HOM:"R module M1; R module M2; R module M3;
      f  mHom R M1 M2; g  mHom R M2 M3; z1  mHom R M3 (Zm R e); 
      exact4 R M1 f M2 g M3 z1 (Zm R e)  
 N. R module N  
 exact4 R (HOM⇘R(Zm R e) N) (sup_sharp R M3 (Zm R e) N z1) (HOM⇘RM3 N) 
(sup_sharp R M2 M3 N g) (HOM⇘RM2 N) (sup_sharp R M1 M2 N f) (HOM⇘RM1 N)"  

 (*              f     g    z1
             M1 → M2 → M3 → (Zm R e)               
                         |
                         N                     *)
apply (rule allI) apply (rule impI)
apply (subst exact4_def)
apply (rule conjI)
 apply (cut_tac Zm_Module [of e])
 apply (subst HOM_carrier [of  "Zm R e"])
 apply (simp add:mHom_Z_M)
 apply (simp add:sup_sharp_def)
 apply (simp add:mzeromap_mHom)
 apply (simp add:ker_def)
 apply (simp add:HOM_def)
 apply (rule equalityI)
 apply (rule subsetI)
 apply simp
 apply (frule_tac N = N in mzeromap_mHom [of "Zm R e"], assumption+)
   thm Module.mHom_compos[of "Zm R e" R M3 _ z1]
 apply (frule_tac N = N and g = "mzeromap (Zm R e) N" in 
                 Module.mHom_compos[of  "Zm R e" R M3 _ z1], assumption+,
        simp)  
 apply (frule_tac N = N in mzeromap_mHom [of M2], assumption+)
 apply (rule_tac N = N and f = "compos M2 (compos M3 (mzeromap (Zm R e) N) z1) g" and g = "mzeromap M2 N" in Module.mHom_eq [of M2 _ ], assumption+)
 apply (rule Module.mHom_compos, assumption+)
 apply (rule ballI) 
 apply (simp add:compos_def mzeromap_def compose_def)
 apply (simp add:Module.mHom_mem)+
 apply (rule subsetI, simp, erule conjE, simp)
 apply (frule_tac N = N in mzeromap_mHom [of "Zm R e"], assumption+)
 apply (frule_tac N = N and g = "mzeromap (Zm R e) N" in 
        Module.mHom_compos [of "Zm R e" R M3  _ z1], assumption+)
 apply (rule_tac  N = N and f = x and 
        g = "compos M3 (mzeromap (Zm R e) N) z1" in Module.mHom_eq[of M3 _], 
        assumption+)
 apply (rule ballI)
 apply (subst compos_def, subst compose_def)
 apply (subst mzeromap_def) apply (simp add:Module.mHom_mem)
 apply (simp add:exact4_def)
 apply (subgoal_tac "exact3 R M2 g  M3 z1 (Zm R e)")
 prefer 2 apply (simp add:exact3_def)
 apply (frule right_exact_surjec [of M2 M3 g z1], assumption+)
 apply (simp add:surjec_def surj_to_def, erule conjE)
 apply (thin_tac "f ` carrier M1 = ker⇘M2,M3g",
        thin_tac "g  aHom M2 M3  ker⇘M3,Zm R ez1 = carrier M3",
        rotate_tac -1, frule sym, thin_tac "g ` carrier M2 = carrier M3",
        simp add:image_def, fold image_def)
 apply (erule bexE, simp) apply (simp add:compos_def compose_def mzeromap_def)
 apply (frule_tac f = "λxacarrier M2. x (g xa)" and g = "λxcarrier M2. 𝟬N⇙"
        and x = xa in eq_fun_eq_val, 
        thin_tac "(λxacarrier M2. x (g xa)) = (λxcarrier M2. 𝟬N)",
        simp)
(* apply (erule conjE) apply (simp add:surj_to_def)
 apply (simp add:image_def)
 apply (subgoal_tac "m ∈ {y. ∃x∈carrier M2. y = g x}")
 apply (thin_tac "{y. ∃x∈carrier M2. y = g x} = carrier M3")
 prefer 2 apply simp apply simp
 apply (subgoal_tac "∀y∈carrier M2. m = g y ⟶ x m = 0N")
 apply blast
 apply (rule ballI) apply (rule impI) apply simp
 apply (thin_tac "compos M3 (mzeromap (Zm R e) N) z1 ∈ mHom R M3 N")
 apply (thin_tac "∃x∈carrier M2. g y = g x") apply (thin_tac "m = g y")
 apply (simp add:compos_def compose_def)
 apply (subgoal_tac "(λxa∈carrier M2. x (g xa)) y = x ( g y)")
 apply (simp add:mzeromap_def) apply (thin_tac "(λxa∈carrier M2. x (g xa)) = mzeromap M2 N")
 apply simp  *)
apply (rule equalityI)
 apply (rule subsetI)
 apply (simp add:image_def)
 apply (simp add:ker_def)
 apply (simp add:HOM_carrier)
 apply (erule bexE)
 apply (frule_tac L = N and f = xa in Module.sup_sharp_homTr[of M2 R M3 _ g],
         assumption+, simp)
  thm Module.sup_sharp_homTr[of M1 R M2 _ f]
 apply (frule_tac L = N and f = "sup_sharp R M2 M3 N g xa" in 
        Module.sup_sharp_homTr[of M1 R M2 _ f], assumption+)
 apply (simp add:HOM_def)
 apply (frule_tac N = N in  mzeromap_mHom[of M1], assumption)
 apply (rule Module.mHom_eq, assumption+)
apply (rule ballI)
 apply (subst sup_sharp_def) 
 apply simp
 apply (subst compos_def, subst compose_def)
 apply (subst sup_sharp_def, simp) 
 apply (subst compos_def, subst compose_def, simp) 
 apply (simp add:Module.mHom_mem)
 apply (subgoal_tac "exact3 R M1 f M2 g M3")
 prefer 2 apply (simp add:exact4_def exact3_def)
 apply (frule_tac exact3_comp_0 [of M1 M2 M3 f g], assumption+)
 apply (frule_tac f = "compos M1 g f" and g = "mzeromap M1 M3"
        and x = m in eq_fun_eq_val, 
        thin_tac "compos M1 g f = mzeromap M1 M3", 
        simp add:compos_def compose_def mzeromap_def)
 apply (simp add:Module.mHom_0)
apply (rule subsetI)
 apply (simp add:ker_def)
 apply (erule conjE)
 apply (simp add:HOM_carrier)
 apply (simp add:HOM_def)
 apply (simp add:sup_sharp_def)
 apply (simp add:image_def)
 apply (frule_tac Z = N and x = x in  exact_im_sub_ker1 [of M1 M2 M3 f g z1 e],
        assumption+)
 apply (rotate_tac -1) apply (frule sym)
 apply (thin_tac "compos M2 (λzcarrier M3. x 
                      (SOME y. y  carrier M2  g y = z)) g = x")
 apply (frule_tac Z = N and x = x in exact_im_sub_ker [of M1 M2 M3 f g z1 e], 
        assumption+)
 apply blast
done

lemma exact_HOM_exactTr:"Ring (R::('r, 'm1) Ring_scheme); f  mHom R M1 M2;
      g  mHom R M2 M3; z1  mHom R M3 (Zm R e); R module NV;
     (N::('a, 'r, 'm) Module_scheme). R module N 
      exact4 R (HOM⇘R(Zm R e) N)(sup_sharp R M3 (Zm R e) N z1)
      (HOM⇘RM3 N) (sup_sharp R M2 M3 N g) (HOM⇘RM2 N) (sup_sharp R M1 M2 N f)
      (HOM⇘RM1 N); R module (L::('a, 'r, 'm) Module_scheme)  
  exact4 R (HOM⇘R(Zm R e) L) (sup_sharp R M3 (Zm R e) L z1)
 (HOM⇘RM3 L) (sup_sharp R M2 M3 L g) (HOM⇘RM2 L) (sup_sharp R M1 M2 L f)
 (HOM⇘RM1 L)"
apply simp
done 

(*
lemma exact_HOM_exact:"⟦ring (R:: ('r, 'm) RingType_scheme); R module M1; R module M2; R module M3; f ∈ mHom R M1 M2; g ∈ mHom R M2 M3; z1 ∈ mHom R M3 (Zm R e); R module (NV::('g, 'r) ModuleType); ∀(N::('g, 'r) ModuleType). R module N ⟶ exact4 R (HOMR (Zm R e) N) (HOMR M3 N) (HOMR M2 N) (HOMR M1 N) (sup_sharp R M3 (Zm R e) N z1) (sup_sharp R M2 M3 N g) (sup_sharp R M1 M2 N f) ⟧ ⟹ exact4 R M1 M2 M3 (Zm R e) f g z1"
apply (subst exact4_def)
apply (subgoal_tac "surjecM2,M3 g")
 apply (frule surjec_right_exact [of "R" "M2" "M3" "g" "z1" "e"], assumption+)
 apply (simp add:exact3_def)
prefer 2
apply (frule img_set_submodule [of "R" "M2" "M3" "g"], assumption+)
apply (frule qmodule_module [of "R" "M3" "g ` carrier M2"], assumption+)
apply (subgoal_tac "exact4 R (HOMR (Zm R e) (M3 /m (g ` carrier M2))) (HOMR M3 (M3 /m (g ` carrier M2))) (HOMR M2 (M3 /m (g ` carrier M2))) (HOMR M1 (M3 /m (g ` carrier M2))) (sup_sharp R M3 (Zm R e) (M3 /m (g ` carrier M2)) z1) (sup_sharp R M2 M3 (M3 /m (g ` carrier M2)) g) (sup_sharp R M1 M2 (M3 /m (g ` carrier M2)) f)")
prefer 2 
 apply (thin_tac "submodule R M3 (g ` carrier M2)")
 apply (thin_tac "ring R") apply (thin_tac "R module M1")
 apply (thin_tac " R module M2") apply (thin_tac "R module M3")
apply blast ML
apply (rule allI)
 apply (rule impI)
 apply simp     ????????????
*)
   
lemma lexact4_rexact4_HOM:"Ring R; R module M1; R module M2; R module M3;
f  mHom R M1 M2; g  mHom R M2 M3; z  mHom R (Zm R e) M1; 
exact4 R (Zm R e) z M1 f M2 g M3   
N. R module N  exact4 R (HOM⇘RN (Zm R e)) (sub_sharp R N (Zm R e) M1 z)
    (HOM⇘RN M1) (sub_sharp R N M1 M2 f) (HOM⇘RN M2) (sub_sharp R N M2 M3 g)
    (HOM⇘RN M3)"  

 (*       
                        N
                     z  |   f     g    
            (Zm R e) → M1 → M2 → M3  *)
apply (rule allI) apply (rule impI)
apply (subst exact4_def)
apply (rule conjI)
 apply (rule equalityI)
 apply (rule subsetI)  
 apply (simp add:image_def)
 apply (simp add:HOM_def) apply (fold HOM_def)
 apply (erule bexE)
 apply (simp add:ker_def) apply (simp add:HOM_def)
 apply (simp add:sub_sharp_def)
 apply (cut_tac Ring.Zm_Module [of R e])
 apply (simp add:Module.mHom_compos)
 apply (frule_tac L = N and f = xa in 
                  Module.mHom_compos[of "Zm R e" R _ M1 _ z], assumption+) 
 apply (frule_tac L = N and f = "compos N z xa" in 
                             Module.mHom_compos[of M1 R _ M2 _ f], assumption+)
 apply (frule_tac M = N in Ring.mzeromap_mHom [of R _  M2], assumption+)
 apply (rule Module.mHom_eq, assumption+)
 apply (rule ballI)
 apply (simp add:mzeromap_def) apply (simp add:compos_def compose_def)
  apply (frule_tac M = N and f = xa and m = m in Module.mHom_mem [of _ R 
         "Zm R e"], assumption+)
 apply (simp add:exact4_def)
 apply (frule conjunct1) 
 apply (thin_tac "z ` carrier (Zm R e) = ker⇘M1,M2f")
 apply (erule conjE, thin_tac "f ` carrier M1 = ker⇘M2,M3g")
 apply (frule Ring.mHom_func[of R z "(Zm R e)" M1], assumption)
 apply (frule_tac a = "xa m" in mem_in_image
          [of z "carrier (Zm R e)" "carrier M1"], assumption+, simp)
 apply (simp add:ker_def, assumption) 

apply (rule subsetI) 
 apply (simp add:ker_def) apply (simp add:HOM_def)
 apply (erule conjE)
 apply (frule_tac M = N in Ring.mHom_to_zero [of "R" _ "e"], assumption+)
 apply simp apply (simp add:sub_sharp_def)
 apply (simp add:exact4_def) apply (frule conjunct1)
 apply (thin_tac "z ` carrier (Zm R e) = ker⇘M1,M2f 
                                          f ` carrier M1 = ker⇘M2,M3g")
 apply (simp add:Zm_def, fold Zm_def)
 apply (frule_tac L = N and f = x in Ring.mzero_im_sub_ker [of R _ M1 M2 _ f], assumption+) apply (rotate_tac -2) apply (frule sym) 
 apply (thin_tac "{z e} = ker⇘M1,M2f", simp)
 apply (subgoal_tac "mzeromap N (Zm R e)  mHom R N (Zm R e)")
 prefer 2  apply simp
 apply (frule Ring.Zm_Module[of R e]) 
 apply (frule_tac L = N and f = "mzeromap N (Zm R e)" in
        Module.mHom_compos [of "Zm R e" R _  M1 _ z], assumption+)
 apply (rule Module.mHom_eq, assumption+) apply (rule ballI)
 apply (simp add:compos_def compose_def mzeromap_def)
 apply (simp add:Zm_def, fold Zm_def)
 apply (frule_tac M = N and f = x in Ring.mHom_func[of R _ _ M1], assumption+)
 apply (frule_tac f = x and A = "carrier N" and B = "carrier M1" and a = m in
         mem_in_image, assumption+)
 apply (frule_tac c = "x m" and A = "x ` carrier N" and B = "{z e}" in 
                      subsetD, assumption+)  apply simp

apply (simp add:image_def ker_def HOM_def)
apply (rule equalityI)
 apply (rule subsetI)
 apply (simp, erule bexE)
 apply (simp add:sub_sharp_def)
 apply (frule_tac L = N and f = xa in Module.mHom_compos [of M1 R _ M2 _ "f"],
                              assumption+) apply simp
 apply (frule_tac L = N and f = "compos N f xa" in Module.mHom_compos [of M2 R
        _ M3 _ g], assumption+)
 apply (rule Module.mHom_eq, assumption+) 
 apply (simp add:Ring.mzeromap_mHom)
 apply (rule ballI)
 apply (simp add:compos_def compose_def mzeromap_def)
 apply (thin_tac " x = (λxcarrier N. f (xa x))")
 apply (thin_tac "(λxcarrier N. f (xa x))  mHom R N M2")
 apply (thin_tac "(λxcarrier N. g (if x  carrier N then f (xa x) else undefined))  mHom R N M3")
 apply (frule_tac M = N and f = xa and m = m in Module.mHom_mem [of _ R M1], 
        assumption+) 
 apply (simp add:exact4_def) apply (frule conjunct2)
 apply (thin_tac "z ` carrier (Zm R e) = ker⇘M1,M2f 
                                     f ` carrier M1 = ker⇘M2,M3g")
 apply (frule Ring.mHom_func[of R f M1 M2], assumption+,
        frule_tac f = f and A = "carrier M1" and B = "carrier M2" and 
        a = "xa m" in  mem_in_image, assumption+, simp)
 apply (simp add:ker_def)

 apply (rule subsetI)
 apply simp apply (erule conjE)
 apply (simp add:sub_sharp_def)
 apply (frule_tac L = N and f = x in Ring.mzero_im_sub_ker [of R _ M2 M3 _ g],
        assumption+)
 apply (simp add:exact4_def)
 apply (frule conjunct2) apply (rotate_tac -1) apply (frule sym)
 apply (thin_tac "f ` carrier M1 = ker⇘M2,M3g")
 apply simp apply (thin_tac "ker⇘M2,M3g = f ` carrier M1")
 apply (frule Ring.left_exact_injec[of "R" "M1" "M2" "z" "e" "f"], assumption+)
 apply (simp add:exact3_def exact4_def) 
 apply (frule_tac N = N and x = x in Ring.injec_mHom_image[of R _ M1 M2 _ f], 
        assumption+)
 apply (erule conjE) apply (rotate_tac -1) apply (frule sym)
 apply (thin_tac "compos N f (λncarrier N. SOME m. m  carrier M1  
                                                        x n = f m) = x")
 apply blast
done

(* Now, we cannot prove following because of type problem
lemma l_exact4_HOM_lexact4:"⟦ring R; R module M1; R module M2; R module M3; f ∈ mHom R M1 M2;
   g ∈ mHom R M2 M3; z ∈ mHom R (Zm R e) M1;
   ∀N. R module N ⟶
       exact4 R (HOMR N Zm R e) (HOMR N M1) (HOMR N M2) (HOMR N M3)
        (sub_sharp R N (Zm R e) M1 z) (sub_sharp R N M1 M2 f)
        (sub_sharp R N M2 M3 g)⟧
⟹ exact4 R (Zm R e) M1 M2 M3 z f g" *)

(*
lemma exact_coker:"⟦ring R; R module M1; R module M2; R module M3; z ∈ mHom R (Zm R e) M1; f ∈ mHom R M1 M2; g ∈ mHom R M2 M3; z1 ∈ mHom R M3 (Zm R ee);  R module N1; R module N2; R module N3; h ∈ mHom R N1 N2; i ∈ mHom R N2 N3; exact5 (Zm R e) M1 M2 M3 (Zm R ee) z f g z1: exact5 (Zm R u) N1 N2 N3 (Zm R uu) z h i z1: f1 ∈ mHom R M1 N1; f2 ∈ mHom R M2 N2; f3 ∈ mHom R M3 N3; compos m1 f2 f = compos M1 h f1; compos M2 f3 g = compos M2 i f2⟧ ⟹ exact8 (Zm R e) (mdl M1 (kerM1,N1 f1)) (mdl M2 (kerM2,N2 f2)) (mdl M3 (kerM3,N3 f3)) (N1 /m (f1 ` (carrier M1))) (N2 /m (f2 ` (carrier M2))) (N3 /m (f3 ` (carrier M3))) z f g zz hh ii zz1 "


*)

section "Tensor product"

definition
  prod_carr :: "[('a, 'r, 'm) Module_scheme, ('b, 'r, 'm) Module_scheme]
    ('a * 'b) set" (infixl ×c 100) where
  "M ×c N = carrier M × carrier N"

definition
  bilinear_map :: "['a * 'b  'c, ('r, 'm) Ring_scheme, 
    ('a, 'r, 'm1) Module_scheme, ('b, 'r, 'm1) Module_scheme, 
    ('c, 'r, 'm1) Module_scheme]  bool" where
  "bilinear_map f R M1 M2 N  f  M1 ×c M2  carrier N  
                             f  extensional (M1 ×c M2)  
   (x1  carrier M1. x2  carrier M1. 
         ycarrier M2.(f (x1 ±M1x2, y) = f (x1, y) ±N(f (x2, y))))  
   (xcarrier M1. y1carrier M2. 
         y2carrier M2. f (x, y1 ±M2y2) = f (x, y1) ±N(f (x, y2)))  
   (xcarrier M1. ycarrier M2. 
         rcarrier R. f (r sM1x, y) = r sN(f (x, y))  
                       f (x, r sM2y) = r sN(f (x, y)))"

lemma (in Ring) prod_carr_mem:"R module M; R module N; m  carrier M; 
       n  carrier N  (m, n)  M ×c N" 
by (simp add:prod_carr_def)

lemma (in Ring) bilinear_func:"bilinear_map f R M N Z 
                  f  M ×c N  carrier Z"
by (simp add:bilinear_map_def)

lemma (in Ring) bilinear_mem:"R module M1; R module M2; R module N; 
      m1  carrier M1; m2  carrier M2; bilinear_map f R M1 M2 N  
      f (m1, m2)  carrier N" 
apply (simp add:bilinear_map_def) apply (erule conjE)+
apply (rule funcset_mem [of "f" "M1 ×c M2" "carrier N"], assumption+)
apply (simp add:prod_carr_def)
done

lemma (in Ring) bilinear_l_add:"R module M1; R module M2; R module N; 
       m11  carrier M1; m12  carrier M1; m2  carrier M2; 
       bilinear_map f R M1 M2 N  
       f (m11 ±M1m12, m2) = f (m11, m2) ±N(f (m12, m2))" 
apply (simp add:bilinear_map_def) 
done

lemma (in Ring) bilinear_l_add1:"R module M1; R module M2; R module N; 
       m11  carrier M1; m12  carrier M1; m2  carrier M2; 
       bilinear_map f R M1 M2 N  
       f (m11 ±M1m12, m2) ±N-aN(f (m11, m2) ±N(f (m12, m2))) = 𝟬N⇙"
apply (frule Module.module_is_ag[of N],
       frule Module.module_is_ag[of M1],
       subst aGroup.ag_eq_diffzero[of N, THEN sym], assumption+,
       frule_tac x = m11 and y = m12 in aGroup.ag_pOp_closed, assumption+)
 apply (simp add:bilinear_mem,
       rule aGroup.ag_pOp_closed, assumption+)
       apply ((simp add:bilinear_mem)+, simp add:bilinear_l_add)
done
 
lemma (in Ring) bilinear_r_add:"R module M1; R module M2; R module N; 
      m  carrier M1; m21  carrier M2; m22  carrier M2; 
      bilinear_map f R M1 M2 N  
      f (m, m21 ±M2m22) = f (m, m21) ±N(f (m, m22))" 
apply (simp add:bilinear_map_def) 
done

lemma (in Ring) bilinear_r_add1:"R module M1; R module M2; R module N; 
       m  carrier M1; m21  carrier M2; m22  carrier M2; 
       bilinear_map f R M1 M2 N  
       f (m, m21 ±M2m22) ±N-aN(f (m, m21) ±N(f (m, m22))) = 𝟬N⇙"
apply (frule Module.module_is_ag[of N],
       frule Module.module_is_ag[of M2],
       subst aGroup.ag_eq_diffzero[of N, THEN sym], assumption+,
       frule_tac x = m21 and y = m22 in aGroup.ag_pOp_closed, assumption+)
 apply (simp add:bilinear_mem,
       rule aGroup.ag_pOp_closed, assumption+)
       apply ((simp add:bilinear_mem)+, simp add:bilinear_r_add)
done

lemma (in Ring) bilinear_l_lin:"R module M1; R module M2; R module N; 
      m1  carrier M1; m2  carrier M2; r  carrier R; 
      bilinear_map f R M1 M2 N  f (r sM1m1, m2) = r sN(f (m1, m2))"
by (simp add:bilinear_map_def)

lemma (in Ring) bilinear_l_lin1:"R module M1; R module M2; R module N; 
      m1  carrier M1; m2  carrier M2; r  carrier R; 
      bilinear_map f R M1 M2 N  
         f (r sM1m1, m2) ±N-aN(r sN(f (m1, m2))) = 𝟬N⇙"
apply (frule Module.module_is_ag[of N],
       subst aGroup.ag_eq_diffzero[of N, THEN sym], assumption+,
       frule_tac a = r and m = m1 in Module.sc_mem[of M1 R], assumption+,
       simp add:bilinear_mem)
 apply (rule Module.sc_mem, assumption+, simp add:bilinear_mem,
        simp add:bilinear_l_lin)
done

lemma (in Ring) bilinear_r_lin:"R module M1; R module M2; R module N; 
      m1  carrier M1; m2  carrier M2; r  carrier R; 
      bilinear_map f R M1 M2 N  f (m1, r sM2m2) = r sN(f (m1, m2))"
apply (simp add:bilinear_map_def)
done

lemma (in Ring) bilinear_r_lin1:"R module M1; R module M2; R module N; 
      m1  carrier M1; m2  carrier M2; r  carrier R; 
      bilinear_map f R M1 M2 N  
      f (m1, r sM2m2)  ±N-aN(r sN(f (m1, m2))) = 𝟬N⇙ "
apply (frule Module.module_is_ag[of N],
       subst aGroup.ag_eq_diffzero[of N, THEN sym], assumption+,
       frule_tac a = r and m = m2 in Module.sc_mem[of M2 R], assumption+,
       simp add:bilinear_mem)
 apply (rule Module.sc_mem, assumption+, simp add:bilinear_mem,
        simp add:bilinear_r_lin)
done

lemma (in Ring) bilinear_l_0:"R module M1; R module M2; R module N; 
      m2  carrier M2; bilinear_map f R M1 M2 N  f (𝟬M1, m2) = 𝟬N⇙"
apply (frule Module.module_inc_zero [of M1 R])
apply (frule bilinear_l_add [of M1 M2 N "𝟬M1⇙" "𝟬M1⇙" "m2" "f"], assumption+) 
 apply (frule Module.module_is_ag [of M1 R], simp add:aGroup.ag_l_zero)
 apply (frule bilinear_mem [of M1 M2 N "𝟬M1⇙" m2 f], assumption+)
 apply (frule Module.module_is_ag [of N R])
 apply (frule aGroup.ag_eq_sol1 [of "N" "f (𝟬M1, m2)" "f (𝟬M1, m2)"
        "f (𝟬M1, m2)"], assumption+)
 apply (rule sym, assumption+)
apply (simp add:aGroup.ag_l_inv1)
done

lemma (in Ring) bilinear_r_0:"R module M1; R module M2; R module N; 
      m1  carrier M1; bilinear_map f R M1 M2 N  f (m1, 𝟬M2) = 𝟬N⇙"
apply (frule Module.module_inc_zero [of M2 R])
apply (frule bilinear_r_add [of M1 M2 N m1 "𝟬M2⇙" "𝟬M2⇙" "f"], assumption+) 
 apply (frule Module.module_is_ag [of M2 R])
 apply (simp add:aGroup.ag_l_zero)
 apply (frule bilinear_mem [of M1 M2 N m1 "𝟬M2⇙" "f"], assumption+)
 apply (frule Module.module_is_ag [of N R])
 apply (frule aGroup.ag_eq_sol1 [of N "f (m1, 𝟬M2)" "f (m1, 𝟬M2)" 
       "f (m1, 𝟬M2)"], assumption+)
 apply (rule sym, assumption+)
apply (simp add:aGroup.ag_l_inv1)
done

definition
  universal_property :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
                     ('b, 'r, 'm1) Module_scheme, ('c, 'r, 'm1) Module_scheme, 
                     'a * 'b 'c]   bool" where
  "universal_property (R::('r, 'm) Ring_scheme) (M::('a, 'r, 'm1) Module_scheme)
    (N:: ('b, 'r, 'm1) Module_scheme) (MN::('c, 'r, 'm1) Module_scheme) 
    (f:: 'a * 'b  'c)  (bilinear_map f R M N MN)  
    ((Z :: ('c, 'r, 'm1) Module_scheme). (g :: 'a * 'b  'c). (R module Z)  
    (bilinear_map g R M N Z)   ((∃!h. (h  mHom R MN Z)  
                                        (compose (M ×c N) h f = g))))" 

(* universal_property R MV M N MN f *)

lemma tensor_prod_uniqueTr:"Ring R; R module (M::('a, 'r, 'm1) Module_scheme); 
      R module (N:: ('b, 'r, 'm1) Module_scheme); 
      R module (MN:: ('c, 'r, 'm1) Module_scheme); 
      R module (MN1::('c, 'r, 'm1) Module_scheme); 
      universal_property R M N MN f; universal_property R M N MN1 g 
      ∃!k. k  mHom R MN1 MN  compose (M ×c N) k g = f" 
apply (simp add: universal_property_def [of  _ _ _ _ "f"])
 apply (frule conjunct1) apply (fold universal_property_def)
 apply (simp add:universal_property_def [of _ _ _ _ "g"])
done

lemma tensor_prod_unique:"Ring (R:: ('r, 'm) Ring_scheme); 
      R module (M :: ('a, 'r, 'm1) Module_scheme); 
      R module (N:: ('b, 'r, 'm1) Module_scheme); 
      R module (MN:: ('c, 'r, 'm1) Module_scheme); 
      R module (MN1::('c, 'r, 'm1) Module_scheme); 
      universal_property R M N MN f; universal_property R M N MN1 g  
      MN ≅⇘RMN1"
apply (frule tensor_prod_uniqueTr[of R M N MN MN1 f g], assumption+,
       erule ex1E,
       thin_tac "y. y  mHom R MN1 MN  compose (M ×c N) y g = f  y = k",
       frule tensor_prod_uniqueTr [of R M N MN1 MN g f], assumption+)
apply (erule ex1E,
       thin_tac "y. y  mHom R MN MN1  compose (M ×c N) y f = g  y = ka",
       (erule conjE)+,
       rename_tac k h,
       frule_tac f = k in Ring.mHom_func[of R _ MN1 MN], assumption)
apply (subgoal_tac "f  (M ×c N)  (carrier MN)")
 prefer 2 apply (simp add:universal_property_def bilinear_map_def)
apply (frule_tac f = h in Ring.mHom_func[of R _ MN MN1], assumption,
        frule_tac  g = h and h = k in compose_assoc [of "f" "M ×c N" "carrier MN"], simp)
apply (subgoal_tac "g  (M ×c N)  (carrier MN1)")
 prefer 2 apply (simp add:universal_property_def bilinear_map_def)
apply (frule_tac g = k and h = h in compose_assoc [of "g" "M ×c N" "carrier MN1"], simp)
apply (subgoal_tac "compose (M ×c N) (mId⇘MN) f = f")
 prefer 2 
 apply (frule Module.mId_mHom [of MN R],
        frule_tac f = "mId⇘MN⇙" in Ring.mHom_func[of R _ MN MN], assumption,
        frule  composition [of f "M ×c N" "carrier MN" "mId⇘MN⇙" "carrier MN"],
         assumption+,
        rule funcset_eq [of _ "M ×c N"] )
   apply (simp add:compose_def restrict_def extensional_def,
        simp add:universal_property_def bilinear_map_def)
 apply (simp add:compose_def mId_def funcset_mem del:Pi_I')
apply (rotate_tac -4)
apply (frule sym,
        thin_tac "f = compose (M ×c N) (compose (carrier MN) k h) f")
apply (subgoal_tac "(compose (carrier MN) k h) = mId⇘MN⇙")
 apply (subgoal_tac "(compose (carrier MN1) h k) = (mId⇘MN1)") 
  apply (simp add:misomorphic_def)
  apply (frule_tac f = h and g = k in Module.mHom_mId_bijec [of MN R MN1],
            assumption+)
  apply blast  (* compose (carrier MN1) h k = mIdMN1 *)
 apply (subgoal_tac "compose (M ×c N) (mId⇘MN1) g = g")
  prefer 2 
  apply (frule Module.mId_mHom [of MN1 R])
  apply (subgoal_tac "mId⇘MN1 carrier MN1  carrier MN1")
   prefer 2 apply (simp add:mHom_def aHom_def)
  apply (frule  composition [of "g" "M ×c N" "carrier MN1" "mId⇘MN1⇙" 
      "carrier MN1"], assumption+,
      rule funcset_eq [of _ "M ×c N"],
      simp add:compose_def restrict_def extensional_def,
      simp add:universal_property_def bilinear_map_def)
  apply (simp add:compose_def mId_def,
       simp add:funcset_mem del:Pi_I',
       frule sym,
       thin_tac "g = compose (M ×c N) (compose (carrier MN1) h k) g",
       frule tensor_prod_uniqueTr [of R M N MN1 MN1 g g], assumption+)
 apply (erule ex1E,
        frule Module.mId_mHom [of MN1 R])
 apply (subgoal_tac "mId⇘MN1= ka") prefer 2 
  apply (thin_tac "compose (M ×c N) k g = f",
         thin_tac "compose (M ×c N) h f = g",
         thin_tac "compose (M ×c N) (compose (carrier MN) k h) f = f",
         thin_tac "compose (carrier MN) k h = mId⇘MN⇙",
         thin_tac "ka  mHom R MN1 MN1  compose (M ×c N) ka g = g",
         thin_tac "compose (M ×c N) (compose (carrier MN1) h k) g = g",
      blast)
 apply (subgoal_tac "compose (carrier MN1) h k = ka",
        thin_tac "k  mHom R MN1 MN",
        thin_tac "compose (M ×c N) k g = f", 
        thin_tac "compose (M ×c N) h f = g",
        thin_tac "k  carrier MN1  carrier MN",
        thin_tac "f  M ×c N  carrier MN",
        thin_tac "h  carrier MN  carrier MN1",
        thin_tac "compose (M ×c N) (compose (carrier MN) k h) f = f",
        thin_tac "compose (carrier MN) k h = mId⇘MN⇙",
        thin_tac "compose (M ×c N) (mId⇘MN1) g = g",
        thin_tac "compose (M ×c N) (compose (carrier MN1) h k) g = g",
        thin_tac "ka  mHom R MN1 MN1  compose (M ×c N) ka g = g",
        thin_tac "y. y  mHom R MN1 MN1  
                               compose (M ×c N) y g = g  y = ka") 
  apply simp
 apply (thin_tac "mId⇘MN1 mHom R MN1 MN1",
        thin_tac "compose (M ×c N) (mId⇘MN1) g = g",
        thin_tac "ka  mHom R MN1 MN1  compose (M ×c N) ka g = g",
        thin_tac "mId⇘MN1= ka")
 apply (subgoal_tac "(compose (carrier MN1) h k)  mHom R MN1 MN1")
  apply simp
 apply (thin_tac "y. y  mHom R MN1 MN1  
             compose (M ×c N) y g = g  y = ka") 

 apply (frule_tac f = k and g = h in  Module.mHom_compos[of MN R MN1 MN1], 
                        assumption+)
 apply (simp add:compos_def)  (** compose (carrier MN1) h k = mIdMN1 done **)
  (* compose (carrier MN) k h = mIdMN *)
apply (frule Module.mId_mHom [of MN R])
apply (subgoal_tac "compose (M ×c N) (mId⇘MN) f = f")
 prefer 2
 apply (frule_tac f = "mId⇘MN⇙" in Ring.mHom_func[of R _ MN MN], assumption)
 apply (frule  composition [of "f" "M ×c N" "carrier MN" "mId⇘MN⇙" "carrier MN"],
        assumption+)
apply (frule tensor_prod_uniqueTr [of "R" "M" "N" "MN" "MN" "f" "f"], 
       assumption+)
apply (erule ex1E)
apply (subgoal_tac "mId⇘MN= ka") prefer 2 
 apply (thin_tac "compose (M ×c N) k g = f",
         thin_tac "compose (M ×c N) h f = g",
         thin_tac "ka  mHom R MN MN  compose (M ×c N) ka f = f")
 apply blast
apply (rotate_tac -1) apply (frule sym, thin_tac "mId⇘MN= ka")
apply (thin_tac "compose (M ×c N) k g = f",
        thin_tac "compose (M ×c N) h f = g",
        thin_tac "k  carrier MN1  carrier MN",
        thin_tac "f  M ×c N  carrier MN",
        thin_tac "h  carrier MN  carrier MN1",
        thin_tac "compose (M ×c N) (mId⇘MN) f = f")
apply (subgoal_tac "(compose (carrier MN) k h)  mHom R MN MN")
 apply simp
apply (frule_tac f = h and g = k in Module.mHom_compos[of MN1 R MN "MN"], 
                        assumption+)
apply (metis compos_def)
done

chapter "Construction of an abelian group"

section "Free generated abelian group I, direct sum and direct product 2"

(** Make a free generated abelian group **)

definition (* for abelian groups, modules *) 
  bpp :: "['a  'a  'a, 'a, 'a]  'a" where
  "bpp f a b = f a b"

definition
  ipp :: "['a  'a, 'a]  'a"  ((_-/ _) [64,65]64) where
  "i- a == i a"

definition (* for modules *)
  sop :: "['r  'a  'a, 'r, 'a]  'a" where
  "sop s r a = s r a"

abbreviation
  BOP :: "['a, 'a  'a  'a, 'a]  'a"
    ((3_/ _+/ _) [62,62,63]62) where
  "a f+ b == bpp f a b"

abbreviation
  SOP :: "['r, 'r  'a  'a, 'a]  'a"
    ((3_/ _ _) [68,68,69]68) where
  "r s a == sop s r a"

definition
 minus_set :: "['a  'a, 'a set]  'a set" where
 "minus_set i A = {x. yA. x = i- y}"

definition
 pm_set :: "['a  'a, 'a set]  'a set" where
 "pm_set i A = A  (minus_set i A)"

definition
  s_set :: "[('r, 'm) Ring_scheme, 'r  'a  'a, 'a set]  'a set" where
  "s_set R s A = {x. rcarrier R. aA. x = r s a}  A"

primrec add_set :: "['a  'a  'a, 'a set]  nat  'a set"
where
  add_set_0 : "add_set f A 0 = A"
| add_set_Suc: "add_set f A (Suc n) =
                      {x. s (add_set f A n). t A. x = s f+ t}"

definition
  aug_pm_set :: "['a, 'a  'a, 'a set]  'a set" where
  "aug_pm_set z i A = {z}  A  (minus_set i A)"

definition
  addition_set :: "['a  'a  'a, 'a set]  'a set" where
  "addition_set f A = {add_set f A n | n. (0::nat) n}"

definition
  assoc_bpp :: "['a set, 'a  'a  'a]  bool" where
  "assoc_bpp A f 
    (a(addition_set f A). b(addition_set f A). c(addition_set f A). (a f+ b) f+ c = a f+ (b f+ c))"

definition
  commute_bpp :: "['a  'a  'a, 'a set]  bool" where
  "commute_bpp f A  (xaddition_set f A. yaddition_set f A. x f+ y = y f+ x)"

definition
  zeroA :: "['a, 'a  'a, 'a  'a  'a, 'a set]  'a  bool" where
  "zeroA z i f A z1  (x  addition_set f (aug_pm_set z i A). z1 f+ x = x)"

definition
  inv_ipp :: "['a, 'a  'a, 'a  'a  'a, 'a set]  bool" where
  "inv_ipp z i f A  (aaddition_set f (aug_pm_set z i A). zeroA z i f A ((i- a) f+ a))"

definition
  ipp_cond1 :: "['a set, 'a  'a]  bool" where
  "ipp_cond1 A i  (xA. i- (i- x) = x)"

definition
  ipp_cond2 :: "['a, 'a set, 'a  'a,  'a  'a  'a]  bool" where
  "ipp_cond2 z A i f == x(addition_set f (aug_pm_set z i A)). 
    y (addition_set f (aug_pm_set z i A)). i-(x f+ y) = i- y f+ (i- x)"

definition
  ipp_cond3 :: "['a, 'a  'a]  bool" where
  "ipp_cond3 z i  i- z = z"

lemma add_set_mono:"A  B  add_set f A n  add_set f B n"
apply (induct_tac n)
 apply simp
apply (rule subsetI, simp)
 apply (erule bexE)+
 apply (frule_tac A = "add_set f A n" and B = "add_set f B n" and c = s in 
        subsetD, assumption+)
 apply (frule_tac A = A and B = B and c = t in subsetD, assumption+) 
 apply blast
done

lemma addition_inc_add:"add_set f A n  addition_set f A"
apply (rule subsetI)
 apply (simp add:addition_set_def)
 apply blast
done

lemma addition_inc_add0:" A  addition_set f A"
apply (rule subsetI)
apply (insert addition_inc_add [of "f" "A" "0"]) 
 apply simp
 apply (simp add:subsetD)
done

lemma addition_set_mono:"A  B  addition_set f A  addition_set f B"
apply (rule subsetI)
apply (simp add:addition_set_def [of "f" "A"])
 apply (erule exE, erule conjE, erule exE, simp)
 apply (frule_tac n = n in add_set_mono [of "A" "B" "f"],
        frule_tac A = "add_set f A n" and B = "add_set f B n" and c = x in 
        subsetD, assumption+) 
 apply (cut_tac n = n in addition_inc_add[of f B])
 apply (simp add:subsetD)
done

lemma a_in_aug_pm_set:"a  A  a  aug_pm_set z i A"
apply (simp add:aug_pm_set_def)
done

lemma A_sub_aug_pm_set:"A  aug_pm_set z i A" 
by (rule subsetI, simp add:aug_pm_set_def)

lemma addition_sub_aug_pm_addition:"
        addition_set f A  addition_set f (aug_pm_set z i A)"
apply (cut_tac A_sub_aug_pm_set[of A z i])
apply (simp add:addition_set_mono)
done

lemma assoc_bpp_restrict:" A  B; assoc_bpp B f  assoc_bpp A f"
apply (simp add:assoc_bpp_def)
 apply (rule ballI)+
 apply (frule addition_set_mono[of A B f])
 apply blast
done

lemma addition_assoc:"assoc_bpp A f; x  addition_set f A; 
                       y  addition_set f A; z  addition_set f A  
            (x f+ y) f+ z = x f+ (y f+ z)"
apply (simp add:assoc_bpp_def)
done

lemma bpp_closedTr:"assoc_bpp A f   
      x y. x  add_set f A n  y  add_set f A m  
                  x f+ y  add_set f A (n + m + Suc 0)"
apply (induct_tac m, simp, blast) 
 apply ((rule allI)+, rule impI, erule conjE)
 apply (simp, (erule bexE)+)
 apply (cut_tac addition_inc_add[of f A n],
        cut_tac n = na in addition_inc_add[of f A],
        cut_tac addition_inc_add0[of A f])
 apply (drule_tac x = x in spec,
        drule_tac a = s in forall_spec, simp)

 apply ((erule bexE)+, simp)
 apply (cut_tac n = "n + na" in addition_inc_add[of f A],
        frule_tac c = sa and A = "add_set f A (n + na)" in subsetD[of _
         "addition_set f A"], assumption+,
        frule_tac c = x in subsetD[of "add_set f A n" "addition_set f A"],
         assumption+,
        frule_tac c = s and A = "add_set f A na" in
            subsetD[of _ "addition_set f A"], assumption+,
        frule_tac c = t in subsetD[of A "addition_set f A"], assumption+,
        frule_tac c = ta in subsetD[of A "addition_set f A"], assumption+)
apply (frule_tac x1 = x and y1 = s and z1 = t in addition_assoc[THEN sym,
                 of A f],  assumption+) apply simp apply blast
done
 
lemma bpp_closed1:"assoc_bpp A f; x  add_set f A n; y  add_set f A m 
                    x f+ y  add_set f A (n + m + Suc 0)"
apply (insert bpp_closedTr[of "A" "f"])
apply blast
done
lemma bpp_closed:"assoc_bpp A f; x  addition_set f A; y  addition_set f A
               x f+ y  addition_set f A"
apply (simp add:addition_set_def)
 apply ((erule exE)+, (erule conjE)+, (erule exE)+, simp)
 apply (frule_tac x = x and n = n and y = y and m = na in bpp_closed1,
        assumption+)
 apply blast
done  

lemma aug_addition_inc_z:" z  addition_set f (aug_pm_set z i A)"
apply (subgoal_tac "z  aug_pm_set z i A")
apply (subgoal_tac "aug_pm_set z i A  addition_set f (aug_pm_set z i A)")
 apply (simp add:subsetD)
 apply (simp add:addition_inc_add0)
 apply (simp add:aug_pm_set_def)
done

lemma aug_bpp_closed:"assoc_bpp (aug_pm_set z i A) f; 
      x  addition_set f (aug_pm_set z i A); 
      y  addition_set f (aug_pm_set z i A)   
                  x f+ y  addition_set f (aug_pm_set z i A)"
apply (simp add:bpp_closed [of "aug_pm_set z i A" "f"])
done

lemma aug_commute:"commute_bpp f (aug_pm_set z i A); 
     x  addition_set f (aug_pm_set z i A); 
     y  addition_set f (aug_pm_set z i A)  x f+ y = y f+ x"
apply (simp add: commute_bpp_def)
done

lemma addition_set_inc_z:"z  addition_set f (aug_pm_set z i A)"
apply (simp add:addition_set_def)
apply (subgoal_tac "z  add_set f (aug_pm_set z i A) 0")
apply blast
apply (simp add:aug_pm_set_def)
done

lemma  aug_ipp_closed0:"commute_bpp f (aug_pm_set z i A); 
       assoc_bpp (aug_pm_set z i A) f; ipp_cond1 A i; ipp_cond2 z A i f; 
       ipp_cond3 z i; x  add_set f (aug_pm_set z i A) 0 
         i- x  add_set f (aug_pm_set z i A) 0"
 apply (simp add:aug_pm_set_def)
 apply (case_tac "x  A", simp add:minus_set_def, blast)
 apply simp
 apply (simp add:minus_set_def)
 apply (case_tac "x = z", simp, simp add:ipp_cond3_def)
 apply simp
 apply (erule bexE)
 apply (simp add:ipp_cond1_def)
done

lemma aug_ipp_closedTr:"commute_bpp f (aug_pm_set z i A); 
      assoc_bpp (aug_pm_set z i A) f; ipp_cond1 A i; ipp_cond2 z A i f; 
      ipp_cond3 z i   
      x. x  add_set f (aug_pm_set z i A) n 
                             i- x  add_set f (aug_pm_set z i A) n" 
apply (induct_tac n, rule allI, rule impI) 
 apply (simp add:aug_pm_set_def)
 apply (case_tac "x = z", simp add:ipp_cond3_def)
 apply simp
 apply (case_tac "x  A", simp add:minus_set_def, blast)
 apply simp
 apply (simp add:minus_set_def)
 apply (erule bexE, simp add:ipp_cond1_def)
apply (rule allI, rule impI, simp)
 apply (erule bexE)+
 apply (drule_tac a = s in forall_spec, assumption)
 apply (cut_tac ipp_cond2_def[of z A i f], simp) 
 apply (cut_tac n = n in addition_inc_add[of f "aug_pm_set z i A"],
        frule_tac c = s and A = "add_set f (aug_pm_set z i A) n" in
        subsetD[of _ "addition_set f (aug_pm_set z i A)"], assumption+,
        cut_tac addition_inc_add0[of "aug_pm_set z i A" f],
        frule_tac c = t in subsetD[of "aug_pm_set z i A"
         "addition_set f (aug_pm_set z i A)"], assumption+)
 apply (drule_tac x = s in bspec, assumption,
        drule_tac x = t in bspec, assumption)
        
 apply (frule_tac x = t in aug_ipp_closed0[of f z i A], assumption+,
        simp, assumption+, simp, simp,
        frule_tac c = "i- t" in subsetD[of "aug_pm_set z i A"
                        "addition_set f (aug_pm_set z i A)"], assumption+)
 apply (frule_tac c = "i- s" and A = "add_set f (aug_pm_set z i A) n" in 
        subsetD[of _ "addition_set f (aug_pm_set z i A)"], assumption+,
        frule_tac x = "i- t" and y = "i- s" in aug_commute[of f z i A],
                         assumption+, simp)
 apply blast
done

lemma aug_ipp_closedTr2:"commute_bpp f (aug_pm_set z i A); 
      assoc_bpp (aug_pm_set z i A) f; ipp_cond1 A i; ipp_cond2 z A i f; 
      ipp_cond3 z i; x  add_set f (aug_pm_set z i A) n 
         i- x  add_set f (aug_pm_set z i A) n" 
apply (simp add:aug_ipp_closedTr)
done

lemma aug_ipp_closed:"commute_bpp f (aug_pm_set z i A); 
      assoc_bpp (aug_pm_set z i A) f; ipp_cond1 A i; ipp_cond2 z A i f; 
      ipp_cond3 z i; x  addition_set f (aug_pm_set z i A)  
      i- x  addition_set f (aug_pm_set z i A)"
apply (simp add:addition_set_def, erule exE, erule conjE, erule exE, simp)
 apply (frule_tac n = n in aug_ipp_closedTr2[of f z i A x], assumption+)
 apply blast
done
              
lemma aug_zero_unique:"commute_bpp f (aug_pm_set z i A); 
      z1  addition_set f (aug_pm_set z i A); zeroA z i f A z; 
      zeroA z i f A z1  z = z1"
apply (simp add:zeroA_def[of "z" _ _ _ "z"])
apply (drule_tac x = z1 in bspec, assumption)
       
apply (cut_tac addition_set_inc_z [of z f i A])
apply (frule aug_commute [of f z i A z z1], assumption+)
apply simp
apply (simp add:zeroA_def[of _ _ _ _ "z1"])
done

lemma inv_aug_addition:"commute_bpp f (aug_pm_set z i A); 
      assoc_bpp (aug_pm_set z i A) f; ipp_cond1 A i; ipp_cond2 z A i f; 
      ipp_cond3 z i; inv_ipp z i f A; commute_bpp f (aug_pm_set z i A); 
      zeroA z i f A z  
     aaddition_set f (aug_pm_set z i A). (i-a) f+ a = z"   
apply (simp add:inv_ipp_def)
apply (rule ballI) 
apply (drule_tac x = a in bspec, assumption)
 apply (frule_tac ?z1.0 = "(i- a f+ a)" in aug_zero_unique [of f z i A])
 apply (frule_tac x = a in aug_ipp_closed [of f z i A], assumption+)
 apply (rule_tac x = "i- a" and y = a in aug_bpp_closed [of z i A f],
                                       assumption+)
 apply (simp add:zeroA_def)
done

definition
  fag_gen_by :: "['a set, 'a  'a  'a, 'a  'a, 'a]  'a aGroup" where
  "fag_gen_by A f i z = carrier = addition_set f (aug_pm_set z i A), 
  pop = λx(addition_set f (aug_pm_set z i A)). 
          λy(addition_set f (aug_pm_set z i A)). x f+ y, 
  mop = λx(addition_set f (aug_pm_set z i A)). i- x, zero = z"  

lemma fag_gen_carrier:"commute_bpp f (aug_pm_set z i A); 
      assoc_bpp (aug_pm_set z i A) f; ipp_cond1 A i; ipp_cond2 z A i f; 
      ipp_cond3 z i; inv_ipp z i f A; commute_bpp f (aug_pm_set z i A); 
      zeroA z i f A z  
      carrier (fag_gen_by A f i z) = addition_set f (aug_pm_set z i A)" 
by (simp add:fag_gen_by_def)


lemma addition_set_sub_fag_gen_carrier:"commute_bpp f (aug_pm_set z i A); 
      assoc_bpp (aug_pm_set z i A) f; ipp_cond1 A i; ipp_cond2 z A i f; 
      ipp_cond3 z i; inv_ipp z i f A; commute_bpp f (aug_pm_set z i A); 
      zeroA z i f A z  addition_set f A  carrier (fag_gen_by A f i z)"
apply (simp add:fag_gen_carrier)
apply (simp add:addition_sub_aug_pm_addition)
done

lemma fag_aGroup:"commute_bpp f (aug_pm_set z i A); 
      assoc_bpp (aug_pm_set z i A) f; ipp_cond1 A i; ipp_cond2 z A i f; 
      ipp_cond3 z i; inv_ipp z i f A; commute_bpp f (aug_pm_set z i A); 
      zeroA z i f A z  aGroup (fag_gen_by A f i z)"
apply (rule aGroup.intro)
 apply (simp add:fag_gen_by_def aug_bpp_closed)
 
apply (simp add:fag_gen_by_def)
 apply (simp add:aug_bpp_closed)
 apply (simp add:assoc_bpp_def)

 apply (simp add:fag_gen_by_def)
 apply (simp add:aug_commute)

 apply (simp add:fag_gen_by_def aug_ipp_closed)

 apply (simp add:fag_gen_by_def inv_aug_addition aug_ipp_closed)

 apply (simp add:fag_gen_by_def  addition_set_inc_z)

 apply (simp add:fag_gen_by_def addition_set_inc_z zeroA_def)
done
 
section "Abelian group generated by a singleton (constructive)" 
 
definition
  fag_single :: "['a, 'a  'a  'a, 'a  'a, 'a]  'a aGroup" where
  "fag_single a f i z = fag_gen_by {a} f i z" 

lemma aug_pm_aug_pm_minus:"ipp_cond1 {a} i  
                      aug_pm_set z i {a} = aug_pm_set z i {i- a}"
apply (simp add:aug_pm_set_def minus_set_def)
 apply (simp add:ipp_cond1_def)
 apply (rule equalityI, rule subsetI, simp, blast) 
 apply (rule subsetI, simp, blast)
done

lemma ipp_cond1_minus:"ipp_cond1 {a} i  ipp_cond1 {i- a} i"
by (simp add:ipp_cond1_def)

lemma ipp_cond2_minus:"ipp_cond1 {a} i; ipp_cond2 z {a} i f  
                                             ipp_cond2 z {i- a} i f"
by (simp add:ipp_cond2_def, simp add:aug_pm_aug_pm_minus)

lemma zeroA_minus:"ipp_cond1 {a} i; zeroA z i f {a} z1  
                   zeroA z i f {i- a} z1"
apply (simp add:zeroA_def)
apply (simp add:aug_pm_aug_pm_minus)
done

lemma inv_ipp_minus:"ipp_cond1 {a} i; inv_ipp z i f {a}  
      inv_ipp z i f {i- a}"
 apply (simp add:inv_ipp_def [of _ _ _ "{a}"])
 apply (simp add:aug_pm_aug_pm_minus) 
apply (simp add:inv_ipp_def)
apply (simp add:zeroA_minus)
done

lemma fag_single_additionTr1:"commute_bpp f (aug_pm_set z i {a});
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z  
 s. s add_set f {a} (Suc n)  s f+ i- a  add_set f {a} n"
apply (cut_tac addition_inc_add0[of "aug_pm_set z i {a}" f])
 apply (cut_tac a_in_aug_pm_set[of a "{a}" z i], simp)
 apply (frule subsetD[of "aug_pm_set z i {a}" 
         "addition_set f (aug_pm_set z i {a})" "a"], assumption+)
apply (induct_tac n)
 apply (rule allI, rule impI, simp)
 apply (frule aug_ipp_closed [of f z i "{a}" a], assumption+)
 apply (simp add:addition_assoc)
 apply (frule aug_commute [of f z i "{a}" a "i- a"], assumption+)
 apply simp apply (thin_tac "a f+ i- a = i- a f+ a")
 apply (simp add:inv_aug_addition)
 apply (cut_tac addition_set_inc_z[of z f i "{a}"])
 apply (frule aug_commute [of "f" "z" "i" "{a}" "a" "z"], assumption+)
 apply simp apply (thin_tac "a f+ z = z f+ a")
 apply (simp add:zeroA_def) 

apply (rule allI) apply (rule impI)
 apply (erule bexE)
 apply (thin_tac "s. (saadd_set f {a} n. s = sa f+ a) 
            s f+ i- a  add_set f {a} n")
 apply (simp del:add_set_Suc)
 apply (frule fag_aGroup[of f z i "{a}"], assumption+)
 apply (cut_tac n = "Suc n" in addition_inc_add[of f "{a}"],
              cut_tac addition_sub_aug_pm_addition[of f "{a}" z i],
        frule_tac c = sa and A = "add_set f {a} (Suc n)" in subsetD[of _
          "addition_set f {a}"], assumption+,
        frule_tac c = sa in subsetD[of "addition_set f {a}"
                    "addition_set f (aug_pm_set z i {a})"], assumption+)
 apply (cut_tac x = sa and y = a and z = " i- a" in 
              aGroup.ag_pOp_assoc[of "fag_gen_by {a} f i z"], assumption,
        simp del:add_set_Suc add:fag_gen_carrier)
 apply (simp add:fag_gen_carrier,
        cut_tac addition_inc_add0[of "aug_pm_set z i {a}"],
        simp add:subsetD)
 apply (subst fag_gen_carrier, assumption+) 
        apply (rule aug_ipp_closed[of f z i "{a}" a], assumption+)
        apply (simp add:subsetD)
apply (simp del:add_set_Suc add:fag_gen_by_def, fold fag_gen_by_def,
       frule aug_ipp_closed[of f z i "{a}" a], assumption+,
       simp del:add_set_Suc add:aug_bpp_closed,
       simp del:add_set_Suc add:aug_commute[of f z i "{a}" a "i- a"],
       thin_tac "sa f+ a f+ i- a = sa f+ (i- a f+ a)",
       simp del:add_set_Suc add:inv_aug_addition,
       cut_tac addition_set_inc_z[of z f i "{a}"])
 apply (subst aug_commute[of f z i "{a}" _ z], assumption+)
 apply (simp del:add_set_Suc add:zeroA_def[of z i f "{a}" z])

 apply simp
done

lemma fag_single_additionTr2:"commute_bpp f (aug_pm_set z i {a}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z; s  add_set f {a} 0  s f+ i- a = z"
 apply simp
 apply (cut_tac a_in_aug_pm_set[of a "{a}" z i],
        cut_tac addition_inc_add0[of "aug_pm_set z i {a}" f],
        frule subsetD[of "aug_pm_set z i {a}"
                      "addition_set f (aug_pm_set z i {a})" a], assumption+)
 apply (frule aug_ipp_closed [of "f" "z" "i" "{a}" "a"], assumption+)
  apply (frule aug_commute [of "f" "z" "i" "{a}" "a" "i- a"], assumption+)
  apply simp apply (thin_tac "a f+ i- a = i- a f+ a")
  apply (simp add:inv_aug_addition)
 apply simp
done

lemma ipp_conditions:"assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i;
        ipp_cond2 z {a} i f; ipp_cond3 z i; inv_ipp z i f {a};
        commute_bpp f (aug_pm_set z i {a}); zeroA z i f {a} z 
        assoc_bpp (aug_pm_set z i { i- a}) f  ipp_cond1 { i- a} i 
        ipp_cond2 z { i- a} i f  inv_ipp z i f { i- a}  
        commute_bpp f (aug_pm_set z i { i- a})  zeroA z i f { i- a} z"
apply (simp add:aug_pm_aug_pm_minus[THEN sym])
apply (rule conjI)
 apply (subst ipp_cond1_def, rule ballI, simp, simp add:ipp_cond1_def)

apply (rule conjI)
 apply (subst ipp_cond2_def,
        simp add:aug_pm_aug_pm_minus[THEN sym] ipp_cond2_def)

apply (simp add:zeroA_def inv_ipp_def,
       simp add:aug_pm_aug_pm_minus)
done


lemma fag_single_additionTr3:"commute_bpp f (aug_pm_set z i {a});
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z; s add_set f {i- a} n 
          s f+ i- a  add_set f {i- a} (Suc n)"
apply simp apply blast
done

lemma fag_single_elemTr:"commute_bpp f (aug_pm_set z i {a}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z  
     x. x  add_set f (aug_pm_set z i {a}) n 
     (n1. x  add_set f {a} n1)  (m1. x  add_set f {i- a} m1)  x = z"
 apply (cut_tac a_in_aug_pm_set[of a "{a}" z i],
        cut_tac addition_inc_add0[of "aug_pm_set z i {a}" f],
        frule subsetD[of "aug_pm_set z i {a}"
                      "addition_set f (aug_pm_set z i {a})" a], assumption+)
 apply (cut_tac addition_set_inc_z[of z f i "{a}"])
 prefer 2 apply simp
 apply (cut_tac assoc_bpp_restrict[of "{a}" "aug_pm_set z i {a}" f])
apply (induct_tac n)
 apply (rule allI, rule impI, simp add:aug_pm_set_def)
 apply (erule disjE, simp)
  apply (subgoal_tac "a  add_set f {a} 0", blast)
  apply simp
  apply (erule disjE, simp)
  apply (simp add:minus_set_def)
  apply (subgoal_tac " i- a  add_set f {i- a} 0", blast, simp)
apply (rule allI, rule impI)
 apply (simp, (erule bexE)+)
 apply (drule_tac a = s in forall_spec, assumption)
 apply (subgoal_tac "t = a  t = z  t = i- a")
 prefer 2 apply (simp add:aug_pm_set_def minus_set_def, blast)
 apply (erule disjE, erule exE)
 apply (case_tac "n1 = 0", simp)
  apply (case_tac "t = a", simp)
  apply (cut_tac A_sub_aug_pm_set[of "{a}" z i])
  apply (cut_tac addition_inc_add0[of "{a}"f])
  apply (frule bpp_closed1[of "{a}" f a 0 a 0],
          simp, simp, blast)
  apply simp
  apply (case_tac "t = z", simp)
  apply (simp add:aug_commute[of f z i "{a}" a z])
  apply (simp add:zeroA_def,
         subgoal_tac "a  add_set f {a} 0", blast, simp)

  apply simp
  apply (simp add:fag_single_additionTr2[of f z i a a])

  apply simp
  apply (case_tac "t = a", simp,
         frule_tac x = s and n = n1 and y = a in bpp_closed1[of "{a}" f
              _ _ _ 0], assumption, simp, blast)
  
 apply (case_tac "t = z", simp)
   apply (cut_tac n = n in addition_inc_add[of f "aug_pm_set z i {a}"],
          frule_tac c = s and A = "add_set f (aug_pm_set z i {a}) n" in 
          subsetD[of _ "addition_set f (aug_pm_set z i {a})"], assumption+)
   apply (simp add:aug_commute[of f z i "{a}" _ z])
  apply (simp add:zeroA_def, blast)

 apply simp
 apply (frule_tac n = "n1 - Suc 0" in fag_single_additionTr1[of f z i a],
        assumption+, simp)
 apply (drule_tac a = s in forall_spec, assumption, blast)

 apply (rotate_tac -1, erule disjE)
 apply (erule exE)

 apply (frule ipp_conditions[of z i a f], assumption+, (erule conjE)+)

 apply (case_tac "m1 = 0", simp)
  apply (case_tac "t = a", simp) 
  apply (cut_tac aug_pm_aug_pm_minus[of a i z])
  apply (cut_tac addition_inc_add0[of "{i- a}" f])
  apply (cut_tac A = "{i- a}" and B = "aug_pm_set z i {a}" in
          addition_set_mono[of _ _ f], simp add:aug_pm_set_def) 
  apply (frule subsetD[of "{i- a}" "addition_set f {i- a}" "i- a"], simp,
         frule subsetD[of "addition_set f {i- a}"
              "addition_set f (aug_pm_set z i {a})"], assumption+)
  apply (frule inv_aug_addition[of f z i "{a}"], assumption+)
  apply (drule_tac x = a in bspec, assumption,
         simp, assumption+)

  apply (case_tac "t = z", simp,
         cut_tac n = n in addition_inc_add[of f "aug_pm_set z i {a}"],
         frule_tac c = "i- a" and A = "add_set f (aug_pm_set z i {a}) n" in 
         subsetD[of _ "addition_set f (aug_pm_set z i {a})"], assumption+)
  apply (simp add:aug_commute)
  apply (simp add:zeroA_def)
  apply (subgoal_tac " i- a  add_set f {i- a} 0", blast, simp)

  apply simp
  apply (cut_tac assoc_bpp_restrict[of "{i- a}" "aug_pm_set z i {a}"],
         frule bpp_closed1[of "{i- a}" f "i- a" 0 "i- a" 0], simp, simp, blast)
  apply (simp add:aug_pm_set_def, assumption+)

  apply simp
  apply (case_tac "t = a", simp)
  apply (frule_tac n = "m1 - Suc 0" in fag_single_additionTr1[of f z i "i- a"],
         assumption+, simp) 
  apply (thin_tac "s  add_set f (aug_pm_set z i {a}) n",
         drule_tac a = s in forall_spec, assumption,
         simp add:ipp_cond1_def, blast)
  
  apply (case_tac "t = z", simp,
         cut_tac n = n in addition_inc_add[of f "aug_pm_set z i {a}"],
         frule_tac c = s and A = "add_set f (aug_pm_set z i {a}) n" in 
              subsetD[of _ "addition_set f (aug_pm_set z i {a})"], assumption+)
  apply (frule_tac x = s and y = z in aug_commute[of f z i "{a}"], assumption+,
          simp)
  apply (simp add:zeroA_def, blast)

  apply simp
  apply (cut_tac assoc_bpp_restrict[of "{i- a}" "aug_pm_set z i {i- a}" f],
         frule_tac x = s and n = m1 and y = "i- a" in 
          bpp_closed1[of "{i- a}" f  _ _ _ 0], assumption, simp, blast,
          simp add:aug_pm_set_def, assumption)
  
  apply simp
  apply (case_tac "t = a", simp)
  apply (simp add:zeroA_def)
  apply (subgoal_tac "a  add_set f {a} 0", blast, simp)
  
 apply (case_tac "t = z", simp, simp add:zeroA_def)

 apply simp
 apply (frule subsetD[of "aug_pm_set z i {a}" 
        "addition_set f (aug_pm_set z i {a})" " i- a"], assumption+,
        simp add:zeroA_def, 
        subgoal_tac " i- a  add_set f {i- a} 0", blast, simp)

 apply (simp add:aug_pm_set_def)
 apply assumption
done

lemma fag_single_elem:"commute_bpp f (aug_pm_set z i {a}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z; x  addition_set f (aug_pm_set z i {a})   
    (n1. x  add_set f {a} n1)  (m1. x  add_set f {i- a} m1)  x = z"
apply (simp add:addition_set_def)
apply (erule exE, erule conjE, erule exE, simp)
apply (simp add:fag_single_elemTr)
done

lemma add_set_single1Tr:"commute_bpp f (aug_pm_set z i {a}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z  
      x y. x  add_set f {a} n  y  add_set f {a} n  x = y"
apply (induct_tac n)
 apply ((rule allI)+, rule impI, erule conjE)
 apply simp
apply ((rule allI)+, rule impI, erule conjE, simp, (erule bexE)+)
 apply (drule_tac x = s in spec,
        drule_tac a = sa in forall_spec, simp)
       
apply simp
done

lemma add_set_single_nonempty1:"commute_bpp f (aug_pm_set z i {a}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z   x. xadd_set f {a} n"
apply (induct_tac n)
 apply simp
 apply (erule exE)
 apply simp apply blast
done

lemma add_set_single_nonempty2:"commute_bpp f (aug_pm_set z i {a}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z   x. xadd_set f {i- a} n"
apply (simp add:aug_pm_aug_pm_minus,
       frule ipp_cond1_minus[of "a" "i"],
       frule ipp_cond2_minus[of "a" "i" "z" "f"], assumption+,
       frule inv_ipp_minus[of "a" "i" "z" "f"], assumption+,
       frule zeroA_minus[of "a" "i" "z" "f" "z"], assumption+)
apply (simp add:add_set_single_nonempty1 [of "f" "z" "i" "i- a" "n"])
done

lemma add_set_single1:"commute_bpp f (aug_pm_set z i {a}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z; x  add_set f {a} n; y  add_set f {a} n  x = y"
apply (frule add_set_single1Tr [of f z i a n], assumption+)
apply blast
done

lemma add_set_single2:"commute_bpp f (aug_pm_set z i {a}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z; x  add_set f {i- a} n; y  add_set f {i- a} n   
      x = y"
apply (simp add:aug_pm_aug_pm_minus)
apply (frule ipp_cond1_minus[of "a" "i"])
apply (frule ipp_cond2_minus[of "a" "i" "z" "f"], assumption+)
apply (frule inv_ipp_minus[of "a" "i" "z" "f"], assumption+)
apply (frule zeroA_minus[of "a" "i" "z" "f" "z"], assumption+)
apply (rule add_set_single1 [of "f" "z" "i" "i- a" _ "n" _], assumption+)
done

lemma fag_single_additionTr4:"commute_bpp f (aug_pm_set z i {a});
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z   
      s t. s  add_set f {a} n  t  add_set f {i- a} n s f+ t = z"
 apply (cut_tac a_in_aug_pm_set[of a "{a}" z i], simp,
        cut_tac addition_inc_add0[of "aug_pm_set z i {a}" f],
        frule subsetD[of "aug_pm_set z i {a}"
                     "addition_set f (aug_pm_set z i {a})" a], assumption+)

apply (induct_tac n)
 apply simp
 apply (frule inv_aug_addition [of "f" "z" "i" "{a}"], assumption+)
 apply (frule aug_ipp_closed [of "f" "z" "i" "{a}" "a"], assumption+)
 apply (frule fag_single_additionTr2 [of "f" "z" "i" "a" "a"], assumption+)
 apply (simp, assumption)
apply ((rule allI)+, rule impI, erule conjE)
 apply simp
 apply (erule bexE)+
 apply (frule aug_ipp_closed [of "f" "z" "i" "{a}" "a"], assumption+)
 apply simp
 apply (drule_tac x = sa in spec, 
        drule_tac a = sb in forall_spec, simp)
 apply (cut_tac n = n in addition_inc_add[of f "{a}"],
        cut_tac addition_set_mono[of "{a}" "aug_pm_set z i {a}" f],
        frule_tac c = sa and A = "add_set f {a} n" in subsetD[of _
          "addition_set f {a}"], assumption+,
        frule_tac c = sa in subsetD[of "addition_set f {a}"
               "addition_set f (aug_pm_set z i {a})"], assumption+)
 apply (cut_tac n = n in addition_inc_add[of f "{i- a}"],
        cut_tac addition_set_mono[of "{i- a}" "aug_pm_set z i {i- a}" f],
        frule_tac c = sb and A = "add_set f {i- a} n" in subsetD[of _
          "addition_set f {i- a}"], assumption+,
        frule_tac c = sb in subsetD[of "addition_set f {i- a}"
               "addition_set f (aug_pm_set z i {i- a})"], assumption+)
  apply (simp add:aug_pm_aug_pm_minus[THEN sym])
  apply (frule_tac x = sb in aug_bpp_closed [of z i "{a}" f _ " i- a"],
         assumption+)
 apply (frule_tac x = sa and y = a and z = "sb f+ i- a" in
                 addition_assoc [of "aug_pm_set z i {a}" "f"], assumption+)
 apply simp apply (thin_tac "sa f+ a f+ (sb f+ i- a) = sa f+ (a f+ (sb f+ i- a))")
 apply (frule_tac x1 = a and y1 = sb and z1 = "i- a" in 
        addition_assoc [THEN sym, of "aug_pm_set z i {a}" "f"], assumption+)
  apply simp
 apply (frule_tac y = sb in aug_commute [of "f" "z" "i" "{a}" "a"], 
        assumption+, simp)
 apply (frule_tac x = sb and y = a and z = "i- a" in
                 addition_assoc [of "aug_pm_set z i {a}" "f"], assumption+)
 apply simp apply (thin_tac "a f+ (sb f+ i- a) = sb f+ (a f+ i- a)")
 apply (thin_tac "a f+ sb = sb f+ a",
        thin_tac "sb f+ a f+ i- a = sb f+ (a f+ i- a)",
        thin_tac "sb f+ i- a  addition_set f (aug_pm_set z i {a})")
 apply (frule_tac y = " i- a" in aug_commute [of "f" "z" "i" "{a}" "a"], 
            assumption+, simp)
 apply (frule_tac x = sa and y = a and z = "sb f+ i- a" in
                 addition_assoc [of "aug_pm_set z i {a}" "f"], assumption+)
 apply (frule_tac x = sb in aug_bpp_closed [of "z" "i" "{a}" "f" _ " i- a"],
  assumption+) 
 apply (thin_tac "sa f+ a f+ (sb f+ i- a) = sa f+ (a f+ (sb f+ i- a))")
 apply (thin_tac "a f+ i- a = i- a f+ a")
 apply (frule inv_aug_addition [of "f" "z" "i" "{a}" ], assumption+)
 apply (drule_tac x = a in bspec, assumption, simp)
        
 apply (frule_tac x = sb and y = z in aug_commute [of "f" "z" "i" "{a}"], 
             assumption+) 
 apply (simp add:addition_set_inc_z)
apply (frule_tac x = sb and y = z in aug_commute [of "f" "z" "i" "{a}"], 
       assumption+, simp add:addition_set_inc_z, simp)
 apply (simp add:zeroA_def)
 apply (rule subsetI, simp add:aug_pm_set_def minus_set_def)
 apply (simp add:aug_pm_set_def)
 apply simp
done

lemma fag_single_additionTr4_1:"commute_bpp f (aug_pm_set z i {a});
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z;s  add_set f {a} n; t  add_set f {i- a} n   
      s f+ t = z"
apply (frule fag_single_additionTr4[of "f" "z" "i" "a" "n"], assumption+)
 apply blast
done

lemma fag_single_additionTr5:"assoc_bpp (aug_pm_set z i {a}) f; 
      ipp_cond1 {a} i; ipp_cond2 z {a} i f; ipp_cond3 z i; inv_ipp z i f {a}; 
      commute_bpp f (aug_pm_set z i {a}); zeroA z i f {a} z   
      m. m < Suc n  (THE x. x  add_set f {a} (Suc n)) f+ 
         (THE x. x  add_set f {i- a} m) = (THE x. x  add_set f {a} (n - m))"
apply (cut_tac a_in_aug_pm_set[of a "{a}" z i],
        cut_tac addition_inc_add0[of "aug_pm_set z i {a}" f],
        frule subsetD[of "aug_pm_set z i {a}"
                     "addition_set f (aug_pm_set z i {a})" a], assumption+)
prefer 2 apply simp
 apply (cut_tac aug_addition_inc_z[of z f i "{a}"])
 apply (frule aug_ipp_closed [of "f" "z" "i" "{a}" "a"], assumption+)

apply (induct_tac n)
 apply (rule allI, rule impI, simp)
 apply (simp add: addition_assoc [of "aug_pm_set z i {a}" "f" "a" "a" "i- a"])
 apply (frule fag_single_additionTr2 [of "f" "z" "i" "a" "a"], assumption+)
 apply simp apply simp
 apply (thin_tac "a f+ i- a = z")
 apply (simp add:aug_commute [of "f" "z" "i" "{a}" "a" "z"])
 apply (simp add:zeroA_def)

apply (rule allI, rule impI)
apply (subgoal_tac "(THE x. x  add_set f {a} (Suc (Suc n))) = 
                (THE x. x  add_set f {a} (Suc n)) f+ a")
 apply (simp del:add_set_Suc)
 apply (frule_tac m = m and n = "Suc (Suc n)" in Suc_leI,
        thin_tac "Suc m  Suc (Suc n)")
 apply (case_tac "Suc m = Suc (Suc n)")
  apply (frule_tac x = m and y = "Suc n" in Suc_inject,
         thin_tac "Suc m = Suc (Suc n)")
  apply (rotate_tac -1, frule sym, thin_tac "m = Suc n",
         thin_tac "m. m < Suc n  (THE x. x  add_set f {a} (Suc n)) f+
  (THE x. x  add_set f {i- a} m) = (THE x. x  add_set f {a} (n - m))",
         thin_tac "(THE x. x  add_set f {a} (Suc (Suc n))) =
             (THE x. x  add_set f {a} (Suc n)) f+ a")
  apply simp 
 apply (subgoal_tac "(THE x. x  add_set f {a} m)  
                             addition_set f (aug_pm_set z i {a})")
 apply (subgoal_tac "(THE x. x  add_set f {i- a} m)  
                             addition_set f (aug_pm_set z i {a})")
 apply (frule_tac x = "THE x. x  add_set f {a} m" and y = a and 
                  z = "THE x. x  add_set f {i- a} m" in 
                  addition_assoc [of "aug_pm_set z i {a}" "f"], assumption+) 
 apply simp
 apply (thin_tac "(THE x. x  add_set f {a} m) f+ a f+ 
       (THE x. x  add_set f {i- a} m) = 
       (THE x. x  add_set f {a} m) f+ (a f+ (THE x. x  add_set f {i- a} m))")
 apply (frule_tac x = a and y = "THE x. x  add_set f {i- a} m" in 
        aug_commute [of "f" "z" "i" "{a}"], assumption+) apply simp
 apply (thin_tac "a f+ (THE x. x  add_set f {i- a} m) = 
        (THE x. x  add_set f {i- a} m) f+ a")
 apply (frule_tac x1 = "THE x. x  add_set f {a} m" and 
         y1 = "THE x. x  add_set f {i- a} m" and z1 = a in 
        addition_assoc[THEN sym, of "aug_pm_set z i {a}" "f"], assumption+)  
 apply simp
 apply (frule_tac n = m in fag_single_additionTr4 [of "f" "z" "i" "a"], 
        assumption+)
 apply (subgoal_tac "(THE x. x  add_set f {a} m) f+ 
        (THE x. x  add_set f {i- a} m) = z")
 prefer 2 
 apply (thin_tac "(THE x. x  add_set f {a} m)
              addition_set f (aug_pm_set z i {a})",
        thin_tac "(THE x. x  add_set f {i- a} m)
              addition_set f (aug_pm_set z i {a})",
        thin_tac "(THE x. x  add_set f {a} m) f+ 
        ((THE x. x  add_set f {i- a} m) f+ a) = 
        (THE x. x  add_set f {a} m) f+ (THE x. x  add_set f {i- a} m)  f+ a")
 apply (subgoal_tac "(THE x. x  add_set f {a} m)  add_set f {a} m")
 apply (subgoal_tac "(THE x. x  add_set f {i- a} m)  add_set f {i- a} m")
  apply simp
  apply (thin_tac "s t. s  add_set f {a} m  t  add_set f {i- a} m  
        s f+ t = z",
         thin_tac "(THE x. x  add_set f {a} m)  add_set f {a} m")
 apply (rule theI') apply (rule ex_ex1I) 
  apply (simp add:add_set_single_nonempty2)
  apply (simp add:add_set_single2) 
 apply (rule theI') apply (rule ex_ex1I) 
  apply (simp add:add_set_single_nonempty1)
  apply (simp add:add_set_single1) 
  apply (thin_tac "s t. s  add_set f {a} m  t  add_set f {i- a} m 
                   s f+ t = z")
  apply simp apply (simp add:zeroA_def) 
 apply (subgoal_tac "(THE x. x  add_set f {i- a} m)  add_set f {i- a} m")
 apply (subgoal_tac "{i- a}  (aug_pm_set z i {a})")
 apply (frule_tac n = m in add_set_mono[of "{i- a}" "aug_pm_set z i {a}" "f"])
 apply (frule_tac A = "add_set f {i- a} m" and 
        B = "add_set f (aug_pm_set z i {a}) m" and 
        c = "THE x. x  add_set f {i- a} m" in subsetD, assumption+)
 apply (subgoal_tac "add_set f (aug_pm_set z i {a}) m  
                                addition_set f (aug_pm_set z i {a})")  
 apply (simp add:subsetD) apply (simp add:addition_inc_add)
 apply (rule subsetI) apply (simp add:aug_pm_set_def minus_set_def)
 apply (thin_tac "(THE x. x  add_set f {a} m)
              addition_set f (aug_pm_set z i {a})")
 apply (rule theI') apply (rule ex_ex1I) 
  apply (simp add:add_set_single_nonempty2)
  apply (simp add:add_set_single2) 
 apply (subgoal_tac "(THE x. x  add_set f {a} m)  add_set f {a} m")
 apply (subgoal_tac "{a}  (aug_pm_set z i {a})")
 apply (frule_tac n = m in add_set_mono[of "{a}" "aug_pm_set z i {a}" "f"])
 apply (frule_tac A = "add_set f {a} m" and 
        B = "add_set f (aug_pm_set z i {a}) m" and 
        c = "THE x. x  add_set f {a} m" in subsetD, assumption+)
 apply (subgoal_tac "add_set f (aug_pm_set z i {a}) m  
                                addition_set f (aug_pm_set z i {a})")  
 apply (simp add:subsetD) apply (simp add:addition_inc_add)
 apply (rule subsetI) apply (simp add:aug_pm_set_def minus_set_def)
 apply (rule theI') apply (rule ex_ex1I) 
  apply (simp add:add_set_single_nonempty1)
  apply (simp add:add_set_single1) apply (simp del:add_set_Suc)
  apply (subgoal_tac "(THE x. x  add_set f {a} (Suc n)) f+
       (THE x. x  add_set f {i- a} m) = (THE x. x  add_set f {a} (n - m))")
 prefer 2 apply simp
 apply (thin_tac "m. m < Suc n  (THE x. x  add_set f {a} (Suc n)) f+
 (THE x. x  add_set f {i- a} m) =  (THE x. x  add_set f {a} (n - m))")
 apply (thin_tac "(THE x. x  add_set f {a} (Suc (Suc n))) =
             (THE x. x  add_set f {a} (Suc n)) f+ a")
 apply (subgoal_tac "(THE x. x  add_set f {a} (Suc n))  add_set f {a} (Suc n)")
 prefer 2
 apply (rule theI') apply (rule ex_ex1I)
  apply (simp del:add_set_Suc add:add_set_single_nonempty1)
  apply (simp del:add_set_Suc add:add_set_single1) 
 apply (subgoal_tac "(THE x. x  add_set f {i- a} m)  add_set f {i- a} m")
 apply (subgoal_tac "(THE x. x  add_set f {a} (Suc n))  addition_set f (aug_pm_set z i {a})")
 apply (subgoal_tac "(THE x. x  add_set f {i- a} m)  addition_set f (aug_pm_set z i {a})")
 apply (frule_tac x = "THE x. x  add_set f {a} (Suc n)" and y = a and z = "THE x. x  add_set f {i- a} m" in addition_assoc [of "aug_pm_set z i {a}" "f"], assumption+) apply (simp del:add_set_Suc)
 apply (frule_tac x = a and y = "THE x. x  add_set f {i- a} m" in aug_commute [of "f" "z" "i" "{a}"], assumption+) apply (simp del:add_set_Suc)
apply (frule_tac x1 = "THE x. x  add_set f {a} (Suc n)" and y1 = "THE x. x  add_set f {i- a} m" and z1 = a in addition_assoc[THEN sym, of "aug_pm_set z i {a}" "f"], assumption+)  apply (simp del:add_set_Suc)
 apply (thin_tac "(THE x. x  add_set f {a} (Suc n)) f+
       (THE x. x  add_set f {i- a} m) = (THE x. x  add_set f {a} (n - m))",
        thin_tac " (THE x. x  add_set f {a} (Suc n)) f+ a f+
  (THE x. x  add_set f {i- a} m) = (THE x. x  add_set f {a} (n - m)) f+ a",
        thin_tac "(THE x. x  add_set f {a} (Suc n)) f+
  ((THE x. x  add_set f {i- a} m) f+ a) = (THE x. x  
                                             add_set f {a} (n - m)) f+ a",
       thin_tac "a f+ (THE x. x  add_set f {i- a} m) =
                       (THE x. x  add_set f {i- a} m) f+ a")
 apply (subgoal_tac "Suc n - m = Suc (n - m)", simp del:add_set_Suc) 
 apply (thin_tac "Suc n - m = Suc (n - m)")
 apply (subgoal_tac "(THE x. x  add_set f {a} (Suc (n - m)))  add_set f {a}
 (Suc (n - m))")
 apply (subgoal_tac "sadd_set f {a} (n - m). (THE x. x add_set f {a} (Suc (n - m))) = s f+ a") prefer 2 apply simp
 apply (subgoal_tac "sadd_set f {a} (n - m). (THE x. xadd_set f {a} (Suc (n - m))) = s f+ a  (THE x. x  add_set f {a} (n - m)) f+ a =
             (THE x. x  add_set f {a} (Suc (n - m)))")
 apply blast apply (thin_tac "sadd_set f {a} (n - m).
                (THE x. x  add_set f {a} (Suc (n - m))) = s f+ a")
 apply (rule ballI) apply (rule impI) apply (simp del:add_set_Suc)
 apply (thin_tac "s f+ a  add_set f {a} (Suc (n - m))")
 apply (thin_tac "(THE x. x  add_set f {a} (Suc (n - m))) = s f+ a")
 apply (subgoal_tac "(THE x. x  add_set f {a} (n - m)) = s ")
 apply simp
 apply (subgoal_tac "(THE x. x  add_set f {a} (n - m))  
                                           add_set f {a} (n - m)")
 apply (simp add:add_set_single1)
 apply (rule theI') apply (rule ex_ex1I)
  apply (simp add:add_set_single_nonempty1)
  apply (simp add:add_set_single1) 
 apply (thin_tac "(THE x. x  add_set f {i- a} m)  
                          addition_set f (aug_pm_set z i {a})",
        thin_tac "(THE x. x  add_set f {a} (Suc n))  add_set f {a} (Suc n)",
        thin_tac "(THE x. x  add_set f {i- a} m)  add_set f {i- a} m",
        thin_tac "(THE x. x  add_set f {a} (Suc n))
              addition_set f (aug_pm_set z i {a})")
 apply (rule theI') apply (rule ex_ex1I)
  apply (simp del:add_set_Suc add:add_set_single_nonempty1)
  apply (simp del:add_set_Suc add:add_set_single1) 
  apply (simp add:Suc_diff_le)
  apply(thin_tac "(THE x. x  add_set f {a} (Suc n)) f+
       (THE x. x  add_set f {i- a} m) = (THE x. x  add_set f {a} (n - m))",
        thin_tac "(THE x. x  add_set f {a} (Suc n))  add_set f {a} (Suc n)",
        thin_tac "(THE x. x  add_set f {i- a} m)  add_set f {i- a} m",
        thin_tac "(THE x. x  add_set f {a} (Suc n))
              addition_set f (aug_pm_set z i {a})")
 apply (subgoal_tac "{i- a}  (aug_pm_set z i {a})")
 apply (frule_tac A = "{i- a}" and B = "(aug_pm_set z i {a})" and 
           n = m and f = f in add_set_mono)
 apply (subgoal_tac "(THE x. x  add_set f {i- a} m)  add_set f {i- a} m")
 apply (cut_tac n = m in addition_inc_add[of f "aug_pm_set z i {a}"])
 apply (simp add:subsetD)+ 
 apply (thin_tac "add_set f {i- a} m  add_set f (aug_pm_set z i {a}) m")
 apply (rule theI', rule ex_ex1I)
  apply (simp add:add_set_single_nonempty2)
  apply (simp add:add_set_single2) 
  apply (rule subsetI, simp add:aug_pm_set_def minus_set_def)
 apply (thin_tac "(THE x. x  add_set f {a} (Suc n)) f+
  (THE x. x  add_set f {i- a} m) = (THE x. x  add_set f {a} (n - m))",
        thin_tac "(THE x. x  add_set f {i- a} m)  add_set f {i- a} m")
  apply (subgoal_tac "{a}  (aug_pm_set z i {a})")
  apply (frule_tac A = "{a}" and B = "aug_pm_set z i {a}" and 
                                 n = "Suc n" and f = f in add_set_mono)
   apply (cut_tac n = "Suc n" in addition_inc_add[of f "aug_pm_set z i {a}"])
  apply (simp del:add_set_Suc add:subsetD)+
 apply (thin_tac "(THE x. x  add_set f {a} (Suc n)) f+
       (THE x. x  add_set f {i- a} m) = (THE x. x  add_set f {a} (n - m))",
        thin_tac "(THE x. x  add_set f {a} (Suc n))  add_set f {a} (Suc n)")
 apply (rule theI') apply (rule ex_ex1I)
  apply (simp add:add_set_single_nonempty2)
  apply (simp add:add_set_single2) 
 apply (thin_tac "m. m < Suc n  (THE x. x  add_set f {a} (Suc n)) f+
 (THE x. x  add_set f {i- a} m) = (THE x. x  add_set f {a} (n - m))")
 apply (subgoal_tac "(THE x. x  add_set f {a} (Suc (Suc n)))  
                                           add_set f {a} (Suc (Suc n))")
 apply (subgoal_tac "sadd_set f {a} (Suc n). 
        (THE x. x  add_set f {a} (Suc (Suc n))) = s f+ a") 
 prefer 2 apply simp 
 apply (erule bexE)
 apply (simp del:add_set_Suc)
 apply (thin_tac "(THE x. x  add_set f {a} (Suc (Suc n))) = s f+ a")
 apply (subgoal_tac "s = (THE x. x  add_set f {a} (Suc n))")
 apply (simp del:add_set_Suc)
 apply (subgoal_tac "(THE x. x  add_set f {a} (Suc n))  
                                      add_set f {a} (Suc n)")
 apply (simp del:add_set_Suc add:add_set_single1)
 apply (rule theI') apply (rule ex_ex1I)
  apply (simp del:add_set_Suc add:add_set_single_nonempty1)
  apply (simp del:add_set_Suc add:add_set_single1) 
 apply (rule theI') apply (rule ex_ex1I)
  apply (simp del:add_set_Suc add:add_set_single_nonempty1)
  apply (simp del:add_set_Suc add:add_set_single1) 
done

lemma fag_single_additionTr5_1:"assoc_bpp (aug_pm_set z i {a}) f; 
      ipp_cond1 {a} i; ipp_cond2 z {a} i f; ipp_cond3 z i; inv_ipp z i f {a}; 
      commute_bpp f (aug_pm_set z i {a}); zeroA z i f {a} z; m < Suc n  
 (THE x. x  add_set f {a} (Suc n)) f+ (THE x. x  add_set f {i- a} m) = 
                            (THE x. x  add_set f {a} (n - m))"
apply (frule_tac n = n in fag_single_additionTr5 [of "z" "i" "a" "f"], 
         assumption+) apply simp
done

lemma fag_single_additionTr5_2:"assoc_bpp (aug_pm_set z i {a}) f; 
      ipp_cond1 {a} i; ipp_cond2 z {a} i f; ipp_cond3 z i; inv_ipp z i f {a}; 
      commute_bpp f (aug_pm_set z i {a}); zeroA z i f {a} z; n < Suc m  
     (THE x. x  add_set f {i- a} (Suc m)) f+ (THE x. x  add_set f {a} n) = 
           (THE x. x  add_set f {i- a} (m - n))"
apply (simp del:add_set_Suc add:aug_pm_aug_pm_minus)
 apply (frule ipp_cond1_minus[of "a" "i"])
 apply (frule ipp_cond2_minus[of "a" "i" "z" "f"], assumption+)
 apply (frule inv_ipp_minus[of "a" "i" "z" "f"], assumption+)
 apply (frule zeroA_minus[of "a" "i" "z" "f" "z"], assumption+)
 apply (subgoal_tac "i- (i- a) = a")
 apply (frule fag_single_additionTr5_1 [of z i "i- a" f n m], assumption+)
  apply (simp del:add_set_Suc)
 apply (simp add:ipp_cond1_def)
done


definition
  free_gen_condition :: "['a  'a  'a, 'a  'a, 'a, 'a]  bool" where
  "free_gen_condition f i a z  (n. z  add_set f {a} n)"

definition
  fg_elem_single :: "['a  'a  'a, 'a  'a, 'a, 'a]  int  'a" where
  "fg_elem_single f i a z n = (if 0 = n then z else 
      (if 0 < n then (THE x. x  (add_set f {a} (nat (n - 1)))) 
        else (THE x. x  (add_set f {i- a} (nat (- n - 1))))))"

abbreviation
  FGELEMSNGLE  ((5___,_,_) [99,98,98,98,98]99) where
  "naf,i,z== fg_elem_single f i a z n"

lemma  single_addition_pm_mem:"assoc_bpp (aug_pm_set z i {a}) f; 
       ipp_cond1 {a} i; ipp_cond2 z {a} i f; ipp_cond3 z i; inv_ipp z i f {a};
       commute_bpp f (aug_pm_set z i {a}); zeroA z i f {a} z  
      (naf,i,z)  addition_set f (aug_pm_set z i {a})"
apply (case_tac "n = 0")
 apply (simp add:fg_elem_single_def) apply (simp add:aug_addition_inc_z)
 apply (frule_tac non_zero_int [of "n"]) 
apply (case_tac "0 < n")
 apply (simp add:fg_elem_single_def)
 apply (subgoal_tac "(THE x. x  add_set f {a} (nat (n - 1)))  
                                           add_set f {a} (nat (n - 1))")
 apply (subgoal_tac "add_set f {a} (nat (n - 1))  addition_set f {a}")
 apply (subgoal_tac "addition_set f {a} 
                                 addition_set f (aug_pm_set z i {a})") 
 apply (simp add:subsetD)+
 apply (rule addition_set_mono)
 apply (rule subsetI) apply (simp add:aug_pm_set_def minus_set_def)
 apply (simp add:addition_inc_add)
 apply (rule theI') 
  apply (rule ex_ex1I)
  apply (simp add:add_set_single_nonempty1)
  apply (simp add:add_set_single1) apply (thin_tac "n  0")
 apply simp
 apply (simp add:fg_elem_single_def)
 apply (subgoal_tac "(THE x. x  add_set f {i- a} (nat (- n - 1)))  
                                       add_set f {i- a} (nat (- n - 1))")
 apply (subgoal_tac "add_set f {i- a} (nat (- n - 1))  
                                       addition_set f {i- a}")
 apply (subgoal_tac "addition_set f {i- a}   
                         addition_set f (aug_pm_set z i {a})") 
 apply (simp add:subsetD)+
 apply (rule addition_set_mono)
 apply (rule subsetI) apply (simp add:aug_pm_set_def minus_set_def)
 apply (simp add:addition_inc_add)
 apply (rule theI') 
  apply (rule ex_ex1I)
  apply (simp add:add_set_single_nonempty2)
  apply (simp add:add_set_single2) 
done

lemma assoc_aug_assoc:"assoc_bpp (aug_pm_set z i {a}) f  assoc_bpp {a} f"
apply (simp add:assoc_bpp_def)
apply (rule ballI)+
apply (subgoal_tac "{a}  aug_pm_set z i {a}")
apply (frule addition_set_mono[of "{a}" "aug_pm_set z i {a}" "f"])
 apply (frule_tac c = aa in subsetD[of "addition_set f {a}" 
                    "addition_set f (aug_pm_set z i {a})"], assumption+)
 apply (frule_tac c = b in subsetD[of "addition_set f {a}" 
                    "addition_set f (aug_pm_set z i {a})"], assumption+)
 apply (frule_tac c = c in subsetD[of "addition_set f {a}" 
                    "addition_set f (aug_pm_set z i {a})"], assumption+)
 apply simp
 apply (rule subsetI)
 apply (simp add:aug_pm_set_def minus_set_def)
done

lemma single_addition_posTr:"commute_bpp f (aug_pm_set z i {(a::'a)}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z; 0 < (n::int); 0 < (m::int)  
  (THE x. x  add_set f {a} (nat (n - 1))) f+ 
   (THE x. x  add_set f {a} (nat (m - 1))) = 
                              (THE x. x  add_set f {a} (nat (n + m - 1)))" 
apply (subgoal_tac "(THE x. x  add_set f {a} (nat (n - 1)))  
                                        add_set f {a} (nat (n - 1))")
apply (subgoal_tac "(THE x. x  add_set f {a} (nat (m - 1))) 
                                        
                                          add_set f {a} (nat (m - 1))") 
apply (subgoal_tac "(THE x. x  add_set f {a} (nat (n + m - 1))) 
                                      add_set f {a} (nat (n + m - 1))")
apply (frule assoc_aug_assoc [of "z" "i" "a" "f"]) 
apply (frule_tac x = "THE x. x  add_set f {a} (nat (n - 1))" and 
       y = "THE x. x  add_set f {a} (nat (m - 1))" in 
       bpp_closed1 [of "{a}" "f" _ "nat (n - 1)" _ "nat (m - 1)"], assumption+)
apply (subgoal_tac "nat (n - 1) + nat (m - 1) + Suc 0 = nat (n + m - 1)")
apply (simp del:add_set_Suc)
apply (simp add:add_set_single1) 
prefer 2
  apply (thin_tac "(THE x. x  add_set f {a} (nat (n - 1)))
        add_set f {a} (nat (n - 1))")
  apply (thin_tac "(THE x. x  add_set f {a} (nat (m - 1)))
        add_set f {a} (nat (m - 1))")
  apply (rule theI')
  apply (rule ex_ex1I)
  apply (simp add:add_set_single_nonempty1)
  apply (simp add:add_set_single1)
prefer 2 
  apply (rule theI')
  apply (rule ex_ex1I)
  apply (simp add:add_set_single_nonempty1)
  apply (simp add:add_set_single1)
prefer 2
  apply (rule theI')
  apply (rule ex_ex1I)
  apply (simp add:add_set_single_nonempty1)
  apply (simp add:add_set_single1)
apply (rule int_nat_add, assumption+)
done

lemma single_addition_pos:"commute_bpp f (aug_pm_set z i {(a::'a)}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z; 0 < (n::int); 0 < (m::int)  
     (naf,i,z) f+ (maf,i,z) = (n + m)af,i,z⇙"
apply (frule_tac single_addition_posTr [of f z i a n m], assumption+)
apply (simp add:fg_elem_single_def)
done 

lemma single_addition_neg:"commute_bpp f (aug_pm_set z i {(a::'a)});
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z; (n::int) < 0; (m::int) < 0   
           (naf,i,z) f+ (maf,i,z) = (n + m)af,i,z⇙" 
apply (simp add:fg_elem_single_def)
apply (simp del:add_set_Suc add:aug_pm_aug_pm_minus)
 apply (frule ipp_cond1_minus[of "a" "i"])
 apply (frule ipp_cond2_minus[of "a" "i" "z" "f"], assumption+)
 apply (frule inv_ipp_minus[of "a" "i" "z" "f"], assumption+)
 apply (frule zeroA_minus[of "a" "i" "z" "f" "z"], assumption+)
apply (frule single_addition_posTr [of f z i "i- a" "- n" "- m"], assumption+)
apply simp+
done

lemma single_addition_zero:"commute_bpp f (aug_pm_set z i {(a::'a)}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z  0af,i,z= z" 
apply (simp add:fg_elem_single_def)
done

lemma s_a_p_1:"assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; 
                ipp_cond2 z {a} i f; ipp_cond3 z i; inv_ipp z i f {a}; 
                commute_bpp f (aug_pm_set z i {a}); zeroA z i f {a} z; 
                m < 0; 0 < n  (naf,i,z) f+ (maf,i,z) = (n + m)af,i,z⇙"
 
apply (case_tac "- m < n")
 apply (subst zminus_zadd_cancel [THEN sym, of "n" "m"])
 apply (subgoal_tac "0 < -m") apply (subgoal_tac "0 < m + n")
 apply (subst single_addition_pos[THEN sym, of "f" "z" "i" "a" "-m" "m + n"],
             assumption+)
 apply (simp add:zminus_zadd_cancel [of "m" "m + n"])
apply (frule single_addition_pm_mem[of "z" "i" "a" "f" "-m"], assumption+)
 apply (frule single_addition_pm_mem[of "z" "i" "a" "f" "m + n"], assumption+) 
 apply (frule single_addition_pm_mem[of "z" "i" "a" "f" "m"], assumption+) 
 apply (subst addition_assoc, assumption+)
 apply (simp add:aug_commute[of "f" "z" "i" "{a}" "(m + n)af,i,z⇙" "maf,i,z⇙"])
 apply (subst addition_assoc[THEN sym], assumption+)
 apply (subgoal_tac "((- m)af,i,z) f+ (maf,i,z) = z") apply simp
 apply (simp add:zeroA_def)  apply (simp add:add.commute)
 apply (simp add:fg_elem_single_def)  
 apply (rule fag_single_additionTr4_1[of "f" "z" "i" "a"_ "nat (- m - 1)"], assumption+)
  apply (rule theI') apply (rule ex_ex1I) 
   apply (simp add:add_set_single_nonempty1)
   apply (simp add:add_set_single1)
  apply (rule theI') apply (rule ex_ex1I) 
   apply (simp add:add_set_single_nonempty2)
  apply (simp add:add_set_single2) 
 apply simp+ apply (subgoal_tac "n  - m") prefer 2 apply simp
 apply (thin_tac "¬ - m < n") 
 apply (frule zle_imp_zless_or_eq) apply (thin_tac "n  - m")
 apply (case_tac "n = -m")
 apply (thin_tac "n < - m  n = - m")   apply simp
 apply (subgoal_tac "0 < -m") apply (thin_tac "n = - m")
 apply (simp add:fg_elem_single_def)
 apply (rule fag_single_additionTr4_1 [of "f" "z" "i" "a" _ "nat (-m - 1)"], assumption+)
  apply (rule theI') apply (rule ex_ex1I) 
   apply (simp add:add_set_single_nonempty1)
   apply (simp add:add_set_single1) 
  apply (rule theI') apply (rule ex_ex1I) 
   apply (simp add:add_set_single_nonempty2)
   apply (simp add:add_set_single2) 
  apply simp 
 apply simp
apply (subst zminus_zadd_cancel [THEN sym, of "m" "n"])
 apply (subst single_addition_neg[THEN sym, of "f" "z" "i" "a" "-n" "n + m"], assumption+) apply simp apply simp
 apply (frule single_addition_pm_mem[of "z" "i" "a" "f" "-n"], assumption+) 
 apply (frule single_addition_pm_mem[of "z" "i" "a" "f" "n + m"], assumption+)  apply (frule single_addition_pm_mem[of "z" "i" "a" "f" "n"], assumption+)   
 apply (simp add:addition_assoc[THEN sym])
 apply (subgoal_tac "(naf,i,z) f+ ((- n)af,i,z) = z") apply (simp add:zeroA_def)
 apply (thin_tac "(- n)af,i,z addition_set f (aug_pm_set z i {a})")
 apply (thin_tac "(n + m)af,i,z addition_set f (aug_pm_set z i {a})")
 apply (thin_tac "naf,i,z addition_set f (aug_pm_set z i {a})")
 apply (simp add:fg_elem_single_def)
 apply (subgoal_tac "(THE x. x  add_set f {a} (nat (n - 1)))  
                                      add_set f {a} (nat (n - 1))") 
 apply (subgoal_tac "(THE x. x  add_set f {i- a} (nat (n - 1)))  
                                   add_set f {i- a} (nat (n - 1))")
 apply (simp add:fag_single_additionTr4_1)
 apply (rule theI') apply (rule ex_ex1I) 
   apply (simp add:add_set_single_nonempty2)
   apply (simp add:add_set_single2) 
  apply (rule theI') apply (rule ex_ex1I) 
   apply (simp add:add_set_single_nonempty1)
   apply (simp add:add_set_single1) 
done
  
lemma single_addition_pm:"commute_bpp f (aug_pm_set z i {(a::'a)}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z  (naf,i,z) f+ (maf,i,z) = (n + m)af,i,z⇙" 
apply (frule single_addition_pm_mem[of "z" "i" "a" "f" "n"], assumption+)
 apply (frule single_addition_pm_mem[of "z" "i" "a" "f" "m"], assumption+)
 apply (case_tac "n = 0") 
 apply (subgoal_tac "(naf,i,z) = z", simp)
 apply (simp add:zeroA_def, subst fg_elem_single_def, simp)
 apply (case_tac "m = 0")
 apply (simp add:aug_commute)
 apply (subgoal_tac " (0af,i,z) = z", simp)
 apply (simp add:zeroA_def, subst fg_elem_single_def, simp)
apply (frule_tac non_zero_int [of "n"], thin_tac "n  0") 
apply (frule_tac non_zero_int [of "m"], thin_tac "m  0")
apply (case_tac "0 < n", thin_tac "0 < n  n < 0")
 apply (case_tac "0 < m", thin_tac "0 < m  m < 0")
 apply (simp add:single_addition_pos) 
 apply (simp, thin_tac "maf,i,z addition_set f (aug_pm_set z i {a})") 
apply (simp add:s_a_p_1)
 apply simp
 apply (subst aug_commute, assumption+)
 apply (case_tac "0 < m", thin_tac "0 < m  m < 0")
 apply (simp add:s_a_p_1, simp add:add.commute)
apply simp
 apply (simp add:single_addition_neg, simp add:add.commute)
done

lemma single_inv:"commute_bpp f (aug_pm_set z i {(a::'a)}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z   i- (maf,i,z) = (-m)af,i,z⇙"
apply (frule single_addition_pm_mem[of "z" "i" "a" "f" "m"], assumption+,
       frule single_addition_pm_mem[of "z" "i" "a" "f" "- m"], assumption+,
       frule single_addition_pm[THEN sym, of "f" "z" "i" "a" "-m" "m"],
                                         assumption+, simp)
apply (simp add:single_addition_zero)
apply (subgoal_tac "z f+ (i- (maf,i,z)) =
            ((- m)af,i,z) f+ (maf,i,z) f+ (i- (maf,i,z))") prefer 2 apply simp
 apply (frule aug_ipp_closed [of "f" "z" "i" "{a}" "maf,i,z⇙"], assumption+)
 apply (thin_tac "z = ((- m)af,i,z) f+ (maf,i,z)")
 apply (simp add:addition_assoc)
 apply (simp add:aug_commute [of "f" "z" "i" "{a}" "(maf,i,z)" "i- (maf,i,z)"])
 apply (subgoal_tac "i- (maf,i,z) f+ (maf,i,z) = z") apply simp
 apply (subgoal_tac "z  addition_set f (aug_pm_set z i {a})")
 prefer 2 apply (simp add:addition_set_inc_z)
 apply (simp add:aug_commute[of "f" "z" "i" "{a}" "(- m)af,i,z⇙" "z"])
 apply (simp add:zeroA_def)
 apply (simp add:inv_ipp_def)
 apply (subgoal_tac "zeroA z i f {a} (i- (maf,i,z) f+ (maf,i,z))")
 prefer 2  apply simp
 apply (frule aug_zero_unique[of "f" "z" "i" "{a}" "i- (maf,i,z) f+ (maf,i,z)"])
 apply (rule aug_bpp_closed, assumption+)
 apply (rule sym) apply assumption
done

lemma free_ag_single:"commute_bpp f (aug_pm_set z i {a}); 
      assoc_bpp (aug_pm_set z i {a}) f; ipp_cond1 {a} i; ipp_cond2 z {a} i f; 
      ipp_cond3 z i; inv_ipp z i f {a}; commute_bpp f (aug_pm_set z i {a}); 
      zeroA z i f {a} z; free_gen_condition f i a z; n  m  
      (naf,i,z)  (maf,i,z)"
apply (rule contrapos_pp, simp+)
apply (frule single_addition_pm[THEN sym, of f z i a n "-m"], assumption+) 
apply simp
apply (frule single_addition_pm_mem[of "z" "i" "a" "f" "m"], assumption+)
 apply (frule single_addition_pm_mem[of "z" "i" "a" "f" "- m"], assumption+)
 apply (simp add:aug_commute [of "f" "z" "i" "{a}" "(maf,i,z)" "(- m)af,i,z⇙"])
 apply (simp add:single_inv[THEN sym, of "f" "z" "i" "a" "m"])
 apply (simp add:inv_aug_addition)
 apply (thin_tac "naf,i,z= maf,i,z⇙")
 apply (thin_tac " i- (maf,i,z)  addition_set f (aug_pm_set z i {a})")
 apply (thin_tac " maf,i,z addition_set f (aug_pm_set z i {a})")
 apply (insert int_neq_iff[of "n" "m"]) apply simp
 apply (case_tac "n < m") apply (thin_tac "n < m  m < n")
 apply (frule single_inv [THEN sym, of "f" "z" "i" "a" "n - m"], assumption+)
 apply simp apply (subgoal_tac "i- z = z") apply simp
 apply (thin_tac " i- z = z") prefer 2  apply (simp add:ipp_cond3_def)
 apply (subgoal_tac "0 < m - n") prefer 2 apply simp
 apply (thin_tac "(n - m)af,i,z= z")
 apply (simp add:fg_elem_single_def)
 apply (subgoal_tac "(THE x. x  add_set f {a} (nat (m - n - 1)))  add_set f {a} (nat (m - n - 1))") apply simp
 apply (thin_tac "(THE x. x  add_set f {a} (nat (m - n - 1))) = z")
 apply (simp add:free_gen_condition_def)
  apply (rule theI') apply (rule ex_ex1I)
  apply (simp add:add_set_single_nonempty1)
   apply (simp add:add_set_single1) apply simp
 apply (subgoal_tac "0 < n - m") prefer 2 apply simp 
 apply (simp add:fg_elem_single_def)
 apply (subgoal_tac "(THE x. x  add_set f {a} (nat (n - m - 1)))  add_set f {a} (nat (n - m - 1))") apply simp
 apply (thin_tac "(THE x. x  add_set f {a} (nat (n - m - 1))) = z")
apply (simp add:free_gen_condition_def)
  apply (rule theI') apply (rule ex_ex1I)
  apply (simp add:add_set_single_nonempty1)
   apply (simp add:add_set_single1) 
done

definition
  fags_cond :: "['a  'a  'a, 'a, 'a  'a, 'a]  bool" where
  "fags_cond f z i a  commute_bpp f (aug_pm_set z i {a})  
       assoc_bpp (aug_pm_set z i {a}) f  ipp_cond1 {a} i  
       ipp_cond2 z {a} i f   ipp_cond3 z i  inv_ipp z i f {a}  
       commute_bpp f (aug_pm_set z i {a})   zeroA z i f {a} z  
       free_gen_condition f i a z"

lemma fag_single_free:"fags_cond f z i a; n  m  (naf,i,z)  (maf,i,z)"
apply (simp add:fags_cond_def) apply (erule conjE)+
 apply (simp add:free_ag_single)
done

lemma fag_single_free1:"fags_cond f z i a;(naf,i,z) = (maf,i,z)  n = m"
apply (rule contrapos_pp, simp+)
apply (frule fag_single_free [of "f" "z" "i" "a" "n" "m"], assumption+)
apply simp
done

definition
  fags_carr :: "['a  'a  'a, 'a, 'a  'a, 'a]  'a set" where
  "fags_carr f z i a = {x. n. x = naf,i,z}" 

definition
  fags_bpp :: "['a  'a  'a, 'a, 'a  'a, 'a]  'a  'a  'a" where
  "fags_bpp f z i a = (λx(fags_carr f z i a). λy(fags_carr f z i a).
        ((THE n. x = naf,i,z) + (THE m. y = maf,i,z))af,i,z)"

definition
  fags_ipp :: "['a  'a  'a, 'a, 'a  'a, 'a]  'a  'a" where
  "fags_ipp f z i a = (λx(fags_carr f z i a). 
                                   (- (THE n. x = naf,i,z))af,i,z)"

lemma fags_mem:"fags_cond f z i a  (naf,i,z)  fags_carr f z i a" 
apply (simp add:fags_carr_def) 
apply blast
done

lemma fags_ippTr:"fags_cond f z i a  
                  fags_ipp f z i a (naf,i,z) = (- n)af,i,z⇙"
apply (subgoal_tac "(naf,i,z)   fags_carr f z i a") 
apply (simp add:fags_ipp_def)
 apply (subgoal_tac "naf,i,z= (THE na. naf,i,z= naaf,i,z)af,i,z⇙")
 apply (frule_tac m = "THE na. naf,i,z= naaf,i,z⇙" in 
        fag_single_free1[of "f" "z" "i" "a" "n"], assumption+, simp)
 apply (rule theI', rule ex_ex1I, blast)
 apply (simp add:fag_single_free1 [of f z i a])
 apply (simp add:fags_mem)
done

lemma fags_bppTr:"fags_cond f z i a  
                  fags_bpp f z i a (naf,i,z) (maf,i,z) = (n + m)af,i,z⇙"
apply (subgoal_tac "(naf,i,z)   fags_carr f z i a") 
apply (subgoal_tac "(maf,i,z)   fags_carr f z i a")
apply (simp add:fags_bpp_def)
 apply (subgoal_tac "naf,i,z= (THE na. naf,i,z= naaf,i,z)af,i,z⇙")
 apply (frule_tac m = "THE na. naf,i,z= naaf,i,z⇙" in 
                  fag_single_free1 [of "f" "z" "i" "a" "n"], assumption+)
 apply (subgoal_tac "maf,i,z= (THE ma. maf,i,z= maaf,i,z)af,i,z⇙")
 apply (frule_tac m = "THE ma. maf,i,z= maaf,i,z⇙" in 
                  fag_single_free1 [of f z i a m], assumption+)
 apply simp
 apply (rule theI', rule ex_ex1I, blast) 
 apply (simp add:fag_single_free1 [of f z i a])
 apply (rule theI', rule ex_ex1I, blast)
 apply (simp add:fag_single_free1 [of f z i a])
apply (simp add:fags_carr_def, blast)
apply (simp add:fags_carr_def, blast)
done 

definition
  fags :: "['a  'a  'a, 'a, 'a  'a, 'a]  'a aGroup" where
  "fags f z i a = carrier = fags_carr f z i a, 
                   pop = fags_bpp f z i a, 
                   mop = fags_ipp f z i a, zero = z"

lemma fags_ag:"fags_cond f z i a  aGroup (fags f z i a)"
apply (rule aGroup.intro)
 apply (rule Pi_I)+
 apply (simp add:fags_def fags_carr_def)
 apply ((erule exE)+, simp)
  apply (simp add:fags_bppTr)
  apply blast

 apply (simp add:fags_def, simp add:fags_carr_def,
        (erule exE)+, simp)
 apply (simp add:fags_bppTr, simp add:add.assoc)

 apply (simp add:fags_def, simp add:fags_carr_def,
        (erule exE)+, simp)
 apply (simp add:fags_bppTr, simp add:add.commute)

 apply (rule Pi_I)
 apply (simp add:fags_def, simp add:fags_carr_def, erule exE, simp)
 apply (simp add:fags_ippTr, blast)

 apply (simp add:fags_def, simp add:fags_carr_def, erule exE, simp)
 apply (simp add:fags_ippTr fags_bppTr,
        simp add:fags_cond_def single_addition_zero[of f z i a])

 apply (simp add:fags_def fags_carr_def,
        simp add:fags_cond_def, (erule conjE)+,
        frule  single_addition_zero, assumption+, rotate_tac -1, frule sym,
        blast)

 apply (simp add:fags_def fags_carr_def, erule exE) 
 apply (subgoal_tac "fags_bpp f z i a z aa = fags_bpp f z i a (0af,i,z) aa",
        simp add:fags_bppTr,
        thin_tac "aa = naf,i,z⇙")
 apply (cut_tac single_addition_zero[of f z i a], simp)
  apply (simp add:fags_cond_def)+
done

section "Abelian Group generated by one element II (nonconstructive)"

definition
  ag_single_gen :: "[('a, 'm) aGroup_scheme, 'a]  bool" where
  "ag_single_gen A a  aGroup A  carrier A =  {H. asubGroup A H  a  H}"


primrec aSum :: "[('a, 'm) aGroup_scheme, nat,'a]   'a" where
  aSum_0: "aSum A 0 a = 𝟬A⇙"
| aSum_Suc: "aSum A (Suc n) a = aSum A n a ±Aa"

definition
  sprod_n_a ::"[('a, 'm) aGroup_scheme, int, 'a]   'a" where
  "sprod_n_a A n x = (if 0  n then (aSum A (nat n) x) 
                      else (aSum A (nat (- n)) (-aAx)))"

abbreviation
  SPRODNA  ((3___) [95,95,96]95) where
  "naA== sprod_n_a A n a"

lemma (in aGroup) asum_mem:"a  carrier A  aSum A n a  carrier A"
apply (induct_tac n)
 apply simp apply (simp add:ag_inc_zero)
 apply simp
 apply (rule ag_pOp_closed, assumption+)
done

lemma (in aGroup) nt_mem0:"a  carrier A  naA carrier A"
apply (case_tac "n = 0", simp add:sprod_n_a_def)
 apply (simp add:ag_inc_zero)
 apply (frule non_zero_int[of "n"])
 apply (case_tac "0 < n") 
 apply (simp add:sprod_n_a_def)
 apply (simp add:asum_mem, thin_tac "n  0", simp)
apply (simp add:sprod_n_a_def)
 apply (rule asum_mem)
 apply (simp add:ag_mOp_closed)
done

lemma (in aGroup) nt_zero0:"a  carrier A  0aA= 𝟬"
apply (simp add:sprod_n_a_def)
done

lemma (in aGroup) nt_1:"a  carrier A  1aA= a"
apply (simp add:sprod_n_a_def)
apply (simp add:ag_l_zero)
done

lemma (in aGroup) asumTr:"a  carrier A  
              aSum A (n + m) a = aSum A n a ± (aSum A m a)"
apply (induct_tac m)
 apply simp 
 apply (frule asum_mem[of a n])
apply (rule ag_r_zero [THEN sym, of "aSum A n a"], assumption+)
 apply (subgoal_tac "n + Suc na = Suc (n + na)", simp)
 apply (rule ag_pOp_assoc)
 apply (simp add:asum_mem)+
done

lemma (in aGroup) aSum_zero:"a  carrier A  aSum A n 𝟬 = 𝟬"
apply (induct_tac n) 
 apply (simp, simp, rule ag_r_zero)
 apply (simp add:ag_inc_zero)
done

lemma (in aGroup)  agsum_add1p:" a  carrier A; 0  n; 0  m 
                      (n + m)aA= naA± (maA)"
apply (simp add:sprod_n_a_def)
apply (subst nat_add_distrib[of "n" "m"], assumption+)
apply (simp add:asumTr)
done

lemma (in aGroup)  agsum_add1m:" a  carrier A; n < 0; m < 0 
                      (n + m)aA= naA± (maA)"
apply (simp add:sprod_n_a_def) 
 apply (subst zdiff)
 apply (subgoal_tac "0  -n") apply (thin_tac "n < 0")
 apply (subgoal_tac "0  -m") apply (thin_tac "m < 0")
 apply (subst nat_add_distrib [of "-n" "-m"], assumption+)
 apply (rule asumTr)
 apply (simp add:ag_mOp_closed) apply simp+
done

lemma (in aGroup) agsum_add2Tr:"a  carrier A  
                𝟬  = aSum A n a ± (aSum A n (-a a))"
apply (induct_tac n)
 apply simp
 apply (cut_tac ag_inc_zero)
 apply (simp add:ag_r_zero[THEN sym]) 
apply simp
 apply (frule  ag_mOp_closed[of a])
 apply (frule_tac n = n in asum_mem [of "-a a"])
 apply (frule_tac x = "aSum A n (-a a)" in ag_pOp_closed [of  _ "-a a"],
        assumption)
 apply (frule_tac n = n in asum_mem [of a])
 apply (subst ag_pOp_assoc[of  _ "a"], assumption+)
 apply (subst ag_pOp_assoc[THEN sym, of a _ "-a a"], assumption+)
 apply (subst ag_pOp_commute[of a], assumption+)
 apply (subst ag_pOp_assoc[of  _ a "-a a"], assumption+)
apply (frule sym) apply (thin_tac "𝟬 =  aSum A n a ± (aSum A n (-a a))")
apply (simp add:ag_r_inv1[of a])
 apply (subst ag_r_zero, assumption+) apply simp
done

lemma (in aGroup) agsum_add2p:"a  carrier A; 0  n 
                                    𝟬 = naA± ((-n)aA)"
apply (case_tac "n = 0") 
 apply (simp add:sprod_n_a_def)
 apply (cut_tac ag_inc_zero)
 apply (simp add:ag_r_zero[THEN sym])
apply (frule non_zero_int[of "n"], thin_tac "n  0", simp)
 apply (subgoal_tac "-n < 0") prefer 2 apply simp
apply (simp add:sprod_n_a_def)
 apply (simp add:agsum_add2Tr)
done
 
lemma (in aGroup) agsum_add2m:"a  carrier A; n < 0 
                                    𝟬 = naA± ((-n)aA)"
apply (simp add:sprod_n_a_def)
apply (subst ag_pOp_commute)
 apply (rule asum_mem)
 apply (simp add:ag_mOp_closed)
 apply (rule asum_mem, assumption+)
 apply (simp add:agsum_add2Tr)
done

lemma (in aGroup) agsum_add3pm:"a  carrier A; 0 < n; m < 0 
                        (n + m)aA= naA± (maA)"
apply (cut_tac less_linear[of n "- m"])
apply (case_tac "n = -m") 
 apply simp
 apply (subst ag_pOp_commute)
 apply (simp add:nt_mem0)+
 apply (subgoal_tac "0aA= 𝟬") apply (simp add:agsum_add2m)
apply (simp add:sprod_n_a_def) apply simp
apply (case_tac "n < -m")
 apply (subst zminus_zadd_cancel[THEN sym, of "m" "n"])
 apply (subgoal_tac "-n < 0") prefer 2 apply simp  (** atode shiraberu **)
 apply (subgoal_tac "n + m < 0") prefer 2 apply simp  (** atode shiraberu *)
 apply simp
 apply (cut_tac agsum_add1m [of "a" "-n" "n + m"])
 apply simp
 apply (subst ag_pOp_assoc [THEN sym])
 apply (simp add:nt_mem0)+
 apply (subst agsum_add2p [THEN sym, of a n], assumption+)
 apply simp 
 apply (rule ag_l_zero[THEN sym, of  "(n + m)aA⇙"])
 apply (simp add:nt_mem0, assumption) apply arith apply assumption
 apply (thin_tac "n  - m") apply simp
apply (subst zminus_zadd_cancel[THEN sym, of "n" "m"])
 apply (frule zminus_minus_pos[of m], frule zless_imp_zle[of 0 "- m"])
 apply (subgoal_tac "0  n + m") prefer 2 apply simp  
 apply (frule agsum_add1p [of "a" "-m" "n + m"], assumption+)
 apply simp apply (thin_tac "naA=  (- m)aA± (n + m)aA⇙")
 apply (subst ag_pOp_commute[of "(- m)aA⇙" "(n + m)aA⇙"])
 apply (simp add:nt_mem0)+
 apply (subst ag_pOp_assoc)
 apply (simp add:nt_mem0)+
 apply (subst ag_pOp_commute [of "(- m)aA⇙" "maA⇙"])
 apply (simp add:nt_mem0)+
 apply (simp add:agsum_add2m[THEN sym])
apply (rule ag_r_zero[THEN sym])
 apply (simp add:nt_mem0)
done
 
lemma (in aGroup)  agsum_add3mp:" a  carrier A; n < 0; 0 < m 
                        (n + m)aA= naA± (maA)"
apply (simp add:add.commute)
apply (subst ag_pOp_commute, (simp add:nt_mem0)+)
apply (simp add:agsum_add3pm)
done

lemma (in aGroup)  nt_sum0:" a  carrier A  (n + m)aA= naA± (maA)"
apply (cut_tac less_linear[of n 0])
 apply (case_tac "n < 0")
  apply (cut_tac less_linear[of m 0])
  apply (case_tac "m < 0", simp add:agsum_add1m, simp)
  apply (thin_tac "¬ m < 0")
  apply (case_tac "m = 0", simp)
  apply (simp add:nt_zero0)
  apply (rule ag_r_zero[THEN sym], simp add:nt_mem0)
  apply (simp, simp add:agsum_add3mp, simp)
  apply (thin_tac "¬ n < 0")
 apply (case_tac "n = 0", simp, simp add:nt_zero0)
  apply (rule ag_l_zero[THEN sym], simp add:nt_mem0)
  apply simp
  apply (cut_tac less_linear[of m 0])
  apply (case_tac "m < 0")
  apply (simp add:agsum_add3pm, simp, thin_tac "¬ m < 0")
   apply (case_tac "m = 0", simp, simp add:nt_zero0)
   apply (rule ag_r_zero[THEN sym], simp add:nt_mem0)
   apply simp
  apply (simp add:agsum_add1p)
done

lemma (in aGroup)  nt_inv0:"a  carrier A  -a (naA) = (- n)aA⇙"
apply (subgoal_tac "(n + -n)aA= naA± ((-n)aA)")
 prefer 2 apply (rule nt_sum0, assumption+) apply (simp add:nt_zero0)
 apply (subgoal_tac "-a (naA) ± 𝟬 = -a (naA) ± (naA± (- n)aA)")
 apply (subgoal_tac "naA carrier A") 
 apply (frule ag_mOp_closed [of "naA⇙"])
 apply (thin_tac "𝟬 =  naA± (- n)aA⇙")
 apply (simp add:ag_r_zero)
 apply (subgoal_tac "(- n)aA carrier A")
 apply (simp add:ag_pOp_assoc[THEN sym])
 apply (simp add:ag_l_inv1) apply (simp add:ag_l_zero)
 apply (simp add:nt_mem0)+
done

lemma (in aGroup) m_x_asum:" a  carrier A; b  carrier A 
         aSum A m (a ± b) = (aSum A m a) ± (aSum A m b)"
apply (induct_tac m) apply simp
 apply (rule ag_r_zero[THEN sym])
 apply (simp add:ag_inc_zero)
 apply simp
 apply (frule_tac n = n in asum_mem[of "a"])
 apply (frule_tac n = n in asum_mem[of  "b"])
 apply (frule_tac a = "aSum A n a" and c = "aSum A n b" in 
         pOp_assocTr43 [of  _ "a" _ "b"], assumption+) apply simp
 apply (frule_tac x = a and y = "aSum A n b" in ag_pOp_commute, assumption+) 
 apply simp
 apply (simp add:pOp_assocTr43[THEN sym])
done

lemma (in aGroup) asum_multTr_pp:"a  carrier A 
                  aSum A m (aSum A n a) = aSum A (m * n) a"
apply (induct_tac n)
 apply simp
 apply (induct_tac m, simp)
 apply (simp, rule ag_r_zero, simp add:ag_inc_zero)
 apply simp
 apply (frule_tac n = n in asum_mem[of a])
 apply (frule_tac a = "aSum A n a" and b = a and m= m in m_x_asum,
                                           assumption+, simp)
 apply (frule_tac n = m and m = "m * n" in asumTr [of a])
 apply simp
 apply (frule_tac n = "m * n" in asum_mem[of a])
 apply (frule_tac n = m in asum_mem[of "a"])
 apply (simp add:ag_pOp_commute)
done

lemma (in aGroup) nt_mult_pp:" a  carrier A; 0  m; 0  n 
                                    m(naA)A= (m * n)aA⇙"
apply (simp add:sprod_n_a_def)
 apply (subgoal_tac "0  m * n", simp)
 apply (simp add:asum_multTr_pp)
 apply (simp add:nat_mult_distrib)
 apply (frule int_mult_le [of "0" "m" "n"], assumption+)
 apply (simp add:mult.commute)
done

lemma (in aGroup) asum_multTr_pm:"a  carrier A; 0  m; n < 0  
       aSum A (nat m) (aSum A (nat (- n)) (-a a)) = 
                                    aSum A (nat (m * (- n))) (-a a)"
apply (frule ag_mOp_closed [of  a])
 apply (simp add:asum_multTr_pp)
 apply (subgoal_tac "nat m * nat (- n) = nat (- (m * n))", simp)
 apply (subgoal_tac "(nat m) * (nat (- n)) = nat (m * (- n))", simp)
 apply simp
apply (subst zmult_zminus_right[THEN sym, of "m" "n"])
 apply (rule nat_mult_distrib [THEN sym, of "m"], assumption+)
done

lemma (in aGroup) nt_mult_pm:"a  carrier A; 0  m; n < 0  
                        m(naA)A= (m * n)aA⇙"
apply (frule zmult_zle_mono1_neg [of "0" "m" "n"])
 apply (simp add:zless_imp_zle, simp)
 apply (simp add:sprod_n_a_def)
 apply (rule impI) 
 apply (simp add: asum_multTr_pm)
done

lemma (in aGroup) asum_multTr_mp:"a  carrier A; m < 0; 0  n  
 aSum A (nat (-m))(-a (aSum A (nat n) a)) = aSum A (nat ((- m) * n)) (-a a)"
apply (frule asum_mem [of  "a" "nat n"])
apply (frule ag_mOp_closed [of  "aSum A (nat n) a"])
apply (simp add:sprod_n_a_def)
apply (subgoal_tac "-a (aSum A (nat n) a) = aSum A (nat n) (-a a)")
 apply simp 
 apply (subst asum_multTr_pp)
 apply (simp add:ag_mOp_closed)
 apply (subgoal_tac "(nat (- m)) * (nat n) = nat ((- m) * n)", simp)
 apply (subst nat_mult_distrib, simp, simp)
 apply (frule nt_inv0 [of  "a" "n"])
 apply (simp add:sprod_n_a_def)
done

lemma (in aGroup) nt_mult_mp:"a  carrier A; m < 0; 0  n  
                        m(naA)A= (m * n)aA⇙" 
apply (simp add:sprod_n_a_def)
 apply (cut_tac zless_imp_zle[of m 0])
 apply (frule int_mult_le [of "m" "0" "n"], assumption, simp) 
 apply (case_tac "0  m * n", simp)
 apply (frule zle_imp_zless_or_eq [of "0" "m * n"])  
 apply (thin_tac "0  m * n", simp add:zle mult.commute)
 apply (simp add:ag_inv_zero, simp add:aSum_zero)
apply simp
 apply (simp add:asum_multTr_mp)
 apply (simp add:zle_imp_zless_or_eq)
done

lemma (in aGroup) asum_multTr_mm:"a  carrier A; m < 0; n < 0  
       aSum A (nat (-m))(-a (aSum A (nat (- n)) (-a a))) = 
                                   aSum A (nat ((- m) * (- n))) a"
apply (simp add:sprod_n_a_def)
apply (subgoal_tac "-a (aSum A (nat (- n)) (-a a)) = aSum A (nat (- n)) a")
 apply simp
 apply (simp add:asum_multTr_pp)
 apply (subst nat_mult_distrib[THEN sym]) apply simp
 apply simp
 apply (frule ag_mOp_closed [of  "a"])
 apply (frule nt_inv0 [of  "-a a" "- n"])
 apply (simp add:sprod_n_a_def)
 apply (simp add:ag_inv_inv)
done

lemma (in aGroup)  nt_mult_mm:" a  carrier A; m < 0; n < 0  
                     m(naA)A= (m * n)aA⇙"
apply (simp add:sprod_n_a_def)
apply (subgoal_tac "0  m * n") apply simp
 apply (simp add:asum_multTr_mm)
 apply (frule zmult_neg_neg[of "m" "n"], assumption+)
 apply (simp add:zle_imp_zless_or_eq)
done

lemma (in aGroup)  nt_mult_assoc0:"a  carrier A  mnaA⇙⇘A= (m * n)aA⇙"
apply (case_tac "0  n")
 apply (case_tac "0  m")
  apply (simp add:nt_mult_pp,  simp add:zle)
  apply (frule nt_mult_mp[of a m n], assumption, simp, simp)
  apply (cut_tac less_linear[of 0 m])
 apply (case_tac "0  m")
 apply (simp add:nt_mult_pm)
apply (simp add:zle)
 apply (simp add:nt_mult_mm)
done

lemma (in aGroup) single_gen_carrTr:"a  carrier A 
                               asubGroup A {x. n. x = (naA)}"
apply (rule asubg_test)
 apply (rule subsetI, simp)
 apply (erule exE, simp add:nt_mem0)
 apply (simp, blast)
apply ((rule ballI)+, simp)
 apply (erule exE)+
 apply (simp add:nt_inv0)
 apply (subst nt_sum0[THEN sym], assumption+)
 apply blast
done

lemma (in aGroup) ag_single_inc_a:"ag_single_gen A a  a  carrier A"
apply (simp add:ag_single_gen_def)
done

lemma (in aGroup) single_gen:"ag_single_gen A a  
                           carrier A = {g. n. g = (naA)}" 
apply (rule equalityI)
 apply (frule ag_single_inc_a [of  "a"])
 apply (rule subsetI, simp)
apply (unfold ag_single_gen_def, erule conjE)
apply (frule single_gen_carrTr [of  "a"])
apply (subgoal_tac "a  {x. n. x = naA}")
 apply (subgoal_tac "{H. A +> H  a  H}  {x. n. x = naA}")
 apply (frule_tac A = "{H. A +> H  a  H}" and B = "{x. n. x = naA}" and
         c = x in  subsetD) 
 apply (frule sym, thin_tac "carrier A = {H. A +> H  a  H}") 
 apply (simp, simp)
 apply (thin_tac "carrier A = {H. A +> H  a  H}")
 apply (rule subsetI, blast)
apply (thin_tac "carrier A = {H. A +> H  a  H}",
       thin_tac "A +> {x. n. x = naA}")
 apply (subgoal_tac "a = 1aA⇙", blast) 
 apply (simp add:sprod_n_a_def, simp add:ag_l_zero[THEN sym])
apply (fold ag_single_gen_def)
  apply (frule ag_single_inc_a [of  "a"])
 apply (unfold ag_single_gen_def)
 apply (erule conjE) 
 apply (thin_tac "carrier A = {H. A +> H  a  H}")
 apply (rule subsetI, simp)
 apply (erule exE)
 apply (simp, simp add:nt_mem0)
done 

definition
  single_gen_free :: "[('a, 'm) aGroup_scheme, 'a]  bool" where
  "single_gen_free A a == n. n  0  𝟬A naA⇙"

definition
  sfg :: "[('a, 'm) aGroup_scheme, 'a]  bool" where
  "sfg A a  ag_single_gen A a  single_gen_free A a"
  (** single free generated by a **)  

lemma (in aGroup) single_gen_free_neg:"sfg A a; naA= 𝟬  n = 0" 
apply (simp add:sfg_def, erule conjE)
apply (rule contrapos_pp, simp+)
apply (simp add:single_gen_free_def)
 apply (drule_tac a = n in forall_spec, simp)
 apply simp
done

lemma (in aGroup) sfg_G_inc_a:"sfg A a  a  carrier A"
apply (simp add:sfg_def ag_single_inc_a)
done

lemma sfg_agroup:"sfg A a  aGroup A"
apply (simp add:sfg_def ag_single_gen_def)
done

lemma (in aGroup) mem_G_nt:"sfg A a; x  carrier A  n. x = naA⇙"
apply (simp add:sfg_def)  apply (erule conjE)
 apply (frule single_gen [of  "a"]) apply simp
done

lemma (in aGroup) nt_mem:"sfg A a  naA carrier A"
apply (frule sfg_G_inc_a)
apply (frule sfg_agroup)
apply (simp add:nt_mem0)
done

lemma (in aGroup) nt_zero:"sfg A a  0aA= 𝟬"
apply (frule sfg_G_inc_a)
apply (frule sfg_agroup)
apply (simp add:nt_zero0)
done

lemma (in aGroup) nt_sum:"sfg A a  (n + m)aA= naA± (maA)"
apply (frule sfg_G_inc_a)
apply (frule sfg_agroup)
apply (simp add:nt_sum0)
done

lemma (in aGroup) nt_inv:"sfg A a  -a(naA) = (- n)aA⇙"
apply (frule sfg_G_inc_a)
apply (frule sfg_agroup)
apply (simp add:nt_inv0)
done

lemma (in aGroup) nt_mult_assoc:"sfg A a  mnaA⇙⇘A= (m * n)aA⇙"
apply (frule sfg_G_inc_a)
apply (frule sfg_agroup )
apply (simp add:nt_mult_assoc0)
done
 
lemma (in aGroup) sfg_free:"sfg A a; n  m   naA (maA)"
apply (rule contrapos_pp, simp+)
apply (frule sfg_G_inc_a)
apply (frule sfg_agroup )
 apply (frule nt_mem [of  "a" "m"])
 apply (frule nt_mem [of  "a" "n"])
 apply (subgoal_tac "naA± (-a (maA)) = 𝟬")
 apply (thin_tac "naA= maA⇙")  (*  remove this equation *)
 apply (simp add:nt_inv)
 apply (simp add:nt_sum[THEN sym])
 apply (frule single_gen_free_neg[of  "a" "n - m"], assumption+)
 apply simp 
apply (simp add:ag_r_inv1)
done

lemma (in aGroup) sfg_free_inj:"sfg A a; naA= (maA)   n = m"
apply (rule contrapos_pp, simp+)
apply (simp add:sfg_free)
done

section "Free Generated Modules (constructive)"

definition
  sop_one::"[('r, 'm) Ring_scheme, 'r  'a  'a, 'a set]  bool" where
  "sop_one R s A  (xA. (1rR) s x = x)"

definition
  sop_assoc :: "[('r, 'm) Ring_scheme, 'r  'a  'a, 'a set]  bool" where
  "sop_assoc R s A  (acarrier R. bcarrier R. xA.
                         (a rRb) s x = a s (b s x))"

definition
  sop_inv :: "[('r, 'm) Ring_scheme, 'r  'a  'a, 'a  'a, 'a set] 
       bool" where
  "sop_inv R s i A  (rcarrier R. xA. r s (i- x) = (-aRr) s x)"

definition
  sop_distr1 :: "[('r, 'm) Ring_scheme, 'r  'a  'a, 'a  'a  'a,
    'a  'a, 'a set, 'a]  bool" where
  "sop_distr1 R s f i A z  (acarrier R. bcarrier R. 
          x(aug_pm_set z i A). (a ±Rb) s x = (a s x) f+ (b s x))"

definition
  sop_distr2 :: "[('r, 'm) Ring_scheme, 'r  'a  'a, 'a  'a  'a, 
                'a  'a, 'a set, 'a]  bool" where
  "sop_distr2 R s f i A z  (acarrier R. 
         xaddition_set f (aug_pm_set z i A). 
           yaddition_set f (aug_pm_set z i A). 
                 a s (x f+ y) = (a s x) f+ (a s y))"

definition
  sop_z :: "[('r, 'm) Ring_scheme, 'r  'a  'a, 'a]  bool" where
  "sop_z R s z  (rcarrier R. r s z = z)"

definition
  fgmodule :: "[('r, 'm) Ring_scheme, 'a set, 'a, 'a  'a, 'a  'a  'a, 
      'r  'a  'a]  ('a, 'r) Module" where
  "fgmodule R A z i f s =
     carrier = addition_set f (aug_pm_set z i (s_set R s A)), 
       pop = λxaddition_set f (aug_pm_set z i (s_set R s A)). 
               λyaddition_set f (aug_pm_set z i (s_set R s A)). x f+ y, 
       mop = λxaddition_set f (aug_pm_set z i (s_set R s A)). i- x, 
       zero = z, 
       sprod = λrcarrier R. 
                 λxaddition_set f (aug_pm_set z i (s_set R s A)). r s x "

lemma fgmodule_carr:"carrier (fgmodule R A z i f s) = 
             addition_set f (aug_pm_set z i (s_set R s A))"
by (simp add:fgmodule_def)

lemma a_in_s_set:"a  A  a  s_set R s A"
by (simp add:s_set_def)

lemma (in Ring) ra_in_s_set:"r  carrier R; a  A  r s a  s_set R s A" 
by (simp add:s_set_def, blast)

lemma in_aug_pm_set:
       "x  aug_pm_set z i A = (x = z  x  A  x  minus_set i A)"
by (simp add:aug_pm_set_def)

lemma (in Ring) in_s_set:"x  s_set R s A  (r  carrier R. a  A. 
      x = r s a )  x  A" 
by (simp add:s_set_def)

lemma (in Ring) sop_closedTr0:"ipp_cond1 (s_set R s A) i; 
       ipp_cond2 z (s_set R s A) i f; ipp_cond3 z i; 
       inv_ipp z i f (s_set R s A); zeroA z i f (s_set R s A) z; 
       sop_distr2 R s f i (s_set R s A) z; 
       sop_assoc R s (aug_pm_set z i (s_set R s A)); 
       sop_inv R s i (s_set R s A); 
       sop_one R s (aug_pm_set z i (s_set R s A)); sop_z R s z;  
       r  carrier R; x  aug_pm_set z i (s_set R s A)  
                        r s x  aug_pm_set z i (s_set R s A)"
apply (simp add:in_aug_pm_set)
apply (case_tac "x = z", simp, simp add:sop_z_def)

apply (case_tac "x  s_set R s A", simp add:s_set_def, fold s_set_def)
 apply (case_tac "x  A", simp, blast)

apply simp
apply ((erule bexE)+, simp)
 apply (simp add:sop_assoc_def)
 apply (drule_tac x = r in bspec, assumption,
        drule_tac x = ra in bspec, assumption,
        frule_tac x = a in bspec,
        subst in_aug_pm_set, simp add:a_in_s_set)
 apply (rotate_tac -1, frule sym, thin_tac "r r ra s a = r s (ra s a)",
        simp)
 apply (frule_tac x = r and y = ra in ring_tOp_closed, assumption+, blast)

apply simp apply (thin_tac "x  z", thin_tac "x  s_set R s A")
apply (simp add:minus_set_def, erule bexE, simp)
 apply (simp add:sop_inv_def[of R s i "s_set R s A"])
 apply (cut_tac ring_is_ag,
        frule_tac x = r in aGroup.ag_mOp_closed[of R], assumption)
 apply (frule_tac a = y in ra_in_s_set[of "-a r" _ "s_set R s A" s],
         assumption+)
 apply (frule_tac x = y in in_s_set[of _ s "A"])
 apply (case_tac "y  A", simp, simp add:ra_in_s_set)

apply simp apply ((erule bexE)+, simp)
 apply (simp add:sop_assoc_def)
 apply (drule_tac x = "-a r" in bspec, assumption,
        thin_tac "rcarrier R. xs_set R s A. r s (i- x) = (-a r) s x",
        drule_tac x = ra in bspec, assumption,
        drule_tac x = a in bspec,
                simp add:aug_pm_set_def, simp add:a_in_s_set) 
 apply (rotate_tac -1, frule sym, 
              thin_tac "(-a r) r ra s a = (-a r) s (ra s a)", simp,
        frule_tac x = "-a r" and y = ra in ring_tOp_closed, assumption,
              simp add:ra_in_s_set)
done

lemma (in Ring) sop_closedTr:"ipp_cond1 (s_set R s A) i; 
    ipp_cond2 z (s_set R s A) i f; ipp_cond3 z i; 
     inv_ipp z i f (s_set R s A); zeroA z i f (s_set R s A) z; 
      sop_distr2 R s f i (s_set R s A) z; 
       sop_assoc R s (aug_pm_set z i (s_set R s A)); 
        sop_inv R s i (s_set R s A); 
         sop_one R s (aug_pm_set z i (s_set R s A)); sop_z R s z 
    rcarrier R. xadd_set f (aug_pm_set z i (s_set R s A)) n. 
                  r s x  add_set f (aug_pm_set z i (s_set R s A)) n"
apply (induct_tac n)  
 apply (simp, (rule ballI)+, simp add:sop_closedTr0)

apply (rule ballI)+ apply simp
 apply (erule bexE)+ 
 apply (frule_tac r = r and x = t in sop_closedTr0 [of s A i z f], 
         assumption+)

 apply (subgoal_tac "sa  addition_set f (aug_pm_set z i (s_set R s A))")
 apply (subgoal_tac "t  addition_set f (aug_pm_set z i (s_set R s A))") 
 apply (simp add:sop_distr2_def) apply blast
 apply (cut_tac addition_inc_add0[of "aug_pm_set z i (s_set R s A)" f])
 apply (simp add:subsetD)
 apply (cut_tac n = n in addition_inc_add[of f
                                         "aug_pm_set z i (s_set R s A)"])
 apply (simp add:subsetD)
done

lemma (in Ring) sop_closed:"ipp_cond1 (s_set R s A) i; 
     ipp_cond2 z (s_set R s A) i f; ipp_cond3 z i; 
      inv_ipp z i f (s_set R s A); zeroA z i f (s_set R s A) z; 
       sop_distr2 R s f i (s_set R s A) z; 
        sop_assoc R s (aug_pm_set z i (s_set R s A)); 
         sop_inv R s i (s_set R s A); 
          sop_one R s (aug_pm_set z i (s_set R s A)); sop_z R s z 
     rcarrier R. xaddition_set f (aug_pm_set z i (s_set R s A)). 
          r s x  addition_set f (aug_pm_set z i (s_set R s A))"
apply (subst addition_set_def)
 apply simp
 apply (rule ballI) apply (rule allI) apply (rule impI)
 apply (erule exE)
 apply (rule ballI) apply simp
 apply (thin_tac "y = add_set f (aug_pm_set z i (s_set R s A)) n")
 apply (frule_tac n = n in sop_closedTr[of s A i z f], assumption+)
 apply (drule_tac x = r in bspec, assumption,
        drule_tac x = x in bspec, assumption)

 apply (cut_tac n = n in addition_inc_add [of f
                                  "aug_pm_set z i (s_set R s A)"],
        simp add:subsetD)
done

lemma (in Ring) sop_oneTr:"commute_bpp f (aug_pm_set z i (s_set R s A)); 
  assoc_bpp (aug_pm_set z i (s_set R s A)) f; 
   ipp_cond1 (s_set R s A) i; ipp_cond2 z (s_set R s A) i f; 
    ipp_cond3 z i; inv_ipp z i f (s_set R s A); zeroA z i f (s_set R s A) z; 
     sop_distr2 R s f i (s_set R s A) z; 
      sop_assoc R s (aug_pm_set z i (s_set R s A)); 
       sop_one R s (aug_pm_set z i (s_set R s A))   
   xadd_set f (aug_pm_set z i (s_set R s A)) n.  (1r) s x = x"
apply (induct_tac n)
 apply (rule ballI, simp, simp add:sop_one_def)

apply (rule ballI) 
 apply (simp, (erule bexE)+, simp)
 apply (subgoal_tac "t  addition_set f (aug_pm_set z i (s_set R s A))")
 apply (subgoal_tac "sa  addition_set f (aug_pm_set z i (s_set R s A))")
 apply (cut_tac ring_one)
 apply (simp add:sop_distr2_def)
 apply (thin_tac "xadd_set f (aug_pm_set z i (s_set R s A)) n. (1r) s x 
                       = x") apply (simp add:sop_one_def)
 apply (cut_tac n = n in addition_inc_add[of f
                     "aug_pm_set z i (s_set R s A)"], simp add:subsetD,
        cut_tac addition_inc_add0[of "aug_pm_set z i (s_set R s A)" f],
                           simp add:subsetD)
done

lemma (in Ring) sop_one:"commute_bpp f (aug_pm_set z i (s_set R s A)); 
   assoc_bpp (aug_pm_set z i (s_set R s A)) f; ipp_cond1 (s_set R s A) i; 
    ipp_cond2 z (s_set R s A) i f; ipp_cond3 z i; 
     inv_ipp z i f (s_set R s A); zeroA z i f (s_set R s A) z; 
      sop_distr2 R s f i (s_set R s A) z; 
       sop_assoc R s (aug_pm_set z i (s_set R s A)); 
        sop_one R s (aug_pm_set z i (s_set R s A))   
   xaddition_set f (aug_pm_set z i (s_set R s A)). (1r) s x = x"
apply (rule ballI) apply (simp add:addition_set_def)
apply (erule exE, erule conjE, erule exE, simp,
       thin_tac "xa = add_set f (aug_pm_set z i (s_set R s A)) n")
apply (simp add:sop_oneTr)
done

lemma (in Ring) sop_assocTr:"ipp_cond1 (s_set R s A) i; 
      ipp_cond2 z (s_set R s A) i f; ipp_cond3 z i; 
      inv_ipp z i f (s_set R s A); zeroA z i f (s_set R s A) z; 
      sop_distr2 R s f i (s_set R s A) z; 
      sop_assoc R s (aug_pm_set z i (s_set R s A)); 
      sop_inv R s i (s_set R s A); 
      sop_one R s (aug_pm_set z i (s_set R s A)); sop_z R s z  
     acarrier R. bcarrier R. 
      xadd_set f (aug_pm_set z i (s_set R s A)) n.  
                         a s ( b s x) = (a r b) s x"
apply (induct_tac n)
apply (rule ballI)+
apply (simp add:sop_assoc_def)
apply (rule ballI)+ apply simp
apply (erule bexE)+
 apply (drule_tac x = a in bspec, assumption,
        drule_tac x = b in bspec, assumption,
        drule_tac x = sa in bspec, assumption)
      
 apply simp
 apply (cut_tac n = n in addition_inc_add[of f
                     "aug_pm_set z i (s_set R s A)"],
        cut_tac addition_inc_add0[of "aug_pm_set z i (s_set R s A)" f])
 apply (frule_tac c = sa and A = "add_set f (aug_pm_set z i (s_set R s A)) n"
         in subsetD[of _ "addition_set f (aug_pm_set z i (s_set R s A))"],
         assumption+,
        frule_tac c = t in subsetD[of "aug_pm_set z i (s_set R s A)"
         "addition_set f (aug_pm_set z i (s_set R s A))"], assumption+,
        frule_tac x = a and y = b in  ring_tOp_closed, assumption+)
 apply (simp add:sop_distr2_def[of R s f i "s_set R s A" z]) 
 apply (frule sop_closed[of s A i z f], assumption+, simp add:sop_distr2_def,
        assumption+, rotate_tac -1)
 apply (drule_tac x = b in bspec, assumption,
        rotate_tac -1,
        frule_tac x = sa in bspec, assumption,
        drule_tac x = t in bspec, assumption)
  apply (simp, 
        thin_tac "acarrier R.
           xaddition_set f (aug_pm_set z i (s_set R s A)).
              yaddition_set f (aug_pm_set z i (s_set R s A)).
                 a s (x f+ y) = a s x f+ a s y")
  apply (simp add:sop_assoc_def)
done

lemma (in Ring) sop_assoc:"ipp_cond1 (s_set R s A) i; 
    ipp_cond2 z (s_set R s A) i f; ipp_cond3 z i; 
     inv_ipp z i f (s_set R s A); zeroA z i f (s_set R s A) z; 
      sop_distr2 R s f i (s_set R s A) z; 
       sop_assoc R s (aug_pm_set z i (s_set R s A)); 
        sop_inv R s i (s_set R s A); sop_z R s z; 
         sop_one R s (aug_pm_set z i (s_set R s A))  
   acarrier R. bcarrier R. 
      xaddition_set f (aug_pm_set z i (s_set R s A)).  
                           a s (b s x) = ( a r b) s x"
apply (rule ballI)+ apply (simp add:addition_set_def)
 apply (erule exE, erule conjE, erule exE, simp)
 apply (simp add:sop_assocTr)
done

lemma (in Ring) s_set_commute:"commute_bpp f (aug_pm_set z i (s_set R s A));
       x  addition_set f (aug_pm_set z i (s_set R s A)); 
        y  addition_set f (aug_pm_set z i (s_set R s A)) 
               x f+ y = y f+ x"
apply (simp add:commute_bpp_def)
done

lemma (in Ring) add_s_set_inc_add_set:"
      add_set f (aug_pm_set z i A) n  
             add_set f (aug_pm_set z i (s_set R s A)) n" 
apply (rule add_set_mono[of "aug_pm_set z i A" 
                            "aug_pm_set z i (s_set R s A)" f n])
apply (rule subsetI, simp add:aug_pm_set_def s_set_def)
apply (case_tac "x = z", simp)
 apply (case_tac "x  A", simp)
 
 apply simp
 apply (simp add:minus_set_def, erule bexE)
 apply blast
done

lemma (in Ring) sop_distr1Tr:"commute_bpp f (aug_pm_set z i (s_set R s A)); 
    assoc_bpp (aug_pm_set z i (s_set R s A)) f; ipp_cond1 (s_set R s A) i;
     ipp_cond2 z (s_set R s A) i f; ipp_cond3 z i; 
      inv_ipp z i f (s_set R s A); zeroA z i f (s_set R s A) z; 
       sop_distr1 R s f i (s_set R s A) z; 
        sop_distr2 R s f i (s_set R s A) z; 
         sop_assoc R s (aug_pm_set z i (s_set R s A)); 
          sop_inv R s i (s_set R s A); 
           sop_one R s (aug_pm_set z i (s_set R s A)); sop_z R s z   
 acarrier R. bcarrier R. x add_set f (aug_pm_set z i (s_set R s A)) n.
          (a ± b) s x = a s x f+ (b s x)" 
apply (induct_tac n)
 apply ((rule ballI)+, simp add:sop_distr1_def) 

apply (rule ballI)+ apply simp
 apply (erule bexE)+
 apply (cut_tac ring_is_ag,
        frule_tac x = a and y = b in aGroup.ag_pOp_closed [of "R"], 
        assumption+)
  apply (cut_tac n = n in addition_inc_add[of f
                     "aug_pm_set z i (s_set R s A)"],
        cut_tac addition_inc_add0[of "aug_pm_set z i (s_set R s A)" f])
 apply (frule_tac c = sa and A = "add_set f (aug_pm_set z i (s_set R s A)) n"
         in subsetD[of _ "addition_set f (aug_pm_set z i (s_set R s A))"],
         assumption+,
        frule_tac c = t in subsetD[of "aug_pm_set z i (s_set R s A)"
         "addition_set f (aug_pm_set z i (s_set R s A))"], assumption+,
        frule_tac x = a and y = b in  ring_tOp_closed, assumption+)
 apply simp
 apply (simp add:sop_distr2_def sop_distr1_def)
 apply (frule sop_closed[of s A i z f], assumption+,
        simp add:sop_distr2_def, simp add:sop_assoc_def, assumption+)
 apply (rotate_tac -1,
        frule_tac x = a in bspec, assumption,
        rotate_tac -1,
        frule_tac x = sa in bspec, assumption,
        drule_tac x = t in bspec, assumption)
  apply (frule_tac x = b in bspec, assumption,
        rotate_tac -1,
        frule_tac x = sa in bspec, assumption,
        drule_tac x = t in bspec, assumption, 
        thin_tac "rcarrier R.
           xaddition_set f (aug_pm_set z i (s_set R s A)).
              r s x  addition_set f (aug_pm_set z i (s_set R s A))")
apply (frule_tac x = "a s t" and y = "b s t" in 
       bpp_closed [of "(aug_pm_set z i (s_set R s A))" "f"], assumption+)
apply (subst addition_assoc, assumption+)
 apply (frule_tac ?x1 = "b s sa" and  ?y1 = "a s t" and ?z1 = "b s t" in
          addition_assoc[THEN sym, of "aug_pm_set z i (s_set R s A)" f],
          assumption+, simp,
        thin_tac "b s sa f+ (a s t f+ b s t) = b s sa f+ a s t f+ b s t")
 apply (frule_tac x = "b s sa" and y = "a s t" in 
           s_set_commute[of f z i s A], assumption+, simp)
 apply (frule_tac x = "a s t" and y = "b s sa" and z = "b s t" in
          addition_assoc[of "aug_pm_set z i (s_set R s A)" f],
          assumption+, simp,
        thin_tac "a s t f+ b s sa f+ b s t = a s t f+ (b s sa f+ b s t)",
        frule_tac x = "b s sa" and y = "b s t" in 
        bpp_closed [of "(aug_pm_set z i (s_set R s A))" "f"], assumption+,
        subst addition_assoc, assumption+, simp)
done

lemma (in Ring) sop_distr1:"commute_bpp f (aug_pm_set z i (s_set R s A)); 
      assoc_bpp (aug_pm_set z i (s_set R s A)) f; ipp_cond1 (s_set R s A) i; 
       ipp_cond2 z (s_set R s A) i f; ipp_cond3 z i; 
        inv_ipp z i f (s_set R s A); zeroA z i f (s_set R s A) z; 
         sop_distr1 R s f i (s_set R s A) z; 
          sop_distr2 R s f i (s_set R s A) z; 
           sop_assoc R s (aug_pm_set z i (s_set R s A)); 
            sop_inv R s i (s_set R s A); 
             sop_one R s (aug_pm_set z i (s_set R s A)); sop_z R s z   
      acarrier R. bcarrier R. 
         x addition_set f (aug_pm_set z i (s_set R s A)). 
                      (a ± b) s x = a s x f+ (b s x)" 
apply (rule ballI)+
 apply (simp add:addition_set_def) 
 apply (erule exE, erule conjE, erule exE, simp,
        thin_tac "xa = add_set f (aug_pm_set z i (s_set R s A)) n")
apply (simp add:sop_distr1Tr) 
done

definition
  fgmodule_condition ::"[('r, 'm) Ring_scheme, 'a  'a  'a, 'a  'a,
         'r  'a  'a, 'a set, 'a]  bool" where
  "fgmodule_condition R f i s A z 
    commute_bpp f (aug_pm_set z i (s_set R s A))  
      assoc_bpp (aug_pm_set z i (s_set R s A)) f  
       ipp_cond1 (s_set R s A) i  ipp_cond2 z (s_set R s A) i f  
        ipp_cond3 z i  inv_ipp z i f (s_set R s A) 
         zeroA z i f (s_set R s A) z  sop_distr1 R s f i (s_set R s A) z 
          sop_distr2 R s f i (s_set R s A) z  
           sop_assoc R s (aug_pm_set z i (s_set R s A))  
          sop_inv R s i (s_set R s A)  
         sop_one R s (aug_pm_set z i (s_set R s A))  sop_z R s z"

lemma (in Ring) sop_closed1:"fgmodule_condition R f i s A z; r  carrier R;
      x  addition_set f (aug_pm_set z i (s_set R s A)) 
          r s x  addition_set f (aug_pm_set z i (s_set R s A))"
apply(simp add:fgmodule_condition_def, (erule conjE)+)
 apply (simp add:sop_closed)
done

lemma (in Ring) fgmodule_is_module:"fgmodule_condition R f i s A z
                                  R module (fgmodule R A z i f s)"
apply (simp add:fgmodule_condition_def, (erule conjE)+)
apply (rule Module.intro)
 apply (frule fag_aGroup [of f z i "s_set R s A"], assumption+)
apply (simp add:fag_gen_by_def fgmodule_def, simp add:aGroup_def)
 apply (rule allI, rule impI)
 apply (simp add:zeroA_def)

apply (rule Module_axioms.intro)
 apply (rule Ring_axioms)

 apply (simp add:fgmodule_carr, subst fgmodule_def, simp,
        simp add:sop_closed)

  apply (simp add:fgmodule_carr, (subst fgmodule_def)+, simp,
         simp add:sop_closed,
         cut_tac ring_is_ag,
         frule_tac x = a and y = b in aGroup.ag_pOp_closed, assumption+, simp)
  apply (simp add:sop_distr1) 

  apply (simp add:fgmodule_carr, (subst fgmodule_def)+, simp,
         simp add:sop_closed bpp_closed)
  apply (simp add:sop_distr2_def)

  apply (frule_tac x = a and y = b in ring_tOp_closed, assumption+,
         simp add:fgmodule_carr, (subst fgmodule_def)+, simp,
         simp add:sop_closed bpp_closed)
  apply (simp add:sop_assoc)

 apply (cut_tac ring_one)
 apply (simp add:fgmodule_carr, (subst fgmodule_def)+, simp)
 apply (simp add:sop_one)  
done

lemma (in Ring) a_in_carr_fgmodule:"a  A
                                  a  carrier (fgmodule R A z i f s)"
apply (simp add:fgmodule_carr)
 apply (cut_tac addition_inc_add0[of "aug_pm_set z i (s_set R s A)"],
        rule subsetD[of "aug_pm_set z i (s_set R s A)"
           "addition_set f (aug_pm_set z i (s_set R s A))" a], assumption)
 apply (simp add:aug_pm_set_def s_set_def)
done

section "A fgmodule and a free module"

lemma (in Ring) fg_zeroTr:"fgmodule_condition R f i s A z; a  A  
                     𝟬  s a = z" 
apply (frule fgmodule_is_module [of f i s A z])
apply (frule a_in_carr_fgmodule[of a A z i f s])
apply (frule Module.sc_0_m [of "fgmodule R A z i f s" R "a"],
       simp add:a_in_carr_fgmodule)
apply (cut_tac ring_zero,
       simp add:fgmodule_def)
done

lemma (in Ring) fg_genTr0:"fgmodule_condition R f i s A z; 
      x  aug_pm_set z i (s_set R s A)  
        x  linear_span R (fgmodule R A z i f s) (carrier R) A"
 apply (simp add:aug_pm_set_def s_set_def)
 apply (case_tac "x = z")
 apply simp
 apply (simp add:linear_span_def) 
  apply (case_tac "A = {}", simp, simp add:fgmodule_def)
  apply simp
  apply (frule nonempty_ex[of "A"], thin_tac "A  {}")
  apply (erule exE)
  apply (subgoal_tac "(λj{j. j  (0::nat)}. xa)  {j. j  (0::nat)}  A") 
  apply (subgoal_tac "(λk{j. j  (0::nat)}. 𝟬)  {j. j  (0::nat)}  
                                        carrier R")
  apply (subgoal_tac "z = l_comb R (fgmodule R A z i f s) 0 
          (λk{j. j  (0::nat)}. 𝟬) (λj{j. j  (0::nat)}. xa)") apply blast
  apply (simp add:l_comb_def) 
  apply (frule_tac a1 = xa in fg_zeroTr [THEN sym, of f i s A z],
                      assumption+)
  apply (frule_tac a = xa in a_in_carr_fgmodule [of _ A z i f s])
  apply (frule fgmodule_is_module [of f i s A z])
  apply (cut_tac ring_zero, simp add:fgmodule_def)
  apply (simp add:ring_zero) 
  apply (simp)
 apply (case_tac "x  A",
        frule_tac x = x and A = A in nonempty,
        simp add:linear_span_def)
  apply (subgoal_tac "(λj{j. j  (0::nat)}. x)  {j. j  (0::nat)}  A")
  apply (subgoal_tac "(λk{j. j  (0::nat)}. 1r)  {j. j  (0::nat)}  
                        carrier R")
  apply (subgoal_tac "x = l_comb R (fgmodule R A z i f s) 0 
                 (λk{j. j  (0::nat)}. 1r) (λj{j. j  (0::nat)}. x)") 
  apply blast
  apply (simp add:l_comb_def)
  apply (frule fgmodule_is_module [of f i s A z])
  apply (frule_tac a = x in a_in_carr_fgmodule[of _ A z i f s])
  apply (simp add:fgmodule_def, fold fgmodule_def,
         simp add:ring_one, simp add:fgmodule_condition_def,
         (erule conjE)+, simp add:sop_one)

  apply (simp add:ring_one)
  apply (simp)

apply (case_tac "rcarrier R. aA. x = r s a")
  apply ((erule bexE)+, simp)
  apply (frule_tac x = a and A = A in nonempty, simp add:linear_span_def)
  apply (subgoal_tac "(λj{j. j  (0::nat)}. a)  {j. j  (0::nat)}  A")
  apply (subgoal_tac "(λk{j. j  (0::nat)}. r)  {j. j  (0::nat)}  
                        carrier R")
  apply (subgoal_tac "r s a = l_comb R (fgmodule R A z i f s) 0 
                 (λk{j. j  (0::nat)}. r) (λj{j. j  (0::nat)}. a)") 
  apply blast
  apply (frule_tac a = a in a_in_carr_fgmodule[of _ A z i f s]) 
  apply (simp add:l_comb_def fgmodule_def)
  apply (simp)
  apply (simp)

apply (simp add:minus_set_def,
      thin_tac "x  z", thin_tac "x  A", 
      thin_tac "rcarrier R. aA. x  r s a")
  apply (erule bexE)
  apply simp
  apply (erule disjE)
  apply ((erule bexE)+, simp)
apply (frule fgmodule_is_module [of f i s A z]) 
 apply (frule_tac a = r and m = a in 
         Module.sc_minus_am1[of "fgmodule R A z i f s" R], assumption+)
 apply (frule_tac a = a in a_in_carr_fgmodule[of _ A z i f s], assumption)
 apply (cut_tac ring_is_ag,
        frule_tac x = r in aGroup.ag_mOp_closed, assumption+)

 apply (frule_tac a = a in a_in_carr_fgmodule[of _ A z i f s])
 apply (simp add:fgmodule_def, fold fgmodule_def,
        simp add:sop_closed1)
 apply (frule_tac x = a and A = A in nonempty, simp add:linear_span_def)
  apply (subgoal_tac "(λj{j. j  (0::nat)}. a)  {j. j  (0::nat)}  A")
  apply (subgoal_tac "(λk{j. j  (0::nat)}. (-a r))  {j. j  (0::nat)}  
                        carrier R")
  apply (subgoal_tac "(-a r) s a = l_comb R (fgmodule R A z i f s) 0 
                 (λk{j. j  (0::nat)}. (-a r)) (λj{j. j  (0::nat)}. a)") 
  apply blast
  apply (simp add:l_comb_def fgmodule_def)
  apply (simp)
  apply (simp)

apply (frule_tac x = y and A = A in nonempty, simp add:linear_span_def)
  apply (subgoal_tac "(λj{j. j  (0::nat)}. y)  {j. j  (0::nat)}  A")
  apply (subgoal_tac "(λk{j. j  (0::nat)}. (-a 1r))  {j. j  (0::nat)}  
                        carrier R")
  apply (subgoal_tac "i- y = l_comb R (fgmodule R A z i f s) 0 
                 (λk{j. j  (0::nat)}. (-a 1r)) (λj{j. j  (0::nat)}. y)") 
  apply blast 
  apply (simp add:l_comb_def)
  apply (frule fgmodule_is_module [of "f" "i" "s" "A" "z"])
  apply (frule_tac a = y in a_in_carr_fgmodule [of  _ A z i f s])
  apply (cut_tac ring_one , cut_tac ring_is_ag,
         frule aGroup.ag_mOp_closed [of R "1r"], assumption)
  apply (simp add:Module.sc_minus_am1[THEN sym, of "fgmodule R A z i f s" R])
  apply (simp add:Module.sprod_one)
  apply (simp add:fgmodule_def)
  apply (rule Pi_I, simp,
         cut_tac ring_one, cut_tac ring_is_ag,
         simp add:aGroup.ag_mOp_closed[of R])
    apply (simp)
done

lemma (in Ring) fg_genTr:"fgmodule_condition R f i s A z 
      x. x  (add_set f (aug_pm_set z i (s_set R s A)) n)  
             x  linear_span R (fgmodule R A z i f s) (carrier R) A"
apply (induct_tac n)
apply (rule allI, rule impI, simp) 
apply (simp add:fg_genTr0)
apply (rule allI, rule impI, simp)
 apply (erule bexE)+
 apply (drule_tac x = sa in spec)
 apply simp
 apply (frule_tac x = t in fg_genTr0[of f i s A z], assumption+)
 apply (cut_tac whole_ideal)
 apply (frule fgmodule_is_module[of f i s A z])
 apply (frule_tac a = sa and b = t in Module.linear_span_pOp_closed[of
          "fgmodule R A z i f s" R "carrier R" A], assumption+,
        rule subsetI, simp add:a_in_carr_fgmodule, assumption+)
 apply (cut_tac n = n in addition_inc_add[of f "aug_pm_set z i (s_set R s A)"],
        cut_tac addition_inc_add0[of "aug_pm_set z i (s_set R s A)" f],
        frule_tac c = sa and A = "add_set f (aug_pm_set z i (s_set R s A)) n"
        in subsetD[of _ "addition_set f (aug_pm_set z i (s_set R s A))"],
        assumption+,
         frule_tac c = t and A = "aug_pm_set z i (s_set R s A)"
        in subsetD[of _ "addition_set f (aug_pm_set z i (s_set R s A))"],
        assumption+)
 apply (simp add:fgmodule_def)
done
  
lemma (in Ring) generator_of_fgm:"fgmodule_condition R f i s A z  
                 generator R (fgmodule R A z i f s) A"
apply (cut_tac whole_ideal) 
apply (simp add:generator_def)
apply (frule fgmodule_is_module [of f i s A z]) 
apply (rule conjI)
 apply (rule subsetI, simp add:a_in_carr_fgmodule)
 apply (frule Module.linear_span_sub[of "fgmodule R A z i f s" R 
                         "carrier R" A], assumption+)
 apply (rule subsetI, 
        simp add:a_in_carr_fgmodule)
 apply (rule equalityI, assumption,
        thin_tac "linear_span R (fgmodule R A z i f s) (carrier R) A
      carrier (fgmodule R A z i f s)")
 apply (rule subsetI)
 apply (simp add:fgmodule_carr, simp add:addition_set_def)
 apply (erule exE, erule conjE, erule exE, simp)
 apply (simp add: fg_genTr)      
done

lemma (in Ring) fg_freeTr1:"R module M; free_generator R M A;
  R module fgmodule R A z i f s; free_generator R (fgmodule R A z i f s) A;
  g  mHom R M (fgmodule R A z i f s); xA. g x = x  
  fa sa. fa  {j. j  (n::nat)}  A  sa  {j. j  n}  carrier R  
        l_comb R (fgmodule R A z i f s) n sa (cmp g fa) = 
                      l_comb R (fgmodule R A z i f s) n sa fa"
apply (induct_tac n, (rule allI)+, rule impI, erule conjE)
 apply (simp add:l_comb_def)
 apply (simp add:cmp_def)
apply ((rule allI)+, rule impI, erule conjE)
 apply (frule_tac f = fa and n = n and A = A in func_pre,
        frule_tac f = sa and n = n and A = "carrier R" in func_pre) 
 apply (drule_tac x = fa in spec,
        drule_tac x = sa in spec, simp)
      
 apply (frule_tac s = sa and f = fa and n = n in Module.l_comb_Suc[of 
         "fgmodule R A z i f s" R A "carrier R"], 
        rule subsetI,simp add:a_in_carr_fgmodule, 
           cut_tac whole_ideal, simp, assumption+, simp)
 apply (frule_tac s = sa and f = "cmp g fa" and n = n in Module.l_comb_Suc[of 
         "fgmodule R A z i f s" R A "carrier R"],
        rule subsetI,simp add:a_in_carr_fgmodule,
        cut_tac whole_ideal, simp, simp,
        rule Pi_I, simp add:cmp_def,
        frule_tac x = x and f = fa and A = "{j. j  Suc n}" and B = A
               in funcset_mem, simp+) 
 apply (frule_tac  x = "Suc n" and f = fa and A = "{j. j  Suc n}" and B = A
               in funcset_mem, simp+, simp add:cmp_def)
done

lemma (in Ring) fg_freeTr:"R module M; free_generator R M A;
      R module fgmodule R A z i f s; 
      free_generator R (fgmodule R A z i f s) A;
      g  mHom R M (fgmodule R A z i f s); xA. g x = x; 
      fa  {j. j  (n::nat)}  A; sa  {j. j  n}  carrier R  
      l_comb R (fgmodule R A z i f s) n sa (cmp g fa) =
                            l_comb R (fgmodule R A z i f s) n sa fa"
apply (simp add:fg_freeTr1)
done

lemma (in Ring) fg_free1:" A  {}; fgmodule_condition R f i s A z; 
      free_generator R (fgmodule R A z i f s) A; R module M; 
      free_generator R M A  M ≅⇘R(fgmodule R A z i f s)" 
apply (subgoal_tac "(λxA. x)  A  carrier (fgmodule R A z i f s)") 
 prefer 2 
   apply (rule Pi_I, simp, simp add:a_in_carr_fgmodule)
 apply (frule fgmodule_is_module [of f i s A z])
 apply (frule Module.exist_extension_mhom[of M R "fgmodule R A z i f s" A 
         "λxA. x"], assumption+)
 apply (erule bexE)
 apply (thin_tac "(λxA. x)  A  carrier (fgmodule R A z i f s)")
 apply (simp add:misomorphic_def)
 apply (subgoal_tac "bijec⇘M,(fgmodule R A z i f s)g", blast)
 apply (simp add:bijec_def)
apply (rule conjI)
 apply (simp add:injec_def)
 apply (rule conjI, simp add:mHom_def)
 apply (rule equalityI)
 apply (rule subsetI)
 apply (simp add:ker_def, erule conjE) 
 apply (frule Module.free_generator_generator[of M R A], assumption)
  apply (simp add:generator_def, erule conjE) 
  apply (rotate_tac -1, frule sym, 
         thin_tac "linear_span R M (carrier R) A = carrier M",
         frule_tac a = x and A = "carrier M" and 
                B = "linear_span R M (carrier R) A" in eq_set_inc, assumption+,
         thin_tac "carrier M = linear_span R M (carrier R) A",
         simp add:linear_span_def)
  apply (erule exE, (erule bexE)+)
  apply (cut_tac whole_ideal)
 apply (frule_tac s = sa and n = n and f = fa and H = A in 
        Module.same_together[of M R "carrier R"], assumption+) 
 apply ((erule bexE)+, erule conjE)
 apply (fold l_comb_def[of R M], simp)
 apply (frule_tac f = g and s = t and n = "card (fa ` {j. j  n}) - Suc 0" 
        and g = ga in Module.linmap_im_lincomb[of M R "carrier R" 
        "fgmodule R A z i f s" _  A], assumption+)
 apply (frule_tac f = fa and A = "{j. j  n}" in img_subset[of _ _ A],
        frule_tac f = ga and A = "{j. j  card (fa ` {j. j  n}) - Suc 0}"
        and B = "fa ` {j. j  n}" and ?B1.0 = A in extend_fun, assumption+)
 apply (rotate_tac -1, frule sym,
        thin_tac "g (l_comb R M (card (fa ` {j. j  n}) - Suc 0) t ga) =
        l_comb R (fgmodule R A z i f s) (card (fa ` {j. j  n}) - Suc 0) t
         (cmp g ga)", simp) 

 apply (frule_tac g = g and fa = ga and sa = t and 
        n = "card (fa ` {j. j  n}) - Suc 0" in fg_freeTr[of M A z i f s],
        assumption+)
  apply (frule_tac f = fa and A = "{j. j  n}" in img_subset[of _ _ A],
        frule_tac f = ga and A = "{j. j  card (fa ` {j. j  n}) - Suc 0}"
        and B = "fa ` {j. j  n}" and ?B1.0 = A in extend_fun, assumption+)
  apply simp
  apply (cut_tac k = n in finite_Collect_le_nat,
         cut_tac k = "card (fa ` {j. j  n}) - Suc 0" in finite_Collect_le_nat,
         cut_tac F = "{j. j  n}" and h = fa in finite_imageI,
         assumption)
  apply (frule_tac f = fa and A = "{j. j  n}" and B = A and a = 0 in
         mem_in_image, simp,
         frule_tac x = "fa 0" and A = "fa ` {j. j  n}" in nonempty,
         frule_tac A = "fa ` {j. j  n}" in nonempty_card_pos, assumption)
  apply (frule_tac A = "fa ` {j. j  n}" and 
         n = "card (fa ` {j. j  n}) - Suc 0" and f = ga in Nset2finite_inj,
         simp, assumption)
  apply (frule Module.free_generator_sub[of "fgmodule R A z i f s" R A],
          assumption)
  apply (frule_tac s = t and m = ga and n = "card (fa ` {j. j  n}) - Suc 0"
         in Module.unique_expression1[of "fgmodule R A z i f s" R A], 
         assumption+)
   apply (frule_tac f = fa and A = "{j. j  n}" in img_subset[of _ _ A],
        rule_tac f = ga and A = "{j. j  card (fa ` {j. j  n}) - Suc 0}"
        and B = "fa ` {j. j  n}" and ?B1.0 = A in extend_fun, assumption+)
 apply (rule_tac s = t and n = "card (fa ` {j. j  n}) - Suc 0" and
        m = ga in Module.linear_comb0_1[of M R A], assumption+,
        simp)
    apply (frule_tac f = fa and A = "{j. j  n}" in img_subset[of _ _ A],
        rule_tac f = ga and A = "{j. j  card (fa ` {j. j  n}) - Suc 0}"
        and B = "fa ` {j. j  n}" and ?B1.0 = A in extend_fun, assumption+)
  
apply (simp add:ker_def,
      frule Module.module_is_ag[of M],
      simp add: aGroup.ag_inc_zero[of M],
      simp add:Module.mHom_0)

apply (simp add:surjec_def,
       rule conjI, simp add:mHom_def)
 apply (rule surj_to_test, simp add:mHom_def aHom_def)
 apply (rule ballI)
 apply (frule Module.free_generator_generator[of "fgmodule R A z i f s" R A],
        assumption+)
 apply (cut_tac generator_def[of R "fgmodule R A z i f s" A], simp,
        erule conjE, rotate_tac -1, frule sym,
        thin_tac "linear_span R (fgmodule R A z i f s) (carrier R) A =
           carrier (fgmodule R A z i f s)", simp,
        thin_tac "carrier (fgmodule R A z i f s) =
           linear_span R (fgmodule R A z i f s) (carrier R) A",
        thin_tac "A  linear_span R (fgmodule R A z i f s) (carrier R) A",
        thin_tac "generator R (fgmodule R A z i f s) A  True")
 apply (simp add:linear_span_def, erule exE, (erule bexE)+)
 apply (frule_tac g1 = g and fa1 = fa and n1 = n and sa1 = sa in 
        fg_freeTr[THEN sym, of M A z i f s], assumption+, simp,
        thin_tac "l_comb R (fgmodule R A z i f s) n sa fa =
        l_comb R (fgmodule R A z i f s) n sa (cmp g fa)")
 apply (frule_tac f1 = g and s1 = sa and g1 = fa and n1 = n in 
        Module.linmap_im_lincomb[THEN sym, of M R "carrier R" 
              "fgmodule R A z i f s" _  A],
        simp add:whole_ideal, assumption+,
        simp add:Module.free_generator_sub, assumption+, simp)
 apply (cut_tac whole_ideal,
        frule_tac s = sa and n = n and m = fa in 
        Module.l_comb_mem[of M R "carrier R" A], assumption+,
        simp add:Module.free_generator_sub, assumption+)
 apply blast
done
        
lemma (in Ring) fg_free:"fgmodule_condition R f i s A z; 
       free_generator R (fgmodule R A z i f s) A; R module M; 
       free_generator R M A  M ≅⇘R(fgmodule R A z i f s)" 
apply (case_tac "A = {}")
apply (simp add:free_generator_def generator_def linear_span_def)
 apply (erule conjE)+
apply (cut_tac fgmodule_is_module[of f i s A z])
apply (rule Module.Modules_single_carrier_isom[of M R "fgmodule R {} z i f s"],
       assumption+, simp, rule sym, assumption, rule sym, assumption, simp)
apply (simp add: fg_free1)
done

section "Direct sum, again"

definition
  miota :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
    ('a, 'r, 'm1) Module_scheme]  'a  'a" where
  "miota R M1 M = (λxcarrier M1. x)"

definition
 msubmodule ::"[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
 ('a, 'r, 'm1) Module_scheme]  bool" where
 "msubmodule R M M1  miota R M1 M  mHom R M1 M  
                         (carrier M1)  (carrier M)"  
     (** M and M1 are R modules. **) 

definition
  ds2 :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
          ('a, 'r, 'm1) Module_scheme, ('a, 'r, 'm1) Module_scheme]  bool" where

  "ds2 R M M1 M2  R module M  msubmodule R M M1  msubmodule R M M2  
          (xcarrier M. m1carrier M1. m2carrier M2. x = m1 ±Mm2)   
           (carrier M1)  (carrier M2) = {𝟬M}"

abbreviation
  DS2  ((4_/ ⨁⇘_,_ _) [92,93,92,92]92) where
  "M1 ⨁⇘R,MM2 == ds2 R M M1 M2"

lemma (in Ring) ds2_commute:"R module M1; R module M2; R module M; 
               M1 ⨁⇘R,MM2  M2 ⨁⇘R,MM1"
apply (simp add:ds2_def)
 apply (erule conjE)+
 apply (subst Int_commute, simp) 
 apply (rule ballI, 
       drule_tac x = x in bspec, assumption,
       (erule bexE)+)
 apply (simp add:msubmodule_def, (erule conjE)+,
        frule_tac c = m1 in subsetD[of "carrier M1" "carrier M"], assumption+,
        frule_tac c = m2 in subsetD[of "carrier M2" "carrier M"], assumption+)
 apply (frule Module.module_is_ag[of M R],
        frule_tac x = m1 and y = m2 in aGroup.ag_pOp_commute, assumption+,
        simp)
 apply blast
done

lemma (in Ring) msub_addition:"R module M; R module M1; msubmodule R M M1;
       x  carrier M1; y  carrier M1  x ±M1y = x ±My"
apply (simp add:msubmodule_def, (erule conjE)+)
apply (frule Module.mHom_add[of M1 R M "miota R M1 M" x y], assumption+) 
apply (frule Module.module_is_ag[of M1],
       frule aGroup.ag_pOp_closed[of M1 x y], assumption+,
       simp add:miota_def)
done 

lemma (in Ring) msub_mOp:"R module M; R module M1; msubmodule R M M1;
       x  carrier M1  -aM1x  = -aMx"
apply (simp add:msubmodule_def, (erule conjE)+)
apply (frule Module.module_is_ag[of M1],
       frule_tac x = x in aGroup.ag_mOp_closed[of M1], assumption+)
apply (frule Module.mHom_inv[of M1 R M x "miota R M1 M"], assumption+,
       simp add:miota_def)
done

lemma (in Ring) msub_sprod:"R module M; R module M1; msubmodule R M M1;
       a  carrier R; x  carrier M1  a sM1x = a sMx" 
apply (simp add:msubmodule_def, (erule conjE)+)
apply (frule Module.mHom_lin[of M1 R M x "miota R M1 M" a], assumption+)
apply (frule Module.sc_mem[of M1 R a x], assumption+)
apply (simp add:miota_def)
done

lemma (in Ring) msub_submodule:"R module M; R module M1; msubmodule R M M1
          submodule R M (carrier M1)"
apply (simp add:submodule_def msubmodule_def, erule conjE)
apply (rule conjI)
 apply (frule Module.module_is_ag[of M R],
        frule Module.module_is_ag[of M1 R])
 apply (rule aGroup.asubg_test, assumption+,
        frule Module.module_inc_zero[of M1 R], blast)
 apply ((rule ballI)+,
       frule_tac x = b in aGroup.ag_mOp_closed[of M1], assumption+) 
 apply (frule_tac m = a and n = "-aM1b" in Module.mHom_add[of M1 R M 
        "miota R M1 M"], assumption+,
        frule_tac x = a and y = "-aM1b" in aGroup.ag_pOp_closed,
        assumption+, simp add:miota_def)
 apply (frule_tac m = b in Module.mHom_inv[of M1 R M _ "miota R M1 M"], 
          assumption+, simp add:miota_def, simp add:miota_def)
apply ((rule allI)+, rule impI, erule conjE)
 apply (frule_tac m = m and a = a in Module.mHom_lin [of M1 R M _ 
       "miota R M1 M" _], assumption+)
 apply (frule_tac a = a and m = m in Module.sc_mem[of M1 R], assumption+)
 apply (simp add:miota_def)
done

lemma (in Ring) ds2_unique:"R module M; R module M1; R module M2; 
       ds2 R M M1 M2;  m1  carrier M1; m1'  carrier M1; 
                       m2  carrier M2; m2'  carrier M2; 
       m1 ±Mm2 = m1' ±Mm2'  m1 = m1'  m2 = m2'"
apply (frule msub_submodule [of M M1], assumption+,
       simp add:ds2_def)
apply (frule msub_submodule [of M M2], assumption+,
       simp add:ds2_def)
 apply (frule Module.submodule_subset[of M R "carrier M1"], assumption,
        frule Module.submodule_subset[of M R "carrier M2"], assumption)
 apply (frule subsetD [of "carrier M1" "carrier M" m1], assumption+,
        frule subsetD [of "carrier M1" "carrier M" m1'], assumption+,
        frule subsetD [of "carrier M2" "carrier M" "m2"], assumption+,
        frule subsetD [of "carrier M2" "carrier M" "m2'"], assumption+)
 apply (frule_tac Module.module_is_ag[of M R],
        frule_tac x = m1 and y = m2 in aGroup.ag_pOp_closed, assumption+,
        frule_tac x = m1' and y = m2' in aGroup.ag_pOp_closed, assumption+,
        frule_tac x = m2' in aGroup.ag_mOp_closed, assumption+,
        frule_tac x = m2 in aGroup.ag_mOp_closed, assumption+)
 apply (frule_tac a = "m1 ±Mm2" and b = "m1' ±Mm2'" and c = "-aMm2" in 
        aGroup.ag_pOp_add_r, assumption+,
        thin_tac "m1 ±Mm2 = m1' ±Mm2'",
        simp add:aGroup.ag_pOp_assoc[of M m1 m2 "-aMm2"]
                                     aGroup.ag_r_inv1 aGroup.ag_r_zero,
        simp add:aGroup.ag_pOp_assoc[of M m1' m2' "-aMm2"],
        frule_tac x = m2' and y = "-aMm2" in aGroup.ag_pOp_closed,
               assumption+,
        frule_tac x = m1' in aGroup.ag_mOp_closed, assumption+,
        frule_tac x = m1' and y = "m2' ±M-aMm2" in 
                           aGroup.ag_pOp_closed, assumption+,
        frule_tac a = m1 and b = "m1' ±M(m2' ±M-aMm2)" and 
        c = "-aMm1'" in aGroup.ag_pOp_add_l[of M], simp, assumption+)
 apply (frule aGroup.ag_pOp_assoc[THEN sym, of M "-aMm1'" "m1'"
                                                      "(m2' ±M-aMm2)"],
        assumption+) 
apply (rotate_tac -6, frule sym, thin_tac "m1 = m1' ±M(m2' ±M-aMm2)",
       thin_tac "-aMm1' ±Mm1 = -aMm1' ±M(m1' ±M(m2' ±M-aMm2))",
       simp, simp add:aGroup.ag_l_inv1 aGroup.ag_l_zero)
 apply (frule Module.submodule_mOp_closed [of M R "carrier M1" m1'], 
                                                     assumption+,
        frule Module.submodule_pOp_closed [of M R "carrier M1" "-aMm1'" 
                                                m1], assumption+,
        frule Module.submodule_pOp_closed [of M R "carrier M2" m2' "-aMm2"],
                                                     assumption+,
     rule Module.submodule_mOp_closed [of M R "carrier M2" m2], assumption+) 
 apply (subgoal_tac "(-aMm1' ±Mm1)  carrier M1  carrier M2")
 prefer 2 apply simp 
 apply (subgoal_tac "carrier M1  carrier M2 = {𝟬M}")
 apply (frule sym, thin_tac "-aMm1' ±Mm1 = m2' ±M-aMm2")
 apply simp 
 apply (simp add:aGroup.ag_eq_diffzero[THEN sym, of M m2' m2] aGroup.ag_r_zero)

 apply (simp add:ds2_def)
done

lemma (in Ring) miota_injec:"R module M; R module M1; R module M2; 
       ds2 R M M1 M2; msubmodule R M M1  
       miota R M1 M  mHom R M1 M  injec⇘M1,M(miota R M1 M)"
apply (rule conjI)
 apply (simp add:msubmodule_def)
apply (simp add:injec_def)
 apply (rule conjI)
 apply (simp add:msubmodule_def mHom_def)
apply (rule equalityI)
 prefer 2
 apply (rule subsetI, simp, simp add:ker_def)
 apply (simp add:Module.module_inc_zero, simp add:msubmodule_def)
 apply (erule conjE)
 apply (simp add:Module.mHom_0)
apply (rule subsetI)
 apply (simp add:ker_def, erule conjE)
 apply (simp add:miota_def, simp add:msubmodule_def)
 apply (erule conjE)
 apply (frule Module.mHom_0 [of M1 R M "miota R M1 M"], assumption+)
 apply (frule Module.module_inc_zero [of M1 R])
 apply (simp add:miota_def)
done

definition
  mproj1 :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme,
    ('a, 'r, 'm1) Module_scheme, ('a, 'r, 'm1) Module_scheme]  'a  'a" where
  "mproj1 R M1 M2 M = (λxcarrier M. THE x1. x1  carrier M1 
                                          (x ±M(-aMx1))  carrier M2)"
  
definition
  mproj2 :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme,
    ('a, 'r, 'm1) Module_scheme, ('a, 'r, 'm1) Module_scheme]  'a  'a" where
  "mproj2 R M1 M2 M = mproj1 R M2 M1 M"

(** mproj is used under the condition ds2 R M M1 M2 **)

lemma (in Ring) ds2_components:"R module M1; R module M2; R module M;
       M1 ⨁⇘R,MM2; a  carrier M  
         a1carrier M1. a2carrier M2. a = a1 ±Ma2"
by (simp add:ds2_def)

lemma (in Ring) ds2_components1:"R module M1; R module M2; R module M;
       M1 ⨁⇘R,MM2; a  carrier M  
         a1carrier M1. a ±M-aMa1  carrier M2"
apply (frule ds2_components[of M1 M2 M a], assumption+, (erule bexE)+,
       frule Module.module_is_ag[of M R],
       unfold ds2_def, frule conjunct2[THEN conjunct1],
       frule conjunct2[THEN conjunct2[THEN conjunct1]], fold ds2_def)
 apply (simp add:msubmodule_def, (erule conjE)+)
 apply (frule_tac c = a1 in subsetD[of "carrier M1" "carrier M"], assumption+,
        frule_tac c = a2 in subsetD[of "carrier M2" "carrier M"], assumption+)
 
 apply (frule Module.module_is_ag[of M],
        frule_tac x = a1 and y = a2 in aGroup.ag_pOp_commute, assumption+,
        simp)
 apply (frule_tac x = a1 and y = a2 in aGroup.ag_pOp_closed[of M], assumption+,
        frule_tac x = a1 in aGroup.ag_mOp_closed[of M], assumption+,
        frule_tac a = "a1 ±Ma2" and b = "a2 ±Ma1" and c = "-aMa1" in 
        aGroup.ag_pOp_add_r[of M], assumption+,
        frule_tac x = a2 and y = a1 and z = "-aMa1" in 
                                   aGroup.ag_pOp_assoc[of M], assumption+,
        simp add:aGroup.ag_r_inv1 aGroup.ag_r_zero)
 apply (thin_tac "a2 ±Ma1  carrier M",
        thin_tac "a = a2 ±Ma1",
        thin_tac "a2  carrier M",
        thin_tac "a1 ±Ma2 = a2 ±Ma1",
        frule sym, thin_tac "a2 ±Ma1 ±M-aMa1 = a2")
 apply (frule_tac a = a2 and b = "a2 ±Ma1 ±M-aMa1" in 
                      eq_elem_in[of _ "carrier M2"], assumption, blast)
done

lemma (in Ring) mprojTr1:"R module M1; R module M2; R module M; ds2 R M M1 M2;
  x  carrier M   ∃!x1. x1  carrier M1  (x ±M-aMx1)  carrier M2"
apply (frule Module.module_is_ag[of M R])
apply (rule ex_ex1I) 
 apply (frule ds2_components1 [of M1 M2 M x], assumption+, erule bexE, blast)

 apply (simp add:ds2_def, (erule conjE)+, simp add:msubmodule_def,
        (erule conjE)+,
        frule_tac c = x1 in subsetD[of "carrier M1" "carrier M"], assumption+,
        frule_tac x = x1 in aGroup.ag_mOp_closed, assumption+,
        frule_tac c = y in subsetD[of "carrier M1" "carrier M"], assumption+,
        frule_tac x = y in aGroup.ag_mOp_closed, assumption+,
        frule_tac ?m1.0 = x1 and ?m1' = y and ?m2.0 = "x ±M-aMx1" and 
         ?m2' = "x ±M-aMy" in ds2_unique[of M M1 M2], assumption+)
 apply (simp add:ds2_def msubmodule_def, assumption+)
 apply (simp add:aGroup.ag_pOp_commute[of M x])
 apply (simp add:aGroup.ag_pOp_assoc[THEN sym, of M _ _ x]) 
 apply ((subst aGroup.ag_r_inv1, assumption+)+, simp, simp)
done

lemma (in Ring) mprojTr2:"R module M1; R module M2; R module M; ds2 R M M1 M2;
      x  carrier M; x1  carrier M1; (x ±M(-aMx1))  carrier M2; 
      y1  carrier M1;(x ±M(-aMy1))  carrier M2    x1 = y1"
apply (frule_tac x = x in mprojTr1[of M1 M2 M], assumption+)
apply blast
done

lemma (in Ring) mprojTr3:"R module M1; R module M2; R module M; ds2 R M M1 M2;
      a  carrier M; a1  carrier M1; (a ±M(-aMa1))  carrier M2 
      (THE x1. x1  carrier M1  a ±M-aMx1  carrier M2) = a1"
apply (subgoal_tac "(THE x1. x1  carrier M1  a ±M-aMx1  carrier M2)  
         carrier M1   a ±M-aM(THE x1. x1  carrier M1   
         a ±M-aMx1  carrier M2)  carrier M2", 
       rule mprojTr2[of M1 M2 M a 
       "THE x1. x1  carrier M1  a ±M-aMx1  carrier M2" a1], assumption+,
       simp, simp, assumption+)
apply (rule theI')
apply (simp add:mprojTr1)
done

lemma (in Ring) mproj:"R module M1; R module M2; R module M; ds2 R M M1 M2
       mproj1 R M1 M2 M  mHom R M M1"
apply (simp add:mHom_def)
apply (rule conjI)
 apply (simp add:aHom_def)
 apply (rule conjI)
 apply (rule Pi_I)
 apply (simp add:mproj1_def)
 apply (frule_tac a = x in ds2_components1[of M1 M2 M], assumption+,
        erule bexE,
        frule_tac a = x and ?a1.0 = a1 in mprojTr3[of M1 M2 M], assumption+,
        simp)

apply (simp add:restrict_def mproj1_def extensional_def)

 apply ((rule ballI)+,
        frule Module.module_is_ag[of M R], simp add:aGroup.ag_pOp_closed,
        frule_tac x = a and y = b in aGroup.ag_pOp_closed, assumption+,
        frule_tac a = a in ds2_components1[of M1 M2 M], assumption+,
        frule_tac a = b in ds2_components1[of M1 M2 M], assumption+,
        frule_tac a = "a ±Mb" in ds2_components1[of M1 M2 M], assumption+,
        (erule bexE)+, rename_tac b1 ab,
        frule_tac a = "a ±Mb" and ?a1.0 = ab in mprojTr3[of M1 M2 M],
        assumption+,
        frule_tac a = a and ?a1.0 = a1 in mprojTr3[of M1 M2 M], assumption+,
        frule_tac a = b and ?a1.0 = b1 in mprojTr3[of M1 M2 M], assumption+,
        simp,
        thin_tac "(THE x1. x1  carrier M1 
                                  a ±Mb ±M-aMx1  carrier M2) = ab",
        thin_tac "(THE x1. x1  carrier M1  a ±M-aMx1  carrier M2) = a1",
        thin_tac "(THE x1. x1  carrier M1  b ±M-aMx1  carrier M2) = b1")
 apply (frule Module.module_is_ag[of M2],
        frule_tac x = "a ±M-aMa1" and y = "b ±M-aMb1" in 
                       aGroup.ag_pOp_closed[of M2], assumption+,
        frule_tac x = "a ±M-aMa1" and y = "b ±M-aMb1" in 
                     msub_addition[of M M2], assumption+,
        simp add:ds2_def, assumption+, simp,
          thin_tac "a ±M-aMa1 ±M2(b ±M-aMb1) = 
                                 a ±M-aMa1 ±M(b ±M-aMb1)",
        unfold ds2_def, frule conjunct2[THEN conjunct1], fold ds2_def,
          simp add:msubmodule_def, (erule conjE)+,
         frule_tac c = a1 in subsetD[of "carrier M1" "carrier M"], assumption+,
         frule_tac c = b1 in subsetD[of "carrier M1" "carrier M"], assumption+,
         frule_tac x = a1 in aGroup.ag_mOp_closed, assumption+,
         frule_tac x = b1 in aGroup.ag_mOp_closed, assumption+,
         frule_tac a = a and b = "-aMa1" and c = b and d = "-aMb1" in 
                   aGroup.pOp_assocTr43[of M], assumption+, simp,
         thin_tac "a ±M-aMa1 ±M(b ±M-aMb1) =
                                      a ±M(-aMa1 ±Mb) ±M-aMb1",
         frule_tac x = "-aMa1" and y = b in aGroup.ag_pOp_commute, 
         assumption+, simp,
         frule_tac a1 = a and b1 = b and c1 = "-aMa1" and d1 = "-aMb1" in 
         aGroup.pOp_assocTr43[THEN sym], assumption+, simp,
         thin_tac "a ±M(b ±M-aMa1) ±M-aMb1 = 
                                        a ±Mb ±M(-aMa1 ±M-aMb1)",
         frule_tac x1 = a1 and y1 = b1 in aGroup.ag_p_inv[THEN sym, of M],
          assumption+, simp,
         thin_tac "-aMa1 ±M-aMb1 = -aM(a1 ±Mb1)")
  apply (rule_tac x = "a ±Mb" and ?x1.0 = ab and ?y1.0 = "a1 ±M1b1" in 
          mprojTr2[of M1 M2 M], assumption+,
         frule Module.module_is_ag[of M1], 
         simp add:aGroup.ag_pOp_closed[of M1], unfold ds2_def, 
         frule conjunct2[THEN conjunct1], fold ds2_def,
         subst msub_addition[of M M1], assumption+)

apply (rule ballI)+
 apply (simp add:mproj1_def, simp add:Module.sc_mem)
 apply (frule_tac a = m in ds2_components1[of M1 M2 M], assumption+,
        erule bexE,
        unfold ds2_def, frule conjunct2[THEN conjunct1], fold ds2_def,
          simp add:msubmodule_def, (erule conjE)+,
        frule_tac c = a1 in subsetD[of "carrier M1" "carrier M"], assumption+,
        frule_tac a = a and m = "m ±M-aMa1" in Module.sc_mem[of M2 R],
        assumption+,
        unfold ds2_def, frule conjunct2[THEN conjunct1], 
        frule conjunct2[THEN conjunct2[THEN conjunct1]], fold ds2_def,
        simp add:msub_sprod[of M M2],
        frule Module.module_is_ag[of M],
        frule_tac x = a1 in aGroup.ag_mOp_closed[of M], assumption+,
        simp add:Module.sc_r_distr,
        frule Module.module_is_ag[of M1],
        frule_tac x = a1 in aGroup.ag_mOp_closed[of M1], assumption+,
        frule_tac a1 = a and x1 = "-aMa1" in msub_sprod[THEN sym, of M M1],
        assumption+)
  apply (simp add:msub_mOp,
        frule_tac x1 = a1 in msub_mOp[THEN sym, of M M1], assumption+, simp)
  apply (
        thin_tac "a sM(-aM1a1) = a sM1(-aM1a1)",
        thin_tac "-aMa1 = -aM1a1",
        thin_tac "-aM1a1  carrier M",
        frule_tac a = a and m = a1 in Module.sc_mem[of M1 R], 
        assumption+,
        frule_tac a = m and ?a1.0 = a1 in mprojTr3[of M1 M2 M],
           assumption+, simp add:msub_mOp, simp,
        thin_tac "(THE x1. x1  carrier M1  m ±M-aMx1  carrier M2) = a1",
        frule_tac a = "a sMm" and ?a1.0 = "a sM1a1" in 
         mprojTr3[of M1 M2 M], assumption+,
        simp add:Module.sc_mem, assumption)
  apply (simp add:msub_mOp msub_sprod,
         simp add:Module.sc_minus_am[of M R])
  apply simp
done

lemma (in Ring) mproj2:"R module M1; R module M2; R module M; M1 ⨁⇘R,MM2
     mproj2 R M1 M2 M  mHom R M M2"
 apply (frule ds2_commute[of M1 M2 M], assumption+)
 apply (simp add:mproj2_def)
 apply (simp add:mproj) 
done

subsection "Existence of the tensor product"

definition
  fm_gen_by_prod :: "[('r, 'm) Ring_scheme, (('a * 'b), 'r, 'm1) Module_scheme,
      ('a, 'r, 'm1) Module_scheme, ('b, 'r, 'm1) Module_scheme]  bool"
    ((4FM⇘_/ _ _ _) [100,100,101]100) where
  "FM⇘RP M N  R module P  free_generator R P (M ×c N)"

lemma (in Ring) free_gen_gen:"FM⇘RP M N  generator R P (M ×c N)"  
apply (simp add:fm_gen_by_prod_def)
apply (erule conjE)
apply (simp add:free_generator_def)
done

lemma (in Ring) free_gen_mem:"FM⇘RP M N; a  (M ×c N)   a  carrier P"
apply (simp add:fm_gen_by_prod_def)
 apply (erule conjE)
 apply (simp add:free_generator_def, (erule conjE)+)
 apply (simp add:generator_def)
 apply (erule conjE)+
 apply (simp add:subsetD)
done 

lemma (in Ring) mHom_lin_nsumTr:"R module M; R module N; t  mHom R M N 
 f  {j. j  (n::nat)}  carrier M   t (nsum M f n) = nsum N (cmp t f) n"
apply (induct_tac n)
 apply (rule impI, simp add:cmp_def)
apply (rule impI)
 apply simp
 apply (frule_tac func_pre, simp)
 apply (frule Module.module_is_ag [of M R])
 apply (frule_tac n = n in aGroup.nsum_mem [of M _ f],
        rule allI, simp add:Pi_def,
        frule_tac x = "Suc n" and A = "{j. j  Suc n}" and f = f and
        B = "carrier M" in funcset_mem, simp)
 apply (simp add:Module.mHom_add cmp_def)
done

lemma (in Ring) mHom_lin_nsum:"R module M; R module N; t  mHom R M N;
       f  {j. j  (n::nat)}  carrier M 
                           t (nsum M f n) = nsum N (cmp t f) n"
apply (simp add:mHom_lin_nsumTr)
done

lemma (in Ring) module_over_zeroring:"zeroring R; R module M   
                    carrier M = {𝟬M}"
apply (simp add:zeroring_def, erule conjE) 
apply (rule equalityI)
 apply (rule subsetI)
 apply (frule_tac m = x in Module.sprod_one [of M R], assumption)
 apply (cut_tac ring_one, simp)
 apply (simp add:Module.sc_0_m[of M R]) 
apply (frule Module.module_is_ag [of M R])
 apply (simp add:aGroup.ag_inc_zero)
done

lemma (in Ring) submodule_over_zeroring:"zeroring R; R module M; 
                 submodule R M N   N =  {𝟬M}"
apply (rule equalityI)
 apply (rule subsetI)
 apply (simp add:submodule_def, (erule conjE)+)
 apply (thin_tac "a m. a  carrier R  m  N  a sMm  N")
 apply (cut_tac module_over_zeroring [of M])
 apply simp
 apply (frule_tac A = N and B = "{𝟬M}" and c = x in subsetD, assumption+)
 apply (simp, assumption+)
apply (frule Module.submodule_inc_0 [of M R N], assumption+) 
 apply simp
done

definition
  Least_submodule :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme,
                       'a set]  'a set"
         ((3LSM⇘_/ _/ _) [100,100,101]100) where
  "LSM⇘RM T = {N. submodule R M N  T  N}" 

lemma (in Ring) LSM_mem:"R module M; T  carrier M; t  T  
                                                     t  (LSM⇘RM T)"
apply (simp add:Least_submodule_def, rule allI, rule impI)
apply (erule conjE)
apply (simp add:subsetD)
done


lemma (in Ring) LSM_sub_M:"R module M; T  carrier M 
                              (LSM⇘RM T)  carrier M"
apply (rule subsetI, simp add:Least_submodule_def)
apply (frule Module.submodule_whole[of M R])
apply (drule_tac x = "carrier M" in spec,
       simp)
done

lemma (in Ring) LSM_sub_submodule:"R module M; T  carrier M; 
      submodule R M N; T  N   (LSM⇘RM T)  N"
by (rule subsetI, simp add:Least_submodule_def)

lemma (in Ring) LSM_inc_T:"R module M; T  carrier M  T  (LSM⇘RM T)"
apply (rule subsetI)
apply (simp add:LSM_mem)
done 

lemma (in Ring) LSM_submodule:"R module M; T  carrier M 
                submodule R M (LSM⇘RM T)" 
apply (frule LSM_sub_M[of M T], assumption+)
 apply (subst Least_submodule_def)
 apply (simp add:submodule_def)
apply (rule conjI)
 apply (rule subsetI, simp,
        drule_tac a = "carrier M" in forall_spec)
  apply simp
  apply (frule Module.submodule_whole[of M R])
  apply (simp add:submodule_def[of R M "carrier M"], assumption)

apply (frule Module.module_is_ag [of M R])
 apply (rule aGroup.asubg_test, assumption+)
 apply (rule subsetI, simp,
        drule_tac a = "carrier M" in forall_spec, simp,
        frule Module.submodule_whole[of M R],
        simp add:submodule_def[of R M "carrier M"], assumption) 
 apply (cut_tac x = "𝟬M⇙" and A = "{N. N  carrier M 
          M +> N  (a m. a  carrier R  m  N  a sMm  N)  T  N}"
        in  nonempty)
 apply simp
 apply (rule allI, rule impI, (erule conjE)+,
        frule_tac H = x in aGroup.asubg_inc_zero[of M], assumption+)

apply ((rule ballI)+, simp, rule allI, rule impI)
 apply (drule_tac x = x in spec,
        drule_tac x = x in spec)
 apply simp
 apply (frule_tac H = x and x = b in aGroup.asubg_mOp_closed[of M], simp+)
 apply (rule_tac H = x and x = a and y = "-aMb" in 
        aGroup.asubg_pOp_closed[of M], assumption+, simp+)
done

lemma (in Ring) linear_comb_memTr:"R module M; submodule R M N; T  N 
      f s. f  {j. j  (n::nat)}  T  s  {j. j  n}  carrier R  
      l_comb R M n s f  N"
apply (induct_tac n)
 apply ((rule allI)+, rule impI, (erule conjE)+)
 apply (simp add:l_comb_def del: Pi_split_insert_domain)
 apply (rule Module.submodule_sc_closed, assumption+)
 apply (simp add:Pi_def, simp add:Pi_def subsetD)

apply ((rule allI)+, rule impI, erule conjE)
 apply (frule_tac f = f and n = n and A = T in func_pre)
 apply (frule_tac f = s and n = n and A = "carrier R" in func_pre) 
 apply (drule_tac x = f in spec,
        drule_tac x = s in spec, simp)
       
 apply (cut_tac whole_ideal,
        frule_tac s = s and n = n and f = f in 
                   Module.l_comb_Suc[of M R T "carrier R"],
        frule Module.submodule_subset[of M R N], assumption+,
        rule subset_trans[of T N "carrier M"], assumption+, simp,
        thin_tac "l_comb R M (Suc n) s f =
        l_comb R M n s f ±Ms (Suc n) sMf (Suc n)")
 apply (rule Module.submodule_pOp_closed[of M R N], assumption+,
        rule_tac a = "s (Suc n)" and h = "f (Suc n)" in 
         Module.submodule_sc_closed[of M R N], assumption+,
        simp add:Pi_def, simp add:Pi_def subsetD)
done

lemma (in Ring) linear_comb_mem:"R module M; submodule R M N; T  N; 
      f  {j. j  (n::nat)}  T; s  {j. j  n}  carrier R  
                      l_comb R M n s f  N"
apply (simp add:linear_comb_memTr)
done

lemma (in Ring) LSM_eq_linear_span:"R module M; T  carrier M  
          (LSM⇘RM T) = linear_span R M (carrier R) T"
apply (cut_tac whole_ideal)
apply (rule equalityI)
 apply (frule Module.linear_span_subModule[of M R "carrier R" T], assumption+)
 apply (rule LSM_sub_submodule[of M T "linear_span R M (carrier R) T"],
        assumption+, simp add:Module.l_span_cont_H[of M R T])
 apply (frule LSM_submodule[of M T], assumption) 
 apply (frule LSM_inc_T[of M T], assumption)
 apply (rule Module.l_span_sub_submodule[of M R "carrier R" "LSM⇘RM T" T],
         assumption+)
done

lemma (in Ring) LSM_sub_ker:"R module M; R module N; T  carrier M; 
       f  mHom R M N; T  ker⇘M,Nf  LSM⇘RM T  ker⇘M,Nf"
apply (frule Module.mker_submodule[of M R N f], assumption+)
apply (rule LSM_sub_submodule[of M T "ker⇘M,Nf"], assumption+)
done

(* in the following costdefs, MN is the free module generated by  M ×c N *)
definition
  tensor_relations1 :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
       ('b, 'r, 'm1) Module_scheme, (('a * 'b), 'r, 'm1) Module_scheme]  
       ('a * 'b) set"
       ((4TR1/ _/ _/ _/ _) [100,100,100,101]100) where
  "TR1 R M N MN = {x. m1carrier M. m2carrier M. ncarrier N.
       x = (m1 ±Mm2, n) ±MN(-aMN((m1, n) ±MN(m2, n)))}"

definition
  tensor_relations2 :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
      ('b, 'r, 'm1) Module_scheme, (('a * 'b), 'r, 'm1) Module_scheme] 
       ('a * 'b) set"
       ((4TR2/ _/ _/ _/ _) [100,100,100, 101]100) where
   "TR2 R M N MN = {x. mcarrier M. n1carrier N. n2carrier N.
            x = (m, n1 ±Nn2) ±MN(-aMN((m, n1) ±MN(m, n2)))}"

definition
  tensor_relations3 :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
      ('b, 'r, 'm1) Module_scheme, (('a * 'b), 'r, 'm1) Module_scheme]  
      ('a * 'b ) set"
       ((4TR3/ _/ _/ _/ _) [100,100,100,101]100) where
  "TR3 R M N P = {x. mcarrier M. ncarrier N.  acarrier R.
        x = (a sMm, n) ±P(-aP(a sP(m, n)))}"

definition
  tensor_relations4 :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
    ('b, 'r, 'm1) Module_scheme, (('a * 'b), 'r, 'm1) Module_scheme] 
    ('a * 'b) set"
                  ((4TR4/ _/ _/ _/ _) [100,100,100,101]100) where
  "TR4 R M N MN = {x. mcarrier M. ncarrier N.  acarrier R.
  x = (m, a sNn) ±MN(-aMN(a sMN(m, n)))}"

definition
  tensor_relations :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
    ('b, 'r, 'm1) Module_scheme, (('a * 'b), 'r, 'm1) Module_scheme] 
    ('a * 'b) set"
                   ((4TR⇘_ _/ _/ _) [100,100,101]100) where
  "TR⇘RM N MN = LSM⇘RMN ((TR1 R M N MN)  (TR2 R M N MN)  
                                     (TR3 R M N MN)  (TR4 R M N MN))"

definition
  tensor_product :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
   ('b, 'r, 'm1) Module_scheme, (('a * 'b), 'r, 'm1) Module_scheme]  
   (('a * 'b) set, 'r) Module" where
  "tensor_product R M N MN = MN /m (TR⇘RM N MN)" 

abbreviation
  TENSORPROD  ((4_/ _⇙⨂⇘_/ _) [92,92,92,93]92) where
  "MP⇙⨂⇘RN == tensor_product R M N P"

lemma (in Ring) mem_cartesian:"R module M; R module N; m  carrier M;
      n  carrier N   (m, n)  M ×c N"
by (simp add:prod_carr_def)

lemma (in Ring) cartesianTr:"R module M; R module N; x  M ×c N  
       m n. mcarrier M  n  carrier N  x = (m, n)"
by (cases x) (simp add: prod_carr_def)

lemma (in Ring) free_module_mem:"R module M; R module N; m  carrier M;
         n  carrier N; FM⇘RP M N   (m, n)  carrier P"
 apply (rule free_gen_mem [of P M N], assumption+)
 apply (simp add:prod_carr_def)
done

lemma (in Ring) FM_P_module:"R module M; R module N; FM⇘RP M N
         R module P"
by (simp add:fm_gen_by_prod_def)

lemma (in Ring) TR1_sub_carr:"R module M; R module N; FM⇘RP M N  
                                      (TR1 R M N P)  carrier P"
apply (simp add:fm_gen_by_prod_def)
apply (erule conjE)
 apply (rule subsetI, simp add:tensor_relations1_def)
 apply ((erule bexE)+,
        frule Module.module_is_ag [of P], simp)
 apply (rule aGroup.ag_pOp_closed, assumption,
        rule free_module_mem, assumption+)
 apply (frule Module.module_is_ag[of M R],
        rule aGroup.ag_pOp_closed [of "M"], assumption+,
        simp add:fm_gen_by_prod_def)
apply (rule aGroup.ag_mOp_closed[of P], assumption,
       rule aGroup.ag_pOp_closed [of "P"], assumption+,
       rule free_module_mem, assumption+,
       simp add:fm_gen_by_prod_def)
 apply (rule free_module_mem, assumption+, simp add:fm_gen_by_prod_def)
done

lemma (in Ring) TR2_sub_carr:"R module M; R module N; FM⇘RP M N  
               (TR2 R M N P)  carrier P"
apply (simp add:fm_gen_by_prod_def, erule conjE)
 apply (rule subsetI) 
 apply (simp add:tensor_relations2_def, (erule bexE)+, simp)
 apply (frule Module.module_is_ag[of P R]) 
 apply (rule aGroup.ag_pOp_closed, assumption)
 apply (rule free_module_mem, assumption+)
 apply (frule Module.module_is_ag [of N R])
 apply (rule aGroup.ag_pOp_closed [of "N"], assumption+,
        simp add:fm_gen_by_prod_def)
apply (rule aGroup.ag_mOp_closed [of "P"], assumption+,
       rule aGroup.ag_pOp_closed [of "P"], assumption+,
       rule free_module_mem, assumption+, simp add:fm_gen_by_prod_def)
 apply (rule free_module_mem, assumption+,
        simp add:fm_gen_by_prod_def)
done

lemma (in Ring) TR3_sub_carr:"R module M; R module N; FM⇘RP M N 
                      (TR3 R M N P)  carrier P"
apply (simp add:fm_gen_by_prod_def)
apply (erule conjE)
 apply (rule subsetI) 
 apply (simp add:tensor_relations3_def)
 apply ((erule bexE)+, simp,
        thin_tac "x = (a sMm, n) ±P-aP(a sP(m, n))")

 apply (frule Module.module_is_ag [of P R]) 
 apply (rule aGroup.ag_pOp_closed, assumption)
 apply (rule free_module_mem, assumption+)
 apply (simp add:Module.sc_mem, assumption,
        simp add:fm_gen_by_prod_def)
 apply (rule aGroup.ag_mOp_closed[of "P"], assumption+)
 apply (rule Module.sc_mem, assumption+)
 apply (rule free_module_mem, assumption+,
        simp add:fm_gen_by_prod_def)
done

lemma (in Ring) TR4_sub_carr:"R module M; R module N; FM⇘RP M N  
                       (TR4 R M N P)  carrier P"
apply (simp add:fm_gen_by_prod_def)
apply (erule conjE)
 apply (rule subsetI) 
 apply (simp add:tensor_relations4_def)
 apply ((erule bexE)+, simp)

 apply (frule Module.module_is_ag [of P R]) 
 apply (rule aGroup.ag_pOp_closed, assumption)
 apply (rule free_module_mem, assumption+,
        simp add:Module.sc_mem, simp add:fm_gen_by_prod_def)
 apply (rule aGroup.ag_mOp_closed[of P], assumption+)
 apply (rule Module.sc_mem, assumption+)
 apply (rule free_module_mem, assumption+,
        simp add:fm_gen_by_prod_def)
done

lemma (in Ring) TR_sub_carr:"R module M; R module N; FM⇘RP M N 
  (TR1 R M N P)  (TR2 R M N P)  (TR3 R M N P)  (TR4 R M N P)  carrier P"
apply (rule subsetI)
 apply (case_tac "x  TR1 R M N P", simp)
 apply (frule TR1_sub_carr [of M N P], assumption+, simp add:subsetD)

 apply (case_tac "x  TR2 R M N P", simp,
        frule TR2_sub_carr [of M N P], assumption+, simp add:subsetD)

 apply (case_tac "x  TR3 R M N P", simp,
         frule TR3_sub_carr [of M N P], assumption+, simp add:subsetD)  

apply simp
 apply (frule TR4_sub_carr [of M N P], assumption+, simp add:subsetD)
done
 
lemma (in Ring) TR_submodule:"R module M; R module N; FM⇘RP M N  
                 submodule R P (TR⇘RM N P)"
apply (simp add:tensor_relations_def)
 apply (rule LSM_submodule[of P _])
 apply (simp add:fm_gen_by_prod_def)
 apply (rule TR_sub_carr, assumption+)
done

lemma (in Ring) TR_cont_TR1234:"R module M; R module N; FM⇘RP M N 
  TR1 R M N P  TR2 R M N P  TR3 R M N P  TR4 R M N P  TR⇘RM N P" 
apply (simp add:tensor_relations_def)
 apply (cut_tac LSM_inc_T [of P "TR1 R M N P  TR2 R M N P  TR3 R M N P 
         TR4 R M N P"], simp) 
 apply (simp add:fm_gen_by_prod_def)
 apply (rule TR_sub_carr, assumption+)
done

lemma (in Ring) TR1_mem:"R module M; R module N; FM⇘RP M N; m1  carrier M;
m2  carrier M; n  carrier N  (m1 ±Mm2, n) ±P-aP((m1, n) ±P(m2, n))
  TR⇘RM N P"
apply (rule subsetD[of "TR1 R M N P" "TR⇘RM N P" 
                          "(m1 ±Mm2, n) ±P-aP((m1, n) ±P(m2, n))"])
apply (rule subset_trans[of "TR1 R M N P" "TR1 R M N P  TR2 R M N P 
       TR3 R M N P  TR4 R M N P" "TR⇘RM N P"],
       rule subsetI, simp,
       cut_tac TR_cont_TR1234[of M N P], assumption+)
apply (simp add:tensor_relations1_def)
apply blast
done

lemma (in Ring) TR2_mem:"R module M; R module N; FM⇘RP M N; m  carrier M;
       n1  carrier N; n2  carrier N   
     (m, n1 ±Nn2) ±P-aP((m, n1) ±P(m, n2))  TR⇘RM N P"
apply (rule subsetD[of "TR2 R M N P" "TR⇘RM N P" 
                          "(m, n1 ±Nn2) ±P-aP((m, n1) ±P(m, n2))"])
apply (rule subset_trans[of "TR2 R M N P" "TR1 R M N P  TR2 R M N P 
       TR3 R M N P  TR4 R M N P" "TR⇘RM N P"],
       rule subsetI, simp,
       cut_tac TR_cont_TR1234[of M N P], assumption+)
apply (simp add:tensor_relations2_def)
apply blast
done

lemma (in Ring) TR3_mem:"R module M; R module N; FM⇘RP M N; m  carrier M;
      n  carrier N; a  carrier R  
         (a sMm, n) ±P-aP(a sP(m, n))  TR⇘RM N P"
apply (rule subsetD[of "TR3 R M N P" "TR⇘RM N P" 
                          "(a sMm, n) ±P-aP(a sP(m, n))"])
apply (rule subset_trans[of "TR3 R M N P" "TR1 R M N P  TR2 R M N P 
       TR3 R M N P  TR4 R M N P" "TR⇘RM N P"],
       rule subsetI, simp,
       cut_tac TR_cont_TR1234[of M N P], assumption+)
apply (simp add:tensor_relations3_def)
apply blast
done

lemma (in Ring) TR4_mem:"R module M; R module N; FM⇘RP M N; m  carrier M;
       n  carrier N; a  carrier R  
            (m, a sNn) ±P-aP(a sP(m, n))  TR⇘RM N P"
apply (rule subsetD[of "TR4 R M N P" "TR⇘RM N P" 
                          "(m, a sNn) ±P-aP(a sP(m, n))"])
apply (rule subset_trans[of "TR4 R M N P" "TR1 R M N P  TR2 R M N P 
       TR3 R M N P  TR4 R M N P" "TR⇘RM N P"],
       rule subsetI, simp,
       cut_tac TR_cont_TR1234[of M N P], assumption+)
apply (simp add:tensor_relations4_def)
apply blast
done

lemma (in Ring) tensor_product_module:"R module M; R module N; FM⇘RP M N  
       R module (tensor_product R M N P)" 
apply (simp add:fm_gen_by_prod_def, erule conjE)
apply (frule TR_submodule [of M N P], assumption+)
 apply (simp add:fm_gen_by_prod_def)
 apply (simp add:tensor_product_def)
 apply (simp add:Module.qmodule_module [of P R "TR⇘RM N P"])
done

lemma (in Ring) tau_mpj_bilin1:" R module M; R module N; FM⇘RP M N;
        x1  carrier M; x2  carrier M; y  carrier N   
  (mpj P (TR⇘RM N P)) ( x1 ±Mx2, y) = 
    (mpj P (TR⇘RM N P)) (x1, y) ±(MP⇙⨂⇘RN)(mpj   P (TR⇘RM N P) (x2, y))" 
apply (frule FM_P_module[of M N P], assumption+)
apply (subgoal_tac "(x1 ±Mx2, y) ±P(-aP((x1, y) ±P(x2, y)))   
              ker⇘P,(P /m (TR⇘RM N P))(mpj P (TR⇘RM N P))")
apply (frule TR_submodule [of M N P], assumption+,
       frule Module.qmodule_module [of P R "TR⇘RM N P"], assumption+,
       frule Module.mpj_mHom [of P R "TR⇘RM N P"], assumption+,
       frule  Module.mHom_eq_ker[of P R "P /m (TR⇘RM N P)" "mpj P (TR⇘RM N P)"
              "( x1 ±Mx2, y)" "(x1, y) ±P(x2, y)"], assumption+,
       rule free_module_mem, assumption+)
  apply (frule Module.module_is_ag [of M R],
         simp add:aGroup.ag_pOp_closed, assumption+,
         frule Module.module_is_ag [of P R],
         rule aGroup.ag_pOp_closed, assumption+,
         rule free_module_mem, assumption+,
         rule free_module_mem, assumption+)
 apply (frule_tac m = x1 and n = y in free_module_mem[of M N _ _ P],
                  assumption+,
        frule_tac m = x2 and n = y in free_module_mem[of M N _ _ P],
                  assumption+,
        simp add:Module.mHom_add[of P R "P /m (TR⇘RM N P)" "mpj P (TR⇘RM N P)"
          "(x1, y)" "(x2, y)"],
        simp add:tensor_product_def)
apply (frule TR_submodule [of M N P], assumption+, 
       simp add:Module.mker_of_mpj, thin_tac "submodule R P (TR⇘RM N P)",
       simp add:TR1_mem)
done

lemma (in Ring) tau_mpj_bilin2:"R module M; R module N; FM⇘RP M N;
       m  carrier M; n1  carrier N; n2  carrier N   
  (mpj P (TR⇘RM N P)) (m, n1 ±Nn2) = 
   (mpj P (TR⇘RM N P)) (m, n1) ±(MP⇙⨂⇘RN)(mpj P (TR⇘RM N P) (m, n2))"
apply (frule FM_P_module[of M N P], assumption+)
apply (subgoal_tac "(m, n1 ±Nn2) ±P(-aP((m, n1) ±P(m, n2)))   
              ker⇘P,(P /m (TR⇘RM N P))(mpj P (TR⇘RM N P))")
apply (frule TR_submodule [of M N P], assumption+,
       frule Module.qmodule_module [of P R "TR⇘RM N P"], assumption+,
       frule Module.mpj_mHom [of P R "TR⇘RM N P"], assumption+,
       frule  Module.mHom_eq_ker[of P R "P /m (TR⇘RM N P)" "mpj P (TR⇘RM N P)"
              "(m, n1 ±Nn2)" "(m, n1) ±P(m, n2)"], assumption+,
       rule free_module_mem, assumption+)
  apply (frule Module.module_is_ag [of N R],
         simp add:aGroup.ag_pOp_closed, assumption+,
         frule Module.module_is_ag [of P R],
         rule aGroup.ag_pOp_closed, assumption+,
         rule free_module_mem, assumption+,
         rule free_module_mem, assumption+)
 apply (frule_tac m = m and n = n1 in free_module_mem[of M N _ _ P],
                  assumption+,
        frule_tac m = m and n = n2 in free_module_mem[of M N _ _ P],
                  assumption+, simp add:tensor_product_def)
 apply (rule Module.mHom_add[of P R "P /m (TR⇘RM N P)" "mpj P (TR⇘RM N P)"
          "(m, n1)" "(m, n2)"], assumption+) 
apply (frule TR_submodule [of M N P], assumption+, 
       simp add:Module.mker_of_mpj, thin_tac "submodule R P (TR⇘RM N P)",
       simp add:TR2_mem)
done

lemma (in Ring) tau_mpj_bilin3:"R module M; R module N; FM⇘RP M N;
   m  carrier M; n  carrier N; a  carrier R   
  (mpj P (TR⇘RM N P)) (a sMm, n) = a s(MP⇙⨂⇘RN)(mpj P (TR⇘RM N P) (m, n))"
apply (frule FM_P_module[of M N P], assumption+)
apply (subgoal_tac "(a sMm, n) ±P-aP(a sP(m, n))  
          ker⇘P,(P /m (TR⇘RM N P))(mpj P (TR⇘RM N P))")
apply (frule TR_submodule [of M N P], assumption+,
       frule Module.qmodule_module [of P R "TR⇘RM N P"], assumption+,
       frule Module.mpj_mHom [of P R "TR⇘RM N P"], assumption+,
       frule  Module.mHom_eq_ker[of P R "P /m (TR⇘RM N P)" "mpj P (TR⇘RM N P)"
              "(a sMm, n)" "a sP(m, n)"], assumption+,
       rule free_module_mem, assumption+)
  apply (simp add:Module.sc_mem, assumption+)
 apply (frule_tac m = m and n = n in free_module_mem[of M N _ _ P],
                  assumption+,
        simp add:Module.sc_mem,
                  assumption+, simp add:tensor_product_def)
 apply (rule Module.mHom_lin[of P R "P /m (TR⇘RM N P)" "(m, n)"
        "mpj P (TR⇘RM N P)" a], assumption+, simp add:free_module_mem,
        assumption+) 
apply (frule TR_submodule [of M N P], assumption+, 
       simp add:Module.mker_of_mpj, thin_tac "submodule R P (TR⇘RM N P)",
       simp add:TR3_mem)
done

lemma (in Ring) tau_mpj_bilin4:"R module M; R module N; FM⇘RP M N; 
      m  carrier M; n  carrier N; a  carrier R  
     (mpj P (TR⇘RM N P)) (m, a sNn) = a s(MP⇙⨂⇘RN)(mpj P (TR⇘RM N P) (m, n))"
apply (frule FM_P_module[of M N P], assumption+)
apply (subgoal_tac "(m, a sNn) ±P-aP(a sP(m, n))  
          ker⇘P,(P /m (TR⇘RM N P))(mpj P (TR⇘RM N P))")
apply (frule TR_submodule [of M N P], assumption+,
       frule Module.qmodule_module [of P R "TR⇘RM N P"], assumption+,
       frule Module.mpj_mHom [of P R "TR⇘RM N P"], assumption+,
       frule  Module.mHom_eq_ker[of P R "P /m (TR⇘RM N P)" "mpj P (TR⇘RM N P)"
              "(m, a sNn)" "a sP(m, n)"], assumption+,
       rule free_module_mem, assumption+)
  apply (simp add:Module.sc_mem, assumption+)
 apply (frule_tac m = m and n = n in free_module_mem[of M N _ _ P],
                  assumption+,
        simp add:Module.sc_mem,
                  assumption+, simp add:tensor_product_def)
 apply (rule Module.mHom_lin[of P R "P /m (TR⇘RM N P)" "(m, n)"
        "mpj P (TR⇘RM N P)" a], assumption+, simp add:free_module_mem,
        assumption+) 
apply (frule TR_submodule [of M N P], assumption+, 
       simp add:Module.mker_of_mpj, thin_tac "submodule R P (TR⇘RM N P)",
       simp add:TR4_mem)
done

definition
  tau :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
        ('b, 'r, 'm1) Module_scheme, (('a * 'b), 'r, 'm1) Module_scheme]  
                                            ('a * 'b)  ('a * 'b)" where
  "tau R M N P = (λx(M ×c N). x)"

lemma (in Ring) tau_func:"R module M; R module N; FM⇘RP M N 
                                 tau R M N P  M ×c N  carrier P"
 apply (rule Pi_I)
 apply (frule_tac x = x in cartesianTr [of M N], assumption+)
 apply ((erule exE)+, (erule conjE)+, simp add:tau_def)
apply (simp add:free_module_mem)
done

lemma (in Ring) tau_mem:"R module M; R module N; m  carrier M; 
      n  carrier N; FM⇘RP M N  tau R M N P (m, n)  carrier P"
apply (frule tau_func [of M N P], assumption+)
apply (rule funcset_mem, assumption+)
apply (simp add:prod_carr_mem)
done

lemma (in Ring) tau_inj0:"¬ zeroring R;  R module M; R module N; FM⇘RP M N 
       inj_on (tau R M N P) (M ×c N)"
apply (simp add:inj_on_def, (rule ballI)+)
 apply (rule impI)
 apply (simp add:tau_def)
done

lemma (in Ring) tau_inj1:"zeroring R; R module M; R module N; FM⇘RP M N 
           inj_on (tau R M N P) (M ×c N)"
apply (simp add:inj_on_def)
apply ((rule ballI)+, rule impI)
apply (frule module_over_zeroring[of M], assumption+)
apply (frule module_over_zeroring[of N], assumption+)
apply (simp add:zeroring_def, erule conjE)
apply (frule_tac x = x in cartesianTr[of M N], assumption+)
 apply ((erule exE)+, (erule conjE)+, simp add:tau_def)
done  

lemma (in Ring) tau_inj:"R module M; R module N; FM⇘RP M N  
                inj_on (tau R M N P) (M ×c N)"
apply (case_tac "zeroring R")
 apply (simp add:tau_inj1)
 apply (simp add:tau_inj0)
done  

lemma (in Ring) tau_mpj_bilinear:"R module M; R module N; FM⇘RP M N   
      bilinear_map (compose (M ×c N) (mpj P (TR⇘RM N P)) (tau R M N P)) 
                     R M N (MP⇙⨂⇘RN)"
apply (simp add:bilinear_map_def)
apply (rule conjI)
 apply (rule Pi_I)
 apply (simp add:compose_def tau_def tensor_product_def)
 apply (frule TR_submodule [of M N P], assumption+)
 apply (frule FM_P_module[of M N P], assumption+,
        frule Module.mpj_mHom[of P R "TR⇘RM N P"], assumption+)
 apply (frule Module.qmodule_module [of P R "TR⇘RM N P"], assumption+,
       rule Module.mHom_mem [of P R "P /m (TR⇘RM N P)" "(mpj P (TR⇘RM N P))"],
       assumption+, simp add:free_gen_mem)
apply (rule conjI)
 apply (rule ballI)+
 apply (simp add:compose_def tau_def mem_cartesian)
 apply (frule Module.module_is_ag[of M R],
        frule_tac x = x1 and y = x2 in aGroup.ag_pOp_closed, assumption+,
        simp add:mem_cartesian, simp add:tau_mpj_bilin1)
apply (rule conjI)
 apply (rule ballI)+
 apply (simp add:compose_def tau_def mem_cartesian)
 apply (frule Module.module_is_ag[of N R],
        frule_tac x = y1 and y = y2 in aGroup.ag_pOp_closed, assumption+,
        simp add:mem_cartesian, simp add:tau_mpj_bilin2)
apply (rule ballI)+
 apply (rule conjI)
 apply (simp add:compose_def tau_def mem_cartesian)
 apply (frule_tac a = r and m = x in Module.sc_mem[of M R], assumption+,
        simp add:mem_cartesian, simp add:tau_mpj_bilin3)
 apply (simp add:compose_def tau_def mem_cartesian)
 apply (frule_tac a = r and m = y in Module.sc_mem[of N R], assumption+,
        simp add:mem_cartesian, simp add:tau_mpj_bilin4)
done
    
definition
  tnm :: "[('r, 'm) Ring_scheme, (('a * 'b), 'r, 'm1) Module_scheme, 
        ('a, 'r, 'm1) Module_scheme, ('b, 'r, 'm1) Module_scheme]  
        ('a * 'b)  ('a * 'b) set" where
  "tnm R P M N = compose (M ×c N) (mpj P (TR⇘RM N P)) (tau R M N P)" 
 (* tensor natural map *)

lemma (in Ring) tnm_bilinear:"R module M; R module N; FM⇘RP M N  
        bilinear_map (tnm R P M N) R M N (MP⇙⨂⇘RN)"
apply (simp add:tnm_def)
apply (simp add:tau_mpj_bilinear)
done  

lemma (in Ring) tnm_mem:" R module M; R module N; FM⇘RP M N; m  carrier M; 
       n  carrier N   tnm R P M N (m, n)  carrier (MP⇙⨂⇘RN)"
apply (simp add:tnm_def)
apply (frule tau_mem [of M N m n], assumption+)
 apply (simp add:compose_def, simp add:prod_carr_def)
 apply (simp add:tensor_product_def)
 apply (frule TR_submodule [of M N P], assumption+) 
apply (rule Module.mpj_mem[of P R "TR⇘RM N P" "tau R M N P (m, n)"],
       rule FM_P_module[of M N P], assumption+)
done

definition
  tensor_elem :: "[('r, 'm) Ring_scheme, (('a * 'b), 'r, 'm1) Module_scheme, 
   ('a, 'r, 'm1) Module_scheme, ('b, 'r, 'm1) Module_scheme]  'a   'b 
    ('a * 'b) set" where
  "tensor_elem R P M N m n = tnm R P M N (m, n)" 

abbreviation
  TNSELEM  ((6_ _,_⇙⊗⇘_,_/ _) [100,100,100,100,100,101]101) where
  "mR,P⇙⊗⇘M,Nn == tensor_elem R P M N m n"
  
lemma (in Ring) tensor_univ_propTr:"R module M; R module N; FM⇘RP M N; 
      R module Z; bilinear_map f R M N Z 
     g. g  mHom R P Z  (compose (M ×c N) g (tau R M N P)) = f"
apply (unfold fm_gen_by_prod_def)
 apply (frule conjunct1, frule conjunct2) 
 apply (fold fm_gen_by_prod_def)
 apply (frule bilinear_func[of f M N Z])
apply (frule Module.exist_extension_mhom [of P R Z "M ×c N" "f"], assumption+)
 apply (erule bexE, rename_tac h)
 apply (subgoal_tac "compose (M ×c N) h (tau R M N P) = f")
 apply blast
apply (rule funcset_eq [of _ "M ×c N"])
 apply (simp add:compose_def, simp add:bilinear_map_def)
 apply (rule ballI)
 apply (simp add:compose_def tau_def)
done

lemma (in Ring) tensor_univ_propTr1:"R module M; R module N; FM⇘RP M N; 
      R module Z; bilinear_map f R M N Z 
     ∃!g. g(mHom R (MP⇙⨂⇘RN) Z)  (compose (M ×c N) g (tnm R P M N)) = f"
apply (frule Module.module_is_ag[of M],
       frule Module.module_is_ag[of N],
       frule FM_P_module[of M N P], assumption+,
       frule Module.module_is_ag[of P])
apply (simp add:tnm_def)

 apply (frule tensor_univ_propTr[of M N P Z f], assumption+)
 apply (erule exE, erule conjE, 
        frule TR_submodule[of M N P], assumption+,
        frule_tac Module.indmhom1[of P R "TR⇘RM N P" Z], assumption+,
        frule Module.submodule_subset[of P R "TR⇘RM N P"], assumption+,
        simp add:tensor_relations_def)
 apply (rule_tac f = g in LSM_sub_ker[of P Z "(TR1 R M N P  TR2 R M N P 
         TR3 R M N P  TR4 R M N P)"], assumption+, 
        rule TR_sub_carr, assumption+,
        thin_tac "submodule R P
          (LSM⇘RP (TR1 R M N P  TR2 R M N P  TR3 R M N P  TR4 R M N P))",
        thin_tac "LSM⇘RP (TR1 R M N P  TR2 R M N P  TR3 R M N P  
          TR4 R M N P)  carrier P")

(** show member of TR1 is in kerP,Z g **)
 apply (rule subsetI, simp, erule disjE,
        frule TR1_sub_carr[of M N P], assumption+,
        frule_tac c = x in subsetD[of "TR1 R M N P" "carrier P"], assumption+,
        thin_tac "TR1 R M N P  carrier P",
        simp add:tensor_relations1_def, (erule bexE)+, simp add:ker_def)
 apply (frule_tac x = m1 and y = m2 in aGroup.ag_pOp_closed, assumption+,
        frule_tac m = "m1 ±Mm2" and n = n in free_module_mem[of M N], 
         assumption+,
        frule_tac m = m1 and n = n in free_module_mem[of M N], assumption+,
        frule_tac m = m2 and n = n in free_module_mem[of M N], assumption+)
apply (subst Module.mHom_add[of P R Z], assumption+,
       rule aGroup.ag_mOp_closed, assumption+,
       rule aGroup.ag_pOp_closed, assumption+,
       frule_tac x = "(m1, n)" and y = "(m2, n)" in aGroup.ag_pOp_closed, 
       assumption+,
       simp add:Module.mHom_inv[of P R Z] Module.mHom_add)
 apply (frule_tac f = "compose (M ×c N) g (tau R M N P)" and g = f and 
                x = "(m1 ±Mm2, n)" in eq_fun_eq_val, 
        frule_tac f = "compose (M ×c N) g (tau R M N P)" and g = f and 
                x = "(m1, n)" in eq_fun_eq_val,
        frule_tac f = "compose (M ×c N) g (tau R M N P)" and g = f and 
                x = "(m2, n)" in eq_fun_eq_val,
        thin_tac "compose (M ×c N) g (tau R M N P) = f",
        simp add:compose_def cmp_def, simp add:mem_cartesian tau_def,
        simp add:bilinear_l_add1)

(** show member of TR2 is in kerP,Z g **)
 apply (erule disjE,
        frule TR2_sub_carr[of M N P], assumption+,
        frule_tac c = x in subsetD[of "TR2 R M N P" "carrier P"], assumption+,
        thin_tac "TR2 R M N P  carrier P",
        simp add:tensor_relations2_def, (erule bexE)+, simp add:ker_def)
 apply (frule_tac x = n1 and y = n2 in aGroup.ag_pOp_closed, assumption+,
        frule_tac m = m and n = "n1 ±Nn2" in free_module_mem[of M N], 
         assumption+,
        frule_tac m = m and n = n1 in free_module_mem[of M N], assumption+,
        frule_tac m = m and n = n2 in free_module_mem[of M N], assumption+)
apply (subst Module.mHom_add[of P R Z], assumption+,
       rule aGroup.ag_mOp_closed, assumption+,
       rule aGroup.ag_pOp_closed, assumption+,
       frule_tac x = "(m, n1)" and y = "(m, n2)" in aGroup.ag_pOp_closed, 
       assumption+,
       simp add:Module.mHom_inv[of P R Z] Module.mHom_add)
 apply (frule_tac f = "compose (M ×c N) g (tau R M N P)" and g = f and 
                x = "(m, n1 ±Nn2)" in eq_fun_eq_val, 
        frule_tac f = "compose (M ×c N) g (tau R M N P)" and g = f and 
                x = "(m, n1)" in eq_fun_eq_val,
        frule_tac f = "compose (M ×c N) g (tau R M N P)" and g = f and 
                x = "(m, n2)" in eq_fun_eq_val,
        thin_tac "compose (M ×c N) g (tau R M N P) = f",
        simp add:compose_def cmp_def, simp add:mem_cartesian tau_def,
        simp add:bilinear_r_add1)   

(** show member of TR3 is in kerP,Z g **)
 apply (erule disjE,
        frule TR3_sub_carr[of M N P], assumption+,
        frule_tac c = x in subsetD[of "TR3 R M N P" "carrier P"], assumption+,
        thin_tac "TR3 R M N P  carrier P",
        simp add:tensor_relations3_def, (erule bexE)+, simp add:ker_def)
 apply (frule_tac a = a and m = m in Module.sc_mem, assumption+,
        frule_tac m = "a sMm" and n = n in free_module_mem[of M N], 
         assumption+,
        frule_tac m = m and n = n in free_module_mem[of M N], assumption+,
        frule_tac a = a and m = "(m, n)" in Module.sc_mem, assumption+,
        frule_tac x = "(a sP(m, n))" in aGroup.ag_mOp_closed, assumption+)
apply (subst Module.mHom_add[of P R Z], assumption+,
       simp add:Module.mHom_inv[of P R Z],
       simp add:Module.mHom_lin)
 apply (frule_tac f = "compose (M ×c N) g (tau R M N P)" and g = f and 
                x = "(a sMm, n)" in eq_fun_eq_val, 
        frule_tac f = "compose (M ×c N) g (tau R M N P)" and g = f and 
                x = "(m, n)" in eq_fun_eq_val)
     apply (thin_tac "compose (M ×c N) g (tau R M N P) = f",
        simp add:compose_def cmp_def, simp add:mem_cartesian tau_def,
        simp add:bilinear_l_lin1)   

(** show member of TR4 is in kerP,Z g **)
 apply (frule TR4_sub_carr[of M N P], assumption+,
        frule_tac c = x in subsetD[of "TR4 R M N P" "carrier P"], assumption+,
        thin_tac "TR4 R M N P  carrier P",
        simp add:tensor_relations4_def, (erule bexE)+, simp add:ker_def)
 apply (frule_tac a = a and m = n in Module.sc_mem, assumption+,
        frule_tac m = m and n = "a sNn" in free_module_mem[of M N], 
         assumption+,
        frule_tac m = m and n = n in free_module_mem[of M N], assumption+,
        frule_tac a = a and m = "(m, n)" in Module.sc_mem, assumption+,
        frule_tac x = "(a sP(m, n))" in aGroup.ag_mOp_closed, assumption+)
apply (subst Module.mHom_add[of P R Z], assumption+,
       simp add:Module.mHom_inv[of P R Z],
       simp add:Module.mHom_lin)
 apply (frule_tac f = "compose (M ×c N) g (tau R M N P)" and g = f and 
                x = "(m, a sNn)" in eq_fun_eq_val, 
        frule_tac f = "compose (M ×c N) g (tau R M N P)" and g = f and 
                x = "(m, n)" in eq_fun_eq_val)
     apply (thin_tac "compose (M ×c N) g (tau R M N P) = f",
        simp add:compose_def cmp_def, simp add:mem_cartesian tau_def)
        apply (simp add:bilinear_r_lin1)
 apply (frule Module.qmodule_module [of P R "TR⇘RM N P"], assumption+)
 apply (rule ex_ex1I)   
     apply (erule ex1E, erule conjE,
            thin_tac "y. y  mHom R (P /m (TR⇘RM N P)) Z 
            compos P y (mpj P (TR⇘RM N P)) = g  y = ga")
   apply (simp add:tensor_product_def)
   apply (cut_tac g = "mpj P (TR⇘RM N P)" and h = ga 
          in compose_assoc[of "tau R M N P" "M ×c N" "carrier P"],
         simp add:tau_func,
        simp add:compos_def, blast)         
 apply (simp add:tensor_product_def,
        thin_tac "∃!ga. ga  mHom R (P /m (TR⇘RM N P)) Z 
              compos P ga (mpj P (TR⇘RM N P)) = g")
  apply (frule free_gen_gen[of P M N],
         frule Module.surjec_generator[of P R "P /m (TR⇘RM N P)" 
         "mpj P (TR⇘RM N P)" "M ×c N"],
         simp add:Module.qmodule_module,
         simp add:Module.mpj_mHom,
         simp add:Module.mpj_surjec, assumption)
  apply (rule_tac f = ga and g = y in Module.gen_mHom_eq[of "P /m (TR⇘RM N P)"
         R Z "mpj P (TR⇘RM N P) ` M ×c N"], assumption+, simp, simp)
  apply (erule conjE)+
  apply (rule ballI,
      thin_tac "generator R (P /m (TR⇘RM N P)) (mpj P (TR⇘RM N P) ` M ×c N)",
      simp add:image_def, erule bexE)
  apply (frule_tac f = "compose (M ×c N) ga
         (compose (M ×c N) (mpj P (TR⇘RM N P)) (tau R M N P))" and g = f and 
         x = x in eq_fun_eq_val,
         thin_tac "compose (M ×c N) ga (compose (M ×c N) (mpj P (TR⇘RM N P))
                   (tau R M N P)) = f",
         frule_tac f = "compose (M ×c N) y
         (compose (M ×c N) (mpj P (TR⇘RM N P)) (tau R M N P))" and g = f and 
         x = x in eq_fun_eq_val,
         thin_tac "compose (M ×c N) y (compose (M ×c N) (mpj P (TR⇘RM N P))
                   (tau R M N P)) = f")
   apply (simp add:compose_def tau_def)
done
      
lemma (in Ring) tensor_universal_property:"R module M; R module N; FM⇘RP M N 
   universal_property R M N (MP⇙⨂⇘RN) (tnm R P M N)"
apply (simp add:universal_property_def)
 apply (frule tau_mpj_bilinear [of M N], assumption+)
 apply (rule conjI, simp add:tnm_def)
apply ((rule allI)+, rule impI, erule conjE)
 apply (rule_tac Z = Z and f = g in tensor_univ_propTr1 [of M N],
             assumption+)
done

    (*                    f
                  M × N  ⟶ Z
                    |       /
         tnp R M N  |      / g
                    |     /
                  M ⨂R N
     *)

end