# Theory Group

theory Group
imports Complete_Lattice
(*  Title:      HOL/Algebra/Group.thy
Author:     Clemens Ballarin, started 4 February 2003

Based on work by Florian Kammueller, L C Paulson and Markus Wenzel.
With additional contributions from Martin Baillon and Paulo EmÃ­lio de Vilhena.
*)

theory Group
imports Complete_Lattice "HOL-Library.FuncSet"
begin

section ‹Monoids and Groups›

subsection ‹Definitions›

text ‹
›

record 'a monoid =  "'a partial_object" +
mult    :: "['a, 'a] ⇒ 'a" (infixl "⊗ı" 70)
one     :: 'a ("𝟭ı")

definition
m_inv :: "('a, 'b) monoid_scheme => 'a => 'a" ("invı _" [81] 80)
where "inv⇘G⇙ x = (THE y. y ∈ carrier G ∧ x ⊗⇘G⇙ y = 𝟭⇘G⇙ ∧ y ⊗⇘G⇙ x = 𝟭⇘G⇙)"

definition
Units :: "_ => 'a set"
― ‹The set of invertible elements›
where "Units G = {y. y ∈ carrier G ∧ (∃x ∈ carrier G. x ⊗⇘G⇙ y = 𝟭⇘G⇙ ∧ y ⊗⇘G⇙ x = 𝟭⇘G⇙)}"

consts
pow :: "[('a, 'm) monoid_scheme, 'a, 'b::semiring_1] => 'a"  (infixr "[^]ı" 75)

begin
definition "nat_pow G a n = rec_nat 𝟭⇘G⇙ (%u b. b ⊗⇘G⇙ a) n"
end

begin
definition "int_pow G a z =
(let p = rec_nat 𝟭⇘G⇙ (%u b. b ⊗⇘G⇙ a)
in if z < 0 then inv⇘G⇙ (p (nat (-z))) else p (nat z))"
end

lemma int_pow_int: "x [^]⇘G⇙ (int n) = x [^]⇘G⇙ n"

locale monoid =
fixes G (structure)
assumes m_closed [intro, simp]:
"⟦x ∈ carrier G; y ∈ carrier G⟧ ⟹ x ⊗ y ∈ carrier G"
and m_assoc:
"⟦x ∈ carrier G; y ∈ carrier G; z ∈ carrier G⟧
⟹ (x ⊗ y) ⊗ z = x ⊗ (y ⊗ z)"
and one_closed [intro, simp]: "𝟭 ∈ carrier G"
and l_one [simp]: "x ∈ carrier G ⟹ 𝟭 ⊗ x = x"
and r_one [simp]: "x ∈ carrier G ⟹ x ⊗ 𝟭 = x"

lemma monoidI:
fixes G (structure)
assumes m_closed:
"!!x y. [| x ∈ carrier G; y ∈ carrier G |] ==> x ⊗ y ∈ carrier G"
and one_closed: "𝟭 ∈ carrier G"
and m_assoc:
"!!x y z. [| x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |] ==>
(x ⊗ y) ⊗ z = x ⊗ (y ⊗ z)"
and l_one: "!!x. x ∈ carrier G ==> 𝟭 ⊗ x = x"
and r_one: "!!x. x ∈ carrier G ==> x ⊗ 𝟭 = x"
shows "monoid G"
by (fast intro!: monoid.intro intro: assms)

lemma (in monoid) Units_closed [dest]:
"x ∈ Units G ==> x ∈ carrier G"
by (unfold Units_def) fast

lemma (in monoid) one_unique:
assumes "u ∈ carrier G"
and "⋀x. x ∈ carrier G ⟹ u ⊗ x = x"
shows "u = 𝟭"
using assms(2)[OF one_closed] r_one[OF assms(1)] by simp

lemma (in monoid) inv_unique:
assumes eq: "y ⊗ x = 𝟭"  "x ⊗ y' = 𝟭"
and G: "x ∈ carrier G"  "y ∈ carrier G"  "y' ∈ carrier G"
shows "y = y'"
proof -
from G eq have "y = y ⊗ (x ⊗ y')" by simp
also from G have "... = (y ⊗ x) ⊗ y'" by (simp add: m_assoc)
also from G eq have "... = y'" by simp
finally show ?thesis .
qed

lemma (in monoid) Units_m_closed [simp, intro]:
assumes x: "x ∈ Units G" and y: "y ∈ Units G"
shows "x ⊗ y ∈ Units G"
proof -
from x obtain x' where x: "x ∈ carrier G" "x' ∈ carrier G" and xinv: "x ⊗ x' = 𝟭" "x' ⊗ x = 𝟭"
unfolding Units_def by fast
from y obtain y' where y: "y ∈ carrier G" "y' ∈ carrier G" and yinv: "y ⊗ y' = 𝟭" "y' ⊗ y = 𝟭"
unfolding Units_def by fast
from x y xinv yinv have "y' ⊗ (x' ⊗ x) ⊗ y = 𝟭" by simp
moreover from x y xinv yinv have "x ⊗ (y ⊗ y') ⊗ x' = 𝟭" by simp
moreover note x y
ultimately show ?thesis unfolding Units_def
by simp (metis m_assoc m_closed)
qed

lemma (in monoid) Units_one_closed [intro, simp]:
"𝟭 ∈ Units G"
by (unfold Units_def) auto

lemma (in monoid) Units_inv_closed [intro, simp]:
"x ∈ Units G ==> inv x ∈ carrier G"
by (metis (mono_tags, lifting) inv_unique the_equality)

lemma (in monoid) Units_l_inv_ex:
"x ∈ Units G ==> ∃y ∈ carrier G. y ⊗ x = 𝟭"
by (unfold Units_def) auto

lemma (in monoid) Units_r_inv_ex:
"x ∈ Units G ==> ∃y ∈ carrier G. x ⊗ y = 𝟭"
by (unfold Units_def) auto

lemma (in monoid) Units_l_inv [simp]:
"x ∈ Units G ==> inv x ⊗ x = 𝟭"
apply (unfold Units_def m_inv_def, simp)
by (metis (mono_tags, lifting) inv_unique the_equality)

lemma (in monoid) Units_r_inv [simp]:
"x ∈ Units G ==> x ⊗ inv x = 𝟭"
by (metis (full_types) Units_closed Units_inv_closed Units_l_inv Units_r_inv_ex inv_unique)

lemma (in monoid) inv_one [simp]:
"inv 𝟭 = 𝟭"
by (metis Units_one_closed Units_r_inv l_one monoid.Units_inv_closed monoid_axioms)

lemma (in monoid) Units_inv_Units [intro, simp]:
"x ∈ Units G ==> inv x ∈ Units G"
proof -
assume x: "x ∈ Units G"
show "inv x ∈ Units G"
intro: Units_l_inv Units_r_inv x Units_closed [OF x])
qed

lemma (in monoid) Units_l_cancel [simp]:
"[| x ∈ Units G; y ∈ carrier G; z ∈ carrier G |] ==>
(x ⊗ y = x ⊗ z) = (y = z)"
proof
assume eq: "x ⊗ y = x ⊗ z"
and G: "x ∈ Units G"  "y ∈ carrier G"  "z ∈ carrier G"
then have "(inv x ⊗ x) ⊗ y = (inv x ⊗ x) ⊗ z"
by (simp add: m_assoc Units_closed del: Units_l_inv)
with G show "y = z" by simp
next
assume eq: "y = z"
and G: "x ∈ Units G"  "y ∈ carrier G"  "z ∈ carrier G"
then show "x ⊗ y = x ⊗ z" by simp
qed

lemma (in monoid) Units_inv_inv [simp]:
"x ∈ Units G ==> inv (inv x) = x"
proof -
assume x: "x ∈ Units G"
then have "inv x ⊗ inv (inv x) = inv x ⊗ x" by simp
with x show ?thesis by (simp add: Units_closed del: Units_l_inv Units_r_inv)
qed

lemma (in monoid) inv_inj_on_Units:
"inj_on (m_inv G) (Units G)"
proof (rule inj_onI)
fix x y
assume G: "x ∈ Units G"  "y ∈ Units G" and eq: "inv x = inv y"
then have "inv (inv x) = inv (inv y)" by simp
with G show "x = y" by simp
qed

lemma (in monoid) Units_inv_comm:
assumes inv: "x ⊗ y = 𝟭"
and G: "x ∈ Units G"  "y ∈ Units G"
shows "y ⊗ x = 𝟭"
proof -
from G have "x ⊗ y ⊗ x = x ⊗ 𝟭" by (auto simp add: inv Units_closed)
with G show ?thesis by (simp del: r_one add: m_assoc Units_closed)
qed

