/usr/share/gap/lib/monofree.gi is in gap-libs 4r7p9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | #############################################################################
##
#W monofree.gi GAP library Thomas Breuer
##
##
#Y Copyright (C) 1997, Lehrstuhl D für Mathematik, RWTH Aachen, Germany
#Y (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland
#Y Copyright (C) 2002 The GAP Group
##
## This file contains the methods for free monoids.
##
#############################################################################
##
#M IsWholeFamily( <M> ) . . . . . . . . . is a free monoid the whole family
##
## <M> contains the whole family of its elements if and only if all
## magma generators of the family are among the monoid generators of <M>.
##
InstallMethod( IsWholeFamily,
"for a free monoid",
[ IsAssocWordWithOneCollection and IsMonoid ],
M -> IsSubset( MagmaGeneratorsOfFamily( FamilyObj( M ) ),
GeneratorsOfMagmaWithOne( M ) ) );
#############################################################################
##
#M Iterator( <M> ) . . . . . . . . . . . . . . . iterator for a free monoid
##
## Iterator and enumerator of free monoids are implemented very similar
## to iterator and enumerator for free semigroups.
## The only difference is the existence of the empty word.
##
InstallMethod( Iterator,
"for a free monoid",
[ IsAssocWordWithOneCollection and IsWholeFamily ],
function( M )
# A free group needs another method.
# A trivial group needs another method.
if IsAssocWordWithInverseCollection( M ) or IsTrivial( M ) then
TryNextMethod();
fi;
return IteratorByFunctions( rec(
IsDoneIterator := ReturnFalse,
NextIterator := NextIterator_FreeSemigroup,
ShallowCopy := ShallowCopy_FreeSemigroup,
family := ElementsFamily( FamilyObj( M ) ),
nrgenerators := Length( GeneratorsOfMagmaWithOne( M ) ),
exp := 0,
word := [],
counter := [ 0, 0 ],
length := 0 ) );
end );
#############################################################################
##
#M Enumerator( <M> ) . . . . . . . . . . . . . enumerator for a free monoid
##
InstallMethod( Enumerator,
"for a free monoid",
[ IsAssocWordWithOneCollection and IsWholeFamily and IsMonoid ],
function( M )
# A free group needs another method.
# A trivial group needs another method.
if IsAssocWordWithInverseCollection( M ) or IsTrivial( M ) then
TryNextMethod();
fi;
return EnumeratorByFunctions( M, rec(
ElementNumber := ElementNumber_FreeMonoid,
NumberElement := NumberElement_FreeMonoid,
family := ElementsFamily( FamilyObj( M ) ),
nrgenerators := Length( ElementsFamily(
FamilyObj( M ) )!.names ) ) );
end );
#############################################################################
##
#M Random( <M> ) . . . . . . . . . . . . . . random element of a free monoid
##
#T use better method for the whole family, and for abelian monoids
##
InstallMethod( Random,
"for a free monoid",
[ IsMonoid and IsAssocWordWithOneCollection ],
function( M )
local len, result, gens, i;
# Get a random length for the word.
len:= Random( Integers );
if 0 < len then
len:= 2 * len;
elif len < 0 then
len:= -2 * len - 1;
else
return One( M );
fi;
# Multiply 'len' random generators.
gens:= GeneratorsOfMagmaWithOne( M );
result:= Random( gens );
for i in [ 2 .. len ] do
result:= result * Random( gens );
od;
# Return the result.
return result;
end );
#############################################################################
##
#M Size( <M> ) . . . . . . . . . . . . . . . . . . . . size of a free monoid
##
InstallMethod( Size,
"for a free monoid",
[ IsMonoid and IsAssocWordWithOneCollection ],
function( M )
if IsTrivial( M ) then
return 1;
else
return infinity;
fi;
end );
#############################################################################
##
#A One( <Fam> )
##
InstallOtherMethod( One,
"for a family of free monoid elements",
[ IsAssocWordWithOneFamily ],
F -> ObjByExtRep( F, 1, 1, [] ) );
#############################################################################
##
#A MagmaGeneratorsOfFamily( <F> )
##
InstallMethod( MagmaGeneratorsOfFamily,
"for a family of free monoid elements",
[ IsAssocWordWithOneFamily ],
function( F )
local gens;
# Make the generators.
gens:= List( [ 1 .. Length( F!.names ) ],
i -> ObjByExtRep( F, 1, 1, [ i, 1 ] ) );
Add( gens, One( F ) );
# Return the magma generators.
return gens;
end );
# GeneratorsOfMonoid returns the generators in ascending order
InstallMethod( GeneratorsSmallest,
"for a free monoid",
[ IsFreeMonoid ],
GeneratorsOfMonoid);
#############################################################################
##
#F FreeMonoid( <rank> )
#F FreeMonoid( <rank>, <name> )
#F FreeMonoid( <name1>, <name2>, ... )
#F FreeMonoid( <names> )
#F FreeMonoid( infinity, <name>, <init> )
##
InstallGlobalFunction( FreeMonoid, function( arg )
local names, # list of generators names
F, # family of free monoid element objects
zarg,
lesy, # filter for letter or syllable words family
M; # free monoid, result
lesy:=IsLetterWordsFamily; # default
if 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( "m" );
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( "m", 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 Length( zarg ) = 1 and IsList( zarg[1] ) and IsEmpty( zarg[1] ) then
names:= zarg[1];
elif 1 <= Length( zarg ) and ForAll( zarg, IsString ) then
names:= zarg;
elif Length( zarg ) = 1 and IsList( zarg[1] )
and ForAll( zarg[1], IsString ) then
names:= zarg[1];
else
Error("usage: FreeMonoid(<name1>,<name2>..) or FreeMonoid(<rank>)");
fi;
# Handle the trivial case.
if IsEmpty( names ) then
M:=FreeGroup( 0 );
# we still need to set some monoid specific entries to keep
# the monoid code happy
F:=ElementsFamily(FamilyObj(M));
FamilyObj(M)!.wholeMonoid:= M;
F!.freeMonoid:=M;
return M;
fi;
# deal with letter words family types
if lesy=IsLetterWordsFamily then
if Length(names)>127 then
lesy:=IsWLetterWordsFamily;
else
lesy:=IsBLetterWordsFamily;
fi;
elif lesy=IsBLetterWordsFamily and Length(names)>127 then
lesy:=IsWLetterWordsFamily;
fi;
# Construct the family of element objects of our monoid.
F:= NewFamily( "FreeMonoidElementsFamily", IsAssocWordWithOne,
CanEasilySortElements, # the free monoid can.
CanEasilySortElements # the free monoid can.
and lesy);
# Install the data (names, no. of bits available for exponents, types).
StoreInfoFreeMagma( F, names, IsAssocWordWithOne );
# Make the monoid
if IsFinite( names ) then
M:= MonoidByGenerators( List( [ 1 .. Length( names ) ],
i -> ObjByExtRep( F, 1, 1, [ i, 1 ] ) ) );
else
M:= MonoidByGenerators( InfiniteListOfGenerators( F ) );
fi;
SetIsFreeMonoid(M,true);
SetIsWholeFamily( M, true );
SetIsTrivial( M, false );
# store the whole monoid in the family
FamilyObj(M)!.wholeMonoid:= M;
F!.freeMonoid:=M;
# Return the free monoid.
return M;
end );
#############################################################################
##
#M ViewObj( <M> ) . . . . . . . . . . . . . . . . . . . . for a free monoid
##
InstallMethod( ViewObj,
"for a free monoid containing the whole family",
[ IsMonoid and IsAssocWordCollection and IsWholeFamily ],
function( M )
if GAPInfo.ViewLength * 10 < Length( GeneratorsOfMagmaWithOne( M ) ) then
Print( "<free monoid with ", Length( GeneratorsOfMagmaWithOne( M ) ),
" generators>" );
else
Print( "<free monoid on the generators ",
GeneratorsOfMagmaWithOne( M ), ">" );
fi;
end );
#############################################################################
##
#E
|