Theory Algebra9

Up to index of Isabelle/HOL/Valuation

theory Algebra9
imports Algebra8
begin

(**        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 "8. exact sequence"

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

lemma (in Ring) Zm_Module:"R module (Zm R e)"
apply (simp add:Module_def)
apply (rule conjI)
 apply (simp add:aGroup_def)
 apply (simp add:Zm_def)
 apply (rule conjI)+
 apply (rule bivar_func_test)
 apply (rule ballI)+
 apply simp
 apply (rule univar_func_test)
 apply (rule ballI)
 apply simp
apply (simp add:Module_axioms_def)
apply (simp add:Zm_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 = \<zero>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 (rule univar_func_test, rule ballI,
        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 (HOMR M 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 = {\<zero>};
      carrier N = {\<zero>N}|] ==> M ≅R N"
apply (subgoal_tac "bijecM, N (λx∈{\<zero>}. \<zero>N) ∧
                          (λx∈{\<zero>}. \<zero>N) ∈ mHom R M N")
apply (simp add:misomorphic_def, blast,
       subgoal_tac "(λx∈{\<zero>}. \<zero>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)
apply (rule univar_func_test, rule ballI, simp)
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 ==> HOMR (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 = "HOMR (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 (HOMR M (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 ==> HOMR M (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)|] ==>
                              kerM,(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

constdefs
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" 
"exact3 R L0 h0 L1 h1 L2 == h0 ` (carrier L0) = ker(L1),(L2) h1"

constdefs
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" 
"exact4 R L0 h0 L1 h1 L2 h2 L3 == h0 ` (carrier L0) = ker(L1),(L2) h1 ∧ 
                                     h1 ` (carrier L1) = ker(L2),(L3) h2 "

constdefs
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" 
"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 "
constdefs
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" 
"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) ⊆ kerM,N g"
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) ⊆ kerM,N g"
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 "(λx∈carrier L. g (f x)) xa = mzeromap L N xa")
 prefer 2 apply simp
 apply (thin_tac "(λx∈carrier 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|] ==>
      injecM,N f"
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; injecM,N f|] ==> 
       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);
       injecM1,M2 f|]==>
   (λ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 "(λn∈carrier 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 "(λn∈carrier 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 "∃xa∈carrier 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 univar_func_test, rule ballI)
 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 ±N b" in mem_in_image[of x "carrier N" "carrier M2"],
        assumption+,
        frule_tac c = "x (a ±N b)" 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 "∃xa∈carrier N. x ( a ±N b) = x xa",
        thin_tac "∃xa∈carrier M1. x ( a ±N b) = f xa",
        thin_tac "∃xa∈carrier N. x a = x xa",
        thin_tac "∃xa∈carrier M1. x a = f xa",
        thin_tac "∃xa∈carrier N. x b = x xa",
        thin_tac "∃xa∈carrier 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 ·sN m" in mem_in_image[of x "carrier N" "carrier M2"],
        assumption+,
        frule_tac c = "x (a ·sN m)" 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 ·sN m) ∈ 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 "∃xa∈carrier M1. x m = f xa",
        thin_tac "∃xa∈carrier M1. x (a ·sN m) = 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)|] ==> surjecM,N f" 
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); surjecM,N f|] ==> 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)|] ==> bijecM,N f"
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 univar_func_test)
 apply (rule ballI)
 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 "∃x∈carrier M. a = g x", thin_tac "∃x∈carrier 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 ±N b = g xb")
 apply blast
 apply (thin_tac "∃x∈carrier M. a = g x", thin_tac "∃x∈carrier M. b = g x",
        thin_tac "∃x∈carrier M. a ±N b = 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 ±N b", 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 = kerM,N g ∧ g ` carrier M = kerN,Zm R e z1")
 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 ±M xa"
        and b = xb in Module.mHom_ker_eq, assumption+)
 apply (frule_tac c = "xaa ±M xa ±M -aM xb" in subsetD[of "kerM,N g" 
                   "kerM,Z x"], assumption+)
 apply (frule_tac a = "xaa ±M xa" 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 "∃x∈carrier M. m = g x") 
 apply (rule someI2_ex)  
 apply (erule bexE, rotate_tac -1, frule sym, thin_tac "a ·sN m = g xaa")
 apply blast
 apply (thin_tac "∃x∈carrier M. a ·sN m = 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 ·sM xa" 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 = kerM,N g ∧ g ` carrier M = kerN,Zm R e z1")
 apply (frule_tac c = "xaa ±M -aM (a ·sM xa)" in 
                  subsetD[of "kerM,N g " "kerM,Z x"], assumption+)
 apply (rule_tac a = xaa and b = "a ·sM xa" 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 = "(λz∈carrier 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 (λz∈carrier N. x 
                 (SOME y. y ∈ carrier M ∧ g y = z)) g ∈ mHom R M Z")
 apply (thin_tac "(λz∈carrier 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 "∃x∈carrier 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 -aM m" in subsetD[of "kerM,N g" "kerM,Z x"],
                  assumption+)
 apply (rule_tac a = xa and b = m in Module.mHom_eq_ker[of M R Z x],
           assumption+)
done
 
constdefs
 module_iota::"[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme] =>
                'a => 'a"  ("(mι_ _)" [92, 93]92)
 "mιR M == λx∈carrier 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 (HOMR (Zm R e) N) (sup_sharp R M3 (Zm R e) N z1) (HOMR M3 N) 
(sup_sharp R M2 M3 N g) (HOMR M2 N) (sup_sharp R M1 M2 N f) (HOMR M1 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 = kerM2,M3 g",
        thin_tac "g ∈ aHom M2 M3 ∧ kerM3,Zm R e z1 = 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 = "λxa∈carrier M2. x (g xa)" and g = "λx∈carrier M2. \<zero>N"
        and x = xa in eq_fun_eq_val, 
        thin_tac "(λxa∈carrier M2. x (g xa)) = (λx∈carrier M2. \<zero>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 (λz∈carrier 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 (HOMR (Zm R e) N)(sup_sharp R M3 (Zm R e) N z1)
      (HOMR M3 N) (sup_sharp R M2 M3 N g) (HOMR M2 N) (sup_sharp R M1 M2 N f)
      (HOMR M1 N); R module (L::('a, 'r, 'm) Module_scheme)|] ==> 
  exact4 R (HOMR (Zm R e) L) (sup_sharp R M3 (Zm R e) L z1)
 (HOMR M3 L) (sup_sharp R M2 M3 L g) (HOMR M2 L) (sup_sharp R M1 M2 L f)
 (HOMR M1 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 "reset show_types"
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 (HOMR N (Zm R e)) (sub_sharp R N (Zm R e) M1 z)
    (HOMR N M1) (sub_sharp R N M1 M2 f) (HOMR N M2) (sub_sharp R N M2 M3 g)
    (HOMR N 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) = kerM1,M2 f")
 apply (erule conjE, thin_tac "f ` carrier M1 = kerM2,M3 g")
 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) = kerM1,M2 f ∧
                                          f ` carrier M1 = kerM2,M3 g")
 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} = kerM1,M2 f", 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 = (λx∈carrier N. f (xa x))")
 apply (thin_tac "(λx∈carrier N. f (xa x)) ∈ mHom R N M2")
 apply (thin_tac "(λx∈carrier N. g (if x ∈ carrier N then f (xa x) else arbitrary)) ∈ 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) = kerM1,M2 f ∧
                                     f ` carrier M1 = kerM2,M3 g")
 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 = kerM2,M3 g")
 apply simp apply (thin_tac "kerM2,M3 g = 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 (λn∈carrier 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 "


constdefs *)

section "9. Tensor product"

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

constdefs
 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"
 "bilinear_map f R M1 M2 N == f ∈ M1 ×c M2 -> carrier N ∧ 
                             f ∈ extensional (M1 ×c M2) ∧ 
   (∀x1 ∈ carrier M1. ∀x2 ∈ carrier M1. 
         ∀y∈carrier M2.(f (x1 ±M1 x2, y) = f (x1, y) ±N (f (x2, y)))) ∧ 
   (∀x∈carrier M1. ∀y1∈carrier M2. 
         ∀y2∈carrier M2. f (x, y1 ±M2 y2) = f (x, y1) ±N (f (x, y2))) ∧ 
   (∀x∈carrier M1. ∀y∈carrier M2. 
         ∀r∈carrier R. f (r ·sM1 x, y) = r ·sN (f (x, y)) ∧ 
                       f (x, r ·sM2 y) = 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 ±M1 m12, 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 ±M1 m12, m2) ±N -aN (f (m11, m2) ±N (f (m12, m2))) = \<zero>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 ±M2 m22) = 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 ±M2 m22) ±N -aN (f (m, m21) ±N (f (m, m22))) = \<zero>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 ·sM1 m1, 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 ·sM1 m1, m2) ±N -aN (r ·sN (f (m1, m2))) = \<zero>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 ·sM2 m2) = 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 ·sM2 m2)  ±N -aN (r ·sN (f (m1, m2))) = \<zero>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 (\<zero>M1, m2) = \<zero>N"
apply (frule Module.module_inc_zero [of M1 R])
apply (frule bilinear_l_add [of M1 M2 N "\<zero>M1" "\<zero>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 "\<zero>M1" m2 f], assumption+)
 apply (frule Module.module_is_ag [of N R])
 apply (frule aGroup.ag_eq_sol1 [of "N" "f (\<zero>M1, m2)" "f (\<zero>M1, m2)"
        "f (\<zero>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, \<zero>M2) = \<zero>N"
apply (frule Module.module_inc_zero [of M2 R])
apply (frule bilinear_r_add [of M1 M2 N m1 "\<zero>M2" "\<zero>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 "\<zero>M2" "f"], assumption+)
 apply (frule Module.module_is_ag [of N R])
 apply (frule aGroup.ag_eq_sol1 [of N "f (m1, \<zero>M2)" "f (m1, \<zero>M2)" 
       "f (m1, \<zero>M2)"], assumption+)
 apply (rule sym, assumption+)
apply (simp add:aGroup.ag_l_inv1)
done

constdefs 
 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"

 "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 ≅R MN1"
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" _ "carrier MN1" _ "carrier MN"], assumption+,
        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" _ "carrier MN" _ "carrier MN1"], assumption+,
        simp)
 apply (subgoal_tac "compose (M ×c N) (mIdMN) f = f")
 prefer 2 
 apply (frule Module.mId_mHom [of MN R],
        frule_tac f = "mIdMN" in Ring.mHom_func[of R _ MN MN], assumption,
        frule  composition [of f "M ×c N" "carrier MN" "mIdMN" "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 (rule ballI)
 apply (simp add:compose_def mId_def,
        simp add:funcset_mem) 
 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) = mIdMN")
 apply (subgoal_tac "(compose (carrier MN1) h k) = (mIdMN1)") 
 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) (mIdMN1) g = g")
 prefer 2 
 apply (frule Module.mId_mHom [of MN1 R])
 apply (subgoal_tac "mIdMN1  ∈ carrier MN1 -> carrier MN1")
 prefer 2 apply (simp add:mHom_def aHom_def)
apply (frule  composition [of "g" "M ×c N" "carrier MN1" "mIdMN1" 
      "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 (rule ballI,
       simp add:compose_def mId_def,
       simp add:funcset_mem,
       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 "mIdMN1 = 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 = mIdMN",
         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 = mIdMN",
        thin_tac "compose (M ×c N) (mIdMN1 ) 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 "mIdMN1  ∈ mHom R MN1 MN1",
        thin_tac "compose (M ×c N) (mIdMN1 ) g = g",
        thin_tac "ka ∈ mHom R MN1 MN1 ∧ compose (M ×c N) ka g = g",
        thin_tac "mIdMN1  = 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) (mIdMN) f = f")
 prefer 2
 apply (frule_tac f = "mIdMN" in Ring.mHom_func[of R _ MN MN], assumption)
apply (frule  composition [of "f" "M ×c N" "carrier MN" "mIdMN" "carrier MN"],
        assumption+)
apply (frule tensor_prod_uniqueTr [of "R" "M" "N" "MN" "MN" "f" "f"], 
       assumption+)
 apply (erule ex1E)
 apply (subgoal_tac "mIdMN = 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 "mIdMN  = ka")
 apply simp
 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) (mIdMN) 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 (simp add:compos_def)
done

chapter "6. Construction of an abelian group"

section "1. free generated abelian group I, direct sum and direct product 2"

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

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

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

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

syntax
 "@BOP"::"['a, 'a => 'a => 'a, 'a] => 'a"
     ("(3_/ _+/ _)" [62,62,63]62)

 "@SOP"::"['r, 'r => 'a => 'a, 'a] => 'a"
     ("(3_/ _· _)" [68,68,69]68)

translations
 "a f+ b" == "bpp f a b"
 "r s· a" == "sop s r a"

constdefs
 minus_set :: "['a => 'a, 'a set] => 'a set"
 "minus_set i A == {x. ∃y∈A. x = i- y}"

 pm_set :: "['a => 'a, 'a set] => 'a set"
 "pm_set i A == A ∪ (minus_set i A)"

 s_set :: "[('r, 'm) Ring_scheme, 'r => 'a => 'a, 'a set] => 'a set"
 "s_set R s A == {x. ∃r∈carrier R. ∃a∈A. x = r s· a} ∪ A"

consts
 add_set :: "['a => 'a => 'a, 'a set] => nat => 'a set"

primrec
 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}"

constdefs
 aug_pm_set::"['a, 'a => 'a, 'a set] => 'a set"
    "aug_pm_set z i A == {z} ∪ A ∪ (minus_set i A)"

constdefs
 addition_set::"['a => 'a => 'a, 'a set] => 'a set"
 "addition_set f A == \<Union> {add_set f A n | n. (0::nat)≤ n}"

constdefs
 assoc_bpp :: "['a set, 'a => 'a => 'a] => bool"
 "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)"

 commute_bpp::"['a => 'a => 'a, 'a set] => bool" 
"commute_bpp f A == ∀x∈addition_set f A. ∀y∈addition_set f A. x f+ y = y f+ x"

constdefs
 zeroA :: "['a, 'a => 'a, 'a => 'a => 'a, 'a set] => 'a => bool"
 "zeroA z i f A z1 == ∀x ∈ addition_set f (aug_pm_set z i A). z1 f+ x = x"

constdefs
 inv_ipp :: "['a, 'a => 'a, 'a => 'a => 'a, 'a set] => bool" 
 "inv_ipp z i f A  == ∀a∈addition_set f (aug_pm_set z i A). zeroA z i f A ((i- a) f+ a)"

 ipp_cond1::"['a set, 'a => 'a] => bool"
 "ipp_cond1 A i == ∀x∈A. i- (i- x) = x"

 ipp_cond2::"['a, 'a set, 'a => 'a,  'a => 'a => 'a] => bool"
 "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)"

 ipp_cond3::"['a, 'a => 'a] => bool"
 "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 a = x in forall_spec1,
        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 b = s in forball_spec1, assumption,
        drule_tac b = t in forball_spec1, 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 b = z1 in forball_spec1, 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|] ==> 
     ∀a∈addition_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 b = a in forball_spec1, 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

constdefs
 fag_gen_by ::"['a set, 'a => 'a => 'a, 'a => 'a, 'a] => 'a aGroup"
 "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 (rule bivar_func_test)
 apply (rule ballI)+
 apply (simp add:fag_gen_by_def)
 apply (simp add: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 (rule univar_func_test, rule ballI)
 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 "2. Abelian group generated by a singleton (constructive)" 
 
 constdefs
 fag_single :: "['a, 'a => 'a => 'a, 'a => 'a, 'a] => 'a aGroup"
 "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. (∃sa∈add_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 b = a in forball_spec1, 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 a = s in forall_spec1,
        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. x∈add_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. x∈add_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 a = sa in forall_spec1, 
        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 b = a in forball_spec1, 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 "∃s∈add_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 "∀s∈add_set f {a} (n - m). (THE x. x∈add_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 "∃s∈add_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 "∃s∈add_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


constdefs
 free_gen_condition::"['a => 'a => 'a, 'a => 'a, 'a, 'a] => bool"
    "free_gen_condition f i a z == ∀n. z ∉ add_set f {a} n"

constdefs
 fg_elem_single::"['a => 'a => 'a, 'a => 'a, 'a, 'a] => int => 'a"
 "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)))))"

syntax 
 "@FGELEMSNGLE"::"[int, 'a, 'a => 'a => 'a, 'a => 'a, 'a] => 'a"
             ("(5_\<Odot>__,_,_)" [99,98,98,98,98]99)
translations
  "n\<Odot>af,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|] ==> 
      (n\<Odot>af,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)|] ==> 
     (n\<Odot>af,i,z) f+ (m\<Odot>af,i,z) = (n + m)\<Odot>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 |] ==> 
           (n\<Odot>af,i,z) f+ (m\<Odot>af,i,z) = (n + m)\<Odot>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|] ==> 0\<Odot>af,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|] ==> (n\<Odot>af,i,z) f+ (m\<Odot>af,i,z) = (n + m)\<Odot>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)\<Odot>af,i,z" "m\<Odot>af,i,z"])
 apply (subst addition_assoc[THEN sym], assumption+)
 apply (subgoal_tac "((- m)\<Odot>af,i,z) f+ (m\<Odot>af,i,z) = z") apply simp
 apply (simp add:zeroA_def)  apply (simp add:zadd_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 "(n\<Odot>af,i,z) f+ ((- n)\<Odot>af,i,z) = z") apply (simp add:zeroA_def)
 apply (thin_tac "(- n)\<Odot>af,i,z ∈ addition_set f (aug_pm_set z i {a})")
 apply (thin_tac "(n + m)\<Odot>af,i,z ∈ addition_set f (aug_pm_set z i {a})")
 apply (thin_tac "n\<Odot>af,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|] ==> (n\<Odot>af,i,z) f+ (m\<Odot>af,i,z) = (n + m)\<Odot>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 "(n\<Odot>af,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 " (0\<Odot>af,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 "m\<Odot>af,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:zadd_commute)
apply simp
 apply (simp add:single_addition_neg, simp add:zadd_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- (m\<Odot>af,i,z) = (-m)\<Odot>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- (m\<Odot>af,i,z)) =
            ((- m)\<Odot>af,i,z) f+ (m\<Odot>af,i,z) f+ (i- (m\<Odot>af,i,z))") prefer 2 apply simp
 apply (frule aug_ipp_closed [of "f" "z" "i" "{a}" "m\<Odot>af,i,z"], assumption+)
 apply (thin_tac "z = ((- m)\<Odot>af,i,z) f+ (m\<Odot>af,i,z)")
 apply (simp add:addition_assoc)
 apply (simp add:aug_commute [of "f" "z" "i" "{a}" "(m\<Odot>af,i,z)" "i- (m\<Odot>af,i,z)"])
 apply (subgoal_tac "i- (m\<Odot>af,i,z) f+ (m\<Odot>af,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)\<Odot>af,i,z" "z"])
 apply (simp add:zeroA_def)
 apply (simp add:inv_ipp_def)
 apply (subgoal_tac "zeroA z i f {a} (i- (m\<Odot>af,i,z) f+ (m\<Odot>af,i,z))")
 prefer 2  apply simp
 apply (frule aug_zero_unique[of "f" "z" "i" "{a}" "i- (m\<Odot>af,i,z) f+ (m\<Odot>af,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|] ==> 
      (n\<Odot>af,i,z) ≠ (m\<Odot>af,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}" "(m\<Odot>af,i,z)" "(- m)\<Odot>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 "n\<Odot>af,i,z = m\<Odot>af,i,z")
 apply (thin_tac " i- (m\<Odot>af,i,z) ∈ addition_set f (aug_pm_set z i {a})")
 apply (thin_tac " m\<Odot>af,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)\<Odot>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

constdefs
 fags_cond::"['a => 'a => 'a, 'a, 'a => 'a, 'a] => bool"
 "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|] ==> (n\<Odot>af,i,z) ≠ (m\<Odot>af,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;(n\<Odot>af,i,z) = (m\<Odot>af,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

constdefs
 fags_carr::"['a => 'a => 'a, 'a, 'a => 'a, 'a] => 'a set"
 "fags_carr f z i a == {x. ∃n. x = n\<Odot>af,i,z}" 

constdefs
 fags_bpp::"['a => 'a => 'a, 'a, 'a => 'a, 'a] => 'a => 'a => 'a"
 "fags_bpp f z i a == λx∈(fags_carr f z i a). λy∈(fags_carr f z i a).
        ((THE n. x = n\<Odot>af,i,z) + (THE m. y = m\<Odot>af,i,z))\<Odot>af,i,z"

constdefs
 fags_ipp::"['a => 'a => 'a, 'a, 'a => 'a, 'a] => 'a => 'a"
 "fags_ipp f z i a == λx∈(fags_carr f z i a). 
                                   (- (THE n. x = n\<Odot>af,i,z))\<Odot>af,i,z"

lemma fags_mem:"fags_cond f z i a ==> (n\<Odot>af,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 (n\<Odot>af,i,z) = (- n)\<Odot>af,i,z"
apply (subgoal_tac "(n\<Odot>af,i,z) ∈  fags_carr f z i a") 
apply (simp add:fags_ipp_def)
 apply (subgoal_tac "n\<Odot>af,i,z = (THE na. n\<Odot>af,i,z = na\<Odot>af,i,z)\<Odot>af,i,z")
 apply (frule_tac m = "THE na. n\<Odot>af,i,z = na\<Odot>af,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 (n\<Odot>af,i,z) (m\<Odot>af,i,z) = (n + m)\<Odot>af,i,z"
apply (subgoal_tac "(n\<Odot>af,i,z) ∈  fags_carr f z i a") 
apply (subgoal_tac "(m\<Odot>af,i,z) ∈  fags_carr f z i a")
apply (simp add:fags_bpp_def)
 apply (subgoal_tac "n\<Odot>af,i,z = (THE na. n\<Odot>af,i,z = na\<Odot>af,i,z)\<Odot>af,i,z")
 apply (frule_tac m = "THE na. n\<Odot>af,i,z = na\<Odot>af,i,z" in 
                  fag_single_free1 [of "f" "z" "i" "a" "n"], assumption+)
 apply (subgoal_tac "m\<Odot>af,i,z = (THE ma. m\<Odot>af,i,z = ma\<Odot>af,i,z)\<Odot>af,i,z")
 apply (frule_tac m = "THE ma. m\<Odot>af,i,z = ma\<Odot>af,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 

constdefs
 fags :: "['a => 'a => 'a, 'a, 'a => 'a, 'a] => 'a aGroup"
 "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 bivar_func_test)
 apply (rule ballI)+
 apply (simp add:fags_def)
 apply (simp add: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:zadd_assoc)

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

 apply (rule univar_func_test, rule ballI)
 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 (0\<Odot>af,i,z) aa",
        simp add:fags_bppTr,
        thin_tac "aa = n\<Odot>af,i,z")
 apply (cut_tac single_addition_zero[of f z i a], simp)
  apply (simp add:fags_cond_def)+
done

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

constdefs
 ag_single_gen::"[('a, 'm) aGroup_scheme, 'a] => bool" 
  "ag_single_gen A a == aGroup A ∧ carrier A = \<Inter> {H. asubGroup A H ∧ a ∈ H}"

(* syntax
   "@AGSGEN"::"['a AgroupType, 'a] => bool" 
    ("(__)" [90,91]90)
translations
  "Ga" == "ag_single_gen A a" *)

consts
 aSum :: "[('a, 'm) aGroup_scheme, nat,'a]  => 'a"
 
primrec
 aSum_0   :"aSum A 0 a = \<zero>A"
 aSum_Suc :"aSum A (Suc n) a = aSum A n a ±A a"

constdefs
 sprod_n_a ::"[('a, 'm) aGroup_scheme, int, 'a]  => 'a"
 "sprod_n_a A n x ==  if 0 ≤ n then (aSum A (nat n) x) 
                          else (aSum A (nat (- n)) (-aA x))"

syntax
 "@SPRODNA" :: "[int, 'a, ('a, 'm) aGroup_scheme] => 'a"
                  ("(3_\<triangleright>__)" [95,95,96]95)
translations
  "n\<triangleright>aA" == "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 ==> n\<triangleright>aA ∈ 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 ==> 0\<triangleright>aA = \<zero>"
apply (simp add:sprod_n_a_def)
done

lemma (in aGroup) nt_1:"a ∈ carrier A ==> 1\<triangleright>aA = 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 \<zero> = \<zero>"
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)\<triangleright>aA = n\<triangleright>aA ± (m\<triangleright>aA)"
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)\<triangleright>aA = n\<triangleright>aA ± (m\<triangleright>aA)"
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 ==> 
                \<zero>  = 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 "\<zero> =  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|] ==>
                                    \<zero> = n\<triangleright>aA ± ((-n)\<triangleright>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|] ==>
                                    \<zero> = n\<triangleright>aA ± ((-n)\<triangleright>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)\<triangleright>aA = n\<triangleright>aA ± (m\<triangleright>aA)"
apply (cut_tac zless_linear[of n "- m"])
apply (case_tac "n = -m") 
 apply simp
 apply (subst ag_pOp_commute)
 apply (simp add:nt_mem0)+
 apply (subgoal_tac "0\<triangleright>aA = \<zero>") 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)\<triangleright>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 "n\<triangleright>aA =  (- m)\<triangleright>aA ± (n + m)\<triangleright>aA")
 apply (subst ag_pOp_commute[of "(- m)\<triangleright>aA" "(n + m)\<triangleright>aA"])
 apply (simp add:nt_mem0)+
 apply (subst ag_pOp_assoc)
 apply (simp add:nt_mem0)+
 apply (subst ag_pOp_commute [of "(- m)\<triangleright>aA" "m\<triangleright>aA"])
 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)\<triangleright>aA = n\<triangleright>aA ± (m\<triangleright>aA)"
