Skip to content

Commit

Permalink
Check arguments to FreeSemigroup more thoroughly
Browse files Browse the repository at this point in the history
See #1385

It was previously possible to use `FreeSemigroup` to create a
seemingly free semigroup with zero generators (rank zero), but
this object believed itself to be infinite, and to contain elements
(such a semigroup would be empty). Note that free semigroups of rank
zero are not documented as being supported.

Some other ways that one might want to create free semigroups of
rank zero led to unhelpful error messages, and in general, the
argument checking of `FreeSemigroup` was not very thorough and gave
sometimes unhelpful error messages.

In this commit, I overhaul the way that `FreeSemigroup` checks its
arguments, in particular always disallowing a free semigroup of rank
zero, and overall making the checking more robust, and making the error
messages more descriptive.

This addresses the _bugs_ reported in issue #1385, although it does not
address the _feature request_ in that issue to support free semigroups of
rank zero.

I also took the opportunity to fix a typo in the documentation
for `FreeSemigroup`, and to make a clarification. Further improvements
could be made to this documentation, but I leave that to the future.
  • Loading branch information
wilfwilson committed Apr 10, 2021
1 parent f541fa8 commit a83c443
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 37 deletions.
16 changes: 8 additions & 8 deletions lib/semigrp.gd
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,10 @@ DeclareAttribute("CayleyGraphDualSemigroup",IsSemigroup);