lemma (in monoid) carrier_not_empty: "carrier G ≠ {}"
by auto

text ‹Power›

lemma (in monoid) nat_pow_closed [intro, simp]:
"x ∈ carrier G ==> x [^] (n::nat) ∈ carrier G"
by (induct n) (simp_all add: nat_pow_def)

lemma (in monoid) nat_pow_0 [simp]:
"x [^] (0::nat) = 𝟭"

lemma (in monoid) nat_pow_Suc [simp]:
"x [^] (Suc n) = x [^] n ⊗ x"

lemma (in monoid) nat_pow_one [simp]:
"𝟭 [^] (n::nat) = 𝟭"
by (induct n) simp_all

lemma (in monoid) nat_pow_mult:
"x ∈ carrier G ==> x [^] (n::nat) ⊗ x [^] m = x [^] (n + m)"
by (induct m) (simp_all add: m_assoc [THEN sym])

lemma (in monoid) nat_pow_comm:
"x ∈ carrier G ⟹ (x [^] (n::nat)) ⊗ (x [^] (m :: nat)) = (x [^] m) ⊗ (x [^] n)"
using nat_pow_mult[of x n m] nat_pow_mult[of x m n] by (simp add: add.commute)

lemma (in monoid) nat_pow_Suc2:
"x ∈ carrier G ⟹ x [^] (Suc n) = x ⊗ (x [^] n)"
using nat_pow_mult[of x 1 n] Suc_eq_plus1[of n]
by (metis One_nat_def Suc_eq_plus1_left l_one nat.rec(1) nat_pow_Suc nat_pow_def)

lemma (in monoid) nat_pow_pow:
"x ∈ carrier G ==> (x [^] n) [^] m = x [^] (n * m::nat)"

lemma (in monoid) nat_pow_consistent:
"x [^] (n :: nat) = x [^]⇘(G ⦇ carrier := H ⦈)⇙ n"
unfolding nat_pow_def by simp

(* Jacobson defines submonoid here. *)
(* Jacobson defines the order of a monoid here. *)

subsection ‹Groups›

text ‹
A group is a monoid all of whose elements are invertible.
›

locale group = monoid +
assumes Units: "carrier G <= Units G"

lemma (in group) is_group: "group G" by (rule group_axioms)

theorem groupI:
fixes G (structure)
assumes m_closed [simp]:
"!!x y. [| x ∈ carrier G; y ∈ carrier G |] ==> x ⊗ y ∈ carrier G"
and one_closed [simp]: "𝟭 ∈ carrier G"
and m_assoc:
"!!x y z. [| x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |] ==>
(x ⊗ y) ⊗ z = x ⊗ (y ⊗ z)"
and l_one [simp]: "!!x. x ∈ carrier G ==> 𝟭 ⊗ x = x"
and l_inv_ex: "!!x. x ∈ carrier G ==> ∃y ∈ carrier G. y ⊗ x = 𝟭"
shows "group G"
proof -
have l_cancel [simp]:
"!!x y z. [| x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |] ==>
(x ⊗ y = x ⊗ z) = (y = z)"
proof
fix x y z
assume eq: "x ⊗ y = x ⊗ z"
and G: "x ∈ carrier G"  "y ∈ carrier G"  "z ∈ carrier G"
with l_inv_ex obtain x_inv where xG: "x_inv ∈ carrier G"
and l_inv: "x_inv ⊗ x = 𝟭" by fast
from G eq xG have "(x_inv ⊗ x) ⊗ y = (x_inv ⊗ x) ⊗ z"
with G show "y = z" by (simp add: l_inv)
next
fix x y z
assume eq: "y = z"
and G: "x ∈ carrier G"  "y ∈ carrier G"  "z ∈ carrier G"
then show "x ⊗ y = x ⊗ z" by simp
qed
have r_one:
"!!x. x ∈ carrier G ==> x ⊗ 𝟭 = x"
proof -
fix x
assume x: "x ∈ carrier G"
with l_inv_ex obtain x_inv where xG: "x_inv ∈ carrier G"
and l_inv: "x_inv ⊗ x = 𝟭" by fast
from x xG have "x_inv ⊗ (x ⊗ 𝟭) = x_inv ⊗ x"
by (simp add: m_assoc [symmetric] l_inv)
with x xG show "x ⊗ 𝟭 = x" by simp
qed
have inv_ex:
"⋀x. x ∈ carrier G ⟹ ∃y ∈ carrier G. y ⊗ x = 𝟭 ∧ x ⊗ y = 𝟭"
proof -
fix x
assume x: "x ∈ carrier G"
with l_inv_ex obtain y where y: "y ∈ carrier G"
and l_inv: "y ⊗ x = 𝟭" by fast
from x y have "y ⊗ (x ⊗ y) = y ⊗ 𝟭"
by (simp add: m_assoc [symmetric] l_inv r_one)
with x y have r_inv: "x ⊗ y = 𝟭"
by simp
from x y show "∃y ∈ carrier G. y ⊗ x = 𝟭 ∧ x ⊗ y = 𝟭"
by (fast intro: l_inv r_inv)
qed
then have carrier_subset_Units: "carrier G ⊆ Units G"
by (unfold Units_def) fast
show ?thesis
by standard (auto simp: r_one m_assoc carrier_subset_Units)
qed

lemma (in monoid) group_l_invI:
assumes l_inv_ex:
"!!x. x ∈ carrier G ==> ∃y ∈ carrier G. y ⊗ x = 𝟭"
shows "group G"
by (rule groupI) (auto intro: m_assoc l_inv_ex)

lemma (in group) Units_eq [simp]:
"Units G = carrier G"
proof
show "Units G ⊆ carrier G" by fast
next
show "carrier G ⊆ Units G" by (rule Units)
qed

lemma (in group) inv_closed [intro, simp]:
"x ∈ carrier G ==> inv x ∈ carrier G"
using Units_inv_closed by simp

lemma (in group) l_inv_ex [simp]:
"x ∈ carrier G ==> ∃y ∈ carrier G. y ⊗ x = 𝟭"
using Units_l_inv_ex by simp

lemma (in group) r_inv_ex [simp]:
"x ∈ carrier G ==> ∃y ∈ carrier G. x ⊗ y = 𝟭"
using Units_r_inv_ex by simp

lemma (in group) l_inv [simp]:
"x ∈ carrier G ==> inv x ⊗ x = 𝟭"
by simp

subsection ‹Cancellation Laws and Basic Properties›

lemma (in group) r_inv [simp]:
"x ∈ carrier G ==> x ⊗ inv x = 𝟭"
by simp

lemma (in group) right_cancel [simp]:
"[| x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |] ==>
(y ⊗ x = z ⊗ x) = (y = z)"
by (metis inv_closed m_assoc r_inv r_one)

lemma (in group) inv_inv [simp]:
"x ∈ carrier G ==> inv (inv x) = x"
using Units_inv_inv by simp

lemma (in group) inv_inj:
"inj_on (m_inv G) (carrier G)"
using inv_inj_on_Units by simp

lemma (in group) inv_mult_group:
"[| x ∈ carrier G; y ∈ carrier G |] ==> inv (x ⊗ y) = inv y ⊗ inv x"
proof -
assume G: "x ∈ carrier G"  "y ∈ carrier G"
then have "inv (x ⊗ y) ⊗ (x ⊗ y) = (inv y ⊗ inv x) ⊗ (x ⊗ y)"
with G show ?thesis by (simp del: l_inv Units_l_inv)
qed

lemma (in group) inv_comm:
"[| x ⊗ y = 𝟭; x ∈ carrier G; y ∈ carrier G |] ==> y ⊗ x = 𝟭"
by (rule Units_inv_comm) auto

lemma (in group) inv_equality:
"[|y ⊗ x = 𝟭; x ∈ carrier G; y ∈ carrier G|] ==> inv x = y"
using inv_unique r_inv by blast

(* Contributed by Joachim Breitner *)
lemma (in group) inv_solve_left:
"⟦ a ∈ carrier G; b ∈ carrier G; c ∈ carrier G ⟧ ⟹ a = inv b ⊗ c ⟷ c = b ⊗ a"
by (metis inv_equality l_inv_ex l_one m_assoc r_inv)
lemma (in group) inv_solve_right:
"⟦ a ∈ carrier G; b ∈ carrier G; c ∈ carrier G ⟧ ⟹ a = b ⊗ inv c ⟷ b = a ⊗ c"
by (metis inv_equality l_inv_ex l_one m_assoc r_inv)

text ‹Power›

lemma (in group) int_pow_def2:
"a [^] (z::int) = (if z < 0 then inv (a [^] (nat (-z))) else a [^] (nat z))"
by (simp add: int_pow_def nat_pow_def Let_def)