apply (simp add:zadd_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)\<triangleright>aA = n\<triangleright>aA ± (m\<triangleright>aA)"
apply (cut_tac zless_linear[of n 0])
 apply (case_tac "n < 0")
  apply (cut_tac zless_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 zless_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 (n\<triangleright>aA) = (- n)\<triangleright>aA"
apply (subgoal_tac "(n + -n)\<triangleright>aA = n\<triangleright>aA ± ((-n)\<triangleright>aA)")
 prefer 2 apply (rule nt_sum0, assumption+) apply (simp add:nt_zero0)
 apply (subgoal_tac "-a (n\<triangleright>aA) ± \<zero> = -a (n\<triangleright>aA) ± (n\<triangleright>aA ± (- n)\<triangleright>aA)")
 apply (subgoal_tac "n\<triangleright>aA ∈ carrier A") 
 apply (frule ag_mOp_closed [of "n\<triangleright>aA"])
 apply (thin_tac "\<zero> =  n\<triangleright>aA ± (- n)\<triangleright>aA")
 apply (simp add:ag_r_zero)
 apply (subgoal_tac "(- n)\<triangleright>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\<triangleright>(n\<triangleright>aA)A = (m * n)\<triangleright>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:zmult_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\<triangleright>(n\<triangleright>aA)A = (m * n)\<triangleright>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\<triangleright>(n\<triangleright>aA)A = (m * n)\<triangleright>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 zmult_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\<triangleright>(n\<triangleright>aA)A = (m * n)\<triangleright>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 ==> m\<triangleright>n\<triangleright>aAA = (m * n)\<triangleright>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 zless_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 = (n\<triangleright>aA)}"
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 = (n\<triangleright>aA)}" 
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 = n\<triangleright>aA}")
 apply (subgoal_tac "\<Inter>{H. A +> H ∧ a ∈ H} ⊆ {x. ∃n. x = n\<triangleright>aA}")
 apply (frule_tac A = "\<Inter>{H. A +> H ∧ a ∈ H}" and B = "{x. ∃n. x = n\<triangleright>aA}" and
         c = x in  subsetD) 
 apply (frule sym, thin_tac "carrier A = \<Inter>{H. A +> H ∧ a ∈ H}") 
 apply (simp, simp)
 apply (thin_tac "carrier A = \<Inter>{H. A +> H ∧ a ∈ H}")
 apply (rule subsetI, blast)
