Skip to content

Commit

Permalink
Introduce associativity check to AsSemigroup
Browse files Browse the repository at this point in the history
See #4030.

Fixes #4030.
  • Loading branch information
wilfwilson committed May 18, 2021
1 parent f610071 commit 8afa55d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/semigrp.gd
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ DeclareOperation( "SemigroupByGenerators", [ IsCollection ] );
## <Oper Name="AsSemigroup" Arg='C'/>
##
## <Description>
## If <A>C</A> is a collection whose elements form a semigroup
## (see&nbsp;<Ref Filt="IsSemigroup"/>) then <Ref Oper="AsSemigroup"/>
## If <A>C</A> is a collection whose elements can be multiplied via
## <Ref Oper="\*"/> and thereby forms a semigroup
## (see&nbsp;<Ref Filt="IsSemigroup"/>), then <Ref Oper="AsSemigroup"/>
## returns this semigroup.
## Otherwise <K>fail</K> is returned.
## </Description>
Expand Down
4 changes: 4 additions & 0 deletions lib/semigrp.gi
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,10 @@ InstallMethod( AsSemigroup,
function ( D )
local S, L;

if not IsAssociative( D ) then
return fail;
fi;

D := AsSSortedList( D );
L := ShallowCopy( D );
S := Submagma( SemigroupByGenerators( D ), [] );
Expand Down
52 changes: 52 additions & 0 deletions tst/testbugfix/2021-04-08-non-associative-semigroup.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# https://github.com/gap-system/gap/issues/4030
#
gap> T := [
> [ 1, 2, 6, 5, 4, 3 ],
> [ 2, 1, 4, 2, 2, 5 ],
> [ 6, 5, 1, 6, 6, 4 ],
> [ 5, 6, 3, 4, 1, 2 ],
> [ 4, 3, 2, 1, 5, 6 ],
> [ 3, 4, 5, 3, 3, 1 ]
> ];;
gap> M := MagmaByMultiplicationTable(T);
<magma with 6 generators>
gap> IsAssociative(M);
false
gap> AsSemigroup(M);
fail
gap> M := MagmaByMultiplicationTable(T);
<magma with 6 generators>
gap> AsSemigroup(M);
fail
gap> AsSemigroup(Elements(M));
fail

#
gap> T := [
> [ 1, 2, 3, 4, 5 ],
> [ 2, 2, 2, 5, 5 ],
> [ 3, 2, 3, 4, 5 ],
> [ 4, 2, 4, 3, 5 ],
> [ 5, 2, 5, 2, 5 ]
> ];;
gap> M := MagmaByMultiplicationTable(T);
<magma with 5 generators>
gap> IsAssociative(M);
true
gap> S := AsSemigroup(M);;
gap> IsSemigroup(S);
true
gap> Size(S);
5
gap> M := MagmaByMultiplicationTable(T);
<magma with 5 generators>
gap> S := AsSemigroup(M);;
gap> IsSemigroup(S);
true
gap> Size(S);
5
gap> S := AsSemigroup(Elements(M));;
gap> IsSemigroup(S);
true
gap> Size(S);
5
8 changes: 7 additions & 1 deletion tst/testinstall/magma.tst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#@local M,T
#@local F,M,T
gap> START_TEST( "magma.tst" );

#
Expand Down Expand Up @@ -35,6 +35,10 @@ gap> IsAssociative(M) or IsCommutative(M);
false
gap> Filtered(Combinations(Elements(M)), x -> Size(x) > 0 and IsAssociative(x));
[ [ m1 ], [ m1, m3 ], [ m2 ], [ m2, m4 ], [ m3 ], [ m4 ] ]
gap> AsSemigroup([Elements(M)[1], Elements(M)[2]]);
fail
gap> AsSemigroup([Elements(M)[1], Elements(M)[3]]);
<semigroup of size 2, with 2 generators>
gap> Filtered(Combinations(Elements(M)), x -> Size(x) > 0 and IsCommutative(x));
[ [ m1 ], [ m1, m2 ], [ m1, m2, m3 ], [ m1, m2, m5 ], [ m1, m3 ], [ m1, m5 ],
[ m2 ], [ m2, m3 ], [ m2, m4 ], [ m2, m5 ], [ m3 ], [ m4 ], [ m5 ] ]
Expand All @@ -47,6 +51,8 @@ gap> IsCommutative( F );
false
gap> Number( Combinations( F, 3 ), IsCommutative );
1
gap> AsSemigroup( F );
<semigroup of size 6, with 2 generators>

#
gap> STOP_TEST( "magma.tst" );

0 comments on commit 8afa55d

Please sign in to comment.