#############################################################################
##
#F FreeSemigroup( [<wfilt>,]<rank> )
#F FreeSemigroup( [<wfilt>,]<rank>, <name> )
#F FreeSemigroup( [<wfilt>,]<name1>, <name2>, ... )
#F FreeSemigroup( [<wfilt>,]<names> )
#F FreeSemigroup( [<wfilt>,]infinity, <name>, <init> )
#F FreeSemigroup( [<wfilt>, ]<rank>[, <name>] )
#F FreeSemigroup( [<wfilt>, ]<name1>[, <name2>, ...] )
#F FreeSemigroup( [<wfilt>, ]<names> )
#F FreeSemigroup( [<wfilt>, ]infinity[, <name>[, <init>]] )
##
## <#GAPDoc Label="FreeSemigroup">
## <ManSection>
Expand All @@ -292,17 +291,18 @@ DeclareAttribute("CayleyGraphDualSemigroup",IsSemigroup);
## Label="for various names"/>
## <Func Name="FreeSemigroup" Arg='[wfilt, ]names'
## Label="for a list of names"/>
## <Func Name="FreeSemigroup" Arg='[wfilt, ]infinity, name, init'
## <Func Name="FreeSemigroup" Arg='[wfilt, ]infinity[, name[, init]]'
## Label="for infinitely many generators"/>
##
## <Description>
## Called with a positive integer <A>rank</A>,
## <Ref Func="FreeSemigroup" Label="for given rank"/> returns
## a free semigroup on <A>rank</A> generators.
## If the optional argument <A>name</A> is given then the generators are
## If the optional argument <A>name</A> (a string) is given,
## then the generators are
## printed as <A>name</A><C>1</C>, <A>name</A><C>2</C> etc.,
## that is, each name is the concatenation of the string <A>name</A> and an
## integer from <C>1</C> to <A>range</A>.
## integer from <C>1</C> to <A>rank</A>.
## The default for <A>name</A> is the string <C>"s"</C>.
## <P/>
## Called in the second form,
Expand Down
122 changes: 93 additions & 29 deletions lib/smgrpfre.gi
Original file line number Diff line number Diff line change
Expand Up @@ -377,50 +377,113 @@ InstallMethod( GeneratorsSmallest,

#############################################################################
##
#F FreeSemigroup( <rank> )
#F FreeSemigroup( <rank>, <name> )
#F FreeSemigroup( <name1>, <name2>, ... )
#F FreeSemigroup( <names> )
#F FreeSemigroup( infinity, <name>, <init> )
#F FreeSemigroup( [<wfilt>, ]<rank>[, <name>] )
#F FreeSemigroup( [<wfilt>, ]<name1>[, <name2>, ...] )
#F FreeSemigroup( [<wfilt>, ]<names> )
#F FreeSemigroup( [<wfilt>, ]infinity[, <name>[, <init>]] )
##
InstallGlobalFunction( FreeSemigroup, function( arg )
local names, # list of generators names
F, # family of free semigroup element objects
zarg,
lesy, # filter for letter or syllable words family
S; # free semigroup, result
S, # free semigroup, result
err, # string; helpful error message
form, # string: assumed argument form of call to FreeSemigroup
rank,
name,
init;

lesy:=IsLetterWordsFamily; # default:
if IsFilter(arg[1]) then
if not IsEmpty(arg) and IsFilter(arg[1]) then
lesy:=arg[1];
zarg:=arg{[2..Length(arg)]};
else
zarg:=arg;
fi;

# Get and check the argument list, and construct names if necessary.
if Length( zarg ) = 1 and zarg[1] = infinity then
names:= InfiniteListOfNames( "s" );
elif Length( zarg ) = 2 and zarg[1] = infinity then
names:= InfiniteListOfNames( zarg[2] );
elif Length( zarg ) = 3 and zarg[1] = infinity then
names:= InfiniteListOfNames( zarg[2], zarg[3] );
elif Length( zarg ) = 1 and IsInt( zarg[1] ) and 0 < zarg[1] then
names:= List( [ 1 .. zarg[1] ],
i -> Concatenation( "s", String(i) ) );
MakeImmutable( names );
elif Length( zarg ) = 2 and IsInt( zarg[1] ) and 0 < zarg[1] then
names:= List( [ 1 .. zarg[1] ],
i -> Concatenation( zarg[2], String(i) ) );
MakeImmutable( names );
elif 1 <= Length( zarg ) and ForAll( zarg, IsString ) then
names:= zarg;
elif Length( zarg ) = 1 and IsList( zarg[1] )
and not IsEmpty( zarg[1] )
and ForAll( zarg[1], IsString ) then
names:= zarg[1];
# Process and validate the argument list, constructing names if necessary
err := "";

if Length( zarg ) = 0 or zarg[1] = 0
or (Length(zarg) = 1 and IsList( zarg[1] ) and IsEmpty( zarg[1] )) then
Error("free semigroups of rank zero are not supported");

# FreeSemigroup( <rank>[, <name> ] )
elif IsPosInt( zarg[1] ) and Length( zarg ) <= 2 then

# Get default and optional arguments
rank := zarg[1];
if Length( zarg ) = 1 then
name := "s";
else
name := zarg[2];
fi;

# Error checking
if not IsString( name ) then
form := "<rank>, <name>";
err := "<name> must be a string";

# Construct names
else
names:= List( [ 1 .. rank ], i -> Concatenation( name, String(i) ) );
MakeImmutable( names );
fi;

# FreeSemigroup( <name1>[, <name2>, ...] ), or a list of such arguments
elif ForAll( zarg, IsString ) or Length( zarg ) = 1 and IsList( zarg[1] ) then
if Length( zarg ) = 1 and not IsString( zarg[1] ) then
form := "[ <name1>, <name2>, ... ]";
names:= zarg[1];
else
form := "<name1>, <name2>, ...";
names:= zarg;
fi;
if not ForAll( names, s -> IsString(s) and not IsEmpty(s) ) then
err := "the names must be nonempty strings";
fi;

# FreeSemigroup( infinity[, <name>[, <init>]] )
elif zarg[1] = infinity and Length( zarg ) <= 3 then

# Get default and optional arguments
name := "s";
init := [];
if Length( zarg ) = 3 then
form := "infinity, <name>, <init>";
name := zarg[2];
init := zarg[3];
elif Length( zarg ) = 2 then
form := "infinity, <name>";
name := zarg[2];
fi;

# Error checking
if not IsString( name ) then
err := "<name> must be a string";
fi;
if not ( IsList( init ) and ForAll( init, s -> IsString(s) and not IsEmpty(s) ) ) then
if not IsEmpty(err) then
Append(err, " and ");
fi;
Append(err, "<init> must be a list of nonempty strings");
fi;

# Construct names
if IsEmpty(err) then
names:= InfiniteListOfNames( name, init );
fi;

else
Error("usage: FreeSemigroup(<name1>,<name2>..),FreeSemigroup(<rank>)");
ErrorNoReturn("""usage: FreeSemigroup( [<wfilt>, ]<rank>[, <name>] )
FreeSemigroup( [<wfilt>, ]<name1>[, <name2>, ...] )
FreeSemigroup( [<wfilt>, ]<names> )
FreeSemigroup( [<wfilt>, ]infinity[, <name>[, <init>]] )""");
fi;

if not IsEmpty(err) then
ErrorNoReturn(StringFormatted("FreeSemigroup( {} ): {}", form, err));
fi;

# deal with letter words family types
Expand Down Expand Up @@ -457,6 +520,7 @@ InstallGlobalFunction( FreeSemigroup, function( arg )
SetIsFreeSemigroup(S,true);
SetIsWholeFamily( S, true );
SetIsTrivial( S, false );
SetIsCommutative( S, Length(names) = 1 );
return S;
end );

Expand Down
11 changes: 11 additions & 0 deletions tst/testbugfix/2021-04-08-empty-FreeSemigroup.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# See https://github.com/gap-system/gap/issues/1385
gap> FreeSemigroup();
Error, free semigroups of rank zero are not supported
gap> FreeSemigroup([]);
Error, free semigroups of rank zero are not supported
gap> FreeSemigroup("");
Error, free semigroups of rank zero are not supported
gap> FreeSemigroup(0);
Error, free semigroups of rank zero are not supported
gap> FreeSemigroup(0, "name");
Error, free semigroups of rank zero are not supported
127 changes: 127 additions & 0 deletions tst/testinstall/smgrpfre.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#@local F
gap> START_TEST("smgrpfre.tst");

# FreeSemigroup
gap> FreeSemigroup(fail);
Error, usage: FreeSemigroup( [<wfilt>, ]<rank>[, <name>] )
FreeSemigroup( [<wfilt>, ]<name1>[, <name2>, ...] )
FreeSemigroup( [<wfilt>, ]<names> )
FreeSemigroup( [<wfilt>, ]infinity[, <name>[, <init>]] )

# FreeSemigroup: rank 0
gap> FreeSemigroup();
Error, free semigroups of rank zero are not supported
gap> FreeSemigroup([]);
Error, free semigroups of rank zero are not supported
gap> FreeSemigroup("");
Error, free semigroups of rank zero are not supported
gap> FreeSemigroup(0);
Error, free semigroups of rank zero are not supported
gap> FreeSemigroup(0, "name");
Error, free semigroups of rank zero are not supported

# FreeSemigroup(infinity[, name[, init]])
gap> FreeSemigroup(infinity);
<free semigroup on the generators [ s1, s2, ... ]>
gap> FreeSemigroup(infinity, fail);
Error, FreeSemigroup( infinity, <name> ): <name> must be a string
gap> FreeSemigroup(infinity, []);
<free semigroup on the generators [ 1, 2, ... ]>
gap> FreeSemigroup(infinity, "");
<free semigroup on the generators [ 1, 2, ... ]>
gap> FreeSemigroup(infinity, "nicename");
<free semigroup on the generators [ nicename1, nicename2, ... ]>
gap> FreeSemigroup(infinity, fail, fail);
Error, FreeSemigroup( infinity, <name>, <init> ): <name> must be a string and \
<init> must be a list of nonempty strings
gap> FreeSemigroup(infinity, "nicename", fail);
Error, FreeSemigroup( infinity, <name>, <init> ): <init> must be a list of non\
empty strings
gap> FreeSemigroup(infinity, "gen", []);
<free semigroup on the generators [ gen1, gen2, ... ]>
gap> FreeSemigroup(infinity, "gen", [""]);
Error, FreeSemigroup( infinity, <name>, <init> ): <init> must be a list of non\
empty strings
gap> FreeSemigroup(infinity, "gen", ["starter"]);
<free semigroup on the generators [ starter, gen2, ... ]>
gap> FreeSemigroup(infinity, "gen", ["starter", ""]);
Error, FreeSemigroup( infinity, <name>, <init> ): <init> must be a list of non\
empty strings
gap> F := FreeSemigroup(infinity, "gen", ["starter", "second", "third"]);
<free semigroup on the generators [ starter, second, ... ]>
gap> GeneratorsOfSemigroup(F){[1 .. 4]};
[ starter, second, third, gen4 ]
gap> FreeSemigroup(infinity, "gen", ["starter"], fail);
Error, usage: FreeSemigroup( [<wfilt>, ]<rank>[, <name>] )
FreeSemigroup( [<wfilt>, ]<name1>[, <name2>, ...] )
FreeSemigroup( [<wfilt>, ]<names> )
FreeSemigroup( [<wfilt>, ]infinity[, <name>[, <init>]] )

# FreeSemigroup(rank[, name])
gap> F := FreeSemigroup(1);
<free semigroup on the generators [ s1 ]>
gap> HasIsCommutative(F) and IsCommutative(F);
true
gap> F := FreeSemigroup(2);
<free semigroup on the generators [ s1, s2 ]>
gap> HasIsCommutative(F) and not IsCommutative(F);
true
gap> F := FreeSemigroup(10);
<free semigroup on the generators [ s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 ]>
gap> F := FreeSemigroup(3, fail);
Error, FreeSemigroup( <rank>, <name> ): <name> must be a string
gap> F := FreeSemigroup(4, "");
<free semigroup on the generators [ 1, 2, 3, 4 ]>
gap> F := FreeSemigroup(5, []);
<free semigroup on the generators [ 1, 2, 3, 4, 5 ]>
gap> F := FreeSemigroup(4, "cheese");
<free semigroup on the generators [ cheese1, cheese2, cheese3, cheese4 ]>
gap> FreeSemigroup(3, "car", fail);
Error, usage: FreeSemigroup( [<wfilt>, ]<rank>[, <name>] )
FreeSemigroup( [<wfilt>, ]<name1>[, <name2>, ...] )
FreeSemigroup( [<wfilt>, ]<names> )
FreeSemigroup( [<wfilt>, ]infinity[, <name>[, <init>]] )

# FreeSemigroup( <name1>[, <name2>, ...] )
gap> FreeSemigroup("", "second");
Error, FreeSemigroup( <name1>, <name2>, ... ): the names must be nonempty stri\
ngs
gap> FreeSemigroup("first", "");
Error, FreeSemigroup( <name1>, <name2>, ... ): the names must be nonempty stri\
ngs
gap> FreeSemigroup("first", []);
Error, FreeSemigroup( <name1>, <name2>, ... ): the names must be nonempty stri\
ngs
gap> FreeSemigroup([], []);
Error, FreeSemigroup( <name1>, <name2>, ... ): the names must be nonempty stri\
ngs
gap> FreeSemigroup("bacon", "eggs", "beans");
<free semigroup on the generators [ bacon, eggs, beans ]>
gap> FreeSemigroup("shed");
<free semigroup on the generators [ shed ]>

# FreeSemigroup( [ <name1>[, <name2>, ...] ] )
gap> FreeSemigroup(["", "second"]);
Error, FreeSemigroup( [ <name1>, <name2>, ... ] ): the names must be nonempty \
strings
gap> FreeSemigroup(["first", ""]);
Error, FreeSemigroup( [ <name1>, <name2>, ... ] ): the names must be nonempty \
strings
gap> FreeSemigroup(["first", []]);
Error, FreeSemigroup( [ <name1>, <name2>, ... ] ): the names must be nonempty \
strings
gap> FreeSemigroup([[], []]);
Error, FreeSemigroup( [ <name1>, <name2>, ... ] ): the names must be nonempty \
strings
gap> FreeSemigroup(["bacon", "eggs", "beans"]);
<free semigroup on the generators [ bacon, eggs, beans ]>
gap> FreeSemigroup(["grid"]);
<free semigroup on the generators [ grid ]>
gap> FreeSemigroup(["grid"], fail);
Error, usage: FreeSemigroup( [<wfilt>, ]<rank>[, <name>] )
FreeSemigroup( [<wfilt>, ]<name1>[, <name2>, ...] )
FreeSemigroup( [<wfilt>, ]<names> )
FreeSemigroup( [<wfilt>, ]infinity[, <name>[, <init>]] )

#
gap> STOP_TEST( "smgrpfre.tst", 1);

0 comments on commit a83c443

Please sign in to comment.