apply (thin_tac "carrier A = \<Inter>{H. A +> H ∧ a ∈ H}",
       thin_tac "A +> {x. ∃n. x = n\<triangleright>aA}")
 apply (subgoal_tac "a = 1\<triangleright>aA", 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 = \<Inter>{H. A +> H ∧ a ∈ H}")
 apply (rule subsetI, simp)
 apply (erule exE)
 apply (simp, simp add:nt_mem0)
done 

constdefs
 single_gen_free::"[('a, 'm) aGroup_scheme, 'a] => bool"
 "single_gen_free A a == ∀n. n ≠ 0 --> \<zero>A ≠ n\<triangleright>aA"

constdefs
 sfg ::"[('a, 'm) aGroup_scheme, 'a] => bool"
 "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; n\<triangleright>aA = \<zero>|] ==> 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 = n\<triangleright>aA"
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 ==> n\<triangleright>aA ∈ 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 ==> 0\<triangleright>aA = \<zero>"
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)\<triangleright>aA = n\<triangleright>aA ± (m\<triangleright>aA)"
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(n\<triangleright>aA) = (- n)\<triangleright>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 ==> m\<triangleright>n\<triangleright>aAA = (m * n)\<triangleright>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 |] ==> n\<triangleright>aA ≠ (m\<triangleright>aA)"
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 "n\<triangleright>aA ± (-a (m\<triangleright>aA)) = \<zero>")
 apply (thin_tac "n\<triangleright>aA = m\<triangleright>aA")  (*  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; n\<triangleright>aA = (m\<triangleright>aA) |] ==> n = m"
apply (rule contrapos_pp, simp+)
apply (simp add:sfg_free)
done

section "3. Free Generated Modules (constructive)"

constdefs
 sop_one::"[('r, 'm) Ring_scheme, 'r => 'a => 'a, 'a set] => bool"
 "sop_one R s A == ∀x∈A. (1rR) s· x = x"

constdefs
 sop_assoc::"[('r, 'm) Ring_scheme, 'r => 'a => 'a, 'a set] => bool"
 "sop_assoc R s A == ∀a∈carrier R. ∀b∈carrier R. ∀x∈A.
                         (a ·rR b) s· x = a s· (b s· x)"
constdefs
 sop_inv ::"[('r, 'm) Ring_scheme, 'r => 'a => 'a, 'a => 'a, 'a set] 
      => bool"
 "sop_inv R s i A == ∀r∈carrier R. ∀x∈A. r s· (i- x) = (-aR r) s· x"

 sop_distr1 :: "[('r, 'm) Ring_scheme, 'r => 'a => 'a, 'a => 'a => 'a,
 'a => 'a, 'a set, 'a] => bool"

 "sop_distr1 R s f i A z == ∀a∈carrier R. ∀b∈carrier R. 
          ∀x∈(aug_pm_set z i A). (a ±R b) s· x = (a s· x) f+ (b s· x)"

 sop_distr2 :: "[('r, 'm) Ring_scheme, 'r => 'a => 'a, 'a => 'a => 'a, 
                'a => 'a, 'a set, 'a] => bool"

 "sop_distr2 R s f i A z == ∀a∈carrier R. 
         ∀x∈addition_set f (aug_pm_set z i A). 
           ∀y∈addition_set f (aug_pm_set z i A). 
                 a s· (x f+ y) = (a s· x) f+ (a s· y)"

 sop_z :: "[('r, 'm) Ring_scheme, 'r => 'a => 'a, 'a] => bool"
           "sop_z R s z == ∀r∈carrier R. r s· z = z"

constdefs
 fgmodule :: "[('r, 'm) Ring_scheme, 'a set, 'a, 'a => 'a, 'a => 'a => 'a, 
      'r => 'a => 'a] => ('a, 'r) Module"

 "fgmodule R A z i f s == 
     (|carrier = addition_set f (aug_pm_set z i (s_set R s A)), 
       pop = λ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, 
       mop = λx∈addition_set f (aug_pm_set z i (s_set R s A)). i- x, 
       zero = z, 
       sprod = λr∈carrier R. 
                 λx∈addition_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 b = r in forball_spec1, assumption,
        drule_tac b = ra in forball_spec1, assumption,
        frule_tac b = a in forball_spec1,
        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 b = "-a r" in forball_spec1, assumption,
        thin_tac "∀r∈carrier R. ∀x∈s_set R s A. r s· (i- x) = (-a r) s· x",
        drule_tac b = ra in forball_spec1, assumption,
        drule_tac b = a in forball_spec1,
                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|] ==>
    ∀r∈carrier R. ∀x∈add_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|] ==>
     ∀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 (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 b = r in forball_spec1, assumption,
        drule_tac b = x in forball_spec1, 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))|]  ==> 
   ∀x∈add_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 "∀x∈add_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))|]  ==> 
   ∀x∈addition_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|]  ==>
     ∀a∈carrier R. ∀b∈carrier R. 
      ∀x∈add_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 b = a in forball_spec1, assumption,
        drule_tac b = b in forball_spec1, assumption,
        drule_tac b = sa in forball_spec1, 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 b = b in forball_spec1, assumption,
        rotate_tac -1,
        frule_tac b = sa in forball_spec1, assumption,
        drule_tac b = t in forball_spec1, assumption)
  apply (simp, 
        thin_tac "∀a∈carrier R.
           ∀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)).
                 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))|] ==> 
   ∀a∈carrier R. ∀b∈carrier R. 
      ∀x∈addition_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|]  ==> 
 ∀a∈carrier R. ∀b∈carrier 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 b = a in forball_spec1, assumption,
        rotate_tac -1,
        frule_tac b = sa in forball_spec1, assumption,
        drule_tac b = t in forball_spec1, assumption)
  apply (frule_tac b = b in forball_spec1, assumption,
        rotate_tac -1,
        frule_tac b = sa in forball_spec1, assumption,
        drule_tac b = t in forball_spec1, assumption, 
        thin_tac "∀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 (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|]  ==> 
      ∀a∈carrier R. ∀b∈carrier 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

constdefs
 fgmodule_condition ::"[('r, 'm) Ring_scheme, 'a => 'a => 'a, 'a => 'a,
         'r => 'a => 'a, 'a set, 'a] => bool"
 "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 assumption

 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 "4. a fgmodule and a free module"

lemma (in Ring) fg_zeroTr:"[|fgmodule_condition R f i s A z; a ∈ A|] ==> 
                     \<zero>  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)}. \<zero>) ∈ {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)}. \<zero>) (λ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 (rule univar_func_test, rule ballI, simp add:ring_zero) 
  apply (rule univar_func_test, rule ballI, 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 (rule univar_func_test, rule ballI, simp add:ring_one)
  apply (rule univar_func_test, rule ballI, simp)

apply (case_tac "∃r∈carrier R. ∃a∈A. 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 (rule univar_func_test, rule ballI, simp)
  apply (rule univar_func_test, rule ballI, simp)

apply (simp add:minus_set_def,
      thin_tac "x ≠ z", thin_tac "x ∉ A", 
      thin_tac "∀r∈carrier R. ∀a∈A. 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 (rule univar_func_test, rule ballI, simp)
  apply (rule univar_func_test, rule ballI, 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 univar_func_test, rule ballI, simp,
         cut_tac ring_one, cut_tac ring_is_ag,
         simp add:aGroup.ag_mOp_closed[of R])
    apply (rule univar_func_test, rule ballI, 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 a = sa in forall_spec1)
 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); ∀x∈A. 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 (frule_tac f = fa and A = "{0}" and B = A in funcset_mem, simp)
 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 a = fa in forall_spec1,
        drule_tac a = sa in forall_spec1, 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 univar_func_test, rule ballI, 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); ∀x∈A. 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 "(λx∈A. x) ∈ A -> carrier (fgmodule R A z i f s)") 
 prefer 2 
   apply (rule univar_func_test, rule ballI, 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 
         "λx∈A. x"], assumption+)
 apply (erule bexE)
 apply (thin_tac "(λx∈A. x) ∈ A -> carrier (fgmodule R A z i f s)")
 apply (simp add:misomorphic_def)
 apply (subgoal_tac "bijecM,(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 n = n in finite_Nset,
         cut_tac n = "card (fa ` {j. j ≤ n}) - Suc 0" in finite_Nset,
         cut_tac F = "{j. j ≤ n}" and h = fa in finite_imageI,
         assumption) thm mem_in_image
  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+,
        rule univar_func_test, rule ballI, 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 "5. direct sum, again"

constdefs
 miota :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
 ('a, 'r, 'm1) Module_scheme] => 'a => 'a"
 "miota R M1 M == λx∈carrier M1. x"

 msubmodule ::"[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
 ('a, 'r, 'm1) Module_scheme] => bool"
 "msubmodule R M M1 == miota R M1 M ∈ mHom R M1 M ∧ 
                         (carrier M1) ⊆ (carrier M)"  
     (** M and M1 are R modules. **) 

constdefs
 ds2 :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme, 
          ('a, 'r, 'm1) Module_scheme, ('a, 'r, 'm1) Module_scheme] => bool"

  "ds2 R M M1 M2 == R module M ∧ msubmodule R M M1 ∧ msubmodule R M M2 ∧ 
          (∀x∈carrier M. ∃m1∈carrier M1. ∃m2∈carrier M2. x = m1 ±M m2) ∧  
           (carrier M1) ∩ (carrier M2) = {\<zero>M}"

syntax
"@DS2"::"[('a, 'r, 'm1) Module_scheme, ('r, 'm) Ring_scheme, 
          ('a, 'r, 'm1) Module_scheme, ('a, 'r, 'm1) Module_scheme] => bool"
  ("(4_/ \<Oplus>_,_ _)" [92,93,92,92]92)
 translations
 "M1 \<Oplus>R,M M2" == "ds2 R M M1 M2"

lemma (in Ring) ds2_commute:"[|R module M1; R module M2; R module M; 
               M1 \<Oplus>R,M M2|] ==> M2 \<Oplus>R,M M1"
apply (simp add:ds2_def)
 apply (erule conjE)+
 apply (subst Int_commute, simp) 
 apply (rule ballI, 
       drule_tac b = x in forball_spec1, 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 ±M1 y = x ±M y"
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|] ==> -aM1 x  = -aM x"
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 ·sM1 x = a ·sM x" 
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 = "-aM1 b" in Module.mHom_add[of M1 R M 
        "miota R M1 M"], assumption+,
        frule_tac x = a and y = "-aM1 b" 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 ±M m2 = m1' ±M m2'|] ==> 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 ±M m2" and b = "m1' ±M m2'" and c = "-aM m2" in 
        aGroup.ag_pOp_add_r, assumption+,
        thin_tac "m1 ±M m2 = m1' ±M m2'",
        simp add:aGroup.ag_pOp_assoc[of M m1 m2 "-aM m2"]
                                     aGroup.ag_r_inv1 aGroup.ag_r_zero,
        simp add:aGroup.ag_pOp_assoc[of M m1' m2' "-aM m2"],
        frule_tac x = m2' and y = "-aM m2" in aGroup.ag_pOp_closed,
               assumption+,
        frule_tac x = m1' in aGroup.ag_mOp_closed, assumption+,
        frule_tac x = m1' and y = "m2' ±M -aM m2" in 
                           aGroup.ag_pOp_closed, assumption+,
        frule_tac a = m1 and b = "m1' ±M (m2' ±M -aM m2)" and 
        c = "-aM m1'" in aGroup.ag_pOp_add_l[of M], simp, assumption+)
 apply (frule aGroup.ag_pOp_assoc[THEN sym, of M "-aM m1'" "m1'"
                                                      "(m2' ±M -aM m2)"],
        assumption+) 