lemma (in group) int_pow_0 [simp]:
"x [^] (0::int) = 𝟭"

lemma (in group) int_pow_one [simp]:
"𝟭 [^] (z::int) = 𝟭"

(* The following are contributed by Joachim Breitner *)

lemma (in group) int_pow_closed [intro, simp]:
"x ∈ carrier G ==> x [^] (i::int) ∈ carrier G"

lemma (in group) int_pow_1 [simp]:
"x ∈ carrier G ⟹ x [^] (1::int) = x"

lemma (in group) int_pow_neg:
"x ∈ carrier G ⟹ x [^] (-i::int) = inv (x [^] i)"

lemma (in group) int_pow_mult:
assumes "x ∈ carrier G" shows "x [^] (i + j::int) = x [^] i ⊗ x [^] j"
proof -
have [simp]: "-i - j = -j - i" by simp
show ?thesis
by (auto simp add: assms int_pow_def2 inv_solve_left inv_solve_right nat_add_distrib [symmetric] nat_pow_mult )
qed

lemma (in group) nat_pow_inv:
assumes "x ∈ carrier G" shows "(inv x) [^] (i :: nat) = inv (x [^] i)"
proof (induction i)
case 0 thus ?case by simp
next
case (Suc i)
have "(inv x) [^] Suc i = ((inv x) [^] i) ⊗ inv x"
by simp
also have " ... = (inv (x [^] i)) ⊗ inv x"
also have " ... = inv (x ⊗ (x [^] i))"
also have " ... = inv (x [^] (Suc i))"
using assms nat_pow_Suc2 by auto
finally show ?case .
qed

lemma (in group) int_pow_inv:
"x ∈ carrier G ⟹ (inv x) [^] (i :: int) = inv (x [^] i)"

lemma (in group) int_pow_pow:
assumes "x ∈ carrier G"
shows "(x [^] (n :: int)) [^] (m :: int) = x [^] (n * m :: int)"
proof (cases)
assume n_ge: "n ≥ 0" thus ?thesis
proof (cases)
assume m_ge: "m ≥ 0" thus ?thesis
using n_ge nat_pow_pow[OF assms, of "nat n" "nat m"] int_pow_def2
next
assume m_lt: "¬ m ≥ 0"
with n_ge show ?thesis
by (metis assms mult_minus_right n_ge nat_mult_distrib nat_pow_pow)
qed
next
assume n_lt: "¬ n ≥ 0" thus ?thesis
proof (cases)
assume m_ge: "m ≥ 0"
have "inv x [^] (nat m * nat (- n)) = inv x [^] nat (- (m * n))"
by (metis (full_types) m_ge mult_minus_right nat_mult_distrib)
with m_ge n_lt show ?thesis
by (simp add: int_pow_def2 mult_less_0_iff assms mult.commute nat_pow_inv nat_pow_pow)
next
assume m_lt: "¬ m ≥ 0" thus ?thesis
using n_lt by (auto simp: int_pow_def2 mult_less_0_iff assms nat_mult_distrib_neg nat_pow_inv nat_pow_pow)
qed
qed

lemma (in group) int_pow_diff:
"x ∈ carrier G ⟹ x [^] (n - m :: int) = x [^] n ⊗ inv (x [^] m)"

lemma (in group) inj_on_multc: "c ∈ carrier G ⟹ inj_on (λx. x ⊗ c) (carrier G)"

lemma (in group) inj_on_cmult: "c ∈ carrier G ⟹ inj_on (λx. c ⊗ x) (carrier G)"

(*Following subsection contributed by Martin Baillon*)
subsection ‹Submonoids›

locale submonoid =
fixes H and G (structure)
assumes subset: "H ⊆ carrier G"
and m_closed [intro, simp]: "⟦x ∈ H; y ∈ H⟧ ⟹ x ⊗ y ∈ H"
and one_closed [simp]: "𝟭 ∈ H"

lemma (in submonoid) is_submonoid:
"submonoid H G" by (rule submonoid_axioms)

lemma (in submonoid) mem_carrier [simp]:
"x ∈ H ⟹ x ∈ carrier G"
using subset by blast

lemma (in submonoid) submonoid_is_monoid [intro]:
assumes "monoid G"
shows "monoid (G⦇carrier := H⦈)"
proof -
interpret monoid G by fact
show ?thesis
qed

lemma submonoid_nonempty:
"~ submonoid {} G"
by (blast dest: submonoid.one_closed)

lemma (in submonoid) finite_monoid_imp_card_positive:
"finite (carrier G) ==> 0 < card H"
proof (rule classical)
assume "finite (carrier G)" and a: "~ 0 < card H"
then have "finite H" by (blast intro: finite_subset [OF subset])
with is_submonoid a have "submonoid {} G" by simp
with submonoid_nonempty show ?thesis by contradiction
qed

lemma (in monoid) monoid_incl_imp_submonoid :
assumes "H ⊆ carrier G"
and "monoid (G⦇carrier := H⦈)"
shows "submonoid H G"
proof (intro submonoid.intro[OF assms(1)])
have ab_eq : "⋀ a b. a ∈ H ⟹ b ∈ H ⟹ a ⊗⇘G⦇carrier := H⦈⇙ b = a ⊗ b" using assms by simp
have "⋀a b. a ∈ H ⟹ b ∈ H ⟹ a ⊗ b ∈ carrier (G⦇carrier := H⦈) "
using assms ab_eq unfolding group_def using monoid.m_closed by fastforce
thus "⋀a b. a ∈ H ⟹ b ∈ H ⟹ a ⊗ b ∈ H" by simp
show "𝟭 ∈ H " using monoid.one_closed[OF assms(2)] assms by simp
qed

lemma (in monoid) inv_unique':
assumes "x ∈ carrier G" "y ∈ carrier G"
shows "⟦ x ⊗ y = 𝟭; y ⊗ x = 𝟭 ⟧ ⟹ y = inv x"
proof -
assume "x ⊗ y = 𝟭" and l_inv: "y ⊗ x = 𝟭"
hence unit: "x ∈ Units G"
using assms unfolding Units_def by auto
show "y = inv x"
using inv_unique[OF l_inv Units_r_inv[OF unit] assms Units_inv_closed[OF unit]] .
qed

lemma (in monoid) m_inv_monoid_consistent: (* contributed by Paulo *)
assumes "x ∈ Units (G ⦇ carrier := H ⦈)" and "submonoid H G"
shows "inv⇘(G ⦇ carrier := H ⦈)⇙ x = inv x"
proof -
have monoid: "monoid (G ⦇ carrier := H ⦈)"
using submonoid.submonoid_is_monoid[OF assms(2) monoid_axioms] .
obtain y where y: "y ∈ H" "x ⊗ y = 𝟭" "y ⊗ x = 𝟭"
using assms(1) unfolding Units_def by auto
have x: "x ∈ H" and in_carrier: "x ∈ carrier G" "y ∈ carrier G"
using y(1) submonoid.subset[OF assms(2)] assms(1) unfolding Units_def by auto
show ?thesis
using monoid.inv_unique'[OF monoid, of x y] x y
using inv_unique'[OF in_carrier y(2-3)] by auto
qed

subsection ‹Subgroups›

locale subgroup =
fixes H and G (structure)
assumes subset: "H ⊆ carrier G"
and m_closed [intro, simp]: "⟦x ∈ H; y ∈ H⟧ ⟹ x ⊗ y ∈ H"
and one_closed [simp]: "𝟭 ∈ H"
and m_inv_closed [intro,simp]: "x ∈ H ⟹ inv x ∈ H"

lemma (in subgroup) is_subgroup:
"subgroup H G" by (rule subgroup_axioms)

declare (in subgroup) group.intro [intro]

lemma (in subgroup) mem_carrier [simp]:
"x ∈ H ⟹ x ∈ carrier G"
using subset by blast

lemma (in subgroup) subgroup_is_group [intro]:
assumes "group G"
shows "group (G⦇carrier := H⦈)"
proof -
interpret group G by fact
have "Group.monoid (G⦇carrier := H⦈)"
by (simp add: monoid_axioms submonoid.intro submonoid.submonoid_is_monoid subset)
then show ?thesis
by (rule monoid.group_l_invI) (auto intro: l_inv mem_carrier)
qed

lemma subgroup_is_submonoid:
assumes "subgroup H G" shows "submonoid H G"
using assms by (auto intro: submonoid.intro simp add: subgroup_def)

lemma (in group) subgroup_Units:
assumes "subgroup H G" shows "H ⊆ Units (G ⦇ carrier := H ⦈)"
using group.Units[OF subgroup.subgroup_is_group[OF assms group_axioms]] by simp