apply (rotate_tac -6, frule sym, thin_tac "m1 = m1' ±M (m2' ±M -aM m2)",
       thin_tac "-aM m1' ±M m1 = -aM m1' ±M (m1' ±M (m2' ±M -aM m2))",
       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" "-aM m1'" 
                                                m1], assumption+,
        frule Module.submodule_pOp_closed [of M R "carrier M2" m2' "-aM m2"],
                                                     assumption+,
     rule Module.submodule_mOp_closed [of M R "carrier M2" m2], assumption+) 
 apply (subgoal_tac "(-aM m1' ±M m1) ∈ carrier M1 ∩ carrier M2")
 prefer 2 apply simp 
 apply (subgoal_tac "carrier M1 ∩ carrier M2 = {\<zero>M}")
 apply (frule sym, thin_tac "-aM m1' ±M m1 = m2' ±M -aM m2")
 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 ∧ injecM1,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

constdefs
 mproj1 :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme,
    ('a, 'r, 'm1) Module_scheme, ('a, 'r, 'm1) Module_scheme] => 'a => 'a"
 "mproj1 R M1 M2 M ==λx∈carrier M. THE x1. x1 ∈ carrier M1 ∧
                                          (x ±M (-aM x1)) ∈ carrier M2"
  
 mproj2 :: "[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme,
    ('a, 'r, 'm1) Module_scheme, ('a, 'r, 'm1) Module_scheme] => 'a => 'a"
 "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 \<Oplus>R,M M2; a ∈ carrier M|] ==> 
         ∃a1∈carrier M1. ∃a2∈carrier M2. a = a1 ±M a2"
by (simp add:ds2_def)

lemma (in Ring) ds2_components1:"[|R module M1; R module M2; R module M;
       M1 \<Oplus>R,M M2; a ∈ carrier M|] ==> 
         ∃a1∈carrier M1. a ±M -aM a1 ∈ 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 ±M a2" and b = "a2 ±M a1" and c = "-aM a1" in 
        aGroup.ag_pOp_add_r[of M], assumption+,
        frule_tac x = a2 and y = a1 and z = "-aM a1" in 
                                   aGroup.ag_pOp_assoc[of M], assumption+,
        simp add:aGroup.ag_r_inv1 aGroup.ag_r_zero)
 apply (thin_tac "a2 ±M a1 ∈ carrier M",
        thin_tac "a = a2 ±M a1",
        thin_tac "a2 ∈ carrier M",
        thin_tac "a1 ±M a2 = a2 ±M a1",
        frule sym, thin_tac "a2 ±M a1 ±M -aM a1 = a2")
 apply (frule_tac a = a2 and b = "a2 ±M a1 ±M -aM a1" 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 -aM x1) ∈ 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 -aM x1" and 
         ?m2' = "x ±M -aM y" 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 (-aM x1)) ∈ carrier M2; 
      y1 ∈ carrier M1;(x ±M (-aM y1)) ∈ 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 (-aM a1)) ∈ carrier M2|] ==>
      (THE x1. x1 ∈ carrier M1 ∧ a ±M -aM x1 ∈ carrier M2) = a1"
apply (subgoal_tac "(THE x1. x1 ∈ carrier M1 ∧ a ±M -aM x1 ∈ carrier M2) ∈ 
         carrier M1 ∧  a ±M -aM (THE x1. x1 ∈ carrier M1 ∧  
         a ±M -aM x1 ∈ carrier M2) ∈ carrier M2", 
       rule mprojTr2[of M1 M2 M a 
       "THE x1. x1 ∈ carrier M1 ∧ a ±M -aM x1 ∈ 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 univar_func_test, rule ballI)
 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 ±M b" in ds2_components1[of M1 M2 M], assumption+,
        (erule bexE)+, rename_tac b1 ab,
        frule_tac a = "a ±M b" 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 ±M b ±M -aM x1 ∈ carrier M2) = ab",
        thin_tac "(THE x1. x1 ∈ carrier M1 ∧ a ±M -aM x1 ∈ carrier M2) = a1",
        thin_tac "(THE x1. x1 ∈ carrier M1 ∧ b ±M -aM x1 ∈ carrier M2) = b1")
 apply (frule Module.module_is_ag[of M2],
        frule_tac x = "a ±M -aM a1" and y = "b ±M -aM b1" in 
                       aGroup.ag_pOp_closed[of M2], assumption+,
        frule_tac x = "a ±M -aM a1" and y = "b ±M -aM b1" in 
                     msub_addition[of M M2], assumption+,
        simp add:ds2_def, assumption+, simp,
          thin_tac "a ±M -aM a1 ±M2 (b ±M -aM b1) = 
                                 a ±M -aM a1 ±M (b ±M -aM b1)",
        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 = "-aM a1" and c = b and d = "-aM b1" in 
                   aGroup.pOp_assocTr43[of M], assumption+, simp,
         thin_tac "a ±M -aM a1 ±M (b ±M -aM b1) =
                                      a ±M (-aM a1 ±M b) ±M -aM b1",
         frule_tac x = "-aM a1" and y = b in aGroup.ag_pOp_commute, 
         assumption+, simp,
         frule_tac a1 = a and b1 = b and c1 = "-aM a1" and d1 = "-aM b1" in 
         aGroup.pOp_assocTr43[THEN sym], assumption+, simp,
         thin_tac "a ±M (b ±M -aM a1) ±M -aM b1 = 
                                        a ±M b ±M (-aM a1 ±M -aM b1)",
         frule_tac x1 = a1 and y1 = b1 in aGroup.ag_p_inv[THEN sym, of M],
          assumption+, simp,
         thin_tac "-aM a1 ±M -aM b1 = -aM (a1 ±M b1)")
  apply (rule_tac x = "a ±M b" and ?x1.0 = ab and ?y1.0 = "a1 ±M1 b1" 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 -aM a1" 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 = "-aM a1" 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 (-aM1 a1) = a ·sM1 (-aM1 a1)",
        thin_tac "-aM a1 = -aM1 a1",
        thin_tac "-aM1 a1 ∈ 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 -aM x1 ∈ carrier M2) = a1",
        frule_tac a = "a ·sM m" and ?a1.0 = "a ·sM1 a1" 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 \<Oplus>R,M M2|]
    ==> 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 "5-1. existence of the tensor product"

constdefs
 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) 
 "FMR P M N == R module P ∧ free_generator R P (M ×c N)"

lemma (in Ring) free_gen_gen:"FMR P 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:"[|FMR P 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:funcset_mem,
        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 = {\<zero>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 =  {\<zero>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 ·sM m ∈ N")
 apply (cut_tac module_over_zeroring [of M])
 apply simp
 apply (frule_tac A = N and B = "{\<zero>M}" and c = x in subsetD, assumption+)
 apply (simp, assumption+)
apply (frule Module.submodule_inc_0 [of M R N], assumption+) 
 apply simp
done

constdefs
 Least_submodule::"[('r, 'm) Ring_scheme, ('a, 'r, 'm1) Module_scheme,
                       'a set] => 'a set"
         ("(3LSM_/ _/ _)" [100,100,101]100)
 "LSMR M T == \<Inter> {N. submodule R M N ∧ T ⊆ N}" 

lemma (in Ring) LSM_mem:"[|R module M; T ⊆ carrier M; t ∈ T|] ==> 
                                                     t ∈ (LSMR M 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|] ==>
                              (LSMR M T) ⊆ carrier M"
apply (rule subsetI, simp add:Least_submodule_def)
apply (frule Module.submodule_whole[of M R])
apply (drule_tac a = "carrier M" in forall_spec1,
       simp)
done

lemma (in Ring) LSM_sub_submodule:"[|R module M; T ⊆ carrier M; 
      submodule R M N; T ⊆ N |] ==> (LSMR M T) ⊆ N"
by (rule subsetI, simp add:Least_submodule_def)

lemma (in Ring) LSM_inc_T:"[|R module M; T ⊆ carrier M|] ==> T ⊆ (LSMR M T)"
apply (rule subsetI)
apply (simp add:LSM_mem)
done 

lemma (in Ring) LSM_submodule:"[|R module M; T ⊆ carrier M|] ==>
                submodule R M (LSMR M 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 = "\<zero>M" and A = "\<Inter>{N. N ⊆ carrier M ∧
          M +> N ∧ (∀a m. a ∈ carrier R ∧ m ∈ N --> a ·sM m ∈ 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 a = x in forall_spec1,
        drule_tac a = x in forall_spec1)
 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 = "-aM b" 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) 
 apply (rule Module.submodule_sc_closed, assumption+)
 apply (simp add:funcset_mem, simp add:funcset_mem 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 a = f in forall_spec1,
        drule_tac a = s in forall_spec1, 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 ±M s (Suc n) ·sM f (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:funcset_mem, simp add:funcset_mem 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|] ==> 
          (LSMR M 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" "LSMR M 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 ⊆ kerM,N f|] ==> LSMR M T ⊆ kerM,N f"
apply (frule Module.mker_submodule[of M R N f], assumption+)
apply (rule LSM_sub_submodule[of M T "kerM,N f"], assumption+)
done

(* in the following costdefs, MN is the free module generated by  M ×c N *)
constdefs
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)
 "TR1 R M N MN == {x. ∃m1∈carrier M. ∃m2∈carrier M. ∃n∈carrier N.
       x = (m1 ±M m2, n) ±MN (-aMN ((m1, n) ±MN (m2, n)))}"

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)
 "TR2 R M N MN == {x. ∃m∈carrier M. ∃n1∈carrier N. ∃n2∈carrier N.
            x = (m, n1 ±N n2) ±MN (-aMN ((m, n1) ±MN (m, n2)))}"

 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)
 "TR3 R M N P == {x. ∃m∈carrier M. ∃n∈carrier N. ∃ a∈carrier R.
        x = (a ·sM m, n) ±P (-aP (a ·sP (m, n)))}"
constdefs
 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)
 "TR4 R M N MN == {x. ∃m∈carrier M. ∃n∈carrier N. ∃ a∈carrier R.
  x = (m, a ·sN n) ±MN (-aMN (a ·s MN (m, n)))}"

 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)

 "TRR M N MN == LSMR MN ((TR1 R M N MN) ∪ (TR2 R M N MN) ∪ 
                                     (TR3 R M N MN) ∪ (TR4 R M N MN))"

constdefs
 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"
     "tensor_product R M N MN == MN /m (TRR M N MN)" 

syntax
 "@TENSORPROD"::"[('a, 'r, 'm1) Module_scheme,
    (('a * 'b), 'r, 'm1) Module_scheme, ('r, 'm) Ring_scheme,
    ('b, 'r, 'm1) Module_scheme] => (('a * 'b) set, 'r) Module"
  ("(4_/ _\<Otimes>_/ _)" [92,92,92,93]92)
 
translations
 "M P\<Otimes>R N" == "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. m∈carrier 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; FMR P 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; FMR P 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; FMR P 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; FMR P 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; FMR P 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 ·sM m, 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; FMR P 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; FMR P 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; FMR P M N|] ==> 
                 submodule R P (TRR M 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; FMR P M N|] ==>
  TR1 R M N P ∪ TR2 R M N P ∪ TR3 R M N P ∪ TR4 R M N P ⊆ TRR M 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; FMR P M N; m1 ∈ carrier M;
m2 ∈ carrier M; n ∈ carrier N|] ==> (m1 ±M m2, n) ±P -aP ((m1, n) ±P (m2, n))
 ∈ TRR M N P"
apply (rule subsetD[of "TR1 R M N P" "TRR M N P" 
                          "(m1 ±M m2, 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" "TRR M 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; FMR P M N; m ∈ carrier M;
       n1 ∈ carrier N; n2 ∈ carrier N |] ==> 
     (m, n1 ±N n2) ±P -aP ((m, n1) ±P (m, n2)) ∈ TRR M N P"
apply (rule subsetD[of "TR2 R M N P" "TRR M N P" 
                          "(m, n1 ±N n2) ±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" "TRR M 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; FMR P M N; m ∈ carrier M;
      n ∈ carrier N; a ∈ carrier R|] ==> 
         (a ·sM m, n) ±P -aP (a ·sP (m, n)) ∈ TRR M N P"
apply (rule subsetD[of "TR3 R M N P" "TRR M N P" 
                          "(a ·sM m, 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" "TRR M 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; FMR P M N; m ∈ carrier M;
       n ∈ carrier N; a ∈ carrier R|] ==> 
            (m, a ·sN n) ±P -aP (a ·sP (m, n)) ∈ TRR M N P"
apply (rule subsetD[of "TR4 R M N P" "TRR M N P" 
                          "(m, a ·sN n) ±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" "TRR M 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; FMR P 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 "TRR M N P"])
done

lemma (in Ring) tau_mpj_bilin1:"[| R module M; R module N; FMR P M N;
        x1 ∈ carrier M; x2 ∈ carrier M; y ∈ carrier N|]  ==> 
  (mpj P (TRR M N P)) ( x1 ±M x2, y) = 
    (mpj P (TRR M N P)) (x1, y) ±(M P\<Otimes>R N) (mpj   P (TRR M N P) (x2, y))" 
apply (frule FM_P_module[of M N P], assumption+)
apply (subgoal_tac "(x1 ±M x2, y) ±P (-aP ((x1, y) ±P (x2, y))) ∈  
              kerP,(P /m (TRR M N P)) (mpj P (TRR M N P))")
apply (frule TR_submodule [of M N P], assumption+,
       frule Module.qmodule_module [of P R "TRR M N P"], assumption+,
       frule Module.mpj_mHom [of P R "TRR M N P"], assumption+,
       frule  Module.mHom_eq_ker[of P R "P /m (TRR M N P)" "mpj P (TRR M N P)"
              "( x1 ±M x2, 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 (TRR M N P)" "mpj P (TRR M 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 (TRR M N P)",
       simp add:TR1_mem)
done

lemma (in Ring) tau_mpj_bilin2:"[|R module M; R module N; FMR P M N;
       m ∈ carrier M; n1 ∈ carrier N; n2 ∈ carrier N|]  ==> 
  (mpj P (TRR M N P)) (m, n1 ±N n2) = 
   (mpj P (TRR M N P)) (m, n1) ±(M P\<Otimes>R N) (mpj P (TRR M N P) (m, n2))"
apply (frule FM_P_module[of M N P], assumption+)
apply (subgoal_tac "(m, n1 ±N n2) ±P (-aP ((m, n1) ±P (m, n2))) ∈  
              kerP,(P /m (TRR M N P)) (mpj P (TRR M N P))")
apply (frule TR_submodule [of M N P], assumption+,
       frule Module.qmodule_module [of P R "TRR M N P"], assumption+,
       frule Module.mpj_mHom [of P R "TRR M N P"], assumption+,
       frule  Module.mHom_eq_ker[of P R "P /m (TRR M N P)" "mpj P (TRR M N P)"
              "(m, n1 ±N n2)" "(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 (TRR M N P)" "mpj P (TRR M 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 (TRR M N P)",
       simp add:TR2_mem)
done

lemma (in Ring) tau_mpj_bilin3:"[|R module M; R module N; FMR P M N;
   m ∈ carrier M; n ∈ carrier N; a ∈ carrier R|]  ==> 
  (mpj P (TRR M N P)) (a ·sM m, n) = a ·s(M P\<Otimes>R N) 
                                        (mpj P (TRR M N P) (m, n))"
apply (frule FM_P_module[of M N P], assumption+)
apply (subgoal_tac "(a ·sM m, n) ±P -aP (a ·sP (m, n)) ∈ 
          kerP,(P /m (TRR M N P)) (mpj P (TRR M N P))")
apply (frule TR_submodule [of M N P], assumption+,
       frule Module.qmodule_module [of P R "TRR M N P"], assumption+,
       frule Module.mpj_mHom [of P R "TRR M N P"], assumption+,
       frule  Module.mHom_eq_ker[of P R "P /m (TRR M N P)" "mpj P (TRR M N P)"
              "(a ·sM m, 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 (TRR M N P)" "(m, n)"
        "mpj P (TRR M 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 (TRR M N P)",
       simp add:TR3_mem)
done

lemma (in Ring) tau_mpj_bilin4:"[|R module M; R module N; FMR P M N; 
      m ∈ carrier M; n ∈ carrier N; a ∈ carrier R|]  ==>
     (mpj P (TRR M N P)) (m, a ·sN n) = a ·s(M P\<Otimes>R N) 
            (mpj P (TRR M N P) (m, n))"
apply (frule FM_P_module[of M N P], assumption+)
apply (subgoal_tac "(m, a ·sN n) ±P -aP (a ·sP (m, n)) ∈ 
          kerP,(P /m (TRR M N P)) (mpj P (TRR M N P))")
apply (frule TR_submodule [of M N P], assumption+,
       frule Module.qmodule_module [of P R "TRR M N P"], assumption+,
       frule Module.mpj_mHom [of P R "TRR M N P"], assumption+,
       frule  Module.mHom_eq_ker[of P R "P /m (TRR M N P)" "mpj P (TRR M N P)"
              "(m, a ·sN 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 (TRR M N P)" "(m, n)"
        "mpj P (TRR M 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 (TRR M N P)",
       simp add:TR4_mem)
done

constdefs
 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)"
        "tau R M N P == λx∈(M ×c N). x"

lemma (in Ring) tau_func:"[|R module M; R module N; FMR P M N|] ==>
                                 tau R M N P ∈ M ×c N -> carrier P"
apply (rule univar_func_test)
 apply (rule ballI)
 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; FMR P 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; FMR P 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; FMR P 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; FMR P 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; FMR P M N|] ==>  
      bilinear_map (compose (M ×c N) (mpj P (TRR M N P)) (tau R M N P)) 
                     R M N (M P\<Otimes>R N)"
apply (simp add:bilinear_map_def)
apply (rule conjI)
 apply (rule univar_func_test, rule ballI)
 apply (simp add:compose_def)
 apply (simp add:tau_def)
 apply (simp add: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 "TRR M N P"], assumption+)
 apply (frule Module.qmodule_module [of P R "TRR M N P"], assumption+,
       rule Module.mHom_mem [of P R "P /m (TRR M N P)" "(mpj P (TRR M 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
    
constdefs
 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"

 "tnm R P M N == compose (M ×c N) (mpj P (TRR M N P)) (tau R M N P)" 
 (* tensor natural map *)

lemma (in Ring) tnm_bilinear:"[|R module M; R module N; FMR P M N|] ==> 
        bilinear_map (tnm R P M N) R M N (M P\<Otimes>R N)"
apply (simp add:tnm_def)
apply (simp add:tau_mpj_bilinear)
done  

lemma (in Ring) tnm_mem:"[| R module M; R module N; FMR P M N; m ∈ carrier M; 
       n ∈ carrier N|]  ==> tnm R P M N (m, n) ∈ carrier (M P\<Otimes>R N)"
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 "TRR M N P" "tau R M N P (m, n)"],
       rule FM_P_module[of M N P], assumption+)
done

constdefs
 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"

  "tensor_elem R P M N m n == tnm R P M N (m, n)" 

syntax
 "@TNSELEM"::"['a, ('r, 'm) Ring_scheme, (('a * 'b), 'r, 'm1) Module_scheme,
 ('a, 'r, 'm1) Module_scheme, ('b, 'r, 'm1) Module_scheme, 'b] =>
 ('a * 'b) set" 
  ("(6_ _,__,_/ _)" [100,100,100,100,100,101]101)

 translations
 "m R,PM,N n" == "tensor_elem R P M N m n"
  
lemma (in Ring) tensor_univ_propTr:"[|R module M; R module N; FMR P 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; FMR P M N; 
      R module Z; bilinear_map f R M N Z|] ==>
     ∃!g. g∈(mHom R (M P\<Otimes>R N) 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 "TRR M N P" Z], assumption+,
        frule Module.submodule_subset[of P R "TRR M 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
          (LSMR P (TR1 R M N P ∪ TR2 R M N P ∪ TR3 R M N P ∪ TR4 R M N P))",
        thin_tac "LSMR P (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 ±M m2" 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 ±M m2, 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 ±N n2" 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 ±N n2)" 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 ·sM m" 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 ·sM m, 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 ·sN 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 = "(m, a ·sN 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)
        apply (simp add:bilinear_r_lin1)
 apply (frule Module.qmodule_module [of P R "TRR M N P"], assumption+)
 apply (rule ex_ex1I)   
     apply (erule ex1E, erule conjE,
            thin_tac "∀y. y ∈ mHom R (P /m (TRR M N P)) Z ∧
            compos P y (mpj P (TRR M N P)) = g --> y = ga")
     apply (simp add:tensor_product_def)
 apply (cut_tac h = ga in compose_assoc[of "tau R M N P" "M ×c N" 
        "carrier P" "mpj P (TRR M N P)" "carrier (P /m (TRR M N P))" _  
        "carrier Z"], simp add:tau_func,
        rule univar_func_test, rule ballI, simp add:Module.mpj_mem,
        rule univar_func_test, rule ballI,
        rule_tac f = ga and m = x in
                 Module.mHom_mem[of "P /m (TRR M N P)" R Z], assumption+,
        simp add:compos_def, blast) 

 apply (simp add:tensor_product_def,
        thin_tac "∃!ga. ga ∈ mHom R (P /m (TRR M N P)) Z ∧
              compos P ga (mpj P (TRR M N P)) = g")
  apply (frule free_gen_gen[of P M N],
         frule Module.surjec_generator[of P R "P /m (TRR M N P)" 
         "mpj P (TRR M 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 (TRR M N P)"
         R Z "mpj P (TRR M N P) ` M ×c N"], assumption+, simp, simp)
  apply (erule conjE)+
  apply (rule ballI,
      thin_tac "generator R (P /m (TRR M N P)) (mpj P (TRR M 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 (TRR M 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 (TRR M N P))
                   (tau R M N P)) = f",
         frule_tac f = "compose (M ×c N) y
         (compose (M ×c N) (mpj P (TRR M 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 (TRR M 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; FMR P M N |]
 ==>  universal_property R M N (M P\<Otimes>R N) (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

text{*                    f
                  M × N  --> Z
                    |       /
         tnp R M N  |      / g
                    |     /
                  M \<Otimes>R N
     *}

end

8. exact sequence

lemma Zm_Module:

  Module (Zm R e) R

lemma Zm_carrier:

  carrier (Zm R e) = {e}

lemma Zm_to_M_0:

  [| Module M R; f ∈ mHom R (Zm R e) M |] ==> f e = \<zero>M

lemma Z_to_M:

  [| Module M R; f ∈ mHom R (Zm R e) M; g ∈ mHom R (Zm R e) M |] ==> f = g

lemma mzeromap_mHom:

  [| Module M R; Module N R |] ==> mzeromap M N ∈ mHom R M N

lemma HOM_carrier:

  carrier (HOMR M N) = mHom R M N

lemma mHom_Z_M:

  Module M R ==> mHom R (Zm R e) M = {mzeromap (Zm R e) M}

lemma Modules_single_carrier_isom:

  [| Module N R; carrier M = {\<zero>}; carrier N = {\<zero>N} |] ==> MR N

lemma Zm_isom:

  Zm R eR Zm R u

lemma HOM_Z_M_0:

  Module M R ==> HOMR Zm R e MR Zm R e

lemma M_to_Z:

  [| Module M R; f ∈ mHom R M (Zm R e); g ∈ mHom R M (Zm R e) |] ==> f = g

lemma mHom_to_zero:

  Module M R ==> mHom R M (Zm R e) = {mzeromap M (Zm R e)}

lemma carrier_HOM_M_Z:

  Module M R ==> carrier (HOMR M Zm R e) = {mzeromap M (Zm R e)}

lemma HOM_M_Z_0:

  Module M R ==> HOMR M Zm R eR Zm R e

lemma M_to_Z_0:

  [| Module M R; f ∈ mHom R M (Zm R e) |] ==> kerM,Zm R e f = carrier M

lemma exact3_comp_0:

  [| Module L R; Module M R; Module N R; f ∈ mHom R L M; g ∈ mHom R M N;
     exact3 R L f M g N |]
  ==> compos L g f = mzeromap L N

lemma exact_im_sub_kern:

  [| Module L R; Module M R; Module N R; f ∈ mHom R L M; g ∈ mHom R M N;
     exact3 R L f M g N |]
  ==> f ` carrier L  kerM,N g

lemma mzero_im_sub_ker:

  [| Module L R; Module M R; Module N R; f ∈ mHom R L M; g ∈ mHom R M N;
     compos L g f = mzeromap L N |]
  ==> f ` carrier L  kerM,N g

lemma left_exact_injec:

  [| Module M R; Module N R; z ∈ mHom R (Zm R e) M; f ∈ mHom R M N;
     exact3 R (Zm R e) z M f N |]
  ==> injecM,N f

lemma injec_left_exact:

  [| Module M R; Module N R; z ∈ mHom R (Zm R e) M; f ∈ mHom R M N; injecM,N f |]
  ==> exact3 R (Zm R e) z M f N

lemma injec_mHom_image:

  [| Module N R; Module M1.0 R; Module M2.0 R; x ∈ mHom R N M2.0;
     f ∈ mHom R M1.0 M2.0; x ` carrier N  f ` carrier M1.0; injecM1.0,M2.0 f |]
  ==> (λn∈carrier N. SOME m. m ∈ carrier M1.0x n = f m) ∈ mHom R N M1.0 ∧
      compos N fn∈carrier N. SOME m. m ∈ carrier M1.0x n = f m) = x

lemma right_exact_surjec:

  [| Module M R; Module N R; f ∈ mHom R M N; p ∈ mHom R N (Zm R e);
     exact3 R M f N p (Zm R e) |]
  ==> surjecM,N f

lemma surjec_right_exact:

  [| Module M R; Module N R; f ∈ mHom R M N; p ∈ mHom R N (Zm R e); surjecM,N f |]
  ==> exact3 R M f N p (Zm R e)

lemma exact4_exact3:

  [| Module M R; Module N R; z ∈ mHom R (Zm R e) M; f ∈ mHom R M N;
     z1.0 ∈ mHom R N (Zm R e); exact4 R (Zm R e) z M f N z1.0 (Zm R e) |]
  ==> exact3 R (Zm R e) z M f N ∧ exact3 R M f N z1.0 (Zm R e)

lemma exact4_bijec:

  [| Module M R; Module N R; z ∈ mHom R (Zm R e) M; f ∈ mHom R M N;
     z1.0 ∈ mHom R N (Zm R e); exact4 R (Zm R e) z M f N z1.0 (Zm R e) |]
  ==> bijecM,N f

lemma exact_im_sub_ker:

  [| Module L R; Module M R; Module N R; f ∈ mHom R L M; g ∈ mHom R M N;
     z1.0 ∈ mHom R N (Zm R e); Module Z R; exact4 R L f M g N z1.0 (Zm R e);
     x ∈ mHom R M Z; compos L x f = mzeromap L Z |]
  ==> (λz∈carrier N. x (SOME y. y ∈ carrier Mg y = z)) ∈ mHom R N Z

lemma exact_im_sub_ker1:

  [| Module L R; Module M R; Module N R; f ∈ mHom R L M; g ∈ mHom R M N;
     z1.0 ∈ mHom R N (Zm R e); Module Z R; exact4 R L f M g N z1.0 (Zm R e);
     x ∈ mHom R M Z; compos L x f = mzeromap L Z |]
  ==> compos Mz∈carrier N. x (SOME y. y ∈ carrier Mg y = z)) g = x

lemma short_exact_sequence:

  [| Module M R; submodule R M N; z ∈ mHom R (Zm R e) (mdl M N);
     z1.0 ∈ 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.0
       (Zm R e)

lemma rexact4_lexact4_HOM:

  [| Module M1.0 R; Module M2.0 R; Module M3.0 R; f ∈ mHom R M1.0 M2.0;
     g ∈ mHom R M2.0 M3.0; z1.0 ∈ mHom R M3.0 (Zm R e);
     exact4 R M1.0 f M2.0 g M3.0 z1.0 (Zm R e) |]
  ==> ∀N. Module N R -->
          exact4 R (HOMR Zm R e N) (sup_sharp R M3.0 (Zm R e) N z1.0)
           (HOMR M3.0 N) (sup_sharp R M2.0 M3.0 N g) (HOMR M2.0 N)
           (sup_sharp R M1.0 M2.0 N f) (HOMR M1.0 N)

lemma exact_HOM_exactTr:

  [| Ring R; f ∈ mHom R M1.0 M2.0; g ∈ mHom R M2.0 M3.0;
     z1.0 ∈ mHom R M3.0 (Zm R e); Module NV R;
     ∀N. Module N R -->
         exact4 R (HOMR Zm R e N) (sup_sharp R M3.0 (Zm R e) N z1.0) (HOMR M3.0 N)
          (sup_sharp R M2.0 M3.0 N g) (HOMR M2.0 N) (sup_sharp R M1.0 M2.0 N f)
          (HOMR M1.0 N);
     Module L R |]
  ==> exact4 R (HOMR Zm R e L) (sup_sharp R M3.0 (Zm R e) L z1.0) (HOMR M3.0 L)
       (sup_sharp R M2.0 M3.0 L g) (HOMR M2.0 L) (sup_sharp R M1.0 M2.0 L f)
       (HOMR M1.0 L)

lemma lexact4_rexact4_HOM:

  [| Ring R; Module M1.0 R; Module M2.0 R; Module M3.0 R; f ∈ mHom R M1.0 M2.0;
     g ∈ mHom R M2.0 M3.0; z ∈ mHom R (Zm R e) M1.0;
     exact4 R (Zm R e) z M1.0 f M2.0 g M3.0 |]
  ==> ∀N. Module N R -->
          exact4 R (HOMR N Zm R e) (sub_sharp R N (Zm R e) M1.0 z) (HOMR N M1.0)
           (sub_sharp R N M1.0 M2.0 f) (HOMR N M2.0) (sub_sharp R N M2.0 M3.0 g)
           (HOMR N M3.0)

9. Tensor product

lemma prod_carr_mem:

  [| Module M R; Module N R; m ∈ carrier M; n ∈ carrier N |] ==> (m, n) ∈ M ×c N

lemma bilinear_func:

  bilinear_map f R M N Z ==> fM ×c N -> carrier Z

lemma bilinear_mem:

  [| Module M1.0 R; Module M2.0 R; Module N R; m1.0 ∈ carrier M1.0;
     m2.0 ∈ carrier M2.0; bilinear_map f R M1.0 M2.0 N |]
  ==> f (m1.0, m2.0) ∈ carrier N

lemma bilinear_l_add:

  [| Module M1.0 R; Module M2.0 R; Module N R; m11.0 ∈ carrier M1.0;
     m12.0 ∈ carrier M1.0; m2.0 ∈ carrier M2.0; bilinear_map f R M1.0 M2.0 N |]
  ==> f (m11.0 ±M1.0 m12.0, m2.0) = f (m11.0, m2.0) ±N f (m12.0, m2.0)

lemma bilinear_l_add1:

  [| Module M1.0 R; Module M2.0 R; Module N R; m11.0 ∈ carrier M1.0;
     m12.0 ∈ carrier M1.0; m2.0 ∈ carrier M2.0; bilinear_map f R M1.0 M2.0 N |]
  ==> f (m11.0 ±M1.0 m12.0, m2.0) ±N -aN (f (m11.0, m2.0) ±N f (m12.0, m2.0)) =
      \<zero>N

lemma bilinear_r_add:

  [| Module M1.0 R; Module M2.0 R; Module N R; m ∈ carrier M1.0;
     m21.0 ∈ carrier M2.0; m22.0 ∈ carrier M2.0; bilinear_map f R M1.0 M2.0 N |]
  ==> f (m, m21.0 ±M2.0 m22.0) = f (m, m21.0) ±N f (m, m22.0)

lemma bilinear_r_add1:

  [| Module M1.0 R; Module M2.0 R; Module N R; m ∈ carrier M1.0;
     m21.0 ∈ carrier M2.0; m22.0 ∈ carrier M2.0; bilinear_map f R M1.0 M2.0 N |]
  ==> f (m, m21.0 ±M2.0 m22.0) ±N -aN (f (m, m21.0) ±N f (m, m22.0)) = \<zero>N

lemma bilinear_l_lin:

  [| Module M1.0 R; Module M2.0 R; Module N R; m1.0 ∈ carrier M1.0;
     m2.0 ∈ carrier M2.0; r ∈ carrier R; bilinear_map f R M1.0 M2.0 N |]
  ==> f (r ·sM1.0 m1.0, m2.0) = r ·sN f (m1.0, m2.0)

lemma bilinear_l_lin1:

  [| Module M1.0 R; Module M2.0 R; Module N R; m1.0 ∈ carrier M1.0;
     m2.0 ∈ carrier M2.0; r ∈ carrier R; bilinear_map f R M1.0 M2.0 N |]
  ==> f (r ·sM1.0 m1.0, m2.0) ±N -aN r ·sN f (m1.0, m2.0) = \<zero>N

lemma bilinear_r_lin:

  [| Module M1.0 R; Module M2.0 R; Module N R; m1.0 ∈ carrier M1.0;
     m2.0 ∈ carrier M2.0; r ∈ carrier R; bilinear_map f R M1.0 M2.0 N |]
  ==> f (m1.0, r ·sM2.0 m2.0) = r ·sN f (m1.0, m2.0)

lemma bilinear_r_lin1:

  [| Module M1.0 R; Module M2.0 R; Module N R; m1.0 ∈ carrier M1.0;
     m2.0 ∈ carrier M2.0; r ∈ carrier R; bilinear_map f R M1.0 M2.0 N |]
  ==> f (m1.0, r ·sM2.0 m2.0) ±N -aN r ·sN f (m1.0, m2.0) = \<zero>N

lemma bilinear_l_0:

  [| Module M1.0 R; Module M2.0 R; Module N R; m2.0 ∈ carrier M2.0;
     bilinear_map f R M1.0 M2.0 N |]
  ==> f (\<zero>M1.0, m2.0) = \<zero>N

lemma bilinear_r_0:

  [| Module M1.0 R; Module M2.0 R; Module N R; m1.0 ∈ carrier M1.0;
     bilinear_map f R M1.0 M2.0 N |]
  ==> f (m1.0, \<zero>M2.0) = \<zero>N

lemma tensor_prod_uniqueTr:

  [| Ring R; Module M R; Module N R; Module MN R; Module MN1.0 R;
     universal_property R M N MN f; universal_property R M N MN1.0 g |]
  ==> ∃!k. k ∈ mHom R MN1.0 MNcompose (M ×c N) k g = f

lemma tensor_prod_unique:

  [| Ring R; Module M R; Module N R; Module MN R; Module MN1.0 R;
     universal_property R M N MN f; universal_property R M N MN1.0 g |]
  ==> MNR MN1.0

6. Construction of an abelian group

1. free generated abelian group I, direct sum and direct product 2

lemma add_set_mono:

  A  B ==> add_set f A n  add_set f B n

lemma addition_inc_add:

  add_set f A n  addition_set f A

lemma addition_inc_add0:

  A  addition_set f A

lemma addition_set_mono:

  A  B ==> addition_set f A  addition_set f B

lemma a_in_aug_pm_set:

  aA ==> a ∈ aug_pm_set z i A

lemma A_sub_aug_pm_set:

  A  aug_pm_set z i A

lemma addition_sub_aug_pm_addition:

  addition_set f A  addition_set f (aug_pm_set z i A)

lemma assoc_bpp_restrict:

  [| A  B; assoc_bpp B f |] ==> assoc_bpp A f

lemma addition_assoc:

  [| assoc_bpp A f; x ∈ addition_set f A; y ∈ addition_set f A;
     z ∈ addition_set f A |]
  ==> x \<^sub>f+ y \<^sub>f+ z = x \<^sub>f+ (y \<^sub>f+ z)

lemma bpp_closedTr:

  assoc_bpp A f
  ==> ∀x y. x ∈ add_set f A ny ∈ add_set f A m -->
            x \<^sub>f+ y ∈ add_set f A (n + m + Suc 0)

lemma bpp_closed1:

  [| assoc_bpp A f; x ∈ add_set f A n; y ∈ add_set f A m |]
  ==> x \<^sub>f+ y ∈ add_set f A (n + m + Suc 0)

lemma bpp_closed:

  [| assoc_bpp A f; x ∈ addition_set f A; y ∈ addition_set f A |]
  ==> x \<^sub>f+ y ∈ addition_set f A

lemma aug_addition_inc_z:

  z ∈ addition_set f (aug_pm_set z i A)

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 \<^sub>f+ y ∈ addition_set f (aug_pm_set z i A)

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 \<^sub>f+ y = y \<^sub>f+ x

lemma addition_set_inc_z:

  z ∈ addition_set f (aug_pm_set z i A)

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 |]
  ==> \<^sub>i- x ∈ add_set f (aug_pm_set z i A) 0

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 -->
          \<^sub>i- x ∈ add_set f (aug_pm_set z i A) n

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 |]
  ==> \<^sub>i- x ∈ add_set f (aug_pm_set z i A) n

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) |]
  ==> \<^sub>i- x ∈ addition_set f (aug_pm_set z i A)

lemma aug_zero_unique:

  [| commute_bpp f (aug_pm_set z i A); z1.0 ∈ addition_set f (aug_pm_set z i A);
     zeroA z i f A z; zeroA z i f A z1.0 |]
  ==> z = z1.0

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 |]
  ==> ∀a∈addition_set f (aug_pm_set z i A). \<^sub>i- a \<^sub>f+ a = 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)

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)

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)

2. Abelian group generated by a singleton (constructive)

lemma aug_pm_aug_pm_minus:

  ipp_cond1 {a} i ==> aug_pm_set z i {a} = aug_pm_set z i {\<^sub>i- a}

lemma ipp_cond1_minus:

  ipp_cond1 {a} i ==> ipp_cond1 {\<^sub>i- a} i

lemma ipp_cond2_minus:

  [| ipp_cond1 {a} i; ipp_cond2 z {a} i f |] ==> ipp_cond2 z {\<^sub>i- a} i f

lemma zeroA_minus:

  [| ipp_cond1 {a} i; zeroA z i f {a} z1.0 |] ==> zeroA z i f {\<^sub>i- a} z1.0

lemma inv_ipp_minus:

  [| ipp_cond1 {a} i; inv_ipp z i f {a} |] ==> inv_ipp z i f {\<^sub>i- a}

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 \<^sub>f+ \<^sub>i- a ∈ add_set f {a} n

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 \<^sub>f+ \<^sub>i- a = z

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 {\<^sub>i- a}) f ∧
      ipp_cond1 {\<^sub>i- a} i ∧
      ipp_cond2 z {\<^sub>i- a} i f ∧
      inv_ipp z i f {\<^sub>i- a} ∧
      commute_bpp f (aug_pm_set z i {\<^sub>i- a}) ∧ zeroA z i f {\<^sub>i- a} z

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 {\<^sub>i- a} n |]
  ==> s \<^sub>f+ \<^sub>i- a ∈ add_set f {\<^sub>i- a} (Suc n)

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 {\<^sub>i- a} m1) ∨ x = z

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 {\<^sub>i- a} m1) ∨ x = z

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} ny ∈ add_set f {a} n --> x = y

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. x ∈ add_set f {a} n

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. x ∈ add_set f {\<^sub>i- a} n

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

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 {\<^sub>i- a} n; y ∈ add_set f {\<^sub>i- a} n |]
  ==> x = y

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} nt ∈ add_set f {\<^sub>i- a} n -->
            s \<^sub>f+ t = z

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 {\<^sub>i- a} n |]
  ==> s \<^sub>f+ t = z

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<Suc n.
         (THE x. x ∈ add_set f {a} (Suc n)) \<^sub>f+
            (THE x. x ∈ add_set f {\<^sub>i- a} m) =
         (THE x. x ∈ add_set f {a} (n - m))

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)) \<^sub>f+
         (THE x. x ∈ add_set f {\<^sub>i- a} m) =
      (THE x. x ∈ add_set f {a} (n - m))

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 {\<^sub>i- a} (Suc m)) \<^sub>f+
         (THE x. x ∈ add_set f {a} n) =
      (THE x. x ∈ add_set f {\<^sub>i- a} (m - 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 |]
  ==> n\<Odot>af,i,z ∈ addition_set f (aug_pm_set z i {a})

lemma assoc_aug_assoc:

  assoc_bpp (aug_pm_set z i {a}) f ==> assoc_bpp {a} f

lemma single_addition_posTr:

  [| 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; 0 < n; 0 < m |]
  ==> (THE x. x ∈ add_set f {a} (nat (n - 1))) \<^sub>f+
         (THE x. x ∈ add_set f {a} (nat (m - 1))) =
      (THE x. x ∈ add_set f {a} (nat (n + m - 1)))

lemma single_addition_pos:

  [| 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; 0 < n; 0 < m |]
  ==> n\<Odot>af,i,z \<^sub>f+ m\<Odot>af,i,z = (n + m)\<Odot>af,i,z

lemma single_addition_neg:

  [| 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; n < 0; m < 0 |]
  ==> n\<Odot>af,i,z \<^sub>f+ m\<Odot>af,i,z = (n + m)\<Odot>af,i,z

lemma single_addition_zero:

  [| 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 |]
  ==> 0\<Odot>af,i,z = z

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 |]
  ==> n\<Odot>af,i,z \<^sub>f+ m\<Odot>af,i,z = (n + m)\<Odot>af,i,z