lemma (in group) m_inv_consistent:
assumes "subgroup H G" "x ∈ H"
shows "inv⇘(G ⦇ carrier := H ⦈)⇙ x = inv x"
using assms m_inv_monoid_consistent[OF _ subgroup_is_submonoid] subgroup_Units[of H] by auto

lemma (in group) int_pow_consistent: (* by Paulo *)
assumes "subgroup H G" "x ∈ H"
shows "x [^] (n :: int) = x [^]⇘(G ⦇ carrier := H ⦈)⇙ n"
proof (cases)
assume ge: "n ≥ 0"
hence "x [^] n = x [^] (nat n)"
using int_pow_def2 by auto
also have " ... = x [^]⇘(G ⦇ carrier := H ⦈)⇙ (nat n)"
using nat_pow_consistent by simp
also have " ... = x [^]⇘(G ⦇ carrier := H ⦈)⇙ n"
using group.int_pow_def2[OF subgroup.subgroup_is_group[OF assms(1) is_group]] ge by auto
finally show ?thesis .
next
assume "¬ n ≥ 0" hence lt: "n < 0" by simp
hence "x [^] n = inv (x [^] (nat (- n)))"
using int_pow_def2 by auto
also have " ... = (inv x) [^] (nat (- n))"
by (metis assms nat_pow_inv subgroup.mem_carrier)
also have " ... = (inv⇘(G ⦇ carrier := H ⦈)⇙ x) [^]⇘(G ⦇ carrier := H ⦈)⇙ (nat (- n))"
using m_inv_consistent[OF assms] nat_pow_consistent by auto
also have " ... = inv⇘(G ⦇ carrier := H ⦈)⇙ (x [^]⇘(G ⦇ carrier := H ⦈)⇙ (nat (- n)))"
using group.nat_pow_inv[OF subgroup.subgroup_is_group[OF assms(1) is_group]] assms(2) by auto
also have " ... = x [^]⇘(G ⦇ carrier := H ⦈)⇙ n"
using group.int_pow_def2[OF subgroup.subgroup_is_group[OF assms(1) is_group]] lt by auto
finally show ?thesis .
qed

text ‹
Since @{term H} is nonempty, it contains some element @{term x}.  Since
it is closed under inverse, it contains ‹inv x›.  Since
it is closed under product, it contains ‹x ⊗ inv x = 𝟭›.
›

lemma (in group) one_in_subset:
"[| H ⊆ carrier G; H ≠ {}; ∀a ∈ H. inv a ∈ H; ∀a∈H. ∀b∈H. a ⊗ b ∈ H |]
==> 𝟭 ∈ H"
by force

text ‹A characterization of subgroups: closed, non-empty subset.›

lemma (in group) subgroupI:
assumes subset: "H ⊆ carrier G" and non_empty: "H ≠ {}"
and inv: "!!a. a ∈ H ⟹ inv a ∈ H"
and mult: "!!a b. ⟦a ∈ H; b ∈ H⟧ ⟹ a ⊗ b ∈ H"
shows "subgroup H G"
show "𝟭 ∈ H" by (rule one_in_subset) (auto simp only: assms)
qed

lemma (in group) subgroupE:
assumes "subgroup H G"
shows "H ⊆ carrier G"
and "H ≠ {}"
and "⋀a. a ∈ H ⟹ inv a ∈ H"
and "⋀a b. ⟦ a ∈ H; b ∈ H ⟧ ⟹ a ⊗ b ∈ H"
using assms unfolding subgroup_def[of H G] by auto

declare monoid.one_closed [iff] group.inv_closed [simp]
monoid.l_one [simp] monoid.r_one [simp] group.inv_inv [simp]

lemma subgroup_nonempty:
"¬ subgroup {} G"
by (blast dest: subgroup.one_closed)

lemma (in subgroup) finite_imp_card_positive: "finite (carrier G) ⟹ 0 < card H"
using subset one_closed card_gt_0_iff finite_subset by blast

(*Following 3 lemmas contributed by Martin Baillon*)

lemma (in subgroup) subgroup_is_submonoid :
"submonoid H G"

lemma (in group) submonoid_subgroupI :
assumes "submonoid H G"
and "⋀a. a ∈ H ⟹ inv a ∈ H"
shows "subgroup H G"
by (metis assms subgroup_def submonoid_def)

lemma (in group) group_incl_imp_subgroup:
assumes "H ⊆ carrier G"
and "group (G⦇carrier := H⦈)"
shows "subgroup H G"
proof (intro submonoid_subgroupI[OF monoid_incl_imp_submonoid[OF assms(1)]])
show "monoid (G⦇carrier := H⦈)" using group_def assms by blast
have ab_eq : "⋀ a b. a ∈ H ⟹ b ∈ H ⟹ a ⊗⇘G⦇carrier := H⦈⇙ b = a ⊗ b" using assms by simp
fix a  assume aH : "a ∈ H"
have " inv⇘G⦇carrier := H⦈⇙ a ∈ carrier G"
using assms aH group.inv_closed[OF assms(2)] by auto
moreover have "𝟭⇘G⦇carrier := H⦈⇙ = 𝟭" using assms monoid.one_closed ab_eq one_def by simp
hence "a ⊗⇘G⦇carrier := H⦈⇙ inv⇘G⦇carrier := H⦈⇙ a= 𝟭"
using assms ab_eq aH  group.r_inv[OF assms(2)] by simp
hence "a ⊗ inv⇘G⦇carrier := H⦈⇙ a= 𝟭"
using aH assms group.inv_closed[OF assms(2)] ab_eq by simp
ultimately have "inv⇘G⦇carrier := H⦈⇙ a = inv a"
by (metis aH assms(1) contra_subsetD group.inv_inv is_group local.inv_equality)
moreover have "inv⇘G⦇carrier := H⦈⇙ a ∈ H"
using aH group.inv_closed[OF assms(2)] by auto
ultimately show "inv a ∈ H" by auto
qed

subsection ‹Direct Products›

definition
DirProd :: "_ ⇒ _ ⇒ ('a × 'b) monoid" (infixr "××" 80) where
"G ×× H =
⦇carrier = carrier G × carrier H,
mult = (λ(g, h) (g', h'). (g ⊗⇘G⇙ g', h ⊗⇘H⇙ h')),
one = (𝟭⇘G⇙, 𝟭⇘H⇙)⦈"

lemma DirProd_monoid:
assumes "monoid G" and "monoid H"
shows "monoid (G ×× H)"
proof -
interpret G: monoid G by fact
interpret H: monoid H by fact
from assms
show ?thesis by (unfold monoid_def DirProd_def, auto)
qed