lemma single_addition_pm:

  [| 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 |]
  ==> n\<Odot>af,i,z \<^sub>f+ m\<Odot>af,i,z = (n + m)\<Odot>af,i,z

lemma single_inv:

  [| 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 |]
  ==> \<^sub>i- m\<Odot>af,i,z = (- m)\<Odot>af,i,z

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 |]
  ==> n\<Odot>af,i,z  m\<Odot>af,i,z

lemma fag_single_free:

  [| fags_cond f z i a; n  m |] ==> n\<Odot>af,i,z  m\<Odot>af,i,z

lemma fag_single_free1:

  [| fags_cond f z i a; n\<Odot>af,i,z = m\<Odot>af,i,z |] ==> n = m

lemma fags_mem:

  fags_cond f z i a ==> n\<Odot>af,i,z ∈ fags_carr f z i a

lemma fags_ippTr:

  fags_cond f z i a ==> fags_ipp f z i a (n\<Odot>af,i,z) = (- n)\<Odot>af,i,z

lemma fags_bppTr:

  fags_cond f z i a
  ==> fags_bpp f z i a (n\<Odot>af,i,z) (m\<Odot>af,i,z) = (n + m)\<Odot>af,i,z

lemma fags_ag:

  fags_cond f z i a ==> aGroup (fags f z i a)

3. Abelian Group generated by one element II (nonconstructive)

lemma asum_mem:

  a ∈ carrier A ==> aSum A n a ∈ carrier A

lemma nt_mem0:

  a ∈ carrier A ==> n\<triangleright>aA ∈ carrier A

lemma nt_zero0:

  a ∈ carrier A ==> 0\<triangleright>aA = \<zero>

lemma nt_1:

  a ∈ carrier A ==> 1\<triangleright>aA = a

lemma asumTr:

  a ∈ carrier A ==> aSum A (n + m) a = aSum A n a ± aSum A m a

lemma aSum_zero:

  a ∈ carrier A ==> aSum A n \<zero> = \<zero>

lemma agsum_add1p:

  [| a ∈ carrier A; 0  n; 0  m |]
  ==> (n + m)\<triangleright>aA = n\<triangleright>aA ± m\<triangleright>aA

lemma agsum_add1m:

  [| a ∈ carrier A; n < 0; m < 0 |]
  ==> (n + m)\<triangleright>aA = n\<triangleright>aA ± m\<triangleright>aA

lemma agsum_add2Tr:

  a ∈ carrier A ==> \<zero> = aSum A n a ± aSum A n (-a a)

lemma agsum_add2p:

  [| a ∈ carrier A; 0  n |]
  ==> \<zero> = n\<triangleright>aA ± (- n)\<triangleright>aA

lemma agsum_add2m:

  [| a ∈ carrier A; n < 0 |]
  ==> \<zero> = n\<triangleright>aA ± (- n)\<triangleright>aA

lemma agsum_add3pm:

  [| a ∈ carrier A; 0 < n; m < 0 |]
  ==> (n + m)\<triangleright>aA = n\<triangleright>aA ± m\<triangleright>aA

lemma agsum_add3mp:

  [| a ∈ carrier A; n < 0; 0 < m |]
  ==> (n + m)\<triangleright>aA = n\<triangleright>aA ± m\<triangleright>aA

lemma nt_sum0:

  a ∈ carrier A
  ==> (n + m)\<triangleright>aA = n\<triangleright>aA ± m\<triangleright>aA

lemma nt_inv0:

  a ∈ carrier A ==> -a n\<triangleright>aA = (- n)\<triangleright>aA

lemma m_x_asum:

  [| a ∈ carrier A; b ∈ carrier A |]
  ==> aSum A m (a ± b) = aSum A m a ± aSum A m b

lemma asum_multTr_pp:

  a ∈ carrier A ==> aSum A m (aSum A n a) = aSum A (m * n) a

lemma nt_mult_pp:

  [| a ∈ carrier A; 0  m; 0  n |]
  ==> m\<triangleright>n\<triangleright>aAA = (m * n)\<triangleright>aA

lemma 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)

lemma nt_mult_pm:

  [| a ∈ carrier A; 0  m; n < 0 |]
  ==> m\<triangleright>n\<triangleright>aAA = (m * n)\<triangleright>aA

lemma 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)

lemma nt_mult_mp:

  [| a ∈ carrier A; m < 0; 0  n |]
  ==> m\<triangleright>n\<triangleright>aAA = (m * n)\<triangleright>aA

lemma 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

lemma nt_mult_mm:

  [| a ∈ carrier A; m < 0; n < 0 |]
  ==> m\<triangleright>n\<triangleright>aAA = (m * n)\<triangleright>aA

lemma nt_mult_assoc0:

  a ∈ carrier A
  ==> m\<triangleright>n\<triangleright>aAA = (m * n)\<triangleright>aA

lemma single_gen_carrTr:

  a ∈ carrier A ==> @ASubG A {x. ∃n. x = n\<triangleright>aA}

lemma ag_single_inc_a:

  ag_single_gen A a ==> a ∈ carrier A

lemma single_gen:

  ag_single_gen A a ==> carrier A = {g. ∃n. g = n\<triangleright>aA}

lemma single_gen_free_neg:

  [| sfg A a; n\<triangleright>aA = \<zero> |] ==> n = 0

lemma sfg_G_inc_a:

  sfg A a ==> a ∈ carrier A

lemma sfg_agroup:

  sfg A a ==> aGroup A

lemma mem_G_nt:

  [| sfg A a; x ∈ carrier A |] ==> ∃n. x = n\<triangleright>aA

lemma nt_mem:

  sfg A a ==> n\<triangleright>aA ∈ carrier A

lemma nt_zero:

  sfg A a ==> 0\<triangleright>aA = \<zero>

lemma nt_sum:

  sfg A a
  ==> (n + m)\<triangleright>aA = n\<triangleright>aA ± m\<triangleright>aA

lemma nt_inv:

  sfg A a ==> -a n\<triangleright>aA = (- n)\<triangleright>aA

lemma nt_mult_assoc:

  sfg A a ==> m\<triangleright>n\<triangleright>aAA = (m * n)\<triangleright>aA

lemma sfg_free:

  [| sfg A a; n  m |] ==> n\<triangleright>aA  m\<triangleright>aA

lemma sfg_free_inj:

  [| sfg A a; n\<triangleright>aA = m\<triangleright>aA |] ==> n = m

3. Free Generated Modules (constructive)

lemma fgmodule_carr:

  carrier (fgmodule R A z i f s) = addition_set f (aug_pm_set z i (s_set R s A))

lemma a_in_s_set:

  aA ==> a ∈ s_set R s A

lemma ra_in_s_set:

  [| r ∈ carrier R; aA |] ==> r \<^sub>s· a ∈ s_set R s A

lemma in_aug_pm_set:

  (x ∈ aug_pm_set z i A) = (x = zxAx ∈ minus_set i A)

lemma in_s_set:

  x ∈ s_set R s A ==> (∃r∈carrier R. ∃aA. x = r \<^sub>s· a) ∨ xA

lemma 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 \<^sub>s· x ∈ aug_pm_set z i (s_set R s A)

lemma 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 |]
  ==> ∀r∈carrier R.
         ∀x∈add_set f (aug_pm_set z i (s_set R s A)) n.
            r \<^sub>s· x ∈ add_set f (aug_pm_set z i (s_set R s A)) n

lemma 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 |]
  ==> ∀r∈carrier R.
         ∀x∈addition_set f (aug_pm_set z i (s_set R s A)).
            r \<^sub>s· x ∈ addition_set f (aug_pm_set z i (s_set R s A))

lemma 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)) |]
  ==> ∀x∈add_set f (aug_pm_set z i (s_set R s A)) n. 1r \<^sub>s· x = x

lemma 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)) |]
  ==> ∀x∈addition_set f (aug_pm_set z i (s_set R s A)). 1r \<^sub>s· x = x

lemma 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 |]
  ==> ∀a∈carrier R.
         ∀b∈carrier R.
            ∀x∈add_set f (aug_pm_set z i (s_set R s A)) n.
               a \<^sub>s· (b \<^sub>s· x) = a ·r b \<^sub>s· x

lemma 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)) |]
  ==> ∀a∈carrier R.
         ∀b∈carrier R.
            ∀x∈addition_set f (aug_pm_set z i (s_set R s A)).
               a \<^sub>s· (b \<^sub>s· x) = a ·r b \<^sub>s· x

lemma 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 \<^sub>f+ y = y \<^sub>f+ x

lemma 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

lemma 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 |]
  ==> ∀a∈carrier R.
         ∀b∈carrier R.
            ∀x∈add_set f (aug_pm_set z i (s_set R s A)) n.
               (a ± b) \<^sub>s· x = a \<^sub>s· x \<^sub>f+ b \<^sub>s· x

lemma 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 |]
  ==> ∀a∈carrier R.
         ∀b∈carrier R.
            ∀x∈addition_set f (aug_pm_set z i (s_set R s A)).
               (a ± b) \<^sub>s· x = a \<^sub>s· x \<^sub>f+ b \<^sub>s· x

lemma 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 \<^sub>s· x ∈ addition_set f (aug_pm_set z i (s_set R s A))

lemma fgmodule_is_module:

  fgmodule_condition R f i s A z ==> Module (fgmodule R A z i f s) R

lemma a_in_carr_fgmodule:

  aA ==> a ∈ carrier (fgmodule R A z i f s)

4. a fgmodule and a free module

lemma fg_zeroTr:

  [| fgmodule_condition R f i s A z; aA |] ==> \<zero> \<^sub>s· a = z

lemma 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

lemma 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

lemma generator_of_fgm:

  fgmodule_condition R f i s A z ==> generator R (fgmodule R A z i f s) A