text‹Does not use the previous result because it's easier just to use auto.›
lemma DirProd_group:
assumes "group G" and "group H"
shows "group (G ×× H)"
proof -
interpret G: group G by fact
interpret H: group H by fact
show ?thesis by (rule groupI)
(auto intro: G.m_assoc H.m_assoc G.l_inv H.l_inv
qed

lemma carrier_DirProd [simp]: "carrier (G ×× H) = carrier G × carrier H"

lemma one_DirProd [simp]: "𝟭⇘G ×× H⇙ = (𝟭⇘G⇙, 𝟭⇘H⇙)"

lemma mult_DirProd [simp]: "(g, h) ⊗⇘(G ×× H)⇙ (g', h') = (g ⊗⇘G⇙ g', h ⊗⇘H⇙ h')"

lemma DirProd_assoc: "(G ×× H ×× I) = (G ×× (H ×× I))"
by auto

lemma inv_DirProd [simp]:
assumes "group G" and "group H"
assumes g: "g ∈ carrier G"
and h: "h ∈ carrier H"
shows "m_inv (G ×× H) (g, h) = (inv⇘G⇙ g, inv⇘H⇙ h)"
proof -
interpret G: group G by fact
interpret H: group H by fact
interpret Prod: group "G ×× H"
by (auto intro: DirProd_group group.intro group.axioms assms)
show ?thesis by (simp add: Prod.inv_equality g h)
qed

lemma DirProd_subgroups :
assumes "group G"
and "subgroup H G"
and "group K"
and "subgroup I K"
shows "subgroup (H × I) (G ×× K)"
proof (intro group.group_incl_imp_subgroup[OF DirProd_group[OF assms(1)assms(3)]])
have "H ⊆ carrier G" "I ⊆ carrier K" using subgroup.subset assms apply blast+.
thus "(H × I) ⊆ carrier (G ×× K)" unfolding DirProd_def by auto
have "Group.group ((G⦇carrier := H⦈) ×× (K⦇carrier := I⦈))"
using DirProd_group[OF subgroup.subgroup_is_group[OF assms(2)assms(1)]
subgroup.subgroup_is_group[OF assms(4)assms(3)]].
moreover have "((G⦇carrier := H⦈) ×× (K⦇carrier := I⦈)) = ((G ×× K)⦇carrier := H × I⦈)"
unfolding DirProd_def using assms apply simp.
ultimately show "Group.group ((G ×× K)⦇carrier := H × I⦈)" by simp
qed

subsection ‹Homomorphisms and Isomorphisms›

definition
hom :: "_ => _ => ('a => 'b) set" where
"hom G H =
{h. h ∈ carrier G → carrier H ∧
(∀x ∈ carrier G. ∀y ∈ carrier G. h (x ⊗⇘G⇙ y) = h x ⊗⇘H⇙ h y)}"

lemma (in group) hom_compose:
"[|h ∈ hom G H; i ∈ hom H I|] ==> compose (carrier G) i h ∈ hom G I"
by (fastforce simp add: hom_def compose_def)

definition
iso :: "_ => _ => ('a => 'b) set"
where "iso G H = {h. h ∈ hom G H ∧ bij_betw h (carrier G) (carrier H)}"

definition
is_iso :: "_ ⇒ _ ⇒ bool" (infixr "≅" 60)
where "G ≅ H = (iso G H  ≠ {})"

lemma iso_set_refl: "(λx. x) ∈ iso G G"
by (simp add: iso_def hom_def inj_on_def bij_betw_def Pi_def)

corollary iso_refl : "G ≅ G"
using iso_set_refl unfolding is_iso_def by auto

lemma (in group) iso_set_sym:
assumes "h ∈ iso G H"
shows "inv_into (carrier G) h ∈ iso H G"
proof -
have h: "h ∈ hom G H" "bij_betw h (carrier G) (carrier H)"
using assms by (auto simp add: iso_def bij_betw_inv_into)
then have HG: "bij_betw (inv_into (carrier G) h) (carrier H) (carrier G)"
have "inv_into (carrier G) h ∈ hom H G"
unfolding hom_def
proof safe
show *: "⋀x. x ∈ carrier H ⟹ inv_into (carrier G) h x ∈ carrier G"
by (meson HG bij_betwE)
show "inv_into (carrier G) h (x ⊗⇘H⇙ y) = inv_into (carrier G) h x ⊗ inv_into (carrier G) h y"
if "x ∈ carrier H" "y ∈ carrier H" for x y
proof (rule inv_into_f_eq)
show "inj_on h (carrier G)"
using bij_betw_def h(2) by blast
show "inv_into (carrier G) h x ⊗ inv_into (carrier G) h y ∈ carrier G"
show "h (inv_into (carrier G) h x ⊗ inv_into (carrier G) h y) = x ⊗⇘H⇙ y"
using h bij_betw_inv_into_right [of h] unfolding hom_def by (simp add: "*" that)
qed
qed
then show ?thesis
by (simp add: Group.iso_def bij_betw_inv_into h)
qed

corollary (in group) iso_sym: "G ≅ H ⟹ H ≅ G"
using iso_set_sym unfolding is_iso_def by auto

lemma (in group) iso_set_trans:
"[|h ∈ iso G H; i ∈ iso H I|] ==> (compose (carrier G) i h) ∈ iso G I"
by (auto simp add: iso_def hom_compose bij_betw_compose)

corollary (in group) iso_trans: "⟦G ≅ H ; H ≅ I⟧ ⟹ G ≅ I"
using iso_set_trans unfolding is_iso_def by blast

(* Next four lemmas contributed by Paulo. *)

lemma (in monoid) hom_imp_img_monoid:
assumes "h ∈ hom G H"
shows "monoid (H ⦇ carrier := h  (carrier G), one := h 𝟭⇘G⇙ ⦈)" (is "monoid ?h_img")
proof (rule monoidI)
show "𝟭⇘?h_img⇙ ∈ carrier ?h_img"
by auto
next
fix x y z assume "x ∈ carrier ?h_img" "y ∈ carrier ?h_img" "z ∈ carrier ?h_img"
then obtain g1 g2 g3
where g1: "g1 ∈ carrier G" "x = h g1"
and g2: "g2 ∈ carrier G" "y = h g2"
and g3: "g3 ∈ carrier G" "z = h g3"
using image_iff[where ?f = h and ?A = "carrier G"] by auto
have aux_lemma:
"⋀a b. ⟦ a ∈ carrier G; b ∈ carrier G ⟧ ⟹ h a ⊗⇘(?h_img)⇙ h b = h (a ⊗ b)"
using assms unfolding hom_def by auto

show "x ⊗⇘(?h_img)⇙ 𝟭⇘(?h_img)⇙ = x"
using aux_lemma[OF g1(1) one_closed] g1(2) r_one[OF g1(1)] by simp

show "𝟭⇘(?h_img)⇙ ⊗⇘(?h_img)⇙ x = x"
using aux_lemma[OF one_closed g1(1)] g1(2) l_one[OF g1(1)] by simp

have "x ⊗⇘(?h_img)⇙ y = h (g1 ⊗ g2)"
using aux_lemma g1 g2 by auto
thus "x ⊗⇘(?h_img)⇙ y ∈ carrier ?h_img"
using g1(1) g2(1) by simp

have "(x ⊗⇘(?h_img)⇙ y) ⊗⇘(?h_img)⇙ z = h ((g1 ⊗ g2) ⊗ g3)"
using aux_lemma g1 g2 g3 by auto
also have " ... = h (g1 ⊗ (g2 ⊗ g3))"
using m_assoc[OF g1(1) g2(1) g3(1)] by simp
also have " ... = x ⊗⇘(?h_img)⇙ (y ⊗⇘(?h_img)⇙ z)"
using aux_lemma g1 g2 g3 by auto
finally show "(x ⊗⇘(?h_img)⇙ y) ⊗⇘(?h_img)⇙ z = x ⊗⇘(?h_img)⇙ (y ⊗⇘(?h_img)⇙ z)" .
qed

lemma (in group) hom_imp_img_group:
assumes "h ∈ hom G H"
shows "group (H ⦇ carrier := h  (carrier G), one := h 𝟭⇘G⇙ ⦈)" (is "group ?h_img")
proof -
interpret monoid ?h_img
using hom_imp_img_monoid[OF assms] .

show ?thesis
proof (unfold_locales)
show "carrier ?h_img ⊆ Units ?h_img"
have aux_lemma:
"⋀g1 g2. ⟦ g1 ∈ carrier G; g2 ∈ carrier G ⟧ ⟹ h g1 ⊗⇘H⇙ h g2 = h (g1 ⊗ g2)"
using assms unfolding hom_def by auto

fix g1 assume g1: "g1 ∈ carrier G"
thus "∃g2 ∈ carrier G. (h g2) ⊗⇘H⇙ (h g1) = h 𝟭 ∧ (h g1) ⊗⇘H⇙ (h g2) = h 𝟭"
using aux_lemma[OF g1 inv_closed[OF g1]]
aux_lemma[OF inv_closed[OF g1] g1]
inv_closed by auto
qed
qed
qed

lemma (in group) iso_imp_group:
assumes "G ≅ H" and "monoid H"
shows "group H"
proof -
obtain φ where phi: "φ ∈ iso G H" "inv_into (carrier G) φ ∈ iso H G"
using iso_set_sym assms unfolding is_iso_def by blast
define ψ where psi_def: "ψ = inv_into (carrier G) φ"

have surj: "φ  (carrier G) = (carrier H)" "ψ  (carrier H) = (carrier G)"
and inj: "inj_on φ (carrier G)" "inj_on ψ (carrier H)"
and phi_hom: "⋀g1 g2. ⟦ g1 ∈ carrier G; g2 ∈ carrier G ⟧ ⟹ φ (g1 ⊗ g2) = (φ g1) ⊗⇘H⇙ (φ g2)"
and psi_hom: "⋀h1 h2. ⟦ h1 ∈ carrier H; h2 ∈ carrier H ⟧ ⟹ ψ (h1 ⊗⇘H⇙ h2) = (ψ h1) ⊗ (ψ h2)"
using phi psi_def unfolding iso_def bij_betw_def hom_def by auto

have phi_one: "φ 𝟭 = 𝟭⇘H⇙"
proof -
have "(φ 𝟭) ⊗⇘H⇙ 𝟭⇘H⇙ = (φ 𝟭) ⊗⇘H⇙ (φ 𝟭)"
by (metis assms(2) image_eqI monoid.r_one one_closed phi_hom r_one surj(1))
thus ?thesis
by (metis (no_types, hide_lams) Units_eq Units_one_closed assms(2) f_inv_into_f imageI
monoid.l_one monoid.one_closed phi_hom psi_def r_one surj)
qed

have "carrier H ⊆ Units H"
proof
fix h assume h: "h ∈ carrier H"
let ?inv_h = "φ (inv (ψ h))"
have "h ⊗⇘H⇙ ?inv_h = φ (ψ h) ⊗⇘H⇙ ?inv_h"
by (simp add: f_inv_into_f h psi_def surj(1))
also have " ... = φ ((ψ h) ⊗ inv (ψ h))"
by (metis h imageI inv_closed phi_hom surj(2))
also have " ... = φ 𝟭"
by (simp add: h inv_into_into psi_def surj(1))
finally have 1: "h ⊗⇘H⇙ ?inv_h = 𝟭⇘H⇙"
using phi_one by simp

have "?inv_h ⊗⇘H⇙ h = ?inv_h ⊗⇘H⇙ φ (ψ h)"
by (simp add: f_inv_into_f h psi_def surj(1))
also have " ... = φ (inv (ψ h) ⊗ (ψ h))"
by (metis h imageI inv_closed phi_hom surj(2))
also have " ... = φ 𝟭"
by (simp add: h inv_into_into psi_def surj(1))
finally have 2: "?inv_h ⊗⇘H⇙ h = 𝟭⇘H⇙"
using phi_one by simp

thus "h ∈ Units H" unfolding Units_def using 1 2 h surj by fastforce
qed
thus ?thesis unfolding group_def group_axioms_def using assms(2) by simp
qed

corollary (in group) iso_imp_img_group:
assumes "h ∈ iso G H"
shows "group (H ⦇ one := h 𝟭 ⦈)"
proof -
let ?h_img = "H ⦇ carrier := h  (carrier G), one := h 𝟭 ⦈"
have "h ∈ iso G ?h_img"
using assms unfolding iso_def hom_def bij_betw_def by auto
hence "G ≅ ?h_img"
unfolding is_iso_def by auto
hence "group ?h_img"
using iso_imp_group[of ?h_img] hom_imp_img_monoid[of h H] assms unfolding iso_def by simp
moreover have "carrier H = carrier ?h_img"
using assms unfolding iso_def bij_betw_def by simp
hence "H ⦇ one := h 𝟭 ⦈ = ?h_img"
by simp
ultimately show ?thesis by simp
qed

lemma DirProd_commute_iso_set:
shows "(λ(x,y). (y,x)) ∈ iso (G ×× H) (H ×× G)"
by (auto simp add: iso_def hom_def inj_on_def bij_betw_def)

corollary DirProd_commute_iso :
"(G ×× H) ≅ (H ×× G)"
using DirProd_commute_iso_set unfolding is_iso_def by blast

lemma DirProd_assoc_iso_set:
shows "(λ(x,y,z). (x,(y,z))) ∈ iso (G ×× H ×× I) (G ×× (H ×× I))"
by (auto simp add: iso_def hom_def inj_on_def bij_betw_def)

lemma (in group) DirProd_iso_set_trans:
assumes "g ∈ iso G G2"
and "h ∈ iso H I"
shows "(λ(x,y). (g x, h y)) ∈ iso (G ×× H) (G2 ×× I)"
proof-
have "(λ(x,y). (g x, h y)) ∈ hom (G ×× H) (G2 ×× I)"
using assms unfolding iso_def hom_def by auto
moreover have " inj_on (λ(x,y). (g x, h y)) (carrier (G ×× H))"
using assms unfolding iso_def DirProd_def bij_betw_def inj_on_def by auto
moreover have "(λ(x, y). (g x, h y))  carrier (G ×× H) = carrier (G2 ×× I)"
using assms unfolding iso_def bij_betw_def image_def DirProd_def by fastforce
ultimately show "(λ(x,y). (g x, h y)) ∈ iso (G ×× H) (G2 ×× I)"
unfolding iso_def bij_betw_def by auto
qed

corollary (in group) DirProd_iso_trans :
assumes "G ≅ G2" and "H ≅ I"
shows "G ×× H ≅ G2 ×× I"
using DirProd_iso_set_trans assms unfolding is_iso_def by blast

text‹Basis for homomorphism proofs: we assume two groups @{term G} and
@{term H}, with a homomorphism @{term h} between them›
locale group_hom = G?: group G + H?: group H for G (structure) and H (structure) +
fixes h
assumes homh: "h ∈ hom G H"

lemma (in group_hom) hom_mult [simp]:
"[| x ∈ carrier G; y ∈ carrier G |] ==> h (x ⊗⇘G⇙ y) = h x ⊗⇘H⇙ h y"
proof -
assume "x ∈ carrier G" "y ∈ carrier G"
with homh [unfolded hom_def] show ?thesis by simp
qed

lemma (in group_hom) hom_closed [simp]:
"x ∈ carrier G ==> h x ∈ carrier H"
proof -
assume "x ∈ carrier G"
with homh [unfolded hom_def] show ?thesis by auto
qed

lemma (in group_hom) one_closed [simp]: "h 𝟭 ∈ carrier H"
by simp

lemma (in group_hom) hom_one [simp]: "h 𝟭 = 𝟭⇘H⇙"
proof -
have "h 𝟭 ⊗⇘H⇙ 𝟭⇘H⇙ = h 𝟭 ⊗⇘H⇙ h 𝟭"
by (simp add: hom_mult [symmetric] del: hom_mult)
then show ?thesis by (simp del: r_one)
qed

lemma (in group_hom) inv_closed [simp]:
"x ∈ carrier G ==> h (inv x) ∈ carrier H"
by simp

lemma (in group_hom) hom_inv [simp]:
assumes "x ∈ carrier G" shows "h (inv x) = inv⇘H⇙ (h x)"
proof -
have "h x ⊗⇘H⇙ h (inv x) = h x ⊗⇘H⇙ inv⇘H⇙ (h x)"
using assms by (simp flip: hom_mult)
with assms show ?thesis by (simp del: H.r_inv H.Units_r_inv)
qed

(* Contributed by Joachim Breitner *)
lemma (in group) int_pow_is_hom:
"x ∈ carrier G ⟹ (([^]) x) ∈ hom ⦇ carrier = UNIV, mult = (+), one = 0::int ⦈ G "
unfolding hom_def by (simp add: int_pow_mult)

(* Next six lemmas contributed by Paulo. *)

lemma (in group_hom) img_is_subgroup: "subgroup (h  (carrier G)) H"
apply (rule subgroupI)
apply (metis (no_types, hide_lams) G.inv_closed hom_inv image_iff)
by (metis G.monoid_axioms hom_mult image_eqI monoid.m_closed)

lemma (in group_hom) subgroup_img_is_subgroup:
assumes "subgroup I G"
shows "subgroup (h  I) H"
proof -
have "h ∈ hom (G ⦇ carrier := I ⦈) H"
using G.subgroupE[OF assms] subgroup.mem_carrier[OF assms] homh
unfolding hom_def by auto
hence "group_hom (G ⦇ carrier := I ⦈) H h"
using subgroup.subgroup_is_group[OF assms G.is_group] is_group
unfolding group_hom_def group_hom_axioms_def by simp
thus ?thesis
using group_hom.img_is_subgroup[of "G ⦇ carrier := I ⦈" H h] by simp
qed

lemma (in group_hom) induced_group_hom:
assumes "subgroup I G"
shows "group_hom (G ⦇ carrier := I ⦈) (H ⦇ carrier := h  I ⦈) h"
proof -
have "h ∈ hom (G ⦇ carrier := I ⦈) (H ⦇ carrier := h  I ⦈)"
using homh subgroup.mem_carrier[OF assms] unfolding hom_def by auto
thus ?thesis
unfolding group_hom_def group_hom_axioms_def
using subgroup.subgroup_is_group[OF assms G.is_group]
subgroup.subgroup_is_group[OF subgroup_img_is_subgroup[OF assms] is_group] by simp
qed

lemma (in group) canonical_inj_is_hom:
assumes "subgroup H G"
shows "group_hom (G ⦇ carrier := H ⦈) G id"
unfolding group_hom_def group_hom_axioms_def hom_def
using subgroup.subgroup_is_group[OF assms is_group]
is_group subgroup.subset[OF assms] by auto

lemma (in group_hom) nat_pow_hom:
"x ∈ carrier G ⟹ h (x [^] (n :: nat)) = (h x) [^]⇘H⇙ n"
by (induction n) auto

lemma (in group_hom) int_pow_hom:
"x ∈ carrier G ⟹ h (x [^] (n :: int)) = (h x) [^]⇘H⇙ n"
using int_pow_def2 nat_pow_hom by (simp add: G.int_pow_def2)

subsection ‹Commutative Structures›

text ‹
Naming convention: multiplicative structures that are commutative
are called \emph{commutative}, additive structures are called
\emph{Abelian}.
›

locale comm_monoid = monoid +
assumes m_comm: "⟦x ∈ carrier G; y ∈ carrier G⟧ ⟹ x ⊗ y = y ⊗ x"

lemma (in comm_monoid) m_lcomm:
"⟦x ∈ carrier G; y ∈ carrier G; z ∈ carrier G⟧ ⟹
x ⊗ (y ⊗ z) = y ⊗ (x ⊗ z)"
proof -
assume xyz: "x ∈ carrier G"  "y ∈ carrier G"  "z ∈ carrier G"
from xyz have "x ⊗ (y ⊗ z) = (x ⊗ y) ⊗ z" by (simp add: m_assoc)
also from xyz have "... = (y ⊗ x) ⊗ z" by (simp add: m_comm)
also from xyz have "... = y ⊗ (x ⊗ z)" by (simp add: m_assoc)
finally show ?thesis .
qed

lemmas (in comm_monoid) m_ac = m_assoc m_comm m_lcomm

lemma comm_monoidI:
fixes G (structure)
assumes m_closed:
"!!x y. [| x ∈ carrier G; y ∈ carrier G |] ==> x ⊗ y ∈ carrier G"
and one_closed: "𝟭 ∈ carrier G"
and m_assoc:
"!!x y z. [| x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |] ==>
(x ⊗ y) ⊗ z = x ⊗ (y ⊗ z)"
and l_one: "!!x. x ∈ carrier G ==> 𝟭 ⊗ x = x"
and m_comm:
"!!x y. [| x ∈ carrier G; y ∈ carrier G |] ==> x ⊗ y = y ⊗ x"
shows "comm_monoid G"
using l_one
by (auto intro!: comm_monoid.intro comm_monoid_axioms.intro monoid.intro
intro: assms simp: m_closed one_closed m_comm)

lemma (in monoid) monoid_comm_monoidI:
assumes m_comm:
"!!x y. [| x ∈ carrier G; y ∈ carrier G |] ==> x ⊗ y = y ⊗ x"
shows "comm_monoid G"
by (rule comm_monoidI) (auto intro: m_assoc m_comm)

lemma (in comm_monoid) nat_pow_distr:
"[| x ∈ carrier G; y ∈ carrier G |] ==>
(x ⊗ y) [^] (n::nat) = x [^] n ⊗ y [^] n"
by (induct n) (simp, simp add: m_ac)

lemma (in comm_monoid) submonoid_is_comm_monoid :
assumes "submonoid H G"
shows "comm_monoid (G⦇carrier := H⦈)"
proof (intro monoid.monoid_comm_monoidI)
show "monoid (G⦇carrier := H⦈)"
using submonoid.submonoid_is_monoid assms comm_monoid_axioms comm_monoid_def by blast
show "⋀x y. x ∈ carrier (G⦇carrier := H⦈) ⟹ y ∈ carrier (G⦇carrier := H⦈)
⟹ x ⊗⇘G⦇carrier := H⦈⇙ y = y ⊗⇘G⦇carrier := H⦈⇙ x" apply simp
using  assms comm_monoid_axioms_def submonoid.mem_carrier
by (metis m_comm)
qed

locale comm_group = comm_monoid + group

lemma (in group) group_comm_groupI:
assumes m_comm: "!!x y. [| x ∈ carrier G; y ∈ carrier G |] ==> x ⊗ y = y ⊗ x"
shows "comm_group G"

lemma comm_groupI:
fixes G (structure)
assumes m_closed:
"!!x y. [| x ∈ carrier G; y ∈ carrier G |] ==> x ⊗ y ∈ carrier G"
and one_closed: "𝟭 ∈ carrier G"
and m_assoc:
"!!x y z. [| x ∈ carrier G; y ∈ carrier G; z ∈ carrier G |] ==>
(x ⊗ y) ⊗ z = x ⊗ (y ⊗ z)"
and m_comm:
"!!x y. [| x ∈ carrier G; y ∈ carrier G |] ==> x ⊗ y = y ⊗ x"
and l_one: "!!x. x ∈ carrier G ==> 𝟭 ⊗ x = x"
and l_inv_ex: "!!x. x ∈ carrier G ==> ∃y ∈ carrier G. y ⊗ x = 𝟭"
shows "comm_group G"
by (fast intro: group.group_comm_groupI groupI assms)

lemma comm_groupE:
fixes G (structure)
assumes "comm_group G"
shows "⋀x y. ⟦ x ∈ carrier G; y ∈ carrier G ⟧ ⟹ x ⊗ y ∈ carrier G"
and "𝟭 ∈ carrier G"
and "⋀x y z. ⟦ x ∈ carrier G; y ∈ carrier G; z ∈ carrier G ⟧ ⟹ (x ⊗ y) ⊗ z = x ⊗ (y ⊗ z)"
and "⋀x y. ⟦ x ∈ carrier G; y ∈ carrier G ⟧ ⟹ x ⊗ y = y ⊗ x"
and "⋀x. x ∈ carrier G ⟹ 𝟭 ⊗ x = x"
and "⋀x. x ∈ carrier G ⟹ ∃y ∈ carrier G. y ⊗ x = 𝟭"
apply (simp_all add: group.axioms assms comm_group.axioms comm_monoid.m_comm comm_monoid.m_ac(1))
by (simp_all add: Group.group.axioms(1) assms comm_group.axioms(2) monoid.m_closed group.r_inv_ex)

lemma (in comm_group) inv_mult:
"[| x ∈ carrier G; y ∈ carrier G |] ==> inv (x ⊗ y) = inv x ⊗ inv y"

(* Next three lemmas contributed by Paulo. *)

lemma (in comm_monoid) hom_imp_img_comm_monoid:
assumes "h ∈ hom G H"
shows "comm_monoid (H ⦇ carrier := h  (carrier G), one := h 𝟭⇘G⇙ ⦈)" (is "comm_monoid ?h_img")
proof (rule monoid.monoid_comm_monoidI)
show "monoid ?h_img"
using hom_imp_img_monoid[OF assms] .
next
fix x y assume "x ∈ carrier ?h_img" "y ∈ carrier ?h_img"
then obtain g1 g2
where g1: "g1 ∈ carrier G" "x = h g1"
and g2: "g2 ∈ carrier G" "y = h g2"
by auto
have "x ⊗⇘(?h_img)⇙ y = h (g1 ⊗ g2)"
using g1 g2 assms unfolding hom_def by auto
also have " ... = h (g2 ⊗ g1)"
using m_comm[OF g1(1) g2(1)] by simp
also have " ... = y ⊗⇘(?h_img)⇙ x"
using g1 g2 assms unfolding hom_def by auto
finally show "x ⊗⇘(?h_img)⇙ y = y ⊗⇘(?h_img)⇙ x" .
qed

lemma (in comm_group) hom_imp_img_comm_group:
assumes "h ∈ hom G H"
shows "comm_group (H ⦇ carrier := h  (carrier G), one := h 𝟭⇘G⇙ ⦈)"
unfolding comm_group_def
using hom_imp_img_group[OF assms] hom_imp_img_comm_monoid[OF assms] by simp

lemma (in comm_group) iso_imp_img_comm_group:
assumes "h ∈ iso G H"
shows "comm_group (H ⦇ one := h 𝟭⇘G⇙ ⦈)"
proof -
let ?h_img = "H ⦇ carrier := h  (carrier G), one := h 𝟭 ⦈"
have "comm_group ?h_img"
using hom_imp_img_comm_group[of h H] assms unfolding iso_def by auto
moreover have "carrier H = carrier ?h_img"
using assms unfolding iso_def bij_betw_def by simp
hence "H ⦇ one := h 𝟭 ⦈ = ?h_img"
by simp
ultimately show ?thesis by simp
qed

lemma (in comm_group) iso_imp_comm_group:
assumes "G ≅ H" "monoid H"
shows "comm_group H"
proof -
obtain h where h: "h ∈ iso G H"
using assms(1) unfolding is_iso_def by auto
hence comm_gr: "comm_group (H ⦇ one := h 𝟭 ⦈)"
using iso_imp_img_comm_group[of h H] by simp
hence "⋀x. x ∈ carrier H ⟹ h 𝟭 ⊗⇘H⇙ x = x"
using monoid.l_one[of "H ⦇ one := h 𝟭 ⦈"] unfolding comm_group_def comm_monoid_def by simp
moreover have "h 𝟭 ∈ carrier H"
using h one_closed unfolding iso_def hom_def by auto
ultimately have "h 𝟭 = 𝟭⇘H⇙"
using monoid.one_unique[OF assms(2), of "h 𝟭"] by simp
hence "H = H ⦇ one := h 𝟭 ⦈"
by simp
thus ?thesis
using comm_gr by simp
qed

(*A subgroup of a subgroup is a subgroup of the group*)
lemma (in group) incl_subgroup:
assumes "subgroup J G"
and "subgroup I (G⦇carrier:=J⦈)"
shows "subgroup I G" unfolding subgroup_def
proof
have H1: "I ⊆ carrier (G⦇carrier:=J⦈)" using assms(2) subgroup.subset by blast
also have H2: "...⊆J" by simp
also  have "...⊆(carrier G)"  by (simp add: assms(1) subgroup.subset)
finally have H: "I ⊆ carrier G" by simp
have "(⋀x y. ⟦x ∈ I ; y ∈ I⟧ ⟹ x ⊗ y ∈ I)" using assms(2) by (auto simp add: subgroup_def)
thus  "I ⊆ carrier G ∧ (∀x y. x ∈ I ⟶ y ∈ I ⟶ x ⊗ y ∈ I)"  using H by blast
have K: "𝟭 ∈ I" using assms(2) by (auto simp add: subgroup_def)
have "(⋀x. x ∈ I ⟹ inv x ∈ I)" using assms  subgroup.m_inv_closed H
by (metis H1 H2 m_inv_consistent subsetCE)
thus "𝟭 ∈ I ∧ (∀x. x ∈ I ⟶ inv x ∈ I)" using K by blast
qed

(*A subgroup included in another subgroup is a subgroup of the subgroup*)
lemma (in group) subgroup_incl:
assumes "subgroup I G" and "subgroup J G" and "I ⊆ J"
shows "subgroup I (G ⦇ carrier := J ⦈)"
using group.group_incl_imp_subgroup[of "G ⦇ carrier := J ⦈" I]
assms(1-2)[THEN subgroup.subgroup_is_group[OF _ group_axioms]] assms(3) by auto

subsection ‹The Lattice of Subgroups of a Group›

text_raw ‹\label{sec:subgroup-lattice}›

theorem (in group) subgroups_partial_order:
"partial_order ⦇carrier = {H. subgroup H G}, eq = (=), le = (⊆)⦈"
by standard simp_all

lemma (in group) subgroup_self:
"subgroup (carrier G) G"
by (rule subgroupI) auto

lemma (in group) subgroup_imp_group:
"subgroup H G ==> group (G⦇carrier := H⦈)"
by (erule subgroup.subgroup_is_group) (rule group_axioms)

lemma (in group) is_monoid [intro, simp]:
"monoid G"
by (auto intro: monoid.intro m_assoc)

lemma (in group) subgroup_mult_equality:
"⟦ subgroup H G; h1 ∈ H; h2 ∈ H ⟧ ⟹  h1 ⊗⇘G ⦇ carrier := H ⦈⇙ h2 = h1 ⊗ h2"
unfolding subgroup_def by simp

theorem (in group) subgroups_Inter:
assumes subgr: "(⋀H. H ∈ A ⟹ subgroup H G)"
and not_empty: "A ≠ {}"
shows "subgroup (⋂A) G"
proof (rule subgroupI)
from subgr [THEN subgroup.subset] and not_empty
show "⋂A ⊆ carrier G" by blast
next
from subgr [THEN subgroup.one_closed]
show "⋂A ≠ {}" by blast
next
fix x assume "x ∈ ⋂A"
with subgr [THEN subgroup.m_inv_closed]
show "inv x ∈ ⋂A" by blast
next
fix x y assume "x ∈ ⋂A" "y ∈ ⋂A"
with subgr [THEN subgroup.m_closed]
show "x ⊗ y ∈ ⋂A" by blast
qed

lemma (in group) subgroups_Inter_pair :
assumes  "subgroup I G"
and  "subgroup J G"
shows "subgroup (I∩J) G" using subgroups_Inter[ where ?A = "{I,J}"] assms by auto

theorem (in group) subgroups_complete_lattice:
"complete_lattice ⦇carrier = {H. subgroup H G}, eq = (=), le = (⊆)⦈"
(is "complete_lattice ?L")
proof (rule partial_order.complete_lattice_criterion1)
show "partial_order ?L" by (rule subgroups_partial_order)
next
have "greatest ?L (carrier G) (carrier ?L)"
by (unfold greatest_def) (simp add: subgroup.subset subgroup_self)
then show "∃G. greatest ?L G (carrier ?L)" ..
next
fix A
assume L: "A ⊆ carrier ?L" and non_empty: "A ≠ {}"
then have Int_subgroup: "subgroup (⋂A) G"
by (fastforce intro: subgroups_Inter)
have "greatest ?L (⋂A) (Lower ?L A)" (is "greatest _ ?Int _")
proof (rule greatest_LowerI)
fix H
assume H: "H ∈ A"
with L have subgroupH: "subgroup H G" by auto
from subgroupH have groupH: "group (G ⦇carrier := H⦈)" (is "group ?H")
by (rule subgroup_imp_group)
from groupH have monoidH: "monoid ?H"
by (rule group.is_monoid)
from H have Int_subset: "?Int ⊆ H" by fastforce
then show "le ?L ?Int H" by simp
next
fix H
assume H: "H ∈ Lower ?L A"
with L Int_subgroup show "le ?L H ?Int"
by (fastforce simp: Lower_def intro: Inter_greatest)
next
show "A ⊆ carrier ?L" by (rule L)
next
show "?Int ∈ carrier ?L" by simp (rule Int_subgroup)
qed
then show "∃I. greatest ?L I (Lower ?L A)" ..
qed

text ‹
Show that the units in any monoid give rise to a group.

The file Residues.thy provides some infrastructure to use
facts about the unit group within the ring locale.
›

definition units_of :: "('a, 'b) monoid_scheme ⇒ 'a monoid"
where "units_of G =
⦇carrier = Units G, Group.monoid.mult = Group.monoid.mult G, one  = one G⦈"

lemma (in monoid) units_group: "group (units_of G)"
proof -
have "⋀x y z. ⟦x ∈ Units G; y ∈ Units G; z ∈ Units G⟧ ⟹ x ⊗ y ⊗ z = x ⊗ (y ⊗ z)"
moreover have "⋀x. x ∈ Units G ⟹ ∃y∈Units G. y ⊗ x = 𝟭"
using Units_l_inv by blast
ultimately show ?thesis
unfolding units_of_def
by (force intro!: groupI)
qed

lemma (in comm_monoid) units_comm_group: "comm_group (units_of G)"
proof -
have "⋀x y. ⟦x ∈ carrier (units_of G); y ∈ carrier (units_of G)⟧
⟹ x ⊗⇘units_of G⇙ y = y ⊗⇘units_of G⇙ x"
by (simp add: Units_closed m_comm units_of_def)
then show ?thesis
by (rule group.group_comm_groupI [OF units_group]) auto
qed

lemma units_of_carrier: "carrier (units_of G) = Units G"
by (auto simp: units_of_def)

lemma units_of_mult: "mult (units_of G) = mult G"
by (auto simp: units_of_def)

lemma units_of_one: "one (units_of G) = one G"
by (auto simp: units_of_def)

lemma (in monoid) units_of_inv:
assumes "x ∈ Units G"
shows "m_inv (units_of G) x = m_inv G x"
by (simp add: assms group.inv_equality units_group units_of_carrier units_of_mult units_of_one)

lemma units_of_units [simp] : "Units (units_of G) = Units G"
unfolding units_of_def Units_def by force

lemma (in group) surj_const_mult: "a ∈ carrier G ⟹ (λx. a ⊗ x)  carrier G = carrier G"
by (metis inv_closed inv_solve_left m_closed)

lemma (in group) l_cancel_one [simp]: "x ∈ carrier G ⟹ a ∈ carrier G ⟹ x ⊗ a = x ⟷ a = one G"
by (metis Units_eq Units_l_cancel monoid.r_one monoid_axioms one_closed)

lemma (in group) r_cancel_one [simp]: "x ∈ carrier G ⟹ a ∈ carrier G ⟹ a ⊗ x = x ⟷ a = one G"
by (metis monoid.l_one monoid_axioms one_closed right_cancel)

lemma (in group) l_cancel_one' [simp]: "x ∈ carrier G ⟹ a ∈ carrier G ⟹ x = x ⊗ a ⟷ a = one G"
using l_cancel_one by fastforce

lemma (in group) r_cancel_one' [simp]: "x ∈ carrier G ⟹ a ∈ carrier G ⟹ x = a ⊗ x ⟷ a = one G"
using r_cancel_one by fastforce

end