lemma fg_freeTr1:

  [| Module M R; free_generator R M A; Module (fgmodule R A z i f s) R;
     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} -> Asa ∈ {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

lemma fg_freeTr:

  [| Module M R; free_generator R M A; Module (fgmodule R A z i f s) R;
     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} -> 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

lemma fg_free1:

  [| A  {}; fgmodule_condition R f i s A z;
     free_generator R (fgmodule R A z i f s) A; Module M R;
     free_generator R M A |]
  ==> MR fgmodule R A z i f s

lemma fg_free:

  [| fgmodule_condition R f i s A z; free_generator R (fgmodule R A z i f s) A;
     Module M R; free_generator R M A |]
  ==> MR fgmodule R A z i f s

5. direct sum, again

lemma ds2_commute:

  [| Module M1.0 R; Module M2.0 R; Module M R; M1.0 \<Oplus>R,M M2.0 |]
  ==> M2.0 \<Oplus>R,M M1.0

lemma msub_addition:

  [| Module M R; Module M1.0 R; msubmodule R M M1.0; x ∈ carrier M1.0;
     y ∈ carrier M1.0 |]
  ==> x ±M1.0 y = x ±M y

lemma msub_mOp:

  [| Module M R; Module M1.0 R; msubmodule R M M1.0; x ∈ carrier M1.0 |]
  ==> -aM1.0 x = -aM x

lemma msub_sprod:

  [| Module M R; Module M1.0 R; msubmodule R M M1.0; a ∈ carrier R;
     x ∈ carrier M1.0 |]
  ==> a ·sM1.0 x = a ·sM x

lemma msub_submodule:

  [| Module M R; Module M1.0 R; msubmodule R M M1.0 |]
  ==> submodule R M (carrier M1.0)

lemma ds2_unique:

  [| Module M R; Module M1.0 R; Module M2.0 R; M1.0 \<Oplus>R,M M2.0;
     m1.0 ∈ carrier M1.0; m1' ∈ carrier M1.0; m2.0 ∈ carrier M2.0;
     m2' ∈ carrier M2.0; m1.0 ±M m2.0 = m1' ±M m2' |]
  ==> m1.0 = m1'm2.0 = m2'

lemma miota_injec:

  [| Module M R; Module M1.0 R; Module M2.0 R; M1.0 \<Oplus>R,M M2.0;
     msubmodule R M M1.0 |]
  ==> miota R M1.0 M ∈ mHom R M1.0 M ∧ injecM1.0,M miota R M1.0 M

lemma ds2_components:

  [| Module M1.0 R; Module M2.0 R; Module M R; M1.0 \<Oplus>R,M M2.0;
     a ∈ carrier M |]
  ==> ∃a1∈carrier M1.0. ∃a2∈carrier M2.0. a = a1 ±M a2

lemma ds2_components1:

  [| Module M1.0 R; Module M2.0 R; Module M R; M1.0 \<Oplus>R,M M2.0;
     a ∈ carrier M |]
  ==> ∃a1∈carrier M1.0. a ±M -aM a1 ∈ carrier M2.0

lemma mprojTr1:

  [| Module M1.0 R; Module M2.0 R; Module M R; M1.0 \<Oplus>R,M M2.0;
     x ∈ carrier M |]
  ==> ∃!x1. x1 ∈ carrier M1.0x ±M -aM x1 ∈ carrier M2.0

lemma mprojTr2:

  [| Module M1.0 R; Module M2.0 R; Module M R; M1.0 \<Oplus>R,M M2.0;
     x ∈ carrier M; x1.0 ∈ carrier M1.0; x ±M -aM x1.0 ∈ carrier M2.0;
     y1.0 ∈ carrier M1.0; x ±M -aM y1.0 ∈ carrier M2.0 |]
  ==> x1.0 = y1.0

lemma mprojTr3:

  [| Module M1.0 R; Module M2.0 R; Module M R; M1.0 \<Oplus>R,M M2.0;
     a ∈ carrier M; a1.0 ∈ carrier M1.0; a ±M -aM a1.0 ∈ carrier M2.0 |]
  ==> (THE x1. x1 ∈ carrier M1.0a ±M -aM x1 ∈ carrier M2.0) = a1.0

lemma mproj:

  [| Module M1.0 R; Module M2.0 R; Module M R; M1.0 \<Oplus>R,M M2.0 |]
  ==> mproj1 R M1.0 M2.0 M ∈ mHom R M M1.0

lemma mproj2:

  [| Module M1.0 R; Module M2.0 R; Module M R; M1.0 \<Oplus>R,M M2.0 |]
  ==> mproj2 R M1.0 M2.0 M ∈ mHom R M M2.0

5-1. existence of the tensor product

lemma free_gen_gen:

  FMR P M N ==> generator R P (M ×c N)

lemma free_gen_mem:

  [| FMR P M N; aM ×c N |] ==> a ∈ carrier P

lemma mHom_lin_nsumTr:

  [| Module M R; Module N R; t ∈ mHom R M N |]
  ==> f ∈ {j. j  n} -> carrier M --> te M f n) = Σe N cmp t f n

lemma mHom_lin_nsum:

  [| Module M R; Module N R; t ∈ mHom R M N; f ∈ {j. j  n} -> carrier M |]
  ==> te M f n) = Σe N cmp t f n

lemma module_over_zeroring:

  [| zeroring R; Module M R |] ==> carrier M = {\<zero>M}

lemma submodule_over_zeroring:

  [| zeroring R; Module M R; submodule R M N |] ==> N = {\<zero>M}

lemma LSM_mem:

  [| Module M R; T  carrier M; tT |] ==> t ∈ LSMR M T

lemma LSM_sub_M:

  [| Module M R; T  carrier M |] ==> LSMR M T  carrier M

lemma LSM_sub_submodule:

  [| Module M R; T  carrier M; submodule R M N; T  N |] ==> LSMR M T  N

lemma LSM_inc_T:

  [| Module M R; T  carrier M |] ==> T  LSMR M T

lemma LSM_submodule:

  [| Module M R; T  carrier M |] ==> submodule R M (LSMR M T)

lemma linear_comb_memTr:

  [| Module M R; submodule R M N; T  N |]
  ==> ∀f s. f ∈ {j. j  n} -> Ts ∈ {j. j  n} -> carrier R -->
            l_comb R M n s fN

lemma linear_comb_mem:

  [| Module M R; submodule R M N; T  N; f ∈ {j. j  n} -> T;
     s ∈ {j. j  n} -> carrier R |]
  ==> l_comb R M n s fN

lemma LSM_eq_linear_span:

  [| Module M R; T  carrier M |] ==> LSMR M T = linear_span R M (carrier R) T

lemma LSM_sub_ker:

  [| Module M R; Module N R; T  carrier M; f ∈ mHom R M N; T  kerM,N f |]
  ==> LSMR M T  kerM,N f

lemma mem_cartesian:

  [| Module M R; Module N R; m ∈ carrier M; n ∈ carrier N |] ==> (m, n) ∈ M ×c N

lemma cartesianTr:

  [| Module M R; Module N R; xM ×c N |]
  ==> ∃m n. m ∈ carrier Mn ∈ carrier Nx = (m, n)

lemma free_module_mem:

  [| Module M R; Module N R; m ∈ carrier M; n ∈ carrier N; FMR P M N |]
  ==> (m, n) ∈ carrier P

lemma FM_P_module:

  [| Module M R; Module N R; FMR P M N |] ==> Module P R

lemma TR1_sub_carr:

  [| Module M R; Module N R; FMR P M N |] ==> TR1 R M N P  carrier P

lemma TR2_sub_carr:

  [| Module M R; Module N R; FMR P M N |] ==> TR2 R M N P  carrier P

lemma TR3_sub_carr:

  [| Module M R; Module N R; FMR P M N |] ==> TR3 R M N P  carrier P

lemma TR4_sub_carr:

  [| Module M R; Module N R; FMR P M N |] ==> TR4 R M N P  carrier P

lemma TR_sub_carr:

  [| Module M R; Module N R; FMR P M N |]
  ==> TR1 R M N P ∪ TR2 R M N P ∪ TR3 R M N P ∪ TR4 R M N P  carrier P

lemma TR_submodule:

  [| Module M R; Module N R; FMR P M N |] ==> submodule R P (TRR M N P)

lemma TR_cont_TR1234:

  [| Module M R; Module N R; FMR P M N |]
  ==> TR1 R M N P ∪ TR2 R M N P ∪ TR3 R M N P ∪ TR4 R M N P  TRR M N P

lemma TR1_mem:

  [| Module M R; Module N R; FMR P M N; m1.0 ∈ carrier M; m2.0 ∈ carrier M;
     n ∈ carrier N |]
  ==> (m1.0 ±M m2.0, n) ±P -aP ((m1.0, n) ±P (m2.0, n)) ∈ TRR M N P

lemma TR2_mem:

  [| Module M R; Module N R; FMR P M N; m ∈ carrier M; n1.0 ∈ carrier N;
     n2.0 ∈ carrier N |]
  ==> (m, n1.0 ±N n2.0) ±P -aP ((m, n1.0) ±P (m, n2.0)) ∈ TRR M N P

lemma TR3_mem:

  [| Module M R; Module N R; FMR P M N; m ∈ carrier M; n ∈ carrier N;
     a ∈ carrier R |]
  ==> (a ·sM m, n) ±P -aP a ·sP (m, n) ∈ TRR M N P

lemma TR4_mem:

  [| Module M R; Module N R; FMR P M N; m ∈ carrier M; n ∈ carrier N;
     a ∈ carrier R |]
  ==> (m, a ·sN n) ±P -aP a ·sP (m, n) ∈ TRR M N P

lemma tensor_product_module:

  [| Module M R; Module N R; FMR P M N |] ==> Module (M P\<Otimes>R N) R

lemma tau_mpj_bilin1:

  [| Module M R; Module N R; FMR P M N; x1.0 ∈ carrier M; x2.0 ∈ carrier M;
     y ∈ carrier N |]
  ==> mpj P (TRR M N P) (x1.0 ±M x2.0, y) =
      mpj P (TRR M N P) (x1.0, y) ±M P\<Otimes>R N mpj P (TRR M N P) (x2.0, y)

lemma tau_mpj_bilin2:

  [| Module M R; Module N R; FMR P M N; m ∈ carrier M; n1.0 ∈ carrier N;
     n2.0 ∈ carrier N |]
  ==> mpj P (TRR M N P) (m, n1.0 ±N n2.0) =
      mpj P (TRR M N P) (m, n1.0) ±M P\<Otimes>R N mpj P (TRR M N P) (m, n2.0)

lemma tau_mpj_bilin3:

  [| Module M R; Module N R; FMR P M N; m ∈ carrier M; n ∈ carrier N;
     a ∈ carrier R |]
  ==> mpj P (TRR M N P) (a ·sM m, n) =
      a ·sM P\<Otimes>R N mpj P (TRR M N P) (m, n)

lemma tau_mpj_bilin4:

  [| Module M R; Module N R; FMR P M N; m ∈ carrier M; n ∈ carrier N;
     a ∈ carrier R |]
  ==> mpj P (TRR M N P) (m, a ·sN n) =
      a ·sM P\<Otimes>R N mpj P (TRR M N P) (m, n)

lemma tau_func:

  [| Module M R; Module N R; FMR P M N |] ==> tau R M N PM ×c N -> carrier P

lemma tau_mem:

  [| Module M R; Module N R; m ∈ carrier M; n ∈ carrier N; FMR P M N |]
  ==> tau R M N P (m, n) ∈ carrier P

lemma tau_inj0:

  [| ¬ zeroring R; Module M R; Module N R; FMR P M N |]
  ==> inj_on (tau R M N P) (M ×c N)

lemma tau_inj1:

  [| zeroring R; Module M R; Module N R; FMR P M N |]
  ==> inj_on (tau R M N P) (M ×c N)

lemma tau_inj:

  [| Module M R; Module N R; FMR P M N |] ==> inj_on (tau R M N P) (M ×c N)

lemma tau_mpj_bilinear:

  [| Module M R; Module N R; FMR P M N |]
  ==> bilinear_map (compose (M ×c N) (mpj P (TRR M N P)) (tau R M N P)) R M N
       (M P\<Otimes>R N)

lemma tnm_bilinear:

  [| Module M R; Module N R; FMR P M N |]
  ==> bilinear_map (tnm R P M N) R M N (M P\<Otimes>R N)

lemma tnm_mem:

  [| Module M R; Module N R; FMR P M N; m ∈ carrier M; n ∈ carrier N |]
  ==> tnm R P M N (m, n) ∈ carrier (M P\<Otimes>R N)

lemma tensor_univ_propTr:

  [| Module M R; Module N R; FMR P M N; Module Z R; bilinear_map f R M N Z |]
  ==> ∃g. g ∈ mHom R P Zcompose (M ×c N) g (tau R M N P) = f

lemma tensor_univ_propTr1:

  [| Module M R; Module N R; FMR P M N; Module Z R; bilinear_map f R M N Z |]
  ==> ∃!g. g ∈ mHom R (M P\<Otimes>R N) Zcompose (M ×c N) g (tnm R P M N) = f

lemma tensor_universal_property:

  [| Module M R; Module N R; FMR P M N |]
  ==> universal_property R M N (M P\<Otimes>R N) (tnm R P M N)